blob: 3a1aedce72c2b04204132c9fc397941ba18491dd [file] [log] [blame]
Adam Langleyd0592972015-03-30 14:49:51 -07001/*
2 * Copyright (c) 2012 Will Drewry <wad@dataspill.org>
3 *
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.
7 *
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.
15 */
16
17/*
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.
21 *
22 * Instead, live systems the auditctl(8) may be used to monitor failures.
23 * E.g.
24 * auditctl -a task,always -F uid=<privsep uid>
25 */
26/* #define SANDBOX_SECCOMP_FILTER_DEBUG 1 */
27
28/* XXX it should be possible to do logging via the log socket safely */
29
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 */
37
38#include "includes.h"
39
40#ifdef SANDBOX_SECCOMP_FILTER
41
42#include <sys/types.h>
43#include <sys/resource.h>
44#include <sys/prctl.h>
45
Greg Hartmanccacbc92016-02-03 09:59:44 -080046#include <linux/net.h>
Adam Langleyd0592972015-03-30 14:49:51 -070047#include <linux/audit.h>
48#include <linux/filter.h>
49#include <linux/seccomp.h>
50#include <elf.h>
51
52#include <asm/unistd.h>
53
54#include <errno.h>
55#include <signal.h>
56#include <stdarg.h>
57#include <stddef.h> /* for offsetof */
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include <unistd.h>
62
63#include "log.h"
64#include "ssh-sandbox.h"
65#include "xmalloc.h"
66
67/* Linux seccomp_filter sandbox */
68#define SECCOMP_FILTER_FAIL SECCOMP_RET_KILL
69
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 */
75
Greg Hartman9768ca42017-06-22 20:49:52 -070076#if __BYTE_ORDER == __LITTLE_ENDIAN
77# define ARG_LO_OFFSET 0
78# define ARG_HI_OFFSET sizeof(uint32_t)
79#elif __BYTE_ORDER == __BIG_ENDIAN
80# define ARG_LO_OFFSET sizeof(uint32_t)
81# define ARG_HI_OFFSET 0
82#else
83#error "Unknown endianness"
84#endif
85
Adam Langleyd0592972015-03-30 14:49:51 -070086/* Simple helpers to avoid manual errors (but larger BPF programs). */
87#define SC_DENY(_nr, _errno) \
Greg Hartman9768ca42017-06-22 20:49:52 -070088 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 1), \
Adam Langleyd0592972015-03-30 14:49:51 -070089 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ERRNO|(_errno))
90#define SC_ALLOW(_nr) \
Greg Hartman9768ca42017-06-22 20:49:52 -070091 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 1), \
Adam Langleyd0592972015-03-30 14:49:51 -070092 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
Greg Hartmanccacbc92016-02-03 09:59:44 -080093#define SC_ALLOW_ARG(_nr, _arg_nr, _arg_val) \
Greg Hartman9768ca42017-06-22 20:49:52 -070094 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 6), \
95 /* load and test first syscall argument, low word */ \
Greg Hartmanccacbc92016-02-03 09:59:44 -080096 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
Greg Hartman9768ca42017-06-22 20:49:52 -070097 offsetof(struct seccomp_data, args[(_arg_nr)]) + ARG_LO_OFFSET), \
98 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, \
99 ((_arg_val) & 0xFFFFFFFF), 0, 3), \
100 /* load and test first syscall argument, high word */ \
101 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
102 offsetof(struct seccomp_data, args[(_arg_nr)]) + ARG_HI_OFFSET), \
103 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, \
104 (((uint32_t)((uint64_t)(_arg_val) >> 32)) & 0xFFFFFFFF), 0, 1), \
Greg Hartmanccacbc92016-02-03 09:59:44 -0800105 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW), \
106 /* reload syscall number; all rules expect it in accumulator */ \
107 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
108 offsetof(struct seccomp_data, nr))
Adam Langleyd0592972015-03-30 14:49:51 -0700109
110/* Syscall filtering set for preauth. */
111static const struct sock_filter preauth_insns[] = {
112 /* Ensure the syscall arch convention is as expected. */
113 BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
114 offsetof(struct seccomp_data, arch)),
115 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SECCOMP_AUDIT_ARCH, 1, 0),
116 BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL),
117 /* Load the syscall number for checking. */
118 BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
119 offsetof(struct seccomp_data, nr)),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800120
121 /* Syscalls to non-fatally deny */
Greg Hartman9768ca42017-06-22 20:49:52 -0700122#ifdef __NR_lstat
123 SC_DENY(__NR_lstat, EACCES),
124#endif
125#ifdef __NR_lstat64
126 SC_DENY(__NR_lstat64, EACCES),
127#endif
Greg Hartmanccacbc92016-02-03 09:59:44 -0800128#ifdef __NR_fstat
Greg Hartman9768ca42017-06-22 20:49:52 -0700129 SC_DENY(__NR_fstat, EACCES),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800130#endif
131#ifdef __NR_fstat64
Greg Hartman9768ca42017-06-22 20:49:52 -0700132 SC_DENY(__NR_fstat64, EACCES),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800133#endif
134#ifdef __NR_open
Greg Hartman9768ca42017-06-22 20:49:52 -0700135 SC_DENY(__NR_open, EACCES),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800136#endif
137#ifdef __NR_openat
Greg Hartman9768ca42017-06-22 20:49:52 -0700138 SC_DENY(__NR_openat, EACCES),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800139#endif
140#ifdef __NR_newfstatat
Greg Hartman9768ca42017-06-22 20:49:52 -0700141 SC_DENY(__NR_newfstatat, EACCES),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800142#endif
143#ifdef __NR_stat
Greg Hartman9768ca42017-06-22 20:49:52 -0700144 SC_DENY(__NR_stat, EACCES),
Adam Langleyd0592972015-03-30 14:49:51 -0700145#endif
Greg Hartmanccacbc92016-02-03 09:59:44 -0800146#ifdef __NR_stat64
Greg Hartman9768ca42017-06-22 20:49:52 -0700147 SC_DENY(__NR_stat64, EACCES),
Adam Langleyd0592972015-03-30 14:49:51 -0700148#endif
Greg Hartmanccacbc92016-02-03 09:59:44 -0800149
150 /* Syscalls to permit */
151#ifdef __NR_brk
Greg Hartman9768ca42017-06-22 20:49:52 -0700152 SC_ALLOW(__NR_brk),
Adam Langleyd0592972015-03-30 14:49:51 -0700153#endif
Greg Hartmanccacbc92016-02-03 09:59:44 -0800154#ifdef __NR_clock_gettime
Greg Hartman9768ca42017-06-22 20:49:52 -0700155 SC_ALLOW(__NR_clock_gettime),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800156#endif
157#ifdef __NR_close
Greg Hartman9768ca42017-06-22 20:49:52 -0700158 SC_ALLOW(__NR_close),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800159#endif
160#ifdef __NR_exit
Greg Hartman9768ca42017-06-22 20:49:52 -0700161 SC_ALLOW(__NR_exit),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800162#endif
163#ifdef __NR_exit_group
Greg Hartman9768ca42017-06-22 20:49:52 -0700164 SC_ALLOW(__NR_exit_group),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800165#endif
166#ifdef __NR_getpgid
Greg Hartman9768ca42017-06-22 20:49:52 -0700167 SC_ALLOW(__NR_getpgid),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800168#endif
169#ifdef __NR_getpid
Greg Hartman9768ca42017-06-22 20:49:52 -0700170 SC_ALLOW(__NR_getpid),
171#endif
172#ifdef __NR_getrandom
173 SC_ALLOW(__NR_getrandom),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800174#endif
175#ifdef __NR_gettimeofday
Greg Hartman9768ca42017-06-22 20:49:52 -0700176 SC_ALLOW(__NR_gettimeofday),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800177#endif
178#ifdef __NR_madvise
Greg Hartman9768ca42017-06-22 20:49:52 -0700179 SC_ALLOW(__NR_madvise),
Adam Langleyd0592972015-03-30 14:49:51 -0700180#endif
181#ifdef __NR_mmap
Greg Hartman9768ca42017-06-22 20:49:52 -0700182 SC_ALLOW(__NR_mmap),
Adam Langleyd0592972015-03-30 14:49:51 -0700183#endif
Greg Hartmanccacbc92016-02-03 09:59:44 -0800184#ifdef __NR_mmap2
Greg Hartman9768ca42017-06-22 20:49:52 -0700185 SC_ALLOW(__NR_mmap2),
Adam Langleyd0592972015-03-30 14:49:51 -0700186#endif
Greg Hartmanccacbc92016-02-03 09:59:44 -0800187#ifdef __NR_mremap
Greg Hartman9768ca42017-06-22 20:49:52 -0700188 SC_ALLOW(__NR_mremap),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800189#endif
190#ifdef __NR_munmap
Greg Hartman9768ca42017-06-22 20:49:52 -0700191 SC_ALLOW(__NR_munmap),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800192#endif
193#ifdef __NR__newselect
Greg Hartman9768ca42017-06-22 20:49:52 -0700194 SC_ALLOW(__NR__newselect),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800195#endif
196#ifdef __NR_poll
Greg Hartman9768ca42017-06-22 20:49:52 -0700197 SC_ALLOW(__NR_poll),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800198#endif
199#ifdef __NR_pselect6
Greg Hartman9768ca42017-06-22 20:49:52 -0700200 SC_ALLOW(__NR_pselect6),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800201#endif
202#ifdef __NR_read
Greg Hartman9768ca42017-06-22 20:49:52 -0700203 SC_ALLOW(__NR_read),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800204#endif
Adam Langleyd0592972015-03-30 14:49:51 -0700205#ifdef __NR_rt_sigprocmask
Greg Hartman9768ca42017-06-22 20:49:52 -0700206 SC_ALLOW(__NR_rt_sigprocmask),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800207#endif
208#ifdef __NR_select
Greg Hartman9768ca42017-06-22 20:49:52 -0700209 SC_ALLOW(__NR_select),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800210#endif
211#ifdef __NR_shutdown
Greg Hartman9768ca42017-06-22 20:49:52 -0700212 SC_ALLOW(__NR_shutdown),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800213#endif
214#ifdef __NR_sigprocmask
Greg Hartman9768ca42017-06-22 20:49:52 -0700215 SC_ALLOW(__NR_sigprocmask),
Adam Langleyd0592972015-03-30 14:49:51 -0700216#endif
Greg Hartmanccacbc92016-02-03 09:59:44 -0800217#ifdef __NR_time
Greg Hartman9768ca42017-06-22 20:49:52 -0700218 SC_ALLOW(__NR_time),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800219#endif
220#ifdef __NR_write
Greg Hartman9768ca42017-06-22 20:49:52 -0700221 SC_ALLOW(__NR_write),
Greg Hartmanccacbc92016-02-03 09:59:44 -0800222#endif
223#ifdef __NR_socketcall
Greg Hartman9768ca42017-06-22 20:49:52 -0700224 SC_ALLOW_ARG(__NR_socketcall, 0, SYS_SHUTDOWN),
225#endif
226#if defined(__NR_ioctl) && defined(__s390__)
227 /* Allow ioctls for ICA crypto card on s390 */
228 SC_ALLOW_ARG(__NR_ioctl, 1, Z90STAT_STATUS_MASK),
229 SC_ALLOW_ARG(__NR_ioctl, 1, ICARSAMODEXPO),
230 SC_ALLOW_ARG(__NR_ioctl, 1, ICARSACRT),
231#endif
232#if defined(__x86_64__) && defined(__ILP32__) && defined(__X32_SYSCALL_BIT)
233 /*
234 * On Linux x32, the clock_gettime VDSO falls back to the
235 * x86-64 syscall under some circumstances, e.g.
236 * https://bugs.debian.org/849923
237 */
238 SC_ALLOW(__NR_clock_gettime & ~__X32_SYSCALL_BIT);
Greg Hartmanccacbc92016-02-03 09:59:44 -0800239#endif
240
241 /* Default deny */
Adam Langleyd0592972015-03-30 14:49:51 -0700242 BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL),
243};
244
245static const struct sock_fprog preauth_program = {
246 .len = (unsigned short)(sizeof(preauth_insns)/sizeof(preauth_insns[0])),
247 .filter = (struct sock_filter *)preauth_insns,
248};
249
250struct ssh_sandbox {
251 pid_t child_pid;
252};
253
254struct ssh_sandbox *
255ssh_sandbox_init(struct monitor *monitor)
256{
257 struct ssh_sandbox *box;
258
259 /*
260 * Strictly, we don't need to maintain any state here but we need
261 * to return non-NULL to satisfy the API.
262 */
263 debug3("%s: preparing seccomp filter sandbox", __func__);
264 box = xcalloc(1, sizeof(*box));
265 box->child_pid = 0;
266
267 return box;
268}
269
270#ifdef SANDBOX_SECCOMP_FILTER_DEBUG
271extern struct monitor *pmonitor;
272void mm_log_handler(LogLevel level, const char *msg, void *ctx);
273
274static void
275ssh_sandbox_violation(int signum, siginfo_t *info, void *void_context)
276{
277 char msg[256];
278
279 snprintf(msg, sizeof(msg),
280 "%s: unexpected system call (arch:0x%x,syscall:%d @ %p)",
281 __func__, info->si_arch, info->si_syscall, info->si_call_addr);
282 mm_log_handler(SYSLOG_LEVEL_FATAL, msg, pmonitor);
283 _exit(1);
284}
285
286static void
287ssh_sandbox_child_debugging(void)
288{
289 struct sigaction act;
290 sigset_t mask;
291
292 debug3("%s: installing SIGSYS handler", __func__);
293 memset(&act, 0, sizeof(act));
294 sigemptyset(&mask);
295 sigaddset(&mask, SIGSYS);
296
297 act.sa_sigaction = &ssh_sandbox_violation;
298 act.sa_flags = SA_SIGINFO;
299 if (sigaction(SIGSYS, &act, NULL) == -1)
300 fatal("%s: sigaction(SIGSYS): %s", __func__, strerror(errno));
301 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
302 fatal("%s: sigprocmask(SIGSYS): %s",
303 __func__, strerror(errno));
304}
305#endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
306
307void
308ssh_sandbox_child(struct ssh_sandbox *box)
309{
310 struct rlimit rl_zero;
311 int nnp_failed = 0;
312
313 /* Set rlimits for completeness if possible. */
314 rl_zero.rlim_cur = rl_zero.rlim_max = 0;
315 if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
316 fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
317 __func__, strerror(errno));
318 if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
319 fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
320 __func__, strerror(errno));
321 if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
322 fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
323 __func__, strerror(errno));
324
325#ifdef SANDBOX_SECCOMP_FILTER_DEBUG
326 ssh_sandbox_child_debugging();
327#endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
328
329 debug3("%s: setting PR_SET_NO_NEW_PRIVS", __func__);
330 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) {
331 debug("%s: prctl(PR_SET_NO_NEW_PRIVS): %s",
332 __func__, strerror(errno));
333 nnp_failed = 1;
334 }
335 debug3("%s: attaching seccomp filter program", __func__);
336 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &preauth_program) == -1)
337 debug("%s: prctl(PR_SET_SECCOMP): %s",
338 __func__, strerror(errno));
339 else if (nnp_failed)
340 fatal("%s: SECCOMP_MODE_FILTER activated but "
341 "PR_SET_NO_NEW_PRIVS failed", __func__);
342}
343
344void
345ssh_sandbox_parent_finish(struct ssh_sandbox *box)
346{
347 free(box);
348 debug3("%s: finished", __func__);
349}
350
351void
352ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
353{
354 box->child_pid = child_pid;
355}
356
357#endif /* SANDBOX_SECCOMP_FILTER */