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>
38 #include <net/netfilter/nf_bpf_link.h>
40 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
41 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
42 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
43 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
44 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
45 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
48 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
50 DEFINE_PER_CPU(int, bpf_prog_active);
51 static DEFINE_IDR(prog_idr);
52 static DEFINE_SPINLOCK(prog_idr_lock);
53 static DEFINE_IDR(map_idr);
54 static DEFINE_SPINLOCK(map_idr_lock);
55 static DEFINE_IDR(link_idr);
56 static DEFINE_SPINLOCK(link_idr_lock);
58 int sysctl_unprivileged_bpf_disabled __read_mostly =
59 IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
61 static const struct bpf_map_ops * const bpf_map_types[] = {
62 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
63 #define BPF_MAP_TYPE(_id, _ops) \
65 #define BPF_LINK_TYPE(_id, _name)
66 #include <linux/bpf_types.h>
73 * If we're handed a bigger struct than we know of, ensure all the unknown bits
74 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
75 * we don't know about yet.
77 * There is a ToCToU between this function call and the following
78 * copy_from_user() call. However, this is not a concern since this function is
79 * meant to be a future-proofing of bits.
81 int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
87 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
90 if (actual_size <= expected_size)
94 res = memchr_inv(uaddr.kernel + expected_size, 0,
95 actual_size - expected_size) == NULL;
97 res = check_zeroed_user(uaddr.user + expected_size,
98 actual_size - expected_size);
101 return res ? 0 : -E2BIG;
104 const struct bpf_map_ops bpf_map_offload_ops = {
105 .map_meta_equal = bpf_map_meta_equal,
106 .map_alloc = bpf_map_offload_map_alloc,
107 .map_free = bpf_map_offload_map_free,
108 .map_check_btf = map_check_no_btf,
109 .map_mem_usage = bpf_map_offload_map_mem_usage,
112 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
114 const struct bpf_map_ops *ops;
115 u32 type = attr->map_type;
119 if (type >= ARRAY_SIZE(bpf_map_types))
120 return ERR_PTR(-EINVAL);
121 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
122 ops = bpf_map_types[type];
124 return ERR_PTR(-EINVAL);
126 if (ops->map_alloc_check) {
127 err = ops->map_alloc_check(attr);
131 if (attr->map_ifindex)
132 ops = &bpf_map_offload_ops;
133 if (!ops->map_mem_usage)
134 return ERR_PTR(-EINVAL);
135 map = ops->map_alloc(attr);
139 map->map_type = type;
143 static void bpf_map_write_active_inc(struct bpf_map *map)
145 atomic64_inc(&map->writecnt);
148 static void bpf_map_write_active_dec(struct bpf_map *map)
150 atomic64_dec(&map->writecnt);
153 bool bpf_map_write_active(const struct bpf_map *map)
155 return atomic64_read(&map->writecnt) != 0;
158 static u32 bpf_map_value_size(const struct bpf_map *map)
160 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
161 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
162 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
163 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
164 return round_up(map->value_size, 8) * num_possible_cpus();
165 else if (IS_FD_MAP(map))
168 return map->value_size;
171 static void maybe_wait_bpf_programs(struct bpf_map *map)
173 /* Wait for any running BPF programs to complete so that
174 * userspace, when we return to it, knows that all programs
175 * that could be running use the new map value.
177 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
178 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
182 static int bpf_map_update_value(struct bpf_map *map, struct file *map_file,
183 void *key, void *value, __u64 flags)
187 /* Need to create a kthread, thus must support schedule */
188 if (bpf_map_is_offloaded(map)) {
189 return bpf_map_offload_update_elem(map, key, value, flags);
190 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
191 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
192 return map->ops->map_update_elem(map, key, value, flags);
193 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
194 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
195 return sock_map_update_elem_sys(map, key, value, flags);
196 } else if (IS_FD_PROG_ARRAY(map)) {
197 return bpf_fd_array_map_update_elem(map, map_file, key, value,
201 bpf_disable_instrumentation();
202 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
203 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
204 err = bpf_percpu_hash_update(map, key, value, flags);
205 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
206 err = bpf_percpu_array_update(map, key, value, flags);
207 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
208 err = bpf_percpu_cgroup_storage_update(map, key, value,
210 } else if (IS_FD_ARRAY(map)) {
212 err = bpf_fd_array_map_update_elem(map, map_file, key, value,
215 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
217 err = bpf_fd_htab_map_update_elem(map, map_file, key, value,
220 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
221 /* rcu_read_lock() is not needed */
222 err = bpf_fd_reuseport_array_update_elem(map, key, value,
224 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
225 map->map_type == BPF_MAP_TYPE_STACK ||
226 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
227 err = map->ops->map_push_elem(map, value, flags);
230 err = map->ops->map_update_elem(map, key, value, flags);
233 bpf_enable_instrumentation();
234 maybe_wait_bpf_programs(map);
239 static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
245 if (bpf_map_is_offloaded(map))
246 return bpf_map_offload_lookup_elem(map, key, value);
248 bpf_disable_instrumentation();
249 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
250 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
251 err = bpf_percpu_hash_copy(map, key, value);
252 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
253 err = bpf_percpu_array_copy(map, key, value);
254 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
255 err = bpf_percpu_cgroup_storage_copy(map, key, value);
256 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
257 err = bpf_stackmap_copy(map, key, value);
258 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
259 err = bpf_fd_array_map_lookup_elem(map, key, value);
260 } else if (IS_FD_HASH(map)) {
261 err = bpf_fd_htab_map_lookup_elem(map, key, value);
262 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
263 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
264 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
265 map->map_type == BPF_MAP_TYPE_STACK ||
266 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
267 err = map->ops->map_peek_elem(map, value);
268 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
269 /* struct_ops map requires directly updating "value" */
270 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
273 if (map->ops->map_lookup_elem_sys_only)
274 ptr = map->ops->map_lookup_elem_sys_only(map, key);
276 ptr = map->ops->map_lookup_elem(map, key);
283 if (flags & BPF_F_LOCK)
284 /* lock 'ptr' and copy everything but lock */
285 copy_map_value_locked(map, value, ptr, true);
287 copy_map_value(map, value, ptr);
288 /* mask lock and timer, since value wasn't zero inited */
289 check_and_init_map_value(map, value);
294 bpf_enable_instrumentation();
295 maybe_wait_bpf_programs(map);
300 /* Please, do not use this function outside from the map creation path
301 * (e.g. in map update path) without taking care of setting the active
302 * memory cgroup (see at bpf_map_kmalloc_node() for example).
304 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
306 /* We really just want to fail instead of triggering OOM killer
307 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
308 * which is used for lower order allocation requests.
310 * It has been observed that higher order allocation requests done by
311 * vmalloc with __GFP_NORETRY being set might fail due to not trying
312 * to reclaim memory from the page cache, thus we set
313 * __GFP_RETRY_MAYFAIL to avoid such situations.
316 gfp_t gfp = bpf_memcg_flags(__GFP_NOWARN | __GFP_ZERO);
317 unsigned int flags = 0;
318 unsigned long align = 1;
321 if (size >= SIZE_MAX)
324 /* kmalloc()'ed memory can't be mmap()'ed */
326 BUG_ON(!PAGE_ALIGNED(size));
329 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
330 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
336 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
337 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
338 flags, numa_node, __builtin_return_address(0));
341 void *bpf_map_area_alloc(u64 size, int numa_node)
343 return __bpf_map_area_alloc(size, numa_node, false);
346 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
348 return __bpf_map_area_alloc(size, numa_node, true);
351 void bpf_map_area_free(void *area)
356 static u32 bpf_map_flags_retain_permanent(u32 flags)
358 /* Some map creation flags are not tied to the map object but
359 * rather to the map fd instead, so they have no meaning upon
360 * map object inspection since multiple file descriptors with
361 * different (access) properties can exist here. Thus, given
362 * this has zero meaning for the map itself, lets clear these
365 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
368 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
370 map->map_type = attr->map_type;
371 map->key_size = attr->key_size;
372 map->value_size = attr->value_size;
373 map->max_entries = attr->max_entries;
374 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
375 map->numa_node = bpf_map_attr_numa_node(attr);
376 map->map_extra = attr->map_extra;
379 static int bpf_map_alloc_id(struct bpf_map *map)
383 idr_preload(GFP_KERNEL);
384 spin_lock_bh(&map_idr_lock);
385 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
388 spin_unlock_bh(&map_idr_lock);
391 if (WARN_ON_ONCE(!id))
394 return id > 0 ? 0 : id;
397 void bpf_map_free_id(struct bpf_map *map)
401 /* Offloaded maps are removed from the IDR store when their device
402 * disappears - even if someone holds an fd to them they are unusable,
403 * the memory is gone, all ops will fail; they are simply waiting for
404 * refcnt to drop to be freed.
409 spin_lock_irqsave(&map_idr_lock, flags);
411 idr_remove(&map_idr, map->id);
414 spin_unlock_irqrestore(&map_idr_lock, flags);
417 #ifdef CONFIG_MEMCG_KMEM
418 static void bpf_map_save_memcg(struct bpf_map *map)
420 /* Currently if a map is created by a process belonging to the root
421 * memory cgroup, get_obj_cgroup_from_current() will return NULL.
422 * So we have to check map->objcg for being NULL each time it's
425 if (memcg_bpf_enabled())
426 map->objcg = get_obj_cgroup_from_current();
429 static void bpf_map_release_memcg(struct bpf_map *map)
432 obj_cgroup_put(map->objcg);
435 static struct mem_cgroup *bpf_map_get_memcg(const struct bpf_map *map)
438 return get_mem_cgroup_from_objcg(map->objcg);
440 return root_mem_cgroup;
443 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
446 struct mem_cgroup *memcg, *old_memcg;
449 memcg = bpf_map_get_memcg(map);
450 old_memcg = set_active_memcg(memcg);
451 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
452 set_active_memcg(old_memcg);
453 mem_cgroup_put(memcg);
458 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
460 struct mem_cgroup *memcg, *old_memcg;
463 memcg = bpf_map_get_memcg(map);
464 old_memcg = set_active_memcg(memcg);
465 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
466 set_active_memcg(old_memcg);
467 mem_cgroup_put(memcg);
472 void *bpf_map_kvcalloc(struct bpf_map *map, size_t n, size_t size,
475 struct mem_cgroup *memcg, *old_memcg;
478 memcg = bpf_map_get_memcg(map);
479 old_memcg = set_active_memcg(memcg);
480 ptr = kvcalloc(n, size, flags | __GFP_ACCOUNT);
481 set_active_memcg(old_memcg);
482 mem_cgroup_put(memcg);
487 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
488 size_t align, gfp_t flags)
490 struct mem_cgroup *memcg, *old_memcg;
493 memcg = bpf_map_get_memcg(map);
494 old_memcg = set_active_memcg(memcg);
495 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
496 set_active_memcg(old_memcg);
497 mem_cgroup_put(memcg);
503 static void bpf_map_save_memcg(struct bpf_map *map)
507 static void bpf_map_release_memcg(struct bpf_map *map)
512 static int btf_field_cmp(const void *a, const void *b)
514 const struct btf_field *f1 = a, *f2 = b;
516 if (f1->offset < f2->offset)
518 else if (f1->offset > f2->offset)
523 struct btf_field *btf_record_find(const struct btf_record *rec, u32 offset,
526 struct btf_field *field;
528 if (IS_ERR_OR_NULL(rec) || !(rec->field_mask & field_mask))
530 field = bsearch(&offset, rec->fields, rec->cnt, sizeof(rec->fields[0]), btf_field_cmp);
531 if (!field || !(field->type & field_mask))
536 void btf_record_free(struct btf_record *rec)
540 if (IS_ERR_OR_NULL(rec))
542 for (i = 0; i < rec->cnt; i++) {
543 switch (rec->fields[i].type) {
546 if (rec->fields[i].kptr.module)
547 module_put(rec->fields[i].kptr.module);
548 btf_put(rec->fields[i].kptr.btf);
557 /* Nothing to release */
567 void bpf_map_free_record(struct bpf_map *map)
569 btf_record_free(map->record);
573 struct btf_record *btf_record_dup(const struct btf_record *rec)
575 const struct btf_field *fields;
576 struct btf_record *new_rec;
579 if (IS_ERR_OR_NULL(rec))
581 size = offsetof(struct btf_record, fields[rec->cnt]);
582 new_rec = kmemdup(rec, size, GFP_KERNEL | __GFP_NOWARN);
584 return ERR_PTR(-ENOMEM);
585 /* Do a deep copy of the btf_record */
586 fields = rec->fields;
588 for (i = 0; i < rec->cnt; i++) {
589 switch (fields[i].type) {
592 btf_get(fields[i].kptr.btf);
593 if (fields[i].kptr.module && !try_module_get(fields[i].kptr.module)) {
605 /* Nothing to acquire */
616 btf_record_free(new_rec);
620 bool btf_record_equal(const struct btf_record *rec_a, const struct btf_record *rec_b)
622 bool a_has_fields = !IS_ERR_OR_NULL(rec_a), b_has_fields = !IS_ERR_OR_NULL(rec_b);
625 if (!a_has_fields && !b_has_fields)
627 if (a_has_fields != b_has_fields)
629 if (rec_a->cnt != rec_b->cnt)
631 size = offsetof(struct btf_record, fields[rec_a->cnt]);
632 /* btf_parse_fields uses kzalloc to allocate a btf_record, so unused
633 * members are zeroed out. So memcmp is safe to do without worrying
634 * about padding/unused fields.
636 * While spin_lock, timer, and kptr have no relation to map BTF,
637 * list_head metadata is specific to map BTF, the btf and value_rec
638 * members in particular. btf is the map BTF, while value_rec points to
639 * btf_record in that map BTF.
641 * So while by default, we don't rely on the map BTF (which the records
642 * were parsed from) matching for both records, which is not backwards
643 * compatible, in case list_head is part of it, we implicitly rely on
644 * that by way of depending on memcmp succeeding for it.
646 return !memcmp(rec_a, rec_b, size);
649 void bpf_obj_free_timer(const struct btf_record *rec, void *obj)
651 if (WARN_ON_ONCE(!btf_record_has_field(rec, BPF_TIMER)))
653 bpf_timer_cancel_and_free(obj + rec->timer_off);
656 extern void __bpf_obj_drop_impl(void *p, const struct btf_record *rec);
658 void bpf_obj_free_fields(const struct btf_record *rec, void *obj)
660 const struct btf_field *fields;
663 if (IS_ERR_OR_NULL(rec))
665 fields = rec->fields;
666 for (i = 0; i < rec->cnt; i++) {
667 struct btf_struct_meta *pointee_struct_meta;
668 const struct btf_field *field = &fields[i];
669 void *field_ptr = obj + field->offset;
672 switch (fields[i].type) {
676 bpf_timer_cancel_and_free(field_ptr);
679 WRITE_ONCE(*(u64 *)field_ptr, 0);
682 xchgd_field = (void *)xchg((unsigned long *)field_ptr, 0);
686 if (!btf_is_kernel(field->kptr.btf)) {
687 pointee_struct_meta = btf_find_struct_meta(field->kptr.btf,
689 WARN_ON_ONCE(!pointee_struct_meta);
691 __bpf_obj_drop_impl(xchgd_field, pointee_struct_meta ?
692 pointee_struct_meta->record :
696 field->kptr.dtor(xchgd_field);
700 if (WARN_ON_ONCE(rec->spin_lock_off < 0))
702 bpf_list_head_free(field, field_ptr, obj + rec->spin_lock_off);
705 if (WARN_ON_ONCE(rec->spin_lock_off < 0))
707 bpf_rb_root_free(field, field_ptr, obj + rec->spin_lock_off);
720 /* called from workqueue */
721 static void bpf_map_free_deferred(struct work_struct *work)
723 struct bpf_map *map = container_of(work, struct bpf_map, work);
724 struct btf_record *rec = map->record;
726 security_bpf_map_free(map);
727 bpf_map_release_memcg(map);
728 /* implementation dependent freeing */
729 map->ops->map_free(map);
730 /* Delay freeing of btf_record for maps, as map_free
731 * callback usually needs access to them. It is better to do it here
732 * than require each callback to do the free itself manually.
734 * Note that the btf_record stashed in map->inner_map_meta->record was
735 * already freed using the map_free callback for map in map case which
736 * eventually calls bpf_map_free_meta, since inner_map_meta is only a
737 * template bpf_map struct used during verification.
739 btf_record_free(rec);
742 static void bpf_map_put_uref(struct bpf_map *map)
744 if (atomic64_dec_and_test(&map->usercnt)) {
745 if (map->ops->map_release_uref)
746 map->ops->map_release_uref(map);
750 /* decrement map refcnt and schedule it for freeing via workqueue
751 * (underlying map implementation ops->map_free() might sleep)
753 void bpf_map_put(struct bpf_map *map)
755 if (atomic64_dec_and_test(&map->refcnt)) {
756 /* bpf_map_free_id() must be called first */
757 bpf_map_free_id(map);
759 INIT_WORK(&map->work, bpf_map_free_deferred);
760 /* Avoid spawning kworkers, since they all might contend
761 * for the same mutex like slab_mutex.
763 queue_work(system_unbound_wq, &map->work);
766 EXPORT_SYMBOL_GPL(bpf_map_put);
768 void bpf_map_put_with_uref(struct bpf_map *map)
770 bpf_map_put_uref(map);
774 static int bpf_map_release(struct inode *inode, struct file *filp)
776 struct bpf_map *map = filp->private_data;
778 if (map->ops->map_release)
779 map->ops->map_release(map, filp);
781 bpf_map_put_with_uref(map);
785 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
787 fmode_t mode = f.file->f_mode;
789 /* Our file permissions may have been overridden by global
790 * map permissions facing syscall side.
792 if (READ_ONCE(map->frozen))
793 mode &= ~FMODE_CAN_WRITE;
797 #ifdef CONFIG_PROC_FS
798 /* Show the memory usage of a bpf map */
799 static u64 bpf_map_memory_usage(const struct bpf_map *map)
801 return map->ops->map_mem_usage(map);
804 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
806 struct bpf_map *map = filp->private_data;
807 u32 type = 0, jited = 0;
809 if (map_type_contains_progs(map)) {
810 spin_lock(&map->owner.lock);
811 type = map->owner.type;
812 jited = map->owner.jited;
813 spin_unlock(&map->owner.lock);
822 "map_extra:\t%#llx\n"
831 (unsigned long long)map->map_extra,
832 bpf_map_memory_usage(map),
834 READ_ONCE(map->frozen));
836 seq_printf(m, "owner_prog_type:\t%u\n", type);
837 seq_printf(m, "owner_jited:\t%u\n", jited);
842 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
845 /* We need this handler such that alloc_file() enables
846 * f_mode with FMODE_CAN_READ.
851 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
852 size_t siz, loff_t *ppos)
854 /* We need this handler such that alloc_file() enables
855 * f_mode with FMODE_CAN_WRITE.
860 /* called for any extra memory-mapped regions (except initial) */
861 static void bpf_map_mmap_open(struct vm_area_struct *vma)
863 struct bpf_map *map = vma->vm_file->private_data;
865 if (vma->vm_flags & VM_MAYWRITE)
866 bpf_map_write_active_inc(map);
869 /* called for all unmapped memory region (including initial) */
870 static void bpf_map_mmap_close(struct vm_area_struct *vma)
872 struct bpf_map *map = vma->vm_file->private_data;
874 if (vma->vm_flags & VM_MAYWRITE)
875 bpf_map_write_active_dec(map);
878 static const struct vm_operations_struct bpf_map_default_vmops = {
879 .open = bpf_map_mmap_open,
880 .close = bpf_map_mmap_close,
883 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
885 struct bpf_map *map = filp->private_data;
888 if (!map->ops->map_mmap || !IS_ERR_OR_NULL(map->record))
891 if (!(vma->vm_flags & VM_SHARED))
894 mutex_lock(&map->freeze_mutex);
896 if (vma->vm_flags & VM_WRITE) {
901 /* map is meant to be read-only, so do not allow mapping as
902 * writable, because it's possible to leak a writable page
903 * reference and allows user-space to still modify it after
904 * freezing, while verifier will assume contents do not change
906 if (map->map_flags & BPF_F_RDONLY_PROG) {
912 /* set default open/close callbacks */
913 vma->vm_ops = &bpf_map_default_vmops;
914 vma->vm_private_data = map;
915 vm_flags_clear(vma, VM_MAYEXEC);
916 if (!(vma->vm_flags & VM_WRITE))
917 /* disallow re-mapping with PROT_WRITE */
918 vm_flags_clear(vma, VM_MAYWRITE);
920 err = map->ops->map_mmap(map, vma);
924 if (vma->vm_flags & VM_MAYWRITE)
925 bpf_map_write_active_inc(map);
927 mutex_unlock(&map->freeze_mutex);
931 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
933 struct bpf_map *map = filp->private_data;
935 if (map->ops->map_poll)
936 return map->ops->map_poll(map, filp, pts);
941 const struct file_operations bpf_map_fops = {
942 #ifdef CONFIG_PROC_FS
943 .show_fdinfo = bpf_map_show_fdinfo,
945 .release = bpf_map_release,
946 .read = bpf_dummy_read,
947 .write = bpf_dummy_write,
948 .mmap = bpf_map_mmap,
949 .poll = bpf_map_poll,
952 int bpf_map_new_fd(struct bpf_map *map, int flags)
956 ret = security_bpf_map(map, OPEN_FMODE(flags));
960 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
964 int bpf_get_file_flag(int flags)
966 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
968 if (flags & BPF_F_RDONLY)
970 if (flags & BPF_F_WRONLY)
975 /* helper macro to check that unused fields 'union bpf_attr' are zero */
976 #define CHECK_ATTR(CMD) \
977 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
978 sizeof(attr->CMD##_LAST_FIELD), 0, \
980 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
981 sizeof(attr->CMD##_LAST_FIELD)) != NULL
983 /* dst and src must have at least "size" number of bytes.
984 * Return strlen on success and < 0 on error.
986 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
988 const char *end = src + size;
989 const char *orig_src = src;
991 memset(dst, 0, size);
992 /* Copy all isalnum(), '_' and '.' chars. */
993 while (src < end && *src) {
994 if (!isalnum(*src) &&
995 *src != '_' && *src != '.')
1000 /* No '\0' found in "size" number of bytes */
1004 return src - orig_src;
1007 int map_check_no_btf(const struct bpf_map *map,
1008 const struct btf *btf,
1009 const struct btf_type *key_type,
1010 const struct btf_type *value_type)
1015 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
1016 u32 btf_key_id, u32 btf_value_id)
1018 const struct btf_type *key_type, *value_type;
1019 u32 key_size, value_size;
1022 /* Some maps allow key to be unspecified. */
1024 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
1025 if (!key_type || key_size != map->key_size)
1028 key_type = btf_type_by_id(btf, 0);
1029 if (!map->ops->map_check_btf)
1033 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
1034 if (!value_type || value_size != map->value_size)
1037 map->record = btf_parse_fields(btf, value_type,
1038 BPF_SPIN_LOCK | BPF_TIMER | BPF_KPTR | BPF_LIST_HEAD |
1039 BPF_RB_ROOT | BPF_REFCOUNT,
1041 if (!IS_ERR_OR_NULL(map->record)) {
1044 if (!bpf_capable()) {
1048 if (map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) {
1052 for (i = 0; i < sizeof(map->record->field_mask) * 8; i++) {
1053 switch (map->record->field_mask & (1 << i)) {
1057 if (map->map_type != BPF_MAP_TYPE_HASH &&
1058 map->map_type != BPF_MAP_TYPE_ARRAY &&
1059 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
1060 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
1061 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
1062 map->map_type != BPF_MAP_TYPE_TASK_STORAGE &&
1063 map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) {
1069 if (map->map_type != BPF_MAP_TYPE_HASH &&
1070 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1071 map->map_type != BPF_MAP_TYPE_ARRAY) {
1076 case BPF_KPTR_UNREF:
1079 if (map->map_type != BPF_MAP_TYPE_HASH &&
1080 map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
1081 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1082 map->map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH &&
1083 map->map_type != BPF_MAP_TYPE_ARRAY &&
1084 map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY &&
1085 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
1086 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
1087 map->map_type != BPF_MAP_TYPE_TASK_STORAGE &&
1088 map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) {
1095 if (map->map_type != BPF_MAP_TYPE_HASH &&
1096 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1097 map->map_type != BPF_MAP_TYPE_ARRAY) {
1103 /* Fail if map_type checks are missing for a field type */
1110 ret = btf_check_and_fixup_fields(btf, map->record);
1114 if (map->ops->map_check_btf) {
1115 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
1122 bpf_map_free_record(map);
1126 #define BPF_MAP_CREATE_LAST_FIELD map_extra
1127 /* called via syscall */
1128 static int map_create(union bpf_attr *attr)
1130 int numa_node = bpf_map_attr_numa_node(attr);
1131 struct bpf_map *map;
1135 err = CHECK_ATTR(BPF_MAP_CREATE);
1139 if (attr->btf_vmlinux_value_type_id) {
1140 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
1141 attr->btf_key_type_id || attr->btf_value_type_id)
1143 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
1147 if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER &&
1148 attr->map_extra != 0)
1151 f_flags = bpf_get_file_flag(attr->map_flags);
1155 if (numa_node != NUMA_NO_NODE &&
1156 ((unsigned int)numa_node >= nr_node_ids ||
1157 !node_online(numa_node)))
1160 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
1161 map = find_and_alloc_map(attr);
1163 return PTR_ERR(map);
1165 err = bpf_obj_name_cpy(map->name, attr->map_name,
1166 sizeof(attr->map_name));
1170 atomic64_set(&map->refcnt, 1);
1171 atomic64_set(&map->usercnt, 1);
1172 mutex_init(&map->freeze_mutex);
1173 spin_lock_init(&map->owner.lock);
1175 if (attr->btf_key_type_id || attr->btf_value_type_id ||
1176 /* Even the map's value is a kernel's struct,
1177 * the bpf_prog.o must have BTF to begin with
1178 * to figure out the corresponding kernel's
1179 * counter part. Thus, attr->btf_fd has
1182 attr->btf_vmlinux_value_type_id) {
1185 btf = btf_get_by_fd(attr->btf_fd);
1190 if (btf_is_kernel(btf)) {
1197 if (attr->btf_value_type_id) {
1198 err = map_check_btf(map, btf, attr->btf_key_type_id,
1199 attr->btf_value_type_id);
1204 map->btf_key_type_id = attr->btf_key_type_id;
1205 map->btf_value_type_id = attr->btf_value_type_id;
1206 map->btf_vmlinux_value_type_id =
1207 attr->btf_vmlinux_value_type_id;
1210 err = security_bpf_map_alloc(map);
1214 err = bpf_map_alloc_id(map);
1218 bpf_map_save_memcg(map);
1220 err = bpf_map_new_fd(map, f_flags);
1222 /* failed to allocate fd.
1223 * bpf_map_put_with_uref() is needed because the above
1224 * bpf_map_alloc_id() has published the map
1225 * to the userspace and the userspace may
1226 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
1228 bpf_map_put_with_uref(map);
1235 security_bpf_map_free(map);
1238 map->ops->map_free(map);
1242 /* if error is returned, fd is released.
1243 * On success caller should complete fd access with matching fdput()
1245 struct bpf_map *__bpf_map_get(struct fd f)
1248 return ERR_PTR(-EBADF);
1249 if (f.file->f_op != &bpf_map_fops) {
1251 return ERR_PTR(-EINVAL);
1254 return f.file->private_data;
1257 void bpf_map_inc(struct bpf_map *map)
1259 atomic64_inc(&map->refcnt);
1261 EXPORT_SYMBOL_GPL(bpf_map_inc);
1263 void bpf_map_inc_with_uref(struct bpf_map *map)
1265 atomic64_inc(&map->refcnt);
1266 atomic64_inc(&map->usercnt);
1268 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
1270 struct bpf_map *bpf_map_get(u32 ufd)
1272 struct fd f = fdget(ufd);
1273 struct bpf_map *map;
1275 map = __bpf_map_get(f);
1284 EXPORT_SYMBOL(bpf_map_get);
1286 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
1288 struct fd f = fdget(ufd);
1289 struct bpf_map *map;
1291 map = __bpf_map_get(f);
1295 bpf_map_inc_with_uref(map);
1301 /* map_idr_lock should have been held or the map should have been
1302 * protected by rcu read lock.
1304 struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
1308 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
1310 return ERR_PTR(-ENOENT);
1312 atomic64_inc(&map->usercnt);
1317 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
1319 spin_lock_bh(&map_idr_lock);
1320 map = __bpf_map_inc_not_zero(map, false);
1321 spin_unlock_bh(&map_idr_lock);
1325 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
1327 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1332 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1335 return vmemdup_user(ukey, key_size);
1338 return ERR_PTR(-EINVAL);
1343 static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1346 return kvmemdup_bpfptr(ukey, key_size);
1348 if (!bpfptr_is_null(ukey))
1349 return ERR_PTR(-EINVAL);
1354 /* last field in 'union bpf_attr' used by this command */
1355 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1357 static int map_lookup_elem(union bpf_attr *attr)
1359 void __user *ukey = u64_to_user_ptr(attr->key);
1360 void __user *uvalue = u64_to_user_ptr(attr->value);
1361 int ufd = attr->map_fd;
1362 struct bpf_map *map;
1368 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1371 if (attr->flags & ~BPF_F_LOCK)
1375 map = __bpf_map_get(f);
1377 return PTR_ERR(map);
1378 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1383 if ((attr->flags & BPF_F_LOCK) &&
1384 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1389 key = __bpf_copy_key(ukey, map->key_size);
1395 value_size = bpf_map_value_size(map);
1398 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1402 if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
1403 if (copy_from_user(value, uvalue, value_size))
1406 err = bpf_map_copy_value(map, key, value, attr->flags);
1410 err = bpf_map_copy_value(map, key, value, attr->flags);
1415 if (copy_to_user(uvalue, value, value_size) != 0)
1430 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1432 static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
1434 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1435 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
1436 int ufd = attr->map_fd;
1437 struct bpf_map *map;
1443 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1447 map = __bpf_map_get(f);
1449 return PTR_ERR(map);
1450 bpf_map_write_active_inc(map);
1451 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1456 if ((attr->flags & BPF_F_LOCK) &&
1457 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1462 key = ___bpf_copy_key(ukey, map->key_size);
1468 value_size = bpf_map_value_size(map);
1469 value = kvmemdup_bpfptr(uvalue, value_size);
1470 if (IS_ERR(value)) {
1471 err = PTR_ERR(value);
1475 err = bpf_map_update_value(map, f.file, key, value, attr->flags);
1481 bpf_map_write_active_dec(map);
1486 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1488 static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr)
1490 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1491 int ufd = attr->map_fd;
1492 struct bpf_map *map;
1497 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1501 map = __bpf_map_get(f);
1503 return PTR_ERR(map);
1504 bpf_map_write_active_inc(map);
1505 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1510 key = ___bpf_copy_key(ukey, map->key_size);
1516 if (bpf_map_is_offloaded(map)) {
1517 err = bpf_map_offload_delete_elem(map, key);
1519 } else if (IS_FD_PROG_ARRAY(map) ||
1520 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1521 /* These maps require sleepable context */
1522 err = map->ops->map_delete_elem(map, key);
1526 bpf_disable_instrumentation();
1528 err = map->ops->map_delete_elem(map, key);
1530 bpf_enable_instrumentation();
1531 maybe_wait_bpf_programs(map);
1535 bpf_map_write_active_dec(map);
1540 /* last field in 'union bpf_attr' used by this command */
1541 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1543 static int map_get_next_key(union bpf_attr *attr)
1545 void __user *ukey = u64_to_user_ptr(attr->key);
1546 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1547 int ufd = attr->map_fd;
1548 struct bpf_map *map;
1549 void *key, *next_key;
1553 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1557 map = __bpf_map_get(f);
1559 return PTR_ERR(map);
1560 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1566 key = __bpf_copy_key(ukey, map->key_size);
1576 next_key = kvmalloc(map->key_size, GFP_USER);
1580 if (bpf_map_is_offloaded(map)) {
1581 err = bpf_map_offload_get_next_key(map, key, next_key);
1586 err = map->ops->map_get_next_key(map, key, next_key);
1593 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1607 int generic_map_delete_batch(struct bpf_map *map,
1608 const union bpf_attr *attr,
1609 union bpf_attr __user *uattr)
1611 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1616 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1619 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1620 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1624 max_count = attr->batch.count;
1628 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1632 for (cp = 0; cp < max_count; cp++) {
1634 if (copy_from_user(key, keys + cp * map->key_size,
1638 if (bpf_map_is_offloaded(map)) {
1639 err = bpf_map_offload_delete_elem(map, key);
1643 bpf_disable_instrumentation();
1645 err = map->ops->map_delete_elem(map, key);
1647 bpf_enable_instrumentation();
1652 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1657 maybe_wait_bpf_programs(map);
1661 int generic_map_update_batch(struct bpf_map *map, struct file *map_file,
1662 const union bpf_attr *attr,
1663 union bpf_attr __user *uattr)
1665 void __user *values = u64_to_user_ptr(attr->batch.values);
1666 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1667 u32 value_size, cp, max_count;
1671 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1674 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1675 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1679 value_size = bpf_map_value_size(map);
1681 max_count = attr->batch.count;
1685 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1689 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1695 for (cp = 0; cp < max_count; cp++) {
1697 if (copy_from_user(key, keys + cp * map->key_size,
1699 copy_from_user(value, values + cp * value_size, value_size))
1702 err = bpf_map_update_value(map, map_file, key, value,
1703 attr->batch.elem_flags);
1710 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1718 #define MAP_LOOKUP_RETRIES 3
1720 int generic_map_lookup_batch(struct bpf_map *map,
1721 const union bpf_attr *attr,
1722 union bpf_attr __user *uattr)
1724 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1725 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1726 void __user *values = u64_to_user_ptr(attr->batch.values);
1727 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1728 void *buf, *buf_prevkey, *prev_key, *key, *value;
1729 int err, retry = MAP_LOOKUP_RETRIES;
1730 u32 value_size, cp, max_count;
1732 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1735 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1736 !btf_record_has_field(map->record, BPF_SPIN_LOCK))
1739 value_size = bpf_map_value_size(map);
1741 max_count = attr->batch.count;
1745 if (put_user(0, &uattr->batch.count))
1748 buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1752 buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1754 kvfree(buf_prevkey);
1760 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1763 value = key + map->key_size;
1765 prev_key = buf_prevkey;
1767 for (cp = 0; cp < max_count;) {
1769 err = map->ops->map_get_next_key(map, prev_key, key);
1773 err = bpf_map_copy_value(map, key, value,
1774 attr->batch.elem_flags);
1776 if (err == -ENOENT) {
1788 if (copy_to_user(keys + cp * map->key_size, key,
1793 if (copy_to_user(values + cp * value_size, value, value_size)) {
1799 prev_key = buf_prevkey;
1801 swap(prev_key, key);
1802 retry = MAP_LOOKUP_RETRIES;
1810 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1811 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1815 kvfree(buf_prevkey);
1820 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
1822 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1824 void __user *ukey = u64_to_user_ptr(attr->key);
1825 void __user *uvalue = u64_to_user_ptr(attr->value);
1826 int ufd = attr->map_fd;
1827 struct bpf_map *map;
1833 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1836 if (attr->flags & ~BPF_F_LOCK)
1840 map = __bpf_map_get(f);
1842 return PTR_ERR(map);
1843 bpf_map_write_active_inc(map);
1844 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1845 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1851 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1852 map->map_type == BPF_MAP_TYPE_STACK)) {
1857 if ((attr->flags & BPF_F_LOCK) &&
1858 !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1863 key = __bpf_copy_key(ukey, map->key_size);
1869 value_size = bpf_map_value_size(map);
1872 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1877 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1878 map->map_type == BPF_MAP_TYPE_STACK) {
1879 err = map->ops->map_pop_elem(map, value);
1880 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1881 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1882 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1883 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1884 if (!bpf_map_is_offloaded(map)) {
1885 bpf_disable_instrumentation();
1887 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1889 bpf_enable_instrumentation();
1896 if (copy_to_user(uvalue, value, value_size) != 0) {
1908 bpf_map_write_active_dec(map);
1913 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1915 static int map_freeze(const union bpf_attr *attr)
1917 int err = 0, ufd = attr->map_fd;
1918 struct bpf_map *map;
1921 if (CHECK_ATTR(BPF_MAP_FREEZE))
1925 map = __bpf_map_get(f);
1927 return PTR_ERR(map);
1929 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS || !IS_ERR_OR_NULL(map->record)) {
1934 mutex_lock(&map->freeze_mutex);
1935 if (bpf_map_write_active(map)) {
1939 if (READ_ONCE(map->frozen)) {
1943 if (!bpf_capable()) {
1948 WRITE_ONCE(map->frozen, true);
1950 mutex_unlock(&map->freeze_mutex);
1955 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1956 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1957 [_id] = & _name ## _prog_ops,
1958 #define BPF_MAP_TYPE(_id, _ops)
1959 #define BPF_LINK_TYPE(_id, _name)
1960 #include <linux/bpf_types.h>
1961 #undef BPF_PROG_TYPE
1963 #undef BPF_LINK_TYPE
1966 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1968 const struct bpf_prog_ops *ops;
1970 if (type >= ARRAY_SIZE(bpf_prog_types))
1972 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1973 ops = bpf_prog_types[type];
1977 if (!bpf_prog_is_offloaded(prog->aux))
1978 prog->aux->ops = ops;
1980 prog->aux->ops = &bpf_offload_prog_ops;
1991 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1992 [BPF_AUDIT_LOAD] = "LOAD",
1993 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1996 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1998 struct audit_context *ctx = NULL;
1999 struct audit_buffer *ab;
2001 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
2003 if (audit_enabled == AUDIT_OFF)
2005 if (!in_irq() && !irqs_disabled())
2006 ctx = audit_context();
2007 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
2010 audit_log_format(ab, "prog-id=%u op=%s",
2011 prog->aux->id, bpf_audit_str[op]);
2015 static int bpf_prog_alloc_id(struct bpf_prog *prog)
2019 idr_preload(GFP_KERNEL);
2020 spin_lock_bh(&prog_idr_lock);
2021 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
2024 spin_unlock_bh(&prog_idr_lock);
2027 /* id is in [1, INT_MAX) */
2028 if (WARN_ON_ONCE(!id))
2031 return id > 0 ? 0 : id;
2034 void bpf_prog_free_id(struct bpf_prog *prog)
2036 unsigned long flags;
2038 /* cBPF to eBPF migrations are currently not in the idr store.
2039 * Offloaded programs are removed from the store when their device
2040 * disappears - even if someone grabs an fd to them they are unusable,
2041 * simply waiting for refcnt to drop to be freed.
2046 spin_lock_irqsave(&prog_idr_lock, flags);
2047 idr_remove(&prog_idr, prog->aux->id);
2049 spin_unlock_irqrestore(&prog_idr_lock, flags);
2052 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
2054 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
2056 kvfree(aux->func_info);
2057 kfree(aux->func_info_aux);
2058 free_uid(aux->user);
2059 security_bpf_prog_free(aux);
2060 bpf_prog_free(aux->prog);
2063 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
2065 bpf_prog_kallsyms_del_all(prog);
2066 btf_put(prog->aux->btf);
2067 module_put(prog->aux->mod);
2068 kvfree(prog->aux->jited_linfo);
2069 kvfree(prog->aux->linfo);
2070 kfree(prog->aux->kfunc_tab);
2071 if (prog->aux->attach_btf)
2072 btf_put(prog->aux->attach_btf);
2075 if (prog->aux->sleepable)
2076 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
2078 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
2080 __bpf_prog_put_rcu(&prog->aux->rcu);
2084 static void bpf_prog_put_deferred(struct work_struct *work)
2086 struct bpf_prog_aux *aux;
2087 struct bpf_prog *prog;
2089 aux = container_of(work, struct bpf_prog_aux, work);
2091 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
2092 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
2093 bpf_prog_free_id(prog);
2094 __bpf_prog_put_noref(prog, true);
2097 static void __bpf_prog_put(struct bpf_prog *prog)
2099 struct bpf_prog_aux *aux = prog->aux;
2101 if (atomic64_dec_and_test(&aux->refcnt)) {
2102 if (in_irq() || irqs_disabled()) {
2103 INIT_WORK(&aux->work, bpf_prog_put_deferred);
2104 schedule_work(&aux->work);
2106 bpf_prog_put_deferred(&aux->work);
2111 void bpf_prog_put(struct bpf_prog *prog)
2113 __bpf_prog_put(prog);
2115 EXPORT_SYMBOL_GPL(bpf_prog_put);
2117 static int bpf_prog_release(struct inode *inode, struct file *filp)
2119 struct bpf_prog *prog = filp->private_data;
2125 struct bpf_prog_kstats {
2131 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog)
2133 struct bpf_prog_stats *stats;
2136 stats = this_cpu_ptr(prog->stats);
2137 flags = u64_stats_update_begin_irqsave(&stats->syncp);
2138 u64_stats_inc(&stats->misses);
2139 u64_stats_update_end_irqrestore(&stats->syncp, flags);
2142 static void bpf_prog_get_stats(const struct bpf_prog *prog,
2143 struct bpf_prog_kstats *stats)
2145 u64 nsecs = 0, cnt = 0, misses = 0;
2148 for_each_possible_cpu(cpu) {
2149 const struct bpf_prog_stats *st;
2151 u64 tnsecs, tcnt, tmisses;
2153 st = per_cpu_ptr(prog->stats, cpu);
2155 start = u64_stats_fetch_begin(&st->syncp);
2156 tnsecs = u64_stats_read(&st->nsecs);
2157 tcnt = u64_stats_read(&st->cnt);
2158 tmisses = u64_stats_read(&st->misses);
2159 } while (u64_stats_fetch_retry(&st->syncp, start));
2164 stats->nsecs = nsecs;
2166 stats->misses = misses;
2169 #ifdef CONFIG_PROC_FS
2170 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
2172 const struct bpf_prog *prog = filp->private_data;
2173 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2174 struct bpf_prog_kstats stats;
2176 bpf_prog_get_stats(prog, &stats);
2177 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2184 "run_time_ns:\t%llu\n"
2186 "recursion_misses:\t%llu\n"
2187 "verified_insns:\t%u\n",
2191 prog->pages * 1ULL << PAGE_SHIFT,
2196 prog->aux->verified_insns);
2200 const struct file_operations bpf_prog_fops = {
2201 #ifdef CONFIG_PROC_FS
2202 .show_fdinfo = bpf_prog_show_fdinfo,
2204 .release = bpf_prog_release,
2205 .read = bpf_dummy_read,
2206 .write = bpf_dummy_write,
2209 int bpf_prog_new_fd(struct bpf_prog *prog)
2213 ret = security_bpf_prog(prog);
2217 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
2218 O_RDWR | O_CLOEXEC);
2221 static struct bpf_prog *____bpf_prog_get(struct fd f)
2224 return ERR_PTR(-EBADF);
2225 if (f.file->f_op != &bpf_prog_fops) {
2227 return ERR_PTR(-EINVAL);
2230 return f.file->private_data;
2233 void bpf_prog_add(struct bpf_prog *prog, int i)
2235 atomic64_add(i, &prog->aux->refcnt);
2237 EXPORT_SYMBOL_GPL(bpf_prog_add);
2239 void bpf_prog_sub(struct bpf_prog *prog, int i)
2241 /* Only to be used for undoing previous bpf_prog_add() in some
2242 * error path. We still know that another entity in our call
2243 * path holds a reference to the program, thus atomic_sub() can
2244 * be safely used in such cases!
2246 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
2248 EXPORT_SYMBOL_GPL(bpf_prog_sub);
2250 void bpf_prog_inc(struct bpf_prog *prog)
2252 atomic64_inc(&prog->aux->refcnt);
2254 EXPORT_SYMBOL_GPL(bpf_prog_inc);
2256 /* prog_idr_lock should have been held */
2257 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
2261 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
2264 return ERR_PTR(-ENOENT);
2268 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
2270 bool bpf_prog_get_ok(struct bpf_prog *prog,
2271 enum bpf_prog_type *attach_type, bool attach_drv)
2273 /* not an attachment, just a refcount inc, always allow */
2277 if (prog->type != *attach_type)
2279 if (bpf_prog_is_offloaded(prog->aux) && !attach_drv)
2285 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
2288 struct fd f = fdget(ufd);
2289 struct bpf_prog *prog;
2291 prog = ____bpf_prog_get(f);
2294 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
2295 prog = ERR_PTR(-EINVAL);
2305 struct bpf_prog *bpf_prog_get(u32 ufd)
2307 return __bpf_prog_get(ufd, NULL, false);
2310 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
2313 return __bpf_prog_get(ufd, &type, attach_drv);
2315 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
2317 /* Initially all BPF programs could be loaded w/o specifying
2318 * expected_attach_type. Later for some of them specifying expected_attach_type
2319 * at load time became required so that program could be validated properly.
2320 * Programs of types that are allowed to be loaded both w/ and w/o (for
2321 * backward compatibility) expected_attach_type, should have the default attach
2322 * type assigned to expected_attach_type for the latter case, so that it can be
2323 * validated later at attach time.
2325 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
2326 * prog type requires it but has some attach types that have to be backward
2329 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
2331 switch (attr->prog_type) {
2332 case BPF_PROG_TYPE_CGROUP_SOCK:
2333 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
2334 * exist so checking for non-zero is the way to go here.
2336 if (!attr->expected_attach_type)
2337 attr->expected_attach_type =
2338 BPF_CGROUP_INET_SOCK_CREATE;
2340 case BPF_PROG_TYPE_SK_REUSEPORT:
2341 if (!attr->expected_attach_type)
2342 attr->expected_attach_type =
2343 BPF_SK_REUSEPORT_SELECT;
2349 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2350 enum bpf_attach_type expected_attach_type,
2351 struct btf *attach_btf, u32 btf_id,
2352 struct bpf_prog *dst_prog)
2355 if (btf_id > BTF_MAX_TYPE)
2358 if (!attach_btf && !dst_prog)
2361 switch (prog_type) {
2362 case BPF_PROG_TYPE_TRACING:
2363 case BPF_PROG_TYPE_LSM:
2364 case BPF_PROG_TYPE_STRUCT_OPS:
2365 case BPF_PROG_TYPE_EXT:
2372 if (attach_btf && (!btf_id || dst_prog))
2375 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
2376 prog_type != BPF_PROG_TYPE_EXT)
2379 switch (prog_type) {
2380 case BPF_PROG_TYPE_CGROUP_SOCK:
2381 switch (expected_attach_type) {
2382 case BPF_CGROUP_INET_SOCK_CREATE:
2383 case BPF_CGROUP_INET_SOCK_RELEASE:
2384 case BPF_CGROUP_INET4_POST_BIND:
2385 case BPF_CGROUP_INET6_POST_BIND:
2390 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2391 switch (expected_attach_type) {
2392 case BPF_CGROUP_INET4_BIND:
2393 case BPF_CGROUP_INET6_BIND:
2394 case BPF_CGROUP_INET4_CONNECT:
2395 case BPF_CGROUP_INET6_CONNECT:
2396 case BPF_CGROUP_INET4_GETPEERNAME:
2397 case BPF_CGROUP_INET6_GETPEERNAME:
2398 case BPF_CGROUP_INET4_GETSOCKNAME:
2399 case BPF_CGROUP_INET6_GETSOCKNAME:
2400 case BPF_CGROUP_UDP4_SENDMSG:
2401 case BPF_CGROUP_UDP6_SENDMSG:
2402 case BPF_CGROUP_UDP4_RECVMSG:
2403 case BPF_CGROUP_UDP6_RECVMSG:
2408 case BPF_PROG_TYPE_CGROUP_SKB:
2409 switch (expected_attach_type) {
2410 case BPF_CGROUP_INET_INGRESS:
2411 case BPF_CGROUP_INET_EGRESS:
2416 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2417 switch (expected_attach_type) {
2418 case BPF_CGROUP_SETSOCKOPT:
2419 case BPF_CGROUP_GETSOCKOPT:
2424 case BPF_PROG_TYPE_SK_LOOKUP:
2425 if (expected_attach_type == BPF_SK_LOOKUP)
2428 case BPF_PROG_TYPE_SK_REUSEPORT:
2429 switch (expected_attach_type) {
2430 case BPF_SK_REUSEPORT_SELECT:
2431 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2436 case BPF_PROG_TYPE_SYSCALL:
2437 case BPF_PROG_TYPE_EXT:
2438 if (expected_attach_type)
2446 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2448 switch (prog_type) {
2449 case BPF_PROG_TYPE_SCHED_CLS:
2450 case BPF_PROG_TYPE_SCHED_ACT:
2451 case BPF_PROG_TYPE_XDP:
2452 case BPF_PROG_TYPE_LWT_IN:
2453 case BPF_PROG_TYPE_LWT_OUT:
2454 case BPF_PROG_TYPE_LWT_XMIT:
2455 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2456 case BPF_PROG_TYPE_SK_SKB:
2457 case BPF_PROG_TYPE_SK_MSG:
2458 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2459 case BPF_PROG_TYPE_CGROUP_DEVICE:
2460 case BPF_PROG_TYPE_CGROUP_SOCK:
2461 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2462 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2463 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2464 case BPF_PROG_TYPE_SOCK_OPS:
2465 case BPF_PROG_TYPE_EXT: /* extends any prog */
2466 case BPF_PROG_TYPE_NETFILTER:
2468 case BPF_PROG_TYPE_CGROUP_SKB:
2470 case BPF_PROG_TYPE_SK_REUSEPORT:
2471 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2477 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2479 switch (prog_type) {
2480 case BPF_PROG_TYPE_KPROBE:
2481 case BPF_PROG_TYPE_TRACEPOINT:
2482 case BPF_PROG_TYPE_PERF_EVENT:
2483 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2484 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2485 case BPF_PROG_TYPE_TRACING:
2486 case BPF_PROG_TYPE_LSM:
2487 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2488 case BPF_PROG_TYPE_EXT: /* extends any prog */
2495 /* last field in 'union bpf_attr' used by this command */
2496 #define BPF_PROG_LOAD_LAST_FIELD log_true_size
2498 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
2500 enum bpf_prog_type type = attr->prog_type;
2501 struct bpf_prog *prog, *dst_prog = NULL;
2502 struct btf *attach_btf = NULL;
2507 if (CHECK_ATTR(BPF_PROG_LOAD))
2510 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2511 BPF_F_ANY_ALIGNMENT |
2512 BPF_F_TEST_STATE_FREQ |
2514 BPF_F_TEST_RND_HI32 |
2515 BPF_F_XDP_HAS_FRAGS |
2516 BPF_F_XDP_DEV_BOUND_ONLY))
2519 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2520 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2524 /* copy eBPF program license from user space */
2525 if (strncpy_from_bpfptr(license,
2526 make_bpfptr(attr->license, uattr.is_kernel),
2527 sizeof(license) - 1) < 0)
2529 license[sizeof(license) - 1] = 0;
2531 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2532 is_gpl = license_is_gpl_compatible(license);
2534 if (attr->insn_cnt == 0 ||
2535 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2537 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2538 type != BPF_PROG_TYPE_CGROUP_SKB &&
2542 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2544 if (is_perfmon_prog_type(type) && !perfmon_capable())
2547 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2548 * or btf, we need to check which one it is
2550 if (attr->attach_prog_fd) {
2551 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2552 if (IS_ERR(dst_prog)) {
2554 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2555 if (IS_ERR(attach_btf))
2557 if (!btf_is_kernel(attach_btf)) {
2558 /* attaching through specifying bpf_prog's BTF
2559 * objects directly might be supported eventually
2561 btf_put(attach_btf);
2565 } else if (attr->attach_btf_id) {
2566 /* fall back to vmlinux BTF, if BTF type ID is specified */
2567 attach_btf = bpf_get_btf_vmlinux();
2568 if (IS_ERR(attach_btf))
2569 return PTR_ERR(attach_btf);
2572 btf_get(attach_btf);
2575 bpf_prog_load_fixup_attach_type(attr);
2576 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2577 attach_btf, attr->attach_btf_id,
2580 bpf_prog_put(dst_prog);
2582 btf_put(attach_btf);
2586 /* plain bpf_prog allocation */
2587 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2590 bpf_prog_put(dst_prog);
2592 btf_put(attach_btf);
2596 prog->expected_attach_type = attr->expected_attach_type;
2597 prog->aux->attach_btf = attach_btf;
2598 prog->aux->attach_btf_id = attr->attach_btf_id;
2599 prog->aux->dst_prog = dst_prog;
2600 prog->aux->dev_bound = !!attr->prog_ifindex;
2601 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2602 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
2604 err = security_bpf_prog_alloc(prog->aux);
2608 prog->aux->user = get_current_user();
2609 prog->len = attr->insn_cnt;
2612 if (copy_from_bpfptr(prog->insns,
2613 make_bpfptr(attr->insns, uattr.is_kernel),
2614 bpf_prog_insn_size(prog)) != 0)
2617 prog->orig_prog = NULL;
2620 atomic64_set(&prog->aux->refcnt, 1);
2621 prog->gpl_compatible = is_gpl ? 1 : 0;
2623 if (bpf_prog_is_dev_bound(prog->aux)) {
2624 err = bpf_prog_dev_bound_init(prog, attr);
2629 if (type == BPF_PROG_TYPE_EXT && dst_prog &&
2630 bpf_prog_is_dev_bound(dst_prog->aux)) {
2631 err = bpf_prog_dev_bound_inherit(prog, dst_prog);
2636 /* find program type: socket_filter vs tracing_filter */
2637 err = find_prog_type(type, prog);
2641 prog->aux->load_time = ktime_get_boottime_ns();
2642 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2643 sizeof(attr->prog_name));
2647 /* run eBPF verifier */
2648 err = bpf_check(&prog, attr, uattr, uattr_size);
2650 goto free_used_maps;
2652 prog = bpf_prog_select_runtime(prog, &err);
2654 goto free_used_maps;
2656 err = bpf_prog_alloc_id(prog);
2658 goto free_used_maps;
2660 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2661 * effectively publicly exposed. However, retrieving via
2662 * bpf_prog_get_fd_by_id() will take another reference,
2663 * therefore it cannot be gone underneath us.
2665 * Only for the time /after/ successful bpf_prog_new_fd()
2666 * and before returning to userspace, we might just hold
2667 * one reference and any parallel close on that fd could
2668 * rip everything out. Hence, below notifications must
2669 * happen before bpf_prog_new_fd().
2671 * Also, any failure handling from this point onwards must
2672 * be using bpf_prog_put() given the program is exposed.
2674 bpf_prog_kallsyms_add(prog);
2675 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2676 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2678 err = bpf_prog_new_fd(prog);
2684 /* In case we have subprogs, we need to wait for a grace
2685 * period before we can tear down JIT memory since symbols
2686 * are already exposed under kallsyms.
2688 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2691 free_uid(prog->aux->user);
2692 security_bpf_prog_free(prog->aux);
2694 if (prog->aux->attach_btf)
2695 btf_put(prog->aux->attach_btf);
2696 bpf_prog_free(prog);
2700 #define BPF_OBJ_LAST_FIELD file_flags
2702 static int bpf_obj_pin(const union bpf_attr *attr)
2704 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2707 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2710 static int bpf_obj_get(const union bpf_attr *attr)
2712 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2713 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2716 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2720 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2721 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2723 atomic64_set(&link->refcnt, 1);
2730 static void bpf_link_free_id(int id)
2735 spin_lock_bh(&link_idr_lock);
2736 idr_remove(&link_idr, id);
2737 spin_unlock_bh(&link_idr_lock);
2740 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2741 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2742 * anon_inode's release() call. This helper marksbpf_link as
2743 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2744 * is not decremented, it's the responsibility of a calling code that failed
2745 * to complete bpf_link initialization.
2747 void bpf_link_cleanup(struct bpf_link_primer *primer)
2749 primer->link->prog = NULL;
2750 bpf_link_free_id(primer->id);
2752 put_unused_fd(primer->fd);
2755 void bpf_link_inc(struct bpf_link *link)
2757 atomic64_inc(&link->refcnt);
2760 /* bpf_link_free is guaranteed to be called from process context */
2761 static void bpf_link_free(struct bpf_link *link)
2763 bpf_link_free_id(link->id);
2765 /* detach BPF program, clean up used resources */
2766 link->ops->release(link);
2767 bpf_prog_put(link->prog);
2769 /* free bpf_link and its containing memory */
2770 link->ops->dealloc(link);
2773 static void bpf_link_put_deferred(struct work_struct *work)
2775 struct bpf_link *link = container_of(work, struct bpf_link, work);
2777 bpf_link_free(link);
2780 /* bpf_link_put can be called from atomic context, but ensures that resources
2781 * are freed from process context
2783 void bpf_link_put(struct bpf_link *link)
2785 if (!atomic64_dec_and_test(&link->refcnt))
2789 INIT_WORK(&link->work, bpf_link_put_deferred);
2790 schedule_work(&link->work);
2792 bpf_link_free(link);
2795 EXPORT_SYMBOL(bpf_link_put);
2797 static int bpf_link_release(struct inode *inode, struct file *filp)
2799 struct bpf_link *link = filp->private_data;
2805 #ifdef CONFIG_PROC_FS
2806 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2807 #define BPF_MAP_TYPE(_id, _ops)
2808 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2809 static const char *bpf_link_type_strs[] = {
2810 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2811 #include <linux/bpf_types.h>
2813 #undef BPF_PROG_TYPE
2815 #undef BPF_LINK_TYPE
2817 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2819 const struct bpf_link *link = filp->private_data;
2820 const struct bpf_prog *prog = link->prog;
2821 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2826 bpf_link_type_strs[link->type],
2829 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2836 if (link->ops->show_fdinfo)
2837 link->ops->show_fdinfo(link, m);
2841 static const struct file_operations bpf_link_fops = {
2842 #ifdef CONFIG_PROC_FS
2843 .show_fdinfo = bpf_link_show_fdinfo,
2845 .release = bpf_link_release,
2846 .read = bpf_dummy_read,
2847 .write = bpf_dummy_write,
2850 static int bpf_link_alloc_id(struct bpf_link *link)
2854 idr_preload(GFP_KERNEL);
2855 spin_lock_bh(&link_idr_lock);
2856 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2857 spin_unlock_bh(&link_idr_lock);
2863 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2864 * reserving unused FD and allocating ID from link_idr. This is to be paired
2865 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2866 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2867 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2868 * transient state is passed around in struct bpf_link_primer.
2869 * This is preferred way to create and initialize bpf_link, especially when
2870 * there are complicated and expensive operations in between creating bpf_link
2871 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2872 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2873 * expensive (and potentially failing) roll back operations in a rare case
2874 * that file, FD, or ID can't be allocated.
2876 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2881 fd = get_unused_fd_flags(O_CLOEXEC);
2886 id = bpf_link_alloc_id(link);
2892 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2894 bpf_link_free_id(id);
2896 return PTR_ERR(file);
2899 primer->link = link;
2900 primer->file = file;
2906 int bpf_link_settle(struct bpf_link_primer *primer)
2908 /* make bpf_link fetchable by ID */
2909 spin_lock_bh(&link_idr_lock);
2910 primer->link->id = primer->id;
2911 spin_unlock_bh(&link_idr_lock);
2912 /* make bpf_link fetchable by FD */
2913 fd_install(primer->fd, primer->file);
2914 /* pass through installed FD */
2918 int bpf_link_new_fd(struct bpf_link *link)
2920 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2923 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2925 struct fd f = fdget(ufd);
2926 struct bpf_link *link;
2929 return ERR_PTR(-EBADF);
2930 if (f.file->f_op != &bpf_link_fops) {
2932 return ERR_PTR(-EINVAL);
2935 link = f.file->private_data;
2941 EXPORT_SYMBOL(bpf_link_get_from_fd);
2943 static void bpf_tracing_link_release(struct bpf_link *link)
2945 struct bpf_tracing_link *tr_link =
2946 container_of(link, struct bpf_tracing_link, link.link);
2948 WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link,
2949 tr_link->trampoline));
2951 bpf_trampoline_put(tr_link->trampoline);
2953 /* tgt_prog is NULL if target is a kernel function */
2954 if (tr_link->tgt_prog)
2955 bpf_prog_put(tr_link->tgt_prog);
2958 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2960 struct bpf_tracing_link *tr_link =
2961 container_of(link, struct bpf_tracing_link, link.link);
2966 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2967 struct seq_file *seq)
2969 struct bpf_tracing_link *tr_link =
2970 container_of(link, struct bpf_tracing_link, link.link);
2973 "attach_type:\t%d\n",
2974 tr_link->attach_type);
2977 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2978 struct bpf_link_info *info)
2980 struct bpf_tracing_link *tr_link =
2981 container_of(link, struct bpf_tracing_link, link.link);
2983 info->tracing.attach_type = tr_link->attach_type;
2984 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2985 &info->tracing.target_obj_id,
2986 &info->tracing.target_btf_id);
2991 static const struct bpf_link_ops bpf_tracing_link_lops = {
2992 .release = bpf_tracing_link_release,
2993 .dealloc = bpf_tracing_link_dealloc,
2994 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2995 .fill_link_info = bpf_tracing_link_fill_link_info,
2998 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
3003 struct bpf_link_primer link_primer;
3004 struct bpf_prog *tgt_prog = NULL;
3005 struct bpf_trampoline *tr = NULL;
3006 struct bpf_tracing_link *link;
3010 switch (prog->type) {
3011 case BPF_PROG_TYPE_TRACING:
3012 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
3013 prog->expected_attach_type != BPF_TRACE_FEXIT &&
3014 prog->expected_attach_type != BPF_MODIFY_RETURN) {
3019 case BPF_PROG_TYPE_EXT:
3020 if (prog->expected_attach_type != 0) {
3025 case BPF_PROG_TYPE_LSM:
3026 if (prog->expected_attach_type != BPF_LSM_MAC) {
3036 if (!!tgt_prog_fd != !!btf_id) {
3042 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
3043 if (prog->type != BPF_PROG_TYPE_EXT) {
3048 tgt_prog = bpf_prog_get(tgt_prog_fd);
3049 if (IS_ERR(tgt_prog)) {
3050 err = PTR_ERR(tgt_prog);
3055 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
3058 link = kzalloc(sizeof(*link), GFP_USER);
3063 bpf_link_init(&link->link.link, BPF_LINK_TYPE_TRACING,
3064 &bpf_tracing_link_lops, prog);
3065 link->attach_type = prog->expected_attach_type;
3066 link->link.cookie = bpf_cookie;
3068 mutex_lock(&prog->aux->dst_mutex);
3070 /* There are a few possible cases here:
3072 * - if prog->aux->dst_trampoline is set, the program was just loaded
3073 * and not yet attached to anything, so we can use the values stored
3076 * - if prog->aux->dst_trampoline is NULL, the program has already been
3077 * attached to a target and its initial target was cleared (below)
3079 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
3080 * target_btf_id using the link_create API.
3082 * - if tgt_prog == NULL when this function was called using the old
3083 * raw_tracepoint_open API, and we need a target from prog->aux
3085 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
3086 * was detached and is going for re-attachment.
3088 if (!prog->aux->dst_trampoline && !tgt_prog) {
3090 * Allow re-attach for TRACING and LSM programs. If it's
3091 * currently linked, bpf_trampoline_link_prog will fail.
3092 * EXT programs need to specify tgt_prog_fd, so they
3093 * re-attach in separate code path.
3095 if (prog->type != BPF_PROG_TYPE_TRACING &&
3096 prog->type != BPF_PROG_TYPE_LSM) {
3100 btf_id = prog->aux->attach_btf_id;
3101 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
3104 if (!prog->aux->dst_trampoline ||
3105 (key && key != prog->aux->dst_trampoline->key)) {
3106 /* If there is no saved target, or the specified target is
3107 * different from the destination specified at load time, we
3108 * need a new trampoline and a check for compatibility
3110 struct bpf_attach_target_info tgt_info = {};
3112 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
3117 if (tgt_info.tgt_mod) {
3118 module_put(prog->aux->mod);
3119 prog->aux->mod = tgt_info.tgt_mod;
3122 tr = bpf_trampoline_get(key, &tgt_info);
3128 /* The caller didn't specify a target, or the target was the
3129 * same as the destination supplied during program load. This
3130 * means we can reuse the trampoline and reference from program
3131 * load time, and there is no need to allocate a new one. This
3132 * can only happen once for any program, as the saved values in
3133 * prog->aux are cleared below.
3135 tr = prog->aux->dst_trampoline;
3136 tgt_prog = prog->aux->dst_prog;
3139 err = bpf_link_prime(&link->link.link, &link_primer);
3143 err = bpf_trampoline_link_prog(&link->link, tr);
3145 bpf_link_cleanup(&link_primer);
3150 link->tgt_prog = tgt_prog;
3151 link->trampoline = tr;
3153 /* Always clear the trampoline and target prog from prog->aux to make
3154 * sure the original attach destination is not kept alive after a
3155 * program is (re-)attached to another target.
3157 if (prog->aux->dst_prog &&
3158 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
3159 /* got extra prog ref from syscall, or attaching to different prog */
3160 bpf_prog_put(prog->aux->dst_prog);
3161 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
3162 /* we allocated a new trampoline, so free the old one */
3163 bpf_trampoline_put(prog->aux->dst_trampoline);
3165 prog->aux->dst_prog = NULL;
3166 prog->aux->dst_trampoline = NULL;
3167 mutex_unlock(&prog->aux->dst_mutex);
3169 return bpf_link_settle(&link_primer);
3171 if (tr && tr != prog->aux->dst_trampoline)
3172 bpf_trampoline_put(tr);
3173 mutex_unlock(&prog->aux->dst_mutex);
3176 if (tgt_prog_fd && tgt_prog)
3177 bpf_prog_put(tgt_prog);
3181 struct bpf_raw_tp_link {
3182 struct bpf_link link;
3183 struct bpf_raw_event_map *btp;
3186 static void bpf_raw_tp_link_release(struct bpf_link *link)
3188 struct bpf_raw_tp_link *raw_tp =
3189 container_of(link, struct bpf_raw_tp_link, link);
3191 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
3192 bpf_put_raw_tracepoint(raw_tp->btp);
3195 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
3197 struct bpf_raw_tp_link *raw_tp =
3198 container_of(link, struct bpf_raw_tp_link, link);
3203 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
3204 struct seq_file *seq)
3206 struct bpf_raw_tp_link *raw_tp_link =
3207 container_of(link, struct bpf_raw_tp_link, link);
3211 raw_tp_link->btp->tp->name);
3214 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
3215 struct bpf_link_info *info)
3217 struct bpf_raw_tp_link *raw_tp_link =
3218 container_of(link, struct bpf_raw_tp_link, link);
3219 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
3220 const char *tp_name = raw_tp_link->btp->tp->name;
3221 u32 ulen = info->raw_tracepoint.tp_name_len;
3222 size_t tp_len = strlen(tp_name);
3227 info->raw_tracepoint.tp_name_len = tp_len + 1;
3232 if (ulen >= tp_len + 1) {
3233 if (copy_to_user(ubuf, tp_name, tp_len + 1))
3238 if (copy_to_user(ubuf, tp_name, ulen - 1))
3240 if (put_user(zero, ubuf + ulen - 1))
3248 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
3249 .release = bpf_raw_tp_link_release,
3250 .dealloc = bpf_raw_tp_link_dealloc,
3251 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
3252 .fill_link_info = bpf_raw_tp_link_fill_link_info,
3255 #ifdef CONFIG_PERF_EVENTS
3256 struct bpf_perf_link {
3257 struct bpf_link link;
3258 struct file *perf_file;
3261 static void bpf_perf_link_release(struct bpf_link *link)
3263 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3264 struct perf_event *event = perf_link->perf_file->private_data;
3266 perf_event_free_bpf_prog(event);
3267 fput(perf_link->perf_file);
3270 static void bpf_perf_link_dealloc(struct bpf_link *link)
3272 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3277 static const struct bpf_link_ops bpf_perf_link_lops = {
3278 .release = bpf_perf_link_release,
3279 .dealloc = bpf_perf_link_dealloc,
3282 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3284 struct bpf_link_primer link_primer;
3285 struct bpf_perf_link *link;
3286 struct perf_event *event;
3287 struct file *perf_file;
3290 if (attr->link_create.flags)
3293 perf_file = perf_event_get(attr->link_create.target_fd);
3294 if (IS_ERR(perf_file))
3295 return PTR_ERR(perf_file);
3297 link = kzalloc(sizeof(*link), GFP_USER);
3302 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog);
3303 link->perf_file = perf_file;
3305 err = bpf_link_prime(&link->link, &link_primer);
3311 event = perf_file->private_data;
3312 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
3314 bpf_link_cleanup(&link_primer);
3317 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
3320 return bpf_link_settle(&link_primer);
3327 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3331 #endif /* CONFIG_PERF_EVENTS */
3333 static int bpf_raw_tp_link_attach(struct bpf_prog *prog,
3334 const char __user *user_tp_name)
3336 struct bpf_link_primer link_primer;
3337 struct bpf_raw_tp_link *link;
3338 struct bpf_raw_event_map *btp;
3339 const char *tp_name;
3343 switch (prog->type) {
3344 case BPF_PROG_TYPE_TRACING:
3345 case BPF_PROG_TYPE_EXT:
3346 case BPF_PROG_TYPE_LSM:
3348 /* The attach point for this category of programs
3349 * should be specified via btf_id during program load.
3352 if (prog->type == BPF_PROG_TYPE_TRACING &&
3353 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
3354 tp_name = prog->aux->attach_func_name;
3357 return bpf_tracing_prog_attach(prog, 0, 0, 0);
3358 case BPF_PROG_TYPE_RAW_TRACEPOINT:
3359 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
3360 if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0)
3362 buf[sizeof(buf) - 1] = 0;
3369 btp = bpf_get_raw_tracepoint(tp_name);
3373 link = kzalloc(sizeof(*link), GFP_USER);
3378 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
3379 &bpf_raw_tp_link_lops, prog);
3382 err = bpf_link_prime(&link->link, &link_primer);
3388 err = bpf_probe_register(link->btp, prog);
3390 bpf_link_cleanup(&link_primer);
3394 return bpf_link_settle(&link_primer);
3397 bpf_put_raw_tracepoint(btp);
3401 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
3403 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
3405 struct bpf_prog *prog;
3408 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
3411 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
3413 return PTR_ERR(prog);
3415 fd = bpf_raw_tp_link_attach(prog, u64_to_user_ptr(attr->raw_tracepoint.name));
3421 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
3422 enum bpf_attach_type attach_type)
3424 switch (prog->type) {
3425 case BPF_PROG_TYPE_CGROUP_SOCK:
3426 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3427 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3428 case BPF_PROG_TYPE_SK_LOOKUP:
3429 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
3430 case BPF_PROG_TYPE_CGROUP_SKB:
3431 if (!capable(CAP_NET_ADMIN))
3432 /* cg-skb progs can be loaded by unpriv user.
3433 * check permissions at attach time.
3436 return prog->enforce_expected_attach_type &&
3437 prog->expected_attach_type != attach_type ?
3444 static enum bpf_prog_type
3445 attach_type_to_prog_type(enum bpf_attach_type attach_type)
3447 switch (attach_type) {
3448 case BPF_CGROUP_INET_INGRESS:
3449 case BPF_CGROUP_INET_EGRESS:
3450 return BPF_PROG_TYPE_CGROUP_SKB;
3451 case BPF_CGROUP_INET_SOCK_CREATE:
3452 case BPF_CGROUP_INET_SOCK_RELEASE:
3453 case BPF_CGROUP_INET4_POST_BIND:
3454 case BPF_CGROUP_INET6_POST_BIND:
3455 return BPF_PROG_TYPE_CGROUP_SOCK;
3456 case BPF_CGROUP_INET4_BIND:
3457 case BPF_CGROUP_INET6_BIND:
3458 case BPF_CGROUP_INET4_CONNECT:
3459 case BPF_CGROUP_INET6_CONNECT:
3460 case BPF_CGROUP_INET4_GETPEERNAME:
3461 case BPF_CGROUP_INET6_GETPEERNAME:
3462 case BPF_CGROUP_INET4_GETSOCKNAME:
3463 case BPF_CGROUP_INET6_GETSOCKNAME:
3464 case BPF_CGROUP_UDP4_SENDMSG:
3465 case BPF_CGROUP_UDP6_SENDMSG:
3466 case BPF_CGROUP_UDP4_RECVMSG:
3467 case BPF_CGROUP_UDP6_RECVMSG:
3468 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3469 case BPF_CGROUP_SOCK_OPS:
3470 return BPF_PROG_TYPE_SOCK_OPS;
3471 case BPF_CGROUP_DEVICE:
3472 return BPF_PROG_TYPE_CGROUP_DEVICE;
3473 case BPF_SK_MSG_VERDICT:
3474 return BPF_PROG_TYPE_SK_MSG;
3475 case BPF_SK_SKB_STREAM_PARSER:
3476 case BPF_SK_SKB_STREAM_VERDICT:
3477 case BPF_SK_SKB_VERDICT:
3478 return BPF_PROG_TYPE_SK_SKB;
3479 case BPF_LIRC_MODE2:
3480 return BPF_PROG_TYPE_LIRC_MODE2;
3481 case BPF_FLOW_DISSECTOR:
3482 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3483 case BPF_CGROUP_SYSCTL:
3484 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3485 case BPF_CGROUP_GETSOCKOPT:
3486 case BPF_CGROUP_SETSOCKOPT:
3487 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
3488 case BPF_TRACE_ITER:
3489 case BPF_TRACE_RAW_TP:
3490 case BPF_TRACE_FENTRY:
3491 case BPF_TRACE_FEXIT:
3492 case BPF_MODIFY_RETURN:
3493 return BPF_PROG_TYPE_TRACING;
3495 return BPF_PROG_TYPE_LSM;
3497 return BPF_PROG_TYPE_SK_LOOKUP;
3499 return BPF_PROG_TYPE_XDP;
3500 case BPF_LSM_CGROUP:
3501 return BPF_PROG_TYPE_LSM;
3503 return BPF_PROG_TYPE_UNSPEC;
3507 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
3509 #define BPF_F_ATTACH_MASK \
3510 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
3512 static int bpf_prog_attach(const union bpf_attr *attr)
3514 enum bpf_prog_type ptype;
3515 struct bpf_prog *prog;
3518 if (CHECK_ATTR(BPF_PROG_ATTACH))
3521 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
3524 ptype = attach_type_to_prog_type(attr->attach_type);
3525 if (ptype == BPF_PROG_TYPE_UNSPEC)
3528 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3530 return PTR_ERR(prog);
3532 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3538 case BPF_PROG_TYPE_SK_SKB:
3539 case BPF_PROG_TYPE_SK_MSG:
3540 ret = sock_map_get_from_fd(attr, prog);
3542 case BPF_PROG_TYPE_LIRC_MODE2:
3543 ret = lirc_prog_attach(attr, prog);
3545 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3546 ret = netns_bpf_prog_attach(attr, prog);
3548 case BPF_PROG_TYPE_CGROUP_DEVICE:
3549 case BPF_PROG_TYPE_CGROUP_SKB:
3550 case BPF_PROG_TYPE_CGROUP_SOCK:
3551 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3552 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3553 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3554 case BPF_PROG_TYPE_SOCK_OPS:
3555 case BPF_PROG_TYPE_LSM:
3556 if (ptype == BPF_PROG_TYPE_LSM &&
3557 prog->expected_attach_type != BPF_LSM_CGROUP)
3560 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3571 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3573 static int bpf_prog_detach(const union bpf_attr *attr)
3575 enum bpf_prog_type ptype;
3577 if (CHECK_ATTR(BPF_PROG_DETACH))
3580 ptype = attach_type_to_prog_type(attr->attach_type);
3583 case BPF_PROG_TYPE_SK_MSG:
3584 case BPF_PROG_TYPE_SK_SKB:
3585 return sock_map_prog_detach(attr, ptype);
3586 case BPF_PROG_TYPE_LIRC_MODE2:
3587 return lirc_prog_detach(attr);
3588 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3589 return netns_bpf_prog_detach(attr, ptype);
3590 case BPF_PROG_TYPE_CGROUP_DEVICE:
3591 case BPF_PROG_TYPE_CGROUP_SKB:
3592 case BPF_PROG_TYPE_CGROUP_SOCK:
3593 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3594 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3595 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3596 case BPF_PROG_TYPE_SOCK_OPS:
3597 case BPF_PROG_TYPE_LSM:
3598 return cgroup_bpf_prog_detach(attr, ptype);
3604 #define BPF_PROG_QUERY_LAST_FIELD query.prog_attach_flags
3606 static int bpf_prog_query(const union bpf_attr *attr,
3607 union bpf_attr __user *uattr)
3609 if (!capable(CAP_NET_ADMIN))
3611 if (CHECK_ATTR(BPF_PROG_QUERY))
3613 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3616 switch (attr->query.attach_type) {
3617 case BPF_CGROUP_INET_INGRESS:
3618 case BPF_CGROUP_INET_EGRESS:
3619 case BPF_CGROUP_INET_SOCK_CREATE:
3620 case BPF_CGROUP_INET_SOCK_RELEASE:
3621 case BPF_CGROUP_INET4_BIND:
3622 case BPF_CGROUP_INET6_BIND:
3623 case BPF_CGROUP_INET4_POST_BIND:
3624 case BPF_CGROUP_INET6_POST_BIND:
3625 case BPF_CGROUP_INET4_CONNECT:
3626 case BPF_CGROUP_INET6_CONNECT:
3627 case BPF_CGROUP_INET4_GETPEERNAME:
3628 case BPF_CGROUP_INET6_GETPEERNAME:
3629 case BPF_CGROUP_INET4_GETSOCKNAME:
3630 case BPF_CGROUP_INET6_GETSOCKNAME:
3631 case BPF_CGROUP_UDP4_SENDMSG:
3632 case BPF_CGROUP_UDP6_SENDMSG:
3633 case BPF_CGROUP_UDP4_RECVMSG:
3634 case BPF_CGROUP_UDP6_RECVMSG:
3635 case BPF_CGROUP_SOCK_OPS:
3636 case BPF_CGROUP_DEVICE:
3637 case BPF_CGROUP_SYSCTL:
3638 case BPF_CGROUP_GETSOCKOPT:
3639 case BPF_CGROUP_SETSOCKOPT:
3640 case BPF_LSM_CGROUP:
3641 return cgroup_bpf_prog_query(attr, uattr);
3642 case BPF_LIRC_MODE2:
3643 return lirc_prog_query(attr, uattr);
3644 case BPF_FLOW_DISSECTOR:
3646 return netns_bpf_prog_query(attr, uattr);
3647 case BPF_SK_SKB_STREAM_PARSER:
3648 case BPF_SK_SKB_STREAM_VERDICT:
3649 case BPF_SK_MSG_VERDICT:
3650 case BPF_SK_SKB_VERDICT:
3651 return sock_map_bpf_prog_query(attr, uattr);
3657 #define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size
3659 static int bpf_prog_test_run(const union bpf_attr *attr,
3660 union bpf_attr __user *uattr)
3662 struct bpf_prog *prog;
3663 int ret = -ENOTSUPP;
3665 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3668 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3669 (!attr->test.ctx_size_in && attr->test.ctx_in))
3672 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3673 (!attr->test.ctx_size_out && attr->test.ctx_out))
3676 prog = bpf_prog_get(attr->test.prog_fd);
3678 return PTR_ERR(prog);
3680 if (prog->aux->ops->test_run)
3681 ret = prog->aux->ops->test_run(prog, attr, uattr);
3687 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3689 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3690 union bpf_attr __user *uattr,
3694 u32 next_id = attr->start_id;
3697 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3700 if (!capable(CAP_SYS_ADMIN))
3705 if (!idr_get_next(idr, &next_id))
3707 spin_unlock_bh(lock);
3710 err = put_user(next_id, &uattr->next_id);
3715 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3717 struct bpf_map *map;
3719 spin_lock_bh(&map_idr_lock);
3721 map = idr_get_next(&map_idr, id);
3723 map = __bpf_map_inc_not_zero(map, false);
3729 spin_unlock_bh(&map_idr_lock);
3734 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3736 struct bpf_prog *prog;
3738 spin_lock_bh(&prog_idr_lock);
3740 prog = idr_get_next(&prog_idr, id);
3742 prog = bpf_prog_inc_not_zero(prog);
3748 spin_unlock_bh(&prog_idr_lock);
3753 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3755 struct bpf_prog *bpf_prog_by_id(u32 id)
3757 struct bpf_prog *prog;
3760 return ERR_PTR(-ENOENT);
3762 spin_lock_bh(&prog_idr_lock);
3763 prog = idr_find(&prog_idr, id);
3765 prog = bpf_prog_inc_not_zero(prog);
3767 prog = ERR_PTR(-ENOENT);
3768 spin_unlock_bh(&prog_idr_lock);
3772 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3774 struct bpf_prog *prog;
3775 u32 id = attr->prog_id;
3778 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3781 if (!capable(CAP_SYS_ADMIN))
3784 prog = bpf_prog_by_id(id);
3786 return PTR_ERR(prog);
3788 fd = bpf_prog_new_fd(prog);
3795 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3797 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3799 struct bpf_map *map;
3800 u32 id = attr->map_id;
3804 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3805 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3808 if (!capable(CAP_SYS_ADMIN))
3811 f_flags = bpf_get_file_flag(attr->open_flags);
3815 spin_lock_bh(&map_idr_lock);
3816 map = idr_find(&map_idr, id);
3818 map = __bpf_map_inc_not_zero(map, true);
3820 map = ERR_PTR(-ENOENT);
3821 spin_unlock_bh(&map_idr_lock);
3824 return PTR_ERR(map);
3826 fd = bpf_map_new_fd(map, f_flags);
3828 bpf_map_put_with_uref(map);
3833 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3834 unsigned long addr, u32 *off,
3837 const struct bpf_map *map;
3840 mutex_lock(&prog->aux->used_maps_mutex);
3841 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3842 map = prog->aux->used_maps[i];
3843 if (map == (void *)addr) {
3844 *type = BPF_PSEUDO_MAP_FD;
3847 if (!map->ops->map_direct_value_meta)
3849 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3850 *type = BPF_PSEUDO_MAP_VALUE;
3857 mutex_unlock(&prog->aux->used_maps_mutex);
3861 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3862 const struct cred *f_cred)
3864 const struct bpf_map *map;
3865 struct bpf_insn *insns;
3871 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3876 for (i = 0; i < prog->len; i++) {
3877 code = insns[i].code;
3879 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3880 insns[i].code = BPF_JMP | BPF_CALL;
3881 insns[i].imm = BPF_FUNC_tail_call;
3884 if (code == (BPF_JMP | BPF_CALL) ||
3885 code == (BPF_JMP | BPF_CALL_ARGS)) {
3886 if (code == (BPF_JMP | BPF_CALL_ARGS))
3887 insns[i].code = BPF_JMP | BPF_CALL;
3888 if (!bpf_dump_raw_ok(f_cred))
3892 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3893 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3897 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3900 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3901 map = bpf_map_from_imm(prog, imm, &off, &type);
3903 insns[i].src_reg = type;
3904 insns[i].imm = map->id;
3905 insns[i + 1].imm = off;
3913 static int set_info_rec_size(struct bpf_prog_info *info)
3916 * Ensure info.*_rec_size is the same as kernel expected size
3920 * Only allow zero *_rec_size if both _rec_size and _cnt are
3921 * zero. In this case, the kernel will set the expected
3922 * _rec_size back to the info.
3925 if ((info->nr_func_info || info->func_info_rec_size) &&
3926 info->func_info_rec_size != sizeof(struct bpf_func_info))
3929 if ((info->nr_line_info || info->line_info_rec_size) &&
3930 info->line_info_rec_size != sizeof(struct bpf_line_info))
3933 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3934 info->jited_line_info_rec_size != sizeof(__u64))
3937 info->func_info_rec_size = sizeof(struct bpf_func_info);
3938 info->line_info_rec_size = sizeof(struct bpf_line_info);
3939 info->jited_line_info_rec_size = sizeof(__u64);
3944 static int bpf_prog_get_info_by_fd(struct file *file,
3945 struct bpf_prog *prog,
3946 const union bpf_attr *attr,
3947 union bpf_attr __user *uattr)
3949 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3950 struct btf *attach_btf = bpf_prog_get_target_btf(prog);
3951 struct bpf_prog_info info;
3952 u32 info_len = attr->info.info_len;
3953 struct bpf_prog_kstats stats;
3954 char __user *uinsns;
3958 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3961 info_len = min_t(u32, sizeof(info), info_len);
3963 memset(&info, 0, sizeof(info));
3964 if (copy_from_user(&info, uinfo, info_len))
3967 info.type = prog->type;
3968 info.id = prog->aux->id;
3969 info.load_time = prog->aux->load_time;
3970 info.created_by_uid = from_kuid_munged(current_user_ns(),
3971 prog->aux->user->uid);
3972 info.gpl_compatible = prog->gpl_compatible;
3974 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3975 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3977 mutex_lock(&prog->aux->used_maps_mutex);
3978 ulen = info.nr_map_ids;
3979 info.nr_map_ids = prog->aux->used_map_cnt;
3980 ulen = min_t(u32, info.nr_map_ids, ulen);
3982 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3985 for (i = 0; i < ulen; i++)
3986 if (put_user(prog->aux->used_maps[i]->id,
3987 &user_map_ids[i])) {
3988 mutex_unlock(&prog->aux->used_maps_mutex);
3992 mutex_unlock(&prog->aux->used_maps_mutex);
3994 err = set_info_rec_size(&info);
3998 bpf_prog_get_stats(prog, &stats);
3999 info.run_time_ns = stats.nsecs;
4000 info.run_cnt = stats.cnt;
4001 info.recursion_misses = stats.misses;
4003 info.verified_insns = prog->aux->verified_insns;
4005 if (!bpf_capable()) {
4006 info.jited_prog_len = 0;
4007 info.xlated_prog_len = 0;
4008 info.nr_jited_ksyms = 0;
4009 info.nr_jited_func_lens = 0;
4010 info.nr_func_info = 0;
4011 info.nr_line_info = 0;
4012 info.nr_jited_line_info = 0;
4016 ulen = info.xlated_prog_len;
4017 info.xlated_prog_len = bpf_prog_insn_size(prog);
4018 if (info.xlated_prog_len && ulen) {
4019 struct bpf_insn *insns_sanitized;
4022 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
4023 info.xlated_prog_insns = 0;
4026 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
4027 if (!insns_sanitized)
4029 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
4030 ulen = min_t(u32, info.xlated_prog_len, ulen);
4031 fault = copy_to_user(uinsns, insns_sanitized, ulen);
4032 kfree(insns_sanitized);
4037 if (bpf_prog_is_offloaded(prog->aux)) {
4038 err = bpf_prog_offload_info_fill(&info, prog);
4044 /* NOTE: the following code is supposed to be skipped for offload.
4045 * bpf_prog_offload_info_fill() is the place to fill similar fields
4048 ulen = info.jited_prog_len;
4049 if (prog->aux->func_cnt) {
4052 info.jited_prog_len = 0;
4053 for (i = 0; i < prog->aux->func_cnt; i++)
4054 info.jited_prog_len += prog->aux->func[i]->jited_len;
4056 info.jited_prog_len = prog->jited_len;
4059 if (info.jited_prog_len && ulen) {
4060 if (bpf_dump_raw_ok(file->f_cred)) {
4061 uinsns = u64_to_user_ptr(info.jited_prog_insns);
4062 ulen = min_t(u32, info.jited_prog_len, ulen);
4064 /* for multi-function programs, copy the JITed
4065 * instructions for all the functions
4067 if (prog->aux->func_cnt) {
4072 for (i = 0; i < prog->aux->func_cnt; i++) {
4073 len = prog->aux->func[i]->jited_len;
4074 len = min_t(u32, len, free);
4075 img = (u8 *) prog->aux->func[i]->bpf_func;
4076 if (copy_to_user(uinsns, img, len))
4084 if (copy_to_user(uinsns, prog->bpf_func, ulen))
4088 info.jited_prog_insns = 0;
4092 ulen = info.nr_jited_ksyms;
4093 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
4095 if (bpf_dump_raw_ok(file->f_cred)) {
4096 unsigned long ksym_addr;
4097 u64 __user *user_ksyms;
4100 /* copy the address of the kernel symbol
4101 * corresponding to each function
4103 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
4104 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
4105 if (prog->aux->func_cnt) {
4106 for (i = 0; i < ulen; i++) {
4107 ksym_addr = (unsigned long)
4108 prog->aux->func[i]->bpf_func;
4109 if (put_user((u64) ksym_addr,
4114 ksym_addr = (unsigned long) prog->bpf_func;
4115 if (put_user((u64) ksym_addr, &user_ksyms[0]))
4119 info.jited_ksyms = 0;
4123 ulen = info.nr_jited_func_lens;
4124 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
4126 if (bpf_dump_raw_ok(file->f_cred)) {
4127 u32 __user *user_lens;
4130 /* copy the JITed image lengths for each function */
4131 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
4132 user_lens = u64_to_user_ptr(info.jited_func_lens);
4133 if (prog->aux->func_cnt) {
4134 for (i = 0; i < ulen; i++) {
4136 prog->aux->func[i]->jited_len;
4137 if (put_user(func_len, &user_lens[i]))
4141 func_len = prog->jited_len;
4142 if (put_user(func_len, &user_lens[0]))
4146 info.jited_func_lens = 0;
4151 info.btf_id = btf_obj_id(prog->aux->btf);
4152 info.attach_btf_id = prog->aux->attach_btf_id;
4154 info.attach_btf_obj_id = btf_obj_id(attach_btf);
4156 ulen = info.nr_func_info;
4157 info.nr_func_info = prog->aux->func_info_cnt;
4158 if (info.nr_func_info && ulen) {
4159 char __user *user_finfo;
4161 user_finfo = u64_to_user_ptr(info.func_info);
4162 ulen = min_t(u32, info.nr_func_info, ulen);
4163 if (copy_to_user(user_finfo, prog->aux->func_info,
4164 info.func_info_rec_size * ulen))
4168 ulen = info.nr_line_info;
4169 info.nr_line_info = prog->aux->nr_linfo;
4170 if (info.nr_line_info && ulen) {
4171 __u8 __user *user_linfo;
4173 user_linfo = u64_to_user_ptr(info.line_info);
4174 ulen = min_t(u32, info.nr_line_info, ulen);
4175 if (copy_to_user(user_linfo, prog->aux->linfo,
4176 info.line_info_rec_size * ulen))
4180 ulen = info.nr_jited_line_info;
4181 if (prog->aux->jited_linfo)
4182 info.nr_jited_line_info = prog->aux->nr_linfo;
4184 info.nr_jited_line_info = 0;
4185 if (info.nr_jited_line_info && ulen) {
4186 if (bpf_dump_raw_ok(file->f_cred)) {
4187 unsigned long line_addr;
4188 __u64 __user *user_linfo;
4191 user_linfo = u64_to_user_ptr(info.jited_line_info);
4192 ulen = min_t(u32, info.nr_jited_line_info, ulen);
4193 for (i = 0; i < ulen; i++) {
4194 line_addr = (unsigned long)prog->aux->jited_linfo[i];
4195 if (put_user((__u64)line_addr, &user_linfo[i]))
4199 info.jited_line_info = 0;
4203 ulen = info.nr_prog_tags;
4204 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
4206 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
4209 user_prog_tags = u64_to_user_ptr(info.prog_tags);
4210 ulen = min_t(u32, info.nr_prog_tags, ulen);
4211 if (prog->aux->func_cnt) {
4212 for (i = 0; i < ulen; i++) {
4213 if (copy_to_user(user_prog_tags[i],
4214 prog->aux->func[i]->tag,
4219 if (copy_to_user(user_prog_tags[0],
4220 prog->tag, BPF_TAG_SIZE))
4226 if (copy_to_user(uinfo, &info, info_len) ||
4227 put_user(info_len, &uattr->info.info_len))
4233 static int bpf_map_get_info_by_fd(struct file *file,
4234 struct bpf_map *map,
4235 const union bpf_attr *attr,
4236 union bpf_attr __user *uattr)
4238 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4239 struct bpf_map_info info;
4240 u32 info_len = attr->info.info_len;
4243 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
4246 info_len = min_t(u32, sizeof(info), info_len);
4248 memset(&info, 0, sizeof(info));
4249 info.type = map->map_type;
4251 info.key_size = map->key_size;
4252 info.value_size = map->value_size;
4253 info.max_entries = map->max_entries;
4254 info.map_flags = map->map_flags;
4255 info.map_extra = map->map_extra;
4256 memcpy(info.name, map->name, sizeof(map->name));
4259 info.btf_id = btf_obj_id(map->btf);
4260 info.btf_key_type_id = map->btf_key_type_id;
4261 info.btf_value_type_id = map->btf_value_type_id;
4263 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
4265 if (bpf_map_is_offloaded(map)) {
4266 err = bpf_map_offload_info_fill(&info, map);
4271 if (copy_to_user(uinfo, &info, info_len) ||
4272 put_user(info_len, &uattr->info.info_len))
4278 static int bpf_btf_get_info_by_fd(struct file *file,
4280 const union bpf_attr *attr,
4281 union bpf_attr __user *uattr)
4283 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4284 u32 info_len = attr->info.info_len;
4287 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
4291 return btf_get_info_by_fd(btf, attr, uattr);
4294 static int bpf_link_get_info_by_fd(struct file *file,
4295 struct bpf_link *link,
4296 const union bpf_attr *attr,
4297 union bpf_attr __user *uattr)
4299 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4300 struct bpf_link_info info;
4301 u32 info_len = attr->info.info_len;
4304 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
4307 info_len = min_t(u32, sizeof(info), info_len);
4309 memset(&info, 0, sizeof(info));
4310 if (copy_from_user(&info, uinfo, info_len))
4313 info.type = link->type;
4316 info.prog_id = link->prog->aux->id;
4318 if (link->ops->fill_link_info) {
4319 err = link->ops->fill_link_info(link, &info);
4324 if (copy_to_user(uinfo, &info, info_len) ||
4325 put_user(info_len, &uattr->info.info_len))
4332 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
4334 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
4335 union bpf_attr __user *uattr)
4337 int ufd = attr->info.bpf_fd;
4341 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
4348 if (f.file->f_op == &bpf_prog_fops)
4349 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
4351 else if (f.file->f_op == &bpf_map_fops)
4352 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
4354 else if (f.file->f_op == &btf_fops)
4355 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
4356 else if (f.file->f_op == &bpf_link_fops)
4357 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
4366 #define BPF_BTF_LOAD_LAST_FIELD btf_log_true_size
4368 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size)
4370 if (CHECK_ATTR(BPF_BTF_LOAD))
4376 return btf_new_fd(attr, uattr, uattr_size);
4379 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
4381 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
4383 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
4386 if (!capable(CAP_SYS_ADMIN))
4389 return btf_get_fd_by_id(attr->btf_id);
4392 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
4393 union bpf_attr __user *uattr,
4394 u32 prog_id, u32 fd_type,
4395 const char *buf, u64 probe_offset,
4398 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
4399 u32 len = buf ? strlen(buf) : 0, input_len;
4402 if (put_user(len, &uattr->task_fd_query.buf_len))
4404 input_len = attr->task_fd_query.buf_len;
4405 if (input_len && ubuf) {
4407 /* nothing to copy, just make ubuf NULL terminated */
4410 if (put_user(zero, ubuf))
4412 } else if (input_len >= len + 1) {
4413 /* ubuf can hold the string with NULL terminator */
4414 if (copy_to_user(ubuf, buf, len + 1))
4417 /* ubuf cannot hold the string with NULL terminator,
4418 * do a partial copy with NULL terminator.
4423 if (copy_to_user(ubuf, buf, input_len - 1))
4425 if (put_user(zero, ubuf + input_len - 1))
4430 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
4431 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
4432 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
4433 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
4439 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
4441 static int bpf_task_fd_query(const union bpf_attr *attr,
4442 union bpf_attr __user *uattr)
4444 pid_t pid = attr->task_fd_query.pid;
4445 u32 fd = attr->task_fd_query.fd;
4446 const struct perf_event *event;
4447 struct task_struct *task;
4451 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
4454 if (!capable(CAP_SYS_ADMIN))
4457 if (attr->task_fd_query.flags != 0)
4461 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
4467 file = fget_task(task, fd);
4468 put_task_struct(task);
4472 if (file->f_op == &bpf_link_fops) {
4473 struct bpf_link *link = file->private_data;
4475 if (link->ops == &bpf_raw_tp_link_lops) {
4476 struct bpf_raw_tp_link *raw_tp =
4477 container_of(link, struct bpf_raw_tp_link, link);
4478 struct bpf_raw_event_map *btp = raw_tp->btp;
4480 err = bpf_task_fd_query_copy(attr, uattr,
4481 raw_tp->link.prog->aux->id,
4482 BPF_FD_TYPE_RAW_TRACEPOINT,
4483 btp->tp->name, 0, 0);
4489 event = perf_get_event(file);
4490 if (!IS_ERR(event)) {
4491 u64 probe_offset, probe_addr;
4492 u32 prog_id, fd_type;
4495 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4496 &buf, &probe_offset,
4499 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4513 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
4515 #define BPF_DO_BATCH(fn, ...) \
4521 err = fn(__VA_ARGS__); \
4524 static int bpf_map_do_batch(const union bpf_attr *attr,
4525 union bpf_attr __user *uattr,
4528 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH ||
4529 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
4530 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
4531 struct bpf_map *map;
4535 if (CHECK_ATTR(BPF_MAP_BATCH))
4538 ufd = attr->batch.map_fd;
4540 map = __bpf_map_get(f);
4542 return PTR_ERR(map);
4544 bpf_map_write_active_inc(map);
4545 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4549 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4554 if (cmd == BPF_MAP_LOOKUP_BATCH)
4555 BPF_DO_BATCH(map->ops->map_lookup_batch, map, attr, uattr);
4556 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4557 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch, map, attr, uattr);
4558 else if (cmd == BPF_MAP_UPDATE_BATCH)
4559 BPF_DO_BATCH(map->ops->map_update_batch, map, f.file, attr, uattr);
4561 BPF_DO_BATCH(map->ops->map_delete_batch, map, attr, uattr);
4564 bpf_map_write_active_dec(map);
4569 #define BPF_LINK_CREATE_LAST_FIELD link_create.kprobe_multi.cookies
4570 static int link_create(union bpf_attr *attr, bpfptr_t uattr)
4572 enum bpf_prog_type ptype;
4573 struct bpf_prog *prog;
4576 if (CHECK_ATTR(BPF_LINK_CREATE))
4579 if (attr->link_create.attach_type == BPF_STRUCT_OPS)
4580 return bpf_struct_ops_link_create(attr);
4582 prog = bpf_prog_get(attr->link_create.prog_fd);
4584 return PTR_ERR(prog);
4586 ret = bpf_prog_attach_check_attach_type(prog,
4587 attr->link_create.attach_type);
4591 switch (prog->type) {
4592 case BPF_PROG_TYPE_EXT:
4593 case BPF_PROG_TYPE_NETFILTER:
4595 case BPF_PROG_TYPE_PERF_EVENT:
4596 case BPF_PROG_TYPE_TRACEPOINT:
4597 if (attr->link_create.attach_type != BPF_PERF_EVENT) {
4602 case BPF_PROG_TYPE_KPROBE:
4603 if (attr->link_create.attach_type != BPF_PERF_EVENT &&
4604 attr->link_create.attach_type != BPF_TRACE_KPROBE_MULTI) {
4610 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4611 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4618 switch (prog->type) {
4619 case BPF_PROG_TYPE_CGROUP_SKB:
4620 case BPF_PROG_TYPE_CGROUP_SOCK:
4621 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4622 case BPF_PROG_TYPE_SOCK_OPS:
4623 case BPF_PROG_TYPE_CGROUP_DEVICE:
4624 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4625 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4626 ret = cgroup_bpf_link_attach(attr, prog);
4628 case BPF_PROG_TYPE_EXT:
4629 ret = bpf_tracing_prog_attach(prog,
4630 attr->link_create.target_fd,
4631 attr->link_create.target_btf_id,
4632 attr->link_create.tracing.cookie);
4634 case BPF_PROG_TYPE_LSM:
4635 case BPF_PROG_TYPE_TRACING:
4636 if (attr->link_create.attach_type != prog->expected_attach_type) {
4640 if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
4641 ret = bpf_raw_tp_link_attach(prog, NULL);
4642 else if (prog->expected_attach_type == BPF_TRACE_ITER)
4643 ret = bpf_iter_link_attach(attr, uattr, prog);
4644 else if (prog->expected_attach_type == BPF_LSM_CGROUP)
4645 ret = cgroup_bpf_link_attach(attr, prog);
4647 ret = bpf_tracing_prog_attach(prog,
4648 attr->link_create.target_fd,
4649 attr->link_create.target_btf_id,
4650 attr->link_create.tracing.cookie);
4652 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4653 case BPF_PROG_TYPE_SK_LOOKUP:
4654 ret = netns_bpf_link_create(attr, prog);
4657 case BPF_PROG_TYPE_XDP:
4658 ret = bpf_xdp_link_attach(attr, prog);
4660 case BPF_PROG_TYPE_NETFILTER:
4661 ret = bpf_nf_link_attach(attr, prog);
4664 case BPF_PROG_TYPE_PERF_EVENT:
4665 case BPF_PROG_TYPE_TRACEPOINT:
4666 ret = bpf_perf_link_attach(attr, prog);
4668 case BPF_PROG_TYPE_KPROBE:
4669 if (attr->link_create.attach_type == BPF_PERF_EVENT)
4670 ret = bpf_perf_link_attach(attr, prog);
4672 ret = bpf_kprobe_multi_link_attach(attr, prog);
4684 static int link_update_map(struct bpf_link *link, union bpf_attr *attr)
4686 struct bpf_map *new_map, *old_map = NULL;
4689 new_map = bpf_map_get(attr->link_update.new_map_fd);
4690 if (IS_ERR(new_map))
4691 return PTR_ERR(new_map);
4693 if (attr->link_update.flags & BPF_F_REPLACE) {
4694 old_map = bpf_map_get(attr->link_update.old_map_fd);
4695 if (IS_ERR(old_map)) {
4696 ret = PTR_ERR(old_map);
4699 } else if (attr->link_update.old_map_fd) {
4704 ret = link->ops->update_map(link, new_map, old_map);
4707 bpf_map_put(old_map);
4709 bpf_map_put(new_map);
4713 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4715 static int link_update(union bpf_attr *attr)
4717 struct bpf_prog *old_prog = NULL, *new_prog;
4718 struct bpf_link *link;
4722 if (CHECK_ATTR(BPF_LINK_UPDATE))
4725 flags = attr->link_update.flags;
4726 if (flags & ~BPF_F_REPLACE)
4729 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4731 return PTR_ERR(link);
4733 if (link->ops->update_map) {
4734 ret = link_update_map(link, attr);
4738 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4739 if (IS_ERR(new_prog)) {
4740 ret = PTR_ERR(new_prog);
4744 if (flags & BPF_F_REPLACE) {
4745 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4746 if (IS_ERR(old_prog)) {
4747 ret = PTR_ERR(old_prog);
4751 } else if (attr->link_update.old_prog_fd) {
4756 if (link->ops->update_prog)
4757 ret = link->ops->update_prog(link, new_prog, old_prog);
4763 bpf_prog_put(old_prog);
4765 bpf_prog_put(new_prog);
4771 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4773 static int link_detach(union bpf_attr *attr)
4775 struct bpf_link *link;
4778 if (CHECK_ATTR(BPF_LINK_DETACH))
4781 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4783 return PTR_ERR(link);
4785 if (link->ops->detach)
4786 ret = link->ops->detach(link);
4794 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4796 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4799 struct bpf_link *bpf_link_by_id(u32 id)
4801 struct bpf_link *link;
4804 return ERR_PTR(-ENOENT);
4806 spin_lock_bh(&link_idr_lock);
4807 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4808 link = idr_find(&link_idr, id);
4811 link = bpf_link_inc_not_zero(link);
4813 link = ERR_PTR(-EAGAIN);
4815 link = ERR_PTR(-ENOENT);
4817 spin_unlock_bh(&link_idr_lock);
4821 struct bpf_link *bpf_link_get_curr_or_next(u32 *id)
4823 struct bpf_link *link;
4825 spin_lock_bh(&link_idr_lock);
4827 link = idr_get_next(&link_idr, id);
4829 link = bpf_link_inc_not_zero(link);
4835 spin_unlock_bh(&link_idr_lock);
4840 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4842 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4844 struct bpf_link *link;
4845 u32 id = attr->link_id;
4848 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4851 if (!capable(CAP_SYS_ADMIN))
4854 link = bpf_link_by_id(id);
4856 return PTR_ERR(link);
4858 fd = bpf_link_new_fd(link);
4865 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4867 static int bpf_stats_release(struct inode *inode, struct file *file)
4869 mutex_lock(&bpf_stats_enabled_mutex);
4870 static_key_slow_dec(&bpf_stats_enabled_key.key);
4871 mutex_unlock(&bpf_stats_enabled_mutex);
4875 static const struct file_operations bpf_stats_fops = {
4876 .release = bpf_stats_release,
4879 static int bpf_enable_runtime_stats(void)
4883 mutex_lock(&bpf_stats_enabled_mutex);
4885 /* Set a very high limit to avoid overflow */
4886 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4887 mutex_unlock(&bpf_stats_enabled_mutex);
4891 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4893 static_key_slow_inc(&bpf_stats_enabled_key.key);
4895 mutex_unlock(&bpf_stats_enabled_mutex);
4899 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4901 static int bpf_enable_stats(union bpf_attr *attr)
4904 if (CHECK_ATTR(BPF_ENABLE_STATS))
4907 if (!capable(CAP_SYS_ADMIN))
4910 switch (attr->enable_stats.type) {
4911 case BPF_STATS_RUN_TIME:
4912 return bpf_enable_runtime_stats();
4919 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4921 static int bpf_iter_create(union bpf_attr *attr)
4923 struct bpf_link *link;
4926 if (CHECK_ATTR(BPF_ITER_CREATE))
4929 if (attr->iter_create.flags)
4932 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4934 return PTR_ERR(link);
4936 err = bpf_iter_new_fd(link);
4942 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4944 static int bpf_prog_bind_map(union bpf_attr *attr)
4946 struct bpf_prog *prog;
4947 struct bpf_map *map;
4948 struct bpf_map **used_maps_old, **used_maps_new;
4951 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4954 if (attr->prog_bind_map.flags)
4957 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4959 return PTR_ERR(prog);
4961 map = bpf_map_get(attr->prog_bind_map.map_fd);
4967 mutex_lock(&prog->aux->used_maps_mutex);
4969 used_maps_old = prog->aux->used_maps;
4971 for (i = 0; i < prog->aux->used_map_cnt; i++)
4972 if (used_maps_old[i] == map) {
4977 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4978 sizeof(used_maps_new[0]),
4980 if (!used_maps_new) {
4985 memcpy(used_maps_new, used_maps_old,
4986 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4987 used_maps_new[prog->aux->used_map_cnt] = map;
4989 prog->aux->used_map_cnt++;
4990 prog->aux->used_maps = used_maps_new;
4992 kfree(used_maps_old);
4995 mutex_unlock(&prog->aux->used_maps_mutex);
5004 static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
5006 union bpf_attr attr;
5010 capable = bpf_capable() || !sysctl_unprivileged_bpf_disabled;
5012 /* Intent here is for unprivileged_bpf_disabled to block key object
5013 * creation commands for unprivileged users; other actions depend
5014 * of fd availability and access to bpffs, so are dependent on
5015 * object creation success. Capabilities are later verified for
5016 * operations such as load and map create, so even with unprivileged
5017 * BPF disabled, capability checks are still carried out for these
5018 * and other operations.
5021 (cmd == BPF_MAP_CREATE || cmd == BPF_PROG_LOAD))
5024 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
5027 size = min_t(u32, size, sizeof(attr));
5029 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
5030 memset(&attr, 0, sizeof(attr));
5031 if (copy_from_bpfptr(&attr, uattr, size) != 0)
5034 err = security_bpf(cmd, &attr, size);
5039 case BPF_MAP_CREATE:
5040 err = map_create(&attr);
5042 case BPF_MAP_LOOKUP_ELEM:
5043 err = map_lookup_elem(&attr);
5045 case BPF_MAP_UPDATE_ELEM:
5046 err = map_update_elem(&attr, uattr);
5048 case BPF_MAP_DELETE_ELEM:
5049 err = map_delete_elem(&attr, uattr);
5051 case BPF_MAP_GET_NEXT_KEY:
5052 err = map_get_next_key(&attr);
5054 case BPF_MAP_FREEZE:
5055 err = map_freeze(&attr);
5058 err = bpf_prog_load(&attr, uattr, size);
5061 err = bpf_obj_pin(&attr);
5064 err = bpf_obj_get(&attr);
5066 case BPF_PROG_ATTACH:
5067 err = bpf_prog_attach(&attr);
5069 case BPF_PROG_DETACH:
5070 err = bpf_prog_detach(&attr);
5072 case BPF_PROG_QUERY:
5073 err = bpf_prog_query(&attr, uattr.user);
5075 case BPF_PROG_TEST_RUN:
5076 err = bpf_prog_test_run(&attr, uattr.user);
5078 case BPF_PROG_GET_NEXT_ID:
5079 err = bpf_obj_get_next_id(&attr, uattr.user,
5080 &prog_idr, &prog_idr_lock);
5082 case BPF_MAP_GET_NEXT_ID:
5083 err = bpf_obj_get_next_id(&attr, uattr.user,
5084 &map_idr, &map_idr_lock);
5086 case BPF_BTF_GET_NEXT_ID:
5087 err = bpf_obj_get_next_id(&attr, uattr.user,
5088 &btf_idr, &btf_idr_lock);
5090 case BPF_PROG_GET_FD_BY_ID:
5091 err = bpf_prog_get_fd_by_id(&attr);
5093 case BPF_MAP_GET_FD_BY_ID:
5094 err = bpf_map_get_fd_by_id(&attr);
5096 case BPF_OBJ_GET_INFO_BY_FD:
5097 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
5099 case BPF_RAW_TRACEPOINT_OPEN:
5100 err = bpf_raw_tracepoint_open(&attr);
5103 err = bpf_btf_load(&attr, uattr, size);
5105 case BPF_BTF_GET_FD_BY_ID:
5106 err = bpf_btf_get_fd_by_id(&attr);
5108 case BPF_TASK_FD_QUERY:
5109 err = bpf_task_fd_query(&attr, uattr.user);
5111 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
5112 err = map_lookup_and_delete_elem(&attr);
5114 case BPF_MAP_LOOKUP_BATCH:
5115 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
5117 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
5118 err = bpf_map_do_batch(&attr, uattr.user,
5119 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
5121 case BPF_MAP_UPDATE_BATCH:
5122 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
5124 case BPF_MAP_DELETE_BATCH:
5125 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
5127 case BPF_LINK_CREATE:
5128 err = link_create(&attr, uattr);
5130 case BPF_LINK_UPDATE:
5131 err = link_update(&attr);
5133 case BPF_LINK_GET_FD_BY_ID:
5134 err = bpf_link_get_fd_by_id(&attr);
5136 case BPF_LINK_GET_NEXT_ID:
5137 err = bpf_obj_get_next_id(&attr, uattr.user,
5138 &link_idr, &link_idr_lock);
5140 case BPF_ENABLE_STATS:
5141 err = bpf_enable_stats(&attr);
5143 case BPF_ITER_CREATE:
5144 err = bpf_iter_create(&attr);
5146 case BPF_LINK_DETACH:
5147 err = link_detach(&attr);
5149 case BPF_PROG_BIND_MAP:
5150 err = bpf_prog_bind_map(&attr);
5160 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
5162 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
5165 static bool syscall_prog_is_valid_access(int off, int size,
5166 enum bpf_access_type type,
5167 const struct bpf_prog *prog,
5168 struct bpf_insn_access_aux *info)
5170 if (off < 0 || off >= U16_MAX)
5172 if (off % size != 0)
5177 BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
5180 case BPF_MAP_CREATE:
5181 case BPF_MAP_DELETE_ELEM:
5182 case BPF_MAP_UPDATE_ELEM:
5183 case BPF_MAP_FREEZE:
5184 case BPF_MAP_GET_FD_BY_ID:
5187 case BPF_LINK_CREATE:
5188 case BPF_RAW_TRACEPOINT_OPEN:
5193 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
5197 /* To shut up -Wmissing-prototypes.
5198 * This function is used by the kernel light skeleton
5199 * to load bpf programs when modules are loaded or during kernel boot.
5200 * See tools/lib/bpf/skel_internal.h
5202 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size);
5204 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5206 struct bpf_prog * __maybe_unused prog;
5207 struct bpf_tramp_run_ctx __maybe_unused run_ctx;
5210 #ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */
5211 case BPF_PROG_TEST_RUN:
5212 if (attr->test.data_in || attr->test.data_out ||
5213 attr->test.ctx_out || attr->test.duration ||
5214 attr->test.repeat || attr->test.flags)
5217 prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL);
5219 return PTR_ERR(prog);
5221 if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
5222 attr->test.ctx_size_in > U16_MAX) {
5227 run_ctx.bpf_cookie = 0;
5228 run_ctx.saved_run_ctx = NULL;
5229 if (!__bpf_prog_enter_sleepable_recur(prog, &run_ctx)) {
5230 /* recursion detected */
5234 attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
5235 __bpf_prog_exit_sleepable_recur(prog, 0 /* bpf_prog_run does runtime stats */,
5241 return ____bpf_sys_bpf(cmd, attr, size);
5244 EXPORT_SYMBOL(kern_sys_bpf);
5246 static const struct bpf_func_proto bpf_sys_bpf_proto = {
5247 .func = bpf_sys_bpf,
5249 .ret_type = RET_INTEGER,
5250 .arg1_type = ARG_ANYTHING,
5251 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
5252 .arg3_type = ARG_CONST_SIZE,
5255 const struct bpf_func_proto * __weak
5256 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5258 return bpf_base_func_proto(func_id);
5261 BPF_CALL_1(bpf_sys_close, u32, fd)
5263 /* When bpf program calls this helper there should not be
5264 * an fdget() without matching completed fdput().
5265 * This helper is allowed in the following callchain only:
5266 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
5268 return close_fd(fd);
5271 static const struct bpf_func_proto bpf_sys_close_proto = {
5272 .func = bpf_sys_close,
5274 .ret_type = RET_INTEGER,
5275 .arg1_type = ARG_ANYTHING,
5278 BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res)
5283 if (name_sz <= 1 || name[name_sz - 1])
5286 if (!bpf_dump_raw_ok(current_cred()))
5289 *res = kallsyms_lookup_name(name);
5290 return *res ? 0 : -ENOENT;
5293 static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = {
5294 .func = bpf_kallsyms_lookup_name,
5296 .ret_type = RET_INTEGER,
5297 .arg1_type = ARG_PTR_TO_MEM,
5298 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
5299 .arg3_type = ARG_ANYTHING,
5300 .arg4_type = ARG_PTR_TO_LONG,
5303 static const struct bpf_func_proto *
5304 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5307 case BPF_FUNC_sys_bpf:
5308 return !perfmon_capable() ? NULL : &bpf_sys_bpf_proto;
5309 case BPF_FUNC_btf_find_by_name_kind:
5310 return &bpf_btf_find_by_name_kind_proto;
5311 case BPF_FUNC_sys_close:
5312 return &bpf_sys_close_proto;
5313 case BPF_FUNC_kallsyms_lookup_name:
5314 return &bpf_kallsyms_lookup_name_proto;
5316 return tracing_prog_func_proto(func_id, prog);
5320 const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
5321 .get_func_proto = syscall_prog_func_proto,
5322 .is_valid_access = syscall_prog_is_valid_access,
5325 const struct bpf_prog_ops bpf_syscall_prog_ops = {
5326 .test_run = bpf_prog_test_run_syscall,
5329 #ifdef CONFIG_SYSCTL
5330 static int bpf_stats_handler(struct ctl_table *table, int write,
5331 void *buffer, size_t *lenp, loff_t *ppos)
5333 struct static_key *key = (struct static_key *)table->data;
5334 static int saved_val;
5336 struct ctl_table tmp = {
5338 .maxlen = sizeof(val),
5339 .mode = table->mode,
5340 .extra1 = SYSCTL_ZERO,
5341 .extra2 = SYSCTL_ONE,
5344 if (write && !capable(CAP_SYS_ADMIN))
5347 mutex_lock(&bpf_stats_enabled_mutex);
5349 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
5350 if (write && !ret && val != saved_val) {
5352 static_key_slow_inc(key);
5354 static_key_slow_dec(key);
5357 mutex_unlock(&bpf_stats_enabled_mutex);
5361 void __weak unpriv_ebpf_notify(int new_state)
5365 static int bpf_unpriv_handler(struct ctl_table *table, int write,
5366 void *buffer, size_t *lenp, loff_t *ppos)
5368 int ret, unpriv_enable = *(int *)table->data;
5369 bool locked_state = unpriv_enable == 1;
5370 struct ctl_table tmp = *table;
5372 if (write && !capable(CAP_SYS_ADMIN))
5375 tmp.data = &unpriv_enable;
5376 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
5377 if (write && !ret) {
5378 if (locked_state && unpriv_enable != 1)
5380 *(int *)table->data = unpriv_enable;
5383 unpriv_ebpf_notify(unpriv_enable);
5388 static struct ctl_table bpf_syscall_table[] = {
5390 .procname = "unprivileged_bpf_disabled",
5391 .data = &sysctl_unprivileged_bpf_disabled,
5392 .maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
5394 .proc_handler = bpf_unpriv_handler,
5395 .extra1 = SYSCTL_ZERO,
5396 .extra2 = SYSCTL_TWO,
5399 .procname = "bpf_stats_enabled",
5400 .data = &bpf_stats_enabled_key.key,
5402 .proc_handler = bpf_stats_handler,
5407 static int __init bpf_syscall_sysctl_init(void)
5409 register_sysctl_init("kernel", bpf_syscall_table);
5412 late_initcall(bpf_syscall_sysctl_init);
5413 #endif /* CONFIG_SYSCTL */