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