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