c4bb298bfab938735aba850bb163cefa1e0424cd
[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("Attempt to send invalid file descriptor to master (fd = %i)", fd);
333 /* Return 0 as this is not a fatal error. */
334 return 0;
335 }
336
337 len = lttcomm_send_fds_unix_sock(sock, &fd, 1);
338 if (len < 0) {
339 PERROR("lttcomm_send_fds_unix_sock");
340 return -1;
341 }
342 return 0;
343 }
344
345 static
346 int do_recv_fd(int sock, int *fd)
347 {
348 ssize_t len;
349
350 len = lttcomm_recv_fds_unix_sock(sock, fd, 1);
351
352 if (!len) {
353 return -1;
354 } else if (len < 0) {
355 PERROR("lttcomm_recv_fds_unix_sock");
356 return -1;
357 }
358 if (*fd < 0) {
359 ERR("Invalid file descriptor received from worker (fd = %i)", *fd);
360 /* Return 0 as this is not a fatal error. */
361 return 0;
362 }
363
364 return 0;
365 }
366
367 static
368 int send_fd_to_worker(struct run_as_worker *worker, enum run_as_cmd cmd, int fd)
369 {
370 int ret = 0;
371
372 switch (cmd) {
373 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
374 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
375 break;
376 default:
377 return 0;
378 }
379
380 if (fd < 0) {
381 ERR("Refusing to send invalid fd to worker (fd = %i)", fd);
382 return -1;
383 }
384
385 ret = do_send_fd(worker->sockpair[0], fd);
386 if (ret < 0) {
387 PERROR("do_send_fd");
388 ret = -1;
389 }
390
391 return ret;
392 }
393
394 static
395 int send_fd_to_master(struct run_as_worker *worker, enum run_as_cmd cmd, int fd)
396 {
397 int ret = 0, ret_close = 0;
398
399 switch (cmd) {
400 case RUN_AS_OPEN:
401 break;
402 default:
403 return 0;
404 }
405
406 if (fd < 0) {
407 DBG("Not sending file descriptor to master as it is invalid (fd = %i)", fd);
408 return 0;
409 }
410 ret = do_send_fd(worker->sockpair[1], fd);
411 if (ret < 0) {
412 PERROR("do_send_fd error");
413 ret = -1;
414 }
415
416 ret_close = close(fd);
417 if (ret_close < 0) {
418 PERROR("close");
419 }
420
421 return ret;
422 }
423
424 static
425 int recv_fd_from_worker(struct run_as_worker *worker, enum run_as_cmd cmd, int *fd)
426 {
427 int ret = 0;
428
429 switch (cmd) {
430 case RUN_AS_OPEN:
431 break;
432 default:
433 return 0;
434 }
435
436 ret = do_recv_fd(worker->sockpair[0], fd);
437 if (ret < 0) {
438 PERROR("do_recv_fd error");
439 ret = -1;
440 }
441
442 return ret;
443 }
444
445 static
446 int recv_fd_from_master(struct run_as_worker *worker, enum run_as_cmd cmd, int *fd)
447 {
448 int ret = 0;
449
450 switch (cmd) {
451 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
452 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
453 break;
454 default:
455 return 0;
456 }
457
458 ret = do_recv_fd(worker->sockpair[1], fd);
459 if (ret < 0) {
460 PERROR("do_recv_fd error");
461 ret = -1;
462 }
463
464 return ret;
465 }
466
467 static
468 int cleanup_received_fd(enum run_as_cmd cmd, int fd)
469 {
470 int ret = 0;
471
472 switch (cmd) {
473 case RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET:
474 case RUN_AS_EXTRACT_SDT_PROBE_OFFSETS:
475 break;
476 default:
477 return 0;
478 }
479
480 if (fd < 0) {
481 return 0;
482 }
483 ret = close(fd);
484 if (ret < 0) {
485 PERROR("close error");
486 ret = -1;
487 }
488
489 return ret;
490 }
491
492 /*
493 * Return < 0 on error, 0 if OK, 1 on hangup.
494 */
495 static
496 int handle_one_cmd(struct run_as_worker *worker)
497 {
498 int ret = 0;
499 struct run_as_data data;
500 ssize_t readlen, writelen;
501 struct run_as_ret sendret;
502 run_as_fct cmd;
503 uid_t prev_euid;
504
505 memset(&sendret, 0, sizeof(sendret));
506 sendret.fd = -1;
507
508 /*
509 * Stage 1: Receive run_as_data struct from the master.
510 * The structure contains the command type and all the parameters needed for
511 * its execution
512 */
513 readlen = lttcomm_recv_unix_sock(worker->sockpair[1], &data,
514 sizeof(data));
515 if (readlen == 0) {
516 /* hang up */
517 ret = 1;
518 goto end;
519 }
520 if (readlen < sizeof(data)) {
521 PERROR("lttcomm_recv_unix_sock error");
522 ret = -1;
523 goto end;
524 }
525
526 cmd = run_as_enum_to_fct(data.cmd);
527 if (!cmd) {
528 ret = -1;
529 goto end;
530 }
531
532 /*
533 * Stage 2: Receive file descriptor from master.
534 * Some commands need a file descriptor as input so if it's needed we
535 * receive the fd using the Unix socket.
536 */
537 ret = recv_fd_from_master(worker, data.cmd, &data.fd);
538 if (ret < 0) {
539 PERROR("recv_fd_from_master error");
540 ret = -1;
541 goto end;
542 }
543
544 prev_euid = getuid();
545 if (data.gid != getegid()) {
546 ret = setegid(data.gid);
547 if (ret < 0) {
548 sendret._error = true;
549 sendret._errno = errno;
550 PERROR("setegid");
551 goto write_return;
552 }
553 }
554 if (data.uid != prev_euid) {
555 ret = seteuid(data.uid);
556 if (ret < 0) {
557 sendret._error = true;
558 sendret._errno = errno;
559 PERROR("seteuid");
560 goto write_return;
561 }
562 }
563
564 /*
565 * Also set umask to 0 for mkdir executable bit.
566 */
567 umask(0);
568
569 /*
570 * Stage 3: Execute the command
571 */
572 ret = (*cmd)(&data, &sendret);
573 if (ret < 0) {
574 DBG("Execution of command returned an error");
575 }
576
577 write_return:
578 ret = cleanup_received_fd(data.cmd, data.fd);
579 if (ret < 0) {
580 ERR("Error cleaning up FD");
581 goto end;
582 }
583
584 /*
585 * Stage 4: Send run_as_ret structure to the master.
586 * This structure contain the return value of the command and the errno.
587 */
588 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
589 sizeof(sendret));
590 if (writelen < sizeof(sendret)) {
591 PERROR("lttcomm_send_unix_sock error");
592 ret = -1;
593 goto end;
594 }
595
596 /*
597 * Stage 5: Send file descriptor to the master
598 * Some commands return a file descriptor so if it's needed we pass it back
599 * to the master using the Unix socket.
600 */
601 ret = send_fd_to_master(worker, data.cmd, sendret.fd);
602 if (ret < 0) {
603 DBG("Sending FD to master returned an error");
604 goto end;
605 }
606
607 if (seteuid(prev_euid) < 0) {
608 PERROR("seteuid");
609 ret = -1;
610 goto end;
611 }
612 ret = 0;
613 end:
614 return ret;
615 }
616
617 static
618 int run_as_worker(struct run_as_worker *worker)
619 {
620 int ret;
621 ssize_t writelen;
622 struct run_as_ret sendret;
623 size_t proc_orig_len;
624
625 /*
626 * Initialize worker. Set a different process cmdline.
627 */
628 proc_orig_len = strlen(worker->procname);
629 memset(worker->procname, 0, proc_orig_len);
630 strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len);
631
632 ret = lttng_prctl(PR_SET_NAME,
633 (unsigned long) DEFAULT_RUN_AS_WORKER_NAME, 0, 0, 0);
634 if (ret && ret != -ENOSYS) {
635 /* Don't fail as this is not essential. */
636 PERROR("prctl PR_SET_NAME");
637 }
638
639 memset(&sendret, 0, sizeof(sendret));
640
641 writelen = lttcomm_send_unix_sock(worker->sockpair[1], &sendret,
642 sizeof(sendret));
643 if (writelen < sizeof(sendret)) {
644 PERROR("lttcomm_send_unix_sock error");
645 ret = EXIT_FAILURE;
646 goto end;
647 }
648
649 for (;;) {
650 ret = handle_one_cmd(worker);
651 if (ret < 0) {
652 ret = EXIT_FAILURE;
653 goto end;
654 } else if (ret > 0) {
655 break;
656 } else {
657 continue; /* Next command. */
658 }
659 }
660 ret = EXIT_SUCCESS;
661 end:
662 return ret;
663 }
664
665 static
666 int run_as_cmd(struct run_as_worker *worker,
667 enum run_as_cmd cmd,
668 struct run_as_data *data,
669 struct run_as_ret *ret_value,
670 uid_t uid, gid_t gid)
671 {
672 int ret = 0;
673 ssize_t readlen, writelen;
674
675 /*
676 * If we are non-root, we can only deal with our own uid.
677 */
678 if (geteuid() != 0) {
679 if (uid != geteuid()) {
680 ret = -1;
681 ret_value->_errno = EPERM;
682 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
683 (int) uid, (int) geteuid());
684 goto end;
685 }
686 }
687
688 data->cmd = cmd;
689 data->uid = uid;
690 data->gid = gid;
691
692 /*
693 * Stage 1: Send the run_as_data struct to the worker process
694 */
695 writelen = lttcomm_send_unix_sock(worker->sockpair[0], data,
696 sizeof(*data));
697 if (writelen < sizeof(*data)) {
698 PERROR("Error writing message to run_as");
699 ret = -1;
700 ret_value->_errno = EIO;
701 goto end;
702 }
703
704 /*
705 * Stage 2: Send file descriptor to the worker process if needed
706 */
707 ret = send_fd_to_worker(worker, data->cmd, data->fd);
708 if (ret) {
709 PERROR("do_send_fd error");
710 ret = -1;
711 ret_value->_errno = EIO;
712 goto end;
713 }
714
715 /*
716 * Stage 3: Wait for the execution of the command
717 */
718
719 /*
720 * Stage 4: Receive the run_as_ret struct containing the return value and
721 * errno
722 */
723 readlen = lttcomm_recv_unix_sock(worker->sockpair[0], ret_value,
724 sizeof(*ret_value));
725 if (!readlen) {
726 ERR("Run-as worker has hung-up during run_as_cmd");
727 ret = -1;
728 ret_value->_errno = EIO;
729 goto end;
730 } else if (readlen < sizeof(*ret_value)) {
731 PERROR("Error reading response from run_as");
732 ret = -1;
733 ret_value->_errno = errno;
734 goto end;
735 }
736
737 if (ret_value->_error) {
738 /* Skip stage 5 on error as there will be no fd to receive. */
739 goto end;
740 }
741
742 /*
743 * Stage 5: Receive file descriptor if needed
744 */
745 ret = recv_fd_from_worker(worker, data->cmd, &ret_value->fd);
746 if (ret < 0) {
747 ERR("Error receiving fd");
748 ret = -1;
749 ret_value->_errno = EIO;
750 }
751
752 end:
753 return ret;
754 }
755
756 /*
757 * This is for debugging ONLY and should not be considered secure.
758 */
759 static
760 int run_as_noworker(enum run_as_cmd cmd,
761 struct run_as_data *data, struct run_as_ret *ret_value,
762 uid_t uid, gid_t gid)
763 {
764 int ret, saved_errno;
765 mode_t old_mask;
766 run_as_fct fct;
767
768 fct = run_as_enum_to_fct(cmd);
769 if (!fct) {
770 errno = -ENOSYS;
771 ret = -1;
772 goto end;
773 }
774 old_mask = umask(0);
775 ret = fct(data, ret_value);
776 saved_errno = ret_value->_errno;
777 umask(old_mask);
778 errno = saved_errno;
779 end:
780 return ret;
781 }
782
783 static
784 int run_as_restart_worker(struct run_as_worker *worker)
785 {
786 int ret = 0;
787 char *procname = NULL;
788
789 procname = worker->procname;
790
791 /* Close socket to run_as worker process and clean up the zombie process */
792 run_as_destroy_worker();
793
794 /* Create a new run_as worker process*/
795 ret = run_as_create_worker(procname);
796 if (ret < 0 ) {
797 ERR("Restarting the worker process failed");
798 ret = -1;
799 goto err;
800 }
801 err:
802 return ret;
803 }
804
805 static
806 int run_as(enum run_as_cmd cmd, struct run_as_data *data,
807 struct run_as_ret *ret_value, uid_t uid, gid_t gid)
808 {
809 int ret, saved_errno;
810
811 if (use_clone()) {
812 DBG("Using run_as worker");
813 pthread_mutex_lock(&worker_lock);
814 assert(global_worker);
815
816 ret = run_as_cmd(global_worker, cmd, data, ret_value, uid, gid);
817 saved_errno = ret_value->_errno;
818
819 pthread_mutex_unlock(&worker_lock);
820 /*
821 * If the worker thread crashed the errno is set to EIO. we log
822 * the error and start a new worker process.
823 */
824 if (ret == -1 && saved_errno == EIO) {
825 DBG("Socket closed unexpectedly... "
826 "Restarting the worker process");
827 ret = run_as_restart_worker(global_worker);
828 if (ret == -1) {
829 ERR("Failed to restart worker process.");
830 goto err;
831 }
832 }
833 } else {
834 DBG("Using run_as without worker");
835 ret = run_as_noworker(cmd, data, ret_value, uid, gid);
836 }
837 err:
838 return ret;
839 }
840
841 LTTNG_HIDDEN
842 int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
843 {
844 struct run_as_data data;
845 struct run_as_ret ret;
846
847 memset(&data, 0, sizeof(data));
848 memset(&ret, 0, sizeof(ret));
849 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
850 path, (int) mode, (int) uid, (int) gid);
851 strncpy(data.u.mkdir.path, path, PATH_MAX - 1);
852 data.u.mkdir.path[PATH_MAX - 1] = '\0';
853 data.u.mkdir.mode = mode;
854
855 run_as(RUN_AS_MKDIR_RECURSIVE, &data, &ret, uid, gid);
856 errno = ret._errno;
857 return ret.u.mkdir.ret;
858 }
859
860 LTTNG_HIDDEN
861 int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
862 {
863 struct run_as_data data;
864 struct run_as_ret ret;
865
866 memset(&data, 0, sizeof(data));
867 memset(&ret, 0, sizeof(ret));
868
869 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
870 path, (int) mode, (int) uid, (int) gid);
871 strncpy(data.u.mkdir.path, path, PATH_MAX - 1);
872 data.u.mkdir.path[PATH_MAX - 1] = '\0';
873 data.u.mkdir.mode = mode;
874 run_as(RUN_AS_MKDIR, &data, &ret, uid, gid);
875 errno = ret._errno;
876 return ret.u.mkdir.ret;
877 }
878
879 LTTNG_HIDDEN
880 int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
881 {
882 struct run_as_data data;
883 struct run_as_ret ret;
884
885 memset(&data, 0, sizeof(data));
886 memset(&ret, 0, sizeof(ret));
887
888 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
889 path, flags, (int) mode, (int) uid, (int) gid);
890 strncpy(data.u.open.path, path, PATH_MAX - 1);
891 data.u.open.path[PATH_MAX - 1] = '\0';
892 data.u.open.flags = flags;
893 data.u.open.mode = mode;
894 run_as(RUN_AS_OPEN, &data, &ret, uid, gid);
895 errno = ret._errno;
896 ret.u.open.ret = ret.fd;
897 return ret.u.open.ret;
898 }
899
900 LTTNG_HIDDEN
901 int run_as_unlink(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("unlink() %s with for uid %d and gid %d",
910 path, (int) uid, (int) gid);
911 strncpy(data.u.unlink.path, path, PATH_MAX - 1);
912 data.u.unlink.path[PATH_MAX - 1] = '\0';
913 run_as(RUN_AS_UNLINK, &data, &ret, uid, gid);
914 errno = ret._errno;
915 return ret.u.unlink.ret;
916 }
917
918 LTTNG_HIDDEN
919 int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid)
920 {
921 struct run_as_data data;
922 struct run_as_ret ret;
923
924 memset(&data, 0, sizeof(data));
925 memset(&ret, 0, sizeof(ret));
926
927 DBG3("rmdir_recursive() %s with for uid %d and gid %d",
928 path, (int) uid, (int) gid);
929 strncpy(data.u.rmdir_recursive.path, path, PATH_MAX - 1);
930 data.u.rmdir_recursive.path[PATH_MAX - 1] = '\0';
931 run_as(RUN_AS_RMDIR_RECURSIVE, &data, &ret, uid, gid);
932 errno = ret._errno;
933 return ret.u.rmdir_recursive.ret;
934 }
935
936 LTTNG_HIDDEN
937 int run_as_extract_elf_symbol_offset(int fd, const char* function,
938 uid_t uid, gid_t gid, uint64_t *offset)
939 {
940 struct run_as_data data;
941 struct run_as_ret ret;
942
943 memset(&data, 0, sizeof(data));
944 memset(&ret, 0, sizeof(ret));
945
946 DBG3("extract_elf_symbol_offset() on fd=%d and function=%s "
947 "with for uid %d and gid %d", fd, function, (int) uid, (int) gid);
948
949 data.fd = fd;
950
951 strncpy(data.u.extract_elf_symbol_offset.function, function, LTTNG_SYMBOL_NAME_LEN - 1);
952
953 data.u.extract_elf_symbol_offset.function[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
954
955 run_as(RUN_AS_EXTRACT_ELF_SYMBOL_OFFSET, &data, &ret, uid, gid);
956
957 errno = ret._errno;
958
959 if (ret._error) {
960 return -1;
961 }
962
963 *offset = ret.u.extract_elf_symbol_offset.offset;
964 return 0;
965 }
966
967 LTTNG_HIDDEN
968 int run_as_extract_sdt_probe_offsets(int fd, const char* provider_name,
969 const char* probe_name, uid_t uid, gid_t gid,
970 uint64_t **offsets, uint32_t *num_offset)
971 {
972 struct run_as_data data;
973 struct run_as_ret ret;
974
975 memset(&data, 0, sizeof(data));
976 memset(&ret, 0, sizeof(ret));
977
978 DBG3("extract_sdt_probe_offsets() on fd=%d, probe_name=%s and "
979 "provider_name=%s with for uid %d and gid %d", fd, probe_name,
980 provider_name, (int) uid, (int) gid);
981
982 data.fd = fd;
983
984 strncpy(data.u.extract_sdt_probe_offsets.probe_name, probe_name, LTTNG_SYMBOL_NAME_LEN - 1);
985 strncpy(data.u.extract_sdt_probe_offsets.provider_name, provider_name, LTTNG_SYMBOL_NAME_LEN - 1);
986
987 data.u.extract_sdt_probe_offsets.probe_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
988 data.u.extract_sdt_probe_offsets.provider_name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
989
990 run_as(RUN_AS_EXTRACT_SDT_PROBE_OFFSETS, &data, &ret, uid, gid);
991
992 errno = ret._errno;
993
994 if (ret._error) {
995 return -1;
996 }
997
998 *num_offset = ret.u.extract_sdt_probe_offsets.num_offset;
999
1000 *offsets = zmalloc(*num_offset * sizeof(uint64_t));
1001 if (!*offsets) {
1002 return -ENOMEM;
1003 }
1004
1005 memcpy(*offsets, ret.u.extract_sdt_probe_offsets.offsets, *num_offset * sizeof(uint64_t));
1006 return 0;
1007 }
1008
1009 static
1010 int reset_sighandler(void)
1011 {
1012 int sig;
1013
1014 DBG("Resetting run_as worker signal handlers to default");
1015 for (sig = 1; sig <= 31; sig++) {
1016 (void) signal(sig, SIG_DFL);
1017 }
1018 return 0;
1019 }
1020
1021 static
1022 void worker_sighandler(int sig)
1023 {
1024 const char *signame;
1025
1026 /*
1027 * The worker will inherit its parent's signals since they are part of
1028 * the same process group. However, in the case of SIGINT and SIGTERM,
1029 * we want to give the worker a chance to teardown gracefully when its
1030 * parent closes the command socket.
1031 */
1032 switch (sig) {
1033 case SIGINT:
1034 signame = "SIGINT";
1035 break;
1036 case SIGTERM:
1037 signame = "SIGTERM";
1038 break;
1039 default:
1040 signame = NULL;
1041 }
1042
1043 if (signame) {
1044 DBG("run_as worker received signal %s", signame);
1045 } else {
1046 DBG("run_as_worker received signal %d", sig);
1047 }
1048 }
1049
1050 static
1051 int set_worker_sighandlers(void)
1052 {
1053 int ret = 0;
1054 sigset_t sigset;
1055 struct sigaction sa;
1056
1057 if ((ret = sigemptyset(&sigset)) < 0) {
1058 PERROR("sigemptyset");
1059 goto end;
1060 }
1061
1062 sa.sa_handler = worker_sighandler;
1063 sa.sa_mask = sigset;
1064 sa.sa_flags = 0;
1065 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
1066 PERROR("sigaction SIGINT");
1067 goto end;
1068 }
1069
1070 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
1071 PERROR("sigaction SIGTERM");
1072 goto end;
1073 }
1074
1075 DBG("run_as signal handler set for SIGTERM and SIGINT");
1076 end:
1077 return ret;
1078 }
1079
1080 LTTNG_HIDDEN
1081 int run_as_create_worker(char *procname)
1082 {
1083 pid_t pid;
1084 int i, ret = 0;
1085 ssize_t readlen;
1086 struct run_as_ret recvret;
1087 struct run_as_worker *worker;
1088
1089 pthread_mutex_lock(&worker_lock);
1090 assert(!global_worker);
1091 if (!use_clone()) {
1092 /*
1093 * Don't initialize a worker, all run_as tasks will be performed
1094 * in the current process.
1095 */
1096 ret = 0;
1097 goto end;
1098 }
1099 worker = zmalloc(sizeof(*worker));
1100 if (!worker) {
1101 ret = -ENOMEM;
1102 goto end;
1103 }
1104 worker->procname = procname;
1105 /* Create unix socket. */
1106 if (lttcomm_create_anon_unix_socketpair(worker->sockpair) < 0) {
1107 ret = -1;
1108 goto error_sock;
1109 }
1110
1111 /* Fork worker. */
1112 pid = fork();
1113 if (pid < 0) {
1114 PERROR("fork");
1115 ret = -1;
1116 goto error_fork;
1117 } else if (pid == 0) {
1118 /* Child */
1119
1120 reset_sighandler();
1121
1122 set_worker_sighandlers();
1123
1124 /* The child has no use for this lock. */
1125 pthread_mutex_unlock(&worker_lock);
1126 /* Just close, no shutdown. */
1127 if (close(worker->sockpair[0])) {
1128 PERROR("close");
1129 exit(EXIT_FAILURE);
1130 }
1131
1132 /*
1133 * Close all FDs aside from STDIN, STDOUT, STDERR and sockpair[1]
1134 * Sockpair[1] is used as a control channel with the master
1135 */
1136 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
1137 if (i != worker->sockpair[1]) {
1138 (void) close(i);
1139 }
1140 }
1141
1142 worker->sockpair[0] = -1;
1143 ret = run_as_worker(worker);
1144 if (lttcomm_close_unix_sock(worker->sockpair[1])) {
1145 PERROR("close");
1146 ret = -1;
1147 }
1148 worker->sockpair[1] = -1;
1149 LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret);
1150 exit(ret ? EXIT_FAILURE : EXIT_SUCCESS);
1151 } else {
1152 /* Parent */
1153
1154 /* Just close, no shutdown. */
1155 if (close(worker->sockpair[1])) {
1156 PERROR("close");
1157 ret = -1;
1158 goto error_fork;
1159 }
1160 worker->sockpair[1] = -1;
1161 worker->pid = pid;
1162 /* Wait for worker to become ready. */
1163 readlen = lttcomm_recv_unix_sock(worker->sockpair[0],
1164 &recvret, sizeof(recvret));
1165 if (readlen < sizeof(recvret)) {
1166 ERR("readlen: %zd", readlen);
1167 PERROR("Error reading response from run_as at creation");
1168 ret = -1;
1169 goto error_fork;
1170 }
1171 global_worker = worker;
1172 }
1173 end:
1174 pthread_mutex_unlock(&worker_lock);
1175 return ret;
1176
1177 /* Error handling. */
1178 error_fork:
1179 for (i = 0; i < 2; i++) {
1180 if (worker->sockpair[i] < 0) {
1181 continue;
1182 }
1183 if (lttcomm_close_unix_sock(worker->sockpair[i])) {
1184 PERROR("close");
1185 }
1186 worker->sockpair[i] = -1;
1187 }
1188 error_sock:
1189 free(worker);
1190 pthread_mutex_unlock(&worker_lock);
1191 return ret;
1192 }
1193
1194 LTTNG_HIDDEN
1195 void run_as_destroy_worker(void)
1196 {
1197 struct run_as_worker *worker = global_worker;
1198
1199 DBG("Destroying run_as worker");
1200 pthread_mutex_lock(&worker_lock);
1201 if (!worker) {
1202 goto end;
1203 }
1204 /* Close unix socket */
1205 DBG("Closing run_as worker socket");
1206 if (lttcomm_close_unix_sock(worker->sockpair[0])) {
1207 PERROR("close");
1208 }
1209 worker->sockpair[0] = -1;
1210 /* Wait for worker. */
1211 for (;;) {
1212 int status;
1213 pid_t wait_ret;
1214
1215 wait_ret = waitpid(worker->pid, &status, 0);
1216 if (wait_ret < 0) {
1217 if (errno == EINTR) {
1218 continue;
1219 }
1220 PERROR("waitpid");
1221 break;
1222 }
1223
1224 if (WIFEXITED(status)) {
1225 LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR,
1226 DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d",
1227 WEXITSTATUS(status));
1228 break;
1229 } else if (WIFSIGNALED(status)) {
1230 ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d",
1231 WTERMSIG(status));
1232 break;
1233 }
1234 }
1235 free(worker);
1236 global_worker = NULL;
1237 end:
1238 pthread_mutex_unlock(&worker_lock);
1239 }
This page took 0.056432 seconds and 3 git commands to generate.