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

1.4     ! djm         1: /* $OpenBSD: sandbox-systrace.c,v 1.3 2011/06/23 09:34:13 djm 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>
                     23:
                     24: #include <dev/systrace.h>
                     25:
                     26: #include <errno.h>
                     27: #include <fcntl.h>
                     28: #include <limits.h>
                     29: #include <stdarg.h>
                     30: #include <stdio.h>
                     31: #include <stdlib.h>
                     32: #include <string.h>
                     33: #include <unistd.h>
                     34:
                     35: #include "atomicio.h"
                     36: #include "log.h"
1.3       djm        37: #include "ssh-sandbox.h"
1.1       djm        38: #include "xmalloc.h"
                     39:
1.4     ! djm        40: struct sandbox_policy {
        !            41:        int syscall;
        !            42:        int action;
        !            43: };
        !            44:
        !            45: /* Permitted syscalls in preauth. Unlisted syscalls get SYSTR_POLICY_KILL */
        !            46: static const struct sandbox_policy preauth_policy[] = {
        !            47:        { SYS_open, SYSTR_POLICY_NEVER },
        !            48:
        !            49:        { SYS___sysctl, SYSTR_POLICY_PERMIT },
        !            50:        { SYS_close, SYSTR_POLICY_PERMIT },
        !            51:        { SYS_exit, SYSTR_POLICY_PERMIT },
        !            52:        { SYS_getpid, SYSTR_POLICY_PERMIT },
        !            53:        { SYS_gettimeofday, SYSTR_POLICY_PERMIT },
        !            54:        { SYS_madvise, SYSTR_POLICY_PERMIT },
        !            55:        { SYS_mmap, SYSTR_POLICY_PERMIT },
        !            56:        { SYS_mprotect, SYSTR_POLICY_PERMIT },
        !            57:        { SYS_poll, SYSTR_POLICY_PERMIT },
        !            58:        { SYS_munmap, SYSTR_POLICY_PERMIT },
        !            59:        { SYS_read, SYSTR_POLICY_PERMIT },
        !            60:        { SYS_select, SYSTR_POLICY_PERMIT },
        !            61:        { SYS_sigprocmask, SYSTR_POLICY_PERMIT },
        !            62:        { SYS_write, SYSTR_POLICY_PERMIT },
        !            63:        { -1, -1 }
1.1       djm        64: };
                     65:
                     66: struct ssh_sandbox {
                     67:        int child_sock;
                     68:        int parent_sock;
                     69:        int systrace_fd;
                     70:        pid_t child_pid;
                     71: };
                     72:
                     73: struct ssh_sandbox *
                     74: ssh_sandbox_init(void)
                     75: {
                     76:        struct ssh_sandbox *box;
                     77:        int s[2];
                     78:
                     79:        debug3("%s: preparing systrace sandbox", __func__);
                     80:        box = xcalloc(1, sizeof(*box));
                     81:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, s) == -1)
                     82:                fatal("%s: socketpair: %s", __func__, strerror(errno));
                     83:        box->child_sock = s[0];
                     84:        box->parent_sock = s[1];
                     85:        box->systrace_fd = -1;
                     86:        box->child_pid = 0;
                     87:
                     88:        return box;
                     89: }
                     90:
                     91: void
                     92: ssh_sandbox_child(struct ssh_sandbox *box)
                     93: {
                     94:        char whatever = 0;
                     95:
                     96:        close(box->parent_sock);
                     97:        /* Signal parent that we are ready */
                     98:        debug3("%s: ready", __func__);
                     99:        if (atomicio(vwrite, box->child_sock, &whatever, 1) != 1)
                    100:                fatal("%s: write: %s", __func__, strerror(errno));
                    101:        /* Wait for parent to signal for us to go */
                    102:        if (atomicio(read, box->child_sock, &whatever, 1) != 1)
                    103:                fatal("%s: read: %s", __func__, strerror(errno));
                    104:        debug3("%s: started", __func__);
                    105:        close(box->child_sock);
                    106: }
                    107:
                    108: static void
                    109: ssh_sandbox_parent(struct ssh_sandbox *box, pid_t child_pid,
1.4     ! djm       110:     const struct sandbox_policy *allowed_syscalls)
1.1       djm       111: {
                    112:        int dev_systrace, i, j, found;
                    113:        char whatever = 0;
1.4     ! djm       114:        struct systrace_policy policy;
1.1       djm       115:
                    116:        debug3("%s: wait for child %ld", __func__, (long)child_pid);
                    117:        box->child_pid = child_pid;
                    118:        close(box->child_sock);
                    119:        /* Wait for child to signal that it is ready */
                    120:        if (atomicio(read, box->parent_sock, &whatever, 1) != 1)
                    121:                fatal("%s: read: %s", __func__, strerror(errno));
                    122:        debug3("%s: child %ld ready", __func__, (long)child_pid);
                    123:
                    124:        /* Set up systracing of child */
                    125:        if ((dev_systrace = open("/dev/systrace", O_RDONLY)) == -1)
                    126:                fatal("%s: open(\"/dev/systrace\"): %s", __func__,
                    127:                    strerror(errno));
                    128:        if (ioctl(dev_systrace, STRIOCCLONE, &box->systrace_fd) == -1)
                    129:                fatal("%s: ioctl(STRIOCCLONE, %d): %s", __func__,
                    130:                    dev_systrace, strerror(errno));
                    131:        close(dev_systrace);
                    132:        debug3("%s: systrace attach, fd=%d", __func__, box->systrace_fd);
                    133:        if (ioctl(box->systrace_fd, STRIOCATTACH, &child_pid) == -1)
                    134:                fatal("%s: ioctl(%d, STRIOCATTACH, %d): %s", __func__,
                    135:                    box->systrace_fd, child_pid, strerror(errno));
                    136:
                    137:        /* Allocate and assign policy */
1.4     ! djm       138:        bzero(&policy, sizeof(policy));
        !           139:        policy.strp_op = SYSTR_POLICY_NEW;
        !           140:        policy.strp_maxents = SYS_MAXSYSCALL;
        !           141:        if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
1.1       djm       142:                fatal("%s: ioctl(%d, STRIOCPOLICY (new)): %s", __func__,
                    143:                    box->systrace_fd, strerror(errno));
                    144:
1.4     ! djm       145:        policy.strp_op = SYSTR_POLICY_ASSIGN;
        !           146:        policy.strp_pid = box->child_pid;
        !           147:        if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
1.1       djm       148:                fatal("%s: ioctl(%d, STRIOCPOLICY (assign)): %s",
                    149:                    __func__, box->systrace_fd, strerror(errno));
                    150:
                    151:        /* Set per-syscall policy */
                    152:        for (i = 0; i < SYS_MAXSYSCALL; i++) {
1.4     ! djm       153:                found = 0;
        !           154:                for (j = 0; allowed_syscalls[j].syscall != -1; j++) {
        !           155:                        if (allowed_syscalls[j].syscall == i) {
1.1       djm       156:                                found = 1;
1.4     ! djm       157:                                break;
        !           158:                        }
1.1       djm       159:                }
1.4     ! djm       160:                policy.strp_op = SYSTR_POLICY_MODIFY;
        !           161:                policy.strp_code = i;
        !           162:                policy.strp_policy = found ?
        !           163:                    allowed_syscalls[j].action : SYSTR_POLICY_KILL;
1.1       djm       164:                if (found)
                    165:                        debug3("%s: policy: enable syscall %d", __func__, i);
1.4     ! djm       166:                if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
1.1       djm       167:                        fatal("%s: ioctl(%d, STRIOCPOLICY (modify)): %s",
                    168:                            __func__, box->systrace_fd, strerror(errno));
                    169:        }
                    170:
                    171:        /* Signal the child to start running */
                    172:        debug3("%s: start child %ld", __func__, (long)child_pid);
                    173:        if (atomicio(vwrite, box->parent_sock, &whatever, 1) != 1)
                    174:                fatal("%s: write: %s", __func__, strerror(errno));
                    175:        close(box->parent_sock);
                    176: }
                    177:
                    178: void
                    179: ssh_sandbox_parent_finish(struct ssh_sandbox *box)
                    180: {
                    181:        /* Closing this before the child exits will terminate it */
                    182:        close(box->systrace_fd);
                    183:
                    184:        free(box);
                    185:        debug3("%s: finished", __func__);
                    186: }
                    187:
                    188: void
                    189: ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
                    190: {
                    191:        ssh_sandbox_parent(box, child_pid, preauth_policy);
                    192: }