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>
26 #include <uapi/linux/btf.h>
28 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
29 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
31 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
32 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
33 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
35 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
37 DEFINE_PER_CPU(int, bpf_prog_active);
38 static DEFINE_IDR(prog_idr);
39 static DEFINE_SPINLOCK(prog_idr_lock);
40 static DEFINE_IDR(map_idr);
41 static DEFINE_SPINLOCK(map_idr_lock);
43 int sysctl_unprivileged_bpf_disabled __read_mostly;
45 static const struct bpf_map_ops * const bpf_map_types[] = {
46 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
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(size_t 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 <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
146 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
152 return __vmalloc_node_flags_caller(size, numa_node,
153 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
154 flags, __builtin_return_address(0));
157 void bpf_map_area_free(void *area)
162 static u32 bpf_map_flags_retain_permanent(u32 flags)
164 /* Some map creation flags are not tied to the map object but
165 * rather to the map fd instead, so they have no meaning upon
166 * map object inspection since multiple file descriptors with
167 * different (access) properties can exist here. Thus, given
168 * this has zero meaning for the map itself, lets clear these
171 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
174 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
176 map->map_type = attr->map_type;
177 map->key_size = attr->key_size;
178 map->value_size = attr->value_size;
179 map->max_entries = attr->max_entries;
180 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
181 map->numa_node = bpf_map_attr_numa_node(attr);
184 static int bpf_charge_memlock(struct user_struct *user, u32 pages)
186 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
188 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
189 atomic_long_sub(pages, &user->locked_vm);
195 static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
198 atomic_long_sub(pages, &user->locked_vm);
201 int bpf_map_charge_init(struct bpf_map_memory *mem, size_t size)
203 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
204 struct user_struct *user;
207 if (size >= U32_MAX - PAGE_SIZE)
210 user = get_current_user();
211 ret = bpf_charge_memlock(user, pages);
223 void bpf_map_charge_finish(struct bpf_map_memory *mem)
225 bpf_uncharge_memlock(mem->user, mem->pages);
229 void bpf_map_charge_move(struct bpf_map_memory *dst,
230 struct bpf_map_memory *src)
234 /* Make sure src will not be used for the redundant uncharging. */
235 memset(src, 0, sizeof(struct bpf_map_memory));
238 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
242 ret = bpf_charge_memlock(map->memory.user, pages);
245 map->memory.pages += pages;
249 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
251 bpf_uncharge_memlock(map->memory.user, pages);
252 map->memory.pages -= pages;
255 static int bpf_map_alloc_id(struct bpf_map *map)
259 idr_preload(GFP_KERNEL);
260 spin_lock_bh(&map_idr_lock);
261 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
264 spin_unlock_bh(&map_idr_lock);
267 if (WARN_ON_ONCE(!id))
270 return id > 0 ? 0 : id;
273 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
277 /* Offloaded maps are removed from the IDR store when their device
278 * disappears - even if someone holds an fd to them they are unusable,
279 * the memory is gone, all ops will fail; they are simply waiting for
280 * refcnt to drop to be freed.
286 spin_lock_irqsave(&map_idr_lock, flags);
288 __acquire(&map_idr_lock);
290 idr_remove(&map_idr, map->id);
294 spin_unlock_irqrestore(&map_idr_lock, flags);
296 __release(&map_idr_lock);
299 /* called from workqueue */
300 static void bpf_map_free_deferred(struct work_struct *work)
302 struct bpf_map *map = container_of(work, struct bpf_map, work);
303 struct bpf_map_memory mem;
305 bpf_map_charge_move(&mem, &map->memory);
306 security_bpf_map_free(map);
307 /* implementation dependent freeing */
308 map->ops->map_free(map);
309 bpf_map_charge_finish(&mem);
312 static void bpf_map_put_uref(struct bpf_map *map)
314 if (atomic_dec_and_test(&map->usercnt)) {
315 if (map->ops->map_release_uref)
316 map->ops->map_release_uref(map);
320 /* decrement map refcnt and schedule it for freeing via workqueue
321 * (unrelying map implementation ops->map_free() might sleep)
323 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
325 if (atomic_dec_and_test(&map->refcnt)) {
326 /* bpf_map_free_id() must be called first */
327 bpf_map_free_id(map, do_idr_lock);
329 INIT_WORK(&map->work, bpf_map_free_deferred);
330 schedule_work(&map->work);
334 void bpf_map_put(struct bpf_map *map)
336 __bpf_map_put(map, true);
338 EXPORT_SYMBOL_GPL(bpf_map_put);
340 void bpf_map_put_with_uref(struct bpf_map *map)
342 bpf_map_put_uref(map);
346 static int bpf_map_release(struct inode *inode, struct file *filp)
348 struct bpf_map *map = filp->private_data;
350 if (map->ops->map_release)
351 map->ops->map_release(map, filp);
353 bpf_map_put_with_uref(map);
357 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
359 fmode_t mode = f.file->f_mode;
361 /* Our file permissions may have been overridden by global
362 * map permissions facing syscall side.
364 if (READ_ONCE(map->frozen))
365 mode &= ~FMODE_CAN_WRITE;
369 #ifdef CONFIG_PROC_FS
370 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
372 const struct bpf_map *map = filp->private_data;
373 const struct bpf_array *array;
374 u32 owner_prog_type = 0;
377 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
378 array = container_of(map, struct bpf_array, map);
379 owner_prog_type = array->owner_prog_type;
380 owner_jited = array->owner_jited;
397 map->memory.pages * 1ULL << PAGE_SHIFT,
399 READ_ONCE(map->frozen));
401 if (owner_prog_type) {
402 seq_printf(m, "owner_prog_type:\t%u\n",
404 seq_printf(m, "owner_jited:\t%u\n",
410 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
413 /* We need this handler such that alloc_file() enables
414 * f_mode with FMODE_CAN_READ.
419 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
420 size_t siz, loff_t *ppos)
422 /* We need this handler such that alloc_file() enables
423 * f_mode with FMODE_CAN_WRITE.
428 const struct file_operations bpf_map_fops = {
429 #ifdef CONFIG_PROC_FS
430 .show_fdinfo = bpf_map_show_fdinfo,
432 .release = bpf_map_release,
433 .read = bpf_dummy_read,
434 .write = bpf_dummy_write,
437 int bpf_map_new_fd(struct bpf_map *map, int flags)
441 ret = security_bpf_map(map, OPEN_FMODE(flags));
445 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
449 int bpf_get_file_flag(int flags)
451 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
453 if (flags & BPF_F_RDONLY)
455 if (flags & BPF_F_WRONLY)
460 /* helper macro to check that unused fields 'union bpf_attr' are zero */
461 #define CHECK_ATTR(CMD) \
462 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
463 sizeof(attr->CMD##_LAST_FIELD), 0, \
465 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
466 sizeof(attr->CMD##_LAST_FIELD)) != NULL
468 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
469 * Return 0 on success and < 0 on error.
471 static int bpf_obj_name_cpy(char *dst, const char *src)
473 const char *end = src + BPF_OBJ_NAME_LEN;
475 memset(dst, 0, BPF_OBJ_NAME_LEN);
476 /* Copy all isalnum(), '_' and '.' chars. */
477 while (src < end && *src) {
478 if (!isalnum(*src) &&
479 *src != '_' && *src != '.')
484 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
491 int map_check_no_btf(const struct bpf_map *map,
492 const struct btf *btf,
493 const struct btf_type *key_type,
494 const struct btf_type *value_type)
499 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
500 u32 btf_key_id, u32 btf_value_id)
502 const struct btf_type *key_type, *value_type;
503 u32 key_size, value_size;
506 /* Some maps allow key to be unspecified. */
508 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
509 if (!key_type || key_size != map->key_size)
512 key_type = btf_type_by_id(btf, 0);
513 if (!map->ops->map_check_btf)
517 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
518 if (!value_type || value_size != map->value_size)
521 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
523 if (map_value_has_spin_lock(map)) {
524 if (map->map_flags & BPF_F_RDONLY_PROG)
526 if (map->map_type != BPF_MAP_TYPE_HASH &&
527 map->map_type != BPF_MAP_TYPE_ARRAY &&
528 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
529 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
531 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
534 "verifier bug spin_lock_off %d value_size %d\n",
535 map->spin_lock_off, map->value_size);
540 if (map->ops->map_check_btf)
541 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
546 #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
547 /* called via syscall */
548 static int map_create(union bpf_attr *attr)
550 int numa_node = bpf_map_attr_numa_node(attr);
551 struct bpf_map_memory mem;
556 err = CHECK_ATTR(BPF_MAP_CREATE);
560 f_flags = bpf_get_file_flag(attr->map_flags);
564 if (numa_node != NUMA_NO_NODE &&
565 ((unsigned int)numa_node >= nr_node_ids ||
566 !node_online(numa_node)))
569 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
570 map = find_and_alloc_map(attr);
574 err = bpf_obj_name_cpy(map->name, attr->map_name);
578 atomic_set(&map->refcnt, 1);
579 atomic_set(&map->usercnt, 1);
581 if (attr->btf_key_type_id || attr->btf_value_type_id) {
584 if (!attr->btf_value_type_id) {
589 btf = btf_get_by_fd(attr->btf_fd);
595 err = map_check_btf(map, btf, attr->btf_key_type_id,
596 attr->btf_value_type_id);
603 map->btf_key_type_id = attr->btf_key_type_id;
604 map->btf_value_type_id = attr->btf_value_type_id;
606 map->spin_lock_off = -EINVAL;
609 err = security_bpf_map_alloc(map);
613 err = bpf_map_alloc_id(map);
617 err = bpf_map_new_fd(map, f_flags);
619 /* failed to allocate fd.
620 * bpf_map_put_with_uref() is needed because the above
621 * bpf_map_alloc_id() has published the map
622 * to the userspace and the userspace may
623 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
625 bpf_map_put_with_uref(map);
632 security_bpf_map_free(map);
635 bpf_map_charge_move(&mem, &map->memory);
636 map->ops->map_free(map);
637 bpf_map_charge_finish(&mem);
641 /* if error is returned, fd is released.
642 * On success caller should complete fd access with matching fdput()
644 struct bpf_map *__bpf_map_get(struct fd f)
647 return ERR_PTR(-EBADF);
648 if (f.file->f_op != &bpf_map_fops) {
650 return ERR_PTR(-EINVAL);
653 return f.file->private_data;
656 /* prog's and map's refcnt limit */
657 #define BPF_MAX_REFCNT 32768
659 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
661 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
662 atomic_dec(&map->refcnt);
663 return ERR_PTR(-EBUSY);
666 atomic_inc(&map->usercnt);
669 EXPORT_SYMBOL_GPL(bpf_map_inc);
671 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
673 struct fd f = fdget(ufd);
676 map = __bpf_map_get(f);
680 map = bpf_map_inc(map, true);
686 /* map_idr_lock should have been held */
687 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map,
692 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
694 if (refold >= BPF_MAX_REFCNT) {
695 __bpf_map_put(map, false);
696 return ERR_PTR(-EBUSY);
700 return ERR_PTR(-ENOENT);
703 atomic_inc(&map->usercnt);
708 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
710 spin_lock_bh(&map_idr_lock);
711 map = __bpf_map_inc_not_zero(map, uref);
712 spin_unlock_bh(&map_idr_lock);
716 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
718 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
723 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
726 return memdup_user(ukey, key_size);
729 return ERR_PTR(-EINVAL);
734 /* last field in 'union bpf_attr' used by this command */
735 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
737 static int map_lookup_elem(union bpf_attr *attr)
739 void __user *ukey = u64_to_user_ptr(attr->key);
740 void __user *uvalue = u64_to_user_ptr(attr->value);
741 int ufd = attr->map_fd;
743 void *key, *value, *ptr;
748 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
751 if (attr->flags & ~BPF_F_LOCK)
755 map = __bpf_map_get(f);
758 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
763 if ((attr->flags & BPF_F_LOCK) &&
764 !map_value_has_spin_lock(map)) {
769 key = __bpf_copy_key(ukey, map->key_size);
775 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
776 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
777 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
778 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
779 value_size = round_up(map->value_size, 8) * num_possible_cpus();
780 else if (IS_FD_MAP(map))
781 value_size = sizeof(u32);
783 value_size = map->value_size;
786 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
790 if (bpf_map_is_dev_bound(map)) {
791 err = bpf_map_offload_lookup_elem(map, key, value);
796 this_cpu_inc(bpf_prog_active);
797 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
798 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
799 err = bpf_percpu_hash_copy(map, key, value);
800 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
801 err = bpf_percpu_array_copy(map, key, value);
802 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
803 err = bpf_percpu_cgroup_storage_copy(map, key, value);
804 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
805 err = bpf_stackmap_copy(map, key, value);
806 } else if (IS_FD_ARRAY(map)) {
807 err = bpf_fd_array_map_lookup_elem(map, key, value);
808 } else if (IS_FD_HASH(map)) {
809 err = bpf_fd_htab_map_lookup_elem(map, key, value);
810 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
811 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
812 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
813 map->map_type == BPF_MAP_TYPE_STACK) {
814 err = map->ops->map_peek_elem(map, value);
817 if (map->ops->map_lookup_elem_sys_only)
818 ptr = map->ops->map_lookup_elem_sys_only(map, key);
820 ptr = map->ops->map_lookup_elem(map, key);
827 if (attr->flags & BPF_F_LOCK)
828 /* lock 'ptr' and copy everything but lock */
829 copy_map_value_locked(map, value, ptr, true);
831 copy_map_value(map, value, ptr);
832 /* mask lock, since value wasn't zero inited */
833 check_and_init_map_lock(map, value);
837 this_cpu_dec(bpf_prog_active);
845 if (copy_to_user(uvalue, value, value_size) != 0)
859 static void maybe_wait_bpf_programs(struct bpf_map *map)
861 /* Wait for any running BPF programs to complete so that
862 * userspace, when we return to it, knows that all programs
863 * that could be running use the new map value.
865 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
866 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
870 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
872 static int map_update_elem(union bpf_attr *attr)
874 void __user *ukey = u64_to_user_ptr(attr->key);
875 void __user *uvalue = u64_to_user_ptr(attr->value);
876 int ufd = attr->map_fd;
883 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
887 map = __bpf_map_get(f);
890 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
895 if ((attr->flags & BPF_F_LOCK) &&
896 !map_value_has_spin_lock(map)) {
901 key = __bpf_copy_key(ukey, map->key_size);
907 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
908 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
909 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
910 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
911 value_size = round_up(map->value_size, 8) * num_possible_cpus();
913 value_size = map->value_size;
916 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
921 if (copy_from_user(value, uvalue, value_size) != 0)
924 /* Need to create a kthread, thus must support schedule */
925 if (bpf_map_is_dev_bound(map)) {
926 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
928 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
929 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
930 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
931 err = map->ops->map_update_elem(map, key, value, attr->flags);
935 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
936 * inside bpf map update or delete otherwise deadlocks are possible
939 __this_cpu_inc(bpf_prog_active);
940 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
941 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
942 err = bpf_percpu_hash_update(map, key, value, attr->flags);
943 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
944 err = bpf_percpu_array_update(map, key, value, attr->flags);
945 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
946 err = bpf_percpu_cgroup_storage_update(map, key, value,
948 } else if (IS_FD_ARRAY(map)) {
950 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
953 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
955 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
958 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
959 /* rcu_read_lock() is not needed */
960 err = bpf_fd_reuseport_array_update_elem(map, key, value,
962 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
963 map->map_type == BPF_MAP_TYPE_STACK) {
964 err = map->ops->map_push_elem(map, value, attr->flags);
967 err = map->ops->map_update_elem(map, key, value, attr->flags);
970 __this_cpu_dec(bpf_prog_active);
972 maybe_wait_bpf_programs(map);
983 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
985 static int map_delete_elem(union bpf_attr *attr)
987 void __user *ukey = u64_to_user_ptr(attr->key);
988 int ufd = attr->map_fd;
994 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
998 map = __bpf_map_get(f);
1000 return PTR_ERR(map);
1001 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1006 key = __bpf_copy_key(ukey, map->key_size);
1012 if (bpf_map_is_dev_bound(map)) {
1013 err = bpf_map_offload_delete_elem(map, key);
1018 __this_cpu_inc(bpf_prog_active);
1020 err = map->ops->map_delete_elem(map, key);
1022 __this_cpu_dec(bpf_prog_active);
1024 maybe_wait_bpf_programs(map);
1032 /* last field in 'union bpf_attr' used by this command */
1033 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1035 static int map_get_next_key(union bpf_attr *attr)
1037 void __user *ukey = u64_to_user_ptr(attr->key);
1038 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1039 int ufd = attr->map_fd;
1040 struct bpf_map *map;
1041 void *key, *next_key;
1045 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1049 map = __bpf_map_get(f);
1051 return PTR_ERR(map);
1052 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1058 key = __bpf_copy_key(ukey, map->key_size);
1068 next_key = kmalloc(map->key_size, GFP_USER);
1072 if (bpf_map_is_dev_bound(map)) {
1073 err = bpf_map_offload_get_next_key(map, key, next_key);
1078 err = map->ops->map_get_next_key(map, key, next_key);
1085 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1099 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1101 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1103 void __user *ukey = u64_to_user_ptr(attr->key);
1104 void __user *uvalue = u64_to_user_ptr(attr->value);
1105 int ufd = attr->map_fd;
1106 struct bpf_map *map;
1112 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1116 map = __bpf_map_get(f);
1118 return PTR_ERR(map);
1119 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1124 key = __bpf_copy_key(ukey, map->key_size);
1130 value_size = map->value_size;
1133 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1137 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1138 map->map_type == BPF_MAP_TYPE_STACK) {
1139 err = map->ops->map_pop_elem(map, value);
1147 if (copy_to_user(uvalue, value, value_size) != 0)
1161 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1163 static int map_freeze(const union bpf_attr *attr)
1165 int err = 0, ufd = attr->map_fd;
1166 struct bpf_map *map;
1169 if (CHECK_ATTR(BPF_MAP_FREEZE))
1173 map = __bpf_map_get(f);
1175 return PTR_ERR(map);
1176 if (READ_ONCE(map->frozen)) {
1180 if (!capable(CAP_SYS_ADMIN)) {
1185 WRITE_ONCE(map->frozen, true);
1191 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1192 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1193 [_id] = & _name ## _prog_ops,
1194 #define BPF_MAP_TYPE(_id, _ops)
1195 #include <linux/bpf_types.h>
1196 #undef BPF_PROG_TYPE
1200 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1202 const struct bpf_prog_ops *ops;
1204 if (type >= ARRAY_SIZE(bpf_prog_types))
1206 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1207 ops = bpf_prog_types[type];
1211 if (!bpf_prog_is_dev_bound(prog->aux))
1212 prog->aux->ops = ops;
1214 prog->aux->ops = &bpf_offload_prog_ops;
1219 /* drop refcnt on maps used by eBPF program and free auxilary data */
1220 static void free_used_maps(struct bpf_prog_aux *aux)
1222 enum bpf_cgroup_storage_type stype;
1225 for_each_cgroup_storage_type(stype) {
1226 if (!aux->cgroup_storage[stype])
1228 bpf_cgroup_storage_release(aux->prog,
1229 aux->cgroup_storage[stype]);
1232 for (i = 0; i < aux->used_map_cnt; i++)
1233 bpf_map_put(aux->used_maps[i]);
1235 kfree(aux->used_maps);
1238 int __bpf_prog_charge(struct user_struct *user, u32 pages)
1240 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1241 unsigned long user_bufs;
1244 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1245 if (user_bufs > memlock_limit) {
1246 atomic_long_sub(pages, &user->locked_vm);
1254 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1257 atomic_long_sub(pages, &user->locked_vm);
1260 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1262 struct user_struct *user = get_current_user();
1265 ret = __bpf_prog_charge(user, prog->pages);
1271 prog->aux->user = user;
1275 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1277 struct user_struct *user = prog->aux->user;
1279 __bpf_prog_uncharge(user, prog->pages);
1283 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1287 idr_preload(GFP_KERNEL);
1288 spin_lock_bh(&prog_idr_lock);
1289 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1292 spin_unlock_bh(&prog_idr_lock);
1295 /* id is in [1, INT_MAX) */
1296 if (WARN_ON_ONCE(!id))
1299 return id > 0 ? 0 : id;
1302 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1304 /* cBPF to eBPF migrations are currently not in the idr store.
1305 * Offloaded programs are removed from the store when their device
1306 * disappears - even if someone grabs an fd to them they are unusable,
1307 * simply waiting for refcnt to drop to be freed.
1313 spin_lock_bh(&prog_idr_lock);
1315 __acquire(&prog_idr_lock);
1317 idr_remove(&prog_idr, prog->aux->id);
1321 spin_unlock_bh(&prog_idr_lock);
1323 __release(&prog_idr_lock);
1326 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1328 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1330 kvfree(aux->func_info);
1331 free_used_maps(aux);
1332 bpf_prog_uncharge_memlock(aux->prog);
1333 security_bpf_prog_free(aux);
1334 bpf_prog_free(aux->prog);
1337 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1339 bpf_prog_kallsyms_del_all(prog);
1340 btf_put(prog->aux->btf);
1341 bpf_prog_free_linfo(prog);
1344 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1346 __bpf_prog_put_rcu(&prog->aux->rcu);
1349 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1351 if (atomic_dec_and_test(&prog->aux->refcnt)) {
1352 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1353 /* bpf_prog_free_id() must be called first */
1354 bpf_prog_free_id(prog, do_idr_lock);
1355 __bpf_prog_put_noref(prog, true);
1359 void bpf_prog_put(struct bpf_prog *prog)
1361 __bpf_prog_put(prog, true);
1363 EXPORT_SYMBOL_GPL(bpf_prog_put);
1365 static int bpf_prog_release(struct inode *inode, struct file *filp)
1367 struct bpf_prog *prog = filp->private_data;
1373 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1374 struct bpf_prog_stats *stats)
1376 u64 nsecs = 0, cnt = 0;
1379 for_each_possible_cpu(cpu) {
1380 const struct bpf_prog_stats *st;
1384 st = per_cpu_ptr(prog->aux->stats, cpu);
1386 start = u64_stats_fetch_begin_irq(&st->syncp);
1389 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1393 stats->nsecs = nsecs;
1397 #ifdef CONFIG_PROC_FS
1398 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1400 const struct bpf_prog *prog = filp->private_data;
1401 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1402 struct bpf_prog_stats stats;
1404 bpf_prog_get_stats(prog, &stats);
1405 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1412 "run_time_ns:\t%llu\n"
1417 prog->pages * 1ULL << PAGE_SHIFT,
1424 const struct file_operations bpf_prog_fops = {
1425 #ifdef CONFIG_PROC_FS
1426 .show_fdinfo = bpf_prog_show_fdinfo,
1428 .release = bpf_prog_release,
1429 .read = bpf_dummy_read,
1430 .write = bpf_dummy_write,
1433 int bpf_prog_new_fd(struct bpf_prog *prog)
1437 ret = security_bpf_prog(prog);
1441 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1442 O_RDWR | O_CLOEXEC);
1445 static struct bpf_prog *____bpf_prog_get(struct fd f)
1448 return ERR_PTR(-EBADF);
1449 if (f.file->f_op != &bpf_prog_fops) {
1451 return ERR_PTR(-EINVAL);
1454 return f.file->private_data;
1457 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
1459 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1460 atomic_sub(i, &prog->aux->refcnt);
1461 return ERR_PTR(-EBUSY);
1465 EXPORT_SYMBOL_GPL(bpf_prog_add);
1467 void bpf_prog_sub(struct bpf_prog *prog, int i)
1469 /* Only to be used for undoing previous bpf_prog_add() in some
1470 * error path. We still know that another entity in our call
1471 * path holds a reference to the program, thus atomic_sub() can
1472 * be safely used in such cases!
1474 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1476 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1478 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1480 return bpf_prog_add(prog, 1);
1482 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1484 /* prog_idr_lock should have been held */
1485 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1489 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1491 if (refold >= BPF_MAX_REFCNT) {
1492 __bpf_prog_put(prog, false);
1493 return ERR_PTR(-EBUSY);
1497 return ERR_PTR(-ENOENT);
1501 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1503 bool bpf_prog_get_ok(struct bpf_prog *prog,
1504 enum bpf_prog_type *attach_type, bool attach_drv)
1506 /* not an attachment, just a refcount inc, always allow */
1510 if (prog->type != *attach_type)
1512 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1518 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1521 struct fd f = fdget(ufd);
1522 struct bpf_prog *prog;
1524 prog = ____bpf_prog_get(f);
1527 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1528 prog = ERR_PTR(-EINVAL);
1532 prog = bpf_prog_inc(prog);
1538 struct bpf_prog *bpf_prog_get(u32 ufd)
1540 return __bpf_prog_get(ufd, NULL, false);
1543 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1546 return __bpf_prog_get(ufd, &type, attach_drv);
1548 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1550 /* Initially all BPF programs could be loaded w/o specifying
1551 * expected_attach_type. Later for some of them specifying expected_attach_type
1552 * at load time became required so that program could be validated properly.
1553 * Programs of types that are allowed to be loaded both w/ and w/o (for
1554 * backward compatibility) expected_attach_type, should have the default attach
1555 * type assigned to expected_attach_type for the latter case, so that it can be
1556 * validated later at attach time.
1558 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1559 * prog type requires it but has some attach types that have to be backward
1562 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1564 switch (attr->prog_type) {
1565 case BPF_PROG_TYPE_CGROUP_SOCK:
1566 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1567 * exist so checking for non-zero is the way to go here.
1569 if (!attr->expected_attach_type)
1570 attr->expected_attach_type =
1571 BPF_CGROUP_INET_SOCK_CREATE;
1577 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1578 enum bpf_attach_type expected_attach_type,
1581 switch (prog_type) {
1582 case BPF_PROG_TYPE_TRACING:
1583 if (btf_id > BTF_MAX_TYPE)
1592 switch (prog_type) {
1593 case BPF_PROG_TYPE_CGROUP_SOCK:
1594 switch (expected_attach_type) {
1595 case BPF_CGROUP_INET_SOCK_CREATE:
1596 case BPF_CGROUP_INET4_POST_BIND:
1597 case BPF_CGROUP_INET6_POST_BIND:
1602 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1603 switch (expected_attach_type) {
1604 case BPF_CGROUP_INET4_BIND:
1605 case BPF_CGROUP_INET6_BIND:
1606 case BPF_CGROUP_INET4_CONNECT:
1607 case BPF_CGROUP_INET6_CONNECT:
1608 case BPF_CGROUP_UDP4_SENDMSG:
1609 case BPF_CGROUP_UDP6_SENDMSG:
1610 case BPF_CGROUP_UDP4_RECVMSG:
1611 case BPF_CGROUP_UDP6_RECVMSG:
1616 case BPF_PROG_TYPE_CGROUP_SKB:
1617 switch (expected_attach_type) {
1618 case BPF_CGROUP_INET_INGRESS:
1619 case BPF_CGROUP_INET_EGRESS:
1624 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1625 switch (expected_attach_type) {
1626 case BPF_CGROUP_SETSOCKOPT:
1627 case BPF_CGROUP_GETSOCKOPT:
1637 /* last field in 'union bpf_attr' used by this command */
1638 #define BPF_PROG_LOAD_LAST_FIELD attach_btf_id
1640 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
1642 enum bpf_prog_type type = attr->prog_type;
1643 struct bpf_prog *prog;
1648 if (CHECK_ATTR(BPF_PROG_LOAD))
1651 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
1652 BPF_F_ANY_ALIGNMENT |
1653 BPF_F_TEST_STATE_FREQ |
1654 BPF_F_TEST_RND_HI32))
1657 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1658 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1659 !capable(CAP_SYS_ADMIN))
1662 /* copy eBPF program license from user space */
1663 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1664 sizeof(license) - 1) < 0)
1666 license[sizeof(license) - 1] = 0;
1668 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1669 is_gpl = license_is_gpl_compatible(license);
1671 if (attr->insn_cnt == 0 ||
1672 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
1674 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1675 type != BPF_PROG_TYPE_CGROUP_SKB &&
1676 !capable(CAP_SYS_ADMIN))
1679 bpf_prog_load_fixup_attach_type(attr);
1680 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
1681 attr->attach_btf_id))
1684 /* plain bpf_prog allocation */
1685 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1689 prog->expected_attach_type = attr->expected_attach_type;
1690 prog->aux->attach_btf_id = attr->attach_btf_id;
1692 prog->aux->offload_requested = !!attr->prog_ifindex;
1694 err = security_bpf_prog_alloc(prog->aux);
1696 goto free_prog_nouncharge;
1698 err = bpf_prog_charge_memlock(prog);
1702 prog->len = attr->insn_cnt;
1705 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1706 bpf_prog_insn_size(prog)) != 0)
1709 prog->orig_prog = NULL;
1712 atomic_set(&prog->aux->refcnt, 1);
1713 prog->gpl_compatible = is_gpl ? 1 : 0;
1715 if (bpf_prog_is_dev_bound(prog->aux)) {
1716 err = bpf_prog_offload_init(prog, attr);
1721 /* find program type: socket_filter vs tracing_filter */
1722 err = find_prog_type(type, prog);
1726 prog->aux->load_time = ktime_get_boottime_ns();
1727 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1731 /* run eBPF verifier */
1732 err = bpf_check(&prog, attr, uattr);
1734 goto free_used_maps;
1736 prog = bpf_prog_select_runtime(prog, &err);
1738 goto free_used_maps;
1740 err = bpf_prog_alloc_id(prog);
1742 goto free_used_maps;
1744 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
1745 * effectively publicly exposed. However, retrieving via
1746 * bpf_prog_get_fd_by_id() will take another reference,
1747 * therefore it cannot be gone underneath us.
1749 * Only for the time /after/ successful bpf_prog_new_fd()
1750 * and before returning to userspace, we might just hold
1751 * one reference and any parallel close on that fd could
1752 * rip everything out. Hence, below notifications must
1753 * happen before bpf_prog_new_fd().
1755 * Also, any failure handling from this point onwards must
1756 * be using bpf_prog_put() given the program is exposed.
1758 bpf_prog_kallsyms_add(prog);
1759 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
1761 err = bpf_prog_new_fd(prog);
1767 /* In case we have subprogs, we need to wait for a grace
1768 * period before we can tear down JIT memory since symbols
1769 * are already exposed under kallsyms.
1771 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
1774 bpf_prog_uncharge_memlock(prog);
1776 security_bpf_prog_free(prog->aux);
1777 free_prog_nouncharge:
1778 bpf_prog_free(prog);
1782 #define BPF_OBJ_LAST_FIELD file_flags
1784 static int bpf_obj_pin(const union bpf_attr *attr)
1786 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1789 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1792 static int bpf_obj_get(const union bpf_attr *attr)
1794 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1795 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1798 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1802 static int bpf_tracing_prog_release(struct inode *inode, struct file *filp)
1804 struct bpf_prog *prog = filp->private_data;
1806 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
1811 static const struct file_operations bpf_tracing_prog_fops = {
1812 .release = bpf_tracing_prog_release,
1813 .read = bpf_dummy_read,
1814 .write = bpf_dummy_write,
1817 static int bpf_tracing_prog_attach(struct bpf_prog *prog)
1821 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
1822 prog->expected_attach_type != BPF_TRACE_FEXIT) {
1827 err = bpf_trampoline_link_prog(prog);
1831 tr_fd = anon_inode_getfd("bpf-tracing-prog", &bpf_tracing_prog_fops,
1834 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
1845 struct bpf_raw_tracepoint {
1846 struct bpf_raw_event_map *btp;
1847 struct bpf_prog *prog;
1850 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1852 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1855 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1856 bpf_prog_put(raw_tp->prog);
1858 bpf_put_raw_tracepoint(raw_tp->btp);
1863 static const struct file_operations bpf_raw_tp_fops = {
1864 .release = bpf_raw_tracepoint_release,
1865 .read = bpf_dummy_read,
1866 .write = bpf_dummy_write,
1869 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1871 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1873 struct bpf_raw_tracepoint *raw_tp;
1874 struct bpf_raw_event_map *btp;
1875 struct bpf_prog *prog;
1876 const char *tp_name;
1880 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
1883 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
1885 return PTR_ERR(prog);
1887 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1888 prog->type != BPF_PROG_TYPE_TRACING &&
1889 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
1894 if (prog->type == BPF_PROG_TYPE_TRACING) {
1895 if (attr->raw_tracepoint.name) {
1896 /* The attach point for this category of programs
1897 * should be specified via btf_id during program load.
1902 if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
1903 tp_name = prog->aux->attach_func_name;
1905 return bpf_tracing_prog_attach(prog);
1907 if (strncpy_from_user(buf,
1908 u64_to_user_ptr(attr->raw_tracepoint.name),
1909 sizeof(buf) - 1) < 0) {
1913 buf[sizeof(buf) - 1] = 0;
1917 btp = bpf_get_raw_tracepoint(tp_name);
1923 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1929 raw_tp->prog = prog;
1931 err = bpf_probe_register(raw_tp->btp, prog);
1935 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1938 bpf_probe_unregister(raw_tp->btp, prog);
1947 bpf_put_raw_tracepoint(btp);
1953 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1954 enum bpf_attach_type attach_type)
1956 switch (prog->type) {
1957 case BPF_PROG_TYPE_CGROUP_SOCK:
1958 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1959 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1960 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1961 case BPF_PROG_TYPE_CGROUP_SKB:
1962 return prog->enforce_expected_attach_type &&
1963 prog->expected_attach_type != attach_type ?
1970 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1972 #define BPF_F_ATTACH_MASK \
1973 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1975 static int bpf_prog_attach(const union bpf_attr *attr)
1977 enum bpf_prog_type ptype;
1978 struct bpf_prog *prog;
1981 if (!capable(CAP_NET_ADMIN))
1984 if (CHECK_ATTR(BPF_PROG_ATTACH))
1987 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
1990 switch (attr->attach_type) {
1991 case BPF_CGROUP_INET_INGRESS:
1992 case BPF_CGROUP_INET_EGRESS:
1993 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1995 case BPF_CGROUP_INET_SOCK_CREATE:
1996 case BPF_CGROUP_INET4_POST_BIND:
1997 case BPF_CGROUP_INET6_POST_BIND:
1998 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2000 case BPF_CGROUP_INET4_BIND:
2001 case BPF_CGROUP_INET6_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_UDP4_RECVMSG:
2007 case BPF_CGROUP_UDP6_RECVMSG:
2008 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2010 case BPF_CGROUP_SOCK_OPS:
2011 ptype = BPF_PROG_TYPE_SOCK_OPS;
2013 case BPF_CGROUP_DEVICE:
2014 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2016 case BPF_SK_MSG_VERDICT:
2017 ptype = BPF_PROG_TYPE_SK_MSG;
2019 case BPF_SK_SKB_STREAM_PARSER:
2020 case BPF_SK_SKB_STREAM_VERDICT:
2021 ptype = BPF_PROG_TYPE_SK_SKB;
2023 case BPF_LIRC_MODE2:
2024 ptype = BPF_PROG_TYPE_LIRC_MODE2;
2026 case BPF_FLOW_DISSECTOR:
2027 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
2029 case BPF_CGROUP_SYSCTL:
2030 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2032 case BPF_CGROUP_GETSOCKOPT:
2033 case BPF_CGROUP_SETSOCKOPT:
2034 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2040 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2042 return PTR_ERR(prog);
2044 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
2050 case BPF_PROG_TYPE_SK_SKB:
2051 case BPF_PROG_TYPE_SK_MSG:
2052 ret = sock_map_get_from_fd(attr, prog);
2054 case BPF_PROG_TYPE_LIRC_MODE2:
2055 ret = lirc_prog_attach(attr, prog);
2057 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2058 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
2061 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
2069 #define BPF_PROG_DETACH_LAST_FIELD attach_type
2071 static int bpf_prog_detach(const union bpf_attr *attr)
2073 enum bpf_prog_type ptype;
2075 if (!capable(CAP_NET_ADMIN))
2078 if (CHECK_ATTR(BPF_PROG_DETACH))
2081 switch (attr->attach_type) {
2082 case BPF_CGROUP_INET_INGRESS:
2083 case BPF_CGROUP_INET_EGRESS:
2084 ptype = BPF_PROG_TYPE_CGROUP_SKB;
2086 case BPF_CGROUP_INET_SOCK_CREATE:
2087 case BPF_CGROUP_INET4_POST_BIND:
2088 case BPF_CGROUP_INET6_POST_BIND:
2089 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2091 case BPF_CGROUP_INET4_BIND:
2092 case BPF_CGROUP_INET6_BIND:
2093 case BPF_CGROUP_INET4_CONNECT:
2094 case BPF_CGROUP_INET6_CONNECT:
2095 case BPF_CGROUP_UDP4_SENDMSG:
2096 case BPF_CGROUP_UDP6_SENDMSG:
2097 case BPF_CGROUP_UDP4_RECVMSG:
2098 case BPF_CGROUP_UDP6_RECVMSG:
2099 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2101 case BPF_CGROUP_SOCK_OPS:
2102 ptype = BPF_PROG_TYPE_SOCK_OPS;
2104 case BPF_CGROUP_DEVICE:
2105 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2107 case BPF_SK_MSG_VERDICT:
2108 return sock_map_get_from_fd(attr, NULL);
2109 case BPF_SK_SKB_STREAM_PARSER:
2110 case BPF_SK_SKB_STREAM_VERDICT:
2111 return sock_map_get_from_fd(attr, NULL);
2112 case BPF_LIRC_MODE2:
2113 return lirc_prog_detach(attr);
2114 case BPF_FLOW_DISSECTOR:
2115 return skb_flow_dissector_bpf_prog_detach(attr);
2116 case BPF_CGROUP_SYSCTL:
2117 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2119 case BPF_CGROUP_GETSOCKOPT:
2120 case BPF_CGROUP_SETSOCKOPT:
2121 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2127 return cgroup_bpf_prog_detach(attr, ptype);
2130 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2132 static int bpf_prog_query(const union bpf_attr *attr,
2133 union bpf_attr __user *uattr)
2135 if (!capable(CAP_NET_ADMIN))
2137 if (CHECK_ATTR(BPF_PROG_QUERY))
2139 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2142 switch (attr->query.attach_type) {
2143 case BPF_CGROUP_INET_INGRESS:
2144 case BPF_CGROUP_INET_EGRESS:
2145 case BPF_CGROUP_INET_SOCK_CREATE:
2146 case BPF_CGROUP_INET4_BIND:
2147 case BPF_CGROUP_INET6_BIND:
2148 case BPF_CGROUP_INET4_POST_BIND:
2149 case BPF_CGROUP_INET6_POST_BIND:
2150 case BPF_CGROUP_INET4_CONNECT:
2151 case BPF_CGROUP_INET6_CONNECT:
2152 case BPF_CGROUP_UDP4_SENDMSG:
2153 case BPF_CGROUP_UDP6_SENDMSG:
2154 case BPF_CGROUP_UDP4_RECVMSG:
2155 case BPF_CGROUP_UDP6_RECVMSG:
2156 case BPF_CGROUP_SOCK_OPS:
2157 case BPF_CGROUP_DEVICE:
2158 case BPF_CGROUP_SYSCTL:
2159 case BPF_CGROUP_GETSOCKOPT:
2160 case BPF_CGROUP_SETSOCKOPT:
2162 case BPF_LIRC_MODE2:
2163 return lirc_prog_query(attr, uattr);
2164 case BPF_FLOW_DISSECTOR:
2165 return skb_flow_dissector_prog_query(attr, uattr);
2170 return cgroup_bpf_prog_query(attr, uattr);
2173 #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
2175 static int bpf_prog_test_run(const union bpf_attr *attr,
2176 union bpf_attr __user *uattr)
2178 struct bpf_prog *prog;
2179 int ret = -ENOTSUPP;
2181 if (!capable(CAP_SYS_ADMIN))
2183 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2186 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2187 (!attr->test.ctx_size_in && attr->test.ctx_in))
2190 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2191 (!attr->test.ctx_size_out && attr->test.ctx_out))
2194 prog = bpf_prog_get(attr->test.prog_fd);
2196 return PTR_ERR(prog);
2198 if (prog->aux->ops->test_run)
2199 ret = prog->aux->ops->test_run(prog, attr, uattr);
2205 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2207 static int bpf_obj_get_next_id(const union bpf_attr *attr,
2208 union bpf_attr __user *uattr,
2212 u32 next_id = attr->start_id;
2215 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2218 if (!capable(CAP_SYS_ADMIN))
2223 if (!idr_get_next(idr, &next_id))
2225 spin_unlock_bh(lock);
2228 err = put_user(next_id, &uattr->next_id);
2233 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2235 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2237 struct bpf_prog *prog;
2238 u32 id = attr->prog_id;
2241 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2244 if (!capable(CAP_SYS_ADMIN))
2247 spin_lock_bh(&prog_idr_lock);
2248 prog = idr_find(&prog_idr, id);
2250 prog = bpf_prog_inc_not_zero(prog);
2252 prog = ERR_PTR(-ENOENT);
2253 spin_unlock_bh(&prog_idr_lock);
2256 return PTR_ERR(prog);
2258 fd = bpf_prog_new_fd(prog);
2265 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2267 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2269 struct bpf_map *map;
2270 u32 id = attr->map_id;
2274 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2275 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
2278 if (!capable(CAP_SYS_ADMIN))
2281 f_flags = bpf_get_file_flag(attr->open_flags);
2285 spin_lock_bh(&map_idr_lock);
2286 map = idr_find(&map_idr, id);
2288 map = __bpf_map_inc_not_zero(map, true);
2290 map = ERR_PTR(-ENOENT);
2291 spin_unlock_bh(&map_idr_lock);
2294 return PTR_ERR(map);
2296 fd = bpf_map_new_fd(map, f_flags);
2298 bpf_map_put_with_uref(map);
2303 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
2304 unsigned long addr, u32 *off,
2307 const struct bpf_map *map;
2310 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2311 map = prog->aux->used_maps[i];
2312 if (map == (void *)addr) {
2313 *type = BPF_PSEUDO_MAP_FD;
2316 if (!map->ops->map_direct_value_meta)
2318 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2319 *type = BPF_PSEUDO_MAP_VALUE;
2327 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2329 const struct bpf_map *map;
2330 struct bpf_insn *insns;
2335 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2340 for (i = 0; i < prog->len; i++) {
2341 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2342 insns[i].code = BPF_JMP | BPF_CALL;
2343 insns[i].imm = BPF_FUNC_tail_call;
2346 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2347 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2348 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2349 insns[i].code = BPF_JMP | BPF_CALL;
2350 if (!bpf_dump_raw_ok())
2355 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2358 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
2359 map = bpf_map_from_imm(prog, imm, &off, &type);
2361 insns[i].src_reg = type;
2362 insns[i].imm = map->id;
2363 insns[i + 1].imm = off;
2371 static int set_info_rec_size(struct bpf_prog_info *info)
2374 * Ensure info.*_rec_size is the same as kernel expected size
2378 * Only allow zero *_rec_size if both _rec_size and _cnt are
2379 * zero. In this case, the kernel will set the expected
2380 * _rec_size back to the info.
2383 if ((info->nr_func_info || info->func_info_rec_size) &&
2384 info->func_info_rec_size != sizeof(struct bpf_func_info))
2387 if ((info->nr_line_info || info->line_info_rec_size) &&
2388 info->line_info_rec_size != sizeof(struct bpf_line_info))
2391 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
2392 info->jited_line_info_rec_size != sizeof(__u64))
2395 info->func_info_rec_size = sizeof(struct bpf_func_info);
2396 info->line_info_rec_size = sizeof(struct bpf_line_info);
2397 info->jited_line_info_rec_size = sizeof(__u64);
2402 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2403 const union bpf_attr *attr,
2404 union bpf_attr __user *uattr)
2406 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2407 struct bpf_prog_info info = {};
2408 u32 info_len = attr->info.info_len;
2409 struct bpf_prog_stats stats;
2410 char __user *uinsns;
2414 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2417 info_len = min_t(u32, sizeof(info), info_len);
2419 if (copy_from_user(&info, uinfo, info_len))
2422 info.type = prog->type;
2423 info.id = prog->aux->id;
2424 info.load_time = prog->aux->load_time;
2425 info.created_by_uid = from_kuid_munged(current_user_ns(),
2426 prog->aux->user->uid);
2427 info.gpl_compatible = prog->gpl_compatible;
2429 memcpy(info.tag, prog->tag, sizeof(prog->tag));
2430 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2432 ulen = info.nr_map_ids;
2433 info.nr_map_ids = prog->aux->used_map_cnt;
2434 ulen = min_t(u32, info.nr_map_ids, ulen);
2436 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
2439 for (i = 0; i < ulen; i++)
2440 if (put_user(prog->aux->used_maps[i]->id,
2445 err = set_info_rec_size(&info);
2449 bpf_prog_get_stats(prog, &stats);
2450 info.run_time_ns = stats.nsecs;
2451 info.run_cnt = stats.cnt;
2453 if (!capable(CAP_SYS_ADMIN)) {
2454 info.jited_prog_len = 0;
2455 info.xlated_prog_len = 0;
2456 info.nr_jited_ksyms = 0;
2457 info.nr_jited_func_lens = 0;
2458 info.nr_func_info = 0;
2459 info.nr_line_info = 0;
2460 info.nr_jited_line_info = 0;
2464 ulen = info.xlated_prog_len;
2465 info.xlated_prog_len = bpf_prog_insn_size(prog);
2466 if (info.xlated_prog_len && ulen) {
2467 struct bpf_insn *insns_sanitized;
2470 if (prog->blinded && !bpf_dump_raw_ok()) {
2471 info.xlated_prog_insns = 0;
2474 insns_sanitized = bpf_insn_prepare_dump(prog);
2475 if (!insns_sanitized)
2477 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2478 ulen = min_t(u32, info.xlated_prog_len, ulen);
2479 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2480 kfree(insns_sanitized);
2485 if (bpf_prog_is_dev_bound(prog->aux)) {
2486 err = bpf_prog_offload_info_fill(&info, prog);
2492 /* NOTE: the following code is supposed to be skipped for offload.
2493 * bpf_prog_offload_info_fill() is the place to fill similar fields
2496 ulen = info.jited_prog_len;
2497 if (prog->aux->func_cnt) {
2500 info.jited_prog_len = 0;
2501 for (i = 0; i < prog->aux->func_cnt; i++)
2502 info.jited_prog_len += prog->aux->func[i]->jited_len;
2504 info.jited_prog_len = prog->jited_len;
2507 if (info.jited_prog_len && ulen) {
2508 if (bpf_dump_raw_ok()) {
2509 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2510 ulen = min_t(u32, info.jited_prog_len, ulen);
2512 /* for multi-function programs, copy the JITed
2513 * instructions for all the functions
2515 if (prog->aux->func_cnt) {
2520 for (i = 0; i < prog->aux->func_cnt; i++) {
2521 len = prog->aux->func[i]->jited_len;
2522 len = min_t(u32, len, free);
2523 img = (u8 *) prog->aux->func[i]->bpf_func;
2524 if (copy_to_user(uinsns, img, len))
2532 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2536 info.jited_prog_insns = 0;
2540 ulen = info.nr_jited_ksyms;
2541 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
2543 if (bpf_dump_raw_ok()) {
2544 unsigned long ksym_addr;
2545 u64 __user *user_ksyms;
2548 /* copy the address of the kernel symbol
2549 * corresponding to each function
2551 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2552 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2553 if (prog->aux->func_cnt) {
2554 for (i = 0; i < ulen; i++) {
2555 ksym_addr = (unsigned long)
2556 prog->aux->func[i]->bpf_func;
2557 if (put_user((u64) ksym_addr,
2562 ksym_addr = (unsigned long) prog->bpf_func;
2563 if (put_user((u64) ksym_addr, &user_ksyms[0]))
2567 info.jited_ksyms = 0;
2571 ulen = info.nr_jited_func_lens;
2572 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
2574 if (bpf_dump_raw_ok()) {
2575 u32 __user *user_lens;
2578 /* copy the JITed image lengths for each function */
2579 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2580 user_lens = u64_to_user_ptr(info.jited_func_lens);
2581 if (prog->aux->func_cnt) {
2582 for (i = 0; i < ulen; i++) {
2584 prog->aux->func[i]->jited_len;
2585 if (put_user(func_len, &user_lens[i]))
2589 func_len = prog->jited_len;
2590 if (put_user(func_len, &user_lens[0]))
2594 info.jited_func_lens = 0;
2599 info.btf_id = btf_id(prog->aux->btf);
2601 ulen = info.nr_func_info;
2602 info.nr_func_info = prog->aux->func_info_cnt;
2603 if (info.nr_func_info && ulen) {
2604 char __user *user_finfo;
2606 user_finfo = u64_to_user_ptr(info.func_info);
2607 ulen = min_t(u32, info.nr_func_info, ulen);
2608 if (copy_to_user(user_finfo, prog->aux->func_info,
2609 info.func_info_rec_size * ulen))
2613 ulen = info.nr_line_info;
2614 info.nr_line_info = prog->aux->nr_linfo;
2615 if (info.nr_line_info && ulen) {
2616 __u8 __user *user_linfo;
2618 user_linfo = u64_to_user_ptr(info.line_info);
2619 ulen = min_t(u32, info.nr_line_info, ulen);
2620 if (copy_to_user(user_linfo, prog->aux->linfo,
2621 info.line_info_rec_size * ulen))
2625 ulen = info.nr_jited_line_info;
2626 if (prog->aux->jited_linfo)
2627 info.nr_jited_line_info = prog->aux->nr_linfo;
2629 info.nr_jited_line_info = 0;
2630 if (info.nr_jited_line_info && ulen) {
2631 if (bpf_dump_raw_ok()) {
2632 __u64 __user *user_linfo;
2635 user_linfo = u64_to_user_ptr(info.jited_line_info);
2636 ulen = min_t(u32, info.nr_jited_line_info, ulen);
2637 for (i = 0; i < ulen; i++) {
2638 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2643 info.jited_line_info = 0;
2647 ulen = info.nr_prog_tags;
2648 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2650 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2653 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2654 ulen = min_t(u32, info.nr_prog_tags, ulen);
2655 if (prog->aux->func_cnt) {
2656 for (i = 0; i < ulen; i++) {
2657 if (copy_to_user(user_prog_tags[i],
2658 prog->aux->func[i]->tag,
2663 if (copy_to_user(user_prog_tags[0],
2664 prog->tag, BPF_TAG_SIZE))
2670 if (copy_to_user(uinfo, &info, info_len) ||
2671 put_user(info_len, &uattr->info.info_len))
2677 static int bpf_map_get_info_by_fd(struct bpf_map *map,
2678 const union bpf_attr *attr,
2679 union bpf_attr __user *uattr)
2681 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2682 struct bpf_map_info info = {};
2683 u32 info_len = attr->info.info_len;
2686 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2689 info_len = min_t(u32, sizeof(info), info_len);
2691 info.type = map->map_type;
2693 info.key_size = map->key_size;
2694 info.value_size = map->value_size;
2695 info.max_entries = map->max_entries;
2696 info.map_flags = map->map_flags;
2697 memcpy(info.name, map->name, sizeof(map->name));
2700 info.btf_id = btf_id(map->btf);
2701 info.btf_key_type_id = map->btf_key_type_id;
2702 info.btf_value_type_id = map->btf_value_type_id;
2705 if (bpf_map_is_dev_bound(map)) {
2706 err = bpf_map_offload_info_fill(&info, map);
2711 if (copy_to_user(uinfo, &info, info_len) ||
2712 put_user(info_len, &uattr->info.info_len))
2718 static int bpf_btf_get_info_by_fd(struct btf *btf,
2719 const union bpf_attr *attr,
2720 union bpf_attr __user *uattr)
2722 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2723 u32 info_len = attr->info.info_len;
2726 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
2730 return btf_get_info_by_fd(btf, attr, uattr);
2733 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2735 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2736 union bpf_attr __user *uattr)
2738 int ufd = attr->info.bpf_fd;
2742 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2749 if (f.file->f_op == &bpf_prog_fops)
2750 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2752 else if (f.file->f_op == &bpf_map_fops)
2753 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2755 else if (f.file->f_op == &btf_fops)
2756 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
2764 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2766 static int bpf_btf_load(const union bpf_attr *attr)
2768 if (CHECK_ATTR(BPF_BTF_LOAD))
2771 if (!capable(CAP_SYS_ADMIN))
2774 return btf_new_fd(attr);
2777 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2779 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2781 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2784 if (!capable(CAP_SYS_ADMIN))
2787 return btf_get_fd_by_id(attr->btf_id);
2790 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2791 union bpf_attr __user *uattr,
2792 u32 prog_id, u32 fd_type,
2793 const char *buf, u64 probe_offset,
2796 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2797 u32 len = buf ? strlen(buf) : 0, input_len;
2800 if (put_user(len, &uattr->task_fd_query.buf_len))
2802 input_len = attr->task_fd_query.buf_len;
2803 if (input_len && ubuf) {
2805 /* nothing to copy, just make ubuf NULL terminated */
2808 if (put_user(zero, ubuf))
2810 } else if (input_len >= len + 1) {
2811 /* ubuf can hold the string with NULL terminator */
2812 if (copy_to_user(ubuf, buf, len + 1))
2815 /* ubuf cannot hold the string with NULL terminator,
2816 * do a partial copy with NULL terminator.
2821 if (copy_to_user(ubuf, buf, input_len - 1))
2823 if (put_user(zero, ubuf + input_len - 1))
2828 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2829 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2830 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2831 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2837 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2839 static int bpf_task_fd_query(const union bpf_attr *attr,
2840 union bpf_attr __user *uattr)
2842 pid_t pid = attr->task_fd_query.pid;
2843 u32 fd = attr->task_fd_query.fd;
2844 const struct perf_event *event;
2845 struct files_struct *files;
2846 struct task_struct *task;
2850 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2853 if (!capable(CAP_SYS_ADMIN))
2856 if (attr->task_fd_query.flags != 0)
2859 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2863 files = get_files_struct(task);
2864 put_task_struct(task);
2869 spin_lock(&files->file_lock);
2870 file = fcheck_files(files, fd);
2875 spin_unlock(&files->file_lock);
2876 put_files_struct(files);
2881 if (file->f_op == &bpf_raw_tp_fops) {
2882 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2883 struct bpf_raw_event_map *btp = raw_tp->btp;
2885 err = bpf_task_fd_query_copy(attr, uattr,
2886 raw_tp->prog->aux->id,
2887 BPF_FD_TYPE_RAW_TRACEPOINT,
2888 btp->tp->name, 0, 0);
2892 event = perf_get_event(file);
2893 if (!IS_ERR(event)) {
2894 u64 probe_offset, probe_addr;
2895 u32 prog_id, fd_type;
2898 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2899 &buf, &probe_offset,
2902 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2916 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2918 union bpf_attr attr = {};
2921 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
2924 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
2927 size = min_t(u32, size, sizeof(attr));
2929 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2930 if (copy_from_user(&attr, uattr, size) != 0)
2933 err = security_bpf(cmd, &attr, size);
2938 case BPF_MAP_CREATE:
2939 err = map_create(&attr);
2941 case BPF_MAP_LOOKUP_ELEM:
2942 err = map_lookup_elem(&attr);
2944 case BPF_MAP_UPDATE_ELEM:
2945 err = map_update_elem(&attr);
2947 case BPF_MAP_DELETE_ELEM:
2948 err = map_delete_elem(&attr);
2950 case BPF_MAP_GET_NEXT_KEY:
2951 err = map_get_next_key(&attr);
2953 case BPF_MAP_FREEZE:
2954 err = map_freeze(&attr);
2957 err = bpf_prog_load(&attr, uattr);
2960 err = bpf_obj_pin(&attr);
2963 err = bpf_obj_get(&attr);
2965 case BPF_PROG_ATTACH:
2966 err = bpf_prog_attach(&attr);
2968 case BPF_PROG_DETACH:
2969 err = bpf_prog_detach(&attr);
2971 case BPF_PROG_QUERY:
2972 err = bpf_prog_query(&attr, uattr);
2974 case BPF_PROG_TEST_RUN:
2975 err = bpf_prog_test_run(&attr, uattr);
2977 case BPF_PROG_GET_NEXT_ID:
2978 err = bpf_obj_get_next_id(&attr, uattr,
2979 &prog_idr, &prog_idr_lock);
2981 case BPF_MAP_GET_NEXT_ID:
2982 err = bpf_obj_get_next_id(&attr, uattr,
2983 &map_idr, &map_idr_lock);
2985 case BPF_BTF_GET_NEXT_ID:
2986 err = bpf_obj_get_next_id(&attr, uattr,
2987 &btf_idr, &btf_idr_lock);
2989 case BPF_PROG_GET_FD_BY_ID:
2990 err = bpf_prog_get_fd_by_id(&attr);
2992 case BPF_MAP_GET_FD_BY_ID:
2993 err = bpf_map_get_fd_by_id(&attr);
2995 case BPF_OBJ_GET_INFO_BY_FD:
2996 err = bpf_obj_get_info_by_fd(&attr, uattr);
2998 case BPF_RAW_TRACEPOINT_OPEN:
2999 err = bpf_raw_tracepoint_open(&attr);
3002 err = bpf_btf_load(&attr);
3004 case BPF_BTF_GET_FD_BY_ID:
3005 err = bpf_btf_get_fd_by_id(&attr);
3007 case BPF_TASK_FD_QUERY:
3008 err = bpf_task_fd_query(&attr, uattr);
3010 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
3011 err = map_lookup_and_delete_elem(&attr);