GNU Linux-libre 4.9.309-gnu1
[releases.git] / kernel / bpf / syscall.c
1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of version 2 of the GNU General Public
5  * License as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * General Public License for more details.
11  */
12 #include <linux/bpf.h>
13 #include <linux/syscalls.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/mmzone.h>
17 #include <linux/anon_inodes.h>
18 #include <linux/file.h>
19 #include <linux/license.h>
20 #include <linux/filter.h>
21 #include <linux/version.h>
22
23 DEFINE_PER_CPU(int, bpf_prog_active);
24
25 int sysctl_unprivileged_bpf_disabled __read_mostly =
26         IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
27
28 static LIST_HEAD(bpf_map_types);
29
30 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
31 {
32         struct bpf_map_type_list *tl;
33         struct bpf_map *map;
34
35         list_for_each_entry(tl, &bpf_map_types, list_node) {
36                 if (tl->type == attr->map_type) {
37                         map = tl->ops->map_alloc(attr);
38                         if (IS_ERR(map))
39                                 return map;
40                         map->ops = tl->ops;
41                         map->map_type = attr->map_type;
42                         return map;
43                 }
44         }
45         return ERR_PTR(-EINVAL);
46 }
47
48 /* boot time registration of different map implementations */
49 void bpf_register_map_type(struct bpf_map_type_list *tl)
50 {
51         list_add(&tl->list_node, &bpf_map_types);
52 }
53
54 void *bpf_map_area_alloc(size_t size)
55 {
56         /* We definitely need __GFP_NORETRY, so OOM killer doesn't
57          * trigger under memory pressure as we really just want to
58          * fail instead.
59          */
60         const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
61         void *area;
62
63         if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
64                 area = kmalloc(size, GFP_USER | flags);
65                 if (area != NULL)
66                         return area;
67         }
68
69         return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | flags,
70                          PAGE_KERNEL);
71 }
72
73 void bpf_map_area_free(void *area)
74 {
75         kvfree(area);
76 }
77
78 int bpf_map_precharge_memlock(u32 pages)
79 {
80         struct user_struct *user = get_current_user();
81         unsigned long memlock_limit, cur;
82
83         memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
84         cur = atomic_long_read(&user->locked_vm);
85         free_uid(user);
86         if (cur + pages > memlock_limit)
87                 return -EPERM;
88         return 0;
89 }
90
91 static int bpf_map_charge_memlock(struct bpf_map *map)
92 {
93         struct user_struct *user = get_current_user();
94         unsigned long memlock_limit;
95
96         memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
97
98         atomic_long_add(map->pages, &user->locked_vm);
99
100         if (atomic_long_read(&user->locked_vm) > memlock_limit) {
101                 atomic_long_sub(map->pages, &user->locked_vm);
102                 free_uid(user);
103                 return -EPERM;
104         }
105         map->user = user;
106         return 0;
107 }
108
109 static void bpf_map_uncharge_memlock(struct bpf_map *map)
110 {
111         struct user_struct *user = map->user;
112
113         atomic_long_sub(map->pages, &user->locked_vm);
114         free_uid(user);
115 }
116
117 /* called from workqueue */
118 static void bpf_map_free_deferred(struct work_struct *work)
119 {
120         struct bpf_map *map = container_of(work, struct bpf_map, work);
121
122         bpf_map_uncharge_memlock(map);
123         /* implementation dependent freeing */
124         map->ops->map_free(map);
125 }
126
127 static void bpf_map_put_uref(struct bpf_map *map)
128 {
129         if (atomic_dec_and_test(&map->usercnt)) {
130                 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
131                         bpf_fd_array_map_clear(map);
132         }
133 }
134
135 /* decrement map refcnt and schedule it for freeing via workqueue
136  * (unrelying map implementation ops->map_free() might sleep)
137  */
138 void bpf_map_put(struct bpf_map *map)
139 {
140         if (atomic_dec_and_test(&map->refcnt)) {
141                 INIT_WORK(&map->work, bpf_map_free_deferred);
142                 schedule_work(&map->work);
143         }
144 }
145
146 void bpf_map_put_with_uref(struct bpf_map *map)
147 {
148         bpf_map_put_uref(map);
149         bpf_map_put(map);
150 }
151
152 static int bpf_map_release(struct inode *inode, struct file *filp)
153 {
154         struct bpf_map *map = filp->private_data;
155
156         if (map->ops->map_release)
157                 map->ops->map_release(map, filp);
158
159         bpf_map_put_with_uref(map);
160         return 0;
161 }
162
163 #ifdef CONFIG_PROC_FS
164 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
165 {
166         const struct bpf_map *map = filp->private_data;
167
168         seq_printf(m,
169                    "map_type:\t%u\n"
170                    "key_size:\t%u\n"
171                    "value_size:\t%u\n"
172                    "max_entries:\t%u\n"
173                    "map_flags:\t%#x\n",
174                    map->map_type,
175                    map->key_size,
176                    map->value_size,
177                    map->max_entries,
178                    map->map_flags);
179 }
180 #endif
181
182 static const struct file_operations bpf_map_fops = {
183 #ifdef CONFIG_PROC_FS
184         .show_fdinfo    = bpf_map_show_fdinfo,
185 #endif
186         .release        = bpf_map_release,
187 };
188
189 int bpf_map_new_fd(struct bpf_map *map)
190 {
191         return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
192                                 O_RDWR | O_CLOEXEC);
193 }
194
195 /* helper macro to check that unused fields 'union bpf_attr' are zero */
196 #define CHECK_ATTR(CMD) \
197         memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
198                    sizeof(attr->CMD##_LAST_FIELD), 0, \
199                    sizeof(*attr) - \
200                    offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
201                    sizeof(attr->CMD##_LAST_FIELD)) != NULL
202
203 #define BPF_MAP_CREATE_LAST_FIELD map_flags
204 /* called via syscall */
205 static int map_create(union bpf_attr *attr)
206 {
207         struct bpf_map *map;
208         int err;
209
210         err = CHECK_ATTR(BPF_MAP_CREATE);
211         if (err)
212                 return -EINVAL;
213
214         /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
215         map = find_and_alloc_map(attr);
216         if (IS_ERR(map))
217                 return PTR_ERR(map);
218
219         atomic_set(&map->refcnt, 1);
220         atomic_set(&map->usercnt, 1);
221
222         err = bpf_map_charge_memlock(map);
223         if (err)
224                 goto free_map_nouncharge;
225
226         err = bpf_map_new_fd(map);
227         if (err < 0)
228                 /* failed to allocate fd */
229                 goto free_map;
230
231         return err;
232
233 free_map:
234         bpf_map_uncharge_memlock(map);
235 free_map_nouncharge:
236         map->ops->map_free(map);
237         return err;
238 }
239
240 /* if error is returned, fd is released.
241  * On success caller should complete fd access with matching fdput()
242  */
243 struct bpf_map *__bpf_map_get(struct fd f)
244 {
245         if (!f.file)
246                 return ERR_PTR(-EBADF);
247         if (f.file->f_op != &bpf_map_fops) {
248                 fdput(f);
249                 return ERR_PTR(-EINVAL);
250         }
251
252         return f.file->private_data;
253 }
254
255 /* prog's and map's refcnt limit */
256 #define BPF_MAX_REFCNT 32768
257
258 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
259 {
260         if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
261                 atomic_dec(&map->refcnt);
262                 return ERR_PTR(-EBUSY);
263         }
264         if (uref)
265                 atomic_inc(&map->usercnt);
266         return map;
267 }
268
269 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
270 {
271         struct fd f = fdget(ufd);
272         struct bpf_map *map;
273
274         map = __bpf_map_get(f);
275         if (IS_ERR(map))
276                 return map;
277
278         map = bpf_map_inc(map, true);
279         fdput(f);
280
281         return map;
282 }
283
284 /* helper to convert user pointers passed inside __aligned_u64 fields */
285 static void __user *u64_to_ptr(__u64 val)
286 {
287         return (void __user *) (unsigned long) val;
288 }
289
290 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
291 {
292         return -ENOTSUPP;
293 }
294
295 /* last field in 'union bpf_attr' used by this command */
296 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
297
298 static int map_lookup_elem(union bpf_attr *attr)
299 {
300         void __user *ukey = u64_to_ptr(attr->key);
301         void __user *uvalue = u64_to_ptr(attr->value);
302         int ufd = attr->map_fd;
303         struct bpf_map *map;
304         void *key, *value, *ptr;
305         u32 value_size;
306         struct fd f;
307         int err;
308
309         if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
310                 return -EINVAL;
311
312         f = fdget(ufd);
313         map = __bpf_map_get(f);
314         if (IS_ERR(map))
315                 return PTR_ERR(map);
316
317         err = -ENOMEM;
318         key = kmalloc(map->key_size, GFP_USER);
319         if (!key)
320                 goto err_put;
321
322         err = -EFAULT;
323         if (copy_from_user(key, ukey, map->key_size) != 0)
324                 goto free_key;
325
326         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
327             map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
328                 value_size = round_up(map->value_size, 8) * num_possible_cpus();
329         else
330                 value_size = map->value_size;
331
332         err = -ENOMEM;
333         value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
334         if (!value)
335                 goto free_key;
336
337         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
338                 err = bpf_percpu_hash_copy(map, key, value);
339         } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
340                 err = bpf_percpu_array_copy(map, key, value);
341         } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
342                 err = bpf_stackmap_copy(map, key, value);
343         } else {
344                 rcu_read_lock();
345                 ptr = map->ops->map_lookup_elem(map, key);
346                 if (ptr)
347                         memcpy(value, ptr, value_size);
348                 rcu_read_unlock();
349                 err = ptr ? 0 : -ENOENT;
350         }
351
352         if (err)
353                 goto free_value;
354
355         err = -EFAULT;
356         if (copy_to_user(uvalue, value, value_size) != 0)
357                 goto free_value;
358
359         err = 0;
360
361 free_value:
362         kfree(value);
363 free_key:
364         kfree(key);
365 err_put:
366         fdput(f);
367         return err;
368 }
369
370 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
371
372 static int map_update_elem(union bpf_attr *attr)
373 {
374         void __user *ukey = u64_to_ptr(attr->key);
375         void __user *uvalue = u64_to_ptr(attr->value);
376         int ufd = attr->map_fd;
377         struct bpf_map *map;
378         void *key, *value;
379         u32 value_size;
380         struct fd f;
381         int err;
382
383         if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
384                 return -EINVAL;
385
386         f = fdget(ufd);
387         map = __bpf_map_get(f);
388         if (IS_ERR(map))
389                 return PTR_ERR(map);
390
391         err = -ENOMEM;
392         key = kmalloc(map->key_size, GFP_USER);
393         if (!key)
394                 goto err_put;
395
396         err = -EFAULT;
397         if (copy_from_user(key, ukey, map->key_size) != 0)
398                 goto free_key;
399
400         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
401             map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
402                 value_size = round_up(map->value_size, 8) * num_possible_cpus();
403         else
404                 value_size = map->value_size;
405
406         err = -ENOMEM;
407         value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
408         if (!value)
409                 goto free_key;
410
411         err = -EFAULT;
412         if (copy_from_user(value, uvalue, value_size) != 0)
413                 goto free_value;
414
415         /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
416          * inside bpf map update or delete otherwise deadlocks are possible
417          */
418         preempt_disable();
419         __this_cpu_inc(bpf_prog_active);
420         if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
421                 err = bpf_percpu_hash_update(map, key, value, attr->flags);
422         } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
423                 err = bpf_percpu_array_update(map, key, value, attr->flags);
424         } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
425                    map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
426                    map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY) {
427                 rcu_read_lock();
428                 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
429                                                    attr->flags);
430                 rcu_read_unlock();
431         } else {
432                 rcu_read_lock();
433                 err = map->ops->map_update_elem(map, key, value, attr->flags);
434                 rcu_read_unlock();
435         }
436         __this_cpu_dec(bpf_prog_active);
437         preempt_enable();
438
439 free_value:
440         kfree(value);
441 free_key:
442         kfree(key);
443 err_put:
444         fdput(f);
445         return err;
446 }
447
448 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
449
450 static int map_delete_elem(union bpf_attr *attr)
451 {
452         void __user *ukey = u64_to_ptr(attr->key);
453         int ufd = attr->map_fd;
454         struct bpf_map *map;
455         struct fd f;
456         void *key;
457         int err;
458
459         if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
460                 return -EINVAL;
461
462         f = fdget(ufd);
463         map = __bpf_map_get(f);
464         if (IS_ERR(map))
465                 return PTR_ERR(map);
466
467         err = -ENOMEM;
468         key = kmalloc(map->key_size, GFP_USER);
469         if (!key)
470                 goto err_put;
471
472         err = -EFAULT;
473         if (copy_from_user(key, ukey, map->key_size) != 0)
474                 goto free_key;
475
476         preempt_disable();
477         __this_cpu_inc(bpf_prog_active);
478         rcu_read_lock();
479         err = map->ops->map_delete_elem(map, key);
480         rcu_read_unlock();
481         __this_cpu_dec(bpf_prog_active);
482         preempt_enable();
483
484 free_key:
485         kfree(key);
486 err_put:
487         fdput(f);
488         return err;
489 }
490
491 /* last field in 'union bpf_attr' used by this command */
492 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
493
494 static int map_get_next_key(union bpf_attr *attr)
495 {
496         void __user *ukey = u64_to_ptr(attr->key);
497         void __user *unext_key = u64_to_ptr(attr->next_key);
498         int ufd = attr->map_fd;
499         struct bpf_map *map;
500         void *key, *next_key;
501         struct fd f;
502         int err;
503
504         if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
505                 return -EINVAL;
506
507         f = fdget(ufd);
508         map = __bpf_map_get(f);
509         if (IS_ERR(map))
510                 return PTR_ERR(map);
511
512         if (ukey) {
513                 err = -ENOMEM;
514                 key = kmalloc(map->key_size, GFP_USER);
515                 if (!key)
516                         goto err_put;
517
518                 err = -EFAULT;
519                 if (copy_from_user(key, ukey, map->key_size) != 0)
520                         goto free_key;
521         } else {
522                 key = NULL;
523         }
524
525         err = -ENOMEM;
526         next_key = kmalloc(map->key_size, GFP_USER);
527         if (!next_key)
528                 goto free_key;
529
530         rcu_read_lock();
531         err = map->ops->map_get_next_key(map, key, next_key);
532         rcu_read_unlock();
533         if (err)
534                 goto free_next_key;
535
536         err = -EFAULT;
537         if (copy_to_user(unext_key, next_key, map->key_size) != 0)
538                 goto free_next_key;
539
540         err = 0;
541
542 free_next_key:
543         kfree(next_key);
544 free_key:
545         kfree(key);
546 err_put:
547         fdput(f);
548         return err;
549 }
550
551 static LIST_HEAD(bpf_prog_types);
552
553 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
554 {
555         struct bpf_prog_type_list *tl;
556
557         list_for_each_entry(tl, &bpf_prog_types, list_node) {
558                 if (tl->type == type) {
559                         prog->aux->ops = tl->ops;
560                         prog->type = type;
561                         return 0;
562                 }
563         }
564
565         return -EINVAL;
566 }
567
568 void bpf_register_prog_type(struct bpf_prog_type_list *tl)
569 {
570         list_add(&tl->list_node, &bpf_prog_types);
571 }
572
573 /* drop refcnt on maps used by eBPF program and free auxilary data */
574 static void free_used_maps(struct bpf_prog_aux *aux)
575 {
576         int i;
577
578         for (i = 0; i < aux->used_map_cnt; i++)
579                 bpf_map_put(aux->used_maps[i]);
580
581         kfree(aux->used_maps);
582 }
583
584 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
585 {
586         struct user_struct *user = get_current_user();
587         unsigned long memlock_limit;
588
589         memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
590
591         atomic_long_add(prog->pages, &user->locked_vm);
592         if (atomic_long_read(&user->locked_vm) > memlock_limit) {
593                 atomic_long_sub(prog->pages, &user->locked_vm);
594                 free_uid(user);
595                 return -EPERM;
596         }
597         prog->aux->user = user;
598         return 0;
599 }
600
601 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
602 {
603         struct user_struct *user = prog->aux->user;
604
605         atomic_long_sub(prog->pages, &user->locked_vm);
606         free_uid(user);
607 }
608
609 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
610 {
611         struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
612
613         free_used_maps(aux);
614         bpf_prog_uncharge_memlock(aux->prog);
615         bpf_prog_free(aux->prog);
616 }
617
618 void bpf_prog_put(struct bpf_prog *prog)
619 {
620         if (atomic_dec_and_test(&prog->aux->refcnt))
621                 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
622 }
623 EXPORT_SYMBOL_GPL(bpf_prog_put);
624
625 static int bpf_prog_release(struct inode *inode, struct file *filp)
626 {
627         struct bpf_prog *prog = filp->private_data;
628
629         bpf_prog_put(prog);
630         return 0;
631 }
632
633 static const struct file_operations bpf_prog_fops = {
634         .release = bpf_prog_release,
635 };
636
637 int bpf_prog_new_fd(struct bpf_prog *prog)
638 {
639         return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
640                                 O_RDWR | O_CLOEXEC);
641 }
642
643 static struct bpf_prog *____bpf_prog_get(struct fd f)
644 {
645         if (!f.file)
646                 return ERR_PTR(-EBADF);
647         if (f.file->f_op != &bpf_prog_fops) {
648                 fdput(f);
649                 return ERR_PTR(-EINVAL);
650         }
651
652         return f.file->private_data;
653 }
654
655 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
656 {
657         if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
658                 atomic_sub(i, &prog->aux->refcnt);
659                 return ERR_PTR(-EBUSY);
660         }
661         return prog;
662 }
663 EXPORT_SYMBOL_GPL(bpf_prog_add);
664
665 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
666 {
667         return bpf_prog_add(prog, 1);
668 }
669
670 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
671 {
672         struct fd f = fdget(ufd);
673         struct bpf_prog *prog;
674
675         prog = ____bpf_prog_get(f);
676         if (IS_ERR(prog))
677                 return prog;
678         if (type && prog->type != *type) {
679                 prog = ERR_PTR(-EINVAL);
680                 goto out;
681         }
682
683         prog = bpf_prog_inc(prog);
684 out:
685         fdput(f);
686         return prog;
687 }
688
689 struct bpf_prog *bpf_prog_get(u32 ufd)
690 {
691         return __bpf_prog_get(ufd, NULL);
692 }
693
694 struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
695 {
696         return __bpf_prog_get(ufd, &type);
697 }
698 EXPORT_SYMBOL_GPL(bpf_prog_get_type);
699
700 /* last field in 'union bpf_attr' used by this command */
701 #define BPF_PROG_LOAD_LAST_FIELD kern_version
702
703 static int bpf_prog_load(union bpf_attr *attr)
704 {
705         enum bpf_prog_type type = attr->prog_type;
706         struct bpf_prog *prog;
707         int err;
708         char license[128];
709         bool is_gpl;
710
711         if (CHECK_ATTR(BPF_PROG_LOAD))
712                 return -EINVAL;
713
714         /* copy eBPF program license from user space */
715         if (strncpy_from_user(license, u64_to_ptr(attr->license),
716                               sizeof(license) - 1) < 0)
717                 return -EFAULT;
718         license[sizeof(license) - 1] = 0;
719
720         /* eBPF programs must be GPL compatible to use GPL-ed functions */
721         is_gpl = license_is_gpl_compatible(license);
722
723         if (attr->insn_cnt >= BPF_MAXINSNS)
724                 return -EINVAL;
725
726         if (type == BPF_PROG_TYPE_KPROBE &&
727             attr->kern_version != LINUX_VERSION_CODE)
728                 return -EINVAL;
729
730         if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
731                 return -EPERM;
732
733         /* plain bpf_prog allocation */
734         prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
735         if (!prog)
736                 return -ENOMEM;
737
738         err = bpf_prog_charge_memlock(prog);
739         if (err)
740                 goto free_prog_nouncharge;
741
742         prog->len = attr->insn_cnt;
743
744         err = -EFAULT;
745         if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
746                            prog->len * sizeof(struct bpf_insn)) != 0)
747                 goto free_prog;
748
749         prog->orig_prog = NULL;
750         prog->jited = 0;
751
752         atomic_set(&prog->aux->refcnt, 1);
753         prog->gpl_compatible = is_gpl ? 1 : 0;
754
755         /* find program type: socket_filter vs tracing_filter */
756         err = find_prog_type(type, prog);
757         if (err < 0)
758                 goto free_prog;
759
760         /* run eBPF verifier */
761         err = bpf_check(&prog, attr);
762         if (err < 0)
763                 goto free_used_maps;
764
765         /* eBPF program is ready to be JITed */
766         prog = bpf_prog_select_runtime(prog, &err);
767         if (err < 0)
768                 goto free_used_maps;
769
770         err = bpf_prog_new_fd(prog);
771         if (err < 0)
772                 /* failed to allocate fd */
773                 goto free_used_maps;
774
775         return err;
776
777 free_used_maps:
778         free_used_maps(prog->aux);
779 free_prog:
780         bpf_prog_uncharge_memlock(prog);
781 free_prog_nouncharge:
782         bpf_prog_free(prog);
783         return err;
784 }
785
786 #define BPF_OBJ_LAST_FIELD bpf_fd
787
788 static int bpf_obj_pin(const union bpf_attr *attr)
789 {
790         if (CHECK_ATTR(BPF_OBJ))
791                 return -EINVAL;
792
793         return bpf_obj_pin_user(attr->bpf_fd, u64_to_ptr(attr->pathname));
794 }
795
796 static int bpf_obj_get(const union bpf_attr *attr)
797 {
798         if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
799                 return -EINVAL;
800
801         return bpf_obj_get_user(u64_to_ptr(attr->pathname));
802 }
803
804 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
805 {
806         union bpf_attr attr;
807         int err;
808
809         if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
810                 return -EPERM;
811
812         if (!access_ok(VERIFY_READ, uattr, 1))
813                 return -EFAULT;
814
815         if (size > PAGE_SIZE)   /* silly large */
816                 return -E2BIG;
817
818         /* If we're handed a bigger struct than we know of,
819          * ensure all the unknown bits are 0 - i.e. new
820          * user-space does not rely on any kernel feature
821          * extensions we dont know about yet.
822          */
823         if (size > sizeof(attr)) {
824                 unsigned char __user *addr;
825                 unsigned char __user *end;
826                 unsigned char val;
827
828                 addr = (void __user *)uattr + sizeof(attr);
829                 end  = (void __user *)uattr + size;
830
831                 for (; addr < end; addr++) {
832                         err = get_user(val, addr);
833                         if (err)
834                                 return err;
835                         if (val)
836                                 return -E2BIG;
837                 }
838                 size = sizeof(attr);
839         }
840
841         /* copy attributes from user space, may be less than sizeof(bpf_attr) */
842         memset(&attr, 0, sizeof(attr));
843         if (copy_from_user(&attr, uattr, size) != 0)
844                 return -EFAULT;
845
846         switch (cmd) {
847         case BPF_MAP_CREATE:
848                 err = map_create(&attr);
849                 break;
850         case BPF_MAP_LOOKUP_ELEM:
851                 err = map_lookup_elem(&attr);
852                 break;
853         case BPF_MAP_UPDATE_ELEM:
854                 err = map_update_elem(&attr);
855                 break;
856         case BPF_MAP_DELETE_ELEM:
857                 err = map_delete_elem(&attr);
858                 break;
859         case BPF_MAP_GET_NEXT_KEY:
860                 err = map_get_next_key(&attr);
861                 break;
862         case BPF_PROG_LOAD:
863                 err = bpf_prog_load(&attr);
864                 break;
865         case BPF_OBJ_PIN:
866                 err = bpf_obj_pin(&attr);
867                 break;
868         case BPF_OBJ_GET:
869                 err = bpf_obj_get(&attr);
870                 break;
871         default:
872                 err = -EINVAL;
873                 break;
874         }
875
876         return err;
877 }