privsep.c revision 1.7.48.1       1  1.7.48.1  christos /*	$NetBSD: privsep.c,v 1.7.48.1 2019/06/10 21:42:15 christos Exp $	*/
      2       1.6      yamt /*	$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.6      yamt #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.6      yamt 	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.7  christos static int  set_snaplen(uint32_t);
     68       1.7  christos static int  move_log(const char *);
     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.6      yamt 		if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1)
    109       1.6      yamt 			err(1, "setresgid() failed");
    110       1.6      yamt #else
    111       1.1    itojun 		if (setgid(pw->pw_gid) == -1)
    112       1.1    itojun 			err(1, "setgid() failed");
    113       1.6      yamt #endif
    114       1.6      yamt 		if (setgroups(1, gidset) == -1)
    115       1.6      yamt 			err(1, "setgroups() failed");
    116       1.2     peter #ifdef __OpenBSD__
    117       1.6      yamt 		if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
    118       1.6      yamt 			err(1, "setresuid() failed");
    119       1.6      yamt #else
    120       1.1    itojun 		if (setuid(pw->pw_uid) == -1)
    121       1.1    itojun 			err(1, "setuid() failed");
    122       1.6      yamt #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.6      yamt 		case PRIV_MOVE_LOG:
    177       1.6      yamt 			logmsg(LOG_DEBUG,
    178       1.6      yamt 			    "[priv]: msg PRIV_MOVE_LOG received");
    179       1.6      yamt 			ret = move_log(filename);
    180       1.6      yamt 			must_write(socks[0], &ret, sizeof(int));
    181       1.6      yamt 			break;
    182       1.6      yamt 
    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.7  christos set_snaplen(uint32_t 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.6      yamt static int
    207       1.6      yamt move_log(const char *name)
    208       1.6      yamt {
    209       1.6      yamt 	char ren[PATH_MAX];
    210       1.6      yamt 	int len;
    211       1.6      yamt 
    212       1.6      yamt 	for (;;) {
    213       1.6      yamt 		int fd;
    214       1.6      yamt 
    215       1.6      yamt 		len = snprintf(ren, sizeof(ren), "%s.bad.%08x",
    216       1.6      yamt 		    name, arc4random());
    217       1.7  christos 		if ((size_t)len >= sizeof(ren)) {
    218       1.6      yamt 			logmsg(LOG_ERR, "[priv] new name too long");
    219       1.6      yamt 			return (1);
    220       1.6      yamt 		}
    221       1.6      yamt 
    222       1.6      yamt 		/* lock destinanion */
    223       1.6      yamt 		fd = open(ren, O_CREAT|O_EXCL, 0);
    224       1.6      yamt 		if (fd >= 0) {
    225       1.6      yamt 			close(fd);
    226       1.6      yamt 			break;
    227       1.6      yamt 		}
    228       1.6      yamt 		/* if file exists, try another name */
    229       1.6      yamt 		if (errno != EEXIST && errno != EINTR) {
    230       1.6      yamt 			logmsg(LOG_ERR, "[priv] failed to create new name: %s",
    231       1.6      yamt 			    strerror(errno));
    232       1.6      yamt 			return (1);
    233       1.6      yamt 		}
    234       1.6      yamt 	}
    235       1.6      yamt 
    236       1.6      yamt 	if (rename(name, ren)) {
    237       1.6      yamt 		logmsg(LOG_ERR, "[priv] failed to rename %s to %s: %s",
    238       1.6      yamt 		    name, ren, strerror(errno));
    239       1.6      yamt 		return (1);
    240       1.6      yamt 	}
    241       1.6      yamt 
    242       1.6      yamt 	logmsg(LOG_NOTICE,
    243       1.6      yamt 	       "[priv]: log file %s moved to %s", name, ren);
    244       1.6      yamt 
    245       1.6      yamt 	return (0);
    246       1.6      yamt }
    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.6      yamt /* Move-away and reopen log-file */
    289       1.6      yamt int
    290       1.6      yamt priv_move_log(void)
    291       1.6      yamt {
    292       1.6      yamt 	int cmd, ret;
    293       1.6      yamt 
    294       1.6      yamt 	if (priv_fd < 0)
    295       1.6      yamt 		errx(1, "%s: called from privileged portion\n", __func__);
    296       1.6      yamt 
    297       1.6      yamt 	cmd = PRIV_MOVE_LOG;
    298       1.6      yamt 	must_write(priv_fd, &cmd, sizeof(int));
    299       1.6      yamt 	must_read(priv_fd, &ret, sizeof(int));
    300       1.6      yamt 
    301       1.6      yamt 	return (ret);
    302       1.6      yamt }
    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.7  christos 	while (n > (size_t)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.7.48.1  christos 			/* FALLTHROUGH */
    336       1.1    itojun 		case 0:
    337       1.1    itojun 			return (1);
    338       1.1    itojun 		default:
    339       1.1    itojun 			pos += res;
    340       1.1    itojun 		}
    341       1.1    itojun 	}
    342       1.1    itojun 	return (0);
    343       1.1    itojun }
    344       1.1    itojun 
    345       1.1    itojun /* Read data with the assertion that it all must come through, or
    346       1.1    itojun  * else abort the process.  Based on atomicio() from openssh. */
    347       1.1    itojun static void
    348       1.1    itojun must_read(int fd, void *buf, size_t n)
    349       1.1    itojun {
    350       1.1    itojun 	char *s = buf;
    351       1.1    itojun 	ssize_t res, pos = 0;
    352       1.1    itojun 
    353       1.7  christos 	while (n > (size_t)pos) {
    354       1.1    itojun 		res = read(fd, s + pos, n - pos);
    355       1.1    itojun 		switch (res) {
    356       1.1    itojun 		case -1:
    357       1.1    itojun 			if (errno == EINTR || errno == EAGAIN)
    358       1.1    itojun 				continue;
    359  1.7.48.1  christos 			/* FALLTHROUGH */
    360       1.1    itojun 		case 0:
    361       1.1    itojun 			_exit(0);
    362       1.1    itojun 		default:
    363       1.1    itojun 			pos += res;
    364       1.1    itojun 		}
    365       1.1    itojun 	}
    366       1.1    itojun }
    367       1.1    itojun 
    368       1.1    itojun /* Write data with the assertion that it all has to be written, or
    369       1.1    itojun  * else abort the process.  Based on atomicio() from openssh. */
    370       1.1    itojun static void
    371       1.1    itojun must_write(int fd, void *buf, size_t n)
    372       1.1    itojun {
    373       1.1    itojun 	char *s = buf;
    374       1.1    itojun 	ssize_t res, pos = 0;
    375       1.1    itojun 
    376       1.7  christos 	while (n > (size_t)pos) {
    377       1.1    itojun 		res = write(fd, s + pos, n - pos);
    378       1.1    itojun 		switch (res) {
    379       1.1    itojun 		case -1:
    380       1.1    itojun 			if (errno == EINTR || errno == EAGAIN)
    381       1.1    itojun 				continue;
    382  1.7.48.1  christos 			/* FALLTHROUGH */
    383       1.1    itojun 		case 0:
    384       1.1    itojun 			_exit(0);
    385       1.1    itojun 		default:
    386       1.1    itojun 			pos += res;
    387       1.1    itojun 		}
    388       1.1    itojun 	}
    389       1.1    itojun }
    390