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 and timer, since value wasn't zero inited */
264 check_and_init_map_value(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 spin_lock(&array->aux->owner.lock);
547 type = array->aux->owner.type;
548 jited = array->aux->owner.jited;
549 spin_unlock(&array->aux->owner.lock);
566 bpf_map_memory_footprint(map),
568 READ_ONCE(map->frozen));
570 seq_printf(m, "owner_prog_type:\t%u\n", type);
571 seq_printf(m, "owner_jited:\t%u\n", jited);
576 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
579 /* We need this handler such that alloc_file() enables
580 * f_mode with FMODE_CAN_READ.
585 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
586 size_t siz, loff_t *ppos)
588 /* We need this handler such that alloc_file() enables
589 * f_mode with FMODE_CAN_WRITE.
594 /* called for any extra memory-mapped regions (except initial) */
595 static void bpf_map_mmap_open(struct vm_area_struct *vma)
597 struct bpf_map *map = vma->vm_file->private_data;
599 if (vma->vm_flags & VM_MAYWRITE) {
600 mutex_lock(&map->freeze_mutex);
602 mutex_unlock(&map->freeze_mutex);
606 /* called for all unmapped memory region (including initial) */
607 static void bpf_map_mmap_close(struct vm_area_struct *vma)
609 struct bpf_map *map = vma->vm_file->private_data;
611 if (vma->vm_flags & VM_MAYWRITE) {
612 mutex_lock(&map->freeze_mutex);
614 mutex_unlock(&map->freeze_mutex);
618 static const struct vm_operations_struct bpf_map_default_vmops = {
619 .open = bpf_map_mmap_open,
620 .close = bpf_map_mmap_close,
623 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
625 struct bpf_map *map = filp->private_data;
628 if (!map->ops->map_mmap || map_value_has_spin_lock(map) ||
629 map_value_has_timer(map))
632 if (!(vma->vm_flags & VM_SHARED))
635 mutex_lock(&map->freeze_mutex);
637 if (vma->vm_flags & VM_WRITE) {
642 /* map is meant to be read-only, so do not allow mapping as
643 * writable, because it's possible to leak a writable page
644 * reference and allows user-space to still modify it after
645 * freezing, while verifier will assume contents do not change
647 if (map->map_flags & BPF_F_RDONLY_PROG) {
653 /* set default open/close callbacks */
654 vma->vm_ops = &bpf_map_default_vmops;
655 vma->vm_private_data = map;
656 vma->vm_flags &= ~VM_MAYEXEC;
657 if (!(vma->vm_flags & VM_WRITE))
658 /* disallow re-mapping with PROT_WRITE */
659 vma->vm_flags &= ~VM_MAYWRITE;
661 err = map->ops->map_mmap(map, vma);
665 if (vma->vm_flags & VM_MAYWRITE)
668 mutex_unlock(&map->freeze_mutex);
672 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
674 struct bpf_map *map = filp->private_data;
676 if (map->ops->map_poll)
677 return map->ops->map_poll(map, filp, pts);
682 const struct file_operations bpf_map_fops = {
683 #ifdef CONFIG_PROC_FS
684 .show_fdinfo = bpf_map_show_fdinfo,
686 .release = bpf_map_release,
687 .read = bpf_dummy_read,
688 .write = bpf_dummy_write,
689 .mmap = bpf_map_mmap,
690 .poll = bpf_map_poll,
693 int bpf_map_new_fd(struct bpf_map *map, int flags)
697 ret = security_bpf_map(map, OPEN_FMODE(flags));
701 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
705 int bpf_get_file_flag(int flags)
707 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
709 if (flags & BPF_F_RDONLY)
711 if (flags & BPF_F_WRONLY)
716 /* helper macro to check that unused fields 'union bpf_attr' are zero */
717 #define CHECK_ATTR(CMD) \
718 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
719 sizeof(attr->CMD##_LAST_FIELD), 0, \
721 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
722 sizeof(attr->CMD##_LAST_FIELD)) != NULL
724 /* dst and src must have at least "size" number of bytes.
725 * Return strlen on success and < 0 on error.
727 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
729 const char *end = src + size;
730 const char *orig_src = src;
732 memset(dst, 0, size);
733 /* Copy all isalnum(), '_' and '.' chars. */
734 while (src < end && *src) {
735 if (!isalnum(*src) &&
736 *src != '_' && *src != '.')
741 /* No '\0' found in "size" number of bytes */
745 return src - orig_src;
748 int map_check_no_btf(const struct bpf_map *map,
749 const struct btf *btf,
750 const struct btf_type *key_type,
751 const struct btf_type *value_type)
756 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
757 u32 btf_key_id, u32 btf_value_id)
759 const struct btf_type *key_type, *value_type;
760 u32 key_size, value_size;
763 /* Some maps allow key to be unspecified. */
765 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
766 if (!key_type || key_size != map->key_size)
769 key_type = btf_type_by_id(btf, 0);
770 if (!map->ops->map_check_btf)
774 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
775 if (!value_type || value_size != map->value_size)
778 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
780 if (map_value_has_spin_lock(map)) {
781 if (map->map_flags & BPF_F_RDONLY_PROG)
783 if (map->map_type != BPF_MAP_TYPE_HASH &&
784 map->map_type != BPF_MAP_TYPE_ARRAY &&
785 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
786 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
787 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
788 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
790 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
793 "verifier bug spin_lock_off %d value_size %d\n",
794 map->spin_lock_off, map->value_size);
799 map->timer_off = btf_find_timer(btf, value_type);
800 if (map_value_has_timer(map)) {
801 if (map->map_flags & BPF_F_RDONLY_PROG)
803 if (map->map_type != BPF_MAP_TYPE_HASH &&
804 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
805 map->map_type != BPF_MAP_TYPE_ARRAY)
809 if (map->ops->map_check_btf)
810 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
815 #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
816 /* called via syscall */
817 static int map_create(union bpf_attr *attr)
819 int numa_node = bpf_map_attr_numa_node(attr);
824 err = CHECK_ATTR(BPF_MAP_CREATE);
828 if (attr->btf_vmlinux_value_type_id) {
829 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
830 attr->btf_key_type_id || attr->btf_value_type_id)
832 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
836 f_flags = bpf_get_file_flag(attr->map_flags);
840 if (numa_node != NUMA_NO_NODE &&
841 ((unsigned int)numa_node >= nr_node_ids ||
842 !node_online(numa_node)))
845 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
846 map = find_and_alloc_map(attr);
850 err = bpf_obj_name_cpy(map->name, attr->map_name,
851 sizeof(attr->map_name));
855 atomic64_set(&map->refcnt, 1);
856 atomic64_set(&map->usercnt, 1);
857 mutex_init(&map->freeze_mutex);
859 map->spin_lock_off = -EINVAL;
860 map->timer_off = -EINVAL;
861 if (attr->btf_key_type_id || attr->btf_value_type_id ||
862 /* Even the map's value is a kernel's struct,
863 * the bpf_prog.o must have BTF to begin with
864 * to figure out the corresponding kernel's
865 * counter part. Thus, attr->btf_fd has
868 attr->btf_vmlinux_value_type_id) {
871 btf = btf_get_by_fd(attr->btf_fd);
876 if (btf_is_kernel(btf)) {
883 if (attr->btf_value_type_id) {
884 err = map_check_btf(map, btf, attr->btf_key_type_id,
885 attr->btf_value_type_id);
890 map->btf_key_type_id = attr->btf_key_type_id;
891 map->btf_value_type_id = attr->btf_value_type_id;
892 map->btf_vmlinux_value_type_id =
893 attr->btf_vmlinux_value_type_id;
896 err = security_bpf_map_alloc(map);
900 err = bpf_map_alloc_id(map);
904 bpf_map_save_memcg(map);
906 err = bpf_map_new_fd(map, f_flags);
908 /* failed to allocate fd.
909 * bpf_map_put_with_uref() is needed because the above
910 * bpf_map_alloc_id() has published the map
911 * to the userspace and the userspace may
912 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
914 bpf_map_put_with_uref(map);
921 security_bpf_map_free(map);
924 map->ops->map_free(map);
928 /* if error is returned, fd is released.
929 * On success caller should complete fd access with matching fdput()
931 struct bpf_map *__bpf_map_get(struct fd f)
934 return ERR_PTR(-EBADF);
935 if (f.file->f_op != &bpf_map_fops) {
937 return ERR_PTR(-EINVAL);
940 return f.file->private_data;
943 void bpf_map_inc(struct bpf_map *map)
945 atomic64_inc(&map->refcnt);
947 EXPORT_SYMBOL_GPL(bpf_map_inc);
949 void bpf_map_inc_with_uref(struct bpf_map *map)
951 atomic64_inc(&map->refcnt);
952 atomic64_inc(&map->usercnt);
954 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
956 struct bpf_map *bpf_map_get(u32 ufd)
958 struct fd f = fdget(ufd);
961 map = __bpf_map_get(f);
971 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
973 struct fd f = fdget(ufd);
976 map = __bpf_map_get(f);
980 bpf_map_inc_with_uref(map);
986 /* map_idr_lock should have been held */
987 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
991 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
993 return ERR_PTR(-ENOENT);
995 atomic64_inc(&map->usercnt);
1000 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
1002 spin_lock_bh(&map_idr_lock);
1003 map = __bpf_map_inc_not_zero(map, false);
1004 spin_unlock_bh(&map_idr_lock);
1008 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
1010 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1015 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1018 return vmemdup_user(ukey, key_size);
1021 return ERR_PTR(-EINVAL);
1026 static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1029 return kvmemdup_bpfptr(ukey, key_size);
1031 if (!bpfptr_is_null(ukey))
1032 return ERR_PTR(-EINVAL);
1037 /* last field in 'union bpf_attr' used by this command */
1038 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1040 static int map_lookup_elem(union bpf_attr *attr)
1042 void __user *ukey = u64_to_user_ptr(attr->key);
1043 void __user *uvalue = u64_to_user_ptr(attr->value);
1044 int ufd = attr->map_fd;
1045 struct bpf_map *map;
1051 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1054 if (attr->flags & ~BPF_F_LOCK)
1058 map = __bpf_map_get(f);
1060 return PTR_ERR(map);
1061 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1066 if ((attr->flags & BPF_F_LOCK) &&
1067 !map_value_has_spin_lock(map)) {
1072 key = __bpf_copy_key(ukey, map->key_size);
1078 value_size = bpf_map_value_size(map);
1081 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1085 err = bpf_map_copy_value(map, key, value, attr->flags);
1090 if (copy_to_user(uvalue, value, value_size) != 0)
1105 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1107 static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
1109 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1110 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
1111 int ufd = attr->map_fd;
1112 struct bpf_map *map;
1118 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1122 map = __bpf_map_get(f);
1124 return PTR_ERR(map);
1125 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1130 if ((attr->flags & BPF_F_LOCK) &&
1131 !map_value_has_spin_lock(map)) {
1136 key = ___bpf_copy_key(ukey, map->key_size);
1142 value_size = bpf_map_value_size(map);
1145 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1150 if (copy_from_bpfptr(value, uvalue, value_size) != 0)
1153 err = bpf_map_update_value(map, f, key, value, attr->flags);
1164 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1166 static int map_delete_elem(union bpf_attr *attr)
1168 void __user *ukey = u64_to_user_ptr(attr->key);
1169 int ufd = attr->map_fd;
1170 struct bpf_map *map;
1175 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1179 map = __bpf_map_get(f);
1181 return PTR_ERR(map);
1182 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1187 key = __bpf_copy_key(ukey, map->key_size);
1193 if (bpf_map_is_dev_bound(map)) {
1194 err = bpf_map_offload_delete_elem(map, key);
1196 } else if (IS_FD_PROG_ARRAY(map) ||
1197 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1198 /* These maps require sleepable context */
1199 err = map->ops->map_delete_elem(map, key);
1203 bpf_disable_instrumentation();
1205 err = map->ops->map_delete_elem(map, key);
1207 bpf_enable_instrumentation();
1208 maybe_wait_bpf_programs(map);
1216 /* last field in 'union bpf_attr' used by this command */
1217 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1219 static int map_get_next_key(union bpf_attr *attr)
1221 void __user *ukey = u64_to_user_ptr(attr->key);
1222 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1223 int ufd = attr->map_fd;
1224 struct bpf_map *map;
1225 void *key, *next_key;
1229 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1233 map = __bpf_map_get(f);
1235 return PTR_ERR(map);
1236 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1242 key = __bpf_copy_key(ukey, map->key_size);
1252 next_key = kvmalloc(map->key_size, GFP_USER);
1256 if (bpf_map_is_dev_bound(map)) {
1257 err = bpf_map_offload_get_next_key(map, key, next_key);
1262 err = map->ops->map_get_next_key(map, key, next_key);
1269 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1283 int generic_map_delete_batch(struct bpf_map *map,
1284 const union bpf_attr *attr,
1285 union bpf_attr __user *uattr)
1287 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1292 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1295 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1296 !map_value_has_spin_lock(map)) {
1300 max_count = attr->batch.count;
1304 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1308 for (cp = 0; cp < max_count; cp++) {
1310 if (copy_from_user(key, keys + cp * map->key_size,
1314 if (bpf_map_is_dev_bound(map)) {
1315 err = bpf_map_offload_delete_elem(map, key);
1319 bpf_disable_instrumentation();
1321 err = map->ops->map_delete_elem(map, key);
1323 bpf_enable_instrumentation();
1324 maybe_wait_bpf_programs(map);
1328 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1335 int generic_map_update_batch(struct bpf_map *map,
1336 const union bpf_attr *attr,
1337 union bpf_attr __user *uattr)
1339 void __user *values = u64_to_user_ptr(attr->batch.values);
1340 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1341 u32 value_size, cp, max_count;
1342 int ufd = attr->batch.map_fd;
1347 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1350 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1351 !map_value_has_spin_lock(map)) {
1355 value_size = bpf_map_value_size(map);
1357 max_count = attr->batch.count;
1361 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1365 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1371 f = fdget(ufd); /* bpf_map_do_batch() guarantees ufd is valid */
1372 for (cp = 0; cp < max_count; cp++) {
1374 if (copy_from_user(key, keys + cp * map->key_size,
1376 copy_from_user(value, values + cp * value_size, value_size))
1379 err = bpf_map_update_value(map, f, key, value,
1380 attr->batch.elem_flags);
1386 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1395 #define MAP_LOOKUP_RETRIES 3
1397 int generic_map_lookup_batch(struct bpf_map *map,
1398 const union bpf_attr *attr,
1399 union bpf_attr __user *uattr)
1401 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1402 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1403 void __user *values = u64_to_user_ptr(attr->batch.values);
1404 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1405 void *buf, *buf_prevkey, *prev_key, *key, *value;
1406 int err, retry = MAP_LOOKUP_RETRIES;
1407 u32 value_size, cp, max_count;
1409 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1412 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1413 !map_value_has_spin_lock(map))
1416 value_size = bpf_map_value_size(map);
1418 max_count = attr->batch.count;
1422 if (put_user(0, &uattr->batch.count))
1425 buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1429 buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1431 kvfree(buf_prevkey);
1437 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1440 value = key + map->key_size;
1442 prev_key = buf_prevkey;
1444 for (cp = 0; cp < max_count;) {
1446 err = map->ops->map_get_next_key(map, prev_key, key);
1450 err = bpf_map_copy_value(map, key, value,
1451 attr->batch.elem_flags);
1453 if (err == -ENOENT) {
1465 if (copy_to_user(keys + cp * map->key_size, key,
1470 if (copy_to_user(values + cp * value_size, value, value_size)) {
1476 prev_key = buf_prevkey;
1478 swap(prev_key, key);
1479 retry = MAP_LOOKUP_RETRIES;
1486 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1487 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1491 kvfree(buf_prevkey);
1496 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
1498 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1500 void __user *ukey = u64_to_user_ptr(attr->key);
1501 void __user *uvalue = u64_to_user_ptr(attr->value);
1502 int ufd = attr->map_fd;
1503 struct bpf_map *map;
1509 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1512 if (attr->flags & ~BPF_F_LOCK)
1516 map = __bpf_map_get(f);
1518 return PTR_ERR(map);
1519 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1520 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1526 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1527 map->map_type == BPF_MAP_TYPE_STACK)) {
1532 if ((attr->flags & BPF_F_LOCK) &&
1533 !map_value_has_spin_lock(map)) {
1538 key = __bpf_copy_key(ukey, map->key_size);
1544 value_size = bpf_map_value_size(map);
1547 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1552 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1553 map->map_type == BPF_MAP_TYPE_STACK) {
1554 err = map->ops->map_pop_elem(map, value);
1555 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1556 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1557 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1558 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1559 if (!bpf_map_is_dev_bound(map)) {
1560 bpf_disable_instrumentation();
1562 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1564 bpf_enable_instrumentation();
1571 if (copy_to_user(uvalue, value, value_size) != 0) {
1587 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1589 static int map_freeze(const union bpf_attr *attr)
1591 int err = 0, ufd = attr->map_fd;
1592 struct bpf_map *map;
1595 if (CHECK_ATTR(BPF_MAP_FREEZE))
1599 map = __bpf_map_get(f);
1601 return PTR_ERR(map);
1603 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS ||
1604 map_value_has_timer(map)) {
1609 mutex_lock(&map->freeze_mutex);
1611 if (map->writecnt) {
1615 if (READ_ONCE(map->frozen)) {
1619 if (!bpf_capable()) {
1624 WRITE_ONCE(map->frozen, true);
1626 mutex_unlock(&map->freeze_mutex);
1631 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1632 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1633 [_id] = & _name ## _prog_ops,
1634 #define BPF_MAP_TYPE(_id, _ops)
1635 #define BPF_LINK_TYPE(_id, _name)
1636 #include <linux/bpf_types.h>
1637 #undef BPF_PROG_TYPE
1639 #undef BPF_LINK_TYPE
1642 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1644 const struct bpf_prog_ops *ops;
1646 if (type >= ARRAY_SIZE(bpf_prog_types))
1648 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1649 ops = bpf_prog_types[type];
1653 if (!bpf_prog_is_dev_bound(prog->aux))
1654 prog->aux->ops = ops;
1656 prog->aux->ops = &bpf_offload_prog_ops;
1667 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1668 [BPF_AUDIT_LOAD] = "LOAD",
1669 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1672 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1674 struct audit_context *ctx = NULL;
1675 struct audit_buffer *ab;
1677 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1679 if (audit_enabled == AUDIT_OFF)
1681 if (op == BPF_AUDIT_LOAD)
1682 ctx = audit_context();
1683 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1686 audit_log_format(ab, "prog-id=%u op=%s",
1687 prog->aux->id, bpf_audit_str[op]);
1691 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1695 idr_preload(GFP_KERNEL);
1696 spin_lock_bh(&prog_idr_lock);
1697 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1700 spin_unlock_bh(&prog_idr_lock);
1703 /* id is in [1, INT_MAX) */
1704 if (WARN_ON_ONCE(!id))
1707 return id > 0 ? 0 : id;
1710 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1712 unsigned long flags;
1714 /* cBPF to eBPF migrations are currently not in the idr store.
1715 * Offloaded programs are removed from the store when their device
1716 * disappears - even if someone grabs an fd to them they are unusable,
1717 * simply waiting for refcnt to drop to be freed.
1723 spin_lock_irqsave(&prog_idr_lock, flags);
1725 __acquire(&prog_idr_lock);
1727 idr_remove(&prog_idr, prog->aux->id);
1731 spin_unlock_irqrestore(&prog_idr_lock, flags);
1733 __release(&prog_idr_lock);
1736 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1738 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1740 kvfree(aux->func_info);
1741 kfree(aux->func_info_aux);
1742 free_uid(aux->user);
1743 security_bpf_prog_free(aux);
1744 bpf_prog_free(aux->prog);
1747 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1749 bpf_prog_kallsyms_del_all(prog);
1750 btf_put(prog->aux->btf);
1751 kvfree(prog->aux->jited_linfo);
1752 kvfree(prog->aux->linfo);
1753 kfree(prog->aux->kfunc_tab);
1754 if (prog->aux->attach_btf)
1755 btf_put(prog->aux->attach_btf);
1758 if (prog->aux->sleepable)
1759 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1761 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1763 __bpf_prog_put_rcu(&prog->aux->rcu);
1767 static void bpf_prog_put_deferred(struct work_struct *work)
1769 struct bpf_prog_aux *aux;
1770 struct bpf_prog *prog;
1772 aux = container_of(work, struct bpf_prog_aux, work);
1774 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1775 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1776 __bpf_prog_put_noref(prog, true);
1779 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1781 struct bpf_prog_aux *aux = prog->aux;
1783 if (atomic64_dec_and_test(&aux->refcnt)) {
1784 /* bpf_prog_free_id() must be called first */
1785 bpf_prog_free_id(prog, do_idr_lock);
1787 if (in_irq() || irqs_disabled()) {
1788 INIT_WORK(&aux->work, bpf_prog_put_deferred);
1789 schedule_work(&aux->work);
1791 bpf_prog_put_deferred(&aux->work);
1796 void bpf_prog_put(struct bpf_prog *prog)
1798 __bpf_prog_put(prog, true);
1800 EXPORT_SYMBOL_GPL(bpf_prog_put);
1802 static int bpf_prog_release(struct inode *inode, struct file *filp)
1804 struct bpf_prog *prog = filp->private_data;
1810 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1811 struct bpf_prog_stats *stats)
1813 u64 nsecs = 0, cnt = 0, misses = 0;
1816 for_each_possible_cpu(cpu) {
1817 const struct bpf_prog_stats *st;
1819 u64 tnsecs, tcnt, tmisses;
1821 st = per_cpu_ptr(prog->stats, cpu);
1823 start = u64_stats_fetch_begin_irq(&st->syncp);
1826 tmisses = st->misses;
1827 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1832 stats->nsecs = nsecs;
1834 stats->misses = misses;
1837 #ifdef CONFIG_PROC_FS
1838 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1840 const struct bpf_prog *prog = filp->private_data;
1841 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1842 struct bpf_prog_stats stats;
1844 bpf_prog_get_stats(prog, &stats);
1845 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1852 "run_time_ns:\t%llu\n"
1854 "recursion_misses:\t%llu\n",
1858 prog->pages * 1ULL << PAGE_SHIFT,
1866 const struct file_operations bpf_prog_fops = {
1867 #ifdef CONFIG_PROC_FS
1868 .show_fdinfo = bpf_prog_show_fdinfo,
1870 .release = bpf_prog_release,
1871 .read = bpf_dummy_read,
1872 .write = bpf_dummy_write,
1875 int bpf_prog_new_fd(struct bpf_prog *prog)
1879 ret = security_bpf_prog(prog);
1883 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1884 O_RDWR | O_CLOEXEC);
1887 static struct bpf_prog *____bpf_prog_get(struct fd f)
1890 return ERR_PTR(-EBADF);
1891 if (f.file->f_op != &bpf_prog_fops) {
1893 return ERR_PTR(-EINVAL);
1896 return f.file->private_data;
1899 void bpf_prog_add(struct bpf_prog *prog, int i)
1901 atomic64_add(i, &prog->aux->refcnt);
1903 EXPORT_SYMBOL_GPL(bpf_prog_add);
1905 void bpf_prog_sub(struct bpf_prog *prog, int i)
1907 /* Only to be used for undoing previous bpf_prog_add() in some
1908 * error path. We still know that another entity in our call
1909 * path holds a reference to the program, thus atomic_sub() can
1910 * be safely used in such cases!
1912 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1914 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1916 void bpf_prog_inc(struct bpf_prog *prog)
1918 atomic64_inc(&prog->aux->refcnt);
1920 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1922 /* prog_idr_lock should have been held */
1923 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1927 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1930 return ERR_PTR(-ENOENT);
1934 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1936 bool bpf_prog_get_ok(struct bpf_prog *prog,
1937 enum bpf_prog_type *attach_type, bool attach_drv)
1939 /* not an attachment, just a refcount inc, always allow */
1943 if (prog->type != *attach_type)
1945 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1951 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1954 struct fd f = fdget(ufd);
1955 struct bpf_prog *prog;
1957 prog = ____bpf_prog_get(f);
1960 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1961 prog = ERR_PTR(-EINVAL);
1971 struct bpf_prog *bpf_prog_get(u32 ufd)
1973 return __bpf_prog_get(ufd, NULL, false);
1976 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1979 return __bpf_prog_get(ufd, &type, attach_drv);
1981 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1983 /* Initially all BPF programs could be loaded w/o specifying
1984 * expected_attach_type. Later for some of them specifying expected_attach_type
1985 * at load time became required so that program could be validated properly.
1986 * Programs of types that are allowed to be loaded both w/ and w/o (for
1987 * backward compatibility) expected_attach_type, should have the default attach
1988 * type assigned to expected_attach_type for the latter case, so that it can be
1989 * validated later at attach time.
1991 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1992 * prog type requires it but has some attach types that have to be backward
1995 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1997 switch (attr->prog_type) {
1998 case BPF_PROG_TYPE_CGROUP_SOCK:
1999 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
2000 * exist so checking for non-zero is the way to go here.
2002 if (!attr->expected_attach_type)
2003 attr->expected_attach_type =
2004 BPF_CGROUP_INET_SOCK_CREATE;
2006 case BPF_PROG_TYPE_SK_REUSEPORT:
2007 if (!attr->expected_attach_type)
2008 attr->expected_attach_type =
2009 BPF_SK_REUSEPORT_SELECT;
2015 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2016 enum bpf_attach_type expected_attach_type,
2017 struct btf *attach_btf, u32 btf_id,
2018 struct bpf_prog *dst_prog)
2021 if (btf_id > BTF_MAX_TYPE)
2024 if (!attach_btf && !dst_prog)
2027 switch (prog_type) {
2028 case BPF_PROG_TYPE_TRACING:
2029 case BPF_PROG_TYPE_LSM:
2030 case BPF_PROG_TYPE_STRUCT_OPS:
2031 case BPF_PROG_TYPE_EXT:
2038 if (attach_btf && (!btf_id || dst_prog))
2041 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
2042 prog_type != BPF_PROG_TYPE_EXT)
2045 switch (prog_type) {
2046 case BPF_PROG_TYPE_CGROUP_SOCK:
2047 switch (expected_attach_type) {
2048 case BPF_CGROUP_INET_SOCK_CREATE:
2049 case BPF_CGROUP_INET_SOCK_RELEASE:
2050 case BPF_CGROUP_INET4_POST_BIND:
2051 case BPF_CGROUP_INET6_POST_BIND:
2056 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2057 switch (expected_attach_type) {
2058 case BPF_CGROUP_INET4_BIND:
2059 case BPF_CGROUP_INET6_BIND:
2060 case BPF_CGROUP_INET4_CONNECT:
2061 case BPF_CGROUP_INET6_CONNECT:
2062 case BPF_CGROUP_INET4_GETPEERNAME:
2063 case BPF_CGROUP_INET6_GETPEERNAME:
2064 case BPF_CGROUP_INET4_GETSOCKNAME:
2065 case BPF_CGROUP_INET6_GETSOCKNAME:
2066 case BPF_CGROUP_UDP4_SENDMSG:
2067 case BPF_CGROUP_UDP6_SENDMSG:
2068 case BPF_CGROUP_UDP4_RECVMSG:
2069 case BPF_CGROUP_UDP6_RECVMSG:
2074 case BPF_PROG_TYPE_CGROUP_SKB:
2075 switch (expected_attach_type) {
2076 case BPF_CGROUP_INET_INGRESS:
2077 case BPF_CGROUP_INET_EGRESS:
2082 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2083 switch (expected_attach_type) {
2084 case BPF_CGROUP_SETSOCKOPT:
2085 case BPF_CGROUP_GETSOCKOPT:
2090 case BPF_PROG_TYPE_SK_LOOKUP:
2091 if (expected_attach_type == BPF_SK_LOOKUP)
2094 case BPF_PROG_TYPE_SK_REUSEPORT:
2095 switch (expected_attach_type) {
2096 case BPF_SK_REUSEPORT_SELECT:
2097 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2102 case BPF_PROG_TYPE_SYSCALL:
2103 case BPF_PROG_TYPE_EXT:
2104 if (expected_attach_type)
2112 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2114 switch (prog_type) {
2115 case BPF_PROG_TYPE_SCHED_CLS:
2116 case BPF_PROG_TYPE_SCHED_ACT:
2117 case BPF_PROG_TYPE_XDP:
2118 case BPF_PROG_TYPE_LWT_IN:
2119 case BPF_PROG_TYPE_LWT_OUT:
2120 case BPF_PROG_TYPE_LWT_XMIT:
2121 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2122 case BPF_PROG_TYPE_SK_SKB:
2123 case BPF_PROG_TYPE_SK_MSG:
2124 case BPF_PROG_TYPE_LIRC_MODE2:
2125 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2126 case BPF_PROG_TYPE_CGROUP_DEVICE:
2127 case BPF_PROG_TYPE_CGROUP_SOCK:
2128 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2129 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2130 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2131 case BPF_PROG_TYPE_SOCK_OPS:
2132 case BPF_PROG_TYPE_EXT: /* extends any prog */
2134 case BPF_PROG_TYPE_CGROUP_SKB:
2136 case BPF_PROG_TYPE_SK_REUSEPORT:
2137 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2143 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2145 switch (prog_type) {
2146 case BPF_PROG_TYPE_KPROBE:
2147 case BPF_PROG_TYPE_TRACEPOINT:
2148 case BPF_PROG_TYPE_PERF_EVENT:
2149 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2150 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2151 case BPF_PROG_TYPE_TRACING:
2152 case BPF_PROG_TYPE_LSM:
2153 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2154 case BPF_PROG_TYPE_EXT: /* extends any prog */
2161 /* last field in 'union bpf_attr' used by this command */
2162 #define BPF_PROG_LOAD_LAST_FIELD fd_array
2164 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
2166 enum bpf_prog_type type = attr->prog_type;
2167 struct bpf_prog *prog, *dst_prog = NULL;
2168 struct btf *attach_btf = NULL;
2173 if (CHECK_ATTR(BPF_PROG_LOAD))
2176 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2177 BPF_F_ANY_ALIGNMENT |
2178 BPF_F_TEST_STATE_FREQ |
2180 BPF_F_TEST_RND_HI32))
2183 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2184 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2188 /* copy eBPF program license from user space */
2189 if (strncpy_from_bpfptr(license,
2190 make_bpfptr(attr->license, uattr.is_kernel),
2191 sizeof(license) - 1) < 0)
2193 license[sizeof(license) - 1] = 0;
2195 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2196 is_gpl = license_is_gpl_compatible(license);
2198 if (attr->insn_cnt == 0 ||
2199 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2201 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2202 type != BPF_PROG_TYPE_CGROUP_SKB &&
2206 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2208 if (is_perfmon_prog_type(type) && !perfmon_capable())
2211 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2212 * or btf, we need to check which one it is
2214 if (attr->attach_prog_fd) {
2215 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2216 if (IS_ERR(dst_prog)) {
2218 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2219 if (IS_ERR(attach_btf))
2221 if (!btf_is_kernel(attach_btf)) {
2222 /* attaching through specifying bpf_prog's BTF
2223 * objects directly might be supported eventually
2225 btf_put(attach_btf);
2229 } else if (attr->attach_btf_id) {
2230 /* fall back to vmlinux BTF, if BTF type ID is specified */
2231 attach_btf = bpf_get_btf_vmlinux();
2232 if (IS_ERR(attach_btf))
2233 return PTR_ERR(attach_btf);
2236 btf_get(attach_btf);
2239 bpf_prog_load_fixup_attach_type(attr);
2240 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2241 attach_btf, attr->attach_btf_id,
2244 bpf_prog_put(dst_prog);
2246 btf_put(attach_btf);
2250 /* plain bpf_prog allocation */
2251 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2254 bpf_prog_put(dst_prog);
2256 btf_put(attach_btf);
2260 prog->expected_attach_type = attr->expected_attach_type;
2261 prog->aux->attach_btf = attach_btf;
2262 prog->aux->attach_btf_id = attr->attach_btf_id;
2263 prog->aux->dst_prog = dst_prog;
2264 prog->aux->offload_requested = !!attr->prog_ifindex;
2265 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2267 err = security_bpf_prog_alloc(prog->aux);
2271 prog->aux->user = get_current_user();
2272 prog->len = attr->insn_cnt;
2275 if (copy_from_bpfptr(prog->insns,
2276 make_bpfptr(attr->insns, uattr.is_kernel),
2277 bpf_prog_insn_size(prog)) != 0)
2280 prog->orig_prog = NULL;
2283 atomic64_set(&prog->aux->refcnt, 1);
2284 prog->gpl_compatible = is_gpl ? 1 : 0;
2286 if (bpf_prog_is_dev_bound(prog->aux)) {
2287 err = bpf_prog_offload_init(prog, attr);
2292 /* find program type: socket_filter vs tracing_filter */
2293 err = find_prog_type(type, prog);
2297 prog->aux->load_time = ktime_get_boottime_ns();
2298 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2299 sizeof(attr->prog_name));
2303 /* run eBPF verifier */
2304 err = bpf_check(&prog, attr, uattr);
2306 goto free_used_maps;
2308 prog = bpf_prog_select_runtime(prog, &err);
2310 goto free_used_maps;
2312 err = bpf_prog_alloc_id(prog);
2314 goto free_used_maps;
2316 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2317 * effectively publicly exposed. However, retrieving via
2318 * bpf_prog_get_fd_by_id() will take another reference,
2319 * therefore it cannot be gone underneath us.
2321 * Only for the time /after/ successful bpf_prog_new_fd()
2322 * and before returning to userspace, we might just hold
2323 * one reference and any parallel close on that fd could
2324 * rip everything out. Hence, below notifications must
2325 * happen before bpf_prog_new_fd().
2327 * Also, any failure handling from this point onwards must
2328 * be using bpf_prog_put() given the program is exposed.
2330 bpf_prog_kallsyms_add(prog);
2331 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2332 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2334 err = bpf_prog_new_fd(prog);
2340 /* In case we have subprogs, we need to wait for a grace
2341 * period before we can tear down JIT memory since symbols
2342 * are already exposed under kallsyms.
2344 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2347 free_uid(prog->aux->user);
2348 security_bpf_prog_free(prog->aux);
2350 if (prog->aux->attach_btf)
2351 btf_put(prog->aux->attach_btf);
2352 bpf_prog_free(prog);
2356 #define BPF_OBJ_LAST_FIELD file_flags
2358 static int bpf_obj_pin(const union bpf_attr *attr)
2360 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2363 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2366 static int bpf_obj_get(const union bpf_attr *attr)
2368 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2369 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2372 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2376 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2377 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2379 atomic64_set(&link->refcnt, 1);
2386 static void bpf_link_free_id(int id)
2391 spin_lock_bh(&link_idr_lock);
2392 idr_remove(&link_idr, id);
2393 spin_unlock_bh(&link_idr_lock);
2396 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2397 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2398 * anon_inode's release() call. This helper marksbpf_link as
2399 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2400 * is not decremented, it's the responsibility of a calling code that failed
2401 * to complete bpf_link initialization.
2403 void bpf_link_cleanup(struct bpf_link_primer *primer)
2405 primer->link->prog = NULL;
2406 bpf_link_free_id(primer->id);
2408 put_unused_fd(primer->fd);
2411 void bpf_link_inc(struct bpf_link *link)
2413 atomic64_inc(&link->refcnt);
2416 /* bpf_link_free is guaranteed to be called from process context */
2417 static void bpf_link_free(struct bpf_link *link)
2419 bpf_link_free_id(link->id);
2421 /* detach BPF program, clean up used resources */
2422 link->ops->release(link);
2423 bpf_prog_put(link->prog);
2425 /* free bpf_link and its containing memory */
2426 link->ops->dealloc(link);
2429 static void bpf_link_put_deferred(struct work_struct *work)
2431 struct bpf_link *link = container_of(work, struct bpf_link, work);
2433 bpf_link_free(link);
2436 /* bpf_link_put can be called from atomic context, but ensures that resources
2437 * are freed from process context
2439 void bpf_link_put(struct bpf_link *link)
2441 if (!atomic64_dec_and_test(&link->refcnt))
2445 INIT_WORK(&link->work, bpf_link_put_deferred);
2446 schedule_work(&link->work);
2448 bpf_link_free(link);
2452 static int bpf_link_release(struct inode *inode, struct file *filp)
2454 struct bpf_link *link = filp->private_data;
2460 #ifdef CONFIG_PROC_FS
2461 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2462 #define BPF_MAP_TYPE(_id, _ops)
2463 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2464 static const char *bpf_link_type_strs[] = {
2465 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2466 #include <linux/bpf_types.h>
2468 #undef BPF_PROG_TYPE
2470 #undef BPF_LINK_TYPE
2472 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2474 const struct bpf_link *link = filp->private_data;
2475 const struct bpf_prog *prog = link->prog;
2476 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2478 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2484 bpf_link_type_strs[link->type],
2488 if (link->ops->show_fdinfo)
2489 link->ops->show_fdinfo(link, m);
2493 static const struct file_operations bpf_link_fops = {
2494 #ifdef CONFIG_PROC_FS
2495 .show_fdinfo = bpf_link_show_fdinfo,
2497 .release = bpf_link_release,
2498 .read = bpf_dummy_read,
2499 .write = bpf_dummy_write,
2502 static int bpf_link_alloc_id(struct bpf_link *link)
2506 idr_preload(GFP_KERNEL);
2507 spin_lock_bh(&link_idr_lock);
2508 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2509 spin_unlock_bh(&link_idr_lock);
2515 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2516 * reserving unused FD and allocating ID from link_idr. This is to be paired
2517 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2518 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2519 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2520 * transient state is passed around in struct bpf_link_primer.
2521 * This is preferred way to create and initialize bpf_link, especially when
2522 * there are complicated and expensive operations inbetween creating bpf_link
2523 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2524 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2525 * expensive (and potentially failing) roll back operations in a rare case
2526 * that file, FD, or ID can't be allocated.
2528 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2533 fd = get_unused_fd_flags(O_CLOEXEC);
2538 id = bpf_link_alloc_id(link);
2544 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2546 bpf_link_free_id(id);
2548 return PTR_ERR(file);
2551 primer->link = link;
2552 primer->file = file;
2558 int bpf_link_settle(struct bpf_link_primer *primer)
2560 /* make bpf_link fetchable by ID */
2561 spin_lock_bh(&link_idr_lock);
2562 primer->link->id = primer->id;
2563 spin_unlock_bh(&link_idr_lock);
2564 /* make bpf_link fetchable by FD */
2565 fd_install(primer->fd, primer->file);
2566 /* pass through installed FD */
2570 int bpf_link_new_fd(struct bpf_link *link)
2572 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2575 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2577 struct fd f = fdget(ufd);
2578 struct bpf_link *link;
2581 return ERR_PTR(-EBADF);
2582 if (f.file->f_op != &bpf_link_fops) {
2584 return ERR_PTR(-EINVAL);
2587 link = f.file->private_data;
2594 struct bpf_tracing_link {
2595 struct bpf_link link;
2596 enum bpf_attach_type attach_type;
2597 struct bpf_trampoline *trampoline;
2598 struct bpf_prog *tgt_prog;
2601 static void bpf_tracing_link_release(struct bpf_link *link)
2603 struct bpf_tracing_link *tr_link =
2604 container_of(link, struct bpf_tracing_link, link);
2606 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2607 tr_link->trampoline));
2609 bpf_trampoline_put(tr_link->trampoline);
2611 /* tgt_prog is NULL if target is a kernel function */
2612 if (tr_link->tgt_prog)
2613 bpf_prog_put(tr_link->tgt_prog);
2616 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2618 struct bpf_tracing_link *tr_link =
2619 container_of(link, struct bpf_tracing_link, link);
2624 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2625 struct seq_file *seq)
2627 struct bpf_tracing_link *tr_link =
2628 container_of(link, struct bpf_tracing_link, link);
2631 "attach_type:\t%d\n",
2632 tr_link->attach_type);
2635 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2636 struct bpf_link_info *info)
2638 struct bpf_tracing_link *tr_link =
2639 container_of(link, struct bpf_tracing_link, link);
2641 info->tracing.attach_type = tr_link->attach_type;
2642 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2643 &info->tracing.target_obj_id,
2644 &info->tracing.target_btf_id);
2649 static const struct bpf_link_ops bpf_tracing_link_lops = {
2650 .release = bpf_tracing_link_release,
2651 .dealloc = bpf_tracing_link_dealloc,
2652 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2653 .fill_link_info = bpf_tracing_link_fill_link_info,
2656 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2660 struct bpf_link_primer link_primer;
2661 struct bpf_prog *tgt_prog = NULL;
2662 struct bpf_trampoline *tr = NULL;
2663 struct bpf_tracing_link *link;
2667 switch (prog->type) {
2668 case BPF_PROG_TYPE_TRACING:
2669 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2670 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2671 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2676 case BPF_PROG_TYPE_EXT:
2677 if (prog->expected_attach_type != 0) {
2682 case BPF_PROG_TYPE_LSM:
2683 if (prog->expected_attach_type != BPF_LSM_MAC) {
2693 if (!!tgt_prog_fd != !!btf_id) {
2699 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2700 if (prog->type != BPF_PROG_TYPE_EXT) {
2705 tgt_prog = bpf_prog_get(tgt_prog_fd);
2706 if (IS_ERR(tgt_prog)) {
2707 err = PTR_ERR(tgt_prog);
2712 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
2715 link = kzalloc(sizeof(*link), GFP_USER);
2720 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2721 &bpf_tracing_link_lops, prog);
2722 link->attach_type = prog->expected_attach_type;
2724 mutex_lock(&prog->aux->dst_mutex);
2726 /* There are a few possible cases here:
2728 * - if prog->aux->dst_trampoline is set, the program was just loaded
2729 * and not yet attached to anything, so we can use the values stored
2732 * - if prog->aux->dst_trampoline is NULL, the program has already been
2733 * attached to a target and its initial target was cleared (below)
2735 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2736 * target_btf_id using the link_create API.
2738 * - if tgt_prog == NULL when this function was called using the old
2739 * raw_tracepoint_open API, and we need a target from prog->aux
2741 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
2742 * was detached and is going for re-attachment.
2744 if (!prog->aux->dst_trampoline && !tgt_prog) {
2746 * Allow re-attach for TRACING and LSM programs. If it's
2747 * currently linked, bpf_trampoline_link_prog will fail.
2748 * EXT programs need to specify tgt_prog_fd, so they
2749 * re-attach in separate code path.
2751 if (prog->type != BPF_PROG_TYPE_TRACING &&
2752 prog->type != BPF_PROG_TYPE_LSM) {
2756 btf_id = prog->aux->attach_btf_id;
2757 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
2760 if (!prog->aux->dst_trampoline ||
2761 (key && key != prog->aux->dst_trampoline->key)) {
2762 /* If there is no saved target, or the specified target is
2763 * different from the destination specified at load time, we
2764 * need a new trampoline and a check for compatibility
2766 struct bpf_attach_target_info tgt_info = {};
2768 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2773 tr = bpf_trampoline_get(key, &tgt_info);
2779 /* The caller didn't specify a target, or the target was the
2780 * same as the destination supplied during program load. This
2781 * means we can reuse the trampoline and reference from program
2782 * load time, and there is no need to allocate a new one. This
2783 * can only happen once for any program, as the saved values in
2784 * prog->aux are cleared below.
2786 tr = prog->aux->dst_trampoline;
2787 tgt_prog = prog->aux->dst_prog;
2790 err = bpf_link_prime(&link->link, &link_primer);
2794 err = bpf_trampoline_link_prog(prog, tr);
2796 bpf_link_cleanup(&link_primer);
2801 link->tgt_prog = tgt_prog;
2802 link->trampoline = tr;
2804 /* Always clear the trampoline and target prog from prog->aux to make
2805 * sure the original attach destination is not kept alive after a
2806 * program is (re-)attached to another target.
2808 if (prog->aux->dst_prog &&
2809 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2810 /* got extra prog ref from syscall, or attaching to different prog */
2811 bpf_prog_put(prog->aux->dst_prog);
2812 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2813 /* we allocated a new trampoline, so free the old one */
2814 bpf_trampoline_put(prog->aux->dst_trampoline);
2816 prog->aux->dst_prog = NULL;
2817 prog->aux->dst_trampoline = NULL;
2818 mutex_unlock(&prog->aux->dst_mutex);
2820 return bpf_link_settle(&link_primer);
2822 if (tr && tr != prog->aux->dst_trampoline)
2823 bpf_trampoline_put(tr);
2824 mutex_unlock(&prog->aux->dst_mutex);
2827 if (tgt_prog_fd && tgt_prog)
2828 bpf_prog_put(tgt_prog);
2832 struct bpf_raw_tp_link {
2833 struct bpf_link link;
2834 struct bpf_raw_event_map *btp;
2837 static void bpf_raw_tp_link_release(struct bpf_link *link)
2839 struct bpf_raw_tp_link *raw_tp =
2840 container_of(link, struct bpf_raw_tp_link, link);
2842 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2843 bpf_put_raw_tracepoint(raw_tp->btp);
2846 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2848 struct bpf_raw_tp_link *raw_tp =
2849 container_of(link, struct bpf_raw_tp_link, link);
2854 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2855 struct seq_file *seq)
2857 struct bpf_raw_tp_link *raw_tp_link =
2858 container_of(link, struct bpf_raw_tp_link, link);
2862 raw_tp_link->btp->tp->name);
2865 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2866 struct bpf_link_info *info)
2868 struct bpf_raw_tp_link *raw_tp_link =
2869 container_of(link, struct bpf_raw_tp_link, link);
2870 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2871 const char *tp_name = raw_tp_link->btp->tp->name;
2872 u32 ulen = info->raw_tracepoint.tp_name_len;
2873 size_t tp_len = strlen(tp_name);
2878 info->raw_tracepoint.tp_name_len = tp_len + 1;
2883 if (ulen >= tp_len + 1) {
2884 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2889 if (copy_to_user(ubuf, tp_name, ulen - 1))
2891 if (put_user(zero, ubuf + ulen - 1))
2899 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
2900 .release = bpf_raw_tp_link_release,
2901 .dealloc = bpf_raw_tp_link_dealloc,
2902 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2903 .fill_link_info = bpf_raw_tp_link_fill_link_info,
2906 #ifdef CONFIG_PERF_EVENTS
2907 struct bpf_perf_link {
2908 struct bpf_link link;
2909 struct file *perf_file;
2912 static void bpf_perf_link_release(struct bpf_link *link)
2914 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
2915 struct perf_event *event = perf_link->perf_file->private_data;
2917 perf_event_free_bpf_prog(event);
2918 fput(perf_link->perf_file);
2921 static void bpf_perf_link_dealloc(struct bpf_link *link)
2923 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
2928 static const struct bpf_link_ops bpf_perf_link_lops = {
2929 .release = bpf_perf_link_release,
2930 .dealloc = bpf_perf_link_dealloc,
2933 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2935 struct bpf_link_primer link_primer;
2936 struct bpf_perf_link *link;
2937 struct perf_event *event;
2938 struct file *perf_file;
2941 if (attr->link_create.flags)
2944 perf_file = perf_event_get(attr->link_create.target_fd);
2945 if (IS_ERR(perf_file))
2946 return PTR_ERR(perf_file);
2948 link = kzalloc(sizeof(*link), GFP_USER);
2953 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog);
2954 link->perf_file = perf_file;
2956 err = bpf_link_prime(&link->link, &link_primer);
2962 event = perf_file->private_data;
2963 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
2965 bpf_link_cleanup(&link_primer);
2968 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
2971 return bpf_link_settle(&link_primer);
2977 #endif /* CONFIG_PERF_EVENTS */
2979 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2981 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2983 struct bpf_link_primer link_primer;
2984 struct bpf_raw_tp_link *link;
2985 struct bpf_raw_event_map *btp;
2986 struct bpf_prog *prog;
2987 const char *tp_name;
2991 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2994 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2996 return PTR_ERR(prog);
2998 switch (prog->type) {
2999 case BPF_PROG_TYPE_TRACING:
3000 case BPF_PROG_TYPE_EXT:
3001 case BPF_PROG_TYPE_LSM:
3002 if (attr->raw_tracepoint.name) {
3003 /* The attach point for this category of programs
3004 * should be specified via btf_id during program load.
3009 if (prog->type == BPF_PROG_TYPE_TRACING &&
3010 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
3011 tp_name = prog->aux->attach_func_name;
3014 err = bpf_tracing_prog_attach(prog, 0, 0);
3018 case BPF_PROG_TYPE_RAW_TRACEPOINT:
3019 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
3020 if (strncpy_from_user(buf,
3021 u64_to_user_ptr(attr->raw_tracepoint.name),
3022 sizeof(buf) - 1) < 0) {
3026 buf[sizeof(buf) - 1] = 0;
3034 btp = bpf_get_raw_tracepoint(tp_name);
3040 link = kzalloc(sizeof(*link), GFP_USER);
3045 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
3046 &bpf_raw_tp_link_lops, prog);
3049 err = bpf_link_prime(&link->link, &link_primer);
3055 err = bpf_probe_register(link->btp, prog);
3057 bpf_link_cleanup(&link_primer);
3061 return bpf_link_settle(&link_primer);
3064 bpf_put_raw_tracepoint(btp);
3070 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
3071 enum bpf_attach_type attach_type)
3073 switch (prog->type) {
3074 case BPF_PROG_TYPE_CGROUP_SOCK:
3075 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3076 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3077 case BPF_PROG_TYPE_SK_LOOKUP:
3078 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
3079 case BPF_PROG_TYPE_CGROUP_SKB:
3080 if (!capable(CAP_NET_ADMIN))
3081 /* cg-skb progs can be loaded by unpriv user.
3082 * check permissions at attach time.
3085 return prog->enforce_expected_attach_type &&
3086 prog->expected_attach_type != attach_type ?
3093 static enum bpf_prog_type
3094 attach_type_to_prog_type(enum bpf_attach_type attach_type)
3096 switch (attach_type) {
3097 case BPF_CGROUP_INET_INGRESS:
3098 case BPF_CGROUP_INET_EGRESS:
3099 return BPF_PROG_TYPE_CGROUP_SKB;
3100 case BPF_CGROUP_INET_SOCK_CREATE:
3101 case BPF_CGROUP_INET_SOCK_RELEASE:
3102 case BPF_CGROUP_INET4_POST_BIND:
3103 case BPF_CGROUP_INET6_POST_BIND:
3104 return BPF_PROG_TYPE_CGROUP_SOCK;
3105 case BPF_CGROUP_INET4_BIND:
3106 case BPF_CGROUP_INET6_BIND:
3107 case BPF_CGROUP_INET4_CONNECT:
3108 case BPF_CGROUP_INET6_CONNECT:
3109 case BPF_CGROUP_INET4_GETPEERNAME:
3110 case BPF_CGROUP_INET6_GETPEERNAME:
3111 case BPF_CGROUP_INET4_GETSOCKNAME:
3112 case BPF_CGROUP_INET6_GETSOCKNAME:
3113 case BPF_CGROUP_UDP4_SENDMSG:
3114 case BPF_CGROUP_UDP6_SENDMSG:
3115 case BPF_CGROUP_UDP4_RECVMSG:
3116 case BPF_CGROUP_UDP6_RECVMSG:
3117 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3118 case BPF_CGROUP_SOCK_OPS:
3119 return BPF_PROG_TYPE_SOCK_OPS;
3120 case BPF_CGROUP_DEVICE:
3121 return BPF_PROG_TYPE_CGROUP_DEVICE;
3122 case BPF_SK_MSG_VERDICT:
3123 return BPF_PROG_TYPE_SK_MSG;
3124 case BPF_SK_SKB_STREAM_PARSER:
3125 case BPF_SK_SKB_STREAM_VERDICT:
3126 case BPF_SK_SKB_VERDICT:
3127 return BPF_PROG_TYPE_SK_SKB;
3128 case BPF_LIRC_MODE2:
3129 return BPF_PROG_TYPE_LIRC_MODE2;
3130 case BPF_FLOW_DISSECTOR:
3131 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3132 case BPF_CGROUP_SYSCTL:
3133 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3134 case BPF_CGROUP_GETSOCKOPT:
3135 case BPF_CGROUP_SETSOCKOPT:
3136 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
3137 case BPF_TRACE_ITER:
3138 return BPF_PROG_TYPE_TRACING;
3140 return BPF_PROG_TYPE_SK_LOOKUP;
3142 return BPF_PROG_TYPE_XDP;
3144 return BPF_PROG_TYPE_UNSPEC;
3148 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
3150 #define BPF_F_ATTACH_MASK \
3151 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
3153 static int bpf_prog_attach(const union bpf_attr *attr)
3155 enum bpf_prog_type ptype;
3156 struct bpf_prog *prog;
3159 if (CHECK_ATTR(BPF_PROG_ATTACH))
3162 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
3165 ptype = attach_type_to_prog_type(attr->attach_type);
3166 if (ptype == BPF_PROG_TYPE_UNSPEC)
3169 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3171 return PTR_ERR(prog);
3173 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3179 case BPF_PROG_TYPE_SK_SKB:
3180 case BPF_PROG_TYPE_SK_MSG:
3181 ret = sock_map_get_from_fd(attr, prog);
3183 case BPF_PROG_TYPE_LIRC_MODE2:
3184 ret = lirc_prog_attach(attr, prog);
3186 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3187 ret = netns_bpf_prog_attach(attr, prog);
3189 case BPF_PROG_TYPE_CGROUP_DEVICE:
3190 case BPF_PROG_TYPE_CGROUP_SKB:
3191 case BPF_PROG_TYPE_CGROUP_SOCK:
3192 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3193 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3194 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3195 case BPF_PROG_TYPE_SOCK_OPS:
3196 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3207 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3209 static int bpf_prog_detach(const union bpf_attr *attr)
3211 enum bpf_prog_type ptype;
3213 if (CHECK_ATTR(BPF_PROG_DETACH))
3216 ptype = attach_type_to_prog_type(attr->attach_type);
3219 case BPF_PROG_TYPE_SK_MSG:
3220 case BPF_PROG_TYPE_SK_SKB:
3221 return sock_map_prog_detach(attr, ptype);
3222 case BPF_PROG_TYPE_LIRC_MODE2:
3223 return lirc_prog_detach(attr);
3224 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3225 return netns_bpf_prog_detach(attr, ptype);
3226 case BPF_PROG_TYPE_CGROUP_DEVICE:
3227 case BPF_PROG_TYPE_CGROUP_SKB:
3228 case BPF_PROG_TYPE_CGROUP_SOCK:
3229 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3230 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3231 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3232 case BPF_PROG_TYPE_SOCK_OPS:
3233 return cgroup_bpf_prog_detach(attr, ptype);
3239 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3241 static int bpf_prog_query(const union bpf_attr *attr,
3242 union bpf_attr __user *uattr)
3244 if (!capable(CAP_NET_ADMIN))
3246 if (CHECK_ATTR(BPF_PROG_QUERY))
3248 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3251 switch (attr->query.attach_type) {
3252 case BPF_CGROUP_INET_INGRESS:
3253 case BPF_CGROUP_INET_EGRESS:
3254 case BPF_CGROUP_INET_SOCK_CREATE:
3255 case BPF_CGROUP_INET_SOCK_RELEASE:
3256 case BPF_CGROUP_INET4_BIND:
3257 case BPF_CGROUP_INET6_BIND:
3258 case BPF_CGROUP_INET4_POST_BIND:
3259 case BPF_CGROUP_INET6_POST_BIND:
3260 case BPF_CGROUP_INET4_CONNECT:
3261 case BPF_CGROUP_INET6_CONNECT:
3262 case BPF_CGROUP_INET4_GETPEERNAME:
3263 case BPF_CGROUP_INET6_GETPEERNAME:
3264 case BPF_CGROUP_INET4_GETSOCKNAME:
3265 case BPF_CGROUP_INET6_GETSOCKNAME:
3266 case BPF_CGROUP_UDP4_SENDMSG:
3267 case BPF_CGROUP_UDP6_SENDMSG:
3268 case BPF_CGROUP_UDP4_RECVMSG:
3269 case BPF_CGROUP_UDP6_RECVMSG:
3270 case BPF_CGROUP_SOCK_OPS:
3271 case BPF_CGROUP_DEVICE:
3272 case BPF_CGROUP_SYSCTL:
3273 case BPF_CGROUP_GETSOCKOPT:
3274 case BPF_CGROUP_SETSOCKOPT:
3275 return cgroup_bpf_prog_query(attr, uattr);
3276 case BPF_LIRC_MODE2:
3277 return lirc_prog_query(attr, uattr);
3278 case BPF_FLOW_DISSECTOR:
3280 return netns_bpf_prog_query(attr, uattr);
3286 #define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
3288 static int bpf_prog_test_run(const union bpf_attr *attr,
3289 union bpf_attr __user *uattr)
3291 struct bpf_prog *prog;
3292 int ret = -ENOTSUPP;
3294 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3297 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3298 (!attr->test.ctx_size_in && attr->test.ctx_in))
3301 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3302 (!attr->test.ctx_size_out && attr->test.ctx_out))
3305 prog = bpf_prog_get(attr->test.prog_fd);
3307 return PTR_ERR(prog);
3309 if (prog->aux->ops->test_run)
3310 ret = prog->aux->ops->test_run(prog, attr, uattr);
3316 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3318 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3319 union bpf_attr __user *uattr,
3323 u32 next_id = attr->start_id;
3326 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3329 if (!capable(CAP_SYS_ADMIN))
3334 if (!idr_get_next(idr, &next_id))
3336 spin_unlock_bh(lock);
3339 err = put_user(next_id, &uattr->next_id);
3344 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3346 struct bpf_map *map;
3348 spin_lock_bh(&map_idr_lock);
3350 map = idr_get_next(&map_idr, id);
3352 map = __bpf_map_inc_not_zero(map, false);
3358 spin_unlock_bh(&map_idr_lock);
3363 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3365 struct bpf_prog *prog;
3367 spin_lock_bh(&prog_idr_lock);
3369 prog = idr_get_next(&prog_idr, id);
3371 prog = bpf_prog_inc_not_zero(prog);
3377 spin_unlock_bh(&prog_idr_lock);
3382 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3384 struct bpf_prog *bpf_prog_by_id(u32 id)
3386 struct bpf_prog *prog;
3389 return ERR_PTR(-ENOENT);
3391 spin_lock_bh(&prog_idr_lock);
3392 prog = idr_find(&prog_idr, id);
3394 prog = bpf_prog_inc_not_zero(prog);
3396 prog = ERR_PTR(-ENOENT);
3397 spin_unlock_bh(&prog_idr_lock);
3401 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3403 struct bpf_prog *prog;
3404 u32 id = attr->prog_id;
3407 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3410 if (!capable(CAP_SYS_ADMIN))
3413 prog = bpf_prog_by_id(id);
3415 return PTR_ERR(prog);
3417 fd = bpf_prog_new_fd(prog);
3424 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3426 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3428 struct bpf_map *map;
3429 u32 id = attr->map_id;
3433 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3434 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3437 if (!capable(CAP_SYS_ADMIN))
3440 f_flags = bpf_get_file_flag(attr->open_flags);
3444 spin_lock_bh(&map_idr_lock);
3445 map = idr_find(&map_idr, id);
3447 map = __bpf_map_inc_not_zero(map, true);
3449 map = ERR_PTR(-ENOENT);
3450 spin_unlock_bh(&map_idr_lock);
3453 return PTR_ERR(map);
3455 fd = bpf_map_new_fd(map, f_flags);
3457 bpf_map_put_with_uref(map);
3462 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3463 unsigned long addr, u32 *off,
3466 const struct bpf_map *map;
3469 mutex_lock(&prog->aux->used_maps_mutex);
3470 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3471 map = prog->aux->used_maps[i];
3472 if (map == (void *)addr) {
3473 *type = BPF_PSEUDO_MAP_FD;
3476 if (!map->ops->map_direct_value_meta)
3478 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3479 *type = BPF_PSEUDO_MAP_VALUE;
3486 mutex_unlock(&prog->aux->used_maps_mutex);
3490 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3491 const struct cred *f_cred)
3493 const struct bpf_map *map;
3494 struct bpf_insn *insns;
3500 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3505 for (i = 0; i < prog->len; i++) {
3506 code = insns[i].code;
3508 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3509 insns[i].code = BPF_JMP | BPF_CALL;
3510 insns[i].imm = BPF_FUNC_tail_call;
3513 if (code == (BPF_JMP | BPF_CALL) ||
3514 code == (BPF_JMP | BPF_CALL_ARGS)) {
3515 if (code == (BPF_JMP | BPF_CALL_ARGS))
3516 insns[i].code = BPF_JMP | BPF_CALL;
3517 if (!bpf_dump_raw_ok(f_cred))
3521 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3522 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3526 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3529 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3530 map = bpf_map_from_imm(prog, imm, &off, &type);
3532 insns[i].src_reg = type;
3533 insns[i].imm = map->id;
3534 insns[i + 1].imm = off;
3542 static int set_info_rec_size(struct bpf_prog_info *info)
3545 * Ensure info.*_rec_size is the same as kernel expected size
3549 * Only allow zero *_rec_size if both _rec_size and _cnt are
3550 * zero. In this case, the kernel will set the expected
3551 * _rec_size back to the info.
3554 if ((info->nr_func_info || info->func_info_rec_size) &&
3555 info->func_info_rec_size != sizeof(struct bpf_func_info))
3558 if ((info->nr_line_info || info->line_info_rec_size) &&
3559 info->line_info_rec_size != sizeof(struct bpf_line_info))
3562 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3563 info->jited_line_info_rec_size != sizeof(__u64))
3566 info->func_info_rec_size = sizeof(struct bpf_func_info);
3567 info->line_info_rec_size = sizeof(struct bpf_line_info);
3568 info->jited_line_info_rec_size = sizeof(__u64);
3573 static int bpf_prog_get_info_by_fd(struct file *file,
3574 struct bpf_prog *prog,
3575 const union bpf_attr *attr,
3576 union bpf_attr __user *uattr)
3578 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3579 struct bpf_prog_info info;
3580 u32 info_len = attr->info.info_len;
3581 struct bpf_prog_stats stats;
3582 char __user *uinsns;
3586 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3589 info_len = min_t(u32, sizeof(info), info_len);
3591 memset(&info, 0, sizeof(info));
3592 if (copy_from_user(&info, uinfo, info_len))
3595 info.type = prog->type;
3596 info.id = prog->aux->id;
3597 info.load_time = prog->aux->load_time;
3598 info.created_by_uid = from_kuid_munged(current_user_ns(),
3599 prog->aux->user->uid);
3600 info.gpl_compatible = prog->gpl_compatible;
3602 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3603 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3605 mutex_lock(&prog->aux->used_maps_mutex);
3606 ulen = info.nr_map_ids;
3607 info.nr_map_ids = prog->aux->used_map_cnt;
3608 ulen = min_t(u32, info.nr_map_ids, ulen);
3610 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3613 for (i = 0; i < ulen; i++)
3614 if (put_user(prog->aux->used_maps[i]->id,
3615 &user_map_ids[i])) {
3616 mutex_unlock(&prog->aux->used_maps_mutex);
3620 mutex_unlock(&prog->aux->used_maps_mutex);
3622 err = set_info_rec_size(&info);
3626 bpf_prog_get_stats(prog, &stats);
3627 info.run_time_ns = stats.nsecs;
3628 info.run_cnt = stats.cnt;
3629 info.recursion_misses = stats.misses;
3631 if (!bpf_capable()) {
3632 info.jited_prog_len = 0;
3633 info.xlated_prog_len = 0;
3634 info.nr_jited_ksyms = 0;
3635 info.nr_jited_func_lens = 0;
3636 info.nr_func_info = 0;
3637 info.nr_line_info = 0;
3638 info.nr_jited_line_info = 0;
3642 ulen = info.xlated_prog_len;
3643 info.xlated_prog_len = bpf_prog_insn_size(prog);
3644 if (info.xlated_prog_len && ulen) {
3645 struct bpf_insn *insns_sanitized;
3648 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3649 info.xlated_prog_insns = 0;
3652 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3653 if (!insns_sanitized)
3655 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3656 ulen = min_t(u32, info.xlated_prog_len, ulen);
3657 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3658 kfree(insns_sanitized);
3663 if (bpf_prog_is_dev_bound(prog->aux)) {
3664 err = bpf_prog_offload_info_fill(&info, prog);
3670 /* NOTE: the following code is supposed to be skipped for offload.
3671 * bpf_prog_offload_info_fill() is the place to fill similar fields
3674 ulen = info.jited_prog_len;
3675 if (prog->aux->func_cnt) {
3678 info.jited_prog_len = 0;
3679 for (i = 0; i < prog->aux->func_cnt; i++)
3680 info.jited_prog_len += prog->aux->func[i]->jited_len;
3682 info.jited_prog_len = prog->jited_len;
3685 if (info.jited_prog_len && ulen) {
3686 if (bpf_dump_raw_ok(file->f_cred)) {
3687 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3688 ulen = min_t(u32, info.jited_prog_len, ulen);
3690 /* for multi-function programs, copy the JITed
3691 * instructions for all the functions
3693 if (prog->aux->func_cnt) {
3698 for (i = 0; i < prog->aux->func_cnt; i++) {
3699 len = prog->aux->func[i]->jited_len;
3700 len = min_t(u32, len, free);
3701 img = (u8 *) prog->aux->func[i]->bpf_func;
3702 if (copy_to_user(uinsns, img, len))
3710 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3714 info.jited_prog_insns = 0;
3718 ulen = info.nr_jited_ksyms;
3719 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
3721 if (bpf_dump_raw_ok(file->f_cred)) {
3722 unsigned long ksym_addr;
3723 u64 __user *user_ksyms;
3726 /* copy the address of the kernel symbol
3727 * corresponding to each function
3729 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3730 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3731 if (prog->aux->func_cnt) {
3732 for (i = 0; i < ulen; i++) {
3733 ksym_addr = (unsigned long)
3734 prog->aux->func[i]->bpf_func;
3735 if (put_user((u64) ksym_addr,
3740 ksym_addr = (unsigned long) prog->bpf_func;
3741 if (put_user((u64) ksym_addr, &user_ksyms[0]))
3745 info.jited_ksyms = 0;
3749 ulen = info.nr_jited_func_lens;
3750 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
3752 if (bpf_dump_raw_ok(file->f_cred)) {
3753 u32 __user *user_lens;
3756 /* copy the JITed image lengths for each function */
3757 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3758 user_lens = u64_to_user_ptr(info.jited_func_lens);
3759 if (prog->aux->func_cnt) {
3760 for (i = 0; i < ulen; i++) {
3762 prog->aux->func[i]->jited_len;
3763 if (put_user(func_len, &user_lens[i]))
3767 func_len = prog->jited_len;
3768 if (put_user(func_len, &user_lens[0]))
3772 info.jited_func_lens = 0;
3777 info.btf_id = btf_obj_id(prog->aux->btf);
3779 ulen = info.nr_func_info;
3780 info.nr_func_info = prog->aux->func_info_cnt;
3781 if (info.nr_func_info && ulen) {
3782 char __user *user_finfo;
3784 user_finfo = u64_to_user_ptr(info.func_info);
3785 ulen = min_t(u32, info.nr_func_info, ulen);
3786 if (copy_to_user(user_finfo, prog->aux->func_info,
3787 info.func_info_rec_size * ulen))
3791 ulen = info.nr_line_info;
3792 info.nr_line_info = prog->aux->nr_linfo;
3793 if (info.nr_line_info && ulen) {
3794 __u8 __user *user_linfo;
3796 user_linfo = u64_to_user_ptr(info.line_info);
3797 ulen = min_t(u32, info.nr_line_info, ulen);
3798 if (copy_to_user(user_linfo, prog->aux->linfo,
3799 info.line_info_rec_size * ulen))
3803 ulen = info.nr_jited_line_info;
3804 if (prog->aux->jited_linfo)
3805 info.nr_jited_line_info = prog->aux->nr_linfo;
3807 info.nr_jited_line_info = 0;
3808 if (info.nr_jited_line_info && ulen) {
3809 if (bpf_dump_raw_ok(file->f_cred)) {
3810 __u64 __user *user_linfo;
3813 user_linfo = u64_to_user_ptr(info.jited_line_info);
3814 ulen = min_t(u32, info.nr_jited_line_info, ulen);
3815 for (i = 0; i < ulen; i++) {
3816 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3821 info.jited_line_info = 0;
3825 ulen = info.nr_prog_tags;
3826 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3828 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3831 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3832 ulen = min_t(u32, info.nr_prog_tags, ulen);
3833 if (prog->aux->func_cnt) {
3834 for (i = 0; i < ulen; i++) {
3835 if (copy_to_user(user_prog_tags[i],
3836 prog->aux->func[i]->tag,
3841 if (copy_to_user(user_prog_tags[0],
3842 prog->tag, BPF_TAG_SIZE))
3848 if (copy_to_user(uinfo, &info, info_len) ||
3849 put_user(info_len, &uattr->info.info_len))
3855 static int bpf_map_get_info_by_fd(struct file *file,
3856 struct bpf_map *map,
3857 const union bpf_attr *attr,
3858 union bpf_attr __user *uattr)
3860 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3861 struct bpf_map_info info;
3862 u32 info_len = attr->info.info_len;
3865 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3868 info_len = min_t(u32, sizeof(info), info_len);
3870 memset(&info, 0, sizeof(info));
3871 info.type = map->map_type;
3873 info.key_size = map->key_size;
3874 info.value_size = map->value_size;
3875 info.max_entries = map->max_entries;
3876 info.map_flags = map->map_flags;
3877 memcpy(info.name, map->name, sizeof(map->name));
3880 info.btf_id = btf_obj_id(map->btf);
3881 info.btf_key_type_id = map->btf_key_type_id;
3882 info.btf_value_type_id = map->btf_value_type_id;
3884 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
3886 if (bpf_map_is_dev_bound(map)) {
3887 err = bpf_map_offload_info_fill(&info, map);
3892 if (copy_to_user(uinfo, &info, info_len) ||
3893 put_user(info_len, &uattr->info.info_len))
3899 static int bpf_btf_get_info_by_fd(struct file *file,
3901 const union bpf_attr *attr,
3902 union bpf_attr __user *uattr)
3904 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3905 u32 info_len = attr->info.info_len;
3908 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
3912 return btf_get_info_by_fd(btf, attr, uattr);
3915 static int bpf_link_get_info_by_fd(struct file *file,
3916 struct bpf_link *link,
3917 const union bpf_attr *attr,
3918 union bpf_attr __user *uattr)
3920 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3921 struct bpf_link_info info;
3922 u32 info_len = attr->info.info_len;
3925 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3928 info_len = min_t(u32, sizeof(info), info_len);
3930 memset(&info, 0, sizeof(info));
3931 if (copy_from_user(&info, uinfo, info_len))
3934 info.type = link->type;
3936 info.prog_id = link->prog->aux->id;
3938 if (link->ops->fill_link_info) {
3939 err = link->ops->fill_link_info(link, &info);
3944 if (copy_to_user(uinfo, &info, info_len) ||
3945 put_user(info_len, &uattr->info.info_len))
3952 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3954 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3955 union bpf_attr __user *uattr)
3957 int ufd = attr->info.bpf_fd;
3961 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3968 if (f.file->f_op == &bpf_prog_fops)
3969 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
3971 else if (f.file->f_op == &bpf_map_fops)
3972 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
3974 else if (f.file->f_op == &btf_fops)
3975 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
3976 else if (f.file->f_op == &bpf_link_fops)
3977 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
3986 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3988 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
3990 if (CHECK_ATTR(BPF_BTF_LOAD))
3996 return btf_new_fd(attr, uattr);
3999 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
4001 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
4003 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
4006 if (!capable(CAP_SYS_ADMIN))
4009 return btf_get_fd_by_id(attr->btf_id);
4012 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
4013 union bpf_attr __user *uattr,
4014 u32 prog_id, u32 fd_type,
4015 const char *buf, u64 probe_offset,
4018 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
4019 u32 len = buf ? strlen(buf) : 0, input_len;
4022 if (put_user(len, &uattr->task_fd_query.buf_len))
4024 input_len = attr->task_fd_query.buf_len;
4025 if (input_len && ubuf) {
4027 /* nothing to copy, just make ubuf NULL terminated */
4030 if (put_user(zero, ubuf))
4032 } else if (input_len >= len + 1) {
4033 /* ubuf can hold the string with NULL terminator */
4034 if (copy_to_user(ubuf, buf, len + 1))
4037 /* ubuf cannot hold the string with NULL terminator,
4038 * do a partial copy with NULL terminator.
4043 if (copy_to_user(ubuf, buf, input_len - 1))
4045 if (put_user(zero, ubuf + input_len - 1))
4050 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
4051 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
4052 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
4053 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
4059 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
4061 static int bpf_task_fd_query(const union bpf_attr *attr,
4062 union bpf_attr __user *uattr)
4064 pid_t pid = attr->task_fd_query.pid;
4065 u32 fd = attr->task_fd_query.fd;
4066 const struct perf_event *event;
4067 struct task_struct *task;
4071 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
4074 if (!capable(CAP_SYS_ADMIN))
4077 if (attr->task_fd_query.flags != 0)
4080 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
4085 file = fget_task(task, fd);
4086 put_task_struct(task);
4090 if (file->f_op == &bpf_link_fops) {
4091 struct bpf_link *link = file->private_data;
4093 if (link->ops == &bpf_raw_tp_link_lops) {
4094 struct bpf_raw_tp_link *raw_tp =
4095 container_of(link, struct bpf_raw_tp_link, link);
4096 struct bpf_raw_event_map *btp = raw_tp->btp;
4098 err = bpf_task_fd_query_copy(attr, uattr,
4099 raw_tp->link.prog->aux->id,
4100 BPF_FD_TYPE_RAW_TRACEPOINT,
4101 btp->tp->name, 0, 0);
4107 event = perf_get_event(file);
4108 if (!IS_ERR(event)) {
4109 u64 probe_offset, probe_addr;
4110 u32 prog_id, fd_type;
4113 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4114 &buf, &probe_offset,
4117 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4131 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
4133 #define BPF_DO_BATCH(fn) \
4139 err = fn(map, attr, uattr); \
4142 static int bpf_map_do_batch(const union bpf_attr *attr,
4143 union bpf_attr __user *uattr,
4146 struct bpf_map *map;
4150 if (CHECK_ATTR(BPF_MAP_BATCH))
4153 ufd = attr->batch.map_fd;
4155 map = __bpf_map_get(f);
4157 return PTR_ERR(map);
4159 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
4160 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
4161 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4166 if (cmd != BPF_MAP_LOOKUP_BATCH &&
4167 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4172 if (cmd == BPF_MAP_LOOKUP_BATCH)
4173 BPF_DO_BATCH(map->ops->map_lookup_batch);
4174 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4175 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4176 else if (cmd == BPF_MAP_UPDATE_BATCH)
4177 BPF_DO_BATCH(map->ops->map_update_batch);
4179 BPF_DO_BATCH(map->ops->map_delete_batch);
4186 static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
4187 struct bpf_prog *prog)
4189 if (attr->link_create.attach_type != prog->expected_attach_type)
4192 if (prog->expected_attach_type == BPF_TRACE_ITER)
4193 return bpf_iter_link_attach(attr, uattr, prog);
4194 else if (prog->type == BPF_PROG_TYPE_EXT)
4195 return bpf_tracing_prog_attach(prog,
4196 attr->link_create.target_fd,
4197 attr->link_create.target_btf_id);
4201 #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
4202 static int link_create(union bpf_attr *attr, bpfptr_t uattr)
4204 enum bpf_prog_type ptype;
4205 struct bpf_prog *prog;
4208 if (CHECK_ATTR(BPF_LINK_CREATE))
4211 prog = bpf_prog_get(attr->link_create.prog_fd);
4213 return PTR_ERR(prog);
4215 ret = bpf_prog_attach_check_attach_type(prog,
4216 attr->link_create.attach_type);
4220 switch (prog->type) {
4221 case BPF_PROG_TYPE_EXT:
4222 ret = tracing_bpf_link_attach(attr, uattr, prog);
4224 case BPF_PROG_TYPE_PERF_EVENT:
4225 case BPF_PROG_TYPE_KPROBE:
4226 case BPF_PROG_TYPE_TRACEPOINT:
4227 if (attr->link_create.attach_type != BPF_PERF_EVENT) {
4234 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4235 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4243 case BPF_PROG_TYPE_CGROUP_SKB:
4244 case BPF_PROG_TYPE_CGROUP_SOCK:
4245 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4246 case BPF_PROG_TYPE_SOCK_OPS:
4247 case BPF_PROG_TYPE_CGROUP_DEVICE:
4248 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4249 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4250 ret = cgroup_bpf_link_attach(attr, prog);
4252 case BPF_PROG_TYPE_TRACING:
4253 ret = tracing_bpf_link_attach(attr, uattr, prog);
4255 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4256 case BPF_PROG_TYPE_SK_LOOKUP:
4257 ret = netns_bpf_link_create(attr, prog);
4260 case BPF_PROG_TYPE_XDP:
4261 ret = bpf_xdp_link_attach(attr, prog);
4264 #ifdef CONFIG_PERF_EVENTS
4265 case BPF_PROG_TYPE_PERF_EVENT:
4266 case BPF_PROG_TYPE_TRACEPOINT:
4267 case BPF_PROG_TYPE_KPROBE:
4268 ret = bpf_perf_link_attach(attr, prog);
4281 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4283 static int link_update(union bpf_attr *attr)
4285 struct bpf_prog *old_prog = NULL, *new_prog;
4286 struct bpf_link *link;
4290 if (CHECK_ATTR(BPF_LINK_UPDATE))
4293 flags = attr->link_update.flags;
4294 if (flags & ~BPF_F_REPLACE)
4297 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4299 return PTR_ERR(link);
4301 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4302 if (IS_ERR(new_prog)) {
4303 ret = PTR_ERR(new_prog);
4307 if (flags & BPF_F_REPLACE) {
4308 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4309 if (IS_ERR(old_prog)) {
4310 ret = PTR_ERR(old_prog);
4314 } else if (attr->link_update.old_prog_fd) {
4319 if (link->ops->update_prog)
4320 ret = link->ops->update_prog(link, new_prog, old_prog);
4326 bpf_prog_put(old_prog);
4328 bpf_prog_put(new_prog);
4334 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4336 static int link_detach(union bpf_attr *attr)
4338 struct bpf_link *link;
4341 if (CHECK_ATTR(BPF_LINK_DETACH))
4344 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4346 return PTR_ERR(link);
4348 if (link->ops->detach)
4349 ret = link->ops->detach(link);
4357 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4359 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4362 struct bpf_link *bpf_link_by_id(u32 id)
4364 struct bpf_link *link;
4367 return ERR_PTR(-ENOENT);
4369 spin_lock_bh(&link_idr_lock);
4370 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4371 link = idr_find(&link_idr, id);
4374 link = bpf_link_inc_not_zero(link);
4376 link = ERR_PTR(-EAGAIN);
4378 link = ERR_PTR(-ENOENT);
4380 spin_unlock_bh(&link_idr_lock);
4384 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4386 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4388 struct bpf_link *link;
4389 u32 id = attr->link_id;
4392 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4395 if (!capable(CAP_SYS_ADMIN))
4398 link = bpf_link_by_id(id);
4400 return PTR_ERR(link);
4402 fd = bpf_link_new_fd(link);
4409 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4411 static int bpf_stats_release(struct inode *inode, struct file *file)
4413 mutex_lock(&bpf_stats_enabled_mutex);
4414 static_key_slow_dec(&bpf_stats_enabled_key.key);
4415 mutex_unlock(&bpf_stats_enabled_mutex);
4419 static const struct file_operations bpf_stats_fops = {
4420 .release = bpf_stats_release,
4423 static int bpf_enable_runtime_stats(void)
4427 mutex_lock(&bpf_stats_enabled_mutex);
4429 /* Set a very high limit to avoid overflow */
4430 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4431 mutex_unlock(&bpf_stats_enabled_mutex);
4435 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4437 static_key_slow_inc(&bpf_stats_enabled_key.key);
4439 mutex_unlock(&bpf_stats_enabled_mutex);
4443 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4445 static int bpf_enable_stats(union bpf_attr *attr)
4448 if (CHECK_ATTR(BPF_ENABLE_STATS))
4451 if (!capable(CAP_SYS_ADMIN))
4454 switch (attr->enable_stats.type) {
4455 case BPF_STATS_RUN_TIME:
4456 return bpf_enable_runtime_stats();
4463 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4465 static int bpf_iter_create(union bpf_attr *attr)
4467 struct bpf_link *link;
4470 if (CHECK_ATTR(BPF_ITER_CREATE))
4473 if (attr->iter_create.flags)
4476 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4478 return PTR_ERR(link);
4480 err = bpf_iter_new_fd(link);
4486 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4488 static int bpf_prog_bind_map(union bpf_attr *attr)
4490 struct bpf_prog *prog;
4491 struct bpf_map *map;
4492 struct bpf_map **used_maps_old, **used_maps_new;
4495 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4498 if (attr->prog_bind_map.flags)
4501 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4503 return PTR_ERR(prog);
4505 map = bpf_map_get(attr->prog_bind_map.map_fd);
4511 mutex_lock(&prog->aux->used_maps_mutex);
4513 used_maps_old = prog->aux->used_maps;
4515 for (i = 0; i < prog->aux->used_map_cnt; i++)
4516 if (used_maps_old[i] == map) {
4521 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4522 sizeof(used_maps_new[0]),
4524 if (!used_maps_new) {
4529 memcpy(used_maps_new, used_maps_old,
4530 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4531 used_maps_new[prog->aux->used_map_cnt] = map;
4533 prog->aux->used_map_cnt++;
4534 prog->aux->used_maps = used_maps_new;
4536 kfree(used_maps_old);
4539 mutex_unlock(&prog->aux->used_maps_mutex);
4548 static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
4550 union bpf_attr attr;
4553 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
4556 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4559 size = min_t(u32, size, sizeof(attr));
4561 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4562 memset(&attr, 0, sizeof(attr));
4563 if (copy_from_bpfptr(&attr, uattr, size) != 0)
4566 err = security_bpf(cmd, &attr, size);
4571 case BPF_MAP_CREATE:
4572 err = map_create(&attr);
4574 case BPF_MAP_LOOKUP_ELEM:
4575 err = map_lookup_elem(&attr);
4577 case BPF_MAP_UPDATE_ELEM:
4578 err = map_update_elem(&attr, uattr);
4580 case BPF_MAP_DELETE_ELEM:
4581 err = map_delete_elem(&attr);
4583 case BPF_MAP_GET_NEXT_KEY:
4584 err = map_get_next_key(&attr);
4586 case BPF_MAP_FREEZE:
4587 err = map_freeze(&attr);
4590 err = bpf_prog_load(&attr, uattr);
4593 err = bpf_obj_pin(&attr);
4596 err = bpf_obj_get(&attr);
4598 case BPF_PROG_ATTACH:
4599 err = bpf_prog_attach(&attr);
4601 case BPF_PROG_DETACH:
4602 err = bpf_prog_detach(&attr);
4604 case BPF_PROG_QUERY:
4605 err = bpf_prog_query(&attr, uattr.user);
4607 case BPF_PROG_TEST_RUN:
4608 err = bpf_prog_test_run(&attr, uattr.user);
4610 case BPF_PROG_GET_NEXT_ID:
4611 err = bpf_obj_get_next_id(&attr, uattr.user,
4612 &prog_idr, &prog_idr_lock);
4614 case BPF_MAP_GET_NEXT_ID:
4615 err = bpf_obj_get_next_id(&attr, uattr.user,
4616 &map_idr, &map_idr_lock);
4618 case BPF_BTF_GET_NEXT_ID:
4619 err = bpf_obj_get_next_id(&attr, uattr.user,
4620 &btf_idr, &btf_idr_lock);
4622 case BPF_PROG_GET_FD_BY_ID:
4623 err = bpf_prog_get_fd_by_id(&attr);
4625 case BPF_MAP_GET_FD_BY_ID:
4626 err = bpf_map_get_fd_by_id(&attr);
4628 case BPF_OBJ_GET_INFO_BY_FD:
4629 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
4631 case BPF_RAW_TRACEPOINT_OPEN:
4632 err = bpf_raw_tracepoint_open(&attr);
4635 err = bpf_btf_load(&attr, uattr);
4637 case BPF_BTF_GET_FD_BY_ID:
4638 err = bpf_btf_get_fd_by_id(&attr);
4640 case BPF_TASK_FD_QUERY:
4641 err = bpf_task_fd_query(&attr, uattr.user);
4643 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4644 err = map_lookup_and_delete_elem(&attr);
4646 case BPF_MAP_LOOKUP_BATCH:
4647 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
4649 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4650 err = bpf_map_do_batch(&attr, uattr.user,
4651 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4653 case BPF_MAP_UPDATE_BATCH:
4654 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
4656 case BPF_MAP_DELETE_BATCH:
4657 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
4659 case BPF_LINK_CREATE:
4660 err = link_create(&attr, uattr);
4662 case BPF_LINK_UPDATE:
4663 err = link_update(&attr);
4665 case BPF_LINK_GET_FD_BY_ID:
4666 err = bpf_link_get_fd_by_id(&attr);
4668 case BPF_LINK_GET_NEXT_ID:
4669 err = bpf_obj_get_next_id(&attr, uattr.user,
4670 &link_idr, &link_idr_lock);
4672 case BPF_ENABLE_STATS:
4673 err = bpf_enable_stats(&attr);
4675 case BPF_ITER_CREATE:
4676 err = bpf_iter_create(&attr);
4678 case BPF_LINK_DETACH:
4679 err = link_detach(&attr);
4681 case BPF_PROG_BIND_MAP:
4682 err = bpf_prog_bind_map(&attr);
4692 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4694 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
4697 static bool syscall_prog_is_valid_access(int off, int size,
4698 enum bpf_access_type type,
4699 const struct bpf_prog *prog,
4700 struct bpf_insn_access_aux *info)
4702 if (off < 0 || off >= U16_MAX)
4704 if (off % size != 0)
4709 BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size)
4712 case BPF_MAP_CREATE:
4713 case BPF_MAP_UPDATE_ELEM:
4714 case BPF_MAP_FREEZE:
4718 /* case BPF_PROG_TEST_RUN:
4719 * is not part of this list to prevent recursive test_run
4724 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
4727 static const struct bpf_func_proto bpf_sys_bpf_proto = {
4728 .func = bpf_sys_bpf,
4730 .ret_type = RET_INTEGER,
4731 .arg1_type = ARG_ANYTHING,
4732 .arg2_type = ARG_PTR_TO_MEM,
4733 .arg3_type = ARG_CONST_SIZE,
4736 const struct bpf_func_proto * __weak
4737 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4739 return bpf_base_func_proto(func_id);
4742 BPF_CALL_1(bpf_sys_close, u32, fd)
4744 /* When bpf program calls this helper there should not be
4745 * an fdget() without matching completed fdput().
4746 * This helper is allowed in the following callchain only:
4747 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
4749 return close_fd(fd);
4752 static const struct bpf_func_proto bpf_sys_close_proto = {
4753 .func = bpf_sys_close,
4755 .ret_type = RET_INTEGER,
4756 .arg1_type = ARG_ANYTHING,
4759 static const struct bpf_func_proto *
4760 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4763 case BPF_FUNC_sys_bpf:
4764 return &bpf_sys_bpf_proto;
4765 case BPF_FUNC_btf_find_by_name_kind:
4766 return &bpf_btf_find_by_name_kind_proto;
4767 case BPF_FUNC_sys_close:
4768 return &bpf_sys_close_proto;
4770 return tracing_prog_func_proto(func_id, prog);
4774 const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
4775 .get_func_proto = syscall_prog_func_proto,
4776 .is_valid_access = syscall_prog_is_valid_access,
4779 const struct bpf_prog_ops bpf_syscall_prog_ops = {
4780 .test_run = bpf_prog_test_run_syscall,