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/sched.h>
20 #include <linux/seccomp.h>
21 #include <linux/slab.h>
22 #include <linux/syscalls.h>
24 /* #define SECCOMP_DEBUG 1 */
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>
36 * struct seccomp_filter - container for seccomp BPF programs
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
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.
53 * seccomp_filter objects should never be modified after being attached
54 * to a task_struct (other than @usage).
56 struct seccomp_filter {
58 struct seccomp_filter *prev;
59 unsigned short len; /* Instruction count */
60 struct sock_filter insns[];
63 /* Limit any path through the tree to 256KB worth of instructions. */
64 #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
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
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
76 * Endianness is explicitly ignored and left for BPF program authors to manage
77 * as per the specific architecture.
79 static inline u32 get_u32(u64 data, int index)
81 return ((u32 *)&data)[index];
84 /* Helper for bpf_load below. */
85 #define BPF_DATA(_name) offsetof(struct seccomp_data, _name)
87 * bpf_load: checks and returns a pointer to the requested offset
88 * @off: offset into struct seccomp_data to load from
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.
94 u32 seccomp_bpf_load(int off)
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])) {
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);
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. */
117 * seccomp_check_filter - verify seccomp filter code
118 * @filter: filter to verify
119 * @flen: length of filter
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.
126 * Returns 0 if the rule set is legal or -EINVAL if not.
128 static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
131 for (pc = 0; pc < flen; pc++) {
132 struct sock_filter *ftest = &filter[pc];
133 u16 code = ftest->code;
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)
144 ftest->code = BPF_S_LD_IMM;
145 ftest->k = sizeof(struct seccomp_data);
147 case BPF_S_LDX_W_LEN:
148 ftest->code = BPF_S_LDX_IMM;
149 ftest->k = sizeof(struct seccomp_data);
151 /* Explicitly include allowed calls. */
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:
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:
176 case BPF_S_ALU_DIV_K:
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:
199 * seccomp_run_filters - evaluates all seccomp filters against @syscall
200 * @syscall: number of the current system call
202 * Returns valid seccomp BPF response codes.
204 static u32 seccomp_run_filters(int syscall)
206 struct seccomp_filter *f = ACCESS_ONCE(current->seccomp.filter);
207 struct seccomp_data sd;
208 u32 ret = SECCOMP_RET_ALLOW;
210 /* Ensure unexpected behavior doesn't result in failing open. */
211 if (unlikely(WARN_ON(f == NULL)))
212 return SECCOMP_RET_KILL;
214 /* Make sure cross-thread synced filter points somewhere sane. */
215 smp_read_barrier_depends();
217 populate_seccomp_data(&sd);
220 * All filters in the list are evaluated and the lowest BPF return
221 * value always takes priority (ignoring the DATA).
223 for (; f; f = f->prev) {
224 u32 cur_ret = SK_RUN_FILTER(f->prog, (void *)&sd);
226 if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
231 #endif /* CONFIG_SECCOMP_FILTER */
233 static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
235 BUG_ON(!spin_is_locked(¤t->sighand->siglock));
237 if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
243 static inline void seccomp_assign_mode(struct task_struct *task,
244 unsigned long seccomp_mode)
246 BUG_ON(!spin_is_locked(&task->sighand->siglock));
248 task->seccomp.mode = seccomp_mode;
250 * Make sure TIF_SECCOMP cannot be set before the mode (and
253 smp_mb__before_atomic();
254 set_tsk_thread_flag(task, TIF_SECCOMP);
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)
262 /* NULL is the root ancestor. */
265 for (; child; child = child->prev)
272 * seccomp_can_sync_threads: checks if all threads can be synchronized
274 * Expects sighand and cred_guard_mutex locks to be held.
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
280 static inline pid_t seccomp_can_sync_threads(void)
282 struct task_struct *thread, *caller;
284 BUG_ON(!mutex_is_locked(¤t->signal->cred_guard_mutex));
285 BUG_ON(!spin_is_locked(¤t->sighand->siglock));
287 /* Validate all threads being eligible for synchronization. */
289 for_each_thread(caller, thread) {
292 /* Skip current, since it is initiating the sync. */
293 if (thread == caller)
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)))
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)))
314 * seccomp_sync_threads: sets all threads to use current's filter
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.
321 static inline void seccomp_sync_threads(void)
323 struct task_struct *thread, *caller;
325 BUG_ON(!mutex_is_locked(¤t->signal->cred_guard_mutex));
326 BUG_ON(!spin_is_locked(¤t->sighand->siglock));
328 /* Synchronize all threads. */
330 for_each_thread(caller, thread) {
331 /* Skip current, since it needs no changes. */
332 if (thread == caller)
335 /* Get a task reference for the new leaf node. */
336 get_seccomp_filter(caller);
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.)
342 put_seccomp_filter(thread);
343 smp_store_release(&thread->seccomp.filter,
344 caller->seccomp.filter);
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.
351 if (thread->seccomp.mode == SECCOMP_MODE_DISABLED) {
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,
358 if (task_no_new_privs(caller))
359 task_set_no_new_privs(thread);
361 seccomp_assign_mode(thread, SECCOMP_MODE_FILTER);
367 * seccomp_prepare_filter: Prepares a seccomp filter for use.
368 * @fprog: BPF program to install
370 * Returns filter on success or an ERR_PTR on failure.
372 static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
374 struct seccomp_filter *filter;
375 unsigned long fp_size;
376 struct sock_filter *fp;
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);
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.
391 if (!current->no_new_privs &&
392 security_capable_noaudit(current_cred(), current_user_ns(),
394 return ERR_PTR(-EACCES);
396 fp = kzalloc(fp_size, GFP_KERNEL|__GFP_NOWARN);
398 return ERR_PTR(-ENOMEM);
400 /* Copy the instructions from fprog. */
402 if (copy_from_user(filter->insns, fprog->filter, fp_size))
405 /* Check and rewrite the fprog via the skb checker */
406 ret = sk_chk_filter(filter->insns, filter->len);
410 /* Check and rewrite the fprog for seccomp use */
411 ret = seccomp_check_filter(filter->insns, filter->len);
415 /* Allocate a new seccomp_filter */
417 filter = kzalloc(sizeof(struct seccomp_filter) +
418 sizeof(struct sock_filter_int) * new_len,
419 GFP_KERNEL|__GFP_NOWARN);
423 ret = sk_convert_filter(fp, fprog->len, filter->insnsi, &new_len);
428 atomic_set(&filter->usage, 1);
429 filter->len = new_len;
443 * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
444 * @user_filter: pointer to the user data containing a sock_fprog.
446 * Returns 0 on success and non-zero otherwise.
448 static struct seccomp_filter *
449 seccomp_prepare_user_filter(const char __user *user_filter)
451 struct sock_fprog fprog;
452 struct seccomp_filter *filter = ERR_PTR(-EFAULT);
455 if (is_compat_task()) {
456 struct compat_sock_fprog fprog32;
457 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
459 fprog.len = fprog32.len;
460 fprog.filter = compat_ptr(fprog32.filter);
461 } else /* falls through to the if below. */
463 if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
465 filter = seccomp_prepare_filter(&fprog);
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
475 * Caller must be holding current->sighand->siglock lock.
477 * Returns 0 on success, -ve on error.
479 static long seccomp_attach_filter(unsigned int flags,
480 struct seccomp_filter *filter)
482 unsigned long total_insns;
483 struct seccomp_filter *walker;
485 BUG_ON(!spin_is_locked(¤t->sighand->siglock));
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)
494 /* If thread sync has been requested, check that it is possible. */
495 if (flags & SECCOMP_FILTER_FLAG_TSYNC) {
498 ret = seccomp_can_sync_threads();
504 * If there is an existing filter, make it the prev and don't drop its
507 filter->prev = current->seccomp.filter;
508 current->seccomp.filter = filter;
510 /* Now that the new filter is in place, synchronize to all threads. */
511 if (flags & SECCOMP_FILTER_FLAG_TSYNC)
512 seccomp_sync_threads();
517 /* get_seccomp_filter - increments the reference count of the filter on @tsk */
518 void get_seccomp_filter(struct task_struct *tsk)
520 struct seccomp_filter *orig = tsk->seccomp.filter;
523 /* Reference count is bounded by the number of total processes. */
524 atomic_inc(&orig->usage);
527 static inline void seccomp_filter_free(struct seccomp_filter *filter)
530 sk_filter_free(filter->prog);
535 /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
536 void put_seccomp_filter(struct task_struct *tsk)
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;
543 seccomp_filter_free(freeme);
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)
552 * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
554 static void seccomp_send_sigsys(int syscall, int reason)
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);
566 #endif /* CONFIG_SECCOMP_FILTER */
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.
573 static int mode1_syscalls[] = {
574 __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
575 0, /* null terminated */
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 */
585 int __secure_computing(int this_syscall)
592 * Make sure that any changes to mode from another thread have
593 * been seen after TIF_SECCOMP was seen.
597 switch (current->seccomp.mode) {
598 case SECCOMP_MODE_STRICT:
599 syscall = mode1_syscalls;
601 if (is_compat_task())
602 syscall = mode1_syscalls_32;
605 if (*syscall == this_syscall)
607 } while (*++syscall);
609 ret = SECCOMP_RET_KILL;
611 #ifdef CONFIG_SECCOMP_FILTER
612 case SECCOMP_MODE_FILTER: {
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;
619 case SECCOMP_RET_ERRNO:
620 /* Set the low-order 16-bits as a errno. */
621 syscall_set_return_value(current, regs,
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);
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,
637 /* Allow the BPF to provide the event message */
638 ptrace_event(PTRACE_EVENT_SECCOMP, data);
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.
645 if (fatal_signal_pending(current))
647 if (syscall_get_nr(current, regs) < 0)
648 goto skip; /* Explicit request to skip. */
651 case SECCOMP_RET_ALLOW:
653 case SECCOMP_RET_KILL:
668 audit_seccomp(this_syscall, exit_sig, ret);
670 #ifdef CONFIG_SECCOMP_FILTER
672 audit_seccomp(this_syscall, exit_sig, ret);
677 long prctl_get_seccomp(void)
679 return current->seccomp.mode;
683 * seccomp_set_mode_strict: internal function for setting strict seccomp
685 * Once current->seccomp.mode is non-zero, it may not be changed.
687 * Returns 0 on success or -EINVAL on failure.
689 static long seccomp_set_mode_strict(void)
691 const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
694 spin_lock_irq(¤t->sighand->siglock);
696 if (!seccomp_may_assign_mode(seccomp_mode))
702 seccomp_assign_mode(current, seccomp_mode);
706 spin_unlock_irq(¤t->sighand->siglock);
711 #ifdef CONFIG_SECCOMP_FILTER
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
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.
721 * Once current->seccomp.mode is non-zero, it may not be changed.
723 * Returns 0 on success or -EINVAL on failure.
725 static long seccomp_set_mode_filter(unsigned int flags,
726 const char __user *filter)
728 const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
729 struct seccomp_filter *prepared = NULL;
732 /* Validate flags. */
733 if (flags & ~SECCOMP_FILTER_FLAG_MASK)
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);
742 * Make sure we cannot change seccomp or nnp state via TSYNC
743 * while another thread is in the middle of calling exec.
745 if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
746 mutex_lock_killable(¤t->signal->cred_guard_mutex))
749 spin_lock_irq(¤t->sighand->siglock);
751 if (!seccomp_may_assign_mode(seccomp_mode))
754 ret = seccomp_attach_filter(flags, prepared);
757 /* Do not free the successfully attached filter. */
760 seccomp_assign_mode(current, seccomp_mode);
762 spin_unlock_irq(¤t->sighand->siglock);
763 if (flags & SECCOMP_FILTER_FLAG_TSYNC)
764 mutex_unlock(¤t->signal->cred_guard_mutex);
766 seccomp_filter_free(prepared);
770 static inline long seccomp_set_mode_filter(unsigned int flags,
771 const char __user *filter)
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)
782 case SECCOMP_SET_MODE_STRICT:
783 if (flags != 0 || uargs != NULL)
785 return seccomp_set_mode_strict();
786 case SECCOMP_SET_MODE_FILTER:
787 return seccomp_set_mode_filter(flags, uargs);
793 SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
794 const char __user *, uargs)
796 return do_seccomp(op, flags, uargs);
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
804 * Returns 0 on success or -EINVAL on failure.
806 long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
811 switch (seccomp_mode) {
812 case SECCOMP_MODE_STRICT:
813 op = SECCOMP_SET_MODE_STRICT;
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().
821 case SECCOMP_MODE_FILTER:
822 op = SECCOMP_SET_MODE_FILTER;
829 /* prctl interface doesn't have flags, so they are always zero. */
830 return do_seccomp(op, 0, uargs);