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