[BACK]Return to sandbox-systrace.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sandbox-systrace.c, Revision 1.12

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