blob: ca75cc719b18f0a355638277d5276363370bd674 [file] [log] [blame]
Damien Millere0956e32012-04-04 11:27:54 +10001/*
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
Damien Miller48abc472014-03-17 14:45:56 +110028/* XXX it should be possible to do logging via the log socket safely */
29
Damien Millere0956e32012-04-04 11:27:54 +100030#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
Damien Miller99f33d72015-06-17 10:50:51 +100046#include <linux/net.h>
Damien Millere0956e32012-04-04 11:27:54 +100047#include <linux/audit.h>
48#include <linux/filter.h>
49#include <linux/seccomp.h>
Damien Miller91f40d82013-02-22 11:37:00 +110050#include <elf.h>
Damien Millere0956e32012-04-04 11:27:54 +100051
52#include <asm/unistd.h>
Damien Miller58b8cfa2017-03-22 12:43:02 +110053#ifdef __s390__
54#include <asm/zcrypt.h>
55#endif
Damien Millere0956e32012-04-04 11:27:54 +100056
57#include <errno.h>
58#include <signal.h>
59#include <stdarg.h>
60#include <stddef.h> /* for offsetof */
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <unistd.h>
65
66#include "log.h"
67#include "ssh-sandbox.h"
68#include "xmalloc.h"
69
70/* Linux seccomp_filter sandbox */
71#define SECCOMP_FILTER_FAIL SECCOMP_RET_KILL
72
73/* Use a signal handler to emit violations when debugging */
74#ifdef SANDBOX_SECCOMP_FILTER_DEBUG
75# undef SECCOMP_FILTER_FAIL
76# define SECCOMP_FILTER_FAIL SECCOMP_RET_TRAP
77#endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
78
Damien Miller9e96b412017-03-14 12:24:47 +110079#if __BYTE_ORDER == __LITTLE_ENDIAN
80# define ARG_LO_OFFSET 0
81# define ARG_HI_OFFSET sizeof(uint32_t)
82#elif __BYTE_ORDER == __BIG_ENDIAN
83# define ARG_LO_OFFSET sizeof(uint32_t)
84# define ARG_HI_OFFSET 0
85#else
86#error "Unknown endianness"
87#endif
88
Damien Millere0956e32012-04-04 11:27:54 +100089/* Simple helpers to avoid manual errors (but larger BPF programs). */
90#define SC_DENY(_nr, _errno) \
Damien Millere3ea3352017-03-14 17:48:43 +110091 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 1), \
Damien Millere0956e32012-04-04 11:27:54 +100092 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ERRNO|(_errno))
93#define SC_ALLOW(_nr) \
Damien Millere3ea3352017-03-14 17:48:43 +110094 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 1), \
Damien Millere0956e32012-04-04 11:27:54 +100095 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW)
Damien Miller99f33d72015-06-17 10:50:51 +100096#define SC_ALLOW_ARG(_nr, _arg_nr, _arg_val) \
Damien Millere3ea3352017-03-14 17:48:43 +110097 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, (_nr), 0, 6), \
Damien Miller9e96b412017-03-14 12:24:47 +110098 /* load and test first syscall argument, low word */ \
Damien Miller99f33d72015-06-17 10:50:51 +100099 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
Damien Miller9e96b412017-03-14 12:24:47 +1100100 offsetof(struct seccomp_data, args[(_arg_nr)]) + ARG_LO_OFFSET), \
101 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, \
102 ((_arg_val) & 0xFFFFFFFF), 0, 3), \
103 /* load and test first syscall argument, high word */ \
104 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
105 offsetof(struct seccomp_data, args[(_arg_nr)]) + ARG_HI_OFFSET), \
106 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, \
107 (((uint32_t)((uint64_t)(_arg_val) >> 32)) & 0xFFFFFFFF), 0, 1), \
Damien Miller99f33d72015-06-17 10:50:51 +1000108 BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW), \
109 /* reload syscall number; all rules expect it in accumulator */ \
110 BPF_STMT(BPF_LD+BPF_W+BPF_ABS, \
111 offsetof(struct seccomp_data, nr))
Damien Millere0956e32012-04-04 11:27:54 +1000112
113/* Syscall filtering set for preauth. */
114static const struct sock_filter preauth_insns[] = {
115 /* Ensure the syscall arch convention is as expected. */
116 BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
117 offsetof(struct seccomp_data, arch)),
118 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, SECCOMP_AUDIT_ARCH, 1, 0),
119 BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL),
120 /* Load the syscall number for checking. */
121 BPF_STMT(BPF_LD+BPF_W+BPF_ABS,
122 offsetof(struct seccomp_data, nr)),
Damien Miller99f33d72015-06-17 10:50:51 +1000123
124 /* Syscalls to non-fatally deny */
Damien Millerf64062b2016-05-20 09:56:53 +1000125#ifdef __NR_lstat
Damien Millere3ea3352017-03-14 17:48:43 +1100126 SC_DENY(__NR_lstat, EACCES),
Damien Millerf64062b2016-05-20 09:56:53 +1000127#endif
128#ifdef __NR_lstat64
Damien Millere3ea3352017-03-14 17:48:43 +1100129 SC_DENY(__NR_lstat64, EACCES),
Damien Millerf64062b2016-05-20 09:56:53 +1000130#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000131#ifdef __NR_fstat
Damien Millere3ea3352017-03-14 17:48:43 +1100132 SC_DENY(__NR_fstat, EACCES),
Damien Miller99f33d72015-06-17 10:50:51 +1000133#endif
134#ifdef __NR_fstat64
Damien Millere3ea3352017-03-14 17:48:43 +1100135 SC_DENY(__NR_fstat64, EACCES),
Damien Miller99f33d72015-06-17 10:50:51 +1000136#endif
137#ifdef __NR_open
Damien Millere3ea3352017-03-14 17:48:43 +1100138 SC_DENY(__NR_open, EACCES),
Damien Miller99f33d72015-06-17 10:50:51 +1000139#endif
140#ifdef __NR_openat
Damien Millere3ea3352017-03-14 17:48:43 +1100141 SC_DENY(__NR_openat, EACCES),
Damien Miller99f33d72015-06-17 10:50:51 +1000142#endif
143#ifdef __NR_newfstatat
Damien Millere3ea3352017-03-14 17:48:43 +1100144 SC_DENY(__NR_newfstatat, EACCES),
Damien Miller99f33d72015-06-17 10:50:51 +1000145#endif
146#ifdef __NR_stat
Damien Millere3ea3352017-03-14 17:48:43 +1100147 SC_DENY(__NR_stat, EACCES),
Damien Miller91f40d82013-02-22 11:37:00 +1100148#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000149#ifdef __NR_stat64
Damien Millere3ea3352017-03-14 17:48:43 +1100150 SC_DENY(__NR_stat64, EACCES),
Damien Miller6434cb22014-02-06 11:17:50 +1100151#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000152
153 /* Syscalls to permit */
154#ifdef __NR_brk
Damien Millere3ea3352017-03-14 17:48:43 +1100155 SC_ALLOW(__NR_brk),
Damien Millere0956e32012-04-04 11:27:54 +1000156#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000157#ifdef __NR_clock_gettime
Damien Millere3ea3352017-03-14 17:48:43 +1100158 SC_ALLOW(__NR_clock_gettime),
Damien Miller99f33d72015-06-17 10:50:51 +1000159#endif
160#ifdef __NR_close
Damien Millere3ea3352017-03-14 17:48:43 +1100161 SC_ALLOW(__NR_close),
Damien Miller99f33d72015-06-17 10:50:51 +1000162#endif
163#ifdef __NR_exit
Damien Millere3ea3352017-03-14 17:48:43 +1100164 SC_ALLOW(__NR_exit),
Damien Miller99f33d72015-06-17 10:50:51 +1000165#endif
166#ifdef __NR_exit_group
Damien Millere3ea3352017-03-14 17:48:43 +1100167 SC_ALLOW(__NR_exit_group),
Damien Miller99f33d72015-06-17 10:50:51 +1000168#endif
djm@openbsd.org512cadd2015-06-29 22:35:12 +0000169#ifdef __NR_getpgid
Damien Millere3ea3352017-03-14 17:48:43 +1100170 SC_ALLOW(__NR_getpgid),
djm@openbsd.org512cadd2015-06-29 22:35:12 +0000171#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000172#ifdef __NR_getpid
Damien Millere3ea3352017-03-14 17:48:43 +1100173 SC_ALLOW(__NR_getpid),
Damien Miller99f33d72015-06-17 10:50:51 +1000174#endif
Damien Miller26ad1822015-09-10 10:57:41 +1000175#ifdef __NR_getrandom
Damien Millere3ea3352017-03-14 17:48:43 +1100176 SC_ALLOW(__NR_getrandom),
Damien Miller26ad1822015-09-10 10:57:41 +1000177#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000178#ifdef __NR_gettimeofday
Damien Millere3ea3352017-03-14 17:48:43 +1100179 SC_ALLOW(__NR_gettimeofday),
Damien Miller99f33d72015-06-17 10:50:51 +1000180#endif
181#ifdef __NR_madvise
Damien Millere3ea3352017-03-14 17:48:43 +1100182 SC_ALLOW(__NR_madvise),
Damien Miller91f40d82013-02-22 11:37:00 +1100183#endif
184#ifdef __NR_mmap
Damien Millere3ea3352017-03-14 17:48:43 +1100185 SC_ALLOW(__NR_mmap),
Damien Miller91f40d82013-02-22 11:37:00 +1100186#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000187#ifdef __NR_mmap2
Damien Millere3ea3352017-03-14 17:48:43 +1100188 SC_ALLOW(__NR_mmap2),
Damien Miller0fa0ed02014-09-10 08:15:34 +1000189#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000190#ifdef __NR_mremap
Damien Millere3ea3352017-03-14 17:48:43 +1100191 SC_ALLOW(__NR_mremap),
Damien Miller99f33d72015-06-17 10:50:51 +1000192#endif
193#ifdef __NR_munmap
Damien Millere3ea3352017-03-14 17:48:43 +1100194 SC_ALLOW(__NR_munmap),
Damien Miller99f33d72015-06-17 10:50:51 +1000195#endif
196#ifdef __NR__newselect
Damien Millere3ea3352017-03-14 17:48:43 +1100197 SC_ALLOW(__NR__newselect),
Damien Miller99f33d72015-06-17 10:50:51 +1000198#endif
199#ifdef __NR_poll
Damien Millere3ea3352017-03-14 17:48:43 +1100200 SC_ALLOW(__NR_poll),
Damien Miller99f33d72015-06-17 10:50:51 +1000201#endif
Damien Millerbc202052015-06-25 09:51:39 +1000202#ifdef __NR_pselect6
Damien Millere3ea3352017-03-14 17:48:43 +1100203 SC_ALLOW(__NR_pselect6),
Damien Millerbc202052015-06-25 09:51:39 +1000204#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000205#ifdef __NR_read
Damien Millere3ea3352017-03-14 17:48:43 +1100206 SC_ALLOW(__NR_read),
Damien Miller99f33d72015-06-17 10:50:51 +1000207#endif
Damien Millere0956e32012-04-04 11:27:54 +1000208#ifdef __NR_rt_sigprocmask
Damien Millere3ea3352017-03-14 17:48:43 +1100209 SC_ALLOW(__NR_rt_sigprocmask),
Damien Miller99f33d72015-06-17 10:50:51 +1000210#endif
211#ifdef __NR_select
Damien Millere3ea3352017-03-14 17:48:43 +1100212 SC_ALLOW(__NR_select),
Damien Miller99f33d72015-06-17 10:50:51 +1000213#endif
214#ifdef __NR_shutdown
Damien Millere3ea3352017-03-14 17:48:43 +1100215 SC_ALLOW(__NR_shutdown),
Damien Miller99f33d72015-06-17 10:50:51 +1000216#endif
217#ifdef __NR_sigprocmask
Damien Millere3ea3352017-03-14 17:48:43 +1100218 SC_ALLOW(__NR_sigprocmask),
Damien Millere0956e32012-04-04 11:27:54 +1000219#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000220#ifdef __NR_time
Damien Millere3ea3352017-03-14 17:48:43 +1100221 SC_ALLOW(__NR_time),
Damien Miller99f33d72015-06-17 10:50:51 +1000222#endif
223#ifdef __NR_write
Damien Millere3ea3352017-03-14 17:48:43 +1100224 SC_ALLOW(__NR_write),
Damien Miller99f33d72015-06-17 10:50:51 +1000225#endif
226#ifdef __NR_socketcall
Damien Millere3ea3352017-03-14 17:48:43 +1100227 SC_ALLOW_ARG(__NR_socketcall, 0, SYS_SHUTDOWN),
Darren Tucker17a54a02017-04-25 08:32:27 +1000228 SC_DENY(__NR_socketcall, EACCES),
Damien Miller99f33d72015-06-17 10:50:51 +1000229#endif
Damien Miller5f1596e2017-03-14 13:15:18 +1100230#if defined(__NR_ioctl) && defined(__s390__)
231 /* Allow ioctls for ICA crypto card on s390 */
Damien Millere3ea3352017-03-14 17:48:43 +1100232 SC_ALLOW_ARG(__NR_ioctl, 1, Z90STAT_STATUS_MASK),
233 SC_ALLOW_ARG(__NR_ioctl, 1, ICARSAMODEXPO),
234 SC_ALLOW_ARG(__NR_ioctl, 1, ICARSACRT),
Damien Millerf86586b2017-03-14 18:26:29 +1100235#endif
236#if defined(__x86_64__) && defined(__ILP32__) && defined(__X32_SYSCALL_BIT)
237 /*
238 * On Linux x32, the clock_gettime VDSO falls back to the
239 * x86-64 syscall under some circumstances, e.g.
240 * https://bugs.debian.org/849923
241 */
Damien Miller6b853c62017-03-21 08:47:55 +1100242 SC_ALLOW(__NR_clock_gettime & ~__X32_SYSCALL_BIT),
Damien Millerf86586b2017-03-14 18:26:29 +1100243#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000244
245 /* Default deny */
Damien Millere0956e32012-04-04 11:27:54 +1000246 BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL),
247};
248
249static const struct sock_fprog preauth_program = {
250 .len = (unsigned short)(sizeof(preauth_insns)/sizeof(preauth_insns[0])),
251 .filter = (struct sock_filter *)preauth_insns,
252};
253
254struct ssh_sandbox {
255 pid_t child_pid;
256};
257
258struct ssh_sandbox *
Damien Miller868ea1e2014-01-17 16:47:04 +1100259ssh_sandbox_init(struct monitor *monitor)
Damien Millere0956e32012-04-04 11:27:54 +1000260{
261 struct ssh_sandbox *box;
262
263 /*
264 * Strictly, we don't need to maintain any state here but we need
265 * to return non-NULL to satisfy the API.
266 */
267 debug3("%s: preparing seccomp filter sandbox", __func__);
268 box = xcalloc(1, sizeof(*box));
269 box->child_pid = 0;
270
271 return box;
272}
273
274#ifdef SANDBOX_SECCOMP_FILTER_DEBUG
275extern struct monitor *pmonitor;
276void mm_log_handler(LogLevel level, const char *msg, void *ctx);
277
278static void
279ssh_sandbox_violation(int signum, siginfo_t *info, void *void_context)
280{
281 char msg[256];
282
283 snprintf(msg, sizeof(msg),
284 "%s: unexpected system call (arch:0x%x,syscall:%d @ %p)",
285 __func__, info->si_arch, info->si_syscall, info->si_call_addr);
286 mm_log_handler(SYSLOG_LEVEL_FATAL, msg, pmonitor);
287 _exit(1);
288}
289
290static void
291ssh_sandbox_child_debugging(void)
292{
293 struct sigaction act;
294 sigset_t mask;
295
296 debug3("%s: installing SIGSYS handler", __func__);
297 memset(&act, 0, sizeof(act));
298 sigemptyset(&mask);
299 sigaddset(&mask, SIGSYS);
300
301 act.sa_sigaction = &ssh_sandbox_violation;
302 act.sa_flags = SA_SIGINFO;
303 if (sigaction(SIGSYS, &act, NULL) == -1)
304 fatal("%s: sigaction(SIGSYS): %s", __func__, strerror(errno));
305 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
306 fatal("%s: sigprocmask(SIGSYS): %s",
307 __func__, strerror(errno));
308}
309#endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
310
311void
312ssh_sandbox_child(struct ssh_sandbox *box)
313{
314 struct rlimit rl_zero;
Damien Millera0433a72012-07-06 10:27:10 +1000315 int nnp_failed = 0;
Damien Millere0956e32012-04-04 11:27:54 +1000316
317 /* Set rlimits for completeness if possible. */
318 rl_zero.rlim_cur = rl_zero.rlim_max = 0;
319 if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
320 fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
321 __func__, strerror(errno));
322 if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
323 fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
324 __func__, strerror(errno));
325 if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
326 fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
327 __func__, strerror(errno));
328
329#ifdef SANDBOX_SECCOMP_FILTER_DEBUG
330 ssh_sandbox_child_debugging();
331#endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
332
333 debug3("%s: setting PR_SET_NO_NEW_PRIVS", __func__);
Damien Millera0433a72012-07-06 10:27:10 +1000334 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) {
335 debug("%s: prctl(PR_SET_NO_NEW_PRIVS): %s",
Damien Millere0956e32012-04-04 11:27:54 +1000336 __func__, strerror(errno));
Damien Millera0433a72012-07-06 10:27:10 +1000337 nnp_failed = 1;
338 }
Damien Millere0956e32012-04-04 11:27:54 +1000339 debug3("%s: attaching seccomp filter program", __func__);
340 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &preauth_program) == -1)
Damien Millera0433a72012-07-06 10:27:10 +1000341 debug("%s: prctl(PR_SET_SECCOMP): %s",
Damien Millere0956e32012-04-04 11:27:54 +1000342 __func__, strerror(errno));
Damien Millera0433a72012-07-06 10:27:10 +1000343 else if (nnp_failed)
344 fatal("%s: SECCOMP_MODE_FILTER activated but "
345 "PR_SET_NO_NEW_PRIVS failed", __func__);
Damien Millere0956e32012-04-04 11:27:54 +1000346}
347
348void
349ssh_sandbox_parent_finish(struct ssh_sandbox *box)
350{
351 free(box);
352 debug3("%s: finished", __func__);
353}
354
355void
356ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
357{
358 box->child_pid = child_pid;
359}
360
361#endif /* SANDBOX_SECCOMP_FILTER */