1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
5 #include <linux/bpf_trace.h>
6 #include <linux/bpf_lirc.h>
8 #include <linux/syscalls.h>
9 #include <linux/slab.h>
10 #include <linux/sched/signal.h>
11 #include <linux/vmalloc.h>
12 #include <linux/mmzone.h>
13 #include <linux/anon_inodes.h>
14 #include <linux/fdtable.h>
15 #include <linux/file.h>
17 #include <linux/license.h>
18 #include <linux/filter.h>
19 #include <linux/version.h>
20 #include <linux/kernel.h>
21 #include <linux/idr.h>
22 #include <linux/cred.h>
23 #include <linux/timekeeping.h>
24 #include <linux/ctype.h>
25 #include <linux/nospec.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 BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
36 DEFINE_PER_CPU(int, bpf_prog_active);
37 static DEFINE_IDR(prog_idr);
38 static DEFINE_SPINLOCK(prog_idr_lock);
39 static DEFINE_IDR(map_idr);
40 static DEFINE_SPINLOCK(map_idr_lock);
42 int sysctl_unprivileged_bpf_disabled __read_mostly;
44 static const struct bpf_map_ops * const bpf_map_types[] = {
45 #define BPF_PROG_TYPE(_id, _ops)
46 #define BPF_MAP_TYPE(_id, _ops) \
48 #include <linux/bpf_types.h>
54 * If we're handed a bigger struct than we know of, ensure all the unknown bits
55 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
56 * we don't know about yet.
58 * There is a ToCToU between this function call and the following
59 * copy_from_user() call. However, this is not a concern since this function is
60 * meant to be a future-proofing of bits.
62 int bpf_check_uarg_tail_zero(void __user *uaddr,
66 unsigned char __user *addr;
67 unsigned char __user *end;
71 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
74 if (unlikely(!access_ok(uaddr, actual_size)))
77 if (actual_size <= expected_size)
80 addr = uaddr + expected_size;
81 end = uaddr + actual_size;
83 for (; addr < end; addr++) {
84 err = get_user(val, addr);
94 const struct bpf_map_ops bpf_map_offload_ops = {
95 .map_alloc = bpf_map_offload_map_alloc,
96 .map_free = bpf_map_offload_map_free,
97 .map_check_btf = map_check_no_btf,
100 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
102 const struct bpf_map_ops *ops;
103 u32 type = attr->map_type;
107 if (type >= ARRAY_SIZE(bpf_map_types))
108 return ERR_PTR(-EINVAL);
109 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
110 ops = bpf_map_types[type];
112 return ERR_PTR(-EINVAL);
114 if (ops->map_alloc_check) {
115 err = ops->map_alloc_check(attr);
119 if (attr->map_ifindex)
120 ops = &bpf_map_offload_ops;
121 map = ops->map_alloc(attr);
125 map->map_type = type;
129 void *bpf_map_area_alloc(size_t size, int numa_node)
131 /* We really just want to fail instead of triggering OOM killer
132 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
133 * which is used for lower order allocation requests.
135 * It has been observed that higher order allocation requests done by
136 * vmalloc with __GFP_NORETRY being set might fail due to not trying
137 * to reclaim memory from the page cache, thus we set
138 * __GFP_RETRY_MAYFAIL to avoid such situations.
141 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
144 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
145 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
151 return __vmalloc_node_flags_caller(size, numa_node,
152 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
153 flags, __builtin_return_address(0));
156 void bpf_map_area_free(void *area)
161 static u32 bpf_map_flags_retain_permanent(u32 flags)
163 /* Some map creation flags are not tied to the map object but
164 * rather to the map fd instead, so they have no meaning upon
165 * map object inspection since multiple file descriptors with
166 * different (access) properties can exist here. Thus, given
167 * this has zero meaning for the map itself, lets clear these
170 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
173 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
175 map->map_type = attr->map_type;
176 map->key_size = attr->key_size;
177 map->value_size = attr->value_size;
178 map->max_entries = attr->max_entries;
179 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
180 map->numa_node = bpf_map_attr_numa_node(attr);
183 int bpf_map_precharge_memlock(u32 pages)
185 struct user_struct *user = get_current_user();
186 unsigned long memlock_limit, cur;
188 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
189 cur = atomic_long_read(&user->locked_vm);
191 if (cur + pages > memlock_limit)
196 static int bpf_charge_memlock(struct user_struct *user, u32 pages)
198 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
200 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
201 atomic_long_sub(pages, &user->locked_vm);
207 static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
209 atomic_long_sub(pages, &user->locked_vm);
212 static int bpf_map_init_memlock(struct bpf_map *map)
214 struct user_struct *user = get_current_user();
217 ret = bpf_charge_memlock(user, map->pages);
226 static void bpf_map_release_memlock(struct bpf_map *map)
228 struct user_struct *user = map->user;
229 bpf_uncharge_memlock(user, map->pages);
233 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
237 ret = bpf_charge_memlock(map->user, pages);
244 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
246 bpf_uncharge_memlock(map->user, pages);
250 static int bpf_map_alloc_id(struct bpf_map *map)
254 idr_preload(GFP_KERNEL);
255 spin_lock_bh(&map_idr_lock);
256 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
259 spin_unlock_bh(&map_idr_lock);
262 if (WARN_ON_ONCE(!id))
265 return id > 0 ? 0 : id;
268 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
272 /* Offloaded maps are removed from the IDR store when their device
273 * disappears - even if someone holds an fd to them they are unusable,
274 * the memory is gone, all ops will fail; they are simply waiting for
275 * refcnt to drop to be freed.
281 spin_lock_irqsave(&map_idr_lock, flags);
283 __acquire(&map_idr_lock);
285 idr_remove(&map_idr, map->id);
289 spin_unlock_irqrestore(&map_idr_lock, flags);
291 __release(&map_idr_lock);
294 /* called from workqueue */
295 static void bpf_map_free_deferred(struct work_struct *work)
297 struct bpf_map *map = container_of(work, struct bpf_map, work);
299 bpf_map_release_memlock(map);
300 security_bpf_map_free(map);
301 /* implementation dependent freeing */
302 map->ops->map_free(map);
305 static void bpf_map_put_uref(struct bpf_map *map)
307 if (atomic_dec_and_test(&map->usercnt)) {
308 if (map->ops->map_release_uref)
309 map->ops->map_release_uref(map);
313 /* decrement map refcnt and schedule it for freeing via workqueue
314 * (unrelying map implementation ops->map_free() might sleep)
316 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
318 if (atomic_dec_and_test(&map->refcnt)) {
319 /* bpf_map_free_id() must be called first */
320 bpf_map_free_id(map, do_idr_lock);
322 INIT_WORK(&map->work, bpf_map_free_deferred);
323 schedule_work(&map->work);
327 void bpf_map_put(struct bpf_map *map)
329 __bpf_map_put(map, true);
331 EXPORT_SYMBOL_GPL(bpf_map_put);
333 void bpf_map_put_with_uref(struct bpf_map *map)
335 bpf_map_put_uref(map);
339 static int bpf_map_release(struct inode *inode, struct file *filp)
341 struct bpf_map *map = filp->private_data;
343 if (map->ops->map_release)
344 map->ops->map_release(map, filp);
346 bpf_map_put_with_uref(map);
350 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
352 fmode_t mode = f.file->f_mode;
354 /* Our file permissions may have been overridden by global
355 * map permissions facing syscall side.
357 if (READ_ONCE(map->frozen))
358 mode &= ~FMODE_CAN_WRITE;
362 #ifdef CONFIG_PROC_FS
363 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
365 const struct bpf_map *map = filp->private_data;
366 const struct bpf_array *array;
367 u32 owner_prog_type = 0;
370 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
371 array = container_of(map, struct bpf_array, map);
372 owner_prog_type = array->owner_prog_type;
373 owner_jited = array->owner_jited;
390 map->pages * 1ULL << PAGE_SHIFT,
392 READ_ONCE(map->frozen));
394 if (owner_prog_type) {
395 seq_printf(m, "owner_prog_type:\t%u\n",
397 seq_printf(m, "owner_jited:\t%u\n",
403 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
406 /* We need this handler such that alloc_file() enables
407 * f_mode with FMODE_CAN_READ.
412 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
413 size_t siz, loff_t *ppos)
415 /* We need this handler such that alloc_file() enables
416 * f_mode with FMODE_CAN_WRITE.
421 const struct file_operations bpf_map_fops = {
422 #ifdef CONFIG_PROC_FS
423 .show_fdinfo = bpf_map_show_fdinfo,
425 .release = bpf_map_release,
426 .read = bpf_dummy_read,
427 .write = bpf_dummy_write,
430 int bpf_map_new_fd(struct bpf_map *map, int flags)
434 ret = security_bpf_map(map, OPEN_FMODE(flags));
438 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
442 int bpf_get_file_flag(int flags)
444 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
446 if (flags & BPF_F_RDONLY)
448 if (flags & BPF_F_WRONLY)
453 /* helper macro to check that unused fields 'union bpf_attr' are zero */
454 #define CHECK_ATTR(CMD) \
455 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
456 sizeof(attr->CMD##_LAST_FIELD), 0, \
458 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
459 sizeof(attr->CMD##_LAST_FIELD)) != NULL
461 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
462 * Return 0 on success and < 0 on error.
464 static int bpf_obj_name_cpy(char *dst, const char *src)
466 const char *end = src + BPF_OBJ_NAME_LEN;
468 memset(dst, 0, BPF_OBJ_NAME_LEN);
469 /* Copy all isalnum(), '_' and '.' chars. */
470 while (src < end && *src) {
471 if (!isalnum(*src) &&
472 *src != '_' && *src != '.')
477 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
484 int map_check_no_btf(const struct bpf_map *map,
485 const struct btf *btf,
486 const struct btf_type *key_type,
487 const struct btf_type *value_type)
492 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
493 u32 btf_key_id, u32 btf_value_id)
495 const struct btf_type *key_type, *value_type;
496 u32 key_size, value_size;
499 /* Some maps allow key to be unspecified. */
501 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
502 if (!key_type || key_size != map->key_size)
505 key_type = btf_type_by_id(btf, 0);
506 if (!map->ops->map_check_btf)
510 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
511 if (!value_type || value_size != map->value_size)
514 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
516 if (map_value_has_spin_lock(map)) {
517 if (map->map_flags & BPF_F_RDONLY_PROG)
519 if (map->map_type != BPF_MAP_TYPE_HASH &&
520 map->map_type != BPF_MAP_TYPE_ARRAY &&
521 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
522 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
524 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
527 "verifier bug spin_lock_off %d value_size %d\n",
528 map->spin_lock_off, map->value_size);
533 if (map->ops->map_check_btf)
534 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
539 #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
540 /* called via syscall */
541 static int map_create(union bpf_attr *attr)
543 int numa_node = bpf_map_attr_numa_node(attr);
548 err = CHECK_ATTR(BPF_MAP_CREATE);
552 f_flags = bpf_get_file_flag(attr->map_flags);
556 if (numa_node != NUMA_NO_NODE &&
557 ((unsigned int)numa_node >= nr_node_ids ||
558 !node_online(numa_node)))
561 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
562 map = find_and_alloc_map(attr);
566 err = bpf_obj_name_cpy(map->name, attr->map_name);
568 goto free_map_nouncharge;
570 atomic_set(&map->refcnt, 1);
571 atomic_set(&map->usercnt, 1);
573 if (attr->btf_key_type_id || attr->btf_value_type_id) {
576 if (!attr->btf_value_type_id) {
578 goto free_map_nouncharge;
581 btf = btf_get_by_fd(attr->btf_fd);
584 goto free_map_nouncharge;
587 err = map_check_btf(map, btf, attr->btf_key_type_id,
588 attr->btf_value_type_id);
591 goto free_map_nouncharge;
595 map->btf_key_type_id = attr->btf_key_type_id;
596 map->btf_value_type_id = attr->btf_value_type_id;
598 map->spin_lock_off = -EINVAL;
601 err = security_bpf_map_alloc(map);
603 goto free_map_nouncharge;
605 err = bpf_map_init_memlock(map);
609 err = bpf_map_alloc_id(map);
613 err = bpf_map_new_fd(map, f_flags);
615 /* failed to allocate fd.
616 * bpf_map_put_with_uref() is needed because the above
617 * bpf_map_alloc_id() has published the map
618 * to the userspace and the userspace may
619 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
621 bpf_map_put_with_uref(map);
628 bpf_map_release_memlock(map);
630 security_bpf_map_free(map);
633 map->ops->map_free(map);
637 /* if error is returned, fd is released.
638 * On success caller should complete fd access with matching fdput()
640 struct bpf_map *__bpf_map_get(struct fd f)
643 return ERR_PTR(-EBADF);
644 if (f.file->f_op != &bpf_map_fops) {
646 return ERR_PTR(-EINVAL);
649 return f.file->private_data;
652 /* prog's and map's refcnt limit */
653 #define BPF_MAX_REFCNT 32768
655 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
657 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
658 atomic_dec(&map->refcnt);
659 return ERR_PTR(-EBUSY);
662 atomic_inc(&map->usercnt);
665 EXPORT_SYMBOL_GPL(bpf_map_inc);
667 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
669 struct fd f = fdget(ufd);
672 map = __bpf_map_get(f);
676 map = bpf_map_inc(map, true);
682 /* map_idr_lock should have been held */
683 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
688 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
690 if (refold >= BPF_MAX_REFCNT) {
691 __bpf_map_put(map, false);
692 return ERR_PTR(-EBUSY);
696 return ERR_PTR(-ENOENT);
699 atomic_inc(&map->usercnt);
704 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
709 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
712 return memdup_user(ukey, key_size);
715 return ERR_PTR(-EINVAL);
720 /* last field in 'union bpf_attr' used by this command */
721 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
723 static int map_lookup_elem(union bpf_attr *attr)
725 void __user *ukey = u64_to_user_ptr(attr->key);
726 void __user *uvalue = u64_to_user_ptr(attr->value);
727 int ufd = attr->map_fd;
729 void *key, *value, *ptr;
734 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
737 if (attr->flags & ~BPF_F_LOCK)
741 map = __bpf_map_get(f);
744 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
749 if ((attr->flags & BPF_F_LOCK) &&
750 !map_value_has_spin_lock(map)) {
755 key = __bpf_copy_key(ukey, map->key_size);
761 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
762 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
763 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
764 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
765 value_size = round_up(map->value_size, 8) * num_possible_cpus();
766 else if (IS_FD_MAP(map))
767 value_size = sizeof(u32);
769 value_size = map->value_size;
772 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
776 if (bpf_map_is_dev_bound(map)) {
777 err = bpf_map_offload_lookup_elem(map, key, value);
782 this_cpu_inc(bpf_prog_active);
783 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
784 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
785 err = bpf_percpu_hash_copy(map, key, value);
786 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
787 err = bpf_percpu_array_copy(map, key, value);
788 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
789 err = bpf_percpu_cgroup_storage_copy(map, key, value);
790 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
791 err = bpf_stackmap_copy(map, key, value);
792 } else if (IS_FD_ARRAY(map)) {
793 err = bpf_fd_array_map_lookup_elem(map, key, value);
794 } else if (IS_FD_HASH(map)) {
795 err = bpf_fd_htab_map_lookup_elem(map, key, value);
796 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
797 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
798 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
799 map->map_type == BPF_MAP_TYPE_STACK) {
800 err = map->ops->map_peek_elem(map, value);
803 if (map->ops->map_lookup_elem_sys_only)
804 ptr = map->ops->map_lookup_elem_sys_only(map, key);
806 ptr = map->ops->map_lookup_elem(map, key);
813 if (attr->flags & BPF_F_LOCK)
814 /* lock 'ptr' and copy everything but lock */
815 copy_map_value_locked(map, value, ptr, true);
817 copy_map_value(map, value, ptr);
818 /* mask lock, since value wasn't zero inited */
819 check_and_init_map_lock(map, value);
823 this_cpu_dec(bpf_prog_active);
831 if (copy_to_user(uvalue, value, value_size) != 0)
845 static void maybe_wait_bpf_programs(struct bpf_map *map)
847 /* Wait for any running BPF programs to complete so that
848 * userspace, when we return to it, knows that all programs
849 * that could be running use the new map value.
851 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
852 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
856 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
858 static int map_update_elem(union bpf_attr *attr)
860 void __user *ukey = u64_to_user_ptr(attr->key);
861 void __user *uvalue = u64_to_user_ptr(attr->value);
862 int ufd = attr->map_fd;
869 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
873 map = __bpf_map_get(f);
876 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
881 if ((attr->flags & BPF_F_LOCK) &&
882 !map_value_has_spin_lock(map)) {
887 key = __bpf_copy_key(ukey, map->key_size);
893 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
894 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
895 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
896 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
897 value_size = round_up(map->value_size, 8) * num_possible_cpus();
899 value_size = map->value_size;
902 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
907 if (copy_from_user(value, uvalue, value_size) != 0)
910 /* Need to create a kthread, thus must support schedule */
911 if (bpf_map_is_dev_bound(map)) {
912 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
914 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
915 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
916 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
917 err = map->ops->map_update_elem(map, key, value, attr->flags);
921 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
922 * inside bpf map update or delete otherwise deadlocks are possible
925 __this_cpu_inc(bpf_prog_active);
926 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
927 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
928 err = bpf_percpu_hash_update(map, key, value, attr->flags);
929 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
930 err = bpf_percpu_array_update(map, key, value, attr->flags);
931 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
932 err = bpf_percpu_cgroup_storage_update(map, key, value,
934 } else if (IS_FD_ARRAY(map)) {
936 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
939 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
941 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
944 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
945 /* rcu_read_lock() is not needed */
946 err = bpf_fd_reuseport_array_update_elem(map, key, value,
948 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
949 map->map_type == BPF_MAP_TYPE_STACK) {
950 err = map->ops->map_push_elem(map, value, attr->flags);
953 err = map->ops->map_update_elem(map, key, value, attr->flags);
956 __this_cpu_dec(bpf_prog_active);
958 maybe_wait_bpf_programs(map);
969 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
971 static int map_delete_elem(union bpf_attr *attr)
973 void __user *ukey = u64_to_user_ptr(attr->key);
974 int ufd = attr->map_fd;
980 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
984 map = __bpf_map_get(f);
987 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
992 key = __bpf_copy_key(ukey, map->key_size);
998 if (bpf_map_is_dev_bound(map)) {
999 err = bpf_map_offload_delete_elem(map, key);
1004 __this_cpu_inc(bpf_prog_active);
1006 err = map->ops->map_delete_elem(map, key);
1008 __this_cpu_dec(bpf_prog_active);
1010 maybe_wait_bpf_programs(map);
1018 /* last field in 'union bpf_attr' used by this command */
1019 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1021 static int map_get_next_key(union bpf_attr *attr)
1023 void __user *ukey = u64_to_user_ptr(attr->key);
1024 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1025 int ufd = attr->map_fd;
1026 struct bpf_map *map;
1027 void *key, *next_key;
1031 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1035 map = __bpf_map_get(f);
1037 return PTR_ERR(map);
1038 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1044 key = __bpf_copy_key(ukey, map->key_size);
1054 next_key = kmalloc(map->key_size, GFP_USER);
1058 if (bpf_map_is_dev_bound(map)) {
1059 err = bpf_map_offload_get_next_key(map, key, next_key);
1064 err = map->ops->map_get_next_key(map, key, next_key);
1071 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1085 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1087 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1089 void __user *ukey = u64_to_user_ptr(attr->key);
1090 void __user *uvalue = u64_to_user_ptr(attr->value);
1091 int ufd = attr->map_fd;
1092 struct bpf_map *map;
1098 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1102 map = __bpf_map_get(f);
1104 return PTR_ERR(map);
1105 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1110 key = __bpf_copy_key(ukey, map->key_size);
1116 value_size = map->value_size;
1119 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1123 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1124 map->map_type == BPF_MAP_TYPE_STACK) {
1125 err = map->ops->map_pop_elem(map, value);
1133 if (copy_to_user(uvalue, value, value_size) != 0)
1147 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1149 static int map_freeze(const union bpf_attr *attr)
1151 int err = 0, ufd = attr->map_fd;
1152 struct bpf_map *map;
1155 if (CHECK_ATTR(BPF_MAP_FREEZE))
1159 map = __bpf_map_get(f);
1161 return PTR_ERR(map);
1162 if (READ_ONCE(map->frozen)) {
1166 if (!capable(CAP_SYS_ADMIN)) {
1171 WRITE_ONCE(map->frozen, true);
1177 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1178 #define BPF_PROG_TYPE(_id, _name) \
1179 [_id] = & _name ## _prog_ops,
1180 #define BPF_MAP_TYPE(_id, _ops)
1181 #include <linux/bpf_types.h>
1182 #undef BPF_PROG_TYPE
1186 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1188 const struct bpf_prog_ops *ops;
1190 if (type >= ARRAY_SIZE(bpf_prog_types))
1192 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1193 ops = bpf_prog_types[type];
1197 if (!bpf_prog_is_dev_bound(prog->aux))
1198 prog->aux->ops = ops;
1200 prog->aux->ops = &bpf_offload_prog_ops;
1205 /* drop refcnt on maps used by eBPF program and free auxilary data */
1206 static void free_used_maps(struct bpf_prog_aux *aux)
1208 enum bpf_cgroup_storage_type stype;
1211 for_each_cgroup_storage_type(stype) {
1212 if (!aux->cgroup_storage[stype])
1214 bpf_cgroup_storage_release(aux->prog,
1215 aux->cgroup_storage[stype]);
1218 for (i = 0; i < aux->used_map_cnt; i++)
1219 bpf_map_put(aux->used_maps[i]);
1221 kfree(aux->used_maps);
1224 int __bpf_prog_charge(struct user_struct *user, u32 pages)
1226 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1227 unsigned long user_bufs;
1230 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1231 if (user_bufs > memlock_limit) {
1232 atomic_long_sub(pages, &user->locked_vm);
1240 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1243 atomic_long_sub(pages, &user->locked_vm);
1246 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1248 struct user_struct *user = get_current_user();
1251 ret = __bpf_prog_charge(user, prog->pages);
1257 prog->aux->user = user;
1261 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1263 struct user_struct *user = prog->aux->user;
1265 __bpf_prog_uncharge(user, prog->pages);
1269 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1273 idr_preload(GFP_KERNEL);
1274 spin_lock_bh(&prog_idr_lock);
1275 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1278 spin_unlock_bh(&prog_idr_lock);
1281 /* id is in [1, INT_MAX) */
1282 if (WARN_ON_ONCE(!id))
1285 return id > 0 ? 0 : id;
1288 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1290 /* cBPF to eBPF migrations are currently not in the idr store.
1291 * Offloaded programs are removed from the store when their device
1292 * disappears - even if someone grabs an fd to them they are unusable,
1293 * simply waiting for refcnt to drop to be freed.
1299 spin_lock_bh(&prog_idr_lock);
1301 __acquire(&prog_idr_lock);
1303 idr_remove(&prog_idr, prog->aux->id);
1307 spin_unlock_bh(&prog_idr_lock);
1309 __release(&prog_idr_lock);
1312 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1314 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1316 free_used_maps(aux);
1317 bpf_prog_uncharge_memlock(aux->prog);
1318 security_bpf_prog_free(aux);
1319 bpf_prog_free(aux->prog);
1322 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1324 if (atomic_dec_and_test(&prog->aux->refcnt)) {
1325 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1326 /* bpf_prog_free_id() must be called first */
1327 bpf_prog_free_id(prog, do_idr_lock);
1328 bpf_prog_kallsyms_del_all(prog);
1329 btf_put(prog->aux->btf);
1330 kvfree(prog->aux->func_info);
1331 bpf_prog_free_linfo(prog);
1333 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1337 void bpf_prog_put(struct bpf_prog *prog)
1339 __bpf_prog_put(prog, true);
1341 EXPORT_SYMBOL_GPL(bpf_prog_put);
1343 static int bpf_prog_release(struct inode *inode, struct file *filp)
1345 struct bpf_prog *prog = filp->private_data;
1351 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1352 struct bpf_prog_stats *stats)
1354 u64 nsecs = 0, cnt = 0;
1357 for_each_possible_cpu(cpu) {
1358 const struct bpf_prog_stats *st;
1362 st = per_cpu_ptr(prog->aux->stats, cpu);
1364 start = u64_stats_fetch_begin_irq(&st->syncp);
1367 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1371 stats->nsecs = nsecs;
1375 #ifdef CONFIG_PROC_FS
1376 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1378 const struct bpf_prog *prog = filp->private_data;
1379 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1380 struct bpf_prog_stats stats;
1382 bpf_prog_get_stats(prog, &stats);
1383 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1390 "run_time_ns:\t%llu\n"
1395 prog->pages * 1ULL << PAGE_SHIFT,
1402 const struct file_operations bpf_prog_fops = {
1403 #ifdef CONFIG_PROC_FS
1404 .show_fdinfo = bpf_prog_show_fdinfo,
1406 .release = bpf_prog_release,
1407 .read = bpf_dummy_read,
1408 .write = bpf_dummy_write,
1411 int bpf_prog_new_fd(struct bpf_prog *prog)
1415 ret = security_bpf_prog(prog);
1419 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1420 O_RDWR | O_CLOEXEC);
1423 static struct bpf_prog *____bpf_prog_get(struct fd f)
1426 return ERR_PTR(-EBADF);
1427 if (f.file->f_op != &bpf_prog_fops) {
1429 return ERR_PTR(-EINVAL);
1432 return f.file->private_data;
1435 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
1437 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1438 atomic_sub(i, &prog->aux->refcnt);
1439 return ERR_PTR(-EBUSY);
1443 EXPORT_SYMBOL_GPL(bpf_prog_add);
1445 void bpf_prog_sub(struct bpf_prog *prog, int i)
1447 /* Only to be used for undoing previous bpf_prog_add() in some
1448 * error path. We still know that another entity in our call
1449 * path holds a reference to the program, thus atomic_sub() can
1450 * be safely used in such cases!
1452 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1454 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1456 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1458 return bpf_prog_add(prog, 1);
1460 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1462 /* prog_idr_lock should have been held */
1463 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1467 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1469 if (refold >= BPF_MAX_REFCNT) {
1470 __bpf_prog_put(prog, false);
1471 return ERR_PTR(-EBUSY);
1475 return ERR_PTR(-ENOENT);
1479 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1481 bool bpf_prog_get_ok(struct bpf_prog *prog,
1482 enum bpf_prog_type *attach_type, bool attach_drv)
1484 /* not an attachment, just a refcount inc, always allow */
1488 if (prog->type != *attach_type)
1490 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1496 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1499 struct fd f = fdget(ufd);
1500 struct bpf_prog *prog;
1502 prog = ____bpf_prog_get(f);
1505 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1506 prog = ERR_PTR(-EINVAL);
1510 prog = bpf_prog_inc(prog);
1516 struct bpf_prog *bpf_prog_get(u32 ufd)
1518 return __bpf_prog_get(ufd, NULL, false);
1521 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1524 return __bpf_prog_get(ufd, &type, attach_drv);
1526 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1528 /* Initially all BPF programs could be loaded w/o specifying
1529 * expected_attach_type. Later for some of them specifying expected_attach_type
1530 * at load time became required so that program could be validated properly.
1531 * Programs of types that are allowed to be loaded both w/ and w/o (for
1532 * backward compatibility) expected_attach_type, should have the default attach
1533 * type assigned to expected_attach_type for the latter case, so that it can be
1534 * validated later at attach time.
1536 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1537 * prog type requires it but has some attach types that have to be backward
1540 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1542 switch (attr->prog_type) {
1543 case BPF_PROG_TYPE_CGROUP_SOCK:
1544 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1545 * exist so checking for non-zero is the way to go here.
1547 if (!attr->expected_attach_type)
1548 attr->expected_attach_type =
1549 BPF_CGROUP_INET_SOCK_CREATE;
1555 bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1556 enum bpf_attach_type expected_attach_type)
1558 switch (prog_type) {
1559 case BPF_PROG_TYPE_CGROUP_SOCK:
1560 switch (expected_attach_type) {
1561 case BPF_CGROUP_INET_SOCK_CREATE:
1562 case BPF_CGROUP_INET4_POST_BIND:
1563 case BPF_CGROUP_INET6_POST_BIND:
1568 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1569 switch (expected_attach_type) {
1570 case BPF_CGROUP_INET4_BIND:
1571 case BPF_CGROUP_INET6_BIND:
1572 case BPF_CGROUP_INET4_CONNECT:
1573 case BPF_CGROUP_INET6_CONNECT:
1574 case BPF_CGROUP_UDP4_SENDMSG:
1575 case BPF_CGROUP_UDP6_SENDMSG:
1585 /* last field in 'union bpf_attr' used by this command */
1586 #define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
1588 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
1590 enum bpf_prog_type type = attr->prog_type;
1591 struct bpf_prog *prog;
1596 if (CHECK_ATTR(BPF_PROG_LOAD))
1599 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | BPF_F_ANY_ALIGNMENT))
1602 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1603 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1604 !capable(CAP_SYS_ADMIN))
1607 /* copy eBPF program license from user space */
1608 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1609 sizeof(license) - 1) < 0)
1611 license[sizeof(license) - 1] = 0;
1613 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1614 is_gpl = license_is_gpl_compatible(license);
1616 if (attr->insn_cnt == 0 ||
1617 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
1619 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1620 type != BPF_PROG_TYPE_CGROUP_SKB &&
1621 !capable(CAP_SYS_ADMIN))
1624 bpf_prog_load_fixup_attach_type(attr);
1625 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1628 /* plain bpf_prog allocation */
1629 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1633 prog->expected_attach_type = attr->expected_attach_type;
1635 prog->aux->offload_requested = !!attr->prog_ifindex;
1637 err = security_bpf_prog_alloc(prog->aux);
1639 goto free_prog_nouncharge;
1641 err = bpf_prog_charge_memlock(prog);
1645 prog->len = attr->insn_cnt;
1648 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1649 bpf_prog_insn_size(prog)) != 0)
1652 prog->orig_prog = NULL;
1655 atomic_set(&prog->aux->refcnt, 1);
1656 prog->gpl_compatible = is_gpl ? 1 : 0;
1658 if (bpf_prog_is_dev_bound(prog->aux)) {
1659 err = bpf_prog_offload_init(prog, attr);
1664 /* find program type: socket_filter vs tracing_filter */
1665 err = find_prog_type(type, prog);
1669 prog->aux->load_time = ktime_get_boot_ns();
1670 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1674 /* run eBPF verifier */
1675 err = bpf_check(&prog, attr, uattr);
1677 goto free_used_maps;
1679 prog = bpf_prog_select_runtime(prog, &err);
1681 goto free_used_maps;
1683 err = bpf_prog_alloc_id(prog);
1685 goto free_used_maps;
1687 err = bpf_prog_new_fd(prog);
1689 /* failed to allocate fd.
1690 * bpf_prog_put() is needed because the above
1691 * bpf_prog_alloc_id() has published the prog
1692 * to the userspace and the userspace may
1693 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1699 bpf_prog_kallsyms_add(prog);
1700 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
1704 bpf_prog_free_linfo(prog);
1705 kvfree(prog->aux->func_info);
1706 btf_put(prog->aux->btf);
1707 bpf_prog_kallsyms_del_subprogs(prog);
1708 free_used_maps(prog->aux);
1710 bpf_prog_uncharge_memlock(prog);
1712 security_bpf_prog_free(prog->aux);
1713 free_prog_nouncharge:
1714 bpf_prog_free(prog);
1718 #define BPF_OBJ_LAST_FIELD file_flags
1720 static int bpf_obj_pin(const union bpf_attr *attr)
1722 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1725 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1728 static int bpf_obj_get(const union bpf_attr *attr)
1730 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1731 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1734 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1738 struct bpf_raw_tracepoint {
1739 struct bpf_raw_event_map *btp;
1740 struct bpf_prog *prog;
1743 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1745 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1748 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1749 bpf_prog_put(raw_tp->prog);
1751 bpf_put_raw_tracepoint(raw_tp->btp);
1756 static const struct file_operations bpf_raw_tp_fops = {
1757 .release = bpf_raw_tracepoint_release,
1758 .read = bpf_dummy_read,
1759 .write = bpf_dummy_write,
1762 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1764 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1766 struct bpf_raw_tracepoint *raw_tp;
1767 struct bpf_raw_event_map *btp;
1768 struct bpf_prog *prog;
1772 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1773 sizeof(tp_name) - 1) < 0)
1775 tp_name[sizeof(tp_name) - 1] = 0;
1777 btp = bpf_get_raw_tracepoint(tp_name);
1781 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1788 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
1790 err = PTR_ERR(prog);
1793 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1794 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
1799 err = bpf_probe_register(raw_tp->btp, prog);
1803 raw_tp->prog = prog;
1804 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1807 bpf_probe_unregister(raw_tp->btp, prog);
1818 bpf_put_raw_tracepoint(btp);
1822 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1823 enum bpf_attach_type attach_type)
1825 switch (prog->type) {
1826 case BPF_PROG_TYPE_CGROUP_SOCK:
1827 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1828 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1834 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1836 #define BPF_F_ATTACH_MASK \
1837 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1839 static int bpf_prog_attach(const union bpf_attr *attr)
1841 enum bpf_prog_type ptype;
1842 struct bpf_prog *prog;
1845 if (!capable(CAP_NET_ADMIN))
1848 if (CHECK_ATTR(BPF_PROG_ATTACH))
1851 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
1854 switch (attr->attach_type) {
1855 case BPF_CGROUP_INET_INGRESS:
1856 case BPF_CGROUP_INET_EGRESS:
1857 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1859 case BPF_CGROUP_INET_SOCK_CREATE:
1860 case BPF_CGROUP_INET4_POST_BIND:
1861 case BPF_CGROUP_INET6_POST_BIND:
1862 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1864 case BPF_CGROUP_INET4_BIND:
1865 case BPF_CGROUP_INET6_BIND:
1866 case BPF_CGROUP_INET4_CONNECT:
1867 case BPF_CGROUP_INET6_CONNECT:
1868 case BPF_CGROUP_UDP4_SENDMSG:
1869 case BPF_CGROUP_UDP6_SENDMSG:
1870 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1872 case BPF_CGROUP_SOCK_OPS:
1873 ptype = BPF_PROG_TYPE_SOCK_OPS;
1875 case BPF_CGROUP_DEVICE:
1876 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1878 case BPF_SK_MSG_VERDICT:
1879 ptype = BPF_PROG_TYPE_SK_MSG;
1881 case BPF_SK_SKB_STREAM_PARSER:
1882 case BPF_SK_SKB_STREAM_VERDICT:
1883 ptype = BPF_PROG_TYPE_SK_SKB;
1885 case BPF_LIRC_MODE2:
1886 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1888 case BPF_FLOW_DISSECTOR:
1889 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1891 case BPF_CGROUP_SYSCTL:
1892 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1898 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1900 return PTR_ERR(prog);
1902 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1908 case BPF_PROG_TYPE_SK_SKB:
1909 case BPF_PROG_TYPE_SK_MSG:
1910 ret = sock_map_get_from_fd(attr, prog);
1912 case BPF_PROG_TYPE_LIRC_MODE2:
1913 ret = lirc_prog_attach(attr, prog);
1915 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1916 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1919 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
1927 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1929 static int bpf_prog_detach(const union bpf_attr *attr)
1931 enum bpf_prog_type ptype;
1933 if (!capable(CAP_NET_ADMIN))
1936 if (CHECK_ATTR(BPF_PROG_DETACH))
1939 switch (attr->attach_type) {
1940 case BPF_CGROUP_INET_INGRESS:
1941 case BPF_CGROUP_INET_EGRESS:
1942 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1944 case BPF_CGROUP_INET_SOCK_CREATE:
1945 case BPF_CGROUP_INET4_POST_BIND:
1946 case BPF_CGROUP_INET6_POST_BIND:
1947 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1949 case BPF_CGROUP_INET4_BIND:
1950 case BPF_CGROUP_INET6_BIND:
1951 case BPF_CGROUP_INET4_CONNECT:
1952 case BPF_CGROUP_INET6_CONNECT:
1953 case BPF_CGROUP_UDP4_SENDMSG:
1954 case BPF_CGROUP_UDP6_SENDMSG:
1955 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1957 case BPF_CGROUP_SOCK_OPS:
1958 ptype = BPF_PROG_TYPE_SOCK_OPS;
1960 case BPF_CGROUP_DEVICE:
1961 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1963 case BPF_SK_MSG_VERDICT:
1964 return sock_map_get_from_fd(attr, NULL);
1965 case BPF_SK_SKB_STREAM_PARSER:
1966 case BPF_SK_SKB_STREAM_VERDICT:
1967 return sock_map_get_from_fd(attr, NULL);
1968 case BPF_LIRC_MODE2:
1969 return lirc_prog_detach(attr);
1970 case BPF_FLOW_DISSECTOR:
1971 return skb_flow_dissector_bpf_prog_detach(attr);
1972 case BPF_CGROUP_SYSCTL:
1973 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1979 return cgroup_bpf_prog_detach(attr, ptype);
1982 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1984 static int bpf_prog_query(const union bpf_attr *attr,
1985 union bpf_attr __user *uattr)
1987 if (!capable(CAP_NET_ADMIN))
1989 if (CHECK_ATTR(BPF_PROG_QUERY))
1991 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1994 switch (attr->query.attach_type) {
1995 case BPF_CGROUP_INET_INGRESS:
1996 case BPF_CGROUP_INET_EGRESS:
1997 case BPF_CGROUP_INET_SOCK_CREATE:
1998 case BPF_CGROUP_INET4_BIND:
1999 case BPF_CGROUP_INET6_BIND:
2000 case BPF_CGROUP_INET4_POST_BIND:
2001 case BPF_CGROUP_INET6_POST_BIND:
2002 case BPF_CGROUP_INET4_CONNECT:
2003 case BPF_CGROUP_INET6_CONNECT:
2004 case BPF_CGROUP_UDP4_SENDMSG:
2005 case BPF_CGROUP_UDP6_SENDMSG:
2006 case BPF_CGROUP_SOCK_OPS:
2007 case BPF_CGROUP_DEVICE:
2008 case BPF_CGROUP_SYSCTL:
2010 case BPF_LIRC_MODE2:
2011 return lirc_prog_query(attr, uattr);
2012 case BPF_FLOW_DISSECTOR:
2013 return skb_flow_dissector_prog_query(attr, uattr);
2018 return cgroup_bpf_prog_query(attr, uattr);
2021 #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
2023 static int bpf_prog_test_run(const union bpf_attr *attr,
2024 union bpf_attr __user *uattr)
2026 struct bpf_prog *prog;
2027 int ret = -ENOTSUPP;
2029 if (!capable(CAP_SYS_ADMIN))
2031 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2034 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2035 (!attr->test.ctx_size_in && attr->test.ctx_in))
2038 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2039 (!attr->test.ctx_size_out && attr->test.ctx_out))
2042 prog = bpf_prog_get(attr->test.prog_fd);
2044 return PTR_ERR(prog);
2046 if (prog->aux->ops->test_run)
2047 ret = prog->aux->ops->test_run(prog, attr, uattr);
2053 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2055 static int bpf_obj_get_next_id(const union bpf_attr *attr,
2056 union bpf_attr __user *uattr,
2060 u32 next_id = attr->start_id;
2063 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2066 if (!capable(CAP_SYS_ADMIN))
2071 if (!idr_get_next(idr, &next_id))
2073 spin_unlock_bh(lock);
2076 err = put_user(next_id, &uattr->next_id);
2081 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2083 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2085 struct bpf_prog *prog;
2086 u32 id = attr->prog_id;
2089 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2092 if (!capable(CAP_SYS_ADMIN))
2095 spin_lock_bh(&prog_idr_lock);
2096 prog = idr_find(&prog_idr, id);
2098 prog = bpf_prog_inc_not_zero(prog);
2100 prog = ERR_PTR(-ENOENT);
2101 spin_unlock_bh(&prog_idr_lock);
2104 return PTR_ERR(prog);
2106 fd = bpf_prog_new_fd(prog);
2113 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2115 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2117 struct bpf_map *map;
2118 u32 id = attr->map_id;
2122 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2123 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
2126 if (!capable(CAP_SYS_ADMIN))
2129 f_flags = bpf_get_file_flag(attr->open_flags);
2133 spin_lock_bh(&map_idr_lock);
2134 map = idr_find(&map_idr, id);
2136 map = bpf_map_inc_not_zero(map, true);
2138 map = ERR_PTR(-ENOENT);
2139 spin_unlock_bh(&map_idr_lock);
2142 return PTR_ERR(map);
2144 fd = bpf_map_new_fd(map, f_flags);
2146 bpf_map_put_with_uref(map);
2151 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
2152 unsigned long addr, u32 *off,
2155 const struct bpf_map *map;
2158 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2159 map = prog->aux->used_maps[i];
2160 if (map == (void *)addr) {
2161 *type = BPF_PSEUDO_MAP_FD;
2164 if (!map->ops->map_direct_value_meta)
2166 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2167 *type = BPF_PSEUDO_MAP_VALUE;
2175 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2177 const struct bpf_map *map;
2178 struct bpf_insn *insns;
2183 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2188 for (i = 0; i < prog->len; i++) {
2189 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2190 insns[i].code = BPF_JMP | BPF_CALL;
2191 insns[i].imm = BPF_FUNC_tail_call;
2194 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2195 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2196 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2197 insns[i].code = BPF_JMP | BPF_CALL;
2198 if (!bpf_dump_raw_ok())
2203 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2206 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
2207 map = bpf_map_from_imm(prog, imm, &off, &type);
2209 insns[i].src_reg = type;
2210 insns[i].imm = map->id;
2211 insns[i + 1].imm = off;
2219 static int set_info_rec_size(struct bpf_prog_info *info)
2222 * Ensure info.*_rec_size is the same as kernel expected size
2226 * Only allow zero *_rec_size if both _rec_size and _cnt are
2227 * zero. In this case, the kernel will set the expected
2228 * _rec_size back to the info.
2231 if ((info->nr_func_info || info->func_info_rec_size) &&
2232 info->func_info_rec_size != sizeof(struct bpf_func_info))
2235 if ((info->nr_line_info || info->line_info_rec_size) &&
2236 info->line_info_rec_size != sizeof(struct bpf_line_info))
2239 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
2240 info->jited_line_info_rec_size != sizeof(__u64))
2243 info->func_info_rec_size = sizeof(struct bpf_func_info);
2244 info->line_info_rec_size = sizeof(struct bpf_line_info);
2245 info->jited_line_info_rec_size = sizeof(__u64);
2250 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2251 const union bpf_attr *attr,
2252 union bpf_attr __user *uattr)
2254 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2255 struct bpf_prog_info info = {};
2256 u32 info_len = attr->info.info_len;
2257 struct bpf_prog_stats stats;
2258 char __user *uinsns;
2262 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2265 info_len = min_t(u32, sizeof(info), info_len);
2267 if (copy_from_user(&info, uinfo, info_len))
2270 info.type = prog->type;
2271 info.id = prog->aux->id;
2272 info.load_time = prog->aux->load_time;
2273 info.created_by_uid = from_kuid_munged(current_user_ns(),
2274 prog->aux->user->uid);
2275 info.gpl_compatible = prog->gpl_compatible;
2277 memcpy(info.tag, prog->tag, sizeof(prog->tag));
2278 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2280 ulen = info.nr_map_ids;
2281 info.nr_map_ids = prog->aux->used_map_cnt;
2282 ulen = min_t(u32, info.nr_map_ids, ulen);
2284 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
2287 for (i = 0; i < ulen; i++)
2288 if (put_user(prog->aux->used_maps[i]->id,
2293 err = set_info_rec_size(&info);
2297 bpf_prog_get_stats(prog, &stats);
2298 info.run_time_ns = stats.nsecs;
2299 info.run_cnt = stats.cnt;
2301 if (!capable(CAP_SYS_ADMIN)) {
2302 info.jited_prog_len = 0;
2303 info.xlated_prog_len = 0;
2304 info.nr_jited_ksyms = 0;
2305 info.nr_jited_func_lens = 0;
2306 info.nr_func_info = 0;
2307 info.nr_line_info = 0;
2308 info.nr_jited_line_info = 0;
2312 ulen = info.xlated_prog_len;
2313 info.xlated_prog_len = bpf_prog_insn_size(prog);
2314 if (info.xlated_prog_len && ulen) {
2315 struct bpf_insn *insns_sanitized;
2318 if (prog->blinded && !bpf_dump_raw_ok()) {
2319 info.xlated_prog_insns = 0;
2322 insns_sanitized = bpf_insn_prepare_dump(prog);
2323 if (!insns_sanitized)
2325 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2326 ulen = min_t(u32, info.xlated_prog_len, ulen);
2327 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2328 kfree(insns_sanitized);
2333 if (bpf_prog_is_dev_bound(prog->aux)) {
2334 err = bpf_prog_offload_info_fill(&info, prog);
2340 /* NOTE: the following code is supposed to be skipped for offload.
2341 * bpf_prog_offload_info_fill() is the place to fill similar fields
2344 ulen = info.jited_prog_len;
2345 if (prog->aux->func_cnt) {
2348 info.jited_prog_len = 0;
2349 for (i = 0; i < prog->aux->func_cnt; i++)
2350 info.jited_prog_len += prog->aux->func[i]->jited_len;
2352 info.jited_prog_len = prog->jited_len;
2355 if (info.jited_prog_len && ulen) {
2356 if (bpf_dump_raw_ok()) {
2357 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2358 ulen = min_t(u32, info.jited_prog_len, ulen);
2360 /* for multi-function programs, copy the JITed
2361 * instructions for all the functions
2363 if (prog->aux->func_cnt) {
2368 for (i = 0; i < prog->aux->func_cnt; i++) {
2369 len = prog->aux->func[i]->jited_len;
2370 len = min_t(u32, len, free);
2371 img = (u8 *) prog->aux->func[i]->bpf_func;
2372 if (copy_to_user(uinsns, img, len))
2380 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2384 info.jited_prog_insns = 0;
2388 ulen = info.nr_jited_ksyms;
2389 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
2391 if (bpf_dump_raw_ok()) {
2392 unsigned long ksym_addr;
2393 u64 __user *user_ksyms;
2396 /* copy the address of the kernel symbol
2397 * corresponding to each function
2399 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2400 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2401 if (prog->aux->func_cnt) {
2402 for (i = 0; i < ulen; i++) {
2403 ksym_addr = (unsigned long)
2404 prog->aux->func[i]->bpf_func;
2405 if (put_user((u64) ksym_addr,
2410 ksym_addr = (unsigned long) prog->bpf_func;
2411 if (put_user((u64) ksym_addr, &user_ksyms[0]))
2415 info.jited_ksyms = 0;
2419 ulen = info.nr_jited_func_lens;
2420 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
2422 if (bpf_dump_raw_ok()) {
2423 u32 __user *user_lens;
2426 /* copy the JITed image lengths for each function */
2427 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2428 user_lens = u64_to_user_ptr(info.jited_func_lens);
2429 if (prog->aux->func_cnt) {
2430 for (i = 0; i < ulen; i++) {
2432 prog->aux->func[i]->jited_len;
2433 if (put_user(func_len, &user_lens[i]))
2437 func_len = prog->jited_len;
2438 if (put_user(func_len, &user_lens[0]))
2442 info.jited_func_lens = 0;
2447 info.btf_id = btf_id(prog->aux->btf);
2449 ulen = info.nr_func_info;
2450 info.nr_func_info = prog->aux->func_info_cnt;
2451 if (info.nr_func_info && ulen) {
2452 char __user *user_finfo;
2454 user_finfo = u64_to_user_ptr(info.func_info);
2455 ulen = min_t(u32, info.nr_func_info, ulen);
2456 if (copy_to_user(user_finfo, prog->aux->func_info,
2457 info.func_info_rec_size * ulen))
2461 ulen = info.nr_line_info;
2462 info.nr_line_info = prog->aux->nr_linfo;
2463 if (info.nr_line_info && ulen) {
2464 __u8 __user *user_linfo;
2466 user_linfo = u64_to_user_ptr(info.line_info);
2467 ulen = min_t(u32, info.nr_line_info, ulen);
2468 if (copy_to_user(user_linfo, prog->aux->linfo,
2469 info.line_info_rec_size * ulen))
2473 ulen = info.nr_jited_line_info;
2474 if (prog->aux->jited_linfo)
2475 info.nr_jited_line_info = prog->aux->nr_linfo;
2477 info.nr_jited_line_info = 0;
2478 if (info.nr_jited_line_info && ulen) {
2479 if (bpf_dump_raw_ok()) {
2480 __u64 __user *user_linfo;
2483 user_linfo = u64_to_user_ptr(info.jited_line_info);
2484 ulen = min_t(u32, info.nr_jited_line_info, ulen);
2485 for (i = 0; i < ulen; i++) {
2486 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2491 info.jited_line_info = 0;
2495 ulen = info.nr_prog_tags;
2496 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2498 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2501 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2502 ulen = min_t(u32, info.nr_prog_tags, ulen);
2503 if (prog->aux->func_cnt) {
2504 for (i = 0; i < ulen; i++) {
2505 if (copy_to_user(user_prog_tags[i],
2506 prog->aux->func[i]->tag,
2511 if (copy_to_user(user_prog_tags[0],
2512 prog->tag, BPF_TAG_SIZE))
2518 if (copy_to_user(uinfo, &info, info_len) ||
2519 put_user(info_len, &uattr->info.info_len))
2525 static int bpf_map_get_info_by_fd(struct bpf_map *map,
2526 const union bpf_attr *attr,
2527 union bpf_attr __user *uattr)
2529 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2530 struct bpf_map_info info = {};
2531 u32 info_len = attr->info.info_len;
2534 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2537 info_len = min_t(u32, sizeof(info), info_len);
2539 info.type = map->map_type;
2541 info.key_size = map->key_size;
2542 info.value_size = map->value_size;
2543 info.max_entries = map->max_entries;
2544 info.map_flags = map->map_flags;
2545 memcpy(info.name, map->name, sizeof(map->name));
2548 info.btf_id = btf_id(map->btf);
2549 info.btf_key_type_id = map->btf_key_type_id;
2550 info.btf_value_type_id = map->btf_value_type_id;
2553 if (bpf_map_is_dev_bound(map)) {
2554 err = bpf_map_offload_info_fill(&info, map);
2559 if (copy_to_user(uinfo, &info, info_len) ||
2560 put_user(info_len, &uattr->info.info_len))
2566 static int bpf_btf_get_info_by_fd(struct btf *btf,
2567 const union bpf_attr *attr,
2568 union bpf_attr __user *uattr)
2570 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2571 u32 info_len = attr->info.info_len;
2574 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
2578 return btf_get_info_by_fd(btf, attr, uattr);
2581 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2583 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2584 union bpf_attr __user *uattr)
2586 int ufd = attr->info.bpf_fd;
2590 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2597 if (f.file->f_op == &bpf_prog_fops)
2598 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2600 else if (f.file->f_op == &bpf_map_fops)
2601 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2603 else if (f.file->f_op == &btf_fops)
2604 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
2612 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2614 static int bpf_btf_load(const union bpf_attr *attr)
2616 if (CHECK_ATTR(BPF_BTF_LOAD))
2619 if (!capable(CAP_SYS_ADMIN))
2622 return btf_new_fd(attr);
2625 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2627 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2629 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2632 if (!capable(CAP_SYS_ADMIN))
2635 return btf_get_fd_by_id(attr->btf_id);
2638 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2639 union bpf_attr __user *uattr,
2640 u32 prog_id, u32 fd_type,
2641 const char *buf, u64 probe_offset,
2644 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2645 u32 len = buf ? strlen(buf) : 0, input_len;
2648 if (put_user(len, &uattr->task_fd_query.buf_len))
2650 input_len = attr->task_fd_query.buf_len;
2651 if (input_len && ubuf) {
2653 /* nothing to copy, just make ubuf NULL terminated */
2656 if (put_user(zero, ubuf))
2658 } else if (input_len >= len + 1) {
2659 /* ubuf can hold the string with NULL terminator */
2660 if (copy_to_user(ubuf, buf, len + 1))
2663 /* ubuf cannot hold the string with NULL terminator,
2664 * do a partial copy with NULL terminator.
2669 if (copy_to_user(ubuf, buf, input_len - 1))
2671 if (put_user(zero, ubuf + input_len - 1))
2676 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2677 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2678 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2679 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2685 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2687 static int bpf_task_fd_query(const union bpf_attr *attr,
2688 union bpf_attr __user *uattr)
2690 pid_t pid = attr->task_fd_query.pid;
2691 u32 fd = attr->task_fd_query.fd;
2692 const struct perf_event *event;
2693 struct files_struct *files;
2694 struct task_struct *task;
2698 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2701 if (!capable(CAP_SYS_ADMIN))
2704 if (attr->task_fd_query.flags != 0)
2707 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2711 files = get_files_struct(task);
2712 put_task_struct(task);
2717 spin_lock(&files->file_lock);
2718 file = fcheck_files(files, fd);
2723 spin_unlock(&files->file_lock);
2724 put_files_struct(files);
2729 if (file->f_op == &bpf_raw_tp_fops) {
2730 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2731 struct bpf_raw_event_map *btp = raw_tp->btp;
2733 err = bpf_task_fd_query_copy(attr, uattr,
2734 raw_tp->prog->aux->id,
2735 BPF_FD_TYPE_RAW_TRACEPOINT,
2736 btp->tp->name, 0, 0);
2740 event = perf_get_event(file);
2741 if (!IS_ERR(event)) {
2742 u64 probe_offset, probe_addr;
2743 u32 prog_id, fd_type;
2746 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2747 &buf, &probe_offset,
2750 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2764 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2766 union bpf_attr attr = {};
2769 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
2772 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
2775 size = min_t(u32, size, sizeof(attr));
2777 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2778 if (copy_from_user(&attr, uattr, size) != 0)
2781 err = security_bpf(cmd, &attr, size);
2786 case BPF_MAP_CREATE:
2787 err = map_create(&attr);
2789 case BPF_MAP_LOOKUP_ELEM:
2790 err = map_lookup_elem(&attr);
2792 case BPF_MAP_UPDATE_ELEM:
2793 err = map_update_elem(&attr);
2795 case BPF_MAP_DELETE_ELEM:
2796 err = map_delete_elem(&attr);
2798 case BPF_MAP_GET_NEXT_KEY:
2799 err = map_get_next_key(&attr);
2801 case BPF_MAP_FREEZE:
2802 err = map_freeze(&attr);
2805 err = bpf_prog_load(&attr, uattr);
2808 err = bpf_obj_pin(&attr);
2811 err = bpf_obj_get(&attr);
2813 case BPF_PROG_ATTACH:
2814 err = bpf_prog_attach(&attr);
2816 case BPF_PROG_DETACH:
2817 err = bpf_prog_detach(&attr);
2819 case BPF_PROG_QUERY:
2820 err = bpf_prog_query(&attr, uattr);
2822 case BPF_PROG_TEST_RUN:
2823 err = bpf_prog_test_run(&attr, uattr);
2825 case BPF_PROG_GET_NEXT_ID:
2826 err = bpf_obj_get_next_id(&attr, uattr,
2827 &prog_idr, &prog_idr_lock);
2829 case BPF_MAP_GET_NEXT_ID:
2830 err = bpf_obj_get_next_id(&attr, uattr,
2831 &map_idr, &map_idr_lock);
2833 case BPF_PROG_GET_FD_BY_ID:
2834 err = bpf_prog_get_fd_by_id(&attr);
2836 case BPF_MAP_GET_FD_BY_ID:
2837 err = bpf_map_get_fd_by_id(&attr);
2839 case BPF_OBJ_GET_INFO_BY_FD:
2840 err = bpf_obj_get_info_by_fd(&attr, uattr);
2842 case BPF_RAW_TRACEPOINT_OPEN:
2843 err = bpf_raw_tracepoint_open(&attr);
2846 err = bpf_btf_load(&attr);
2848 case BPF_BTF_GET_FD_BY_ID:
2849 err = bpf_btf_get_fd_by_id(&attr);
2851 case BPF_TASK_FD_QUERY:
2852 err = bpf_task_fd_query(&attr, uattr);
2854 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2855 err = map_lookup_and_delete_elem(&attr);