drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / seccomp.c
CommitLineData
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>
e2cfabdf
WD
19#include <linux/sched.h>
20#include <linux/seccomp.h>
6fa3eb70
S
21#include <linux/slab.h>
22#include <linux/syscalls.h>
1da177e4
LT
23
24/* #define SECCOMP_DEBUG 1 */
e2cfabdf
WD
25
26#ifdef CONFIG_SECCOMP_FILTER
27#include <asm/syscall.h>
28#include <linux/filter.h>
6fa3eb70 29#include <linux/pid.h>
fb0fadf9 30#include <linux/ptrace.h>
e2cfabdf 31#include <linux/security.h>
e2cfabdf
WD
32#include <linux/tracehook.h>
33#include <linux/uaccess.h>
34
35/**
36 * struct seccomp_filter - container for seccomp BPF programs
37 *
38 * @usage: reference count to manage the object lifetime.
39 * get/put helpers should be used when accessing an instance
40 * outside of a lifetime-guarded section. In general, this
41 * is only needed for handling filters shared across tasks.
42 * @prev: points to a previously installed, or inherited, filter
43 * @len: the number of instructions in the program
44 * @insns: the BPF program instructions to evaluate
45 *
46 * seccomp_filter objects are organized in a tree linked via the @prev
47 * pointer. For any task, it appears to be a singly-linked list starting
48 * with current->seccomp.filter, the most recently attached or inherited filter.
49 * However, multiple filters may share a @prev node, by way of fork(), which
50 * results in a unidirectional tree existing in memory. This is similar to
51 * how namespaces work.
52 *
53 * seccomp_filter objects should never be modified after being attached
54 * to a task_struct (other than @usage).
55 */
56struct seccomp_filter {
57 atomic_t usage;
58 struct seccomp_filter *prev;
59 unsigned short len; /* Instruction count */
60 struct sock_filter insns[];
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
e2cfabdf
WD
66/**
67 * get_u32 - returns a u32 offset into data
68 * @data: a unsigned 64 bit value
69 * @index: 0 or 1 to return the first or second 32-bits
70 *
71 * This inline exists to hide the length of unsigned long. If a 32-bit
72 * unsigned long is passed in, it will be extended and the top 32-bits will be
73 * 0. If it is a 64-bit unsigned long, then whatever data is resident will be
74 * properly returned.
75 *
76 * Endianness is explicitly ignored and left for BPF program authors to manage
77 * as per the specific architecture.
78 */
79static inline u32 get_u32(u64 data, int index)
80{
81 return ((u32 *)&data)[index];
82}
83
84/* Helper for bpf_load below. */
85#define BPF_DATA(_name) offsetof(struct seccomp_data, _name)
86/**
87 * bpf_load: checks and returns a pointer to the requested offset
88 * @off: offset into struct seccomp_data to load from
89 *
90 * Returns the requested 32-bits of data.
91 * seccomp_check_filter() should assure that @off is 32-bit aligned
92 * and not out of bounds. Failure to do so is a BUG.
93 */
94u32 seccomp_bpf_load(int off)
95{
96 struct pt_regs *regs = task_pt_regs(current);
97 if (off == BPF_DATA(nr))
98 return syscall_get_nr(current, regs);
99 if (off == BPF_DATA(arch))
6fa3eb70 100 return syscall_get_arch();
e2cfabdf
WD
101 if (off >= BPF_DATA(args[0]) && off < BPF_DATA(args[6])) {
102 unsigned long value;
103 int arg = (off - BPF_DATA(args[0])) / sizeof(u64);
104 int index = !!(off % sizeof(u64));
105 syscall_get_arguments(current, regs, arg, 1, &value);
106 return get_u32(value, index);
107 }
108 if (off == BPF_DATA(instruction_pointer))
109 return get_u32(KSTK_EIP(current), 0);
110 if (off == BPF_DATA(instruction_pointer) + sizeof(u32))
111 return get_u32(KSTK_EIP(current), 1);
112 /* seccomp_check_filter should make this impossible. */
113 BUG();
114}
115
116/**
117 * seccomp_check_filter - verify seccomp filter code
118 * @filter: filter to verify
119 * @flen: length of filter
120 *
121 * Takes a previously checked filter (by sk_chk_filter) and
122 * redirects all filter code that loads struct sk_buff data
123 * and related data through seccomp_bpf_load. It also
124 * enforces length and alignment checking of those loads.
125 *
126 * Returns 0 if the rule set is legal or -EINVAL if not.
127 */
128static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
129{
130 int pc;
131 for (pc = 0; pc < flen; pc++) {
132 struct sock_filter *ftest = &filter[pc];
133 u16 code = ftest->code;
134 u32 k = ftest->k;
135
136 switch (code) {
137 case BPF_S_LD_W_ABS:
138 ftest->code = BPF_S_ANC_SECCOMP_LD_W;
139 /* 32-bit aligned and not out of bounds. */
140 if (k >= sizeof(struct seccomp_data) || k & 3)
141 return -EINVAL;
142 continue;
143 case BPF_S_LD_W_LEN:
144 ftest->code = BPF_S_LD_IMM;
145 ftest->k = sizeof(struct seccomp_data);
146 continue;
147 case BPF_S_LDX_W_LEN:
148 ftest->code = BPF_S_LDX_IMM;
149 ftest->k = sizeof(struct seccomp_data);
150 continue;
151 /* Explicitly include allowed calls. */
152 case BPF_S_RET_K:
153 case BPF_S_RET_A:
154 case BPF_S_ALU_ADD_K:
155 case BPF_S_ALU_ADD_X:
156 case BPF_S_ALU_SUB_K:
157 case BPF_S_ALU_SUB_X:
158 case BPF_S_ALU_MUL_K:
159 case BPF_S_ALU_MUL_X:
160 case BPF_S_ALU_DIV_X:
161 case BPF_S_ALU_AND_K:
162 case BPF_S_ALU_AND_X:
163 case BPF_S_ALU_OR_K:
164 case BPF_S_ALU_OR_X:
d1327479
NS
165 case BPF_S_ALU_XOR_K:
166 case BPF_S_ALU_XOR_X:
e2cfabdf
WD
167 case BPF_S_ALU_LSH_K:
168 case BPF_S_ALU_LSH_X:
169 case BPF_S_ALU_RSH_K:
170 case BPF_S_ALU_RSH_X:
171 case BPF_S_ALU_NEG:
172 case BPF_S_LD_IMM:
173 case BPF_S_LDX_IMM:
174 case BPF_S_MISC_TAX:
175 case BPF_S_MISC_TXA:
176 case BPF_S_ALU_DIV_K:
177 case BPF_S_LD_MEM:
178 case BPF_S_LDX_MEM:
179 case BPF_S_ST:
180 case BPF_S_STX:
181 case BPF_S_JMP_JA:
182 case BPF_S_JMP_JEQ_K:
183 case BPF_S_JMP_JEQ_X:
184 case BPF_S_JMP_JGE_K:
185 case BPF_S_JMP_JGE_X:
186 case BPF_S_JMP_JGT_K:
187 case BPF_S_JMP_JGT_X:
188 case BPF_S_JMP_JSET_K:
189 case BPF_S_JMP_JSET_X:
190 continue;
191 default:
192 return -EINVAL;
193 }
194 }
195 return 0;
196}
197
198/**
199 * seccomp_run_filters - evaluates all seccomp filters against @syscall
200 * @syscall: number of the current system call
201 *
202 * Returns valid seccomp BPF response codes.
203 */
204static u32 seccomp_run_filters(int syscall)
205{
6fa3eb70 206 struct seccomp_filter *f = ACCESS_ONCE(current->seccomp.filter);
acf3b2c7
WD
207 u32 ret = SECCOMP_RET_ALLOW;
208
209 /* Ensure unexpected behavior doesn't result in failing open. */
6fa3eb70 210 if (unlikely(WARN_ON(f == NULL)))
acf3b2c7
WD
211 return SECCOMP_RET_KILL;
212
6fa3eb70
S
213 /* Make sure cross-thread synced filter points somewhere sane. */
214 smp_read_barrier_depends();
215
e2cfabdf
WD
216 /*
217 * All filters in the list are evaluated and the lowest BPF return
acf3b2c7 218 * value always takes priority (ignoring the DATA).
e2cfabdf 219 */
6fa3eb70 220 for (; f; f = f->prev) {
acf3b2c7 221 u32 cur_ret = sk_run_filter(NULL, f->insns);
6fa3eb70 222
acf3b2c7
WD
223 if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
224 ret = cur_ret;
e2cfabdf
WD
225 }
226 return ret;
227}
6fa3eb70
S
228#endif /* CONFIG_SECCOMP_FILTER */
229
230static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
231{
232 assert_spin_locked(&current->sighand->siglock);
233
234 if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
235 return false;
236
237 return true;
238}
239
240static inline void seccomp_assign_mode(struct task_struct *task,
241 unsigned long seccomp_mode)
242{
243 assert_spin_locked(&task->sighand->siglock);
244
245 task->seccomp.mode = seccomp_mode;
246 /*
247 * Make sure TIF_SECCOMP cannot be set before the mode (and
248 * filter) is set.
249 */
250 smp_mb();
251 set_tsk_thread_flag(task, TIF_SECCOMP);
252}
253
254#ifdef CONFIG_SECCOMP_FILTER
255/* Returns 1 if the parent is an ancestor of the child. */
256static int is_ancestor(struct seccomp_filter *parent,
257 struct seccomp_filter *child)
258{
259 /* NULL is the root ancestor. */
260 if (parent == NULL)
261 return 1;
262 for (; child; child = child->prev)
263 if (child == parent)
264 return 1;
265 return 0;
266}
e2cfabdf
WD
267
268/**
6fa3eb70
S
269 * seccomp_can_sync_threads: checks if all threads can be synchronized
270 *
271 * Expects sighand and cred_guard_mutex locks to be held.
272 *
273 * Returns 0 on success, -ve on error, or the pid of a thread which was
274 * either not in the correct seccomp mode or it did not have an ancestral
275 * seccomp filter.
276 */
277static inline pid_t seccomp_can_sync_threads(void)
278{
279 struct task_struct *thread, *caller;
280
281 BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
282 assert_spin_locked(&current->sighand->siglock);
283
284 /* Validate all threads being eligible for synchronization. */
285 caller = current;
286 for_each_thread(caller, thread) {
287 pid_t failed;
288
289 /* Skip current, since it is initiating the sync. */
290 if (thread == caller)
291 continue;
292
293 if (thread->seccomp.mode == SECCOMP_MODE_DISABLED ||
294 (thread->seccomp.mode == SECCOMP_MODE_FILTER &&
295 is_ancestor(thread->seccomp.filter,
296 caller->seccomp.filter)))
297 continue;
298
299 /* Return the first thread that cannot be synchronized. */
300 failed = task_pid_vnr(thread);
301 /* If the pid cannot be resolved, then return -ESRCH */
302 if (unlikely(WARN_ON(failed == 0)))
303 failed = -ESRCH;
304 return failed;
305 }
306
307 return 0;
308}
309
310/**
311 * seccomp_sync_threads: sets all threads to use current's filter
312 *
313 * Expects sighand and cred_guard_mutex locks to be held, and for
314 * seccomp_can_sync_threads() to have returned success already
315 * without dropping the locks.
316 *
317 */
318static inline void seccomp_sync_threads(void)
319{
320 struct task_struct *thread, *caller;
321
322 BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
323 assert_spin_locked(&current->sighand->siglock);
324
325 /* Synchronize all threads. */
326 caller = current;
327 for_each_thread(caller, thread) {
328 /* Skip current, since it needs no changes. */
329 if (thread == caller)
330 continue;
331
332 /* Get a task reference for the new leaf node. */
333 get_seccomp_filter(caller);
334 /*
335 * Drop the task reference to the shared ancestor since
336 * current's path will hold a reference. (This also
337 * allows a put before the assignment.)
338 */
339 put_seccomp_filter(thread);
340 smp_store_release(&thread->seccomp.filter,
341 caller->seccomp.filter);
342 /*
343 * Opt the other thread into seccomp if needed.
344 * As threads are considered to be trust-realm
345 * equivalent (see ptrace_may_access), it is safe to
346 * allow one thread to transition the other.
347 */
348 if (thread->seccomp.mode == SECCOMP_MODE_DISABLED) {
349 /*
350 * Don't let an unprivileged task work around
351 * the no_new_privs restriction by creating
352 * a thread that sets it up, enters seccomp,
353 * then dies.
354 */
355 if (task_no_new_privs(caller))
356 task_set_no_new_privs(thread);
357
358 seccomp_assign_mode(thread, SECCOMP_MODE_FILTER);
359 }
360 }
361}
362
363/**
364 * seccomp_prepare_filter: Prepares a seccomp filter for use.
e2cfabdf
WD
365 * @fprog: BPF program to install
366 *
6fa3eb70 367 * Returns filter on success or an ERR_PTR on failure.
e2cfabdf 368 */
6fa3eb70 369static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
e2cfabdf
WD
370{
371 struct seccomp_filter *filter;
372 unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
373 unsigned long total_insns = fprog->len;
374 long ret;
375
376 if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
6fa3eb70
S
377 return ERR_PTR(-EINVAL);
378 BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
e2cfabdf
WD
379
380 for (filter = current->seccomp.filter; filter; filter = filter->prev)
381 total_insns += filter->len + 4; /* include a 4 instr penalty */
382 if (total_insns > MAX_INSNS_PER_PATH)
6fa3eb70 383 return ERR_PTR(-ENOMEM);
e2cfabdf
WD
384
385 /*
386 * Installing a seccomp filter requires that the task have
387 * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
388 * This avoids scenarios where unprivileged tasks can affect the
389 * behavior of privileged children.
390 */
6fa3eb70 391 if (!task_no_new_privs(current) &&
e2cfabdf
WD
392 security_capable_noaudit(current_cred(), current_user_ns(),
393 CAP_SYS_ADMIN) != 0)
6fa3eb70 394 return ERR_PTR(-EACCES);
e2cfabdf
WD
395
396 /* Allocate a new seccomp_filter */
397 filter = kzalloc(sizeof(struct seccomp_filter) + fp_size,
398 GFP_KERNEL|__GFP_NOWARN);
399 if (!filter)
6fa3eb70 400 return ERR_PTR(-ENOMEM);;
e2cfabdf
WD
401 atomic_set(&filter->usage, 1);
402 filter->len = fprog->len;
403
404 /* Copy the instructions from fprog. */
405 ret = -EFAULT;
406 if (copy_from_user(filter->insns, fprog->filter, fp_size))
407 goto fail;
408
409 /* Check and rewrite the fprog via the skb checker */
410 ret = sk_chk_filter(filter->insns, filter->len);
411 if (ret)
412 goto fail;
413
414 /* Check and rewrite the fprog for seccomp use */
415 ret = seccomp_check_filter(filter->insns, filter->len);
416 if (ret)
417 goto fail;
418
6fa3eb70
S
419 return filter;
420
e2cfabdf
WD
421fail:
422 kfree(filter);
6fa3eb70 423 return ERR_PTR(ret);
e2cfabdf
WD
424}
425
426/**
6fa3eb70 427 * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
e2cfabdf
WD
428 * @user_filter: pointer to the user data containing a sock_fprog.
429 *
430 * Returns 0 on success and non-zero otherwise.
431 */
6fa3eb70
S
432static struct seccomp_filter *
433seccomp_prepare_user_filter(const char __user *user_filter)
e2cfabdf
WD
434{
435 struct sock_fprog fprog;
6fa3eb70 436 struct seccomp_filter *filter = ERR_PTR(-EFAULT);
e2cfabdf
WD
437
438#ifdef CONFIG_COMPAT
439 if (is_compat_task()) {
440 struct compat_sock_fprog fprog32;
441 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
442 goto out;
443 fprog.len = fprog32.len;
444 fprog.filter = compat_ptr(fprog32.filter);
445 } else /* falls through to the if below. */
446#endif
447 if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
448 goto out;
6fa3eb70 449 filter = seccomp_prepare_filter(&fprog);
e2cfabdf 450out:
6fa3eb70
S
451 return filter;
452}
453
454/**
455 * seccomp_attach_filter: validate and attach filter
456 * @flags: flags to change filter behavior
457 * @filter: seccomp filter to add to the current process
458 *
459 * Caller must be holding current->sighand->siglock lock.
460 *
461 * Returns 0 on success, -ve on error.
462 */
463static long seccomp_attach_filter(unsigned int flags,
464 struct seccomp_filter *filter)
465{
466 unsigned long total_insns;
467 struct seccomp_filter *walker;
468
469 assert_spin_locked(&current->sighand->siglock);
470
471 /* Validate resulting filter length. */
472 total_insns = filter->len;
473 for (walker = current->seccomp.filter; walker; walker = walker->prev)
474 total_insns += walker->len + 4; /* 4 instr penalty */
475 if (total_insns > MAX_INSNS_PER_PATH)
476 return -ENOMEM;
477
478 /* If thread sync has been requested, check that it is possible. */
479 if (flags & SECCOMP_FILTER_FLAG_TSYNC) {
480 int ret;
481
482 ret = seccomp_can_sync_threads();
483 if (ret)
484 return ret;
485 }
486
487 /*
488 * If there is an existing filter, make it the prev and don't drop its
489 * task reference.
490 */
491 filter->prev = current->seccomp.filter;
492 current->seccomp.filter = filter;
493
494 /* Now that the new filter is in place, synchronize to all threads. */
495 if (flags & SECCOMP_FILTER_FLAG_TSYNC)
496 seccomp_sync_threads();
497
498 return 0;
e2cfabdf
WD
499}
500
501/* get_seccomp_filter - increments the reference count of the filter on @tsk */
502void get_seccomp_filter(struct task_struct *tsk)
503{
504 struct seccomp_filter *orig = tsk->seccomp.filter;
505 if (!orig)
506 return;
507 /* Reference count is bounded by the number of total processes. */
508 atomic_inc(&orig->usage);
509}
510
6fa3eb70
S
511static inline void seccomp_filter_free(struct seccomp_filter *filter)
512{
513 if (filter) {
514 kfree(filter);
515 }
516}
517
e2cfabdf
WD
518/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
519void put_seccomp_filter(struct task_struct *tsk)
520{
521 struct seccomp_filter *orig = tsk->seccomp.filter;
522 /* Clean up single-reference branches iteratively. */
523 while (orig && atomic_dec_and_test(&orig->usage)) {
524 struct seccomp_filter *freeme = orig;
525 orig = orig->prev;
6fa3eb70 526 seccomp_filter_free(freeme);
e2cfabdf
WD
527 }
528}
bb6ea430
WD
529
530/**
531 * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
532 * @syscall: syscall number to send to userland
533 * @reason: filter-supplied reason code to send to userland (via si_errno)
534 *
535 * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
536 */
537static void seccomp_send_sigsys(int syscall, int reason)
538{
539 struct siginfo info;
540 memset(&info, 0, sizeof(info));
541 info.si_signo = SIGSYS;
542 info.si_code = SYS_SECCOMP;
543 info.si_call_addr = (void __user *)KSTK_EIP(current);
544 info.si_errno = reason;
6fa3eb70 545 info.si_arch = syscall_get_arch();
bb6ea430
WD
546 info.si_syscall = syscall;
547 force_sig_info(SIGSYS, &info, current);
548}
e2cfabdf 549#endif /* CONFIG_SECCOMP_FILTER */
1da177e4
LT
550
551/*
552 * Secure computing mode 1 allows only read/write/exit/sigreturn.
553 * To be fully secure this must be combined with rlimit
554 * to limit the stack allocations too.
555 */
556static int mode1_syscalls[] = {
557 __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
558 0, /* null terminated */
559};
560
5b101740 561#ifdef CONFIG_COMPAT
1da177e4
LT
562static int mode1_syscalls_32[] = {
563 __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
564 0, /* null terminated */
565};
566#endif
567
acf3b2c7 568int __secure_computing(int this_syscall)
1da177e4 569{
e2cfabdf
WD
570 int exit_sig = 0;
571 int *syscall;
8156b451 572 u32 ret;
1da177e4 573
6fa3eb70
S
574 /*
575 * Make sure that any changes to mode from another thread have
576 * been seen after TIF_SECCOMP was seen.
577 */
578 rmb();
579
580 switch (current->seccomp.mode) {
e2cfabdf 581 case SECCOMP_MODE_STRICT:
1da177e4 582 syscall = mode1_syscalls;
5b101740
RM
583#ifdef CONFIG_COMPAT
584 if (is_compat_task())
1da177e4
LT
585 syscall = mode1_syscalls_32;
586#endif
587 do {
588 if (*syscall == this_syscall)
acf3b2c7 589 return 0;
1da177e4 590 } while (*++syscall);
e2cfabdf 591 exit_sig = SIGKILL;
8156b451 592 ret = SECCOMP_RET_KILL;
1da177e4 593 break;
e2cfabdf 594#ifdef CONFIG_SECCOMP_FILTER
8156b451
WD
595 case SECCOMP_MODE_FILTER: {
596 int data;
87b526d3 597 struct pt_regs *regs = task_pt_regs(current);
acf3b2c7
WD
598 ret = seccomp_run_filters(this_syscall);
599 data = ret & SECCOMP_RET_DATA;
8156b451
WD
600 ret &= SECCOMP_RET_ACTION;
601 switch (ret) {
acf3b2c7
WD
602 case SECCOMP_RET_ERRNO:
603 /* Set the low-order 16-bits as a errno. */
87b526d3 604 syscall_set_return_value(current, regs,
acf3b2c7
WD
605 -data, 0);
606 goto skip;
bb6ea430
WD
607 case SECCOMP_RET_TRAP:
608 /* Show the handler the original registers. */
87b526d3 609 syscall_rollback(current, regs);
bb6ea430
WD
610 /* Let the filter pass back 16 bits of data. */
611 seccomp_send_sigsys(this_syscall, data);
612 goto skip;
fb0fadf9
WD
613 case SECCOMP_RET_TRACE:
614 /* Skip these calls if there is no tracer. */
87b526d3
AL
615 if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
616 syscall_set_return_value(current, regs,
617 -ENOSYS, 0);
fb0fadf9 618 goto skip;
87b526d3 619 }
fb0fadf9
WD
620 /* Allow the BPF to provide the event message */
621 ptrace_event(PTRACE_EVENT_SECCOMP, data);
622 /*
623 * The delivery of a fatal signal during event
624 * notification may silently skip tracer notification.
625 * Terminating the task now avoids executing a system
626 * call that may not be intended.
627 */
628 if (fatal_signal_pending(current))
629 break;
87b526d3
AL
630 if (syscall_get_nr(current, regs) < 0)
631 goto skip; /* Explicit request to skip. */
632
fb0fadf9 633 return 0;
acf3b2c7
WD
634 case SECCOMP_RET_ALLOW:
635 return 0;
636 case SECCOMP_RET_KILL:
637 default:
638 break;
639 }
e2cfabdf
WD
640 exit_sig = SIGSYS;
641 break;
8156b451 642 }
e2cfabdf 643#endif
1da177e4
LT
644 default:
645 BUG();
646 }
647
648#ifdef SECCOMP_DEBUG
649 dump_stack();
650#endif
acf3b2c7 651 audit_seccomp(this_syscall, exit_sig, ret);
e2cfabdf 652 do_exit(exit_sig);
8156b451 653#ifdef CONFIG_SECCOMP_FILTER
acf3b2c7
WD
654skip:
655 audit_seccomp(this_syscall, exit_sig, ret);
8156b451 656#endif
acf3b2c7 657 return -1;
1da177e4 658}
1d9d02fe
AA
659
660long prctl_get_seccomp(void)
661{
662 return current->seccomp.mode;
663}
664
e2cfabdf 665/**
6fa3eb70
S
666 * seccomp_set_mode_strict: internal function for setting strict seccomp
667 *
668 * Once current->seccomp.mode is non-zero, it may not be changed.
669 *
670 * Returns 0 on success or -EINVAL on failure.
671 */
672static long seccomp_set_mode_strict(void)
673{
674 const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
675 long ret = -EINVAL;
676
677 spin_lock_irq(&current->sighand->siglock);
678
679 if (!seccomp_may_assign_mode(seccomp_mode))
680 goto out;
681
682#ifdef TIF_NOTSC
683 disable_TSC();
684#endif
685 seccomp_assign_mode(current, seccomp_mode);
686 ret = 0;
687
688out:
689 spin_unlock_irq(&current->sighand->siglock);
690
691 return ret;
692}
693
694#ifdef CONFIG_SECCOMP_FILTER
695/**
696 * seccomp_set_mode_filter: internal function for setting seccomp filter
697 * @flags: flags to change filter behavior
698 * @filter: struct sock_fprog containing filter
e2cfabdf 699 *
6fa3eb70
S
700 * This function may be called repeatedly to install additional filters.
701 * Every filter successfully installed will be evaluated (in reverse order)
702 * for each system call the task makes.
e2cfabdf
WD
703 *
704 * Once current->seccomp.mode is non-zero, it may not be changed.
705 *
706 * Returns 0 on success or -EINVAL on failure.
707 */
6fa3eb70
S
708static long seccomp_set_mode_filter(unsigned int flags,
709 const char __user *filter)
1d9d02fe 710{
6fa3eb70
S
711 const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
712 struct seccomp_filter *prepared = NULL;
e2cfabdf 713 long ret = -EINVAL;
1d9d02fe 714
6fa3eb70
S
715 /* Validate flags. */
716 if (flags & ~SECCOMP_FILTER_FLAG_MASK)
717 return -EINVAL;
718
719 /* Prepare the new filter before holding any locks. */
720 prepared = seccomp_prepare_user_filter(filter);
721 if (IS_ERR(prepared))
722 return PTR_ERR(prepared);
723
724 /*
725 * Make sure we cannot change seccomp or nnp state via TSYNC
726 * while another thread is in the middle of calling exec.
727 */
728 if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
729 mutex_lock_killable(&current->signal->cred_guard_mutex))
730 goto out_free;
731
732 spin_lock_irq(&current->sighand->siglock);
733
734 if (!seccomp_may_assign_mode(seccomp_mode))
735 goto out;
736
737 ret = seccomp_attach_filter(flags, prepared);
738 if (ret)
1d9d02fe 739 goto out;
6fa3eb70
S
740 /* Do not free the successfully attached filter. */
741 prepared = NULL;
742
743 seccomp_assign_mode(current, seccomp_mode);
744out:
745 spin_unlock_irq(&current->sighand->siglock);
746 if (flags & SECCOMP_FILTER_FLAG_TSYNC)
747 mutex_unlock(&current->signal->cred_guard_mutex);
748out_free:
749 seccomp_filter_free(prepared);
750 return ret;
751}
752#else
753static inline long seccomp_set_mode_filter(unsigned int flags,
754 const char __user *filter)
755{
756 return -EINVAL;
757}
758#endif
759
760/* Common entry point for both prctl and syscall. */
761static long do_seccomp(unsigned int op, unsigned int flags,
762 const char __user *uargs)
763{
764 switch (op) {
765 case SECCOMP_SET_MODE_STRICT:
766 if (flags != 0 || uargs != NULL)
767 return -EINVAL;
768 return seccomp_set_mode_strict();
769 case SECCOMP_SET_MODE_FILTER:
770 return seccomp_set_mode_filter(flags, uargs);
771 default:
772 return -EINVAL;
773 }
774}
775
776SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
777 const char __user *, uargs)
778{
779 return do_seccomp(op, flags, uargs);
780}
781
782/**
783 * prctl_set_seccomp: configures current->seccomp.mode
784 * @seccomp_mode: requested mode to use
785 * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
786 *
787 * Returns 0 on success or -EINVAL on failure.
788 */
789long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
790{
791 unsigned int op;
792 char __user *uargs;
1d9d02fe 793
e2cfabdf
WD
794 switch (seccomp_mode) {
795 case SECCOMP_MODE_STRICT:
6fa3eb70
S
796 op = SECCOMP_SET_MODE_STRICT;
797 /*
798 * Setting strict mode through prctl always ignored filter,
799 * so make sure it is always NULL here to pass the internal
800 * check in do_seccomp().
801 */
802 uargs = NULL;
e2cfabdf 803 break;
e2cfabdf 804 case SECCOMP_MODE_FILTER:
6fa3eb70
S
805 op = SECCOMP_SET_MODE_FILTER;
806 uargs = filter;
e2cfabdf 807 break;
e2cfabdf 808 default:
6fa3eb70 809 return -EINVAL;
1d9d02fe
AA
810 }
811
6fa3eb70
S
812 /* prctl interface doesn't have flags, so they are always zero. */
813 return do_seccomp(op, 0, uargs);
1d9d02fe 814}