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 <linux/audit.h>
27 #include <uapi/linux/btf.h>
29 #define IS_FD_ARRAY(map) ((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_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
33 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
34 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
37 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
39 DEFINE_PER_CPU(int, bpf_prog_active);
40 static DEFINE_IDR(prog_idr);
41 static DEFINE_SPINLOCK(prog_idr_lock);
42 static DEFINE_IDR(map_idr);
43 static DEFINE_SPINLOCK(map_idr_lock);
45 int sysctl_unprivileged_bpf_disabled __read_mostly;
47 static const struct bpf_map_ops * const bpf_map_types[] = {
48 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
49 #define BPF_MAP_TYPE(_id, _ops) \
51 #include <linux/bpf_types.h>
57 * If we're handed a bigger struct than we know of, ensure all the unknown bits
58 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
59 * we don't know about yet.
61 * There is a ToCToU between this function call and the following
62 * copy_from_user() call. However, this is not a concern since this function is
63 * meant to be a future-proofing of bits.
65 int bpf_check_uarg_tail_zero(void __user *uaddr,
69 unsigned char __user *addr;
70 unsigned char __user *end;
74 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
77 if (unlikely(!access_ok(uaddr, actual_size)))
80 if (actual_size <= expected_size)
83 addr = uaddr + expected_size;
84 end = uaddr + actual_size;
86 for (; addr < end; addr++) {
87 err = get_user(val, addr);
97 const struct bpf_map_ops bpf_map_offload_ops = {
98 .map_alloc = bpf_map_offload_map_alloc,
99 .map_free = bpf_map_offload_map_free,
100 .map_check_btf = map_check_no_btf,
103 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
105 const struct bpf_map_ops *ops;
106 u32 type = attr->map_type;
110 if (type >= ARRAY_SIZE(bpf_map_types))
111 return ERR_PTR(-EINVAL);
112 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
113 ops = bpf_map_types[type];
115 return ERR_PTR(-EINVAL);
117 if (ops->map_alloc_check) {
118 err = ops->map_alloc_check(attr);
122 if (attr->map_ifindex)
123 ops = &bpf_map_offload_ops;
124 map = ops->map_alloc(attr);
128 map->map_type = type;
132 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
134 /* We really just want to fail instead of triggering OOM killer
135 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
136 * which is used for lower order allocation requests.
138 * It has been observed that higher order allocation requests done by
139 * vmalloc with __GFP_NORETRY being set might fail due to not trying
140 * to reclaim memory from the page cache, thus we set
141 * __GFP_RETRY_MAYFAIL to avoid such situations.
144 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
147 if (size >= SIZE_MAX)
150 /* kmalloc()'ed memory can't be mmap()'ed */
151 if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
152 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
158 BUG_ON(!PAGE_ALIGNED(size));
159 return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL |
160 __GFP_RETRY_MAYFAIL | flags);
162 return __vmalloc_node_flags_caller(size, numa_node,
163 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
164 flags, __builtin_return_address(0));
167 void *bpf_map_area_alloc(u64 size, int numa_node)
169 return __bpf_map_area_alloc(size, numa_node, false);
172 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
174 return __bpf_map_area_alloc(size, numa_node, true);
177 void bpf_map_area_free(void *area)
182 static u32 bpf_map_flags_retain_permanent(u32 flags)
184 /* Some map creation flags are not tied to the map object but
185 * rather to the map fd instead, so they have no meaning upon
186 * map object inspection since multiple file descriptors with
187 * different (access) properties can exist here. Thus, given
188 * this has zero meaning for the map itself, lets clear these
191 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
194 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
196 map->map_type = attr->map_type;
197 map->key_size = attr->key_size;
198 map->value_size = attr->value_size;
199 map->max_entries = attr->max_entries;
200 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
201 map->numa_node = bpf_map_attr_numa_node(attr);
204 static int bpf_charge_memlock(struct user_struct *user, u32 pages)
206 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
208 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
209 atomic_long_sub(pages, &user->locked_vm);
215 static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
218 atomic_long_sub(pages, &user->locked_vm);
221 int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
223 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
224 struct user_struct *user;
227 if (size >= U32_MAX - PAGE_SIZE)
230 user = get_current_user();
231 ret = bpf_charge_memlock(user, pages);
243 void bpf_map_charge_finish(struct bpf_map_memory *mem)
245 bpf_uncharge_memlock(mem->user, mem->pages);
249 void bpf_map_charge_move(struct bpf_map_memory *dst,
250 struct bpf_map_memory *src)
254 /* Make sure src will not be used for the redundant uncharging. */
255 memset(src, 0, sizeof(struct bpf_map_memory));
258 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
262 ret = bpf_charge_memlock(map->memory.user, pages);
265 map->memory.pages += pages;
269 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
271 bpf_uncharge_memlock(map->memory.user, pages);
272 map->memory.pages -= pages;
275 static int bpf_map_alloc_id(struct bpf_map *map)
279 idr_preload(GFP_KERNEL);
280 spin_lock_bh(&map_idr_lock);
281 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
284 spin_unlock_bh(&map_idr_lock);
287 if (WARN_ON_ONCE(!id))
290 return id > 0 ? 0 : id;
293 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
297 /* Offloaded maps are removed from the IDR store when their device
298 * disappears - even if someone holds an fd to them they are unusable,
299 * the memory is gone, all ops will fail; they are simply waiting for
300 * refcnt to drop to be freed.
306 spin_lock_irqsave(&map_idr_lock, flags);
308 __acquire(&map_idr_lock);
310 idr_remove(&map_idr, map->id);
314 spin_unlock_irqrestore(&map_idr_lock, flags);
316 __release(&map_idr_lock);
319 /* called from workqueue */
320 static void bpf_map_free_deferred(struct work_struct *work)
322 struct bpf_map *map = container_of(work, struct bpf_map, work);
323 struct bpf_map_memory mem;
325 bpf_map_charge_move(&mem, &map->memory);
326 security_bpf_map_free(map);
327 /* implementation dependent freeing */
328 map->ops->map_free(map);
329 bpf_map_charge_finish(&mem);
332 static void bpf_map_put_uref(struct bpf_map *map)
334 if (atomic64_dec_and_test(&map->usercnt)) {
335 if (map->ops->map_release_uref)
336 map->ops->map_release_uref(map);
340 /* decrement map refcnt and schedule it for freeing via workqueue
341 * (unrelying map implementation ops->map_free() might sleep)
343 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
345 if (atomic64_dec_and_test(&map->refcnt)) {
346 /* bpf_map_free_id() must be called first */
347 bpf_map_free_id(map, do_idr_lock);
349 INIT_WORK(&map->work, bpf_map_free_deferred);
350 schedule_work(&map->work);
354 void bpf_map_put(struct bpf_map *map)
356 __bpf_map_put(map, true);
358 EXPORT_SYMBOL_GPL(bpf_map_put);
360 void bpf_map_put_with_uref(struct bpf_map *map)
362 bpf_map_put_uref(map);
366 static int bpf_map_release(struct inode *inode, struct file *filp)
368 struct bpf_map *map = filp->private_data;
370 if (map->ops->map_release)
371 map->ops->map_release(map, filp);
373 bpf_map_put_with_uref(map);
377 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
379 fmode_t mode = f.file->f_mode;
381 /* Our file permissions may have been overridden by global
382 * map permissions facing syscall side.
384 if (READ_ONCE(map->frozen))
385 mode &= ~FMODE_CAN_WRITE;
389 #ifdef CONFIG_PROC_FS
390 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
392 const struct bpf_map *map = filp->private_data;
393 const struct bpf_array *array;
394 u32 type = 0, jited = 0;
396 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
397 array = container_of(map, struct bpf_array, map);
398 type = array->aux->type;
399 jited = array->aux->jited;
416 map->memory.pages * 1ULL << PAGE_SHIFT,
418 READ_ONCE(map->frozen));
420 seq_printf(m, "owner_prog_type:\t%u\n", type);
421 seq_printf(m, "owner_jited:\t%u\n", jited);
426 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
429 /* We need this handler such that alloc_file() enables
430 * f_mode with FMODE_CAN_READ.
435 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
436 size_t siz, loff_t *ppos)
438 /* We need this handler such that alloc_file() enables
439 * f_mode with FMODE_CAN_WRITE.
444 /* called for any extra memory-mapped regions (except initial) */
445 static void bpf_map_mmap_open(struct vm_area_struct *vma)
447 struct bpf_map *map = vma->vm_file->private_data;
449 bpf_map_inc_with_uref(map);
451 if (vma->vm_flags & VM_WRITE) {
452 mutex_lock(&map->freeze_mutex);
454 mutex_unlock(&map->freeze_mutex);
458 /* called for all unmapped memory region (including initial) */
459 static void bpf_map_mmap_close(struct vm_area_struct *vma)
461 struct bpf_map *map = vma->vm_file->private_data;
463 if (vma->vm_flags & VM_WRITE) {
464 mutex_lock(&map->freeze_mutex);
466 mutex_unlock(&map->freeze_mutex);
469 bpf_map_put_with_uref(map);
472 static const struct vm_operations_struct bpf_map_default_vmops = {
473 .open = bpf_map_mmap_open,
474 .close = bpf_map_mmap_close,
477 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
479 struct bpf_map *map = filp->private_data;
482 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
485 if (!(vma->vm_flags & VM_SHARED))
488 mutex_lock(&map->freeze_mutex);
490 if ((vma->vm_flags & VM_WRITE) && map->frozen) {
495 /* set default open/close callbacks */
496 vma->vm_ops = &bpf_map_default_vmops;
497 vma->vm_private_data = map;
499 err = map->ops->map_mmap(map, vma);
503 bpf_map_inc_with_uref(map);
505 if (vma->vm_flags & VM_WRITE)
508 mutex_unlock(&map->freeze_mutex);
512 const struct file_operations bpf_map_fops = {
513 #ifdef CONFIG_PROC_FS
514 .show_fdinfo = bpf_map_show_fdinfo,
516 .release = bpf_map_release,
517 .read = bpf_dummy_read,
518 .write = bpf_dummy_write,
519 .mmap = bpf_map_mmap,
522 int bpf_map_new_fd(struct bpf_map *map, int flags)
526 ret = security_bpf_map(map, OPEN_FMODE(flags));
530 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
534 int bpf_get_file_flag(int flags)
536 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
538 if (flags & BPF_F_RDONLY)
540 if (flags & BPF_F_WRONLY)
545 /* helper macro to check that unused fields 'union bpf_attr' are zero */
546 #define CHECK_ATTR(CMD) \
547 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
548 sizeof(attr->CMD##_LAST_FIELD), 0, \
550 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
551 sizeof(attr->CMD##_LAST_FIELD)) != NULL
553 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
554 * Return 0 on success and < 0 on error.
556 static int bpf_obj_name_cpy(char *dst, const char *src)
558 const char *end = src + BPF_OBJ_NAME_LEN;
560 memset(dst, 0, BPF_OBJ_NAME_LEN);
561 /* Copy all isalnum(), '_' and '.' chars. */
562 while (src < end && *src) {
563 if (!isalnum(*src) &&
564 *src != '_' && *src != '.')
569 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
576 int map_check_no_btf(const struct bpf_map *map,
577 const struct btf *btf,
578 const struct btf_type *key_type,
579 const struct btf_type *value_type)
584 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
585 u32 btf_key_id, u32 btf_value_id)
587 const struct btf_type *key_type, *value_type;
588 u32 key_size, value_size;
591 /* Some maps allow key to be unspecified. */
593 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
594 if (!key_type || key_size != map->key_size)
597 key_type = btf_type_by_id(btf, 0);
598 if (!map->ops->map_check_btf)
602 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
603 if (!value_type || value_size != map->value_size)
606 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
608 if (map_value_has_spin_lock(map)) {
609 if (map->map_flags & BPF_F_RDONLY_PROG)
611 if (map->map_type != BPF_MAP_TYPE_HASH &&
612 map->map_type != BPF_MAP_TYPE_ARRAY &&
613 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
614 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
616 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
619 "verifier bug spin_lock_off %d value_size %d\n",
620 map->spin_lock_off, map->value_size);
625 if (map->ops->map_check_btf)
626 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
631 #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
632 /* called via syscall */
633 static int map_create(union bpf_attr *attr)
635 int numa_node = bpf_map_attr_numa_node(attr);
636 struct bpf_map_memory mem;
641 err = CHECK_ATTR(BPF_MAP_CREATE);
645 f_flags = bpf_get_file_flag(attr->map_flags);
649 if (numa_node != NUMA_NO_NODE &&
650 ((unsigned int)numa_node >= nr_node_ids ||
651 !node_online(numa_node)))
654 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
655 map = find_and_alloc_map(attr);
659 err = bpf_obj_name_cpy(map->name, attr->map_name);
663 atomic64_set(&map->refcnt, 1);
664 atomic64_set(&map->usercnt, 1);
665 mutex_init(&map->freeze_mutex);
667 if (attr->btf_key_type_id || attr->btf_value_type_id) {
670 if (!attr->btf_value_type_id) {
675 btf = btf_get_by_fd(attr->btf_fd);
681 err = map_check_btf(map, btf, attr->btf_key_type_id,
682 attr->btf_value_type_id);
689 map->btf_key_type_id = attr->btf_key_type_id;
690 map->btf_value_type_id = attr->btf_value_type_id;
692 map->spin_lock_off = -EINVAL;
695 err = security_bpf_map_alloc(map);
699 err = bpf_map_alloc_id(map);
703 err = bpf_map_new_fd(map, f_flags);
705 /* failed to allocate fd.
706 * bpf_map_put_with_uref() is needed because the above
707 * bpf_map_alloc_id() has published the map
708 * to the userspace and the userspace may
709 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
711 bpf_map_put_with_uref(map);
718 security_bpf_map_free(map);
721 bpf_map_charge_move(&mem, &map->memory);
722 map->ops->map_free(map);
723 bpf_map_charge_finish(&mem);
727 /* if error is returned, fd is released.
728 * On success caller should complete fd access with matching fdput()
730 struct bpf_map *__bpf_map_get(struct fd f)
733 return ERR_PTR(-EBADF);
734 if (f.file->f_op != &bpf_map_fops) {
736 return ERR_PTR(-EINVAL);
739 return f.file->private_data;
742 void bpf_map_inc(struct bpf_map *map)
744 atomic64_inc(&map->refcnt);
746 EXPORT_SYMBOL_GPL(bpf_map_inc);
748 void bpf_map_inc_with_uref(struct bpf_map *map)
750 atomic64_inc(&map->refcnt);
751 atomic64_inc(&map->usercnt);
753 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
755 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
757 struct fd f = fdget(ufd);
760 map = __bpf_map_get(f);
764 bpf_map_inc_with_uref(map);
770 /* map_idr_lock should have been held */
771 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
775 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
777 return ERR_PTR(-ENOENT);
779 atomic64_inc(&map->usercnt);
784 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
786 spin_lock_bh(&map_idr_lock);
787 map = __bpf_map_inc_not_zero(map, false);
788 spin_unlock_bh(&map_idr_lock);
792 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
794 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
799 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
802 return memdup_user(ukey, key_size);
805 return ERR_PTR(-EINVAL);
810 /* last field in 'union bpf_attr' used by this command */
811 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
813 static int map_lookup_elem(union bpf_attr *attr)
815 void __user *ukey = u64_to_user_ptr(attr->key);
816 void __user *uvalue = u64_to_user_ptr(attr->value);
817 int ufd = attr->map_fd;
819 void *key, *value, *ptr;
824 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
827 if (attr->flags & ~BPF_F_LOCK)
831 map = __bpf_map_get(f);
834 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
839 if ((attr->flags & BPF_F_LOCK) &&
840 !map_value_has_spin_lock(map)) {
845 key = __bpf_copy_key(ukey, map->key_size);
851 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
852 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
853 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
854 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
855 value_size = round_up(map->value_size, 8) * num_possible_cpus();
856 else if (IS_FD_MAP(map))
857 value_size = sizeof(u32);
859 value_size = map->value_size;
862 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
866 if (bpf_map_is_dev_bound(map)) {
867 err = bpf_map_offload_lookup_elem(map, key, value);
872 this_cpu_inc(bpf_prog_active);
873 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
874 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
875 err = bpf_percpu_hash_copy(map, key, value);
876 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
877 err = bpf_percpu_array_copy(map, key, value);
878 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
879 err = bpf_percpu_cgroup_storage_copy(map, key, value);
880 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
881 err = bpf_stackmap_copy(map, key, value);
882 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
883 err = bpf_fd_array_map_lookup_elem(map, key, value);
884 } else if (IS_FD_HASH(map)) {
885 err = bpf_fd_htab_map_lookup_elem(map, key, value);
886 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
887 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
888 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
889 map->map_type == BPF_MAP_TYPE_STACK) {
890 err = map->ops->map_peek_elem(map, value);
893 if (map->ops->map_lookup_elem_sys_only)
894 ptr = map->ops->map_lookup_elem_sys_only(map, key);
896 ptr = map->ops->map_lookup_elem(map, key);
903 if (attr->flags & BPF_F_LOCK)
904 /* lock 'ptr' and copy everything but lock */
905 copy_map_value_locked(map, value, ptr, true);
907 copy_map_value(map, value, ptr);
908 /* mask lock, since value wasn't zero inited */
909 check_and_init_map_lock(map, value);
913 this_cpu_dec(bpf_prog_active);
921 if (copy_to_user(uvalue, value, value_size) != 0)
935 static void maybe_wait_bpf_programs(struct bpf_map *map)
937 /* Wait for any running BPF programs to complete so that
938 * userspace, when we return to it, knows that all programs
939 * that could be running use the new map value.
941 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
942 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
946 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
948 static int map_update_elem(union bpf_attr *attr)
950 void __user *ukey = u64_to_user_ptr(attr->key);
951 void __user *uvalue = u64_to_user_ptr(attr->value);
952 int ufd = attr->map_fd;
959 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
963 map = __bpf_map_get(f);
966 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
971 if ((attr->flags & BPF_F_LOCK) &&
972 !map_value_has_spin_lock(map)) {
977 key = __bpf_copy_key(ukey, map->key_size);
983 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
984 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
985 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
986 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
987 value_size = round_up(map->value_size, 8) * num_possible_cpus();
989 value_size = map->value_size;
992 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
997 if (copy_from_user(value, uvalue, value_size) != 0)
1000 /* Need to create a kthread, thus must support schedule */
1001 if (bpf_map_is_dev_bound(map)) {
1002 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
1004 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
1005 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
1006 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
1007 err = map->ops->map_update_elem(map, key, value, attr->flags);
1009 } else if (IS_FD_PROG_ARRAY(map)) {
1010 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
1015 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
1016 * inside bpf map update or delete otherwise deadlocks are possible
1019 __this_cpu_inc(bpf_prog_active);
1020 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1021 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1022 err = bpf_percpu_hash_update(map, key, value, attr->flags);
1023 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
1024 err = bpf_percpu_array_update(map, key, value, attr->flags);
1025 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
1026 err = bpf_percpu_cgroup_storage_update(map, key, value,
1028 } else if (IS_FD_ARRAY(map)) {
1030 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
1033 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
1035 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
1038 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
1039 /* rcu_read_lock() is not needed */
1040 err = bpf_fd_reuseport_array_update_elem(map, key, value,
1042 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1043 map->map_type == BPF_MAP_TYPE_STACK) {
1044 err = map->ops->map_push_elem(map, value, attr->flags);
1047 err = map->ops->map_update_elem(map, key, value, attr->flags);
1050 __this_cpu_dec(bpf_prog_active);
1052 maybe_wait_bpf_programs(map);
1063 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1065 static int map_delete_elem(union bpf_attr *attr)
1067 void __user *ukey = u64_to_user_ptr(attr->key);
1068 int ufd = attr->map_fd;
1069 struct bpf_map *map;
1074 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1078 map = __bpf_map_get(f);
1080 return PTR_ERR(map);
1081 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1086 key = __bpf_copy_key(ukey, map->key_size);
1092 if (bpf_map_is_dev_bound(map)) {
1093 err = bpf_map_offload_delete_elem(map, key);
1095 } else if (IS_FD_PROG_ARRAY(map)) {
1096 err = map->ops->map_delete_elem(map, key);
1101 __this_cpu_inc(bpf_prog_active);
1103 err = map->ops->map_delete_elem(map, key);
1105 __this_cpu_dec(bpf_prog_active);
1107 maybe_wait_bpf_programs(map);
1115 /* last field in 'union bpf_attr' used by this command */
1116 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1118 static int map_get_next_key(union bpf_attr *attr)
1120 void __user *ukey = u64_to_user_ptr(attr->key);
1121 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1122 int ufd = attr->map_fd;
1123 struct bpf_map *map;
1124 void *key, *next_key;
1128 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1132 map = __bpf_map_get(f);
1134 return PTR_ERR(map);
1135 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1141 key = __bpf_copy_key(ukey, map->key_size);
1151 next_key = kmalloc(map->key_size, GFP_USER);
1155 if (bpf_map_is_dev_bound(map)) {
1156 err = bpf_map_offload_get_next_key(map, key, next_key);
1161 err = map->ops->map_get_next_key(map, key, next_key);
1168 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1182 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1184 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1186 void __user *ukey = u64_to_user_ptr(attr->key);
1187 void __user *uvalue = u64_to_user_ptr(attr->value);
1188 int ufd = attr->map_fd;
1189 struct bpf_map *map;
1195 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1199 map = __bpf_map_get(f);
1201 return PTR_ERR(map);
1202 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1207 key = __bpf_copy_key(ukey, map->key_size);
1213 value_size = map->value_size;
1216 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1220 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1221 map->map_type == BPF_MAP_TYPE_STACK) {
1222 err = map->ops->map_pop_elem(map, value);
1230 if (copy_to_user(uvalue, value, value_size) != 0)
1244 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1246 static int map_freeze(const union bpf_attr *attr)
1248 int err = 0, ufd = attr->map_fd;
1249 struct bpf_map *map;
1252 if (CHECK_ATTR(BPF_MAP_FREEZE))
1256 map = __bpf_map_get(f);
1258 return PTR_ERR(map);
1260 mutex_lock(&map->freeze_mutex);
1262 if (map->writecnt) {
1266 if (READ_ONCE(map->frozen)) {
1270 if (!capable(CAP_SYS_ADMIN)) {
1275 WRITE_ONCE(map->frozen, true);
1277 mutex_unlock(&map->freeze_mutex);
1282 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1283 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1284 [_id] = & _name ## _prog_ops,
1285 #define BPF_MAP_TYPE(_id, _ops)
1286 #include <linux/bpf_types.h>
1287 #undef BPF_PROG_TYPE
1291 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1293 const struct bpf_prog_ops *ops;
1295 if (type >= ARRAY_SIZE(bpf_prog_types))
1297 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1298 ops = bpf_prog_types[type];
1302 if (!bpf_prog_is_dev_bound(prog->aux))
1303 prog->aux->ops = ops;
1305 prog->aux->ops = &bpf_offload_prog_ops;
1316 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1317 [BPF_AUDIT_LOAD] = "LOAD",
1318 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1321 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1323 struct audit_context *ctx = NULL;
1324 struct audit_buffer *ab;
1326 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1328 if (audit_enabled == AUDIT_OFF)
1330 if (op == BPF_AUDIT_LOAD)
1331 ctx = audit_context();
1332 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1335 audit_log_format(ab, "prog-id=%u op=%s",
1336 prog->aux->id, bpf_audit_str[op]);
1340 int __bpf_prog_charge(struct user_struct *user, u32 pages)
1342 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1343 unsigned long user_bufs;
1346 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1347 if (user_bufs > memlock_limit) {
1348 atomic_long_sub(pages, &user->locked_vm);
1356 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1359 atomic_long_sub(pages, &user->locked_vm);
1362 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1364 struct user_struct *user = get_current_user();
1367 ret = __bpf_prog_charge(user, prog->pages);
1373 prog->aux->user = user;
1377 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1379 struct user_struct *user = prog->aux->user;
1381 __bpf_prog_uncharge(user, prog->pages);
1385 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1389 idr_preload(GFP_KERNEL);
1390 spin_lock_bh(&prog_idr_lock);
1391 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1394 spin_unlock_bh(&prog_idr_lock);
1397 /* id is in [1, INT_MAX) */
1398 if (WARN_ON_ONCE(!id))
1401 return id > 0 ? 0 : id;
1404 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1406 /* cBPF to eBPF migrations are currently not in the idr store.
1407 * Offloaded programs are removed from the store when their device
1408 * disappears - even if someone grabs an fd to them they are unusable,
1409 * simply waiting for refcnt to drop to be freed.
1415 spin_lock_bh(&prog_idr_lock);
1417 __acquire(&prog_idr_lock);
1419 idr_remove(&prog_idr, prog->aux->id);
1423 spin_unlock_bh(&prog_idr_lock);
1425 __release(&prog_idr_lock);
1428 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1430 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1432 kvfree(aux->func_info);
1433 kfree(aux->func_info_aux);
1434 bpf_prog_uncharge_memlock(aux->prog);
1435 security_bpf_prog_free(aux);
1436 bpf_prog_free(aux->prog);
1439 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1441 bpf_prog_kallsyms_del_all(prog);
1442 btf_put(prog->aux->btf);
1443 bpf_prog_free_linfo(prog);
1446 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1448 __bpf_prog_put_rcu(&prog->aux->rcu);
1451 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1453 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
1454 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1455 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1456 /* bpf_prog_free_id() must be called first */
1457 bpf_prog_free_id(prog, do_idr_lock);
1458 __bpf_prog_put_noref(prog, true);
1462 void bpf_prog_put(struct bpf_prog *prog)
1464 __bpf_prog_put(prog, true);
1466 EXPORT_SYMBOL_GPL(bpf_prog_put);
1468 static int bpf_prog_release(struct inode *inode, struct file *filp)
1470 struct bpf_prog *prog = filp->private_data;
1476 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1477 struct bpf_prog_stats *stats)
1479 u64 nsecs = 0, cnt = 0;
1482 for_each_possible_cpu(cpu) {
1483 const struct bpf_prog_stats *st;
1487 st = per_cpu_ptr(prog->aux->stats, cpu);
1489 start = u64_stats_fetch_begin_irq(&st->syncp);
1492 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1496 stats->nsecs = nsecs;
1500 #ifdef CONFIG_PROC_FS
1501 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1503 const struct bpf_prog *prog = filp->private_data;
1504 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1505 struct bpf_prog_stats stats;
1507 bpf_prog_get_stats(prog, &stats);
1508 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1515 "run_time_ns:\t%llu\n"
1520 prog->pages * 1ULL << PAGE_SHIFT,
1527 const struct file_operations bpf_prog_fops = {
1528 #ifdef CONFIG_PROC_FS
1529 .show_fdinfo = bpf_prog_show_fdinfo,
1531 .release = bpf_prog_release,
1532 .read = bpf_dummy_read,
1533 .write = bpf_dummy_write,
1536 int bpf_prog_new_fd(struct bpf_prog *prog)
1540 ret = security_bpf_prog(prog);
1544 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1545 O_RDWR | O_CLOEXEC);
1548 static struct bpf_prog *____bpf_prog_get(struct fd f)
1551 return ERR_PTR(-EBADF);
1552 if (f.file->f_op != &bpf_prog_fops) {
1554 return ERR_PTR(-EINVAL);
1557 return f.file->private_data;
1560 void bpf_prog_add(struct bpf_prog *prog, int i)
1562 atomic64_add(i, &prog->aux->refcnt);
1564 EXPORT_SYMBOL_GPL(bpf_prog_add);
1566 void bpf_prog_sub(struct bpf_prog *prog, int i)
1568 /* Only to be used for undoing previous bpf_prog_add() in some
1569 * error path. We still know that another entity in our call
1570 * path holds a reference to the program, thus atomic_sub() can
1571 * be safely used in such cases!
1573 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1575 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1577 void bpf_prog_inc(struct bpf_prog *prog)
1579 atomic64_inc(&prog->aux->refcnt);
1581 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1583 /* prog_idr_lock should have been held */
1584 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1588 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1591 return ERR_PTR(-ENOENT);
1595 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1597 bool bpf_prog_get_ok(struct bpf_prog *prog,
1598 enum bpf_prog_type *attach_type, bool attach_drv)
1600 /* not an attachment, just a refcount inc, always allow */
1604 if (prog->type != *attach_type)
1606 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1612 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1615 struct fd f = fdget(ufd);
1616 struct bpf_prog *prog;
1618 prog = ____bpf_prog_get(f);
1621 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1622 prog = ERR_PTR(-EINVAL);
1632 struct bpf_prog *bpf_prog_get(u32 ufd)
1634 return __bpf_prog_get(ufd, NULL, false);
1637 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1640 return __bpf_prog_get(ufd, &type, attach_drv);
1642 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1644 /* Initially all BPF programs could be loaded w/o specifying
1645 * expected_attach_type. Later for some of them specifying expected_attach_type
1646 * at load time became required so that program could be validated properly.
1647 * Programs of types that are allowed to be loaded both w/ and w/o (for
1648 * backward compatibility) expected_attach_type, should have the default attach
1649 * type assigned to expected_attach_type for the latter case, so that it can be
1650 * validated later at attach time.
1652 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1653 * prog type requires it but has some attach types that have to be backward
1656 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1658 switch (attr->prog_type) {
1659 case BPF_PROG_TYPE_CGROUP_SOCK:
1660 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1661 * exist so checking for non-zero is the way to go here.
1663 if (!attr->expected_attach_type)
1664 attr->expected_attach_type =
1665 BPF_CGROUP_INET_SOCK_CREATE;
1671 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1672 enum bpf_attach_type expected_attach_type,
1673 u32 btf_id, u32 prog_fd)
1675 switch (prog_type) {
1676 case BPF_PROG_TYPE_TRACING:
1677 if (btf_id > BTF_MAX_TYPE)
1681 if (btf_id || prog_fd)
1686 switch (prog_type) {
1687 case BPF_PROG_TYPE_CGROUP_SOCK:
1688 switch (expected_attach_type) {
1689 case BPF_CGROUP_INET_SOCK_CREATE:
1690 case BPF_CGROUP_INET4_POST_BIND:
1691 case BPF_CGROUP_INET6_POST_BIND:
1696 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1697 switch (expected_attach_type) {
1698 case BPF_CGROUP_INET4_BIND:
1699 case BPF_CGROUP_INET6_BIND:
1700 case BPF_CGROUP_INET4_CONNECT:
1701 case BPF_CGROUP_INET6_CONNECT:
1702 case BPF_CGROUP_UDP4_SENDMSG:
1703 case BPF_CGROUP_UDP6_SENDMSG:
1704 case BPF_CGROUP_UDP4_RECVMSG:
1705 case BPF_CGROUP_UDP6_RECVMSG:
1710 case BPF_PROG_TYPE_CGROUP_SKB:
1711 switch (expected_attach_type) {
1712 case BPF_CGROUP_INET_INGRESS:
1713 case BPF_CGROUP_INET_EGRESS:
1718 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1719 switch (expected_attach_type) {
1720 case BPF_CGROUP_SETSOCKOPT:
1721 case BPF_CGROUP_GETSOCKOPT:
1731 /* last field in 'union bpf_attr' used by this command */
1732 #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
1734 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
1736 enum bpf_prog_type type = attr->prog_type;
1737 struct bpf_prog *prog;
1742 if (CHECK_ATTR(BPF_PROG_LOAD))
1745 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
1746 BPF_F_ANY_ALIGNMENT |
1747 BPF_F_TEST_STATE_FREQ |
1748 BPF_F_TEST_RND_HI32))
1751 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1752 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1753 !capable(CAP_SYS_ADMIN))
1756 /* copy eBPF program license from user space */
1757 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1758 sizeof(license) - 1) < 0)
1760 license[sizeof(license) - 1] = 0;
1762 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1763 is_gpl = license_is_gpl_compatible(license);
1765 if (attr->insn_cnt == 0 ||
1766 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
1768 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1769 type != BPF_PROG_TYPE_CGROUP_SKB &&
1770 !capable(CAP_SYS_ADMIN))
1773 bpf_prog_load_fixup_attach_type(attr);
1774 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
1775 attr->attach_btf_id,
1776 attr->attach_prog_fd))
1779 /* plain bpf_prog allocation */
1780 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1784 prog->expected_attach_type = attr->expected_attach_type;
1785 prog->aux->attach_btf_id = attr->attach_btf_id;
1786 if (attr->attach_prog_fd) {
1787 struct bpf_prog *tgt_prog;
1789 tgt_prog = bpf_prog_get(attr->attach_prog_fd);
1790 if (IS_ERR(tgt_prog)) {
1791 err = PTR_ERR(tgt_prog);
1792 goto free_prog_nouncharge;
1794 prog->aux->linked_prog = tgt_prog;
1797 prog->aux->offload_requested = !!attr->prog_ifindex;
1799 err = security_bpf_prog_alloc(prog->aux);
1801 goto free_prog_nouncharge;
1803 err = bpf_prog_charge_memlock(prog);
1807 prog->len = attr->insn_cnt;
1810 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1811 bpf_prog_insn_size(prog)) != 0)
1814 prog->orig_prog = NULL;
1817 atomic64_set(&prog->aux->refcnt, 1);
1818 prog->gpl_compatible = is_gpl ? 1 : 0;
1820 if (bpf_prog_is_dev_bound(prog->aux)) {
1821 err = bpf_prog_offload_init(prog, attr);
1826 /* find program type: socket_filter vs tracing_filter */
1827 err = find_prog_type(type, prog);
1831 prog->aux->load_time = ktime_get_boottime_ns();
1832 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1836 /* run eBPF verifier */
1837 err = bpf_check(&prog, attr, uattr);
1839 goto free_used_maps;
1841 prog = bpf_prog_select_runtime(prog, &err);
1843 goto free_used_maps;
1845 err = bpf_prog_alloc_id(prog);
1847 goto free_used_maps;
1849 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
1850 * effectively publicly exposed. However, retrieving via
1851 * bpf_prog_get_fd_by_id() will take another reference,
1852 * therefore it cannot be gone underneath us.
1854 * Only for the time /after/ successful bpf_prog_new_fd()
1855 * and before returning to userspace, we might just hold
1856 * one reference and any parallel close on that fd could
1857 * rip everything out. Hence, below notifications must
1858 * happen before bpf_prog_new_fd().
1860 * Also, any failure handling from this point onwards must
1861 * be using bpf_prog_put() given the program is exposed.
1863 bpf_prog_kallsyms_add(prog);
1864 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
1865 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
1867 err = bpf_prog_new_fd(prog);
1873 /* In case we have subprogs, we need to wait for a grace
1874 * period before we can tear down JIT memory since symbols
1875 * are already exposed under kallsyms.
1877 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
1880 bpf_prog_uncharge_memlock(prog);
1882 security_bpf_prog_free(prog->aux);
1883 free_prog_nouncharge:
1884 bpf_prog_free(prog);
1888 #define BPF_OBJ_LAST_FIELD file_flags
1890 static int bpf_obj_pin(const union bpf_attr *attr)
1892 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1895 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1898 static int bpf_obj_get(const union bpf_attr *attr)
1900 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1901 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1904 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1908 static int bpf_tracing_prog_release(struct inode *inode, struct file *filp)
1910 struct bpf_prog *prog = filp->private_data;
1912 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
1917 static const struct file_operations bpf_tracing_prog_fops = {
1918 .release = bpf_tracing_prog_release,
1919 .read = bpf_dummy_read,
1920 .write = bpf_dummy_write,
1923 static int bpf_tracing_prog_attach(struct bpf_prog *prog)
1927 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
1928 prog->expected_attach_type != BPF_TRACE_FEXIT) {
1933 err = bpf_trampoline_link_prog(prog);
1937 tr_fd = anon_inode_getfd("bpf-tracing-prog", &bpf_tracing_prog_fops,
1940 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
1951 struct bpf_raw_tracepoint {
1952 struct bpf_raw_event_map *btp;
1953 struct bpf_prog *prog;
1956 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1958 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1961 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1962 bpf_prog_put(raw_tp->prog);
1964 bpf_put_raw_tracepoint(raw_tp->btp);
1969 static const struct file_operations bpf_raw_tp_fops = {
1970 .release = bpf_raw_tracepoint_release,
1971 .read = bpf_dummy_read,
1972 .write = bpf_dummy_write,
1975 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1977 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1979 struct bpf_raw_tracepoint *raw_tp;
1980 struct bpf_raw_event_map *btp;
1981 struct bpf_prog *prog;
1982 const char *tp_name;
1986 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
1989 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
1991 return PTR_ERR(prog);
1993 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1994 prog->type != BPF_PROG_TYPE_TRACING &&
1995 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
2000 if (prog->type == BPF_PROG_TYPE_TRACING) {
2001 if (attr->raw_tracepoint.name) {
2002 /* The attach point for this category of programs
2003 * should be specified via btf_id during program load.
2008 if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
2009 tp_name = prog->aux->attach_func_name;
2011 return bpf_tracing_prog_attach(prog);
2013 if (strncpy_from_user(buf,
2014 u64_to_user_ptr(attr->raw_tracepoint.name),
2015 sizeof(buf) - 1) < 0) {
2019 buf[sizeof(buf) - 1] = 0;
2023 btp = bpf_get_raw_tracepoint(tp_name);
2029 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
2035 raw_tp->prog = prog;
2037 err = bpf_probe_register(raw_tp->btp, prog);
2041 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
2044 bpf_probe_unregister(raw_tp->btp, prog);
2053 bpf_put_raw_tracepoint(btp);
2059 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2060 enum bpf_attach_type attach_type)
2062 switch (prog->type) {
2063 case BPF_PROG_TYPE_CGROUP_SOCK:
2064 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2065 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2066 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
2067 case BPF_PROG_TYPE_CGROUP_SKB:
2068 return prog->enforce_expected_attach_type &&
2069 prog->expected_attach_type != attach_type ?
2076 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
2078 #define BPF_F_ATTACH_MASK \
2079 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
2081 static int bpf_prog_attach(const union bpf_attr *attr)
2083 enum bpf_prog_type ptype;
2084 struct bpf_prog *prog;
2087 if (!capable(CAP_NET_ADMIN))
2090 if (CHECK_ATTR(BPF_PROG_ATTACH))
2093 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
2096 switch (attr->attach_type) {
2097 case BPF_CGROUP_INET_INGRESS:
2098 case BPF_CGROUP_INET_EGRESS:
2099 ptype = BPF_PROG_TYPE_CGROUP_SKB;
2101 case BPF_CGROUP_INET_SOCK_CREATE:
2102 case BPF_CGROUP_INET4_POST_BIND:
2103 case BPF_CGROUP_INET6_POST_BIND:
2104 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2106 case BPF_CGROUP_INET4_BIND:
2107 case BPF_CGROUP_INET6_BIND:
2108 case BPF_CGROUP_INET4_CONNECT:
2109 case BPF_CGROUP_INET6_CONNECT:
2110 case BPF_CGROUP_UDP4_SENDMSG:
2111 case BPF_CGROUP_UDP6_SENDMSG:
2112 case BPF_CGROUP_UDP4_RECVMSG:
2113 case BPF_CGROUP_UDP6_RECVMSG:
2114 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2116 case BPF_CGROUP_SOCK_OPS:
2117 ptype = BPF_PROG_TYPE_SOCK_OPS;
2119 case BPF_CGROUP_DEVICE:
2120 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2122 case BPF_SK_MSG_VERDICT:
2123 ptype = BPF_PROG_TYPE_SK_MSG;
2125 case BPF_SK_SKB_STREAM_PARSER:
2126 case BPF_SK_SKB_STREAM_VERDICT:
2127 ptype = BPF_PROG_TYPE_SK_SKB;
2129 case BPF_LIRC_MODE2:
2130 ptype = BPF_PROG_TYPE_LIRC_MODE2;
2132 case BPF_FLOW_DISSECTOR:
2133 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
2135 case BPF_CGROUP_SYSCTL:
2136 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2138 case BPF_CGROUP_GETSOCKOPT:
2139 case BPF_CGROUP_SETSOCKOPT:
2140 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2146 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2148 return PTR_ERR(prog);
2150 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
2156 case BPF_PROG_TYPE_SK_SKB:
2157 case BPF_PROG_TYPE_SK_MSG:
2158 ret = sock_map_get_from_fd(attr, prog);
2160 case BPF_PROG_TYPE_LIRC_MODE2:
2161 ret = lirc_prog_attach(attr, prog);
2163 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2164 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
2167 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
2175 #define BPF_PROG_DETACH_LAST_FIELD attach_type
2177 static int bpf_prog_detach(const union bpf_attr *attr)
2179 enum bpf_prog_type ptype;
2181 if (!capable(CAP_NET_ADMIN))
2184 if (CHECK_ATTR(BPF_PROG_DETACH))
2187 switch (attr->attach_type) {
2188 case BPF_CGROUP_INET_INGRESS:
2189 case BPF_CGROUP_INET_EGRESS:
2190 ptype = BPF_PROG_TYPE_CGROUP_SKB;
2192 case BPF_CGROUP_INET_SOCK_CREATE:
2193 case BPF_CGROUP_INET4_POST_BIND:
2194 case BPF_CGROUP_INET6_POST_BIND:
2195 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2197 case BPF_CGROUP_INET4_BIND:
2198 case BPF_CGROUP_INET6_BIND:
2199 case BPF_CGROUP_INET4_CONNECT:
2200 case BPF_CGROUP_INET6_CONNECT:
2201 case BPF_CGROUP_UDP4_SENDMSG:
2202 case BPF_CGROUP_UDP6_SENDMSG:
2203 case BPF_CGROUP_UDP4_RECVMSG:
2204 case BPF_CGROUP_UDP6_RECVMSG:
2205 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2207 case BPF_CGROUP_SOCK_OPS:
2208 ptype = BPF_PROG_TYPE_SOCK_OPS;
2210 case BPF_CGROUP_DEVICE:
2211 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2213 case BPF_SK_MSG_VERDICT:
2214 return sock_map_get_from_fd(attr, NULL);
2215 case BPF_SK_SKB_STREAM_PARSER:
2216 case BPF_SK_SKB_STREAM_VERDICT:
2217 return sock_map_get_from_fd(attr, NULL);
2218 case BPF_LIRC_MODE2:
2219 return lirc_prog_detach(attr);
2220 case BPF_FLOW_DISSECTOR:
2221 return skb_flow_dissector_bpf_prog_detach(attr);
2222 case BPF_CGROUP_SYSCTL:
2223 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2225 case BPF_CGROUP_GETSOCKOPT:
2226 case BPF_CGROUP_SETSOCKOPT:
2227 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2233 return cgroup_bpf_prog_detach(attr, ptype);
2236 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2238 static int bpf_prog_query(const union bpf_attr *attr,
2239 union bpf_attr __user *uattr)
2241 if (!capable(CAP_NET_ADMIN))
2243 if (CHECK_ATTR(BPF_PROG_QUERY))
2245 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2248 switch (attr->query.attach_type) {
2249 case BPF_CGROUP_INET_INGRESS:
2250 case BPF_CGROUP_INET_EGRESS:
2251 case BPF_CGROUP_INET_SOCK_CREATE:
2252 case BPF_CGROUP_INET4_BIND:
2253 case BPF_CGROUP_INET6_BIND:
2254 case BPF_CGROUP_INET4_POST_BIND:
2255 case BPF_CGROUP_INET6_POST_BIND:
2256 case BPF_CGROUP_INET4_CONNECT:
2257 case BPF_CGROUP_INET6_CONNECT:
2258 case BPF_CGROUP_UDP4_SENDMSG:
2259 case BPF_CGROUP_UDP6_SENDMSG:
2260 case BPF_CGROUP_UDP4_RECVMSG:
2261 case BPF_CGROUP_UDP6_RECVMSG:
2262 case BPF_CGROUP_SOCK_OPS:
2263 case BPF_CGROUP_DEVICE:
2264 case BPF_CGROUP_SYSCTL:
2265 case BPF_CGROUP_GETSOCKOPT:
2266 case BPF_CGROUP_SETSOCKOPT:
2268 case BPF_LIRC_MODE2:
2269 return lirc_prog_query(attr, uattr);
2270 case BPF_FLOW_DISSECTOR:
2271 return skb_flow_dissector_prog_query(attr, uattr);
2276 return cgroup_bpf_prog_query(attr, uattr);
2279 #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
2281 static int bpf_prog_test_run(const union bpf_attr *attr,
2282 union bpf_attr __user *uattr)
2284 struct bpf_prog *prog;
2285 int ret = -ENOTSUPP;
2287 if (!capable(CAP_SYS_ADMIN))
2289 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2292 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2293 (!attr->test.ctx_size_in && attr->test.ctx_in))
2296 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2297 (!attr->test.ctx_size_out && attr->test.ctx_out))
2300 prog = bpf_prog_get(attr->test.prog_fd);
2302 return PTR_ERR(prog);
2304 if (prog->aux->ops->test_run)
2305 ret = prog->aux->ops->test_run(prog, attr, uattr);
2311 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2313 static int bpf_obj_get_next_id(const union bpf_attr *attr,
2314 union bpf_attr __user *uattr,
2318 u32 next_id = attr->start_id;
2321 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2324 if (!capable(CAP_SYS_ADMIN))
2329 if (!idr_get_next(idr, &next_id))
2331 spin_unlock_bh(lock);
2334 err = put_user(next_id, &uattr->next_id);
2339 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2341 struct bpf_prog *bpf_prog_by_id(u32 id)
2343 struct bpf_prog *prog;
2346 return ERR_PTR(-ENOENT);
2348 spin_lock_bh(&prog_idr_lock);
2349 prog = idr_find(&prog_idr, id);
2351 prog = bpf_prog_inc_not_zero(prog);
2353 prog = ERR_PTR(-ENOENT);
2354 spin_unlock_bh(&prog_idr_lock);
2358 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2360 struct bpf_prog *prog;
2361 u32 id = attr->prog_id;
2364 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2367 if (!capable(CAP_SYS_ADMIN))
2370 prog = bpf_prog_by_id(id);
2372 return PTR_ERR(prog);
2374 fd = bpf_prog_new_fd(prog);
2381 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2383 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2385 struct bpf_map *map;
2386 u32 id = attr->map_id;
2390 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2391 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
2394 if (!capable(CAP_SYS_ADMIN))
2397 f_flags = bpf_get_file_flag(attr->open_flags);
2401 spin_lock_bh(&map_idr_lock);
2402 map = idr_find(&map_idr, id);
2404 map = __bpf_map_inc_not_zero(map, true);
2406 map = ERR_PTR(-ENOENT);
2407 spin_unlock_bh(&map_idr_lock);
2410 return PTR_ERR(map);
2412 fd = bpf_map_new_fd(map, f_flags);
2414 bpf_map_put_with_uref(map);
2419 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
2420 unsigned long addr, u32 *off,
2423 const struct bpf_map *map;
2426 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2427 map = prog->aux->used_maps[i];
2428 if (map == (void *)addr) {
2429 *type = BPF_PSEUDO_MAP_FD;
2432 if (!map->ops->map_direct_value_meta)
2434 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2435 *type = BPF_PSEUDO_MAP_VALUE;
2443 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2445 const struct bpf_map *map;
2446 struct bpf_insn *insns;
2451 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2456 for (i = 0; i < prog->len; i++) {
2457 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2458 insns[i].code = BPF_JMP | BPF_CALL;
2459 insns[i].imm = BPF_FUNC_tail_call;
2462 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2463 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2464 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2465 insns[i].code = BPF_JMP | BPF_CALL;
2466 if (!bpf_dump_raw_ok())
2471 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2474 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
2475 map = bpf_map_from_imm(prog, imm, &off, &type);
2477 insns[i].src_reg = type;
2478 insns[i].imm = map->id;
2479 insns[i + 1].imm = off;
2487 static int set_info_rec_size(struct bpf_prog_info *info)
2490 * Ensure info.*_rec_size is the same as kernel expected size
2494 * Only allow zero *_rec_size if both _rec_size and _cnt are
2495 * zero. In this case, the kernel will set the expected
2496 * _rec_size back to the info.
2499 if ((info->nr_func_info || info->func_info_rec_size) &&
2500 info->func_info_rec_size != sizeof(struct bpf_func_info))
2503 if ((info->nr_line_info || info->line_info_rec_size) &&
2504 info->line_info_rec_size != sizeof(struct bpf_line_info))
2507 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
2508 info->jited_line_info_rec_size != sizeof(__u64))
2511 info->func_info_rec_size = sizeof(struct bpf_func_info);
2512 info->line_info_rec_size = sizeof(struct bpf_line_info);
2513 info->jited_line_info_rec_size = sizeof(__u64);
2518 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2519 const union bpf_attr *attr,
2520 union bpf_attr __user *uattr)
2522 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2523 struct bpf_prog_info info = {};
2524 u32 info_len = attr->info.info_len;
2525 struct bpf_prog_stats stats;
2526 char __user *uinsns;
2530 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2533 info_len = min_t(u32, sizeof(info), info_len);
2535 if (copy_from_user(&info, uinfo, info_len))
2538 info.type = prog->type;
2539 info.id = prog->aux->id;
2540 info.load_time = prog->aux->load_time;
2541 info.created_by_uid = from_kuid_munged(current_user_ns(),
2542 prog->aux->user->uid);
2543 info.gpl_compatible = prog->gpl_compatible;
2545 memcpy(info.tag, prog->tag, sizeof(prog->tag));
2546 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2548 ulen = info.nr_map_ids;
2549 info.nr_map_ids = prog->aux->used_map_cnt;
2550 ulen = min_t(u32, info.nr_map_ids, ulen);
2552 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
2555 for (i = 0; i < ulen; i++)
2556 if (put_user(prog->aux->used_maps[i]->id,
2561 err = set_info_rec_size(&info);
2565 bpf_prog_get_stats(prog, &stats);
2566 info.run_time_ns = stats.nsecs;
2567 info.run_cnt = stats.cnt;
2569 if (!capable(CAP_SYS_ADMIN)) {
2570 info.jited_prog_len = 0;
2571 info.xlated_prog_len = 0;
2572 info.nr_jited_ksyms = 0;
2573 info.nr_jited_func_lens = 0;
2574 info.nr_func_info = 0;
2575 info.nr_line_info = 0;
2576 info.nr_jited_line_info = 0;
2580 ulen = info.xlated_prog_len;
2581 info.xlated_prog_len = bpf_prog_insn_size(prog);
2582 if (info.xlated_prog_len && ulen) {
2583 struct bpf_insn *insns_sanitized;
2586 if (prog->blinded && !bpf_dump_raw_ok()) {
2587 info.xlated_prog_insns = 0;
2590 insns_sanitized = bpf_insn_prepare_dump(prog);
2591 if (!insns_sanitized)
2593 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2594 ulen = min_t(u32, info.xlated_prog_len, ulen);
2595 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2596 kfree(insns_sanitized);
2601 if (bpf_prog_is_dev_bound(prog->aux)) {
2602 err = bpf_prog_offload_info_fill(&info, prog);
2608 /* NOTE: the following code is supposed to be skipped for offload.
2609 * bpf_prog_offload_info_fill() is the place to fill similar fields
2612 ulen = info.jited_prog_len;
2613 if (prog->aux->func_cnt) {
2616 info.jited_prog_len = 0;
2617 for (i = 0; i < prog->aux->func_cnt; i++)
2618 info.jited_prog_len += prog->aux->func[i]->jited_len;
2620 info.jited_prog_len = prog->jited_len;
2623 if (info.jited_prog_len && ulen) {
2624 if (bpf_dump_raw_ok()) {
2625 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2626 ulen = min_t(u32, info.jited_prog_len, ulen);
2628 /* for multi-function programs, copy the JITed
2629 * instructions for all the functions
2631 if (prog->aux->func_cnt) {
2636 for (i = 0; i < prog->aux->func_cnt; i++) {
2637 len = prog->aux->func[i]->jited_len;
2638 len = min_t(u32, len, free);
2639 img = (u8 *) prog->aux->func[i]->bpf_func;
2640 if (copy_to_user(uinsns, img, len))
2648 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2652 info.jited_prog_insns = 0;
2656 ulen = info.nr_jited_ksyms;
2657 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
2659 if (bpf_dump_raw_ok()) {
2660 unsigned long ksym_addr;
2661 u64 __user *user_ksyms;
2664 /* copy the address of the kernel symbol
2665 * corresponding to each function
2667 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2668 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2669 if (prog->aux->func_cnt) {
2670 for (i = 0; i < ulen; i++) {
2671 ksym_addr = (unsigned long)
2672 prog->aux->func[i]->bpf_func;
2673 if (put_user((u64) ksym_addr,
2678 ksym_addr = (unsigned long) prog->bpf_func;
2679 if (put_user((u64) ksym_addr, &user_ksyms[0]))
2683 info.jited_ksyms = 0;
2687 ulen = info.nr_jited_func_lens;
2688 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
2690 if (bpf_dump_raw_ok()) {
2691 u32 __user *user_lens;
2694 /* copy the JITed image lengths for each function */
2695 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2696 user_lens = u64_to_user_ptr(info.jited_func_lens);
2697 if (prog->aux->func_cnt) {
2698 for (i = 0; i < ulen; i++) {
2700 prog->aux->func[i]->jited_len;
2701 if (put_user(func_len, &user_lens[i]))
2705 func_len = prog->jited_len;
2706 if (put_user(func_len, &user_lens[0]))
2710 info.jited_func_lens = 0;
2715 info.btf_id = btf_id(prog->aux->btf);
2717 ulen = info.nr_func_info;
2718 info.nr_func_info = prog->aux->func_info_cnt;
2719 if (info.nr_func_info && ulen) {
2720 char __user *user_finfo;
2722 user_finfo = u64_to_user_ptr(info.func_info);
2723 ulen = min_t(u32, info.nr_func_info, ulen);
2724 if (copy_to_user(user_finfo, prog->aux->func_info,
2725 info.func_info_rec_size * ulen))
2729 ulen = info.nr_line_info;
2730 info.nr_line_info = prog->aux->nr_linfo;
2731 if (info.nr_line_info && ulen) {
2732 __u8 __user *user_linfo;
2734 user_linfo = u64_to_user_ptr(info.line_info);
2735 ulen = min_t(u32, info.nr_line_info, ulen);
2736 if (copy_to_user(user_linfo, prog->aux->linfo,
2737 info.line_info_rec_size * ulen))
2741 ulen = info.nr_jited_line_info;
2742 if (prog->aux->jited_linfo)
2743 info.nr_jited_line_info = prog->aux->nr_linfo;
2745 info.nr_jited_line_info = 0;
2746 if (info.nr_jited_line_info && ulen) {
2747 if (bpf_dump_raw_ok()) {
2748 __u64 __user *user_linfo;
2751 user_linfo = u64_to_user_ptr(info.jited_line_info);
2752 ulen = min_t(u32, info.nr_jited_line_info, ulen);
2753 for (i = 0; i < ulen; i++) {
2754 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2759 info.jited_line_info = 0;
2763 ulen = info.nr_prog_tags;
2764 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2766 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2769 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2770 ulen = min_t(u32, info.nr_prog_tags, ulen);
2771 if (prog->aux->func_cnt) {
2772 for (i = 0; i < ulen; i++) {
2773 if (copy_to_user(user_prog_tags[i],
2774 prog->aux->func[i]->tag,
2779 if (copy_to_user(user_prog_tags[0],
2780 prog->tag, BPF_TAG_SIZE))
2786 if (copy_to_user(uinfo, &info, info_len) ||
2787 put_user(info_len, &uattr->info.info_len))
2793 static int bpf_map_get_info_by_fd(struct bpf_map *map,
2794 const union bpf_attr *attr,
2795 union bpf_attr __user *uattr)
2797 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2798 struct bpf_map_info info = {};
2799 u32 info_len = attr->info.info_len;
2802 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2805 info_len = min_t(u32, sizeof(info), info_len);
2807 info.type = map->map_type;
2809 info.key_size = map->key_size;
2810 info.value_size = map->value_size;
2811 info.max_entries = map->max_entries;
2812 info.map_flags = map->map_flags;
2813 memcpy(info.name, map->name, sizeof(map->name));
2816 info.btf_id = btf_id(map->btf);
2817 info.btf_key_type_id = map->btf_key_type_id;
2818 info.btf_value_type_id = map->btf_value_type_id;
2821 if (bpf_map_is_dev_bound(map)) {
2822 err = bpf_map_offload_info_fill(&info, map);
2827 if (copy_to_user(uinfo, &info, info_len) ||
2828 put_user(info_len, &uattr->info.info_len))
2834 static int bpf_btf_get_info_by_fd(struct btf *btf,
2835 const union bpf_attr *attr,
2836 union bpf_attr __user *uattr)
2838 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2839 u32 info_len = attr->info.info_len;
2842 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
2846 return btf_get_info_by_fd(btf, attr, uattr);
2849 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2851 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2852 union bpf_attr __user *uattr)
2854 int ufd = attr->info.bpf_fd;
2858 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2865 if (f.file->f_op == &bpf_prog_fops)
2866 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2868 else if (f.file->f_op == &bpf_map_fops)
2869 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2871 else if (f.file->f_op == &btf_fops)
2872 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
2880 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2882 static int bpf_btf_load(const union bpf_attr *attr)
2884 if (CHECK_ATTR(BPF_BTF_LOAD))
2887 if (!capable(CAP_SYS_ADMIN))
2890 return btf_new_fd(attr);
2893 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2895 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2897 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2900 if (!capable(CAP_SYS_ADMIN))
2903 return btf_get_fd_by_id(attr->btf_id);
2906 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2907 union bpf_attr __user *uattr,
2908 u32 prog_id, u32 fd_type,
2909 const char *buf, u64 probe_offset,
2912 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2913 u32 len = buf ? strlen(buf) : 0, input_len;
2916 if (put_user(len, &uattr->task_fd_query.buf_len))
2918 input_len = attr->task_fd_query.buf_len;
2919 if (input_len && ubuf) {
2921 /* nothing to copy, just make ubuf NULL terminated */
2924 if (put_user(zero, ubuf))
2926 } else if (input_len >= len + 1) {
2927 /* ubuf can hold the string with NULL terminator */
2928 if (copy_to_user(ubuf, buf, len + 1))
2931 /* ubuf cannot hold the string with NULL terminator,
2932 * do a partial copy with NULL terminator.
2937 if (copy_to_user(ubuf, buf, input_len - 1))
2939 if (put_user(zero, ubuf + input_len - 1))
2944 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2945 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2946 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2947 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2953 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2955 static int bpf_task_fd_query(const union bpf_attr *attr,
2956 union bpf_attr __user *uattr)
2958 pid_t pid = attr->task_fd_query.pid;
2959 u32 fd = attr->task_fd_query.fd;
2960 const struct perf_event *event;
2961 struct files_struct *files;
2962 struct task_struct *task;
2966 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2969 if (!capable(CAP_SYS_ADMIN))
2972 if (attr->task_fd_query.flags != 0)
2975 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2979 files = get_files_struct(task);
2980 put_task_struct(task);
2985 spin_lock(&files->file_lock);
2986 file = fcheck_files(files, fd);
2991 spin_unlock(&files->file_lock);
2992 put_files_struct(files);
2997 if (file->f_op == &bpf_raw_tp_fops) {
2998 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2999 struct bpf_raw_event_map *btp = raw_tp->btp;
3001 err = bpf_task_fd_query_copy(attr, uattr,
3002 raw_tp->prog->aux->id,
3003 BPF_FD_TYPE_RAW_TRACEPOINT,
3004 btp->tp->name, 0, 0);
3008 event = perf_get_event(file);
3009 if (!IS_ERR(event)) {
3010 u64 probe_offset, probe_addr;
3011 u32 prog_id, fd_type;
3014 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3015 &buf, &probe_offset,
3018 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3032 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
3034 union bpf_attr attr = {};
3037 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
3040 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
3043 size = min_t(u32, size, sizeof(attr));
3045 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
3046 if (copy_from_user(&attr, uattr, size) != 0)
3049 err = security_bpf(cmd, &attr, size);
3054 case BPF_MAP_CREATE:
3055 err = map_create(&attr);
3057 case BPF_MAP_LOOKUP_ELEM:
3058 err = map_lookup_elem(&attr);
3060 case BPF_MAP_UPDATE_ELEM:
3061 err = map_update_elem(&attr);
3063 case BPF_MAP_DELETE_ELEM:
3064 err = map_delete_elem(&attr);
3066 case BPF_MAP_GET_NEXT_KEY:
3067 err = map_get_next_key(&attr);
3069 case BPF_MAP_FREEZE:
3070 err = map_freeze(&attr);
3073 err = bpf_prog_load(&attr, uattr);
3076 err = bpf_obj_pin(&attr);
3079 err = bpf_obj_get(&attr);
3081 case BPF_PROG_ATTACH:
3082 err = bpf_prog_attach(&attr);
3084 case BPF_PROG_DETACH:
3085 err = bpf_prog_detach(&attr);
3087 case BPF_PROG_QUERY:
3088 err = bpf_prog_query(&attr, uattr);
3090 case BPF_PROG_TEST_RUN:
3091 err = bpf_prog_test_run(&attr, uattr);
3093 case BPF_PROG_GET_NEXT_ID:
3094 err = bpf_obj_get_next_id(&attr, uattr,
3095 &prog_idr, &prog_idr_lock);
3097 case BPF_MAP_GET_NEXT_ID:
3098 err = bpf_obj_get_next_id(&attr, uattr,
3099 &map_idr, &map_idr_lock);
3101 case BPF_BTF_GET_NEXT_ID:
3102 err = bpf_obj_get_next_id(&attr, uattr,
3103 &btf_idr, &btf_idr_lock);
3105 case BPF_PROG_GET_FD_BY_ID:
3106 err = bpf_prog_get_fd_by_id(&attr);
3108 case BPF_MAP_GET_FD_BY_ID:
3109 err = bpf_map_get_fd_by_id(&attr);
3111 case BPF_OBJ_GET_INFO_BY_FD:
3112 err = bpf_obj_get_info_by_fd(&attr, uattr);
3114 case BPF_RAW_TRACEPOINT_OPEN:
3115 err = bpf_raw_tracepoint_open(&attr);
3118 err = bpf_btf_load(&attr);
3120 case BPF_BTF_GET_FD_BY_ID:
3121 err = bpf_btf_get_fd_by_id(&attr);
3123 case BPF_TASK_FD_QUERY:
3124 err = bpf_task_fd_query(&attr, uattr);
3126 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
3127 err = map_lookup_and_delete_elem(&attr);