2 * Copyright (c) 2012 Will Drewry <wad@dataspill.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Uncomment the SANDBOX_SECCOMP_FILTER_DEBUG macro below to help diagnose
19 * filter breakage during development. *Do not* use this in production,
20 * as it relies on making library calls that are unsafe in signal context.
22 * Instead, live systems the auditctl(8) may be used to monitor failures.
24 * auditctl -a task,always -F uid=<privsep uid>
26 /* #define SANDBOX_SECCOMP_FILTER_DEBUG 1 */
28 /* XXX it should be possible to do logging via the log socket safely */
30 #ifdef SANDBOX_SECCOMP_FILTER_DEBUG
31 /* Use the kernel headers in case of an older toolchain. */
32 # include <asm/siginfo.h>
33 # define __have_siginfo_t 1
34 # define __have_sigval_t 1
35 # define __have_sigevent_t 1
36 #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
40 #ifdef SANDBOX_SECCOMP_FILTER
42 #include <sys/types.h>
43 #include <sys/resource.h>
44 #include <sys/prctl.h>
46 #include <linux/net.h>
47 #include <linux/audit.h>
48 #include <linux/filter.h>
49 #include <linux/seccomp.h>
52 #include <asm/unistd.h>
57 #include <stddef.h> /* for offsetof */
64 #include "ssh-sandbox.h"
67 /* Linux seccomp_filter sandbox */
68 #define SECCOMP_FILTER_FAIL SECCOMP_RET_KILL
70 /* Use a signal handler to emit violations when debugging */
71 #ifdef SANDBOX_SECCOMP_FILTER_DEBUG
72 # undef SECCOMP_FILTER_FAIL
73 # define SECCOMP_FILTER_FAIL SECCOMP_RET_TRAP
74 #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
76 /* Simple helpers to avoid manual errors (but larger BPF programs). */
77 #define SC_DENY(_nr, _errno) \
78 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_ ## _nr, 0, 1), \
79 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ERRNO|(_errno))
80 #define SC_ALLOW(_nr) \
81 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_ ## _nr, 0, 1), \
82 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
83 #define SC_ALLOW_ARG(_nr, _arg_nr, _arg_val) \
84 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_ ## _nr, 0, 4), \
85 /* load first syscall argument */ \
86 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
87 offsetof(struct seccomp_data, args[(_arg_nr)])), \
88 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_arg_val), 0, 1), \
89 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW), \
90 /* reload syscall number; all rules expect it in accumulator */ \
91 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
92 offsetof(struct seccomp_data, nr))
94 /* Syscall filtering set for preauth. */
95 static const struct sock_filter preauth_insns[] = {
96 /* Ensure the syscall arch convention is as expected. */
97 BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
98 offsetof(struct seccomp_data, arch)),
99 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SECCOMP_AUDIT_ARCH, 1, 0),
100 BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL),
101 /* Load the syscall number for checking. */
102 BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
103 offsetof(struct seccomp_data, nr)),
105 /* Syscalls to non-fatally deny */
107 SC_DENY(fstat, EACCES),
110 SC_DENY(fstat64, EACCES),
113 SC_DENY(open, EACCES),
116 SC_DENY(openat, EACCES),
118 #ifdef __NR_newfstatat
119 SC_DENY(newfstatat, EACCES),
122 SC_DENY(stat, EACCES),
125 SC_DENY(stat64, EACCES),
128 /* Syscalls to permit */
132 #ifdef __NR_clock_gettime
133 SC_ALLOW(clock_gettime),
141 #ifdef __NR_exit_group
142 SC_ALLOW(exit_group),
150 #ifdef __NR_gettimeofday
151 SC_ALLOW(gettimeofday),
168 #ifdef __NR__newselect
169 SC_ALLOW(_newselect),
180 #ifdef __NR_rt_sigprocmask
181 SC_ALLOW(rt_sigprocmask),
189 #ifdef __NR_sigprocmask
190 SC_ALLOW(sigprocmask),
198 #ifdef __NR_socketcall
199 SC_ALLOW_ARG(socketcall, 0, SYS_SHUTDOWN),
203 BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL),
206 static const struct sock_fprog preauth_program = {
207 .len = (unsigned short)(sizeof(preauth_insns)/sizeof(preauth_insns[0])),
208 .filter = (struct sock_filter *)preauth_insns,
216 ssh_sandbox_init(struct monitor *monitor)
218 struct ssh_sandbox *box;
221 * Strictly, we don't need to maintain any state here but we need
222 * to return non-NULL to satisfy the API.
224 debug3("%s: preparing seccomp filter sandbox", __func__);
225 box = xcalloc(1, sizeof(*box));
231 #ifdef SANDBOX_SECCOMP_FILTER_DEBUG
232 extern struct monitor *pmonitor;
233 void mm_log_handler(LogLevel level, const char *msg, void *ctx);
236 ssh_sandbox_violation(int signum, siginfo_t *info, void *void_context)
240 snprintf(msg, sizeof(msg),
241 "%s: unexpected system call (arch:0x%x,syscall:%d @ %p)",
242 __func__, info->si_arch, info->si_syscall, info->si_call_addr);
243 mm_log_handler(SYSLOG_LEVEL_FATAL, msg, pmonitor);
248 ssh_sandbox_child_debugging(void)
250 struct sigaction act;
253 debug3("%s: installing SIGSYS handler", __func__);
254 memset(&act, 0, sizeof(act));
256 sigaddset(&mask, SIGSYS);
258 act.sa_sigaction = &ssh_sandbox_violation;
259 act.sa_flags = SA_SIGINFO;
260 if (sigaction(SIGSYS, &act, NULL) == -1)
261 fatal("%s: sigaction(SIGSYS): %s", __func__, strerror(errno));
262 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
263 fatal("%s: sigprocmask(SIGSYS): %s",
264 __func__, strerror(errno));
266 #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
269 ssh_sandbox_child(struct ssh_sandbox *box)
271 struct rlimit rl_zero;
274 /* Set rlimits for completeness if possible. */
275 rl_zero.rlim_cur = rl_zero.rlim_max = 0;
276 if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
277 fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
278 __func__, strerror(errno));
279 if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
280 fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
281 __func__, strerror(errno));
282 if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
283 fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
284 __func__, strerror(errno));
286 #ifdef SANDBOX_SECCOMP_FILTER_DEBUG
287 ssh_sandbox_child_debugging();
288 #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
290 debug3("%s: setting PR_SET_NO_NEW_PRIVS", __func__);
291 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) {
292 debug("%s: prctl(PR_SET_NO_NEW_PRIVS): %s",
293 __func__, strerror(errno));
296 debug3("%s: attaching seccomp filter program", __func__);
297 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &preauth_program) == -1)
298 debug("%s: prctl(PR_SET_SECCOMP): %s",
299 __func__, strerror(errno));
301 fatal("%s: SECCOMP_MODE_FILTER activated but "
302 "PR_SET_NO_NEW_PRIVS failed", __func__);
306 ssh_sandbox_parent_finish(struct ssh_sandbox *box)
309 debug3("%s: finished", __func__);
313 ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
315 box->child_pid = child_pid;
318 #endif /* SANDBOX_SECCOMP_FILTER */