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