GNU Linux-libre 4.9.337-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 int __bpf_prog_charge(struct user_struct *user, u32 pages)
585 {
586         unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
587         unsigned long user_bufs;
588
589         if (user) {
590                 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
591                 if (user_bufs > memlock_limit) {
592                         atomic_long_sub(pages, &user->locked_vm);
593                         return -EPERM;
594                 }
595         }
596
597         return 0;
598 }
599
600 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
601 {
602         if (user)
603                 atomic_long_sub(pages, &user->locked_vm);
604 }
605
606 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
607 {
608         struct user_struct *user = get_current_user();
609         int ret;
610
611         ret = __bpf_prog_charge(user, prog->pages);
612         if (ret) {
613                 free_uid(user);
614                 return ret;
615         }
616
617         prog->aux->user = user;
618         return 0;
619 }
620
621 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
622 {
623         struct user_struct *user = prog->aux->user;
624
625         __bpf_prog_uncharge(user, prog->pages);
626         free_uid(user);
627 }
628
629 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
630 {
631         struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
632
633         free_used_maps(aux);
634         bpf_prog_uncharge_memlock(aux->prog);
635         bpf_prog_free(aux->prog);
636 }
637
638 void bpf_prog_put(struct bpf_prog *prog)
639 {
640         if (atomic_dec_and_test(&prog->aux->refcnt))
641                 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
642 }
643 EXPORT_SYMBOL_GPL(bpf_prog_put);
644
645 static int bpf_prog_release(struct inode *inode, struct file *filp)
646 {
647         struct bpf_prog *prog = filp->private_data;
648
649         bpf_prog_put(prog);
650         return 0;
651 }
652
653 static const struct file_operations bpf_prog_fops = {
654         .release = bpf_prog_release,
655 };
656
657 int bpf_prog_new_fd(struct bpf_prog *prog)
658 {
659         return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
660                                 O_RDWR | O_CLOEXEC);
661 }
662
663 static struct bpf_prog *____bpf_prog_get(struct fd f)
664 {
665         if (!f.file)
666                 return ERR_PTR(-EBADF);
667         if (f.file->f_op != &bpf_prog_fops) {
668                 fdput(f);
669                 return ERR_PTR(-EINVAL);
670         }
671
672         return f.file->private_data;
673 }
674
675 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
676 {
677         if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
678                 atomic_sub(i, &prog->aux->refcnt);
679                 return ERR_PTR(-EBUSY);
680         }
681         return prog;
682 }
683 EXPORT_SYMBOL_GPL(bpf_prog_add);
684
685 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
686 {
687         return bpf_prog_add(prog, 1);
688 }
689
690 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
691 {
692         struct fd f = fdget(ufd);
693         struct bpf_prog *prog;
694
695         prog = ____bpf_prog_get(f);
696         if (IS_ERR(prog))
697                 return prog;
698         if (type && prog->type != *type) {
699                 prog = ERR_PTR(-EINVAL);
700                 goto out;
701         }
702
703         prog = bpf_prog_inc(prog);
704 out:
705         fdput(f);
706         return prog;
707 }
708
709 struct bpf_prog *bpf_prog_get(u32 ufd)
710 {
711         return __bpf_prog_get(ufd, NULL);
712 }
713
714 struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
715 {
716         return __bpf_prog_get(ufd, &type);
717 }
718 EXPORT_SYMBOL_GPL(bpf_prog_get_type);
719
720 /* last field in 'union bpf_attr' used by this command */
721 #define BPF_PROG_LOAD_LAST_FIELD kern_version
722
723 static int bpf_prog_load(union bpf_attr *attr)
724 {
725         enum bpf_prog_type type = attr->prog_type;
726         struct bpf_prog *prog;
727         int err;
728         char license[128];
729         bool is_gpl;
730
731         if (CHECK_ATTR(BPF_PROG_LOAD))
732                 return -EINVAL;
733
734         /* copy eBPF program license from user space */
735         if (strncpy_from_user(license, u64_to_ptr(attr->license),
736                               sizeof(license) - 1) < 0)
737                 return -EFAULT;
738         license[sizeof(license) - 1] = 0;
739
740         /* eBPF programs must be GPL compatible to use GPL-ed functions */
741         is_gpl = license_is_gpl_compatible(license);
742
743         if (attr->insn_cnt >= BPF_MAXINSNS)
744                 return -EINVAL;
745
746         if (type == BPF_PROG_TYPE_KPROBE &&
747             attr->kern_version != LINUX_VERSION_CODE)
748                 return -EINVAL;
749
750         if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
751                 return -EPERM;
752
753         /* plain bpf_prog allocation */
754         prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
755         if (!prog)
756                 return -ENOMEM;
757
758         err = bpf_prog_charge_memlock(prog);
759         if (err)
760                 goto free_prog_nouncharge;
761
762         prog->len = attr->insn_cnt;
763
764         err = -EFAULT;
765         if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
766                            prog->len * sizeof(struct bpf_insn)) != 0)
767                 goto free_prog;
768
769         prog->orig_prog = NULL;
770         prog->jited = 0;
771
772         atomic_set(&prog->aux->refcnt, 1);
773         prog->gpl_compatible = is_gpl ? 1 : 0;
774
775         /* find program type: socket_filter vs tracing_filter */
776         err = find_prog_type(type, prog);
777         if (err < 0)
778                 goto free_prog;
779
780         /* run eBPF verifier */
781         err = bpf_check(&prog, attr);
782         if (err < 0)
783                 goto free_used_maps;
784
785         /* eBPF program is ready to be JITed */
786         prog = bpf_prog_select_runtime(prog, &err);
787         if (err < 0)
788                 goto free_used_maps;
789
790         err = bpf_prog_new_fd(prog);
791         if (err < 0)
792                 /* failed to allocate fd */
793                 goto free_used_maps;
794
795         return err;
796
797 free_used_maps:
798         free_used_maps(prog->aux);
799 free_prog:
800         bpf_prog_uncharge_memlock(prog);
801 free_prog_nouncharge:
802         bpf_prog_free(prog);
803         return err;
804 }
805
806 #define BPF_OBJ_LAST_FIELD bpf_fd
807
808 static int bpf_obj_pin(const union bpf_attr *attr)
809 {
810         if (CHECK_ATTR(BPF_OBJ))
811                 return -EINVAL;
812
813         return bpf_obj_pin_user(attr->bpf_fd, u64_to_ptr(attr->pathname));
814 }
815
816 static int bpf_obj_get(const union bpf_attr *attr)
817 {
818         if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
819                 return -EINVAL;
820
821         return bpf_obj_get_user(u64_to_ptr(attr->pathname));
822 }
823
824 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
825 {
826         union bpf_attr attr;
827         int err;
828
829         if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
830                 return -EPERM;
831
832         if (!access_ok(VERIFY_READ, uattr, 1))
833                 return -EFAULT;
834
835         if (size > PAGE_SIZE)   /* silly large */
836                 return -E2BIG;
837
838         /* If we're handed a bigger struct than we know of,
839          * ensure all the unknown bits are 0 - i.e. new
840          * user-space does not rely on any kernel feature
841          * extensions we dont know about yet.
842          */
843         if (size > sizeof(attr)) {
844                 unsigned char __user *addr;
845                 unsigned char __user *end;
846                 unsigned char val;
847
848                 addr = (void __user *)uattr + sizeof(attr);
849                 end  = (void __user *)uattr + size;
850
851                 for (; addr < end; addr++) {
852                         err = get_user(val, addr);
853                         if (err)
854                                 return err;
855                         if (val)
856                                 return -E2BIG;
857                 }
858                 size = sizeof(attr);
859         }
860
861         /* copy attributes from user space, may be less than sizeof(bpf_attr) */
862         memset(&attr, 0, sizeof(attr));
863         if (copy_from_user(&attr, uattr, size) != 0)
864                 return -EFAULT;
865
866         switch (cmd) {
867         case BPF_MAP_CREATE:
868                 err = map_create(&attr);
869                 break;
870         case BPF_MAP_LOOKUP_ELEM:
871                 err = map_lookup_elem(&attr);
872                 break;
873         case BPF_MAP_UPDATE_ELEM:
874                 err = map_update_elem(&attr);
875                 break;
876         case BPF_MAP_DELETE_ELEM:
877                 err = map_delete_elem(&attr);
878                 break;
879         case BPF_MAP_GET_NEXT_KEY:
880                 err = map_get_next_key(&attr);
881                 break;
882         case BPF_PROG_LOAD:
883                 err = bpf_prog_load(&attr);
884                 break;
885         case BPF_OBJ_PIN:
886                 err = bpf_obj_pin(&attr);
887                 break;
888         case BPF_OBJ_GET:
889                 err = bpf_obj_get(&attr);
890                 break;
891         default:
892                 err = -EINVAL;
893                 break;
894         }
895
896         return err;
897 }