Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

seccomp: add system call filtering using BPF

[This patch depends on luto@mit.edu's no_new_privs patch:
https://lkml.org/lkml/2012/1/30/264
The whole series including Andrew's patches can be found here:
https://github.com/redpig/linux/tree/seccomp
Complete diff here:
https://github.com/redpig/linux/compare/1dc65fed...seccomp
]

This patch adds support for seccomp mode 2. Mode 2 introduces the
ability for unprivileged processes to install system call filtering
policy expressed in terms of a Berkeley Packet Filter (BPF) program.
This program will be evaluated in the kernel for each system call
the task makes and computes a result based on data in the format
of struct seccomp_data.

A filter program may be installed by calling:
struct sock_fprog fprog = { ... };
...
prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &fprog);

The return value of the filter program determines if the system call is
allowed to proceed or denied. If the first filter program installed
allows prctl(2) calls, then the above call may be made repeatedly
by a task to further reduce its access to the kernel. All attached
programs must be evaluated before a system call will be allowed to
proceed.

Filter programs will be inherited across fork/clone and execve.
However, if the task attaching the filter is unprivileged
(!CAP_SYS_ADMIN) the no_new_privs bit will be set on the task. This
ensures that unprivileged tasks cannot attach filters that affect
privileged tasks (e.g., setuid binary).

There are a number of benefits to this approach. A few of which are
as follows:
- BPF has been exposed to userland for a long time
- BPF optimization (and JIT'ing) are well understood
- Userland already knows its ABI: system call numbers and desired
arguments
- No time-of-check-time-of-use vulnerable data accesses are possible.
- system call arguments are loaded on access only to minimize copying
required for system call policy decisions.

Mode 2 support is restricted to architectures that enable
HAVE_ARCH_SECCOMP_FILTER. In this patch, the primary dependency is on
syscall_get_arguments(). The full desired scope of this feature will
add a few minor additional requirements expressed later in this series.
Based on discussion, SECCOMP_RET_ERRNO and SECCOMP_RET_TRACE seem to be
the desired additional functionality.

No architectures are enabled in this patch.

Signed-off-by: Will Drewry <wad@chromium.org>
Acked-by: Serge Hallyn <serge.hallyn@canonical.com>
Reviewed-by: Indan Zupancic <indan@nul.nu>
Acked-by: Eric Paris <eparis@redhat.com>
Reviewed-by: Kees Cook <keescook@chromium.org>

v18: - rebase to v3.4-rc2
- s/chk/check/ (akpm@linux-foundation.org,jmorris@namei.org)
- allocate with GFP_KERNEL|__GFP_NOWARN (indan@nul.nu)
- add a comment for get_u32 regarding endianness (akpm@)
- fix other typos, style mistakes (akpm@)
- added acked-by
v17: - properly guard seccomp filter needed headers (leann@ubuntu.com)
- tighten return mask to 0x7fff0000
v16: - no change
v15: - add a 4 instr penalty when counting a path to account for seccomp_filter
size (indan@nul.nu)
- drop the max insns to 256KB (indan@nul.nu)
- return ENOMEM if the max insns limit has been hit (indan@nul.nu)
- move IP checks after args (indan@nul.nu)
- drop !user_filter check (indan@nul.nu)
- only allow explicit bpf codes (indan@nul.nu)
- exit_code -> exit_sig
v14: - put/get_seccomp_filter takes struct task_struct
(indan@nul.nu,keescook@chromium.org)
- adds seccomp_chk_filter and drops general bpf_run/chk_filter user
- add seccomp_bpf_load for use by net/core/filter.c
- lower max per-process/per-hierarchy: 1MB
- moved nnp/capability check prior to allocation
(all of the above: indan@nul.nu)
v13: - rebase on to 88ebdda6159ffc15699f204c33feb3e431bf9bdc
v12: - added a maximum instruction count per path (indan@nul.nu,oleg@redhat.com)
- removed copy_seccomp (keescook@chromium.org,indan@nul.nu)
- reworded the prctl_set_seccomp comment (indan@nul.nu)
v11: - reorder struct seccomp_data to allow future args expansion (hpa@zytor.com)
- style clean up, @compat dropped, compat_sock_fprog32 (indan@nul.nu)
- do_exit(SIGSYS) (keescook@chromium.org, luto@mit.edu)
- pare down Kconfig doc reference.
- extra comment clean up
v10: - seccomp_data has changed again to be more aesthetically pleasing
(hpa@zytor.com)
- calling convention is noted in a new u32 field using syscall_get_arch.
This allows for cross-calling convention tasks to use seccomp filters.
(hpa@zytor.com)
- lots of clean up (thanks, Indan!)
v9: - n/a
v8: - use bpf_chk_filter, bpf_run_filter. update load_fns
- Lots of fixes courtesy of indan@nul.nu:
-- fix up load behavior, compat fixups, and merge alloc code,
-- renamed pc and dropped __packed, use bool compat.
-- Added a hidden CONFIG_SECCOMP_FILTER to synthesize non-arch
dependencies
v7: (massive overhaul thanks to Indan, others)
- added CONFIG_HAVE_ARCH_SECCOMP_FILTER
- merged into seccomp.c
- minimal seccomp_filter.h
- no config option (part of seccomp)
- no new prctl
- doesn't break seccomp on systems without asm/syscall.h
(works but arg access always fails)
- dropped seccomp_init_task, extra free functions, ...
- dropped the no-asm/syscall.h code paths
- merges with network sk_run_filter and sk_chk_filter
v6: - fix memory leak on attach compat check failure
- require no_new_privs || CAP_SYS_ADMIN prior to filter
installation. (luto@mit.edu)
- s/seccomp_struct_/seccomp_/ for macros/functions (amwang@redhat.com)
- cleaned up Kconfig (amwang@redhat.com)
- on block, note if the call was compat (so the # means something)
v5: - uses syscall_get_arguments
(indan@nul.nu,oleg@redhat.com, mcgrathr@chromium.org)
- uses union-based arg storage with hi/lo struct to
handle endianness. Compromises between the two alternate
proposals to minimize extra arg shuffling and account for
endianness assuming userspace uses offsetof().
(mcgrathr@chromium.org, indan@nul.nu)
- update Kconfig description
- add include/seccomp_filter.h and add its installation
- (naive) on-demand syscall argument loading
- drop seccomp_t (eparis@redhat.com)
v4: - adjusted prctl to make room for PR_[SG]ET_NO_NEW_PRIVS
- now uses current->no_new_privs
(luto@mit.edu,torvalds@linux-foundation.com)
- assign names to seccomp modes (rdunlap@xenotime.net)
- fix style issues (rdunlap@xenotime.net)
- reworded Kconfig entry (rdunlap@xenotime.net)
v3: - macros to inline (oleg@redhat.com)
- init_task behavior fixed (oleg@redhat.com)
- drop creator entry and extra NULL check (oleg@redhat.com)
- alloc returns -EINVAL on bad sizing (serge.hallyn@canonical.com)
- adds tentative use of "always_unprivileged" as per
torvalds@linux-foundation.org and luto@mit.edu
v2: - (patch 2 only)
Signed-off-by: James Morris <james.l.morris@oracle.com>

authored by

Will Drewry and committed by
James Morris
e2cfabdf b7456536

+472 -23
+17
arch/Kconfig
··· 216 216 config ARCH_WANT_OLD_COMPAT_IPC 217 217 bool 218 218 219 + config HAVE_ARCH_SECCOMP_FILTER 220 + bool 221 + help 222 + This symbol should be selected by an architecure if it provides 223 + asm/syscall.h, specifically syscall_get_arguments() and 224 + syscall_get_arch(). 225 + 226 + config SECCOMP_FILTER 227 + def_bool y 228 + depends on HAVE_ARCH_SECCOMP_FILTER && SECCOMP && NET 229 + help 230 + Enable tasks to build secure computing environments defined 231 + in terms of Berkeley Packet Filter programs which implement 232 + task-defined system call filtering polices. 233 + 234 + See Documentation/prctl/seccomp_filter.txt for details. 235 + 219 236 source "kernel/gcov/Kconfig"
+1
include/linux/Kbuild
··· 332 332 header-y += sched.h 333 333 header-y += screen_info.h 334 334 header-y += sdla.h 335 + header-y += seccomp.h 335 336 header-y += securebits.h 336 337 header-y += selinux_netlink.h 337 338 header-y += sem.h
+72 -4
include/linux/seccomp.h
··· 1 1 #ifndef _LINUX_SECCOMP_H 2 2 #define _LINUX_SECCOMP_H 3 3 4 + #include <linux/compiler.h> 5 + #include <linux/types.h> 4 6 7 + 8 + /* Valid values for seccomp.mode and prctl(PR_SET_SECCOMP, <mode>) */ 9 + #define SECCOMP_MODE_DISABLED 0 /* seccomp is not in use. */ 10 + #define SECCOMP_MODE_STRICT 1 /* uses hard-coded filter. */ 11 + #define SECCOMP_MODE_FILTER 2 /* uses user-supplied filter. */ 12 + 13 + /* 14 + * All BPF programs must return a 32-bit value. 15 + * The bottom 16-bits are reserved for future use. 16 + * The upper 16-bits are ordered from least permissive values to most. 17 + * 18 + * The ordering ensures that a min_t() over composed return values always 19 + * selects the least permissive choice. 20 + */ 21 + #define SECCOMP_RET_KILL 0x00000000U /* kill the task immediately */ 22 + #define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */ 23 + 24 + /* Masks for the return value sections. */ 25 + #define SECCOMP_RET_ACTION 0x7fff0000U 26 + #define SECCOMP_RET_DATA 0x0000ffffU 27 + 28 + /** 29 + * struct seccomp_data - the format the BPF program executes over. 30 + * @nr: the system call number 31 + * @arch: indicates system call convention as an AUDIT_ARCH_* value 32 + * as defined in <linux/audit.h>. 33 + * @instruction_pointer: at the time of the system call. 34 + * @args: up to 6 system call arguments always stored as 64-bit values 35 + * regardless of the architecture. 36 + */ 37 + struct seccomp_data { 38 + int nr; 39 + __u32 arch; 40 + __u64 instruction_pointer; 41 + __u64 args[6]; 42 + }; 43 + 44 + #ifdef __KERNEL__ 5 45 #ifdef CONFIG_SECCOMP 6 46 7 47 #include <linux/thread_info.h> 8 48 #include <asm/seccomp.h> 9 49 50 + struct seccomp_filter; 51 + /** 52 + * struct seccomp - the state of a seccomp'ed process 53 + * 54 + * @mode: indicates one of the valid values above for controlled 55 + * system calls available to a process. 56 + * @filter: The metadata and ruleset for determining what system calls 57 + * are allowed for a task. 58 + * 59 + * @filter must only be accessed from the context of current as there 60 + * is no locking. 61 + */ 10 62 struct seccomp { 11 63 int mode; 64 + struct seccomp_filter *filter; 12 65 }; 13 66 14 67 extern void __secure_computing(int); ··· 72 19 } 73 20 74 21 extern long prctl_get_seccomp(void); 75 - extern long prctl_set_seccomp(unsigned long); 22 + extern long prctl_set_seccomp(unsigned long, char __user *); 76 23 77 24 static inline int seccomp_mode(struct seccomp *s) 78 25 { ··· 84 31 #include <linux/errno.h> 85 32 86 33 struct seccomp { }; 34 + struct seccomp_filter { }; 87 35 88 - #define secure_computing(x) do { } while (0) 36 + #define secure_computing(x) 0 89 37 90 38 static inline long prctl_get_seccomp(void) 91 39 { 92 40 return -EINVAL; 93 41 } 94 42 95 - static inline long prctl_set_seccomp(unsigned long arg2) 43 + static inline long prctl_set_seccomp(unsigned long arg2, char __user *arg3) 96 44 { 97 45 return -EINVAL; 98 46 } ··· 102 48 { 103 49 return 0; 104 50 } 105 - 106 51 #endif /* CONFIG_SECCOMP */ 107 52 53 + #ifdef CONFIG_SECCOMP_FILTER 54 + extern void put_seccomp_filter(struct task_struct *tsk); 55 + extern void get_seccomp_filter(struct task_struct *tsk); 56 + extern u32 seccomp_bpf_load(int off); 57 + #else /* CONFIG_SECCOMP_FILTER */ 58 + static inline void put_seccomp_filter(struct task_struct *tsk) 59 + { 60 + return; 61 + } 62 + static inline void get_seccomp_filter(struct task_struct *tsk) 63 + { 64 + return; 65 + } 66 + #endif /* CONFIG_SECCOMP_FILTER */ 67 + #endif /* __KERNEL__ */ 108 68 #endif /* _LINUX_SECCOMP_H */
+3
kernel/fork.c
··· 34 34 #include <linux/cgroup.h> 35 35 #include <linux/security.h> 36 36 #include <linux/hugetlb.h> 37 + #include <linux/seccomp.h> 37 38 #include <linux/swap.h> 38 39 #include <linux/syscalls.h> 39 40 #include <linux/jiffies.h> ··· 171 170 free_thread_info(tsk->stack); 172 171 rt_mutex_debug_task_free(tsk); 173 172 ftrace_graph_exit_task(tsk); 173 + put_seccomp_filter(tsk); 174 174 free_task_struct(tsk); 175 175 } 176 176 EXPORT_SYMBOL(free_task); ··· 1164 1162 goto fork_out; 1165 1163 1166 1164 ftrace_graph_init_task(p); 1165 + get_seccomp_filter(p); 1167 1166 1168 1167 rt_mutex_init_task(p); 1169 1168
+378 -18
kernel/seccomp.c
··· 3 3 * 4 4 * Copyright 2004-2005 Andrea Arcangeli <andrea@cpushare.com> 5 5 * 6 - * This defines a simple but solid secure-computing mode. 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. 7 14 */ 8 15 16 + #include <linux/atomic.h> 9 17 #include <linux/audit.h> 10 - #include <linux/seccomp.h> 11 - #include <linux/sched.h> 12 18 #include <linux/compat.h> 19 + #include <linux/sched.h> 20 + #include <linux/seccomp.h> 13 21 14 22 /* #define SECCOMP_DEBUG 1 */ 15 - #define NR_SECCOMP_MODES 1 23 + 24 + #ifdef CONFIG_SECCOMP_FILTER 25 + #include <asm/syscall.h> 26 + #include <linux/filter.h> 27 + #include <linux/security.h> 28 + #include <linux/slab.h> 29 + #include <linux/tracehook.h> 30 + #include <linux/uaccess.h> 31 + 32 + /** 33 + * struct seccomp_filter - container for seccomp BPF programs 34 + * 35 + * @usage: reference count to manage the object lifetime. 36 + * get/put helpers should be used when accessing an instance 37 + * outside of a lifetime-guarded section. In general, this 38 + * is only needed for handling filters shared across tasks. 39 + * @prev: points to a previously installed, or inherited, filter 40 + * @len: the number of instructions in the program 41 + * @insns: the BPF program instructions to evaluate 42 + * 43 + * seccomp_filter objects are organized in a tree linked via the @prev 44 + * pointer. For any task, it appears to be a singly-linked list starting 45 + * with current->seccomp.filter, the most recently attached or inherited filter. 46 + * However, multiple filters may share a @prev node, by way of fork(), which 47 + * results in a unidirectional tree existing in memory. This is similar to 48 + * how namespaces work. 49 + * 50 + * seccomp_filter objects should never be modified after being attached 51 + * to a task_struct (other than @usage). 52 + */ 53 + struct seccomp_filter { 54 + atomic_t usage; 55 + struct seccomp_filter *prev; 56 + unsigned short len; /* Instruction count */ 57 + struct sock_filter insns[]; 58 + }; 59 + 60 + /* Limit any path through the tree to 256KB worth of instructions. */ 61 + #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter)) 62 + 63 + static void seccomp_filter_log_failure(int syscall) 64 + { 65 + int compat = 0; 66 + #ifdef CONFIG_COMPAT 67 + compat = is_compat_task(); 68 + #endif 69 + pr_info("%s[%d]: %ssystem call %d blocked at 0x%lx\n", 70 + current->comm, task_pid_nr(current), 71 + (compat ? "compat " : ""), 72 + syscall, KSTK_EIP(current)); 73 + } 74 + 75 + /** 76 + * get_u32 - returns a u32 offset into data 77 + * @data: a unsigned 64 bit value 78 + * @index: 0 or 1 to return the first or second 32-bits 79 + * 80 + * This inline exists to hide the length of unsigned long. If a 32-bit 81 + * unsigned long is passed in, it will be extended and the top 32-bits will be 82 + * 0. If it is a 64-bit unsigned long, then whatever data is resident will be 83 + * properly returned. 84 + * 85 + * Endianness is explicitly ignored and left for BPF program authors to manage 86 + * as per the specific architecture. 87 + */ 88 + static inline u32 get_u32(u64 data, int index) 89 + { 90 + return ((u32 *)&data)[index]; 91 + } 92 + 93 + /* Helper for bpf_load below. */ 94 + #define BPF_DATA(_name) offsetof(struct seccomp_data, _name) 95 + /** 96 + * bpf_load: checks and returns a pointer to the requested offset 97 + * @off: offset into struct seccomp_data to load from 98 + * 99 + * Returns the requested 32-bits of data. 100 + * seccomp_check_filter() should assure that @off is 32-bit aligned 101 + * and not out of bounds. Failure to do so is a BUG. 102 + */ 103 + u32 seccomp_bpf_load(int off) 104 + { 105 + struct pt_regs *regs = task_pt_regs(current); 106 + if (off == BPF_DATA(nr)) 107 + return syscall_get_nr(current, regs); 108 + if (off == BPF_DATA(arch)) 109 + return syscall_get_arch(current, regs); 110 + if (off >= BPF_DATA(args[0]) && off < BPF_DATA(args[6])) { 111 + unsigned long value; 112 + int arg = (off - BPF_DATA(args[0])) / sizeof(u64); 113 + int index = !!(off % sizeof(u64)); 114 + syscall_get_arguments(current, regs, arg, 1, &value); 115 + return get_u32(value, index); 116 + } 117 + if (off == BPF_DATA(instruction_pointer)) 118 + return get_u32(KSTK_EIP(current), 0); 119 + if (off == BPF_DATA(instruction_pointer) + sizeof(u32)) 120 + return get_u32(KSTK_EIP(current), 1); 121 + /* seccomp_check_filter should make this impossible. */ 122 + BUG(); 123 + } 124 + 125 + /** 126 + * seccomp_check_filter - verify seccomp filter code 127 + * @filter: filter to verify 128 + * @flen: length of filter 129 + * 130 + * Takes a previously checked filter (by sk_chk_filter) and 131 + * redirects all filter code that loads struct sk_buff data 132 + * and related data through seccomp_bpf_load. It also 133 + * enforces length and alignment checking of those loads. 134 + * 135 + * Returns 0 if the rule set is legal or -EINVAL if not. 136 + */ 137 + static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen) 138 + { 139 + int pc; 140 + for (pc = 0; pc < flen; pc++) { 141 + struct sock_filter *ftest = &filter[pc]; 142 + u16 code = ftest->code; 143 + u32 k = ftest->k; 144 + 145 + switch (code) { 146 + case BPF_S_LD_W_ABS: 147 + ftest->code = BPF_S_ANC_SECCOMP_LD_W; 148 + /* 32-bit aligned and not out of bounds. */ 149 + if (k >= sizeof(struct seccomp_data) || k & 3) 150 + return -EINVAL; 151 + continue; 152 + case BPF_S_LD_W_LEN: 153 + ftest->code = BPF_S_LD_IMM; 154 + ftest->k = sizeof(struct seccomp_data); 155 + continue; 156 + case BPF_S_LDX_W_LEN: 157 + ftest->code = BPF_S_LDX_IMM; 158 + ftest->k = sizeof(struct seccomp_data); 159 + continue; 160 + /* Explicitly include allowed calls. */ 161 + case BPF_S_RET_K: 162 + case BPF_S_RET_A: 163 + case BPF_S_ALU_ADD_K: 164 + case BPF_S_ALU_ADD_X: 165 + case BPF_S_ALU_SUB_K: 166 + case BPF_S_ALU_SUB_X: 167 + case BPF_S_ALU_MUL_K: 168 + case BPF_S_ALU_MUL_X: 169 + case BPF_S_ALU_DIV_X: 170 + case BPF_S_ALU_AND_K: 171 + case BPF_S_ALU_AND_X: 172 + case BPF_S_ALU_OR_K: 173 + case BPF_S_ALU_OR_X: 174 + case BPF_S_ALU_LSH_K: 175 + case BPF_S_ALU_LSH_X: 176 + case BPF_S_ALU_RSH_K: 177 + case BPF_S_ALU_RSH_X: 178 + case BPF_S_ALU_NEG: 179 + case BPF_S_LD_IMM: 180 + case BPF_S_LDX_IMM: 181 + case BPF_S_MISC_TAX: 182 + case BPF_S_MISC_TXA: 183 + case BPF_S_ALU_DIV_K: 184 + case BPF_S_LD_MEM: 185 + case BPF_S_LDX_MEM: 186 + case BPF_S_ST: 187 + case BPF_S_STX: 188 + case BPF_S_JMP_JA: 189 + case BPF_S_JMP_JEQ_K: 190 + case BPF_S_JMP_JEQ_X: 191 + case BPF_S_JMP_JGE_K: 192 + case BPF_S_JMP_JGE_X: 193 + case BPF_S_JMP_JGT_K: 194 + case BPF_S_JMP_JGT_X: 195 + case BPF_S_JMP_JSET_K: 196 + case BPF_S_JMP_JSET_X: 197 + continue; 198 + default: 199 + return -EINVAL; 200 + } 201 + } 202 + return 0; 203 + } 204 + 205 + /** 206 + * seccomp_run_filters - evaluates all seccomp filters against @syscall 207 + * @syscall: number of the current system call 208 + * 209 + * Returns valid seccomp BPF response codes. 210 + */ 211 + static u32 seccomp_run_filters(int syscall) 212 + { 213 + struct seccomp_filter *f; 214 + u32 ret = SECCOMP_RET_KILL; 215 + /* 216 + * All filters in the list are evaluated and the lowest BPF return 217 + * value always takes priority. 218 + */ 219 + for (f = current->seccomp.filter; f; f = f->prev) { 220 + ret = sk_run_filter(NULL, f->insns); 221 + if (ret != SECCOMP_RET_ALLOW) 222 + break; 223 + } 224 + return ret; 225 + } 226 + 227 + /** 228 + * seccomp_attach_filter: Attaches a seccomp filter to current. 229 + * @fprog: BPF program to install 230 + * 231 + * Returns 0 on success or an errno on failure. 232 + */ 233 + static long seccomp_attach_filter(struct sock_fprog *fprog) 234 + { 235 + struct seccomp_filter *filter; 236 + unsigned long fp_size = fprog->len * sizeof(struct sock_filter); 237 + unsigned long total_insns = fprog->len; 238 + long ret; 239 + 240 + if (fprog->len == 0 || fprog->len > BPF_MAXINSNS) 241 + return -EINVAL; 242 + 243 + for (filter = current->seccomp.filter; filter; filter = filter->prev) 244 + total_insns += filter->len + 4; /* include a 4 instr penalty */ 245 + if (total_insns > MAX_INSNS_PER_PATH) 246 + return -ENOMEM; 247 + 248 + /* 249 + * Installing a seccomp filter requires that the task have 250 + * CAP_SYS_ADMIN in its namespace or be running with no_new_privs. 251 + * This avoids scenarios where unprivileged tasks can affect the 252 + * behavior of privileged children. 253 + */ 254 + if (!current->no_new_privs && 255 + security_capable_noaudit(current_cred(), current_user_ns(), 256 + CAP_SYS_ADMIN) != 0) 257 + return -EACCES; 258 + 259 + /* Allocate a new seccomp_filter */ 260 + filter = kzalloc(sizeof(struct seccomp_filter) + fp_size, 261 + GFP_KERNEL|__GFP_NOWARN); 262 + if (!filter) 263 + return -ENOMEM; 264 + atomic_set(&filter->usage, 1); 265 + filter->len = fprog->len; 266 + 267 + /* Copy the instructions from fprog. */ 268 + ret = -EFAULT; 269 + if (copy_from_user(filter->insns, fprog->filter, fp_size)) 270 + goto fail; 271 + 272 + /* Check and rewrite the fprog via the skb checker */ 273 + ret = sk_chk_filter(filter->insns, filter->len); 274 + if (ret) 275 + goto fail; 276 + 277 + /* Check and rewrite the fprog for seccomp use */ 278 + ret = seccomp_check_filter(filter->insns, filter->len); 279 + if (ret) 280 + goto fail; 281 + 282 + /* 283 + * If there is an existing filter, make it the prev and don't drop its 284 + * task reference. 285 + */ 286 + filter->prev = current->seccomp.filter; 287 + current->seccomp.filter = filter; 288 + return 0; 289 + fail: 290 + kfree(filter); 291 + return ret; 292 + } 293 + 294 + /** 295 + * seccomp_attach_user_filter - attaches a user-supplied sock_fprog 296 + * @user_filter: pointer to the user data containing a sock_fprog. 297 + * 298 + * Returns 0 on success and non-zero otherwise. 299 + */ 300 + long seccomp_attach_user_filter(char __user *user_filter) 301 + { 302 + struct sock_fprog fprog; 303 + long ret = -EFAULT; 304 + 305 + #ifdef CONFIG_COMPAT 306 + if (is_compat_task()) { 307 + struct compat_sock_fprog fprog32; 308 + if (copy_from_user(&fprog32, user_filter, sizeof(fprog32))) 309 + goto out; 310 + fprog.len = fprog32.len; 311 + fprog.filter = compat_ptr(fprog32.filter); 312 + } else /* falls through to the if below. */ 313 + #endif 314 + if (copy_from_user(&fprog, user_filter, sizeof(fprog))) 315 + goto out; 316 + ret = seccomp_attach_filter(&fprog); 317 + out: 318 + return ret; 319 + } 320 + 321 + /* get_seccomp_filter - increments the reference count of the filter on @tsk */ 322 + void get_seccomp_filter(struct task_struct *tsk) 323 + { 324 + struct seccomp_filter *orig = tsk->seccomp.filter; 325 + if (!orig) 326 + return; 327 + /* Reference count is bounded by the number of total processes. */ 328 + atomic_inc(&orig->usage); 329 + } 330 + 331 + /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */ 332 + void put_seccomp_filter(struct task_struct *tsk) 333 + { 334 + struct seccomp_filter *orig = tsk->seccomp.filter; 335 + /* Clean up single-reference branches iteratively. */ 336 + while (orig && atomic_dec_and_test(&orig->usage)) { 337 + struct seccomp_filter *freeme = orig; 338 + orig = orig->prev; 339 + kfree(freeme); 340 + } 341 + } 342 + #endif /* CONFIG_SECCOMP_FILTER */ 16 343 17 344 /* 18 345 * Secure computing mode 1 allows only read/write/exit/sigreturn. ··· 361 34 void __secure_computing(int this_syscall) 362 35 { 363 36 int mode = current->seccomp.mode; 364 - int * syscall; 37 + int exit_sig = 0; 38 + int *syscall; 365 39 366 40 switch (mode) { 367 - case 1: 41 + case SECCOMP_MODE_STRICT: 368 42 syscall = mode1_syscalls; 369 43 #ifdef CONFIG_COMPAT 370 44 if (is_compat_task()) ··· 375 47 if (*syscall == this_syscall) 376 48 return; 377 49 } while (*++syscall); 50 + exit_sig = SIGKILL; 378 51 break; 52 + #ifdef CONFIG_SECCOMP_FILTER 53 + case SECCOMP_MODE_FILTER: 54 + if (seccomp_run_filters(this_syscall) == SECCOMP_RET_ALLOW) 55 + return; 56 + seccomp_filter_log_failure(this_syscall); 57 + exit_sig = SIGSYS; 58 + break; 59 + #endif 379 60 default: 380 61 BUG(); 381 62 } ··· 393 56 dump_stack(); 394 57 #endif 395 58 audit_seccomp(this_syscall); 396 - do_exit(SIGKILL); 59 + do_exit(exit_sig); 397 60 } 398 61 399 62 long prctl_get_seccomp(void) ··· 401 64 return current->seccomp.mode; 402 65 } 403 66 404 - long prctl_set_seccomp(unsigned long seccomp_mode) 67 + /** 68 + * prctl_set_seccomp: configures current->seccomp.mode 69 + * @seccomp_mode: requested mode to use 70 + * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER 71 + * 72 + * This function may be called repeatedly with a @seccomp_mode of 73 + * SECCOMP_MODE_FILTER to install additional filters. Every filter 74 + * successfully installed will be evaluated (in reverse order) for each system 75 + * call the task makes. 76 + * 77 + * Once current->seccomp.mode is non-zero, it may not be changed. 78 + * 79 + * Returns 0 on success or -EINVAL on failure. 80 + */ 81 + long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter) 405 82 { 406 - long ret; 83 + long ret = -EINVAL; 407 84 408 - /* can set it only once to be even more secure */ 409 - ret = -EPERM; 410 - if (unlikely(current->seccomp.mode)) 85 + if (current->seccomp.mode && 86 + current->seccomp.mode != seccomp_mode) 411 87 goto out; 412 88 413 - ret = -EINVAL; 414 - if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) { 415 - current->seccomp.mode = seccomp_mode; 416 - set_thread_flag(TIF_SECCOMP); 89 + switch (seccomp_mode) { 90 + case SECCOMP_MODE_STRICT: 91 + ret = 0; 417 92 #ifdef TIF_NOTSC 418 93 disable_TSC(); 419 94 #endif 420 - ret = 0; 95 + break; 96 + #ifdef CONFIG_SECCOMP_FILTER 97 + case SECCOMP_MODE_FILTER: 98 + ret = seccomp_attach_user_filter(filter); 99 + if (ret) 100 + goto out; 101 + break; 102 + #endif 103 + default: 104 + goto out; 421 105 } 422 106 423 - out: 107 + current->seccomp.mode = seccomp_mode; 108 + set_thread_flag(TIF_SECCOMP); 109 + out: 424 110 return ret; 425 111 }
+1 -1
kernel/sys.c
··· 1908 1908 error = prctl_get_seccomp(); 1909 1909 break; 1910 1910 case PR_SET_SECCOMP: 1911 - error = prctl_set_seccomp(arg2); 1911 + error = prctl_set_seccomp(arg2, (char __user *)arg3); 1912 1912 break; 1913 1913 case PR_GET_TSC: 1914 1914 error = GET_TSC_CTL(arg2);