2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
36 #include <common/lttng-kernel.h>
37 #include <common/common.h>
38 #include <common/utils.h>
39 #include <common/compat/getenv.h>
40 #include <common/compat/prctl.h>
41 #include <common/unix.h>
42 #include <common/defaults.h>
43 #include <common/lttng-elf.h>
45 #include <lttng/constant.h>
51 typedef int (*run_as_fct
)(struct run_as_data
*data
, struct run_as_ret
*ret_value
);
56 RUN_AS_MKDIR_RECURSIVE
,
57 RUN_AS_MKDIRAT_RECURSIVE
,
64 RUN_AS_RMDIR_RECURSIVE
,
65 RUN_AS_RMDIRAT_RECURSIVE
,
68 RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
,
69 RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
,
72 struct run_as_mkdir_data
{
74 char path
[LTTNG_PATH_MAX
];
78 struct run_as_open_data
{
80 char path
[LTTNG_PATH_MAX
];
85 struct run_as_unlink_data
{
87 char path
[LTTNG_PATH_MAX
];
90 struct run_as_rmdir_data
{
92 char path
[LTTNG_PATH_MAX
];
93 int flags
; /* enum lttng_directory_handle_rmdir_recursive_flags */
96 struct run_as_extract_elf_symbol_offset_data
{
98 char function
[LTTNG_SYMBOL_NAME_LEN
];
101 struct run_as_extract_sdt_probe_offsets_data
{
103 char probe_name
[LTTNG_SYMBOL_NAME_LEN
];
104 char provider_name
[LTTNG_SYMBOL_NAME_LEN
];
107 struct run_as_rename_data
{
113 char old_path
[LTTNG_PATH_MAX
];
114 char new_path
[LTTNG_PATH_MAX
];
117 struct run_as_open_ret
{
121 struct run_as_extract_elf_symbol_offset_ret
{
125 struct run_as_extract_sdt_probe_offsets_ret
{
127 uint64_t offsets
[LTTNG_KERNEL_MAX_UPROBE_NUM
];
133 struct run_as_mkdir_data mkdir
;
134 struct run_as_open_data open
;
135 struct run_as_unlink_data unlink
;
136 struct run_as_rmdir_data rmdir
;
137 struct run_as_rename_data rename
;
138 struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset
;
139 struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets
;
146 * The run_as_ret structure holds the returned value and status of the command.
148 * The `u` union field holds the return value of the command; in most cases it
149 * represents the success or the failure of the command. In more complex
150 * commands, it holds a computed value.
152 * The _errno field is the errno recorded after the execution of the command.
154 * The _error fields is used the signify that return status of the command. For
155 * simple commands returning `int` the _error field will be the same as the
156 * ret_int field. In complex commands, it signify the success or failure of the
163 struct run_as_open_ret open
;
164 struct run_as_extract_elf_symbol_offset_ret extract_elf_symbol_offset
;
165 struct run_as_extract_sdt_probe_offsets_ret extract_sdt_probe_offsets
;
171 #define COMMAND_IN_FDS(data_ptr) ({ \
173 if (command_properties[data_ptr->cmd].in_fds_offset != -1) { \
174 fds = (int *) ((char *) data_ptr + command_properties[data_ptr->cmd].in_fds_offset); \
179 #define COMMAND_OUT_FDS(cmd, ret_ptr) ({ \
181 if (command_properties[cmd].out_fds_offset != -1) { \
182 fds = (int *) ((char *) ret_ptr + command_properties[cmd].out_fds_offset); \
187 #define COMMAND_IN_FD_COUNT(data_ptr) ({ \
188 command_properties[data_ptr->cmd].in_fd_count; \
191 #define COMMAND_OUT_FD_COUNT(cmd) ({ \
192 command_properties[cmd].out_fd_count; \
195 #define COMMAND_USE_CWD_FD(data_ptr) command_properties[data_ptr->cmd].use_cwd_fd
197 struct run_as_command_properties
{
198 /* Set to -1 when not applicable. */
199 ptrdiff_t in_fds_offset
, out_fds_offset
;
200 unsigned int in_fd_count
, out_fd_count
;
204 static const struct run_as_command_properties command_properties
[] = {
206 .in_fds_offset
= offsetof(struct run_as_data
, u
.mkdir
.dirfd
),
208 .out_fds_offset
= -1,
213 .in_fds_offset
= offsetof(struct run_as_data
, u
.mkdir
.dirfd
),
215 .out_fds_offset
= -1,
219 [RUN_AS_MKDIR_RECURSIVE
] = {
220 .in_fds_offset
= offsetof(struct run_as_data
, u
.mkdir
.dirfd
),
222 .out_fds_offset
= -1,
226 [RUN_AS_MKDIRAT_RECURSIVE
] = {
227 .in_fds_offset
= offsetof(struct run_as_data
, u
.mkdir
.dirfd
),
229 .out_fds_offset
= -1,
234 .in_fds_offset
= offsetof(struct run_as_data
, u
.open
.dirfd
),
236 .out_fds_offset
= offsetof(struct run_as_ret
, u
.open
.fd
),
241 .in_fds_offset
= offsetof(struct run_as_data
, u
.open
.dirfd
),
243 .out_fds_offset
= offsetof(struct run_as_ret
, u
.open
.fd
),
248 .in_fds_offset
= offsetof(struct run_as_data
, u
.unlink
.dirfd
),
250 .out_fds_offset
= -1,
254 [RUN_AS_UNLINKAT
] = {
255 .in_fds_offset
= offsetof(struct run_as_data
, u
.unlink
.dirfd
),
257 .out_fds_offset
= -1,
261 [RUN_AS_RMDIR_RECURSIVE
] = {
262 .in_fds_offset
= offsetof(struct run_as_data
, u
.rmdir
.dirfd
),
264 .out_fds_offset
= -1,
268 [RUN_AS_RMDIRAT_RECURSIVE
] = {
269 .in_fds_offset
= offsetof(struct run_as_data
, u
.rmdir
.dirfd
),
271 .out_fds_offset
= -1,
276 .in_fds_offset
= offsetof(struct run_as_data
, u
.rmdir
.dirfd
),
278 .out_fds_offset
= -1,
283 .in_fds_offset
= offsetof(struct run_as_data
, u
.rmdir
.dirfd
),
285 .out_fds_offset
= -1,
290 .in_fds_offset
= offsetof(struct run_as_data
, u
.rename
.dirfds
),
292 .out_fds_offset
= -1,
296 [RUN_AS_RENAMEAT
] = {
297 .in_fds_offset
= offsetof(struct run_as_data
, u
.rename
.dirfds
),
299 .out_fds_offset
= -1,
303 [RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
] = {
304 .in_fds_offset
= offsetof(struct run_as_data
,
305 u
.extract_elf_symbol_offset
.fd
),
307 .out_fds_offset
= -1,
311 [RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
] = {
312 .in_fds_offset
= offsetof(struct run_as_data
,
313 u
.extract_sdt_probe_offsets
.fd
),
315 .out_fds_offset
= -1,
321 struct run_as_worker
{
322 pid_t pid
; /* Worker PID. */
327 /* Single global worker per process (for now). */
328 static struct run_as_worker
*global_worker
;
329 /* Lock protecting the worker. */
330 static pthread_mutex_t worker_lock
= PTHREAD_MUTEX_INITIALIZER
;
342 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
347 * Create recursively directory using the FULL path.
350 int _mkdirat_recursive(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
354 struct lttng_directory_handle handle
;
356 path
= data
->u
.mkdir
.path
;
357 mode
= data
->u
.mkdir
.mode
;
359 (void) lttng_directory_handle_init_from_dirfd(&handle
,
360 data
->u
.mkdir
.dirfd
);
361 /* Ownership of dirfd is transferred to the handle. */
362 data
->u
.mkdir
.dirfd
= -1;
363 /* Safe to call as we have transitioned to the requested uid/gid. */
365 lttng_directory_handle_create_subdirectory_recursive(
366 &handle
, path
, mode
);
367 ret_value
->_errno
= errno
;
368 ret_value
->_error
= (ret_value
->u
.ret
) ? true : false;
369 lttng_directory_handle_fini(&handle
);
370 return ret_value
->u
.ret
;
374 int _mkdirat(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
378 struct lttng_directory_handle handle
;
380 path
= data
->u
.mkdir
.path
;
381 mode
= data
->u
.mkdir
.mode
;
383 (void) lttng_directory_handle_init_from_dirfd(&handle
,
384 data
->u
.mkdir
.dirfd
);
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. */
389 lttng_directory_handle_create_subdirectory(
390 &handle
, path
, mode
);
391 ret_value
->_errno
= errno
;
392 ret_value
->_error
= (ret_value
->u
.ret
) ? true : false;
393 lttng_directory_handle_fini(&handle
);
394 return ret_value
->u
.ret
;
398 int _open(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
401 struct lttng_directory_handle handle
;
403 (void) lttng_directory_handle_init_from_dirfd(&handle
,
405 /* Ownership of dirfd is transferred to the handle. */
406 data
->u
.open
.dirfd
= -1;
408 fd
= lttng_directory_handle_open_file(&handle
,
409 data
->u
.open
.path
, data
->u
.open
.flags
,
412 ret_value
->u
.ret
= -1;
413 ret_value
->u
.open
.fd
= -1;
415 ret_value
->u
.ret
= 0;
416 ret_value
->u
.open
.fd
= fd
;
419 ret_value
->_errno
= errno
;
420 ret_value
->_error
= fd
< 0;
421 lttng_directory_handle_fini(&handle
);
422 return ret_value
->u
.ret
;
426 int _unlink(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
428 struct lttng_directory_handle handle
;
430 (void) lttng_directory_handle_init_from_dirfd(&handle
,
431 data
->u
.unlink
.dirfd
);
433 /* Ownership of dirfd is transferred to the handle. */
434 data
->u
.unlink
.dirfd
= -1;
436 ret_value
->u
.ret
= lttng_directory_handle_unlink_file(&handle
,
437 data
->u
.unlink
.path
);
438 ret_value
->_errno
= errno
;
439 ret_value
->_error
= (ret_value
->u
.ret
) ? true : false;
440 lttng_directory_handle_fini(&handle
);
441 return ret_value
->u
.ret
;
445 int _rmdir(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
447 struct lttng_directory_handle handle
;
449 (void) lttng_directory_handle_init_from_dirfd(&handle
,
450 data
->u
.rmdir
.dirfd
);
452 /* Ownership of dirfd is transferred to the handle. */
453 data
->u
.rmdir
.dirfd
= -1;
455 ret_value
->u
.ret
= lttng_directory_handle_remove_subdirectory(
456 &handle
, data
->u
.rmdir
.path
);
457 ret_value
->_errno
= errno
;
458 ret_value
->_error
= (ret_value
->u
.ret
) ? true : false;
459 lttng_directory_handle_fini(&handle
);
460 return ret_value
->u
.ret
;
464 int _rmdir_recursive(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
466 struct lttng_directory_handle handle
;
468 (void) lttng_directory_handle_init_from_dirfd(&handle
,
469 data
->u
.rmdir
.dirfd
);
471 /* Ownership of dirfd is transferred to the handle. */
472 data
->u
.rmdir
.dirfd
= -1;
474 ret_value
->u
.ret
= lttng_directory_handle_remove_subdirectory_recursive(
475 &handle
, data
->u
.rmdir
.path
, data
->u
.rmdir
.flags
);
476 ret_value
->_errno
= errno
;
477 ret_value
->_error
= (ret_value
->u
.ret
) ? true : false;
478 lttng_directory_handle_fini(&handle
);
479 return ret_value
->u
.ret
;
483 int _rename(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
485 const char *old_path
, *new_path
;
486 struct lttng_directory_handle old_handle
, new_handle
;
488 old_path
= data
->u
.rename
.old_path
;
489 new_path
= data
->u
.rename
.new_path
;
491 (void) lttng_directory_handle_init_from_dirfd(&old_handle
,
492 data
->u
.rename
.dirfds
[0]);
493 (void) lttng_directory_handle_init_from_dirfd(&new_handle
,
494 data
->u
.rename
.dirfds
[1]);
496 /* Ownership of dirfds are transferred to the handles. */
497 data
->u
.rename
.dirfds
[0] = data
->u
.rename
.dirfds
[1] = -1;
499 /* Safe to call as we have transitioned to the requested uid/gid. */
500 ret_value
->u
.ret
= lttng_directory_handle_rename(
501 &old_handle
, old_path
, &new_handle
, new_path
);
502 ret_value
->_errno
= errno
;
503 ret_value
->_error
= (ret_value
->u
.ret
) ? true : false;
504 lttng_directory_handle_fini(&old_handle
);
505 lttng_directory_handle_fini(&new_handle
);
506 return ret_value
->u
.ret
;
511 int _extract_elf_symbol_offset(struct run_as_data
*data
,
512 struct run_as_ret
*ret_value
)
516 ret_value
->_error
= false;
517 ret
= lttng_elf_get_symbol_offset(data
->u
.extract_elf_symbol_offset
.fd
,
518 data
->u
.extract_elf_symbol_offset
.function
,
519 &ret_value
->u
.extract_elf_symbol_offset
.offset
);
521 DBG("Failed to extract ELF function offset");
522 ret_value
->_error
= true;
529 int _extract_sdt_probe_offsets(struct run_as_data
*data
,
530 struct run_as_ret
*ret_value
)
533 uint64_t *offsets
= NULL
;
536 ret_value
->_error
= false;
538 /* On success, this call allocates the offsets paramater. */
539 ret
= lttng_elf_get_sdt_probe_offsets(
540 data
->u
.extract_sdt_probe_offsets
.fd
,
541 data
->u
.extract_sdt_probe_offsets
.provider_name
,
542 data
->u
.extract_sdt_probe_offsets
.probe_name
,
543 &offsets
, &num_offset
);
546 DBG("Failed to extract SDT probe offsets");
547 ret_value
->_error
= true;
551 if (num_offset
<= 0 || num_offset
> LTTNG_KERNEL_MAX_UPROBE_NUM
) {
552 DBG("Wrong number of probes.");
554 ret_value
->_error
= true;
558 /* Copy the content of the offsets array to the ret struct. */
559 memcpy(ret_value
->u
.extract_sdt_probe_offsets
.offsets
,
560 offsets
, num_offset
* sizeof(uint64_t));
562 ret_value
->u
.extract_sdt_probe_offsets
.num_offset
= num_offset
;
571 int _extract_elf_symbol_offset(struct run_as_data
*data
,
572 struct run_as_ret
*ret_value
)
574 ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET");
579 int _extract_sdt_probe_offsets(struct run_as_data
*data
,
580 struct run_as_ret
*ret_value
)
582 ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS");
588 run_as_fct
run_as_enum_to_fct(enum run_as_cmd cmd
)
594 case RUN_AS_MKDIR_RECURSIVE
:
595 case RUN_AS_MKDIRAT_RECURSIVE
:
596 return _mkdirat_recursive
;
601 case RUN_AS_UNLINKAT
:
606 case RUN_AS_RMDIR_RECURSIVE
:
607 case RUN_AS_RMDIRAT_RECURSIVE
:
608 return _rmdir_recursive
;
610 case RUN_AS_RENAMEAT
:
612 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
613 return _extract_elf_symbol_offset
;
614 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
615 return _extract_sdt_probe_offsets
;
617 ERR("Unknown command %d", (int) cmd
);
623 int do_send_fds(int sock
, const int *fds
, unsigned int fd_count
)
628 for (i
= 0; i
< fd_count
; i
++) {
630 ERR("Attempt to send invalid file descriptor to master (fd = %i)",
632 /* Return 0 as this is not a fatal error. */
637 len
= lttcomm_send_fds_unix_sock(sock
, fds
, fd_count
);
638 return len
< 0 ? -1 : 0;
642 int do_recv_fds(int sock
, int *fds
, unsigned int fd_count
)
648 len
= lttcomm_recv_fds_unix_sock(sock
, fds
, fd_count
);
652 } else if (len
< 0) {
653 PERROR("Failed to receive file descriptors from socket");
658 for (i
= 0; i
< fd_count
; i
++) {
660 ERR("Invalid file descriptor received from worker (fd = %i)", fds
[i
]);
661 /* Return 0 as this is not a fatal error. */
669 int send_fds_to_worker(const struct run_as_worker
*worker
,
670 const struct run_as_data
*data
)
675 if (COMMAND_USE_CWD_FD(data
) || COMMAND_IN_FD_COUNT(data
) == 0) {
679 for (i
= 0; i
< COMMAND_IN_FD_COUNT(data
); i
++) {
680 if (COMMAND_IN_FDS(data
)[i
] < 0) {
681 ERR("Refusing to send invalid fd to worker (fd = %i)",
682 COMMAND_IN_FDS(data
)[i
]);
688 ret
= do_send_fds(worker
->sockpair
[0], COMMAND_IN_FDS(data
),
689 COMMAND_IN_FD_COUNT(data
));
691 PERROR("Failed to send file descriptor to run-as worker");
700 int send_fds_to_master(struct run_as_worker
*worker
, enum run_as_cmd cmd
,
701 struct run_as_ret
*run_as_ret
)
706 if (COMMAND_OUT_FD_COUNT(cmd
) == 0) {
710 ret
= do_send_fds(worker
->sockpair
[1], COMMAND_OUT_FDS(cmd
, run_as_ret
),
711 COMMAND_OUT_FD_COUNT(cmd
));
713 PERROR("Failed to send file descriptor to master process");
717 for (i
= 0; i
< COMMAND_OUT_FD_COUNT(cmd
); i
++) {
718 int ret_close
= close(COMMAND_OUT_FDS(cmd
, run_as_ret
)[i
]);
721 PERROR("Failed to close result file descriptor");
729 int recv_fds_from_worker(const struct run_as_worker
*worker
, enum run_as_cmd cmd
,
730 struct run_as_ret
*run_as_ret
)
734 if (COMMAND_OUT_FD_COUNT(cmd
) == 0) {
738 ret
= do_recv_fds(worker
->sockpair
[0], COMMAND_OUT_FDS(cmd
, run_as_ret
),
739 COMMAND_OUT_FD_COUNT(cmd
));
741 PERROR("Failed to receive file descriptor from run-as worker");
749 int recv_fds_from_master(struct run_as_worker
*worker
, struct run_as_data
*data
)
753 if (COMMAND_USE_CWD_FD(data
)) {
756 for (i
= 0; i
< COMMAND_IN_FD_COUNT(data
); i
++) {
757 COMMAND_IN_FDS(data
)[i
] = AT_FDCWD
;
762 ret
= do_recv_fds(worker
->sockpair
[1], COMMAND_IN_FDS(data
),
763 COMMAND_IN_FD_COUNT(data
));
765 PERROR("Failed to receive file descriptors from master process");
773 int cleanup_received_fds(struct run_as_data
*data
)
777 for (i
= 0; i
< COMMAND_IN_FD_COUNT(data
); i
++) {
778 if (COMMAND_IN_FDS(data
)[i
] == -1) {
781 ret
= close(COMMAND_IN_FDS(data
)[i
]);
783 PERROR("Failed to close file descriptor received fd in run-as worker");
792 * Return < 0 on error, 0 if OK, 1 on hangup.
795 int handle_one_cmd(struct run_as_worker
*worker
)
798 struct run_as_data data
= {};
799 ssize_t readlen
, writelen
;
800 struct run_as_ret sendret
= {};
805 * Stage 1: Receive run_as_data struct from the master.
806 * The structure contains the command type and all the parameters needed for
809 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[1], &data
,
816 if (readlen
< sizeof(data
)) {
817 PERROR("lttcomm_recv_unix_sock error");
822 cmd
= run_as_enum_to_fct(data
.cmd
);
829 * Stage 2: Receive file descriptor from master.
830 * Some commands need a file descriptor as input so if it's needed we
831 * receive the fd using the Unix socket.
833 ret
= recv_fds_from_master(worker
, &data
);
835 PERROR("recv_fd_from_master error");
840 prev_euid
= getuid();
841 if (data
.gid
!= getegid()) {
842 ret
= setegid(data
.gid
);
844 sendret
._error
= true;
845 sendret
._errno
= errno
;
850 if (data
.uid
!= prev_euid
) {
851 ret
= seteuid(data
.uid
);
853 sendret
._error
= true;
854 sendret
._errno
= errno
;
861 * Also set umask to 0 for mkdir executable bit.
866 * Stage 3: Execute the command
868 ret
= (*cmd
)(&data
, &sendret
);
870 DBG("Execution of command returned an error");
874 ret
= cleanup_received_fds(&data
);
876 ERR("Error cleaning up FD");
881 * Stage 4: Send run_as_ret structure to the master.
882 * This structure contain the return value of the command and the errno.
884 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
886 if (writelen
< sizeof(sendret
)) {
887 PERROR("lttcomm_send_unix_sock error");
893 * Stage 5: Send resulting file descriptors to the master.
895 ret
= send_fds_to_master(worker
, data
.cmd
, &sendret
);
897 DBG("Sending FD to master returned an error");
901 if (seteuid(prev_euid
) < 0) {
912 int run_as_worker(struct run_as_worker
*worker
)
916 struct run_as_ret sendret
;
917 size_t proc_orig_len
;
920 * Initialize worker. Set a different process cmdline.
922 proc_orig_len
= strlen(worker
->procname
);
923 memset(worker
->procname
, 0, proc_orig_len
);
924 strncpy(worker
->procname
, DEFAULT_RUN_AS_WORKER_NAME
, proc_orig_len
);
926 ret
= lttng_prctl(PR_SET_NAME
,
927 (unsigned long) DEFAULT_RUN_AS_WORKER_NAME
, 0, 0, 0);
928 if (ret
&& ret
!= -ENOSYS
) {
929 /* Don't fail as this is not essential. */
930 PERROR("prctl PR_SET_NAME");
933 memset(&sendret
, 0, sizeof(sendret
));
935 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
937 if (writelen
< sizeof(sendret
)) {
938 PERROR("lttcomm_send_unix_sock error");
944 ret
= handle_one_cmd(worker
);
948 } else if (ret
> 0) {
951 continue; /* Next command. */
960 int run_as_cmd(struct run_as_worker
*worker
,
962 struct run_as_data
*data
,
963 struct run_as_ret
*ret_value
,
964 uid_t uid
, gid_t gid
)
967 ssize_t readlen
, writelen
;
970 * If we are non-root, we can only deal with our own uid.
972 if (geteuid() != 0) {
973 if (uid
!= geteuid()) {
975 ret_value
->_errno
= EPERM
;
976 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
977 (int) uid
, (int) geteuid());
987 * Stage 1: Send the run_as_data struct to the worker process
989 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[0], data
,
991 if (writelen
< sizeof(*data
)) {
992 PERROR("Error writing message to run_as");
994 ret_value
->_errno
= EIO
;
999 * Stage 2: Send file descriptor to the worker process if needed
1001 ret
= send_fds_to_worker(worker
, data
);
1003 PERROR("do_send_fd error");
1005 ret_value
->_errno
= EIO
;
1010 * Stage 3: Wait for the execution of the command
1014 * Stage 4: Receive the run_as_ret struct containing the return value and
1017 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0], ret_value
,
1018 sizeof(*ret_value
));
1020 ERR("Run-as worker has hung-up during run_as_cmd");
1022 ret_value
->_errno
= EIO
;
1024 } else if (readlen
< sizeof(*ret_value
)) {
1025 PERROR("Error reading response from run_as");
1027 ret_value
->_errno
= errno
;
1031 if (ret_value
->_error
) {
1032 /* Skip stage 5 on error as there will be no fd to receive. */
1037 * Stage 5: Receive file descriptor if needed
1039 ret
= recv_fds_from_worker(worker
, cmd
, ret_value
);
1041 ERR("Error receiving fd");
1043 ret_value
->_errno
= EIO
;
1051 * This is for debugging ONLY and should not be considered secure.
1054 int run_as_noworker(enum run_as_cmd cmd
,
1055 struct run_as_data
*data
, struct run_as_ret
*ret_value
,
1056 uid_t uid
, gid_t gid
)
1058 int ret
, saved_errno
;
1062 fct
= run_as_enum_to_fct(cmd
);
1068 old_mask
= umask(0);
1069 ret
= fct(data
, ret_value
);
1070 saved_errno
= ret_value
->_errno
;
1072 errno
= saved_errno
;
1078 int reset_sighandler(void)
1082 DBG("Resetting run_as worker signal handlers to default");
1083 for (sig
= 1; sig
<= 31; sig
++) {
1084 (void) signal(sig
, SIG_DFL
);
1090 void worker_sighandler(int sig
)
1092 const char *signame
;
1095 * The worker will inherit its parent's signals since they are part of
1096 * the same process group. However, in the case of SIGINT and SIGTERM,
1097 * we want to give the worker a chance to teardown gracefully when its
1098 * parent closes the command socket.
1105 signame
= "SIGTERM";
1112 DBG("run_as worker received signal %s", signame
);
1114 DBG("run_as_worker received signal %d", sig
);
1119 int set_worker_sighandlers(void)
1123 struct sigaction sa
;
1125 if ((ret
= sigemptyset(&sigset
)) < 0) {
1126 PERROR("sigemptyset");
1130 sa
.sa_handler
= worker_sighandler
;
1131 sa
.sa_mask
= sigset
;
1133 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1134 PERROR("sigaction SIGINT");
1138 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1139 PERROR("sigaction SIGTERM");
1143 DBG("run_as signal handler set for SIGTERM and SIGINT");
1149 int run_as_create_worker_no_lock(const char *procname
,
1150 post_fork_cleanup_cb clean_up_func
,
1151 void *clean_up_user_data
)
1156 struct run_as_ret recvret
;
1157 struct run_as_worker
*worker
;
1159 assert(!global_worker
);
1162 * Don't initialize a worker, all run_as tasks will be performed
1163 * in the current process.
1168 worker
= zmalloc(sizeof(*worker
));
1173 worker
->procname
= strdup(procname
);
1174 if (!worker
->procname
) {
1176 goto error_procname_alloc
;
1178 /* Create unix socket. */
1179 if (lttcomm_create_anon_unix_socketpair(worker
->sockpair
) < 0) {
1190 } else if (pid
== 0) {
1195 set_worker_sighandlers();
1196 if (clean_up_func
) {
1197 if (clean_up_func(clean_up_user_data
) < 0) {
1198 ERR("Run-as post-fork clean-up failed, exiting.");
1203 /* Just close, no shutdown. */
1204 if (close(worker
->sockpair
[0])) {
1210 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
1211 * Sockpair[1] is used as a control channel with the master
1213 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1214 if (i
!= worker
->sockpair
[1]) {
1219 worker
->sockpair
[0] = -1;
1220 ret
= run_as_worker(worker
);
1221 if (lttcomm_close_unix_sock(worker
->sockpair
[1])) {
1225 worker
->sockpair
[1] = -1;
1226 free(worker
->procname
);
1228 LOG(ret
? PRINT_ERR
: PRINT_DBG
, "run_as worker exiting (ret = %d)", ret
);
1229 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
1233 /* Just close, no shutdown. */
1234 if (close(worker
->sockpair
[1])) {
1239 worker
->sockpair
[1] = -1;
1241 /* Wait for worker to become ready. */
1242 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0],
1243 &recvret
, sizeof(recvret
));
1244 if (readlen
< sizeof(recvret
)) {
1245 ERR("readlen: %zd", readlen
);
1246 PERROR("Error reading response from run_as at creation");
1250 global_worker
= worker
;
1255 /* Error handling. */
1257 for (i
= 0; i
< 2; i
++) {
1258 if (worker
->sockpair
[i
] < 0) {
1261 if (lttcomm_close_unix_sock(worker
->sockpair
[i
])) {
1264 worker
->sockpair
[i
] = -1;
1267 free(worker
->procname
);
1268 error_procname_alloc
:
1274 void run_as_destroy_worker_no_lock(void)
1276 struct run_as_worker
*worker
= global_worker
;
1278 DBG("Destroying run_as worker");
1282 /* Close unix socket */
1283 DBG("Closing run_as worker socket");
1284 if (lttcomm_close_unix_sock(worker
->sockpair
[0])) {
1287 worker
->sockpair
[0] = -1;
1288 /* Wait for worker. */
1293 wait_ret
= waitpid(worker
->pid
, &status
, 0);
1295 if (errno
== EINTR
) {
1302 if (WIFEXITED(status
)) {
1303 LOG(WEXITSTATUS(status
) == 0 ? PRINT_DBG
: PRINT_ERR
,
1304 DEFAULT_RUN_AS_WORKER_NAME
" terminated with status code %d",
1305 WEXITSTATUS(status
));
1307 } else if (WIFSIGNALED(status
)) {
1308 ERR(DEFAULT_RUN_AS_WORKER_NAME
" was killed by signal %d",
1313 free(worker
->procname
);
1315 global_worker
= NULL
;
1319 int run_as_restart_worker(struct run_as_worker
*worker
)
1322 char *procname
= NULL
;
1324 procname
= worker
->procname
;
1326 /* Close socket to run_as worker process and clean up the zombie process */
1327 run_as_destroy_worker_no_lock();
1329 /* Create a new run_as worker process*/
1330 ret
= run_as_create_worker_no_lock(procname
, NULL
, NULL
);
1332 ERR("Restarting the worker process failed");
1341 int run_as(enum run_as_cmd cmd
, struct run_as_data
*data
,
1342 struct run_as_ret
*ret_value
, uid_t uid
, gid_t gid
)
1344 int ret
, saved_errno
;
1346 pthread_mutex_lock(&worker_lock
);
1348 DBG("Using run_as worker");
1350 assert(global_worker
);
1352 ret
= run_as_cmd(global_worker
, cmd
, data
, ret_value
, uid
, gid
);
1353 saved_errno
= ret_value
->_errno
;
1356 * If the worker thread crashed the errno is set to EIO. we log
1357 * the error and start a new worker process.
1359 if (ret
== -1 && saved_errno
== EIO
) {
1360 DBG("Socket closed unexpectedly... "
1361 "Restarting the worker process");
1362 ret
= run_as_restart_worker(global_worker
);
1365 ERR("Failed to restart worker process.");
1370 DBG("Using run_as without worker");
1371 ret
= run_as_noworker(cmd
, data
, ret_value
, uid
, gid
);
1374 pthread_mutex_unlock(&worker_lock
);
1379 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1381 return run_as_mkdirat_recursive(AT_FDCWD
, path
, mode
, uid
, gid
);
1385 int run_as_mkdirat_recursive(int dirfd
, const char *path
, mode_t mode
,
1386 uid_t uid
, gid_t gid
)
1389 struct run_as_data data
= {};
1390 struct run_as_ret run_as_ret
= {};
1392 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1393 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1394 path
, (int) mode
, (int) uid
, (int) gid
);
1395 ret
= lttng_strncpy(data
.u
.mkdir
.path
, path
,
1396 sizeof(data
.u
.mkdir
.path
));
1398 ERR("Failed to copy path argument of mkdirat recursive command");
1401 data
.u
.mkdir
.path
[sizeof(data
.u
.mkdir
.path
) - 1] = '\0';
1402 data
.u
.mkdir
.mode
= mode
;
1403 data
.u
.mkdir
.dirfd
= dirfd
;
1404 run_as(dirfd
== AT_FDCWD
? RUN_AS_MKDIR_RECURSIVE
: RUN_AS_MKDIRAT_RECURSIVE
,
1405 &data
, &run_as_ret
, uid
, gid
);
1406 errno
= run_as_ret
._errno
;
1407 ret
= run_as_ret
.u
.ret
;
1413 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1415 return run_as_mkdirat(AT_FDCWD
, path
, mode
, uid
, gid
);
1419 int run_as_mkdirat(int dirfd
, const char *path
, mode_t mode
,
1420 uid_t uid
, gid_t gid
)
1423 struct run_as_data data
= {};
1424 struct run_as_ret run_as_ret
= {};
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
));
1432 ERR("Failed to copy path argument of mkdirat command");
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
: RUN_AS_MKDIRAT
,
1439 &data
, &run_as_ret
, uid
, gid
);
1440 errno
= run_as_ret
._errno
;
1441 ret
= run_as_ret
.u
.ret
;
1447 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
,
1450 return run_as_openat(AT_FDCWD
, path
, flags
, mode
, uid
, gid
);
1454 int run_as_openat(int dirfd
, const char *path
, int flags
, mode_t mode
,
1455 uid_t uid
, gid_t gid
)
1458 struct run_as_data data
= {};
1459 struct run_as_ret run_as_ret
= {};
1461 DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
1462 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1463 path
, flags
, (int) mode
, (int) uid
, (int) gid
);
1464 ret
= lttng_strncpy(data
.u
.open
.path
, path
, sizeof(data
.u
.open
.path
));
1466 ERR("Failed to copy path argument of open command");
1469 data
.u
.open
.flags
= flags
;
1470 data
.u
.open
.mode
= mode
;
1471 data
.u
.open
.dirfd
= dirfd
;
1472 run_as(dirfd
== AT_FDCWD
? RUN_AS_OPEN
: RUN_AS_OPENAT
,
1473 &data
, &run_as_ret
, uid
, gid
);
1474 errno
= run_as_ret
._errno
;
1475 ret
= run_as_ret
.u
.ret
< 0 ? run_as_ret
.u
.ret
:
1476 run_as_ret
.u
.open
.fd
;
1482 int run_as_unlink(const char *path
, uid_t uid
, gid_t gid
)
1484 return run_as_unlinkat(AT_FDCWD
, path
, uid
, gid
);
1488 int run_as_unlinkat(int dirfd
, const char *path
, uid_t uid
, gid_t gid
)
1491 struct run_as_data data
= {};
1492 struct run_as_ret run_as_ret
= {};
1494 DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
1495 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1496 path
, (int) uid
, (int) gid
);
1497 ret
= lttng_strncpy(data
.u
.unlink
.path
, path
,
1498 sizeof(data
.u
.unlink
.path
));
1502 data
.u
.unlink
.dirfd
= dirfd
;
1503 run_as(dirfd
== AT_FDCWD
? RUN_AS_UNLINK
: RUN_AS_UNLINKAT
, &data
,
1504 &run_as_ret
, uid
, gid
);
1505 errno
= run_as_ret
._errno
;
1506 ret
= run_as_ret
.u
.ret
;
1512 int run_as_rmdir(const char *path
, uid_t uid
, gid_t gid
)
1514 return run_as_rmdirat(AT_FDCWD
, path
, uid
, gid
);
1518 int run_as_rmdirat(int dirfd
, const char *path
, uid_t uid
, gid_t gid
)
1521 struct run_as_data data
= {};
1522 struct run_as_ret run_as_ret
= {};
1524 DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d",
1525 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1526 path
, (int) uid
, (int) gid
);
1527 ret
= lttng_strncpy(data
.u
.rmdir
.path
, path
,
1528 sizeof(data
.u
.rmdir
.path
));
1532 data
.u
.rmdir
.dirfd
= dirfd
;
1533 run_as(dirfd
== AT_FDCWD
? RUN_AS_RMDIR
: RUN_AS_RMDIRAT
, &data
,
1534 &run_as_ret
, uid
, gid
);
1535 errno
= run_as_ret
._errno
;
1536 ret
= run_as_ret
.u
.ret
;
1542 int run_as_rmdir_recursive(const char *path
, uid_t uid
, gid_t gid
, int flags
)
1544 return run_as_rmdirat_recursive(AT_FDCWD
, path
, uid
, gid
, flags
);
1548 int run_as_rmdirat_recursive(int dirfd
, const char *path
, uid_t uid
, gid_t gid
, int flags
)
1551 struct run_as_data data
= {};
1552 struct run_as_ret run_as_ret
= {};
1554 DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d",
1555 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1556 path
, (int) uid
, (int) gid
);
1557 ret
= lttng_strncpy(data
.u
.rmdir
.path
, path
,
1558 sizeof(data
.u
.rmdir
.path
));
1562 data
.u
.rmdir
.dirfd
= dirfd
;
1563 data
.u
.rmdir
.flags
= flags
;
1564 run_as(dirfd
== AT_FDCWD
? RUN_AS_RMDIR_RECURSIVE
: RUN_AS_RMDIRAT_RECURSIVE
,
1565 &data
, &run_as_ret
, uid
, gid
);
1566 errno
= run_as_ret
._errno
;
1567 ret
= run_as_ret
.u
.ret
;
1573 int run_as_rename(const char *old
, const char *new, uid_t uid
, gid_t gid
)
1575 return run_as_renameat(AT_FDCWD
, old
, AT_FDCWD
, new, uid
, gid
);
1579 int run_as_renameat(int old_dirfd
, const char *old_name
,
1580 int new_dirfd
, const char *new_name
, uid_t uid
, gid_t gid
)
1583 struct run_as_data data
= {};
1584 struct run_as_ret run_as_ret
= {};
1586 DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d",
1587 old_dirfd
, old_dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1589 new_dirfd
, new_dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1590 new_name
, (int) uid
, (int) gid
);
1591 ret
= lttng_strncpy(data
.u
.rename
.old_path
, old_name
,
1592 sizeof(data
.u
.rename
.old_path
));
1596 ret
= lttng_strncpy(data
.u
.rename
.new_path
, new_name
,
1597 sizeof(data
.u
.rename
.new_path
));
1602 data
.u
.rename
.dirfds
[0] = old_dirfd
;
1603 data
.u
.rename
.dirfds
[1] = new_dirfd
;
1604 run_as(old_dirfd
== AT_FDCWD
&& new_dirfd
== AT_FDCWD
?
1605 RUN_AS_RENAME
: RUN_AS_RENAMEAT
,
1606 &data
, &run_as_ret
, uid
, gid
);
1607 errno
= run_as_ret
._errno
;
1608 ret
= run_as_ret
.u
.ret
;
1614 int run_as_extract_elf_symbol_offset(int fd
, const char* function
,
1615 uid_t uid
, gid_t gid
, uint64_t *offset
)
1618 struct run_as_data data
= {};
1619 struct run_as_ret run_as_ret
= {};
1621 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
1622 "with for uid %d and gid %d", fd
, function
,
1623 (int) uid
, (int) gid
);
1625 data
.u
.extract_elf_symbol_offset
.fd
= fd
;
1627 strncpy(data
.u
.extract_elf_symbol_offset
.function
, function
, LTTNG_SYMBOL_NAME_LEN
- 1);
1628 data
.u
.extract_elf_symbol_offset
.function
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1629 ret
= lttng_strncpy(data
.u
.extract_elf_symbol_offset
.function
,
1631 sizeof(data
.u
.extract_elf_symbol_offset
.function
));
1636 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
, &data
, &run_as_ret
, uid
, gid
);
1637 errno
= run_as_ret
._errno
;
1638 if (run_as_ret
._error
) {
1643 *offset
= run_as_ret
.u
.extract_elf_symbol_offset
.offset
;
1649 int run_as_extract_sdt_probe_offsets(int fd
, const char* provider_name
,
1650 const char* probe_name
, uid_t uid
, gid_t gid
,
1651 uint64_t **offsets
, uint32_t *num_offset
)
1654 struct run_as_data data
= {};
1655 struct run_as_ret run_as_ret
= {};
1657 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
1658 "provider_name=%s with for uid %d and gid %d", fd
,
1659 probe_name
, provider_name
, (int) uid
, (int) gid
);
1661 data
.u
.extract_sdt_probe_offsets
.fd
= fd
;
1663 ret
= lttng_strncpy(data
.u
.extract_sdt_probe_offsets
.probe_name
, probe_name
,
1664 sizeof(data
.u
.extract_sdt_probe_offsets
.probe_name
));
1668 ret
= lttng_strncpy(data
.u
.extract_sdt_probe_offsets
.provider_name
,
1670 sizeof(data
.u
.extract_sdt_probe_offsets
.provider_name
));
1675 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
, &data
, &run_as_ret
, uid
, gid
);
1676 errno
= run_as_ret
._errno
;
1677 if (run_as_ret
._error
) {
1682 *num_offset
= run_as_ret
.u
.extract_sdt_probe_offsets
.num_offset
;
1683 *offsets
= zmalloc(*num_offset
* sizeof(uint64_t));
1689 memcpy(*offsets
, run_as_ret
.u
.extract_sdt_probe_offsets
.offsets
,
1690 *num_offset
* sizeof(uint64_t));
1696 int run_as_create_worker(const char *procname
,
1697 post_fork_cleanup_cb clean_up_func
,
1698 void *clean_up_user_data
)
1702 pthread_mutex_lock(&worker_lock
);
1703 ret
= run_as_create_worker_no_lock(procname
, clean_up_func
,
1704 clean_up_user_data
);
1705 pthread_mutex_unlock(&worker_lock
);
1710 void run_as_destroy_worker(void)
1712 pthread_mutex_lock(&worker_lock
);
1713 run_as_destroy_worker_no_lock();
1714 pthread_mutex_unlock(&worker_lock
);