Fix: include stdlib.h in compat/string.h
[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>
fcde531a 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
fcde531a
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
fcde531a
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 {
fcde531a
JG
79 int dirfd;
80 char path[LTTNG_PATH_MAX];
60b6c79c
MD
81 int flags;
82 mode_t mode;
fcde531a 83} LTTNG_PACKED;
60b6c79c 84
4628484a 85struct run_as_unlink_data {
fcde531a
JG
86 int dirfd;
87 char path[LTTNG_PATH_MAX];
88} LTTNG_PACKED;
4628484a 89
fcde531a
JG
90struct run_as_rmdir_data {
91 int dirfd;
92 char path[LTTNG_PATH_MAX];
58ec7253 93 int flags; /* enum lttng_directory_handle_rmdir_recursive_flags */
fcde531a 94} LTTNG_PACKED;
7567352f 95
241e0a5a 96struct run_as_extract_elf_symbol_offset_data {
fcde531a 97 int fd;
241e0a5a 98 char function[LTTNG_SYMBOL_NAME_LEN];
fcde531a 99} LTTNG_PACKED;
241e0a5a 100
0ef03255 101struct run_as_extract_sdt_probe_offsets_data {
fcde531a 102 int fd;
0ef03255
FD
103 char probe_name[LTTNG_SYMBOL_NAME_LEN];
104 char provider_name[LTTNG_SYMBOL_NAME_LEN];
fcde531a 105} LTTNG_PACKED;
0ef03255 106
fcde531a
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 {
fcde531a
JG
118 int fd;
119} LTTNG_PACKED;
fe9f7760 120
241e0a5a
FD
121struct run_as_extract_elf_symbol_offset_ret {
122 uint64_t offset;
fcde531a 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];
fcde531a 128} LTTNG_PACKED;
7567352f
MD
129
130struct run_as_data {
131 enum run_as_cmd cmd;
132 union {
fcde531a 133 struct run_as_mkdir_data mkdir;
7567352f
MD
134 struct run_as_open_data open;
135 struct run_as_unlink_data unlink;
fcde531a
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;
fcde531a 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 {
fcde531a 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;
fcde531a
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
a42089bd 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;
a42089bd 354 struct lttng_directory_handle handle;
60b6c79c 355
fcde531a
JG
356 path = data->u.mkdir.path;
357 mode = data->u.mkdir.mode;
60b6c79c 358
fcde531a
JG
359 (void) lttng_directory_handle_init_from_dirfd(&handle,
360 data->u.mkdir.dirfd);
e1ddb036 361 /* Ownership of dirfd is transferred to the handle. */
fcde531a 362 data->u.mkdir.dirfd = -1;
d77dded2 363 /* Safe to call as we have transitioned to the requested uid/gid. */
fcde531a 364 ret_value->u.ret =
a42089bd
JG
365 lttng_directory_handle_create_subdirectory_recursive(
366 &handle, path, mode);
fe9f7760 367 ret_value->_errno = errno;
fcde531a 368 ret_value->_error = (ret_value->u.ret) ? true : false;
a42089bd 369 lttng_directory_handle_fini(&handle);
fcde531a 370 return ret_value->u.ret;
60b6c79c
MD
371}
372
373static
a42089bd 374int _mkdirat(struct run_as_data *data, struct run_as_ret *ret_value)
60b6c79c 375{
a42089bd
JG
376 const char *path;
377 mode_t mode;
378 struct lttng_directory_handle handle;
379
fcde531a
JG
380 path = data->u.mkdir.path;
381 mode = data->u.mkdir.mode;
a42089bd 382
fcde531a
JG
383 (void) lttng_directory_handle_init_from_dirfd(&handle,
384 data->u.mkdir.dirfd);
e1ddb036 385 /* Ownership of dirfd is transferred to the handle. */
fcde531a 386 data->u.mkdir.dirfd = -1;
a42089bd 387 /* Safe to call as we have transitioned to the requested uid/gid. */
fcde531a 388 ret_value->u.ret =
a42089bd
JG
389 lttng_directory_handle_create_subdirectory(
390 &handle, path, mode);
fe9f7760 391 ret_value->_errno = errno;
fcde531a 392 ret_value->_error = (ret_value->u.ret) ? true : false;
a42089bd 393 lttng_directory_handle_fini(&handle);
fcde531a 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{
fcde531a
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;
fcde531a
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{
fcde531a
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;
fcde531a
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{
fcde531a
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(
58ec7253 475 &handle, data->u.rmdir.path, data->u.rmdir.flags);
fe9f7760 476 ret_value->_errno = errno;
fcde531a
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
df458f8c 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;
ab4b3982 515 uint64_t offset;
241e0a5a 516
fcde531a
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,
ab4b3982 520 &offset);
241e0a5a
FD
521 if (ret) {
522 DBG("Failed to extract ELF function offset");
523 ret_value->_error = true;
524 }
ab4b3982 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. */
fcde531a
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}
df458f8c
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:
a42089bd
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:
a4ee1b7e 600 case RUN_AS_OPENAT:
7567352f
MD
601 return _open;
602 case RUN_AS_UNLINK:
a4ee1b7e 603 case RUN_AS_UNLINKAT:
7567352f 604 return _unlink;
fcde531a
JG
605 case RUN_AS_RMDIR:
606 case RUN_AS_RMDIRAT:
607 return _rmdir;
7567352f 608 case RUN_AS_RMDIR_RECURSIVE:
fcde531a 609 case RUN_AS_RMDIRAT_RECURSIVE:
7567352f 610 return _rmdir_recursive;
fcde531a
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
fcde531a 625int do_send_fds(int sock, const int *fds, unsigned int fd_count)
4628484a 626{
7567352f 627 ssize_t len;
fcde531a
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
fcde531a
JG
639 len = lttcomm_send_fds_unix_sock(sock, fds, fd_count);
640 return len < 0 ? -1 : 0;
4628484a
MD
641}
642
643static
fcde531a 644int do_recv_fds(int sock, int *fds, unsigned int fd_count)
4628484a 645{
fcde531a
JG
646 int ret = 0;
647 unsigned int i;
7567352f 648 ssize_t len;
4628484a 649
fcde531a
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) {
fcde531a
JG
655 PERROR("Failed to receive file descriptors from socket");
656 ret = -1;
657 goto end;
38069abf
JG
658 }
659
fcde531a
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
fcde531a
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;
fcde531a 675 unsigned int i;
fe9f7760 676
fcde531a
JG
677 if (COMMAND_USE_CWD_FD(data) || COMMAND_IN_FD_COUNT(data) == 0) {
678 goto end;
fe9f7760
FD
679 }
680
fcde531a
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 }
38069abf 689
fcde531a
JG
690 ret = do_send_fds(worker->sockpair[0], COMMAND_IN_FDS(data),
691 COMMAND_IN_FD_COUNT(data));
fe9f7760 692 if (ret < 0) {
fcde531a 693 PERROR("Failed to send file descriptor to run-as worker");
fe9f7760 694 ret = -1;
fcde531a 695 goto end;
fe9f7760 696 }
fcde531a 697end:
fe9f7760
FD
698 return ret;
699}
700
701static
fcde531a
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{
fcde531a
JG
705 int ret = 0;
706 unsigned int i;
fe9f7760 707
fcde531a
JG
708 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
709 goto end;
fe9f7760
FD
710 }
711
fcde531a
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) {
fcde531a
JG
715 PERROR("Failed to send file descriptor to master process");
716 goto end;
fe9f7760
FD
717 }
718
fcde531a
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]);
38069abf 721
fcde531a
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
fcde531a
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
fcde531a
JG
736 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
737 goto end;
fe9f7760
FD
738 }
739
fcde531a
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) {
fcde531a 743 PERROR("Failed to receive file descriptor from run-as worker");
fe9f7760
FD
744 ret = -1;
745 }
fcde531a 746end:
fe9f7760
FD
747 return ret;
748}
749
750static
fcde531a 751int recv_fds_from_master(struct run_as_worker *worker, struct run_as_data *data)
fe9f7760
FD
752{
753 int ret = 0;
754
fcde531a
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
fcde531a
JG
764 ret = do_recv_fds(worker->sockpair[1], COMMAND_IN_FDS(data),
765 COMMAND_IN_FD_COUNT(data));
fe9f7760 766 if (ret < 0) {
fcde531a 767 PERROR("Failed to receive file descriptors from master process");
fe9f7760
FD
768 ret = -1;
769 }
fcde531a 770end:
fe9f7760
FD
771 return ret;
772}
773
774static
fcde531a 775int cleanup_received_fds(struct run_as_data *data)
fe9f7760 776{
fcde531a 777 int ret = 0, i;
fe9f7760 778
fcde531a
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 }
fcde531a 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;
fcde531a
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 */
fcde531a 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:
fcde531a 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 /*
fcde531a 895 * Stage 5: Send resulting file descriptors to the master.
fe9f7760 896 */
fcde531a 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 */
fcde531a 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
38069abf
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 */
fcde531a 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
cd9251b8
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
5bedd986
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)
cd9251b8
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;
8b4bc28a 1178 goto error_procname_alloc;
cd9251b8
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();
5bedd986
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 }
cd9251b8
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;
547aabb4
JG
1228 free(worker->procname);
1229 free(worker);
cd9251b8
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:
8b4bc28a
FD
1269 free(worker->procname);
1270error_procname_alloc:
cd9251b8
JG
1271 free(worker);
1272 return ret;
1273}
1274
3340c2a1
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 */
3340c2a1 1329 run_as_destroy_worker_no_lock();
fe9f7760
FD
1330
1331 /* Create a new run_as worker process*/
5bedd986 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
cd9251b8 1348 pthread_mutex_lock(&worker_lock);
749b7a0c 1349 if (use_clone()) {
7567352f 1350 DBG("Using run_as worker");
cd9251b8 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);
1365
1366 if (ret == -1) {
1367 ERR("Failed to restart worker process.");
1368 goto err;
1369 }
1370 }
2d85a600 1371 } else {
7567352f 1372 DBG("Using run_as without worker");
fe9f7760 1373 ret = run_as_noworker(cmd, data, ret_value, uid, gid);
2d85a600 1374 }
fe9f7760 1375err:
cd9251b8 1376 pthread_mutex_unlock(&worker_lock);
7567352f 1377 return ret;
2d85a600
MD
1378}
1379
90e535ef 1380LTTNG_HIDDEN
e11d277b 1381int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 1382{
a42089bd
JG
1383 return run_as_mkdirat_recursive(AT_FDCWD, path, mode, uid, gid);
1384}
1385
1386LTTNG_HIDDEN
1387int run_as_mkdirat_recursive(int dirfd, const char *path, mode_t mode,
1388 uid_t uid, gid_t gid)
1389{
1390 int ret;
fcde531a
JG
1391 struct run_as_data data = {};
1392 struct run_as_ret run_as_ret = {};
60b6c79c 1393
a42089bd
JG
1394 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1395 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1396 path, (int) mode, (int) uid, (int) gid);
fcde531a
JG
1397 ret = lttng_strncpy(data.u.mkdir.path, path,
1398 sizeof(data.u.mkdir.path));
a42089bd
JG
1399 if (ret) {
1400 ERR("Failed to copy path argument of mkdirat recursive command");
1401 goto error;
1402 }
fcde531a
JG
1403 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1404 data.u.mkdir.mode = mode;
1405 data.u.mkdir.dirfd = dirfd;
a42089bd
JG
1406 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR_RECURSIVE : RUN_AS_MKDIRAT_RECURSIVE,
1407 &data, &run_as_ret, uid, gid);
1408 errno = run_as_ret._errno;
fcde531a 1409 ret = run_as_ret.u.ret;
a42089bd
JG
1410error:
1411 return ret;
60b6c79c
MD
1412}
1413
90e535ef 1414LTTNG_HIDDEN
e11d277b 1415int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 1416{
a42089bd
JG
1417 return run_as_mkdirat(AT_FDCWD, path, mode, uid, gid);
1418}
1419
1420LTTNG_HIDDEN
1421int run_as_mkdirat(int dirfd, const char *path, mode_t mode,
1422 uid_t uid, gid_t gid)
1423{
1424 int ret;
fcde531a
JG
1425 struct run_as_data data = {};
1426 struct run_as_ret run_as_ret = {};
fe9f7760 1427
a42089bd
JG
1428 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1429 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1430 path, (int) mode, (int) uid, (int) gid);
fcde531a
JG
1431 ret = lttng_strncpy(data.u.mkdir.path, path,
1432 sizeof(data.u.mkdir.path));
a42089bd
JG
1433 if (ret) {
1434 ERR("Failed to copy path argument of mkdirat command");
1435 goto error;
1436 }
fcde531a
JG
1437 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1438 data.u.mkdir.mode = mode;
1439 data.u.mkdir.dirfd = dirfd;
a42089bd
JG
1440 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR : RUN_AS_MKDIRAT,
1441 &data, &run_as_ret, uid, gid);
1442 errno = run_as_ret._errno;
fcde531a 1443 ret = run_as_ret.u.ret;
a42089bd
JG
1444error:
1445 return ret;
60b6c79c
MD
1446}
1447
90e535ef 1448LTTNG_HIDDEN
a4ee1b7e
JG
1449int run_as_open(const char *path, int flags, mode_t mode, uid_t uid,
1450 gid_t gid)
1451{
1452 return run_as_openat(AT_FDCWD, path, flags, mode, uid, gid);
1453}
1454
1455LTTNG_HIDDEN
1456int run_as_openat(int dirfd, const char *path, int flags, mode_t mode,
1457 uid_t uid, gid_t gid)
60b6c79c 1458{
fcde531a
JG
1459 int ret;
1460 struct run_as_data data = {};
1461 struct run_as_ret run_as_ret = {};
fe9f7760 1462
a4ee1b7e
JG
1463 DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
1464 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1465 path, flags, (int) mode, (int) uid, (int) gid);
fcde531a
JG
1466 ret = lttng_strncpy(data.u.open.path, path, sizeof(data.u.open.path));
1467 if (ret) {
1468 ERR("Failed to copy path argument of open command");
1469 goto error;
1470 }
7567352f
MD
1471 data.u.open.flags = flags;
1472 data.u.open.mode = mode;
fcde531a 1473 data.u.open.dirfd = dirfd;
a4ee1b7e 1474 run_as(dirfd == AT_FDCWD ? RUN_AS_OPEN : RUN_AS_OPENAT,
fcde531a
JG
1475 &data, &run_as_ret, uid, gid);
1476 errno = run_as_ret._errno;
1477 ret = run_as_ret.u.ret < 0 ? run_as_ret.u.ret :
1478 run_as_ret.u.open.fd;
1479error:
1480 return ret;
60b6c79c 1481}
4628484a
MD
1482
1483LTTNG_HIDDEN
1484int run_as_unlink(const char *path, uid_t uid, gid_t gid)
a4ee1b7e
JG
1485{
1486 return run_as_unlinkat(AT_FDCWD, path, uid, gid);
1487}
1488
1489LTTNG_HIDDEN
1490int run_as_unlinkat(int dirfd, const char *path, uid_t uid, gid_t gid)
4628484a 1491{
fcde531a
JG
1492 int ret;
1493 struct run_as_data data = {};
1494 struct run_as_ret run_as_ret = {};
fe9f7760 1495
a4ee1b7e
JG
1496 DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
1497 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1498 path, (int) uid, (int) gid);
fcde531a
JG
1499 ret = lttng_strncpy(data.u.unlink.path, path,
1500 sizeof(data.u.unlink.path));
1501 if (ret) {
1502 goto error;
1503 }
1504 data.u.unlink.dirfd = dirfd;
1505 run_as(dirfd == AT_FDCWD ? RUN_AS_UNLINK : RUN_AS_UNLINKAT, &data,
1506 &run_as_ret, uid, gid);
1507 errno = run_as_ret._errno;
1508 ret = run_as_ret.u.ret;
1509error:
1510 return ret;
1511}
1512
1513LTTNG_HIDDEN
1514int run_as_rmdir(const char *path, uid_t uid, gid_t gid)
1515{
1516 return run_as_rmdirat(AT_FDCWD, path, uid, gid);
1517}
1518
1519LTTNG_HIDDEN
1520int run_as_rmdirat(int dirfd, const char *path, uid_t uid, gid_t gid)
1521{
1522 int ret;
1523 struct run_as_data data = {};
1524 struct run_as_ret run_as_ret = {};
1525
1526 DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d",
1527 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1528 path, (int) uid, (int) gid);
1529 ret = lttng_strncpy(data.u.rmdir.path, path,
1530 sizeof(data.u.rmdir.path));
1531 if (ret) {
1532 goto error;
1533 }
1534 data.u.rmdir.dirfd = dirfd;
1535 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR : RUN_AS_RMDIRAT, &data,
1536 &run_as_ret, uid, gid);
1537 errno = run_as_ret._errno;
1538 ret = run_as_ret.u.ret;
1539error:
1540 return ret;
4628484a
MD
1541}
1542
1543LTTNG_HIDDEN
58ec7253 1544int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid, int flags)
4628484a 1545{
58ec7253 1546 return run_as_rmdirat_recursive(AT_FDCWD, path, uid, gid, flags);
fcde531a 1547}
fe9f7760 1548
fcde531a 1549LTTNG_HIDDEN
58ec7253 1550int run_as_rmdirat_recursive(int dirfd, const char *path, uid_t uid, gid_t gid, int flags)
fcde531a
JG
1551{
1552 int ret;
1553 struct run_as_data data = {};
1554 struct run_as_ret run_as_ret = {};
4628484a 1555
fcde531a
JG
1556 DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d",
1557 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
08797918 1558 path, (int) uid, (int) gid);
fcde531a
JG
1559 ret = lttng_strncpy(data.u.rmdir.path, path,
1560 sizeof(data.u.rmdir.path));
1561 if (ret) {
1562 goto error;
1563 }
1564 data.u.rmdir.dirfd = dirfd;
58ec7253 1565 data.u.rmdir.flags = flags;
fcde531a
JG
1566 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR_RECURSIVE : RUN_AS_RMDIRAT_RECURSIVE,
1567 &data, &run_as_ret, uid, gid);
1568 errno = run_as_ret._errno;
1569 ret = run_as_ret.u.ret;
1570error:
1571 return ret;
1572}
1573
1574LTTNG_HIDDEN
1575int run_as_rename(const char *old, const char *new, uid_t uid, gid_t gid)
1576{
1577 return run_as_renameat(AT_FDCWD, old, AT_FDCWD, new, uid, gid);
1578}
1579
1580LTTNG_HIDDEN
1581int run_as_renameat(int old_dirfd, const char *old_name,
1582 int new_dirfd, const char *new_name, uid_t uid, gid_t gid)
1583{
1584 int ret;
1585 struct run_as_data data = {};
1586 struct run_as_ret run_as_ret = {};
1587
1588 DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d",
1589 old_dirfd, old_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1590 old_name,
1591 new_dirfd, new_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1592 new_name, (int) uid, (int) gid);
1593 ret = lttng_strncpy(data.u.rename.old_path, old_name,
1594 sizeof(data.u.rename.old_path));
1595 if (ret) {
1596 goto error;
1597 }
1598 ret = lttng_strncpy(data.u.rename.new_path, new_name,
1599 sizeof(data.u.rename.new_path));
1600 if (ret) {
1601 goto error;
1602 }
1603
1604 data.u.rename.dirfds[0] = old_dirfd;
1605 data.u.rename.dirfds[1] = new_dirfd;
1606 run_as(old_dirfd == AT_FDCWD && new_dirfd == AT_FDCWD ?
1607 RUN_AS_RENAME : RUN_AS_RENAMEAT,
1608 &data, &run_as_ret, uid, gid);
1609 errno = run_as_ret._errno;
1610 ret = run_as_ret.u.ret;
1611error:
1612 return ret;
7567352f
MD
1613}
1614
241e0a5a
FD
1615LTTNG_HIDDEN
1616int run_as_extract_elf_symbol_offset(int fd, const char* function,
1617 uid_t uid, gid_t gid, uint64_t *offset)
1618{
fcde531a
JG
1619 int ret;
1620 struct run_as_data data = {};
1621 struct run_as_ret run_as_ret = {};
f726677b 1622
241e0a5a 1623 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
fcde531a
JG
1624 "with for uid %d and gid %d", fd, function,
1625 (int) uid, (int) gid);
241e0a5a 1626
fcde531a 1627 data.u.extract_elf_symbol_offset.fd = fd;
241e0a5a
FD
1628
1629 strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1);
241e0a5a 1630 data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
fcde531a
JG
1631 ret = lttng_strncpy(data.u.extract_elf_symbol_offset.function,
1632 function,
1633 sizeof(data.u.extract_elf_symbol_offset.function));
1634 if (ret) {
1635 goto error;
1636 }
241e0a5a 1637
fcde531a
JG
1638 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &run_as_ret, uid, gid);
1639 errno = run_as_ret._errno;
1640 if (run_as_ret._error) {
1641 ret = -1;
1642 goto error;
241e0a5a
FD
1643 }
1644
fcde531a
JG
1645 *offset = run_as_ret.u.extract_elf_symbol_offset.offset;
1646error:
1647 return ret;
241e0a5a
FD
1648}
1649
0ef03255
FD
1650LTTNG_HIDDEN
1651int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name,
1652 const char* probe_name, uid_t uid, gid_t gid,
1653 uint64_t **offsets, uint32_t *num_offset)
1654{
fcde531a
JG
1655 int ret;
1656 struct run_as_data data = {};
1657 struct run_as_ret run_as_ret = {};
f726677b 1658
0ef03255 1659 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
fcde531a
JG
1660 "provider_name=%s with for uid %d and gid %d", fd,
1661 probe_name, provider_name, (int) uid, (int) gid);
0ef03255 1662
fcde531a 1663 data.u.extract_sdt_probe_offsets.fd = fd;
0ef03255 1664
fcde531a
JG
1665 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name,
1666 sizeof(data.u.extract_sdt_probe_offsets.probe_name));
1667 if (ret) {
1668 goto error;
1669 }
1670 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.provider_name,
1671 provider_name,
1672 sizeof(data.u.extract_sdt_probe_offsets.provider_name));
1673 if (ret) {
1674 goto error;
0ef03255
FD
1675 }
1676
fcde531a
JG
1677 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &run_as_ret, uid, gid);
1678 errno = run_as_ret._errno;
1679 if (run_as_ret._error) {
1680 ret = -1;
1681 goto error;
1682 }
0ef03255 1683
fcde531a 1684 *num_offset = run_as_ret.u.extract_sdt_probe_offsets.num_offset;
0ef03255
FD
1685 *offsets = zmalloc(*num_offset * sizeof(uint64_t));
1686 if (!*offsets) {
fcde531a
JG
1687 ret = -ENOMEM;
1688 goto error;
0ef03255
FD
1689 }
1690
fcde531a
JG
1691 memcpy(*offsets, run_as_ret.u.extract_sdt_probe_offsets.offsets,
1692 *num_offset * sizeof(uint64_t));
1693error:
1694 return ret;
0ef03255
FD
1695}
1696
7567352f 1697LTTNG_HIDDEN
5bedd986
JG
1698int run_as_create_worker(const char *procname,
1699 post_fork_cleanup_cb clean_up_func,
1700 void *clean_up_user_data)
7567352f 1701{
cd9251b8 1702 int ret;
7567352f 1703
749b7a0c 1704 pthread_mutex_lock(&worker_lock);
5bedd986
JG
1705 ret = run_as_create_worker_no_lock(procname, clean_up_func,
1706 clean_up_user_data);
749b7a0c 1707 pthread_mutex_unlock(&worker_lock);
7567352f
MD
1708 return ret;
1709}
1710
1711LTTNG_HIDDEN
1712void run_as_destroy_worker(void)
1713{
749b7a0c 1714 pthread_mutex_lock(&worker_lock);
3340c2a1 1715 run_as_destroy_worker_no_lock();
749b7a0c 1716 pthread_mutex_unlock(&worker_lock);
4628484a 1717}
This page took 0.131378 seconds and 4 git commands to generate.