1296781Sdes/* $OpenBSD: sandbox-systrace.c,v 1.18 2015/10/02 01:39:26 deraadt Exp $ */ 2225825Sdes/* 3225825Sdes * Copyright (c) 2011 Damien Miller <djm@mindrot.org> 4225825Sdes * 5225825Sdes * Permission to use, copy, modify, and distribute this software for any 6225825Sdes * purpose with or without fee is hereby granted, provided that the above 7225825Sdes * copyright notice and this permission notice appear in all copies. 8225825Sdes * 9225825Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10225825Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11225825Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12225825Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13225825Sdes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14225825Sdes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15225825Sdes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16225825Sdes */ 17225825Sdes 18225825Sdes#include "includes.h" 19225825Sdes 20225825Sdes#ifdef SANDBOX_SYSTRACE 21225825Sdes 22225825Sdes#include <sys/types.h> 23225825Sdes#include <sys/ioctl.h> 24225825Sdes#include <sys/syscall.h> 25225825Sdes#include <sys/socket.h> 26240075Sdes#include <sys/wait.h> 27225825Sdes 28225825Sdes#include <dev/systrace.h> 29225825Sdes 30225825Sdes#include <errno.h> 31225825Sdes#include <fcntl.h> 32225825Sdes#include <limits.h> 33240075Sdes#include <signal.h> 34225825Sdes#include <stdarg.h> 35225825Sdes#include <stdio.h> 36225825Sdes#include <stdlib.h> 37225825Sdes#include <string.h> 38225825Sdes#include <unistd.h> 39295367Sdes#include <limits.h> 40225825Sdes 41225825Sdes#include "atomicio.h" 42225825Sdes#include "log.h" 43225825Sdes#include "ssh-sandbox.h" 44225825Sdes#include "xmalloc.h" 45225825Sdes 46225825Sdesstruct sandbox_policy { 47225825Sdes int syscall; 48225825Sdes int action; 49225825Sdes}; 50225825Sdes 51225825Sdes/* Permitted syscalls in preauth. Unlisted syscalls get SYSTR_POLICY_KILL */ 52225825Sdesstatic const struct sandbox_policy preauth_policy[] = { 53296781Sdes { SYS_exit, SYSTR_POLICY_PERMIT }, 54296781Sdes#ifdef SYS_kbind 55296781Sdes { SYS_kbind, SYSTR_POLICY_PERMIT }, 56296781Sdes#endif 57296781Sdes 58296781Sdes { SYS_getpid, SYSTR_POLICY_PERMIT }, 59296781Sdes { SYS_getpgid, SYSTR_POLICY_PERMIT }, 60295367Sdes { SYS_clock_gettime, SYSTR_POLICY_PERMIT }, 61296781Sdes { SYS_gettimeofday, SYSTR_POLICY_PERMIT }, 62296781Sdes { SYS_sigprocmask, SYSTR_POLICY_PERMIT }, 63296781Sdes 64295367Sdes#ifdef SYS_getentropy 65295367Sdes /* OpenBSD 5.6 and newer use getentropy(2) to seed arc4random(3). */ 66295367Sdes { SYS_getentropy, SYSTR_POLICY_PERMIT }, 67295367Sdes#else 68295367Sdes /* Previous releases used sysctl(3)'s kern.arnd variable. */ 69295367Sdes { SYS___sysctl, SYSTR_POLICY_PERMIT }, 70295367Sdes#endif 71296781Sdes#ifdef SYS_sendsyslog 72296781Sdes { SYS_sendsyslog, SYSTR_POLICY_PERMIT }, 73295367Sdes#endif 74296781Sdes 75225825Sdes { SYS_madvise, SYSTR_POLICY_PERMIT }, 76225825Sdes { SYS_mmap, SYSTR_POLICY_PERMIT }, 77225825Sdes { SYS_mprotect, SYSTR_POLICY_PERMIT }, 78240075Sdes { SYS_mquery, SYSTR_POLICY_PERMIT }, 79295367Sdes { SYS_munmap, SYSTR_POLICY_PERMIT }, 80296781Sdes 81225825Sdes { SYS_poll, SYSTR_POLICY_PERMIT }, 82296781Sdes { SYS_select, SYSTR_POLICY_PERMIT }, 83225825Sdes { SYS_read, SYSTR_POLICY_PERMIT }, 84296781Sdes { SYS_write, SYSTR_POLICY_PERMIT }, 85264377Sdes { SYS_shutdown, SYSTR_POLICY_PERMIT }, 86296781Sdes { SYS_close, SYSTR_POLICY_PERMIT }, 87296781Sdes 88296781Sdes { SYS_open, SYSTR_POLICY_NEVER }, 89296781Sdes 90225825Sdes { -1, -1 } 91225825Sdes}; 92225825Sdes 93225825Sdesstruct ssh_sandbox { 94225825Sdes int systrace_fd; 95225825Sdes pid_t child_pid; 96240075Sdes void (*osigchld)(int); 97225825Sdes}; 98225825Sdes 99225825Sdesstruct ssh_sandbox * 100262566Sdesssh_sandbox_init(struct monitor *monitor) 101225825Sdes{ 102225825Sdes struct ssh_sandbox *box; 103225825Sdes 104225825Sdes debug3("%s: preparing systrace sandbox", __func__); 105225825Sdes box = xcalloc(1, sizeof(*box)); 106225825Sdes box->systrace_fd = -1; 107225825Sdes box->child_pid = 0; 108240075Sdes box->osigchld = signal(SIGCHLD, SIG_IGN); 109225825Sdes 110225825Sdes return box; 111225825Sdes} 112225825Sdes 113225825Sdesvoid 114225825Sdesssh_sandbox_child(struct ssh_sandbox *box) 115225825Sdes{ 116225825Sdes debug3("%s: ready", __func__); 117240075Sdes signal(SIGCHLD, box->osigchld); 118240075Sdes if (kill(getpid(), SIGSTOP) != 0) 119240075Sdes fatal("%s: kill(%d, SIGSTOP)", __func__, getpid()); 120225825Sdes debug3("%s: started", __func__); 121225825Sdes} 122225825Sdes 123225825Sdesstatic void 124225825Sdesssh_sandbox_parent(struct ssh_sandbox *box, pid_t child_pid, 125225825Sdes const struct sandbox_policy *allowed_syscalls) 126225825Sdes{ 127240075Sdes int dev_systrace, i, j, found, status; 128240075Sdes pid_t pid; 129225825Sdes struct systrace_policy policy; 130225825Sdes 131240075Sdes /* Wait for the child to send itself a SIGSTOP */ 132225825Sdes debug3("%s: wait for child %ld", __func__, (long)child_pid); 133240075Sdes do { 134240075Sdes pid = waitpid(child_pid, &status, WUNTRACED); 135240075Sdes } while (pid == -1 && errno == EINTR); 136240075Sdes signal(SIGCHLD, box->osigchld); 137240075Sdes if (!WIFSTOPPED(status)) { 138240075Sdes if (WIFSIGNALED(status)) 139240075Sdes fatal("%s: child terminated with signal %d", 140240075Sdes __func__, WTERMSIG(status)); 141240075Sdes if (WIFEXITED(status)) 142240075Sdes fatal("%s: child exited with status %d", 143240075Sdes __func__, WEXITSTATUS(status)); 144240075Sdes fatal("%s: child not stopped", __func__); 145240075Sdes } 146240075Sdes debug3("%s: child %ld stopped", __func__, (long)child_pid); 147225825Sdes box->child_pid = child_pid; 148225825Sdes 149225825Sdes /* Set up systracing of child */ 150225825Sdes if ((dev_systrace = open("/dev/systrace", O_RDONLY)) == -1) 151225825Sdes fatal("%s: open(\"/dev/systrace\"): %s", __func__, 152225825Sdes strerror(errno)); 153225825Sdes if (ioctl(dev_systrace, STRIOCCLONE, &box->systrace_fd) == -1) 154225825Sdes fatal("%s: ioctl(STRIOCCLONE, %d): %s", __func__, 155225825Sdes dev_systrace, strerror(errno)); 156225825Sdes close(dev_systrace); 157225825Sdes debug3("%s: systrace attach, fd=%d", __func__, box->systrace_fd); 158225825Sdes if (ioctl(box->systrace_fd, STRIOCATTACH, &child_pid) == -1) 159225825Sdes fatal("%s: ioctl(%d, STRIOCATTACH, %d): %s", __func__, 160225825Sdes box->systrace_fd, child_pid, strerror(errno)); 161225825Sdes 162225825Sdes /* Allocate and assign policy */ 163264377Sdes memset(&policy, 0, sizeof(policy)); 164225825Sdes policy.strp_op = SYSTR_POLICY_NEW; 165225825Sdes policy.strp_maxents = SYS_MAXSYSCALL; 166225825Sdes if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1) 167225825Sdes fatal("%s: ioctl(%d, STRIOCPOLICY (new)): %s", __func__, 168225825Sdes box->systrace_fd, strerror(errno)); 169225825Sdes 170225825Sdes policy.strp_op = SYSTR_POLICY_ASSIGN; 171225825Sdes policy.strp_pid = box->child_pid; 172225825Sdes if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1) 173225825Sdes fatal("%s: ioctl(%d, STRIOCPOLICY (assign)): %s", 174225825Sdes __func__, box->systrace_fd, strerror(errno)); 175225825Sdes 176225825Sdes /* Set per-syscall policy */ 177225825Sdes for (i = 0; i < SYS_MAXSYSCALL; i++) { 178225825Sdes found = 0; 179225825Sdes for (j = 0; allowed_syscalls[j].syscall != -1; j++) { 180225825Sdes if (allowed_syscalls[j].syscall == i) { 181225825Sdes found = 1; 182225825Sdes break; 183225825Sdes } 184225825Sdes } 185225825Sdes policy.strp_op = SYSTR_POLICY_MODIFY; 186225825Sdes policy.strp_code = i; 187225825Sdes policy.strp_policy = found ? 188225825Sdes allowed_syscalls[j].action : SYSTR_POLICY_KILL; 189225825Sdes if (found) 190225825Sdes debug3("%s: policy: enable syscall %d", __func__, i); 191225825Sdes if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1) 192225825Sdes fatal("%s: ioctl(%d, STRIOCPOLICY (modify)): %s", 193225825Sdes __func__, box->systrace_fd, strerror(errno)); 194225825Sdes } 195225825Sdes 196225825Sdes /* Signal the child to start running */ 197225825Sdes debug3("%s: start child %ld", __func__, (long)child_pid); 198240075Sdes if (kill(box->child_pid, SIGCONT) != 0) 199240075Sdes fatal("%s: kill(%d, SIGCONT)", __func__, box->child_pid); 200225825Sdes} 201225825Sdes 202225825Sdesvoid 203225825Sdesssh_sandbox_parent_finish(struct ssh_sandbox *box) 204225825Sdes{ 205225825Sdes /* Closing this before the child exits will terminate it */ 206225825Sdes close(box->systrace_fd); 207225825Sdes 208225825Sdes free(box); 209225825Sdes debug3("%s: finished", __func__); 210225825Sdes} 211225825Sdes 212225825Sdesvoid 213225825Sdesssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid) 214225825Sdes{ 215225825Sdes ssh_sandbox_parent(box, child_pid, preauth_policy); 216225825Sdes} 217225825Sdes 218225825Sdes#endif /* SANDBOX_SYSTRACE */ 219