1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/* $OpenBSD: proc.c,v 1.10 2013/11/15 12:30:19 mikeb Exp $ */
/*
* Copyright (c) 2010-2013 Reyk Floeter <reyk@openbsd.org>
* Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <pwd.h>
#include <event.h>
#include <openssl/rand.h>
#include "iked.h"
void proc_setup(struct privsep *);
void proc_shutdown(struct privsep_proc *);
void proc_sig_handler(int, short, void *);
void
proc_init(struct privsep *ps, struct privsep_proc *p, u_int nproc)
{
u_int i;
/*
* Called from parent
*/
privsep_process = PROC_PARENT;
ps->ps_title[PROC_PARENT] = "parent";
ps->ps_pid[PROC_PARENT] = getpid();
proc_setup(ps);
/* Engage! */
for (i = 0; i < nproc; i++, p++) {
ps->ps_title[p->p_id] = p->p_title;
ps->ps_pid[p->p_id] = (*p->p_init)(ps, p);
}
}
void
proc_kill(struct privsep *ps)
{
pid_t pid;
u_int i;
if (privsep_process != PROC_PARENT)
return;
for (i = 0; i < PROC_MAX; i++) {
if (ps->ps_pid[i] == 0)
continue;
kill(ps->ps_pid[i], SIGTERM);
}
do {
pid = waitpid(WAIT_ANY, NULL, 0);
} while (pid != -1 || (pid == -1 && errno == EINTR));
}
void
proc_setup(struct privsep *ps)
{
int i, j, sockpair[2];
for (i = 0; i < PROC_MAX; i++)
for (j = 0; j < PROC_MAX; j++) {
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC,
sockpair) == -1)
fatal("sockpair");
ps->ps_pipes[i][j] = sockpair[0];
ps->ps_pipes[j][i] = sockpair[1];
socket_set_blockmode(ps->ps_pipes[i][j],
BM_NONBLOCK);
socket_set_blockmode(ps->ps_pipes[j][i],
BM_NONBLOCK);
}
}
void
proc_config(struct privsep *ps, struct privsep_proc *p, u_int nproc)
{
u_int src, dst, i, j, k, found;
src = privsep_process;
/*
* close unused pipes
*/
for (i = 0; i < PROC_MAX; i++) {
if (i != privsep_process) {
for (j = 0; j < PROC_MAX; j++) {
close(ps->ps_pipes[i][j]);
ps->ps_pipes[i][j] = -1;
}
} else {
for (j = found = 0; j < PROC_MAX; j++, found = 0) {
for (k = 0; k < nproc; k++) {
if (p[k].p_id == j)
found++;
}
if (!found) {
close(ps->ps_pipes[i][j]);
ps->ps_pipes[i][j] = -1;
}
}
}
}
/*
* listen on appropriate pipes
*/
for (i = 0; i < nproc; i++, p++) {
dst = p->p_id;
p->p_ps = ps;
p->p_env = ps->ps_env;
imsg_init(&ps->ps_ievs[dst].ibuf,
ps->ps_pipes[src][dst]);
ps->ps_ievs[dst].handler = proc_dispatch;
ps->ps_ievs[dst].events = EV_READ;
ps->ps_ievs[dst].data = p;
ps->ps_ievs[dst].name = p->p_title;
event_set(&ps->ps_ievs[dst].ev,
ps->ps_ievs[dst].ibuf.fd,
ps->ps_ievs[dst].events,
ps->ps_ievs[dst].handler,
ps->ps_ievs[dst].data);
event_add(&ps->ps_ievs[dst].ev, NULL);
}
}
void
proc_shutdown(struct privsep_proc *p)
{
struct privsep *ps = p->p_ps;
if (p->p_id == PROC_CONTROL && ps)
control_cleanup(&ps->ps_csock);
log_info("%s exiting", p->p_title);
_exit(0);
}
void
proc_sig_handler(int sig, short event, void *arg)
{
struct privsep_proc *p = arg;
switch (sig) {
case SIGINT:
case SIGTERM:
proc_shutdown(p);
break;
case SIGCHLD:
case SIGHUP:
case SIGPIPE:
/* ignore */
break;
default:
fatalx("proc_sig_handler: unexpected signal");
/* NOTREACHED */
}
}
pid_t
proc_run(struct privsep *ps, struct privsep_proc *p,
struct privsep_proc *procs, u_int nproc,
void (*init)(struct privsep *, void *), void *arg)
{
pid_t pid;
struct passwd *pw;
const char *root;
u_int32_t seed[256];
switch (pid = fork()) {
case -1:
fatal("proc_run: cannot fork");
case 0:
break;
default:
return (pid);
}
pw = ps->ps_pw;
if (p->p_id == PROC_CONTROL) {
if (control_init(ps, &ps->ps_csock) == -1)
fatalx(p->p_title);
}
/* Change root directory */
if (p->p_chroot != NULL)
root = p->p_chroot;
else
root = pw->pw_dir;
#ifndef DEBUG
if (chroot(root) == -1)
fatal("proc_run: chroot");
if (chdir("/") == -1)
fatal("proc_run: chdir(\"/\")");
#else
#warning disabling privilege revocation and chroot in DEBUG MODE
if (p->p_chroot != NULL) {
if (chroot(root) == -1)
fatal("proc_run: chroot");
if (chdir("/") == -1)
fatal("proc_run: chdir(\"/\")");
}
#endif
privsep_process = p->p_id;
setproctitle("%s", p->p_title);
#ifndef DEBUG
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
fatal("proc_run: cannot drop privileges");
#endif
event_init();
signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
signal_add(&ps->ps_evsigint, NULL);
signal_add(&ps->ps_evsigterm, NULL);
signal_add(&ps->ps_evsigchld, NULL);
signal_add(&ps->ps_evsighup, NULL);
signal_add(&ps->ps_evsigpipe, NULL);
proc_config(ps, procs, nproc);
arc4random_buf(seed, sizeof(seed));
RAND_seed(seed, sizeof(seed));
if (p->p_id == PROC_CONTROL) {
TAILQ_INIT(&ctl_conns);
if (control_listen(&ps->ps_csock) == -1)
fatalx(p->p_title);
}
if (init != NULL)
init(ps, arg);
event_dispatch();
proc_shutdown(p);
return (0);
}
void
proc_dispatch(int fd, short event, void *arg)
{
struct privsep_proc *p = (struct privsep_proc *)arg;
struct privsep *ps = p->p_ps;
struct imsgev *iev;
struct imsgbuf *ibuf;
struct imsg imsg;
ssize_t n;
int verbose;
const char *title;
title = ps->ps_title[privsep_process];
iev = &ps->ps_ievs[p->p_id];
ibuf = &iev->ibuf;
if (event & EV_READ) {
if ((n = imsg_read(ibuf)) == -1)
fatal(title);
if (n == 0) {
/* this pipe is dead, so remove the event handler */
event_del(&iev->ev);
event_loopexit(NULL);
return;
}
}
if (event & EV_WRITE) {
if (msgbuf_write(&ibuf->w) <= 0 && errno != EAGAIN)
fatal(title);
}
for (;;) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatal(title);
if (n == 0)
break;
/*
* Check the message with the program callback
*/
if ((p->p_cb)(fd, p, &imsg) == 0) {
/* Message was handled by the callback, continue */
imsg_free(&imsg);
continue;
}
/*
* Generic message handling
*/
switch (imsg.hdr.type) {
case IMSG_CTL_VERBOSE:
IMSG_SIZE_CHECK(&imsg, &verbose);
memcpy(&verbose, imsg.data, sizeof(verbose));
log_verbose(verbose);
break;
default:
log_warnx("%s: %s got imsg %d", __func__, p->p_title,
imsg.hdr.type);
fatalx(title);
}
imsg_free(&imsg);
}
imsg_event_add(iev);
}
void
imsg_event_add(struct imsgev *iev)
{
if (iev->handler == NULL) {
imsg_flush(&iev->ibuf);
return;
}
iev->events = EV_READ;
if (iev->ibuf.w.queued)
iev->events |= EV_WRITE;
event_del(&iev->ev);
event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
event_add(&iev->ev, NULL);
}
int
imsg_compose_event(struct imsgev *iev, u_int16_t type, u_int32_t peerid,
pid_t pid, int fd, void *data, u_int16_t datalen)
{
int ret;
if ((ret = imsg_compose(&iev->ibuf, type, peerid,
pid, fd, data, datalen)) == -1)
return (ret);
imsg_event_add(iev);
return (ret);
}
int
imsg_composev_event(struct imsgev *iev, u_int16_t type, u_int32_t peerid,
pid_t pid, int fd, const struct iovec *iov, int iovcnt)
{
int ret;
if ((ret = imsg_composev(&iev->ibuf, type, peerid,
pid, fd, iov, iovcnt)) == -1)
return (ret);
imsg_event_add(iev);
return (ret);
}
int
proc_compose_imsg(struct iked *env, enum privsep_procid id,
u_int16_t type, int fd, void *data, u_int16_t datalen)
{
return (imsg_compose_event(&env->sc_ps.ps_ievs[id],
type, -1, 0, fd, data, datalen));
}
int
proc_composev_imsg(struct iked *env, enum privsep_procid id,
u_int16_t type, int fd, const struct iovec *iov, int iovcnt)
{
return (imsg_composev_event(&env->sc_ps.ps_ievs[id],
type, -1, 0, fd, iov, iovcnt));
}
int
proc_forward_imsg(struct iked *env, struct imsg *imsg,
enum privsep_procid id)
{
return (proc_compose_imsg(env, id, imsg->hdr.type,
imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
}
void
proc_flush_imsg(struct iked *env, enum privsep_procid id)
{
imsg_flush(&env->sc_ps.ps_ievs[id].ibuf);
}
|