Remove unneeded creation of the UST domain sub-directory
[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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _LGPL_SOURCE
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sched.h>
31 #include <signal.h>
32 #include <assert.h>
33 #include <signal.h>
34
35 #include <common/lttng-kernel.h>
36 #include <common/common.h>
37 #include <common/utils.h>
38 #include <common/compat/getenv.h>
39 #include <common/compat/prctl.h>
40 #include <common/unix.h>
41 #include <common/defaults.h>
42 #include <common/lttng-elf.h>
43
44 #include <lttng/constant.h>
45
46 #include "runas.h"
47
48 struct run_as_data;
49 struct run_as_ret;
50 typedef int (*run_as_fct)(struct run_as_data *data, struct run_as_ret *ret_value);
51
52 struct run_as_mkdirat_data {
53 char path[PATH_MAX];
54 mode_t mode;
55 };
56
57 struct run_as_open_data {
58 char path[PATH_MAX];
59 int flags;
60 mode_t mode;
61 };
62
63 struct run_as_unlink_data {
64 char path[PATH_MAX];
65 };
66
67 struct run_as_rmdir_recursive_data {
68 char path[PATH_MAX];
69 };
70
71 struct run_as_extract_elf_symbol_offset_data {
72 char function[LTTNG_SYMBOL_NAME_LEN];
73 };
74
75 struct run_as_extract_sdt_probe_offsets_data {
76 char probe_name[LTTNG_SYMBOL_NAME_LEN];
77 char provider_name[LTTNG_SYMBOL_NAME_LEN];
78 };
79
80 struct run_as_mkdirat_ret {
81 int ret;
82 };
83
84 struct run_as_open_ret {
85 int ret;
86 };
87
88 struct run_as_unlink_ret {
89 int ret;
90 };
91
92 struct run_as_rmdir_recursive_ret {
93 int ret;
94 };
95
96 struct run_as_extract_elf_symbol_offset_ret {
97 uint64_t offset;
98 };
99
100 struct run_as_extract_sdt_probe_offsets_ret {
101 uint32_t num_offset;
102 uint64_t offsets[LTTNG_KERNEL_MAX_UPROBE_NUM];
103 };
104
105 enum run_as_cmd {
106 RUN_AS_MKDIR,
107 RUN_AS_MKDIRAT,
108 RUN_AS_MKDIR_RECURSIVE,
109 RUN_AS_MKDIRAT_RECURSIVE,
110 RUN_AS_OPEN,
111 RUN_AS_UNLINK,
112 RUN_AS_RMDIR_RECURSIVE,
113 RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET,
114 RUN_AS_EXTRACT_SDT_PROBE_OFFSETS,
115 };
116
117 struct run_as_data {
118 enum run_as_cmd cmd;
119 int fd;
120 union {
121 struct run_as_mkdirat_data mkdirat;
122 struct run_as_open_data open;
123 struct run_as_unlink_data unlink;
124 struct run_as_rmdir_recursive_data rmdir_recursive;
125 struct run_as_extract_elf_symbol_offset_data extract_elf_symbol_offset;
126 struct run_as_extract_sdt_probe_offsets_data extract_sdt_probe_offsets;
127 } u;
128 uid_t uid;
129 gid_t gid;
130 };
131
132 /*
133 * The run_as_ret structure holds the returned value and status of the command.
134 *
135 * The `u` union field holds the return value of the command; in most cases it
136 * represents the success or the failure of the command. In more complex
137 * commands, it holds a computed value.
138 *
139 * The _errno field is the errno recorded after the execution of the command.
140 *
141 * The _error fields is used the signify that return status of the command. For
142 * simple commands returning `int` the _error field will be the same as the
143 * ret_int field. In complex commands, it signify the success or failure of the
144 * command.
145 *
146 */
147 struct run_as_ret {
148 int fd;
149 union {
150 struct run_as_mkdirat_ret mkdirat;
151 struct run_as_open_ret open;
152 struct run_as_unlink_ret unlink;
153 struct run_as_rmdir_recursive_ret rmdir_recursive;
154 struct run_as_extract_elf_symbol_offset_ret extract_elf_symbol_offset;
155 struct run_as_extract_sdt_probe_offsets_ret extract_sdt_probe_offsets;
156 } u;
157 int _errno;
158 bool _error;
159 };
160
161 struct run_as_worker {
162 pid_t pid; /* Worker PID. */
163 int sockpair[2];
164 char *procname;
165 };
166
167 /* Single global worker per process (for now). */
168 static struct run_as_worker *global_worker;
169 /* Lock protecting the worker. */
170 static pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER;
171
172 #ifdef VALGRIND
173 static
174 int use_clone(void)
175 {
176 return 0;
177 }
178 #else
179 static
180 int use_clone(void)
181 {
182 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
183 }
184 #endif
185
186 /*
187 * Create recursively directory using the FULL path.
188 */
189 static
190 int _mkdirat_recursive(struct run_as_data *data, struct run_as_ret *ret_value)
191 {
192 const char *path;
193 mode_t mode;
194 struct lttng_directory_handle handle;
195
196 path = data->u.mkdirat.path;
197 mode = data->u.mkdirat.mode;
198
199 (void) lttng_directory_handle_init_from_dirfd(&handle, data->fd);
200 /* Safe to call as we have transitioned to the requested uid/gid. */
201 ret_value->u.mkdirat.ret =
202 lttng_directory_handle_create_subdirectory_recursive(
203 &handle, path, mode);
204 ret_value->_errno = errno;
205 ret_value->_error = (ret_value->u.mkdirat.ret) ? true : false;
206 lttng_directory_handle_fini(&handle);
207 return ret_value->u.mkdirat.ret;
208 }
209
210 static
211 int _mkdirat(struct run_as_data *data, struct run_as_ret *ret_value)
212 {
213 const char *path;
214 mode_t mode;
215 struct lttng_directory_handle handle;
216
217 path = data->u.mkdirat.path;
218 mode = data->u.mkdirat.mode;
219
220 (void) lttng_directory_handle_init_from_dirfd(&handle, data->fd);
221 /* Safe to call as we have transitioned to the requested uid/gid. */
222 ret_value->u.mkdirat.ret =
223 lttng_directory_handle_create_subdirectory(
224 &handle, path, mode);
225 ret_value->_errno = errno;
226 ret_value->_error = (ret_value->u.mkdirat.ret) ? true : false;
227 lttng_directory_handle_fini(&handle);
228 return ret_value->u.mkdirat.ret;
229 }
230
231 static
232 int _open(struct run_as_data *data, struct run_as_ret *ret_value)
233 {
234 ret_value->u.open.ret = open(data->u.open.path, data->u.open.flags, data->u.open.mode);
235 ret_value->fd = ret_value->u.open.ret;
236 ret_value->_errno = errno;
237 ret_value->_error = ret_value->u.open.ret < 0;
238 return ret_value->u.open.ret;
239 }
240
241 static
242 int _unlink(struct run_as_data *data, struct run_as_ret *ret_value)
243 {
244 ret_value->u.unlink.ret = unlink(data->u.unlink.path);
245 ret_value->_errno = errno;
246 ret_value->_error = (ret_value->u.unlink.ret) ? true : false;
247 return ret_value->u.unlink.ret;
248 }
249
250 static
251 int _rmdir_recursive(struct run_as_data *data, struct run_as_ret *ret_value)
252 {
253 ret_value->u.rmdir_recursive.ret = utils_recursive_rmdir(data->u.rmdir_recursive.path);
254 ret_value->_errno = errno;
255 ret_value->_error = (ret_value->u.rmdir_recursive.ret) ? true : false;
256 return ret_value->u.rmdir_recursive.ret;
257 }
258
259 #ifdef HAVE_ELF_H
260 static
261 int _extract_elf_symbol_offset(struct run_as_data *data,
262 struct run_as_ret *ret_value)
263 {
264 int ret = 0;
265 ret_value->_error = false;
266
267 ret = lttng_elf_get_symbol_offset(data->fd,
268 data->u.extract_elf_symbol_offset.function,
269 &ret_value->u.extract_elf_symbol_offset.offset);
270 if (ret) {
271 DBG("Failed to extract ELF function offset");
272 ret_value->_error = true;
273 }
274
275 return ret;
276 }
277
278 static
279 int _extract_sdt_probe_offsets(struct run_as_data *data,
280 struct run_as_ret *ret_value)
281 {
282 int ret = 0;
283 uint64_t *offsets = NULL;
284 uint32_t num_offset;
285
286 ret_value->_error = false;
287
288 /* On success, this call allocates the offsets paramater. */
289 ret = lttng_elf_get_sdt_probe_offsets(data->fd,
290 data->u.extract_sdt_probe_offsets.provider_name,
291 data->u.extract_sdt_probe_offsets.probe_name,
292 &offsets, &num_offset);
293
294 if (ret) {
295 DBG("Failed to extract SDT probe offsets");
296 ret_value->_error = true;
297 goto end;
298 }
299
300 if (num_offset <= 0 || num_offset > LTTNG_KERNEL_MAX_UPROBE_NUM) {
301 DBG("Wrong number of probes.");
302 ret = -1;
303 ret_value->_error = true;
304 goto free_offset;
305 }
306
307 /* Copy the content of the offsets array to the ret struct. */
308 memcpy(ret_value->u.extract_sdt_probe_offsets.offsets,
309 offsets, num_offset * sizeof(uint64_t));
310
311 ret_value->u.extract_sdt_probe_offsets.num_offset = num_offset;
312
313 free_offset:
314 free(offsets);
315 end:
316 return ret;
317 }
318 #else
319 static
320 int _extract_elf_symbol_offset(struct run_as_data *data,
321 struct run_as_ret *ret_value)
322 {
323 ERR("Unimplemented runas command RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET");
324 return -1;
325 }
326
327 static
328 int _extract_sdt_probe_offsets(struct run_as_data *data,
329 struct run_as_ret *ret_value)
330 {
331 ERR("Unimplemented runas command RUN_AS_EXTRACT_SDT_PROBE_OFFSETS");
332 return -1;
333 }
334 #endif
335
336 static
337 run_as_fct run_as_enum_to_fct(enum run_as_cmd cmd)
338 {
339 switch (cmd) {
340 case RUN_AS_MKDIR:
341 case RUN_AS_MKDIRAT:
342 return _mkdirat;
343 case RUN_AS_MKDIR_RECURSIVE:
344 case RUN_AS_MKDIRAT_RECURSIVE:
345 return _mkdirat_recursive;
346 case RUN_AS_OPEN:
347 return _open;
348 case RUN_AS_UNLINK:
349 return _unlink;
350 case RUN_AS_RMDIR_RECURSIVE:
351 return _rmdir_recursive;
352 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
353 return _extract_elf_symbol_offset;
354 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
355 return _extract_sdt_probe_offsets;
356 default:
357 ERR("Unknown command %d", (int) cmd);
358 return NULL;
359 }
360 }
361
362 static
363 int do_send_fd(int sock, int fd)
364 {
365 ssize_t len;
366
367 if (fd < 0) {
368 ERR("Attempt to send invalid file descriptor to master (fd = %i)", fd);
369 /* Return 0 as this is not a fatal error. */
370 return 0;
371 }
372
373 len = lttcomm_send_fds_unix_sock(sock, &fd, 1);
374 if (len < 0) {
375 PERROR("lttcomm_send_fds_unix_sock");
376 return -1;
377 }
378 return 0;
379 }
380
381 static
382 int do_recv_fd(int sock, int *fd)
383 {
384 ssize_t len;
385
386 len = lttcomm_recv_fds_unix_sock(sock, fd, 1);
387
388 if (!len) {
389 return -1;
390 } else if (len < 0) {
391 PERROR("lttcomm_recv_fds_unix_sock");
392 return -1;
393 }
394 if (*fd < 0) {
395 ERR("Invalid file descriptor received from worker (fd = %i)", *fd);
396 /* Return 0 as this is not a fatal error. */
397 return 0;
398 }
399
400 return 0;
401 }
402
403 static
404 int send_fd_to_worker(struct run_as_worker *worker, enum run_as_cmd cmd, int fd)
405 {
406 int ret = 0;
407
408 switch (cmd) {
409 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
410 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
411 case RUN_AS_MKDIRAT:
412 case RUN_AS_MKDIRAT_RECURSIVE:
413 break;
414 default:
415 return 0;
416 }
417
418 if (fd < 0) {
419 ERR("Refusing to send invalid fd to worker (fd = %i)", fd);
420 return -1;
421 }
422
423 ret = do_send_fd(worker->sockpair[0], fd);
424 if (ret < 0) {
425 PERROR("do_send_fd");
426 ret = -1;
427 }
428
429 return ret;
430 }
431
432 static
433 int send_fd_to_master(struct run_as_worker *worker, enum run_as_cmd cmd, int fd)
434 {
435 int ret = 0, ret_close = 0;
436
437 switch (cmd) {
438 case RUN_AS_OPEN:
439 break;
440 default:
441 return 0;
442 }
443
444 if (fd < 0) {
445 DBG("Not sending file descriptor to master as it is invalid (fd = %i)", fd);
446 return 0;
447 }
448 ret = do_send_fd(worker->sockpair[1], fd);
449 if (ret < 0) {
450 PERROR("do_send_fd error");
451 ret = -1;
452 }
453
454 ret_close = close(fd);
455 if (ret_close < 0) {
456 PERROR("close");
457 }
458
459 return ret;
460 }
461
462 static
463 int recv_fd_from_worker(struct run_as_worker *worker, enum run_as_cmd cmd, int *fd)
464 {
465 int ret = 0;
466
467 switch (cmd) {
468 case RUN_AS_OPEN:
469 break;
470 default:
471 return 0;
472 }
473
474 ret = do_recv_fd(worker->sockpair[0], fd);
475 if (ret < 0) {
476 PERROR("do_recv_fd error");
477 ret = -1;
478 }
479
480 return ret;
481 }
482
483 static
484 int recv_fd_from_master(struct run_as_worker *worker, enum run_as_cmd cmd, int *fd)
485 {
486 int ret = 0;
487
488 switch (cmd) {
489 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
490 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
491 case RUN_AS_MKDIRAT:
492 case RUN_AS_MKDIRAT_RECURSIVE:
493 break;
494 case RUN_AS_MKDIR:
495 case RUN_AS_MKDIR_RECURSIVE:
496 *fd = AT_FDCWD;
497 /* fall-through */
498 default:
499 return 0;
500 }
501
502 ret = do_recv_fd(worker->sockpair[1], fd);
503 if (ret < 0) {
504 PERROR("do_recv_fd error");
505 ret = -1;
506 }
507
508 return ret;
509 }
510
511 static
512 int cleanup_received_fd(enum run_as_cmd cmd, int fd)
513 {
514 int ret = 0;
515
516 switch (cmd) {
517 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
518 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
519 case RUN_AS_MKDIRAT:
520 case RUN_AS_MKDIRAT_RECURSIVE:
521 break;
522 default:
523 return 0;
524 }
525
526 if (fd < 0) {
527 return 0;
528 }
529 ret = close(fd);
530 if (ret < 0) {
531 PERROR("close error");
532 ret = -1;
533 }
534
535 return ret;
536 }
537
538 /*
539 * Return < 0 on error, 0 if OK, 1 on hangup.
540 */
541 static
542 int handle_one_cmd(struct run_as_worker *worker)
543 {
544 int ret = 0;
545 struct run_as_data data;
546 ssize_t readlen, writelen;
547 struct run_as_ret sendret;
548 run_as_fct cmd;
549 uid_t prev_euid;
550
551 memset(&sendret, 0, sizeof(sendret));
552 sendret.fd = -1;
553
554 /*
555 * Stage 1: Receive run_as_data struct from the master.
556 * The structure contains the command type and all the parameters needed for
557 * its execution
558 */
559 readlen = lttcomm_recv_unix_sock(worker->sockpair[1], &data,
560 sizeof(data));
561 if (readlen == 0) {
562 /* hang up */
563 ret = 1;
564 goto end;
565 }
566 if (readlen < sizeof(data)) {
567 PERROR("lttcomm_recv_unix_sock error");
568 ret = -1;
569 goto end;
570 }
571
572 cmd = run_as_enum_to_fct(data.cmd);
573 if (!cmd) {
574 ret = -1;
575 goto end;
576 }
577
578 /*
579 * Stage 2: Receive file descriptor from master.
580 * Some commands need a file descriptor as input so if it's needed we
581 * receive the fd using the Unix socket.
582 */
583 ret = recv_fd_from_master(worker, data.cmd, &data.fd);
584 if (ret < 0) {
585 PERROR("recv_fd_from_master error");
586 ret = -1;
587 goto end;
588 }
589
590 prev_euid = getuid();
591 if (data.gid != getegid()) {
592 ret = setegid(data.gid);
593 if (ret < 0) {
594 sendret._error = true;
595 sendret._errno = errno;
596 PERROR("setegid");
597 goto write_return;
598 }
599 }
600 if (data.uid != prev_euid) {
601 ret = seteuid(data.uid);
602 if (ret < 0) {
603 sendret._error = true;
604 sendret._errno = errno;
605 PERROR("seteuid");
606 goto write_return;
607 }
608 }
609
610 /*
611 * Also set umask to 0 for mkdir executable bit.
612 */
613 umask(0);
614
615 /*
616 * Stage 3: Execute the command
617 */
618 ret = (*cmd)(&data, &sendret);
619 if (ret < 0) {
620 DBG("Execution of command returned an error");
621 }
622
623 write_return:
624 ret = cleanup_received_fd(data.cmd, data.fd);
625 if (ret < 0) {
626 ERR("Error cleaning up FD");
627 goto end;
628 }
629
630 /*
631 * Stage 4: Send run_as_ret structure to the master.
632 * This structure contain the return value of the command and the errno.
633 */
634 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
635 sizeof(sendret));
636 if (writelen < sizeof(sendret)) {
637 PERROR("lttcomm_send_unix_sock error");
638 ret = -1;
639 goto end;
640 }
641
642 /*
643 * Stage 5: Send file descriptor to the master
644 * Some commands return a file descriptor so if it's needed we pass it back
645 * to the master using the Unix socket.
646 */
647 ret = send_fd_to_master(worker, data.cmd, sendret.fd);
648 if (ret < 0) {
649 DBG("Sending FD to master returned an error");
650 goto end;
651 }
652
653 if (seteuid(prev_euid) < 0) {
654 PERROR("seteuid");
655 ret = -1;
656 goto end;
657 }
658 ret = 0;
659 end:
660 return ret;
661 }
662
663 static
664 int run_as_worker(struct run_as_worker *worker)
665 {
666 int ret;
667 ssize_t writelen;
668 struct run_as_ret sendret;
669 size_t proc_orig_len;
670
671 /*
672 * Initialize worker. Set a different process cmdline.
673 */
674 proc_orig_len = strlen(worker->procname);
675 memset(worker->procname, 0, proc_orig_len);
676 strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len);
677
678 ret = lttng_prctl(PR_SET_NAME,
679 (unsigned long) DEFAULT_RUN_AS_WORKER_NAME, 0, 0, 0);
680 if (ret && ret != -ENOSYS) {
681 /* Don't fail as this is not essential. */
682 PERROR("prctl PR_SET_NAME");
683 }
684
685 memset(&sendret, 0, sizeof(sendret));
686
687 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
688 sizeof(sendret));
689 if (writelen < sizeof(sendret)) {
690 PERROR("lttcomm_send_unix_sock error");
691 ret = EXIT_FAILURE;
692 goto end;
693 }
694
695 for (;;) {
696 ret = handle_one_cmd(worker);
697 if (ret < 0) {
698 ret = EXIT_FAILURE;
699 goto end;
700 } else if (ret > 0) {
701 break;
702 } else {
703 continue; /* Next command. */
704 }
705 }
706 ret = EXIT_SUCCESS;
707 end:
708 return ret;
709 }
710
711 static
712 int run_as_cmd(struct run_as_worker *worker,
713 enum run_as_cmd cmd,
714 struct run_as_data *data,
715 struct run_as_ret *ret_value,
716 uid_t uid, gid_t gid)
717 {
718 int ret = 0;
719 ssize_t readlen, writelen;
720
721 /*
722 * If we are non-root, we can only deal with our own uid.
723 */
724 if (geteuid() != 0) {
725 if (uid != geteuid()) {
726 ret = -1;
727 ret_value->_errno = EPERM;
728 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
729 (int) uid, (int) geteuid());
730 goto end;
731 }
732 }
733
734 data->cmd = cmd;
735 data->uid = uid;
736 data->gid = gid;
737
738 /*
739 * Stage 1: Send the run_as_data struct to the worker process
740 */
741 writelen = lttcomm_send_unix_sock(worker->sockpair[0], data,
742 sizeof(*data));
743 if (writelen < sizeof(*data)) {
744 PERROR("Error writing message to run_as");
745 ret = -1;
746 ret_value->_errno = EIO;
747 goto end;
748 }
749
750 /*
751 * Stage 2: Send file descriptor to the worker process if needed
752 */
753 ret = send_fd_to_worker(worker, data->cmd, data->fd);
754 if (ret) {
755 PERROR("do_send_fd error");
756 ret = -1;
757 ret_value->_errno = EIO;
758 goto end;
759 }
760
761 /*
762 * Stage 3: Wait for the execution of the command
763 */
764
765 /*
766 * Stage 4: Receive the run_as_ret struct containing the return value and
767 * errno
768 */
769 readlen = lttcomm_recv_unix_sock(worker->sockpair[0], ret_value,
770 sizeof(*ret_value));
771 if (!readlen) {
772 ERR("Run-as worker has hung-up during run_as_cmd");
773 ret = -1;
774 ret_value->_errno = EIO;
775 goto end;
776 } else if (readlen < sizeof(*ret_value)) {
777 PERROR("Error reading response from run_as");
778 ret = -1;
779 ret_value->_errno = errno;
780 goto end;
781 }
782
783 if (ret_value->_error) {
784 /* Skip stage 5 on error as there will be no fd to receive. */
785 goto end;
786 }
787
788 /*
789 * Stage 5: Receive file descriptor if needed
790 */
791 ret = recv_fd_from_worker(worker, data->cmd, &ret_value->fd);
792 if (ret < 0) {
793 ERR("Error receiving fd");
794 ret = -1;
795 ret_value->_errno = EIO;
796 }
797
798 end:
799 return ret;
800 }
801
802 /*
803 * This is for debugging ONLY and should not be considered secure.
804 */
805 static
806 int run_as_noworker(enum run_as_cmd cmd,
807 struct run_as_data *data, struct run_as_ret *ret_value,
808 uid_t uid, gid_t gid)
809 {
810 int ret, saved_errno;
811 mode_t old_mask;
812 run_as_fct fct;
813
814 fct = run_as_enum_to_fct(cmd);
815 if (!fct) {
816 errno = -ENOSYS;
817 ret = -1;
818 goto end;
819 }
820 old_mask = umask(0);
821 ret = fct(data, ret_value);
822 saved_errno = ret_value->_errno;
823 umask(old_mask);
824 errno = saved_errno;
825 end:
826 return ret;
827 }
828
829 static
830 int reset_sighandler(void)
831 {
832 int sig;
833
834 DBG("Resetting run_as worker signal handlers to default");
835 for (sig = 1; sig <= 31; sig++) {
836 (void) signal(sig, SIG_DFL);
837 }
838 return 0;
839 }
840
841 static
842 void worker_sighandler(int sig)
843 {
844 const char *signame;
845
846 /*
847 * The worker will inherit its parent's signals since they are part of
848 * the same process group. However, in the case of SIGINT and SIGTERM,
849 * we want to give the worker a chance to teardown gracefully when its
850 * parent closes the command socket.
851 */
852 switch (sig) {
853 case SIGINT:
854 signame = "SIGINT";
855 break;
856 case SIGTERM:
857 signame = "SIGTERM";
858 break;
859 default:
860 signame = NULL;
861 }
862
863 if (signame) {
864 DBG("run_as worker received signal %s", signame);
865 } else {
866 DBG("run_as_worker received signal %d", sig);
867 }
868 }
869
870 static
871 int set_worker_sighandlers(void)
872 {
873 int ret = 0;
874 sigset_t sigset;
875 struct sigaction sa;
876
877 if ((ret = sigemptyset(&sigset)) < 0) {
878 PERROR("sigemptyset");
879 goto end;
880 }
881
882 sa.sa_handler = worker_sighandler;
883 sa.sa_mask = sigset;
884 sa.sa_flags = 0;
885 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
886 PERROR("sigaction SIGINT");
887 goto end;
888 }
889
890 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
891 PERROR("sigaction SIGTERM");
892 goto end;
893 }
894
895 DBG("run_as signal handler set for SIGTERM and SIGINT");
896 end:
897 return ret;
898 }
899
900 static
901 int run_as_create_worker_no_lock(const char *procname,
902 post_fork_cleanup_cb clean_up_func,
903 void *clean_up_user_data)
904 {
905 pid_t pid;
906 int i, ret = 0;
907 ssize_t readlen;
908 struct run_as_ret recvret;
909 struct run_as_worker *worker;
910
911 assert(!global_worker);
912 if (!use_clone()) {
913 /*
914 * Don't initialize a worker, all run_as tasks will be performed
915 * in the current process.
916 */
917 ret = 0;
918 goto end;
919 }
920 worker = zmalloc(sizeof(*worker));
921 if (!worker) {
922 ret = -ENOMEM;
923 goto end;
924 }
925 worker->procname = strdup(procname);
926 if (!worker->procname) {
927 ret = -ENOMEM;
928 goto error_procname_alloc;
929 }
930 /* Create unix socket. */
931 if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) {
932 ret = -1;
933 goto error_sock;
934 }
935
936 /* Fork worker. */
937 pid = fork();
938 if (pid < 0) {
939 PERROR("fork");
940 ret = -1;
941 goto error_fork;
942 } else if (pid == 0) {
943 /* Child */
944
945 reset_sighandler();
946
947 set_worker_sighandlers();
948 if (clean_up_func) {
949 if (clean_up_func(clean_up_user_data) < 0) {
950 ERR("Run-as post-fork clean-up failed, exiting.");
951 exit(EXIT_FAILURE);
952 }
953 }
954
955 /* Just close, no shutdown. */
956 if (close(worker->sockpair[0])) {
957 PERROR("close");
958 exit(EXIT_FAILURE);
959 }
960
961 /*
962 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
963 * Sockpair[1] is used as a control channel with the master
964 */
965 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
966 if (i != worker->sockpair[1]) {
967 (void) close(i);
968 }
969 }
970
971 worker->sockpair[0] = -1;
972 ret = run_as_worker(worker);
973 if (lttcomm_close_unix_sock(worker->sockpair[1])) {
974 PERROR("close");
975 ret = -1;
976 }
977 worker->sockpair[1] = -1;
978 free(worker->procname);
979 free(worker);
980 LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret);
981 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
982 } else {
983 /* Parent */
984
985 /* Just close, no shutdown. */
986 if (close(worker->sockpair[1])) {
987 PERROR("close");
988 ret = -1;
989 goto error_fork;
990 }
991 worker->sockpair[1] = -1;
992 worker->pid = pid;
993 /* Wait for worker to become ready. */
994 readlen = lttcomm_recv_unix_sock(worker->sockpair[0],
995 &recvret, sizeof(recvret));
996 if (readlen < sizeof(recvret)) {
997 ERR("readlen: %zd", readlen);
998 PERROR("Error reading response from run_as at creation");
999 ret = -1;
1000 goto error_fork;
1001 }
1002 global_worker = worker;
1003 }
1004 end:
1005 return ret;
1006
1007 /* Error handling. */
1008 error_fork:
1009 for (i = 0; i < 2; i++) {
1010 if (worker->sockpair[i] < 0) {
1011 continue;
1012 }
1013 if (lttcomm_close_unix_sock(worker->sockpair[i])) {
1014 PERROR("close");
1015 }
1016 worker->sockpair[i] = -1;
1017 }
1018 error_sock:
1019 free(worker->procname);
1020 error_procname_alloc:
1021 free(worker);
1022 return ret;
1023 }
1024
1025 static
1026 void run_as_destroy_worker_no_lock(void)
1027 {
1028 struct run_as_worker *worker = global_worker;
1029
1030 DBG("Destroying run_as worker");
1031 if (!worker) {
1032 return;
1033 }
1034 /* Close unix socket */
1035 DBG("Closing run_as worker socket");
1036 if (lttcomm_close_unix_sock(worker->sockpair[0])) {
1037 PERROR("close");
1038 }
1039 worker->sockpair[0] = -1;
1040 /* Wait for worker. */
1041 for (;;) {
1042 int status;
1043 pid_t wait_ret;
1044
1045 wait_ret = waitpid(worker->pid, &status, 0);
1046 if (wait_ret < 0) {
1047 if (errno == EINTR) {
1048 continue;
1049 }
1050 PERROR("waitpid");
1051 break;
1052 }
1053
1054 if (WIFEXITED(status)) {
1055 LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR,
1056 DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d",
1057 WEXITSTATUS(status));
1058 break;
1059 } else if (WIFSIGNALED(status)) {
1060 ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d",
1061 WTERMSIG(status));
1062 break;
1063 }
1064 }
1065 free(worker->procname);
1066 free(worker);
1067 global_worker = NULL;
1068 }
1069
1070 static
1071 int run_as_restart_worker(struct run_as_worker *worker)
1072 {
1073 int ret = 0;
1074 char *procname = NULL;
1075
1076 procname = worker->procname;
1077
1078 /* Close socket to run_as worker process and clean up the zombie process */
1079 run_as_destroy_worker_no_lock();
1080
1081 /* Create a new run_as worker process*/
1082 ret = run_as_create_worker_no_lock(procname, NULL, NULL);
1083 if (ret < 0 ) {
1084 ERR("Restarting the worker process failed");
1085 ret = -1;
1086 goto err;
1087 }
1088 err:
1089 return ret;
1090 }
1091
1092 static
1093 int run_as(enum run_as_cmd cmd, struct run_as_data *data,
1094 struct run_as_ret *ret_value, uid_t uid, gid_t gid)
1095 {
1096 int ret, saved_errno;
1097
1098 pthread_mutex_lock(&worker_lock);
1099 if (use_clone()) {
1100 DBG("Using run_as worker");
1101
1102 assert(global_worker);
1103
1104 ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid);
1105 saved_errno = ret_value->_errno;
1106
1107 /*
1108 * If the worker thread crashed the errno is set to EIO. we log
1109 * the error and start a new worker process.
1110 */
1111 if (ret == -1 && saved_errno == EIO) {
1112 DBG("Socket closed unexpectedly... "
1113 "Restarting the worker process");
1114 ret = run_as_restart_worker(global_worker);
1115 if (ret == -1) {
1116 ERR("Failed to restart worker process.");
1117 goto err;
1118 }
1119 }
1120 } else {
1121 DBG("Using run_as without worker");
1122 ret = run_as_noworker(cmd, data, ret_value, uid, gid);
1123 }
1124 err:
1125 pthread_mutex_unlock(&worker_lock);
1126 return ret;
1127 }
1128
1129 LTTNG_HIDDEN
1130 int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
1131 {
1132 return run_as_mkdirat_recursive(AT_FDCWD, path, mode, uid, gid);
1133 }
1134
1135 LTTNG_HIDDEN
1136 int run_as_mkdirat_recursive(int dirfd, const char *path, mode_t mode,
1137 uid_t uid, gid_t gid)
1138 {
1139 int ret;
1140 struct run_as_data data;
1141 struct run_as_ret run_as_ret;
1142
1143 memset(&data, 0, sizeof(data));
1144 memset(&run_as_ret, 0, sizeof(run_as_ret));
1145 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1146 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1147 path, (int) mode, (int) uid, (int) gid);
1148 ret = lttng_strncpy(data.u.mkdirat.path, path,
1149 sizeof(data.u.mkdirat.path));
1150 if (ret) {
1151 ERR("Failed to copy path argument of mkdirat recursive command");
1152 goto error;
1153 }
1154 data.u.mkdirat.path[PATH_MAX - 1] = '\0';
1155 data.u.mkdirat.mode = mode;
1156 data.fd = dirfd;
1157 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR_RECURSIVE : RUN_AS_MKDIRAT_RECURSIVE,
1158 &data, &run_as_ret, uid, gid);
1159 errno = run_as_ret._errno;
1160 ret = run_as_ret.u.mkdirat.ret;
1161 error:
1162 return ret;
1163 }
1164
1165 LTTNG_HIDDEN
1166 int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
1167 {
1168 return run_as_mkdirat(AT_FDCWD, path, mode, uid, gid);
1169 }
1170
1171 LTTNG_HIDDEN
1172 int run_as_mkdirat(int dirfd, const char *path, mode_t mode,
1173 uid_t uid, gid_t gid)
1174 {
1175 int ret;
1176 struct run_as_data data;
1177 struct run_as_ret run_as_ret;
1178
1179 memset(&data, 0, sizeof(data));
1180 memset(&run_as_ret, 0, sizeof(run_as_ret));
1181
1182 DBG3("mkdirat() recursive fd = %d%s, path = %s, mode = %d, uid = %d, gid = %d",
1183 dirfd, dirfd == AT_FDCWD ? " (AT_FDCWD)" : "",
1184 path, (int) mode, (int) uid, (int) gid);
1185 ret = lttng_strncpy(data.u.mkdirat.path, path,
1186 sizeof(data.u.mkdirat.path));
1187 if (ret) {
1188 ERR("Failed to copy path argument of mkdirat command");
1189 goto error;
1190 }
1191 data.u.mkdirat.path[PATH_MAX - 1] = '\0';
1192 data.u.mkdirat.mode = mode;
1193 data.fd = dirfd;
1194 run_as(dirfd == AT_FDCWD ? RUN_AS_MKDIR : RUN_AS_MKDIRAT,
1195 &data, &run_as_ret, uid, gid);
1196 errno = run_as_ret._errno;
1197 ret = run_as_ret._errno;
1198 error:
1199 return ret;
1200 }
1201
1202 LTTNG_HIDDEN
1203 int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
1204 {
1205 struct run_as_data data;
1206 struct run_as_ret ret;
1207
1208 memset(&data, 0, sizeof(data));
1209 memset(&ret, 0, sizeof(ret));
1210
1211 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
1212 path, flags, (int) mode, (int) uid, (int) gid);
1213 strncpy(data.u.open.path, path, PATH_MAX - 1);
1214 data.u.open.path[PATH_MAX - 1] = '\0';
1215 data.u.open.flags = flags;
1216 data.u.open.mode = mode;
1217 run_as(RUN_AS_OPEN, &data, &ret, uid, gid);
1218 errno = ret._errno;
1219 ret.u.open.ret = ret.fd;
1220 return ret.u.open.ret;
1221 }
1222
1223 LTTNG_HIDDEN
1224 int run_as_unlink(const char *path, uid_t uid, gid_t gid)
1225 {
1226 struct run_as_data data;
1227 struct run_as_ret ret;
1228
1229 memset(&data, 0, sizeof(data));
1230 memset(&ret, 0, sizeof(ret));
1231
1232 DBG3("unlink() %s with for uid %d and gid %d",
1233 path, (int) uid, (int) gid);
1234 strncpy(data.u.unlink.path, path, PATH_MAX - 1);
1235 data.u.unlink.path[PATH_MAX - 1] = '\0';
1236 run_as(RUN_AS_UNLINK, &data, &ret, uid, gid);
1237 errno = ret._errno;
1238 return ret.u.unlink.ret;
1239 }
1240
1241 LTTNG_HIDDEN
1242 int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid)
1243 {
1244 struct run_as_data data;
1245 struct run_as_ret ret;
1246
1247 memset(&data, 0, sizeof(data));
1248 memset(&ret, 0, sizeof(ret));
1249
1250 DBG3("rmdir_recursive() %s with for uid %d and gid %d",
1251 path, (int) uid, (int) gid);
1252 strncpy(data.u.rmdir_recursive.path, path, PATH_MAX - 1);
1253 data.u.rmdir_recursive.path[PATH_MAX - 1] = '\0';
1254 run_as(RUN_AS_RMDIR_RECURSIVE, &data, &ret, uid, gid);
1255 errno = ret._errno;
1256 return ret.u.rmdir_recursive.ret;
1257 }
1258
1259 LTTNG_HIDDEN
1260 int run_as_extract_elf_symbol_offset(int fd, const char* function,
1261 uid_t uid, gid_t gid, uint64_t *offset)
1262 {
1263 struct run_as_data data;
1264 struct run_as_ret ret;
1265
1266 memset(&data, 0, sizeof(data));
1267 memset(&ret, 0, sizeof(ret));
1268
1269 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
1270 "with for uid %d and gid %d", fd, function, (int) uid, (int) gid);
1271
1272 data.fd = fd;
1273
1274 strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1);
1275
1276 data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1277
1278 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &ret, uid, gid);
1279
1280 errno = ret._errno;
1281
1282 if (ret._error) {
1283 return -1;
1284 }
1285
1286 *offset = ret.u.extract_elf_symbol_offset.offset;
1287 return 0;
1288 }
1289
1290 LTTNG_HIDDEN
1291 int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name,
1292 const char* probe_name, uid_t uid, gid_t gid,
1293 uint64_t **offsets, uint32_t *num_offset)
1294 {
1295 struct run_as_data data;
1296 struct run_as_ret ret;
1297
1298 memset(&data, 0, sizeof(data));
1299 memset(&ret, 0, sizeof(ret));
1300
1301 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
1302 "provider_name=%s with for uid %d and gid %d", fd, probe_name,
1303 provider_name, (int) uid, (int) gid);
1304
1305 data.fd = fd;
1306
1307 strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name, LTTNG_SYMBOL_NAME_LEN - 1);
1308 strncpy(data.u.extract_sdt_probe_offsets.provider_name, provider_name, LTTNG_SYMBOL_NAME_LEN - 1);
1309
1310 data.u.extract_sdt_probe_offsets.probe_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1311 data.u.extract_sdt_probe_offsets.provider_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1312
1313 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &ret, uid, gid);
1314
1315 errno = ret._errno;
1316
1317 if (ret._error) {
1318 return -1;
1319 }
1320
1321 *num_offset = ret.u.extract_sdt_probe_offsets.num_offset;
1322
1323 *offsets = zmalloc(*num_offset * sizeof(uint64_t));
1324 if (!*offsets) {
1325 return -ENOMEM;
1326 }
1327
1328 memcpy(*offsets, ret.u.extract_sdt_probe_offsets.offsets, *num_offset * sizeof(uint64_t));
1329 return 0;
1330 }
1331
1332 LTTNG_HIDDEN
1333 int run_as_create_worker(const char *procname,
1334 post_fork_cleanup_cb clean_up_func,
1335 void *clean_up_user_data)
1336 {
1337 int ret;
1338
1339 pthread_mutex_lock(&worker_lock);
1340 ret = run_as_create_worker_no_lock(procname, clean_up_func,
1341 clean_up_user_data);
1342 pthread_mutex_unlock(&worker_lock);
1343 return ret;
1344 }
1345
1346 LTTNG_HIDDEN
1347 void run_as_destroy_worker(void)
1348 {
1349 pthread_mutex_lock(&worker_lock);
1350 run_as_destroy_worker_no_lock();
1351 pthread_mutex_unlock(&worker_lock);
1352 }
This page took 0.058481 seconds and 4 git commands to generate.