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