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