1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
5 #include <linux/bpf_trace.h>
6 #include <linux/bpf_lirc.h>
7 #include <linux/bpf_verifier.h>
9 #include <linux/syscalls.h>
10 #include <linux/slab.h>
11 #include <linux/sched/signal.h>
12 #include <linux/vmalloc.h>
13 #include <linux/mmzone.h>
14 #include <linux/anon_inodes.h>
15 #include <linux/fdtable.h>
16 #include <linux/file.h>
18 #include <linux/license.h>
19 #include <linux/filter.h>
20 #include <linux/kernel.h>
21 #include <linux/idr.h>
22 #include <linux/cred.h>
23 #include <linux/timekeeping.h>
24 #include <linux/ctype.h>
25 #include <linux/nospec.h>
26 #include <linux/audit.h>
27 #include <uapi/linux/btf.h>
28 #include <linux/pgtable.h>
29 #include <linux/bpf_lsm.h>
30 #include <linux/poll.h>
31 #include <linux/bpf-netns.h>
32 #include <linux/rcupdate_trace.h>
33 #include <linux/memcontrol.h>
35 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
36 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
38 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
39 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
40 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
43 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
45 DEFINE_PER_CPU(int, bpf_prog_active);
46 static DEFINE_IDR(prog_idr);
47 static DEFINE_SPINLOCK(prog_idr_lock);
48 static DEFINE_IDR(map_idr);
49 static DEFINE_SPINLOCK(map_idr_lock);
50 static DEFINE_IDR(link_idr);
51 static DEFINE_SPINLOCK(link_idr_lock);
53 int sysctl_unprivileged_bpf_disabled __read_mostly =
54 IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
56 static const struct bpf_map_ops * const bpf_map_types[] = {
57 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
58 #define BPF_MAP_TYPE(_id, _ops) \
60 #define BPF_LINK_TYPE(_id, _name)
61 #include <linux/bpf_types.h>
68 * If we're handed a bigger struct than we know of, ensure all the unknown bits
69 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
70 * we don't know about yet.
72 * There is a ToCToU between this function call and the following
73 * copy_from_user() call. However, this is not a concern since this function is
74 * meant to be a future-proofing of bits.
76 int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
82 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
85 if (actual_size <= expected_size)
89 res = memchr_inv(uaddr.kernel + expected_size, 0,
90 actual_size - expected_size) == NULL;
92 res = check_zeroed_user(uaddr.user + expected_size,
93 actual_size - expected_size);
96 return res ? 0 : -E2BIG;
99 const struct bpf_map_ops bpf_map_offload_ops = {
100 .map_meta_equal = bpf_map_meta_equal,
101 .map_alloc = bpf_map_offload_map_alloc,
102 .map_free = bpf_map_offload_map_free,
103 .map_check_btf = map_check_no_btf,
106 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
108 const struct bpf_map_ops *ops;
109 u32 type = attr->map_type;
113 if (type >= ARRAY_SIZE(bpf_map_types))
114 return ERR_PTR(-EINVAL);
115 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
116 ops = bpf_map_types[type];
118 return ERR_PTR(-EINVAL);
120 if (ops->map_alloc_check) {
121 err = ops->map_alloc_check(attr);
125 if (attr->map_ifindex)
126 ops = &bpf_map_offload_ops;
127 map = ops->map_alloc(attr);
131 map->map_type = type;
135 static u32 bpf_map_value_size(const struct bpf_map *map)
137 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
138 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
139 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
140 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
141 return round_up(map->value_size, 8) * num_possible_cpus();
142 else if (IS_FD_MAP(map))
145 return map->value_size;
148 static void maybe_wait_bpf_programs(struct bpf_map *map)
150 /* Wait for any running BPF programs to complete so that
151 * userspace, when we return to it, knows that all programs
152 * that could be running use the new map value.
154 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
155 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
159 static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
160 void *value, __u64 flags)
164 /* Need to create a kthread, thus must support schedule */
165 if (bpf_map_is_dev_bound(map)) {
166 return bpf_map_offload_update_elem(map, key, value, flags);
167 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
168 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
169 return map->ops->map_update_elem(map, key, value, flags);
170 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
171 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
172 return sock_map_update_elem_sys(map, key, value, flags);
173 } else if (IS_FD_PROG_ARRAY(map)) {
174 return bpf_fd_array_map_update_elem(map, f.file, key, value,
178 bpf_disable_instrumentation();
179 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
180 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
181 err = bpf_percpu_hash_update(map, key, value, flags);
182 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
183 err = bpf_percpu_array_update(map, key, value, flags);
184 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
185 err = bpf_percpu_cgroup_storage_update(map, key, value,
187 } else if (IS_FD_ARRAY(map)) {
189 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
192 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
194 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
197 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
198 /* rcu_read_lock() is not needed */
199 err = bpf_fd_reuseport_array_update_elem(map, key, value,
201 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
202 map->map_type == BPF_MAP_TYPE_STACK) {
203 err = map->ops->map_push_elem(map, value, flags);
206 err = map->ops->map_update_elem(map, key, value, flags);
209 bpf_enable_instrumentation();
210 maybe_wait_bpf_programs(map);
215 static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
221 if (bpf_map_is_dev_bound(map))
222 return bpf_map_offload_lookup_elem(map, key, value);
224 bpf_disable_instrumentation();
225 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
226 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
227 err = bpf_percpu_hash_copy(map, key, value);
228 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
229 err = bpf_percpu_array_copy(map, key, value);
230 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
231 err = bpf_percpu_cgroup_storage_copy(map, key, value);
232 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
233 err = bpf_stackmap_copy(map, key, value);
234 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
235 err = bpf_fd_array_map_lookup_elem(map, key, value);
236 } else if (IS_FD_HASH(map)) {
237 err = bpf_fd_htab_map_lookup_elem(map, key, value);
238 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
239 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
240 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
241 map->map_type == BPF_MAP_TYPE_STACK) {
242 err = map->ops->map_peek_elem(map, value);
243 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
244 /* struct_ops map requires directly updating "value" */
245 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
248 if (map->ops->map_lookup_elem_sys_only)
249 ptr = map->ops->map_lookup_elem_sys_only(map, key);
251 ptr = map->ops->map_lookup_elem(map, key);
258 if (flags & BPF_F_LOCK)
259 /* lock 'ptr' and copy everything but lock */
260 copy_map_value_locked(map, value, ptr, true);
262 copy_map_value(map, value, ptr);
263 /* mask lock, since value wasn't zero inited */
264 check_and_init_map_lock(map, value);
269 bpf_enable_instrumentation();
270 maybe_wait_bpf_programs(map);
275 /* Please, do not use this function outside from the map creation path
276 * (e.g. in map update path) without taking care of setting the active
277 * memory cgroup (see at bpf_map_kmalloc_node() for example).
279 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
281 /* We really just want to fail instead of triggering OOM killer
282 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
283 * which is used for lower order allocation requests.
285 * It has been observed that higher order allocation requests done by
286 * vmalloc with __GFP_NORETRY being set might fail due to not trying
287 * to reclaim memory from the page cache, thus we set
288 * __GFP_RETRY_MAYFAIL to avoid such situations.
291 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
292 unsigned int flags = 0;
293 unsigned long align = 1;
296 if (size >= SIZE_MAX)
299 /* kmalloc()'ed memory can't be mmap()'ed */
301 BUG_ON(!PAGE_ALIGNED(size));
304 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
305 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
311 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
312 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
313 flags, numa_node, __builtin_return_address(0));
316 void *bpf_map_area_alloc(u64 size, int numa_node)
318 return __bpf_map_area_alloc(size, numa_node, false);
321 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
323 return __bpf_map_area_alloc(size, numa_node, true);
326 void bpf_map_area_free(void *area)
331 static u32 bpf_map_flags_retain_permanent(u32 flags)
333 /* Some map creation flags are not tied to the map object but
334 * rather to the map fd instead, so they have no meaning upon
335 * map object inspection since multiple file descriptors with
336 * different (access) properties can exist here. Thus, given
337 * this has zero meaning for the map itself, lets clear these
340 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
343 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
345 map->map_type = attr->map_type;
346 map->key_size = attr->key_size;
347 map->value_size = attr->value_size;
348 map->max_entries = attr->max_entries;
349 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
350 map->numa_node = bpf_map_attr_numa_node(attr);
353 static int bpf_map_alloc_id(struct bpf_map *map)
357 idr_preload(GFP_KERNEL);
358 spin_lock_bh(&map_idr_lock);
359 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
362 spin_unlock_bh(&map_idr_lock);
365 if (WARN_ON_ONCE(!id))
368 return id > 0 ? 0 : id;
371 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
375 /* Offloaded maps are removed from the IDR store when their device
376 * disappears - even if someone holds an fd to them they are unusable,
377 * the memory is gone, all ops will fail; they are simply waiting for
378 * refcnt to drop to be freed.
384 spin_lock_irqsave(&map_idr_lock, flags);
386 __acquire(&map_idr_lock);
388 idr_remove(&map_idr, map->id);
392 spin_unlock_irqrestore(&map_idr_lock, flags);
394 __release(&map_idr_lock);
397 #ifdef CONFIG_MEMCG_KMEM
398 static void bpf_map_save_memcg(struct bpf_map *map)
400 map->memcg = get_mem_cgroup_from_mm(current->mm);
403 static void bpf_map_release_memcg(struct bpf_map *map)
405 mem_cgroup_put(map->memcg);
408 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
411 struct mem_cgroup *old_memcg;
414 old_memcg = set_active_memcg(map->memcg);
415 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
416 set_active_memcg(old_memcg);
421 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
423 struct mem_cgroup *old_memcg;
426 old_memcg = set_active_memcg(map->memcg);
427 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
428 set_active_memcg(old_memcg);
433 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
434 size_t align, gfp_t flags)
436 struct mem_cgroup *old_memcg;
439 old_memcg = set_active_memcg(map->memcg);
440 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
441 set_active_memcg(old_memcg);
447 static void bpf_map_save_memcg(struct bpf_map *map)
451 static void bpf_map_release_memcg(struct bpf_map *map)
456 /* called from workqueue */
457 static void bpf_map_free_deferred(struct work_struct *work)
459 struct bpf_map *map = container_of(work, struct bpf_map, work);
461 security_bpf_map_free(map);
462 bpf_map_release_memcg(map);
463 /* implementation dependent freeing */
464 map->ops->map_free(map);
467 static void bpf_map_put_uref(struct bpf_map *map)
469 if (atomic64_dec_and_test(&map->usercnt)) {
470 if (map->ops->map_release_uref)
471 map->ops->map_release_uref(map);
475 /* decrement map refcnt and schedule it for freeing via workqueue
476 * (unrelying map implementation ops->map_free() might sleep)
478 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
480 if (atomic64_dec_and_test(&map->refcnt)) {
481 /* bpf_map_free_id() must be called first */
482 bpf_map_free_id(map, do_idr_lock);
484 INIT_WORK(&map->work, bpf_map_free_deferred);
485 schedule_work(&map->work);
489 void bpf_map_put(struct bpf_map *map)
491 __bpf_map_put(map, true);
493 EXPORT_SYMBOL_GPL(bpf_map_put);
495 void bpf_map_put_with_uref(struct bpf_map *map)
497 bpf_map_put_uref(map);
501 static int bpf_map_release(struct inode *inode, struct file *filp)
503 struct bpf_map *map = filp->private_data;
505 if (map->ops->map_release)
506 map->ops->map_release(map, filp);
508 bpf_map_put_with_uref(map);
512 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
514 fmode_t mode = f.file->f_mode;
516 /* Our file permissions may have been overridden by global
517 * map permissions facing syscall side.
519 if (READ_ONCE(map->frozen))
520 mode &= ~FMODE_CAN_WRITE;
524 #ifdef CONFIG_PROC_FS
525 /* Provides an approximation of the map's memory footprint.
526 * Used only to provide a backward compatibility and display
527 * a reasonable "memlock" info.
529 static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
533 size = round_up(map->key_size + bpf_map_value_size(map), 8);
535 return round_up(map->max_entries * size, PAGE_SIZE);
538 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
540 const struct bpf_map *map = filp->private_data;
541 const struct bpf_array *array;
542 u32 type = 0, jited = 0;
544 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
545 array = container_of(map, struct bpf_array, map);
546 type = array->aux->type;
547 jited = array->aux->jited;
564 bpf_map_memory_footprint(map),
566 READ_ONCE(map->frozen));
568 seq_printf(m, "owner_prog_type:\t%u\n", type);
569 seq_printf(m, "owner_jited:\t%u\n", jited);
574 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
577 /* We need this handler such that alloc_file() enables
578 * f_mode with FMODE_CAN_READ.
583 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
584 size_t siz, loff_t *ppos)
586 /* We need this handler such that alloc_file() enables
587 * f_mode with FMODE_CAN_WRITE.
592 /* called for any extra memory-mapped regions (except initial) */
593 static void bpf_map_mmap_open(struct vm_area_struct *vma)
595 struct bpf_map *map = vma->vm_file->private_data;
597 if (vma->vm_flags & VM_MAYWRITE) {
598 mutex_lock(&map->freeze_mutex);
600 mutex_unlock(&map->freeze_mutex);
604 /* called for all unmapped memory region (including initial) */
605 static void bpf_map_mmap_close(struct vm_area_struct *vma)
607 struct bpf_map *map = vma->vm_file->private_data;
609 if (vma->vm_flags & VM_MAYWRITE) {
610 mutex_lock(&map->freeze_mutex);
612 mutex_unlock(&map->freeze_mutex);
616 static const struct vm_operations_struct bpf_map_default_vmops = {
617 .open = bpf_map_mmap_open,
618 .close = bpf_map_mmap_close,
621 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
623 struct bpf_map *map = filp->private_data;
626 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
629 if (!(vma->vm_flags & VM_SHARED))
632 mutex_lock(&map->freeze_mutex);
634 if (vma->vm_flags & VM_WRITE) {
639 /* map is meant to be read-only, so do not allow mapping as
640 * writable, because it's possible to leak a writable page
641 * reference and allows user-space to still modify it after
642 * freezing, while verifier will assume contents do not change
644 if (map->map_flags & BPF_F_RDONLY_PROG) {
650 /* set default open/close callbacks */
651 vma->vm_ops = &bpf_map_default_vmops;
652 vma->vm_private_data = map;
653 vma->vm_flags &= ~VM_MAYEXEC;
654 if (!(vma->vm_flags & VM_WRITE))
655 /* disallow re-mapping with PROT_WRITE */
656 vma->vm_flags &= ~VM_MAYWRITE;
658 err = map->ops->map_mmap(map, vma);
662 if (vma->vm_flags & VM_MAYWRITE)
665 mutex_unlock(&map->freeze_mutex);
669 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
671 struct bpf_map *map = filp->private_data;
673 if (map->ops->map_poll)
674 return map->ops->map_poll(map, filp, pts);
679 const struct file_operations bpf_map_fops = {
680 #ifdef CONFIG_PROC_FS
681 .show_fdinfo = bpf_map_show_fdinfo,
683 .release = bpf_map_release,
684 .read = bpf_dummy_read,
685 .write = bpf_dummy_write,
686 .mmap = bpf_map_mmap,
687 .poll = bpf_map_poll,
690 int bpf_map_new_fd(struct bpf_map *map, int flags)
694 ret = security_bpf_map(map, OPEN_FMODE(flags));
698 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
702 int bpf_get_file_flag(int flags)
704 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
706 if (flags & BPF_F_RDONLY)
708 if (flags & BPF_F_WRONLY)
713 /* helper macro to check that unused fields 'union bpf_attr' are zero */
714 #define CHECK_ATTR(CMD) \
715 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
716 sizeof(attr->CMD##_LAST_FIELD), 0, \
718 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
719 sizeof(attr->CMD##_LAST_FIELD)) != NULL
721 /* dst and src must have at least "size" number of bytes.
722 * Return strlen on success and < 0 on error.
724 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
726 const char *end = src + size;
727 const char *orig_src = src;
729 memset(dst, 0, size);
730 /* Copy all isalnum(), '_' and '.' chars. */
731 while (src < end && *src) {
732 if (!isalnum(*src) &&
733 *src != '_' && *src != '.')
738 /* No '\0' found in "size" number of bytes */
742 return src - orig_src;
745 int map_check_no_btf(const struct bpf_map *map,
746 const struct btf *btf,
747 const struct btf_type *key_type,
748 const struct btf_type *value_type)
753 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
754 u32 btf_key_id, u32 btf_value_id)
756 const struct btf_type *key_type, *value_type;
757 u32 key_size, value_size;
760 /* Some maps allow key to be unspecified. */
762 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
763 if (!key_type || key_size != map->key_size)
766 key_type = btf_type_by_id(btf, 0);
767 if (!map->ops->map_check_btf)
771 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
772 if (!value_type || value_size != map->value_size)
775 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
777 if (map_value_has_spin_lock(map)) {
778 if (map->map_flags & BPF_F_RDONLY_PROG)
780 if (map->map_type != BPF_MAP_TYPE_HASH &&
781 map->map_type != BPF_MAP_TYPE_ARRAY &&
782 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
783 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
784 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
785 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
787 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
790 "verifier bug spin_lock_off %d value_size %d\n",
791 map->spin_lock_off, map->value_size);
796 if (map->ops->map_check_btf)
797 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
802 #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
803 /* called via syscall */
804 static int map_create(union bpf_attr *attr)
806 int numa_node = bpf_map_attr_numa_node(attr);
811 err = CHECK_ATTR(BPF_MAP_CREATE);
815 if (attr->btf_vmlinux_value_type_id) {
816 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
817 attr->btf_key_type_id || attr->btf_value_type_id)
819 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
823 f_flags = bpf_get_file_flag(attr->map_flags);
827 if (numa_node != NUMA_NO_NODE &&
828 ((unsigned int)numa_node >= nr_node_ids ||
829 !node_online(numa_node)))
832 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
833 map = find_and_alloc_map(attr);
837 err = bpf_obj_name_cpy(map->name, attr->map_name,
838 sizeof(attr->map_name));
842 atomic64_set(&map->refcnt, 1);
843 atomic64_set(&map->usercnt, 1);
844 mutex_init(&map->freeze_mutex);
846 map->spin_lock_off = -EINVAL;
847 if (attr->btf_key_type_id || attr->btf_value_type_id ||
848 /* Even the map's value is a kernel's struct,
849 * the bpf_prog.o must have BTF to begin with
850 * to figure out the corresponding kernel's
851 * counter part. Thus, attr->btf_fd has
854 attr->btf_vmlinux_value_type_id) {
857 btf = btf_get_by_fd(attr->btf_fd);
862 if (btf_is_kernel(btf)) {
869 if (attr->btf_value_type_id) {
870 err = map_check_btf(map, btf, attr->btf_key_type_id,
871 attr->btf_value_type_id);
876 map->btf_key_type_id = attr->btf_key_type_id;
877 map->btf_value_type_id = attr->btf_value_type_id;
878 map->btf_vmlinux_value_type_id =
879 attr->btf_vmlinux_value_type_id;
882 err = security_bpf_map_alloc(map);
886 err = bpf_map_alloc_id(map);
890 bpf_map_save_memcg(map);
892 err = bpf_map_new_fd(map, f_flags);
894 /* failed to allocate fd.
895 * bpf_map_put_with_uref() is needed because the above
896 * bpf_map_alloc_id() has published the map
897 * to the userspace and the userspace may
898 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
900 bpf_map_put_with_uref(map);
907 security_bpf_map_free(map);
910 map->ops->map_free(map);
914 /* if error is returned, fd is released.
915 * On success caller should complete fd access with matching fdput()
917 struct bpf_map *__bpf_map_get(struct fd f)
920 return ERR_PTR(-EBADF);
921 if (f.file->f_op != &bpf_map_fops) {
923 return ERR_PTR(-EINVAL);
926 return f.file->private_data;
929 void bpf_map_inc(struct bpf_map *map)
931 atomic64_inc(&map->refcnt);
933 EXPORT_SYMBOL_GPL(bpf_map_inc);
935 void bpf_map_inc_with_uref(struct bpf_map *map)
937 atomic64_inc(&map->refcnt);
938 atomic64_inc(&map->usercnt);
940 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
942 struct bpf_map *bpf_map_get(u32 ufd)
944 struct fd f = fdget(ufd);
947 map = __bpf_map_get(f);
957 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
959 struct fd f = fdget(ufd);
962 map = __bpf_map_get(f);
966 bpf_map_inc_with_uref(map);
972 /* map_idr_lock should have been held */
973 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
977 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
979 return ERR_PTR(-ENOENT);
981 atomic64_inc(&map->usercnt);
986 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
988 spin_lock_bh(&map_idr_lock);
989 map = __bpf_map_inc_not_zero(map, false);
990 spin_unlock_bh(&map_idr_lock);
994 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
996 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1001 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1004 return memdup_user(ukey, key_size);
1007 return ERR_PTR(-EINVAL);
1012 static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1015 return memdup_bpfptr(ukey, key_size);
1017 if (!bpfptr_is_null(ukey))
1018 return ERR_PTR(-EINVAL);
1023 /* last field in 'union bpf_attr' used by this command */
1024 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1026 static int map_lookup_elem(union bpf_attr *attr)
1028 void __user *ukey = u64_to_user_ptr(attr->key);
1029 void __user *uvalue = u64_to_user_ptr(attr->value);
1030 int ufd = attr->map_fd;
1031 struct bpf_map *map;
1037 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1040 if (attr->flags & ~BPF_F_LOCK)
1044 map = __bpf_map_get(f);
1046 return PTR_ERR(map);
1047 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1052 if ((attr->flags & BPF_F_LOCK) &&
1053 !map_value_has_spin_lock(map)) {
1058 key = __bpf_copy_key(ukey, map->key_size);
1064 value_size = bpf_map_value_size(map);
1067 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1071 err = bpf_map_copy_value(map, key, value, attr->flags);
1076 if (copy_to_user(uvalue, value, value_size) != 0)
1091 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1093 static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
1095 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1096 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
1097 int ufd = attr->map_fd;
1098 struct bpf_map *map;
1104 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1108 map = __bpf_map_get(f);
1110 return PTR_ERR(map);
1111 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1116 if ((attr->flags & BPF_F_LOCK) &&
1117 !map_value_has_spin_lock(map)) {
1122 key = ___bpf_copy_key(ukey, map->key_size);
1128 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1129 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
1130 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1131 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
1132 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1134 value_size = map->value_size;
1137 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1142 if (copy_from_bpfptr(value, uvalue, value_size) != 0)
1145 err = bpf_map_update_value(map, f, key, value, attr->flags);
1156 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1158 static int map_delete_elem(union bpf_attr *attr)
1160 void __user *ukey = u64_to_user_ptr(attr->key);
1161 int ufd = attr->map_fd;
1162 struct bpf_map *map;
1167 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1171 map = __bpf_map_get(f);
1173 return PTR_ERR(map);
1174 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1179 key = __bpf_copy_key(ukey, map->key_size);
1185 if (bpf_map_is_dev_bound(map)) {
1186 err = bpf_map_offload_delete_elem(map, key);
1188 } else if (IS_FD_PROG_ARRAY(map) ||
1189 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1190 /* These maps require sleepable context */
1191 err = map->ops->map_delete_elem(map, key);
1195 bpf_disable_instrumentation();
1197 err = map->ops->map_delete_elem(map, key);
1199 bpf_enable_instrumentation();
1200 maybe_wait_bpf_programs(map);
1208 /* last field in 'union bpf_attr' used by this command */
1209 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1211 static int map_get_next_key(union bpf_attr *attr)
1213 void __user *ukey = u64_to_user_ptr(attr->key);
1214 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1215 int ufd = attr->map_fd;
1216 struct bpf_map *map;
1217 void *key, *next_key;
1221 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1225 map = __bpf_map_get(f);
1227 return PTR_ERR(map);
1228 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1234 key = __bpf_copy_key(ukey, map->key_size);
1244 next_key = kmalloc(map->key_size, GFP_USER);
1248 if (bpf_map_is_dev_bound(map)) {
1249 err = bpf_map_offload_get_next_key(map, key, next_key);
1254 err = map->ops->map_get_next_key(map, key, next_key);
1261 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1275 int generic_map_delete_batch(struct bpf_map *map,
1276 const union bpf_attr *attr,
1277 union bpf_attr __user *uattr)
1279 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1284 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1287 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1288 !map_value_has_spin_lock(map)) {
1292 max_count = attr->batch.count;
1296 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1300 for (cp = 0; cp < max_count; cp++) {
1302 if (copy_from_user(key, keys + cp * map->key_size,
1306 if (bpf_map_is_dev_bound(map)) {
1307 err = bpf_map_offload_delete_elem(map, key);
1311 bpf_disable_instrumentation();
1313 err = map->ops->map_delete_elem(map, key);
1315 bpf_enable_instrumentation();
1316 maybe_wait_bpf_programs(map);
1320 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1327 int generic_map_update_batch(struct bpf_map *map,
1328 const union bpf_attr *attr,
1329 union bpf_attr __user *uattr)
1331 void __user *values = u64_to_user_ptr(attr->batch.values);
1332 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1333 u32 value_size, cp, max_count;
1334 int ufd = attr->map_fd;
1340 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1343 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1344 !map_value_has_spin_lock(map)) {
1348 value_size = bpf_map_value_size(map);
1350 max_count = attr->batch.count;
1354 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1358 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1364 for (cp = 0; cp < max_count; cp++) {
1366 if (copy_from_user(key, keys + cp * map->key_size,
1368 copy_from_user(value, values + cp * value_size, value_size))
1371 err = bpf_map_update_value(map, f, key, value,
1372 attr->batch.elem_flags);
1378 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1386 #define MAP_LOOKUP_RETRIES 3
1388 int generic_map_lookup_batch(struct bpf_map *map,
1389 const union bpf_attr *attr,
1390 union bpf_attr __user *uattr)
1392 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1393 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1394 void __user *values = u64_to_user_ptr(attr->batch.values);
1395 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1396 void *buf, *buf_prevkey, *prev_key, *key, *value;
1397 int err, retry = MAP_LOOKUP_RETRIES;
1398 u32 value_size, cp, max_count;
1400 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1403 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1404 !map_value_has_spin_lock(map))
1407 value_size = bpf_map_value_size(map);
1409 max_count = attr->batch.count;
1413 if (put_user(0, &uattr->batch.count))
1416 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1420 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1428 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1431 value = key + map->key_size;
1433 prev_key = buf_prevkey;
1435 for (cp = 0; cp < max_count;) {
1437 err = map->ops->map_get_next_key(map, prev_key, key);
1441 err = bpf_map_copy_value(map, key, value,
1442 attr->batch.elem_flags);
1444 if (err == -ENOENT) {
1456 if (copy_to_user(keys + cp * map->key_size, key,
1461 if (copy_to_user(values + cp * value_size, value, value_size)) {
1467 prev_key = buf_prevkey;
1469 swap(prev_key, key);
1470 retry = MAP_LOOKUP_RETRIES;
1477 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1478 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1487 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
1489 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1491 void __user *ukey = u64_to_user_ptr(attr->key);
1492 void __user *uvalue = u64_to_user_ptr(attr->value);
1493 int ufd = attr->map_fd;
1494 struct bpf_map *map;
1500 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1503 if (attr->flags & ~BPF_F_LOCK)
1507 map = __bpf_map_get(f);
1509 return PTR_ERR(map);
1510 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1511 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1517 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1518 map->map_type == BPF_MAP_TYPE_STACK)) {
1523 if ((attr->flags & BPF_F_LOCK) &&
1524 !map_value_has_spin_lock(map)) {
1529 key = __bpf_copy_key(ukey, map->key_size);
1535 value_size = bpf_map_value_size(map);
1538 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1543 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1544 map->map_type == BPF_MAP_TYPE_STACK) {
1545 err = map->ops->map_pop_elem(map, value);
1546 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1547 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1548 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1549 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1550 if (!bpf_map_is_dev_bound(map)) {
1551 bpf_disable_instrumentation();
1553 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1555 bpf_enable_instrumentation();
1562 if (copy_to_user(uvalue, value, value_size) != 0) {
1578 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1580 static int map_freeze(const union bpf_attr *attr)
1582 int err = 0, ufd = attr->map_fd;
1583 struct bpf_map *map;
1586 if (CHECK_ATTR(BPF_MAP_FREEZE))
1590 map = __bpf_map_get(f);
1592 return PTR_ERR(map);
1594 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1599 mutex_lock(&map->freeze_mutex);
1601 if (map->writecnt) {
1605 if (READ_ONCE(map->frozen)) {
1609 if (!bpf_capable()) {
1614 WRITE_ONCE(map->frozen, true);
1616 mutex_unlock(&map->freeze_mutex);
1621 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1622 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1623 [_id] = & _name ## _prog_ops,
1624 #define BPF_MAP_TYPE(_id, _ops)
1625 #define BPF_LINK_TYPE(_id, _name)
1626 #include <linux/bpf_types.h>
1627 #undef BPF_PROG_TYPE
1629 #undef BPF_LINK_TYPE
1632 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1634 const struct bpf_prog_ops *ops;
1636 if (type >= ARRAY_SIZE(bpf_prog_types))
1638 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1639 ops = bpf_prog_types[type];
1643 if (!bpf_prog_is_dev_bound(prog->aux))
1644 prog->aux->ops = ops;
1646 prog->aux->ops = &bpf_offload_prog_ops;
1657 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1658 [BPF_AUDIT_LOAD] = "LOAD",
1659 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1662 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1664 struct audit_context *ctx = NULL;
1665 struct audit_buffer *ab;
1667 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1669 if (audit_enabled == AUDIT_OFF)
1671 if (op == BPF_AUDIT_LOAD)
1672 ctx = audit_context();
1673 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1676 audit_log_format(ab, "prog-id=%u op=%s",
1677 prog->aux->id, bpf_audit_str[op]);
1681 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1685 idr_preload(GFP_KERNEL);
1686 spin_lock_bh(&prog_idr_lock);
1687 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1690 spin_unlock_bh(&prog_idr_lock);
1693 /* id is in [1, INT_MAX) */
1694 if (WARN_ON_ONCE(!id))
1697 return id > 0 ? 0 : id;
1700 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1702 /* cBPF to eBPF migrations are currently not in the idr store.
1703 * Offloaded programs are removed from the store when their device
1704 * disappears - even if someone grabs an fd to them they are unusable,
1705 * simply waiting for refcnt to drop to be freed.
1711 spin_lock_bh(&prog_idr_lock);
1713 __acquire(&prog_idr_lock);
1715 idr_remove(&prog_idr, prog->aux->id);
1719 spin_unlock_bh(&prog_idr_lock);
1721 __release(&prog_idr_lock);
1724 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1726 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1728 kvfree(aux->func_info);
1729 kfree(aux->func_info_aux);
1730 free_uid(aux->user);
1731 security_bpf_prog_free(aux);
1732 bpf_prog_free(aux->prog);
1735 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1737 bpf_prog_kallsyms_del_all(prog);
1738 btf_put(prog->aux->btf);
1739 kvfree(prog->aux->jited_linfo);
1740 kvfree(prog->aux->linfo);
1741 kfree(prog->aux->kfunc_tab);
1742 if (prog->aux->attach_btf)
1743 btf_put(prog->aux->attach_btf);
1746 if (prog->aux->sleepable)
1747 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1749 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1751 __bpf_prog_put_rcu(&prog->aux->rcu);
1755 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1757 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
1758 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1759 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1760 /* bpf_prog_free_id() must be called first */
1761 bpf_prog_free_id(prog, do_idr_lock);
1762 __bpf_prog_put_noref(prog, true);
1766 void bpf_prog_put(struct bpf_prog *prog)
1768 __bpf_prog_put(prog, true);
1770 EXPORT_SYMBOL_GPL(bpf_prog_put);
1772 static int bpf_prog_release(struct inode *inode, struct file *filp)
1774 struct bpf_prog *prog = filp->private_data;
1780 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1781 struct bpf_prog_stats *stats)
1783 u64 nsecs = 0, cnt = 0, misses = 0;
1786 for_each_possible_cpu(cpu) {
1787 const struct bpf_prog_stats *st;
1789 u64 tnsecs, tcnt, tmisses;
1791 st = per_cpu_ptr(prog->stats, cpu);
1793 start = u64_stats_fetch_begin_irq(&st->syncp);
1796 tmisses = st->misses;
1797 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1802 stats->nsecs = nsecs;
1804 stats->misses = misses;
1807 #ifdef CONFIG_PROC_FS
1808 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1810 const struct bpf_prog *prog = filp->private_data;
1811 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1812 struct bpf_prog_stats stats;
1814 bpf_prog_get_stats(prog, &stats);
1815 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1822 "run_time_ns:\t%llu\n"
1824 "recursion_misses:\t%llu\n",
1828 prog->pages * 1ULL << PAGE_SHIFT,
1836 const struct file_operations bpf_prog_fops = {
1837 #ifdef CONFIG_PROC_FS
1838 .show_fdinfo = bpf_prog_show_fdinfo,
1840 .release = bpf_prog_release,
1841 .read = bpf_dummy_read,
1842 .write = bpf_dummy_write,
1845 int bpf_prog_new_fd(struct bpf_prog *prog)
1849 ret = security_bpf_prog(prog);
1853 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1854 O_RDWR | O_CLOEXEC);
1857 static struct bpf_prog *____bpf_prog_get(struct fd f)
1860 return ERR_PTR(-EBADF);
1861 if (f.file->f_op != &bpf_prog_fops) {
1863 return ERR_PTR(-EINVAL);
1866 return f.file->private_data;
1869 void bpf_prog_add(struct bpf_prog *prog, int i)
1871 atomic64_add(i, &prog->aux->refcnt);
1873 EXPORT_SYMBOL_GPL(bpf_prog_add);
1875 void bpf_prog_sub(struct bpf_prog *prog, int i)
1877 /* Only to be used for undoing previous bpf_prog_add() in some
1878 * error path. We still know that another entity in our call
1879 * path holds a reference to the program, thus atomic_sub() can
1880 * be safely used in such cases!
1882 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1884 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1886 void bpf_prog_inc(struct bpf_prog *prog)
1888 atomic64_inc(&prog->aux->refcnt);
1890 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1892 /* prog_idr_lock should have been held */
1893 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1897 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1900 return ERR_PTR(-ENOENT);
1904 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1906 bool bpf_prog_get_ok(struct bpf_prog *prog,
1907 enum bpf_prog_type *attach_type, bool attach_drv)
1909 /* not an attachment, just a refcount inc, always allow */
1913 if (prog->type != *attach_type)
1915 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1921 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1924 struct fd f = fdget(ufd);
1925 struct bpf_prog *prog;
1927 prog = ____bpf_prog_get(f);
1930 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1931 prog = ERR_PTR(-EINVAL);
1941 struct bpf_prog *bpf_prog_get(u32 ufd)
1943 return __bpf_prog_get(ufd, NULL, false);
1946 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1949 return __bpf_prog_get(ufd, &type, attach_drv);
1951 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1953 /* Initially all BPF programs could be loaded w/o specifying
1954 * expected_attach_type. Later for some of them specifying expected_attach_type
1955 * at load time became required so that program could be validated properly.
1956 * Programs of types that are allowed to be loaded both w/ and w/o (for
1957 * backward compatibility) expected_attach_type, should have the default attach
1958 * type assigned to expected_attach_type for the latter case, so that it can be
1959 * validated later at attach time.
1961 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1962 * prog type requires it but has some attach types that have to be backward
1965 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1967 switch (attr->prog_type) {
1968 case BPF_PROG_TYPE_CGROUP_SOCK:
1969 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1970 * exist so checking for non-zero is the way to go here.
1972 if (!attr->expected_attach_type)
1973 attr->expected_attach_type =
1974 BPF_CGROUP_INET_SOCK_CREATE;
1976 case BPF_PROG_TYPE_SK_REUSEPORT:
1977 if (!attr->expected_attach_type)
1978 attr->expected_attach_type =
1979 BPF_SK_REUSEPORT_SELECT;
1985 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1986 enum bpf_attach_type expected_attach_type,
1987 struct btf *attach_btf, u32 btf_id,
1988 struct bpf_prog *dst_prog)
1991 if (btf_id > BTF_MAX_TYPE)
1994 if (!attach_btf && !dst_prog)
1997 switch (prog_type) {
1998 case BPF_PROG_TYPE_TRACING:
1999 case BPF_PROG_TYPE_LSM:
2000 case BPF_PROG_TYPE_STRUCT_OPS:
2001 case BPF_PROG_TYPE_EXT:
2008 if (attach_btf && (!btf_id || dst_prog))
2011 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
2012 prog_type != BPF_PROG_TYPE_EXT)
2015 switch (prog_type) {
2016 case BPF_PROG_TYPE_CGROUP_SOCK:
2017 switch (expected_attach_type) {
2018 case BPF_CGROUP_INET_SOCK_CREATE:
2019 case BPF_CGROUP_INET_SOCK_RELEASE:
2020 case BPF_CGROUP_INET4_POST_BIND:
2021 case BPF_CGROUP_INET6_POST_BIND:
2026 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2027 switch (expected_attach_type) {
2028 case BPF_CGROUP_INET4_BIND:
2029 case BPF_CGROUP_INET6_BIND:
2030 case BPF_CGROUP_INET4_CONNECT:
2031 case BPF_CGROUP_INET6_CONNECT:
2032 case BPF_CGROUP_INET4_GETPEERNAME:
2033 case BPF_CGROUP_INET6_GETPEERNAME:
2034 case BPF_CGROUP_INET4_GETSOCKNAME:
2035 case BPF_CGROUP_INET6_GETSOCKNAME:
2036 case BPF_CGROUP_UDP4_SENDMSG:
2037 case BPF_CGROUP_UDP6_SENDMSG:
2038 case BPF_CGROUP_UDP4_RECVMSG:
2039 case BPF_CGROUP_UDP6_RECVMSG:
2044 case BPF_PROG_TYPE_CGROUP_SKB:
2045 switch (expected_attach_type) {
2046 case BPF_CGROUP_INET_INGRESS:
2047 case BPF_CGROUP_INET_EGRESS:
2052 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2053 switch (expected_attach_type) {
2054 case BPF_CGROUP_SETSOCKOPT:
2055 case BPF_CGROUP_GETSOCKOPT:
2060 case BPF_PROG_TYPE_SK_LOOKUP:
2061 if (expected_attach_type == BPF_SK_LOOKUP)
2064 case BPF_PROG_TYPE_SK_REUSEPORT:
2065 switch (expected_attach_type) {
2066 case BPF_SK_REUSEPORT_SELECT:
2067 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2072 case BPF_PROG_TYPE_SYSCALL:
2073 case BPF_PROG_TYPE_EXT:
2074 if (expected_attach_type)
2082 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2084 switch (prog_type) {
2085 case BPF_PROG_TYPE_SCHED_CLS:
2086 case BPF_PROG_TYPE_SCHED_ACT:
2087 case BPF_PROG_TYPE_XDP:
2088 case BPF_PROG_TYPE_LWT_IN:
2089 case BPF_PROG_TYPE_LWT_OUT:
2090 case BPF_PROG_TYPE_LWT_XMIT:
2091 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2092 case BPF_PROG_TYPE_SK_SKB:
2093 case BPF_PROG_TYPE_SK_MSG:
2094 case BPF_PROG_TYPE_LIRC_MODE2:
2095 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2096 case BPF_PROG_TYPE_CGROUP_DEVICE:
2097 case BPF_PROG_TYPE_CGROUP_SOCK:
2098 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2099 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2100 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2101 case BPF_PROG_TYPE_SOCK_OPS:
2102 case BPF_PROG_TYPE_EXT: /* extends any prog */
2104 case BPF_PROG_TYPE_CGROUP_SKB:
2106 case BPF_PROG_TYPE_SK_REUSEPORT:
2107 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2113 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2115 switch (prog_type) {
2116 case BPF_PROG_TYPE_KPROBE:
2117 case BPF_PROG_TYPE_TRACEPOINT:
2118 case BPF_PROG_TYPE_PERF_EVENT:
2119 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2120 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2121 case BPF_PROG_TYPE_TRACING:
2122 case BPF_PROG_TYPE_LSM:
2123 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2124 case BPF_PROG_TYPE_EXT: /* extends any prog */
2131 /* last field in 'union bpf_attr' used by this command */
2132 #define BPF_PROG_LOAD_LAST_FIELD fd_array
2134 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
2136 enum bpf_prog_type type = attr->prog_type;
2137 struct bpf_prog *prog, *dst_prog = NULL;
2138 struct btf *attach_btf = NULL;
2143 if (CHECK_ATTR(BPF_PROG_LOAD))
2146 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2147 BPF_F_ANY_ALIGNMENT |
2148 BPF_F_TEST_STATE_FREQ |
2150 BPF_F_TEST_RND_HI32))
2153 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2154 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2158 /* copy eBPF program license from user space */
2159 if (strncpy_from_bpfptr(license,
2160 make_bpfptr(attr->license, uattr.is_kernel),
2161 sizeof(license) - 1) < 0)
2163 license[sizeof(license) - 1] = 0;
2165 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2166 is_gpl = license_is_gpl_compatible(license);
2168 if (attr->insn_cnt == 0 ||
2169 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2171 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2172 type != BPF_PROG_TYPE_CGROUP_SKB &&
2176 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2178 if (is_perfmon_prog_type(type) && !perfmon_capable())
2181 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2182 * or btf, we need to check which one it is
2184 if (attr->attach_prog_fd) {
2185 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2186 if (IS_ERR(dst_prog)) {
2188 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2189 if (IS_ERR(attach_btf))
2191 if (!btf_is_kernel(attach_btf)) {
2192 /* attaching through specifying bpf_prog's BTF
2193 * objects directly might be supported eventually
2195 btf_put(attach_btf);
2199 } else if (attr->attach_btf_id) {
2200 /* fall back to vmlinux BTF, if BTF type ID is specified */
2201 attach_btf = bpf_get_btf_vmlinux();
2202 if (IS_ERR(attach_btf))
2203 return PTR_ERR(attach_btf);
2206 btf_get(attach_btf);
2209 bpf_prog_load_fixup_attach_type(attr);
2210 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2211 attach_btf, attr->attach_btf_id,
2214 bpf_prog_put(dst_prog);
2216 btf_put(attach_btf);
2220 /* plain bpf_prog allocation */
2221 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2224 bpf_prog_put(dst_prog);
2226 btf_put(attach_btf);
2230 prog->expected_attach_type = attr->expected_attach_type;
2231 prog->aux->attach_btf = attach_btf;
2232 prog->aux->attach_btf_id = attr->attach_btf_id;
2233 prog->aux->dst_prog = dst_prog;
2234 prog->aux->offload_requested = !!attr->prog_ifindex;
2235 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2237 err = security_bpf_prog_alloc(prog->aux);
2241 prog->aux->user = get_current_user();
2242 prog->len = attr->insn_cnt;
2245 if (copy_from_bpfptr(prog->insns,
2246 make_bpfptr(attr->insns, uattr.is_kernel),
2247 bpf_prog_insn_size(prog)) != 0)
2250 prog->orig_prog = NULL;
2253 atomic64_set(&prog->aux->refcnt, 1);
2254 prog->gpl_compatible = is_gpl ? 1 : 0;
2256 if (bpf_prog_is_dev_bound(prog->aux)) {
2257 err = bpf_prog_offload_init(prog, attr);
2262 /* find program type: socket_filter vs tracing_filter */
2263 err = find_prog_type(type, prog);
2267 prog->aux->load_time = ktime_get_boottime_ns();
2268 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2269 sizeof(attr->prog_name));
2273 /* run eBPF verifier */
2274 err = bpf_check(&prog, attr, uattr);
2276 goto free_used_maps;
2278 prog = bpf_prog_select_runtime(prog, &err);
2280 goto free_used_maps;
2282 err = bpf_prog_alloc_id(prog);
2284 goto free_used_maps;
2286 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2287 * effectively publicly exposed. However, retrieving via
2288 * bpf_prog_get_fd_by_id() will take another reference,
2289 * therefore it cannot be gone underneath us.
2291 * Only for the time /after/ successful bpf_prog_new_fd()
2292 * and before returning to userspace, we might just hold
2293 * one reference and any parallel close on that fd could
2294 * rip everything out. Hence, below notifications must
2295 * happen before bpf_prog_new_fd().
2297 * Also, any failure handling from this point onwards must
2298 * be using bpf_prog_put() given the program is exposed.
2300 bpf_prog_kallsyms_add(prog);
2301 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2302 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2304 err = bpf_prog_new_fd(prog);
2310 /* In case we have subprogs, we need to wait for a grace
2311 * period before we can tear down JIT memory since symbols
2312 * are already exposed under kallsyms.
2314 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2317 free_uid(prog->aux->user);
2318 security_bpf_prog_free(prog->aux);
2320 if (prog->aux->attach_btf)
2321 btf_put(prog->aux->attach_btf);
2322 bpf_prog_free(prog);
2326 #define BPF_OBJ_LAST_FIELD file_flags
2328 static int bpf_obj_pin(const union bpf_attr *attr)
2330 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2333 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2336 static int bpf_obj_get(const union bpf_attr *attr)
2338 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2339 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2342 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2346 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2347 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2349 atomic64_set(&link->refcnt, 1);
2356 static void bpf_link_free_id(int id)
2361 spin_lock_bh(&link_idr_lock);
2362 idr_remove(&link_idr, id);
2363 spin_unlock_bh(&link_idr_lock);
2366 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2367 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2368 * anon_inode's release() call. This helper marksbpf_link as
2369 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2370 * is not decremented, it's the responsibility of a calling code that failed
2371 * to complete bpf_link initialization.
2373 void bpf_link_cleanup(struct bpf_link_primer *primer)
2375 primer->link->prog = NULL;
2376 bpf_link_free_id(primer->id);
2378 put_unused_fd(primer->fd);
2381 void bpf_link_inc(struct bpf_link *link)
2383 atomic64_inc(&link->refcnt);
2386 /* bpf_link_free is guaranteed to be called from process context */
2387 static void bpf_link_free(struct bpf_link *link)
2389 bpf_link_free_id(link->id);
2391 /* detach BPF program, clean up used resources */
2392 link->ops->release(link);
2393 bpf_prog_put(link->prog);
2395 /* free bpf_link and its containing memory */
2396 link->ops->dealloc(link);
2399 static void bpf_link_put_deferred(struct work_struct *work)
2401 struct bpf_link *link = container_of(work, struct bpf_link, work);
2403 bpf_link_free(link);
2406 /* bpf_link_put can be called from atomic context, but ensures that resources
2407 * are freed from process context
2409 void bpf_link_put(struct bpf_link *link)
2411 if (!atomic64_dec_and_test(&link->refcnt))
2415 INIT_WORK(&link->work, bpf_link_put_deferred);
2416 schedule_work(&link->work);
2418 bpf_link_free(link);
2422 static int bpf_link_release(struct inode *inode, struct file *filp)
2424 struct bpf_link *link = filp->private_data;
2430 #ifdef CONFIG_PROC_FS
2431 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2432 #define BPF_MAP_TYPE(_id, _ops)
2433 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2434 static const char *bpf_link_type_strs[] = {
2435 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2436 #include <linux/bpf_types.h>
2438 #undef BPF_PROG_TYPE
2440 #undef BPF_LINK_TYPE
2442 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2444 const struct bpf_link *link = filp->private_data;
2445 const struct bpf_prog *prog = link->prog;
2446 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2448 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2454 bpf_link_type_strs[link->type],
2458 if (link->ops->show_fdinfo)
2459 link->ops->show_fdinfo(link, m);
2463 static const struct file_operations bpf_link_fops = {
2464 #ifdef CONFIG_PROC_FS
2465 .show_fdinfo = bpf_link_show_fdinfo,
2467 .release = bpf_link_release,
2468 .read = bpf_dummy_read,
2469 .write = bpf_dummy_write,
2472 static int bpf_link_alloc_id(struct bpf_link *link)
2476 idr_preload(GFP_KERNEL);
2477 spin_lock_bh(&link_idr_lock);
2478 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2479 spin_unlock_bh(&link_idr_lock);
2485 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2486 * reserving unused FD and allocating ID from link_idr. This is to be paired
2487 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2488 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2489 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2490 * transient state is passed around in struct bpf_link_primer.
2491 * This is preferred way to create and initialize bpf_link, especially when
2492 * there are complicated and expensive operations inbetween creating bpf_link
2493 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2494 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2495 * expensive (and potentially failing) roll back operations in a rare case
2496 * that file, FD, or ID can't be allocated.
2498 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2503 fd = get_unused_fd_flags(O_CLOEXEC);
2508 id = bpf_link_alloc_id(link);
2514 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2516 bpf_link_free_id(id);
2518 return PTR_ERR(file);
2521 primer->link = link;
2522 primer->file = file;
2528 int bpf_link_settle(struct bpf_link_primer *primer)
2530 /* make bpf_link fetchable by ID */
2531 spin_lock_bh(&link_idr_lock);
2532 primer->link->id = primer->id;
2533 spin_unlock_bh(&link_idr_lock);
2534 /* make bpf_link fetchable by FD */
2535 fd_install(primer->fd, primer->file);
2536 /* pass through installed FD */
2540 int bpf_link_new_fd(struct bpf_link *link)
2542 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2545 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2547 struct fd f = fdget(ufd);
2548 struct bpf_link *link;
2551 return ERR_PTR(-EBADF);
2552 if (f.file->f_op != &bpf_link_fops) {
2554 return ERR_PTR(-EINVAL);
2557 link = f.file->private_data;
2564 struct bpf_tracing_link {
2565 struct bpf_link link;
2566 enum bpf_attach_type attach_type;
2567 struct bpf_trampoline *trampoline;
2568 struct bpf_prog *tgt_prog;
2571 static void bpf_tracing_link_release(struct bpf_link *link)
2573 struct bpf_tracing_link *tr_link =
2574 container_of(link, struct bpf_tracing_link, link);
2576 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2577 tr_link->trampoline));
2579 bpf_trampoline_put(tr_link->trampoline);
2581 /* tgt_prog is NULL if target is a kernel function */
2582 if (tr_link->tgt_prog)
2583 bpf_prog_put(tr_link->tgt_prog);
2586 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2588 struct bpf_tracing_link *tr_link =
2589 container_of(link, struct bpf_tracing_link, link);
2594 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2595 struct seq_file *seq)
2597 struct bpf_tracing_link *tr_link =
2598 container_of(link, struct bpf_tracing_link, link);
2601 "attach_type:\t%d\n",
2602 tr_link->attach_type);
2605 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2606 struct bpf_link_info *info)
2608 struct bpf_tracing_link *tr_link =
2609 container_of(link, struct bpf_tracing_link, link);
2611 info->tracing.attach_type = tr_link->attach_type;
2612 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2613 &info->tracing.target_obj_id,
2614 &info->tracing.target_btf_id);
2619 static const struct bpf_link_ops bpf_tracing_link_lops = {
2620 .release = bpf_tracing_link_release,
2621 .dealloc = bpf_tracing_link_dealloc,
2622 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2623 .fill_link_info = bpf_tracing_link_fill_link_info,
2626 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2630 struct bpf_link_primer link_primer;
2631 struct bpf_prog *tgt_prog = NULL;
2632 struct bpf_trampoline *tr = NULL;
2633 struct bpf_tracing_link *link;
2637 switch (prog->type) {
2638 case BPF_PROG_TYPE_TRACING:
2639 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2640 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2641 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2646 case BPF_PROG_TYPE_EXT:
2647 if (prog->expected_attach_type != 0) {
2652 case BPF_PROG_TYPE_LSM:
2653 if (prog->expected_attach_type != BPF_LSM_MAC) {
2663 if (!!tgt_prog_fd != !!btf_id) {
2669 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2670 if (prog->type != BPF_PROG_TYPE_EXT) {
2675 tgt_prog = bpf_prog_get(tgt_prog_fd);
2676 if (IS_ERR(tgt_prog)) {
2677 err = PTR_ERR(tgt_prog);
2682 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
2685 link = kzalloc(sizeof(*link), GFP_USER);
2690 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2691 &bpf_tracing_link_lops, prog);
2692 link->attach_type = prog->expected_attach_type;
2694 mutex_lock(&prog->aux->dst_mutex);
2696 /* There are a few possible cases here:
2698 * - if prog->aux->dst_trampoline is set, the program was just loaded
2699 * and not yet attached to anything, so we can use the values stored
2702 * - if prog->aux->dst_trampoline is NULL, the program has already been
2703 * attached to a target and its initial target was cleared (below)
2705 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2706 * target_btf_id using the link_create API.
2708 * - if tgt_prog == NULL when this function was called using the old
2709 * raw_tracepoint_open API, and we need a target from prog->aux
2711 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
2712 * was detached and is going for re-attachment.
2714 if (!prog->aux->dst_trampoline && !tgt_prog) {
2716 * Allow re-attach for TRACING and LSM programs. If it's
2717 * currently linked, bpf_trampoline_link_prog will fail.
2718 * EXT programs need to specify tgt_prog_fd, so they
2719 * re-attach in separate code path.
2721 if (prog->type != BPF_PROG_TYPE_TRACING &&
2722 prog->type != BPF_PROG_TYPE_LSM) {
2726 btf_id = prog->aux->attach_btf_id;
2727 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
2730 if (!prog->aux->dst_trampoline ||
2731 (key && key != prog->aux->dst_trampoline->key)) {
2732 /* If there is no saved target, or the specified target is
2733 * different from the destination specified at load time, we
2734 * need a new trampoline and a check for compatibility
2736 struct bpf_attach_target_info tgt_info = {};
2738 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2743 tr = bpf_trampoline_get(key, &tgt_info);
2749 /* The caller didn't specify a target, or the target was the
2750 * same as the destination supplied during program load. This
2751 * means we can reuse the trampoline and reference from program
2752 * load time, and there is no need to allocate a new one. This
2753 * can only happen once for any program, as the saved values in
2754 * prog->aux are cleared below.
2756 tr = prog->aux->dst_trampoline;
2757 tgt_prog = prog->aux->dst_prog;
2760 err = bpf_link_prime(&link->link, &link_primer);
2764 err = bpf_trampoline_link_prog(prog, tr);
2766 bpf_link_cleanup(&link_primer);
2771 link->tgt_prog = tgt_prog;
2772 link->trampoline = tr;
2774 /* Always clear the trampoline and target prog from prog->aux to make
2775 * sure the original attach destination is not kept alive after a
2776 * program is (re-)attached to another target.
2778 if (prog->aux->dst_prog &&
2779 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2780 /* got extra prog ref from syscall, or attaching to different prog */
2781 bpf_prog_put(prog->aux->dst_prog);
2782 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2783 /* we allocated a new trampoline, so free the old one */
2784 bpf_trampoline_put(prog->aux->dst_trampoline);
2786 prog->aux->dst_prog = NULL;
2787 prog->aux->dst_trampoline = NULL;
2788 mutex_unlock(&prog->aux->dst_mutex);
2790 return bpf_link_settle(&link_primer);
2792 if (tr && tr != prog->aux->dst_trampoline)
2793 bpf_trampoline_put(tr);
2794 mutex_unlock(&prog->aux->dst_mutex);
2797 if (tgt_prog_fd && tgt_prog)
2798 bpf_prog_put(tgt_prog);
2802 struct bpf_raw_tp_link {
2803 struct bpf_link link;
2804 struct bpf_raw_event_map *btp;
2807 static void bpf_raw_tp_link_release(struct bpf_link *link)
2809 struct bpf_raw_tp_link *raw_tp =
2810 container_of(link, struct bpf_raw_tp_link, link);
2812 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2813 bpf_put_raw_tracepoint(raw_tp->btp);
2816 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2818 struct bpf_raw_tp_link *raw_tp =
2819 container_of(link, struct bpf_raw_tp_link, link);
2824 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2825 struct seq_file *seq)
2827 struct bpf_raw_tp_link *raw_tp_link =
2828 container_of(link, struct bpf_raw_tp_link, link);
2832 raw_tp_link->btp->tp->name);
2835 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2836 struct bpf_link_info *info)
2838 struct bpf_raw_tp_link *raw_tp_link =
2839 container_of(link, struct bpf_raw_tp_link, link);
2840 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2841 const char *tp_name = raw_tp_link->btp->tp->name;
2842 u32 ulen = info->raw_tracepoint.tp_name_len;
2843 size_t tp_len = strlen(tp_name);
2848 info->raw_tracepoint.tp_name_len = tp_len + 1;
2853 if (ulen >= tp_len + 1) {
2854 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2859 if (copy_to_user(ubuf, tp_name, ulen - 1))
2861 if (put_user(zero, ubuf + ulen - 1))
2869 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
2870 .release = bpf_raw_tp_link_release,
2871 .dealloc = bpf_raw_tp_link_dealloc,
2872 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2873 .fill_link_info = bpf_raw_tp_link_fill_link_info,
2876 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2878 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2880 struct bpf_link_primer link_primer;
2881 struct bpf_raw_tp_link *link;
2882 struct bpf_raw_event_map *btp;
2883 struct bpf_prog *prog;
2884 const char *tp_name;
2888 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2891 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2893 return PTR_ERR(prog);
2895 switch (prog->type) {
2896 case BPF_PROG_TYPE_TRACING:
2897 case BPF_PROG_TYPE_EXT:
2898 case BPF_PROG_TYPE_LSM:
2899 if (attr->raw_tracepoint.name) {
2900 /* The attach point for this category of programs
2901 * should be specified via btf_id during program load.
2906 if (prog->type == BPF_PROG_TYPE_TRACING &&
2907 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
2908 tp_name = prog->aux->attach_func_name;
2911 err = bpf_tracing_prog_attach(prog, 0, 0);
2915 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2916 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2917 if (strncpy_from_user(buf,
2918 u64_to_user_ptr(attr->raw_tracepoint.name),
2919 sizeof(buf) - 1) < 0) {
2923 buf[sizeof(buf) - 1] = 0;
2931 btp = bpf_get_raw_tracepoint(tp_name);
2937 link = kzalloc(sizeof(*link), GFP_USER);
2942 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2943 &bpf_raw_tp_link_lops, prog);
2946 err = bpf_link_prime(&link->link, &link_primer);
2952 err = bpf_probe_register(link->btp, prog);
2954 bpf_link_cleanup(&link_primer);
2958 return bpf_link_settle(&link_primer);
2961 bpf_put_raw_tracepoint(btp);
2967 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2968 enum bpf_attach_type attach_type)
2970 switch (prog->type) {
2971 case BPF_PROG_TYPE_CGROUP_SOCK:
2972 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2973 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2974 case BPF_PROG_TYPE_SK_LOOKUP:
2975 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
2976 case BPF_PROG_TYPE_CGROUP_SKB:
2977 if (!capable(CAP_NET_ADMIN))
2978 /* cg-skb progs can be loaded by unpriv user.
2979 * check permissions at attach time.
2982 return prog->enforce_expected_attach_type &&
2983 prog->expected_attach_type != attach_type ?
2990 static enum bpf_prog_type
2991 attach_type_to_prog_type(enum bpf_attach_type attach_type)
2993 switch (attach_type) {
2994 case BPF_CGROUP_INET_INGRESS:
2995 case BPF_CGROUP_INET_EGRESS:
2996 return BPF_PROG_TYPE_CGROUP_SKB;
2997 case BPF_CGROUP_INET_SOCK_CREATE:
2998 case BPF_CGROUP_INET_SOCK_RELEASE:
2999 case BPF_CGROUP_INET4_POST_BIND:
3000 case BPF_CGROUP_INET6_POST_BIND:
3001 return BPF_PROG_TYPE_CGROUP_SOCK;
3002 case BPF_CGROUP_INET4_BIND:
3003 case BPF_CGROUP_INET6_BIND:
3004 case BPF_CGROUP_INET4_CONNECT:
3005 case BPF_CGROUP_INET6_CONNECT:
3006 case BPF_CGROUP_INET4_GETPEERNAME:
3007 case BPF_CGROUP_INET6_GETPEERNAME:
3008 case BPF_CGROUP_INET4_GETSOCKNAME:
3009 case BPF_CGROUP_INET6_GETSOCKNAME:
3010 case BPF_CGROUP_UDP4_SENDMSG:
3011 case BPF_CGROUP_UDP6_SENDMSG:
3012 case BPF_CGROUP_UDP4_RECVMSG:
3013 case BPF_CGROUP_UDP6_RECVMSG:
3014 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3015 case BPF_CGROUP_SOCK_OPS:
3016 return BPF_PROG_TYPE_SOCK_OPS;
3017 case BPF_CGROUP_DEVICE:
3018 return BPF_PROG_TYPE_CGROUP_DEVICE;
3019 case BPF_SK_MSG_VERDICT:
3020 return BPF_PROG_TYPE_SK_MSG;
3021 case BPF_SK_SKB_STREAM_PARSER:
3022 case BPF_SK_SKB_STREAM_VERDICT:
3023 case BPF_SK_SKB_VERDICT:
3024 return BPF_PROG_TYPE_SK_SKB;
3025 case BPF_LIRC_MODE2:
3026 return BPF_PROG_TYPE_LIRC_MODE2;
3027 case BPF_FLOW_DISSECTOR:
3028 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3029 case BPF_CGROUP_SYSCTL:
3030 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3031 case BPF_CGROUP_GETSOCKOPT:
3032 case BPF_CGROUP_SETSOCKOPT:
3033 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
3034 case BPF_TRACE_ITER:
3035 return BPF_PROG_TYPE_TRACING;
3037 return BPF_PROG_TYPE_SK_LOOKUP;
3039 return BPF_PROG_TYPE_XDP;
3041 return BPF_PROG_TYPE_UNSPEC;
3045 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
3047 #define BPF_F_ATTACH_MASK \
3048 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
3050 static int bpf_prog_attach(const union bpf_attr *attr)
3052 enum bpf_prog_type ptype;
3053 struct bpf_prog *prog;
3056 if (CHECK_ATTR(BPF_PROG_ATTACH))
3059 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
3062 ptype = attach_type_to_prog_type(attr->attach_type);
3063 if (ptype == BPF_PROG_TYPE_UNSPEC)
3066 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3068 return PTR_ERR(prog);
3070 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3076 case BPF_PROG_TYPE_SK_SKB:
3077 case BPF_PROG_TYPE_SK_MSG:
3078 ret = sock_map_get_from_fd(attr, prog);
3080 case BPF_PROG_TYPE_LIRC_MODE2:
3081 ret = lirc_prog_attach(attr, prog);
3083 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3084 ret = netns_bpf_prog_attach(attr, prog);
3086 case BPF_PROG_TYPE_CGROUP_DEVICE:
3087 case BPF_PROG_TYPE_CGROUP_SKB:
3088 case BPF_PROG_TYPE_CGROUP_SOCK:
3089 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3090 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3091 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3092 case BPF_PROG_TYPE_SOCK_OPS:
3093 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3104 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3106 static int bpf_prog_detach(const union bpf_attr *attr)
3108 enum bpf_prog_type ptype;
3110 if (CHECK_ATTR(BPF_PROG_DETACH))
3113 ptype = attach_type_to_prog_type(attr->attach_type);
3116 case BPF_PROG_TYPE_SK_MSG:
3117 case BPF_PROG_TYPE_SK_SKB:
3118 return sock_map_prog_detach(attr, ptype);
3119 case BPF_PROG_TYPE_LIRC_MODE2:
3120 return lirc_prog_detach(attr);
3121 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3122 return netns_bpf_prog_detach(attr, ptype);
3123 case BPF_PROG_TYPE_CGROUP_DEVICE:
3124 case BPF_PROG_TYPE_CGROUP_SKB:
3125 case BPF_PROG_TYPE_CGROUP_SOCK:
3126 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3127 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3128 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3129 case BPF_PROG_TYPE_SOCK_OPS:
3130 return cgroup_bpf_prog_detach(attr, ptype);
3136 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3138 static int bpf_prog_query(const union bpf_attr *attr,
3139 union bpf_attr __user *uattr)
3141 if (!capable(CAP_NET_ADMIN))
3143 if (CHECK_ATTR(BPF_PROG_QUERY))
3145 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3148 switch (attr->query.attach_type) {
3149 case BPF_CGROUP_INET_INGRESS:
3150 case BPF_CGROUP_INET_EGRESS:
3151 case BPF_CGROUP_INET_SOCK_CREATE:
3152 case BPF_CGROUP_INET_SOCK_RELEASE:
3153 case BPF_CGROUP_INET4_BIND:
3154 case BPF_CGROUP_INET6_BIND:
3155 case BPF_CGROUP_INET4_POST_BIND:
3156 case BPF_CGROUP_INET6_POST_BIND:
3157 case BPF_CGROUP_INET4_CONNECT:
3158 case BPF_CGROUP_INET6_CONNECT:
3159 case BPF_CGROUP_INET4_GETPEERNAME:
3160 case BPF_CGROUP_INET6_GETPEERNAME:
3161 case BPF_CGROUP_INET4_GETSOCKNAME:
3162 case BPF_CGROUP_INET6_GETSOCKNAME:
3163 case BPF_CGROUP_UDP4_SENDMSG:
3164 case BPF_CGROUP_UDP6_SENDMSG:
3165 case BPF_CGROUP_UDP4_RECVMSG:
3166 case BPF_CGROUP_UDP6_RECVMSG:
3167 case BPF_CGROUP_SOCK_OPS:
3168 case BPF_CGROUP_DEVICE:
3169 case BPF_CGROUP_SYSCTL:
3170 case BPF_CGROUP_GETSOCKOPT:
3171 case BPF_CGROUP_SETSOCKOPT:
3172 return cgroup_bpf_prog_query(attr, uattr);
3173 case BPF_LIRC_MODE2:
3174 return lirc_prog_query(attr, uattr);
3175 case BPF_FLOW_DISSECTOR:
3177 return netns_bpf_prog_query(attr, uattr);
3183 #define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
3185 static int bpf_prog_test_run(const union bpf_attr *attr,
3186 union bpf_attr __user *uattr)
3188 struct bpf_prog *prog;
3189 int ret = -ENOTSUPP;
3191 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3194 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3195 (!attr->test.ctx_size_in && attr->test.ctx_in))
3198 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3199 (!attr->test.ctx_size_out && attr->test.ctx_out))
3202 prog = bpf_prog_get(attr->test.prog_fd);
3204 return PTR_ERR(prog);
3206 if (prog->aux->ops->test_run)
3207 ret = prog->aux->ops->test_run(prog, attr, uattr);
3213 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3215 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3216 union bpf_attr __user *uattr,
3220 u32 next_id = attr->start_id;
3223 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3226 if (!capable(CAP_SYS_ADMIN))
3231 if (!idr_get_next(idr, &next_id))
3233 spin_unlock_bh(lock);
3236 err = put_user(next_id, &uattr->next_id);
3241 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3243 struct bpf_map *map;
3245 spin_lock_bh(&map_idr_lock);
3247 map = idr_get_next(&map_idr, id);
3249 map = __bpf_map_inc_not_zero(map, false);
3255 spin_unlock_bh(&map_idr_lock);
3260 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3262 struct bpf_prog *prog;
3264 spin_lock_bh(&prog_idr_lock);
3266 prog = idr_get_next(&prog_idr, id);
3268 prog = bpf_prog_inc_not_zero(prog);
3274 spin_unlock_bh(&prog_idr_lock);
3279 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3281 struct bpf_prog *bpf_prog_by_id(u32 id)
3283 struct bpf_prog *prog;
3286 return ERR_PTR(-ENOENT);
3288 spin_lock_bh(&prog_idr_lock);
3289 prog = idr_find(&prog_idr, id);
3291 prog = bpf_prog_inc_not_zero(prog);
3293 prog = ERR_PTR(-ENOENT);
3294 spin_unlock_bh(&prog_idr_lock);
3298 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3300 struct bpf_prog *prog;
3301 u32 id = attr->prog_id;
3304 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3307 if (!capable(CAP_SYS_ADMIN))
3310 prog = bpf_prog_by_id(id);
3312 return PTR_ERR(prog);
3314 fd = bpf_prog_new_fd(prog);
3321 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3323 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3325 struct bpf_map *map;
3326 u32 id = attr->map_id;
3330 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3331 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3334 if (!capable(CAP_SYS_ADMIN))
3337 f_flags = bpf_get_file_flag(attr->open_flags);
3341 spin_lock_bh(&map_idr_lock);
3342 map = idr_find(&map_idr, id);
3344 map = __bpf_map_inc_not_zero(map, true);
3346 map = ERR_PTR(-ENOENT);
3347 spin_unlock_bh(&map_idr_lock);
3350 return PTR_ERR(map);
3352 fd = bpf_map_new_fd(map, f_flags);
3354 bpf_map_put_with_uref(map);
3359 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3360 unsigned long addr, u32 *off,
3363 const struct bpf_map *map;
3366 mutex_lock(&prog->aux->used_maps_mutex);
3367 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3368 map = prog->aux->used_maps[i];
3369 if (map == (void *)addr) {
3370 *type = BPF_PSEUDO_MAP_FD;
3373 if (!map->ops->map_direct_value_meta)
3375 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3376 *type = BPF_PSEUDO_MAP_VALUE;
3383 mutex_unlock(&prog->aux->used_maps_mutex);
3387 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3388 const struct cred *f_cred)
3390 const struct bpf_map *map;
3391 struct bpf_insn *insns;
3397 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3402 for (i = 0; i < prog->len; i++) {
3403 code = insns[i].code;
3405 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3406 insns[i].code = BPF_JMP | BPF_CALL;
3407 insns[i].imm = BPF_FUNC_tail_call;
3410 if (code == (BPF_JMP | BPF_CALL) ||
3411 code == (BPF_JMP | BPF_CALL_ARGS)) {
3412 if (code == (BPF_JMP | BPF_CALL_ARGS))
3413 insns[i].code = BPF_JMP | BPF_CALL;
3414 if (!bpf_dump_raw_ok(f_cred))
3418 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3419 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3423 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3426 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3427 map = bpf_map_from_imm(prog, imm, &off, &type);
3429 insns[i].src_reg = type;
3430 insns[i].imm = map->id;
3431 insns[i + 1].imm = off;
3439 static int set_info_rec_size(struct bpf_prog_info *info)
3442 * Ensure info.*_rec_size is the same as kernel expected size
3446 * Only allow zero *_rec_size if both _rec_size and _cnt are
3447 * zero. In this case, the kernel will set the expected
3448 * _rec_size back to the info.
3451 if ((info->nr_func_info || info->func_info_rec_size) &&
3452 info->func_info_rec_size != sizeof(struct bpf_func_info))
3455 if ((info->nr_line_info || info->line_info_rec_size) &&
3456 info->line_info_rec_size != sizeof(struct bpf_line_info))
3459 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3460 info->jited_line_info_rec_size != sizeof(__u64))
3463 info->func_info_rec_size = sizeof(struct bpf_func_info);
3464 info->line_info_rec_size = sizeof(struct bpf_line_info);
3465 info->jited_line_info_rec_size = sizeof(__u64);
3470 static int bpf_prog_get_info_by_fd(struct file *file,
3471 struct bpf_prog *prog,
3472 const union bpf_attr *attr,
3473 union bpf_attr __user *uattr)
3475 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3476 struct bpf_prog_info info;
3477 u32 info_len = attr->info.info_len;
3478 struct bpf_prog_stats stats;
3479 char __user *uinsns;
3483 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3486 info_len = min_t(u32, sizeof(info), info_len);
3488 memset(&info, 0, sizeof(info));
3489 if (copy_from_user(&info, uinfo, info_len))
3492 info.type = prog->type;
3493 info.id = prog->aux->id;
3494 info.load_time = prog->aux->load_time;
3495 info.created_by_uid = from_kuid_munged(current_user_ns(),
3496 prog->aux->user->uid);
3497 info.gpl_compatible = prog->gpl_compatible;
3499 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3500 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3502 mutex_lock(&prog->aux->used_maps_mutex);
3503 ulen = info.nr_map_ids;
3504 info.nr_map_ids = prog->aux->used_map_cnt;
3505 ulen = min_t(u32, info.nr_map_ids, ulen);
3507 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3510 for (i = 0; i < ulen; i++)
3511 if (put_user(prog->aux->used_maps[i]->id,
3512 &user_map_ids[i])) {
3513 mutex_unlock(&prog->aux->used_maps_mutex);
3517 mutex_unlock(&prog->aux->used_maps_mutex);
3519 err = set_info_rec_size(&info);
3523 bpf_prog_get_stats(prog, &stats);
3524 info.run_time_ns = stats.nsecs;
3525 info.run_cnt = stats.cnt;
3526 info.recursion_misses = stats.misses;
3528 if (!bpf_capable()) {
3529 info.jited_prog_len = 0;
3530 info.xlated_prog_len = 0;
3531 info.nr_jited_ksyms = 0;
3532 info.nr_jited_func_lens = 0;
3533 info.nr_func_info = 0;
3534 info.nr_line_info = 0;
3535 info.nr_jited_line_info = 0;
3539 ulen = info.xlated_prog_len;
3540 info.xlated_prog_len = bpf_prog_insn_size(prog);
3541 if (info.xlated_prog_len && ulen) {
3542 struct bpf_insn *insns_sanitized;
3545 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3546 info.xlated_prog_insns = 0;
3549 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3550 if (!insns_sanitized)
3552 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3553 ulen = min_t(u32, info.xlated_prog_len, ulen);
3554 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3555 kfree(insns_sanitized);
3560 if (bpf_prog_is_dev_bound(prog->aux)) {
3561 err = bpf_prog_offload_info_fill(&info, prog);
3567 /* NOTE: the following code is supposed to be skipped for offload.
3568 * bpf_prog_offload_info_fill() is the place to fill similar fields
3571 ulen = info.jited_prog_len;
3572 if (prog->aux->func_cnt) {
3575 info.jited_prog_len = 0;
3576 for (i = 0; i < prog->aux->func_cnt; i++)
3577 info.jited_prog_len += prog->aux->func[i]->jited_len;
3579 info.jited_prog_len = prog->jited_len;
3582 if (info.jited_prog_len && ulen) {
3583 if (bpf_dump_raw_ok(file->f_cred)) {
3584 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3585 ulen = min_t(u32, info.jited_prog_len, ulen);
3587 /* for multi-function programs, copy the JITed
3588 * instructions for all the functions
3590 if (prog->aux->func_cnt) {
3595 for (i = 0; i < prog->aux->func_cnt; i++) {
3596 len = prog->aux->func[i]->jited_len;
3597 len = min_t(u32, len, free);
3598 img = (u8 *) prog->aux->func[i]->bpf_func;
3599 if (copy_to_user(uinsns, img, len))
3607 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3611 info.jited_prog_insns = 0;
3615 ulen = info.nr_jited_ksyms;
3616 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
3618 if (bpf_dump_raw_ok(file->f_cred)) {
3619 unsigned long ksym_addr;
3620 u64 __user *user_ksyms;
3623 /* copy the address of the kernel symbol
3624 * corresponding to each function
3626 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3627 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3628 if (prog->aux->func_cnt) {
3629 for (i = 0; i < ulen; i++) {
3630 ksym_addr = (unsigned long)
3631 prog->aux->func[i]->bpf_func;
3632 if (put_user((u64) ksym_addr,
3637 ksym_addr = (unsigned long) prog->bpf_func;
3638 if (put_user((u64) ksym_addr, &user_ksyms[0]))
3642 info.jited_ksyms = 0;
3646 ulen = info.nr_jited_func_lens;
3647 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
3649 if (bpf_dump_raw_ok(file->f_cred)) {
3650 u32 __user *user_lens;
3653 /* copy the JITed image lengths for each function */
3654 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3655 user_lens = u64_to_user_ptr(info.jited_func_lens);
3656 if (prog->aux->func_cnt) {
3657 for (i = 0; i < ulen; i++) {
3659 prog->aux->func[i]->jited_len;
3660 if (put_user(func_len, &user_lens[i]))
3664 func_len = prog->jited_len;
3665 if (put_user(func_len, &user_lens[0]))
3669 info.jited_func_lens = 0;
3674 info.btf_id = btf_obj_id(prog->aux->btf);
3676 ulen = info.nr_func_info;
3677 info.nr_func_info = prog->aux->func_info_cnt;
3678 if (info.nr_func_info && ulen) {
3679 char __user *user_finfo;
3681 user_finfo = u64_to_user_ptr(info.func_info);
3682 ulen = min_t(u32, info.nr_func_info, ulen);
3683 if (copy_to_user(user_finfo, prog->aux->func_info,
3684 info.func_info_rec_size * ulen))
3688 ulen = info.nr_line_info;
3689 info.nr_line_info = prog->aux->nr_linfo;
3690 if (info.nr_line_info && ulen) {
3691 __u8 __user *user_linfo;
3693 user_linfo = u64_to_user_ptr(info.line_info);
3694 ulen = min_t(u32, info.nr_line_info, ulen);
3695 if (copy_to_user(user_linfo, prog->aux->linfo,
3696 info.line_info_rec_size * ulen))
3700 ulen = info.nr_jited_line_info;
3701 if (prog->aux->jited_linfo)
3702 info.nr_jited_line_info = prog->aux->nr_linfo;
3704 info.nr_jited_line_info = 0;
3705 if (info.nr_jited_line_info && ulen) {
3706 if (bpf_dump_raw_ok(file->f_cred)) {
3707 __u64 __user *user_linfo;
3710 user_linfo = u64_to_user_ptr(info.jited_line_info);
3711 ulen = min_t(u32, info.nr_jited_line_info, ulen);
3712 for (i = 0; i < ulen; i++) {
3713 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3718 info.jited_line_info = 0;
3722 ulen = info.nr_prog_tags;
3723 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3725 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3728 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3729 ulen = min_t(u32, info.nr_prog_tags, ulen);
3730 if (prog->aux->func_cnt) {
3731 for (i = 0; i < ulen; i++) {
3732 if (copy_to_user(user_prog_tags[i],
3733 prog->aux->func[i]->tag,
3738 if (copy_to_user(user_prog_tags[0],
3739 prog->tag, BPF_TAG_SIZE))
3745 if (copy_to_user(uinfo, &info, info_len) ||
3746 put_user(info_len, &uattr->info.info_len))
3752 static int bpf_map_get_info_by_fd(struct file *file,
3753 struct bpf_map *map,
3754 const union bpf_attr *attr,
3755 union bpf_attr __user *uattr)
3757 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3758 struct bpf_map_info info;
3759 u32 info_len = attr->info.info_len;
3762 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3765 info_len = min_t(u32, sizeof(info), info_len);
3767 memset(&info, 0, sizeof(info));
3768 info.type = map->map_type;
3770 info.key_size = map->key_size;
3771 info.value_size = map->value_size;
3772 info.max_entries = map->max_entries;
3773 info.map_flags = map->map_flags;
3774 memcpy(info.name, map->name, sizeof(map->name));
3777 info.btf_id = btf_obj_id(map->btf);
3778 info.btf_key_type_id = map->btf_key_type_id;
3779 info.btf_value_type_id = map->btf_value_type_id;
3781 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
3783 if (bpf_map_is_dev_bound(map)) {
3784 err = bpf_map_offload_info_fill(&info, map);
3789 if (copy_to_user(uinfo, &info, info_len) ||
3790 put_user(info_len, &uattr->info.info_len))
3796 static int bpf_btf_get_info_by_fd(struct file *file,
3798 const union bpf_attr *attr,
3799 union bpf_attr __user *uattr)
3801 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3802 u32 info_len = attr->info.info_len;
3805 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
3809 return btf_get_info_by_fd(btf, attr, uattr);
3812 static int bpf_link_get_info_by_fd(struct file *file,
3813 struct bpf_link *link,
3814 const union bpf_attr *attr,
3815 union bpf_attr __user *uattr)
3817 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3818 struct bpf_link_info info;
3819 u32 info_len = attr->info.info_len;
3822 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3825 info_len = min_t(u32, sizeof(info), info_len);
3827 memset(&info, 0, sizeof(info));
3828 if (copy_from_user(&info, uinfo, info_len))
3831 info.type = link->type;
3833 info.prog_id = link->prog->aux->id;
3835 if (link->ops->fill_link_info) {
3836 err = link->ops->fill_link_info(link, &info);
3841 if (copy_to_user(uinfo, &info, info_len) ||
3842 put_user(info_len, &uattr->info.info_len))
3849 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3851 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3852 union bpf_attr __user *uattr)
3854 int ufd = attr->info.bpf_fd;
3858 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3865 if (f.file->f_op == &bpf_prog_fops)
3866 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
3868 else if (f.file->f_op == &bpf_map_fops)
3869 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
3871 else if (f.file->f_op == &btf_fops)
3872 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
3873 else if (f.file->f_op == &bpf_link_fops)
3874 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
3883 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3885 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
3887 if (CHECK_ATTR(BPF_BTF_LOAD))
3893 return btf_new_fd(attr, uattr);
3896 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3898 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3900 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3903 if (!capable(CAP_SYS_ADMIN))
3906 return btf_get_fd_by_id(attr->btf_id);
3909 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3910 union bpf_attr __user *uattr,
3911 u32 prog_id, u32 fd_type,
3912 const char *buf, u64 probe_offset,
3915 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3916 u32 len = buf ? strlen(buf) : 0, input_len;
3919 if (put_user(len, &uattr->task_fd_query.buf_len))
3921 input_len = attr->task_fd_query.buf_len;
3922 if (input_len && ubuf) {
3924 /* nothing to copy, just make ubuf NULL terminated */
3927 if (put_user(zero, ubuf))
3929 } else if (input_len >= len + 1) {
3930 /* ubuf can hold the string with NULL terminator */
3931 if (copy_to_user(ubuf, buf, len + 1))
3934 /* ubuf cannot hold the string with NULL terminator,
3935 * do a partial copy with NULL terminator.
3940 if (copy_to_user(ubuf, buf, input_len - 1))
3942 if (put_user(zero, ubuf + input_len - 1))
3947 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3948 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3949 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3950 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3956 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3958 static int bpf_task_fd_query(const union bpf_attr *attr,
3959 union bpf_attr __user *uattr)
3961 pid_t pid = attr->task_fd_query.pid;
3962 u32 fd = attr->task_fd_query.fd;
3963 const struct perf_event *event;
3964 struct task_struct *task;
3968 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3971 if (!capable(CAP_SYS_ADMIN))
3974 if (attr->task_fd_query.flags != 0)
3977 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3982 file = fget_task(task, fd);
3983 put_task_struct(task);
3987 if (file->f_op == &bpf_link_fops) {
3988 struct bpf_link *link = file->private_data;
3990 if (link->ops == &bpf_raw_tp_link_lops) {
3991 struct bpf_raw_tp_link *raw_tp =
3992 container_of(link, struct bpf_raw_tp_link, link);
3993 struct bpf_raw_event_map *btp = raw_tp->btp;
3995 err = bpf_task_fd_query_copy(attr, uattr,
3996 raw_tp->link.prog->aux->id,
3997 BPF_FD_TYPE_RAW_TRACEPOINT,
3998 btp->tp->name, 0, 0);
4004 event = perf_get_event(file);
4005 if (!IS_ERR(event)) {
4006 u64 probe_offset, probe_addr;
4007 u32 prog_id, fd_type;
4010 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4011 &buf, &probe_offset,
4014 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4028 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
4030 #define BPF_DO_BATCH(fn) \
4036 err = fn(map, attr, uattr); \
4039 static int bpf_map_do_batch(const union bpf_attr *attr,
4040 union bpf_attr __user *uattr,
4043 struct bpf_map *map;
4047 if (CHECK_ATTR(BPF_MAP_BATCH))
4050 ufd = attr->batch.map_fd;
4052 map = __bpf_map_get(f);
4054 return PTR_ERR(map);
4056 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
4057 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
4058 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4063 if (cmd != BPF_MAP_LOOKUP_BATCH &&
4064 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4069 if (cmd == BPF_MAP_LOOKUP_BATCH)
4070 BPF_DO_BATCH(map->ops->map_lookup_batch);
4071 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4072 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4073 else if (cmd == BPF_MAP_UPDATE_BATCH)
4074 BPF_DO_BATCH(map->ops->map_update_batch);
4076 BPF_DO_BATCH(map->ops->map_delete_batch);
4083 static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
4084 struct bpf_prog *prog)
4086 if (attr->link_create.attach_type != prog->expected_attach_type)
4089 if (prog->expected_attach_type == BPF_TRACE_ITER)
4090 return bpf_iter_link_attach(attr, uattr, prog);
4091 else if (prog->type == BPF_PROG_TYPE_EXT)
4092 return bpf_tracing_prog_attach(prog,
4093 attr->link_create.target_fd,
4094 attr->link_create.target_btf_id);
4098 #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
4099 static int link_create(union bpf_attr *attr, bpfptr_t uattr)
4101 enum bpf_prog_type ptype;
4102 struct bpf_prog *prog;
4105 if (CHECK_ATTR(BPF_LINK_CREATE))
4108 prog = bpf_prog_get(attr->link_create.prog_fd);
4110 return PTR_ERR(prog);
4112 ret = bpf_prog_attach_check_attach_type(prog,
4113 attr->link_create.attach_type);
4117 if (prog->type == BPF_PROG_TYPE_EXT) {
4118 ret = tracing_bpf_link_attach(attr, uattr, prog);
4122 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4123 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4129 case BPF_PROG_TYPE_CGROUP_SKB:
4130 case BPF_PROG_TYPE_CGROUP_SOCK:
4131 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4132 case BPF_PROG_TYPE_SOCK_OPS:
4133 case BPF_PROG_TYPE_CGROUP_DEVICE:
4134 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4135 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4136 ret = cgroup_bpf_link_attach(attr, prog);
4138 case BPF_PROG_TYPE_TRACING:
4139 ret = tracing_bpf_link_attach(attr, uattr, prog);
4141 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4142 case BPF_PROG_TYPE_SK_LOOKUP:
4143 ret = netns_bpf_link_create(attr, prog);
4146 case BPF_PROG_TYPE_XDP:
4147 ret = bpf_xdp_link_attach(attr, prog);
4160 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4162 static int link_update(union bpf_attr *attr)
4164 struct bpf_prog *old_prog = NULL, *new_prog;
4165 struct bpf_link *link;
4169 if (CHECK_ATTR(BPF_LINK_UPDATE))
4172 flags = attr->link_update.flags;
4173 if (flags & ~BPF_F_REPLACE)
4176 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4178 return PTR_ERR(link);
4180 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4181 if (IS_ERR(new_prog)) {
4182 ret = PTR_ERR(new_prog);
4186 if (flags & BPF_F_REPLACE) {
4187 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4188 if (IS_ERR(old_prog)) {
4189 ret = PTR_ERR(old_prog);
4193 } else if (attr->link_update.old_prog_fd) {
4198 if (link->ops->update_prog)
4199 ret = link->ops->update_prog(link, new_prog, old_prog);
4205 bpf_prog_put(old_prog);
4207 bpf_prog_put(new_prog);
4213 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4215 static int link_detach(union bpf_attr *attr)
4217 struct bpf_link *link;
4220 if (CHECK_ATTR(BPF_LINK_DETACH))
4223 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4225 return PTR_ERR(link);
4227 if (link->ops->detach)
4228 ret = link->ops->detach(link);
4236 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4238 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4241 struct bpf_link *bpf_link_by_id(u32 id)
4243 struct bpf_link *link;
4246 return ERR_PTR(-ENOENT);
4248 spin_lock_bh(&link_idr_lock);
4249 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4250 link = idr_find(&link_idr, id);
4253 link = bpf_link_inc_not_zero(link);
4255 link = ERR_PTR(-EAGAIN);
4257 link = ERR_PTR(-ENOENT);
4259 spin_unlock_bh(&link_idr_lock);
4263 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4265 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4267 struct bpf_link *link;
4268 u32 id = attr->link_id;
4271 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4274 if (!capable(CAP_SYS_ADMIN))
4277 link = bpf_link_by_id(id);
4279 return PTR_ERR(link);
4281 fd = bpf_link_new_fd(link);
4288 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4290 static int bpf_stats_release(struct inode *inode, struct file *file)
4292 mutex_lock(&bpf_stats_enabled_mutex);
4293 static_key_slow_dec(&bpf_stats_enabled_key.key);
4294 mutex_unlock(&bpf_stats_enabled_mutex);
4298 static const struct file_operations bpf_stats_fops = {
4299 .release = bpf_stats_release,
4302 static int bpf_enable_runtime_stats(void)
4306 mutex_lock(&bpf_stats_enabled_mutex);
4308 /* Set a very high limit to avoid overflow */
4309 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4310 mutex_unlock(&bpf_stats_enabled_mutex);
4314 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4316 static_key_slow_inc(&bpf_stats_enabled_key.key);
4318 mutex_unlock(&bpf_stats_enabled_mutex);
4322 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4324 static int bpf_enable_stats(union bpf_attr *attr)
4327 if (CHECK_ATTR(BPF_ENABLE_STATS))
4330 if (!capable(CAP_SYS_ADMIN))
4333 switch (attr->enable_stats.type) {
4334 case BPF_STATS_RUN_TIME:
4335 return bpf_enable_runtime_stats();
4342 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4344 static int bpf_iter_create(union bpf_attr *attr)
4346 struct bpf_link *link;
4349 if (CHECK_ATTR(BPF_ITER_CREATE))
4352 if (attr->iter_create.flags)
4355 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4357 return PTR_ERR(link);
4359 err = bpf_iter_new_fd(link);
4365 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4367 static int bpf_prog_bind_map(union bpf_attr *attr)
4369 struct bpf_prog *prog;
4370 struct bpf_map *map;
4371 struct bpf_map **used_maps_old, **used_maps_new;
4374 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4377 if (attr->prog_bind_map.flags)
4380 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4382 return PTR_ERR(prog);
4384 map = bpf_map_get(attr->prog_bind_map.map_fd);
4390 mutex_lock(&prog->aux->used_maps_mutex);
4392 used_maps_old = prog->aux->used_maps;
4394 for (i = 0; i < prog->aux->used_map_cnt; i++)
4395 if (used_maps_old[i] == map) {
4400 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4401 sizeof(used_maps_new[0]),
4403 if (!used_maps_new) {
4408 memcpy(used_maps_new, used_maps_old,
4409 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4410 used_maps_new[prog->aux->used_map_cnt] = map;
4412 prog->aux->used_map_cnt++;
4413 prog->aux->used_maps = used_maps_new;
4415 kfree(used_maps_old);
4418 mutex_unlock(&prog->aux->used_maps_mutex);
4427 static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
4429 union bpf_attr attr;
4432 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
4435 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4438 size = min_t(u32, size, sizeof(attr));
4440 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4441 memset(&attr, 0, sizeof(attr));
4442 if (copy_from_bpfptr(&attr, uattr, size) != 0)
4445 err = security_bpf(cmd, &attr, size);
4450 case BPF_MAP_CREATE:
4451 err = map_create(&attr);
4453 case BPF_MAP_LOOKUP_ELEM:
4454 err = map_lookup_elem(&attr);
4456 case BPF_MAP_UPDATE_ELEM:
4457 err = map_update_elem(&attr, uattr);
4459 case BPF_MAP_DELETE_ELEM:
4460 err = map_delete_elem(&attr);
4462 case BPF_MAP_GET_NEXT_KEY:
4463 err = map_get_next_key(&attr);
4465 case BPF_MAP_FREEZE:
4466 err = map_freeze(&attr);
4469 err = bpf_prog_load(&attr, uattr);
4472 err = bpf_obj_pin(&attr);
4475 err = bpf_obj_get(&attr);
4477 case BPF_PROG_ATTACH:
4478 err = bpf_prog_attach(&attr);
4480 case BPF_PROG_DETACH:
4481 err = bpf_prog_detach(&attr);
4483 case BPF_PROG_QUERY:
4484 err = bpf_prog_query(&attr, uattr.user);
4486 case BPF_PROG_TEST_RUN:
4487 err = bpf_prog_test_run(&attr, uattr.user);
4489 case BPF_PROG_GET_NEXT_ID:
4490 err = bpf_obj_get_next_id(&attr, uattr.user,
4491 &prog_idr, &prog_idr_lock);
4493 case BPF_MAP_GET_NEXT_ID:
4494 err = bpf_obj_get_next_id(&attr, uattr.user,
4495 &map_idr, &map_idr_lock);
4497 case BPF_BTF_GET_NEXT_ID:
4498 err = bpf_obj_get_next_id(&attr, uattr.user,
4499 &btf_idr, &btf_idr_lock);
4501 case BPF_PROG_GET_FD_BY_ID:
4502 err = bpf_prog_get_fd_by_id(&attr);
4504 case BPF_MAP_GET_FD_BY_ID:
4505 err = bpf_map_get_fd_by_id(&attr);
4507 case BPF_OBJ_GET_INFO_BY_FD:
4508 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
4510 case BPF_RAW_TRACEPOINT_OPEN:
4511 err = bpf_raw_tracepoint_open(&attr);
4514 err = bpf_btf_load(&attr, uattr);
4516 case BPF_BTF_GET_FD_BY_ID:
4517 err = bpf_btf_get_fd_by_id(&attr);
4519 case BPF_TASK_FD_QUERY:
4520 err = bpf_task_fd_query(&attr, uattr.user);
4522 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4523 err = map_lookup_and_delete_elem(&attr);
4525 case BPF_MAP_LOOKUP_BATCH:
4526 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
4528 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4529 err = bpf_map_do_batch(&attr, uattr.user,
4530 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4532 case BPF_MAP_UPDATE_BATCH:
4533 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
4535 case BPF_MAP_DELETE_BATCH:
4536 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
4538 case BPF_LINK_CREATE:
4539 err = link_create(&attr, uattr);
4541 case BPF_LINK_UPDATE:
4542 err = link_update(&attr);
4544 case BPF_LINK_GET_FD_BY_ID:
4545 err = bpf_link_get_fd_by_id(&attr);
4547 case BPF_LINK_GET_NEXT_ID:
4548 err = bpf_obj_get_next_id(&attr, uattr.user,
4549 &link_idr, &link_idr_lock);
4551 case BPF_ENABLE_STATS:
4552 err = bpf_enable_stats(&attr);
4554 case BPF_ITER_CREATE:
4555 err = bpf_iter_create(&attr);
4557 case BPF_LINK_DETACH:
4558 err = link_detach(&attr);
4560 case BPF_PROG_BIND_MAP:
4561 err = bpf_prog_bind_map(&attr);
4571 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4573 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
4576 static bool syscall_prog_is_valid_access(int off, int size,
4577 enum bpf_access_type type,
4578 const struct bpf_prog *prog,
4579 struct bpf_insn_access_aux *info)
4581 if (off < 0 || off >= U16_MAX)
4583 if (off % size != 0)
4588 BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size)
4591 case BPF_MAP_CREATE:
4592 case BPF_MAP_UPDATE_ELEM:
4593 case BPF_MAP_FREEZE:
4597 /* case BPF_PROG_TEST_RUN:
4598 * is not part of this list to prevent recursive test_run
4603 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
4606 static const struct bpf_func_proto bpf_sys_bpf_proto = {
4607 .func = bpf_sys_bpf,
4609 .ret_type = RET_INTEGER,
4610 .arg1_type = ARG_ANYTHING,
4611 .arg2_type = ARG_PTR_TO_MEM,
4612 .arg3_type = ARG_CONST_SIZE,
4615 const struct bpf_func_proto * __weak
4616 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4618 return bpf_base_func_proto(func_id);
4621 BPF_CALL_1(bpf_sys_close, u32, fd)
4623 /* When bpf program calls this helper there should not be
4624 * an fdget() without matching completed fdput().
4625 * This helper is allowed in the following callchain only:
4626 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
4628 return close_fd(fd);
4631 static const struct bpf_func_proto bpf_sys_close_proto = {
4632 .func = bpf_sys_close,
4634 .ret_type = RET_INTEGER,
4635 .arg1_type = ARG_ANYTHING,
4638 static const struct bpf_func_proto *
4639 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4642 case BPF_FUNC_sys_bpf:
4643 return &bpf_sys_bpf_proto;
4644 case BPF_FUNC_btf_find_by_name_kind:
4645 return &bpf_btf_find_by_name_kind_proto;
4646 case BPF_FUNC_sys_close:
4647 return &bpf_sys_close_proto;
4649 return tracing_prog_func_proto(func_id, prog);
4653 const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
4654 .get_func_proto = syscall_prog_func_proto,
4655 .is_valid_access = syscall_prog_is_valid_access,
4658 const struct bpf_prog_ops bpf_syscall_prog_ops = {
4659 .test_run = bpf_prog_test_run_syscall,