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

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