]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - kernel/seccomp.c
x86/speculation: Provide IBPB always command line options
[thirdparty/kernel/stable.git] / kernel / seccomp.c
1 /*
2 * linux/kernel/seccomp.c
3 *
4 * Copyright 2004-2005 Andrea Arcangeli <andrea@cpushare.com>
5 *
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.
14 */
15
16 #include <linux/atomic.h>
17 #include <linux/audit.h>
18 #include <linux/compat.h>
19 #include <linux/nospec.h>
20 #include <linux/prctl.h>
21 #include <linux/sched.h>
22 #include <linux/seccomp.h>
23 #include <linux/syscalls.h>
24
25 /* #define SECCOMP_DEBUG 1 */
26
27 #ifdef CONFIG_SECCOMP_FILTER
28 #include <asm/syscall.h>
29 #include <linux/filter.h>
30 #include <linux/ptrace.h>
31 #include <linux/security.h>
32 #include <linux/slab.h>
33 #include <linux/tracehook.h>
34 #include <linux/uaccess.h>
35
36 /**
37 * struct seccomp_filter - container for seccomp BPF programs
38 *
39 * @usage: reference count to manage the object lifetime.
40 * get/put helpers should be used when accessing an instance
41 * outside of a lifetime-guarded section. In general, this
42 * is only needed for handling filters shared across tasks.
43 * @prev: points to a previously installed, or inherited, filter
44 * @len: the number of instructions in the program
45 * @insnsi: the BPF program instructions to evaluate
46 *
47 * seccomp_filter objects are organized in a tree linked via the @prev
48 * pointer. For any task, it appears to be a singly-linked list starting
49 * with current->seccomp.filter, the most recently attached or inherited filter.
50 * However, multiple filters may share a @prev node, by way of fork(), which
51 * results in a unidirectional tree existing in memory. This is similar to
52 * how namespaces work.
53 *
54 * seccomp_filter objects should never be modified after being attached
55 * to a task_struct (other than @usage).
56 */
57 struct seccomp_filter {
58 atomic_t usage;
59 struct seccomp_filter *prev;
60 struct sk_filter *prog;
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
66 /*
67 * Endianness is explicitly ignored and left for BPF program authors to manage
68 * as per the specific architecture.
69 */
70 static void populate_seccomp_data(struct seccomp_data *sd)
71 {
72 struct task_struct *task = current;
73 struct pt_regs *regs = task_pt_regs(task);
74 unsigned long args[6];
75
76 sd->nr = syscall_get_nr(task, regs);
77 sd->arch = syscall_get_arch();
78 syscall_get_arguments(task, regs, 0, 6, args);
79 sd->args[0] = args[0];
80 sd->args[1] = args[1];
81 sd->args[2] = args[2];
82 sd->args[3] = args[3];
83 sd->args[4] = args[4];
84 sd->args[5] = args[5];
85 sd->instruction_pointer = KSTK_EIP(task);
86 }
87
88 /**
89 * seccomp_check_filter - verify seccomp filter code
90 * @filter: filter to verify
91 * @flen: length of filter
92 *
93 * Takes a previously checked filter (by sk_chk_filter) and
94 * redirects all filter code that loads struct sk_buff data
95 * and related data through seccomp_bpf_load. It also
96 * enforces length and alignment checking of those loads.
97 *
98 * Returns 0 if the rule set is legal or -EINVAL if not.
99 */
100 static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
101 {
102 int pc;
103 for (pc = 0; pc < flen; pc++) {
104 struct sock_filter *ftest = &filter[pc];
105 u16 code = ftest->code;
106 u32 k = ftest->k;
107
108 switch (code) {
109 case BPF_LD | BPF_W | BPF_ABS:
110 ftest->code = BPF_LDX | BPF_W | BPF_ABS;
111 /* 32-bit aligned and not out of bounds. */
112 if (k >= sizeof(struct seccomp_data) || k & 3)
113 return -EINVAL;
114 continue;
115 case BPF_LD | BPF_W | BPF_LEN:
116 ftest->code = BPF_LD | BPF_IMM;
117 ftest->k = sizeof(struct seccomp_data);
118 continue;
119 case BPF_LDX | BPF_W | BPF_LEN:
120 ftest->code = BPF_LDX | BPF_IMM;
121 ftest->k = sizeof(struct seccomp_data);
122 continue;
123 /* Explicitly include allowed calls. */
124 case BPF_RET | BPF_K:
125 case BPF_RET | BPF_A:
126 case BPF_ALU | BPF_ADD | BPF_K:
127 case BPF_ALU | BPF_ADD | BPF_X:
128 case BPF_ALU | BPF_SUB | BPF_K:
129 case BPF_ALU | BPF_SUB | BPF_X:
130 case BPF_ALU | BPF_MUL | BPF_K:
131 case BPF_ALU | BPF_MUL | BPF_X:
132 case BPF_ALU | BPF_DIV | BPF_K:
133 case BPF_ALU | BPF_DIV | BPF_X:
134 case BPF_ALU | BPF_AND | BPF_K:
135 case BPF_ALU | BPF_AND | BPF_X:
136 case BPF_ALU | BPF_OR | BPF_K:
137 case BPF_ALU | BPF_OR | BPF_X:
138 case BPF_ALU | BPF_XOR | BPF_K:
139 case BPF_ALU | BPF_XOR | BPF_X:
140 case BPF_ALU | BPF_LSH | BPF_K:
141 case BPF_ALU | BPF_LSH | BPF_X:
142 case BPF_ALU | BPF_RSH | BPF_K:
143 case BPF_ALU | BPF_RSH | BPF_X:
144 case BPF_ALU | BPF_NEG:
145 case BPF_LD | BPF_IMM:
146 case BPF_LDX | BPF_IMM:
147 case BPF_MISC | BPF_TAX:
148 case BPF_MISC | BPF_TXA:
149 case BPF_LD | BPF_MEM:
150 case BPF_LDX | BPF_MEM:
151 case BPF_ST:
152 case BPF_STX:
153 case BPF_JMP | BPF_JA:
154 case BPF_JMP | BPF_JEQ | BPF_K:
155 case BPF_JMP | BPF_JEQ | BPF_X:
156 case BPF_JMP | BPF_JGE | BPF_K:
157 case BPF_JMP | BPF_JGE | BPF_X:
158 case BPF_JMP | BPF_JGT | BPF_K:
159 case BPF_JMP | BPF_JGT | BPF_X:
160 case BPF_JMP | BPF_JSET | BPF_K:
161 case BPF_JMP | BPF_JSET | BPF_X:
162 continue;
163 default:
164 return -EINVAL;
165 }
166 }
167 return 0;
168 }
169
170 /**
171 * seccomp_run_filters - evaluates all seccomp filters against @syscall
172 * @syscall: number of the current system call
173 *
174 * Returns valid seccomp BPF response codes.
175 */
176 static u32 seccomp_run_filters(int syscall)
177 {
178 struct seccomp_filter *f;
179 struct seccomp_data sd;
180 u32 ret = SECCOMP_RET_ALLOW;
181
182 /* Ensure unexpected behavior doesn't result in failing open. */
183 if (WARN_ON(current->seccomp.filter == NULL))
184 return SECCOMP_RET_KILL;
185
186 populate_seccomp_data(&sd);
187
188 /*
189 * All filters in the list are evaluated and the lowest BPF return
190 * value always takes priority (ignoring the DATA).
191 */
192 for (f = current->seccomp.filter; f; f = f->prev) {
193 u32 cur_ret = SK_RUN_FILTER(f->prog, (void *)&sd);
194
195 if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
196 ret = cur_ret;
197 }
198 return ret;
199 }
200 #endif /* CONFIG_SECCOMP_FILTER */
201
202 static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
203 {
204 if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
205 return false;
206
207 return true;
208 }
209
210 void __weak arch_seccomp_spec_mitigate(struct task_struct *task) { }
211
212 static inline void seccomp_assign_mode(unsigned long seccomp_mode,
213 unsigned long flags)
214 {
215 current->seccomp.mode = seccomp_mode;
216 /* Assume default seccomp processes want spec flaw mitigation. */
217 if ((flags & SECCOMP_FILTER_FLAG_SPEC_ALLOW) == 0)
218 arch_seccomp_spec_mitigate(current);
219 set_tsk_thread_flag(current, TIF_SECCOMP);
220 }
221
222 #ifdef CONFIG_SECCOMP_FILTER
223 /**
224 * seccomp_attach_filter: Attaches a seccomp filter to current.
225 * @fprog: BPF program to install
226 *
227 * Returns 0 on success or an errno on failure.
228 */
229 static long seccomp_attach_filter(struct sock_fprog *fprog)
230 {
231 struct seccomp_filter *filter;
232 unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
233 unsigned long total_insns = fprog->len;
234 struct sock_filter *fp;
235 int new_len;
236 long ret;
237
238 if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
239 return -EINVAL;
240
241 for (filter = current->seccomp.filter; filter; filter = filter->prev)
242 total_insns += filter->prog->len + 4; /* include a 4 instr penalty */
243 if (total_insns > MAX_INSNS_PER_PATH)
244 return -ENOMEM;
245
246 /*
247 * Installing a seccomp filter requires that the task has
248 * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
249 * This avoids scenarios where unprivileged tasks can affect the
250 * behavior of privileged children.
251 */
252 if (!task_no_new_privs(current) &&
253 security_capable_noaudit(current_cred(), current_user_ns(),
254 CAP_SYS_ADMIN) != 0)
255 return -EACCES;
256
257 fp = kzalloc(fp_size, GFP_KERNEL|__GFP_NOWARN);
258 if (!fp)
259 return -ENOMEM;
260
261 /* Copy the instructions from fprog. */
262 ret = -EFAULT;
263 if (copy_from_user(fp, fprog->filter, fp_size))
264 goto free_prog;
265
266 /* Check and rewrite the fprog via the skb checker */
267 ret = sk_chk_filter(fp, fprog->len);
268 if (ret)
269 goto free_prog;
270
271 /* Check and rewrite the fprog for seccomp use */
272 ret = seccomp_check_filter(fp, fprog->len);
273 if (ret)
274 goto free_prog;
275
276 /* Convert 'sock_filter' insns to 'sock_filter_int' insns */
277 ret = sk_convert_filter(fp, fprog->len, NULL, &new_len);
278 if (ret)
279 goto free_prog;
280
281 /* Allocate a new seccomp_filter */
282 ret = -ENOMEM;
283 filter = kzalloc(sizeof(struct seccomp_filter),
284 GFP_KERNEL|__GFP_NOWARN);
285 if (!filter)
286 goto free_prog;
287
288 filter->prog = kzalloc(sk_filter_size(new_len),
289 GFP_KERNEL|__GFP_NOWARN);
290 if (!filter->prog)
291 goto free_filter;
292
293 ret = sk_convert_filter(fp, fprog->len, filter->prog->insnsi, &new_len);
294 if (ret)
295 goto free_filter_prog;
296 kfree(fp);
297
298 atomic_set(&filter->usage, 1);
299 filter->prog->len = new_len;
300
301 sk_filter_select_runtime(filter->prog);
302
303 /*
304 * If there is an existing filter, make it the prev and don't drop its
305 * task reference.
306 */
307 filter->prev = current->seccomp.filter;
308 current->seccomp.filter = filter;
309 return 0;
310
311 free_filter_prog:
312 kfree(filter->prog);
313 free_filter:
314 kfree(filter);
315 free_prog:
316 kfree(fp);
317 return ret;
318 }
319
320 /**
321 * seccomp_attach_user_filter - attaches a user-supplied sock_fprog
322 * @user_filter: pointer to the user data containing a sock_fprog.
323 *
324 * Returns 0 on success and non-zero otherwise.
325 */
326 static long seccomp_attach_user_filter(const char __user *user_filter)
327 {
328 struct sock_fprog fprog;
329 long ret = -EFAULT;
330
331 #ifdef CONFIG_COMPAT
332 if (is_compat_task()) {
333 struct compat_sock_fprog fprog32;
334 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
335 goto out;
336 fprog.len = fprog32.len;
337 fprog.filter = compat_ptr(fprog32.filter);
338 } else /* falls through to the if below. */
339 #endif
340 if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
341 goto out;
342 ret = seccomp_attach_filter(&fprog);
343 out:
344 return ret;
345 }
346
347 /* get_seccomp_filter - increments the reference count of the filter on @tsk */
348 void get_seccomp_filter(struct task_struct *tsk)
349 {
350 struct seccomp_filter *orig = tsk->seccomp.filter;
351 if (!orig)
352 return;
353 /* Reference count is bounded by the number of total processes. */
354 atomic_inc(&orig->usage);
355 }
356
357 /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
358 void put_seccomp_filter(struct task_struct *tsk)
359 {
360 struct seccomp_filter *orig = tsk->seccomp.filter;
361 /* Clean up single-reference branches iteratively. */
362 while (orig && atomic_dec_and_test(&orig->usage)) {
363 struct seccomp_filter *freeme = orig;
364 orig = orig->prev;
365 sk_filter_free(freeme->prog);
366 kfree(freeme);
367 }
368 }
369
370 /**
371 * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
372 * @syscall: syscall number to send to userland
373 * @reason: filter-supplied reason code to send to userland (via si_errno)
374 *
375 * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
376 */
377 static void seccomp_send_sigsys(int syscall, int reason)
378 {
379 struct siginfo info;
380 memset(&info, 0, sizeof(info));
381 info.si_signo = SIGSYS;
382 info.si_code = SYS_SECCOMP;
383 info.si_call_addr = (void __user *)KSTK_EIP(current);
384 info.si_errno = reason;
385 info.si_arch = syscall_get_arch();
386 info.si_syscall = syscall;
387 force_sig_info(SIGSYS, &info, current);
388 }
389 #endif /* CONFIG_SECCOMP_FILTER */
390
391 /*
392 * Secure computing mode 1 allows only read/write/exit/sigreturn.
393 * To be fully secure this must be combined with rlimit
394 * to limit the stack allocations too.
395 */
396 static int mode1_syscalls[] = {
397 __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
398 0, /* null terminated */
399 };
400
401 #ifdef CONFIG_COMPAT
402 static int mode1_syscalls_32[] = {
403 __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
404 0, /* null terminated */
405 };
406 #endif
407
408 int __secure_computing(int this_syscall)
409 {
410 int mode = current->seccomp.mode;
411 int exit_sig = 0;
412 int *syscall;
413 u32 ret;
414
415 switch (mode) {
416 case SECCOMP_MODE_STRICT:
417 syscall = mode1_syscalls;
418 #ifdef CONFIG_COMPAT
419 if (is_compat_task())
420 syscall = mode1_syscalls_32;
421 #endif
422 do {
423 if (*syscall == this_syscall)
424 return 0;
425 } while (*++syscall);
426 exit_sig = SIGKILL;
427 ret = SECCOMP_RET_KILL;
428 break;
429 #ifdef CONFIG_SECCOMP_FILTER
430 case SECCOMP_MODE_FILTER: {
431 int data;
432 struct pt_regs *regs = task_pt_regs(current);
433 ret = seccomp_run_filters(this_syscall);
434 data = ret & SECCOMP_RET_DATA;
435 ret &= SECCOMP_RET_ACTION;
436 switch (ret) {
437 case SECCOMP_RET_ERRNO:
438 /* Set the low-order 16-bits as a errno. */
439 syscall_set_return_value(current, regs,
440 -data, 0);
441 goto skip;
442 case SECCOMP_RET_TRAP:
443 /* Show the handler the original registers. */
444 syscall_rollback(current, regs);
445 /* Let the filter pass back 16 bits of data. */
446 seccomp_send_sigsys(this_syscall, data);
447 goto skip;
448 case SECCOMP_RET_TRACE:
449 /* Skip these calls if there is no tracer. */
450 if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
451 syscall_set_return_value(current, regs,
452 -ENOSYS, 0);
453 goto skip;
454 }
455 /* Allow the BPF to provide the event message */
456 ptrace_event(PTRACE_EVENT_SECCOMP, data);
457 /*
458 * The delivery of a fatal signal during event
459 * notification may silently skip tracer notification.
460 * Terminating the task now avoids executing a system
461 * call that may not be intended.
462 */
463 if (fatal_signal_pending(current))
464 break;
465 if (syscall_get_nr(current, regs) < 0)
466 goto skip; /* Explicit request to skip. */
467
468 return 0;
469 case SECCOMP_RET_ALLOW:
470 return 0;
471 case SECCOMP_RET_KILL:
472 default:
473 break;
474 }
475 exit_sig = SIGSYS;
476 break;
477 }
478 #endif
479 default:
480 BUG();
481 }
482
483 #ifdef SECCOMP_DEBUG
484 dump_stack();
485 #endif
486 audit_seccomp(this_syscall, exit_sig, ret);
487 do_exit(exit_sig);
488 #ifdef CONFIG_SECCOMP_FILTER
489 skip:
490 audit_seccomp(this_syscall, exit_sig, ret);
491 #endif
492 return -1;
493 }
494
495 long prctl_get_seccomp(void)
496 {
497 return current->seccomp.mode;
498 }
499
500 /**
501 * seccomp_set_mode_strict: internal function for setting strict seccomp
502 *
503 * Once current->seccomp.mode is non-zero, it may not be changed.
504 *
505 * Returns 0 on success or -EINVAL on failure.
506 */
507 static long seccomp_set_mode_strict(void)
508 {
509 const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
510 long ret = -EINVAL;
511
512 if (!seccomp_may_assign_mode(seccomp_mode))
513 goto out;
514
515 #ifdef TIF_NOTSC
516 disable_TSC();
517 #endif
518 seccomp_assign_mode(seccomp_mode, 0);
519 ret = 0;
520
521 out:
522
523 return ret;
524 }
525
526 #ifdef CONFIG_SECCOMP_FILTER
527 /**
528 * seccomp_set_mode_filter: internal function for setting seccomp filter
529 * @flags: flags to change filter behavior
530 * @filter: struct sock_fprog containing filter
531 *
532 * This function may be called repeatedly to install additional filters.
533 * Every filter successfully installed will be evaluated (in reverse order)
534 * for each system call the task makes.
535 *
536 * Once current->seccomp.mode is non-zero, it may not be changed.
537 *
538 * Returns 0 on success or -EINVAL on failure.
539 */
540 static long seccomp_set_mode_filter(unsigned int flags,
541 const char __user *filter)
542 {
543 const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
544 long ret = -EINVAL;
545
546 /* Validate flags. */
547 if (flags & ~SECCOMP_FILTER_FLAG_MASK)
548 goto out;
549
550 if (!seccomp_may_assign_mode(seccomp_mode))
551 goto out;
552
553 ret = seccomp_attach_user_filter(filter);
554 if (ret)
555 goto out;
556
557 seccomp_assign_mode(seccomp_mode, flags);
558 out:
559 return ret;
560 }
561 #else
562 static inline long seccomp_set_mode_filter(unsigned int flags,
563 const char __user *filter)
564 {
565 return -EINVAL;
566 }
567 #endif
568
569 /* Common entry point for both prctl and syscall. */
570 static long do_seccomp(unsigned int op, unsigned int flags,
571 const char __user *uargs)
572 {
573 switch (op) {
574 case SECCOMP_SET_MODE_STRICT:
575 if (flags != 0 || uargs != NULL)
576 return -EINVAL;
577 return seccomp_set_mode_strict();
578 case SECCOMP_SET_MODE_FILTER:
579 return seccomp_set_mode_filter(flags, uargs);
580 default:
581 return -EINVAL;
582 }
583 }
584
585 SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
586 const char __user *, uargs)
587 {
588 return do_seccomp(op, flags, uargs);
589 }
590
591 /**
592 * prctl_set_seccomp: configures current->seccomp.mode
593 * @seccomp_mode: requested mode to use
594 * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
595 *
596 * Returns 0 on success or -EINVAL on failure.
597 */
598 long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
599 {
600 unsigned int op;
601 char __user *uargs;
602
603 switch (seccomp_mode) {
604 case SECCOMP_MODE_STRICT:
605 op = SECCOMP_SET_MODE_STRICT;
606 /*
607 * Setting strict mode through prctl always ignored filter,
608 * so make sure it is always NULL here to pass the internal
609 * check in do_seccomp().
610 */
611 uargs = NULL;
612 break;
613 case SECCOMP_MODE_FILTER:
614 op = SECCOMP_SET_MODE_FILTER;
615 uargs = filter;
616 break;
617 default:
618 return -EINVAL;
619 }
620
621 /* prctl interface doesn't have flags, so they are always zero. */
622 return do_seccomp(op, 0, uargs);
623 }