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 type = array->aux->type;
547 jited = array->aux->jited;
564 bpf_map_memory_footprint(map),
566 READ_ONCE(map->frozen));
568 seq_printf(m, "owner_prog_type:\t%u\n", type);
569 seq_printf(m, "owner_jited:\t%u\n", jited);
574 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
577 /* We need this handler such that alloc_file() enables
578 * f_mode with FMODE_CAN_READ.
583 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
584 size_t siz, loff_t *ppos)
586 /* We need this handler such that alloc_file() enables
587 * f_mode with FMODE_CAN_WRITE.
592 /* called for any extra memory-mapped regions (except initial) */
593 static void bpf_map_mmap_open(struct vm_area_struct *vma)
595 struct bpf_map *map = vma->vm_file->private_data;
597 if (vma->vm_flags & VM_MAYWRITE) {
598 mutex_lock(&map->freeze_mutex);
600 mutex_unlock(&map->freeze_mutex);
604 /* called for all unmapped memory region (including initial) */
605 static void bpf_map_mmap_close(struct vm_area_struct *vma)
607 struct bpf_map *map = vma->vm_file->private_data;
609 if (vma->vm_flags & VM_MAYWRITE) {
610 mutex_lock(&map->freeze_mutex);
612 mutex_unlock(&map->freeze_mutex);
616 static const struct vm_operations_struct bpf_map_default_vmops = {
617 .open = bpf_map_mmap_open,
618 .close = bpf_map_mmap_close,
621 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
623 struct bpf_map *map = filp->private_data;
626 if (!map->ops->map_mmap || map_value_has_spin_lock(map) ||
627 map_value_has_timer(map))
630 if (!(vma->vm_flags & VM_SHARED))
633 mutex_lock(&map->freeze_mutex);
635 if (vma->vm_flags & VM_WRITE) {
640 /* map is meant to be read-only, so do not allow mapping as
641 * writable, because it's possible to leak a writable page
642 * reference and allows user-space to still modify it after
643 * freezing, while verifier will assume contents do not change
645 if (map->map_flags & BPF_F_RDONLY_PROG) {
651 /* set default open/close callbacks */
652 vma->vm_ops = &bpf_map_default_vmops;
653 vma->vm_private_data = map;
654 vma->vm_flags &= ~VM_MAYEXEC;
655 if (!(vma->vm_flags & VM_WRITE))
656 /* disallow re-mapping with PROT_WRITE */
657 vma->vm_flags &= ~VM_MAYWRITE;
659 err = map->ops->map_mmap(map, vma);
663 if (vma->vm_flags & VM_MAYWRITE)
666 mutex_unlock(&map->freeze_mutex);
670 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
672 struct bpf_map *map = filp->private_data;
674 if (map->ops->map_poll)
675 return map->ops->map_poll(map, filp, pts);
680 const struct file_operations bpf_map_fops = {
681 #ifdef CONFIG_PROC_FS
682 .show_fdinfo = bpf_map_show_fdinfo,
684 .release = bpf_map_release,
685 .read = bpf_dummy_read,
686 .write = bpf_dummy_write,
687 .mmap = bpf_map_mmap,
688 .poll = bpf_map_poll,
691 int bpf_map_new_fd(struct bpf_map *map, int flags)
695 ret = security_bpf_map(map, OPEN_FMODE(flags));
699 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
703 int bpf_get_file_flag(int flags)
705 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
707 if (flags & BPF_F_RDONLY)
709 if (flags & BPF_F_WRONLY)
714 /* helper macro to check that unused fields 'union bpf_attr' are zero */
715 #define CHECK_ATTR(CMD) \
716 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
717 sizeof(attr->CMD##_LAST_FIELD), 0, \
719 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
720 sizeof(attr->CMD##_LAST_FIELD)) != NULL
722 /* dst and src must have at least "size" number of bytes.
723 * Return strlen on success and < 0 on error.
725 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
727 const char *end = src + size;
728 const char *orig_src = src;
730 memset(dst, 0, size);
731 /* Copy all isalnum(), '_' and '.' chars. */
732 while (src < end && *src) {
733 if (!isalnum(*src) &&
734 *src != '_' && *src != '.')
739 /* No '\0' found in "size" number of bytes */
743 return src - orig_src;
746 int map_check_no_btf(const struct bpf_map *map,
747 const struct btf *btf,
748 const struct btf_type *key_type,
749 const struct btf_type *value_type)
754 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
755 u32 btf_key_id, u32 btf_value_id)
757 const struct btf_type *key_type, *value_type;
758 u32 key_size, value_size;
761 /* Some maps allow key to be unspecified. */
763 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
764 if (!key_type || key_size != map->key_size)
767 key_type = btf_type_by_id(btf, 0);
768 if (!map->ops->map_check_btf)
772 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
773 if (!value_type || value_size != map->value_size)
776 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
778 if (map_value_has_spin_lock(map)) {
779 if (map->map_flags & BPF_F_RDONLY_PROG)
781 if (map->map_type != BPF_MAP_TYPE_HASH &&
782 map->map_type != BPF_MAP_TYPE_ARRAY &&
783 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
784 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
785 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
786 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
788 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
791 "verifier bug spin_lock_off %d value_size %d\n",
792 map->spin_lock_off, map->value_size);
797 map->timer_off = btf_find_timer(btf, value_type);
798 if (map_value_has_timer(map)) {
799 if (map->map_flags & BPF_F_RDONLY_PROG)
801 if (map->map_type != BPF_MAP_TYPE_HASH &&
802 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
803 map->map_type != BPF_MAP_TYPE_ARRAY)
807 if (map->ops->map_check_btf)
808 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
813 #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
814 /* called via syscall */
815 static int map_create(union bpf_attr *attr)
817 int numa_node = bpf_map_attr_numa_node(attr);
822 err = CHECK_ATTR(BPF_MAP_CREATE);
826 if (attr->btf_vmlinux_value_type_id) {
827 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
828 attr->btf_key_type_id || attr->btf_value_type_id)
830 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
834 f_flags = bpf_get_file_flag(attr->map_flags);
838 if (numa_node != NUMA_NO_NODE &&
839 ((unsigned int)numa_node >= nr_node_ids ||
840 !node_online(numa_node)))
843 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
844 map = find_and_alloc_map(attr);
848 err = bpf_obj_name_cpy(map->name, attr->map_name,
849 sizeof(attr->map_name));
853 atomic64_set(&map->refcnt, 1);
854 atomic64_set(&map->usercnt, 1);
855 mutex_init(&map->freeze_mutex);
857 map->spin_lock_off = -EINVAL;
858 map->timer_off = -EINVAL;
859 if (attr->btf_key_type_id || attr->btf_value_type_id ||
860 /* Even the map's value is a kernel's struct,
861 * the bpf_prog.o must have BTF to begin with
862 * to figure out the corresponding kernel's
863 * counter part. Thus, attr->btf_fd has
866 attr->btf_vmlinux_value_type_id) {
869 btf = btf_get_by_fd(attr->btf_fd);
874 if (btf_is_kernel(btf)) {
881 if (attr->btf_value_type_id) {
882 err = map_check_btf(map, btf, attr->btf_key_type_id,
883 attr->btf_value_type_id);
888 map->btf_key_type_id = attr->btf_key_type_id;
889 map->btf_value_type_id = attr->btf_value_type_id;
890 map->btf_vmlinux_value_type_id =
891 attr->btf_vmlinux_value_type_id;
894 err = security_bpf_map_alloc(map);
898 err = bpf_map_alloc_id(map);
902 bpf_map_save_memcg(map);
904 err = bpf_map_new_fd(map, f_flags);
906 /* failed to allocate fd.
907 * bpf_map_put_with_uref() is needed because the above
908 * bpf_map_alloc_id() has published the map
909 * to the userspace and the userspace may
910 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
912 bpf_map_put_with_uref(map);
919 security_bpf_map_free(map);
922 map->ops->map_free(map);
926 /* if error is returned, fd is released.
927 * On success caller should complete fd access with matching fdput()
929 struct bpf_map *__bpf_map_get(struct fd f)
932 return ERR_PTR(-EBADF);
933 if (f.file->f_op != &bpf_map_fops) {
935 return ERR_PTR(-EINVAL);
938 return f.file->private_data;
941 void bpf_map_inc(struct bpf_map *map)
943 atomic64_inc(&map->refcnt);
945 EXPORT_SYMBOL_GPL(bpf_map_inc);
947 void bpf_map_inc_with_uref(struct bpf_map *map)
949 atomic64_inc(&map->refcnt);
950 atomic64_inc(&map->usercnt);
952 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
954 struct bpf_map *bpf_map_get(u32 ufd)
956 struct fd f = fdget(ufd);
959 map = __bpf_map_get(f);
969 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
971 struct fd f = fdget(ufd);
974 map = __bpf_map_get(f);
978 bpf_map_inc_with_uref(map);
984 /* map_idr_lock should have been held */
985 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
989 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
991 return ERR_PTR(-ENOENT);
993 atomic64_inc(&map->usercnt);
998 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
1000 spin_lock_bh(&map_idr_lock);
1001 map = __bpf_map_inc_not_zero(map, false);
1002 spin_unlock_bh(&map_idr_lock);
1006 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
1008 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1013 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1016 return vmemdup_user(ukey, key_size);
1019 return ERR_PTR(-EINVAL);
1024 static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1027 return kvmemdup_bpfptr(ukey, key_size);
1029 if (!bpfptr_is_null(ukey))
1030 return ERR_PTR(-EINVAL);
1035 /* last field in 'union bpf_attr' used by this command */
1036 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1038 static int map_lookup_elem(union bpf_attr *attr)
1040 void __user *ukey = u64_to_user_ptr(attr->key);
1041 void __user *uvalue = u64_to_user_ptr(attr->value);
1042 int ufd = attr->map_fd;
1043 struct bpf_map *map;
1049 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1052 if (attr->flags & ~BPF_F_LOCK)
1056 map = __bpf_map_get(f);
1058 return PTR_ERR(map);
1059 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1064 if ((attr->flags & BPF_F_LOCK) &&
1065 !map_value_has_spin_lock(map)) {
1070 key = __bpf_copy_key(ukey, map->key_size);
1076 value_size = bpf_map_value_size(map);
1079 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1083 err = bpf_map_copy_value(map, key, value, attr->flags);
1088 if (copy_to_user(uvalue, value, value_size) != 0)
1103 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1105 static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
1107 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1108 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
1109 int ufd = attr->map_fd;
1110 struct bpf_map *map;
1116 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1120 map = __bpf_map_get(f);
1122 return PTR_ERR(map);
1123 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1128 if ((attr->flags & BPF_F_LOCK) &&
1129 !map_value_has_spin_lock(map)) {
1134 key = ___bpf_copy_key(ukey, map->key_size);
1140 value_size = bpf_map_value_size(map);
1143 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1148 if (copy_from_bpfptr(value, uvalue, value_size) != 0)
1151 err = bpf_map_update_value(map, f, key, value, attr->flags);
1162 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1164 static int map_delete_elem(union bpf_attr *attr)
1166 void __user *ukey = u64_to_user_ptr(attr->key);
1167 int ufd = attr->map_fd;
1168 struct bpf_map *map;
1173 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1177 map = __bpf_map_get(f);
1179 return PTR_ERR(map);
1180 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1185 key = __bpf_copy_key(ukey, map->key_size);
1191 if (bpf_map_is_dev_bound(map)) {
1192 err = bpf_map_offload_delete_elem(map, key);
1194 } else if (IS_FD_PROG_ARRAY(map) ||
1195 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1196 /* These maps require sleepable context */
1197 err = map->ops->map_delete_elem(map, key);
1201 bpf_disable_instrumentation();
1203 err = map->ops->map_delete_elem(map, key);
1205 bpf_enable_instrumentation();
1206 maybe_wait_bpf_programs(map);
1214 /* last field in 'union bpf_attr' used by this command */
1215 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1217 static int map_get_next_key(union bpf_attr *attr)
1219 void __user *ukey = u64_to_user_ptr(attr->key);
1220 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1221 int ufd = attr->map_fd;
1222 struct bpf_map *map;
1223 void *key, *next_key;
1227 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1231 map = __bpf_map_get(f);
1233 return PTR_ERR(map);
1234 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1240 key = __bpf_copy_key(ukey, map->key_size);
1250 next_key = kvmalloc(map->key_size, GFP_USER);
1254 if (bpf_map_is_dev_bound(map)) {
1255 err = bpf_map_offload_get_next_key(map, key, next_key);
1260 err = map->ops->map_get_next_key(map, key, next_key);
1267 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1281 int generic_map_delete_batch(struct bpf_map *map,
1282 const union bpf_attr *attr,
1283 union bpf_attr __user *uattr)
1285 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1290 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1293 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1294 !map_value_has_spin_lock(map)) {
1298 max_count = attr->batch.count;
1302 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1306 for (cp = 0; cp < max_count; cp++) {
1308 if (copy_from_user(key, keys + cp * map->key_size,
1312 if (bpf_map_is_dev_bound(map)) {
1313 err = bpf_map_offload_delete_elem(map, key);
1317 bpf_disable_instrumentation();
1319 err = map->ops->map_delete_elem(map, key);
1321 bpf_enable_instrumentation();
1322 maybe_wait_bpf_programs(map);
1326 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1333 int generic_map_update_batch(struct bpf_map *map,
1334 const union bpf_attr *attr,
1335 union bpf_attr __user *uattr)
1337 void __user *values = u64_to_user_ptr(attr->batch.values);
1338 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1339 u32 value_size, cp, max_count;
1340 int ufd = attr->map_fd;
1346 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1349 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1350 !map_value_has_spin_lock(map)) {
1354 value_size = bpf_map_value_size(map);
1356 max_count = attr->batch.count;
1360 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1364 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1370 for (cp = 0; cp < max_count; cp++) {
1372 if (copy_from_user(key, keys + cp * map->key_size,
1374 copy_from_user(value, values + cp * value_size, value_size))
1377 err = bpf_map_update_value(map, f, key, value,
1378 attr->batch.elem_flags);
1384 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1392 #define MAP_LOOKUP_RETRIES 3
1394 int generic_map_lookup_batch(struct bpf_map *map,
1395 const union bpf_attr *attr,
1396 union bpf_attr __user *uattr)
1398 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1399 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1400 void __user *values = u64_to_user_ptr(attr->batch.values);
1401 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1402 void *buf, *buf_prevkey, *prev_key, *key, *value;
1403 int err, retry = MAP_LOOKUP_RETRIES;
1404 u32 value_size, cp, max_count;
1406 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1409 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1410 !map_value_has_spin_lock(map))
1413 value_size = bpf_map_value_size(map);
1415 max_count = attr->batch.count;
1419 if (put_user(0, &uattr->batch.count))
1422 buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1426 buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1428 kvfree(buf_prevkey);
1434 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1437 value = key + map->key_size;
1439 prev_key = buf_prevkey;
1441 for (cp = 0; cp < max_count;) {
1443 err = map->ops->map_get_next_key(map, prev_key, key);
1447 err = bpf_map_copy_value(map, key, value,
1448 attr->batch.elem_flags);
1450 if (err == -ENOENT) {
1462 if (copy_to_user(keys + cp * map->key_size, key,
1467 if (copy_to_user(values + cp * value_size, value, value_size)) {
1473 prev_key = buf_prevkey;
1475 swap(prev_key, key);
1476 retry = MAP_LOOKUP_RETRIES;
1483 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1484 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1488 kvfree(buf_prevkey);
1493 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
1495 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1497 void __user *ukey = u64_to_user_ptr(attr->key);
1498 void __user *uvalue = u64_to_user_ptr(attr->value);
1499 int ufd = attr->map_fd;
1500 struct bpf_map *map;
1506 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1509 if (attr->flags & ~BPF_F_LOCK)
1513 map = __bpf_map_get(f);
1515 return PTR_ERR(map);
1516 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1517 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1523 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1524 map->map_type == BPF_MAP_TYPE_STACK)) {
1529 if ((attr->flags & BPF_F_LOCK) &&
1530 !map_value_has_spin_lock(map)) {
1535 key = __bpf_copy_key(ukey, map->key_size);
1541 value_size = bpf_map_value_size(map);
1544 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1549 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1550 map->map_type == BPF_MAP_TYPE_STACK) {
1551 err = map->ops->map_pop_elem(map, value);
1552 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1553 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1554 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1555 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1556 if (!bpf_map_is_dev_bound(map)) {
1557 bpf_disable_instrumentation();
1559 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1561 bpf_enable_instrumentation();
1568 if (copy_to_user(uvalue, value, value_size) != 0) {
1584 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1586 static int map_freeze(const union bpf_attr *attr)
1588 int err = 0, ufd = attr->map_fd;
1589 struct bpf_map *map;
1592 if (CHECK_ATTR(BPF_MAP_FREEZE))
1596 map = __bpf_map_get(f);
1598 return PTR_ERR(map);
1600 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS ||
1601 map_value_has_timer(map)) {
1606 mutex_lock(&map->freeze_mutex);
1608 if (map->writecnt) {
1612 if (READ_ONCE(map->frozen)) {
1616 if (!bpf_capable()) {
1621 WRITE_ONCE(map->frozen, true);
1623 mutex_unlock(&map->freeze_mutex);
1628 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1629 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1630 [_id] = & _name ## _prog_ops,
1631 #define BPF_MAP_TYPE(_id, _ops)
1632 #define BPF_LINK_TYPE(_id, _name)
1633 #include <linux/bpf_types.h>
1634 #undef BPF_PROG_TYPE
1636 #undef BPF_LINK_TYPE
1639 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1641 const struct bpf_prog_ops *ops;
1643 if (type >= ARRAY_SIZE(bpf_prog_types))
1645 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1646 ops = bpf_prog_types[type];
1650 if (!bpf_prog_is_dev_bound(prog->aux))
1651 prog->aux->ops = ops;
1653 prog->aux->ops = &bpf_offload_prog_ops;
1664 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1665 [BPF_AUDIT_LOAD] = "LOAD",
1666 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1669 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1671 struct audit_context *ctx = NULL;
1672 struct audit_buffer *ab;
1674 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1676 if (audit_enabled == AUDIT_OFF)
1678 if (op == BPF_AUDIT_LOAD)
1679 ctx = audit_context();
1680 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1683 audit_log_format(ab, "prog-id=%u op=%s",
1684 prog->aux->id, bpf_audit_str[op]);
1688 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1692 idr_preload(GFP_KERNEL);
1693 spin_lock_bh(&prog_idr_lock);
1694 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1697 spin_unlock_bh(&prog_idr_lock);
1700 /* id is in [1, INT_MAX) */
1701 if (WARN_ON_ONCE(!id))
1704 return id > 0 ? 0 : id;
1707 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1709 unsigned long flags;
1711 /* cBPF to eBPF migrations are currently not in the idr store.
1712 * Offloaded programs are removed from the store when their device
1713 * disappears - even if someone grabs an fd to them they are unusable,
1714 * simply waiting for refcnt to drop to be freed.
1720 spin_lock_irqsave(&prog_idr_lock, flags);
1722 __acquire(&prog_idr_lock);
1724 idr_remove(&prog_idr, prog->aux->id);
1728 spin_unlock_irqrestore(&prog_idr_lock, flags);
1730 __release(&prog_idr_lock);
1733 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1735 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1737 kvfree(aux->func_info);
1738 kfree(aux->func_info_aux);
1739 free_uid(aux->user);
1740 security_bpf_prog_free(aux);
1741 bpf_prog_free(aux->prog);
1744 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1746 bpf_prog_kallsyms_del_all(prog);
1747 btf_put(prog->aux->btf);
1748 kvfree(prog->aux->jited_linfo);
1749 kvfree(prog->aux->linfo);
1750 kfree(prog->aux->kfunc_tab);
1751 if (prog->aux->attach_btf)
1752 btf_put(prog->aux->attach_btf);
1755 if (prog->aux->sleepable)
1756 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1758 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1760 __bpf_prog_put_rcu(&prog->aux->rcu);
1764 static void bpf_prog_put_deferred(struct work_struct *work)
1766 struct bpf_prog_aux *aux;
1767 struct bpf_prog *prog;
1769 aux = container_of(work, struct bpf_prog_aux, work);
1771 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1772 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1773 __bpf_prog_put_noref(prog, true);
1776 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1778 struct bpf_prog_aux *aux = prog->aux;
1780 if (atomic64_dec_and_test(&aux->refcnt)) {
1781 /* bpf_prog_free_id() must be called first */
1782 bpf_prog_free_id(prog, do_idr_lock);
1784 if (in_irq() || irqs_disabled()) {
1785 INIT_WORK(&aux->work, bpf_prog_put_deferred);
1786 schedule_work(&aux->work);
1788 bpf_prog_put_deferred(&aux->work);
1793 void bpf_prog_put(struct bpf_prog *prog)
1795 __bpf_prog_put(prog, true);
1797 EXPORT_SYMBOL_GPL(bpf_prog_put);
1799 static int bpf_prog_release(struct inode *inode, struct file *filp)
1801 struct bpf_prog *prog = filp->private_data;
1807 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1808 struct bpf_prog_stats *stats)
1810 u64 nsecs = 0, cnt = 0, misses = 0;
1813 for_each_possible_cpu(cpu) {
1814 const struct bpf_prog_stats *st;
1816 u64 tnsecs, tcnt, tmisses;
1818 st = per_cpu_ptr(prog->stats, cpu);
1820 start = u64_stats_fetch_begin_irq(&st->syncp);
1823 tmisses = st->misses;
1824 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1829 stats->nsecs = nsecs;
1831 stats->misses = misses;
1834 #ifdef CONFIG_PROC_FS
1835 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1837 const struct bpf_prog *prog = filp->private_data;
1838 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1839 struct bpf_prog_stats stats;
1841 bpf_prog_get_stats(prog, &stats);
1842 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1849 "run_time_ns:\t%llu\n"
1851 "recursion_misses:\t%llu\n",
1855 prog->pages * 1ULL << PAGE_SHIFT,
1863 const struct file_operations bpf_prog_fops = {
1864 #ifdef CONFIG_PROC_FS
1865 .show_fdinfo = bpf_prog_show_fdinfo,
1867 .release = bpf_prog_release,
1868 .read = bpf_dummy_read,
1869 .write = bpf_dummy_write,
1872 int bpf_prog_new_fd(struct bpf_prog *prog)
1876 ret = security_bpf_prog(prog);
1880 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1881 O_RDWR | O_CLOEXEC);
1884 static struct bpf_prog *____bpf_prog_get(struct fd f)
1887 return ERR_PTR(-EBADF);
1888 if (f.file->f_op != &bpf_prog_fops) {
1890 return ERR_PTR(-EINVAL);
1893 return f.file->private_data;
1896 void bpf_prog_add(struct bpf_prog *prog, int i)
1898 atomic64_add(i, &prog->aux->refcnt);
1900 EXPORT_SYMBOL_GPL(bpf_prog_add);
1902 void bpf_prog_sub(struct bpf_prog *prog, int i)
1904 /* Only to be used for undoing previous bpf_prog_add() in some
1905 * error path. We still know that another entity in our call
1906 * path holds a reference to the program, thus atomic_sub() can
1907 * be safely used in such cases!
1909 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1911 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1913 void bpf_prog_inc(struct bpf_prog *prog)
1915 atomic64_inc(&prog->aux->refcnt);
1917 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1919 /* prog_idr_lock should have been held */
1920 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1924 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1927 return ERR_PTR(-ENOENT);
1931 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1933 bool bpf_prog_get_ok(struct bpf_prog *prog,
1934 enum bpf_prog_type *attach_type, bool attach_drv)
1936 /* not an attachment, just a refcount inc, always allow */
1940 if (prog->type != *attach_type)
1942 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1948 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1951 struct fd f = fdget(ufd);
1952 struct bpf_prog *prog;
1954 prog = ____bpf_prog_get(f);
1957 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1958 prog = ERR_PTR(-EINVAL);
1968 struct bpf_prog *bpf_prog_get(u32 ufd)
1970 return __bpf_prog_get(ufd, NULL, false);
1973 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1976 return __bpf_prog_get(ufd, &type, attach_drv);
1978 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1980 /* Initially all BPF programs could be loaded w/o specifying
1981 * expected_attach_type. Later for some of them specifying expected_attach_type
1982 * at load time became required so that program could be validated properly.
1983 * Programs of types that are allowed to be loaded both w/ and w/o (for
1984 * backward compatibility) expected_attach_type, should have the default attach
1985 * type assigned to expected_attach_type for the latter case, so that it can be
1986 * validated later at attach time.
1988 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1989 * prog type requires it but has some attach types that have to be backward
1992 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1994 switch (attr->prog_type) {
1995 case BPF_PROG_TYPE_CGROUP_SOCK:
1996 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1997 * exist so checking for non-zero is the way to go here.
1999 if (!attr->expected_attach_type)
2000 attr->expected_attach_type =
2001 BPF_CGROUP_INET_SOCK_CREATE;
2003 case BPF_PROG_TYPE_SK_REUSEPORT:
2004 if (!attr->expected_attach_type)
2005 attr->expected_attach_type =
2006 BPF_SK_REUSEPORT_SELECT;
2012 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2013 enum bpf_attach_type expected_attach_type,
2014 struct btf *attach_btf, u32 btf_id,
2015 struct bpf_prog *dst_prog)
2018 if (btf_id > BTF_MAX_TYPE)
2021 if (!attach_btf && !dst_prog)
2024 switch (prog_type) {
2025 case BPF_PROG_TYPE_TRACING:
2026 case BPF_PROG_TYPE_LSM:
2027 case BPF_PROG_TYPE_STRUCT_OPS:
2028 case BPF_PROG_TYPE_EXT:
2035 if (attach_btf && (!btf_id || dst_prog))
2038 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
2039 prog_type != BPF_PROG_TYPE_EXT)
2042 switch (prog_type) {
2043 case BPF_PROG_TYPE_CGROUP_SOCK:
2044 switch (expected_attach_type) {
2045 case BPF_CGROUP_INET_SOCK_CREATE:
2046 case BPF_CGROUP_INET_SOCK_RELEASE:
2047 case BPF_CGROUP_INET4_POST_BIND:
2048 case BPF_CGROUP_INET6_POST_BIND:
2053 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2054 switch (expected_attach_type) {
2055 case BPF_CGROUP_INET4_BIND:
2056 case BPF_CGROUP_INET6_BIND:
2057 case BPF_CGROUP_INET4_CONNECT:
2058 case BPF_CGROUP_INET6_CONNECT:
2059 case BPF_CGROUP_INET4_GETPEERNAME:
2060 case BPF_CGROUP_INET6_GETPEERNAME:
2061 case BPF_CGROUP_INET4_GETSOCKNAME:
2062 case BPF_CGROUP_INET6_GETSOCKNAME:
2063 case BPF_CGROUP_UDP4_SENDMSG:
2064 case BPF_CGROUP_UDP6_SENDMSG:
2065 case BPF_CGROUP_UDP4_RECVMSG:
2066 case BPF_CGROUP_UDP6_RECVMSG:
2071 case BPF_PROG_TYPE_CGROUP_SKB:
2072 switch (expected_attach_type) {
2073 case BPF_CGROUP_INET_INGRESS:
2074 case BPF_CGROUP_INET_EGRESS:
2079 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2080 switch (expected_attach_type) {
2081 case BPF_CGROUP_SETSOCKOPT:
2082 case BPF_CGROUP_GETSOCKOPT:
2087 case BPF_PROG_TYPE_SK_LOOKUP:
2088 if (expected_attach_type == BPF_SK_LOOKUP)
2091 case BPF_PROG_TYPE_SK_REUSEPORT:
2092 switch (expected_attach_type) {
2093 case BPF_SK_REUSEPORT_SELECT:
2094 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2099 case BPF_PROG_TYPE_SYSCALL:
2100 case BPF_PROG_TYPE_EXT:
2101 if (expected_attach_type)
2109 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2111 switch (prog_type) {
2112 case BPF_PROG_TYPE_SCHED_CLS:
2113 case BPF_PROG_TYPE_SCHED_ACT:
2114 case BPF_PROG_TYPE_XDP:
2115 case BPF_PROG_TYPE_LWT_IN:
2116 case BPF_PROG_TYPE_LWT_OUT:
2117 case BPF_PROG_TYPE_LWT_XMIT:
2118 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2119 case BPF_PROG_TYPE_SK_SKB:
2120 case BPF_PROG_TYPE_SK_MSG:
2121 case BPF_PROG_TYPE_LIRC_MODE2:
2122 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2123 case BPF_PROG_TYPE_CGROUP_DEVICE:
2124 case BPF_PROG_TYPE_CGROUP_SOCK:
2125 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2126 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2127 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2128 case BPF_PROG_TYPE_SOCK_OPS:
2129 case BPF_PROG_TYPE_EXT: /* extends any prog */
2131 case BPF_PROG_TYPE_CGROUP_SKB:
2133 case BPF_PROG_TYPE_SK_REUSEPORT:
2134 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2140 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2142 switch (prog_type) {
2143 case BPF_PROG_TYPE_KPROBE:
2144 case BPF_PROG_TYPE_TRACEPOINT:
2145 case BPF_PROG_TYPE_PERF_EVENT:
2146 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2147 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2148 case BPF_PROG_TYPE_TRACING:
2149 case BPF_PROG_TYPE_LSM:
2150 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2151 case BPF_PROG_TYPE_EXT: /* extends any prog */
2158 /* last field in 'union bpf_attr' used by this command */
2159 #define BPF_PROG_LOAD_LAST_FIELD fd_array
2161 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
2163 enum bpf_prog_type type = attr->prog_type;
2164 struct bpf_prog *prog, *dst_prog = NULL;
2165 struct btf *attach_btf = NULL;
2170 if (CHECK_ATTR(BPF_PROG_LOAD))
2173 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2174 BPF_F_ANY_ALIGNMENT |
2175 BPF_F_TEST_STATE_FREQ |
2177 BPF_F_TEST_RND_HI32))
2180 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2181 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2185 /* copy eBPF program license from user space */
2186 if (strncpy_from_bpfptr(license,
2187 make_bpfptr(attr->license, uattr.is_kernel),
2188 sizeof(license) - 1) < 0)
2190 license[sizeof(license) - 1] = 0;
2192 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2193 is_gpl = license_is_gpl_compatible(license);
2195 if (attr->insn_cnt == 0 ||
2196 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2198 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2199 type != BPF_PROG_TYPE_CGROUP_SKB &&
2203 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2205 if (is_perfmon_prog_type(type) && !perfmon_capable())
2208 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2209 * or btf, we need to check which one it is
2211 if (attr->attach_prog_fd) {
2212 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2213 if (IS_ERR(dst_prog)) {
2215 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2216 if (IS_ERR(attach_btf))
2218 if (!btf_is_kernel(attach_btf)) {
2219 /* attaching through specifying bpf_prog's BTF
2220 * objects directly might be supported eventually
2222 btf_put(attach_btf);
2226 } else if (attr->attach_btf_id) {
2227 /* fall back to vmlinux BTF, if BTF type ID is specified */
2228 attach_btf = bpf_get_btf_vmlinux();
2229 if (IS_ERR(attach_btf))
2230 return PTR_ERR(attach_btf);
2233 btf_get(attach_btf);
2236 bpf_prog_load_fixup_attach_type(attr);
2237 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2238 attach_btf, attr->attach_btf_id,
2241 bpf_prog_put(dst_prog);
2243 btf_put(attach_btf);
2247 /* plain bpf_prog allocation */
2248 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2251 bpf_prog_put(dst_prog);
2253 btf_put(attach_btf);
2257 prog->expected_attach_type = attr->expected_attach_type;
2258 prog->aux->attach_btf = attach_btf;
2259 prog->aux->attach_btf_id = attr->attach_btf_id;
2260 prog->aux->dst_prog = dst_prog;
2261 prog->aux->offload_requested = !!attr->prog_ifindex;
2262 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2264 err = security_bpf_prog_alloc(prog->aux);
2268 prog->aux->user = get_current_user();
2269 prog->len = attr->insn_cnt;
2272 if (copy_from_bpfptr(prog->insns,
2273 make_bpfptr(attr->insns, uattr.is_kernel),
2274 bpf_prog_insn_size(prog)) != 0)
2277 prog->orig_prog = NULL;
2280 atomic64_set(&prog->aux->refcnt, 1);
2281 prog->gpl_compatible = is_gpl ? 1 : 0;
2283 if (bpf_prog_is_dev_bound(prog->aux)) {
2284 err = bpf_prog_offload_init(prog, attr);
2289 /* find program type: socket_filter vs tracing_filter */
2290 err = find_prog_type(type, prog);
2294 prog->aux->load_time = ktime_get_boottime_ns();
2295 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2296 sizeof(attr->prog_name));
2300 /* run eBPF verifier */
2301 err = bpf_check(&prog, attr, uattr);
2303 goto free_used_maps;
2305 prog = bpf_prog_select_runtime(prog, &err);
2307 goto free_used_maps;
2309 err = bpf_prog_alloc_id(prog);
2311 goto free_used_maps;
2313 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2314 * effectively publicly exposed. However, retrieving via
2315 * bpf_prog_get_fd_by_id() will take another reference,
2316 * therefore it cannot be gone underneath us.
2318 * Only for the time /after/ successful bpf_prog_new_fd()
2319 * and before returning to userspace, we might just hold
2320 * one reference and any parallel close on that fd could
2321 * rip everything out. Hence, below notifications must
2322 * happen before bpf_prog_new_fd().
2324 * Also, any failure handling from this point onwards must
2325 * be using bpf_prog_put() given the program is exposed.
2327 bpf_prog_kallsyms_add(prog);
2328 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2329 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2331 err = bpf_prog_new_fd(prog);
2337 /* In case we have subprogs, we need to wait for a grace
2338 * period before we can tear down JIT memory since symbols
2339 * are already exposed under kallsyms.
2341 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2344 free_uid(prog->aux->user);
2345 security_bpf_prog_free(prog->aux);
2347 if (prog->aux->attach_btf)
2348 btf_put(prog->aux->attach_btf);
2349 bpf_prog_free(prog);
2353 #define BPF_OBJ_LAST_FIELD file_flags
2355 static int bpf_obj_pin(const union bpf_attr *attr)
2357 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2360 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2363 static int bpf_obj_get(const union bpf_attr *attr)
2365 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2366 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2369 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2373 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2374 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2376 atomic64_set(&link->refcnt, 1);
2383 static void bpf_link_free_id(int id)
2388 spin_lock_bh(&link_idr_lock);
2389 idr_remove(&link_idr, id);
2390 spin_unlock_bh(&link_idr_lock);
2393 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2394 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2395 * anon_inode's release() call. This helper marksbpf_link as
2396 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2397 * is not decremented, it's the responsibility of a calling code that failed
2398 * to complete bpf_link initialization.
2400 void bpf_link_cleanup(struct bpf_link_primer *primer)
2402 primer->link->prog = NULL;
2403 bpf_link_free_id(primer->id);
2405 put_unused_fd(primer->fd);
2408 void bpf_link_inc(struct bpf_link *link)
2410 atomic64_inc(&link->refcnt);
2413 /* bpf_link_free is guaranteed to be called from process context */
2414 static void bpf_link_free(struct bpf_link *link)
2416 bpf_link_free_id(link->id);
2418 /* detach BPF program, clean up used resources */
2419 link->ops->release(link);
2420 bpf_prog_put(link->prog);
2422 /* free bpf_link and its containing memory */
2423 link->ops->dealloc(link);
2426 static void bpf_link_put_deferred(struct work_struct *work)
2428 struct bpf_link *link = container_of(work, struct bpf_link, work);
2430 bpf_link_free(link);
2433 /* bpf_link_put can be called from atomic context, but ensures that resources
2434 * are freed from process context
2436 void bpf_link_put(struct bpf_link *link)
2438 if (!atomic64_dec_and_test(&link->refcnt))
2442 INIT_WORK(&link->work, bpf_link_put_deferred);
2443 schedule_work(&link->work);
2445 bpf_link_free(link);
2449 static int bpf_link_release(struct inode *inode, struct file *filp)
2451 struct bpf_link *link = filp->private_data;
2457 #ifdef CONFIG_PROC_FS
2458 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2459 #define BPF_MAP_TYPE(_id, _ops)
2460 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2461 static const char *bpf_link_type_strs[] = {
2462 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2463 #include <linux/bpf_types.h>
2465 #undef BPF_PROG_TYPE
2467 #undef BPF_LINK_TYPE
2469 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2471 const struct bpf_link *link = filp->private_data;
2472 const struct bpf_prog *prog = link->prog;
2473 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2475 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2481 bpf_link_type_strs[link->type],
2485 if (link->ops->show_fdinfo)
2486 link->ops->show_fdinfo(link, m);
2490 static const struct file_operations bpf_link_fops = {
2491 #ifdef CONFIG_PROC_FS
2492 .show_fdinfo = bpf_link_show_fdinfo,
2494 .release = bpf_link_release,
2495 .read = bpf_dummy_read,
2496 .write = bpf_dummy_write,
2499 static int bpf_link_alloc_id(struct bpf_link *link)
2503 idr_preload(GFP_KERNEL);
2504 spin_lock_bh(&link_idr_lock);
2505 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2506 spin_unlock_bh(&link_idr_lock);
2512 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2513 * reserving unused FD and allocating ID from link_idr. This is to be paired
2514 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2515 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2516 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2517 * transient state is passed around in struct bpf_link_primer.
2518 * This is preferred way to create and initialize bpf_link, especially when
2519 * there are complicated and expensive operations inbetween creating bpf_link
2520 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2521 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2522 * expensive (and potentially failing) roll back operations in a rare case
2523 * that file, FD, or ID can't be allocated.
2525 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2530 fd = get_unused_fd_flags(O_CLOEXEC);
2535 id = bpf_link_alloc_id(link);
2541 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2543 bpf_link_free_id(id);
2545 return PTR_ERR(file);
2548 primer->link = link;
2549 primer->file = file;
2555 int bpf_link_settle(struct bpf_link_primer *primer)
2557 /* make bpf_link fetchable by ID */
2558 spin_lock_bh(&link_idr_lock);
2559 primer->link->id = primer->id;
2560 spin_unlock_bh(&link_idr_lock);
2561 /* make bpf_link fetchable by FD */
2562 fd_install(primer->fd, primer->file);
2563 /* pass through installed FD */
2567 int bpf_link_new_fd(struct bpf_link *link)
2569 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2572 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2574 struct fd f = fdget(ufd);
2575 struct bpf_link *link;
2578 return ERR_PTR(-EBADF);
2579 if (f.file->f_op != &bpf_link_fops) {
2581 return ERR_PTR(-EINVAL);
2584 link = f.file->private_data;
2591 struct bpf_tracing_link {
2592 struct bpf_link link;
2593 enum bpf_attach_type attach_type;
2594 struct bpf_trampoline *trampoline;
2595 struct bpf_prog *tgt_prog;
2598 static void bpf_tracing_link_release(struct bpf_link *link)
2600 struct bpf_tracing_link *tr_link =
2601 container_of(link, struct bpf_tracing_link, link);
2603 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2604 tr_link->trampoline));
2606 bpf_trampoline_put(tr_link->trampoline);
2608 /* tgt_prog is NULL if target is a kernel function */
2609 if (tr_link->tgt_prog)
2610 bpf_prog_put(tr_link->tgt_prog);
2613 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2615 struct bpf_tracing_link *tr_link =
2616 container_of(link, struct bpf_tracing_link, link);
2621 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2622 struct seq_file *seq)
2624 struct bpf_tracing_link *tr_link =
2625 container_of(link, struct bpf_tracing_link, link);
2628 "attach_type:\t%d\n",
2629 tr_link->attach_type);
2632 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2633 struct bpf_link_info *info)
2635 struct bpf_tracing_link *tr_link =
2636 container_of(link, struct bpf_tracing_link, link);
2638 info->tracing.attach_type = tr_link->attach_type;
2639 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2640 &info->tracing.target_obj_id,
2641 &info->tracing.target_btf_id);
2646 static const struct bpf_link_ops bpf_tracing_link_lops = {
2647 .release = bpf_tracing_link_release,
2648 .dealloc = bpf_tracing_link_dealloc,
2649 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2650 .fill_link_info = bpf_tracing_link_fill_link_info,
2653 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2657 struct bpf_link_primer link_primer;
2658 struct bpf_prog *tgt_prog = NULL;
2659 struct bpf_trampoline *tr = NULL;
2660 struct bpf_tracing_link *link;
2664 switch (prog->type) {
2665 case BPF_PROG_TYPE_TRACING:
2666 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2667 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2668 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2673 case BPF_PROG_TYPE_EXT:
2674 if (prog->expected_attach_type != 0) {
2679 case BPF_PROG_TYPE_LSM:
2680 if (prog->expected_attach_type != BPF_LSM_MAC) {
2690 if (!!tgt_prog_fd != !!btf_id) {
2696 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2697 if (prog->type != BPF_PROG_TYPE_EXT) {
2702 tgt_prog = bpf_prog_get(tgt_prog_fd);
2703 if (IS_ERR(tgt_prog)) {
2704 err = PTR_ERR(tgt_prog);
2709 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
2712 link = kzalloc(sizeof(*link), GFP_USER);
2717 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2718 &bpf_tracing_link_lops, prog);
2719 link->attach_type = prog->expected_attach_type;
2721 mutex_lock(&prog->aux->dst_mutex);
2723 /* There are a few possible cases here:
2725 * - if prog->aux->dst_trampoline is set, the program was just loaded
2726 * and not yet attached to anything, so we can use the values stored
2729 * - if prog->aux->dst_trampoline is NULL, the program has already been
2730 * attached to a target and its initial target was cleared (below)
2732 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2733 * target_btf_id using the link_create API.
2735 * - if tgt_prog == NULL when this function was called using the old
2736 * raw_tracepoint_open API, and we need a target from prog->aux
2738 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
2739 * was detached and is going for re-attachment.
2741 if (!prog->aux->dst_trampoline && !tgt_prog) {
2743 * Allow re-attach for TRACING and LSM programs. If it's
2744 * currently linked, bpf_trampoline_link_prog will fail.
2745 * EXT programs need to specify tgt_prog_fd, so they
2746 * re-attach in separate code path.
2748 if (prog->type != BPF_PROG_TYPE_TRACING &&
2749 prog->type != BPF_PROG_TYPE_LSM) {
2753 btf_id = prog->aux->attach_btf_id;
2754 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
2757 if (!prog->aux->dst_trampoline ||
2758 (key && key != prog->aux->dst_trampoline->key)) {
2759 /* If there is no saved target, or the specified target is
2760 * different from the destination specified at load time, we
2761 * need a new trampoline and a check for compatibility
2763 struct bpf_attach_target_info tgt_info = {};
2765 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2770 tr = bpf_trampoline_get(key, &tgt_info);
2776 /* The caller didn't specify a target, or the target was the
2777 * same as the destination supplied during program load. This
2778 * means we can reuse the trampoline and reference from program
2779 * load time, and there is no need to allocate a new one. This
2780 * can only happen once for any program, as the saved values in
2781 * prog->aux are cleared below.
2783 tr = prog->aux->dst_trampoline;
2784 tgt_prog = prog->aux->dst_prog;
2787 err = bpf_link_prime(&link->link, &link_primer);
2791 err = bpf_trampoline_link_prog(prog, tr);
2793 bpf_link_cleanup(&link_primer);
2798 link->tgt_prog = tgt_prog;
2799 link->trampoline = tr;
2801 /* Always clear the trampoline and target prog from prog->aux to make
2802 * sure the original attach destination is not kept alive after a
2803 * program is (re-)attached to another target.
2805 if (prog->aux->dst_prog &&
2806 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2807 /* got extra prog ref from syscall, or attaching to different prog */
2808 bpf_prog_put(prog->aux->dst_prog);
2809 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2810 /* we allocated a new trampoline, so free the old one */
2811 bpf_trampoline_put(prog->aux->dst_trampoline);
2813 prog->aux->dst_prog = NULL;
2814 prog->aux->dst_trampoline = NULL;
2815 mutex_unlock(&prog->aux->dst_mutex);
2817 return bpf_link_settle(&link_primer);
2819 if (tr && tr != prog->aux->dst_trampoline)
2820 bpf_trampoline_put(tr);
2821 mutex_unlock(&prog->aux->dst_mutex);
2824 if (tgt_prog_fd && tgt_prog)
2825 bpf_prog_put(tgt_prog);
2829 struct bpf_raw_tp_link {
2830 struct bpf_link link;
2831 struct bpf_raw_event_map *btp;
2834 static void bpf_raw_tp_link_release(struct bpf_link *link)
2836 struct bpf_raw_tp_link *raw_tp =
2837 container_of(link, struct bpf_raw_tp_link, link);
2839 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2840 bpf_put_raw_tracepoint(raw_tp->btp);
2843 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2845 struct bpf_raw_tp_link *raw_tp =
2846 container_of(link, struct bpf_raw_tp_link, link);
2851 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2852 struct seq_file *seq)
2854 struct bpf_raw_tp_link *raw_tp_link =
2855 container_of(link, struct bpf_raw_tp_link, link);
2859 raw_tp_link->btp->tp->name);
2862 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2863 struct bpf_link_info *info)
2865 struct bpf_raw_tp_link *raw_tp_link =
2866 container_of(link, struct bpf_raw_tp_link, link);
2867 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2868 const char *tp_name = raw_tp_link->btp->tp->name;
2869 u32 ulen = info->raw_tracepoint.tp_name_len;
2870 size_t tp_len = strlen(tp_name);
2875 info->raw_tracepoint.tp_name_len = tp_len + 1;
2880 if (ulen >= tp_len + 1) {
2881 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2886 if (copy_to_user(ubuf, tp_name, ulen - 1))
2888 if (put_user(zero, ubuf + ulen - 1))
2896 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
2897 .release = bpf_raw_tp_link_release,
2898 .dealloc = bpf_raw_tp_link_dealloc,
2899 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2900 .fill_link_info = bpf_raw_tp_link_fill_link_info,
2903 #ifdef CONFIG_PERF_EVENTS
2904 struct bpf_perf_link {
2905 struct bpf_link link;
2906 struct file *perf_file;
2909 static void bpf_perf_link_release(struct bpf_link *link)
2911 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
2912 struct perf_event *event = perf_link->perf_file->private_data;
2914 perf_event_free_bpf_prog(event);
2915 fput(perf_link->perf_file);
2918 static void bpf_perf_link_dealloc(struct bpf_link *link)
2920 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
2925 static const struct bpf_link_ops bpf_perf_link_lops = {
2926 .release = bpf_perf_link_release,
2927 .dealloc = bpf_perf_link_dealloc,
2930 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2932 struct bpf_link_primer link_primer;
2933 struct bpf_perf_link *link;
2934 struct perf_event *event;
2935 struct file *perf_file;
2938 if (attr->link_create.flags)
2941 perf_file = perf_event_get(attr->link_create.target_fd);
2942 if (IS_ERR(perf_file))
2943 return PTR_ERR(perf_file);
2945 link = kzalloc(sizeof(*link), GFP_USER);
2950 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog);
2951 link->perf_file = perf_file;
2953 err = bpf_link_prime(&link->link, &link_primer);
2959 event = perf_file->private_data;
2960 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
2962 bpf_link_cleanup(&link_primer);
2965 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
2968 return bpf_link_settle(&link_primer);
2974 #endif /* CONFIG_PERF_EVENTS */
2976 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2978 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2980 struct bpf_link_primer link_primer;
2981 struct bpf_raw_tp_link *link;
2982 struct bpf_raw_event_map *btp;
2983 struct bpf_prog *prog;
2984 const char *tp_name;
2988 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2991 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2993 return PTR_ERR(prog);
2995 switch (prog->type) {
2996 case BPF_PROG_TYPE_TRACING:
2997 case BPF_PROG_TYPE_EXT:
2998 case BPF_PROG_TYPE_LSM:
2999 if (attr->raw_tracepoint.name) {
3000 /* The attach point for this category of programs
3001 * should be specified via btf_id during program load.
3006 if (prog->type == BPF_PROG_TYPE_TRACING &&
3007 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
3008 tp_name = prog->aux->attach_func_name;
3011 err = bpf_tracing_prog_attach(prog, 0, 0);
3015 case BPF_PROG_TYPE_RAW_TRACEPOINT:
3016 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
3017 if (strncpy_from_user(buf,
3018 u64_to_user_ptr(attr->raw_tracepoint.name),
3019 sizeof(buf) - 1) < 0) {
3023 buf[sizeof(buf) - 1] = 0;
3031 btp = bpf_get_raw_tracepoint(tp_name);
3037 link = kzalloc(sizeof(*link), GFP_USER);
3042 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
3043 &bpf_raw_tp_link_lops, prog);
3046 err = bpf_link_prime(&link->link, &link_primer);
3052 err = bpf_probe_register(link->btp, prog);
3054 bpf_link_cleanup(&link_primer);
3058 return bpf_link_settle(&link_primer);
3061 bpf_put_raw_tracepoint(btp);
3067 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
3068 enum bpf_attach_type attach_type)
3070 switch (prog->type) {
3071 case BPF_PROG_TYPE_CGROUP_SOCK:
3072 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3073 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3074 case BPF_PROG_TYPE_SK_LOOKUP:
3075 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
3076 case BPF_PROG_TYPE_CGROUP_SKB:
3077 if (!capable(CAP_NET_ADMIN))
3078 /* cg-skb progs can be loaded by unpriv user.
3079 * check permissions at attach time.
3082 return prog->enforce_expected_attach_type &&
3083 prog->expected_attach_type != attach_type ?
3090 static enum bpf_prog_type
3091 attach_type_to_prog_type(enum bpf_attach_type attach_type)
3093 switch (attach_type) {
3094 case BPF_CGROUP_INET_INGRESS:
3095 case BPF_CGROUP_INET_EGRESS:
3096 return BPF_PROG_TYPE_CGROUP_SKB;
3097 case BPF_CGROUP_INET_SOCK_CREATE:
3098 case BPF_CGROUP_INET_SOCK_RELEASE:
3099 case BPF_CGROUP_INET4_POST_BIND:
3100 case BPF_CGROUP_INET6_POST_BIND:
3101 return BPF_PROG_TYPE_CGROUP_SOCK;
3102 case BPF_CGROUP_INET4_BIND:
3103 case BPF_CGROUP_INET6_BIND:
3104 case BPF_CGROUP_INET4_CONNECT:
3105 case BPF_CGROUP_INET6_CONNECT:
3106 case BPF_CGROUP_INET4_GETPEERNAME:
3107 case BPF_CGROUP_INET6_GETPEERNAME:
3108 case BPF_CGROUP_INET4_GETSOCKNAME:
3109 case BPF_CGROUP_INET6_GETSOCKNAME:
3110 case BPF_CGROUP_UDP4_SENDMSG:
3111 case BPF_CGROUP_UDP6_SENDMSG:
3112 case BPF_CGROUP_UDP4_RECVMSG:
3113 case BPF_CGROUP_UDP6_RECVMSG:
3114 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3115 case BPF_CGROUP_SOCK_OPS:
3116 return BPF_PROG_TYPE_SOCK_OPS;
3117 case BPF_CGROUP_DEVICE:
3118 return BPF_PROG_TYPE_CGROUP_DEVICE;
3119 case BPF_SK_MSG_VERDICT:
3120 return BPF_PROG_TYPE_SK_MSG;
3121 case BPF_SK_SKB_STREAM_PARSER:
3122 case BPF_SK_SKB_STREAM_VERDICT:
3123 case BPF_SK_SKB_VERDICT:
3124 return BPF_PROG_TYPE_SK_SKB;
3125 case BPF_LIRC_MODE2:
3126 return BPF_PROG_TYPE_LIRC_MODE2;
3127 case BPF_FLOW_DISSECTOR:
3128 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3129 case BPF_CGROUP_SYSCTL:
3130 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3131 case BPF_CGROUP_GETSOCKOPT:
3132 case BPF_CGROUP_SETSOCKOPT:
3133 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
3134 case BPF_TRACE_ITER:
3135 return BPF_PROG_TYPE_TRACING;
3137 return BPF_PROG_TYPE_SK_LOOKUP;
3139 return BPF_PROG_TYPE_XDP;
3141 return BPF_PROG_TYPE_UNSPEC;
3145 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
3147 #define BPF_F_ATTACH_MASK \
3148 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
3150 static int bpf_prog_attach(const union bpf_attr *attr)
3152 enum bpf_prog_type ptype;
3153 struct bpf_prog *prog;
3156 if (CHECK_ATTR(BPF_PROG_ATTACH))
3159 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
3162 ptype = attach_type_to_prog_type(attr->attach_type);
3163 if (ptype == BPF_PROG_TYPE_UNSPEC)
3166 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3168 return PTR_ERR(prog);
3170 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3176 case BPF_PROG_TYPE_SK_SKB:
3177 case BPF_PROG_TYPE_SK_MSG:
3178 ret = sock_map_get_from_fd(attr, prog);
3180 case BPF_PROG_TYPE_LIRC_MODE2:
3181 ret = lirc_prog_attach(attr, prog);
3183 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3184 ret = netns_bpf_prog_attach(attr, prog);
3186 case BPF_PROG_TYPE_CGROUP_DEVICE:
3187 case BPF_PROG_TYPE_CGROUP_SKB:
3188 case BPF_PROG_TYPE_CGROUP_SOCK:
3189 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3190 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3191 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3192 case BPF_PROG_TYPE_SOCK_OPS:
3193 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3204 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3206 static int bpf_prog_detach(const union bpf_attr *attr)
3208 enum bpf_prog_type ptype;
3210 if (CHECK_ATTR(BPF_PROG_DETACH))
3213 ptype = attach_type_to_prog_type(attr->attach_type);
3216 case BPF_PROG_TYPE_SK_MSG:
3217 case BPF_PROG_TYPE_SK_SKB:
3218 return sock_map_prog_detach(attr, ptype);
3219 case BPF_PROG_TYPE_LIRC_MODE2:
3220 return lirc_prog_detach(attr);
3221 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3222 return netns_bpf_prog_detach(attr, ptype);
3223 case BPF_PROG_TYPE_CGROUP_DEVICE:
3224 case BPF_PROG_TYPE_CGROUP_SKB:
3225 case BPF_PROG_TYPE_CGROUP_SOCK:
3226 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3227 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3228 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3229 case BPF_PROG_TYPE_SOCK_OPS:
3230 return cgroup_bpf_prog_detach(attr, ptype);
3236 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3238 static int bpf_prog_query(const union bpf_attr *attr,
3239 union bpf_attr __user *uattr)
3241 if (!capable(CAP_NET_ADMIN))
3243 if (CHECK_ATTR(BPF_PROG_QUERY))
3245 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3248 switch (attr->query.attach_type) {
3249 case BPF_CGROUP_INET_INGRESS:
3250 case BPF_CGROUP_INET_EGRESS:
3251 case BPF_CGROUP_INET_SOCK_CREATE:
3252 case BPF_CGROUP_INET_SOCK_RELEASE:
3253 case BPF_CGROUP_INET4_BIND:
3254 case BPF_CGROUP_INET6_BIND:
3255 case BPF_CGROUP_INET4_POST_BIND:
3256 case BPF_CGROUP_INET6_POST_BIND:
3257 case BPF_CGROUP_INET4_CONNECT:
3258 case BPF_CGROUP_INET6_CONNECT:
3259 case BPF_CGROUP_INET4_GETPEERNAME:
3260 case BPF_CGROUP_INET6_GETPEERNAME:
3261 case BPF_CGROUP_INET4_GETSOCKNAME:
3262 case BPF_CGROUP_INET6_GETSOCKNAME:
3263 case BPF_CGROUP_UDP4_SENDMSG:
3264 case BPF_CGROUP_UDP6_SENDMSG:
3265 case BPF_CGROUP_UDP4_RECVMSG:
3266 case BPF_CGROUP_UDP6_RECVMSG:
3267 case BPF_CGROUP_SOCK_OPS:
3268 case BPF_CGROUP_DEVICE:
3269 case BPF_CGROUP_SYSCTL:
3270 case BPF_CGROUP_GETSOCKOPT:
3271 case BPF_CGROUP_SETSOCKOPT:
3272 return cgroup_bpf_prog_query(attr, uattr);
3273 case BPF_LIRC_MODE2:
3274 return lirc_prog_query(attr, uattr);
3275 case BPF_FLOW_DISSECTOR:
3277 return netns_bpf_prog_query(attr, uattr);
3283 #define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
3285 static int bpf_prog_test_run(const union bpf_attr *attr,
3286 union bpf_attr __user *uattr)
3288 struct bpf_prog *prog;
3289 int ret = -ENOTSUPP;
3291 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3294 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3295 (!attr->test.ctx_size_in && attr->test.ctx_in))
3298 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3299 (!attr->test.ctx_size_out && attr->test.ctx_out))
3302 prog = bpf_prog_get(attr->test.prog_fd);
3304 return PTR_ERR(prog);
3306 if (prog->aux->ops->test_run)
3307 ret = prog->aux->ops->test_run(prog, attr, uattr);
3313 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3315 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3316 union bpf_attr __user *uattr,
3320 u32 next_id = attr->start_id;
3323 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3326 if (!capable(CAP_SYS_ADMIN))
3331 if (!idr_get_next(idr, &next_id))
3333 spin_unlock_bh(lock);
3336 err = put_user(next_id, &uattr->next_id);
3341 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3343 struct bpf_map *map;
3345 spin_lock_bh(&map_idr_lock);
3347 map = idr_get_next(&map_idr, id);
3349 map = __bpf_map_inc_not_zero(map, false);
3355 spin_unlock_bh(&map_idr_lock);
3360 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3362 struct bpf_prog *prog;
3364 spin_lock_bh(&prog_idr_lock);
3366 prog = idr_get_next(&prog_idr, id);
3368 prog = bpf_prog_inc_not_zero(prog);
3374 spin_unlock_bh(&prog_idr_lock);
3379 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3381 struct bpf_prog *bpf_prog_by_id(u32 id)
3383 struct bpf_prog *prog;
3386 return ERR_PTR(-ENOENT);
3388 spin_lock_bh(&prog_idr_lock);
3389 prog = idr_find(&prog_idr, id);
3391 prog = bpf_prog_inc_not_zero(prog);
3393 prog = ERR_PTR(-ENOENT);
3394 spin_unlock_bh(&prog_idr_lock);
3398 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3400 struct bpf_prog *prog;
3401 u32 id = attr->prog_id;
3404 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3407 if (!capable(CAP_SYS_ADMIN))
3410 prog = bpf_prog_by_id(id);
3412 return PTR_ERR(prog);
3414 fd = bpf_prog_new_fd(prog);
3421 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3423 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3425 struct bpf_map *map;
3426 u32 id = attr->map_id;
3430 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3431 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3434 if (!capable(CAP_SYS_ADMIN))
3437 f_flags = bpf_get_file_flag(attr->open_flags);
3441 spin_lock_bh(&map_idr_lock);
3442 map = idr_find(&map_idr, id);
3444 map = __bpf_map_inc_not_zero(map, true);
3446 map = ERR_PTR(-ENOENT);
3447 spin_unlock_bh(&map_idr_lock);
3450 return PTR_ERR(map);
3452 fd = bpf_map_new_fd(map, f_flags);
3454 bpf_map_put_with_uref(map);
3459 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3460 unsigned long addr, u32 *off,
3463 const struct bpf_map *map;
3466 mutex_lock(&prog->aux->used_maps_mutex);
3467 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3468 map = prog->aux->used_maps[i];
3469 if (map == (void *)addr) {
3470 *type = BPF_PSEUDO_MAP_FD;
3473 if (!map->ops->map_direct_value_meta)
3475 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3476 *type = BPF_PSEUDO_MAP_VALUE;
3483 mutex_unlock(&prog->aux->used_maps_mutex);
3487 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3488 const struct cred *f_cred)
3490 const struct bpf_map *map;
3491 struct bpf_insn *insns;
3497 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3502 for (i = 0; i < prog->len; i++) {
3503 code = insns[i].code;
3505 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3506 insns[i].code = BPF_JMP | BPF_CALL;
3507 insns[i].imm = BPF_FUNC_tail_call;
3510 if (code == (BPF_JMP | BPF_CALL) ||
3511 code == (BPF_JMP | BPF_CALL_ARGS)) {
3512 if (code == (BPF_JMP | BPF_CALL_ARGS))
3513 insns[i].code = BPF_JMP | BPF_CALL;
3514 if (!bpf_dump_raw_ok(f_cred))
3518 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3519 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3523 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3526 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3527 map = bpf_map_from_imm(prog, imm, &off, &type);
3529 insns[i].src_reg = type;
3530 insns[i].imm = map->id;
3531 insns[i + 1].imm = off;
3539 static int set_info_rec_size(struct bpf_prog_info *info)
3542 * Ensure info.*_rec_size is the same as kernel expected size
3546 * Only allow zero *_rec_size if both _rec_size and _cnt are
3547 * zero. In this case, the kernel will set the expected
3548 * _rec_size back to the info.
3551 if ((info->nr_func_info || info->func_info_rec_size) &&
3552 info->func_info_rec_size != sizeof(struct bpf_func_info))
3555 if ((info->nr_line_info || info->line_info_rec_size) &&
3556 info->line_info_rec_size != sizeof(struct bpf_line_info))
3559 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3560 info->jited_line_info_rec_size != sizeof(__u64))
3563 info->func_info_rec_size = sizeof(struct bpf_func_info);
3564 info->line_info_rec_size = sizeof(struct bpf_line_info);
3565 info->jited_line_info_rec_size = sizeof(__u64);
3570 static int bpf_prog_get_info_by_fd(struct file *file,
3571 struct bpf_prog *prog,
3572 const union bpf_attr *attr,
3573 union bpf_attr __user *uattr)
3575 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3576 struct bpf_prog_info info;
3577 u32 info_len = attr->info.info_len;
3578 struct bpf_prog_stats stats;
3579 char __user *uinsns;
3583 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3586 info_len = min_t(u32, sizeof(info), info_len);
3588 memset(&info, 0, sizeof(info));
3589 if (copy_from_user(&info, uinfo, info_len))
3592 info.type = prog->type;
3593 info.id = prog->aux->id;
3594 info.load_time = prog->aux->load_time;
3595 info.created_by_uid = from_kuid_munged(current_user_ns(),
3596 prog->aux->user->uid);
3597 info.gpl_compatible = prog->gpl_compatible;
3599 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3600 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3602 mutex_lock(&prog->aux->used_maps_mutex);
3603 ulen = info.nr_map_ids;
3604 info.nr_map_ids = prog->aux->used_map_cnt;
3605 ulen = min_t(u32, info.nr_map_ids, ulen);
3607 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3610 for (i = 0; i < ulen; i++)
3611 if (put_user(prog->aux->used_maps[i]->id,
3612 &user_map_ids[i])) {
3613 mutex_unlock(&prog->aux->used_maps_mutex);
3617 mutex_unlock(&prog->aux->used_maps_mutex);
3619 err = set_info_rec_size(&info);
3623 bpf_prog_get_stats(prog, &stats);
3624 info.run_time_ns = stats.nsecs;
3625 info.run_cnt = stats.cnt;
3626 info.recursion_misses = stats.misses;
3628 if (!bpf_capable()) {
3629 info.jited_prog_len = 0;
3630 info.xlated_prog_len = 0;
3631 info.nr_jited_ksyms = 0;
3632 info.nr_jited_func_lens = 0;
3633 info.nr_func_info = 0;
3634 info.nr_line_info = 0;
3635 info.nr_jited_line_info = 0;
3639 ulen = info.xlated_prog_len;
3640 info.xlated_prog_len = bpf_prog_insn_size(prog);
3641 if (info.xlated_prog_len && ulen) {
3642 struct bpf_insn *insns_sanitized;
3645 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3646 info.xlated_prog_insns = 0;
3649 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3650 if (!insns_sanitized)
3652 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3653 ulen = min_t(u32, info.xlated_prog_len, ulen);
3654 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3655 kfree(insns_sanitized);
3660 if (bpf_prog_is_dev_bound(prog->aux)) {
3661 err = bpf_prog_offload_info_fill(&info, prog);
3667 /* NOTE: the following code is supposed to be skipped for offload.
3668 * bpf_prog_offload_info_fill() is the place to fill similar fields
3671 ulen = info.jited_prog_len;
3672 if (prog->aux->func_cnt) {
3675 info.jited_prog_len = 0;
3676 for (i = 0; i < prog->aux->func_cnt; i++)
3677 info.jited_prog_len += prog->aux->func[i]->jited_len;
3679 info.jited_prog_len = prog->jited_len;
3682 if (info.jited_prog_len && ulen) {
3683 if (bpf_dump_raw_ok(file->f_cred)) {
3684 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3685 ulen = min_t(u32, info.jited_prog_len, ulen);
3687 /* for multi-function programs, copy the JITed
3688 * instructions for all the functions
3690 if (prog->aux->func_cnt) {
3695 for (i = 0; i < prog->aux->func_cnt; i++) {
3696 len = prog->aux->func[i]->jited_len;
3697 len = min_t(u32, len, free);
3698 img = (u8 *) prog->aux->func[i]->bpf_func;
3699 if (copy_to_user(uinsns, img, len))
3707 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3711 info.jited_prog_insns = 0;
3715 ulen = info.nr_jited_ksyms;
3716 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
3718 if (bpf_dump_raw_ok(file->f_cred)) {
3719 unsigned long ksym_addr;
3720 u64 __user *user_ksyms;
3723 /* copy the address of the kernel symbol
3724 * corresponding to each function
3726 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3727 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3728 if (prog->aux->func_cnt) {
3729 for (i = 0; i < ulen; i++) {
3730 ksym_addr = (unsigned long)
3731 prog->aux->func[i]->bpf_func;
3732 if (put_user((u64) ksym_addr,
3737 ksym_addr = (unsigned long) prog->bpf_func;
3738 if (put_user((u64) ksym_addr, &user_ksyms[0]))
3742 info.jited_ksyms = 0;
3746 ulen = info.nr_jited_func_lens;
3747 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
3749 if (bpf_dump_raw_ok(file->f_cred)) {
3750 u32 __user *user_lens;
3753 /* copy the JITed image lengths for each function */
3754 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3755 user_lens = u64_to_user_ptr(info.jited_func_lens);
3756 if (prog->aux->func_cnt) {
3757 for (i = 0; i < ulen; i++) {
3759 prog->aux->func[i]->jited_len;
3760 if (put_user(func_len, &user_lens[i]))
3764 func_len = prog->jited_len;
3765 if (put_user(func_len, &user_lens[0]))
3769 info.jited_func_lens = 0;
3774 info.btf_id = btf_obj_id(prog->aux->btf);
3776 ulen = info.nr_func_info;
3777 info.nr_func_info = prog->aux->func_info_cnt;
3778 if (info.nr_func_info && ulen) {
3779 char __user *user_finfo;
3781 user_finfo = u64_to_user_ptr(info.func_info);
3782 ulen = min_t(u32, info.nr_func_info, ulen);
3783 if (copy_to_user(user_finfo, prog->aux->func_info,
3784 info.func_info_rec_size * ulen))
3788 ulen = info.nr_line_info;
3789 info.nr_line_info = prog->aux->nr_linfo;
3790 if (info.nr_line_info && ulen) {
3791 __u8 __user *user_linfo;
3793 user_linfo = u64_to_user_ptr(info.line_info);
3794 ulen = min_t(u32, info.nr_line_info, ulen);
3795 if (copy_to_user(user_linfo, prog->aux->linfo,
3796 info.line_info_rec_size * ulen))
3800 ulen = info.nr_jited_line_info;
3801 if (prog->aux->jited_linfo)
3802 info.nr_jited_line_info = prog->aux->nr_linfo;
3804 info.nr_jited_line_info = 0;
3805 if (info.nr_jited_line_info && ulen) {
3806 if (bpf_dump_raw_ok(file->f_cred)) {
3807 __u64 __user *user_linfo;
3810 user_linfo = u64_to_user_ptr(info.jited_line_info);
3811 ulen = min_t(u32, info.nr_jited_line_info, ulen);
3812 for (i = 0; i < ulen; i++) {
3813 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3818 info.jited_line_info = 0;
3822 ulen = info.nr_prog_tags;
3823 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3825 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3828 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3829 ulen = min_t(u32, info.nr_prog_tags, ulen);
3830 if (prog->aux->func_cnt) {
3831 for (i = 0; i < ulen; i++) {
3832 if (copy_to_user(user_prog_tags[i],
3833 prog->aux->func[i]->tag,
3838 if (copy_to_user(user_prog_tags[0],
3839 prog->tag, BPF_TAG_SIZE))
3845 if (copy_to_user(uinfo, &info, info_len) ||
3846 put_user(info_len, &uattr->info.info_len))
3852 static int bpf_map_get_info_by_fd(struct file *file,
3853 struct bpf_map *map,
3854 const union bpf_attr *attr,
3855 union bpf_attr __user *uattr)
3857 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3858 struct bpf_map_info info;
3859 u32 info_len = attr->info.info_len;
3862 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3865 info_len = min_t(u32, sizeof(info), info_len);
3867 memset(&info, 0, sizeof(info));
3868 info.type = map->map_type;
3870 info.key_size = map->key_size;
3871 info.value_size = map->value_size;
3872 info.max_entries = map->max_entries;
3873 info.map_flags = map->map_flags;
3874 memcpy(info.name, map->name, sizeof(map->name));
3877 info.btf_id = btf_obj_id(map->btf);
3878 info.btf_key_type_id = map->btf_key_type_id;
3879 info.btf_value_type_id = map->btf_value_type_id;
3881 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
3883 if (bpf_map_is_dev_bound(map)) {
3884 err = bpf_map_offload_info_fill(&info, map);
3889 if (copy_to_user(uinfo, &info, info_len) ||
3890 put_user(info_len, &uattr->info.info_len))
3896 static int bpf_btf_get_info_by_fd(struct file *file,
3898 const union bpf_attr *attr,
3899 union bpf_attr __user *uattr)
3901 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3902 u32 info_len = attr->info.info_len;
3905 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
3909 return btf_get_info_by_fd(btf, attr, uattr);
3912 static int bpf_link_get_info_by_fd(struct file *file,
3913 struct bpf_link *link,
3914 const union bpf_attr *attr,
3915 union bpf_attr __user *uattr)
3917 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3918 struct bpf_link_info info;
3919 u32 info_len = attr->info.info_len;
3922 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3925 info_len = min_t(u32, sizeof(info), info_len);
3927 memset(&info, 0, sizeof(info));
3928 if (copy_from_user(&info, uinfo, info_len))
3931 info.type = link->type;
3933 info.prog_id = link->prog->aux->id;
3935 if (link->ops->fill_link_info) {
3936 err = link->ops->fill_link_info(link, &info);
3941 if (copy_to_user(uinfo, &info, info_len) ||
3942 put_user(info_len, &uattr->info.info_len))
3949 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3951 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3952 union bpf_attr __user *uattr)
3954 int ufd = attr->info.bpf_fd;
3958 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3965 if (f.file->f_op == &bpf_prog_fops)
3966 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
3968 else if (f.file->f_op == &bpf_map_fops)
3969 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
3971 else if (f.file->f_op == &btf_fops)
3972 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
3973 else if (f.file->f_op == &bpf_link_fops)
3974 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
3983 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3985 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
3987 if (CHECK_ATTR(BPF_BTF_LOAD))
3993 return btf_new_fd(attr, uattr);
3996 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3998 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
4000 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
4003 if (!capable(CAP_SYS_ADMIN))
4006 return btf_get_fd_by_id(attr->btf_id);
4009 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
4010 union bpf_attr __user *uattr,
4011 u32 prog_id, u32 fd_type,
4012 const char *buf, u64 probe_offset,
4015 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
4016 u32 len = buf ? strlen(buf) : 0, input_len;
4019 if (put_user(len, &uattr->task_fd_query.buf_len))
4021 input_len = attr->task_fd_query.buf_len;
4022 if (input_len && ubuf) {
4024 /* nothing to copy, just make ubuf NULL terminated */
4027 if (put_user(zero, ubuf))
4029 } else if (input_len >= len + 1) {
4030 /* ubuf can hold the string with NULL terminator */
4031 if (copy_to_user(ubuf, buf, len + 1))
4034 /* ubuf cannot hold the string with NULL terminator,
4035 * do a partial copy with NULL terminator.
4040 if (copy_to_user(ubuf, buf, input_len - 1))
4042 if (put_user(zero, ubuf + input_len - 1))
4047 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
4048 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
4049 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
4050 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
4056 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
4058 static int bpf_task_fd_query(const union bpf_attr *attr,
4059 union bpf_attr __user *uattr)
4061 pid_t pid = attr->task_fd_query.pid;
4062 u32 fd = attr->task_fd_query.fd;
4063 const struct perf_event *event;
4064 struct task_struct *task;
4068 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
4071 if (!capable(CAP_SYS_ADMIN))
4074 if (attr->task_fd_query.flags != 0)
4077 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
4082 file = fget_task(task, fd);
4083 put_task_struct(task);
4087 if (file->f_op == &bpf_link_fops) {
4088 struct bpf_link *link = file->private_data;
4090 if (link->ops == &bpf_raw_tp_link_lops) {
4091 struct bpf_raw_tp_link *raw_tp =
4092 container_of(link, struct bpf_raw_tp_link, link);
4093 struct bpf_raw_event_map *btp = raw_tp->btp;
4095 err = bpf_task_fd_query_copy(attr, uattr,
4096 raw_tp->link.prog->aux->id,
4097 BPF_FD_TYPE_RAW_TRACEPOINT,
4098 btp->tp->name, 0, 0);
4104 event = perf_get_event(file);
4105 if (!IS_ERR(event)) {
4106 u64 probe_offset, probe_addr;
4107 u32 prog_id, fd_type;
4110 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4111 &buf, &probe_offset,
4114 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4128 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
4130 #define BPF_DO_BATCH(fn) \
4136 err = fn(map, attr, uattr); \
4139 static int bpf_map_do_batch(const union bpf_attr *attr,
4140 union bpf_attr __user *uattr,
4143 struct bpf_map *map;
4147 if (CHECK_ATTR(BPF_MAP_BATCH))
4150 ufd = attr->batch.map_fd;
4152 map = __bpf_map_get(f);
4154 return PTR_ERR(map);
4156 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
4157 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
4158 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4163 if (cmd != BPF_MAP_LOOKUP_BATCH &&
4164 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4169 if (cmd == BPF_MAP_LOOKUP_BATCH)
4170 BPF_DO_BATCH(map->ops->map_lookup_batch);
4171 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4172 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4173 else if (cmd == BPF_MAP_UPDATE_BATCH)
4174 BPF_DO_BATCH(map->ops->map_update_batch);
4176 BPF_DO_BATCH(map->ops->map_delete_batch);
4183 static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
4184 struct bpf_prog *prog)
4186 if (attr->link_create.attach_type != prog->expected_attach_type)
4189 if (prog->expected_attach_type == BPF_TRACE_ITER)
4190 return bpf_iter_link_attach(attr, uattr, prog);
4191 else if (prog->type == BPF_PROG_TYPE_EXT)
4192 return bpf_tracing_prog_attach(prog,
4193 attr->link_create.target_fd,
4194 attr->link_create.target_btf_id);
4198 #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
4199 static int link_create(union bpf_attr *attr, bpfptr_t uattr)
4201 enum bpf_prog_type ptype;
4202 struct bpf_prog *prog;
4205 if (CHECK_ATTR(BPF_LINK_CREATE))
4208 prog = bpf_prog_get(attr->link_create.prog_fd);
4210 return PTR_ERR(prog);
4212 ret = bpf_prog_attach_check_attach_type(prog,
4213 attr->link_create.attach_type);
4217 switch (prog->type) {
4218 case BPF_PROG_TYPE_EXT:
4219 ret = tracing_bpf_link_attach(attr, uattr, prog);
4221 case BPF_PROG_TYPE_PERF_EVENT:
4222 case BPF_PROG_TYPE_KPROBE:
4223 case BPF_PROG_TYPE_TRACEPOINT:
4224 if (attr->link_create.attach_type != BPF_PERF_EVENT) {
4231 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4232 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4240 case BPF_PROG_TYPE_CGROUP_SKB:
4241 case BPF_PROG_TYPE_CGROUP_SOCK:
4242 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4243 case BPF_PROG_TYPE_SOCK_OPS:
4244 case BPF_PROG_TYPE_CGROUP_DEVICE:
4245 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4246 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4247 ret = cgroup_bpf_link_attach(attr, prog);
4249 case BPF_PROG_TYPE_TRACING:
4250 ret = tracing_bpf_link_attach(attr, uattr, prog);
4252 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4253 case BPF_PROG_TYPE_SK_LOOKUP:
4254 ret = netns_bpf_link_create(attr, prog);
4257 case BPF_PROG_TYPE_XDP:
4258 ret = bpf_xdp_link_attach(attr, prog);
4261 #ifdef CONFIG_PERF_EVENTS
4262 case BPF_PROG_TYPE_PERF_EVENT:
4263 case BPF_PROG_TYPE_TRACEPOINT:
4264 case BPF_PROG_TYPE_KPROBE:
4265 ret = bpf_perf_link_attach(attr, prog);
4278 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4280 static int link_update(union bpf_attr *attr)
4282 struct bpf_prog *old_prog = NULL, *new_prog;
4283 struct bpf_link *link;
4287 if (CHECK_ATTR(BPF_LINK_UPDATE))
4290 flags = attr->link_update.flags;
4291 if (flags & ~BPF_F_REPLACE)
4294 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4296 return PTR_ERR(link);
4298 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4299 if (IS_ERR(new_prog)) {
4300 ret = PTR_ERR(new_prog);
4304 if (flags & BPF_F_REPLACE) {
4305 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4306 if (IS_ERR(old_prog)) {
4307 ret = PTR_ERR(old_prog);
4311 } else if (attr->link_update.old_prog_fd) {
4316 if (link->ops->update_prog)
4317 ret = link->ops->update_prog(link, new_prog, old_prog);
4323 bpf_prog_put(old_prog);
4325 bpf_prog_put(new_prog);
4331 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4333 static int link_detach(union bpf_attr *attr)
4335 struct bpf_link *link;
4338 if (CHECK_ATTR(BPF_LINK_DETACH))
4341 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4343 return PTR_ERR(link);
4345 if (link->ops->detach)
4346 ret = link->ops->detach(link);
4354 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4356 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4359 struct bpf_link *bpf_link_by_id(u32 id)
4361 struct bpf_link *link;
4364 return ERR_PTR(-ENOENT);
4366 spin_lock_bh(&link_idr_lock);
4367 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4368 link = idr_find(&link_idr, id);
4371 link = bpf_link_inc_not_zero(link);
4373 link = ERR_PTR(-EAGAIN);
4375 link = ERR_PTR(-ENOENT);
4377 spin_unlock_bh(&link_idr_lock);
4381 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4383 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4385 struct bpf_link *link;
4386 u32 id = attr->link_id;
4389 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4392 if (!capable(CAP_SYS_ADMIN))
4395 link = bpf_link_by_id(id);
4397 return PTR_ERR(link);
4399 fd = bpf_link_new_fd(link);
4406 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4408 static int bpf_stats_release(struct inode *inode, struct file *file)
4410 mutex_lock(&bpf_stats_enabled_mutex);
4411 static_key_slow_dec(&bpf_stats_enabled_key.key);
4412 mutex_unlock(&bpf_stats_enabled_mutex);
4416 static const struct file_operations bpf_stats_fops = {
4417 .release = bpf_stats_release,
4420 static int bpf_enable_runtime_stats(void)
4424 mutex_lock(&bpf_stats_enabled_mutex);
4426 /* Set a very high limit to avoid overflow */
4427 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4428 mutex_unlock(&bpf_stats_enabled_mutex);
4432 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4434 static_key_slow_inc(&bpf_stats_enabled_key.key);
4436 mutex_unlock(&bpf_stats_enabled_mutex);
4440 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4442 static int bpf_enable_stats(union bpf_attr *attr)
4445 if (CHECK_ATTR(BPF_ENABLE_STATS))
4448 if (!capable(CAP_SYS_ADMIN))
4451 switch (attr->enable_stats.type) {
4452 case BPF_STATS_RUN_TIME:
4453 return bpf_enable_runtime_stats();
4460 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4462 static int bpf_iter_create(union bpf_attr *attr)
4464 struct bpf_link *link;
4467 if (CHECK_ATTR(BPF_ITER_CREATE))
4470 if (attr->iter_create.flags)
4473 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4475 return PTR_ERR(link);
4477 err = bpf_iter_new_fd(link);
4483 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4485 static int bpf_prog_bind_map(union bpf_attr *attr)
4487 struct bpf_prog *prog;
4488 struct bpf_map *map;
4489 struct bpf_map **used_maps_old, **used_maps_new;
4492 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4495 if (attr->prog_bind_map.flags)
4498 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4500 return PTR_ERR(prog);
4502 map = bpf_map_get(attr->prog_bind_map.map_fd);
4508 mutex_lock(&prog->aux->used_maps_mutex);
4510 used_maps_old = prog->aux->used_maps;
4512 for (i = 0; i < prog->aux->used_map_cnt; i++)
4513 if (used_maps_old[i] == map) {
4518 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4519 sizeof(used_maps_new[0]),
4521 if (!used_maps_new) {
4526 memcpy(used_maps_new, used_maps_old,
4527 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4528 used_maps_new[prog->aux->used_map_cnt] = map;
4530 prog->aux->used_map_cnt++;
4531 prog->aux->used_maps = used_maps_new;
4533 kfree(used_maps_old);
4536 mutex_unlock(&prog->aux->used_maps_mutex);
4545 static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
4547 union bpf_attr attr;
4550 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
4553 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4556 size = min_t(u32, size, sizeof(attr));
4558 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4559 memset(&attr, 0, sizeof(attr));
4560 if (copy_from_bpfptr(&attr, uattr, size) != 0)
4563 err = security_bpf(cmd, &attr, size);
4568 case BPF_MAP_CREATE:
4569 err = map_create(&attr);
4571 case BPF_MAP_LOOKUP_ELEM:
4572 err = map_lookup_elem(&attr);
4574 case BPF_MAP_UPDATE_ELEM:
4575 err = map_update_elem(&attr, uattr);
4577 case BPF_MAP_DELETE_ELEM:
4578 err = map_delete_elem(&attr);
4580 case BPF_MAP_GET_NEXT_KEY:
4581 err = map_get_next_key(&attr);
4583 case BPF_MAP_FREEZE:
4584 err = map_freeze(&attr);
4587 err = bpf_prog_load(&attr, uattr);
4590 err = bpf_obj_pin(&attr);
4593 err = bpf_obj_get(&attr);
4595 case BPF_PROG_ATTACH:
4596 err = bpf_prog_attach(&attr);
4598 case BPF_PROG_DETACH:
4599 err = bpf_prog_detach(&attr);
4601 case BPF_PROG_QUERY:
4602 err = bpf_prog_query(&attr, uattr.user);
4604 case BPF_PROG_TEST_RUN:
4605 err = bpf_prog_test_run(&attr, uattr.user);
4607 case BPF_PROG_GET_NEXT_ID:
4608 err = bpf_obj_get_next_id(&attr, uattr.user,
4609 &prog_idr, &prog_idr_lock);
4611 case BPF_MAP_GET_NEXT_ID:
4612 err = bpf_obj_get_next_id(&attr, uattr.user,
4613 &map_idr, &map_idr_lock);
4615 case BPF_BTF_GET_NEXT_ID:
4616 err = bpf_obj_get_next_id(&attr, uattr.user,
4617 &btf_idr, &btf_idr_lock);
4619 case BPF_PROG_GET_FD_BY_ID:
4620 err = bpf_prog_get_fd_by_id(&attr);
4622 case BPF_MAP_GET_FD_BY_ID:
4623 err = bpf_map_get_fd_by_id(&attr);
4625 case BPF_OBJ_GET_INFO_BY_FD:
4626 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
4628 case BPF_RAW_TRACEPOINT_OPEN:
4629 err = bpf_raw_tracepoint_open(&attr);
4632 err = bpf_btf_load(&attr, uattr);
4634 case BPF_BTF_GET_FD_BY_ID:
4635 err = bpf_btf_get_fd_by_id(&attr);
4637 case BPF_TASK_FD_QUERY:
4638 err = bpf_task_fd_query(&attr, uattr.user);
4640 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4641 err = map_lookup_and_delete_elem(&attr);
4643 case BPF_MAP_LOOKUP_BATCH:
4644 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
4646 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4647 err = bpf_map_do_batch(&attr, uattr.user,
4648 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4650 case BPF_MAP_UPDATE_BATCH:
4651 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
4653 case BPF_MAP_DELETE_BATCH:
4654 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
4656 case BPF_LINK_CREATE:
4657 err = link_create(&attr, uattr);
4659 case BPF_LINK_UPDATE:
4660 err = link_update(&attr);
4662 case BPF_LINK_GET_FD_BY_ID:
4663 err = bpf_link_get_fd_by_id(&attr);
4665 case BPF_LINK_GET_NEXT_ID:
4666 err = bpf_obj_get_next_id(&attr, uattr.user,
4667 &link_idr, &link_idr_lock);
4669 case BPF_ENABLE_STATS:
4670 err = bpf_enable_stats(&attr);
4672 case BPF_ITER_CREATE:
4673 err = bpf_iter_create(&attr);
4675 case BPF_LINK_DETACH:
4676 err = link_detach(&attr);
4678 case BPF_PROG_BIND_MAP:
4679 err = bpf_prog_bind_map(&attr);
4689 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4691 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
4694 static bool syscall_prog_is_valid_access(int off, int size,
4695 enum bpf_access_type type,
4696 const struct bpf_prog *prog,
4697 struct bpf_insn_access_aux *info)
4699 if (off < 0 || off >= U16_MAX)
4701 if (off % size != 0)
4706 BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size)
4709 case BPF_MAP_CREATE:
4710 case BPF_MAP_UPDATE_ELEM:
4711 case BPF_MAP_FREEZE:
4715 /* case BPF_PROG_TEST_RUN:
4716 * is not part of this list to prevent recursive test_run
4721 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
4724 static const struct bpf_func_proto bpf_sys_bpf_proto = {
4725 .func = bpf_sys_bpf,
4727 .ret_type = RET_INTEGER,
4728 .arg1_type = ARG_ANYTHING,
4729 .arg2_type = ARG_PTR_TO_MEM,
4730 .arg3_type = ARG_CONST_SIZE,
4733 const struct bpf_func_proto * __weak
4734 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4736 return bpf_base_func_proto(func_id);
4739 BPF_CALL_1(bpf_sys_close, u32, fd)
4741 /* When bpf program calls this helper there should not be
4742 * an fdget() without matching completed fdput().
4743 * This helper is allowed in the following callchain only:
4744 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
4746 return close_fd(fd);
4749 static const struct bpf_func_proto bpf_sys_close_proto = {
4750 .func = bpf_sys_close,
4752 .ret_type = RET_INTEGER,
4753 .arg1_type = ARG_ANYTHING,
4756 static const struct bpf_func_proto *
4757 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4760 case BPF_FUNC_sys_bpf:
4761 return &bpf_sys_bpf_proto;
4762 case BPF_FUNC_btf_find_by_name_kind:
4763 return &bpf_btf_find_by_name_kind_proto;
4764 case BPF_FUNC_sys_close:
4765 return &bpf_sys_close_proto;
4767 return tracing_prog_func_proto(func_id, prog);
4771 const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
4772 .get_func_proto = syscall_prog_func_proto,
4773 .is_valid_access = syscall_prog_is_valid_access,
4776 const struct bpf_prog_ops bpf_syscall_prog_ops = {
4777 .test_run = bpf_prog_test_run_syscall,