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/seccomp.h>
19 #include <linux/sched.h>
20 #include <linux/compat.h>
22 /* #define SECCOMP_DEBUG 1 */
24 #ifdef CONFIG_SECCOMP_FILTER
25 #include <asm/syscall.h>
26 #include <linux/filter.h>
27 #include <linux/security.h>
28 #include <linux/slab.h>
29 #include <linux/tracehook.h>
30 #include <linux/uaccess.h>
33 * struct seccomp_filter - container for seccomp BPF programs
35 * @usage: reference count to manage the object lifetime.
36 * get/put helpers should be used when accessing an instance
37 * outside of a lifetime-guarded section. In general, this
38 * is only needed for handling filters shared across tasks.
39 * @prev: points to a previously installed, or inherited, filter
40 * @len: the number of instructions in the program
41 * @insns: the BPF program instructions to evaluate
43 * seccomp_filter objects are organized in a tree linked via the @prev
44 * pointer. For any task, it appears to be a singly-linked list starting
45 * with current->seccomp.filter, the most recently attached or inherited filter.
46 * However, multiple filters may share a @prev node, by way of fork(), which
47 * results in a unidirectional tree existing in memory. This is similar to
48 * how namespaces work.
50 * seccomp_filter objects should never be modified after being attached
51 * to a task_struct (other than @usage).
53 struct seccomp_filter {
55 struct seccomp_filter *prev;
56 unsigned short len; /* Instruction count */
57 struct sock_filter insns[];
60 /* Limit any path through the tree to 256KB worth of instructions. */
61 #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
64 * get_u32 - returns a u32 offset into data
65 * @data: a unsigned 64 bit value
66 * @index: 0 or 1 to return the first or second 32-bits
68 * This inline exists to hide the length of unsigned long. If a 32-bit
69 * unsigned long is passed in, it will be extended and the top 32-bits will be
70 * 0. If it is a 64-bit unsigned long, then whatever data is resident will be
73 * Endianness is explicitly ignored and left for BPF program authors to manage
74 * as per the specific architecture.
76 static inline u32 get_u32(u64 data, int index)
78 return ((u32 *)&data)[index];
81 /* Helper for bpf_load below. */
82 #define BPF_DATA(_name) offsetof(struct seccomp_data, _name)
84 * bpf_load: checks and returns a pointer to the requested offset
85 * @off: offset into struct seccomp_data to load from
87 * Returns the requested 32-bits of data.
88 * seccomp_check_filter() should assure that @off is 32-bit aligned
89 * and not out of bounds. Failure to do so is a BUG.
91 u32 seccomp_bpf_load(int off)
93 struct pt_regs *regs = task_pt_regs(current);
94 if (off == BPF_DATA(nr))
95 return syscall_get_nr(current, regs);
96 if (off == BPF_DATA(arch))
97 return syscall_get_arch(current, regs);
98 if (off >= BPF_DATA(args[0]) && off < BPF_DATA(args[6])) {
100 int arg = (off - BPF_DATA(args[0])) / sizeof(u64);
101 int index = !!(off % sizeof(u64));
102 syscall_get_arguments(current, regs, arg, 1, &value);
103 return get_u32(value, index);
105 if (off == BPF_DATA(instruction_pointer))
106 return get_u32(KSTK_EIP(current), 0);
107 if (off == BPF_DATA(instruction_pointer) + sizeof(u32))
108 return get_u32(KSTK_EIP(current), 1);
109 /* seccomp_check_filter should make this impossible. */
114 * seccomp_check_filter - verify seccomp filter code
115 * @filter: filter to verify
116 * @flen: length of filter
118 * Takes a previously checked filter (by sk_chk_filter) and
119 * redirects all filter code that loads struct sk_buff data
120 * and related data through seccomp_bpf_load. It also
121 * enforces length and alignment checking of those loads.
123 * Returns 0 if the rule set is legal or -EINVAL if not.
125 static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
128 for (pc = 0; pc < flen; pc++) {
129 struct sock_filter *ftest = &filter[pc];
130 u16 code = ftest->code;
135 ftest->code = BPF_S_ANC_SECCOMP_LD_W;
136 /* 32-bit aligned and not out of bounds. */
137 if (k >= sizeof(struct seccomp_data) || k & 3)
141 ftest->code = BPF_S_LD_IMM;
142 ftest->k = sizeof(struct seccomp_data);
144 case BPF_S_LDX_W_LEN:
145 ftest->code = BPF_S_LDX_IMM;
146 ftest->k = sizeof(struct seccomp_data);
148 /* Explicitly include allowed calls. */
151 case BPF_S_ALU_ADD_K:
152 case BPF_S_ALU_ADD_X:
153 case BPF_S_ALU_SUB_K:
154 case BPF_S_ALU_SUB_X:
155 case BPF_S_ALU_MUL_K:
156 case BPF_S_ALU_MUL_X:
157 case BPF_S_ALU_DIV_X:
158 case BPF_S_ALU_AND_K:
159 case BPF_S_ALU_AND_X:
162 case BPF_S_ALU_LSH_K:
163 case BPF_S_ALU_LSH_X:
164 case BPF_S_ALU_RSH_K:
165 case BPF_S_ALU_RSH_X:
171 case BPF_S_ALU_DIV_K:
177 case BPF_S_JMP_JEQ_K:
178 case BPF_S_JMP_JEQ_X:
179 case BPF_S_JMP_JGE_K:
180 case BPF_S_JMP_JGE_X:
181 case BPF_S_JMP_JGT_K:
182 case BPF_S_JMP_JGT_X:
183 case BPF_S_JMP_JSET_K:
184 case BPF_S_JMP_JSET_X:
194 * seccomp_run_filters - evaluates all seccomp filters against @syscall
195 * @syscall: number of the current system call
197 * Returns valid seccomp BPF response codes.
199 static u32 seccomp_run_filters(int syscall)
201 struct seccomp_filter *f;
202 u32 ret = SECCOMP_RET_KILL;
204 * All filters in the list are evaluated and the lowest BPF return
205 * value always takes priority.
207 for (f = current->seccomp.filter; f; f = f->prev) {
208 ret = sk_run_filter(NULL, f->insns);
209 if (ret != SECCOMP_RET_ALLOW)
216 * seccomp_attach_filter: Attaches a seccomp filter to current.
217 * @fprog: BPF program to install
219 * Returns 0 on success or an errno on failure.
221 static long seccomp_attach_filter(struct sock_fprog *fprog)
223 struct seccomp_filter *filter;
224 unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
225 unsigned long total_insns = fprog->len;
228 if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
231 for (filter = current->seccomp.filter; filter; filter = filter->prev)
232 total_insns += filter->len + 4; /* include a 4 instr penalty */
233 if (total_insns > MAX_INSNS_PER_PATH)
237 * Installing a seccomp filter requires that the task have
238 * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
239 * This avoids scenarios where unprivileged tasks can affect the
240 * behavior of privileged children.
242 if (!current->no_new_privs &&
243 security_capable_noaudit(current_cred(),
244 current_user_ns(), CAP_SYS_ADMIN) != 0)
247 /* Allocate a new seccomp_filter */
248 filter = kzalloc(sizeof(struct seccomp_filter) + fp_size,
249 GFP_KERNEL|__GFP_NOWARN);
252 atomic_set(&filter->usage, 1);
253 filter->len = fprog->len;
255 /* Copy the instructions from fprog. */
257 if (copy_from_user(filter->insns, fprog->filter, fp_size))
260 /* Check and rewrite the fprog via the skb checker */
261 ret = sk_chk_filter(filter->insns, filter->len);
265 /* Check and rewrite the fprog for seccomp use */
266 ret = seccomp_check_filter(filter->insns, filter->len);
271 * If there is an existing filter, make it the prev and don't drop its
274 filter->prev = current->seccomp.filter;
275 current->seccomp.filter = filter;
283 * seccomp_attach_user_filter - attaches a user-supplied sock_fprog
284 * @user_filter: pointer to the user data containing a sock_fprog.
286 * Returns 0 on success and non-zero otherwise.
288 long seccomp_attach_user_filter(char __user *user_filter)
290 struct sock_fprog fprog;
294 if (is_compat_task()) {
295 struct compat_sock_fprog fprog32;
296 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
298 fprog.len = fprog32.len;
299 fprog.filter = compat_ptr(fprog32.filter);
300 } else /* falls through to the if below. */
302 if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
304 ret = seccomp_attach_filter(&fprog);
309 /* get_seccomp_filter - increments the reference count of the filter on @tsk */
310 void get_seccomp_filter(struct task_struct *tsk)
312 struct seccomp_filter *orig = tsk->seccomp.filter;
315 /* Reference count is bounded by the number of total processes. */
316 atomic_inc(&orig->usage);
319 /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
320 void put_seccomp_filter(struct task_struct *tsk)
322 struct seccomp_filter *orig = tsk->seccomp.filter;
323 /* Clean up single-reference branches iteratively. */
324 while (orig && atomic_dec_and_test(&orig->usage)) {
325 struct seccomp_filter *freeme = orig;
330 #endif /* CONFIG_SECCOMP_FILTER */
333 * Secure computing mode 1 allows only read/write/exit/sigreturn.
334 * To be fully secure this must be combined with rlimit
335 * to limit the stack allocations too.
337 static int mode1_syscalls[] = {
338 __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
339 0, /* null terminated */
343 static int mode1_syscalls_32[] = {
344 __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
345 0, /* null terminated */
349 void __secure_computing(int this_syscall)
351 int mode = current->seccomp.mode;
356 case SECCOMP_MODE_STRICT:
357 syscall = mode1_syscalls;
359 if (is_compat_task())
360 syscall = mode1_syscalls_32;
363 if (*syscall == this_syscall)
365 } while (*++syscall);
368 #ifdef CONFIG_SECCOMP_FILTER
369 case SECCOMP_MODE_FILTER:
370 if (seccomp_run_filters(this_syscall) == SECCOMP_RET_ALLOW)
382 audit_seccomp(this_syscall, exit_code, SECCOMP_RET_KILL);
386 long prctl_get_seccomp(void)
388 return current->seccomp.mode;
392 * prctl_set_seccomp: configures current->seccomp.mode
393 * @seccomp_mode: requested mode to use
394 * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
396 * This function may be called repeatedly with a @seccomp_mode of
397 * SECCOMP_MODE_FILTER to install additional filters. Every filter
398 * successfully installed will be evaluated (in reverse order) for each system
399 * call the task makes.
401 * Once current->seccomp.mode is non-zero, it may not be changed.
403 * Returns 0 on success or -EINVAL on failure.
405 long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
409 if (current->seccomp.mode &&
410 current->seccomp.mode != seccomp_mode)
413 switch (seccomp_mode) {
414 case SECCOMP_MODE_STRICT:
420 #ifdef CONFIG_SECCOMP_FILTER
421 case SECCOMP_MODE_FILTER:
422 ret = seccomp_attach_user_filter(filter);
431 current->seccomp.mode = seccomp_mode;
432 set_thread_flag(TIF_SECCOMP);