xref: /openssh-portable/sandbox-systrace.c (revision 3bf2a6ac)
1c61b42f2Sderaadt@openbsd.org /* $OpenBSD: sandbox-systrace.c,v 1.18 2015/10/02 01:39:26 deraadt Exp $ */
269ff1df9SDamien Miller /*
369ff1df9SDamien Miller  * Copyright (c) 2011 Damien Miller <djm@mindrot.org>
469ff1df9SDamien Miller  *
569ff1df9SDamien Miller  * Permission to use, copy, modify, and distribute this software for any
669ff1df9SDamien Miller  * purpose with or without fee is hereby granted, provided that the above
769ff1df9SDamien Miller  * copyright notice and this permission notice appear in all copies.
869ff1df9SDamien Miller  *
969ff1df9SDamien Miller  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1069ff1df9SDamien Miller  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1169ff1df9SDamien Miller  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1269ff1df9SDamien Miller  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1369ff1df9SDamien Miller  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1469ff1df9SDamien Miller  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1569ff1df9SDamien Miller  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1669ff1df9SDamien Miller  */
1769ff1df9SDamien Miller 
180dc74512SDamien Miller #include "includes.h"
190dc74512SDamien Miller 
200dc74512SDamien Miller #ifdef SANDBOX_SYSTRACE
210dc74512SDamien Miller 
2269ff1df9SDamien Miller #include <sys/types.h>
2369ff1df9SDamien Miller #include <sys/ioctl.h>
2469ff1df9SDamien Miller #include <sys/syscall.h>
2569ff1df9SDamien Miller #include <sys/socket.h>
263b4b2d30SDarren Tucker #include <sys/wait.h>
2769ff1df9SDamien Miller 
2869ff1df9SDamien Miller #include <dev/systrace.h>
2969ff1df9SDamien Miller 
3069ff1df9SDamien Miller #include <errno.h>
3169ff1df9SDamien Miller #include <fcntl.h>
3269ff1df9SDamien Miller #include <limits.h>
333b4b2d30SDarren Tucker #include <signal.h>
3469ff1df9SDamien Miller #include <stdarg.h>
3569ff1df9SDamien Miller #include <stdio.h>
3669ff1df9SDamien Miller #include <stdlib.h>
3769ff1df9SDamien Miller #include <string.h>
3869ff1df9SDamien Miller #include <unistd.h>
3969ff1df9SDamien Miller 
4069ff1df9SDamien Miller #include "atomicio.h"
4169ff1df9SDamien Miller #include "log.h"
42dcbd41e7SDamien Miller #include "ssh-sandbox.h"
4369ff1df9SDamien Miller #include "xmalloc.h"
4469ff1df9SDamien Miller 
4535e48198SDamien Miller struct sandbox_policy {
4635e48198SDamien Miller 	int syscall;
4735e48198SDamien Miller 	int action;
4835e48198SDamien Miller };
4935e48198SDamien Miller 
5035e48198SDamien Miller /* Permitted syscalls in preauth. Unlisted syscalls get SYSTR_POLICY_KILL */
5135e48198SDamien Miller static const struct sandbox_policy preauth_policy[] = {
527cc44ef7Sderaadt@openbsd.org 	{ SYS_exit, SYSTR_POLICY_PERMIT },
53c61b42f2Sderaadt@openbsd.org #ifdef SYS_kbind
54c61b42f2Sderaadt@openbsd.org 	{ SYS_kbind, SYSTR_POLICY_PERMIT },
55c61b42f2Sderaadt@openbsd.org #endif
56c61b42f2Sderaadt@openbsd.org 
57c61b42f2Sderaadt@openbsd.org 	{ SYS_getpid, SYSTR_POLICY_PERMIT },
58c61b42f2Sderaadt@openbsd.org 	{ SYS_getpgid, SYSTR_POLICY_PERMIT },
59c61b42f2Sderaadt@openbsd.org 	{ SYS_clock_gettime, SYSTR_POLICY_PERMIT },
60c61b42f2Sderaadt@openbsd.org 	{ SYS_gettimeofday, SYSTR_POLICY_PERMIT },
611c5b4bc8SDarren Tucker 	{ SYS_nanosleep, SYSTR_POLICY_PERMIT },
62c61b42f2Sderaadt@openbsd.org 	{ SYS_sigprocmask, SYSTR_POLICY_PERMIT },
63c61b42f2Sderaadt@openbsd.org 
6484a89161SDamien Miller #ifdef SYS_getentropy
6584a89161SDamien Miller 	/* OpenBSD 5.6 and newer use getentropy(2) to seed arc4random(3). */
6684a89161SDamien Miller 	{ SYS_getentropy, SYSTR_POLICY_PERMIT },
6784a89161SDamien Miller #else
6884a89161SDamien Miller 	/* Previous releases used sysctl(3)'s kern.arnd variable. */
6935e48198SDamien Miller 	{ SYS___sysctl, SYSTR_POLICY_PERMIT },
7084a89161SDamien Miller #endif
71996b24ceSDarren Tucker #ifdef SYS_sendsyslog
72c61b42f2Sderaadt@openbsd.org 	{ SYS_sendsyslog, SYSTR_POLICY_PERMIT },
73996b24ceSDarren Tucker #endif
74c61b42f2Sderaadt@openbsd.org 
7535e48198SDamien Miller 	{ SYS_madvise, SYSTR_POLICY_PERMIT },
7635e48198SDamien Miller 	{ SYS_mmap, SYSTR_POLICY_PERMIT },
7735e48198SDamien Miller 	{ SYS_mprotect, SYSTR_POLICY_PERMIT },
78560de922SDamien Miller 	{ SYS_mquery, SYSTR_POLICY_PERMIT },
7935e48198SDamien Miller 	{ SYS_munmap, SYSTR_POLICY_PERMIT },
80c61b42f2Sderaadt@openbsd.org 
817cc44ef7Sderaadt@openbsd.org 	{ SYS_poll, SYSTR_POLICY_PERMIT },
8235e48198SDamien Miller 	{ SYS_select, SYSTR_POLICY_PERMIT },
83c61b42f2Sderaadt@openbsd.org 	{ SYS_read, SYSTR_POLICY_PERMIT },
8435e48198SDamien Miller 	{ SYS_write, SYSTR_POLICY_PERMIT },
85c61b42f2Sderaadt@openbsd.org 	{ SYS_shutdown, SYSTR_POLICY_PERMIT },
86c61b42f2Sderaadt@openbsd.org 	{ SYS_close, SYSTR_POLICY_PERMIT },
87c61b42f2Sderaadt@openbsd.org 
88c61b42f2Sderaadt@openbsd.org 	{ SYS_open, SYSTR_POLICY_NEVER },
89c61b42f2Sderaadt@openbsd.org 
9035e48198SDamien Miller 	{ -1, -1 }
9169ff1df9SDamien Miller };
9269ff1df9SDamien Miller 
9369ff1df9SDamien Miller struct ssh_sandbox {
9469ff1df9SDamien Miller 	int systrace_fd;
9569ff1df9SDamien Miller 	pid_t child_pid;
963b4b2d30SDarren Tucker 	void (*osigchld)(int);
9769ff1df9SDamien Miller };
9869ff1df9SDamien Miller 
9969ff1df9SDamien Miller struct ssh_sandbox *
ssh_sandbox_init(struct monitor * monitor)1000dc74512SDamien Miller ssh_sandbox_init(struct monitor *monitor)
10169ff1df9SDamien Miller {
10269ff1df9SDamien Miller 	struct ssh_sandbox *box;
10369ff1df9SDamien Miller 
10469ff1df9SDamien Miller 	debug3("%s: preparing systrace sandbox", __func__);
10569ff1df9SDamien Miller 	box = xcalloc(1, sizeof(*box));
10669ff1df9SDamien Miller 	box->systrace_fd = -1;
10769ff1df9SDamien Miller 	box->child_pid = 0;
108*3bf2a6acSdtucker@openbsd.org 	box->osigchld = ssh_signal(SIGCHLD, SIG_IGN);
10969ff1df9SDamien Miller 
11069ff1df9SDamien Miller 	return box;
11169ff1df9SDamien Miller }
11269ff1df9SDamien Miller 
11369ff1df9SDamien Miller void
ssh_sandbox_child(struct ssh_sandbox * box)11469ff1df9SDamien Miller ssh_sandbox_child(struct ssh_sandbox *box)
11569ff1df9SDamien Miller {
11669ff1df9SDamien Miller 	debug3("%s: ready", __func__);
117*3bf2a6acSdtucker@openbsd.org 	ssh_signal(SIGCHLD, box->osigchld);
1183b4b2d30SDarren Tucker 	if (kill(getpid(), SIGSTOP) != 0)
1193b4b2d30SDarren Tucker 		fatal("%s: kill(%d, SIGSTOP)", __func__, getpid());
12069ff1df9SDamien Miller 	debug3("%s: started", __func__);
12169ff1df9SDamien Miller }
12269ff1df9SDamien Miller 
12369ff1df9SDamien Miller static void
ssh_sandbox_parent(struct ssh_sandbox * box,pid_t child_pid,const struct sandbox_policy * allowed_syscalls)12469ff1df9SDamien Miller ssh_sandbox_parent(struct ssh_sandbox *box, pid_t child_pid,
12535e48198SDamien Miller     const struct sandbox_policy *allowed_syscalls)
12669ff1df9SDamien Miller {
1273b4b2d30SDarren Tucker 	int dev_systrace, i, j, found, status;
1283b4b2d30SDarren Tucker 	pid_t pid;
12935e48198SDamien Miller 	struct systrace_policy policy;
13069ff1df9SDamien Miller 
1313b4b2d30SDarren Tucker 	/* Wait for the child to send itself a SIGSTOP */
13269ff1df9SDamien Miller 	debug3("%s: wait for child %ld", __func__, (long)child_pid);
1333b4b2d30SDarren Tucker 	do {
1343b4b2d30SDarren Tucker 		pid = waitpid(child_pid, &status, WUNTRACED);
1353b4b2d30SDarren Tucker 	} while (pid == -1 && errno == EINTR);
136*3bf2a6acSdtucker@openbsd.org 	ssh_signal(SIGCHLD, box->osigchld);
1373b4b2d30SDarren Tucker 	if (!WIFSTOPPED(status)) {
1383b4b2d30SDarren Tucker 		if (WIFSIGNALED(status))
1393b4b2d30SDarren Tucker 			fatal("%s: child terminated with signal %d",
1403b4b2d30SDarren Tucker 			    __func__, WTERMSIG(status));
1413b4b2d30SDarren Tucker 		if (WIFEXITED(status))
1423b4b2d30SDarren Tucker 			fatal("%s: child exited with status %d",
1433b4b2d30SDarren Tucker 			    __func__, WEXITSTATUS(status));
1443b4b2d30SDarren Tucker 		fatal("%s: child not stopped", __func__);
1453b4b2d30SDarren Tucker 	}
1463b4b2d30SDarren Tucker 	debug3("%s: child %ld stopped", __func__, (long)child_pid);
14769ff1df9SDamien Miller 	box->child_pid = child_pid;
14869ff1df9SDamien Miller 
14969ff1df9SDamien Miller 	/* Set up systracing of child */
15069ff1df9SDamien Miller 	if ((dev_systrace = open("/dev/systrace", O_RDONLY)) == -1)
15169ff1df9SDamien Miller 		fatal("%s: open(\"/dev/systrace\"): %s", __func__,
15269ff1df9SDamien Miller 		    strerror(errno));
15369ff1df9SDamien Miller 	if (ioctl(dev_systrace, STRIOCCLONE, &box->systrace_fd) == -1)
15469ff1df9SDamien Miller 		fatal("%s: ioctl(STRIOCCLONE, %d): %s", __func__,
15569ff1df9SDamien Miller 		    dev_systrace, strerror(errno));
15669ff1df9SDamien Miller 	close(dev_systrace);
15769ff1df9SDamien Miller 	debug3("%s: systrace attach, fd=%d", __func__, box->systrace_fd);
15869ff1df9SDamien Miller 	if (ioctl(box->systrace_fd, STRIOCATTACH, &child_pid) == -1)
15969ff1df9SDamien Miller 		fatal("%s: ioctl(%d, STRIOCATTACH, %d): %s", __func__,
16069ff1df9SDamien Miller 		    box->systrace_fd, child_pid, strerror(errno));
16169ff1df9SDamien Miller 
16269ff1df9SDamien Miller 	/* Allocate and assign policy */
1631d2c4564SDamien Miller 	memset(&policy, 0, sizeof(policy));
16435e48198SDamien Miller 	policy.strp_op = SYSTR_POLICY_NEW;
16535e48198SDamien Miller 	policy.strp_maxents = SYS_MAXSYSCALL;
16635e48198SDamien Miller 	if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
16769ff1df9SDamien Miller 		fatal("%s: ioctl(%d, STRIOCPOLICY (new)): %s", __func__,
16869ff1df9SDamien Miller 		    box->systrace_fd, strerror(errno));
16969ff1df9SDamien Miller 
17035e48198SDamien Miller 	policy.strp_op = SYSTR_POLICY_ASSIGN;
17135e48198SDamien Miller 	policy.strp_pid = box->child_pid;
17235e48198SDamien Miller 	if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
17369ff1df9SDamien Miller 		fatal("%s: ioctl(%d, STRIOCPOLICY (assign)): %s",
17469ff1df9SDamien Miller 		    __func__, box->systrace_fd, strerror(errno));
17569ff1df9SDamien Miller 
17669ff1df9SDamien Miller 	/* Set per-syscall policy */
17769ff1df9SDamien Miller 	for (i = 0; i < SYS_MAXSYSCALL; i++) {
17835e48198SDamien Miller 		found = 0;
17935e48198SDamien Miller 		for (j = 0; allowed_syscalls[j].syscall != -1; j++) {
18035e48198SDamien Miller 			if (allowed_syscalls[j].syscall == i) {
18169ff1df9SDamien Miller 				found = 1;
18235e48198SDamien Miller 				break;
18369ff1df9SDamien Miller 			}
18435e48198SDamien Miller 		}
18535e48198SDamien Miller 		policy.strp_op = SYSTR_POLICY_MODIFY;
18635e48198SDamien Miller 		policy.strp_code = i;
18735e48198SDamien Miller 		policy.strp_policy = found ?
18835e48198SDamien Miller 		    allowed_syscalls[j].action : SYSTR_POLICY_KILL;
18969ff1df9SDamien Miller 		if (found)
19069ff1df9SDamien Miller 			debug3("%s: policy: enable syscall %d", __func__, i);
19135e48198SDamien Miller 		if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
19269ff1df9SDamien Miller 			fatal("%s: ioctl(%d, STRIOCPOLICY (modify)): %s",
19369ff1df9SDamien Miller 			    __func__, box->systrace_fd, strerror(errno));
19469ff1df9SDamien Miller 	}
19569ff1df9SDamien Miller 
19669ff1df9SDamien Miller 	/* Signal the child to start running */
19769ff1df9SDamien Miller 	debug3("%s: start child %ld", __func__, (long)child_pid);
1983b4b2d30SDarren Tucker 	if (kill(box->child_pid, SIGCONT) != 0)
1993b4b2d30SDarren Tucker 		fatal("%s: kill(%d, SIGCONT)", __func__, box->child_pid);
20069ff1df9SDamien Miller }
20169ff1df9SDamien Miller 
20269ff1df9SDamien Miller void
ssh_sandbox_parent_finish(struct ssh_sandbox * box)20369ff1df9SDamien Miller ssh_sandbox_parent_finish(struct ssh_sandbox *box)
20469ff1df9SDamien Miller {
20569ff1df9SDamien Miller 	/* Closing this before the child exits will terminate it */
20669ff1df9SDamien Miller 	close(box->systrace_fd);
20769ff1df9SDamien Miller 
20869ff1df9SDamien Miller 	free(box);
20969ff1df9SDamien Miller 	debug3("%s: finished", __func__);
21069ff1df9SDamien Miller }
21169ff1df9SDamien Miller 
21269ff1df9SDamien Miller void
ssh_sandbox_parent_preauth(struct ssh_sandbox * box,pid_t child_pid)21369ff1df9SDamien Miller ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
21469ff1df9SDamien Miller {
21569ff1df9SDamien Miller 	ssh_sandbox_parent(box, child_pid, preauth_policy);
21669ff1df9SDamien Miller }
2170dc74512SDamien Miller 
2180dc74512SDamien Miller #endif /* SANDBOX_SYSTRACE */
219