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
13 #include <common/bytecode/bytecode.hpp>
14 #include <common/common.hpp>
15 #include <common/compat/errno.hpp>
16 #include <common/compat/getenv.hpp>
17 #include <common/compat/string.hpp>
18 #include <common/defaults.hpp>
19 #include <common/filter/filter-ast.hpp>
20 #include <common/lttng-elf.hpp>
21 #include <common/lttng-kernel.hpp>
22 #include <common/sessiond-comm/sessiond-comm.hpp>
23 #include <common/thread.hpp>
24 #include <common/unix.hpp>
25 #include <common/utils.hpp>
27 #include <lttng/constant.h>
39 #include <sys/types.h>
43 #define GETPW_BUFFER_FALLBACK_SIZE 4096
48 RUN_AS_MKDIR_RECURSIVE
,
49 RUN_AS_MKDIRAT_RECURSIVE
,
56 RUN_AS_RMDIR_RECURSIVE
,
57 RUN_AS_RMDIRAT_RECURSIVE
,
60 RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
,
61 RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
,
62 RUN_AS_GENERATE_FILTER_BYTECODE
,
68 using run_as_fct
= int (*)(struct run_as_data
*, struct run_as_ret
*);
70 struct run_as_mkdir_data
{
72 char path
[LTTNG_PATH_MAX
];
76 struct run_as_open_data
{
78 char path
[LTTNG_PATH_MAX
];
83 struct run_as_unlink_data
{
85 char path
[LTTNG_PATH_MAX
];
88 struct run_as_rmdir_data
{
90 char path
[LTTNG_PATH_MAX
];
91 int flags
; /* enum lttng_directory_handle_rmdir_recursive_flags. */
94 struct run_as_extract_elf_symbol_offset_data
{
96 char function
[LTTNG_SYMBOL_NAME_LEN
];
99 struct run_as_extract_sdt_probe_offsets_data
{
101 char probe_name
[LTTNG_SYMBOL_NAME_LEN
];
102 char provider_name
[LTTNG_SYMBOL_NAME_LEN
];
105 struct run_as_generate_filter_bytecode_data
{
106 char filter_expression
[LTTNG_FILTER_MAX_LEN
];
109 struct run_as_rename_data
{
115 char old_path
[LTTNG_PATH_MAX
];
116 char new_path
[LTTNG_PATH_MAX
];
119 struct run_as_open_ret
{
123 struct run_as_extract_elf_symbol_offset_ret
{
127 struct run_as_extract_sdt_probe_offsets_ret
{
129 uint64_t offsets
[LTTNG_KERNEL_ABI_MAX_UPROBE_NUM
];
132 struct run_as_generate_filter_bytecode_ret
{
133 /* A lttng_bytecode_filter struct with 'dynamic' payload. */
134 char bytecode
[LTTNG_FILTER_MAX_LEN
];
140 struct run_as_mkdir_data mkdir
;
141 struct run_as_open_data open
;
142 struct run_as_unlink_data unlink
;
143 struct run_as_rmdir_data rmdir
;
144 struct run_as_rename_data rename
;
145 struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset
;
146 struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets
;
147 struct run_as_generate_filter_bytecode_data generate_filter_bytecode
;
154 * The run_as_ret structure holds the returned value and status of the command.
156 * The `u` union field holds the return value of the command; in most cases it
157 * represents the success or the failure of the command. In more complex
158 * commands, it holds a computed value.
160 * The _errno field is the errno recorded after the execution of the command.
162 * The _error fields is used the signify that return status of the command. For
163 * simple commands returning `int` the _error field will be the same as the
164 * ret_int field. In complex commands, it signify the success or failure of the
171 struct run_as_open_ret open
;
172 struct run_as_extract_elf_symbol_offset_ret extract_elf_symbol_offset
;
173 struct run_as_extract_sdt_probe_offsets_ret extract_sdt_probe_offsets
;
174 struct run_as_generate_filter_bytecode_ret generate_filter_bytecode
;
180 #define COMMAND_IN_FDS(data_ptr) \
183 if (command_properties[(data_ptr)->cmd].in_fds_offset != -1) { \
184 fds = (int *) ((char *) (data_ptr) + \
185 command_properties[(data_ptr)->cmd].in_fds_offset); \
190 #define COMMAND_OUT_FDS(cmd, ret_ptr) \
193 if (command_properties[cmd].out_fds_offset != -1) { \
194 fds = (int *) ((char *) (ret_ptr) + \
195 command_properties[cmd].out_fds_offset); \
200 #define COMMAND_IN_FD_COUNT(data_ptr) ({ command_properties[(data_ptr)->cmd].in_fd_count; })
202 #define COMMAND_OUT_FD_COUNT(cmd) ({ command_properties[cmd].out_fd_count; })
204 #define COMMAND_USE_CWD_FD(data_ptr) command_properties[(data_ptr)->cmd].use_cwd_fd
206 struct run_as_command_properties
{
207 /* Set to -1 when not applicable. */
208 ptrdiff_t in_fds_offset
, out_fds_offset
;
209 unsigned int in_fd_count
, out_fd_count
;
213 const struct run_as_command_properties command_properties
[] = {
215 .in_fds_offset
= offsetof(struct run_as_data
, u
.mkdir
.dirfd
),
216 .out_fds_offset
= -1,
222 .in_fds_offset
= offsetof(struct run_as_data
, u
.mkdir
.dirfd
),
223 .out_fds_offset
= -1,
229 .in_fds_offset
= offsetof(struct run_as_data
, u
.mkdir
.dirfd
),
230 .out_fds_offset
= -1,
236 .in_fds_offset
= offsetof(struct run_as_data
, u
.mkdir
.dirfd
),
237 .out_fds_offset
= -1,
243 .in_fds_offset
= offsetof(struct run_as_data
, u
.open
.dirfd
),
244 .out_fds_offset
= offsetof(struct run_as_ret
, u
.open
.fd
),
250 .in_fds_offset
= offsetof(struct run_as_data
, u
.open
.dirfd
),
251 .out_fds_offset
= offsetof(struct run_as_ret
, u
.open
.fd
),
257 .in_fds_offset
= offsetof(struct run_as_data
, u
.unlink
.dirfd
),
258 .out_fds_offset
= -1,
264 .in_fds_offset
= offsetof(struct run_as_data
, u
.unlink
.dirfd
),
265 .out_fds_offset
= -1,
271 .in_fds_offset
= offsetof(struct run_as_data
, u
.rmdir
.dirfd
),
272 .out_fds_offset
= -1,
278 .in_fds_offset
= offsetof(struct run_as_data
, u
.rmdir
.dirfd
),
279 .out_fds_offset
= -1,
285 .in_fds_offset
= offsetof(struct run_as_data
, u
.rmdir
.dirfd
),
286 .out_fds_offset
= -1,
292 .in_fds_offset
= offsetof(struct run_as_data
, u
.rmdir
.dirfd
),
293 .out_fds_offset
= -1,
299 .in_fds_offset
= offsetof(struct run_as_data
, u
.rename
.dirfds
),
300 .out_fds_offset
= -1,
306 .in_fds_offset
= offsetof(struct run_as_data
, u
.rename
.dirfds
),
307 .out_fds_offset
= -1,
313 .in_fds_offset
= offsetof(struct run_as_data
, u
.extract_elf_symbol_offset
.fd
),
314 .out_fds_offset
= -1,
320 .in_fds_offset
= offsetof(struct run_as_data
, u
.extract_sdt_probe_offsets
.fd
),
321 .out_fds_offset
= -1,
328 .out_fds_offset
= -1,
335 struct run_as_worker_data
{
336 pid_t pid
; /* Worker PID. */
341 /* Single global worker per process (for now). */
342 run_as_worker_data
*global_worker
;
343 /* Lock protecting the worker. */
344 pthread_mutex_t worker_lock
= PTHREAD_MUTEX_INITIALIZER
;
348 static int use_clone(void)
353 static int use_clone()
355 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
360 * Create recursively directory using the FULL path.
362 static int _mkdirat_recursive(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
366 struct lttng_directory_handle
*handle
;
368 path
= data
->u
.mkdir
.path
;
369 mode
= data
->u
.mkdir
.mode
;
371 handle
= lttng_directory_handle_create_from_dirfd(data
->u
.mkdir
.dirfd
);
373 ret_value
->_errno
= errno
;
374 ret_value
->_error
= true;
375 ret_value
->u
.ret
= -1;
378 /* Ownership of dirfd is transferred to the handle. */
379 data
->u
.mkdir
.dirfd
= -1;
380 /* Safe to call as we have transitioned to the requested uid/gid. */
381 ret_value
->u
.ret
= lttng_directory_handle_create_subdirectory_recursive(handle
, path
, mode
);
382 ret_value
->_errno
= errno
;
383 ret_value
->_error
= (ret_value
->u
.ret
) != 0;
384 lttng_directory_handle_put(handle
);
386 return ret_value
->u
.ret
;
389 static int _mkdirat(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
393 struct lttng_directory_handle
*handle
;
395 path
= data
->u
.mkdir
.path
;
396 mode
= data
->u
.mkdir
.mode
;
398 handle
= lttng_directory_handle_create_from_dirfd(data
->u
.mkdir
.dirfd
);
400 ret_value
->u
.ret
= -1;
401 ret_value
->_errno
= errno
;
402 ret_value
->_error
= true;
405 /* Ownership of dirfd is transferred to the handle. */
406 data
->u
.mkdir
.dirfd
= -1;
407 /* Safe to call as we have transitioned to the requested uid/gid. */
408 ret_value
->u
.ret
= lttng_directory_handle_create_subdirectory(handle
, path
, mode
);
409 ret_value
->_errno
= errno
;
410 ret_value
->_error
= (ret_value
->u
.ret
) != 0;
411 lttng_directory_handle_put(handle
);
413 return ret_value
->u
.ret
;
416 static int _open(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
419 struct lttng_directory_handle
*handle
;
421 handle
= lttng_directory_handle_create_from_dirfd(data
->u
.open
.dirfd
);
423 ret_value
->_errno
= errno
;
424 ret_value
->_error
= true;
425 ret_value
->u
.ret
= -1;
428 /* Ownership of dirfd is transferred to the handle. */
429 data
->u
.open
.dirfd
= -1;
431 fd
= lttng_directory_handle_open_file(
432 handle
, data
->u
.open
.path
, data
->u
.open
.flags
, data
->u
.open
.mode
);
434 ret_value
->u
.ret
= -1;
435 ret_value
->u
.open
.fd
= -1;
437 ret_value
->u
.ret
= 0;
438 ret_value
->u
.open
.fd
= fd
;
441 ret_value
->_errno
= errno
;
442 ret_value
->_error
= fd
< 0;
443 lttng_directory_handle_put(handle
);
445 return ret_value
->u
.ret
;
448 static int _unlink(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
450 struct lttng_directory_handle
*handle
;
452 handle
= lttng_directory_handle_create_from_dirfd(data
->u
.unlink
.dirfd
);
454 ret_value
->u
.ret
= -1;
455 ret_value
->_errno
= errno
;
456 ret_value
->_error
= true;
460 /* Ownership of dirfd is transferred to the handle. */
461 data
->u
.unlink
.dirfd
= -1;
463 ret_value
->u
.ret
= lttng_directory_handle_unlink_file(handle
, data
->u
.unlink
.path
);
464 ret_value
->_errno
= errno
;
465 ret_value
->_error
= (ret_value
->u
.ret
) != 0;
466 lttng_directory_handle_put(handle
);
468 return ret_value
->u
.ret
;
471 static int _rmdir(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
473 struct lttng_directory_handle
*handle
;
475 handle
= lttng_directory_handle_create_from_dirfd(data
->u
.rmdir
.dirfd
);
477 ret_value
->u
.ret
= -1;
478 ret_value
->_errno
= errno
;
479 ret_value
->_error
= true;
483 /* Ownership of dirfd is transferred to the handle. */
484 data
->u
.rmdir
.dirfd
= -1;
486 ret_value
->u
.ret
= lttng_directory_handle_remove_subdirectory(handle
, data
->u
.rmdir
.path
);
487 ret_value
->_errno
= errno
;
488 ret_value
->_error
= (ret_value
->u
.ret
) != 0;
489 lttng_directory_handle_put(handle
);
491 return ret_value
->u
.ret
;
494 static int _rmdir_recursive(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
496 struct lttng_directory_handle
*handle
;
498 handle
= lttng_directory_handle_create_from_dirfd(data
->u
.rmdir
.dirfd
);
500 ret_value
->u
.ret
= -1;
501 ret_value
->_errno
= errno
;
502 ret_value
->_error
= true;
506 /* Ownership of dirfd is transferred to the handle. */
507 data
->u
.rmdir
.dirfd
= -1;
509 ret_value
->u
.ret
= lttng_directory_handle_remove_subdirectory_recursive(
510 handle
, data
->u
.rmdir
.path
, data
->u
.rmdir
.flags
);
511 ret_value
->_errno
= errno
;
512 ret_value
->_error
= (ret_value
->u
.ret
) != 0;
513 lttng_directory_handle_put(handle
);
515 return ret_value
->u
.ret
;
518 static int _rename(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
520 const char *old_path
, *new_path
;
521 struct lttng_directory_handle
*old_handle
= nullptr, *new_handle
= nullptr;
523 old_path
= data
->u
.rename
.old_path
;
524 new_path
= data
->u
.rename
.new_path
;
526 old_handle
= lttng_directory_handle_create_from_dirfd(data
->u
.rename
.dirfds
[0]);
528 ret_value
->u
.ret
= -1;
531 new_handle
= lttng_directory_handle_create_from_dirfd(data
->u
.rename
.dirfds
[1]);
533 ret_value
->u
.ret
= -1;
537 /* Ownership of dirfds are transferred to the handles. */
538 data
->u
.rename
.dirfds
[0] = data
->u
.rename
.dirfds
[1] = -1;
540 /* Safe to call as we have transitioned to the requested uid/gid. */
542 lttng_directory_handle_rename(old_handle
, old_path
, new_handle
, new_path
);
544 lttng_directory_handle_put(old_handle
);
545 lttng_directory_handle_put(new_handle
);
546 ret_value
->_errno
= errno
;
547 ret_value
->_error
= (ret_value
->u
.ret
) != 0;
548 return ret_value
->u
.ret
;
552 static int _extract_elf_symbol_offset(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
557 ret_value
->_error
= false;
558 ret
= lttng_elf_get_symbol_offset(data
->u
.extract_elf_symbol_offset
.fd
,
559 data
->u
.extract_elf_symbol_offset
.function
,
562 DBG("Failed to extract ELF function offset");
563 ret_value
->_error
= true;
565 ret_value
->u
.extract_elf_symbol_offset
.offset
= offset
;
570 static int _extract_sdt_probe_offsets(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
573 uint64_t *offsets
= nullptr;
576 ret_value
->_error
= false;
578 /* On success, this call allocates the offsets paramater. */
579 ret
= lttng_elf_get_sdt_probe_offsets(data
->u
.extract_sdt_probe_offsets
.fd
,
580 data
->u
.extract_sdt_probe_offsets
.provider_name
,
581 data
->u
.extract_sdt_probe_offsets
.probe_name
,
586 DBG("Failed to extract SDT probe offsets");
587 ret_value
->_error
= true;
591 if (num_offset
<= 0 || num_offset
> LTTNG_KERNEL_ABI_MAX_UPROBE_NUM
) {
592 DBG("Wrong number of probes.");
594 ret_value
->_error
= true;
598 /* Copy the content of the offsets array to the ret struct. */
599 memcpy(ret_value
->u
.extract_sdt_probe_offsets
.offsets
,
601 num_offset
* sizeof(uint64_t));
603 ret_value
->u
.extract_sdt_probe_offsets
.num_offset
= num_offset
;
611 static int _extract_elf_symbol_offset(struct run_as_data
*data
__attribute__((unused
)),
612 struct run_as_ret
*ret_value
__attribute__((unused
)))
614 ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET");
618 static int _extract_sdt_probe_offsets(struct run_as_data
*data
__attribute__((unused
)),
619 struct run_as_ret
*ret_value
__attribute__((unused
)))
621 ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS");
626 static int _generate_filter_bytecode(struct run_as_data
*data
, struct run_as_ret
*ret_value
)
629 const char *filter_expression
= nullptr;
630 struct filter_parser_ctx
*ctx
= nullptr;
632 ret_value
->_error
= false;
634 filter_expression
= data
->u
.generate_filter_bytecode
.filter_expression
;
636 if (lttng_strnlen(filter_expression
, LTTNG_FILTER_MAX_LEN
- 1) ==
637 LTTNG_FILTER_MAX_LEN
- 1) {
638 ret_value
->_error
= true;
643 ret
= filter_parser_ctx_create_from_filter_expression(filter_expression
, &ctx
);
645 ret_value
->_error
= true;
650 DBG("Size of bytecode generated: %u bytes.", bytecode_get_len(&ctx
->bytecode
->b
));
652 /* Copy the lttng_bytecode_filter object to the return structure. */
653 memcpy(ret_value
->u
.generate_filter_bytecode
.bytecode
,
655 sizeof(ctx
->bytecode
->b
) + bytecode_get_len(&ctx
->bytecode
->b
));
659 filter_bytecode_free(ctx
);
661 filter_parser_ctx_free(ctx
);
666 static run_as_fct
run_as_enum_to_fct(enum run_as_cmd cmd
)
672 case RUN_AS_MKDIR_RECURSIVE
:
673 case RUN_AS_MKDIRAT_RECURSIVE
:
674 return _mkdirat_recursive
;
679 case RUN_AS_UNLINKAT
:
684 case RUN_AS_RMDIR_RECURSIVE
:
685 case RUN_AS_RMDIRAT_RECURSIVE
:
686 return _rmdir_recursive
;
688 case RUN_AS_RENAMEAT
:
690 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
:
691 return _extract_elf_symbol_offset
;
692 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
:
693 return _extract_sdt_probe_offsets
;
694 case RUN_AS_GENERATE_FILTER_BYTECODE
:
695 return _generate_filter_bytecode
;
697 ERR("Unknown command %d", (int) cmd
);
702 static int do_send_fds(int sock
, const int *fds
, unsigned int fd_count
)
707 for (i
= 0; i
< fd_count
; i
++) {
709 DBG("Attempt to send invalid file descriptor (fd = %i)", fds
[i
]);
710 /* Return 0 as this is not a fatal error. */
715 len
= lttcomm_send_fds_unix_sock(sock
, fds
, fd_count
);
716 return len
< 0 ? -1 : 0;
719 static int do_recv_fds(int sock
, int *fds
, unsigned int fd_count
)
725 len
= lttcomm_recv_fds_unix_sock(sock
, fds
, fd_count
);
729 } else if (len
< 0) {
730 PERROR("Failed to receive file descriptors from socket");
735 for (i
= 0; i
< fd_count
; i
++) {
737 ERR("Invalid file descriptor received from worker (fd = %i)", fds
[i
]);
738 /* Return 0 as this is not a fatal error. */
745 static int send_fds_to_worker(const run_as_worker_data
*worker
, const struct run_as_data
*data
)
750 if (COMMAND_USE_CWD_FD(data
) || COMMAND_IN_FD_COUNT(data
) == 0) {
754 for (i
= 0; i
< COMMAND_IN_FD_COUNT(data
); i
++) {
755 if (COMMAND_IN_FDS(data
)[i
] < 0) {
756 ERR("Refusing to send invalid fd to worker (fd = %i)",
757 COMMAND_IN_FDS(data
)[i
]);
763 ret
= do_send_fds(worker
->sockpair
[0], COMMAND_IN_FDS(data
), COMMAND_IN_FD_COUNT(data
));
765 PERROR("Failed to send file descriptor to run-as worker");
774 send_fds_to_master(run_as_worker_data
*worker
, enum run_as_cmd cmd
, struct run_as_ret
*run_as_ret
)
779 if (COMMAND_OUT_FD_COUNT(cmd
) == 0) {
784 worker
->sockpair
[1], COMMAND_OUT_FDS(cmd
, run_as_ret
), COMMAND_OUT_FD_COUNT(cmd
));
786 PERROR("Failed to send file descriptor to master process");
790 for (i
= 0; i
< COMMAND_OUT_FD_COUNT(cmd
); i
++) {
791 int fd
= COMMAND_OUT_FDS(cmd
, run_as_ret
)[i
];
793 int ret_close
= close(fd
);
796 PERROR("Failed to close result file descriptor (fd = %i)", fd
);
804 static int recv_fds_from_worker(const run_as_worker_data
*worker
,
806 struct run_as_ret
*run_as_ret
)
810 if (COMMAND_OUT_FD_COUNT(cmd
) == 0) {
815 worker
->sockpair
[0], COMMAND_OUT_FDS(cmd
, run_as_ret
), COMMAND_OUT_FD_COUNT(cmd
));
817 PERROR("Failed to receive file descriptor from run-as worker");
824 static int recv_fds_from_master(run_as_worker_data
*worker
, struct run_as_data
*data
)
828 if (COMMAND_USE_CWD_FD(data
)) {
831 for (i
= 0; i
< COMMAND_IN_FD_COUNT(data
); i
++) {
832 COMMAND_IN_FDS(data
)[i
] = AT_FDCWD
;
837 if (COMMAND_IN_FD_COUNT(data
) == 0) {
841 ret
= do_recv_fds(worker
->sockpair
[1], COMMAND_IN_FDS(data
), COMMAND_IN_FD_COUNT(data
));
843 PERROR("Failed to receive file descriptors from master process");
850 static int cleanup_received_fds(struct run_as_data
*data
)
854 for (i
= 0; i
< COMMAND_IN_FD_COUNT(data
); i
++) {
855 if (COMMAND_IN_FDS(data
)[i
] == -1) {
858 ret
= close(COMMAND_IN_FDS(data
)[i
]);
860 PERROR("Failed to close file descriptor received fd in run-as worker");
868 static int get_user_infos_from_uid(uid_t uid
, char **username
, gid_t
*primary_gid
)
872 long raw_get_pw_buf_size
;
873 size_t get_pw_buf_size
;
875 struct passwd
*result
= nullptr;
877 /* Fetch the max size for the temporary buffer. */
879 raw_get_pw_buf_size
= sysconf(_SC_GETPW_R_SIZE_MAX
);
880 if (raw_get_pw_buf_size
< 0) {
882 PERROR("Failed to query _SC_GETPW_R_SIZE_MAX");
886 /* Limit is indeterminate. */
887 WARN("Failed to query _SC_GETPW_R_SIZE_MAX as it is "
888 "indeterminate; falling back to default buffer size");
889 raw_get_pw_buf_size
= GETPW_BUFFER_FALLBACK_SIZE
;
892 get_pw_buf_size
= (size_t) raw_get_pw_buf_size
;
894 buf
= calloc
<char>(get_pw_buf_size
);
895 if (buf
== nullptr) {
896 PERROR("Failed to allocate buffer to get password file entries");
900 ret
= getpwuid_r(uid
, &pwd
, buf
, get_pw_buf_size
, &result
);
902 PERROR("Failed to get user information for user: uid = %d", (int) uid
);
906 if (result
== nullptr) {
907 ERR("Failed to find user information in password entries: uid = %d", (int) uid
);
912 *username
= strdup(result
->pw_name
);
913 if (*username
== nullptr) {
914 PERROR("Failed to copy user name");
918 *primary_gid
= result
->pw_gid
;
930 static int demote_creds(uid_t prev_uid
, gid_t prev_gid
, uid_t new_uid
, gid_t new_gid
)
934 char *username
= nullptr;
936 /* Change the group id. */
937 if (prev_gid
!= new_gid
) {
938 ret
= setegid(new_gid
);
940 PERROR("Failed to set effective group id: new_gid = %d", (int) new_gid
);
945 /* Change the user id. */
946 if (prev_uid
!= new_uid
) {
947 ret
= get_user_infos_from_uid(new_uid
, &username
, &primary_gid
);
953 * Initialize the supplementary group access list.
955 * This is needed to handle cases where the supplementary groups
956 * of the user the process is demoting-to would give it access
957 * to a given file/folder, but not it's primary group.
961 * Primary Group: User1
962 * Secondary group: Disk, Network
964 * mkdir inside the following directory must work since User1
965 * is part of the Network group.
967 * drwxrwx--- 2 root Network 4096 Jul 23 17:17 /tmp/my_folder/
970 * The order of the following initgroups and seteuid calls is
972 * Only a root process or one with CAP_SETGID capability can
973 * call the the initgroups() function. We must initialize the
974 * supplementary groups before we change the effective
975 * UID to a less-privileged user.
977 ret
= initgroups(username
, primary_gid
);
979 PERROR("Failed to init the supplementary group access list: "
980 "username = `%s`, primary gid = %d",
986 ret
= seteuid(new_uid
);
988 PERROR("Failed to set effective user id: new_uid = %d", (int) new_uid
);
997 static int promote_creds(uid_t prev_uid
, gid_t prev_gid
, uid_t new_uid
, gid_t new_gid
)
1001 char *username
= nullptr;
1003 /* Change the group id. */
1004 if (prev_gid
!= new_gid
) {
1005 ret
= setegid(new_gid
);
1007 PERROR("Failed to set effective group id: new_gid = %d", (int) new_gid
);
1012 /* Change the user id. */
1013 if (prev_uid
!= new_uid
) {
1014 ret
= get_user_infos_from_uid(new_uid
, &username
, &primary_gid
);
1020 * seteuid call must be done before the initgroups call because
1021 * we need to be privileged (CAP_SETGID) to call initgroups().
1023 ret
= seteuid(new_uid
);
1025 PERROR("Failed to set effective user id: new_uid = %d", (int) new_uid
);
1030 * Initialize the supplementary group access list.
1032 * There is a possibility the groups we set in the following
1033 * initgroups() call are not exactly the same as the ones we
1034 * had when we originally demoted. This can happen if the
1035 * /etc/group file is modified after the runas process is
1036 * forked. This is very unlikely.
1038 ret
= initgroups(username
, primary_gid
);
1040 PERROR("Failed to init the supplementary group access "
1041 "list: username = `%s`, primary gid = %d",
1053 * Return < 0 on error, 0 if OK, 1 on hangup.
1055 static int handle_one_cmd(run_as_worker_data
*worker
)
1057 int ret
= 0, promote_ret
;
1058 struct run_as_data data
= {};
1059 ssize_t readlen
, writelen
;
1060 struct run_as_ret sendret
= {};
1062 const uid_t prev_ruid
= getuid();
1063 const gid_t prev_rgid
= getgid();
1066 * Stage 1: Receive run_as_data struct from the master.
1067 * The structure contains the command type and all the parameters needed for
1070 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[1], &data
, sizeof(data
));
1076 if (readlen
< sizeof(data
)) {
1077 PERROR("lttcomm_recv_unix_sock error");
1082 cmd
= run_as_enum_to_fct(data
.cmd
);
1089 * Stage 2: Receive file descriptor from master.
1090 * Some commands need a file descriptor as input so if it's needed we
1091 * receive the fd using the Unix socket.
1093 ret
= recv_fds_from_master(worker
, &data
);
1095 PERROR("recv_fd_from_master error");
1100 ret
= demote_creds(prev_ruid
, prev_rgid
, data
.uid
, data
.gid
);
1106 * Also set umask to 0 for mkdir executable bit.
1111 * Stage 3: Execute the command
1113 ret
= (*cmd
)(&data
, &sendret
);
1115 DBG("Execution of command returned an error");
1119 ret
= cleanup_received_fds(&data
);
1121 ERR("Error cleaning up FD");
1126 * Stage 4: Send run_as_ret structure to the master.
1127 * This structure contain the return value of the command and the errno.
1129 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
, sizeof(sendret
));
1130 if (writelen
< sizeof(sendret
)) {
1131 PERROR("lttcomm_send_unix_sock error");
1137 * Stage 5: Send resulting file descriptors to the master.
1139 ret
= send_fds_to_master(worker
, data
.cmd
, &sendret
);
1141 DBG("Sending FD to master returned an error");
1147 /* Return to previous uid/gid. */
1148 promote_ret
= promote_creds(data
.uid
, data
.gid
, prev_ruid
, prev_rgid
);
1149 if (promote_ret
< 0) {
1150 ERR("Failed to promote back to the initial credentials");
1157 static int run_as_worker(run_as_worker_data
*worker
)
1161 struct run_as_ret sendret
;
1162 size_t proc_orig_len
;
1165 * Initialize worker. Set a different process cmdline.
1167 proc_orig_len
= strlen(worker
->procname
);
1168 memset(worker
->procname
, 0, proc_orig_len
);
1169 strncpy(worker
->procname
, DEFAULT_RUN_AS_WORKER_NAME
, proc_orig_len
);
1171 ret
= lttng_thread_setname(DEFAULT_RUN_AS_WORKER_NAME
);
1172 if (ret
&& ret
!= -ENOSYS
) {
1173 /* Don't fail as this is not essential. */
1174 DBG("Failed to set pthread name attribute");
1177 memset(&sendret
, 0, sizeof(sendret
));
1179 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
, sizeof(sendret
));
1180 if (writelen
< sizeof(sendret
)) {
1181 PERROR("lttcomm_send_unix_sock error");
1187 ret
= handle_one_cmd(worker
);
1191 } else if (ret
> 0) {
1194 continue; /* Next command. */
1202 static int run_as_cmd(run_as_worker_data
*worker
,
1203 enum run_as_cmd cmd
,
1204 struct run_as_data
*data
,
1205 struct run_as_ret
*ret_value
,
1210 ssize_t readlen
, writelen
;
1213 * If we are non-root, we can only deal with our own uid.
1215 if (geteuid() != 0) {
1216 if (uid
!= geteuid()) {
1218 ret_value
->_errno
= EPERM
;
1219 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
1231 * Stage 1: Send the run_as_data struct to the worker process
1233 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[0], data
, sizeof(*data
));
1234 if (writelen
< sizeof(*data
)) {
1235 PERROR("Error writing message to run_as");
1237 ret_value
->_errno
= EIO
;
1242 * Stage 2: Send file descriptor to the worker process if needed
1244 ret
= send_fds_to_worker(worker
, data
);
1246 PERROR("do_send_fd error");
1248 ret_value
->_errno
= EIO
;
1253 * Stage 3: Wait for the execution of the command
1257 * Stage 4: Receive the run_as_ret struct containing the return value and
1260 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0], ret_value
, sizeof(*ret_value
));
1262 ERR("Run-as worker has hung-up during run_as_cmd");
1264 ret_value
->_errno
= EIO
;
1266 } else if (readlen
< sizeof(*ret_value
)) {
1267 PERROR("Error reading response from run_as");
1269 ret_value
->_errno
= errno
;
1273 if (ret_value
->_error
) {
1274 /* Skip stage 5 on error as there will be no fd to receive. */
1279 * Stage 5: Receive file descriptor if needed
1281 ret
= recv_fds_from_worker(worker
, cmd
, ret_value
);
1283 ERR("Error receiving fd");
1285 ret_value
->_errno
= EIO
;
1293 * This is for debugging ONLY and should not be considered secure.
1295 static int run_as_noworker(enum run_as_cmd cmd
,
1296 struct run_as_data
*data
,
1297 struct run_as_ret
*ret_value
,
1298 uid_t uid
__attribute__((unused
)),
1299 gid_t gid
__attribute__((unused
)))
1301 int ret
, saved_errno
;
1305 fct
= run_as_enum_to_fct(cmd
);
1311 old_mask
= umask(0);
1312 ret
= fct(data
, ret_value
);
1313 saved_errno
= ret_value
->_errno
;
1315 errno
= saved_errno
;
1320 static int reset_sighandler()
1324 DBG("Resetting run_as worker signal handlers to default");
1325 for (sig
= 1; sig
<= 31; sig
++) {
1326 (void) signal(sig
, SIG_DFL
);
1331 static void worker_sighandler(int sig
)
1333 const char *signame
;
1336 * The worker will inherit its parent's signals since they are part of
1337 * the same process group. However, in the case of SIGINT and SIGTERM,
1338 * we want to give the worker a chance to teardown gracefully when its
1339 * parent closes the command socket.
1346 signame
= "SIGTERM";
1353 DBG("run_as worker received signal %s", signame
);
1355 DBG("run_as_worker received signal %d", sig
);
1359 static int set_worker_sighandlers()
1363 struct sigaction sa
;
1365 if ((ret
= sigemptyset(&sigset
)) < 0) {
1366 PERROR("sigemptyset");
1370 sa
.sa_handler
= worker_sighandler
;
1371 sa
.sa_mask
= sigset
;
1373 if ((ret
= sigaction(SIGINT
, &sa
, nullptr)) < 0) {
1374 PERROR("sigaction SIGINT");
1378 if ((ret
= sigaction(SIGTERM
, &sa
, nullptr)) < 0) {
1379 PERROR("sigaction SIGTERM");
1383 DBG("run_as signal handler set for SIGTERM and SIGINT");
1388 static int run_as_create_worker_no_lock(const char *procname
,
1389 post_fork_cleanup_cb clean_up_func
,
1390 void *clean_up_user_data
)
1395 struct run_as_ret recvret
;
1396 run_as_worker_data
*worker
;
1398 LTTNG_ASSERT(!global_worker
);
1401 * Don't initialize a worker, all run_as tasks will be performed
1402 * in the current process.
1407 worker
= zmalloc
<run_as_worker_data
>();
1412 worker
->procname
= strdup(procname
);
1413 if (!worker
->procname
) {
1415 goto error_procname_alloc
;
1417 /* Create unix socket. */
1418 if (lttcomm_create_anon_unix_socketpair(worker
->sockpair
) < 0) {
1429 } else if (pid
== 0) {
1434 set_worker_sighandlers();
1436 logger_set_thread_name("Run-as worker", true);
1438 if (clean_up_func
) {
1439 if (clean_up_func(clean_up_user_data
) < 0) {
1440 ERR("Run-as post-fork clean-up failed, exiting.");
1445 /* Just close, no shutdown. */
1446 if (close(worker
->sockpair
[0])) {
1452 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
1453 * Sockpair[1] is used as a control channel with the master
1455 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
1456 if (i
!= worker
->sockpair
[1]) {
1461 worker
->sockpair
[0] = -1;
1462 ret
= run_as_worker(worker
);
1463 if (lttcomm_close_unix_sock(worker
->sockpair
[1])) {
1467 worker
->sockpair
[1] = -1;
1468 free(worker
->procname
);
1470 LOG(ret
? PRINT_ERR
: PRINT_DBG
, "run_as worker exiting (ret = %d)", ret
);
1471 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
1475 /* Just close, no shutdown. */
1476 if (close(worker
->sockpair
[1])) {
1481 worker
->sockpair
[1] = -1;
1483 /* Wait for worker to become ready. */
1484 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0], &recvret
, sizeof(recvret
));
1485 if (readlen
< sizeof(recvret
)) {
1486 ERR("readlen: %zd", readlen
);
1487 PERROR("Error reading response from run_as at creation");
1491 global_worker
= worker
;
1496 /* Error handling. */
1498 for (i
= 0; i
< 2; i
++) {
1499 if (worker
->sockpair
[i
] < 0) {
1502 if (lttcomm_close_unix_sock(worker
->sockpair
[i
])) {
1505 worker
->sockpair
[i
] = -1;
1508 free(worker
->procname
);
1509 error_procname_alloc
:
1514 static void run_as_destroy_worker_no_lock()
1516 run_as_worker_data
*worker
= global_worker
;
1518 DBG("Destroying run_as worker");
1522 /* Close unix socket */
1523 DBG("Closing run_as worker socket");
1524 if (lttcomm_close_unix_sock(worker
->sockpair
[0])) {
1527 worker
->sockpair
[0] = -1;
1528 /* Wait for worker. */
1533 wait_ret
= waitpid(worker
->pid
, &status
, 0);
1535 if (errno
== EINTR
) {
1542 if (WIFEXITED(status
)) {
1543 LOG(WEXITSTATUS(status
) == 0 ? PRINT_DBG
: PRINT_ERR
,
1544 DEFAULT_RUN_AS_WORKER_NAME
" terminated with status code %d",
1545 WEXITSTATUS(status
));
1547 } else if (WIFSIGNALED(status
)) {
1548 ERR(DEFAULT_RUN_AS_WORKER_NAME
" was killed by signal %d",
1553 free(worker
->procname
);
1555 global_worker
= nullptr;
1558 static int run_as_restart_worker(run_as_worker_data
*worker
)
1561 char *procname
= nullptr;
1563 procname
= worker
->procname
;
1565 /* Close socket to run_as worker process and clean up the zombie process */
1566 run_as_destroy_worker_no_lock();
1568 /* Create a new run_as worker process*/
1569 ret
= run_as_create_worker_no_lock(procname
, nullptr, nullptr);
1571 ERR("Restarting the worker process failed");
1579 static int run_as(enum run_as_cmd cmd
,
1580 struct run_as_data
*data
,
1581 struct run_as_ret
*ret_value
,
1585 int ret
, saved_errno
;
1587 pthread_mutex_lock(&worker_lock
);
1589 DBG("Using run_as worker");
1591 LTTNG_ASSERT(global_worker
);
1593 ret
= run_as_cmd(global_worker
, cmd
, data
, ret_value
, uid
, gid
);
1594 saved_errno
= ret_value
->_errno
;
1597 * If the worker thread crashed the errno is set to EIO. we log
1598 * the error and start a new worker process.
1600 if (ret
== -1 && saved_errno
== EIO
) {
1601 DBG("Socket closed unexpectedly... "
1602 "Restarting the worker process");
1603 ret
= run_as_restart_worker(global_worker
);
1605 ERR("Failed to restart worker process.");
1610 DBG("Using run_as without worker");
1611 ret
= run_as_noworker(cmd
, data
, ret_value
, uid
, gid
);
1614 pthread_mutex_unlock(&worker_lock
);
1618 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1620 return run_as_mkdirat_recursive(AT_FDCWD
, path
, mode
, uid
, gid
);
1623 int run_as_mkdirat_recursive(int dirfd
, const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1626 struct run_as_data data
= {};
1627 struct run_as_ret run_as_ret
= {};
1629 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1631 dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1636 ret
= lttng_strncpy(data
.u
.mkdir
.path
, path
, sizeof(data
.u
.mkdir
.path
));
1638 ERR("Failed to copy path argument of mkdirat recursive command");
1641 data
.u
.mkdir
.path
[sizeof(data
.u
.mkdir
.path
) - 1] = '\0';
1642 data
.u
.mkdir
.mode
= mode
;
1643 data
.u
.mkdir
.dirfd
= dirfd
;
1644 run_as(dirfd
== AT_FDCWD
? RUN_AS_MKDIR_RECURSIVE
: RUN_AS_MKDIRAT_RECURSIVE
,
1649 errno
= run_as_ret
._errno
;
1650 ret
= run_as_ret
.u
.ret
;
1655 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1657 return run_as_mkdirat(AT_FDCWD
, path
, mode
, uid
, gid
);
1660 int run_as_mkdirat(int dirfd
, const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
1663 struct run_as_data data
= {};
1664 struct run_as_ret run_as_ret
= {};
1666 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1668 dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1673 ret
= lttng_strncpy(data
.u
.mkdir
.path
, path
, sizeof(data
.u
.mkdir
.path
));
1675 ERR("Failed to copy path argument of mkdirat command");
1678 data
.u
.mkdir
.path
[sizeof(data
.u
.mkdir
.path
) - 1] = '\0';
1679 data
.u
.mkdir
.mode
= mode
;
1680 data
.u
.mkdir
.dirfd
= dirfd
;
1681 run_as(dirfd
== AT_FDCWD
? RUN_AS_MKDIR
: RUN_AS_MKDIRAT
, &data
, &run_as_ret
, uid
, gid
);
1682 errno
= run_as_ret
._errno
;
1683 ret
= run_as_ret
.u
.ret
;
1688 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
1690 return run_as_openat(AT_FDCWD
, path
, flags
, mode
, uid
, gid
);
1693 int run_as_openat(int dirfd
, const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
1696 struct run_as_data data
= {};
1697 struct run_as_ret run_as_ret
= {};
1699 DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
1701 dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1707 ret
= lttng_strncpy(data
.u
.open
.path
, path
, sizeof(data
.u
.open
.path
));
1709 ERR("Failed to copy path argument of open command");
1712 data
.u
.open
.flags
= flags
;
1713 data
.u
.open
.mode
= mode
;
1714 data
.u
.open
.dirfd
= dirfd
;
1715 run_as(dirfd
== AT_FDCWD
? RUN_AS_OPEN
: RUN_AS_OPENAT
, &data
, &run_as_ret
, uid
, gid
);
1716 errno
= run_as_ret
._errno
;
1717 ret
= run_as_ret
.u
.ret
< 0 ? run_as_ret
.u
.ret
: run_as_ret
.u
.open
.fd
;
1722 int run_as_unlink(const char *path
, uid_t uid
, gid_t gid
)
1724 return run_as_unlinkat(AT_FDCWD
, path
, uid
, gid
);
1727 int run_as_unlinkat(int dirfd
, const char *path
, uid_t uid
, gid_t gid
)
1730 struct run_as_data data
= {};
1731 struct run_as_ret run_as_ret
= {};
1733 DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
1735 dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1739 ret
= lttng_strncpy(data
.u
.unlink
.path
, path
, sizeof(data
.u
.unlink
.path
));
1743 data
.u
.unlink
.dirfd
= dirfd
;
1744 run_as(dirfd
== AT_FDCWD
? RUN_AS_UNLINK
: RUN_AS_UNLINKAT
, &data
, &run_as_ret
, uid
, gid
);
1745 errno
= run_as_ret
._errno
;
1746 ret
= run_as_ret
.u
.ret
;
1751 int run_as_rmdir(const char *path
, uid_t uid
, gid_t gid
)
1753 return run_as_rmdirat(AT_FDCWD
, path
, uid
, gid
);
1756 int run_as_rmdirat(int dirfd
, const char *path
, uid_t uid
, gid_t gid
)
1759 struct run_as_data data
= {};
1760 struct run_as_ret run_as_ret
= {};
1762 DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d",
1764 dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1768 ret
= lttng_strncpy(data
.u
.rmdir
.path
, path
, sizeof(data
.u
.rmdir
.path
));
1772 data
.u
.rmdir
.dirfd
= dirfd
;
1773 run_as(dirfd
== AT_FDCWD
? RUN_AS_RMDIR
: RUN_AS_RMDIRAT
, &data
, &run_as_ret
, uid
, gid
);
1774 errno
= run_as_ret
._errno
;
1775 ret
= run_as_ret
.u
.ret
;
1780 int run_as_rmdir_recursive(const char *path
, uid_t uid
, gid_t gid
, int flags
)
1782 return run_as_rmdirat_recursive(AT_FDCWD
, path
, uid
, gid
, flags
);
1785 int run_as_rmdirat_recursive(int dirfd
, const char *path
, uid_t uid
, gid_t gid
, int flags
)
1788 struct run_as_data data
= {};
1789 struct run_as_ret run_as_ret
= {};
1791 DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d",
1793 dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1797 ret
= lttng_strncpy(data
.u
.rmdir
.path
, path
, sizeof(data
.u
.rmdir
.path
));
1801 data
.u
.rmdir
.dirfd
= dirfd
;
1802 data
.u
.rmdir
.flags
= flags
;
1803 run_as(dirfd
== AT_FDCWD
? RUN_AS_RMDIR_RECURSIVE
: RUN_AS_RMDIRAT_RECURSIVE
,
1808 errno
= run_as_ret
._errno
;
1809 ret
= run_as_ret
.u
.ret
;
1814 int run_as_rename(const char *old_name
, const char *new_name
, uid_t uid
, gid_t gid
)
1816 return run_as_renameat(AT_FDCWD
, old_name
, AT_FDCWD
, new_name
, uid
, gid
);
1819 int run_as_renameat(int old_dirfd
,
1820 const char *old_name
,
1822 const char *new_name
,
1827 struct run_as_data data
= {};
1828 struct run_as_ret run_as_ret
= {};
1830 DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d",
1832 old_dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1835 new_dirfd
== AT_FDCWD
? " (AT_FDCWD)" : "",
1839 ret
= lttng_strncpy(data
.u
.rename
.old_path
, old_name
, sizeof(data
.u
.rename
.old_path
));
1843 ret
= lttng_strncpy(data
.u
.rename
.new_path
, new_name
, sizeof(data
.u
.rename
.new_path
));
1848 data
.u
.rename
.dirfds
[0] = old_dirfd
;
1849 data
.u
.rename
.dirfds
[1] = new_dirfd
;
1850 run_as(old_dirfd
== AT_FDCWD
&& new_dirfd
== AT_FDCWD
? RUN_AS_RENAME
: RUN_AS_RENAMEAT
,
1855 errno
= run_as_ret
._errno
;
1856 ret
= run_as_ret
.u
.ret
;
1861 int run_as_extract_elf_symbol_offset(
1862 int fd
, const char *function
, uid_t uid
, gid_t gid
, uint64_t *offset
)
1865 struct run_as_data data
= {};
1866 struct run_as_ret run_as_ret
= {};
1868 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
1869 "with for uid %d and gid %d",
1875 data
.u
.extract_elf_symbol_offset
.fd
= fd
;
1877 strncpy(data
.u
.extract_elf_symbol_offset
.function
, function
, LTTNG_SYMBOL_NAME_LEN
- 1);
1878 data
.u
.extract_elf_symbol_offset
.function
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1879 ret
= lttng_strncpy(data
.u
.extract_elf_symbol_offset
.function
,
1881 sizeof(data
.u
.extract_elf_symbol_offset
.function
));
1886 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET
, &data
, &run_as_ret
, uid
, gid
);
1887 errno
= run_as_ret
._errno
;
1888 if (run_as_ret
._error
) {
1893 *offset
= run_as_ret
.u
.extract_elf_symbol_offset
.offset
;
1898 int run_as_extract_sdt_probe_offsets(int fd
,
1899 const char *provider_name
,
1900 const char *probe_name
,
1904 uint32_t *num_offset
)
1907 struct run_as_data data
= {};
1908 struct run_as_ret run_as_ret
= {};
1910 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
1911 "provider_name=%s with for uid %d and gid %d",
1918 data
.u
.extract_sdt_probe_offsets
.fd
= fd
;
1920 ret
= lttng_strncpy(data
.u
.extract_sdt_probe_offsets
.probe_name
,
1922 sizeof(data
.u
.extract_sdt_probe_offsets
.probe_name
));
1926 ret
= lttng_strncpy(data
.u
.extract_sdt_probe_offsets
.provider_name
,
1928 sizeof(data
.u
.extract_sdt_probe_offsets
.provider_name
));
1933 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS
, &data
, &run_as_ret
, uid
, gid
);
1934 errno
= run_as_ret
._errno
;
1935 if (run_as_ret
._error
) {
1940 *num_offset
= run_as_ret
.u
.extract_sdt_probe_offsets
.num_offset
;
1941 *offsets
= calloc
<uint64_t>(*num_offset
);
1948 run_as_ret
.u
.extract_sdt_probe_offsets
.offsets
,
1949 *num_offset
* sizeof(uint64_t));
1954 int run_as_generate_filter_bytecode(const char *filter_expression
,
1955 const struct lttng_credentials
*creds
,
1956 struct lttng_bytecode
**bytecode
)
1959 struct run_as_data data
= {};
1960 struct run_as_ret run_as_ret
= {};
1961 const struct lttng_bytecode
*view_bytecode
= nullptr;
1962 struct lttng_bytecode
*local_bytecode
= nullptr;
1963 const uid_t uid
= lttng_credentials_get_uid(creds
);
1964 const gid_t gid
= lttng_credentials_get_gid(creds
);
1966 DBG3("generate_filter_bytecode() from expression=\"%s\" for uid %d and gid %d",
1971 ret
= lttng_strncpy(data
.u
.generate_filter_bytecode
.filter_expression
,
1973 sizeof(data
.u
.generate_filter_bytecode
.filter_expression
));
1978 run_as(RUN_AS_GENERATE_FILTER_BYTECODE
, &data
, &run_as_ret
, uid
, gid
);
1979 errno
= run_as_ret
._errno
;
1980 if (run_as_ret
._error
) {
1986 (const struct lttng_bytecode
*) run_as_ret
.u
.generate_filter_bytecode
.bytecode
;
1988 local_bytecode
= calloc
<lttng_bytecode
>(view_bytecode
->len
);
1989 if (!local_bytecode
) {
1994 memcpy(local_bytecode
,
1995 run_as_ret
.u
.generate_filter_bytecode
.bytecode
,
1996 sizeof(*local_bytecode
) + view_bytecode
->len
);
1997 *bytecode
= local_bytecode
;
2002 int run_as_create_worker(const char *procname
,
2003 post_fork_cleanup_cb clean_up_func
,
2004 void *clean_up_user_data
)
2008 pthread_mutex_lock(&worker_lock
);
2009 ret
= run_as_create_worker_no_lock(procname
, clean_up_func
, clean_up_user_data
);
2010 pthread_mutex_unlock(&worker_lock
);
2014 void run_as_destroy_worker()
2016 pthread_mutex_lock(&worker_lock
);
2017 run_as_destroy_worker_no_lock();
2018 pthread_mutex_unlock(&worker_lock
);