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

1.5     ! dtucker     1: /* $OpenBSD: sandbox-systrace.c,v 1.4 2011/07/29 14:42:45 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 },
1.5     ! dtucker    57:        { SYS_mquery, SYSTR_POLICY_PERMIT },
1.4       djm        58:        { SYS_poll, SYSTR_POLICY_PERMIT },
                     59:        { SYS_munmap, SYSTR_POLICY_PERMIT },
                     60:        { SYS_read, SYSTR_POLICY_PERMIT },
                     61:        { SYS_select, SYSTR_POLICY_PERMIT },
                     62:        { SYS_sigprocmask, SYSTR_POLICY_PERMIT },
                     63:        { SYS_write, SYSTR_POLICY_PERMIT },
                     64:        { -1, -1 }
1.1       djm        65: };
                     66:
                     67: struct ssh_sandbox {
                     68:        int child_sock;
                     69:        int parent_sock;
                     70:        int systrace_fd;
                     71:        pid_t child_pid;
                     72: };
                     73:
                     74: struct ssh_sandbox *
                     75: ssh_sandbox_init(void)
                     76: {
                     77:        struct ssh_sandbox *box;
                     78:        int s[2];
                     79:
                     80:        debug3("%s: preparing systrace sandbox", __func__);
                     81:        box = xcalloc(1, sizeof(*box));
                     82:        if (socketpair(AF_UNIX, SOCK_STREAM, 0, s) == -1)
                     83:                fatal("%s: socketpair: %s", __func__, strerror(errno));
                     84:        box->child_sock = s[0];
                     85:        box->parent_sock = s[1];
                     86:        box->systrace_fd = -1;
                     87:        box->child_pid = 0;
                     88:
                     89:        return box;
                     90: }
                     91:
                     92: void
                     93: ssh_sandbox_child(struct ssh_sandbox *box)
                     94: {
                     95:        char whatever = 0;
                     96:
                     97:        close(box->parent_sock);
                     98:        /* Signal parent that we are ready */
                     99:        debug3("%s: ready", __func__);
                    100:        if (atomicio(vwrite, box->child_sock, &whatever, 1) != 1)
                    101:                fatal("%s: write: %s", __func__, strerror(errno));
                    102:        /* Wait for parent to signal for us to go */
                    103:        if (atomicio(read, box->child_sock, &whatever, 1) != 1)
                    104:                fatal("%s: read: %s", __func__, strerror(errno));
                    105:        debug3("%s: started", __func__);
                    106:        close(box->child_sock);
                    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: {
                    113:        int dev_systrace, i, j, found;
                    114:        char whatever = 0;
1.4       djm       115:        struct systrace_policy policy;
1.1       djm       116:
                    117:        debug3("%s: wait for child %ld", __func__, (long)child_pid);
                    118:        box->child_pid = child_pid;
                    119:        close(box->child_sock);
                    120:        /* Wait for child to signal that it is ready */
                    121:        if (atomicio(read, box->parent_sock, &whatever, 1) != 1)
                    122:                fatal("%s: read: %s", __func__, strerror(errno));
                    123:        debug3("%s: child %ld ready", __func__, (long)child_pid);
                    124:
                    125:        /* Set up systracing of child */
                    126:        if ((dev_systrace = open("/dev/systrace", O_RDONLY)) == -1)
                    127:                fatal("%s: open(\"/dev/systrace\"): %s", __func__,
                    128:                    strerror(errno));
                    129:        if (ioctl(dev_systrace, STRIOCCLONE, &box->systrace_fd) == -1)
                    130:                fatal("%s: ioctl(STRIOCCLONE, %d): %s", __func__,
                    131:                    dev_systrace, strerror(errno));
                    132:        close(dev_systrace);
                    133:        debug3("%s: systrace attach, fd=%d", __func__, box->systrace_fd);
                    134:        if (ioctl(box->systrace_fd, STRIOCATTACH, &child_pid) == -1)
                    135:                fatal("%s: ioctl(%d, STRIOCATTACH, %d): %s", __func__,
                    136:                    box->systrace_fd, child_pid, strerror(errno));
                    137:
                    138:        /* Allocate and assign policy */
1.4       djm       139:        bzero(&policy, sizeof(policy));
                    140:        policy.strp_op = SYSTR_POLICY_NEW;
                    141:        policy.strp_maxents = SYS_MAXSYSCALL;
                    142:        if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
1.1       djm       143:                fatal("%s: ioctl(%d, STRIOCPOLICY (new)): %s", __func__,
                    144:                    box->systrace_fd, strerror(errno));
                    145:
1.4       djm       146:        policy.strp_op = SYSTR_POLICY_ASSIGN;
                    147:        policy.strp_pid = box->child_pid;
                    148:        if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
1.1       djm       149:                fatal("%s: ioctl(%d, STRIOCPOLICY (assign)): %s",
                    150:                    __func__, box->systrace_fd, strerror(errno));
                    151:
                    152:        /* Set per-syscall policy */
                    153:        for (i = 0; i < SYS_MAXSYSCALL; i++) {
1.4       djm       154:                found = 0;
                    155:                for (j = 0; allowed_syscalls[j].syscall != -1; j++) {
                    156:                        if (allowed_syscalls[j].syscall == i) {
1.1       djm       157:                                found = 1;
1.4       djm       158:                                break;
                    159:                        }
1.1       djm       160:                }
1.4       djm       161:                policy.strp_op = SYSTR_POLICY_MODIFY;
                    162:                policy.strp_code = i;
                    163:                policy.strp_policy = found ?
                    164:                    allowed_syscalls[j].action : SYSTR_POLICY_KILL;
1.1       djm       165:                if (found)
                    166:                        debug3("%s: policy: enable syscall %d", __func__, i);
1.4       djm       167:                if (ioctl(box->systrace_fd, STRIOCPOLICY, &policy) == -1)
1.1       djm       168:                        fatal("%s: ioctl(%d, STRIOCPOLICY (modify)): %s",
                    169:                            __func__, box->systrace_fd, strerror(errno));
                    170:        }
                    171:
                    172:        /* Signal the child to start running */
                    173:        debug3("%s: start child %ld", __func__, (long)child_pid);
                    174:        if (atomicio(vwrite, box->parent_sock, &whatever, 1) != 1)
                    175:                fatal("%s: write: %s", __func__, strerror(errno));
                    176:        close(box->parent_sock);
                    177: }
                    178:
                    179: void
                    180: ssh_sandbox_parent_finish(struct ssh_sandbox *box)
                    181: {
                    182:        /* Closing this before the child exits will terminate it */
                    183:        close(box->systrace_fd);
                    184:
                    185:        free(box);
                    186:        debug3("%s: finished", __func__);
                    187: }
                    188:
                    189: void
                    190: ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
                    191: {
                    192:        ssh_sandbox_parent(box, child_pid, preauth_policy);
                    193: }