]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/seccomp.c | |
3 | * | |
4 | * Copyright 2004-2005 Andrea Arcangeli <andrea@cpushare.com> | |
5 | * | |
e2cfabdf WD |
6 | * 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. | |
1da177e4 LT |
14 | */ |
15 | ||
e2cfabdf | 16 | #include <linux/atomic.h> |
85e7bac3 | 17 | #include <linux/audit.h> |
5b101740 | 18 | #include <linux/compat.h> |
284aa155 KC |
19 | #include <linux/nospec.h> |
20 | #include <linux/prctl.h> | |
e2cfabdf WD |
21 | #include <linux/sched.h> |
22 | #include <linux/seccomp.h> | |
9fd2b97a | 23 | #include <linux/syscalls.h> |
1da177e4 LT |
24 | |
25 | /* #define SECCOMP_DEBUG 1 */ | |
e2cfabdf WD |
26 | |
27 | #ifdef CONFIG_SECCOMP_FILTER | |
28 | #include <asm/syscall.h> | |
29 | #include <linux/filter.h> | |
fb0fadf9 | 30 | #include <linux/ptrace.h> |
e2cfabdf WD |
31 | #include <linux/security.h> |
32 | #include <linux/slab.h> | |
33 | #include <linux/tracehook.h> | |
34 | #include <linux/uaccess.h> | |
35 | ||
36 | /** | |
37 | * struct seccomp_filter - container for seccomp BPF programs | |
38 | * | |
39 | * @usage: reference count to manage the object lifetime. | |
40 | * get/put helpers should be used when accessing an instance | |
41 | * outside of a lifetime-guarded section. In general, this | |
42 | * is only needed for handling filters shared across tasks. | |
43 | * @prev: points to a previously installed, or inherited, filter | |
44 | * @len: the number of instructions in the program | |
119ce5c8 | 45 | * @insnsi: the BPF program instructions to evaluate |
e2cfabdf WD |
46 | * |
47 | * seccomp_filter objects are organized in a tree linked via the @prev | |
48 | * pointer. For any task, it appears to be a singly-linked list starting | |
49 | * with current->seccomp.filter, the most recently attached or inherited filter. | |
50 | * However, multiple filters may share a @prev node, by way of fork(), which | |
51 | * results in a unidirectional tree existing in memory. This is similar to | |
52 | * how namespaces work. | |
53 | * | |
54 | * seccomp_filter objects should never be modified after being attached | |
55 | * to a task_struct (other than @usage). | |
56 | */ | |
57 | struct seccomp_filter { | |
58 | atomic_t usage; | |
59 | struct seccomp_filter *prev; | |
8f577cad | 60 | struct sk_filter *prog; |
e2cfabdf WD |
61 | }; |
62 | ||
63 | /* Limit any path through the tree to 256KB worth of instructions. */ | |
64 | #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter)) | |
65 | ||
bd4cf0ed | 66 | /* |
e2cfabdf WD |
67 | * Endianness is explicitly ignored and left for BPF program authors to manage |
68 | * as per the specific architecture. | |
69 | */ | |
bd4cf0ed | 70 | static void populate_seccomp_data(struct seccomp_data *sd) |
e2cfabdf | 71 | { |
bd4cf0ed AS |
72 | struct task_struct *task = current; |
73 | struct pt_regs *regs = task_pt_regs(task); | |
2eac7648 | 74 | unsigned long args[6]; |
e2cfabdf | 75 | |
bd4cf0ed | 76 | sd->nr = syscall_get_nr(task, regs); |
0b747172 | 77 | sd->arch = syscall_get_arch(); |
2eac7648 DB |
78 | syscall_get_arguments(task, regs, 0, 6, args); |
79 | sd->args[0] = args[0]; | |
80 | sd->args[1] = args[1]; | |
81 | sd->args[2] = args[2]; | |
82 | sd->args[3] = args[3]; | |
83 | sd->args[4] = args[4]; | |
84 | sd->args[5] = args[5]; | |
bd4cf0ed | 85 | sd->instruction_pointer = KSTK_EIP(task); |
e2cfabdf WD |
86 | } |
87 | ||
88 | /** | |
89 | * seccomp_check_filter - verify seccomp filter code | |
90 | * @filter: filter to verify | |
91 | * @flen: length of filter | |
92 | * | |
93 | * Takes a previously checked filter (by sk_chk_filter) and | |
94 | * redirects all filter code that loads struct sk_buff data | |
95 | * and related data through seccomp_bpf_load. It also | |
96 | * enforces length and alignment checking of those loads. | |
97 | * | |
98 | * Returns 0 if the rule set is legal or -EINVAL if not. | |
99 | */ | |
100 | static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) | |
101 | { | |
102 | int pc; | |
103 | for (pc = 0; pc < flen; pc++) { | |
104 | struct sock_filter *ftest = &filter[pc]; | |
105 | u16 code = ftest->code; | |
106 | u32 k = ftest->k; | |
107 | ||
108 | switch (code) { | |
34805931 | 109 | case BPF_LD | BPF_W | BPF_ABS: |
bd4cf0ed | 110 | ftest->code = BPF_LDX | BPF_W | BPF_ABS; |
e2cfabdf WD |
111 | /* 32-bit aligned and not out of bounds. */ |
112 | if (k >= sizeof(struct seccomp_data) || k & 3) | |
113 | return -EINVAL; | |
114 | continue; | |
34805931 | 115 | case BPF_LD | BPF_W | BPF_LEN: |
bd4cf0ed | 116 | ftest->code = BPF_LD | BPF_IMM; |
e2cfabdf WD |
117 | ftest->k = sizeof(struct seccomp_data); |
118 | continue; | |
34805931 | 119 | case BPF_LDX | BPF_W | BPF_LEN: |
bd4cf0ed | 120 | ftest->code = BPF_LDX | BPF_IMM; |
e2cfabdf WD |
121 | ftest->k = sizeof(struct seccomp_data); |
122 | continue; | |
123 | /* Explicitly include allowed calls. */ | |
34805931 DB |
124 | case BPF_RET | BPF_K: |
125 | case BPF_RET | BPF_A: | |
126 | case BPF_ALU | BPF_ADD | BPF_K: | |
127 | case BPF_ALU | BPF_ADD | BPF_X: | |
128 | case BPF_ALU | BPF_SUB | BPF_K: | |
129 | case BPF_ALU | BPF_SUB | BPF_X: | |
130 | case BPF_ALU | BPF_MUL | BPF_K: | |
131 | case BPF_ALU | BPF_MUL | BPF_X: | |
132 | case BPF_ALU | BPF_DIV | BPF_K: | |
133 | case BPF_ALU | BPF_DIV | BPF_X: | |
134 | case BPF_ALU | BPF_AND | BPF_K: | |
135 | case BPF_ALU | BPF_AND | BPF_X: | |
136 | case BPF_ALU | BPF_OR | BPF_K: | |
137 | case BPF_ALU | BPF_OR | BPF_X: | |
138 | case BPF_ALU | BPF_XOR | BPF_K: | |
139 | case BPF_ALU | BPF_XOR | BPF_X: | |
140 | case BPF_ALU | BPF_LSH | BPF_K: | |
141 | case BPF_ALU | BPF_LSH | BPF_X: | |
142 | case BPF_ALU | BPF_RSH | BPF_K: | |
143 | case BPF_ALU | BPF_RSH | BPF_X: | |
144 | case BPF_ALU | BPF_NEG: | |
145 | case BPF_LD | BPF_IMM: | |
146 | case BPF_LDX | BPF_IMM: | |
147 | case BPF_MISC | BPF_TAX: | |
148 | case BPF_MISC | BPF_TXA: | |
149 | case BPF_LD | BPF_MEM: | |
150 | case BPF_LDX | BPF_MEM: | |
151 | case BPF_ST: | |
152 | case BPF_STX: | |
153 | case BPF_JMP | BPF_JA: | |
154 | case BPF_JMP | BPF_JEQ | BPF_K: | |
155 | case BPF_JMP | BPF_JEQ | BPF_X: | |
156 | case BPF_JMP | BPF_JGE | BPF_K: | |
157 | case BPF_JMP | BPF_JGE | BPF_X: | |
158 | case BPF_JMP | BPF_JGT | BPF_K: | |
159 | case BPF_JMP | BPF_JGT | BPF_X: | |
160 | case BPF_JMP | BPF_JSET | BPF_K: | |
161 | case BPF_JMP | BPF_JSET | BPF_X: | |
e2cfabdf WD |
162 | continue; |
163 | default: | |
164 | return -EINVAL; | |
165 | } | |
166 | } | |
167 | return 0; | |
168 | } | |
169 | ||
170 | /** | |
171 | * seccomp_run_filters - evaluates all seccomp filters against @syscall | |
172 | * @syscall: number of the current system call | |
173 | * | |
174 | * Returns valid seccomp BPF response codes. | |
175 | */ | |
176 | static u32 seccomp_run_filters(int syscall) | |
177 | { | |
178 | struct seccomp_filter *f; | |
bd4cf0ed | 179 | struct seccomp_data sd; |
acf3b2c7 WD |
180 | u32 ret = SECCOMP_RET_ALLOW; |
181 | ||
182 | /* Ensure unexpected behavior doesn't result in failing open. */ | |
183 | if (WARN_ON(current->seccomp.filter == NULL)) | |
184 | return SECCOMP_RET_KILL; | |
185 | ||
bd4cf0ed AS |
186 | populate_seccomp_data(&sd); |
187 | ||
e2cfabdf WD |
188 | /* |
189 | * All filters in the list are evaluated and the lowest BPF return | |
acf3b2c7 | 190 | * value always takes priority (ignoring the DATA). |
e2cfabdf WD |
191 | */ |
192 | for (f = current->seccomp.filter; f; f = f->prev) { | |
8f577cad AS |
193 | u32 cur_ret = SK_RUN_FILTER(f->prog, (void *)&sd); |
194 | ||
acf3b2c7 WD |
195 | if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION)) |
196 | ret = cur_ret; | |
e2cfabdf WD |
197 | } |
198 | return ret; | |
199 | } | |
f4ab7e36 | 200 | #endif /* CONFIG_SECCOMP_FILTER */ |
e2cfabdf | 201 | |
f4ab7e36 KC |
202 | static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode) |
203 | { | |
204 | if (current->seccomp.mode && current->seccomp.mode != seccomp_mode) | |
205 | return false; | |
206 | ||
207 | return true; | |
208 | } | |
209 | ||
34be01c4 | 210 | void __weak arch_seccomp_spec_mitigate(struct task_struct *task) { } |
284aa155 | 211 | |
92856049 KC |
212 | static inline void seccomp_assign_mode(unsigned long seccomp_mode, |
213 | unsigned long flags) | |
f4ab7e36 KC |
214 | { |
215 | current->seccomp.mode = seccomp_mode; | |
92856049 KC |
216 | /* Assume default seccomp processes want spec flaw mitigation. */ |
217 | if ((flags & SECCOMP_FILTER_FLAG_SPEC_ALLOW) == 0) | |
34be01c4 | 218 | arch_seccomp_spec_mitigate(current); |
f4ab7e36 KC |
219 | set_tsk_thread_flag(current, TIF_SECCOMP); |
220 | } | |
221 | ||
222 | #ifdef CONFIG_SECCOMP_FILTER | |
e2cfabdf WD |
223 | /** |
224 | * seccomp_attach_filter: Attaches a seccomp filter to current. | |
225 | * @fprog: BPF program to install | |
226 | * | |
227 | * Returns 0 on success or an errno on failure. | |
228 | */ | |
229 | static long seccomp_attach_filter(struct sock_fprog *fprog) | |
230 | { | |
231 | struct seccomp_filter *filter; | |
232 | unsigned long fp_size = fprog->len * sizeof(struct sock_filter); | |
233 | unsigned long total_insns = fprog->len; | |
bd4cf0ed AS |
234 | struct sock_filter *fp; |
235 | int new_len; | |
e2cfabdf WD |
236 | long ret; |
237 | ||
238 | if (fprog->len == 0 || fprog->len > BPF_MAXINSNS) | |
239 | return -EINVAL; | |
240 | ||
241 | for (filter = current->seccomp.filter; filter; filter = filter->prev) | |
8f577cad | 242 | total_insns += filter->prog->len + 4; /* include a 4 instr penalty */ |
e2cfabdf WD |
243 | if (total_insns > MAX_INSNS_PER_PATH) |
244 | return -ENOMEM; | |
245 | ||
246 | /* | |
119ce5c8 | 247 | * Installing a seccomp filter requires that the task has |
e2cfabdf WD |
248 | * CAP_SYS_ADMIN in its namespace or be running with no_new_privs. |
249 | * This avoids scenarios where unprivileged tasks can affect the | |
250 | * behavior of privileged children. | |
251 | */ | |
5f333321 | 252 | if (!task_no_new_privs(current) && |
e2cfabdf WD |
253 | security_capable_noaudit(current_cred(), current_user_ns(), |
254 | CAP_SYS_ADMIN) != 0) | |
255 | return -EACCES; | |
256 | ||
bd4cf0ed AS |
257 | fp = kzalloc(fp_size, GFP_KERNEL|__GFP_NOWARN); |
258 | if (!fp) | |
e2cfabdf | 259 | return -ENOMEM; |
e2cfabdf WD |
260 | |
261 | /* Copy the instructions from fprog. */ | |
262 | ret = -EFAULT; | |
bd4cf0ed AS |
263 | if (copy_from_user(fp, fprog->filter, fp_size)) |
264 | goto free_prog; | |
e2cfabdf WD |
265 | |
266 | /* Check and rewrite the fprog via the skb checker */ | |
bd4cf0ed | 267 | ret = sk_chk_filter(fp, fprog->len); |
e2cfabdf | 268 | if (ret) |
bd4cf0ed | 269 | goto free_prog; |
e2cfabdf WD |
270 | |
271 | /* Check and rewrite the fprog for seccomp use */ | |
bd4cf0ed AS |
272 | ret = seccomp_check_filter(fp, fprog->len); |
273 | if (ret) | |
274 | goto free_prog; | |
275 | ||
276 | /* Convert 'sock_filter' insns to 'sock_filter_int' insns */ | |
277 | ret = sk_convert_filter(fp, fprog->len, NULL, &new_len); | |
278 | if (ret) | |
279 | goto free_prog; | |
280 | ||
281 | /* Allocate a new seccomp_filter */ | |
0acf07d2 | 282 | ret = -ENOMEM; |
8f577cad | 283 | filter = kzalloc(sizeof(struct seccomp_filter), |
bd4cf0ed AS |
284 | GFP_KERNEL|__GFP_NOWARN); |
285 | if (!filter) | |
286 | goto free_prog; | |
287 | ||
8f577cad AS |
288 | filter->prog = kzalloc(sk_filter_size(new_len), |
289 | GFP_KERNEL|__GFP_NOWARN); | |
290 | if (!filter->prog) | |
bd4cf0ed | 291 | goto free_filter; |
8f577cad AS |
292 | |
293 | ret = sk_convert_filter(fp, fprog->len, filter->prog->insnsi, &new_len); | |
294 | if (ret) | |
295 | goto free_filter_prog; | |
0acf07d2 | 296 | kfree(fp); |
bd4cf0ed AS |
297 | |
298 | atomic_set(&filter->usage, 1); | |
8f577cad | 299 | filter->prog->len = new_len; |
8f577cad | 300 | |
5fe821a9 | 301 | sk_filter_select_runtime(filter->prog); |
e2cfabdf WD |
302 | |
303 | /* | |
304 | * If there is an existing filter, make it the prev and don't drop its | |
305 | * task reference. | |
306 | */ | |
307 | filter->prev = current->seccomp.filter; | |
308 | current->seccomp.filter = filter; | |
309 | return 0; | |
bd4cf0ed | 310 | |
8f577cad AS |
311 | free_filter_prog: |
312 | kfree(filter->prog); | |
bd4cf0ed | 313 | free_filter: |
e2cfabdf | 314 | kfree(filter); |
bd4cf0ed AS |
315 | free_prog: |
316 | kfree(fp); | |
e2cfabdf WD |
317 | return ret; |
318 | } | |
319 | ||
320 | /** | |
321 | * seccomp_attach_user_filter - attaches a user-supplied sock_fprog | |
322 | * @user_filter: pointer to the user data containing a sock_fprog. | |
323 | * | |
324 | * Returns 0 on success and non-zero otherwise. | |
325 | */ | |
9fd2b97a | 326 | static long seccomp_attach_user_filter(const char __user *user_filter) |
e2cfabdf WD |
327 | { |
328 | struct sock_fprog fprog; | |
329 | long ret = -EFAULT; | |
330 | ||
331 | #ifdef CONFIG_COMPAT | |
332 | if (is_compat_task()) { | |
333 | struct compat_sock_fprog fprog32; | |
334 | if (copy_from_user(&fprog32, user_filter, sizeof(fprog32))) | |
335 | goto out; | |
336 | fprog.len = fprog32.len; | |
337 | fprog.filter = compat_ptr(fprog32.filter); | |
338 | } else /* falls through to the if below. */ | |
339 | #endif | |
340 | if (copy_from_user(&fprog, user_filter, sizeof(fprog))) | |
341 | goto out; | |
342 | ret = seccomp_attach_filter(&fprog); | |
343 | out: | |
344 | return ret; | |
345 | } | |
346 | ||
347 | /* get_seccomp_filter - increments the reference count of the filter on @tsk */ | |
348 | void get_seccomp_filter(struct task_struct *tsk) | |
349 | { | |
350 | struct seccomp_filter *orig = tsk->seccomp.filter; | |
351 | if (!orig) | |
352 | return; | |
353 | /* Reference count is bounded by the number of total processes. */ | |
354 | atomic_inc(&orig->usage); | |
355 | } | |
356 | ||
357 | /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */ | |
358 | void put_seccomp_filter(struct task_struct *tsk) | |
359 | { | |
360 | struct seccomp_filter *orig = tsk->seccomp.filter; | |
361 | /* Clean up single-reference branches iteratively. */ | |
362 | while (orig && atomic_dec_and_test(&orig->usage)) { | |
363 | struct seccomp_filter *freeme = orig; | |
364 | orig = orig->prev; | |
5fe821a9 | 365 | sk_filter_free(freeme->prog); |
e2cfabdf WD |
366 | kfree(freeme); |
367 | } | |
368 | } | |
bb6ea430 WD |
369 | |
370 | /** | |
371 | * seccomp_send_sigsys - signals the task to allow in-process syscall emulation | |
372 | * @syscall: syscall number to send to userland | |
373 | * @reason: filter-supplied reason code to send to userland (via si_errno) | |
374 | * | |
375 | * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info. | |
376 | */ | |
377 | static void seccomp_send_sigsys(int syscall, int reason) | |
378 | { | |
379 | struct siginfo info; | |
380 | memset(&info, 0, sizeof(info)); | |
381 | info.si_signo = SIGSYS; | |
382 | info.si_code = SYS_SECCOMP; | |
383 | info.si_call_addr = (void __user *)KSTK_EIP(current); | |
384 | info.si_errno = reason; | |
5e937a9a | 385 | info.si_arch = syscall_get_arch(); |
bb6ea430 WD |
386 | info.si_syscall = syscall; |
387 | force_sig_info(SIGSYS, &info, current); | |
388 | } | |
e2cfabdf | 389 | #endif /* CONFIG_SECCOMP_FILTER */ |
1da177e4 LT |
390 | |
391 | /* | |
392 | * Secure computing mode 1 allows only read/write/exit/sigreturn. | |
393 | * To be fully secure this must be combined with rlimit | |
394 | * to limit the stack allocations too. | |
395 | */ | |
396 | static int mode1_syscalls[] = { | |
397 | __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn, | |
398 | 0, /* null terminated */ | |
399 | }; | |
400 | ||
5b101740 | 401 | #ifdef CONFIG_COMPAT |
1da177e4 LT |
402 | static int mode1_syscalls_32[] = { |
403 | __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32, | |
404 | 0, /* null terminated */ | |
405 | }; | |
406 | #endif | |
407 | ||
acf3b2c7 | 408 | int __secure_computing(int this_syscall) |
1da177e4 LT |
409 | { |
410 | int mode = current->seccomp.mode; | |
e2cfabdf WD |
411 | int exit_sig = 0; |
412 | int *syscall; | |
8156b451 | 413 | u32 ret; |
1da177e4 LT |
414 | |
415 | switch (mode) { | |
e2cfabdf | 416 | case SECCOMP_MODE_STRICT: |
1da177e4 | 417 | syscall = mode1_syscalls; |
5b101740 RM |
418 | #ifdef CONFIG_COMPAT |
419 | if (is_compat_task()) | |
1da177e4 LT |
420 | syscall = mode1_syscalls_32; |
421 | #endif | |
422 | do { | |
423 | if (*syscall == this_syscall) | |
acf3b2c7 | 424 | return 0; |
1da177e4 | 425 | } while (*++syscall); |
e2cfabdf | 426 | exit_sig = SIGKILL; |
8156b451 | 427 | ret = SECCOMP_RET_KILL; |
1da177e4 | 428 | break; |
e2cfabdf | 429 | #ifdef CONFIG_SECCOMP_FILTER |
8156b451 WD |
430 | case SECCOMP_MODE_FILTER: { |
431 | int data; | |
87b526d3 | 432 | struct pt_regs *regs = task_pt_regs(current); |
acf3b2c7 WD |
433 | ret = seccomp_run_filters(this_syscall); |
434 | data = ret & SECCOMP_RET_DATA; | |
8156b451 WD |
435 | ret &= SECCOMP_RET_ACTION; |
436 | switch (ret) { | |
acf3b2c7 WD |
437 | case SECCOMP_RET_ERRNO: |
438 | /* Set the low-order 16-bits as a errno. */ | |
87b526d3 | 439 | syscall_set_return_value(current, regs, |
acf3b2c7 WD |
440 | -data, 0); |
441 | goto skip; | |
bb6ea430 WD |
442 | case SECCOMP_RET_TRAP: |
443 | /* Show the handler the original registers. */ | |
87b526d3 | 444 | syscall_rollback(current, regs); |
bb6ea430 WD |
445 | /* Let the filter pass back 16 bits of data. */ |
446 | seccomp_send_sigsys(this_syscall, data); | |
447 | goto skip; | |
fb0fadf9 WD |
448 | case SECCOMP_RET_TRACE: |
449 | /* Skip these calls if there is no tracer. */ | |
87b526d3 AL |
450 | if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) { |
451 | syscall_set_return_value(current, regs, | |
452 | -ENOSYS, 0); | |
fb0fadf9 | 453 | goto skip; |
87b526d3 | 454 | } |
fb0fadf9 WD |
455 | /* Allow the BPF to provide the event message */ |
456 | ptrace_event(PTRACE_EVENT_SECCOMP, data); | |
457 | /* | |
458 | * The delivery of a fatal signal during event | |
459 | * notification may silently skip tracer notification. | |
460 | * Terminating the task now avoids executing a system | |
461 | * call that may not be intended. | |
462 | */ | |
463 | if (fatal_signal_pending(current)) | |
464 | break; | |
87b526d3 AL |
465 | if (syscall_get_nr(current, regs) < 0) |
466 | goto skip; /* Explicit request to skip. */ | |
467 | ||
fb0fadf9 | 468 | return 0; |
acf3b2c7 WD |
469 | case SECCOMP_RET_ALLOW: |
470 | return 0; | |
471 | case SECCOMP_RET_KILL: | |
472 | default: | |
473 | break; | |
474 | } | |
e2cfabdf WD |
475 | exit_sig = SIGSYS; |
476 | break; | |
8156b451 | 477 | } |
e2cfabdf | 478 | #endif |
1da177e4 LT |
479 | default: |
480 | BUG(); | |
481 | } | |
482 | ||
483 | #ifdef SECCOMP_DEBUG | |
484 | dump_stack(); | |
485 | #endif | |
acf3b2c7 | 486 | audit_seccomp(this_syscall, exit_sig, ret); |
e2cfabdf | 487 | do_exit(exit_sig); |
8156b451 | 488 | #ifdef CONFIG_SECCOMP_FILTER |
acf3b2c7 WD |
489 | skip: |
490 | audit_seccomp(this_syscall, exit_sig, ret); | |
8156b451 | 491 | #endif |
acf3b2c7 | 492 | return -1; |
1da177e4 | 493 | } |
1d9d02fe AA |
494 | |
495 | long prctl_get_seccomp(void) | |
496 | { | |
497 | return current->seccomp.mode; | |
498 | } | |
499 | ||
e2cfabdf | 500 | /** |
c8085221 | 501 | * seccomp_set_mode_strict: internal function for setting strict seccomp |
e2cfabdf WD |
502 | * |
503 | * Once current->seccomp.mode is non-zero, it may not be changed. | |
504 | * | |
505 | * Returns 0 on success or -EINVAL on failure. | |
506 | */ | |
c8085221 | 507 | static long seccomp_set_mode_strict(void) |
1d9d02fe | 508 | { |
c8085221 | 509 | const unsigned long seccomp_mode = SECCOMP_MODE_STRICT; |
e2cfabdf | 510 | long ret = -EINVAL; |
1d9d02fe | 511 | |
f4ab7e36 | 512 | if (!seccomp_may_assign_mode(seccomp_mode)) |
1d9d02fe AA |
513 | goto out; |
514 | ||
cf99abac | 515 | #ifdef TIF_NOTSC |
c8085221 | 516 | disable_TSC(); |
cf99abac | 517 | #endif |
92856049 | 518 | seccomp_assign_mode(seccomp_mode, 0); |
c8085221 KC |
519 | ret = 0; |
520 | ||
521 | out: | |
522 | ||
523 | return ret; | |
524 | } | |
525 | ||
e2cfabdf | 526 | #ifdef CONFIG_SECCOMP_FILTER |
c8085221 KC |
527 | /** |
528 | * seccomp_set_mode_filter: internal function for setting seccomp filter | |
9fd2b97a | 529 | * @flags: flags to change filter behavior |
c8085221 KC |
530 | * @filter: struct sock_fprog containing filter |
531 | * | |
532 | * This function may be called repeatedly to install additional filters. | |
533 | * Every filter successfully installed will be evaluated (in reverse order) | |
534 | * for each system call the task makes. | |
535 | * | |
536 | * Once current->seccomp.mode is non-zero, it may not be changed. | |
537 | * | |
538 | * Returns 0 on success or -EINVAL on failure. | |
539 | */ | |
9fd2b97a KC |
540 | static long seccomp_set_mode_filter(unsigned int flags, |
541 | const char __user *filter) | |
c8085221 KC |
542 | { |
543 | const unsigned long seccomp_mode = SECCOMP_MODE_FILTER; | |
544 | long ret = -EINVAL; | |
545 | ||
9fd2b97a | 546 | /* Validate flags. */ |
92856049 | 547 | if (flags & ~SECCOMP_FILTER_FLAG_MASK) |
9fd2b97a KC |
548 | goto out; |
549 | ||
c8085221 KC |
550 | if (!seccomp_may_assign_mode(seccomp_mode)) |
551 | goto out; | |
552 | ||
553 | ret = seccomp_attach_user_filter(filter); | |
554 | if (ret) | |
e2cfabdf | 555 | goto out; |
1d9d02fe | 556 | |
92856049 | 557 | seccomp_assign_mode(seccomp_mode, flags); |
e2cfabdf | 558 | out: |
1d9d02fe AA |
559 | return ret; |
560 | } | |
c8085221 | 561 | #else |
9fd2b97a KC |
562 | static inline long seccomp_set_mode_filter(unsigned int flags, |
563 | const char __user *filter) | |
c8085221 KC |
564 | { |
565 | return -EINVAL; | |
566 | } | |
567 | #endif | |
14d4b3d5 | 568 | |
9fd2b97a KC |
569 | /* Common entry point for both prctl and syscall. */ |
570 | static long do_seccomp(unsigned int op, unsigned int flags, | |
571 | const char __user *uargs) | |
572 | { | |
573 | switch (op) { | |
574 | case SECCOMP_SET_MODE_STRICT: | |
575 | if (flags != 0 || uargs != NULL) | |
576 | return -EINVAL; | |
577 | return seccomp_set_mode_strict(); | |
578 | case SECCOMP_SET_MODE_FILTER: | |
579 | return seccomp_set_mode_filter(flags, uargs); | |
580 | default: | |
581 | return -EINVAL; | |
582 | } | |
583 | } | |
584 | ||
585 | SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags, | |
586 | const char __user *, uargs) | |
587 | { | |
588 | return do_seccomp(op, flags, uargs); | |
589 | } | |
590 | ||
14d4b3d5 KC |
591 | /** |
592 | * prctl_set_seccomp: configures current->seccomp.mode | |
593 | * @seccomp_mode: requested mode to use | |
594 | * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER | |
595 | * | |
596 | * Returns 0 on success or -EINVAL on failure. | |
597 | */ | |
598 | long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter) | |
599 | { | |
9fd2b97a KC |
600 | unsigned int op; |
601 | char __user *uargs; | |
602 | ||
c8085221 KC |
603 | switch (seccomp_mode) { |
604 | case SECCOMP_MODE_STRICT: | |
9fd2b97a KC |
605 | op = SECCOMP_SET_MODE_STRICT; |
606 | /* | |
607 | * Setting strict mode through prctl always ignored filter, | |
608 | * so make sure it is always NULL here to pass the internal | |
609 | * check in do_seccomp(). | |
610 | */ | |
611 | uargs = NULL; | |
612 | break; | |
c8085221 | 613 | case SECCOMP_MODE_FILTER: |
9fd2b97a KC |
614 | op = SECCOMP_SET_MODE_FILTER; |
615 | uargs = filter; | |
616 | break; | |
c8085221 KC |
617 | default: |
618 | return -EINVAL; | |
619 | } | |
9fd2b97a KC |
620 | |
621 | /* prctl interface doesn't have flags, so they are always zero. */ | |
622 | return do_seccomp(op, 0, uargs); | |
14d4b3d5 | 623 | } |