privsep.c revision 1.1.1.4 1 1.1.1.4 martti /* $NetBSD: privsep.c,v 1.1.1.4 2009/12/01 07:03:05 martti Exp $ */
2 1.1.1.4 martti /* $OpenBSD: privsep.c,v 1.16 2006/10/25 20:55:04 moritz Exp $ */
3 1.1 itojun
4 1.1 itojun /*
5 1.1 itojun * Copyright (c) 2003 Can Erkin Acar
6 1.1 itojun * Copyright (c) 2003 Anil Madhavapeddy <anil (at) recoil.org>
7 1.1 itojun *
8 1.1 itojun * Permission to use, copy, modify, and distribute this software for any
9 1.1 itojun * purpose with or without fee is hereby granted, provided that the above
10 1.1 itojun * copyright notice and this permission notice appear in all copies.
11 1.1 itojun *
12 1.1 itojun * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 1.1 itojun * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 1.1 itojun * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 1.1 itojun * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 1.1 itojun * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 1.1 itojun * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 1.1 itojun * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 1.1 itojun */
20 1.1 itojun #include <sys/types.h>
21 1.1 itojun #include <sys/time.h>
22 1.1 itojun #include <sys/socket.h>
23 1.1 itojun #include <sys/ioctl.h>
24 1.1 itojun
25 1.1 itojun #include <net/if.h>
26 1.1 itojun #include <net/bpf.h>
27 1.1 itojun
28 1.1 itojun #include <err.h>
29 1.1 itojun #include <errno.h>
30 1.1 itojun #include <fcntl.h>
31 1.1.1.4 martti #include <limits.h>
32 1.1 itojun #include <pcap.h>
33 1.1 itojun #include <pcap-int.h>
34 1.1 itojun #include <pwd.h>
35 1.1 itojun #include <signal.h>
36 1.1 itojun #include <stdio.h>
37 1.1 itojun #include <stdlib.h>
38 1.1 itojun #include <string.h>
39 1.1 itojun #include <syslog.h>
40 1.1 itojun #include <unistd.h>
41 1.1 itojun #include "pflogd.h"
42 1.1 itojun
43 1.1 itojun enum cmd_types {
44 1.1 itojun PRIV_SET_SNAPLEN, /* set the snaplength */
45 1.1.1.4 martti PRIV_MOVE_LOG, /* move logfile away */
46 1.1 itojun PRIV_OPEN_LOG /* open logfile for appending */
47 1.1 itojun };
48 1.1 itojun
49 1.1 itojun static int priv_fd = -1;
50 1.1 itojun static volatile pid_t child_pid = -1;
51 1.1 itojun
52 1.1 itojun volatile sig_atomic_t gotsig_chld = 0;
53 1.1 itojun
54 1.1 itojun static void sig_pass_to_chld(int);
55 1.1 itojun static void sig_chld(int);
56 1.1 itojun static int may_read(int, void *, size_t);
57 1.1 itojun static void must_read(int, void *, size_t);
58 1.1 itojun static void must_write(int, void *, size_t);
59 1.1 itojun static int set_snaplen(int snap);
60 1.1.1.4 martti static int move_log(const char *name);
61 1.1 itojun
62 1.1 itojun extern char *filename;
63 1.1 itojun extern pcap_t *hpcap;
64 1.1 itojun
65 1.1 itojun /* based on syslogd privsep */
66 1.1 itojun int
67 1.1 itojun priv_init(void)
68 1.1 itojun {
69 1.1 itojun int i, fd, socks[2], cmd;
70 1.1.1.2 yamt int snaplen, ret, olderrno;
71 1.1 itojun struct passwd *pw;
72 1.1 itojun
73 1.1 itojun for (i = 1; i < _NSIG; i++)
74 1.1 itojun signal(i, SIG_DFL);
75 1.1 itojun
76 1.1 itojun /* Create sockets */
77 1.1 itojun if (socketpair(AF_LOCAL, SOCK_STREAM, PF_UNSPEC, socks) == -1)
78 1.1 itojun err(1, "socketpair() failed");
79 1.1 itojun
80 1.1 itojun pw = getpwnam("_pflogd");
81 1.1 itojun if (pw == NULL)
82 1.1 itojun errx(1, "unknown user _pflogd");
83 1.1 itojun endpwent();
84 1.1 itojun
85 1.1 itojun child_pid = fork();
86 1.1 itojun if (child_pid < 0)
87 1.1 itojun err(1, "fork() failed");
88 1.1 itojun
89 1.1 itojun if (!child_pid) {
90 1.1 itojun gid_t gidset[1];
91 1.1 itojun
92 1.1 itojun /* Child - drop privileges and return */
93 1.1 itojun if (chroot(pw->pw_dir) != 0)
94 1.1 itojun err(1, "unable to chroot");
95 1.1 itojun if (chdir("/") != 0)
96 1.1 itojun err(1, "unable to chdir");
97 1.1 itojun
98 1.1 itojun gidset[0] = pw->pw_gid;
99 1.1.1.4 martti if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1)
100 1.1.1.4 martti err(1, "setresgid() failed");
101 1.1 itojun if (setgroups(1, gidset) == -1)
102 1.1 itojun err(1, "setgroups() failed");
103 1.1.1.4 martti if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
104 1.1.1.4 martti err(1, "setresuid() failed");
105 1.1 itojun close(socks[0]);
106 1.1 itojun priv_fd = socks[1];
107 1.1 itojun return 0;
108 1.1 itojun }
109 1.1 itojun
110 1.1 itojun /* Father */
111 1.1.1.3 peter /* Pass ALRM/TERM/HUP/INT/QUIT through to child, and accept CHLD */
112 1.1 itojun signal(SIGALRM, sig_pass_to_chld);
113 1.1 itojun signal(SIGTERM, sig_pass_to_chld);
114 1.1 itojun signal(SIGHUP, sig_pass_to_chld);
115 1.1.1.3 peter signal(SIGINT, sig_pass_to_chld);
116 1.1.1.3 peter signal(SIGQUIT, sig_pass_to_chld);
117 1.1 itojun signal(SIGCHLD, sig_chld);
118 1.1 itojun
119 1.1 itojun setproctitle("[priv]");
120 1.1 itojun close(socks[1]);
121 1.1 itojun
122 1.1 itojun while (!gotsig_chld) {
123 1.1 itojun if (may_read(socks[0], &cmd, sizeof(int)))
124 1.1 itojun break;
125 1.1 itojun switch (cmd) {
126 1.1 itojun case PRIV_SET_SNAPLEN:
127 1.1 itojun logmsg(LOG_DEBUG,
128 1.1 itojun "[priv]: msg PRIV_SET_SNAPLENGTH received");
129 1.1 itojun must_read(socks[0], &snaplen, sizeof(int));
130 1.1 itojun
131 1.1 itojun ret = set_snaplen(snaplen);
132 1.1 itojun if (ret) {
133 1.1 itojun logmsg(LOG_NOTICE,
134 1.1 itojun "[priv]: set_snaplen failed for snaplen %d",
135 1.1 itojun snaplen);
136 1.1 itojun }
137 1.1 itojun
138 1.1 itojun must_write(socks[0], &ret, sizeof(int));
139 1.1 itojun break;
140 1.1 itojun
141 1.1 itojun case PRIV_OPEN_LOG:
142 1.1 itojun logmsg(LOG_DEBUG,
143 1.1 itojun "[priv]: msg PRIV_OPEN_LOG received");
144 1.1 itojun /* create or append logs but do not follow symlinks */
145 1.1 itojun fd = open(filename,
146 1.1 itojun O_RDWR|O_CREAT|O_APPEND|O_NONBLOCK|O_NOFOLLOW,
147 1.1 itojun 0600);
148 1.1.1.2 yamt olderrno = errno;
149 1.1.1.2 yamt send_fd(socks[0], fd);
150 1.1 itojun if (fd < 0)
151 1.1 itojun logmsg(LOG_NOTICE,
152 1.1 itojun "[priv]: failed to open %s: %s",
153 1.1.1.2 yamt filename, strerror(olderrno));
154 1.1.1.2 yamt else
155 1.1.1.2 yamt close(fd);
156 1.1 itojun break;
157 1.1 itojun
158 1.1.1.4 martti case PRIV_MOVE_LOG:
159 1.1.1.4 martti logmsg(LOG_DEBUG,
160 1.1.1.4 martti "[priv]: msg PRIV_MOVE_LOG received");
161 1.1.1.4 martti ret = move_log(filename);
162 1.1.1.4 martti must_write(socks[0], &ret, sizeof(int));
163 1.1.1.4 martti break;
164 1.1.1.4 martti
165 1.1 itojun default:
166 1.1 itojun logmsg(LOG_ERR, "[priv]: unknown command %d", cmd);
167 1.1 itojun _exit(1);
168 1.1 itojun /* NOTREACHED */
169 1.1 itojun }
170 1.1 itojun }
171 1.1 itojun
172 1.1 itojun _exit(1);
173 1.1 itojun }
174 1.1 itojun
175 1.1 itojun /* this is called from parent */
176 1.1 itojun static int
177 1.1 itojun set_snaplen(int snap)
178 1.1 itojun {
179 1.1 itojun if (hpcap == NULL)
180 1.1 itojun return (1);
181 1.1 itojun
182 1.1 itojun hpcap->snapshot = snap;
183 1.1 itojun set_pcap_filter();
184 1.1 itojun
185 1.1 itojun return 0;
186 1.1 itojun }
187 1.1 itojun
188 1.1.1.4 martti static int
189 1.1.1.4 martti move_log(const char *name)
190 1.1.1.4 martti {
191 1.1.1.4 martti char ren[PATH_MAX];
192 1.1.1.4 martti int len;
193 1.1.1.4 martti
194 1.1.1.4 martti for (;;) {
195 1.1.1.4 martti int fd;
196 1.1.1.4 martti
197 1.1.1.4 martti len = snprintf(ren, sizeof(ren), "%s.bad.%08x",
198 1.1.1.4 martti name, arc4random());
199 1.1.1.4 martti if (len >= sizeof(ren)) {
200 1.1.1.4 martti logmsg(LOG_ERR, "[priv] new name too long");
201 1.1.1.4 martti return (1);
202 1.1.1.4 martti }
203 1.1.1.4 martti
204 1.1.1.4 martti /* lock destinanion */
205 1.1.1.4 martti fd = open(ren, O_CREAT|O_EXCL, 0);
206 1.1.1.4 martti if (fd >= 0) {
207 1.1.1.4 martti close(fd);
208 1.1.1.4 martti break;
209 1.1.1.4 martti }
210 1.1.1.4 martti /* if file exists, try another name */
211 1.1.1.4 martti if (errno != EEXIST && errno != EINTR) {
212 1.1.1.4 martti logmsg(LOG_ERR, "[priv] failed to create new name: %s",
213 1.1.1.4 martti strerror(errno));
214 1.1.1.4 martti return (1);
215 1.1.1.4 martti }
216 1.1.1.4 martti }
217 1.1.1.4 martti
218 1.1.1.4 martti if (rename(name, ren)) {
219 1.1.1.4 martti logmsg(LOG_ERR, "[priv] failed to rename %s to %s: %s",
220 1.1.1.4 martti name, ren, strerror(errno));
221 1.1.1.4 martti return (1);
222 1.1.1.4 martti }
223 1.1.1.4 martti
224 1.1.1.4 martti logmsg(LOG_NOTICE,
225 1.1.1.4 martti "[priv]: log file %s moved to %s", name, ren);
226 1.1.1.4 martti
227 1.1.1.4 martti return (0);
228 1.1.1.4 martti }
229 1.1 itojun
230 1.1 itojun /*
231 1.1 itojun * send the snaplength to privileged process
232 1.1 itojun */
233 1.1 itojun int
234 1.1 itojun priv_set_snaplen(int snaplen)
235 1.1 itojun {
236 1.1 itojun int cmd, ret;
237 1.1 itojun
238 1.1 itojun if (priv_fd < 0)
239 1.1 itojun errx(1, "%s: called from privileged portion", __func__);
240 1.1 itojun
241 1.1 itojun cmd = PRIV_SET_SNAPLEN;
242 1.1 itojun
243 1.1 itojun must_write(priv_fd, &cmd, sizeof(int));
244 1.1 itojun must_write(priv_fd, &snaplen, sizeof(int));
245 1.1 itojun
246 1.1 itojun must_read(priv_fd, &ret, sizeof(int));
247 1.1 itojun
248 1.1 itojun /* also set hpcap->snapshot in child */
249 1.1 itojun if (ret == 0)
250 1.1 itojun hpcap->snapshot = snaplen;
251 1.1 itojun
252 1.1 itojun return (ret);
253 1.1 itojun }
254 1.1 itojun
255 1.1 itojun /* Open log-file */
256 1.1 itojun int
257 1.1 itojun priv_open_log(void)
258 1.1 itojun {
259 1.1 itojun int cmd, fd;
260 1.1 itojun
261 1.1 itojun if (priv_fd < 0)
262 1.1.1.2 yamt errx(1, "%s: called from privileged portion", __func__);
263 1.1 itojun
264 1.1 itojun cmd = PRIV_OPEN_LOG;
265 1.1 itojun must_write(priv_fd, &cmd, sizeof(int));
266 1.1 itojun fd = receive_fd(priv_fd);
267 1.1 itojun
268 1.1 itojun return (fd);
269 1.1 itojun }
270 1.1.1.4 martti /* Move-away and reopen log-file */
271 1.1.1.4 martti int
272 1.1.1.4 martti priv_move_log(void)
273 1.1.1.4 martti {
274 1.1.1.4 martti int cmd, ret;
275 1.1.1.4 martti
276 1.1.1.4 martti if (priv_fd < 0)
277 1.1.1.4 martti errx(1, "%s: called from privileged portion\n", __func__);
278 1.1.1.4 martti
279 1.1.1.4 martti cmd = PRIV_MOVE_LOG;
280 1.1.1.4 martti must_write(priv_fd, &cmd, sizeof(int));
281 1.1.1.4 martti must_read(priv_fd, &ret, sizeof(int));
282 1.1.1.4 martti
283 1.1.1.4 martti return (ret);
284 1.1.1.4 martti }
285 1.1 itojun
286 1.1 itojun /* If priv parent gets a TERM or HUP, pass it through to child instead */
287 1.1 itojun static void
288 1.1 itojun sig_pass_to_chld(int sig)
289 1.1 itojun {
290 1.1 itojun int oerrno = errno;
291 1.1 itojun
292 1.1 itojun if (child_pid != -1)
293 1.1 itojun kill(child_pid, sig);
294 1.1 itojun errno = oerrno;
295 1.1 itojun }
296 1.1 itojun
297 1.1 itojun /* if parent gets a SIGCHLD, it will exit */
298 1.1 itojun static void
299 1.1 itojun sig_chld(int sig)
300 1.1 itojun {
301 1.1 itojun gotsig_chld = 1;
302 1.1 itojun }
303 1.1 itojun
304 1.1 itojun /* Read all data or return 1 for error. */
305 1.1 itojun static int
306 1.1 itojun may_read(int fd, void *buf, size_t n)
307 1.1 itojun {
308 1.1 itojun char *s = buf;
309 1.1 itojun ssize_t res, pos = 0;
310 1.1 itojun
311 1.1 itojun while (n > pos) {
312 1.1 itojun res = read(fd, s + pos, n - pos);
313 1.1 itojun switch (res) {
314 1.1 itojun case -1:
315 1.1 itojun if (errno == EINTR || errno == EAGAIN)
316 1.1 itojun continue;
317 1.1 itojun case 0:
318 1.1 itojun return (1);
319 1.1 itojun default:
320 1.1 itojun pos += res;
321 1.1 itojun }
322 1.1 itojun }
323 1.1 itojun return (0);
324 1.1 itojun }
325 1.1 itojun
326 1.1 itojun /* Read data with the assertion that it all must come through, or
327 1.1 itojun * else abort the process. Based on atomicio() from openssh. */
328 1.1 itojun static void
329 1.1 itojun must_read(int fd, void *buf, size_t n)
330 1.1 itojun {
331 1.1 itojun char *s = buf;
332 1.1 itojun ssize_t res, pos = 0;
333 1.1 itojun
334 1.1 itojun while (n > pos) {
335 1.1 itojun res = read(fd, s + pos, n - pos);
336 1.1 itojun switch (res) {
337 1.1 itojun case -1:
338 1.1 itojun if (errno == EINTR || errno == EAGAIN)
339 1.1 itojun continue;
340 1.1 itojun case 0:
341 1.1 itojun _exit(0);
342 1.1 itojun default:
343 1.1 itojun pos += res;
344 1.1 itojun }
345 1.1 itojun }
346 1.1 itojun }
347 1.1 itojun
348 1.1 itojun /* Write data with the assertion that it all has to be written, or
349 1.1 itojun * else abort the process. Based on atomicio() from openssh. */
350 1.1 itojun static void
351 1.1 itojun must_write(int fd, void *buf, size_t n)
352 1.1 itojun {
353 1.1 itojun char *s = buf;
354 1.1 itojun ssize_t res, pos = 0;
355 1.1 itojun
356 1.1 itojun while (n > pos) {
357 1.1 itojun res = write(fd, s + pos, n - pos);
358 1.1 itojun switch (res) {
359 1.1 itojun case -1:
360 1.1 itojun if (errno == EINTR || errno == EAGAIN)
361 1.1 itojun continue;
362 1.1 itojun case 0:
363 1.1 itojun _exit(0);
364 1.1 itojun default:
365 1.1 itojun pos += res;
366 1.1 itojun }
367 1.1 itojun }
368 1.1 itojun }
369