blob: c1fe1f3e9554f0dae98fc7bc138e029c1fd971bd [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>
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
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)
Damien Miller99f33d72015-06-17 10:50:51 +100083#define SC_ALLOW_ARG(_nr, _arg_nr, _arg_val) \
Damien Miller97e2e152015-06-17 14:36:54 +100084 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_ ## _nr, 0, 4), \
Damien Miller99f33d72015-06-17 10:50:51 +100085 /* 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))
Damien Millere0956e32012-04-04 11:27:54 +100093
94/* Syscall filtering set for preauth. */
95static 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)),
Damien Miller99f33d72015-06-17 10:50:51 +1000104
105 /* Syscalls to non-fatally deny */
106#ifdef __NR_fstat
107 SC_DENY(fstat, EACCES),
108#endif
109#ifdef __NR_fstat64
110 SC_DENY(fstat64, EACCES),
111#endif
112#ifdef __NR_open
Damien Millere0956e32012-04-04 11:27:54 +1000113 SC_DENY(open, EACCES),
Damien Miller99f33d72015-06-17 10:50:51 +1000114#endif
115#ifdef __NR_openat
116 SC_DENY(openat, EACCES),
117#endif
118#ifdef __NR_newfstatat
119 SC_DENY(newfstatat, EACCES),
120#endif
121#ifdef __NR_stat
Damien Miller48abc472014-03-17 14:45:56 +1100122 SC_DENY(stat, EACCES),
Damien Miller91f40d82013-02-22 11:37:00 +1100123#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000124#ifdef __NR_stat64
125 SC_DENY(stat64, EACCES),
Damien Miller6434cb22014-02-06 11:17:50 +1100126#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000127
128 /* Syscalls to permit */
129#ifdef __NR_brk
Damien Millere0956e32012-04-04 11:27:54 +1000130 SC_ALLOW(brk),
Damien Millere0956e32012-04-04 11:27:54 +1000131#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000132#ifdef __NR_clock_gettime
133 SC_ALLOW(clock_gettime),
134#endif
135#ifdef __NR_close
136 SC_ALLOW(close),
137#endif
138#ifdef __NR_exit
139 SC_ALLOW(exit),
140#endif
141#ifdef __NR_exit_group
142 SC_ALLOW(exit_group),
143#endif
144#ifdef __NR_getpid
145 SC_ALLOW(getpid),
146#endif
147#ifdef __NR_gettimeofday
148 SC_ALLOW(gettimeofday),
149#endif
150#ifdef __NR_madvise
Damien Millere0956e32012-04-04 11:27:54 +1000151 SC_ALLOW(madvise),
Damien Miller91f40d82013-02-22 11:37:00 +1100152#endif
153#ifdef __NR_mmap
Damien Millere0956e32012-04-04 11:27:54 +1000154 SC_ALLOW(mmap),
Damien Miller91f40d82013-02-22 11:37:00 +1100155#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000156#ifdef __NR_mmap2
157 SC_ALLOW(mmap2),
Damien Miller0fa0ed02014-09-10 08:15:34 +1000158#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000159#ifdef __NR_mremap
160 SC_ALLOW(mremap),
161#endif
162#ifdef __NR_munmap
Damien Millere0956e32012-04-04 11:27:54 +1000163 SC_ALLOW(munmap),
Damien Miller99f33d72015-06-17 10:50:51 +1000164#endif
165#ifdef __NR__newselect
166 SC_ALLOW(_newselect),
167#endif
168#ifdef __NR_poll
169 SC_ALLOW(poll),
170#endif
171#ifdef __NR_read
172 SC_ALLOW(read),
173#endif
Damien Millere0956e32012-04-04 11:27:54 +1000174#ifdef __NR_rt_sigprocmask
175 SC_ALLOW(rt_sigprocmask),
Damien Miller99f33d72015-06-17 10:50:51 +1000176#endif
177#ifdef __NR_select
178 SC_ALLOW(select),
179#endif
180#ifdef __NR_shutdown
181 SC_ALLOW(shutdown),
182#endif
183#ifdef __NR_sigprocmask
Damien Millere0956e32012-04-04 11:27:54 +1000184 SC_ALLOW(sigprocmask),
185#endif
Damien Miller99f33d72015-06-17 10:50:51 +1000186#ifdef __NR_time
187 SC_ALLOW(time),
188#endif
189#ifdef __NR_write
190 SC_ALLOW(write),
191#endif
192#ifdef __NR_socketcall
193 SC_ALLOW_ARG(socketcall, 0, SYS_SHUTDOWN),
194#endif
195
196 /* Default deny */
Damien Millere0956e32012-04-04 11:27:54 +1000197 BPF_STMT(BPF_RET+BPF_K, SECCOMP_FILTER_FAIL),
198};
199
200static const struct sock_fprog preauth_program = {
201 .len = (unsigned short)(sizeof(preauth_insns)/sizeof(preauth_insns[0])),
202 .filter = (struct sock_filter *)preauth_insns,
203};
204
205struct ssh_sandbox {
206 pid_t child_pid;
207};
208
209struct ssh_sandbox *
Damien Miller868ea1e2014-01-17 16:47:04 +1100210ssh_sandbox_init(struct monitor *monitor)
Damien Millere0956e32012-04-04 11:27:54 +1000211{
212 struct ssh_sandbox *box;
213
214 /*
215 * Strictly, we don't need to maintain any state here but we need
216 * to return non-NULL to satisfy the API.
217 */
218 debug3("%s: preparing seccomp filter sandbox", __func__);
219 box = xcalloc(1, sizeof(*box));
220 box->child_pid = 0;
221
222 return box;
223}
224
225#ifdef SANDBOX_SECCOMP_FILTER_DEBUG
226extern struct monitor *pmonitor;
227void mm_log_handler(LogLevel level, const char *msg, void *ctx);
228
229static void
230ssh_sandbox_violation(int signum, siginfo_t *info, void *void_context)
231{
232 char msg[256];
233
234 snprintf(msg, sizeof(msg),
235 "%s: unexpected system call (arch:0x%x,syscall:%d @ %p)",
236 __func__, info->si_arch, info->si_syscall, info->si_call_addr);
237 mm_log_handler(SYSLOG_LEVEL_FATAL, msg, pmonitor);
238 _exit(1);
239}
240
241static void
242ssh_sandbox_child_debugging(void)
243{
244 struct sigaction act;
245 sigset_t mask;
246
247 debug3("%s: installing SIGSYS handler", __func__);
248 memset(&act, 0, sizeof(act));
249 sigemptyset(&mask);
250 sigaddset(&mask, SIGSYS);
251
252 act.sa_sigaction = &ssh_sandbox_violation;
253 act.sa_flags = SA_SIGINFO;
254 if (sigaction(SIGSYS, &act, NULL) == -1)
255 fatal("%s: sigaction(SIGSYS): %s", __func__, strerror(errno));
256 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1)
257 fatal("%s: sigprocmask(SIGSYS): %s",
258 __func__, strerror(errno));
259}
260#endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
261
262void
263ssh_sandbox_child(struct ssh_sandbox *box)
264{
265 struct rlimit rl_zero;
Damien Millera0433a72012-07-06 10:27:10 +1000266 int nnp_failed = 0;
Damien Millere0956e32012-04-04 11:27:54 +1000267
268 /* Set rlimits for completeness if possible. */
269 rl_zero.rlim_cur = rl_zero.rlim_max = 0;
270 if (setrlimit(RLIMIT_FSIZE, &rl_zero) == -1)
271 fatal("%s: setrlimit(RLIMIT_FSIZE, { 0, 0 }): %s",
272 __func__, strerror(errno));
273 if (setrlimit(RLIMIT_NOFILE, &rl_zero) == -1)
274 fatal("%s: setrlimit(RLIMIT_NOFILE, { 0, 0 }): %s",
275 __func__, strerror(errno));
276 if (setrlimit(RLIMIT_NPROC, &rl_zero) == -1)
277 fatal("%s: setrlimit(RLIMIT_NPROC, { 0, 0 }): %s",
278 __func__, strerror(errno));
279
280#ifdef SANDBOX_SECCOMP_FILTER_DEBUG
281 ssh_sandbox_child_debugging();
282#endif /* SANDBOX_SECCOMP_FILTER_DEBUG */
283
284 debug3("%s: setting PR_SET_NO_NEW_PRIVS", __func__);
Damien Millera0433a72012-07-06 10:27:10 +1000285 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) {
286 debug("%s: prctl(PR_SET_NO_NEW_PRIVS): %s",
Damien Millere0956e32012-04-04 11:27:54 +1000287 __func__, strerror(errno));
Damien Millera0433a72012-07-06 10:27:10 +1000288 nnp_failed = 1;
289 }
Damien Millere0956e32012-04-04 11:27:54 +1000290 debug3("%s: attaching seccomp filter program", __func__);
291 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &preauth_program) == -1)
Damien Millera0433a72012-07-06 10:27:10 +1000292 debug("%s: prctl(PR_SET_SECCOMP): %s",
Damien Millere0956e32012-04-04 11:27:54 +1000293 __func__, strerror(errno));
Damien Millera0433a72012-07-06 10:27:10 +1000294 else if (nnp_failed)
295 fatal("%s: SECCOMP_MODE_FILTER activated but "
296 "PR_SET_NO_NEW_PRIVS failed", __func__);
Damien Millere0956e32012-04-04 11:27:54 +1000297}
298
299void
300ssh_sandbox_parent_finish(struct ssh_sandbox *box)
301{
302 free(box);
303 debug3("%s: finished", __func__);
304}
305
306void
307ssh_sandbox_parent_preauth(struct ssh_sandbox *box, pid_t child_pid)
308{
309 box->child_pid = child_pid;
310}
311
312#endif /* SANDBOX_SECCOMP_FILTER */