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)
190 spin_lock_bh(&map_idr_lock);
192 __acquire(&map_idr_lock);
194 idr_remove(&map_idr, map->id);
197 spin_unlock_bh(&map_idr_lock);
199 __release(&map_idr_lock);
202 /* called from workqueue */
203 static void bpf_map_free_deferred(struct work_struct *work)
205 struct bpf_map *map = container_of(work, struct bpf_map, work);
207 bpf_map_uncharge_memlock(map);
208 /* implementation dependent freeing */
209 map->ops->map_free(map);
212 static void bpf_map_put_uref(struct bpf_map *map)
214 if (atomic_dec_and_test(&map->usercnt)) {
215 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
216 bpf_fd_array_map_clear(map);
220 /* decrement map refcnt and schedule it for freeing via workqueue
221 * (unrelying map implementation ops->map_free() might sleep)
223 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
225 if (atomic_dec_and_test(&map->refcnt)) {
226 /* bpf_map_free_id() must be called first */
227 bpf_map_free_id(map, do_idr_lock);
228 INIT_WORK(&map->work, bpf_map_free_deferred);
229 schedule_work(&map->work);
233 void bpf_map_put(struct bpf_map *map)
235 __bpf_map_put(map, true);
238 void bpf_map_put_with_uref(struct bpf_map *map)
240 bpf_map_put_uref(map);
244 static int bpf_map_release(struct inode *inode, struct file *filp)
246 struct bpf_map *map = filp->private_data;
248 if (map->ops->map_release)
249 map->ops->map_release(map, filp);
251 bpf_map_put_with_uref(map);
255 #ifdef CONFIG_PROC_FS
256 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
258 const struct bpf_map *map = filp->private_data;
259 const struct bpf_array *array;
260 u32 owner_prog_type = 0;
263 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
264 array = container_of(map, struct bpf_array, map);
265 owner_prog_type = array->owner_prog_type;
266 owner_jited = array->owner_jited;
281 map->pages * 1ULL << PAGE_SHIFT);
283 if (owner_prog_type) {
284 seq_printf(m, "owner_prog_type:\t%u\n",
286 seq_printf(m, "owner_jited:\t%u\n",
292 static const struct file_operations bpf_map_fops = {
293 #ifdef CONFIG_PROC_FS
294 .show_fdinfo = bpf_map_show_fdinfo,
296 .release = bpf_map_release,
299 int bpf_map_new_fd(struct bpf_map *map)
301 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
305 /* helper macro to check that unused fields 'union bpf_attr' are zero */
306 #define CHECK_ATTR(CMD) \
307 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
308 sizeof(attr->CMD##_LAST_FIELD), 0, \
310 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
311 sizeof(attr->CMD##_LAST_FIELD)) != NULL
313 #define BPF_MAP_CREATE_LAST_FIELD numa_node
314 /* called via syscall */
315 static int map_create(union bpf_attr *attr)
317 int numa_node = bpf_map_attr_numa_node(attr);
321 err = CHECK_ATTR(BPF_MAP_CREATE);
325 if (numa_node != NUMA_NO_NODE &&
326 ((unsigned int)numa_node >= nr_node_ids ||
327 !node_online(numa_node)))
330 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
331 map = find_and_alloc_map(attr);
335 atomic_set(&map->refcnt, 1);
336 atomic_set(&map->usercnt, 1);
338 err = bpf_map_charge_memlock(map);
340 goto free_map_nouncharge;
342 err = bpf_map_alloc_id(map);
346 err = bpf_map_new_fd(map);
348 /* failed to allocate fd.
349 * bpf_map_put() is needed because the above
350 * bpf_map_alloc_id() has published the map
351 * to the userspace and the userspace may
352 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
358 trace_bpf_map_create(map, err);
362 bpf_map_uncharge_memlock(map);
364 map->ops->map_free(map);
368 /* if error is returned, fd is released.
369 * On success caller should complete fd access with matching fdput()
371 struct bpf_map *__bpf_map_get(struct fd f)
374 return ERR_PTR(-EBADF);
375 if (f.file->f_op != &bpf_map_fops) {
377 return ERR_PTR(-EINVAL);
380 return f.file->private_data;
383 /* prog's and map's refcnt limit */
384 #define BPF_MAX_REFCNT 32768
386 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
388 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
389 atomic_dec(&map->refcnt);
390 return ERR_PTR(-EBUSY);
393 atomic_inc(&map->usercnt);
397 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
399 struct fd f = fdget(ufd);
402 map = __bpf_map_get(f);
406 map = bpf_map_inc(map, true);
412 /* map_idr_lock should have been held */
413 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
418 refold = __atomic_add_unless(&map->refcnt, 1, 0);
420 if (refold >= BPF_MAX_REFCNT) {
421 __bpf_map_put(map, false);
422 return ERR_PTR(-EBUSY);
426 return ERR_PTR(-ENOENT);
429 atomic_inc(&map->usercnt);
434 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
439 /* last field in 'union bpf_attr' used by this command */
440 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
442 static int map_lookup_elem(union bpf_attr *attr)
444 void __user *ukey = u64_to_user_ptr(attr->key);
445 void __user *uvalue = u64_to_user_ptr(attr->value);
446 int ufd = attr->map_fd;
448 void *key, *value, *ptr;
453 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
457 map = __bpf_map_get(f);
461 key = memdup_user(ukey, map->key_size);
467 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
468 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
469 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
470 value_size = round_up(map->value_size, 8) * num_possible_cpus();
471 else if (IS_FD_MAP(map))
472 value_size = sizeof(u32);
474 value_size = map->value_size;
477 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
481 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
482 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
483 err = bpf_percpu_hash_copy(map, key, value);
484 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
485 err = bpf_percpu_array_copy(map, key, value);
486 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
487 err = bpf_stackmap_copy(map, key, value);
488 } else if (IS_FD_ARRAY(map)) {
489 err = bpf_fd_array_map_lookup_elem(map, key, value);
490 } else if (IS_FD_HASH(map)) {
491 err = bpf_fd_htab_map_lookup_elem(map, key, value);
494 ptr = map->ops->map_lookup_elem(map, key);
496 memcpy(value, ptr, value_size);
498 err = ptr ? 0 : -ENOENT;
505 if (copy_to_user(uvalue, value, value_size) != 0)
508 trace_bpf_map_lookup_elem(map, ufd, key, value);
520 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
522 static int map_update_elem(union bpf_attr *attr)
524 void __user *ukey = u64_to_user_ptr(attr->key);
525 void __user *uvalue = u64_to_user_ptr(attr->value);
526 int ufd = attr->map_fd;
533 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
537 map = __bpf_map_get(f);
541 key = memdup_user(ukey, map->key_size);
547 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
548 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
549 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
550 value_size = round_up(map->value_size, 8) * num_possible_cpus();
552 value_size = map->value_size;
555 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
560 if (copy_from_user(value, uvalue, value_size) != 0)
563 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
564 * inside bpf map update or delete otherwise deadlocks are possible
567 __this_cpu_inc(bpf_prog_active);
568 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
569 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
570 err = bpf_percpu_hash_update(map, key, value, attr->flags);
571 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
572 err = bpf_percpu_array_update(map, key, value, attr->flags);
573 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
574 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
575 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
576 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
578 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
581 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
583 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
588 err = map->ops->map_update_elem(map, key, value, attr->flags);
591 __this_cpu_dec(bpf_prog_active);
595 trace_bpf_map_update_elem(map, ufd, key, value);
605 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
607 static int map_delete_elem(union bpf_attr *attr)
609 void __user *ukey = u64_to_user_ptr(attr->key);
610 int ufd = attr->map_fd;
616 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
620 map = __bpf_map_get(f);
624 key = memdup_user(ukey, map->key_size);
631 __this_cpu_inc(bpf_prog_active);
633 err = map->ops->map_delete_elem(map, key);
635 __this_cpu_dec(bpf_prog_active);
639 trace_bpf_map_delete_elem(map, ufd, key);
646 /* last field in 'union bpf_attr' used by this command */
647 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
649 static int map_get_next_key(union bpf_attr *attr)
651 void __user *ukey = u64_to_user_ptr(attr->key);
652 void __user *unext_key = u64_to_user_ptr(attr->next_key);
653 int ufd = attr->map_fd;
655 void *key, *next_key;
659 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
663 map = __bpf_map_get(f);
668 key = memdup_user(ukey, map->key_size);
678 next_key = kmalloc(map->key_size, GFP_USER);
683 err = map->ops->map_get_next_key(map, key, next_key);
689 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
692 trace_bpf_map_next_key(map, ufd, key, next_key);
704 static const struct bpf_verifier_ops * const bpf_prog_types[] = {
705 #define BPF_PROG_TYPE(_id, _ops) \
707 #define BPF_MAP_TYPE(_id, _ops)
708 #include <linux/bpf_types.h>
713 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
715 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
718 prog->aux->ops = bpf_prog_types[type];
723 /* drop refcnt on maps used by eBPF program and free auxilary data */
724 static void free_used_maps(struct bpf_prog_aux *aux)
728 for (i = 0; i < aux->used_map_cnt; i++)
729 bpf_map_put(aux->used_maps[i]);
731 kfree(aux->used_maps);
734 int __bpf_prog_charge(struct user_struct *user, u32 pages)
736 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
737 unsigned long user_bufs;
740 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
741 if (user_bufs > memlock_limit) {
742 atomic_long_sub(pages, &user->locked_vm);
750 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
753 atomic_long_sub(pages, &user->locked_vm);
756 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
758 struct user_struct *user = get_current_user();
761 ret = __bpf_prog_charge(user, prog->pages);
767 prog->aux->user = user;
771 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
773 struct user_struct *user = prog->aux->user;
775 __bpf_prog_uncharge(user, prog->pages);
779 static int bpf_prog_alloc_id(struct bpf_prog *prog)
783 spin_lock_bh(&prog_idr_lock);
784 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
787 spin_unlock_bh(&prog_idr_lock);
789 /* id is in [1, INT_MAX) */
790 if (WARN_ON_ONCE(!id))
793 return id > 0 ? 0 : id;
796 static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
798 /* cBPF to eBPF migrations are currently not in the idr store. */
803 spin_lock_bh(&prog_idr_lock);
805 __acquire(&prog_idr_lock);
807 idr_remove(&prog_idr, prog->aux->id);
810 spin_unlock_bh(&prog_idr_lock);
812 __release(&prog_idr_lock);
815 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
817 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
820 bpf_prog_uncharge_memlock(aux->prog);
821 bpf_prog_free(aux->prog);
824 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
826 if (atomic_dec_and_test(&prog->aux->refcnt)) {
827 trace_bpf_prog_put_rcu(prog);
828 /* bpf_prog_free_id() must be called first */
829 bpf_prog_free_id(prog, do_idr_lock);
830 bpf_prog_kallsyms_del(prog);
831 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
835 void bpf_prog_put(struct bpf_prog *prog)
837 __bpf_prog_put(prog, true);
839 EXPORT_SYMBOL_GPL(bpf_prog_put);
841 static int bpf_prog_release(struct inode *inode, struct file *filp)
843 struct bpf_prog *prog = filp->private_data;
849 #ifdef CONFIG_PROC_FS
850 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
852 const struct bpf_prog *prog = filp->private_data;
853 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
855 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
864 prog->pages * 1ULL << PAGE_SHIFT);
868 static const struct file_operations bpf_prog_fops = {
869 #ifdef CONFIG_PROC_FS
870 .show_fdinfo = bpf_prog_show_fdinfo,
872 .release = bpf_prog_release,
875 int bpf_prog_new_fd(struct bpf_prog *prog)
877 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
881 static struct bpf_prog *____bpf_prog_get(struct fd f)
884 return ERR_PTR(-EBADF);
885 if (f.file->f_op != &bpf_prog_fops) {
887 return ERR_PTR(-EINVAL);
890 return f.file->private_data;
893 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
895 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
896 atomic_sub(i, &prog->aux->refcnt);
897 return ERR_PTR(-EBUSY);
901 EXPORT_SYMBOL_GPL(bpf_prog_add);
903 void bpf_prog_sub(struct bpf_prog *prog, int i)
905 /* Only to be used for undoing previous bpf_prog_add() in some
906 * error path. We still know that another entity in our call
907 * path holds a reference to the program, thus atomic_sub() can
908 * be safely used in such cases!
910 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
912 EXPORT_SYMBOL_GPL(bpf_prog_sub);
914 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
916 return bpf_prog_add(prog, 1);
918 EXPORT_SYMBOL_GPL(bpf_prog_inc);
920 /* prog_idr_lock should have been held */
921 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
925 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
927 if (refold >= BPF_MAX_REFCNT) {
928 __bpf_prog_put(prog, false);
929 return ERR_PTR(-EBUSY);
933 return ERR_PTR(-ENOENT);
937 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
939 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
941 struct fd f = fdget(ufd);
942 struct bpf_prog *prog;
944 prog = ____bpf_prog_get(f);
947 if (type && prog->type != *type) {
948 prog = ERR_PTR(-EINVAL);
952 prog = bpf_prog_inc(prog);
958 struct bpf_prog *bpf_prog_get(u32 ufd)
960 return __bpf_prog_get(ufd, NULL);
963 struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
965 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
968 trace_bpf_prog_get_type(prog);
971 EXPORT_SYMBOL_GPL(bpf_prog_get_type);
973 /* last field in 'union bpf_attr' used by this command */
974 #define BPF_PROG_LOAD_LAST_FIELD prog_flags
976 static int bpf_prog_load(union bpf_attr *attr)
978 enum bpf_prog_type type = attr->prog_type;
979 struct bpf_prog *prog;
984 if (CHECK_ATTR(BPF_PROG_LOAD))
987 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
990 /* copy eBPF program license from user space */
991 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
992 sizeof(license) - 1) < 0)
994 license[sizeof(license) - 1] = 0;
996 /* eBPF programs must be GPL compatible to use GPL-ed functions */
997 is_gpl = license_is_gpl_compatible(license);
999 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1002 if (type == BPF_PROG_TYPE_KPROBE &&
1003 attr->kern_version != LINUX_VERSION_CODE)
1006 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1007 type != BPF_PROG_TYPE_CGROUP_SKB &&
1008 !capable(CAP_SYS_ADMIN))
1011 /* plain bpf_prog allocation */
1012 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1016 err = bpf_prog_charge_memlock(prog);
1018 goto free_prog_nouncharge;
1020 prog->len = attr->insn_cnt;
1023 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1024 bpf_prog_insn_size(prog)) != 0)
1027 prog->orig_prog = NULL;
1030 atomic_set(&prog->aux->refcnt, 1);
1031 prog->gpl_compatible = is_gpl ? 1 : 0;
1033 /* find program type: socket_filter vs tracing_filter */
1034 err = find_prog_type(type, prog);
1038 /* run eBPF verifier */
1039 err = bpf_check(&prog, attr);
1041 goto free_used_maps;
1043 /* eBPF program is ready to be JITed */
1044 prog = bpf_prog_select_runtime(prog, &err);
1046 goto free_used_maps;
1048 err = bpf_prog_alloc_id(prog);
1050 goto free_used_maps;
1052 err = bpf_prog_new_fd(prog);
1054 /* failed to allocate fd.
1055 * bpf_prog_put() is needed because the above
1056 * bpf_prog_alloc_id() has published the prog
1057 * to the userspace and the userspace may
1058 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1064 bpf_prog_kallsyms_add(prog);
1065 trace_bpf_prog_load(prog, err);
1069 free_used_maps(prog->aux);
1071 bpf_prog_uncharge_memlock(prog);
1072 free_prog_nouncharge:
1073 bpf_prog_free(prog);
1077 #define BPF_OBJ_LAST_FIELD bpf_fd
1079 static int bpf_obj_pin(const union bpf_attr *attr)
1081 if (CHECK_ATTR(BPF_OBJ))
1084 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1087 static int bpf_obj_get(const union bpf_attr *attr)
1089 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
1092 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
1095 #ifdef CONFIG_CGROUP_BPF
1097 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1099 static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
1101 struct bpf_prog *prog = NULL;
1102 int ufd = attr->target_fd;
1103 struct bpf_map *map;
1108 map = __bpf_map_get(f);
1110 return PTR_ERR(map);
1113 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1114 BPF_PROG_TYPE_SK_SKB);
1117 return PTR_ERR(prog);
1121 err = sock_map_prog(map, prog, attr->attach_type);
1133 static int bpf_prog_attach(const union bpf_attr *attr)
1135 enum bpf_prog_type ptype;
1136 struct bpf_prog *prog;
1137 struct cgroup *cgrp;
1140 if (!capable(CAP_NET_ADMIN))
1143 if (CHECK_ATTR(BPF_PROG_ATTACH))
1146 if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
1149 switch (attr->attach_type) {
1150 case BPF_CGROUP_INET_INGRESS:
1151 case BPF_CGROUP_INET_EGRESS:
1152 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1154 case BPF_CGROUP_INET_SOCK_CREATE:
1155 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1157 case BPF_CGROUP_SOCK_OPS:
1158 ptype = BPF_PROG_TYPE_SOCK_OPS;
1160 case BPF_SK_SKB_STREAM_PARSER:
1161 case BPF_SK_SKB_STREAM_VERDICT:
1162 return sockmap_get_from_fd(attr, true);
1167 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1169 return PTR_ERR(prog);
1171 cgrp = cgroup_get_from_fd(attr->target_fd);
1174 return PTR_ERR(cgrp);
1177 ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
1178 attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
1186 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1188 static int bpf_prog_detach(const union bpf_attr *attr)
1190 struct cgroup *cgrp;
1193 if (!capable(CAP_NET_ADMIN))
1196 if (CHECK_ATTR(BPF_PROG_DETACH))
1199 switch (attr->attach_type) {
1200 case BPF_CGROUP_INET_INGRESS:
1201 case BPF_CGROUP_INET_EGRESS:
1202 case BPF_CGROUP_INET_SOCK_CREATE:
1203 case BPF_CGROUP_SOCK_OPS:
1204 cgrp = cgroup_get_from_fd(attr->target_fd);
1206 return PTR_ERR(cgrp);
1208 ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
1211 case BPF_SK_SKB_STREAM_PARSER:
1212 case BPF_SK_SKB_STREAM_VERDICT:
1213 ret = sockmap_get_from_fd(attr, false);
1222 #endif /* CONFIG_CGROUP_BPF */
1224 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1226 static int bpf_prog_test_run(const union bpf_attr *attr,
1227 union bpf_attr __user *uattr)
1229 struct bpf_prog *prog;
1230 int ret = -ENOTSUPP;
1232 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1235 prog = bpf_prog_get(attr->test.prog_fd);
1237 return PTR_ERR(prog);
1239 if (prog->aux->ops->test_run)
1240 ret = prog->aux->ops->test_run(prog, attr, uattr);
1246 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1248 static int bpf_obj_get_next_id(const union bpf_attr *attr,
1249 union bpf_attr __user *uattr,
1253 u32 next_id = attr->start_id;
1256 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1259 if (!capable(CAP_SYS_ADMIN))
1264 if (!idr_get_next(idr, &next_id))
1266 spin_unlock_bh(lock);
1269 err = put_user(next_id, &uattr->next_id);
1274 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1276 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1278 struct bpf_prog *prog;
1279 u32 id = attr->prog_id;
1282 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1285 if (!capable(CAP_SYS_ADMIN))
1288 spin_lock_bh(&prog_idr_lock);
1289 prog = idr_find(&prog_idr, id);
1291 prog = bpf_prog_inc_not_zero(prog);
1293 prog = ERR_PTR(-ENOENT);
1294 spin_unlock_bh(&prog_idr_lock);
1297 return PTR_ERR(prog);
1299 fd = bpf_prog_new_fd(prog);
1306 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD map_id
1308 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1310 struct bpf_map *map;
1311 u32 id = attr->map_id;
1314 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID))
1317 if (!capable(CAP_SYS_ADMIN))
1320 spin_lock_bh(&map_idr_lock);
1321 map = idr_find(&map_idr, id);
1323 map = bpf_map_inc_not_zero(map, true);
1325 map = ERR_PTR(-ENOENT);
1326 spin_unlock_bh(&map_idr_lock);
1329 return PTR_ERR(map);
1331 fd = bpf_map_new_fd(map);
1338 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1339 const union bpf_attr *attr,
1340 union bpf_attr __user *uattr)
1342 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1343 struct bpf_prog_info info = {};
1344 u32 info_len = attr->info.info_len;
1345 char __user *uinsns;
1349 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1352 info_len = min_t(u32, sizeof(info), info_len);
1354 if (copy_from_user(&info, uinfo, info_len))
1357 info.type = prog->type;
1358 info.id = prog->aux->id;
1360 memcpy(info.tag, prog->tag, sizeof(prog->tag));
1362 if (!capable(CAP_SYS_ADMIN)) {
1363 info.jited_prog_len = 0;
1364 info.xlated_prog_len = 0;
1368 ulen = info.jited_prog_len;
1369 info.jited_prog_len = prog->jited_len;
1370 if (info.jited_prog_len && ulen) {
1371 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1372 ulen = min_t(u32, info.jited_prog_len, ulen);
1373 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1377 ulen = info.xlated_prog_len;
1378 info.xlated_prog_len = bpf_prog_insn_size(prog);
1379 if (info.xlated_prog_len && ulen) {
1380 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1381 ulen = min_t(u32, info.xlated_prog_len, ulen);
1382 if (copy_to_user(uinsns, prog->insnsi, ulen))
1387 if (copy_to_user(uinfo, &info, info_len) ||
1388 put_user(info_len, &uattr->info.info_len))
1394 static int bpf_map_get_info_by_fd(struct bpf_map *map,
1395 const union bpf_attr *attr,
1396 union bpf_attr __user *uattr)
1398 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1399 struct bpf_map_info info = {};
1400 u32 info_len = attr->info.info_len;
1403 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1406 info_len = min_t(u32, sizeof(info), info_len);
1408 info.type = map->map_type;
1410 info.key_size = map->key_size;
1411 info.value_size = map->value_size;
1412 info.max_entries = map->max_entries;
1413 info.map_flags = map->map_flags;
1415 if (copy_to_user(uinfo, &info, info_len) ||
1416 put_user(info_len, &uattr->info.info_len))
1422 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1424 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1425 union bpf_attr __user *uattr)
1427 int ufd = attr->info.bpf_fd;
1431 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1438 if (f.file->f_op == &bpf_prog_fops)
1439 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1441 else if (f.file->f_op == &bpf_map_fops)
1442 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1451 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1453 union bpf_attr attr = {};
1456 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
1459 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1462 size = min_t(u32, size, sizeof(attr));
1464 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1465 if (copy_from_user(&attr, uattr, size) != 0)
1469 case BPF_MAP_CREATE:
1470 err = map_create(&attr);
1472 case BPF_MAP_LOOKUP_ELEM:
1473 err = map_lookup_elem(&attr);
1475 case BPF_MAP_UPDATE_ELEM:
1476 err = map_update_elem(&attr);
1478 case BPF_MAP_DELETE_ELEM:
1479 err = map_delete_elem(&attr);
1481 case BPF_MAP_GET_NEXT_KEY:
1482 err = map_get_next_key(&attr);
1485 err = bpf_prog_load(&attr);
1488 err = bpf_obj_pin(&attr);
1491 err = bpf_obj_get(&attr);
1493 #ifdef CONFIG_CGROUP_BPF
1494 case BPF_PROG_ATTACH:
1495 err = bpf_prog_attach(&attr);
1497 case BPF_PROG_DETACH:
1498 err = bpf_prog_detach(&attr);
1501 case BPF_PROG_TEST_RUN:
1502 err = bpf_prog_test_run(&attr, uattr);
1504 case BPF_PROG_GET_NEXT_ID:
1505 err = bpf_obj_get_next_id(&attr, uattr,
1506 &prog_idr, &prog_idr_lock);
1508 case BPF_MAP_GET_NEXT_ID:
1509 err = bpf_obj_get_next_id(&attr, uattr,
1510 &map_idr, &map_idr_lock);
1512 case BPF_PROG_GET_FD_BY_ID:
1513 err = bpf_prog_get_fd_by_id(&attr);
1515 case BPF_MAP_GET_FD_BY_ID:
1516 err = bpf_map_get_fd_by_id(&attr);
1518 case BPF_OBJ_GET_INFO_BY_FD:
1519 err = bpf_obj_get_info_by_fd(&attr, uattr);