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 #include <net/netfilter/nf_bpf_link.h>
40 #include <net/netkit.h>
43 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
44 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
45 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
46 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
47 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
48 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
51 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
53 DEFINE_PER_CPU(int, bpf_prog_active);
54 static DEFINE_IDR(prog_idr);
55 static DEFINE_SPINLOCK(prog_idr_lock);
56 static DEFINE_IDR(map_idr);
57 static DEFINE_SPINLOCK(map_idr_lock);
58 static DEFINE_IDR(link_idr);
59 static DEFINE_SPINLOCK(link_idr_lock);
61 int sysctl_unprivileged_bpf_disabled __read_mostly =
62 IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
64 static const struct bpf_map_ops * const bpf_map_types[] = {
65 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
66 #define BPF_MAP_TYPE(_id, _ops) \
68 #define BPF_LINK_TYPE(_id, _name)
69 #include <linux/bpf_types.h>
76 * If we're handed a bigger struct than we know of, ensure all the unknown bits
77 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
78 * we don't know about yet.
80 * There is a ToCToU between this function call and the following
81 * copy_from_user() call. However, this is not a concern since this function is
82 * meant to be a future-proofing of bits.
84 int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
90 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
93 if (actual_size <= expected_size)
97 res = memchr_inv(uaddr.kernel + expected_size, 0,
98 actual_size - expected_size) == NULL;
100 res = check_zeroed_user(uaddr.user + expected_size,
101 actual_size - expected_size);
104 return res ? 0 : -E2BIG;
107 const struct bpf_map_ops bpf_map_offload_ops = {
108 .map_meta_equal = bpf_map_meta_equal,
109 .map_alloc = bpf_map_offload_map_alloc,
110 .map_free = bpf_map_offload_map_free,
111 .map_check_btf = map_check_no_btf,
112 .map_mem_usage = bpf_map_offload_map_mem_usage,
115 static void bpf_map_write_active_inc(struct bpf_map *map)
117 atomic64_inc(&map->writecnt);
120 static void bpf_map_write_active_dec(struct bpf_map *map)
122 atomic64_dec(&map->writecnt);
125 bool bpf_map_write_active(const struct bpf_map *map)
127 return atomic64_read(&map->writecnt) != 0;
130 static u32 bpf_map_value_size(const struct bpf_map *map)
132 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
133 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
134 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
135 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
136 return round_up(map->value_size, 8) * num_possible_cpus();
137 else if (IS_FD_MAP(map))
140 return map->value_size;
143 static void maybe_wait_bpf_programs(struct bpf_map *map)
145 /* Wait for any running non-sleepable BPF programs to complete so that
146 * userspace, when we return to it, knows that all non-sleepable
147 * programs that could be running use the new map value. For sleepable
148 * BPF programs, synchronize_rcu_tasks_trace() should be used to wait
149 * for the completions of these programs, but considering the waiting
150 * time can be very long and userspace may think it will hang forever,
151 * so don't handle sleepable BPF programs now.
153 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
154 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
158 static int bpf_map_update_value(struct bpf_map *map, struct file *map_file,
159 void *key, void *value, __u64 flags)
163 /* Need to create a kthread, thus must support schedule */
164 if (bpf_map_is_offloaded(map)) {
165 return bpf_map_offload_update_elem(map, key, value, flags);
166 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
167 map->map_type == BPF_MAP_TYPE_ARENA ||
168 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
169 return map->ops->map_update_elem(map, key, value, flags);
170 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
171 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
172 return sock_map_update_elem_sys(map, key, value, flags);
173 } else if (IS_FD_PROG_ARRAY(map)) {
174 return bpf_fd_array_map_update_elem(map, map_file, key, value,
178 bpf_disable_instrumentation();
179 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
180 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
181 err = bpf_percpu_hash_update(map, key, value, flags);
182 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
183 err = bpf_percpu_array_update(map, key, value, flags);
184 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
185 err = bpf_percpu_cgroup_storage_update(map, key, value,
187 } else if (IS_FD_ARRAY(map)) {
188 err = bpf_fd_array_map_update_elem(map, map_file, key, value,
190 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
191 err = bpf_fd_htab_map_update_elem(map, map_file, key, value,
193 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
194 /* rcu_read_lock() is not needed */
195 err = bpf_fd_reuseport_array_update_elem(map, key, value,
197 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
198 map->map_type == BPF_MAP_TYPE_STACK ||
199 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
200 err = map->ops->map_push_elem(map, value, flags);
203 err = map->ops->map_update_elem(map, key, value, flags);
206 bpf_enable_instrumentation();
211 static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
217 if (bpf_map_is_offloaded(map))
218 return bpf_map_offload_lookup_elem(map, key, value);
220 bpf_disable_instrumentation();
221 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
222 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
223 err = bpf_percpu_hash_copy(map, key, value);
224 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
225 err = bpf_percpu_array_copy(map, key, value);
226 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
227 err = bpf_percpu_cgroup_storage_copy(map, key, value);
228 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
229 err = bpf_stackmap_copy(map, key, value);
230 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
231 err = bpf_fd_array_map_lookup_elem(map, key, value);
232 } else if (IS_FD_HASH(map)) {
233 err = bpf_fd_htab_map_lookup_elem(map, key, value);
234 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
235 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
236 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
237 map->map_type == BPF_MAP_TYPE_STACK ||
238 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
239 err = map->ops->map_peek_elem(map, value);
240 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
241 /* struct_ops map requires directly updating "value" */
242 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
245 if (map->ops->map_lookup_elem_sys_only)
246 ptr = map->ops->map_lookup_elem_sys_only(map, key);
248 ptr = map->ops->map_lookup_elem(map, key);
255 if (flags & BPF_F_LOCK)
256 /* lock 'ptr' and copy everything but lock */
257 copy_map_value_locked(map, value, ptr, true);
259 copy_map_value(map, value, ptr);
260 /* mask lock and timer, since value wasn't zero inited */
261 check_and_init_map_value(map, value);
266 bpf_enable_instrumentation();
271 /* Please, do not use this function outside from the map creation path
272 * (e.g. in map update path) without taking care of setting the active
273 * memory cgroup (see at bpf_map_kmalloc_node() for example).
275 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
277 /* We really just want to fail instead of triggering OOM killer
278 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
279 * which is used for lower order allocation requests.
281 * It has been observed that higher order allocation requests done by
282 * vmalloc with __GFP_NORETRY being set might fail due to not trying
283 * to reclaim memory from the page cache, thus we set
284 * __GFP_RETRY_MAYFAIL to avoid such situations.
287 gfp_t gfp = bpf_memcg_flags(__GFP_NOWARN | __GFP_ZERO);
288 unsigned int flags = 0;
289 unsigned long align = 1;
292 if (size >= SIZE_MAX)
295 /* kmalloc()'ed memory can't be mmap()'ed */
297 BUG_ON(!PAGE_ALIGNED(size));
300 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
301 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
307 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
308 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
309 flags, numa_node, __builtin_return_address(0));
312 void *bpf_map_area_alloc(u64 size, int numa_node)
314 return __bpf_map_area_alloc(size, numa_node, false);
317 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
319 return __bpf_map_area_alloc(size, numa_node, true);
322 void bpf_map_area_free(void *area)
327 static u32 bpf_map_flags_retain_permanent(u32 flags)
329 /* Some map creation flags are not tied to the map object but
330 * rather to the map fd instead, so they have no meaning upon
331 * map object inspection since multiple file descriptors with
332 * different (access) properties can exist here. Thus, given
333 * this has zero meaning for the map itself, lets clear these
336 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
339 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
341 map->map_type = attr->map_type;
342 map->key_size = attr->key_size;
343 map->value_size = attr->value_size;
344 map->max_entries = attr->max_entries;
345 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
346 map->numa_node = bpf_map_attr_numa_node(attr);
347 map->map_extra = attr->map_extra;
350 static int bpf_map_alloc_id(struct bpf_map *map)
354 idr_preload(GFP_KERNEL);
355 spin_lock_bh(&map_idr_lock);
356 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
359 spin_unlock_bh(&map_idr_lock);
362 if (WARN_ON_ONCE(!id))
365 return id > 0 ? 0 : id;
368 void bpf_map_free_id(struct bpf_map *map)
372 /* Offloaded maps are removed from the IDR store when their device
373 * disappears - even if someone holds an fd to them they are unusable,
374 * the memory is gone, all ops will fail; they are simply waiting for
375 * refcnt to drop to be freed.
380 spin_lock_irqsave(&map_idr_lock, flags);
382 idr_remove(&map_idr, map->id);
385 spin_unlock_irqrestore(&map_idr_lock, flags);
388 #ifdef CONFIG_MEMCG_KMEM
389 static void bpf_map_save_memcg(struct bpf_map *map)
391 /* Currently if a map is created by a process belonging to the root
392 * memory cgroup, get_obj_cgroup_from_current() will return NULL.
393 * So we have to check map->objcg for being NULL each time it's
396 if (memcg_bpf_enabled())
397 map->objcg = get_obj_cgroup_from_current();
400 static void bpf_map_release_memcg(struct bpf_map *map)
403 obj_cgroup_put(map->objcg);
406 static struct mem_cgroup *bpf_map_get_memcg(const struct bpf_map *map)
409 return get_mem_cgroup_from_objcg(map->objcg);
411 return root_mem_cgroup;
414 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
417 struct mem_cgroup *memcg, *old_memcg;
420 memcg = bpf_map_get_memcg(map);
421 old_memcg = set_active_memcg(memcg);
422 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
423 set_active_memcg(old_memcg);
424 mem_cgroup_put(memcg);
429 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
431 struct mem_cgroup *memcg, *old_memcg;
434 memcg = bpf_map_get_memcg(map);
435 old_memcg = set_active_memcg(memcg);
436 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
437 set_active_memcg(old_memcg);
438 mem_cgroup_put(memcg);
443 void *bpf_map_kvcalloc(struct bpf_map *map, size_t n, size_t size,
446 struct mem_cgroup *memcg, *old_memcg;
449 memcg = bpf_map_get_memcg(map);
450 old_memcg = set_active_memcg(memcg);
451 ptr = kvcalloc(n, size, flags | __GFP_ACCOUNT);
452 set_active_memcg(old_memcg);
453 mem_cgroup_put(memcg);
458 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
459 size_t align, 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 = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
467 set_active_memcg(old_memcg);
468 mem_cgroup_put(memcg);
474 static void bpf_map_save_memcg(struct bpf_map *map)
478 static void bpf_map_release_memcg(struct bpf_map *map)
483 int bpf_map_alloc_pages(const struct bpf_map *map, gfp_t gfp, int nid,
484 unsigned long nr_pages, struct page **pages)
489 #ifdef CONFIG_MEMCG_KMEM
490 struct mem_cgroup *memcg, *old_memcg;
492 memcg = bpf_map_get_memcg(map);
493 old_memcg = set_active_memcg(memcg);
495 for (i = 0; i < nr_pages; i++) {
496 pg = alloc_pages_node(nid, gfp | __GFP_ACCOUNT, 0);
502 for (j = 0; j < i; j++)
503 __free_page(pages[j]);
508 #ifdef CONFIG_MEMCG_KMEM
509 set_active_memcg(old_memcg);
510 mem_cgroup_put(memcg);
516 static int btf_field_cmp(const void *a, const void *b)
518 const struct btf_field *f1 = a, *f2 = b;
520 if (f1->offset < f2->offset)
522 else if (f1->offset > f2->offset)
527 struct btf_field *btf_record_find(const struct btf_record *rec, u32 offset,
530 struct btf_field *field;
532 if (IS_ERR_OR_NULL(rec) || !(rec->field_mask & field_mask))
534 field = bsearch(&offset, rec->fields, rec->cnt, sizeof(rec->fields[0]), btf_field_cmp);
535 if (!field || !(field->type & field_mask))
540 void btf_record_free(struct btf_record *rec)
544 if (IS_ERR_OR_NULL(rec))
546 for (i = 0; i < rec->cnt; i++) {
547 switch (rec->fields[i].type) {
550 case BPF_KPTR_PERCPU:
551 if (rec->fields[i].kptr.module)
552 module_put(rec->fields[i].kptr.module);
553 btf_put(rec->fields[i].kptr.btf);
563 /* Nothing to release */
573 void bpf_map_free_record(struct bpf_map *map)
575 btf_record_free(map->record);
579 struct btf_record *btf_record_dup(const struct btf_record *rec)
581 const struct btf_field *fields;
582 struct btf_record *new_rec;
585 if (IS_ERR_OR_NULL(rec))
587 size = offsetof(struct btf_record, fields[rec->cnt]);
588 new_rec = kmemdup(rec, size, GFP_KERNEL | __GFP_NOWARN);
590 return ERR_PTR(-ENOMEM);
591 /* Do a deep copy of the btf_record */
592 fields = rec->fields;
594 for (i = 0; i < rec->cnt; i++) {
595 switch (fields[i].type) {
598 case BPF_KPTR_PERCPU:
599 btf_get(fields[i].kptr.btf);
600 if (fields[i].kptr.module && !try_module_get(fields[i].kptr.module)) {
613 /* Nothing to acquire */
624 btf_record_free(new_rec);
628 bool btf_record_equal(const struct btf_record *rec_a, const struct btf_record *rec_b)
630 bool a_has_fields = !IS_ERR_OR_NULL(rec_a), b_has_fields = !IS_ERR_OR_NULL(rec_b);
633 if (!a_has_fields && !b_has_fields)
635 if (a_has_fields != b_has_fields)
637 if (rec_a->cnt != rec_b->cnt)
639 size = offsetof(struct btf_record, fields[rec_a->cnt]);
640 /* btf_parse_fields uses kzalloc to allocate a btf_record, so unused
641 * members are zeroed out. So memcmp is safe to do without worrying
642 * about padding/unused fields.
644 * While spin_lock, timer, and kptr have no relation to map BTF,
645 * list_head metadata is specific to map BTF, the btf and value_rec
646 * members in particular. btf is the map BTF, while value_rec points to
647 * btf_record in that map BTF.
649 * So while by default, we don't rely on the map BTF (which the records
650 * were parsed from) matching for both records, which is not backwards
651 * compatible, in case list_head is part of it, we implicitly rely on
652 * that by way of depending on memcmp succeeding for it.
654 return !memcmp(rec_a, rec_b, size);
657 void bpf_obj_free_timer(const struct btf_record *rec, void *obj)
659 if (WARN_ON_ONCE(!btf_record_has_field(rec, BPF_TIMER)))
661 bpf_timer_cancel_and_free(obj + rec->timer_off);
664 void bpf_obj_free_workqueue(const struct btf_record *rec, void *obj)
666 if (WARN_ON_ONCE(!btf_record_has_field(rec, BPF_WORKQUEUE)))
668 bpf_wq_cancel_and_free(obj + rec->wq_off);
671 void bpf_obj_free_fields(const struct btf_record *rec, void *obj)
673 const struct btf_field *fields;
676 if (IS_ERR_OR_NULL(rec))
678 fields = rec->fields;
679 for (i = 0; i < rec->cnt; i++) {
680 struct btf_struct_meta *pointee_struct_meta;
681 const struct btf_field *field = &fields[i];
682 void *field_ptr = obj + field->offset;
685 switch (fields[i].type) {
689 bpf_timer_cancel_and_free(field_ptr);
692 bpf_wq_cancel_and_free(field_ptr);
695 WRITE_ONCE(*(u64 *)field_ptr, 0);
698 case BPF_KPTR_PERCPU:
699 xchgd_field = (void *)xchg((unsigned long *)field_ptr, 0);
703 if (!btf_is_kernel(field->kptr.btf)) {
704 pointee_struct_meta = btf_find_struct_meta(field->kptr.btf,
707 __bpf_obj_drop_impl(xchgd_field, pointee_struct_meta ?
708 pointee_struct_meta->record : NULL,
709 fields[i].type == BPF_KPTR_PERCPU);
712 field->kptr.dtor(xchgd_field);
716 if (WARN_ON_ONCE(rec->spin_lock_off < 0))
718 bpf_list_head_free(field, field_ptr, obj + rec->spin_lock_off);
721 if (WARN_ON_ONCE(rec->spin_lock_off < 0))
723 bpf_rb_root_free(field, field_ptr, obj + rec->spin_lock_off);
736 /* called from workqueue */
737 static void bpf_map_free_deferred(struct work_struct *work)
739 struct bpf_map *map = container_of(work, struct bpf_map, work);
740 struct btf_record *rec = map->record;
741 struct btf *btf = map->btf;
743 security_bpf_map_free(map);
744 bpf_map_release_memcg(map);
745 /* implementation dependent freeing */
746 map->ops->map_free(map);
747 /* Delay freeing of btf_record for maps, as map_free
748 * callback usually needs access to them. It is better to do it here
749 * than require each callback to do the free itself manually.
751 * Note that the btf_record stashed in map->inner_map_meta->record was
752 * already freed using the map_free callback for map in map case which
753 * eventually calls bpf_map_free_meta, since inner_map_meta is only a
754 * template bpf_map struct used during verification.
756 btf_record_free(rec);
757 /* Delay freeing of btf for maps, as map_free callback may need
758 * struct_meta info which will be freed with btf_put().
763 static void bpf_map_put_uref(struct bpf_map *map)
765 if (atomic64_dec_and_test(&map->usercnt)) {
766 if (map->ops->map_release_uref)
767 map->ops->map_release_uref(map);
771 static void bpf_map_free_in_work(struct bpf_map *map)
773 INIT_WORK(&map->work, bpf_map_free_deferred);
774 /* Avoid spawning kworkers, since they all might contend
775 * for the same mutex like slab_mutex.
777 queue_work(system_unbound_wq, &map->work);
780 static void bpf_map_free_rcu_gp(struct rcu_head *rcu)
782 bpf_map_free_in_work(container_of(rcu, struct bpf_map, rcu));
785 static void bpf_map_free_mult_rcu_gp(struct rcu_head *rcu)
787 if (rcu_trace_implies_rcu_gp())
788 bpf_map_free_rcu_gp(rcu);
790 call_rcu(rcu, bpf_map_free_rcu_gp);
793 /* decrement map refcnt and schedule it for freeing via workqueue
794 * (underlying map implementation ops->map_free() might sleep)
796 void bpf_map_put(struct bpf_map *map)
798 if (atomic64_dec_and_test(&map->refcnt)) {
799 /* bpf_map_free_id() must be called first */
800 bpf_map_free_id(map);
802 WARN_ON_ONCE(atomic64_read(&map->sleepable_refcnt));
803 if (READ_ONCE(map->free_after_mult_rcu_gp))
804 call_rcu_tasks_trace(&map->rcu, bpf_map_free_mult_rcu_gp);
805 else if (READ_ONCE(map->free_after_rcu_gp))
806 call_rcu(&map->rcu, bpf_map_free_rcu_gp);
808 bpf_map_free_in_work(map);
811 EXPORT_SYMBOL_GPL(bpf_map_put);
813 void bpf_map_put_with_uref(struct bpf_map *map)
815 bpf_map_put_uref(map);
819 static int bpf_map_release(struct inode *inode, struct file *filp)
821 struct bpf_map *map = filp->private_data;
823 if (map->ops->map_release)
824 map->ops->map_release(map, filp);
826 bpf_map_put_with_uref(map);
830 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
832 fmode_t mode = f.file->f_mode;
834 /* Our file permissions may have been overridden by global
835 * map permissions facing syscall side.
837 if (READ_ONCE(map->frozen))
838 mode &= ~FMODE_CAN_WRITE;
842 #ifdef CONFIG_PROC_FS
843 /* Show the memory usage of a bpf map */
844 static u64 bpf_map_memory_usage(const struct bpf_map *map)
846 return map->ops->map_mem_usage(map);
849 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
851 struct bpf_map *map = filp->private_data;
852 u32 type = 0, jited = 0;
854 if (map_type_contains_progs(map)) {
855 spin_lock(&map->owner.lock);
856 type = map->owner.type;
857 jited = map->owner.jited;
858 spin_unlock(&map->owner.lock);
867 "map_extra:\t%#llx\n"
876 (unsigned long long)map->map_extra,
877 bpf_map_memory_usage(map),
879 READ_ONCE(map->frozen));
881 seq_printf(m, "owner_prog_type:\t%u\n", type);
882 seq_printf(m, "owner_jited:\t%u\n", jited);
887 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
890 /* We need this handler such that alloc_file() enables
891 * f_mode with FMODE_CAN_READ.
896 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
897 size_t siz, loff_t *ppos)
899 /* We need this handler such that alloc_file() enables
900 * f_mode with FMODE_CAN_WRITE.
905 /* called for any extra memory-mapped regions (except initial) */
906 static void bpf_map_mmap_open(struct vm_area_struct *vma)
908 struct bpf_map *map = vma->vm_file->private_data;
910 if (vma->vm_flags & VM_MAYWRITE)
911 bpf_map_write_active_inc(map);
914 /* called for all unmapped memory region (including initial) */
915 static void bpf_map_mmap_close(struct vm_area_struct *vma)
917 struct bpf_map *map = vma->vm_file->private_data;
919 if (vma->vm_flags & VM_MAYWRITE)
920 bpf_map_write_active_dec(map);
923 static const struct vm_operations_struct bpf_map_default_vmops = {
924 .open = bpf_map_mmap_open,
925 .close = bpf_map_mmap_close,
928 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
930 struct bpf_map *map = filp->private_data;
933 if (!map->ops->map_mmap || !IS_ERR_OR_NULL(map->record))
936 if (!(vma->vm_flags & VM_SHARED))
939 mutex_lock(&map->freeze_mutex);
941 if (vma->vm_flags & VM_WRITE) {
946 /* map is meant to be read-only, so do not allow mapping as
947 * writable, because it's possible to leak a writable page
948 * reference and allows user-space to still modify it after
949 * freezing, while verifier will assume contents do not change
951 if (map->map_flags & BPF_F_RDONLY_PROG) {
957 /* set default open/close callbacks */
958 vma->vm_ops = &bpf_map_default_vmops;
959 vma->vm_private_data = map;
960 vm_flags_clear(vma, VM_MAYEXEC);
961 if (!(vma->vm_flags & VM_WRITE))
962 /* disallow re-mapping with PROT_WRITE */
963 vm_flags_clear(vma, VM_MAYWRITE);
965 err = map->ops->map_mmap(map, vma);
969 if (vma->vm_flags & VM_MAYWRITE)
970 bpf_map_write_active_inc(map);
972 mutex_unlock(&map->freeze_mutex);
976 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
978 struct bpf_map *map = filp->private_data;
980 if (map->ops->map_poll)
981 return map->ops->map_poll(map, filp, pts);
986 static unsigned long bpf_get_unmapped_area(struct file *filp, unsigned long addr,
987 unsigned long len, unsigned long pgoff,
990 struct bpf_map *map = filp->private_data;
992 if (map->ops->map_get_unmapped_area)
993 return map->ops->map_get_unmapped_area(filp, addr, len, pgoff, flags);
995 return mm_get_unmapped_area(current->mm, filp, addr, len, pgoff, flags);
1001 const struct file_operations bpf_map_fops = {
1002 #ifdef CONFIG_PROC_FS
1003 .show_fdinfo = bpf_map_show_fdinfo,
1005 .release = bpf_map_release,
1006 .read = bpf_dummy_read,
1007 .write = bpf_dummy_write,
1008 .mmap = bpf_map_mmap,
1009 .poll = bpf_map_poll,
1010 .get_unmapped_area = bpf_get_unmapped_area,
1013 int bpf_map_new_fd(struct bpf_map *map, int flags)
1017 ret = security_bpf_map(map, OPEN_FMODE(flags));
1021 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
1025 int bpf_get_file_flag(int flags)
1027 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
1029 if (flags & BPF_F_RDONLY)
1031 if (flags & BPF_F_WRONLY)
1036 /* helper macro to check that unused fields 'union bpf_attr' are zero */
1037 #define CHECK_ATTR(CMD) \
1038 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
1039 sizeof(attr->CMD##_LAST_FIELD), 0, \
1041 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
1042 sizeof(attr->CMD##_LAST_FIELD)) != NULL
1044 /* dst and src must have at least "size" number of bytes.
1045 * Return strlen on success and < 0 on error.
1047 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
1049 const char *end = src + size;
1050 const char *orig_src = src;
1052 memset(dst, 0, size);
1053 /* Copy all isalnum(), '_' and '.' chars. */
1054 while (src < end && *src) {
1055 if (!isalnum(*src) &&
1056 *src != '_' && *src != '.')
1061 /* No '\0' found in "size" number of bytes */
1065 return src - orig_src;
1068 int map_check_no_btf(const struct bpf_map *map,
1069 const struct btf *btf,
1070 const struct btf_type *key_type,
1071 const struct btf_type *value_type)
1076 static int map_check_btf(struct bpf_map *map, struct bpf_token *token,
1077 const struct btf *btf, u32 btf_key_id, u32 btf_value_id)
1079 const struct btf_type *key_type, *value_type;
1080 u32 key_size, value_size;
1083 /* Some maps allow key to be unspecified. */
1085 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
1086 if (!key_type || key_size != map->key_size)
1089 key_type = btf_type_by_id(btf, 0);
1090 if (!map->ops->map_check_btf)
1094 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
1095 if (!value_type || value_size != map->value_size)
1098 map->record = btf_parse_fields(btf, value_type,
1099 BPF_SPIN_LOCK | BPF_TIMER | BPF_KPTR | BPF_LIST_HEAD |
1100 BPF_RB_ROOT | BPF_REFCOUNT | BPF_WORKQUEUE,
1102 if (!IS_ERR_OR_NULL(map->record)) {
1105 if (!bpf_token_capable(token, CAP_BPF)) {
1109 if (map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) {
1113 for (i = 0; i < sizeof(map->record->field_mask) * 8; i++) {
1114 switch (map->record->field_mask & (1 << i)) {
1118 if (map->map_type != BPF_MAP_TYPE_HASH &&
1119 map->map_type != BPF_MAP_TYPE_ARRAY &&
1120 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
1121 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
1122 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
1123 map->map_type != BPF_MAP_TYPE_TASK_STORAGE &&
1124 map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) {
1131 if (map->map_type != BPF_MAP_TYPE_HASH &&
1132 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1133 map->map_type != BPF_MAP_TYPE_ARRAY) {
1138 case BPF_KPTR_UNREF:
1140 case BPF_KPTR_PERCPU:
1142 if (map->map_type != BPF_MAP_TYPE_HASH &&
1143 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
1144 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1145 map->map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH &&
1146 map->map_type != BPF_MAP_TYPE_ARRAY &&
1147 map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY &&
1148 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
1149 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
1150 map->map_type != BPF_MAP_TYPE_TASK_STORAGE &&
1151 map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) {
1158 if (map->map_type != BPF_MAP_TYPE_HASH &&
1159 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1160 map->map_type != BPF_MAP_TYPE_ARRAY) {
1166 /* Fail if map_type checks are missing for a field type */
1173 ret = btf_check_and_fixup_fields(btf, map->record);
1177 if (map->ops->map_check_btf) {
1178 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
1185 bpf_map_free_record(map);
1189 static bool bpf_net_capable(void)
1191 return capable(CAP_NET_ADMIN) || capable(CAP_SYS_ADMIN);
1194 #define BPF_MAP_CREATE_LAST_FIELD map_token_fd
1195 /* called via syscall */
1196 static int map_create(union bpf_attr *attr)
1198 const struct bpf_map_ops *ops;
1199 struct bpf_token *token = NULL;
1200 int numa_node = bpf_map_attr_numa_node(attr);
1201 u32 map_type = attr->map_type;
1202 struct bpf_map *map;
1207 err = CHECK_ATTR(BPF_MAP_CREATE);
1211 /* check BPF_F_TOKEN_FD flag, remember if it's set, and then clear it
1212 * to avoid per-map type checks tripping on unknown flag
1214 token_flag = attr->map_flags & BPF_F_TOKEN_FD;
1215 attr->map_flags &= ~BPF_F_TOKEN_FD;
1217 if (attr->btf_vmlinux_value_type_id) {
1218 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
1219 attr->btf_key_type_id || attr->btf_value_type_id)
1221 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
1225 if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER &&
1226 attr->map_type != BPF_MAP_TYPE_ARENA &&
1227 attr->map_extra != 0)
1230 f_flags = bpf_get_file_flag(attr->map_flags);
1234 if (numa_node != NUMA_NO_NODE &&
1235 ((unsigned int)numa_node >= nr_node_ids ||
1236 !node_online(numa_node)))
1239 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
1240 map_type = attr->map_type;
1241 if (map_type >= ARRAY_SIZE(bpf_map_types))
1243 map_type = array_index_nospec(map_type, ARRAY_SIZE(bpf_map_types));
1244 ops = bpf_map_types[map_type];
1248 if (ops->map_alloc_check) {
1249 err = ops->map_alloc_check(attr);
1253 if (attr->map_ifindex)
1254 ops = &bpf_map_offload_ops;
1255 if (!ops->map_mem_usage)
1259 token = bpf_token_get_from_fd(attr->map_token_fd);
1261 return PTR_ERR(token);
1263 /* if current token doesn't grant map creation permissions,
1264 * then we can't use this token, so ignore it and rely on
1265 * system-wide capabilities checks
1267 if (!bpf_token_allow_cmd(token, BPF_MAP_CREATE) ||
1268 !bpf_token_allow_map_type(token, attr->map_type)) {
1269 bpf_token_put(token);
1276 /* Intent here is for unprivileged_bpf_disabled to block BPF map
1277 * creation for unprivileged users; other actions depend
1278 * on fd availability and access to bpffs, so are dependent on
1279 * object creation success. Even with unprivileged BPF disabled,
1280 * capability checks are still carried out.
1282 if (sysctl_unprivileged_bpf_disabled && !bpf_token_capable(token, CAP_BPF))
1285 /* check privileged map type permissions */
1287 case BPF_MAP_TYPE_ARRAY:
1288 case BPF_MAP_TYPE_PERCPU_ARRAY:
1289 case BPF_MAP_TYPE_PROG_ARRAY:
1290 case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1291 case BPF_MAP_TYPE_CGROUP_ARRAY:
1292 case BPF_MAP_TYPE_ARRAY_OF_MAPS:
1293 case BPF_MAP_TYPE_HASH:
1294 case BPF_MAP_TYPE_PERCPU_HASH:
1295 case BPF_MAP_TYPE_HASH_OF_MAPS:
1296 case BPF_MAP_TYPE_RINGBUF:
1297 case BPF_MAP_TYPE_USER_RINGBUF:
1298 case BPF_MAP_TYPE_CGROUP_STORAGE:
1299 case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
1302 case BPF_MAP_TYPE_SK_STORAGE:
1303 case BPF_MAP_TYPE_INODE_STORAGE:
1304 case BPF_MAP_TYPE_TASK_STORAGE:
1305 case BPF_MAP_TYPE_CGRP_STORAGE:
1306 case BPF_MAP_TYPE_BLOOM_FILTER:
1307 case BPF_MAP_TYPE_LPM_TRIE:
1308 case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
1309 case BPF_MAP_TYPE_STACK_TRACE:
1310 case BPF_MAP_TYPE_QUEUE:
1311 case BPF_MAP_TYPE_STACK:
1312 case BPF_MAP_TYPE_LRU_HASH:
1313 case BPF_MAP_TYPE_LRU_PERCPU_HASH:
1314 case BPF_MAP_TYPE_STRUCT_OPS:
1315 case BPF_MAP_TYPE_CPUMAP:
1316 case BPF_MAP_TYPE_ARENA:
1317 if (!bpf_token_capable(token, CAP_BPF))
1320 case BPF_MAP_TYPE_SOCKMAP:
1321 case BPF_MAP_TYPE_SOCKHASH:
1322 case BPF_MAP_TYPE_DEVMAP:
1323 case BPF_MAP_TYPE_DEVMAP_HASH:
1324 case BPF_MAP_TYPE_XSKMAP:
1325 if (!bpf_token_capable(token, CAP_NET_ADMIN))
1329 WARN(1, "unsupported map type %d", map_type);
1333 map = ops->map_alloc(attr);
1339 map->map_type = map_type;
1341 err = bpf_obj_name_cpy(map->name, attr->map_name,
1342 sizeof(attr->map_name));
1346 atomic64_set(&map->refcnt, 1);
1347 atomic64_set(&map->usercnt, 1);
1348 mutex_init(&map->freeze_mutex);
1349 spin_lock_init(&map->owner.lock);
1351 if (attr->btf_key_type_id || attr->btf_value_type_id ||
1352 /* Even the map's value is a kernel's struct,
1353 * the bpf_prog.o must have BTF to begin with
1354 * to figure out the corresponding kernel's
1355 * counter part. Thus, attr->btf_fd has
1358 attr->btf_vmlinux_value_type_id) {
1361 btf = btf_get_by_fd(attr->btf_fd);
1366 if (btf_is_kernel(btf)) {
1373 if (attr->btf_value_type_id) {
1374 err = map_check_btf(map, token, btf, attr->btf_key_type_id,
1375 attr->btf_value_type_id);
1380 map->btf_key_type_id = attr->btf_key_type_id;
1381 map->btf_value_type_id = attr->btf_value_type_id;
1382 map->btf_vmlinux_value_type_id =
1383 attr->btf_vmlinux_value_type_id;
1386 err = security_bpf_map_create(map, attr, token);
1390 err = bpf_map_alloc_id(map);
1394 bpf_map_save_memcg(map);
1395 bpf_token_put(token);
1397 err = bpf_map_new_fd(map, f_flags);
1399 /* failed to allocate fd.
1400 * bpf_map_put_with_uref() is needed because the above
1401 * bpf_map_alloc_id() has published the map
1402 * to the userspace and the userspace may
1403 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
1405 bpf_map_put_with_uref(map);
1412 security_bpf_map_free(map);
1415 map->ops->map_free(map);
1417 bpf_token_put(token);
1421 /* if error is returned, fd is released.
1422 * On success caller should complete fd access with matching fdput()
1424 struct bpf_map *__bpf_map_get(struct fd f)
1427 return ERR_PTR(-EBADF);
1428 if (f.file->f_op != &bpf_map_fops) {
1430 return ERR_PTR(-EINVAL);
1433 return f.file->private_data;
1436 void bpf_map_inc(struct bpf_map *map)
1438 atomic64_inc(&map->refcnt);
1440 EXPORT_SYMBOL_GPL(bpf_map_inc);
1442 void bpf_map_inc_with_uref(struct bpf_map *map)
1444 atomic64_inc(&map->refcnt);
1445 atomic64_inc(&map->usercnt);
1447 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
1449 struct bpf_map *bpf_map_get(u32 ufd)
1451 struct fd f = fdget(ufd);
1452 struct bpf_map *map;
1454 map = __bpf_map_get(f);
1463 EXPORT_SYMBOL(bpf_map_get);
1465 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
1467 struct fd f = fdget(ufd);
1468 struct bpf_map *map;
1470 map = __bpf_map_get(f);
1474 bpf_map_inc_with_uref(map);
1480 /* map_idr_lock should have been held or the map should have been
1481 * protected by rcu read lock.
1483 struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
1487 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
1489 return ERR_PTR(-ENOENT);
1491 atomic64_inc(&map->usercnt);
1496 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
1498 spin_lock_bh(&map_idr_lock);
1499 map = __bpf_map_inc_not_zero(map, false);
1500 spin_unlock_bh(&map_idr_lock);
1504 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
1506 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1511 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1514 return vmemdup_user(ukey, key_size);
1517 return ERR_PTR(-EINVAL);
1522 static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1525 return kvmemdup_bpfptr(ukey, key_size);
1527 if (!bpfptr_is_null(ukey))
1528 return ERR_PTR(-EINVAL);
1533 /* last field in 'union bpf_attr' used by this command */
1534 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1536 static int map_lookup_elem(union bpf_attr *attr)
1538 void __user *ukey = u64_to_user_ptr(attr->key);
1539 void __user *uvalue = u64_to_user_ptr(attr->value);
1540 int ufd = attr->map_fd;
1541 struct bpf_map *map;
1547 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1550 if (attr->flags & ~BPF_F_LOCK)
1554 map = __bpf_map_get(f);
1556 return PTR_ERR(map);
1557 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1562 if ((attr->flags & BPF_F_LOCK) &&
1563 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1568 key = __bpf_copy_key(ukey, map->key_size);
1574 value_size = bpf_map_value_size(map);
1577 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1581 if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
1582 if (copy_from_user(value, uvalue, value_size))
1585 err = bpf_map_copy_value(map, key, value, attr->flags);
1589 err = bpf_map_copy_value(map, key, value, attr->flags);
1594 if (copy_to_user(uvalue, value, value_size) != 0)
1609 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1611 static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
1613 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1614 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
1615 int ufd = attr->map_fd;
1616 struct bpf_map *map;
1622 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1626 map = __bpf_map_get(f);
1628 return PTR_ERR(map);
1629 bpf_map_write_active_inc(map);
1630 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1635 if ((attr->flags & BPF_F_LOCK) &&
1636 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1641 key = ___bpf_copy_key(ukey, map->key_size);
1647 value_size = bpf_map_value_size(map);
1648 value = kvmemdup_bpfptr(uvalue, value_size);
1649 if (IS_ERR(value)) {
1650 err = PTR_ERR(value);
1654 err = bpf_map_update_value(map, f.file, key, value, attr->flags);
1656 maybe_wait_bpf_programs(map);
1662 bpf_map_write_active_dec(map);
1667 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1669 static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr)
1671 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1672 int ufd = attr->map_fd;
1673 struct bpf_map *map;
1678 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1682 map = __bpf_map_get(f);
1684 return PTR_ERR(map);
1685 bpf_map_write_active_inc(map);
1686 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1691 key = ___bpf_copy_key(ukey, map->key_size);
1697 if (bpf_map_is_offloaded(map)) {
1698 err = bpf_map_offload_delete_elem(map, key);
1700 } else if (IS_FD_PROG_ARRAY(map) ||
1701 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1702 /* These maps require sleepable context */
1703 err = map->ops->map_delete_elem(map, key);
1707 bpf_disable_instrumentation();
1709 err = map->ops->map_delete_elem(map, key);
1711 bpf_enable_instrumentation();
1713 maybe_wait_bpf_programs(map);
1717 bpf_map_write_active_dec(map);
1722 /* last field in 'union bpf_attr' used by this command */
1723 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1725 static int map_get_next_key(union bpf_attr *attr)
1727 void __user *ukey = u64_to_user_ptr(attr->key);
1728 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1729 int ufd = attr->map_fd;
1730 struct bpf_map *map;
1731 void *key, *next_key;
1735 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1739 map = __bpf_map_get(f);
1741 return PTR_ERR(map);
1742 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1748 key = __bpf_copy_key(ukey, map->key_size);
1758 next_key = kvmalloc(map->key_size, GFP_USER);
1762 if (bpf_map_is_offloaded(map)) {
1763 err = bpf_map_offload_get_next_key(map, key, next_key);
1768 err = map->ops->map_get_next_key(map, key, next_key);
1775 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1789 int generic_map_delete_batch(struct bpf_map *map,
1790 const union bpf_attr *attr,
1791 union bpf_attr __user *uattr)
1793 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1798 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1801 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1802 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1806 max_count = attr->batch.count;
1810 if (put_user(0, &uattr->batch.count))
1813 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1817 for (cp = 0; cp < max_count; cp++) {
1819 if (copy_from_user(key, keys + cp * map->key_size,
1823 if (bpf_map_is_offloaded(map)) {
1824 err = bpf_map_offload_delete_elem(map, key);
1828 bpf_disable_instrumentation();
1830 err = map->ops->map_delete_elem(map, key);
1832 bpf_enable_instrumentation();
1837 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1845 int generic_map_update_batch(struct bpf_map *map, struct file *map_file,
1846 const union bpf_attr *attr,
1847 union bpf_attr __user *uattr)
1849 void __user *values = u64_to_user_ptr(attr->batch.values);
1850 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1851 u32 value_size, cp, max_count;
1855 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1858 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1859 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1863 value_size = bpf_map_value_size(map);
1865 max_count = attr->batch.count;
1869 if (put_user(0, &uattr->batch.count))
1872 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1876 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1882 for (cp = 0; cp < max_count; cp++) {
1884 if (copy_from_user(key, keys + cp * map->key_size,
1886 copy_from_user(value, values + cp * value_size, value_size))
1889 err = bpf_map_update_value(map, map_file, key, value,
1890 attr->batch.elem_flags);
1897 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1906 #define MAP_LOOKUP_RETRIES 3
1908 int generic_map_lookup_batch(struct bpf_map *map,
1909 const union bpf_attr *attr,
1910 union bpf_attr __user *uattr)
1912 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1913 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1914 void __user *values = u64_to_user_ptr(attr->batch.values);
1915 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1916 void *buf, *buf_prevkey, *prev_key, *key, *value;
1917 int err, retry = MAP_LOOKUP_RETRIES;
1918 u32 value_size, cp, max_count;
1920 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1923 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1924 !btf_record_has_field(map->record, BPF_SPIN_LOCK))
1927 value_size = bpf_map_value_size(map);
1929 max_count = attr->batch.count;
1933 if (put_user(0, &uattr->batch.count))
1936 buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1940 buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1942 kvfree(buf_prevkey);
1948 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1951 value = key + map->key_size;
1953 prev_key = buf_prevkey;
1955 for (cp = 0; cp < max_count;) {
1957 err = map->ops->map_get_next_key(map, prev_key, key);
1961 err = bpf_map_copy_value(map, key, value,
1962 attr->batch.elem_flags);
1964 if (err == -ENOENT) {
1976 if (copy_to_user(keys + cp * map->key_size, key,
1981 if (copy_to_user(values + cp * value_size, value, value_size)) {
1987 prev_key = buf_prevkey;
1989 swap(prev_key, key);
1990 retry = MAP_LOOKUP_RETRIES;
1998 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1999 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
2003 kvfree(buf_prevkey);
2008 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
2010 static int map_lookup_and_delete_elem(union bpf_attr *attr)
2012 void __user *ukey = u64_to_user_ptr(attr->key);
2013 void __user *uvalue = u64_to_user_ptr(attr->value);
2014 int ufd = attr->map_fd;
2015 struct bpf_map *map;
2021 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
2024 if (attr->flags & ~BPF_F_LOCK)
2028 map = __bpf_map_get(f);
2030 return PTR_ERR(map);
2031 bpf_map_write_active_inc(map);
2032 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
2033 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
2039 (map->map_type == BPF_MAP_TYPE_QUEUE ||
2040 map->map_type == BPF_MAP_TYPE_STACK)) {
2045 if ((attr->flags & BPF_F_LOCK) &&
2046 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
2051 key = __bpf_copy_key(ukey, map->key_size);
2057 value_size = bpf_map_value_size(map);
2060 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
2065 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
2066 map->map_type == BPF_MAP_TYPE_STACK) {
2067 err = map->ops->map_pop_elem(map, value);
2068 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
2069 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
2070 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
2071 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
2072 if (!bpf_map_is_offloaded(map)) {
2073 bpf_disable_instrumentation();
2075 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
2077 bpf_enable_instrumentation();
2084 if (copy_to_user(uvalue, value, value_size) != 0) {
2096 bpf_map_write_active_dec(map);
2101 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
2103 static int map_freeze(const union bpf_attr *attr)
2105 int err = 0, ufd = attr->map_fd;
2106 struct bpf_map *map;
2109 if (CHECK_ATTR(BPF_MAP_FREEZE))
2113 map = __bpf_map_get(f);
2115 return PTR_ERR(map);
2117 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS || !IS_ERR_OR_NULL(map->record)) {
2122 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
2127 mutex_lock(&map->freeze_mutex);
2128 if (bpf_map_write_active(map)) {
2132 if (READ_ONCE(map->frozen)) {
2137 WRITE_ONCE(map->frozen, true);
2139 mutex_unlock(&map->freeze_mutex);
2144 static const struct bpf_prog_ops * const bpf_prog_types[] = {
2145 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
2146 [_id] = & _name ## _prog_ops,
2147 #define BPF_MAP_TYPE(_id, _ops)
2148 #define BPF_LINK_TYPE(_id, _name)
2149 #include <linux/bpf_types.h>
2150 #undef BPF_PROG_TYPE
2152 #undef BPF_LINK_TYPE
2155 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
2157 const struct bpf_prog_ops *ops;
2159 if (type >= ARRAY_SIZE(bpf_prog_types))
2161 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
2162 ops = bpf_prog_types[type];
2166 if (!bpf_prog_is_offloaded(prog->aux))
2167 prog->aux->ops = ops;
2169 prog->aux->ops = &bpf_offload_prog_ops;
2180 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
2181 [BPF_AUDIT_LOAD] = "LOAD",
2182 [BPF_AUDIT_UNLOAD] = "UNLOAD",
2185 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
2187 struct audit_context *ctx = NULL;
2188 struct audit_buffer *ab;
2190 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
2192 if (audit_enabled == AUDIT_OFF)
2194 if (!in_irq() && !irqs_disabled())
2195 ctx = audit_context();
2196 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
2199 audit_log_format(ab, "prog-id=%u op=%s",
2200 prog->aux->id, bpf_audit_str[op]);
2204 static int bpf_prog_alloc_id(struct bpf_prog *prog)
2208 idr_preload(GFP_KERNEL);
2209 spin_lock_bh(&prog_idr_lock);
2210 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
2213 spin_unlock_bh(&prog_idr_lock);
2216 /* id is in [1, INT_MAX) */
2217 if (WARN_ON_ONCE(!id))
2220 return id > 0 ? 0 : id;
2223 void bpf_prog_free_id(struct bpf_prog *prog)
2225 unsigned long flags;
2227 /* cBPF to eBPF migrations are currently not in the idr store.
2228 * Offloaded programs are removed from the store when their device
2229 * disappears - even if someone grabs an fd to them they are unusable,
2230 * simply waiting for refcnt to drop to be freed.
2235 spin_lock_irqsave(&prog_idr_lock, flags);
2236 idr_remove(&prog_idr, prog->aux->id);
2238 spin_unlock_irqrestore(&prog_idr_lock, flags);
2241 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
2243 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
2245 kvfree(aux->func_info);
2246 kfree(aux->func_info_aux);
2247 free_uid(aux->user);
2248 security_bpf_prog_free(aux->prog);
2249 bpf_prog_free(aux->prog);
2252 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
2254 bpf_prog_kallsyms_del_all(prog);
2255 btf_put(prog->aux->btf);
2256 module_put(prog->aux->mod);
2257 kvfree(prog->aux->jited_linfo);
2258 kvfree(prog->aux->linfo);
2259 kfree(prog->aux->kfunc_tab);
2260 if (prog->aux->attach_btf)
2261 btf_put(prog->aux->attach_btf);
2264 if (prog->sleepable)
2265 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
2267 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
2269 __bpf_prog_put_rcu(&prog->aux->rcu);
2273 static void bpf_prog_put_deferred(struct work_struct *work)
2275 struct bpf_prog_aux *aux;
2276 struct bpf_prog *prog;
2278 aux = container_of(work, struct bpf_prog_aux, work);
2280 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
2281 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
2282 bpf_prog_free_id(prog);
2283 __bpf_prog_put_noref(prog, true);
2286 static void __bpf_prog_put(struct bpf_prog *prog)
2288 struct bpf_prog_aux *aux = prog->aux;
2290 if (atomic64_dec_and_test(&aux->refcnt)) {
2291 if (in_irq() || irqs_disabled()) {
2292 INIT_WORK(&aux->work, bpf_prog_put_deferred);
2293 schedule_work(&aux->work);
2295 bpf_prog_put_deferred(&aux->work);
2300 void bpf_prog_put(struct bpf_prog *prog)
2302 __bpf_prog_put(prog);
2304 EXPORT_SYMBOL_GPL(bpf_prog_put);
2306 static int bpf_prog_release(struct inode *inode, struct file *filp)
2308 struct bpf_prog *prog = filp->private_data;
2314 struct bpf_prog_kstats {
2320 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog)
2322 struct bpf_prog_stats *stats;
2325 stats = this_cpu_ptr(prog->stats);
2326 flags = u64_stats_update_begin_irqsave(&stats->syncp);
2327 u64_stats_inc(&stats->misses);
2328 u64_stats_update_end_irqrestore(&stats->syncp, flags);
2331 static void bpf_prog_get_stats(const struct bpf_prog *prog,
2332 struct bpf_prog_kstats *stats)
2334 u64 nsecs = 0, cnt = 0, misses = 0;
2337 for_each_possible_cpu(cpu) {
2338 const struct bpf_prog_stats *st;
2340 u64 tnsecs, tcnt, tmisses;
2342 st = per_cpu_ptr(prog->stats, cpu);
2344 start = u64_stats_fetch_begin(&st->syncp);
2345 tnsecs = u64_stats_read(&st->nsecs);
2346 tcnt = u64_stats_read(&st->cnt);
2347 tmisses = u64_stats_read(&st->misses);
2348 } while (u64_stats_fetch_retry(&st->syncp, start));
2353 stats->nsecs = nsecs;
2355 stats->misses = misses;
2358 #ifdef CONFIG_PROC_FS
2359 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
2361 const struct bpf_prog *prog = filp->private_data;
2362 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2363 struct bpf_prog_kstats stats;
2365 bpf_prog_get_stats(prog, &stats);
2366 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2373 "run_time_ns:\t%llu\n"
2375 "recursion_misses:\t%llu\n"
2376 "verified_insns:\t%u\n",
2380 prog->pages * 1ULL << PAGE_SHIFT,
2385 prog->aux->verified_insns);
2389 const struct file_operations bpf_prog_fops = {
2390 #ifdef CONFIG_PROC_FS
2391 .show_fdinfo = bpf_prog_show_fdinfo,
2393 .release = bpf_prog_release,
2394 .read = bpf_dummy_read,
2395 .write = bpf_dummy_write,
2398 int bpf_prog_new_fd(struct bpf_prog *prog)
2402 ret = security_bpf_prog(prog);
2406 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
2407 O_RDWR | O_CLOEXEC);
2410 static struct bpf_prog *____bpf_prog_get(struct fd f)
2413 return ERR_PTR(-EBADF);
2414 if (f.file->f_op != &bpf_prog_fops) {
2416 return ERR_PTR(-EINVAL);
2419 return f.file->private_data;
2422 void bpf_prog_add(struct bpf_prog *prog, int i)
2424 atomic64_add(i, &prog->aux->refcnt);
2426 EXPORT_SYMBOL_GPL(bpf_prog_add);
2428 void bpf_prog_sub(struct bpf_prog *prog, int i)
2430 /* Only to be used for undoing previous bpf_prog_add() in some
2431 * error path. We still know that another entity in our call
2432 * path holds a reference to the program, thus atomic_sub() can
2433 * be safely used in such cases!
2435 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
2437 EXPORT_SYMBOL_GPL(bpf_prog_sub);
2439 void bpf_prog_inc(struct bpf_prog *prog)
2441 atomic64_inc(&prog->aux->refcnt);
2443 EXPORT_SYMBOL_GPL(bpf_prog_inc);
2445 /* prog_idr_lock should have been held */
2446 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
2450 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
2453 return ERR_PTR(-ENOENT);
2457 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
2459 bool bpf_prog_get_ok(struct bpf_prog *prog,
2460 enum bpf_prog_type *attach_type, bool attach_drv)
2462 /* not an attachment, just a refcount inc, always allow */
2466 if (prog->type != *attach_type)
2468 if (bpf_prog_is_offloaded(prog->aux) && !attach_drv)
2474 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
2477 struct fd f = fdget(ufd);
2478 struct bpf_prog *prog;
2480 prog = ____bpf_prog_get(f);
2483 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
2484 prog = ERR_PTR(-EINVAL);
2494 struct bpf_prog *bpf_prog_get(u32 ufd)
2496 return __bpf_prog_get(ufd, NULL, false);
2499 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
2502 return __bpf_prog_get(ufd, &type, attach_drv);
2504 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
2506 /* Initially all BPF programs could be loaded w/o specifying
2507 * expected_attach_type. Later for some of them specifying expected_attach_type
2508 * at load time became required so that program could be validated properly.
2509 * Programs of types that are allowed to be loaded both w/ and w/o (for
2510 * backward compatibility) expected_attach_type, should have the default attach
2511 * type assigned to expected_attach_type for the latter case, so that it can be
2512 * validated later at attach time.
2514 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
2515 * prog type requires it but has some attach types that have to be backward
2518 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
2520 switch (attr->prog_type) {
2521 case BPF_PROG_TYPE_CGROUP_SOCK:
2522 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
2523 * exist so checking for non-zero is the way to go here.
2525 if (!attr->expected_attach_type)
2526 attr->expected_attach_type =
2527 BPF_CGROUP_INET_SOCK_CREATE;
2529 case BPF_PROG_TYPE_SK_REUSEPORT:
2530 if (!attr->expected_attach_type)
2531 attr->expected_attach_type =
2532 BPF_SK_REUSEPORT_SELECT;
2538 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2539 enum bpf_attach_type expected_attach_type,
2540 struct btf *attach_btf, u32 btf_id,
2541 struct bpf_prog *dst_prog)
2544 if (btf_id > BTF_MAX_TYPE)
2547 if (!attach_btf && !dst_prog)
2550 switch (prog_type) {
2551 case BPF_PROG_TYPE_TRACING:
2552 case BPF_PROG_TYPE_LSM:
2553 case BPF_PROG_TYPE_STRUCT_OPS:
2554 case BPF_PROG_TYPE_EXT:
2561 if (attach_btf && (!btf_id || dst_prog))
2564 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
2565 prog_type != BPF_PROG_TYPE_EXT)
2568 switch (prog_type) {
2569 case BPF_PROG_TYPE_CGROUP_SOCK:
2570 switch (expected_attach_type) {
2571 case BPF_CGROUP_INET_SOCK_CREATE:
2572 case BPF_CGROUP_INET_SOCK_RELEASE:
2573 case BPF_CGROUP_INET4_POST_BIND:
2574 case BPF_CGROUP_INET6_POST_BIND:
2579 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2580 switch (expected_attach_type) {
2581 case BPF_CGROUP_INET4_BIND:
2582 case BPF_CGROUP_INET6_BIND:
2583 case BPF_CGROUP_INET4_CONNECT:
2584 case BPF_CGROUP_INET6_CONNECT:
2585 case BPF_CGROUP_UNIX_CONNECT:
2586 case BPF_CGROUP_INET4_GETPEERNAME:
2587 case BPF_CGROUP_INET6_GETPEERNAME:
2588 case BPF_CGROUP_UNIX_GETPEERNAME:
2589 case BPF_CGROUP_INET4_GETSOCKNAME:
2590 case BPF_CGROUP_INET6_GETSOCKNAME:
2591 case BPF_CGROUP_UNIX_GETSOCKNAME:
2592 case BPF_CGROUP_UDP4_SENDMSG:
2593 case BPF_CGROUP_UDP6_SENDMSG:
2594 case BPF_CGROUP_UNIX_SENDMSG:
2595 case BPF_CGROUP_UDP4_RECVMSG:
2596 case BPF_CGROUP_UDP6_RECVMSG:
2597 case BPF_CGROUP_UNIX_RECVMSG:
2602 case BPF_PROG_TYPE_CGROUP_SKB:
2603 switch (expected_attach_type) {
2604 case BPF_CGROUP_INET_INGRESS:
2605 case BPF_CGROUP_INET_EGRESS:
2610 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2611 switch (expected_attach_type) {
2612 case BPF_CGROUP_SETSOCKOPT:
2613 case BPF_CGROUP_GETSOCKOPT:
2618 case BPF_PROG_TYPE_SK_LOOKUP:
2619 if (expected_attach_type == BPF_SK_LOOKUP)
2622 case BPF_PROG_TYPE_SK_REUSEPORT:
2623 switch (expected_attach_type) {
2624 case BPF_SK_REUSEPORT_SELECT:
2625 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2630 case BPF_PROG_TYPE_NETFILTER:
2631 if (expected_attach_type == BPF_NETFILTER)
2634 case BPF_PROG_TYPE_SYSCALL:
2635 case BPF_PROG_TYPE_EXT:
2636 if (expected_attach_type)
2644 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2646 switch (prog_type) {
2647 case BPF_PROG_TYPE_SCHED_CLS:
2648 case BPF_PROG_TYPE_SCHED_ACT:
2649 case BPF_PROG_TYPE_XDP:
2650 case BPF_PROG_TYPE_LWT_IN:
2651 case BPF_PROG_TYPE_LWT_OUT:
2652 case BPF_PROG_TYPE_LWT_XMIT:
2653 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2654 case BPF_PROG_TYPE_SK_SKB:
2655 case BPF_PROG_TYPE_SK_MSG:
2656 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2657 case BPF_PROG_TYPE_CGROUP_DEVICE:
2658 case BPF_PROG_TYPE_CGROUP_SOCK:
2659 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2660 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2661 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2662 case BPF_PROG_TYPE_SOCK_OPS:
2663 case BPF_PROG_TYPE_EXT: /* extends any prog */
2664 case BPF_PROG_TYPE_NETFILTER:
2666 case BPF_PROG_TYPE_CGROUP_SKB:
2668 case BPF_PROG_TYPE_SK_REUSEPORT:
2669 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2675 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2677 switch (prog_type) {
2678 case BPF_PROG_TYPE_KPROBE:
2679 case BPF_PROG_TYPE_TRACEPOINT:
2680 case BPF_PROG_TYPE_PERF_EVENT:
2681 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2682 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2683 case BPF_PROG_TYPE_TRACING:
2684 case BPF_PROG_TYPE_LSM:
2685 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2686 case BPF_PROG_TYPE_EXT: /* extends any prog */
2693 /* last field in 'union bpf_attr' used by this command */
2694 #define BPF_PROG_LOAD_LAST_FIELD prog_token_fd
2696 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
2698 enum bpf_prog_type type = attr->prog_type;
2699 struct bpf_prog *prog, *dst_prog = NULL;
2700 struct btf *attach_btf = NULL;
2701 struct bpf_token *token = NULL;
2706 if (CHECK_ATTR(BPF_PROG_LOAD))
2709 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2710 BPF_F_ANY_ALIGNMENT |
2711 BPF_F_TEST_STATE_FREQ |
2713 BPF_F_TEST_RND_HI32 |
2714 BPF_F_XDP_HAS_FRAGS |
2715 BPF_F_XDP_DEV_BOUND_ONLY |
2716 BPF_F_TEST_REG_INVARIANTS |
2720 bpf_prog_load_fixup_attach_type(attr);
2722 if (attr->prog_flags & BPF_F_TOKEN_FD) {
2723 token = bpf_token_get_from_fd(attr->prog_token_fd);
2725 return PTR_ERR(token);
2726 /* if current token doesn't grant prog loading permissions,
2727 * then we can't use this token, so ignore it and rely on
2728 * system-wide capabilities checks
2730 if (!bpf_token_allow_cmd(token, BPF_PROG_LOAD) ||
2731 !bpf_token_allow_prog_type(token, attr->prog_type,
2732 attr->expected_attach_type)) {
2733 bpf_token_put(token);
2738 bpf_cap = bpf_token_capable(token, CAP_BPF);
2741 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2742 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2746 /* Intent here is for unprivileged_bpf_disabled to block BPF program
2747 * creation for unprivileged users; other actions depend
2748 * on fd availability and access to bpffs, so are dependent on
2749 * object creation success. Even with unprivileged BPF disabled,
2750 * capability checks are still carried out for these
2751 * and other operations.
2753 if (sysctl_unprivileged_bpf_disabled && !bpf_cap)
2756 if (attr->insn_cnt == 0 ||
2757 attr->insn_cnt > (bpf_cap ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) {
2761 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2762 type != BPF_PROG_TYPE_CGROUP_SKB &&
2766 if (is_net_admin_prog_type(type) && !bpf_token_capable(token, CAP_NET_ADMIN))
2768 if (is_perfmon_prog_type(type) && !bpf_token_capable(token, CAP_PERFMON))
2771 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2772 * or btf, we need to check which one it is
2774 if (attr->attach_prog_fd) {
2775 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2776 if (IS_ERR(dst_prog)) {
2778 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2779 if (IS_ERR(attach_btf)) {
2783 if (!btf_is_kernel(attach_btf)) {
2784 /* attaching through specifying bpf_prog's BTF
2785 * objects directly might be supported eventually
2787 btf_put(attach_btf);
2792 } else if (attr->attach_btf_id) {
2793 /* fall back to vmlinux BTF, if BTF type ID is specified */
2794 attach_btf = bpf_get_btf_vmlinux();
2795 if (IS_ERR(attach_btf)) {
2796 err = PTR_ERR(attach_btf);
2803 btf_get(attach_btf);
2806 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2807 attach_btf, attr->attach_btf_id,
2810 bpf_prog_put(dst_prog);
2812 btf_put(attach_btf);
2817 /* plain bpf_prog allocation */
2818 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2821 bpf_prog_put(dst_prog);
2823 btf_put(attach_btf);
2828 prog->expected_attach_type = attr->expected_attach_type;
2829 prog->sleepable = !!(attr->prog_flags & BPF_F_SLEEPABLE);
2830 prog->aux->attach_btf = attach_btf;
2831 prog->aux->attach_btf_id = attr->attach_btf_id;
2832 prog->aux->dst_prog = dst_prog;
2833 prog->aux->dev_bound = !!attr->prog_ifindex;
2834 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
2836 /* move token into prog->aux, reuse taken refcnt */
2837 prog->aux->token = token;
2840 prog->aux->user = get_current_user();
2841 prog->len = attr->insn_cnt;
2844 if (copy_from_bpfptr(prog->insns,
2845 make_bpfptr(attr->insns, uattr.is_kernel),
2846 bpf_prog_insn_size(prog)) != 0)
2848 /* copy eBPF program license from user space */
2849 if (strncpy_from_bpfptr(license,
2850 make_bpfptr(attr->license, uattr.is_kernel),
2851 sizeof(license) - 1) < 0)
2853 license[sizeof(license) - 1] = 0;
2855 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2856 prog->gpl_compatible = license_is_gpl_compatible(license) ? 1 : 0;
2858 prog->orig_prog = NULL;
2861 atomic64_set(&prog->aux->refcnt, 1);
2863 if (bpf_prog_is_dev_bound(prog->aux)) {
2864 err = bpf_prog_dev_bound_init(prog, attr);
2869 if (type == BPF_PROG_TYPE_EXT && dst_prog &&
2870 bpf_prog_is_dev_bound(dst_prog->aux)) {
2871 err = bpf_prog_dev_bound_inherit(prog, dst_prog);
2877 * Bookkeeping for managing the program attachment chain.
2879 * It might be tempting to set attach_tracing_prog flag at the attachment
2880 * time, but this will not prevent from loading bunch of tracing prog
2881 * first, then attach them one to another.
2883 * The flag attach_tracing_prog is set for the whole program lifecycle, and
2884 * doesn't have to be cleared in bpf_tracing_link_release, since tracing
2885 * programs cannot change attachment target.
2887 if (type == BPF_PROG_TYPE_TRACING && dst_prog &&
2888 dst_prog->type == BPF_PROG_TYPE_TRACING) {
2889 prog->aux->attach_tracing_prog = true;
2892 /* find program type: socket_filter vs tracing_filter */
2893 err = find_prog_type(type, prog);
2897 prog->aux->load_time = ktime_get_boottime_ns();
2898 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2899 sizeof(attr->prog_name));
2903 err = security_bpf_prog_load(prog, attr, token);
2907 /* run eBPF verifier */
2908 err = bpf_check(&prog, attr, uattr, uattr_size);
2910 goto free_used_maps;
2912 prog = bpf_prog_select_runtime(prog, &err);
2914 goto free_used_maps;
2916 err = bpf_prog_alloc_id(prog);
2918 goto free_used_maps;
2920 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2921 * effectively publicly exposed. However, retrieving via
2922 * bpf_prog_get_fd_by_id() will take another reference,
2923 * therefore it cannot be gone underneath us.
2925 * Only for the time /after/ successful bpf_prog_new_fd()
2926 * and before returning to userspace, we might just hold
2927 * one reference and any parallel close on that fd could
2928 * rip everything out. Hence, below notifications must
2929 * happen before bpf_prog_new_fd().
2931 * Also, any failure handling from this point onwards must
2932 * be using bpf_prog_put() given the program is exposed.
2934 bpf_prog_kallsyms_add(prog);
2935 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2936 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2938 err = bpf_prog_new_fd(prog);
2944 /* In case we have subprogs, we need to wait for a grace
2945 * period before we can tear down JIT memory since symbols
2946 * are already exposed under kallsyms.
2948 __bpf_prog_put_noref(prog, prog->aux->real_func_cnt);
2952 security_bpf_prog_free(prog);
2954 free_uid(prog->aux->user);
2955 if (prog->aux->attach_btf)
2956 btf_put(prog->aux->attach_btf);
2957 bpf_prog_free(prog);
2959 bpf_token_put(token);
2963 #define BPF_OBJ_LAST_FIELD path_fd
2965 static int bpf_obj_pin(const union bpf_attr *attr)
2969 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags & ~BPF_F_PATH_FD)
2972 /* path_fd has to be accompanied by BPF_F_PATH_FD flag */
2973 if (!(attr->file_flags & BPF_F_PATH_FD) && attr->path_fd)
2976 path_fd = attr->file_flags & BPF_F_PATH_FD ? attr->path_fd : AT_FDCWD;
2977 return bpf_obj_pin_user(attr->bpf_fd, path_fd,
2978 u64_to_user_ptr(attr->pathname));
2981 static int bpf_obj_get(const union bpf_attr *attr)
2985 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2986 attr->file_flags & ~(BPF_OBJ_FLAG_MASK | BPF_F_PATH_FD))
2989 /* path_fd has to be accompanied by BPF_F_PATH_FD flag */
2990 if (!(attr->file_flags & BPF_F_PATH_FD) && attr->path_fd)
2993 path_fd = attr->file_flags & BPF_F_PATH_FD ? attr->path_fd : AT_FDCWD;
2994 return bpf_obj_get_user(path_fd, u64_to_user_ptr(attr->pathname),
2998 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2999 const struct bpf_link_ops *ops, struct bpf_prog *prog)
3001 atomic64_set(&link->refcnt, 1);
3008 static void bpf_link_free_id(int id)
3013 spin_lock_bh(&link_idr_lock);
3014 idr_remove(&link_idr, id);
3015 spin_unlock_bh(&link_idr_lock);
3018 /* Clean up bpf_link and corresponding anon_inode file and FD. After
3019 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
3020 * anon_inode's release() call. This helper marks bpf_link as
3021 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
3022 * is not decremented, it's the responsibility of a calling code that failed
3023 * to complete bpf_link initialization.
3024 * This helper eventually calls link's dealloc callback, but does not call
3025 * link's release callback.
3027 void bpf_link_cleanup(struct bpf_link_primer *primer)
3029 primer->link->prog = NULL;
3030 bpf_link_free_id(primer->id);
3032 put_unused_fd(primer->fd);
3035 void bpf_link_inc(struct bpf_link *link)
3037 atomic64_inc(&link->refcnt);
3040 static void bpf_link_defer_dealloc_rcu_gp(struct rcu_head *rcu)
3042 struct bpf_link *link = container_of(rcu, struct bpf_link, rcu);
3044 /* free bpf_link and its containing memory */
3045 link->ops->dealloc_deferred(link);
3048 static void bpf_link_defer_dealloc_mult_rcu_gp(struct rcu_head *rcu)
3050 if (rcu_trace_implies_rcu_gp())
3051 bpf_link_defer_dealloc_rcu_gp(rcu);
3053 call_rcu(rcu, bpf_link_defer_dealloc_rcu_gp);
3056 /* bpf_link_free is guaranteed to be called from process context */
3057 static void bpf_link_free(struct bpf_link *link)
3059 bool sleepable = false;
3061 bpf_link_free_id(link->id);
3063 sleepable = link->prog->sleepable;
3064 /* detach BPF program, clean up used resources */
3065 link->ops->release(link);
3066 bpf_prog_put(link->prog);
3068 if (link->ops->dealloc_deferred) {
3069 /* schedule BPF link deallocation; if underlying BPF program
3070 * is sleepable, we need to first wait for RCU tasks trace
3071 * sync, then go through "classic" RCU grace period
3074 call_rcu_tasks_trace(&link->rcu, bpf_link_defer_dealloc_mult_rcu_gp);
3076 call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp);
3078 if (link->ops->dealloc)
3079 link->ops->dealloc(link);
3082 static void bpf_link_put_deferred(struct work_struct *work)
3084 struct bpf_link *link = container_of(work, struct bpf_link, work);
3086 bpf_link_free(link);
3089 /* bpf_link_put might be called from atomic context. It needs to be called
3090 * from sleepable context in order to acquire sleeping locks during the process.
3092 void bpf_link_put(struct bpf_link *link)
3094 if (!atomic64_dec_and_test(&link->refcnt))
3097 INIT_WORK(&link->work, bpf_link_put_deferred);
3098 schedule_work(&link->work);
3100 EXPORT_SYMBOL(bpf_link_put);
3102 static void bpf_link_put_direct(struct bpf_link *link)
3104 if (!atomic64_dec_and_test(&link->refcnt))
3106 bpf_link_free(link);
3109 static int bpf_link_release(struct inode *inode, struct file *filp)
3111 struct bpf_link *link = filp->private_data;
3113 bpf_link_put_direct(link);
3117 #ifdef CONFIG_PROC_FS
3118 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
3119 #define BPF_MAP_TYPE(_id, _ops)
3120 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
3121 static const char *bpf_link_type_strs[] = {
3122 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
3123 #include <linux/bpf_types.h>
3125 #undef BPF_PROG_TYPE
3127 #undef BPF_LINK_TYPE
3129 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
3131 const struct bpf_link *link = filp->private_data;
3132 const struct bpf_prog *prog = link->prog;
3133 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
3138 bpf_link_type_strs[link->type],
3141 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
3148 if (link->ops->show_fdinfo)
3149 link->ops->show_fdinfo(link, m);
3153 static const struct file_operations bpf_link_fops = {
3154 #ifdef CONFIG_PROC_FS
3155 .show_fdinfo = bpf_link_show_fdinfo,
3157 .release = bpf_link_release,
3158 .read = bpf_dummy_read,
3159 .write = bpf_dummy_write,
3162 static int bpf_link_alloc_id(struct bpf_link *link)
3166 idr_preload(GFP_KERNEL);
3167 spin_lock_bh(&link_idr_lock);
3168 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
3169 spin_unlock_bh(&link_idr_lock);
3175 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
3176 * reserving unused FD and allocating ID from link_idr. This is to be paired
3177 * with bpf_link_settle() to install FD and ID and expose bpf_link to
3178 * user-space, if bpf_link is successfully attached. If not, bpf_link and
3179 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
3180 * transient state is passed around in struct bpf_link_primer.
3181 * This is preferred way to create and initialize bpf_link, especially when
3182 * there are complicated and expensive operations in between creating bpf_link
3183 * itself and attaching it to BPF hook. By using bpf_link_prime() and
3184 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
3185 * expensive (and potentially failing) roll back operations in a rare case
3186 * that file, FD, or ID can't be allocated.
3188 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
3193 fd = get_unused_fd_flags(O_CLOEXEC);
3198 id = bpf_link_alloc_id(link);
3204 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
3206 bpf_link_free_id(id);
3208 return PTR_ERR(file);
3211 primer->link = link;
3212 primer->file = file;
3218 int bpf_link_settle(struct bpf_link_primer *primer)
3220 /* make bpf_link fetchable by ID */
3221 spin_lock_bh(&link_idr_lock);
3222 primer->link->id = primer->id;
3223 spin_unlock_bh(&link_idr_lock);
3224 /* make bpf_link fetchable by FD */
3225 fd_install(primer->fd, primer->file);
3226 /* pass through installed FD */
3230 int bpf_link_new_fd(struct bpf_link *link)
3232 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
3235 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
3237 struct fd f = fdget(ufd);
3238 struct bpf_link *link;
3241 return ERR_PTR(-EBADF);
3242 if (f.file->f_op != &bpf_link_fops) {
3244 return ERR_PTR(-EINVAL);
3247 link = f.file->private_data;
3253 EXPORT_SYMBOL(bpf_link_get_from_fd);
3255 static void bpf_tracing_link_release(struct bpf_link *link)
3257 struct bpf_tracing_link *tr_link =
3258 container_of(link, struct bpf_tracing_link, link.link);
3260 WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link,
3261 tr_link->trampoline));
3263 bpf_trampoline_put(tr_link->trampoline);
3265 /* tgt_prog is NULL if target is a kernel function */
3266 if (tr_link->tgt_prog)
3267 bpf_prog_put(tr_link->tgt_prog);
3270 static void bpf_tracing_link_dealloc(struct bpf_link *link)
3272 struct bpf_tracing_link *tr_link =
3273 container_of(link, struct bpf_tracing_link, link.link);
3278 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
3279 struct seq_file *seq)
3281 struct bpf_tracing_link *tr_link =
3282 container_of(link, struct bpf_tracing_link, link.link);
3283 u32 target_btf_id, target_obj_id;
3285 bpf_trampoline_unpack_key(tr_link->trampoline->key,
3286 &target_obj_id, &target_btf_id);
3288 "attach_type:\t%d\n"
3289 "target_obj_id:\t%u\n"
3290 "target_btf_id:\t%u\n",
3291 tr_link->attach_type,
3296 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
3297 struct bpf_link_info *info)
3299 struct bpf_tracing_link *tr_link =
3300 container_of(link, struct bpf_tracing_link, link.link);
3302 info->tracing.attach_type = tr_link->attach_type;
3303 bpf_trampoline_unpack_key(tr_link->trampoline->key,
3304 &info->tracing.target_obj_id,
3305 &info->tracing.target_btf_id);
3310 static const struct bpf_link_ops bpf_tracing_link_lops = {
3311 .release = bpf_tracing_link_release,
3312 .dealloc = bpf_tracing_link_dealloc,
3313 .show_fdinfo = bpf_tracing_link_show_fdinfo,
3314 .fill_link_info = bpf_tracing_link_fill_link_info,
3317 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
3322 struct bpf_link_primer link_primer;
3323 struct bpf_prog *tgt_prog = NULL;
3324 struct bpf_trampoline *tr = NULL;
3325 struct bpf_tracing_link *link;
3329 switch (prog->type) {
3330 case BPF_PROG_TYPE_TRACING:
3331 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
3332 prog->expected_attach_type != BPF_TRACE_FEXIT &&
3333 prog->expected_attach_type != BPF_MODIFY_RETURN) {
3338 case BPF_PROG_TYPE_EXT:
3339 if (prog->expected_attach_type != 0) {
3344 case BPF_PROG_TYPE_LSM:
3345 if (prog->expected_attach_type != BPF_LSM_MAC) {
3355 if (!!tgt_prog_fd != !!btf_id) {
3362 * For now we only allow new targets for BPF_PROG_TYPE_EXT. If this
3363 * part would be changed to implement the same for
3364 * BPF_PROG_TYPE_TRACING, do not forget to update the way how
3365 * attach_tracing_prog flag is set.
3367 if (prog->type != BPF_PROG_TYPE_EXT) {
3372 tgt_prog = bpf_prog_get(tgt_prog_fd);
3373 if (IS_ERR(tgt_prog)) {
3374 err = PTR_ERR(tgt_prog);
3379 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
3382 link = kzalloc(sizeof(*link), GFP_USER);
3387 bpf_link_init(&link->link.link, BPF_LINK_TYPE_TRACING,
3388 &bpf_tracing_link_lops, prog);
3389 link->attach_type = prog->expected_attach_type;
3390 link->link.cookie = bpf_cookie;
3392 mutex_lock(&prog->aux->dst_mutex);
3394 /* There are a few possible cases here:
3396 * - if prog->aux->dst_trampoline is set, the program was just loaded
3397 * and not yet attached to anything, so we can use the values stored
3400 * - if prog->aux->dst_trampoline is NULL, the program has already been
3401 * attached to a target and its initial target was cleared (below)
3403 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
3404 * target_btf_id using the link_create API.
3406 * - if tgt_prog == NULL when this function was called using the old
3407 * raw_tracepoint_open API, and we need a target from prog->aux
3409 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
3410 * was detached and is going for re-attachment.
3412 * - if prog->aux->dst_trampoline is NULL and tgt_prog and prog->aux->attach_btf
3413 * are NULL, then program was already attached and user did not provide
3414 * tgt_prog_fd so we have no way to find out or create trampoline
3416 if (!prog->aux->dst_trampoline && !tgt_prog) {
3418 * Allow re-attach for TRACING and LSM programs. If it's
3419 * currently linked, bpf_trampoline_link_prog will fail.
3420 * EXT programs need to specify tgt_prog_fd, so they
3421 * re-attach in separate code path.
3423 if (prog->type != BPF_PROG_TYPE_TRACING &&
3424 prog->type != BPF_PROG_TYPE_LSM) {
3428 /* We can allow re-attach only if we have valid attach_btf. */
3429 if (!prog->aux->attach_btf) {
3433 btf_id = prog->aux->attach_btf_id;
3434 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
3437 if (!prog->aux->dst_trampoline ||
3438 (key && key != prog->aux->dst_trampoline->key)) {
3439 /* If there is no saved target, or the specified target is
3440 * different from the destination specified at load time, we
3441 * need a new trampoline and a check for compatibility
3443 struct bpf_attach_target_info tgt_info = {};
3445 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
3450 if (tgt_info.tgt_mod) {
3451 module_put(prog->aux->mod);
3452 prog->aux->mod = tgt_info.tgt_mod;
3455 tr = bpf_trampoline_get(key, &tgt_info);
3461 /* The caller didn't specify a target, or the target was the
3462 * same as the destination supplied during program load. This
3463 * means we can reuse the trampoline and reference from program
3464 * load time, and there is no need to allocate a new one. This
3465 * can only happen once for any program, as the saved values in
3466 * prog->aux are cleared below.
3468 tr = prog->aux->dst_trampoline;
3469 tgt_prog = prog->aux->dst_prog;
3472 err = bpf_link_prime(&link->link.link, &link_primer);
3476 err = bpf_trampoline_link_prog(&link->link, tr);
3478 bpf_link_cleanup(&link_primer);
3483 link->tgt_prog = tgt_prog;
3484 link->trampoline = tr;
3486 /* Always clear the trampoline and target prog from prog->aux to make
3487 * sure the original attach destination is not kept alive after a
3488 * program is (re-)attached to another target.
3490 if (prog->aux->dst_prog &&
3491 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
3492 /* got extra prog ref from syscall, or attaching to different prog */
3493 bpf_prog_put(prog->aux->dst_prog);
3494 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
3495 /* we allocated a new trampoline, so free the old one */
3496 bpf_trampoline_put(prog->aux->dst_trampoline);
3498 prog->aux->dst_prog = NULL;
3499 prog->aux->dst_trampoline = NULL;
3500 mutex_unlock(&prog->aux->dst_mutex);
3502 return bpf_link_settle(&link_primer);
3504 if (tr && tr != prog->aux->dst_trampoline)
3505 bpf_trampoline_put(tr);
3506 mutex_unlock(&prog->aux->dst_mutex);
3509 if (tgt_prog_fd && tgt_prog)
3510 bpf_prog_put(tgt_prog);
3514 static void bpf_raw_tp_link_release(struct bpf_link *link)
3516 struct bpf_raw_tp_link *raw_tp =
3517 container_of(link, struct bpf_raw_tp_link, link);
3519 bpf_probe_unregister(raw_tp->btp, raw_tp);
3520 bpf_put_raw_tracepoint(raw_tp->btp);
3523 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
3525 struct bpf_raw_tp_link *raw_tp =
3526 container_of(link, struct bpf_raw_tp_link, link);
3531 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
3532 struct seq_file *seq)
3534 struct bpf_raw_tp_link *raw_tp_link =
3535 container_of(link, struct bpf_raw_tp_link, link);
3539 raw_tp_link->btp->tp->name);
3542 static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen,
3545 if (ulen >= len + 1) {
3546 if (copy_to_user(ubuf, buf, len + 1))
3551 if (copy_to_user(ubuf, buf, ulen - 1))
3553 if (put_user(zero, ubuf + ulen - 1))
3561 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
3562 struct bpf_link_info *info)
3564 struct bpf_raw_tp_link *raw_tp_link =
3565 container_of(link, struct bpf_raw_tp_link, link);
3566 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
3567 const char *tp_name = raw_tp_link->btp->tp->name;
3568 u32 ulen = info->raw_tracepoint.tp_name_len;
3569 size_t tp_len = strlen(tp_name);
3574 info->raw_tracepoint.tp_name_len = tp_len + 1;
3579 return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len);
3582 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
3583 .release = bpf_raw_tp_link_release,
3584 .dealloc_deferred = bpf_raw_tp_link_dealloc,
3585 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
3586 .fill_link_info = bpf_raw_tp_link_fill_link_info,
3589 #ifdef CONFIG_PERF_EVENTS
3590 struct bpf_perf_link {
3591 struct bpf_link link;
3592 struct file *perf_file;
3595 static void bpf_perf_link_release(struct bpf_link *link)
3597 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3598 struct perf_event *event = perf_link->perf_file->private_data;
3600 perf_event_free_bpf_prog(event);
3601 fput(perf_link->perf_file);
3604 static void bpf_perf_link_dealloc(struct bpf_link *link)
3606 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3611 static int bpf_perf_link_fill_common(const struct perf_event *event,
3612 char __user *uname, u32 ulen,
3613 u64 *probe_offset, u64 *probe_addr,
3614 u32 *fd_type, unsigned long *missed)
3624 err = bpf_get_perf_event_info(event, &prog_id, fd_type, &buf,
3625 probe_offset, probe_addr, missed);
3632 err = bpf_copy_to_user(uname, buf, ulen, len);
3638 if (put_user(zero, uname))
3644 #ifdef CONFIG_KPROBE_EVENTS
3645 static int bpf_perf_link_fill_kprobe(const struct perf_event *event,
3646 struct bpf_link_info *info)
3648 unsigned long missed;
3654 uname = u64_to_user_ptr(info->perf_event.kprobe.func_name);
3655 ulen = info->perf_event.kprobe.name_len;
3656 err = bpf_perf_link_fill_common(event, uname, ulen, &offset, &addr,
3660 if (type == BPF_FD_TYPE_KRETPROBE)
3661 info->perf_event.type = BPF_PERF_EVENT_KRETPROBE;
3663 info->perf_event.type = BPF_PERF_EVENT_KPROBE;
3665 info->perf_event.kprobe.offset = offset;
3666 info->perf_event.kprobe.missed = missed;
3667 if (!kallsyms_show_value(current_cred()))
3669 info->perf_event.kprobe.addr = addr;
3670 info->perf_event.kprobe.cookie = event->bpf_cookie;
3675 #ifdef CONFIG_UPROBE_EVENTS
3676 static int bpf_perf_link_fill_uprobe(const struct perf_event *event,
3677 struct bpf_link_info *info)
3684 uname = u64_to_user_ptr(info->perf_event.uprobe.file_name);
3685 ulen = info->perf_event.uprobe.name_len;
3686 err = bpf_perf_link_fill_common(event, uname, ulen, &offset, &addr,
3691 if (type == BPF_FD_TYPE_URETPROBE)
3692 info->perf_event.type = BPF_PERF_EVENT_URETPROBE;
3694 info->perf_event.type = BPF_PERF_EVENT_UPROBE;
3695 info->perf_event.uprobe.offset = offset;
3696 info->perf_event.uprobe.cookie = event->bpf_cookie;
3701 static int bpf_perf_link_fill_probe(const struct perf_event *event,
3702 struct bpf_link_info *info)
3704 #ifdef CONFIG_KPROBE_EVENTS
3705 if (event->tp_event->flags & TRACE_EVENT_FL_KPROBE)
3706 return bpf_perf_link_fill_kprobe(event, info);
3708 #ifdef CONFIG_UPROBE_EVENTS
3709 if (event->tp_event->flags & TRACE_EVENT_FL_UPROBE)
3710 return bpf_perf_link_fill_uprobe(event, info);
3715 static int bpf_perf_link_fill_tracepoint(const struct perf_event *event,
3716 struct bpf_link_info *info)
3721 uname = u64_to_user_ptr(info->perf_event.tracepoint.tp_name);
3722 ulen = info->perf_event.tracepoint.name_len;
3723 info->perf_event.type = BPF_PERF_EVENT_TRACEPOINT;
3724 info->perf_event.tracepoint.cookie = event->bpf_cookie;
3725 return bpf_perf_link_fill_common(event, uname, ulen, NULL, NULL, NULL, NULL);
3728 static int bpf_perf_link_fill_perf_event(const struct perf_event *event,
3729 struct bpf_link_info *info)
3731 info->perf_event.event.type = event->attr.type;
3732 info->perf_event.event.config = event->attr.config;
3733 info->perf_event.event.cookie = event->bpf_cookie;
3734 info->perf_event.type = BPF_PERF_EVENT_EVENT;
3738 static int bpf_perf_link_fill_link_info(const struct bpf_link *link,
3739 struct bpf_link_info *info)
3741 struct bpf_perf_link *perf_link;
3742 const struct perf_event *event;
3744 perf_link = container_of(link, struct bpf_perf_link, link);
3745 event = perf_get_event(perf_link->perf_file);
3747 return PTR_ERR(event);
3749 switch (event->prog->type) {
3750 case BPF_PROG_TYPE_PERF_EVENT:
3751 return bpf_perf_link_fill_perf_event(event, info);
3752 case BPF_PROG_TYPE_TRACEPOINT:
3753 return bpf_perf_link_fill_tracepoint(event, info);
3754 case BPF_PROG_TYPE_KPROBE:
3755 return bpf_perf_link_fill_probe(event, info);
3761 static const struct bpf_link_ops bpf_perf_link_lops = {
3762 .release = bpf_perf_link_release,
3763 .dealloc = bpf_perf_link_dealloc,
3764 .fill_link_info = bpf_perf_link_fill_link_info,
3767 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3769 struct bpf_link_primer link_primer;
3770 struct bpf_perf_link *link;
3771 struct perf_event *event;
3772 struct file *perf_file;
3775 if (attr->link_create.flags)
3778 perf_file = perf_event_get(attr->link_create.target_fd);
3779 if (IS_ERR(perf_file))
3780 return PTR_ERR(perf_file);
3782 link = kzalloc(sizeof(*link), GFP_USER);
3787 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog);
3788 link->perf_file = perf_file;
3790 err = bpf_link_prime(&link->link, &link_primer);
3796 event = perf_file->private_data;
3797 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
3799 bpf_link_cleanup(&link_primer);
3802 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
3805 return bpf_link_settle(&link_primer);
3812 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3816 #endif /* CONFIG_PERF_EVENTS */
3818 static int bpf_raw_tp_link_attach(struct bpf_prog *prog,
3819 const char __user *user_tp_name, u64 cookie)
3821 struct bpf_link_primer link_primer;
3822 struct bpf_raw_tp_link *link;
3823 struct bpf_raw_event_map *btp;
3824 const char *tp_name;
3828 switch (prog->type) {
3829 case BPF_PROG_TYPE_TRACING:
3830 case BPF_PROG_TYPE_EXT:
3831 case BPF_PROG_TYPE_LSM:
3833 /* The attach point for this category of programs
3834 * should be specified via btf_id during program load.
3837 if (prog->type == BPF_PROG_TYPE_TRACING &&
3838 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
3839 tp_name = prog->aux->attach_func_name;
3842 return bpf_tracing_prog_attach(prog, 0, 0, 0);
3843 case BPF_PROG_TYPE_RAW_TRACEPOINT:
3844 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
3845 if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0)
3847 buf[sizeof(buf) - 1] = 0;
3854 btp = bpf_get_raw_tracepoint(tp_name);
3858 link = kzalloc(sizeof(*link), GFP_USER);
3863 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
3864 &bpf_raw_tp_link_lops, prog);
3866 link->cookie = cookie;
3868 err = bpf_link_prime(&link->link, &link_primer);
3874 err = bpf_probe_register(link->btp, link);
3876 bpf_link_cleanup(&link_primer);
3880 return bpf_link_settle(&link_primer);
3883 bpf_put_raw_tracepoint(btp);
3887 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.cookie
3889 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
3891 struct bpf_prog *prog;
3892 void __user *tp_name;
3896 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
3899 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
3901 return PTR_ERR(prog);
3903 tp_name = u64_to_user_ptr(attr->raw_tracepoint.name);
3904 cookie = attr->raw_tracepoint.cookie;
3905 fd = bpf_raw_tp_link_attach(prog, tp_name, cookie);
3911 static enum bpf_prog_type
3912 attach_type_to_prog_type(enum bpf_attach_type attach_type)
3914 switch (attach_type) {
3915 case BPF_CGROUP_INET_INGRESS:
3916 case BPF_CGROUP_INET_EGRESS:
3917 return BPF_PROG_TYPE_CGROUP_SKB;
3918 case BPF_CGROUP_INET_SOCK_CREATE:
3919 case BPF_CGROUP_INET_SOCK_RELEASE:
3920 case BPF_CGROUP_INET4_POST_BIND:
3921 case BPF_CGROUP_INET6_POST_BIND:
3922 return BPF_PROG_TYPE_CGROUP_SOCK;
3923 case BPF_CGROUP_INET4_BIND:
3924 case BPF_CGROUP_INET6_BIND:
3925 case BPF_CGROUP_INET4_CONNECT:
3926 case BPF_CGROUP_INET6_CONNECT:
3927 case BPF_CGROUP_UNIX_CONNECT:
3928 case BPF_CGROUP_INET4_GETPEERNAME:
3929 case BPF_CGROUP_INET6_GETPEERNAME:
3930 case BPF_CGROUP_UNIX_GETPEERNAME:
3931 case BPF_CGROUP_INET4_GETSOCKNAME:
3932 case BPF_CGROUP_INET6_GETSOCKNAME:
3933 case BPF_CGROUP_UNIX_GETSOCKNAME:
3934 case BPF_CGROUP_UDP4_SENDMSG:
3935 case BPF_CGROUP_UDP6_SENDMSG:
3936 case BPF_CGROUP_UNIX_SENDMSG:
3937 case BPF_CGROUP_UDP4_RECVMSG:
3938 case BPF_CGROUP_UDP6_RECVMSG:
3939 case BPF_CGROUP_UNIX_RECVMSG:
3940 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3941 case BPF_CGROUP_SOCK_OPS:
3942 return BPF_PROG_TYPE_SOCK_OPS;
3943 case BPF_CGROUP_DEVICE:
3944 return BPF_PROG_TYPE_CGROUP_DEVICE;
3945 case BPF_SK_MSG_VERDICT:
3946 return BPF_PROG_TYPE_SK_MSG;
3947 case BPF_SK_SKB_STREAM_PARSER:
3948 case BPF_SK_SKB_STREAM_VERDICT:
3949 case BPF_SK_SKB_VERDICT:
3950 return BPF_PROG_TYPE_SK_SKB;
3951 case BPF_LIRC_MODE2:
3952 return BPF_PROG_TYPE_LIRC_MODE2;
3953 case BPF_FLOW_DISSECTOR:
3954 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3955 case BPF_CGROUP_SYSCTL:
3956 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3957 case BPF_CGROUP_GETSOCKOPT:
3958 case BPF_CGROUP_SETSOCKOPT:
3959 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
3960 case BPF_TRACE_ITER:
3961 case BPF_TRACE_RAW_TP:
3962 case BPF_TRACE_FENTRY:
3963 case BPF_TRACE_FEXIT:
3964 case BPF_MODIFY_RETURN:
3965 return BPF_PROG_TYPE_TRACING;
3967 return BPF_PROG_TYPE_LSM;
3969 return BPF_PROG_TYPE_SK_LOOKUP;
3971 return BPF_PROG_TYPE_XDP;
3972 case BPF_LSM_CGROUP:
3973 return BPF_PROG_TYPE_LSM;
3974 case BPF_TCX_INGRESS:
3975 case BPF_TCX_EGRESS:
3976 case BPF_NETKIT_PRIMARY:
3977 case BPF_NETKIT_PEER:
3978 return BPF_PROG_TYPE_SCHED_CLS;
3980 return BPF_PROG_TYPE_UNSPEC;
3984 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
3985 enum bpf_attach_type attach_type)
3987 enum bpf_prog_type ptype;
3989 switch (prog->type) {
3990 case BPF_PROG_TYPE_CGROUP_SOCK:
3991 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3992 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3993 case BPF_PROG_TYPE_SK_LOOKUP:
3994 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
3995 case BPF_PROG_TYPE_CGROUP_SKB:
3996 if (!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN))
3997 /* cg-skb progs can be loaded by unpriv user.
3998 * check permissions at attach time.
4002 ptype = attach_type_to_prog_type(attach_type);
4003 if (prog->type != ptype)
4006 return prog->enforce_expected_attach_type &&
4007 prog->expected_attach_type != attach_type ?
4009 case BPF_PROG_TYPE_EXT:
4011 case BPF_PROG_TYPE_NETFILTER:
4012 if (attach_type != BPF_NETFILTER)
4015 case BPF_PROG_TYPE_PERF_EVENT:
4016 case BPF_PROG_TYPE_TRACEPOINT:
4017 if (attach_type != BPF_PERF_EVENT)
4020 case BPF_PROG_TYPE_KPROBE:
4021 if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI &&
4022 attach_type != BPF_TRACE_KPROBE_MULTI)
4024 if (prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION &&
4025 attach_type != BPF_TRACE_KPROBE_SESSION)
4027 if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI &&
4028 attach_type != BPF_TRACE_UPROBE_MULTI)
4030 if (attach_type != BPF_PERF_EVENT &&
4031 attach_type != BPF_TRACE_KPROBE_MULTI &&
4032 attach_type != BPF_TRACE_KPROBE_SESSION &&
4033 attach_type != BPF_TRACE_UPROBE_MULTI)
4036 case BPF_PROG_TYPE_SCHED_CLS:
4037 if (attach_type != BPF_TCX_INGRESS &&
4038 attach_type != BPF_TCX_EGRESS &&
4039 attach_type != BPF_NETKIT_PRIMARY &&
4040 attach_type != BPF_NETKIT_PEER)
4044 ptype = attach_type_to_prog_type(attach_type);
4045 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type)
4051 #define BPF_PROG_ATTACH_LAST_FIELD expected_revision
4053 #define BPF_F_ATTACH_MASK_BASE \
4054 (BPF_F_ALLOW_OVERRIDE | \
4055 BPF_F_ALLOW_MULTI | \
4058 #define BPF_F_ATTACH_MASK_MPROG \
4065 static int bpf_prog_attach(const union bpf_attr *attr)
4067 enum bpf_prog_type ptype;
4068 struct bpf_prog *prog;
4071 if (CHECK_ATTR(BPF_PROG_ATTACH))
4074 ptype = attach_type_to_prog_type(attr->attach_type);
4075 if (ptype == BPF_PROG_TYPE_UNSPEC)
4077 if (bpf_mprog_supported(ptype)) {
4078 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_MPROG)
4081 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_BASE)
4083 if (attr->relative_fd ||
4084 attr->expected_revision)
4088 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
4090 return PTR_ERR(prog);
4092 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
4098 case BPF_PROG_TYPE_SK_SKB:
4099 case BPF_PROG_TYPE_SK_MSG:
4100 ret = sock_map_get_from_fd(attr, prog);
4102 case BPF_PROG_TYPE_LIRC_MODE2:
4103 ret = lirc_prog_attach(attr, prog);
4105 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4106 ret = netns_bpf_prog_attach(attr, prog);
4108 case BPF_PROG_TYPE_CGROUP_DEVICE:
4109 case BPF_PROG_TYPE_CGROUP_SKB:
4110 case BPF_PROG_TYPE_CGROUP_SOCK:
4111 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4112 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4113 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4114 case BPF_PROG_TYPE_SOCK_OPS:
4115 case BPF_PROG_TYPE_LSM:
4116 if (ptype == BPF_PROG_TYPE_LSM &&
4117 prog->expected_attach_type != BPF_LSM_CGROUP)
4120 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
4122 case BPF_PROG_TYPE_SCHED_CLS:
4123 if (attr->attach_type == BPF_TCX_INGRESS ||
4124 attr->attach_type == BPF_TCX_EGRESS)
4125 ret = tcx_prog_attach(attr, prog);
4127 ret = netkit_prog_attach(attr, prog);
4138 #define BPF_PROG_DETACH_LAST_FIELD expected_revision
4140 static int bpf_prog_detach(const union bpf_attr *attr)
4142 struct bpf_prog *prog = NULL;
4143 enum bpf_prog_type ptype;
4146 if (CHECK_ATTR(BPF_PROG_DETACH))
4149 ptype = attach_type_to_prog_type(attr->attach_type);
4150 if (bpf_mprog_supported(ptype)) {
4151 if (ptype == BPF_PROG_TYPE_UNSPEC)
4153 if (attr->attach_flags & ~BPF_F_ATTACH_MASK_MPROG)
4155 if (attr->attach_bpf_fd) {
4156 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
4158 return PTR_ERR(prog);
4160 } else if (attr->attach_flags ||
4161 attr->relative_fd ||
4162 attr->expected_revision) {
4167 case BPF_PROG_TYPE_SK_MSG:
4168 case BPF_PROG_TYPE_SK_SKB:
4169 ret = sock_map_prog_detach(attr, ptype);
4171 case BPF_PROG_TYPE_LIRC_MODE2:
4172 ret = lirc_prog_detach(attr);
4174 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4175 ret = netns_bpf_prog_detach(attr, ptype);
4177 case BPF_PROG_TYPE_CGROUP_DEVICE:
4178 case BPF_PROG_TYPE_CGROUP_SKB:
4179 case BPF_PROG_TYPE_CGROUP_SOCK:
4180 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4181 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4182 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4183 case BPF_PROG_TYPE_SOCK_OPS:
4184 case BPF_PROG_TYPE_LSM:
4185 ret = cgroup_bpf_prog_detach(attr, ptype);
4187 case BPF_PROG_TYPE_SCHED_CLS:
4188 if (attr->attach_type == BPF_TCX_INGRESS ||
4189 attr->attach_type == BPF_TCX_EGRESS)
4190 ret = tcx_prog_detach(attr, prog);
4192 ret = netkit_prog_detach(attr, prog);
4203 #define BPF_PROG_QUERY_LAST_FIELD query.revision
4205 static int bpf_prog_query(const union bpf_attr *attr,
4206 union bpf_attr __user *uattr)
4208 if (!bpf_net_capable())
4210 if (CHECK_ATTR(BPF_PROG_QUERY))
4212 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
4215 switch (attr->query.attach_type) {
4216 case BPF_CGROUP_INET_INGRESS:
4217 case BPF_CGROUP_INET_EGRESS:
4218 case BPF_CGROUP_INET_SOCK_CREATE:
4219 case BPF_CGROUP_INET_SOCK_RELEASE:
4220 case BPF_CGROUP_INET4_BIND:
4221 case BPF_CGROUP_INET6_BIND:
4222 case BPF_CGROUP_INET4_POST_BIND:
4223 case BPF_CGROUP_INET6_POST_BIND:
4224 case BPF_CGROUP_INET4_CONNECT:
4225 case BPF_CGROUP_INET6_CONNECT:
4226 case BPF_CGROUP_UNIX_CONNECT:
4227 case BPF_CGROUP_INET4_GETPEERNAME:
4228 case BPF_CGROUP_INET6_GETPEERNAME:
4229 case BPF_CGROUP_UNIX_GETPEERNAME:
4230 case BPF_CGROUP_INET4_GETSOCKNAME:
4231 case BPF_CGROUP_INET6_GETSOCKNAME:
4232 case BPF_CGROUP_UNIX_GETSOCKNAME:
4233 case BPF_CGROUP_UDP4_SENDMSG:
4234 case BPF_CGROUP_UDP6_SENDMSG:
4235 case BPF_CGROUP_UNIX_SENDMSG:
4236 case BPF_CGROUP_UDP4_RECVMSG:
4237 case BPF_CGROUP_UDP6_RECVMSG:
4238 case BPF_CGROUP_UNIX_RECVMSG:
4239 case BPF_CGROUP_SOCK_OPS:
4240 case BPF_CGROUP_DEVICE:
4241 case BPF_CGROUP_SYSCTL:
4242 case BPF_CGROUP_GETSOCKOPT:
4243 case BPF_CGROUP_SETSOCKOPT:
4244 case BPF_LSM_CGROUP:
4245 return cgroup_bpf_prog_query(attr, uattr);
4246 case BPF_LIRC_MODE2:
4247 return lirc_prog_query(attr, uattr);
4248 case BPF_FLOW_DISSECTOR:
4250 return netns_bpf_prog_query(attr, uattr);
4251 case BPF_SK_SKB_STREAM_PARSER:
4252 case BPF_SK_SKB_STREAM_VERDICT:
4253 case BPF_SK_MSG_VERDICT:
4254 case BPF_SK_SKB_VERDICT:
4255 return sock_map_bpf_prog_query(attr, uattr);
4256 case BPF_TCX_INGRESS:
4257 case BPF_TCX_EGRESS:
4258 return tcx_prog_query(attr, uattr);
4259 case BPF_NETKIT_PRIMARY:
4260 case BPF_NETKIT_PEER:
4261 return netkit_prog_query(attr, uattr);
4267 #define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size
4269 static int bpf_prog_test_run(const union bpf_attr *attr,
4270 union bpf_attr __user *uattr)
4272 struct bpf_prog *prog;
4273 int ret = -ENOTSUPP;
4275 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
4278 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
4279 (!attr->test.ctx_size_in && attr->test.ctx_in))
4282 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
4283 (!attr->test.ctx_size_out && attr->test.ctx_out))
4286 prog = bpf_prog_get(attr->test.prog_fd);
4288 return PTR_ERR(prog);
4290 if (prog->aux->ops->test_run)
4291 ret = prog->aux->ops->test_run(prog, attr, uattr);
4297 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
4299 static int bpf_obj_get_next_id(const union bpf_attr *attr,
4300 union bpf_attr __user *uattr,
4304 u32 next_id = attr->start_id;
4307 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
4310 if (!capable(CAP_SYS_ADMIN))
4315 if (!idr_get_next(idr, &next_id))
4317 spin_unlock_bh(lock);
4320 err = put_user(next_id, &uattr->next_id);
4325 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
4327 struct bpf_map *map;
4329 spin_lock_bh(&map_idr_lock);
4331 map = idr_get_next(&map_idr, id);
4333 map = __bpf_map_inc_not_zero(map, false);
4339 spin_unlock_bh(&map_idr_lock);
4344 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
4346 struct bpf_prog *prog;
4348 spin_lock_bh(&prog_idr_lock);
4350 prog = idr_get_next(&prog_idr, id);
4352 prog = bpf_prog_inc_not_zero(prog);
4358 spin_unlock_bh(&prog_idr_lock);
4363 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
4365 struct bpf_prog *bpf_prog_by_id(u32 id)
4367 struct bpf_prog *prog;
4370 return ERR_PTR(-ENOENT);
4372 spin_lock_bh(&prog_idr_lock);
4373 prog = idr_find(&prog_idr, id);
4375 prog = bpf_prog_inc_not_zero(prog);
4377 prog = ERR_PTR(-ENOENT);
4378 spin_unlock_bh(&prog_idr_lock);
4382 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
4384 struct bpf_prog *prog;
4385 u32 id = attr->prog_id;
4388 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
4391 if (!capable(CAP_SYS_ADMIN))
4394 prog = bpf_prog_by_id(id);
4396 return PTR_ERR(prog);
4398 fd = bpf_prog_new_fd(prog);
4405 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
4407 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
4409 struct bpf_map *map;
4410 u32 id = attr->map_id;
4414 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
4415 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
4418 if (!capable(CAP_SYS_ADMIN))
4421 f_flags = bpf_get_file_flag(attr->open_flags);
4425 spin_lock_bh(&map_idr_lock);
4426 map = idr_find(&map_idr, id);
4428 map = __bpf_map_inc_not_zero(map, true);
4430 map = ERR_PTR(-ENOENT);
4431 spin_unlock_bh(&map_idr_lock);
4434 return PTR_ERR(map);
4436 fd = bpf_map_new_fd(map, f_flags);
4438 bpf_map_put_with_uref(map);
4443 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
4444 unsigned long addr, u32 *off,
4447 const struct bpf_map *map;
4450 mutex_lock(&prog->aux->used_maps_mutex);
4451 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
4452 map = prog->aux->used_maps[i];
4453 if (map == (void *)addr) {
4454 *type = BPF_PSEUDO_MAP_FD;
4457 if (!map->ops->map_direct_value_meta)
4459 if (!map->ops->map_direct_value_meta(map, addr, off)) {
4460 *type = BPF_PSEUDO_MAP_VALUE;
4467 mutex_unlock(&prog->aux->used_maps_mutex);
4471 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
4472 const struct cred *f_cred)
4474 const struct bpf_map *map;
4475 struct bpf_insn *insns;
4481 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
4486 for (i = 0; i < prog->len; i++) {
4487 code = insns[i].code;
4489 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
4490 insns[i].code = BPF_JMP | BPF_CALL;
4491 insns[i].imm = BPF_FUNC_tail_call;
4494 if (code == (BPF_JMP | BPF_CALL) ||
4495 code == (BPF_JMP | BPF_CALL_ARGS)) {
4496 if (code == (BPF_JMP | BPF_CALL_ARGS))
4497 insns[i].code = BPF_JMP | BPF_CALL;
4498 if (!bpf_dump_raw_ok(f_cred))
4502 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
4503 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
4507 if ((BPF_CLASS(code) == BPF_LDX || BPF_CLASS(code) == BPF_STX ||
4508 BPF_CLASS(code) == BPF_ST) && BPF_MODE(code) == BPF_PROBE_MEM32) {
4509 insns[i].code = BPF_CLASS(code) | BPF_SIZE(code) | BPF_MEM;
4513 if (code != (BPF_LD | BPF_IMM | BPF_DW))
4516 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
4517 map = bpf_map_from_imm(prog, imm, &off, &type);
4519 insns[i].src_reg = type;
4520 insns[i].imm = map->id;
4521 insns[i + 1].imm = off;
4529 static int set_info_rec_size(struct bpf_prog_info *info)
4532 * Ensure info.*_rec_size is the same as kernel expected size
4536 * Only allow zero *_rec_size if both _rec_size and _cnt are
4537 * zero. In this case, the kernel will set the expected
4538 * _rec_size back to the info.
4541 if ((info->nr_func_info || info->func_info_rec_size) &&
4542 info->func_info_rec_size != sizeof(struct bpf_func_info))
4545 if ((info->nr_line_info || info->line_info_rec_size) &&
4546 info->line_info_rec_size != sizeof(struct bpf_line_info))
4549 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
4550 info->jited_line_info_rec_size != sizeof(__u64))
4553 info->func_info_rec_size = sizeof(struct bpf_func_info);
4554 info->line_info_rec_size = sizeof(struct bpf_line_info);
4555 info->jited_line_info_rec_size = sizeof(__u64);
4560 static int bpf_prog_get_info_by_fd(struct file *file,
4561 struct bpf_prog *prog,
4562 const union bpf_attr *attr,
4563 union bpf_attr __user *uattr)
4565 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4566 struct btf *attach_btf = bpf_prog_get_target_btf(prog);
4567 struct bpf_prog_info info;
4568 u32 info_len = attr->info.info_len;
4569 struct bpf_prog_kstats stats;
4570 char __user *uinsns;
4574 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
4577 info_len = min_t(u32, sizeof(info), info_len);
4579 memset(&info, 0, sizeof(info));
4580 if (copy_from_user(&info, uinfo, info_len))
4583 info.type = prog->type;
4584 info.id = prog->aux->id;
4585 info.load_time = prog->aux->load_time;
4586 info.created_by_uid = from_kuid_munged(current_user_ns(),
4587 prog->aux->user->uid);
4588 info.gpl_compatible = prog->gpl_compatible;
4590 memcpy(info.tag, prog->tag, sizeof(prog->tag));
4591 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
4593 mutex_lock(&prog->aux->used_maps_mutex);
4594 ulen = info.nr_map_ids;
4595 info.nr_map_ids = prog->aux->used_map_cnt;
4596 ulen = min_t(u32, info.nr_map_ids, ulen);
4598 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
4601 for (i = 0; i < ulen; i++)
4602 if (put_user(prog->aux->used_maps[i]->id,
4603 &user_map_ids[i])) {
4604 mutex_unlock(&prog->aux->used_maps_mutex);
4608 mutex_unlock(&prog->aux->used_maps_mutex);
4610 err = set_info_rec_size(&info);
4614 bpf_prog_get_stats(prog, &stats);
4615 info.run_time_ns = stats.nsecs;
4616 info.run_cnt = stats.cnt;
4617 info.recursion_misses = stats.misses;
4619 info.verified_insns = prog->aux->verified_insns;
4621 if (!bpf_capable()) {
4622 info.jited_prog_len = 0;
4623 info.xlated_prog_len = 0;
4624 info.nr_jited_ksyms = 0;
4625 info.nr_jited_func_lens = 0;
4626 info.nr_func_info = 0;
4627 info.nr_line_info = 0;
4628 info.nr_jited_line_info = 0;
4632 ulen = info.xlated_prog_len;
4633 info.xlated_prog_len = bpf_prog_insn_size(prog);
4634 if (info.xlated_prog_len && ulen) {
4635 struct bpf_insn *insns_sanitized;
4638 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
4639 info.xlated_prog_insns = 0;
4642 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
4643 if (!insns_sanitized)
4645 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
4646 ulen = min_t(u32, info.xlated_prog_len, ulen);
4647 fault = copy_to_user(uinsns, insns_sanitized, ulen);
4648 kfree(insns_sanitized);
4653 if (bpf_prog_is_offloaded(prog->aux)) {
4654 err = bpf_prog_offload_info_fill(&info, prog);
4660 /* NOTE: the following code is supposed to be skipped for offload.
4661 * bpf_prog_offload_info_fill() is the place to fill similar fields
4664 ulen = info.jited_prog_len;
4665 if (prog->aux->func_cnt) {
4668 info.jited_prog_len = 0;
4669 for (i = 0; i < prog->aux->func_cnt; i++)
4670 info.jited_prog_len += prog->aux->func[i]->jited_len;
4672 info.jited_prog_len = prog->jited_len;
4675 if (info.jited_prog_len && ulen) {
4676 if (bpf_dump_raw_ok(file->f_cred)) {
4677 uinsns = u64_to_user_ptr(info.jited_prog_insns);
4678 ulen = min_t(u32, info.jited_prog_len, ulen);
4680 /* for multi-function programs, copy the JITed
4681 * instructions for all the functions
4683 if (prog->aux->func_cnt) {
4688 for (i = 0; i < prog->aux->func_cnt; i++) {
4689 len = prog->aux->func[i]->jited_len;
4690 len = min_t(u32, len, free);
4691 img = (u8 *) prog->aux->func[i]->bpf_func;
4692 if (copy_to_user(uinsns, img, len))
4700 if (copy_to_user(uinsns, prog->bpf_func, ulen))
4704 info.jited_prog_insns = 0;
4708 ulen = info.nr_jited_ksyms;
4709 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
4711 if (bpf_dump_raw_ok(file->f_cred)) {
4712 unsigned long ksym_addr;
4713 u64 __user *user_ksyms;
4716 /* copy the address of the kernel symbol
4717 * corresponding to each function
4719 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
4720 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
4721 if (prog->aux->func_cnt) {
4722 for (i = 0; i < ulen; i++) {
4723 ksym_addr = (unsigned long)
4724 prog->aux->func[i]->bpf_func;
4725 if (put_user((u64) ksym_addr,
4730 ksym_addr = (unsigned long) prog->bpf_func;
4731 if (put_user((u64) ksym_addr, &user_ksyms[0]))
4735 info.jited_ksyms = 0;
4739 ulen = info.nr_jited_func_lens;
4740 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
4742 if (bpf_dump_raw_ok(file->f_cred)) {
4743 u32 __user *user_lens;
4746 /* copy the JITed image lengths for each function */
4747 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
4748 user_lens = u64_to_user_ptr(info.jited_func_lens);
4749 if (prog->aux->func_cnt) {
4750 for (i = 0; i < ulen; i++) {
4752 prog->aux->func[i]->jited_len;
4753 if (put_user(func_len, &user_lens[i]))
4757 func_len = prog->jited_len;
4758 if (put_user(func_len, &user_lens[0]))
4762 info.jited_func_lens = 0;
4767 info.btf_id = btf_obj_id(prog->aux->btf);
4768 info.attach_btf_id = prog->aux->attach_btf_id;
4770 info.attach_btf_obj_id = btf_obj_id(attach_btf);
4772 ulen = info.nr_func_info;
4773 info.nr_func_info = prog->aux->func_info_cnt;
4774 if (info.nr_func_info && ulen) {
4775 char __user *user_finfo;
4777 user_finfo = u64_to_user_ptr(info.func_info);
4778 ulen = min_t(u32, info.nr_func_info, ulen);
4779 if (copy_to_user(user_finfo, prog->aux->func_info,
4780 info.func_info_rec_size * ulen))
4784 ulen = info.nr_line_info;
4785 info.nr_line_info = prog->aux->nr_linfo;
4786 if (info.nr_line_info && ulen) {
4787 __u8 __user *user_linfo;
4789 user_linfo = u64_to_user_ptr(info.line_info);
4790 ulen = min_t(u32, info.nr_line_info, ulen);
4791 if (copy_to_user(user_linfo, prog->aux->linfo,
4792 info.line_info_rec_size * ulen))
4796 ulen = info.nr_jited_line_info;
4797 if (prog->aux->jited_linfo)
4798 info.nr_jited_line_info = prog->aux->nr_linfo;
4800 info.nr_jited_line_info = 0;
4801 if (info.nr_jited_line_info && ulen) {
4802 if (bpf_dump_raw_ok(file->f_cred)) {
4803 unsigned long line_addr;
4804 __u64 __user *user_linfo;
4807 user_linfo = u64_to_user_ptr(info.jited_line_info);
4808 ulen = min_t(u32, info.nr_jited_line_info, ulen);
4809 for (i = 0; i < ulen; i++) {
4810 line_addr = (unsigned long)prog->aux->jited_linfo[i];
4811 if (put_user((__u64)line_addr, &user_linfo[i]))
4815 info.jited_line_info = 0;
4819 ulen = info.nr_prog_tags;
4820 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
4822 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
4825 user_prog_tags = u64_to_user_ptr(info.prog_tags);
4826 ulen = min_t(u32, info.nr_prog_tags, ulen);
4827 if (prog->aux->func_cnt) {
4828 for (i = 0; i < ulen; i++) {
4829 if (copy_to_user(user_prog_tags[i],
4830 prog->aux->func[i]->tag,
4835 if (copy_to_user(user_prog_tags[0],
4836 prog->tag, BPF_TAG_SIZE))
4842 if (copy_to_user(uinfo, &info, info_len) ||
4843 put_user(info_len, &uattr->info.info_len))
4849 static int bpf_map_get_info_by_fd(struct file *file,
4850 struct bpf_map *map,
4851 const union bpf_attr *attr,
4852 union bpf_attr __user *uattr)
4854 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4855 struct bpf_map_info info;
4856 u32 info_len = attr->info.info_len;
4859 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
4862 info_len = min_t(u32, sizeof(info), info_len);
4864 memset(&info, 0, sizeof(info));
4865 info.type = map->map_type;
4867 info.key_size = map->key_size;
4868 info.value_size = map->value_size;
4869 info.max_entries = map->max_entries;
4870 info.map_flags = map->map_flags;
4871 info.map_extra = map->map_extra;
4872 memcpy(info.name, map->name, sizeof(map->name));
4875 info.btf_id = btf_obj_id(map->btf);
4876 info.btf_key_type_id = map->btf_key_type_id;
4877 info.btf_value_type_id = map->btf_value_type_id;
4879 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
4880 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS)
4881 bpf_map_struct_ops_info_fill(&info, map);
4883 if (bpf_map_is_offloaded(map)) {
4884 err = bpf_map_offload_info_fill(&info, map);
4889 if (copy_to_user(uinfo, &info, info_len) ||
4890 put_user(info_len, &uattr->info.info_len))
4896 static int bpf_btf_get_info_by_fd(struct file *file,
4898 const union bpf_attr *attr,
4899 union bpf_attr __user *uattr)
4901 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4902 u32 info_len = attr->info.info_len;
4905 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
4909 return btf_get_info_by_fd(btf, attr, uattr);
4912 static int bpf_link_get_info_by_fd(struct file *file,
4913 struct bpf_link *link,
4914 const union bpf_attr *attr,
4915 union bpf_attr __user *uattr)
4917 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4918 struct bpf_link_info info;
4919 u32 info_len = attr->info.info_len;
4922 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
4925 info_len = min_t(u32, sizeof(info), info_len);
4927 memset(&info, 0, sizeof(info));
4928 if (copy_from_user(&info, uinfo, info_len))
4931 info.type = link->type;
4934 info.prog_id = link->prog->aux->id;
4936 if (link->ops->fill_link_info) {
4937 err = link->ops->fill_link_info(link, &info);
4942 if (copy_to_user(uinfo, &info, info_len) ||
4943 put_user(info_len, &uattr->info.info_len))
4950 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
4952 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
4953 union bpf_attr __user *uattr)
4955 int ufd = attr->info.bpf_fd;
4959 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
4966 if (f.file->f_op == &bpf_prog_fops)
4967 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
4969 else if (f.file->f_op == &bpf_map_fops)
4970 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
4972 else if (f.file->f_op == &btf_fops)
4973 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
4974 else if (f.file->f_op == &bpf_link_fops)
4975 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
4984 #define BPF_BTF_LOAD_LAST_FIELD btf_token_fd
4986 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size)
4988 struct bpf_token *token = NULL;
4990 if (CHECK_ATTR(BPF_BTF_LOAD))
4993 if (attr->btf_flags & ~BPF_F_TOKEN_FD)
4996 if (attr->btf_flags & BPF_F_TOKEN_FD) {
4997 token = bpf_token_get_from_fd(attr->btf_token_fd);
4999 return PTR_ERR(token);
5000 if (!bpf_token_allow_cmd(token, BPF_BTF_LOAD)) {
5001 bpf_token_put(token);
5006 if (!bpf_token_capable(token, CAP_BPF)) {
5007 bpf_token_put(token);
5011 bpf_token_put(token);
5013 return btf_new_fd(attr, uattr, uattr_size);
5016 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
5018 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
5020 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
5023 if (!capable(CAP_SYS_ADMIN))
5026 return btf_get_fd_by_id(attr->btf_id);
5029 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
5030 union bpf_attr __user *uattr,
5031 u32 prog_id, u32 fd_type,
5032 const char *buf, u64 probe_offset,
5035 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
5036 u32 len = buf ? strlen(buf) : 0, input_len;
5039 if (put_user(len, &uattr->task_fd_query.buf_len))
5041 input_len = attr->task_fd_query.buf_len;
5042 if (input_len && ubuf) {
5044 /* nothing to copy, just make ubuf NULL terminated */
5047 if (put_user(zero, ubuf))
5049 } else if (input_len >= len + 1) {
5050 /* ubuf can hold the string with NULL terminator */
5051 if (copy_to_user(ubuf, buf, len + 1))
5054 /* ubuf cannot hold the string with NULL terminator,
5055 * do a partial copy with NULL terminator.
5060 if (copy_to_user(ubuf, buf, input_len - 1))
5062 if (put_user(zero, ubuf + input_len - 1))
5067 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
5068 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
5069 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
5070 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
5076 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
5078 static int bpf_task_fd_query(const union bpf_attr *attr,
5079 union bpf_attr __user *uattr)
5081 pid_t pid = attr->task_fd_query.pid;
5082 u32 fd = attr->task_fd_query.fd;
5083 const struct perf_event *event;
5084 struct task_struct *task;
5088 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
5091 if (!capable(CAP_SYS_ADMIN))
5094 if (attr->task_fd_query.flags != 0)
5098 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
5104 file = fget_task(task, fd);
5105 put_task_struct(task);
5109 if (file->f_op == &bpf_link_fops) {
5110 struct bpf_link *link = file->private_data;
5112 if (link->ops == &bpf_raw_tp_link_lops) {
5113 struct bpf_raw_tp_link *raw_tp =
5114 container_of(link, struct bpf_raw_tp_link, link);
5115 struct bpf_raw_event_map *btp = raw_tp->btp;
5117 err = bpf_task_fd_query_copy(attr, uattr,
5118 raw_tp->link.prog->aux->id,
5119 BPF_FD_TYPE_RAW_TRACEPOINT,
5120 btp->tp->name, 0, 0);
5126 event = perf_get_event(file);
5127 if (!IS_ERR(event)) {
5128 u64 probe_offset, probe_addr;
5129 u32 prog_id, fd_type;
5132 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
5133 &buf, &probe_offset,
5136 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
5150 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
5152 #define BPF_DO_BATCH(fn, ...) \
5158 err = fn(__VA_ARGS__); \
5161 static int bpf_map_do_batch(const union bpf_attr *attr,
5162 union bpf_attr __user *uattr,
5165 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH ||
5166 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
5167 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
5168 struct bpf_map *map;
5172 if (CHECK_ATTR(BPF_MAP_BATCH))
5175 ufd = attr->batch.map_fd;
5177 map = __bpf_map_get(f);
5179 return PTR_ERR(map);
5181 bpf_map_write_active_inc(map);
5182 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
5186 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
5191 if (cmd == BPF_MAP_LOOKUP_BATCH)
5192 BPF_DO_BATCH(map->ops->map_lookup_batch, map, attr, uattr);
5193 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
5194 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch, map, attr, uattr);
5195 else if (cmd == BPF_MAP_UPDATE_BATCH)
5196 BPF_DO_BATCH(map->ops->map_update_batch, map, f.file, attr, uattr);
5198 BPF_DO_BATCH(map->ops->map_delete_batch, map, attr, uattr);
5201 maybe_wait_bpf_programs(map);
5202 bpf_map_write_active_dec(map);
5208 #define BPF_LINK_CREATE_LAST_FIELD link_create.uprobe_multi.pid
5209 static int link_create(union bpf_attr *attr, bpfptr_t uattr)
5211 struct bpf_prog *prog;
5214 if (CHECK_ATTR(BPF_LINK_CREATE))
5217 if (attr->link_create.attach_type == BPF_STRUCT_OPS)
5218 return bpf_struct_ops_link_create(attr);
5220 prog = bpf_prog_get(attr->link_create.prog_fd);
5222 return PTR_ERR(prog);
5224 ret = bpf_prog_attach_check_attach_type(prog,
5225 attr->link_create.attach_type);
5229 switch (prog->type) {
5230 case BPF_PROG_TYPE_CGROUP_SKB:
5231 case BPF_PROG_TYPE_CGROUP_SOCK:
5232 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
5233 case BPF_PROG_TYPE_SOCK_OPS:
5234 case BPF_PROG_TYPE_CGROUP_DEVICE:
5235 case BPF_PROG_TYPE_CGROUP_SYSCTL:
5236 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
5237 ret = cgroup_bpf_link_attach(attr, prog);
5239 case BPF_PROG_TYPE_EXT:
5240 ret = bpf_tracing_prog_attach(prog,
5241 attr->link_create.target_fd,
5242 attr->link_create.target_btf_id,
5243 attr->link_create.tracing.cookie);
5245 case BPF_PROG_TYPE_LSM:
5246 case BPF_PROG_TYPE_TRACING:
5247 if (attr->link_create.attach_type != prog->expected_attach_type) {
5251 if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
5252 ret = bpf_raw_tp_link_attach(prog, NULL, attr->link_create.tracing.cookie);
5253 else if (prog->expected_attach_type == BPF_TRACE_ITER)
5254 ret = bpf_iter_link_attach(attr, uattr, prog);
5255 else if (prog->expected_attach_type == BPF_LSM_CGROUP)
5256 ret = cgroup_bpf_link_attach(attr, prog);
5258 ret = bpf_tracing_prog_attach(prog,
5259 attr->link_create.target_fd,
5260 attr->link_create.target_btf_id,
5261 attr->link_create.tracing.cookie);
5263 case BPF_PROG_TYPE_FLOW_DISSECTOR:
5264 case BPF_PROG_TYPE_SK_LOOKUP:
5265 ret = netns_bpf_link_create(attr, prog);
5267 case BPF_PROG_TYPE_SK_MSG:
5268 case BPF_PROG_TYPE_SK_SKB:
5269 ret = sock_map_link_create(attr, prog);
5272 case BPF_PROG_TYPE_XDP:
5273 ret = bpf_xdp_link_attach(attr, prog);
5275 case BPF_PROG_TYPE_SCHED_CLS:
5276 if (attr->link_create.attach_type == BPF_TCX_INGRESS ||
5277 attr->link_create.attach_type == BPF_TCX_EGRESS)
5278 ret = tcx_link_attach(attr, prog);
5280 ret = netkit_link_attach(attr, prog);
5282 case BPF_PROG_TYPE_NETFILTER:
5283 ret = bpf_nf_link_attach(attr, prog);
5286 case BPF_PROG_TYPE_PERF_EVENT:
5287 case BPF_PROG_TYPE_TRACEPOINT:
5288 ret = bpf_perf_link_attach(attr, prog);
5290 case BPF_PROG_TYPE_KPROBE:
5291 if (attr->link_create.attach_type == BPF_PERF_EVENT)
5292 ret = bpf_perf_link_attach(attr, prog);
5293 else if (attr->link_create.attach_type == BPF_TRACE_KPROBE_MULTI ||
5294 attr->link_create.attach_type == BPF_TRACE_KPROBE_SESSION)
5295 ret = bpf_kprobe_multi_link_attach(attr, prog);
5296 else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI)
5297 ret = bpf_uprobe_multi_link_attach(attr, prog);
5309 static int link_update_map(struct bpf_link *link, union bpf_attr *attr)
5311 struct bpf_map *new_map, *old_map = NULL;
5314 new_map = bpf_map_get(attr->link_update.new_map_fd);
5315 if (IS_ERR(new_map))
5316 return PTR_ERR(new_map);
5318 if (attr->link_update.flags & BPF_F_REPLACE) {
5319 old_map = bpf_map_get(attr->link_update.old_map_fd);
5320 if (IS_ERR(old_map)) {
5321 ret = PTR_ERR(old_map);
5324 } else if (attr->link_update.old_map_fd) {
5329 ret = link->ops->update_map(link, new_map, old_map);
5332 bpf_map_put(old_map);
5334 bpf_map_put(new_map);
5338 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
5340 static int link_update(union bpf_attr *attr)
5342 struct bpf_prog *old_prog = NULL, *new_prog;
5343 struct bpf_link *link;
5347 if (CHECK_ATTR(BPF_LINK_UPDATE))
5350 flags = attr->link_update.flags;
5351 if (flags & ~BPF_F_REPLACE)
5354 link = bpf_link_get_from_fd(attr->link_update.link_fd);
5356 return PTR_ERR(link);
5358 if (link->ops->update_map) {
5359 ret = link_update_map(link, attr);
5363 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
5364 if (IS_ERR(new_prog)) {
5365 ret = PTR_ERR(new_prog);
5369 if (flags & BPF_F_REPLACE) {
5370 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
5371 if (IS_ERR(old_prog)) {
5372 ret = PTR_ERR(old_prog);
5376 } else if (attr->link_update.old_prog_fd) {
5381 if (link->ops->update_prog)
5382 ret = link->ops->update_prog(link, new_prog, old_prog);
5388 bpf_prog_put(old_prog);
5390 bpf_prog_put(new_prog);
5392 bpf_link_put_direct(link);
5396 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
5398 static int link_detach(union bpf_attr *attr)
5400 struct bpf_link *link;
5403 if (CHECK_ATTR(BPF_LINK_DETACH))
5406 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
5408 return PTR_ERR(link);
5410 if (link->ops->detach)
5411 ret = link->ops->detach(link);
5415 bpf_link_put_direct(link);
5419 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
5421 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
5424 struct bpf_link *bpf_link_by_id(u32 id)
5426 struct bpf_link *link;
5429 return ERR_PTR(-ENOENT);
5431 spin_lock_bh(&link_idr_lock);
5432 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
5433 link = idr_find(&link_idr, id);
5436 link = bpf_link_inc_not_zero(link);
5438 link = ERR_PTR(-EAGAIN);
5440 link = ERR_PTR(-ENOENT);
5442 spin_unlock_bh(&link_idr_lock);
5446 struct bpf_link *bpf_link_get_curr_or_next(u32 *id)
5448 struct bpf_link *link;
5450 spin_lock_bh(&link_idr_lock);
5452 link = idr_get_next(&link_idr, id);
5454 link = bpf_link_inc_not_zero(link);
5460 spin_unlock_bh(&link_idr_lock);
5465 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
5467 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
5469 struct bpf_link *link;
5470 u32 id = attr->link_id;
5473 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
5476 if (!capable(CAP_SYS_ADMIN))
5479 link = bpf_link_by_id(id);
5481 return PTR_ERR(link);
5483 fd = bpf_link_new_fd(link);
5485 bpf_link_put_direct(link);
5490 DEFINE_MUTEX(bpf_stats_enabled_mutex);
5492 static int bpf_stats_release(struct inode *inode, struct file *file)
5494 mutex_lock(&bpf_stats_enabled_mutex);
5495 static_key_slow_dec(&bpf_stats_enabled_key.key);
5496 mutex_unlock(&bpf_stats_enabled_mutex);
5500 static const struct file_operations bpf_stats_fops = {
5501 .release = bpf_stats_release,
5504 static int bpf_enable_runtime_stats(void)
5508 mutex_lock(&bpf_stats_enabled_mutex);
5510 /* Set a very high limit to avoid overflow */
5511 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
5512 mutex_unlock(&bpf_stats_enabled_mutex);
5516 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
5518 static_key_slow_inc(&bpf_stats_enabled_key.key);
5520 mutex_unlock(&bpf_stats_enabled_mutex);
5524 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
5526 static int bpf_enable_stats(union bpf_attr *attr)
5529 if (CHECK_ATTR(BPF_ENABLE_STATS))
5532 if (!capable(CAP_SYS_ADMIN))
5535 switch (attr->enable_stats.type) {
5536 case BPF_STATS_RUN_TIME:
5537 return bpf_enable_runtime_stats();
5544 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
5546 static int bpf_iter_create(union bpf_attr *attr)
5548 struct bpf_link *link;
5551 if (CHECK_ATTR(BPF_ITER_CREATE))
5554 if (attr->iter_create.flags)
5557 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
5559 return PTR_ERR(link);
5561 err = bpf_iter_new_fd(link);
5562 bpf_link_put_direct(link);
5567 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
5569 static int bpf_prog_bind_map(union bpf_attr *attr)
5571 struct bpf_prog *prog;
5572 struct bpf_map *map;
5573 struct bpf_map **used_maps_old, **used_maps_new;
5576 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
5579 if (attr->prog_bind_map.flags)
5582 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
5584 return PTR_ERR(prog);
5586 map = bpf_map_get(attr->prog_bind_map.map_fd);
5592 mutex_lock(&prog->aux->used_maps_mutex);
5594 used_maps_old = prog->aux->used_maps;
5596 for (i = 0; i < prog->aux->used_map_cnt; i++)
5597 if (used_maps_old[i] == map) {
5602 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
5603 sizeof(used_maps_new[0]),
5605 if (!used_maps_new) {
5610 /* The bpf program will not access the bpf map, but for the sake of
5611 * simplicity, increase sleepable_refcnt for sleepable program as well.
5613 if (prog->sleepable)
5614 atomic64_inc(&map->sleepable_refcnt);
5615 memcpy(used_maps_new, used_maps_old,
5616 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
5617 used_maps_new[prog->aux->used_map_cnt] = map;
5619 prog->aux->used_map_cnt++;
5620 prog->aux->used_maps = used_maps_new;
5622 kfree(used_maps_old);
5625 mutex_unlock(&prog->aux->used_maps_mutex);
5634 #define BPF_TOKEN_CREATE_LAST_FIELD token_create.bpffs_fd
5636 static int token_create(union bpf_attr *attr)
5638 if (CHECK_ATTR(BPF_TOKEN_CREATE))
5641 /* no flags are supported yet */
5642 if (attr->token_create.flags)
5645 return bpf_token_create(attr);
5648 static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
5650 union bpf_attr attr;
5653 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
5656 size = min_t(u32, size, sizeof(attr));
5658 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
5659 memset(&attr, 0, sizeof(attr));
5660 if (copy_from_bpfptr(&attr, uattr, size) != 0)
5663 err = security_bpf(cmd, &attr, size);
5668 case BPF_MAP_CREATE:
5669 err = map_create(&attr);
5671 case BPF_MAP_LOOKUP_ELEM:
5672 err = map_lookup_elem(&attr);
5674 case BPF_MAP_UPDATE_ELEM:
5675 err = map_update_elem(&attr, uattr);
5677 case BPF_MAP_DELETE_ELEM:
5678 err = map_delete_elem(&attr, uattr);
5680 case BPF_MAP_GET_NEXT_KEY:
5681 err = map_get_next_key(&attr);
5683 case BPF_MAP_FREEZE:
5684 err = map_freeze(&attr);
5687 err = bpf_prog_load(&attr, uattr, size);
5690 err = bpf_obj_pin(&attr);
5693 err = bpf_obj_get(&attr);
5695 case BPF_PROG_ATTACH:
5696 err = bpf_prog_attach(&attr);
5698 case BPF_PROG_DETACH:
5699 err = bpf_prog_detach(&attr);
5701 case BPF_PROG_QUERY:
5702 err = bpf_prog_query(&attr, uattr.user);
5704 case BPF_PROG_TEST_RUN:
5705 err = bpf_prog_test_run(&attr, uattr.user);
5707 case BPF_PROG_GET_NEXT_ID:
5708 err = bpf_obj_get_next_id(&attr, uattr.user,
5709 &prog_idr, &prog_idr_lock);
5711 case BPF_MAP_GET_NEXT_ID:
5712 err = bpf_obj_get_next_id(&attr, uattr.user,
5713 &map_idr, &map_idr_lock);
5715 case BPF_BTF_GET_NEXT_ID:
5716 err = bpf_obj_get_next_id(&attr, uattr.user,
5717 &btf_idr, &btf_idr_lock);
5719 case BPF_PROG_GET_FD_BY_ID:
5720 err = bpf_prog_get_fd_by_id(&attr);
5722 case BPF_MAP_GET_FD_BY_ID:
5723 err = bpf_map_get_fd_by_id(&attr);
5725 case BPF_OBJ_GET_INFO_BY_FD:
5726 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
5728 case BPF_RAW_TRACEPOINT_OPEN:
5729 err = bpf_raw_tracepoint_open(&attr);
5732 err = bpf_btf_load(&attr, uattr, size);
5734 case BPF_BTF_GET_FD_BY_ID:
5735 err = bpf_btf_get_fd_by_id(&attr);
5737 case BPF_TASK_FD_QUERY:
5738 err = bpf_task_fd_query(&attr, uattr.user);
5740 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
5741 err = map_lookup_and_delete_elem(&attr);
5743 case BPF_MAP_LOOKUP_BATCH:
5744 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
5746 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
5747 err = bpf_map_do_batch(&attr, uattr.user,
5748 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
5750 case BPF_MAP_UPDATE_BATCH:
5751 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
5753 case BPF_MAP_DELETE_BATCH:
5754 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
5756 case BPF_LINK_CREATE:
5757 err = link_create(&attr, uattr);
5759 case BPF_LINK_UPDATE:
5760 err = link_update(&attr);
5762 case BPF_LINK_GET_FD_BY_ID:
5763 err = bpf_link_get_fd_by_id(&attr);
5765 case BPF_LINK_GET_NEXT_ID:
5766 err = bpf_obj_get_next_id(&attr, uattr.user,
5767 &link_idr, &link_idr_lock);
5769 case BPF_ENABLE_STATS:
5770 err = bpf_enable_stats(&attr);
5772 case BPF_ITER_CREATE:
5773 err = bpf_iter_create(&attr);
5775 case BPF_LINK_DETACH:
5776 err = link_detach(&attr);
5778 case BPF_PROG_BIND_MAP:
5779 err = bpf_prog_bind_map(&attr);
5781 case BPF_TOKEN_CREATE:
5782 err = token_create(&attr);
5792 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
5794 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
5797 static bool syscall_prog_is_valid_access(int off, int size,
5798 enum bpf_access_type type,
5799 const struct bpf_prog *prog,
5800 struct bpf_insn_access_aux *info)
5802 if (off < 0 || off >= U16_MAX)
5804 if (off % size != 0)
5809 BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
5812 case BPF_MAP_CREATE:
5813 case BPF_MAP_DELETE_ELEM:
5814 case BPF_MAP_UPDATE_ELEM:
5815 case BPF_MAP_FREEZE:
5816 case BPF_MAP_GET_FD_BY_ID:
5819 case BPF_LINK_CREATE:
5820 case BPF_RAW_TRACEPOINT_OPEN:
5825 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
5829 /* To shut up -Wmissing-prototypes.
5830 * This function is used by the kernel light skeleton
5831 * to load bpf programs when modules are loaded or during kernel boot.
5832 * See tools/lib/bpf/skel_internal.h
5834 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size);
5836 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5838 struct bpf_prog * __maybe_unused prog;
5839 struct bpf_tramp_run_ctx __maybe_unused run_ctx;
5842 #ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */
5843 case BPF_PROG_TEST_RUN:
5844 if (attr->test.data_in || attr->test.data_out ||
5845 attr->test.ctx_out || attr->test.duration ||
5846 attr->test.repeat || attr->test.flags)
5849 prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL);
5851 return PTR_ERR(prog);
5853 if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
5854 attr->test.ctx_size_in > U16_MAX) {
5859 run_ctx.bpf_cookie = 0;
5860 if (!__bpf_prog_enter_sleepable_recur(prog, &run_ctx)) {
5861 /* recursion detected */
5862 __bpf_prog_exit_sleepable_recur(prog, 0, &run_ctx);
5866 attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
5867 __bpf_prog_exit_sleepable_recur(prog, 0 /* bpf_prog_run does runtime stats */,
5873 return ____bpf_sys_bpf(cmd, attr, size);
5876 EXPORT_SYMBOL(kern_sys_bpf);
5878 static const struct bpf_func_proto bpf_sys_bpf_proto = {
5879 .func = bpf_sys_bpf,
5881 .ret_type = RET_INTEGER,
5882 .arg1_type = ARG_ANYTHING,
5883 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5884 .arg3_type = ARG_CONST_SIZE,
5887 const struct bpf_func_proto * __weak
5888 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5890 return bpf_base_func_proto(func_id, prog);
5893 BPF_CALL_1(bpf_sys_close, u32, fd)
5895 /* When bpf program calls this helper there should not be
5896 * an fdget() without matching completed fdput().
5897 * This helper is allowed in the following callchain only:
5898 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
5900 return close_fd(fd);
5903 static const struct bpf_func_proto bpf_sys_close_proto = {
5904 .func = bpf_sys_close,
5906 .ret_type = RET_INTEGER,
5907 .arg1_type = ARG_ANYTHING,
5910 BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res)
5915 if (name_sz <= 1 || name[name_sz - 1])
5918 if (!bpf_dump_raw_ok(current_cred()))
5921 *res = kallsyms_lookup_name(name);
5922 return *res ? 0 : -ENOENT;
5925 static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = {
5926 .func = bpf_kallsyms_lookup_name,
5928 .ret_type = RET_INTEGER,
5929 .arg1_type = ARG_PTR_TO_MEM,
5930 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
5931 .arg3_type = ARG_ANYTHING,
5932 .arg4_type = ARG_PTR_TO_LONG,
5935 static const struct bpf_func_proto *
5936 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5939 case BPF_FUNC_sys_bpf:
5940 return !bpf_token_capable(prog->aux->token, CAP_PERFMON)
5941 ? NULL : &bpf_sys_bpf_proto;
5942 case BPF_FUNC_btf_find_by_name_kind:
5943 return &bpf_btf_find_by_name_kind_proto;
5944 case BPF_FUNC_sys_close:
5945 return &bpf_sys_close_proto;
5946 case BPF_FUNC_kallsyms_lookup_name:
5947 return &bpf_kallsyms_lookup_name_proto;
5949 return tracing_prog_func_proto(func_id, prog);
5953 const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
5954 .get_func_proto = syscall_prog_func_proto,
5955 .is_valid_access = syscall_prog_is_valid_access,
5958 const struct bpf_prog_ops bpf_syscall_prog_ops = {
5959 .test_run = bpf_prog_test_run_syscall,
5962 #ifdef CONFIG_SYSCTL
5963 static int bpf_stats_handler(struct ctl_table *table, int write,
5964 void *buffer, size_t *lenp, loff_t *ppos)
5966 struct static_key *key = (struct static_key *)table->data;
5967 static int saved_val;
5969 struct ctl_table tmp = {
5971 .maxlen = sizeof(val),
5972 .mode = table->mode,
5973 .extra1 = SYSCTL_ZERO,
5974 .extra2 = SYSCTL_ONE,
5977 if (write && !capable(CAP_SYS_ADMIN))
5980 mutex_lock(&bpf_stats_enabled_mutex);
5982 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
5983 if (write && !ret && val != saved_val) {
5985 static_key_slow_inc(key);
5987 static_key_slow_dec(key);
5990 mutex_unlock(&bpf_stats_enabled_mutex);
5994 void __weak unpriv_ebpf_notify(int new_state)
5998 static int bpf_unpriv_handler(struct ctl_table *table, int write,
5999 void *buffer, size_t *lenp, loff_t *ppos)
6001 int ret, unpriv_enable = *(int *)table->data;
6002 bool locked_state = unpriv_enable == 1;
6003 struct ctl_table tmp = *table;
6005 if (write && !capable(CAP_SYS_ADMIN))
6008 tmp.data = &unpriv_enable;
6009 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
6010 if (write && !ret) {
6011 if (locked_state && unpriv_enable != 1)
6013 *(int *)table->data = unpriv_enable;
6017 unpriv_ebpf_notify(unpriv_enable);
6022 static struct ctl_table bpf_syscall_table[] = {
6024 .procname = "unprivileged_bpf_disabled",
6025 .data = &sysctl_unprivileged_bpf_disabled,
6026 .maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
6028 .proc_handler = bpf_unpriv_handler,
6029 .extra1 = SYSCTL_ZERO,
6030 .extra2 = SYSCTL_TWO,
6033 .procname = "bpf_stats_enabled",
6034 .data = &bpf_stats_enabled_key.key,
6036 .proc_handler = bpf_stats_handler,
6040 static int __init bpf_syscall_sysctl_init(void)
6042 register_sysctl_init("kernel", bpf_syscall_table);
6045 late_initcall(bpf_syscall_sysctl_init);
6046 #endif /* CONFIG_SYSCTL */