1 /* $OpenBSD: privsep.c,v 1.16 2006/10/25 20:55:04 moritz Exp $ */
4 * Copyright (c) 2003 Can Erkin Acar
5 * Copyright (c) 2003 Anil Madhavapeddy <anil@recoil.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/cdefs.h>
21 __FBSDID("$FreeBSD$");
23 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <sys/ioctl.h>
47 PRIV_SET_SNAPLEN, /* set the snaplength */
48 PRIV_MOVE_LOG, /* move logfile away */
49 PRIV_OPEN_LOG /* open logfile for appending */
52 static int priv_fd = -1;
53 static volatile pid_t child_pid = -1;
55 volatile sig_atomic_t gotsig_chld = 0;
57 static void sig_pass_to_chld(int);
58 static void sig_chld(int);
59 static int may_read(int, void *, size_t);
60 static void must_read(int, void *, size_t);
61 static void must_write(int, void *, size_t);
62 static int set_snaplen(int snap);
63 static int move_log(const char *name);
65 extern char *filename;
68 /* based on syslogd privsep */
72 int i, fd, socks[2], cmd;
73 int snaplen, ret, olderrno;
77 for (i = 1; i < NSIG; i++)
79 for (i = 1; i < _NSIG; i++)
84 if (socketpair(AF_LOCAL, SOCK_STREAM, PF_UNSPEC, socks) == -1)
85 err(1, "socketpair() failed");
87 pw = getpwnam("_pflogd");
89 errx(1, "unknown user _pflogd");
94 err(1, "fork() failed");
99 /* Child - drop privileges and return */
100 if (chroot(pw->pw_dir) != 0)
101 err(1, "unable to chroot");
103 err(1, "unable to chdir");
105 gidset[0] = pw->pw_gid;
106 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1)
107 err(1, "setresgid() failed");
108 if (setgroups(1, gidset) == -1)
109 err(1, "setgroups() failed");
110 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
111 err(1, "setresuid() failed");
118 /* Pass ALRM/TERM/HUP/INT/QUIT through to child, and accept CHLD */
119 signal(SIGALRM, sig_pass_to_chld);
120 signal(SIGTERM, sig_pass_to_chld);
121 signal(SIGHUP, sig_pass_to_chld);
122 signal(SIGINT, sig_pass_to_chld);
123 signal(SIGQUIT, sig_pass_to_chld);
124 signal(SIGCHLD, sig_chld);
126 setproctitle("[priv]");
129 while (!gotsig_chld) {
130 if (may_read(socks[0], &cmd, sizeof(int)))
133 case PRIV_SET_SNAPLEN:
135 "[priv]: msg PRIV_SET_SNAPLENGTH received");
136 must_read(socks[0], &snaplen, sizeof(int));
138 ret = set_snaplen(snaplen);
141 "[priv]: set_snaplen failed for snaplen %d",
145 must_write(socks[0], &ret, sizeof(int));
150 "[priv]: msg PRIV_OPEN_LOG received");
151 /* create or append logs but do not follow symlinks */
153 O_RDWR|O_CREAT|O_APPEND|O_NONBLOCK|O_NOFOLLOW,
156 send_fd(socks[0], fd);
159 "[priv]: failed to open %s: %s",
160 filename, strerror(olderrno));
167 "[priv]: msg PRIV_MOVE_LOG received");
168 ret = move_log(filename);
169 must_write(socks[0], &ret, sizeof(int));
173 logmsg(LOG_ERR, "[priv]: unknown command %d", cmd);
182 /* this is called from parent */
184 set_snaplen(int snap)
189 hpcap->snapshot = snap;
196 move_log(const char *name)
204 len = snprintf(ren, sizeof(ren), "%s.bad.%08x",
206 if (len >= sizeof(ren)) {
207 logmsg(LOG_ERR, "[priv] new name too long");
211 /* lock destinanion */
212 fd = open(ren, O_CREAT|O_EXCL, 0);
217 /* if file exists, try another name */
218 if (errno != EEXIST && errno != EINTR) {
219 logmsg(LOG_ERR, "[priv] failed to create new name: %s",
225 if (rename(name, ren)) {
226 logmsg(LOG_ERR, "[priv] failed to rename %s to %s: %s",
227 name, ren, strerror(errno));
232 "[priv]: log file %s moved to %s", name, ren);
238 * send the snaplength to privileged process
241 priv_set_snaplen(int snaplen)
246 errx(1, "%s: called from privileged portion", __func__);
248 cmd = PRIV_SET_SNAPLEN;
250 must_write(priv_fd, &cmd, sizeof(int));
251 must_write(priv_fd, &snaplen, sizeof(int));
253 must_read(priv_fd, &ret, sizeof(int));
255 /* also set hpcap->snapshot in child */
257 hpcap->snapshot = snaplen;
269 errx(1, "%s: called from privileged portion", __func__);
272 must_write(priv_fd, &cmd, sizeof(int));
273 fd = receive_fd(priv_fd);
277 /* Move-away and reopen log-file */
284 errx(1, "%s: called from privileged portion\n", __func__);
287 must_write(priv_fd, &cmd, sizeof(int));
288 must_read(priv_fd, &ret, sizeof(int));
293 /* If priv parent gets a TERM or HUP, pass it through to child instead */
295 sig_pass_to_chld(int sig)
300 kill(child_pid, sig);
304 /* if parent gets a SIGCHLD, it will exit */
311 /* Read all data or return 1 for error. */
313 may_read(int fd, void *buf, size_t n)
316 ssize_t res, pos = 0;
319 res = read(fd, s + pos, n - pos);
322 if (errno == EINTR || errno == EAGAIN)
333 /* Read data with the assertion that it all must come through, or
334 * else abort the process. Based on atomicio() from openssh. */
336 must_read(int fd, void *buf, size_t n)
339 ssize_t res, pos = 0;
342 res = read(fd, s + pos, n - pos);
345 if (errno == EINTR || errno == EAGAIN)
355 /* Write data with the assertion that it all has to be written, or
356 * else abort the process. Based on atomicio() from openssh. */
358 must_write(int fd, void *buf, size_t n)
361 ssize_t res, pos = 0;
364 res = write(fd, s + pos, n - pos);
367 if (errno == EINTR || errno == EAGAIN)