2 * linux/kernel/seccomp.c
4 * Copyright 2004-2005 Andrea Arcangeli <andrea@cpushare.com>
6 * Copyright (C) 2012 Google, Inc.
7 * Will Drewry <wad@chromium.org>
9 * This defines a simple but solid secure-computing facility.
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.
16 #include <linux/atomic.h>
17 #include <linux/audit.h>
18 #include <linux/compat.h>
19 #include <linux/filter.h>
20 #include <linux/ptrace.h>
21 #include <linux/sched.h>
22 #include <linux/seccomp.h>
23 #include <linux/security.h>
24 #include <linux/slab.h>
25 #include <linux/uaccess.h>
27 #ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
28 #include <asm/syscall.h>
31 /* #define SECCOMP_DEBUG 1 */
33 #ifdef CONFIG_SECCOMP_FILTER
35 * struct seccomp_filter - container for seccomp BPF programs
37 * @usage: reference count to manage the object liftime.
38 * get/put helpers should be used when accessing an instance
39 * outside of a lifetime-guarded section. In general, this
40 * is only needed for handling filters shared across tasks.
41 * @prev: points to a previously installed, or inherited, filter
42 * @len: the number of instructions in the program
43 * @insns: the BPF program instructions to evaluate
45 * seccomp_filter objects are organized in a tree linked via the @prev
46 * pointer. For any task, it appears to be a singly-linked list starting
47 * with current->seccomp.filter, the most recently attached or inherited filter.
48 * However, multiple filters may share a @prev node, by way of fork(), which
49 * results in a unidirectional tree existing in memory. This is similar to
50 * how namespaces work.
52 * seccomp_filter objects should never be modified after being attached
53 * to a task_struct (other than @usage).
55 struct seccomp_filter {
57 struct seccomp_filter *prev;
58 unsigned short len; /* Instruction count */
59 struct sock_filter insns[];
62 /* Limit any path through the tree to 256KB worth of instructions. */
63 #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
66 * get_u32 - returns a u32 offset into data
67 * @data: a unsigned 64 bit value
68 * @index: 0 or 1 to return the first or second 32-bits
70 * This inline exists to hide the length of unsigned long.
71 * If a 32-bit unsigned long is passed in, it will be extended
72 * and the top 32-bits will be 0. If it is a 64-bit unsigned
73 * long, then whatever data is resident will be properly returned.
75 static inline u32 get_u32(u64 data, int index)
77 return ((u32 *)&data)[index];
80 /* Helper for bpf_load below. */
81 #define BPF_DATA(_name) offsetof(struct seccomp_data, _name)
83 * bpf_load: checks and returns a pointer to the requested offset
84 * @off: offset into struct seccomp_data to load from
86 * Returns the requested 32-bits of data.
87 * seccomp_chk_filter() should assure that @off is 32-bit aligned
88 * and not out of bounds. Failure to do so is a BUG.
90 u32 seccomp_bpf_load(int off)
92 struct pt_regs *regs = task_pt_regs(current);
93 if (off == BPF_DATA(nr))
94 return syscall_get_nr(current, regs);
95 if (off == BPF_DATA(arch))
96 return syscall_get_arch(current, regs);
97 if (off >= BPF_DATA(args[0]) && off < BPF_DATA(args[6])) {
99 int arg = (off - BPF_DATA(args[0])) / sizeof(u64);
100 int index = !!(off % sizeof(u64));
101 syscall_get_arguments(current, regs, arg, 1, &value);
102 return get_u32(value, index);
104 if (off == BPF_DATA(instruction_pointer))
105 return get_u32(KSTK_EIP(current), 0);
106 if (off == BPF_DATA(instruction_pointer) + sizeof(u32))
107 return get_u32(KSTK_EIP(current), 1);
108 /* seccomp_chk_filter should make this impossible. */
113 * seccomp_chk_filter - verify seccomp filter code
114 * @filter: filter to verify
115 * @flen: length of filter
117 * Takes a previously checked filter (by sk_chk_filter) and
118 * redirects all filter code that loads struct sk_buff data
119 * and related data through seccomp_bpf_load. It also
120 * enforces length and alignment checking of those loads.
122 * Returns 0 if the rule set is legal or -EINVAL if not.
124 static int seccomp_chk_filter(struct sock_filter *filter, unsigned int flen)
127 for (pc = 0; pc < flen; pc++) {
128 struct sock_filter *ftest = &filter[pc];
129 u16 code = ftest->code;
133 ftest->code = BPF_S_ANC_SECCOMP_LD_W;
134 /* 32-bit aligned and not out of bounds. */
135 if (k >= sizeof(struct seccomp_data) || k & 3)
139 ftest->code = BPF_S_LD_IMM;
140 ftest->k = sizeof(struct seccomp_data);
142 case BPF_S_LDX_W_LEN:
143 ftest->code = BPF_S_LDX_IMM;
144 ftest->k = sizeof(struct seccomp_data);
146 /* Explicitly include allowed calls. */
149 case BPF_S_ALU_ADD_K:
150 case BPF_S_ALU_ADD_X:
151 case BPF_S_ALU_SUB_K:
152 case BPF_S_ALU_SUB_X:
153 case BPF_S_ALU_MUL_K:
154 case BPF_S_ALU_MUL_X:
155 case BPF_S_ALU_DIV_X:
156 case BPF_S_ALU_AND_K:
157 case BPF_S_ALU_AND_X:
160 case BPF_S_ALU_LSH_K:
161 case BPF_S_ALU_LSH_X:
162 case BPF_S_ALU_RSH_K:
163 case BPF_S_ALU_RSH_X:
169 case BPF_S_ALU_DIV_K:
175 case BPF_S_JMP_JEQ_K:
176 case BPF_S_JMP_JEQ_X:
177 case BPF_S_JMP_JGE_K:
178 case BPF_S_JMP_JGE_X:
179 case BPF_S_JMP_JGT_K:
180 case BPF_S_JMP_JGT_X:
181 case BPF_S_JMP_JSET_K:
182 case BPF_S_JMP_JSET_X:
192 * seccomp_run_filters - evaluates all seccomp filters against @syscall
193 * @syscall: number of the current system call
195 * Returns valid seccomp BPF response codes.
197 static u32 seccomp_run_filters(int syscall)
199 struct seccomp_filter *f;
200 u32 ret = SECCOMP_RET_ALLOW;
202 /* Ensure unexpected behavior doesn't result in failing open. */
203 if (WARN_ON(current->seccomp.filter == NULL))
204 return SECCOMP_RET_KILL;
207 * All filters are evaluated in order of youngest to oldest. The lowest
208 * BPF return value (ignoring the DATA) always takes priority.
210 for (f = current->seccomp.filter; f; f = f->prev) {
211 u32 cur_ret = sk_run_filter(NULL, f->insns);
212 if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
219 * seccomp_attach_filter: Attaches a seccomp filter to current.
220 * @fprog: BPF program to install
222 * Returns 0 on success or an errno on failure.
224 static long seccomp_attach_filter(struct sock_fprog *fprog)
226 struct seccomp_filter *filter;
227 unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
228 unsigned long total_insns = fprog->len;
231 if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
234 for (filter = current->seccomp.filter; filter; filter = filter->prev)
235 total_insns += filter->len + 4; /* include a 4 instr penalty */
236 if (total_insns > MAX_INSNS_PER_PATH)
240 * Installing a seccomp filter requires that the task have
241 * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
242 * This avoids scenarios where unprivileged tasks can affect the
243 * behavior of privileged children.
245 if (!current->no_new_privs &&
246 security_real_capable_noaudit(current, current_user_ns(),
250 /* Allocate a new seccomp_filter */
251 filter = kzalloc(sizeof(struct seccomp_filter) + fp_size, GFP_KERNEL);
254 atomic_set(&filter->usage, 1);
255 filter->len = fprog->len;
257 /* Copy the instructions from fprog. */
259 if (copy_from_user(filter->insns, fprog->filter, fp_size))
262 /* Check and rewrite the fprog via the skb checker */
263 ret = sk_chk_filter(filter->insns, filter->len);
267 /* Check and rewrite the fprog for seccomp use */
268 ret = seccomp_chk_filter(filter->insns, filter->len);
273 * If there is an existing filter, make it the prev and don't drop its
276 filter->prev = current->seccomp.filter;
277 current->seccomp.filter = filter;
285 * seccomp_attach_user_filter - attaches a user-supplied sock_fprog
286 * @user_filter: pointer to the user data containing a sock_fprog.
288 * Returns 0 on success and non-zero otherwise.
290 long seccomp_attach_user_filter(char __user *user_filter)
292 struct sock_fprog fprog;
296 if (is_compat_task()) {
297 struct compat_sock_fprog fprog32;
298 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
300 fprog.len = fprog32.len;
301 fprog.filter = compat_ptr(fprog32.filter);
302 } else /* falls through to the if below. */
304 if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
306 ret = seccomp_attach_filter(&fprog);
311 /* get_seccomp_filter - increments the reference count of the filter on @tsk */
312 void get_seccomp_filter(struct task_struct *tsk)
314 struct seccomp_filter *orig = tsk->seccomp.filter;
317 /* Reference count is bounded by the number of total processes. */
318 atomic_inc(&orig->usage);
321 /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
322 void put_seccomp_filter(struct task_struct *tsk)
324 struct seccomp_filter *orig = tsk->seccomp.filter;
325 /* Clean up single-reference branches iteratively. */
326 while (orig && atomic_dec_and_test(&orig->usage)) {
327 struct seccomp_filter *freeme = orig;
334 * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
335 * @syscall: syscall number to send to userland
336 * @reason: filter-supplied reason code to send to userland (via si_errno)
338 * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
340 static void seccomp_send_sigsys(int syscall, int reason)
343 memset(&info, 0, sizeof(info));
344 info.si_signo = SIGSYS;
345 info.si_code = SYS_SECCOMP;
346 info.si_call_addr = (void __user *)KSTK_EIP(current);
347 info.si_errno = reason;
348 info.si_arch = syscall_get_arch(current, task_pt_regs(current));
349 info.si_syscall = syscall;
350 force_sig_info(SIGSYS, &info, current);
352 #endif /* CONFIG_SECCOMP_FILTER */
355 * Secure computing mode 1 allows only read/write/exit/sigreturn.
356 * To be fully secure this must be combined with rlimit
357 * to limit the stack allocations too.
359 static int mode1_syscalls[] = {
360 __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
361 0, /* null terminated */
365 static int mode1_syscalls_32[] = {
366 __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
367 0, /* null terminated */
371 void __secure_computing(int this_syscall)
373 /* Filter calls should never use this function. */
374 BUG_ON(current->seccomp.mode == SECCOMP_MODE_FILTER);
375 __secure_computing_int(this_syscall);
378 int __secure_computing_int(int this_syscall)
380 int mode = current->seccomp.mode;
383 u32 ret = SECCOMP_RET_KILL;
387 case SECCOMP_MODE_STRICT:
388 syscall = mode1_syscalls;
390 if (is_compat_task())
391 syscall = mode1_syscalls_32;
394 if (*syscall == this_syscall)
396 } while (*++syscall);
399 #ifdef CONFIG_SECCOMP_FILTER
400 case SECCOMP_MODE_FILTER:
401 ret = seccomp_run_filters(this_syscall);
402 data = ret & SECCOMP_RET_DATA;
403 switch (ret & SECCOMP_RET_ACTION) {
404 case SECCOMP_RET_ERRNO:
405 /* Set the low-order 16-bits as a errno. */
406 syscall_set_return_value(current, task_pt_regs(current),
409 case SECCOMP_RET_TRAP:
410 /* Show the handler the original registers. */
411 syscall_rollback(current, task_pt_regs(current));
412 /* Let the filter pass back 16 bits of data. */
413 seccomp_send_sigsys(this_syscall, data);
415 case SECCOMP_RET_TRACE:
416 /* Skip these calls if there is no tracer. */
417 if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP))
419 /* Allow the BPF to provide the event message */
420 ptrace_event(PTRACE_EVENT_SECCOMP, data);
421 if (fatal_signal_pending(current))
424 case SECCOMP_RET_ALLOW:
426 case SECCOMP_RET_KILL:
440 audit_seccomp(this_syscall, exit_sig, ret);
443 audit_seccomp(this_syscall, exit_sig, ret);
447 long prctl_get_seccomp(void)
449 return current->seccomp.mode;
453 * prctl_set_seccomp: configures current->seccomp.mode
454 * @seccomp_mode: requested mode to use
455 * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
457 * This function may be called repeatedly with a @seccomp_mode of
458 * SECCOMP_MODE_FILTER to install additional filters. Every filter
459 * successfully installed will be evaluated (in reverse order) for each system
460 * call the task makes.
462 * Once current->seccomp.mode is non-zero, it may not be changed.
464 * Returns 0 on success or -EINVAL on failure.
466 long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
470 if (current->seccomp.mode &&
471 current->seccomp.mode != seccomp_mode)
474 switch (seccomp_mode) {
475 case SECCOMP_MODE_STRICT:
481 #ifdef CONFIG_SECCOMP_FILTER
482 case SECCOMP_MODE_FILTER:
483 ret = seccomp_attach_user_filter(filter);
492 current->seccomp.mode = seccomp_mode;
493 set_thread_flag(TIF_SECCOMP);