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 #ifdef SANDBOX_SECCOMP_FILTER_DEBUG
29 /* Use the kernel headers in case of an older toolchain. */
30 # include <asm/siginfo.h>
31 # define __have_siginfo_t 1
32 # define __have_sigval_t 1
33 # define __have_sigevent_t 1
34 #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
38 #ifdef SANDBOX_SECCOMP_FILTER
40 #include <sys/types.h>
41 #include <sys/resource.h>
42 #include <sys/prctl.h>
44 #include <linux/audit.h>
45 #include <linux/filter.h>
46 #include <linux/seccomp.h>
48 #include <asm/unistd.h>
53 #include <stddef.h> /* for offsetof */
60 #include "ssh-sandbox.h"
63 /* Linux seccomp_filter sandbox */
64 #define SECCOMP_FILTER_FAIL SECCOMP_RET_KILL
66 /* Use a signal handler to emit violations when debugging */
67 #ifdef SANDBOX_SECCOMP_FILTER_DEBUG
68 # undef SECCOMP_FILTER_FAIL
69 # define SECCOMP_FILTER_FAIL SECCOMP_RET_TRAP
70 #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
72 /* Simple helpers to avoid manual errors (but larger BPF programs). */
73 #define SC_DENY(_nr, _errno) \
74 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_ ## _nr, 0, 1), \
75 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ERRNO|(_errno))
76 #define SC_ALLOW(_nr) \
77 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_ ## _nr, 0, 1), \
78 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
80 /* Syscall filtering set for preauth. */
81 static const struct sock_filter preauth_insns[] = {
82 /* Ensure the syscall arch convention is as expected. */
83 BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
84 offsetof(struct seccomp_data, arch)),
85 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SECCOMP_AUDIT_ARCH, 1, 0),
86 BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL),
87 /* Load the syscall number for checking. */
88 BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
89 offsetof(struct seccomp_data, nr)),
90 SC_DENY(open, EACCES),
92 SC_ALLOW(gettimeofday),
99 #ifdef __NR__newselect
100 SC_ALLOW(_newselect),
107 SC_ALLOW(exit_group),
108 #ifdef __NR_rt_sigprocmask
109 SC_ALLOW(rt_sigprocmask),
111 SC_ALLOW(sigprocmask),
113 BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL),
116 static const struct sock_fprog preauth_program = {
117 .len = (unsigned short)(sizeof(preauth_insns)/sizeof(preauth_insns[0])),
118 .filter = (struct sock_filter *)preauth_insns,
126 ssh_sandbox_init(void)
128 struct ssh_sandbox *box;
131 * Strictly, we don't need to maintain any state here but we need
132 * to return non-NULL to satisfy the API.
134 debug3("%s: preparing seccomp filter sandbox", __func__);
135 box = xcalloc(1, sizeof(*box));
141 #ifdef SANDBOX_SECCOMP_FILTER_DEBUG
142 extern struct monitor *pmonitor;
143 void mm_log_handler(LogLevel level, const char *msg, void *ctx);
146 ssh_sandbox_violation(int signum, siginfo_t *info, void *void_context)
150 snprintf(msg, sizeof(msg),
151 "%s: unexpected system call (arch:0x%x,syscall:%d @ %p)",
152 __func__, info->si_arch, info->si_syscall, info->si_call_addr);
153 mm_log_handler(SYSLOG_LEVEL_FATAL, msg, pmonitor);
158 ssh_sandbox_child_debugging(void)
160 struct sigaction act;
163 debug3("%s: installing SIGSYS handler", __func__);
164 memset(&act, 0, sizeof(act));
166 sigaddset(&mask, SIGSYS);
168 act.sa_sigaction = &ssh_sandbox_violation;
169 act.sa_flags = SA_SIGINFO;
170 if (sigaction(SIGSYS, &act, NULL) == -1)
171 fatal("%s: sigaction(SIGSYS): %s", __func__, strerror(errno));
172 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
173 fatal("%s: sigprocmask(SIGSYS): %s",
174 __func__, strerror(errno));
176 #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
179 ssh_sandbox_child(struct ssh_sandbox *box)
181 struct rlimit rl_zero;
184 /* Set rlimits for completeness if possible. */
185 rl_zero.rlim_cur = rl_zero.rlim_max = 0;
186 if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
187 fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
188 __func__, strerror(errno));
189 if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
190 fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
191 __func__, strerror(errno));
192 if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
193 fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
194 __func__, strerror(errno));
196 #ifdef SANDBOX_SECCOMP_FILTER_DEBUG
197 ssh_sandbox_child_debugging();
198 #endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
200 debug3("%s: setting PR_SET_NO_NEW_PRIVS", __func__);
201 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) {
202 debug("%s: prctl(PR_SET_NO_NEW_PRIVS): %s",
203 __func__, strerror(errno));
206 debug3("%s: attaching seccomp filter program", __func__);
207 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &preauth_program) == -1)
208 debug("%s: prctl(PR_SET_SECCOMP): %s",
209 __func__, strerror(errno));
211 fatal("%s: SECCOMP_MODE_FILTER activated but "
212 "PR_SET_NO_NEW_PRIVS failed", __func__);
216 ssh_sandbox_parent_finish(struct ssh_sandbox *box)
219 debug3("%s: finished", __func__);
223 ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
225 box->child_pid = child_pid;
228 #endif /* SANDBOX_SECCOMP_FILTER */