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 =
43 IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
45 static const struct bpf_map_ops * const bpf_map_types[] = {
46 #define BPF_PROG_TYPE(_id, _ops)
47 #define BPF_MAP_TYPE(_id, _ops) \
49 #include <linux/bpf_types.h>
55 * If we're handed a bigger struct than we know of, ensure all the unknown bits
56 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
57 * we don't know about yet.
59 * There is a ToCToU between this function call and the following
60 * copy_from_user() call. However, this is not a concern since this function is
61 * meant to be a future-proofing of bits.
63 int bpf_check_uarg_tail_zero(void __user *uaddr,
67 unsigned char __user *addr;
68 unsigned char __user *end;
72 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
75 if (unlikely(!access_ok(uaddr, actual_size)))
78 if (actual_size <= expected_size)
81 addr = uaddr + expected_size;
82 end = uaddr + actual_size;
84 for (; addr < end; addr++) {
85 err = get_user(val, addr);
95 const struct bpf_map_ops bpf_map_offload_ops = {
96 .map_alloc = bpf_map_offload_map_alloc,
97 .map_free = bpf_map_offload_map_free,
98 .map_check_btf = map_check_no_btf,
101 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
103 const struct bpf_map_ops *ops;
104 u32 type = attr->map_type;
108 if (type >= ARRAY_SIZE(bpf_map_types))
109 return ERR_PTR(-EINVAL);
110 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
111 ops = bpf_map_types[type];
113 return ERR_PTR(-EINVAL);
115 if (ops->map_alloc_check) {
116 err = ops->map_alloc_check(attr);
120 if (attr->map_ifindex)
121 ops = &bpf_map_offload_ops;
122 map = ops->map_alloc(attr);
126 map->map_type = type;
130 void *bpf_map_area_alloc(u64 size, int numa_node)
132 /* We really just want to fail instead of triggering OOM killer
133 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
134 * which is used for lower order allocation requests.
136 * It has been observed that higher order allocation requests done by
137 * vmalloc with __GFP_NORETRY being set might fail due to not trying
138 * to reclaim memory from the page cache, thus we set
139 * __GFP_RETRY_MAYFAIL to avoid such situations.
142 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
145 if (size >= SIZE_MAX)
148 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
149 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
155 return __vmalloc_node_flags_caller(size, numa_node,
156 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
157 flags, __builtin_return_address(0));
160 void bpf_map_area_free(void *area)
165 static u32 bpf_map_flags_retain_permanent(u32 flags)
167 /* Some map creation flags are not tied to the map object but
168 * rather to the map fd instead, so they have no meaning upon
169 * map object inspection since multiple file descriptors with
170 * different (access) properties can exist here. Thus, given
171 * this has zero meaning for the map itself, lets clear these
174 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
177 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
179 map->map_type = attr->map_type;
180 map->key_size = attr->key_size;
181 map->value_size = attr->value_size;
182 map->max_entries = attr->max_entries;
183 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
184 map->numa_node = bpf_map_attr_numa_node(attr);
187 static int bpf_charge_memlock(struct user_struct *user, u32 pages)
189 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
191 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
192 atomic_long_sub(pages, &user->locked_vm);
198 static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
201 atomic_long_sub(pages, &user->locked_vm);
204 int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
206 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
207 struct user_struct *user;
210 if (size >= U32_MAX - PAGE_SIZE)
213 user = get_current_user();
214 ret = bpf_charge_memlock(user, pages);
226 void bpf_map_charge_finish(struct bpf_map_memory *mem)
228 bpf_uncharge_memlock(mem->user, mem->pages);
232 void bpf_map_charge_move(struct bpf_map_memory *dst,
233 struct bpf_map_memory *src)
237 /* Make sure src will not be used for the redundant uncharging. */
238 memset(src, 0, sizeof(struct bpf_map_memory));
241 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
245 ret = bpf_charge_memlock(map->memory.user, pages);
248 map->memory.pages += pages;
252 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
254 bpf_uncharge_memlock(map->memory.user, pages);
255 map->memory.pages -= pages;
258 static int bpf_map_alloc_id(struct bpf_map *map)
262 idr_preload(GFP_KERNEL);
263 spin_lock_bh(&map_idr_lock);
264 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
267 spin_unlock_bh(&map_idr_lock);
270 if (WARN_ON_ONCE(!id))
273 return id > 0 ? 0 : id;
276 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
280 /* Offloaded maps are removed from the IDR store when their device
281 * disappears - even if someone holds an fd to them they are unusable,
282 * the memory is gone, all ops will fail; they are simply waiting for
283 * refcnt to drop to be freed.
289 spin_lock_irqsave(&map_idr_lock, flags);
291 __acquire(&map_idr_lock);
293 idr_remove(&map_idr, map->id);
297 spin_unlock_irqrestore(&map_idr_lock, flags);
299 __release(&map_idr_lock);
302 /* called from workqueue */
303 static void bpf_map_free_deferred(struct work_struct *work)
305 struct bpf_map *map = container_of(work, struct bpf_map, work);
306 struct bpf_map_memory mem;
308 bpf_map_charge_move(&mem, &map->memory);
309 security_bpf_map_free(map);
310 /* implementation dependent freeing */
311 map->ops->map_free(map);
312 bpf_map_charge_finish(&mem);
315 static void bpf_map_put_uref(struct bpf_map *map)
317 if (atomic_dec_and_test(&map->usercnt)) {
318 if (map->ops->map_release_uref)
319 map->ops->map_release_uref(map);
323 /* decrement map refcnt and schedule it for freeing via workqueue
324 * (unrelying map implementation ops->map_free() might sleep)
326 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
328 if (atomic_dec_and_test(&map->refcnt)) {
329 /* bpf_map_free_id() must be called first */
330 bpf_map_free_id(map, do_idr_lock);
332 INIT_WORK(&map->work, bpf_map_free_deferred);
333 schedule_work(&map->work);
337 void bpf_map_put(struct bpf_map *map)
339 __bpf_map_put(map, true);
341 EXPORT_SYMBOL_GPL(bpf_map_put);
343 void bpf_map_put_with_uref(struct bpf_map *map)
345 bpf_map_put_uref(map);
349 static int bpf_map_release(struct inode *inode, struct file *filp)
351 struct bpf_map *map = filp->private_data;
353 if (map->ops->map_release)
354 map->ops->map_release(map, filp);
356 bpf_map_put_with_uref(map);
360 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
362 fmode_t mode = f.file->f_mode;
364 /* Our file permissions may have been overridden by global
365 * map permissions facing syscall side.
367 if (READ_ONCE(map->frozen))
368 mode &= ~FMODE_CAN_WRITE;
372 #ifdef CONFIG_PROC_FS
373 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
375 const struct bpf_map *map = filp->private_data;
376 const struct bpf_array *array;
377 u32 owner_prog_type = 0;
380 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
381 array = container_of(map, struct bpf_array, map);
382 owner_prog_type = array->owner_prog_type;
383 owner_jited = array->owner_jited;
400 map->memory.pages * 1ULL << PAGE_SHIFT,
402 READ_ONCE(map->frozen));
404 if (owner_prog_type) {
405 seq_printf(m, "owner_prog_type:\t%u\n",
407 seq_printf(m, "owner_jited:\t%u\n",
413 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
416 /* We need this handler such that alloc_file() enables
417 * f_mode with FMODE_CAN_READ.
422 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
423 size_t siz, loff_t *ppos)
425 /* We need this handler such that alloc_file() enables
426 * f_mode with FMODE_CAN_WRITE.
431 const struct file_operations bpf_map_fops = {
432 #ifdef CONFIG_PROC_FS
433 .show_fdinfo = bpf_map_show_fdinfo,
435 .release = bpf_map_release,
436 .read = bpf_dummy_read,
437 .write = bpf_dummy_write,
440 int bpf_map_new_fd(struct bpf_map *map, int flags)
444 ret = security_bpf_map(map, OPEN_FMODE(flags));
448 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
452 int bpf_get_file_flag(int flags)
454 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
456 if (flags & BPF_F_RDONLY)
458 if (flags & BPF_F_WRONLY)
463 /* helper macro to check that unused fields 'union bpf_attr' are zero */
464 #define CHECK_ATTR(CMD) \
465 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
466 sizeof(attr->CMD##_LAST_FIELD), 0, \
468 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
469 sizeof(attr->CMD##_LAST_FIELD)) != NULL
471 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
472 * Return 0 on success and < 0 on error.
474 static int bpf_obj_name_cpy(char *dst, const char *src)
476 const char *end = src + BPF_OBJ_NAME_LEN;
478 memset(dst, 0, BPF_OBJ_NAME_LEN);
479 /* Copy all isalnum(), '_' and '.' chars. */
480 while (src < end && *src) {
481 if (!isalnum(*src) &&
482 *src != '_' && *src != '.')
487 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
494 int map_check_no_btf(const struct bpf_map *map,
495 const struct btf *btf,
496 const struct btf_type *key_type,
497 const struct btf_type *value_type)
502 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
503 u32 btf_key_id, u32 btf_value_id)
505 const struct btf_type *key_type, *value_type;
506 u32 key_size, value_size;
509 /* Some maps allow key to be unspecified. */
511 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
512 if (!key_type || key_size != map->key_size)
515 key_type = btf_type_by_id(btf, 0);
516 if (!map->ops->map_check_btf)
520 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
521 if (!value_type || value_size != map->value_size)
524 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
526 if (map_value_has_spin_lock(map)) {
527 if (map->map_flags & BPF_F_RDONLY_PROG)
529 if (map->map_type != BPF_MAP_TYPE_HASH &&
530 map->map_type != BPF_MAP_TYPE_ARRAY &&
531 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
532 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
534 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
537 "verifier bug spin_lock_off %d value_size %d\n",
538 map->spin_lock_off, map->value_size);
543 if (map->ops->map_check_btf)
544 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
549 #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
550 /* called via syscall */
551 static int map_create(union bpf_attr *attr)
553 int numa_node = bpf_map_attr_numa_node(attr);
554 struct bpf_map_memory mem;
559 err = CHECK_ATTR(BPF_MAP_CREATE);
563 f_flags = bpf_get_file_flag(attr->map_flags);
567 if (numa_node != NUMA_NO_NODE &&
568 ((unsigned int)numa_node >= nr_node_ids ||
569 !node_online(numa_node)))
572 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
573 map = find_and_alloc_map(attr);
577 err = bpf_obj_name_cpy(map->name, attr->map_name);
581 atomic_set(&map->refcnt, 1);
582 atomic_set(&map->usercnt, 1);
584 if (attr->btf_key_type_id || attr->btf_value_type_id) {
587 if (!attr->btf_value_type_id) {
592 btf = btf_get_by_fd(attr->btf_fd);
598 err = map_check_btf(map, btf, attr->btf_key_type_id,
599 attr->btf_value_type_id);
606 map->btf_key_type_id = attr->btf_key_type_id;
607 map->btf_value_type_id = attr->btf_value_type_id;
609 map->spin_lock_off = -EINVAL;
612 err = security_bpf_map_alloc(map);
616 err = bpf_map_alloc_id(map);
620 err = bpf_map_new_fd(map, f_flags);
622 /* failed to allocate fd.
623 * bpf_map_put_with_uref() is needed because the above
624 * bpf_map_alloc_id() has published the map
625 * to the userspace and the userspace may
626 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
628 bpf_map_put_with_uref(map);
635 security_bpf_map_free(map);
638 bpf_map_charge_move(&mem, &map->memory);
639 map->ops->map_free(map);
640 bpf_map_charge_finish(&mem);
644 /* if error is returned, fd is released.
645 * On success caller should complete fd access with matching fdput()
647 struct bpf_map *__bpf_map_get(struct fd f)
650 return ERR_PTR(-EBADF);
651 if (f.file->f_op != &bpf_map_fops) {
653 return ERR_PTR(-EINVAL);
656 return f.file->private_data;
659 /* prog's and map's refcnt limit */
660 #define BPF_MAX_REFCNT 32768
662 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
664 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
665 atomic_dec(&map->refcnt);
666 return ERR_PTR(-EBUSY);
669 atomic_inc(&map->usercnt);
672 EXPORT_SYMBOL_GPL(bpf_map_inc);
674 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
676 struct fd f = fdget(ufd);
679 map = __bpf_map_get(f);
683 map = bpf_map_inc(map, true);
689 /* map_idr_lock should have been held */
690 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map,
695 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
697 if (refold >= BPF_MAX_REFCNT) {
698 __bpf_map_put(map, false);
699 return ERR_PTR(-EBUSY);
703 return ERR_PTR(-ENOENT);
706 atomic_inc(&map->usercnt);
711 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
713 spin_lock_bh(&map_idr_lock);
714 map = __bpf_map_inc_not_zero(map, uref);
715 spin_unlock_bh(&map_idr_lock);
719 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
721 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
726 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
729 return memdup_user(ukey, key_size);
732 return ERR_PTR(-EINVAL);
737 /* last field in 'union bpf_attr' used by this command */
738 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
740 static int map_lookup_elem(union bpf_attr *attr)
742 void __user *ukey = u64_to_user_ptr(attr->key);
743 void __user *uvalue = u64_to_user_ptr(attr->value);
744 int ufd = attr->map_fd;
746 void *key, *value, *ptr;
751 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
754 if (attr->flags & ~BPF_F_LOCK)
758 map = __bpf_map_get(f);
761 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
766 if ((attr->flags & BPF_F_LOCK) &&
767 !map_value_has_spin_lock(map)) {
772 key = __bpf_copy_key(ukey, map->key_size);
778 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
779 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
780 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
781 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
782 value_size = round_up(map->value_size, 8) * num_possible_cpus();
783 else if (IS_FD_MAP(map))
784 value_size = sizeof(u32);
786 value_size = map->value_size;
789 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
793 if (bpf_map_is_dev_bound(map)) {
794 err = bpf_map_offload_lookup_elem(map, key, value);
799 this_cpu_inc(bpf_prog_active);
800 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
801 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
802 err = bpf_percpu_hash_copy(map, key, value);
803 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
804 err = bpf_percpu_array_copy(map, key, value);
805 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
806 err = bpf_percpu_cgroup_storage_copy(map, key, value);
807 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
808 err = bpf_stackmap_copy(map, key, value);
809 } else if (IS_FD_ARRAY(map)) {
810 err = bpf_fd_array_map_lookup_elem(map, key, value);
811 } else if (IS_FD_HASH(map)) {
812 err = bpf_fd_htab_map_lookup_elem(map, key, value);
813 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
814 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
815 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
816 map->map_type == BPF_MAP_TYPE_STACK) {
817 err = map->ops->map_peek_elem(map, value);
820 if (map->ops->map_lookup_elem_sys_only)
821 ptr = map->ops->map_lookup_elem_sys_only(map, key);
823 ptr = map->ops->map_lookup_elem(map, key);
830 if (attr->flags & BPF_F_LOCK)
831 /* lock 'ptr' and copy everything but lock */
832 copy_map_value_locked(map, value, ptr, true);
834 copy_map_value(map, value, ptr);
835 /* mask lock, since value wasn't zero inited */
836 check_and_init_map_lock(map, value);
840 this_cpu_dec(bpf_prog_active);
848 if (copy_to_user(uvalue, value, value_size) != 0)
862 static void maybe_wait_bpf_programs(struct bpf_map *map)
864 /* Wait for any running BPF programs to complete so that
865 * userspace, when we return to it, knows that all programs
866 * that could be running use the new map value.
868 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
869 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
873 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
875 static int map_update_elem(union bpf_attr *attr)
877 void __user *ukey = u64_to_user_ptr(attr->key);
878 void __user *uvalue = u64_to_user_ptr(attr->value);
879 int ufd = attr->map_fd;
886 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
890 map = __bpf_map_get(f);
893 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
898 if ((attr->flags & BPF_F_LOCK) &&
899 !map_value_has_spin_lock(map)) {
904 key = __bpf_copy_key(ukey, map->key_size);
910 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
911 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
912 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
913 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
914 value_size = round_up(map->value_size, 8) * num_possible_cpus();
916 value_size = map->value_size;
919 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
924 if (copy_from_user(value, uvalue, value_size) != 0)
927 /* Need to create a kthread, thus must support schedule */
928 if (bpf_map_is_dev_bound(map)) {
929 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
931 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
932 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
933 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
934 err = map->ops->map_update_elem(map, key, value, attr->flags);
938 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
939 * inside bpf map update or delete otherwise deadlocks are possible
942 __this_cpu_inc(bpf_prog_active);
943 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
944 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
945 err = bpf_percpu_hash_update(map, key, value, attr->flags);
946 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
947 err = bpf_percpu_array_update(map, key, value, attr->flags);
948 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
949 err = bpf_percpu_cgroup_storage_update(map, key, value,
951 } else if (IS_FD_ARRAY(map)) {
953 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
956 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
958 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
961 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
962 /* rcu_read_lock() is not needed */
963 err = bpf_fd_reuseport_array_update_elem(map, key, value,
965 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
966 map->map_type == BPF_MAP_TYPE_STACK) {
967 err = map->ops->map_push_elem(map, value, attr->flags);
970 err = map->ops->map_update_elem(map, key, value, attr->flags);
973 __this_cpu_dec(bpf_prog_active);
975 maybe_wait_bpf_programs(map);
986 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
988 static int map_delete_elem(union bpf_attr *attr)
990 void __user *ukey = u64_to_user_ptr(attr->key);
991 int ufd = attr->map_fd;
997 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1001 map = __bpf_map_get(f);
1003 return PTR_ERR(map);
1004 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1009 key = __bpf_copy_key(ukey, map->key_size);
1015 if (bpf_map_is_dev_bound(map)) {
1016 err = bpf_map_offload_delete_elem(map, key);
1021 __this_cpu_inc(bpf_prog_active);
1023 err = map->ops->map_delete_elem(map, key);
1025 __this_cpu_dec(bpf_prog_active);
1027 maybe_wait_bpf_programs(map);
1035 /* last field in 'union bpf_attr' used by this command */
1036 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1038 static int map_get_next_key(union bpf_attr *attr)
1040 void __user *ukey = u64_to_user_ptr(attr->key);
1041 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1042 int ufd = attr->map_fd;
1043 struct bpf_map *map;
1044 void *key, *next_key;
1048 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1052 map = __bpf_map_get(f);
1054 return PTR_ERR(map);
1055 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1061 key = __bpf_copy_key(ukey, map->key_size);
1071 next_key = kmalloc(map->key_size, GFP_USER);
1075 if (bpf_map_is_dev_bound(map)) {
1076 err = bpf_map_offload_get_next_key(map, key, next_key);
1081 err = map->ops->map_get_next_key(map, key, next_key);
1088 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1102 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1104 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1106 void __user *ukey = u64_to_user_ptr(attr->key);
1107 void __user *uvalue = u64_to_user_ptr(attr->value);
1108 int ufd = attr->map_fd;
1109 struct bpf_map *map;
1115 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1119 map = __bpf_map_get(f);
1121 return PTR_ERR(map);
1122 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1123 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1128 key = __bpf_copy_key(ukey, map->key_size);
1134 value_size = map->value_size;
1137 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1141 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1142 map->map_type == BPF_MAP_TYPE_STACK) {
1143 err = map->ops->map_pop_elem(map, value);
1151 if (copy_to_user(uvalue, value, value_size) != 0) {
1167 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1169 static int map_freeze(const union bpf_attr *attr)
1171 int err = 0, ufd = attr->map_fd;
1172 struct bpf_map *map;
1175 if (CHECK_ATTR(BPF_MAP_FREEZE))
1179 map = __bpf_map_get(f);
1181 return PTR_ERR(map);
1182 if (READ_ONCE(map->frozen)) {
1186 if (!capable(CAP_SYS_ADMIN)) {
1191 WRITE_ONCE(map->frozen, true);
1197 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1198 #define BPF_PROG_TYPE(_id, _name) \
1199 [_id] = & _name ## _prog_ops,
1200 #define BPF_MAP_TYPE(_id, _ops)
1201 #include <linux/bpf_types.h>
1202 #undef BPF_PROG_TYPE
1206 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1208 const struct bpf_prog_ops *ops;
1210 if (type >= ARRAY_SIZE(bpf_prog_types))
1212 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1213 ops = bpf_prog_types[type];
1217 if (!bpf_prog_is_dev_bound(prog->aux))
1218 prog->aux->ops = ops;
1220 prog->aux->ops = &bpf_offload_prog_ops;
1225 /* drop refcnt on maps used by eBPF program and free auxilary data */
1226 static void free_used_maps(struct bpf_prog_aux *aux)
1228 enum bpf_cgroup_storage_type stype;
1231 for_each_cgroup_storage_type(stype) {
1232 if (!aux->cgroup_storage[stype])
1234 bpf_cgroup_storage_release(aux->prog,
1235 aux->cgroup_storage[stype]);
1238 for (i = 0; i < aux->used_map_cnt; i++)
1239 bpf_map_put(aux->used_maps[i]);
1241 kfree(aux->used_maps);
1244 int __bpf_prog_charge(struct user_struct *user, u32 pages)
1246 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1247 unsigned long user_bufs;
1250 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1251 if (user_bufs > memlock_limit) {
1252 atomic_long_sub(pages, &user->locked_vm);
1260 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1263 atomic_long_sub(pages, &user->locked_vm);
1266 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1268 struct user_struct *user = get_current_user();
1271 ret = __bpf_prog_charge(user, prog->pages);
1277 prog->aux->user = user;
1281 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1283 struct user_struct *user = prog->aux->user;
1285 __bpf_prog_uncharge(user, prog->pages);
1289 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1293 idr_preload(GFP_KERNEL);
1294 spin_lock_bh(&prog_idr_lock);
1295 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1298 spin_unlock_bh(&prog_idr_lock);
1301 /* id is in [1, INT_MAX) */
1302 if (WARN_ON_ONCE(!id))
1305 return id > 0 ? 0 : id;
1308 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1310 /* cBPF to eBPF migrations are currently not in the idr store.
1311 * Offloaded programs are removed from the store when their device
1312 * disappears - even if someone grabs an fd to them they are unusable,
1313 * simply waiting for refcnt to drop to be freed.
1319 spin_lock_bh(&prog_idr_lock);
1321 __acquire(&prog_idr_lock);
1323 idr_remove(&prog_idr, prog->aux->id);
1327 spin_unlock_bh(&prog_idr_lock);
1329 __release(&prog_idr_lock);
1332 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1334 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1336 kvfree(aux->func_info);
1337 free_used_maps(aux);
1338 bpf_prog_uncharge_memlock(aux->prog);
1339 security_bpf_prog_free(aux);
1340 bpf_prog_free(aux->prog);
1343 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1345 bpf_prog_kallsyms_del_all(prog);
1346 btf_put(prog->aux->btf);
1347 bpf_prog_free_linfo(prog);
1350 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1352 __bpf_prog_put_rcu(&prog->aux->rcu);
1355 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1357 if (atomic_dec_and_test(&prog->aux->refcnt)) {
1358 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1359 /* bpf_prog_free_id() must be called first */
1360 bpf_prog_free_id(prog, do_idr_lock);
1361 __bpf_prog_put_noref(prog, true);
1365 void bpf_prog_put(struct bpf_prog *prog)
1367 __bpf_prog_put(prog, true);
1369 EXPORT_SYMBOL_GPL(bpf_prog_put);
1371 static int bpf_prog_release(struct inode *inode, struct file *filp)
1373 struct bpf_prog *prog = filp->private_data;
1379 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1380 struct bpf_prog_stats *stats)
1382 u64 nsecs = 0, cnt = 0;
1385 for_each_possible_cpu(cpu) {
1386 const struct bpf_prog_stats *st;
1390 st = per_cpu_ptr(prog->aux->stats, cpu);
1392 start = u64_stats_fetch_begin_irq(&st->syncp);
1395 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1399 stats->nsecs = nsecs;
1403 #ifdef CONFIG_PROC_FS
1404 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1406 const struct bpf_prog *prog = filp->private_data;
1407 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1408 struct bpf_prog_stats stats;
1410 bpf_prog_get_stats(prog, &stats);
1411 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1418 "run_time_ns:\t%llu\n"
1423 prog->pages * 1ULL << PAGE_SHIFT,
1430 const struct file_operations bpf_prog_fops = {
1431 #ifdef CONFIG_PROC_FS
1432 .show_fdinfo = bpf_prog_show_fdinfo,
1434 .release = bpf_prog_release,
1435 .read = bpf_dummy_read,
1436 .write = bpf_dummy_write,
1439 int bpf_prog_new_fd(struct bpf_prog *prog)
1443 ret = security_bpf_prog(prog);
1447 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1448 O_RDWR | O_CLOEXEC);
1451 static struct bpf_prog *____bpf_prog_get(struct fd f)
1454 return ERR_PTR(-EBADF);
1455 if (f.file->f_op != &bpf_prog_fops) {
1457 return ERR_PTR(-EINVAL);
1460 return f.file->private_data;
1463 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
1465 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1466 atomic_sub(i, &prog->aux->refcnt);
1467 return ERR_PTR(-EBUSY);
1471 EXPORT_SYMBOL_GPL(bpf_prog_add);
1473 void bpf_prog_sub(struct bpf_prog *prog, int i)
1475 /* Only to be used for undoing previous bpf_prog_add() in some
1476 * error path. We still know that another entity in our call
1477 * path holds a reference to the program, thus atomic_sub() can
1478 * be safely used in such cases!
1480 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1482 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1484 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1486 return bpf_prog_add(prog, 1);
1488 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1490 /* prog_idr_lock should have been held */
1491 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1495 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1497 if (refold >= BPF_MAX_REFCNT) {
1498 __bpf_prog_put(prog, false);
1499 return ERR_PTR(-EBUSY);
1503 return ERR_PTR(-ENOENT);
1507 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1509 bool bpf_prog_get_ok(struct bpf_prog *prog,
1510 enum bpf_prog_type *attach_type, bool attach_drv)
1512 /* not an attachment, just a refcount inc, always allow */
1516 if (prog->type != *attach_type)
1518 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1524 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1527 struct fd f = fdget(ufd);
1528 struct bpf_prog *prog;
1530 prog = ____bpf_prog_get(f);
1533 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1534 prog = ERR_PTR(-EINVAL);
1538 prog = bpf_prog_inc(prog);
1544 struct bpf_prog *bpf_prog_get(u32 ufd)
1546 return __bpf_prog_get(ufd, NULL, false);
1549 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1552 return __bpf_prog_get(ufd, &type, attach_drv);
1554 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1556 /* Initially all BPF programs could be loaded w/o specifying
1557 * expected_attach_type. Later for some of them specifying expected_attach_type
1558 * at load time became required so that program could be validated properly.
1559 * Programs of types that are allowed to be loaded both w/ and w/o (for
1560 * backward compatibility) expected_attach_type, should have the default attach
1561 * type assigned to expected_attach_type for the latter case, so that it can be
1562 * validated later at attach time.
1564 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1565 * prog type requires it but has some attach types that have to be backward
1568 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1570 switch (attr->prog_type) {
1571 case BPF_PROG_TYPE_CGROUP_SOCK:
1572 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1573 * exist so checking for non-zero is the way to go here.
1575 if (!attr->expected_attach_type)
1576 attr->expected_attach_type =
1577 BPF_CGROUP_INET_SOCK_CREATE;
1583 bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1584 enum bpf_attach_type expected_attach_type)
1586 switch (prog_type) {
1587 case BPF_PROG_TYPE_CGROUP_SOCK:
1588 switch (expected_attach_type) {
1589 case BPF_CGROUP_INET_SOCK_CREATE:
1590 case BPF_CGROUP_INET4_POST_BIND:
1591 case BPF_CGROUP_INET6_POST_BIND:
1596 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1597 switch (expected_attach_type) {
1598 case BPF_CGROUP_INET4_BIND:
1599 case BPF_CGROUP_INET6_BIND:
1600 case BPF_CGROUP_INET4_CONNECT:
1601 case BPF_CGROUP_INET6_CONNECT:
1602 case BPF_CGROUP_UDP4_SENDMSG:
1603 case BPF_CGROUP_UDP6_SENDMSG:
1604 case BPF_CGROUP_UDP4_RECVMSG:
1605 case BPF_CGROUP_UDP6_RECVMSG:
1610 case BPF_PROG_TYPE_CGROUP_SKB:
1611 switch (expected_attach_type) {
1612 case BPF_CGROUP_INET_INGRESS:
1613 case BPF_CGROUP_INET_EGRESS:
1618 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1619 switch (expected_attach_type) {
1620 case BPF_CGROUP_SETSOCKOPT:
1621 case BPF_CGROUP_GETSOCKOPT:
1631 /* last field in 'union bpf_attr' used by this command */
1632 #define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
1634 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
1636 enum bpf_prog_type type = attr->prog_type;
1637 struct bpf_prog *prog;
1642 if (CHECK_ATTR(BPF_PROG_LOAD))
1645 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
1646 BPF_F_ANY_ALIGNMENT |
1647 BPF_F_TEST_STATE_FREQ |
1648 BPF_F_TEST_RND_HI32))
1651 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1652 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1653 !capable(CAP_SYS_ADMIN))
1656 /* copy eBPF program license from user space */
1657 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1658 sizeof(license) - 1) < 0)
1660 license[sizeof(license) - 1] = 0;
1662 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1663 is_gpl = license_is_gpl_compatible(license);
1665 if (attr->insn_cnt == 0 ||
1666 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
1668 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1669 type != BPF_PROG_TYPE_CGROUP_SKB &&
1670 !capable(CAP_SYS_ADMIN))
1673 bpf_prog_load_fixup_attach_type(attr);
1674 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1677 /* plain bpf_prog allocation */
1678 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1682 prog->expected_attach_type = attr->expected_attach_type;
1684 prog->aux->offload_requested = !!attr->prog_ifindex;
1686 err = security_bpf_prog_alloc(prog->aux);
1688 goto free_prog_nouncharge;
1690 err = bpf_prog_charge_memlock(prog);
1694 prog->len = attr->insn_cnt;
1697 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1698 bpf_prog_insn_size(prog)) != 0)
1701 prog->orig_prog = NULL;
1704 atomic_set(&prog->aux->refcnt, 1);
1705 prog->gpl_compatible = is_gpl ? 1 : 0;
1707 if (bpf_prog_is_dev_bound(prog->aux)) {
1708 err = bpf_prog_offload_init(prog, attr);
1713 /* find program type: socket_filter vs tracing_filter */
1714 err = find_prog_type(type, prog);
1718 prog->aux->load_time = ktime_get_boottime_ns();
1719 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1723 /* run eBPF verifier */
1724 err = bpf_check(&prog, attr, uattr);
1726 goto free_used_maps;
1728 prog = bpf_prog_select_runtime(prog, &err);
1730 goto free_used_maps;
1732 err = bpf_prog_alloc_id(prog);
1734 goto free_used_maps;
1736 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
1737 * effectively publicly exposed. However, retrieving via
1738 * bpf_prog_get_fd_by_id() will take another reference,
1739 * therefore it cannot be gone underneath us.
1741 * Only for the time /after/ successful bpf_prog_new_fd()
1742 * and before returning to userspace, we might just hold
1743 * one reference and any parallel close on that fd could
1744 * rip everything out. Hence, below notifications must
1745 * happen before bpf_prog_new_fd().
1747 * Also, any failure handling from this point onwards must
1748 * be using bpf_prog_put() given the program is exposed.
1750 bpf_prog_kallsyms_add(prog);
1751 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
1753 err = bpf_prog_new_fd(prog);
1759 /* In case we have subprogs, we need to wait for a grace
1760 * period before we can tear down JIT memory since symbols
1761 * are already exposed under kallsyms.
1763 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
1766 bpf_prog_uncharge_memlock(prog);
1768 security_bpf_prog_free(prog->aux);
1769 free_prog_nouncharge:
1770 bpf_prog_free(prog);
1774 #define BPF_OBJ_LAST_FIELD file_flags
1776 static int bpf_obj_pin(const union bpf_attr *attr)
1778 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1781 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1784 static int bpf_obj_get(const union bpf_attr *attr)
1786 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1787 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1790 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1794 struct bpf_raw_tracepoint {
1795 struct bpf_raw_event_map *btp;
1796 struct bpf_prog *prog;
1799 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1801 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1804 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1805 bpf_prog_put(raw_tp->prog);
1807 bpf_put_raw_tracepoint(raw_tp->btp);
1812 static const struct file_operations bpf_raw_tp_fops = {
1813 .release = bpf_raw_tracepoint_release,
1814 .read = bpf_dummy_read,
1815 .write = bpf_dummy_write,
1818 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1820 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1822 struct bpf_raw_tracepoint *raw_tp;
1823 struct bpf_raw_event_map *btp;
1824 struct bpf_prog *prog;
1828 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1829 sizeof(tp_name) - 1) < 0)
1831 tp_name[sizeof(tp_name) - 1] = 0;
1833 btp = bpf_get_raw_tracepoint(tp_name);
1837 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1844 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
1846 err = PTR_ERR(prog);
1849 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1850 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
1855 err = bpf_probe_register(raw_tp->btp, prog);
1859 raw_tp->prog = prog;
1860 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1863 bpf_probe_unregister(raw_tp->btp, prog);
1874 bpf_put_raw_tracepoint(btp);
1878 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1879 enum bpf_attach_type attach_type)
1881 switch (prog->type) {
1882 case BPF_PROG_TYPE_CGROUP_SOCK:
1883 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1884 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1885 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1886 case BPF_PROG_TYPE_CGROUP_SKB:
1887 return prog->enforce_expected_attach_type &&
1888 prog->expected_attach_type != attach_type ?
1895 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1897 #define BPF_F_ATTACH_MASK \
1898 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1900 static int bpf_prog_attach(const union bpf_attr *attr)
1902 enum bpf_prog_type ptype;
1903 struct bpf_prog *prog;
1906 if (!capable(CAP_NET_ADMIN))
1909 if (CHECK_ATTR(BPF_PROG_ATTACH))
1912 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
1915 switch (attr->attach_type) {
1916 case BPF_CGROUP_INET_INGRESS:
1917 case BPF_CGROUP_INET_EGRESS:
1918 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1920 case BPF_CGROUP_INET_SOCK_CREATE:
1921 case BPF_CGROUP_INET4_POST_BIND:
1922 case BPF_CGROUP_INET6_POST_BIND:
1923 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1925 case BPF_CGROUP_INET4_BIND:
1926 case BPF_CGROUP_INET6_BIND:
1927 case BPF_CGROUP_INET4_CONNECT:
1928 case BPF_CGROUP_INET6_CONNECT:
1929 case BPF_CGROUP_UDP4_SENDMSG:
1930 case BPF_CGROUP_UDP6_SENDMSG:
1931 case BPF_CGROUP_UDP4_RECVMSG:
1932 case BPF_CGROUP_UDP6_RECVMSG:
1933 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1935 case BPF_CGROUP_SOCK_OPS:
1936 ptype = BPF_PROG_TYPE_SOCK_OPS;
1938 case BPF_CGROUP_DEVICE:
1939 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1941 case BPF_SK_MSG_VERDICT:
1942 ptype = BPF_PROG_TYPE_SK_MSG;
1944 case BPF_SK_SKB_STREAM_PARSER:
1945 case BPF_SK_SKB_STREAM_VERDICT:
1946 ptype = BPF_PROG_TYPE_SK_SKB;
1948 case BPF_LIRC_MODE2:
1949 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1951 case BPF_FLOW_DISSECTOR:
1952 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1954 case BPF_CGROUP_SYSCTL:
1955 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1957 case BPF_CGROUP_GETSOCKOPT:
1958 case BPF_CGROUP_SETSOCKOPT:
1959 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
1965 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1967 return PTR_ERR(prog);
1969 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1975 case BPF_PROG_TYPE_SK_SKB:
1976 case BPF_PROG_TYPE_SK_MSG:
1977 ret = sock_map_get_from_fd(attr, prog);
1979 case BPF_PROG_TYPE_LIRC_MODE2:
1980 ret = lirc_prog_attach(attr, prog);
1982 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1983 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1986 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
1994 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1996 static int bpf_prog_detach(const union bpf_attr *attr)
1998 enum bpf_prog_type ptype;
2000 if (!capable(CAP_NET_ADMIN))
2003 if (CHECK_ATTR(BPF_PROG_DETACH))
2006 switch (attr->attach_type) {
2007 case BPF_CGROUP_INET_INGRESS:
2008 case BPF_CGROUP_INET_EGRESS:
2009 ptype = BPF_PROG_TYPE_CGROUP_SKB;
2011 case BPF_CGROUP_INET_SOCK_CREATE:
2012 case BPF_CGROUP_INET4_POST_BIND:
2013 case BPF_CGROUP_INET6_POST_BIND:
2014 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2016 case BPF_CGROUP_INET4_BIND:
2017 case BPF_CGROUP_INET6_BIND:
2018 case BPF_CGROUP_INET4_CONNECT:
2019 case BPF_CGROUP_INET6_CONNECT:
2020 case BPF_CGROUP_UDP4_SENDMSG:
2021 case BPF_CGROUP_UDP6_SENDMSG:
2022 case BPF_CGROUP_UDP4_RECVMSG:
2023 case BPF_CGROUP_UDP6_RECVMSG:
2024 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2026 case BPF_CGROUP_SOCK_OPS:
2027 ptype = BPF_PROG_TYPE_SOCK_OPS;
2029 case BPF_CGROUP_DEVICE:
2030 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2032 case BPF_SK_MSG_VERDICT:
2033 return sock_map_prog_detach(attr, BPF_PROG_TYPE_SK_MSG);
2034 case BPF_SK_SKB_STREAM_PARSER:
2035 case BPF_SK_SKB_STREAM_VERDICT:
2036 return sock_map_prog_detach(attr, BPF_PROG_TYPE_SK_SKB);
2037 case BPF_LIRC_MODE2:
2038 return lirc_prog_detach(attr);
2039 case BPF_FLOW_DISSECTOR:
2040 return skb_flow_dissector_bpf_prog_detach(attr);
2041 case BPF_CGROUP_SYSCTL:
2042 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2044 case BPF_CGROUP_GETSOCKOPT:
2045 case BPF_CGROUP_SETSOCKOPT:
2046 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2052 return cgroup_bpf_prog_detach(attr, ptype);
2055 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2057 static int bpf_prog_query(const union bpf_attr *attr,
2058 union bpf_attr __user *uattr)
2060 if (!capable(CAP_NET_ADMIN))
2062 if (CHECK_ATTR(BPF_PROG_QUERY))
2064 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2067 switch (attr->query.attach_type) {
2068 case BPF_CGROUP_INET_INGRESS:
2069 case BPF_CGROUP_INET_EGRESS:
2070 case BPF_CGROUP_INET_SOCK_CREATE:
2071 case BPF_CGROUP_INET4_BIND:
2072 case BPF_CGROUP_INET6_BIND:
2073 case BPF_CGROUP_INET4_POST_BIND:
2074 case BPF_CGROUP_INET6_POST_BIND:
2075 case BPF_CGROUP_INET4_CONNECT:
2076 case BPF_CGROUP_INET6_CONNECT:
2077 case BPF_CGROUP_UDP4_SENDMSG:
2078 case BPF_CGROUP_UDP6_SENDMSG:
2079 case BPF_CGROUP_UDP4_RECVMSG:
2080 case BPF_CGROUP_UDP6_RECVMSG:
2081 case BPF_CGROUP_SOCK_OPS:
2082 case BPF_CGROUP_DEVICE:
2083 case BPF_CGROUP_SYSCTL:
2084 case BPF_CGROUP_GETSOCKOPT:
2085 case BPF_CGROUP_SETSOCKOPT:
2087 case BPF_LIRC_MODE2:
2088 return lirc_prog_query(attr, uattr);
2089 case BPF_FLOW_DISSECTOR:
2090 return skb_flow_dissector_prog_query(attr, uattr);
2095 return cgroup_bpf_prog_query(attr, uattr);
2098 #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
2100 static int bpf_prog_test_run(const union bpf_attr *attr,
2101 union bpf_attr __user *uattr)
2103 struct bpf_prog *prog;
2104 int ret = -ENOTSUPP;
2106 if (!capable(CAP_SYS_ADMIN))
2108 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2111 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2112 (!attr->test.ctx_size_in && attr->test.ctx_in))
2115 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2116 (!attr->test.ctx_size_out && attr->test.ctx_out))
2119 prog = bpf_prog_get(attr->test.prog_fd);
2121 return PTR_ERR(prog);
2123 if (prog->aux->ops->test_run)
2124 ret = prog->aux->ops->test_run(prog, attr, uattr);
2130 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2132 static int bpf_obj_get_next_id(const union bpf_attr *attr,
2133 union bpf_attr __user *uattr,
2137 u32 next_id = attr->start_id;
2140 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2143 if (!capable(CAP_SYS_ADMIN))
2148 if (!idr_get_next(idr, &next_id))
2150 spin_unlock_bh(lock);
2153 err = put_user(next_id, &uattr->next_id);
2158 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2160 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2162 struct bpf_prog *prog;
2163 u32 id = attr->prog_id;
2166 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2169 if (!capable(CAP_SYS_ADMIN))
2172 spin_lock_bh(&prog_idr_lock);
2173 prog = idr_find(&prog_idr, id);
2175 prog = bpf_prog_inc_not_zero(prog);
2177 prog = ERR_PTR(-ENOENT);
2178 spin_unlock_bh(&prog_idr_lock);
2181 return PTR_ERR(prog);
2183 fd = bpf_prog_new_fd(prog);
2190 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2192 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2194 struct bpf_map *map;
2195 u32 id = attr->map_id;
2199 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2200 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
2203 if (!capable(CAP_SYS_ADMIN))
2206 f_flags = bpf_get_file_flag(attr->open_flags);
2210 spin_lock_bh(&map_idr_lock);
2211 map = idr_find(&map_idr, id);
2213 map = __bpf_map_inc_not_zero(map, true);
2215 map = ERR_PTR(-ENOENT);
2216 spin_unlock_bh(&map_idr_lock);
2219 return PTR_ERR(map);
2221 fd = bpf_map_new_fd(map, f_flags);
2223 bpf_map_put_with_uref(map);
2228 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
2229 unsigned long addr, u32 *off,
2232 const struct bpf_map *map;
2235 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2236 map = prog->aux->used_maps[i];
2237 if (map == (void *)addr) {
2238 *type = BPF_PSEUDO_MAP_FD;
2241 if (!map->ops->map_direct_value_meta)
2243 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2244 *type = BPF_PSEUDO_MAP_VALUE;
2252 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
2253 const struct cred *f_cred)
2255 const struct bpf_map *map;
2256 struct bpf_insn *insns;
2261 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2266 for (i = 0; i < prog->len; i++) {
2267 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2268 insns[i].code = BPF_JMP | BPF_CALL;
2269 insns[i].imm = BPF_FUNC_tail_call;
2272 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2273 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2274 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2275 insns[i].code = BPF_JMP | BPF_CALL;
2276 if (!bpf_dump_raw_ok(f_cred))
2281 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2284 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
2285 map = bpf_map_from_imm(prog, imm, &off, &type);
2287 insns[i].src_reg = type;
2288 insns[i].imm = map->id;
2289 insns[i + 1].imm = off;
2297 static int set_info_rec_size(struct bpf_prog_info *info)
2300 * Ensure info.*_rec_size is the same as kernel expected size
2304 * Only allow zero *_rec_size if both _rec_size and _cnt are
2305 * zero. In this case, the kernel will set the expected
2306 * _rec_size back to the info.
2309 if ((info->nr_func_info || info->func_info_rec_size) &&
2310 info->func_info_rec_size != sizeof(struct bpf_func_info))
2313 if ((info->nr_line_info || info->line_info_rec_size) &&
2314 info->line_info_rec_size != sizeof(struct bpf_line_info))
2317 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
2318 info->jited_line_info_rec_size != sizeof(__u64))
2321 info->func_info_rec_size = sizeof(struct bpf_func_info);
2322 info->line_info_rec_size = sizeof(struct bpf_line_info);
2323 info->jited_line_info_rec_size = sizeof(__u64);
2328 static int bpf_prog_get_info_by_fd(struct file *file,
2329 struct bpf_prog *prog,
2330 const union bpf_attr *attr,
2331 union bpf_attr __user *uattr)
2333 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2334 struct bpf_prog_info info;
2335 u32 info_len = attr->info.info_len;
2336 struct bpf_prog_stats stats;
2337 char __user *uinsns;
2341 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2344 info_len = min_t(u32, sizeof(info), info_len);
2346 memset(&info, 0, sizeof(info));
2347 if (copy_from_user(&info, uinfo, info_len))
2350 info.type = prog->type;
2351 info.id = prog->aux->id;
2352 info.load_time = prog->aux->load_time;
2353 info.created_by_uid = from_kuid_munged(current_user_ns(),
2354 prog->aux->user->uid);
2355 info.gpl_compatible = prog->gpl_compatible;
2357 memcpy(info.tag, prog->tag, sizeof(prog->tag));
2358 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2360 ulen = info.nr_map_ids;
2361 info.nr_map_ids = prog->aux->used_map_cnt;
2362 ulen = min_t(u32, info.nr_map_ids, ulen);
2364 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
2367 for (i = 0; i < ulen; i++)
2368 if (put_user(prog->aux->used_maps[i]->id,
2373 err = set_info_rec_size(&info);
2377 bpf_prog_get_stats(prog, &stats);
2378 info.run_time_ns = stats.nsecs;
2379 info.run_cnt = stats.cnt;
2381 if (!capable(CAP_SYS_ADMIN)) {
2382 info.jited_prog_len = 0;
2383 info.xlated_prog_len = 0;
2384 info.nr_jited_ksyms = 0;
2385 info.nr_jited_func_lens = 0;
2386 info.nr_func_info = 0;
2387 info.nr_line_info = 0;
2388 info.nr_jited_line_info = 0;
2392 ulen = info.xlated_prog_len;
2393 info.xlated_prog_len = bpf_prog_insn_size(prog);
2394 if (info.xlated_prog_len && ulen) {
2395 struct bpf_insn *insns_sanitized;
2398 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
2399 info.xlated_prog_insns = 0;
2402 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
2403 if (!insns_sanitized)
2405 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2406 ulen = min_t(u32, info.xlated_prog_len, ulen);
2407 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2408 kfree(insns_sanitized);
2413 if (bpf_prog_is_dev_bound(prog->aux)) {
2414 err = bpf_prog_offload_info_fill(&info, prog);
2420 /* NOTE: the following code is supposed to be skipped for offload.
2421 * bpf_prog_offload_info_fill() is the place to fill similar fields
2424 ulen = info.jited_prog_len;
2425 if (prog->aux->func_cnt) {
2428 info.jited_prog_len = 0;
2429 for (i = 0; i < prog->aux->func_cnt; i++)
2430 info.jited_prog_len += prog->aux->func[i]->jited_len;
2432 info.jited_prog_len = prog->jited_len;
2435 if (info.jited_prog_len && ulen) {
2436 if (bpf_dump_raw_ok(file->f_cred)) {
2437 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2438 ulen = min_t(u32, info.jited_prog_len, ulen);
2440 /* for multi-function programs, copy the JITed
2441 * instructions for all the functions
2443 if (prog->aux->func_cnt) {
2448 for (i = 0; i < prog->aux->func_cnt; i++) {
2449 len = prog->aux->func[i]->jited_len;
2450 len = min_t(u32, len, free);
2451 img = (u8 *) prog->aux->func[i]->bpf_func;
2452 if (copy_to_user(uinsns, img, len))
2460 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2464 info.jited_prog_insns = 0;
2468 ulen = info.nr_jited_ksyms;
2469 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
2471 if (bpf_dump_raw_ok(file->f_cred)) {
2472 unsigned long ksym_addr;
2473 u64 __user *user_ksyms;
2476 /* copy the address of the kernel symbol
2477 * corresponding to each function
2479 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2480 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2481 if (prog->aux->func_cnt) {
2482 for (i = 0; i < ulen; i++) {
2483 ksym_addr = (unsigned long)
2484 prog->aux->func[i]->bpf_func;
2485 if (put_user((u64) ksym_addr,
2490 ksym_addr = (unsigned long) prog->bpf_func;
2491 if (put_user((u64) ksym_addr, &user_ksyms[0]))
2495 info.jited_ksyms = 0;
2499 ulen = info.nr_jited_func_lens;
2500 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
2502 if (bpf_dump_raw_ok(file->f_cred)) {
2503 u32 __user *user_lens;
2506 /* copy the JITed image lengths for each function */
2507 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2508 user_lens = u64_to_user_ptr(info.jited_func_lens);
2509 if (prog->aux->func_cnt) {
2510 for (i = 0; i < ulen; i++) {
2512 prog->aux->func[i]->jited_len;
2513 if (put_user(func_len, &user_lens[i]))
2517 func_len = prog->jited_len;
2518 if (put_user(func_len, &user_lens[0]))
2522 info.jited_func_lens = 0;
2527 info.btf_id = btf_id(prog->aux->btf);
2529 ulen = info.nr_func_info;
2530 info.nr_func_info = prog->aux->func_info_cnt;
2531 if (info.nr_func_info && ulen) {
2532 char __user *user_finfo;
2534 user_finfo = u64_to_user_ptr(info.func_info);
2535 ulen = min_t(u32, info.nr_func_info, ulen);
2536 if (copy_to_user(user_finfo, prog->aux->func_info,
2537 info.func_info_rec_size * ulen))
2541 ulen = info.nr_line_info;
2542 info.nr_line_info = prog->aux->nr_linfo;
2543 if (info.nr_line_info && ulen) {
2544 __u8 __user *user_linfo;
2546 user_linfo = u64_to_user_ptr(info.line_info);
2547 ulen = min_t(u32, info.nr_line_info, ulen);
2548 if (copy_to_user(user_linfo, prog->aux->linfo,
2549 info.line_info_rec_size * ulen))
2553 ulen = info.nr_jited_line_info;
2554 if (prog->aux->jited_linfo)
2555 info.nr_jited_line_info = prog->aux->nr_linfo;
2557 info.nr_jited_line_info = 0;
2558 if (info.nr_jited_line_info && ulen) {
2559 if (bpf_dump_raw_ok(file->f_cred)) {
2560 __u64 __user *user_linfo;
2563 user_linfo = u64_to_user_ptr(info.jited_line_info);
2564 ulen = min_t(u32, info.nr_jited_line_info, ulen);
2565 for (i = 0; i < ulen; i++) {
2566 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2571 info.jited_line_info = 0;
2575 ulen = info.nr_prog_tags;
2576 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2578 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2581 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2582 ulen = min_t(u32, info.nr_prog_tags, ulen);
2583 if (prog->aux->func_cnt) {
2584 for (i = 0; i < ulen; i++) {
2585 if (copy_to_user(user_prog_tags[i],
2586 prog->aux->func[i]->tag,
2591 if (copy_to_user(user_prog_tags[0],
2592 prog->tag, BPF_TAG_SIZE))
2598 if (copy_to_user(uinfo, &info, info_len) ||
2599 put_user(info_len, &uattr->info.info_len))
2605 static int bpf_map_get_info_by_fd(struct file *file,
2606 struct bpf_map *map,
2607 const union bpf_attr *attr,
2608 union bpf_attr __user *uattr)
2610 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2611 struct bpf_map_info info;
2612 u32 info_len = attr->info.info_len;
2615 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2618 info_len = min_t(u32, sizeof(info), info_len);
2620 memset(&info, 0, sizeof(info));
2621 info.type = map->map_type;
2623 info.key_size = map->key_size;
2624 info.value_size = map->value_size;
2625 info.max_entries = map->max_entries;
2626 info.map_flags = map->map_flags;
2627 memcpy(info.name, map->name, sizeof(map->name));
2630 info.btf_id = btf_id(map->btf);
2631 info.btf_key_type_id = map->btf_key_type_id;
2632 info.btf_value_type_id = map->btf_value_type_id;
2635 if (bpf_map_is_dev_bound(map)) {
2636 err = bpf_map_offload_info_fill(&info, map);
2641 if (copy_to_user(uinfo, &info, info_len) ||
2642 put_user(info_len, &uattr->info.info_len))
2648 static int bpf_btf_get_info_by_fd(struct file *file,
2650 const union bpf_attr *attr,
2651 union bpf_attr __user *uattr)
2653 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2654 u32 info_len = attr->info.info_len;
2657 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
2661 return btf_get_info_by_fd(btf, attr, uattr);
2664 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2666 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2667 union bpf_attr __user *uattr)
2669 int ufd = attr->info.bpf_fd;
2673 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2680 if (f.file->f_op == &bpf_prog_fops)
2681 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
2683 else if (f.file->f_op == &bpf_map_fops)
2684 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
2686 else if (f.file->f_op == &btf_fops)
2687 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
2695 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2697 static int bpf_btf_load(const union bpf_attr *attr)
2699 if (CHECK_ATTR(BPF_BTF_LOAD))
2702 if (!capable(CAP_SYS_ADMIN))
2705 return btf_new_fd(attr);
2708 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2710 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2712 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2715 if (!capable(CAP_SYS_ADMIN))
2718 return btf_get_fd_by_id(attr->btf_id);
2721 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2722 union bpf_attr __user *uattr,
2723 u32 prog_id, u32 fd_type,
2724 const char *buf, u64 probe_offset,
2727 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2728 u32 len = buf ? strlen(buf) : 0, input_len;
2731 if (put_user(len, &uattr->task_fd_query.buf_len))
2733 input_len = attr->task_fd_query.buf_len;
2734 if (input_len && ubuf) {
2736 /* nothing to copy, just make ubuf NULL terminated */
2739 if (put_user(zero, ubuf))
2741 } else if (input_len >= len + 1) {
2742 /* ubuf can hold the string with NULL terminator */
2743 if (copy_to_user(ubuf, buf, len + 1))
2746 /* ubuf cannot hold the string with NULL terminator,
2747 * do a partial copy with NULL terminator.
2752 if (copy_to_user(ubuf, buf, input_len - 1))
2754 if (put_user(zero, ubuf + input_len - 1))
2759 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2760 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2761 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2762 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2768 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2770 static int bpf_task_fd_query(const union bpf_attr *attr,
2771 union bpf_attr __user *uattr)
2773 pid_t pid = attr->task_fd_query.pid;
2774 u32 fd = attr->task_fd_query.fd;
2775 const struct perf_event *event;
2776 struct files_struct *files;
2777 struct task_struct *task;
2781 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2784 if (!capable(CAP_SYS_ADMIN))
2787 if (attr->task_fd_query.flags != 0)
2790 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2794 files = get_files_struct(task);
2795 put_task_struct(task);
2800 spin_lock(&files->file_lock);
2801 file = fcheck_files(files, fd);
2806 spin_unlock(&files->file_lock);
2807 put_files_struct(files);
2812 if (file->f_op == &bpf_raw_tp_fops) {
2813 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2814 struct bpf_raw_event_map *btp = raw_tp->btp;
2816 err = bpf_task_fd_query_copy(attr, uattr,
2817 raw_tp->prog->aux->id,
2818 BPF_FD_TYPE_RAW_TRACEPOINT,
2819 btp->tp->name, 0, 0);
2823 event = perf_get_event(file);
2824 if (!IS_ERR(event)) {
2825 u64 probe_offset, probe_addr;
2826 u32 prog_id, fd_type;
2829 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2830 &buf, &probe_offset,
2833 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2847 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2849 union bpf_attr attr;
2852 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
2855 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
2858 size = min_t(u32, size, sizeof(attr));
2860 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2861 memset(&attr, 0, sizeof(attr));
2862 if (copy_from_user(&attr, uattr, size) != 0)
2865 err = security_bpf(cmd, &attr, size);
2870 case BPF_MAP_CREATE:
2871 err = map_create(&attr);
2873 case BPF_MAP_LOOKUP_ELEM:
2874 err = map_lookup_elem(&attr);
2876 case BPF_MAP_UPDATE_ELEM:
2877 err = map_update_elem(&attr);
2879 case BPF_MAP_DELETE_ELEM:
2880 err = map_delete_elem(&attr);
2882 case BPF_MAP_GET_NEXT_KEY:
2883 err = map_get_next_key(&attr);
2885 case BPF_MAP_FREEZE:
2886 err = map_freeze(&attr);
2889 err = bpf_prog_load(&attr, uattr);
2892 err = bpf_obj_pin(&attr);
2895 err = bpf_obj_get(&attr);
2897 case BPF_PROG_ATTACH:
2898 err = bpf_prog_attach(&attr);
2900 case BPF_PROG_DETACH:
2901 err = bpf_prog_detach(&attr);
2903 case BPF_PROG_QUERY:
2904 err = bpf_prog_query(&attr, uattr);
2906 case BPF_PROG_TEST_RUN:
2907 err = bpf_prog_test_run(&attr, uattr);
2909 case BPF_PROG_GET_NEXT_ID:
2910 err = bpf_obj_get_next_id(&attr, uattr,
2911 &prog_idr, &prog_idr_lock);
2913 case BPF_MAP_GET_NEXT_ID:
2914 err = bpf_obj_get_next_id(&attr, uattr,
2915 &map_idr, &map_idr_lock);
2917 case BPF_BTF_GET_NEXT_ID:
2918 err = bpf_obj_get_next_id(&attr, uattr,
2919 &btf_idr, &btf_idr_lock);
2921 case BPF_PROG_GET_FD_BY_ID:
2922 err = bpf_prog_get_fd_by_id(&attr);
2924 case BPF_MAP_GET_FD_BY_ID:
2925 err = bpf_map_get_fd_by_id(&attr);
2927 case BPF_OBJ_GET_INFO_BY_FD:
2928 err = bpf_obj_get_info_by_fd(&attr, uattr);
2930 case BPF_RAW_TRACEPOINT_OPEN:
2931 err = bpf_raw_tracepoint_open(&attr);
2934 err = bpf_btf_load(&attr);
2936 case BPF_BTF_GET_FD_BY_ID:
2937 err = bpf_btf_get_fd_by_id(&attr);
2939 case BPF_TASK_FD_QUERY:
2940 err = bpf_task_fd_query(&attr, uattr);
2942 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2943 err = map_lookup_and_delete_elem(&attr);