privsep.c revision 1.1.1.3 1 1.1.1.3 peter /* $OpenBSD: privsep.c,v 1.13 2004/12/22 09:21:02 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.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 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.1.3 peter /* Pass ALRM/TERM/HUP/INT/QUIT 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.1.3 peter signal(SIGINT, sig_pass_to_chld);
120 1.1.1.3 peter signal(SIGQUIT, sig_pass_to_chld);
121 1.1 itojun signal(SIGCHLD, sig_chld);
122 1.1 itojun
123 1.1 itojun setproctitle("[priv]");
124 1.1 itojun close(socks[1]);
125 1.1 itojun
126 1.1 itojun while (!gotsig_chld) {
127 1.1 itojun if (may_read(socks[0], &cmd, sizeof(int)))
128 1.1 itojun break;
129 1.1 itojun switch (cmd) {
130 1.1 itojun case PRIV_SET_SNAPLEN:
131 1.1 itojun logmsg(LOG_DEBUG,
132 1.1 itojun "[priv]: msg PRIV_SET_SNAPLENGTH received");
133 1.1 itojun must_read(socks[0], &snaplen, sizeof(int));
134 1.1 itojun
135 1.1 itojun ret = set_snaplen(snaplen);
136 1.1 itojun if (ret) {
137 1.1 itojun logmsg(LOG_NOTICE,
138 1.1 itojun "[priv]: set_snaplen failed for snaplen %d",
139 1.1 itojun snaplen);
140 1.1 itojun }
141 1.1 itojun
142 1.1 itojun must_write(socks[0], &ret, sizeof(int));
143 1.1 itojun break;
144 1.1 itojun
145 1.1 itojun case PRIV_OPEN_LOG:
146 1.1 itojun logmsg(LOG_DEBUG,
147 1.1 itojun "[priv]: msg PRIV_OPEN_LOG received");
148 1.1 itojun /* create or append logs but do not follow symlinks */
149 1.1 itojun fd = open(filename,
150 1.1 itojun O_RDWR|O_CREAT|O_APPEND|O_NONBLOCK|O_NOFOLLOW,
151 1.1 itojun 0600);
152 1.1.1.2 yamt olderrno = errno;
153 1.1.1.2 yamt send_fd(socks[0], fd);
154 1.1 itojun if (fd < 0)
155 1.1 itojun logmsg(LOG_NOTICE,
156 1.1 itojun "[priv]: failed to open %s: %s",
157 1.1.1.2 yamt filename, strerror(olderrno));
158 1.1.1.2 yamt else
159 1.1.1.2 yamt close(fd);
160 1.1 itojun break;
161 1.1 itojun
162 1.1 itojun default:
163 1.1 itojun logmsg(LOG_ERR, "[priv]: unknown command %d", cmd);
164 1.1 itojun _exit(1);
165 1.1 itojun /* NOTREACHED */
166 1.1 itojun }
167 1.1 itojun }
168 1.1 itojun
169 1.1 itojun _exit(1);
170 1.1 itojun }
171 1.1 itojun
172 1.1 itojun /* this is called from parent */
173 1.1 itojun static int
174 1.1 itojun set_snaplen(int snap)
175 1.1 itojun {
176 1.1 itojun if (hpcap == NULL)
177 1.1 itojun return (1);
178 1.1 itojun
179 1.1 itojun hpcap->snapshot = snap;
180 1.1 itojun set_pcap_filter();
181 1.1 itojun
182 1.1 itojun return 0;
183 1.1 itojun }
184 1.1 itojun
185 1.1 itojun
186 1.1 itojun /*
187 1.1 itojun * send the snaplength to privileged process
188 1.1 itojun */
189 1.1 itojun int
190 1.1 itojun priv_set_snaplen(int snaplen)
191 1.1 itojun {
192 1.1 itojun int cmd, ret;
193 1.1 itojun
194 1.1 itojun if (priv_fd < 0)
195 1.1 itojun errx(1, "%s: called from privileged portion", __func__);
196 1.1 itojun
197 1.1 itojun cmd = PRIV_SET_SNAPLEN;
198 1.1 itojun
199 1.1 itojun must_write(priv_fd, &cmd, sizeof(int));
200 1.1 itojun must_write(priv_fd, &snaplen, sizeof(int));
201 1.1 itojun
202 1.1 itojun must_read(priv_fd, &ret, sizeof(int));
203 1.1 itojun
204 1.1 itojun /* also set hpcap->snapshot in child */
205 1.1 itojun if (ret == 0)
206 1.1 itojun hpcap->snapshot = snaplen;
207 1.1 itojun
208 1.1 itojun return (ret);
209 1.1 itojun }
210 1.1 itojun
211 1.1 itojun /* Open log-file */
212 1.1 itojun int
213 1.1 itojun priv_open_log(void)
214 1.1 itojun {
215 1.1 itojun int cmd, fd;
216 1.1 itojun
217 1.1 itojun if (priv_fd < 0)
218 1.1.1.2 yamt errx(1, "%s: called from privileged portion", __func__);
219 1.1 itojun
220 1.1 itojun cmd = PRIV_OPEN_LOG;
221 1.1 itojun must_write(priv_fd, &cmd, sizeof(int));
222 1.1 itojun fd = receive_fd(priv_fd);
223 1.1 itojun
224 1.1 itojun return (fd);
225 1.1 itojun }
226 1.1 itojun
227 1.1 itojun /* If priv parent gets a TERM or HUP, pass it through to child instead */
228 1.1 itojun static void
229 1.1 itojun sig_pass_to_chld(int sig)
230 1.1 itojun {
231 1.1 itojun int oerrno = errno;
232 1.1 itojun
233 1.1 itojun if (child_pid != -1)
234 1.1 itojun kill(child_pid, sig);
235 1.1 itojun errno = oerrno;
236 1.1 itojun }
237 1.1 itojun
238 1.1 itojun /* if parent gets a SIGCHLD, it will exit */
239 1.1 itojun static void
240 1.1 itojun sig_chld(int sig)
241 1.1 itojun {
242 1.1 itojun gotsig_chld = 1;
243 1.1 itojun }
244 1.1 itojun
245 1.1 itojun /* Read all data or return 1 for error. */
246 1.1 itojun static int
247 1.1 itojun may_read(int fd, void *buf, size_t n)
248 1.1 itojun {
249 1.1 itojun char *s = buf;
250 1.1 itojun ssize_t res, pos = 0;
251 1.1 itojun
252 1.1 itojun while (n > pos) {
253 1.1 itojun res = read(fd, s + pos, n - pos);
254 1.1 itojun switch (res) {
255 1.1 itojun case -1:
256 1.1 itojun if (errno == EINTR || errno == EAGAIN)
257 1.1 itojun continue;
258 1.1 itojun case 0:
259 1.1 itojun return (1);
260 1.1 itojun default:
261 1.1 itojun pos += res;
262 1.1 itojun }
263 1.1 itojun }
264 1.1 itojun return (0);
265 1.1 itojun }
266 1.1 itojun
267 1.1 itojun /* Read data with the assertion that it all must come through, or
268 1.1 itojun * else abort the process. Based on atomicio() from openssh. */
269 1.1 itojun static void
270 1.1 itojun must_read(int fd, void *buf, size_t n)
271 1.1 itojun {
272 1.1 itojun char *s = buf;
273 1.1 itojun ssize_t res, pos = 0;
274 1.1 itojun
275 1.1 itojun while (n > pos) {
276 1.1 itojun res = read(fd, s + pos, n - pos);
277 1.1 itojun switch (res) {
278 1.1 itojun case -1:
279 1.1 itojun if (errno == EINTR || errno == EAGAIN)
280 1.1 itojun continue;
281 1.1 itojun case 0:
282 1.1 itojun _exit(0);
283 1.1 itojun default:
284 1.1 itojun pos += res;
285 1.1 itojun }
286 1.1 itojun }
287 1.1 itojun }
288 1.1 itojun
289 1.1 itojun /* Write data with the assertion that it all has to be written, or
290 1.1 itojun * else abort the process. Based on atomicio() from openssh. */
291 1.1 itojun static void
292 1.1 itojun must_write(int fd, void *buf, size_t n)
293 1.1 itojun {
294 1.1 itojun char *s = buf;
295 1.1 itojun ssize_t res, pos = 0;
296 1.1 itojun
297 1.1 itojun while (n > pos) {
298 1.1 itojun res = write(fd, s + pos, n - pos);
299 1.1 itojun switch (res) {
300 1.1 itojun case -1:
301 1.1 itojun if (errno == EINTR || errno == EAGAIN)
302 1.1 itojun continue;
303 1.1 itojun case 0:
304 1.1 itojun _exit(0);
305 1.1 itojun default:
306 1.1 itojun pos += res;
307 1.1 itojun }
308 1.1 itojun }
309 1.1 itojun }
310