1264377Sdes/* $OpenBSD: sandbox-systrace.c,v 1.9 2014/01/31 16:39:19 tedu 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/param.h> 24225825Sdes#include <sys/ioctl.h> 25225825Sdes#include <sys/syscall.h> 26225825Sdes#include <sys/socket.h> 27240075Sdes#include <sys/wait.h> 28225825Sdes 29225825Sdes#include <dev/systrace.h> 30225825Sdes 31225825Sdes#include <errno.h> 32225825Sdes#include <fcntl.h> 33225825Sdes#include <limits.h> 34240075Sdes#include <signal.h> 35225825Sdes#include <stdarg.h> 36225825Sdes#include <stdio.h> 37225825Sdes#include <stdlib.h> 38225825Sdes#include <string.h> 39225825Sdes#include <unistd.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[] = { 53225825Sdes { SYS_open, SYSTR_POLICY_NEVER }, 54225825Sdes 55225825Sdes { SYS___sysctl, SYSTR_POLICY_PERMIT }, 56225825Sdes { SYS_close, SYSTR_POLICY_PERMIT }, 57225825Sdes { SYS_exit, SYSTR_POLICY_PERMIT }, 58225825Sdes { SYS_getpid, SYSTR_POLICY_PERMIT }, 59225825Sdes { SYS_gettimeofday, SYSTR_POLICY_PERMIT }, 60255767Sdes { SYS_clock_gettime, SYSTR_POLICY_PERMIT }, 61225825Sdes { SYS_madvise, SYSTR_POLICY_PERMIT }, 62225825Sdes { SYS_mmap, SYSTR_POLICY_PERMIT }, 63225825Sdes { SYS_mprotect, SYSTR_POLICY_PERMIT }, 64240075Sdes { SYS_mquery, SYSTR_POLICY_PERMIT }, 65225825Sdes { SYS_poll, SYSTR_POLICY_PERMIT }, 66225825Sdes { SYS_munmap, SYSTR_POLICY_PERMIT }, 67225825Sdes { SYS_read, SYSTR_POLICY_PERMIT }, 68225825Sdes { SYS_select, SYSTR_POLICY_PERMIT }, 69264377Sdes { SYS_shutdown, SYSTR_POLICY_PERMIT }, 70225825Sdes { SYS_sigprocmask, SYSTR_POLICY_PERMIT }, 71225825Sdes { SYS_write, SYSTR_POLICY_PERMIT }, 72225825Sdes { -1, -1 } 73225825Sdes}; 74225825Sdes 75225825Sdesstruct ssh_sandbox { 76225825Sdes int systrace_fd; 77225825Sdes pid_t child_pid; 78240075Sdes void (*osigchld)(int); 79225825Sdes}; 80225825Sdes 81225825Sdesstruct ssh_sandbox * 82262566Sdesssh_sandbox_init(struct monitor *monitor) 83225825Sdes{ 84225825Sdes struct ssh_sandbox *box; 85225825Sdes 86225825Sdes debug3("%s: preparing systrace sandbox", __func__); 87225825Sdes box = xcalloc(1, sizeof(*box)); 88225825Sdes box->systrace_fd = -1; 89225825Sdes box->child_pid = 0; 90240075Sdes box->osigchld = signal(SIGCHLD, SIG_IGN); 91225825Sdes 92225825Sdes return box; 93225825Sdes} 94225825Sdes 95225825Sdesvoid 96225825Sdesssh_sandbox_child(struct ssh_sandbox *box) 97225825Sdes{ 98225825Sdes debug3("%s: ready", __func__); 99240075Sdes signal(SIGCHLD, box->osigchld); 100240075Sdes if (kill(getpid(), SIGSTOP) != 0) 101240075Sdes fatal("%s: kill(%d, SIGSTOP)", __func__, getpid()); 102225825Sdes debug3("%s: started", __func__); 103225825Sdes} 104225825Sdes 105225825Sdesstatic void 106225825Sdesssh_sandbox_parent(struct ssh_sandbox *box, pid_t child_pid, 107225825Sdes const struct sandbox_policy *allowed_syscalls) 108225825Sdes{ 109240075Sdes int dev_systrace, i, j, found, status; 110240075Sdes pid_t pid; 111225825Sdes struct systrace_policy policy; 112225825Sdes 113240075Sdes /* Wait for the child to send itself a SIGSTOP */ 114225825Sdes debug3("%s: wait for child %ld", __func__, (long)child_pid); 115240075Sdes do { 116240075Sdes pid = waitpid(child_pid, &status, WUNTRACED); 117240075Sdes } while (pid == -1 && errno == EINTR); 118240075Sdes signal(SIGCHLD, box->osigchld); 119240075Sdes if (!WIFSTOPPED(status)) { 120240075Sdes if (WIFSIGNALED(status)) 121240075Sdes fatal("%s: child terminated with signal %d", 122240075Sdes __func__, WTERMSIG(status)); 123240075Sdes if (WIFEXITED(status)) 124240075Sdes fatal("%s: child exited with status %d", 125240075Sdes __func__, WEXITSTATUS(status)); 126240075Sdes fatal("%s: child not stopped", __func__); 127240075Sdes } 128240075Sdes debug3("%s: child %ld stopped", __func__, (long)child_pid); 129225825Sdes box->child_pid = child_pid; 130225825Sdes 131225825Sdes /* Set up systracing of child */ 132225825Sdes if ((dev_systrace = open("/dev/systrace", O_RDONLY)) == -1) 133225825Sdes fatal("%s: open(\"/dev/systrace\"): %s", __func__, 134225825Sdes strerror(errno)); 135225825Sdes if (ioctl(dev_systrace, STRIOCCLONE, &box->systrace_fd) == -1) 136225825Sdes fatal("%s: ioctl(STRIOCCLONE, %d): %s", __func__, 137225825Sdes dev_systrace, strerror(errno)); 138225825Sdes close(dev_systrace); 139225825Sdes debug3("%s: systrace attach, fd=%d", __func__, box->systrace_fd); 140225825Sdes if (ioctl(box->systrace_fd, STRIOCATTACH, &child_pid) == -1) 141225825Sdes fatal("%s: ioctl(%d, STRIOCATTACH, %d): %s", __func__, 142225825Sdes box->systrace_fd, child_pid, strerror(errno)); 143225825Sdes 144225825Sdes /* Allocate and assign policy */ 145264377Sdes memset(&policy, 0, sizeof(policy)); 146225825Sdes policy.strp_op = SYSTR_POLICY_NEW; 147225825Sdes policy.strp_maxents = SYS_MAXSYSCALL; 148225825Sdes if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1) 149225825Sdes fatal("%s: ioctl(%d, STRIOCPOLICY (new)): %s", __func__, 150225825Sdes box->systrace_fd, strerror(errno)); 151225825Sdes 152225825Sdes policy.strp_op = SYSTR_POLICY_ASSIGN; 153225825Sdes policy.strp_pid = box->child_pid; 154225825Sdes if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1) 155225825Sdes fatal("%s: ioctl(%d, STRIOCPOLICY (assign)): %s", 156225825Sdes __func__, box->systrace_fd, strerror(errno)); 157225825Sdes 158225825Sdes /* Set per-syscall policy */ 159225825Sdes for (i = 0; i < SYS_MAXSYSCALL; i++) { 160225825Sdes found = 0; 161225825Sdes for (j = 0; allowed_syscalls[j].syscall != -1; j++) { 162225825Sdes if (allowed_syscalls[j].syscall == i) { 163225825Sdes found = 1; 164225825Sdes break; 165225825Sdes } 166225825Sdes } 167225825Sdes policy.strp_op = SYSTR_POLICY_MODIFY; 168225825Sdes policy.strp_code = i; 169225825Sdes policy.strp_policy = found ? 170225825Sdes allowed_syscalls[j].action : SYSTR_POLICY_KILL; 171225825Sdes if (found) 172225825Sdes debug3("%s: policy: enable syscall %d", __func__, i); 173225825Sdes if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1) 174225825Sdes fatal("%s: ioctl(%d, STRIOCPOLICY (modify)): %s", 175225825Sdes __func__, box->systrace_fd, strerror(errno)); 176225825Sdes } 177225825Sdes 178225825Sdes /* Signal the child to start running */ 179225825Sdes debug3("%s: start child %ld", __func__, (long)child_pid); 180240075Sdes if (kill(box->child_pid, SIGCONT) != 0) 181240075Sdes fatal("%s: kill(%d, SIGCONT)", __func__, box->child_pid); 182225825Sdes} 183225825Sdes 184225825Sdesvoid 185225825Sdesssh_sandbox_parent_finish(struct ssh_sandbox *box) 186225825Sdes{ 187225825Sdes /* Closing this before the child exits will terminate it */ 188225825Sdes close(box->systrace_fd); 189225825Sdes 190225825Sdes free(box); 191225825Sdes debug3("%s: finished", __func__); 192225825Sdes} 193225825Sdes 194225825Sdesvoid 195225825Sdesssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid) 196225825Sdes{ 197225825Sdes ssh_sandbox_parent(box, child_pid, preauth_policy); 198225825Sdes} 199225825Sdes 200225825Sdes#endif /* SANDBOX_SYSTRACE */ 201