1 /* $OpenBSD: proc.c,v 1.53 2026/03/02 19:28:01 rsadowski 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.
20 #include <sys/types.h>
21 #include <sys/queue.h>
22 #include <sys/socket.h>
40 void proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int,
42 void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
43 void proc_open(struct privsep *, int, int);
44 void proc_accept(struct privsep *, int, enum privsep_procid,
46 void proc_close(struct privsep *);
47 void proc_shutdown(struct privsep_proc *);
48 void proc_sig_handler(int, short, void *);
49 void proc_range(struct privsep *, enum privsep_procid, int *, int *);
50 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
53 proc_getid(struct privsep_proc *procs, unsigned int nproc,
54 const char *proc_name)
56 struct privsep_proc *p;
59 for (proc = 0; proc < nproc; proc++) {
61 if (strcmp(p->p_title, proc_name))
71 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
72 int argc, char **argv)
74 unsigned int proc, nargc, i, proc_i;
76 struct privsep_proc *p;
80 /* Prepare the new process argv. */
81 nargv = calloc(argc + 5, sizeof(char *));
83 fatal("%s: calloc", __func__);
85 /* Copy call argument first. */
87 nargv[nargc++] = argv[0];
89 /* Set process name argument and save the position. */
90 nargv[nargc++] = "-P";
94 /* Point process instance arg to stack and copy the original args. */
95 nargv[nargc++] = "-I";
97 for (i = 1; i < (unsigned int) argc; i++)
98 nargv[nargc++] = argv[i];
102 for (proc = 0; proc < nproc; proc++) {
105 /* Update args with process title. */
106 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
108 /* Fire children processes. */
109 for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
110 /* Update the process instance number. */
111 snprintf(num, sizeof(num), "%u", i);
113 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0];
114 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0] = -1;
118 fatal("%s: fork", __func__);
121 /* Prepare parent socket. */
122 if (fd != PROC_PARENT_SOCK_FILENO) {
123 if (dup2(fd, PROC_PARENT_SOCK_FILENO)
126 } else if (fcntl(fd, F_SETFD, 0) == -1)
129 execvp(argv[0], nargv);
130 fatal("%s: execvp", __func__);
133 /* Close child end. */
143 proc_connect(struct privsep *ps)
146 unsigned int src, dst, inst;
148 /* Don't distribute any sockets if we are not really going to run. */
152 for (dst = 0; dst < PROC_MAX; dst++) {
153 /* We don't communicate with ourselves. */
154 if (dst == PROC_PARENT)
157 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
158 iev = &ps->ps_ievs[dst][inst];
159 if (imsgbuf_init(&iev->ibuf,
160 ps->ps_pp->pp_pipes[dst][inst]) == -1)
161 fatal("imsgbuf_init");
162 imsgbuf_allow_fdpass(&iev->ibuf);
163 event_set(&iev->ev, iev->ibuf.fd, iev->events,
164 iev->handler, iev->data);
165 event_add(&iev->ev, NULL);
169 /* Distribute the socketpair()s for everyone. */
170 for (src = 0; src < PROC_MAX; src++)
171 for (dst = src; dst < PROC_MAX; dst++) {
172 /* Parent already distributed its fds. */
173 if (src == PROC_PARENT || dst == PROC_PARENT)
176 proc_open(ps, src, dst);
181 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
182 int debug, int argc, char **argv, enum privsep_procid proc_id)
184 struct privsep_proc *p = NULL;
185 struct privsep_pipes *pa, *pb;
190 /* Don't initiate anything if we are not really going to run. */
194 if (proc_id == PROC_PARENT) {
195 privsep_process = PROC_PARENT;
196 proc_setup(ps, procs, nproc);
198 if (!debug && daemon(1, 0) == -1)
199 fatal("failed to daemonize");
202 * Create the children sockets so we can use them
203 * to distribute the rest of the socketpair()s using
204 * proc_connect() later.
206 for (dst = 0; dst < PROC_MAX; dst++) {
207 /* Don't create socket for ourselves. */
208 if (dst == PROC_PARENT)
211 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
212 pa = &ps->ps_pipes[PROC_PARENT][0];
213 pb = &ps->ps_pipes[dst][proc];
214 if (socketpair(AF_UNIX,
215 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
216 PF_UNSPEC, fds) == -1)
217 fatal("%s: socketpair", __func__);
219 pa->pp_pipes[dst][proc] = fds[0];
220 pb->pp_pipes[PROC_PARENT][0] = fds[1];
225 proc_exec(ps, procs, nproc, argc, argv);
229 /* Initialize a child */
230 for (proc = 0; proc < nproc; proc++) {
231 if (procs[proc].p_id != proc_id)
236 if (p == NULL || p->p_init == NULL)
237 fatalx("%s: process %d missing process initialization",
242 fatalx("failed to initiate child process");
246 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
249 struct privsep_pipes *pp = ps->ps_pp;
252 if (ps->ps_ievs[dst] == NULL) {
254 log_debug("%s: %s src %d %d to dst %d %d not connected",
255 __func__, ps->ps_title[privsep_process],
256 privsep_process, ps->ps_instance + 1,
263 if (pp->pp_pipes[dst][n] != -1) {
264 log_warnx("%s: duplicated descriptor", __func__);
268 pp->pp_pipes[dst][n] = fd;
270 iev = &ps->ps_ievs[dst][n];
271 if (imsgbuf_init(&iev->ibuf, fd) == -1)
272 fatal("imsgbuf_init");
273 imsgbuf_allow_fdpass(&iev->ibuf);
274 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
275 event_add(&iev->ev, NULL);
279 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
281 unsigned int i, j, src, dst, id;
282 struct privsep_pipes *pp;
284 /* Initialize parent title, ps_instances and procs. */
285 ps->ps_title[PROC_PARENT] = "parent";
287 for (src = 0; src < PROC_MAX; src++)
288 /* Default to 1 process instance */
289 if (ps->ps_instances[src] < 1)
290 ps->ps_instances[src] = 1;
292 for (src = 0; src < nproc; src++) {
293 procs[src].p_ps = ps;
294 if (procs[src].p_cb == NULL)
295 procs[src].p_cb = proc_dispatch_null;
297 id = procs[src].p_id;
298 ps->ps_title[id] = procs[src].p_title;
299 if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
300 sizeof(struct imsgev))) == NULL)
301 fatal("%s: calloc", __func__);
303 /* With this set up, we are ready to call imsgbuf_init(). */
304 for (i = 0; i < ps->ps_instances[id]; i++) {
305 ps->ps_ievs[id][i].handler = proc_dispatch;
306 ps->ps_ievs[id][i].events = EV_READ;
307 ps->ps_ievs[id][i].proc = &procs[src];
308 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
313 * Allocate pipes for all process instances (incl. parent)
315 * - ps->ps_pipes: N:M mapping
316 * N source processes connected to M destination processes:
317 * [src][instances][dst][instances], for example
318 * [PROC_RELAY][3][PROC_CA][3]
320 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
321 * Each process instance has a destination array of socketpair fds:
322 * [dst][instances], for example
325 for (src = 0; src < PROC_MAX; src++) {
326 /* Allocate destination array for each process */
327 if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
328 sizeof(struct privsep_pipes))) == NULL)
329 fatal("%s: calloc", __func__);
331 for (i = 0; i < ps->ps_instances[src]; i++) {
332 pp = &ps->ps_pipes[src][i];
334 for (dst = 0; dst < PROC_MAX; dst++) {
335 /* Allocate maximum fd integers */
336 if ((pp->pp_pipes[dst] =
337 calloc(ps->ps_instances[dst],
338 sizeof(int))) == NULL)
339 fatal("%s: calloc", __func__);
341 /* Mark fd as unused */
342 for (j = 0; j < ps->ps_instances[dst]; j++)
343 pp->pp_pipes[dst][j] = -1;
348 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
352 proc_kill(struct privsep *ps)
358 if (privsep_process != PROC_PARENT)
364 pid = waitpid(WAIT_ANY, &status, 0);
368 if (WIFSIGNALED(status)) {
369 len = asprintf(&cause, "terminated; signal %d",
371 } else if (WIFEXITED(status)) {
372 if (WEXITSTATUS(status) != 0)
373 len = asprintf(&cause, "exited abnormally");
380 /* child exited OK, don't print a warning message */
381 } else if (len != -1) {
382 log_warnx("lost child: pid %u %s", pid, cause);
385 log_warnx("lost child: pid %u", pid);
386 } while (pid != -1 || (pid == -1 && errno == EINTR));
390 proc_open(struct privsep *ps, int src, int dst)
392 struct privsep_pipes *pa, *pb;
393 struct privsep_fd pf;
397 /* Exchange pipes between process. */
398 for (i = 0; i < ps->ps_instances[src]; i++) {
399 for (j = 0; j < ps->ps_instances[dst]; j++) {
400 /* Don't create sockets for ourself. */
401 if (src == dst && i == j)
404 /* No need for CA to CA or RELAY to RELAY sockets. */
405 if ((src == PROC_CA && dst == PROC_CA) ||
406 (src == PROC_RELAY && dst == PROC_RELAY))
409 pa = &ps->ps_pipes[src][i];
410 pb = &ps->ps_pipes[dst][j];
411 if (socketpair(AF_UNIX,
412 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
413 PF_UNSPEC, fds) == -1)
414 fatal("%s: socketpair", __func__);
416 pa->pp_pipes[dst][j] = fds[0];
417 pb->pp_pipes[src][i] = fds[1];
421 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
422 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
423 fatal("%s: proc_compose_imsg", __func__);
427 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
428 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
429 fatal("%s: proc_compose_imsg", __func__);
432 * We have to flush to send the descriptors and close
433 * them to avoid the fd ramp on startup.
435 if (proc_flush_imsg(ps, src, i) == -1 ||
436 proc_flush_imsg(ps, dst, j) == -1)
437 fatal("%s: proc_flush_imsg", __func__);
443 proc_close(struct privsep *ps)
446 struct privsep_pipes *pp;
453 for (dst = 0; dst < PROC_MAX; dst++) {
454 if (ps->ps_ievs[dst] == NULL)
457 for (n = 0; n < ps->ps_instances[dst]; n++) {
458 if (pp->pp_pipes[dst][n] == -1)
461 /* Cancel the fd, close and invalidate the fd */
462 event_del(&(ps->ps_ievs[dst][n].ev));
463 imsgbuf_clear(&(ps->ps_ievs[dst][n].ibuf));
464 close(pp->pp_pipes[dst][n]);
465 pp->pp_pipes[dst][n] = -1;
467 free(ps->ps_ievs[dst]);
472 proc_shutdown(struct privsep_proc *p)
474 struct privsep *ps = p->p_ps;
476 if (p->p_id == PROC_CONTROL && ps)
477 control_cleanup(&ps->ps_csock);
479 if (p->p_shutdown != NULL)
484 log_info("%s exiting, pid %d", p->p_title, getpid());
490 proc_sig_handler(int sig, short event, void *arg)
492 struct privsep_proc *p = arg;
506 fatalx("%s: unexpected signal", __func__);
512 proc_run(struct privsep *ps, struct privsep_proc *p,
513 struct privsep_proc *procs, unsigned int nproc,
514 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
518 struct control_sock *rcs;
520 log_procinit(p->p_title);
522 if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) {
523 if (control_init(ps, &ps->ps_csock) == -1)
524 fatalx("%s: control_init", __func__);
525 TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry)
526 if (control_init(ps, rcs) == -1)
527 fatalx("%s: control_init", __func__);
530 /* Use non-standard user */
536 /* Change root directory */
537 if (p->p_chroot != NULL)
542 if (chroot(root) == -1)
543 fatal("%s: chroot", __func__);
544 if (chdir("/") == -1)
545 fatal("%s: chdir(\"/\")", __func__);
547 privsep_process = p->p_id;
549 setproctitle("%s", p->p_title);
551 if (setgroups(1, &pw->pw_gid) ||
552 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
553 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
554 fatal("%s: cannot drop privileges", __func__);
558 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
559 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
560 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
561 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
562 signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
563 signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
565 signal_add(&ps->ps_evsigint, NULL);
566 signal_add(&ps->ps_evsigterm, NULL);
567 signal_add(&ps->ps_evsigchld, NULL);
568 signal_add(&ps->ps_evsighup, NULL);
569 signal_add(&ps->ps_evsigpipe, NULL);
570 signal_add(&ps->ps_evsigusr1, NULL);
572 proc_setup(ps, procs, nproc);
573 proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0);
574 if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) {
575 if (control_listen(&ps->ps_csock) == -1)
576 fatalx("%s: control_listen", __func__);
577 TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry)
578 if (control_listen(rcs) == -1)
579 fatalx("%s: control_listen", __func__);
582 DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
583 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
594 proc_dispatch(int fd, short event, void *arg)
596 struct imsgev *iev = arg;
597 struct privsep_proc *p = iev->proc;
598 struct privsep *ps = p->p_ps;
599 struct imsgbuf *ibuf;
604 struct privsep_fd pf;
606 title = ps->ps_title[privsep_process];
609 if (event & EV_READ) {
610 if ((n = imsgbuf_read(ibuf)) == -1)
611 fatal("%s: imsgbuf_read", __func__);
613 /* this pipe is dead, so remove the event handler */
615 event_loopexit(NULL);
620 if (event & EV_WRITE) {
621 if (imsgbuf_write(ibuf) == -1) {
622 if (errno == EPIPE) {
623 /* this pipe is dead, remove the handler */
625 event_loopexit(NULL);
628 fatal("%s: imsgbuf_write", __func__);
633 if ((n = imsg_get(ibuf, &imsg)) == -1)
634 fatal("%s: imsg_get", __func__);
639 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
640 __func__, title, ps->ps_instance + 1,
641 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
645 * Check the message with the program callback
647 if ((p->p_cb)(fd, p, &imsg) == 0) {
648 /* Message was handled by the callback, continue */
654 * Generic message handling
656 switch (imsg.hdr.type) {
657 case IMSG_CTL_VERBOSE:
658 IMSG_SIZE_CHECK(&imsg, &verbose);
659 memcpy(&verbose, imsg.data, sizeof(verbose));
660 log_setverbose(verbose);
662 case IMSG_CTL_PROCFD:
663 IMSG_SIZE_CHECK(&imsg, &pf);
664 memcpy(&pf, imsg.data, sizeof(pf));
665 proc_accept(ps, imsg_get_fd(&imsg), pf.pf_procid,
669 fatalx("%s: %s %d got invalid imsg %d peerid %d "
671 __func__, title, ps->ps_instance + 1,
672 imsg.hdr.type, imsg.hdr.peerid,
673 p->p_title, imsg.hdr.pid);
681 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
687 * imsg helper functions
691 imsg_event_add(struct imsgev *iev)
693 if (iev->handler == NULL) {
694 imsgbuf_flush(&iev->ibuf);
698 iev->events = EV_READ;
699 if (imsgbuf_queuelen(&iev->ibuf) > 0)
700 iev->events |= EV_WRITE;
703 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
704 event_add(&iev->ev, NULL);
708 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
709 pid_t pid, int fd, void *data, uint16_t datalen)
713 if ((ret = imsg_compose(&iev->ibuf, type, peerid,
714 pid, fd, data, datalen)) == -1)
721 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
722 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
726 if ((ret = imsg_composev(&iev->ibuf, type, peerid,
727 pid, fd, iov, iovcnt)) == -1)
734 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
737 /* Use a range of all target instances */
739 *m = ps->ps_instances[id];
741 /* Use only a single slot of the specified peer process */
747 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
748 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
752 proc_range(ps, id, &n, &m);
754 if (imsg_compose_event(&ps->ps_ievs[id][n],
755 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
763 proc_compose(struct privsep *ps, enum privsep_procid id,
764 uint16_t type, void *data, uint16_t datalen)
766 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
770 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
771 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
775 proc_range(ps, id, &n, &m);
777 if (imsg_composev_event(&ps->ps_ievs[id][n],
778 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
785 proc_composev(struct privsep *ps, enum privsep_procid id,
786 uint16_t type, const struct iovec *iov, int iovcnt)
788 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
792 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
793 enum privsep_procid id, int n)
795 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
796 imsg->hdr.peerid, -1, imsg->data, IMSG_DATA_SIZE(imsg)));
800 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
804 proc_range(ps, id, &n, &m);
805 return (&ps->ps_ievs[id][n].ibuf);
809 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
813 proc_range(ps, id, &n, &m);
814 return (&ps->ps_ievs[id][n]);
817 /* This function should only be called with care as it breaks async I/O */
819 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
821 struct imsgbuf *ibuf;
824 proc_range(ps, id, &n, &m);
826 if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
828 if ((ret = imsgbuf_flush(ibuf)) == -1)
830 imsg_event_add(&ps->ps_ievs[id][n]);