Merge branch 'linux-linaro-lsk' into linux-linaro-lsk-android
[firefly-linux-kernel-4.4.55.git] / kernel / seccomp.c
1 /*
2  * linux/kernel/seccomp.c
3  *
4  * Copyright 2004-2005  Andrea Arcangeli <andrea@cpushare.com>
5  *
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.
14  */
15
16 #include <linux/atomic.h>
17 #include <linux/audit.h>
18 #include <linux/compat.h>
19 #include <linux/sched.h>
20 #include <linux/seccomp.h>
21 #include <linux/slab.h>
22 #include <linux/syscalls.h>
23
24 /* #define SECCOMP_DEBUG 1 */
25
26 #ifdef CONFIG_SECCOMP_FILTER
27 #include <asm/syscall.h>
28 #include <linux/filter.h>
29 #include <linux/pid.h>
30 #include <linux/ptrace.h>
31 #include <linux/security.h>
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  */
56 struct 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
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  */
79 static 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  */
94 u32 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))
100                 return syscall_get_arch(current, regs);
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  */
128 static 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:
165                 case BPF_S_ALU_XOR_K:
166                 case BPF_S_ALU_XOR_X:
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  */
204 static u32 seccomp_run_filters(int syscall)
205 {
206         struct seccomp_filter *f = ACCESS_ONCE(current->seccomp.filter);
207         struct seccomp_data sd;
208         u32 ret = SECCOMP_RET_ALLOW;
209
210         /* Ensure unexpected behavior doesn't result in failing open. */
211         if (unlikely(WARN_ON(f == NULL)))
212                 return SECCOMP_RET_KILL;
213
214         /* Make sure cross-thread synced filter points somewhere sane. */
215         smp_read_barrier_depends();
216
217         populate_seccomp_data(&sd);
218
219         /*
220          * All filters in the list are evaluated and the lowest BPF return
221          * value always takes priority (ignoring the DATA).
222          */
223         for (; f; f = f->prev) {
224                 u32 cur_ret = SK_RUN_FILTER(f->prog, (void *)&sd);
225
226                 if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
227                         ret = cur_ret;
228         }
229         return ret;
230 }
231 #endif /* CONFIG_SECCOMP_FILTER */
232
233 static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
234 {
235         BUG_ON(!spin_is_locked(&current->sighand->siglock));
236
237         if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
238                 return false;
239
240         return true;
241 }
242
243 static inline void seccomp_assign_mode(struct task_struct *task,
244                                        unsigned long seccomp_mode)
245 {
246         BUG_ON(!spin_is_locked(&task->sighand->siglock));
247
248         task->seccomp.mode = seccomp_mode;
249         /*
250          * Make sure TIF_SECCOMP cannot be set before the mode (and
251          * filter) is set.
252          */
253         smp_mb__before_atomic();
254         set_tsk_thread_flag(task, TIF_SECCOMP);
255 }
256
257 #ifdef CONFIG_SECCOMP_FILTER
258 /* Returns 1 if the parent is an ancestor of the child. */
259 static int is_ancestor(struct seccomp_filter *parent,
260                        struct seccomp_filter *child)
261 {
262         /* NULL is the root ancestor. */
263         if (parent == NULL)
264                 return 1;
265         for (; child; child = child->prev)
266                 if (child == parent)
267                         return 1;
268         return 0;
269 }
270
271 /**
272  * seccomp_can_sync_threads: checks if all threads can be synchronized
273  *
274  * Expects sighand and cred_guard_mutex locks to be held.
275  *
276  * Returns 0 on success, -ve on error, or the pid of a thread which was
277  * either not in the correct seccomp mode or it did not have an ancestral
278  * seccomp filter.
279  */
280 static inline pid_t seccomp_can_sync_threads(void)
281 {
282         struct task_struct *thread, *caller;
283
284         BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
285         BUG_ON(!spin_is_locked(&current->sighand->siglock));
286
287         /* Validate all threads being eligible for synchronization. */
288         caller = current;
289         for_each_thread(caller, thread) {
290                 pid_t failed;
291
292                 /* Skip current, since it is initiating the sync. */
293                 if (thread == caller)
294                         continue;
295
296                 if (thread->seccomp.mode == SECCOMP_MODE_DISABLED ||
297                     (thread->seccomp.mode == SECCOMP_MODE_FILTER &&
298                      is_ancestor(thread->seccomp.filter,
299                                  caller->seccomp.filter)))
300                         continue;
301
302                 /* Return the first thread that cannot be synchronized. */
303                 failed = task_pid_vnr(thread);
304                 /* If the pid cannot be resolved, then return -ESRCH */
305                 if (unlikely(WARN_ON(failed == 0)))
306                         failed = -ESRCH;
307                 return failed;
308         }
309
310         return 0;
311 }
312
313 /**
314  * seccomp_sync_threads: sets all threads to use current's filter
315  *
316  * Expects sighand and cred_guard_mutex locks to be held, and for
317  * seccomp_can_sync_threads() to have returned success already
318  * without dropping the locks.
319  *
320  */
321 static inline void seccomp_sync_threads(void)
322 {
323         struct task_struct *thread, *caller;
324
325         BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
326         BUG_ON(!spin_is_locked(&current->sighand->siglock));
327
328         /* Synchronize all threads. */
329         caller = current;
330         for_each_thread(caller, thread) {
331                 /* Skip current, since it needs no changes. */
332                 if (thread == caller)
333                         continue;
334
335                 /* Get a task reference for the new leaf node. */
336                 get_seccomp_filter(caller);
337                 /*
338                  * Drop the task reference to the shared ancestor since
339                  * current's path will hold a reference.  (This also
340                  * allows a put before the assignment.)
341                  */
342                 put_seccomp_filter(thread);
343                 smp_store_release(&thread->seccomp.filter,
344                                   caller->seccomp.filter);
345                 /*
346                  * Opt the other thread into seccomp if needed.
347                  * As threads are considered to be trust-realm
348                  * equivalent (see ptrace_may_access), it is safe to
349                  * allow one thread to transition the other.
350                  */
351                 if (thread->seccomp.mode == SECCOMP_MODE_DISABLED) {
352                         /*
353                          * Don't let an unprivileged task work around
354                          * the no_new_privs restriction by creating
355                          * a thread that sets it up, enters seccomp,
356                          * then dies.
357                          */
358                         if (task_no_new_privs(caller))
359                                 task_set_no_new_privs(thread);
360
361                         seccomp_assign_mode(thread, SECCOMP_MODE_FILTER);
362                 }
363         }
364 }
365
366 /**
367  * seccomp_prepare_filter: Prepares a seccomp filter for use.
368  * @fprog: BPF program to install
369  *
370  * Returns filter on success or an ERR_PTR on failure.
371  */
372 static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
373 {
374         struct seccomp_filter *filter;
375         unsigned long fp_size;
376         struct sock_filter *fp;
377         int new_len;
378         long ret;
379
380         if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
381                 return ERR_PTR(-EINVAL);
382         BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
383         fp_size = fprog->len * sizeof(struct sock_filter);
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          */
391         if (!current->no_new_privs &&
392             security_capable_noaudit(current_cred(), current_user_ns(),
393                                      CAP_SYS_ADMIN) != 0)
394                 return ERR_PTR(-EACCES);
395
396         fp = kzalloc(fp_size, GFP_KERNEL|__GFP_NOWARN);
397         if (!fp)
398                 return ERR_PTR(-ENOMEM);
399
400         /* Copy the instructions from fprog. */
401         ret = -EFAULT;
402         if (copy_from_user(filter->insns, fprog->filter, fp_size))
403                 goto fail;
404
405         /* Check and rewrite the fprog via the skb checker */
406         ret = sk_chk_filter(filter->insns, filter->len);
407         if (ret)
408                 goto fail;
409
410         /* Check and rewrite the fprog for seccomp use */
411         ret = seccomp_check_filter(filter->insns, filter->len);
412         if (ret)
413                 goto free_prog;
414
415         /* Allocate a new seccomp_filter */
416         ret = -ENOMEM;
417         filter = kzalloc(sizeof(struct seccomp_filter) +
418                          sizeof(struct sock_filter_int) * new_len,
419                          GFP_KERNEL|__GFP_NOWARN);
420         if (!filter)
421                 goto free_prog;
422
423         ret = sk_convert_filter(fp, fprog->len, filter->insnsi, &new_len);
424         if (ret)
425                 goto free_filter;
426         kfree(fp);
427
428         atomic_set(&filter->usage, 1);
429         filter->len = new_len;
430
431         return filter;
432
433 free_filter_prog:
434         kfree(filter->prog);
435 free_filter:
436         kfree(filter);
437 free_prog:
438         kfree(fp);
439         return ERR_PTR(ret);
440 }
441
442 /**
443  * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
444  * @user_filter: pointer to the user data containing a sock_fprog.
445  *
446  * Returns 0 on success and non-zero otherwise.
447  */
448 static struct seccomp_filter *
449 seccomp_prepare_user_filter(const char __user *user_filter)
450 {
451         struct sock_fprog fprog;
452         struct seccomp_filter *filter = ERR_PTR(-EFAULT);
453
454 #ifdef CONFIG_COMPAT
455         if (is_compat_task()) {
456                 struct compat_sock_fprog fprog32;
457                 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
458                         goto out;
459                 fprog.len = fprog32.len;
460                 fprog.filter = compat_ptr(fprog32.filter);
461         } else /* falls through to the if below. */
462 #endif
463         if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
464                 goto out;
465         filter = seccomp_prepare_filter(&fprog);
466 out:
467         return filter;
468 }
469
470 /**
471  * seccomp_attach_filter: validate and attach filter
472  * @flags:  flags to change filter behavior
473  * @filter: seccomp filter to add to the current process
474  *
475  * Caller must be holding current->sighand->siglock lock.
476  *
477  * Returns 0 on success, -ve on error.
478  */
479 static long seccomp_attach_filter(unsigned int flags,
480                                   struct seccomp_filter *filter)
481 {
482         unsigned long total_insns;
483         struct seccomp_filter *walker;
484
485         BUG_ON(!spin_is_locked(&current->sighand->siglock));
486
487         /* Validate resulting filter length. */
488         total_insns = filter->prog->len;
489         for (walker = current->seccomp.filter; walker; walker = walker->prev)
490                 total_insns += walker->prog->len + 4;  /* 4 instr penalty */
491         if (total_insns > MAX_INSNS_PER_PATH)
492                 return -ENOMEM;
493
494         /* If thread sync has been requested, check that it is possible. */
495         if (flags & SECCOMP_FILTER_FLAG_TSYNC) {
496                 int ret;
497
498                 ret = seccomp_can_sync_threads();
499                 if (ret)
500                         return ret;
501         }
502
503         /*
504          * If there is an existing filter, make it the prev and don't drop its
505          * task reference.
506          */
507         filter->prev = current->seccomp.filter;
508         current->seccomp.filter = filter;
509
510         /* Now that the new filter is in place, synchronize to all threads. */
511         if (flags & SECCOMP_FILTER_FLAG_TSYNC)
512                 seccomp_sync_threads();
513
514         return 0;
515 }
516
517 /* get_seccomp_filter - increments the reference count of the filter on @tsk */
518 void get_seccomp_filter(struct task_struct *tsk)
519 {
520         struct seccomp_filter *orig = tsk->seccomp.filter;
521         if (!orig)
522                 return;
523         /* Reference count is bounded by the number of total processes. */
524         atomic_inc(&orig->usage);
525 }
526
527 static inline void seccomp_filter_free(struct seccomp_filter *filter)
528 {
529         if (filter) {
530                 sk_filter_free(filter->prog);
531                 kfree(filter);
532         }
533 }
534
535 /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
536 void put_seccomp_filter(struct task_struct *tsk)
537 {
538         struct seccomp_filter *orig = tsk->seccomp.filter;
539         /* Clean up single-reference branches iteratively. */
540         while (orig && atomic_dec_and_test(&orig->usage)) {
541                 struct seccomp_filter *freeme = orig;
542                 orig = orig->prev;
543                 seccomp_filter_free(freeme);
544         }
545 }
546
547 /**
548  * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
549  * @syscall: syscall number to send to userland
550  * @reason: filter-supplied reason code to send to userland (via si_errno)
551  *
552  * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
553  */
554 static void seccomp_send_sigsys(int syscall, int reason)
555 {
556         struct siginfo info;
557         memset(&info, 0, sizeof(info));
558         info.si_signo = SIGSYS;
559         info.si_code = SYS_SECCOMP;
560         info.si_call_addr = (void __user *)KSTK_EIP(current);
561         info.si_errno = reason;
562         info.si_arch = syscall_get_arch(current, task_pt_regs(current));
563         info.si_syscall = syscall;
564         force_sig_info(SIGSYS, &info, current);
565 }
566 #endif  /* CONFIG_SECCOMP_FILTER */
567
568 /*
569  * Secure computing mode 1 allows only read/write/exit/sigreturn.
570  * To be fully secure this must be combined with rlimit
571  * to limit the stack allocations too.
572  */
573 static int mode1_syscalls[] = {
574         __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
575         0, /* null terminated */
576 };
577
578 #ifdef CONFIG_COMPAT
579 static int mode1_syscalls_32[] = {
580         __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
581         0, /* null terminated */
582 };
583 #endif
584
585 int __secure_computing(int this_syscall)
586 {
587         int exit_sig = 0;
588         int *syscall;
589         u32 ret;
590
591         /*
592          * Make sure that any changes to mode from another thread have
593          * been seen after TIF_SECCOMP was seen.
594          */
595         rmb();
596
597         switch (current->seccomp.mode) {
598         case SECCOMP_MODE_STRICT:
599                 syscall = mode1_syscalls;
600 #ifdef CONFIG_COMPAT
601                 if (is_compat_task())
602                         syscall = mode1_syscalls_32;
603 #endif
604                 do {
605                         if (*syscall == this_syscall)
606                                 return 0;
607                 } while (*++syscall);
608                 exit_sig = SIGKILL;
609                 ret = SECCOMP_RET_KILL;
610                 break;
611 #ifdef CONFIG_SECCOMP_FILTER
612         case SECCOMP_MODE_FILTER: {
613                 int data;
614                 struct pt_regs *regs = task_pt_regs(current);
615                 ret = seccomp_run_filters(this_syscall);
616                 data = ret & SECCOMP_RET_DATA;
617                 ret &= SECCOMP_RET_ACTION;
618                 switch (ret) {
619                 case SECCOMP_RET_ERRNO:
620                         /* Set the low-order 16-bits as a errno. */
621                         syscall_set_return_value(current, regs,
622                                                  -data, 0);
623                         goto skip;
624                 case SECCOMP_RET_TRAP:
625                         /* Show the handler the original registers. */
626                         syscall_rollback(current, regs);
627                         /* Let the filter pass back 16 bits of data. */
628                         seccomp_send_sigsys(this_syscall, data);
629                         goto skip;
630                 case SECCOMP_RET_TRACE:
631                         /* Skip these calls if there is no tracer. */
632                         if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
633                                 syscall_set_return_value(current, regs,
634                                                          -ENOSYS, 0);
635                                 goto skip;
636                         }
637                         /* Allow the BPF to provide the event message */
638                         ptrace_event(PTRACE_EVENT_SECCOMP, data);
639                         /*
640                          * The delivery of a fatal signal during event
641                          * notification may silently skip tracer notification.
642                          * Terminating the task now avoids executing a system
643                          * call that may not be intended.
644                          */
645                         if (fatal_signal_pending(current))
646                                 break;
647                         if (syscall_get_nr(current, regs) < 0)
648                                 goto skip;  /* Explicit request to skip. */
649
650                         return 0;
651                 case SECCOMP_RET_ALLOW:
652                         return 0;
653                 case SECCOMP_RET_KILL:
654                 default:
655                         break;
656                 }
657                 exit_sig = SIGSYS;
658                 break;
659         }
660 #endif
661         default:
662                 BUG();
663         }
664
665 #ifdef SECCOMP_DEBUG
666         dump_stack();
667 #endif
668         audit_seccomp(this_syscall, exit_sig, ret);
669         do_exit(exit_sig);
670 #ifdef CONFIG_SECCOMP_FILTER
671 skip:
672         audit_seccomp(this_syscall, exit_sig, ret);
673 #endif
674         return -1;
675 }
676
677 long prctl_get_seccomp(void)
678 {
679         return current->seccomp.mode;
680 }
681
682 /**
683  * seccomp_set_mode_strict: internal function for setting strict seccomp
684  *
685  * Once current->seccomp.mode is non-zero, it may not be changed.
686  *
687  * Returns 0 on success or -EINVAL on failure.
688  */
689 static long seccomp_set_mode_strict(void)
690 {
691         const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
692         long ret = -EINVAL;
693
694         spin_lock_irq(&current->sighand->siglock);
695
696         if (!seccomp_may_assign_mode(seccomp_mode))
697                 goto out;
698
699 #ifdef TIF_NOTSC
700         disable_TSC();
701 #endif
702         seccomp_assign_mode(current, seccomp_mode);
703         ret = 0;
704
705 out:
706         spin_unlock_irq(&current->sighand->siglock);
707
708         return ret;
709 }
710
711 #ifdef CONFIG_SECCOMP_FILTER
712 /**
713  * seccomp_set_mode_filter: internal function for setting seccomp filter
714  * @flags:  flags to change filter behavior
715  * @filter: struct sock_fprog containing filter
716  *
717  * This function may be called repeatedly to install additional filters.
718  * Every filter successfully installed will be evaluated (in reverse order)
719  * for each system call the task makes.
720  *
721  * Once current->seccomp.mode is non-zero, it may not be changed.
722  *
723  * Returns 0 on success or -EINVAL on failure.
724  */
725 static long seccomp_set_mode_filter(unsigned int flags,
726                                     const char __user *filter)
727 {
728         const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
729         struct seccomp_filter *prepared = NULL;
730         long ret = -EINVAL;
731
732         /* Validate flags. */
733         if (flags & ~SECCOMP_FILTER_FLAG_MASK)
734                 return -EINVAL;
735
736         /* Prepare the new filter before holding any locks. */
737         prepared = seccomp_prepare_user_filter(filter);
738         if (IS_ERR(prepared))
739                 return PTR_ERR(prepared);
740
741         /*
742          * Make sure we cannot change seccomp or nnp state via TSYNC
743          * while another thread is in the middle of calling exec.
744          */
745         if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
746             mutex_lock_killable(&current->signal->cred_guard_mutex))
747                 goto out_free;
748
749         spin_lock_irq(&current->sighand->siglock);
750
751         if (!seccomp_may_assign_mode(seccomp_mode))
752                 goto out;
753
754         ret = seccomp_attach_filter(flags, prepared);
755         if (ret)
756                 goto out;
757         /* Do not free the successfully attached filter. */
758         prepared = NULL;
759
760         seccomp_assign_mode(current, seccomp_mode);
761 out:
762         spin_unlock_irq(&current->sighand->siglock);
763         if (flags & SECCOMP_FILTER_FLAG_TSYNC)
764                 mutex_unlock(&current->signal->cred_guard_mutex);
765 out_free:
766         seccomp_filter_free(prepared);
767         return ret;
768 }
769 #else
770 static inline long seccomp_set_mode_filter(unsigned int flags,
771                                            const char __user *filter)
772 {
773         return -EINVAL;
774 }
775 #endif
776
777 /* Common entry point for both prctl and syscall. */
778 static long do_seccomp(unsigned int op, unsigned int flags,
779                        const char __user *uargs)
780 {
781         switch (op) {
782         case SECCOMP_SET_MODE_STRICT:
783                 if (flags != 0 || uargs != NULL)
784                         return -EINVAL;
785                 return seccomp_set_mode_strict();
786         case SECCOMP_SET_MODE_FILTER:
787                 return seccomp_set_mode_filter(flags, uargs);
788         default:
789                 return -EINVAL;
790         }
791 }
792
793 SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
794                          const char __user *, uargs)
795 {
796         return do_seccomp(op, flags, uargs);
797 }
798
799 /**
800  * prctl_set_seccomp: configures current->seccomp.mode
801  * @seccomp_mode: requested mode to use
802  * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
803  *
804  * Returns 0 on success or -EINVAL on failure.
805  */
806 long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
807 {
808         unsigned int op;
809         char __user *uargs;
810
811         switch (seccomp_mode) {
812         case SECCOMP_MODE_STRICT:
813                 op = SECCOMP_SET_MODE_STRICT;
814                 /*
815                  * Setting strict mode through prctl always ignored filter,
816                  * so make sure it is always NULL here to pass the internal
817                  * check in do_seccomp().
818                  */
819                 uargs = NULL;
820                 break;
821         case SECCOMP_MODE_FILTER:
822                 op = SECCOMP_SET_MODE_FILTER;
823                 uargs = filter;
824                 break;
825         default:
826                 return -EINVAL;
827         }
828
829         /* prctl interface doesn't have flags, so they are always zero. */
830         return do_seccomp(op, 0, uargs);
831 }