1321807Sbapt/* $OpenBSD: sandbox-systrace.c,v 1.18 2015/10/02 01:39:26 deraadt Exp $ */
2321807Sbapt/*
3321807Sbapt * Copyright (c) 2011 Damien Miller <djm@mindrot.org>
4321807Sbapt *
5321807Sbapt * Permission to use, copy, modify, and distribute this software for any
6321807Sbapt * purpose with or without fee is hereby granted, provided that the above
7321807Sbapt * copyright notice and this permission notice appear in all copies.
8321807Sbapt *
9321807Sbapt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10321807Sbapt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11321807Sbapt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12321807Sbapt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13321807Sbapt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14321807Sbapt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15321807Sbapt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16321807Sbapt */
17321807Sbapt
18321807Sbapt#include "includes.h"
19321807Sbapt
20321807Sbapt#ifdef SANDBOX_SYSTRACE
21321807Sbapt
22321807Sbapt#include <sys/types.h>
23321807Sbapt#include <sys/ioctl.h>
24321807Sbapt#include <sys/syscall.h>
25321807Sbapt#include <sys/socket.h>
26321807Sbapt#include <sys/wait.h>
27321807Sbapt
28321807Sbapt#include <dev/systrace.h>
29321807Sbapt
30321807Sbapt#include <errno.h>
31321807Sbapt#include <fcntl.h>
32321807Sbapt#include <limits.h>
33321807Sbapt#include <signal.h>
34321807Sbapt#include <stdarg.h>
35321807Sbapt#include <stdio.h>
36321807Sbapt#include <stdlib.h>
37321807Sbapt#include <string.h>
38321807Sbapt#include <unistd.h>
39321807Sbapt#include <limits.h>
40321807Sbapt
41321807Sbapt#include "atomicio.h"
42321807Sbapt#include "log.h"
43321807Sbapt#include "ssh-sandbox.h"
44321807Sbapt#include "xmalloc.h"
45321807Sbapt
46321807Sbaptstruct sandbox_policy {
47321807Sbapt	int syscall;
48321807Sbapt	int action;
49321807Sbapt};
50321807Sbapt
51321807Sbapt/* Permitted syscalls in preauth. Unlisted syscalls get SYSTR_POLICY_KILL */
52321807Sbaptstatic const struct sandbox_policy preauth_policy[] = {
53321807Sbapt	{ SYS_exit, SYSTR_POLICY_PERMIT },
54321807Sbapt#ifdef SYS_kbind
55321807Sbapt	{ SYS_kbind, SYSTR_POLICY_PERMIT },
56321807Sbapt#endif
57321807Sbapt
58321807Sbapt	{ SYS_getpid, SYSTR_POLICY_PERMIT },
59321807Sbapt	{ SYS_getpgid, SYSTR_POLICY_PERMIT },
60321807Sbapt	{ SYS_clock_gettime, SYSTR_POLICY_PERMIT },
61321807Sbapt	{ SYS_gettimeofday, SYSTR_POLICY_PERMIT },
62321807Sbapt	{ SYS_sigprocmask, SYSTR_POLICY_PERMIT },
63321807Sbapt
64321807Sbapt#ifdef SYS_getentropy
65321807Sbapt	/* OpenBSD 5.6 and newer use getentropy(2) to seed arc4random(3). */
66321807Sbapt	{ SYS_getentropy, SYSTR_POLICY_PERMIT },
67321807Sbapt#else
68321807Sbapt	/* Previous releases used sysctl(3)'s kern.arnd variable. */
69321807Sbapt	{ SYS___sysctl, SYSTR_POLICY_PERMIT },
70321807Sbapt#endif
71321807Sbapt#ifdef SYS_sendsyslog
72321807Sbapt	{ SYS_sendsyslog, SYSTR_POLICY_PERMIT },
73321807Sbapt#endif
74321807Sbapt
75321807Sbapt	{ SYS_madvise, SYSTR_POLICY_PERMIT },
76321807Sbapt	{ SYS_mmap, SYSTR_POLICY_PERMIT },
77321807Sbapt	{ SYS_mprotect, SYSTR_POLICY_PERMIT },
78321807Sbapt	{ SYS_mquery, SYSTR_POLICY_PERMIT },
79321807Sbapt	{ SYS_munmap, SYSTR_POLICY_PERMIT },
80321807Sbapt
81321807Sbapt	{ SYS_poll, SYSTR_POLICY_PERMIT },
82321807Sbapt	{ SYS_select, SYSTR_POLICY_PERMIT },
83321807Sbapt	{ SYS_read, SYSTR_POLICY_PERMIT },
84321807Sbapt	{ SYS_write, SYSTR_POLICY_PERMIT },
85321807Sbapt	{ SYS_shutdown, SYSTR_POLICY_PERMIT },
86321807Sbapt	{ SYS_close, SYSTR_POLICY_PERMIT },
87321807Sbapt
88321807Sbapt	{ SYS_open, SYSTR_POLICY_NEVER },
89321807Sbapt
90321807Sbapt	{ -1, -1 }
91321807Sbapt};
92321807Sbapt
93321807Sbaptstruct ssh_sandbox {
94321807Sbapt	int systrace_fd;
95321807Sbapt	pid_t child_pid;
96321807Sbapt	void (*osigchld)(int);
97321807Sbapt};
98321807Sbapt
99321807Sbaptstruct ssh_sandbox *
100321807Sbaptssh_sandbox_init(struct monitor *monitor)
101321807Sbapt{
102321807Sbapt	struct ssh_sandbox *box;
103321807Sbapt
104321807Sbapt	debug3("%s: preparing systrace sandbox", __func__);
105321807Sbapt	box = xcalloc(1, sizeof(*box));
106321807Sbapt	box->systrace_fd = -1;
107321807Sbapt	box->child_pid = 0;
108321807Sbapt	box->osigchld = signal(SIGCHLD, SIG_IGN);
109321807Sbapt
110321807Sbapt	return box;
111321807Sbapt}
112321807Sbapt
113321807Sbaptvoid
114321807Sbaptssh_sandbox_child(struct ssh_sandbox *box)
115321807Sbapt{
116321807Sbapt	debug3("%s: ready", __func__);
117321807Sbapt	signal(SIGCHLD, box->osigchld);
118321807Sbapt	if (kill(getpid(), SIGSTOP) != 0)
119321807Sbapt		fatal("%s: kill(%d, SIGSTOP)", __func__, getpid());
120321807Sbapt	debug3("%s: started", __func__);
121321807Sbapt}
122
123static void
124ssh_sandbox_parent(struct ssh_sandbox *box, pid_t child_pid,
125    const struct sandbox_policy *allowed_syscalls)
126{
127	int dev_systrace, i, j, found, status;
128	pid_t pid;
129	struct systrace_policy policy;
130
131	/* Wait for the child to send itself a SIGSTOP */
132	debug3("%s: wait for child %ld", __func__, (long)child_pid);
133	do {
134		pid = waitpid(child_pid, &status, WUNTRACED);
135	} while (pid == -1 && errno == EINTR);
136	signal(SIGCHLD, box->osigchld);
137	if (!WIFSTOPPED(status)) {
138		if (WIFSIGNALED(status))
139			fatal("%s: child terminated with signal %d",
140			    __func__, WTERMSIG(status));
141		if (WIFEXITED(status))
142			fatal("%s: child exited with status %d",
143			    __func__, WEXITSTATUS(status));
144		fatal("%s: child not stopped", __func__);
145	}
146	debug3("%s: child %ld stopped", __func__, (long)child_pid);
147	box->child_pid = child_pid;
148
149	/* Set up systracing of child */
150	if ((dev_systrace = open("/dev/systrace", O_RDONLY)) == -1)
151		fatal("%s: open(\"/dev/systrace\"): %s", __func__,
152		    strerror(errno));
153	if (ioctl(dev_systrace, STRIOCCLONE, &box->systrace_fd) == -1)
154		fatal("%s: ioctl(STRIOCCLONE, %d): %s", __func__,
155		    dev_systrace, strerror(errno));
156	close(dev_systrace);
157	debug3("%s: systrace attach, fd=%d", __func__, box->systrace_fd);
158	if (ioctl(box->systrace_fd, STRIOCATTACH, &child_pid) == -1)
159		fatal("%s: ioctl(%d, STRIOCATTACH, %d): %s", __func__,
160		    box->systrace_fd, child_pid, strerror(errno));
161
162	/* Allocate and assign policy */
163	memset(&policy, 0, sizeof(policy));
164	policy.strp_op = SYSTR_POLICY_NEW;
165	policy.strp_maxents = SYS_MAXSYSCALL;
166	if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
167		fatal("%s: ioctl(%d, STRIOCPOLICY (new)): %s", __func__,
168		    box->systrace_fd, strerror(errno));
169
170	policy.strp_op = SYSTR_POLICY_ASSIGN;
171	policy.strp_pid = box->child_pid;
172	if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
173		fatal("%s: ioctl(%d, STRIOCPOLICY (assign)): %s",
174		    __func__, box->systrace_fd, strerror(errno));
175
176	/* Set per-syscall policy */
177	for (i = 0; i < SYS_MAXSYSCALL; i++) {
178		found = 0;
179		for (j = 0; allowed_syscalls[j].syscall != -1; j++) {
180			if (allowed_syscalls[j].syscall == i) {
181				found = 1;
182				break;
183			}
184		}
185		policy.strp_op = SYSTR_POLICY_MODIFY;
186		policy.strp_code = i;
187		policy.strp_policy = found ?
188		    allowed_syscalls[j].action : SYSTR_POLICY_KILL;
189		if (found)
190			debug3("%s: policy: enable syscall %d", __func__, i);
191		if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
192			fatal("%s: ioctl(%d, STRIOCPOLICY (modify)): %s",
193			    __func__, box->systrace_fd, strerror(errno));
194	}
195
196	/* Signal the child to start running */
197	debug3("%s: start child %ld", __func__, (long)child_pid);
198	if (kill(box->child_pid, SIGCONT) != 0)
199		fatal("%s: kill(%d, SIGCONT)", __func__, box->child_pid);
200}
201
202void
203ssh_sandbox_parent_finish(struct ssh_sandbox *box)
204{
205	/* Closing this before the child exits will terminate it */
206	close(box->systrace_fd);
207
208	free(box);
209	debug3("%s: finished", __func__);
210}
211
212void
213ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
214{
215	ssh_sandbox_parent(box, child_pid, preauth_policy);
216}
217
218#endif /* SANDBOX_SYSTRACE */
219