Clean-up: apply clang-format to the newly added fd-tracker
[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;
65eefd4c 515 uint64_t offset;
241e0a5a 516
93bed9fe
JG
517 ret_value->_error = false;
518 ret = lttng_elf_get_symbol_offset(data->u.extract_elf_symbol_offset.fd,
241e0a5a 519 data->u.extract_elf_symbol_offset.function,
65eefd4c 520 &offset);
241e0a5a
FD
521 if (ret) {
522 DBG("Failed to extract ELF function offset");
523 ret_value->_error = true;
524 }
65eefd4c 525 ret_value->u.extract_elf_symbol_offset.offset = offset;
241e0a5a
FD
526
527 return ret;
528}
529
0ef03255
FD
530static
531int _extract_sdt_probe_offsets(struct run_as_data *data,
532 struct run_as_ret *ret_value)
533{
534 int ret = 0;
535 uint64_t *offsets = NULL;
536 uint32_t num_offset;
537
538 ret_value->_error = false;
539
540 /* On success, this call allocates the offsets paramater. */
93bed9fe
JG
541 ret = lttng_elf_get_sdt_probe_offsets(
542 data->u.extract_sdt_probe_offsets.fd,
0ef03255
FD
543 data->u.extract_sdt_probe_offsets.provider_name,
544 data->u.extract_sdt_probe_offsets.probe_name,
545 &offsets, &num_offset);
546
547 if (ret) {
548 DBG("Failed to extract SDT probe offsets");
549 ret_value->_error = true;
550 goto end;
551 }
552
553 if (num_offset <= 0 || num_offset > LTTNG_KERNEL_MAX_UPROBE_NUM) {
554 DBG("Wrong number of probes.");
555 ret = -1;
556 ret_value->_error = true;
557 goto free_offset;
558 }
559
560 /* Copy the content of the offsets array to the ret struct. */
561 memcpy(ret_value->u.extract_sdt_probe_offsets.offsets,
562 offsets, num_offset * sizeof(uint64_t));
563
564 ret_value->u.extract_sdt_probe_offsets.num_offset = num_offset;
565
566free_offset:
567 free(offsets);
568end:
569 return ret;
570}
b1b34226
MJ
571#else
572static
573int _extract_elf_symbol_offset(struct run_as_data *data,
574 struct run_as_ret *ret_value)
575{
576 ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET");
577 return -1;
578}
579
580static
581int _extract_sdt_probe_offsets(struct run_as_data *data,
582 struct run_as_ret *ret_value)
583{
584 ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS");
585 return -1;
586}
587#endif
241e0a5a 588
7567352f
MD
589static
590run_as_fct run_as_enum_to_fct(enum run_as_cmd cmd)
591{
592 switch (cmd) {
593 case RUN_AS_MKDIR:
18710679
JG
594 case RUN_AS_MKDIRAT:
595 return _mkdirat;
596 case RUN_AS_MKDIR_RECURSIVE:
597 case RUN_AS_MKDIRAT_RECURSIVE:
598 return _mkdirat_recursive;
7567352f 599 case RUN_AS_OPEN:
2912cead 600 case RUN_AS_OPENAT:
7567352f
MD
601 return _open;
602 case RUN_AS_UNLINK:
2912cead 603 case RUN_AS_UNLINKAT:
7567352f 604 return _unlink;
93bed9fe
JG
605 case RUN_AS_RMDIR:
606 case RUN_AS_RMDIRAT:
607 return _rmdir;
7567352f 608 case RUN_AS_RMDIR_RECURSIVE:
93bed9fe 609 case RUN_AS_RMDIRAT_RECURSIVE:
7567352f 610 return _rmdir_recursive;
93bed9fe
JG
611 case RUN_AS_RENAME:
612 case RUN_AS_RENAMEAT:
613 return _rename;
241e0a5a
FD
614 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
615 return _extract_elf_symbol_offset;
0ef03255
FD
616 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
617 return _extract_sdt_probe_offsets;
7567352f 618 default:
62a7b8ed 619 ERR("Unknown command %d", (int) cmd);
7567352f
MD
620 return NULL;
621 }
60b6c79c
MD
622}
623
4628484a 624static
93bed9fe 625int do_send_fds(int sock, const int *fds, unsigned int fd_count)
4628484a 626{
7567352f 627 ssize_t len;
93bed9fe
JG
628 unsigned int i;
629
630 for (i = 0; i < fd_count; i++) {
631 if (fds[i] < 0) {
632 ERR("Attempt to send invalid file descriptor to master (fd = %i)",
633 fds[i]);
634 /* Return 0 as this is not a fatal error. */
635 return 0;
636 }
637 }
4628484a 638
93bed9fe
JG
639 len = lttcomm_send_fds_unix_sock(sock, fds, fd_count);
640 return len < 0 ? -1 : 0;
4628484a
MD
641}
642
643static
93bed9fe 644int do_recv_fds(int sock, int *fds, unsigned int fd_count)
4628484a 645{
93bed9fe
JG
646 int ret = 0;
647 unsigned int i;
7567352f 648 ssize_t len;
4628484a 649
93bed9fe
JG
650 len = lttcomm_recv_fds_unix_sock(sock, fds, fd_count);
651 if (len == 0) {
652 ret = -1;
653 goto end;
da9ee832 654 } else if (len < 0) {
93bed9fe
JG
655 PERROR("Failed to receive file descriptors from socket");
656 ret = -1;
657 goto end;
ca9eb994
JG
658 }
659
93bed9fe
JG
660 for (i = 0; i < fd_count; i++) {
661 if (fds[i] < 0) {
662 ERR("Invalid file descriptor received from worker (fd = %i)", fds[i]);
663 /* Return 0 as this is not a fatal error. */
664 }
665 }
666end:
667 return ret;
4628484a
MD
668}
669
fe9f7760 670static
93bed9fe
JG
671int send_fds_to_worker(const struct run_as_worker *worker,
672 const struct run_as_data *data)
fe9f7760
FD
673{
674 int ret = 0;
93bed9fe 675 unsigned int i;
fe9f7760 676
93bed9fe
JG
677 if (COMMAND_USE_CWD_FD(data) || COMMAND_IN_FD_COUNT(data) == 0) {
678 goto end;
fe9f7760
FD
679 }
680
93bed9fe
JG
681 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
682 if (COMMAND_IN_FDS(data)[i] < 0) {
683 ERR("Refusing to send invalid fd to worker (fd = %i)",
684 COMMAND_IN_FDS(data)[i]);
685 ret = -1;
686 goto end;
687 }
688 }
ca9eb994 689
93bed9fe
JG
690 ret = do_send_fds(worker->sockpair[0], COMMAND_IN_FDS(data),
691 COMMAND_IN_FD_COUNT(data));
fe9f7760 692 if (ret < 0) {
93bed9fe 693 PERROR("Failed to send file descriptor to run-as worker");
fe9f7760 694 ret = -1;
93bed9fe 695 goto end;
fe9f7760 696 }
93bed9fe 697end:
fe9f7760
FD
698 return ret;
699}
700
701static
93bed9fe
JG
702int send_fds_to_master(struct run_as_worker *worker, enum run_as_cmd cmd,
703 struct run_as_ret *run_as_ret)
fe9f7760 704{
93bed9fe
JG
705 int ret = 0;
706 unsigned int i;
fe9f7760 707
93bed9fe
JG
708 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
709 goto end;
fe9f7760
FD
710 }
711
93bed9fe
JG
712 ret = do_send_fds(worker->sockpair[1], COMMAND_OUT_FDS(cmd, run_as_ret),
713 COMMAND_OUT_FD_COUNT(cmd));
fe9f7760 714 if (ret < 0) {
93bed9fe
JG
715 PERROR("Failed to send file descriptor to master process");
716 goto end;
fe9f7760
FD
717 }
718
93bed9fe
JG
719 for (i = 0; i < COMMAND_OUT_FD_COUNT(cmd); i++) {
720 int ret_close = close(COMMAND_OUT_FDS(cmd, run_as_ret)[i]);
ca9eb994 721
93bed9fe
JG
722 if (ret_close < 0) {
723 PERROR("Failed to close result file descriptor");
724 }
725 }
726end:
fe9f7760
FD
727 return ret;
728}
729
730static
93bed9fe
JG
731int recv_fds_from_worker(const struct run_as_worker *worker, enum run_as_cmd cmd,
732 struct run_as_ret *run_as_ret)
fe9f7760
FD
733{
734 int ret = 0;
735
93bed9fe
JG
736 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
737 goto end;
fe9f7760
FD
738 }
739
93bed9fe
JG
740 ret = do_recv_fds(worker->sockpair[0], COMMAND_OUT_FDS(cmd, run_as_ret),
741 COMMAND_OUT_FD_COUNT(cmd));
fe9f7760 742 if (ret < 0) {
93bed9fe 743 PERROR("Failed to receive file descriptor from run-as worker");
fe9f7760
FD
744 ret = -1;
745 }
93bed9fe 746end:
fe9f7760
FD
747 return ret;
748}
749
750static
93bed9fe 751int recv_fds_from_master(struct run_as_worker *worker, struct run_as_data *data)
fe9f7760
FD
752{
753 int ret = 0;
754
93bed9fe
JG
755 if (COMMAND_USE_CWD_FD(data)) {
756 unsigned int i;
757
758 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
759 COMMAND_IN_FDS(data)[i] = AT_FDCWD;
760 }
761 goto end;
fe9f7760
FD
762 }
763
93bed9fe
JG
764 ret = do_recv_fds(worker->sockpair[1], COMMAND_IN_FDS(data),
765 COMMAND_IN_FD_COUNT(data));
fe9f7760 766 if (ret < 0) {
93bed9fe 767 PERROR("Failed to receive file descriptors from master process");
fe9f7760
FD
768 ret = -1;
769 }
93bed9fe 770end:
fe9f7760
FD
771 return ret;
772}
773
774static
93bed9fe 775int cleanup_received_fds(struct run_as_data *data)
fe9f7760 776{
93bed9fe 777 int ret = 0, i;
fe9f7760 778
93bed9fe
JG
779 for (i = 0; i < COMMAND_IN_FD_COUNT(data); i++) {
780 if (COMMAND_IN_FDS(data)[i] == -1) {
781 continue;
782 }
783 ret = close(COMMAND_IN_FDS(data)[i]);
784 if (ret) {
785 PERROR("Failed to close file descriptor received fd in run-as worker");
786 goto end;
787 }
fe9f7760 788 }
93bed9fe 789end:
fe9f7760
FD
790 return ret;
791}
0ef03255 792
7567352f
MD
793/*
794 * Return < 0 on error, 0 if OK, 1 on hangup.
795 */
c2b75c49 796static
7567352f 797int handle_one_cmd(struct run_as_worker *worker)
c2b75c49 798{
7567352f 799 int ret = 0;
93bed9fe
JG
800 struct run_as_data data = {};
801 ssize_t readlen, writelen;
802 struct run_as_ret sendret = {};
803 run_as_fct cmd;
7567352f
MD
804 uid_t prev_euid;
805
fe9f7760
FD
806 /*
807 * Stage 1: Receive run_as_data struct from the master.
808 * The structure contains the command type and all the parameters needed for
809 * its execution
810 */
7567352f
MD
811 readlen = lttcomm_recv_unix_sock(worker->sockpair[1], &data,
812 sizeof(data));
813 if (readlen == 0) {
814 /* hang up */
815 ret = 1;
816 goto end;
817 }
818 if (readlen < sizeof(data)) {
819 PERROR("lttcomm_recv_unix_sock error");
820 ret = -1;
821 goto end;
822 }
c2b75c49 823
7567352f
MD
824 cmd = run_as_enum_to_fct(data.cmd);
825 if (!cmd) {
826 ret = -1;
827 goto end;
828 }
829
fe9f7760
FD
830 /*
831 * Stage 2: Receive file descriptor from master.
832 * Some commands need a file descriptor as input so if it's needed we
833 * receive the fd using the Unix socket.
834 */
93bed9fe 835 ret = recv_fds_from_master(worker, &data);
fe9f7760
FD
836 if (ret < 0) {
837 PERROR("recv_fd_from_master error");
838 ret = -1;
839 goto end;
840 }
841
7567352f
MD
842 prev_euid = getuid();
843 if (data.gid != getegid()) {
844 ret = setegid(data.gid);
1576d582 845 if (ret < 0) {
561f5f2c
JG
846 sendret._error = true;
847 sendret._errno = errno;
4c462e79 848 PERROR("setegid");
6d73c4ef 849 goto write_return;
1576d582 850 }
c2b75c49 851 }
7567352f
MD
852 if (data.uid != prev_euid) {
853 ret = seteuid(data.uid);
1576d582 854 if (ret < 0) {
561f5f2c
JG
855 sendret._error = true;
856 sendret._errno = errno;
4c462e79 857 PERROR("seteuid");
6d73c4ef 858 goto write_return;
1576d582 859 }
c2b75c49 860 }
fe9f7760 861
c2b75c49
MD
862 /*
863 * Also set umask to 0 for mkdir executable bit.
864 */
865 umask(0);
fe9f7760
FD
866
867 /*
868 * Stage 3: Execute the command
869 */
870 ret = (*cmd)(&data, &sendret);
871 if (ret < 0) {
872 DBG("Execution of command returned an error");
873 }
6d73c4ef
MD
874
875write_return:
93bed9fe 876 ret = cleanup_received_fds(&data);
fe9f7760
FD
877 if (ret < 0) {
878 ERR("Error cleaning up FD");
879 goto end;
880 }
881
882 /*
883 * Stage 4: Send run_as_ret structure to the master.
884 * This structure contain the return value of the command and the errno.
885 */
7567352f
MD
886 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
887 sizeof(sendret));
6cd525e8 888 if (writelen < sizeof(sendret)) {
7567352f
MD
889 PERROR("lttcomm_send_unix_sock error");
890 ret = -1;
891 goto end;
892 }
fe9f7760
FD
893
894 /*
93bed9fe 895 * Stage 5: Send resulting file descriptors to the master.
fe9f7760 896 */
93bed9fe 897 ret = send_fds_to_master(worker, data.cmd, &sendret);
fe9f7760
FD
898 if (ret < 0) {
899 DBG("Sending FD to master returned an error");
7567352f
MD
900 goto end;
901 }
fe9f7760 902
7567352f
MD
903 if (seteuid(prev_euid) < 0) {
904 PERROR("seteuid");
905 ret = -1;
906 goto end;
907 }
908 ret = 0;
909end:
910 return ret;
911}
912
913static
914int run_as_worker(struct run_as_worker *worker)
915{
916 int ret;
917 ssize_t writelen;
918 struct run_as_ret sendret;
919 size_t proc_orig_len;
920
921 /*
922 * Initialize worker. Set a different process cmdline.
923 */
924 proc_orig_len = strlen(worker->procname);
925 memset(worker->procname, 0, proc_orig_len);
926 strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len);
927
e1055edb
JG
928 ret = lttng_prctl(PR_SET_NAME,
929 (unsigned long) DEFAULT_RUN_AS_WORKER_NAME, 0, 0, 0);
930 if (ret && ret != -ENOSYS) {
b8090274
JG
931 /* Don't fail as this is not essential. */
932 PERROR("prctl PR_SET_NAME");
6cd525e8 933 }
7567352f 934
fe9f7760
FD
935 memset(&sendret, 0, sizeof(sendret));
936
7567352f
MD
937 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
938 sizeof(sendret));
939 if (writelen < sizeof(sendret)) {
940 PERROR("lttcomm_send_unix_sock error");
941 ret = EXIT_FAILURE;
942 goto end;
943 }
944
945 for (;;) {
946 ret = handle_one_cmd(worker);
947 if (ret < 0) {
948 ret = EXIT_FAILURE;
949 goto end;
950 } else if (ret > 0) {
951 break;
952 } else {
953 continue; /* Next command. */
954 }
955 }
956 ret = EXIT_SUCCESS;
957end:
958 return ret;
c2b75c49
MD
959}
960
60b6c79c 961static
7567352f
MD
962int run_as_cmd(struct run_as_worker *worker,
963 enum run_as_cmd cmd,
964 struct run_as_data *data,
fe9f7760 965 struct run_as_ret *ret_value,
7567352f 966 uid_t uid, gid_t gid)
60b6c79c 967{
fe9f7760 968 int ret = 0;
7567352f 969 ssize_t readlen, writelen;
60b6c79c
MD
970
971 /*
972 * If we are non-root, we can only deal with our own uid.
973 */
974 if (geteuid() != 0) {
975 if (uid != geteuid()) {
fe9f7760
FD
976 ret = -1;
977 ret_value->_errno = EPERM;
60b6c79c 978 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
08797918 979 (int) uid, (int) geteuid());
df5b86c8 980 goto end;
60b6c79c 981 }
60b6c79c
MD
982 }
983
7567352f
MD
984 data->cmd = cmd;
985 data->uid = uid;
986 data->gid = gid;
987
fe9f7760
FD
988 /*
989 * Stage 1: Send the run_as_data struct to the worker process
990 */
7567352f
MD
991 writelen = lttcomm_send_unix_sock(worker->sockpair[0], data,
992 sizeof(*data));
993 if (writelen < sizeof(*data)) {
994 PERROR("Error writing message to run_as");
fe9f7760
FD
995 ret = -1;
996 ret_value->_errno = EIO;
60b6c79c 997 goto end;
c2b75c49 998 }
7567352f 999
fe9f7760
FD
1000 /*
1001 * Stage 2: Send file descriptor to the worker process if needed
1002 */
93bed9fe 1003 ret = send_fds_to_worker(worker, data);
fe9f7760
FD
1004 if (ret) {
1005 PERROR("do_send_fd error");
1006 ret = -1;
1007 ret_value->_errno = EIO;
1008 goto end;
1009 }
1010
1011 /*
1012 * Stage 3: Wait for the execution of the command
1013 */
1014
1015 /*
1016 * Stage 4: Receive the run_as_ret struct containing the return value and
1017 * errno
1018 */
1019 readlen = lttcomm_recv_unix_sock(worker->sockpair[0], ret_value,
1020 sizeof(*ret_value));
da9ee832
JG
1021 if (!readlen) {
1022 ERR("Run-as worker has hung-up during run_as_cmd");
fe9f7760
FD
1023 ret = -1;
1024 ret_value->_errno = EIO;
da9ee832 1025 goto end;
fe9f7760 1026 } else if (readlen < sizeof(*ret_value)) {
7567352f 1027 PERROR("Error reading response from run_as");
fe9f7760
FD
1028 ret = -1;
1029 ret_value->_errno = errno;
033b58a7 1030 goto end;
6cd525e8 1031 }
fe9f7760 1032
ca9eb994
JG
1033 if (ret_value->_error) {
1034 /* Skip stage 5 on error as there will be no fd to receive. */
1035 goto end;
1036 }
1037
fe9f7760
FD
1038 /*
1039 * Stage 5: Receive file descriptor if needed
1040 */
93bed9fe 1041 ret = recv_fds_from_worker(worker, cmd, ret_value);
fe9f7760
FD
1042 if (ret < 0) {
1043 ERR("Error receiving fd");
1044 ret = -1;
1045 ret_value->_errno = EIO;
4c462e79 1046 }
7567352f 1047
60b6c79c 1048end:
fe9f7760 1049 return ret;
60b6c79c
MD
1050}
1051
2d85a600 1052/*
7567352f 1053 * This is for debugging ONLY and should not be considered secure.
2d85a600
MD
1054 */
1055static
fe9f7760
FD
1056int run_as_noworker(enum run_as_cmd cmd,
1057 struct run_as_data *data, struct run_as_ret *ret_value,
1058 uid_t uid, gid_t gid)
2d85a600 1059{
df5b86c8 1060 int ret, saved_errno;
5b73926f 1061 mode_t old_mask;
7567352f 1062 run_as_fct fct;
5b73926f 1063
7567352f
MD
1064 fct = run_as_enum_to_fct(cmd);
1065 if (!fct) {
1066 errno = -ENOSYS;
1067 ret = -1;
1068 goto end;
1069 }
5b73926f 1070 old_mask = umask(0);
fe9f7760
FD
1071 ret = fct(data, ret_value);
1072 saved_errno = ret_value->_errno;
5b73926f 1073 umask(old_mask);
df5b86c8 1074 errno = saved_errno;
7567352f 1075end:
5b73926f 1076 return ret;
2d85a600
MD
1077}
1078
8fec83ea
JG
1079static
1080int reset_sighandler(void)
1081{
1082 int sig;
1083
1084 DBG("Resetting run_as worker signal handlers to default");
1085 for (sig = 1; sig <= 31; sig++) {
1086 (void) signal(sig, SIG_DFL);
1087 }
1088 return 0;
1089}
1090
1091static
1092void worker_sighandler(int sig)
1093{
1094 const char *signame;
1095
1096 /*
1097 * The worker will inherit its parent's signals since they are part of
1098 * the same process group. However, in the case of SIGINT and SIGTERM,
1099 * we want to give the worker a chance to teardown gracefully when its
1100 * parent closes the command socket.
1101 */
1102 switch (sig) {
1103 case SIGINT:
1104 signame = "SIGINT";
1105 break;
1106 case SIGTERM:
1107 signame = "SIGTERM";
1108 break;
1109 default:
1110 signame = NULL;
1111 }
1112
1113 if (signame) {
1114 DBG("run_as worker received signal %s", signame);
1115 } else {
1116 DBG("run_as_worker received signal %d", sig);
1117 }
1118}
1119
1120static
1121int set_worker_sighandlers(void)
1122{
1123 int ret = 0;
1124 sigset_t sigset;
1125 struct sigaction sa;
1126
1127 if ((ret = sigemptyset(&sigset)) < 0) {
1128 PERROR("sigemptyset");
1129 goto end;
1130 }
1131
1132 sa.sa_handler = worker_sighandler;
1133 sa.sa_mask = sigset;
1134 sa.sa_flags = 0;
1135 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
1136 PERROR("sigaction SIGINT");
1137 goto end;
1138 }
1139
1140 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
1141 PERROR("sigaction SIGTERM");
1142 goto end;
1143 }
1144
1145 DBG("run_as signal handler set for SIGTERM and SIGINT");
1146end:
1147 return ret;
1148}
1149
1150static
929f71ec
JG
1151int run_as_create_worker_no_lock(const char *procname,
1152 post_fork_cleanup_cb clean_up_func,
1153 void *clean_up_user_data)
8fec83ea
JG
1154{
1155 pid_t pid;
1156 int i, ret = 0;
1157 ssize_t readlen;
1158 struct run_as_ret recvret;
1159 struct run_as_worker *worker;
1160
1161 assert(!global_worker);
1162 if (!use_clone()) {
1163 /*
1164 * Don't initialize a worker, all run_as tasks will be performed
1165 * in the current process.
1166 */
1167 ret = 0;
1168 goto end;
1169 }
1170 worker = zmalloc(sizeof(*worker));
1171 if (!worker) {
1172 ret = -ENOMEM;
1173 goto end;
1174 }
1175 worker->procname = strdup(procname);
1176 if (!worker->procname) {
1177 ret = -ENOMEM;
8c96eded 1178 goto error_procname_alloc;
8fec83ea
JG
1179 }
1180 /* Create unix socket. */
1181 if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) {
1182 ret = -1;
1183 goto error_sock;
1184 }
1185
1186 /* Fork worker. */
1187 pid = fork();
1188 if (pid < 0) {
1189 PERROR("fork");
1190 ret = -1;
1191 goto error_fork;
1192 } else if (pid == 0) {
1193 /* Child */
1194
1195 reset_sighandler();
1196
1197 set_worker_sighandlers();
929f71ec
JG
1198 if (clean_up_func) {
1199 if (clean_up_func(clean_up_user_data) < 0) {
1200 ERR("Run-as post-fork clean-up failed, exiting.");
1201 exit(EXIT_FAILURE);
1202 }
1203 }
8fec83ea
JG
1204
1205 /* Just close, no shutdown. */
1206 if (close(worker->sockpair[0])) {
1207 PERROR("close");
1208 exit(EXIT_FAILURE);
1209 }
1210
1211 /*
1212 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
1213 * Sockpair[1] is used as a control channel with the master
1214 */
1215 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
1216 if (i != worker->sockpair[1]) {
1217 (void) close(i);
1218 }
1219 }
1220
1221 worker->sockpair[0] = -1;
1222 ret = run_as_worker(worker);
1223 if (lttcomm_close_unix_sock(worker->sockpair[1])) {
1224 PERROR("close");
1225 ret = -1;
1226 }
1227 worker->sockpair[1] = -1;
340cf672
JG
1228 free(worker->procname);
1229 free(worker);
8fec83ea
JG
1230 LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret);
1231 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
1232 } else {
1233 /* Parent */
1234
1235 /* Just close, no shutdown. */
1236 if (close(worker->sockpair[1])) {
1237 PERROR("close");
1238 ret = -1;
1239 goto error_fork;
1240 }
1241 worker->sockpair[1] = -1;
1242 worker->pid = pid;
1243 /* Wait for worker to become ready. */
1244 readlen = lttcomm_recv_unix_sock(worker->sockpair[0],
1245 &recvret, sizeof(recvret));
1246 if (readlen < sizeof(recvret)) {
1247 ERR("readlen: %zd", readlen);
1248 PERROR("Error reading response from run_as at creation");
1249 ret = -1;
1250 goto error_fork;
1251 }
1252 global_worker = worker;
1253 }
1254end:
1255 return ret;
1256
1257 /* Error handling. */
1258error_fork:
1259 for (i = 0; i < 2; i++) {
1260 if (worker->sockpair[i] < 0) {
1261 continue;
1262 }
1263 if (lttcomm_close_unix_sock(worker->sockpair[i])) {
1264 PERROR("close");
1265 }
1266 worker->sockpair[i] = -1;
1267 }
1268error_sock:
8c96eded
FD
1269 free(worker->procname);
1270error_procname_alloc:
8fec83ea
JG
1271 free(worker);
1272 return ret;
1273}
1274
a01c682b
JR
1275static
1276void run_as_destroy_worker_no_lock(void)
1277{
1278 struct run_as_worker *worker = global_worker;
1279
1280 DBG("Destroying run_as worker");
1281 if (!worker) {
1282 return;
1283 }
1284 /* Close unix socket */
1285 DBG("Closing run_as worker socket");
1286 if (lttcomm_close_unix_sock(worker->sockpair[0])) {
1287 PERROR("close");
1288 }
1289 worker->sockpair[0] = -1;
1290 /* Wait for worker. */
1291 for (;;) {
1292 int status;
1293 pid_t wait_ret;
1294
1295 wait_ret = waitpid(worker->pid, &status, 0);
1296 if (wait_ret < 0) {
1297 if (errno == EINTR) {
1298 continue;
1299 }
1300 PERROR("waitpid");
1301 break;
1302 }
1303
1304 if (WIFEXITED(status)) {
1305 LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR,
1306 DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d",
1307 WEXITSTATUS(status));
1308 break;
1309 } else if (WIFSIGNALED(status)) {
1310 ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d",
1311 WTERMSIG(status));
1312 break;
1313 }
1314 }
1315 free(worker->procname);
1316 free(worker);
1317 global_worker = NULL;
1318}
1319
2d85a600 1320static
fe9f7760 1321int run_as_restart_worker(struct run_as_worker *worker)
2d85a600 1322{
fe9f7760
FD
1323 int ret = 0;
1324 char *procname = NULL;
1325
1326 procname = worker->procname;
1327
1328 /* Close socket to run_as worker process and clean up the zombie process */
a01c682b 1329 run_as_destroy_worker_no_lock();
fe9f7760
FD
1330
1331 /* Create a new run_as worker process*/
929f71ec 1332 ret = run_as_create_worker_no_lock(procname, NULL, NULL);
fe9f7760
FD
1333 if (ret < 0 ) {
1334 ERR("Restarting the worker process failed");
1335 ret = -1;
1336 goto err;
1337 }
1338err:
1339 return ret;
1340}
1341
1342static
1343int run_as(enum run_as_cmd cmd, struct run_as_data *data,
1344 struct run_as_ret *ret_value, uid_t uid, gid_t gid)
1345{
1346 int ret, saved_errno;
7567352f 1347
8fec83ea 1348 pthread_mutex_lock(&worker_lock);
749b7a0c 1349 if (use_clone()) {
7567352f 1350 DBG("Using run_as worker");
8fec83ea 1351
749b7a0c 1352 assert(global_worker);
749b7a0c 1353
fe9f7760
FD
1354 ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid);
1355 saved_errno = ret_value->_errno;
1356
fe9f7760
FD
1357 /*
1358 * If the worker thread crashed the errno is set to EIO. we log
1359 * the error and start a new worker process.
1360 */
1361 if (ret == -1 && saved_errno == EIO) {
1362 DBG("Socket closed unexpectedly... "
1363 "Restarting the worker process");
1364 ret = run_as_restart_worker(global_worker);
fe9f7760
FD
1365 if (ret == -1) {
1366 ERR("Failed to restart worker process.");
1367 goto err;
1368 }
1369 }
2d85a600 1370 } else {
7567352f 1371 DBG("Using run_as without worker");
fe9f7760 1372 ret = run_as_noworker(cmd, data, ret_value, uid, gid);
2d85a600 1373 }
fe9f7760 1374err:
8fec83ea 1375 pthread_mutex_unlock(&worker_lock);
7567352f 1376 return ret;
2d85a600
MD
1377}
1378
90e535ef 1379LTTNG_HIDDEN
e11d277b 1380int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 1381{
18710679
JG
1382 return run_as_mkdirat_recursive(AT_FDCWD, path, mode, uid, gid);
1383}
1384
1385LTTNG_HIDDEN
1386int run_as_mkdirat_recursive(int dirfd, const char *path, mode_t mode,
1387 uid_t uid, gid_t gid)
1388{
1389 int ret;
93bed9fe
JG
1390 struct run_as_data data = {};
1391 struct run_as_ret run_as_ret = {};
60b6c79c 1392
18710679
JG
1393 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1394 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1395 path, (int) mode, (int) uid, (int) gid);
93bed9fe
JG
1396 ret = lttng_strncpy(data.u.mkdir.path, path,
1397 sizeof(data.u.mkdir.path));
18710679
JG
1398 if (ret) {
1399 ERR("Failed to copy path argument of mkdirat recursive command");
1400 goto error;
1401 }
93bed9fe
JG
1402 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1403 data.u.mkdir.mode = mode;
1404 data.u.mkdir.dirfd = dirfd;
18710679
JG
1405 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR_RECURSIVE : RUN_AS_MKDIRAT_RECURSIVE,
1406 &data, &run_as_ret, uid, gid);
1407 errno = run_as_ret._errno;
93bed9fe 1408 ret = run_as_ret.u.ret;
18710679
JG
1409error:
1410 return ret;
60b6c79c
MD
1411}
1412
90e535ef 1413LTTNG_HIDDEN
e11d277b 1414int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 1415{
18710679
JG
1416 return run_as_mkdirat(AT_FDCWD, path, mode, uid, gid);
1417}
1418
1419LTTNG_HIDDEN
1420int run_as_mkdirat(int dirfd, const char *path, mode_t mode,
1421 uid_t uid, gid_t gid)
1422{
1423 int ret;
93bed9fe
JG
1424 struct run_as_data data = {};
1425 struct run_as_ret run_as_ret = {};
fe9f7760 1426
18710679
JG
1427 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1428 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1429 path, (int) mode, (int) uid, (int) gid);
93bed9fe
JG
1430 ret = lttng_strncpy(data.u.mkdir.path, path,
1431 sizeof(data.u.mkdir.path));
18710679
JG
1432 if (ret) {
1433 ERR("Failed to copy path argument of mkdirat command");
1434 goto error;
1435 }
93bed9fe
JG
1436 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1437 data.u.mkdir.mode = mode;
1438 data.u.mkdir.dirfd = dirfd;
18710679
JG
1439 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR : RUN_AS_MKDIRAT,
1440 &data, &run_as_ret, uid, gid);
1441 errno = run_as_ret._errno;
93bed9fe 1442 ret = run_as_ret.u.ret;
18710679
JG
1443error:
1444 return ret;
60b6c79c
MD
1445}
1446
90e535ef 1447LTTNG_HIDDEN
2912cead
JG
1448int run_as_open(const char *path, int flags, mode_t mode, uid_t uid,
1449 gid_t gid)
1450{
1451 return run_as_openat(AT_FDCWD, path, flags, mode, uid, gid);
1452}
1453
1454LTTNG_HIDDEN
1455int run_as_openat(int dirfd, const char *path, int flags, mode_t mode,
1456 uid_t uid, gid_t gid)
60b6c79c 1457{
93bed9fe
JG
1458 int ret;
1459 struct run_as_data data = {};
1460 struct run_as_ret run_as_ret = {};
fe9f7760 1461
2912cead
JG
1462 DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
1463 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1464 path, flags, (int) mode, (int) uid, (int) gid);
93bed9fe
JG
1465 ret = lttng_strncpy(data.u.open.path, path, sizeof(data.u.open.path));
1466 if (ret) {
1467 ERR("Failed to copy path argument of open command");
1468 goto error;
1469 }
7567352f
MD
1470 data.u.open.flags = flags;
1471 data.u.open.mode = mode;
93bed9fe 1472 data.u.open.dirfd = dirfd;
2912cead 1473 run_as(dirfd == AT_FDCWD ? RUN_AS_OPEN : RUN_AS_OPENAT,
93bed9fe
JG
1474 &data, &run_as_ret, uid, gid);
1475 errno = run_as_ret._errno;
1476 ret = run_as_ret.u.ret < 0 ? run_as_ret.u.ret :
1477 run_as_ret.u.open.fd;
1478error:
1479 return ret;
60b6c79c 1480}
4628484a
MD
1481
1482LTTNG_HIDDEN
1483int run_as_unlink(const char *path, uid_t uid, gid_t gid)
2912cead
JG
1484{
1485 return run_as_unlinkat(AT_FDCWD, path, uid, gid);
1486}
1487
1488LTTNG_HIDDEN
1489int run_as_unlinkat(int dirfd, const char *path, uid_t uid, gid_t gid)
4628484a 1490{
93bed9fe
JG
1491 int ret;
1492 struct run_as_data data = {};
1493 struct run_as_ret run_as_ret = {};
fe9f7760 1494
2912cead
JG
1495 DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
1496 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1497 path, (int) uid, (int) gid);
93bed9fe
JG
1498 ret = lttng_strncpy(data.u.unlink.path, path,
1499 sizeof(data.u.unlink.path));
1500 if (ret) {
1501 goto error;
1502 }
1503 data.u.unlink.dirfd = dirfd;
1504 run_as(dirfd == AT_FDCWD ? RUN_AS_UNLINK : RUN_AS_UNLINKAT, &data,
1505 &run_as_ret, uid, gid);
1506 errno = run_as_ret._errno;
1507 ret = run_as_ret.u.ret;
1508error:
1509 return ret;
1510}
1511
1512LTTNG_HIDDEN
1513int run_as_rmdir(const char *path, uid_t uid, gid_t gid)
1514{
1515 return run_as_rmdirat(AT_FDCWD, path, uid, gid);
1516}
1517
1518LTTNG_HIDDEN
1519int run_as_rmdirat(int dirfd, const char *path, uid_t uid, gid_t gid)
1520{
1521 int ret;
1522 struct run_as_data data = {};
1523 struct run_as_ret run_as_ret = {};
1524
1525 DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d",
1526 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1527 path, (int) uid, (int) gid);
1528 ret = lttng_strncpy(data.u.rmdir.path, path,
1529 sizeof(data.u.rmdir.path));
1530 if (ret) {
1531 goto error;
1532 }
1533 data.u.rmdir.dirfd = dirfd;
1534 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR : RUN_AS_RMDIRAT, &data,
1535 &run_as_ret, uid, gid);
1536 errno = run_as_ret._errno;
1537 ret = run_as_ret.u.ret;
1538error:
1539 return ret;
4628484a
MD
1540}
1541
1542LTTNG_HIDDEN
f75c5439 1543int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid, int flags)
4628484a 1544{
f75c5439 1545 return run_as_rmdirat_recursive(AT_FDCWD, path, uid, gid, flags);
93bed9fe 1546}
fe9f7760 1547
93bed9fe 1548LTTNG_HIDDEN
f75c5439 1549int run_as_rmdirat_recursive(int dirfd, const char *path, uid_t uid, gid_t gid, int flags)
93bed9fe
JG
1550{
1551 int ret;
1552 struct run_as_data data = {};
1553 struct run_as_ret run_as_ret = {};
4628484a 1554
93bed9fe
JG
1555 DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d",
1556 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1557 path, (int) uid, (int) gid);
93bed9fe
JG
1558 ret = lttng_strncpy(data.u.rmdir.path, path,
1559 sizeof(data.u.rmdir.path));
1560 if (ret) {
1561 goto error;
1562 }
1563 data.u.rmdir.dirfd = dirfd;
f75c5439 1564 data.u.rmdir.flags = flags;
93bed9fe
JG
1565 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR_RECURSIVE : RUN_AS_RMDIRAT_RECURSIVE,
1566 &data, &run_as_ret, uid, gid);
1567 errno = run_as_ret._errno;
1568 ret = run_as_ret.u.ret;
1569error:
1570 return ret;
1571}
1572
1573LTTNG_HIDDEN
1574int run_as_rename(const char *old, const char *new, uid_t uid, gid_t gid)
1575{
1576 return run_as_renameat(AT_FDCWD, old, AT_FDCWD, new, uid, gid);
1577}
1578
1579LTTNG_HIDDEN
1580int run_as_renameat(int old_dirfd, const char *old_name,
1581 int new_dirfd, const char *new_name, uid_t uid, gid_t gid)
1582{
1583 int ret;
1584 struct run_as_data data = {};
1585 struct run_as_ret run_as_ret = {};
1586
1587 DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d",
1588 old_dirfd, old_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1589 old_name,
1590 new_dirfd, new_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1591 new_name, (int) uid, (int) gid);
1592 ret = lttng_strncpy(data.u.rename.old_path, old_name,
1593 sizeof(data.u.rename.old_path));
1594 if (ret) {
1595 goto error;
1596 }
1597 ret = lttng_strncpy(data.u.rename.new_path, new_name,
1598 sizeof(data.u.rename.new_path));
1599 if (ret) {
1600 goto error;
1601 }
1602
1603 data.u.rename.dirfds[0] = old_dirfd;
1604 data.u.rename.dirfds[1] = new_dirfd;
1605 run_as(old_dirfd == AT_FDCWD && new_dirfd == AT_FDCWD ?
1606 RUN_AS_RENAME : RUN_AS_RENAMEAT,
1607 &data, &run_as_ret, uid, gid);
1608 errno = run_as_ret._errno;
1609 ret = run_as_ret.u.ret;
1610error:
1611 return ret;
7567352f
MD
1612}
1613
241e0a5a
FD
1614LTTNG_HIDDEN
1615int run_as_extract_elf_symbol_offset(int fd, const char* function,
1616 uid_t uid, gid_t gid, uint64_t *offset)
1617{
93bed9fe
JG
1618 int ret;
1619 struct run_as_data data = {};
1620 struct run_as_ret run_as_ret = {};
f726677b 1621
241e0a5a 1622 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
93bed9fe
JG
1623 "with for uid %d and gid %d", fd, function,
1624 (int) uid, (int) gid);
241e0a5a 1625
93bed9fe 1626 data.u.extract_elf_symbol_offset.fd = fd;
241e0a5a
FD
1627
1628 strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1);
241e0a5a 1629 data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
93bed9fe
JG
1630 ret = lttng_strncpy(data.u.extract_elf_symbol_offset.function,
1631 function,
1632 sizeof(data.u.extract_elf_symbol_offset.function));
1633 if (ret) {
1634 goto error;
1635 }
241e0a5a 1636
93bed9fe
JG
1637 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &run_as_ret, uid, gid);
1638 errno = run_as_ret._errno;
1639 if (run_as_ret._error) {
1640 ret = -1;
1641 goto error;
241e0a5a
FD
1642 }
1643
93bed9fe
JG
1644 *offset = run_as_ret.u.extract_elf_symbol_offset.offset;
1645error:
1646 return ret;
241e0a5a
FD
1647}
1648
0ef03255
FD
1649LTTNG_HIDDEN
1650int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name,
1651 const char* probe_name, uid_t uid, gid_t gid,
1652 uint64_t **offsets, uint32_t *num_offset)
1653{
93bed9fe
JG
1654 int ret;
1655 struct run_as_data data = {};
1656 struct run_as_ret run_as_ret = {};
f726677b 1657
0ef03255 1658 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
93bed9fe
JG
1659 "provider_name=%s with for uid %d and gid %d", fd,
1660 probe_name, provider_name, (int) uid, (int) gid);
0ef03255 1661
93bed9fe 1662 data.u.extract_sdt_probe_offsets.fd = fd;
0ef03255 1663
93bed9fe
JG
1664 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name,
1665 sizeof(data.u.extract_sdt_probe_offsets.probe_name));
1666 if (ret) {
1667 goto error;
1668 }
1669 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.provider_name,
1670 provider_name,
1671 sizeof(data.u.extract_sdt_probe_offsets.provider_name));
1672 if (ret) {
1673 goto error;
0ef03255
FD
1674 }
1675
93bed9fe
JG
1676 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &run_as_ret, uid, gid);
1677 errno = run_as_ret._errno;
1678 if (run_as_ret._error) {
1679 ret = -1;
1680 goto error;
1681 }
0ef03255 1682
93bed9fe 1683 *num_offset = run_as_ret.u.extract_sdt_probe_offsets.num_offset;
0ef03255
FD
1684 *offsets = zmalloc(*num_offset * sizeof(uint64_t));
1685 if (!*offsets) {
93bed9fe
JG
1686 ret = -ENOMEM;
1687 goto error;
0ef03255
FD
1688 }
1689
93bed9fe
JG
1690 memcpy(*offsets, run_as_ret.u.extract_sdt_probe_offsets.offsets,
1691 *num_offset * sizeof(uint64_t));
1692error:
1693 return ret;
0ef03255
FD
1694}
1695
7567352f 1696LTTNG_HIDDEN
929f71ec
JG
1697int run_as_create_worker(const char *procname,
1698 post_fork_cleanup_cb clean_up_func,
1699 void *clean_up_user_data)
7567352f 1700{
8fec83ea 1701 int ret;
7567352f 1702
749b7a0c 1703 pthread_mutex_lock(&worker_lock);
929f71ec
JG
1704 ret = run_as_create_worker_no_lock(procname, clean_up_func,
1705 clean_up_user_data);
749b7a0c 1706 pthread_mutex_unlock(&worker_lock);
7567352f
MD
1707 return ret;
1708}
1709
1710LTTNG_HIDDEN
1711void run_as_destroy_worker(void)
1712{
749b7a0c 1713 pthread_mutex_lock(&worker_lock);
a01c682b 1714 run_as_destroy_worker_no_lock();
749b7a0c 1715 pthread_mutex_unlock(&worker_lock);
4628484a 1716}
This page took 0.226913 seconds and 4 git commands to generate.