2 8a35f56c 2022-07-16 thomas * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
5 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
6 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
7 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
9 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
19 8a35f56c 2022-07-16 thomas #include <sys/types.h>
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
22 8a35f56c 2022-07-16 thomas #include <sys/wait.h>
24 8a35f56c 2022-07-16 thomas #include <fcntl.h>
25 8a35f56c 2022-07-16 thomas #include <stdio.h>
26 8a35f56c 2022-07-16 thomas #include <stdlib.h>
27 8a35f56c 2022-07-16 thomas #include <unistd.h>
28 8a35f56c 2022-07-16 thomas #include <string.h>
29 8a35f56c 2022-07-16 thomas #include <errno.h>
30 8a35f56c 2022-07-16 thomas #include <signal.h>
31 8a35f56c 2022-07-16 thomas #include <pwd.h>
32 8a35f56c 2022-07-16 thomas #include <event.h>
34 8a35f56c 2022-07-16 thomas #include "proc.h"
36 8a35f56c 2022-07-16 thomas void proc_exec(struct privsep *, struct privsep_proc *, unsigned int,
37 8a35f56c 2022-07-16 thomas int, char **);
38 8a35f56c 2022-07-16 thomas void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
39 8a35f56c 2022-07-16 thomas void proc_open(struct privsep *, int, int);
40 8a35f56c 2022-07-16 thomas void proc_accept(struct privsep *, int, enum privsep_procid,
41 8a35f56c 2022-07-16 thomas unsigned int);
42 8a35f56c 2022-07-16 thomas void proc_close(struct privsep *);
43 8a35f56c 2022-07-16 thomas void proc_shutdown(struct privsep_proc *);
44 8a35f56c 2022-07-16 thomas void proc_sig_handler(int, short, void *);
45 8a35f56c 2022-07-16 thomas int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
47 8a35f56c 2022-07-16 thomas enum privsep_procid privsep_process;
49 8a35f56c 2022-07-16 thomas enum privsep_procid
50 8a35f56c 2022-07-16 thomas proc_getid(struct privsep_proc *procs, unsigned int nproc,
51 8a35f56c 2022-07-16 thomas const char *proc_name)
53 8a35f56c 2022-07-16 thomas struct privsep_proc *p;
54 8a35f56c 2022-07-16 thomas unsigned int proc;
56 8a35f56c 2022-07-16 thomas for (proc = 0; proc < nproc; proc++) {
57 8a35f56c 2022-07-16 thomas p = &procs[proc];
58 8a35f56c 2022-07-16 thomas if (strcmp(p->p_title, proc_name))
61 8a35f56c 2022-07-16 thomas return (p->p_id);
64 8a35f56c 2022-07-16 thomas return (PROC_MAX);
68 8a35f56c 2022-07-16 thomas proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
69 8a35f56c 2022-07-16 thomas int argc, char **argv)
71 8a35f56c 2022-07-16 thomas unsigned int proc, nargc, i, proc_i;
72 8a35f56c 2022-07-16 thomas char **nargv;
73 8a35f56c 2022-07-16 thomas struct privsep_proc *p;
74 8a35f56c 2022-07-16 thomas char num[32];
77 8a35f56c 2022-07-16 thomas /* Prepare the new process argv. */
78 8a35f56c 2022-07-16 thomas nargv = calloc(argc + 5, sizeof(char *));
79 8a35f56c 2022-07-16 thomas if (nargv == NULL)
80 8a35f56c 2022-07-16 thomas fatal("%s: calloc", __func__);
82 8a35f56c 2022-07-16 thomas /* Copy call argument first. */
83 8a35f56c 2022-07-16 thomas nargc = 0;
84 8a35f56c 2022-07-16 thomas nargv[nargc++] = argv[0];
86 8a35f56c 2022-07-16 thomas /* Set process name argument and save the position. */
87 8a35f56c 2022-07-16 thomas nargv[nargc] = strdup("-P");
88 8a35f56c 2022-07-16 thomas if (nargv[nargc] == NULL)
89 8a35f56c 2022-07-16 thomas fatal("%s: strdup", __func__);
91 8a35f56c 2022-07-16 thomas proc_i = nargc;
94 8a35f56c 2022-07-16 thomas /* Point process instance arg to stack and copy the original args. */
95 8a35f56c 2022-07-16 thomas nargv[nargc] = strdup("-I");
96 8a35f56c 2022-07-16 thomas if (nargv[nargc] == NULL)
97 8a35f56c 2022-07-16 thomas fatal("%s: strdup", __func__);
99 8a35f56c 2022-07-16 thomas nargv[nargc++] = num;
100 8a35f56c 2022-07-16 thomas for (i = 1; i < (unsigned int) argc; i++)
101 8a35f56c 2022-07-16 thomas nargv[nargc++] = argv[i];
103 8a35f56c 2022-07-16 thomas nargv[nargc] = NULL;
105 8a35f56c 2022-07-16 thomas for (proc = 0; proc < nproc; proc++) {
106 8a35f56c 2022-07-16 thomas p = &procs[proc];
108 8a35f56c 2022-07-16 thomas /* Update args with process title. */
109 8a35f56c 2022-07-16 thomas nargv[proc_i] = (char *)(uintptr_t)p->p_title;
111 8a35f56c 2022-07-16 thomas /* Fire children processes. */
112 8a35f56c 2022-07-16 thomas for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
113 8a35f56c 2022-07-16 thomas /* Update the process instance number. */
114 8a35f56c 2022-07-16 thomas snprintf(num, sizeof(num), "%u", i);
116 8a35f56c 2022-07-16 thomas fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0];
117 8a35f56c 2022-07-16 thomas ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0] = -1;
119 8a35f56c 2022-07-16 thomas switch (fork()) {
121 8a35f56c 2022-07-16 thomas fatal("%s: fork", __func__);
124 8a35f56c 2022-07-16 thomas /* First create a new session */
125 8a35f56c 2022-07-16 thomas if (setsid() == -1)
126 8a35f56c 2022-07-16 thomas fatal("setsid");
128 8a35f56c 2022-07-16 thomas /* Prepare parent socket. */
129 8a35f56c 2022-07-16 thomas if (fd != PROC_GOTWEBD_SOCK_FILENO) {
130 8a35f56c 2022-07-16 thomas if (dup2(fd, PROC_GOTWEBD_SOCK_FILENO)
132 8a35f56c 2022-07-16 thomas fatal("dup2");
133 8a35f56c 2022-07-16 thomas } else if (fcntl(fd, F_SETFD, 0) == -1)
134 8a35f56c 2022-07-16 thomas fatal("fcntl");
136 8a35f56c 2022-07-16 thomas execvp(argv[0], nargv);
137 8a35f56c 2022-07-16 thomas fatal("%s: execvp", __func__);
140 8a35f56c 2022-07-16 thomas /* Close child end. */
141 8a35f56c 2022-07-16 thomas close(fd);
147 8a35f56c 2022-07-16 thomas free(nargv);
151 8a35f56c 2022-07-16 thomas proc_connect(struct privsep *ps)
153 8a35f56c 2022-07-16 thomas struct imsgev *iev;
154 8a35f56c 2022-07-16 thomas unsigned int src, dst, inst;
156 8a35f56c 2022-07-16 thomas /* Don't distribute any sockets if we are not really going to run. */
157 8a35f56c 2022-07-16 thomas if (ps->ps_noaction)
160 8a35f56c 2022-07-16 thomas for (dst = 0; dst < PROC_MAX; dst++) {
161 8a35f56c 2022-07-16 thomas /* We don't communicate with ourselves. */
162 8a35f56c 2022-07-16 thomas if (dst == PROC_GOTWEBD)
163 8a35f56c 2022-07-16 thomas continue;
165 8a35f56c 2022-07-16 thomas for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
166 8a35f56c 2022-07-16 thomas iev = &ps->ps_ievs[dst][inst];
167 8a35f56c 2022-07-16 thomas imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
168 8a35f56c 2022-07-16 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events,
169 8a35f56c 2022-07-16 thomas iev->handler, iev->data);
170 8a35f56c 2022-07-16 thomas event_add(&iev->ev, NULL);
174 8a35f56c 2022-07-16 thomas /* Distribute the socketpair()s for everyone. */
175 8a35f56c 2022-07-16 thomas for (src = 0; src < PROC_MAX; src++)
176 8a35f56c 2022-07-16 thomas for (dst = src; dst < PROC_MAX; dst++) {
177 8a35f56c 2022-07-16 thomas /* Parent already distributed its fds. */
178 8a35f56c 2022-07-16 thomas if (src == PROC_GOTWEBD || dst == PROC_GOTWEBD)
179 8a35f56c 2022-07-16 thomas continue;
181 8a35f56c 2022-07-16 thomas proc_open(ps, src, dst);
186 8a35f56c 2022-07-16 thomas proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
187 8a35f56c 2022-07-16 thomas int argc, char **argv, enum privsep_procid proc_id)
189 8a35f56c 2022-07-16 thomas struct privsep_proc *p = NULL;
190 8a35f56c 2022-07-16 thomas struct privsep_pipes *pa, *pb;
191 8a35f56c 2022-07-16 thomas unsigned int proc;
192 8a35f56c 2022-07-16 thomas unsigned int dst;
193 8a35f56c 2022-07-16 thomas int fds[2];
195 8a35f56c 2022-07-16 thomas /* Don't initiate anything if we are not really going to run. */
196 8a35f56c 2022-07-16 thomas if (ps->ps_noaction)
199 8a35f56c 2022-07-16 thomas if (proc_id == PROC_GOTWEBD) {
200 8a35f56c 2022-07-16 thomas privsep_process = PROC_GOTWEBD;
201 8a35f56c 2022-07-16 thomas proc_setup(ps, procs, nproc);
204 b6b86fd1 2022-08-30 thomas * Create the children sockets so we can use them
205 8a35f56c 2022-07-16 thomas * to distribute the rest of the socketpair()s using
206 8a35f56c 2022-07-16 thomas * proc_connect() later.
208 8a35f56c 2022-07-16 thomas for (dst = 0; dst < PROC_MAX; dst++) {
209 8a35f56c 2022-07-16 thomas /* Don't create socket for ourselves. */
210 8a35f56c 2022-07-16 thomas if (dst == PROC_GOTWEBD)
211 8a35f56c 2022-07-16 thomas continue;
213 8a35f56c 2022-07-16 thomas for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
214 8a35f56c 2022-07-16 thomas pa = &ps->ps_pipes[PROC_GOTWEBD][0];
215 8a35f56c 2022-07-16 thomas pb = &ps->ps_pipes[dst][proc];
216 dd7a7d5b 2022-07-16 thomas int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
217 dd7a7d5b 2022-07-16 thomas #ifdef SOCK_CLOEXEC
218 dd7a7d5b 2022-07-16 thomas sock_flags |= SOCK_CLOEXEC;
220 dd7a7d5b 2022-07-16 thomas if (socketpair(AF_UNIX, sock_flags,
221 8a35f56c 2022-07-16 thomas PF_UNSPEC, fds) == -1)
222 8a35f56c 2022-07-16 thomas fatal("%s: socketpair", __func__);
224 8a35f56c 2022-07-16 thomas pa->pp_pipes[dst][proc] = fds[0];
225 8a35f56c 2022-07-16 thomas pb->pp_pipes[PROC_GOTWEBD][0] = fds[1];
229 8a35f56c 2022-07-16 thomas /* Engage! */
230 8a35f56c 2022-07-16 thomas proc_exec(ps, procs, nproc, argc, argv);
234 8a35f56c 2022-07-16 thomas /* Initialize a child */
235 8a35f56c 2022-07-16 thomas for (proc = 0; proc < nproc; proc++) {
236 8a35f56c 2022-07-16 thomas if (procs[proc].p_id != proc_id)
237 8a35f56c 2022-07-16 thomas continue;
238 8a35f56c 2022-07-16 thomas p = &procs[proc];
241 8a35f56c 2022-07-16 thomas if (p == NULL || p->p_init == NULL)
242 8a35f56c 2022-07-16 thomas fatalx("%s: process %d missing process initialization",
243 8a35f56c 2022-07-16 thomas __func__, proc_id);
245 8a35f56c 2022-07-16 thomas p->p_init(ps, p);
247 8a35f56c 2022-07-16 thomas fatalx("failed to initiate child process");
251 8a35f56c 2022-07-16 thomas proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
252 8a35f56c 2022-07-16 thomas unsigned int n)
254 8a35f56c 2022-07-16 thomas struct privsep_pipes *pp = ps->ps_pp;
255 8a35f56c 2022-07-16 thomas struct imsgev *iev;
257 8a35f56c 2022-07-16 thomas if (ps->ps_ievs[dst] == NULL) {
258 8a35f56c 2022-07-16 thomas #if DEBUG > 1
259 8a35f56c 2022-07-16 thomas log_debug("%s: %s src %d %d to dst %d %d not connected",
260 8a35f56c 2022-07-16 thomas __func__, ps->ps_title[privsep_process],
261 8a35f56c 2022-07-16 thomas privsep_process, ps->ps_instance + 1,
262 8a35f56c 2022-07-16 thomas dst, n + 1);
264 8a35f56c 2022-07-16 thomas close(fd);
268 8a35f56c 2022-07-16 thomas if (pp->pp_pipes[dst][n] != -1) {
269 8a35f56c 2022-07-16 thomas log_warnx("%s: duplicated descriptor", __func__);
270 8a35f56c 2022-07-16 thomas close(fd);
273 8a35f56c 2022-07-16 thomas pp->pp_pipes[dst][n] = fd;
275 8a35f56c 2022-07-16 thomas iev = &ps->ps_ievs[dst][n];
276 8a35f56c 2022-07-16 thomas imsg_init(&iev->ibuf, fd);
277 8a35f56c 2022-07-16 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
278 8a35f56c 2022-07-16 thomas event_add(&iev->ev, NULL);
282 8a35f56c 2022-07-16 thomas proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
284 8a35f56c 2022-07-16 thomas unsigned int i, j, src, dst, id;
285 8a35f56c 2022-07-16 thomas struct privsep_pipes *pp;
287 8a35f56c 2022-07-16 thomas /* Initialize parent title, ps_instances and procs. */
288 8a35f56c 2022-07-16 thomas ps->ps_title[PROC_GOTWEBD] = "parent";
290 8a35f56c 2022-07-16 thomas for (src = 0; src < PROC_MAX; src++)
291 8a35f56c 2022-07-16 thomas /* Default to 1 process instance */
292 8a35f56c 2022-07-16 thomas if (ps->ps_instances[src] < 1)
293 8a35f56c 2022-07-16 thomas ps->ps_instances[src] = 1;
295 8a35f56c 2022-07-16 thomas for (src = 0; src < nproc; src++) {
296 8a35f56c 2022-07-16 thomas procs[src].p_ps = ps;
297 8a35f56c 2022-07-16 thomas if (procs[src].p_cb == NULL)
298 8a35f56c 2022-07-16 thomas procs[src].p_cb = proc_dispatch_null;
300 8a35f56c 2022-07-16 thomas id = procs[src].p_id;
301 8a35f56c 2022-07-16 thomas ps->ps_title[id] = procs[src].p_title;
302 8a35f56c 2022-07-16 thomas ps->ps_ievs[id] = calloc(ps->ps_instances[id],
303 8a35f56c 2022-07-16 thomas sizeof(struct imsgev));
304 8a35f56c 2022-07-16 thomas if (ps->ps_ievs[id] == NULL)
305 8a35f56c 2022-07-16 thomas fatal("%s: calloc", __func__);
307 8a35f56c 2022-07-16 thomas /* With this set up, we are ready to call imsg_init(). */
308 8a35f56c 2022-07-16 thomas for (i = 0; i < ps->ps_instances[id]; i++) {
309 8a35f56c 2022-07-16 thomas ps->ps_ievs[id][i].handler = proc_dispatch;
310 8a35f56c 2022-07-16 thomas ps->ps_ievs[id][i].events = EV_READ;
311 8a35f56c 2022-07-16 thomas ps->ps_ievs[id][i].proc = &procs[src];
312 8a35f56c 2022-07-16 thomas ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
317 8a35f56c 2022-07-16 thomas * Allocate pipes for all process instances (incl. parent)
319 8a35f56c 2022-07-16 thomas * - ps->ps_pipes: N:M mapping
320 8a35f56c 2022-07-16 thomas * N source processes connected to M destination processes:
321 8a35f56c 2022-07-16 thomas * [src][instances][dst][instances], for example
322 8a35f56c 2022-07-16 thomas * [PROC_RELAY][3][PROC_CA][3]
324 8a35f56c 2022-07-16 thomas * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
325 8a35f56c 2022-07-16 thomas * Each process instance has a destination array of socketpair fds:
326 8a35f56c 2022-07-16 thomas * [dst][instances], for example
327 8a35f56c 2022-07-16 thomas * [PROC_GOTWEBD][0]
329 8a35f56c 2022-07-16 thomas for (src = 0; src < PROC_MAX; src++) {
330 8a35f56c 2022-07-16 thomas /* Allocate destination array for each process */
331 8a35f56c 2022-07-16 thomas ps->ps_pipes[src] = calloc(ps->ps_instances[src],
332 8a35f56c 2022-07-16 thomas sizeof(struct privsep_pipes));
333 8a35f56c 2022-07-16 thomas if (ps->ps_pipes[src] == NULL)
334 8a35f56c 2022-07-16 thomas fatal("%s: calloc", __func__);
336 8a35f56c 2022-07-16 thomas for (i = 0; i < ps->ps_instances[src]; i++) {
337 8a35f56c 2022-07-16 thomas pp = &ps->ps_pipes[src][i];
339 8a35f56c 2022-07-16 thomas for (dst = 0; dst < PROC_MAX; dst++) {
340 8a35f56c 2022-07-16 thomas /* Allocate maximum fd integers */
341 8a35f56c 2022-07-16 thomas pp->pp_pipes[dst] =
342 8a35f56c 2022-07-16 thomas calloc(ps->ps_instances[dst],
343 8a35f56c 2022-07-16 thomas sizeof(int));
344 8a35f56c 2022-07-16 thomas if (pp->pp_pipes[dst] == NULL)
345 8a35f56c 2022-07-16 thomas fatal("%s: calloc", __func__);
347 8a35f56c 2022-07-16 thomas /* Mark fd as unused */
348 8a35f56c 2022-07-16 thomas for (j = 0; j < ps->ps_instances[dst]; j++)
349 8a35f56c 2022-07-16 thomas pp->pp_pipes[dst][j] = -1;
354 8a35f56c 2022-07-16 thomas ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
358 8a35f56c 2022-07-16 thomas proc_kill(struct privsep *ps)
360 8a35f56c 2022-07-16 thomas char *cause;
361 8a35f56c 2022-07-16 thomas pid_t pid;
362 8a35f56c 2022-07-16 thomas int len, status;
364 8a35f56c 2022-07-16 thomas if (privsep_process != PROC_GOTWEBD)
367 8a35f56c 2022-07-16 thomas proc_close(ps);
370 8a35f56c 2022-07-16 thomas pid = waitpid(WAIT_ANY, &status, 0);
371 8a35f56c 2022-07-16 thomas if (pid <= 0)
372 8a35f56c 2022-07-16 thomas continue;
374 8a35f56c 2022-07-16 thomas if (WIFSIGNALED(status)) {
375 8a35f56c 2022-07-16 thomas len = asprintf(&cause, "terminated; signal %d",
376 8a35f56c 2022-07-16 thomas WTERMSIG(status));
377 8a35f56c 2022-07-16 thomas } else if (WIFEXITED(status)) {
378 8a35f56c 2022-07-16 thomas if (WEXITSTATUS(status) != 0)
379 8a35f56c 2022-07-16 thomas len = asprintf(&cause, "exited abnormally");
383 8a35f56c 2022-07-16 thomas len = -1;
385 8a35f56c 2022-07-16 thomas if (len == 0) {
386 8a35f56c 2022-07-16 thomas /* child exited OK, don't print a warning message */
387 8a35f56c 2022-07-16 thomas } else if (len != -1) {
388 8a35f56c 2022-07-16 thomas log_warnx("lost child: pid %u %s", pid, cause);
389 8a35f56c 2022-07-16 thomas free(cause);
391 8a35f56c 2022-07-16 thomas log_warnx("lost child: pid %u", pid);
392 8a35f56c 2022-07-16 thomas } while (pid != -1 || (pid == -1 && errno == EINTR));
396 8a35f56c 2022-07-16 thomas proc_open(struct privsep *ps, int src, int dst)
398 8a35f56c 2022-07-16 thomas struct privsep_pipes *pa, *pb;
399 8a35f56c 2022-07-16 thomas struct privsep_fd pf;
400 8a35f56c 2022-07-16 thomas int fds[2];
401 8a35f56c 2022-07-16 thomas unsigned int i, j;
403 8a35f56c 2022-07-16 thomas /* Exchange pipes between process. */
404 8a35f56c 2022-07-16 thomas for (i = 0; i < ps->ps_instances[src]; i++) {
405 8a35f56c 2022-07-16 thomas for (j = 0; j < ps->ps_instances[dst]; j++) {
406 8a35f56c 2022-07-16 thomas /* Don't create sockets for ourself. */
407 8a35f56c 2022-07-16 thomas if (src == dst && i == j)
408 8a35f56c 2022-07-16 thomas continue;
410 8a35f56c 2022-07-16 thomas pa = &ps->ps_pipes[src][i];
411 8a35f56c 2022-07-16 thomas pb = &ps->ps_pipes[dst][j];
412 dd7a7d5b 2022-07-16 thomas int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
413 dd7a7d5b 2022-07-16 thomas #ifdef SOCK_CLOEXEC
414 dd7a7d5b 2022-07-16 thomas sock_flags |= SOCK_CLOEXEC;
416 dd7a7d5b 2022-07-16 thomas if (socketpair(AF_UNIX, sock_flags,
417 8a35f56c 2022-07-16 thomas PF_UNSPEC, fds) == -1)
418 8a35f56c 2022-07-16 thomas fatal("%s: socketpair", __func__);
420 8a35f56c 2022-07-16 thomas pa->pp_pipes[dst][j] = fds[0];
421 8a35f56c 2022-07-16 thomas pb->pp_pipes[src][i] = fds[1];
423 8a35f56c 2022-07-16 thomas pf.pf_procid = src;
424 8a35f56c 2022-07-16 thomas pf.pf_instance = i;
425 8a35f56c 2022-07-16 thomas if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
426 8a35f56c 2022-07-16 thomas -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
427 8a35f56c 2022-07-16 thomas fatal("%s: proc_compose_imsg", __func__);
429 8a35f56c 2022-07-16 thomas pf.pf_procid = dst;
430 8a35f56c 2022-07-16 thomas pf.pf_instance = j;
431 8a35f56c 2022-07-16 thomas if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
432 8a35f56c 2022-07-16 thomas -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
433 8a35f56c 2022-07-16 thomas fatal("%s: proc_compose_imsg", __func__);
436 8a35f56c 2022-07-16 thomas * We have to flush to send the descriptors and close
437 8a35f56c 2022-07-16 thomas * them to avoid the fd ramp on startup.
439 8a35f56c 2022-07-16 thomas if (proc_flush_imsg(ps, src, i) == -1 ||
440 8a35f56c 2022-07-16 thomas proc_flush_imsg(ps, dst, j) == -1)
441 8a35f56c 2022-07-16 thomas fatal("%s: imsg_flush", __func__);
447 8a35f56c 2022-07-16 thomas proc_close(struct privsep *ps)
449 8a35f56c 2022-07-16 thomas unsigned int dst, n;
450 8a35f56c 2022-07-16 thomas struct privsep_pipes *pp;
452 8a35f56c 2022-07-16 thomas if (ps == NULL)
455 8a35f56c 2022-07-16 thomas pp = ps->ps_pp;
457 8a35f56c 2022-07-16 thomas for (dst = 0; dst < PROC_MAX; dst++) {
458 8a35f56c 2022-07-16 thomas if (ps->ps_ievs[dst] == NULL)
459 8a35f56c 2022-07-16 thomas continue;
461 8a35f56c 2022-07-16 thomas for (n = 0; n < ps->ps_instances[dst]; n++) {
462 8a35f56c 2022-07-16 thomas if (pp->pp_pipes[dst][n] == -1)
463 8a35f56c 2022-07-16 thomas continue;
465 8a35f56c 2022-07-16 thomas /* Cancel the fd, close and invalidate the fd */
466 8a35f56c 2022-07-16 thomas event_del(&(ps->ps_ievs[dst][n].ev));
467 8a35f56c 2022-07-16 thomas imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
468 8a35f56c 2022-07-16 thomas close(pp->pp_pipes[dst][n]);
469 8a35f56c 2022-07-16 thomas pp->pp_pipes[dst][n] = -1;
471 8a35f56c 2022-07-16 thomas free(ps->ps_ievs[dst]);
476 8a35f56c 2022-07-16 thomas proc_shutdown(struct privsep_proc *p)
478 8a35f56c 2022-07-16 thomas struct privsep *ps = p->p_ps;
480 8a35f56c 2022-07-16 thomas if (p->p_shutdown != NULL)
481 8a35f56c 2022-07-16 thomas (*p->p_shutdown)();
483 8a35f56c 2022-07-16 thomas proc_close(ps);
485 8a35f56c 2022-07-16 thomas log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
491 8a35f56c 2022-07-16 thomas proc_sig_handler(int sig, short event, void *arg)
493 8a35f56c 2022-07-16 thomas struct privsep_proc *p = arg;
495 8a35f56c 2022-07-16 thomas switch (sig) {
496 8a35f56c 2022-07-16 thomas case SIGINT:
497 8a35f56c 2022-07-16 thomas case SIGTERM:
498 8a35f56c 2022-07-16 thomas proc_shutdown(p);
500 8a35f56c 2022-07-16 thomas case SIGCHLD:
501 8a35f56c 2022-07-16 thomas case SIGHUP:
502 8a35f56c 2022-07-16 thomas case SIGPIPE:
503 8a35f56c 2022-07-16 thomas case SIGUSR1:
504 8a35f56c 2022-07-16 thomas /* ignore */
507 8a35f56c 2022-07-16 thomas fatalx("proc_sig_handler: unexpected signal");
508 8a35f56c 2022-07-16 thomas /* NOTREACHED */
513 8a35f56c 2022-07-16 thomas proc_run(struct privsep *ps, struct privsep_proc *p,
514 8a35f56c 2022-07-16 thomas struct privsep_proc *procs, unsigned int nproc,
515 8a35f56c 2022-07-16 thomas void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
517 8a35f56c 2022-07-16 thomas struct passwd *pw;
518 8a35f56c 2022-07-16 thomas const char *root;
520 8a35f56c 2022-07-16 thomas log_procinit(p->p_title);
522 8a35f56c 2022-07-16 thomas /* Set the process group of the current process */
523 8a35f56c 2022-07-16 thomas setpgid(0, 0);
525 8a35f56c 2022-07-16 thomas /* Use non-standard user */
526 8a35f56c 2022-07-16 thomas if (p->p_pw != NULL)
527 8a35f56c 2022-07-16 thomas pw = p->p_pw;
529 8a35f56c 2022-07-16 thomas pw = ps->ps_pw;
531 8a35f56c 2022-07-16 thomas /* Change root directory */
532 8a35f56c 2022-07-16 thomas if (p->p_chroot != NULL)
533 8a35f56c 2022-07-16 thomas root = p->p_chroot;
535 8a35f56c 2022-07-16 thomas root = pw->pw_dir;
537 8a35f56c 2022-07-16 thomas if (chroot(root) == -1)
538 8a35f56c 2022-07-16 thomas fatal("proc_run: chroot");
539 8a35f56c 2022-07-16 thomas if (chdir("/") == -1)
540 8a35f56c 2022-07-16 thomas fatal("proc_run: chdir(\"/\")");
542 8a35f56c 2022-07-16 thomas privsep_process = p->p_id;
544 8a35f56c 2022-07-16 thomas setproctitle("%s", p->p_title);
546 8a35f56c 2022-07-16 thomas if (setgroups(1, &pw->pw_gid) ||
547 8a35f56c 2022-07-16 thomas setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
548 8a35f56c 2022-07-16 thomas setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
549 8a35f56c 2022-07-16 thomas fatal("proc_run: cannot drop privileges");
551 8a35f56c 2022-07-16 thomas event_init();
553 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
554 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
555 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
556 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
557 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
558 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
560 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigint, NULL);
561 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigterm, NULL);
562 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigchld, NULL);
563 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsighup, NULL);
564 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigpipe, NULL);
565 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigusr1, NULL);
567 8a35f56c 2022-07-16 thomas proc_setup(ps, procs, nproc);
568 8a35f56c 2022-07-16 thomas proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
570 8a35f56c 2022-07-16 thomas DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
571 8a35f56c 2022-07-16 thomas ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
573 8a35f56c 2022-07-16 thomas if (run != NULL)
574 8a35f56c 2022-07-16 thomas run(ps, p, arg);
576 8a35f56c 2022-07-16 thomas event_dispatch();
578 8a35f56c 2022-07-16 thomas proc_shutdown(p);
582 8a35f56c 2022-07-16 thomas proc_dispatch(int fd, short event, void *arg)
584 8a35f56c 2022-07-16 thomas struct imsgev *iev = arg;
585 8a35f56c 2022-07-16 thomas struct privsep_proc *p = iev->proc;
586 8a35f56c 2022-07-16 thomas struct privsep *ps = p->p_ps;
587 8a35f56c 2022-07-16 thomas struct imsgbuf *ibuf;
588 8a35f56c 2022-07-16 thomas struct imsg imsg;
589 8a35f56c 2022-07-16 thomas ssize_t n;
590 8a35f56c 2022-07-16 thomas int verbose;
591 8a35f56c 2022-07-16 thomas const char *title;
592 8a35f56c 2022-07-16 thomas struct privsep_fd pf;
594 8a35f56c 2022-07-16 thomas title = ps->ps_title[privsep_process];
595 8a35f56c 2022-07-16 thomas ibuf = &iev->ibuf;
597 8a35f56c 2022-07-16 thomas if (event & EV_READ) {
598 8a35f56c 2022-07-16 thomas n = imsg_read(ibuf);
599 8a35f56c 2022-07-16 thomas if (n == -1 && errno != EAGAIN)
600 8a35f56c 2022-07-16 thomas fatal("%s: imsg_read", __func__);
601 8a35f56c 2022-07-16 thomas if (n == 0) {
602 8a35f56c 2022-07-16 thomas /* this pipe is dead, so remove the event handler */
603 8a35f56c 2022-07-16 thomas event_del(&iev->ev);
604 8a35f56c 2022-07-16 thomas event_loopexit(NULL);
609 8a35f56c 2022-07-16 thomas if (event & EV_WRITE) {
610 8a35f56c 2022-07-16 thomas n = msgbuf_write(&ibuf->w);
611 8a35f56c 2022-07-16 thomas if (n == -1 && errno != EAGAIN)
612 8a35f56c 2022-07-16 thomas fatal("%s: msgbuf_write", __func__);
613 8a35f56c 2022-07-16 thomas if (n == 0) {
614 8a35f56c 2022-07-16 thomas /* this pipe is dead, so remove the event handler */
615 8a35f56c 2022-07-16 thomas event_del(&iev->ev);
616 8a35f56c 2022-07-16 thomas event_loopexit(NULL);
621 8a35f56c 2022-07-16 thomas for (;;) {
622 8a35f56c 2022-07-16 thomas n = imsg_get(ibuf, &imsg);
623 8a35f56c 2022-07-16 thomas if (n == -1)
624 8a35f56c 2022-07-16 thomas fatal("%s: imsg_get", __func__);
625 8a35f56c 2022-07-16 thomas if (n == 0)
628 8a35f56c 2022-07-16 thomas #if DEBUG > 1
629 8a35f56c 2022-07-16 thomas log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
630 8a35f56c 2022-07-16 thomas __func__, title, ps->ps_instance + 1,
631 8a35f56c 2022-07-16 thomas imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
635 8a35f56c 2022-07-16 thomas * Check the message with the program callback
637 8a35f56c 2022-07-16 thomas if ((p->p_cb)(fd, p, &imsg) == 0) {
638 8a35f56c 2022-07-16 thomas /* Message was handled by the callback, continue */
639 8a35f56c 2022-07-16 thomas imsg_free(&imsg);
640 8a35f56c 2022-07-16 thomas continue;
644 8a35f56c 2022-07-16 thomas * Generic message handling
646 8a35f56c 2022-07-16 thomas switch (imsg.hdr.type) {
647 8a35f56c 2022-07-16 thomas case IMSG_CTL_VERBOSE:
648 8a35f56c 2022-07-16 thomas log_info("%s", __func__);
649 8a35f56c 2022-07-16 thomas IMSG_SIZE_CHECK(&imsg, &verbose);
650 8a35f56c 2022-07-16 thomas memcpy(&verbose, imsg.data, sizeof(verbose));
651 8a35f56c 2022-07-16 thomas log_setverbose(verbose);
653 8a35f56c 2022-07-16 thomas case IMSG_CTL_PROCFD:
654 8a35f56c 2022-07-16 thomas IMSG_SIZE_CHECK(&imsg, &pf);
655 8a35f56c 2022-07-16 thomas memcpy(&pf, imsg.data, sizeof(pf));
656 8a35f56c 2022-07-16 thomas proc_accept(ps, imsg.fd, pf.pf_procid,
657 8a35f56c 2022-07-16 thomas pf.pf_instance);
660 8a35f56c 2022-07-16 thomas log_warnx("%s: %s %d got invalid imsg %d peerid %d "
661 8a35f56c 2022-07-16 thomas "from %s %d",
662 8a35f56c 2022-07-16 thomas __func__, title, ps->ps_instance + 1,
663 8a35f56c 2022-07-16 thomas imsg.hdr.type, imsg.hdr.peerid,
664 8a35f56c 2022-07-16 thomas p->p_title, imsg.hdr.pid);
666 8a35f56c 2022-07-16 thomas imsg_free(&imsg);
668 8a35f56c 2022-07-16 thomas imsg_event_add(iev);
672 8a35f56c 2022-07-16 thomas proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
674 8a35f56c 2022-07-16 thomas return (-1);
678 8a35f56c 2022-07-16 thomas * imsg helper functions
682 8a35f56c 2022-07-16 thomas imsg_event_add(struct imsgev *iev)
684 8a35f56c 2022-07-16 thomas if (iev->handler == NULL) {
685 8a35f56c 2022-07-16 thomas imsg_flush(&iev->ibuf);
689 8a35f56c 2022-07-16 thomas iev->events = EV_READ;
690 8a35f56c 2022-07-16 thomas if (iev->ibuf.w.queued)
691 8a35f56c 2022-07-16 thomas iev->events |= EV_WRITE;
693 8a35f56c 2022-07-16 thomas event_del(&iev->ev);
694 8a35f56c 2022-07-16 thomas event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
695 8a35f56c 2022-07-16 thomas event_add(&iev->ev, NULL);
699 8a35f56c 2022-07-16 thomas imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
700 8a35f56c 2022-07-16 thomas pid_t pid, int fd, void *data, uint16_t datalen)
704 8a35f56c 2022-07-16 thomas ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
705 8a35f56c 2022-07-16 thomas if (ret == -1)
706 8a35f56c 2022-07-16 thomas return (ret);
707 8a35f56c 2022-07-16 thomas imsg_event_add(iev);
708 8a35f56c 2022-07-16 thomas return (ret);
712 8a35f56c 2022-07-16 thomas imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
713 8a35f56c 2022-07-16 thomas pid_t pid, int fd, const struct iovec *iov, int iovcnt)
717 8a35f56c 2022-07-16 thomas ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
718 8a35f56c 2022-07-16 thomas if (ret == -1)
719 8a35f56c 2022-07-16 thomas return (ret);
720 8a35f56c 2022-07-16 thomas imsg_event_add(iev);
721 8a35f56c 2022-07-16 thomas return (ret);
725 8a35f56c 2022-07-16 thomas proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
727 8a35f56c 2022-07-16 thomas if (*n == -1) {
728 8a35f56c 2022-07-16 thomas /* Use a range of all target instances */
730 8a35f56c 2022-07-16 thomas *m = ps->ps_instances[id];
732 8a35f56c 2022-07-16 thomas /* Use only a single slot of the specified peer process */
733 8a35f56c 2022-07-16 thomas *m = *n + 1;
738 8a35f56c 2022-07-16 thomas proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
739 8a35f56c 2022-07-16 thomas uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
743 8a35f56c 2022-07-16 thomas proc_range(ps, id, &n, &m);
744 8a35f56c 2022-07-16 thomas for (; n < m; n++) {
745 8a35f56c 2022-07-16 thomas if (imsg_compose_event(&ps->ps_ievs[id][n],
746 8a35f56c 2022-07-16 thomas type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
747 8a35f56c 2022-07-16 thomas return (-1);
750 8a35f56c 2022-07-16 thomas return (0);
754 8a35f56c 2022-07-16 thomas proc_compose(struct privsep *ps, enum privsep_procid id,
755 8a35f56c 2022-07-16 thomas uint16_t type, void *data, uint16_t datalen)
757 8a35f56c 2022-07-16 thomas return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
761 8a35f56c 2022-07-16 thomas proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
762 8a35f56c 2022-07-16 thomas uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
766 8a35f56c 2022-07-16 thomas proc_range(ps, id, &n, &m);
767 8a35f56c 2022-07-16 thomas for (; n < m; n++)
768 8a35f56c 2022-07-16 thomas if (imsg_composev_event(&ps->ps_ievs[id][n],
769 8a35f56c 2022-07-16 thomas type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
770 8a35f56c 2022-07-16 thomas return (-1);
772 8a35f56c 2022-07-16 thomas return (0);
776 8a35f56c 2022-07-16 thomas proc_composev(struct privsep *ps, enum privsep_procid id,
777 8a35f56c 2022-07-16 thomas uint16_t type, const struct iovec *iov, int iovcnt)
779 8a35f56c 2022-07-16 thomas return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
783 8a35f56c 2022-07-16 thomas proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
784 8a35f56c 2022-07-16 thomas enum privsep_procid id, int n)
786 8a35f56c 2022-07-16 thomas return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
787 8a35f56c 2022-07-16 thomas imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
790 8a35f56c 2022-07-16 thomas struct imsgbuf *
791 8a35f56c 2022-07-16 thomas proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
795 8a35f56c 2022-07-16 thomas proc_range(ps, id, &n, &m);
796 8a35f56c 2022-07-16 thomas return (&ps->ps_ievs[id][n].ibuf);
799 8a35f56c 2022-07-16 thomas struct imsgev *
800 8a35f56c 2022-07-16 thomas proc_iev(struct privsep *ps, enum privsep_procid id, int n)
804 8a35f56c 2022-07-16 thomas proc_range(ps, id, &n, &m);
805 8a35f56c 2022-07-16 thomas return (&ps->ps_ievs[id][n]);
808 8a35f56c 2022-07-16 thomas /* This function should only be called with care as it breaks async I/O */
810 8a35f56c 2022-07-16 thomas proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
812 8a35f56c 2022-07-16 thomas struct imsgbuf *ibuf;
813 8a35f56c 2022-07-16 thomas int m, ret = 0;
815 8a35f56c 2022-07-16 thomas proc_range(ps, id, &n, &m);
816 8a35f56c 2022-07-16 thomas for (; n < m; n++) {
817 8a35f56c 2022-07-16 thomas ibuf = proc_ibuf(ps, id, n);
818 8a35f56c 2022-07-16 thomas if (ibuf == NULL)
819 8a35f56c 2022-07-16 thomas return (-1);
821 8a35f56c 2022-07-16 thomas ret = imsg_flush(ibuf);
822 8a35f56c 2022-07-16 thomas } while (ret == -1 && errno == EAGAIN);
823 8a35f56c 2022-07-16 thomas if (ret == -1)
825 8a35f56c 2022-07-16 thomas imsg_event_add(&ps->ps_ievs[id][n]);
828 8a35f56c 2022-07-16 thomas return (ret);