1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
5 #include <linux/bpf-cgroup.h>
6 #include <linux/bpf_trace.h>
7 #include <linux/bpf_lirc.h>
8 #include <linux/bpf_verifier.h>
10 #include <linux/syscalls.h>
11 #include <linux/slab.h>
12 #include <linux/sched/signal.h>
13 #include <linux/vmalloc.h>
14 #include <linux/mmzone.h>
15 #include <linux/anon_inodes.h>
16 #include <linux/fdtable.h>
17 #include <linux/file.h>
19 #include <linux/license.h>
20 #include <linux/filter.h>
21 #include <linux/kernel.h>
22 #include <linux/idr.h>
23 #include <linux/cred.h>
24 #include <linux/timekeeping.h>
25 #include <linux/ctype.h>
26 #include <linux/nospec.h>
27 #include <linux/audit.h>
28 #include <uapi/linux/btf.h>
29 #include <linux/pgtable.h>
30 #include <linux/bpf_lsm.h>
31 #include <linux/poll.h>
32 #include <linux/bpf-netns.h>
33 #include <linux/rcupdate_trace.h>
34 #include <linux/memcontrol.h>
36 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
38 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
39 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
40 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
41 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
44 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
46 DEFINE_PER_CPU(int, bpf_prog_active);
47 static DEFINE_IDR(prog_idr);
48 static DEFINE_SPINLOCK(prog_idr_lock);
49 static DEFINE_IDR(map_idr);
50 static DEFINE_SPINLOCK(map_idr_lock);
51 static DEFINE_IDR(link_idr);
52 static DEFINE_SPINLOCK(link_idr_lock);
54 int sysctl_unprivileged_bpf_disabled __read_mostly =
55 IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
57 static const struct bpf_map_ops * const bpf_map_types[] = {
58 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
59 #define BPF_MAP_TYPE(_id, _ops) \
61 #define BPF_LINK_TYPE(_id, _name)
62 #include <linux/bpf_types.h>
69 * If we're handed a bigger struct than we know of, ensure all the unknown bits
70 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
71 * we don't know about yet.
73 * There is a ToCToU between this function call and the following
74 * copy_from_user() call. However, this is not a concern since this function is
75 * meant to be a future-proofing of bits.
77 int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
83 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
86 if (actual_size <= expected_size)
90 res = memchr_inv(uaddr.kernel + expected_size, 0,
91 actual_size - expected_size) == NULL;
93 res = check_zeroed_user(uaddr.user + expected_size,
94 actual_size - expected_size);
97 return res ? 0 : -E2BIG;
100 const struct bpf_map_ops bpf_map_offload_ops = {
101 .map_meta_equal = bpf_map_meta_equal,
102 .map_alloc = bpf_map_offload_map_alloc,
103 .map_free = bpf_map_offload_map_free,
104 .map_check_btf = map_check_no_btf,
107 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
109 const struct bpf_map_ops *ops;
110 u32 type = attr->map_type;
114 if (type >= ARRAY_SIZE(bpf_map_types))
115 return ERR_PTR(-EINVAL);
116 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
117 ops = bpf_map_types[type];
119 return ERR_PTR(-EINVAL);
121 if (ops->map_alloc_check) {
122 err = ops->map_alloc_check(attr);
126 if (attr->map_ifindex)
127 ops = &bpf_map_offload_ops;
128 map = ops->map_alloc(attr);
132 map->map_type = type;
136 static void bpf_map_write_active_inc(struct bpf_map *map)
138 atomic64_inc(&map->writecnt);
141 static void bpf_map_write_active_dec(struct bpf_map *map)
143 atomic64_dec(&map->writecnt);
146 bool bpf_map_write_active(const struct bpf_map *map)
148 return atomic64_read(&map->writecnt) != 0;
151 static u32 bpf_map_value_size(const struct bpf_map *map)
153 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
154 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
155 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
156 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
157 return round_up(map->value_size, 8) * num_possible_cpus();
158 else if (IS_FD_MAP(map))
161 return map->value_size;
164 static void maybe_wait_bpf_programs(struct bpf_map *map)
166 /* Wait for any running BPF programs to complete so that
167 * userspace, when we return to it, knows that all programs
168 * that could be running use the new map value.
170 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
171 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
175 static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
176 void *value, __u64 flags)
180 /* Need to create a kthread, thus must support schedule */
181 if (bpf_map_is_dev_bound(map)) {
182 return bpf_map_offload_update_elem(map, key, value, flags);
183 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
184 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
185 return map->ops->map_update_elem(map, key, value, flags);
186 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
187 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
188 return sock_map_update_elem_sys(map, key, value, flags);
189 } else if (IS_FD_PROG_ARRAY(map)) {
190 return bpf_fd_array_map_update_elem(map, f.file, key, value,
194 bpf_disable_instrumentation();
195 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
196 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
197 err = bpf_percpu_hash_update(map, key, value, flags);
198 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
199 err = bpf_percpu_array_update(map, key, value, flags);
200 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
201 err = bpf_percpu_cgroup_storage_update(map, key, value,
203 } else if (IS_FD_ARRAY(map)) {
205 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
208 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
210 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
213 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
214 /* rcu_read_lock() is not needed */
215 err = bpf_fd_reuseport_array_update_elem(map, key, value,
217 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
218 map->map_type == BPF_MAP_TYPE_STACK ||
219 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
220 err = map->ops->map_push_elem(map, value, flags);
223 err = map->ops->map_update_elem(map, key, value, flags);
226 bpf_enable_instrumentation();
227 maybe_wait_bpf_programs(map);
232 static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
238 if (bpf_map_is_dev_bound(map))
239 return bpf_map_offload_lookup_elem(map, key, value);
241 bpf_disable_instrumentation();
242 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
243 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
244 err = bpf_percpu_hash_copy(map, key, value);
245 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
246 err = bpf_percpu_array_copy(map, key, value);
247 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
248 err = bpf_percpu_cgroup_storage_copy(map, key, value);
249 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
250 err = bpf_stackmap_copy(map, key, value);
251 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
252 err = bpf_fd_array_map_lookup_elem(map, key, value);
253 } else if (IS_FD_HASH(map)) {
254 err = bpf_fd_htab_map_lookup_elem(map, key, value);
255 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
256 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
257 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
258 map->map_type == BPF_MAP_TYPE_STACK ||
259 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
260 err = map->ops->map_peek_elem(map, value);
261 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
262 /* struct_ops map requires directly updating "value" */
263 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
266 if (map->ops->map_lookup_elem_sys_only)
267 ptr = map->ops->map_lookup_elem_sys_only(map, key);
269 ptr = map->ops->map_lookup_elem(map, key);
276 if (flags & BPF_F_LOCK)
277 /* lock 'ptr' and copy everything but lock */
278 copy_map_value_locked(map, value, ptr, true);
280 copy_map_value(map, value, ptr);
281 /* mask lock and timer, since value wasn't zero inited */
282 check_and_init_map_value(map, value);
287 bpf_enable_instrumentation();
288 maybe_wait_bpf_programs(map);
293 /* Please, do not use this function outside from the map creation path
294 * (e.g. in map update path) without taking care of setting the active
295 * memory cgroup (see at bpf_map_kmalloc_node() for example).
297 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
299 /* We really just want to fail instead of triggering OOM killer
300 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
301 * which is used for lower order allocation requests.
303 * It has been observed that higher order allocation requests done by
304 * vmalloc with __GFP_NORETRY being set might fail due to not trying
305 * to reclaim memory from the page cache, thus we set
306 * __GFP_RETRY_MAYFAIL to avoid such situations.
309 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
310 unsigned int flags = 0;
311 unsigned long align = 1;
314 if (size >= SIZE_MAX)
317 /* kmalloc()'ed memory can't be mmap()'ed */
319 BUG_ON(!PAGE_ALIGNED(size));
322 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
323 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
329 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
330 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
331 flags, numa_node, __builtin_return_address(0));
334 void *bpf_map_area_alloc(u64 size, int numa_node)
336 return __bpf_map_area_alloc(size, numa_node, false);
339 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
341 return __bpf_map_area_alloc(size, numa_node, true);
344 void bpf_map_area_free(void *area)
349 static u32 bpf_map_flags_retain_permanent(u32 flags)
351 /* Some map creation flags are not tied to the map object but
352 * rather to the map fd instead, so they have no meaning upon
353 * map object inspection since multiple file descriptors with
354 * different (access) properties can exist here. Thus, given
355 * this has zero meaning for the map itself, lets clear these
358 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
361 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
363 map->map_type = attr->map_type;
364 map->key_size = attr->key_size;
365 map->value_size = attr->value_size;
366 map->max_entries = attr->max_entries;
367 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
368 map->numa_node = bpf_map_attr_numa_node(attr);
369 map->map_extra = attr->map_extra;
372 static int bpf_map_alloc_id(struct bpf_map *map)
376 idr_preload(GFP_KERNEL);
377 spin_lock_bh(&map_idr_lock);
378 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
381 spin_unlock_bh(&map_idr_lock);
384 if (WARN_ON_ONCE(!id))
387 return id > 0 ? 0 : id;
390 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
394 /* Offloaded maps are removed from the IDR store when their device
395 * disappears - even if someone holds an fd to them they are unusable,
396 * the memory is gone, all ops will fail; they are simply waiting for
397 * refcnt to drop to be freed.
403 spin_lock_irqsave(&map_idr_lock, flags);
405 __acquire(&map_idr_lock);
407 idr_remove(&map_idr, map->id);
411 spin_unlock_irqrestore(&map_idr_lock, flags);
413 __release(&map_idr_lock);
416 #ifdef CONFIG_MEMCG_KMEM
417 static void bpf_map_save_memcg(struct bpf_map *map)
419 map->memcg = get_mem_cgroup_from_mm(current->mm);
422 static void bpf_map_release_memcg(struct bpf_map *map)
424 mem_cgroup_put(map->memcg);
427 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
430 struct mem_cgroup *old_memcg;
433 old_memcg = set_active_memcg(map->memcg);
434 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
435 set_active_memcg(old_memcg);
440 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
442 struct mem_cgroup *old_memcg;
445 old_memcg = set_active_memcg(map->memcg);
446 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
447 set_active_memcg(old_memcg);
452 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
453 size_t align, gfp_t flags)
455 struct mem_cgroup *old_memcg;
458 old_memcg = set_active_memcg(map->memcg);
459 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
460 set_active_memcg(old_memcg);
466 static void bpf_map_save_memcg(struct bpf_map *map)
470 static void bpf_map_release_memcg(struct bpf_map *map)
475 /* called from workqueue */
476 static void bpf_map_free_deferred(struct work_struct *work)
478 struct bpf_map *map = container_of(work, struct bpf_map, work);
480 security_bpf_map_free(map);
481 bpf_map_release_memcg(map);
482 /* implementation dependent freeing */
483 map->ops->map_free(map);
486 static void bpf_map_put_uref(struct bpf_map *map)
488 if (atomic64_dec_and_test(&map->usercnt)) {
489 if (map->ops->map_release_uref)
490 map->ops->map_release_uref(map);
494 /* decrement map refcnt and schedule it for freeing via workqueue
495 * (unrelying map implementation ops->map_free() might sleep)
497 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
499 if (atomic64_dec_and_test(&map->refcnt)) {
500 /* bpf_map_free_id() must be called first */
501 bpf_map_free_id(map, do_idr_lock);
503 INIT_WORK(&map->work, bpf_map_free_deferred);
504 schedule_work(&map->work);
508 void bpf_map_put(struct bpf_map *map)
510 __bpf_map_put(map, true);
512 EXPORT_SYMBOL_GPL(bpf_map_put);
514 void bpf_map_put_with_uref(struct bpf_map *map)
516 bpf_map_put_uref(map);
520 static int bpf_map_release(struct inode *inode, struct file *filp)
522 struct bpf_map *map = filp->private_data;
524 if (map->ops->map_release)
525 map->ops->map_release(map, filp);
527 bpf_map_put_with_uref(map);
531 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
533 fmode_t mode = f.file->f_mode;
535 /* Our file permissions may have been overridden by global
536 * map permissions facing syscall side.
538 if (READ_ONCE(map->frozen))
539 mode &= ~FMODE_CAN_WRITE;
543 #ifdef CONFIG_PROC_FS
544 /* Provides an approximation of the map's memory footprint.
545 * Used only to provide a backward compatibility and display
546 * a reasonable "memlock" info.
548 static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
552 size = round_up(map->key_size + bpf_map_value_size(map), 8);
554 return round_up(map->max_entries * size, PAGE_SIZE);
557 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
559 const struct bpf_map *map = filp->private_data;
560 const struct bpf_array *array;
561 u32 type = 0, jited = 0;
563 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
564 array = container_of(map, struct bpf_array, map);
565 spin_lock(&array->aux->owner.lock);
566 type = array->aux->owner.type;
567 jited = array->aux->owner.jited;
568 spin_unlock(&array->aux->owner.lock);
577 "map_extra:\t%#llx\n"
586 (unsigned long long)map->map_extra,
587 bpf_map_memory_footprint(map),
589 READ_ONCE(map->frozen));
591 seq_printf(m, "owner_prog_type:\t%u\n", type);
592 seq_printf(m, "owner_jited:\t%u\n", jited);
597 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
600 /* We need this handler such that alloc_file() enables
601 * f_mode with FMODE_CAN_READ.
606 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
607 size_t siz, loff_t *ppos)
609 /* We need this handler such that alloc_file() enables
610 * f_mode with FMODE_CAN_WRITE.
615 /* called for any extra memory-mapped regions (except initial) */
616 static void bpf_map_mmap_open(struct vm_area_struct *vma)
618 struct bpf_map *map = vma->vm_file->private_data;
620 if (vma->vm_flags & VM_MAYWRITE)
621 bpf_map_write_active_inc(map);
624 /* called for all unmapped memory region (including initial) */
625 static void bpf_map_mmap_close(struct vm_area_struct *vma)
627 struct bpf_map *map = vma->vm_file->private_data;
629 if (vma->vm_flags & VM_MAYWRITE)
630 bpf_map_write_active_dec(map);
633 static const struct vm_operations_struct bpf_map_default_vmops = {
634 .open = bpf_map_mmap_open,
635 .close = bpf_map_mmap_close,
638 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
640 struct bpf_map *map = filp->private_data;
643 if (!map->ops->map_mmap || map_value_has_spin_lock(map) ||
644 map_value_has_timer(map))
647 if (!(vma->vm_flags & VM_SHARED))
650 mutex_lock(&map->freeze_mutex);
652 if (vma->vm_flags & VM_WRITE) {
657 /* map is meant to be read-only, so do not allow mapping as
658 * writable, because it's possible to leak a writable page
659 * reference and allows user-space to still modify it after
660 * freezing, while verifier will assume contents do not change
662 if (map->map_flags & BPF_F_RDONLY_PROG) {
668 /* set default open/close callbacks */
669 vma->vm_ops = &bpf_map_default_vmops;
670 vma->vm_private_data = map;
671 vma->vm_flags &= ~VM_MAYEXEC;
672 if (!(vma->vm_flags & VM_WRITE))
673 /* disallow re-mapping with PROT_WRITE */
674 vma->vm_flags &= ~VM_MAYWRITE;
676 err = map->ops->map_mmap(map, vma);
680 if (vma->vm_flags & VM_MAYWRITE)
681 bpf_map_write_active_inc(map);
683 mutex_unlock(&map->freeze_mutex);
687 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
689 struct bpf_map *map = filp->private_data;
691 if (map->ops->map_poll)
692 return map->ops->map_poll(map, filp, pts);
697 const struct file_operations bpf_map_fops = {
698 #ifdef CONFIG_PROC_FS
699 .show_fdinfo = bpf_map_show_fdinfo,
701 .release = bpf_map_release,
702 .read = bpf_dummy_read,
703 .write = bpf_dummy_write,
704 .mmap = bpf_map_mmap,
705 .poll = bpf_map_poll,
708 int bpf_map_new_fd(struct bpf_map *map, int flags)
712 ret = security_bpf_map(map, OPEN_FMODE(flags));
716 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
720 int bpf_get_file_flag(int flags)
722 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
724 if (flags & BPF_F_RDONLY)
726 if (flags & BPF_F_WRONLY)
731 /* helper macro to check that unused fields 'union bpf_attr' are zero */
732 #define CHECK_ATTR(CMD) \
733 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
734 sizeof(attr->CMD##_LAST_FIELD), 0, \
736 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
737 sizeof(attr->CMD##_LAST_FIELD)) != NULL
739 /* dst and src must have at least "size" number of bytes.
740 * Return strlen on success and < 0 on error.
742 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
744 const char *end = src + size;
745 const char *orig_src = src;
747 memset(dst, 0, size);
748 /* Copy all isalnum(), '_' and '.' chars. */
749 while (src < end && *src) {
750 if (!isalnum(*src) &&
751 *src != '_' && *src != '.')
756 /* No '\0' found in "size" number of bytes */
760 return src - orig_src;
763 int map_check_no_btf(const struct bpf_map *map,
764 const struct btf *btf,
765 const struct btf_type *key_type,
766 const struct btf_type *value_type)
771 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
772 u32 btf_key_id, u32 btf_value_id)
774 const struct btf_type *key_type, *value_type;
775 u32 key_size, value_size;
778 /* Some maps allow key to be unspecified. */
780 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
781 if (!key_type || key_size != map->key_size)
784 key_type = btf_type_by_id(btf, 0);
785 if (!map->ops->map_check_btf)
789 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
790 if (!value_type || value_size != map->value_size)
793 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
795 if (map_value_has_spin_lock(map)) {
796 if (map->map_flags & BPF_F_RDONLY_PROG)
798 if (map->map_type != BPF_MAP_TYPE_HASH &&
799 map->map_type != BPF_MAP_TYPE_ARRAY &&
800 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
801 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
802 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
803 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
805 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
808 "verifier bug spin_lock_off %d value_size %d\n",
809 map->spin_lock_off, map->value_size);
814 map->timer_off = btf_find_timer(btf, value_type);
815 if (map_value_has_timer(map)) {
816 if (map->map_flags & BPF_F_RDONLY_PROG)
818 if (map->map_type != BPF_MAP_TYPE_HASH &&
819 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
820 map->map_type != BPF_MAP_TYPE_ARRAY)
824 if (map->ops->map_check_btf)
825 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
830 #define BPF_MAP_CREATE_LAST_FIELD map_extra
831 /* called via syscall */
832 static int map_create(union bpf_attr *attr)
834 int numa_node = bpf_map_attr_numa_node(attr);
839 err = CHECK_ATTR(BPF_MAP_CREATE);
843 if (attr->btf_vmlinux_value_type_id) {
844 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
845 attr->btf_key_type_id || attr->btf_value_type_id)
847 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
851 if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER &&
852 attr->map_extra != 0)
855 f_flags = bpf_get_file_flag(attr->map_flags);
859 if (numa_node != NUMA_NO_NODE &&
860 ((unsigned int)numa_node >= nr_node_ids ||
861 !node_online(numa_node)))
864 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
865 map = find_and_alloc_map(attr);
869 err = bpf_obj_name_cpy(map->name, attr->map_name,
870 sizeof(attr->map_name));
874 atomic64_set(&map->refcnt, 1);
875 atomic64_set(&map->usercnt, 1);
876 mutex_init(&map->freeze_mutex);
878 map->spin_lock_off = -EINVAL;
879 map->timer_off = -EINVAL;
880 if (attr->btf_key_type_id || attr->btf_value_type_id ||
881 /* Even the map's value is a kernel's struct,
882 * the bpf_prog.o must have BTF to begin with
883 * to figure out the corresponding kernel's
884 * counter part. Thus, attr->btf_fd has
887 attr->btf_vmlinux_value_type_id) {
890 btf = btf_get_by_fd(attr->btf_fd);
895 if (btf_is_kernel(btf)) {
902 if (attr->btf_value_type_id) {
903 err = map_check_btf(map, btf, attr->btf_key_type_id,
904 attr->btf_value_type_id);
909 map->btf_key_type_id = attr->btf_key_type_id;
910 map->btf_value_type_id = attr->btf_value_type_id;
911 map->btf_vmlinux_value_type_id =
912 attr->btf_vmlinux_value_type_id;
915 err = security_bpf_map_alloc(map);
919 err = bpf_map_alloc_id(map);
923 bpf_map_save_memcg(map);
925 err = bpf_map_new_fd(map, f_flags);
927 /* failed to allocate fd.
928 * bpf_map_put_with_uref() is needed because the above
929 * bpf_map_alloc_id() has published the map
930 * to the userspace and the userspace may
931 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
933 bpf_map_put_with_uref(map);
940 security_bpf_map_free(map);
943 map->ops->map_free(map);
947 /* if error is returned, fd is released.
948 * On success caller should complete fd access with matching fdput()
950 struct bpf_map *__bpf_map_get(struct fd f)
953 return ERR_PTR(-EBADF);
954 if (f.file->f_op != &bpf_map_fops) {
956 return ERR_PTR(-EINVAL);
959 return f.file->private_data;
962 void bpf_map_inc(struct bpf_map *map)
964 atomic64_inc(&map->refcnt);
966 EXPORT_SYMBOL_GPL(bpf_map_inc);
968 void bpf_map_inc_with_uref(struct bpf_map *map)
970 atomic64_inc(&map->refcnt);
971 atomic64_inc(&map->usercnt);
973 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
975 struct bpf_map *bpf_map_get(u32 ufd)
977 struct fd f = fdget(ufd);
980 map = __bpf_map_get(f);
990 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
992 struct fd f = fdget(ufd);
995 map = __bpf_map_get(f);
999 bpf_map_inc_with_uref(map);
1005 /* map_idr_lock should have been held */
1006 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
1010 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
1012 return ERR_PTR(-ENOENT);
1014 atomic64_inc(&map->usercnt);
1019 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
1021 spin_lock_bh(&map_idr_lock);
1022 map = __bpf_map_inc_not_zero(map, false);
1023 spin_unlock_bh(&map_idr_lock);
1027 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
1029 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1034 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1037 return vmemdup_user(ukey, key_size);
1040 return ERR_PTR(-EINVAL);
1045 static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1048 return kvmemdup_bpfptr(ukey, key_size);
1050 if (!bpfptr_is_null(ukey))
1051 return ERR_PTR(-EINVAL);
1056 /* last field in 'union bpf_attr' used by this command */
1057 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1059 static int map_lookup_elem(union bpf_attr *attr)
1061 void __user *ukey = u64_to_user_ptr(attr->key);
1062 void __user *uvalue = u64_to_user_ptr(attr->value);
1063 int ufd = attr->map_fd;
1064 struct bpf_map *map;
1070 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1073 if (attr->flags & ~BPF_F_LOCK)
1077 map = __bpf_map_get(f);
1079 return PTR_ERR(map);
1080 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1085 if ((attr->flags & BPF_F_LOCK) &&
1086 !map_value_has_spin_lock(map)) {
1091 key = __bpf_copy_key(ukey, map->key_size);
1097 value_size = bpf_map_value_size(map);
1100 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1104 if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
1105 if (copy_from_user(value, uvalue, value_size))
1108 err = bpf_map_copy_value(map, key, value, attr->flags);
1112 err = bpf_map_copy_value(map, key, value, attr->flags);
1117 if (copy_to_user(uvalue, value, value_size) != 0)
1132 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1134 static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
1136 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1137 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
1138 int ufd = attr->map_fd;
1139 struct bpf_map *map;
1145 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1149 map = __bpf_map_get(f);
1151 return PTR_ERR(map);
1152 bpf_map_write_active_inc(map);
1153 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1158 if ((attr->flags & BPF_F_LOCK) &&
1159 !map_value_has_spin_lock(map)) {
1164 key = ___bpf_copy_key(ukey, map->key_size);
1170 value_size = bpf_map_value_size(map);
1173 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1178 if (copy_from_bpfptr(value, uvalue, value_size) != 0)
1181 err = bpf_map_update_value(map, f, key, value, attr->flags);
1188 bpf_map_write_active_dec(map);
1193 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1195 static int map_delete_elem(union bpf_attr *attr)
1197 void __user *ukey = u64_to_user_ptr(attr->key);
1198 int ufd = attr->map_fd;
1199 struct bpf_map *map;
1204 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1208 map = __bpf_map_get(f);
1210 return PTR_ERR(map);
1211 bpf_map_write_active_inc(map);
1212 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1217 key = __bpf_copy_key(ukey, map->key_size);
1223 if (bpf_map_is_dev_bound(map)) {
1224 err = bpf_map_offload_delete_elem(map, key);
1226 } else if (IS_FD_PROG_ARRAY(map) ||
1227 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1228 /* These maps require sleepable context */
1229 err = map->ops->map_delete_elem(map, key);
1233 bpf_disable_instrumentation();
1235 err = map->ops->map_delete_elem(map, key);
1237 bpf_enable_instrumentation();
1238 maybe_wait_bpf_programs(map);
1242 bpf_map_write_active_dec(map);
1247 /* last field in 'union bpf_attr' used by this command */
1248 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1250 static int map_get_next_key(union bpf_attr *attr)
1252 void __user *ukey = u64_to_user_ptr(attr->key);
1253 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1254 int ufd = attr->map_fd;
1255 struct bpf_map *map;
1256 void *key, *next_key;
1260 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1264 map = __bpf_map_get(f);
1266 return PTR_ERR(map);
1267 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1273 key = __bpf_copy_key(ukey, map->key_size);
1283 next_key = kvmalloc(map->key_size, GFP_USER);
1287 if (bpf_map_is_dev_bound(map)) {
1288 err = bpf_map_offload_get_next_key(map, key, next_key);
1293 err = map->ops->map_get_next_key(map, key, next_key);
1300 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1314 int generic_map_delete_batch(struct bpf_map *map,
1315 const union bpf_attr *attr,
1316 union bpf_attr __user *uattr)
1318 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1323 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1326 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1327 !map_value_has_spin_lock(map)) {
1331 max_count = attr->batch.count;
1335 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1339 for (cp = 0; cp < max_count; cp++) {
1341 if (copy_from_user(key, keys + cp * map->key_size,
1345 if (bpf_map_is_dev_bound(map)) {
1346 err = bpf_map_offload_delete_elem(map, key);
1350 bpf_disable_instrumentation();
1352 err = map->ops->map_delete_elem(map, key);
1354 bpf_enable_instrumentation();
1355 maybe_wait_bpf_programs(map);
1360 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1367 int generic_map_update_batch(struct bpf_map *map,
1368 const union bpf_attr *attr,
1369 union bpf_attr __user *uattr)
1371 void __user *values = u64_to_user_ptr(attr->batch.values);
1372 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1373 u32 value_size, cp, max_count;
1374 int ufd = attr->batch.map_fd;
1379 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1382 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1383 !map_value_has_spin_lock(map)) {
1387 value_size = bpf_map_value_size(map);
1389 max_count = attr->batch.count;
1393 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1397 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1403 f = fdget(ufd); /* bpf_map_do_batch() guarantees ufd is valid */
1404 for (cp = 0; cp < max_count; cp++) {
1406 if (copy_from_user(key, keys + cp * map->key_size,
1408 copy_from_user(value, values + cp * value_size, value_size))
1411 err = bpf_map_update_value(map, f, key, value,
1412 attr->batch.elem_flags);
1419 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1428 #define MAP_LOOKUP_RETRIES 3
1430 int generic_map_lookup_batch(struct bpf_map *map,
1431 const union bpf_attr *attr,
1432 union bpf_attr __user *uattr)
1434 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1435 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1436 void __user *values = u64_to_user_ptr(attr->batch.values);
1437 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1438 void *buf, *buf_prevkey, *prev_key, *key, *value;
1439 int err, retry = MAP_LOOKUP_RETRIES;
1440 u32 value_size, cp, max_count;
1442 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1445 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1446 !map_value_has_spin_lock(map))
1449 value_size = bpf_map_value_size(map);
1451 max_count = attr->batch.count;
1455 if (put_user(0, &uattr->batch.count))
1458 buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1462 buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1464 kvfree(buf_prevkey);
1470 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1473 value = key + map->key_size;
1475 prev_key = buf_prevkey;
1477 for (cp = 0; cp < max_count;) {
1479 err = map->ops->map_get_next_key(map, prev_key, key);
1483 err = bpf_map_copy_value(map, key, value,
1484 attr->batch.elem_flags);
1486 if (err == -ENOENT) {
1498 if (copy_to_user(keys + cp * map->key_size, key,
1503 if (copy_to_user(values + cp * value_size, value, value_size)) {
1509 prev_key = buf_prevkey;
1511 swap(prev_key, key);
1512 retry = MAP_LOOKUP_RETRIES;
1520 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1521 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1525 kvfree(buf_prevkey);
1530 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
1532 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1534 void __user *ukey = u64_to_user_ptr(attr->key);
1535 void __user *uvalue = u64_to_user_ptr(attr->value);
1536 int ufd = attr->map_fd;
1537 struct bpf_map *map;
1543 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1546 if (attr->flags & ~BPF_F_LOCK)
1550 map = __bpf_map_get(f);
1552 return PTR_ERR(map);
1553 bpf_map_write_active_inc(map);
1554 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1555 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1561 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1562 map->map_type == BPF_MAP_TYPE_STACK)) {
1567 if ((attr->flags & BPF_F_LOCK) &&
1568 !map_value_has_spin_lock(map)) {
1573 key = __bpf_copy_key(ukey, map->key_size);
1579 value_size = bpf_map_value_size(map);
1582 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1587 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1588 map->map_type == BPF_MAP_TYPE_STACK) {
1589 err = map->ops->map_pop_elem(map, value);
1590 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1591 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1592 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1593 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1594 if (!bpf_map_is_dev_bound(map)) {
1595 bpf_disable_instrumentation();
1597 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1599 bpf_enable_instrumentation();
1606 if (copy_to_user(uvalue, value, value_size) != 0) {
1618 bpf_map_write_active_dec(map);
1623 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1625 static int map_freeze(const union bpf_attr *attr)
1627 int err = 0, ufd = attr->map_fd;
1628 struct bpf_map *map;
1631 if (CHECK_ATTR(BPF_MAP_FREEZE))
1635 map = __bpf_map_get(f);
1637 return PTR_ERR(map);
1639 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS ||
1640 map_value_has_timer(map)) {
1645 mutex_lock(&map->freeze_mutex);
1646 if (bpf_map_write_active(map)) {
1650 if (READ_ONCE(map->frozen)) {
1654 if (!bpf_capable()) {
1659 WRITE_ONCE(map->frozen, true);
1661 mutex_unlock(&map->freeze_mutex);
1666 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1667 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1668 [_id] = & _name ## _prog_ops,
1669 #define BPF_MAP_TYPE(_id, _ops)
1670 #define BPF_LINK_TYPE(_id, _name)
1671 #include <linux/bpf_types.h>
1672 #undef BPF_PROG_TYPE
1674 #undef BPF_LINK_TYPE
1677 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1679 const struct bpf_prog_ops *ops;
1681 if (type >= ARRAY_SIZE(bpf_prog_types))
1683 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1684 ops = bpf_prog_types[type];
1688 if (!bpf_prog_is_dev_bound(prog->aux))
1689 prog->aux->ops = ops;
1691 prog->aux->ops = &bpf_offload_prog_ops;
1702 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1703 [BPF_AUDIT_LOAD] = "LOAD",
1704 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1707 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1709 struct audit_context *ctx = NULL;
1710 struct audit_buffer *ab;
1712 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1714 if (audit_enabled == AUDIT_OFF)
1716 if (op == BPF_AUDIT_LOAD)
1717 ctx = audit_context();
1718 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1721 audit_log_format(ab, "prog-id=%u op=%s",
1722 prog->aux->id, bpf_audit_str[op]);
1726 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1730 idr_preload(GFP_KERNEL);
1731 spin_lock_bh(&prog_idr_lock);
1732 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1735 spin_unlock_bh(&prog_idr_lock);
1738 /* id is in [1, INT_MAX) */
1739 if (WARN_ON_ONCE(!id))
1742 return id > 0 ? 0 : id;
1745 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1747 unsigned long flags;
1749 /* cBPF to eBPF migrations are currently not in the idr store.
1750 * Offloaded programs are removed from the store when their device
1751 * disappears - even if someone grabs an fd to them they are unusable,
1752 * simply waiting for refcnt to drop to be freed.
1758 spin_lock_irqsave(&prog_idr_lock, flags);
1760 __acquire(&prog_idr_lock);
1762 idr_remove(&prog_idr, prog->aux->id);
1766 spin_unlock_irqrestore(&prog_idr_lock, flags);
1768 __release(&prog_idr_lock);
1771 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1773 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1775 kvfree(aux->func_info);
1776 kfree(aux->func_info_aux);
1777 free_uid(aux->user);
1778 security_bpf_prog_free(aux);
1779 bpf_prog_free(aux->prog);
1782 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1784 bpf_prog_kallsyms_del_all(prog);
1785 btf_put(prog->aux->btf);
1786 kvfree(prog->aux->jited_linfo);
1787 kvfree(prog->aux->linfo);
1788 kfree(prog->aux->kfunc_tab);
1789 if (prog->aux->attach_btf)
1790 btf_put(prog->aux->attach_btf);
1793 if (prog->aux->sleepable)
1794 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1796 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1798 __bpf_prog_put_rcu(&prog->aux->rcu);
1802 static void bpf_prog_put_deferred(struct work_struct *work)
1804 struct bpf_prog_aux *aux;
1805 struct bpf_prog *prog;
1807 aux = container_of(work, struct bpf_prog_aux, work);
1809 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1810 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1811 __bpf_prog_put_noref(prog, true);
1814 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1816 struct bpf_prog_aux *aux = prog->aux;
1818 if (atomic64_dec_and_test(&aux->refcnt)) {
1819 /* bpf_prog_free_id() must be called first */
1820 bpf_prog_free_id(prog, do_idr_lock);
1822 if (in_irq() || irqs_disabled()) {
1823 INIT_WORK(&aux->work, bpf_prog_put_deferred);
1824 schedule_work(&aux->work);
1826 bpf_prog_put_deferred(&aux->work);
1831 void bpf_prog_put(struct bpf_prog *prog)
1833 __bpf_prog_put(prog, true);
1835 EXPORT_SYMBOL_GPL(bpf_prog_put);
1837 static int bpf_prog_release(struct inode *inode, struct file *filp)
1839 struct bpf_prog *prog = filp->private_data;
1845 struct bpf_prog_kstats {
1851 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1852 struct bpf_prog_kstats *stats)
1854 u64 nsecs = 0, cnt = 0, misses = 0;
1857 for_each_possible_cpu(cpu) {
1858 const struct bpf_prog_stats *st;
1860 u64 tnsecs, tcnt, tmisses;
1862 st = per_cpu_ptr(prog->stats, cpu);
1864 start = u64_stats_fetch_begin_irq(&st->syncp);
1865 tnsecs = u64_stats_read(&st->nsecs);
1866 tcnt = u64_stats_read(&st->cnt);
1867 tmisses = u64_stats_read(&st->misses);
1868 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1873 stats->nsecs = nsecs;
1875 stats->misses = misses;
1878 #ifdef CONFIG_PROC_FS
1879 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1881 const struct bpf_prog *prog = filp->private_data;
1882 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1883 struct bpf_prog_kstats stats;
1885 bpf_prog_get_stats(prog, &stats);
1886 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1893 "run_time_ns:\t%llu\n"
1895 "recursion_misses:\t%llu\n"
1896 "verified_insns:\t%u\n",
1900 prog->pages * 1ULL << PAGE_SHIFT,
1905 prog->aux->verified_insns);
1909 const struct file_operations bpf_prog_fops = {
1910 #ifdef CONFIG_PROC_FS
1911 .show_fdinfo = bpf_prog_show_fdinfo,
1913 .release = bpf_prog_release,
1914 .read = bpf_dummy_read,
1915 .write = bpf_dummy_write,
1918 int bpf_prog_new_fd(struct bpf_prog *prog)
1922 ret = security_bpf_prog(prog);
1926 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1927 O_RDWR | O_CLOEXEC);
1930 static struct bpf_prog *____bpf_prog_get(struct fd f)
1933 return ERR_PTR(-EBADF);
1934 if (f.file->f_op != &bpf_prog_fops) {
1936 return ERR_PTR(-EINVAL);
1939 return f.file->private_data;
1942 void bpf_prog_add(struct bpf_prog *prog, int i)
1944 atomic64_add(i, &prog->aux->refcnt);
1946 EXPORT_SYMBOL_GPL(bpf_prog_add);
1948 void bpf_prog_sub(struct bpf_prog *prog, int i)
1950 /* Only to be used for undoing previous bpf_prog_add() in some
1951 * error path. We still know that another entity in our call
1952 * path holds a reference to the program, thus atomic_sub() can
1953 * be safely used in such cases!
1955 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1957 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1959 void bpf_prog_inc(struct bpf_prog *prog)
1961 atomic64_inc(&prog->aux->refcnt);
1963 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1965 /* prog_idr_lock should have been held */
1966 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1970 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1973 return ERR_PTR(-ENOENT);
1977 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1979 bool bpf_prog_get_ok(struct bpf_prog *prog,
1980 enum bpf_prog_type *attach_type, bool attach_drv)
1982 /* not an attachment, just a refcount inc, always allow */
1986 if (prog->type != *attach_type)
1988 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1994 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1997 struct fd f = fdget(ufd);
1998 struct bpf_prog *prog;
2000 prog = ____bpf_prog_get(f);
2003 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
2004 prog = ERR_PTR(-EINVAL);
2014 struct bpf_prog *bpf_prog_get(u32 ufd)
2016 return __bpf_prog_get(ufd, NULL, false);
2019 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
2022 return __bpf_prog_get(ufd, &type, attach_drv);
2024 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
2026 /* Initially all BPF programs could be loaded w/o specifying
2027 * expected_attach_type. Later for some of them specifying expected_attach_type
2028 * at load time became required so that program could be validated properly.
2029 * Programs of types that are allowed to be loaded both w/ and w/o (for
2030 * backward compatibility) expected_attach_type, should have the default attach
2031 * type assigned to expected_attach_type for the latter case, so that it can be
2032 * validated later at attach time.
2034 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
2035 * prog type requires it but has some attach types that have to be backward
2038 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
2040 switch (attr->prog_type) {
2041 case BPF_PROG_TYPE_CGROUP_SOCK:
2042 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
2043 * exist so checking for non-zero is the way to go here.
2045 if (!attr->expected_attach_type)
2046 attr->expected_attach_type =
2047 BPF_CGROUP_INET_SOCK_CREATE;
2049 case BPF_PROG_TYPE_SK_REUSEPORT:
2050 if (!attr->expected_attach_type)
2051 attr->expected_attach_type =
2052 BPF_SK_REUSEPORT_SELECT;
2058 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2059 enum bpf_attach_type expected_attach_type,
2060 struct btf *attach_btf, u32 btf_id,
2061 struct bpf_prog *dst_prog)
2064 if (btf_id > BTF_MAX_TYPE)
2067 if (!attach_btf && !dst_prog)
2070 switch (prog_type) {
2071 case BPF_PROG_TYPE_TRACING:
2072 case BPF_PROG_TYPE_LSM:
2073 case BPF_PROG_TYPE_STRUCT_OPS:
2074 case BPF_PROG_TYPE_EXT:
2081 if (attach_btf && (!btf_id || dst_prog))
2084 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
2085 prog_type != BPF_PROG_TYPE_EXT)
2088 switch (prog_type) {
2089 case BPF_PROG_TYPE_CGROUP_SOCK:
2090 switch (expected_attach_type) {
2091 case BPF_CGROUP_INET_SOCK_CREATE:
2092 case BPF_CGROUP_INET_SOCK_RELEASE:
2093 case BPF_CGROUP_INET4_POST_BIND:
2094 case BPF_CGROUP_INET6_POST_BIND:
2099 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2100 switch (expected_attach_type) {
2101 case BPF_CGROUP_INET4_BIND:
2102 case BPF_CGROUP_INET6_BIND:
2103 case BPF_CGROUP_INET4_CONNECT:
2104 case BPF_CGROUP_INET6_CONNECT:
2105 case BPF_CGROUP_INET4_GETPEERNAME:
2106 case BPF_CGROUP_INET6_GETPEERNAME:
2107 case BPF_CGROUP_INET4_GETSOCKNAME:
2108 case BPF_CGROUP_INET6_GETSOCKNAME:
2109 case BPF_CGROUP_UDP4_SENDMSG:
2110 case BPF_CGROUP_UDP6_SENDMSG:
2111 case BPF_CGROUP_UDP4_RECVMSG:
2112 case BPF_CGROUP_UDP6_RECVMSG:
2117 case BPF_PROG_TYPE_CGROUP_SKB:
2118 switch (expected_attach_type) {
2119 case BPF_CGROUP_INET_INGRESS:
2120 case BPF_CGROUP_INET_EGRESS:
2125 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2126 switch (expected_attach_type) {
2127 case BPF_CGROUP_SETSOCKOPT:
2128 case BPF_CGROUP_GETSOCKOPT:
2133 case BPF_PROG_TYPE_SK_LOOKUP:
2134 if (expected_attach_type == BPF_SK_LOOKUP)
2137 case BPF_PROG_TYPE_SK_REUSEPORT:
2138 switch (expected_attach_type) {
2139 case BPF_SK_REUSEPORT_SELECT:
2140 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2145 case BPF_PROG_TYPE_SYSCALL:
2146 case BPF_PROG_TYPE_EXT:
2147 if (expected_attach_type)
2155 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2157 switch (prog_type) {
2158 case BPF_PROG_TYPE_SCHED_CLS:
2159 case BPF_PROG_TYPE_SCHED_ACT:
2160 case BPF_PROG_TYPE_XDP:
2161 case BPF_PROG_TYPE_LWT_IN:
2162 case BPF_PROG_TYPE_LWT_OUT:
2163 case BPF_PROG_TYPE_LWT_XMIT:
2164 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2165 case BPF_PROG_TYPE_SK_SKB:
2166 case BPF_PROG_TYPE_SK_MSG:
2167 case BPF_PROG_TYPE_LIRC_MODE2:
2168 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2169 case BPF_PROG_TYPE_CGROUP_DEVICE:
2170 case BPF_PROG_TYPE_CGROUP_SOCK:
2171 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2172 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2173 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2174 case BPF_PROG_TYPE_SOCK_OPS:
2175 case BPF_PROG_TYPE_EXT: /* extends any prog */
2177 case BPF_PROG_TYPE_CGROUP_SKB:
2179 case BPF_PROG_TYPE_SK_REUSEPORT:
2180 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2186 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2188 switch (prog_type) {
2189 case BPF_PROG_TYPE_KPROBE:
2190 case BPF_PROG_TYPE_TRACEPOINT:
2191 case BPF_PROG_TYPE_PERF_EVENT:
2192 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2193 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2194 case BPF_PROG_TYPE_TRACING:
2195 case BPF_PROG_TYPE_LSM:
2196 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2197 case BPF_PROG_TYPE_EXT: /* extends any prog */
2204 /* last field in 'union bpf_attr' used by this command */
2205 #define BPF_PROG_LOAD_LAST_FIELD core_relo_rec_size
2207 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
2209 enum bpf_prog_type type = attr->prog_type;
2210 struct bpf_prog *prog, *dst_prog = NULL;
2211 struct btf *attach_btf = NULL;
2216 if (CHECK_ATTR(BPF_PROG_LOAD))
2219 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2220 BPF_F_ANY_ALIGNMENT |
2221 BPF_F_TEST_STATE_FREQ |
2223 BPF_F_TEST_RND_HI32))
2226 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2227 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2231 /* copy eBPF program license from user space */
2232 if (strncpy_from_bpfptr(license,
2233 make_bpfptr(attr->license, uattr.is_kernel),
2234 sizeof(license) - 1) < 0)
2236 license[sizeof(license) - 1] = 0;
2238 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2239 is_gpl = license_is_gpl_compatible(license);
2241 if (attr->insn_cnt == 0 ||
2242 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2244 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2245 type != BPF_PROG_TYPE_CGROUP_SKB &&
2249 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2251 if (is_perfmon_prog_type(type) && !perfmon_capable())
2254 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2255 * or btf, we need to check which one it is
2257 if (attr->attach_prog_fd) {
2258 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2259 if (IS_ERR(dst_prog)) {
2261 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2262 if (IS_ERR(attach_btf))
2264 if (!btf_is_kernel(attach_btf)) {
2265 /* attaching through specifying bpf_prog's BTF
2266 * objects directly might be supported eventually
2268 btf_put(attach_btf);
2272 } else if (attr->attach_btf_id) {
2273 /* fall back to vmlinux BTF, if BTF type ID is specified */
2274 attach_btf = bpf_get_btf_vmlinux();
2275 if (IS_ERR(attach_btf))
2276 return PTR_ERR(attach_btf);
2279 btf_get(attach_btf);
2282 bpf_prog_load_fixup_attach_type(attr);
2283 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2284 attach_btf, attr->attach_btf_id,
2287 bpf_prog_put(dst_prog);
2289 btf_put(attach_btf);
2293 /* plain bpf_prog allocation */
2294 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2297 bpf_prog_put(dst_prog);
2299 btf_put(attach_btf);
2303 prog->expected_attach_type = attr->expected_attach_type;
2304 prog->aux->attach_btf = attach_btf;
2305 prog->aux->attach_btf_id = attr->attach_btf_id;
2306 prog->aux->dst_prog = dst_prog;
2307 prog->aux->offload_requested = !!attr->prog_ifindex;
2308 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2310 err = security_bpf_prog_alloc(prog->aux);
2314 prog->aux->user = get_current_user();
2315 prog->len = attr->insn_cnt;
2318 if (copy_from_bpfptr(prog->insns,
2319 make_bpfptr(attr->insns, uattr.is_kernel),
2320 bpf_prog_insn_size(prog)) != 0)
2323 prog->orig_prog = NULL;
2326 atomic64_set(&prog->aux->refcnt, 1);
2327 prog->gpl_compatible = is_gpl ? 1 : 0;
2329 if (bpf_prog_is_dev_bound(prog->aux)) {
2330 err = bpf_prog_offload_init(prog, attr);
2335 /* find program type: socket_filter vs tracing_filter */
2336 err = find_prog_type(type, prog);
2340 prog->aux->load_time = ktime_get_boottime_ns();
2341 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2342 sizeof(attr->prog_name));
2346 /* run eBPF verifier */
2347 err = bpf_check(&prog, attr, uattr);
2349 goto free_used_maps;
2351 prog = bpf_prog_select_runtime(prog, &err);
2353 goto free_used_maps;
2355 err = bpf_prog_alloc_id(prog);
2357 goto free_used_maps;
2359 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2360 * effectively publicly exposed. However, retrieving via
2361 * bpf_prog_get_fd_by_id() will take another reference,
2362 * therefore it cannot be gone underneath us.
2364 * Only for the time /after/ successful bpf_prog_new_fd()
2365 * and before returning to userspace, we might just hold
2366 * one reference and any parallel close on that fd could
2367 * rip everything out. Hence, below notifications must
2368 * happen before bpf_prog_new_fd().
2370 * Also, any failure handling from this point onwards must
2371 * be using bpf_prog_put() given the program is exposed.
2373 bpf_prog_kallsyms_add(prog);
2374 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2375 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2377 err = bpf_prog_new_fd(prog);
2383 /* In case we have subprogs, we need to wait for a grace
2384 * period before we can tear down JIT memory since symbols
2385 * are already exposed under kallsyms.
2387 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2390 free_uid(prog->aux->user);
2391 security_bpf_prog_free(prog->aux);
2393 if (prog->aux->attach_btf)
2394 btf_put(prog->aux->attach_btf);
2395 bpf_prog_free(prog);
2399 #define BPF_OBJ_LAST_FIELD file_flags
2401 static int bpf_obj_pin(const union bpf_attr *attr)
2403 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2406 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2409 static int bpf_obj_get(const union bpf_attr *attr)
2411 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2412 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2415 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2419 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2420 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2422 atomic64_set(&link->refcnt, 1);
2429 static void bpf_link_free_id(int id)
2434 spin_lock_bh(&link_idr_lock);
2435 idr_remove(&link_idr, id);
2436 spin_unlock_bh(&link_idr_lock);
2439 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2440 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2441 * anon_inode's release() call. This helper marksbpf_link as
2442 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2443 * is not decremented, it's the responsibility of a calling code that failed
2444 * to complete bpf_link initialization.
2446 void bpf_link_cleanup(struct bpf_link_primer *primer)
2448 primer->link->prog = NULL;
2449 bpf_link_free_id(primer->id);
2451 put_unused_fd(primer->fd);
2454 void bpf_link_inc(struct bpf_link *link)
2456 atomic64_inc(&link->refcnt);
2459 /* bpf_link_free is guaranteed to be called from process context */
2460 static void bpf_link_free(struct bpf_link *link)
2462 bpf_link_free_id(link->id);
2464 /* detach BPF program, clean up used resources */
2465 link->ops->release(link);
2466 bpf_prog_put(link->prog);
2468 /* free bpf_link and its containing memory */
2469 link->ops->dealloc(link);
2472 static void bpf_link_put_deferred(struct work_struct *work)
2474 struct bpf_link *link = container_of(work, struct bpf_link, work);
2476 bpf_link_free(link);
2479 /* bpf_link_put can be called from atomic context, but ensures that resources
2480 * are freed from process context
2482 void bpf_link_put(struct bpf_link *link)
2484 if (!atomic64_dec_and_test(&link->refcnt))
2488 INIT_WORK(&link->work, bpf_link_put_deferred);
2489 schedule_work(&link->work);
2491 bpf_link_free(link);
2495 static int bpf_link_release(struct inode *inode, struct file *filp)
2497 struct bpf_link *link = filp->private_data;
2503 #ifdef CONFIG_PROC_FS
2504 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2505 #define BPF_MAP_TYPE(_id, _ops)
2506 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2507 static const char *bpf_link_type_strs[] = {
2508 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2509 #include <linux/bpf_types.h>
2511 #undef BPF_PROG_TYPE
2513 #undef BPF_LINK_TYPE
2515 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2517 const struct bpf_link *link = filp->private_data;
2518 const struct bpf_prog *prog = link->prog;
2519 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2521 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2527 bpf_link_type_strs[link->type],
2531 if (link->ops->show_fdinfo)
2532 link->ops->show_fdinfo(link, m);
2536 static const struct file_operations bpf_link_fops = {
2537 #ifdef CONFIG_PROC_FS
2538 .show_fdinfo = bpf_link_show_fdinfo,
2540 .release = bpf_link_release,
2541 .read = bpf_dummy_read,
2542 .write = bpf_dummy_write,
2545 static int bpf_link_alloc_id(struct bpf_link *link)
2549 idr_preload(GFP_KERNEL);
2550 spin_lock_bh(&link_idr_lock);
2551 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2552 spin_unlock_bh(&link_idr_lock);
2558 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2559 * reserving unused FD and allocating ID from link_idr. This is to be paired
2560 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2561 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2562 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2563 * transient state is passed around in struct bpf_link_primer.
2564 * This is preferred way to create and initialize bpf_link, especially when
2565 * there are complicated and expensive operations inbetween creating bpf_link
2566 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2567 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2568 * expensive (and potentially failing) roll back operations in a rare case
2569 * that file, FD, or ID can't be allocated.
2571 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2576 fd = get_unused_fd_flags(O_CLOEXEC);
2581 id = bpf_link_alloc_id(link);
2587 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2589 bpf_link_free_id(id);
2591 return PTR_ERR(file);
2594 primer->link = link;
2595 primer->file = file;
2601 int bpf_link_settle(struct bpf_link_primer *primer)
2603 /* make bpf_link fetchable by ID */
2604 spin_lock_bh(&link_idr_lock);
2605 primer->link->id = primer->id;
2606 spin_unlock_bh(&link_idr_lock);
2607 /* make bpf_link fetchable by FD */
2608 fd_install(primer->fd, primer->file);
2609 /* pass through installed FD */
2613 int bpf_link_new_fd(struct bpf_link *link)
2615 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2618 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2620 struct fd f = fdget(ufd);
2621 struct bpf_link *link;
2624 return ERR_PTR(-EBADF);
2625 if (f.file->f_op != &bpf_link_fops) {
2627 return ERR_PTR(-EINVAL);
2630 link = f.file->private_data;
2637 struct bpf_tracing_link {
2638 struct bpf_link link;
2639 enum bpf_attach_type attach_type;
2640 struct bpf_trampoline *trampoline;
2641 struct bpf_prog *tgt_prog;
2644 static void bpf_tracing_link_release(struct bpf_link *link)
2646 struct bpf_tracing_link *tr_link =
2647 container_of(link, struct bpf_tracing_link, link);
2649 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2650 tr_link->trampoline));
2652 bpf_trampoline_put(tr_link->trampoline);
2654 /* tgt_prog is NULL if target is a kernel function */
2655 if (tr_link->tgt_prog)
2656 bpf_prog_put(tr_link->tgt_prog);
2659 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2661 struct bpf_tracing_link *tr_link =
2662 container_of(link, struct bpf_tracing_link, link);
2667 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2668 struct seq_file *seq)
2670 struct bpf_tracing_link *tr_link =
2671 container_of(link, struct bpf_tracing_link, link);
2674 "attach_type:\t%d\n",
2675 tr_link->attach_type);
2678 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2679 struct bpf_link_info *info)
2681 struct bpf_tracing_link *tr_link =
2682 container_of(link, struct bpf_tracing_link, link);
2684 info->tracing.attach_type = tr_link->attach_type;
2685 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2686 &info->tracing.target_obj_id,
2687 &info->tracing.target_btf_id);
2692 static const struct bpf_link_ops bpf_tracing_link_lops = {
2693 .release = bpf_tracing_link_release,
2694 .dealloc = bpf_tracing_link_dealloc,
2695 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2696 .fill_link_info = bpf_tracing_link_fill_link_info,
2699 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2703 struct bpf_link_primer link_primer;
2704 struct bpf_prog *tgt_prog = NULL;
2705 struct bpf_trampoline *tr = NULL;
2706 struct bpf_tracing_link *link;
2710 switch (prog->type) {
2711 case BPF_PROG_TYPE_TRACING:
2712 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2713 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2714 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2719 case BPF_PROG_TYPE_EXT:
2720 if (prog->expected_attach_type != 0) {
2725 case BPF_PROG_TYPE_LSM:
2726 if (prog->expected_attach_type != BPF_LSM_MAC) {
2736 if (!!tgt_prog_fd != !!btf_id) {
2742 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2743 if (prog->type != BPF_PROG_TYPE_EXT) {
2748 tgt_prog = bpf_prog_get(tgt_prog_fd);
2749 if (IS_ERR(tgt_prog)) {
2750 err = PTR_ERR(tgt_prog);
2755 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
2758 link = kzalloc(sizeof(*link), GFP_USER);
2763 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2764 &bpf_tracing_link_lops, prog);
2765 link->attach_type = prog->expected_attach_type;
2767 mutex_lock(&prog->aux->dst_mutex);
2769 /* There are a few possible cases here:
2771 * - if prog->aux->dst_trampoline is set, the program was just loaded
2772 * and not yet attached to anything, so we can use the values stored
2775 * - if prog->aux->dst_trampoline is NULL, the program has already been
2776 * attached to a target and its initial target was cleared (below)
2778 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2779 * target_btf_id using the link_create API.
2781 * - if tgt_prog == NULL when this function was called using the old
2782 * raw_tracepoint_open API, and we need a target from prog->aux
2784 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
2785 * was detached and is going for re-attachment.
2787 if (!prog->aux->dst_trampoline && !tgt_prog) {
2789 * Allow re-attach for TRACING and LSM programs. If it's
2790 * currently linked, bpf_trampoline_link_prog will fail.
2791 * EXT programs need to specify tgt_prog_fd, so they
2792 * re-attach in separate code path.
2794 if (prog->type != BPF_PROG_TYPE_TRACING &&
2795 prog->type != BPF_PROG_TYPE_LSM) {
2799 btf_id = prog->aux->attach_btf_id;
2800 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
2803 if (!prog->aux->dst_trampoline ||
2804 (key && key != prog->aux->dst_trampoline->key)) {
2805 /* If there is no saved target, or the specified target is
2806 * different from the destination specified at load time, we
2807 * need a new trampoline and a check for compatibility
2809 struct bpf_attach_target_info tgt_info = {};
2811 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2816 tr = bpf_trampoline_get(key, &tgt_info);
2822 /* The caller didn't specify a target, or the target was the
2823 * same as the destination supplied during program load. This
2824 * means we can reuse the trampoline and reference from program
2825 * load time, and there is no need to allocate a new one. This
2826 * can only happen once for any program, as the saved values in
2827 * prog->aux are cleared below.
2829 tr = prog->aux->dst_trampoline;
2830 tgt_prog = prog->aux->dst_prog;
2833 err = bpf_link_prime(&link->link, &link_primer);
2837 err = bpf_trampoline_link_prog(prog, tr);
2839 bpf_link_cleanup(&link_primer);
2844 link->tgt_prog = tgt_prog;
2845 link->trampoline = tr;
2847 /* Always clear the trampoline and target prog from prog->aux to make
2848 * sure the original attach destination is not kept alive after a
2849 * program is (re-)attached to another target.
2851 if (prog->aux->dst_prog &&
2852 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2853 /* got extra prog ref from syscall, or attaching to different prog */
2854 bpf_prog_put(prog->aux->dst_prog);
2855 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2856 /* we allocated a new trampoline, so free the old one */
2857 bpf_trampoline_put(prog->aux->dst_trampoline);
2859 prog->aux->dst_prog = NULL;
2860 prog->aux->dst_trampoline = NULL;
2861 mutex_unlock(&prog->aux->dst_mutex);
2863 return bpf_link_settle(&link_primer);
2865 if (tr && tr != prog->aux->dst_trampoline)
2866 bpf_trampoline_put(tr);
2867 mutex_unlock(&prog->aux->dst_mutex);
2870 if (tgt_prog_fd && tgt_prog)
2871 bpf_prog_put(tgt_prog);
2875 struct bpf_raw_tp_link {
2876 struct bpf_link link;
2877 struct bpf_raw_event_map *btp;
2880 static void bpf_raw_tp_link_release(struct bpf_link *link)
2882 struct bpf_raw_tp_link *raw_tp =
2883 container_of(link, struct bpf_raw_tp_link, link);
2885 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2886 bpf_put_raw_tracepoint(raw_tp->btp);
2889 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2891 struct bpf_raw_tp_link *raw_tp =
2892 container_of(link, struct bpf_raw_tp_link, link);
2897 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2898 struct seq_file *seq)
2900 struct bpf_raw_tp_link *raw_tp_link =
2901 container_of(link, struct bpf_raw_tp_link, link);
2905 raw_tp_link->btp->tp->name);
2908 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2909 struct bpf_link_info *info)
2911 struct bpf_raw_tp_link *raw_tp_link =
2912 container_of(link, struct bpf_raw_tp_link, link);
2913 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2914 const char *tp_name = raw_tp_link->btp->tp->name;
2915 u32 ulen = info->raw_tracepoint.tp_name_len;
2916 size_t tp_len = strlen(tp_name);
2921 info->raw_tracepoint.tp_name_len = tp_len + 1;
2926 if (ulen >= tp_len + 1) {
2927 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2932 if (copy_to_user(ubuf, tp_name, ulen - 1))
2934 if (put_user(zero, ubuf + ulen - 1))
2942 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
2943 .release = bpf_raw_tp_link_release,
2944 .dealloc = bpf_raw_tp_link_dealloc,
2945 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2946 .fill_link_info = bpf_raw_tp_link_fill_link_info,
2949 #ifdef CONFIG_PERF_EVENTS
2950 struct bpf_perf_link {
2951 struct bpf_link link;
2952 struct file *perf_file;
2955 static void bpf_perf_link_release(struct bpf_link *link)
2957 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
2958 struct perf_event *event = perf_link->perf_file->private_data;
2960 perf_event_free_bpf_prog(event);
2961 fput(perf_link->perf_file);
2964 static void bpf_perf_link_dealloc(struct bpf_link *link)
2966 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
2971 static const struct bpf_link_ops bpf_perf_link_lops = {
2972 .release = bpf_perf_link_release,
2973 .dealloc = bpf_perf_link_dealloc,
2976 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2978 struct bpf_link_primer link_primer;
2979 struct bpf_perf_link *link;
2980 struct perf_event *event;
2981 struct file *perf_file;
2984 if (attr->link_create.flags)
2987 perf_file = perf_event_get(attr->link_create.target_fd);
2988 if (IS_ERR(perf_file))
2989 return PTR_ERR(perf_file);
2991 link = kzalloc(sizeof(*link), GFP_USER);
2996 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog);
2997 link->perf_file = perf_file;
2999 err = bpf_link_prime(&link->link, &link_primer);
3005 event = perf_file->private_data;
3006 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
3008 bpf_link_cleanup(&link_primer);
3011 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
3014 return bpf_link_settle(&link_primer);
3020 #endif /* CONFIG_PERF_EVENTS */
3022 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
3024 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
3026 struct bpf_link_primer link_primer;
3027 struct bpf_raw_tp_link *link;
3028 struct bpf_raw_event_map *btp;
3029 struct bpf_prog *prog;
3030 const char *tp_name;
3034 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
3037 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
3039 return PTR_ERR(prog);
3041 switch (prog->type) {
3042 case BPF_PROG_TYPE_TRACING:
3043 case BPF_PROG_TYPE_EXT:
3044 case BPF_PROG_TYPE_LSM:
3045 if (attr->raw_tracepoint.name) {
3046 /* The attach point for this category of programs
3047 * should be specified via btf_id during program load.
3052 if (prog->type == BPF_PROG_TYPE_TRACING &&
3053 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
3054 tp_name = prog->aux->attach_func_name;
3057 err = bpf_tracing_prog_attach(prog, 0, 0);
3061 case BPF_PROG_TYPE_RAW_TRACEPOINT:
3062 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
3063 if (strncpy_from_user(buf,
3064 u64_to_user_ptr(attr->raw_tracepoint.name),
3065 sizeof(buf) - 1) < 0) {
3069 buf[sizeof(buf) - 1] = 0;
3077 btp = bpf_get_raw_tracepoint(tp_name);
3083 link = kzalloc(sizeof(*link), GFP_USER);
3088 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
3089 &bpf_raw_tp_link_lops, prog);
3092 err = bpf_link_prime(&link->link, &link_primer);
3098 err = bpf_probe_register(link->btp, prog);
3100 bpf_link_cleanup(&link_primer);
3104 return bpf_link_settle(&link_primer);
3107 bpf_put_raw_tracepoint(btp);
3113 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
3114 enum bpf_attach_type attach_type)
3116 switch (prog->type) {
3117 case BPF_PROG_TYPE_CGROUP_SOCK:
3118 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3119 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3120 case BPF_PROG_TYPE_SK_LOOKUP:
3121 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
3122 case BPF_PROG_TYPE_CGROUP_SKB:
3123 if (!capable(CAP_NET_ADMIN))
3124 /* cg-skb progs can be loaded by unpriv user.
3125 * check permissions at attach time.
3128 return prog->enforce_expected_attach_type &&
3129 prog->expected_attach_type != attach_type ?
3136 static enum bpf_prog_type
3137 attach_type_to_prog_type(enum bpf_attach_type attach_type)
3139 switch (attach_type) {
3140 case BPF_CGROUP_INET_INGRESS:
3141 case BPF_CGROUP_INET_EGRESS:
3142 return BPF_PROG_TYPE_CGROUP_SKB;
3143 case BPF_CGROUP_INET_SOCK_CREATE:
3144 case BPF_CGROUP_INET_SOCK_RELEASE:
3145 case BPF_CGROUP_INET4_POST_BIND:
3146 case BPF_CGROUP_INET6_POST_BIND:
3147 return BPF_PROG_TYPE_CGROUP_SOCK;
3148 case BPF_CGROUP_INET4_BIND:
3149 case BPF_CGROUP_INET6_BIND:
3150 case BPF_CGROUP_INET4_CONNECT:
3151 case BPF_CGROUP_INET6_CONNECT:
3152 case BPF_CGROUP_INET4_GETPEERNAME:
3153 case BPF_CGROUP_INET6_GETPEERNAME:
3154 case BPF_CGROUP_INET4_GETSOCKNAME:
3155 case BPF_CGROUP_INET6_GETSOCKNAME:
3156 case BPF_CGROUP_UDP4_SENDMSG:
3157 case BPF_CGROUP_UDP6_SENDMSG:
3158 case BPF_CGROUP_UDP4_RECVMSG:
3159 case BPF_CGROUP_UDP6_RECVMSG:
3160 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3161 case BPF_CGROUP_SOCK_OPS:
3162 return BPF_PROG_TYPE_SOCK_OPS;
3163 case BPF_CGROUP_DEVICE:
3164 return BPF_PROG_TYPE_CGROUP_DEVICE;
3165 case BPF_SK_MSG_VERDICT:
3166 return BPF_PROG_TYPE_SK_MSG;
3167 case BPF_SK_SKB_STREAM_PARSER:
3168 case BPF_SK_SKB_STREAM_VERDICT:
3169 case BPF_SK_SKB_VERDICT:
3170 return BPF_PROG_TYPE_SK_SKB;
3171 case BPF_LIRC_MODE2:
3172 return BPF_PROG_TYPE_LIRC_MODE2;
3173 case BPF_FLOW_DISSECTOR:
3174 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3175 case BPF_CGROUP_SYSCTL:
3176 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3177 case BPF_CGROUP_GETSOCKOPT:
3178 case BPF_CGROUP_SETSOCKOPT:
3179 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
3180 case BPF_TRACE_ITER:
3181 return BPF_PROG_TYPE_TRACING;
3183 return BPF_PROG_TYPE_SK_LOOKUP;
3185 return BPF_PROG_TYPE_XDP;
3187 return BPF_PROG_TYPE_UNSPEC;
3191 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
3193 #define BPF_F_ATTACH_MASK \
3194 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
3196 static int bpf_prog_attach(const union bpf_attr *attr)
3198 enum bpf_prog_type ptype;
3199 struct bpf_prog *prog;
3202 if (CHECK_ATTR(BPF_PROG_ATTACH))
3205 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
3208 ptype = attach_type_to_prog_type(attr->attach_type);
3209 if (ptype == BPF_PROG_TYPE_UNSPEC)
3212 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3214 return PTR_ERR(prog);
3216 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3222 case BPF_PROG_TYPE_SK_SKB:
3223 case BPF_PROG_TYPE_SK_MSG:
3224 ret = sock_map_get_from_fd(attr, prog);
3226 case BPF_PROG_TYPE_LIRC_MODE2:
3227 ret = lirc_prog_attach(attr, prog);
3229 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3230 ret = netns_bpf_prog_attach(attr, prog);
3232 case BPF_PROG_TYPE_CGROUP_DEVICE:
3233 case BPF_PROG_TYPE_CGROUP_SKB:
3234 case BPF_PROG_TYPE_CGROUP_SOCK:
3235 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3236 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3237 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3238 case BPF_PROG_TYPE_SOCK_OPS:
3239 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3250 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3252 static int bpf_prog_detach(const union bpf_attr *attr)
3254 enum bpf_prog_type ptype;
3256 if (CHECK_ATTR(BPF_PROG_DETACH))
3259 ptype = attach_type_to_prog_type(attr->attach_type);
3262 case BPF_PROG_TYPE_SK_MSG:
3263 case BPF_PROG_TYPE_SK_SKB:
3264 return sock_map_prog_detach(attr, ptype);
3265 case BPF_PROG_TYPE_LIRC_MODE2:
3266 return lirc_prog_detach(attr);
3267 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3268 return netns_bpf_prog_detach(attr, ptype);
3269 case BPF_PROG_TYPE_CGROUP_DEVICE:
3270 case BPF_PROG_TYPE_CGROUP_SKB:
3271 case BPF_PROG_TYPE_CGROUP_SOCK:
3272 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3273 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3274 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3275 case BPF_PROG_TYPE_SOCK_OPS:
3276 return cgroup_bpf_prog_detach(attr, ptype);
3282 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3284 static int bpf_prog_query(const union bpf_attr *attr,
3285 union bpf_attr __user *uattr)
3287 if (!capable(CAP_NET_ADMIN))
3289 if (CHECK_ATTR(BPF_PROG_QUERY))
3291 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3294 switch (attr->query.attach_type) {
3295 case BPF_CGROUP_INET_INGRESS:
3296 case BPF_CGROUP_INET_EGRESS:
3297 case BPF_CGROUP_INET_SOCK_CREATE:
3298 case BPF_CGROUP_INET_SOCK_RELEASE:
3299 case BPF_CGROUP_INET4_BIND:
3300 case BPF_CGROUP_INET6_BIND:
3301 case BPF_CGROUP_INET4_POST_BIND:
3302 case BPF_CGROUP_INET6_POST_BIND:
3303 case BPF_CGROUP_INET4_CONNECT:
3304 case BPF_CGROUP_INET6_CONNECT:
3305 case BPF_CGROUP_INET4_GETPEERNAME:
3306 case BPF_CGROUP_INET6_GETPEERNAME:
3307 case BPF_CGROUP_INET4_GETSOCKNAME:
3308 case BPF_CGROUP_INET6_GETSOCKNAME:
3309 case BPF_CGROUP_UDP4_SENDMSG:
3310 case BPF_CGROUP_UDP6_SENDMSG:
3311 case BPF_CGROUP_UDP4_RECVMSG:
3312 case BPF_CGROUP_UDP6_RECVMSG:
3313 case BPF_CGROUP_SOCK_OPS:
3314 case BPF_CGROUP_DEVICE:
3315 case BPF_CGROUP_SYSCTL:
3316 case BPF_CGROUP_GETSOCKOPT:
3317 case BPF_CGROUP_SETSOCKOPT:
3318 return cgroup_bpf_prog_query(attr, uattr);
3319 case BPF_LIRC_MODE2:
3320 return lirc_prog_query(attr, uattr);
3321 case BPF_FLOW_DISSECTOR:
3323 return netns_bpf_prog_query(attr, uattr);
3329 #define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
3331 static int bpf_prog_test_run(const union bpf_attr *attr,
3332 union bpf_attr __user *uattr)
3334 struct bpf_prog *prog;
3335 int ret = -ENOTSUPP;
3337 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3340 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3341 (!attr->test.ctx_size_in && attr->test.ctx_in))
3344 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3345 (!attr->test.ctx_size_out && attr->test.ctx_out))
3348 prog = bpf_prog_get(attr->test.prog_fd);
3350 return PTR_ERR(prog);
3352 if (prog->aux->ops->test_run)
3353 ret = prog->aux->ops->test_run(prog, attr, uattr);
3359 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3361 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3362 union bpf_attr __user *uattr,
3366 u32 next_id = attr->start_id;
3369 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3372 if (!capable(CAP_SYS_ADMIN))
3377 if (!idr_get_next(idr, &next_id))
3379 spin_unlock_bh(lock);
3382 err = put_user(next_id, &uattr->next_id);
3387 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3389 struct bpf_map *map;
3391 spin_lock_bh(&map_idr_lock);
3393 map = idr_get_next(&map_idr, id);
3395 map = __bpf_map_inc_not_zero(map, false);
3401 spin_unlock_bh(&map_idr_lock);
3406 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3408 struct bpf_prog *prog;
3410 spin_lock_bh(&prog_idr_lock);
3412 prog = idr_get_next(&prog_idr, id);
3414 prog = bpf_prog_inc_not_zero(prog);
3420 spin_unlock_bh(&prog_idr_lock);
3425 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3427 struct bpf_prog *bpf_prog_by_id(u32 id)
3429 struct bpf_prog *prog;
3432 return ERR_PTR(-ENOENT);
3434 spin_lock_bh(&prog_idr_lock);
3435 prog = idr_find(&prog_idr, id);
3437 prog = bpf_prog_inc_not_zero(prog);
3439 prog = ERR_PTR(-ENOENT);
3440 spin_unlock_bh(&prog_idr_lock);
3444 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3446 struct bpf_prog *prog;
3447 u32 id = attr->prog_id;
3450 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3453 if (!capable(CAP_SYS_ADMIN))
3456 prog = bpf_prog_by_id(id);
3458 return PTR_ERR(prog);
3460 fd = bpf_prog_new_fd(prog);
3467 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3469 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3471 struct bpf_map *map;
3472 u32 id = attr->map_id;
3476 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3477 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3480 if (!capable(CAP_SYS_ADMIN))
3483 f_flags = bpf_get_file_flag(attr->open_flags);
3487 spin_lock_bh(&map_idr_lock);
3488 map = idr_find(&map_idr, id);
3490 map = __bpf_map_inc_not_zero(map, true);
3492 map = ERR_PTR(-ENOENT);
3493 spin_unlock_bh(&map_idr_lock);
3496 return PTR_ERR(map);
3498 fd = bpf_map_new_fd(map, f_flags);
3500 bpf_map_put_with_uref(map);
3505 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3506 unsigned long addr, u32 *off,
3509 const struct bpf_map *map;
3512 mutex_lock(&prog->aux->used_maps_mutex);
3513 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3514 map = prog->aux->used_maps[i];
3515 if (map == (void *)addr) {
3516 *type = BPF_PSEUDO_MAP_FD;
3519 if (!map->ops->map_direct_value_meta)
3521 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3522 *type = BPF_PSEUDO_MAP_VALUE;
3529 mutex_unlock(&prog->aux->used_maps_mutex);
3533 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3534 const struct cred *f_cred)
3536 const struct bpf_map *map;
3537 struct bpf_insn *insns;
3543 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3548 for (i = 0; i < prog->len; i++) {
3549 code = insns[i].code;
3551 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3552 insns[i].code = BPF_JMP | BPF_CALL;
3553 insns[i].imm = BPF_FUNC_tail_call;
3556 if (code == (BPF_JMP | BPF_CALL) ||
3557 code == (BPF_JMP | BPF_CALL_ARGS)) {
3558 if (code == (BPF_JMP | BPF_CALL_ARGS))
3559 insns[i].code = BPF_JMP | BPF_CALL;
3560 if (!bpf_dump_raw_ok(f_cred))
3564 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3565 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3569 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3572 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3573 map = bpf_map_from_imm(prog, imm, &off, &type);
3575 insns[i].src_reg = type;
3576 insns[i].imm = map->id;
3577 insns[i + 1].imm = off;
3585 static int set_info_rec_size(struct bpf_prog_info *info)
3588 * Ensure info.*_rec_size is the same as kernel expected size
3592 * Only allow zero *_rec_size if both _rec_size and _cnt are
3593 * zero. In this case, the kernel will set the expected
3594 * _rec_size back to the info.
3597 if ((info->nr_func_info || info->func_info_rec_size) &&
3598 info->func_info_rec_size != sizeof(struct bpf_func_info))
3601 if ((info->nr_line_info || info->line_info_rec_size) &&
3602 info->line_info_rec_size != sizeof(struct bpf_line_info))
3605 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3606 info->jited_line_info_rec_size != sizeof(__u64))
3609 info->func_info_rec_size = sizeof(struct bpf_func_info);
3610 info->line_info_rec_size = sizeof(struct bpf_line_info);
3611 info->jited_line_info_rec_size = sizeof(__u64);
3616 static int bpf_prog_get_info_by_fd(struct file *file,
3617 struct bpf_prog *prog,
3618 const union bpf_attr *attr,
3619 union bpf_attr __user *uattr)
3621 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3622 struct bpf_prog_info info;
3623 u32 info_len = attr->info.info_len;
3624 struct bpf_prog_kstats stats;
3625 char __user *uinsns;
3629 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3632 info_len = min_t(u32, sizeof(info), info_len);
3634 memset(&info, 0, sizeof(info));
3635 if (copy_from_user(&info, uinfo, info_len))
3638 info.type = prog->type;
3639 info.id = prog->aux->id;
3640 info.load_time = prog->aux->load_time;
3641 info.created_by_uid = from_kuid_munged(current_user_ns(),
3642 prog->aux->user->uid);
3643 info.gpl_compatible = prog->gpl_compatible;
3645 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3646 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3648 mutex_lock(&prog->aux->used_maps_mutex);
3649 ulen = info.nr_map_ids;
3650 info.nr_map_ids = prog->aux->used_map_cnt;
3651 ulen = min_t(u32, info.nr_map_ids, ulen);
3653 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3656 for (i = 0; i < ulen; i++)
3657 if (put_user(prog->aux->used_maps[i]->id,
3658 &user_map_ids[i])) {
3659 mutex_unlock(&prog->aux->used_maps_mutex);
3663 mutex_unlock(&prog->aux->used_maps_mutex);
3665 err = set_info_rec_size(&info);
3669 bpf_prog_get_stats(prog, &stats);
3670 info.run_time_ns = stats.nsecs;
3671 info.run_cnt = stats.cnt;
3672 info.recursion_misses = stats.misses;
3674 info.verified_insns = prog->aux->verified_insns;
3676 if (!bpf_capable()) {
3677 info.jited_prog_len = 0;
3678 info.xlated_prog_len = 0;
3679 info.nr_jited_ksyms = 0;
3680 info.nr_jited_func_lens = 0;
3681 info.nr_func_info = 0;
3682 info.nr_line_info = 0;
3683 info.nr_jited_line_info = 0;
3687 ulen = info.xlated_prog_len;
3688 info.xlated_prog_len = bpf_prog_insn_size(prog);
3689 if (info.xlated_prog_len && ulen) {
3690 struct bpf_insn *insns_sanitized;
3693 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3694 info.xlated_prog_insns = 0;
3697 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3698 if (!insns_sanitized)
3700 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3701 ulen = min_t(u32, info.xlated_prog_len, ulen);
3702 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3703 kfree(insns_sanitized);
3708 if (bpf_prog_is_dev_bound(prog->aux)) {
3709 err = bpf_prog_offload_info_fill(&info, prog);
3715 /* NOTE: the following code is supposed to be skipped for offload.
3716 * bpf_prog_offload_info_fill() is the place to fill similar fields
3719 ulen = info.jited_prog_len;
3720 if (prog->aux->func_cnt) {
3723 info.jited_prog_len = 0;
3724 for (i = 0; i < prog->aux->func_cnt; i++)
3725 info.jited_prog_len += prog->aux->func[i]->jited_len;
3727 info.jited_prog_len = prog->jited_len;
3730 if (info.jited_prog_len && ulen) {
3731 if (bpf_dump_raw_ok(file->f_cred)) {
3732 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3733 ulen = min_t(u32, info.jited_prog_len, ulen);
3735 /* for multi-function programs, copy the JITed
3736 * instructions for all the functions
3738 if (prog->aux->func_cnt) {
3743 for (i = 0; i < prog->aux->func_cnt; i++) {
3744 len = prog->aux->func[i]->jited_len;
3745 len = min_t(u32, len, free);
3746 img = (u8 *) prog->aux->func[i]->bpf_func;
3747 if (copy_to_user(uinsns, img, len))
3755 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3759 info.jited_prog_insns = 0;
3763 ulen = info.nr_jited_ksyms;
3764 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
3766 if (bpf_dump_raw_ok(file->f_cred)) {
3767 unsigned long ksym_addr;
3768 u64 __user *user_ksyms;
3771 /* copy the address of the kernel symbol
3772 * corresponding to each function
3774 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3775 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3776 if (prog->aux->func_cnt) {
3777 for (i = 0; i < ulen; i++) {
3778 ksym_addr = (unsigned long)
3779 prog->aux->func[i]->bpf_func;
3780 if (put_user((u64) ksym_addr,
3785 ksym_addr = (unsigned long) prog->bpf_func;
3786 if (put_user((u64) ksym_addr, &user_ksyms[0]))
3790 info.jited_ksyms = 0;
3794 ulen = info.nr_jited_func_lens;
3795 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
3797 if (bpf_dump_raw_ok(file->f_cred)) {
3798 u32 __user *user_lens;
3801 /* copy the JITed image lengths for each function */
3802 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3803 user_lens = u64_to_user_ptr(info.jited_func_lens);
3804 if (prog->aux->func_cnt) {
3805 for (i = 0; i < ulen; i++) {
3807 prog->aux->func[i]->jited_len;
3808 if (put_user(func_len, &user_lens[i]))
3812 func_len = prog->jited_len;
3813 if (put_user(func_len, &user_lens[0]))
3817 info.jited_func_lens = 0;
3822 info.btf_id = btf_obj_id(prog->aux->btf);
3824 ulen = info.nr_func_info;
3825 info.nr_func_info = prog->aux->func_info_cnt;
3826 if (info.nr_func_info && ulen) {
3827 char __user *user_finfo;
3829 user_finfo = u64_to_user_ptr(info.func_info);
3830 ulen = min_t(u32, info.nr_func_info, ulen);
3831 if (copy_to_user(user_finfo, prog->aux->func_info,
3832 info.func_info_rec_size * ulen))
3836 ulen = info.nr_line_info;
3837 info.nr_line_info = prog->aux->nr_linfo;
3838 if (info.nr_line_info && ulen) {
3839 __u8 __user *user_linfo;
3841 user_linfo = u64_to_user_ptr(info.line_info);
3842 ulen = min_t(u32, info.nr_line_info, ulen);
3843 if (copy_to_user(user_linfo, prog->aux->linfo,
3844 info.line_info_rec_size * ulen))
3848 ulen = info.nr_jited_line_info;
3849 if (prog->aux->jited_linfo)
3850 info.nr_jited_line_info = prog->aux->nr_linfo;
3852 info.nr_jited_line_info = 0;
3853 if (info.nr_jited_line_info && ulen) {
3854 if (bpf_dump_raw_ok(file->f_cred)) {
3855 __u64 __user *user_linfo;
3858 user_linfo = u64_to_user_ptr(info.jited_line_info);
3859 ulen = min_t(u32, info.nr_jited_line_info, ulen);
3860 for (i = 0; i < ulen; i++) {
3861 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3866 info.jited_line_info = 0;
3870 ulen = info.nr_prog_tags;
3871 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3873 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3876 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3877 ulen = min_t(u32, info.nr_prog_tags, ulen);
3878 if (prog->aux->func_cnt) {
3879 for (i = 0; i < ulen; i++) {
3880 if (copy_to_user(user_prog_tags[i],
3881 prog->aux->func[i]->tag,
3886 if (copy_to_user(user_prog_tags[0],
3887 prog->tag, BPF_TAG_SIZE))
3893 if (copy_to_user(uinfo, &info, info_len) ||
3894 put_user(info_len, &uattr->info.info_len))
3900 static int bpf_map_get_info_by_fd(struct file *file,
3901 struct bpf_map *map,
3902 const union bpf_attr *attr,
3903 union bpf_attr __user *uattr)
3905 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3906 struct bpf_map_info info;
3907 u32 info_len = attr->info.info_len;
3910 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3913 info_len = min_t(u32, sizeof(info), info_len);
3915 memset(&info, 0, sizeof(info));
3916 info.type = map->map_type;
3918 info.key_size = map->key_size;
3919 info.value_size = map->value_size;
3920 info.max_entries = map->max_entries;
3921 info.map_flags = map->map_flags;
3922 info.map_extra = map->map_extra;
3923 memcpy(info.name, map->name, sizeof(map->name));
3926 info.btf_id = btf_obj_id(map->btf);
3927 info.btf_key_type_id = map->btf_key_type_id;
3928 info.btf_value_type_id = map->btf_value_type_id;
3930 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
3932 if (bpf_map_is_dev_bound(map)) {
3933 err = bpf_map_offload_info_fill(&info, map);
3938 if (copy_to_user(uinfo, &info, info_len) ||
3939 put_user(info_len, &uattr->info.info_len))
3945 static int bpf_btf_get_info_by_fd(struct file *file,
3947 const union bpf_attr *attr,
3948 union bpf_attr __user *uattr)
3950 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3951 u32 info_len = attr->info.info_len;
3954 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
3958 return btf_get_info_by_fd(btf, attr, uattr);
3961 static int bpf_link_get_info_by_fd(struct file *file,
3962 struct bpf_link *link,
3963 const union bpf_attr *attr,
3964 union bpf_attr __user *uattr)
3966 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3967 struct bpf_link_info info;
3968 u32 info_len = attr->info.info_len;
3971 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3974 info_len = min_t(u32, sizeof(info), info_len);
3976 memset(&info, 0, sizeof(info));
3977 if (copy_from_user(&info, uinfo, info_len))
3980 info.type = link->type;
3982 info.prog_id = link->prog->aux->id;
3984 if (link->ops->fill_link_info) {
3985 err = link->ops->fill_link_info(link, &info);
3990 if (copy_to_user(uinfo, &info, info_len) ||
3991 put_user(info_len, &uattr->info.info_len))
3998 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
4000 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
4001 union bpf_attr __user *uattr)
4003 int ufd = attr->info.bpf_fd;
4007 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
4014 if (f.file->f_op == &bpf_prog_fops)
4015 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
4017 else if (f.file->f_op == &bpf_map_fops)
4018 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
4020 else if (f.file->f_op == &btf_fops)
4021 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
4022 else if (f.file->f_op == &bpf_link_fops)
4023 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
4032 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
4034 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
4036 if (CHECK_ATTR(BPF_BTF_LOAD))
4042 return btf_new_fd(attr, uattr);
4045 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
4047 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
4049 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
4052 if (!capable(CAP_SYS_ADMIN))
4055 return btf_get_fd_by_id(attr->btf_id);
4058 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
4059 union bpf_attr __user *uattr,
4060 u32 prog_id, u32 fd_type,
4061 const char *buf, u64 probe_offset,
4064 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
4065 u32 len = buf ? strlen(buf) : 0, input_len;
4068 if (put_user(len, &uattr->task_fd_query.buf_len))
4070 input_len = attr->task_fd_query.buf_len;
4071 if (input_len && ubuf) {
4073 /* nothing to copy, just make ubuf NULL terminated */
4076 if (put_user(zero, ubuf))
4078 } else if (input_len >= len + 1) {
4079 /* ubuf can hold the string with NULL terminator */
4080 if (copy_to_user(ubuf, buf, len + 1))
4083 /* ubuf cannot hold the string with NULL terminator,
4084 * do a partial copy with NULL terminator.
4089 if (copy_to_user(ubuf, buf, input_len - 1))
4091 if (put_user(zero, ubuf + input_len - 1))
4096 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
4097 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
4098 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
4099 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
4105 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
4107 static int bpf_task_fd_query(const union bpf_attr *attr,
4108 union bpf_attr __user *uattr)
4110 pid_t pid = attr->task_fd_query.pid;
4111 u32 fd = attr->task_fd_query.fd;
4112 const struct perf_event *event;
4113 struct task_struct *task;
4117 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
4120 if (!capable(CAP_SYS_ADMIN))
4123 if (attr->task_fd_query.flags != 0)
4126 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
4131 file = fget_task(task, fd);
4132 put_task_struct(task);
4136 if (file->f_op == &bpf_link_fops) {
4137 struct bpf_link *link = file->private_data;
4139 if (link->ops == &bpf_raw_tp_link_lops) {
4140 struct bpf_raw_tp_link *raw_tp =
4141 container_of(link, struct bpf_raw_tp_link, link);
4142 struct bpf_raw_event_map *btp = raw_tp->btp;
4144 err = bpf_task_fd_query_copy(attr, uattr,
4145 raw_tp->link.prog->aux->id,
4146 BPF_FD_TYPE_RAW_TRACEPOINT,
4147 btp->tp->name, 0, 0);
4153 event = perf_get_event(file);
4154 if (!IS_ERR(event)) {
4155 u64 probe_offset, probe_addr;
4156 u32 prog_id, fd_type;
4159 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4160 &buf, &probe_offset,
4163 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4177 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
4179 #define BPF_DO_BATCH(fn) \
4185 err = fn(map, attr, uattr); \
4188 static int bpf_map_do_batch(const union bpf_attr *attr,
4189 union bpf_attr __user *uattr,
4192 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH ||
4193 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
4194 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
4195 struct bpf_map *map;
4199 if (CHECK_ATTR(BPF_MAP_BATCH))
4202 ufd = attr->batch.map_fd;
4204 map = __bpf_map_get(f);
4206 return PTR_ERR(map);
4208 bpf_map_write_active_inc(map);
4209 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4213 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4218 if (cmd == BPF_MAP_LOOKUP_BATCH)
4219 BPF_DO_BATCH(map->ops->map_lookup_batch);
4220 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4221 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4222 else if (cmd == BPF_MAP_UPDATE_BATCH)
4223 BPF_DO_BATCH(map->ops->map_update_batch);
4225 BPF_DO_BATCH(map->ops->map_delete_batch);
4228 bpf_map_write_active_dec(map);
4233 static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
4234 struct bpf_prog *prog)
4236 if (attr->link_create.attach_type != prog->expected_attach_type)
4239 if (prog->expected_attach_type == BPF_TRACE_ITER)
4240 return bpf_iter_link_attach(attr, uattr, prog);
4241 else if (prog->type == BPF_PROG_TYPE_EXT)
4242 return bpf_tracing_prog_attach(prog,
4243 attr->link_create.target_fd,
4244 attr->link_create.target_btf_id);
4248 #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
4249 static int link_create(union bpf_attr *attr, bpfptr_t uattr)
4251 enum bpf_prog_type ptype;
4252 struct bpf_prog *prog;
4255 if (CHECK_ATTR(BPF_LINK_CREATE))
4258 prog = bpf_prog_get(attr->link_create.prog_fd);
4260 return PTR_ERR(prog);
4262 ret = bpf_prog_attach_check_attach_type(prog,
4263 attr->link_create.attach_type);
4267 switch (prog->type) {
4268 case BPF_PROG_TYPE_EXT:
4269 ret = tracing_bpf_link_attach(attr, uattr, prog);
4271 case BPF_PROG_TYPE_PERF_EVENT:
4272 case BPF_PROG_TYPE_KPROBE:
4273 case BPF_PROG_TYPE_TRACEPOINT:
4274 if (attr->link_create.attach_type != BPF_PERF_EVENT) {
4281 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4282 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4290 case BPF_PROG_TYPE_CGROUP_SKB:
4291 case BPF_PROG_TYPE_CGROUP_SOCK:
4292 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4293 case BPF_PROG_TYPE_SOCK_OPS:
4294 case BPF_PROG_TYPE_CGROUP_DEVICE:
4295 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4296 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4297 ret = cgroup_bpf_link_attach(attr, prog);
4299 case BPF_PROG_TYPE_TRACING:
4300 ret = tracing_bpf_link_attach(attr, uattr, prog);
4302 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4303 case BPF_PROG_TYPE_SK_LOOKUP:
4304 ret = netns_bpf_link_create(attr, prog);
4307 case BPF_PROG_TYPE_XDP:
4308 ret = bpf_xdp_link_attach(attr, prog);
4311 #ifdef CONFIG_PERF_EVENTS
4312 case BPF_PROG_TYPE_PERF_EVENT:
4313 case BPF_PROG_TYPE_TRACEPOINT:
4314 case BPF_PROG_TYPE_KPROBE:
4315 ret = bpf_perf_link_attach(attr, prog);
4328 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4330 static int link_update(union bpf_attr *attr)
4332 struct bpf_prog *old_prog = NULL, *new_prog;
4333 struct bpf_link *link;
4337 if (CHECK_ATTR(BPF_LINK_UPDATE))
4340 flags = attr->link_update.flags;
4341 if (flags & ~BPF_F_REPLACE)
4344 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4346 return PTR_ERR(link);
4348 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4349 if (IS_ERR(new_prog)) {
4350 ret = PTR_ERR(new_prog);
4354 if (flags & BPF_F_REPLACE) {
4355 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4356 if (IS_ERR(old_prog)) {
4357 ret = PTR_ERR(old_prog);
4361 } else if (attr->link_update.old_prog_fd) {
4366 if (link->ops->update_prog)
4367 ret = link->ops->update_prog(link, new_prog, old_prog);
4373 bpf_prog_put(old_prog);
4375 bpf_prog_put(new_prog);
4381 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4383 static int link_detach(union bpf_attr *attr)
4385 struct bpf_link *link;
4388 if (CHECK_ATTR(BPF_LINK_DETACH))
4391 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4393 return PTR_ERR(link);
4395 if (link->ops->detach)
4396 ret = link->ops->detach(link);
4404 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4406 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4409 struct bpf_link *bpf_link_by_id(u32 id)
4411 struct bpf_link *link;
4414 return ERR_PTR(-ENOENT);
4416 spin_lock_bh(&link_idr_lock);
4417 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4418 link = idr_find(&link_idr, id);
4421 link = bpf_link_inc_not_zero(link);
4423 link = ERR_PTR(-EAGAIN);
4425 link = ERR_PTR(-ENOENT);
4427 spin_unlock_bh(&link_idr_lock);
4431 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4433 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4435 struct bpf_link *link;
4436 u32 id = attr->link_id;
4439 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4442 if (!capable(CAP_SYS_ADMIN))
4445 link = bpf_link_by_id(id);
4447 return PTR_ERR(link);
4449 fd = bpf_link_new_fd(link);
4456 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4458 static int bpf_stats_release(struct inode *inode, struct file *file)
4460 mutex_lock(&bpf_stats_enabled_mutex);
4461 static_key_slow_dec(&bpf_stats_enabled_key.key);
4462 mutex_unlock(&bpf_stats_enabled_mutex);
4466 static const struct file_operations bpf_stats_fops = {
4467 .release = bpf_stats_release,
4470 static int bpf_enable_runtime_stats(void)
4474 mutex_lock(&bpf_stats_enabled_mutex);
4476 /* Set a very high limit to avoid overflow */
4477 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4478 mutex_unlock(&bpf_stats_enabled_mutex);
4482 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4484 static_key_slow_inc(&bpf_stats_enabled_key.key);
4486 mutex_unlock(&bpf_stats_enabled_mutex);
4490 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4492 static int bpf_enable_stats(union bpf_attr *attr)
4495 if (CHECK_ATTR(BPF_ENABLE_STATS))
4498 if (!capable(CAP_SYS_ADMIN))
4501 switch (attr->enable_stats.type) {
4502 case BPF_STATS_RUN_TIME:
4503 return bpf_enable_runtime_stats();
4510 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4512 static int bpf_iter_create(union bpf_attr *attr)
4514 struct bpf_link *link;
4517 if (CHECK_ATTR(BPF_ITER_CREATE))
4520 if (attr->iter_create.flags)
4523 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4525 return PTR_ERR(link);
4527 err = bpf_iter_new_fd(link);
4533 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4535 static int bpf_prog_bind_map(union bpf_attr *attr)
4537 struct bpf_prog *prog;
4538 struct bpf_map *map;
4539 struct bpf_map **used_maps_old, **used_maps_new;
4542 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4545 if (attr->prog_bind_map.flags)
4548 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4550 return PTR_ERR(prog);
4552 map = bpf_map_get(attr->prog_bind_map.map_fd);
4558 mutex_lock(&prog->aux->used_maps_mutex);
4560 used_maps_old = prog->aux->used_maps;
4562 for (i = 0; i < prog->aux->used_map_cnt; i++)
4563 if (used_maps_old[i] == map) {
4568 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4569 sizeof(used_maps_new[0]),
4571 if (!used_maps_new) {
4576 memcpy(used_maps_new, used_maps_old,
4577 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4578 used_maps_new[prog->aux->used_map_cnt] = map;
4580 prog->aux->used_map_cnt++;
4581 prog->aux->used_maps = used_maps_new;
4583 kfree(used_maps_old);
4586 mutex_unlock(&prog->aux->used_maps_mutex);
4595 static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
4597 union bpf_attr attr;
4600 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
4603 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4606 size = min_t(u32, size, sizeof(attr));
4608 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4609 memset(&attr, 0, sizeof(attr));
4610 if (copy_from_bpfptr(&attr, uattr, size) != 0)
4613 err = security_bpf(cmd, &attr, size);
4618 case BPF_MAP_CREATE:
4619 err = map_create(&attr);
4621 case BPF_MAP_LOOKUP_ELEM:
4622 err = map_lookup_elem(&attr);
4624 case BPF_MAP_UPDATE_ELEM:
4625 err = map_update_elem(&attr, uattr);
4627 case BPF_MAP_DELETE_ELEM:
4628 err = map_delete_elem(&attr);
4630 case BPF_MAP_GET_NEXT_KEY:
4631 err = map_get_next_key(&attr);
4633 case BPF_MAP_FREEZE:
4634 err = map_freeze(&attr);
4637 err = bpf_prog_load(&attr, uattr);
4640 err = bpf_obj_pin(&attr);
4643 err = bpf_obj_get(&attr);
4645 case BPF_PROG_ATTACH:
4646 err = bpf_prog_attach(&attr);
4648 case BPF_PROG_DETACH:
4649 err = bpf_prog_detach(&attr);
4651 case BPF_PROG_QUERY:
4652 err = bpf_prog_query(&attr, uattr.user);
4654 case BPF_PROG_TEST_RUN:
4655 err = bpf_prog_test_run(&attr, uattr.user);
4657 case BPF_PROG_GET_NEXT_ID:
4658 err = bpf_obj_get_next_id(&attr, uattr.user,
4659 &prog_idr, &prog_idr_lock);
4661 case BPF_MAP_GET_NEXT_ID:
4662 err = bpf_obj_get_next_id(&attr, uattr.user,
4663 &map_idr, &map_idr_lock);
4665 case BPF_BTF_GET_NEXT_ID:
4666 err = bpf_obj_get_next_id(&attr, uattr.user,
4667 &btf_idr, &btf_idr_lock);
4669 case BPF_PROG_GET_FD_BY_ID:
4670 err = bpf_prog_get_fd_by_id(&attr);
4672 case BPF_MAP_GET_FD_BY_ID:
4673 err = bpf_map_get_fd_by_id(&attr);
4675 case BPF_OBJ_GET_INFO_BY_FD:
4676 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
4678 case BPF_RAW_TRACEPOINT_OPEN:
4679 err = bpf_raw_tracepoint_open(&attr);
4682 err = bpf_btf_load(&attr, uattr);
4684 case BPF_BTF_GET_FD_BY_ID:
4685 err = bpf_btf_get_fd_by_id(&attr);
4687 case BPF_TASK_FD_QUERY:
4688 err = bpf_task_fd_query(&attr, uattr.user);
4690 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4691 err = map_lookup_and_delete_elem(&attr);
4693 case BPF_MAP_LOOKUP_BATCH:
4694 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
4696 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4697 err = bpf_map_do_batch(&attr, uattr.user,
4698 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4700 case BPF_MAP_UPDATE_BATCH:
4701 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
4703 case BPF_MAP_DELETE_BATCH:
4704 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
4706 case BPF_LINK_CREATE:
4707 err = link_create(&attr, uattr);
4709 case BPF_LINK_UPDATE:
4710 err = link_update(&attr);
4712 case BPF_LINK_GET_FD_BY_ID:
4713 err = bpf_link_get_fd_by_id(&attr);
4715 case BPF_LINK_GET_NEXT_ID:
4716 err = bpf_obj_get_next_id(&attr, uattr.user,
4717 &link_idr, &link_idr_lock);
4719 case BPF_ENABLE_STATS:
4720 err = bpf_enable_stats(&attr);
4722 case BPF_ITER_CREATE:
4723 err = bpf_iter_create(&attr);
4725 case BPF_LINK_DETACH:
4726 err = link_detach(&attr);
4728 case BPF_PROG_BIND_MAP:
4729 err = bpf_prog_bind_map(&attr);
4739 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4741 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
4744 static bool syscall_prog_is_valid_access(int off, int size,
4745 enum bpf_access_type type,
4746 const struct bpf_prog *prog,
4747 struct bpf_insn_access_aux *info)
4749 if (off < 0 || off >= U16_MAX)
4751 if (off % size != 0)
4756 BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size)
4759 case BPF_MAP_CREATE:
4760 case BPF_MAP_UPDATE_ELEM:
4761 case BPF_MAP_FREEZE:
4765 /* case BPF_PROG_TEST_RUN:
4766 * is not part of this list to prevent recursive test_run
4771 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
4774 static const struct bpf_func_proto bpf_sys_bpf_proto = {
4775 .func = bpf_sys_bpf,
4777 .ret_type = RET_INTEGER,
4778 .arg1_type = ARG_ANYTHING,
4779 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
4780 .arg3_type = ARG_CONST_SIZE,
4783 const struct bpf_func_proto * __weak
4784 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4786 return bpf_base_func_proto(func_id);
4789 BPF_CALL_1(bpf_sys_close, u32, fd)
4791 /* When bpf program calls this helper there should not be
4792 * an fdget() without matching completed fdput().
4793 * This helper is allowed in the following callchain only:
4794 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
4796 return close_fd(fd);
4799 static const struct bpf_func_proto bpf_sys_close_proto = {
4800 .func = bpf_sys_close,
4802 .ret_type = RET_INTEGER,
4803 .arg1_type = ARG_ANYTHING,
4806 BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res)
4811 if (name_sz <= 1 || name[name_sz - 1])
4814 if (!bpf_dump_raw_ok(current_cred()))
4817 *res = kallsyms_lookup_name(name);
4818 return *res ? 0 : -ENOENT;
4821 const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = {
4822 .func = bpf_kallsyms_lookup_name,
4824 .ret_type = RET_INTEGER,
4825 .arg1_type = ARG_PTR_TO_MEM,
4826 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
4827 .arg3_type = ARG_ANYTHING,
4828 .arg4_type = ARG_PTR_TO_LONG,
4831 static const struct bpf_func_proto *
4832 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4835 case BPF_FUNC_sys_bpf:
4836 return &bpf_sys_bpf_proto;
4837 case BPF_FUNC_btf_find_by_name_kind:
4838 return &bpf_btf_find_by_name_kind_proto;
4839 case BPF_FUNC_sys_close:
4840 return &bpf_sys_close_proto;
4841 case BPF_FUNC_kallsyms_lookup_name:
4842 return &bpf_kallsyms_lookup_name_proto;
4844 return tracing_prog_func_proto(func_id, prog);
4848 const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
4849 .get_func_proto = syscall_prog_func_proto,
4850 .is_valid_access = syscall_prog_is_valid_access,
4853 const struct bpf_prog_ops bpf_syscall_prog_ops = {
4854 .test_run = bpf_prog_test_run_syscall,