[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.13

1.13    ! djm         1: /* $OpenBSD: sandbox-systrace.c,v 1.12 2014/07/11 08:09:54 deraadt 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
1.13    ! djm        58: #ifdef SYS_sendsyslog
1.12      deraadt    59:        { SYS_sendsyslog, SYSTR_POLICY_PERMIT },
1.13    ! djm        60: #endif
1.4       djm        61:        { SYS_close, SYSTR_POLICY_PERMIT },
                     62:        { SYS_exit, SYSTR_POLICY_PERMIT },
                     63:        { SYS_getpid, SYSTR_POLICY_PERMIT },
                     64:        { SYS_gettimeofday, SYSTR_POLICY_PERMIT },
1.7       dtucker    65:        { SYS_clock_gettime, SYSTR_POLICY_PERMIT },
1.4       djm        66:        { SYS_madvise, SYSTR_POLICY_PERMIT },
                     67:        { SYS_mmap, SYSTR_POLICY_PERMIT },
                     68:        { SYS_mprotect, SYSTR_POLICY_PERMIT },
1.5       dtucker    69:        { SYS_mquery, SYSTR_POLICY_PERMIT },
1.4       djm        70:        { SYS_poll, SYSTR_POLICY_PERMIT },
                     71:        { SYS_munmap, SYSTR_POLICY_PERMIT },
                     72:        { SYS_read, SYSTR_POLICY_PERMIT },
                     73:        { SYS_select, SYSTR_POLICY_PERMIT },
1.8       djm        74:        { SYS_shutdown, SYSTR_POLICY_PERMIT },
1.4       djm        75:        { SYS_sigprocmask, SYSTR_POLICY_PERMIT },
                     76:        { SYS_write, SYSTR_POLICY_PERMIT },
                     77:        { -1, -1 }
1.1       djm        78: };
                     79:
                     80: struct ssh_sandbox {
                     81:        int systrace_fd;
                     82:        pid_t child_pid;
1.6       markus     83:        void (*osigchld)(int);
1.1       djm        84: };
                     85:
                     86: struct ssh_sandbox *
                     87: ssh_sandbox_init(void)
                     88: {
                     89:        struct ssh_sandbox *box;
                     90:
                     91:        debug3("%s: preparing systrace sandbox", __func__);
                     92:        box = xcalloc(1, sizeof(*box));
                     93:        box->systrace_fd = -1;
                     94:        box->child_pid = 0;
1.6       markus     95:        box->osigchld = signal(SIGCHLD, SIG_IGN);
1.1       djm        96:
                     97:        return box;
                     98: }
                     99:
                    100: void
                    101: ssh_sandbox_child(struct ssh_sandbox *box)
                    102: {
                    103:        debug3("%s: ready", __func__);
1.6       markus    104:        signal(SIGCHLD, box->osigchld);
                    105:        if (kill(getpid(), SIGSTOP) != 0)
                    106:                fatal("%s: kill(%d, SIGSTOP)", __func__, getpid());
1.1       djm       107:        debug3("%s: started", __func__);
                    108: }
                    109:
                    110: static void
                    111: ssh_sandbox_parent(struct ssh_sandbox *box, pid_t child_pid,
1.4       djm       112:     const struct sandbox_policy *allowed_syscalls)
1.1       djm       113: {
1.6       markus    114:        int dev_systrace, i, j, found, status;
                    115:        pid_t pid;
1.4       djm       116:        struct systrace_policy policy;
1.1       djm       117:
1.6       markus    118:        /* Wait for the child to send itself a SIGSTOP */
1.1       djm       119:        debug3("%s: wait for child %ld", __func__, (long)child_pid);
1.6       markus    120:        do {
                    121:                pid = waitpid(child_pid, &status, WUNTRACED);
                    122:        } while (pid == -1 && errno == EINTR);
                    123:        signal(SIGCHLD, box->osigchld);
                    124:        if (!WIFSTOPPED(status)) {
                    125:                if (WIFSIGNALED(status))
                    126:                        fatal("%s: child terminated with signal %d",
                    127:                            __func__, WTERMSIG(status));
                    128:                if (WIFEXITED(status))
                    129:                        fatal("%s: child exited with status %d",
                    130:                            __func__, WEXITSTATUS(status));
                    131:                fatal("%s: child not stopped", __func__);
                    132:        }
                    133:        debug3("%s: child %ld stopped", __func__, (long)child_pid);
1.1       djm       134:        box->child_pid = child_pid;
                    135:
                    136:        /* Set up systracing of child */
                    137:        if ((dev_systrace = open("/dev/systrace", O_RDONLY)) == -1)
                    138:                fatal("%s: open(\"/dev/systrace\"): %s", __func__,
                    139:                    strerror(errno));
                    140:        if (ioctl(dev_systrace, STRIOCCLONE, &box->systrace_fd) == -1)
                    141:                fatal("%s: ioctl(STRIOCCLONE, %d): %s", __func__,
                    142:                    dev_systrace, strerror(errno));
                    143:        close(dev_systrace);
                    144:        debug3("%s: systrace attach, fd=%d", __func__, box->systrace_fd);
                    145:        if (ioctl(box->systrace_fd, STRIOCATTACH, &child_pid) == -1)
                    146:                fatal("%s: ioctl(%d, STRIOCATTACH, %d): %s", __func__,
                    147:                    box->systrace_fd, child_pid, strerror(errno));
                    148:
                    149:        /* Allocate and assign policy */
1.9       tedu      150:        memset(&policy, 0, sizeof(policy));
1.4       djm       151:        policy.strp_op = SYSTR_POLICY_NEW;
                    152:        policy.strp_maxents = SYS_MAXSYSCALL;
                    153:        if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
1.1       djm       154:                fatal("%s: ioctl(%d, STRIOCPOLICY (new)): %s", __func__,
                    155:                    box->systrace_fd, strerror(errno));
                    156:
1.4       djm       157:        policy.strp_op = SYSTR_POLICY_ASSIGN;
                    158:        policy.strp_pid = box->child_pid;
                    159:        if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
1.1       djm       160:                fatal("%s: ioctl(%d, STRIOCPOLICY (assign)): %s",
                    161:                    __func__, box->systrace_fd, strerror(errno));
                    162:
                    163:        /* Set per-syscall policy */
                    164:        for (i = 0; i < SYS_MAXSYSCALL; i++) {
1.4       djm       165:                found = 0;
                    166:                for (j = 0; allowed_syscalls[j].syscall != -1; j++) {
                    167:                        if (allowed_syscalls[j].syscall == i) {
1.1       djm       168:                                found = 1;
1.4       djm       169:                                break;
                    170:                        }
1.1       djm       171:                }
1.4       djm       172:                policy.strp_op = SYSTR_POLICY_MODIFY;
                    173:                policy.strp_code = i;
                    174:                policy.strp_policy = found ?
                    175:                    allowed_syscalls[j].action : SYSTR_POLICY_KILL;
1.1       djm       176:                if (found)
                    177:                        debug3("%s: policy: enable syscall %d", __func__, i);
1.4       djm       178:                if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
1.1       djm       179:                        fatal("%s: ioctl(%d, STRIOCPOLICY (modify)): %s",
                    180:                            __func__, box->systrace_fd, strerror(errno));
                    181:        }
                    182:
                    183:        /* Signal the child to start running */
                    184:        debug3("%s: start child %ld", __func__, (long)child_pid);
1.6       markus    185:        if (kill(box->child_pid, SIGCONT) != 0)
                    186:                fatal("%s: kill(%d, SIGCONT)", __func__, box->child_pid);
1.1       djm       187: }
                    188:
                    189: void
                    190: ssh_sandbox_parent_finish(struct ssh_sandbox *box)
                    191: {
                    192:        /* Closing this before the child exits will terminate it */
                    193:        close(box->systrace_fd);
                    194:
                    195:        free(box);
                    196:        debug3("%s: finished", __func__);
                    197: }
                    198:
                    199: void
                    200: ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
                    201: {
                    202:        ssh_sandbox_parent(box, child_pid, preauth_policy);
                    203: }