net: filter: add test for loading SKF_AD_OFF limits
[linux-block.git] / kernel / seccomp.c
CommitLineData
1da177e4
LT
1/*
2 * linux/kernel/seccomp.c
3 *
4 * Copyright 2004-2005 Andrea Arcangeli <andrea@cpushare.com>
5 *
e2cfabdf
WD
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.
1da177e4
LT
14 */
15
e2cfabdf 16#include <linux/atomic.h>
85e7bac3 17#include <linux/audit.h>
5b101740 18#include <linux/compat.h>
e2cfabdf
WD
19#include <linux/sched.h>
20#include <linux/seccomp.h>
1da177e4
LT
21
22/* #define SECCOMP_DEBUG 1 */
e2cfabdf
WD
23
24#ifdef CONFIG_SECCOMP_FILTER
25#include <asm/syscall.h>
26#include <linux/filter.h>
fb0fadf9 27#include <linux/ptrace.h>
e2cfabdf
WD
28#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;
8f577cad 57 struct sk_filter *prog;
e2cfabdf
WD
58};
59
60/* Limit any path through the tree to 256KB worth of instructions. */
61#define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
62
bd4cf0ed 63/*
e2cfabdf
WD
64 * Endianness is explicitly ignored and left for BPF program authors to manage
65 * as per the specific architecture.
66 */
bd4cf0ed 67static void populate_seccomp_data(struct seccomp_data *sd)
e2cfabdf 68{
bd4cf0ed
AS
69 struct task_struct *task = current;
70 struct pt_regs *regs = task_pt_regs(task);
2eac7648 71 unsigned long args[6];
e2cfabdf 72
bd4cf0ed 73 sd->nr = syscall_get_nr(task, regs);
0b747172 74 sd->arch = syscall_get_arch();
2eac7648
DB
75 syscall_get_arguments(task, regs, 0, 6, args);
76 sd->args[0] = args[0];
77 sd->args[1] = args[1];
78 sd->args[2] = args[2];
79 sd->args[3] = args[3];
80 sd->args[4] = args[4];
81 sd->args[5] = args[5];
bd4cf0ed 82 sd->instruction_pointer = KSTK_EIP(task);
e2cfabdf
WD
83}
84
85/**
86 * seccomp_check_filter - verify seccomp filter code
87 * @filter: filter to verify
88 * @flen: length of filter
89 *
90 * Takes a previously checked filter (by sk_chk_filter) and
91 * redirects all filter code that loads struct sk_buff data
92 * and related data through seccomp_bpf_load. It also
93 * enforces length and alignment checking of those loads.
94 *
95 * Returns 0 if the rule set is legal or -EINVAL if not.
96 */
97static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
98{
99 int pc;
100 for (pc = 0; pc < flen; pc++) {
101 struct sock_filter *ftest = &filter[pc];
102 u16 code = ftest->code;
103 u32 k = ftest->k;
104
105 switch (code) {
106 case BPF_S_LD_W_ABS:
bd4cf0ed 107 ftest->code = BPF_LDX | BPF_W | BPF_ABS;
e2cfabdf
WD
108 /* 32-bit aligned and not out of bounds. */
109 if (k >= sizeof(struct seccomp_data) || k & 3)
110 return -EINVAL;
111 continue;
112 case BPF_S_LD_W_LEN:
bd4cf0ed 113 ftest->code = BPF_LD | BPF_IMM;
e2cfabdf
WD
114 ftest->k = sizeof(struct seccomp_data);
115 continue;
116 case BPF_S_LDX_W_LEN:
bd4cf0ed 117 ftest->code = BPF_LDX | BPF_IMM;
e2cfabdf
WD
118 ftest->k = sizeof(struct seccomp_data);
119 continue;
120 /* Explicitly include allowed calls. */
121 case BPF_S_RET_K:
122 case BPF_S_RET_A:
123 case BPF_S_ALU_ADD_K:
124 case BPF_S_ALU_ADD_X:
125 case BPF_S_ALU_SUB_K:
126 case BPF_S_ALU_SUB_X:
127 case BPF_S_ALU_MUL_K:
128 case BPF_S_ALU_MUL_X:
129 case BPF_S_ALU_DIV_X:
130 case BPF_S_ALU_AND_K:
131 case BPF_S_ALU_AND_X:
132 case BPF_S_ALU_OR_K:
133 case BPF_S_ALU_OR_X:
d1327479
NS
134 case BPF_S_ALU_XOR_K:
135 case BPF_S_ALU_XOR_X:
e2cfabdf
WD
136 case BPF_S_ALU_LSH_K:
137 case BPF_S_ALU_LSH_X:
138 case BPF_S_ALU_RSH_K:
139 case BPF_S_ALU_RSH_X:
140 case BPF_S_ALU_NEG:
141 case BPF_S_LD_IMM:
142 case BPF_S_LDX_IMM:
143 case BPF_S_MISC_TAX:
144 case BPF_S_MISC_TXA:
145 case BPF_S_ALU_DIV_K:
146 case BPF_S_LD_MEM:
147 case BPF_S_LDX_MEM:
148 case BPF_S_ST:
149 case BPF_S_STX:
150 case BPF_S_JMP_JA:
151 case BPF_S_JMP_JEQ_K:
152 case BPF_S_JMP_JEQ_X:
153 case BPF_S_JMP_JGE_K:
154 case BPF_S_JMP_JGE_X:
155 case BPF_S_JMP_JGT_K:
156 case BPF_S_JMP_JGT_X:
157 case BPF_S_JMP_JSET_K:
158 case BPF_S_JMP_JSET_X:
bd4cf0ed 159 sk_decode_filter(ftest, ftest);
e2cfabdf
WD
160 continue;
161 default:
162 return -EINVAL;
163 }
164 }
165 return 0;
166}
167
168/**
169 * seccomp_run_filters - evaluates all seccomp filters against @syscall
170 * @syscall: number of the current system call
171 *
172 * Returns valid seccomp BPF response codes.
173 */
174static u32 seccomp_run_filters(int syscall)
175{
176 struct seccomp_filter *f;
bd4cf0ed 177 struct seccomp_data sd;
acf3b2c7
WD
178 u32 ret = SECCOMP_RET_ALLOW;
179
180 /* Ensure unexpected behavior doesn't result in failing open. */
181 if (WARN_ON(current->seccomp.filter == NULL))
182 return SECCOMP_RET_KILL;
183
bd4cf0ed
AS
184 populate_seccomp_data(&sd);
185
e2cfabdf
WD
186 /*
187 * All filters in the list are evaluated and the lowest BPF return
acf3b2c7 188 * value always takes priority (ignoring the DATA).
e2cfabdf
WD
189 */
190 for (f = current->seccomp.filter; f; f = f->prev) {
8f577cad
AS
191 u32 cur_ret = SK_RUN_FILTER(f->prog, (void *)&sd);
192
acf3b2c7
WD
193 if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
194 ret = cur_ret;
e2cfabdf
WD
195 }
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;
bd4cf0ed
AS
210 struct sock_filter *fp;
211 int new_len;
e2cfabdf
WD
212 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)
8f577cad 218 total_insns += filter->prog->len + 4; /* include a 4 instr penalty */
e2cfabdf
WD
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
bd4cf0ed
AS
233 fp = kzalloc(fp_size, GFP_KERNEL|__GFP_NOWARN);
234 if (!fp)
e2cfabdf 235 return -ENOMEM;
e2cfabdf
WD
236
237 /* Copy the instructions from fprog. */
238 ret = -EFAULT;
bd4cf0ed
AS
239 if (copy_from_user(fp, fprog->filter, fp_size))
240 goto free_prog;
e2cfabdf
WD
241
242 /* Check and rewrite the fprog via the skb checker */
bd4cf0ed 243 ret = sk_chk_filter(fp, fprog->len);
e2cfabdf 244 if (ret)
bd4cf0ed 245 goto free_prog;
e2cfabdf
WD
246
247 /* Check and rewrite the fprog for seccomp use */
bd4cf0ed
AS
248 ret = seccomp_check_filter(fp, fprog->len);
249 if (ret)
250 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 */
0acf07d2 258 ret = -ENOMEM;
8f577cad 259 filter = kzalloc(sizeof(struct seccomp_filter),
bd4cf0ed
AS
260 GFP_KERNEL|__GFP_NOWARN);
261 if (!filter)
262 goto free_prog;
263
8f577cad
AS
264 filter->prog = kzalloc(sk_filter_size(new_len),
265 GFP_KERNEL|__GFP_NOWARN);
266 if (!filter->prog)
bd4cf0ed 267 goto free_filter;
8f577cad
AS
268
269 ret = sk_convert_filter(fp, fprog->len, filter->prog->insnsi, &new_len);
270 if (ret)
271 goto free_filter_prog;
0acf07d2 272 kfree(fp);
bd4cf0ed
AS
273
274 atomic_set(&filter->usage, 1);
8f577cad 275 filter->prog->len = new_len;
8f577cad 276
5fe821a9 277 sk_filter_select_runtime(filter->prog);
e2cfabdf
WD
278
279 /*
280 * If there is an existing filter, make it the prev and don't drop its
281 * task reference.
282 */
283 filter->prev = current->seccomp.filter;
284 current->seccomp.filter = filter;
285 return 0;
bd4cf0ed 286
8f577cad
AS
287free_filter_prog:
288 kfree(filter->prog);
bd4cf0ed 289free_filter:
e2cfabdf 290 kfree(filter);
bd4cf0ed
AS
291free_prog:
292 kfree(fp);
e2cfabdf
WD
293 return ret;
294}
295
296/**
297 * seccomp_attach_user_filter - attaches a user-supplied sock_fprog
298 * @user_filter: pointer to the user data containing a sock_fprog.
299 *
300 * Returns 0 on success and non-zero otherwise.
301 */
864f32a5 302static long seccomp_attach_user_filter(char __user *user_filter)
e2cfabdf
WD
303{
304 struct sock_fprog fprog;
305 long ret = -EFAULT;
306
307#ifdef CONFIG_COMPAT
308 if (is_compat_task()) {
309 struct compat_sock_fprog fprog32;
310 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
311 goto out;
312 fprog.len = fprog32.len;
313 fprog.filter = compat_ptr(fprog32.filter);
314 } else /* falls through to the if below. */
315#endif
316 if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
317 goto out;
318 ret = seccomp_attach_filter(&fprog);
319out:
320 return ret;
321}
322
323/* get_seccomp_filter - increments the reference count of the filter on @tsk */
324void get_seccomp_filter(struct task_struct *tsk)
325{
326 struct seccomp_filter *orig = tsk->seccomp.filter;
327 if (!orig)
328 return;
329 /* Reference count is bounded by the number of total processes. */
330 atomic_inc(&orig->usage);
331}
332
333/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
334void put_seccomp_filter(struct task_struct *tsk)
335{
336 struct seccomp_filter *orig = tsk->seccomp.filter;
337 /* Clean up single-reference branches iteratively. */
338 while (orig && atomic_dec_and_test(&orig->usage)) {
339 struct seccomp_filter *freeme = orig;
340 orig = orig->prev;
5fe821a9 341 sk_filter_free(freeme->prog);
e2cfabdf
WD
342 kfree(freeme);
343 }
344}
bb6ea430
WD
345
346/**
347 * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
348 * @syscall: syscall number to send to userland
349 * @reason: filter-supplied reason code to send to userland (via si_errno)
350 *
351 * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
352 */
353static void seccomp_send_sigsys(int syscall, int reason)
354{
355 struct siginfo info;
356 memset(&info, 0, sizeof(info));
357 info.si_signo = SIGSYS;
358 info.si_code = SYS_SECCOMP;
359 info.si_call_addr = (void __user *)KSTK_EIP(current);
360 info.si_errno = reason;
5e937a9a 361 info.si_arch = syscall_get_arch();
bb6ea430
WD
362 info.si_syscall = syscall;
363 force_sig_info(SIGSYS, &info, current);
364}
e2cfabdf 365#endif /* CONFIG_SECCOMP_FILTER */
1da177e4
LT
366
367/*
368 * Secure computing mode 1 allows only read/write/exit/sigreturn.
369 * To be fully secure this must be combined with rlimit
370 * to limit the stack allocations too.
371 */
372static int mode1_syscalls[] = {
373 __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
374 0, /* null terminated */
375};
376
5b101740 377#ifdef CONFIG_COMPAT
1da177e4
LT
378static int mode1_syscalls_32[] = {
379 __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
380 0, /* null terminated */
381};
382#endif
383
acf3b2c7 384int __secure_computing(int this_syscall)
1da177e4
LT
385{
386 int mode = current->seccomp.mode;
e2cfabdf
WD
387 int exit_sig = 0;
388 int *syscall;
8156b451 389 u32 ret;
1da177e4
LT
390
391 switch (mode) {
e2cfabdf 392 case SECCOMP_MODE_STRICT:
1da177e4 393 syscall = mode1_syscalls;
5b101740
RM
394#ifdef CONFIG_COMPAT
395 if (is_compat_task())
1da177e4
LT
396 syscall = mode1_syscalls_32;
397#endif
398 do {
399 if (*syscall == this_syscall)
acf3b2c7 400 return 0;
1da177e4 401 } while (*++syscall);
e2cfabdf 402 exit_sig = SIGKILL;
8156b451 403 ret = SECCOMP_RET_KILL;
1da177e4 404 break;
e2cfabdf 405#ifdef CONFIG_SECCOMP_FILTER
8156b451
WD
406 case SECCOMP_MODE_FILTER: {
407 int data;
87b526d3 408 struct pt_regs *regs = task_pt_regs(current);
acf3b2c7
WD
409 ret = seccomp_run_filters(this_syscall);
410 data = ret & SECCOMP_RET_DATA;
8156b451
WD
411 ret &= SECCOMP_RET_ACTION;
412 switch (ret) {
acf3b2c7
WD
413 case SECCOMP_RET_ERRNO:
414 /* Set the low-order 16-bits as a errno. */
87b526d3 415 syscall_set_return_value(current, regs,
acf3b2c7
WD
416 -data, 0);
417 goto skip;
bb6ea430
WD
418 case SECCOMP_RET_TRAP:
419 /* Show the handler the original registers. */
87b526d3 420 syscall_rollback(current, regs);
bb6ea430
WD
421 /* Let the filter pass back 16 bits of data. */
422 seccomp_send_sigsys(this_syscall, data);
423 goto skip;
fb0fadf9
WD
424 case SECCOMP_RET_TRACE:
425 /* Skip these calls if there is no tracer. */
87b526d3
AL
426 if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
427 syscall_set_return_value(current, regs,
428 -ENOSYS, 0);
fb0fadf9 429 goto skip;
87b526d3 430 }
fb0fadf9
WD
431 /* Allow the BPF to provide the event message */
432 ptrace_event(PTRACE_EVENT_SECCOMP, data);
433 /*
434 * The delivery of a fatal signal during event
435 * notification may silently skip tracer notification.
436 * Terminating the task now avoids executing a system
437 * call that may not be intended.
438 */
439 if (fatal_signal_pending(current))
440 break;
87b526d3
AL
441 if (syscall_get_nr(current, regs) < 0)
442 goto skip; /* Explicit request to skip. */
443
fb0fadf9 444 return 0;
acf3b2c7
WD
445 case SECCOMP_RET_ALLOW:
446 return 0;
447 case SECCOMP_RET_KILL:
448 default:
449 break;
450 }
e2cfabdf
WD
451 exit_sig = SIGSYS;
452 break;
8156b451 453 }
e2cfabdf 454#endif
1da177e4
LT
455 default:
456 BUG();
457 }
458
459#ifdef SECCOMP_DEBUG
460 dump_stack();
461#endif
acf3b2c7 462 audit_seccomp(this_syscall, exit_sig, ret);
e2cfabdf 463 do_exit(exit_sig);
8156b451 464#ifdef CONFIG_SECCOMP_FILTER
acf3b2c7
WD
465skip:
466 audit_seccomp(this_syscall, exit_sig, ret);
8156b451 467#endif
acf3b2c7 468 return -1;
1da177e4 469}
1d9d02fe
AA
470
471long prctl_get_seccomp(void)
472{
473 return current->seccomp.mode;
474}
475
e2cfabdf
WD
476/**
477 * prctl_set_seccomp: configures current->seccomp.mode
478 * @seccomp_mode: requested mode to use
479 * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
480 *
481 * This function may be called repeatedly with a @seccomp_mode of
482 * SECCOMP_MODE_FILTER to install additional filters. Every filter
483 * successfully installed will be evaluated (in reverse order) for each system
484 * call the task makes.
485 *
486 * Once current->seccomp.mode is non-zero, it may not be changed.
487 *
488 * Returns 0 on success or -EINVAL on failure.
489 */
490long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
1d9d02fe 491{
e2cfabdf 492 long ret = -EINVAL;
1d9d02fe 493
e2cfabdf
WD
494 if (current->seccomp.mode &&
495 current->seccomp.mode != seccomp_mode)
1d9d02fe
AA
496 goto out;
497
e2cfabdf
WD
498 switch (seccomp_mode) {
499 case SECCOMP_MODE_STRICT:
500 ret = 0;
cf99abac
AA
501#ifdef TIF_NOTSC
502 disable_TSC();
503#endif
e2cfabdf
WD
504 break;
505#ifdef CONFIG_SECCOMP_FILTER
506 case SECCOMP_MODE_FILTER:
507 ret = seccomp_attach_user_filter(filter);
508 if (ret)
509 goto out;
510 break;
511#endif
512 default:
513 goto out;
1d9d02fe
AA
514 }
515
e2cfabdf
WD
516 current->seccomp.mode = seccomp_mode;
517 set_thread_flag(TIF_SECCOMP);
518out:
1d9d02fe
AA
519 return ret;
520}