Fix: runas: less-than-zero comparison of an unsigned value
[lttng-tools.git] / src / common / runas.c
CommitLineData
60b6c79c 1/*
ab5be9fa
MJ
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2019 Jérémie Galarneau <jeremie.galarneau@efficios.com>
60b6c79c 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
60b6c79c 7 *
60b6c79c
MD
8 */
9
6c1c0768 10#define _LGPL_SOURCE
37ccf8ec
FD
11#include <assert.h>
12#include <fcntl.h>
13#include <grp.h>
60b6c79c 14#include <limits.h>
37ccf8ec
FD
15#include <pwd.h>
16#include <sched.h>
17#include <signal.h>
60b6c79c
MD
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
60b6c79c 21#include <sys/stat.h>
37ccf8ec
FD
22#include <sys/types.h>
23#include <sys/wait.h>
60b6c79c 24#include <unistd.h>
60b6c79c 25
0ae3cfc6 26#include <common/bytecode/bytecode.h>
0ef03255 27#include <common/lttng-kernel.h>
90e535ef 28#include <common/common.h>
3fd15a74 29#include <common/utils.h>
edf4b93e 30#include <common/compat/errno.h>
e8fa9fb0 31#include <common/compat/getenv.h>
c73f064a 32#include <common/compat/string.h>
2038dd6c
JG
33#include <common/unix.h>
34#include <common/defaults.h>
241e0a5a 35#include <common/lttng-elf.h>
cb8d0d24 36#include <common/thread.h>
241e0a5a
FD
37
38#include <lttng/constant.h>
60b6c79c 39
c73f064a
JR
40#include <common/sessiond-comm/sessiond-comm.h>
41#include <common/filter/filter-ast.h>
c73f064a 42
0857097f
DG
43#include "runas.h"
44
37ccf8ec
FD
45#define GETPW_BUFFER_FALLBACK_SIZE 4096
46
7567352f 47struct run_as_data;
fe9f7760
FD
48struct run_as_ret;
49typedef int (*run_as_fct)(struct run_as_data *data, struct run_as_ret *ret_value);
c2b75c49 50
93bed9fe
JG
51enum run_as_cmd {
52 RUN_AS_MKDIR,
53 RUN_AS_MKDIRAT,
54 RUN_AS_MKDIR_RECURSIVE,
55 RUN_AS_MKDIRAT_RECURSIVE,
56 RUN_AS_OPEN,
57 RUN_AS_OPENAT,
58 RUN_AS_UNLINK,
59 RUN_AS_UNLINKAT,
60 RUN_AS_RMDIR,
61 RUN_AS_RMDIRAT,
62 RUN_AS_RMDIR_RECURSIVE,
63 RUN_AS_RMDIRAT_RECURSIVE,
64 RUN_AS_RENAME,
65 RUN_AS_RENAMEAT,
66 RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET,
67 RUN_AS_EXTRACT_SDT_PROBE_OFFSETS,
c73f064a 68 RUN_AS_GENERATE_FILTER_BYTECODE,
60b6c79c
MD
69};
70
93bed9fe
JG
71struct run_as_mkdir_data {
72 int dirfd;
73 char path[LTTNG_PATH_MAX];
74 mode_t mode;
75} LTTNG_PACKED;
76
e11d277b 77struct run_as_open_data {
93bed9fe
JG
78 int dirfd;
79 char path[LTTNG_PATH_MAX];
60b6c79c
MD
80 int flags;
81 mode_t mode;
93bed9fe 82} LTTNG_PACKED;
60b6c79c 83
4628484a 84struct run_as_unlink_data {
93bed9fe
JG
85 int dirfd;
86 char path[LTTNG_PATH_MAX];
87} LTTNG_PACKED;
4628484a 88
93bed9fe
JG
89struct run_as_rmdir_data {
90 int dirfd;
91 char path[LTTNG_PATH_MAX];
c73f064a 92 int flags; /* enum lttng_directory_handle_rmdir_recursive_flags. */
93bed9fe 93} LTTNG_PACKED;
7567352f 94
241e0a5a 95struct run_as_extract_elf_symbol_offset_data {
93bed9fe 96 int fd;
241e0a5a 97 char function[LTTNG_SYMBOL_NAME_LEN];
93bed9fe 98} LTTNG_PACKED;
241e0a5a 99
0ef03255 100struct run_as_extract_sdt_probe_offsets_data {
93bed9fe 101 int fd;
0ef03255
FD
102 char probe_name[LTTNG_SYMBOL_NAME_LEN];
103 char provider_name[LTTNG_SYMBOL_NAME_LEN];
93bed9fe 104} LTTNG_PACKED;
0ef03255 105
c73f064a
JR
106struct run_as_generate_filter_bytecode_data {
107 char filter_expression[LTTNG_FILTER_MAX_LEN];
108} LTTNG_PACKED;
109
93bed9fe
JG
110struct run_as_rename_data {
111 /*
112 * [0] = old_dirfd
113 * [1] = new_dirfd
114 */
115 int dirfds[2];
116 char old_path[LTTNG_PATH_MAX];
117 char new_path[LTTNG_PATH_MAX];
118} LTTNG_PACKED;
fe9f7760
FD
119
120struct run_as_open_ret {
93bed9fe
JG
121 int fd;
122} LTTNG_PACKED;
fe9f7760 123
241e0a5a
FD
124struct run_as_extract_elf_symbol_offset_ret {
125 uint64_t offset;
93bed9fe 126} LTTNG_PACKED;
241e0a5a 127
0ef03255
FD
128struct run_as_extract_sdt_probe_offsets_ret {
129 uint32_t num_offset;
b8e2fb80 130 uint64_t offsets[LTTNG_KERNEL_ABI_MAX_UPROBE_NUM];
93bed9fe 131} LTTNG_PACKED;
7567352f 132
c73f064a
JR
133struct run_as_generate_filter_bytecode_ret {
134 /* A lttng_bytecode_filter struct with 'dynamic' payload. */
135 char bytecode[LTTNG_FILTER_MAX_LEN];
136} LTTNG_PACKED;
137
7567352f
MD
138struct run_as_data {
139 enum run_as_cmd cmd;
140 union {
93bed9fe 141 struct run_as_mkdir_data mkdir;
7567352f
MD
142 struct run_as_open_data open;
143 struct run_as_unlink_data unlink;
93bed9fe
JG
144 struct run_as_rmdir_data rmdir;
145 struct run_as_rename_data rename;
241e0a5a 146 struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset;
0ef03255 147 struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets;
c73f064a 148 struct run_as_generate_filter_bytecode_data generate_filter_bytecode;
7567352f
MD
149 } u;
150 uid_t uid;
151 gid_t gid;
93bed9fe 152} LTTNG_PACKED;
4628484a 153
fe9f7760
FD
154/*
155 * The run_as_ret structure holds the returned value and status of the command.
156 *
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.
160 *
161 * The _errno field is the errno recorded after the execution of the command.
162 *
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
166 * command.
167 *
168 */
df5b86c8 169struct run_as_ret {
fe9f7760 170 union {
93bed9fe 171 int ret;
fe9f7760 172 struct run_as_open_ret open;
241e0a5a 173 struct run_as_extract_elf_symbol_offset_ret extract_elf_symbol_offset;
0ef03255 174 struct run_as_extract_sdt_probe_offsets_ret extract_sdt_probe_offsets;
c73f064a 175 struct run_as_generate_filter_bytecode_ret generate_filter_bytecode;
fe9f7760 176 } u;
df5b86c8 177 int _errno;
fe9f7760 178 bool _error;
93bed9fe
JG
179} LTTNG_PACKED;
180
181#define COMMAND_IN_FDS(data_ptr) ({ \
182 int *fds = NULL; \
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); \
185 } \
186 fds; \
187})
188
189#define COMMAND_OUT_FDS(cmd, ret_ptr) ({ \
190 int *fds = NULL; \
191 if (command_properties[cmd].out_fds_offset != -1) { \
192 fds = (int *) ((char *) ret_ptr + command_properties[cmd].out_fds_offset); \
193 } \
194 fds; \
195})
196
197#define COMMAND_IN_FD_COUNT(data_ptr) ({ \
198 command_properties[data_ptr->cmd].in_fd_count; \
199})
200
201#define COMMAND_OUT_FD_COUNT(cmd) ({ \
202 command_properties[cmd].out_fd_count; \
203})
204
205#define COMMAND_USE_CWD_FD(data_ptr) command_properties[data_ptr->cmd].use_cwd_fd
206
207struct 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;
211 bool use_cwd_fd;
212};
213
214static const struct run_as_command_properties command_properties[] = {
215 [RUN_AS_MKDIR] = {
216 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
217 .in_fd_count = 1,
218 .out_fds_offset = -1,
219 .out_fd_count = 0,
220 .use_cwd_fd = true,
221 },
222 [RUN_AS_MKDIRAT] = {
223 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
224 .in_fd_count = 1,
225 .out_fds_offset = -1,
226 .out_fd_count = 0,
227 .use_cwd_fd = false,
228 },
229 [RUN_AS_MKDIR_RECURSIVE] = {
230 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
231 .in_fd_count = 1,
232 .out_fds_offset = -1,
233 .out_fd_count = 0,
234 .use_cwd_fd = true,
235 },
236 [RUN_AS_MKDIRAT_RECURSIVE] = {
237 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
238 .in_fd_count = 1,
239 .out_fds_offset = -1,
240 .out_fd_count = 0,
241 .use_cwd_fd = false,
242 },
243 [RUN_AS_OPEN] = {
244 .in_fds_offset = offsetof(struct run_as_data, u.open.dirfd),
245 .in_fd_count = 1,
246 .out_fds_offset = offsetof(struct run_as_ret, u.open.fd),
247 .out_fd_count = 1,
248 .use_cwd_fd = true,
249 },
250 [RUN_AS_OPENAT] = {
251 .in_fds_offset = offsetof(struct run_as_data, u.open.dirfd),
252 .in_fd_count = 1,
253 .out_fds_offset = offsetof(struct run_as_ret, u.open.fd),
254 .out_fd_count = 1,
255 .use_cwd_fd = false,
256 },
257 [RUN_AS_UNLINK] = {
258 .in_fds_offset = offsetof(struct run_as_data, u.unlink.dirfd),
259 .in_fd_count = 1,
260 .out_fds_offset = -1,
261 .out_fd_count = 0,
262 .use_cwd_fd = true,
263 },
264 [RUN_AS_UNLINKAT] = {
265 .in_fds_offset = offsetof(struct run_as_data, u.unlink.dirfd),
266 .in_fd_count = 1,
267 .out_fds_offset = -1,
268 .out_fd_count = 0,
269 .use_cwd_fd = false,
270 },
271 [RUN_AS_RMDIR_RECURSIVE] = {
272 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
273 .in_fd_count = 1,
274 .out_fds_offset = -1,
275 .out_fd_count = 0,
276 .use_cwd_fd = true,
277 },
278 [RUN_AS_RMDIRAT_RECURSIVE] = {
279 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
280 .in_fd_count = 1,
281 .out_fds_offset = -1,
282 .out_fd_count = 0,
283 .use_cwd_fd = false,
284 },
285 [RUN_AS_RMDIR] = {
286 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
287 .in_fd_count = 1,
288 .out_fds_offset = -1,
289 .out_fd_count = 0,
290 .use_cwd_fd = true,
291 },
292 [RUN_AS_RMDIRAT] = {
293 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
294 .in_fd_count = 1,
295 .out_fds_offset = -1,
296 .out_fd_count = 0,
297 .use_cwd_fd = false,
298 },
299 [RUN_AS_RENAME] = {
300 .in_fds_offset = offsetof(struct run_as_data, u.rename.dirfds),
301 .in_fd_count = 2,
302 .out_fds_offset = -1,
303 .out_fd_count = 0,
304 .use_cwd_fd = true,
305 },
306 [RUN_AS_RENAMEAT] = {
307 .in_fds_offset = offsetof(struct run_as_data, u.rename.dirfds),
308 .in_fd_count = 2,
309 .out_fds_offset = -1,
310 .out_fd_count = 0,
311 .use_cwd_fd = false,
312 },
313 [RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET] = {
314 .in_fds_offset = offsetof(struct run_as_data,
315 u.extract_elf_symbol_offset.fd),
316 .in_fd_count = 1,
317 .out_fds_offset = -1,
318 .out_fd_count = 0,
319 .use_cwd_fd = false,
320 },
321 [RUN_AS_EXTRACT_SDT_PROBE_OFFSETS] = {
322 .in_fds_offset = offsetof(struct run_as_data,
323 u.extract_sdt_probe_offsets.fd),
324 .in_fd_count = 1,
325 .out_fds_offset = -1,
326 .out_fd_count = 0,
327 .use_cwd_fd = false,
328 },
c73f064a
JR
329 [RUN_AS_GENERATE_FILTER_BYTECODE] = {
330 .in_fds_offset = -1,
331 .in_fd_count = 0,
332 .out_fds_offset = -1,
333 .out_fd_count = 0,
334 .use_cwd_fd = false,
335 },
df5b86c8
MD
336};
337
7567352f
MD
338struct run_as_worker {
339 pid_t pid; /* Worker PID. */
340 int sockpair[2];
341 char *procname;
342};
343
344/* Single global worker per process (for now). */
345static struct run_as_worker *global_worker;
346/* Lock protecting the worker. */
347static pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER;
348
8f0044bf
MD
349#ifdef VALGRIND
350static
351int use_clone(void)
352{
353 return 0;
354}
355#else
356static
357int use_clone(void)
358{
e8fa9fb0 359 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
8f0044bf
MD
360}
361#endif
362
60b6c79c
MD
363/*
364 * Create recursively directory using the FULL path.
365 */
366static
18710679 367int _mkdirat_recursive(struct run_as_data *data, struct run_as_ret *ret_value)
60b6c79c 368{
60b6c79c 369 const char *path;
60b6c79c 370 mode_t mode;
cbf53d23 371 struct lttng_directory_handle *handle;
60b6c79c 372
93bed9fe
JG
373 path = data->u.mkdir.path;
374 mode = data->u.mkdir.mode;
60b6c79c 375
cbf53d23
JG
376 handle = lttng_directory_handle_create_from_dirfd(data->u.mkdir.dirfd);
377 if (!handle) {
378 ret_value->_errno = errno;
379 ret_value->_error = true;
380 ret_value->u.ret = -1;
381 goto end;
382 }
87bbb856 383 /* Ownership of dirfd is transferred to the handle. */
93bed9fe 384 data->u.mkdir.dirfd = -1;
d77dded2 385 /* Safe to call as we have transitioned to the requested uid/gid. */
cbf53d23
JG
386 ret_value->u.ret = lttng_directory_handle_create_subdirectory_recursive(
387 handle, path, mode);
fe9f7760 388 ret_value->_errno = errno;
93bed9fe 389 ret_value->_error = (ret_value->u.ret) ? true : false;
cbf53d23
JG
390 lttng_directory_handle_put(handle);
391end:
93bed9fe 392 return ret_value->u.ret;
60b6c79c
MD
393}
394
395static
18710679 396int _mkdirat(struct run_as_data *data, struct run_as_ret *ret_value)
60b6c79c 397{
18710679
JG
398 const char *path;
399 mode_t mode;
cbf53d23 400 struct lttng_directory_handle *handle;
18710679 401
93bed9fe
JG
402 path = data->u.mkdir.path;
403 mode = data->u.mkdir.mode;
18710679 404
cbf53d23
JG
405 handle = lttng_directory_handle_create_from_dirfd(data->u.mkdir.dirfd);
406 if (!handle) {
407 ret_value->u.ret = -1;
408 ret_value->_errno = errno;
409 ret_value->_error = true;
410 goto end;
411 }
87bbb856 412 /* Ownership of dirfd is transferred to the handle. */
93bed9fe 413 data->u.mkdir.dirfd = -1;
18710679 414 /* Safe to call as we have transitioned to the requested uid/gid. */
cbf53d23
JG
415 ret_value->u.ret = lttng_directory_handle_create_subdirectory(
416 handle, path, mode);
fe9f7760 417 ret_value->_errno = errno;
93bed9fe 418 ret_value->_error = (ret_value->u.ret) ? true : false;
cbf53d23
JG
419 lttng_directory_handle_put(handle);
420end:
93bed9fe 421 return ret_value->u.ret;
7567352f 422}
7ce36756 423
7567352f 424static
fe9f7760 425int _open(struct run_as_data *data, struct run_as_ret *ret_value)
7567352f 426{
93bed9fe 427 int fd;
cbf53d23 428 struct lttng_directory_handle *handle;
93bed9fe 429
cbf53d23
JG
430 handle = lttng_directory_handle_create_from_dirfd(data->u.open.dirfd);
431 if (!handle) {
432 ret_value->_errno = errno;
433 ret_value->_error = true;
434 ret_value->u.ret = -1;
435 goto end;
436 }
93bed9fe
JG
437 /* Ownership of dirfd is transferred to the handle. */
438 data->u.open.dirfd = -1;
439
cbf53d23 440 fd = lttng_directory_handle_open_file(handle,
93bed9fe
JG
441 data->u.open.path, data->u.open.flags,
442 data->u.open.mode);
443 if (fd < 0) {
444 ret_value->u.ret = -1;
445 ret_value->u.open.fd = -1;
446 } else {
447 ret_value->u.ret = 0;
448 ret_value->u.open.fd = fd;
449 }
450
fe9f7760 451 ret_value->_errno = errno;
93bed9fe 452 ret_value->_error = fd < 0;
cbf53d23
JG
453 lttng_directory_handle_put(handle);
454end:
93bed9fe 455 return ret_value->u.ret;
7567352f
MD
456}
457
458static
fe9f7760 459int _unlink(struct run_as_data *data, struct run_as_ret *ret_value)
7567352f 460{
cbf53d23 461 struct lttng_directory_handle *handle;
93bed9fe 462
cbf53d23
JG
463 handle = lttng_directory_handle_create_from_dirfd(data->u.unlink.dirfd);
464 if (!handle) {
465 ret_value->u.ret = -1;
466 ret_value->_errno = errno;
467 ret_value->_error = true;
468 goto end;
469 }
93bed9fe
JG
470
471 /* Ownership of dirfd is transferred to the handle. */
472 data->u.unlink.dirfd = -1;
473
cbf53d23 474 ret_value->u.ret = lttng_directory_handle_unlink_file(handle,
93bed9fe
JG
475 data->u.unlink.path);
476 ret_value->_errno = errno;
477 ret_value->_error = (ret_value->u.ret) ? true : false;
cbf53d23
JG
478 lttng_directory_handle_put(handle);
479end:
93bed9fe
JG
480 return ret_value->u.ret;
481}
482
483static
484int _rmdir(struct run_as_data *data, struct run_as_ret *ret_value)
485{
cbf53d23 486 struct lttng_directory_handle *handle;
93bed9fe 487
cbf53d23
JG
488 handle = lttng_directory_handle_create_from_dirfd(data->u.rmdir.dirfd);
489 if (!handle) {
490 ret_value->u.ret = -1;
491 ret_value->_errno = errno;
492 ret_value->_error = true;
493 goto end;
494 }
93bed9fe
JG
495
496 /* Ownership of dirfd is transferred to the handle. */
497 data->u.rmdir.dirfd = -1;
498
499 ret_value->u.ret = lttng_directory_handle_remove_subdirectory(
cbf53d23 500 handle, data->u.rmdir.path);
fe9f7760 501 ret_value->_errno = errno;
93bed9fe 502 ret_value->_error = (ret_value->u.ret) ? true : false;
cbf53d23
JG
503 lttng_directory_handle_put(handle);
504end:
93bed9fe 505 return ret_value->u.ret;
60b6c79c
MD
506}
507
508static
fe9f7760 509int _rmdir_recursive(struct run_as_data *data, struct run_as_ret *ret_value)
60b6c79c 510{
cbf53d23 511 struct lttng_directory_handle *handle;
93bed9fe 512
cbf53d23
JG
513 handle = lttng_directory_handle_create_from_dirfd(data->u.rmdir.dirfd);
514 if (!handle) {
515 ret_value->u.ret = -1;
516 ret_value->_errno = errno;
517 ret_value->_error = true;
518 goto end;
519 }
93bed9fe
JG
520
521 /* Ownership of dirfd is transferred to the handle. */
522 data->u.rmdir.dirfd = -1;
523
524 ret_value->u.ret = lttng_directory_handle_remove_subdirectory_recursive(
cbf53d23 525 handle, data->u.rmdir.path, data->u.rmdir.flags);
fe9f7760 526 ret_value->_errno = errno;
93bed9fe 527 ret_value->_error = (ret_value->u.ret) ? true : false;
cbf53d23
JG
528 lttng_directory_handle_put(handle);
529end:
93bed9fe
JG
530 return ret_value->u.ret;
531}
532
533static
534int _rename(struct run_as_data *data, struct run_as_ret *ret_value)
535{
536 const char *old_path, *new_path;
cbf53d23 537 struct lttng_directory_handle *old_handle = NULL, *new_handle = NULL;
93bed9fe
JG
538
539 old_path = data->u.rename.old_path;
540 new_path = data->u.rename.new_path;
541
cbf53d23 542 old_handle = lttng_directory_handle_create_from_dirfd(
93bed9fe 543 data->u.rename.dirfds[0]);
cbf53d23
JG
544 if (!old_handle) {
545 ret_value->u.ret = -1;
546 goto end;
547 }
548 new_handle = lttng_directory_handle_create_from_dirfd(
93bed9fe 549 data->u.rename.dirfds[1]);
cbf53d23
JG
550 if (!new_handle) {
551 ret_value->u.ret = -1;
552 goto end;
553 }
93bed9fe
JG
554
555 /* Ownership of dirfds are transferred to the handles. */
556 data->u.rename.dirfds[0] = data->u.rename.dirfds[1] = -1;
557
558 /* Safe to call as we have transitioned to the requested uid/gid. */
559 ret_value->u.ret = lttng_directory_handle_rename(
cbf53d23
JG
560 old_handle, old_path, new_handle, new_path);
561end:
562 lttng_directory_handle_put(old_handle);
563 lttng_directory_handle_put(new_handle);
93bed9fe
JG
564 ret_value->_errno = errno;
565 ret_value->_error = (ret_value->u.ret) ? true : false;
93bed9fe 566 return ret_value->u.ret;
7567352f 567}
df5b86c8 568
b1b34226 569#ifdef HAVE_ELF_H
241e0a5a
FD
570static
571int _extract_elf_symbol_offset(struct run_as_data *data,
572 struct run_as_ret *ret_value)
573{
574 int ret = 0;
65eefd4c 575 uint64_t offset;
241e0a5a 576
93bed9fe
JG
577 ret_value->_error = false;
578 ret = lttng_elf_get_symbol_offset(data->u.extract_elf_symbol_offset.fd,
241e0a5a 579 data->u.extract_elf_symbol_offset.function,
65eefd4c 580 &offset);
241e0a5a
FD
581 if (ret) {
582 DBG("Failed to extract ELF function offset");
583 ret_value->_error = true;
584 }
65eefd4c 585 ret_value->u.extract_elf_symbol_offset.offset = offset;
241e0a5a
FD
586
587 return ret;
588}
589
0ef03255
FD
590static
591int _extract_sdt_probe_offsets(struct run_as_data *data,
592 struct run_as_ret *ret_value)
593{
594 int ret = 0;
595 uint64_t *offsets = NULL;
596 uint32_t num_offset;
597
598 ret_value->_error = false;
599
600 /* On success, this call allocates the offsets paramater. */
93bed9fe
JG
601 ret = lttng_elf_get_sdt_probe_offsets(
602 data->u.extract_sdt_probe_offsets.fd,
0ef03255
FD
603 data->u.extract_sdt_probe_offsets.provider_name,
604 data->u.extract_sdt_probe_offsets.probe_name,
605 &offsets, &num_offset);
606
607 if (ret) {
608 DBG("Failed to extract SDT probe offsets");
609 ret_value->_error = true;
610 goto end;
611 }
612
b8e2fb80 613 if (num_offset <= 0 || num_offset > LTTNG_KERNEL_ABI_MAX_UPROBE_NUM) {
0ef03255
FD
614 DBG("Wrong number of probes.");
615 ret = -1;
616 ret_value->_error = true;
617 goto free_offset;
618 }
619
620 /* Copy the content of the offsets array to the ret struct. */
621 memcpy(ret_value->u.extract_sdt_probe_offsets.offsets,
622 offsets, num_offset * sizeof(uint64_t));
623
624 ret_value->u.extract_sdt_probe_offsets.num_offset = num_offset;
625
626free_offset:
627 free(offsets);
628end:
629 return ret;
630}
b1b34226
MJ
631#else
632static
633int _extract_elf_symbol_offset(struct run_as_data *data,
634 struct run_as_ret *ret_value)
635{
636 ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET");
637 return -1;
638}
639
640static
641int _extract_sdt_probe_offsets(struct run_as_data *data,
642 struct run_as_ret *ret_value)
643{
644 ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS");
645 return -1;
646}
647#endif
241e0a5a 648
c73f064a
JR
649static
650int _generate_filter_bytecode(struct run_as_data *data,
651 struct run_as_ret *ret_value) {
652 int ret = 0;
653 const char *filter_expression = NULL;
654 struct filter_parser_ctx *ctx = NULL;
655
656 ret_value->_error = false;
657
658 filter_expression = data->u.generate_filter_bytecode.filter_expression;
659
660 if (lttng_strnlen(filter_expression, LTTNG_FILTER_MAX_LEN - 1) == LTTNG_FILTER_MAX_LEN - 1) {
661 ret_value->_error = true;
662 ret = -1;
663 goto end;
664 }
665
666 ret = filter_parser_ctx_create_from_filter_expression(filter_expression, &ctx);
667 if (ret < 0) {
668 ret_value->_error = true;
669 ret = -1;
670 goto end;
671 }
672
673 DBG("Size of bytecode generated: %u bytes.",
674 bytecode_get_len(&ctx->bytecode->b));
675
676 /* Copy the lttng_bytecode_filter object to the return structure. */
677 memcpy(ret_value->u.generate_filter_bytecode.bytecode,
678 &ctx->bytecode->b,
679 sizeof(ctx->bytecode->b) +
680 bytecode_get_len(&ctx->bytecode->b));
681
682end:
683 if (ctx) {
684 filter_bytecode_free(ctx);
685 filter_ir_free(ctx);
686 filter_parser_ctx_free(ctx);
687 }
688
689 return ret;
690}
7567352f
MD
691static
692run_as_fct run_as_enum_to_fct(enum run_as_cmd cmd)
693{
694 switch (cmd) {
695 case RUN_AS_MKDIR:
18710679
JG
696 case RUN_AS_MKDIRAT:
697 return _mkdirat;
698 case RUN_AS_MKDIR_RECURSIVE:
699 case RUN_AS_MKDIRAT_RECURSIVE:
700 return _mkdirat_recursive;
7567352f 701 case RUN_AS_OPEN:
2912cead 702 case RUN_AS_OPENAT:
7567352f
MD
703 return _open;
704 case RUN_AS_UNLINK:
2912cead 705 case RUN_AS_UNLINKAT:
7567352f 706 return _unlink;
93bed9fe
JG
707 case RUN_AS_RMDIR:
708 case RUN_AS_RMDIRAT:
709 return _rmdir;
7567352f 710 case RUN_AS_RMDIR_RECURSIVE:
93bed9fe 711 case RUN_AS_RMDIRAT_RECURSIVE:
7567352f 712 return _rmdir_recursive;
93bed9fe
JG
713 case RUN_AS_RENAME:
714 case RUN_AS_RENAMEAT:
715 return _rename;
241e0a5a
FD
716 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
717 return _extract_elf_symbol_offset;
0ef03255
FD
718 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
719 return _extract_sdt_probe_offsets;
c73f064a
JR
720 case RUN_AS_GENERATE_FILTER_BYTECODE:
721 return _generate_filter_bytecode;
7567352f 722 default:
62a7b8ed 723 ERR("Unknown command %d", (int) cmd);
7567352f
MD
724 return NULL;
725 }
60b6c79c
MD
726}
727
4628484a 728static
93bed9fe 729int do_send_fds(int sock, const int *fds, unsigned int fd_count)
4628484a 730{
7567352f 731 ssize_t len;
93bed9fe
JG
732 unsigned int i;
733
734 for (i = 0; i < fd_count; i++) {
735 if (fds[i] < 0) {
a4c64187 736 DBG("Attempt to send invalid file descriptor (fd = %i)",
93bed9fe
JG
737 fds[i]);
738 /* Return 0 as this is not a fatal error. */
739 return 0;
740 }
4a76dfd3 741 }
4628484a 742
4a76dfd3 743 len = lttcomm_send_fds_unix_sock(sock, fds, fd_count);
93bed9fe 744 return len < 0 ? -1 : 0;
4628484a
MD
745}
746
747static
93bed9fe 748int do_recv_fds(int sock, int *fds, unsigned int fd_count)
4628484a 749{
93bed9fe
JG
750 int ret = 0;
751 unsigned int i;
7567352f 752 ssize_t len;
4628484a 753
93bed9fe
JG
754 len = lttcomm_recv_fds_unix_sock(sock, fds, fd_count);
755 if (len == 0) {
756 ret = -1;
757 goto end;
da9ee832 758 } else if (len < 0) {
93bed9fe
JG
759 PERROR("Failed to receive file descriptors from socket");
760 ret = -1;
761 goto end;
ca9eb994
JG
762 }
763
93bed9fe
JG
764 for (i = 0; i < fd_count; i++) {
765 if (fds[i] < 0) {
766 ERR("Invalid file descriptor received from worker (fd = %i)", fds[i]);
767 /* Return 0 as this is not a fatal error. */
768 }
55cb0d6f 769 }
93bed9fe 770end:
55cb0d6f 771 return ret;
4628484a
MD
772}
773
fe9f7760 774static
93bed9fe
JG
775int send_fds_to_worker(const struct run_as_worker *worker,
776 const struct run_as_data *data)
fe9f7760
FD
777{
778 int ret = 0;
93bed9fe 779 unsigned int i;
fe9f7760 780
93bed9fe
JG
781 if (COMMAND_USE_CWD_FD(data) || COMMAND_IN_FD_COUNT(data) == 0) {
782 goto end;
fe9f7760
FD
783 }
784
93bed9fe
JG
785 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
786 if (COMMAND_IN_FDS(data)[i] < 0) {
787 ERR("Refusing to send invalid fd to worker (fd = %i)",
788 COMMAND_IN_FDS(data)[i]);
789 ret = -1;
790 goto end;
791 }
55cb0d6f 792 }
ca9eb994 793
55cb0d6f 794 ret = do_send_fds(worker->sockpair[0], COMMAND_IN_FDS(data),
93bed9fe 795 COMMAND_IN_FD_COUNT(data));
fe9f7760 796 if (ret < 0) {
93bed9fe 797 PERROR("Failed to send file descriptor to run-as worker");
fe9f7760 798 ret = -1;
93bed9fe 799 goto end;
fe9f7760 800 }
93bed9fe 801end:
fe9f7760
FD
802 return ret;
803}
804
805static
93bed9fe
JG
806int send_fds_to_master(struct run_as_worker *worker, enum run_as_cmd cmd,
807 struct run_as_ret *run_as_ret)
fe9f7760 808{
93bed9fe
JG
809 int ret = 0;
810 unsigned int i;
fe9f7760 811
93bed9fe
JG
812 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
813 goto end;
fe9f7760
FD
814 }
815
93bed9fe
JG
816 ret = do_send_fds(worker->sockpair[1], COMMAND_OUT_FDS(cmd, run_as_ret),
817 COMMAND_OUT_FD_COUNT(cmd));
fe9f7760 818 if (ret < 0) {
93bed9fe
JG
819 PERROR("Failed to send file descriptor to master process");
820 goto end;
fe9f7760
FD
821 }
822
93bed9fe 823 for (i = 0; i < COMMAND_OUT_FD_COUNT(cmd); i++) {
a4c64187
FD
824 int fd = COMMAND_OUT_FDS(cmd, run_as_ret)[i];
825 if (fd >= 0) {
826 int ret_close = close(fd);
827
828 if (ret_close < 0) {
829 PERROR("Failed to close result file descriptor (fd = %i)",
830 fd);
831 }
93bed9fe
JG
832 }
833 }
834end:
fe9f7760
FD
835 return ret;
836}
837
838static
93bed9fe
JG
839int recv_fds_from_worker(const struct run_as_worker *worker, enum run_as_cmd cmd,
840 struct run_as_ret *run_as_ret)
fe9f7760
FD
841{
842 int ret = 0;
843
93bed9fe
JG
844 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
845 goto end;
fe9f7760
FD
846 }
847
93bed9fe
JG
848 ret = do_recv_fds(worker->sockpair[0], COMMAND_OUT_FDS(cmd, run_as_ret),
849 COMMAND_OUT_FD_COUNT(cmd));
fe9f7760 850 if (ret < 0) {
93bed9fe 851 PERROR("Failed to receive file descriptor from run-as worker");
fe9f7760
FD
852 ret = -1;
853 }
93bed9fe 854end:
fe9f7760
FD
855 return ret;
856}
857
858static
93bed9fe 859int recv_fds_from_master(struct run_as_worker *worker, struct run_as_data *data)
fe9f7760
FD
860{
861 int ret = 0;
862
93bed9fe
JG
863 if (COMMAND_USE_CWD_FD(data)) {
864 unsigned int i;
865
866 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
867 COMMAND_IN_FDS(data)[i] = AT_FDCWD;
868 }
869 goto end;
fe9f7760
FD
870 }
871
c73f064a
JR
872 if (COMMAND_IN_FD_COUNT(data) == 0) {
873 goto end;
874 }
875
93bed9fe
JG
876 ret = do_recv_fds(worker->sockpair[1], COMMAND_IN_FDS(data),
877 COMMAND_IN_FD_COUNT(data));
fe9f7760 878 if (ret < 0) {
93bed9fe 879 PERROR("Failed to receive file descriptors from master process");
fe9f7760
FD
880 ret = -1;
881 }
93bed9fe 882end:
fe9f7760
FD
883 return ret;
884}
885
886static
93bed9fe 887int cleanup_received_fds(struct run_as_data *data)
fe9f7760 888{
93bed9fe 889 int ret = 0, i;
fe9f7760 890
93bed9fe
JG
891 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
892 if (COMMAND_IN_FDS(data)[i] == -1) {
893 continue;
894 }
895 ret = close(COMMAND_IN_FDS(data)[i]);
896 if (ret) {
897 PERROR("Failed to close file descriptor received fd in run-as worker");
898 goto end;
899 }
fe9f7760 900 }
93bed9fe 901end:
fe9f7760
FD
902 return ret;
903}
0ef03255 904
37ccf8ec
FD
905static int get_user_infos_from_uid(
906 uid_t uid, char **username, gid_t *primary_gid)
907{
908 int ret;
909 char *buf = NULL;
222b6734
FD
910 long raw_get_pw_buf_size;
911 size_t get_pw_buf_size;
37ccf8ec
FD
912 struct passwd pwd;
913 struct passwd *result = NULL;
914
915 /* Fetch the max size for the temporary buffer. */
916 errno = 0;
222b6734
FD
917 raw_get_pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
918 if (raw_get_pw_buf_size < 0) {
37ccf8ec
FD
919 if (errno != 0) {
920 PERROR("Failed to query _SC_GETPW_R_SIZE_MAX");
921 goto error;
922 }
923
924 /* Limit is indeterminate. */
925 WARN("Failed to query _SC_GETPW_R_SIZE_MAX as it is "
926 "indeterminate; falling back to default buffer size");
222b6734 927 raw_get_pw_buf_size = GETPW_BUFFER_FALLBACK_SIZE;
37ccf8ec
FD
928 }
929
222b6734
FD
930 get_pw_buf_size = (size_t) raw_get_pw_buf_size;
931
932 buf = zmalloc(get_pw_buf_size);
37ccf8ec
FD
933 if (buf == NULL) {
934 PERROR("Failed to allocate buffer to get password file entries");
935 goto error;
936 }
937
222b6734 938 ret = getpwuid_r(uid, &pwd, buf, get_pw_buf_size, &result);
37ccf8ec
FD
939 if (ret < 0) {
940 PERROR("Failed to get user information for user: uid = %d",
941 (int) uid);
942 goto error;
943 }
944
945 if (result == NULL) {
946 ERR("Failed to find user information in password entries: uid = %d",
947 (int) uid);
948 ret = -1;
949 goto error;
950 }
951
952 *username = strdup(result->pw_name);
953 if (*username == NULL) {
954 PERROR("Failed to copy user name");
955 goto error;
956 }
957
958 *primary_gid = result->pw_gid;
959
960end:
961 free(buf);
962 return ret;
963error:
964 *username = NULL;
965 *primary_gid = -1;
966 ret = -1;
967 goto end;
968}
969
970static int demote_creds(
971 uid_t prev_uid, gid_t prev_gid, uid_t new_uid, gid_t new_gid)
972{
973 int ret = 0;
974 gid_t primary_gid;
975 char *username = NULL;
976
977 /* Change the group id. */
978 if (prev_gid != new_gid) {
979 ret = setegid(new_gid);
980 if (ret < 0) {
981 PERROR("Failed to set effective group id: new_gid = %d",
982 (int) new_gid);
983 goto end;
984 }
985 }
986
987 /* Change the user id. */
988 if (prev_uid != new_uid) {
989 ret = get_user_infos_from_uid(new_uid, &username, &primary_gid);
990 if (ret < 0) {
991 goto end;
992 }
993
994 /*
995 * Initialize the supplementary group access list.
996 *
997 * This is needed to handle cases where the supplementary groups
998 * of the user the process is demoting-to would give it access
999 * to a given file/folder, but not it's primary group.
1000 *
1001 * e.g
1002 * username: User1
1003 * Primary Group: User1
1004 * Secondary group: Disk, Network
1005 *
1006 * mkdir inside the following directory must work since User1
1007 * is part of the Network group.
1008 *
1009 * drwxrwx--- 2 root Network 4096 Jul 23 17:17 /tmp/my_folder/
1010 *
1011 *
1012 * The order of the following initgroups and seteuid calls is
1013 * important here;
1014 * Only a root process or one with CAP_SETGID capability can
1015 * call the the initgroups() function. We must initialize the
1016 * supplementary groups before we change the effective
1017 * UID to a less-privileged user.
1018 */
1019 ret = initgroups(username, primary_gid);
1020 if (ret < 0) {
1021 PERROR("Failed to init the supplementary group access list: "
1022 "username = `%s`, primary gid = %d", username,
1023 (int) primary_gid);
1024 goto end;
1025 }
1026
1027 ret = seteuid(new_uid);
1028 if (ret < 0) {
1029 PERROR("Failed to set effective user id: new_uid = %d",
1030 (int) new_uid);
1031 goto end;
1032 }
1033 }
1034end:
1035 free(username);
1036 return ret;
1037}
1038
1039static int promote_creds(
1040 uid_t prev_uid, gid_t prev_gid, uid_t new_uid, gid_t new_gid)
1041{
1042 int ret = 0;
1043 gid_t primary_gid;
1044 char *username = NULL;
1045
1046 /* Change the group id. */
1047 if (prev_gid != new_gid) {
1048 ret = setegid(new_gid);
1049 if (ret < 0) {
1050 PERROR("Failed to set effective group id: new_gid = %d",
1051 (int) new_gid);
1052 goto end;
1053 }
1054 }
1055
1056 /* Change the user id. */
1057 if (prev_uid != new_uid) {
1058 ret = get_user_infos_from_uid(new_uid, &username, &primary_gid);
1059 if (ret < 0) {
1060 goto end;
1061 }
1062
1063 /*
1064 * seteuid call must be done before the initgroups call because
1065 * we need to be privileged (CAP_SETGID) to call initgroups().
1066 */
1067 ret = seteuid(new_uid);
1068 if (ret < 0) {
1069 PERROR("Failed to set effective user id: new_uid = %d",
1070 (int) new_uid);
1071 goto end;
1072 }
1073
1074 /*
1075 * Initialize the supplementary group access list.
1076 *
1077 * There is a possibility the groups we set in the following
1078 * initgroups() call are not exactly the same as the ones we
1079 * had when we originally demoted. This can happen if the
1080 * /etc/group file is modified after the runas process is
1081 * forked. This is very unlikely.
1082 */
1083 ret = initgroups(username, primary_gid);
1084 if (ret < 0) {
1085 PERROR("Failed to init the supplementary group access "
1086 "list: username = `%s`, primary gid = %d",
1087 username, (int) primary_gid)
1088 goto end;
1089 }
1090 }
1091end:
1092 free(username);
1093 return ret;
1094}
1095
7567352f
MD
1096/*
1097 * Return < 0 on error, 0 if OK, 1 on hangup.
1098 */
c2b75c49 1099static
7567352f 1100int handle_one_cmd(struct run_as_worker *worker)
c2b75c49 1101{
37ccf8ec 1102 int ret = 0, promote_ret;
55cb0d6f
JG
1103 struct run_as_data data = {};
1104 ssize_t readlen, writelen;
1105 struct run_as_ret sendret = {};
1106 run_as_fct cmd;
45d6ecaa
JG
1107 const uid_t prev_ruid = getuid();
1108 const gid_t prev_rgid = getgid();
7567352f 1109
fe9f7760
FD
1110 /*
1111 * Stage 1: Receive run_as_data struct from the master.
1112 * The structure contains the command type and all the parameters needed for
1113 * its execution
1114 */
7567352f
MD
1115 readlen = lttcomm_recv_unix_sock(worker->sockpair[1], &data,
1116 sizeof(data));
1117 if (readlen == 0) {
1118 /* hang up */
1119 ret = 1;
1120 goto end;
1121 }
1122 if (readlen < sizeof(data)) {
1123 PERROR("lttcomm_recv_unix_sock error");
1124 ret = -1;
1125 goto end;
1126 }
c2b75c49 1127
7567352f
MD
1128 cmd = run_as_enum_to_fct(data.cmd);
1129 if (!cmd) {
1130 ret = -1;
1131 goto end;
1132 }
1133
fe9f7760
FD
1134 /*
1135 * Stage 2: Receive file descriptor from master.
1136 * Some commands need a file descriptor as input so if it's needed we
1137 * receive the fd using the Unix socket.
1138 */
93bed9fe 1139 ret = recv_fds_from_master(worker, &data);
fe9f7760
FD
1140 if (ret < 0) {
1141 PERROR("recv_fd_from_master error");
1142 ret = -1;
1143 goto end;
1144 }
1145
37ccf8ec
FD
1146 ret = demote_creds(prev_ruid, prev_rgid, data.uid, data.gid);
1147 if (ret < 0) {
1148 goto write_return;
c2b75c49 1149 }
fe9f7760 1150
c2b75c49
MD
1151 /*
1152 * Also set umask to 0 for mkdir executable bit.
1153 */
1154 umask(0);
fe9f7760
FD
1155
1156 /*
1157 * Stage 3: Execute the command
1158 */
1159 ret = (*cmd)(&data, &sendret);
1160 if (ret < 0) {
1161 DBG("Execution of command returned an error");
1162 }
6d73c4ef
MD
1163
1164write_return:
93bed9fe 1165 ret = cleanup_received_fds(&data);
fe9f7760
FD
1166 if (ret < 0) {
1167 ERR("Error cleaning up FD");
37ccf8ec 1168 goto promote_back;
fe9f7760
FD
1169 }
1170
1171 /*
1172 * Stage 4: Send run_as_ret structure to the master.
1173 * This structure contain the return value of the command and the errno.
1174 */
7567352f
MD
1175 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
1176 sizeof(sendret));
6cd525e8 1177 if (writelen < sizeof(sendret)) {
7567352f
MD
1178 PERROR("lttcomm_send_unix_sock error");
1179 ret = -1;
37ccf8ec 1180 goto promote_back;
7567352f 1181 }
fe9f7760
FD
1182
1183 /*
93bed9fe 1184 * Stage 5: Send resulting file descriptors to the master.
fe9f7760 1185 */
93bed9fe 1186 ret = send_fds_to_master(worker, data.cmd, &sendret);
fe9f7760
FD
1187 if (ret < 0) {
1188 DBG("Sending FD to master returned an error");
7567352f 1189 }
fe9f7760 1190
7567352f 1191 ret = 0;
37ccf8ec
FD
1192
1193promote_back:
1194 /* Return to previous uid/gid. */
1195 promote_ret = promote_creds(data.uid, data.gid, prev_ruid, prev_rgid);
1196 if (promote_ret < 0) {
1197 ERR("Failed to promote back to the initial credentials");
1198 }
1199
7567352f
MD
1200end:
1201 return ret;
1202}
1203
1204static
1205int run_as_worker(struct run_as_worker *worker)
1206{
1207 int ret;
1208 ssize_t writelen;
1209 struct run_as_ret sendret;
1210 size_t proc_orig_len;
1211
1212 /*
1213 * Initialize worker. Set a different process cmdline.
1214 */
1215 proc_orig_len = strlen(worker->procname);
1216 memset(worker->procname, 0, proc_orig_len);
1217 strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len);
1218
cb8d0d24 1219 ret = lttng_thread_setname(DEFAULT_RUN_AS_WORKER_NAME);
e1055edb 1220 if (ret && ret != -ENOSYS) {
b8090274 1221 /* Don't fail as this is not essential. */
cb8d0d24 1222 DBG("Failed to set pthread name attribute");
6cd525e8 1223 }
7567352f 1224
fe9f7760
FD
1225 memset(&sendret, 0, sizeof(sendret));
1226
7567352f
MD
1227 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
1228 sizeof(sendret));
1229 if (writelen < sizeof(sendret)) {
1230 PERROR("lttcomm_send_unix_sock error");
1231 ret = EXIT_FAILURE;
1232 goto end;
1233 }
1234
1235 for (;;) {
1236 ret = handle_one_cmd(worker);
1237 if (ret < 0) {
1238 ret = EXIT_FAILURE;
1239 goto end;
1240 } else if (ret > 0) {
1241 break;
1242 } else {
1243 continue; /* Next command. */
1244 }
1245 }
1246 ret = EXIT_SUCCESS;
1247end:
1248 return ret;
c2b75c49
MD
1249}
1250
60b6c79c 1251static
7567352f
MD
1252int run_as_cmd(struct run_as_worker *worker,
1253 enum run_as_cmd cmd,
1254 struct run_as_data *data,
fe9f7760 1255 struct run_as_ret *ret_value,
7567352f 1256 uid_t uid, gid_t gid)
60b6c79c 1257{
fe9f7760 1258 int ret = 0;
7567352f 1259 ssize_t readlen, writelen;
60b6c79c
MD
1260
1261 /*
1262 * If we are non-root, we can only deal with our own uid.
1263 */
1264 if (geteuid() != 0) {
1265 if (uid != geteuid()) {
fe9f7760
FD
1266 ret = -1;
1267 ret_value->_errno = EPERM;
60b6c79c 1268 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
08797918 1269 (int) uid, (int) geteuid());
df5b86c8 1270 goto end;
60b6c79c 1271 }
60b6c79c
MD
1272 }
1273
7567352f
MD
1274 data->cmd = cmd;
1275 data->uid = uid;
1276 data->gid = gid;
1277
fe9f7760
FD
1278 /*
1279 * Stage 1: Send the run_as_data struct to the worker process
1280 */
7567352f
MD
1281 writelen = lttcomm_send_unix_sock(worker->sockpair[0], data,
1282 sizeof(*data));
1283 if (writelen < sizeof(*data)) {
1284 PERROR("Error writing message to run_as");
fe9f7760
FD
1285 ret = -1;
1286 ret_value->_errno = EIO;
60b6c79c 1287 goto end;
c2b75c49 1288 }
7567352f 1289
fe9f7760
FD
1290 /*
1291 * Stage 2: Send file descriptor to the worker process if needed
1292 */
93bed9fe 1293 ret = send_fds_to_worker(worker, data);
fe9f7760
FD
1294 if (ret) {
1295 PERROR("do_send_fd error");
1296 ret = -1;
1297 ret_value->_errno = EIO;
1298 goto end;
1299 }
1300
1301 /*
1302 * Stage 3: Wait for the execution of the command
1303 */
1304
1305 /*
1306 * Stage 4: Receive the run_as_ret struct containing the return value and
1307 * errno
1308 */
1309 readlen = lttcomm_recv_unix_sock(worker->sockpair[0], ret_value,
1310 sizeof(*ret_value));
da9ee832
JG
1311 if (!readlen) {
1312 ERR("Run-as worker has hung-up during run_as_cmd");
fe9f7760
FD
1313 ret = -1;
1314 ret_value->_errno = EIO;
da9ee832 1315 goto end;
fe9f7760 1316 } else if (readlen < sizeof(*ret_value)) {
7567352f 1317 PERROR("Error reading response from run_as");
fe9f7760
FD
1318 ret = -1;
1319 ret_value->_errno = errno;
033b58a7 1320 goto end;
6cd525e8 1321 }
fe9f7760 1322
ca9eb994
JG
1323 if (ret_value->_error) {
1324 /* Skip stage 5 on error as there will be no fd to receive. */
1325 goto end;
1326 }
1327
fe9f7760
FD
1328 /*
1329 * Stage 5: Receive file descriptor if needed
1330 */
93bed9fe 1331 ret = recv_fds_from_worker(worker, cmd, ret_value);
fe9f7760
FD
1332 if (ret < 0) {
1333 ERR("Error receiving fd");
1334 ret = -1;
1335 ret_value->_errno = EIO;
4c462e79 1336 }
7567352f 1337
60b6c79c 1338end:
fe9f7760 1339 return ret;
60b6c79c
MD
1340}
1341
2d85a600 1342/*
7567352f 1343 * This is for debugging ONLY and should not be considered secure.
2d85a600
MD
1344 */
1345static
fe9f7760
FD
1346int run_as_noworker(enum run_as_cmd cmd,
1347 struct run_as_data *data, struct run_as_ret *ret_value,
1348 uid_t uid, gid_t gid)
2d85a600 1349{
df5b86c8 1350 int ret, saved_errno;
5b73926f 1351 mode_t old_mask;
7567352f 1352 run_as_fct fct;
5b73926f 1353
7567352f
MD
1354 fct = run_as_enum_to_fct(cmd);
1355 if (!fct) {
1356 errno = -ENOSYS;
1357 ret = -1;
1358 goto end;
1359 }
5b73926f 1360 old_mask = umask(0);
fe9f7760
FD
1361 ret = fct(data, ret_value);
1362 saved_errno = ret_value->_errno;
5b73926f 1363 umask(old_mask);
df5b86c8 1364 errno = saved_errno;
7567352f 1365end:
5b73926f 1366 return ret;
2d85a600
MD
1367}
1368
8fec83ea
JG
1369static
1370int reset_sighandler(void)
1371{
1372 int sig;
1373
1374 DBG("Resetting run_as worker signal handlers to default");
1375 for (sig = 1; sig <= 31; sig++) {
1376 (void) signal(sig, SIG_DFL);
1377 }
1378 return 0;
1379}
1380
1381static
1382void worker_sighandler(int sig)
1383{
1384 const char *signame;
1385
1386 /*
1387 * The worker will inherit its parent's signals since they are part of
1388 * the same process group. However, in the case of SIGINT and SIGTERM,
1389 * we want to give the worker a chance to teardown gracefully when its
1390 * parent closes the command socket.
1391 */
1392 switch (sig) {
1393 case SIGINT:
1394 signame = "SIGINT";
1395 break;
1396 case SIGTERM:
1397 signame = "SIGTERM";
1398 break;
1399 default:
1400 signame = NULL;
1401 }
1402
1403 if (signame) {
1404 DBG("run_as worker received signal %s", signame);
1405 } else {
1406 DBG("run_as_worker received signal %d", sig);
1407 }
1408}
1409
1410static
1411int set_worker_sighandlers(void)
1412{
1413 int ret = 0;
1414 sigset_t sigset;
1415 struct sigaction sa;
1416
1417 if ((ret = sigemptyset(&sigset)) < 0) {
1418 PERROR("sigemptyset");
1419 goto end;
1420 }
1421
1422 sa.sa_handler = worker_sighandler;
1423 sa.sa_mask = sigset;
1424 sa.sa_flags = 0;
1425 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
1426 PERROR("sigaction SIGINT");
1427 goto end;
1428 }
1429
1430 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
1431 PERROR("sigaction SIGTERM");
1432 goto end;
1433 }
1434
1435 DBG("run_as signal handler set for SIGTERM and SIGINT");
1436end:
1437 return ret;
1438}
1439
1440static
929f71ec
JG
1441int run_as_create_worker_no_lock(const char *procname,
1442 post_fork_cleanup_cb clean_up_func,
1443 void *clean_up_user_data)
8fec83ea
JG
1444{
1445 pid_t pid;
1446 int i, ret = 0;
1447 ssize_t readlen;
1448 struct run_as_ret recvret;
1449 struct run_as_worker *worker;
1450
1451 assert(!global_worker);
1452 if (!use_clone()) {
1453 /*
1454 * Don't initialize a worker, all run_as tasks will be performed
1455 * in the current process.
1456 */
1457 ret = 0;
1458 goto end;
1459 }
1460 worker = zmalloc(sizeof(*worker));
1461 if (!worker) {
1462 ret = -ENOMEM;
1463 goto end;
1464 }
1465 worker->procname = strdup(procname);
1466 if (!worker->procname) {
1467 ret = -ENOMEM;
8c96eded 1468 goto error_procname_alloc;
8fec83ea
JG
1469 }
1470 /* Create unix socket. */
1471 if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) {
1472 ret = -1;
1473 goto error_sock;
1474 }
1475
1476 /* Fork worker. */
1477 pid = fork();
1478 if (pid < 0) {
1479 PERROR("fork");
1480 ret = -1;
1481 goto error_fork;
1482 } else if (pid == 0) {
1483 /* Child */
1484
1485 reset_sighandler();
1486
1487 set_worker_sighandlers();
c989e0d4
FD
1488
1489 logger_set_thread_name("Run-as worker", true);
1490
929f71ec
JG
1491 if (clean_up_func) {
1492 if (clean_up_func(clean_up_user_data) < 0) {
1493 ERR("Run-as post-fork clean-up failed, exiting.");
1494 exit(EXIT_FAILURE);
1495 }
1496 }
8fec83ea
JG
1497
1498 /* Just close, no shutdown. */
1499 if (close(worker->sockpair[0])) {
1500 PERROR("close");
1501 exit(EXIT_FAILURE);
1502 }
1503
1504 /*
1505 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
1506 * Sockpair[1] is used as a control channel with the master
1507 */
1508 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
1509 if (i != worker->sockpair[1]) {
1510 (void) close(i);
1511 }
1512 }
1513
1514 worker->sockpair[0] = -1;
1515 ret = run_as_worker(worker);
1516 if (lttcomm_close_unix_sock(worker->sockpair[1])) {
1517 PERROR("close");
1518 ret = -1;
1519 }
1520 worker->sockpair[1] = -1;
55cb0d6f 1521 free(worker->procname);
340cf672 1522 free(worker);
8fec83ea
JG
1523 LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret);
1524 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
1525 } else {
1526 /* Parent */
1527
1528 /* Just close, no shutdown. */
1529 if (close(worker->sockpair[1])) {
1530 PERROR("close");
1531 ret = -1;
1532 goto error_fork;
1533 }
1534 worker->sockpair[1] = -1;
1535 worker->pid = pid;
1536 /* Wait for worker to become ready. */
1537 readlen = lttcomm_recv_unix_sock(worker->sockpair[0],
1538 &recvret, sizeof(recvret));
1539 if (readlen < sizeof(recvret)) {
1540 ERR("readlen: %zd", readlen);
1541 PERROR("Error reading response from run_as at creation");
1542 ret = -1;
1543 goto error_fork;
1544 }
1545 global_worker = worker;
1546 }
1547end:
1548 return ret;
1549
1550 /* Error handling. */
1551error_fork:
1552 for (i = 0; i < 2; i++) {
1553 if (worker->sockpair[i] < 0) {
1554 continue;
1555 }
1556 if (lttcomm_close_unix_sock(worker->sockpair[i])) {
1557 PERROR("close");
1558 }
1559 worker->sockpair[i] = -1;
1560 }
1561error_sock:
8c96eded
FD
1562 free(worker->procname);
1563error_procname_alloc:
8fec83ea
JG
1564 free(worker);
1565 return ret;
1566}
1567
a01c682b
JR
1568static
1569void run_as_destroy_worker_no_lock(void)
1570{
1571 struct run_as_worker *worker = global_worker;
1572
1573 DBG("Destroying run_as worker");
1574 if (!worker) {
1575 return;
1576 }
1577 /* Close unix socket */
1578 DBG("Closing run_as worker socket");
1579 if (lttcomm_close_unix_sock(worker->sockpair[0])) {
1580 PERROR("close");
1581 }
1582 worker->sockpair[0] = -1;
1583 /* Wait for worker. */
1584 for (;;) {
1585 int status;
1586 pid_t wait_ret;
1587
1588 wait_ret = waitpid(worker->pid, &status, 0);
1589 if (wait_ret < 0) {
1590 if (errno == EINTR) {
1591 continue;
1592 }
1593 PERROR("waitpid");
1594 break;
1595 }
1596
1597 if (WIFEXITED(status)) {
1598 LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR,
1599 DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d",
55cb0d6f 1600 WEXITSTATUS(status));
a01c682b
JR
1601 break;
1602 } else if (WIFSIGNALED(status)) {
1603 ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d",
1604 WTERMSIG(status));
1605 break;
1606 }
1607 }
1608 free(worker->procname);
1609 free(worker);
1610 global_worker = NULL;
1611}
1612
2d85a600 1613static
fe9f7760 1614int run_as_restart_worker(struct run_as_worker *worker)
2d85a600 1615{
fe9f7760
FD
1616 int ret = 0;
1617 char *procname = NULL;
1618
1619 procname = worker->procname;
1620
1621 /* Close socket to run_as worker process and clean up the zombie process */
a01c682b 1622 run_as_destroy_worker_no_lock();
fe9f7760
FD
1623
1624 /* Create a new run_as worker process*/
929f71ec 1625 ret = run_as_create_worker_no_lock(procname, NULL, NULL);
fe9f7760
FD
1626 if (ret < 0 ) {
1627 ERR("Restarting the worker process failed");
1628 ret = -1;
1629 goto err;
1630 }
1631err:
1632 return ret;
1633}
1634
1635static
1636int run_as(enum run_as_cmd cmd, struct run_as_data *data,
1637 struct run_as_ret *ret_value, uid_t uid, gid_t gid)
1638{
1639 int ret, saved_errno;
7567352f 1640
8fec83ea 1641 pthread_mutex_lock(&worker_lock);
749b7a0c 1642 if (use_clone()) {
7567352f 1643 DBG("Using run_as worker");
8fec83ea 1644
749b7a0c 1645 assert(global_worker);
749b7a0c 1646
fe9f7760
FD
1647 ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid);
1648 saved_errno = ret_value->_errno;
1649
fe9f7760
FD
1650 /*
1651 * If the worker thread crashed the errno is set to EIO. we log
1652 * the error and start a new worker process.
1653 */
1654 if (ret == -1 && saved_errno == EIO) {
1655 DBG("Socket closed unexpectedly... "
1656 "Restarting the worker process");
1657 ret = run_as_restart_worker(global_worker);
fe9f7760
FD
1658 if (ret == -1) {
1659 ERR("Failed to restart worker process.");
1660 goto err;
1661 }
1662 }
2d85a600 1663 } else {
7567352f 1664 DBG("Using run_as without worker");
fe9f7760 1665 ret = run_as_noworker(cmd, data, ret_value, uid, gid);
2d85a600 1666 }
fe9f7760 1667err:
8fec83ea 1668 pthread_mutex_unlock(&worker_lock);
7567352f 1669 return ret;
2d85a600
MD
1670}
1671
90e535ef 1672LTTNG_HIDDEN
e11d277b 1673int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 1674{
18710679
JG
1675 return run_as_mkdirat_recursive(AT_FDCWD, path, mode, uid, gid);
1676}
1677
1678LTTNG_HIDDEN
1679int run_as_mkdirat_recursive(int dirfd, const char *path, mode_t mode,
1680 uid_t uid, gid_t gid)
1681{
1682 int ret;
93bed9fe
JG
1683 struct run_as_data data = {};
1684 struct run_as_ret run_as_ret = {};
60b6c79c 1685
18710679
JG
1686 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1687 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1688 path, (int) mode, (int) uid, (int) gid);
93bed9fe
JG
1689 ret = lttng_strncpy(data.u.mkdir.path, path,
1690 sizeof(data.u.mkdir.path));
18710679
JG
1691 if (ret) {
1692 ERR("Failed to copy path argument of mkdirat recursive command");
1693 goto error;
1694 }
93bed9fe
JG
1695 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1696 data.u.mkdir.mode = mode;
1697 data.u.mkdir.dirfd = dirfd;
18710679
JG
1698 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR_RECURSIVE : RUN_AS_MKDIRAT_RECURSIVE,
1699 &data, &run_as_ret, uid, gid);
1700 errno = run_as_ret._errno;
93bed9fe 1701 ret = run_as_ret.u.ret;
18710679
JG
1702error:
1703 return ret;
60b6c79c
MD
1704}
1705
90e535ef 1706LTTNG_HIDDEN
e11d277b 1707int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 1708{
18710679
JG
1709 return run_as_mkdirat(AT_FDCWD, path, mode, uid, gid);
1710}
1711
1712LTTNG_HIDDEN
1713int run_as_mkdirat(int dirfd, const char *path, mode_t mode,
1714 uid_t uid, gid_t gid)
1715{
1716 int ret;
93bed9fe
JG
1717 struct run_as_data data = {};
1718 struct run_as_ret run_as_ret = {};
fe9f7760 1719
18710679
JG
1720 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1721 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1722 path, (int) mode, (int) uid, (int) gid);
93bed9fe
JG
1723 ret = lttng_strncpy(data.u.mkdir.path, path,
1724 sizeof(data.u.mkdir.path));
18710679
JG
1725 if (ret) {
1726 ERR("Failed to copy path argument of mkdirat command");
1727 goto error;
1728 }
93bed9fe
JG
1729 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1730 data.u.mkdir.mode = mode;
1731 data.u.mkdir.dirfd = dirfd;
18710679
JG
1732 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR : RUN_AS_MKDIRAT,
1733 &data, &run_as_ret, uid, gid);
1734 errno = run_as_ret._errno;
93bed9fe 1735 ret = run_as_ret.u.ret;
18710679
JG
1736error:
1737 return ret;
60b6c79c
MD
1738}
1739
90e535ef 1740LTTNG_HIDDEN
2912cead 1741int run_as_open(const char *path, int flags, mode_t mode, uid_t uid,
55cb0d6f 1742 gid_t gid)
2912cead
JG
1743{
1744 return run_as_openat(AT_FDCWD, path, flags, mode, uid, gid);
1745}
1746
1747LTTNG_HIDDEN
1748int run_as_openat(int dirfd, const char *path, int flags, mode_t mode,
1749 uid_t uid, gid_t gid)
60b6c79c 1750{
93bed9fe 1751 int ret;
55cb0d6f
JG
1752 struct run_as_data data = {};
1753 struct run_as_ret run_as_ret = {};
fe9f7760 1754
2912cead
JG
1755 DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
1756 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1757 path, flags, (int) mode, (int) uid, (int) gid);
93bed9fe
JG
1758 ret = lttng_strncpy(data.u.open.path, path, sizeof(data.u.open.path));
1759 if (ret) {
1760 ERR("Failed to copy path argument of open command");
1761 goto error;
1762 }
7567352f
MD
1763 data.u.open.flags = flags;
1764 data.u.open.mode = mode;
93bed9fe 1765 data.u.open.dirfd = dirfd;
2912cead 1766 run_as(dirfd == AT_FDCWD ? RUN_AS_OPEN : RUN_AS_OPENAT,
93bed9fe
JG
1767 &data, &run_as_ret, uid, gid);
1768 errno = run_as_ret._errno;
1769 ret = run_as_ret.u.ret < 0 ? run_as_ret.u.ret :
1770 run_as_ret.u.open.fd;
1771error:
1772 return ret;
60b6c79c 1773}
4628484a
MD
1774
1775LTTNG_HIDDEN
1776int run_as_unlink(const char *path, uid_t uid, gid_t gid)
2912cead
JG
1777{
1778 return run_as_unlinkat(AT_FDCWD, path, uid, gid);
1779}
1780
1781LTTNG_HIDDEN
1782int run_as_unlinkat(int dirfd, const char *path, uid_t uid, gid_t gid)
4628484a 1783{
93bed9fe
JG
1784 int ret;
1785 struct run_as_data data = {};
1786 struct run_as_ret run_as_ret = {};
fe9f7760 1787
2912cead
JG
1788 DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
1789 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1790 path, (int) uid, (int) gid);
93bed9fe
JG
1791 ret = lttng_strncpy(data.u.unlink.path, path,
1792 sizeof(data.u.unlink.path));
1793 if (ret) {
1794 goto error;
1795 }
1796 data.u.unlink.dirfd = dirfd;
1797 run_as(dirfd == AT_FDCWD ? RUN_AS_UNLINK : RUN_AS_UNLINKAT, &data,
1798 &run_as_ret, uid, gid);
1799 errno = run_as_ret._errno;
1800 ret = run_as_ret.u.ret;
1801error:
1802 return ret;
1803}
1804
1805LTTNG_HIDDEN
1806int run_as_rmdir(const char *path, uid_t uid, gid_t gid)
1807{
1808 return run_as_rmdirat(AT_FDCWD, path, uid, gid);
1809}
1810
1811LTTNG_HIDDEN
1812int run_as_rmdirat(int dirfd, const char *path, uid_t uid, gid_t gid)
1813{
1814 int ret;
1815 struct run_as_data data = {};
1816 struct run_as_ret run_as_ret = {};
1817
1818 DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d",
1819 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1820 path, (int) uid, (int) gid);
1821 ret = lttng_strncpy(data.u.rmdir.path, path,
1822 sizeof(data.u.rmdir.path));
1823 if (ret) {
1824 goto error;
1825 }
1826 data.u.rmdir.dirfd = dirfd;
1827 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR : RUN_AS_RMDIRAT, &data,
1828 &run_as_ret, uid, gid);
1829 errno = run_as_ret._errno;
1830 ret = run_as_ret.u.ret;
1831error:
1832 return ret;
4628484a
MD
1833}
1834
1835LTTNG_HIDDEN
f75c5439 1836int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid, int flags)
4628484a 1837{
f75c5439 1838 return run_as_rmdirat_recursive(AT_FDCWD, path, uid, gid, flags);
93bed9fe 1839}
fe9f7760 1840
93bed9fe 1841LTTNG_HIDDEN
f75c5439 1842int run_as_rmdirat_recursive(int dirfd, const char *path, uid_t uid, gid_t gid, int flags)
93bed9fe
JG
1843{
1844 int ret;
1845 struct run_as_data data = {};
1846 struct run_as_ret run_as_ret = {};
4628484a 1847
93bed9fe
JG
1848 DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d",
1849 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1850 path, (int) uid, (int) gid);
93bed9fe
JG
1851 ret = lttng_strncpy(data.u.rmdir.path, path,
1852 sizeof(data.u.rmdir.path));
1853 if (ret) {
1854 goto error;
1855 }
1856 data.u.rmdir.dirfd = dirfd;
f75c5439 1857 data.u.rmdir.flags = flags;
93bed9fe
JG
1858 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR_RECURSIVE : RUN_AS_RMDIRAT_RECURSIVE,
1859 &data, &run_as_ret, uid, gid);
1860 errno = run_as_ret._errno;
1861 ret = run_as_ret.u.ret;
1862error:
1863 return ret;
1864}
1865
1866LTTNG_HIDDEN
1867int run_as_rename(const char *old, const char *new, uid_t uid, gid_t gid)
1868{
1869 return run_as_renameat(AT_FDCWD, old, AT_FDCWD, new, uid, gid);
1870}
1871
1872LTTNG_HIDDEN
1873int run_as_renameat(int old_dirfd, const char *old_name,
1874 int new_dirfd, const char *new_name, uid_t uid, gid_t gid)
1875{
1876 int ret;
1877 struct run_as_data data = {};
1878 struct run_as_ret run_as_ret = {};
1879
1880 DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d",
1881 old_dirfd, old_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1882 old_name,
1883 new_dirfd, new_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1884 new_name, (int) uid, (int) gid);
1885 ret = lttng_strncpy(data.u.rename.old_path, old_name,
1886 sizeof(data.u.rename.old_path));
1887 if (ret) {
1888 goto error;
1889 }
1890 ret = lttng_strncpy(data.u.rename.new_path, new_name,
1891 sizeof(data.u.rename.new_path));
1892 if (ret) {
1893 goto error;
1894 }
1895
1896 data.u.rename.dirfds[0] = old_dirfd;
1897 data.u.rename.dirfds[1] = new_dirfd;
1898 run_as(old_dirfd == AT_FDCWD && new_dirfd == AT_FDCWD ?
1899 RUN_AS_RENAME : RUN_AS_RENAMEAT,
1900 &data, &run_as_ret, uid, gid);
1901 errno = run_as_ret._errno;
1902 ret = run_as_ret.u.ret;
1903error:
1904 return ret;
7567352f
MD
1905}
1906
241e0a5a
FD
1907LTTNG_HIDDEN
1908int run_as_extract_elf_symbol_offset(int fd, const char* function,
1909 uid_t uid, gid_t gid, uint64_t *offset)
1910{
93bed9fe
JG
1911 int ret;
1912 struct run_as_data data = {};
55cb0d6f 1913 struct run_as_ret run_as_ret = {};
f726677b 1914
241e0a5a 1915 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
93bed9fe
JG
1916 "with for uid %d and gid %d", fd, function,
1917 (int) uid, (int) gid);
241e0a5a 1918
93bed9fe 1919 data.u.extract_elf_symbol_offset.fd = fd;
241e0a5a
FD
1920
1921 strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1);
241e0a5a 1922 data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
93bed9fe
JG
1923 ret = lttng_strncpy(data.u.extract_elf_symbol_offset.function,
1924 function,
1925 sizeof(data.u.extract_elf_symbol_offset.function));
1926 if (ret) {
1927 goto error;
1928 }
241e0a5a 1929
93bed9fe
JG
1930 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &run_as_ret, uid, gid);
1931 errno = run_as_ret._errno;
1932 if (run_as_ret._error) {
1933 ret = -1;
1934 goto error;
241e0a5a
FD
1935 }
1936
93bed9fe
JG
1937 *offset = run_as_ret.u.extract_elf_symbol_offset.offset;
1938error:
1939 return ret;
241e0a5a
FD
1940}
1941
0ef03255
FD
1942LTTNG_HIDDEN
1943int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name,
1944 const char* probe_name, uid_t uid, gid_t gid,
1945 uint64_t **offsets, uint32_t *num_offset)
1946{
93bed9fe
JG
1947 int ret;
1948 struct run_as_data data = {};
1949 struct run_as_ret run_as_ret = {};
f726677b 1950
0ef03255 1951 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
93bed9fe
JG
1952 "provider_name=%s with for uid %d and gid %d", fd,
1953 probe_name, provider_name, (int) uid, (int) gid);
0ef03255 1954
93bed9fe 1955 data.u.extract_sdt_probe_offsets.fd = fd;
0ef03255 1956
93bed9fe
JG
1957 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name,
1958 sizeof(data.u.extract_sdt_probe_offsets.probe_name));
1959 if (ret) {
1960 goto error;
1961 }
1962 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.provider_name,
1963 provider_name,
1964 sizeof(data.u.extract_sdt_probe_offsets.provider_name));
1965 if (ret) {
1966 goto error;
0ef03255
FD
1967 }
1968
93bed9fe
JG
1969 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &run_as_ret, uid, gid);
1970 errno = run_as_ret._errno;
1971 if (run_as_ret._error) {
1972 ret = -1;
1973 goto error;
1974 }
0ef03255 1975
93bed9fe 1976 *num_offset = run_as_ret.u.extract_sdt_probe_offsets.num_offset;
0ef03255
FD
1977 *offsets = zmalloc(*num_offset * sizeof(uint64_t));
1978 if (!*offsets) {
93bed9fe
JG
1979 ret = -ENOMEM;
1980 goto error;
0ef03255
FD
1981 }
1982
93bed9fe
JG
1983 memcpy(*offsets, run_as_ret.u.extract_sdt_probe_offsets.offsets,
1984 *num_offset * sizeof(uint64_t));
1985error:
1986 return ret;
0ef03255
FD
1987}
1988
c73f064a
JR
1989LTTNG_HIDDEN
1990int run_as_generate_filter_bytecode(const char *filter_expression,
58daac01 1991 const struct lttng_credentials *creds,
2b00d462 1992 struct lttng_bytecode **bytecode)
c73f064a
JR
1993{
1994 int ret;
1995 struct run_as_data data = {};
1996 struct run_as_ret run_as_ret = {};
2b00d462
SM
1997 const struct lttng_bytecode *view_bytecode = NULL;
1998 struct lttng_bytecode *local_bytecode = NULL;
58daac01
JR
1999 const uid_t uid = lttng_credentials_get_uid(creds);
2000 const gid_t gid = lttng_credentials_get_gid(creds);
c73f064a
JR
2001
2002 DBG3("generate_filter_bytecode() from expression=\"%s\" for uid %d and gid %d",
2003 filter_expression, (int) uid, (int) gid);
2004
2005 ret = lttng_strncpy(data.u.generate_filter_bytecode.filter_expression, filter_expression,
2006 sizeof(data.u.generate_filter_bytecode.filter_expression));
2007 if (ret) {
2008 goto error;
2009 }
2010
2011 run_as(RUN_AS_GENERATE_FILTER_BYTECODE, &data, &run_as_ret, uid, gid);
2012 errno = run_as_ret._errno;
2013 if (run_as_ret._error) {
2014 ret = -1;
2015 goto error;
2016 }
2017
2b00d462 2018 view_bytecode = (const struct lttng_bytecode *) run_as_ret.u.generate_filter_bytecode.bytecode;
c73f064a
JR
2019
2020 local_bytecode = zmalloc(sizeof(*local_bytecode) + view_bytecode->len);
2021 if (!local_bytecode) {
2022 ret = -ENOMEM;
2023 goto error;
2024 }
2025
2026 memcpy(local_bytecode, run_as_ret.u.generate_filter_bytecode.bytecode,
2027 sizeof(*local_bytecode) + view_bytecode->len);
2028 *bytecode = local_bytecode;
2029error:
2030 return ret;
2031}
2032
7567352f 2033LTTNG_HIDDEN
929f71ec
JG
2034int run_as_create_worker(const char *procname,
2035 post_fork_cleanup_cb clean_up_func,
2036 void *clean_up_user_data)
7567352f 2037{
8fec83ea 2038 int ret;
7567352f 2039
749b7a0c 2040 pthread_mutex_lock(&worker_lock);
929f71ec
JG
2041 ret = run_as_create_worker_no_lock(procname, clean_up_func,
2042 clean_up_user_data);
749b7a0c 2043 pthread_mutex_unlock(&worker_lock);
7567352f
MD
2044 return ret;
2045}
2046
2047LTTNG_HIDDEN
2048void run_as_destroy_worker(void)
2049{
749b7a0c 2050 pthread_mutex_lock(&worker_lock);
a01c682b 2051 run_as_destroy_worker_no_lock();
749b7a0c 2052 pthread_mutex_unlock(&worker_lock);
4628484a 2053}
This page took 0.154125 seconds and 4 git commands to generate.