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