seccomp: split mode setting routines
[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
22 /* #define SECCOMP_DEBUG 1 */
23
24 #ifdef CONFIG_SECCOMP_FILTER
25 #include <asm/syscall.h>
26 #include <linux/filter.h>
27 #include <linux/ptrace.h>
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  */
54 struct seccomp_filter {
55         atomic_t usage;
56         struct seccomp_filter *prev;
57         unsigned short len;  /* Instruction count */
58         struct sock_filter insns[];
59 };
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
64 /**
65  * get_u32 - returns a u32 offset into data
66  * @data: a unsigned 64 bit value
67  * @index: 0 or 1 to return the first or second 32-bits
68  *
69  * This inline exists to hide the length of unsigned long.  If a 32-bit
70  * unsigned long is passed in, it will be extended and the top 32-bits will be
71  * 0. If it is a 64-bit unsigned long, then whatever data is resident will be
72  * properly returned.
73  *
74  * Endianness is explicitly ignored and left for BPF program authors to manage
75  * as per the specific architecture.
76  */
77 static inline u32 get_u32(u64 data, int index)
78 {
79         return ((u32 *)&data)[index];
80 }
81
82 /* Helper for bpf_load below. */
83 #define BPF_DATA(_name) offsetof(struct seccomp_data, _name)
84 /**
85  * bpf_load: checks and returns a pointer to the requested offset
86  * @off: offset into struct seccomp_data to load from
87  *
88  * Returns the requested 32-bits of data.
89  * seccomp_check_filter() should assure that @off is 32-bit aligned
90  * and not out of bounds.  Failure to do so is a BUG.
91  */
92 u32 seccomp_bpf_load(int off)
93 {
94         struct pt_regs *regs = task_pt_regs(current);
95         if (off == BPF_DATA(nr))
96                 return syscall_get_nr(current, regs);
97         if (off == BPF_DATA(arch))
98                 return syscall_get_arch();
99         if (off >= BPF_DATA(args[0]) && off < BPF_DATA(args[6])) {
100                 unsigned long value;
101                 int arg = (off - BPF_DATA(args[0])) / sizeof(u64);
102                 int index = !!(off % sizeof(u64));
103                 syscall_get_arguments(current, regs, arg, 1, &value);
104                 return get_u32(value, index);
105         }
106         if (off == BPF_DATA(instruction_pointer))
107                 return get_u32(KSTK_EIP(current), 0);
108         if (off == BPF_DATA(instruction_pointer) + sizeof(u32))
109                 return get_u32(KSTK_EIP(current), 1);
110         /* seccomp_check_filter should make this impossible. */
111         BUG();
112 }
113
114 /**
115  *      seccomp_check_filter - verify seccomp filter code
116  *      @filter: filter to verify
117  *      @flen: length of filter
118  *
119  * Takes a previously checked filter (by sk_chk_filter) and
120  * redirects all filter code that loads struct sk_buff data
121  * and related data through seccomp_bpf_load.  It also
122  * enforces length and alignment checking of those loads.
123  *
124  * Returns 0 if the rule set is legal or -EINVAL if not.
125  */
126 static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
127 {
128         int pc;
129         for (pc = 0; pc < flen; pc++) {
130                 struct sock_filter *ftest = &filter[pc];
131                 u16 code = ftest->code;
132                 u32 k = ftest->k;
133
134                 switch (code) {
135                 case BPF_S_LD_W_ABS:
136                         ftest->code = BPF_S_ANC_SECCOMP_LD_W;
137                         /* 32-bit aligned and not out of bounds. */
138                         if (k >= sizeof(struct seccomp_data) || k & 3)
139                                 return -EINVAL;
140                         continue;
141                 case BPF_S_LD_W_LEN:
142                         ftest->code = BPF_S_LD_IMM;
143                         ftest->k = sizeof(struct seccomp_data);
144                         continue;
145                 case BPF_S_LDX_W_LEN:
146                         ftest->code = BPF_S_LDX_IMM;
147                         ftest->k = sizeof(struct seccomp_data);
148                         continue;
149                 /* Explicitly include allowed calls. */
150                 case BPF_S_RET_K:
151                 case BPF_S_RET_A:
152                 case BPF_S_ALU_ADD_K:
153                 case BPF_S_ALU_ADD_X:
154                 case BPF_S_ALU_SUB_K:
155                 case BPF_S_ALU_SUB_X:
156                 case BPF_S_ALU_MUL_K:
157                 case BPF_S_ALU_MUL_X:
158                 case BPF_S_ALU_DIV_X:
159                 case BPF_S_ALU_AND_K:
160                 case BPF_S_ALU_AND_X:
161                 case BPF_S_ALU_OR_K:
162                 case BPF_S_ALU_OR_X:
163                 case BPF_S_ALU_XOR_K:
164                 case BPF_S_ALU_XOR_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  */
202 static u32 seccomp_run_filters(int syscall)
203 {
204         struct seccomp_filter *f;
205         u32 ret = SECCOMP_RET_ALLOW;
206
207         /* Ensure unexpected behavior doesn't result in failing open. */
208         if (WARN_ON(current->seccomp.filter == NULL))
209                 return SECCOMP_RET_KILL;
210
211         /*
212          * All filters in the list are evaluated and the lowest BPF return
213          * value always takes priority (ignoring the DATA).
214          */
215         for (f = current->seccomp.filter; f; f = f->prev) {
216                 u32 cur_ret = sk_run_filter(NULL, f->insns);
217                 if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
218                         ret = cur_ret;
219         }
220         return ret;
221 }
222 #endif /* CONFIG_SECCOMP_FILTER */
223
224 static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
225 {
226         if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
227                 return false;
228
229         return true;
230 }
231
232 static inline void seccomp_assign_mode(unsigned long seccomp_mode)
233 {
234         current->seccomp.mode = seccomp_mode;
235         set_tsk_thread_flag(current, TIF_SECCOMP);
236 }
237
238 #ifdef CONFIG_SECCOMP_FILTER
239 /**
240  * seccomp_attach_filter: Attaches a seccomp filter to current.
241  * @fprog: BPF program to install
242  *
243  * Returns 0 on success or an errno on failure.
244  */
245 static long seccomp_attach_filter(struct sock_fprog *fprog)
246 {
247         struct seccomp_filter *filter;
248         unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
249         unsigned long total_insns = fprog->len;
250         long ret;
251
252         if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
253                 return -EINVAL;
254
255         for (filter = current->seccomp.filter; filter; filter = filter->prev)
256                 total_insns += filter->len + 4;  /* include a 4 instr penalty */
257         if (total_insns > MAX_INSNS_PER_PATH)
258                 return -ENOMEM;
259
260         /*
261          * Installing a seccomp filter requires that the task have
262          * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
263          * This avoids scenarios where unprivileged tasks can affect the
264          * behavior of privileged children.
265          */
266         if (!current->no_new_privs &&
267             security_capable_noaudit(current_cred(), current_user_ns(),
268                                      CAP_SYS_ADMIN) != 0)
269                 return -EACCES;
270
271         /* Allocate a new seccomp_filter */
272         filter = kzalloc(sizeof(struct seccomp_filter) + fp_size,
273                          GFP_KERNEL|__GFP_NOWARN);
274         if (!filter)
275                 return -ENOMEM;
276         atomic_set(&filter->usage, 1);
277         filter->len = fprog->len;
278
279         /* Copy the instructions from fprog. */
280         ret = -EFAULT;
281         if (copy_from_user(filter->insns, fprog->filter, fp_size))
282                 goto fail;
283
284         /* Check and rewrite the fprog via the skb checker */
285         ret = sk_chk_filter(filter->insns, filter->len);
286         if (ret)
287                 goto fail;
288
289         /* Check and rewrite the fprog for seccomp use */
290         ret = seccomp_check_filter(filter->insns, filter->len);
291         if (ret)
292                 goto fail;
293
294         /*
295          * If there is an existing filter, make it the prev and don't drop its
296          * task reference.
297          */
298         filter->prev = current->seccomp.filter;
299         current->seccomp.filter = filter;
300         return 0;
301 fail:
302         kfree(filter);
303         return ret;
304 }
305
306 /**
307  * seccomp_attach_user_filter - attaches a user-supplied sock_fprog
308  * @user_filter: pointer to the user data containing a sock_fprog.
309  *
310  * Returns 0 on success and non-zero otherwise.
311  */
312 long seccomp_attach_user_filter(char __user *user_filter)
313 {
314         struct sock_fprog fprog;
315         long ret = -EFAULT;
316
317 #ifdef CONFIG_COMPAT
318         if (is_compat_task()) {
319                 struct compat_sock_fprog fprog32;
320                 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
321                         goto out;
322                 fprog.len = fprog32.len;
323                 fprog.filter = compat_ptr(fprog32.filter);
324         } else /* falls through to the if below. */
325 #endif
326         if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
327                 goto out;
328         ret = seccomp_attach_filter(&fprog);
329 out:
330         return ret;
331 }
332
333 /* get_seccomp_filter - increments the reference count of the filter on @tsk */
334 void get_seccomp_filter(struct task_struct *tsk)
335 {
336         struct seccomp_filter *orig = tsk->seccomp.filter;
337         if (!orig)
338                 return;
339         /* Reference count is bounded by the number of total processes. */
340         atomic_inc(&orig->usage);
341 }
342
343 /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
344 void put_seccomp_filter(struct task_struct *tsk)
345 {
346         struct seccomp_filter *orig = tsk->seccomp.filter;
347         /* Clean up single-reference branches iteratively. */
348         while (orig && atomic_dec_and_test(&orig->usage)) {
349                 struct seccomp_filter *freeme = orig;
350                 orig = orig->prev;
351                 kfree(freeme);
352         }
353 }
354
355 /**
356  * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
357  * @syscall: syscall number to send to userland
358  * @reason: filter-supplied reason code to send to userland (via si_errno)
359  *
360  * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
361  */
362 static void seccomp_send_sigsys(int syscall, int reason)
363 {
364         struct siginfo info;
365         memset(&info, 0, sizeof(info));
366         info.si_signo = SIGSYS;
367         info.si_code = SYS_SECCOMP;
368         info.si_call_addr = (void __user *)KSTK_EIP(current);
369         info.si_errno = reason;
370         info.si_arch = syscall_get_arch();
371         info.si_syscall = syscall;
372         force_sig_info(SIGSYS, &info, current);
373 }
374 #endif  /* CONFIG_SECCOMP_FILTER */
375
376 /*
377  * Secure computing mode 1 allows only read/write/exit/sigreturn.
378  * To be fully secure this must be combined with rlimit
379  * to limit the stack allocations too.
380  */
381 static int mode1_syscalls[] = {
382         __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
383         0, /* null terminated */
384 };
385
386 #ifdef CONFIG_COMPAT
387 static int mode1_syscalls_32[] = {
388         __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
389         0, /* null terminated */
390 };
391 #endif
392
393 int __secure_computing(int this_syscall)
394 {
395         int mode = current->seccomp.mode;
396         int exit_sig = 0;
397         int *syscall;
398         u32 ret;
399
400         switch (mode) {
401         case SECCOMP_MODE_STRICT:
402                 syscall = mode1_syscalls;
403 #ifdef CONFIG_COMPAT
404                 if (is_compat_task())
405                         syscall = mode1_syscalls_32;
406 #endif
407                 do {
408                         if (*syscall == this_syscall)
409                                 return 0;
410                 } while (*++syscall);
411                 exit_sig = SIGKILL;
412                 ret = SECCOMP_RET_KILL;
413                 break;
414 #ifdef CONFIG_SECCOMP_FILTER
415         case SECCOMP_MODE_FILTER: {
416                 int data;
417                 struct pt_regs *regs = task_pt_regs(current);
418                 ret = seccomp_run_filters(this_syscall);
419                 data = ret & SECCOMP_RET_DATA;
420                 ret &= SECCOMP_RET_ACTION;
421                 switch (ret) {
422                 case SECCOMP_RET_ERRNO:
423                         /* Set the low-order 16-bits as a errno. */
424                         syscall_set_return_value(current, regs,
425                                                  -data, 0);
426                         goto skip;
427                 case SECCOMP_RET_TRAP:
428                         /* Show the handler the original registers. */
429                         syscall_rollback(current, regs);
430                         /* Let the filter pass back 16 bits of data. */
431                         seccomp_send_sigsys(this_syscall, data);
432                         goto skip;
433                 case SECCOMP_RET_TRACE:
434                         /* Skip these calls if there is no tracer. */
435                         if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
436                                 syscall_set_return_value(current, regs,
437                                                          -ENOSYS, 0);
438                                 goto skip;
439                         }
440                         /* Allow the BPF to provide the event message */
441                         ptrace_event(PTRACE_EVENT_SECCOMP, data);
442                         /*
443                          * The delivery of a fatal signal during event
444                          * notification may silently skip tracer notification.
445                          * Terminating the task now avoids executing a system
446                          * call that may not be intended.
447                          */
448                         if (fatal_signal_pending(current))
449                                 break;
450                         if (syscall_get_nr(current, regs) < 0)
451                                 goto skip;  /* Explicit request to skip. */
452
453                         return 0;
454                 case SECCOMP_RET_ALLOW:
455                         return 0;
456                 case SECCOMP_RET_KILL:
457                 default:
458                         break;
459                 }
460                 exit_sig = SIGSYS;
461                 break;
462         }
463 #endif
464         default:
465                 BUG();
466         }
467
468 #ifdef SECCOMP_DEBUG
469         dump_stack();
470 #endif
471         audit_seccomp(this_syscall, exit_sig, ret);
472         do_exit(exit_sig);
473 #ifdef CONFIG_SECCOMP_FILTER
474 skip:
475         audit_seccomp(this_syscall, exit_sig, ret);
476 #endif
477         return -1;
478 }
479
480 long prctl_get_seccomp(void)
481 {
482         return current->seccomp.mode;
483 }
484
485 /**
486  * seccomp_set_mode_strict: internal function for setting strict seccomp
487  *
488  * Once current->seccomp.mode is non-zero, it may not be changed.
489  *
490  * Returns 0 on success or -EINVAL on failure.
491  */
492 static long seccomp_set_mode_strict(void)
493 {
494         const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
495         long ret = -EINVAL;
496
497         if (!seccomp_may_assign_mode(seccomp_mode))
498                 goto out;
499
500 #ifdef TIF_NOTSC
501         disable_TSC();
502 #endif
503         seccomp_assign_mode(seccomp_mode);
504         ret = 0;
505
506 out:
507
508         return ret;
509 }
510
511 #ifdef CONFIG_SECCOMP_FILTER
512 /**
513  * seccomp_set_mode_filter: internal function for setting seccomp filter
514  * @filter: struct sock_fprog containing filter
515  *
516  * This function may be called repeatedly to install additional filters.
517  * Every filter successfully installed will be evaluated (in reverse order)
518  * for each system call the task makes.
519  *
520  * Once current->seccomp.mode is non-zero, it may not be changed.
521  *
522  * Returns 0 on success or -EINVAL on failure.
523  */
524 static long seccomp_set_mode_filter(char __user *filter)
525 {
526         const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
527         long ret = -EINVAL;
528
529         if (!seccomp_may_assign_mode(seccomp_mode))
530                 goto out;
531
532         ret = seccomp_attach_user_filter(filter);
533         if (ret)
534                 goto out;
535
536         seccomp_assign_mode(seccomp_mode);
537 out:
538         return ret;
539 }
540 #else
541 static inline long seccomp_set_mode_filter(char __user *filter)
542 {
543         return -EINVAL;
544 }
545 #endif
546
547 /**
548  * prctl_set_seccomp: configures current->seccomp.mode
549  * @seccomp_mode: requested mode to use
550  * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
551  *
552  * Returns 0 on success or -EINVAL on failure.
553  */
554 long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
555 {
556         switch (seccomp_mode) {
557         case SECCOMP_MODE_STRICT:
558                 return seccomp_set_mode_strict();
559         case SECCOMP_MODE_FILTER:
560                 return seccomp_set_mode_filter(filter);
561         default:
562                 return -EINVAL;
563         }
564 }