pch_gbe: Do not abort probe on bad MAC
[linux-flexiantxendom0.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/filter.h>
20 #include <linux/ptrace.h>
21 #include <linux/sched.h>
22 #include <linux/seccomp.h>
23 #include <linux/security.h>
24 #include <linux/slab.h>
25 #include <linux/uaccess.h>
26
27 #ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
28 #include <asm/syscall.h>
29 #endif
30
31 /* #define SECCOMP_DEBUG 1 */
32
33 #ifdef CONFIG_SECCOMP_FILTER
34 /**
35  * struct seccomp_filter - container for seccomp BPF programs
36  *
37  * @usage: reference count to manage the object liftime.
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.
71  * If a 32-bit unsigned long is passed in, it will be extended
72  * and the top 32-bits will be 0. If it is a 64-bit unsigned
73  * long, then whatever data is resident will be properly returned.
74  */
75 static inline u32 get_u32(u64 data, int index)
76 {
77         return ((u32 *)&data)[index];
78 }
79
80 /* Helper for bpf_load below. */
81 #define BPF_DATA(_name) offsetof(struct seccomp_data, _name)
82 /**
83  * bpf_load: checks and returns a pointer to the requested offset
84  * @off: offset into struct seccomp_data to load from
85  *
86  * Returns the requested 32-bits of data.
87  * seccomp_chk_filter() should assure that @off is 32-bit aligned
88  * and not out of bounds.  Failure to do so is a BUG.
89  */
90 u32 seccomp_bpf_load(int off)
91 {
92         struct pt_regs *regs = task_pt_regs(current);
93         if (off == BPF_DATA(nr))
94                 return syscall_get_nr(current, regs);
95         if (off == BPF_DATA(arch))
96                 return syscall_get_arch(current, regs);
97         if (off >= BPF_DATA(args[0]) && off < BPF_DATA(args[6])) {
98                 unsigned long value;
99                 int arg = (off - BPF_DATA(args[0])) / sizeof(u64);
100                 int index = !!(off % sizeof(u64));
101                 syscall_get_arguments(current, regs, arg, 1, &value);
102                 return get_u32(value, index);
103         }
104         if (off == BPF_DATA(instruction_pointer))
105                 return get_u32(KSTK_EIP(current), 0);
106         if (off == BPF_DATA(instruction_pointer) + sizeof(u32))
107                 return get_u32(KSTK_EIP(current), 1);
108         /* seccomp_chk_filter should make this impossible. */
109         BUG();
110 }
111
112 /**
113  *      seccomp_chk_filter - verify seccomp filter code
114  *      @filter: filter to verify
115  *      @flen: length of filter
116  *
117  * Takes a previously checked filter (by sk_chk_filter) and
118  * redirects all filter code that loads struct sk_buff data
119  * and related data through seccomp_bpf_load.  It also
120  * enforces length and alignment checking of those loads.
121  *
122  * Returns 0 if the rule set is legal or -EINVAL if not.
123  */
124 static int seccomp_chk_filter(struct sock_filter *filter, unsigned int flen)
125 {
126         int pc;
127         for (pc = 0; pc < flen; pc++) {
128                 struct sock_filter *ftest = &filter[pc];
129                 u16 code = ftest->code;
130                 u32 k = ftest->k;
131                 switch (code) {
132                 case BPF_S_LD_W_ABS:
133                         ftest->code = BPF_S_ANC_SECCOMP_LD_W;
134                         /* 32-bit aligned and not out of bounds. */
135                         if (k >= sizeof(struct seccomp_data) || k & 3)
136                                 return -EINVAL;
137                         continue;
138                 case BPF_S_LD_W_LEN:
139                         ftest->code = BPF_S_LD_IMM;
140                         ftest->k = sizeof(struct seccomp_data);
141                         continue;
142                 case BPF_S_LDX_W_LEN:
143                         ftest->code = BPF_S_LDX_IMM;
144                         ftest->k = sizeof(struct seccomp_data);
145                         continue;
146                 /* Explicitly include allowed calls. */
147                 case BPF_S_RET_K:
148                 case BPF_S_RET_A:
149                 case BPF_S_ALU_ADD_K:
150                 case BPF_S_ALU_ADD_X:
151                 case BPF_S_ALU_SUB_K:
152                 case BPF_S_ALU_SUB_X:
153                 case BPF_S_ALU_MUL_K:
154                 case BPF_S_ALU_MUL_X:
155                 case BPF_S_ALU_DIV_X:
156                 case BPF_S_ALU_AND_K:
157                 case BPF_S_ALU_AND_X:
158                 case BPF_S_ALU_OR_K:
159                 case BPF_S_ALU_OR_X:
160                 case BPF_S_ALU_LSH_K:
161                 case BPF_S_ALU_LSH_X:
162                 case BPF_S_ALU_RSH_K:
163                 case BPF_S_ALU_RSH_X:
164                 case BPF_S_ALU_NEG:
165                 case BPF_S_LD_IMM:
166                 case BPF_S_LDX_IMM:
167                 case BPF_S_MISC_TAX:
168                 case BPF_S_MISC_TXA:
169                 case BPF_S_ALU_DIV_K:
170                 case BPF_S_LD_MEM:
171                 case BPF_S_LDX_MEM:
172                 case BPF_S_ST:
173                 case BPF_S_STX:
174                 case BPF_S_JMP_JA:
175                 case BPF_S_JMP_JEQ_K:
176                 case BPF_S_JMP_JEQ_X:
177                 case BPF_S_JMP_JGE_K:
178                 case BPF_S_JMP_JGE_X:
179                 case BPF_S_JMP_JGT_K:
180                 case BPF_S_JMP_JGT_X:
181                 case BPF_S_JMP_JSET_K:
182                 case BPF_S_JMP_JSET_X:
183                         continue;
184                 default:
185                         return -EINVAL;
186                 }
187         }
188         return 0;
189 }
190
191 /**
192  * seccomp_run_filters - evaluates all seccomp filters against @syscall
193  * @syscall: number of the current system call
194  *
195  * Returns valid seccomp BPF response codes.
196  */
197 static u32 seccomp_run_filters(int syscall)
198 {
199         struct seccomp_filter *f;
200         u32 ret = SECCOMP_RET_ALLOW;
201
202         /* Ensure unexpected behavior doesn't result in failing open. */
203         if (WARN_ON(current->seccomp.filter == NULL))
204                 return SECCOMP_RET_KILL;
205
206         /*
207          * All filters are evaluated in order of youngest to oldest. The lowest
208          * BPF return value (ignoring the DATA) always takes priority.
209          */
210         for (f = current->seccomp.filter; f; f = f->prev) {
211                 u32 cur_ret = sk_run_filter(NULL, f->insns);
212                 if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
213                         ret = cur_ret;
214         }
215         return ret;
216 }
217
218 /**
219  * seccomp_attach_filter: Attaches a seccomp filter to current.
220  * @fprog: BPF program to install
221  *
222  * Returns 0 on success or an errno on failure.
223  */
224 static long seccomp_attach_filter(struct sock_fprog *fprog)
225 {
226         struct seccomp_filter *filter;
227         unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
228         unsigned long total_insns = fprog->len;
229         long ret;
230
231         if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
232                 return -EINVAL;
233
234         for (filter = current->seccomp.filter; filter; filter = filter->prev)
235                 total_insns += filter->len + 4;  /* include a 4 instr penalty */
236         if (total_insns > MAX_INSNS_PER_PATH)
237                 return -ENOMEM;
238
239         /*
240          * Installing a seccomp filter requires that the task have
241          * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
242          * This avoids scenarios where unprivileged tasks can affect the
243          * behavior of privileged children.
244          */
245         if (!current->no_new_privs &&
246             security_real_capable_noaudit(current, current_user_ns(),
247                                      CAP_SYS_ADMIN) != 0)
248                 return -EACCES;
249
250         /* Allocate a new seccomp_filter */
251         filter = kzalloc(sizeof(struct seccomp_filter) + fp_size, GFP_KERNEL);
252         if (!filter)
253                 return -ENOMEM;
254         atomic_set(&filter->usage, 1);
255         filter->len = fprog->len;
256
257         /* Copy the instructions from fprog. */
258         ret = -EFAULT;
259         if (copy_from_user(filter->insns, fprog->filter, fp_size))
260                 goto fail;
261
262         /* Check and rewrite the fprog via the skb checker */
263         ret = sk_chk_filter(filter->insns, filter->len);
264         if (ret)
265                 goto fail;
266
267         /* Check and rewrite the fprog for seccomp use */
268         ret = seccomp_chk_filter(filter->insns, filter->len);
269         if (ret)
270                 goto fail;
271
272         /*
273          * If there is an existing filter, make it the prev and don't drop its
274          * task reference.
275          */
276         filter->prev = current->seccomp.filter;
277         current->seccomp.filter = filter;
278         return 0;
279 fail:
280         kfree(filter);
281         return ret;
282 }
283
284 /**
285  * seccomp_attach_user_filter - attaches a user-supplied sock_fprog
286  * @user_filter: pointer to the user data containing a sock_fprog.
287  *
288  * Returns 0 on success and non-zero otherwise.
289  */
290 long seccomp_attach_user_filter(char __user *user_filter)
291 {
292         struct sock_fprog fprog;
293         long ret = -EFAULT;
294
295 #ifdef CONFIG_COMPAT
296         if (is_compat_task()) {
297                 struct compat_sock_fprog fprog32;
298                 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
299                         goto out;
300                 fprog.len = fprog32.len;
301                 fprog.filter = compat_ptr(fprog32.filter);
302         } else /* falls through to the if below. */
303 #endif
304         if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
305                 goto out;
306         ret = seccomp_attach_filter(&fprog);
307 out:
308         return ret;
309 }
310
311 /* get_seccomp_filter - increments the reference count of the filter on @tsk */
312 void get_seccomp_filter(struct task_struct *tsk)
313 {
314         struct seccomp_filter *orig = tsk->seccomp.filter;
315         if (!orig)
316                 return;
317         /* Reference count is bounded by the number of total processes. */
318         atomic_inc(&orig->usage);
319 }
320
321 /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
322 void put_seccomp_filter(struct task_struct *tsk)
323 {
324         struct seccomp_filter *orig = tsk->seccomp.filter;
325         /* Clean up single-reference branches iteratively. */
326         while (orig && atomic_dec_and_test(&orig->usage)) {
327                 struct seccomp_filter *freeme = orig;
328                 orig = orig->prev;
329                 kfree(freeme);
330         }
331 }
332
333 /**
334  * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
335  * @syscall: syscall number to send to userland
336  * @reason: filter-supplied reason code to send to userland (via si_errno)
337  *
338  * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
339  */
340 static void seccomp_send_sigsys(int syscall, int reason)
341 {
342         struct siginfo info;
343         memset(&info, 0, sizeof(info));
344         info.si_signo = SIGSYS;
345         info.si_code = SYS_SECCOMP;
346         info.si_call_addr = (void __user *)KSTK_EIP(current);
347         info.si_errno = reason;
348         info.si_arch = syscall_get_arch(current, task_pt_regs(current));
349         info.si_syscall = syscall;
350         force_sig_info(SIGSYS, &info, current);
351 }
352 #endif  /* CONFIG_SECCOMP_FILTER */
353
354 /*
355  * Secure computing mode 1 allows only read/write/exit/sigreturn.
356  * To be fully secure this must be combined with rlimit
357  * to limit the stack allocations too.
358  */
359 static int mode1_syscalls[] = {
360         __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
361         0, /* null terminated */
362 };
363
364 #ifdef CONFIG_COMPAT
365 static int mode1_syscalls_32[] = {
366         __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
367         0, /* null terminated */
368 };
369 #endif
370
371 void __secure_computing(int this_syscall)
372 {
373         /* Filter calls should never use this function. */
374         BUG_ON(current->seccomp.mode == SECCOMP_MODE_FILTER);
375         __secure_computing_int(this_syscall);
376 }
377
378 int __secure_computing_int(int this_syscall)
379 {
380         int mode = current->seccomp.mode;
381         int exit_sig = 0;
382         int *syscall;
383         u32 ret = SECCOMP_RET_KILL;
384         int data;
385
386         switch (mode) {
387         case SECCOMP_MODE_STRICT:
388                 syscall = mode1_syscalls;
389 #ifdef CONFIG_COMPAT
390                 if (is_compat_task())
391                         syscall = mode1_syscalls_32;
392 #endif
393                 do {
394                         if (*syscall == this_syscall)
395                                 return 0;
396                 } while (*++syscall);
397                 exit_sig = SIGKILL;
398                 break;
399 #ifdef CONFIG_SECCOMP_FILTER
400         case SECCOMP_MODE_FILTER:
401                 ret = seccomp_run_filters(this_syscall);
402                 data = ret & SECCOMP_RET_DATA;
403                 switch (ret & SECCOMP_RET_ACTION) {
404                 case SECCOMP_RET_ERRNO:
405                         /* Set the low-order 16-bits as a errno. */
406                         syscall_set_return_value(current, task_pt_regs(current),
407                                                  -data, 0);
408                         goto skip;
409                 case SECCOMP_RET_TRAP:
410                         /* Show the handler the original registers. */
411                         syscall_rollback(current, task_pt_regs(current));
412                         /* Let the filter pass back 16 bits of data. */
413                         seccomp_send_sigsys(this_syscall, data);
414                         goto skip;
415                 case SECCOMP_RET_TRACE:
416                         /* Skip these calls if there is no tracer. */
417                         if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP))
418                                 goto skip;
419                         /* Allow the BPF to provide the event message */
420                         ptrace_event(PTRACE_EVENT_SECCOMP, data);
421                         if (fatal_signal_pending(current))
422                                 break;
423                         return 0;
424                 case SECCOMP_RET_ALLOW:
425                         return 0;
426                 case SECCOMP_RET_KILL:
427                 default:
428                         break;
429                 }
430                 exit_sig = SIGSYS;
431                 break;
432 #endif
433         default:
434                 BUG();
435         }
436
437 #ifdef SECCOMP_DEBUG
438         dump_stack();
439 #endif
440         audit_seccomp(this_syscall, exit_sig, ret);
441         do_exit(exit_sig);
442 skip:
443         audit_seccomp(this_syscall, exit_sig, ret);
444         return -1;
445 }
446
447 long prctl_get_seccomp(void)
448 {
449         return current->seccomp.mode;
450 }
451
452 /**
453  * prctl_set_seccomp: configures current->seccomp.mode
454  * @seccomp_mode: requested mode to use
455  * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
456  *
457  * This function may be called repeatedly with a @seccomp_mode of
458  * SECCOMP_MODE_FILTER to install additional filters.  Every filter
459  * successfully installed will be evaluated (in reverse order) for each system
460  * call the task makes.
461  *
462  * Once current->seccomp.mode is non-zero, it may not be changed.
463  *
464  * Returns 0 on success or -EINVAL on failure.
465  */
466 long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
467 {
468         long ret = -EINVAL;
469
470         if (current->seccomp.mode &&
471             current->seccomp.mode != seccomp_mode)
472                 goto out;
473
474         switch (seccomp_mode) {
475         case SECCOMP_MODE_STRICT:
476                 ret = 0;
477 #ifdef TIF_NOTSC
478                 disable_TSC();
479 #endif
480                 break;
481 #ifdef CONFIG_SECCOMP_FILTER
482         case SECCOMP_MODE_FILTER:
483                 ret = seccomp_attach_user_filter(filter);
484                 if (ret)
485                         goto out;
486                 break;
487 #endif
488         default:
489                 goto out;
490         }
491
492         current->seccomp.mode = seccomp_mode;
493         set_thread_flag(TIF_SECCOMP);
494 out:
495         return ret;
496 }