blob: aaa3d8f0a62f4ce50493b5083763130cfcf38f89 [file] [log] [blame]
Damien Miller1b833202014-07-18 15:03:02 +10001/* $OpenBSD: sandbox-systrace.c,v 1.13 2014/07/17 00:10:56 djm 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
18#include "includes.h"
19
20#ifdef SANDBOX_SYSTRACE
21
22#include <sys/types.h>
23#include <sys/param.h>
24#include <sys/ioctl.h>
25#include <sys/syscall.h>
26#include <sys/socket.h>
Darren Tucker3b4b2d32012-07-02 18:54:31 +100027#include <sys/wait.h>
Damien Miller69ff1df2011-06-23 08:30:03 +100028
29#include <dev/systrace.h>
30
31#include <errno.h>
32#include <fcntl.h>
33#include <limits.h>
Darren Tucker3b4b2d32012-07-02 18:54:31 +100034#include <signal.h>
Damien Miller69ff1df2011-06-23 08:30:03 +100035#include <stdarg.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40
41#include "atomicio.h"
42#include "log.h"
Damien Millerdcbd41e2011-06-23 19:45:51 +100043#include "ssh-sandbox.h"
Damien Miller69ff1df2011-06-23 08:30:03 +100044#include "xmalloc.h"
45
Damien Miller35e48192011-08-06 06:16:23 +100046struct sandbox_policy {
47 int syscall;
48 int action;
49};
50
51/* Permitted syscalls in preauth. Unlisted syscalls get SYSTR_POLICY_KILL */
52static const struct sandbox_policy preauth_policy[] = {
53 { SYS_open, SYSTR_POLICY_NEVER },
54
Damien Miller84a89162014-07-02 12:47:48 +100055#ifdef SYS_getentropy
56 /* OpenBSD 5.6 and newer use getentropy(2) to seed arc4random(3). */
57 { SYS_getentropy, SYSTR_POLICY_PERMIT },
58#else
59 /* Previous releases used sysctl(3)'s kern.arnd variable. */
Damien Miller35e48192011-08-06 06:16:23 +100060 { SYS___sysctl, SYSTR_POLICY_PERMIT },
Damien Miller84a89162014-07-02 12:47:48 +100061#endif
62
Damien Miller1b833202014-07-18 15:03:02 +100063#ifdef SYS_sendsyslog
64 { SYS_sendsyslog, SYSTR_POLICY_PERMIT },
65#endif
Damien Miller35e48192011-08-06 06:16:23 +100066 { SYS_close, SYSTR_POLICY_PERMIT },
67 { SYS_exit, SYSTR_POLICY_PERMIT },
68 { SYS_getpid, SYSTR_POLICY_PERMIT },
69 { SYS_gettimeofday, SYSTR_POLICY_PERMIT },
Darren Tuckerb759c9c2013-06-02 07:46:16 +100070 { SYS_clock_gettime, SYSTR_POLICY_PERMIT },
Damien Miller35e48192011-08-06 06:16:23 +100071 { SYS_madvise, SYSTR_POLICY_PERMIT },
72 { SYS_mmap, SYSTR_POLICY_PERMIT },
73 { SYS_mprotect, SYSTR_POLICY_PERMIT },
Damien Miller560de922012-06-30 08:33:53 +100074 { SYS_mquery, SYSTR_POLICY_PERMIT },
Damien Miller35e48192011-08-06 06:16:23 +100075 { SYS_poll, SYSTR_POLICY_PERMIT },
76 { SYS_munmap, SYSTR_POLICY_PERMIT },
77 { SYS_read, SYSTR_POLICY_PERMIT },
78 { SYS_select, SYSTR_POLICY_PERMIT },
Damien Miller7e5cec62014-01-31 09:25:34 +110079 { SYS_shutdown, SYSTR_POLICY_PERMIT },
Damien Miller35e48192011-08-06 06:16:23 +100080 { SYS_sigprocmask, SYSTR_POLICY_PERMIT },
81 { SYS_write, SYSTR_POLICY_PERMIT },
82 { -1, -1 }
Damien Miller69ff1df2011-06-23 08:30:03 +100083};
84
85struct ssh_sandbox {
Damien Miller69ff1df2011-06-23 08:30:03 +100086 int systrace_fd;
87 pid_t child_pid;
Darren Tucker3b4b2d32012-07-02 18:54:31 +100088 void (*osigchld)(int);
Damien Miller69ff1df2011-06-23 08:30:03 +100089};
90
91struct ssh_sandbox *
Damien Miller868ea1e2014-01-17 16:47:04 +110092ssh_sandbox_init(struct monitor *monitor)
Damien Miller69ff1df2011-06-23 08:30:03 +100093{
94 struct ssh_sandbox *box;
Damien Miller69ff1df2011-06-23 08:30:03 +100095
96 debug3("%s: preparing systrace sandbox", __func__);
97 box = xcalloc(1, sizeof(*box));
Damien Miller69ff1df2011-06-23 08:30:03 +100098 box->systrace_fd = -1;
99 box->child_pid = 0;
Darren Tucker3b4b2d32012-07-02 18:54:31 +1000100 box->osigchld = signal(SIGCHLD, SIG_IGN);
Damien Miller69ff1df2011-06-23 08:30:03 +1000101
102 return box;
103}
104
105void
106ssh_sandbox_child(struct ssh_sandbox *box)
107{
Damien Miller69ff1df2011-06-23 08:30:03 +1000108 debug3("%s: ready", __func__);
Darren Tucker3b4b2d32012-07-02 18:54:31 +1000109 signal(SIGCHLD, box->osigchld);
110 if (kill(getpid(), SIGSTOP) != 0)
111 fatal("%s: kill(%d, SIGSTOP)", __func__, getpid());
Damien Miller69ff1df2011-06-23 08:30:03 +1000112 debug3("%s: started", __func__);
Damien Miller69ff1df2011-06-23 08:30:03 +1000113}
114
115static void
116ssh_sandbox_parent(struct ssh_sandbox *box, pid_t child_pid,
Damien Miller35e48192011-08-06 06:16:23 +1000117 const struct sandbox_policy *allowed_syscalls)
Damien Miller69ff1df2011-06-23 08:30:03 +1000118{
Darren Tucker3b4b2d32012-07-02 18:54:31 +1000119 int dev_systrace, i, j, found, status;
120 pid_t pid;
Damien Miller35e48192011-08-06 06:16:23 +1000121 struct systrace_policy policy;
Damien Miller69ff1df2011-06-23 08:30:03 +1000122
Darren Tucker3b4b2d32012-07-02 18:54:31 +1000123 /* Wait for the child to send itself a SIGSTOP */
Damien Miller69ff1df2011-06-23 08:30:03 +1000124 debug3("%s: wait for child %ld", __func__, (long)child_pid);
Darren Tucker3b4b2d32012-07-02 18:54:31 +1000125 do {
126 pid = waitpid(child_pid, &status, WUNTRACED);
127 } while (pid == -1 && errno == EINTR);
128 signal(SIGCHLD, box->osigchld);
129 if (!WIFSTOPPED(status)) {
130 if (WIFSIGNALED(status))
131 fatal("%s: child terminated with signal %d",
132 __func__, WTERMSIG(status));
133 if (WIFEXITED(status))
134 fatal("%s: child exited with status %d",
135 __func__, WEXITSTATUS(status));
136 fatal("%s: child not stopped", __func__);
137 }
138 debug3("%s: child %ld stopped", __func__, (long)child_pid);
Damien Miller69ff1df2011-06-23 08:30:03 +1000139 box->child_pid = child_pid;
Damien Miller69ff1df2011-06-23 08:30:03 +1000140
141 /* Set up systracing of child */
142 if ((dev_systrace = open("/dev/systrace", O_RDONLY)) == -1)
143 fatal("%s: open(\"/dev/systrace\"): %s", __func__,
144 strerror(errno));
145 if (ioctl(dev_systrace, STRIOCCLONE, &box->systrace_fd) == -1)
146 fatal("%s: ioctl(STRIOCCLONE, %d): %s", __func__,
147 dev_systrace, strerror(errno));
148 close(dev_systrace);
149 debug3("%s: systrace attach, fd=%d", __func__, box->systrace_fd);
150 if (ioctl(box->systrace_fd, STRIOCATTACH, &child_pid) == -1)
151 fatal("%s: ioctl(%d, STRIOCATTACH, %d): %s", __func__,
152 box->systrace_fd, child_pid, strerror(errno));
153
154 /* Allocate and assign policy */
Damien Miller1d2c4562014-02-04 11:18:20 +1100155 memset(&policy, 0, sizeof(policy));
Damien Miller35e48192011-08-06 06:16:23 +1000156 policy.strp_op = SYSTR_POLICY_NEW;
157 policy.strp_maxents = SYS_MAXSYSCALL;
158 if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
Damien Miller69ff1df2011-06-23 08:30:03 +1000159 fatal("%s: ioctl(%d, STRIOCPOLICY (new)): %s", __func__,
160 box->systrace_fd, strerror(errno));
161
Damien Miller35e48192011-08-06 06:16:23 +1000162 policy.strp_op = SYSTR_POLICY_ASSIGN;
163 policy.strp_pid = box->child_pid;
164 if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
Damien Miller69ff1df2011-06-23 08:30:03 +1000165 fatal("%s: ioctl(%d, STRIOCPOLICY (assign)): %s",
166 __func__, box->systrace_fd, strerror(errno));
167
168 /* Set per-syscall policy */
169 for (i = 0; i < SYS_MAXSYSCALL; i++) {
Damien Miller35e48192011-08-06 06:16:23 +1000170 found = 0;
171 for (j = 0; allowed_syscalls[j].syscall != -1; j++) {
172 if (allowed_syscalls[j].syscall == i) {
Damien Miller69ff1df2011-06-23 08:30:03 +1000173 found = 1;
Damien Miller35e48192011-08-06 06:16:23 +1000174 break;
175 }
Damien Miller69ff1df2011-06-23 08:30:03 +1000176 }
Damien Miller35e48192011-08-06 06:16:23 +1000177 policy.strp_op = SYSTR_POLICY_MODIFY;
178 policy.strp_code = i;
179 policy.strp_policy = found ?
180 allowed_syscalls[j].action : SYSTR_POLICY_KILL;
Damien Miller69ff1df2011-06-23 08:30:03 +1000181 if (found)
182 debug3("%s: policy: enable syscall %d", __func__, i);
Damien Miller35e48192011-08-06 06:16:23 +1000183 if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
Damien Miller69ff1df2011-06-23 08:30:03 +1000184 fatal("%s: ioctl(%d, STRIOCPOLICY (modify)): %s",
185 __func__, box->systrace_fd, strerror(errno));
186 }
187
188 /* Signal the child to start running */
189 debug3("%s: start child %ld", __func__, (long)child_pid);
Darren Tucker3b4b2d32012-07-02 18:54:31 +1000190 if (kill(box->child_pid, SIGCONT) != 0)
191 fatal("%s: kill(%d, SIGCONT)", __func__, box->child_pid);
Damien Miller69ff1df2011-06-23 08:30:03 +1000192}
193
194void
195ssh_sandbox_parent_finish(struct ssh_sandbox *box)
196{
197 /* Closing this before the child exits will terminate it */
198 close(box->systrace_fd);
199
200 free(box);
201 debug3("%s: finished", __func__);
202}
203
204void
205ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
206{
207 ssh_sandbox_parent(box, child_pid, preauth_policy);
208}
209
210#endif /* SANDBOX_SYSTRACE */