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 int proc_ispeer(struct privsep_proc *, unsigned int, enum privsep_procid);
53 void proc_shutdown(struct privsep_proc *);
54 void proc_sig_handler(int, short, void *);
55 void proc_range(struct privsep *, enum privsep_procid, int *, int *);
56 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
59 proc_ispeer(struct privsep_proc *procs, unsigned int nproc,
60 enum privsep_procid type)
64 for (i = 0; i < nproc; i++)
65 if (procs[i].p_id == type)
71 proc_getid(struct privsep_proc *procs, unsigned int nproc,
72 const char *proc_name)
74 struct privsep_proc *p;
77 for (proc = 0; proc < nproc; proc++) {
79 if (strcmp(p->p_title, proc_name))
89 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
90 int debug, int argc, char **argv)
92 unsigned int proc, nargc, i, proc_i;
94 struct privsep_proc *p;
98 /* Prepare the new process argv. */
99 nargv = calloc(argc + 5, sizeof(char *));
101 fatal("%s: calloc", __func__);
103 /* Copy call argument first. */
105 nargv[nargc++] = argv[0];
107 /* Set process name argument and save the position. */
108 nargv[nargc++] = "-T";
112 /* Point process instance arg to stack and copy the original args. */
113 nargv[nargc++] = "-I";
114 nargv[nargc++] = num;
115 for (i = 1; i < (unsigned int) argc; i++)
116 nargv[nargc++] = argv[i];
120 for (proc = 0; proc < nproc; proc++) {
123 /* Update args with process title. */
124 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
126 /* Fire children processes. */
127 for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
128 /* Update the process instance number. */
129 snprintf(num, sizeof(num), "%u", i);
131 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0];
132 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0] = -1;
136 fatal("%s: fork", __func__);
139 /* First create a new session */
143 /* Prepare parent socket. */
144 if (fd != PROC_PARENT_SOCK_FILENO) {
145 if (dup2(fd, PROC_PARENT_SOCK_FILENO)
148 } else if (fcntl(fd, F_SETFD, 0) == -1)
151 /* Daemons detach from terminal. */
153 open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
154 (void)dup2(fd, STDIN_FILENO);
155 (void)dup2(fd, STDOUT_FILENO);
156 (void)dup2(fd, STDERR_FILENO);
161 /* obnoxious casts */
162 execvp(argv[0], (char *const *)nargv);
163 fatal("%s: execvp", __func__);
166 /* Close child end. */
176 proc_connect(struct privsep *ps)
179 unsigned int src, dst, inst;
181 /* Don't distribute any sockets if we are not really going to run. */
185 for (dst = 0; dst < PROC_MAX; dst++) {
186 /* We don't communicate with ourselves. */
187 if (dst == PROC_PARENT)
190 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
191 iev = &ps->ps_ievs[dst][inst];
192 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
193 event_set(&iev->ev, iev->ibuf.fd, iev->events,
194 iev->handler, iev->data);
195 event_add(&iev->ev, NULL);
199 /* Distribute the socketpair()s for everyone. */
200 for (src = 0; src < PROC_MAX; src++)
201 for (dst = src; dst < PROC_MAX; dst++) {
202 /* Parent already distributed its fds. */
203 if (src == PROC_PARENT || dst == PROC_PARENT)
206 proc_open(ps, src, dst);
211 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
212 int debug, int argc, char **argv, enum privsep_procid proc_id)
214 struct privsep_proc *p = NULL;
215 struct privsep_pipes *pa, *pb;
220 /* Don't initiate anything if we are not really going to run. */
224 if (proc_id == PROC_PARENT) {
225 privsep_process = PROC_PARENT;
226 proc_setup(ps, procs, nproc);
229 * Create the children sockets so we can use them
230 * to distribute the rest of the socketpair()s using
231 * proc_connect() later.
233 for (dst = 0; dst < PROC_MAX; dst++) {
234 /* Don't create socket for ourselves. */
235 if (dst == PROC_PARENT)
238 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
239 pa = &ps->ps_pipes[PROC_PARENT][0];
240 pb = &ps->ps_pipes[dst][proc];
241 if (socketpair(AF_UNIX,
242 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
243 PF_UNSPEC, fds) == -1)
244 fatal("%s: socketpair", __func__);
246 pa->pp_pipes[dst][proc] = fds[0];
247 pb->pp_pipes[PROC_PARENT][0] = fds[1];
252 proc_exec(ps, procs, nproc, debug, argc, argv);
256 /* Initialize a child */
257 for (proc = 0; proc < nproc; proc++) {
258 if (procs[proc].p_id != proc_id)
263 if (p == NULL || p->p_init == NULL)
264 fatalx("%s: process %d missing process initialization",
269 fatalx("failed to initiate child process");
273 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
276 struct privsep_pipes *pp = ps->ps_pp;
279 if (ps->ps_ievs[dst] == NULL) {
281 log_debug("%s: %s src %d %d to dst %d %d not connected",
282 __func__, ps->ps_title[privsep_process],
283 privsep_process, ps->ps_instance + 1,
290 if (pp->pp_pipes[dst][n] != -1) {
291 log_warnx("%s: duplicated descriptor", __func__);
295 pp->pp_pipes[dst][n] = fd;
297 iev = &ps->ps_ievs[dst][n];
298 imsg_init(&iev->ibuf, fd);
299 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
300 event_add(&iev->ev, NULL);
304 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
306 unsigned int i, j, src, dst, id;
307 struct privsep_pipes *pp;
309 /* Initialize parent title, ps_instances and procs. */
310 ps->ps_title[PROC_PARENT] = "parent";
312 for (src = 0; src < PROC_MAX; src++)
313 /* Default to 1 process instance */
314 if (ps->ps_instances[src] < 1)
315 ps->ps_instances[src] = 1;
317 for (src = 0; src < nproc; src++) {
318 procs[src].p_ps = ps;
319 if (procs[src].p_cb == NULL)
320 procs[src].p_cb = proc_dispatch_null;
322 id = procs[src].p_id;
323 ps->ps_title[id] = procs[src].p_title;
324 if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
325 sizeof(struct imsgev))) == NULL)
326 fatal("%s: calloc", __func__);
328 /* With this set up, we are ready to call imsg_init(). */
329 for (i = 0; i < ps->ps_instances[id]; i++) {
330 ps->ps_ievs[id][i].handler = proc_dispatch;
331 ps->ps_ievs[id][i].events = EV_READ;
332 ps->ps_ievs[id][i].proc = &procs[src];
333 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
338 * Allocate pipes for all process instances (incl. parent)
340 * - ps->ps_pipes: N:M mapping
341 * N source processes connected to M destination processes:
342 * [src][instances][dst][instances], for example
343 * [PROC_RELAY][3][PROC_CA][3]
345 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
346 * Each process instance has a destination array of socketpair fds:
347 * [dst][instances], for example
350 for (src = 0; src < PROC_MAX; src++) {
351 /* Allocate destination array for each process */
352 if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
353 sizeof(struct privsep_pipes))) == NULL)
354 fatal("%s: calloc", __func__);
356 for (i = 0; i < ps->ps_instances[src]; i++) {
357 pp = &ps->ps_pipes[src][i];
359 for (dst = 0; dst < PROC_MAX; dst++) {
360 /* Allocate maximum fd integers */
361 if ((pp->pp_pipes[dst] =
362 calloc(ps->ps_instances[dst],
363 sizeof(int))) == NULL)
364 fatal("%s: calloc", __func__);
366 /* Mark fd as unused */
367 for (j = 0; j < ps->ps_instances[dst]; j++)
368 pp->pp_pipes[dst][j] = -1;
373 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
377 proc_kill(struct privsep *ps)
383 if (privsep_process != PROC_PARENT)
389 pid = waitpid(WAIT_ANY, &status, 0);
393 if (WIFSIGNALED(status)) {
394 len = asprintf(&cause, "terminated; signal %d",
396 } else if (WIFEXITED(status)) {
397 if (WEXITSTATUS(status) != 0)
398 len = asprintf(&cause, "exited abnormally");
405 /* child exited OK, don't print a warning message */
406 } else if (len != -1) {
407 log_warnx("lost child: pid %u %s", pid, cause);
410 log_warnx("lost child: pid %u", pid);
411 } while (pid != -1 || errno == EINTR);
415 proc_open(struct privsep *ps, int src, int dst)
417 struct privsep_pipes *pa, *pb;
418 struct privsep_fd pf;
422 /* Exchange pipes between process. */
423 for (i = 0; i < ps->ps_instances[src]; i++) {
424 for (j = 0; j < ps->ps_instances[dst]; j++) {
425 /* Don't create sockets for ourself. */
426 if (src == dst && i == j)
429 /* Servers don't talk to each other. */
430 if (src == PROC_SERVER && dst == PROC_SERVER)
433 pa = &ps->ps_pipes[src][i];
434 pb = &ps->ps_pipes[dst][j];
435 if (socketpair(AF_UNIX,
436 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
437 PF_UNSPEC, fds) == -1)
438 fatal("%s: socketpair", __func__);
440 pa->pp_pipes[dst][j] = fds[0];
441 pb->pp_pipes[src][i] = fds[1];
445 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
446 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
447 fatal("%s: proc_compose_imsg", __func__);
451 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
452 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
453 fatal("%s: proc_compose_imsg", __func__);
456 * We have to flush to send the descriptors and close
457 * them to avoid the fd ramp on startup.
459 if (proc_flush_imsg(ps, src, i) == -1 ||
460 proc_flush_imsg(ps, dst, j) == -1)
461 fatal("%s: imsg_flush", __func__);
467 proc_close(struct privsep *ps)
470 struct privsep_pipes *pp;
477 for (dst = 0; dst < PROC_MAX; dst++) {
478 if (ps->ps_ievs[dst] == NULL)
481 for (n = 0; n < ps->ps_instances[dst]; n++) {
482 if (pp->pp_pipes[dst][n] == -1)
485 /* Cancel the fd, close and invalidate the fd */
486 event_del(&(ps->ps_ievs[dst][n].ev));
487 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
488 close(pp->pp_pipes[dst][n]);
489 pp->pp_pipes[dst][n] = -1;
491 free(ps->ps_ievs[dst]);
496 proc_shutdown(struct privsep_proc *p)
498 struct privsep *ps = p->p_ps;
500 if (p->p_shutdown != NULL)
505 log_info("%s exiting, pid %d", p->p_title, getpid());
511 proc_sig_handler(int sig, short event, void *arg)
513 struct privsep_proc *p = arg;
525 fatalx("%s: unexpected signal", __func__);
531 proc_run(struct privsep *ps, struct privsep_proc *p,
532 struct privsep_proc *procs, unsigned int nproc,
533 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
538 log_procinit(p->p_title);
540 if (ps->ps_pw == NULL)
543 /* Set the process group of the current process */
546 /* Use non-standard user */
552 /* Change root directory */
553 if (p->p_chroot != NULL)
558 if (chroot(root) == -1)
559 fatal("%s: chroot", __func__);
560 if (chdir("/") == -1)
561 fatal("%s: chdir(\"/\")", __func__);
563 privsep_process = p->p_id;
565 setproctitle("%s", p->p_title);
567 if (setgroups(1, &pw->pw_gid) ||
568 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
569 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
570 fatal("%s: cannot drop privileges", __func__);
575 signal(SIGPIPE, SIG_IGN);
577 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
578 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
579 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
580 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
582 signal_add(&ps->ps_evsigint, NULL);
583 signal_add(&ps->ps_evsigterm, NULL);
584 signal_add(&ps->ps_evsigchld, NULL);
585 signal_add(&ps->ps_evsighup, NULL);
587 proc_setup(ps, procs, nproc);
588 proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0);
590 log_debug("%s: %s %d/%d, pid %d", __func__, p->p_title,
591 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
602 proc_dispatch(int fd, short event, void *arg)
604 struct imsgev *iev = arg;
605 struct privsep_proc *p = iev->proc;
606 struct privsep *ps = p->p_ps;
607 struct imsgbuf *ibuf;
611 struct privsep_fd pf;
613 title = ps->ps_title[privsep_process];
616 if (event & EV_READ) {
617 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
618 fatal("%s: imsg_read", __func__);
620 /* this pipe is dead, so remove the event handler */
622 event_loopexit(NULL);
627 if (event & EV_WRITE) {
628 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
629 fatal("%s: msgbuf_write", __func__);
631 /* this pipe is dead, so remove the event handler */
633 event_loopexit(NULL);
639 if ((n = imsg_get(ibuf, &imsg)) == -1)
640 fatal("%s: imsg_get", __func__);
645 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
646 __func__, title, ps->ps_instance + 1,
647 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
651 * Check the message with the program callback
653 if ((p->p_cb)(fd, p, &imsg) == 0) {
654 /* Message was handled by the callback, continue */
660 * Generic message handling
662 switch (imsg.hdr.type) {
663 case IMSG_CTL_PROCFD:
664 IMSG_SIZE_CHECK(&imsg, &pf);
665 memcpy(&pf, imsg.data, sizeof(pf));
666 proc_accept(ps, imsg.fd, pf.pf_procid,
670 fatalx("%s: %s %d got invalid imsg %d peerid %d "
672 __func__, title, ps->ps_instance + 1,
673 imsg.hdr.type, imsg.hdr.peerid,
674 p->p_title, imsg.hdr.pid);
682 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
688 * imsg helper functions
692 imsg_event_add(struct imsgev *iev)
694 if (iev->handler == NULL) {
695 imsg_flush(&iev->ibuf);
699 iev->events = EV_READ;
700 if (iev->ibuf.w.queued)
701 iev->events |= EV_WRITE;
704 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
705 event_add(&iev->ev, NULL);
709 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
710 pid_t pid, int fd, void *data, uint16_t datalen)
714 if ((ret = imsg_compose(&iev->ibuf, type, peerid,
715 pid, fd, data, datalen)) == -1)
722 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
723 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
727 if ((ret = imsg_composev(&iev->ibuf, type, peerid,
728 pid, fd, iov, iovcnt)) == -1)
735 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
738 /* Use a range of all target instances */
740 *m = ps->ps_instances[id];
742 /* Use only a single slot of the specified peer process */
748 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
749 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
753 proc_range(ps, id, &n, &m);
755 if (imsg_compose_event(&ps->ps_ievs[id][n],
756 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
764 proc_compose(struct privsep *ps, enum privsep_procid id,
765 uint16_t type, void *data, uint16_t datalen)
767 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
771 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
772 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
776 proc_range(ps, id, &n, &m);
778 if (imsg_composev_event(&ps->ps_ievs[id][n],
779 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
786 proc_composev(struct privsep *ps, enum privsep_procid id,
787 uint16_t type, const struct iovec *iov, int iovcnt)
789 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
793 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
794 enum privsep_procid id, int n)
796 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
797 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
801 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
805 proc_range(ps, id, &n, &m);
806 return (&ps->ps_ievs[id][n].ibuf);
810 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
814 proc_range(ps, id, &n, &m);
815 return (&ps->ps_ievs[id][n]);
818 /* This function should only be called with care as it breaks async I/O */
820 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
822 struct imsgbuf *ibuf;
825 proc_range(ps, id, &n, &m);
827 if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
831 ret = imsg_flush(ibuf);
832 } while (ret == -1 && errno == EAGAIN);
835 imsg_event_add(&ps->ps_ievs[id][n]);