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