Fix: runas: less-than-zero comparison of an unsigned value
[lttng-tools.git] / src / common / runas.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10 #define _LGPL_SOURCE
11 #include <assert.h>
12 #include <fcntl.h>
13 #include <grp.h>
14 #include <limits.h>
15 #include <pwd.h>
16 #include <sched.h>
17 #include <signal.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <sys/wait.h>
24 #include <unistd.h>
25
26 #include <common/bytecode/bytecode.h>
27 #include <common/lttng-kernel.h>
28 #include <common/common.h>
29 #include <common/utils.h>
30 #include <common/compat/errno.h>
31 #include <common/compat/getenv.h>
32 #include <common/compat/string.h>
33 #include <common/unix.h>
34 #include <common/defaults.h>
35 #include <common/lttng-elf.h>
36 #include <common/thread.h>
37
38 #include <lttng/constant.h>
39
40 #include <common/sessiond-comm/sessiond-comm.h>
41 #include <common/filter/filter-ast.h>
42
43 #include "runas.h"
44
45 #define GETPW_BUFFER_FALLBACK_SIZE 4096
46
47 struct run_as_data;
48 struct run_as_ret;
49 typedef int (*run_as_fct)(struct run_as_data *data, struct run_as_ret *ret_value);
50
51 enum run_as_cmd {
52 RUN_AS_MKDIR,
53 RUN_AS_MKDIRAT,
54 RUN_AS_MKDIR_RECURSIVE,
55 RUN_AS_MKDIRAT_RECURSIVE,
56 RUN_AS_OPEN,
57 RUN_AS_OPENAT,
58 RUN_AS_UNLINK,
59 RUN_AS_UNLINKAT,
60 RUN_AS_RMDIR,
61 RUN_AS_RMDIRAT,
62 RUN_AS_RMDIR_RECURSIVE,
63 RUN_AS_RMDIRAT_RECURSIVE,
64 RUN_AS_RENAME,
65 RUN_AS_RENAMEAT,
66 RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET,
67 RUN_AS_EXTRACT_SDT_PROBE_OFFSETS,
68 RUN_AS_GENERATE_FILTER_BYTECODE,
69 };
70
71 struct run_as_mkdir_data {
72 int dirfd;
73 char path[LTTNG_PATH_MAX];
74 mode_t mode;
75 } LTTNG_PACKED;
76
77 struct run_as_open_data {
78 int dirfd;
79 char path[LTTNG_PATH_MAX];
80 int flags;
81 mode_t mode;
82 } LTTNG_PACKED;
83
84 struct run_as_unlink_data {
85 int dirfd;
86 char path[LTTNG_PATH_MAX];
87 } LTTNG_PACKED;
88
89 struct run_as_rmdir_data {
90 int dirfd;
91 char path[LTTNG_PATH_MAX];
92 int flags; /* enum lttng_directory_handle_rmdir_recursive_flags. */
93 } LTTNG_PACKED;
94
95 struct run_as_extract_elf_symbol_offset_data {
96 int fd;
97 char function[LTTNG_SYMBOL_NAME_LEN];
98 } LTTNG_PACKED;
99
100 struct run_as_extract_sdt_probe_offsets_data {
101 int fd;
102 char probe_name[LTTNG_SYMBOL_NAME_LEN];
103 char provider_name[LTTNG_SYMBOL_NAME_LEN];
104 } LTTNG_PACKED;
105
106 struct run_as_generate_filter_bytecode_data {
107 char filter_expression[LTTNG_FILTER_MAX_LEN];
108 } LTTNG_PACKED;
109
110 struct run_as_rename_data {
111 /*
112 * [0] = old_dirfd
113 * [1] = new_dirfd
114 */
115 int dirfds[2];
116 char old_path[LTTNG_PATH_MAX];
117 char new_path[LTTNG_PATH_MAX];
118 } LTTNG_PACKED;
119
120 struct run_as_open_ret {
121 int fd;
122 } LTTNG_PACKED;
123
124 struct run_as_extract_elf_symbol_offset_ret {
125 uint64_t offset;
126 } LTTNG_PACKED;
127
128 struct run_as_extract_sdt_probe_offsets_ret {
129 uint32_t num_offset;
130 uint64_t offsets[LTTNG_KERNEL_ABI_MAX_UPROBE_NUM];
131 } LTTNG_PACKED;
132
133 struct run_as_generate_filter_bytecode_ret {
134 /* A lttng_bytecode_filter struct with 'dynamic' payload. */
135 char bytecode[LTTNG_FILTER_MAX_LEN];
136 } LTTNG_PACKED;
137
138 struct run_as_data {
139 enum run_as_cmd cmd;
140 union {
141 struct run_as_mkdir_data mkdir;
142 struct run_as_open_data open;
143 struct run_as_unlink_data unlink;
144 struct run_as_rmdir_data rmdir;
145 struct run_as_rename_data rename;
146 struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset;
147 struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets;
148 struct run_as_generate_filter_bytecode_data generate_filter_bytecode;
149 } u;
150 uid_t uid;
151 gid_t gid;
152 } LTTNG_PACKED;
153
154 /*
155 * The run_as_ret structure holds the returned value and status of the command.
156 *
157 * The `u` union field holds the return value of the command; in most cases it
158 * represents the success or the failure of the command. In more complex
159 * commands, it holds a computed value.
160 *
161 * The _errno field is the errno recorded after the execution of the command.
162 *
163 * The _error fields is used the signify that return status of the command. For
164 * simple commands returning `int` the _error field will be the same as the
165 * ret_int field. In complex commands, it signify the success or failure of the
166 * command.
167 *
168 */
169 struct run_as_ret {
170 union {
171 int ret;
172 struct run_as_open_ret open;
173 struct run_as_extract_elf_symbol_offset_ret extract_elf_symbol_offset;
174 struct run_as_extract_sdt_probe_offsets_ret extract_sdt_probe_offsets;
175 struct run_as_generate_filter_bytecode_ret generate_filter_bytecode;
176 } u;
177 int _errno;
178 bool _error;
179 } LTTNG_PACKED;
180
181 #define COMMAND_IN_FDS(data_ptr) ({ \
182 int *fds = NULL; \
183 if (command_properties[data_ptr->cmd].in_fds_offset != -1) { \
184 fds = (int *) ((char *) data_ptr + command_properties[data_ptr->cmd].in_fds_offset); \
185 } \
186 fds; \
187 })
188
189 #define COMMAND_OUT_FDS(cmd, ret_ptr) ({ \
190 int *fds = NULL; \
191 if (command_properties[cmd].out_fds_offset != -1) { \
192 fds = (int *) ((char *) ret_ptr + command_properties[cmd].out_fds_offset); \
193 } \
194 fds; \
195 })
196
197 #define COMMAND_IN_FD_COUNT(data_ptr) ({ \
198 command_properties[data_ptr->cmd].in_fd_count; \
199 })
200
201 #define COMMAND_OUT_FD_COUNT(cmd) ({ \
202 command_properties[cmd].out_fd_count; \
203 })
204
205 #define COMMAND_USE_CWD_FD(data_ptr) command_properties[data_ptr->cmd].use_cwd_fd
206
207 struct run_as_command_properties {
208 /* Set to -1 when not applicable. */
209 ptrdiff_t in_fds_offset, out_fds_offset;
210 unsigned int in_fd_count, out_fd_count;
211 bool use_cwd_fd;
212 };
213
214 static const struct run_as_command_properties command_properties[] = {
215 [RUN_AS_MKDIR] = {
216 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
217 .in_fd_count = 1,
218 .out_fds_offset = -1,
219 .out_fd_count = 0,
220 .use_cwd_fd = true,
221 },
222 [RUN_AS_MKDIRAT] = {
223 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
224 .in_fd_count = 1,
225 .out_fds_offset = -1,
226 .out_fd_count = 0,
227 .use_cwd_fd = false,
228 },
229 [RUN_AS_MKDIR_RECURSIVE] = {
230 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
231 .in_fd_count = 1,
232 .out_fds_offset = -1,
233 .out_fd_count = 0,
234 .use_cwd_fd = true,
235 },
236 [RUN_AS_MKDIRAT_RECURSIVE] = {
237 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
238 .in_fd_count = 1,
239 .out_fds_offset = -1,
240 .out_fd_count = 0,
241 .use_cwd_fd = false,
242 },
243 [RUN_AS_OPEN] = {
244 .in_fds_offset = offsetof(struct run_as_data, u.open.dirfd),
245 .in_fd_count = 1,
246 .out_fds_offset = offsetof(struct run_as_ret, u.open.fd),
247 .out_fd_count = 1,
248 .use_cwd_fd = true,
249 },
250 [RUN_AS_OPENAT] = {
251 .in_fds_offset = offsetof(struct run_as_data, u.open.dirfd),
252 .in_fd_count = 1,
253 .out_fds_offset = offsetof(struct run_as_ret, u.open.fd),
254 .out_fd_count = 1,
255 .use_cwd_fd = false,
256 },
257 [RUN_AS_UNLINK] = {
258 .in_fds_offset = offsetof(struct run_as_data, u.unlink.dirfd),
259 .in_fd_count = 1,
260 .out_fds_offset = -1,
261 .out_fd_count = 0,
262 .use_cwd_fd = true,
263 },
264 [RUN_AS_UNLINKAT] = {
265 .in_fds_offset = offsetof(struct run_as_data, u.unlink.dirfd),
266 .in_fd_count = 1,
267 .out_fds_offset = -1,
268 .out_fd_count = 0,
269 .use_cwd_fd = false,
270 },
271 [RUN_AS_RMDIR_RECURSIVE] = {
272 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
273 .in_fd_count = 1,
274 .out_fds_offset = -1,
275 .out_fd_count = 0,
276 .use_cwd_fd = true,
277 },
278 [RUN_AS_RMDIRAT_RECURSIVE] = {
279 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
280 .in_fd_count = 1,
281 .out_fds_offset = -1,
282 .out_fd_count = 0,
283 .use_cwd_fd = false,
284 },
285 [RUN_AS_RMDIR] = {
286 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
287 .in_fd_count = 1,
288 .out_fds_offset = -1,
289 .out_fd_count = 0,
290 .use_cwd_fd = true,
291 },
292 [RUN_AS_RMDIRAT] = {
293 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
294 .in_fd_count = 1,
295 .out_fds_offset = -1,
296 .out_fd_count = 0,
297 .use_cwd_fd = false,
298 },
299 [RUN_AS_RENAME] = {
300 .in_fds_offset = offsetof(struct run_as_data, u.rename.dirfds),
301 .in_fd_count = 2,
302 .out_fds_offset = -1,
303 .out_fd_count = 0,
304 .use_cwd_fd = true,
305 },
306 [RUN_AS_RENAMEAT] = {
307 .in_fds_offset = offsetof(struct run_as_data, u.rename.dirfds),
308 .in_fd_count = 2,
309 .out_fds_offset = -1,
310 .out_fd_count = 0,
311 .use_cwd_fd = false,
312 },
313 [RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET] = {
314 .in_fds_offset = offsetof(struct run_as_data,
315 u.extract_elf_symbol_offset.fd),
316 .in_fd_count = 1,
317 .out_fds_offset = -1,
318 .out_fd_count = 0,
319 .use_cwd_fd = false,
320 },
321 [RUN_AS_EXTRACT_SDT_PROBE_OFFSETS] = {
322 .in_fds_offset = offsetof(struct run_as_data,
323 u.extract_sdt_probe_offsets.fd),
324 .in_fd_count = 1,
325 .out_fds_offset = -1,
326 .out_fd_count = 0,
327 .use_cwd_fd = false,
328 },
329 [RUN_AS_GENERATE_FILTER_BYTECODE] = {
330 .in_fds_offset = -1,
331 .in_fd_count = 0,
332 .out_fds_offset = -1,
333 .out_fd_count = 0,
334 .use_cwd_fd = false,
335 },
336 };
337
338 struct run_as_worker {
339 pid_t pid; /* Worker PID. */
340 int sockpair[2];
341 char *procname;
342 };
343
344 /* Single global worker per process (for now). */
345 static struct run_as_worker *global_worker;
346 /* Lock protecting the worker. */
347 static pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER;
348
349 #ifdef VALGRIND
350 static
351 int use_clone(void)
352 {
353 return 0;
354 }
355 #else
356 static
357 int use_clone(void)
358 {
359 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
360 }
361 #endif
362
363 /*
364 * Create recursively directory using the FULL path.
365 */
366 static
367 int _mkdirat_recursive(struct run_as_data *data, struct run_as_ret *ret_value)
368 {
369 const char *path;
370 mode_t mode;
371 struct lttng_directory_handle *handle;
372
373 path = data->u.mkdir.path;
374 mode = data->u.mkdir.mode;
375
376 handle = lttng_directory_handle_create_from_dirfd(data->u.mkdir.dirfd);
377 if (!handle) {
378 ret_value->_errno = errno;
379 ret_value->_error = true;
380 ret_value->u.ret = -1;
381 goto end;
382 }
383 /* Ownership of dirfd is transferred to the handle. */
384 data->u.mkdir.dirfd = -1;
385 /* Safe to call as we have transitioned to the requested uid/gid. */
386 ret_value->u.ret = lttng_directory_handle_create_subdirectory_recursive(
387 handle, path, mode);
388 ret_value->_errno = errno;
389 ret_value->_error = (ret_value->u.ret) ? true : false;
390 lttng_directory_handle_put(handle);
391 end:
392 return ret_value->u.ret;
393 }
394
395 static
396 int _mkdirat(struct run_as_data *data, struct run_as_ret *ret_value)
397 {
398 const char *path;
399 mode_t mode;
400 struct lttng_directory_handle *handle;
401
402 path = data->u.mkdir.path;
403 mode = data->u.mkdir.mode;
404
405 handle = lttng_directory_handle_create_from_dirfd(data->u.mkdir.dirfd);
406 if (!handle) {
407 ret_value->u.ret = -1;
408 ret_value->_errno = errno;
409 ret_value->_error = true;
410 goto end;
411 }
412 /* Ownership of dirfd is transferred to the handle. */
413 data->u.mkdir.dirfd = -1;
414 /* Safe to call as we have transitioned to the requested uid/gid. */
415 ret_value->u.ret = lttng_directory_handle_create_subdirectory(
416 handle, path, mode);
417 ret_value->_errno = errno;
418 ret_value->_error = (ret_value->u.ret) ? true : false;
419 lttng_directory_handle_put(handle);
420 end:
421 return ret_value->u.ret;
422 }
423
424 static
425 int _open(struct run_as_data *data, struct run_as_ret *ret_value)
426 {
427 int fd;
428 struct lttng_directory_handle *handle;
429
430 handle = lttng_directory_handle_create_from_dirfd(data->u.open.dirfd);
431 if (!handle) {
432 ret_value->_errno = errno;
433 ret_value->_error = true;
434 ret_value->u.ret = -1;
435 goto end;
436 }
437 /* Ownership of dirfd is transferred to the handle. */
438 data->u.open.dirfd = -1;
439
440 fd = lttng_directory_handle_open_file(handle,
441 data->u.open.path, data->u.open.flags,
442 data->u.open.mode);
443 if (fd < 0) {
444 ret_value->u.ret = -1;
445 ret_value->u.open.fd = -1;
446 } else {
447 ret_value->u.ret = 0;
448 ret_value->u.open.fd = fd;
449 }
450
451 ret_value->_errno = errno;
452 ret_value->_error = fd < 0;
453 lttng_directory_handle_put(handle);
454 end:
455 return ret_value->u.ret;
456 }
457
458 static
459 int _unlink(struct run_as_data *data, struct run_as_ret *ret_value)
460 {
461 struct lttng_directory_handle *handle;
462
463 handle = lttng_directory_handle_create_from_dirfd(data->u.unlink.dirfd);
464 if (!handle) {
465 ret_value->u.ret = -1;
466 ret_value->_errno = errno;
467 ret_value->_error = true;
468 goto end;
469 }
470
471 /* Ownership of dirfd is transferred to the handle. */
472 data->u.unlink.dirfd = -1;
473
474 ret_value->u.ret = lttng_directory_handle_unlink_file(handle,
475 data->u.unlink.path);
476 ret_value->_errno = errno;
477 ret_value->_error = (ret_value->u.ret) ? true : false;
478 lttng_directory_handle_put(handle);
479 end:
480 return ret_value->u.ret;
481 }
482
483 static
484 int _rmdir(struct run_as_data *data, struct run_as_ret *ret_value)
485 {
486 struct lttng_directory_handle *handle;
487
488 handle = lttng_directory_handle_create_from_dirfd(data->u.rmdir.dirfd);
489 if (!handle) {
490 ret_value->u.ret = -1;
491 ret_value->_errno = errno;
492 ret_value->_error = true;
493 goto end;
494 }
495
496 /* Ownership of dirfd is transferred to the handle. */
497 data->u.rmdir.dirfd = -1;
498
499 ret_value->u.ret = lttng_directory_handle_remove_subdirectory(
500 handle, data->u.rmdir.path);
501 ret_value->_errno = errno;
502 ret_value->_error = (ret_value->u.ret) ? true : false;
503 lttng_directory_handle_put(handle);
504 end:
505 return ret_value->u.ret;
506 }
507
508 static
509 int _rmdir_recursive(struct run_as_data *data, struct run_as_ret *ret_value)
510 {
511 struct lttng_directory_handle *handle;
512
513 handle = lttng_directory_handle_create_from_dirfd(data->u.rmdir.dirfd);
514 if (!handle) {
515 ret_value->u.ret = -1;
516 ret_value->_errno = errno;
517 ret_value->_error = true;
518 goto end;
519 }
520
521 /* Ownership of dirfd is transferred to the handle. */
522 data->u.rmdir.dirfd = -1;
523
524 ret_value->u.ret = lttng_directory_handle_remove_subdirectory_recursive(
525 handle, data->u.rmdir.path, data->u.rmdir.flags);
526 ret_value->_errno = errno;
527 ret_value->_error = (ret_value->u.ret) ? true : false;
528 lttng_directory_handle_put(handle);
529 end:
530 return ret_value->u.ret;
531 }
532
533 static
534 int _rename(struct run_as_data *data, struct run_as_ret *ret_value)
535 {
536 const char *old_path, *new_path;
537 struct lttng_directory_handle *old_handle = NULL, *new_handle = NULL;
538
539 old_path = data->u.rename.old_path;
540 new_path = data->u.rename.new_path;
541
542 old_handle = lttng_directory_handle_create_from_dirfd(
543 data->u.rename.dirfds[0]);
544 if (!old_handle) {
545 ret_value->u.ret = -1;
546 goto end;
547 }
548 new_handle = lttng_directory_handle_create_from_dirfd(
549 data->u.rename.dirfds[1]);
550 if (!new_handle) {
551 ret_value->u.ret = -1;
552 goto end;
553 }
554
555 /* Ownership of dirfds are transferred to the handles. */
556 data->u.rename.dirfds[0] = data->u.rename.dirfds[1] = -1;
557
558 /* Safe to call as we have transitioned to the requested uid/gid. */
559 ret_value->u.ret = lttng_directory_handle_rename(
560 old_handle, old_path, new_handle, new_path);
561 end:
562 lttng_directory_handle_put(old_handle);
563 lttng_directory_handle_put(new_handle);
564 ret_value->_errno = errno;
565 ret_value->_error = (ret_value->u.ret) ? true : false;
566 return ret_value->u.ret;
567 }
568
569 #ifdef HAVE_ELF_H
570 static
571 int _extract_elf_symbol_offset(struct run_as_data *data,
572 struct run_as_ret *ret_value)
573 {
574 int ret = 0;
575 uint64_t offset;
576
577 ret_value->_error = false;
578 ret = lttng_elf_get_symbol_offset(data->u.extract_elf_symbol_offset.fd,
579 data->u.extract_elf_symbol_offset.function,
580 &offset);
581 if (ret) {
582 DBG("Failed to extract ELF function offset");
583 ret_value->_error = true;
584 }
585 ret_value->u.extract_elf_symbol_offset.offset = offset;
586
587 return ret;
588 }
589
590 static
591 int _extract_sdt_probe_offsets(struct run_as_data *data,
592 struct run_as_ret *ret_value)
593 {
594 int ret = 0;
595 uint64_t *offsets = NULL;
596 uint32_t num_offset;
597
598 ret_value->_error = false;
599
600 /* On success, this call allocates the offsets paramater. */
601 ret = lttng_elf_get_sdt_probe_offsets(
602 data->u.extract_sdt_probe_offsets.fd,
603 data->u.extract_sdt_probe_offsets.provider_name,
604 data->u.extract_sdt_probe_offsets.probe_name,
605 &offsets, &num_offset);
606
607 if (ret) {
608 DBG("Failed to extract SDT probe offsets");
609 ret_value->_error = true;
610 goto end;
611 }
612
613 if (num_offset <= 0 || num_offset > LTTNG_KERNEL_ABI_MAX_UPROBE_NUM) {
614 DBG("Wrong number of probes.");
615 ret = -1;
616 ret_value->_error = true;
617 goto free_offset;
618 }
619
620 /* Copy the content of the offsets array to the ret struct. */
621 memcpy(ret_value->u.extract_sdt_probe_offsets.offsets,
622 offsets, num_offset * sizeof(uint64_t));
623
624 ret_value->u.extract_sdt_probe_offsets.num_offset = num_offset;
625
626 free_offset:
627 free(offsets);
628 end:
629 return ret;
630 }
631 #else
632 static
633 int _extract_elf_symbol_offset(struct run_as_data *data,
634 struct run_as_ret *ret_value)
635 {
636 ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET");
637 return -1;
638 }
639
640 static
641 int _extract_sdt_probe_offsets(struct run_as_data *data,
642 struct run_as_ret *ret_value)
643 {
644 ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS");
645 return -1;
646 }
647 #endif
648
649 static
650 int _generate_filter_bytecode(struct run_as_data *data,
651 struct run_as_ret *ret_value) {
652 int ret = 0;
653 const char *filter_expression = NULL;
654 struct filter_parser_ctx *ctx = NULL;
655
656 ret_value->_error = false;
657
658 filter_expression = data->u.generate_filter_bytecode.filter_expression;
659
660 if (lttng_strnlen(filter_expression, LTTNG_FILTER_MAX_LEN - 1) == LTTNG_FILTER_MAX_LEN - 1) {
661 ret_value->_error = true;
662 ret = -1;
663 goto end;
664 }
665
666 ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx);
667 if (ret < 0) {
668 ret_value->_error = true;
669 ret = -1;
670 goto end;
671 }
672
673 DBG("Size of bytecode generated: %u bytes.",
674 bytecode_get_len(&ctx->bytecode->b));
675
676 /* Copy the lttng_bytecode_filter object to the return structure. */
677 memcpy(ret_value->u.generate_filter_bytecode.bytecode,
678 &ctx->bytecode->b,
679 sizeof(ctx->bytecode->b) +
680 bytecode_get_len(&ctx->bytecode->b));
681
682 end:
683 if (ctx) {
684 filter_bytecode_free(ctx);
685 filter_ir_free(ctx);
686 filter_parser_ctx_free(ctx);
687 }
688
689 return ret;
690 }
691 static
692 run_as_fct run_as_enum_to_fct(enum run_as_cmd cmd)
693 {
694 switch (cmd) {
695 case RUN_AS_MKDIR:
696 case RUN_AS_MKDIRAT:
697 return _mkdirat;
698 case RUN_AS_MKDIR_RECURSIVE:
699 case RUN_AS_MKDIRAT_RECURSIVE:
700 return _mkdirat_recursive;
701 case RUN_AS_OPEN:
702 case RUN_AS_OPENAT:
703 return _open;
704 case RUN_AS_UNLINK:
705 case RUN_AS_UNLINKAT:
706 return _unlink;
707 case RUN_AS_RMDIR:
708 case RUN_AS_RMDIRAT:
709 return _rmdir;
710 case RUN_AS_RMDIR_RECURSIVE:
711 case RUN_AS_RMDIRAT_RECURSIVE:
712 return _rmdir_recursive;
713 case RUN_AS_RENAME:
714 case RUN_AS_RENAMEAT:
715 return _rename;
716 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
717 return _extract_elf_symbol_offset;
718 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
719 return _extract_sdt_probe_offsets;
720 case RUN_AS_GENERATE_FILTER_BYTECODE:
721 return _generate_filter_bytecode;
722 default:
723 ERR("Unknown command %d", (int) cmd);
724 return NULL;
725 }
726 }
727
728 static
729 int do_send_fds(int sock, const int *fds, unsigned int fd_count)
730 {
731 ssize_t len;
732 unsigned int i;
733
734 for (i = 0; i < fd_count; i++) {
735 if (fds[i] < 0) {
736 DBG("Attempt to send invalid file descriptor (fd = %i)",
737 fds[i]);
738 /* Return 0 as this is not a fatal error. */
739 return 0;
740 }
741 }
742
743 len = lttcomm_send_fds_unix_sock(sock, fds, fd_count);
744 return len < 0 ? -1 : 0;
745 }
746
747 static
748 int do_recv_fds(int sock, int *fds, unsigned int fd_count)
749 {
750 int ret = 0;
751 unsigned int i;
752 ssize_t len;
753
754 len = lttcomm_recv_fds_unix_sock(sock, fds, fd_count);
755 if (len == 0) {
756 ret = -1;
757 goto end;
758 } else if (len < 0) {
759 PERROR("Failed to receive file descriptors from socket");
760 ret = -1;
761 goto end;
762 }
763
764 for (i = 0; i < fd_count; i++) {
765 if (fds[i] < 0) {
766 ERR("Invalid file descriptor received from worker (fd = %i)", fds[i]);
767 /* Return 0 as this is not a fatal error. */
768 }
769 }
770 end:
771 return ret;
772 }
773
774 static
775 int send_fds_to_worker(const struct run_as_worker *worker,
776 const struct run_as_data *data)
777 {
778 int ret = 0;
779 unsigned int i;
780
781 if (COMMAND_USE_CWD_FD(data) || COMMAND_IN_FD_COUNT(data) == 0) {
782 goto end;
783 }
784
785 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
786 if (COMMAND_IN_FDS(data)[i] < 0) {
787 ERR("Refusing to send invalid fd to worker (fd = %i)",
788 COMMAND_IN_FDS(data)[i]);
789 ret = -1;
790 goto end;
791 }
792 }
793
794 ret = do_send_fds(worker->sockpair[0], COMMAND_IN_FDS(data),
795 COMMAND_IN_FD_COUNT(data));
796 if (ret < 0) {
797 PERROR("Failed to send file descriptor to run-as worker");
798 ret = -1;
799 goto end;
800 }
801 end:
802 return ret;
803 }
804
805 static
806 int send_fds_to_master(struct run_as_worker *worker, enum run_as_cmd cmd,
807 struct run_as_ret *run_as_ret)
808 {
809 int ret = 0;
810 unsigned int i;
811
812 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
813 goto end;
814 }
815
816 ret = do_send_fds(worker->sockpair[1], COMMAND_OUT_FDS(cmd, run_as_ret),
817 COMMAND_OUT_FD_COUNT(cmd));
818 if (ret < 0) {
819 PERROR("Failed to send file descriptor to master process");
820 goto end;
821 }
822
823 for (i = 0; i < COMMAND_OUT_FD_COUNT(cmd); i++) {
824 int fd = COMMAND_OUT_FDS(cmd, run_as_ret)[i];
825 if (fd >= 0) {
826 int ret_close = close(fd);
827
828 if (ret_close < 0) {
829 PERROR("Failed to close result file descriptor (fd = %i)",
830 fd);
831 }
832 }
833 }
834 end:
835 return ret;
836 }
837
838 static
839 int recv_fds_from_worker(const struct run_as_worker *worker, enum run_as_cmd cmd,
840 struct run_as_ret *run_as_ret)
841 {
842 int ret = 0;
843
844 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
845 goto end;
846 }
847
848 ret = do_recv_fds(worker->sockpair[0], COMMAND_OUT_FDS(cmd, run_as_ret),
849 COMMAND_OUT_FD_COUNT(cmd));
850 if (ret < 0) {
851 PERROR("Failed to receive file descriptor from run-as worker");
852 ret = -1;
853 }
854 end:
855 return ret;
856 }
857
858 static
859 int recv_fds_from_master(struct run_as_worker *worker, struct run_as_data *data)
860 {
861 int ret = 0;
862
863 if (COMMAND_USE_CWD_FD(data)) {
864 unsigned int i;
865
866 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
867 COMMAND_IN_FDS(data)[i] = AT_FDCWD;
868 }
869 goto end;
870 }
871
872 if (COMMAND_IN_FD_COUNT(data) == 0) {
873 goto end;
874 }
875
876 ret = do_recv_fds(worker->sockpair[1], COMMAND_IN_FDS(data),
877 COMMAND_IN_FD_COUNT(data));
878 if (ret < 0) {
879 PERROR("Failed to receive file descriptors from master process");
880 ret = -1;
881 }
882 end:
883 return ret;
884 }
885
886 static
887 int cleanup_received_fds(struct run_as_data *data)
888 {
889 int ret = 0, i;
890
891 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
892 if (COMMAND_IN_FDS(data)[i] == -1) {
893 continue;
894 }
895 ret = close(COMMAND_IN_FDS(data)[i]);
896 if (ret) {
897 PERROR("Failed to close file descriptor received fd in run-as worker");
898 goto end;
899 }
900 }
901 end:
902 return ret;
903 }
904
905 static int get_user_infos_from_uid(
906 uid_t uid, char **username, gid_t *primary_gid)
907 {
908 int ret;
909 char *buf = NULL;
910 long raw_get_pw_buf_size;
911 size_t get_pw_buf_size;
912 struct passwd pwd;
913 struct passwd *result = NULL;
914
915 /* Fetch the max size for the temporary buffer. */
916 errno = 0;
917 raw_get_pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
918 if (raw_get_pw_buf_size < 0) {
919 if (errno != 0) {
920 PERROR("Failed to query _SC_GETPW_R_SIZE_MAX");
921 goto error;
922 }
923
924 /* Limit is indeterminate. */
925 WARN("Failed to query _SC_GETPW_R_SIZE_MAX as it is "
926 "indeterminate; falling back to default buffer size");
927 raw_get_pw_buf_size = GETPW_BUFFER_FALLBACK_SIZE;
928 }
929
930 get_pw_buf_size = (size_t) raw_get_pw_buf_size;
931
932 buf = zmalloc(get_pw_buf_size);
933 if (buf == NULL) {
934 PERROR("Failed to allocate buffer to get password file entries");
935 goto error;
936 }
937
938 ret = getpwuid_r(uid, &pwd, buf, get_pw_buf_size, &result);
939 if (ret < 0) {
940 PERROR("Failed to get user information for user: uid = %d",
941 (int) uid);
942 goto error;
943 }
944
945 if (result == NULL) {
946 ERR("Failed to find user information in password entries: uid = %d",
947 (int) uid);
948 ret = -1;
949 goto error;
950 }
951
952 *username = strdup(result->pw_name);
953 if (*username == NULL) {
954 PERROR("Failed to copy user name");
955 goto error;
956 }
957
958 *primary_gid = result->pw_gid;
959
960 end:
961 free(buf);
962 return ret;
963 error:
964 *username = NULL;
965 *primary_gid = -1;
966 ret = -1;
967 goto end;
968 }
969
970 static int demote_creds(
971 uid_t prev_uid, gid_t prev_gid, uid_t new_uid, gid_t new_gid)
972 {
973 int ret = 0;
974 gid_t primary_gid;
975 char *username = NULL;
976
977 /* Change the group id. */
978 if (prev_gid != new_gid) {
979 ret = setegid(new_gid);
980 if (ret < 0) {
981 PERROR("Failed to set effective group id: new_gid = %d",
982 (int) new_gid);
983 goto end;
984 }
985 }
986
987 /* Change the user id. */
988 if (prev_uid != new_uid) {
989 ret = get_user_infos_from_uid(new_uid, &username, &primary_gid);
990 if (ret < 0) {
991 goto end;
992 }
993
994 /*
995 * Initialize the supplementary group access list.
996 *
997 * This is needed to handle cases where the supplementary groups
998 * of the user the process is demoting-to would give it access
999 * to a given file/folder, but not it's primary group.
1000 *
1001 * e.g
1002 * username: User1
1003 * Primary Group: User1
1004 * Secondary group: Disk, Network
1005 *
1006 * mkdir inside the following directory must work since User1
1007 * is part of the Network group.
1008 *
1009 * drwxrwx--- 2 root Network 4096 Jul 23 17:17 /tmp/my_folder/
1010 *
1011 *
1012 * The order of the following initgroups and seteuid calls is
1013 * important here;
1014 * Only a root process or one with CAP_SETGID capability can
1015 * call the the initgroups() function. We must initialize the
1016 * supplementary groups before we change the effective
1017 * UID to a less-privileged user.
1018 */
1019 ret = initgroups(username, primary_gid);
1020 if (ret < 0) {
1021 PERROR("Failed to init the supplementary group access list: "
1022 "username = `%s`, primary gid = %d", username,
1023 (int) primary_gid);
1024 goto end;
1025 }
1026
1027 ret = seteuid(new_uid);
1028 if (ret < 0) {
1029 PERROR("Failed to set effective user id: new_uid = %d",
1030 (int) new_uid);
1031 goto end;
1032 }
1033 }
1034 end:
1035 free(username);
1036 return ret;
1037 }
1038
1039 static int promote_creds(
1040 uid_t prev_uid, gid_t prev_gid, uid_t new_uid, gid_t new_gid)
1041 {
1042 int ret = 0;
1043 gid_t primary_gid;
1044 char *username = NULL;
1045
1046 /* Change the group id. */
1047 if (prev_gid != new_gid) {
1048 ret = setegid(new_gid);
1049 if (ret < 0) {
1050 PERROR("Failed to set effective group id: new_gid = %d",
1051 (int) new_gid);
1052 goto end;
1053 }
1054 }
1055
1056 /* Change the user id. */
1057 if (prev_uid != new_uid) {
1058 ret = get_user_infos_from_uid(new_uid, &username, &primary_gid);
1059 if (ret < 0) {
1060 goto end;
1061 }
1062
1063 /*
1064 * seteuid call must be done before the initgroups call because
1065 * we need to be privileged (CAP_SETGID) to call initgroups().
1066 */
1067 ret = seteuid(new_uid);
1068 if (ret < 0) {
1069 PERROR("Failed to set effective user id: new_uid = %d",
1070 (int) new_uid);
1071 goto end;
1072 }
1073
1074 /*
1075 * Initialize the supplementary group access list.
1076 *
1077 * There is a possibility the groups we set in the following
1078 * initgroups() call are not exactly the same as the ones we
1079 * had when we originally demoted. This can happen if the
1080 * /etc/group file is modified after the runas process is
1081 * forked. This is very unlikely.
1082 */
1083 ret = initgroups(username, primary_gid);
1084 if (ret < 0) {
1085 PERROR("Failed to init the supplementary group access "
1086 "list: username = `%s`, primary gid = %d",
1087 username, (int) primary_gid)
1088 goto end;
1089 }
1090 }
1091 end:
1092 free(username);
1093 return ret;
1094 }
1095
1096 /*
1097 * Return < 0 on error, 0 if OK, 1 on hangup.
1098 */
1099 static
1100 int handle_one_cmd(struct run_as_worker *worker)
1101 {
1102 int ret = 0, promote_ret;
1103 struct run_as_data data = {};
1104 ssize_t readlen, writelen;
1105 struct run_as_ret sendret = {};
1106 run_as_fct cmd;
1107 uid_t prev_ruid;
1108 gid_t prev_rgid;
1109
1110 /*
1111 * Stage 1: Receive run_as_data struct from the master.
1112 * The structure contains the command type and all the parameters needed for
1113 * its execution
1114 */
1115 readlen = lttcomm_recv_unix_sock(worker->sockpair[1], &data,
1116 sizeof(data));
1117 if (readlen == 0) {
1118 /* hang up */
1119 ret = 1;
1120 goto end;
1121 }
1122 if (readlen < sizeof(data)) {
1123 PERROR("lttcomm_recv_unix_sock error");
1124 ret = -1;
1125 goto end;
1126 }
1127
1128 cmd = run_as_enum_to_fct(data.cmd);
1129 if (!cmd) {
1130 ret = -1;
1131 goto end;
1132 }
1133
1134 /*
1135 * Stage 2: Receive file descriptor from master.
1136 * Some commands need a file descriptor as input so if it's needed we
1137 * receive the fd using the Unix socket.
1138 */
1139 ret = recv_fds_from_master(worker, &data);
1140 if (ret < 0) {
1141 PERROR("recv_fd_from_master error");
1142 ret = -1;
1143 goto end;
1144 }
1145
1146 prev_ruid = getuid();
1147 prev_rgid = getgid();
1148
1149 ret = demote_creds(prev_ruid, prev_rgid, data.uid, data.gid);
1150 if (ret < 0) {
1151 goto write_return;
1152 }
1153
1154 /*
1155 * Also set umask to 0 for mkdir executable bit.
1156 */
1157 umask(0);
1158
1159 /*
1160 * Stage 3: Execute the command
1161 */
1162 ret = (*cmd)(&data, &sendret);
1163 if (ret < 0) {
1164 DBG("Execution of command returned an error");
1165 }
1166
1167 write_return:
1168 ret = cleanup_received_fds(&data);
1169 if (ret < 0) {
1170 ERR("Error cleaning up FD");
1171 goto promote_back;
1172 }
1173
1174 /*
1175 * Stage 4: Send run_as_ret structure to the master.
1176 * This structure contain the return value of the command and the errno.
1177 */
1178 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
1179 sizeof(sendret));
1180 if (writelen < sizeof(sendret)) {
1181 PERROR("lttcomm_send_unix_sock error");
1182 ret = -1;
1183 goto promote_back;
1184 }
1185
1186 /*
1187 * Stage 5: Send resulting file descriptors to the master.
1188 */
1189 ret = send_fds_to_master(worker, data.cmd, &sendret);
1190 if (ret < 0) {
1191 DBG("Sending FD to master returned an error");
1192 }
1193
1194 ret = 0;
1195
1196 promote_back:
1197 /* Return to previous uid/gid. */
1198 promote_ret = promote_creds(data.uid, data.gid, prev_ruid, prev_rgid);
1199 if (promote_ret < 0) {
1200 ERR("Failed to promote back to the initial credentials");
1201 }
1202
1203 end:
1204 return ret;
1205 }
1206
1207 static
1208 int run_as_worker(struct run_as_worker *worker)
1209 {
1210 int ret;
1211 ssize_t writelen;
1212 struct run_as_ret sendret;
1213 size_t proc_orig_len;
1214
1215 /*
1216 * Initialize worker. Set a different process cmdline.
1217 */
1218 proc_orig_len = strlen(worker->procname);
1219 memset(worker->procname, 0, proc_orig_len);
1220 strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len);
1221
1222 ret = lttng_thread_setname(DEFAULT_RUN_AS_WORKER_NAME);
1223 if (ret && ret != -ENOSYS) {
1224 /* Don't fail as this is not essential. */
1225 DBG("Failed to set pthread name attribute");
1226 }
1227
1228 memset(&sendret, 0, sizeof(sendret));
1229
1230 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
1231 sizeof(sendret));
1232 if (writelen < sizeof(sendret)) {
1233 PERROR("lttcomm_send_unix_sock error");
1234 ret = EXIT_FAILURE;
1235 goto end;
1236 }
1237
1238 for (;;) {
1239 ret = handle_one_cmd(worker);
1240 if (ret < 0) {
1241 ret = EXIT_FAILURE;
1242 goto end;
1243 } else if (ret > 0) {
1244 break;
1245 } else {
1246 continue; /* Next command. */
1247 }
1248 }
1249 ret = EXIT_SUCCESS;
1250 end:
1251 return ret;
1252 }
1253
1254 static
1255 int run_as_cmd(struct run_as_worker *worker,
1256 enum run_as_cmd cmd,
1257 struct run_as_data *data,
1258 struct run_as_ret *ret_value,
1259 uid_t uid, gid_t gid)
1260 {
1261 int ret = 0;
1262 ssize_t readlen, writelen;
1263
1264 /*
1265 * If we are non-root, we can only deal with our own uid.
1266 */
1267 if (geteuid() != 0) {
1268 if (uid != geteuid()) {
1269 ret = -1;
1270 ret_value->_errno = EPERM;
1271 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
1272 (int) uid, (int) geteuid());
1273 goto end;
1274 }
1275 }
1276
1277 data->cmd = cmd;
1278 data->uid = uid;
1279 data->gid = gid;
1280
1281 /*
1282 * Stage 1: Send the run_as_data struct to the worker process
1283 */
1284 writelen = lttcomm_send_unix_sock(worker->sockpair[0], data,
1285 sizeof(*data));
1286 if (writelen < sizeof(*data)) {
1287 PERROR("Error writing message to run_as");
1288 ret = -1;
1289 ret_value->_errno = EIO;
1290 goto end;
1291 }
1292
1293 /*
1294 * Stage 2: Send file descriptor to the worker process if needed
1295 */
1296 ret = send_fds_to_worker(worker, data);
1297 if (ret) {
1298 PERROR("do_send_fd error");
1299 ret = -1;
1300 ret_value->_errno = EIO;
1301 goto end;
1302 }
1303
1304 /*
1305 * Stage 3: Wait for the execution of the command
1306 */
1307
1308 /*
1309 * Stage 4: Receive the run_as_ret struct containing the return value and
1310 * errno
1311 */
1312 readlen = lttcomm_recv_unix_sock(worker->sockpair[0], ret_value,
1313 sizeof(*ret_value));
1314 if (!readlen) {
1315 ERR("Run-as worker has hung-up during run_as_cmd");
1316 ret = -1;
1317 ret_value->_errno = EIO;
1318 goto end;
1319 } else if (readlen < sizeof(*ret_value)) {
1320 PERROR("Error reading response from run_as");
1321 ret = -1;
1322 ret_value->_errno = errno;
1323 goto end;
1324 }
1325
1326 if (ret_value->_error) {
1327 /* Skip stage 5 on error as there will be no fd to receive. */
1328 goto end;
1329 }
1330
1331 /*
1332 * Stage 5: Receive file descriptor if needed
1333 */
1334 ret = recv_fds_from_worker(worker, cmd, ret_value);
1335 if (ret < 0) {
1336 ERR("Error receiving fd");
1337 ret = -1;
1338 ret_value->_errno = EIO;
1339 }
1340
1341 end:
1342 return ret;
1343 }
1344
1345 /*
1346 * This is for debugging ONLY and should not be considered secure.
1347 */
1348 static
1349 int run_as_noworker(enum run_as_cmd cmd,
1350 struct run_as_data *data, struct run_as_ret *ret_value,
1351 uid_t uid, gid_t gid)
1352 {
1353 int ret, saved_errno;
1354 mode_t old_mask;
1355 run_as_fct fct;
1356
1357 fct = run_as_enum_to_fct(cmd);
1358 if (!fct) {
1359 errno = -ENOSYS;
1360 ret = -1;
1361 goto end;
1362 }
1363 old_mask = umask(0);
1364 ret = fct(data, ret_value);
1365 saved_errno = ret_value->_errno;
1366 umask(old_mask);
1367 errno = saved_errno;
1368 end:
1369 return ret;
1370 }
1371
1372 static
1373 int reset_sighandler(void)
1374 {
1375 int sig;
1376
1377 DBG("Resetting run_as worker signal handlers to default");
1378 for (sig = 1; sig <= 31; sig++) {
1379 (void) signal(sig, SIG_DFL);
1380 }
1381 return 0;
1382 }
1383
1384 static
1385 void worker_sighandler(int sig)
1386 {
1387 const char *signame;
1388
1389 /*
1390 * The worker will inherit its parent's signals since they are part of
1391 * the same process group. However, in the case of SIGINT and SIGTERM,
1392 * we want to give the worker a chance to teardown gracefully when its
1393 * parent closes the command socket.
1394 */
1395 switch (sig) {
1396 case SIGINT:
1397 signame = "SIGINT";
1398 break;
1399 case SIGTERM:
1400 signame = "SIGTERM";
1401 break;
1402 default:
1403 signame = NULL;
1404 }
1405
1406 if (signame) {
1407 DBG("run_as worker received signal %s", signame);
1408 } else {
1409 DBG("run_as_worker received signal %d", sig);
1410 }
1411 }
1412
1413 static
1414 int set_worker_sighandlers(void)
1415 {
1416 int ret = 0;
1417 sigset_t sigset;
1418 struct sigaction sa;
1419
1420 if ((ret = sigemptyset(&sigset)) < 0) {
1421 PERROR("sigemptyset");
1422 goto end;
1423 }
1424
1425 sa.sa_handler = worker_sighandler;
1426 sa.sa_mask = sigset;
1427 sa.sa_flags = 0;
1428 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
1429 PERROR("sigaction SIGINT");
1430 goto end;
1431 }
1432
1433 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
1434 PERROR("sigaction SIGTERM");
1435 goto end;
1436 }
1437
1438 DBG("run_as signal handler set for SIGTERM and SIGINT");
1439 end:
1440 return ret;
1441 }
1442
1443 static
1444 int run_as_create_worker_no_lock(const char *procname,
1445 post_fork_cleanup_cb clean_up_func,
1446 void *clean_up_user_data)
1447 {
1448 pid_t pid;
1449 int i, ret = 0;
1450 ssize_t readlen;
1451 struct run_as_ret recvret;
1452 struct run_as_worker *worker;
1453
1454 assert(!global_worker);
1455 if (!use_clone()) {
1456 /*
1457 * Don't initialize a worker, all run_as tasks will be performed
1458 * in the current process.
1459 */
1460 ret = 0;
1461 goto end;
1462 }
1463 worker = zmalloc(sizeof(*worker));
1464 if (!worker) {
1465 ret = -ENOMEM;
1466 goto end;
1467 }
1468 worker->procname = strdup(procname);
1469 if (!worker->procname) {
1470 ret = -ENOMEM;
1471 goto error_procname_alloc;
1472 }
1473 /* Create unix socket. */
1474 if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) {
1475 ret = -1;
1476 goto error_sock;
1477 }
1478
1479 /* Fork worker. */
1480 pid = fork();
1481 if (pid < 0) {
1482 PERROR("fork");
1483 ret = -1;
1484 goto error_fork;
1485 } else if (pid == 0) {
1486 /* Child */
1487
1488 reset_sighandler();
1489
1490 set_worker_sighandlers();
1491
1492 logger_set_thread_name("Run-as worker", true);
1493
1494 if (clean_up_func) {
1495 if (clean_up_func(clean_up_user_data) < 0) {
1496 ERR("Run-as post-fork clean-up failed, exiting.");
1497 exit(EXIT_FAILURE);
1498 }
1499 }
1500
1501 /* Just close, no shutdown. */
1502 if (close(worker->sockpair[0])) {
1503 PERROR("close");
1504 exit(EXIT_FAILURE);
1505 }
1506
1507 /*
1508 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
1509 * Sockpair[1] is used as a control channel with the master
1510 */
1511 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
1512 if (i != worker->sockpair[1]) {
1513 (void) close(i);
1514 }
1515 }
1516
1517 worker->sockpair[0] = -1;
1518 ret = run_as_worker(worker);
1519 if (lttcomm_close_unix_sock(worker->sockpair[1])) {
1520 PERROR("close");
1521 ret = -1;
1522 }
1523 worker->sockpair[1] = -1;
1524 free(worker->procname);
1525 free(worker);
1526 LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret);
1527 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
1528 } else {
1529 /* Parent */
1530
1531 /* Just close, no shutdown. */
1532 if (close(worker->sockpair[1])) {
1533 PERROR("close");
1534 ret = -1;
1535 goto error_fork;
1536 }
1537 worker->sockpair[1] = -1;
1538 worker->pid = pid;
1539 /* Wait for worker to become ready. */
1540 readlen = lttcomm_recv_unix_sock(worker->sockpair[0],
1541 &recvret, sizeof(recvret));
1542 if (readlen < sizeof(recvret)) {
1543 ERR("readlen: %zd", readlen);
1544 PERROR("Error reading response from run_as at creation");
1545 ret = -1;
1546 goto error_fork;
1547 }
1548 global_worker = worker;
1549 }
1550 end:
1551 return ret;
1552
1553 /* Error handling. */
1554 error_fork:
1555 for (i = 0; i < 2; i++) {
1556 if (worker->sockpair[i] < 0) {
1557 continue;
1558 }
1559 if (lttcomm_close_unix_sock(worker->sockpair[i])) {
1560 PERROR("close");
1561 }
1562 worker->sockpair[i] = -1;
1563 }
1564 error_sock:
1565 free(worker->procname);
1566 error_procname_alloc:
1567 free(worker);
1568 return ret;
1569 }
1570
1571 static
1572 void run_as_destroy_worker_no_lock(void)
1573 {
1574 struct run_as_worker *worker = global_worker;
1575
1576 DBG("Destroying run_as worker");
1577 if (!worker) {
1578 return;
1579 }
1580 /* Close unix socket */
1581 DBG("Closing run_as worker socket");
1582 if (lttcomm_close_unix_sock(worker->sockpair[0])) {
1583 PERROR("close");
1584 }
1585 worker->sockpair[0] = -1;
1586 /* Wait for worker. */
1587 for (;;) {
1588 int status;
1589 pid_t wait_ret;
1590
1591 wait_ret = waitpid(worker->pid, &status, 0);
1592 if (wait_ret < 0) {
1593 if (errno == EINTR) {
1594 continue;
1595 }
1596 PERROR("waitpid");
1597 break;
1598 }
1599
1600 if (WIFEXITED(status)) {
1601 LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR,
1602 DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d",
1603 WEXITSTATUS(status));
1604 break;
1605 } else if (WIFSIGNALED(status)) {
1606 ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d",
1607 WTERMSIG(status));
1608 break;
1609 }
1610 }
1611 free(worker->procname);
1612 free(worker);
1613 global_worker = NULL;
1614 }
1615
1616 static
1617 int run_as_restart_worker(struct run_as_worker *worker)
1618 {
1619 int ret = 0;
1620 char *procname = NULL;
1621
1622 procname = worker->procname;
1623
1624 /* Close socket to run_as worker process and clean up the zombie process */
1625 run_as_destroy_worker_no_lock();
1626
1627 /* Create a new run_as worker process*/
1628 ret = run_as_create_worker_no_lock(procname, NULL, NULL);
1629 if (ret < 0 ) {
1630 ERR("Restarting the worker process failed");
1631 ret = -1;
1632 goto err;
1633 }
1634 err:
1635 return ret;
1636 }
1637
1638 static
1639 int run_as(enum run_as_cmd cmd, struct run_as_data *data,
1640 struct run_as_ret *ret_value, uid_t uid, gid_t gid)
1641 {
1642 int ret, saved_errno;
1643
1644 pthread_mutex_lock(&worker_lock);
1645 if (use_clone()) {
1646 DBG("Using run_as worker");
1647
1648 assert(global_worker);
1649
1650 ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid);
1651 saved_errno = ret_value->_errno;
1652
1653 /*
1654 * If the worker thread crashed the errno is set to EIO. we log
1655 * the error and start a new worker process.
1656 */
1657 if (ret == -1 && saved_errno == EIO) {
1658 DBG("Socket closed unexpectedly... "
1659 "Restarting the worker process");
1660 ret = run_as_restart_worker(global_worker);
1661 if (ret == -1) {
1662 ERR("Failed to restart worker process.");
1663 goto err;
1664 }
1665 }
1666 } else {
1667 DBG("Using run_as without worker");
1668 ret = run_as_noworker(cmd, data, ret_value, uid, gid);
1669 }
1670 err:
1671 pthread_mutex_unlock(&worker_lock);
1672 return ret;
1673 }
1674
1675 LTTNG_HIDDEN
1676 int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
1677 {
1678 return run_as_mkdirat_recursive(AT_FDCWD, path, mode, uid, gid);
1679 }
1680
1681 LTTNG_HIDDEN
1682 int run_as_mkdirat_recursive(int dirfd, const char *path, mode_t mode,
1683 uid_t uid, gid_t gid)
1684 {
1685 int ret;
1686 struct run_as_data data = {};
1687 struct run_as_ret run_as_ret = {};
1688
1689 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1690 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1691 path, (int) mode, (int) uid, (int) gid);
1692 ret = lttng_strncpy(data.u.mkdir.path, path,
1693 sizeof(data.u.mkdir.path));
1694 if (ret) {
1695 ERR("Failed to copy path argument of mkdirat recursive command");
1696 goto error;
1697 }
1698 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1699 data.u.mkdir.mode = mode;
1700 data.u.mkdir.dirfd = dirfd;
1701 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR_RECURSIVE : RUN_AS_MKDIRAT_RECURSIVE,
1702 &data, &run_as_ret, uid, gid);
1703 errno = run_as_ret._errno;
1704 ret = run_as_ret.u.ret;
1705 error:
1706 return ret;
1707 }
1708
1709 LTTNG_HIDDEN
1710 int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
1711 {
1712 return run_as_mkdirat(AT_FDCWD, path, mode, uid, gid);
1713 }
1714
1715 LTTNG_HIDDEN
1716 int run_as_mkdirat(int dirfd, const char *path, mode_t mode,
1717 uid_t uid, gid_t gid)
1718 {
1719 int ret;
1720 struct run_as_data data = {};
1721 struct run_as_ret run_as_ret = {};
1722
1723 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1724 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1725 path, (int) mode, (int) uid, (int) gid);
1726 ret = lttng_strncpy(data.u.mkdir.path, path,
1727 sizeof(data.u.mkdir.path));
1728 if (ret) {
1729 ERR("Failed to copy path argument of mkdirat command");
1730 goto error;
1731 }
1732 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1733 data.u.mkdir.mode = mode;
1734 data.u.mkdir.dirfd = dirfd;
1735 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR : RUN_AS_MKDIRAT,
1736 &data, &run_as_ret, uid, gid);
1737 errno = run_as_ret._errno;
1738 ret = run_as_ret.u.ret;
1739 error:
1740 return ret;
1741 }
1742
1743 LTTNG_HIDDEN
1744 int run_as_open(const char *path, int flags, mode_t mode, uid_t uid,
1745 gid_t gid)
1746 {
1747 return run_as_openat(AT_FDCWD, path, flags, mode, uid, gid);
1748 }
1749
1750 LTTNG_HIDDEN
1751 int run_as_openat(int dirfd, const char *path, int flags, mode_t mode,
1752 uid_t uid, gid_t gid)
1753 {
1754 int ret;
1755 struct run_as_data data = {};
1756 struct run_as_ret run_as_ret = {};
1757
1758 DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
1759 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1760 path, flags, (int) mode, (int) uid, (int) gid);
1761 ret = lttng_strncpy(data.u.open.path, path, sizeof(data.u.open.path));
1762 if (ret) {
1763 ERR("Failed to copy path argument of open command");
1764 goto error;
1765 }
1766 data.u.open.flags = flags;
1767 data.u.open.mode = mode;
1768 data.u.open.dirfd = dirfd;
1769 run_as(dirfd == AT_FDCWD ? RUN_AS_OPEN : RUN_AS_OPENAT,
1770 &data, &run_as_ret, uid, gid);
1771 errno = run_as_ret._errno;
1772 ret = run_as_ret.u.ret < 0 ? run_as_ret.u.ret :
1773 run_as_ret.u.open.fd;
1774 error:
1775 return ret;
1776 }
1777
1778 LTTNG_HIDDEN
1779 int run_as_unlink(const char *path, uid_t uid, gid_t gid)
1780 {
1781 return run_as_unlinkat(AT_FDCWD, path, uid, gid);
1782 }
1783
1784 LTTNG_HIDDEN
1785 int run_as_unlinkat(int dirfd, const char *path, uid_t uid, gid_t gid)
1786 {
1787 int ret;
1788 struct run_as_data data = {};
1789 struct run_as_ret run_as_ret = {};
1790
1791 DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
1792 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1793 path, (int) uid, (int) gid);
1794 ret = lttng_strncpy(data.u.unlink.path, path,
1795 sizeof(data.u.unlink.path));
1796 if (ret) {
1797 goto error;
1798 }
1799 data.u.unlink.dirfd = dirfd;
1800 run_as(dirfd == AT_FDCWD ? RUN_AS_UNLINK : RUN_AS_UNLINKAT, &data,
1801 &run_as_ret, uid, gid);
1802 errno = run_as_ret._errno;
1803 ret = run_as_ret.u.ret;
1804 error:
1805 return ret;
1806 }
1807
1808 LTTNG_HIDDEN
1809 int run_as_rmdir(const char *path, uid_t uid, gid_t gid)
1810 {
1811 return run_as_rmdirat(AT_FDCWD, path, uid, gid);
1812 }
1813
1814 LTTNG_HIDDEN
1815 int run_as_rmdirat(int dirfd, const char *path, uid_t uid, gid_t gid)
1816 {
1817 int ret;
1818 struct run_as_data data = {};
1819 struct run_as_ret run_as_ret = {};
1820
1821 DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d",
1822 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1823 path, (int) uid, (int) gid);
1824 ret = lttng_strncpy(data.u.rmdir.path, path,
1825 sizeof(data.u.rmdir.path));
1826 if (ret) {
1827 goto error;
1828 }
1829 data.u.rmdir.dirfd = dirfd;
1830 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR : RUN_AS_RMDIRAT, &data,
1831 &run_as_ret, uid, gid);
1832 errno = run_as_ret._errno;
1833 ret = run_as_ret.u.ret;
1834 error:
1835 return ret;
1836 }
1837
1838 LTTNG_HIDDEN
1839 int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid, int flags)
1840 {
1841 return run_as_rmdirat_recursive(AT_FDCWD, path, uid, gid, flags);
1842 }
1843
1844 LTTNG_HIDDEN
1845 int run_as_rmdirat_recursive(int dirfd, const char *path, uid_t uid, gid_t gid, int flags)
1846 {
1847 int ret;
1848 struct run_as_data data = {};
1849 struct run_as_ret run_as_ret = {};
1850
1851 DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d",
1852 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1853 path, (int) uid, (int) gid);
1854 ret = lttng_strncpy(data.u.rmdir.path, path,
1855 sizeof(data.u.rmdir.path));
1856 if (ret) {
1857 goto error;
1858 }
1859 data.u.rmdir.dirfd = dirfd;
1860 data.u.rmdir.flags = flags;
1861 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR_RECURSIVE : RUN_AS_RMDIRAT_RECURSIVE,
1862 &data, &run_as_ret, uid, gid);
1863 errno = run_as_ret._errno;
1864 ret = run_as_ret.u.ret;
1865 error:
1866 return ret;
1867 }
1868
1869 LTTNG_HIDDEN
1870 int run_as_rename(const char *old, const char *new, uid_t uid, gid_t gid)
1871 {
1872 return run_as_renameat(AT_FDCWD, old, AT_FDCWD, new, uid, gid);
1873 }
1874
1875 LTTNG_HIDDEN
1876 int run_as_renameat(int old_dirfd, const char *old_name,
1877 int new_dirfd, const char *new_name, uid_t uid, gid_t gid)
1878 {
1879 int ret;
1880 struct run_as_data data = {};
1881 struct run_as_ret run_as_ret = {};
1882
1883 DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d",
1884 old_dirfd, old_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1885 old_name,
1886 new_dirfd, new_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1887 new_name, (int) uid, (int) gid);
1888 ret = lttng_strncpy(data.u.rename.old_path, old_name,
1889 sizeof(data.u.rename.old_path));
1890 if (ret) {
1891 goto error;
1892 }
1893 ret = lttng_strncpy(data.u.rename.new_path, new_name,
1894 sizeof(data.u.rename.new_path));
1895 if (ret) {
1896 goto error;
1897 }
1898
1899 data.u.rename.dirfds[0] = old_dirfd;
1900 data.u.rename.dirfds[1] = new_dirfd;
1901 run_as(old_dirfd == AT_FDCWD && new_dirfd == AT_FDCWD ?
1902 RUN_AS_RENAME : RUN_AS_RENAMEAT,
1903 &data, &run_as_ret, uid, gid);
1904 errno = run_as_ret._errno;
1905 ret = run_as_ret.u.ret;
1906 error:
1907 return ret;
1908 }
1909
1910 LTTNG_HIDDEN
1911 int run_as_extract_elf_symbol_offset(int fd, const char* function,
1912 uid_t uid, gid_t gid, uint64_t *offset)
1913 {
1914 int ret;
1915 struct run_as_data data = {};
1916 struct run_as_ret run_as_ret = {};
1917
1918 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
1919 "with for uid %d and gid %d", fd, function,
1920 (int) uid, (int) gid);
1921
1922 data.u.extract_elf_symbol_offset.fd = fd;
1923
1924 strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1);
1925 data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1926 ret = lttng_strncpy(data.u.extract_elf_symbol_offset.function,
1927 function,
1928 sizeof(data.u.extract_elf_symbol_offset.function));
1929 if (ret) {
1930 goto error;
1931 }
1932
1933 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &run_as_ret, uid, gid);
1934 errno = run_as_ret._errno;
1935 if (run_as_ret._error) {
1936 ret = -1;
1937 goto error;
1938 }
1939
1940 *offset = run_as_ret.u.extract_elf_symbol_offset.offset;
1941 error:
1942 return ret;
1943 }
1944
1945 LTTNG_HIDDEN
1946 int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name,
1947 const char* probe_name, uid_t uid, gid_t gid,
1948 uint64_t **offsets, uint32_t *num_offset)
1949 {
1950 int ret;
1951 struct run_as_data data = {};
1952 struct run_as_ret run_as_ret = {};
1953
1954 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
1955 "provider_name=%s with for uid %d and gid %d", fd,
1956 probe_name, provider_name, (int) uid, (int) gid);
1957
1958 data.u.extract_sdt_probe_offsets.fd = fd;
1959
1960 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name,
1961 sizeof(data.u.extract_sdt_probe_offsets.probe_name));
1962 if (ret) {
1963 goto error;
1964 }
1965 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.provider_name,
1966 provider_name,
1967 sizeof(data.u.extract_sdt_probe_offsets.provider_name));
1968 if (ret) {
1969 goto error;
1970 }
1971
1972 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &run_as_ret, uid, gid);
1973 errno = run_as_ret._errno;
1974 if (run_as_ret._error) {
1975 ret = -1;
1976 goto error;
1977 }
1978
1979 *num_offset = run_as_ret.u.extract_sdt_probe_offsets.num_offset;
1980 *offsets = zmalloc(*num_offset * sizeof(uint64_t));
1981 if (!*offsets) {
1982 ret = -ENOMEM;
1983 goto error;
1984 }
1985
1986 memcpy(*offsets, run_as_ret.u.extract_sdt_probe_offsets.offsets,
1987 *num_offset * sizeof(uint64_t));
1988 error:
1989 return ret;
1990 }
1991
1992 LTTNG_HIDDEN
1993 int run_as_generate_filter_bytecode(const char *filter_expression,
1994 const struct lttng_credentials *creds,
1995 struct lttng_bytecode **bytecode)
1996 {
1997 int ret;
1998 struct run_as_data data = {};
1999 struct run_as_ret run_as_ret = {};
2000 const struct lttng_bytecode *view_bytecode = NULL;
2001 struct lttng_bytecode *local_bytecode = NULL;
2002 const uid_t uid = lttng_credentials_get_uid(creds);
2003 const gid_t gid = lttng_credentials_get_gid(creds);
2004
2005 DBG3("generate_filter_bytecode() from expression=\"%s\" for uid %d and gid %d",
2006 filter_expression, (int) uid, (int) gid);
2007
2008 ret = lttng_strncpy(data.u.generate_filter_bytecode.filter_expression, filter_expression,
2009 sizeof(data.u.generate_filter_bytecode.filter_expression));
2010 if (ret) {
2011 goto error;
2012 }
2013
2014 run_as(RUN_AS_GENERATE_FILTER_BYTECODE, &data, &run_as_ret, uid, gid);
2015 errno = run_as_ret._errno;
2016 if (run_as_ret._error) {
2017 ret = -1;
2018 goto error;
2019 }
2020
2021 view_bytecode = (const struct lttng_bytecode *) run_as_ret.u.generate_filter_bytecode.bytecode;
2022
2023 local_bytecode = zmalloc(sizeof(*local_bytecode) + view_bytecode->len);
2024 if (!local_bytecode) {
2025 ret = -ENOMEM;
2026 goto error;
2027 }
2028
2029 memcpy(local_bytecode, run_as_ret.u.generate_filter_bytecode.bytecode,
2030 sizeof(*local_bytecode) + view_bytecode->len);
2031 *bytecode = local_bytecode;
2032 error:
2033 return ret;
2034 }
2035
2036 LTTNG_HIDDEN
2037 int run_as_create_worker(const char *procname,
2038 post_fork_cleanup_cb clean_up_func,
2039 void *clean_up_user_data)
2040 {
2041 int ret;
2042
2043 pthread_mutex_lock(&worker_lock);
2044 ret = run_as_create_worker_no_lock(procname, clean_up_func,
2045 clean_up_user_data);
2046 pthread_mutex_unlock(&worker_lock);
2047 return ret;
2048 }
2049
2050 LTTNG_HIDDEN
2051 void run_as_destroy_worker(void)
2052 {
2053 pthread_mutex_lock(&worker_lock);
2054 run_as_destroy_worker_no_lock();
2055 pthread_mutex_unlock(&worker_lock);
2056 }
This page took 0.102699 seconds and 4 git commands to generate.