1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
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.
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.
12 #include <linux/bpf.h>
13 #include <linux/bpf_trace.h>
14 #include <linux/syscalls.h>
15 #include <linux/slab.h>
16 #include <linux/sched/signal.h>
17 #include <linux/vmalloc.h>
18 #include <linux/mmzone.h>
19 #include <linux/anon_inodes.h>
20 #include <linux/file.h>
21 #include <linux/license.h>
22 #include <linux/filter.h>
23 #include <linux/version.h>
24 #include <linux/kernel.h>
25 #include <linux/idr.h>
27 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
28 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
29 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
31 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
32 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
34 DEFINE_PER_CPU(int, bpf_prog_active);
35 static DEFINE_IDR(prog_idr);
36 static DEFINE_SPINLOCK(prog_idr_lock);
37 static DEFINE_IDR(map_idr);
38 static DEFINE_SPINLOCK(map_idr_lock);
40 int sysctl_unprivileged_bpf_disabled __read_mostly;
42 static const struct bpf_map_ops * const bpf_map_types[] = {
43 #define BPF_PROG_TYPE(_id, _ops)
44 #define BPF_MAP_TYPE(_id, _ops) \
46 #include <linux/bpf_types.h>
52 * If we're handed a bigger struct than we know of, ensure all the unknown bits
53 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
54 * we don't know about yet.
56 * There is a ToCToU between this function call and the following
57 * copy_from_user() call. However, this is not a concern since this function is
58 * meant to be a future-proofing of bits.
60 static int check_uarg_tail_zero(void __user *uaddr,
64 unsigned char __user *addr;
65 unsigned char __user *end;
69 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
72 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
75 if (actual_size <= expected_size)
78 addr = uaddr + expected_size;
79 end = uaddr + actual_size;
81 for (; addr < end; addr++) {
82 err = get_user(val, addr);
92 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
96 if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
97 !bpf_map_types[attr->map_type])
98 return ERR_PTR(-EINVAL);
100 map = bpf_map_types[attr->map_type]->map_alloc(attr);
103 map->ops = bpf_map_types[attr->map_type];
104 map->map_type = attr->map_type;
108 void *bpf_map_area_alloc(size_t size, int numa_node)
110 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
111 * trigger under memory pressure as we really just want to
114 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
117 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
118 area = kmalloc_node(size, GFP_USER | flags, numa_node);
123 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
124 __builtin_return_address(0));
127 void bpf_map_area_free(void *area)
132 int bpf_map_precharge_memlock(u32 pages)
134 struct user_struct *user = get_current_user();
135 unsigned long memlock_limit, cur;
137 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
138 cur = atomic_long_read(&user->locked_vm);
140 if (cur + pages > memlock_limit)
145 static int bpf_map_charge_memlock(struct bpf_map *map)
147 struct user_struct *user = get_current_user();
148 unsigned long memlock_limit;
150 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
152 atomic_long_add(map->pages, &user->locked_vm);
154 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
155 atomic_long_sub(map->pages, &user->locked_vm);
163 static void bpf_map_uncharge_memlock(struct bpf_map *map)
165 struct user_struct *user = map->user;
167 atomic_long_sub(map->pages, &user->locked_vm);
171 static int bpf_map_alloc_id(struct bpf_map *map)
175 spin_lock_bh(&map_idr_lock);
176 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
179 spin_unlock_bh(&map_idr_lock);
181 if (WARN_ON_ONCE(!id))
184 return id > 0 ? 0 : id;
187 static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
192 spin_lock_irqsave(&map_idr_lock, flags);
194 __acquire(&map_idr_lock);
196 idr_remove(&map_idr, map->id);
199 spin_unlock_irqrestore(&map_idr_lock, flags);
201 __release(&map_idr_lock);
204 /* called from workqueue */
205 static void bpf_map_free_deferred(struct work_struct *work)
207 struct bpf_map *map = container_of(work, struct bpf_map, work);
209 bpf_map_uncharge_memlock(map);
210 /* implementation dependent freeing */
211 map->ops->map_free(map);
214 static void bpf_map_put_uref(struct bpf_map *map)
216 if (atomic_dec_and_test(&map->usercnt)) {
217 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
218 bpf_fd_array_map_clear(map);
222 /* decrement map refcnt and schedule it for freeing via workqueue
223 * (unrelying map implementation ops->map_free() might sleep)
225 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
227 if (atomic_dec_and_test(&map->refcnt)) {
228 /* bpf_map_free_id() must be called first */
229 bpf_map_free_id(map, do_idr_lock);
230 INIT_WORK(&map->work, bpf_map_free_deferred);
231 schedule_work(&map->work);
235 void bpf_map_put(struct bpf_map *map)
237 __bpf_map_put(map, true);
240 void bpf_map_put_with_uref(struct bpf_map *map)
242 bpf_map_put_uref(map);
246 static int bpf_map_release(struct inode *inode, struct file *filp)
248 struct bpf_map *map = filp->private_data;
250 if (map->ops->map_release)
251 map->ops->map_release(map, filp);
253 bpf_map_put_with_uref(map);
257 #ifdef CONFIG_PROC_FS
258 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
260 const struct bpf_map *map = filp->private_data;
261 const struct bpf_array *array;
262 u32 owner_prog_type = 0;
265 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
266 array = container_of(map, struct bpf_array, map);
267 owner_prog_type = array->owner_prog_type;
268 owner_jited = array->owner_jited;
283 map->pages * 1ULL << PAGE_SHIFT);
285 if (owner_prog_type) {
286 seq_printf(m, "owner_prog_type:\t%u\n",
288 seq_printf(m, "owner_jited:\t%u\n",
294 static const struct file_operations bpf_map_fops = {
295 #ifdef CONFIG_PROC_FS
296 .show_fdinfo = bpf_map_show_fdinfo,
298 .release = bpf_map_release,
301 int bpf_map_new_fd(struct bpf_map *map)
303 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
307 /* helper macro to check that unused fields 'union bpf_attr' are zero */
308 #define CHECK_ATTR(CMD) \
309 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
310 sizeof(attr->CMD##_LAST_FIELD), 0, \
312 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
313 sizeof(attr->CMD##_LAST_FIELD)) != NULL
315 #define BPF_MAP_CREATE_LAST_FIELD numa_node
316 /* called via syscall */
317 static int map_create(union bpf_attr *attr)
319 int numa_node = bpf_map_attr_numa_node(attr);
323 err = CHECK_ATTR(BPF_MAP_CREATE);
327 if (numa_node != NUMA_NO_NODE &&
328 ((unsigned int)numa_node >= nr_node_ids ||
329 !node_online(numa_node)))
332 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
333 map = find_and_alloc_map(attr);
337 atomic_set(&map->refcnt, 1);
338 atomic_set(&map->usercnt, 1);
340 err = bpf_map_charge_memlock(map);
342 goto free_map_nouncharge;
344 err = bpf_map_alloc_id(map);
348 err = bpf_map_new_fd(map);
350 /* failed to allocate fd.
351 * bpf_map_put() is needed because the above
352 * bpf_map_alloc_id() has published the map
353 * to the userspace and the userspace may
354 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
360 trace_bpf_map_create(map, err);
364 bpf_map_uncharge_memlock(map);
366 map->ops->map_free(map);
370 /* if error is returned, fd is released.
371 * On success caller should complete fd access with matching fdput()
373 struct bpf_map *__bpf_map_get(struct fd f)
376 return ERR_PTR(-EBADF);
377 if (f.file->f_op != &bpf_map_fops) {
379 return ERR_PTR(-EINVAL);
382 return f.file->private_data;
385 /* prog's and map's refcnt limit */
386 #define BPF_MAX_REFCNT 32768
388 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
390 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
391 atomic_dec(&map->refcnt);
392 return ERR_PTR(-EBUSY);
395 atomic_inc(&map->usercnt);
399 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
401 struct fd f = fdget(ufd);
404 map = __bpf_map_get(f);
408 map = bpf_map_inc(map, true);
414 /* map_idr_lock should have been held */
415 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
420 refold = __atomic_add_unless(&map->refcnt, 1, 0);
422 if (refold >= BPF_MAX_REFCNT) {
423 __bpf_map_put(map, false);
424 return ERR_PTR(-EBUSY);
428 return ERR_PTR(-ENOENT);
431 atomic_inc(&map->usercnt);
436 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
441 /* last field in 'union bpf_attr' used by this command */
442 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
444 static int map_lookup_elem(union bpf_attr *attr)
446 void __user *ukey = u64_to_user_ptr(attr->key);
447 void __user *uvalue = u64_to_user_ptr(attr->value);
448 int ufd = attr->map_fd;
450 void *key, *value, *ptr;
455 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
459 map = __bpf_map_get(f);
463 key = memdup_user(ukey, map->key_size);
469 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
470 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
471 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
472 value_size = round_up(map->value_size, 8) * num_possible_cpus();
473 else if (IS_FD_MAP(map))
474 value_size = sizeof(u32);
476 value_size = map->value_size;
479 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
483 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
484 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
485 err = bpf_percpu_hash_copy(map, key, value);
486 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
487 err = bpf_percpu_array_copy(map, key, value);
488 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
489 err = bpf_stackmap_copy(map, key, value);
490 } else if (IS_FD_ARRAY(map)) {
491 err = bpf_fd_array_map_lookup_elem(map, key, value);
492 } else if (IS_FD_HASH(map)) {
493 err = bpf_fd_htab_map_lookup_elem(map, key, value);
496 ptr = map->ops->map_lookup_elem(map, key);
498 memcpy(value, ptr, value_size);
500 err = ptr ? 0 : -ENOENT;
507 if (copy_to_user(uvalue, value, value_size) != 0)
510 trace_bpf_map_lookup_elem(map, ufd, key, value);
522 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
524 static int map_update_elem(union bpf_attr *attr)
526 void __user *ukey = u64_to_user_ptr(attr->key);
527 void __user *uvalue = u64_to_user_ptr(attr->value);
528 int ufd = attr->map_fd;
535 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
539 map = __bpf_map_get(f);
543 key = memdup_user(ukey, map->key_size);
549 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
550 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
551 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
552 value_size = round_up(map->value_size, 8) * num_possible_cpus();
554 value_size = map->value_size;
557 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
562 if (copy_from_user(value, uvalue, value_size) != 0)
565 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
566 * inside bpf map update or delete otherwise deadlocks are possible
569 __this_cpu_inc(bpf_prog_active);
570 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
571 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
572 err = bpf_percpu_hash_update(map, key, value, attr->flags);
573 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
574 err = bpf_percpu_array_update(map, key, value, attr->flags);
575 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
576 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
577 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
578 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
580 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
583 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
585 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
590 err = map->ops->map_update_elem(map, key, value, attr->flags);
593 __this_cpu_dec(bpf_prog_active);
597 trace_bpf_map_update_elem(map, ufd, key, value);
607 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
609 static int map_delete_elem(union bpf_attr *attr)
611 void __user *ukey = u64_to_user_ptr(attr->key);
612 int ufd = attr->map_fd;
618 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
622 map = __bpf_map_get(f);
626 key = memdup_user(ukey, map->key_size);
633 __this_cpu_inc(bpf_prog_active);
635 err = map->ops->map_delete_elem(map, key);
637 __this_cpu_dec(bpf_prog_active);
641 trace_bpf_map_delete_elem(map, ufd, key);
648 /* last field in 'union bpf_attr' used by this command */
649 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
651 static int map_get_next_key(union bpf_attr *attr)
653 void __user *ukey = u64_to_user_ptr(attr->key);
654 void __user *unext_key = u64_to_user_ptr(attr->next_key);
655 int ufd = attr->map_fd;
657 void *key, *next_key;
661 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
665 map = __bpf_map_get(f);
670 key = memdup_user(ukey, map->key_size);
680 next_key = kmalloc(map->key_size, GFP_USER);
685 err = map->ops->map_get_next_key(map, key, next_key);
691 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
694 trace_bpf_map_next_key(map, ufd, key, next_key);
706 static const struct bpf_verifier_ops * const bpf_prog_types[] = {
707 #define BPF_PROG_TYPE(_id, _ops) \
709 #define BPF_MAP_TYPE(_id, _ops)
710 #include <linux/bpf_types.h>
715 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
717 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
720 prog->aux->ops = bpf_prog_types[type];
725 /* drop refcnt on maps used by eBPF program and free auxilary data */
726 static void free_used_maps(struct bpf_prog_aux *aux)
730 for (i = 0; i < aux->used_map_cnt; i++)
731 bpf_map_put(aux->used_maps[i]);
733 kfree(aux->used_maps);
736 int __bpf_prog_charge(struct user_struct *user, u32 pages)
738 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
739 unsigned long user_bufs;
742 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
743 if (user_bufs > memlock_limit) {
744 atomic_long_sub(pages, &user->locked_vm);
752 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
755 atomic_long_sub(pages, &user->locked_vm);
758 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
760 struct user_struct *user = get_current_user();
763 ret = __bpf_prog_charge(user, prog->pages);
769 prog->aux->user = user;
773 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
775 struct user_struct *user = prog->aux->user;
777 __bpf_prog_uncharge(user, prog->pages);
781 static int bpf_prog_alloc_id(struct bpf_prog *prog)
785 spin_lock_bh(&prog_idr_lock);
786 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
789 spin_unlock_bh(&prog_idr_lock);
791 /* id is in [1, INT_MAX) */
792 if (WARN_ON_ONCE(!id))
795 return id > 0 ? 0 : id;
798 static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
800 /* cBPF to eBPF migrations are currently not in the idr store. */
805 spin_lock_bh(&prog_idr_lock);
807 __acquire(&prog_idr_lock);
809 idr_remove(&prog_idr, prog->aux->id);
812 spin_unlock_bh(&prog_idr_lock);
814 __release(&prog_idr_lock);
817 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
819 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
822 bpf_prog_uncharge_memlock(aux->prog);
823 bpf_prog_free(aux->prog);
826 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
828 if (atomic_dec_and_test(&prog->aux->refcnt)) {
829 trace_bpf_prog_put_rcu(prog);
830 /* bpf_prog_free_id() must be called first */
831 bpf_prog_free_id(prog, do_idr_lock);
832 bpf_prog_kallsyms_del(prog);
833 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
837 void bpf_prog_put(struct bpf_prog *prog)
839 __bpf_prog_put(prog, true);
841 EXPORT_SYMBOL_GPL(bpf_prog_put);
843 static int bpf_prog_release(struct inode *inode, struct file *filp)
845 struct bpf_prog *prog = filp->private_data;
851 #ifdef CONFIG_PROC_FS
852 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
854 const struct bpf_prog *prog = filp->private_data;
855 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
857 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
866 prog->pages * 1ULL << PAGE_SHIFT);
870 static const struct file_operations bpf_prog_fops = {
871 #ifdef CONFIG_PROC_FS
872 .show_fdinfo = bpf_prog_show_fdinfo,
874 .release = bpf_prog_release,
877 int bpf_prog_new_fd(struct bpf_prog *prog)
879 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
883 static struct bpf_prog *____bpf_prog_get(struct fd f)
886 return ERR_PTR(-EBADF);
887 if (f.file->f_op != &bpf_prog_fops) {
889 return ERR_PTR(-EINVAL);
892 return f.file->private_data;
895 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
897 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
898 atomic_sub(i, &prog->aux->refcnt);
899 return ERR_PTR(-EBUSY);
903 EXPORT_SYMBOL_GPL(bpf_prog_add);
905 void bpf_prog_sub(struct bpf_prog *prog, int i)
907 /* Only to be used for undoing previous bpf_prog_add() in some
908 * error path. We still know that another entity in our call
909 * path holds a reference to the program, thus atomic_sub() can
910 * be safely used in such cases!
912 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
914 EXPORT_SYMBOL_GPL(bpf_prog_sub);
916 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
918 return bpf_prog_add(prog, 1);
920 EXPORT_SYMBOL_GPL(bpf_prog_inc);
922 /* prog_idr_lock should have been held */
923 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
927 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
929 if (refold >= BPF_MAX_REFCNT) {
930 __bpf_prog_put(prog, false);
931 return ERR_PTR(-EBUSY);
935 return ERR_PTR(-ENOENT);
939 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
941 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
943 struct fd f = fdget(ufd);
944 struct bpf_prog *prog;
946 prog = ____bpf_prog_get(f);
949 if (type && prog->type != *type) {
950 prog = ERR_PTR(-EINVAL);
954 prog = bpf_prog_inc(prog);
960 struct bpf_prog *bpf_prog_get(u32 ufd)
962 return __bpf_prog_get(ufd, NULL);
965 struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
967 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
970 trace_bpf_prog_get_type(prog);
973 EXPORT_SYMBOL_GPL(bpf_prog_get_type);
975 /* last field in 'union bpf_attr' used by this command */
976 #define BPF_PROG_LOAD_LAST_FIELD prog_flags
978 static int bpf_prog_load(union bpf_attr *attr)
980 enum bpf_prog_type type = attr->prog_type;
981 struct bpf_prog *prog;
986 if (CHECK_ATTR(BPF_PROG_LOAD))
989 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
992 /* copy eBPF program license from user space */
993 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
994 sizeof(license) - 1) < 0)
996 license[sizeof(license) - 1] = 0;
998 /* eBPF programs must be GPL compatible to use GPL-ed functions */
999 is_gpl = license_is_gpl_compatible(license);
1001 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1004 if (type == BPF_PROG_TYPE_KPROBE &&
1005 attr->kern_version != LINUX_VERSION_CODE)
1008 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1009 type != BPF_PROG_TYPE_CGROUP_SKB &&
1010 !capable(CAP_SYS_ADMIN))
1013 /* plain bpf_prog allocation */
1014 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1018 err = bpf_prog_charge_memlock(prog);
1020 goto free_prog_nouncharge;
1022 prog->len = attr->insn_cnt;
1025 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1026 bpf_prog_insn_size(prog)) != 0)
1029 prog->orig_prog = NULL;
1032 atomic_set(&prog->aux->refcnt, 1);
1033 prog->gpl_compatible = is_gpl ? 1 : 0;
1035 /* find program type: socket_filter vs tracing_filter */
1036 err = find_prog_type(type, prog);
1040 /* run eBPF verifier */
1041 err = bpf_check(&prog, attr);
1043 goto free_used_maps;
1045 /* eBPF program is ready to be JITed */
1046 prog = bpf_prog_select_runtime(prog, &err);
1048 goto free_used_maps;
1050 err = bpf_prog_alloc_id(prog);
1052 goto free_used_maps;
1054 err = bpf_prog_new_fd(prog);
1056 /* failed to allocate fd.
1057 * bpf_prog_put() is needed because the above
1058 * bpf_prog_alloc_id() has published the prog
1059 * to the userspace and the userspace may
1060 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1066 bpf_prog_kallsyms_add(prog);
1067 trace_bpf_prog_load(prog, err);
1071 free_used_maps(prog->aux);
1073 bpf_prog_uncharge_memlock(prog);
1074 free_prog_nouncharge:
1075 bpf_prog_free(prog);
1079 #define BPF_OBJ_LAST_FIELD bpf_fd
1081 static int bpf_obj_pin(const union bpf_attr *attr)
1083 if (CHECK_ATTR(BPF_OBJ))
1086 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1089 static int bpf_obj_get(const union bpf_attr *attr)
1091 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
1094 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
1097 #ifdef CONFIG_CGROUP_BPF
1099 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1101 static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
1103 struct bpf_prog *prog = NULL;
1104 int ufd = attr->target_fd;
1105 struct bpf_map *map;
1110 map = __bpf_map_get(f);
1112 return PTR_ERR(map);
1115 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1116 BPF_PROG_TYPE_SK_SKB);
1119 return PTR_ERR(prog);
1123 err = sock_map_prog(map, prog, attr->attach_type);
1135 static int bpf_prog_attach(const union bpf_attr *attr)
1137 enum bpf_prog_type ptype;
1138 struct bpf_prog *prog;
1139 struct cgroup *cgrp;
1142 if (!capable(CAP_NET_ADMIN))
1145 if (CHECK_ATTR(BPF_PROG_ATTACH))
1148 if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
1151 switch (attr->attach_type) {
1152 case BPF_CGROUP_INET_INGRESS:
1153 case BPF_CGROUP_INET_EGRESS:
1154 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1156 case BPF_CGROUP_INET_SOCK_CREATE:
1157 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1159 case BPF_CGROUP_SOCK_OPS:
1160 ptype = BPF_PROG_TYPE_SOCK_OPS;
1162 case BPF_SK_SKB_STREAM_PARSER:
1163 case BPF_SK_SKB_STREAM_VERDICT:
1164 return sockmap_get_from_fd(attr, true);
1169 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1171 return PTR_ERR(prog);
1173 cgrp = cgroup_get_from_fd(attr->target_fd);
1176 return PTR_ERR(cgrp);
1179 ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
1180 attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
1188 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1190 static int bpf_prog_detach(const union bpf_attr *attr)
1192 struct cgroup *cgrp;
1195 if (!capable(CAP_NET_ADMIN))
1198 if (CHECK_ATTR(BPF_PROG_DETACH))
1201 switch (attr->attach_type) {
1202 case BPF_CGROUP_INET_INGRESS:
1203 case BPF_CGROUP_INET_EGRESS:
1204 case BPF_CGROUP_INET_SOCK_CREATE:
1205 case BPF_CGROUP_SOCK_OPS:
1206 cgrp = cgroup_get_from_fd(attr->target_fd);
1208 return PTR_ERR(cgrp);
1210 ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
1213 case BPF_SK_SKB_STREAM_PARSER:
1214 case BPF_SK_SKB_STREAM_VERDICT:
1215 ret = sockmap_get_from_fd(attr, false);
1224 #endif /* CONFIG_CGROUP_BPF */
1226 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1228 static int bpf_prog_test_run(const union bpf_attr *attr,
1229 union bpf_attr __user *uattr)
1231 struct bpf_prog *prog;
1232 int ret = -ENOTSUPP;
1234 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1237 prog = bpf_prog_get(attr->test.prog_fd);
1239 return PTR_ERR(prog);
1241 if (prog->aux->ops->test_run)
1242 ret = prog->aux->ops->test_run(prog, attr, uattr);
1248 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1250 static int bpf_obj_get_next_id(const union bpf_attr *attr,
1251 union bpf_attr __user *uattr,
1255 u32 next_id = attr->start_id;
1258 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1261 if (!capable(CAP_SYS_ADMIN))
1266 if (!idr_get_next(idr, &next_id))
1268 spin_unlock_bh(lock);
1271 err = put_user(next_id, &uattr->next_id);
1276 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1278 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1280 struct bpf_prog *prog;
1281 u32 id = attr->prog_id;
1284 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1287 if (!capable(CAP_SYS_ADMIN))
1290 spin_lock_bh(&prog_idr_lock);
1291 prog = idr_find(&prog_idr, id);
1293 prog = bpf_prog_inc_not_zero(prog);
1295 prog = ERR_PTR(-ENOENT);
1296 spin_unlock_bh(&prog_idr_lock);
1299 return PTR_ERR(prog);
1301 fd = bpf_prog_new_fd(prog);
1308 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD map_id
1310 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1312 struct bpf_map *map;
1313 u32 id = attr->map_id;
1316 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID))
1319 if (!capable(CAP_SYS_ADMIN))
1322 spin_lock_bh(&map_idr_lock);
1323 map = idr_find(&map_idr, id);
1325 map = bpf_map_inc_not_zero(map, true);
1327 map = ERR_PTR(-ENOENT);
1328 spin_unlock_bh(&map_idr_lock);
1331 return PTR_ERR(map);
1333 fd = bpf_map_new_fd(map);
1340 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1341 const union bpf_attr *attr,
1342 union bpf_attr __user *uattr)
1344 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1345 struct bpf_prog_info info = {};
1346 u32 info_len = attr->info.info_len;
1347 char __user *uinsns;
1351 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1354 info_len = min_t(u32, sizeof(info), info_len);
1356 if (copy_from_user(&info, uinfo, info_len))
1359 info.type = prog->type;
1360 info.id = prog->aux->id;
1362 memcpy(info.tag, prog->tag, sizeof(prog->tag));
1364 if (!capable(CAP_SYS_ADMIN)) {
1365 info.jited_prog_len = 0;
1366 info.xlated_prog_len = 0;
1370 ulen = info.jited_prog_len;
1371 info.jited_prog_len = prog->jited_len;
1372 if (info.jited_prog_len && ulen) {
1373 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1374 ulen = min_t(u32, info.jited_prog_len, ulen);
1375 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1379 ulen = info.xlated_prog_len;
1380 info.xlated_prog_len = bpf_prog_insn_size(prog);
1381 if (info.xlated_prog_len && ulen) {
1382 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1383 ulen = min_t(u32, info.xlated_prog_len, ulen);
1384 if (copy_to_user(uinsns, prog->insnsi, ulen))
1389 if (copy_to_user(uinfo, &info, info_len) ||
1390 put_user(info_len, &uattr->info.info_len))
1396 static int bpf_map_get_info_by_fd(struct bpf_map *map,
1397 const union bpf_attr *attr,
1398 union bpf_attr __user *uattr)
1400 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1401 struct bpf_map_info info = {};
1402 u32 info_len = attr->info.info_len;
1405 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1408 info_len = min_t(u32, sizeof(info), info_len);
1410 info.type = map->map_type;
1412 info.key_size = map->key_size;
1413 info.value_size = map->value_size;
1414 info.max_entries = map->max_entries;
1415 info.map_flags = map->map_flags;
1417 if (copy_to_user(uinfo, &info, info_len) ||
1418 put_user(info_len, &uattr->info.info_len))
1424 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1426 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1427 union bpf_attr __user *uattr)
1429 int ufd = attr->info.bpf_fd;
1433 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1440 if (f.file->f_op == &bpf_prog_fops)
1441 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1443 else if (f.file->f_op == &bpf_map_fops)
1444 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1453 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1455 union bpf_attr attr = {};
1458 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
1461 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1464 size = min_t(u32, size, sizeof(attr));
1466 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1467 if (copy_from_user(&attr, uattr, size) != 0)
1471 case BPF_MAP_CREATE:
1472 err = map_create(&attr);
1474 case BPF_MAP_LOOKUP_ELEM:
1475 err = map_lookup_elem(&attr);
1477 case BPF_MAP_UPDATE_ELEM:
1478 err = map_update_elem(&attr);
1480 case BPF_MAP_DELETE_ELEM:
1481 err = map_delete_elem(&attr);
1483 case BPF_MAP_GET_NEXT_KEY:
1484 err = map_get_next_key(&attr);
1487 err = bpf_prog_load(&attr);
1490 err = bpf_obj_pin(&attr);
1493 err = bpf_obj_get(&attr);
1495 #ifdef CONFIG_CGROUP_BPF
1496 case BPF_PROG_ATTACH:
1497 err = bpf_prog_attach(&attr);
1499 case BPF_PROG_DETACH:
1500 err = bpf_prog_detach(&attr);
1503 case BPF_PROG_TEST_RUN:
1504 err = bpf_prog_test_run(&attr, uattr);
1506 case BPF_PROG_GET_NEXT_ID:
1507 err = bpf_obj_get_next_id(&attr, uattr,
1508 &prog_idr, &prog_idr_lock);
1510 case BPF_MAP_GET_NEXT_ID:
1511 err = bpf_obj_get_next_id(&attr, uattr,
1512 &map_idr, &map_idr_lock);
1514 case BPF_PROG_GET_FD_BY_ID:
1515 err = bpf_prog_get_fd_by_id(&attr);
1517 case BPF_MAP_GET_FD_BY_ID:
1518 err = bpf_map_get_fd_by_id(&attr);
1520 case BPF_OBJ_GET_INFO_BY_FD:
1521 err = bpf_obj_get_info_by_fd(&attr, uattr);