Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 1 | /* $OpenBSD: sandbox-systrace.c,v 1.7 2013/06/01 13:15:52 dtucker Exp $ */ |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 2 | /* |
| 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 Tucker | 3b4b2d3 | 2012-07-02 18:54:31 +1000 | [diff] [blame] | 27 | #include <sys/wait.h> |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 28 | |
| 29 | #include <dev/systrace.h> |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <limits.h> |
Darren Tucker | 3b4b2d3 | 2012-07-02 18:54:31 +1000 | [diff] [blame] | 34 | #include <signal.h> |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 35 | #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 Miller | dcbd41e | 2011-06-23 19:45:51 +1000 | [diff] [blame] | 43 | #include "ssh-sandbox.h" |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 44 | #include "xmalloc.h" |
| 45 | |
Damien Miller | 35e4819 | 2011-08-06 06:16:23 +1000 | [diff] [blame] | 46 | struct sandbox_policy { |
| 47 | int syscall; |
| 48 | int action; |
| 49 | }; |
| 50 | |
| 51 | /* Permitted syscalls in preauth. Unlisted syscalls get SYSTR_POLICY_KILL */ |
| 52 | static const struct sandbox_policy preauth_policy[] = { |
| 53 | { SYS_open, SYSTR_POLICY_NEVER }, |
| 54 | |
| 55 | { SYS___sysctl, SYSTR_POLICY_PERMIT }, |
| 56 | { SYS_close, SYSTR_POLICY_PERMIT }, |
| 57 | { SYS_exit, SYSTR_POLICY_PERMIT }, |
| 58 | { SYS_getpid, SYSTR_POLICY_PERMIT }, |
| 59 | { SYS_gettimeofday, SYSTR_POLICY_PERMIT }, |
Darren Tucker | b759c9c | 2013-06-02 07:46:16 +1000 | [diff] [blame] | 60 | { SYS_clock_gettime, SYSTR_POLICY_PERMIT }, |
Damien Miller | 35e4819 | 2011-08-06 06:16:23 +1000 | [diff] [blame] | 61 | { SYS_madvise, SYSTR_POLICY_PERMIT }, |
| 62 | { SYS_mmap, SYSTR_POLICY_PERMIT }, |
| 63 | { SYS_mprotect, SYSTR_POLICY_PERMIT }, |
Damien Miller | 560de92 | 2012-06-30 08:33:53 +1000 | [diff] [blame] | 64 | { SYS_mquery, SYSTR_POLICY_PERMIT }, |
Damien Miller | 35e4819 | 2011-08-06 06:16:23 +1000 | [diff] [blame] | 65 | { SYS_poll, SYSTR_POLICY_PERMIT }, |
| 66 | { SYS_munmap, SYSTR_POLICY_PERMIT }, |
| 67 | { SYS_read, SYSTR_POLICY_PERMIT }, |
| 68 | { SYS_select, SYSTR_POLICY_PERMIT }, |
| 69 | { SYS_sigprocmask, SYSTR_POLICY_PERMIT }, |
| 70 | { SYS_write, SYSTR_POLICY_PERMIT }, |
| 71 | { -1, -1 } |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | struct ssh_sandbox { |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 75 | int systrace_fd; |
| 76 | pid_t child_pid; |
Darren Tucker | 3b4b2d3 | 2012-07-02 18:54:31 +1000 | [diff] [blame] | 77 | void (*osigchld)(int); |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | struct ssh_sandbox * |
| 81 | ssh_sandbox_init(void) |
| 82 | { |
| 83 | struct ssh_sandbox *box; |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 84 | |
| 85 | debug3("%s: preparing systrace sandbox", __func__); |
| 86 | box = xcalloc(1, sizeof(*box)); |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 87 | box->systrace_fd = -1; |
| 88 | box->child_pid = 0; |
Darren Tucker | 3b4b2d3 | 2012-07-02 18:54:31 +1000 | [diff] [blame] | 89 | box->osigchld = signal(SIGCHLD, SIG_IGN); |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 90 | |
| 91 | return box; |
| 92 | } |
| 93 | |
| 94 | void |
| 95 | ssh_sandbox_child(struct ssh_sandbox *box) |
| 96 | { |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 97 | debug3("%s: ready", __func__); |
Darren Tucker | 3b4b2d3 | 2012-07-02 18:54:31 +1000 | [diff] [blame] | 98 | signal(SIGCHLD, box->osigchld); |
| 99 | if (kill(getpid(), SIGSTOP) != 0) |
| 100 | fatal("%s: kill(%d, SIGSTOP)", __func__, getpid()); |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 101 | debug3("%s: started", __func__); |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void |
| 105 | ssh_sandbox_parent(struct ssh_sandbox *box, pid_t child_pid, |
Damien Miller | 35e4819 | 2011-08-06 06:16:23 +1000 | [diff] [blame] | 106 | const struct sandbox_policy *allowed_syscalls) |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 107 | { |
Darren Tucker | 3b4b2d3 | 2012-07-02 18:54:31 +1000 | [diff] [blame] | 108 | int dev_systrace, i, j, found, status; |
| 109 | pid_t pid; |
Damien Miller | 35e4819 | 2011-08-06 06:16:23 +1000 | [diff] [blame] | 110 | struct systrace_policy policy; |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 111 | |
Darren Tucker | 3b4b2d3 | 2012-07-02 18:54:31 +1000 | [diff] [blame] | 112 | /* Wait for the child to send itself a SIGSTOP */ |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 113 | debug3("%s: wait for child %ld", __func__, (long)child_pid); |
Darren Tucker | 3b4b2d3 | 2012-07-02 18:54:31 +1000 | [diff] [blame] | 114 | do { |
| 115 | pid = waitpid(child_pid, &status, WUNTRACED); |
| 116 | } while (pid == -1 && errno == EINTR); |
| 117 | signal(SIGCHLD, box->osigchld); |
| 118 | if (!WIFSTOPPED(status)) { |
| 119 | if (WIFSIGNALED(status)) |
| 120 | fatal("%s: child terminated with signal %d", |
| 121 | __func__, WTERMSIG(status)); |
| 122 | if (WIFEXITED(status)) |
| 123 | fatal("%s: child exited with status %d", |
| 124 | __func__, WEXITSTATUS(status)); |
| 125 | fatal("%s: child not stopped", __func__); |
| 126 | } |
| 127 | debug3("%s: child %ld stopped", __func__, (long)child_pid); |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 128 | box->child_pid = child_pid; |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 129 | |
| 130 | /* Set up systracing of child */ |
| 131 | if ((dev_systrace = open("/dev/systrace", O_RDONLY)) == -1) |
| 132 | fatal("%s: open(\"/dev/systrace\"): %s", __func__, |
| 133 | strerror(errno)); |
| 134 | if (ioctl(dev_systrace, STRIOCCLONE, &box->systrace_fd) == -1) |
| 135 | fatal("%s: ioctl(STRIOCCLONE, %d): %s", __func__, |
| 136 | dev_systrace, strerror(errno)); |
| 137 | close(dev_systrace); |
| 138 | debug3("%s: systrace attach, fd=%d", __func__, box->systrace_fd); |
| 139 | if (ioctl(box->systrace_fd, STRIOCATTACH, &child_pid) == -1) |
| 140 | fatal("%s: ioctl(%d, STRIOCATTACH, %d): %s", __func__, |
| 141 | box->systrace_fd, child_pid, strerror(errno)); |
| 142 | |
| 143 | /* Allocate and assign policy */ |
Damien Miller | 35e4819 | 2011-08-06 06:16:23 +1000 | [diff] [blame] | 144 | bzero(&policy, sizeof(policy)); |
| 145 | policy.strp_op = SYSTR_POLICY_NEW; |
| 146 | policy.strp_maxents = SYS_MAXSYSCALL; |
| 147 | if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1) |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 148 | fatal("%s: ioctl(%d, STRIOCPOLICY (new)): %s", __func__, |
| 149 | box->systrace_fd, strerror(errno)); |
| 150 | |
Damien Miller | 35e4819 | 2011-08-06 06:16:23 +1000 | [diff] [blame] | 151 | policy.strp_op = SYSTR_POLICY_ASSIGN; |
| 152 | policy.strp_pid = box->child_pid; |
| 153 | if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1) |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 154 | fatal("%s: ioctl(%d, STRIOCPOLICY (assign)): %s", |
| 155 | __func__, box->systrace_fd, strerror(errno)); |
| 156 | |
| 157 | /* Set per-syscall policy */ |
| 158 | for (i = 0; i < SYS_MAXSYSCALL; i++) { |
Damien Miller | 35e4819 | 2011-08-06 06:16:23 +1000 | [diff] [blame] | 159 | found = 0; |
| 160 | for (j = 0; allowed_syscalls[j].syscall != -1; j++) { |
| 161 | if (allowed_syscalls[j].syscall == i) { |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 162 | found = 1; |
Damien Miller | 35e4819 | 2011-08-06 06:16:23 +1000 | [diff] [blame] | 163 | break; |
| 164 | } |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 165 | } |
Damien Miller | 35e4819 | 2011-08-06 06:16:23 +1000 | [diff] [blame] | 166 | policy.strp_op = SYSTR_POLICY_MODIFY; |
| 167 | policy.strp_code = i; |
| 168 | policy.strp_policy = found ? |
| 169 | allowed_syscalls[j].action : SYSTR_POLICY_KILL; |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 170 | if (found) |
| 171 | debug3("%s: policy: enable syscall %d", __func__, i); |
Damien Miller | 35e4819 | 2011-08-06 06:16:23 +1000 | [diff] [blame] | 172 | if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1) |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 173 | fatal("%s: ioctl(%d, STRIOCPOLICY (modify)): %s", |
| 174 | __func__, box->systrace_fd, strerror(errno)); |
| 175 | } |
| 176 | |
| 177 | /* Signal the child to start running */ |
| 178 | debug3("%s: start child %ld", __func__, (long)child_pid); |
Darren Tucker | 3b4b2d3 | 2012-07-02 18:54:31 +1000 | [diff] [blame] | 179 | if (kill(box->child_pid, SIGCONT) != 0) |
| 180 | fatal("%s: kill(%d, SIGCONT)", __func__, box->child_pid); |
Damien Miller | 69ff1df | 2011-06-23 08:30:03 +1000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void |
| 184 | ssh_sandbox_parent_finish(struct ssh_sandbox *box) |
| 185 | { |
| 186 | /* Closing this before the child exits will terminate it */ |
| 187 | close(box->systrace_fd); |
| 188 | |
| 189 | free(box); |
| 190 | debug3("%s: finished", __func__); |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid) |
| 195 | { |
| 196 | ssh_sandbox_parent(box, child_pid, preauth_policy); |
| 197 | } |
| 198 | |
| 199 | #endif /* SANDBOX_SYSTRACE */ |