blob: 590c37925084d08e046e6f51de58a4845b7d5f72 [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 Drewrye2cfabdf2012-04-12 16:47:57 -05006 * 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 Drewrye2cfabdf2012-04-12 16:47:57 -050016#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 Drewrye2cfabdf2012-04-12 16:47:57 -050019#include <linux/sched.h>
20#include <linux/seccomp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/* #define SECCOMP_DEBUG 1 */
Will Drewrye2cfabdf2012-04-12 16:47:57 -050023
24#ifdef CONFIG_SECCOMP_FILTER
25#include <asm/syscall.h>
26#include <linux/filter.h>
Will Drewryfb0fadf2012-04-12 16:48:02 -050027#include <linux/ptrace.h>
Will Drewrye2cfabdf2012-04-12 16:47:57 -050028#include <linux/security.h>
29#include <linux/slab.h>
30#include <linux/tracehook.h>
31#include <linux/uaccess.h>
32
33/**
34 * struct seccomp_filter - container for seccomp BPF programs
35 *
36 * @usage: reference count to manage the object lifetime.
37 * get/put helpers should be used when accessing an instance
38 * outside of a lifetime-guarded section. In general, this
39 * is only needed for handling filters shared across tasks.
40 * @prev: points to a previously installed, or inherited, filter
41 * @len: the number of instructions in the program
42 * @insns: the BPF program instructions to evaluate
43 *
44 * seccomp_filter objects are organized in a tree linked via the @prev
45 * pointer. For any task, it appears to be a singly-linked list starting
46 * with current->seccomp.filter, the most recently attached or inherited filter.
47 * However, multiple filters may share a @prev node, by way of fork(), which
48 * results in a unidirectional tree existing in memory. This is similar to
49 * how namespaces work.
50 *
51 * seccomp_filter objects should never be modified after being attached
52 * to a task_struct (other than @usage).
53 */
54struct seccomp_filter {
55 atomic_t usage;
56 struct seccomp_filter *prev;
57 unsigned short len; /* Instruction count */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010058 struct sock_filter_int insnsi[];
Will Drewrye2cfabdf2012-04-12 16:47:57 -050059};
60
61/* Limit any path through the tree to 256KB worth of instructions. */
62#define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
63
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010064/*
Will Drewrye2cfabdf2012-04-12 16:47:57 -050065 * Endianness is explicitly ignored and left for BPF program authors to manage
66 * as per the specific architecture.
67 */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010068static void populate_seccomp_data(struct seccomp_data *sd)
Will Drewrye2cfabdf2012-04-12 16:47:57 -050069{
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010070 struct task_struct *task = current;
71 struct pt_regs *regs = task_pt_regs(task);
Daniel Borkmann2eac7642014-04-14 21:02:59 +020072 unsigned long args[6];
Will Drewrye2cfabdf2012-04-12 16:47:57 -050073
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010074 sd->nr = syscall_get_nr(task, regs);
Linus Torvalds0b747172014-04-12 12:38:53 -070075 sd->arch = syscall_get_arch();
Daniel Borkmann2eac7642014-04-14 21:02:59 +020076 syscall_get_arguments(task, regs, 0, 6, args);
77 sd->args[0] = args[0];
78 sd->args[1] = args[1];
79 sd->args[2] = args[2];
80 sd->args[3] = args[3];
81 sd->args[4] = args[4];
82 sd->args[5] = args[5];
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +010083 sd->instruction_pointer = KSTK_EIP(task);
Will Drewrye2cfabdf2012-04-12 16:47:57 -050084}
85
86/**
87 * seccomp_check_filter - verify seccomp filter code
88 * @filter: filter to verify
89 * @flen: length of filter
90 *
91 * Takes a previously checked filter (by sk_chk_filter) and
92 * redirects all filter code that loads struct sk_buff data
93 * and related data through seccomp_bpf_load. It also
94 * enforces length and alignment checking of those loads.
95 *
96 * Returns 0 if the rule set is legal or -EINVAL if not.
97 */
98static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
99{
100 int pc;
101 for (pc = 0; pc < flen; pc++) {
102 struct sock_filter *ftest = &filter[pc];
103 u16 code = ftest->code;
104 u32 k = ftest->k;
105
106 switch (code) {
107 case BPF_S_LD_W_ABS:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100108 ftest->code = BPF_LDX | BPF_W | BPF_ABS;
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500109 /* 32-bit aligned and not out of bounds. */
110 if (k >= sizeof(struct seccomp_data) || k & 3)
111 return -EINVAL;
112 continue;
113 case BPF_S_LD_W_LEN:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100114 ftest->code = BPF_LD | BPF_IMM;
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500115 ftest->k = sizeof(struct seccomp_data);
116 continue;
117 case BPF_S_LDX_W_LEN:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100118 ftest->code = BPF_LDX | BPF_IMM;
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500119 ftest->k = sizeof(struct seccomp_data);
120 continue;
121 /* Explicitly include allowed calls. */
122 case BPF_S_RET_K:
123 case BPF_S_RET_A:
124 case BPF_S_ALU_ADD_K:
125 case BPF_S_ALU_ADD_X:
126 case BPF_S_ALU_SUB_K:
127 case BPF_S_ALU_SUB_X:
128 case BPF_S_ALU_MUL_K:
129 case BPF_S_ALU_MUL_X:
130 case BPF_S_ALU_DIV_X:
131 case BPF_S_ALU_AND_K:
132 case BPF_S_ALU_AND_X:
133 case BPF_S_ALU_OR_K:
134 case BPF_S_ALU_OR_X:
Nicolas Schichand1327472013-03-15 18:02:00 +0100135 case BPF_S_ALU_XOR_K:
136 case BPF_S_ALU_XOR_X:
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500137 case BPF_S_ALU_LSH_K:
138 case BPF_S_ALU_LSH_X:
139 case BPF_S_ALU_RSH_K:
140 case BPF_S_ALU_RSH_X:
141 case BPF_S_ALU_NEG:
142 case BPF_S_LD_IMM:
143 case BPF_S_LDX_IMM:
144 case BPF_S_MISC_TAX:
145 case BPF_S_MISC_TXA:
146 case BPF_S_ALU_DIV_K:
147 case BPF_S_LD_MEM:
148 case BPF_S_LDX_MEM:
149 case BPF_S_ST:
150 case BPF_S_STX:
151 case BPF_S_JMP_JA:
152 case BPF_S_JMP_JEQ_K:
153 case BPF_S_JMP_JEQ_X:
154 case BPF_S_JMP_JGE_K:
155 case BPF_S_JMP_JGE_X:
156 case BPF_S_JMP_JGT_K:
157 case BPF_S_JMP_JGT_X:
158 case BPF_S_JMP_JSET_K:
159 case BPF_S_JMP_JSET_X:
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100160 sk_decode_filter(ftest, ftest);
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500161 continue;
162 default:
163 return -EINVAL;
164 }
165 }
166 return 0;
167}
168
169/**
170 * seccomp_run_filters - evaluates all seccomp filters against @syscall
171 * @syscall: number of the current system call
172 *
173 * Returns valid seccomp BPF response codes.
174 */
175static u32 seccomp_run_filters(int syscall)
176{
177 struct seccomp_filter *f;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100178 struct seccomp_data sd;
Will Drewryacf3b2c2012-04-12 16:47:59 -0500179 u32 ret = SECCOMP_RET_ALLOW;
180
181 /* Ensure unexpected behavior doesn't result in failing open. */
182 if (WARN_ON(current->seccomp.filter == NULL))
183 return SECCOMP_RET_KILL;
184
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100185 populate_seccomp_data(&sd);
186
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500187 /*
188 * All filters in the list are evaluated and the lowest BPF return
Will Drewryacf3b2c2012-04-12 16:47:59 -0500189 * value always takes priority (ignoring the DATA).
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500190 */
191 for (f = current->seccomp.filter; f; f = f->prev) {
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100192 u32 cur_ret = sk_run_filter_int_seccomp(&sd, f->insnsi);
Will Drewryacf3b2c2012-04-12 16:47:59 -0500193 if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
194 ret = cur_ret;
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500195 }
196 return ret;
197}
198
199/**
200 * seccomp_attach_filter: Attaches a seccomp filter to current.
201 * @fprog: BPF program to install
202 *
203 * Returns 0 on success or an errno on failure.
204 */
205static long seccomp_attach_filter(struct sock_fprog *fprog)
206{
207 struct seccomp_filter *filter;
208 unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
209 unsigned long total_insns = fprog->len;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100210 struct sock_filter *fp;
211 int new_len;
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500212 long ret;
213
214 if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
215 return -EINVAL;
216
217 for (filter = current->seccomp.filter; filter; filter = filter->prev)
218 total_insns += filter->len + 4; /* include a 4 instr penalty */
219 if (total_insns > MAX_INSNS_PER_PATH)
220 return -ENOMEM;
221
222 /*
223 * Installing a seccomp filter requires that the task have
224 * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
225 * This avoids scenarios where unprivileged tasks can affect the
226 * behavior of privileged children.
227 */
228 if (!current->no_new_privs &&
229 security_capable_noaudit(current_cred(), current_user_ns(),
230 CAP_SYS_ADMIN) != 0)
231 return -EACCES;
232
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100233 fp = kzalloc(fp_size, GFP_KERNEL|__GFP_NOWARN);
234 if (!fp)
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500235 return -ENOMEM;
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500236
237 /* Copy the instructions from fprog. */
238 ret = -EFAULT;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100239 if (copy_from_user(fp, fprog->filter, fp_size))
240 goto free_prog;
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500241
242 /* Check and rewrite the fprog via the skb checker */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100243 ret = sk_chk_filter(fp, fprog->len);
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500244 if (ret)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100245 goto free_prog;
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500246
247 /* Check and rewrite the fprog for seccomp use */
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100248 ret = seccomp_check_filter(fp, fprog->len);
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500249 if (ret)
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100250 goto free_prog;
251
252 /* Convert 'sock_filter' insns to 'sock_filter_int' insns */
253 ret = sk_convert_filter(fp, fprog->len, NULL, &new_len);
254 if (ret)
255 goto free_prog;
256
257 /* Allocate a new seccomp_filter */
258 filter = kzalloc(sizeof(struct seccomp_filter) +
259 sizeof(struct sock_filter_int) * new_len,
260 GFP_KERNEL|__GFP_NOWARN);
261 if (!filter)
262 goto free_prog;
263
264 ret = sk_convert_filter(fp, fprog->len, filter->insnsi, &new_len);
265 if (ret)
266 goto free_filter;
267
268 atomic_set(&filter->usage, 1);
269 filter->len = new_len;
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500270
271 /*
272 * If there is an existing filter, make it the prev and don't drop its
273 * task reference.
274 */
275 filter->prev = current->seccomp.filter;
276 current->seccomp.filter = filter;
277 return 0;
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100278
279free_filter:
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500280 kfree(filter);
Alexei Starovoitovbd4cf0e2014-03-28 18:58:25 +0100281free_prog:
282 kfree(fp);
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500283 return ret;
284}
285
286/**
287 * seccomp_attach_user_filter - attaches a user-supplied sock_fprog
288 * @user_filter: pointer to the user data containing a sock_fprog.
289 *
290 * Returns 0 on success and non-zero otherwise.
291 */
Rashika Kheria864f32a2014-02-27 17:50:19 +0530292static long seccomp_attach_user_filter(char __user *user_filter)
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500293{
294 struct sock_fprog fprog;
295 long ret = -EFAULT;
296
297#ifdef CONFIG_COMPAT
298 if (is_compat_task()) {
299 struct compat_sock_fprog fprog32;
300 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
301 goto out;
302 fprog.len = fprog32.len;
303 fprog.filter = compat_ptr(fprog32.filter);
304 } else /* falls through to the if below. */
305#endif
306 if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
307 goto out;
308 ret = seccomp_attach_filter(&fprog);
309out:
310 return ret;
311}
312
313/* get_seccomp_filter - increments the reference count of the filter on @tsk */
314void get_seccomp_filter(struct task_struct *tsk)
315{
316 struct seccomp_filter *orig = tsk->seccomp.filter;
317 if (!orig)
318 return;
319 /* Reference count is bounded by the number of total processes. */
320 atomic_inc(&orig->usage);
321}
322
323/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
324void put_seccomp_filter(struct task_struct *tsk)
325{
326 struct seccomp_filter *orig = tsk->seccomp.filter;
327 /* Clean up single-reference branches iteratively. */
328 while (orig && atomic_dec_and_test(&orig->usage)) {
329 struct seccomp_filter *freeme = orig;
330 orig = orig->prev;
331 kfree(freeme);
332 }
333}
Will Drewrybb6ea432012-04-12 16:48:01 -0500334
335/**
336 * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
337 * @syscall: syscall number to send to userland
338 * @reason: filter-supplied reason code to send to userland (via si_errno)
339 *
340 * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
341 */
342static void seccomp_send_sigsys(int syscall, int reason)
343{
344 struct siginfo info;
345 memset(&info, 0, sizeof(info));
346 info.si_signo = SIGSYS;
347 info.si_code = SYS_SECCOMP;
348 info.si_call_addr = (void __user *)KSTK_EIP(current);
349 info.si_errno = reason;
Eric Paris5e937a92014-03-11 12:48:43 -0400350 info.si_arch = syscall_get_arch();
Will Drewrybb6ea432012-04-12 16:48:01 -0500351 info.si_syscall = syscall;
352 force_sig_info(SIGSYS, &info, current);
353}
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500354#endif /* CONFIG_SECCOMP_FILTER */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356/*
357 * Secure computing mode 1 allows only read/write/exit/sigreturn.
358 * To be fully secure this must be combined with rlimit
359 * to limit the stack allocations too.
360 */
361static int mode1_syscalls[] = {
362 __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
363 0, /* null terminated */
364};
365
Roland McGrath5b101742009-02-27 23:25:54 -0800366#ifdef CONFIG_COMPAT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static int mode1_syscalls_32[] = {
368 __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
369 0, /* null terminated */
370};
371#endif
372
Will Drewryacf3b2c2012-04-12 16:47:59 -0500373int __secure_computing(int this_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 int mode = current->seccomp.mode;
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500376 int exit_sig = 0;
377 int *syscall;
Will Drewry8156b452012-04-17 14:48:58 -0500378 u32 ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 switch (mode) {
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500381 case SECCOMP_MODE_STRICT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 syscall = mode1_syscalls;
Roland McGrath5b101742009-02-27 23:25:54 -0800383#ifdef CONFIG_COMPAT
384 if (is_compat_task())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 syscall = mode1_syscalls_32;
386#endif
387 do {
388 if (*syscall == this_syscall)
Will Drewryacf3b2c2012-04-12 16:47:59 -0500389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 } while (*++syscall);
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500391 exit_sig = SIGKILL;
Will Drewry8156b452012-04-17 14:48:58 -0500392 ret = SECCOMP_RET_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 break;
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500394#ifdef CONFIG_SECCOMP_FILTER
Will Drewry8156b452012-04-17 14:48:58 -0500395 case SECCOMP_MODE_FILTER: {
396 int data;
Andy Lutomirski87b526d2012-10-01 11:40:45 -0700397 struct pt_regs *regs = task_pt_regs(current);
Will Drewryacf3b2c2012-04-12 16:47:59 -0500398 ret = seccomp_run_filters(this_syscall);
399 data = ret & SECCOMP_RET_DATA;
Will Drewry8156b452012-04-17 14:48:58 -0500400 ret &= SECCOMP_RET_ACTION;
401 switch (ret) {
Will Drewryacf3b2c2012-04-12 16:47:59 -0500402 case SECCOMP_RET_ERRNO:
403 /* Set the low-order 16-bits as a errno. */
Andy Lutomirski87b526d2012-10-01 11:40:45 -0700404 syscall_set_return_value(current, regs,
Will Drewryacf3b2c2012-04-12 16:47:59 -0500405 -data, 0);
406 goto skip;
Will Drewrybb6ea432012-04-12 16:48:01 -0500407 case SECCOMP_RET_TRAP:
408 /* Show the handler the original registers. */
Andy Lutomirski87b526d2012-10-01 11:40:45 -0700409 syscall_rollback(current, regs);
Will Drewrybb6ea432012-04-12 16:48:01 -0500410 /* Let the filter pass back 16 bits of data. */
411 seccomp_send_sigsys(this_syscall, data);
412 goto skip;
Will Drewryfb0fadf2012-04-12 16:48:02 -0500413 case SECCOMP_RET_TRACE:
414 /* Skip these calls if there is no tracer. */
Andy Lutomirski87b526d2012-10-01 11:40:45 -0700415 if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
416 syscall_set_return_value(current, regs,
417 -ENOSYS, 0);
Will Drewryfb0fadf2012-04-12 16:48:02 -0500418 goto skip;
Andy Lutomirski87b526d2012-10-01 11:40:45 -0700419 }
Will Drewryfb0fadf2012-04-12 16:48:02 -0500420 /* Allow the BPF to provide the event message */
421 ptrace_event(PTRACE_EVENT_SECCOMP, data);
422 /*
423 * The delivery of a fatal signal during event
424 * notification may silently skip tracer notification.
425 * Terminating the task now avoids executing a system
426 * call that may not be intended.
427 */
428 if (fatal_signal_pending(current))
429 break;
Andy Lutomirski87b526d2012-10-01 11:40:45 -0700430 if (syscall_get_nr(current, regs) < 0)
431 goto skip; /* Explicit request to skip. */
432
Will Drewryfb0fadf2012-04-12 16:48:02 -0500433 return 0;
Will Drewryacf3b2c2012-04-12 16:47:59 -0500434 case SECCOMP_RET_ALLOW:
435 return 0;
436 case SECCOMP_RET_KILL:
437 default:
438 break;
439 }
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500440 exit_sig = SIGSYS;
441 break;
Will Drewry8156b452012-04-17 14:48:58 -0500442 }
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500443#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 default:
445 BUG();
446 }
447
448#ifdef SECCOMP_DEBUG
449 dump_stack();
450#endif
Will Drewryacf3b2c2012-04-12 16:47:59 -0500451 audit_seccomp(this_syscall, exit_sig, ret);
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500452 do_exit(exit_sig);
Will Drewry8156b452012-04-17 14:48:58 -0500453#ifdef CONFIG_SECCOMP_FILTER
Will Drewryacf3b2c2012-04-12 16:47:59 -0500454skip:
455 audit_seccomp(this_syscall, exit_sig, ret);
Will Drewry8156b452012-04-17 14:48:58 -0500456#endif
Will Drewryacf3b2c2012-04-12 16:47:59 -0500457 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700459
460long prctl_get_seccomp(void)
461{
462 return current->seccomp.mode;
463}
464
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500465/**
466 * prctl_set_seccomp: configures current->seccomp.mode
467 * @seccomp_mode: requested mode to use
468 * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
469 *
470 * This function may be called repeatedly with a @seccomp_mode of
471 * SECCOMP_MODE_FILTER to install additional filters. Every filter
472 * successfully installed will be evaluated (in reverse order) for each system
473 * call the task makes.
474 *
475 * Once current->seccomp.mode is non-zero, it may not be changed.
476 *
477 * Returns 0 on success or -EINVAL on failure.
478 */
479long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700480{
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500481 long ret = -EINVAL;
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700482
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500483 if (current->seccomp.mode &&
484 current->seccomp.mode != seccomp_mode)
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700485 goto out;
486
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500487 switch (seccomp_mode) {
488 case SECCOMP_MODE_STRICT:
489 ret = 0;
Andrea Arcangelicf99aba2007-07-15 23:41:33 -0700490#ifdef TIF_NOTSC
491 disable_TSC();
492#endif
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500493 break;
494#ifdef CONFIG_SECCOMP_FILTER
495 case SECCOMP_MODE_FILTER:
496 ret = seccomp_attach_user_filter(filter);
497 if (ret)
498 goto out;
499 break;
500#endif
501 default:
502 goto out;
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700503 }
504
Will Drewrye2cfabdf2012-04-12 16:47:57 -0500505 current->seccomp.mode = seccomp_mode;
506 set_thread_flag(TIF_SECCOMP);
507out:
Andrea Arcangeli1d9d02f2007-07-15 23:41:32 -0700508 return ret;
509}