blob: b89afe04cd54658408143d87c8c827bfaae30cc0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/seccomp.c
3 *
4 * Copyright 2004-2005 Andrea Arcangeli <andrea@cpushare.com>
5 *
Will Drewry980e9202012-02-09 11:50:58 -06006 * Copyright (C) 2012 Google, Inc.
7 * Will Drewry <wad@chromium.org>
8 *
9 * This defines a simple but solid secure-computing facility.
10 *
11 * Mode 1 uses a fixed list of allowed system calls.
12 * Mode 2 allows user-defined system call filters in the form
13 * of Berkeley Packet Filters/Linux Socket Filters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Will Drewry980e9202012-02-09 11:50:58 -060016#include <linux/atomic.h>
Eric Paris85e7bac2012-01-03 14:23:05 -050017#include <linux/audit.h>
Roland McGrath5b101742009-02-27 23:25:54 -080018#include <linux/compat.h>
Will Drewry980e9202012-02-09 11:50:58 -060019#include <linux/sched.h>
20#include <linux/seccomp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/* #define SECCOMP_DEBUG 1 */
Will Drewry980e9202012-02-09 11:50:58 -060023
24#ifdef CONFIG_SECCOMP_FILTER
25#include <asm/syscall.h>
26#include <linux/filter.h>
Will Drewry4ca6e672012-02-09 12:08:39 -060027#include <linux/ptrace.h>
Will Drewry980e9202012-02-09 11:50:58 -060028#include <linux/security.h>
29#include <linux/slab.h>
30#include <linux/tracehook.h>
31#include <linux/uaccess.h>
32
33/**
34 * struct seccomp_filter - container for seccomp BPF programs
35 *
36 * @usage: reference count to manage the object lifetime.
37 * get/put helpers should be used when accessing an instance
38 * outside of a lifetime-guarded section. In general, this
39 * is only needed for handling filters shared across tasks.
40 * @prev: points to a previously installed, or inherited, filter
41 * @len: the number of instructions in the program
42 * @insns: the BPF program instructions to evaluate
43 *
44 * seccomp_filter objects are organized in a tree linked via the @prev
45 * pointer. For any task, it appears to be a singly-linked list starting
46 * with current->seccomp.filter, the most recently attached or inherited filter.
47 * However, multiple filters may share a @prev node, by way of fork(), which
48 * results in a unidirectional tree existing in memory. This is similar to
49 * how namespaces work.
50 *
51 * seccomp_filter objects should never be modified after being attached
52 * to a task_struct (other than @usage).
53 */
54struct seccomp_filter {
55 atomic_t usage;
56 struct seccomp_filter *prev;
57 unsigned short len; /* Instruction count */
58 struct sock_filter insns[];
59};
60
61/* Limit any path through the tree to 256KB worth of instructions. */
62#define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
63
Will Drewry980e9202012-02-09 11:50:58 -060064/**
65 * get_u32 - returns a u32 offset into data
66 * @data: a unsigned 64 bit value
67 * @index: 0 or 1 to return the first or second 32-bits
68 *
69 * This inline exists to hide the length of unsigned long. If a 32-bit
70 * unsigned long is passed in, it will be extended and the top 32-bits will be
71 * 0. If it is a 64-bit unsigned long, then whatever data is resident will be
72 * properly returned.
73 *
74 * Endianness is explicitly ignored and left for BPF program authors to manage
75 * as per the specific architecture.
76 */
77static inline u32 get_u32(u64 data, int index)
78{
79 return ((u32 *)&data)[index];
80}
81
82/* Helper for bpf_load below. */
83#define BPF_DATA(_name) offsetof(struct seccomp_data, _name)
84/**
85 * bpf_load: checks and returns a pointer to the requested offset
86 * @off: offset into struct seccomp_data to load from
87 *
88 * Returns the requested 32-bits of data.
89 * seccomp_check_filter() should assure that @off is 32-bit aligned
90 * and not out of bounds. Failure to do so is a BUG.
91 */
92u32 seccomp_bpf_load(int off)
93{
94 struct pt_regs *regs = task_pt_regs(current);
95 if (off == BPF_DATA(nr))
96 return syscall_get_nr(current, regs);
97 if (off == BPF_DATA(arch))
98 return syscall_get_arch(current, regs);
99 if (off >= BPF_DATA(args[0]) && off < BPF_DATA(args[6])) {
100 unsigned long value;
101 int arg = (off - BPF_DATA(args[0])) / sizeof(u64);
102 int index = !!(off % sizeof(u64));
103 syscall_get_arguments(current, regs, arg, 1, &value);
104 return get_u32(value, index);
105 }
106 if (off == BPF_DATA(instruction_pointer))
107 return get_u32(KSTK_EIP(current), 0);
108 if (off == BPF_DATA(instruction_pointer) + sizeof(u32))
109 return get_u32(KSTK_EIP(current), 1);
110 /* seccomp_check_filter should make this impossible. */
111 BUG();
112}
113
114/**
115 * seccomp_check_filter - verify seccomp filter code
116 * @filter: filter to verify
117 * @flen: length of filter
118 *
119 * Takes a previously checked filter (by sk_chk_filter) and
120 * redirects all filter code that loads struct sk_buff data
121 * and related data through seccomp_bpf_load. It also
122 * enforces length and alignment checking of those loads.
123 *
124 * Returns 0 if the rule set is legal or -EINVAL if not.
125 */
126static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
127{
128 int pc;
129 for (pc = 0; pc < flen; pc++) {
130 struct sock_filter *ftest = &filter[pc];
131 u16 code = ftest->code;
132 u32 k = ftest->k;
133
134 switch (code) {
135 case BPF_S_LD_W_ABS:
136 ftest->code = BPF_S_ANC_SECCOMP_LD_W;
137 /* 32-bit aligned and not out of bounds. */
138 if (k >= sizeof(struct seccomp_data) || k & 3)
139 return -EINVAL;
140 continue;
141 case BPF_S_LD_W_LEN:
142 ftest->code = BPF_S_LD_IMM;
143 ftest->k = sizeof(struct seccomp_data);
144 continue;
145 case BPF_S_LDX_W_LEN:
146 ftest->code = BPF_S_LDX_IMM;
147 ftest->k = sizeof(struct seccomp_data);
148 continue;
149 /* Explicitly include allowed calls. */
150 case BPF_S_RET_K:
151 case BPF_S_RET_A:
152 case BPF_S_ALU_ADD_K:
153 case BPF_S_ALU_ADD_X:
154 case BPF_S_ALU_SUB_K:
155 case BPF_S_ALU_SUB_X:
156 case BPF_S_ALU_MUL_K:
157 case BPF_S_ALU_MUL_X:
158 case BPF_S_ALU_DIV_X:
159 case BPF_S_ALU_AND_K:
160 case BPF_S_ALU_AND_X:
161 case BPF_S_ALU_OR_K:
162 case BPF_S_ALU_OR_X:
163 case BPF_S_ALU_LSH_K:
164 case BPF_S_ALU_LSH_X:
165 case BPF_S_ALU_RSH_K:
166 case BPF_S_ALU_RSH_X:
167 case BPF_S_ALU_NEG:
168 case BPF_S_LD_IMM:
169 case BPF_S_LDX_IMM:
170 case BPF_S_MISC_TAX:
171 case BPF_S_MISC_TXA:
172 case BPF_S_ALU_DIV_K:
173 case BPF_S_LD_MEM:
174 case BPF_S_LDX_MEM:
175 case BPF_S_ST:
176 case BPF_S_STX:
177 case BPF_S_JMP_JA:
178 case BPF_S_JMP_JEQ_K:
179 case BPF_S_JMP_JEQ_X:
180 case BPF_S_JMP_JGE_K:
181 case BPF_S_JMP_JGE_X:
182 case BPF_S_JMP_JGT_K:
183 case BPF_S_JMP_JGT_X:
184 case BPF_S_JMP_JSET_K:
185 case BPF_S_JMP_JSET_X:
186 continue;
187 default:
188 return -EINVAL;
189 }
190 }
191 return 0;
192}
193
194/**
195 * seccomp_run_filters - evaluates all seccomp filters against @syscall
196 * @syscall: number of the current system call
197 *
198 * Returns valid seccomp BPF response codes.
199 */
200static u32 seccomp_run_filters(int syscall)
201{
202 struct seccomp_filter *f;
Will Drewry43ec8252012-02-15 20:45:54 -0600203 u32 ret = SECCOMP_RET_ALLOW;
204
205 /* Ensure unexpected behavior doesn't result in failing open. */
206 if (WARN_ON(current->seccomp.filter == NULL))
207 return SECCOMP_RET_KILL;
208
Will Drewry980e9202012-02-09 11:50:58 -0600209 /*
210 * All filters in the list are evaluated and the lowest BPF return
Will Drewry43ec8252012-02-15 20:45:54 -0600211 * value always takes priority (ignoring the DATA).
Will Drewry980e9202012-02-09 11:50:58 -0600212 */
213 for (f = current->seccomp.filter; f; f = f->prev) {
Will Drewry43ec8252012-02-15 20:45:54 -0600214 u32 cur_ret = sk_run_filter(NULL, f->insns);
215 if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
216 ret = cur_ret;
Will Drewry980e9202012-02-09 11:50:58 -0600217 }
218 return ret;
219}
Kees Cookc5c2ce72014-06-25 15:38:02 -0700220#endif /* CONFIG_SECCOMP_FILTER */
Will Drewry980e9202012-02-09 11:50:58 -0600221
Kees Cookc5c2ce72014-06-25 15:38:02 -0700222static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
223{
224 if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
225 return false;
226
227 return true;
228}
229
230static inline void seccomp_assign_mode(unsigned long seccomp_mode)
231{
232 current->seccomp.mode = seccomp_mode;
233 set_tsk_thread_flag(current, TIF_SECCOMP);
234}
235
236#ifdef CONFIG_SECCOMP_FILTER
Will Drewry980e9202012-02-09 11:50:58 -0600237/**
238 * seccomp_attach_filter: Attaches a seccomp filter to current.
239 * @fprog: BPF program to install
240 *
241 * Returns 0 on success or an errno on failure.
242 */
243static long seccomp_attach_filter(struct sock_fprog *fprog)
244{
245 struct seccomp_filter *filter;
246 unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
247 unsigned long total_insns = fprog->len;
248 long ret;
249
250 if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
251 return -EINVAL;
252
253 for (filter = current->seccomp.filter; filter; filter = filter->prev)
254 total_insns += filter->len + 4; /* include a 4 instr penalty */
255 if (total_insns > MAX_INSNS_PER_PATH)
256 return -ENOMEM;
257
258 /*
259 * Installing a seccomp filter requires that the task have
260 * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
261 * This avoids scenarios where unprivileged tasks can affect the
262 * behavior of privileged children.
263 */
264 if (!current->no_new_privs &&
265 security_capable_noaudit(current_cred(), current_user_ns(),
266 CAP_SYS_ADMIN) != 0)
267 return -EACCES;
268
269 /* Allocate a new seccomp_filter */
270 filter = kzalloc(sizeof(struct seccomp_filter) + fp_size,
271 GFP_KERNEL|__GFP_NOWARN);
272 if (!filter)
273 return -ENOMEM;
274 atomic_set(&filter->usage, 1);
275 filter->len = fprog->len;
276
277 /* Copy the instructions from fprog. */
278 ret = -EFAULT;
279 if (copy_from_user(filter->insns, fprog->filter, fp_size))
280 goto fail;
281
282 /* Check and rewrite the fprog via the skb checker */
283 ret = sk_chk_filter(filter->insns, filter->len);
284 if (ret)
285 goto fail;
286
287 /* Check and rewrite the fprog for seccomp use */
288 ret = seccomp_check_filter(filter->insns, filter->len);
289 if (ret)
290 goto fail;
291
292 /*
293 * If there is an existing filter, make it the prev and don't drop its
294 * task reference.
295 */
296 filter->prev = current->seccomp.filter;
297 current->seccomp.filter = filter;
298 return 0;
299fail:
300 kfree(filter);
301 return ret;
302}
303
304/**
305 * seccomp_attach_user_filter - attaches a user-supplied sock_fprog
306 * @user_filter: pointer to the user data containing a sock_fprog.
307 *
308 * Returns 0 on success and non-zero otherwise.
309 */
310long seccomp_attach_user_filter(char __user *user_filter)
311{
312 struct sock_fprog fprog;
313 long ret = -EFAULT;
314
315#ifdef CONFIG_COMPAT
316 if (is_compat_task()) {
317 struct compat_sock_fprog fprog32;
318 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
319 goto out;
320 fprog.len = fprog32.len;
321 fprog.filter = compat_ptr(fprog32.filter);
322 } else /* falls through to the if below. */
323#endif
324 if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
325 goto out;
326 ret = seccomp_attach_filter(&fprog);
327out:
328 return ret;
329}
330
331/* get_seccomp_filter - increments the reference count of the filter on @tsk */
332void get_seccomp_filter(struct task_struct *tsk)
333{
334 struct seccomp_filter *orig = tsk->seccomp.filter;
335 if (!orig)
336 return;
337 /* Reference count is bounded by the number of total processes. */
338 atomic_inc(&orig->usage);
339}
340
341/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
342void put_seccomp_filter(struct task_struct *tsk)
343{
344 struct seccomp_filter *orig = tsk->seccomp.filter;
345 /* Clean up single-reference branches iteratively. */
346 while (orig && atomic_dec_and_test(&orig->usage)) {
347 struct seccomp_filter *freeme = orig;
348 orig = orig->prev;
349 kfree(freeme);
350 }
351}
Will Drewry69537022012-02-09 12:01:37 -0600352
353/**
354 * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
355 * @syscall: syscall number to send to userland
356 * @reason: filter-supplied reason code to send to userland (via si_errno)
357 *
358 * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
359 */
360static void seccomp_send_sigsys(int syscall, int reason)
361{
362 struct siginfo info;
363 memset(&info, 0, sizeof(info));
364 info.si_signo = SIGSYS;
365 info.si_code = SYS_SECCOMP;
366 info.si_call_addr = (void __user *)KSTK_EIP(current);
367 info.si_errno = reason;
368 info.si_arch = syscall_get_arch(current, task_pt_regs(current));
369 info.si_syscall = syscall;
370 force_sig_info(SIGSYS, &info, current);
371}
Will Drewry980e9202012-02-09 11:50:58 -0600372#endif /* CONFIG_SECCOMP_FILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374/*
375 * Secure computing mode 1 allows only read/write/exit/sigreturn.
376 * To be fully secure this must be combined with rlimit
377 * to limit the stack allocations too.
378 */
379static int mode1_syscalls[] = {
380 __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
381 0, /* null terminated */
382};
383
Roland McGrath5b101742009-02-27 23:25:54 -0800384#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385static int mode1_syscalls_32[] = {
386 __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
387 0, /* null terminated */
388};
389#endif
390
Will Drewry43ec8252012-02-15 20:45:54 -0600391int __secure_computing(int this_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 int mode = current->seccomp.mode;
Will Drewry980e9202012-02-09 11:50:58 -0600394 int exit_sig = 0;
395 int *syscall;
Will Drewry171ba892012-04-17 14:48:58 -0500396 u32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 switch (mode) {
Will Drewry980e9202012-02-09 11:50:58 -0600399 case SECCOMP_MODE_STRICT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 syscall = mode1_syscalls;
Roland McGrath5b101742009-02-27 23:25:54 -0800401#ifdef CONFIG_COMPAT
402 if (is_compat_task())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 syscall = mode1_syscalls_32;
404#endif
405 do {
406 if (*syscall == this_syscall)
Will Drewry43ec8252012-02-15 20:45:54 -0600407 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 } while (*++syscall);
Will Drewry980e9202012-02-09 11:50:58 -0600409 exit_sig = SIGKILL;
Will Drewry171ba892012-04-17 14:48:58 -0500410 ret = SECCOMP_RET_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 break;
Will Drewry980e9202012-02-09 11:50:58 -0600412#ifdef CONFIG_SECCOMP_FILTER
Will Drewry171ba892012-04-17 14:48:58 -0500413 case SECCOMP_MODE_FILTER: {
414 int data;
Will Drewry43ec8252012-02-15 20:45:54 -0600415 ret = seccomp_run_filters(this_syscall);
416 data = ret & SECCOMP_RET_DATA;
Will Drewry171ba892012-04-17 14:48:58 -0500417 ret &= SECCOMP_RET_ACTION;
418 switch (ret) {
Will Drewry43ec8252012-02-15 20:45:54 -0600419 case SECCOMP_RET_ERRNO:
420 /* Set the low-order 16-bits as a errno. */
421 syscall_set_return_value(current, task_pt_regs(current),
422 -data, 0);
423 goto skip;
Will Drewry69537022012-02-09 12:01:37 -0600424 case SECCOMP_RET_TRAP:
425 /* Show the handler the original registers. */
426 syscall_rollback(current, task_pt_regs(current));
427 /* Let the filter pass back 16 bits of data. */
428 seccomp_send_sigsys(this_syscall, data);
429 goto skip;
Will Drewry4ca6e672012-02-09 12:08:39 -0600430 case SECCOMP_RET_TRACE:
431 /* Skip these calls if there is no tracer. */
Will Drewrybefe2872012-04-27 11:25:30 -0500432 if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
433 /* Make sure userspace sees an ENOSYS. */
434 syscall_set_return_value(current,
435 task_pt_regs(current), -ENOSYS, 0);
Will Drewry4ca6e672012-02-09 12:08:39 -0600436 goto skip;
Will Drewrybefe2872012-04-27 11:25:30 -0500437 }
Will Drewry4ca6e672012-02-09 12:08:39 -0600438 /* Allow the BPF to provide the event message */
439 ptrace_event(PTRACE_EVENT_SECCOMP, data);
440 /*
441 * The delivery of a fatal signal during event
442 * notification may silently skip tracer notification.
443 * Terminating the task now avoids executing a system
444 * call that may not be intended.
445 */
446 if (fatal_signal_pending(current))
447 break;
448 return 0;
Will Drewry43ec8252012-02-15 20:45:54 -0600449 case SECCOMP_RET_ALLOW:
450 return 0;
451 case SECCOMP_RET_KILL:
452 default:
453 break;
454 }
Will Drewry980e9202012-02-09 11:50:58 -0600455 exit_sig = SIGSYS;
456 break;
Will Drewry171ba892012-04-17 14:48:58 -0500457 }
Will Drewry980e9202012-02-09 11:50:58 -0600458#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 default:
460 BUG();
461 }
462
463#ifdef SECCOMP_DEBUG
464 dump_stack();
465#endif
Will Drewry43ec8252012-02-15 20:45:54 -0600466 audit_seccomp(this_syscall, exit_sig, ret);
Will Drewry980e9202012-02-09 11:50:58 -0600467 do_exit(exit_sig);
Will Drewry171ba892012-04-17 14:48:58 -0500468#ifdef CONFIG_SECCOMP_FILTER
Will Drewry43ec8252012-02-15 20:45:54 -0600469skip:
470 audit_seccomp(this_syscall, exit_sig, ret);
Will Drewry171ba892012-04-17 14:48:58 -0500471#endif
Will Drewry43ec8252012-02-15 20:45:54 -0600472 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700474
475long prctl_get_seccomp(void)
476{
477 return current->seccomp.mode;
478}
479
Will Drewry980e9202012-02-09 11:50:58 -0600480/**
Kees Cook2d5f6982014-05-21 15:02:11 -0700481 * seccomp_set_mode: internal function for setting seccomp mode
Will Drewry980e9202012-02-09 11:50:58 -0600482 * @seccomp_mode: requested mode to use
483 * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
484 *
485 * This function may be called repeatedly with a @seccomp_mode of
486 * SECCOMP_MODE_FILTER to install additional filters. Every filter
487 * successfully installed will be evaluated (in reverse order) for each system
488 * call the task makes.
489 *
490 * Once current->seccomp.mode is non-zero, it may not be changed.
491 *
492 * Returns 0 on success or -EINVAL on failure.
493 */
Kees Cook2d5f6982014-05-21 15:02:11 -0700494static long seccomp_set_mode(unsigned long seccomp_mode, char __user *filter)
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700495{
Will Drewry980e9202012-02-09 11:50:58 -0600496 long ret = -EINVAL;
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700497
Kees Cookc5c2ce72014-06-25 15:38:02 -0700498 if (!seccomp_may_assign_mode(seccomp_mode))
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700499 goto out;
500
Will Drewry980e9202012-02-09 11:50:58 -0600501 switch (seccomp_mode) {
502 case SECCOMP_MODE_STRICT:
503 ret = 0;
Andrea Arcangelicf99aba2007-07-15 23:41:33 -0700504#ifdef TIF_NOTSC
505 disable_TSC();
506#endif
Will Drewry980e9202012-02-09 11:50:58 -0600507 break;
508#ifdef CONFIG_SECCOMP_FILTER
509 case SECCOMP_MODE_FILTER:
510 ret = seccomp_attach_user_filter(filter);
511 if (ret)
512 goto out;
513 break;
514#endif
515 default:
516 goto out;
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700517 }
518
Kees Cookc5c2ce72014-06-25 15:38:02 -0700519 seccomp_assign_mode(seccomp_mode);
Will Drewry980e9202012-02-09 11:50:58 -0600520out:
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700521 return ret;
522}
Kees Cook2d5f6982014-05-21 15:02:11 -0700523
524/**
525 * prctl_set_seccomp: configures current->seccomp.mode
526 * @seccomp_mode: requested mode to use
527 * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
528 *
529 * Returns 0 on success or -EINVAL on failure.
530 */
531long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
532{
533 return seccomp_set_mode(seccomp_mode, filter);
534}