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 /* Currently if a map is created by a process belonging to the root
423 * memory cgroup, get_obj_cgroup_from_current() will return NULL.
424 * So we have to check map->objcg for being NULL each time it's
427 map->objcg = get_obj_cgroup_from_current();
430 static void bpf_map_release_memcg(struct bpf_map *map)
433 obj_cgroup_put(map->objcg);
436 static struct mem_cgroup *bpf_map_get_memcg(const struct bpf_map *map)
439 return get_mem_cgroup_from_objcg(map->objcg);
441 return root_mem_cgroup;
444 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
447 struct mem_cgroup *memcg, *old_memcg;
450 memcg = bpf_map_get_memcg(map);
451 old_memcg = set_active_memcg(memcg);
452 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
453 set_active_memcg(old_memcg);
454 mem_cgroup_put(memcg);
459 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
461 struct mem_cgroup *memcg, *old_memcg;
464 memcg = bpf_map_get_memcg(map);
465 old_memcg = set_active_memcg(memcg);
466 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
467 set_active_memcg(old_memcg);
468 mem_cgroup_put(memcg);
473 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
474 size_t align, gfp_t flags)
476 struct mem_cgroup *memcg, *old_memcg;
479 memcg = bpf_map_get_memcg(map);
480 old_memcg = set_active_memcg(memcg);
481 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
482 set_active_memcg(old_memcg);
483 mem_cgroup_put(memcg);
489 static void bpf_map_save_memcg(struct bpf_map *map)
493 static void bpf_map_release_memcg(struct bpf_map *map)
498 static int bpf_map_kptr_off_cmp(const void *a, const void *b)
500 const struct bpf_map_value_off_desc *off_desc1 = a, *off_desc2 = b;
502 if (off_desc1->offset < off_desc2->offset)
504 else if (off_desc1->offset > off_desc2->offset)
509 struct bpf_map_value_off_desc *bpf_map_kptr_off_contains(struct bpf_map *map, u32 offset)
511 /* Since members are iterated in btf_find_field in increasing order,
512 * offsets appended to kptr_off_tab are in increasing order, so we can
513 * do bsearch to find exact match.
515 struct bpf_map_value_off *tab;
517 if (!map_value_has_kptrs(map))
519 tab = map->kptr_off_tab;
520 return bsearch(&offset, tab->off, tab->nr_off, sizeof(tab->off[0]), bpf_map_kptr_off_cmp);
523 void bpf_map_free_kptr_off_tab(struct bpf_map *map)
525 struct bpf_map_value_off *tab = map->kptr_off_tab;
528 if (!map_value_has_kptrs(map))
530 for (i = 0; i < tab->nr_off; i++) {
531 if (tab->off[i].kptr.module)
532 module_put(tab->off[i].kptr.module);
533 btf_put(tab->off[i].kptr.btf);
536 map->kptr_off_tab = NULL;
539 struct bpf_map_value_off *bpf_map_copy_kptr_off_tab(const struct bpf_map *map)
541 struct bpf_map_value_off *tab = map->kptr_off_tab, *new_tab;
544 if (!map_value_has_kptrs(map))
545 return ERR_PTR(-ENOENT);
546 size = offsetof(struct bpf_map_value_off, off[tab->nr_off]);
547 new_tab = kmemdup(tab, size, GFP_KERNEL | __GFP_NOWARN);
549 return ERR_PTR(-ENOMEM);
550 /* Do a deep copy of the kptr_off_tab */
551 for (i = 0; i < tab->nr_off; i++) {
552 btf_get(tab->off[i].kptr.btf);
553 if (tab->off[i].kptr.module && !try_module_get(tab->off[i].kptr.module)) {
555 if (tab->off[i].kptr.module)
556 module_put(tab->off[i].kptr.module);
557 btf_put(tab->off[i].kptr.btf);
560 return ERR_PTR(-ENXIO);
566 bool bpf_map_equal_kptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b)
568 struct bpf_map_value_off *tab_a = map_a->kptr_off_tab, *tab_b = map_b->kptr_off_tab;
569 bool a_has_kptr = map_value_has_kptrs(map_a), b_has_kptr = map_value_has_kptrs(map_b);
572 if (!a_has_kptr && !b_has_kptr)
574 if (a_has_kptr != b_has_kptr)
576 if (tab_a->nr_off != tab_b->nr_off)
578 size = offsetof(struct bpf_map_value_off, off[tab_a->nr_off]);
579 return !memcmp(tab_a, tab_b, size);
582 /* Caller must ensure map_value_has_kptrs is true. Note that this function can
583 * be called on a map value while the map_value is visible to BPF programs, as
584 * it ensures the correct synchronization, and we already enforce the same using
585 * the bpf_kptr_xchg helper on the BPF program side for referenced kptrs.
587 void bpf_map_free_kptrs(struct bpf_map *map, void *map_value)
589 struct bpf_map_value_off *tab = map->kptr_off_tab;
590 unsigned long *btf_id_ptr;
593 for (i = 0; i < tab->nr_off; i++) {
594 struct bpf_map_value_off_desc *off_desc = &tab->off[i];
595 unsigned long old_ptr;
597 btf_id_ptr = map_value + off_desc->offset;
598 if (off_desc->type == BPF_KPTR_UNREF) {
599 u64 *p = (u64 *)btf_id_ptr;
604 old_ptr = xchg(btf_id_ptr, 0);
605 off_desc->kptr.dtor((void *)old_ptr);
609 /* called from workqueue */
610 static void bpf_map_free_deferred(struct work_struct *work)
612 struct bpf_map *map = container_of(work, struct bpf_map, work);
614 security_bpf_map_free(map);
616 bpf_map_release_memcg(map);
617 /* implementation dependent freeing, map_free callback also does
618 * bpf_map_free_kptr_off_tab, if needed.
620 map->ops->map_free(map);
623 static void bpf_map_put_uref(struct bpf_map *map)
625 if (atomic64_dec_and_test(&map->usercnt)) {
626 if (map->ops->map_release_uref)
627 map->ops->map_release_uref(map);
631 /* decrement map refcnt and schedule it for freeing via workqueue
632 * (unrelying map implementation ops->map_free() might sleep)
634 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
636 if (atomic64_dec_and_test(&map->refcnt)) {
637 /* bpf_map_free_id() must be called first */
638 bpf_map_free_id(map, do_idr_lock);
640 INIT_WORK(&map->work, bpf_map_free_deferred);
641 schedule_work(&map->work);
645 void bpf_map_put(struct bpf_map *map)
647 __bpf_map_put(map, true);
649 EXPORT_SYMBOL_GPL(bpf_map_put);
651 void bpf_map_put_with_uref(struct bpf_map *map)
653 bpf_map_put_uref(map);
657 static int bpf_map_release(struct inode *inode, struct file *filp)
659 struct bpf_map *map = filp->private_data;
661 if (map->ops->map_release)
662 map->ops->map_release(map, filp);
664 bpf_map_put_with_uref(map);
668 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
670 fmode_t mode = f.file->f_mode;
672 /* Our file permissions may have been overridden by global
673 * map permissions facing syscall side.
675 if (READ_ONCE(map->frozen))
676 mode &= ~FMODE_CAN_WRITE;
680 #ifdef CONFIG_PROC_FS
681 /* Provides an approximation of the map's memory footprint.
682 * Used only to provide a backward compatibility and display
683 * a reasonable "memlock" info.
685 static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
689 size = round_up(map->key_size + bpf_map_value_size(map), 8);
691 return round_up(map->max_entries * size, PAGE_SIZE);
694 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
696 struct bpf_map *map = filp->private_data;
697 u32 type = 0, jited = 0;
699 if (map_type_contains_progs(map)) {
700 spin_lock(&map->owner.lock);
701 type = map->owner.type;
702 jited = map->owner.jited;
703 spin_unlock(&map->owner.lock);
712 "map_extra:\t%#llx\n"
721 (unsigned long long)map->map_extra,
722 bpf_map_memory_footprint(map),
724 READ_ONCE(map->frozen));
726 seq_printf(m, "owner_prog_type:\t%u\n", type);
727 seq_printf(m, "owner_jited:\t%u\n", jited);
732 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
735 /* We need this handler such that alloc_file() enables
736 * f_mode with FMODE_CAN_READ.
741 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
742 size_t siz, loff_t *ppos)
744 /* We need this handler such that alloc_file() enables
745 * f_mode with FMODE_CAN_WRITE.
750 /* called for any extra memory-mapped regions (except initial) */
751 static void bpf_map_mmap_open(struct vm_area_struct *vma)
753 struct bpf_map *map = vma->vm_file->private_data;
755 if (vma->vm_flags & VM_MAYWRITE)
756 bpf_map_write_active_inc(map);
759 /* called for all unmapped memory region (including initial) */
760 static void bpf_map_mmap_close(struct vm_area_struct *vma)
762 struct bpf_map *map = vma->vm_file->private_data;
764 if (vma->vm_flags & VM_MAYWRITE)
765 bpf_map_write_active_dec(map);
768 static const struct vm_operations_struct bpf_map_default_vmops = {
769 .open = bpf_map_mmap_open,
770 .close = bpf_map_mmap_close,
773 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
775 struct bpf_map *map = filp->private_data;
778 if (!map->ops->map_mmap || map_value_has_spin_lock(map) ||
779 map_value_has_timer(map) || map_value_has_kptrs(map))
782 if (!(vma->vm_flags & VM_SHARED))
785 mutex_lock(&map->freeze_mutex);
787 if (vma->vm_flags & VM_WRITE) {
792 /* map is meant to be read-only, so do not allow mapping as
793 * writable, because it's possible to leak a writable page
794 * reference and allows user-space to still modify it after
795 * freezing, while verifier will assume contents do not change
797 if (map->map_flags & BPF_F_RDONLY_PROG) {
803 /* set default open/close callbacks */
804 vma->vm_ops = &bpf_map_default_vmops;
805 vma->vm_private_data = map;
806 vma->vm_flags &= ~VM_MAYEXEC;
807 if (!(vma->vm_flags & VM_WRITE))
808 /* disallow re-mapping with PROT_WRITE */
809 vma->vm_flags &= ~VM_MAYWRITE;
811 err = map->ops->map_mmap(map, vma);
815 if (vma->vm_flags & VM_MAYWRITE)
816 bpf_map_write_active_inc(map);
818 mutex_unlock(&map->freeze_mutex);
822 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
824 struct bpf_map *map = filp->private_data;
826 if (map->ops->map_poll)
827 return map->ops->map_poll(map, filp, pts);
832 const struct file_operations bpf_map_fops = {
833 #ifdef CONFIG_PROC_FS
834 .show_fdinfo = bpf_map_show_fdinfo,
836 .release = bpf_map_release,
837 .read = bpf_dummy_read,
838 .write = bpf_dummy_write,
839 .mmap = bpf_map_mmap,
840 .poll = bpf_map_poll,
843 int bpf_map_new_fd(struct bpf_map *map, int flags)
847 ret = security_bpf_map(map, OPEN_FMODE(flags));
851 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
855 int bpf_get_file_flag(int flags)
857 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
859 if (flags & BPF_F_RDONLY)
861 if (flags & BPF_F_WRONLY)
866 /* helper macro to check that unused fields 'union bpf_attr' are zero */
867 #define CHECK_ATTR(CMD) \
868 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
869 sizeof(attr->CMD##_LAST_FIELD), 0, \
871 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
872 sizeof(attr->CMD##_LAST_FIELD)) != NULL
874 /* dst and src must have at least "size" number of bytes.
875 * Return strlen on success and < 0 on error.
877 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
879 const char *end = src + size;
880 const char *orig_src = src;
882 memset(dst, 0, size);
883 /* Copy all isalnum(), '_' and '.' chars. */
884 while (src < end && *src) {
885 if (!isalnum(*src) &&
886 *src != '_' && *src != '.')
891 /* No '\0' found in "size" number of bytes */
895 return src - orig_src;
898 int map_check_no_btf(const struct bpf_map *map,
899 const struct btf *btf,
900 const struct btf_type *key_type,
901 const struct btf_type *value_type)
906 static int map_off_arr_cmp(const void *_a, const void *_b, const void *priv)
908 const u32 a = *(const u32 *)_a;
909 const u32 b = *(const u32 *)_b;
918 static void map_off_arr_swap(void *_a, void *_b, int size, const void *priv)
920 struct bpf_map *map = (struct bpf_map *)priv;
921 u32 *off_base = map->off_arr->field_off;
922 u32 *a = _a, *b = _b;
925 sz_a = map->off_arr->field_sz + (a - off_base);
926 sz_b = map->off_arr->field_sz + (b - off_base);
932 static int bpf_map_alloc_off_arr(struct bpf_map *map)
934 bool has_spin_lock = map_value_has_spin_lock(map);
935 bool has_timer = map_value_has_timer(map);
936 bool has_kptrs = map_value_has_kptrs(map);
937 struct bpf_map_off_arr *off_arr;
940 if (!has_spin_lock && !has_timer && !has_kptrs) {
945 off_arr = kmalloc(sizeof(*map->off_arr), GFP_KERNEL | __GFP_NOWARN);
948 map->off_arr = off_arr;
954 off_arr->field_off[i] = map->spin_lock_off;
955 off_arr->field_sz[i] = sizeof(struct bpf_spin_lock);
961 off_arr->field_off[i] = map->timer_off;
962 off_arr->field_sz[i] = sizeof(struct bpf_timer);
966 struct bpf_map_value_off *tab = map->kptr_off_tab;
967 u32 *off = &off_arr->field_off[off_arr->cnt];
968 u8 *sz = &off_arr->field_sz[off_arr->cnt];
970 for (i = 0; i < tab->nr_off; i++) {
971 *off++ = tab->off[i].offset;
974 off_arr->cnt += tab->nr_off;
977 if (off_arr->cnt == 1)
979 sort_r(off_arr->field_off, off_arr->cnt, sizeof(off_arr->field_off[0]),
980 map_off_arr_cmp, map_off_arr_swap, map);
984 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
985 u32 btf_key_id, u32 btf_value_id)
987 const struct btf_type *key_type, *value_type;
988 u32 key_size, value_size;
991 /* Some maps allow key to be unspecified. */
993 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
994 if (!key_type || key_size != map->key_size)
997 key_type = btf_type_by_id(btf, 0);
998 if (!map->ops->map_check_btf)
1002 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
1003 if (!value_type || value_size != map->value_size)
1006 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
1008 if (map_value_has_spin_lock(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_ARRAY &&
1013 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
1014 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
1015 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
1016 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
1018 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
1021 "verifier bug spin_lock_off %d value_size %d\n",
1022 map->spin_lock_off, map->value_size);
1027 map->timer_off = btf_find_timer(btf, value_type);
1028 if (map_value_has_timer(map)) {
1029 if (map->map_flags & BPF_F_RDONLY_PROG)
1031 if (map->map_type != BPF_MAP_TYPE_HASH &&
1032 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1033 map->map_type != BPF_MAP_TYPE_ARRAY)
1037 map->kptr_off_tab = btf_parse_kptrs(btf, value_type);
1038 if (map_value_has_kptrs(map)) {
1039 if (!bpf_capable()) {
1043 if (map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) {
1047 if (map->map_type != BPF_MAP_TYPE_HASH &&
1048 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1049 map->map_type != BPF_MAP_TYPE_ARRAY) {
1055 if (map->ops->map_check_btf) {
1056 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
1063 bpf_map_free_kptr_off_tab(map);
1067 #define BPF_MAP_CREATE_LAST_FIELD map_extra
1068 /* called via syscall */
1069 static int map_create(union bpf_attr *attr)
1071 int numa_node = bpf_map_attr_numa_node(attr);
1072 struct bpf_map *map;
1076 err = CHECK_ATTR(BPF_MAP_CREATE);
1080 if (attr->btf_vmlinux_value_type_id) {
1081 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
1082 attr->btf_key_type_id || attr->btf_value_type_id)
1084 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
1088 if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER &&
1089 attr->map_extra != 0)
1092 f_flags = bpf_get_file_flag(attr->map_flags);
1096 if (numa_node != NUMA_NO_NODE &&
1097 ((unsigned int)numa_node >= nr_node_ids ||
1098 !node_online(numa_node)))
1101 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
1102 map = find_and_alloc_map(attr);
1104 return PTR_ERR(map);
1106 err = bpf_obj_name_cpy(map->name, attr->map_name,
1107 sizeof(attr->map_name));
1111 atomic64_set(&map->refcnt, 1);
1112 atomic64_set(&map->usercnt, 1);
1113 mutex_init(&map->freeze_mutex);
1114 spin_lock_init(&map->owner.lock);
1116 map->spin_lock_off = -EINVAL;
1117 map->timer_off = -EINVAL;
1118 if (attr->btf_key_type_id || attr->btf_value_type_id ||
1119 /* Even the map's value is a kernel's struct,
1120 * the bpf_prog.o must have BTF to begin with
1121 * to figure out the corresponding kernel's
1122 * counter part. Thus, attr->btf_fd has
1125 attr->btf_vmlinux_value_type_id) {
1128 btf = btf_get_by_fd(attr->btf_fd);
1133 if (btf_is_kernel(btf)) {
1140 if (attr->btf_value_type_id) {
1141 err = map_check_btf(map, btf, attr->btf_key_type_id,
1142 attr->btf_value_type_id);
1147 map->btf_key_type_id = attr->btf_key_type_id;
1148 map->btf_value_type_id = attr->btf_value_type_id;
1149 map->btf_vmlinux_value_type_id =
1150 attr->btf_vmlinux_value_type_id;
1153 err = bpf_map_alloc_off_arr(map);
1157 err = security_bpf_map_alloc(map);
1159 goto free_map_off_arr;
1161 err = bpf_map_alloc_id(map);
1165 bpf_map_save_memcg(map);
1167 err = bpf_map_new_fd(map, f_flags);
1169 /* failed to allocate fd.
1170 * bpf_map_put_with_uref() is needed because the above
1171 * bpf_map_alloc_id() has published the map
1172 * to the userspace and the userspace may
1173 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
1175 bpf_map_put_with_uref(map);
1182 security_bpf_map_free(map);
1184 kfree(map->off_arr);
1187 map->ops->map_free(map);
1191 /* if error is returned, fd is released.
1192 * On success caller should complete fd access with matching fdput()
1194 struct bpf_map *__bpf_map_get(struct fd f)
1197 return ERR_PTR(-EBADF);
1198 if (f.file->f_op != &bpf_map_fops) {
1200 return ERR_PTR(-EINVAL);
1203 return f.file->private_data;
1206 void bpf_map_inc(struct bpf_map *map)
1208 atomic64_inc(&map->refcnt);
1210 EXPORT_SYMBOL_GPL(bpf_map_inc);
1212 void bpf_map_inc_with_uref(struct bpf_map *map)
1214 atomic64_inc(&map->refcnt);
1215 atomic64_inc(&map->usercnt);
1217 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
1219 struct bpf_map *bpf_map_get(u32 ufd)
1221 struct fd f = fdget(ufd);
1222 struct bpf_map *map;
1224 map = __bpf_map_get(f);
1233 EXPORT_SYMBOL(bpf_map_get);
1235 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
1237 struct fd f = fdget(ufd);
1238 struct bpf_map *map;
1240 map = __bpf_map_get(f);
1244 bpf_map_inc_with_uref(map);
1250 /* map_idr_lock should have been held */
1251 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
1255 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
1257 return ERR_PTR(-ENOENT);
1259 atomic64_inc(&map->usercnt);
1264 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
1266 spin_lock_bh(&map_idr_lock);
1267 map = __bpf_map_inc_not_zero(map, false);
1268 spin_unlock_bh(&map_idr_lock);
1272 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
1274 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1279 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1282 return vmemdup_user(ukey, key_size);
1285 return ERR_PTR(-EINVAL);
1290 static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1293 return kvmemdup_bpfptr(ukey, key_size);
1295 if (!bpfptr_is_null(ukey))
1296 return ERR_PTR(-EINVAL);
1301 /* last field in 'union bpf_attr' used by this command */
1302 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1304 static int map_lookup_elem(union bpf_attr *attr)
1306 void __user *ukey = u64_to_user_ptr(attr->key);
1307 void __user *uvalue = u64_to_user_ptr(attr->value);
1308 int ufd = attr->map_fd;
1309 struct bpf_map *map;
1315 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1318 if (attr->flags & ~BPF_F_LOCK)
1322 map = __bpf_map_get(f);
1324 return PTR_ERR(map);
1325 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1330 if ((attr->flags & BPF_F_LOCK) &&
1331 !map_value_has_spin_lock(map)) {
1336 key = __bpf_copy_key(ukey, map->key_size);
1342 value_size = bpf_map_value_size(map);
1345 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1349 if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
1350 if (copy_from_user(value, uvalue, value_size))
1353 err = bpf_map_copy_value(map, key, value, attr->flags);
1357 err = bpf_map_copy_value(map, key, value, attr->flags);
1362 if (copy_to_user(uvalue, value, value_size) != 0)
1377 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1379 static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
1381 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1382 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
1383 int ufd = attr->map_fd;
1384 struct bpf_map *map;
1390 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1394 map = __bpf_map_get(f);
1396 return PTR_ERR(map);
1397 bpf_map_write_active_inc(map);
1398 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1403 if ((attr->flags & BPF_F_LOCK) &&
1404 !map_value_has_spin_lock(map)) {
1409 key = ___bpf_copy_key(ukey, map->key_size);
1415 value_size = bpf_map_value_size(map);
1418 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1423 if (copy_from_bpfptr(value, uvalue, value_size) != 0)
1426 err = bpf_map_update_value(map, f, key, value, attr->flags);
1433 bpf_map_write_active_dec(map);
1438 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1440 static int map_delete_elem(union bpf_attr *attr)
1442 void __user *ukey = u64_to_user_ptr(attr->key);
1443 int ufd = attr->map_fd;
1444 struct bpf_map *map;
1449 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1453 map = __bpf_map_get(f);
1455 return PTR_ERR(map);
1456 bpf_map_write_active_inc(map);
1457 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1462 key = __bpf_copy_key(ukey, map->key_size);
1468 if (bpf_map_is_dev_bound(map)) {
1469 err = bpf_map_offload_delete_elem(map, key);
1471 } else if (IS_FD_PROG_ARRAY(map) ||
1472 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1473 /* These maps require sleepable context */
1474 err = map->ops->map_delete_elem(map, key);
1478 bpf_disable_instrumentation();
1480 err = map->ops->map_delete_elem(map, key);
1482 bpf_enable_instrumentation();
1483 maybe_wait_bpf_programs(map);
1487 bpf_map_write_active_dec(map);
1492 /* last field in 'union bpf_attr' used by this command */
1493 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1495 static int map_get_next_key(union bpf_attr *attr)
1497 void __user *ukey = u64_to_user_ptr(attr->key);
1498 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1499 int ufd = attr->map_fd;
1500 struct bpf_map *map;
1501 void *key, *next_key;
1505 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1509 map = __bpf_map_get(f);
1511 return PTR_ERR(map);
1512 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1518 key = __bpf_copy_key(ukey, map->key_size);
1528 next_key = kvmalloc(map->key_size, GFP_USER);
1532 if (bpf_map_is_dev_bound(map)) {
1533 err = bpf_map_offload_get_next_key(map, key, next_key);
1538 err = map->ops->map_get_next_key(map, key, next_key);
1545 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1559 int generic_map_delete_batch(struct bpf_map *map,
1560 const union bpf_attr *attr,
1561 union bpf_attr __user *uattr)
1563 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1568 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1571 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1572 !map_value_has_spin_lock(map)) {
1576 max_count = attr->batch.count;
1580 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1584 for (cp = 0; cp < max_count; cp++) {
1586 if (copy_from_user(key, keys + cp * map->key_size,
1590 if (bpf_map_is_dev_bound(map)) {
1591 err = bpf_map_offload_delete_elem(map, key);
1595 bpf_disable_instrumentation();
1597 err = map->ops->map_delete_elem(map, key);
1599 bpf_enable_instrumentation();
1604 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1609 maybe_wait_bpf_programs(map);
1613 int generic_map_update_batch(struct bpf_map *map,
1614 const union bpf_attr *attr,
1615 union bpf_attr __user *uattr)
1617 void __user *values = u64_to_user_ptr(attr->batch.values);
1618 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1619 u32 value_size, cp, max_count;
1620 int ufd = attr->batch.map_fd;
1625 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1628 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1629 !map_value_has_spin_lock(map)) {
1633 value_size = bpf_map_value_size(map);
1635 max_count = attr->batch.count;
1639 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1643 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1649 f = fdget(ufd); /* bpf_map_do_batch() guarantees ufd is valid */
1650 for (cp = 0; cp < max_count; cp++) {
1652 if (copy_from_user(key, keys + cp * map->key_size,
1654 copy_from_user(value, values + cp * value_size, value_size))
1657 err = bpf_map_update_value(map, f, key, value,
1658 attr->batch.elem_flags);
1665 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1674 #define MAP_LOOKUP_RETRIES 3
1676 int generic_map_lookup_batch(struct bpf_map *map,
1677 const union bpf_attr *attr,
1678 union bpf_attr __user *uattr)
1680 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1681 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1682 void __user *values = u64_to_user_ptr(attr->batch.values);
1683 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1684 void *buf, *buf_prevkey, *prev_key, *key, *value;
1685 int err, retry = MAP_LOOKUP_RETRIES;
1686 u32 value_size, cp, max_count;
1688 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1691 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1692 !map_value_has_spin_lock(map))
1695 value_size = bpf_map_value_size(map);
1697 max_count = attr->batch.count;
1701 if (put_user(0, &uattr->batch.count))
1704 buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1708 buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1710 kvfree(buf_prevkey);
1716 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1719 value = key + map->key_size;
1721 prev_key = buf_prevkey;
1723 for (cp = 0; cp < max_count;) {
1725 err = map->ops->map_get_next_key(map, prev_key, key);
1729 err = bpf_map_copy_value(map, key, value,
1730 attr->batch.elem_flags);
1732 if (err == -ENOENT) {
1744 if (copy_to_user(keys + cp * map->key_size, key,
1749 if (copy_to_user(values + cp * value_size, value, value_size)) {
1755 prev_key = buf_prevkey;
1757 swap(prev_key, key);
1758 retry = MAP_LOOKUP_RETRIES;
1766 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1767 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1771 kvfree(buf_prevkey);
1776 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
1778 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1780 void __user *ukey = u64_to_user_ptr(attr->key);
1781 void __user *uvalue = u64_to_user_ptr(attr->value);
1782 int ufd = attr->map_fd;
1783 struct bpf_map *map;
1789 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1792 if (attr->flags & ~BPF_F_LOCK)
1796 map = __bpf_map_get(f);
1798 return PTR_ERR(map);
1799 bpf_map_write_active_inc(map);
1800 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1801 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1807 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1808 map->map_type == BPF_MAP_TYPE_STACK)) {
1813 if ((attr->flags & BPF_F_LOCK) &&
1814 !map_value_has_spin_lock(map)) {
1819 key = __bpf_copy_key(ukey, map->key_size);
1825 value_size = bpf_map_value_size(map);
1828 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1833 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1834 map->map_type == BPF_MAP_TYPE_STACK) {
1835 err = map->ops->map_pop_elem(map, value);
1836 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1837 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1838 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1839 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1840 if (!bpf_map_is_dev_bound(map)) {
1841 bpf_disable_instrumentation();
1843 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1845 bpf_enable_instrumentation();
1852 if (copy_to_user(uvalue, value, value_size) != 0) {
1864 bpf_map_write_active_dec(map);
1869 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1871 static int map_freeze(const union bpf_attr *attr)
1873 int err = 0, ufd = attr->map_fd;
1874 struct bpf_map *map;
1877 if (CHECK_ATTR(BPF_MAP_FREEZE))
1881 map = __bpf_map_get(f);
1883 return PTR_ERR(map);
1885 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS ||
1886 map_value_has_timer(map) || map_value_has_kptrs(map)) {
1891 mutex_lock(&map->freeze_mutex);
1892 if (bpf_map_write_active(map)) {
1896 if (READ_ONCE(map->frozen)) {
1900 if (!bpf_capable()) {
1905 WRITE_ONCE(map->frozen, true);
1907 mutex_unlock(&map->freeze_mutex);
1912 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1913 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1914 [_id] = & _name ## _prog_ops,
1915 #define BPF_MAP_TYPE(_id, _ops)
1916 #define BPF_LINK_TYPE(_id, _name)
1917 #include <linux/bpf_types.h>
1918 #undef BPF_PROG_TYPE
1920 #undef BPF_LINK_TYPE
1923 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1925 const struct bpf_prog_ops *ops;
1927 if (type >= ARRAY_SIZE(bpf_prog_types))
1929 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1930 ops = bpf_prog_types[type];
1934 if (!bpf_prog_is_dev_bound(prog->aux))
1935 prog->aux->ops = ops;
1937 prog->aux->ops = &bpf_offload_prog_ops;
1948 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1949 [BPF_AUDIT_LOAD] = "LOAD",
1950 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1953 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1955 struct audit_context *ctx = NULL;
1956 struct audit_buffer *ab;
1958 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1960 if (audit_enabled == AUDIT_OFF)
1962 if (op == BPF_AUDIT_LOAD)
1963 ctx = audit_context();
1964 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1967 audit_log_format(ab, "prog-id=%u op=%s",
1968 prog->aux->id, bpf_audit_str[op]);
1972 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1976 idr_preload(GFP_KERNEL);
1977 spin_lock_bh(&prog_idr_lock);
1978 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1981 spin_unlock_bh(&prog_idr_lock);
1984 /* id is in [1, INT_MAX) */
1985 if (WARN_ON_ONCE(!id))
1988 return id > 0 ? 0 : id;
1991 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1993 unsigned long flags;
1995 /* cBPF to eBPF migrations are currently not in the idr store.
1996 * Offloaded programs are removed from the store when their device
1997 * disappears - even if someone grabs an fd to them they are unusable,
1998 * simply waiting for refcnt to drop to be freed.
2004 spin_lock_irqsave(&prog_idr_lock, flags);
2006 __acquire(&prog_idr_lock);
2008 idr_remove(&prog_idr, prog->aux->id);
2012 spin_unlock_irqrestore(&prog_idr_lock, flags);
2014 __release(&prog_idr_lock);
2017 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
2019 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
2021 kvfree(aux->func_info);
2022 kfree(aux->func_info_aux);
2023 free_uid(aux->user);
2024 security_bpf_prog_free(aux);
2025 bpf_prog_free(aux->prog);
2028 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
2030 bpf_prog_kallsyms_del_all(prog);
2031 btf_put(prog->aux->btf);
2032 kvfree(prog->aux->jited_linfo);
2033 kvfree(prog->aux->linfo);
2034 kfree(prog->aux->kfunc_tab);
2035 if (prog->aux->attach_btf)
2036 btf_put(prog->aux->attach_btf);
2039 if (prog->aux->sleepable)
2040 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
2042 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
2044 __bpf_prog_put_rcu(&prog->aux->rcu);
2048 static void bpf_prog_put_deferred(struct work_struct *work)
2050 struct bpf_prog_aux *aux;
2051 struct bpf_prog *prog;
2053 aux = container_of(work, struct bpf_prog_aux, work);
2055 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
2056 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
2057 __bpf_prog_put_noref(prog, true);
2060 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
2062 struct bpf_prog_aux *aux = prog->aux;
2064 if (atomic64_dec_and_test(&aux->refcnt)) {
2065 /* bpf_prog_free_id() must be called first */
2066 bpf_prog_free_id(prog, do_idr_lock);
2068 if (in_irq() || irqs_disabled()) {
2069 INIT_WORK(&aux->work, bpf_prog_put_deferred);
2070 schedule_work(&aux->work);
2072 bpf_prog_put_deferred(&aux->work);
2077 void bpf_prog_put(struct bpf_prog *prog)
2079 __bpf_prog_put(prog, true);
2081 EXPORT_SYMBOL_GPL(bpf_prog_put);
2083 static int bpf_prog_release(struct inode *inode, struct file *filp)
2085 struct bpf_prog *prog = filp->private_data;
2091 struct bpf_prog_kstats {
2097 static void bpf_prog_get_stats(const struct bpf_prog *prog,
2098 struct bpf_prog_kstats *stats)
2100 u64 nsecs = 0, cnt = 0, misses = 0;
2103 for_each_possible_cpu(cpu) {
2104 const struct bpf_prog_stats *st;
2106 u64 tnsecs, tcnt, tmisses;
2108 st = per_cpu_ptr(prog->stats, cpu);
2110 start = u64_stats_fetch_begin_irq(&st->syncp);
2111 tnsecs = u64_stats_read(&st->nsecs);
2112 tcnt = u64_stats_read(&st->cnt);
2113 tmisses = u64_stats_read(&st->misses);
2114 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
2119 stats->nsecs = nsecs;
2121 stats->misses = misses;
2124 #ifdef CONFIG_PROC_FS
2125 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
2127 const struct bpf_prog *prog = filp->private_data;
2128 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2129 struct bpf_prog_kstats stats;
2131 bpf_prog_get_stats(prog, &stats);
2132 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2139 "run_time_ns:\t%llu\n"
2141 "recursion_misses:\t%llu\n"
2142 "verified_insns:\t%u\n",
2146 prog->pages * 1ULL << PAGE_SHIFT,
2151 prog->aux->verified_insns);
2155 const struct file_operations bpf_prog_fops = {
2156 #ifdef CONFIG_PROC_FS
2157 .show_fdinfo = bpf_prog_show_fdinfo,
2159 .release = bpf_prog_release,
2160 .read = bpf_dummy_read,
2161 .write = bpf_dummy_write,
2164 int bpf_prog_new_fd(struct bpf_prog *prog)
2168 ret = security_bpf_prog(prog);
2172 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
2173 O_RDWR | O_CLOEXEC);
2176 static struct bpf_prog *____bpf_prog_get(struct fd f)
2179 return ERR_PTR(-EBADF);
2180 if (f.file->f_op != &bpf_prog_fops) {
2182 return ERR_PTR(-EINVAL);
2185 return f.file->private_data;
2188 void bpf_prog_add(struct bpf_prog *prog, int i)
2190 atomic64_add(i, &prog->aux->refcnt);
2192 EXPORT_SYMBOL_GPL(bpf_prog_add);
2194 void bpf_prog_sub(struct bpf_prog *prog, int i)
2196 /* Only to be used for undoing previous bpf_prog_add() in some
2197 * error path. We still know that another entity in our call
2198 * path holds a reference to the program, thus atomic_sub() can
2199 * be safely used in such cases!
2201 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
2203 EXPORT_SYMBOL_GPL(bpf_prog_sub);
2205 void bpf_prog_inc(struct bpf_prog *prog)
2207 atomic64_inc(&prog->aux->refcnt);
2209 EXPORT_SYMBOL_GPL(bpf_prog_inc);
2211 /* prog_idr_lock should have been held */
2212 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
2216 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
2219 return ERR_PTR(-ENOENT);
2223 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
2225 bool bpf_prog_get_ok(struct bpf_prog *prog,
2226 enum bpf_prog_type *attach_type, bool attach_drv)
2228 /* not an attachment, just a refcount inc, always allow */
2232 if (prog->type != *attach_type)
2234 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
2240 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
2243 struct fd f = fdget(ufd);
2244 struct bpf_prog *prog;
2246 prog = ____bpf_prog_get(f);
2249 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
2250 prog = ERR_PTR(-EINVAL);
2260 struct bpf_prog *bpf_prog_get(u32 ufd)
2262 return __bpf_prog_get(ufd, NULL, false);
2265 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
2268 return __bpf_prog_get(ufd, &type, attach_drv);
2270 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
2272 /* Initially all BPF programs could be loaded w/o specifying
2273 * expected_attach_type. Later for some of them specifying expected_attach_type
2274 * at load time became required so that program could be validated properly.
2275 * Programs of types that are allowed to be loaded both w/ and w/o (for
2276 * backward compatibility) expected_attach_type, should have the default attach
2277 * type assigned to expected_attach_type for the latter case, so that it can be
2278 * validated later at attach time.
2280 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
2281 * prog type requires it but has some attach types that have to be backward
2284 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
2286 switch (attr->prog_type) {
2287 case BPF_PROG_TYPE_CGROUP_SOCK:
2288 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
2289 * exist so checking for non-zero is the way to go here.
2291 if (!attr->expected_attach_type)
2292 attr->expected_attach_type =
2293 BPF_CGROUP_INET_SOCK_CREATE;
2295 case BPF_PROG_TYPE_SK_REUSEPORT:
2296 if (!attr->expected_attach_type)
2297 attr->expected_attach_type =
2298 BPF_SK_REUSEPORT_SELECT;
2304 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2305 enum bpf_attach_type expected_attach_type,
2306 struct btf *attach_btf, u32 btf_id,
2307 struct bpf_prog *dst_prog)
2310 if (btf_id > BTF_MAX_TYPE)
2313 if (!attach_btf && !dst_prog)
2316 switch (prog_type) {
2317 case BPF_PROG_TYPE_TRACING:
2318 case BPF_PROG_TYPE_LSM:
2319 case BPF_PROG_TYPE_STRUCT_OPS:
2320 case BPF_PROG_TYPE_EXT:
2327 if (attach_btf && (!btf_id || dst_prog))
2330 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
2331 prog_type != BPF_PROG_TYPE_EXT)
2334 switch (prog_type) {
2335 case BPF_PROG_TYPE_CGROUP_SOCK:
2336 switch (expected_attach_type) {
2337 case BPF_CGROUP_INET_SOCK_CREATE:
2338 case BPF_CGROUP_INET_SOCK_RELEASE:
2339 case BPF_CGROUP_INET4_POST_BIND:
2340 case BPF_CGROUP_INET6_POST_BIND:
2345 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2346 switch (expected_attach_type) {
2347 case BPF_CGROUP_INET4_BIND:
2348 case BPF_CGROUP_INET6_BIND:
2349 case BPF_CGROUP_INET4_CONNECT:
2350 case BPF_CGROUP_INET6_CONNECT:
2351 case BPF_CGROUP_INET4_GETPEERNAME:
2352 case BPF_CGROUP_INET6_GETPEERNAME:
2353 case BPF_CGROUP_INET4_GETSOCKNAME:
2354 case BPF_CGROUP_INET6_GETSOCKNAME:
2355 case BPF_CGROUP_UDP4_SENDMSG:
2356 case BPF_CGROUP_UDP6_SENDMSG:
2357 case BPF_CGROUP_UDP4_RECVMSG:
2358 case BPF_CGROUP_UDP6_RECVMSG:
2363 case BPF_PROG_TYPE_CGROUP_SKB:
2364 switch (expected_attach_type) {
2365 case BPF_CGROUP_INET_INGRESS:
2366 case BPF_CGROUP_INET_EGRESS:
2371 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2372 switch (expected_attach_type) {
2373 case BPF_CGROUP_SETSOCKOPT:
2374 case BPF_CGROUP_GETSOCKOPT:
2379 case BPF_PROG_TYPE_SK_LOOKUP:
2380 if (expected_attach_type == BPF_SK_LOOKUP)
2383 case BPF_PROG_TYPE_SK_REUSEPORT:
2384 switch (expected_attach_type) {
2385 case BPF_SK_REUSEPORT_SELECT:
2386 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2391 case BPF_PROG_TYPE_SYSCALL:
2392 case BPF_PROG_TYPE_EXT:
2393 if (expected_attach_type)
2401 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2403 switch (prog_type) {
2404 case BPF_PROG_TYPE_SCHED_CLS:
2405 case BPF_PROG_TYPE_SCHED_ACT:
2406 case BPF_PROG_TYPE_XDP:
2407 case BPF_PROG_TYPE_LWT_IN:
2408 case BPF_PROG_TYPE_LWT_OUT:
2409 case BPF_PROG_TYPE_LWT_XMIT:
2410 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2411 case BPF_PROG_TYPE_SK_SKB:
2412 case BPF_PROG_TYPE_SK_MSG:
2413 case BPF_PROG_TYPE_LIRC_MODE2:
2414 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2415 case BPF_PROG_TYPE_CGROUP_DEVICE:
2416 case BPF_PROG_TYPE_CGROUP_SOCK:
2417 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2418 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2419 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2420 case BPF_PROG_TYPE_SOCK_OPS:
2421 case BPF_PROG_TYPE_EXT: /* extends any prog */
2423 case BPF_PROG_TYPE_CGROUP_SKB:
2425 case BPF_PROG_TYPE_SK_REUSEPORT:
2426 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2432 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2434 switch (prog_type) {
2435 case BPF_PROG_TYPE_KPROBE:
2436 case BPF_PROG_TYPE_TRACEPOINT:
2437 case BPF_PROG_TYPE_PERF_EVENT:
2438 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2439 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2440 case BPF_PROG_TYPE_TRACING:
2441 case BPF_PROG_TYPE_LSM:
2442 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2443 case BPF_PROG_TYPE_EXT: /* extends any prog */
2450 /* last field in 'union bpf_attr' used by this command */
2451 #define BPF_PROG_LOAD_LAST_FIELD core_relo_rec_size
2453 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
2455 enum bpf_prog_type type = attr->prog_type;
2456 struct bpf_prog *prog, *dst_prog = NULL;
2457 struct btf *attach_btf = NULL;
2462 if (CHECK_ATTR(BPF_PROG_LOAD))
2465 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2466 BPF_F_ANY_ALIGNMENT |
2467 BPF_F_TEST_STATE_FREQ |
2469 BPF_F_TEST_RND_HI32 |
2470 BPF_F_XDP_HAS_FRAGS))
2473 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2474 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2478 /* copy eBPF program license from user space */
2479 if (strncpy_from_bpfptr(license,
2480 make_bpfptr(attr->license, uattr.is_kernel),
2481 sizeof(license) - 1) < 0)
2483 license[sizeof(license) - 1] = 0;
2485 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2486 is_gpl = license_is_gpl_compatible(license);
2488 if (attr->insn_cnt == 0 ||
2489 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2491 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2492 type != BPF_PROG_TYPE_CGROUP_SKB &&
2496 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2498 if (is_perfmon_prog_type(type) && !perfmon_capable())
2501 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2502 * or btf, we need to check which one it is
2504 if (attr->attach_prog_fd) {
2505 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2506 if (IS_ERR(dst_prog)) {
2508 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2509 if (IS_ERR(attach_btf))
2511 if (!btf_is_kernel(attach_btf)) {
2512 /* attaching through specifying bpf_prog's BTF
2513 * objects directly might be supported eventually
2515 btf_put(attach_btf);
2519 } else if (attr->attach_btf_id) {
2520 /* fall back to vmlinux BTF, if BTF type ID is specified */
2521 attach_btf = bpf_get_btf_vmlinux();
2522 if (IS_ERR(attach_btf))
2523 return PTR_ERR(attach_btf);
2526 btf_get(attach_btf);
2529 bpf_prog_load_fixup_attach_type(attr);
2530 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2531 attach_btf, attr->attach_btf_id,
2534 bpf_prog_put(dst_prog);
2536 btf_put(attach_btf);
2540 /* plain bpf_prog allocation */
2541 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2544 bpf_prog_put(dst_prog);
2546 btf_put(attach_btf);
2550 prog->expected_attach_type = attr->expected_attach_type;
2551 prog->aux->attach_btf = attach_btf;
2552 prog->aux->attach_btf_id = attr->attach_btf_id;
2553 prog->aux->dst_prog = dst_prog;
2554 prog->aux->offload_requested = !!attr->prog_ifindex;
2555 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2556 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
2558 err = security_bpf_prog_alloc(prog->aux);
2562 prog->aux->user = get_current_user();
2563 prog->len = attr->insn_cnt;
2566 if (copy_from_bpfptr(prog->insns,
2567 make_bpfptr(attr->insns, uattr.is_kernel),
2568 bpf_prog_insn_size(prog)) != 0)
2571 prog->orig_prog = NULL;
2574 atomic64_set(&prog->aux->refcnt, 1);
2575 prog->gpl_compatible = is_gpl ? 1 : 0;
2577 if (bpf_prog_is_dev_bound(prog->aux)) {
2578 err = bpf_prog_offload_init(prog, attr);
2583 /* find program type: socket_filter vs tracing_filter */
2584 err = find_prog_type(type, prog);
2588 prog->aux->load_time = ktime_get_boottime_ns();
2589 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2590 sizeof(attr->prog_name));
2594 /* run eBPF verifier */
2595 err = bpf_check(&prog, attr, uattr);
2597 goto free_used_maps;
2599 prog = bpf_prog_select_runtime(prog, &err);
2601 goto free_used_maps;
2603 err = bpf_prog_alloc_id(prog);
2605 goto free_used_maps;
2607 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2608 * effectively publicly exposed. However, retrieving via
2609 * bpf_prog_get_fd_by_id() will take another reference,
2610 * therefore it cannot be gone underneath us.
2612 * Only for the time /after/ successful bpf_prog_new_fd()
2613 * and before returning to userspace, we might just hold
2614 * one reference and any parallel close on that fd could
2615 * rip everything out. Hence, below notifications must
2616 * happen before bpf_prog_new_fd().
2618 * Also, any failure handling from this point onwards must
2619 * be using bpf_prog_put() given the program is exposed.
2621 bpf_prog_kallsyms_add(prog);
2622 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2623 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2625 err = bpf_prog_new_fd(prog);
2631 /* In case we have subprogs, we need to wait for a grace
2632 * period before we can tear down JIT memory since symbols
2633 * are already exposed under kallsyms.
2635 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2638 free_uid(prog->aux->user);
2639 security_bpf_prog_free(prog->aux);
2641 if (prog->aux->attach_btf)
2642 btf_put(prog->aux->attach_btf);
2643 bpf_prog_free(prog);
2647 #define BPF_OBJ_LAST_FIELD file_flags
2649 static int bpf_obj_pin(const union bpf_attr *attr)
2651 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2654 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2657 static int bpf_obj_get(const union bpf_attr *attr)
2659 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2660 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2663 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2667 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2668 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2670 atomic64_set(&link->refcnt, 1);
2677 static void bpf_link_free_id(int id)
2682 spin_lock_bh(&link_idr_lock);
2683 idr_remove(&link_idr, id);
2684 spin_unlock_bh(&link_idr_lock);
2687 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2688 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2689 * anon_inode's release() call. This helper marksbpf_link as
2690 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2691 * is not decremented, it's the responsibility of a calling code that failed
2692 * to complete bpf_link initialization.
2694 void bpf_link_cleanup(struct bpf_link_primer *primer)
2696 primer->link->prog = NULL;
2697 bpf_link_free_id(primer->id);
2699 put_unused_fd(primer->fd);
2702 void bpf_link_inc(struct bpf_link *link)
2704 atomic64_inc(&link->refcnt);
2707 /* bpf_link_free is guaranteed to be called from process context */
2708 static void bpf_link_free(struct bpf_link *link)
2710 bpf_link_free_id(link->id);
2712 /* detach BPF program, clean up used resources */
2713 link->ops->release(link);
2714 bpf_prog_put(link->prog);
2716 /* free bpf_link and its containing memory */
2717 link->ops->dealloc(link);
2720 static void bpf_link_put_deferred(struct work_struct *work)
2722 struct bpf_link *link = container_of(work, struct bpf_link, work);
2724 bpf_link_free(link);
2727 /* bpf_link_put can be called from atomic context, but ensures that resources
2728 * are freed from process context
2730 void bpf_link_put(struct bpf_link *link)
2732 if (!atomic64_dec_and_test(&link->refcnt))
2736 INIT_WORK(&link->work, bpf_link_put_deferred);
2737 schedule_work(&link->work);
2739 bpf_link_free(link);
2742 EXPORT_SYMBOL(bpf_link_put);
2744 static int bpf_link_release(struct inode *inode, struct file *filp)
2746 struct bpf_link *link = filp->private_data;
2752 #ifdef CONFIG_PROC_FS
2753 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2754 #define BPF_MAP_TYPE(_id, _ops)
2755 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2756 static const char *bpf_link_type_strs[] = {
2757 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2758 #include <linux/bpf_types.h>
2760 #undef BPF_PROG_TYPE
2762 #undef BPF_LINK_TYPE
2764 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2766 const struct bpf_link *link = filp->private_data;
2767 const struct bpf_prog *prog = link->prog;
2768 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2770 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2776 bpf_link_type_strs[link->type],
2780 if (link->ops->show_fdinfo)
2781 link->ops->show_fdinfo(link, m);
2785 static const struct file_operations bpf_link_fops = {
2786 #ifdef CONFIG_PROC_FS
2787 .show_fdinfo = bpf_link_show_fdinfo,
2789 .release = bpf_link_release,
2790 .read = bpf_dummy_read,
2791 .write = bpf_dummy_write,
2794 static int bpf_link_alloc_id(struct bpf_link *link)
2798 idr_preload(GFP_KERNEL);
2799 spin_lock_bh(&link_idr_lock);
2800 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2801 spin_unlock_bh(&link_idr_lock);
2807 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2808 * reserving unused FD and allocating ID from link_idr. This is to be paired
2809 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2810 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2811 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2812 * transient state is passed around in struct bpf_link_primer.
2813 * This is preferred way to create and initialize bpf_link, especially when
2814 * there are complicated and expensive operations in between creating bpf_link
2815 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2816 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2817 * expensive (and potentially failing) roll back operations in a rare case
2818 * that file, FD, or ID can't be allocated.
2820 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2825 fd = get_unused_fd_flags(O_CLOEXEC);
2830 id = bpf_link_alloc_id(link);
2836 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2838 bpf_link_free_id(id);
2840 return PTR_ERR(file);
2843 primer->link = link;
2844 primer->file = file;
2850 int bpf_link_settle(struct bpf_link_primer *primer)
2852 /* make bpf_link fetchable by ID */
2853 spin_lock_bh(&link_idr_lock);
2854 primer->link->id = primer->id;
2855 spin_unlock_bh(&link_idr_lock);
2856 /* make bpf_link fetchable by FD */
2857 fd_install(primer->fd, primer->file);
2858 /* pass through installed FD */
2862 int bpf_link_new_fd(struct bpf_link *link)
2864 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2867 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2869 struct fd f = fdget(ufd);
2870 struct bpf_link *link;
2873 return ERR_PTR(-EBADF);
2874 if (f.file->f_op != &bpf_link_fops) {
2876 return ERR_PTR(-EINVAL);
2879 link = f.file->private_data;
2885 EXPORT_SYMBOL(bpf_link_get_from_fd);
2887 static void bpf_tracing_link_release(struct bpf_link *link)
2889 struct bpf_tracing_link *tr_link =
2890 container_of(link, struct bpf_tracing_link, link.link);
2892 WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link,
2893 tr_link->trampoline));
2895 bpf_trampoline_put(tr_link->trampoline);
2897 /* tgt_prog is NULL if target is a kernel function */
2898 if (tr_link->tgt_prog)
2899 bpf_prog_put(tr_link->tgt_prog);
2902 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2904 struct bpf_tracing_link *tr_link =
2905 container_of(link, struct bpf_tracing_link, link.link);
2910 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2911 struct seq_file *seq)
2913 struct bpf_tracing_link *tr_link =
2914 container_of(link, struct bpf_tracing_link, link.link);
2917 "attach_type:\t%d\n",
2918 tr_link->attach_type);
2921 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2922 struct bpf_link_info *info)
2924 struct bpf_tracing_link *tr_link =
2925 container_of(link, struct bpf_tracing_link, link.link);
2927 info->tracing.attach_type = tr_link->attach_type;
2928 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2929 &info->tracing.target_obj_id,
2930 &info->tracing.target_btf_id);
2935 static const struct bpf_link_ops bpf_tracing_link_lops = {
2936 .release = bpf_tracing_link_release,
2937 .dealloc = bpf_tracing_link_dealloc,
2938 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2939 .fill_link_info = bpf_tracing_link_fill_link_info,
2942 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2947 struct bpf_link_primer link_primer;
2948 struct bpf_prog *tgt_prog = NULL;
2949 struct bpf_trampoline *tr = NULL;
2950 struct bpf_tracing_link *link;
2954 switch (prog->type) {
2955 case BPF_PROG_TYPE_TRACING:
2956 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2957 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2958 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2963 case BPF_PROG_TYPE_EXT:
2964 if (prog->expected_attach_type != 0) {
2969 case BPF_PROG_TYPE_LSM:
2970 if (prog->expected_attach_type != BPF_LSM_MAC) {
2980 if (!!tgt_prog_fd != !!btf_id) {
2986 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2987 if (prog->type != BPF_PROG_TYPE_EXT) {
2992 tgt_prog = bpf_prog_get(tgt_prog_fd);
2993 if (IS_ERR(tgt_prog)) {
2994 err = PTR_ERR(tgt_prog);
2999 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
3002 link = kzalloc(sizeof(*link), GFP_USER);
3007 bpf_link_init(&link->link.link, BPF_LINK_TYPE_TRACING,
3008 &bpf_tracing_link_lops, prog);
3009 link->attach_type = prog->expected_attach_type;
3010 link->link.cookie = bpf_cookie;
3012 mutex_lock(&prog->aux->dst_mutex);
3014 /* There are a few possible cases here:
3016 * - if prog->aux->dst_trampoline is set, the program was just loaded
3017 * and not yet attached to anything, so we can use the values stored
3020 * - if prog->aux->dst_trampoline is NULL, the program has already been
3021 * attached to a target and its initial target was cleared (below)
3023 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
3024 * target_btf_id using the link_create API.
3026 * - if tgt_prog == NULL when this function was called using the old
3027 * raw_tracepoint_open API, and we need a target from prog->aux
3029 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
3030 * was detached and is going for re-attachment.
3032 if (!prog->aux->dst_trampoline && !tgt_prog) {
3034 * Allow re-attach for TRACING and LSM programs. If it's
3035 * currently linked, bpf_trampoline_link_prog will fail.
3036 * EXT programs need to specify tgt_prog_fd, so they
3037 * re-attach in separate code path.
3039 if (prog->type != BPF_PROG_TYPE_TRACING &&
3040 prog->type != BPF_PROG_TYPE_LSM) {
3044 btf_id = prog->aux->attach_btf_id;
3045 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
3048 if (!prog->aux->dst_trampoline ||
3049 (key && key != prog->aux->dst_trampoline->key)) {
3050 /* If there is no saved target, or the specified target is
3051 * different from the destination specified at load time, we
3052 * need a new trampoline and a check for compatibility
3054 struct bpf_attach_target_info tgt_info = {};
3056 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
3061 tr = bpf_trampoline_get(key, &tgt_info);
3067 /* The caller didn't specify a target, or the target was the
3068 * same as the destination supplied during program load. This
3069 * means we can reuse the trampoline and reference from program
3070 * load time, and there is no need to allocate a new one. This
3071 * can only happen once for any program, as the saved values in
3072 * prog->aux are cleared below.
3074 tr = prog->aux->dst_trampoline;
3075 tgt_prog = prog->aux->dst_prog;
3078 err = bpf_link_prime(&link->link.link, &link_primer);
3082 err = bpf_trampoline_link_prog(&link->link, tr);
3084 bpf_link_cleanup(&link_primer);
3089 link->tgt_prog = tgt_prog;
3090 link->trampoline = tr;
3092 /* Always clear the trampoline and target prog from prog->aux to make
3093 * sure the original attach destination is not kept alive after a
3094 * program is (re-)attached to another target.
3096 if (prog->aux->dst_prog &&
3097 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
3098 /* got extra prog ref from syscall, or attaching to different prog */
3099 bpf_prog_put(prog->aux->dst_prog);
3100 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
3101 /* we allocated a new trampoline, so free the old one */
3102 bpf_trampoline_put(prog->aux->dst_trampoline);
3104 prog->aux->dst_prog = NULL;
3105 prog->aux->dst_trampoline = NULL;
3106 mutex_unlock(&prog->aux->dst_mutex);
3108 return bpf_link_settle(&link_primer);
3110 if (tr && tr != prog->aux->dst_trampoline)
3111 bpf_trampoline_put(tr);
3112 mutex_unlock(&prog->aux->dst_mutex);
3115 if (tgt_prog_fd && tgt_prog)
3116 bpf_prog_put(tgt_prog);
3120 struct bpf_raw_tp_link {
3121 struct bpf_link link;
3122 struct bpf_raw_event_map *btp;
3125 static void bpf_raw_tp_link_release(struct bpf_link *link)
3127 struct bpf_raw_tp_link *raw_tp =
3128 container_of(link, struct bpf_raw_tp_link, link);
3130 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
3131 bpf_put_raw_tracepoint(raw_tp->btp);
3134 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
3136 struct bpf_raw_tp_link *raw_tp =
3137 container_of(link, struct bpf_raw_tp_link, link);
3142 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
3143 struct seq_file *seq)
3145 struct bpf_raw_tp_link *raw_tp_link =
3146 container_of(link, struct bpf_raw_tp_link, link);
3150 raw_tp_link->btp->tp->name);
3153 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
3154 struct bpf_link_info *info)
3156 struct bpf_raw_tp_link *raw_tp_link =
3157 container_of(link, struct bpf_raw_tp_link, link);
3158 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
3159 const char *tp_name = raw_tp_link->btp->tp->name;
3160 u32 ulen = info->raw_tracepoint.tp_name_len;
3161 size_t tp_len = strlen(tp_name);
3166 info->raw_tracepoint.tp_name_len = tp_len + 1;
3171 if (ulen >= tp_len + 1) {
3172 if (copy_to_user(ubuf, tp_name, tp_len + 1))
3177 if (copy_to_user(ubuf, tp_name, ulen - 1))
3179 if (put_user(zero, ubuf + ulen - 1))
3187 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
3188 .release = bpf_raw_tp_link_release,
3189 .dealloc = bpf_raw_tp_link_dealloc,
3190 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
3191 .fill_link_info = bpf_raw_tp_link_fill_link_info,
3194 #ifdef CONFIG_PERF_EVENTS
3195 struct bpf_perf_link {
3196 struct bpf_link link;
3197 struct file *perf_file;
3200 static void bpf_perf_link_release(struct bpf_link *link)
3202 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3203 struct perf_event *event = perf_link->perf_file->private_data;
3205 perf_event_free_bpf_prog(event);
3206 fput(perf_link->perf_file);
3209 static void bpf_perf_link_dealloc(struct bpf_link *link)
3211 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3216 static const struct bpf_link_ops bpf_perf_link_lops = {
3217 .release = bpf_perf_link_release,
3218 .dealloc = bpf_perf_link_dealloc,
3221 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3223 struct bpf_link_primer link_primer;
3224 struct bpf_perf_link *link;
3225 struct perf_event *event;
3226 struct file *perf_file;
3229 if (attr->link_create.flags)
3232 perf_file = perf_event_get(attr->link_create.target_fd);
3233 if (IS_ERR(perf_file))
3234 return PTR_ERR(perf_file);
3236 link = kzalloc(sizeof(*link), GFP_USER);
3241 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog);
3242 link->perf_file = perf_file;
3244 err = bpf_link_prime(&link->link, &link_primer);
3250 event = perf_file->private_data;
3251 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
3253 bpf_link_cleanup(&link_primer);
3256 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
3259 return bpf_link_settle(&link_primer);
3266 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3270 #endif /* CONFIG_PERF_EVENTS */
3272 static int bpf_raw_tp_link_attach(struct bpf_prog *prog,
3273 const char __user *user_tp_name)
3275 struct bpf_link_primer link_primer;
3276 struct bpf_raw_tp_link *link;
3277 struct bpf_raw_event_map *btp;
3278 const char *tp_name;
3282 switch (prog->type) {
3283 case BPF_PROG_TYPE_TRACING:
3284 case BPF_PROG_TYPE_EXT:
3285 case BPF_PROG_TYPE_LSM:
3287 /* The attach point for this category of programs
3288 * should be specified via btf_id during program load.
3291 if (prog->type == BPF_PROG_TYPE_TRACING &&
3292 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
3293 tp_name = prog->aux->attach_func_name;
3296 return bpf_tracing_prog_attach(prog, 0, 0, 0);
3297 case BPF_PROG_TYPE_RAW_TRACEPOINT:
3298 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
3299 if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0)
3301 buf[sizeof(buf) - 1] = 0;
3308 btp = bpf_get_raw_tracepoint(tp_name);
3312 link = kzalloc(sizeof(*link), GFP_USER);
3317 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
3318 &bpf_raw_tp_link_lops, prog);
3321 err = bpf_link_prime(&link->link, &link_primer);
3327 err = bpf_probe_register(link->btp, prog);
3329 bpf_link_cleanup(&link_primer);
3333 return bpf_link_settle(&link_primer);
3336 bpf_put_raw_tracepoint(btp);
3340 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
3342 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
3344 struct bpf_prog *prog;
3347 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
3350 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
3352 return PTR_ERR(prog);
3354 fd = bpf_raw_tp_link_attach(prog, u64_to_user_ptr(attr->raw_tracepoint.name));
3360 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
3361 enum bpf_attach_type attach_type)
3363 switch (prog->type) {
3364 case BPF_PROG_TYPE_CGROUP_SOCK:
3365 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3366 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3367 case BPF_PROG_TYPE_SK_LOOKUP:
3368 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
3369 case BPF_PROG_TYPE_CGROUP_SKB:
3370 if (!capable(CAP_NET_ADMIN))
3371 /* cg-skb progs can be loaded by unpriv user.
3372 * check permissions at attach time.
3375 return prog->enforce_expected_attach_type &&
3376 prog->expected_attach_type != attach_type ?
3383 static enum bpf_prog_type
3384 attach_type_to_prog_type(enum bpf_attach_type attach_type)
3386 switch (attach_type) {
3387 case BPF_CGROUP_INET_INGRESS:
3388 case BPF_CGROUP_INET_EGRESS:
3389 return BPF_PROG_TYPE_CGROUP_SKB;
3390 case BPF_CGROUP_INET_SOCK_CREATE:
3391 case BPF_CGROUP_INET_SOCK_RELEASE:
3392 case BPF_CGROUP_INET4_POST_BIND:
3393 case BPF_CGROUP_INET6_POST_BIND:
3394 return BPF_PROG_TYPE_CGROUP_SOCK;
3395 case BPF_CGROUP_INET4_BIND:
3396 case BPF_CGROUP_INET6_BIND:
3397 case BPF_CGROUP_INET4_CONNECT:
3398 case BPF_CGROUP_INET6_CONNECT:
3399 case BPF_CGROUP_INET4_GETPEERNAME:
3400 case BPF_CGROUP_INET6_GETPEERNAME:
3401 case BPF_CGROUP_INET4_GETSOCKNAME:
3402 case BPF_CGROUP_INET6_GETSOCKNAME:
3403 case BPF_CGROUP_UDP4_SENDMSG:
3404 case BPF_CGROUP_UDP6_SENDMSG:
3405 case BPF_CGROUP_UDP4_RECVMSG:
3406 case BPF_CGROUP_UDP6_RECVMSG:
3407 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3408 case BPF_CGROUP_SOCK_OPS:
3409 return BPF_PROG_TYPE_SOCK_OPS;
3410 case BPF_CGROUP_DEVICE:
3411 return BPF_PROG_TYPE_CGROUP_DEVICE;
3412 case BPF_SK_MSG_VERDICT:
3413 return BPF_PROG_TYPE_SK_MSG;
3414 case BPF_SK_SKB_STREAM_PARSER:
3415 case BPF_SK_SKB_STREAM_VERDICT:
3416 case BPF_SK_SKB_VERDICT:
3417 return BPF_PROG_TYPE_SK_SKB;
3418 case BPF_LIRC_MODE2:
3419 return BPF_PROG_TYPE_LIRC_MODE2;
3420 case BPF_FLOW_DISSECTOR:
3421 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3422 case BPF_CGROUP_SYSCTL:
3423 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3424 case BPF_CGROUP_GETSOCKOPT:
3425 case BPF_CGROUP_SETSOCKOPT:
3426 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
3427 case BPF_TRACE_ITER:
3428 case BPF_TRACE_RAW_TP:
3429 case BPF_TRACE_FENTRY:
3430 case BPF_TRACE_FEXIT:
3431 case BPF_MODIFY_RETURN:
3432 return BPF_PROG_TYPE_TRACING;
3434 return BPF_PROG_TYPE_LSM;
3436 return BPF_PROG_TYPE_SK_LOOKUP;
3438 return BPF_PROG_TYPE_XDP;
3439 case BPF_LSM_CGROUP:
3440 return BPF_PROG_TYPE_LSM;
3442 return BPF_PROG_TYPE_UNSPEC;
3446 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
3448 #define BPF_F_ATTACH_MASK \
3449 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
3451 static int bpf_prog_attach(const union bpf_attr *attr)
3453 enum bpf_prog_type ptype;
3454 struct bpf_prog *prog;
3457 if (CHECK_ATTR(BPF_PROG_ATTACH))
3460 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
3463 ptype = attach_type_to_prog_type(attr->attach_type);
3464 if (ptype == BPF_PROG_TYPE_UNSPEC)
3467 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3469 return PTR_ERR(prog);
3471 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3477 case BPF_PROG_TYPE_SK_SKB:
3478 case BPF_PROG_TYPE_SK_MSG:
3479 ret = sock_map_get_from_fd(attr, prog);
3481 case BPF_PROG_TYPE_LIRC_MODE2:
3482 ret = lirc_prog_attach(attr, prog);
3484 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3485 ret = netns_bpf_prog_attach(attr, prog);
3487 case BPF_PROG_TYPE_CGROUP_DEVICE:
3488 case BPF_PROG_TYPE_CGROUP_SKB:
3489 case BPF_PROG_TYPE_CGROUP_SOCK:
3490 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3491 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3492 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3493 case BPF_PROG_TYPE_SOCK_OPS:
3494 case BPF_PROG_TYPE_LSM:
3495 if (ptype == BPF_PROG_TYPE_LSM &&
3496 prog->expected_attach_type != BPF_LSM_CGROUP)
3499 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3510 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3512 static int bpf_prog_detach(const union bpf_attr *attr)
3514 enum bpf_prog_type ptype;
3516 if (CHECK_ATTR(BPF_PROG_DETACH))
3519 ptype = attach_type_to_prog_type(attr->attach_type);
3522 case BPF_PROG_TYPE_SK_MSG:
3523 case BPF_PROG_TYPE_SK_SKB:
3524 return sock_map_prog_detach(attr, ptype);
3525 case BPF_PROG_TYPE_LIRC_MODE2:
3526 return lirc_prog_detach(attr);
3527 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3528 return netns_bpf_prog_detach(attr, ptype);
3529 case BPF_PROG_TYPE_CGROUP_DEVICE:
3530 case BPF_PROG_TYPE_CGROUP_SKB:
3531 case BPF_PROG_TYPE_CGROUP_SOCK:
3532 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3533 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3534 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3535 case BPF_PROG_TYPE_SOCK_OPS:
3536 case BPF_PROG_TYPE_LSM:
3537 return cgroup_bpf_prog_detach(attr, ptype);
3543 #define BPF_PROG_QUERY_LAST_FIELD query.prog_attach_flags
3545 static int bpf_prog_query(const union bpf_attr *attr,
3546 union bpf_attr __user *uattr)
3548 if (!capable(CAP_NET_ADMIN))
3550 if (CHECK_ATTR(BPF_PROG_QUERY))
3552 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3555 switch (attr->query.attach_type) {
3556 case BPF_CGROUP_INET_INGRESS:
3557 case BPF_CGROUP_INET_EGRESS:
3558 case BPF_CGROUP_INET_SOCK_CREATE:
3559 case BPF_CGROUP_INET_SOCK_RELEASE:
3560 case BPF_CGROUP_INET4_BIND:
3561 case BPF_CGROUP_INET6_BIND:
3562 case BPF_CGROUP_INET4_POST_BIND:
3563 case BPF_CGROUP_INET6_POST_BIND:
3564 case BPF_CGROUP_INET4_CONNECT:
3565 case BPF_CGROUP_INET6_CONNECT:
3566 case BPF_CGROUP_INET4_GETPEERNAME:
3567 case BPF_CGROUP_INET6_GETPEERNAME:
3568 case BPF_CGROUP_INET4_GETSOCKNAME:
3569 case BPF_CGROUP_INET6_GETSOCKNAME:
3570 case BPF_CGROUP_UDP4_SENDMSG:
3571 case BPF_CGROUP_UDP6_SENDMSG:
3572 case BPF_CGROUP_UDP4_RECVMSG:
3573 case BPF_CGROUP_UDP6_RECVMSG:
3574 case BPF_CGROUP_SOCK_OPS:
3575 case BPF_CGROUP_DEVICE:
3576 case BPF_CGROUP_SYSCTL:
3577 case BPF_CGROUP_GETSOCKOPT:
3578 case BPF_CGROUP_SETSOCKOPT:
3579 case BPF_LSM_CGROUP:
3580 return cgroup_bpf_prog_query(attr, uattr);
3581 case BPF_LIRC_MODE2:
3582 return lirc_prog_query(attr, uattr);
3583 case BPF_FLOW_DISSECTOR:
3585 return netns_bpf_prog_query(attr, uattr);
3586 case BPF_SK_SKB_STREAM_PARSER:
3587 case BPF_SK_SKB_STREAM_VERDICT:
3588 case BPF_SK_MSG_VERDICT:
3589 case BPF_SK_SKB_VERDICT:
3590 return sock_map_bpf_prog_query(attr, uattr);
3596 #define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size
3598 static int bpf_prog_test_run(const union bpf_attr *attr,
3599 union bpf_attr __user *uattr)
3601 struct bpf_prog *prog;
3602 int ret = -ENOTSUPP;
3604 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3607 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3608 (!attr->test.ctx_size_in && attr->test.ctx_in))
3611 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3612 (!attr->test.ctx_size_out && attr->test.ctx_out))
3615 prog = bpf_prog_get(attr->test.prog_fd);
3617 return PTR_ERR(prog);
3619 if (prog->aux->ops->test_run)
3620 ret = prog->aux->ops->test_run(prog, attr, uattr);
3626 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3628 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3629 union bpf_attr __user *uattr,
3633 u32 next_id = attr->start_id;
3636 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3639 if (!capable(CAP_SYS_ADMIN))
3644 if (!idr_get_next(idr, &next_id))
3646 spin_unlock_bh(lock);
3649 err = put_user(next_id, &uattr->next_id);
3654 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3656 struct bpf_map *map;
3658 spin_lock_bh(&map_idr_lock);
3660 map = idr_get_next(&map_idr, id);
3662 map = __bpf_map_inc_not_zero(map, false);
3668 spin_unlock_bh(&map_idr_lock);
3673 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3675 struct bpf_prog *prog;
3677 spin_lock_bh(&prog_idr_lock);
3679 prog = idr_get_next(&prog_idr, id);
3681 prog = bpf_prog_inc_not_zero(prog);
3687 spin_unlock_bh(&prog_idr_lock);
3692 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3694 struct bpf_prog *bpf_prog_by_id(u32 id)
3696 struct bpf_prog *prog;
3699 return ERR_PTR(-ENOENT);
3701 spin_lock_bh(&prog_idr_lock);
3702 prog = idr_find(&prog_idr, id);
3704 prog = bpf_prog_inc_not_zero(prog);
3706 prog = ERR_PTR(-ENOENT);
3707 spin_unlock_bh(&prog_idr_lock);
3711 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3713 struct bpf_prog *prog;
3714 u32 id = attr->prog_id;
3717 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3720 if (!capable(CAP_SYS_ADMIN))
3723 prog = bpf_prog_by_id(id);
3725 return PTR_ERR(prog);
3727 fd = bpf_prog_new_fd(prog);
3734 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3736 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3738 struct bpf_map *map;
3739 u32 id = attr->map_id;
3743 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3744 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3747 if (!capable(CAP_SYS_ADMIN))
3750 f_flags = bpf_get_file_flag(attr->open_flags);
3754 spin_lock_bh(&map_idr_lock);
3755 map = idr_find(&map_idr, id);
3757 map = __bpf_map_inc_not_zero(map, true);
3759 map = ERR_PTR(-ENOENT);
3760 spin_unlock_bh(&map_idr_lock);
3763 return PTR_ERR(map);
3765 fd = bpf_map_new_fd(map, f_flags);
3767 bpf_map_put_with_uref(map);
3772 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3773 unsigned long addr, u32 *off,
3776 const struct bpf_map *map;
3779 mutex_lock(&prog->aux->used_maps_mutex);
3780 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3781 map = prog->aux->used_maps[i];
3782 if (map == (void *)addr) {
3783 *type = BPF_PSEUDO_MAP_FD;
3786 if (!map->ops->map_direct_value_meta)
3788 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3789 *type = BPF_PSEUDO_MAP_VALUE;
3796 mutex_unlock(&prog->aux->used_maps_mutex);
3800 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3801 const struct cred *f_cred)
3803 const struct bpf_map *map;
3804 struct bpf_insn *insns;
3810 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3815 for (i = 0; i < prog->len; i++) {
3816 code = insns[i].code;
3818 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3819 insns[i].code = BPF_JMP | BPF_CALL;
3820 insns[i].imm = BPF_FUNC_tail_call;
3823 if (code == (BPF_JMP | BPF_CALL) ||
3824 code == (BPF_JMP | BPF_CALL_ARGS)) {
3825 if (code == (BPF_JMP | BPF_CALL_ARGS))
3826 insns[i].code = BPF_JMP | BPF_CALL;
3827 if (!bpf_dump_raw_ok(f_cred))
3831 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3832 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3836 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3839 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3840 map = bpf_map_from_imm(prog, imm, &off, &type);
3842 insns[i].src_reg = type;
3843 insns[i].imm = map->id;
3844 insns[i + 1].imm = off;
3852 static int set_info_rec_size(struct bpf_prog_info *info)
3855 * Ensure info.*_rec_size is the same as kernel expected size
3859 * Only allow zero *_rec_size if both _rec_size and _cnt are
3860 * zero. In this case, the kernel will set the expected
3861 * _rec_size back to the info.
3864 if ((info->nr_func_info || info->func_info_rec_size) &&
3865 info->func_info_rec_size != sizeof(struct bpf_func_info))
3868 if ((info->nr_line_info || info->line_info_rec_size) &&
3869 info->line_info_rec_size != sizeof(struct bpf_line_info))
3872 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3873 info->jited_line_info_rec_size != sizeof(__u64))
3876 info->func_info_rec_size = sizeof(struct bpf_func_info);
3877 info->line_info_rec_size = sizeof(struct bpf_line_info);
3878 info->jited_line_info_rec_size = sizeof(__u64);
3883 static int bpf_prog_get_info_by_fd(struct file *file,
3884 struct bpf_prog *prog,
3885 const union bpf_attr *attr,
3886 union bpf_attr __user *uattr)
3888 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3889 struct btf *attach_btf = bpf_prog_get_target_btf(prog);
3890 struct bpf_prog_info info;
3891 u32 info_len = attr->info.info_len;
3892 struct bpf_prog_kstats stats;
3893 char __user *uinsns;
3897 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3900 info_len = min_t(u32, sizeof(info), info_len);
3902 memset(&info, 0, sizeof(info));
3903 if (copy_from_user(&info, uinfo, info_len))
3906 info.type = prog->type;
3907 info.id = prog->aux->id;
3908 info.load_time = prog->aux->load_time;
3909 info.created_by_uid = from_kuid_munged(current_user_ns(),
3910 prog->aux->user->uid);
3911 info.gpl_compatible = prog->gpl_compatible;
3913 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3914 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3916 mutex_lock(&prog->aux->used_maps_mutex);
3917 ulen = info.nr_map_ids;
3918 info.nr_map_ids = prog->aux->used_map_cnt;
3919 ulen = min_t(u32, info.nr_map_ids, ulen);
3921 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3924 for (i = 0; i < ulen; i++)
3925 if (put_user(prog->aux->used_maps[i]->id,
3926 &user_map_ids[i])) {
3927 mutex_unlock(&prog->aux->used_maps_mutex);
3931 mutex_unlock(&prog->aux->used_maps_mutex);
3933 err = set_info_rec_size(&info);
3937 bpf_prog_get_stats(prog, &stats);
3938 info.run_time_ns = stats.nsecs;
3939 info.run_cnt = stats.cnt;
3940 info.recursion_misses = stats.misses;
3942 info.verified_insns = prog->aux->verified_insns;
3944 if (!bpf_capable()) {
3945 info.jited_prog_len = 0;
3946 info.xlated_prog_len = 0;
3947 info.nr_jited_ksyms = 0;
3948 info.nr_jited_func_lens = 0;
3949 info.nr_func_info = 0;
3950 info.nr_line_info = 0;
3951 info.nr_jited_line_info = 0;
3955 ulen = info.xlated_prog_len;
3956 info.xlated_prog_len = bpf_prog_insn_size(prog);
3957 if (info.xlated_prog_len && ulen) {
3958 struct bpf_insn *insns_sanitized;
3961 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3962 info.xlated_prog_insns = 0;
3965 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3966 if (!insns_sanitized)
3968 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3969 ulen = min_t(u32, info.xlated_prog_len, ulen);
3970 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3971 kfree(insns_sanitized);
3976 if (bpf_prog_is_dev_bound(prog->aux)) {
3977 err = bpf_prog_offload_info_fill(&info, prog);
3983 /* NOTE: the following code is supposed to be skipped for offload.
3984 * bpf_prog_offload_info_fill() is the place to fill similar fields
3987 ulen = info.jited_prog_len;
3988 if (prog->aux->func_cnt) {
3991 info.jited_prog_len = 0;
3992 for (i = 0; i < prog->aux->func_cnt; i++)
3993 info.jited_prog_len += prog->aux->func[i]->jited_len;
3995 info.jited_prog_len = prog->jited_len;
3998 if (info.jited_prog_len && ulen) {
3999 if (bpf_dump_raw_ok(file->f_cred)) {
4000 uinsns = u64_to_user_ptr(info.jited_prog_insns);
4001 ulen = min_t(u32, info.jited_prog_len, ulen);
4003 /* for multi-function programs, copy the JITed
4004 * instructions for all the functions
4006 if (prog->aux->func_cnt) {
4011 for (i = 0; i < prog->aux->func_cnt; i++) {
4012 len = prog->aux->func[i]->jited_len;
4013 len = min_t(u32, len, free);
4014 img = (u8 *) prog->aux->func[i]->bpf_func;
4015 if (copy_to_user(uinsns, img, len))
4023 if (copy_to_user(uinsns, prog->bpf_func, ulen))
4027 info.jited_prog_insns = 0;
4031 ulen = info.nr_jited_ksyms;
4032 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
4034 if (bpf_dump_raw_ok(file->f_cred)) {
4035 unsigned long ksym_addr;
4036 u64 __user *user_ksyms;
4039 /* copy the address of the kernel symbol
4040 * corresponding to each function
4042 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
4043 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
4044 if (prog->aux->func_cnt) {
4045 for (i = 0; i < ulen; i++) {
4046 ksym_addr = (unsigned long)
4047 prog->aux->func[i]->bpf_func;
4048 if (put_user((u64) ksym_addr,
4053 ksym_addr = (unsigned long) prog->bpf_func;
4054 if (put_user((u64) ksym_addr, &user_ksyms[0]))
4058 info.jited_ksyms = 0;
4062 ulen = info.nr_jited_func_lens;
4063 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
4065 if (bpf_dump_raw_ok(file->f_cred)) {
4066 u32 __user *user_lens;
4069 /* copy the JITed image lengths for each function */
4070 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
4071 user_lens = u64_to_user_ptr(info.jited_func_lens);
4072 if (prog->aux->func_cnt) {
4073 for (i = 0; i < ulen; i++) {
4075 prog->aux->func[i]->jited_len;
4076 if (put_user(func_len, &user_lens[i]))
4080 func_len = prog->jited_len;
4081 if (put_user(func_len, &user_lens[0]))
4085 info.jited_func_lens = 0;
4090 info.btf_id = btf_obj_id(prog->aux->btf);
4091 info.attach_btf_id = prog->aux->attach_btf_id;
4093 info.attach_btf_obj_id = btf_obj_id(attach_btf);
4095 ulen = info.nr_func_info;
4096 info.nr_func_info = prog->aux->func_info_cnt;
4097 if (info.nr_func_info && ulen) {
4098 char __user *user_finfo;
4100 user_finfo = u64_to_user_ptr(info.func_info);
4101 ulen = min_t(u32, info.nr_func_info, ulen);
4102 if (copy_to_user(user_finfo, prog->aux->func_info,
4103 info.func_info_rec_size * ulen))
4107 ulen = info.nr_line_info;
4108 info.nr_line_info = prog->aux->nr_linfo;
4109 if (info.nr_line_info && ulen) {
4110 __u8 __user *user_linfo;
4112 user_linfo = u64_to_user_ptr(info.line_info);
4113 ulen = min_t(u32, info.nr_line_info, ulen);
4114 if (copy_to_user(user_linfo, prog->aux->linfo,
4115 info.line_info_rec_size * ulen))
4119 ulen = info.nr_jited_line_info;
4120 if (prog->aux->jited_linfo)
4121 info.nr_jited_line_info = prog->aux->nr_linfo;
4123 info.nr_jited_line_info = 0;
4124 if (info.nr_jited_line_info && ulen) {
4125 if (bpf_dump_raw_ok(file->f_cred)) {
4126 unsigned long line_addr;
4127 __u64 __user *user_linfo;
4130 user_linfo = u64_to_user_ptr(info.jited_line_info);
4131 ulen = min_t(u32, info.nr_jited_line_info, ulen);
4132 for (i = 0; i < ulen; i++) {
4133 line_addr = (unsigned long)prog->aux->jited_linfo[i];
4134 if (put_user((__u64)line_addr, &user_linfo[i]))
4138 info.jited_line_info = 0;
4142 ulen = info.nr_prog_tags;
4143 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
4145 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
4148 user_prog_tags = u64_to_user_ptr(info.prog_tags);
4149 ulen = min_t(u32, info.nr_prog_tags, ulen);
4150 if (prog->aux->func_cnt) {
4151 for (i = 0; i < ulen; i++) {
4152 if (copy_to_user(user_prog_tags[i],
4153 prog->aux->func[i]->tag,
4158 if (copy_to_user(user_prog_tags[0],
4159 prog->tag, BPF_TAG_SIZE))
4165 if (copy_to_user(uinfo, &info, info_len) ||
4166 put_user(info_len, &uattr->info.info_len))
4172 static int bpf_map_get_info_by_fd(struct file *file,
4173 struct bpf_map *map,
4174 const union bpf_attr *attr,
4175 union bpf_attr __user *uattr)
4177 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4178 struct bpf_map_info info;
4179 u32 info_len = attr->info.info_len;
4182 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
4185 info_len = min_t(u32, sizeof(info), info_len);
4187 memset(&info, 0, sizeof(info));
4188 info.type = map->map_type;
4190 info.key_size = map->key_size;
4191 info.value_size = map->value_size;
4192 info.max_entries = map->max_entries;
4193 info.map_flags = map->map_flags;
4194 info.map_extra = map->map_extra;
4195 memcpy(info.name, map->name, sizeof(map->name));
4198 info.btf_id = btf_obj_id(map->btf);
4199 info.btf_key_type_id = map->btf_key_type_id;
4200 info.btf_value_type_id = map->btf_value_type_id;
4202 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
4204 if (bpf_map_is_dev_bound(map)) {
4205 err = bpf_map_offload_info_fill(&info, map);
4210 if (copy_to_user(uinfo, &info, info_len) ||
4211 put_user(info_len, &uattr->info.info_len))
4217 static int bpf_btf_get_info_by_fd(struct file *file,
4219 const union bpf_attr *attr,
4220 union bpf_attr __user *uattr)
4222 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4223 u32 info_len = attr->info.info_len;
4226 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
4230 return btf_get_info_by_fd(btf, attr, uattr);
4233 static int bpf_link_get_info_by_fd(struct file *file,
4234 struct bpf_link *link,
4235 const union bpf_attr *attr,
4236 union bpf_attr __user *uattr)
4238 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4239 struct bpf_link_info info;
4240 u32 info_len = attr->info.info_len;
4243 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
4246 info_len = min_t(u32, sizeof(info), info_len);
4248 memset(&info, 0, sizeof(info));
4249 if (copy_from_user(&info, uinfo, info_len))
4252 info.type = link->type;
4254 info.prog_id = link->prog->aux->id;
4256 if (link->ops->fill_link_info) {
4257 err = link->ops->fill_link_info(link, &info);
4262 if (copy_to_user(uinfo, &info, info_len) ||
4263 put_user(info_len, &uattr->info.info_len))
4270 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
4272 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
4273 union bpf_attr __user *uattr)
4275 int ufd = attr->info.bpf_fd;
4279 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
4286 if (f.file->f_op == &bpf_prog_fops)
4287 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
4289 else if (f.file->f_op == &bpf_map_fops)
4290 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
4292 else if (f.file->f_op == &btf_fops)
4293 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
4294 else if (f.file->f_op == &bpf_link_fops)
4295 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
4304 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
4306 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
4308 if (CHECK_ATTR(BPF_BTF_LOAD))
4314 return btf_new_fd(attr, uattr);
4317 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
4319 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
4321 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
4324 if (!capable(CAP_SYS_ADMIN))
4327 return btf_get_fd_by_id(attr->btf_id);
4330 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
4331 union bpf_attr __user *uattr,
4332 u32 prog_id, u32 fd_type,
4333 const char *buf, u64 probe_offset,
4336 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
4337 u32 len = buf ? strlen(buf) : 0, input_len;
4340 if (put_user(len, &uattr->task_fd_query.buf_len))
4342 input_len = attr->task_fd_query.buf_len;
4343 if (input_len && ubuf) {
4345 /* nothing to copy, just make ubuf NULL terminated */
4348 if (put_user(zero, ubuf))
4350 } else if (input_len >= len + 1) {
4351 /* ubuf can hold the string with NULL terminator */
4352 if (copy_to_user(ubuf, buf, len + 1))
4355 /* ubuf cannot hold the string with NULL terminator,
4356 * do a partial copy with NULL terminator.
4361 if (copy_to_user(ubuf, buf, input_len - 1))
4363 if (put_user(zero, ubuf + input_len - 1))
4368 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
4369 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
4370 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
4371 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
4377 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
4379 static int bpf_task_fd_query(const union bpf_attr *attr,
4380 union bpf_attr __user *uattr)
4382 pid_t pid = attr->task_fd_query.pid;
4383 u32 fd = attr->task_fd_query.fd;
4384 const struct perf_event *event;
4385 struct task_struct *task;
4389 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
4392 if (!capable(CAP_SYS_ADMIN))
4395 if (attr->task_fd_query.flags != 0)
4398 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
4403 file = fget_task(task, fd);
4404 put_task_struct(task);
4408 if (file->f_op == &bpf_link_fops) {
4409 struct bpf_link *link = file->private_data;
4411 if (link->ops == &bpf_raw_tp_link_lops) {
4412 struct bpf_raw_tp_link *raw_tp =
4413 container_of(link, struct bpf_raw_tp_link, link);
4414 struct bpf_raw_event_map *btp = raw_tp->btp;
4416 err = bpf_task_fd_query_copy(attr, uattr,
4417 raw_tp->link.prog->aux->id,
4418 BPF_FD_TYPE_RAW_TRACEPOINT,
4419 btp->tp->name, 0, 0);
4425 event = perf_get_event(file);
4426 if (!IS_ERR(event)) {
4427 u64 probe_offset, probe_addr;
4428 u32 prog_id, fd_type;
4431 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4432 &buf, &probe_offset,
4435 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4449 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
4451 #define BPF_DO_BATCH(fn) \
4457 err = fn(map, attr, uattr); \
4460 static int bpf_map_do_batch(const union bpf_attr *attr,
4461 union bpf_attr __user *uattr,
4464 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH ||
4465 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
4466 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
4467 struct bpf_map *map;
4471 if (CHECK_ATTR(BPF_MAP_BATCH))
4474 ufd = attr->batch.map_fd;
4476 map = __bpf_map_get(f);
4478 return PTR_ERR(map);
4480 bpf_map_write_active_inc(map);
4481 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4485 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4490 if (cmd == BPF_MAP_LOOKUP_BATCH)
4491 BPF_DO_BATCH(map->ops->map_lookup_batch);
4492 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4493 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4494 else if (cmd == BPF_MAP_UPDATE_BATCH)
4495 BPF_DO_BATCH(map->ops->map_update_batch);
4497 BPF_DO_BATCH(map->ops->map_delete_batch);
4500 bpf_map_write_active_dec(map);
4505 #define BPF_LINK_CREATE_LAST_FIELD link_create.kprobe_multi.cookies
4506 static int link_create(union bpf_attr *attr, bpfptr_t uattr)
4508 enum bpf_prog_type ptype;
4509 struct bpf_prog *prog;
4512 if (CHECK_ATTR(BPF_LINK_CREATE))
4515 prog = bpf_prog_get(attr->link_create.prog_fd);
4517 return PTR_ERR(prog);
4519 ret = bpf_prog_attach_check_attach_type(prog,
4520 attr->link_create.attach_type);
4524 switch (prog->type) {
4525 case BPF_PROG_TYPE_EXT:
4527 case BPF_PROG_TYPE_PERF_EVENT:
4528 case BPF_PROG_TYPE_TRACEPOINT:
4529 if (attr->link_create.attach_type != BPF_PERF_EVENT) {
4534 case BPF_PROG_TYPE_KPROBE:
4535 if (attr->link_create.attach_type != BPF_PERF_EVENT &&
4536 attr->link_create.attach_type != BPF_TRACE_KPROBE_MULTI) {
4542 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4543 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4550 switch (prog->type) {
4551 case BPF_PROG_TYPE_CGROUP_SKB:
4552 case BPF_PROG_TYPE_CGROUP_SOCK:
4553 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4554 case BPF_PROG_TYPE_SOCK_OPS:
4555 case BPF_PROG_TYPE_CGROUP_DEVICE:
4556 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4557 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4558 ret = cgroup_bpf_link_attach(attr, prog);
4560 case BPF_PROG_TYPE_EXT:
4561 ret = bpf_tracing_prog_attach(prog,
4562 attr->link_create.target_fd,
4563 attr->link_create.target_btf_id,
4564 attr->link_create.tracing.cookie);
4566 case BPF_PROG_TYPE_LSM:
4567 case BPF_PROG_TYPE_TRACING:
4568 if (attr->link_create.attach_type != prog->expected_attach_type) {
4572 if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
4573 ret = bpf_raw_tp_link_attach(prog, NULL);
4574 else if (prog->expected_attach_type == BPF_TRACE_ITER)
4575 ret = bpf_iter_link_attach(attr, uattr, prog);
4576 else if (prog->expected_attach_type == BPF_LSM_CGROUP)
4577 ret = cgroup_bpf_link_attach(attr, prog);
4579 ret = bpf_tracing_prog_attach(prog,
4580 attr->link_create.target_fd,
4581 attr->link_create.target_btf_id,
4582 attr->link_create.tracing.cookie);
4584 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4585 case BPF_PROG_TYPE_SK_LOOKUP:
4586 ret = netns_bpf_link_create(attr, prog);
4589 case BPF_PROG_TYPE_XDP:
4590 ret = bpf_xdp_link_attach(attr, prog);
4593 case BPF_PROG_TYPE_PERF_EVENT:
4594 case BPF_PROG_TYPE_TRACEPOINT:
4595 ret = bpf_perf_link_attach(attr, prog);
4597 case BPF_PROG_TYPE_KPROBE:
4598 if (attr->link_create.attach_type == BPF_PERF_EVENT)
4599 ret = bpf_perf_link_attach(attr, prog);
4601 ret = bpf_kprobe_multi_link_attach(attr, prog);
4613 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4615 static int link_update(union bpf_attr *attr)
4617 struct bpf_prog *old_prog = NULL, *new_prog;
4618 struct bpf_link *link;
4622 if (CHECK_ATTR(BPF_LINK_UPDATE))
4625 flags = attr->link_update.flags;
4626 if (flags & ~BPF_F_REPLACE)
4629 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4631 return PTR_ERR(link);
4633 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4634 if (IS_ERR(new_prog)) {
4635 ret = PTR_ERR(new_prog);
4639 if (flags & BPF_F_REPLACE) {
4640 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4641 if (IS_ERR(old_prog)) {
4642 ret = PTR_ERR(old_prog);
4646 } else if (attr->link_update.old_prog_fd) {
4651 if (link->ops->update_prog)
4652 ret = link->ops->update_prog(link, new_prog, old_prog);
4658 bpf_prog_put(old_prog);
4660 bpf_prog_put(new_prog);
4666 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4668 static int link_detach(union bpf_attr *attr)
4670 struct bpf_link *link;
4673 if (CHECK_ATTR(BPF_LINK_DETACH))
4676 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4678 return PTR_ERR(link);
4680 if (link->ops->detach)
4681 ret = link->ops->detach(link);
4689 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4691 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4694 struct bpf_link *bpf_link_by_id(u32 id)
4696 struct bpf_link *link;
4699 return ERR_PTR(-ENOENT);
4701 spin_lock_bh(&link_idr_lock);
4702 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4703 link = idr_find(&link_idr, id);
4706 link = bpf_link_inc_not_zero(link);
4708 link = ERR_PTR(-EAGAIN);
4710 link = ERR_PTR(-ENOENT);
4712 spin_unlock_bh(&link_idr_lock);
4716 struct bpf_link *bpf_link_get_curr_or_next(u32 *id)
4718 struct bpf_link *link;
4720 spin_lock_bh(&link_idr_lock);
4722 link = idr_get_next(&link_idr, id);
4724 link = bpf_link_inc_not_zero(link);
4730 spin_unlock_bh(&link_idr_lock);
4735 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4737 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4739 struct bpf_link *link;
4740 u32 id = attr->link_id;
4743 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4746 if (!capable(CAP_SYS_ADMIN))
4749 link = bpf_link_by_id(id);
4751 return PTR_ERR(link);
4753 fd = bpf_link_new_fd(link);
4760 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4762 static int bpf_stats_release(struct inode *inode, struct file *file)
4764 mutex_lock(&bpf_stats_enabled_mutex);
4765 static_key_slow_dec(&bpf_stats_enabled_key.key);
4766 mutex_unlock(&bpf_stats_enabled_mutex);
4770 static const struct file_operations bpf_stats_fops = {
4771 .release = bpf_stats_release,
4774 static int bpf_enable_runtime_stats(void)
4778 mutex_lock(&bpf_stats_enabled_mutex);
4780 /* Set a very high limit to avoid overflow */
4781 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4782 mutex_unlock(&bpf_stats_enabled_mutex);
4786 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4788 static_key_slow_inc(&bpf_stats_enabled_key.key);
4790 mutex_unlock(&bpf_stats_enabled_mutex);
4794 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4796 static int bpf_enable_stats(union bpf_attr *attr)
4799 if (CHECK_ATTR(BPF_ENABLE_STATS))
4802 if (!capable(CAP_SYS_ADMIN))
4805 switch (attr->enable_stats.type) {
4806 case BPF_STATS_RUN_TIME:
4807 return bpf_enable_runtime_stats();
4814 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4816 static int bpf_iter_create(union bpf_attr *attr)
4818 struct bpf_link *link;
4821 if (CHECK_ATTR(BPF_ITER_CREATE))
4824 if (attr->iter_create.flags)
4827 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4829 return PTR_ERR(link);
4831 err = bpf_iter_new_fd(link);
4837 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4839 static int bpf_prog_bind_map(union bpf_attr *attr)
4841 struct bpf_prog *prog;
4842 struct bpf_map *map;
4843 struct bpf_map **used_maps_old, **used_maps_new;
4846 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4849 if (attr->prog_bind_map.flags)
4852 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4854 return PTR_ERR(prog);
4856 map = bpf_map_get(attr->prog_bind_map.map_fd);
4862 mutex_lock(&prog->aux->used_maps_mutex);
4864 used_maps_old = prog->aux->used_maps;
4866 for (i = 0; i < prog->aux->used_map_cnt; i++)
4867 if (used_maps_old[i] == map) {
4872 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4873 sizeof(used_maps_new[0]),
4875 if (!used_maps_new) {
4880 memcpy(used_maps_new, used_maps_old,
4881 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4882 used_maps_new[prog->aux->used_map_cnt] = map;
4884 prog->aux->used_map_cnt++;
4885 prog->aux->used_maps = used_maps_new;
4887 kfree(used_maps_old);
4890 mutex_unlock(&prog->aux->used_maps_mutex);
4899 static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
4901 union bpf_attr attr;
4905 capable = bpf_capable() || !sysctl_unprivileged_bpf_disabled;
4907 /* Intent here is for unprivileged_bpf_disabled to block key object
4908 * creation commands for unprivileged users; other actions depend
4909 * of fd availability and access to bpffs, so are dependent on
4910 * object creation success. Capabilities are later verified for
4911 * operations such as load and map create, so even with unprivileged
4912 * BPF disabled, capability checks are still carried out for these
4913 * and other operations.
4916 (cmd == BPF_MAP_CREATE || cmd == BPF_PROG_LOAD))
4919 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4922 size = min_t(u32, size, sizeof(attr));
4924 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4925 memset(&attr, 0, sizeof(attr));
4926 if (copy_from_bpfptr(&attr, uattr, size) != 0)
4929 err = security_bpf(cmd, &attr, size);
4934 case BPF_MAP_CREATE:
4935 err = map_create(&attr);
4937 case BPF_MAP_LOOKUP_ELEM:
4938 err = map_lookup_elem(&attr);
4940 case BPF_MAP_UPDATE_ELEM:
4941 err = map_update_elem(&attr, uattr);
4943 case BPF_MAP_DELETE_ELEM:
4944 err = map_delete_elem(&attr);
4946 case BPF_MAP_GET_NEXT_KEY:
4947 err = map_get_next_key(&attr);
4949 case BPF_MAP_FREEZE:
4950 err = map_freeze(&attr);
4953 err = bpf_prog_load(&attr, uattr);
4956 err = bpf_obj_pin(&attr);
4959 err = bpf_obj_get(&attr);
4961 case BPF_PROG_ATTACH:
4962 err = bpf_prog_attach(&attr);
4964 case BPF_PROG_DETACH:
4965 err = bpf_prog_detach(&attr);
4967 case BPF_PROG_QUERY:
4968 err = bpf_prog_query(&attr, uattr.user);
4970 case BPF_PROG_TEST_RUN:
4971 err = bpf_prog_test_run(&attr, uattr.user);
4973 case BPF_PROG_GET_NEXT_ID:
4974 err = bpf_obj_get_next_id(&attr, uattr.user,
4975 &prog_idr, &prog_idr_lock);
4977 case BPF_MAP_GET_NEXT_ID:
4978 err = bpf_obj_get_next_id(&attr, uattr.user,
4979 &map_idr, &map_idr_lock);
4981 case BPF_BTF_GET_NEXT_ID:
4982 err = bpf_obj_get_next_id(&attr, uattr.user,
4983 &btf_idr, &btf_idr_lock);
4985 case BPF_PROG_GET_FD_BY_ID:
4986 err = bpf_prog_get_fd_by_id(&attr);
4988 case BPF_MAP_GET_FD_BY_ID:
4989 err = bpf_map_get_fd_by_id(&attr);
4991 case BPF_OBJ_GET_INFO_BY_FD:
4992 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
4994 case BPF_RAW_TRACEPOINT_OPEN:
4995 err = bpf_raw_tracepoint_open(&attr);
4998 err = bpf_btf_load(&attr, uattr);
5000 case BPF_BTF_GET_FD_BY_ID:
5001 err = bpf_btf_get_fd_by_id(&attr);
5003 case BPF_TASK_FD_QUERY:
5004 err = bpf_task_fd_query(&attr, uattr.user);
5006 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
5007 err = map_lookup_and_delete_elem(&attr);
5009 case BPF_MAP_LOOKUP_BATCH:
5010 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
5012 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
5013 err = bpf_map_do_batch(&attr, uattr.user,
5014 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
5016 case BPF_MAP_UPDATE_BATCH:
5017 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
5019 case BPF_MAP_DELETE_BATCH:
5020 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
5022 case BPF_LINK_CREATE:
5023 err = link_create(&attr, uattr);
5025 case BPF_LINK_UPDATE:
5026 err = link_update(&attr);
5028 case BPF_LINK_GET_FD_BY_ID:
5029 err = bpf_link_get_fd_by_id(&attr);
5031 case BPF_LINK_GET_NEXT_ID:
5032 err = bpf_obj_get_next_id(&attr, uattr.user,
5033 &link_idr, &link_idr_lock);
5035 case BPF_ENABLE_STATS:
5036 err = bpf_enable_stats(&attr);
5038 case BPF_ITER_CREATE:
5039 err = bpf_iter_create(&attr);
5041 case BPF_LINK_DETACH:
5042 err = link_detach(&attr);
5044 case BPF_PROG_BIND_MAP:
5045 err = bpf_prog_bind_map(&attr);
5055 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
5057 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
5060 static bool syscall_prog_is_valid_access(int off, int size,
5061 enum bpf_access_type type,
5062 const struct bpf_prog *prog,
5063 struct bpf_insn_access_aux *info)
5065 if (off < 0 || off >= U16_MAX)
5067 if (off % size != 0)
5072 BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
5075 case BPF_MAP_CREATE:
5076 case BPF_MAP_UPDATE_ELEM:
5077 case BPF_MAP_FREEZE:
5080 case BPF_LINK_CREATE:
5081 case BPF_RAW_TRACEPOINT_OPEN:
5086 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
5090 /* To shut up -Wmissing-prototypes.
5091 * This function is used by the kernel light skeleton
5092 * to load bpf programs when modules are loaded or during kernel boot.
5093 * See tools/lib/bpf/skel_internal.h
5095 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size);
5097 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5099 struct bpf_prog * __maybe_unused prog;
5100 struct bpf_tramp_run_ctx __maybe_unused run_ctx;
5103 #ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */
5104 case BPF_PROG_TEST_RUN:
5105 if (attr->test.data_in || attr->test.data_out ||
5106 attr->test.ctx_out || attr->test.duration ||
5107 attr->test.repeat || attr->test.flags)
5110 prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL);
5112 return PTR_ERR(prog);
5114 if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
5115 attr->test.ctx_size_in > U16_MAX) {
5120 run_ctx.bpf_cookie = 0;
5121 run_ctx.saved_run_ctx = NULL;
5122 if (!__bpf_prog_enter_sleepable(prog, &run_ctx)) {
5123 /* recursion detected */
5127 attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
5128 __bpf_prog_exit_sleepable(prog, 0 /* bpf_prog_run does runtime stats */, &run_ctx);
5133 return ____bpf_sys_bpf(cmd, attr, size);
5136 EXPORT_SYMBOL(kern_sys_bpf);
5138 static const struct bpf_func_proto bpf_sys_bpf_proto = {
5139 .func = bpf_sys_bpf,
5141 .ret_type = RET_INTEGER,
5142 .arg1_type = ARG_ANYTHING,
5143 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5144 .arg3_type = ARG_CONST_SIZE,
5147 const struct bpf_func_proto * __weak
5148 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5150 return bpf_base_func_proto(func_id);
5153 BPF_CALL_1(bpf_sys_close, u32, fd)
5155 /* When bpf program calls this helper there should not be
5156 * an fdget() without matching completed fdput().
5157 * This helper is allowed in the following callchain only:
5158 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
5160 return close_fd(fd);
5163 static const struct bpf_func_proto bpf_sys_close_proto = {
5164 .func = bpf_sys_close,
5166 .ret_type = RET_INTEGER,
5167 .arg1_type = ARG_ANYTHING,
5170 BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res)
5175 if (name_sz <= 1 || name[name_sz - 1])
5178 if (!bpf_dump_raw_ok(current_cred()))
5181 *res = kallsyms_lookup_name(name);
5182 return *res ? 0 : -ENOENT;
5185 static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = {
5186 .func = bpf_kallsyms_lookup_name,
5188 .ret_type = RET_INTEGER,
5189 .arg1_type = ARG_PTR_TO_MEM,
5190 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
5191 .arg3_type = ARG_ANYTHING,
5192 .arg4_type = ARG_PTR_TO_LONG,
5195 static const struct bpf_func_proto *
5196 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5199 case BPF_FUNC_sys_bpf:
5200 return !perfmon_capable() ? NULL : &bpf_sys_bpf_proto;
5201 case BPF_FUNC_btf_find_by_name_kind:
5202 return &bpf_btf_find_by_name_kind_proto;
5203 case BPF_FUNC_sys_close:
5204 return &bpf_sys_close_proto;
5205 case BPF_FUNC_kallsyms_lookup_name:
5206 return &bpf_kallsyms_lookup_name_proto;
5208 return tracing_prog_func_proto(func_id, prog);
5212 const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
5213 .get_func_proto = syscall_prog_func_proto,
5214 .is_valid_access = syscall_prog_is_valid_access,
5217 const struct bpf_prog_ops bpf_syscall_prog_ops = {
5218 .test_run = bpf_prog_test_run_syscall,
5221 #ifdef CONFIG_SYSCTL
5222 static int bpf_stats_handler(struct ctl_table *table, int write,
5223 void *buffer, size_t *lenp, loff_t *ppos)
5225 struct static_key *key = (struct static_key *)table->data;
5226 static int saved_val;
5228 struct ctl_table tmp = {
5230 .maxlen = sizeof(val),
5231 .mode = table->mode,
5232 .extra1 = SYSCTL_ZERO,
5233 .extra2 = SYSCTL_ONE,
5236 if (write && !capable(CAP_SYS_ADMIN))
5239 mutex_lock(&bpf_stats_enabled_mutex);
5241 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
5242 if (write && !ret && val != saved_val) {
5244 static_key_slow_inc(key);
5246 static_key_slow_dec(key);
5249 mutex_unlock(&bpf_stats_enabled_mutex);
5253 void __weak unpriv_ebpf_notify(int new_state)
5257 static int bpf_unpriv_handler(struct ctl_table *table, int write,
5258 void *buffer, size_t *lenp, loff_t *ppos)
5260 int ret, unpriv_enable = *(int *)table->data;
5261 bool locked_state = unpriv_enable == 1;
5262 struct ctl_table tmp = *table;
5264 if (write && !capable(CAP_SYS_ADMIN))
5267 tmp.data = &unpriv_enable;
5268 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
5269 if (write && !ret) {
5270 if (locked_state && unpriv_enable != 1)
5272 *(int *)table->data = unpriv_enable;
5275 unpriv_ebpf_notify(unpriv_enable);
5280 static struct ctl_table bpf_syscall_table[] = {
5282 .procname = "unprivileged_bpf_disabled",
5283 .data = &sysctl_unprivileged_bpf_disabled,
5284 .maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
5286 .proc_handler = bpf_unpriv_handler,
5287 .extra1 = SYSCTL_ZERO,
5288 .extra2 = SYSCTL_TWO,
5291 .procname = "bpf_stats_enabled",
5292 .data = &bpf_stats_enabled_key.key,
5293 .maxlen = sizeof(bpf_stats_enabled_key),
5295 .proc_handler = bpf_stats_handler,
5300 static int __init bpf_syscall_sysctl_init(void)
5302 register_sysctl_init("kernel", bpf_syscall_table);
5305 late_initcall(bpf_syscall_sysctl_init);
5306 #endif /* CONFIG_SYSCTL */