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