blob: e35c742a05d5d87416b14833b36827b9911f0118 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/seccomp.c
3 *
4 * Copyright 2004-2005 Andrea Arcangeli <andrea@cpushare.com>
5 *
Will Drewry980e9202012-02-09 11:50:58 -06006 * 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 Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
Will Drewry980e9202012-02-09 11:50:58 -060016#include <linux/atomic.h>
Eric Paris85e7bac2012-01-03 14:23:05 -050017#include <linux/audit.h>
Roland McGrath5b101742009-02-27 23:25:54 -080018#include <linux/compat.h>
Will Drewry980e9202012-02-09 11:50:58 -060019#include <linux/sched.h>
20#include <linux/seccomp.h>
Kees Cook42835bc2014-06-27 15:16:33 -070021#include <linux/slab.h>
Kees Cook95de3e52014-06-25 16:08:24 -070022#include <linux/syscalls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/* #define SECCOMP_DEBUG 1 */
Will Drewry980e9202012-02-09 11:50:58 -060025
26#ifdef CONFIG_SECCOMP_FILTER
27#include <asm/syscall.h>
28#include <linux/filter.h>
Kees Cookd32af362014-06-05 00:23:17 -070029#include <linux/pid.h>
Will Drewry4ca6e672012-02-09 12:08:39 -060030#include <linux/ptrace.h>
Will Drewry980e9202012-02-09 11:50:58 -060031#include <linux/security.h>
Will Drewry980e9202012-02-09 11:50:58 -060032#include <linux/tracehook.h>
33#include <linux/uaccess.h>
34
35/**
36 * struct seccomp_filter - container for seccomp BPF programs
37 *
38 * @usage: reference count to manage the object lifetime.
39 * get/put helpers should be used when accessing an instance
40 * outside of a lifetime-guarded section. In general, this
41 * is only needed for handling filters shared across tasks.
42 * @prev: points to a previously installed, or inherited, filter
43 * @len: the number of instructions in the program
44 * @insns: the BPF program instructions to evaluate
45 *
46 * seccomp_filter objects are organized in a tree linked via the @prev
47 * pointer. For any task, it appears to be a singly-linked list starting
48 * with current->seccomp.filter, the most recently attached or inherited filter.
49 * However, multiple filters may share a @prev node, by way of fork(), which
50 * results in a unidirectional tree existing in memory. This is similar to
51 * how namespaces work.
52 *
53 * seccomp_filter objects should never be modified after being attached
54 * to a task_struct (other than @usage).
55 */
56struct seccomp_filter {
57 atomic_t usage;
58 struct seccomp_filter *prev;
59 unsigned short len; /* Instruction count */
60 struct sock_filter insns[];
61};
62
63/* Limit any path through the tree to 256KB worth of instructions. */
64#define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
65
Will Drewry980e9202012-02-09 11:50:58 -060066/**
67 * get_u32 - returns a u32 offset into data
68 * @data: a unsigned 64 bit value
69 * @index: 0 or 1 to return the first or second 32-bits
70 *
71 * This inline exists to hide the length of unsigned long. If a 32-bit
72 * unsigned long is passed in, it will be extended and the top 32-bits will be
73 * 0. If it is a 64-bit unsigned long, then whatever data is resident will be
74 * properly returned.
75 *
76 * Endianness is explicitly ignored and left for BPF program authors to manage
77 * as per the specific architecture.
78 */
79static inline u32 get_u32(u64 data, int index)
80{
81 return ((u32 *)&data)[index];
82}
83
84/* Helper for bpf_load below. */
85#define BPF_DATA(_name) offsetof(struct seccomp_data, _name)
86/**
87 * bpf_load: checks and returns a pointer to the requested offset
88 * @off: offset into struct seccomp_data to load from
89 *
90 * Returns the requested 32-bits of data.
91 * seccomp_check_filter() should assure that @off is 32-bit aligned
92 * and not out of bounds. Failure to do so is a BUG.
93 */
94u32 seccomp_bpf_load(int off)
95{
96 struct pt_regs *regs = task_pt_regs(current);
97 if (off == BPF_DATA(nr))
98 return syscall_get_nr(current, regs);
99 if (off == BPF_DATA(arch))
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 */
128static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
129{
130 int pc;
131 for (pc = 0; pc < flen; pc++) {
132 struct sock_filter *ftest = &filter[pc];
133 u16 code = ftest->code;
134 u32 k = ftest->k;
135
136 switch (code) {
137 case BPF_S_LD_W_ABS:
138 ftest->code = BPF_S_ANC_SECCOMP_LD_W;
139 /* 32-bit aligned and not out of bounds. */
140 if (k >= sizeof(struct seccomp_data) || k & 3)
141 return -EINVAL;
142 continue;
143 case BPF_S_LD_W_LEN:
144 ftest->code = BPF_S_LD_IMM;
145 ftest->k = sizeof(struct seccomp_data);
146 continue;
147 case BPF_S_LDX_W_LEN:
148 ftest->code = BPF_S_LDX_IMM;
149 ftest->k = sizeof(struct seccomp_data);
150 continue;
151 /* Explicitly include allowed calls. */
152 case BPF_S_RET_K:
153 case BPF_S_RET_A:
154 case BPF_S_ALU_ADD_K:
155 case BPF_S_ALU_ADD_X:
156 case BPF_S_ALU_SUB_K:
157 case BPF_S_ALU_SUB_X:
158 case BPF_S_ALU_MUL_K:
159 case BPF_S_ALU_MUL_X:
160 case BPF_S_ALU_DIV_X:
161 case BPF_S_ALU_AND_K:
162 case BPF_S_ALU_AND_X:
163 case BPF_S_ALU_OR_K:
164 case BPF_S_ALU_OR_X:
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 */
202static u32 seccomp_run_filters(int syscall)
203{
Kees Cook38daf672014-06-27 15:01:35 -0700204 struct seccomp_filter *f = ACCESS_ONCE(current->seccomp.filter);
Will Drewry43ec8252012-02-15 20:45:54 -0600205 u32 ret = SECCOMP_RET_ALLOW;
206
207 /* Ensure unexpected behavior doesn't result in failing open. */
Kees Cook38daf672014-06-27 15:01:35 -0700208 if (unlikely(WARN_ON(f == NULL)))
Will Drewry43ec8252012-02-15 20:45:54 -0600209 return SECCOMP_RET_KILL;
210
Kees Cook38daf672014-06-27 15:01:35 -0700211 /* Make sure cross-thread synced filter points somewhere sane. */
212 smp_read_barrier_depends();
213
Will Drewry980e9202012-02-09 11:50:58 -0600214 /*
215 * All filters in the list are evaluated and the lowest BPF return
Will Drewry43ec8252012-02-15 20:45:54 -0600216 * value always takes priority (ignoring the DATA).
Will Drewry980e9202012-02-09 11:50:58 -0600217 */
Kees Cook38daf672014-06-27 15:01:35 -0700218 for (; f; f = f->prev) {
Will Drewry43ec8252012-02-15 20:45:54 -0600219 u32 cur_ret = sk_run_filter(NULL, f->insns);
Kees Cook38daf672014-06-27 15:01:35 -0700220
Will Drewry43ec8252012-02-15 20:45:54 -0600221 if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
222 ret = cur_ret;
Will Drewry980e9202012-02-09 11:50:58 -0600223 }
224 return ret;
225}
Kees Cookc5c2ce72014-06-25 15:38:02 -0700226#endif /* CONFIG_SECCOMP_FILTER */
Will Drewry980e9202012-02-09 11:50:58 -0600227
Kees Cookc5c2ce72014-06-25 15:38:02 -0700228static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
229{
Kees Cook880e1ab2014-06-27 15:18:48 -0700230 BUG_ON(!spin_is_locked(&current->sighand->siglock));
231
Kees Cookc5c2ce72014-06-25 15:38:02 -0700232 if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
233 return false;
234
235 return true;
236}
237
Kees Cook38daf672014-06-27 15:01:35 -0700238static inline void seccomp_assign_mode(struct task_struct *task,
239 unsigned long seccomp_mode)
Kees Cookc5c2ce72014-06-25 15:38:02 -0700240{
Kees Cook38daf672014-06-27 15:01:35 -0700241 BUG_ON(!spin_is_locked(&task->sighand->siglock));
Kees Cook880e1ab2014-06-27 15:18:48 -0700242
Kees Cook38daf672014-06-27 15:01:35 -0700243 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 Cookc5c2ce72014-06-25 15:38:02 -0700250}
251
252#ifdef CONFIG_SECCOMP_FILTER
Kees Cookd32af362014-06-05 00:23:17 -0700253/* Returns 1 if the parent is an ancestor of the child. */
254static 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 */
275static inline pid_t seccomp_can_sync_threads(void)
276{
277 struct task_struct *thread, *caller;
278
279 BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
280 BUG_ON(!spin_is_locked(&current->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 */
316static inline void seccomp_sync_threads(void)
317{
318 struct task_struct *thread, *caller;
319
320 BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
321 BUG_ON(!spin_is_locked(&current->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 Drewry980e9202012-02-09 11:50:58 -0600361/**
Kees Cook42835bc2014-06-27 15:16:33 -0700362 * seccomp_prepare_filter: Prepares a seccomp filter for use.
Will Drewry980e9202012-02-09 11:50:58 -0600363 * @fprog: BPF program to install
364 *
Kees Cook42835bc2014-06-27 15:16:33 -0700365 * Returns filter on success or an ERR_PTR on failure.
Will Drewry980e9202012-02-09 11:50:58 -0600366 */
Kees Cook42835bc2014-06-27 15:16:33 -0700367static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
Will Drewry980e9202012-02-09 11:50:58 -0600368{
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 Cook42835bc2014-06-27 15:16:33 -0700375 return ERR_PTR(-EINVAL);
376 BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
Will Drewry980e9202012-02-09 11:50:58 -0600377
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 Cook42835bc2014-06-27 15:16:33 -0700381 return ERR_PTR(-ENOMEM);
Will Drewry980e9202012-02-09 11:50:58 -0600382
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 Cook5eab1302014-05-21 15:23:46 -0700389 if (!task_no_new_privs(current) &&
Will Drewry980e9202012-02-09 11:50:58 -0600390 security_capable_noaudit(current_cred(), current_user_ns(),
391 CAP_SYS_ADMIN) != 0)
Kees Cook42835bc2014-06-27 15:16:33 -0700392 return ERR_PTR(-EACCES);
Will Drewry980e9202012-02-09 11:50:58 -0600393
394 /* Allocate a new seccomp_filter */
395 filter = kzalloc(sizeof(struct seccomp_filter) + fp_size,
396 GFP_KERNEL|__GFP_NOWARN);
397 if (!filter)
Kees Cook42835bc2014-06-27 15:16:33 -0700398 return ERR_PTR(-ENOMEM);
Will Drewry980e9202012-02-09 11:50:58 -0600399 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 Cook42835bc2014-06-27 15:16:33 -0700417 return filter;
Will Drewry980e9202012-02-09 11:50:58 -0600418fail:
419 kfree(filter);
Kees Cook42835bc2014-06-27 15:16:33 -0700420 return ERR_PTR(ret);
Will Drewry980e9202012-02-09 11:50:58 -0600421}
422
423/**
Kees Cook42835bc2014-06-27 15:16:33 -0700424 * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
Will Drewry980e9202012-02-09 11:50:58 -0600425 * @user_filter: pointer to the user data containing a sock_fprog.
426 *
427 * Returns 0 on success and non-zero otherwise.
428 */
Kees Cook42835bc2014-06-27 15:16:33 -0700429static struct seccomp_filter *
430seccomp_prepare_user_filter(const char __user *user_filter)
Will Drewry980e9202012-02-09 11:50:58 -0600431{
432 struct sock_fprog fprog;
Kees Cook42835bc2014-06-27 15:16:33 -0700433 struct seccomp_filter *filter = ERR_PTR(-EFAULT);
Will Drewry980e9202012-02-09 11:50:58 -0600434
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 Cook42835bc2014-06-27 15:16:33 -0700446 filter = seccomp_prepare_filter(&fprog);
Will Drewry980e9202012-02-09 11:50:58 -0600447out:
Kees Cook42835bc2014-06-27 15:16:33 -0700448 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 Cook880e1ab2014-06-27 15:18:48 -0700456 * Caller must be holding current->sighand->siglock lock.
457 *
Kees Cook42835bc2014-06-27 15:16:33 -0700458 * Returns 0 on success, -ve on error.
459 */
460static 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 Cook880e1ab2014-06-27 15:18:48 -0700466 BUG_ON(!spin_is_locked(&current->sighand->siglock));
467
Kees Cook42835bc2014-06-27 15:16:33 -0700468 /* 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 Cookd32af362014-06-05 00:23:17 -0700475 /* 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 Cook42835bc2014-06-27 15:16:33 -0700484 /*
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 Cookd32af362014-06-05 00:23:17 -0700491 /* 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 Cook42835bc2014-06-27 15:16:33 -0700495 return 0;
Will Drewry980e9202012-02-09 11:50:58 -0600496}
497
498/* get_seccomp_filter - increments the reference count of the filter on @tsk */
499void 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 Cook42835bc2014-06-27 15:16:33 -0700508static inline void seccomp_filter_free(struct seccomp_filter *filter)
509{
510 if (filter) {
511 kfree(filter);
512 }
513}
514
Will Drewry980e9202012-02-09 11:50:58 -0600515/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
516void 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 Cook42835bc2014-06-27 15:16:33 -0700523 seccomp_filter_free(freeme);
Will Drewry980e9202012-02-09 11:50:58 -0600524 }
525}
Will Drewry69537022012-02-09 12:01:37 -0600526
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 */
534static 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 Drewry980e9202012-02-09 11:50:58 -0600546#endif /* CONFIG_SECCOMP_FILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
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 */
553static int mode1_syscalls[] = {
554 __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
555 0, /* null terminated */
556};
557
Roland McGrath5b101742009-02-27 23:25:54 -0800558#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559static 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 Drewry43ec8252012-02-15 20:45:54 -0600565int __secure_computing(int this_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Will Drewry980e9202012-02-09 11:50:58 -0600567 int exit_sig = 0;
568 int *syscall;
Will Drewry171ba892012-04-17 14:48:58 -0500569 u32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Kees Cook38daf672014-06-27 15:01:35 -0700571 /*
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 Drewry980e9202012-02-09 11:50:58 -0600578 case SECCOMP_MODE_STRICT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 syscall = mode1_syscalls;
Roland McGrath5b101742009-02-27 23:25:54 -0800580#ifdef CONFIG_COMPAT
581 if (is_compat_task())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 syscall = mode1_syscalls_32;
583#endif
584 do {
585 if (*syscall == this_syscall)
Will Drewry43ec8252012-02-15 20:45:54 -0600586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 } while (*++syscall);
Will Drewry980e9202012-02-09 11:50:58 -0600588 exit_sig = SIGKILL;
Will Drewry171ba892012-04-17 14:48:58 -0500589 ret = SECCOMP_RET_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 break;
Will Drewry980e9202012-02-09 11:50:58 -0600591#ifdef CONFIG_SECCOMP_FILTER
Will Drewry171ba892012-04-17 14:48:58 -0500592 case SECCOMP_MODE_FILTER: {
593 int data;
Will Drewry43ec8252012-02-15 20:45:54 -0600594 ret = seccomp_run_filters(this_syscall);
595 data = ret & SECCOMP_RET_DATA;
Will Drewry171ba892012-04-17 14:48:58 -0500596 ret &= SECCOMP_RET_ACTION;
597 switch (ret) {
Will Drewry43ec8252012-02-15 20:45:54 -0600598 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 Drewry69537022012-02-09 12:01:37 -0600603 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 Drewry4ca6e672012-02-09 12:08:39 -0600609 case SECCOMP_RET_TRACE:
610 /* Skip these calls if there is no tracer. */
Will Drewrybefe2872012-04-27 11:25:30 -0500611 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 Drewry4ca6e672012-02-09 12:08:39 -0600615 goto skip;
Will Drewrybefe2872012-04-27 11:25:30 -0500616 }
Will Drewry4ca6e672012-02-09 12:08:39 -0600617 /* 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 Drewry43ec8252012-02-15 20:45:54 -0600628 case SECCOMP_RET_ALLOW:
629 return 0;
630 case SECCOMP_RET_KILL:
631 default:
632 break;
633 }
Will Drewry980e9202012-02-09 11:50:58 -0600634 exit_sig = SIGSYS;
635 break;
Will Drewry171ba892012-04-17 14:48:58 -0500636 }
Will Drewry980e9202012-02-09 11:50:58 -0600637#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 default:
639 BUG();
640 }
641
642#ifdef SECCOMP_DEBUG
643 dump_stack();
644#endif
Will Drewry43ec8252012-02-15 20:45:54 -0600645 audit_seccomp(this_syscall, exit_sig, ret);
Will Drewry980e9202012-02-09 11:50:58 -0600646 do_exit(exit_sig);
Will Drewry171ba892012-04-17 14:48:58 -0500647#ifdef CONFIG_SECCOMP_FILTER
Will Drewry43ec8252012-02-15 20:45:54 -0600648skip:
649 audit_seccomp(this_syscall, exit_sig, ret);
Will Drewry171ba892012-04-17 14:48:58 -0500650#endif
Will Drewry43ec8252012-02-15 20:45:54 -0600651 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700653
654long prctl_get_seccomp(void)
655{
656 return current->seccomp.mode;
657}
658
Will Drewry980e9202012-02-09 11:50:58 -0600659/**
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700660 * seccomp_set_mode_strict: internal function for setting strict seccomp
Will Drewry980e9202012-02-09 11:50:58 -0600661 *
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 Cookbc4bd0f2014-06-25 15:55:25 -0700666static long seccomp_set_mode_strict(void)
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700667{
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700668 const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
Will Drewry980e9202012-02-09 11:50:58 -0600669 long ret = -EINVAL;
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700670
Kees Cook880e1ab2014-06-27 15:18:48 -0700671 spin_lock_irq(&current->sighand->siglock);
672
Kees Cookc5c2ce72014-06-25 15:38:02 -0700673 if (!seccomp_may_assign_mode(seccomp_mode))
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700674 goto out;
675
Andrea Arcangelicf99aba2007-07-15 23:41:33 -0700676#ifdef TIF_NOTSC
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700677 disable_TSC();
Andrea Arcangelicf99aba2007-07-15 23:41:33 -0700678#endif
Kees Cook38daf672014-06-27 15:01:35 -0700679 seccomp_assign_mode(current, seccomp_mode);
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700680 ret = 0;
681
682out:
Kees Cook880e1ab2014-06-27 15:18:48 -0700683 spin_unlock_irq(&current->sighand->siglock);
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700684
685 return ret;
686}
687
Will Drewry980e9202012-02-09 11:50:58 -0600688#ifdef CONFIG_SECCOMP_FILTER
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700689/**
690 * seccomp_set_mode_filter: internal function for setting seccomp filter
Kees Cook95de3e52014-06-25 16:08:24 -0700691 * @flags: flags to change filter behavior
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700692 * @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 Cook95de3e52014-06-25 16:08:24 -0700702static long seccomp_set_mode_filter(unsigned int flags,
703 const char __user *filter)
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700704{
705 const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
Kees Cook42835bc2014-06-27 15:16:33 -0700706 struct seccomp_filter *prepared = NULL;
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700707 long ret = -EINVAL;
708
Kees Cook95de3e52014-06-25 16:08:24 -0700709 /* Validate flags. */
Kees Cookd32af362014-06-05 00:23:17 -0700710 if (flags & ~SECCOMP_FILTER_FLAG_MASK)
Kees Cook880e1ab2014-06-27 15:18:48 -0700711 return -EINVAL;
Kees Cook95de3e52014-06-25 16:08:24 -0700712
Kees Cook42835bc2014-06-27 15:16:33 -0700713 /* 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 Cookd32af362014-06-05 00:23:17 -0700718 /*
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(&current->signal->cred_guard_mutex))
724 goto out_free;
725
Kees Cook880e1ab2014-06-27 15:18:48 -0700726 spin_lock_irq(&current->sighand->siglock);
727
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700728 if (!seccomp_may_assign_mode(seccomp_mode))
Will Drewry980e9202012-02-09 11:50:58 -0600729 goto out;
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700730
Kees Cook42835bc2014-06-27 15:16:33 -0700731 ret = seccomp_attach_filter(flags, prepared);
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700732 if (ret)
733 goto out;
Kees Cook42835bc2014-06-27 15:16:33 -0700734 /* Do not free the successfully attached filter. */
735 prepared = NULL;
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700736
Kees Cook38daf672014-06-27 15:01:35 -0700737 seccomp_assign_mode(current, seccomp_mode);
Will Drewry980e9202012-02-09 11:50:58 -0600738out:
Kees Cook880e1ab2014-06-27 15:18:48 -0700739 spin_unlock_irq(&current->sighand->siglock);
Kees Cookd32af362014-06-05 00:23:17 -0700740 if (flags & SECCOMP_FILTER_FLAG_TSYNC)
741 mutex_unlock(&current->signal->cred_guard_mutex);
742out_free:
Kees Cook42835bc2014-06-27 15:16:33 -0700743 seccomp_filter_free(prepared);
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700744 return ret;
745}
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700746#else
Kees Cook95de3e52014-06-25 16:08:24 -0700747static inline long seccomp_set_mode_filter(unsigned int flags,
748 const char __user *filter)
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700749{
750 return -EINVAL;
751}
752#endif
Kees Cook2d5f6982014-05-21 15:02:11 -0700753
Kees Cook95de3e52014-06-25 16:08:24 -0700754/* Common entry point for both prctl and syscall. */
755static 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
770SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
771 const char __user *, uargs)
772{
773 return do_seccomp(op, flags, uargs);
774}
775
Kees Cook2d5f6982014-05-21 15:02:11 -0700776/**
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 */
783long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
784{
Kees Cook95de3e52014-06-25 16:08:24 -0700785 unsigned int op;
786 char __user *uargs;
787
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700788 switch (seccomp_mode) {
789 case SECCOMP_MODE_STRICT:
Kees Cook95de3e52014-06-25 16:08:24 -0700790 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 Cookbc4bd0f2014-06-25 15:55:25 -0700798 case SECCOMP_MODE_FILTER:
Kees Cook95de3e52014-06-25 16:08:24 -0700799 op = SECCOMP_SET_MODE_FILTER;
800 uargs = filter;
801 break;
Kees Cookbc4bd0f2014-06-25 15:55:25 -0700802 default:
803 return -EINVAL;
804 }
Kees Cook95de3e52014-06-25 16:08:24 -0700805
806 /* prctl interface doesn't have flags, so they are always zero. */
807 return do_seccomp(op, 0, uargs);
Kees Cook2d5f6982014-05-21 15:02:11 -0700808}