1 c26f2460 2023-06-08 op /* $OpenBSD: proc.c,v 1.41 2021/12/04 06:52:58 florian Exp $ */
4 c26f2460 2023-06-08 op * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
5 c26f2460 2023-06-08 op * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
7 c26f2460 2023-06-08 op * Permission to use, copy, modify, and distribute this software for any
8 c26f2460 2023-06-08 op * purpose with or without fee is hereby granted, provided that the above
9 c26f2460 2023-06-08 op * copyright notice and this permission notice appear in all copies.
11 c26f2460 2023-06-08 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 c26f2460 2023-06-08 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 c26f2460 2023-06-08 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 c26f2460 2023-06-08 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 c26f2460 2023-06-08 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 c26f2460 2023-06-08 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 c26f2460 2023-06-08 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 9b89eaeb 2023-06-08 op #include "gmid.h"
22 c26f2460 2023-06-08 op #include <sys/types.h>
23 9b89eaeb 2023-06-08 op /* #include <sys/queue.h> XXX provided by gmid.h */
24 9b89eaeb 2023-06-08 op /* #include <sys/tree.h> XXX provided by gmid.h */
25 c26f2460 2023-06-08 op #include <sys/socket.h>
26 c26f2460 2023-06-08 op #include <sys/wait.h>
28 c26f2460 2023-06-08 op #include <fcntl.h>
29 c26f2460 2023-06-08 op #include <grp.h>
30 c26f2460 2023-06-08 op #include <limits.h>
31 c26f2460 2023-06-08 op #include <stdio.h>
32 c26f2460 2023-06-08 op #include <stdlib.h>
33 c26f2460 2023-06-08 op #include <unistd.h>
34 c26f2460 2023-06-08 op #include <string.h>
35 c26f2460 2023-06-08 op #include <errno.h>
36 c26f2460 2023-06-08 op #include <signal.h>
37 c26f2460 2023-06-08 op #include <paths.h>
38 c26f2460 2023-06-08 op #include <pwd.h>
39 9b89eaeb 2023-06-08 op /* #include <event.h> XXX provided by gmid.h */
40 9b89eaeb 2023-06-08 op /* #include <imsg.h> XXX provided by gmid.h */
42 c26f2460 2023-06-08 op #include "log.h"
43 c26f2460 2023-06-08 op #include "proc.h"
45 c26f2460 2023-06-08 op void proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int,
46 c26f2460 2023-06-08 op int, char **);
47 c26f2460 2023-06-08 op void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
48 c26f2460 2023-06-08 op void proc_open(struct privsep *, int, int);
49 c26f2460 2023-06-08 op void proc_accept(struct privsep *, int, enum privsep_procid,
50 c26f2460 2023-06-08 op unsigned int);
51 c26f2460 2023-06-08 op void proc_close(struct privsep *);
52 c26f2460 2023-06-08 op int proc_ispeer(struct privsep_proc *, unsigned int, enum privsep_procid);
53 c26f2460 2023-06-08 op void proc_shutdown(struct privsep_proc *);
54 c26f2460 2023-06-08 op void proc_sig_handler(int, short, void *);
55 c26f2460 2023-06-08 op void proc_range(struct privsep *, enum privsep_procid, int *, int *);
56 c26f2460 2023-06-08 op int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
59 c26f2460 2023-06-08 op proc_ispeer(struct privsep_proc *procs, unsigned int nproc,
60 c26f2460 2023-06-08 op enum privsep_procid type)
62 c26f2460 2023-06-08 op unsigned int i;
64 c26f2460 2023-06-08 op for (i = 0; i < nproc; i++)
65 c26f2460 2023-06-08 op if (procs[i].p_id == type)
70 c26f2460 2023-06-08 op enum privsep_procid
71 c26f2460 2023-06-08 op proc_getid(struct privsep_proc *procs, unsigned int nproc,
72 c26f2460 2023-06-08 op const char *proc_name)
74 c26f2460 2023-06-08 op struct privsep_proc *p;
75 c26f2460 2023-06-08 op unsigned int proc;
77 c26f2460 2023-06-08 op for (proc = 0; proc < nproc; proc++) {
78 c26f2460 2023-06-08 op p = &procs[proc];
79 c26f2460 2023-06-08 op if (strcmp(p->p_title, proc_name))
82 c26f2460 2023-06-08 op return (p->p_id);
85 c26f2460 2023-06-08 op return (PROC_MAX);
89 c26f2460 2023-06-08 op proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
90 c26f2460 2023-06-08 op int debug, int argc, char **argv)
92 7fff8aa6 2023-06-09 op unsigned int proc, nargc, i, proc_i, proc_X = 0;
93 c26f2460 2023-06-08 op const char **nargv;
94 c26f2460 2023-06-08 op struct privsep_proc *p;
98 c26f2460 2023-06-08 op /* Prepare the new process argv. */
99 7fff8aa6 2023-06-09 op nargv = calloc(argc + 9, sizeof(char *));
100 c26f2460 2023-06-08 op if (nargv == NULL)
101 c26f2460 2023-06-08 op fatal("%s: calloc", __func__);
103 c26f2460 2023-06-08 op /* Copy call argument first. */
105 c26f2460 2023-06-08 op nargv[nargc++] = argv[0];
107 c26f2460 2023-06-08 op /* Set process name argument and save the position. */
108 c26f2460 2023-06-08 op nargv[nargc++] = "-T";
109 c26f2460 2023-06-08 op proc_i = nargc;
112 7fff8aa6 2023-06-09 op /* Set user and chroot */
113 7fff8aa6 2023-06-09 op if (ps->ps_pw != NULL) {
114 7fff8aa6 2023-06-09 op nargv[nargc++] = "-U";
115 7fff8aa6 2023-06-09 op nargv[nargc++] = ps->ps_pw->pw_name;
117 7fff8aa6 2023-06-09 op nargv[nargc++] = "-X";
118 7fff8aa6 2023-06-09 op proc_X = nargc;
122 c26f2460 2023-06-08 op /* Point process instance arg to stack and copy the original args. */
123 c26f2460 2023-06-08 op nargv[nargc++] = "-I";
124 c26f2460 2023-06-08 op nargv[nargc++] = num;
125 c26f2460 2023-06-08 op for (i = 1; i < (unsigned int) argc; i++)
126 c26f2460 2023-06-08 op nargv[nargc++] = argv[i];
128 c26f2460 2023-06-08 op nargv[nargc] = NULL;
130 c26f2460 2023-06-08 op for (proc = 0; proc < nproc; proc++) {
131 c26f2460 2023-06-08 op p = &procs[proc];
133 7fff8aa6 2023-06-09 op /* Update args with process title and chroot. */
134 c26f2460 2023-06-08 op nargv[proc_i] = (char *)(uintptr_t)p->p_title;
135 7fff8aa6 2023-06-09 op if (proc_X && p->p_chroot != NULL)
136 7fff8aa6 2023-06-09 op nargv[proc_X] = p->p_chroot;
138 c26f2460 2023-06-08 op /* Fire children processes. */
139 c26f2460 2023-06-08 op for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
140 c26f2460 2023-06-08 op /* Update the process instance number. */
141 c26f2460 2023-06-08 op snprintf(num, sizeof(num), "%u", i);
143 c26f2460 2023-06-08 op fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0];
144 c26f2460 2023-06-08 op ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0] = -1;
146 c26f2460 2023-06-08 op switch (fork()) {
148 c26f2460 2023-06-08 op fatal("%s: fork", __func__);
151 c26f2460 2023-06-08 op /* First create a new session */
152 c26f2460 2023-06-08 op if (setsid() == -1)
153 c26f2460 2023-06-08 op fatal("setsid");
155 c26f2460 2023-06-08 op /* Prepare parent socket. */
156 c26f2460 2023-06-08 op if (fd != PROC_PARENT_SOCK_FILENO) {
157 c26f2460 2023-06-08 op if (dup2(fd, PROC_PARENT_SOCK_FILENO)
159 c26f2460 2023-06-08 op fatal("dup2");
160 c26f2460 2023-06-08 op } else if (fcntl(fd, F_SETFD, 0) == -1)
161 c26f2460 2023-06-08 op fatal("fcntl");
163 c26f2460 2023-06-08 op /* Daemons detach from terminal. */
164 c26f2460 2023-06-08 op if (!debug && (fd =
165 c26f2460 2023-06-08 op open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
166 c26f2460 2023-06-08 op (void)dup2(fd, STDIN_FILENO);
167 c26f2460 2023-06-08 op (void)dup2(fd, STDOUT_FILENO);
168 c26f2460 2023-06-08 op (void)dup2(fd, STDERR_FILENO);
170 c26f2460 2023-06-08 op (void)close(fd);
173 c26f2460 2023-06-08 op /* obnoxious casts */
174 c26f2460 2023-06-08 op execvp(argv[0], (char *const *)nargv);
175 c26f2460 2023-06-08 op fatal("%s: execvp", __func__);
178 c26f2460 2023-06-08 op /* Close child end. */
188 c26f2460 2023-06-08 op proc_connect(struct privsep *ps)
190 c26f2460 2023-06-08 op struct imsgev *iev;
191 c26f2460 2023-06-08 op unsigned int src, dst, inst;
193 c26f2460 2023-06-08 op /* Don't distribute any sockets if we are not really going to run. */
194 c26f2460 2023-06-08 op if (ps->ps_noaction)
197 c26f2460 2023-06-08 op for (dst = 0; dst < PROC_MAX; dst++) {
198 c26f2460 2023-06-08 op /* We don't communicate with ourselves. */
199 c26f2460 2023-06-08 op if (dst == PROC_PARENT)
202 c26f2460 2023-06-08 op for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
203 c26f2460 2023-06-08 op iev = &ps->ps_ievs[dst][inst];
204 c26f2460 2023-06-08 op imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
205 c26f2460 2023-06-08 op event_set(&iev->ev, iev->ibuf.fd, iev->events,
206 c26f2460 2023-06-08 op iev->handler, iev->data);
207 c26f2460 2023-06-08 op event_add(&iev->ev, NULL);
211 c26f2460 2023-06-08 op /* Distribute the socketpair()s for everyone. */
212 c26f2460 2023-06-08 op for (src = 0; src < PROC_MAX; src++)
213 c26f2460 2023-06-08 op for (dst = src; dst < PROC_MAX; dst++) {
214 c26f2460 2023-06-08 op /* Parent already distributed its fds. */
215 c26f2460 2023-06-08 op if (src == PROC_PARENT || dst == PROC_PARENT)
218 c26f2460 2023-06-08 op proc_open(ps, src, dst);
223 c26f2460 2023-06-08 op proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
224 c26f2460 2023-06-08 op int debug, int argc, char **argv, enum privsep_procid proc_id)
226 c26f2460 2023-06-08 op struct privsep_proc *p = NULL;
227 c26f2460 2023-06-08 op struct privsep_pipes *pa, *pb;
228 c26f2460 2023-06-08 op unsigned int proc;
229 c26f2460 2023-06-08 op unsigned int dst;
232 c26f2460 2023-06-08 op /* Don't initiate anything if we are not really going to run. */
233 c26f2460 2023-06-08 op if (ps->ps_noaction)
236 c26f2460 2023-06-08 op if (proc_id == PROC_PARENT) {
237 c26f2460 2023-06-08 op privsep_process = PROC_PARENT;
238 c26f2460 2023-06-08 op proc_setup(ps, procs, nproc);
241 c26f2460 2023-06-08 op * Create the children sockets so we can use them
242 c26f2460 2023-06-08 op * to distribute the rest of the socketpair()s using
243 c26f2460 2023-06-08 op * proc_connect() later.
245 c26f2460 2023-06-08 op for (dst = 0; dst < PROC_MAX; dst++) {
246 c26f2460 2023-06-08 op /* Don't create socket for ourselves. */
247 c26f2460 2023-06-08 op if (dst == PROC_PARENT)
250 c26f2460 2023-06-08 op for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
251 c26f2460 2023-06-08 op pa = &ps->ps_pipes[PROC_PARENT][0];
252 c26f2460 2023-06-08 op pb = &ps->ps_pipes[dst][proc];
253 c26f2460 2023-06-08 op if (socketpair(AF_UNIX,
254 c26f2460 2023-06-08 op SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
255 c26f2460 2023-06-08 op PF_UNSPEC, fds) == -1)
256 c26f2460 2023-06-08 op fatal("%s: socketpair", __func__);
258 c26f2460 2023-06-08 op pa->pp_pipes[dst][proc] = fds[0];
259 c26f2460 2023-06-08 op pb->pp_pipes[PROC_PARENT][0] = fds[1];
263 c26f2460 2023-06-08 op /* Engage! */
264 c26f2460 2023-06-08 op proc_exec(ps, procs, nproc, debug, argc, argv);
268 c26f2460 2023-06-08 op /* Initialize a child */
269 c26f2460 2023-06-08 op for (proc = 0; proc < nproc; proc++) {
270 c26f2460 2023-06-08 op if (procs[proc].p_id != proc_id)
272 c26f2460 2023-06-08 op p = &procs[proc];
275 c26f2460 2023-06-08 op if (p == NULL || p->p_init == NULL)
276 c26f2460 2023-06-08 op fatalx("%s: process %d missing process initialization",
277 c26f2460 2023-06-08 op __func__, proc_id);
279 c26f2460 2023-06-08 op p->p_init(ps, p);
281 c26f2460 2023-06-08 op fatalx("failed to initiate child process");
285 c26f2460 2023-06-08 op proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
286 c26f2460 2023-06-08 op unsigned int n)
288 c26f2460 2023-06-08 op struct privsep_pipes *pp = ps->ps_pp;
289 c26f2460 2023-06-08 op struct imsgev *iev;
291 c26f2460 2023-06-08 op if (ps->ps_ievs[dst] == NULL) {
292 c26f2460 2023-06-08 op #if DEBUG > 1
293 c26f2460 2023-06-08 op log_debug("%s: %s src %d %d to dst %d %d not connected",
294 c26f2460 2023-06-08 op __func__, ps->ps_title[privsep_process],
295 c26f2460 2023-06-08 op privsep_process, ps->ps_instance + 1,
302 c26f2460 2023-06-08 op if (pp->pp_pipes[dst][n] != -1) {
303 c26f2460 2023-06-08 op log_warnx("%s: duplicated descriptor", __func__);
307 c26f2460 2023-06-08 op pp->pp_pipes[dst][n] = fd;
309 c26f2460 2023-06-08 op iev = &ps->ps_ievs[dst][n];
310 c26f2460 2023-06-08 op imsg_init(&iev->ibuf, fd);
311 c26f2460 2023-06-08 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
312 c26f2460 2023-06-08 op event_add(&iev->ev, NULL);
316 c26f2460 2023-06-08 op proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
318 c26f2460 2023-06-08 op unsigned int i, j, src, dst, id;
319 c26f2460 2023-06-08 op struct privsep_pipes *pp;
321 c26f2460 2023-06-08 op /* Initialize parent title, ps_instances and procs. */
322 c26f2460 2023-06-08 op ps->ps_title[PROC_PARENT] = "parent";
324 c26f2460 2023-06-08 op for (src = 0; src < PROC_MAX; src++)
325 c26f2460 2023-06-08 op /* Default to 1 process instance */
326 c26f2460 2023-06-08 op if (ps->ps_instances[src] < 1)
327 c26f2460 2023-06-08 op ps->ps_instances[src] = 1;
329 c26f2460 2023-06-08 op for (src = 0; src < nproc; src++) {
330 c26f2460 2023-06-08 op procs[src].p_ps = ps;
331 c26f2460 2023-06-08 op if (procs[src].p_cb == NULL)
332 c26f2460 2023-06-08 op procs[src].p_cb = proc_dispatch_null;
334 c26f2460 2023-06-08 op id = procs[src].p_id;
335 c26f2460 2023-06-08 op ps->ps_title[id] = procs[src].p_title;
336 c26f2460 2023-06-08 op if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
337 c26f2460 2023-06-08 op sizeof(struct imsgev))) == NULL)
338 c26f2460 2023-06-08 op fatal("%s: calloc", __func__);
340 c26f2460 2023-06-08 op /* With this set up, we are ready to call imsg_init(). */
341 c26f2460 2023-06-08 op for (i = 0; i < ps->ps_instances[id]; i++) {
342 c26f2460 2023-06-08 op ps->ps_ievs[id][i].handler = proc_dispatch;
343 c26f2460 2023-06-08 op ps->ps_ievs[id][i].events = EV_READ;
344 c26f2460 2023-06-08 op ps->ps_ievs[id][i].proc = &procs[src];
345 c26f2460 2023-06-08 op ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
350 c26f2460 2023-06-08 op * Allocate pipes for all process instances (incl. parent)
352 c26f2460 2023-06-08 op * - ps->ps_pipes: N:M mapping
353 c26f2460 2023-06-08 op * N source processes connected to M destination processes:
354 c26f2460 2023-06-08 op * [src][instances][dst][instances], for example
355 c26f2460 2023-06-08 op * [PROC_RELAY][3][PROC_CA][3]
357 c26f2460 2023-06-08 op * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
358 c26f2460 2023-06-08 op * Each process instance has a destination array of socketpair fds:
359 c26f2460 2023-06-08 op * [dst][instances], for example
360 c26f2460 2023-06-08 op * [PROC_PARENT][0]
362 c26f2460 2023-06-08 op for (src = 0; src < PROC_MAX; src++) {
363 c26f2460 2023-06-08 op /* Allocate destination array for each process */
364 c26f2460 2023-06-08 op if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
365 c26f2460 2023-06-08 op sizeof(struct privsep_pipes))) == NULL)
366 c26f2460 2023-06-08 op fatal("%s: calloc", __func__);
368 c26f2460 2023-06-08 op for (i = 0; i < ps->ps_instances[src]; i++) {
369 c26f2460 2023-06-08 op pp = &ps->ps_pipes[src][i];
371 c26f2460 2023-06-08 op for (dst = 0; dst < PROC_MAX; dst++) {
372 c26f2460 2023-06-08 op /* Allocate maximum fd integers */
373 c26f2460 2023-06-08 op if ((pp->pp_pipes[dst] =
374 c26f2460 2023-06-08 op calloc(ps->ps_instances[dst],
375 c26f2460 2023-06-08 op sizeof(int))) == NULL)
376 c26f2460 2023-06-08 op fatal("%s: calloc", __func__);
378 c26f2460 2023-06-08 op /* Mark fd as unused */
379 c26f2460 2023-06-08 op for (j = 0; j < ps->ps_instances[dst]; j++)
380 c26f2460 2023-06-08 op pp->pp_pipes[dst][j] = -1;
385 c26f2460 2023-06-08 op ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
389 c26f2460 2023-06-08 op proc_kill(struct privsep *ps)
393 c26f2460 2023-06-08 op int len, status;
395 c26f2460 2023-06-08 op if (privsep_process != PROC_PARENT)
398 c26f2460 2023-06-08 op proc_close(ps);
401 c26f2460 2023-06-08 op pid = waitpid(WAIT_ANY, &status, 0);
402 c26f2460 2023-06-08 op if (pid <= 0)
405 c26f2460 2023-06-08 op if (WIFSIGNALED(status)) {
406 c26f2460 2023-06-08 op len = asprintf(&cause, "terminated; signal %d",
407 c26f2460 2023-06-08 op WTERMSIG(status));
408 c26f2460 2023-06-08 op } else if (WIFEXITED(status)) {
409 c26f2460 2023-06-08 op if (WEXITSTATUS(status) != 0)
410 c26f2460 2023-06-08 op len = asprintf(&cause, "exited abnormally");
416 c26f2460 2023-06-08 op if (len == 0) {
417 c26f2460 2023-06-08 op /* child exited OK, don't print a warning message */
418 c26f2460 2023-06-08 op } else if (len != -1) {
419 c26f2460 2023-06-08 op log_warnx("lost child: pid %u %s", pid, cause);
422 c26f2460 2023-06-08 op log_warnx("lost child: pid %u", pid);
423 c26f2460 2023-06-08 op } while (pid != -1 || errno == EINTR);
427 c26f2460 2023-06-08 op proc_open(struct privsep *ps, int src, int dst)
429 c26f2460 2023-06-08 op struct privsep_pipes *pa, *pb;
430 c26f2460 2023-06-08 op struct privsep_fd pf;
432 c26f2460 2023-06-08 op unsigned int i, j;
434 c26f2460 2023-06-08 op /* Exchange pipes between process. */
435 c26f2460 2023-06-08 op for (i = 0; i < ps->ps_instances[src]; i++) {
436 c26f2460 2023-06-08 op for (j = 0; j < ps->ps_instances[dst]; j++) {
437 c26f2460 2023-06-08 op /* Don't create sockets for ourself. */
438 c26f2460 2023-06-08 op if (src == dst && i == j)
441 c26f2460 2023-06-08 op /* Servers don't talk to each other. */
442 c26f2460 2023-06-08 op if (src == PROC_SERVER && dst == PROC_SERVER)
445 c26f2460 2023-06-08 op pa = &ps->ps_pipes[src][i];
446 c26f2460 2023-06-08 op pb = &ps->ps_pipes[dst][j];
447 c26f2460 2023-06-08 op if (socketpair(AF_UNIX,
448 c26f2460 2023-06-08 op SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
449 c26f2460 2023-06-08 op PF_UNSPEC, fds) == -1)
450 c26f2460 2023-06-08 op fatal("%s: socketpair", __func__);
452 c26f2460 2023-06-08 op pa->pp_pipes[dst][j] = fds[0];
453 c26f2460 2023-06-08 op pb->pp_pipes[src][i] = fds[1];
455 c26f2460 2023-06-08 op pf.pf_procid = src;
456 c26f2460 2023-06-08 op pf.pf_instance = i;
457 c26f2460 2023-06-08 op if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
458 c26f2460 2023-06-08 op -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
459 c26f2460 2023-06-08 op fatal("%s: proc_compose_imsg", __func__);
461 c26f2460 2023-06-08 op pf.pf_procid = dst;
462 c26f2460 2023-06-08 op pf.pf_instance = j;
463 c26f2460 2023-06-08 op if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
464 c26f2460 2023-06-08 op -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
465 c26f2460 2023-06-08 op fatal("%s: proc_compose_imsg", __func__);
468 c26f2460 2023-06-08 op * We have to flush to send the descriptors and close
469 c26f2460 2023-06-08 op * them to avoid the fd ramp on startup.
471 c26f2460 2023-06-08 op if (proc_flush_imsg(ps, src, i) == -1 ||
472 c26f2460 2023-06-08 op proc_flush_imsg(ps, dst, j) == -1)
473 c26f2460 2023-06-08 op fatal("%s: imsg_flush", __func__);
479 c26f2460 2023-06-08 op proc_close(struct privsep *ps)
481 c26f2460 2023-06-08 op unsigned int dst, n;
482 c26f2460 2023-06-08 op struct privsep_pipes *pp;
484 c26f2460 2023-06-08 op if (ps == NULL)
487 c26f2460 2023-06-08 op pp = ps->ps_pp;
489 c26f2460 2023-06-08 op for (dst = 0; dst < PROC_MAX; dst++) {
490 c26f2460 2023-06-08 op if (ps->ps_ievs[dst] == NULL)
493 c26f2460 2023-06-08 op for (n = 0; n < ps->ps_instances[dst]; n++) {
494 c26f2460 2023-06-08 op if (pp->pp_pipes[dst][n] == -1)
497 c26f2460 2023-06-08 op /* Cancel the fd, close and invalidate the fd */
498 c26f2460 2023-06-08 op event_del(&(ps->ps_ievs[dst][n].ev));
499 c26f2460 2023-06-08 op imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
500 c26f2460 2023-06-08 op close(pp->pp_pipes[dst][n]);
501 c26f2460 2023-06-08 op pp->pp_pipes[dst][n] = -1;
503 c26f2460 2023-06-08 op free(ps->ps_ievs[dst]);
508 c26f2460 2023-06-08 op proc_shutdown(struct privsep_proc *p)
510 c26f2460 2023-06-08 op struct privsep *ps = p->p_ps;
512 c26f2460 2023-06-08 op if (p->p_shutdown != NULL)
513 c26f2460 2023-06-08 op (*p->p_shutdown)();
515 c26f2460 2023-06-08 op proc_close(ps);
517 c26f2460 2023-06-08 op log_info("%s exiting, pid %d", p->p_title, getpid());
523 c26f2460 2023-06-08 op proc_sig_handler(int sig, short event, void *arg)
525 c26f2460 2023-06-08 op struct privsep_proc *p = arg;
527 c26f2460 2023-06-08 op switch (sig) {
529 c26f2460 2023-06-08 op case SIGTERM:
530 c26f2460 2023-06-08 op proc_shutdown(p);
532 c26f2460 2023-06-08 op case SIGCHLD:
537 c26f2460 2023-06-08 op fatalx("%s: unexpected signal", __func__);
538 c26f2460 2023-06-08 op /* NOTREACHED */
543 c26f2460 2023-06-08 op proc_run(struct privsep *ps, struct privsep_proc *p,
544 c26f2460 2023-06-08 op struct privsep_proc *procs, unsigned int nproc,
545 c26f2460 2023-06-08 op void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
547 c26f2460 2023-06-08 op struct passwd *pw;
548 c26f2460 2023-06-08 op const char *root;
550 c26f2460 2023-06-08 op log_procinit(p->p_title);
552 c26f2460 2023-06-08 op if (ps->ps_pw == NULL)
555 c26f2460 2023-06-08 op /* Set the process group of the current process */
556 c26f2460 2023-06-08 op setpgid(0, 0);
558 c26f2460 2023-06-08 op /* Use non-standard user */
559 c26f2460 2023-06-08 op if (p->p_pw != NULL)
560 c26f2460 2023-06-08 op pw = p->p_pw;
562 c26f2460 2023-06-08 op pw = ps->ps_pw;
564 c26f2460 2023-06-08 op /* Change root directory */
565 c26f2460 2023-06-08 op if (p->p_chroot != NULL)
566 c26f2460 2023-06-08 op root = p->p_chroot;
568 c26f2460 2023-06-08 op root = pw->pw_dir;
570 c26f2460 2023-06-08 op if (chroot(root) == -1)
571 c26f2460 2023-06-08 op fatal("%s: chroot", __func__);
572 c26f2460 2023-06-08 op if (chdir("/") == -1)
573 c26f2460 2023-06-08 op fatal("%s: chdir(\"/\")", __func__);
575 c26f2460 2023-06-08 op privsep_process = p->p_id;
577 c26f2460 2023-06-08 op setproctitle("%s", p->p_title);
579 c26f2460 2023-06-08 op if (setgroups(1, &pw->pw_gid) ||
580 c26f2460 2023-06-08 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
581 c26f2460 2023-06-08 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
582 c26f2460 2023-06-08 op fatal("%s: cannot drop privileges", __func__);
585 c26f2460 2023-06-08 op event_init();
587 c26f2460 2023-06-08 op signal(SIGPIPE, SIG_IGN);
589 c26f2460 2023-06-08 op signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
590 c26f2460 2023-06-08 op signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
591 c26f2460 2023-06-08 op signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
592 c26f2460 2023-06-08 op signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
594 c26f2460 2023-06-08 op signal_add(&ps->ps_evsigint, NULL);
595 c26f2460 2023-06-08 op signal_add(&ps->ps_evsigterm, NULL);
596 c26f2460 2023-06-08 op signal_add(&ps->ps_evsigchld, NULL);
597 c26f2460 2023-06-08 op signal_add(&ps->ps_evsighup, NULL);
599 c26f2460 2023-06-08 op proc_setup(ps, procs, nproc);
600 c26f2460 2023-06-08 op proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0);
602 c26f2460 2023-06-08 op log_debug("%s: %s %d/%d, pid %d", __func__, p->p_title,
603 c26f2460 2023-06-08 op ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
605 c26f2460 2023-06-08 op if (run != NULL)
606 c26f2460 2023-06-08 op run(ps, p, arg);
608 c26f2460 2023-06-08 op event_dispatch();
610 c26f2460 2023-06-08 op proc_shutdown(p);
614 c26f2460 2023-06-08 op proc_dispatch(int fd, short event, void *arg)
616 c26f2460 2023-06-08 op struct imsgev *iev = arg;
617 c26f2460 2023-06-08 op struct privsep_proc *p = iev->proc;
618 c26f2460 2023-06-08 op struct privsep *ps = p->p_ps;
619 c26f2460 2023-06-08 op struct imsgbuf *ibuf;
620 c26f2460 2023-06-08 op struct imsg imsg;
622 c26f2460 2023-06-08 op const char *title;
623 c26f2460 2023-06-08 op struct privsep_fd pf;
625 c26f2460 2023-06-08 op title = ps->ps_title[privsep_process];
626 c26f2460 2023-06-08 op ibuf = &iev->ibuf;
628 c26f2460 2023-06-08 op if (event & EV_READ) {
629 c26f2460 2023-06-08 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
630 c26f2460 2023-06-08 op fatal("%s: imsg_read", __func__);
631 c26f2460 2023-06-08 op if (n == 0) {
632 c26f2460 2023-06-08 op /* this pipe is dead, so remove the event handler */
633 c26f2460 2023-06-08 op event_del(&iev->ev);
634 c26f2460 2023-06-08 op event_loopexit(NULL);
639 c26f2460 2023-06-08 op if (event & EV_WRITE) {
640 c26f2460 2023-06-08 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
641 c26f2460 2023-06-08 op fatal("%s: msgbuf_write", __func__);
642 c26f2460 2023-06-08 op if (n == 0) {
643 c26f2460 2023-06-08 op /* this pipe is dead, so remove the event handler */
644 c26f2460 2023-06-08 op event_del(&iev->ev);
645 c26f2460 2023-06-08 op event_loopexit(NULL);
651 c26f2460 2023-06-08 op if ((n = imsg_get(ibuf, &imsg)) == -1)
652 c26f2460 2023-06-08 op fatal("%s: imsg_get", __func__);
656 c26f2460 2023-06-08 op #if DEBUG > 1
657 c26f2460 2023-06-08 op log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
658 c26f2460 2023-06-08 op __func__, title, ps->ps_instance + 1,
659 c26f2460 2023-06-08 op imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
663 c26f2460 2023-06-08 op * Check the message with the program callback
665 c26f2460 2023-06-08 op if ((p->p_cb)(fd, p, &imsg) == 0) {
666 c26f2460 2023-06-08 op /* Message was handled by the callback, continue */
667 c26f2460 2023-06-08 op imsg_free(&imsg);
672 c26f2460 2023-06-08 op * Generic message handling
674 c26f2460 2023-06-08 op switch (imsg.hdr.type) {
675 c26f2460 2023-06-08 op case IMSG_CTL_PROCFD:
676 c26f2460 2023-06-08 op IMSG_SIZE_CHECK(&imsg, &pf);
677 c26f2460 2023-06-08 op memcpy(&pf, imsg.data, sizeof(pf));
678 c26f2460 2023-06-08 op proc_accept(ps, imsg.fd, pf.pf_procid,
679 c26f2460 2023-06-08 op pf.pf_instance);
682 c26f2460 2023-06-08 op fatalx("%s: %s %d got invalid imsg %d peerid %d "
683 c26f2460 2023-06-08 op "from %s %d",
684 c26f2460 2023-06-08 op __func__, title, ps->ps_instance + 1,
685 c26f2460 2023-06-08 op imsg.hdr.type, imsg.hdr.peerid,
686 c26f2460 2023-06-08 op p->p_title, imsg.hdr.pid);
688 c26f2460 2023-06-08 op imsg_free(&imsg);
690 c26f2460 2023-06-08 op imsg_event_add(iev);
694 c26f2460 2023-06-08 op proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
700 c26f2460 2023-06-08 op * imsg helper functions
704 c26f2460 2023-06-08 op imsg_event_add(struct imsgev *iev)
706 c26f2460 2023-06-08 op if (iev->handler == NULL) {
707 c26f2460 2023-06-08 op imsg_flush(&iev->ibuf);
711 c26f2460 2023-06-08 op iev->events = EV_READ;
712 c26f2460 2023-06-08 op if (iev->ibuf.w.queued)
713 c26f2460 2023-06-08 op iev->events |= EV_WRITE;
715 c26f2460 2023-06-08 op event_del(&iev->ev);
716 c26f2460 2023-06-08 op event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
717 c26f2460 2023-06-08 op event_add(&iev->ev, NULL);
721 c26f2460 2023-06-08 op imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
722 c26f2460 2023-06-08 op pid_t pid, int fd, void *data, uint16_t datalen)
726 c26f2460 2023-06-08 op if ((ret = imsg_compose(&iev->ibuf, type, peerid,
727 c26f2460 2023-06-08 op pid, fd, data, datalen)) == -1)
728 c26f2460 2023-06-08 op return (ret);
729 c26f2460 2023-06-08 op imsg_event_add(iev);
730 c26f2460 2023-06-08 op return (ret);
734 c26f2460 2023-06-08 op imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
735 c26f2460 2023-06-08 op pid_t pid, int fd, const struct iovec *iov, int iovcnt)
739 c26f2460 2023-06-08 op if ((ret = imsg_composev(&iev->ibuf, type, peerid,
740 c26f2460 2023-06-08 op pid, fd, iov, iovcnt)) == -1)
741 c26f2460 2023-06-08 op return (ret);
742 c26f2460 2023-06-08 op imsg_event_add(iev);
743 c26f2460 2023-06-08 op return (ret);
747 c26f2460 2023-06-08 op proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
749 c26f2460 2023-06-08 op if (*n == -1) {
750 c26f2460 2023-06-08 op /* Use a range of all target instances */
752 c26f2460 2023-06-08 op *m = ps->ps_instances[id];
754 c26f2460 2023-06-08 op /* Use only a single slot of the specified peer process */
760 c26f2460 2023-06-08 op proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
761 c26f2460 2023-06-08 op uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
765 c26f2460 2023-06-08 op proc_range(ps, id, &n, &m);
766 c26f2460 2023-06-08 op for (; n < m; n++) {
767 c26f2460 2023-06-08 op if (imsg_compose_event(&ps->ps_ievs[id][n],
768 c26f2460 2023-06-08 op type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
776 c26f2460 2023-06-08 op proc_compose(struct privsep *ps, enum privsep_procid id,
777 c26f2460 2023-06-08 op uint16_t type, void *data, uint16_t datalen)
779 c26f2460 2023-06-08 op return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
783 c26f2460 2023-06-08 op proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
784 c26f2460 2023-06-08 op uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
788 c26f2460 2023-06-08 op proc_range(ps, id, &n, &m);
789 c26f2460 2023-06-08 op for (; n < m; n++)
790 c26f2460 2023-06-08 op if (imsg_composev_event(&ps->ps_ievs[id][n],
791 c26f2460 2023-06-08 op type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
798 c26f2460 2023-06-08 op proc_composev(struct privsep *ps, enum privsep_procid id,
799 c26f2460 2023-06-08 op uint16_t type, const struct iovec *iov, int iovcnt)
801 c26f2460 2023-06-08 op return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
805 c26f2460 2023-06-08 op proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
806 c26f2460 2023-06-08 op enum privsep_procid id, int n)
808 c26f2460 2023-06-08 op return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
809 c26f2460 2023-06-08 op imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
812 c26f2460 2023-06-08 op struct imsgbuf *
813 c26f2460 2023-06-08 op proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
817 c26f2460 2023-06-08 op proc_range(ps, id, &n, &m);
818 c26f2460 2023-06-08 op return (&ps->ps_ievs[id][n].ibuf);
821 c26f2460 2023-06-08 op struct imsgev *
822 c26f2460 2023-06-08 op proc_iev(struct privsep *ps, enum privsep_procid id, int n)
826 c26f2460 2023-06-08 op proc_range(ps, id, &n, &m);
827 c26f2460 2023-06-08 op return (&ps->ps_ievs[id][n]);
830 c26f2460 2023-06-08 op /* This function should only be called with care as it breaks async I/O */
832 c26f2460 2023-06-08 op proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
834 c26f2460 2023-06-08 op struct imsgbuf *ibuf;
835 c26f2460 2023-06-08 op int m, ret = 0;
837 c26f2460 2023-06-08 op proc_range(ps, id, &n, &m);
838 c26f2460 2023-06-08 op for (; n < m; n++) {
839 c26f2460 2023-06-08 op if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
843 c26f2460 2023-06-08 op ret = imsg_flush(ibuf);
844 c26f2460 2023-06-08 op } while (ret == -1 && errno == EAGAIN);
845 c26f2460 2023-06-08 op if (ret == -1)
847 c26f2460 2023-06-08 op imsg_event_add(&ps->ps_ievs[id][n]);
850 c26f2460 2023-06-08 op return (ret);