1 /* $OpenBSD: proc.c,v 1.41 2021/12/04 06:52:58 florian Exp $ */
4 * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
23 /* #include <sys/queue.h> XXX provided by gmid.h */
24 /* #include <sys/tree.h> XXX provided by gmid.h */
25 #include <sys/socket.h>
39 /* #include <event.h> XXX provided by gmid.h */
40 /* #include <imsg.h> XXX provided by gmid.h */
45 void proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int,
47 void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
48 void proc_open(struct privsep *, int, int);
49 void proc_accept(struct privsep *, int, enum privsep_procid,
51 void proc_close(struct privsep *);
52 void proc_shutdown(struct privsep_proc *);
53 void proc_sig_handler(int, short, void *);
54 void proc_range(struct privsep *, enum privsep_procid, int *, int *);
55 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
58 proc_getid(struct privsep_proc *procs, unsigned int nproc,
59 const char *proc_name)
61 struct privsep_proc *p;
64 for (proc = 0; proc < nproc; proc++) {
66 if (strcmp(p->p_title, proc_name))
76 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
77 int debug, int argc, char **argv)
79 unsigned int proc, nargc, i, proc_i, proc_X = 0;
81 struct privsep_proc *p;
85 /* Prepare the new process argv. */
86 nargv = calloc(argc + 9, sizeof(char *));
88 fatal("%s: calloc", __func__);
90 /* Copy call argument first. */
92 nargv[nargc++] = argv[0];
94 /* Set process name argument and save the position. */
95 nargv[nargc++] = "-T";
99 /* Set user and chroot */
100 if (ps->ps_pw != NULL) {
101 nargv[nargc++] = "-U";
102 nargv[nargc++] = ps->ps_pw->pw_name;
104 nargv[nargc++] = "-X";
109 /* Point process instance arg to stack and copy the original args. */
110 nargv[nargc++] = "-I";
111 nargv[nargc++] = num;
112 for (i = 1; i < (unsigned int) argc; i++)
113 nargv[nargc++] = argv[i];
117 for (proc = 0; proc < nproc; proc++) {
120 /* Update args with process title and chroot. */
121 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
123 nargv[proc_X] = p->p_chroot;
125 /* Fire children processes. */
126 for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
127 /* Update the process instance number. */
128 snprintf(num, sizeof(num), "%u", i);
130 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0];
131 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0] = -1;
135 fatal("%s: fork", __func__);
138 /* First create a new session */
142 /* Prepare parent socket. */
143 if (fd != PROC_PARENT_SOCK_FILENO) {
144 if (dup2(fd, PROC_PARENT_SOCK_FILENO)
147 } else if (fcntl(fd, F_SETFD, 0) == -1)
150 /* Daemons detach from terminal. */
152 open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
153 (void)dup2(fd, STDIN_FILENO);
154 (void)dup2(fd, STDOUT_FILENO);
155 (void)dup2(fd, STDERR_FILENO);
160 /* obnoxious casts */
161 execvp(argv[0], (char *const *)nargv);
162 fatal("%s: execvp", __func__);
165 /* Close child end. */
175 proc_connect(struct privsep *ps)
178 unsigned int src, dst, inst;
180 /* Don't distribute any sockets if we are not really going to run. */
184 for (dst = 0; dst < PROC_MAX; dst++) {
185 /* We don't communicate with ourselves. */
186 if (dst == PROC_PARENT)
189 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
190 iev = &ps->ps_ievs[dst][inst];
191 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
192 event_set(&iev->ev, iev->ibuf.fd, iev->events,
193 iev->handler, iev->data);
194 event_add(&iev->ev, NULL);
198 /* Distribute the socketpair()s for everyone. */
199 for (src = 0; src < PROC_MAX; src++)
200 for (dst = src; dst < PROC_MAX; dst++) {
201 /* Parent already distributed its fds. */
202 if (src == PROC_PARENT || dst == PROC_PARENT)
205 proc_open(ps, src, dst);
210 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
211 int debug, int argc, char **argv, enum privsep_procid proc_id)
213 struct privsep_proc *p = NULL;
214 struct privsep_pipes *pa, *pb;
219 /* Don't initiate anything if we are not really going to run. */
223 if (proc_id == PROC_PARENT) {
224 privsep_process = PROC_PARENT;
225 proc_setup(ps, procs, nproc);
228 * Create the children sockets so we can use them
229 * to distribute the rest of the socketpair()s using
230 * proc_connect() later.
232 for (dst = 0; dst < PROC_MAX; dst++) {
233 /* Don't create socket for ourselves. */
234 if (dst == PROC_PARENT)
237 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
238 pa = &ps->ps_pipes[PROC_PARENT][0];
239 pb = &ps->ps_pipes[dst][proc];
240 if (socketpair(AF_UNIX,
242 PF_UNSPEC, fds) == -1)
243 fatal("%s: socketpair", __func__);
245 mark_nonblock(fds[0]);
246 mark_nonblock(fds[1]);
247 if (fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
248 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1)
249 fatal("%s: fcntl F_SETFD", __func__);
251 pa->pp_pipes[dst][proc] = fds[0];
252 pb->pp_pipes[PROC_PARENT][0] = fds[1];
257 proc_exec(ps, procs, nproc, debug, argc, argv);
261 /* Initialize a child */
262 for (proc = 0; proc < nproc; proc++) {
263 if (procs[proc].p_id != proc_id)
268 if (p == NULL || p->p_init == NULL)
269 fatalx("%s: process %d missing process initialization",
274 fatalx("failed to initiate child process");
278 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
281 struct privsep_pipes *pp = ps->ps_pp;
284 if (ps->ps_ievs[dst] == NULL) {
286 log_debug("%s: %s src %d %d to dst %d %d not connected",
287 __func__, ps->ps_title[privsep_process],
288 privsep_process, ps->ps_instance + 1,
295 if (pp->pp_pipes[dst][n] != -1) {
296 log_warnx("%s: duplicated descriptor", __func__);
300 pp->pp_pipes[dst][n] = fd;
302 iev = &ps->ps_ievs[dst][n];
303 imsg_init(&iev->ibuf, fd);
304 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
305 event_add(&iev->ev, NULL);
309 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
311 unsigned int i, j, src, dst, id;
312 struct privsep_pipes *pp;
314 /* Initialize parent title, ps_instances and procs. */
315 ps->ps_title[PROC_PARENT] = "parent";
317 for (src = 0; src < PROC_MAX; src++)
318 /* Default to 1 process instance */
319 if (ps->ps_instances[src] < 1)
320 ps->ps_instances[src] = 1;
322 for (src = 0; src < nproc; src++) {
323 procs[src].p_ps = ps;
324 if (procs[src].p_cb == NULL)
325 procs[src].p_cb = proc_dispatch_null;
327 id = procs[src].p_id;
328 ps->ps_title[id] = procs[src].p_title;
329 if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
330 sizeof(struct imsgev))) == NULL)
331 fatal("%s: calloc", __func__);
333 /* With this set up, we are ready to call imsg_init(). */
334 for (i = 0; i < ps->ps_instances[id]; i++) {
335 ps->ps_ievs[id][i].handler = proc_dispatch;
336 ps->ps_ievs[id][i].events = EV_READ;
337 ps->ps_ievs[id][i].proc = &procs[src];
338 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
343 * Allocate pipes for all process instances (incl. parent)
345 * - ps->ps_pipes: N:M mapping
346 * N source processes connected to M destination processes:
347 * [src][instances][dst][instances], for example
348 * [PROC_RELAY][3][PROC_CA][3]
350 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
351 * Each process instance has a destination array of socketpair fds:
352 * [dst][instances], for example
355 for (src = 0; src < PROC_MAX; src++) {
356 /* Allocate destination array for each process */
357 if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
358 sizeof(struct privsep_pipes))) == NULL)
359 fatal("%s: calloc", __func__);
361 for (i = 0; i < ps->ps_instances[src]; i++) {
362 pp = &ps->ps_pipes[src][i];
364 for (dst = 0; dst < PROC_MAX; dst++) {
365 /* Allocate maximum fd integers */
366 if ((pp->pp_pipes[dst] =
367 calloc(ps->ps_instances[dst],
368 sizeof(int))) == NULL)
369 fatal("%s: calloc", __func__);
371 /* Mark fd as unused */
372 for (j = 0; j < ps->ps_instances[dst]; j++)
373 pp->pp_pipes[dst][j] = -1;
378 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
382 proc_kill(struct privsep *ps)
388 if (privsep_process != PROC_PARENT)
394 pid = waitpid(WAIT_ANY, &status, 0);
398 if (WIFSIGNALED(status)) {
399 len = asprintf(&cause, "terminated; signal %d",
401 } else if (WIFEXITED(status)) {
402 if (WEXITSTATUS(status) != 0)
403 len = asprintf(&cause, "exited abnormally");
410 /* child exited OK, don't print a warning message */
411 } else if (len != -1) {
412 log_warnx("lost child: pid %u %s", pid, cause);
415 log_warnx("lost child: pid %u", pid);
416 } while (pid != -1 || errno == EINTR);
420 proc_open(struct privsep *ps, int src, int dst)
422 struct privsep_pipes *pa, *pb;
423 struct privsep_fd pf;
427 /* Exchange pipes between process. */
428 for (i = 0; i < ps->ps_instances[src]; i++) {
429 for (j = 0; j < ps->ps_instances[dst]; j++) {
430 /* Don't create sockets for ourself. */
431 if (src == dst && i == j)
434 /* Servers don't talk to each other. */
435 if (src == PROC_SERVER && dst == PROC_SERVER)
438 pa = &ps->ps_pipes[src][i];
439 pb = &ps->ps_pipes[dst][j];
440 if (socketpair(AF_UNIX,
442 PF_UNSPEC, fds) == -1)
443 fatal("%s: socketpair", __func__);
445 mark_nonblock(fds[0]);
446 mark_nonblock(fds[1]);
447 if (fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
448 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1)
449 fatal("%s: fcntl F_SETFD", __func__);
451 pa->pp_pipes[dst][j] = fds[0];
452 pb->pp_pipes[src][i] = fds[1];
456 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
457 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
458 fatal("%s: proc_compose_imsg", __func__);
462 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
463 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
464 fatal("%s: proc_compose_imsg", __func__);
467 * We have to flush to send the descriptors and close
468 * them to avoid the fd ramp on startup.
470 if (proc_flush_imsg(ps, src, i) == -1 ||
471 proc_flush_imsg(ps, dst, j) == -1)
472 fatal("%s: imsg_flush", __func__);
478 proc_close(struct privsep *ps)
481 struct privsep_pipes *pp;
488 for (dst = 0; dst < PROC_MAX; dst++) {
489 if (ps->ps_ievs[dst] == NULL)
492 for (n = 0; n < ps->ps_instances[dst]; n++) {
493 if (pp->pp_pipes[dst][n] == -1)
496 /* Cancel the fd, close and invalidate the fd */
497 event_del(&(ps->ps_ievs[dst][n].ev));
498 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
499 close(pp->pp_pipes[dst][n]);
500 pp->pp_pipes[dst][n] = -1;
502 free(ps->ps_ievs[dst]);
507 proc_shutdown(struct privsep_proc *p)
509 struct privsep *ps = p->p_ps;
511 if (p->p_shutdown != NULL)
516 log_info("%s exiting, pid %d", p->p_title, getpid());
522 proc_sig_handler(int sig, short event, void *arg)
524 struct privsep_proc *p = arg;
536 fatalx("%s: unexpected signal", __func__);
542 proc_run(struct privsep *ps, struct privsep_proc *p,
543 struct privsep_proc *procs, unsigned int nproc,
544 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
549 log_procinit(p->p_title);
550 setproctitle("%s", p->p_title);
552 privsep_process = p->p_id;
554 if (ps->ps_pw == NULL)
557 /* Set the process group of the current process */
560 /* Use non-standard user */
566 /* Change root directory */
567 if (p->p_chroot != NULL)
572 if (chroot(root) == -1)
573 fatal("%s: chroot", __func__);
574 if (chdir("/") == -1)
575 fatal("%s: chdir(\"/\")", __func__);
577 if (setgroups(1, &pw->pw_gid) ||
578 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
579 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
580 fatal("%s: cannot drop privileges", __func__);
585 signal(SIGPIPE, SIG_IGN);
587 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
588 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
589 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
590 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
592 signal_add(&ps->ps_evsigint, NULL);
593 signal_add(&ps->ps_evsigterm, NULL);
594 signal_add(&ps->ps_evsigchld, NULL);
595 signal_add(&ps->ps_evsighup, NULL);
597 proc_setup(ps, procs, nproc);
598 proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0);
600 log_debug("%s: %s %d/%d, pid %d", __func__, p->p_title,
601 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
612 proc_dispatch(int fd, short event, void *arg)
614 struct imsgev *iev = arg;
615 struct privsep_proc *p = iev->proc;
616 struct privsep *ps = p->p_ps;
617 struct imsgbuf *ibuf;
621 struct privsep_fd pf;
623 title = ps->ps_title[privsep_process];
626 if (event & EV_READ) {
627 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
628 fatal("%s: imsg_read", __func__);
630 /* this pipe is dead, so remove the event handler */
632 event_loopexit(NULL);
637 if (event & EV_WRITE) {
638 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
639 fatal("%s: msgbuf_write", __func__);
641 /* this pipe is dead, so remove the event handler */
643 event_loopexit(NULL);
649 if ((n = imsg_get(ibuf, &imsg)) == -1)
650 fatal("%s: imsg_get", __func__);
655 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
656 __func__, title, ps->ps_instance + 1,
657 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
661 * Check the message with the program callback
663 if ((p->p_cb)(fd, p, &imsg) == 0) {
664 /* Message was handled by the callback, continue */
670 * Generic message handling
672 switch (imsg.hdr.type) {
673 case IMSG_CTL_PROCFD:
674 if (imsg_get_data(&imsg, &pf, sizeof(pf)))
675 fatalx("bad length imsg CTL_PROCFD");
676 proc_accept(ps, imsg_get_fd(&imsg), pf.pf_procid,
680 fatalx("%s: %s %d got invalid imsg %d peerid %d "
682 __func__, title, ps->ps_instance + 1,
683 imsg.hdr.type, imsg.hdr.peerid,
684 p->p_title, imsg.hdr.pid);
692 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
698 * imsg helper functions
702 imsg_event_add(struct imsgev *iev)
704 if (iev->handler == NULL) {
705 imsg_flush(&iev->ibuf);
709 iev->events = EV_READ;
710 if (iev->ibuf.w.queued)
711 iev->events |= EV_WRITE;
714 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
715 event_add(&iev->ev, NULL);
719 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
720 pid_t pid, int fd, void *data, uint16_t datalen)
724 if ((ret = imsg_compose(&iev->ibuf, type, peerid,
725 pid, fd, data, datalen)) == -1)
732 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
733 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
737 if ((ret = imsg_composev(&iev->ibuf, type, peerid,
738 pid, fd, iov, iovcnt)) == -1)
745 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
748 /* Use a range of all target instances */
750 *m = ps->ps_instances[id];
752 /* Use only a single slot of the specified peer process */
758 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
759 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
763 proc_range(ps, id, &n, &m);
765 if (imsg_compose_event(&ps->ps_ievs[id][n],
766 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
774 proc_compose(struct privsep *ps, enum privsep_procid id,
775 uint16_t type, void *data, uint16_t datalen)
777 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
781 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
782 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
786 proc_range(ps, id, &n, &m);
788 if (imsg_composev_event(&ps->ps_ievs[id][n],
789 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
796 proc_composev(struct privsep *ps, enum privsep_procid id,
797 uint16_t type, const struct iovec *iov, int iovcnt)
799 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
803 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
807 proc_range(ps, id, &n, &m);
808 return (&ps->ps_ievs[id][n].ibuf);
812 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
816 proc_range(ps, id, &n, &m);
817 return (&ps->ps_ievs[id][n]);
820 /* This function should only be called with care as it breaks async I/O */
822 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
824 struct imsgbuf *ibuf;
827 proc_range(ps, id, &n, &m);
829 if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
833 ret = imsg_flush(ibuf);
834 } while (ret == -1 && errno == EAGAIN);
837 imsg_event_add(&ps->ps_ievs[id][n]);