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