blob: 8ebdb73df4cbaefd44c179b1ae52887ffaa372c6 [file] [log] [blame]
deraadt@openbsd.orgc61b42f2015-10-02 01:39:26 +00001/* $OpenBSD: sandbox-systrace.c,v 1.18 2015/10/02 01:39:26 deraadt Exp $ */
Damien Miller69ff1df2011-06-23 08:30:03 +10002/*
3 * Copyright (c) 2011 Damien Miller <djm@mindrot.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
Damien Miller69ff1df2011-06-23 08:30:03 +100018#include <sys/types.h>
Damien Miller69ff1df2011-06-23 08:30:03 +100019#include <sys/ioctl.h>
20#include <sys/syscall.h>
21#include <sys/socket.h>
Darren Tucker3b4b2d32012-07-02 18:54:31 +100022#include <sys/wait.h>
Damien Miller69ff1df2011-06-23 08:30:03 +100023
24#include <dev/systrace.h>
25
26#include <errno.h>
27#include <fcntl.h>
28#include <limits.h>
Darren Tucker3b4b2d32012-07-02 18:54:31 +100029#include <signal.h>
Damien Miller69ff1df2011-06-23 08:30:03 +100030#include <stdarg.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000035#include <limits.h>
Damien Miller69ff1df2011-06-23 08:30:03 +100036
37#include "atomicio.h"
38#include "log.h"
Damien Millerdcbd41e2011-06-23 19:45:51 +100039#include "ssh-sandbox.h"
Damien Miller69ff1df2011-06-23 08:30:03 +100040#include "xmalloc.h"
41
Damien Miller35e48192011-08-06 06:16:23 +100042struct sandbox_policy {
43 int syscall;
44 int action;
45};
46
47/* Permitted syscalls in preauth. Unlisted syscalls get SYSTR_POLICY_KILL */
48static const struct sandbox_policy preauth_policy[] = {
deraadt@openbsd.org7cc44ef2015-05-18 15:06:05 +000049 { SYS_exit, SYSTR_POLICY_PERMIT },
deraadt@openbsd.orgc61b42f2015-10-02 01:39:26 +000050#ifdef SYS_kbind
51 { SYS_kbind, SYSTR_POLICY_PERMIT },
52#endif
53
54 { SYS_getpid, SYSTR_POLICY_PERMIT },
55 { SYS_getpgid, SYSTR_POLICY_PERMIT },
56 { SYS_clock_gettime, SYSTR_POLICY_PERMIT },
57 { SYS_gettimeofday, SYSTR_POLICY_PERMIT },
58 { SYS_sigprocmask, SYSTR_POLICY_PERMIT },
59
Damien Miller84a89162014-07-02 12:47:48 +100060#ifdef SYS_getentropy
61 /* OpenBSD 5.6 and newer use getentropy(2) to seed arc4random(3). */
62 { SYS_getentropy, SYSTR_POLICY_PERMIT },
63#else
64 /* Previous releases used sysctl(3)'s kern.arnd variable. */
Damien Miller35e48192011-08-06 06:16:23 +100065 { SYS___sysctl, SYSTR_POLICY_PERMIT },
Damien Miller84a89162014-07-02 12:47:48 +100066#endif
deraadt@openbsd.orgc61b42f2015-10-02 01:39:26 +000067 { SYS_sendsyslog, SYSTR_POLICY_PERMIT },
68
Damien Miller35e48192011-08-06 06:16:23 +100069 { SYS_madvise, SYSTR_POLICY_PERMIT },
70 { SYS_mmap, SYSTR_POLICY_PERMIT },
71 { SYS_mprotect, SYSTR_POLICY_PERMIT },
Damien Miller560de922012-06-30 08:33:53 +100072 { SYS_mquery, SYSTR_POLICY_PERMIT },
Damien Miller35e48192011-08-06 06:16:23 +100073 { SYS_munmap, SYSTR_POLICY_PERMIT },
deraadt@openbsd.orgc61b42f2015-10-02 01:39:26 +000074
deraadt@openbsd.org7cc44ef2015-05-18 15:06:05 +000075 { SYS_poll, SYSTR_POLICY_PERMIT },
Damien Miller35e48192011-08-06 06:16:23 +100076 { SYS_select, SYSTR_POLICY_PERMIT },
deraadt@openbsd.orgc61b42f2015-10-02 01:39:26 +000077 { SYS_read, SYSTR_POLICY_PERMIT },
Damien Miller35e48192011-08-06 06:16:23 +100078 { SYS_write, SYSTR_POLICY_PERMIT },
deraadt@openbsd.orgc61b42f2015-10-02 01:39:26 +000079 { SYS_shutdown, SYSTR_POLICY_PERMIT },
80 { SYS_close, SYSTR_POLICY_PERMIT },
81
82 { SYS_open, SYSTR_POLICY_NEVER },
83
Damien Miller35e48192011-08-06 06:16:23 +100084 { -1, -1 }
Damien Miller69ff1df2011-06-23 08:30:03 +100085};
86
87struct ssh_sandbox {
Damien Miller69ff1df2011-06-23 08:30:03 +100088 int systrace_fd;
89 pid_t child_pid;
Darren Tucker3b4b2d32012-07-02 18:54:31 +100090 void (*osigchld)(int);
Damien Miller69ff1df2011-06-23 08:30:03 +100091};
92
93struct ssh_sandbox *
deraadt@openbsd.orgc61b42f2015-10-02 01:39:26 +000094ssh_sandbox_init(void)
Damien Miller69ff1df2011-06-23 08:30:03 +100095{
96 struct ssh_sandbox *box;
Damien Miller69ff1df2011-06-23 08:30:03 +100097
98 debug3("%s: preparing systrace sandbox", __func__);
99 box = xcalloc(1, sizeof(*box));
Damien Miller69ff1df2011-06-23 08:30:03 +1000100 box->systrace_fd = -1;
101 box->child_pid = 0;
Darren Tucker3b4b2d32012-07-02 18:54:31 +1000102 box->osigchld = signal(SIGCHLD, SIG_IGN);
Damien Miller69ff1df2011-06-23 08:30:03 +1000103
104 return box;
105}
106
107void
108ssh_sandbox_child(struct ssh_sandbox *box)
109{
Damien Miller69ff1df2011-06-23 08:30:03 +1000110 debug3("%s: ready", __func__);
Darren Tucker3b4b2d32012-07-02 18:54:31 +1000111 signal(SIGCHLD, box->osigchld);
112 if (kill(getpid(), SIGSTOP) != 0)
113 fatal("%s: kill(%d, SIGSTOP)", __func__, getpid());
Damien Miller69ff1df2011-06-23 08:30:03 +1000114 debug3("%s: started", __func__);
Damien Miller69ff1df2011-06-23 08:30:03 +1000115}
116
117static void
118ssh_sandbox_parent(struct ssh_sandbox *box, pid_t child_pid,
Damien Miller35e48192011-08-06 06:16:23 +1000119 const struct sandbox_policy *allowed_syscalls)
Damien Miller69ff1df2011-06-23 08:30:03 +1000120{
Darren Tucker3b4b2d32012-07-02 18:54:31 +1000121 int dev_systrace, i, j, found, status;
122 pid_t pid;
Damien Miller35e48192011-08-06 06:16:23 +1000123 struct systrace_policy policy;
Damien Miller69ff1df2011-06-23 08:30:03 +1000124
Darren Tucker3b4b2d32012-07-02 18:54:31 +1000125 /* Wait for the child to send itself a SIGSTOP */
Damien Miller69ff1df2011-06-23 08:30:03 +1000126 debug3("%s: wait for child %ld", __func__, (long)child_pid);
Darren Tucker3b4b2d32012-07-02 18:54:31 +1000127 do {
128 pid = waitpid(child_pid, &status, WUNTRACED);
129 } while (pid == -1 && errno == EINTR);
130 signal(SIGCHLD, box->osigchld);
131 if (!WIFSTOPPED(status)) {
132 if (WIFSIGNALED(status))
133 fatal("%s: child terminated with signal %d",
134 __func__, WTERMSIG(status));
135 if (WIFEXITED(status))
136 fatal("%s: child exited with status %d",
137 __func__, WEXITSTATUS(status));
138 fatal("%s: child not stopped", __func__);
139 }
140 debug3("%s: child %ld stopped", __func__, (long)child_pid);
Damien Miller69ff1df2011-06-23 08:30:03 +1000141 box->child_pid = child_pid;
Damien Miller69ff1df2011-06-23 08:30:03 +1000142
143 /* Set up systracing of child */
144 if ((dev_systrace = open("/dev/systrace", O_RDONLY)) == -1)
145 fatal("%s: open(\"/dev/systrace\"): %s", __func__,
146 strerror(errno));
147 if (ioctl(dev_systrace, STRIOCCLONE, &box->systrace_fd) == -1)
148 fatal("%s: ioctl(STRIOCCLONE, %d): %s", __func__,
149 dev_systrace, strerror(errno));
150 close(dev_systrace);
151 debug3("%s: systrace attach, fd=%d", __func__, box->systrace_fd);
152 if (ioctl(box->systrace_fd, STRIOCATTACH, &child_pid) == -1)
153 fatal("%s: ioctl(%d, STRIOCATTACH, %d): %s", __func__,
154 box->systrace_fd, child_pid, strerror(errno));
155
156 /* Allocate and assign policy */
Damien Miller1d2c4562014-02-04 11:18:20 +1100157 memset(&policy, 0, sizeof(policy));
Damien Miller35e48192011-08-06 06:16:23 +1000158 policy.strp_op = SYSTR_POLICY_NEW;
159 policy.strp_maxents = SYS_MAXSYSCALL;
160 if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
Damien Miller69ff1df2011-06-23 08:30:03 +1000161 fatal("%s: ioctl(%d, STRIOCPOLICY (new)): %s", __func__,
162 box->systrace_fd, strerror(errno));
163
Damien Miller35e48192011-08-06 06:16:23 +1000164 policy.strp_op = SYSTR_POLICY_ASSIGN;
165 policy.strp_pid = box->child_pid;
166 if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
Damien Miller69ff1df2011-06-23 08:30:03 +1000167 fatal("%s: ioctl(%d, STRIOCPOLICY (assign)): %s",
168 __func__, box->systrace_fd, strerror(errno));
169
170 /* Set per-syscall policy */
171 for (i = 0; i < SYS_MAXSYSCALL; i++) {
Damien Miller35e48192011-08-06 06:16:23 +1000172 found = 0;
173 for (j = 0; allowed_syscalls[j].syscall != -1; j++) {
174 if (allowed_syscalls[j].syscall == i) {
Damien Miller69ff1df2011-06-23 08:30:03 +1000175 found = 1;
Damien Miller35e48192011-08-06 06:16:23 +1000176 break;
177 }
Damien Miller69ff1df2011-06-23 08:30:03 +1000178 }
Damien Miller35e48192011-08-06 06:16:23 +1000179 policy.strp_op = SYSTR_POLICY_MODIFY;
180 policy.strp_code = i;
181 policy.strp_policy = found ?
182 allowed_syscalls[j].action : SYSTR_POLICY_KILL;
Damien Miller69ff1df2011-06-23 08:30:03 +1000183 if (found)
184 debug3("%s: policy: enable syscall %d", __func__, i);
Damien Miller35e48192011-08-06 06:16:23 +1000185 if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
Damien Miller69ff1df2011-06-23 08:30:03 +1000186 fatal("%s: ioctl(%d, STRIOCPOLICY (modify)): %s",
187 __func__, box->systrace_fd, strerror(errno));
188 }
189
190 /* Signal the child to start running */
191 debug3("%s: start child %ld", __func__, (long)child_pid);
Darren Tucker3b4b2d32012-07-02 18:54:31 +1000192 if (kill(box->child_pid, SIGCONT) != 0)
193 fatal("%s: kill(%d, SIGCONT)", __func__, box->child_pid);
Damien Miller69ff1df2011-06-23 08:30:03 +1000194}
195
196void
197ssh_sandbox_parent_finish(struct ssh_sandbox *box)
198{
199 /* Closing this before the child exits will terminate it */
200 close(box->systrace_fd);
201
202 free(box);
203 debug3("%s: finished", __func__);
204}
205
206void
207ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
208{
209 ssh_sandbox_parent(box, child_pid, preauth_policy);
210}