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>
9 #include <linux/bsearch.h>
10 #include <linux/btf.h>
11 #include <linux/syscalls.h>
12 #include <linux/slab.h>
13 #include <linux/sched/signal.h>
14 #include <linux/vmalloc.h>
15 #include <linux/mmzone.h>
16 #include <linux/anon_inodes.h>
17 #include <linux/fdtable.h>
18 #include <linux/file.h>
20 #include <linux/license.h>
21 #include <linux/filter.h>
22 #include <linux/kernel.h>
23 #include <linux/idr.h>
24 #include <linux/cred.h>
25 #include <linux/timekeeping.h>
26 #include <linux/ctype.h>
27 #include <linux/nospec.h>
28 #include <linux/audit.h>
29 #include <uapi/linux/btf.h>
30 #include <linux/pgtable.h>
31 #include <linux/bpf_lsm.h>
32 #include <linux/poll.h>
33 #include <linux/sort.h>
34 #include <linux/bpf-netns.h>
35 #include <linux/rcupdate_trace.h>
36 #include <linux/memcontrol.h>
37 #include <linux/trace_events.h>
39 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
40 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
41 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
42 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
43 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
44 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
47 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
49 DEFINE_PER_CPU(int, bpf_prog_active);
50 static DEFINE_IDR(prog_idr);
51 static DEFINE_SPINLOCK(prog_idr_lock);
52 static DEFINE_IDR(map_idr);
53 static DEFINE_SPINLOCK(map_idr_lock);
54 static DEFINE_IDR(link_idr);
55 static DEFINE_SPINLOCK(link_idr_lock);
57 int sysctl_unprivileged_bpf_disabled __read_mostly =
58 IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
60 static const struct bpf_map_ops * const bpf_map_types[] = {
61 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
62 #define BPF_MAP_TYPE(_id, _ops) \
64 #define BPF_LINK_TYPE(_id, _name)
65 #include <linux/bpf_types.h>
72 * If we're handed a bigger struct than we know of, ensure all the unknown bits
73 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
74 * we don't know about yet.
76 * There is a ToCToU between this function call and the following
77 * copy_from_user() call. However, this is not a concern since this function is
78 * meant to be a future-proofing of bits.
80 int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
86 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
89 if (actual_size <= expected_size)
93 res = memchr_inv(uaddr.kernel + expected_size, 0,
94 actual_size - expected_size) == NULL;
96 res = check_zeroed_user(uaddr.user + expected_size,
97 actual_size - expected_size);
100 return res ? 0 : -E2BIG;
103 const struct bpf_map_ops bpf_map_offload_ops = {
104 .map_meta_equal = bpf_map_meta_equal,
105 .map_alloc = bpf_map_offload_map_alloc,
106 .map_free = bpf_map_offload_map_free,
107 .map_check_btf = map_check_no_btf,
110 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
112 const struct bpf_map_ops *ops;
113 u32 type = attr->map_type;
117 if (type >= ARRAY_SIZE(bpf_map_types))
118 return ERR_PTR(-EINVAL);
119 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
120 ops = bpf_map_types[type];
122 return ERR_PTR(-EINVAL);
124 if (ops->map_alloc_check) {
125 err = ops->map_alloc_check(attr);
129 if (attr->map_ifindex)
130 ops = &bpf_map_offload_ops;
131 map = ops->map_alloc(attr);
135 map->map_type = type;
139 static void bpf_map_write_active_inc(struct bpf_map *map)
141 atomic64_inc(&map->writecnt);
144 static void bpf_map_write_active_dec(struct bpf_map *map)
146 atomic64_dec(&map->writecnt);
149 bool bpf_map_write_active(const struct bpf_map *map)
151 return atomic64_read(&map->writecnt) != 0;
154 static u32 bpf_map_value_size(const struct bpf_map *map)
156 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
157 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
158 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
159 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
160 return round_up(map->value_size, 8) * num_possible_cpus();
161 else if (IS_FD_MAP(map))
164 return map->value_size;
167 static void maybe_wait_bpf_programs(struct bpf_map *map)
169 /* Wait for any running BPF programs to complete so that
170 * userspace, when we return to it, knows that all programs
171 * that could be running use the new map value.
173 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
174 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
178 static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
179 void *value, __u64 flags)
183 /* Need to create a kthread, thus must support schedule */
184 if (bpf_map_is_dev_bound(map)) {
185 return bpf_map_offload_update_elem(map, key, value, flags);
186 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
187 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
188 return map->ops->map_update_elem(map, key, value, flags);
189 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
190 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
191 return sock_map_update_elem_sys(map, key, value, flags);
192 } else if (IS_FD_PROG_ARRAY(map)) {
193 return bpf_fd_array_map_update_elem(map, f.file, key, value,
197 bpf_disable_instrumentation();
198 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
199 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
200 err = bpf_percpu_hash_update(map, key, value, flags);
201 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
202 err = bpf_percpu_array_update(map, key, value, flags);
203 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
204 err = bpf_percpu_cgroup_storage_update(map, key, value,
206 } else if (IS_FD_ARRAY(map)) {
208 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
211 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
213 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
216 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
217 /* rcu_read_lock() is not needed */
218 err = bpf_fd_reuseport_array_update_elem(map, key, value,
220 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
221 map->map_type == BPF_MAP_TYPE_STACK ||
222 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
223 err = map->ops->map_push_elem(map, value, flags);
226 err = map->ops->map_update_elem(map, key, value, flags);
229 bpf_enable_instrumentation();
230 maybe_wait_bpf_programs(map);
235 static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
241 if (bpf_map_is_dev_bound(map))
242 return bpf_map_offload_lookup_elem(map, key, value);
244 bpf_disable_instrumentation();
245 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
246 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
247 err = bpf_percpu_hash_copy(map, key, value);
248 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
249 err = bpf_percpu_array_copy(map, key, value);
250 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
251 err = bpf_percpu_cgroup_storage_copy(map, key, value);
252 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
253 err = bpf_stackmap_copy(map, key, value);
254 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
255 err = bpf_fd_array_map_lookup_elem(map, key, value);
256 } else if (IS_FD_HASH(map)) {
257 err = bpf_fd_htab_map_lookup_elem(map, key, value);
258 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
259 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
260 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
261 map->map_type == BPF_MAP_TYPE_STACK ||
262 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
263 err = map->ops->map_peek_elem(map, value);
264 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
265 /* struct_ops map requires directly updating "value" */
266 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
269 if (map->ops->map_lookup_elem_sys_only)
270 ptr = map->ops->map_lookup_elem_sys_only(map, key);
272 ptr = map->ops->map_lookup_elem(map, key);
279 if (flags & BPF_F_LOCK)
280 /* lock 'ptr' and copy everything but lock */
281 copy_map_value_locked(map, value, ptr, true);
283 copy_map_value(map, value, ptr);
284 /* mask lock and timer, since value wasn't zero inited */
285 check_and_init_map_value(map, value);
290 bpf_enable_instrumentation();
291 maybe_wait_bpf_programs(map);
296 /* Please, do not use this function outside from the map creation path
297 * (e.g. in map update path) without taking care of setting the active
298 * memory cgroup (see at bpf_map_kmalloc_node() for example).
300 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
302 /* We really just want to fail instead of triggering OOM killer
303 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
304 * which is used for lower order allocation requests.
306 * It has been observed that higher order allocation requests done by
307 * vmalloc with __GFP_NORETRY being set might fail due to not trying
308 * to reclaim memory from the page cache, thus we set
309 * __GFP_RETRY_MAYFAIL to avoid such situations.
312 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
313 unsigned int flags = 0;
314 unsigned long align = 1;
317 if (size >= SIZE_MAX)
320 /* kmalloc()'ed memory can't be mmap()'ed */
322 BUG_ON(!PAGE_ALIGNED(size));
325 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
326 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
332 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
333 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
334 flags, numa_node, __builtin_return_address(0));
337 void *bpf_map_area_alloc(u64 size, int numa_node)
339 return __bpf_map_area_alloc(size, numa_node, false);
342 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
344 return __bpf_map_area_alloc(size, numa_node, true);
347 void bpf_map_area_free(void *area)
352 static u32 bpf_map_flags_retain_permanent(u32 flags)
354 /* Some map creation flags are not tied to the map object but
355 * rather to the map fd instead, so they have no meaning upon
356 * map object inspection since multiple file descriptors with
357 * different (access) properties can exist here. Thus, given
358 * this has zero meaning for the map itself, lets clear these
361 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
364 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
366 map->map_type = attr->map_type;
367 map->key_size = attr->key_size;
368 map->value_size = attr->value_size;
369 map->max_entries = attr->max_entries;
370 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
371 map->numa_node = bpf_map_attr_numa_node(attr);
372 map->map_extra = attr->map_extra;
375 static int bpf_map_alloc_id(struct bpf_map *map)
379 idr_preload(GFP_KERNEL);
380 spin_lock_bh(&map_idr_lock);
381 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
384 spin_unlock_bh(&map_idr_lock);
387 if (WARN_ON_ONCE(!id))
390 return id > 0 ? 0 : id;
393 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
397 /* Offloaded maps are removed from the IDR store when their device
398 * disappears - even if someone holds an fd to them they are unusable,
399 * the memory is gone, all ops will fail; they are simply waiting for
400 * refcnt to drop to be freed.
406 spin_lock_irqsave(&map_idr_lock, flags);
408 __acquire(&map_idr_lock);
410 idr_remove(&map_idr, map->id);
414 spin_unlock_irqrestore(&map_idr_lock, flags);
416 __release(&map_idr_lock);
419 #ifdef CONFIG_MEMCG_KMEM
420 static void bpf_map_save_memcg(struct bpf_map *map)
422 map->memcg = get_mem_cgroup_from_mm(current->mm);
425 static void bpf_map_release_memcg(struct bpf_map *map)
427 mem_cgroup_put(map->memcg);
430 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
433 struct mem_cgroup *old_memcg;
436 old_memcg = set_active_memcg(map->memcg);
437 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
438 set_active_memcg(old_memcg);
443 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
445 struct mem_cgroup *old_memcg;
448 old_memcg = set_active_memcg(map->memcg);
449 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
450 set_active_memcg(old_memcg);
455 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
456 size_t align, gfp_t flags)
458 struct mem_cgroup *old_memcg;
461 old_memcg = set_active_memcg(map->memcg);
462 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
463 set_active_memcg(old_memcg);
469 static void bpf_map_save_memcg(struct bpf_map *map)
473 static void bpf_map_release_memcg(struct bpf_map *map)
478 static int bpf_map_kptr_off_cmp(const void *a, const void *b)
480 const struct bpf_map_value_off_desc *off_desc1 = a, *off_desc2 = b;
482 if (off_desc1->offset < off_desc2->offset)
484 else if (off_desc1->offset > off_desc2->offset)
489 struct bpf_map_value_off_desc *bpf_map_kptr_off_contains(struct bpf_map *map, u32 offset)
491 /* Since members are iterated in btf_find_field in increasing order,
492 * offsets appended to kptr_off_tab are in increasing order, so we can
493 * do bsearch to find exact match.
495 struct bpf_map_value_off *tab;
497 if (!map_value_has_kptrs(map))
499 tab = map->kptr_off_tab;
500 return bsearch(&offset, tab->off, tab->nr_off, sizeof(tab->off[0]), bpf_map_kptr_off_cmp);
503 void bpf_map_free_kptr_off_tab(struct bpf_map *map)
505 struct bpf_map_value_off *tab = map->kptr_off_tab;
508 if (!map_value_has_kptrs(map))
510 for (i = 0; i < tab->nr_off; i++) {
511 if (tab->off[i].kptr.module)
512 module_put(tab->off[i].kptr.module);
513 btf_put(tab->off[i].kptr.btf);
516 map->kptr_off_tab = NULL;
519 struct bpf_map_value_off *bpf_map_copy_kptr_off_tab(const struct bpf_map *map)
521 struct bpf_map_value_off *tab = map->kptr_off_tab, *new_tab;
524 if (!map_value_has_kptrs(map))
525 return ERR_PTR(-ENOENT);
526 size = offsetof(struct bpf_map_value_off, off[tab->nr_off]);
527 new_tab = kmemdup(tab, size, GFP_KERNEL | __GFP_NOWARN);
529 return ERR_PTR(-ENOMEM);
530 /* Do a deep copy of the kptr_off_tab */
531 for (i = 0; i < tab->nr_off; i++) {
532 btf_get(tab->off[i].kptr.btf);
533 if (tab->off[i].kptr.module && !try_module_get(tab->off[i].kptr.module)) {
535 if (tab->off[i].kptr.module)
536 module_put(tab->off[i].kptr.module);
537 btf_put(tab->off[i].kptr.btf);
540 return ERR_PTR(-ENXIO);
546 bool bpf_map_equal_kptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b)
548 struct bpf_map_value_off *tab_a = map_a->kptr_off_tab, *tab_b = map_b->kptr_off_tab;
549 bool a_has_kptr = map_value_has_kptrs(map_a), b_has_kptr = map_value_has_kptrs(map_b);
552 if (!a_has_kptr && !b_has_kptr)
554 if (a_has_kptr != b_has_kptr)
556 if (tab_a->nr_off != tab_b->nr_off)
558 size = offsetof(struct bpf_map_value_off, off[tab_a->nr_off]);
559 return !memcmp(tab_a, tab_b, size);
562 /* Caller must ensure map_value_has_kptrs is true. Note that this function can
563 * be called on a map value while the map_value is visible to BPF programs, as
564 * it ensures the correct synchronization, and we already enforce the same using
565 * the bpf_kptr_xchg helper on the BPF program side for referenced kptrs.
567 void bpf_map_free_kptrs(struct bpf_map *map, void *map_value)
569 struct bpf_map_value_off *tab = map->kptr_off_tab;
570 unsigned long *btf_id_ptr;
573 for (i = 0; i < tab->nr_off; i++) {
574 struct bpf_map_value_off_desc *off_desc = &tab->off[i];
575 unsigned long old_ptr;
577 btf_id_ptr = map_value + off_desc->offset;
578 if (off_desc->type == BPF_KPTR_UNREF) {
579 u64 *p = (u64 *)btf_id_ptr;
584 old_ptr = xchg(btf_id_ptr, 0);
585 off_desc->kptr.dtor((void *)old_ptr);
589 /* called from workqueue */
590 static void bpf_map_free_deferred(struct work_struct *work)
592 struct bpf_map *map = container_of(work, struct bpf_map, work);
594 security_bpf_map_free(map);
596 bpf_map_release_memcg(map);
597 /* implementation dependent freeing, map_free callback also does
598 * bpf_map_free_kptr_off_tab, if needed.
600 map->ops->map_free(map);
603 static void bpf_map_put_uref(struct bpf_map *map)
605 if (atomic64_dec_and_test(&map->usercnt)) {
606 if (map->ops->map_release_uref)
607 map->ops->map_release_uref(map);
611 /* decrement map refcnt and schedule it for freeing via workqueue
612 * (unrelying map implementation ops->map_free() might sleep)
614 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
616 if (atomic64_dec_and_test(&map->refcnt)) {
617 /* bpf_map_free_id() must be called first */
618 bpf_map_free_id(map, do_idr_lock);
620 INIT_WORK(&map->work, bpf_map_free_deferred);
621 schedule_work(&map->work);
625 void bpf_map_put(struct bpf_map *map)
627 __bpf_map_put(map, true);
629 EXPORT_SYMBOL_GPL(bpf_map_put);
631 void bpf_map_put_with_uref(struct bpf_map *map)
633 bpf_map_put_uref(map);
637 static int bpf_map_release(struct inode *inode, struct file *filp)
639 struct bpf_map *map = filp->private_data;
641 if (map->ops->map_release)
642 map->ops->map_release(map, filp);
644 bpf_map_put_with_uref(map);
648 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
650 fmode_t mode = f.file->f_mode;
652 /* Our file permissions may have been overridden by global
653 * map permissions facing syscall side.
655 if (READ_ONCE(map->frozen))
656 mode &= ~FMODE_CAN_WRITE;
660 #ifdef CONFIG_PROC_FS
661 /* Provides an approximation of the map's memory footprint.
662 * Used only to provide a backward compatibility and display
663 * a reasonable "memlock" info.
665 static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
669 size = round_up(map->key_size + bpf_map_value_size(map), 8);
671 return round_up(map->max_entries * size, PAGE_SIZE);
674 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
676 struct bpf_map *map = filp->private_data;
677 u32 type = 0, jited = 0;
679 if (map_type_contains_progs(map)) {
680 spin_lock(&map->owner.lock);
681 type = map->owner.type;
682 jited = map->owner.jited;
683 spin_unlock(&map->owner.lock);
692 "map_extra:\t%#llx\n"
701 (unsigned long long)map->map_extra,
702 bpf_map_memory_footprint(map),
704 READ_ONCE(map->frozen));
706 seq_printf(m, "owner_prog_type:\t%u\n", type);
707 seq_printf(m, "owner_jited:\t%u\n", jited);
712 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
715 /* We need this handler such that alloc_file() enables
716 * f_mode with FMODE_CAN_READ.
721 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
722 size_t siz, loff_t *ppos)
724 /* We need this handler such that alloc_file() enables
725 * f_mode with FMODE_CAN_WRITE.
730 /* called for any extra memory-mapped regions (except initial) */
731 static void bpf_map_mmap_open(struct vm_area_struct *vma)
733 struct bpf_map *map = vma->vm_file->private_data;
735 if (vma->vm_flags & VM_MAYWRITE)
736 bpf_map_write_active_inc(map);
739 /* called for all unmapped memory region (including initial) */
740 static void bpf_map_mmap_close(struct vm_area_struct *vma)
742 struct bpf_map *map = vma->vm_file->private_data;
744 if (vma->vm_flags & VM_MAYWRITE)
745 bpf_map_write_active_dec(map);
748 static const struct vm_operations_struct bpf_map_default_vmops = {
749 .open = bpf_map_mmap_open,
750 .close = bpf_map_mmap_close,
753 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
755 struct bpf_map *map = filp->private_data;
758 if (!map->ops->map_mmap || map_value_has_spin_lock(map) ||
759 map_value_has_timer(map) || map_value_has_kptrs(map))
762 if (!(vma->vm_flags & VM_SHARED))
765 mutex_lock(&map->freeze_mutex);
767 if (vma->vm_flags & VM_WRITE) {
772 /* map is meant to be read-only, so do not allow mapping as
773 * writable, because it's possible to leak a writable page
774 * reference and allows user-space to still modify it after
775 * freezing, while verifier will assume contents do not change
777 if (map->map_flags & BPF_F_RDONLY_PROG) {
783 /* set default open/close callbacks */
784 vma->vm_ops = &bpf_map_default_vmops;
785 vma->vm_private_data = map;
786 vma->vm_flags &= ~VM_MAYEXEC;
787 if (!(vma->vm_flags & VM_WRITE))
788 /* disallow re-mapping with PROT_WRITE */
789 vma->vm_flags &= ~VM_MAYWRITE;
791 err = map->ops->map_mmap(map, vma);
795 if (vma->vm_flags & VM_MAYWRITE)
796 bpf_map_write_active_inc(map);
798 mutex_unlock(&map->freeze_mutex);
802 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
804 struct bpf_map *map = filp->private_data;
806 if (map->ops->map_poll)
807 return map->ops->map_poll(map, filp, pts);
812 const struct file_operations bpf_map_fops = {
813 #ifdef CONFIG_PROC_FS
814 .show_fdinfo = bpf_map_show_fdinfo,
816 .release = bpf_map_release,
817 .read = bpf_dummy_read,
818 .write = bpf_dummy_write,
819 .mmap = bpf_map_mmap,
820 .poll = bpf_map_poll,
823 int bpf_map_new_fd(struct bpf_map *map, int flags)
827 ret = security_bpf_map(map, OPEN_FMODE(flags));
831 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
835 int bpf_get_file_flag(int flags)
837 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
839 if (flags & BPF_F_RDONLY)
841 if (flags & BPF_F_WRONLY)
846 /* helper macro to check that unused fields 'union bpf_attr' are zero */
847 #define CHECK_ATTR(CMD) \
848 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
849 sizeof(attr->CMD##_LAST_FIELD), 0, \
851 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
852 sizeof(attr->CMD##_LAST_FIELD)) != NULL
854 /* dst and src must have at least "size" number of bytes.
855 * Return strlen on success and < 0 on error.
857 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
859 const char *end = src + size;
860 const char *orig_src = src;
862 memset(dst, 0, size);
863 /* Copy all isalnum(), '_' and '.' chars. */
864 while (src < end && *src) {
865 if (!isalnum(*src) &&
866 *src != '_' && *src != '.')
871 /* No '\0' found in "size" number of bytes */
875 return src - orig_src;
878 int map_check_no_btf(const struct bpf_map *map,
879 const struct btf *btf,
880 const struct btf_type *key_type,
881 const struct btf_type *value_type)
886 static int map_off_arr_cmp(const void *_a, const void *_b, const void *priv)
888 const u32 a = *(const u32 *)_a;
889 const u32 b = *(const u32 *)_b;
898 static void map_off_arr_swap(void *_a, void *_b, int size, const void *priv)
900 struct bpf_map *map = (struct bpf_map *)priv;
901 u32 *off_base = map->off_arr->field_off;
902 u32 *a = _a, *b = _b;
905 sz_a = map->off_arr->field_sz + (a - off_base);
906 sz_b = map->off_arr->field_sz + (b - off_base);
912 static int bpf_map_alloc_off_arr(struct bpf_map *map)
914 bool has_spin_lock = map_value_has_spin_lock(map);
915 bool has_timer = map_value_has_timer(map);
916 bool has_kptrs = map_value_has_kptrs(map);
917 struct bpf_map_off_arr *off_arr;
920 if (!has_spin_lock && !has_timer && !has_kptrs) {
925 off_arr = kmalloc(sizeof(*map->off_arr), GFP_KERNEL | __GFP_NOWARN);
928 map->off_arr = off_arr;
934 off_arr->field_off[i] = map->spin_lock_off;
935 off_arr->field_sz[i] = sizeof(struct bpf_spin_lock);
941 off_arr->field_off[i] = map->timer_off;
942 off_arr->field_sz[i] = sizeof(struct bpf_timer);
946 struct bpf_map_value_off *tab = map->kptr_off_tab;
947 u32 *off = &off_arr->field_off[off_arr->cnt];
948 u8 *sz = &off_arr->field_sz[off_arr->cnt];
950 for (i = 0; i < tab->nr_off; i++) {
951 *off++ = tab->off[i].offset;
954 off_arr->cnt += tab->nr_off;
957 if (off_arr->cnt == 1)
959 sort_r(off_arr->field_off, off_arr->cnt, sizeof(off_arr->field_off[0]),
960 map_off_arr_cmp, map_off_arr_swap, map);
964 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
965 u32 btf_key_id, u32 btf_value_id)
967 const struct btf_type *key_type, *value_type;
968 u32 key_size, value_size;
971 /* Some maps allow key to be unspecified. */
973 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
974 if (!key_type || key_size != map->key_size)
977 key_type = btf_type_by_id(btf, 0);
978 if (!map->ops->map_check_btf)
982 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
983 if (!value_type || value_size != map->value_size)
986 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
988 if (map_value_has_spin_lock(map)) {
989 if (map->map_flags & BPF_F_RDONLY_PROG)
991 if (map->map_type != BPF_MAP_TYPE_HASH &&
992 map->map_type != BPF_MAP_TYPE_ARRAY &&
993 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
994 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
995 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
996 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
998 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
1001 "verifier bug spin_lock_off %d value_size %d\n",
1002 map->spin_lock_off, map->value_size);
1007 map->timer_off = btf_find_timer(btf, value_type);
1008 if (map_value_has_timer(map)) {
1009 if (map->map_flags & BPF_F_RDONLY_PROG)
1011 if (map->map_type != BPF_MAP_TYPE_HASH &&
1012 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1013 map->map_type != BPF_MAP_TYPE_ARRAY)
1017 map->kptr_off_tab = btf_parse_kptrs(btf, value_type);
1018 if (map_value_has_kptrs(map)) {
1019 if (!bpf_capable()) {
1023 if (map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) {
1027 if (map->map_type != BPF_MAP_TYPE_HASH &&
1028 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1029 map->map_type != BPF_MAP_TYPE_ARRAY) {
1035 if (map->ops->map_check_btf) {
1036 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
1043 bpf_map_free_kptr_off_tab(map);
1047 #define BPF_MAP_CREATE_LAST_FIELD map_extra
1048 /* called via syscall */
1049 static int map_create(union bpf_attr *attr)
1051 int numa_node = bpf_map_attr_numa_node(attr);
1052 struct bpf_map *map;
1056 err = CHECK_ATTR(BPF_MAP_CREATE);
1060 if (attr->btf_vmlinux_value_type_id) {
1061 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
1062 attr->btf_key_type_id || attr->btf_value_type_id)
1064 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
1068 if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER &&
1069 attr->map_extra != 0)
1072 f_flags = bpf_get_file_flag(attr->map_flags);
1076 if (numa_node != NUMA_NO_NODE &&
1077 ((unsigned int)numa_node >= nr_node_ids ||
1078 !node_online(numa_node)))
1081 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
1082 map = find_and_alloc_map(attr);
1084 return PTR_ERR(map);
1086 err = bpf_obj_name_cpy(map->name, attr->map_name,
1087 sizeof(attr->map_name));
1091 atomic64_set(&map->refcnt, 1);
1092 atomic64_set(&map->usercnt, 1);
1093 mutex_init(&map->freeze_mutex);
1094 spin_lock_init(&map->owner.lock);
1096 map->spin_lock_off = -EINVAL;
1097 map->timer_off = -EINVAL;
1098 if (attr->btf_key_type_id || attr->btf_value_type_id ||
1099 /* Even the map's value is a kernel's struct,
1100 * the bpf_prog.o must have BTF to begin with
1101 * to figure out the corresponding kernel's
1102 * counter part. Thus, attr->btf_fd has
1105 attr->btf_vmlinux_value_type_id) {
1108 btf = btf_get_by_fd(attr->btf_fd);
1113 if (btf_is_kernel(btf)) {
1120 if (attr->btf_value_type_id) {
1121 err = map_check_btf(map, btf, attr->btf_key_type_id,
1122 attr->btf_value_type_id);
1127 map->btf_key_type_id = attr->btf_key_type_id;
1128 map->btf_value_type_id = attr->btf_value_type_id;
1129 map->btf_vmlinux_value_type_id =
1130 attr->btf_vmlinux_value_type_id;
1133 err = bpf_map_alloc_off_arr(map);
1137 err = security_bpf_map_alloc(map);
1139 goto free_map_off_arr;
1141 err = bpf_map_alloc_id(map);
1145 bpf_map_save_memcg(map);
1147 err = bpf_map_new_fd(map, f_flags);
1149 /* failed to allocate fd.
1150 * bpf_map_put_with_uref() is needed because the above
1151 * bpf_map_alloc_id() has published the map
1152 * to the userspace and the userspace may
1153 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
1155 bpf_map_put_with_uref(map);
1162 security_bpf_map_free(map);
1164 kfree(map->off_arr);
1167 map->ops->map_free(map);
1171 /* if error is returned, fd is released.
1172 * On success caller should complete fd access with matching fdput()
1174 struct bpf_map *__bpf_map_get(struct fd f)
1177 return ERR_PTR(-EBADF);
1178 if (f.file->f_op != &bpf_map_fops) {
1180 return ERR_PTR(-EINVAL);
1183 return f.file->private_data;
1186 void bpf_map_inc(struct bpf_map *map)
1188 atomic64_inc(&map->refcnt);
1190 EXPORT_SYMBOL_GPL(bpf_map_inc);
1192 void bpf_map_inc_with_uref(struct bpf_map *map)
1194 atomic64_inc(&map->refcnt);
1195 atomic64_inc(&map->usercnt);
1197 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
1199 struct bpf_map *bpf_map_get(u32 ufd)
1201 struct fd f = fdget(ufd);
1202 struct bpf_map *map;
1204 map = __bpf_map_get(f);
1213 EXPORT_SYMBOL(bpf_map_get);
1215 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
1217 struct fd f = fdget(ufd);
1218 struct bpf_map *map;
1220 map = __bpf_map_get(f);
1224 bpf_map_inc_with_uref(map);
1230 /* map_idr_lock should have been held */
1231 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
1235 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
1237 return ERR_PTR(-ENOENT);
1239 atomic64_inc(&map->usercnt);
1244 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
1246 spin_lock_bh(&map_idr_lock);
1247 map = __bpf_map_inc_not_zero(map, false);
1248 spin_unlock_bh(&map_idr_lock);
1252 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
1254 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1259 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1262 return vmemdup_user(ukey, key_size);
1265 return ERR_PTR(-EINVAL);
1270 static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1273 return kvmemdup_bpfptr(ukey, key_size);
1275 if (!bpfptr_is_null(ukey))
1276 return ERR_PTR(-EINVAL);
1281 /* last field in 'union bpf_attr' used by this command */
1282 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1284 static int map_lookup_elem(union bpf_attr *attr)
1286 void __user *ukey = u64_to_user_ptr(attr->key);
1287 void __user *uvalue = u64_to_user_ptr(attr->value);
1288 int ufd = attr->map_fd;
1289 struct bpf_map *map;
1295 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1298 if (attr->flags & ~BPF_F_LOCK)
1302 map = __bpf_map_get(f);
1304 return PTR_ERR(map);
1305 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1310 if ((attr->flags & BPF_F_LOCK) &&
1311 !map_value_has_spin_lock(map)) {
1316 key = __bpf_copy_key(ukey, map->key_size);
1322 value_size = bpf_map_value_size(map);
1325 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1329 if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
1330 if (copy_from_user(value, uvalue, value_size))
1333 err = bpf_map_copy_value(map, key, value, attr->flags);
1337 err = bpf_map_copy_value(map, key, value, attr->flags);
1342 if (copy_to_user(uvalue, value, value_size) != 0)
1357 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1359 static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
1361 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1362 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
1363 int ufd = attr->map_fd;
1364 struct bpf_map *map;
1370 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1374 map = __bpf_map_get(f);
1376 return PTR_ERR(map);
1377 bpf_map_write_active_inc(map);
1378 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1383 if ((attr->flags & BPF_F_LOCK) &&
1384 !map_value_has_spin_lock(map)) {
1389 key = ___bpf_copy_key(ukey, map->key_size);
1395 value_size = bpf_map_value_size(map);
1398 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1403 if (copy_from_bpfptr(value, uvalue, value_size) != 0)
1406 err = bpf_map_update_value(map, f, key, value, attr->flags);
1413 bpf_map_write_active_dec(map);
1418 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1420 static int map_delete_elem(union bpf_attr *attr)
1422 void __user *ukey = u64_to_user_ptr(attr->key);
1423 int ufd = attr->map_fd;
1424 struct bpf_map *map;
1429 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1433 map = __bpf_map_get(f);
1435 return PTR_ERR(map);
1436 bpf_map_write_active_inc(map);
1437 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1442 key = __bpf_copy_key(ukey, map->key_size);
1448 if (bpf_map_is_dev_bound(map)) {
1449 err = bpf_map_offload_delete_elem(map, key);
1451 } else if (IS_FD_PROG_ARRAY(map) ||
1452 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1453 /* These maps require sleepable context */
1454 err = map->ops->map_delete_elem(map, key);
1458 bpf_disable_instrumentation();
1460 err = map->ops->map_delete_elem(map, key);
1462 bpf_enable_instrumentation();
1463 maybe_wait_bpf_programs(map);
1467 bpf_map_write_active_dec(map);
1472 /* last field in 'union bpf_attr' used by this command */
1473 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1475 static int map_get_next_key(union bpf_attr *attr)
1477 void __user *ukey = u64_to_user_ptr(attr->key);
1478 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1479 int ufd = attr->map_fd;
1480 struct bpf_map *map;
1481 void *key, *next_key;
1485 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1489 map = __bpf_map_get(f);
1491 return PTR_ERR(map);
1492 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1498 key = __bpf_copy_key(ukey, map->key_size);
1508 next_key = kvmalloc(map->key_size, GFP_USER);
1512 if (bpf_map_is_dev_bound(map)) {
1513 err = bpf_map_offload_get_next_key(map, key, next_key);
1518 err = map->ops->map_get_next_key(map, key, next_key);
1525 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1539 int generic_map_delete_batch(struct bpf_map *map,
1540 const union bpf_attr *attr,
1541 union bpf_attr __user *uattr)
1543 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1548 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1551 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1552 !map_value_has_spin_lock(map)) {
1556 max_count = attr->batch.count;
1560 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1564 for (cp = 0; cp < max_count; cp++) {
1566 if (copy_from_user(key, keys + cp * map->key_size,
1570 if (bpf_map_is_dev_bound(map)) {
1571 err = bpf_map_offload_delete_elem(map, key);
1575 bpf_disable_instrumentation();
1577 err = map->ops->map_delete_elem(map, key);
1579 bpf_enable_instrumentation();
1584 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1589 maybe_wait_bpf_programs(map);
1593 int generic_map_update_batch(struct bpf_map *map,
1594 const union bpf_attr *attr,
1595 union bpf_attr __user *uattr)
1597 void __user *values = u64_to_user_ptr(attr->batch.values);
1598 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1599 u32 value_size, cp, max_count;
1600 int ufd = attr->batch.map_fd;
1605 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1608 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1609 !map_value_has_spin_lock(map)) {
1613 value_size = bpf_map_value_size(map);
1615 max_count = attr->batch.count;
1619 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1623 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1629 f = fdget(ufd); /* bpf_map_do_batch() guarantees ufd is valid */
1630 for (cp = 0; cp < max_count; cp++) {
1632 if (copy_from_user(key, keys + cp * map->key_size,
1634 copy_from_user(value, values + cp * value_size, value_size))
1637 err = bpf_map_update_value(map, f, key, value,
1638 attr->batch.elem_flags);
1645 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1654 #define MAP_LOOKUP_RETRIES 3
1656 int generic_map_lookup_batch(struct bpf_map *map,
1657 const union bpf_attr *attr,
1658 union bpf_attr __user *uattr)
1660 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1661 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1662 void __user *values = u64_to_user_ptr(attr->batch.values);
1663 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1664 void *buf, *buf_prevkey, *prev_key, *key, *value;
1665 int err, retry = MAP_LOOKUP_RETRIES;
1666 u32 value_size, cp, max_count;
1668 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1671 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1672 !map_value_has_spin_lock(map))
1675 value_size = bpf_map_value_size(map);
1677 max_count = attr->batch.count;
1681 if (put_user(0, &uattr->batch.count))
1684 buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1688 buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1690 kvfree(buf_prevkey);
1696 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1699 value = key + map->key_size;
1701 prev_key = buf_prevkey;
1703 for (cp = 0; cp < max_count;) {
1705 err = map->ops->map_get_next_key(map, prev_key, key);
1709 err = bpf_map_copy_value(map, key, value,
1710 attr->batch.elem_flags);
1712 if (err == -ENOENT) {
1724 if (copy_to_user(keys + cp * map->key_size, key,
1729 if (copy_to_user(values + cp * value_size, value, value_size)) {
1735 prev_key = buf_prevkey;
1737 swap(prev_key, key);
1738 retry = MAP_LOOKUP_RETRIES;
1746 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1747 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1751 kvfree(buf_prevkey);
1756 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
1758 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1760 void __user *ukey = u64_to_user_ptr(attr->key);
1761 void __user *uvalue = u64_to_user_ptr(attr->value);
1762 int ufd = attr->map_fd;
1763 struct bpf_map *map;
1769 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1772 if (attr->flags & ~BPF_F_LOCK)
1776 map = __bpf_map_get(f);
1778 return PTR_ERR(map);
1779 bpf_map_write_active_inc(map);
1780 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1781 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1787 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1788 map->map_type == BPF_MAP_TYPE_STACK)) {
1793 if ((attr->flags & BPF_F_LOCK) &&
1794 !map_value_has_spin_lock(map)) {
1799 key = __bpf_copy_key(ukey, map->key_size);
1805 value_size = bpf_map_value_size(map);
1808 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1813 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1814 map->map_type == BPF_MAP_TYPE_STACK) {
1815 err = map->ops->map_pop_elem(map, value);
1816 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1817 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1818 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1819 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1820 if (!bpf_map_is_dev_bound(map)) {
1821 bpf_disable_instrumentation();
1823 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1825 bpf_enable_instrumentation();
1832 if (copy_to_user(uvalue, value, value_size) != 0) {
1844 bpf_map_write_active_dec(map);
1849 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1851 static int map_freeze(const union bpf_attr *attr)
1853 int err = 0, ufd = attr->map_fd;
1854 struct bpf_map *map;
1857 if (CHECK_ATTR(BPF_MAP_FREEZE))
1861 map = __bpf_map_get(f);
1863 return PTR_ERR(map);
1865 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS ||
1866 map_value_has_timer(map) || map_value_has_kptrs(map)) {
1871 mutex_lock(&map->freeze_mutex);
1872 if (bpf_map_write_active(map)) {
1876 if (READ_ONCE(map->frozen)) {
1880 if (!bpf_capable()) {
1885 WRITE_ONCE(map->frozen, true);
1887 mutex_unlock(&map->freeze_mutex);
1892 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1893 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1894 [_id] = & _name ## _prog_ops,
1895 #define BPF_MAP_TYPE(_id, _ops)
1896 #define BPF_LINK_TYPE(_id, _name)
1897 #include <linux/bpf_types.h>
1898 #undef BPF_PROG_TYPE
1900 #undef BPF_LINK_TYPE
1903 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1905 const struct bpf_prog_ops *ops;
1907 if (type >= ARRAY_SIZE(bpf_prog_types))
1909 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1910 ops = bpf_prog_types[type];
1914 if (!bpf_prog_is_dev_bound(prog->aux))
1915 prog->aux->ops = ops;
1917 prog->aux->ops = &bpf_offload_prog_ops;
1928 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1929 [BPF_AUDIT_LOAD] = "LOAD",
1930 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1933 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1935 struct audit_context *ctx = NULL;
1936 struct audit_buffer *ab;
1938 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1940 if (audit_enabled == AUDIT_OFF)
1942 if (op == BPF_AUDIT_LOAD)
1943 ctx = audit_context();
1944 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1947 audit_log_format(ab, "prog-id=%u op=%s",
1948 prog->aux->id, bpf_audit_str[op]);
1952 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1956 idr_preload(GFP_KERNEL);
1957 spin_lock_bh(&prog_idr_lock);
1958 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1961 spin_unlock_bh(&prog_idr_lock);
1964 /* id is in [1, INT_MAX) */
1965 if (WARN_ON_ONCE(!id))
1968 return id > 0 ? 0 : id;
1971 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1973 unsigned long flags;
1975 /* cBPF to eBPF migrations are currently not in the idr store.
1976 * Offloaded programs are removed from the store when their device
1977 * disappears - even if someone grabs an fd to them they are unusable,
1978 * simply waiting for refcnt to drop to be freed.
1984 spin_lock_irqsave(&prog_idr_lock, flags);
1986 __acquire(&prog_idr_lock);
1988 idr_remove(&prog_idr, prog->aux->id);
1992 spin_unlock_irqrestore(&prog_idr_lock, flags);
1994 __release(&prog_idr_lock);
1997 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1999 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
2001 kvfree(aux->func_info);
2002 kfree(aux->func_info_aux);
2003 free_uid(aux->user);
2004 security_bpf_prog_free(aux);
2005 bpf_prog_free(aux->prog);
2008 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
2010 bpf_prog_kallsyms_del_all(prog);
2011 btf_put(prog->aux->btf);
2012 kvfree(prog->aux->jited_linfo);
2013 kvfree(prog->aux->linfo);
2014 kfree(prog->aux->kfunc_tab);
2015 if (prog->aux->attach_btf)
2016 btf_put(prog->aux->attach_btf);
2019 if (prog->aux->sleepable)
2020 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
2022 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
2024 __bpf_prog_put_rcu(&prog->aux->rcu);
2028 static void bpf_prog_put_deferred(struct work_struct *work)
2030 struct bpf_prog_aux *aux;
2031 struct bpf_prog *prog;
2033 aux = container_of(work, struct bpf_prog_aux, work);
2035 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
2036 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
2037 __bpf_prog_put_noref(prog, true);
2040 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
2042 struct bpf_prog_aux *aux = prog->aux;
2044 if (atomic64_dec_and_test(&aux->refcnt)) {
2045 /* bpf_prog_free_id() must be called first */
2046 bpf_prog_free_id(prog, do_idr_lock);
2048 if (in_irq() || irqs_disabled()) {
2049 INIT_WORK(&aux->work, bpf_prog_put_deferred);
2050 schedule_work(&aux->work);
2052 bpf_prog_put_deferred(&aux->work);
2057 void bpf_prog_put(struct bpf_prog *prog)
2059 __bpf_prog_put(prog, true);
2061 EXPORT_SYMBOL_GPL(bpf_prog_put);
2063 static int bpf_prog_release(struct inode *inode, struct file *filp)
2065 struct bpf_prog *prog = filp->private_data;
2071 struct bpf_prog_kstats {
2077 static void bpf_prog_get_stats(const struct bpf_prog *prog,
2078 struct bpf_prog_kstats *stats)
2080 u64 nsecs = 0, cnt = 0, misses = 0;
2083 for_each_possible_cpu(cpu) {
2084 const struct bpf_prog_stats *st;
2086 u64 tnsecs, tcnt, tmisses;
2088 st = per_cpu_ptr(prog->stats, cpu);
2090 start = u64_stats_fetch_begin_irq(&st->syncp);
2091 tnsecs = u64_stats_read(&st->nsecs);
2092 tcnt = u64_stats_read(&st->cnt);
2093 tmisses = u64_stats_read(&st->misses);
2094 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
2099 stats->nsecs = nsecs;
2101 stats->misses = misses;
2104 #ifdef CONFIG_PROC_FS
2105 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
2107 const struct bpf_prog *prog = filp->private_data;
2108 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2109 struct bpf_prog_kstats stats;
2111 bpf_prog_get_stats(prog, &stats);
2112 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2119 "run_time_ns:\t%llu\n"
2121 "recursion_misses:\t%llu\n"
2122 "verified_insns:\t%u\n",
2126 prog->pages * 1ULL << PAGE_SHIFT,
2131 prog->aux->verified_insns);
2135 const struct file_operations bpf_prog_fops = {
2136 #ifdef CONFIG_PROC_FS
2137 .show_fdinfo = bpf_prog_show_fdinfo,
2139 .release = bpf_prog_release,
2140 .read = bpf_dummy_read,
2141 .write = bpf_dummy_write,
2144 int bpf_prog_new_fd(struct bpf_prog *prog)
2148 ret = security_bpf_prog(prog);
2152 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
2153 O_RDWR | O_CLOEXEC);
2156 static struct bpf_prog *____bpf_prog_get(struct fd f)
2159 return ERR_PTR(-EBADF);
2160 if (f.file->f_op != &bpf_prog_fops) {
2162 return ERR_PTR(-EINVAL);
2165 return f.file->private_data;
2168 void bpf_prog_add(struct bpf_prog *prog, int i)
2170 atomic64_add(i, &prog->aux->refcnt);
2172 EXPORT_SYMBOL_GPL(bpf_prog_add);
2174 void bpf_prog_sub(struct bpf_prog *prog, int i)
2176 /* Only to be used for undoing previous bpf_prog_add() in some
2177 * error path. We still know that another entity in our call
2178 * path holds a reference to the program, thus atomic_sub() can
2179 * be safely used in such cases!
2181 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
2183 EXPORT_SYMBOL_GPL(bpf_prog_sub);
2185 void bpf_prog_inc(struct bpf_prog *prog)
2187 atomic64_inc(&prog->aux->refcnt);
2189 EXPORT_SYMBOL_GPL(bpf_prog_inc);
2191 /* prog_idr_lock should have been held */
2192 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
2196 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
2199 return ERR_PTR(-ENOENT);
2203 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
2205 bool bpf_prog_get_ok(struct bpf_prog *prog,
2206 enum bpf_prog_type *attach_type, bool attach_drv)
2208 /* not an attachment, just a refcount inc, always allow */
2212 if (prog->type != *attach_type)
2214 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
2220 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
2223 struct fd f = fdget(ufd);
2224 struct bpf_prog *prog;
2226 prog = ____bpf_prog_get(f);
2229 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
2230 prog = ERR_PTR(-EINVAL);
2240 struct bpf_prog *bpf_prog_get(u32 ufd)
2242 return __bpf_prog_get(ufd, NULL, false);
2245 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
2248 return __bpf_prog_get(ufd, &type, attach_drv);
2250 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
2252 /* Initially all BPF programs could be loaded w/o specifying
2253 * expected_attach_type. Later for some of them specifying expected_attach_type
2254 * at load time became required so that program could be validated properly.
2255 * Programs of types that are allowed to be loaded both w/ and w/o (for
2256 * backward compatibility) expected_attach_type, should have the default attach
2257 * type assigned to expected_attach_type for the latter case, so that it can be
2258 * validated later at attach time.
2260 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
2261 * prog type requires it but has some attach types that have to be backward
2264 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
2266 switch (attr->prog_type) {
2267 case BPF_PROG_TYPE_CGROUP_SOCK:
2268 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
2269 * exist so checking for non-zero is the way to go here.
2271 if (!attr->expected_attach_type)
2272 attr->expected_attach_type =
2273 BPF_CGROUP_INET_SOCK_CREATE;
2275 case BPF_PROG_TYPE_SK_REUSEPORT:
2276 if (!attr->expected_attach_type)
2277 attr->expected_attach_type =
2278 BPF_SK_REUSEPORT_SELECT;
2284 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2285 enum bpf_attach_type expected_attach_type,
2286 struct btf *attach_btf, u32 btf_id,
2287 struct bpf_prog *dst_prog)
2290 if (btf_id > BTF_MAX_TYPE)
2293 if (!attach_btf && !dst_prog)
2296 switch (prog_type) {
2297 case BPF_PROG_TYPE_TRACING:
2298 case BPF_PROG_TYPE_LSM:
2299 case BPF_PROG_TYPE_STRUCT_OPS:
2300 case BPF_PROG_TYPE_EXT:
2307 if (attach_btf && (!btf_id || dst_prog))
2310 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
2311 prog_type != BPF_PROG_TYPE_EXT)
2314 switch (prog_type) {
2315 case BPF_PROG_TYPE_CGROUP_SOCK:
2316 switch (expected_attach_type) {
2317 case BPF_CGROUP_INET_SOCK_CREATE:
2318 case BPF_CGROUP_INET_SOCK_RELEASE:
2319 case BPF_CGROUP_INET4_POST_BIND:
2320 case BPF_CGROUP_INET6_POST_BIND:
2325 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2326 switch (expected_attach_type) {
2327 case BPF_CGROUP_INET4_BIND:
2328 case BPF_CGROUP_INET6_BIND:
2329 case BPF_CGROUP_INET4_CONNECT:
2330 case BPF_CGROUP_INET6_CONNECT:
2331 case BPF_CGROUP_INET4_GETPEERNAME:
2332 case BPF_CGROUP_INET6_GETPEERNAME:
2333 case BPF_CGROUP_INET4_GETSOCKNAME:
2334 case BPF_CGROUP_INET6_GETSOCKNAME:
2335 case BPF_CGROUP_UDP4_SENDMSG:
2336 case BPF_CGROUP_UDP6_SENDMSG:
2337 case BPF_CGROUP_UDP4_RECVMSG:
2338 case BPF_CGROUP_UDP6_RECVMSG:
2343 case BPF_PROG_TYPE_CGROUP_SKB:
2344 switch (expected_attach_type) {
2345 case BPF_CGROUP_INET_INGRESS:
2346 case BPF_CGROUP_INET_EGRESS:
2351 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2352 switch (expected_attach_type) {
2353 case BPF_CGROUP_SETSOCKOPT:
2354 case BPF_CGROUP_GETSOCKOPT:
2359 case BPF_PROG_TYPE_SK_LOOKUP:
2360 if (expected_attach_type == BPF_SK_LOOKUP)
2363 case BPF_PROG_TYPE_SK_REUSEPORT:
2364 switch (expected_attach_type) {
2365 case BPF_SK_REUSEPORT_SELECT:
2366 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2371 case BPF_PROG_TYPE_SYSCALL:
2372 case BPF_PROG_TYPE_EXT:
2373 if (expected_attach_type)
2381 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2383 switch (prog_type) {
2384 case BPF_PROG_TYPE_SCHED_CLS:
2385 case BPF_PROG_TYPE_SCHED_ACT:
2386 case BPF_PROG_TYPE_XDP:
2387 case BPF_PROG_TYPE_LWT_IN:
2388 case BPF_PROG_TYPE_LWT_OUT:
2389 case BPF_PROG_TYPE_LWT_XMIT:
2390 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2391 case BPF_PROG_TYPE_SK_SKB:
2392 case BPF_PROG_TYPE_SK_MSG:
2393 case BPF_PROG_TYPE_LIRC_MODE2:
2394 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2395 case BPF_PROG_TYPE_CGROUP_DEVICE:
2396 case BPF_PROG_TYPE_CGROUP_SOCK:
2397 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2398 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2399 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2400 case BPF_PROG_TYPE_SOCK_OPS:
2401 case BPF_PROG_TYPE_EXT: /* extends any prog */
2403 case BPF_PROG_TYPE_CGROUP_SKB:
2405 case BPF_PROG_TYPE_SK_REUSEPORT:
2406 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2412 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2414 switch (prog_type) {
2415 case BPF_PROG_TYPE_KPROBE:
2416 case BPF_PROG_TYPE_TRACEPOINT:
2417 case BPF_PROG_TYPE_PERF_EVENT:
2418 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2419 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2420 case BPF_PROG_TYPE_TRACING:
2421 case BPF_PROG_TYPE_LSM:
2422 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2423 case BPF_PROG_TYPE_EXT: /* extends any prog */
2430 /* last field in 'union bpf_attr' used by this command */
2431 #define BPF_PROG_LOAD_LAST_FIELD core_relo_rec_size
2433 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
2435 enum bpf_prog_type type = attr->prog_type;
2436 struct bpf_prog *prog, *dst_prog = NULL;
2437 struct btf *attach_btf = NULL;
2442 if (CHECK_ATTR(BPF_PROG_LOAD))
2445 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2446 BPF_F_ANY_ALIGNMENT |
2447 BPF_F_TEST_STATE_FREQ |
2449 BPF_F_TEST_RND_HI32 |
2450 BPF_F_XDP_HAS_FRAGS))
2453 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2454 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2458 /* copy eBPF program license from user space */
2459 if (strncpy_from_bpfptr(license,
2460 make_bpfptr(attr->license, uattr.is_kernel),
2461 sizeof(license) - 1) < 0)
2463 license[sizeof(license) - 1] = 0;
2465 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2466 is_gpl = license_is_gpl_compatible(license);
2468 if (attr->insn_cnt == 0 ||
2469 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2471 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2472 type != BPF_PROG_TYPE_CGROUP_SKB &&
2476 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2478 if (is_perfmon_prog_type(type) && !perfmon_capable())
2481 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2482 * or btf, we need to check which one it is
2484 if (attr->attach_prog_fd) {
2485 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2486 if (IS_ERR(dst_prog)) {
2488 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2489 if (IS_ERR(attach_btf))
2491 if (!btf_is_kernel(attach_btf)) {
2492 /* attaching through specifying bpf_prog's BTF
2493 * objects directly might be supported eventually
2495 btf_put(attach_btf);
2499 } else if (attr->attach_btf_id) {
2500 /* fall back to vmlinux BTF, if BTF type ID is specified */
2501 attach_btf = bpf_get_btf_vmlinux();
2502 if (IS_ERR(attach_btf))
2503 return PTR_ERR(attach_btf);
2506 btf_get(attach_btf);
2509 bpf_prog_load_fixup_attach_type(attr);
2510 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2511 attach_btf, attr->attach_btf_id,
2514 bpf_prog_put(dst_prog);
2516 btf_put(attach_btf);
2520 /* plain bpf_prog allocation */
2521 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2524 bpf_prog_put(dst_prog);
2526 btf_put(attach_btf);
2530 prog->expected_attach_type = attr->expected_attach_type;
2531 prog->aux->attach_btf = attach_btf;
2532 prog->aux->attach_btf_id = attr->attach_btf_id;
2533 prog->aux->dst_prog = dst_prog;
2534 prog->aux->offload_requested = !!attr->prog_ifindex;
2535 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2536 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
2538 err = security_bpf_prog_alloc(prog->aux);
2542 prog->aux->user = get_current_user();
2543 prog->len = attr->insn_cnt;
2546 if (copy_from_bpfptr(prog->insns,
2547 make_bpfptr(attr->insns, uattr.is_kernel),
2548 bpf_prog_insn_size(prog)) != 0)
2551 prog->orig_prog = NULL;
2554 atomic64_set(&prog->aux->refcnt, 1);
2555 prog->gpl_compatible = is_gpl ? 1 : 0;
2557 if (bpf_prog_is_dev_bound(prog->aux)) {
2558 err = bpf_prog_offload_init(prog, attr);
2563 /* find program type: socket_filter vs tracing_filter */
2564 err = find_prog_type(type, prog);
2568 prog->aux->load_time = ktime_get_boottime_ns();
2569 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2570 sizeof(attr->prog_name));
2574 /* run eBPF verifier */
2575 err = bpf_check(&prog, attr, uattr);
2577 goto free_used_maps;
2579 prog = bpf_prog_select_runtime(prog, &err);
2581 goto free_used_maps;
2583 err = bpf_prog_alloc_id(prog);
2585 goto free_used_maps;
2587 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2588 * effectively publicly exposed. However, retrieving via
2589 * bpf_prog_get_fd_by_id() will take another reference,
2590 * therefore it cannot be gone underneath us.
2592 * Only for the time /after/ successful bpf_prog_new_fd()
2593 * and before returning to userspace, we might just hold
2594 * one reference and any parallel close on that fd could
2595 * rip everything out. Hence, below notifications must
2596 * happen before bpf_prog_new_fd().
2598 * Also, any failure handling from this point onwards must
2599 * be using bpf_prog_put() given the program is exposed.
2601 bpf_prog_kallsyms_add(prog);
2602 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2603 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2605 err = bpf_prog_new_fd(prog);
2611 /* In case we have subprogs, we need to wait for a grace
2612 * period before we can tear down JIT memory since symbols
2613 * are already exposed under kallsyms.
2615 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2618 free_uid(prog->aux->user);
2619 security_bpf_prog_free(prog->aux);
2621 if (prog->aux->attach_btf)
2622 btf_put(prog->aux->attach_btf);
2623 bpf_prog_free(prog);
2627 #define BPF_OBJ_LAST_FIELD file_flags
2629 static int bpf_obj_pin(const union bpf_attr *attr)
2631 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2634 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2637 static int bpf_obj_get(const union bpf_attr *attr)
2639 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2640 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2643 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2647 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2648 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2650 atomic64_set(&link->refcnt, 1);
2657 static void bpf_link_free_id(int id)
2662 spin_lock_bh(&link_idr_lock);
2663 idr_remove(&link_idr, id);
2664 spin_unlock_bh(&link_idr_lock);
2667 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2668 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2669 * anon_inode's release() call. This helper marksbpf_link as
2670 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2671 * is not decremented, it's the responsibility of a calling code that failed
2672 * to complete bpf_link initialization.
2674 void bpf_link_cleanup(struct bpf_link_primer *primer)
2676 primer->link->prog = NULL;
2677 bpf_link_free_id(primer->id);
2679 put_unused_fd(primer->fd);
2682 void bpf_link_inc(struct bpf_link *link)
2684 atomic64_inc(&link->refcnt);
2687 /* bpf_link_free is guaranteed to be called from process context */
2688 static void bpf_link_free(struct bpf_link *link)
2690 bpf_link_free_id(link->id);
2692 /* detach BPF program, clean up used resources */
2693 link->ops->release(link);
2694 bpf_prog_put(link->prog);
2696 /* free bpf_link and its containing memory */
2697 link->ops->dealloc(link);
2700 static void bpf_link_put_deferred(struct work_struct *work)
2702 struct bpf_link *link = container_of(work, struct bpf_link, work);
2704 bpf_link_free(link);
2707 /* bpf_link_put can be called from atomic context, but ensures that resources
2708 * are freed from process context
2710 void bpf_link_put(struct bpf_link *link)
2712 if (!atomic64_dec_and_test(&link->refcnt))
2716 INIT_WORK(&link->work, bpf_link_put_deferred);
2717 schedule_work(&link->work);
2719 bpf_link_free(link);
2722 EXPORT_SYMBOL(bpf_link_put);
2724 static int bpf_link_release(struct inode *inode, struct file *filp)
2726 struct bpf_link *link = filp->private_data;
2732 #ifdef CONFIG_PROC_FS
2733 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2734 #define BPF_MAP_TYPE(_id, _ops)
2735 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2736 static const char *bpf_link_type_strs[] = {
2737 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2738 #include <linux/bpf_types.h>
2740 #undef BPF_PROG_TYPE
2742 #undef BPF_LINK_TYPE
2744 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2746 const struct bpf_link *link = filp->private_data;
2747 const struct bpf_prog *prog = link->prog;
2748 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2750 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2756 bpf_link_type_strs[link->type],
2760 if (link->ops->show_fdinfo)
2761 link->ops->show_fdinfo(link, m);
2765 static const struct file_operations bpf_link_fops = {
2766 #ifdef CONFIG_PROC_FS
2767 .show_fdinfo = bpf_link_show_fdinfo,
2769 .release = bpf_link_release,
2770 .read = bpf_dummy_read,
2771 .write = bpf_dummy_write,
2774 static int bpf_link_alloc_id(struct bpf_link *link)
2778 idr_preload(GFP_KERNEL);
2779 spin_lock_bh(&link_idr_lock);
2780 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2781 spin_unlock_bh(&link_idr_lock);
2787 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2788 * reserving unused FD and allocating ID from link_idr. This is to be paired
2789 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2790 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2791 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2792 * transient state is passed around in struct bpf_link_primer.
2793 * This is preferred way to create and initialize bpf_link, especially when
2794 * there are complicated and expensive operations in between creating bpf_link
2795 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2796 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2797 * expensive (and potentially failing) roll back operations in a rare case
2798 * that file, FD, or ID can't be allocated.
2800 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2805 fd = get_unused_fd_flags(O_CLOEXEC);
2810 id = bpf_link_alloc_id(link);
2816 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2818 bpf_link_free_id(id);
2820 return PTR_ERR(file);
2823 primer->link = link;
2824 primer->file = file;
2830 int bpf_link_settle(struct bpf_link_primer *primer)
2832 /* make bpf_link fetchable by ID */
2833 spin_lock_bh(&link_idr_lock);
2834 primer->link->id = primer->id;
2835 spin_unlock_bh(&link_idr_lock);
2836 /* make bpf_link fetchable by FD */
2837 fd_install(primer->fd, primer->file);
2838 /* pass through installed FD */
2842 int bpf_link_new_fd(struct bpf_link *link)
2844 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2847 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2849 struct fd f = fdget(ufd);
2850 struct bpf_link *link;
2853 return ERR_PTR(-EBADF);
2854 if (f.file->f_op != &bpf_link_fops) {
2856 return ERR_PTR(-EINVAL);
2859 link = f.file->private_data;
2865 EXPORT_SYMBOL(bpf_link_get_from_fd);
2867 static void bpf_tracing_link_release(struct bpf_link *link)
2869 struct bpf_tracing_link *tr_link =
2870 container_of(link, struct bpf_tracing_link, link.link);
2872 WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link,
2873 tr_link->trampoline));
2875 bpf_trampoline_put(tr_link->trampoline);
2877 /* tgt_prog is NULL if target is a kernel function */
2878 if (tr_link->tgt_prog)
2879 bpf_prog_put(tr_link->tgt_prog);
2882 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2884 struct bpf_tracing_link *tr_link =
2885 container_of(link, struct bpf_tracing_link, link.link);
2890 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2891 struct seq_file *seq)
2893 struct bpf_tracing_link *tr_link =
2894 container_of(link, struct bpf_tracing_link, link.link);
2897 "attach_type:\t%d\n",
2898 tr_link->attach_type);
2901 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2902 struct bpf_link_info *info)
2904 struct bpf_tracing_link *tr_link =
2905 container_of(link, struct bpf_tracing_link, link.link);
2907 info->tracing.attach_type = tr_link->attach_type;
2908 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2909 &info->tracing.target_obj_id,
2910 &info->tracing.target_btf_id);
2915 static const struct bpf_link_ops bpf_tracing_link_lops = {
2916 .release = bpf_tracing_link_release,
2917 .dealloc = bpf_tracing_link_dealloc,
2918 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2919 .fill_link_info = bpf_tracing_link_fill_link_info,
2922 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2927 struct bpf_link_primer link_primer;
2928 struct bpf_prog *tgt_prog = NULL;
2929 struct bpf_trampoline *tr = NULL;
2930 struct bpf_tracing_link *link;
2934 switch (prog->type) {
2935 case BPF_PROG_TYPE_TRACING:
2936 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2937 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2938 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2943 case BPF_PROG_TYPE_EXT:
2944 if (prog->expected_attach_type != 0) {
2949 case BPF_PROG_TYPE_LSM:
2950 if (prog->expected_attach_type != BPF_LSM_MAC) {
2960 if (!!tgt_prog_fd != !!btf_id) {
2966 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2967 if (prog->type != BPF_PROG_TYPE_EXT) {
2972 tgt_prog = bpf_prog_get(tgt_prog_fd);
2973 if (IS_ERR(tgt_prog)) {
2974 err = PTR_ERR(tgt_prog);
2979 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
2982 link = kzalloc(sizeof(*link), GFP_USER);
2987 bpf_link_init(&link->link.link, BPF_LINK_TYPE_TRACING,
2988 &bpf_tracing_link_lops, prog);
2989 link->attach_type = prog->expected_attach_type;
2990 link->link.cookie = bpf_cookie;
2992 mutex_lock(&prog->aux->dst_mutex);
2994 /* There are a few possible cases here:
2996 * - if prog->aux->dst_trampoline is set, the program was just loaded
2997 * and not yet attached to anything, so we can use the values stored
3000 * - if prog->aux->dst_trampoline is NULL, the program has already been
3001 * attached to a target and its initial target was cleared (below)
3003 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
3004 * target_btf_id using the link_create API.
3006 * - if tgt_prog == NULL when this function was called using the old
3007 * raw_tracepoint_open API, and we need a target from prog->aux
3009 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
3010 * was detached and is going for re-attachment.
3012 if (!prog->aux->dst_trampoline && !tgt_prog) {
3014 * Allow re-attach for TRACING and LSM programs. If it's
3015 * currently linked, bpf_trampoline_link_prog will fail.
3016 * EXT programs need to specify tgt_prog_fd, so they
3017 * re-attach in separate code path.
3019 if (prog->type != BPF_PROG_TYPE_TRACING &&
3020 prog->type != BPF_PROG_TYPE_LSM) {
3024 btf_id = prog->aux->attach_btf_id;
3025 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
3028 if (!prog->aux->dst_trampoline ||
3029 (key && key != prog->aux->dst_trampoline->key)) {
3030 /* If there is no saved target, or the specified target is
3031 * different from the destination specified at load time, we
3032 * need a new trampoline and a check for compatibility
3034 struct bpf_attach_target_info tgt_info = {};
3036 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
3041 tr = bpf_trampoline_get(key, &tgt_info);
3047 /* The caller didn't specify a target, or the target was the
3048 * same as the destination supplied during program load. This
3049 * means we can reuse the trampoline and reference from program
3050 * load time, and there is no need to allocate a new one. This
3051 * can only happen once for any program, as the saved values in
3052 * prog->aux are cleared below.
3054 tr = prog->aux->dst_trampoline;
3055 tgt_prog = prog->aux->dst_prog;
3058 err = bpf_link_prime(&link->link.link, &link_primer);
3062 err = bpf_trampoline_link_prog(&link->link, tr);
3064 bpf_link_cleanup(&link_primer);
3069 link->tgt_prog = tgt_prog;
3070 link->trampoline = tr;
3072 /* Always clear the trampoline and target prog from prog->aux to make
3073 * sure the original attach destination is not kept alive after a
3074 * program is (re-)attached to another target.
3076 if (prog->aux->dst_prog &&
3077 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
3078 /* got extra prog ref from syscall, or attaching to different prog */
3079 bpf_prog_put(prog->aux->dst_prog);
3080 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
3081 /* we allocated a new trampoline, so free the old one */
3082 bpf_trampoline_put(prog->aux->dst_trampoline);
3084 prog->aux->dst_prog = NULL;
3085 prog->aux->dst_trampoline = NULL;
3086 mutex_unlock(&prog->aux->dst_mutex);
3088 return bpf_link_settle(&link_primer);
3090 if (tr && tr != prog->aux->dst_trampoline)
3091 bpf_trampoline_put(tr);
3092 mutex_unlock(&prog->aux->dst_mutex);
3095 if (tgt_prog_fd && tgt_prog)
3096 bpf_prog_put(tgt_prog);
3100 struct bpf_raw_tp_link {
3101 struct bpf_link link;
3102 struct bpf_raw_event_map *btp;
3105 static void bpf_raw_tp_link_release(struct bpf_link *link)
3107 struct bpf_raw_tp_link *raw_tp =
3108 container_of(link, struct bpf_raw_tp_link, link);
3110 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
3111 bpf_put_raw_tracepoint(raw_tp->btp);
3114 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
3116 struct bpf_raw_tp_link *raw_tp =
3117 container_of(link, struct bpf_raw_tp_link, link);
3122 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
3123 struct seq_file *seq)
3125 struct bpf_raw_tp_link *raw_tp_link =
3126 container_of(link, struct bpf_raw_tp_link, link);
3130 raw_tp_link->btp->tp->name);
3133 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
3134 struct bpf_link_info *info)
3136 struct bpf_raw_tp_link *raw_tp_link =
3137 container_of(link, struct bpf_raw_tp_link, link);
3138 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
3139 const char *tp_name = raw_tp_link->btp->tp->name;
3140 u32 ulen = info->raw_tracepoint.tp_name_len;
3141 size_t tp_len = strlen(tp_name);
3146 info->raw_tracepoint.tp_name_len = tp_len + 1;
3151 if (ulen >= tp_len + 1) {
3152 if (copy_to_user(ubuf, tp_name, tp_len + 1))
3157 if (copy_to_user(ubuf, tp_name, ulen - 1))
3159 if (put_user(zero, ubuf + ulen - 1))
3167 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
3168 .release = bpf_raw_tp_link_release,
3169 .dealloc = bpf_raw_tp_link_dealloc,
3170 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
3171 .fill_link_info = bpf_raw_tp_link_fill_link_info,
3174 #ifdef CONFIG_PERF_EVENTS
3175 struct bpf_perf_link {
3176 struct bpf_link link;
3177 struct file *perf_file;
3180 static void bpf_perf_link_release(struct bpf_link *link)
3182 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3183 struct perf_event *event = perf_link->perf_file->private_data;
3185 perf_event_free_bpf_prog(event);
3186 fput(perf_link->perf_file);
3189 static void bpf_perf_link_dealloc(struct bpf_link *link)
3191 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3196 static const struct bpf_link_ops bpf_perf_link_lops = {
3197 .release = bpf_perf_link_release,
3198 .dealloc = bpf_perf_link_dealloc,
3201 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3203 struct bpf_link_primer link_primer;
3204 struct bpf_perf_link *link;
3205 struct perf_event *event;
3206 struct file *perf_file;
3209 if (attr->link_create.flags)
3212 perf_file = perf_event_get(attr->link_create.target_fd);
3213 if (IS_ERR(perf_file))
3214 return PTR_ERR(perf_file);
3216 link = kzalloc(sizeof(*link), GFP_USER);
3221 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog);
3222 link->perf_file = perf_file;
3224 err = bpf_link_prime(&link->link, &link_primer);
3230 event = perf_file->private_data;
3231 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
3233 bpf_link_cleanup(&link_primer);
3236 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
3239 return bpf_link_settle(&link_primer);
3246 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3250 #endif /* CONFIG_PERF_EVENTS */
3252 static int bpf_raw_tp_link_attach(struct bpf_prog *prog,
3253 const char __user *user_tp_name)
3255 struct bpf_link_primer link_primer;
3256 struct bpf_raw_tp_link *link;
3257 struct bpf_raw_event_map *btp;
3258 const char *tp_name;
3262 switch (prog->type) {
3263 case BPF_PROG_TYPE_TRACING:
3264 case BPF_PROG_TYPE_EXT:
3265 case BPF_PROG_TYPE_LSM:
3267 /* The attach point for this category of programs
3268 * should be specified via btf_id during program load.
3271 if (prog->type == BPF_PROG_TYPE_TRACING &&
3272 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
3273 tp_name = prog->aux->attach_func_name;
3276 return bpf_tracing_prog_attach(prog, 0, 0, 0);
3277 case BPF_PROG_TYPE_RAW_TRACEPOINT:
3278 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
3279 if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0)
3281 buf[sizeof(buf) - 1] = 0;
3288 btp = bpf_get_raw_tracepoint(tp_name);
3292 link = kzalloc(sizeof(*link), GFP_USER);
3297 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
3298 &bpf_raw_tp_link_lops, prog);
3301 err = bpf_link_prime(&link->link, &link_primer);
3307 err = bpf_probe_register(link->btp, prog);
3309 bpf_link_cleanup(&link_primer);
3313 return bpf_link_settle(&link_primer);
3316 bpf_put_raw_tracepoint(btp);
3320 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
3322 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
3324 struct bpf_prog *prog;
3327 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
3330 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
3332 return PTR_ERR(prog);
3334 fd = bpf_raw_tp_link_attach(prog, u64_to_user_ptr(attr->raw_tracepoint.name));
3340 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
3341 enum bpf_attach_type attach_type)
3343 switch (prog->type) {
3344 case BPF_PROG_TYPE_CGROUP_SOCK:
3345 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3346 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3347 case BPF_PROG_TYPE_SK_LOOKUP:
3348 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
3349 case BPF_PROG_TYPE_CGROUP_SKB:
3350 if (!capable(CAP_NET_ADMIN))
3351 /* cg-skb progs can be loaded by unpriv user.
3352 * check permissions at attach time.
3355 return prog->enforce_expected_attach_type &&
3356 prog->expected_attach_type != attach_type ?
3363 static enum bpf_prog_type
3364 attach_type_to_prog_type(enum bpf_attach_type attach_type)
3366 switch (attach_type) {
3367 case BPF_CGROUP_INET_INGRESS:
3368 case BPF_CGROUP_INET_EGRESS:
3369 return BPF_PROG_TYPE_CGROUP_SKB;
3370 case BPF_CGROUP_INET_SOCK_CREATE:
3371 case BPF_CGROUP_INET_SOCK_RELEASE:
3372 case BPF_CGROUP_INET4_POST_BIND:
3373 case BPF_CGROUP_INET6_POST_BIND:
3374 return BPF_PROG_TYPE_CGROUP_SOCK;
3375 case BPF_CGROUP_INET4_BIND:
3376 case BPF_CGROUP_INET6_BIND:
3377 case BPF_CGROUP_INET4_CONNECT:
3378 case BPF_CGROUP_INET6_CONNECT:
3379 case BPF_CGROUP_INET4_GETPEERNAME:
3380 case BPF_CGROUP_INET6_GETPEERNAME:
3381 case BPF_CGROUP_INET4_GETSOCKNAME:
3382 case BPF_CGROUP_INET6_GETSOCKNAME:
3383 case BPF_CGROUP_UDP4_SENDMSG:
3384 case BPF_CGROUP_UDP6_SENDMSG:
3385 case BPF_CGROUP_UDP4_RECVMSG:
3386 case BPF_CGROUP_UDP6_RECVMSG:
3387 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3388 case BPF_CGROUP_SOCK_OPS:
3389 return BPF_PROG_TYPE_SOCK_OPS;
3390 case BPF_CGROUP_DEVICE:
3391 return BPF_PROG_TYPE_CGROUP_DEVICE;
3392 case BPF_SK_MSG_VERDICT:
3393 return BPF_PROG_TYPE_SK_MSG;
3394 case BPF_SK_SKB_STREAM_PARSER:
3395 case BPF_SK_SKB_STREAM_VERDICT:
3396 case BPF_SK_SKB_VERDICT:
3397 return BPF_PROG_TYPE_SK_SKB;
3398 case BPF_LIRC_MODE2:
3399 return BPF_PROG_TYPE_LIRC_MODE2;
3400 case BPF_FLOW_DISSECTOR:
3401 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3402 case BPF_CGROUP_SYSCTL:
3403 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3404 case BPF_CGROUP_GETSOCKOPT:
3405 case BPF_CGROUP_SETSOCKOPT:
3406 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
3407 case BPF_TRACE_ITER:
3408 case BPF_TRACE_RAW_TP:
3409 case BPF_TRACE_FENTRY:
3410 case BPF_TRACE_FEXIT:
3411 case BPF_MODIFY_RETURN:
3412 return BPF_PROG_TYPE_TRACING;
3414 return BPF_PROG_TYPE_LSM;
3416 return BPF_PROG_TYPE_SK_LOOKUP;
3418 return BPF_PROG_TYPE_XDP;
3420 return BPF_PROG_TYPE_UNSPEC;
3424 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
3426 #define BPF_F_ATTACH_MASK \
3427 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
3429 static int bpf_prog_attach(const union bpf_attr *attr)
3431 enum bpf_prog_type ptype;
3432 struct bpf_prog *prog;
3435 if (CHECK_ATTR(BPF_PROG_ATTACH))
3438 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
3441 ptype = attach_type_to_prog_type(attr->attach_type);
3442 if (ptype == BPF_PROG_TYPE_UNSPEC)
3445 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3447 return PTR_ERR(prog);
3449 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3455 case BPF_PROG_TYPE_SK_SKB:
3456 case BPF_PROG_TYPE_SK_MSG:
3457 ret = sock_map_get_from_fd(attr, prog);
3459 case BPF_PROG_TYPE_LIRC_MODE2:
3460 ret = lirc_prog_attach(attr, prog);
3462 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3463 ret = netns_bpf_prog_attach(attr, prog);
3465 case BPF_PROG_TYPE_CGROUP_DEVICE:
3466 case BPF_PROG_TYPE_CGROUP_SKB:
3467 case BPF_PROG_TYPE_CGROUP_SOCK:
3468 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3469 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3470 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3471 case BPF_PROG_TYPE_SOCK_OPS:
3472 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3483 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3485 static int bpf_prog_detach(const union bpf_attr *attr)
3487 enum bpf_prog_type ptype;
3489 if (CHECK_ATTR(BPF_PROG_DETACH))
3492 ptype = attach_type_to_prog_type(attr->attach_type);
3495 case BPF_PROG_TYPE_SK_MSG:
3496 case BPF_PROG_TYPE_SK_SKB:
3497 return sock_map_prog_detach(attr, ptype);
3498 case BPF_PROG_TYPE_LIRC_MODE2:
3499 return lirc_prog_detach(attr);
3500 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3501 return netns_bpf_prog_detach(attr, ptype);
3502 case BPF_PROG_TYPE_CGROUP_DEVICE:
3503 case BPF_PROG_TYPE_CGROUP_SKB:
3504 case BPF_PROG_TYPE_CGROUP_SOCK:
3505 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3506 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3507 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3508 case BPF_PROG_TYPE_SOCK_OPS:
3509 return cgroup_bpf_prog_detach(attr, ptype);
3515 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3517 static int bpf_prog_query(const union bpf_attr *attr,
3518 union bpf_attr __user *uattr)
3520 if (!capable(CAP_NET_ADMIN))
3522 if (CHECK_ATTR(BPF_PROG_QUERY))
3524 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3527 switch (attr->query.attach_type) {
3528 case BPF_CGROUP_INET_INGRESS:
3529 case BPF_CGROUP_INET_EGRESS:
3530 case BPF_CGROUP_INET_SOCK_CREATE:
3531 case BPF_CGROUP_INET_SOCK_RELEASE:
3532 case BPF_CGROUP_INET4_BIND:
3533 case BPF_CGROUP_INET6_BIND:
3534 case BPF_CGROUP_INET4_POST_BIND:
3535 case BPF_CGROUP_INET6_POST_BIND:
3536 case BPF_CGROUP_INET4_CONNECT:
3537 case BPF_CGROUP_INET6_CONNECT:
3538 case BPF_CGROUP_INET4_GETPEERNAME:
3539 case BPF_CGROUP_INET6_GETPEERNAME:
3540 case BPF_CGROUP_INET4_GETSOCKNAME:
3541 case BPF_CGROUP_INET6_GETSOCKNAME:
3542 case BPF_CGROUP_UDP4_SENDMSG:
3543 case BPF_CGROUP_UDP6_SENDMSG:
3544 case BPF_CGROUP_UDP4_RECVMSG:
3545 case BPF_CGROUP_UDP6_RECVMSG:
3546 case BPF_CGROUP_SOCK_OPS:
3547 case BPF_CGROUP_DEVICE:
3548 case BPF_CGROUP_SYSCTL:
3549 case BPF_CGROUP_GETSOCKOPT:
3550 case BPF_CGROUP_SETSOCKOPT:
3551 return cgroup_bpf_prog_query(attr, uattr);
3552 case BPF_LIRC_MODE2:
3553 return lirc_prog_query(attr, uattr);
3554 case BPF_FLOW_DISSECTOR:
3556 return netns_bpf_prog_query(attr, uattr);
3557 case BPF_SK_SKB_STREAM_PARSER:
3558 case BPF_SK_SKB_STREAM_VERDICT:
3559 case BPF_SK_MSG_VERDICT:
3560 case BPF_SK_SKB_VERDICT:
3561 return sock_map_bpf_prog_query(attr, uattr);
3567 #define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size
3569 static int bpf_prog_test_run(const union bpf_attr *attr,
3570 union bpf_attr __user *uattr)
3572 struct bpf_prog *prog;
3573 int ret = -ENOTSUPP;
3575 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3578 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3579 (!attr->test.ctx_size_in && attr->test.ctx_in))
3582 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3583 (!attr->test.ctx_size_out && attr->test.ctx_out))
3586 prog = bpf_prog_get(attr->test.prog_fd);
3588 return PTR_ERR(prog);
3590 if (prog->aux->ops->test_run)
3591 ret = prog->aux->ops->test_run(prog, attr, uattr);
3597 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3599 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3600 union bpf_attr __user *uattr,
3604 u32 next_id = attr->start_id;
3607 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3610 if (!capable(CAP_SYS_ADMIN))
3615 if (!idr_get_next(idr, &next_id))
3617 spin_unlock_bh(lock);
3620 err = put_user(next_id, &uattr->next_id);
3625 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3627 struct bpf_map *map;
3629 spin_lock_bh(&map_idr_lock);
3631 map = idr_get_next(&map_idr, id);
3633 map = __bpf_map_inc_not_zero(map, false);
3639 spin_unlock_bh(&map_idr_lock);
3644 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3646 struct bpf_prog *prog;
3648 spin_lock_bh(&prog_idr_lock);
3650 prog = idr_get_next(&prog_idr, id);
3652 prog = bpf_prog_inc_not_zero(prog);
3658 spin_unlock_bh(&prog_idr_lock);
3663 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3665 struct bpf_prog *bpf_prog_by_id(u32 id)
3667 struct bpf_prog *prog;
3670 return ERR_PTR(-ENOENT);
3672 spin_lock_bh(&prog_idr_lock);
3673 prog = idr_find(&prog_idr, id);
3675 prog = bpf_prog_inc_not_zero(prog);
3677 prog = ERR_PTR(-ENOENT);
3678 spin_unlock_bh(&prog_idr_lock);
3682 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3684 struct bpf_prog *prog;
3685 u32 id = attr->prog_id;
3688 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3691 if (!capable(CAP_SYS_ADMIN))
3694 prog = bpf_prog_by_id(id);
3696 return PTR_ERR(prog);
3698 fd = bpf_prog_new_fd(prog);
3705 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3707 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3709 struct bpf_map *map;
3710 u32 id = attr->map_id;
3714 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3715 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3718 if (!capable(CAP_SYS_ADMIN))
3721 f_flags = bpf_get_file_flag(attr->open_flags);
3725 spin_lock_bh(&map_idr_lock);
3726 map = idr_find(&map_idr, id);
3728 map = __bpf_map_inc_not_zero(map, true);
3730 map = ERR_PTR(-ENOENT);
3731 spin_unlock_bh(&map_idr_lock);
3734 return PTR_ERR(map);
3736 fd = bpf_map_new_fd(map, f_flags);
3738 bpf_map_put_with_uref(map);
3743 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3744 unsigned long addr, u32 *off,
3747 const struct bpf_map *map;
3750 mutex_lock(&prog->aux->used_maps_mutex);
3751 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3752 map = prog->aux->used_maps[i];
3753 if (map == (void *)addr) {
3754 *type = BPF_PSEUDO_MAP_FD;
3757 if (!map->ops->map_direct_value_meta)
3759 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3760 *type = BPF_PSEUDO_MAP_VALUE;
3767 mutex_unlock(&prog->aux->used_maps_mutex);
3771 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3772 const struct cred *f_cred)
3774 const struct bpf_map *map;
3775 struct bpf_insn *insns;
3781 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3786 for (i = 0; i < prog->len; i++) {
3787 code = insns[i].code;
3789 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3790 insns[i].code = BPF_JMP | BPF_CALL;
3791 insns[i].imm = BPF_FUNC_tail_call;
3794 if (code == (BPF_JMP | BPF_CALL) ||
3795 code == (BPF_JMP | BPF_CALL_ARGS)) {
3796 if (code == (BPF_JMP | BPF_CALL_ARGS))
3797 insns[i].code = BPF_JMP | BPF_CALL;
3798 if (!bpf_dump_raw_ok(f_cred))
3802 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3803 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3807 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3810 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3811 map = bpf_map_from_imm(prog, imm, &off, &type);
3813 insns[i].src_reg = type;
3814 insns[i].imm = map->id;
3815 insns[i + 1].imm = off;
3823 static int set_info_rec_size(struct bpf_prog_info *info)
3826 * Ensure info.*_rec_size is the same as kernel expected size
3830 * Only allow zero *_rec_size if both _rec_size and _cnt are
3831 * zero. In this case, the kernel will set the expected
3832 * _rec_size back to the info.
3835 if ((info->nr_func_info || info->func_info_rec_size) &&
3836 info->func_info_rec_size != sizeof(struct bpf_func_info))
3839 if ((info->nr_line_info || info->line_info_rec_size) &&
3840 info->line_info_rec_size != sizeof(struct bpf_line_info))
3843 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3844 info->jited_line_info_rec_size != sizeof(__u64))
3847 info->func_info_rec_size = sizeof(struct bpf_func_info);
3848 info->line_info_rec_size = sizeof(struct bpf_line_info);
3849 info->jited_line_info_rec_size = sizeof(__u64);
3854 static int bpf_prog_get_info_by_fd(struct file *file,
3855 struct bpf_prog *prog,
3856 const union bpf_attr *attr,
3857 union bpf_attr __user *uattr)
3859 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3860 struct bpf_prog_info info;
3861 u32 info_len = attr->info.info_len;
3862 struct bpf_prog_kstats stats;
3863 char __user *uinsns;
3867 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3870 info_len = min_t(u32, sizeof(info), info_len);
3872 memset(&info, 0, sizeof(info));
3873 if (copy_from_user(&info, uinfo, info_len))
3876 info.type = prog->type;
3877 info.id = prog->aux->id;
3878 info.load_time = prog->aux->load_time;
3879 info.created_by_uid = from_kuid_munged(current_user_ns(),
3880 prog->aux->user->uid);
3881 info.gpl_compatible = prog->gpl_compatible;
3883 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3884 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3886 mutex_lock(&prog->aux->used_maps_mutex);
3887 ulen = info.nr_map_ids;
3888 info.nr_map_ids = prog->aux->used_map_cnt;
3889 ulen = min_t(u32, info.nr_map_ids, ulen);
3891 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3894 for (i = 0; i < ulen; i++)
3895 if (put_user(prog->aux->used_maps[i]->id,
3896 &user_map_ids[i])) {
3897 mutex_unlock(&prog->aux->used_maps_mutex);
3901 mutex_unlock(&prog->aux->used_maps_mutex);
3903 err = set_info_rec_size(&info);
3907 bpf_prog_get_stats(prog, &stats);
3908 info.run_time_ns = stats.nsecs;
3909 info.run_cnt = stats.cnt;
3910 info.recursion_misses = stats.misses;
3912 info.verified_insns = prog->aux->verified_insns;
3914 if (!bpf_capable()) {
3915 info.jited_prog_len = 0;
3916 info.xlated_prog_len = 0;
3917 info.nr_jited_ksyms = 0;
3918 info.nr_jited_func_lens = 0;
3919 info.nr_func_info = 0;
3920 info.nr_line_info = 0;
3921 info.nr_jited_line_info = 0;
3925 ulen = info.xlated_prog_len;
3926 info.xlated_prog_len = bpf_prog_insn_size(prog);
3927 if (info.xlated_prog_len && ulen) {
3928 struct bpf_insn *insns_sanitized;
3931 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3932 info.xlated_prog_insns = 0;
3935 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3936 if (!insns_sanitized)
3938 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3939 ulen = min_t(u32, info.xlated_prog_len, ulen);
3940 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3941 kfree(insns_sanitized);
3946 if (bpf_prog_is_dev_bound(prog->aux)) {
3947 err = bpf_prog_offload_info_fill(&info, prog);
3953 /* NOTE: the following code is supposed to be skipped for offload.
3954 * bpf_prog_offload_info_fill() is the place to fill similar fields
3957 ulen = info.jited_prog_len;
3958 if (prog->aux->func_cnt) {
3961 info.jited_prog_len = 0;
3962 for (i = 0; i < prog->aux->func_cnt; i++)
3963 info.jited_prog_len += prog->aux->func[i]->jited_len;
3965 info.jited_prog_len = prog->jited_len;
3968 if (info.jited_prog_len && ulen) {
3969 if (bpf_dump_raw_ok(file->f_cred)) {
3970 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3971 ulen = min_t(u32, info.jited_prog_len, ulen);
3973 /* for multi-function programs, copy the JITed
3974 * instructions for all the functions
3976 if (prog->aux->func_cnt) {
3981 for (i = 0; i < prog->aux->func_cnt; i++) {
3982 len = prog->aux->func[i]->jited_len;
3983 len = min_t(u32, len, free);
3984 img = (u8 *) prog->aux->func[i]->bpf_func;
3985 if (copy_to_user(uinsns, img, len))
3993 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3997 info.jited_prog_insns = 0;
4001 ulen = info.nr_jited_ksyms;
4002 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
4004 if (bpf_dump_raw_ok(file->f_cred)) {
4005 unsigned long ksym_addr;
4006 u64 __user *user_ksyms;
4009 /* copy the address of the kernel symbol
4010 * corresponding to each function
4012 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
4013 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
4014 if (prog->aux->func_cnt) {
4015 for (i = 0; i < ulen; i++) {
4016 ksym_addr = (unsigned long)
4017 prog->aux->func[i]->bpf_func;
4018 if (put_user((u64) ksym_addr,
4023 ksym_addr = (unsigned long) prog->bpf_func;
4024 if (put_user((u64) ksym_addr, &user_ksyms[0]))
4028 info.jited_ksyms = 0;
4032 ulen = info.nr_jited_func_lens;
4033 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
4035 if (bpf_dump_raw_ok(file->f_cred)) {
4036 u32 __user *user_lens;
4039 /* copy the JITed image lengths for each function */
4040 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
4041 user_lens = u64_to_user_ptr(info.jited_func_lens);
4042 if (prog->aux->func_cnt) {
4043 for (i = 0; i < ulen; i++) {
4045 prog->aux->func[i]->jited_len;
4046 if (put_user(func_len, &user_lens[i]))
4050 func_len = prog->jited_len;
4051 if (put_user(func_len, &user_lens[0]))
4055 info.jited_func_lens = 0;
4060 info.btf_id = btf_obj_id(prog->aux->btf);
4062 ulen = info.nr_func_info;
4063 info.nr_func_info = prog->aux->func_info_cnt;
4064 if (info.nr_func_info && ulen) {
4065 char __user *user_finfo;
4067 user_finfo = u64_to_user_ptr(info.func_info);
4068 ulen = min_t(u32, info.nr_func_info, ulen);
4069 if (copy_to_user(user_finfo, prog->aux->func_info,
4070 info.func_info_rec_size * ulen))
4074 ulen = info.nr_line_info;
4075 info.nr_line_info = prog->aux->nr_linfo;
4076 if (info.nr_line_info && ulen) {
4077 __u8 __user *user_linfo;
4079 user_linfo = u64_to_user_ptr(info.line_info);
4080 ulen = min_t(u32, info.nr_line_info, ulen);
4081 if (copy_to_user(user_linfo, prog->aux->linfo,
4082 info.line_info_rec_size * ulen))
4086 ulen = info.nr_jited_line_info;
4087 if (prog->aux->jited_linfo)
4088 info.nr_jited_line_info = prog->aux->nr_linfo;
4090 info.nr_jited_line_info = 0;
4091 if (info.nr_jited_line_info && ulen) {
4092 if (bpf_dump_raw_ok(file->f_cred)) {
4093 __u64 __user *user_linfo;
4096 user_linfo = u64_to_user_ptr(info.jited_line_info);
4097 ulen = min_t(u32, info.nr_jited_line_info, ulen);
4098 for (i = 0; i < ulen; i++) {
4099 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
4104 info.jited_line_info = 0;
4108 ulen = info.nr_prog_tags;
4109 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
4111 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
4114 user_prog_tags = u64_to_user_ptr(info.prog_tags);
4115 ulen = min_t(u32, info.nr_prog_tags, ulen);
4116 if (prog->aux->func_cnt) {
4117 for (i = 0; i < ulen; i++) {
4118 if (copy_to_user(user_prog_tags[i],
4119 prog->aux->func[i]->tag,
4124 if (copy_to_user(user_prog_tags[0],
4125 prog->tag, BPF_TAG_SIZE))
4131 if (copy_to_user(uinfo, &info, info_len) ||
4132 put_user(info_len, &uattr->info.info_len))
4138 static int bpf_map_get_info_by_fd(struct file *file,
4139 struct bpf_map *map,
4140 const union bpf_attr *attr,
4141 union bpf_attr __user *uattr)
4143 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4144 struct bpf_map_info info;
4145 u32 info_len = attr->info.info_len;
4148 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
4151 info_len = min_t(u32, sizeof(info), info_len);
4153 memset(&info, 0, sizeof(info));
4154 info.type = map->map_type;
4156 info.key_size = map->key_size;
4157 info.value_size = map->value_size;
4158 info.max_entries = map->max_entries;
4159 info.map_flags = map->map_flags;
4160 info.map_extra = map->map_extra;
4161 memcpy(info.name, map->name, sizeof(map->name));
4164 info.btf_id = btf_obj_id(map->btf);
4165 info.btf_key_type_id = map->btf_key_type_id;
4166 info.btf_value_type_id = map->btf_value_type_id;
4168 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
4170 if (bpf_map_is_dev_bound(map)) {
4171 err = bpf_map_offload_info_fill(&info, map);
4176 if (copy_to_user(uinfo, &info, info_len) ||
4177 put_user(info_len, &uattr->info.info_len))
4183 static int bpf_btf_get_info_by_fd(struct file *file,
4185 const union bpf_attr *attr,
4186 union bpf_attr __user *uattr)
4188 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4189 u32 info_len = attr->info.info_len;
4192 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
4196 return btf_get_info_by_fd(btf, attr, uattr);
4199 static int bpf_link_get_info_by_fd(struct file *file,
4200 struct bpf_link *link,
4201 const union bpf_attr *attr,
4202 union bpf_attr __user *uattr)
4204 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4205 struct bpf_link_info info;
4206 u32 info_len = attr->info.info_len;
4209 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
4212 info_len = min_t(u32, sizeof(info), info_len);
4214 memset(&info, 0, sizeof(info));
4215 if (copy_from_user(&info, uinfo, info_len))
4218 info.type = link->type;
4220 info.prog_id = link->prog->aux->id;
4222 if (link->ops->fill_link_info) {
4223 err = link->ops->fill_link_info(link, &info);
4228 if (copy_to_user(uinfo, &info, info_len) ||
4229 put_user(info_len, &uattr->info.info_len))
4236 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
4238 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
4239 union bpf_attr __user *uattr)
4241 int ufd = attr->info.bpf_fd;
4245 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
4252 if (f.file->f_op == &bpf_prog_fops)
4253 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
4255 else if (f.file->f_op == &bpf_map_fops)
4256 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
4258 else if (f.file->f_op == &btf_fops)
4259 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
4260 else if (f.file->f_op == &bpf_link_fops)
4261 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
4270 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
4272 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
4274 if (CHECK_ATTR(BPF_BTF_LOAD))
4280 return btf_new_fd(attr, uattr);
4283 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
4285 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
4287 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
4290 if (!capable(CAP_SYS_ADMIN))
4293 return btf_get_fd_by_id(attr->btf_id);
4296 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
4297 union bpf_attr __user *uattr,
4298 u32 prog_id, u32 fd_type,
4299 const char *buf, u64 probe_offset,
4302 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
4303 u32 len = buf ? strlen(buf) : 0, input_len;
4306 if (put_user(len, &uattr->task_fd_query.buf_len))
4308 input_len = attr->task_fd_query.buf_len;
4309 if (input_len && ubuf) {
4311 /* nothing to copy, just make ubuf NULL terminated */
4314 if (put_user(zero, ubuf))
4316 } else if (input_len >= len + 1) {
4317 /* ubuf can hold the string with NULL terminator */
4318 if (copy_to_user(ubuf, buf, len + 1))
4321 /* ubuf cannot hold the string with NULL terminator,
4322 * do a partial copy with NULL terminator.
4327 if (copy_to_user(ubuf, buf, input_len - 1))
4329 if (put_user(zero, ubuf + input_len - 1))
4334 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
4335 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
4336 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
4337 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
4343 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
4345 static int bpf_task_fd_query(const union bpf_attr *attr,
4346 union bpf_attr __user *uattr)
4348 pid_t pid = attr->task_fd_query.pid;
4349 u32 fd = attr->task_fd_query.fd;
4350 const struct perf_event *event;
4351 struct task_struct *task;
4355 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
4358 if (!capable(CAP_SYS_ADMIN))
4361 if (attr->task_fd_query.flags != 0)
4364 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
4369 file = fget_task(task, fd);
4370 put_task_struct(task);
4374 if (file->f_op == &bpf_link_fops) {
4375 struct bpf_link *link = file->private_data;
4377 if (link->ops == &bpf_raw_tp_link_lops) {
4378 struct bpf_raw_tp_link *raw_tp =
4379 container_of(link, struct bpf_raw_tp_link, link);
4380 struct bpf_raw_event_map *btp = raw_tp->btp;
4382 err = bpf_task_fd_query_copy(attr, uattr,
4383 raw_tp->link.prog->aux->id,
4384 BPF_FD_TYPE_RAW_TRACEPOINT,
4385 btp->tp->name, 0, 0);
4391 event = perf_get_event(file);
4392 if (!IS_ERR(event)) {
4393 u64 probe_offset, probe_addr;
4394 u32 prog_id, fd_type;
4397 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4398 &buf, &probe_offset,
4401 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4415 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
4417 #define BPF_DO_BATCH(fn) \
4423 err = fn(map, attr, uattr); \
4426 static int bpf_map_do_batch(const union bpf_attr *attr,
4427 union bpf_attr __user *uattr,
4430 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH ||
4431 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
4432 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
4433 struct bpf_map *map;
4437 if (CHECK_ATTR(BPF_MAP_BATCH))
4440 ufd = attr->batch.map_fd;
4442 map = __bpf_map_get(f);
4444 return PTR_ERR(map);
4446 bpf_map_write_active_inc(map);
4447 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4451 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4456 if (cmd == BPF_MAP_LOOKUP_BATCH)
4457 BPF_DO_BATCH(map->ops->map_lookup_batch);
4458 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4459 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4460 else if (cmd == BPF_MAP_UPDATE_BATCH)
4461 BPF_DO_BATCH(map->ops->map_update_batch);
4463 BPF_DO_BATCH(map->ops->map_delete_batch);
4466 bpf_map_write_active_dec(map);
4471 #define BPF_LINK_CREATE_LAST_FIELD link_create.kprobe_multi.cookies
4472 static int link_create(union bpf_attr *attr, bpfptr_t uattr)
4474 enum bpf_prog_type ptype;
4475 struct bpf_prog *prog;
4478 if (CHECK_ATTR(BPF_LINK_CREATE))
4481 prog = bpf_prog_get(attr->link_create.prog_fd);
4483 return PTR_ERR(prog);
4485 ret = bpf_prog_attach_check_attach_type(prog,
4486 attr->link_create.attach_type);
4490 switch (prog->type) {
4491 case BPF_PROG_TYPE_EXT:
4493 case BPF_PROG_TYPE_PERF_EVENT:
4494 case BPF_PROG_TYPE_TRACEPOINT:
4495 if (attr->link_create.attach_type != BPF_PERF_EVENT) {
4500 case BPF_PROG_TYPE_KPROBE:
4501 if (attr->link_create.attach_type != BPF_PERF_EVENT &&
4502 attr->link_create.attach_type != BPF_TRACE_KPROBE_MULTI) {
4508 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4509 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4516 switch (prog->type) {
4517 case BPF_PROG_TYPE_CGROUP_SKB:
4518 case BPF_PROG_TYPE_CGROUP_SOCK:
4519 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4520 case BPF_PROG_TYPE_SOCK_OPS:
4521 case BPF_PROG_TYPE_CGROUP_DEVICE:
4522 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4523 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4524 ret = cgroup_bpf_link_attach(attr, prog);
4526 case BPF_PROG_TYPE_EXT:
4527 ret = bpf_tracing_prog_attach(prog,
4528 attr->link_create.target_fd,
4529 attr->link_create.target_btf_id,
4530 attr->link_create.tracing.cookie);
4532 case BPF_PROG_TYPE_LSM:
4533 case BPF_PROG_TYPE_TRACING:
4534 if (attr->link_create.attach_type != prog->expected_attach_type) {
4538 if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
4539 ret = bpf_raw_tp_link_attach(prog, NULL);
4540 else if (prog->expected_attach_type == BPF_TRACE_ITER)
4541 ret = bpf_iter_link_attach(attr, uattr, prog);
4543 ret = bpf_tracing_prog_attach(prog,
4544 attr->link_create.target_fd,
4545 attr->link_create.target_btf_id,
4546 attr->link_create.tracing.cookie);
4548 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4549 case BPF_PROG_TYPE_SK_LOOKUP:
4550 ret = netns_bpf_link_create(attr, prog);
4553 case BPF_PROG_TYPE_XDP:
4554 ret = bpf_xdp_link_attach(attr, prog);
4557 case BPF_PROG_TYPE_PERF_EVENT:
4558 case BPF_PROG_TYPE_TRACEPOINT:
4559 ret = bpf_perf_link_attach(attr, prog);
4561 case BPF_PROG_TYPE_KPROBE:
4562 if (attr->link_create.attach_type == BPF_PERF_EVENT)
4563 ret = bpf_perf_link_attach(attr, prog);
4565 ret = bpf_kprobe_multi_link_attach(attr, prog);
4577 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4579 static int link_update(union bpf_attr *attr)
4581 struct bpf_prog *old_prog = NULL, *new_prog;
4582 struct bpf_link *link;
4586 if (CHECK_ATTR(BPF_LINK_UPDATE))
4589 flags = attr->link_update.flags;
4590 if (flags & ~BPF_F_REPLACE)
4593 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4595 return PTR_ERR(link);
4597 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4598 if (IS_ERR(new_prog)) {
4599 ret = PTR_ERR(new_prog);
4603 if (flags & BPF_F_REPLACE) {
4604 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4605 if (IS_ERR(old_prog)) {
4606 ret = PTR_ERR(old_prog);
4610 } else if (attr->link_update.old_prog_fd) {
4615 if (link->ops->update_prog)
4616 ret = link->ops->update_prog(link, new_prog, old_prog);
4622 bpf_prog_put(old_prog);
4624 bpf_prog_put(new_prog);
4630 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4632 static int link_detach(union bpf_attr *attr)
4634 struct bpf_link *link;
4637 if (CHECK_ATTR(BPF_LINK_DETACH))
4640 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4642 return PTR_ERR(link);
4644 if (link->ops->detach)
4645 ret = link->ops->detach(link);
4653 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4655 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4658 struct bpf_link *bpf_link_by_id(u32 id)
4660 struct bpf_link *link;
4663 return ERR_PTR(-ENOENT);
4665 spin_lock_bh(&link_idr_lock);
4666 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4667 link = idr_find(&link_idr, id);
4670 link = bpf_link_inc_not_zero(link);
4672 link = ERR_PTR(-EAGAIN);
4674 link = ERR_PTR(-ENOENT);
4676 spin_unlock_bh(&link_idr_lock);
4680 struct bpf_link *bpf_link_get_curr_or_next(u32 *id)
4682 struct bpf_link *link;
4684 spin_lock_bh(&link_idr_lock);
4686 link = idr_get_next(&link_idr, id);
4688 link = bpf_link_inc_not_zero(link);
4694 spin_unlock_bh(&link_idr_lock);
4699 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4701 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4703 struct bpf_link *link;
4704 u32 id = attr->link_id;
4707 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4710 if (!capable(CAP_SYS_ADMIN))
4713 link = bpf_link_by_id(id);
4715 return PTR_ERR(link);
4717 fd = bpf_link_new_fd(link);
4724 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4726 static int bpf_stats_release(struct inode *inode, struct file *file)
4728 mutex_lock(&bpf_stats_enabled_mutex);
4729 static_key_slow_dec(&bpf_stats_enabled_key.key);
4730 mutex_unlock(&bpf_stats_enabled_mutex);
4734 static const struct file_operations bpf_stats_fops = {
4735 .release = bpf_stats_release,
4738 static int bpf_enable_runtime_stats(void)
4742 mutex_lock(&bpf_stats_enabled_mutex);
4744 /* Set a very high limit to avoid overflow */
4745 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4746 mutex_unlock(&bpf_stats_enabled_mutex);
4750 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4752 static_key_slow_inc(&bpf_stats_enabled_key.key);
4754 mutex_unlock(&bpf_stats_enabled_mutex);
4758 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4760 static int bpf_enable_stats(union bpf_attr *attr)
4763 if (CHECK_ATTR(BPF_ENABLE_STATS))
4766 if (!capable(CAP_SYS_ADMIN))
4769 switch (attr->enable_stats.type) {
4770 case BPF_STATS_RUN_TIME:
4771 return bpf_enable_runtime_stats();
4778 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4780 static int bpf_iter_create(union bpf_attr *attr)
4782 struct bpf_link *link;
4785 if (CHECK_ATTR(BPF_ITER_CREATE))
4788 if (attr->iter_create.flags)
4791 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4793 return PTR_ERR(link);
4795 err = bpf_iter_new_fd(link);
4801 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4803 static int bpf_prog_bind_map(union bpf_attr *attr)
4805 struct bpf_prog *prog;
4806 struct bpf_map *map;
4807 struct bpf_map **used_maps_old, **used_maps_new;
4810 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4813 if (attr->prog_bind_map.flags)
4816 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4818 return PTR_ERR(prog);
4820 map = bpf_map_get(attr->prog_bind_map.map_fd);
4826 mutex_lock(&prog->aux->used_maps_mutex);
4828 used_maps_old = prog->aux->used_maps;
4830 for (i = 0; i < prog->aux->used_map_cnt; i++)
4831 if (used_maps_old[i] == map) {
4836 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4837 sizeof(used_maps_new[0]),
4839 if (!used_maps_new) {
4844 memcpy(used_maps_new, used_maps_old,
4845 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4846 used_maps_new[prog->aux->used_map_cnt] = map;
4848 prog->aux->used_map_cnt++;
4849 prog->aux->used_maps = used_maps_new;
4851 kfree(used_maps_old);
4854 mutex_unlock(&prog->aux->used_maps_mutex);
4863 static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
4865 union bpf_attr attr;
4869 capable = bpf_capable() || !sysctl_unprivileged_bpf_disabled;
4871 /* Intent here is for unprivileged_bpf_disabled to block key object
4872 * creation commands for unprivileged users; other actions depend
4873 * of fd availability and access to bpffs, so are dependent on
4874 * object creation success. Capabilities are later verified for
4875 * operations such as load and map create, so even with unprivileged
4876 * BPF disabled, capability checks are still carried out for these
4877 * and other operations.
4880 (cmd == BPF_MAP_CREATE || cmd == BPF_PROG_LOAD))
4883 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4886 size = min_t(u32, size, sizeof(attr));
4888 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4889 memset(&attr, 0, sizeof(attr));
4890 if (copy_from_bpfptr(&attr, uattr, size) != 0)
4893 err = security_bpf(cmd, &attr, size);
4898 case BPF_MAP_CREATE:
4899 err = map_create(&attr);
4901 case BPF_MAP_LOOKUP_ELEM:
4902 err = map_lookup_elem(&attr);
4904 case BPF_MAP_UPDATE_ELEM:
4905 err = map_update_elem(&attr, uattr);
4907 case BPF_MAP_DELETE_ELEM:
4908 err = map_delete_elem(&attr);
4910 case BPF_MAP_GET_NEXT_KEY:
4911 err = map_get_next_key(&attr);
4913 case BPF_MAP_FREEZE:
4914 err = map_freeze(&attr);
4917 err = bpf_prog_load(&attr, uattr);
4920 err = bpf_obj_pin(&attr);
4923 err = bpf_obj_get(&attr);
4925 case BPF_PROG_ATTACH:
4926 err = bpf_prog_attach(&attr);
4928 case BPF_PROG_DETACH:
4929 err = bpf_prog_detach(&attr);
4931 case BPF_PROG_QUERY:
4932 err = bpf_prog_query(&attr, uattr.user);
4934 case BPF_PROG_TEST_RUN:
4935 err = bpf_prog_test_run(&attr, uattr.user);
4937 case BPF_PROG_GET_NEXT_ID:
4938 err = bpf_obj_get_next_id(&attr, uattr.user,
4939 &prog_idr, &prog_idr_lock);
4941 case BPF_MAP_GET_NEXT_ID:
4942 err = bpf_obj_get_next_id(&attr, uattr.user,
4943 &map_idr, &map_idr_lock);
4945 case BPF_BTF_GET_NEXT_ID:
4946 err = bpf_obj_get_next_id(&attr, uattr.user,
4947 &btf_idr, &btf_idr_lock);
4949 case BPF_PROG_GET_FD_BY_ID:
4950 err = bpf_prog_get_fd_by_id(&attr);
4952 case BPF_MAP_GET_FD_BY_ID:
4953 err = bpf_map_get_fd_by_id(&attr);
4955 case BPF_OBJ_GET_INFO_BY_FD:
4956 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
4958 case BPF_RAW_TRACEPOINT_OPEN:
4959 err = bpf_raw_tracepoint_open(&attr);
4962 err = bpf_btf_load(&attr, uattr);
4964 case BPF_BTF_GET_FD_BY_ID:
4965 err = bpf_btf_get_fd_by_id(&attr);
4967 case BPF_TASK_FD_QUERY:
4968 err = bpf_task_fd_query(&attr, uattr.user);
4970 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4971 err = map_lookup_and_delete_elem(&attr);
4973 case BPF_MAP_LOOKUP_BATCH:
4974 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
4976 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4977 err = bpf_map_do_batch(&attr, uattr.user,
4978 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4980 case BPF_MAP_UPDATE_BATCH:
4981 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
4983 case BPF_MAP_DELETE_BATCH:
4984 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
4986 case BPF_LINK_CREATE:
4987 err = link_create(&attr, uattr);
4989 case BPF_LINK_UPDATE:
4990 err = link_update(&attr);
4992 case BPF_LINK_GET_FD_BY_ID:
4993 err = bpf_link_get_fd_by_id(&attr);
4995 case BPF_LINK_GET_NEXT_ID:
4996 err = bpf_obj_get_next_id(&attr, uattr.user,
4997 &link_idr, &link_idr_lock);
4999 case BPF_ENABLE_STATS:
5000 err = bpf_enable_stats(&attr);
5002 case BPF_ITER_CREATE:
5003 err = bpf_iter_create(&attr);
5005 case BPF_LINK_DETACH:
5006 err = link_detach(&attr);
5008 case BPF_PROG_BIND_MAP:
5009 err = bpf_prog_bind_map(&attr);
5019 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
5021 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
5024 static bool syscall_prog_is_valid_access(int off, int size,
5025 enum bpf_access_type type,
5026 const struct bpf_prog *prog,
5027 struct bpf_insn_access_aux *info)
5029 if (off < 0 || off >= U16_MAX)
5031 if (off % size != 0)
5036 BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
5039 case BPF_MAP_CREATE:
5040 case BPF_MAP_UPDATE_ELEM:
5041 case BPF_MAP_FREEZE:
5044 case BPF_LINK_CREATE:
5045 case BPF_RAW_TRACEPOINT_OPEN:
5050 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
5053 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5055 struct bpf_prog * __maybe_unused prog;
5056 struct bpf_tramp_run_ctx __maybe_unused run_ctx;
5059 #ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */
5060 case BPF_PROG_TEST_RUN:
5061 if (attr->test.data_in || attr->test.data_out ||
5062 attr->test.ctx_out || attr->test.duration ||
5063 attr->test.repeat || attr->test.flags)
5066 prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL);
5068 return PTR_ERR(prog);
5070 if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
5071 attr->test.ctx_size_in > U16_MAX) {
5076 run_ctx.bpf_cookie = 0;
5077 run_ctx.saved_run_ctx = NULL;
5078 if (!__bpf_prog_enter_sleepable(prog, &run_ctx)) {
5079 /* recursion detected */
5083 attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
5084 __bpf_prog_exit_sleepable(prog, 0 /* bpf_prog_run does runtime stats */, &run_ctx);
5089 return ____bpf_sys_bpf(cmd, attr, size);
5092 EXPORT_SYMBOL(kern_sys_bpf);
5094 static const struct bpf_func_proto bpf_sys_bpf_proto = {
5095 .func = bpf_sys_bpf,
5097 .ret_type = RET_INTEGER,
5098 .arg1_type = ARG_ANYTHING,
5099 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5100 .arg3_type = ARG_CONST_SIZE,
5103 const struct bpf_func_proto * __weak
5104 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5106 return bpf_base_func_proto(func_id);
5109 BPF_CALL_1(bpf_sys_close, u32, fd)
5111 /* When bpf program calls this helper there should not be
5112 * an fdget() without matching completed fdput().
5113 * This helper is allowed in the following callchain only:
5114 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
5116 return close_fd(fd);
5119 static const struct bpf_func_proto bpf_sys_close_proto = {
5120 .func = bpf_sys_close,
5122 .ret_type = RET_INTEGER,
5123 .arg1_type = ARG_ANYTHING,
5126 BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res)
5131 if (name_sz <= 1 || name[name_sz - 1])
5134 if (!bpf_dump_raw_ok(current_cred()))
5137 *res = kallsyms_lookup_name(name);
5138 return *res ? 0 : -ENOENT;
5141 const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = {
5142 .func = bpf_kallsyms_lookup_name,
5144 .ret_type = RET_INTEGER,
5145 .arg1_type = ARG_PTR_TO_MEM,
5146 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
5147 .arg3_type = ARG_ANYTHING,
5148 .arg4_type = ARG_PTR_TO_LONG,
5151 static const struct bpf_func_proto *
5152 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5155 case BPF_FUNC_sys_bpf:
5156 return !perfmon_capable() ? NULL : &bpf_sys_bpf_proto;
5157 case BPF_FUNC_btf_find_by_name_kind:
5158 return &bpf_btf_find_by_name_kind_proto;
5159 case BPF_FUNC_sys_close:
5160 return &bpf_sys_close_proto;
5161 case BPF_FUNC_kallsyms_lookup_name:
5162 return &bpf_kallsyms_lookup_name_proto;
5164 return tracing_prog_func_proto(func_id, prog);
5168 const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
5169 .get_func_proto = syscall_prog_func_proto,
5170 .is_valid_access = syscall_prog_is_valid_access,
5173 const struct bpf_prog_ops bpf_syscall_prog_ops = {
5174 .test_run = bpf_prog_test_run_syscall,
5177 #ifdef CONFIG_SYSCTL
5178 static int bpf_stats_handler(struct ctl_table *table, int write,
5179 void *buffer, size_t *lenp, loff_t *ppos)
5181 struct static_key *key = (struct static_key *)table->data;
5182 static int saved_val;
5184 struct ctl_table tmp = {
5186 .maxlen = sizeof(val),
5187 .mode = table->mode,
5188 .extra1 = SYSCTL_ZERO,
5189 .extra2 = SYSCTL_ONE,
5192 if (write && !capable(CAP_SYS_ADMIN))
5195 mutex_lock(&bpf_stats_enabled_mutex);
5197 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
5198 if (write && !ret && val != saved_val) {
5200 static_key_slow_inc(key);
5202 static_key_slow_dec(key);
5205 mutex_unlock(&bpf_stats_enabled_mutex);
5209 void __weak unpriv_ebpf_notify(int new_state)
5213 static int bpf_unpriv_handler(struct ctl_table *table, int write,
5214 void *buffer, size_t *lenp, loff_t *ppos)
5216 int ret, unpriv_enable = *(int *)table->data;
5217 bool locked_state = unpriv_enable == 1;
5218 struct ctl_table tmp = *table;
5220 if (write && !capable(CAP_SYS_ADMIN))
5223 tmp.data = &unpriv_enable;
5224 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
5225 if (write && !ret) {
5226 if (locked_state && unpriv_enable != 1)
5228 *(int *)table->data = unpriv_enable;
5231 unpriv_ebpf_notify(unpriv_enable);
5236 static struct ctl_table bpf_syscall_table[] = {
5238 .procname = "unprivileged_bpf_disabled",
5239 .data = &sysctl_unprivileged_bpf_disabled,
5240 .maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
5242 .proc_handler = bpf_unpriv_handler,
5243 .extra1 = SYSCTL_ZERO,
5244 .extra2 = SYSCTL_TWO,
5247 .procname = "bpf_stats_enabled",
5248 .data = &bpf_stats_enabled_key.key,
5249 .maxlen = sizeof(bpf_stats_enabled_key),
5251 .proc_handler = bpf_stats_handler,
5256 static int __init bpf_syscall_sysctl_init(void)
5258 register_sysctl_init("kernel", bpf_syscall_table);
5261 late_initcall(bpf_syscall_sysctl_init);
5262 #endif /* CONFIG_SYSCTL */