runas: fix: possible unaligned access in packed structure
[lttng-tools.git] / src / common / runas.c
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
49 struct run_as_data;
50 struct run_as_ret;
51 typedef int (*run_as_fct)(struct run_as_data *data, struct run_as_ret *ret_value);
52
53 enum 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
72 struct run_as_mkdir_data {
73 int dirfd;
74 char path[LTTNG_PATH_MAX];
75 mode_t mode;
76 } LTTNG_PACKED;
77
78 struct run_as_open_data {
79 int dirfd;
80 char path[LTTNG_PATH_MAX];
81 int flags;
82 mode_t mode;
83 } LTTNG_PACKED;
84
85 struct run_as_unlink_data {
86 int dirfd;
87 char path[LTTNG_PATH_MAX];
88 } LTTNG_PACKED;
89
90 struct 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
96 struct run_as_extract_elf_symbol_offset_data {
97 int fd;
98 char function[LTTNG_SYMBOL_NAME_LEN];
99 } LTTNG_PACKED;
100
101 struct 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
107 struct 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
117 struct run_as_open_ret {
118 int fd;
119 } LTTNG_PACKED;
120
121 struct run_as_extract_elf_symbol_offset_ret {
122 uint64_t offset;
123 } LTTNG_PACKED;
124
125 struct 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
130 struct 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 */
160 struct 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
197 struct 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
204 static 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
321 struct 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). */
328 static struct run_as_worker *global_worker;
329 /* Lock protecting the worker. */
330 static pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER;
331
332 #ifdef VALGRIND
333 static
334 int use_clone(void)
335 {
336 return 0;
337 }
338 #else
339 static
340 int 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 */
349 static
350 int _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
373 static
374 int _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
397 static
398 int _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
425 static
426 int _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
444 static
445 int _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
463 static
464 int _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
482 static
483 int _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
510 static
511 int _extract_elf_symbol_offset(struct run_as_data *data,
512 struct run_as_ret *ret_value)
513 {
514 int ret = 0;
515 uint64_t offset;
516
517 ret_value->_error = false;
518 ret = lttng_elf_get_symbol_offset(data->u.extract_elf_symbol_offset.fd,
519 data->u.extract_elf_symbol_offset.function,
520 &offset);
521 if (ret) {
522 DBG("Failed to extract ELF function offset");
523 ret_value->_error = true;
524 }
525 ret_value->u.extract_elf_symbol_offset.offset = offset;
526
527 return ret;
528 }
529
530 static
531 int _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. */
541 ret = lttng_elf_get_sdt_probe_offsets(
542 data->u.extract_sdt_probe_offsets.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
566 free_offset:
567 free(offsets);
568 end:
569 return ret;
570 }
571 #else
572 static
573 int _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
580 static
581 int _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
588
589 static
590 run_as_fct run_as_enum_to_fct(enum run_as_cmd cmd)
591 {
592 switch (cmd) {
593 case RUN_AS_MKDIR:
594 case RUN_AS_MKDIRAT:
595 return _mkdirat;
596 case RUN_AS_MKDIR_RECURSIVE:
597 case RUN_AS_MKDIRAT_RECURSIVE:
598 return _mkdirat_recursive;
599 case RUN_AS_OPEN:
600 case RUN_AS_OPENAT:
601 return _open;
602 case RUN_AS_UNLINK:
603 case RUN_AS_UNLINKAT:
604 return _unlink;
605 case RUN_AS_RMDIR:
606 case RUN_AS_RMDIRAT:
607 return _rmdir;
608 case RUN_AS_RMDIR_RECURSIVE:
609 case RUN_AS_RMDIRAT_RECURSIVE:
610 return _rmdir_recursive;
611 case RUN_AS_RENAME:
612 case RUN_AS_RENAMEAT:
613 return _rename;
614 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
615 return _extract_elf_symbol_offset;
616 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
617 return _extract_sdt_probe_offsets;
618 default:
619 ERR("Unknown command %d", (int) cmd);
620 return NULL;
621 }
622 }
623
624 static
625 int do_send_fds(int sock, const int *fds, unsigned int fd_count)
626 {
627 ssize_t len;
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 }
638
639 len = lttcomm_send_fds_unix_sock(sock, fds, fd_count);
640 return len < 0 ? -1 : 0;
641 }
642
643 static
644 int do_recv_fds(int sock, int *fds, unsigned int fd_count)
645 {
646 int ret = 0;
647 unsigned int i;
648 ssize_t len;
649
650 len = lttcomm_recv_fds_unix_sock(sock, fds, fd_count);
651 if (len == 0) {
652 ret = -1;
653 goto end;
654 } else if (len < 0) {
655 PERROR("Failed to receive file descriptors from socket");
656 ret = -1;
657 goto end;
658 }
659
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 }
666 end:
667 return ret;
668 }
669
670 static
671 int send_fds_to_worker(const struct run_as_worker *worker,
672 const struct run_as_data *data)
673 {
674 int ret = 0;
675 unsigned int i;
676
677 if (COMMAND_USE_CWD_FD(data) || COMMAND_IN_FD_COUNT(data) == 0) {
678 goto end;
679 }
680
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 }
689
690 ret = do_send_fds(worker->sockpair[0], COMMAND_IN_FDS(data),
691 COMMAND_IN_FD_COUNT(data));
692 if (ret < 0) {
693 PERROR("Failed to send file descriptor to run-as worker");
694 ret = -1;
695 goto end;
696 }
697 end:
698 return ret;
699 }
700
701 static
702 int send_fds_to_master(struct run_as_worker *worker, enum run_as_cmd cmd,
703 struct run_as_ret *run_as_ret)
704 {
705 int ret = 0;
706 unsigned int i;
707
708 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
709 goto end;
710 }
711
712 ret = do_send_fds(worker->sockpair[1], COMMAND_OUT_FDS(cmd, run_as_ret),
713 COMMAND_OUT_FD_COUNT(cmd));
714 if (ret < 0) {
715 PERROR("Failed to send file descriptor to master process");
716 goto end;
717 }
718
719 for (i = 0; i < COMMAND_OUT_FD_COUNT(cmd); i++) {
720 int ret_close = close(COMMAND_OUT_FDS(cmd, run_as_ret)[i]);
721
722 if (ret_close < 0) {
723 PERROR("Failed to close result file descriptor");
724 }
725 }
726 end:
727 return ret;
728 }
729
730 static
731 int recv_fds_from_worker(const struct run_as_worker *worker, enum run_as_cmd cmd,
732 struct run_as_ret *run_as_ret)
733 {
734 int ret = 0;
735
736 if (COMMAND_OUT_FD_COUNT(cmd) == 0) {
737 goto end;
738 }
739
740 ret = do_recv_fds(worker->sockpair[0], COMMAND_OUT_FDS(cmd, run_as_ret),
741 COMMAND_OUT_FD_COUNT(cmd));
742 if (ret < 0) {
743 PERROR("Failed to receive file descriptor from run-as worker");
744 ret = -1;
745 }
746 end:
747 return ret;
748 }
749
750 static
751 int recv_fds_from_master(struct run_as_worker *worker, struct run_as_data *data)
752 {
753 int ret = 0;
754
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;
762 }
763
764 ret = do_recv_fds(worker->sockpair[1], COMMAND_IN_FDS(data),
765 COMMAND_IN_FD_COUNT(data));
766 if (ret < 0) {
767 PERROR("Failed to receive file descriptors from master process");
768 ret = -1;
769 }
770 end:
771 return ret;
772 }
773
774 static
775 int cleanup_received_fds(struct run_as_data *data)
776 {
777 int ret = 0, i;
778
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 }
788 }
789 end:
790 return ret;
791 }
792
793 /*
794 * Return < 0 on error, 0 if OK, 1 on hangup.
795 */
796 static
797 int handle_one_cmd(struct run_as_worker *worker)
798 {
799 int ret = 0;
800 struct run_as_data data = {};
801 ssize_t readlen, writelen;
802 struct run_as_ret sendret = {};
803 run_as_fct cmd;
804 uid_t prev_euid;
805
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 */
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 }
823
824 cmd = run_as_enum_to_fct(data.cmd);
825 if (!cmd) {
826 ret = -1;
827 goto end;
828 }
829
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 */
835 ret = recv_fds_from_master(worker, &data);
836 if (ret < 0) {
837 PERROR("recv_fd_from_master error");
838 ret = -1;
839 goto end;
840 }
841
842 prev_euid = getuid();
843 if (data.gid != getegid()) {
844 ret = setegid(data.gid);
845 if (ret < 0) {
846 sendret._error = true;
847 sendret._errno = errno;
848 PERROR("setegid");
849 goto write_return;
850 }
851 }
852 if (data.uid != prev_euid) {
853 ret = seteuid(data.uid);
854 if (ret < 0) {
855 sendret._error = true;
856 sendret._errno = errno;
857 PERROR("seteuid");
858 goto write_return;
859 }
860 }
861
862 /*
863 * Also set umask to 0 for mkdir executable bit.
864 */
865 umask(0);
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 }
874
875 write_return:
876 ret = cleanup_received_fds(&data);
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 */
886 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
887 sizeof(sendret));
888 if (writelen < sizeof(sendret)) {
889 PERROR("lttcomm_send_unix_sock error");
890 ret = -1;
891 goto end;
892 }
893
894 /*
895 * Stage 5: Send resulting file descriptors to the master.
896 */
897 ret = send_fds_to_master(worker, data.cmd, &sendret);
898 if (ret < 0) {
899 DBG("Sending FD to master returned an error");
900 goto end;
901 }
902
903 if (seteuid(prev_euid) < 0) {
904 PERROR("seteuid");
905 ret = -1;
906 goto end;
907 }
908 ret = 0;
909 end:
910 return ret;
911 }
912
913 static
914 int 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
928 ret = lttng_prctl(PR_SET_NAME,
929 (unsigned long) DEFAULT_RUN_AS_WORKER_NAME, 0, 0, 0);
930 if (ret && ret != -ENOSYS) {
931 /* Don't fail as this is not essential. */
932 PERROR("prctl PR_SET_NAME");
933 }
934
935 memset(&sendret, 0, sizeof(sendret));
936
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;
957 end:
958 return ret;
959 }
960
961 static
962 int run_as_cmd(struct run_as_worker *worker,
963 enum run_as_cmd cmd,
964 struct run_as_data *data,
965 struct run_as_ret *ret_value,
966 uid_t uid, gid_t gid)
967 {
968 int ret = 0;
969 ssize_t readlen, writelen;
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()) {
976 ret = -1;
977 ret_value->_errno = EPERM;
978 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
979 (int) uid, (int) geteuid());
980 goto end;
981 }
982 }
983
984 data->cmd = cmd;
985 data->uid = uid;
986 data->gid = gid;
987
988 /*
989 * Stage 1: Send the run_as_data struct to the worker process
990 */
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");
995 ret = -1;
996 ret_value->_errno = EIO;
997 goto end;
998 }
999
1000 /*
1001 * Stage 2: Send file descriptor to the worker process if needed
1002 */
1003 ret = send_fds_to_worker(worker, data);
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));
1021 if (!readlen) {
1022 ERR("Run-as worker has hung-up during run_as_cmd");
1023 ret = -1;
1024 ret_value->_errno = EIO;
1025 goto end;
1026 } else if (readlen < sizeof(*ret_value)) {
1027 PERROR("Error reading response from run_as");
1028 ret = -1;
1029 ret_value->_errno = errno;
1030 goto end;
1031 }
1032
1033 if (ret_value->_error) {
1034 /* Skip stage 5 on error as there will be no fd to receive. */
1035 goto end;
1036 }
1037
1038 /*
1039 * Stage 5: Receive file descriptor if needed
1040 */
1041 ret = recv_fds_from_worker(worker, cmd, ret_value);
1042 if (ret < 0) {
1043 ERR("Error receiving fd");
1044 ret = -1;
1045 ret_value->_errno = EIO;
1046 }
1047
1048 end:
1049 return ret;
1050 }
1051
1052 /*
1053 * This is for debugging ONLY and should not be considered secure.
1054 */
1055 static
1056 int 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)
1059 {
1060 int ret, saved_errno;
1061 mode_t old_mask;
1062 run_as_fct fct;
1063
1064 fct = run_as_enum_to_fct(cmd);
1065 if (!fct) {
1066 errno = -ENOSYS;
1067 ret = -1;
1068 goto end;
1069 }
1070 old_mask = umask(0);
1071 ret = fct(data, ret_value);
1072 saved_errno = ret_value->_errno;
1073 umask(old_mask);
1074 errno = saved_errno;
1075 end:
1076 return ret;
1077 }
1078
1079 static
1080 int 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
1091 static
1092 void 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
1120 static
1121 int 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");
1146 end:
1147 return ret;
1148 }
1149
1150 static
1151 int run_as_create_worker_no_lock(const char *procname,
1152 post_fork_cleanup_cb clean_up_func,
1153 void *clean_up_user_data)
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;
1178 goto error_procname_alloc;
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();
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 }
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;
1228 free(worker->procname);
1229 free(worker);
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 }
1254 end:
1255 return ret;
1256
1257 /* Error handling. */
1258 error_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 }
1268 error_sock:
1269 free(worker->procname);
1270 error_procname_alloc:
1271 free(worker);
1272 return ret;
1273 }
1274
1275 static
1276 void 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
1320 static
1321 int run_as_restart_worker(struct run_as_worker *worker)
1322 {
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 */
1329 run_as_destroy_worker_no_lock();
1330
1331 /* Create a new run_as worker process*/
1332 ret = run_as_create_worker_no_lock(procname, NULL, NULL);
1333 if (ret < 0 ) {
1334 ERR("Restarting the worker process failed");
1335 ret = -1;
1336 goto err;
1337 }
1338 err:
1339 return ret;
1340 }
1341
1342 static
1343 int 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;
1347
1348 pthread_mutex_lock(&worker_lock);
1349 if (use_clone()) {
1350 DBG("Using run_as worker");
1351
1352 assert(global_worker);
1353
1354 ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid);
1355 saved_errno = ret_value->_errno;
1356
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 if (ret == -1) {
1366 ERR("Failed to restart worker process.");
1367 goto err;
1368 }
1369 }
1370 } else {
1371 DBG("Using run_as without worker");
1372 ret = run_as_noworker(cmd, data, ret_value, uid, gid);
1373 }
1374 err:
1375 pthread_mutex_unlock(&worker_lock);
1376 return ret;
1377 }
1378
1379 LTTNG_HIDDEN
1380 int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
1381 {
1382 return run_as_mkdirat_recursive(AT_FDCWD, path, mode, uid, gid);
1383 }
1384
1385 LTTNG_HIDDEN
1386 int run_as_mkdirat_recursive(int dirfd, const char *path, mode_t mode,
1387 uid_t uid, gid_t gid)
1388 {
1389 int ret;
1390 struct run_as_data data = {};
1391 struct run_as_ret run_as_ret = {};
1392
1393 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1394 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1395 path, (int) mode, (int) uid, (int) gid);
1396 ret = lttng_strncpy(data.u.mkdir.path, path,
1397 sizeof(data.u.mkdir.path));
1398 if (ret) {
1399 ERR("Failed to copy path argument of mkdirat recursive command");
1400 goto error;
1401 }
1402 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1403 data.u.mkdir.mode = mode;
1404 data.u.mkdir.dirfd = dirfd;
1405 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR_RECURSIVE : RUN_AS_MKDIRAT_RECURSIVE,
1406 &data, &run_as_ret, uid, gid);
1407 errno = run_as_ret._errno;
1408 ret = run_as_ret.u.ret;
1409 error:
1410 return ret;
1411 }
1412
1413 LTTNG_HIDDEN
1414 int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
1415 {
1416 return run_as_mkdirat(AT_FDCWD, path, mode, uid, gid);
1417 }
1418
1419 LTTNG_HIDDEN
1420 int run_as_mkdirat(int dirfd, const char *path, mode_t mode,
1421 uid_t uid, gid_t gid)
1422 {
1423 int ret;
1424 struct run_as_data data = {};
1425 struct run_as_ret run_as_ret = {};
1426
1427 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1428 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1429 path, (int) mode, (int) uid, (int) gid);
1430 ret = lttng_strncpy(data.u.mkdir.path, path,
1431 sizeof(data.u.mkdir.path));
1432 if (ret) {
1433 ERR("Failed to copy path argument of mkdirat command");
1434 goto error;
1435 }
1436 data.u.mkdir.path[sizeof(data.u.mkdir.path) - 1] = '\0';
1437 data.u.mkdir.mode = mode;
1438 data.u.mkdir.dirfd = dirfd;
1439 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR : RUN_AS_MKDIRAT,
1440 &data, &run_as_ret, uid, gid);
1441 errno = run_as_ret._errno;
1442 ret = run_as_ret.u.ret;
1443 error:
1444 return ret;
1445 }
1446
1447 LTTNG_HIDDEN
1448 int run_as_open(const char *path, int flags, mode_t mode, uid_t uid,
1449 gid_t gid)
1450 {
1451 return run_as_openat(AT_FDCWD, path, flags, mode, uid, gid);
1452 }
1453
1454 LTTNG_HIDDEN
1455 int run_as_openat(int dirfd, const char *path, int flags, mode_t mode,
1456 uid_t uid, gid_t gid)
1457 {
1458 int ret;
1459 struct run_as_data data = {};
1460 struct run_as_ret run_as_ret = {};
1461
1462 DBG3("openat() fd = %d%s, path = %s, flags = %X, mode = %d, uid %d, gid %d",
1463 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1464 path, flags, (int) mode, (int) uid, (int) gid);
1465 ret = lttng_strncpy(data.u.open.path, path, sizeof(data.u.open.path));
1466 if (ret) {
1467 ERR("Failed to copy path argument of open command");
1468 goto error;
1469 }
1470 data.u.open.flags = flags;
1471 data.u.open.mode = mode;
1472 data.u.open.dirfd = dirfd;
1473 run_as(dirfd == AT_FDCWD ? RUN_AS_OPEN : RUN_AS_OPENAT,
1474 &data, &run_as_ret, uid, gid);
1475 errno = run_as_ret._errno;
1476 ret = run_as_ret.u.ret < 0 ? run_as_ret.u.ret :
1477 run_as_ret.u.open.fd;
1478 error:
1479 return ret;
1480 }
1481
1482 LTTNG_HIDDEN
1483 int run_as_unlink(const char *path, uid_t uid, gid_t gid)
1484 {
1485 return run_as_unlinkat(AT_FDCWD, path, uid, gid);
1486 }
1487
1488 LTTNG_HIDDEN
1489 int run_as_unlinkat(int dirfd, const char *path, uid_t uid, gid_t gid)
1490 {
1491 int ret;
1492 struct run_as_data data = {};
1493 struct run_as_ret run_as_ret = {};
1494
1495 DBG3("unlinkat() fd = %d%s, path = %s, uid = %d, gid = %d",
1496 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1497 path, (int) uid, (int) gid);
1498 ret = lttng_strncpy(data.u.unlink.path, path,
1499 sizeof(data.u.unlink.path));
1500 if (ret) {
1501 goto error;
1502 }
1503 data.u.unlink.dirfd = dirfd;
1504 run_as(dirfd == AT_FDCWD ? RUN_AS_UNLINK : RUN_AS_UNLINKAT, &data,
1505 &run_as_ret, uid, gid);
1506 errno = run_as_ret._errno;
1507 ret = run_as_ret.u.ret;
1508 error:
1509 return ret;
1510 }
1511
1512 LTTNG_HIDDEN
1513 int run_as_rmdir(const char *path, uid_t uid, gid_t gid)
1514 {
1515 return run_as_rmdirat(AT_FDCWD, path, uid, gid);
1516 }
1517
1518 LTTNG_HIDDEN
1519 int run_as_rmdirat(int dirfd, const char *path, uid_t uid, gid_t gid)
1520 {
1521 int ret;
1522 struct run_as_data data = {};
1523 struct run_as_ret run_as_ret = {};
1524
1525 DBG3("rmdirat() fd = %d%s, path = %s, uid = %d, gid = %d",
1526 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1527 path, (int) uid, (int) gid);
1528 ret = lttng_strncpy(data.u.rmdir.path, path,
1529 sizeof(data.u.rmdir.path));
1530 if (ret) {
1531 goto error;
1532 }
1533 data.u.rmdir.dirfd = dirfd;
1534 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR : RUN_AS_RMDIRAT, &data,
1535 &run_as_ret, uid, gid);
1536 errno = run_as_ret._errno;
1537 ret = run_as_ret.u.ret;
1538 error:
1539 return ret;
1540 }
1541
1542 LTTNG_HIDDEN
1543 int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid, int flags)
1544 {
1545 return run_as_rmdirat_recursive(AT_FDCWD, path, uid, gid, flags);
1546 }
1547
1548 LTTNG_HIDDEN
1549 int run_as_rmdirat_recursive(int dirfd, const char *path, uid_t uid, gid_t gid, int flags)
1550 {
1551 int ret;
1552 struct run_as_data data = {};
1553 struct run_as_ret run_as_ret = {};
1554
1555 DBG3("rmdirat() recursive fd = %d%s, path = %s, uid = %d, gid = %d",
1556 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1557 path, (int) uid, (int) gid);
1558 ret = lttng_strncpy(data.u.rmdir.path, path,
1559 sizeof(data.u.rmdir.path));
1560 if (ret) {
1561 goto error;
1562 }
1563 data.u.rmdir.dirfd = dirfd;
1564 data.u.rmdir.flags = flags;
1565 run_as(dirfd == AT_FDCWD ? RUN_AS_RMDIR_RECURSIVE : RUN_AS_RMDIRAT_RECURSIVE,
1566 &data, &run_as_ret, uid, gid);
1567 errno = run_as_ret._errno;
1568 ret = run_as_ret.u.ret;
1569 error:
1570 return ret;
1571 }
1572
1573 LTTNG_HIDDEN
1574 int run_as_rename(const char *old, const char *new, uid_t uid, gid_t gid)
1575 {
1576 return run_as_renameat(AT_FDCWD, old, AT_FDCWD, new, uid, gid);
1577 }
1578
1579 LTTNG_HIDDEN
1580 int run_as_renameat(int old_dirfd, const char *old_name,
1581 int new_dirfd, const char *new_name, uid_t uid, gid_t gid)
1582 {
1583 int ret;
1584 struct run_as_data data = {};
1585 struct run_as_ret run_as_ret = {};
1586
1587 DBG3("renameat() old_dirfd = %d%s, old_name = %s, new_dirfd = %d%s, new_name = %s, uid = %d, gid = %d",
1588 old_dirfd, old_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1589 old_name,
1590 new_dirfd, new_dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1591 new_name, (int) uid, (int) gid);
1592 ret = lttng_strncpy(data.u.rename.old_path, old_name,
1593 sizeof(data.u.rename.old_path));
1594 if (ret) {
1595 goto error;
1596 }
1597 ret = lttng_strncpy(data.u.rename.new_path, new_name,
1598 sizeof(data.u.rename.new_path));
1599 if (ret) {
1600 goto error;
1601 }
1602
1603 data.u.rename.dirfds[0] = old_dirfd;
1604 data.u.rename.dirfds[1] = new_dirfd;
1605 run_as(old_dirfd == AT_FDCWD && new_dirfd == AT_FDCWD ?
1606 RUN_AS_RENAME : RUN_AS_RENAMEAT,
1607 &data, &run_as_ret, uid, gid);
1608 errno = run_as_ret._errno;
1609 ret = run_as_ret.u.ret;
1610 error:
1611 return ret;
1612 }
1613
1614 LTTNG_HIDDEN
1615 int run_as_extract_elf_symbol_offset(int fd, const char* function,
1616 uid_t uid, gid_t gid, uint64_t *offset)
1617 {
1618 int ret;
1619 struct run_as_data data = {};
1620 struct run_as_ret run_as_ret = {};
1621
1622 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
1623 "with for uid %d and gid %d", fd, function,
1624 (int) uid, (int) gid);
1625
1626 data.u.extract_elf_symbol_offset.fd = fd;
1627
1628 strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1);
1629 data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1630 ret = lttng_strncpy(data.u.extract_elf_symbol_offset.function,
1631 function,
1632 sizeof(data.u.extract_elf_symbol_offset.function));
1633 if (ret) {
1634 goto error;
1635 }
1636
1637 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &run_as_ret, uid, gid);
1638 errno = run_as_ret._errno;
1639 if (run_as_ret._error) {
1640 ret = -1;
1641 goto error;
1642 }
1643
1644 *offset = run_as_ret.u.extract_elf_symbol_offset.offset;
1645 error:
1646 return ret;
1647 }
1648
1649 LTTNG_HIDDEN
1650 int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name,
1651 const char* probe_name, uid_t uid, gid_t gid,
1652 uint64_t **offsets, uint32_t *num_offset)
1653 {
1654 int ret;
1655 struct run_as_data data = {};
1656 struct run_as_ret run_as_ret = {};
1657
1658 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
1659 "provider_name=%s with for uid %d and gid %d", fd,
1660 probe_name, provider_name, (int) uid, (int) gid);
1661
1662 data.u.extract_sdt_probe_offsets.fd = fd;
1663
1664 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name,
1665 sizeof(data.u.extract_sdt_probe_offsets.probe_name));
1666 if (ret) {
1667 goto error;
1668 }
1669 ret = lttng_strncpy(data.u.extract_sdt_probe_offsets.provider_name,
1670 provider_name,
1671 sizeof(data.u.extract_sdt_probe_offsets.provider_name));
1672 if (ret) {
1673 goto error;
1674 }
1675
1676 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &run_as_ret, uid, gid);
1677 errno = run_as_ret._errno;
1678 if (run_as_ret._error) {
1679 ret = -1;
1680 goto error;
1681 }
1682
1683 *num_offset = run_as_ret.u.extract_sdt_probe_offsets.num_offset;
1684 *offsets = zmalloc(*num_offset * sizeof(uint64_t));
1685 if (!*offsets) {
1686 ret = -ENOMEM;
1687 goto error;
1688 }
1689
1690 memcpy(*offsets, run_as_ret.u.extract_sdt_probe_offsets.offsets,
1691 *num_offset * sizeof(uint64_t));
1692 error:
1693 return ret;
1694 }
1695
1696 LTTNG_HIDDEN
1697 int run_as_create_worker(const char *procname,
1698 post_fork_cleanup_cb clean_up_func,
1699 void *clean_up_user_data)
1700 {
1701 int ret;
1702
1703 pthread_mutex_lock(&worker_lock);
1704 ret = run_as_create_worker_no_lock(procname, clean_up_func,
1705 clean_up_user_data);
1706 pthread_mutex_unlock(&worker_lock);
1707 return ret;
1708 }
1709
1710 LTTNG_HIDDEN
1711 void run_as_destroy_worker(void)
1712 {
1713 pthread_mutex_lock(&worker_lock);
1714 run_as_destroy_worker_no_lock();
1715 pthread_mutex_unlock(&worker_lock);
1716 }
This page took 0.112023 seconds and 6 git commands to generate.