common: Add index allocator for error counters
[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 <limits.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/wait.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <sched.h>
21 #include <signal.h>
22 #include <assert.h>
23 #include <signal.h>
24
25 #include <common/bytecode/bytecode.h>
26 #include <common/lttng-kernel.h>
27 #include <common/common.h>
28 #include <common/utils.h>
29 #include <common/compat/errno.h>
30 #include <common/compat/getenv.h>
31 #include <common/compat/string.h>
32 #include <common/unix.h>
33 #include <common/defaults.h>
34 #include <common/lttng-elf.h>
35 #include <common/thread.h>
36
37 #include <lttng/constant.h>
38
39 #include <common/sessiond-comm/sessiond-comm.h>
40 #include <common/filter/filter-ast.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_PACKED;
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 struct 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
686 return ret;
687 }
688 static
689 run_as_fct run_as_enum_to_fct(enum run_as_cmd cmd)
690 {
691 switch (cmd) {
692 case RUN_AS_MKDIR:
693 case RUN_AS_MKDIRAT:
694 return _mkdirat;
695 case RUN_AS_MKDIR_RECURSIVE:
696 case RUN_AS_MKDIRAT_RECURSIVE:
697 return _mkdirat_recursive;
698 case RUN_AS_OPEN:
699 case RUN_AS_OPENAT:
700 return _open;
701 case RUN_AS_UNLINK:
702 case RUN_AS_UNLINKAT:
703 return _unlink;
704 case RUN_AS_RMDIR:
705 case RUN_AS_RMDIRAT:
706 return _rmdir;
707 case RUN_AS_RMDIR_RECURSIVE:
708 case RUN_AS_RMDIRAT_RECURSIVE:
709 return _rmdir_recursive;
710 case RUN_AS_RENAME:
711 case RUN_AS_RENAMEAT:
712 return _rename;
713 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
714 return _extract_elf_symbol_offset;
715 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
716 return _extract_sdt_probe_offsets;
717 case RUN_AS_GENERATE_FILTER_BYTECODE:
718 return _generate_filter_bytecode;
719 default:
720 ERR("Unknown command %d", (int) cmd);
721 return NULL;
722 }
723 }
724
725 static
726 int do_send_fds(int sock, const int *fds, unsigned int fd_count)
727 {
728 ssize_t len;
729 unsigned int i;
730
731 for (i = 0; i < fd_count; i++) {
732 if (fds[i] < 0) {
733 ERR("Attempt to send invalid file descriptor to master (fd = %i)",
734 fds[i]);
735 /* Return 0 as this is not a fatal error. */
736 return 0;
737 }
738 }
739
740 len = lttcomm_send_fds_unix_sock(sock, fds, fd_count);
741 return len < 0 ? -1 : 0;
742 }
743
744 static
745 int do_recv_fds(int sock, int *fds, unsigned int fd_count)
746 {
747 int ret = 0;
748 unsigned int i;
749 ssize_t len;
750
751 len = lttcomm_recv_fds_unix_sock(sock, fds, fd_count);
752 if (len == 0) {
753 ret = -1;
754 goto end;
755 } else if (len < 0) {
756 PERROR("Failed to receive file descriptors from socket");
757 ret = -1;
758 goto end;
759 }
760
761 for (i = 0; i < fd_count; i++) {
762 if (fds[i] < 0) {
763 ERR("Invalid file descriptor received from worker (fd = %i)", fds[i]);
764 /* Return 0 as this is not a fatal error. */
765 }
766 }
767 end:
768 return ret;
769 }
770
771 static
772 int send_fds_to_worker(const struct run_as_worker *worker,
773 const struct run_as_data *data)
774 {
775 int ret = 0;
776 unsigned int i;
777
778 if (COMMAND_USE_CWD_FD(data) || COMMAND_IN_FD_COUNT(data) == 0) {
779 goto end;
780 }
781
782 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
783 if (COMMAND_IN_FDS(data)[i] < 0) {
784 ERR("Refusing to send invalid fd to worker (fd = %i)",
785 COMMAND_IN_FDS(data)[i]);
786 ret = -1;
787 goto end;
788 }
789 }
790
791 ret = do_send_fds(worker->sockpair[0], COMMAND_IN_FDS(data),
792 COMMAND_IN_FD_COUNT(data));
793 if (ret < 0) {
794 PERROR("Failed to send file descriptor to run-as worker");
795 ret = -1;
796 goto end;
797 }
798 end:
799 return ret;
800 }
801
802 static
803 int send_fds_to_master(struct run_as_worker *worker, enum run_as_cmd cmd,
804 struct run_as_ret *run_as_ret)
805 {
806 int ret = 0;
807 unsigned int i;
808
809 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
810 goto end;
811 }
812
813 ret = do_send_fds(worker->sockpair[1], COMMAND_OUT_FDS(cmd, run_as_ret),
814 COMMAND_OUT_FD_COUNT(cmd));
815 if (ret < 0) {
816 PERROR("Failed to send file descriptor to master process");
817 goto end;
818 }
819
820 for (i = 0; i < COMMAND_OUT_FD_COUNT(cmd); i++) {
821 int ret_close = close(COMMAND_OUT_FDS(cmd, run_as_ret)[i]);
822
823 if (ret_close < 0) {
824 PERROR("Failed to close result file descriptor");
825 }
826 }
827 end:
828 return ret;
829 }
830
831 static
832 int recv_fds_from_worker(const struct run_as_worker *worker, enum run_as_cmd cmd,
833 struct run_as_ret *run_as_ret)
834 {
835 int ret = 0;
836
837 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
838 goto end;
839 }
840
841 ret = do_recv_fds(worker->sockpair[0], COMMAND_OUT_FDS(cmd, run_as_ret),
842 COMMAND_OUT_FD_COUNT(cmd));
843 if (ret < 0) {
844 PERROR("Failed to receive file descriptor from run-as worker");
845 ret = -1;
846 }
847 end:
848 return ret;
849 }
850
851 static
852 int recv_fds_from_master(struct run_as_worker *worker, struct run_as_data *data)
853 {
854 int ret = 0;
855
856 if (COMMAND_USE_CWD_FD(data)) {
857 unsigned int i;
858
859 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
860 COMMAND_IN_FDS(data)[i] = AT_FDCWD;
861 }
862 goto end;
863 }
864
865 if (COMMAND_IN_FD_COUNT(data) == 0) {
866 goto end;
867 }
868
869 ret = do_recv_fds(worker->sockpair[1], COMMAND_IN_FDS(data),
870 COMMAND_IN_FD_COUNT(data));
871 if (ret < 0) {
872 PERROR("Failed to receive file descriptors from master process");
873 ret = -1;
874 }
875 end:
876 return ret;
877 }
878
879 static
880 int cleanup_received_fds(struct run_as_data *data)
881 {
882 int ret = 0, i;
883
884 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
885 if (COMMAND_IN_FDS(data)[i] == -1) {
886 continue;
887 }
888 ret = close(COMMAND_IN_FDS(data)[i]);
889 if (ret) {
890 PERROR("Failed to close file descriptor received fd in run-as worker");
891 goto end;
892 }
893 }
894 end:
895 return ret;
896 }
897
898 /*
899 * Return < 0 on error, 0 if OK, 1 on hangup.
900 */
901 static
902 int handle_one_cmd(struct run_as_worker *worker)
903 {
904 int ret = 0;
905 struct run_as_data data = {};
906 ssize_t readlen, writelen;
907 struct run_as_ret sendret = {};
908 run_as_fct cmd;
909 uid_t prev_euid;
910
911 /*
912 * Stage 1: Receive run_as_data struct from the master.
913 * The structure contains the command type and all the parameters needed for
914 * its execution
915 */
916 readlen = lttcomm_recv_unix_sock(worker->sockpair[1], &data,
917 sizeof(data));
918 if (readlen == 0) {
919 /* hang up */
920 ret = 1;
921 goto end;
922 }
923 if (readlen < sizeof(data)) {
924 PERROR("lttcomm_recv_unix_sock error");
925 ret = -1;
926 goto end;
927 }
928
929 cmd = run_as_enum_to_fct(data.cmd);
930 if (!cmd) {
931 ret = -1;
932 goto end;
933 }
934
935 /*
936 * Stage 2: Receive file descriptor from master.
937 * Some commands need a file descriptor as input so if it's needed we
938 * receive the fd using the Unix socket.
939 */
940 ret = recv_fds_from_master(worker, &data);
941 if (ret < 0) {
942 PERROR("recv_fd_from_master error");
943 ret = -1;
944 goto end;
945 }
946
947 prev_euid = getuid();
948 if (data.gid != getegid()) {
949 ret = setegid(data.gid);
950 if (ret < 0) {
951 sendret._error = true;
952 sendret._errno = errno;
953 PERROR("setegid");
954 goto write_return;
955 }
956 }
957 if (data.uid != prev_euid) {
958 ret = seteuid(data.uid);
959 if (ret < 0) {
960 sendret._error = true;
961 sendret._errno = errno;
962 PERROR("seteuid");
963 goto write_return;
964 }
965 }
966
967 /*
968 * Also set umask to 0 for mkdir executable bit.
969 */
970 umask(0);
971
972 /*
973 * Stage 3: Execute the command
974 */
975 ret = (*cmd)(&data, &sendret);
976 if (ret < 0) {
977 DBG("Execution of command returned an error");
978 }
979
980 write_return:
981 ret = cleanup_received_fds(&data);
982 if (ret < 0) {
983 ERR("Error cleaning up FD");
984 goto end;
985 }
986
987 /*
988 * Stage 4: Send run_as_ret structure to the master.
989 * This structure contain the return value of the command and the errno.
990 */
991 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
992 sizeof(sendret));
993 if (writelen < sizeof(sendret)) {
994 PERROR("lttcomm_send_unix_sock error");
995 ret = -1;
996 goto end;
997 }
998
999 /*
1000 * Stage 5: Send resulting file descriptors to the master.
1001 */
1002 ret = send_fds_to_master(worker, data.cmd, &sendret);
1003 if (ret < 0) {
1004 DBG("Sending FD to master returned an error");
1005 goto end;
1006 }
1007
1008 if (seteuid(prev_euid) < 0) {
1009 PERROR("seteuid");
1010 ret = -1;
1011 goto end;
1012 }
1013 ret = 0;
1014 end:
1015 return ret;
1016 }
1017
1018 static
1019 int run_as_worker(struct run_as_worker *worker)
1020 {
1021 int ret;
1022 ssize_t writelen;
1023 struct run_as_ret sendret;
1024 size_t proc_orig_len;
1025
1026 /*
1027 * Initialize worker. Set a different process cmdline.
1028 */
1029 proc_orig_len = strlen(worker->procname);
1030 memset(worker->procname, 0, proc_orig_len);
1031 strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len);
1032
1033 ret = lttng_thread_setname(DEFAULT_RUN_AS_WORKER_NAME);
1034 if (ret && ret != -ENOSYS) {
1035 /* Don't fail as this is not essential. */
1036 DBG("Failed to set pthread name attribute");
1037 }
1038
1039 memset(&sendret, 0, sizeof(sendret));
1040
1041 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
1042 sizeof(sendret));
1043 if (writelen < sizeof(sendret)) {
1044 PERROR("lttcomm_send_unix_sock error");
1045 ret = EXIT_FAILURE;
1046 goto end;
1047 }
1048
1049 for (;;) {
1050 ret = handle_one_cmd(worker);
1051 if (ret < 0) {
1052 ret = EXIT_FAILURE;
1053 goto end;
1054 } else if (ret > 0) {
1055 break;
1056 } else {
1057 continue; /* Next command. */
1058 }
1059 }
1060 ret = EXIT_SUCCESS;
1061 end:
1062 return ret;
1063 }
1064
1065 static
1066 int run_as_cmd(struct run_as_worker *worker,
1067 enum run_as_cmd cmd,
1068 struct run_as_data *data,
1069 struct run_as_ret *ret_value,
1070 uid_t uid, gid_t gid)
1071 {
1072 int ret = 0;
1073 ssize_t readlen, writelen;
1074
1075 /*
1076 * If we are non-root, we can only deal with our own uid.
1077 */
1078 if (geteuid() != 0) {
1079 if (uid != geteuid()) {
1080 ret = -1;
1081 ret_value->_errno = EPERM;
1082 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
1083 (int) uid, (int) geteuid());
1084 goto end;
1085 }
1086 }
1087
1088 data->cmd = cmd;
1089 data->uid = uid;
1090 data->gid = gid;
1091
1092 /*
1093 * Stage 1: Send the run_as_data struct to the worker process
1094 */
1095 writelen = lttcomm_send_unix_sock(worker->sockpair[0], data,
1096 sizeof(*data));
1097 if (writelen < sizeof(*data)) {
1098 PERROR("Error writing message to run_as");
1099 ret = -1;
1100 ret_value->_errno = EIO;
1101 goto end;
1102 }
1103
1104 /*
1105 * Stage 2: Send file descriptor to the worker process if needed
1106 */
1107 ret = send_fds_to_worker(worker, data);
1108 if (ret) {
1109 PERROR("do_send_fd error");
1110 ret = -1;
1111 ret_value->_errno = EIO;
1112 goto end;
1113 }
1114
1115 /*
1116 * Stage 3: Wait for the execution of the command
1117 */
1118
1119 /*
1120 * Stage 4: Receive the run_as_ret struct containing the return value and
1121 * errno
1122 */
1123 readlen = lttcomm_recv_unix_sock(worker->sockpair[0], ret_value,
1124 sizeof(*ret_value));
1125 if (!readlen) {
1126 ERR("Run-as worker has hung-up during run_as_cmd");
1127 ret = -1;
1128 ret_value->_errno = EIO;
1129 goto end;
1130 } else if (readlen < sizeof(*ret_value)) {
1131 PERROR("Error reading response from run_as");
1132 ret = -1;
1133 ret_value->_errno = errno;
1134 goto end;
1135 }
1136
1137 if (ret_value->_error) {
1138 /* Skip stage 5 on error as there will be no fd to receive. */
1139 goto end;
1140 }
1141
1142 /*
1143 * Stage 5: Receive file descriptor if needed
1144 */
1145 ret = recv_fds_from_worker(worker, cmd, ret_value);
1146 if (ret < 0) {
1147 ERR("Error receiving fd");
1148 ret = -1;
1149 ret_value->_errno = EIO;
1150 }
1151
1152 end:
1153 return ret;
1154 }
1155
1156 /*
1157 * This is for debugging ONLY and should not be considered secure.
1158 */
1159 static
1160 int run_as_noworker(enum run_as_cmd cmd,
1161 struct run_as_data *data, struct run_as_ret *ret_value,
1162 uid_t uid, gid_t gid)
1163 {
1164 int ret, saved_errno;
1165 mode_t old_mask;
1166 run_as_fct fct;
1167
1168 fct = run_as_enum_to_fct(cmd);
1169 if (!fct) {
1170 errno = -ENOSYS;
1171 ret = -1;
1172 goto end;
1173 }
1174 old_mask = umask(0);
1175 ret = fct(data, ret_value);
1176 saved_errno = ret_value->_errno;
1177 umask(old_mask);
1178 errno = saved_errno;
1179 end:
1180 return ret;
1181 }
1182
1183 static
1184 int reset_sighandler(void)
1185 {
1186 int sig;
1187
1188 DBG("Resetting run_as worker signal handlers to default");
1189 for (sig = 1; sig <= 31; sig++) {
1190 (void) signal(sig, SIG_DFL);
1191 }
1192 return 0;
1193 }
1194
1195 static
1196 void worker_sighandler(int sig)
1197 {
1198 const char *signame;
1199
1200 /*
1201 * The worker will inherit its parent's signals since they are part of
1202 * the same process group. However, in the case of SIGINT and SIGTERM,
1203 * we want to give the worker a chance to teardown gracefully when its
1204 * parent closes the command socket.
1205 */
1206 switch (sig) {
1207 case SIGINT:
1208 signame = "SIGINT";
1209 break;
1210 case SIGTERM:
1211 signame = "SIGTERM";
1212 break;
1213 default:
1214 signame = NULL;
1215 }
1216
1217 if (signame) {
1218 DBG("run_as worker received signal %s", signame);
1219 } else {
1220 DBG("run_as_worker received signal %d", sig);
1221 }
1222 }
1223
1224 static
1225 int set_worker_sighandlers(void)
1226 {
1227 int ret = 0;
1228 sigset_t sigset;
1229 struct sigaction sa;
1230
1231 if ((ret = sigemptyset(&sigset)) < 0) {
1232 PERROR("sigemptyset");
1233 goto end;
1234 }
1235
1236 sa.sa_handler = worker_sighandler;
1237 sa.sa_mask = sigset;
1238 sa.sa_flags = 0;
1239 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
1240 PERROR("sigaction SIGINT");
1241 goto end;
1242 }
1243
1244 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
1245 PERROR("sigaction SIGTERM");
1246 goto end;
1247 }
1248
1249 DBG("run_as signal handler set for SIGTERM and SIGINT");
1250 end:
1251 return ret;
1252 }
1253
1254 static
1255 int run_as_create_worker_no_lock(const char *procname,
1256 post_fork_cleanup_cb clean_up_func,
1257 void *clean_up_user_data)
1258 {
1259 pid_t pid;
1260 int i, ret = 0;
1261 ssize_t readlen;
1262 struct run_as_ret recvret;
1263 struct run_as_worker *worker;
1264
1265 assert(!global_worker);
1266 if (!use_clone()) {
1267 /*
1268 * Don't initialize a worker, all run_as tasks will be performed
1269 * in the current process.
1270 */
1271 ret = 0;
1272 goto end;
1273 }
1274 worker = zmalloc(sizeof(*worker));
1275 if (!worker) {
1276 ret = -ENOMEM;
1277 goto end;
1278 }
1279 worker->procname = strdup(procname);
1280 if (!worker->procname) {
1281 ret = -ENOMEM;
1282 goto error_procname_alloc;
1283 }
1284 /* Create unix socket. */
1285 if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) {
1286 ret = -1;
1287 goto error_sock;
1288 }
1289
1290 /* Fork worker. */
1291 pid = fork();
1292 if (pid < 0) {
1293 PERROR("fork");
1294 ret = -1;
1295 goto error_fork;
1296 } else if (pid == 0) {
1297 /* Child */
1298
1299 reset_sighandler();
1300
1301 set_worker_sighandlers();
1302 if (clean_up_func) {
1303 if (clean_up_func(clean_up_user_data) < 0) {
1304 ERR("Run-as post-fork clean-up failed, exiting.");
1305 exit(EXIT_FAILURE);
1306 }
1307 }
1308
1309 /* Just close, no shutdown. */
1310 if (close(worker->sockpair[0])) {
1311 PERROR("close");
1312 exit(EXIT_FAILURE);
1313 }
1314
1315 /*
1316 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
1317 * Sockpair[1] is used as a control channel with the master
1318 */
1319 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
1320 if (i != worker->sockpair[1]) {
1321 (void) close(i);
1322 }
1323 }
1324
1325 worker->sockpair[0] = -1;
1326 ret = run_as_worker(worker);
1327 if (lttcomm_close_unix_sock(worker->sockpair[1])) {
1328 PERROR("close");
1329 ret = -1;
1330 }
1331 worker->sockpair[1] = -1;
1332 free(worker->procname);
1333 free(worker);
1334 LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret);
1335 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
1336 } else {
1337 /* Parent */
1338
1339 /* Just close, no shutdown. */
1340 if (close(worker->sockpair[1])) {
1341 PERROR("close");
1342 ret = -1;
1343 goto error_fork;
1344 }
1345 worker->sockpair[1] = -1;
1346 worker->pid = pid;
1347 /* Wait for worker to become ready. */
1348 readlen = lttcomm_recv_unix_sock(worker->sockpair[0],
1349 &recvret, sizeof(recvret));
1350 if (readlen < sizeof(recvret)) {
1351 ERR("readlen: %zd", readlen);
1352 PERROR("Error reading response from run_as at creation");
1353 ret = -1;
1354 goto error_fork;
1355 }
1356 global_worker = worker;
1357 }
1358 end:
1359 return ret;
1360
1361 /* Error handling. */
1362 error_fork:
1363 for (i = 0; i < 2; i++) {
1364 if (worker->sockpair[i] < 0) {
1365 continue;
1366 }
1367 if (lttcomm_close_unix_sock(worker->sockpair[i])) {
1368 PERROR("close");
1369 }
1370 worker->sockpair[i] = -1;
1371 }
1372 error_sock:
1373 free(worker->procname);
1374 error_procname_alloc:
1375 free(worker);
1376 return ret;
1377 }
1378
1379 static
1380 void run_as_destroy_worker_no_lock(void)
1381 {
1382 struct run_as_worker *worker = global_worker;
1383
1384 DBG("Destroying run_as worker");
1385 if (!worker) {
1386 return;
1387 }
1388 /* Close unix socket */
1389 DBG("Closing run_as worker socket");
1390 if (lttcomm_close_unix_sock(worker->sockpair[0])) {
1391 PERROR("close");
1392 }
1393 worker->sockpair[0] = -1;
1394 /* Wait for worker. */
1395 for (;;) {
1396 int status;
1397 pid_t wait_ret;
1398
1399 wait_ret = waitpid(worker->pid, &status, 0);
1400 if (wait_ret < 0) {
1401 if (errno == EINTR) {
1402 continue;
1403 }
1404 PERROR("waitpid");
1405 break;
1406 }
1407
1408 if (WIFEXITED(status)) {
1409 LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR,
1410 DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d",
1411 WEXITSTATUS(status));
1412 break;
1413 } else if (WIFSIGNALED(status)) {
1414 ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d",
1415 WTERMSIG(status));
1416 break;
1417 }
1418 }
1419 free(worker->procname);
1420 free(worker);
1421 global_worker = NULL;
1422 }
1423
1424 static
1425 int run_as_restart_worker(struct run_as_worker *worker)
1426 {
1427 int ret = 0;
1428 char *procname = NULL;
1429
1430 procname = worker->procname;
1431
1432 /* Close socket to run_as worker process and clean up the zombie process */
1433 run_as_destroy_worker_no_lock();
1434
1435 /* Create a new run_as worker process*/
1436 ret = run_as_create_worker_no_lock(procname, NULL, NULL);
1437 if (ret < 0 ) {
1438 ERR("Restarting the worker process failed");
1439 ret = -1;
1440 goto err;
1441 }
1442 err:
1443 return ret;
1444 }
1445
1446 static
1447 int run_as(enum run_as_cmd cmd, struct run_as_data *data,
1448 struct run_as_ret *ret_value, uid_t uid, gid_t gid)
1449 {
1450 int ret, saved_errno;
1451
1452 pthread_mutex_lock(&worker_lock);
1453 if (use_clone()) {
1454 DBG("Using run_as worker");
1455
1456 assert(global_worker);
1457
1458 ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid);
1459 saved_errno = ret_value->_errno;
1460
1461 /*
1462 * If the worker thread crashed the errno is set to EIO. we log
1463 * the error and start a new worker process.
1464 */
1465 if (ret == -1 && saved_errno == EIO) {
1466 DBG("Socket closed unexpectedly... "
1467 "Restarting the worker process");
1468 ret = run_as_restart_worker(global_worker);
1469 if (ret == -1) {
1470 ERR("Failed to restart worker process.");
1471 goto err;
1472 }
1473 }
1474 } else {
1475 DBG("Using run_as without worker");
1476 ret = run_as_noworker(cmd, data, ret_value, uid, gid);
1477 }
1478 err:
1479 pthread_mutex_unlock(&worker_lock);
1480 return ret;
1481 }
1482
1483 LTTNG_HIDDEN
1484 int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
1485 {
1486 return run_as_mkdirat_recursive(AT_FDCWD, path, mode, uid, gid);
1487 }
1488
1489 LTTNG_HIDDEN
1490 int run_as_mkdirat_recursive(int dirfd, const char *path, mode_t mode,
1491 uid_t uid, gid_t gid)
1492 {
1493 int ret;
1494 struct run_as_data data = {};
1495 struct run_as_ret run_as_ret = {};
1496
1497 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1498 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1499 path, (int) mode, (int) uid, (int) gid);
1500 ret = lttng_strncpy(data.u.mkdir.path, path,
1501 sizeof(data.u.mkdir.path));
1502 if (ret) {
1503 ERR("Failed to copy path argument of mkdirat recursive command");
1504 goto error;
1505 }
1506 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1507 data.u.mkdir.mode = mode;
1508 data.u.mkdir.dirfd = dirfd;
1509 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR_RECURSIVE : RUN_AS_MKDIRAT_RECURSIVE,
1510 &data, &run_as_ret, uid, gid);
1511 errno = run_as_ret._errno;
1512 ret = run_as_ret.u.ret;
1513 error:
1514 return ret;
1515 }
1516
1517 LTTNG_HIDDEN
1518 int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
1519 {
1520 return run_as_mkdirat(AT_FDCWD, path, mode, uid, gid);
1521 }
1522
1523 LTTNG_HIDDEN
1524 int run_as_mkdirat(int dirfd, const char *path, mode_t mode,
1525 uid_t uid, gid_t gid)
1526 {
1527 int ret;
1528 struct run_as_data data = {};
1529 struct run_as_ret run_as_ret = {};
1530
1531 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1532 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1533 path, (int) mode, (int) uid, (int) gid);
1534 ret = lttng_strncpy(data.u.mkdir.path, path,
1535 sizeof(data.u.mkdir.path));
1536 if (ret) {
1537 ERR("Failed to copy path argument of mkdirat command");
1538 goto error;
1539 }
1540 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1541 data.u.mkdir.mode = mode;
1542 data.u.mkdir.dirfd = dirfd;
1543 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR : RUN_AS_MKDIRAT,
1544 &data, &run_as_ret, uid, gid);
1545 errno = run_as_ret._errno;
1546 ret = run_as_ret.u.ret;
1547 error:
1548 return ret;
1549 }
1550
1551 LTTNG_HIDDEN
1552 int run_as_open(const char *path, int flags, mode_t mode, uid_t uid,
1553 gid_t gid)
1554 {
1555 return run_as_openat(AT_FDCWD, path, flags, mode, uid, gid);
1556 }
1557
1558 LTTNG_HIDDEN
1559 int run_as_openat(int dirfd, const char *path, int flags, mode_t mode,
1560 uid_t uid, gid_t gid)
1561 {
1562 int ret;
1563 struct run_as_data data = {};
1564 struct run_as_ret run_as_ret = {};
1565
1566 DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
1567 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1568 path, flags, (int) mode, (int) uid, (int) gid);
1569 ret = lttng_strncpy(data.u.open.path, path, sizeof(data.u.open.path));
1570 if (ret) {
1571 ERR("Failed to copy path argument of open command");
1572 goto error;
1573 }
1574 data.u.open.flags = flags;
1575 data.u.open.mode = mode;
1576 data.u.open.dirfd = dirfd;
1577 run_as(dirfd == AT_FDCWD ? RUN_AS_OPEN : RUN_AS_OPENAT,
1578 &data, &run_as_ret, uid, gid);
1579 errno = run_as_ret._errno;
1580 ret = run_as_ret.u.ret < 0 ? run_as_ret.u.ret :
1581 run_as_ret.u.open.fd;
1582 error:
1583 return ret;
1584 }
1585
1586 LTTNG_HIDDEN
1587 int run_as_unlink(const char *path, uid_t uid, gid_t gid)
1588 {
1589 return run_as_unlinkat(AT_FDCWD, path, uid, gid);
1590 }
1591
1592 LTTNG_HIDDEN
1593 int run_as_unlinkat(int dirfd, const char *path, uid_t uid, gid_t gid)
1594 {
1595 int ret;
1596 struct run_as_data data = {};
1597 struct run_as_ret run_as_ret = {};
1598
1599 DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
1600 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1601 path, (int) uid, (int) gid);
1602 ret = lttng_strncpy(data.u.unlink.path, path,
1603 sizeof(data.u.unlink.path));
1604 if (ret) {
1605 goto error;
1606 }
1607 data.u.unlink.dirfd = dirfd;
1608 run_as(dirfd == AT_FDCWD ? RUN_AS_UNLINK : RUN_AS_UNLINKAT, &data,
1609 &run_as_ret, uid, gid);
1610 errno = run_as_ret._errno;
1611 ret = run_as_ret.u.ret;
1612 error:
1613 return ret;
1614 }
1615
1616 LTTNG_HIDDEN
1617 int run_as_rmdir(const char *path, uid_t uid, gid_t gid)
1618 {
1619 return run_as_rmdirat(AT_FDCWD, path, uid, gid);
1620 }
1621
1622 LTTNG_HIDDEN
1623 int run_as_rmdirat(int dirfd, const char *path, uid_t uid, gid_t gid)
1624 {
1625 int ret;
1626 struct run_as_data data = {};
1627 struct run_as_ret run_as_ret = {};
1628
1629 DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d",
1630 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1631 path, (int) uid, (int) gid);
1632 ret = lttng_strncpy(data.u.rmdir.path, path,
1633 sizeof(data.u.rmdir.path));
1634 if (ret) {
1635 goto error;
1636 }
1637 data.u.rmdir.dirfd = dirfd;
1638 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR : RUN_AS_RMDIRAT, &data,
1639 &run_as_ret, uid, gid);
1640 errno = run_as_ret._errno;
1641 ret = run_as_ret.u.ret;
1642 error:
1643 return ret;
1644 }
1645
1646 LTTNG_HIDDEN
1647 int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid, int flags)
1648 {
1649 return run_as_rmdirat_recursive(AT_FDCWD, path, uid, gid, flags);
1650 }
1651
1652 LTTNG_HIDDEN
1653 int run_as_rmdirat_recursive(int dirfd, const char *path, uid_t uid, gid_t gid, int flags)
1654 {
1655 int ret;
1656 struct run_as_data data = {};
1657 struct run_as_ret run_as_ret = {};
1658
1659 DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d",
1660 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1661 path, (int) uid, (int) gid);
1662 ret = lttng_strncpy(data.u.rmdir.path, path,
1663 sizeof(data.u.rmdir.path));
1664 if (ret) {
1665 goto error;
1666 }
1667 data.u.rmdir.dirfd = dirfd;
1668 data.u.rmdir.flags = flags;
1669 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR_RECURSIVE : RUN_AS_RMDIRAT_RECURSIVE,
1670 &data, &run_as_ret, uid, gid);
1671 errno = run_as_ret._errno;
1672 ret = run_as_ret.u.ret;
1673 error:
1674 return ret;
1675 }
1676
1677 LTTNG_HIDDEN
1678 int run_as_rename(const char *old, const char *new, uid_t uid, gid_t gid)
1679 {
1680 return run_as_renameat(AT_FDCWD, old, AT_FDCWD, new, uid, gid);
1681 }
1682
1683 LTTNG_HIDDEN
1684 int run_as_renameat(int old_dirfd, const char *old_name,
1685 int new_dirfd, const char *new_name, uid_t uid, gid_t gid)
1686 {
1687 int ret;
1688 struct run_as_data data = {};
1689 struct run_as_ret run_as_ret = {};
1690
1691 DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d",
1692 old_dirfd, old_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1693 old_name,
1694 new_dirfd, new_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1695 new_name, (int) uid, (int) gid);
1696 ret = lttng_strncpy(data.u.rename.old_path, old_name,
1697 sizeof(data.u.rename.old_path));
1698 if (ret) {
1699 goto error;
1700 }
1701 ret = lttng_strncpy(data.u.rename.new_path, new_name,
1702 sizeof(data.u.rename.new_path));
1703 if (ret) {
1704 goto error;
1705 }
1706
1707 data.u.rename.dirfds[0] = old_dirfd;
1708 data.u.rename.dirfds[1] = new_dirfd;
1709 run_as(old_dirfd == AT_FDCWD && new_dirfd == AT_FDCWD ?
1710 RUN_AS_RENAME : RUN_AS_RENAMEAT,
1711 &data, &run_as_ret, uid, gid);
1712 errno = run_as_ret._errno;
1713 ret = run_as_ret.u.ret;
1714 error:
1715 return ret;
1716 }
1717
1718 LTTNG_HIDDEN
1719 int run_as_extract_elf_symbol_offset(int fd, const char* function,
1720 uid_t uid, gid_t gid, uint64_t *offset)
1721 {
1722 int ret;
1723 struct run_as_data data = {};
1724 struct run_as_ret run_as_ret = {};
1725
1726 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
1727 "with for uid %d and gid %d", fd, function,
1728 (int) uid, (int) gid);
1729
1730 data.u.extract_elf_symbol_offset.fd = fd;
1731
1732 strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1);
1733 data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1734 ret = lttng_strncpy(data.u.extract_elf_symbol_offset.function,
1735 function,
1736 sizeof(data.u.extract_elf_symbol_offset.function));
1737 if (ret) {
1738 goto error;
1739 }
1740
1741 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &run_as_ret, uid, gid);
1742 errno = run_as_ret._errno;
1743 if (run_as_ret._error) {
1744 ret = -1;
1745 goto error;
1746 }
1747
1748 *offset = run_as_ret.u.extract_elf_symbol_offset.offset;
1749 error:
1750 return ret;
1751 }
1752
1753 LTTNG_HIDDEN
1754 int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name,
1755 const char* probe_name, uid_t uid, gid_t gid,
1756 uint64_t **offsets, uint32_t *num_offset)
1757 {
1758 int ret;
1759 struct run_as_data data = {};
1760 struct run_as_ret run_as_ret = {};
1761
1762 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
1763 "provider_name=%s with for uid %d and gid %d", fd,
1764 probe_name, provider_name, (int) uid, (int) gid);
1765
1766 data.u.extract_sdt_probe_offsets.fd = fd;
1767
1768 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name,
1769 sizeof(data.u.extract_sdt_probe_offsets.probe_name));
1770 if (ret) {
1771 goto error;
1772 }
1773 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.provider_name,
1774 provider_name,
1775 sizeof(data.u.extract_sdt_probe_offsets.provider_name));
1776 if (ret) {
1777 goto error;
1778 }
1779
1780 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &run_as_ret, uid, gid);
1781 errno = run_as_ret._errno;
1782 if (run_as_ret._error) {
1783 ret = -1;
1784 goto error;
1785 }
1786
1787 *num_offset = run_as_ret.u.extract_sdt_probe_offsets.num_offset;
1788 *offsets = zmalloc(*num_offset * sizeof(uint64_t));
1789 if (!*offsets) {
1790 ret = -ENOMEM;
1791 goto error;
1792 }
1793
1794 memcpy(*offsets, run_as_ret.u.extract_sdt_probe_offsets.offsets,
1795 *num_offset * sizeof(uint64_t));
1796 error:
1797 return ret;
1798 }
1799
1800 LTTNG_HIDDEN
1801 int run_as_generate_filter_bytecode(const char *filter_expression,
1802 const struct lttng_credentials *creds,
1803 struct lttng_bytecode **bytecode)
1804 {
1805 int ret;
1806 struct run_as_data data = {};
1807 struct run_as_ret run_as_ret = {};
1808 const struct lttng_bytecode *view_bytecode = NULL;
1809 struct lttng_bytecode *local_bytecode = NULL;
1810 const uid_t uid = lttng_credentials_get_uid(creds);
1811 const gid_t gid = lttng_credentials_get_gid(creds);
1812
1813 DBG3("generate_filter_bytecode() from expression=\"%s\" for uid %d and gid %d",
1814 filter_expression, (int) uid, (int) gid);
1815
1816 ret = lttng_strncpy(data.u.generate_filter_bytecode.filter_expression, filter_expression,
1817 sizeof(data.u.generate_filter_bytecode.filter_expression));
1818 if (ret) {
1819 goto error;
1820 }
1821
1822 run_as(RUN_AS_GENERATE_FILTER_BYTECODE, &data, &run_as_ret, uid, gid);
1823 errno = run_as_ret._errno;
1824 if (run_as_ret._error) {
1825 ret = -1;
1826 goto error;
1827 }
1828
1829 view_bytecode = (const struct lttng_bytecode *) run_as_ret.u.generate_filter_bytecode.bytecode;
1830
1831 local_bytecode = zmalloc(sizeof(*local_bytecode) + view_bytecode->len);
1832 if (!local_bytecode) {
1833 ret = -ENOMEM;
1834 goto error;
1835 }
1836
1837 memcpy(local_bytecode, run_as_ret.u.generate_filter_bytecode.bytecode,
1838 sizeof(*local_bytecode) + view_bytecode->len);
1839 *bytecode = local_bytecode;
1840 error:
1841 return ret;
1842 }
1843
1844 LTTNG_HIDDEN
1845 int run_as_create_worker(const char *procname,
1846 post_fork_cleanup_cb clean_up_func,
1847 void *clean_up_user_data)
1848 {
1849 int ret;
1850
1851 pthread_mutex_lock(&worker_lock);
1852 ret = run_as_create_worker_no_lock(procname, clean_up_func,
1853 clean_up_user_data);
1854 pthread_mutex_unlock(&worker_lock);
1855 return ret;
1856 }
1857
1858 LTTNG_HIDDEN
1859 void run_as_destroy_worker(void)
1860 {
1861 pthread_mutex_lock(&worker_lock);
1862 run_as_destroy_worker_no_lock();
1863 pthread_mutex_unlock(&worker_lock);
1864 }
This page took 0.066429 seconds and 4 git commands to generate.