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 u32 bpf_map_value_size(struct bpf_map *map)
134 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
135 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
136 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
137 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
138 return round_up(map->value_size, 8) * num_possible_cpus();
139 else if (IS_FD_MAP(map))
142 return map->value_size;
145 static void maybe_wait_bpf_programs(struct bpf_map *map)
147 /* Wait for any running BPF programs to complete so that
148 * userspace, when we return to it, knows that all programs
149 * that could be running use the new map value.
151 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
152 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
156 static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
157 void *value, __u64 flags)
161 /* Need to create a kthread, thus must support schedule */
162 if (bpf_map_is_dev_bound(map)) {
163 return bpf_map_offload_update_elem(map, key, value, flags);
164 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
165 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
166 map->map_type == BPF_MAP_TYPE_SOCKMAP ||
167 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
168 return map->ops->map_update_elem(map, key, value, flags);
169 } else if (IS_FD_PROG_ARRAY(map)) {
170 return bpf_fd_array_map_update_elem(map, f.file, key, value,
174 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
175 * inside bpf map update or delete otherwise deadlocks are possible
178 __this_cpu_inc(bpf_prog_active);
179 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
180 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
181 err = bpf_percpu_hash_update(map, key, value, flags);
182 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
183 err = bpf_percpu_array_update(map, key, value, flags);
184 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
185 err = bpf_percpu_cgroup_storage_update(map, key, value,
187 } else if (IS_FD_ARRAY(map)) {
189 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
192 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
194 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
197 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
198 /* rcu_read_lock() is not needed */
199 err = bpf_fd_reuseport_array_update_elem(map, key, value,
201 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
202 map->map_type == BPF_MAP_TYPE_STACK) {
203 err = map->ops->map_push_elem(map, value, flags);
206 err = map->ops->map_update_elem(map, key, value, flags);
209 __this_cpu_dec(bpf_prog_active);
211 maybe_wait_bpf_programs(map);
216 static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
222 if (bpf_map_is_dev_bound(map))
223 return bpf_map_offload_lookup_elem(map, key, value);
226 this_cpu_inc(bpf_prog_active);
227 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
228 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
229 err = bpf_percpu_hash_copy(map, key, value);
230 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
231 err = bpf_percpu_array_copy(map, key, value);
232 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
233 err = bpf_percpu_cgroup_storage_copy(map, key, value);
234 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
235 err = bpf_stackmap_copy(map, key, value);
236 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
237 err = bpf_fd_array_map_lookup_elem(map, key, value);
238 } else if (IS_FD_HASH(map)) {
239 err = bpf_fd_htab_map_lookup_elem(map, key, value);
240 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
241 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
242 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
243 map->map_type == BPF_MAP_TYPE_STACK) {
244 err = map->ops->map_peek_elem(map, value);
245 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
246 /* struct_ops map requires directly updating "value" */
247 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
250 if (map->ops->map_lookup_elem_sys_only)
251 ptr = map->ops->map_lookup_elem_sys_only(map, key);
253 ptr = map->ops->map_lookup_elem(map, key);
260 if (flags & BPF_F_LOCK)
261 /* lock 'ptr' and copy everything but lock */
262 copy_map_value_locked(map, value, ptr, true);
264 copy_map_value(map, value, ptr);
265 /* mask lock, since value wasn't zero inited */
266 check_and_init_map_lock(map, value);
271 this_cpu_dec(bpf_prog_active);
273 maybe_wait_bpf_programs(map);
278 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
280 /* We really just want to fail instead of triggering OOM killer
281 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
282 * which is used for lower order allocation requests.
284 * It has been observed that higher order allocation requests done by
285 * vmalloc with __GFP_NORETRY being set might fail due to not trying
286 * to reclaim memory from the page cache, thus we set
287 * __GFP_RETRY_MAYFAIL to avoid such situations.
290 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
293 if (size >= SIZE_MAX)
296 /* kmalloc()'ed memory can't be mmap()'ed */
297 if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
298 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
304 BUG_ON(!PAGE_ALIGNED(size));
305 return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL |
306 __GFP_RETRY_MAYFAIL | flags);
308 return __vmalloc_node_flags_caller(size, numa_node,
309 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
310 flags, __builtin_return_address(0));
313 void *bpf_map_area_alloc(u64 size, int numa_node)
315 return __bpf_map_area_alloc(size, numa_node, false);
318 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
320 return __bpf_map_area_alloc(size, numa_node, true);
323 void bpf_map_area_free(void *area)
328 static u32 bpf_map_flags_retain_permanent(u32 flags)
330 /* Some map creation flags are not tied to the map object but
331 * rather to the map fd instead, so they have no meaning upon
332 * map object inspection since multiple file descriptors with
333 * different (access) properties can exist here. Thus, given
334 * this has zero meaning for the map itself, lets clear these
337 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
340 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
342 map->map_type = attr->map_type;
343 map->key_size = attr->key_size;
344 map->value_size = attr->value_size;
345 map->max_entries = attr->max_entries;
346 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
347 map->numa_node = bpf_map_attr_numa_node(attr);
350 static int bpf_charge_memlock(struct user_struct *user, u32 pages)
352 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
354 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
355 atomic_long_sub(pages, &user->locked_vm);
361 static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
364 atomic_long_sub(pages, &user->locked_vm);
367 int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
369 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
370 struct user_struct *user;
373 if (size >= U32_MAX - PAGE_SIZE)
376 user = get_current_user();
377 ret = bpf_charge_memlock(user, pages);
389 void bpf_map_charge_finish(struct bpf_map_memory *mem)
391 bpf_uncharge_memlock(mem->user, mem->pages);
395 void bpf_map_charge_move(struct bpf_map_memory *dst,
396 struct bpf_map_memory *src)
400 /* Make sure src will not be used for the redundant uncharging. */
401 memset(src, 0, sizeof(struct bpf_map_memory));
404 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
408 ret = bpf_charge_memlock(map->memory.user, pages);
411 map->memory.pages += pages;
415 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
417 bpf_uncharge_memlock(map->memory.user, pages);
418 map->memory.pages -= pages;
421 static int bpf_map_alloc_id(struct bpf_map *map)
425 idr_preload(GFP_KERNEL);
426 spin_lock_bh(&map_idr_lock);
427 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
430 spin_unlock_bh(&map_idr_lock);
433 if (WARN_ON_ONCE(!id))
436 return id > 0 ? 0 : id;
439 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
443 /* Offloaded maps are removed from the IDR store when their device
444 * disappears - even if someone holds an fd to them they are unusable,
445 * the memory is gone, all ops will fail; they are simply waiting for
446 * refcnt to drop to be freed.
452 spin_lock_irqsave(&map_idr_lock, flags);
454 __acquire(&map_idr_lock);
456 idr_remove(&map_idr, map->id);
460 spin_unlock_irqrestore(&map_idr_lock, flags);
462 __release(&map_idr_lock);
465 /* called from workqueue */
466 static void bpf_map_free_deferred(struct work_struct *work)
468 struct bpf_map *map = container_of(work, struct bpf_map, work);
469 struct bpf_map_memory mem;
471 bpf_map_charge_move(&mem, &map->memory);
472 security_bpf_map_free(map);
473 /* implementation dependent freeing */
474 map->ops->map_free(map);
475 bpf_map_charge_finish(&mem);
478 static void bpf_map_put_uref(struct bpf_map *map)
480 if (atomic64_dec_and_test(&map->usercnt)) {
481 if (map->ops->map_release_uref)
482 map->ops->map_release_uref(map);
486 /* decrement map refcnt and schedule it for freeing via workqueue
487 * (unrelying map implementation ops->map_free() might sleep)
489 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
491 if (atomic64_dec_and_test(&map->refcnt)) {
492 /* bpf_map_free_id() must be called first */
493 bpf_map_free_id(map, do_idr_lock);
495 INIT_WORK(&map->work, bpf_map_free_deferred);
496 schedule_work(&map->work);
500 void bpf_map_put(struct bpf_map *map)
502 __bpf_map_put(map, true);
504 EXPORT_SYMBOL_GPL(bpf_map_put);
506 void bpf_map_put_with_uref(struct bpf_map *map)
508 bpf_map_put_uref(map);
512 static int bpf_map_release(struct inode *inode, struct file *filp)
514 struct bpf_map *map = filp->private_data;
516 if (map->ops->map_release)
517 map->ops->map_release(map, filp);
519 bpf_map_put_with_uref(map);
523 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
525 fmode_t mode = f.file->f_mode;
527 /* Our file permissions may have been overridden by global
528 * map permissions facing syscall side.
530 if (READ_ONCE(map->frozen))
531 mode &= ~FMODE_CAN_WRITE;
535 #ifdef CONFIG_PROC_FS
536 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
538 const struct bpf_map *map = filp->private_data;
539 const struct bpf_array *array;
540 u32 type = 0, jited = 0;
542 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
543 array = container_of(map, struct bpf_array, map);
544 type = array->aux->type;
545 jited = array->aux->jited;
562 map->memory.pages * 1ULL << PAGE_SHIFT,
564 READ_ONCE(map->frozen));
566 seq_printf(m, "owner_prog_type:\t%u\n", type);
567 seq_printf(m, "owner_jited:\t%u\n", jited);
572 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
575 /* We need this handler such that alloc_file() enables
576 * f_mode with FMODE_CAN_READ.
581 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
582 size_t siz, loff_t *ppos)
584 /* We need this handler such that alloc_file() enables
585 * f_mode with FMODE_CAN_WRITE.
590 /* called for any extra memory-mapped regions (except initial) */
591 static void bpf_map_mmap_open(struct vm_area_struct *vma)
593 struct bpf_map *map = vma->vm_file->private_data;
595 bpf_map_inc_with_uref(map);
597 if (vma->vm_flags & VM_WRITE) {
598 mutex_lock(&map->freeze_mutex);
600 mutex_unlock(&map->freeze_mutex);
604 /* called for all unmapped memory region (including initial) */
605 static void bpf_map_mmap_close(struct vm_area_struct *vma)
607 struct bpf_map *map = vma->vm_file->private_data;
609 if (vma->vm_flags & VM_WRITE) {
610 mutex_lock(&map->freeze_mutex);
612 mutex_unlock(&map->freeze_mutex);
615 bpf_map_put_with_uref(map);
618 static const struct vm_operations_struct bpf_map_default_vmops = {
619 .open = bpf_map_mmap_open,
620 .close = bpf_map_mmap_close,
623 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
625 struct bpf_map *map = filp->private_data;
628 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
631 if (!(vma->vm_flags & VM_SHARED))
634 mutex_lock(&map->freeze_mutex);
636 if ((vma->vm_flags & VM_WRITE) && map->frozen) {
641 /* set default open/close callbacks */
642 vma->vm_ops = &bpf_map_default_vmops;
643 vma->vm_private_data = map;
645 err = map->ops->map_mmap(map, vma);
649 bpf_map_inc_with_uref(map);
651 if (vma->vm_flags & VM_WRITE)
654 mutex_unlock(&map->freeze_mutex);
658 const struct file_operations bpf_map_fops = {
659 #ifdef CONFIG_PROC_FS
660 .show_fdinfo = bpf_map_show_fdinfo,
662 .release = bpf_map_release,
663 .read = bpf_dummy_read,
664 .write = bpf_dummy_write,
665 .mmap = bpf_map_mmap,
668 int bpf_map_new_fd(struct bpf_map *map, int flags)
672 ret = security_bpf_map(map, OPEN_FMODE(flags));
676 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
680 int bpf_get_file_flag(int flags)
682 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
684 if (flags & BPF_F_RDONLY)
686 if (flags & BPF_F_WRONLY)
691 /* helper macro to check that unused fields 'union bpf_attr' are zero */
692 #define CHECK_ATTR(CMD) \
693 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
694 sizeof(attr->CMD##_LAST_FIELD), 0, \
696 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
697 sizeof(attr->CMD##_LAST_FIELD)) != NULL
699 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
700 * Return 0 on success and < 0 on error.
702 static int bpf_obj_name_cpy(char *dst, const char *src)
704 const char *end = src + BPF_OBJ_NAME_LEN;
706 memset(dst, 0, BPF_OBJ_NAME_LEN);
707 /* Copy all isalnum(), '_' and '.' chars. */
708 while (src < end && *src) {
709 if (!isalnum(*src) &&
710 *src != '_' && *src != '.')
715 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
722 int map_check_no_btf(const struct bpf_map *map,
723 const struct btf *btf,
724 const struct btf_type *key_type,
725 const struct btf_type *value_type)
730 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
731 u32 btf_key_id, u32 btf_value_id)
733 const struct btf_type *key_type, *value_type;
734 u32 key_size, value_size;
737 /* Some maps allow key to be unspecified. */
739 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
740 if (!key_type || key_size != map->key_size)
743 key_type = btf_type_by_id(btf, 0);
744 if (!map->ops->map_check_btf)
748 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
749 if (!value_type || value_size != map->value_size)
752 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
754 if (map_value_has_spin_lock(map)) {
755 if (map->map_flags & BPF_F_RDONLY_PROG)
757 if (map->map_type != BPF_MAP_TYPE_HASH &&
758 map->map_type != BPF_MAP_TYPE_ARRAY &&
759 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
760 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
762 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
765 "verifier bug spin_lock_off %d value_size %d\n",
766 map->spin_lock_off, map->value_size);
771 if (map->ops->map_check_btf)
772 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
777 #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
778 /* called via syscall */
779 static int map_create(union bpf_attr *attr)
781 int numa_node = bpf_map_attr_numa_node(attr);
782 struct bpf_map_memory mem;
787 err = CHECK_ATTR(BPF_MAP_CREATE);
791 if (attr->btf_vmlinux_value_type_id) {
792 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
793 attr->btf_key_type_id || attr->btf_value_type_id)
795 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
799 f_flags = bpf_get_file_flag(attr->map_flags);
803 if (numa_node != NUMA_NO_NODE &&
804 ((unsigned int)numa_node >= nr_node_ids ||
805 !node_online(numa_node)))
808 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
809 map = find_and_alloc_map(attr);
813 err = bpf_obj_name_cpy(map->name, attr->map_name);
817 atomic64_set(&map->refcnt, 1);
818 atomic64_set(&map->usercnt, 1);
819 mutex_init(&map->freeze_mutex);
821 map->spin_lock_off = -EINVAL;
822 if (attr->btf_key_type_id || attr->btf_value_type_id ||
823 /* Even the map's value is a kernel's struct,
824 * the bpf_prog.o must have BTF to begin with
825 * to figure out the corresponding kernel's
826 * counter part. Thus, attr->btf_fd has
829 attr->btf_vmlinux_value_type_id) {
832 btf = btf_get_by_fd(attr->btf_fd);
839 if (attr->btf_value_type_id) {
840 err = map_check_btf(map, btf, attr->btf_key_type_id,
841 attr->btf_value_type_id);
846 map->btf_key_type_id = attr->btf_key_type_id;
847 map->btf_value_type_id = attr->btf_value_type_id;
848 map->btf_vmlinux_value_type_id =
849 attr->btf_vmlinux_value_type_id;
852 err = security_bpf_map_alloc(map);
856 err = bpf_map_alloc_id(map);
860 err = bpf_map_new_fd(map, f_flags);
862 /* failed to allocate fd.
863 * bpf_map_put_with_uref() is needed because the above
864 * bpf_map_alloc_id() has published the map
865 * to the userspace and the userspace may
866 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
868 bpf_map_put_with_uref(map);
875 security_bpf_map_free(map);
878 bpf_map_charge_move(&mem, &map->memory);
879 map->ops->map_free(map);
880 bpf_map_charge_finish(&mem);
884 /* if error is returned, fd is released.
885 * On success caller should complete fd access with matching fdput()
887 struct bpf_map *__bpf_map_get(struct fd f)
890 return ERR_PTR(-EBADF);
891 if (f.file->f_op != &bpf_map_fops) {
893 return ERR_PTR(-EINVAL);
896 return f.file->private_data;
899 void bpf_map_inc(struct bpf_map *map)
901 atomic64_inc(&map->refcnt);
903 EXPORT_SYMBOL_GPL(bpf_map_inc);
905 void bpf_map_inc_with_uref(struct bpf_map *map)
907 atomic64_inc(&map->refcnt);
908 atomic64_inc(&map->usercnt);
910 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
912 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
914 struct fd f = fdget(ufd);
917 map = __bpf_map_get(f);
921 bpf_map_inc_with_uref(map);
927 /* map_idr_lock should have been held */
928 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
932 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
934 return ERR_PTR(-ENOENT);
936 atomic64_inc(&map->usercnt);
941 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
943 spin_lock_bh(&map_idr_lock);
944 map = __bpf_map_inc_not_zero(map, false);
945 spin_unlock_bh(&map_idr_lock);
949 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
951 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
956 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
959 return memdup_user(ukey, key_size);
962 return ERR_PTR(-EINVAL);
967 /* last field in 'union bpf_attr' used by this command */
968 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
970 static int map_lookup_elem(union bpf_attr *attr)
972 void __user *ukey = u64_to_user_ptr(attr->key);
973 void __user *uvalue = u64_to_user_ptr(attr->value);
974 int ufd = attr->map_fd;
981 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
984 if (attr->flags & ~BPF_F_LOCK)
988 map = __bpf_map_get(f);
991 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
996 if ((attr->flags & BPF_F_LOCK) &&
997 !map_value_has_spin_lock(map)) {
1002 key = __bpf_copy_key(ukey, map->key_size);
1008 value_size = bpf_map_value_size(map);
1011 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1015 err = bpf_map_copy_value(map, key, value, attr->flags);
1020 if (copy_to_user(uvalue, value, value_size) != 0)
1035 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1037 static int map_update_elem(union bpf_attr *attr)
1039 void __user *ukey = u64_to_user_ptr(attr->key);
1040 void __user *uvalue = u64_to_user_ptr(attr->value);
1041 int ufd = attr->map_fd;
1042 struct bpf_map *map;
1048 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1052 map = __bpf_map_get(f);
1054 return PTR_ERR(map);
1055 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1060 if ((attr->flags & BPF_F_LOCK) &&
1061 !map_value_has_spin_lock(map)) {
1066 key = __bpf_copy_key(ukey, map->key_size);
1072 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1073 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
1074 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1075 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
1076 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1078 value_size = map->value_size;
1081 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1086 if (copy_from_user(value, uvalue, value_size) != 0)
1089 err = bpf_map_update_value(map, f, key, value, attr->flags);
1100 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1102 static int map_delete_elem(union bpf_attr *attr)
1104 void __user *ukey = u64_to_user_ptr(attr->key);
1105 int ufd = attr->map_fd;
1106 struct bpf_map *map;
1111 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1115 map = __bpf_map_get(f);
1117 return PTR_ERR(map);
1118 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1123 key = __bpf_copy_key(ukey, map->key_size);
1129 if (bpf_map_is_dev_bound(map)) {
1130 err = bpf_map_offload_delete_elem(map, key);
1132 } else if (IS_FD_PROG_ARRAY(map) ||
1133 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1134 /* These maps require sleepable context */
1135 err = map->ops->map_delete_elem(map, key);
1140 __this_cpu_inc(bpf_prog_active);
1142 err = map->ops->map_delete_elem(map, key);
1144 __this_cpu_dec(bpf_prog_active);
1146 maybe_wait_bpf_programs(map);
1154 /* last field in 'union bpf_attr' used by this command */
1155 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1157 static int map_get_next_key(union bpf_attr *attr)
1159 void __user *ukey = u64_to_user_ptr(attr->key);
1160 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1161 int ufd = attr->map_fd;
1162 struct bpf_map *map;
1163 void *key, *next_key;
1167 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1171 map = __bpf_map_get(f);
1173 return PTR_ERR(map);
1174 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1180 key = __bpf_copy_key(ukey, map->key_size);
1190 next_key = kmalloc(map->key_size, GFP_USER);
1194 if (bpf_map_is_dev_bound(map)) {
1195 err = bpf_map_offload_get_next_key(map, key, next_key);
1200 err = map->ops->map_get_next_key(map, key, next_key);
1207 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1221 int generic_map_delete_batch(struct bpf_map *map,
1222 const union bpf_attr *attr,
1223 union bpf_attr __user *uattr)
1225 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1230 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1233 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1234 !map_value_has_spin_lock(map)) {
1238 max_count = attr->batch.count;
1242 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1246 for (cp = 0; cp < max_count; cp++) {
1248 if (copy_from_user(key, keys + cp * map->key_size,
1252 if (bpf_map_is_dev_bound(map)) {
1253 err = bpf_map_offload_delete_elem(map, key);
1258 __this_cpu_inc(bpf_prog_active);
1260 err = map->ops->map_delete_elem(map, key);
1262 __this_cpu_dec(bpf_prog_active);
1264 maybe_wait_bpf_programs(map);
1268 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1275 int generic_map_update_batch(struct bpf_map *map,
1276 const union bpf_attr *attr,
1277 union bpf_attr __user *uattr)
1279 void __user *values = u64_to_user_ptr(attr->batch.values);
1280 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1281 u32 value_size, cp, max_count;
1282 int ufd = attr->map_fd;
1288 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1291 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1292 !map_value_has_spin_lock(map)) {
1296 value_size = bpf_map_value_size(map);
1298 max_count = attr->batch.count;
1302 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1306 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1312 for (cp = 0; cp < max_count; cp++) {
1314 if (copy_from_user(key, keys + cp * map->key_size,
1316 copy_from_user(value, values + cp * value_size, value_size))
1319 err = bpf_map_update_value(map, f, key, value,
1320 attr->batch.elem_flags);
1326 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1334 #define MAP_LOOKUP_RETRIES 3
1336 int generic_map_lookup_batch(struct bpf_map *map,
1337 const union bpf_attr *attr,
1338 union bpf_attr __user *uattr)
1340 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1341 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1342 void __user *values = u64_to_user_ptr(attr->batch.values);
1343 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1344 void *buf, *buf_prevkey, *prev_key, *key, *value;
1345 int err, retry = MAP_LOOKUP_RETRIES;
1346 u32 value_size, cp, max_count;
1348 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1351 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1352 !map_value_has_spin_lock(map))
1355 value_size = bpf_map_value_size(map);
1357 max_count = attr->batch.count;
1361 if (put_user(0, &uattr->batch.count))
1364 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1368 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1370 kvfree(buf_prevkey);
1376 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1379 value = key + map->key_size;
1381 prev_key = buf_prevkey;
1383 for (cp = 0; cp < max_count;) {
1385 err = map->ops->map_get_next_key(map, prev_key, key);
1389 err = bpf_map_copy_value(map, key, value,
1390 attr->batch.elem_flags);
1392 if (err == -ENOENT) {
1404 if (copy_to_user(keys + cp * map->key_size, key,
1409 if (copy_to_user(values + cp * value_size, value, value_size)) {
1415 prev_key = buf_prevkey;
1417 swap(prev_key, key);
1418 retry = MAP_LOOKUP_RETRIES;
1425 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1426 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1435 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1437 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1439 void __user *ukey = u64_to_user_ptr(attr->key);
1440 void __user *uvalue = u64_to_user_ptr(attr->value);
1441 int ufd = attr->map_fd;
1442 struct bpf_map *map;
1448 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1452 map = __bpf_map_get(f);
1454 return PTR_ERR(map);
1455 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1460 key = __bpf_copy_key(ukey, map->key_size);
1466 value_size = map->value_size;
1469 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1473 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1474 map->map_type == BPF_MAP_TYPE_STACK) {
1475 err = map->ops->map_pop_elem(map, value);
1483 if (copy_to_user(uvalue, value, value_size) != 0)
1497 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1499 static int map_freeze(const union bpf_attr *attr)
1501 int err = 0, ufd = attr->map_fd;
1502 struct bpf_map *map;
1505 if (CHECK_ATTR(BPF_MAP_FREEZE))
1509 map = __bpf_map_get(f);
1511 return PTR_ERR(map);
1513 mutex_lock(&map->freeze_mutex);
1515 if (map->writecnt) {
1519 if (READ_ONCE(map->frozen)) {
1523 if (!capable(CAP_SYS_ADMIN)) {
1528 WRITE_ONCE(map->frozen, true);
1530 mutex_unlock(&map->freeze_mutex);
1535 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1536 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1537 [_id] = & _name ## _prog_ops,
1538 #define BPF_MAP_TYPE(_id, _ops)
1539 #include <linux/bpf_types.h>
1540 #undef BPF_PROG_TYPE
1544 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1546 const struct bpf_prog_ops *ops;
1548 if (type >= ARRAY_SIZE(bpf_prog_types))
1550 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1551 ops = bpf_prog_types[type];
1555 if (!bpf_prog_is_dev_bound(prog->aux))
1556 prog->aux->ops = ops;
1558 prog->aux->ops = &bpf_offload_prog_ops;
1569 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1570 [BPF_AUDIT_LOAD] = "LOAD",
1571 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1574 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1576 struct audit_context *ctx = NULL;
1577 struct audit_buffer *ab;
1579 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1581 if (audit_enabled == AUDIT_OFF)
1583 if (op == BPF_AUDIT_LOAD)
1584 ctx = audit_context();
1585 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1588 audit_log_format(ab, "prog-id=%u op=%s",
1589 prog->aux->id, bpf_audit_str[op]);
1593 int __bpf_prog_charge(struct user_struct *user, u32 pages)
1595 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1596 unsigned long user_bufs;
1599 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1600 if (user_bufs > memlock_limit) {
1601 atomic_long_sub(pages, &user->locked_vm);
1609 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1612 atomic_long_sub(pages, &user->locked_vm);
1615 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1617 struct user_struct *user = get_current_user();
1620 ret = __bpf_prog_charge(user, prog->pages);
1626 prog->aux->user = user;
1630 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1632 struct user_struct *user = prog->aux->user;
1634 __bpf_prog_uncharge(user, prog->pages);
1638 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1642 idr_preload(GFP_KERNEL);
1643 spin_lock_bh(&prog_idr_lock);
1644 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1647 spin_unlock_bh(&prog_idr_lock);
1650 /* id is in [1, INT_MAX) */
1651 if (WARN_ON_ONCE(!id))
1654 return id > 0 ? 0 : id;
1657 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1659 /* cBPF to eBPF migrations are currently not in the idr store.
1660 * Offloaded programs are removed from the store when their device
1661 * disappears - even if someone grabs an fd to them they are unusable,
1662 * simply waiting for refcnt to drop to be freed.
1668 spin_lock_bh(&prog_idr_lock);
1670 __acquire(&prog_idr_lock);
1672 idr_remove(&prog_idr, prog->aux->id);
1676 spin_unlock_bh(&prog_idr_lock);
1678 __release(&prog_idr_lock);
1681 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1683 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1685 kvfree(aux->func_info);
1686 kfree(aux->func_info_aux);
1687 bpf_prog_uncharge_memlock(aux->prog);
1688 security_bpf_prog_free(aux);
1689 bpf_prog_free(aux->prog);
1692 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1694 bpf_prog_kallsyms_del_all(prog);
1695 btf_put(prog->aux->btf);
1696 bpf_prog_free_linfo(prog);
1699 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1701 __bpf_prog_put_rcu(&prog->aux->rcu);
1704 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1706 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
1707 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1708 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1709 /* bpf_prog_free_id() must be called first */
1710 bpf_prog_free_id(prog, do_idr_lock);
1711 __bpf_prog_put_noref(prog, true);
1715 void bpf_prog_put(struct bpf_prog *prog)
1717 __bpf_prog_put(prog, true);
1719 EXPORT_SYMBOL_GPL(bpf_prog_put);
1721 static int bpf_prog_release(struct inode *inode, struct file *filp)
1723 struct bpf_prog *prog = filp->private_data;
1729 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1730 struct bpf_prog_stats *stats)
1732 u64 nsecs = 0, cnt = 0;
1735 for_each_possible_cpu(cpu) {
1736 const struct bpf_prog_stats *st;
1740 st = per_cpu_ptr(prog->aux->stats, cpu);
1742 start = u64_stats_fetch_begin_irq(&st->syncp);
1745 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1749 stats->nsecs = nsecs;
1753 #ifdef CONFIG_PROC_FS
1754 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1756 const struct bpf_prog *prog = filp->private_data;
1757 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1758 struct bpf_prog_stats stats;
1760 bpf_prog_get_stats(prog, &stats);
1761 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1768 "run_time_ns:\t%llu\n"
1773 prog->pages * 1ULL << PAGE_SHIFT,
1780 const struct file_operations bpf_prog_fops = {
1781 #ifdef CONFIG_PROC_FS
1782 .show_fdinfo = bpf_prog_show_fdinfo,
1784 .release = bpf_prog_release,
1785 .read = bpf_dummy_read,
1786 .write = bpf_dummy_write,
1789 int bpf_prog_new_fd(struct bpf_prog *prog)
1793 ret = security_bpf_prog(prog);
1797 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1798 O_RDWR | O_CLOEXEC);
1801 static struct bpf_prog *____bpf_prog_get(struct fd f)
1804 return ERR_PTR(-EBADF);
1805 if (f.file->f_op != &bpf_prog_fops) {
1807 return ERR_PTR(-EINVAL);
1810 return f.file->private_data;
1813 void bpf_prog_add(struct bpf_prog *prog, int i)
1815 atomic64_add(i, &prog->aux->refcnt);
1817 EXPORT_SYMBOL_GPL(bpf_prog_add);
1819 void bpf_prog_sub(struct bpf_prog *prog, int i)
1821 /* Only to be used for undoing previous bpf_prog_add() in some
1822 * error path. We still know that another entity in our call
1823 * path holds a reference to the program, thus atomic_sub() can
1824 * be safely used in such cases!
1826 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1828 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1830 void bpf_prog_inc(struct bpf_prog *prog)
1832 atomic64_inc(&prog->aux->refcnt);
1834 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1836 /* prog_idr_lock should have been held */
1837 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1841 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1844 return ERR_PTR(-ENOENT);
1848 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1850 bool bpf_prog_get_ok(struct bpf_prog *prog,
1851 enum bpf_prog_type *attach_type, bool attach_drv)
1853 /* not an attachment, just a refcount inc, always allow */
1857 if (prog->type != *attach_type)
1859 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1865 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1868 struct fd f = fdget(ufd);
1869 struct bpf_prog *prog;
1871 prog = ____bpf_prog_get(f);
1874 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1875 prog = ERR_PTR(-EINVAL);
1885 struct bpf_prog *bpf_prog_get(u32 ufd)
1887 return __bpf_prog_get(ufd, NULL, false);
1890 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1893 return __bpf_prog_get(ufd, &type, attach_drv);
1895 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1897 /* Initially all BPF programs could be loaded w/o specifying
1898 * expected_attach_type. Later for some of them specifying expected_attach_type
1899 * at load time became required so that program could be validated properly.
1900 * Programs of types that are allowed to be loaded both w/ and w/o (for
1901 * backward compatibility) expected_attach_type, should have the default attach
1902 * type assigned to expected_attach_type for the latter case, so that it can be
1903 * validated later at attach time.
1905 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1906 * prog type requires it but has some attach types that have to be backward
1909 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1911 switch (attr->prog_type) {
1912 case BPF_PROG_TYPE_CGROUP_SOCK:
1913 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1914 * exist so checking for non-zero is the way to go here.
1916 if (!attr->expected_attach_type)
1917 attr->expected_attach_type =
1918 BPF_CGROUP_INET_SOCK_CREATE;
1924 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1925 enum bpf_attach_type expected_attach_type,
1926 u32 btf_id, u32 prog_fd)
1929 if (btf_id > BTF_MAX_TYPE)
1932 switch (prog_type) {
1933 case BPF_PROG_TYPE_TRACING:
1934 case BPF_PROG_TYPE_STRUCT_OPS:
1935 case BPF_PROG_TYPE_EXT:
1942 if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING &&
1943 prog_type != BPF_PROG_TYPE_EXT)
1946 switch (prog_type) {
1947 case BPF_PROG_TYPE_CGROUP_SOCK:
1948 switch (expected_attach_type) {
1949 case BPF_CGROUP_INET_SOCK_CREATE:
1950 case BPF_CGROUP_INET4_POST_BIND:
1951 case BPF_CGROUP_INET6_POST_BIND:
1956 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1957 switch (expected_attach_type) {
1958 case BPF_CGROUP_INET4_BIND:
1959 case BPF_CGROUP_INET6_BIND:
1960 case BPF_CGROUP_INET4_CONNECT:
1961 case BPF_CGROUP_INET6_CONNECT:
1962 case BPF_CGROUP_UDP4_SENDMSG:
1963 case BPF_CGROUP_UDP6_SENDMSG:
1964 case BPF_CGROUP_UDP4_RECVMSG:
1965 case BPF_CGROUP_UDP6_RECVMSG:
1970 case BPF_PROG_TYPE_CGROUP_SKB:
1971 switch (expected_attach_type) {
1972 case BPF_CGROUP_INET_INGRESS:
1973 case BPF_CGROUP_INET_EGRESS:
1978 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1979 switch (expected_attach_type) {
1980 case BPF_CGROUP_SETSOCKOPT:
1981 case BPF_CGROUP_GETSOCKOPT:
1986 case BPF_PROG_TYPE_EXT:
1987 if (expected_attach_type)
1995 /* last field in 'union bpf_attr' used by this command */
1996 #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
1998 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
2000 enum bpf_prog_type type = attr->prog_type;
2001 struct bpf_prog *prog;
2006 if (CHECK_ATTR(BPF_PROG_LOAD))
2009 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2010 BPF_F_ANY_ALIGNMENT |
2011 BPF_F_TEST_STATE_FREQ |
2012 BPF_F_TEST_RND_HI32))
2015 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2016 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2017 !capable(CAP_SYS_ADMIN))
2020 /* copy eBPF program license from user space */
2021 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
2022 sizeof(license) - 1) < 0)
2024 license[sizeof(license) - 1] = 0;
2026 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2027 is_gpl = license_is_gpl_compatible(license);
2029 if (attr->insn_cnt == 0 ||
2030 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2032 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2033 type != BPF_PROG_TYPE_CGROUP_SKB &&
2034 !capable(CAP_SYS_ADMIN))
2037 bpf_prog_load_fixup_attach_type(attr);
2038 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2039 attr->attach_btf_id,
2040 attr->attach_prog_fd))
2043 /* plain bpf_prog allocation */
2044 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2048 prog->expected_attach_type = attr->expected_attach_type;
2049 prog->aux->attach_btf_id = attr->attach_btf_id;
2050 if (attr->attach_prog_fd) {
2051 struct bpf_prog *tgt_prog;
2053 tgt_prog = bpf_prog_get(attr->attach_prog_fd);
2054 if (IS_ERR(tgt_prog)) {
2055 err = PTR_ERR(tgt_prog);
2056 goto free_prog_nouncharge;
2058 prog->aux->linked_prog = tgt_prog;
2061 prog->aux->offload_requested = !!attr->prog_ifindex;
2063 err = security_bpf_prog_alloc(prog->aux);
2065 goto free_prog_nouncharge;
2067 err = bpf_prog_charge_memlock(prog);
2071 prog->len = attr->insn_cnt;
2074 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
2075 bpf_prog_insn_size(prog)) != 0)
2078 prog->orig_prog = NULL;
2081 atomic64_set(&prog->aux->refcnt, 1);
2082 prog->gpl_compatible = is_gpl ? 1 : 0;
2084 if (bpf_prog_is_dev_bound(prog->aux)) {
2085 err = bpf_prog_offload_init(prog, attr);
2090 /* find program type: socket_filter vs tracing_filter */
2091 err = find_prog_type(type, prog);
2095 prog->aux->load_time = ktime_get_boottime_ns();
2096 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
2100 /* run eBPF verifier */
2101 err = bpf_check(&prog, attr, uattr);
2103 goto free_used_maps;
2105 prog = bpf_prog_select_runtime(prog, &err);
2107 goto free_used_maps;
2109 err = bpf_prog_alloc_id(prog);
2111 goto free_used_maps;
2113 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2114 * effectively publicly exposed. However, retrieving via
2115 * bpf_prog_get_fd_by_id() will take another reference,
2116 * therefore it cannot be gone underneath us.
2118 * Only for the time /after/ successful bpf_prog_new_fd()
2119 * and before returning to userspace, we might just hold
2120 * one reference and any parallel close on that fd could
2121 * rip everything out. Hence, below notifications must
2122 * happen before bpf_prog_new_fd().
2124 * Also, any failure handling from this point onwards must
2125 * be using bpf_prog_put() given the program is exposed.
2127 bpf_prog_kallsyms_add(prog);
2128 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2129 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2131 err = bpf_prog_new_fd(prog);
2137 /* In case we have subprogs, we need to wait for a grace
2138 * period before we can tear down JIT memory since symbols
2139 * are already exposed under kallsyms.
2141 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2144 bpf_prog_uncharge_memlock(prog);
2146 security_bpf_prog_free(prog->aux);
2147 free_prog_nouncharge:
2148 bpf_prog_free(prog);
2152 #define BPF_OBJ_LAST_FIELD file_flags
2154 static int bpf_obj_pin(const union bpf_attr *attr)
2156 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2159 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2162 static int bpf_obj_get(const union bpf_attr *attr)
2164 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2165 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2168 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2172 static int bpf_tracing_prog_release(struct inode *inode, struct file *filp)
2174 struct bpf_prog *prog = filp->private_data;
2176 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
2181 static const struct file_operations bpf_tracing_prog_fops = {
2182 .release = bpf_tracing_prog_release,
2183 .read = bpf_dummy_read,
2184 .write = bpf_dummy_write,
2187 static int bpf_tracing_prog_attach(struct bpf_prog *prog)
2191 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2192 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2193 prog->type != BPF_PROG_TYPE_EXT) {
2198 err = bpf_trampoline_link_prog(prog);
2202 tr_fd = anon_inode_getfd("bpf-tracing-prog", &bpf_tracing_prog_fops,
2205 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
2216 struct bpf_raw_tracepoint {
2217 struct bpf_raw_event_map *btp;
2218 struct bpf_prog *prog;
2221 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
2223 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
2226 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
2227 bpf_prog_put(raw_tp->prog);
2229 bpf_put_raw_tracepoint(raw_tp->btp);
2234 static const struct file_operations bpf_raw_tp_fops = {
2235 .release = bpf_raw_tracepoint_release,
2236 .read = bpf_dummy_read,
2237 .write = bpf_dummy_write,
2240 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2242 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2244 struct bpf_raw_tracepoint *raw_tp;
2245 struct bpf_raw_event_map *btp;
2246 struct bpf_prog *prog;
2247 const char *tp_name;
2251 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2254 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2256 return PTR_ERR(prog);
2258 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
2259 prog->type != BPF_PROG_TYPE_TRACING &&
2260 prog->type != BPF_PROG_TYPE_EXT &&
2261 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
2266 if (prog->type == BPF_PROG_TYPE_TRACING ||
2267 prog->type == BPF_PROG_TYPE_EXT) {
2268 if (attr->raw_tracepoint.name) {
2269 /* The attach point for this category of programs
2270 * should be specified via btf_id during program load.
2275 if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
2276 tp_name = prog->aux->attach_func_name;
2278 return bpf_tracing_prog_attach(prog);
2280 if (strncpy_from_user(buf,
2281 u64_to_user_ptr(attr->raw_tracepoint.name),
2282 sizeof(buf) - 1) < 0) {
2286 buf[sizeof(buf) - 1] = 0;
2290 btp = bpf_get_raw_tracepoint(tp_name);
2296 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
2302 raw_tp->prog = prog;
2304 err = bpf_probe_register(raw_tp->btp, prog);
2308 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
2311 bpf_probe_unregister(raw_tp->btp, prog);
2320 bpf_put_raw_tracepoint(btp);
2326 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2327 enum bpf_attach_type attach_type)
2329 switch (prog->type) {
2330 case BPF_PROG_TYPE_CGROUP_SOCK:
2331 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2332 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2333 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
2334 case BPF_PROG_TYPE_CGROUP_SKB:
2335 return prog->enforce_expected_attach_type &&
2336 prog->expected_attach_type != attach_type ?
2343 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
2345 #define BPF_F_ATTACH_MASK \
2346 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
2348 static int bpf_prog_attach(const union bpf_attr *attr)
2350 enum bpf_prog_type ptype;
2351 struct bpf_prog *prog;
2354 if (!capable(CAP_NET_ADMIN))
2357 if (CHECK_ATTR(BPF_PROG_ATTACH))
2360 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
2363 switch (attr->attach_type) {
2364 case BPF_CGROUP_INET_INGRESS:
2365 case BPF_CGROUP_INET_EGRESS:
2366 ptype = BPF_PROG_TYPE_CGROUP_SKB;
2368 case BPF_CGROUP_INET_SOCK_CREATE:
2369 case BPF_CGROUP_INET4_POST_BIND:
2370 case BPF_CGROUP_INET6_POST_BIND:
2371 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2373 case BPF_CGROUP_INET4_BIND:
2374 case BPF_CGROUP_INET6_BIND:
2375 case BPF_CGROUP_INET4_CONNECT:
2376 case BPF_CGROUP_INET6_CONNECT:
2377 case BPF_CGROUP_UDP4_SENDMSG:
2378 case BPF_CGROUP_UDP6_SENDMSG:
2379 case BPF_CGROUP_UDP4_RECVMSG:
2380 case BPF_CGROUP_UDP6_RECVMSG:
2381 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2383 case BPF_CGROUP_SOCK_OPS:
2384 ptype = BPF_PROG_TYPE_SOCK_OPS;
2386 case BPF_CGROUP_DEVICE:
2387 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2389 case BPF_SK_MSG_VERDICT:
2390 ptype = BPF_PROG_TYPE_SK_MSG;
2392 case BPF_SK_SKB_STREAM_PARSER:
2393 case BPF_SK_SKB_STREAM_VERDICT:
2394 ptype = BPF_PROG_TYPE_SK_SKB;
2396 case BPF_LIRC_MODE2:
2397 ptype = BPF_PROG_TYPE_LIRC_MODE2;
2399 case BPF_FLOW_DISSECTOR:
2400 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
2402 case BPF_CGROUP_SYSCTL:
2403 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2405 case BPF_CGROUP_GETSOCKOPT:
2406 case BPF_CGROUP_SETSOCKOPT:
2407 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2413 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2415 return PTR_ERR(prog);
2417 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
2423 case BPF_PROG_TYPE_SK_SKB:
2424 case BPF_PROG_TYPE_SK_MSG:
2425 ret = sock_map_get_from_fd(attr, prog);
2427 case BPF_PROG_TYPE_LIRC_MODE2:
2428 ret = lirc_prog_attach(attr, prog);
2430 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2431 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
2434 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
2442 #define BPF_PROG_DETACH_LAST_FIELD attach_type
2444 static int bpf_prog_detach(const union bpf_attr *attr)
2446 enum bpf_prog_type ptype;
2448 if (!capable(CAP_NET_ADMIN))
2451 if (CHECK_ATTR(BPF_PROG_DETACH))
2454 switch (attr->attach_type) {
2455 case BPF_CGROUP_INET_INGRESS:
2456 case BPF_CGROUP_INET_EGRESS:
2457 ptype = BPF_PROG_TYPE_CGROUP_SKB;
2459 case BPF_CGROUP_INET_SOCK_CREATE:
2460 case BPF_CGROUP_INET4_POST_BIND:
2461 case BPF_CGROUP_INET6_POST_BIND:
2462 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2464 case BPF_CGROUP_INET4_BIND:
2465 case BPF_CGROUP_INET6_BIND:
2466 case BPF_CGROUP_INET4_CONNECT:
2467 case BPF_CGROUP_INET6_CONNECT:
2468 case BPF_CGROUP_UDP4_SENDMSG:
2469 case BPF_CGROUP_UDP6_SENDMSG:
2470 case BPF_CGROUP_UDP4_RECVMSG:
2471 case BPF_CGROUP_UDP6_RECVMSG:
2472 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2474 case BPF_CGROUP_SOCK_OPS:
2475 ptype = BPF_PROG_TYPE_SOCK_OPS;
2477 case BPF_CGROUP_DEVICE:
2478 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2480 case BPF_SK_MSG_VERDICT:
2481 return sock_map_get_from_fd(attr, NULL);
2482 case BPF_SK_SKB_STREAM_PARSER:
2483 case BPF_SK_SKB_STREAM_VERDICT:
2484 return sock_map_get_from_fd(attr, NULL);
2485 case BPF_LIRC_MODE2:
2486 return lirc_prog_detach(attr);
2487 case BPF_FLOW_DISSECTOR:
2488 return skb_flow_dissector_bpf_prog_detach(attr);
2489 case BPF_CGROUP_SYSCTL:
2490 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2492 case BPF_CGROUP_GETSOCKOPT:
2493 case BPF_CGROUP_SETSOCKOPT:
2494 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2500 return cgroup_bpf_prog_detach(attr, ptype);
2503 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2505 static int bpf_prog_query(const union bpf_attr *attr,
2506 union bpf_attr __user *uattr)
2508 if (!capable(CAP_NET_ADMIN))
2510 if (CHECK_ATTR(BPF_PROG_QUERY))
2512 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2515 switch (attr->query.attach_type) {
2516 case BPF_CGROUP_INET_INGRESS:
2517 case BPF_CGROUP_INET_EGRESS:
2518 case BPF_CGROUP_INET_SOCK_CREATE:
2519 case BPF_CGROUP_INET4_BIND:
2520 case BPF_CGROUP_INET6_BIND:
2521 case BPF_CGROUP_INET4_POST_BIND:
2522 case BPF_CGROUP_INET6_POST_BIND:
2523 case BPF_CGROUP_INET4_CONNECT:
2524 case BPF_CGROUP_INET6_CONNECT:
2525 case BPF_CGROUP_UDP4_SENDMSG:
2526 case BPF_CGROUP_UDP6_SENDMSG:
2527 case BPF_CGROUP_UDP4_RECVMSG:
2528 case BPF_CGROUP_UDP6_RECVMSG:
2529 case BPF_CGROUP_SOCK_OPS:
2530 case BPF_CGROUP_DEVICE:
2531 case BPF_CGROUP_SYSCTL:
2532 case BPF_CGROUP_GETSOCKOPT:
2533 case BPF_CGROUP_SETSOCKOPT:
2535 case BPF_LIRC_MODE2:
2536 return lirc_prog_query(attr, uattr);
2537 case BPF_FLOW_DISSECTOR:
2538 return skb_flow_dissector_prog_query(attr, uattr);
2543 return cgroup_bpf_prog_query(attr, uattr);
2546 #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
2548 static int bpf_prog_test_run(const union bpf_attr *attr,
2549 union bpf_attr __user *uattr)
2551 struct bpf_prog *prog;
2552 int ret = -ENOTSUPP;
2554 if (!capable(CAP_SYS_ADMIN))
2556 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2559 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2560 (!attr->test.ctx_size_in && attr->test.ctx_in))
2563 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2564 (!attr->test.ctx_size_out && attr->test.ctx_out))
2567 prog = bpf_prog_get(attr->test.prog_fd);
2569 return PTR_ERR(prog);
2571 if (prog->aux->ops->test_run)
2572 ret = prog->aux->ops->test_run(prog, attr, uattr);
2578 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2580 static int bpf_obj_get_next_id(const union bpf_attr *attr,
2581 union bpf_attr __user *uattr,
2585 u32 next_id = attr->start_id;
2588 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2591 if (!capable(CAP_SYS_ADMIN))
2596 if (!idr_get_next(idr, &next_id))
2598 spin_unlock_bh(lock);
2601 err = put_user(next_id, &uattr->next_id);
2606 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2608 struct bpf_prog *bpf_prog_by_id(u32 id)
2610 struct bpf_prog *prog;
2613 return ERR_PTR(-ENOENT);
2615 spin_lock_bh(&prog_idr_lock);
2616 prog = idr_find(&prog_idr, id);
2618 prog = bpf_prog_inc_not_zero(prog);
2620 prog = ERR_PTR(-ENOENT);
2621 spin_unlock_bh(&prog_idr_lock);
2625 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2627 struct bpf_prog *prog;
2628 u32 id = attr->prog_id;
2631 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2634 if (!capable(CAP_SYS_ADMIN))
2637 prog = bpf_prog_by_id(id);
2639 return PTR_ERR(prog);
2641 fd = bpf_prog_new_fd(prog);
2648 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2650 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2652 struct bpf_map *map;
2653 u32 id = attr->map_id;
2657 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2658 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
2661 if (!capable(CAP_SYS_ADMIN))
2664 f_flags = bpf_get_file_flag(attr->open_flags);
2668 spin_lock_bh(&map_idr_lock);
2669 map = idr_find(&map_idr, id);
2671 map = __bpf_map_inc_not_zero(map, true);
2673 map = ERR_PTR(-ENOENT);
2674 spin_unlock_bh(&map_idr_lock);
2677 return PTR_ERR(map);
2679 fd = bpf_map_new_fd(map, f_flags);
2681 bpf_map_put_with_uref(map);
2686 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
2687 unsigned long addr, u32 *off,
2690 const struct bpf_map *map;
2693 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2694 map = prog->aux->used_maps[i];
2695 if (map == (void *)addr) {
2696 *type = BPF_PSEUDO_MAP_FD;
2699 if (!map->ops->map_direct_value_meta)
2701 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2702 *type = BPF_PSEUDO_MAP_VALUE;
2710 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2712 const struct bpf_map *map;
2713 struct bpf_insn *insns;
2718 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2723 for (i = 0; i < prog->len; i++) {
2724 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2725 insns[i].code = BPF_JMP | BPF_CALL;
2726 insns[i].imm = BPF_FUNC_tail_call;
2729 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2730 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2731 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2732 insns[i].code = BPF_JMP | BPF_CALL;
2733 if (!bpf_dump_raw_ok())
2738 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2741 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
2742 map = bpf_map_from_imm(prog, imm, &off, &type);
2744 insns[i].src_reg = type;
2745 insns[i].imm = map->id;
2746 insns[i + 1].imm = off;
2754 static int set_info_rec_size(struct bpf_prog_info *info)
2757 * Ensure info.*_rec_size is the same as kernel expected size
2761 * Only allow zero *_rec_size if both _rec_size and _cnt are
2762 * zero. In this case, the kernel will set the expected
2763 * _rec_size back to the info.
2766 if ((info->nr_func_info || info->func_info_rec_size) &&
2767 info->func_info_rec_size != sizeof(struct bpf_func_info))
2770 if ((info->nr_line_info || info->line_info_rec_size) &&
2771 info->line_info_rec_size != sizeof(struct bpf_line_info))
2774 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
2775 info->jited_line_info_rec_size != sizeof(__u64))
2778 info->func_info_rec_size = sizeof(struct bpf_func_info);
2779 info->line_info_rec_size = sizeof(struct bpf_line_info);
2780 info->jited_line_info_rec_size = sizeof(__u64);
2785 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2786 const union bpf_attr *attr,
2787 union bpf_attr __user *uattr)
2789 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2790 struct bpf_prog_info info = {};
2791 u32 info_len = attr->info.info_len;
2792 struct bpf_prog_stats stats;
2793 char __user *uinsns;
2797 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2800 info_len = min_t(u32, sizeof(info), info_len);
2802 if (copy_from_user(&info, uinfo, info_len))
2805 info.type = prog->type;
2806 info.id = prog->aux->id;
2807 info.load_time = prog->aux->load_time;
2808 info.created_by_uid = from_kuid_munged(current_user_ns(),
2809 prog->aux->user->uid);
2810 info.gpl_compatible = prog->gpl_compatible;
2812 memcpy(info.tag, prog->tag, sizeof(prog->tag));
2813 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2815 ulen = info.nr_map_ids;
2816 info.nr_map_ids = prog->aux->used_map_cnt;
2817 ulen = min_t(u32, info.nr_map_ids, ulen);
2819 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
2822 for (i = 0; i < ulen; i++)
2823 if (put_user(prog->aux->used_maps[i]->id,
2828 err = set_info_rec_size(&info);
2832 bpf_prog_get_stats(prog, &stats);
2833 info.run_time_ns = stats.nsecs;
2834 info.run_cnt = stats.cnt;
2836 if (!capable(CAP_SYS_ADMIN)) {
2837 info.jited_prog_len = 0;
2838 info.xlated_prog_len = 0;
2839 info.nr_jited_ksyms = 0;
2840 info.nr_jited_func_lens = 0;
2841 info.nr_func_info = 0;
2842 info.nr_line_info = 0;
2843 info.nr_jited_line_info = 0;
2847 ulen = info.xlated_prog_len;
2848 info.xlated_prog_len = bpf_prog_insn_size(prog);
2849 if (info.xlated_prog_len && ulen) {
2850 struct bpf_insn *insns_sanitized;
2853 if (prog->blinded && !bpf_dump_raw_ok()) {
2854 info.xlated_prog_insns = 0;
2857 insns_sanitized = bpf_insn_prepare_dump(prog);
2858 if (!insns_sanitized)
2860 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2861 ulen = min_t(u32, info.xlated_prog_len, ulen);
2862 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2863 kfree(insns_sanitized);
2868 if (bpf_prog_is_dev_bound(prog->aux)) {
2869 err = bpf_prog_offload_info_fill(&info, prog);
2875 /* NOTE: the following code is supposed to be skipped for offload.
2876 * bpf_prog_offload_info_fill() is the place to fill similar fields
2879 ulen = info.jited_prog_len;
2880 if (prog->aux->func_cnt) {
2883 info.jited_prog_len = 0;
2884 for (i = 0; i < prog->aux->func_cnt; i++)
2885 info.jited_prog_len += prog->aux->func[i]->jited_len;
2887 info.jited_prog_len = prog->jited_len;
2890 if (info.jited_prog_len && ulen) {
2891 if (bpf_dump_raw_ok()) {
2892 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2893 ulen = min_t(u32, info.jited_prog_len, ulen);
2895 /* for multi-function programs, copy the JITed
2896 * instructions for all the functions
2898 if (prog->aux->func_cnt) {
2903 for (i = 0; i < prog->aux->func_cnt; i++) {
2904 len = prog->aux->func[i]->jited_len;
2905 len = min_t(u32, len, free);
2906 img = (u8 *) prog->aux->func[i]->bpf_func;
2907 if (copy_to_user(uinsns, img, len))
2915 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2919 info.jited_prog_insns = 0;
2923 ulen = info.nr_jited_ksyms;
2924 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
2926 if (bpf_dump_raw_ok()) {
2927 unsigned long ksym_addr;
2928 u64 __user *user_ksyms;
2931 /* copy the address of the kernel symbol
2932 * corresponding to each function
2934 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2935 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2936 if (prog->aux->func_cnt) {
2937 for (i = 0; i < ulen; i++) {
2938 ksym_addr = (unsigned long)
2939 prog->aux->func[i]->bpf_func;
2940 if (put_user((u64) ksym_addr,
2945 ksym_addr = (unsigned long) prog->bpf_func;
2946 if (put_user((u64) ksym_addr, &user_ksyms[0]))
2950 info.jited_ksyms = 0;
2954 ulen = info.nr_jited_func_lens;
2955 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
2957 if (bpf_dump_raw_ok()) {
2958 u32 __user *user_lens;
2961 /* copy the JITed image lengths for each function */
2962 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2963 user_lens = u64_to_user_ptr(info.jited_func_lens);
2964 if (prog->aux->func_cnt) {
2965 for (i = 0; i < ulen; i++) {
2967 prog->aux->func[i]->jited_len;
2968 if (put_user(func_len, &user_lens[i]))
2972 func_len = prog->jited_len;
2973 if (put_user(func_len, &user_lens[0]))
2977 info.jited_func_lens = 0;
2982 info.btf_id = btf_id(prog->aux->btf);
2984 ulen = info.nr_func_info;
2985 info.nr_func_info = prog->aux->func_info_cnt;
2986 if (info.nr_func_info && ulen) {
2987 char __user *user_finfo;
2989 user_finfo = u64_to_user_ptr(info.func_info);
2990 ulen = min_t(u32, info.nr_func_info, ulen);
2991 if (copy_to_user(user_finfo, prog->aux->func_info,
2992 info.func_info_rec_size * ulen))
2996 ulen = info.nr_line_info;
2997 info.nr_line_info = prog->aux->nr_linfo;
2998 if (info.nr_line_info && ulen) {
2999 __u8 __user *user_linfo;
3001 user_linfo = u64_to_user_ptr(info.line_info);
3002 ulen = min_t(u32, info.nr_line_info, ulen);
3003 if (copy_to_user(user_linfo, prog->aux->linfo,
3004 info.line_info_rec_size * ulen))
3008 ulen = info.nr_jited_line_info;
3009 if (prog->aux->jited_linfo)
3010 info.nr_jited_line_info = prog->aux->nr_linfo;
3012 info.nr_jited_line_info = 0;
3013 if (info.nr_jited_line_info && ulen) {
3014 if (bpf_dump_raw_ok()) {
3015 __u64 __user *user_linfo;
3018 user_linfo = u64_to_user_ptr(info.jited_line_info);
3019 ulen = min_t(u32, info.nr_jited_line_info, ulen);
3020 for (i = 0; i < ulen; i++) {
3021 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3026 info.jited_line_info = 0;
3030 ulen = info.nr_prog_tags;
3031 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3033 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3036 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3037 ulen = min_t(u32, info.nr_prog_tags, ulen);
3038 if (prog->aux->func_cnt) {
3039 for (i = 0; i < ulen; i++) {
3040 if (copy_to_user(user_prog_tags[i],
3041 prog->aux->func[i]->tag,
3046 if (copy_to_user(user_prog_tags[0],
3047 prog->tag, BPF_TAG_SIZE))
3053 if (copy_to_user(uinfo, &info, info_len) ||
3054 put_user(info_len, &uattr->info.info_len))
3060 static int bpf_map_get_info_by_fd(struct bpf_map *map,
3061 const union bpf_attr *attr,
3062 union bpf_attr __user *uattr)
3064 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3065 struct bpf_map_info info = {};
3066 u32 info_len = attr->info.info_len;
3069 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3072 info_len = min_t(u32, sizeof(info), info_len);
3074 info.type = map->map_type;
3076 info.key_size = map->key_size;
3077 info.value_size = map->value_size;
3078 info.max_entries = map->max_entries;
3079 info.map_flags = map->map_flags;
3080 memcpy(info.name, map->name, sizeof(map->name));
3083 info.btf_id = btf_id(map->btf);
3084 info.btf_key_type_id = map->btf_key_type_id;
3085 info.btf_value_type_id = map->btf_value_type_id;
3087 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
3089 if (bpf_map_is_dev_bound(map)) {
3090 err = bpf_map_offload_info_fill(&info, map);
3095 if (copy_to_user(uinfo, &info, info_len) ||
3096 put_user(info_len, &uattr->info.info_len))
3102 static int bpf_btf_get_info_by_fd(struct btf *btf,
3103 const union bpf_attr *attr,
3104 union bpf_attr __user *uattr)
3106 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3107 u32 info_len = attr->info.info_len;
3110 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
3114 return btf_get_info_by_fd(btf, attr, uattr);
3117 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3119 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3120 union bpf_attr __user *uattr)
3122 int ufd = attr->info.bpf_fd;
3126 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3133 if (f.file->f_op == &bpf_prog_fops)
3134 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
3136 else if (f.file->f_op == &bpf_map_fops)
3137 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
3139 else if (f.file->f_op == &btf_fops)
3140 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
3148 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3150 static int bpf_btf_load(const union bpf_attr *attr)
3152 if (CHECK_ATTR(BPF_BTF_LOAD))
3155 if (!capable(CAP_SYS_ADMIN))
3158 return btf_new_fd(attr);
3161 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3163 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3165 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3168 if (!capable(CAP_SYS_ADMIN))
3171 return btf_get_fd_by_id(attr->btf_id);
3174 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3175 union bpf_attr __user *uattr,
3176 u32 prog_id, u32 fd_type,
3177 const char *buf, u64 probe_offset,
3180 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3181 u32 len = buf ? strlen(buf) : 0, input_len;
3184 if (put_user(len, &uattr->task_fd_query.buf_len))
3186 input_len = attr->task_fd_query.buf_len;
3187 if (input_len && ubuf) {
3189 /* nothing to copy, just make ubuf NULL terminated */
3192 if (put_user(zero, ubuf))
3194 } else if (input_len >= len + 1) {
3195 /* ubuf can hold the string with NULL terminator */
3196 if (copy_to_user(ubuf, buf, len + 1))
3199 /* ubuf cannot hold the string with NULL terminator,
3200 * do a partial copy with NULL terminator.
3205 if (copy_to_user(ubuf, buf, input_len - 1))
3207 if (put_user(zero, ubuf + input_len - 1))
3212 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3213 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3214 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3215 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3221 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3223 static int bpf_task_fd_query(const union bpf_attr *attr,
3224 union bpf_attr __user *uattr)
3226 pid_t pid = attr->task_fd_query.pid;
3227 u32 fd = attr->task_fd_query.fd;
3228 const struct perf_event *event;
3229 struct files_struct *files;
3230 struct task_struct *task;
3234 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3237 if (!capable(CAP_SYS_ADMIN))
3240 if (attr->task_fd_query.flags != 0)
3243 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3247 files = get_files_struct(task);
3248 put_task_struct(task);
3253 spin_lock(&files->file_lock);
3254 file = fcheck_files(files, fd);
3259 spin_unlock(&files->file_lock);
3260 put_files_struct(files);
3265 if (file->f_op == &bpf_raw_tp_fops) {
3266 struct bpf_raw_tracepoint *raw_tp = file->private_data;
3267 struct bpf_raw_event_map *btp = raw_tp->btp;
3269 err = bpf_task_fd_query_copy(attr, uattr,
3270 raw_tp->prog->aux->id,
3271 BPF_FD_TYPE_RAW_TRACEPOINT,
3272 btp->tp->name, 0, 0);
3276 event = perf_get_event(file);
3277 if (!IS_ERR(event)) {
3278 u64 probe_offset, probe_addr;
3279 u32 prog_id, fd_type;
3282 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3283 &buf, &probe_offset,
3286 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3300 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
3302 #define BPF_DO_BATCH(fn) \
3308 err = fn(map, attr, uattr); \
3311 static int bpf_map_do_batch(const union bpf_attr *attr,
3312 union bpf_attr __user *uattr,
3315 struct bpf_map *map;
3319 if (CHECK_ATTR(BPF_MAP_BATCH))
3322 ufd = attr->batch.map_fd;
3324 map = __bpf_map_get(f);
3326 return PTR_ERR(map);
3328 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
3329 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
3330 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
3335 if (cmd != BPF_MAP_LOOKUP_BATCH &&
3336 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
3341 if (cmd == BPF_MAP_LOOKUP_BATCH)
3342 BPF_DO_BATCH(map->ops->map_lookup_batch);
3343 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
3344 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
3345 else if (cmd == BPF_MAP_UPDATE_BATCH)
3346 BPF_DO_BATCH(map->ops->map_update_batch);
3348 BPF_DO_BATCH(map->ops->map_delete_batch);
3355 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
3357 union bpf_attr attr = {};
3360 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
3363 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
3366 size = min_t(u32, size, sizeof(attr));
3368 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
3369 if (copy_from_user(&attr, uattr, size) != 0)
3372 err = security_bpf(cmd, &attr, size);
3377 case BPF_MAP_CREATE:
3378 err = map_create(&attr);
3380 case BPF_MAP_LOOKUP_ELEM:
3381 err = map_lookup_elem(&attr);
3383 case BPF_MAP_UPDATE_ELEM:
3384 err = map_update_elem(&attr);
3386 case BPF_MAP_DELETE_ELEM:
3387 err = map_delete_elem(&attr);
3389 case BPF_MAP_GET_NEXT_KEY:
3390 err = map_get_next_key(&attr);
3392 case BPF_MAP_FREEZE:
3393 err = map_freeze(&attr);
3396 err = bpf_prog_load(&attr, uattr);
3399 err = bpf_obj_pin(&attr);
3402 err = bpf_obj_get(&attr);
3404 case BPF_PROG_ATTACH:
3405 err = bpf_prog_attach(&attr);
3407 case BPF_PROG_DETACH:
3408 err = bpf_prog_detach(&attr);
3410 case BPF_PROG_QUERY:
3411 err = bpf_prog_query(&attr, uattr);
3413 case BPF_PROG_TEST_RUN:
3414 err = bpf_prog_test_run(&attr, uattr);
3416 case BPF_PROG_GET_NEXT_ID:
3417 err = bpf_obj_get_next_id(&attr, uattr,
3418 &prog_idr, &prog_idr_lock);
3420 case BPF_MAP_GET_NEXT_ID:
3421 err = bpf_obj_get_next_id(&attr, uattr,
3422 &map_idr, &map_idr_lock);
3424 case BPF_BTF_GET_NEXT_ID:
3425 err = bpf_obj_get_next_id(&attr, uattr,
3426 &btf_idr, &btf_idr_lock);
3428 case BPF_PROG_GET_FD_BY_ID:
3429 err = bpf_prog_get_fd_by_id(&attr);
3431 case BPF_MAP_GET_FD_BY_ID:
3432 err = bpf_map_get_fd_by_id(&attr);
3434 case BPF_OBJ_GET_INFO_BY_FD:
3435 err = bpf_obj_get_info_by_fd(&attr, uattr);
3437 case BPF_RAW_TRACEPOINT_OPEN:
3438 err = bpf_raw_tracepoint_open(&attr);
3441 err = bpf_btf_load(&attr);
3443 case BPF_BTF_GET_FD_BY_ID:
3444 err = bpf_btf_get_fd_by_id(&attr);
3446 case BPF_TASK_FD_QUERY:
3447 err = bpf_task_fd_query(&attr, uattr);
3449 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
3450 err = map_lookup_and_delete_elem(&attr);
3452 case BPF_MAP_LOOKUP_BATCH:
3453 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
3455 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
3456 err = bpf_map_do_batch(&attr, uattr,
3457 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
3459 case BPF_MAP_UPDATE_BATCH:
3460 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
3462 case BPF_MAP_DELETE_BATCH:
3463 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);