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