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