privsep.c revision 1.1 1 1.1 itojun /* $OpenBSD: privsep.c,v 1.8 2004/03/14 19:17:05 otto Exp $ */
2 1.1 itojun
3 1.1 itojun /*
4 1.1 itojun * Copyright (c) 2003 Can Erkin Acar
5 1.1 itojun * Copyright (c) 2003 Anil Madhavapeddy <anil (at) recoil.org>
6 1.1 itojun *
7 1.1 itojun * Permission to use, copy, modify, and distribute this software for any
8 1.1 itojun * purpose with or without fee is hereby granted, provided that the above
9 1.1 itojun * copyright notice and this permission notice appear in all copies.
10 1.1 itojun *
11 1.1 itojun * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 itojun * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 itojun * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 itojun * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 itojun * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 itojun * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 itojun * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 itojun */
19 1.1 itojun #include <sys/ioctl.h>
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 itojun #include <pcap.h>
32 1.1 itojun #include <pcap-int.h>
33 1.1 itojun #include <pwd.h>
34 1.1 itojun #include <signal.h>
35 1.1 itojun #include <stdio.h>
36 1.1 itojun #include <stdlib.h>
37 1.1 itojun #include <string.h>
38 1.1 itojun #include <syslog.h>
39 1.1 itojun #include <unistd.h>
40 1.1 itojun #include "pflogd.h"
41 1.1 itojun
42 1.1 itojun enum cmd_types {
43 1.1 itojun PRIV_SET_SNAPLEN, /* set the snaplength */
44 1.1 itojun PRIV_OPEN_LOG /* open logfile for appending */
45 1.1 itojun };
46 1.1 itojun
47 1.1 itojun static int priv_fd = -1;
48 1.1 itojun static volatile pid_t child_pid = -1;
49 1.1 itojun
50 1.1 itojun volatile sig_atomic_t gotsig_chld = 0;
51 1.1 itojun
52 1.1 itojun static void sig_pass_to_chld(int);
53 1.1 itojun static void sig_chld(int);
54 1.1 itojun static int may_read(int, void *, size_t);
55 1.1 itojun static void must_read(int, void *, size_t);
56 1.1 itojun static void must_write(int, void *, size_t);
57 1.1 itojun static int set_snaplen(int snap);
58 1.1 itojun
59 1.1 itojun /* bpf filter expression common to parent and child */
60 1.1 itojun extern char *filter;
61 1.1 itojun extern char *errbuf;
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 itojun int snaplen, ret;
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 itojun if (setgroups(1, gidset) == -1)
100 1.1 itojun err(1, "setgroups() failed");
101 1.1 itojun if (setegid(pw->pw_gid) == -1)
102 1.1 itojun err(1, "setegid() failed");
103 1.1 itojun if (setgid(pw->pw_gid) == -1)
104 1.1 itojun err(1, "setgid() failed");
105 1.1 itojun if (seteuid(pw->pw_uid) == -1)
106 1.1 itojun err(1, "seteuid() failed");
107 1.1 itojun if (setuid(pw->pw_uid) == -1)
108 1.1 itojun err(1, "setuid() failed");
109 1.1 itojun close(socks[0]);
110 1.1 itojun priv_fd = socks[1];
111 1.1 itojun return 0;
112 1.1 itojun }
113 1.1 itojun
114 1.1 itojun /* Father */
115 1.1 itojun /* Pass ALRM/TERM/HUP through to child, and accept CHLD */
116 1.1 itojun signal(SIGALRM, sig_pass_to_chld);
117 1.1 itojun signal(SIGTERM, sig_pass_to_chld);
118 1.1 itojun signal(SIGHUP, sig_pass_to_chld);
119 1.1 itojun signal(SIGCHLD, sig_chld);
120 1.1 itojun
121 1.1 itojun setproctitle("[priv]");
122 1.1 itojun close(socks[1]);
123 1.1 itojun
124 1.1 itojun while (!gotsig_chld) {
125 1.1 itojun if (may_read(socks[0], &cmd, sizeof(int)))
126 1.1 itojun break;
127 1.1 itojun switch (cmd) {
128 1.1 itojun case PRIV_SET_SNAPLEN:
129 1.1 itojun logmsg(LOG_DEBUG,
130 1.1 itojun "[priv]: msg PRIV_SET_SNAPLENGTH received");
131 1.1 itojun must_read(socks[0], &snaplen, sizeof(int));
132 1.1 itojun
133 1.1 itojun ret = set_snaplen(snaplen);
134 1.1 itojun if (ret) {
135 1.1 itojun logmsg(LOG_NOTICE,
136 1.1 itojun "[priv]: set_snaplen failed for snaplen %d",
137 1.1 itojun snaplen);
138 1.1 itojun }
139 1.1 itojun
140 1.1 itojun must_write(socks[0], &ret, sizeof(int));
141 1.1 itojun break;
142 1.1 itojun
143 1.1 itojun case PRIV_OPEN_LOG:
144 1.1 itojun logmsg(LOG_DEBUG,
145 1.1 itojun "[priv]: msg PRIV_OPEN_LOG received");
146 1.1 itojun /* create or append logs but do not follow symlinks */
147 1.1 itojun fd = open(filename,
148 1.1 itojun O_RDWR|O_CREAT|O_APPEND|O_NONBLOCK|O_NOFOLLOW,
149 1.1 itojun 0600);
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 itojun filename, strerror(errno));
154 1.1 itojun send_fd(socks[0], fd);
155 1.1 itojun close(fd);
156 1.1 itojun break;
157 1.1 itojun
158 1.1 itojun default:
159 1.1 itojun logmsg(LOG_ERR, "[priv]: unknown command %d", cmd);
160 1.1 itojun _exit(1);
161 1.1 itojun /* NOTREACHED */
162 1.1 itojun }
163 1.1 itojun }
164 1.1 itojun
165 1.1 itojun _exit(1);
166 1.1 itojun }
167 1.1 itojun
168 1.1 itojun /* this is called from parent */
169 1.1 itojun static int
170 1.1 itojun set_snaplen(int snap)
171 1.1 itojun {
172 1.1 itojun if (hpcap == NULL)
173 1.1 itojun return (1);
174 1.1 itojun
175 1.1 itojun hpcap->snapshot = snap;
176 1.1 itojun set_pcap_filter();
177 1.1 itojun
178 1.1 itojun return 0;
179 1.1 itojun }
180 1.1 itojun
181 1.1 itojun
182 1.1 itojun /*
183 1.1 itojun * send the snaplength to privileged process
184 1.1 itojun */
185 1.1 itojun int
186 1.1 itojun priv_set_snaplen(int snaplen)
187 1.1 itojun {
188 1.1 itojun int cmd, ret;
189 1.1 itojun
190 1.1 itojun if (priv_fd < 0)
191 1.1 itojun errx(1, "%s: called from privileged portion", __func__);
192 1.1 itojun
193 1.1 itojun cmd = PRIV_SET_SNAPLEN;
194 1.1 itojun
195 1.1 itojun must_write(priv_fd, &cmd, sizeof(int));
196 1.1 itojun must_write(priv_fd, &snaplen, sizeof(int));
197 1.1 itojun
198 1.1 itojun must_read(priv_fd, &ret, sizeof(int));
199 1.1 itojun
200 1.1 itojun /* also set hpcap->snapshot in child */
201 1.1 itojun if (ret == 0)
202 1.1 itojun hpcap->snapshot = snaplen;
203 1.1 itojun
204 1.1 itojun return (ret);
205 1.1 itojun }
206 1.1 itojun
207 1.1 itojun /* Open log-file */
208 1.1 itojun int
209 1.1 itojun priv_open_log(void)
210 1.1 itojun {
211 1.1 itojun int cmd, fd;
212 1.1 itojun
213 1.1 itojun if (priv_fd < 0)
214 1.1 itojun errx(1, "%s: called from privileged portion\n", __func__);
215 1.1 itojun
216 1.1 itojun cmd = PRIV_OPEN_LOG;
217 1.1 itojun must_write(priv_fd, &cmd, sizeof(int));
218 1.1 itojun fd = receive_fd(priv_fd);
219 1.1 itojun
220 1.1 itojun return (fd);
221 1.1 itojun }
222 1.1 itojun
223 1.1 itojun /* If priv parent gets a TERM or HUP, pass it through to child instead */
224 1.1 itojun static void
225 1.1 itojun sig_pass_to_chld(int sig)
226 1.1 itojun {
227 1.1 itojun int oerrno = errno;
228 1.1 itojun
229 1.1 itojun if (child_pid != -1)
230 1.1 itojun kill(child_pid, sig);
231 1.1 itojun errno = oerrno;
232 1.1 itojun }
233 1.1 itojun
234 1.1 itojun /* if parent gets a SIGCHLD, it will exit */
235 1.1 itojun static void
236 1.1 itojun sig_chld(int sig)
237 1.1 itojun {
238 1.1 itojun gotsig_chld = 1;
239 1.1 itojun }
240 1.1 itojun
241 1.1 itojun /* Read all data or return 1 for error. */
242 1.1 itojun static int
243 1.1 itojun may_read(int fd, void *buf, size_t n)
244 1.1 itojun {
245 1.1 itojun char *s = buf;
246 1.1 itojun ssize_t res, pos = 0;
247 1.1 itojun
248 1.1 itojun while (n > pos) {
249 1.1 itojun res = read(fd, s + pos, n - pos);
250 1.1 itojun switch (res) {
251 1.1 itojun case -1:
252 1.1 itojun if (errno == EINTR || errno == EAGAIN)
253 1.1 itojun continue;
254 1.1 itojun case 0:
255 1.1 itojun return (1);
256 1.1 itojun default:
257 1.1 itojun pos += res;
258 1.1 itojun }
259 1.1 itojun }
260 1.1 itojun return (0);
261 1.1 itojun }
262 1.1 itojun
263 1.1 itojun /* Read data with the assertion that it all must come through, or
264 1.1 itojun * else abort the process. Based on atomicio() from openssh. */
265 1.1 itojun static void
266 1.1 itojun must_read(int fd, void *buf, size_t n)
267 1.1 itojun {
268 1.1 itojun char *s = buf;
269 1.1 itojun ssize_t res, pos = 0;
270 1.1 itojun
271 1.1 itojun while (n > pos) {
272 1.1 itojun res = read(fd, s + pos, n - pos);
273 1.1 itojun switch (res) {
274 1.1 itojun case -1:
275 1.1 itojun if (errno == EINTR || errno == EAGAIN)
276 1.1 itojun continue;
277 1.1 itojun case 0:
278 1.1 itojun _exit(0);
279 1.1 itojun default:
280 1.1 itojun pos += res;
281 1.1 itojun }
282 1.1 itojun }
283 1.1 itojun }
284 1.1 itojun
285 1.1 itojun /* Write data with the assertion that it all has to be written, or
286 1.1 itojun * else abort the process. Based on atomicio() from openssh. */
287 1.1 itojun static void
288 1.1 itojun must_write(int fd, void *buf, size_t n)
289 1.1 itojun {
290 1.1 itojun char *s = buf;
291 1.1 itojun ssize_t res, pos = 0;
292 1.1 itojun
293 1.1 itojun while (n > pos) {
294 1.1 itojun res = write(fd, s + pos, n - pos);
295 1.1 itojun switch (res) {
296 1.1 itojun case -1:
297 1.1 itojun if (errno == EINTR || errno == EAGAIN)
298 1.1 itojun continue;
299 1.1 itojun case 0:
300 1.1 itojun _exit(0);
301 1.1 itojun default:
302 1.1 itojun pos += res;
303 1.1 itojun }
304 1.1 itojun }
305 1.1 itojun }
306