Home | History | Annotate | Line # | Download | only in pflogd
privsep.c revision 1.2
      1 /*	$OpenBSD: privsep.c,v 1.12 2004/07/14 19:07:03 henning Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2003 Can Erkin Acar
      5  * Copyright (c) 2003 Anil Madhavapeddy <anil (at) recoil.org>
      6  *
      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.
     10  *
     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.
     18  */
     19 #include <sys/ioctl.h>
     20 #include <sys/types.h>
     21 #include <sys/time.h>
     22 #include <sys/socket.h>
     23 #include <sys/ioctl.h>
     24 
     25 #include <net/if.h>
     26 #include <net/bpf.h>
     27 
     28 #include <string.h>
     29 
     30 #include <err.h>
     31 #include <errno.h>
     32 #include <fcntl.h>
     33 #include <pcap.h>
     34 #include <pcap-int.h>
     35 #include <pwd.h>
     36 #include <signal.h>
     37 #include <stdio.h>
     38 #include <stdlib.h>
     39 #include <syslog.h>
     40 #include <unistd.h>
     41 #include "pflogd.h"
     42 
     43 enum cmd_types {
     44 	PRIV_SET_SNAPLEN,	/* set the snaplength */
     45 	PRIV_OPEN_LOG		/* open logfile for appending */
     46 };
     47 
     48 static int priv_fd = -1;
     49 static volatile pid_t child_pid = -1;
     50 
     51 volatile sig_atomic_t gotsig_chld = 0;
     52 
     53 static void sig_pass_to_chld(int);
     54 static void sig_chld(int);
     55 static int  may_read(int, void *, size_t);
     56 static void must_read(int, void *, size_t);
     57 static void must_write(int, void *, size_t);
     58 static int  set_snaplen(int snap);
     59 
     60 /* bpf filter expression common to parent and child */
     61 extern char *filter;
     62 extern char *errbuf;
     63 extern char *filename;
     64 extern pcap_t *hpcap;
     65 
     66 /* based on syslogd privsep */
     67 int
     68 priv_init(void)
     69 {
     70 	int i, fd, socks[2], cmd;
     71 	int snaplen, ret, olderrno;
     72 	struct passwd *pw;
     73 
     74 	for (i = 1; i < _NSIG; i++)
     75 		signal(i, SIG_DFL);
     76 
     77 	/* Create sockets */
     78 	if (socketpair(AF_LOCAL, SOCK_STREAM, PF_UNSPEC, socks) == -1)
     79 		err(1, "socketpair() failed");
     80 
     81 	pw = getpwnam("_pflogd");
     82 	if (pw == NULL)
     83 		errx(1, "unknown user _pflogd");
     84 	endpwent();
     85 
     86 	child_pid = fork();
     87 	if (child_pid < 0)
     88 		err(1, "fork() failed");
     89 
     90 	if (!child_pid) {
     91 		gid_t gidset[1];
     92 
     93 		/* Child - drop privileges and return */
     94 		if (chroot(pw->pw_dir) != 0)
     95 			err(1, "unable to chroot");
     96 		if (chdir("/") != 0)
     97 			err(1, "unable to chdir");
     98 
     99 		gidset[0] = pw->pw_gid;
    100 		if (setgroups(1, gidset) == -1)
    101 			err(1, "setgroups() failed");
    102 #ifdef __OpenBSD__
    103 		if (setegid(pw->pw_gid) == -1)
    104 			err(1, "setegid() failed");
    105 #endif
    106 		if (setgid(pw->pw_gid) == -1)
    107 			err(1, "setgid() failed");
    108 #ifdef __OpenBSD__
    109 		if (seteuid(pw->pw_uid) == -1)
    110 			err(1, "seteuid() failed");
    111 #endif
    112 		if (setuid(pw->pw_uid) == -1)
    113 			err(1, "setuid() failed");
    114 		close(socks[0]);
    115 		priv_fd = socks[1];
    116 		return 0;
    117 	}
    118 
    119 	/* Father */
    120 	/* Pass ALRM/TERM/HUP through to child, and accept CHLD */
    121 	signal(SIGALRM, sig_pass_to_chld);
    122 	signal(SIGTERM, sig_pass_to_chld);
    123 	signal(SIGHUP,  sig_pass_to_chld);
    124 	signal(SIGCHLD, sig_chld);
    125 
    126 	setproctitle("[priv]");
    127 	close(socks[1]);
    128 
    129 	while (!gotsig_chld) {
    130 		if (may_read(socks[0], &cmd, sizeof(int)))
    131 			break;
    132 		switch (cmd) {
    133 		case PRIV_SET_SNAPLEN:
    134 			logmsg(LOG_DEBUG,
    135 			    "[priv]: msg PRIV_SET_SNAPLENGTH received");
    136 			must_read(socks[0], &snaplen, sizeof(int));
    137 
    138 			ret = set_snaplen(snaplen);
    139 			if (ret) {
    140 				logmsg(LOG_NOTICE,
    141 				   "[priv]: set_snaplen failed for snaplen %d",
    142 				   snaplen);
    143 			}
    144 
    145 			must_write(socks[0], &ret, sizeof(int));
    146 			break;
    147 
    148 		case PRIV_OPEN_LOG:
    149 			logmsg(LOG_DEBUG,
    150 			    "[priv]: msg PRIV_OPEN_LOG received");
    151 			/* create or append logs but do not follow symlinks */
    152 			fd = open(filename,
    153 			    O_RDWR|O_CREAT|O_APPEND|O_NONBLOCK|O_NOFOLLOW,
    154 			    0600);
    155 			olderrno = errno;
    156 			send_fd(socks[0], fd);
    157 			if (fd < 0)
    158 				logmsg(LOG_NOTICE,
    159 				    "[priv]: failed to open %s: %s",
    160 				    filename, strerror(olderrno));
    161 			else
    162 				close(fd);
    163 			break;
    164 
    165 		default:
    166 			logmsg(LOG_ERR, "[priv]: unknown command %d", cmd);
    167 			_exit(1);
    168 			/* NOTREACHED */
    169 		}
    170 	}
    171 
    172 	_exit(1);
    173 }
    174 
    175 /* this is called from parent */
    176 static int
    177 set_snaplen(int snap)
    178 {
    179 	if (hpcap == NULL)
    180 		return (1);
    181 
    182 	hpcap->snapshot = snap;
    183 	set_pcap_filter();
    184 
    185 	return 0;
    186 }
    187 
    188 
    189 /*
    190  * send the snaplength to privileged process
    191  */
    192 int
    193 priv_set_snaplen(int snaplen)
    194 {
    195 	int cmd, ret;
    196 
    197 	if (priv_fd < 0)
    198 		errx(1, "%s: called from privileged portion", __func__);
    199 
    200 	cmd = PRIV_SET_SNAPLEN;
    201 
    202 	must_write(priv_fd, &cmd, sizeof(int));
    203 	must_write(priv_fd, &snaplen, sizeof(int));
    204 
    205 	must_read(priv_fd, &ret, sizeof(int));
    206 
    207 	/* also set hpcap->snapshot in child */
    208 	if (ret == 0)
    209 		hpcap->snapshot = snaplen;
    210 
    211 	return (ret);
    212 }
    213 
    214 /* Open log-file */
    215 int
    216 priv_open_log(void)
    217 {
    218 	int cmd, fd;
    219 
    220 	if (priv_fd < 0)
    221 		errx(1, "%s: called from privileged portion", __func__);
    222 
    223 	cmd = PRIV_OPEN_LOG;
    224 	must_write(priv_fd, &cmd, sizeof(int));
    225 	fd = receive_fd(priv_fd);
    226 
    227 	return (fd);
    228 }
    229 
    230 /* If priv parent gets a TERM or HUP, pass it through to child instead */
    231 static void
    232 sig_pass_to_chld(int sig)
    233 {
    234 	int oerrno = errno;
    235 
    236 	if (child_pid != -1)
    237 		kill(child_pid, sig);
    238 	errno = oerrno;
    239 }
    240 
    241 /* if parent gets a SIGCHLD, it will exit */
    242 static void
    243 sig_chld(int sig)
    244 {
    245 	gotsig_chld = 1;
    246 }
    247 
    248 /* Read all data or return 1 for error.  */
    249 static int
    250 may_read(int fd, void *buf, size_t n)
    251 {
    252 	char *s = buf;
    253 	ssize_t res, pos = 0;
    254 
    255 	while (n > pos) {
    256 		res = read(fd, s + pos, n - pos);
    257 		switch (res) {
    258 		case -1:
    259 			if (errno == EINTR || errno == EAGAIN)
    260 				continue;
    261 		case 0:
    262 			return (1);
    263 		default:
    264 			pos += res;
    265 		}
    266 	}
    267 	return (0);
    268 }
    269 
    270 /* Read data with the assertion that it all must come through, or
    271  * else abort the process.  Based on atomicio() from openssh. */
    272 static void
    273 must_read(int fd, void *buf, size_t n)
    274 {
    275 	char *s = buf;
    276 	ssize_t res, pos = 0;
    277 
    278 	while (n > pos) {
    279 		res = read(fd, s + pos, n - pos);
    280 		switch (res) {
    281 		case -1:
    282 			if (errno == EINTR || errno == EAGAIN)
    283 				continue;
    284 		case 0:
    285 			_exit(0);
    286 		default:
    287 			pos += res;
    288 		}
    289 	}
    290 }
    291 
    292 /* Write data with the assertion that it all has to be written, or
    293  * else abort the process.  Based on atomicio() from openssh. */
    294 static void
    295 must_write(int fd, void *buf, size_t n)
    296 {
    297 	char *s = buf;
    298 	ssize_t res, pos = 0;
    299 
    300 	while (n > pos) {
    301 		res = write(fd, s + pos, n - pos);
    302 		switch (res) {
    303 		case -1:
    304 			if (errno == EINTR || errno == EAGAIN)
    305 				continue;
    306 		case 0:
    307 			_exit(0);
    308 		default:
    309 			pos += res;
    310 		}
    311 	}
    312 }
    313