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
)
517 ret_value
->_error
= false;
518 ret
= lttng_elf_get_symbol_offset(data
->u
.extract_elf_symbol_offset
.fd
,
519 data
->u
.extract_elf_symbol_offset
.function
,
522 DBG("Failed to extract ELF function offset");
523 ret_value
->_error
= true;
525 ret_value
->u
.extract_elf_symbol_offset
.offset
= offset
;
531 int _extract_sdt_probe_offsets(struct run_as_data
*data
,
532 struct run_as_ret
*ret_value
)
535 uint64_t *offsets
= NULL
;
538 ret_value
->_error
= false;
540 /* On success, this call allocates the offsets paramater. */
541 ret
= lttng_elf_get_sdt_probe_offsets(
542 data
->u
.extract_sdt_probe_offsets
.fd
,
543 data
->u
.extract_sdt_probe_offsets
.provider_name
,
544 data
->u
.extract_sdt_probe_offsets
.probe_name
,
545 &offsets
, &num_offset
);
548 DBG("Failed to extract SDT probe offsets");
549 ret_value
->_error
= true;
553 if (num_offset
<= 0 || num_offset
> LTTNG_KERNEL_MAX_UPROBE_NUM
) {
554 DBG("Wrong number of probes.");
556 ret_value
->_error
= true;
560 /* Copy the content of the offsets array to the ret struct. */
561 memcpy(ret_value
->u
.extract_sdt_probe_offsets
.offsets
,
562 offsets
, num_offset
* sizeof(uint64_t));
564 ret_value
->u
.extract_sdt_probe_offsets
.num_offset
= num_offset
;
573 int _extract_elf_symbol_offset(struct run_as_data
*data
,
574 struct run_as_ret
*ret_value
)
576 ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET");
581 int _extract_sdt_probe_offsets(struct run_as_data
*data
,
582 struct run_as_ret
*ret_value
)
584 ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS");
590 run_as_fct
run_as_enum_to_fct(enum run_as_cmd cmd
)
596 case RUN_AS_MKDIR_RECURSIVE
:
597 case RUN_AS_MKDIRAT_RECURSIVE
:
598 return _mkdirat_recursive
;
603 case RUN_AS_UNLINKAT
:
608 case RUN_AS_RMDIR_RECURSIVE
:
609 case RUN_AS_RMDIRAT_RECURSIVE
:
610 return _rmdir_recursive
;
612 case RUN_AS_RENAMEAT
:
614 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
615 return _extract_elf_symbol_offset
;
616 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
617 return _extract_sdt_probe_offsets
;
619 ERR("Unknown command %d", (int) cmd
);
625 int do_send_fds(int sock
, const int *fds
, unsigned int fd_count
)
630 for (i
= 0; i
< fd_count
; i
++) {
632 ERR("Attempt to send invalid file descriptor to master (fd = %i)",
634 /* Return 0 as this is not a fatal error. */
639 len
= lttcomm_send_fds_unix_sock(sock
, fds
, fd_count
);
640 return len
< 0 ? -1 : 0;
644 int do_recv_fds(int sock
, int *fds
, unsigned int fd_count
)
650 len
= lttcomm_recv_fds_unix_sock(sock
, fds
, fd_count
);
654 } else if (len
< 0) {
655 PERROR("Failed to receive file descriptors from socket");
660 for (i
= 0; i
< fd_count
; i
++) {
662 ERR("Invalid file descriptor received from worker (fd = %i)", fds
[i
]);
663 /* Return 0 as this is not a fatal error. */
671 int send_fds_to_worker(const struct run_as_worker
*worker
,
672 const struct run_as_data
*data
)
677 if (COMMAND_USE_CWD_FD(data
) || COMMAND_IN_FD_COUNT(data
) == 0) {
681 for (i
= 0; i
< COMMAND_IN_FD_COUNT(data
); i
++) {
682 if (COMMAND_IN_FDS(data
)[i
] < 0) {
683 ERR("Refusing to send invalid fd to worker (fd = %i)",
684 COMMAND_IN_FDS(data
)[i
]);
690 ret
= do_send_fds(worker
->sockpair
[0], COMMAND_IN_FDS(data
),
691 COMMAND_IN_FD_COUNT(data
));
693 PERROR("Failed to send file descriptor to run-as worker");
702 int send_fds_to_master(struct run_as_worker
*worker
, enum run_as_cmd cmd
,
703 struct run_as_ret
*run_as_ret
)
708 if (COMMAND_OUT_FD_COUNT(cmd
) == 0) {
712 ret
= do_send_fds(worker
->sockpair
[1], COMMAND_OUT_FDS(cmd
, run_as_ret
),
713 COMMAND_OUT_FD_COUNT(cmd
));
715 PERROR("Failed to send file descriptor to master process");
719 for (i
= 0; i
< COMMAND_OUT_FD_COUNT(cmd
); i
++) {
720 int ret_close
= close(COMMAND_OUT_FDS(cmd
, run_as_ret
)[i
]);
723 PERROR("Failed to close result file descriptor");
731 int recv_fds_from_worker(const struct run_as_worker
*worker
, enum run_as_cmd cmd
,
732 struct run_as_ret
*run_as_ret
)
736 if (COMMAND_OUT_FD_COUNT(cmd
) == 0) {
740 ret
= do_recv_fds(worker
->sockpair
[0], COMMAND_OUT_FDS(cmd
, run_as_ret
),
741 COMMAND_OUT_FD_COUNT(cmd
));
743 PERROR("Failed to receive file descriptor from run-as worker");
751 int recv_fds_from_master(struct run_as_worker
*worker
, struct run_as_data
*data
)
755 if (COMMAND_USE_CWD_FD(data
)) {
758 for (i
= 0; i
< COMMAND_IN_FD_COUNT(data
); i
++) {
759 COMMAND_IN_FDS(data
)[i
] = AT_FDCWD
;
764 ret
= do_recv_fds(worker
->sockpair
[1], COMMAND_IN_FDS(data
),
765 COMMAND_IN_FD_COUNT(data
));
767 PERROR("Failed to receive file descriptors from master process");
775 int cleanup_received_fds(struct run_as_data
*data
)
779 for (i
= 0; i
< COMMAND_IN_FD_COUNT(data
); i
++) {
780 if (COMMAND_IN_FDS(data
)[i
] == -1) {
783 ret
= close(COMMAND_IN_FDS(data
)[i
]);
785 PERROR("Failed to close file descriptor received fd in run-as worker");
794 * Return < 0 on error, 0 if OK, 1 on hangup.
797 int handle_one_cmd(struct run_as_worker
*worker
)
800 struct run_as_data data
= {};
801 ssize_t readlen
, writelen
;
802 struct run_as_ret sendret
= {};
807 * Stage 1: Receive run_as_data struct from the master.
808 * The structure contains the command type and all the parameters needed for
811 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[1], &data
,
818 if (readlen
< sizeof(data
)) {
819 PERROR("lttcomm_recv_unix_sock error");
824 cmd
= run_as_enum_to_fct(data
.cmd
);
831 * Stage 2: Receive file descriptor from master.
832 * Some commands need a file descriptor as input so if it's needed we
833 * receive the fd using the Unix socket.
835 ret
= recv_fds_from_master(worker
, &data
);
837 PERROR("recv_fd_from_master error");
842 prev_euid
= getuid();
843 if (data
.gid
!= getegid()) {
844 ret
= setegid(data
.gid
);
846 sendret
._error
= true;
847 sendret
._errno
= errno
;
852 if (data
.uid
!= prev_euid
) {
853 ret
= seteuid(data
.uid
);
855 sendret
._error
= true;
856 sendret
._errno
= errno
;
863 * Also set umask to 0 for mkdir executable bit.
868 * Stage 3: Execute the command
870 ret
= (*cmd
)(&data
, &sendret
);
872 DBG("Execution of command returned an error");
876 ret
= cleanup_received_fds(&data
);
878 ERR("Error cleaning up FD");
883 * Stage 4: Send run_as_ret structure to the master.
884 * This structure contain the return value of the command and the errno.
886 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
888 if (writelen
< sizeof(sendret
)) {
889 PERROR("lttcomm_send_unix_sock error");
895 * Stage 5: Send resulting file descriptors to the master.
897 ret
= send_fds_to_master(worker
, data
.cmd
, &sendret
);
899 DBG("Sending FD to master returned an error");
903 if (seteuid(prev_euid
) < 0) {
914 int run_as_worker(struct run_as_worker
*worker
)
918 struct run_as_ret sendret
;
919 size_t proc_orig_len
;
922 * Initialize worker. Set a different process cmdline.
924 proc_orig_len
= strlen(worker
->procname
);
925 memset(worker
->procname
, 0, proc_orig_len
);
926 strncpy(worker
->procname
, DEFAULT_RUN_AS_WORKER_NAME
, proc_orig_len
);
928 ret
= lttng_prctl(PR_SET_NAME
,
929 (unsigned long) DEFAULT_RUN_AS_WORKER_NAME
, 0, 0, 0);
930 if (ret
&& ret
!= -ENOSYS
) {
931 /* Don't fail as this is not essential. */
932 PERROR("prctl PR_SET_NAME");
935 memset(&sendret
, 0, sizeof(sendret
));
937 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
939 if (writelen
< sizeof(sendret
)) {
940 PERROR("lttcomm_send_unix_sock error");
946 ret
= handle_one_cmd(worker
);
950 } else if (ret
> 0) {
953 continue; /* Next command. */
962 int run_as_cmd(struct run_as_worker
*worker
,
964 struct run_as_data
*data
,
965 struct run_as_ret
*ret_value
,
966 uid_t uid
, gid_t gid
)
969 ssize_t readlen
, writelen
;
972 * If we are non-root, we can only deal with our own uid.
974 if (geteuid() != 0) {
975 if (uid
!= geteuid()) {
977 ret_value
->_errno
= EPERM
;
978 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
979 (int) uid
, (int) geteuid());
989 * Stage 1: Send the run_as_data struct to the worker process
991 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[0], data
,
993 if (writelen
< sizeof(*data
)) {
994 PERROR("Error writing message to run_as");
996 ret_value
->_errno
= EIO
;
1001 * Stage 2: Send file descriptor to the worker process if needed
1003 ret
= send_fds_to_worker(worker
, data
);
1005 PERROR("do_send_fd error");
1007 ret_value
->_errno
= EIO
;
1012 * Stage 3: Wait for the execution of the command
1016 * Stage 4: Receive the run_as_ret struct containing the return value and
1019 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0], ret_value
,
1020 sizeof(*ret_value
));
1022 ERR("Run-as worker has hung-up during run_as_cmd");
1024 ret_value
->_errno
= EIO
;
1026 } else if (readlen
< sizeof(*ret_value
)) {
1027 PERROR("Error reading response from run_as");
1029 ret_value
->_errno
= errno
;
1033 if (ret_value
->_error
) {
1034 /* Skip stage 5 on error as there will be no fd to receive. */
1039 * Stage 5: Receive file descriptor if needed
1041 ret
= recv_fds_from_worker(worker
, cmd
, ret_value
);
1043 ERR("Error receiving fd");
1045 ret_value
->_errno
= EIO
;
1053 * This is for debugging ONLY and should not be considered secure.
1056 int run_as_noworker(enum run_as_cmd cmd
,
1057 struct run_as_data
*data
, struct run_as_ret
*ret_value
,
1058 uid_t uid
, gid_t gid
)
1060 int ret
, saved_errno
;
1064 fct
= run_as_enum_to_fct(cmd
);
1070 old_mask
= umask(0);
1071 ret
= fct(data
, ret_value
);
1072 saved_errno
= ret_value
->_errno
;
1074 errno
= saved_errno
;
1080 int reset_sighandler(void)
1084 DBG("Resetting run_as worker signal handlers to default");
1085 for (sig
= 1; sig
<= 31; sig
++) {
1086 (void) signal(sig
, SIG_DFL
);
1092 void worker_sighandler(int sig
)
1094 const char *signame
;
1097 * The worker will inherit its parent's signals since they are part of
1098 * the same process group. However, in the case of SIGINT and SIGTERM,
1099 * we want to give the worker a chance to teardown gracefully when its
1100 * parent closes the command socket.
1107 signame
= "SIGTERM";
1114 DBG("run_as worker received signal %s", signame
);
1116 DBG("run_as_worker received signal %d", sig
);
1121 int set_worker_sighandlers(void)
1125 struct sigaction sa
;
1127 if ((ret
= sigemptyset(&sigset
)) < 0) {
1128 PERROR("sigemptyset");
1132 sa
.sa_handler
= worker_sighandler
;
1133 sa
.sa_mask
= sigset
;
1135 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1136 PERROR("sigaction SIGINT");
1140 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1141 PERROR("sigaction SIGTERM");
1145 DBG("run_as signal handler set for SIGTERM and SIGINT");
1151 int run_as_create_worker_no_lock(const char *procname
,
1152 post_fork_cleanup_cb clean_up_func
,
1153 void *clean_up_user_data
)
1158 struct run_as_ret recvret
;
1159 struct run_as_worker
*worker
;
1161 assert(!global_worker
);
1164 * Don't initialize a worker, all run_as tasks will be performed
1165 * in the current process.
1170 worker
= zmalloc(sizeof(*worker
));
1175 worker
->procname
= strdup(procname
);
1176 if (!worker
->procname
) {
1178 goto error_procname_alloc
;
1180 /* Create unix socket. */
1181 if (lttcomm_create_anon_unix_socketpair(worker
->sockpair
) < 0) {
1192 } else if (pid
== 0) {
1197 set_worker_sighandlers();
1198 if (clean_up_func
) {
1199 if (clean_up_func(clean_up_user_data
) < 0) {
1200 ERR("Run-as post-fork clean-up failed, exiting.");
1205 /* Just close, no shutdown. */
1206 if (close(worker
->sockpair
[0])) {
1212 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
1213 * Sockpair[1] is used as a control channel with the master
1215 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1216 if (i
!= worker
->sockpair
[1]) {
1221 worker
->sockpair
[0] = -1;
1222 ret
= run_as_worker(worker
);
1223 if (lttcomm_close_unix_sock(worker
->sockpair
[1])) {
1227 worker
->sockpair
[1] = -1;
1228 free(worker
->procname
);
1230 LOG(ret
? PRINT_ERR
: PRINT_DBG
, "run_as worker exiting (ret = %d)", ret
);
1231 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
1235 /* Just close, no shutdown. */
1236 if (close(worker
->sockpair
[1])) {
1241 worker
->sockpair
[1] = -1;
1243 /* Wait for worker to become ready. */
1244 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0],
1245 &recvret
, sizeof(recvret
));
1246 if (readlen
< sizeof(recvret
)) {
1247 ERR("readlen: %zd", readlen
);
1248 PERROR("Error reading response from run_as at creation");
1252 global_worker
= worker
;
1257 /* Error handling. */
1259 for (i
= 0; i
< 2; i
++) {
1260 if (worker
->sockpair
[i
] < 0) {
1263 if (lttcomm_close_unix_sock(worker
->sockpair
[i
])) {
1266 worker
->sockpair
[i
] = -1;
1269 free(worker
->procname
);
1270 error_procname_alloc
:
1276 void run_as_destroy_worker_no_lock(void)
1278 struct run_as_worker
*worker
= global_worker
;
1280 DBG("Destroying run_as worker");
1284 /* Close unix socket */
1285 DBG("Closing run_as worker socket");
1286 if (lttcomm_close_unix_sock(worker
->sockpair
[0])) {
1289 worker
->sockpair
[0] = -1;
1290 /* Wait for worker. */
1295 wait_ret
= waitpid(worker
->pid
, &status
, 0);
1297 if (errno
== EINTR
) {
1304 if (WIFEXITED(status
)) {
1305 LOG(WEXITSTATUS(status
) == 0 ? PRINT_DBG
: PRINT_ERR
,
1306 DEFAULT_RUN_AS_WORKER_NAME
" terminated with status code %d",
1307 WEXITSTATUS(status
));
1309 } else if (WIFSIGNALED(status
)) {
1310 ERR(DEFAULT_RUN_AS_WORKER_NAME
" was killed by signal %d",
1315 free(worker
->procname
);
1317 global_worker
= NULL
;
1321 int run_as_restart_worker(struct run_as_worker
*worker
)
1324 char *procname
= NULL
;
1326 procname
= worker
->procname
;
1328 /* Close socket to run_as worker process and clean up the zombie process */
1329 run_as_destroy_worker_no_lock();
1331 /* Create a new run_as worker process*/
1332 ret
= run_as_create_worker_no_lock(procname
, NULL
, NULL
);
1334 ERR("Restarting the worker process failed");
1343 int run_as(enum run_as_cmd cmd
, struct run_as_data
*data
,
1344 struct run_as_ret
*ret_value
, uid_t uid
, gid_t gid
)
1346 int ret
, saved_errno
;
1348 pthread_mutex_lock(&worker_lock
);
1350 DBG("Using run_as worker");
1352 assert(global_worker
);
1354 ret
= run_as_cmd(global_worker
, cmd
, data
, ret_value
, uid
, gid
);
1355 saved_errno
= ret_value
->_errno
;
1358 * If the worker thread crashed the errno is set to EIO. we log
1359 * the error and start a new worker process.
1361 if (ret
== -1 && saved_errno
== EIO
) {
1362 DBG("Socket closed unexpectedly... "
1363 "Restarting the worker process");
1364 ret
= run_as_restart_worker(global_worker
);
1367 ERR("Failed to restart worker process.");
1372 DBG("Using run_as without worker");
1373 ret
= run_as_noworker(cmd
, data
, ret_value
, uid
, gid
);
1376 pthread_mutex_unlock(&worker_lock
);
1381 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1383 return run_as_mkdirat_recursive(AT_FDCWD
, path
, mode
, uid
, gid
);
1387 int run_as_mkdirat_recursive(int dirfd
, const char *path
, mode_t mode
,
1388 uid_t uid
, gid_t gid
)
1391 struct run_as_data data
= {};
1392 struct run_as_ret run_as_ret
= {};
1394 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1395 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1396 path
, (int) mode
, (int) uid
, (int) gid
);
1397 ret
= lttng_strncpy(data
.u
.mkdir
.path
, path
,
1398 sizeof(data
.u
.mkdir
.path
));
1400 ERR("Failed to copy path argument of mkdirat recursive command");
1403 data
.u
.mkdir
.path
[sizeof(data
.u
.mkdir
.path
) - 1] = '\0';
1404 data
.u
.mkdir
.mode
= mode
;
1405 data
.u
.mkdir
.dirfd
= dirfd
;
1406 run_as(dirfd
== AT_FDCWD
? RUN_AS_MKDIR_RECURSIVE
: RUN_AS_MKDIRAT_RECURSIVE
,
1407 &data
, &run_as_ret
, uid
, gid
);
1408 errno
= run_as_ret
._errno
;
1409 ret
= run_as_ret
.u
.ret
;
1415 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1417 return run_as_mkdirat(AT_FDCWD
, path
, mode
, uid
, gid
);
1421 int run_as_mkdirat(int dirfd
, const char *path
, mode_t mode
,
1422 uid_t uid
, gid_t gid
)
1425 struct run_as_data data
= {};
1426 struct run_as_ret run_as_ret
= {};
1428 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1429 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1430 path
, (int) mode
, (int) uid
, (int) gid
);
1431 ret
= lttng_strncpy(data
.u
.mkdir
.path
, path
,
1432 sizeof(data
.u
.mkdir
.path
));
1434 ERR("Failed to copy path argument of mkdirat command");
1437 data
.u
.mkdir
.path
[sizeof(data
.u
.mkdir
.path
) - 1] = '\0';
1438 data
.u
.mkdir
.mode
= mode
;
1439 data
.u
.mkdir
.dirfd
= dirfd
;
1440 run_as(dirfd
== AT_FDCWD
? RUN_AS_MKDIR
: RUN_AS_MKDIRAT
,
1441 &data
, &run_as_ret
, uid
, gid
);
1442 errno
= run_as_ret
._errno
;
1443 ret
= run_as_ret
.u
.ret
;
1449 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
,
1452 return run_as_openat(AT_FDCWD
, path
, flags
, mode
, uid
, gid
);
1456 int run_as_openat(int dirfd
, const char *path
, int flags
, mode_t mode
,
1457 uid_t uid
, gid_t gid
)
1460 struct run_as_data data
= {};
1461 struct run_as_ret run_as_ret
= {};
1463 DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
1464 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1465 path
, flags
, (int) mode
, (int) uid
, (int) gid
);
1466 ret
= lttng_strncpy(data
.u
.open
.path
, path
, sizeof(data
.u
.open
.path
));
1468 ERR("Failed to copy path argument of open command");
1471 data
.u
.open
.flags
= flags
;
1472 data
.u
.open
.mode
= mode
;
1473 data
.u
.open
.dirfd
= dirfd
;
1474 run_as(dirfd
== AT_FDCWD
? RUN_AS_OPEN
: RUN_AS_OPENAT
,
1475 &data
, &run_as_ret
, uid
, gid
);
1476 errno
= run_as_ret
._errno
;
1477 ret
= run_as_ret
.u
.ret
< 0 ? run_as_ret
.u
.ret
:
1478 run_as_ret
.u
.open
.fd
;
1484 int run_as_unlink(const char *path
, uid_t uid
, gid_t gid
)
1486 return run_as_unlinkat(AT_FDCWD
, path
, uid
, gid
);
1490 int run_as_unlinkat(int dirfd
, const char *path
, uid_t uid
, gid_t gid
)
1493 struct run_as_data data
= {};
1494 struct run_as_ret run_as_ret
= {};
1496 DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
1497 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1498 path
, (int) uid
, (int) gid
);
1499 ret
= lttng_strncpy(data
.u
.unlink
.path
, path
,
1500 sizeof(data
.u
.unlink
.path
));
1504 data
.u
.unlink
.dirfd
= dirfd
;
1505 run_as(dirfd
== AT_FDCWD
? RUN_AS_UNLINK
: RUN_AS_UNLINKAT
, &data
,
1506 &run_as_ret
, uid
, gid
);
1507 errno
= run_as_ret
._errno
;
1508 ret
= run_as_ret
.u
.ret
;
1514 int run_as_rmdir(const char *path
, uid_t uid
, gid_t gid
)
1516 return run_as_rmdirat(AT_FDCWD
, path
, uid
, gid
);
1520 int run_as_rmdirat(int dirfd
, const char *path
, uid_t uid
, gid_t gid
)
1523 struct run_as_data data
= {};
1524 struct run_as_ret run_as_ret
= {};
1526 DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d",
1527 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1528 path
, (int) uid
, (int) gid
);
1529 ret
= lttng_strncpy(data
.u
.rmdir
.path
, path
,
1530 sizeof(data
.u
.rmdir
.path
));
1534 data
.u
.rmdir
.dirfd
= dirfd
;
1535 run_as(dirfd
== AT_FDCWD
? RUN_AS_RMDIR
: RUN_AS_RMDIRAT
, &data
,
1536 &run_as_ret
, uid
, gid
);
1537 errno
= run_as_ret
._errno
;
1538 ret
= run_as_ret
.u
.ret
;
1544 int run_as_rmdir_recursive(const char *path
, uid_t uid
, gid_t gid
, int flags
)
1546 return run_as_rmdirat_recursive(AT_FDCWD
, path
, uid
, gid
, flags
);
1550 int run_as_rmdirat_recursive(int dirfd
, const char *path
, uid_t uid
, gid_t gid
, int flags
)
1553 struct run_as_data data
= {};
1554 struct run_as_ret run_as_ret
= {};
1556 DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d",
1557 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1558 path
, (int) uid
, (int) gid
);
1559 ret
= lttng_strncpy(data
.u
.rmdir
.path
, path
,
1560 sizeof(data
.u
.rmdir
.path
));
1564 data
.u
.rmdir
.dirfd
= dirfd
;
1565 data
.u
.rmdir
.flags
= flags
;
1566 run_as(dirfd
== AT_FDCWD
? RUN_AS_RMDIR_RECURSIVE
: RUN_AS_RMDIRAT_RECURSIVE
,
1567 &data
, &run_as_ret
, uid
, gid
);
1568 errno
= run_as_ret
._errno
;
1569 ret
= run_as_ret
.u
.ret
;
1575 int run_as_rename(const char *old
, const char *new, uid_t uid
, gid_t gid
)
1577 return run_as_renameat(AT_FDCWD
, old
, AT_FDCWD
, new, uid
, gid
);
1581 int run_as_renameat(int old_dirfd
, const char *old_name
,
1582 int new_dirfd
, const char *new_name
, uid_t uid
, gid_t gid
)
1585 struct run_as_data data
= {};
1586 struct run_as_ret run_as_ret
= {};
1588 DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d",
1589 old_dirfd
, old_dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1591 new_dirfd
, new_dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1592 new_name
, (int) uid
, (int) gid
);
1593 ret
= lttng_strncpy(data
.u
.rename
.old_path
, old_name
,
1594 sizeof(data
.u
.rename
.old_path
));
1598 ret
= lttng_strncpy(data
.u
.rename
.new_path
, new_name
,
1599 sizeof(data
.u
.rename
.new_path
));
1604 data
.u
.rename
.dirfds
[0] = old_dirfd
;
1605 data
.u
.rename
.dirfds
[1] = new_dirfd
;
1606 run_as(old_dirfd
== AT_FDCWD
&& new_dirfd
== AT_FDCWD
?
1607 RUN_AS_RENAME
: RUN_AS_RENAMEAT
,
1608 &data
, &run_as_ret
, uid
, gid
);
1609 errno
= run_as_ret
._errno
;
1610 ret
= run_as_ret
.u
.ret
;
1616 int run_as_extract_elf_symbol_offset(int fd
, const char* function
,
1617 uid_t uid
, gid_t gid
, uint64_t *offset
)
1620 struct run_as_data data
= {};
1621 struct run_as_ret run_as_ret
= {};
1623 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
1624 "with for uid %d and gid %d", fd
, function
,
1625 (int) uid
, (int) gid
);
1627 data
.u
.extract_elf_symbol_offset
.fd
= fd
;
1629 strncpy(data
.u
.extract_elf_symbol_offset
.function
, function
, LTTNG_SYMBOL_NAME_LEN
- 1);
1630 data
.u
.extract_elf_symbol_offset
.function
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1631 ret
= lttng_strncpy(data
.u
.extract_elf_symbol_offset
.function
,
1633 sizeof(data
.u
.extract_elf_symbol_offset
.function
));
1638 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
, &data
, &run_as_ret
, uid
, gid
);
1639 errno
= run_as_ret
._errno
;
1640 if (run_as_ret
._error
) {
1645 *offset
= run_as_ret
.u
.extract_elf_symbol_offset
.offset
;
1651 int run_as_extract_sdt_probe_offsets(int fd
, const char* provider_name
,
1652 const char* probe_name
, uid_t uid
, gid_t gid
,
1653 uint64_t **offsets
, uint32_t *num_offset
)
1656 struct run_as_data data
= {};
1657 struct run_as_ret run_as_ret
= {};
1659 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
1660 "provider_name=%s with for uid %d and gid %d", fd
,
1661 probe_name
, provider_name
, (int) uid
, (int) gid
);
1663 data
.u
.extract_sdt_probe_offsets
.fd
= fd
;
1665 ret
= lttng_strncpy(data
.u
.extract_sdt_probe_offsets
.probe_name
, probe_name
,
1666 sizeof(data
.u
.extract_sdt_probe_offsets
.probe_name
));
1670 ret
= lttng_strncpy(data
.u
.extract_sdt_probe_offsets
.provider_name
,
1672 sizeof(data
.u
.extract_sdt_probe_offsets
.provider_name
));
1677 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
, &data
, &run_as_ret
, uid
, gid
);
1678 errno
= run_as_ret
._errno
;
1679 if (run_as_ret
._error
) {
1684 *num_offset
= run_as_ret
.u
.extract_sdt_probe_offsets
.num_offset
;
1685 *offsets
= zmalloc(*num_offset
* sizeof(uint64_t));
1691 memcpy(*offsets
, run_as_ret
.u
.extract_sdt_probe_offsets
.offsets
,
1692 *num_offset
* sizeof(uint64_t));
1698 int run_as_create_worker(const char *procname
,
1699 post_fork_cleanup_cb clean_up_func
,
1700 void *clean_up_user_data
)
1704 pthread_mutex_lock(&worker_lock
);
1705 ret
= run_as_create_worker_no_lock(procname
, clean_up_func
,
1706 clean_up_user_data
);
1707 pthread_mutex_unlock(&worker_lock
);
1712 void run_as_destroy_worker(void)
1714 pthread_mutex_lock(&worker_lock
);
1715 run_as_destroy_worker_no_lock();
1716 pthread_mutex_unlock(&worker_lock
);