Fix: relay: relay_rotate_session_streams uninitialized return value
[lttng-tools.git] / src / common / runas.c
CommitLineData
60b6c79c
MD
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
93bed9fe 4 * 2019 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
60b6c79c 5 *
d14d33bf
AM
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
60b6c79c
MD
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d14d33bf 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
60b6c79c
MD
13 * more details.
14 *
d14d33bf
AM
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
60b6c79c
MD
18 */
19
6c1c0768 20#define _LGPL_SOURCE
60b6c79c
MD
21#include <errno.h>
22#include <limits.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/wait.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <unistd.h>
30#include <fcntl.h>
c2b75c49 31#include <sched.h>
0452bf08 32#include <signal.h>
749b7a0c 33#include <assert.h>
e1055edb 34#include <signal.h>
60b6c79c 35
0ef03255 36#include <common/lttng-kernel.h>
90e535ef 37#include <common/common.h>
3fd15a74 38#include <common/utils.h>
e8fa9fb0 39#include <common/compat/getenv.h>
e1055edb 40#include <common/compat/prctl.h>
2038dd6c
JG
41#include <common/unix.h>
42#include <common/defaults.h>
241e0a5a
FD
43#include <common/lttng-elf.h>
44
45#include <lttng/constant.h>
60b6c79c 46
0857097f
DG
47#include "runas.h"
48
7567352f 49struct run_as_data;
fe9f7760
FD
50struct run_as_ret;
51typedef int (*run_as_fct)(struct run_as_data *data, struct run_as_ret *ret_value);
c2b75c49 52
93bed9fe
JG
53enum run_as_cmd {
54 RUN_AS_MKDIR,
55 RUN_AS_MKDIRAT,
56 RUN_AS_MKDIR_RECURSIVE,
57 RUN_AS_MKDIRAT_RECURSIVE,
58 RUN_AS_OPEN,
59 RUN_AS_OPENAT,
60 RUN_AS_UNLINK,
61 RUN_AS_UNLINKAT,
62 RUN_AS_RMDIR,
63 RUN_AS_RMDIRAT,
64 RUN_AS_RMDIR_RECURSIVE,
65 RUN_AS_RMDIRAT_RECURSIVE,
66 RUN_AS_RENAME,
67 RUN_AS_RENAMEAT,
68 RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET,
69 RUN_AS_EXTRACT_SDT_PROBE_OFFSETS,
60b6c79c
MD
70};
71
93bed9fe
JG
72struct run_as_mkdir_data {
73 int dirfd;
74 char path[LTTNG_PATH_MAX];
75 mode_t mode;
76} LTTNG_PACKED;
77
e11d277b 78struct run_as_open_data {
93bed9fe
JG
79 int dirfd;
80 char path[LTTNG_PATH_MAX];
60b6c79c
MD
81 int flags;
82 mode_t mode;
93bed9fe 83} LTTNG_PACKED;
60b6c79c 84
4628484a 85struct run_as_unlink_data {
93bed9fe
JG
86 int dirfd;
87 char path[LTTNG_PATH_MAX];
88} LTTNG_PACKED;
4628484a 89
93bed9fe
JG
90struct run_as_rmdir_data {
91 int dirfd;
92 char path[LTTNG_PATH_MAX];
f75c5439 93 int flags; /* enum lttng_directory_handle_rmdir_recursive_flags */
93bed9fe 94} LTTNG_PACKED;
7567352f 95
241e0a5a 96struct run_as_extract_elf_symbol_offset_data {
93bed9fe 97 int fd;
241e0a5a 98 char function[LTTNG_SYMBOL_NAME_LEN];
93bed9fe 99} LTTNG_PACKED;
241e0a5a 100
0ef03255 101struct run_as_extract_sdt_probe_offsets_data {
93bed9fe 102 int fd;
0ef03255
FD
103 char probe_name[LTTNG_SYMBOL_NAME_LEN];
104 char provider_name[LTTNG_SYMBOL_NAME_LEN];
93bed9fe 105} LTTNG_PACKED;
0ef03255 106
93bed9fe
JG
107struct run_as_rename_data {
108 /*
109 * [0] = old_dirfd
110 * [1] = new_dirfd
111 */
112 int dirfds[2];
113 char old_path[LTTNG_PATH_MAX];
114 char new_path[LTTNG_PATH_MAX];
115} LTTNG_PACKED;
fe9f7760
FD
116
117struct run_as_open_ret {
93bed9fe
JG
118 int fd;
119} LTTNG_PACKED;
fe9f7760 120
241e0a5a
FD
121struct run_as_extract_elf_symbol_offset_ret {
122 uint64_t offset;
93bed9fe 123} LTTNG_PACKED;
241e0a5a 124
0ef03255
FD
125struct run_as_extract_sdt_probe_offsets_ret {
126 uint32_t num_offset;
127 uint64_t offsets[LTTNG_KERNEL_MAX_UPROBE_NUM];
93bed9fe 128} LTTNG_PACKED;
7567352f
MD
129
130struct run_as_data {
131 enum run_as_cmd cmd;
132 union {
93bed9fe 133 struct run_as_mkdir_data mkdir;
7567352f
MD
134 struct run_as_open_data open;
135 struct run_as_unlink_data unlink;
93bed9fe
JG
136 struct run_as_rmdir_data rmdir;
137 struct run_as_rename_data rename;
241e0a5a 138 struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset;
0ef03255 139 struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets;
7567352f
MD
140 } u;
141 uid_t uid;
142 gid_t gid;
93bed9fe 143} LTTNG_PACKED;
4628484a 144
fe9f7760
FD
145/*
146 * The run_as_ret structure holds the returned value and status of the command.
147 *
148 * The `u` union field holds the return value of the command; in most cases it
149 * represents the success or the failure of the command. In more complex
150 * commands, it holds a computed value.
151 *
152 * The _errno field is the errno recorded after the execution of the command.
153 *
154 * The _error fields is used the signify that return status of the command. For
155 * simple commands returning `int` the _error field will be the same as the
156 * ret_int field. In complex commands, it signify the success or failure of the
157 * command.
158 *
159 */
df5b86c8 160struct run_as_ret {
fe9f7760 161 union {
93bed9fe 162 int ret;
fe9f7760 163 struct run_as_open_ret open;
241e0a5a 164 struct run_as_extract_elf_symbol_offset_ret extract_elf_symbol_offset;
0ef03255 165 struct run_as_extract_sdt_probe_offsets_ret extract_sdt_probe_offsets;
fe9f7760 166 } u;
df5b86c8 167 int _errno;
fe9f7760 168 bool _error;
93bed9fe
JG
169} LTTNG_PACKED;
170
171#define COMMAND_IN_FDS(data_ptr) ({ \
172 int *fds = NULL; \
173 if (command_properties[data_ptr->cmd].in_fds_offset != -1) { \
174 fds = (int *) ((char *) data_ptr + command_properties[data_ptr->cmd].in_fds_offset); \
175 } \
176 fds; \
177})
178
179#define COMMAND_OUT_FDS(cmd, ret_ptr) ({ \
180 int *fds = NULL; \
181 if (command_properties[cmd].out_fds_offset != -1) { \
182 fds = (int *) ((char *) ret_ptr + command_properties[cmd].out_fds_offset); \
183 } \
184 fds; \
185})
186
187#define COMMAND_IN_FD_COUNT(data_ptr) ({ \
188 command_properties[data_ptr->cmd].in_fd_count; \
189})
190
191#define COMMAND_OUT_FD_COUNT(cmd) ({ \
192 command_properties[cmd].out_fd_count; \
193})
194
195#define COMMAND_USE_CWD_FD(data_ptr) command_properties[data_ptr->cmd].use_cwd_fd
196
197struct run_as_command_properties {
198 /* Set to -1 when not applicable. */
199 ptrdiff_t in_fds_offset, out_fds_offset;
200 unsigned int in_fd_count, out_fd_count;
201 bool use_cwd_fd;
202};
203
204static const struct run_as_command_properties command_properties[] = {
205 [RUN_AS_MKDIR] = {
206 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
207 .in_fd_count = 1,
208 .out_fds_offset = -1,
209 .out_fd_count = 0,
210 .use_cwd_fd = true,
211 },
212 [RUN_AS_MKDIRAT] = {
213 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
214 .in_fd_count = 1,
215 .out_fds_offset = -1,
216 .out_fd_count = 0,
217 .use_cwd_fd = false,
218 },
219 [RUN_AS_MKDIR_RECURSIVE] = {
220 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
221 .in_fd_count = 1,
222 .out_fds_offset = -1,
223 .out_fd_count = 0,
224 .use_cwd_fd = true,
225 },
226 [RUN_AS_MKDIRAT_RECURSIVE] = {
227 .in_fds_offset = offsetof(struct run_as_data, u.mkdir.dirfd),
228 .in_fd_count = 1,
229 .out_fds_offset = -1,
230 .out_fd_count = 0,
231 .use_cwd_fd = false,
232 },
233 [RUN_AS_OPEN] = {
234 .in_fds_offset = offsetof(struct run_as_data, u.open.dirfd),
235 .in_fd_count = 1,
236 .out_fds_offset = offsetof(struct run_as_ret, u.open.fd),
237 .out_fd_count = 1,
238 .use_cwd_fd = true,
239 },
240 [RUN_AS_OPENAT] = {
241 .in_fds_offset = offsetof(struct run_as_data, u.open.dirfd),
242 .in_fd_count = 1,
243 .out_fds_offset = offsetof(struct run_as_ret, u.open.fd),
244 .out_fd_count = 1,
245 .use_cwd_fd = false,
246 },
247 [RUN_AS_UNLINK] = {
248 .in_fds_offset = offsetof(struct run_as_data, u.unlink.dirfd),
249 .in_fd_count = 1,
250 .out_fds_offset = -1,
251 .out_fd_count = 0,
252 .use_cwd_fd = true,
253 },
254 [RUN_AS_UNLINKAT] = {
255 .in_fds_offset = offsetof(struct run_as_data, u.unlink.dirfd),
256 .in_fd_count = 1,
257 .out_fds_offset = -1,
258 .out_fd_count = 0,
259 .use_cwd_fd = false,
260 },
261 [RUN_AS_RMDIR_RECURSIVE] = {
262 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
263 .in_fd_count = 1,
264 .out_fds_offset = -1,
265 .out_fd_count = 0,
266 .use_cwd_fd = true,
267 },
268 [RUN_AS_RMDIRAT_RECURSIVE] = {
269 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
270 .in_fd_count = 1,
271 .out_fds_offset = -1,
272 .out_fd_count = 0,
273 .use_cwd_fd = false,
274 },
275 [RUN_AS_RMDIR] = {
276 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
277 .in_fd_count = 1,
278 .out_fds_offset = -1,
279 .out_fd_count = 0,
280 .use_cwd_fd = true,
281 },
282 [RUN_AS_RMDIRAT] = {
283 .in_fds_offset = offsetof(struct run_as_data, u.rmdir.dirfd),
284 .in_fd_count = 1,
285 .out_fds_offset = -1,
286 .out_fd_count = 0,
287 .use_cwd_fd = false,
288 },
289 [RUN_AS_RENAME] = {
290 .in_fds_offset = offsetof(struct run_as_data, u.rename.dirfds),
291 .in_fd_count = 2,
292 .out_fds_offset = -1,
293 .out_fd_count = 0,
294 .use_cwd_fd = true,
295 },
296 [RUN_AS_RENAMEAT] = {
297 .in_fds_offset = offsetof(struct run_as_data, u.rename.dirfds),
298 .in_fd_count = 2,
299 .out_fds_offset = -1,
300 .out_fd_count = 0,
301 .use_cwd_fd = false,
302 },
303 [RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET] = {
304 .in_fds_offset = offsetof(struct run_as_data,
305 u.extract_elf_symbol_offset.fd),
306 .in_fd_count = 1,
307 .out_fds_offset = -1,
308 .out_fd_count = 0,
309 .use_cwd_fd = false,
310 },
311 [RUN_AS_EXTRACT_SDT_PROBE_OFFSETS] = {
312 .in_fds_offset = offsetof(struct run_as_data,
313 u.extract_sdt_probe_offsets.fd),
314 .in_fd_count = 1,
315 .out_fds_offset = -1,
316 .out_fd_count = 0,
317 .use_cwd_fd = false,
318 },
df5b86c8
MD
319};
320
7567352f
MD
321struct run_as_worker {
322 pid_t pid; /* Worker PID. */
323 int sockpair[2];
324 char *procname;
325};
326
327/* Single global worker per process (for now). */
328static struct run_as_worker *global_worker;
329/* Lock protecting the worker. */
330static pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER;
331
8f0044bf
MD
332#ifdef VALGRIND
333static
334int use_clone(void)
335{
336 return 0;
337}
338#else
339static
340int use_clone(void)
341{
e8fa9fb0 342 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
8f0044bf
MD
343}
344#endif
345
60b6c79c
MD
346/*
347 * Create recursively directory using the FULL path.
348 */
349static
18710679 350int _mkdirat_recursive(struct run_as_data *data, struct run_as_ret *ret_value)
60b6c79c 351{
60b6c79c 352 const char *path;
60b6c79c 353 mode_t mode;
18710679 354 struct lttng_directory_handle handle;
60b6c79c 355
93bed9fe
JG
356 path = data->u.mkdir.path;
357 mode = data->u.mkdir.mode;
60b6c79c 358
93bed9fe
JG
359 (void) lttng_directory_handle_init_from_dirfd(&handle,
360 data->u.mkdir.dirfd);
87bbb856 361 /* Ownership of dirfd is transferred to the handle. */
93bed9fe 362 data->u.mkdir.dirfd = -1;
d77dded2 363 /* Safe to call as we have transitioned to the requested uid/gid. */
93bed9fe 364 ret_value->u.ret =
18710679
JG
365 lttng_directory_handle_create_subdirectory_recursive(
366 &handle, path, mode);
fe9f7760 367 ret_value->_errno = errno;
93bed9fe 368 ret_value->_error = (ret_value->u.ret) ? true : false;
18710679 369 lttng_directory_handle_fini(&handle);
93bed9fe 370 return ret_value->u.ret;
60b6c79c
MD
371}
372
373static
18710679 374int _mkdirat(struct run_as_data *data, struct run_as_ret *ret_value)
60b6c79c 375{
18710679
JG
376 const char *path;
377 mode_t mode;
378 struct lttng_directory_handle handle;
379
93bed9fe
JG
380 path = data->u.mkdir.path;
381 mode = data->u.mkdir.mode;
18710679 382
93bed9fe
JG
383 (void) lttng_directory_handle_init_from_dirfd(&handle,
384 data->u.mkdir.dirfd);
87bbb856 385 /* Ownership of dirfd is transferred to the handle. */
93bed9fe 386 data->u.mkdir.dirfd = -1;
18710679 387 /* Safe to call as we have transitioned to the requested uid/gid. */
93bed9fe 388 ret_value->u.ret =
18710679
JG
389 lttng_directory_handle_create_subdirectory(
390 &handle, path, mode);
fe9f7760 391 ret_value->_errno = errno;
93bed9fe 392 ret_value->_error = (ret_value->u.ret) ? true : false;
18710679 393 lttng_directory_handle_fini(&handle);
93bed9fe 394 return ret_value->u.ret;
7567352f 395}
7ce36756 396
7567352f 397static
fe9f7760 398int _open(struct run_as_data *data, struct run_as_ret *ret_value)
7567352f 399{
93bed9fe
JG
400 int fd;
401 struct lttng_directory_handle handle;
402
403 (void) lttng_directory_handle_init_from_dirfd(&handle,
404 data->u.open.dirfd);
405 /* Ownership of dirfd is transferred to the handle. */
406 data->u.open.dirfd = -1;
407
408 fd = lttng_directory_handle_open_file(&handle,
409 data->u.open.path, data->u.open.flags,
410 data->u.open.mode);
411 if (fd < 0) {
412 ret_value->u.ret = -1;
413 ret_value->u.open.fd = -1;
414 } else {
415 ret_value->u.ret = 0;
416 ret_value->u.open.fd = fd;
417 }
418
fe9f7760 419 ret_value->_errno = errno;
93bed9fe
JG
420 ret_value->_error = fd < 0;
421 lttng_directory_handle_fini(&handle);
422 return ret_value->u.ret;
7567352f
MD
423}
424
425static
fe9f7760 426int _unlink(struct run_as_data *data, struct run_as_ret *ret_value)
7567352f 427{
93bed9fe
JG
428 struct lttng_directory_handle handle;
429
430 (void) lttng_directory_handle_init_from_dirfd(&handle,
431 data->u.unlink.dirfd);
432
433 /* Ownership of dirfd is transferred to the handle. */
434 data->u.unlink.dirfd = -1;
435
436 ret_value->u.ret = lttng_directory_handle_unlink_file(&handle,
437 data->u.unlink.path);
438 ret_value->_errno = errno;
439 ret_value->_error = (ret_value->u.ret) ? true : false;
440 lttng_directory_handle_fini(&handle);
441 return ret_value->u.ret;
442}
443
444static
445int _rmdir(struct run_as_data *data, struct run_as_ret *ret_value)
446{
447 struct lttng_directory_handle handle;
448
449 (void) lttng_directory_handle_init_from_dirfd(&handle,
450 data->u.rmdir.dirfd);
451
452 /* Ownership of dirfd is transferred to the handle. */
453 data->u.rmdir.dirfd = -1;
454
455 ret_value->u.ret = lttng_directory_handle_remove_subdirectory(
456 &handle, data->u.rmdir.path);
fe9f7760 457 ret_value->_errno = errno;
93bed9fe
JG
458 ret_value->_error = (ret_value->u.ret) ? true : false;
459 lttng_directory_handle_fini(&handle);
460 return ret_value->u.ret;
60b6c79c
MD
461}
462
463static
fe9f7760 464int _rmdir_recursive(struct run_as_data *data, struct run_as_ret *ret_value)
60b6c79c 465{
93bed9fe
JG
466 struct lttng_directory_handle handle;
467
468 (void) lttng_directory_handle_init_from_dirfd(&handle,
469 data->u.rmdir.dirfd);
470
471 /* Ownership of dirfd is transferred to the handle. */
472 data->u.rmdir.dirfd = -1;
473
474 ret_value->u.ret = lttng_directory_handle_remove_subdirectory_recursive(
f75c5439 475 &handle, data->u.rmdir.path, data->u.rmdir.flags);
fe9f7760 476 ret_value->_errno = errno;
93bed9fe
JG
477 ret_value->_error = (ret_value->u.ret) ? true : false;
478 lttng_directory_handle_fini(&handle);
479 return ret_value->u.ret;
480}
481
482static
483int _rename(struct run_as_data *data, struct run_as_ret *ret_value)
484{
485 const char *old_path, *new_path;
486 struct lttng_directory_handle old_handle, new_handle;
487
488 old_path = data->u.rename.old_path;
489 new_path = data->u.rename.new_path;
490
491 (void) lttng_directory_handle_init_from_dirfd(&old_handle,
492 data->u.rename.dirfds[0]);
493 (void) lttng_directory_handle_init_from_dirfd(&new_handle,
494 data->u.rename.dirfds[1]);
495
496 /* Ownership of dirfds are transferred to the handles. */
497 data->u.rename.dirfds[0] = data->u.rename.dirfds[1] = -1;
498
499 /* Safe to call as we have transitioned to the requested uid/gid. */
500 ret_value->u.ret = lttng_directory_handle_rename(
501 &old_handle, old_path, &new_handle, new_path);
502 ret_value->_errno = errno;
503 ret_value->_error = (ret_value->u.ret) ? true : false;
504 lttng_directory_handle_fini(&old_handle);
505 lttng_directory_handle_fini(&new_handle);
506 return ret_value->u.ret;
7567352f 507}
df5b86c8 508
b1b34226 509#ifdef HAVE_ELF_H
241e0a5a
FD
510static
511int _extract_elf_symbol_offset(struct run_as_data *data,
512 struct run_as_ret *ret_value)
513{
514 int ret = 0;
241e0a5a 515
93bed9fe
JG
516 ret_value->_error = false;
517 ret = lttng_elf_get_symbol_offset(data->u.extract_elf_symbol_offset.fd,
241e0a5a
FD
518 data->u.extract_elf_symbol_offset.function,
519 &ret_value->u.extract_elf_symbol_offset.offset);
520 if (ret) {
521 DBG("Failed to extract ELF function offset");
522 ret_value->_error = true;
523 }
524
525 return ret;
526}
527
0ef03255
FD
528static
529int _extract_sdt_probe_offsets(struct run_as_data *data,
530 struct run_as_ret *ret_value)
531{
532 int ret = 0;
533 uint64_t *offsets = NULL;
534 uint32_t num_offset;
535
536 ret_value->_error = false;
537
538 /* On success, this call allocates the offsets paramater. */
93bed9fe
JG
539 ret = lttng_elf_get_sdt_probe_offsets(
540 data->u.extract_sdt_probe_offsets.fd,
0ef03255
FD
541 data->u.extract_sdt_probe_offsets.provider_name,
542 data->u.extract_sdt_probe_offsets.probe_name,
543 &offsets, &num_offset);
544
545 if (ret) {
546 DBG("Failed to extract SDT probe offsets");
547 ret_value->_error = true;
548 goto end;
549 }
550
551 if (num_offset <= 0 || num_offset > LTTNG_KERNEL_MAX_UPROBE_NUM) {
552 DBG("Wrong number of probes.");
553 ret = -1;
554 ret_value->_error = true;
555 goto free_offset;
556 }
557
558 /* Copy the content of the offsets array to the ret struct. */
559 memcpy(ret_value->u.extract_sdt_probe_offsets.offsets,
560 offsets, num_offset * sizeof(uint64_t));
561
562 ret_value->u.extract_sdt_probe_offsets.num_offset = num_offset;
563
564free_offset:
565 free(offsets);
566end:
567 return ret;
568}
b1b34226
MJ
569#else
570static
571int _extract_elf_symbol_offset(struct run_as_data *data,
572 struct run_as_ret *ret_value)
573{
574 ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET");
575 return -1;
576}
577
578static
579int _extract_sdt_probe_offsets(struct run_as_data *data,
580 struct run_as_ret *ret_value)
581{
582 ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS");
583 return -1;
584}
585#endif
241e0a5a 586
7567352f
MD
587static
588run_as_fct run_as_enum_to_fct(enum run_as_cmd cmd)
589{
590 switch (cmd) {
591 case RUN_AS_MKDIR:
18710679
JG
592 case RUN_AS_MKDIRAT:
593 return _mkdirat;
594 case RUN_AS_MKDIR_RECURSIVE:
595 case RUN_AS_MKDIRAT_RECURSIVE:
596 return _mkdirat_recursive;
7567352f 597 case RUN_AS_OPEN:
2912cead 598 case RUN_AS_OPENAT:
7567352f
MD
599 return _open;
600 case RUN_AS_UNLINK:
2912cead 601 case RUN_AS_UNLINKAT:
7567352f 602 return _unlink;
93bed9fe
JG
603 case RUN_AS_RMDIR:
604 case RUN_AS_RMDIRAT:
605 return _rmdir;
7567352f 606 case RUN_AS_RMDIR_RECURSIVE:
93bed9fe 607 case RUN_AS_RMDIRAT_RECURSIVE:
7567352f 608 return _rmdir_recursive;
93bed9fe
JG
609 case RUN_AS_RENAME:
610 case RUN_AS_RENAMEAT:
611 return _rename;
241e0a5a
FD
612 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
613 return _extract_elf_symbol_offset;
0ef03255
FD
614 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
615 return _extract_sdt_probe_offsets;
7567352f 616 default:
62a7b8ed 617 ERR("Unknown command %d", (int) cmd);
7567352f
MD
618 return NULL;
619 }
60b6c79c
MD
620}
621
4628484a 622static
93bed9fe 623int do_send_fds(int sock, const int *fds, unsigned int fd_count)
4628484a 624{
7567352f 625 ssize_t len;
93bed9fe
JG
626 unsigned int i;
627
628 for (i = 0; i < fd_count; i++) {
629 if (fds[i] < 0) {
630 ERR("Attempt to send invalid file descriptor to master (fd = %i)",
631 fds[i]);
632 /* Return 0 as this is not a fatal error. */
633 return 0;
634 }
635 }
4628484a 636
93bed9fe
JG
637 len = lttcomm_send_fds_unix_sock(sock, fds, fd_count);
638 return len < 0 ? -1 : 0;
4628484a
MD
639}
640
641static
93bed9fe 642int do_recv_fds(int sock, int *fds, unsigned int fd_count)
4628484a 643{
93bed9fe
JG
644 int ret = 0;
645 unsigned int i;
7567352f 646 ssize_t len;
4628484a 647
93bed9fe
JG
648 len = lttcomm_recv_fds_unix_sock(sock, fds, fd_count);
649 if (len == 0) {
650 ret = -1;
651 goto end;
da9ee832 652 } else if (len < 0) {
93bed9fe
JG
653 PERROR("Failed to receive file descriptors from socket");
654 ret = -1;
655 goto end;
ca9eb994
JG
656 }
657
93bed9fe
JG
658 for (i = 0; i < fd_count; i++) {
659 if (fds[i] < 0) {
660 ERR("Invalid file descriptor received from worker (fd = %i)", fds[i]);
661 /* Return 0 as this is not a fatal error. */
662 }
663 }
664end:
665 return ret;
4628484a
MD
666}
667
fe9f7760 668static
93bed9fe
JG
669int send_fds_to_worker(const struct run_as_worker *worker,
670 const struct run_as_data *data)
fe9f7760
FD
671{
672 int ret = 0;
93bed9fe 673 unsigned int i;
fe9f7760 674
93bed9fe
JG
675 if (COMMAND_USE_CWD_FD(data) || COMMAND_IN_FD_COUNT(data) == 0) {
676 goto end;
fe9f7760
FD
677 }
678
93bed9fe
JG
679 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
680 if (COMMAND_IN_FDS(data)[i] < 0) {
681 ERR("Refusing to send invalid fd to worker (fd = %i)",
682 COMMAND_IN_FDS(data)[i]);
683 ret = -1;
684 goto end;
685 }
686 }
ca9eb994 687
93bed9fe
JG
688 ret = do_send_fds(worker->sockpair[0], COMMAND_IN_FDS(data),
689 COMMAND_IN_FD_COUNT(data));
fe9f7760 690 if (ret < 0) {
93bed9fe 691 PERROR("Failed to send file descriptor to run-as worker");
fe9f7760 692 ret = -1;
93bed9fe 693 goto end;
fe9f7760 694 }
93bed9fe 695end:
fe9f7760
FD
696 return ret;
697}
698
699static
93bed9fe
JG
700int send_fds_to_master(struct run_as_worker *worker, enum run_as_cmd cmd,
701 struct run_as_ret *run_as_ret)
fe9f7760 702{
93bed9fe
JG
703 int ret = 0;
704 unsigned int i;
fe9f7760 705
93bed9fe
JG
706 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
707 goto end;
fe9f7760
FD
708 }
709
93bed9fe
JG
710 ret = do_send_fds(worker->sockpair[1], COMMAND_OUT_FDS(cmd, run_as_ret),
711 COMMAND_OUT_FD_COUNT(cmd));
fe9f7760 712 if (ret < 0) {
93bed9fe
JG
713 PERROR("Failed to send file descriptor to master process");
714 goto end;
fe9f7760
FD
715 }
716
93bed9fe
JG
717 for (i = 0; i < COMMAND_OUT_FD_COUNT(cmd); i++) {
718 int ret_close = close(COMMAND_OUT_FDS(cmd, run_as_ret)[i]);
ca9eb994 719
93bed9fe
JG
720 if (ret_close < 0) {
721 PERROR("Failed to close result file descriptor");
722 }
723 }
724end:
fe9f7760
FD
725 return ret;
726}
727
728static
93bed9fe
JG
729int recv_fds_from_worker(const struct run_as_worker *worker, enum run_as_cmd cmd,
730 struct run_as_ret *run_as_ret)
fe9f7760
FD
731{
732 int ret = 0;
733
93bed9fe
JG
734 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
735 goto end;
fe9f7760
FD
736 }
737
93bed9fe
JG
738 ret = do_recv_fds(worker->sockpair[0], COMMAND_OUT_FDS(cmd, run_as_ret),
739 COMMAND_OUT_FD_COUNT(cmd));
fe9f7760 740 if (ret < 0) {
93bed9fe 741 PERROR("Failed to receive file descriptor from run-as worker");
fe9f7760
FD
742 ret = -1;
743 }
93bed9fe 744end:
fe9f7760
FD
745 return ret;
746}
747
748static
93bed9fe 749int recv_fds_from_master(struct run_as_worker *worker, struct run_as_data *data)
fe9f7760
FD
750{
751 int ret = 0;
752
93bed9fe
JG
753 if (COMMAND_USE_CWD_FD(data)) {
754 unsigned int i;
755
756 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
757 COMMAND_IN_FDS(data)[i] = AT_FDCWD;
758 }
759 goto end;
fe9f7760
FD
760 }
761
93bed9fe
JG
762 ret = do_recv_fds(worker->sockpair[1], COMMAND_IN_FDS(data),
763 COMMAND_IN_FD_COUNT(data));
fe9f7760 764 if (ret < 0) {
93bed9fe 765 PERROR("Failed to receive file descriptors from master process");
fe9f7760
FD
766 ret = -1;
767 }
93bed9fe 768end:
fe9f7760
FD
769 return ret;
770}
771
772static
93bed9fe 773int cleanup_received_fds(struct run_as_data *data)
fe9f7760 774{
93bed9fe 775 int ret = 0, i;
fe9f7760 776
93bed9fe
JG
777 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
778 if (COMMAND_IN_FDS(data)[i] == -1) {
779 continue;
780 }
781 ret = close(COMMAND_IN_FDS(data)[i]);
782 if (ret) {
783 PERROR("Failed to close file descriptor received fd in run-as worker");
784 goto end;
785 }
fe9f7760 786 }
93bed9fe 787end:
fe9f7760
FD
788 return ret;
789}
0ef03255 790
7567352f
MD
791/*
792 * Return < 0 on error, 0 if OK, 1 on hangup.
793 */
c2b75c49 794static
7567352f 795int handle_one_cmd(struct run_as_worker *worker)
c2b75c49 796{
7567352f 797 int ret = 0;
93bed9fe
JG
798 struct run_as_data data = {};
799 ssize_t readlen, writelen;
800 struct run_as_ret sendret = {};
801 run_as_fct cmd;
7567352f
MD
802 uid_t prev_euid;
803
fe9f7760
FD
804 /*
805 * Stage 1: Receive run_as_data struct from the master.
806 * The structure contains the command type and all the parameters needed for
807 * its execution
808 */
7567352f
MD
809 readlen = lttcomm_recv_unix_sock(worker->sockpair[1], &data,
810 sizeof(data));
811 if (readlen == 0) {
812 /* hang up */
813 ret = 1;
814 goto end;
815 }
816 if (readlen < sizeof(data)) {
817 PERROR("lttcomm_recv_unix_sock error");
818 ret = -1;
819 goto end;
820 }
c2b75c49 821
7567352f
MD
822 cmd = run_as_enum_to_fct(data.cmd);
823 if (!cmd) {
824 ret = -1;
825 goto end;
826 }
827
fe9f7760
FD
828 /*
829 * Stage 2: Receive file descriptor from master.
830 * Some commands need a file descriptor as input so if it's needed we
831 * receive the fd using the Unix socket.
832 */
93bed9fe 833 ret = recv_fds_from_master(worker, &data);
fe9f7760
FD
834 if (ret < 0) {
835 PERROR("recv_fd_from_master error");
836 ret = -1;
837 goto end;
838 }
839
7567352f
MD
840 prev_euid = getuid();
841 if (data.gid != getegid()) {
842 ret = setegid(data.gid);
1576d582 843 if (ret < 0) {
561f5f2c
JG
844 sendret._error = true;
845 sendret._errno = errno;
4c462e79 846 PERROR("setegid");
6d73c4ef 847 goto write_return;
1576d582 848 }
c2b75c49 849 }
7567352f
MD
850 if (data.uid != prev_euid) {
851 ret = seteuid(data.uid);
1576d582 852 if (ret < 0) {
561f5f2c
JG
853 sendret._error = true;
854 sendret._errno = errno;
4c462e79 855 PERROR("seteuid");
6d73c4ef 856 goto write_return;
1576d582 857 }
c2b75c49 858 }
fe9f7760 859
c2b75c49
MD
860 /*
861 * Also set umask to 0 for mkdir executable bit.
862 */
863 umask(0);
fe9f7760
FD
864
865 /*
866 * Stage 3: Execute the command
867 */
868 ret = (*cmd)(&data, &sendret);
869 if (ret < 0) {
870 DBG("Execution of command returned an error");
871 }
6d73c4ef
MD
872
873write_return:
93bed9fe 874 ret = cleanup_received_fds(&data);
fe9f7760
FD
875 if (ret < 0) {
876 ERR("Error cleaning up FD");
877 goto end;
878 }
879
880 /*
881 * Stage 4: Send run_as_ret structure to the master.
882 * This structure contain the return value of the command and the errno.
883 */
7567352f
MD
884 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
885 sizeof(sendret));
6cd525e8 886 if (writelen < sizeof(sendret)) {
7567352f
MD
887 PERROR("lttcomm_send_unix_sock error");
888 ret = -1;
889 goto end;
890 }
fe9f7760
FD
891
892 /*
93bed9fe 893 * Stage 5: Send resulting file descriptors to the master.
fe9f7760 894 */
93bed9fe 895 ret = send_fds_to_master(worker, data.cmd, &sendret);
fe9f7760
FD
896 if (ret < 0) {
897 DBG("Sending FD to master returned an error");
7567352f
MD
898 goto end;
899 }
fe9f7760 900
7567352f
MD
901 if (seteuid(prev_euid) < 0) {
902 PERROR("seteuid");
903 ret = -1;
904 goto end;
905 }
906 ret = 0;
907end:
908 return ret;
909}
910
911static
912int run_as_worker(struct run_as_worker *worker)
913{
914 int ret;
915 ssize_t writelen;
916 struct run_as_ret sendret;
917 size_t proc_orig_len;
918
919 /*
920 * Initialize worker. Set a different process cmdline.
921 */
922 proc_orig_len = strlen(worker->procname);
923 memset(worker->procname, 0, proc_orig_len);
924 strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len);
925
e1055edb
JG
926 ret = lttng_prctl(PR_SET_NAME,
927 (unsigned long) DEFAULT_RUN_AS_WORKER_NAME, 0, 0, 0);
928 if (ret && ret != -ENOSYS) {
b8090274
JG
929 /* Don't fail as this is not essential. */
930 PERROR("prctl PR_SET_NAME");
6cd525e8 931 }
7567352f 932
fe9f7760
FD
933 memset(&sendret, 0, sizeof(sendret));
934
7567352f
MD
935 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
936 sizeof(sendret));
937 if (writelen < sizeof(sendret)) {
938 PERROR("lttcomm_send_unix_sock error");
939 ret = EXIT_FAILURE;
940 goto end;
941 }
942
943 for (;;) {
944 ret = handle_one_cmd(worker);
945 if (ret < 0) {
946 ret = EXIT_FAILURE;
947 goto end;
948 } else if (ret > 0) {
949 break;
950 } else {
951 continue; /* Next command. */
952 }
953 }
954 ret = EXIT_SUCCESS;
955end:
956 return ret;
c2b75c49
MD
957}
958
60b6c79c 959static
7567352f
MD
960int run_as_cmd(struct run_as_worker *worker,
961 enum run_as_cmd cmd,
962 struct run_as_data *data,
fe9f7760 963 struct run_as_ret *ret_value,
7567352f 964 uid_t uid, gid_t gid)
60b6c79c 965{
fe9f7760 966 int ret = 0;
7567352f 967 ssize_t readlen, writelen;
60b6c79c
MD
968
969 /*
970 * If we are non-root, we can only deal with our own uid.
971 */
972 if (geteuid() != 0) {
973 if (uid != geteuid()) {
fe9f7760
FD
974 ret = -1;
975 ret_value->_errno = EPERM;
60b6c79c 976 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
08797918 977 (int) uid, (int) geteuid());
df5b86c8 978 goto end;
60b6c79c 979 }
60b6c79c
MD
980 }
981
7567352f
MD
982 data->cmd = cmd;
983 data->uid = uid;
984 data->gid = gid;
985
fe9f7760
FD
986 /*
987 * Stage 1: Send the run_as_data struct to the worker process
988 */
7567352f
MD
989 writelen = lttcomm_send_unix_sock(worker->sockpair[0], data,
990 sizeof(*data));
991 if (writelen < sizeof(*data)) {
992 PERROR("Error writing message to run_as");
fe9f7760
FD
993 ret = -1;
994 ret_value->_errno = EIO;
60b6c79c 995 goto end;
c2b75c49 996 }
7567352f 997
fe9f7760
FD
998 /*
999 * Stage 2: Send file descriptor to the worker process if needed
1000 */
93bed9fe 1001 ret = send_fds_to_worker(worker, data);
fe9f7760
FD
1002 if (ret) {
1003 PERROR("do_send_fd error");
1004 ret = -1;
1005 ret_value->_errno = EIO;
1006 goto end;
1007 }
1008
1009 /*
1010 * Stage 3: Wait for the execution of the command
1011 */
1012
1013 /*
1014 * Stage 4: Receive the run_as_ret struct containing the return value and
1015 * errno
1016 */
1017 readlen = lttcomm_recv_unix_sock(worker->sockpair[0], ret_value,
1018 sizeof(*ret_value));
da9ee832
JG
1019 if (!readlen) {
1020 ERR("Run-as worker has hung-up during run_as_cmd");
fe9f7760
FD
1021 ret = -1;
1022 ret_value->_errno = EIO;
da9ee832 1023 goto end;
fe9f7760 1024 } else if (readlen < sizeof(*ret_value)) {
7567352f 1025 PERROR("Error reading response from run_as");
fe9f7760
FD
1026 ret = -1;
1027 ret_value->_errno = errno;
033b58a7 1028 goto end;
6cd525e8 1029 }
fe9f7760 1030
ca9eb994
JG
1031 if (ret_value->_error) {
1032 /* Skip stage 5 on error as there will be no fd to receive. */
1033 goto end;
1034 }
1035
fe9f7760
FD
1036 /*
1037 * Stage 5: Receive file descriptor if needed
1038 */
93bed9fe 1039 ret = recv_fds_from_worker(worker, cmd, ret_value);
fe9f7760
FD
1040 if (ret < 0) {
1041 ERR("Error receiving fd");
1042 ret = -1;
1043 ret_value->_errno = EIO;
4c462e79 1044 }
7567352f 1045
60b6c79c 1046end:
fe9f7760 1047 return ret;
60b6c79c
MD
1048}
1049
2d85a600 1050/*
7567352f 1051 * This is for debugging ONLY and should not be considered secure.
2d85a600
MD
1052 */
1053static
fe9f7760
FD
1054int run_as_noworker(enum run_as_cmd cmd,
1055 struct run_as_data *data, struct run_as_ret *ret_value,
1056 uid_t uid, gid_t gid)
2d85a600 1057{
df5b86c8 1058 int ret, saved_errno;
5b73926f 1059 mode_t old_mask;
7567352f 1060 run_as_fct fct;
5b73926f 1061
7567352f
MD
1062 fct = run_as_enum_to_fct(cmd);
1063 if (!fct) {
1064 errno = -ENOSYS;
1065 ret = -1;
1066 goto end;
1067 }
5b73926f 1068 old_mask = umask(0);
fe9f7760
FD
1069 ret = fct(data, ret_value);
1070 saved_errno = ret_value->_errno;
5b73926f 1071 umask(old_mask);
df5b86c8 1072 errno = saved_errno;
7567352f 1073end:
5b73926f 1074 return ret;
2d85a600
MD
1075}
1076
8fec83ea
JG
1077static
1078int reset_sighandler(void)
1079{
1080 int sig;
1081
1082 DBG("Resetting run_as worker signal handlers to default");
1083 for (sig = 1; sig <= 31; sig++) {
1084 (void) signal(sig, SIG_DFL);
1085 }
1086 return 0;
1087}
1088
1089static
1090void worker_sighandler(int sig)
1091{
1092 const char *signame;
1093
1094 /*
1095 * The worker will inherit its parent's signals since they are part of
1096 * the same process group. However, in the case of SIGINT and SIGTERM,
1097 * we want to give the worker a chance to teardown gracefully when its
1098 * parent closes the command socket.
1099 */
1100 switch (sig) {
1101 case SIGINT:
1102 signame = "SIGINT";
1103 break;
1104 case SIGTERM:
1105 signame = "SIGTERM";
1106 break;
1107 default:
1108 signame = NULL;
1109 }
1110
1111 if (signame) {
1112 DBG("run_as worker received signal %s", signame);
1113 } else {
1114 DBG("run_as_worker received signal %d", sig);
1115 }
1116}
1117
1118static
1119int set_worker_sighandlers(void)
1120{
1121 int ret = 0;
1122 sigset_t sigset;
1123 struct sigaction sa;
1124
1125 if ((ret = sigemptyset(&sigset)) < 0) {
1126 PERROR("sigemptyset");
1127 goto end;
1128 }
1129
1130 sa.sa_handler = worker_sighandler;
1131 sa.sa_mask = sigset;
1132 sa.sa_flags = 0;
1133 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
1134 PERROR("sigaction SIGINT");
1135 goto end;
1136 }
1137
1138 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
1139 PERROR("sigaction SIGTERM");
1140 goto end;
1141 }
1142
1143 DBG("run_as signal handler set for SIGTERM and SIGINT");
1144end:
1145 return ret;
1146}
1147
1148static
929f71ec
JG
1149int run_as_create_worker_no_lock(const char *procname,
1150 post_fork_cleanup_cb clean_up_func,
1151 void *clean_up_user_data)
8fec83ea
JG
1152{
1153 pid_t pid;
1154 int i, ret = 0;
1155 ssize_t readlen;
1156 struct run_as_ret recvret;
1157 struct run_as_worker *worker;
1158
1159 assert(!global_worker);
1160 if (!use_clone()) {
1161 /*
1162 * Don't initialize a worker, all run_as tasks will be performed
1163 * in the current process.
1164 */
1165 ret = 0;
1166 goto end;
1167 }
1168 worker = zmalloc(sizeof(*worker));
1169 if (!worker) {
1170 ret = -ENOMEM;
1171 goto end;
1172 }
1173 worker->procname = strdup(procname);
1174 if (!worker->procname) {
1175 ret = -ENOMEM;
8c96eded 1176 goto error_procname_alloc;
8fec83ea
JG
1177 }
1178 /* Create unix socket. */
1179 if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) {
1180 ret = -1;
1181 goto error_sock;
1182 }
1183
1184 /* Fork worker. */
1185 pid = fork();
1186 if (pid < 0) {
1187 PERROR("fork");
1188 ret = -1;
1189 goto error_fork;
1190 } else if (pid == 0) {
1191 /* Child */
1192
1193 reset_sighandler();
1194
1195 set_worker_sighandlers();
929f71ec
JG
1196 if (clean_up_func) {
1197 if (clean_up_func(clean_up_user_data) < 0) {
1198 ERR("Run-as post-fork clean-up failed, exiting.");
1199 exit(EXIT_FAILURE);
1200 }
1201 }
8fec83ea
JG
1202
1203 /* Just close, no shutdown. */
1204 if (close(worker->sockpair[0])) {
1205 PERROR("close");
1206 exit(EXIT_FAILURE);
1207 }
1208
1209 /*
1210 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
1211 * Sockpair[1] is used as a control channel with the master
1212 */
1213 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
1214 if (i != worker->sockpair[1]) {
1215 (void) close(i);
1216 }
1217 }
1218
1219 worker->sockpair[0] = -1;
1220 ret = run_as_worker(worker);
1221 if (lttcomm_close_unix_sock(worker->sockpair[1])) {
1222 PERROR("close");
1223 ret = -1;
1224 }
1225 worker->sockpair[1] = -1;
340cf672
JG
1226 free(worker->procname);
1227 free(worker);
8fec83ea
JG
1228 LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret);
1229 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
1230 } else {
1231 /* Parent */
1232
1233 /* Just close, no shutdown. */
1234 if (close(worker->sockpair[1])) {
1235 PERROR("close");
1236 ret = -1;
1237 goto error_fork;
1238 }
1239 worker->sockpair[1] = -1;
1240 worker->pid = pid;
1241 /* Wait for worker to become ready. */
1242 readlen = lttcomm_recv_unix_sock(worker->sockpair[0],
1243 &recvret, sizeof(recvret));
1244 if (readlen < sizeof(recvret)) {
1245 ERR("readlen: %zd", readlen);
1246 PERROR("Error reading response from run_as at creation");
1247 ret = -1;
1248 goto error_fork;
1249 }
1250 global_worker = worker;
1251 }
1252end:
1253 return ret;
1254
1255 /* Error handling. */
1256error_fork:
1257 for (i = 0; i < 2; i++) {
1258 if (worker->sockpair[i] < 0) {
1259 continue;
1260 }
1261 if (lttcomm_close_unix_sock(worker->sockpair[i])) {
1262 PERROR("close");
1263 }
1264 worker->sockpair[i] = -1;
1265 }
1266error_sock:
8c96eded
FD
1267 free(worker->procname);
1268error_procname_alloc:
8fec83ea
JG
1269 free(worker);
1270 return ret;
1271}
1272
a01c682b
JR
1273static
1274void run_as_destroy_worker_no_lock(void)
1275{
1276 struct run_as_worker *worker = global_worker;
1277
1278 DBG("Destroying run_as worker");
1279 if (!worker) {
1280 return;
1281 }
1282 /* Close unix socket */
1283 DBG("Closing run_as worker socket");
1284 if (lttcomm_close_unix_sock(worker->sockpair[0])) {
1285 PERROR("close");
1286 }
1287 worker->sockpair[0] = -1;
1288 /* Wait for worker. */
1289 for (;;) {
1290 int status;
1291 pid_t wait_ret;
1292
1293 wait_ret = waitpid(worker->pid, &status, 0);
1294 if (wait_ret < 0) {
1295 if (errno == EINTR) {
1296 continue;
1297 }
1298 PERROR("waitpid");
1299 break;
1300 }
1301
1302 if (WIFEXITED(status)) {
1303 LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR,
1304 DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d",
1305 WEXITSTATUS(status));
1306 break;
1307 } else if (WIFSIGNALED(status)) {
1308 ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d",
1309 WTERMSIG(status));
1310 break;
1311 }
1312 }
1313 free(worker->procname);
1314 free(worker);
1315 global_worker = NULL;
1316}
1317
2d85a600 1318static
fe9f7760 1319int run_as_restart_worker(struct run_as_worker *worker)
2d85a600 1320{
fe9f7760
FD
1321 int ret = 0;
1322 char *procname = NULL;
1323
1324 procname = worker->procname;
1325
1326 /* Close socket to run_as worker process and clean up the zombie process */
a01c682b 1327 run_as_destroy_worker_no_lock();
fe9f7760
FD
1328
1329 /* Create a new run_as worker process*/
929f71ec 1330 ret = run_as_create_worker_no_lock(procname, NULL, NULL);
fe9f7760
FD
1331 if (ret < 0 ) {
1332 ERR("Restarting the worker process failed");
1333 ret = -1;
1334 goto err;
1335 }
1336err:
1337 return ret;
1338}
1339
1340static
1341int run_as(enum run_as_cmd cmd, struct run_as_data *data,
1342 struct run_as_ret *ret_value, uid_t uid, gid_t gid)
1343{
1344 int ret, saved_errno;
7567352f 1345
8fec83ea 1346 pthread_mutex_lock(&worker_lock);
749b7a0c 1347 if (use_clone()) {
7567352f 1348 DBG("Using run_as worker");
8fec83ea 1349
749b7a0c 1350 assert(global_worker);
749b7a0c 1351
fe9f7760
FD
1352 ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid);
1353 saved_errno = ret_value->_errno;
1354
fe9f7760
FD
1355 /*
1356 * If the worker thread crashed the errno is set to EIO. we log
1357 * the error and start a new worker process.
1358 */
1359 if (ret == -1 && saved_errno == EIO) {
1360 DBG("Socket closed unexpectedly... "
1361 "Restarting the worker process");
1362 ret = run_as_restart_worker(global_worker);
fe9f7760
FD
1363 if (ret == -1) {
1364 ERR("Failed to restart worker process.");
1365 goto err;
1366 }
1367 }
2d85a600 1368 } else {
7567352f 1369 DBG("Using run_as without worker");
fe9f7760 1370 ret = run_as_noworker(cmd, data, ret_value, uid, gid);
2d85a600 1371 }
fe9f7760 1372err:
8fec83ea 1373 pthread_mutex_unlock(&worker_lock);
7567352f 1374 return ret;
2d85a600
MD
1375}
1376
90e535ef 1377LTTNG_HIDDEN
e11d277b 1378int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 1379{
18710679
JG
1380 return run_as_mkdirat_recursive(AT_FDCWD, path, mode, uid, gid);
1381}
1382
1383LTTNG_HIDDEN
1384int run_as_mkdirat_recursive(int dirfd, const char *path, mode_t mode,
1385 uid_t uid, gid_t gid)
1386{
1387 int ret;
93bed9fe
JG
1388 struct run_as_data data = {};
1389 struct run_as_ret run_as_ret = {};
60b6c79c 1390
18710679
JG
1391 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1392 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1393 path, (int) mode, (int) uid, (int) gid);
93bed9fe
JG
1394 ret = lttng_strncpy(data.u.mkdir.path, path,
1395 sizeof(data.u.mkdir.path));
18710679
JG
1396 if (ret) {
1397 ERR("Failed to copy path argument of mkdirat recursive command");
1398 goto error;
1399 }
93bed9fe
JG
1400 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1401 data.u.mkdir.mode = mode;
1402 data.u.mkdir.dirfd = dirfd;
18710679
JG
1403 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR_RECURSIVE : RUN_AS_MKDIRAT_RECURSIVE,
1404 &data, &run_as_ret, uid, gid);
1405 errno = run_as_ret._errno;
93bed9fe 1406 ret = run_as_ret.u.ret;
18710679
JG
1407error:
1408 return ret;
60b6c79c
MD
1409}
1410
90e535ef 1411LTTNG_HIDDEN
e11d277b 1412int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 1413{
18710679
JG
1414 return run_as_mkdirat(AT_FDCWD, path, mode, uid, gid);
1415}
1416
1417LTTNG_HIDDEN
1418int run_as_mkdirat(int dirfd, const char *path, mode_t mode,
1419 uid_t uid, gid_t gid)
1420{
1421 int ret;
93bed9fe
JG
1422 struct run_as_data data = {};
1423 struct run_as_ret run_as_ret = {};
fe9f7760 1424
18710679
JG
1425 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1426 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1427 path, (int) mode, (int) uid, (int) gid);
93bed9fe
JG
1428 ret = lttng_strncpy(data.u.mkdir.path, path,
1429 sizeof(data.u.mkdir.path));
18710679
JG
1430 if (ret) {
1431 ERR("Failed to copy path argument of mkdirat command");
1432 goto error;
1433 }
93bed9fe
JG
1434 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1435 data.u.mkdir.mode = mode;
1436 data.u.mkdir.dirfd = dirfd;
18710679
JG
1437 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR : RUN_AS_MKDIRAT,
1438 &data, &run_as_ret, uid, gid);
1439 errno = run_as_ret._errno;
93bed9fe 1440 ret = run_as_ret.u.ret;
18710679
JG
1441error:
1442 return ret;
60b6c79c
MD
1443}
1444
90e535ef 1445LTTNG_HIDDEN
2912cead
JG
1446int run_as_open(const char *path, int flags, mode_t mode, uid_t uid,
1447 gid_t gid)
1448{
1449 return run_as_openat(AT_FDCWD, path, flags, mode, uid, gid);
1450}
1451
1452LTTNG_HIDDEN
1453int run_as_openat(int dirfd, const char *path, int flags, mode_t mode,
1454 uid_t uid, gid_t gid)
60b6c79c 1455{
93bed9fe
JG
1456 int ret;
1457 struct run_as_data data = {};
1458 struct run_as_ret run_as_ret = {};
fe9f7760 1459
2912cead
JG
1460 DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
1461 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1462 path, flags, (int) mode, (int) uid, (int) gid);
93bed9fe
JG
1463 ret = lttng_strncpy(data.u.open.path, path, sizeof(data.u.open.path));
1464 if (ret) {
1465 ERR("Failed to copy path argument of open command");
1466 goto error;
1467 }
7567352f
MD
1468 data.u.open.flags = flags;
1469 data.u.open.mode = mode;
93bed9fe 1470 data.u.open.dirfd = dirfd;
2912cead 1471 run_as(dirfd == AT_FDCWD ? RUN_AS_OPEN : RUN_AS_OPENAT,
93bed9fe
JG
1472 &data, &run_as_ret, uid, gid);
1473 errno = run_as_ret._errno;
1474 ret = run_as_ret.u.ret < 0 ? run_as_ret.u.ret :
1475 run_as_ret.u.open.fd;
1476error:
1477 return ret;
60b6c79c 1478}
4628484a
MD
1479
1480LTTNG_HIDDEN
1481int run_as_unlink(const char *path, uid_t uid, gid_t gid)
2912cead
JG
1482{
1483 return run_as_unlinkat(AT_FDCWD, path, uid, gid);
1484}
1485
1486LTTNG_HIDDEN
1487int run_as_unlinkat(int dirfd, const char *path, uid_t uid, gid_t gid)
4628484a 1488{
93bed9fe
JG
1489 int ret;
1490 struct run_as_data data = {};
1491 struct run_as_ret run_as_ret = {};
fe9f7760 1492
2912cead
JG
1493 DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
1494 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1495 path, (int) uid, (int) gid);
93bed9fe
JG
1496 ret = lttng_strncpy(data.u.unlink.path, path,
1497 sizeof(data.u.unlink.path));
1498 if (ret) {
1499 goto error;
1500 }
1501 data.u.unlink.dirfd = dirfd;
1502 run_as(dirfd == AT_FDCWD ? RUN_AS_UNLINK : RUN_AS_UNLINKAT, &data,
1503 &run_as_ret, uid, gid);
1504 errno = run_as_ret._errno;
1505 ret = run_as_ret.u.ret;
1506error:
1507 return ret;
1508}
1509
1510LTTNG_HIDDEN
1511int run_as_rmdir(const char *path, uid_t uid, gid_t gid)
1512{
1513 return run_as_rmdirat(AT_FDCWD, path, uid, gid);
1514}
1515
1516LTTNG_HIDDEN
1517int run_as_rmdirat(int dirfd, const char *path, uid_t uid, gid_t gid)
1518{
1519 int ret;
1520 struct run_as_data data = {};
1521 struct run_as_ret run_as_ret = {};
1522
1523 DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d",
1524 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1525 path, (int) uid, (int) gid);
1526 ret = lttng_strncpy(data.u.rmdir.path, path,
1527 sizeof(data.u.rmdir.path));
1528 if (ret) {
1529 goto error;
1530 }
1531 data.u.rmdir.dirfd = dirfd;
1532 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR : RUN_AS_RMDIRAT, &data,
1533 &run_as_ret, uid, gid);
1534 errno = run_as_ret._errno;
1535 ret = run_as_ret.u.ret;
1536error:
1537 return ret;
4628484a
MD
1538}
1539
1540LTTNG_HIDDEN
f75c5439 1541int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid, int flags)
4628484a 1542{
f75c5439 1543 return run_as_rmdirat_recursive(AT_FDCWD, path, uid, gid, flags);
93bed9fe 1544}
fe9f7760 1545
93bed9fe 1546LTTNG_HIDDEN
f75c5439 1547int run_as_rmdirat_recursive(int dirfd, const char *path, uid_t uid, gid_t gid, int flags)
93bed9fe
JG
1548{
1549 int ret;
1550 struct run_as_data data = {};
1551 struct run_as_ret run_as_ret = {};
4628484a 1552
93bed9fe
JG
1553 DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d",
1554 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1555 path, (int) uid, (int) gid);
93bed9fe
JG
1556 ret = lttng_strncpy(data.u.rmdir.path, path,
1557 sizeof(data.u.rmdir.path));
1558 if (ret) {
1559 goto error;
1560 }
1561 data.u.rmdir.dirfd = dirfd;
f75c5439 1562 data.u.rmdir.flags = flags;
93bed9fe
JG
1563 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR_RECURSIVE : RUN_AS_RMDIRAT_RECURSIVE,
1564 &data, &run_as_ret, uid, gid);
1565 errno = run_as_ret._errno;
1566 ret = run_as_ret.u.ret;
1567error:
1568 return ret;
1569}
1570
1571LTTNG_HIDDEN
1572int run_as_rename(const char *old, const char *new, uid_t uid, gid_t gid)
1573{
1574 return run_as_renameat(AT_FDCWD, old, AT_FDCWD, new, uid, gid);
1575}
1576
1577LTTNG_HIDDEN
1578int run_as_renameat(int old_dirfd, const char *old_name,
1579 int new_dirfd, const char *new_name, uid_t uid, gid_t gid)
1580{
1581 int ret;
1582 struct run_as_data data = {};
1583 struct run_as_ret run_as_ret = {};
1584
1585 DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d",
1586 old_dirfd, old_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1587 old_name,
1588 new_dirfd, new_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1589 new_name, (int) uid, (int) gid);
1590 ret = lttng_strncpy(data.u.rename.old_path, old_name,
1591 sizeof(data.u.rename.old_path));
1592 if (ret) {
1593 goto error;
1594 }
1595 ret = lttng_strncpy(data.u.rename.new_path, new_name,
1596 sizeof(data.u.rename.new_path));
1597 if (ret) {
1598 goto error;
1599 }
1600
1601 data.u.rename.dirfds[0] = old_dirfd;
1602 data.u.rename.dirfds[1] = new_dirfd;
1603 run_as(old_dirfd == AT_FDCWD && new_dirfd == AT_FDCWD ?
1604 RUN_AS_RENAME : RUN_AS_RENAMEAT,
1605 &data, &run_as_ret, uid, gid);
1606 errno = run_as_ret._errno;
1607 ret = run_as_ret.u.ret;
1608error:
1609 return ret;
7567352f
MD
1610}
1611
241e0a5a
FD
1612LTTNG_HIDDEN
1613int run_as_extract_elf_symbol_offset(int fd, const char* function,
1614 uid_t uid, gid_t gid, uint64_t *offset)
1615{
93bed9fe
JG
1616 int ret;
1617 struct run_as_data data = {};
1618 struct run_as_ret run_as_ret = {};
f726677b 1619
241e0a5a 1620 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
93bed9fe
JG
1621 "with for uid %d and gid %d", fd, function,
1622 (int) uid, (int) gid);
241e0a5a 1623
93bed9fe 1624 data.u.extract_elf_symbol_offset.fd = fd;
241e0a5a
FD
1625
1626 strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1);
241e0a5a 1627 data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
93bed9fe
JG
1628 ret = lttng_strncpy(data.u.extract_elf_symbol_offset.function,
1629 function,
1630 sizeof(data.u.extract_elf_symbol_offset.function));
1631 if (ret) {
1632 goto error;
1633 }
241e0a5a 1634
93bed9fe
JG
1635 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &run_as_ret, uid, gid);
1636 errno = run_as_ret._errno;
1637 if (run_as_ret._error) {
1638 ret = -1;
1639 goto error;
241e0a5a
FD
1640 }
1641
93bed9fe
JG
1642 *offset = run_as_ret.u.extract_elf_symbol_offset.offset;
1643error:
1644 return ret;
241e0a5a
FD
1645}
1646
0ef03255
FD
1647LTTNG_HIDDEN
1648int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name,
1649 const char* probe_name, uid_t uid, gid_t gid,
1650 uint64_t **offsets, uint32_t *num_offset)
1651{
93bed9fe
JG
1652 int ret;
1653 struct run_as_data data = {};
1654 struct run_as_ret run_as_ret = {};
f726677b 1655
0ef03255 1656 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
93bed9fe
JG
1657 "provider_name=%s with for uid %d and gid %d", fd,
1658 probe_name, provider_name, (int) uid, (int) gid);
0ef03255 1659
93bed9fe 1660 data.u.extract_sdt_probe_offsets.fd = fd;
0ef03255 1661
93bed9fe
JG
1662 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name,
1663 sizeof(data.u.extract_sdt_probe_offsets.probe_name));
1664 if (ret) {
1665 goto error;
1666 }
1667 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.provider_name,
1668 provider_name,
1669 sizeof(data.u.extract_sdt_probe_offsets.provider_name));
1670 if (ret) {
1671 goto error;
0ef03255
FD
1672 }
1673
93bed9fe
JG
1674 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &run_as_ret, uid, gid);
1675 errno = run_as_ret._errno;
1676 if (run_as_ret._error) {
1677 ret = -1;
1678 goto error;
1679 }
0ef03255 1680
93bed9fe 1681 *num_offset = run_as_ret.u.extract_sdt_probe_offsets.num_offset;
0ef03255
FD
1682 *offsets = zmalloc(*num_offset * sizeof(uint64_t));
1683 if (!*offsets) {
93bed9fe
JG
1684 ret = -ENOMEM;
1685 goto error;
0ef03255
FD
1686 }
1687
93bed9fe
JG
1688 memcpy(*offsets, run_as_ret.u.extract_sdt_probe_offsets.offsets,
1689 *num_offset * sizeof(uint64_t));
1690error:
1691 return ret;
0ef03255
FD
1692}
1693
7567352f 1694LTTNG_HIDDEN
929f71ec
JG
1695int run_as_create_worker(const char *procname,
1696 post_fork_cleanup_cb clean_up_func,
1697 void *clean_up_user_data)
7567352f 1698{
8fec83ea 1699 int ret;
7567352f 1700
749b7a0c 1701 pthread_mutex_lock(&worker_lock);
929f71ec
JG
1702 ret = run_as_create_worker_no_lock(procname, clean_up_func,
1703 clean_up_user_data);
749b7a0c 1704 pthread_mutex_unlock(&worker_lock);
7567352f
MD
1705 return ret;
1706}
1707
1708LTTNG_HIDDEN
1709void run_as_destroy_worker(void)
1710{
749b7a0c 1711 pthread_mutex_lock(&worker_lock);
a01c682b 1712 run_as_destroy_worker_no_lock();
749b7a0c 1713 pthread_mutex_unlock(&worker_lock);
4628484a 1714}
This page took 0.12992 seconds and 4 git commands to generate.