2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: LGPL-2.1-only
21 #include <sys/types.h>
25 #include <common/bytecode/bytecode.hpp>
26 #include <common/lttng-kernel.hpp>
27 #include <common/common.hpp>
28 #include <common/utils.hpp>
29 #include <common/compat/errno.hpp>
30 #include <common/compat/getenv.hpp>
31 #include <common/compat/string.hpp>
32 #include <common/unix.hpp>
33 #include <common/defaults.hpp>
34 #include <common/lttng-elf.hpp>
35 #include <common/thread.hpp>
37 #include <lttng/constant.h>
39 #include <common/sessiond-comm/sessiond-comm.hpp>
40 #include <common/filter/filter-ast.hpp>
44 #define GETPW_BUFFER_FALLBACK_SIZE 4096
49 RUN_AS_MKDIR_RECURSIVE
,
50 RUN_AS_MKDIRAT_RECURSIVE
,
57 RUN_AS_RMDIR_RECURSIVE
,
58 RUN_AS_RMDIRAT_RECURSIVE
,
61 RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
,
62 RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
,
63 RUN_AS_GENERATE_FILTER_BYTECODE
,
69 typedef int (*run_as_fct
)(struct run_as_data
*data
, struct run_as_ret
*ret_value
);
71 struct run_as_mkdir_data
{
73 char path
[LTTNG_PATH_MAX
];
77 struct run_as_open_data
{
79 char path
[LTTNG_PATH_MAX
];
84 struct run_as_unlink_data
{
86 char path
[LTTNG_PATH_MAX
];
89 struct run_as_rmdir_data
{
91 char path
[LTTNG_PATH_MAX
];
92 int flags
; /* enum lttng_directory_handle_rmdir_recursive_flags. */
95 struct run_as_extract_elf_symbol_offset_data
{
97 char function
[LTTNG_SYMBOL_NAME_LEN
];
100 struct run_as_extract_sdt_probe_offsets_data
{
102 char probe_name
[LTTNG_SYMBOL_NAME_LEN
];
103 char provider_name
[LTTNG_SYMBOL_NAME_LEN
];
106 struct run_as_generate_filter_bytecode_data
{
107 char filter_expression
[LTTNG_FILTER_MAX_LEN
];
110 struct run_as_rename_data
{
116 char old_path
[LTTNG_PATH_MAX
];
117 char new_path
[LTTNG_PATH_MAX
];
120 struct run_as_open_ret
{
124 struct run_as_extract_elf_symbol_offset_ret
{
128 struct run_as_extract_sdt_probe_offsets_ret
{
130 uint64_t offsets
[LTTNG_KERNEL_ABI_MAX_UPROBE_NUM
];
133 struct run_as_generate_filter_bytecode_ret
{
134 /* A lttng_bytecode_filter struct with 'dynamic' payload. */
135 char bytecode
[LTTNG_FILTER_MAX_LEN
];
141 struct run_as_mkdir_data mkdir
;
142 struct run_as_open_data open
;
143 struct run_as_unlink_data unlink
;
144 struct run_as_rmdir_data rmdir
;
145 struct run_as_rename_data rename
;
146 struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset
;
147 struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets
;
148 struct run_as_generate_filter_bytecode_data generate_filter_bytecode
;
155 * The run_as_ret structure holds the returned value and status of the command.
157 * The `u` union field holds the return value of the command; in most cases it
158 * represents the success or the failure of the command. In more complex
159 * commands, it holds a computed value.
161 * The _errno field is the errno recorded after the execution of the command.
163 * The _error fields is used the signify that return status of the command. For
164 * simple commands returning `int` the _error field will be the same as the
165 * ret_int field. In complex commands, it signify the success or failure of the
172 struct run_as_open_ret open
;
173 struct run_as_extract_elf_symbol_offset_ret extract_elf_symbol_offset
;
174 struct run_as_extract_sdt_probe_offsets_ret extract_sdt_probe_offsets
;
175 struct run_as_generate_filter_bytecode_ret generate_filter_bytecode
;
181 #define COMMAND_IN_FDS(data_ptr) ({ \
183 if (command_properties[data_ptr->cmd].in_fds_offset != -1) { \
184 fds = (int *) ((char *) data_ptr + command_properties[data_ptr->cmd].in_fds_offset); \
189 #define COMMAND_OUT_FDS(cmd, ret_ptr) ({ \
191 if (command_properties[cmd].out_fds_offset != -1) { \
192 fds = (int *) ((char *) ret_ptr + command_properties[cmd].out_fds_offset); \
197 #define COMMAND_IN_FD_COUNT(data_ptr) ({ \
198 command_properties[data_ptr->cmd].in_fd_count; \
201 #define COMMAND_OUT_FD_COUNT(cmd) ({ \
202 command_properties[cmd].out_fd_count; \
205 #define COMMAND_USE_CWD_FD(data_ptr) command_properties[data_ptr->cmd].use_cwd_fd
207 struct run_as_command_properties
{
208 /* Set to -1 when not applicable. */
209 ptrdiff_t in_fds_offset
, out_fds_offset
;
210 unsigned int in_fd_count
, out_fd_count
;
214 const struct run_as_command_properties command_properties
[] = {
216 .in_fds_offset
= offsetof(struct run_as_data
, u
.mkdir
.dirfd
),
217 .out_fds_offset
= -1,
223 .in_fds_offset
= offsetof(struct run_as_data
, u
.mkdir
.dirfd
),
224 .out_fds_offset
= -1,
230 .in_fds_offset
= offsetof(struct run_as_data
, u
.mkdir
.dirfd
),
231 .out_fds_offset
= -1,
237 .in_fds_offset
= offsetof(struct run_as_data
, u
.mkdir
.dirfd
),
238 .out_fds_offset
= -1,
244 .in_fds_offset
= offsetof(struct run_as_data
, u
.open
.dirfd
),
245 .out_fds_offset
= offsetof(struct run_as_ret
, u
.open
.fd
),
251 .in_fds_offset
= offsetof(struct run_as_data
, u
.open
.dirfd
),
252 .out_fds_offset
= offsetof(struct run_as_ret
, u
.open
.fd
),
258 .in_fds_offset
= offsetof(struct run_as_data
, u
.unlink
.dirfd
),
259 .out_fds_offset
= -1,
265 .in_fds_offset
= offsetof(struct run_as_data
, u
.unlink
.dirfd
),
266 .out_fds_offset
= -1,
272 .in_fds_offset
= offsetof(struct run_as_data
, u
.rmdir
.dirfd
),
273 .out_fds_offset
= -1,
279 .in_fds_offset
= offsetof(struct run_as_data
, u
.rmdir
.dirfd
),
280 .out_fds_offset
= -1,
286 .in_fds_offset
= offsetof(struct run_as_data
, u
.rmdir
.dirfd
),
287 .out_fds_offset
= -1,
293 .in_fds_offset
= offsetof(struct run_as_data
, u
.rmdir
.dirfd
),
294 .out_fds_offset
= -1,
300 .in_fds_offset
= offsetof(struct run_as_data
, u
.rename
.dirfds
),
301 .out_fds_offset
= -1,
307 .in_fds_offset
= offsetof(struct run_as_data
, u
.rename
.dirfds
),
308 .out_fds_offset
= -1,
314 .in_fds_offset
= offsetof(struct run_as_data
,
315 u
.extract_elf_symbol_offset
.fd
),
316 .out_fds_offset
= -1,
322 .in_fds_offset
= offsetof(struct run_as_data
,
323 u
.extract_sdt_probe_offsets
.fd
),
324 .out_fds_offset
= -1,
331 .out_fds_offset
= -1,
338 struct run_as_worker_data
{
339 pid_t pid
; /* Worker PID. */
344 /* Single global worker per process (for now). */
345 run_as_worker_data
*global_worker
;
346 /* Lock protecting the worker. */
347 pthread_mutex_t worker_lock
= PTHREAD_MUTEX_INITIALIZER
;
360 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
365 * Create recursively directory using the FULL path.
368 int _mkdirat_recursive(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
372 struct lttng_directory_handle
*handle
;
374 path
= data
->u
.mkdir
.path
;
375 mode
= data
->u
.mkdir
.mode
;
377 handle
= lttng_directory_handle_create_from_dirfd(data
->u
.mkdir
.dirfd
);
379 ret_value
->_errno
= errno
;
380 ret_value
->_error
= true;
381 ret_value
->u
.ret
= -1;
384 /* Ownership of dirfd is transferred to the handle. */
385 data
->u
.mkdir
.dirfd
= -1;
386 /* Safe to call as we have transitioned to the requested uid/gid. */
387 ret_value
->u
.ret
= lttng_directory_handle_create_subdirectory_recursive(
389 ret_value
->_errno
= errno
;
390 ret_value
->_error
= (ret_value
->u
.ret
) ? true : false;
391 lttng_directory_handle_put(handle
);
393 return ret_value
->u
.ret
;
397 int _mkdirat(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
401 struct lttng_directory_handle
*handle
;
403 path
= data
->u
.mkdir
.path
;
404 mode
= data
->u
.mkdir
.mode
;
406 handle
= lttng_directory_handle_create_from_dirfd(data
->u
.mkdir
.dirfd
);
408 ret_value
->u
.ret
= -1;
409 ret_value
->_errno
= errno
;
410 ret_value
->_error
= true;
413 /* Ownership of dirfd is transferred to the handle. */
414 data
->u
.mkdir
.dirfd
= -1;
415 /* Safe to call as we have transitioned to the requested uid/gid. */
416 ret_value
->u
.ret
= lttng_directory_handle_create_subdirectory(
418 ret_value
->_errno
= errno
;
419 ret_value
->_error
= (ret_value
->u
.ret
) ? true : false;
420 lttng_directory_handle_put(handle
);
422 return ret_value
->u
.ret
;
426 int _open(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
429 struct lttng_directory_handle
*handle
;
431 handle
= lttng_directory_handle_create_from_dirfd(data
->u
.open
.dirfd
);
433 ret_value
->_errno
= errno
;
434 ret_value
->_error
= true;
435 ret_value
->u
.ret
= -1;
438 /* Ownership of dirfd is transferred to the handle. */
439 data
->u
.open
.dirfd
= -1;
441 fd
= lttng_directory_handle_open_file(handle
,
442 data
->u
.open
.path
, data
->u
.open
.flags
,
445 ret_value
->u
.ret
= -1;
446 ret_value
->u
.open
.fd
= -1;
448 ret_value
->u
.ret
= 0;
449 ret_value
->u
.open
.fd
= fd
;
452 ret_value
->_errno
= errno
;
453 ret_value
->_error
= fd
< 0;
454 lttng_directory_handle_put(handle
);
456 return ret_value
->u
.ret
;
460 int _unlink(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
462 struct lttng_directory_handle
*handle
;
464 handle
= lttng_directory_handle_create_from_dirfd(data
->u
.unlink
.dirfd
);
466 ret_value
->u
.ret
= -1;
467 ret_value
->_errno
= errno
;
468 ret_value
->_error
= true;
472 /* Ownership of dirfd is transferred to the handle. */
473 data
->u
.unlink
.dirfd
= -1;
475 ret_value
->u
.ret
= lttng_directory_handle_unlink_file(handle
,
476 data
->u
.unlink
.path
);
477 ret_value
->_errno
= errno
;
478 ret_value
->_error
= (ret_value
->u
.ret
) ? true : false;
479 lttng_directory_handle_put(handle
);
481 return ret_value
->u
.ret
;
485 int _rmdir(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
487 struct lttng_directory_handle
*handle
;
489 handle
= lttng_directory_handle_create_from_dirfd(data
->u
.rmdir
.dirfd
);
491 ret_value
->u
.ret
= -1;
492 ret_value
->_errno
= errno
;
493 ret_value
->_error
= true;
497 /* Ownership of dirfd is transferred to the handle. */
498 data
->u
.rmdir
.dirfd
= -1;
500 ret_value
->u
.ret
= lttng_directory_handle_remove_subdirectory(
501 handle
, data
->u
.rmdir
.path
);
502 ret_value
->_errno
= errno
;
503 ret_value
->_error
= (ret_value
->u
.ret
) ? true : false;
504 lttng_directory_handle_put(handle
);
506 return ret_value
->u
.ret
;
510 int _rmdir_recursive(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
512 struct lttng_directory_handle
*handle
;
514 handle
= lttng_directory_handle_create_from_dirfd(data
->u
.rmdir
.dirfd
);
516 ret_value
->u
.ret
= -1;
517 ret_value
->_errno
= errno
;
518 ret_value
->_error
= true;
522 /* Ownership of dirfd is transferred to the handle. */
523 data
->u
.rmdir
.dirfd
= -1;
525 ret_value
->u
.ret
= lttng_directory_handle_remove_subdirectory_recursive(
526 handle
, data
->u
.rmdir
.path
, data
->u
.rmdir
.flags
);
527 ret_value
->_errno
= errno
;
528 ret_value
->_error
= (ret_value
->u
.ret
) ? true : false;
529 lttng_directory_handle_put(handle
);
531 return ret_value
->u
.ret
;
535 int _rename(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
537 const char *old_path
, *new_path
;
538 struct lttng_directory_handle
*old_handle
= NULL
, *new_handle
= NULL
;
540 old_path
= data
->u
.rename
.old_path
;
541 new_path
= data
->u
.rename
.new_path
;
543 old_handle
= lttng_directory_handle_create_from_dirfd(
544 data
->u
.rename
.dirfds
[0]);
546 ret_value
->u
.ret
= -1;
549 new_handle
= lttng_directory_handle_create_from_dirfd(
550 data
->u
.rename
.dirfds
[1]);
552 ret_value
->u
.ret
= -1;
556 /* Ownership of dirfds are transferred to the handles. */
557 data
->u
.rename
.dirfds
[0] = data
->u
.rename
.dirfds
[1] = -1;
559 /* Safe to call as we have transitioned to the requested uid/gid. */
560 ret_value
->u
.ret
= lttng_directory_handle_rename(
561 old_handle
, old_path
, new_handle
, new_path
);
563 lttng_directory_handle_put(old_handle
);
564 lttng_directory_handle_put(new_handle
);
565 ret_value
->_errno
= errno
;
566 ret_value
->_error
= (ret_value
->u
.ret
) ? true : false;
567 return ret_value
->u
.ret
;
572 int _extract_elf_symbol_offset(struct run_as_data
*data
,
573 struct run_as_ret
*ret_value
)
578 ret_value
->_error
= false;
579 ret
= lttng_elf_get_symbol_offset(data
->u
.extract_elf_symbol_offset
.fd
,
580 data
->u
.extract_elf_symbol_offset
.function
,
583 DBG("Failed to extract ELF function offset");
584 ret_value
->_error
= true;
586 ret_value
->u
.extract_elf_symbol_offset
.offset
= offset
;
592 int _extract_sdt_probe_offsets(struct run_as_data
*data
,
593 struct run_as_ret
*ret_value
)
596 uint64_t *offsets
= NULL
;
599 ret_value
->_error
= false;
601 /* On success, this call allocates the offsets paramater. */
602 ret
= lttng_elf_get_sdt_probe_offsets(
603 data
->u
.extract_sdt_probe_offsets
.fd
,
604 data
->u
.extract_sdt_probe_offsets
.provider_name
,
605 data
->u
.extract_sdt_probe_offsets
.probe_name
,
606 &offsets
, &num_offset
);
609 DBG("Failed to extract SDT probe offsets");
610 ret_value
->_error
= true;
614 if (num_offset
<= 0 || num_offset
> LTTNG_KERNEL_ABI_MAX_UPROBE_NUM
) {
615 DBG("Wrong number of probes.");
617 ret_value
->_error
= true;
621 /* Copy the content of the offsets array to the ret struct. */
622 memcpy(ret_value
->u
.extract_sdt_probe_offsets
.offsets
,
623 offsets
, num_offset
* sizeof(uint64_t));
625 ret_value
->u
.extract_sdt_probe_offsets
.num_offset
= num_offset
;
634 int _extract_elf_symbol_offset(
635 struct run_as_data
*data
__attribute__((unused
)),
636 struct run_as_ret
*ret_value
__attribute__((unused
)))
638 ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET");
643 int _extract_sdt_probe_offsets(
644 struct run_as_data
*data
__attribute__((unused
)),
645 struct run_as_ret
*ret_value
__attribute__((unused
)))
647 ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS");
653 int _generate_filter_bytecode(struct run_as_data
*data
,
654 struct run_as_ret
*ret_value
) {
656 const char *filter_expression
= NULL
;
657 struct filter_parser_ctx
*ctx
= NULL
;
659 ret_value
->_error
= false;
661 filter_expression
= data
->u
.generate_filter_bytecode
.filter_expression
;
663 if (lttng_strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
- 1) == LTTNG_FILTER_MAX_LEN
- 1) {
664 ret_value
->_error
= true;
669 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
671 ret_value
->_error
= true;
676 DBG("Size of bytecode generated: %u bytes.",
677 bytecode_get_len(&ctx
->bytecode
->b
));
679 /* Copy the lttng_bytecode_filter object to the return structure. */
680 memcpy(ret_value
->u
.generate_filter_bytecode
.bytecode
,
682 sizeof(ctx
->bytecode
->b
) +
683 bytecode_get_len(&ctx
->bytecode
->b
));
687 filter_bytecode_free(ctx
);
689 filter_parser_ctx_free(ctx
);
695 run_as_fct
run_as_enum_to_fct(enum run_as_cmd cmd
)
701 case RUN_AS_MKDIR_RECURSIVE
:
702 case RUN_AS_MKDIRAT_RECURSIVE
:
703 return _mkdirat_recursive
;
708 case RUN_AS_UNLINKAT
:
713 case RUN_AS_RMDIR_RECURSIVE
:
714 case RUN_AS_RMDIRAT_RECURSIVE
:
715 return _rmdir_recursive
;
717 case RUN_AS_RENAMEAT
:
719 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
720 return _extract_elf_symbol_offset
;
721 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
722 return _extract_sdt_probe_offsets
;
723 case RUN_AS_GENERATE_FILTER_BYTECODE
:
724 return _generate_filter_bytecode
;
726 ERR("Unknown command %d", (int) cmd
);
732 int do_send_fds(int sock
, const int *fds
, unsigned int fd_count
)
737 for (i
= 0; i
< fd_count
; i
++) {
739 DBG("Attempt to send invalid file descriptor (fd = %i)",
741 /* Return 0 as this is not a fatal error. */
746 len
= lttcomm_send_fds_unix_sock(sock
, fds
, fd_count
);
747 return len
< 0 ? -1 : 0;
751 int do_recv_fds(int sock
, int *fds
, unsigned int fd_count
)
757 len
= lttcomm_recv_fds_unix_sock(sock
, fds
, fd_count
);
761 } else if (len
< 0) {
762 PERROR("Failed to receive file descriptors from socket");
767 for (i
= 0; i
< fd_count
; i
++) {
769 ERR("Invalid file descriptor received from worker (fd = %i)", fds
[i
]);
770 /* Return 0 as this is not a fatal error. */
778 int send_fds_to_worker(const run_as_worker_data
*worker
,
779 const struct run_as_data
*data
)
784 if (COMMAND_USE_CWD_FD(data
) || COMMAND_IN_FD_COUNT(data
) == 0) {
788 for (i
= 0; i
< COMMAND_IN_FD_COUNT(data
); i
++) {
789 if (COMMAND_IN_FDS(data
)[i
] < 0) {
790 ERR("Refusing to send invalid fd to worker (fd = %i)",
791 COMMAND_IN_FDS(data
)[i
]);
797 ret
= do_send_fds(worker
->sockpair
[0], COMMAND_IN_FDS(data
),
798 COMMAND_IN_FD_COUNT(data
));
800 PERROR("Failed to send file descriptor to run-as worker");
809 int send_fds_to_master(run_as_worker_data
*worker
, enum run_as_cmd cmd
,
810 struct run_as_ret
*run_as_ret
)
815 if (COMMAND_OUT_FD_COUNT(cmd
) == 0) {
819 ret
= do_send_fds(worker
->sockpair
[1], COMMAND_OUT_FDS(cmd
, run_as_ret
),
820 COMMAND_OUT_FD_COUNT(cmd
));
822 PERROR("Failed to send file descriptor to master process");
826 for (i
= 0; i
< COMMAND_OUT_FD_COUNT(cmd
); i
++) {
827 int fd
= COMMAND_OUT_FDS(cmd
, run_as_ret
)[i
];
829 int ret_close
= close(fd
);
832 PERROR("Failed to close result file descriptor (fd = %i)",
842 int recv_fds_from_worker(const run_as_worker_data
*worker
, enum run_as_cmd cmd
,
843 struct run_as_ret
*run_as_ret
)
847 if (COMMAND_OUT_FD_COUNT(cmd
) == 0) {
851 ret
= do_recv_fds(worker
->sockpair
[0], COMMAND_OUT_FDS(cmd
, run_as_ret
),
852 COMMAND_OUT_FD_COUNT(cmd
));
854 PERROR("Failed to receive file descriptor from run-as worker");
862 int recv_fds_from_master(run_as_worker_data
*worker
, struct run_as_data
*data
)
866 if (COMMAND_USE_CWD_FD(data
)) {
869 for (i
= 0; i
< COMMAND_IN_FD_COUNT(data
); i
++) {
870 COMMAND_IN_FDS(data
)[i
] = AT_FDCWD
;
875 if (COMMAND_IN_FD_COUNT(data
) == 0) {
879 ret
= do_recv_fds(worker
->sockpair
[1], COMMAND_IN_FDS(data
),
880 COMMAND_IN_FD_COUNT(data
));
882 PERROR("Failed to receive file descriptors from master process");
890 int cleanup_received_fds(struct run_as_data
*data
)
894 for (i
= 0; i
< COMMAND_IN_FD_COUNT(data
); i
++) {
895 if (COMMAND_IN_FDS(data
)[i
] == -1) {
898 ret
= close(COMMAND_IN_FDS(data
)[i
]);
900 PERROR("Failed to close file descriptor received fd in run-as worker");
908 static int get_user_infos_from_uid(
909 uid_t uid
, char **username
, gid_t
*primary_gid
)
913 long raw_get_pw_buf_size
;
914 size_t get_pw_buf_size
;
916 struct passwd
*result
= NULL
;
918 /* Fetch the max size for the temporary buffer. */
920 raw_get_pw_buf_size
= sysconf(_SC_GETPW_R_SIZE_MAX
);
921 if (raw_get_pw_buf_size
< 0) {
923 PERROR("Failed to query _SC_GETPW_R_SIZE_MAX");
927 /* Limit is indeterminate. */
928 WARN("Failed to query _SC_GETPW_R_SIZE_MAX as it is "
929 "indeterminate; falling back to default buffer size");
930 raw_get_pw_buf_size
= GETPW_BUFFER_FALLBACK_SIZE
;
933 get_pw_buf_size
= (size_t) raw_get_pw_buf_size
;
935 buf
= calloc
<char>(get_pw_buf_size
);
937 PERROR("Failed to allocate buffer to get password file entries");
941 ret
= getpwuid_r(uid
, &pwd
, buf
, get_pw_buf_size
, &result
);
943 PERROR("Failed to get user information for user: uid = %d",
948 if (result
== NULL
) {
949 ERR("Failed to find user information in password entries: uid = %d",
955 *username
= strdup(result
->pw_name
);
956 if (*username
== NULL
) {
957 PERROR("Failed to copy user name");
961 *primary_gid
= result
->pw_gid
;
973 static int demote_creds(
974 uid_t prev_uid
, gid_t prev_gid
, uid_t new_uid
, gid_t new_gid
)
978 char *username
= NULL
;
980 /* Change the group id. */
981 if (prev_gid
!= new_gid
) {
982 ret
= setegid(new_gid
);
984 PERROR("Failed to set effective group id: new_gid = %d",
990 /* Change the user id. */
991 if (prev_uid
!= new_uid
) {
992 ret
= get_user_infos_from_uid(new_uid
, &username
, &primary_gid
);
998 * Initialize the supplementary group access list.
1000 * This is needed to handle cases where the supplementary groups
1001 * of the user the process is demoting-to would give it access
1002 * to a given file/folder, but not it's primary group.
1006 * Primary Group: User1
1007 * Secondary group: Disk, Network
1009 * mkdir inside the following directory must work since User1
1010 * is part of the Network group.
1012 * drwxrwx--- 2 root Network 4096 Jul 23 17:17 /tmp/my_folder/
1015 * The order of the following initgroups and seteuid calls is
1017 * Only a root process or one with CAP_SETGID capability can
1018 * call the the initgroups() function. We must initialize the
1019 * supplementary groups before we change the effective
1020 * UID to a less-privileged user.
1022 ret
= initgroups(username
, primary_gid
);
1024 PERROR("Failed to init the supplementary group access list: "
1025 "username = `%s`, primary gid = %d", username
,
1030 ret
= seteuid(new_uid
);
1032 PERROR("Failed to set effective user id: new_uid = %d",
1042 static int promote_creds(
1043 uid_t prev_uid
, gid_t prev_gid
, uid_t new_uid
, gid_t new_gid
)
1047 char *username
= NULL
;
1049 /* Change the group id. */
1050 if (prev_gid
!= new_gid
) {
1051 ret
= setegid(new_gid
);
1053 PERROR("Failed to set effective group id: new_gid = %d",
1059 /* Change the user id. */
1060 if (prev_uid
!= new_uid
) {
1061 ret
= get_user_infos_from_uid(new_uid
, &username
, &primary_gid
);
1067 * seteuid call must be done before the initgroups call because
1068 * we need to be privileged (CAP_SETGID) to call initgroups().
1070 ret
= seteuid(new_uid
);
1072 PERROR("Failed to set effective user id: new_uid = %d",
1078 * Initialize the supplementary group access list.
1080 * There is a possibility the groups we set in the following
1081 * initgroups() call are not exactly the same as the ones we
1082 * had when we originally demoted. This can happen if the
1083 * /etc/group file is modified after the runas process is
1084 * forked. This is very unlikely.
1086 ret
= initgroups(username
, primary_gid
);
1088 PERROR("Failed to init the supplementary group access "
1089 "list: username = `%s`, primary gid = %d",
1090 username
, (int) primary_gid
)
1100 * Return < 0 on error, 0 if OK, 1 on hangup.
1103 int handle_one_cmd(run_as_worker_data
*worker
)
1105 int ret
= 0, promote_ret
;
1106 struct run_as_data data
= {};
1107 ssize_t readlen
, writelen
;
1108 struct run_as_ret sendret
= {};
1110 const uid_t prev_ruid
= getuid();
1111 const gid_t prev_rgid
= getgid();
1114 * Stage 1: Receive run_as_data struct from the master.
1115 * The structure contains the command type and all the parameters needed for
1118 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[1], &data
,
1125 if (readlen
< sizeof(data
)) {
1126 PERROR("lttcomm_recv_unix_sock error");
1131 cmd
= run_as_enum_to_fct(data
.cmd
);
1138 * Stage 2: Receive file descriptor from master.
1139 * Some commands need a file descriptor as input so if it's needed we
1140 * receive the fd using the Unix socket.
1142 ret
= recv_fds_from_master(worker
, &data
);
1144 PERROR("recv_fd_from_master error");
1149 ret
= demote_creds(prev_ruid
, prev_rgid
, data
.uid
, data
.gid
);
1155 * Also set umask to 0 for mkdir executable bit.
1160 * Stage 3: Execute the command
1162 ret
= (*cmd
)(&data
, &sendret
);
1164 DBG("Execution of command returned an error");
1168 ret
= cleanup_received_fds(&data
);
1170 ERR("Error cleaning up FD");
1175 * Stage 4: Send run_as_ret structure to the master.
1176 * This structure contain the return value of the command and the errno.
1178 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
1180 if (writelen
< sizeof(sendret
)) {
1181 PERROR("lttcomm_send_unix_sock error");
1187 * Stage 5: Send resulting file descriptors to the master.
1189 ret
= send_fds_to_master(worker
, data
.cmd
, &sendret
);
1191 DBG("Sending FD to master returned an error");
1197 /* Return to previous uid/gid. */
1198 promote_ret
= promote_creds(data
.uid
, data
.gid
, prev_ruid
, prev_rgid
);
1199 if (promote_ret
< 0) {
1200 ERR("Failed to promote back to the initial credentials");
1208 int run_as_worker(run_as_worker_data
*worker
)
1212 struct run_as_ret sendret
;
1213 size_t proc_orig_len
;
1216 * Initialize worker. Set a different process cmdline.
1218 proc_orig_len
= strlen(worker
->procname
);
1219 memset(worker
->procname
, 0, proc_orig_len
);
1220 strncpy(worker
->procname
, DEFAULT_RUN_AS_WORKER_NAME
, proc_orig_len
);
1222 ret
= lttng_thread_setname(DEFAULT_RUN_AS_WORKER_NAME
);
1223 if (ret
&& ret
!= -ENOSYS
) {
1224 /* Don't fail as this is not essential. */
1225 DBG("Failed to set pthread name attribute");
1228 memset(&sendret
, 0, sizeof(sendret
));
1230 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
1232 if (writelen
< sizeof(sendret
)) {
1233 PERROR("lttcomm_send_unix_sock error");
1239 ret
= handle_one_cmd(worker
);
1243 } else if (ret
> 0) {
1246 continue; /* Next command. */
1255 int run_as_cmd(run_as_worker_data
*worker
,
1256 enum run_as_cmd cmd
,
1257 struct run_as_data
*data
,
1258 struct run_as_ret
*ret_value
,
1259 uid_t uid
, gid_t gid
)
1262 ssize_t readlen
, writelen
;
1265 * If we are non-root, we can only deal with our own uid.
1267 if (geteuid() != 0) {
1268 if (uid
!= geteuid()) {
1270 ret_value
->_errno
= EPERM
;
1271 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
1272 (int) uid
, (int) geteuid());
1282 * Stage 1: Send the run_as_data struct to the worker process
1284 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[0], data
,
1286 if (writelen
< sizeof(*data
)) {
1287 PERROR("Error writing message to run_as");
1289 ret_value
->_errno
= EIO
;
1294 * Stage 2: Send file descriptor to the worker process if needed
1296 ret
= send_fds_to_worker(worker
, data
);
1298 PERROR("do_send_fd error");
1300 ret_value
->_errno
= EIO
;
1305 * Stage 3: Wait for the execution of the command
1309 * Stage 4: Receive the run_as_ret struct containing the return value and
1312 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0], ret_value
,
1313 sizeof(*ret_value
));
1315 ERR("Run-as worker has hung-up during run_as_cmd");
1317 ret_value
->_errno
= EIO
;
1319 } else if (readlen
< sizeof(*ret_value
)) {
1320 PERROR("Error reading response from run_as");
1322 ret_value
->_errno
= errno
;
1326 if (ret_value
->_error
) {
1327 /* Skip stage 5 on error as there will be no fd to receive. */
1332 * Stage 5: Receive file descriptor if needed
1334 ret
= recv_fds_from_worker(worker
, cmd
, ret_value
);
1336 ERR("Error receiving fd");
1338 ret_value
->_errno
= EIO
;
1346 * This is for debugging ONLY and should not be considered secure.
1349 int run_as_noworker(enum run_as_cmd cmd
,
1350 struct run_as_data
*data
, struct run_as_ret
*ret_value
,
1351 uid_t uid
__attribute__((unused
)), gid_t gid
__attribute__((unused
)))
1353 int ret
, saved_errno
;
1357 fct
= run_as_enum_to_fct(cmd
);
1363 old_mask
= umask(0);
1364 ret
= fct(data
, ret_value
);
1365 saved_errno
= ret_value
->_errno
;
1367 errno
= saved_errno
;
1373 int reset_sighandler(void)
1377 DBG("Resetting run_as worker signal handlers to default");
1378 for (sig
= 1; sig
<= 31; sig
++) {
1379 (void) signal(sig
, SIG_DFL
);
1385 void worker_sighandler(int sig
)
1387 const char *signame
;
1390 * The worker will inherit its parent's signals since they are part of
1391 * the same process group. However, in the case of SIGINT and SIGTERM,
1392 * we want to give the worker a chance to teardown gracefully when its
1393 * parent closes the command socket.
1400 signame
= "SIGTERM";
1407 DBG("run_as worker received signal %s", signame
);
1409 DBG("run_as_worker received signal %d", sig
);
1414 int set_worker_sighandlers(void)
1418 struct sigaction sa
;
1420 if ((ret
= sigemptyset(&sigset
)) < 0) {
1421 PERROR("sigemptyset");
1425 sa
.sa_handler
= worker_sighandler
;
1426 sa
.sa_mask
= sigset
;
1428 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
1429 PERROR("sigaction SIGINT");
1433 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
1434 PERROR("sigaction SIGTERM");
1438 DBG("run_as signal handler set for SIGTERM and SIGINT");
1444 int run_as_create_worker_no_lock(const char *procname
,
1445 post_fork_cleanup_cb clean_up_func
,
1446 void *clean_up_user_data
)
1451 struct run_as_ret recvret
;
1452 run_as_worker_data
*worker
;
1454 LTTNG_ASSERT(!global_worker
);
1457 * Don't initialize a worker, all run_as tasks will be performed
1458 * in the current process.
1463 worker
= zmalloc
<run_as_worker_data
>();
1468 worker
->procname
= strdup(procname
);
1469 if (!worker
->procname
) {
1471 goto error_procname_alloc
;
1473 /* Create unix socket. */
1474 if (lttcomm_create_anon_unix_socketpair(worker
->sockpair
) < 0) {
1485 } else if (pid
== 0) {
1490 set_worker_sighandlers();
1492 logger_set_thread_name("Run-as worker", true);
1494 if (clean_up_func
) {
1495 if (clean_up_func(clean_up_user_data
) < 0) {
1496 ERR("Run-as post-fork clean-up failed, exiting.");
1501 /* Just close, no shutdown. */
1502 if (close(worker
->sockpair
[0])) {
1508 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
1509 * Sockpair[1] is used as a control channel with the master
1511 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1512 if (i
!= worker
->sockpair
[1]) {
1517 worker
->sockpair
[0] = -1;
1518 ret
= run_as_worker(worker
);
1519 if (lttcomm_close_unix_sock(worker
->sockpair
[1])) {
1523 worker
->sockpair
[1] = -1;
1524 free(worker
->procname
);
1526 LOG(ret
? PRINT_ERR
: PRINT_DBG
, "run_as worker exiting (ret = %d)", ret
);
1527 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
1531 /* Just close, no shutdown. */
1532 if (close(worker
->sockpair
[1])) {
1537 worker
->sockpair
[1] = -1;
1539 /* Wait for worker to become ready. */
1540 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0],
1541 &recvret
, sizeof(recvret
));
1542 if (readlen
< sizeof(recvret
)) {
1543 ERR("readlen: %zd", readlen
);
1544 PERROR("Error reading response from run_as at creation");
1548 global_worker
= worker
;
1553 /* Error handling. */
1555 for (i
= 0; i
< 2; i
++) {
1556 if (worker
->sockpair
[i
] < 0) {
1559 if (lttcomm_close_unix_sock(worker
->sockpair
[i
])) {
1562 worker
->sockpair
[i
] = -1;
1565 free(worker
->procname
);
1566 error_procname_alloc
:
1572 void run_as_destroy_worker_no_lock(void)
1574 run_as_worker_data
*worker
= global_worker
;
1576 DBG("Destroying run_as worker");
1580 /* Close unix socket */
1581 DBG("Closing run_as worker socket");
1582 if (lttcomm_close_unix_sock(worker
->sockpair
[0])) {
1585 worker
->sockpair
[0] = -1;
1586 /* Wait for worker. */
1591 wait_ret
= waitpid(worker
->pid
, &status
, 0);
1593 if (errno
== EINTR
) {
1600 if (WIFEXITED(status
)) {
1601 LOG(WEXITSTATUS(status
) == 0 ? PRINT_DBG
: PRINT_ERR
,
1602 DEFAULT_RUN_AS_WORKER_NAME
" terminated with status code %d",
1603 WEXITSTATUS(status
));
1605 } else if (WIFSIGNALED(status
)) {
1606 ERR(DEFAULT_RUN_AS_WORKER_NAME
" was killed by signal %d",
1611 free(worker
->procname
);
1613 global_worker
= NULL
;
1617 int run_as_restart_worker(run_as_worker_data
*worker
)
1620 char *procname
= NULL
;
1622 procname
= worker
->procname
;
1624 /* Close socket to run_as worker process and clean up the zombie process */
1625 run_as_destroy_worker_no_lock();
1627 /* Create a new run_as worker process*/
1628 ret
= run_as_create_worker_no_lock(procname
, NULL
, NULL
);
1630 ERR("Restarting the worker process failed");
1639 int run_as(enum run_as_cmd cmd
, struct run_as_data
*data
,
1640 struct run_as_ret
*ret_value
, uid_t uid
, gid_t gid
)
1642 int ret
, saved_errno
;
1644 pthread_mutex_lock(&worker_lock
);
1646 DBG("Using run_as worker");
1648 LTTNG_ASSERT(global_worker
);
1650 ret
= run_as_cmd(global_worker
, cmd
, data
, ret_value
, uid
, gid
);
1651 saved_errno
= ret_value
->_errno
;
1654 * If the worker thread crashed the errno is set to EIO. we log
1655 * the error and start a new worker process.
1657 if (ret
== -1 && saved_errno
== EIO
) {
1658 DBG("Socket closed unexpectedly... "
1659 "Restarting the worker process");
1660 ret
= run_as_restart_worker(global_worker
);
1662 ERR("Failed to restart worker process.");
1667 DBG("Using run_as without worker");
1668 ret
= run_as_noworker(cmd
, data
, ret_value
, uid
, gid
);
1671 pthread_mutex_unlock(&worker_lock
);
1675 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1677 return run_as_mkdirat_recursive(AT_FDCWD
, path
, mode
, uid
, gid
);
1680 int run_as_mkdirat_recursive(int dirfd
, const char *path
, mode_t mode
,
1681 uid_t uid
, gid_t gid
)
1684 struct run_as_data data
= {};
1685 struct run_as_ret run_as_ret
= {};
1687 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1688 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1689 path
, (int) mode
, (int) uid
, (int) gid
);
1690 ret
= lttng_strncpy(data
.u
.mkdir
.path
, path
,
1691 sizeof(data
.u
.mkdir
.path
));
1693 ERR("Failed to copy path argument of mkdirat recursive command");
1696 data
.u
.mkdir
.path
[sizeof(data
.u
.mkdir
.path
) - 1] = '\0';
1697 data
.u
.mkdir
.mode
= mode
;
1698 data
.u
.mkdir
.dirfd
= dirfd
;
1699 run_as(dirfd
== AT_FDCWD
? RUN_AS_MKDIR_RECURSIVE
: RUN_AS_MKDIRAT_RECURSIVE
,
1700 &data
, &run_as_ret
, uid
, gid
);
1701 errno
= run_as_ret
._errno
;
1702 ret
= run_as_ret
.u
.ret
;
1707 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1709 return run_as_mkdirat(AT_FDCWD
, path
, mode
, uid
, gid
);
1712 int run_as_mkdirat(int dirfd
, const char *path
, mode_t mode
,
1713 uid_t uid
, gid_t gid
)
1716 struct run_as_data data
= {};
1717 struct run_as_ret run_as_ret
= {};
1719 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1720 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1721 path
, (int) mode
, (int) uid
, (int) gid
);
1722 ret
= lttng_strncpy(data
.u
.mkdir
.path
, path
,
1723 sizeof(data
.u
.mkdir
.path
));
1725 ERR("Failed to copy path argument of mkdirat command");
1728 data
.u
.mkdir
.path
[sizeof(data
.u
.mkdir
.path
) - 1] = '\0';
1729 data
.u
.mkdir
.mode
= mode
;
1730 data
.u
.mkdir
.dirfd
= dirfd
;
1731 run_as(dirfd
== AT_FDCWD
? RUN_AS_MKDIR
: RUN_AS_MKDIRAT
,
1732 &data
, &run_as_ret
, uid
, gid
);
1733 errno
= run_as_ret
._errno
;
1734 ret
= run_as_ret
.u
.ret
;
1739 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
,
1742 return run_as_openat(AT_FDCWD
, path
, flags
, mode
, uid
, gid
);
1745 int run_as_openat(int dirfd
, const char *path
, int flags
, mode_t mode
,
1746 uid_t uid
, gid_t gid
)
1749 struct run_as_data data
= {};
1750 struct run_as_ret run_as_ret
= {};
1752 DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
1753 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1754 path
, flags
, (int) mode
, (int) uid
, (int) gid
);
1755 ret
= lttng_strncpy(data
.u
.open
.path
, path
, sizeof(data
.u
.open
.path
));
1757 ERR("Failed to copy path argument of open command");
1760 data
.u
.open
.flags
= flags
;
1761 data
.u
.open
.mode
= mode
;
1762 data
.u
.open
.dirfd
= dirfd
;
1763 run_as(dirfd
== AT_FDCWD
? RUN_AS_OPEN
: RUN_AS_OPENAT
,
1764 &data
, &run_as_ret
, uid
, gid
);
1765 errno
= run_as_ret
._errno
;
1766 ret
= run_as_ret
.u
.ret
< 0 ? run_as_ret
.u
.ret
:
1767 run_as_ret
.u
.open
.fd
;
1772 int run_as_unlink(const char *path
, uid_t uid
, gid_t gid
)
1774 return run_as_unlinkat(AT_FDCWD
, path
, uid
, gid
);
1777 int run_as_unlinkat(int dirfd
, const char *path
, uid_t uid
, gid_t gid
)
1780 struct run_as_data data
= {};
1781 struct run_as_ret run_as_ret
= {};
1783 DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
1784 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1785 path
, (int) uid
, (int) gid
);
1786 ret
= lttng_strncpy(data
.u
.unlink
.path
, path
,
1787 sizeof(data
.u
.unlink
.path
));
1791 data
.u
.unlink
.dirfd
= dirfd
;
1792 run_as(dirfd
== AT_FDCWD
? RUN_AS_UNLINK
: RUN_AS_UNLINKAT
, &data
,
1793 &run_as_ret
, uid
, gid
);
1794 errno
= run_as_ret
._errno
;
1795 ret
= run_as_ret
.u
.ret
;
1800 int run_as_rmdir(const char *path
, uid_t uid
, gid_t gid
)
1802 return run_as_rmdirat(AT_FDCWD
, path
, uid
, gid
);
1805 int run_as_rmdirat(int dirfd
, const char *path
, uid_t uid
, gid_t gid
)
1808 struct run_as_data data
= {};
1809 struct run_as_ret run_as_ret
= {};
1811 DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d",
1812 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1813 path
, (int) uid
, (int) gid
);
1814 ret
= lttng_strncpy(data
.u
.rmdir
.path
, path
,
1815 sizeof(data
.u
.rmdir
.path
));
1819 data
.u
.rmdir
.dirfd
= dirfd
;
1820 run_as(dirfd
== AT_FDCWD
? RUN_AS_RMDIR
: RUN_AS_RMDIRAT
, &data
,
1821 &run_as_ret
, uid
, gid
);
1822 errno
= run_as_ret
._errno
;
1823 ret
= run_as_ret
.u
.ret
;
1828 int run_as_rmdir_recursive(const char *path
, uid_t uid
, gid_t gid
, int flags
)
1830 return run_as_rmdirat_recursive(AT_FDCWD
, path
, uid
, gid
, flags
);
1833 int run_as_rmdirat_recursive(int dirfd
, const char *path
, uid_t uid
, gid_t gid
, int flags
)
1836 struct run_as_data data
= {};
1837 struct run_as_ret run_as_ret
= {};
1839 DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d",
1840 dirfd
, dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1841 path
, (int) uid
, (int) gid
);
1842 ret
= lttng_strncpy(data
.u
.rmdir
.path
, path
,
1843 sizeof(data
.u
.rmdir
.path
));
1847 data
.u
.rmdir
.dirfd
= dirfd
;
1848 data
.u
.rmdir
.flags
= flags
;
1849 run_as(dirfd
== AT_FDCWD
? RUN_AS_RMDIR_RECURSIVE
: RUN_AS_RMDIRAT_RECURSIVE
,
1850 &data
, &run_as_ret
, uid
, gid
);
1851 errno
= run_as_ret
._errno
;
1852 ret
= run_as_ret
.u
.ret
;
1857 int run_as_rename(const char *old_name
, const char *new_name
, uid_t uid
, gid_t gid
)
1859 return run_as_renameat(AT_FDCWD
, old_name
, AT_FDCWD
, new_name
, uid
, gid
);
1862 int run_as_renameat(int old_dirfd
, const char *old_name
,
1863 int new_dirfd
, const char *new_name
, uid_t uid
, gid_t gid
)
1866 struct run_as_data data
= {};
1867 struct run_as_ret run_as_ret
= {};
1869 DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d",
1870 old_dirfd
, old_dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1872 new_dirfd
, new_dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1873 new_name
, (int) uid
, (int) gid
);
1874 ret
= lttng_strncpy(data
.u
.rename
.old_path
, old_name
,
1875 sizeof(data
.u
.rename
.old_path
));
1879 ret
= lttng_strncpy(data
.u
.rename
.new_path
, new_name
,
1880 sizeof(data
.u
.rename
.new_path
));
1885 data
.u
.rename
.dirfds
[0] = old_dirfd
;
1886 data
.u
.rename
.dirfds
[1] = new_dirfd
;
1887 run_as(old_dirfd
== AT_FDCWD
&& new_dirfd
== AT_FDCWD
?
1888 RUN_AS_RENAME
: RUN_AS_RENAMEAT
,
1889 &data
, &run_as_ret
, uid
, gid
);
1890 errno
= run_as_ret
._errno
;
1891 ret
= run_as_ret
.u
.ret
;
1896 int run_as_extract_elf_symbol_offset(int fd
, const char* function
,
1897 uid_t uid
, gid_t gid
, uint64_t *offset
)
1900 struct run_as_data data
= {};
1901 struct run_as_ret run_as_ret
= {};
1903 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
1904 "with for uid %d and gid %d", fd
, function
,
1905 (int) uid
, (int) gid
);
1907 data
.u
.extract_elf_symbol_offset
.fd
= fd
;
1909 strncpy(data
.u
.extract_elf_symbol_offset
.function
, function
, LTTNG_SYMBOL_NAME_LEN
- 1);
1910 data
.u
.extract_elf_symbol_offset
.function
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1911 ret
= lttng_strncpy(data
.u
.extract_elf_symbol_offset
.function
,
1913 sizeof(data
.u
.extract_elf_symbol_offset
.function
));
1918 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
, &data
, &run_as_ret
, uid
, gid
);
1919 errno
= run_as_ret
._errno
;
1920 if (run_as_ret
._error
) {
1925 *offset
= run_as_ret
.u
.extract_elf_symbol_offset
.offset
;
1930 int run_as_extract_sdt_probe_offsets(int fd
, const char* provider_name
,
1931 const char* probe_name
, uid_t uid
, gid_t gid
,
1932 uint64_t **offsets
, uint32_t *num_offset
)
1935 struct run_as_data data
= {};
1936 struct run_as_ret run_as_ret
= {};
1938 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
1939 "provider_name=%s with for uid %d and gid %d", fd
,
1940 probe_name
, provider_name
, (int) uid
, (int) gid
);
1942 data
.u
.extract_sdt_probe_offsets
.fd
= fd
;
1944 ret
= lttng_strncpy(data
.u
.extract_sdt_probe_offsets
.probe_name
, probe_name
,
1945 sizeof(data
.u
.extract_sdt_probe_offsets
.probe_name
));
1949 ret
= lttng_strncpy(data
.u
.extract_sdt_probe_offsets
.provider_name
,
1951 sizeof(data
.u
.extract_sdt_probe_offsets
.provider_name
));
1956 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
, &data
, &run_as_ret
, uid
, gid
);
1957 errno
= run_as_ret
._errno
;
1958 if (run_as_ret
._error
) {
1963 *num_offset
= run_as_ret
.u
.extract_sdt_probe_offsets
.num_offset
;
1964 *offsets
= calloc
<uint64_t>(*num_offset
);
1970 memcpy(*offsets
, run_as_ret
.u
.extract_sdt_probe_offsets
.offsets
,
1971 *num_offset
* sizeof(uint64_t));
1976 int run_as_generate_filter_bytecode(const char *filter_expression
,
1977 const struct lttng_credentials
*creds
,
1978 struct lttng_bytecode
**bytecode
)
1981 struct run_as_data data
= {};
1982 struct run_as_ret run_as_ret
= {};
1983 const struct lttng_bytecode
*view_bytecode
= NULL
;
1984 struct lttng_bytecode
*local_bytecode
= NULL
;
1985 const uid_t uid
= lttng_credentials_get_uid(creds
);
1986 const gid_t gid
= lttng_credentials_get_gid(creds
);
1988 DBG3("generate_filter_bytecode() from expression=\"%s\" for uid %d and gid %d",
1989 filter_expression
, (int) uid
, (int) gid
);
1991 ret
= lttng_strncpy(data
.u
.generate_filter_bytecode
.filter_expression
, filter_expression
,
1992 sizeof(data
.u
.generate_filter_bytecode
.filter_expression
));
1997 run_as(RUN_AS_GENERATE_FILTER_BYTECODE
, &data
, &run_as_ret
, uid
, gid
);
1998 errno
= run_as_ret
._errno
;
1999 if (run_as_ret
._error
) {
2004 view_bytecode
= (const struct lttng_bytecode
*) run_as_ret
.u
.generate_filter_bytecode
.bytecode
;
2006 local_bytecode
= calloc
<lttng_bytecode
>(view_bytecode
->len
);
2007 if (!local_bytecode
) {
2012 memcpy(local_bytecode
, run_as_ret
.u
.generate_filter_bytecode
.bytecode
,
2013 sizeof(*local_bytecode
) + view_bytecode
->len
);
2014 *bytecode
= local_bytecode
;
2019 int run_as_create_worker(const char *procname
,
2020 post_fork_cleanup_cb clean_up_func
,
2021 void *clean_up_user_data
)
2025 pthread_mutex_lock(&worker_lock
);
2026 ret
= run_as_create_worker_no_lock(procname
, clean_up_func
,
2027 clean_up_user_data
);
2028 pthread_mutex_unlock(&worker_lock
);
2032 void run_as_destroy_worker(void)
2034 pthread_mutex_lock(&worker_lock
);
2035 run_as_destroy_worker_no_lock();
2036 pthread_mutex_unlock(&worker_lock
);