Home | History | Annotate | Line # | Download | only in pflogd
privsep.c revision 1.5.6.1
      1  1.5.6.1  wrstuden /*	$NetBSD: privsep.c,v 1.5.6.1 2008/06/23 04:28:53 wrstuden Exp $	*/
      2  1.5.6.1  wrstuden /*	$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.2     peter #include <string.h>
     29      1.2     peter 
     30      1.1    itojun #include <err.h>
     31      1.1    itojun #include <errno.h>
     32      1.1    itojun #include <fcntl.h>
     33  1.5.6.1  wrstuden #include <limits.h>
     34      1.1    itojun #include <pcap.h>
     35      1.4       tls /*
     36      1.4       tls  * If we're going to include parts of the libpcap internals we MUST
     37      1.4       tls  * set the feature-test macros they expect, or they may misbehave.
     38      1.4       tls  */
     39      1.4       tls #define HAVE_STRLCPY
     40      1.4       tls #define HAVE_SNPRINTF
     41      1.4       tls #define HAVE_VSNPRINTF
     42      1.1    itojun #include <pcap-int.h>
     43      1.1    itojun #include <pwd.h>
     44      1.1    itojun #include <signal.h>
     45      1.1    itojun #include <stdio.h>
     46      1.1    itojun #include <stdlib.h>
     47      1.1    itojun #include <syslog.h>
     48      1.1    itojun #include <unistd.h>
     49      1.1    itojun #include "pflogd.h"
     50      1.1    itojun 
     51      1.1    itojun enum cmd_types {
     52      1.1    itojun 	PRIV_SET_SNAPLEN,	/* set the snaplength */
     53  1.5.6.1  wrstuden 	PRIV_MOVE_LOG,		/* move logfile away */
     54      1.1    itojun 	PRIV_OPEN_LOG		/* open logfile for appending */
     55      1.1    itojun };
     56      1.1    itojun 
     57      1.1    itojun static int priv_fd = -1;
     58      1.1    itojun static volatile pid_t child_pid = -1;
     59      1.1    itojun 
     60      1.1    itojun volatile sig_atomic_t gotsig_chld = 0;
     61      1.1    itojun 
     62      1.1    itojun static void sig_pass_to_chld(int);
     63      1.1    itojun static void sig_chld(int);
     64      1.1    itojun static int  may_read(int, void *, size_t);
     65      1.1    itojun static void must_read(int, void *, size_t);
     66      1.1    itojun static void must_write(int, void *, size_t);
     67      1.1    itojun static int  set_snaplen(int snap);
     68  1.5.6.1  wrstuden static int  move_log(const char *name);
     69      1.1    itojun 
     70      1.1    itojun extern char *filename;
     71      1.1    itojun extern pcap_t *hpcap;
     72      1.1    itojun 
     73      1.1    itojun /* based on syslogd privsep */
     74      1.1    itojun int
     75      1.1    itojun priv_init(void)
     76      1.1    itojun {
     77      1.1    itojun 	int i, fd, socks[2], cmd;
     78      1.2     peter 	int snaplen, ret, olderrno;
     79      1.1    itojun 	struct passwd *pw;
     80      1.1    itojun 
     81      1.1    itojun 	for (i = 1; i < _NSIG; i++)
     82      1.1    itojun 		signal(i, SIG_DFL);
     83      1.1    itojun 
     84      1.1    itojun 	/* Create sockets */
     85      1.1    itojun 	if (socketpair(AF_LOCAL, SOCK_STREAM, PF_UNSPEC, socks) == -1)
     86      1.1    itojun 		err(1, "socketpair() failed");
     87      1.1    itojun 
     88      1.1    itojun 	pw = getpwnam("_pflogd");
     89      1.1    itojun 	if (pw == NULL)
     90      1.1    itojun 		errx(1, "unknown user _pflogd");
     91      1.1    itojun 	endpwent();
     92      1.1    itojun 
     93      1.1    itojun 	child_pid = fork();
     94      1.1    itojun 	if (child_pid < 0)
     95      1.1    itojun 		err(1, "fork() failed");
     96      1.1    itojun 
     97      1.1    itojun 	if (!child_pid) {
     98      1.1    itojun 		gid_t gidset[1];
     99      1.1    itojun 
    100      1.1    itojun 		/* Child - drop privileges and return */
    101      1.1    itojun 		if (chroot(pw->pw_dir) != 0)
    102      1.1    itojun 			err(1, "unable to chroot");
    103      1.1    itojun 		if (chdir("/") != 0)
    104      1.1    itojun 			err(1, "unable to chdir");
    105      1.1    itojun 
    106      1.1    itojun 		gidset[0] = pw->pw_gid;
    107      1.2     peter #ifdef __OpenBSD__
    108  1.5.6.1  wrstuden 		if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1)
    109  1.5.6.1  wrstuden 			err(1, "setresgid() failed");
    110  1.5.6.1  wrstuden #else
    111      1.1    itojun 		if (setgid(pw->pw_gid) == -1)
    112      1.1    itojun 			err(1, "setgid() failed");
    113      1.2     peter #endif
    114  1.5.6.1  wrstuden 		if (setgroups(1, gidset) == -1)
    115  1.5.6.1  wrstuden 			err(1, "setgroups() failed");
    116  1.5.6.1  wrstuden #ifdef __OpenBSD__
    117  1.5.6.1  wrstuden 		if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
    118  1.5.6.1  wrstuden 			err(1, "setresuid() failed");
    119  1.5.6.1  wrstuden #else
    120      1.1    itojun 		if (setuid(pw->pw_uid) == -1)
    121      1.1    itojun 			err(1, "setuid() failed");
    122  1.5.6.1  wrstuden #endif
    123      1.1    itojun 		close(socks[0]);
    124      1.1    itojun 		priv_fd = socks[1];
    125      1.1    itojun 		return 0;
    126      1.1    itojun 	}
    127      1.1    itojun 
    128      1.1    itojun 	/* Father */
    129      1.3     peter 	/* Pass ALRM/TERM/HUP/INT/QUIT through to child, and accept CHLD */
    130      1.1    itojun 	signal(SIGALRM, sig_pass_to_chld);
    131      1.1    itojun 	signal(SIGTERM, sig_pass_to_chld);
    132      1.1    itojun 	signal(SIGHUP,  sig_pass_to_chld);
    133      1.3     peter 	signal(SIGINT,  sig_pass_to_chld);
    134      1.3     peter 	signal(SIGQUIT,  sig_pass_to_chld);
    135      1.1    itojun 	signal(SIGCHLD, sig_chld);
    136      1.1    itojun 
    137      1.1    itojun 	setproctitle("[priv]");
    138      1.1    itojun 	close(socks[1]);
    139      1.1    itojun 
    140      1.1    itojun 	while (!gotsig_chld) {
    141      1.1    itojun 		if (may_read(socks[0], &cmd, sizeof(int)))
    142      1.1    itojun 			break;
    143      1.1    itojun 		switch (cmd) {
    144      1.1    itojun 		case PRIV_SET_SNAPLEN:
    145      1.1    itojun 			logmsg(LOG_DEBUG,
    146      1.1    itojun 			    "[priv]: msg PRIV_SET_SNAPLENGTH received");
    147      1.1    itojun 			must_read(socks[0], &snaplen, sizeof(int));
    148      1.1    itojun 
    149      1.1    itojun 			ret = set_snaplen(snaplen);
    150      1.1    itojun 			if (ret) {
    151      1.1    itojun 				logmsg(LOG_NOTICE,
    152      1.1    itojun 				   "[priv]: set_snaplen failed for snaplen %d",
    153      1.1    itojun 				   snaplen);
    154      1.1    itojun 			}
    155      1.1    itojun 
    156      1.1    itojun 			must_write(socks[0], &ret, sizeof(int));
    157      1.1    itojun 			break;
    158      1.1    itojun 
    159      1.1    itojun 		case PRIV_OPEN_LOG:
    160      1.1    itojun 			logmsg(LOG_DEBUG,
    161      1.1    itojun 			    "[priv]: msg PRIV_OPEN_LOG received");
    162      1.1    itojun 			/* create or append logs but do not follow symlinks */
    163      1.1    itojun 			fd = open(filename,
    164      1.1    itojun 			    O_RDWR|O_CREAT|O_APPEND|O_NONBLOCK|O_NOFOLLOW,
    165      1.1    itojun 			    0600);
    166      1.2     peter 			olderrno = errno;
    167      1.2     peter 			send_fd(socks[0], fd);
    168      1.1    itojun 			if (fd < 0)
    169      1.1    itojun 				logmsg(LOG_NOTICE,
    170      1.1    itojun 				    "[priv]: failed to open %s: %s",
    171      1.2     peter 				    filename, strerror(olderrno));
    172      1.2     peter 			else
    173      1.2     peter 				close(fd);
    174      1.1    itojun 			break;
    175      1.1    itojun 
    176  1.5.6.1  wrstuden 		case PRIV_MOVE_LOG:
    177  1.5.6.1  wrstuden 			logmsg(LOG_DEBUG,
    178  1.5.6.1  wrstuden 			    "[priv]: msg PRIV_MOVE_LOG received");
    179  1.5.6.1  wrstuden 			ret = move_log(filename);
    180  1.5.6.1  wrstuden 			must_write(socks[0], &ret, sizeof(int));
    181  1.5.6.1  wrstuden 			break;
    182  1.5.6.1  wrstuden 
    183      1.1    itojun 		default:
    184      1.1    itojun 			logmsg(LOG_ERR, "[priv]: unknown command %d", cmd);
    185      1.1    itojun 			_exit(1);
    186      1.1    itojun 			/* NOTREACHED */
    187      1.1    itojun 		}
    188      1.1    itojun 	}
    189      1.1    itojun 
    190      1.1    itojun 	_exit(1);
    191      1.1    itojun }
    192      1.1    itojun 
    193      1.1    itojun /* this is called from parent */
    194      1.1    itojun static int
    195      1.1    itojun set_snaplen(int snap)
    196      1.1    itojun {
    197      1.1    itojun 	if (hpcap == NULL)
    198      1.1    itojun 		return (1);
    199      1.1    itojun 
    200      1.1    itojun 	hpcap->snapshot = snap;
    201      1.1    itojun 	set_pcap_filter();
    202      1.1    itojun 
    203      1.1    itojun 	return 0;
    204      1.1    itojun }
    205      1.1    itojun 
    206  1.5.6.1  wrstuden static int
    207  1.5.6.1  wrstuden move_log(const char *name)
    208  1.5.6.1  wrstuden {
    209  1.5.6.1  wrstuden 	char ren[PATH_MAX];
    210  1.5.6.1  wrstuden 	int len;
    211  1.5.6.1  wrstuden 
    212  1.5.6.1  wrstuden 	for (;;) {
    213  1.5.6.1  wrstuden 		int fd;
    214  1.5.6.1  wrstuden 
    215  1.5.6.1  wrstuden 		len = snprintf(ren, sizeof(ren), "%s.bad.%08x",
    216  1.5.6.1  wrstuden 		    name, arc4random());
    217  1.5.6.1  wrstuden 		if (len >= sizeof(ren)) {
    218  1.5.6.1  wrstuden 			logmsg(LOG_ERR, "[priv] new name too long");
    219  1.5.6.1  wrstuden 			return (1);
    220  1.5.6.1  wrstuden 		}
    221  1.5.6.1  wrstuden 
    222  1.5.6.1  wrstuden 		/* lock destinanion */
    223  1.5.6.1  wrstuden 		fd = open(ren, O_CREAT|O_EXCL, 0);
    224  1.5.6.1  wrstuden 		if (fd >= 0) {
    225  1.5.6.1  wrstuden 			close(fd);
    226  1.5.6.1  wrstuden 			break;
    227  1.5.6.1  wrstuden 		}
    228  1.5.6.1  wrstuden 		/* if file exists, try another name */
    229  1.5.6.1  wrstuden 		if (errno != EEXIST && errno != EINTR) {
    230  1.5.6.1  wrstuden 			logmsg(LOG_ERR, "[priv] failed to create new name: %s",
    231  1.5.6.1  wrstuden 			    strerror(errno));
    232  1.5.6.1  wrstuden 			return (1);
    233  1.5.6.1  wrstuden 		}
    234  1.5.6.1  wrstuden 	}
    235  1.5.6.1  wrstuden 
    236  1.5.6.1  wrstuden 	if (rename(name, ren)) {
    237  1.5.6.1  wrstuden 		logmsg(LOG_ERR, "[priv] failed to rename %s to %s: %s",
    238  1.5.6.1  wrstuden 		    name, ren, strerror(errno));
    239  1.5.6.1  wrstuden 		return (1);
    240  1.5.6.1  wrstuden 	}
    241  1.5.6.1  wrstuden 
    242  1.5.6.1  wrstuden 	logmsg(LOG_NOTICE,
    243  1.5.6.1  wrstuden 	       "[priv]: log file %s moved to %s", name, ren);
    244  1.5.6.1  wrstuden 
    245  1.5.6.1  wrstuden 	return (0);
    246  1.5.6.1  wrstuden }
    247      1.1    itojun 
    248      1.1    itojun /*
    249      1.1    itojun  * send the snaplength to privileged process
    250      1.1    itojun  */
    251      1.1    itojun int
    252      1.1    itojun priv_set_snaplen(int snaplen)
    253      1.1    itojun {
    254      1.1    itojun 	int cmd, ret;
    255      1.1    itojun 
    256      1.1    itojun 	if (priv_fd < 0)
    257      1.1    itojun 		errx(1, "%s: called from privileged portion", __func__);
    258      1.1    itojun 
    259      1.1    itojun 	cmd = PRIV_SET_SNAPLEN;
    260      1.1    itojun 
    261      1.1    itojun 	must_write(priv_fd, &cmd, sizeof(int));
    262      1.1    itojun 	must_write(priv_fd, &snaplen, sizeof(int));
    263      1.1    itojun 
    264      1.1    itojun 	must_read(priv_fd, &ret, sizeof(int));
    265      1.1    itojun 
    266      1.1    itojun 	/* also set hpcap->snapshot in child */
    267      1.1    itojun 	if (ret == 0)
    268      1.1    itojun 		hpcap->snapshot = snaplen;
    269      1.1    itojun 
    270      1.1    itojun 	return (ret);
    271      1.1    itojun }
    272      1.1    itojun 
    273      1.1    itojun /* Open log-file */
    274      1.1    itojun int
    275      1.1    itojun priv_open_log(void)
    276      1.1    itojun {
    277      1.1    itojun 	int cmd, fd;
    278      1.1    itojun 
    279      1.1    itojun 	if (priv_fd < 0)
    280      1.2     peter 		errx(1, "%s: called from privileged portion", __func__);
    281      1.1    itojun 
    282      1.1    itojun 	cmd = PRIV_OPEN_LOG;
    283      1.1    itojun 	must_write(priv_fd, &cmd, sizeof(int));
    284      1.1    itojun 	fd = receive_fd(priv_fd);
    285      1.1    itojun 
    286      1.1    itojun 	return (fd);
    287      1.1    itojun }
    288  1.5.6.1  wrstuden /* Move-away and reopen log-file */
    289  1.5.6.1  wrstuden int
    290  1.5.6.1  wrstuden priv_move_log(void)
    291  1.5.6.1  wrstuden {
    292  1.5.6.1  wrstuden 	int cmd, ret;
    293  1.5.6.1  wrstuden 
    294  1.5.6.1  wrstuden 	if (priv_fd < 0)
    295  1.5.6.1  wrstuden 		errx(1, "%s: called from privileged portion\n", __func__);
    296  1.5.6.1  wrstuden 
    297  1.5.6.1  wrstuden 	cmd = PRIV_MOVE_LOG;
    298  1.5.6.1  wrstuden 	must_write(priv_fd, &cmd, sizeof(int));
    299  1.5.6.1  wrstuden 	must_read(priv_fd, &ret, sizeof(int));
    300  1.5.6.1  wrstuden 
    301  1.5.6.1  wrstuden 	return (ret);
    302  1.5.6.1  wrstuden }
    303      1.1    itojun 
    304      1.1    itojun /* If priv parent gets a TERM or HUP, pass it through to child instead */
    305      1.1    itojun static void
    306      1.1    itojun sig_pass_to_chld(int sig)
    307      1.1    itojun {
    308      1.1    itojun 	int oerrno = errno;
    309      1.1    itojun 
    310      1.1    itojun 	if (child_pid != -1)
    311      1.1    itojun 		kill(child_pid, sig);
    312      1.1    itojun 	errno = oerrno;
    313      1.1    itojun }
    314      1.1    itojun 
    315      1.1    itojun /* if parent gets a SIGCHLD, it will exit */
    316      1.1    itojun static void
    317      1.1    itojun sig_chld(int sig)
    318      1.1    itojun {
    319      1.1    itojun 	gotsig_chld = 1;
    320      1.1    itojun }
    321      1.1    itojun 
    322      1.1    itojun /* Read all data or return 1 for error.  */
    323      1.1    itojun static int
    324      1.1    itojun may_read(int fd, void *buf, size_t n)
    325      1.1    itojun {
    326      1.1    itojun 	char *s = buf;
    327      1.1    itojun 	ssize_t res, pos = 0;
    328      1.1    itojun 
    329      1.1    itojun 	while (n > pos) {
    330      1.1    itojun 		res = read(fd, s + pos, n - pos);
    331      1.1    itojun 		switch (res) {
    332      1.1    itojun 		case -1:
    333      1.1    itojun 			if (errno == EINTR || errno == EAGAIN)
    334      1.1    itojun 				continue;
    335      1.1    itojun 		case 0:
    336      1.1    itojun 			return (1);
    337      1.1    itojun 		default:
    338      1.1    itojun 			pos += res;
    339      1.1    itojun 		}
    340      1.1    itojun 	}
    341      1.1    itojun 	return (0);
    342      1.1    itojun }
    343      1.1    itojun 
    344      1.1    itojun /* Read data with the assertion that it all must come through, or
    345      1.1    itojun  * else abort the process.  Based on atomicio() from openssh. */
    346      1.1    itojun static void
    347      1.1    itojun must_read(int fd, void *buf, size_t n)
    348      1.1    itojun {
    349      1.1    itojun 	char *s = buf;
    350      1.1    itojun 	ssize_t res, pos = 0;
    351      1.1    itojun 
    352      1.1    itojun 	while (n > pos) {
    353      1.1    itojun 		res = read(fd, s + pos, n - pos);
    354      1.1    itojun 		switch (res) {
    355      1.1    itojun 		case -1:
    356      1.1    itojun 			if (errno == EINTR || errno == EAGAIN)
    357      1.1    itojun 				continue;
    358      1.1    itojun 		case 0:
    359      1.1    itojun 			_exit(0);
    360      1.1    itojun 		default:
    361      1.1    itojun 			pos += res;
    362      1.1    itojun 		}
    363      1.1    itojun 	}
    364      1.1    itojun }
    365      1.1    itojun 
    366      1.1    itojun /* Write data with the assertion that it all has to be written, or
    367      1.1    itojun  * else abort the process.  Based on atomicio() from openssh. */
    368      1.1    itojun static void
    369      1.1    itojun must_write(int fd, void *buf, size_t n)
    370      1.1    itojun {
    371      1.1    itojun 	char *s = buf;
    372      1.1    itojun 	ssize_t res, pos = 0;
    373      1.1    itojun 
    374      1.1    itojun 	while (n > pos) {
    375      1.1    itojun 		res = write(fd, s + pos, n - pos);
    376      1.1    itojun 		switch (res) {
    377      1.1    itojun 		case -1:
    378      1.1    itojun 			if (errno == EINTR || errno == EAGAIN)
    379      1.1    itojun 				continue;
    380      1.1    itojun 		case 0:
    381      1.1    itojun 			_exit(0);
    382      1.1    itojun 		default:
    383      1.1    itojun 			pos += res;
    384      1.1    itojun 		}
    385      1.1    itojun 	}
    386      1.1    itojun }
    387