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