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>
28 #include <linux/pgtable.h>
29 #include <linux/bpf_lsm.h>
30 #include <linux/poll.h>
31 #include <linux/bpf-netns.h>
33 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
34 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
35 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
36 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
37 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
38 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
41 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
43 DEFINE_PER_CPU(int, bpf_prog_active);
44 static DEFINE_IDR(prog_idr);
45 static DEFINE_SPINLOCK(prog_idr_lock);
46 static DEFINE_IDR(map_idr);
47 static DEFINE_SPINLOCK(map_idr_lock);
48 static DEFINE_IDR(link_idr);
49 static DEFINE_SPINLOCK(link_idr_lock);
51 int sysctl_unprivileged_bpf_disabled __read_mostly;
53 static const struct bpf_map_ops * const bpf_map_types[] = {
54 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
55 #define BPF_MAP_TYPE(_id, _ops) \
57 #define BPF_LINK_TYPE(_id, _name)
58 #include <linux/bpf_types.h>
65 * If we're handed a bigger struct than we know of, ensure all the unknown bits
66 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
67 * we don't know about yet.
69 * There is a ToCToU between this function call and the following
70 * copy_from_user() call. However, this is not a concern since this function is
71 * meant to be a future-proofing of bits.
73 int bpf_check_uarg_tail_zero(void __user *uaddr,
77 unsigned char __user *addr = uaddr + expected_size;
80 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
83 if (actual_size <= expected_size)
86 res = check_zeroed_user(addr, actual_size - expected_size);
89 return res ? 0 : -E2BIG;
92 const struct bpf_map_ops bpf_map_offload_ops = {
93 .map_alloc = bpf_map_offload_map_alloc,
94 .map_free = bpf_map_offload_map_free,
95 .map_check_btf = map_check_no_btf,
98 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
100 const struct bpf_map_ops *ops;
101 u32 type = attr->map_type;
105 if (type >= ARRAY_SIZE(bpf_map_types))
106 return ERR_PTR(-EINVAL);
107 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
108 ops = bpf_map_types[type];
110 return ERR_PTR(-EINVAL);
112 if (ops->map_alloc_check) {
113 err = ops->map_alloc_check(attr);
117 if (attr->map_ifindex)
118 ops = &bpf_map_offload_ops;
119 map = ops->map_alloc(attr);
123 map->map_type = type;
127 static u32 bpf_map_value_size(struct bpf_map *map)
129 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
130 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
131 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
132 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
133 return round_up(map->value_size, 8) * num_possible_cpus();
134 else if (IS_FD_MAP(map))
137 return map->value_size;
140 static void maybe_wait_bpf_programs(struct bpf_map *map)
142 /* Wait for any running BPF programs to complete so that
143 * userspace, when we return to it, knows that all programs
144 * that could be running use the new map value.
146 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
147 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
151 static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
152 void *value, __u64 flags)
156 /* Need to create a kthread, thus must support schedule */
157 if (bpf_map_is_dev_bound(map)) {
158 return bpf_map_offload_update_elem(map, key, value, flags);
159 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
160 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
161 map->map_type == BPF_MAP_TYPE_SOCKMAP ||
162 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
163 return map->ops->map_update_elem(map, key, value, flags);
164 } else if (IS_FD_PROG_ARRAY(map)) {
165 return bpf_fd_array_map_update_elem(map, f.file, key, value,
169 bpf_disable_instrumentation();
170 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
171 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
172 err = bpf_percpu_hash_update(map, key, value, flags);
173 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
174 err = bpf_percpu_array_update(map, key, value, flags);
175 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
176 err = bpf_percpu_cgroup_storage_update(map, key, value,
178 } else if (IS_FD_ARRAY(map)) {
180 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
183 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
185 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
188 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
189 /* rcu_read_lock() is not needed */
190 err = bpf_fd_reuseport_array_update_elem(map, key, value,
192 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
193 map->map_type == BPF_MAP_TYPE_STACK) {
194 err = map->ops->map_push_elem(map, value, flags);
197 err = map->ops->map_update_elem(map, key, value, flags);
200 bpf_enable_instrumentation();
201 maybe_wait_bpf_programs(map);
206 static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
212 if (bpf_map_is_dev_bound(map))
213 return bpf_map_offload_lookup_elem(map, key, value);
215 bpf_disable_instrumentation();
216 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
217 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
218 err = bpf_percpu_hash_copy(map, key, value);
219 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
220 err = bpf_percpu_array_copy(map, key, value);
221 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
222 err = bpf_percpu_cgroup_storage_copy(map, key, value);
223 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
224 err = bpf_stackmap_copy(map, key, value);
225 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
226 err = bpf_fd_array_map_lookup_elem(map, key, value);
227 } else if (IS_FD_HASH(map)) {
228 err = bpf_fd_htab_map_lookup_elem(map, key, value);
229 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
230 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
231 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
232 map->map_type == BPF_MAP_TYPE_STACK) {
233 err = map->ops->map_peek_elem(map, value);
234 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
235 /* struct_ops map requires directly updating "value" */
236 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
239 if (map->ops->map_lookup_elem_sys_only)
240 ptr = map->ops->map_lookup_elem_sys_only(map, key);
242 ptr = map->ops->map_lookup_elem(map, key);
249 if (flags & BPF_F_LOCK)
250 /* lock 'ptr' and copy everything but lock */
251 copy_map_value_locked(map, value, ptr, true);
253 copy_map_value(map, value, ptr);
254 /* mask lock, since value wasn't zero inited */
255 check_and_init_map_lock(map, value);
260 bpf_enable_instrumentation();
261 maybe_wait_bpf_programs(map);
266 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
268 /* We really just want to fail instead of triggering OOM killer
269 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
270 * which is used for lower order allocation requests.
272 * It has been observed that higher order allocation requests done by
273 * vmalloc with __GFP_NORETRY being set might fail due to not trying
274 * to reclaim memory from the page cache, thus we set
275 * __GFP_RETRY_MAYFAIL to avoid such situations.
278 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO;
279 unsigned int flags = 0;
280 unsigned long align = 1;
283 if (size >= SIZE_MAX)
286 /* kmalloc()'ed memory can't be mmap()'ed */
288 BUG_ON(!PAGE_ALIGNED(size));
291 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
292 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
298 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
299 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
300 flags, numa_node, __builtin_return_address(0));
303 void *bpf_map_area_alloc(u64 size, int numa_node)
305 return __bpf_map_area_alloc(size, numa_node, false);
308 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
310 return __bpf_map_area_alloc(size, numa_node, true);
313 void bpf_map_area_free(void *area)
318 static u32 bpf_map_flags_retain_permanent(u32 flags)
320 /* Some map creation flags are not tied to the map object but
321 * rather to the map fd instead, so they have no meaning upon
322 * map object inspection since multiple file descriptors with
323 * different (access) properties can exist here. Thus, given
324 * this has zero meaning for the map itself, lets clear these
327 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
330 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
332 map->map_type = attr->map_type;
333 map->key_size = attr->key_size;
334 map->value_size = attr->value_size;
335 map->max_entries = attr->max_entries;
336 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
337 map->numa_node = bpf_map_attr_numa_node(attr);
340 static int bpf_charge_memlock(struct user_struct *user, u32 pages)
342 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
344 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
345 atomic_long_sub(pages, &user->locked_vm);
351 static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
354 atomic_long_sub(pages, &user->locked_vm);
357 int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
359 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
360 struct user_struct *user;
363 if (size >= U32_MAX - PAGE_SIZE)
366 user = get_current_user();
367 ret = bpf_charge_memlock(user, pages);
379 void bpf_map_charge_finish(struct bpf_map_memory *mem)
381 bpf_uncharge_memlock(mem->user, mem->pages);
385 void bpf_map_charge_move(struct bpf_map_memory *dst,
386 struct bpf_map_memory *src)
390 /* Make sure src will not be used for the redundant uncharging. */
391 memset(src, 0, sizeof(struct bpf_map_memory));
394 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
398 ret = bpf_charge_memlock(map->memory.user, pages);
401 map->memory.pages += pages;
405 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
407 bpf_uncharge_memlock(map->memory.user, pages);
408 map->memory.pages -= pages;
411 static int bpf_map_alloc_id(struct bpf_map *map)
415 idr_preload(GFP_KERNEL);
416 spin_lock_bh(&map_idr_lock);
417 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
420 spin_unlock_bh(&map_idr_lock);
423 if (WARN_ON_ONCE(!id))
426 return id > 0 ? 0 : id;
429 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
433 /* Offloaded maps are removed from the IDR store when their device
434 * disappears - even if someone holds an fd to them they are unusable,
435 * the memory is gone, all ops will fail; they are simply waiting for
436 * refcnt to drop to be freed.
442 spin_lock_irqsave(&map_idr_lock, flags);
444 __acquire(&map_idr_lock);
446 idr_remove(&map_idr, map->id);
450 spin_unlock_irqrestore(&map_idr_lock, flags);
452 __release(&map_idr_lock);
455 /* called from workqueue */
456 static void bpf_map_free_deferred(struct work_struct *work)
458 struct bpf_map *map = container_of(work, struct bpf_map, work);
459 struct bpf_map_memory mem;
461 bpf_map_charge_move(&mem, &map->memory);
462 security_bpf_map_free(map);
463 /* implementation dependent freeing */
464 map->ops->map_free(map);
465 bpf_map_charge_finish(&mem);
468 static void bpf_map_put_uref(struct bpf_map *map)
470 if (atomic64_dec_and_test(&map->usercnt)) {
471 if (map->ops->map_release_uref)
472 map->ops->map_release_uref(map);
476 /* decrement map refcnt and schedule it for freeing via workqueue
477 * (unrelying map implementation ops->map_free() might sleep)
479 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
481 if (atomic64_dec_and_test(&map->refcnt)) {
482 /* bpf_map_free_id() must be called first */
483 bpf_map_free_id(map, do_idr_lock);
485 INIT_WORK(&map->work, bpf_map_free_deferred);
486 schedule_work(&map->work);
490 void bpf_map_put(struct bpf_map *map)
492 __bpf_map_put(map, true);
494 EXPORT_SYMBOL_GPL(bpf_map_put);
496 void bpf_map_put_with_uref(struct bpf_map *map)
498 bpf_map_put_uref(map);
502 static int bpf_map_release(struct inode *inode, struct file *filp)
504 struct bpf_map *map = filp->private_data;
506 if (map->ops->map_release)
507 map->ops->map_release(map, filp);
509 bpf_map_put_with_uref(map);
513 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
515 fmode_t mode = f.file->f_mode;
517 /* Our file permissions may have been overridden by global
518 * map permissions facing syscall side.
520 if (READ_ONCE(map->frozen))
521 mode &= ~FMODE_CAN_WRITE;
525 #ifdef CONFIG_PROC_FS
526 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
528 const struct bpf_map *map = filp->private_data;
529 const struct bpf_array *array;
530 u32 type = 0, jited = 0;
532 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
533 array = container_of(map, struct bpf_array, map);
534 type = array->aux->type;
535 jited = array->aux->jited;
552 map->memory.pages * 1ULL << PAGE_SHIFT,
554 READ_ONCE(map->frozen));
556 seq_printf(m, "owner_prog_type:\t%u\n", type);
557 seq_printf(m, "owner_jited:\t%u\n", jited);
562 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
565 /* We need this handler such that alloc_file() enables
566 * f_mode with FMODE_CAN_READ.
571 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
572 size_t siz, loff_t *ppos)
574 /* We need this handler such that alloc_file() enables
575 * f_mode with FMODE_CAN_WRITE.
580 /* called for any extra memory-mapped regions (except initial) */
581 static void bpf_map_mmap_open(struct vm_area_struct *vma)
583 struct bpf_map *map = vma->vm_file->private_data;
585 if (vma->vm_flags & VM_MAYWRITE) {
586 mutex_lock(&map->freeze_mutex);
588 mutex_unlock(&map->freeze_mutex);
592 /* called for all unmapped memory region (including initial) */
593 static void bpf_map_mmap_close(struct vm_area_struct *vma)
595 struct bpf_map *map = vma->vm_file->private_data;
597 if (vma->vm_flags & VM_MAYWRITE) {
598 mutex_lock(&map->freeze_mutex);
600 mutex_unlock(&map->freeze_mutex);
604 static const struct vm_operations_struct bpf_map_default_vmops = {
605 .open = bpf_map_mmap_open,
606 .close = bpf_map_mmap_close,
609 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
611 struct bpf_map *map = filp->private_data;
614 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
617 if (!(vma->vm_flags & VM_SHARED))
620 mutex_lock(&map->freeze_mutex);
622 if (vma->vm_flags & VM_WRITE) {
627 /* map is meant to be read-only, so do not allow mapping as
628 * writable, because it's possible to leak a writable page
629 * reference and allows user-space to still modify it after
630 * freezing, while verifier will assume contents do not change
632 if (map->map_flags & BPF_F_RDONLY_PROG) {
638 /* set default open/close callbacks */
639 vma->vm_ops = &bpf_map_default_vmops;
640 vma->vm_private_data = map;
641 vma->vm_flags &= ~VM_MAYEXEC;
642 if (!(vma->vm_flags & VM_WRITE))
643 /* disallow re-mapping with PROT_WRITE */
644 vma->vm_flags &= ~VM_MAYWRITE;
646 err = map->ops->map_mmap(map, vma);
650 if (vma->vm_flags & VM_MAYWRITE)
653 mutex_unlock(&map->freeze_mutex);
657 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
659 struct bpf_map *map = filp->private_data;
661 if (map->ops->map_poll)
662 return map->ops->map_poll(map, filp, pts);
667 const struct file_operations bpf_map_fops = {
668 #ifdef CONFIG_PROC_FS
669 .show_fdinfo = bpf_map_show_fdinfo,
671 .release = bpf_map_release,
672 .read = bpf_dummy_read,
673 .write = bpf_dummy_write,
674 .mmap = bpf_map_mmap,
675 .poll = bpf_map_poll,
678 int bpf_map_new_fd(struct bpf_map *map, int flags)
682 ret = security_bpf_map(map, OPEN_FMODE(flags));
686 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
690 int bpf_get_file_flag(int flags)
692 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
694 if (flags & BPF_F_RDONLY)
696 if (flags & BPF_F_WRONLY)
701 /* helper macro to check that unused fields 'union bpf_attr' are zero */
702 #define CHECK_ATTR(CMD) \
703 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
704 sizeof(attr->CMD##_LAST_FIELD), 0, \
706 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
707 sizeof(attr->CMD##_LAST_FIELD)) != NULL
709 /* dst and src must have at least "size" number of bytes.
710 * Return strlen on success and < 0 on error.
712 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
714 const char *end = src + size;
715 const char *orig_src = src;
717 memset(dst, 0, size);
718 /* Copy all isalnum(), '_' and '.' chars. */
719 while (src < end && *src) {
720 if (!isalnum(*src) &&
721 *src != '_' && *src != '.')
726 /* No '\0' found in "size" number of bytes */
730 return src - orig_src;
733 int map_check_no_btf(const struct bpf_map *map,
734 const struct btf *btf,
735 const struct btf_type *key_type,
736 const struct btf_type *value_type)
741 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
742 u32 btf_key_id, u32 btf_value_id)
744 const struct btf_type *key_type, *value_type;
745 u32 key_size, value_size;
748 /* Some maps allow key to be unspecified. */
750 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
751 if (!key_type || key_size != map->key_size)
754 key_type = btf_type_by_id(btf, 0);
755 if (!map->ops->map_check_btf)
759 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
760 if (!value_type || value_size != map->value_size)
763 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
765 if (map_value_has_spin_lock(map)) {
766 if (map->map_flags & BPF_F_RDONLY_PROG)
768 if (map->map_type != BPF_MAP_TYPE_HASH &&
769 map->map_type != BPF_MAP_TYPE_ARRAY &&
770 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
771 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
773 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
776 "verifier bug spin_lock_off %d value_size %d\n",
777 map->spin_lock_off, map->value_size);
782 if (map->ops->map_check_btf)
783 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
788 #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
789 /* called via syscall */
790 static int map_create(union bpf_attr *attr)
792 int numa_node = bpf_map_attr_numa_node(attr);
793 struct bpf_map_memory mem;
798 err = CHECK_ATTR(BPF_MAP_CREATE);
802 if (attr->btf_vmlinux_value_type_id) {
803 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
804 attr->btf_key_type_id || attr->btf_value_type_id)
806 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
810 f_flags = bpf_get_file_flag(attr->map_flags);
814 if (numa_node != NUMA_NO_NODE &&
815 ((unsigned int)numa_node >= nr_node_ids ||
816 !node_online(numa_node)))
819 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
820 map = find_and_alloc_map(attr);
824 err = bpf_obj_name_cpy(map->name, attr->map_name,
825 sizeof(attr->map_name));
829 atomic64_set(&map->refcnt, 1);
830 atomic64_set(&map->usercnt, 1);
831 mutex_init(&map->freeze_mutex);
833 map->spin_lock_off = -EINVAL;
834 if (attr->btf_key_type_id || attr->btf_value_type_id ||
835 /* Even the map's value is a kernel's struct,
836 * the bpf_prog.o must have BTF to begin with
837 * to figure out the corresponding kernel's
838 * counter part. Thus, attr->btf_fd has
841 attr->btf_vmlinux_value_type_id) {
844 btf = btf_get_by_fd(attr->btf_fd);
851 if (attr->btf_value_type_id) {
852 err = map_check_btf(map, btf, attr->btf_key_type_id,
853 attr->btf_value_type_id);
858 map->btf_key_type_id = attr->btf_key_type_id;
859 map->btf_value_type_id = attr->btf_value_type_id;
860 map->btf_vmlinux_value_type_id =
861 attr->btf_vmlinux_value_type_id;
864 err = security_bpf_map_alloc(map);
868 err = bpf_map_alloc_id(map);
872 err = bpf_map_new_fd(map, f_flags);
874 /* failed to allocate fd.
875 * bpf_map_put_with_uref() is needed because the above
876 * bpf_map_alloc_id() has published the map
877 * to the userspace and the userspace may
878 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
880 bpf_map_put_with_uref(map);
887 security_bpf_map_free(map);
890 bpf_map_charge_move(&mem, &map->memory);
891 map->ops->map_free(map);
892 bpf_map_charge_finish(&mem);
896 /* if error is returned, fd is released.
897 * On success caller should complete fd access with matching fdput()
899 struct bpf_map *__bpf_map_get(struct fd f)
902 return ERR_PTR(-EBADF);
903 if (f.file->f_op != &bpf_map_fops) {
905 return ERR_PTR(-EINVAL);
908 return f.file->private_data;
911 void bpf_map_inc(struct bpf_map *map)
913 atomic64_inc(&map->refcnt);
915 EXPORT_SYMBOL_GPL(bpf_map_inc);
917 void bpf_map_inc_with_uref(struct bpf_map *map)
919 atomic64_inc(&map->refcnt);
920 atomic64_inc(&map->usercnt);
922 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
924 struct bpf_map *bpf_map_get(u32 ufd)
926 struct fd f = fdget(ufd);
929 map = __bpf_map_get(f);
939 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
941 struct fd f = fdget(ufd);
944 map = __bpf_map_get(f);
948 bpf_map_inc_with_uref(map);
954 /* map_idr_lock should have been held */
955 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
959 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
961 return ERR_PTR(-ENOENT);
963 atomic64_inc(&map->usercnt);
968 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
970 spin_lock_bh(&map_idr_lock);
971 map = __bpf_map_inc_not_zero(map, false);
972 spin_unlock_bh(&map_idr_lock);
976 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
978 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
983 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
986 return memdup_user(ukey, key_size);
989 return ERR_PTR(-EINVAL);
994 /* last field in 'union bpf_attr' used by this command */
995 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
997 static int map_lookup_elem(union bpf_attr *attr)
999 void __user *ukey = u64_to_user_ptr(attr->key);
1000 void __user *uvalue = u64_to_user_ptr(attr->value);
1001 int ufd = attr->map_fd;
1002 struct bpf_map *map;
1008 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1011 if (attr->flags & ~BPF_F_LOCK)
1015 map = __bpf_map_get(f);
1017 return PTR_ERR(map);
1018 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1023 if ((attr->flags & BPF_F_LOCK) &&
1024 !map_value_has_spin_lock(map)) {
1029 key = __bpf_copy_key(ukey, map->key_size);
1035 value_size = bpf_map_value_size(map);
1038 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1042 err = bpf_map_copy_value(map, key, value, attr->flags);
1047 if (copy_to_user(uvalue, value, value_size) != 0)
1062 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1064 static int map_update_elem(union bpf_attr *attr)
1066 void __user *ukey = u64_to_user_ptr(attr->key);
1067 void __user *uvalue = u64_to_user_ptr(attr->value);
1068 int ufd = attr->map_fd;
1069 struct bpf_map *map;
1075 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1079 map = __bpf_map_get(f);
1081 return PTR_ERR(map);
1082 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1087 if ((attr->flags & BPF_F_LOCK) &&
1088 !map_value_has_spin_lock(map)) {
1093 key = __bpf_copy_key(ukey, map->key_size);
1099 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1100 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
1101 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1102 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
1103 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1105 value_size = map->value_size;
1108 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1113 if (copy_from_user(value, uvalue, value_size) != 0)
1116 err = bpf_map_update_value(map, f, key, value, attr->flags);
1127 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1129 static int map_delete_elem(union bpf_attr *attr)
1131 void __user *ukey = u64_to_user_ptr(attr->key);
1132 int ufd = attr->map_fd;
1133 struct bpf_map *map;
1138 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1142 map = __bpf_map_get(f);
1144 return PTR_ERR(map);
1145 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1150 key = __bpf_copy_key(ukey, map->key_size);
1156 if (bpf_map_is_dev_bound(map)) {
1157 err = bpf_map_offload_delete_elem(map, key);
1159 } else if (IS_FD_PROG_ARRAY(map) ||
1160 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1161 /* These maps require sleepable context */
1162 err = map->ops->map_delete_elem(map, key);
1166 bpf_disable_instrumentation();
1168 err = map->ops->map_delete_elem(map, key);
1170 bpf_enable_instrumentation();
1171 maybe_wait_bpf_programs(map);
1179 /* last field in 'union bpf_attr' used by this command */
1180 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1182 static int map_get_next_key(union bpf_attr *attr)
1184 void __user *ukey = u64_to_user_ptr(attr->key);
1185 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1186 int ufd = attr->map_fd;
1187 struct bpf_map *map;
1188 void *key, *next_key;
1192 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1196 map = __bpf_map_get(f);
1198 return PTR_ERR(map);
1199 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1205 key = __bpf_copy_key(ukey, map->key_size);
1215 next_key = kmalloc(map->key_size, GFP_USER);
1219 if (bpf_map_is_dev_bound(map)) {
1220 err = bpf_map_offload_get_next_key(map, key, next_key);
1225 err = map->ops->map_get_next_key(map, key, next_key);
1232 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1246 int generic_map_delete_batch(struct bpf_map *map,
1247 const union bpf_attr *attr,
1248 union bpf_attr __user *uattr)
1250 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1255 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1258 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1259 !map_value_has_spin_lock(map)) {
1263 max_count = attr->batch.count;
1267 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1271 for (cp = 0; cp < max_count; cp++) {
1273 if (copy_from_user(key, keys + cp * map->key_size,
1277 if (bpf_map_is_dev_bound(map)) {
1278 err = bpf_map_offload_delete_elem(map, key);
1282 bpf_disable_instrumentation();
1284 err = map->ops->map_delete_elem(map, key);
1286 bpf_enable_instrumentation();
1287 maybe_wait_bpf_programs(map);
1291 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1298 int generic_map_update_batch(struct bpf_map *map,
1299 const union bpf_attr *attr,
1300 union bpf_attr __user *uattr)
1302 void __user *values = u64_to_user_ptr(attr->batch.values);
1303 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1304 u32 value_size, cp, max_count;
1305 int ufd = attr->map_fd;
1311 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1314 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1315 !map_value_has_spin_lock(map)) {
1319 value_size = bpf_map_value_size(map);
1321 max_count = attr->batch.count;
1325 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1329 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1335 for (cp = 0; cp < max_count; cp++) {
1337 if (copy_from_user(key, keys + cp * map->key_size,
1339 copy_from_user(value, values + cp * value_size, value_size))
1342 err = bpf_map_update_value(map, f, key, value,
1343 attr->batch.elem_flags);
1349 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1357 #define MAP_LOOKUP_RETRIES 3
1359 int generic_map_lookup_batch(struct bpf_map *map,
1360 const union bpf_attr *attr,
1361 union bpf_attr __user *uattr)
1363 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1364 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1365 void __user *values = u64_to_user_ptr(attr->batch.values);
1366 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1367 void *buf, *buf_prevkey, *prev_key, *key, *value;
1368 int err, retry = MAP_LOOKUP_RETRIES;
1369 u32 value_size, cp, max_count;
1371 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1374 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1375 !map_value_has_spin_lock(map))
1378 value_size = bpf_map_value_size(map);
1380 max_count = attr->batch.count;
1384 if (put_user(0, &uattr->batch.count))
1387 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1391 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1399 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1402 value = key + map->key_size;
1404 prev_key = buf_prevkey;
1406 for (cp = 0; cp < max_count;) {
1408 err = map->ops->map_get_next_key(map, prev_key, key);
1412 err = bpf_map_copy_value(map, key, value,
1413 attr->batch.elem_flags);
1415 if (err == -ENOENT) {
1427 if (copy_to_user(keys + cp * map->key_size, key,
1432 if (copy_to_user(values + cp * value_size, value, value_size)) {
1438 prev_key = buf_prevkey;
1440 swap(prev_key, key);
1441 retry = MAP_LOOKUP_RETRIES;
1448 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1449 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1458 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1460 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1462 void __user *ukey = u64_to_user_ptr(attr->key);
1463 void __user *uvalue = u64_to_user_ptr(attr->value);
1464 int ufd = attr->map_fd;
1465 struct bpf_map *map;
1471 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1475 map = __bpf_map_get(f);
1477 return PTR_ERR(map);
1478 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1479 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1484 key = __bpf_copy_key(ukey, map->key_size);
1490 value_size = map->value_size;
1493 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1497 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1498 map->map_type == BPF_MAP_TYPE_STACK) {
1499 err = map->ops->map_pop_elem(map, value);
1507 if (copy_to_user(uvalue, value, value_size) != 0) {
1523 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1525 static int map_freeze(const union bpf_attr *attr)
1527 int err = 0, ufd = attr->map_fd;
1528 struct bpf_map *map;
1531 if (CHECK_ATTR(BPF_MAP_FREEZE))
1535 map = __bpf_map_get(f);
1537 return PTR_ERR(map);
1539 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1544 mutex_lock(&map->freeze_mutex);
1546 if (map->writecnt) {
1550 if (READ_ONCE(map->frozen)) {
1554 if (!bpf_capable()) {
1559 WRITE_ONCE(map->frozen, true);
1561 mutex_unlock(&map->freeze_mutex);
1566 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1567 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1568 [_id] = & _name ## _prog_ops,
1569 #define BPF_MAP_TYPE(_id, _ops)
1570 #define BPF_LINK_TYPE(_id, _name)
1571 #include <linux/bpf_types.h>
1572 #undef BPF_PROG_TYPE
1574 #undef BPF_LINK_TYPE
1577 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1579 const struct bpf_prog_ops *ops;
1581 if (type >= ARRAY_SIZE(bpf_prog_types))
1583 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1584 ops = bpf_prog_types[type];
1588 if (!bpf_prog_is_dev_bound(prog->aux))
1589 prog->aux->ops = ops;
1591 prog->aux->ops = &bpf_offload_prog_ops;
1602 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1603 [BPF_AUDIT_LOAD] = "LOAD",
1604 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1607 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1609 struct audit_context *ctx = NULL;
1610 struct audit_buffer *ab;
1612 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1614 if (audit_enabled == AUDIT_OFF)
1616 if (op == BPF_AUDIT_LOAD)
1617 ctx = audit_context();
1618 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1621 audit_log_format(ab, "prog-id=%u op=%s",
1622 prog->aux->id, bpf_audit_str[op]);
1626 int __bpf_prog_charge(struct user_struct *user, u32 pages)
1628 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1629 unsigned long user_bufs;
1632 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1633 if (user_bufs > memlock_limit) {
1634 atomic_long_sub(pages, &user->locked_vm);
1642 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1645 atomic_long_sub(pages, &user->locked_vm);
1648 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1650 struct user_struct *user = get_current_user();
1653 ret = __bpf_prog_charge(user, prog->pages);
1659 prog->aux->user = user;
1663 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1665 struct user_struct *user = prog->aux->user;
1667 __bpf_prog_uncharge(user, prog->pages);
1671 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1675 idr_preload(GFP_KERNEL);
1676 spin_lock_bh(&prog_idr_lock);
1677 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1680 spin_unlock_bh(&prog_idr_lock);
1683 /* id is in [1, INT_MAX) */
1684 if (WARN_ON_ONCE(!id))
1687 return id > 0 ? 0 : id;
1690 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1692 /* cBPF to eBPF migrations are currently not in the idr store.
1693 * Offloaded programs are removed from the store when their device
1694 * disappears - even if someone grabs an fd to them they are unusable,
1695 * simply waiting for refcnt to drop to be freed.
1701 spin_lock_bh(&prog_idr_lock);
1703 __acquire(&prog_idr_lock);
1705 idr_remove(&prog_idr, prog->aux->id);
1709 spin_unlock_bh(&prog_idr_lock);
1711 __release(&prog_idr_lock);
1714 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1716 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1718 kvfree(aux->func_info);
1719 kfree(aux->func_info_aux);
1720 bpf_prog_uncharge_memlock(aux->prog);
1721 security_bpf_prog_free(aux);
1722 bpf_prog_free(aux->prog);
1725 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1727 bpf_prog_kallsyms_del_all(prog);
1728 btf_put(prog->aux->btf);
1729 bpf_prog_free_linfo(prog);
1732 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1734 __bpf_prog_put_rcu(&prog->aux->rcu);
1737 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1739 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
1740 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1741 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1742 /* bpf_prog_free_id() must be called first */
1743 bpf_prog_free_id(prog, do_idr_lock);
1744 __bpf_prog_put_noref(prog, true);
1748 void bpf_prog_put(struct bpf_prog *prog)
1750 __bpf_prog_put(prog, true);
1752 EXPORT_SYMBOL_GPL(bpf_prog_put);
1754 static int bpf_prog_release(struct inode *inode, struct file *filp)
1756 struct bpf_prog *prog = filp->private_data;
1762 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1763 struct bpf_prog_stats *stats)
1765 u64 nsecs = 0, cnt = 0;
1768 for_each_possible_cpu(cpu) {
1769 const struct bpf_prog_stats *st;
1773 st = per_cpu_ptr(prog->aux->stats, cpu);
1775 start = u64_stats_fetch_begin_irq(&st->syncp);
1778 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1782 stats->nsecs = nsecs;
1786 #ifdef CONFIG_PROC_FS
1787 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1789 const struct bpf_prog *prog = filp->private_data;
1790 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1791 struct bpf_prog_stats stats;
1793 bpf_prog_get_stats(prog, &stats);
1794 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1801 "run_time_ns:\t%llu\n"
1806 prog->pages * 1ULL << PAGE_SHIFT,
1813 const struct file_operations bpf_prog_fops = {
1814 #ifdef CONFIG_PROC_FS
1815 .show_fdinfo = bpf_prog_show_fdinfo,
1817 .release = bpf_prog_release,
1818 .read = bpf_dummy_read,
1819 .write = bpf_dummy_write,
1822 int bpf_prog_new_fd(struct bpf_prog *prog)
1826 ret = security_bpf_prog(prog);
1830 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1831 O_RDWR | O_CLOEXEC);
1834 static struct bpf_prog *____bpf_prog_get(struct fd f)
1837 return ERR_PTR(-EBADF);
1838 if (f.file->f_op != &bpf_prog_fops) {
1840 return ERR_PTR(-EINVAL);
1843 return f.file->private_data;
1846 void bpf_prog_add(struct bpf_prog *prog, int i)
1848 atomic64_add(i, &prog->aux->refcnt);
1850 EXPORT_SYMBOL_GPL(bpf_prog_add);
1852 void bpf_prog_sub(struct bpf_prog *prog, int i)
1854 /* Only to be used for undoing previous bpf_prog_add() in some
1855 * error path. We still know that another entity in our call
1856 * path holds a reference to the program, thus atomic_sub() can
1857 * be safely used in such cases!
1859 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1861 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1863 void bpf_prog_inc(struct bpf_prog *prog)
1865 atomic64_inc(&prog->aux->refcnt);
1867 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1869 /* prog_idr_lock should have been held */
1870 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1874 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1877 return ERR_PTR(-ENOENT);
1881 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1883 bool bpf_prog_get_ok(struct bpf_prog *prog,
1884 enum bpf_prog_type *attach_type, bool attach_drv)
1886 /* not an attachment, just a refcount inc, always allow */
1890 if (prog->type != *attach_type)
1892 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1898 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1901 struct fd f = fdget(ufd);
1902 struct bpf_prog *prog;
1904 prog = ____bpf_prog_get(f);
1907 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1908 prog = ERR_PTR(-EINVAL);
1918 struct bpf_prog *bpf_prog_get(u32 ufd)
1920 return __bpf_prog_get(ufd, NULL, false);
1923 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1926 return __bpf_prog_get(ufd, &type, attach_drv);
1928 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1930 /* Initially all BPF programs could be loaded w/o specifying
1931 * expected_attach_type. Later for some of them specifying expected_attach_type
1932 * at load time became required so that program could be validated properly.
1933 * Programs of types that are allowed to be loaded both w/ and w/o (for
1934 * backward compatibility) expected_attach_type, should have the default attach
1935 * type assigned to expected_attach_type for the latter case, so that it can be
1936 * validated later at attach time.
1938 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1939 * prog type requires it but has some attach types that have to be backward
1942 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1944 switch (attr->prog_type) {
1945 case BPF_PROG_TYPE_CGROUP_SOCK:
1946 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1947 * exist so checking for non-zero is the way to go here.
1949 if (!attr->expected_attach_type)
1950 attr->expected_attach_type =
1951 BPF_CGROUP_INET_SOCK_CREATE;
1957 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1958 enum bpf_attach_type expected_attach_type,
1959 u32 btf_id, u32 prog_fd)
1962 if (btf_id > BTF_MAX_TYPE)
1965 switch (prog_type) {
1966 case BPF_PROG_TYPE_TRACING:
1967 case BPF_PROG_TYPE_LSM:
1968 case BPF_PROG_TYPE_STRUCT_OPS:
1969 case BPF_PROG_TYPE_EXT:
1976 if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING &&
1977 prog_type != BPF_PROG_TYPE_EXT)
1980 switch (prog_type) {
1981 case BPF_PROG_TYPE_CGROUP_SOCK:
1982 switch (expected_attach_type) {
1983 case BPF_CGROUP_INET_SOCK_CREATE:
1984 case BPF_CGROUP_INET_SOCK_RELEASE:
1985 case BPF_CGROUP_INET4_POST_BIND:
1986 case BPF_CGROUP_INET6_POST_BIND:
1991 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1992 switch (expected_attach_type) {
1993 case BPF_CGROUP_INET4_BIND:
1994 case BPF_CGROUP_INET6_BIND:
1995 case BPF_CGROUP_INET4_CONNECT:
1996 case BPF_CGROUP_INET6_CONNECT:
1997 case BPF_CGROUP_INET4_GETPEERNAME:
1998 case BPF_CGROUP_INET6_GETPEERNAME:
1999 case BPF_CGROUP_INET4_GETSOCKNAME:
2000 case BPF_CGROUP_INET6_GETSOCKNAME:
2001 case BPF_CGROUP_UDP4_SENDMSG:
2002 case BPF_CGROUP_UDP6_SENDMSG:
2003 case BPF_CGROUP_UDP4_RECVMSG:
2004 case BPF_CGROUP_UDP6_RECVMSG:
2009 case BPF_PROG_TYPE_CGROUP_SKB:
2010 switch (expected_attach_type) {
2011 case BPF_CGROUP_INET_INGRESS:
2012 case BPF_CGROUP_INET_EGRESS:
2017 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2018 switch (expected_attach_type) {
2019 case BPF_CGROUP_SETSOCKOPT:
2020 case BPF_CGROUP_GETSOCKOPT:
2025 case BPF_PROG_TYPE_SK_LOOKUP:
2026 if (expected_attach_type == BPF_SK_LOOKUP)
2029 case BPF_PROG_TYPE_EXT:
2030 if (expected_attach_type)
2038 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2040 switch (prog_type) {
2041 case BPF_PROG_TYPE_SCHED_CLS:
2042 case BPF_PROG_TYPE_SCHED_ACT:
2043 case BPF_PROG_TYPE_XDP:
2044 case BPF_PROG_TYPE_LWT_IN:
2045 case BPF_PROG_TYPE_LWT_OUT:
2046 case BPF_PROG_TYPE_LWT_XMIT:
2047 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2048 case BPF_PROG_TYPE_SK_SKB:
2049 case BPF_PROG_TYPE_SK_MSG:
2050 case BPF_PROG_TYPE_LIRC_MODE2:
2051 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2052 case BPF_PROG_TYPE_CGROUP_DEVICE:
2053 case BPF_PROG_TYPE_CGROUP_SOCK:
2054 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2055 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2056 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2057 case BPF_PROG_TYPE_SOCK_OPS:
2058 case BPF_PROG_TYPE_EXT: /* extends any prog */
2060 case BPF_PROG_TYPE_CGROUP_SKB:
2062 case BPF_PROG_TYPE_SK_REUSEPORT:
2063 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2069 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2071 switch (prog_type) {
2072 case BPF_PROG_TYPE_KPROBE:
2073 case BPF_PROG_TYPE_TRACEPOINT:
2074 case BPF_PROG_TYPE_PERF_EVENT:
2075 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2076 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2077 case BPF_PROG_TYPE_TRACING:
2078 case BPF_PROG_TYPE_LSM:
2079 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2080 case BPF_PROG_TYPE_EXT: /* extends any prog */
2087 /* last field in 'union bpf_attr' used by this command */
2088 #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
2090 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
2092 enum bpf_prog_type type = attr->prog_type;
2093 struct bpf_prog *prog;
2098 if (CHECK_ATTR(BPF_PROG_LOAD))
2101 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2102 BPF_F_ANY_ALIGNMENT |
2103 BPF_F_TEST_STATE_FREQ |
2104 BPF_F_TEST_RND_HI32))
2107 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2108 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2112 /* copy eBPF program license from user space */
2113 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
2114 sizeof(license) - 1) < 0)
2116 license[sizeof(license) - 1] = 0;
2118 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2119 is_gpl = license_is_gpl_compatible(license);
2121 if (attr->insn_cnt == 0 ||
2122 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2124 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2125 type != BPF_PROG_TYPE_CGROUP_SKB &&
2129 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2131 if (is_perfmon_prog_type(type) && !perfmon_capable())
2134 bpf_prog_load_fixup_attach_type(attr);
2135 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2136 attr->attach_btf_id,
2137 attr->attach_prog_fd))
2140 /* plain bpf_prog allocation */
2141 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2145 prog->expected_attach_type = attr->expected_attach_type;
2146 prog->aux->attach_btf_id = attr->attach_btf_id;
2147 if (attr->attach_prog_fd) {
2148 struct bpf_prog *tgt_prog;
2150 tgt_prog = bpf_prog_get(attr->attach_prog_fd);
2151 if (IS_ERR(tgt_prog)) {
2152 err = PTR_ERR(tgt_prog);
2153 goto free_prog_nouncharge;
2155 prog->aux->linked_prog = tgt_prog;
2158 prog->aux->offload_requested = !!attr->prog_ifindex;
2160 err = security_bpf_prog_alloc(prog->aux);
2162 goto free_prog_nouncharge;
2164 err = bpf_prog_charge_memlock(prog);
2168 prog->len = attr->insn_cnt;
2171 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
2172 bpf_prog_insn_size(prog)) != 0)
2175 prog->orig_prog = NULL;
2178 atomic64_set(&prog->aux->refcnt, 1);
2179 prog->gpl_compatible = is_gpl ? 1 : 0;
2181 if (bpf_prog_is_dev_bound(prog->aux)) {
2182 err = bpf_prog_offload_init(prog, attr);
2187 /* find program type: socket_filter vs tracing_filter */
2188 err = find_prog_type(type, prog);
2192 prog->aux->load_time = ktime_get_boottime_ns();
2193 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2194 sizeof(attr->prog_name));
2198 /* run eBPF verifier */
2199 err = bpf_check(&prog, attr, uattr);
2201 goto free_used_maps;
2203 prog = bpf_prog_select_runtime(prog, &err);
2205 goto free_used_maps;
2207 err = bpf_prog_alloc_id(prog);
2209 goto free_used_maps;
2211 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2212 * effectively publicly exposed. However, retrieving via
2213 * bpf_prog_get_fd_by_id() will take another reference,
2214 * therefore it cannot be gone underneath us.
2216 * Only for the time /after/ successful bpf_prog_new_fd()
2217 * and before returning to userspace, we might just hold
2218 * one reference and any parallel close on that fd could
2219 * rip everything out. Hence, below notifications must
2220 * happen before bpf_prog_new_fd().
2222 * Also, any failure handling from this point onwards must
2223 * be using bpf_prog_put() given the program is exposed.
2225 bpf_prog_kallsyms_add(prog);
2226 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2227 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2229 err = bpf_prog_new_fd(prog);
2235 /* In case we have subprogs, we need to wait for a grace
2236 * period before we can tear down JIT memory since symbols
2237 * are already exposed under kallsyms.
2239 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2242 bpf_prog_uncharge_memlock(prog);
2244 security_bpf_prog_free(prog->aux);
2245 free_prog_nouncharge:
2246 bpf_prog_free(prog);
2250 #define BPF_OBJ_LAST_FIELD file_flags
2252 static int bpf_obj_pin(const union bpf_attr *attr)
2254 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2257 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2260 static int bpf_obj_get(const union bpf_attr *attr)
2262 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2263 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2266 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2270 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2271 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2273 atomic64_set(&link->refcnt, 1);
2280 static void bpf_link_free_id(int id)
2285 spin_lock_bh(&link_idr_lock);
2286 idr_remove(&link_idr, id);
2287 spin_unlock_bh(&link_idr_lock);
2290 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2291 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2292 * anon_inode's release() call. This helper marksbpf_link as
2293 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2294 * is not decremented, it's the responsibility of a calling code that failed
2295 * to complete bpf_link initialization.
2297 void bpf_link_cleanup(struct bpf_link_primer *primer)
2299 primer->link->prog = NULL;
2300 bpf_link_free_id(primer->id);
2302 put_unused_fd(primer->fd);
2305 void bpf_link_inc(struct bpf_link *link)
2307 atomic64_inc(&link->refcnt);
2310 /* bpf_link_free is guaranteed to be called from process context */
2311 static void bpf_link_free(struct bpf_link *link)
2313 bpf_link_free_id(link->id);
2315 /* detach BPF program, clean up used resources */
2316 link->ops->release(link);
2317 bpf_prog_put(link->prog);
2319 /* free bpf_link and its containing memory */
2320 link->ops->dealloc(link);
2323 static void bpf_link_put_deferred(struct work_struct *work)
2325 struct bpf_link *link = container_of(work, struct bpf_link, work);
2327 bpf_link_free(link);
2330 /* bpf_link_put can be called from atomic context, but ensures that resources
2331 * are freed from process context
2333 void bpf_link_put(struct bpf_link *link)
2335 if (!atomic64_dec_and_test(&link->refcnt))
2339 INIT_WORK(&link->work, bpf_link_put_deferred);
2340 schedule_work(&link->work);
2342 bpf_link_free(link);
2346 static int bpf_link_release(struct inode *inode, struct file *filp)
2348 struct bpf_link *link = filp->private_data;
2354 #ifdef CONFIG_PROC_FS
2355 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2356 #define BPF_MAP_TYPE(_id, _ops)
2357 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2358 static const char *bpf_link_type_strs[] = {
2359 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2360 #include <linux/bpf_types.h>
2362 #undef BPF_PROG_TYPE
2364 #undef BPF_LINK_TYPE
2366 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2368 const struct bpf_link *link = filp->private_data;
2369 const struct bpf_prog *prog = link->prog;
2370 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2372 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2378 bpf_link_type_strs[link->type],
2382 if (link->ops->show_fdinfo)
2383 link->ops->show_fdinfo(link, m);
2387 static const struct file_operations bpf_link_fops = {
2388 #ifdef CONFIG_PROC_FS
2389 .show_fdinfo = bpf_link_show_fdinfo,
2391 .release = bpf_link_release,
2392 .read = bpf_dummy_read,
2393 .write = bpf_dummy_write,
2396 static int bpf_link_alloc_id(struct bpf_link *link)
2400 idr_preload(GFP_KERNEL);
2401 spin_lock_bh(&link_idr_lock);
2402 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2403 spin_unlock_bh(&link_idr_lock);
2409 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2410 * reserving unused FD and allocating ID from link_idr. This is to be paired
2411 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2412 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2413 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2414 * transient state is passed around in struct bpf_link_primer.
2415 * This is preferred way to create and initialize bpf_link, especially when
2416 * there are complicated and expensive operations inbetween creating bpf_link
2417 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2418 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2419 * expensive (and potentially failing) roll back operations in a rare case
2420 * that file, FD, or ID can't be allocated.
2422 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2427 fd = get_unused_fd_flags(O_CLOEXEC);
2432 id = bpf_link_alloc_id(link);
2438 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2440 bpf_link_free_id(id);
2442 return PTR_ERR(file);
2445 primer->link = link;
2446 primer->file = file;
2452 int bpf_link_settle(struct bpf_link_primer *primer)
2454 /* make bpf_link fetchable by ID */
2455 spin_lock_bh(&link_idr_lock);
2456 primer->link->id = primer->id;
2457 spin_unlock_bh(&link_idr_lock);
2458 /* make bpf_link fetchable by FD */
2459 fd_install(primer->fd, primer->file);
2460 /* pass through installed FD */
2464 int bpf_link_new_fd(struct bpf_link *link)
2466 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2469 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2471 struct fd f = fdget(ufd);
2472 struct bpf_link *link;
2475 return ERR_PTR(-EBADF);
2476 if (f.file->f_op != &bpf_link_fops) {
2478 return ERR_PTR(-EINVAL);
2481 link = f.file->private_data;
2488 struct bpf_tracing_link {
2489 struct bpf_link link;
2490 enum bpf_attach_type attach_type;
2493 static void bpf_tracing_link_release(struct bpf_link *link)
2495 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog));
2498 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2500 struct bpf_tracing_link *tr_link =
2501 container_of(link, struct bpf_tracing_link, link);
2506 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2507 struct seq_file *seq)
2509 struct bpf_tracing_link *tr_link =
2510 container_of(link, struct bpf_tracing_link, link);
2513 "attach_type:\t%d\n",
2514 tr_link->attach_type);
2517 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2518 struct bpf_link_info *info)
2520 struct bpf_tracing_link *tr_link =
2521 container_of(link, struct bpf_tracing_link, link);
2523 info->tracing.attach_type = tr_link->attach_type;
2528 static const struct bpf_link_ops bpf_tracing_link_lops = {
2529 .release = bpf_tracing_link_release,
2530 .dealloc = bpf_tracing_link_dealloc,
2531 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2532 .fill_link_info = bpf_tracing_link_fill_link_info,
2535 static int bpf_tracing_prog_attach(struct bpf_prog *prog)
2537 struct bpf_link_primer link_primer;
2538 struct bpf_tracing_link *link;
2541 switch (prog->type) {
2542 case BPF_PROG_TYPE_TRACING:
2543 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2544 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2545 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2550 case BPF_PROG_TYPE_EXT:
2551 if (prog->expected_attach_type != 0) {
2556 case BPF_PROG_TYPE_LSM:
2557 if (prog->expected_attach_type != BPF_LSM_MAC) {
2567 link = kzalloc(sizeof(*link), GFP_USER);
2572 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2573 &bpf_tracing_link_lops, prog);
2574 link->attach_type = prog->expected_attach_type;
2576 err = bpf_link_prime(&link->link, &link_primer);
2582 err = bpf_trampoline_link_prog(prog);
2584 bpf_link_cleanup(&link_primer);
2588 return bpf_link_settle(&link_primer);
2594 struct bpf_raw_tp_link {
2595 struct bpf_link link;
2596 struct bpf_raw_event_map *btp;
2599 static void bpf_raw_tp_link_release(struct bpf_link *link)
2601 struct bpf_raw_tp_link *raw_tp =
2602 container_of(link, struct bpf_raw_tp_link, link);
2604 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2605 bpf_put_raw_tracepoint(raw_tp->btp);
2608 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2610 struct bpf_raw_tp_link *raw_tp =
2611 container_of(link, struct bpf_raw_tp_link, link);
2616 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2617 struct seq_file *seq)
2619 struct bpf_raw_tp_link *raw_tp_link =
2620 container_of(link, struct bpf_raw_tp_link, link);
2624 raw_tp_link->btp->tp->name);
2627 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2628 struct bpf_link_info *info)
2630 struct bpf_raw_tp_link *raw_tp_link =
2631 container_of(link, struct bpf_raw_tp_link, link);
2632 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2633 const char *tp_name = raw_tp_link->btp->tp->name;
2634 u32 ulen = info->raw_tracepoint.tp_name_len;
2635 size_t tp_len = strlen(tp_name);
2640 info->raw_tracepoint.tp_name_len = tp_len + 1;
2645 if (ulen >= tp_len + 1) {
2646 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2651 if (copy_to_user(ubuf, tp_name, ulen - 1))
2653 if (put_user(zero, ubuf + ulen - 1))
2661 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
2662 .release = bpf_raw_tp_link_release,
2663 .dealloc = bpf_raw_tp_link_dealloc,
2664 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2665 .fill_link_info = bpf_raw_tp_link_fill_link_info,
2668 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2670 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2672 struct bpf_link_primer link_primer;
2673 struct bpf_raw_tp_link *link;
2674 struct bpf_raw_event_map *btp;
2675 struct bpf_prog *prog;
2676 const char *tp_name;
2680 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2683 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2685 return PTR_ERR(prog);
2687 switch (prog->type) {
2688 case BPF_PROG_TYPE_TRACING:
2689 case BPF_PROG_TYPE_EXT:
2690 case BPF_PROG_TYPE_LSM:
2691 if (attr->raw_tracepoint.name) {
2692 /* The attach point for this category of programs
2693 * should be specified via btf_id during program load.
2698 if (prog->type == BPF_PROG_TYPE_TRACING &&
2699 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
2700 tp_name = prog->aux->attach_func_name;
2703 return bpf_tracing_prog_attach(prog);
2704 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2705 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2706 if (strncpy_from_user(buf,
2707 u64_to_user_ptr(attr->raw_tracepoint.name),
2708 sizeof(buf) - 1) < 0) {
2712 buf[sizeof(buf) - 1] = 0;
2720 btp = bpf_get_raw_tracepoint(tp_name);
2726 link = kzalloc(sizeof(*link), GFP_USER);
2731 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2732 &bpf_raw_tp_link_lops, prog);
2735 err = bpf_link_prime(&link->link, &link_primer);
2741 err = bpf_probe_register(link->btp, prog);
2743 bpf_link_cleanup(&link_primer);
2747 return bpf_link_settle(&link_primer);
2750 bpf_put_raw_tracepoint(btp);
2756 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2757 enum bpf_attach_type attach_type)
2759 switch (prog->type) {
2760 case BPF_PROG_TYPE_CGROUP_SOCK:
2761 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2762 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2763 case BPF_PROG_TYPE_SK_LOOKUP:
2764 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
2765 case BPF_PROG_TYPE_CGROUP_SKB:
2766 if (!capable(CAP_NET_ADMIN))
2767 /* cg-skb progs can be loaded by unpriv user.
2768 * check permissions at attach time.
2771 return prog->enforce_expected_attach_type &&
2772 prog->expected_attach_type != attach_type ?
2779 static enum bpf_prog_type
2780 attach_type_to_prog_type(enum bpf_attach_type attach_type)
2782 switch (attach_type) {
2783 case BPF_CGROUP_INET_INGRESS:
2784 case BPF_CGROUP_INET_EGRESS:
2785 return BPF_PROG_TYPE_CGROUP_SKB;
2787 case BPF_CGROUP_INET_SOCK_CREATE:
2788 case BPF_CGROUP_INET_SOCK_RELEASE:
2789 case BPF_CGROUP_INET4_POST_BIND:
2790 case BPF_CGROUP_INET6_POST_BIND:
2791 return BPF_PROG_TYPE_CGROUP_SOCK;
2792 case BPF_CGROUP_INET4_BIND:
2793 case BPF_CGROUP_INET6_BIND:
2794 case BPF_CGROUP_INET4_CONNECT:
2795 case BPF_CGROUP_INET6_CONNECT:
2796 case BPF_CGROUP_INET4_GETPEERNAME:
2797 case BPF_CGROUP_INET6_GETPEERNAME:
2798 case BPF_CGROUP_INET4_GETSOCKNAME:
2799 case BPF_CGROUP_INET6_GETSOCKNAME:
2800 case BPF_CGROUP_UDP4_SENDMSG:
2801 case BPF_CGROUP_UDP6_SENDMSG:
2802 case BPF_CGROUP_UDP4_RECVMSG:
2803 case BPF_CGROUP_UDP6_RECVMSG:
2804 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2805 case BPF_CGROUP_SOCK_OPS:
2806 return BPF_PROG_TYPE_SOCK_OPS;
2807 case BPF_CGROUP_DEVICE:
2808 return BPF_PROG_TYPE_CGROUP_DEVICE;
2809 case BPF_SK_MSG_VERDICT:
2810 return BPF_PROG_TYPE_SK_MSG;
2811 case BPF_SK_SKB_STREAM_PARSER:
2812 case BPF_SK_SKB_STREAM_VERDICT:
2813 return BPF_PROG_TYPE_SK_SKB;
2814 case BPF_LIRC_MODE2:
2815 return BPF_PROG_TYPE_LIRC_MODE2;
2816 case BPF_FLOW_DISSECTOR:
2817 return BPF_PROG_TYPE_FLOW_DISSECTOR;
2818 case BPF_CGROUP_SYSCTL:
2819 return BPF_PROG_TYPE_CGROUP_SYSCTL;
2820 case BPF_CGROUP_GETSOCKOPT:
2821 case BPF_CGROUP_SETSOCKOPT:
2822 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
2823 case BPF_TRACE_ITER:
2824 return BPF_PROG_TYPE_TRACING;
2826 return BPF_PROG_TYPE_SK_LOOKUP;
2828 return BPF_PROG_TYPE_XDP;
2830 return BPF_PROG_TYPE_UNSPEC;
2834 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
2836 #define BPF_F_ATTACH_MASK \
2837 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
2839 static int bpf_prog_attach(const union bpf_attr *attr)
2841 enum bpf_prog_type ptype;
2842 struct bpf_prog *prog;
2845 if (CHECK_ATTR(BPF_PROG_ATTACH))
2848 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
2851 ptype = attach_type_to_prog_type(attr->attach_type);
2852 if (ptype == BPF_PROG_TYPE_UNSPEC)
2855 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2857 return PTR_ERR(prog);
2859 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
2865 case BPF_PROG_TYPE_SK_SKB:
2866 case BPF_PROG_TYPE_SK_MSG:
2867 ret = sock_map_get_from_fd(attr, prog);
2869 case BPF_PROG_TYPE_LIRC_MODE2:
2870 ret = lirc_prog_attach(attr, prog);
2872 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2873 ret = netns_bpf_prog_attach(attr, prog);
2875 case BPF_PROG_TYPE_CGROUP_DEVICE:
2876 case BPF_PROG_TYPE_CGROUP_SKB:
2877 case BPF_PROG_TYPE_CGROUP_SOCK:
2878 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2879 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2880 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2881 case BPF_PROG_TYPE_SOCK_OPS:
2882 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
2893 #define BPF_PROG_DETACH_LAST_FIELD attach_type
2895 static int bpf_prog_detach(const union bpf_attr *attr)
2897 enum bpf_prog_type ptype;
2899 if (CHECK_ATTR(BPF_PROG_DETACH))
2902 ptype = attach_type_to_prog_type(attr->attach_type);
2905 case BPF_PROG_TYPE_SK_MSG:
2906 case BPF_PROG_TYPE_SK_SKB:
2907 return sock_map_prog_detach(attr, ptype);
2908 case BPF_PROG_TYPE_LIRC_MODE2:
2909 return lirc_prog_detach(attr);
2910 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2911 return netns_bpf_prog_detach(attr, ptype);
2912 case BPF_PROG_TYPE_CGROUP_DEVICE:
2913 case BPF_PROG_TYPE_CGROUP_SKB:
2914 case BPF_PROG_TYPE_CGROUP_SOCK:
2915 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2916 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2917 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2918 case BPF_PROG_TYPE_SOCK_OPS:
2919 return cgroup_bpf_prog_detach(attr, ptype);
2925 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2927 static int bpf_prog_query(const union bpf_attr *attr,
2928 union bpf_attr __user *uattr)
2930 if (!capable(CAP_NET_ADMIN))
2932 if (CHECK_ATTR(BPF_PROG_QUERY))
2934 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2937 switch (attr->query.attach_type) {
2938 case BPF_CGROUP_INET_INGRESS:
2939 case BPF_CGROUP_INET_EGRESS:
2940 case BPF_CGROUP_INET_SOCK_CREATE:
2941 case BPF_CGROUP_INET_SOCK_RELEASE:
2942 case BPF_CGROUP_INET4_BIND:
2943 case BPF_CGROUP_INET6_BIND:
2944 case BPF_CGROUP_INET4_POST_BIND:
2945 case BPF_CGROUP_INET6_POST_BIND:
2946 case BPF_CGROUP_INET4_CONNECT:
2947 case BPF_CGROUP_INET6_CONNECT:
2948 case BPF_CGROUP_INET4_GETPEERNAME:
2949 case BPF_CGROUP_INET6_GETPEERNAME:
2950 case BPF_CGROUP_INET4_GETSOCKNAME:
2951 case BPF_CGROUP_INET6_GETSOCKNAME:
2952 case BPF_CGROUP_UDP4_SENDMSG:
2953 case BPF_CGROUP_UDP6_SENDMSG:
2954 case BPF_CGROUP_UDP4_RECVMSG:
2955 case BPF_CGROUP_UDP6_RECVMSG:
2956 case BPF_CGROUP_SOCK_OPS:
2957 case BPF_CGROUP_DEVICE:
2958 case BPF_CGROUP_SYSCTL:
2959 case BPF_CGROUP_GETSOCKOPT:
2960 case BPF_CGROUP_SETSOCKOPT:
2961 return cgroup_bpf_prog_query(attr, uattr);
2962 case BPF_LIRC_MODE2:
2963 return lirc_prog_query(attr, uattr);
2964 case BPF_FLOW_DISSECTOR:
2966 return netns_bpf_prog_query(attr, uattr);
2972 #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
2974 static int bpf_prog_test_run(const union bpf_attr *attr,
2975 union bpf_attr __user *uattr)
2977 struct bpf_prog *prog;
2978 int ret = -ENOTSUPP;
2980 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2983 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2984 (!attr->test.ctx_size_in && attr->test.ctx_in))
2987 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2988 (!attr->test.ctx_size_out && attr->test.ctx_out))
2991 prog = bpf_prog_get(attr->test.prog_fd);
2993 return PTR_ERR(prog);
2995 if (prog->aux->ops->test_run)
2996 ret = prog->aux->ops->test_run(prog, attr, uattr);
3002 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3004 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3005 union bpf_attr __user *uattr,
3009 u32 next_id = attr->start_id;
3012 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3015 if (!capable(CAP_SYS_ADMIN))
3020 if (!idr_get_next(idr, &next_id))
3022 spin_unlock_bh(lock);
3025 err = put_user(next_id, &uattr->next_id);
3030 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3032 struct bpf_map *map;
3034 spin_lock_bh(&map_idr_lock);
3036 map = idr_get_next(&map_idr, id);
3038 map = __bpf_map_inc_not_zero(map, false);
3044 spin_unlock_bh(&map_idr_lock);
3049 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3051 struct bpf_prog *prog;
3053 spin_lock_bh(&prog_idr_lock);
3055 prog = idr_get_next(&prog_idr, id);
3057 prog = bpf_prog_inc_not_zero(prog);
3063 spin_unlock_bh(&prog_idr_lock);
3068 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3070 struct bpf_prog *bpf_prog_by_id(u32 id)
3072 struct bpf_prog *prog;
3075 return ERR_PTR(-ENOENT);
3077 spin_lock_bh(&prog_idr_lock);
3078 prog = idr_find(&prog_idr, id);
3080 prog = bpf_prog_inc_not_zero(prog);
3082 prog = ERR_PTR(-ENOENT);
3083 spin_unlock_bh(&prog_idr_lock);
3087 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3089 struct bpf_prog *prog;
3090 u32 id = attr->prog_id;
3093 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3096 if (!capable(CAP_SYS_ADMIN))
3099 prog = bpf_prog_by_id(id);
3101 return PTR_ERR(prog);
3103 fd = bpf_prog_new_fd(prog);
3110 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3112 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3114 struct bpf_map *map;
3115 u32 id = attr->map_id;
3119 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3120 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3123 if (!capable(CAP_SYS_ADMIN))
3126 f_flags = bpf_get_file_flag(attr->open_flags);
3130 spin_lock_bh(&map_idr_lock);
3131 map = idr_find(&map_idr, id);
3133 map = __bpf_map_inc_not_zero(map, true);
3135 map = ERR_PTR(-ENOENT);
3136 spin_unlock_bh(&map_idr_lock);
3139 return PTR_ERR(map);
3141 fd = bpf_map_new_fd(map, f_flags);
3143 bpf_map_put_with_uref(map);
3148 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3149 unsigned long addr, u32 *off,
3152 const struct bpf_map *map;
3155 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3156 map = prog->aux->used_maps[i];
3157 if (map == (void *)addr) {
3158 *type = BPF_PSEUDO_MAP_FD;
3161 if (!map->ops->map_direct_value_meta)
3163 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3164 *type = BPF_PSEUDO_MAP_VALUE;
3172 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3173 const struct cred *f_cred)
3175 const struct bpf_map *map;
3176 struct bpf_insn *insns;
3182 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3187 for (i = 0; i < prog->len; i++) {
3188 code = insns[i].code;
3190 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3191 insns[i].code = BPF_JMP | BPF_CALL;
3192 insns[i].imm = BPF_FUNC_tail_call;
3195 if (code == (BPF_JMP | BPF_CALL) ||
3196 code == (BPF_JMP | BPF_CALL_ARGS)) {
3197 if (code == (BPF_JMP | BPF_CALL_ARGS))
3198 insns[i].code = BPF_JMP | BPF_CALL;
3199 if (!bpf_dump_raw_ok(f_cred))
3203 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3204 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3208 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3211 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3212 map = bpf_map_from_imm(prog, imm, &off, &type);
3214 insns[i].src_reg = type;
3215 insns[i].imm = map->id;
3216 insns[i + 1].imm = off;
3224 static int set_info_rec_size(struct bpf_prog_info *info)
3227 * Ensure info.*_rec_size is the same as kernel expected size
3231 * Only allow zero *_rec_size if both _rec_size and _cnt are
3232 * zero. In this case, the kernel will set the expected
3233 * _rec_size back to the info.
3236 if ((info->nr_func_info || info->func_info_rec_size) &&
3237 info->func_info_rec_size != sizeof(struct bpf_func_info))
3240 if ((info->nr_line_info || info->line_info_rec_size) &&
3241 info->line_info_rec_size != sizeof(struct bpf_line_info))
3244 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3245 info->jited_line_info_rec_size != sizeof(__u64))
3248 info->func_info_rec_size = sizeof(struct bpf_func_info);
3249 info->line_info_rec_size = sizeof(struct bpf_line_info);
3250 info->jited_line_info_rec_size = sizeof(__u64);
3255 static int bpf_prog_get_info_by_fd(struct file *file,
3256 struct bpf_prog *prog,
3257 const union bpf_attr *attr,
3258 union bpf_attr __user *uattr)
3260 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3261 struct bpf_prog_info info;
3262 u32 info_len = attr->info.info_len;
3263 struct bpf_prog_stats stats;
3264 char __user *uinsns;
3268 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3271 info_len = min_t(u32, sizeof(info), info_len);
3273 memset(&info, 0, sizeof(info));
3274 if (copy_from_user(&info, uinfo, info_len))
3277 info.type = prog->type;
3278 info.id = prog->aux->id;
3279 info.load_time = prog->aux->load_time;
3280 info.created_by_uid = from_kuid_munged(current_user_ns(),
3281 prog->aux->user->uid);
3282 info.gpl_compatible = prog->gpl_compatible;
3284 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3285 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3287 ulen = info.nr_map_ids;
3288 info.nr_map_ids = prog->aux->used_map_cnt;
3289 ulen = min_t(u32, info.nr_map_ids, ulen);
3291 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3294 for (i = 0; i < ulen; i++)
3295 if (put_user(prog->aux->used_maps[i]->id,
3300 err = set_info_rec_size(&info);
3304 bpf_prog_get_stats(prog, &stats);
3305 info.run_time_ns = stats.nsecs;
3306 info.run_cnt = stats.cnt;
3308 if (!bpf_capable()) {
3309 info.jited_prog_len = 0;
3310 info.xlated_prog_len = 0;
3311 info.nr_jited_ksyms = 0;
3312 info.nr_jited_func_lens = 0;
3313 info.nr_func_info = 0;
3314 info.nr_line_info = 0;
3315 info.nr_jited_line_info = 0;
3319 ulen = info.xlated_prog_len;
3320 info.xlated_prog_len = bpf_prog_insn_size(prog);
3321 if (info.xlated_prog_len && ulen) {
3322 struct bpf_insn *insns_sanitized;
3325 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3326 info.xlated_prog_insns = 0;
3329 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3330 if (!insns_sanitized)
3332 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3333 ulen = min_t(u32, info.xlated_prog_len, ulen);
3334 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3335 kfree(insns_sanitized);
3340 if (bpf_prog_is_dev_bound(prog->aux)) {
3341 err = bpf_prog_offload_info_fill(&info, prog);
3347 /* NOTE: the following code is supposed to be skipped for offload.
3348 * bpf_prog_offload_info_fill() is the place to fill similar fields
3351 ulen = info.jited_prog_len;
3352 if (prog->aux->func_cnt) {
3355 info.jited_prog_len = 0;
3356 for (i = 0; i < prog->aux->func_cnt; i++)
3357 info.jited_prog_len += prog->aux->func[i]->jited_len;
3359 info.jited_prog_len = prog->jited_len;
3362 if (info.jited_prog_len && ulen) {
3363 if (bpf_dump_raw_ok(file->f_cred)) {
3364 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3365 ulen = min_t(u32, info.jited_prog_len, ulen);
3367 /* for multi-function programs, copy the JITed
3368 * instructions for all the functions
3370 if (prog->aux->func_cnt) {
3375 for (i = 0; i < prog->aux->func_cnt; i++) {
3376 len = prog->aux->func[i]->jited_len;
3377 len = min_t(u32, len, free);
3378 img = (u8 *) prog->aux->func[i]->bpf_func;
3379 if (copy_to_user(uinsns, img, len))
3387 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3391 info.jited_prog_insns = 0;
3395 ulen = info.nr_jited_ksyms;
3396 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
3398 if (bpf_dump_raw_ok(file->f_cred)) {
3399 unsigned long ksym_addr;
3400 u64 __user *user_ksyms;
3403 /* copy the address of the kernel symbol
3404 * corresponding to each function
3406 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3407 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3408 if (prog->aux->func_cnt) {
3409 for (i = 0; i < ulen; i++) {
3410 ksym_addr = (unsigned long)
3411 prog->aux->func[i]->bpf_func;
3412 if (put_user((u64) ksym_addr,
3417 ksym_addr = (unsigned long) prog->bpf_func;
3418 if (put_user((u64) ksym_addr, &user_ksyms[0]))
3422 info.jited_ksyms = 0;
3426 ulen = info.nr_jited_func_lens;
3427 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
3429 if (bpf_dump_raw_ok(file->f_cred)) {
3430 u32 __user *user_lens;
3433 /* copy the JITed image lengths for each function */
3434 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3435 user_lens = u64_to_user_ptr(info.jited_func_lens);
3436 if (prog->aux->func_cnt) {
3437 for (i = 0; i < ulen; i++) {
3439 prog->aux->func[i]->jited_len;
3440 if (put_user(func_len, &user_lens[i]))
3444 func_len = prog->jited_len;
3445 if (put_user(func_len, &user_lens[0]))
3449 info.jited_func_lens = 0;
3454 info.btf_id = btf_id(prog->aux->btf);
3456 ulen = info.nr_func_info;
3457 info.nr_func_info = prog->aux->func_info_cnt;
3458 if (info.nr_func_info && ulen) {
3459 char __user *user_finfo;
3461 user_finfo = u64_to_user_ptr(info.func_info);
3462 ulen = min_t(u32, info.nr_func_info, ulen);
3463 if (copy_to_user(user_finfo, prog->aux->func_info,
3464 info.func_info_rec_size * ulen))
3468 ulen = info.nr_line_info;
3469 info.nr_line_info = prog->aux->nr_linfo;
3470 if (info.nr_line_info && ulen) {
3471 __u8 __user *user_linfo;
3473 user_linfo = u64_to_user_ptr(info.line_info);
3474 ulen = min_t(u32, info.nr_line_info, ulen);
3475 if (copy_to_user(user_linfo, prog->aux->linfo,
3476 info.line_info_rec_size * ulen))
3480 ulen = info.nr_jited_line_info;
3481 if (prog->aux->jited_linfo)
3482 info.nr_jited_line_info = prog->aux->nr_linfo;
3484 info.nr_jited_line_info = 0;
3485 if (info.nr_jited_line_info && ulen) {
3486 if (bpf_dump_raw_ok(file->f_cred)) {
3487 __u64 __user *user_linfo;
3490 user_linfo = u64_to_user_ptr(info.jited_line_info);
3491 ulen = min_t(u32, info.nr_jited_line_info, ulen);
3492 for (i = 0; i < ulen; i++) {
3493 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3498 info.jited_line_info = 0;
3502 ulen = info.nr_prog_tags;
3503 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3505 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3508 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3509 ulen = min_t(u32, info.nr_prog_tags, ulen);
3510 if (prog->aux->func_cnt) {
3511 for (i = 0; i < ulen; i++) {
3512 if (copy_to_user(user_prog_tags[i],
3513 prog->aux->func[i]->tag,
3518 if (copy_to_user(user_prog_tags[0],
3519 prog->tag, BPF_TAG_SIZE))
3525 if (copy_to_user(uinfo, &info, info_len) ||
3526 put_user(info_len, &uattr->info.info_len))
3532 static int bpf_map_get_info_by_fd(struct file *file,
3533 struct bpf_map *map,
3534 const union bpf_attr *attr,
3535 union bpf_attr __user *uattr)
3537 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3538 struct bpf_map_info info;
3539 u32 info_len = attr->info.info_len;
3542 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3545 info_len = min_t(u32, sizeof(info), info_len);
3547 memset(&info, 0, sizeof(info));
3548 info.type = map->map_type;
3550 info.key_size = map->key_size;
3551 info.value_size = map->value_size;
3552 info.max_entries = map->max_entries;
3553 info.map_flags = map->map_flags;
3554 memcpy(info.name, map->name, sizeof(map->name));
3557 info.btf_id = btf_id(map->btf);
3558 info.btf_key_type_id = map->btf_key_type_id;
3559 info.btf_value_type_id = map->btf_value_type_id;
3561 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
3563 if (bpf_map_is_dev_bound(map)) {
3564 err = bpf_map_offload_info_fill(&info, map);
3569 if (copy_to_user(uinfo, &info, info_len) ||
3570 put_user(info_len, &uattr->info.info_len))
3576 static int bpf_btf_get_info_by_fd(struct file *file,
3578 const union bpf_attr *attr,
3579 union bpf_attr __user *uattr)
3581 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3582 u32 info_len = attr->info.info_len;
3585 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
3589 return btf_get_info_by_fd(btf, attr, uattr);
3592 static int bpf_link_get_info_by_fd(struct file *file,
3593 struct bpf_link *link,
3594 const union bpf_attr *attr,
3595 union bpf_attr __user *uattr)
3597 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3598 struct bpf_link_info info;
3599 u32 info_len = attr->info.info_len;
3602 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3605 info_len = min_t(u32, sizeof(info), info_len);
3607 memset(&info, 0, sizeof(info));
3608 if (copy_from_user(&info, uinfo, info_len))
3611 info.type = link->type;
3613 info.prog_id = link->prog->aux->id;
3615 if (link->ops->fill_link_info) {
3616 err = link->ops->fill_link_info(link, &info);
3621 if (copy_to_user(uinfo, &info, info_len) ||
3622 put_user(info_len, &uattr->info.info_len))
3629 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3631 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3632 union bpf_attr __user *uattr)
3634 int ufd = attr->info.bpf_fd;
3638 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3645 if (f.file->f_op == &bpf_prog_fops)
3646 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
3648 else if (f.file->f_op == &bpf_map_fops)
3649 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
3651 else if (f.file->f_op == &btf_fops)
3652 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
3653 else if (f.file->f_op == &bpf_link_fops)
3654 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
3663 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3665 static int bpf_btf_load(const union bpf_attr *attr)
3667 if (CHECK_ATTR(BPF_BTF_LOAD))
3673 return btf_new_fd(attr);
3676 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3678 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3680 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3683 if (!capable(CAP_SYS_ADMIN))
3686 return btf_get_fd_by_id(attr->btf_id);
3689 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3690 union bpf_attr __user *uattr,
3691 u32 prog_id, u32 fd_type,
3692 const char *buf, u64 probe_offset,
3695 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3696 u32 len = buf ? strlen(buf) : 0, input_len;
3699 if (put_user(len, &uattr->task_fd_query.buf_len))
3701 input_len = attr->task_fd_query.buf_len;
3702 if (input_len && ubuf) {
3704 /* nothing to copy, just make ubuf NULL terminated */
3707 if (put_user(zero, ubuf))
3709 } else if (input_len >= len + 1) {
3710 /* ubuf can hold the string with NULL terminator */
3711 if (copy_to_user(ubuf, buf, len + 1))
3714 /* ubuf cannot hold the string with NULL terminator,
3715 * do a partial copy with NULL terminator.
3720 if (copy_to_user(ubuf, buf, input_len - 1))
3722 if (put_user(zero, ubuf + input_len - 1))
3727 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3728 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3729 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3730 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3736 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3738 static int bpf_task_fd_query(const union bpf_attr *attr,
3739 union bpf_attr __user *uattr)
3741 pid_t pid = attr->task_fd_query.pid;
3742 u32 fd = attr->task_fd_query.fd;
3743 const struct perf_event *event;
3744 struct files_struct *files;
3745 struct task_struct *task;
3749 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3752 if (!capable(CAP_SYS_ADMIN))
3755 if (attr->task_fd_query.flags != 0)
3758 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3762 files = get_files_struct(task);
3763 put_task_struct(task);
3768 spin_lock(&files->file_lock);
3769 file = fcheck_files(files, fd);
3774 spin_unlock(&files->file_lock);
3775 put_files_struct(files);
3780 if (file->f_op == &bpf_link_fops) {
3781 struct bpf_link *link = file->private_data;
3783 if (link->ops == &bpf_raw_tp_link_lops) {
3784 struct bpf_raw_tp_link *raw_tp =
3785 container_of(link, struct bpf_raw_tp_link, link);
3786 struct bpf_raw_event_map *btp = raw_tp->btp;
3788 err = bpf_task_fd_query_copy(attr, uattr,
3789 raw_tp->link.prog->aux->id,
3790 BPF_FD_TYPE_RAW_TRACEPOINT,
3791 btp->tp->name, 0, 0);
3797 event = perf_get_event(file);
3798 if (!IS_ERR(event)) {
3799 u64 probe_offset, probe_addr;
3800 u32 prog_id, fd_type;
3803 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3804 &buf, &probe_offset,
3807 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3822 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
3824 #define BPF_DO_BATCH(fn) \
3830 err = fn(map, attr, uattr); \
3833 static int bpf_map_do_batch(const union bpf_attr *attr,
3834 union bpf_attr __user *uattr,
3837 struct bpf_map *map;
3841 if (CHECK_ATTR(BPF_MAP_BATCH))
3844 ufd = attr->batch.map_fd;
3846 map = __bpf_map_get(f);
3848 return PTR_ERR(map);
3850 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
3851 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
3852 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
3857 if (cmd != BPF_MAP_LOOKUP_BATCH &&
3858 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
3863 if (cmd == BPF_MAP_LOOKUP_BATCH)
3864 BPF_DO_BATCH(map->ops->map_lookup_batch);
3865 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
3866 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
3867 else if (cmd == BPF_MAP_UPDATE_BATCH)
3868 BPF_DO_BATCH(map->ops->map_update_batch);
3870 BPF_DO_BATCH(map->ops->map_delete_batch);
3877 static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3879 if (attr->link_create.attach_type == BPF_TRACE_ITER &&
3880 prog->expected_attach_type == BPF_TRACE_ITER)
3881 return bpf_iter_link_attach(attr, prog);
3886 #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
3887 static int link_create(union bpf_attr *attr)
3889 enum bpf_prog_type ptype;
3890 struct bpf_prog *prog;
3893 if (CHECK_ATTR(BPF_LINK_CREATE))
3896 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
3897 if (ptype == BPF_PROG_TYPE_UNSPEC)
3900 prog = bpf_prog_get_type(attr->link_create.prog_fd, ptype);
3902 return PTR_ERR(prog);
3904 ret = bpf_prog_attach_check_attach_type(prog,
3905 attr->link_create.attach_type);
3910 case BPF_PROG_TYPE_CGROUP_SKB:
3911 case BPF_PROG_TYPE_CGROUP_SOCK:
3912 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3913 case BPF_PROG_TYPE_SOCK_OPS:
3914 case BPF_PROG_TYPE_CGROUP_DEVICE:
3915 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3916 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3917 ret = cgroup_bpf_link_attach(attr, prog);
3919 case BPF_PROG_TYPE_TRACING:
3920 ret = tracing_bpf_link_attach(attr, prog);
3922 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3923 case BPF_PROG_TYPE_SK_LOOKUP:
3924 ret = netns_bpf_link_create(attr, prog);
3927 case BPF_PROG_TYPE_XDP:
3928 ret = bpf_xdp_link_attach(attr, prog);
3941 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
3943 static int link_update(union bpf_attr *attr)
3945 struct bpf_prog *old_prog = NULL, *new_prog;
3946 struct bpf_link *link;
3950 if (CHECK_ATTR(BPF_LINK_UPDATE))
3953 flags = attr->link_update.flags;
3954 if (flags & ~BPF_F_REPLACE)
3957 link = bpf_link_get_from_fd(attr->link_update.link_fd);
3959 return PTR_ERR(link);
3961 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
3962 if (IS_ERR(new_prog)) {
3963 ret = PTR_ERR(new_prog);
3967 if (flags & BPF_F_REPLACE) {
3968 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
3969 if (IS_ERR(old_prog)) {
3970 ret = PTR_ERR(old_prog);
3974 } else if (attr->link_update.old_prog_fd) {
3979 if (link->ops->update_prog)
3980 ret = link->ops->update_prog(link, new_prog, old_prog);
3986 bpf_prog_put(old_prog);
3988 bpf_prog_put(new_prog);
3994 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
3996 static int link_detach(union bpf_attr *attr)
3998 struct bpf_link *link;
4001 if (CHECK_ATTR(BPF_LINK_DETACH))
4004 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4006 return PTR_ERR(link);
4008 if (link->ops->detach)
4009 ret = link->ops->detach(link);
4017 static int bpf_link_inc_not_zero(struct bpf_link *link)
4019 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? 0 : -ENOENT;
4022 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4024 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4026 struct bpf_link *link;
4027 u32 id = attr->link_id;
4030 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4033 if (!capable(CAP_SYS_ADMIN))
4036 spin_lock_bh(&link_idr_lock);
4037 link = idr_find(&link_idr, id);
4038 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4041 err = bpf_link_inc_not_zero(link);
4047 spin_unlock_bh(&link_idr_lock);
4052 fd = bpf_link_new_fd(link);
4059 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4061 static int bpf_stats_release(struct inode *inode, struct file *file)
4063 mutex_lock(&bpf_stats_enabled_mutex);
4064 static_key_slow_dec(&bpf_stats_enabled_key.key);
4065 mutex_unlock(&bpf_stats_enabled_mutex);
4069 static const struct file_operations bpf_stats_fops = {
4070 .release = bpf_stats_release,
4073 static int bpf_enable_runtime_stats(void)
4077 mutex_lock(&bpf_stats_enabled_mutex);
4079 /* Set a very high limit to avoid overflow */
4080 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4081 mutex_unlock(&bpf_stats_enabled_mutex);
4085 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4087 static_key_slow_inc(&bpf_stats_enabled_key.key);
4089 mutex_unlock(&bpf_stats_enabled_mutex);
4093 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4095 static int bpf_enable_stats(union bpf_attr *attr)
4098 if (CHECK_ATTR(BPF_ENABLE_STATS))
4101 if (!capable(CAP_SYS_ADMIN))
4104 switch (attr->enable_stats.type) {
4105 case BPF_STATS_RUN_TIME:
4106 return bpf_enable_runtime_stats();
4113 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4115 static int bpf_iter_create(union bpf_attr *attr)
4117 struct bpf_link *link;
4120 if (CHECK_ATTR(BPF_ITER_CREATE))
4123 if (attr->iter_create.flags)
4126 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4128 return PTR_ERR(link);
4130 err = bpf_iter_new_fd(link);
4136 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4138 union bpf_attr attr;
4141 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
4144 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4147 size = min_t(u32, size, sizeof(attr));
4149 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4150 memset(&attr, 0, sizeof(attr));
4151 if (copy_from_user(&attr, uattr, size) != 0)
4154 err = security_bpf(cmd, &attr, size);
4159 case BPF_MAP_CREATE:
4160 err = map_create(&attr);
4162 case BPF_MAP_LOOKUP_ELEM:
4163 err = map_lookup_elem(&attr);
4165 case BPF_MAP_UPDATE_ELEM:
4166 err = map_update_elem(&attr);
4168 case BPF_MAP_DELETE_ELEM:
4169 err = map_delete_elem(&attr);
4171 case BPF_MAP_GET_NEXT_KEY:
4172 err = map_get_next_key(&attr);
4174 case BPF_MAP_FREEZE:
4175 err = map_freeze(&attr);
4178 err = bpf_prog_load(&attr, uattr);
4181 err = bpf_obj_pin(&attr);
4184 err = bpf_obj_get(&attr);
4186 case BPF_PROG_ATTACH:
4187 err = bpf_prog_attach(&attr);
4189 case BPF_PROG_DETACH:
4190 err = bpf_prog_detach(&attr);
4192 case BPF_PROG_QUERY:
4193 err = bpf_prog_query(&attr, uattr);
4195 case BPF_PROG_TEST_RUN:
4196 err = bpf_prog_test_run(&attr, uattr);
4198 case BPF_PROG_GET_NEXT_ID:
4199 err = bpf_obj_get_next_id(&attr, uattr,
4200 &prog_idr, &prog_idr_lock);
4202 case BPF_MAP_GET_NEXT_ID:
4203 err = bpf_obj_get_next_id(&attr, uattr,
4204 &map_idr, &map_idr_lock);
4206 case BPF_BTF_GET_NEXT_ID:
4207 err = bpf_obj_get_next_id(&attr, uattr,
4208 &btf_idr, &btf_idr_lock);
4210 case BPF_PROG_GET_FD_BY_ID:
4211 err = bpf_prog_get_fd_by_id(&attr);
4213 case BPF_MAP_GET_FD_BY_ID:
4214 err = bpf_map_get_fd_by_id(&attr);
4216 case BPF_OBJ_GET_INFO_BY_FD:
4217 err = bpf_obj_get_info_by_fd(&attr, uattr);
4219 case BPF_RAW_TRACEPOINT_OPEN:
4220 err = bpf_raw_tracepoint_open(&attr);
4223 err = bpf_btf_load(&attr);
4225 case BPF_BTF_GET_FD_BY_ID:
4226 err = bpf_btf_get_fd_by_id(&attr);
4228 case BPF_TASK_FD_QUERY:
4229 err = bpf_task_fd_query(&attr, uattr);
4231 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4232 err = map_lookup_and_delete_elem(&attr);
4234 case BPF_MAP_LOOKUP_BATCH:
4235 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
4237 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4238 err = bpf_map_do_batch(&attr, uattr,
4239 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4241 case BPF_MAP_UPDATE_BATCH:
4242 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
4244 case BPF_MAP_DELETE_BATCH:
4245 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
4247 case BPF_LINK_CREATE:
4248 err = link_create(&attr);
4250 case BPF_LINK_UPDATE:
4251 err = link_update(&attr);
4253 case BPF_LINK_GET_FD_BY_ID:
4254 err = bpf_link_get_fd_by_id(&attr);
4256 case BPF_LINK_GET_NEXT_ID:
4257 err = bpf_obj_get_next_id(&attr, uattr,
4258 &link_idr, &link_idr_lock);
4260 case BPF_ENABLE_STATS:
4261 err = bpf_enable_stats(&attr);
4263 case BPF_ITER_CREATE:
4264 err = bpf_iter_create(&attr);
4266 case BPF_LINK_DETACH:
4267 err = link_detach(&attr);