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 /* Avoid spawning kworkers, since they all might contend
642 * for the same mutex like slab_mutex.
644 queue_work(system_unbound_wq, &map->work);
648 void bpf_map_put(struct bpf_map *map)
650 __bpf_map_put(map, true);
652 EXPORT_SYMBOL_GPL(bpf_map_put);
654 void bpf_map_put_with_uref(struct bpf_map *map)
656 bpf_map_put_uref(map);
660 static int bpf_map_release(struct inode *inode, struct file *filp)
662 struct bpf_map *map = filp->private_data;
664 if (map->ops->map_release)
665 map->ops->map_release(map, filp);
667 bpf_map_put_with_uref(map);
671 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
673 fmode_t mode = f.file->f_mode;
675 /* Our file permissions may have been overridden by global
676 * map permissions facing syscall side.
678 if (READ_ONCE(map->frozen))
679 mode &= ~FMODE_CAN_WRITE;
683 #ifdef CONFIG_PROC_FS
684 /* Provides an approximation of the map's memory footprint.
685 * Used only to provide a backward compatibility and display
686 * a reasonable "memlock" info.
688 static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
692 size = round_up(map->key_size + bpf_map_value_size(map), 8);
694 return round_up(map->max_entries * size, PAGE_SIZE);
697 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
699 struct bpf_map *map = filp->private_data;
700 u32 type = 0, jited = 0;
702 if (map_type_contains_progs(map)) {
703 spin_lock(&map->owner.lock);
704 type = map->owner.type;
705 jited = map->owner.jited;
706 spin_unlock(&map->owner.lock);
715 "map_extra:\t%#llx\n"
724 (unsigned long long)map->map_extra,
725 bpf_map_memory_footprint(map),
727 READ_ONCE(map->frozen));
729 seq_printf(m, "owner_prog_type:\t%u\n", type);
730 seq_printf(m, "owner_jited:\t%u\n", jited);
735 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
738 /* We need this handler such that alloc_file() enables
739 * f_mode with FMODE_CAN_READ.
744 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
745 size_t siz, loff_t *ppos)
747 /* We need this handler such that alloc_file() enables
748 * f_mode with FMODE_CAN_WRITE.
753 /* called for any extra memory-mapped regions (except initial) */
754 static void bpf_map_mmap_open(struct vm_area_struct *vma)
756 struct bpf_map *map = vma->vm_file->private_data;
758 if (vma->vm_flags & VM_MAYWRITE)
759 bpf_map_write_active_inc(map);
762 /* called for all unmapped memory region (including initial) */
763 static void bpf_map_mmap_close(struct vm_area_struct *vma)
765 struct bpf_map *map = vma->vm_file->private_data;
767 if (vma->vm_flags & VM_MAYWRITE)
768 bpf_map_write_active_dec(map);
771 static const struct vm_operations_struct bpf_map_default_vmops = {
772 .open = bpf_map_mmap_open,
773 .close = bpf_map_mmap_close,
776 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
778 struct bpf_map *map = filp->private_data;
781 if (!map->ops->map_mmap || map_value_has_spin_lock(map) ||
782 map_value_has_timer(map) || map_value_has_kptrs(map))
785 if (!(vma->vm_flags & VM_SHARED))
788 mutex_lock(&map->freeze_mutex);
790 if (vma->vm_flags & VM_WRITE) {
795 /* map is meant to be read-only, so do not allow mapping as
796 * writable, because it's possible to leak a writable page
797 * reference and allows user-space to still modify it after
798 * freezing, while verifier will assume contents do not change
800 if (map->map_flags & BPF_F_RDONLY_PROG) {
806 /* set default open/close callbacks */
807 vma->vm_ops = &bpf_map_default_vmops;
808 vma->vm_private_data = map;
809 vma->vm_flags &= ~VM_MAYEXEC;
810 if (!(vma->vm_flags & VM_WRITE))
811 /* disallow re-mapping with PROT_WRITE */
812 vma->vm_flags &= ~VM_MAYWRITE;
814 err = map->ops->map_mmap(map, vma);
818 if (vma->vm_flags & VM_MAYWRITE)
819 bpf_map_write_active_inc(map);
821 mutex_unlock(&map->freeze_mutex);
825 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
827 struct bpf_map *map = filp->private_data;
829 if (map->ops->map_poll)
830 return map->ops->map_poll(map, filp, pts);
835 const struct file_operations bpf_map_fops = {
836 #ifdef CONFIG_PROC_FS
837 .show_fdinfo = bpf_map_show_fdinfo,
839 .release = bpf_map_release,
840 .read = bpf_dummy_read,
841 .write = bpf_dummy_write,
842 .mmap = bpf_map_mmap,
843 .poll = bpf_map_poll,
846 int bpf_map_new_fd(struct bpf_map *map, int flags)
850 ret = security_bpf_map(map, OPEN_FMODE(flags));
854 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
858 int bpf_get_file_flag(int flags)
860 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
862 if (flags & BPF_F_RDONLY)
864 if (flags & BPF_F_WRONLY)
869 /* helper macro to check that unused fields 'union bpf_attr' are zero */
870 #define CHECK_ATTR(CMD) \
871 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
872 sizeof(attr->CMD##_LAST_FIELD), 0, \
874 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
875 sizeof(attr->CMD##_LAST_FIELD)) != NULL
877 /* dst and src must have at least "size" number of bytes.
878 * Return strlen on success and < 0 on error.
880 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
882 const char *end = src + size;
883 const char *orig_src = src;
885 memset(dst, 0, size);
886 /* Copy all isalnum(), '_' and '.' chars. */
887 while (src < end && *src) {
888 if (!isalnum(*src) &&
889 *src != '_' && *src != '.')
894 /* No '\0' found in "size" number of bytes */
898 return src - orig_src;
901 int map_check_no_btf(const struct bpf_map *map,
902 const struct btf *btf,
903 const struct btf_type *key_type,
904 const struct btf_type *value_type)
909 static int map_off_arr_cmp(const void *_a, const void *_b, const void *priv)
911 const u32 a = *(const u32 *)_a;
912 const u32 b = *(const u32 *)_b;
921 static void map_off_arr_swap(void *_a, void *_b, int size, const void *priv)
923 struct bpf_map *map = (struct bpf_map *)priv;
924 u32 *off_base = map->off_arr->field_off;
925 u32 *a = _a, *b = _b;
928 sz_a = map->off_arr->field_sz + (a - off_base);
929 sz_b = map->off_arr->field_sz + (b - off_base);
935 static int bpf_map_alloc_off_arr(struct bpf_map *map)
937 bool has_spin_lock = map_value_has_spin_lock(map);
938 bool has_timer = map_value_has_timer(map);
939 bool has_kptrs = map_value_has_kptrs(map);
940 struct bpf_map_off_arr *off_arr;
943 if (!has_spin_lock && !has_timer && !has_kptrs) {
948 off_arr = kmalloc(sizeof(*map->off_arr), GFP_KERNEL | __GFP_NOWARN);
951 map->off_arr = off_arr;
957 off_arr->field_off[i] = map->spin_lock_off;
958 off_arr->field_sz[i] = sizeof(struct bpf_spin_lock);
964 off_arr->field_off[i] = map->timer_off;
965 off_arr->field_sz[i] = sizeof(struct bpf_timer);
969 struct bpf_map_value_off *tab = map->kptr_off_tab;
970 u32 *off = &off_arr->field_off[off_arr->cnt];
971 u8 *sz = &off_arr->field_sz[off_arr->cnt];
973 for (i = 0; i < tab->nr_off; i++) {
974 *off++ = tab->off[i].offset;
977 off_arr->cnt += tab->nr_off;
980 if (off_arr->cnt == 1)
982 sort_r(off_arr->field_off, off_arr->cnt, sizeof(off_arr->field_off[0]),
983 map_off_arr_cmp, map_off_arr_swap, map);
987 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
988 u32 btf_key_id, u32 btf_value_id)
990 const struct btf_type *key_type, *value_type;
991 u32 key_size, value_size;
994 /* Some maps allow key to be unspecified. */
996 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
997 if (!key_type || key_size != map->key_size)
1000 key_type = btf_type_by_id(btf, 0);
1001 if (!map->ops->map_check_btf)
1005 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
1006 if (!value_type || value_size != map->value_size)
1009 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
1011 if (map_value_has_spin_lock(map)) {
1012 if (map->map_flags & BPF_F_RDONLY_PROG)
1014 if (map->map_type != BPF_MAP_TYPE_HASH &&
1015 map->map_type != BPF_MAP_TYPE_ARRAY &&
1016 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
1017 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
1018 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
1019 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
1021 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
1024 "verifier bug spin_lock_off %d value_size %d\n",
1025 map->spin_lock_off, map->value_size);
1030 map->timer_off = btf_find_timer(btf, value_type);
1031 if (map_value_has_timer(map)) {
1032 if (map->map_flags & BPF_F_RDONLY_PROG)
1034 if (map->map_type != BPF_MAP_TYPE_HASH &&
1035 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1036 map->map_type != BPF_MAP_TYPE_ARRAY)
1040 map->kptr_off_tab = btf_parse_kptrs(btf, value_type);
1041 if (map_value_has_kptrs(map)) {
1042 if (!bpf_capable()) {
1046 if (map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) {
1050 if (map->map_type != BPF_MAP_TYPE_HASH &&
1051 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1052 map->map_type != BPF_MAP_TYPE_ARRAY &&
1053 map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY) {
1059 if (map->ops->map_check_btf) {
1060 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
1067 bpf_map_free_kptr_off_tab(map);
1071 #define BPF_MAP_CREATE_LAST_FIELD map_extra
1072 /* called via syscall */
1073 static int map_create(union bpf_attr *attr)
1075 int numa_node = bpf_map_attr_numa_node(attr);
1076 struct bpf_map *map;
1080 err = CHECK_ATTR(BPF_MAP_CREATE);
1084 if (attr->btf_vmlinux_value_type_id) {
1085 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
1086 attr->btf_key_type_id || attr->btf_value_type_id)
1088 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
1092 if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER &&
1093 attr->map_extra != 0)
1096 f_flags = bpf_get_file_flag(attr->map_flags);
1100 if (numa_node != NUMA_NO_NODE &&
1101 ((unsigned int)numa_node >= nr_node_ids ||
1102 !node_online(numa_node)))
1105 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
1106 map = find_and_alloc_map(attr);
1108 return PTR_ERR(map);
1110 err = bpf_obj_name_cpy(map->name, attr->map_name,
1111 sizeof(attr->map_name));
1115 atomic64_set(&map->refcnt, 1);
1116 atomic64_set(&map->usercnt, 1);
1117 mutex_init(&map->freeze_mutex);
1118 spin_lock_init(&map->owner.lock);
1120 map->spin_lock_off = -EINVAL;
1121 map->timer_off = -EINVAL;
1122 if (attr->btf_key_type_id || attr->btf_value_type_id ||
1123 /* Even the map's value is a kernel's struct,
1124 * the bpf_prog.o must have BTF to begin with
1125 * to figure out the corresponding kernel's
1126 * counter part. Thus, attr->btf_fd has
1129 attr->btf_vmlinux_value_type_id) {
1132 btf = btf_get_by_fd(attr->btf_fd);
1137 if (btf_is_kernel(btf)) {
1144 if (attr->btf_value_type_id) {
1145 err = map_check_btf(map, btf, attr->btf_key_type_id,
1146 attr->btf_value_type_id);
1151 map->btf_key_type_id = attr->btf_key_type_id;
1152 map->btf_value_type_id = attr->btf_value_type_id;
1153 map->btf_vmlinux_value_type_id =
1154 attr->btf_vmlinux_value_type_id;
1157 err = bpf_map_alloc_off_arr(map);
1161 err = security_bpf_map_alloc(map);
1163 goto free_map_off_arr;
1165 err = bpf_map_alloc_id(map);
1169 bpf_map_save_memcg(map);
1171 err = bpf_map_new_fd(map, f_flags);
1173 /* failed to allocate fd.
1174 * bpf_map_put_with_uref() is needed because the above
1175 * bpf_map_alloc_id() has published the map
1176 * to the userspace and the userspace may
1177 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
1179 bpf_map_put_with_uref(map);
1186 security_bpf_map_free(map);
1188 kfree(map->off_arr);
1191 map->ops->map_free(map);
1195 /* if error is returned, fd is released.
1196 * On success caller should complete fd access with matching fdput()
1198 struct bpf_map *__bpf_map_get(struct fd f)
1201 return ERR_PTR(-EBADF);
1202 if (f.file->f_op != &bpf_map_fops) {
1204 return ERR_PTR(-EINVAL);
1207 return f.file->private_data;
1210 void bpf_map_inc(struct bpf_map *map)
1212 atomic64_inc(&map->refcnt);
1214 EXPORT_SYMBOL_GPL(bpf_map_inc);
1216 void bpf_map_inc_with_uref(struct bpf_map *map)
1218 atomic64_inc(&map->refcnt);
1219 atomic64_inc(&map->usercnt);
1221 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
1223 struct bpf_map *bpf_map_get(u32 ufd)
1225 struct fd f = fdget(ufd);
1226 struct bpf_map *map;
1228 map = __bpf_map_get(f);
1237 EXPORT_SYMBOL(bpf_map_get);
1239 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
1241 struct fd f = fdget(ufd);
1242 struct bpf_map *map;
1244 map = __bpf_map_get(f);
1248 bpf_map_inc_with_uref(map);
1254 /* map_idr_lock should have been held */
1255 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
1259 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
1261 return ERR_PTR(-ENOENT);
1263 atomic64_inc(&map->usercnt);
1268 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
1270 spin_lock_bh(&map_idr_lock);
1271 map = __bpf_map_inc_not_zero(map, false);
1272 spin_unlock_bh(&map_idr_lock);
1276 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
1278 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1283 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1286 return vmemdup_user(ukey, key_size);
1289 return ERR_PTR(-EINVAL);
1294 static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1297 return kvmemdup_bpfptr(ukey, key_size);
1299 if (!bpfptr_is_null(ukey))
1300 return ERR_PTR(-EINVAL);
1305 /* last field in 'union bpf_attr' used by this command */
1306 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1308 static int map_lookup_elem(union bpf_attr *attr)
1310 void __user *ukey = u64_to_user_ptr(attr->key);
1311 void __user *uvalue = u64_to_user_ptr(attr->value);
1312 int ufd = attr->map_fd;
1313 struct bpf_map *map;
1319 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1322 if (attr->flags & ~BPF_F_LOCK)
1326 map = __bpf_map_get(f);
1328 return PTR_ERR(map);
1329 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1334 if ((attr->flags & BPF_F_LOCK) &&
1335 !map_value_has_spin_lock(map)) {
1340 key = __bpf_copy_key(ukey, map->key_size);
1346 value_size = bpf_map_value_size(map);
1349 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1353 if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
1354 if (copy_from_user(value, uvalue, value_size))
1357 err = bpf_map_copy_value(map, key, value, attr->flags);
1361 err = bpf_map_copy_value(map, key, value, attr->flags);
1366 if (copy_to_user(uvalue, value, value_size) != 0)
1381 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1383 static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
1385 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1386 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
1387 int ufd = attr->map_fd;
1388 struct bpf_map *map;
1394 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1398 map = __bpf_map_get(f);
1400 return PTR_ERR(map);
1401 bpf_map_write_active_inc(map);
1402 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1407 if ((attr->flags & BPF_F_LOCK) &&
1408 !map_value_has_spin_lock(map)) {
1413 key = ___bpf_copy_key(ukey, map->key_size);
1419 value_size = bpf_map_value_size(map);
1420 value = kvmemdup_bpfptr(uvalue, value_size);
1421 if (IS_ERR(value)) {
1422 err = PTR_ERR(value);
1426 err = bpf_map_update_value(map, f, key, value, attr->flags);
1432 bpf_map_write_active_dec(map);
1437 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1439 static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr)
1441 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1442 int ufd = attr->map_fd;
1443 struct bpf_map *map;
1448 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1452 map = __bpf_map_get(f);
1454 return PTR_ERR(map);
1455 bpf_map_write_active_inc(map);
1456 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1461 key = ___bpf_copy_key(ukey, map->key_size);
1467 if (bpf_map_is_dev_bound(map)) {
1468 err = bpf_map_offload_delete_elem(map, key);
1470 } else if (IS_FD_PROG_ARRAY(map) ||
1471 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1472 /* These maps require sleepable context */
1473 err = map->ops->map_delete_elem(map, key);
1477 bpf_disable_instrumentation();
1479 err = map->ops->map_delete_elem(map, key);
1481 bpf_enable_instrumentation();
1482 maybe_wait_bpf_programs(map);
1486 bpf_map_write_active_dec(map);
1491 /* last field in 'union bpf_attr' used by this command */
1492 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1494 static int map_get_next_key(union bpf_attr *attr)
1496 void __user *ukey = u64_to_user_ptr(attr->key);
1497 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1498 int ufd = attr->map_fd;
1499 struct bpf_map *map;
1500 void *key, *next_key;
1504 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1508 map = __bpf_map_get(f);
1510 return PTR_ERR(map);
1511 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1517 key = __bpf_copy_key(ukey, map->key_size);
1527 next_key = kvmalloc(map->key_size, GFP_USER);
1531 if (bpf_map_is_dev_bound(map)) {
1532 err = bpf_map_offload_get_next_key(map, key, next_key);
1537 err = map->ops->map_get_next_key(map, key, next_key);
1544 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1558 int generic_map_delete_batch(struct bpf_map *map,
1559 const union bpf_attr *attr,
1560 union bpf_attr __user *uattr)
1562 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1567 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1570 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1571 !map_value_has_spin_lock(map)) {
1575 max_count = attr->batch.count;
1579 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1583 for (cp = 0; cp < max_count; cp++) {
1585 if (copy_from_user(key, keys + cp * map->key_size,
1589 if (bpf_map_is_dev_bound(map)) {
1590 err = bpf_map_offload_delete_elem(map, key);
1594 bpf_disable_instrumentation();
1596 err = map->ops->map_delete_elem(map, key);
1598 bpf_enable_instrumentation();
1603 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1608 maybe_wait_bpf_programs(map);
1612 int generic_map_update_batch(struct bpf_map *map,
1613 const union bpf_attr *attr,
1614 union bpf_attr __user *uattr)
1616 void __user *values = u64_to_user_ptr(attr->batch.values);
1617 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1618 u32 value_size, cp, max_count;
1619 int ufd = attr->batch.map_fd;
1624 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1627 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1628 !map_value_has_spin_lock(map)) {
1632 value_size = bpf_map_value_size(map);
1634 max_count = attr->batch.count;
1638 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1642 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1648 f = fdget(ufd); /* bpf_map_do_batch() guarantees ufd is valid */
1649 for (cp = 0; cp < max_count; cp++) {
1651 if (copy_from_user(key, keys + cp * map->key_size,
1653 copy_from_user(value, values + cp * value_size, value_size))
1656 err = bpf_map_update_value(map, f, key, value,
1657 attr->batch.elem_flags);
1664 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1673 #define MAP_LOOKUP_RETRIES 3
1675 int generic_map_lookup_batch(struct bpf_map *map,
1676 const union bpf_attr *attr,
1677 union bpf_attr __user *uattr)
1679 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1680 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1681 void __user *values = u64_to_user_ptr(attr->batch.values);
1682 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1683 void *buf, *buf_prevkey, *prev_key, *key, *value;
1684 int err, retry = MAP_LOOKUP_RETRIES;
1685 u32 value_size, cp, max_count;
1687 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1690 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1691 !map_value_has_spin_lock(map))
1694 value_size = bpf_map_value_size(map);
1696 max_count = attr->batch.count;
1700 if (put_user(0, &uattr->batch.count))
1703 buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1707 buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1709 kvfree(buf_prevkey);
1715 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1718 value = key + map->key_size;
1720 prev_key = buf_prevkey;
1722 for (cp = 0; cp < max_count;) {
1724 err = map->ops->map_get_next_key(map, prev_key, key);
1728 err = bpf_map_copy_value(map, key, value,
1729 attr->batch.elem_flags);
1731 if (err == -ENOENT) {
1743 if (copy_to_user(keys + cp * map->key_size, key,
1748 if (copy_to_user(values + cp * value_size, value, value_size)) {
1754 prev_key = buf_prevkey;
1756 swap(prev_key, key);
1757 retry = MAP_LOOKUP_RETRIES;
1765 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1766 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1770 kvfree(buf_prevkey);
1775 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
1777 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1779 void __user *ukey = u64_to_user_ptr(attr->key);
1780 void __user *uvalue = u64_to_user_ptr(attr->value);
1781 int ufd = attr->map_fd;
1782 struct bpf_map *map;
1788 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1791 if (attr->flags & ~BPF_F_LOCK)
1795 map = __bpf_map_get(f);
1797 return PTR_ERR(map);
1798 bpf_map_write_active_inc(map);
1799 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1800 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1806 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1807 map->map_type == BPF_MAP_TYPE_STACK)) {
1812 if ((attr->flags & BPF_F_LOCK) &&
1813 !map_value_has_spin_lock(map)) {
1818 key = __bpf_copy_key(ukey, map->key_size);
1824 value_size = bpf_map_value_size(map);
1827 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1832 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1833 map->map_type == BPF_MAP_TYPE_STACK) {
1834 err = map->ops->map_pop_elem(map, value);
1835 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1836 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1837 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1838 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1839 if (!bpf_map_is_dev_bound(map)) {
1840 bpf_disable_instrumentation();
1842 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1844 bpf_enable_instrumentation();
1851 if (copy_to_user(uvalue, value, value_size) != 0) {
1863 bpf_map_write_active_dec(map);
1868 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1870 static int map_freeze(const union bpf_attr *attr)
1872 int err = 0, ufd = attr->map_fd;
1873 struct bpf_map *map;
1876 if (CHECK_ATTR(BPF_MAP_FREEZE))
1880 map = __bpf_map_get(f);
1882 return PTR_ERR(map);
1884 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS ||
1885 map_value_has_timer(map) || map_value_has_kptrs(map)) {
1890 mutex_lock(&map->freeze_mutex);
1891 if (bpf_map_write_active(map)) {
1895 if (READ_ONCE(map->frozen)) {
1899 if (!bpf_capable()) {
1904 WRITE_ONCE(map->frozen, true);
1906 mutex_unlock(&map->freeze_mutex);
1911 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1912 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1913 [_id] = & _name ## _prog_ops,
1914 #define BPF_MAP_TYPE(_id, _ops)
1915 #define BPF_LINK_TYPE(_id, _name)
1916 #include <linux/bpf_types.h>
1917 #undef BPF_PROG_TYPE
1919 #undef BPF_LINK_TYPE
1922 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1924 const struct bpf_prog_ops *ops;
1926 if (type >= ARRAY_SIZE(bpf_prog_types))
1928 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1929 ops = bpf_prog_types[type];
1933 if (!bpf_prog_is_dev_bound(prog->aux))
1934 prog->aux->ops = ops;
1936 prog->aux->ops = &bpf_offload_prog_ops;
1947 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1948 [BPF_AUDIT_LOAD] = "LOAD",
1949 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1952 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1954 struct audit_context *ctx = NULL;
1955 struct audit_buffer *ab;
1957 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1959 if (audit_enabled == AUDIT_OFF)
1961 if (op == BPF_AUDIT_LOAD)
1962 ctx = audit_context();
1963 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1966 audit_log_format(ab, "prog-id=%u op=%s",
1967 prog->aux->id, bpf_audit_str[op]);
1971 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1975 idr_preload(GFP_KERNEL);
1976 spin_lock_bh(&prog_idr_lock);
1977 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1980 spin_unlock_bh(&prog_idr_lock);
1983 /* id is in [1, INT_MAX) */
1984 if (WARN_ON_ONCE(!id))
1987 return id > 0 ? 0 : id;
1990 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1992 unsigned long flags;
1994 /* cBPF to eBPF migrations are currently not in the idr store.
1995 * Offloaded programs are removed from the store when their device
1996 * disappears - even if someone grabs an fd to them they are unusable,
1997 * simply waiting for refcnt to drop to be freed.
2003 spin_lock_irqsave(&prog_idr_lock, flags);
2005 __acquire(&prog_idr_lock);
2007 idr_remove(&prog_idr, prog->aux->id);
2011 spin_unlock_irqrestore(&prog_idr_lock, flags);
2013 __release(&prog_idr_lock);
2016 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
2018 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
2020 kvfree(aux->func_info);
2021 kfree(aux->func_info_aux);
2022 free_uid(aux->user);
2023 security_bpf_prog_free(aux);
2024 bpf_prog_free(aux->prog);
2027 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
2029 bpf_prog_kallsyms_del_all(prog);
2030 btf_put(prog->aux->btf);
2031 kvfree(prog->aux->jited_linfo);
2032 kvfree(prog->aux->linfo);
2033 kfree(prog->aux->kfunc_tab);
2034 if (prog->aux->attach_btf)
2035 btf_put(prog->aux->attach_btf);
2038 if (prog->aux->sleepable)
2039 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
2041 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
2043 __bpf_prog_put_rcu(&prog->aux->rcu);
2047 static void bpf_prog_put_deferred(struct work_struct *work)
2049 struct bpf_prog_aux *aux;
2050 struct bpf_prog *prog;
2052 aux = container_of(work, struct bpf_prog_aux, work);
2054 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
2055 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
2056 __bpf_prog_put_noref(prog, true);
2059 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
2061 struct bpf_prog_aux *aux = prog->aux;
2063 if (atomic64_dec_and_test(&aux->refcnt)) {
2064 /* bpf_prog_free_id() must be called first */
2065 bpf_prog_free_id(prog, do_idr_lock);
2067 if (in_irq() || irqs_disabled()) {
2068 INIT_WORK(&aux->work, bpf_prog_put_deferred);
2069 schedule_work(&aux->work);
2071 bpf_prog_put_deferred(&aux->work);
2076 void bpf_prog_put(struct bpf_prog *prog)
2078 __bpf_prog_put(prog, true);
2080 EXPORT_SYMBOL_GPL(bpf_prog_put);
2082 static int bpf_prog_release(struct inode *inode, struct file *filp)
2084 struct bpf_prog *prog = filp->private_data;
2090 struct bpf_prog_kstats {
2096 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog)
2098 struct bpf_prog_stats *stats;
2101 stats = this_cpu_ptr(prog->stats);
2102 flags = u64_stats_update_begin_irqsave(&stats->syncp);
2103 u64_stats_inc(&stats->misses);
2104 u64_stats_update_end_irqrestore(&stats->syncp, flags);
2107 static void bpf_prog_get_stats(const struct bpf_prog *prog,
2108 struct bpf_prog_kstats *stats)
2110 u64 nsecs = 0, cnt = 0, misses = 0;
2113 for_each_possible_cpu(cpu) {
2114 const struct bpf_prog_stats *st;
2116 u64 tnsecs, tcnt, tmisses;
2118 st = per_cpu_ptr(prog->stats, cpu);
2120 start = u64_stats_fetch_begin_irq(&st->syncp);
2121 tnsecs = u64_stats_read(&st->nsecs);
2122 tcnt = u64_stats_read(&st->cnt);
2123 tmisses = u64_stats_read(&st->misses);
2124 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
2129 stats->nsecs = nsecs;
2131 stats->misses = misses;
2134 #ifdef CONFIG_PROC_FS
2135 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
2137 const struct bpf_prog *prog = filp->private_data;
2138 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2139 struct bpf_prog_kstats stats;
2141 bpf_prog_get_stats(prog, &stats);
2142 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2149 "run_time_ns:\t%llu\n"
2151 "recursion_misses:\t%llu\n"
2152 "verified_insns:\t%u\n",
2156 prog->pages * 1ULL << PAGE_SHIFT,
2161 prog->aux->verified_insns);
2165 const struct file_operations bpf_prog_fops = {
2166 #ifdef CONFIG_PROC_FS
2167 .show_fdinfo = bpf_prog_show_fdinfo,
2169 .release = bpf_prog_release,
2170 .read = bpf_dummy_read,
2171 .write = bpf_dummy_write,
2174 int bpf_prog_new_fd(struct bpf_prog *prog)
2178 ret = security_bpf_prog(prog);
2182 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
2183 O_RDWR | O_CLOEXEC);
2186 static struct bpf_prog *____bpf_prog_get(struct fd f)
2189 return ERR_PTR(-EBADF);
2190 if (f.file->f_op != &bpf_prog_fops) {
2192 return ERR_PTR(-EINVAL);
2195 return f.file->private_data;
2198 void bpf_prog_add(struct bpf_prog *prog, int i)
2200 atomic64_add(i, &prog->aux->refcnt);
2202 EXPORT_SYMBOL_GPL(bpf_prog_add);
2204 void bpf_prog_sub(struct bpf_prog *prog, int i)
2206 /* Only to be used for undoing previous bpf_prog_add() in some
2207 * error path. We still know that another entity in our call
2208 * path holds a reference to the program, thus atomic_sub() can
2209 * be safely used in such cases!
2211 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
2213 EXPORT_SYMBOL_GPL(bpf_prog_sub);
2215 void bpf_prog_inc(struct bpf_prog *prog)
2217 atomic64_inc(&prog->aux->refcnt);
2219 EXPORT_SYMBOL_GPL(bpf_prog_inc);
2221 /* prog_idr_lock should have been held */
2222 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
2226 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
2229 return ERR_PTR(-ENOENT);
2233 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
2235 bool bpf_prog_get_ok(struct bpf_prog *prog,
2236 enum bpf_prog_type *attach_type, bool attach_drv)
2238 /* not an attachment, just a refcount inc, always allow */
2242 if (prog->type != *attach_type)
2244 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
2250 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
2253 struct fd f = fdget(ufd);
2254 struct bpf_prog *prog;
2256 prog = ____bpf_prog_get(f);
2259 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
2260 prog = ERR_PTR(-EINVAL);
2270 struct bpf_prog *bpf_prog_get(u32 ufd)
2272 return __bpf_prog_get(ufd, NULL, false);
2275 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
2278 return __bpf_prog_get(ufd, &type, attach_drv);
2280 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
2282 /* Initially all BPF programs could be loaded w/o specifying
2283 * expected_attach_type. Later for some of them specifying expected_attach_type
2284 * at load time became required so that program could be validated properly.
2285 * Programs of types that are allowed to be loaded both w/ and w/o (for
2286 * backward compatibility) expected_attach_type, should have the default attach
2287 * type assigned to expected_attach_type for the latter case, so that it can be
2288 * validated later at attach time.
2290 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
2291 * prog type requires it but has some attach types that have to be backward
2294 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
2296 switch (attr->prog_type) {
2297 case BPF_PROG_TYPE_CGROUP_SOCK:
2298 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
2299 * exist so checking for non-zero is the way to go here.
2301 if (!attr->expected_attach_type)
2302 attr->expected_attach_type =
2303 BPF_CGROUP_INET_SOCK_CREATE;
2305 case BPF_PROG_TYPE_SK_REUSEPORT:
2306 if (!attr->expected_attach_type)
2307 attr->expected_attach_type =
2308 BPF_SK_REUSEPORT_SELECT;
2314 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2315 enum bpf_attach_type expected_attach_type,
2316 struct btf *attach_btf, u32 btf_id,
2317 struct bpf_prog *dst_prog)
2320 if (btf_id > BTF_MAX_TYPE)
2323 if (!attach_btf && !dst_prog)
2326 switch (prog_type) {
2327 case BPF_PROG_TYPE_TRACING:
2328 case BPF_PROG_TYPE_LSM:
2329 case BPF_PROG_TYPE_STRUCT_OPS:
2330 case BPF_PROG_TYPE_EXT:
2337 if (attach_btf && (!btf_id || dst_prog))
2340 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
2341 prog_type != BPF_PROG_TYPE_EXT)
2344 switch (prog_type) {
2345 case BPF_PROG_TYPE_CGROUP_SOCK:
2346 switch (expected_attach_type) {
2347 case BPF_CGROUP_INET_SOCK_CREATE:
2348 case BPF_CGROUP_INET_SOCK_RELEASE:
2349 case BPF_CGROUP_INET4_POST_BIND:
2350 case BPF_CGROUP_INET6_POST_BIND:
2355 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2356 switch (expected_attach_type) {
2357 case BPF_CGROUP_INET4_BIND:
2358 case BPF_CGROUP_INET6_BIND:
2359 case BPF_CGROUP_INET4_CONNECT:
2360 case BPF_CGROUP_INET6_CONNECT:
2361 case BPF_CGROUP_INET4_GETPEERNAME:
2362 case BPF_CGROUP_INET6_GETPEERNAME:
2363 case BPF_CGROUP_INET4_GETSOCKNAME:
2364 case BPF_CGROUP_INET6_GETSOCKNAME:
2365 case BPF_CGROUP_UDP4_SENDMSG:
2366 case BPF_CGROUP_UDP6_SENDMSG:
2367 case BPF_CGROUP_UDP4_RECVMSG:
2368 case BPF_CGROUP_UDP6_RECVMSG:
2373 case BPF_PROG_TYPE_CGROUP_SKB:
2374 switch (expected_attach_type) {
2375 case BPF_CGROUP_INET_INGRESS:
2376 case BPF_CGROUP_INET_EGRESS:
2381 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2382 switch (expected_attach_type) {
2383 case BPF_CGROUP_SETSOCKOPT:
2384 case BPF_CGROUP_GETSOCKOPT:
2389 case BPF_PROG_TYPE_SK_LOOKUP:
2390 if (expected_attach_type == BPF_SK_LOOKUP)
2393 case BPF_PROG_TYPE_SK_REUSEPORT:
2394 switch (expected_attach_type) {
2395 case BPF_SK_REUSEPORT_SELECT:
2396 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2401 case BPF_PROG_TYPE_SYSCALL:
2402 case BPF_PROG_TYPE_EXT:
2403 if (expected_attach_type)
2411 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2413 switch (prog_type) {
2414 case BPF_PROG_TYPE_SCHED_CLS:
2415 case BPF_PROG_TYPE_SCHED_ACT:
2416 case BPF_PROG_TYPE_XDP:
2417 case BPF_PROG_TYPE_LWT_IN:
2418 case BPF_PROG_TYPE_LWT_OUT:
2419 case BPF_PROG_TYPE_LWT_XMIT:
2420 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2421 case BPF_PROG_TYPE_SK_SKB:
2422 case BPF_PROG_TYPE_SK_MSG:
2423 case BPF_PROG_TYPE_LIRC_MODE2:
2424 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2425 case BPF_PROG_TYPE_CGROUP_DEVICE:
2426 case BPF_PROG_TYPE_CGROUP_SOCK:
2427 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2428 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2429 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2430 case BPF_PROG_TYPE_SOCK_OPS:
2431 case BPF_PROG_TYPE_EXT: /* extends any prog */
2433 case BPF_PROG_TYPE_CGROUP_SKB:
2435 case BPF_PROG_TYPE_SK_REUSEPORT:
2436 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2442 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2444 switch (prog_type) {
2445 case BPF_PROG_TYPE_KPROBE:
2446 case BPF_PROG_TYPE_TRACEPOINT:
2447 case BPF_PROG_TYPE_PERF_EVENT:
2448 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2449 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2450 case BPF_PROG_TYPE_TRACING:
2451 case BPF_PROG_TYPE_LSM:
2452 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2453 case BPF_PROG_TYPE_EXT: /* extends any prog */
2460 /* last field in 'union bpf_attr' used by this command */
2461 #define BPF_PROG_LOAD_LAST_FIELD core_relo_rec_size
2463 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
2465 enum bpf_prog_type type = attr->prog_type;
2466 struct bpf_prog *prog, *dst_prog = NULL;
2467 struct btf *attach_btf = NULL;
2472 if (CHECK_ATTR(BPF_PROG_LOAD))
2475 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2476 BPF_F_ANY_ALIGNMENT |
2477 BPF_F_TEST_STATE_FREQ |
2479 BPF_F_TEST_RND_HI32 |
2480 BPF_F_XDP_HAS_FRAGS))
2483 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2484 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2488 /* copy eBPF program license from user space */
2489 if (strncpy_from_bpfptr(license,
2490 make_bpfptr(attr->license, uattr.is_kernel),
2491 sizeof(license) - 1) < 0)
2493 license[sizeof(license) - 1] = 0;
2495 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2496 is_gpl = license_is_gpl_compatible(license);
2498 if (attr->insn_cnt == 0 ||
2499 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2501 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2502 type != BPF_PROG_TYPE_CGROUP_SKB &&
2506 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2508 if (is_perfmon_prog_type(type) && !perfmon_capable())
2511 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2512 * or btf, we need to check which one it is
2514 if (attr->attach_prog_fd) {
2515 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2516 if (IS_ERR(dst_prog)) {
2518 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2519 if (IS_ERR(attach_btf))
2521 if (!btf_is_kernel(attach_btf)) {
2522 /* attaching through specifying bpf_prog's BTF
2523 * objects directly might be supported eventually
2525 btf_put(attach_btf);
2529 } else if (attr->attach_btf_id) {
2530 /* fall back to vmlinux BTF, if BTF type ID is specified */
2531 attach_btf = bpf_get_btf_vmlinux();
2532 if (IS_ERR(attach_btf))
2533 return PTR_ERR(attach_btf);
2536 btf_get(attach_btf);
2539 bpf_prog_load_fixup_attach_type(attr);
2540 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2541 attach_btf, attr->attach_btf_id,
2544 bpf_prog_put(dst_prog);
2546 btf_put(attach_btf);
2550 /* plain bpf_prog allocation */
2551 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2554 bpf_prog_put(dst_prog);
2556 btf_put(attach_btf);
2560 prog->expected_attach_type = attr->expected_attach_type;
2561 prog->aux->attach_btf = attach_btf;
2562 prog->aux->attach_btf_id = attr->attach_btf_id;
2563 prog->aux->dst_prog = dst_prog;
2564 prog->aux->offload_requested = !!attr->prog_ifindex;
2565 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2566 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
2568 err = security_bpf_prog_alloc(prog->aux);
2572 prog->aux->user = get_current_user();
2573 prog->len = attr->insn_cnt;
2576 if (copy_from_bpfptr(prog->insns,
2577 make_bpfptr(attr->insns, uattr.is_kernel),
2578 bpf_prog_insn_size(prog)) != 0)
2581 prog->orig_prog = NULL;
2584 atomic64_set(&prog->aux->refcnt, 1);
2585 prog->gpl_compatible = is_gpl ? 1 : 0;
2587 if (bpf_prog_is_dev_bound(prog->aux)) {
2588 err = bpf_prog_offload_init(prog, attr);
2593 /* find program type: socket_filter vs tracing_filter */
2594 err = find_prog_type(type, prog);
2598 prog->aux->load_time = ktime_get_boottime_ns();
2599 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2600 sizeof(attr->prog_name));
2604 /* run eBPF verifier */
2605 err = bpf_check(&prog, attr, uattr);
2607 goto free_used_maps;
2609 prog = bpf_prog_select_runtime(prog, &err);
2611 goto free_used_maps;
2613 err = bpf_prog_alloc_id(prog);
2615 goto free_used_maps;
2617 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2618 * effectively publicly exposed. However, retrieving via
2619 * bpf_prog_get_fd_by_id() will take another reference,
2620 * therefore it cannot be gone underneath us.
2622 * Only for the time /after/ successful bpf_prog_new_fd()
2623 * and before returning to userspace, we might just hold
2624 * one reference and any parallel close on that fd could
2625 * rip everything out. Hence, below notifications must
2626 * happen before bpf_prog_new_fd().
2628 * Also, any failure handling from this point onwards must
2629 * be using bpf_prog_put() given the program is exposed.
2631 bpf_prog_kallsyms_add(prog);
2632 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2633 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2635 err = bpf_prog_new_fd(prog);
2641 /* In case we have subprogs, we need to wait for a grace
2642 * period before we can tear down JIT memory since symbols
2643 * are already exposed under kallsyms.
2645 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2648 free_uid(prog->aux->user);
2649 security_bpf_prog_free(prog->aux);
2651 if (prog->aux->attach_btf)
2652 btf_put(prog->aux->attach_btf);
2653 bpf_prog_free(prog);
2657 #define BPF_OBJ_LAST_FIELD file_flags
2659 static int bpf_obj_pin(const union bpf_attr *attr)
2661 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2664 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2667 static int bpf_obj_get(const union bpf_attr *attr)
2669 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2670 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2673 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2677 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2678 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2680 atomic64_set(&link->refcnt, 1);
2687 static void bpf_link_free_id(int id)
2692 spin_lock_bh(&link_idr_lock);
2693 idr_remove(&link_idr, id);
2694 spin_unlock_bh(&link_idr_lock);
2697 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2698 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2699 * anon_inode's release() call. This helper marksbpf_link as
2700 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2701 * is not decremented, it's the responsibility of a calling code that failed
2702 * to complete bpf_link initialization.
2704 void bpf_link_cleanup(struct bpf_link_primer *primer)
2706 primer->link->prog = NULL;
2707 bpf_link_free_id(primer->id);
2709 put_unused_fd(primer->fd);
2712 void bpf_link_inc(struct bpf_link *link)
2714 atomic64_inc(&link->refcnt);
2717 /* bpf_link_free is guaranteed to be called from process context */
2718 static void bpf_link_free(struct bpf_link *link)
2720 bpf_link_free_id(link->id);
2722 /* detach BPF program, clean up used resources */
2723 link->ops->release(link);
2724 bpf_prog_put(link->prog);
2726 /* free bpf_link and its containing memory */
2727 link->ops->dealloc(link);
2730 static void bpf_link_put_deferred(struct work_struct *work)
2732 struct bpf_link *link = container_of(work, struct bpf_link, work);
2734 bpf_link_free(link);
2737 /* bpf_link_put can be called from atomic context, but ensures that resources
2738 * are freed from process context
2740 void bpf_link_put(struct bpf_link *link)
2742 if (!atomic64_dec_and_test(&link->refcnt))
2746 INIT_WORK(&link->work, bpf_link_put_deferred);
2747 schedule_work(&link->work);
2749 bpf_link_free(link);
2752 EXPORT_SYMBOL(bpf_link_put);
2754 static int bpf_link_release(struct inode *inode, struct file *filp)
2756 struct bpf_link *link = filp->private_data;
2762 #ifdef CONFIG_PROC_FS
2763 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2764 #define BPF_MAP_TYPE(_id, _ops)
2765 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2766 static const char *bpf_link_type_strs[] = {
2767 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2768 #include <linux/bpf_types.h>
2770 #undef BPF_PROG_TYPE
2772 #undef BPF_LINK_TYPE
2774 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2776 const struct bpf_link *link = filp->private_data;
2777 const struct bpf_prog *prog = link->prog;
2778 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2780 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2786 bpf_link_type_strs[link->type],
2790 if (link->ops->show_fdinfo)
2791 link->ops->show_fdinfo(link, m);
2795 static const struct file_operations bpf_link_fops = {
2796 #ifdef CONFIG_PROC_FS
2797 .show_fdinfo = bpf_link_show_fdinfo,
2799 .release = bpf_link_release,
2800 .read = bpf_dummy_read,
2801 .write = bpf_dummy_write,
2804 static int bpf_link_alloc_id(struct bpf_link *link)
2808 idr_preload(GFP_KERNEL);
2809 spin_lock_bh(&link_idr_lock);
2810 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2811 spin_unlock_bh(&link_idr_lock);
2817 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2818 * reserving unused FD and allocating ID from link_idr. This is to be paired
2819 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2820 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2821 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2822 * transient state is passed around in struct bpf_link_primer.
2823 * This is preferred way to create and initialize bpf_link, especially when
2824 * there are complicated and expensive operations in between creating bpf_link
2825 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2826 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2827 * expensive (and potentially failing) roll back operations in a rare case
2828 * that file, FD, or ID can't be allocated.
2830 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2835 fd = get_unused_fd_flags(O_CLOEXEC);
2840 id = bpf_link_alloc_id(link);
2846 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2848 bpf_link_free_id(id);
2850 return PTR_ERR(file);
2853 primer->link = link;
2854 primer->file = file;
2860 int bpf_link_settle(struct bpf_link_primer *primer)
2862 /* make bpf_link fetchable by ID */
2863 spin_lock_bh(&link_idr_lock);
2864 primer->link->id = primer->id;
2865 spin_unlock_bh(&link_idr_lock);
2866 /* make bpf_link fetchable by FD */
2867 fd_install(primer->fd, primer->file);
2868 /* pass through installed FD */
2872 int bpf_link_new_fd(struct bpf_link *link)
2874 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2877 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2879 struct fd f = fdget(ufd);
2880 struct bpf_link *link;
2883 return ERR_PTR(-EBADF);
2884 if (f.file->f_op != &bpf_link_fops) {
2886 return ERR_PTR(-EINVAL);
2889 link = f.file->private_data;
2895 EXPORT_SYMBOL(bpf_link_get_from_fd);
2897 static void bpf_tracing_link_release(struct bpf_link *link)
2899 struct bpf_tracing_link *tr_link =
2900 container_of(link, struct bpf_tracing_link, link.link);
2902 WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link,
2903 tr_link->trampoline));
2905 bpf_trampoline_put(tr_link->trampoline);
2907 /* tgt_prog is NULL if target is a kernel function */
2908 if (tr_link->tgt_prog)
2909 bpf_prog_put(tr_link->tgt_prog);
2912 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2914 struct bpf_tracing_link *tr_link =
2915 container_of(link, struct bpf_tracing_link, link.link);
2920 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2921 struct seq_file *seq)
2923 struct bpf_tracing_link *tr_link =
2924 container_of(link, struct bpf_tracing_link, link.link);
2927 "attach_type:\t%d\n",
2928 tr_link->attach_type);
2931 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2932 struct bpf_link_info *info)
2934 struct bpf_tracing_link *tr_link =
2935 container_of(link, struct bpf_tracing_link, link.link);
2937 info->tracing.attach_type = tr_link->attach_type;
2938 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2939 &info->tracing.target_obj_id,
2940 &info->tracing.target_btf_id);
2945 static const struct bpf_link_ops bpf_tracing_link_lops = {
2946 .release = bpf_tracing_link_release,
2947 .dealloc = bpf_tracing_link_dealloc,
2948 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2949 .fill_link_info = bpf_tracing_link_fill_link_info,
2952 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2957 struct bpf_link_primer link_primer;
2958 struct bpf_prog *tgt_prog = NULL;
2959 struct bpf_trampoline *tr = NULL;
2960 struct bpf_tracing_link *link;
2964 switch (prog->type) {
2965 case BPF_PROG_TYPE_TRACING:
2966 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2967 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2968 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2973 case BPF_PROG_TYPE_EXT:
2974 if (prog->expected_attach_type != 0) {
2979 case BPF_PROG_TYPE_LSM:
2980 if (prog->expected_attach_type != BPF_LSM_MAC) {
2990 if (!!tgt_prog_fd != !!btf_id) {
2996 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2997 if (prog->type != BPF_PROG_TYPE_EXT) {
3002 tgt_prog = bpf_prog_get(tgt_prog_fd);
3003 if (IS_ERR(tgt_prog)) {
3004 err = PTR_ERR(tgt_prog);
3009 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
3012 link = kzalloc(sizeof(*link), GFP_USER);
3017 bpf_link_init(&link->link.link, BPF_LINK_TYPE_TRACING,
3018 &bpf_tracing_link_lops, prog);
3019 link->attach_type = prog->expected_attach_type;
3020 link->link.cookie = bpf_cookie;
3022 mutex_lock(&prog->aux->dst_mutex);
3024 /* There are a few possible cases here:
3026 * - if prog->aux->dst_trampoline is set, the program was just loaded
3027 * and not yet attached to anything, so we can use the values stored
3030 * - if prog->aux->dst_trampoline is NULL, the program has already been
3031 * attached to a target and its initial target was cleared (below)
3033 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
3034 * target_btf_id using the link_create API.
3036 * - if tgt_prog == NULL when this function was called using the old
3037 * raw_tracepoint_open API, and we need a target from prog->aux
3039 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
3040 * was detached and is going for re-attachment.
3042 if (!prog->aux->dst_trampoline && !tgt_prog) {
3044 * Allow re-attach for TRACING and LSM programs. If it's
3045 * currently linked, bpf_trampoline_link_prog will fail.
3046 * EXT programs need to specify tgt_prog_fd, so they
3047 * re-attach in separate code path.
3049 if (prog->type != BPF_PROG_TYPE_TRACING &&
3050 prog->type != BPF_PROG_TYPE_LSM) {
3054 btf_id = prog->aux->attach_btf_id;
3055 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
3058 if (!prog->aux->dst_trampoline ||
3059 (key && key != prog->aux->dst_trampoline->key)) {
3060 /* If there is no saved target, or the specified target is
3061 * different from the destination specified at load time, we
3062 * need a new trampoline and a check for compatibility
3064 struct bpf_attach_target_info tgt_info = {};
3066 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
3071 tr = bpf_trampoline_get(key, &tgt_info);
3077 /* The caller didn't specify a target, or the target was the
3078 * same as the destination supplied during program load. This
3079 * means we can reuse the trampoline and reference from program
3080 * load time, and there is no need to allocate a new one. This
3081 * can only happen once for any program, as the saved values in
3082 * prog->aux are cleared below.
3084 tr = prog->aux->dst_trampoline;
3085 tgt_prog = prog->aux->dst_prog;
3088 err = bpf_link_prime(&link->link.link, &link_primer);
3092 err = bpf_trampoline_link_prog(&link->link, tr);
3094 bpf_link_cleanup(&link_primer);
3099 link->tgt_prog = tgt_prog;
3100 link->trampoline = tr;
3102 /* Always clear the trampoline and target prog from prog->aux to make
3103 * sure the original attach destination is not kept alive after a
3104 * program is (re-)attached to another target.
3106 if (prog->aux->dst_prog &&
3107 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
3108 /* got extra prog ref from syscall, or attaching to different prog */
3109 bpf_prog_put(prog->aux->dst_prog);
3110 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
3111 /* we allocated a new trampoline, so free the old one */
3112 bpf_trampoline_put(prog->aux->dst_trampoline);
3114 prog->aux->dst_prog = NULL;
3115 prog->aux->dst_trampoline = NULL;
3116 mutex_unlock(&prog->aux->dst_mutex);
3118 return bpf_link_settle(&link_primer);
3120 if (tr && tr != prog->aux->dst_trampoline)
3121 bpf_trampoline_put(tr);
3122 mutex_unlock(&prog->aux->dst_mutex);
3125 if (tgt_prog_fd && tgt_prog)
3126 bpf_prog_put(tgt_prog);
3130 struct bpf_raw_tp_link {
3131 struct bpf_link link;
3132 struct bpf_raw_event_map *btp;
3135 static void bpf_raw_tp_link_release(struct bpf_link *link)
3137 struct bpf_raw_tp_link *raw_tp =
3138 container_of(link, struct bpf_raw_tp_link, link);
3140 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
3141 bpf_put_raw_tracepoint(raw_tp->btp);
3144 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
3146 struct bpf_raw_tp_link *raw_tp =
3147 container_of(link, struct bpf_raw_tp_link, link);
3152 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
3153 struct seq_file *seq)
3155 struct bpf_raw_tp_link *raw_tp_link =
3156 container_of(link, struct bpf_raw_tp_link, link);
3160 raw_tp_link->btp->tp->name);
3163 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
3164 struct bpf_link_info *info)
3166 struct bpf_raw_tp_link *raw_tp_link =
3167 container_of(link, struct bpf_raw_tp_link, link);
3168 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
3169 const char *tp_name = raw_tp_link->btp->tp->name;
3170 u32 ulen = info->raw_tracepoint.tp_name_len;
3171 size_t tp_len = strlen(tp_name);
3176 info->raw_tracepoint.tp_name_len = tp_len + 1;
3181 if (ulen >= tp_len + 1) {
3182 if (copy_to_user(ubuf, tp_name, tp_len + 1))
3187 if (copy_to_user(ubuf, tp_name, ulen - 1))
3189 if (put_user(zero, ubuf + ulen - 1))
3197 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
3198 .release = bpf_raw_tp_link_release,
3199 .dealloc = bpf_raw_tp_link_dealloc,
3200 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
3201 .fill_link_info = bpf_raw_tp_link_fill_link_info,
3204 #ifdef CONFIG_PERF_EVENTS
3205 struct bpf_perf_link {
3206 struct bpf_link link;
3207 struct file *perf_file;
3210 static void bpf_perf_link_release(struct bpf_link *link)
3212 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3213 struct perf_event *event = perf_link->perf_file->private_data;
3215 perf_event_free_bpf_prog(event);
3216 fput(perf_link->perf_file);
3219 static void bpf_perf_link_dealloc(struct bpf_link *link)
3221 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3226 static const struct bpf_link_ops bpf_perf_link_lops = {
3227 .release = bpf_perf_link_release,
3228 .dealloc = bpf_perf_link_dealloc,
3231 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3233 struct bpf_link_primer link_primer;
3234 struct bpf_perf_link *link;
3235 struct perf_event *event;
3236 struct file *perf_file;
3239 if (attr->link_create.flags)
3242 perf_file = perf_event_get(attr->link_create.target_fd);
3243 if (IS_ERR(perf_file))
3244 return PTR_ERR(perf_file);
3246 link = kzalloc(sizeof(*link), GFP_USER);
3251 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog);
3252 link->perf_file = perf_file;
3254 err = bpf_link_prime(&link->link, &link_primer);
3260 event = perf_file->private_data;
3261 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
3263 bpf_link_cleanup(&link_primer);
3266 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
3269 return bpf_link_settle(&link_primer);
3276 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3280 #endif /* CONFIG_PERF_EVENTS */
3282 static int bpf_raw_tp_link_attach(struct bpf_prog *prog,
3283 const char __user *user_tp_name)
3285 struct bpf_link_primer link_primer;
3286 struct bpf_raw_tp_link *link;
3287 struct bpf_raw_event_map *btp;
3288 const char *tp_name;
3292 switch (prog->type) {
3293 case BPF_PROG_TYPE_TRACING:
3294 case BPF_PROG_TYPE_EXT:
3295 case BPF_PROG_TYPE_LSM:
3297 /* The attach point for this category of programs
3298 * should be specified via btf_id during program load.
3301 if (prog->type == BPF_PROG_TYPE_TRACING &&
3302 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
3303 tp_name = prog->aux->attach_func_name;
3306 return bpf_tracing_prog_attach(prog, 0, 0, 0);
3307 case BPF_PROG_TYPE_RAW_TRACEPOINT:
3308 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
3309 if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0)
3311 buf[sizeof(buf) - 1] = 0;
3318 btp = bpf_get_raw_tracepoint(tp_name);
3322 link = kzalloc(sizeof(*link), GFP_USER);
3327 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
3328 &bpf_raw_tp_link_lops, prog);
3331 err = bpf_link_prime(&link->link, &link_primer);
3337 err = bpf_probe_register(link->btp, prog);
3339 bpf_link_cleanup(&link_primer);
3343 return bpf_link_settle(&link_primer);
3346 bpf_put_raw_tracepoint(btp);
3350 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
3352 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
3354 struct bpf_prog *prog;
3357 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
3360 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
3362 return PTR_ERR(prog);
3364 fd = bpf_raw_tp_link_attach(prog, u64_to_user_ptr(attr->raw_tracepoint.name));
3370 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
3371 enum bpf_attach_type attach_type)
3373 switch (prog->type) {
3374 case BPF_PROG_TYPE_CGROUP_SOCK:
3375 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3376 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3377 case BPF_PROG_TYPE_SK_LOOKUP:
3378 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
3379 case BPF_PROG_TYPE_CGROUP_SKB:
3380 if (!capable(CAP_NET_ADMIN))
3381 /* cg-skb progs can be loaded by unpriv user.
3382 * check permissions at attach time.
3385 return prog->enforce_expected_attach_type &&
3386 prog->expected_attach_type != attach_type ?
3393 static enum bpf_prog_type
3394 attach_type_to_prog_type(enum bpf_attach_type attach_type)
3396 switch (attach_type) {
3397 case BPF_CGROUP_INET_INGRESS:
3398 case BPF_CGROUP_INET_EGRESS:
3399 return BPF_PROG_TYPE_CGROUP_SKB;
3400 case BPF_CGROUP_INET_SOCK_CREATE:
3401 case BPF_CGROUP_INET_SOCK_RELEASE:
3402 case BPF_CGROUP_INET4_POST_BIND:
3403 case BPF_CGROUP_INET6_POST_BIND:
3404 return BPF_PROG_TYPE_CGROUP_SOCK;
3405 case BPF_CGROUP_INET4_BIND:
3406 case BPF_CGROUP_INET6_BIND:
3407 case BPF_CGROUP_INET4_CONNECT:
3408 case BPF_CGROUP_INET6_CONNECT:
3409 case BPF_CGROUP_INET4_GETPEERNAME:
3410 case BPF_CGROUP_INET6_GETPEERNAME:
3411 case BPF_CGROUP_INET4_GETSOCKNAME:
3412 case BPF_CGROUP_INET6_GETSOCKNAME:
3413 case BPF_CGROUP_UDP4_SENDMSG:
3414 case BPF_CGROUP_UDP6_SENDMSG:
3415 case BPF_CGROUP_UDP4_RECVMSG:
3416 case BPF_CGROUP_UDP6_RECVMSG:
3417 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3418 case BPF_CGROUP_SOCK_OPS:
3419 return BPF_PROG_TYPE_SOCK_OPS;
3420 case BPF_CGROUP_DEVICE:
3421 return BPF_PROG_TYPE_CGROUP_DEVICE;
3422 case BPF_SK_MSG_VERDICT:
3423 return BPF_PROG_TYPE_SK_MSG;
3424 case BPF_SK_SKB_STREAM_PARSER:
3425 case BPF_SK_SKB_STREAM_VERDICT:
3426 case BPF_SK_SKB_VERDICT:
3427 return BPF_PROG_TYPE_SK_SKB;
3428 case BPF_LIRC_MODE2:
3429 return BPF_PROG_TYPE_LIRC_MODE2;
3430 case BPF_FLOW_DISSECTOR:
3431 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3432 case BPF_CGROUP_SYSCTL:
3433 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3434 case BPF_CGROUP_GETSOCKOPT:
3435 case BPF_CGROUP_SETSOCKOPT:
3436 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
3437 case BPF_TRACE_ITER:
3438 case BPF_TRACE_RAW_TP:
3439 case BPF_TRACE_FENTRY:
3440 case BPF_TRACE_FEXIT:
3441 case BPF_MODIFY_RETURN:
3442 return BPF_PROG_TYPE_TRACING;
3444 return BPF_PROG_TYPE_LSM;
3446 return BPF_PROG_TYPE_SK_LOOKUP;
3448 return BPF_PROG_TYPE_XDP;
3449 case BPF_LSM_CGROUP:
3450 return BPF_PROG_TYPE_LSM;
3452 return BPF_PROG_TYPE_UNSPEC;
3456 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
3458 #define BPF_F_ATTACH_MASK \
3459 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
3461 static int bpf_prog_attach(const union bpf_attr *attr)
3463 enum bpf_prog_type ptype;
3464 struct bpf_prog *prog;
3467 if (CHECK_ATTR(BPF_PROG_ATTACH))
3470 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
3473 ptype = attach_type_to_prog_type(attr->attach_type);
3474 if (ptype == BPF_PROG_TYPE_UNSPEC)
3477 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3479 return PTR_ERR(prog);
3481 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3487 case BPF_PROG_TYPE_SK_SKB:
3488 case BPF_PROG_TYPE_SK_MSG:
3489 ret = sock_map_get_from_fd(attr, prog);
3491 case BPF_PROG_TYPE_LIRC_MODE2:
3492 ret = lirc_prog_attach(attr, prog);
3494 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3495 ret = netns_bpf_prog_attach(attr, prog);
3497 case BPF_PROG_TYPE_CGROUP_DEVICE:
3498 case BPF_PROG_TYPE_CGROUP_SKB:
3499 case BPF_PROG_TYPE_CGROUP_SOCK:
3500 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3501 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3502 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3503 case BPF_PROG_TYPE_SOCK_OPS:
3504 case BPF_PROG_TYPE_LSM:
3505 if (ptype == BPF_PROG_TYPE_LSM &&
3506 prog->expected_attach_type != BPF_LSM_CGROUP)
3509 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3520 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3522 static int bpf_prog_detach(const union bpf_attr *attr)
3524 enum bpf_prog_type ptype;
3526 if (CHECK_ATTR(BPF_PROG_DETACH))
3529 ptype = attach_type_to_prog_type(attr->attach_type);
3532 case BPF_PROG_TYPE_SK_MSG:
3533 case BPF_PROG_TYPE_SK_SKB:
3534 return sock_map_prog_detach(attr, ptype);
3535 case BPF_PROG_TYPE_LIRC_MODE2:
3536 return lirc_prog_detach(attr);
3537 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3538 return netns_bpf_prog_detach(attr, ptype);
3539 case BPF_PROG_TYPE_CGROUP_DEVICE:
3540 case BPF_PROG_TYPE_CGROUP_SKB:
3541 case BPF_PROG_TYPE_CGROUP_SOCK:
3542 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3543 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3544 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3545 case BPF_PROG_TYPE_SOCK_OPS:
3546 case BPF_PROG_TYPE_LSM:
3547 return cgroup_bpf_prog_detach(attr, ptype);
3553 #define BPF_PROG_QUERY_LAST_FIELD query.prog_attach_flags
3555 static int bpf_prog_query(const union bpf_attr *attr,
3556 union bpf_attr __user *uattr)
3558 if (!capable(CAP_NET_ADMIN))
3560 if (CHECK_ATTR(BPF_PROG_QUERY))
3562 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3565 switch (attr->query.attach_type) {
3566 case BPF_CGROUP_INET_INGRESS:
3567 case BPF_CGROUP_INET_EGRESS:
3568 case BPF_CGROUP_INET_SOCK_CREATE:
3569 case BPF_CGROUP_INET_SOCK_RELEASE:
3570 case BPF_CGROUP_INET4_BIND:
3571 case BPF_CGROUP_INET6_BIND:
3572 case BPF_CGROUP_INET4_POST_BIND:
3573 case BPF_CGROUP_INET6_POST_BIND:
3574 case BPF_CGROUP_INET4_CONNECT:
3575 case BPF_CGROUP_INET6_CONNECT:
3576 case BPF_CGROUP_INET4_GETPEERNAME:
3577 case BPF_CGROUP_INET6_GETPEERNAME:
3578 case BPF_CGROUP_INET4_GETSOCKNAME:
3579 case BPF_CGROUP_INET6_GETSOCKNAME:
3580 case BPF_CGROUP_UDP4_SENDMSG:
3581 case BPF_CGROUP_UDP6_SENDMSG:
3582 case BPF_CGROUP_UDP4_RECVMSG:
3583 case BPF_CGROUP_UDP6_RECVMSG:
3584 case BPF_CGROUP_SOCK_OPS:
3585 case BPF_CGROUP_DEVICE:
3586 case BPF_CGROUP_SYSCTL:
3587 case BPF_CGROUP_GETSOCKOPT:
3588 case BPF_CGROUP_SETSOCKOPT:
3589 case BPF_LSM_CGROUP:
3590 return cgroup_bpf_prog_query(attr, uattr);
3591 case BPF_LIRC_MODE2:
3592 return lirc_prog_query(attr, uattr);
3593 case BPF_FLOW_DISSECTOR:
3595 return netns_bpf_prog_query(attr, uattr);
3596 case BPF_SK_SKB_STREAM_PARSER:
3597 case BPF_SK_SKB_STREAM_VERDICT:
3598 case BPF_SK_MSG_VERDICT:
3599 case BPF_SK_SKB_VERDICT:
3600 return sock_map_bpf_prog_query(attr, uattr);
3606 #define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size
3608 static int bpf_prog_test_run(const union bpf_attr *attr,
3609 union bpf_attr __user *uattr)
3611 struct bpf_prog *prog;
3612 int ret = -ENOTSUPP;
3614 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3617 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3618 (!attr->test.ctx_size_in && attr->test.ctx_in))
3621 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3622 (!attr->test.ctx_size_out && attr->test.ctx_out))
3625 prog = bpf_prog_get(attr->test.prog_fd);
3627 return PTR_ERR(prog);
3629 if (prog->aux->ops->test_run)
3630 ret = prog->aux->ops->test_run(prog, attr, uattr);
3636 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3638 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3639 union bpf_attr __user *uattr,
3643 u32 next_id = attr->start_id;
3646 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3649 if (!capable(CAP_SYS_ADMIN))
3654 if (!idr_get_next(idr, &next_id))
3656 spin_unlock_bh(lock);
3659 err = put_user(next_id, &uattr->next_id);
3664 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3666 struct bpf_map *map;
3668 spin_lock_bh(&map_idr_lock);
3670 map = idr_get_next(&map_idr, id);
3672 map = __bpf_map_inc_not_zero(map, false);
3678 spin_unlock_bh(&map_idr_lock);
3683 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3685 struct bpf_prog *prog;
3687 spin_lock_bh(&prog_idr_lock);
3689 prog = idr_get_next(&prog_idr, id);
3691 prog = bpf_prog_inc_not_zero(prog);
3697 spin_unlock_bh(&prog_idr_lock);
3702 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3704 struct bpf_prog *bpf_prog_by_id(u32 id)
3706 struct bpf_prog *prog;
3709 return ERR_PTR(-ENOENT);
3711 spin_lock_bh(&prog_idr_lock);
3712 prog = idr_find(&prog_idr, id);
3714 prog = bpf_prog_inc_not_zero(prog);
3716 prog = ERR_PTR(-ENOENT);
3717 spin_unlock_bh(&prog_idr_lock);
3721 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3723 struct bpf_prog *prog;
3724 u32 id = attr->prog_id;
3727 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3730 if (!capable(CAP_SYS_ADMIN))
3733 prog = bpf_prog_by_id(id);
3735 return PTR_ERR(prog);
3737 fd = bpf_prog_new_fd(prog);
3744 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3746 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3748 struct bpf_map *map;
3749 u32 id = attr->map_id;
3753 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3754 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3757 if (!capable(CAP_SYS_ADMIN))
3760 f_flags = bpf_get_file_flag(attr->open_flags);
3764 spin_lock_bh(&map_idr_lock);
3765 map = idr_find(&map_idr, id);
3767 map = __bpf_map_inc_not_zero(map, true);
3769 map = ERR_PTR(-ENOENT);
3770 spin_unlock_bh(&map_idr_lock);
3773 return PTR_ERR(map);
3775 fd = bpf_map_new_fd(map, f_flags);
3777 bpf_map_put_with_uref(map);
3782 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3783 unsigned long addr, u32 *off,
3786 const struct bpf_map *map;
3789 mutex_lock(&prog->aux->used_maps_mutex);
3790 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3791 map = prog->aux->used_maps[i];
3792 if (map == (void *)addr) {
3793 *type = BPF_PSEUDO_MAP_FD;
3796 if (!map->ops->map_direct_value_meta)
3798 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3799 *type = BPF_PSEUDO_MAP_VALUE;
3806 mutex_unlock(&prog->aux->used_maps_mutex);
3810 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3811 const struct cred *f_cred)
3813 const struct bpf_map *map;
3814 struct bpf_insn *insns;
3820 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3825 for (i = 0; i < prog->len; i++) {
3826 code = insns[i].code;
3828 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3829 insns[i].code = BPF_JMP | BPF_CALL;
3830 insns[i].imm = BPF_FUNC_tail_call;
3833 if (code == (BPF_JMP | BPF_CALL) ||
3834 code == (BPF_JMP | BPF_CALL_ARGS)) {
3835 if (code == (BPF_JMP | BPF_CALL_ARGS))
3836 insns[i].code = BPF_JMP | BPF_CALL;
3837 if (!bpf_dump_raw_ok(f_cred))
3841 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3842 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3846 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3849 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3850 map = bpf_map_from_imm(prog, imm, &off, &type);
3852 insns[i].src_reg = type;
3853 insns[i].imm = map->id;
3854 insns[i + 1].imm = off;
3862 static int set_info_rec_size(struct bpf_prog_info *info)
3865 * Ensure info.*_rec_size is the same as kernel expected size
3869 * Only allow zero *_rec_size if both _rec_size and _cnt are
3870 * zero. In this case, the kernel will set the expected
3871 * _rec_size back to the info.
3874 if ((info->nr_func_info || info->func_info_rec_size) &&
3875 info->func_info_rec_size != sizeof(struct bpf_func_info))
3878 if ((info->nr_line_info || info->line_info_rec_size) &&
3879 info->line_info_rec_size != sizeof(struct bpf_line_info))
3882 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3883 info->jited_line_info_rec_size != sizeof(__u64))
3886 info->func_info_rec_size = sizeof(struct bpf_func_info);
3887 info->line_info_rec_size = sizeof(struct bpf_line_info);
3888 info->jited_line_info_rec_size = sizeof(__u64);
3893 static int bpf_prog_get_info_by_fd(struct file *file,
3894 struct bpf_prog *prog,
3895 const union bpf_attr *attr,
3896 union bpf_attr __user *uattr)
3898 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3899 struct btf *attach_btf = bpf_prog_get_target_btf(prog);
3900 struct bpf_prog_info info;
3901 u32 info_len = attr->info.info_len;
3902 struct bpf_prog_kstats stats;
3903 char __user *uinsns;
3907 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3910 info_len = min_t(u32, sizeof(info), info_len);
3912 memset(&info, 0, sizeof(info));
3913 if (copy_from_user(&info, uinfo, info_len))
3916 info.type = prog->type;
3917 info.id = prog->aux->id;
3918 info.load_time = prog->aux->load_time;
3919 info.created_by_uid = from_kuid_munged(current_user_ns(),
3920 prog->aux->user->uid);
3921 info.gpl_compatible = prog->gpl_compatible;
3923 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3924 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3926 mutex_lock(&prog->aux->used_maps_mutex);
3927 ulen = info.nr_map_ids;
3928 info.nr_map_ids = prog->aux->used_map_cnt;
3929 ulen = min_t(u32, info.nr_map_ids, ulen);
3931 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3934 for (i = 0; i < ulen; i++)
3935 if (put_user(prog->aux->used_maps[i]->id,
3936 &user_map_ids[i])) {
3937 mutex_unlock(&prog->aux->used_maps_mutex);
3941 mutex_unlock(&prog->aux->used_maps_mutex);
3943 err = set_info_rec_size(&info);
3947 bpf_prog_get_stats(prog, &stats);
3948 info.run_time_ns = stats.nsecs;
3949 info.run_cnt = stats.cnt;
3950 info.recursion_misses = stats.misses;
3952 info.verified_insns = prog->aux->verified_insns;
3954 if (!bpf_capable()) {
3955 info.jited_prog_len = 0;
3956 info.xlated_prog_len = 0;
3957 info.nr_jited_ksyms = 0;
3958 info.nr_jited_func_lens = 0;
3959 info.nr_func_info = 0;
3960 info.nr_line_info = 0;
3961 info.nr_jited_line_info = 0;
3965 ulen = info.xlated_prog_len;
3966 info.xlated_prog_len = bpf_prog_insn_size(prog);
3967 if (info.xlated_prog_len && ulen) {
3968 struct bpf_insn *insns_sanitized;
3971 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3972 info.xlated_prog_insns = 0;
3975 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3976 if (!insns_sanitized)
3978 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3979 ulen = min_t(u32, info.xlated_prog_len, ulen);
3980 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3981 kfree(insns_sanitized);
3986 if (bpf_prog_is_dev_bound(prog->aux)) {
3987 err = bpf_prog_offload_info_fill(&info, prog);
3993 /* NOTE: the following code is supposed to be skipped for offload.
3994 * bpf_prog_offload_info_fill() is the place to fill similar fields
3997 ulen = info.jited_prog_len;
3998 if (prog->aux->func_cnt) {
4001 info.jited_prog_len = 0;
4002 for (i = 0; i < prog->aux->func_cnt; i++)
4003 info.jited_prog_len += prog->aux->func[i]->jited_len;
4005 info.jited_prog_len = prog->jited_len;
4008 if (info.jited_prog_len && ulen) {
4009 if (bpf_dump_raw_ok(file->f_cred)) {
4010 uinsns = u64_to_user_ptr(info.jited_prog_insns);
4011 ulen = min_t(u32, info.jited_prog_len, ulen);
4013 /* for multi-function programs, copy the JITed
4014 * instructions for all the functions
4016 if (prog->aux->func_cnt) {
4021 for (i = 0; i < prog->aux->func_cnt; i++) {
4022 len = prog->aux->func[i]->jited_len;
4023 len = min_t(u32, len, free);
4024 img = (u8 *) prog->aux->func[i]->bpf_func;
4025 if (copy_to_user(uinsns, img, len))
4033 if (copy_to_user(uinsns, prog->bpf_func, ulen))
4037 info.jited_prog_insns = 0;
4041 ulen = info.nr_jited_ksyms;
4042 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
4044 if (bpf_dump_raw_ok(file->f_cred)) {
4045 unsigned long ksym_addr;
4046 u64 __user *user_ksyms;
4049 /* copy the address of the kernel symbol
4050 * corresponding to each function
4052 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
4053 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
4054 if (prog->aux->func_cnt) {
4055 for (i = 0; i < ulen; i++) {
4056 ksym_addr = (unsigned long)
4057 prog->aux->func[i]->bpf_func;
4058 if (put_user((u64) ksym_addr,
4063 ksym_addr = (unsigned long) prog->bpf_func;
4064 if (put_user((u64) ksym_addr, &user_ksyms[0]))
4068 info.jited_ksyms = 0;
4072 ulen = info.nr_jited_func_lens;
4073 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
4075 if (bpf_dump_raw_ok(file->f_cred)) {
4076 u32 __user *user_lens;
4079 /* copy the JITed image lengths for each function */
4080 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
4081 user_lens = u64_to_user_ptr(info.jited_func_lens);
4082 if (prog->aux->func_cnt) {
4083 for (i = 0; i < ulen; i++) {
4085 prog->aux->func[i]->jited_len;
4086 if (put_user(func_len, &user_lens[i]))
4090 func_len = prog->jited_len;
4091 if (put_user(func_len, &user_lens[0]))
4095 info.jited_func_lens = 0;
4100 info.btf_id = btf_obj_id(prog->aux->btf);
4101 info.attach_btf_id = prog->aux->attach_btf_id;
4103 info.attach_btf_obj_id = btf_obj_id(attach_btf);
4105 ulen = info.nr_func_info;
4106 info.nr_func_info = prog->aux->func_info_cnt;
4107 if (info.nr_func_info && ulen) {
4108 char __user *user_finfo;
4110 user_finfo = u64_to_user_ptr(info.func_info);
4111 ulen = min_t(u32, info.nr_func_info, ulen);
4112 if (copy_to_user(user_finfo, prog->aux->func_info,
4113 info.func_info_rec_size * ulen))
4117 ulen = info.nr_line_info;
4118 info.nr_line_info = prog->aux->nr_linfo;
4119 if (info.nr_line_info && ulen) {
4120 __u8 __user *user_linfo;
4122 user_linfo = u64_to_user_ptr(info.line_info);
4123 ulen = min_t(u32, info.nr_line_info, ulen);
4124 if (copy_to_user(user_linfo, prog->aux->linfo,
4125 info.line_info_rec_size * ulen))
4129 ulen = info.nr_jited_line_info;
4130 if (prog->aux->jited_linfo)
4131 info.nr_jited_line_info = prog->aux->nr_linfo;
4133 info.nr_jited_line_info = 0;
4134 if (info.nr_jited_line_info && ulen) {
4135 if (bpf_dump_raw_ok(file->f_cred)) {
4136 unsigned long line_addr;
4137 __u64 __user *user_linfo;
4140 user_linfo = u64_to_user_ptr(info.jited_line_info);
4141 ulen = min_t(u32, info.nr_jited_line_info, ulen);
4142 for (i = 0; i < ulen; i++) {
4143 line_addr = (unsigned long)prog->aux->jited_linfo[i];
4144 if (put_user((__u64)line_addr, &user_linfo[i]))
4148 info.jited_line_info = 0;
4152 ulen = info.nr_prog_tags;
4153 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
4155 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
4158 user_prog_tags = u64_to_user_ptr(info.prog_tags);
4159 ulen = min_t(u32, info.nr_prog_tags, ulen);
4160 if (prog->aux->func_cnt) {
4161 for (i = 0; i < ulen; i++) {
4162 if (copy_to_user(user_prog_tags[i],
4163 prog->aux->func[i]->tag,
4168 if (copy_to_user(user_prog_tags[0],
4169 prog->tag, BPF_TAG_SIZE))
4175 if (copy_to_user(uinfo, &info, info_len) ||
4176 put_user(info_len, &uattr->info.info_len))
4182 static int bpf_map_get_info_by_fd(struct file *file,
4183 struct bpf_map *map,
4184 const union bpf_attr *attr,
4185 union bpf_attr __user *uattr)
4187 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4188 struct bpf_map_info info;
4189 u32 info_len = attr->info.info_len;
4192 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
4195 info_len = min_t(u32, sizeof(info), info_len);
4197 memset(&info, 0, sizeof(info));
4198 info.type = map->map_type;
4200 info.key_size = map->key_size;
4201 info.value_size = map->value_size;
4202 info.max_entries = map->max_entries;
4203 info.map_flags = map->map_flags;
4204 info.map_extra = map->map_extra;
4205 memcpy(info.name, map->name, sizeof(map->name));
4208 info.btf_id = btf_obj_id(map->btf);
4209 info.btf_key_type_id = map->btf_key_type_id;
4210 info.btf_value_type_id = map->btf_value_type_id;
4212 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
4214 if (bpf_map_is_dev_bound(map)) {
4215 err = bpf_map_offload_info_fill(&info, map);
4220 if (copy_to_user(uinfo, &info, info_len) ||
4221 put_user(info_len, &uattr->info.info_len))
4227 static int bpf_btf_get_info_by_fd(struct file *file,
4229 const union bpf_attr *attr,
4230 union bpf_attr __user *uattr)
4232 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4233 u32 info_len = attr->info.info_len;
4236 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
4240 return btf_get_info_by_fd(btf, attr, uattr);
4243 static int bpf_link_get_info_by_fd(struct file *file,
4244 struct bpf_link *link,
4245 const union bpf_attr *attr,
4246 union bpf_attr __user *uattr)
4248 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4249 struct bpf_link_info info;
4250 u32 info_len = attr->info.info_len;
4253 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
4256 info_len = min_t(u32, sizeof(info), info_len);
4258 memset(&info, 0, sizeof(info));
4259 if (copy_from_user(&info, uinfo, info_len))
4262 info.type = link->type;
4264 info.prog_id = link->prog->aux->id;
4266 if (link->ops->fill_link_info) {
4267 err = link->ops->fill_link_info(link, &info);
4272 if (copy_to_user(uinfo, &info, info_len) ||
4273 put_user(info_len, &uattr->info.info_len))
4280 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
4282 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
4283 union bpf_attr __user *uattr)
4285 int ufd = attr->info.bpf_fd;
4289 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
4296 if (f.file->f_op == &bpf_prog_fops)
4297 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
4299 else if (f.file->f_op == &bpf_map_fops)
4300 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
4302 else if (f.file->f_op == &btf_fops)
4303 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
4304 else if (f.file->f_op == &bpf_link_fops)
4305 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
4314 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
4316 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
4318 if (CHECK_ATTR(BPF_BTF_LOAD))
4324 return btf_new_fd(attr, uattr);
4327 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
4329 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
4331 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
4334 if (!capable(CAP_SYS_ADMIN))
4337 return btf_get_fd_by_id(attr->btf_id);
4340 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
4341 union bpf_attr __user *uattr,
4342 u32 prog_id, u32 fd_type,
4343 const char *buf, u64 probe_offset,
4346 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
4347 u32 len = buf ? strlen(buf) : 0, input_len;
4350 if (put_user(len, &uattr->task_fd_query.buf_len))
4352 input_len = attr->task_fd_query.buf_len;
4353 if (input_len && ubuf) {
4355 /* nothing to copy, just make ubuf NULL terminated */
4358 if (put_user(zero, ubuf))
4360 } else if (input_len >= len + 1) {
4361 /* ubuf can hold the string with NULL terminator */
4362 if (copy_to_user(ubuf, buf, len + 1))
4365 /* ubuf cannot hold the string with NULL terminator,
4366 * do a partial copy with NULL terminator.
4371 if (copy_to_user(ubuf, buf, input_len - 1))
4373 if (put_user(zero, ubuf + input_len - 1))
4378 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
4379 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
4380 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
4381 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
4387 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
4389 static int bpf_task_fd_query(const union bpf_attr *attr,
4390 union bpf_attr __user *uattr)
4392 pid_t pid = attr->task_fd_query.pid;
4393 u32 fd = attr->task_fd_query.fd;
4394 const struct perf_event *event;
4395 struct task_struct *task;
4399 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
4402 if (!capable(CAP_SYS_ADMIN))
4405 if (attr->task_fd_query.flags != 0)
4409 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
4415 file = fget_task(task, fd);
4416 put_task_struct(task);
4420 if (file->f_op == &bpf_link_fops) {
4421 struct bpf_link *link = file->private_data;
4423 if (link->ops == &bpf_raw_tp_link_lops) {
4424 struct bpf_raw_tp_link *raw_tp =
4425 container_of(link, struct bpf_raw_tp_link, link);
4426 struct bpf_raw_event_map *btp = raw_tp->btp;
4428 err = bpf_task_fd_query_copy(attr, uattr,
4429 raw_tp->link.prog->aux->id,
4430 BPF_FD_TYPE_RAW_TRACEPOINT,
4431 btp->tp->name, 0, 0);
4437 event = perf_get_event(file);
4438 if (!IS_ERR(event)) {
4439 u64 probe_offset, probe_addr;
4440 u32 prog_id, fd_type;
4443 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4444 &buf, &probe_offset,
4447 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4461 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
4463 #define BPF_DO_BATCH(fn) \
4469 err = fn(map, attr, uattr); \
4472 static int bpf_map_do_batch(const union bpf_attr *attr,
4473 union bpf_attr __user *uattr,
4476 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH ||
4477 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
4478 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
4479 struct bpf_map *map;
4483 if (CHECK_ATTR(BPF_MAP_BATCH))
4486 ufd = attr->batch.map_fd;
4488 map = __bpf_map_get(f);
4490 return PTR_ERR(map);
4492 bpf_map_write_active_inc(map);
4493 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4497 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4502 if (cmd == BPF_MAP_LOOKUP_BATCH)
4503 BPF_DO_BATCH(map->ops->map_lookup_batch);
4504 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4505 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4506 else if (cmd == BPF_MAP_UPDATE_BATCH)
4507 BPF_DO_BATCH(map->ops->map_update_batch);
4509 BPF_DO_BATCH(map->ops->map_delete_batch);
4512 bpf_map_write_active_dec(map);
4517 #define BPF_LINK_CREATE_LAST_FIELD link_create.kprobe_multi.cookies
4518 static int link_create(union bpf_attr *attr, bpfptr_t uattr)
4520 enum bpf_prog_type ptype;
4521 struct bpf_prog *prog;
4524 if (CHECK_ATTR(BPF_LINK_CREATE))
4527 prog = bpf_prog_get(attr->link_create.prog_fd);
4529 return PTR_ERR(prog);
4531 ret = bpf_prog_attach_check_attach_type(prog,
4532 attr->link_create.attach_type);
4536 switch (prog->type) {
4537 case BPF_PROG_TYPE_EXT:
4539 case BPF_PROG_TYPE_PERF_EVENT:
4540 case BPF_PROG_TYPE_TRACEPOINT:
4541 if (attr->link_create.attach_type != BPF_PERF_EVENT) {
4546 case BPF_PROG_TYPE_KPROBE:
4547 if (attr->link_create.attach_type != BPF_PERF_EVENT &&
4548 attr->link_create.attach_type != BPF_TRACE_KPROBE_MULTI) {
4554 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4555 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4562 switch (prog->type) {
4563 case BPF_PROG_TYPE_CGROUP_SKB:
4564 case BPF_PROG_TYPE_CGROUP_SOCK:
4565 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4566 case BPF_PROG_TYPE_SOCK_OPS:
4567 case BPF_PROG_TYPE_CGROUP_DEVICE:
4568 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4569 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4570 ret = cgroup_bpf_link_attach(attr, prog);
4572 case BPF_PROG_TYPE_EXT:
4573 ret = bpf_tracing_prog_attach(prog,
4574 attr->link_create.target_fd,
4575 attr->link_create.target_btf_id,
4576 attr->link_create.tracing.cookie);
4578 case BPF_PROG_TYPE_LSM:
4579 case BPF_PROG_TYPE_TRACING:
4580 if (attr->link_create.attach_type != prog->expected_attach_type) {
4584 if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
4585 ret = bpf_raw_tp_link_attach(prog, NULL);
4586 else if (prog->expected_attach_type == BPF_TRACE_ITER)
4587 ret = bpf_iter_link_attach(attr, uattr, prog);
4588 else if (prog->expected_attach_type == BPF_LSM_CGROUP)
4589 ret = cgroup_bpf_link_attach(attr, prog);
4591 ret = bpf_tracing_prog_attach(prog,
4592 attr->link_create.target_fd,
4593 attr->link_create.target_btf_id,
4594 attr->link_create.tracing.cookie);
4596 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4597 case BPF_PROG_TYPE_SK_LOOKUP:
4598 ret = netns_bpf_link_create(attr, prog);
4601 case BPF_PROG_TYPE_XDP:
4602 ret = bpf_xdp_link_attach(attr, prog);
4605 case BPF_PROG_TYPE_PERF_EVENT:
4606 case BPF_PROG_TYPE_TRACEPOINT:
4607 ret = bpf_perf_link_attach(attr, prog);
4609 case BPF_PROG_TYPE_KPROBE:
4610 if (attr->link_create.attach_type == BPF_PERF_EVENT)
4611 ret = bpf_perf_link_attach(attr, prog);
4613 ret = bpf_kprobe_multi_link_attach(attr, prog);
4625 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4627 static int link_update(union bpf_attr *attr)
4629 struct bpf_prog *old_prog = NULL, *new_prog;
4630 struct bpf_link *link;
4634 if (CHECK_ATTR(BPF_LINK_UPDATE))
4637 flags = attr->link_update.flags;
4638 if (flags & ~BPF_F_REPLACE)
4641 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4643 return PTR_ERR(link);
4645 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4646 if (IS_ERR(new_prog)) {
4647 ret = PTR_ERR(new_prog);
4651 if (flags & BPF_F_REPLACE) {
4652 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4653 if (IS_ERR(old_prog)) {
4654 ret = PTR_ERR(old_prog);
4658 } else if (attr->link_update.old_prog_fd) {
4663 if (link->ops->update_prog)
4664 ret = link->ops->update_prog(link, new_prog, old_prog);
4670 bpf_prog_put(old_prog);
4672 bpf_prog_put(new_prog);
4678 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4680 static int link_detach(union bpf_attr *attr)
4682 struct bpf_link *link;
4685 if (CHECK_ATTR(BPF_LINK_DETACH))
4688 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4690 return PTR_ERR(link);
4692 if (link->ops->detach)
4693 ret = link->ops->detach(link);
4701 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4703 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4706 struct bpf_link *bpf_link_by_id(u32 id)
4708 struct bpf_link *link;
4711 return ERR_PTR(-ENOENT);
4713 spin_lock_bh(&link_idr_lock);
4714 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4715 link = idr_find(&link_idr, id);
4718 link = bpf_link_inc_not_zero(link);
4720 link = ERR_PTR(-EAGAIN);
4722 link = ERR_PTR(-ENOENT);
4724 spin_unlock_bh(&link_idr_lock);
4728 struct bpf_link *bpf_link_get_curr_or_next(u32 *id)
4730 struct bpf_link *link;
4732 spin_lock_bh(&link_idr_lock);
4734 link = idr_get_next(&link_idr, id);
4736 link = bpf_link_inc_not_zero(link);
4742 spin_unlock_bh(&link_idr_lock);
4747 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4749 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4751 struct bpf_link *link;
4752 u32 id = attr->link_id;
4755 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4758 if (!capable(CAP_SYS_ADMIN))
4761 link = bpf_link_by_id(id);
4763 return PTR_ERR(link);
4765 fd = bpf_link_new_fd(link);
4772 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4774 static int bpf_stats_release(struct inode *inode, struct file *file)
4776 mutex_lock(&bpf_stats_enabled_mutex);
4777 static_key_slow_dec(&bpf_stats_enabled_key.key);
4778 mutex_unlock(&bpf_stats_enabled_mutex);
4782 static const struct file_operations bpf_stats_fops = {
4783 .release = bpf_stats_release,
4786 static int bpf_enable_runtime_stats(void)
4790 mutex_lock(&bpf_stats_enabled_mutex);
4792 /* Set a very high limit to avoid overflow */
4793 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4794 mutex_unlock(&bpf_stats_enabled_mutex);
4798 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4800 static_key_slow_inc(&bpf_stats_enabled_key.key);
4802 mutex_unlock(&bpf_stats_enabled_mutex);
4806 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4808 static int bpf_enable_stats(union bpf_attr *attr)
4811 if (CHECK_ATTR(BPF_ENABLE_STATS))
4814 if (!capable(CAP_SYS_ADMIN))
4817 switch (attr->enable_stats.type) {
4818 case BPF_STATS_RUN_TIME:
4819 return bpf_enable_runtime_stats();
4826 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4828 static int bpf_iter_create(union bpf_attr *attr)
4830 struct bpf_link *link;
4833 if (CHECK_ATTR(BPF_ITER_CREATE))
4836 if (attr->iter_create.flags)
4839 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4841 return PTR_ERR(link);
4843 err = bpf_iter_new_fd(link);
4849 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4851 static int bpf_prog_bind_map(union bpf_attr *attr)
4853 struct bpf_prog *prog;
4854 struct bpf_map *map;
4855 struct bpf_map **used_maps_old, **used_maps_new;
4858 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4861 if (attr->prog_bind_map.flags)
4864 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4866 return PTR_ERR(prog);
4868 map = bpf_map_get(attr->prog_bind_map.map_fd);
4874 mutex_lock(&prog->aux->used_maps_mutex);
4876 used_maps_old = prog->aux->used_maps;
4878 for (i = 0; i < prog->aux->used_map_cnt; i++)
4879 if (used_maps_old[i] == map) {
4884 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4885 sizeof(used_maps_new[0]),
4887 if (!used_maps_new) {
4892 memcpy(used_maps_new, used_maps_old,
4893 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4894 used_maps_new[prog->aux->used_map_cnt] = map;
4896 prog->aux->used_map_cnt++;
4897 prog->aux->used_maps = used_maps_new;
4899 kfree(used_maps_old);
4902 mutex_unlock(&prog->aux->used_maps_mutex);
4911 static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
4913 union bpf_attr attr;
4917 capable = bpf_capable() || !sysctl_unprivileged_bpf_disabled;
4919 /* Intent here is for unprivileged_bpf_disabled to block key object
4920 * creation commands for unprivileged users; other actions depend
4921 * of fd availability and access to bpffs, so are dependent on
4922 * object creation success. Capabilities are later verified for
4923 * operations such as load and map create, so even with unprivileged
4924 * BPF disabled, capability checks are still carried out for these
4925 * and other operations.
4928 (cmd == BPF_MAP_CREATE || cmd == BPF_PROG_LOAD))
4931 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4934 size = min_t(u32, size, sizeof(attr));
4936 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4937 memset(&attr, 0, sizeof(attr));
4938 if (copy_from_bpfptr(&attr, uattr, size) != 0)
4941 err = security_bpf(cmd, &attr, size);
4946 case BPF_MAP_CREATE:
4947 err = map_create(&attr);
4949 case BPF_MAP_LOOKUP_ELEM:
4950 err = map_lookup_elem(&attr);
4952 case BPF_MAP_UPDATE_ELEM:
4953 err = map_update_elem(&attr, uattr);
4955 case BPF_MAP_DELETE_ELEM:
4956 err = map_delete_elem(&attr, uattr);
4958 case BPF_MAP_GET_NEXT_KEY:
4959 err = map_get_next_key(&attr);
4961 case BPF_MAP_FREEZE:
4962 err = map_freeze(&attr);
4965 err = bpf_prog_load(&attr, uattr);
4968 err = bpf_obj_pin(&attr);
4971 err = bpf_obj_get(&attr);
4973 case BPF_PROG_ATTACH:
4974 err = bpf_prog_attach(&attr);
4976 case BPF_PROG_DETACH:
4977 err = bpf_prog_detach(&attr);
4979 case BPF_PROG_QUERY:
4980 err = bpf_prog_query(&attr, uattr.user);
4982 case BPF_PROG_TEST_RUN:
4983 err = bpf_prog_test_run(&attr, uattr.user);
4985 case BPF_PROG_GET_NEXT_ID:
4986 err = bpf_obj_get_next_id(&attr, uattr.user,
4987 &prog_idr, &prog_idr_lock);
4989 case BPF_MAP_GET_NEXT_ID:
4990 err = bpf_obj_get_next_id(&attr, uattr.user,
4991 &map_idr, &map_idr_lock);
4993 case BPF_BTF_GET_NEXT_ID:
4994 err = bpf_obj_get_next_id(&attr, uattr.user,
4995 &btf_idr, &btf_idr_lock);
4997 case BPF_PROG_GET_FD_BY_ID:
4998 err = bpf_prog_get_fd_by_id(&attr);
5000 case BPF_MAP_GET_FD_BY_ID:
5001 err = bpf_map_get_fd_by_id(&attr);
5003 case BPF_OBJ_GET_INFO_BY_FD:
5004 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
5006 case BPF_RAW_TRACEPOINT_OPEN:
5007 err = bpf_raw_tracepoint_open(&attr);
5010 err = bpf_btf_load(&attr, uattr);
5012 case BPF_BTF_GET_FD_BY_ID:
5013 err = bpf_btf_get_fd_by_id(&attr);
5015 case BPF_TASK_FD_QUERY:
5016 err = bpf_task_fd_query(&attr, uattr.user);
5018 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
5019 err = map_lookup_and_delete_elem(&attr);
5021 case BPF_MAP_LOOKUP_BATCH:
5022 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
5024 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
5025 err = bpf_map_do_batch(&attr, uattr.user,
5026 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
5028 case BPF_MAP_UPDATE_BATCH:
5029 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
5031 case BPF_MAP_DELETE_BATCH:
5032 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
5034 case BPF_LINK_CREATE:
5035 err = link_create(&attr, uattr);
5037 case BPF_LINK_UPDATE:
5038 err = link_update(&attr);
5040 case BPF_LINK_GET_FD_BY_ID:
5041 err = bpf_link_get_fd_by_id(&attr);
5043 case BPF_LINK_GET_NEXT_ID:
5044 err = bpf_obj_get_next_id(&attr, uattr.user,
5045 &link_idr, &link_idr_lock);
5047 case BPF_ENABLE_STATS:
5048 err = bpf_enable_stats(&attr);
5050 case BPF_ITER_CREATE:
5051 err = bpf_iter_create(&attr);
5053 case BPF_LINK_DETACH:
5054 err = link_detach(&attr);
5056 case BPF_PROG_BIND_MAP:
5057 err = bpf_prog_bind_map(&attr);
5067 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
5069 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
5072 static bool syscall_prog_is_valid_access(int off, int size,
5073 enum bpf_access_type type,
5074 const struct bpf_prog *prog,
5075 struct bpf_insn_access_aux *info)
5077 if (off < 0 || off >= U16_MAX)
5079 if (off % size != 0)
5084 BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
5087 case BPF_MAP_CREATE:
5088 case BPF_MAP_DELETE_ELEM:
5089 case BPF_MAP_UPDATE_ELEM:
5090 case BPF_MAP_FREEZE:
5091 case BPF_MAP_GET_FD_BY_ID:
5094 case BPF_LINK_CREATE:
5095 case BPF_RAW_TRACEPOINT_OPEN:
5100 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
5104 /* To shut up -Wmissing-prototypes.
5105 * This function is used by the kernel light skeleton
5106 * to load bpf programs when modules are loaded or during kernel boot.
5107 * See tools/lib/bpf/skel_internal.h
5109 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size);
5111 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5113 struct bpf_prog * __maybe_unused prog;
5114 struct bpf_tramp_run_ctx __maybe_unused run_ctx;
5117 #ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */
5118 case BPF_PROG_TEST_RUN:
5119 if (attr->test.data_in || attr->test.data_out ||
5120 attr->test.ctx_out || attr->test.duration ||
5121 attr->test.repeat || attr->test.flags)
5124 prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL);
5126 return PTR_ERR(prog);
5128 if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
5129 attr->test.ctx_size_in > U16_MAX) {
5134 run_ctx.bpf_cookie = 0;
5135 run_ctx.saved_run_ctx = NULL;
5136 if (!__bpf_prog_enter_sleepable(prog, &run_ctx)) {
5137 /* recursion detected */
5141 attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
5142 __bpf_prog_exit_sleepable(prog, 0 /* bpf_prog_run does runtime stats */, &run_ctx);
5147 return ____bpf_sys_bpf(cmd, attr, size);
5150 EXPORT_SYMBOL(kern_sys_bpf);
5152 static const struct bpf_func_proto bpf_sys_bpf_proto = {
5153 .func = bpf_sys_bpf,
5155 .ret_type = RET_INTEGER,
5156 .arg1_type = ARG_ANYTHING,
5157 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5158 .arg3_type = ARG_CONST_SIZE,
5161 const struct bpf_func_proto * __weak
5162 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5164 return bpf_base_func_proto(func_id);
5167 BPF_CALL_1(bpf_sys_close, u32, fd)
5169 /* When bpf program calls this helper there should not be
5170 * an fdget() without matching completed fdput().
5171 * This helper is allowed in the following callchain only:
5172 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
5174 return close_fd(fd);
5177 static const struct bpf_func_proto bpf_sys_close_proto = {
5178 .func = bpf_sys_close,
5180 .ret_type = RET_INTEGER,
5181 .arg1_type = ARG_ANYTHING,
5184 BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res)
5189 if (name_sz <= 1 || name[name_sz - 1])
5192 if (!bpf_dump_raw_ok(current_cred()))
5195 *res = kallsyms_lookup_name(name);
5196 return *res ? 0 : -ENOENT;
5199 static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = {
5200 .func = bpf_kallsyms_lookup_name,
5202 .ret_type = RET_INTEGER,
5203 .arg1_type = ARG_PTR_TO_MEM,
5204 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
5205 .arg3_type = ARG_ANYTHING,
5206 .arg4_type = ARG_PTR_TO_LONG,
5209 static const struct bpf_func_proto *
5210 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5213 case BPF_FUNC_sys_bpf:
5214 return !perfmon_capable() ? NULL : &bpf_sys_bpf_proto;
5215 case BPF_FUNC_btf_find_by_name_kind:
5216 return &bpf_btf_find_by_name_kind_proto;
5217 case BPF_FUNC_sys_close:
5218 return &bpf_sys_close_proto;
5219 case BPF_FUNC_kallsyms_lookup_name:
5220 return &bpf_kallsyms_lookup_name_proto;
5222 return tracing_prog_func_proto(func_id, prog);
5226 const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
5227 .get_func_proto = syscall_prog_func_proto,
5228 .is_valid_access = syscall_prog_is_valid_access,
5231 const struct bpf_prog_ops bpf_syscall_prog_ops = {
5232 .test_run = bpf_prog_test_run_syscall,
5235 #ifdef CONFIG_SYSCTL
5236 static int bpf_stats_handler(struct ctl_table *table, int write,
5237 void *buffer, size_t *lenp, loff_t *ppos)
5239 struct static_key *key = (struct static_key *)table->data;
5240 static int saved_val;
5242 struct ctl_table tmp = {
5244 .maxlen = sizeof(val),
5245 .mode = table->mode,
5246 .extra1 = SYSCTL_ZERO,
5247 .extra2 = SYSCTL_ONE,
5250 if (write && !capable(CAP_SYS_ADMIN))
5253 mutex_lock(&bpf_stats_enabled_mutex);
5255 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
5256 if (write && !ret && val != saved_val) {
5258 static_key_slow_inc(key);
5260 static_key_slow_dec(key);
5263 mutex_unlock(&bpf_stats_enabled_mutex);
5267 void __weak unpriv_ebpf_notify(int new_state)
5271 static int bpf_unpriv_handler(struct ctl_table *table, int write,
5272 void *buffer, size_t *lenp, loff_t *ppos)
5274 int ret, unpriv_enable = *(int *)table->data;
5275 bool locked_state = unpriv_enable == 1;
5276 struct ctl_table tmp = *table;
5278 if (write && !capable(CAP_SYS_ADMIN))
5281 tmp.data = &unpriv_enable;
5282 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
5283 if (write && !ret) {
5284 if (locked_state && unpriv_enable != 1)
5286 *(int *)table->data = unpriv_enable;
5289 unpriv_ebpf_notify(unpriv_enable);
5294 static struct ctl_table bpf_syscall_table[] = {
5296 .procname = "unprivileged_bpf_disabled",
5297 .data = &sysctl_unprivileged_bpf_disabled,
5298 .maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
5300 .proc_handler = bpf_unpriv_handler,
5301 .extra1 = SYSCTL_ZERO,
5302 .extra2 = SYSCTL_TWO,
5305 .procname = "bpf_stats_enabled",
5306 .data = &bpf_stats_enabled_key.key,
5307 .maxlen = sizeof(bpf_stats_enabled_key),
5309 .proc_handler = bpf_stats_handler,
5314 static int __init bpf_syscall_sysctl_init(void)
5316 register_sysctl_init("kernel", bpf_syscall_table);
5319 late_initcall(bpf_syscall_sysctl_init);
5320 #endif /* CONFIG_SYSCTL */