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 memdup_user(ukey, key_size);
1019 return ERR_PTR(-EINVAL);
1024 static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1027 return memdup_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 = kmalloc(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 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1141 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
1142 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1143 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
1144 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1146 value_size = map->value_size;
1149 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1154 if (copy_from_bpfptr(value, uvalue, value_size) != 0)
1157 err = bpf_map_update_value(map, f, key, value, attr->flags);
1168 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1170 static int map_delete_elem(union bpf_attr *attr)
1172 void __user *ukey = u64_to_user_ptr(attr->key);
1173 int ufd = attr->map_fd;
1174 struct bpf_map *map;
1179 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1183 map = __bpf_map_get(f);
1185 return PTR_ERR(map);
1186 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1191 key = __bpf_copy_key(ukey, map->key_size);
1197 if (bpf_map_is_dev_bound(map)) {
1198 err = bpf_map_offload_delete_elem(map, key);
1200 } else if (IS_FD_PROG_ARRAY(map) ||
1201 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1202 /* These maps require sleepable context */
1203 err = map->ops->map_delete_elem(map, key);
1207 bpf_disable_instrumentation();
1209 err = map->ops->map_delete_elem(map, key);
1211 bpf_enable_instrumentation();
1212 maybe_wait_bpf_programs(map);
1220 /* last field in 'union bpf_attr' used by this command */
1221 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1223 static int map_get_next_key(union bpf_attr *attr)
1225 void __user *ukey = u64_to_user_ptr(attr->key);
1226 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1227 int ufd = attr->map_fd;
1228 struct bpf_map *map;
1229 void *key, *next_key;
1233 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1237 map = __bpf_map_get(f);
1239 return PTR_ERR(map);
1240 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1246 key = __bpf_copy_key(ukey, map->key_size);
1256 next_key = kmalloc(map->key_size, GFP_USER);
1260 if (bpf_map_is_dev_bound(map)) {
1261 err = bpf_map_offload_get_next_key(map, key, next_key);
1266 err = map->ops->map_get_next_key(map, key, next_key);
1273 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1287 int generic_map_delete_batch(struct bpf_map *map,
1288 const union bpf_attr *attr,
1289 union bpf_attr __user *uattr)
1291 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1296 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1299 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1300 !map_value_has_spin_lock(map)) {
1304 max_count = attr->batch.count;
1308 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1312 for (cp = 0; cp < max_count; cp++) {
1314 if (copy_from_user(key, keys + cp * map->key_size,
1318 if (bpf_map_is_dev_bound(map)) {
1319 err = bpf_map_offload_delete_elem(map, key);
1323 bpf_disable_instrumentation();
1325 err = map->ops->map_delete_elem(map, key);
1327 bpf_enable_instrumentation();
1328 maybe_wait_bpf_programs(map);
1332 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1339 int generic_map_update_batch(struct bpf_map *map,
1340 const union bpf_attr *attr,
1341 union bpf_attr __user *uattr)
1343 void __user *values = u64_to_user_ptr(attr->batch.values);
1344 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1345 u32 value_size, cp, max_count;
1346 int ufd = attr->map_fd;
1352 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1355 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1356 !map_value_has_spin_lock(map)) {
1360 value_size = bpf_map_value_size(map);
1362 max_count = attr->batch.count;
1366 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1370 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1376 for (cp = 0; cp < max_count; cp++) {
1378 if (copy_from_user(key, keys + cp * map->key_size,
1380 copy_from_user(value, values + cp * value_size, value_size))
1383 err = bpf_map_update_value(map, f, key, value,
1384 attr->batch.elem_flags);
1390 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1398 #define MAP_LOOKUP_RETRIES 3
1400 int generic_map_lookup_batch(struct bpf_map *map,
1401 const union bpf_attr *attr,
1402 union bpf_attr __user *uattr)
1404 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1405 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1406 void __user *values = u64_to_user_ptr(attr->batch.values);
1407 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1408 void *buf, *buf_prevkey, *prev_key, *key, *value;
1409 int err, retry = MAP_LOOKUP_RETRIES;
1410 u32 value_size, cp, max_count;
1412 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1415 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1416 !map_value_has_spin_lock(map))
1419 value_size = bpf_map_value_size(map);
1421 max_count = attr->batch.count;
1425 if (put_user(0, &uattr->batch.count))
1428 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1432 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1440 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1443 value = key + map->key_size;
1445 prev_key = buf_prevkey;
1447 for (cp = 0; cp < max_count;) {
1449 err = map->ops->map_get_next_key(map, prev_key, key);
1453 err = bpf_map_copy_value(map, key, value,
1454 attr->batch.elem_flags);
1456 if (err == -ENOENT) {
1468 if (copy_to_user(keys + cp * map->key_size, key,
1473 if (copy_to_user(values + cp * value_size, value, value_size)) {
1479 prev_key = buf_prevkey;
1481 swap(prev_key, key);
1482 retry = MAP_LOOKUP_RETRIES;
1489 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1490 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1499 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
1501 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1503 void __user *ukey = u64_to_user_ptr(attr->key);
1504 void __user *uvalue = u64_to_user_ptr(attr->value);
1505 int ufd = attr->map_fd;
1506 struct bpf_map *map;
1512 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1515 if (attr->flags & ~BPF_F_LOCK)
1519 map = __bpf_map_get(f);
1521 return PTR_ERR(map);
1522 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1523 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1529 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1530 map->map_type == BPF_MAP_TYPE_STACK)) {
1535 if ((attr->flags & BPF_F_LOCK) &&
1536 !map_value_has_spin_lock(map)) {
1541 key = __bpf_copy_key(ukey, map->key_size);
1547 value_size = bpf_map_value_size(map);
1550 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1555 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1556 map->map_type == BPF_MAP_TYPE_STACK) {
1557 err = map->ops->map_pop_elem(map, value);
1558 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1559 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1560 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1561 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1562 if (!bpf_map_is_dev_bound(map)) {
1563 bpf_disable_instrumentation();
1565 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1567 bpf_enable_instrumentation();
1574 if (copy_to_user(uvalue, value, value_size) != 0) {
1590 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1592 static int map_freeze(const union bpf_attr *attr)
1594 int err = 0, ufd = attr->map_fd;
1595 struct bpf_map *map;
1598 if (CHECK_ATTR(BPF_MAP_FREEZE))
1602 map = __bpf_map_get(f);
1604 return PTR_ERR(map);
1606 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS ||
1607 map_value_has_timer(map)) {
1612 mutex_lock(&map->freeze_mutex);
1614 if (map->writecnt) {
1618 if (READ_ONCE(map->frozen)) {
1622 if (!bpf_capable()) {
1627 WRITE_ONCE(map->frozen, true);
1629 mutex_unlock(&map->freeze_mutex);
1634 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1635 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1636 [_id] = & _name ## _prog_ops,
1637 #define BPF_MAP_TYPE(_id, _ops)
1638 #define BPF_LINK_TYPE(_id, _name)
1639 #include <linux/bpf_types.h>
1640 #undef BPF_PROG_TYPE
1642 #undef BPF_LINK_TYPE
1645 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1647 const struct bpf_prog_ops *ops;
1649 if (type >= ARRAY_SIZE(bpf_prog_types))
1651 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1652 ops = bpf_prog_types[type];
1656 if (!bpf_prog_is_dev_bound(prog->aux))
1657 prog->aux->ops = ops;
1659 prog->aux->ops = &bpf_offload_prog_ops;
1670 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1671 [BPF_AUDIT_LOAD] = "LOAD",
1672 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1675 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1677 struct audit_context *ctx = NULL;
1678 struct audit_buffer *ab;
1680 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1682 if (audit_enabled == AUDIT_OFF)
1684 if (op == BPF_AUDIT_LOAD)
1685 ctx = audit_context();
1686 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1689 audit_log_format(ab, "prog-id=%u op=%s",
1690 prog->aux->id, bpf_audit_str[op]);
1694 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1698 idr_preload(GFP_KERNEL);
1699 spin_lock_bh(&prog_idr_lock);
1700 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1703 spin_unlock_bh(&prog_idr_lock);
1706 /* id is in [1, INT_MAX) */
1707 if (WARN_ON_ONCE(!id))
1710 return id > 0 ? 0 : id;
1713 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1715 unsigned long flags;
1717 /* cBPF to eBPF migrations are currently not in the idr store.
1718 * Offloaded programs are removed from the store when their device
1719 * disappears - even if someone grabs an fd to them they are unusable,
1720 * simply waiting for refcnt to drop to be freed.
1726 spin_lock_irqsave(&prog_idr_lock, flags);
1728 __acquire(&prog_idr_lock);
1730 idr_remove(&prog_idr, prog->aux->id);
1734 spin_unlock_irqrestore(&prog_idr_lock, flags);
1736 __release(&prog_idr_lock);
1739 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1741 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1743 kvfree(aux->func_info);
1744 kfree(aux->func_info_aux);
1745 free_uid(aux->user);
1746 security_bpf_prog_free(aux);
1747 bpf_prog_free(aux->prog);
1750 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1752 bpf_prog_kallsyms_del_all(prog);
1753 btf_put(prog->aux->btf);
1754 kvfree(prog->aux->jited_linfo);
1755 kvfree(prog->aux->linfo);
1756 kfree(prog->aux->kfunc_tab);
1757 if (prog->aux->attach_btf)
1758 btf_put(prog->aux->attach_btf);
1761 if (prog->aux->sleepable)
1762 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1764 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1766 __bpf_prog_put_rcu(&prog->aux->rcu);
1770 static void bpf_prog_put_deferred(struct work_struct *work)
1772 struct bpf_prog_aux *aux;
1773 struct bpf_prog *prog;
1775 aux = container_of(work, struct bpf_prog_aux, work);
1777 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1778 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1779 __bpf_prog_put_noref(prog, true);
1782 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1784 struct bpf_prog_aux *aux = prog->aux;
1786 if (atomic64_dec_and_test(&aux->refcnt)) {
1787 /* bpf_prog_free_id() must be called first */
1788 bpf_prog_free_id(prog, do_idr_lock);
1790 if (in_irq() || irqs_disabled()) {
1791 INIT_WORK(&aux->work, bpf_prog_put_deferred);
1792 schedule_work(&aux->work);
1794 bpf_prog_put_deferred(&aux->work);
1799 void bpf_prog_put(struct bpf_prog *prog)
1801 __bpf_prog_put(prog, true);
1803 EXPORT_SYMBOL_GPL(bpf_prog_put);
1805 static int bpf_prog_release(struct inode *inode, struct file *filp)
1807 struct bpf_prog *prog = filp->private_data;
1813 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1814 struct bpf_prog_stats *stats)
1816 u64 nsecs = 0, cnt = 0, misses = 0;
1819 for_each_possible_cpu(cpu) {
1820 const struct bpf_prog_stats *st;
1822 u64 tnsecs, tcnt, tmisses;
1824 st = per_cpu_ptr(prog->stats, cpu);
1826 start = u64_stats_fetch_begin_irq(&st->syncp);
1829 tmisses = st->misses;
1830 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1835 stats->nsecs = nsecs;
1837 stats->misses = misses;
1840 #ifdef CONFIG_PROC_FS
1841 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1843 const struct bpf_prog *prog = filp->private_data;
1844 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1845 struct bpf_prog_stats stats;
1847 bpf_prog_get_stats(prog, &stats);
1848 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1855 "run_time_ns:\t%llu\n"
1857 "recursion_misses:\t%llu\n",
1861 prog->pages * 1ULL << PAGE_SHIFT,
1869 const struct file_operations bpf_prog_fops = {
1870 #ifdef CONFIG_PROC_FS
1871 .show_fdinfo = bpf_prog_show_fdinfo,
1873 .release = bpf_prog_release,
1874 .read = bpf_dummy_read,
1875 .write = bpf_dummy_write,
1878 int bpf_prog_new_fd(struct bpf_prog *prog)
1882 ret = security_bpf_prog(prog);
1886 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1887 O_RDWR | O_CLOEXEC);
1890 static struct bpf_prog *____bpf_prog_get(struct fd f)
1893 return ERR_PTR(-EBADF);
1894 if (f.file->f_op != &bpf_prog_fops) {
1896 return ERR_PTR(-EINVAL);
1899 return f.file->private_data;
1902 void bpf_prog_add(struct bpf_prog *prog, int i)
1904 atomic64_add(i, &prog->aux->refcnt);
1906 EXPORT_SYMBOL_GPL(bpf_prog_add);
1908 void bpf_prog_sub(struct bpf_prog *prog, int i)
1910 /* Only to be used for undoing previous bpf_prog_add() in some
1911 * error path. We still know that another entity in our call
1912 * path holds a reference to the program, thus atomic_sub() can
1913 * be safely used in such cases!
1915 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1917 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1919 void bpf_prog_inc(struct bpf_prog *prog)
1921 atomic64_inc(&prog->aux->refcnt);
1923 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1925 /* prog_idr_lock should have been held */
1926 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1930 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1933 return ERR_PTR(-ENOENT);
1937 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1939 bool bpf_prog_get_ok(struct bpf_prog *prog,
1940 enum bpf_prog_type *attach_type, bool attach_drv)
1942 /* not an attachment, just a refcount inc, always allow */
1946 if (prog->type != *attach_type)
1948 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1954 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1957 struct fd f = fdget(ufd);
1958 struct bpf_prog *prog;
1960 prog = ____bpf_prog_get(f);
1963 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1964 prog = ERR_PTR(-EINVAL);
1974 struct bpf_prog *bpf_prog_get(u32 ufd)
1976 return __bpf_prog_get(ufd, NULL, false);
1979 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1982 return __bpf_prog_get(ufd, &type, attach_drv);
1984 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1986 /* Initially all BPF programs could be loaded w/o specifying
1987 * expected_attach_type. Later for some of them specifying expected_attach_type
1988 * at load time became required so that program could be validated properly.
1989 * Programs of types that are allowed to be loaded both w/ and w/o (for
1990 * backward compatibility) expected_attach_type, should have the default attach
1991 * type assigned to expected_attach_type for the latter case, so that it can be
1992 * validated later at attach time.
1994 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1995 * prog type requires it but has some attach types that have to be backward
1998 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
2000 switch (attr->prog_type) {
2001 case BPF_PROG_TYPE_CGROUP_SOCK:
2002 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
2003 * exist so checking for non-zero is the way to go here.
2005 if (!attr->expected_attach_type)
2006 attr->expected_attach_type =
2007 BPF_CGROUP_INET_SOCK_CREATE;
2009 case BPF_PROG_TYPE_SK_REUSEPORT:
2010 if (!attr->expected_attach_type)
2011 attr->expected_attach_type =
2012 BPF_SK_REUSEPORT_SELECT;
2018 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2019 enum bpf_attach_type expected_attach_type,
2020 struct btf *attach_btf, u32 btf_id,
2021 struct bpf_prog *dst_prog)
2024 if (btf_id > BTF_MAX_TYPE)
2027 if (!attach_btf && !dst_prog)
2030 switch (prog_type) {
2031 case BPF_PROG_TYPE_TRACING:
2032 case BPF_PROG_TYPE_LSM:
2033 case BPF_PROG_TYPE_STRUCT_OPS:
2034 case BPF_PROG_TYPE_EXT:
2041 if (attach_btf && (!btf_id || dst_prog))
2044 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
2045 prog_type != BPF_PROG_TYPE_EXT)
2048 switch (prog_type) {
2049 case BPF_PROG_TYPE_CGROUP_SOCK:
2050 switch (expected_attach_type) {
2051 case BPF_CGROUP_INET_SOCK_CREATE:
2052 case BPF_CGROUP_INET_SOCK_RELEASE:
2053 case BPF_CGROUP_INET4_POST_BIND:
2054 case BPF_CGROUP_INET6_POST_BIND:
2059 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2060 switch (expected_attach_type) {
2061 case BPF_CGROUP_INET4_BIND:
2062 case BPF_CGROUP_INET6_BIND:
2063 case BPF_CGROUP_INET4_CONNECT:
2064 case BPF_CGROUP_INET6_CONNECT:
2065 case BPF_CGROUP_INET4_GETPEERNAME:
2066 case BPF_CGROUP_INET6_GETPEERNAME:
2067 case BPF_CGROUP_INET4_GETSOCKNAME:
2068 case BPF_CGROUP_INET6_GETSOCKNAME:
2069 case BPF_CGROUP_UDP4_SENDMSG:
2070 case BPF_CGROUP_UDP6_SENDMSG:
2071 case BPF_CGROUP_UDP4_RECVMSG:
2072 case BPF_CGROUP_UDP6_RECVMSG:
2077 case BPF_PROG_TYPE_CGROUP_SKB:
2078 switch (expected_attach_type) {
2079 case BPF_CGROUP_INET_INGRESS:
2080 case BPF_CGROUP_INET_EGRESS:
2085 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2086 switch (expected_attach_type) {
2087 case BPF_CGROUP_SETSOCKOPT:
2088 case BPF_CGROUP_GETSOCKOPT:
2093 case BPF_PROG_TYPE_SK_LOOKUP:
2094 if (expected_attach_type == BPF_SK_LOOKUP)
2097 case BPF_PROG_TYPE_SK_REUSEPORT:
2098 switch (expected_attach_type) {
2099 case BPF_SK_REUSEPORT_SELECT:
2100 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2105 case BPF_PROG_TYPE_SYSCALL:
2106 case BPF_PROG_TYPE_EXT:
2107 if (expected_attach_type)
2115 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2117 switch (prog_type) {
2118 case BPF_PROG_TYPE_SCHED_CLS:
2119 case BPF_PROG_TYPE_SCHED_ACT:
2120 case BPF_PROG_TYPE_XDP:
2121 case BPF_PROG_TYPE_LWT_IN:
2122 case BPF_PROG_TYPE_LWT_OUT:
2123 case BPF_PROG_TYPE_LWT_XMIT:
2124 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2125 case BPF_PROG_TYPE_SK_SKB:
2126 case BPF_PROG_TYPE_SK_MSG:
2127 case BPF_PROG_TYPE_LIRC_MODE2:
2128 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2129 case BPF_PROG_TYPE_CGROUP_DEVICE:
2130 case BPF_PROG_TYPE_CGROUP_SOCK:
2131 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2132 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2133 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2134 case BPF_PROG_TYPE_SOCK_OPS:
2135 case BPF_PROG_TYPE_EXT: /* extends any prog */
2137 case BPF_PROG_TYPE_CGROUP_SKB:
2139 case BPF_PROG_TYPE_SK_REUSEPORT:
2140 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2146 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2148 switch (prog_type) {
2149 case BPF_PROG_TYPE_KPROBE:
2150 case BPF_PROG_TYPE_TRACEPOINT:
2151 case BPF_PROG_TYPE_PERF_EVENT:
2152 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2153 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2154 case BPF_PROG_TYPE_TRACING:
2155 case BPF_PROG_TYPE_LSM:
2156 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2157 case BPF_PROG_TYPE_EXT: /* extends any prog */
2164 /* last field in 'union bpf_attr' used by this command */
2165 #define BPF_PROG_LOAD_LAST_FIELD fd_array
2167 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
2169 enum bpf_prog_type type = attr->prog_type;
2170 struct bpf_prog *prog, *dst_prog = NULL;
2171 struct btf *attach_btf = NULL;
2176 if (CHECK_ATTR(BPF_PROG_LOAD))
2179 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2180 BPF_F_ANY_ALIGNMENT |
2181 BPF_F_TEST_STATE_FREQ |
2183 BPF_F_TEST_RND_HI32))
2186 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2187 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2191 /* copy eBPF program license from user space */
2192 if (strncpy_from_bpfptr(license,
2193 make_bpfptr(attr->license, uattr.is_kernel),
2194 sizeof(license) - 1) < 0)
2196 license[sizeof(license) - 1] = 0;
2198 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2199 is_gpl = license_is_gpl_compatible(license);
2201 if (attr->insn_cnt == 0 ||
2202 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2204 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2205 type != BPF_PROG_TYPE_CGROUP_SKB &&
2209 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2211 if (is_perfmon_prog_type(type) && !perfmon_capable())
2214 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2215 * or btf, we need to check which one it is
2217 if (attr->attach_prog_fd) {
2218 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2219 if (IS_ERR(dst_prog)) {
2221 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2222 if (IS_ERR(attach_btf))
2224 if (!btf_is_kernel(attach_btf)) {
2225 /* attaching through specifying bpf_prog's BTF
2226 * objects directly might be supported eventually
2228 btf_put(attach_btf);
2232 } else if (attr->attach_btf_id) {
2233 /* fall back to vmlinux BTF, if BTF type ID is specified */
2234 attach_btf = bpf_get_btf_vmlinux();
2235 if (IS_ERR(attach_btf))
2236 return PTR_ERR(attach_btf);
2239 btf_get(attach_btf);
2242 bpf_prog_load_fixup_attach_type(attr);
2243 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2244 attach_btf, attr->attach_btf_id,
2247 bpf_prog_put(dst_prog);
2249 btf_put(attach_btf);
2253 /* plain bpf_prog allocation */
2254 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2257 bpf_prog_put(dst_prog);
2259 btf_put(attach_btf);
2263 prog->expected_attach_type = attr->expected_attach_type;
2264 prog->aux->attach_btf = attach_btf;
2265 prog->aux->attach_btf_id = attr->attach_btf_id;
2266 prog->aux->dst_prog = dst_prog;
2267 prog->aux->offload_requested = !!attr->prog_ifindex;
2268 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2270 err = security_bpf_prog_alloc(prog->aux);
2274 prog->aux->user = get_current_user();
2275 prog->len = attr->insn_cnt;
2278 if (copy_from_bpfptr(prog->insns,
2279 make_bpfptr(attr->insns, uattr.is_kernel),
2280 bpf_prog_insn_size(prog)) != 0)
2283 prog->orig_prog = NULL;
2286 atomic64_set(&prog->aux->refcnt, 1);
2287 prog->gpl_compatible = is_gpl ? 1 : 0;
2289 if (bpf_prog_is_dev_bound(prog->aux)) {
2290 err = bpf_prog_offload_init(prog, attr);
2295 /* find program type: socket_filter vs tracing_filter */
2296 err = find_prog_type(type, prog);
2300 prog->aux->load_time = ktime_get_boottime_ns();
2301 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2302 sizeof(attr->prog_name));
2306 /* run eBPF verifier */
2307 err = bpf_check(&prog, attr, uattr);
2309 goto free_used_maps;
2311 prog = bpf_prog_select_runtime(prog, &err);
2313 goto free_used_maps;
2315 err = bpf_prog_alloc_id(prog);
2317 goto free_used_maps;
2319 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2320 * effectively publicly exposed. However, retrieving via
2321 * bpf_prog_get_fd_by_id() will take another reference,
2322 * therefore it cannot be gone underneath us.
2324 * Only for the time /after/ successful bpf_prog_new_fd()
2325 * and before returning to userspace, we might just hold
2326 * one reference and any parallel close on that fd could
2327 * rip everything out. Hence, below notifications must
2328 * happen before bpf_prog_new_fd().
2330 * Also, any failure handling from this point onwards must
2331 * be using bpf_prog_put() given the program is exposed.
2333 bpf_prog_kallsyms_add(prog);
2334 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2335 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2337 err = bpf_prog_new_fd(prog);
2343 /* In case we have subprogs, we need to wait for a grace
2344 * period before we can tear down JIT memory since symbols
2345 * are already exposed under kallsyms.
2347 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2350 free_uid(prog->aux->user);
2351 security_bpf_prog_free(prog->aux);
2353 if (prog->aux->attach_btf)
2354 btf_put(prog->aux->attach_btf);
2355 bpf_prog_free(prog);
2359 #define BPF_OBJ_LAST_FIELD file_flags
2361 static int bpf_obj_pin(const union bpf_attr *attr)
2363 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2366 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2369 static int bpf_obj_get(const union bpf_attr *attr)
2371 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2372 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2375 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2379 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2380 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2382 atomic64_set(&link->refcnt, 1);
2389 static void bpf_link_free_id(int id)
2394 spin_lock_bh(&link_idr_lock);
2395 idr_remove(&link_idr, id);
2396 spin_unlock_bh(&link_idr_lock);
2399 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2400 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2401 * anon_inode's release() call. This helper marksbpf_link as
2402 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2403 * is not decremented, it's the responsibility of a calling code that failed
2404 * to complete bpf_link initialization.
2406 void bpf_link_cleanup(struct bpf_link_primer *primer)
2408 primer->link->prog = NULL;
2409 bpf_link_free_id(primer->id);
2411 put_unused_fd(primer->fd);
2414 void bpf_link_inc(struct bpf_link *link)
2416 atomic64_inc(&link->refcnt);
2419 /* bpf_link_free is guaranteed to be called from process context */
2420 static void bpf_link_free(struct bpf_link *link)
2422 bpf_link_free_id(link->id);
2424 /* detach BPF program, clean up used resources */
2425 link->ops->release(link);
2426 bpf_prog_put(link->prog);
2428 /* free bpf_link and its containing memory */
2429 link->ops->dealloc(link);
2432 static void bpf_link_put_deferred(struct work_struct *work)
2434 struct bpf_link *link = container_of(work, struct bpf_link, work);
2436 bpf_link_free(link);
2439 /* bpf_link_put can be called from atomic context, but ensures that resources
2440 * are freed from process context
2442 void bpf_link_put(struct bpf_link *link)
2444 if (!atomic64_dec_and_test(&link->refcnt))
2448 INIT_WORK(&link->work, bpf_link_put_deferred);
2449 schedule_work(&link->work);
2451 bpf_link_free(link);
2455 static int bpf_link_release(struct inode *inode, struct file *filp)
2457 struct bpf_link *link = filp->private_data;
2463 #ifdef CONFIG_PROC_FS
2464 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2465 #define BPF_MAP_TYPE(_id, _ops)
2466 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2467 static const char *bpf_link_type_strs[] = {
2468 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2469 #include <linux/bpf_types.h>
2471 #undef BPF_PROG_TYPE
2473 #undef BPF_LINK_TYPE
2475 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2477 const struct bpf_link *link = filp->private_data;
2478 const struct bpf_prog *prog = link->prog;
2479 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2481 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2487 bpf_link_type_strs[link->type],
2491 if (link->ops->show_fdinfo)
2492 link->ops->show_fdinfo(link, m);
2496 static const struct file_operations bpf_link_fops = {
2497 #ifdef CONFIG_PROC_FS
2498 .show_fdinfo = bpf_link_show_fdinfo,
2500 .release = bpf_link_release,
2501 .read = bpf_dummy_read,
2502 .write = bpf_dummy_write,
2505 static int bpf_link_alloc_id(struct bpf_link *link)
2509 idr_preload(GFP_KERNEL);
2510 spin_lock_bh(&link_idr_lock);
2511 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2512 spin_unlock_bh(&link_idr_lock);
2518 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2519 * reserving unused FD and allocating ID from link_idr. This is to be paired
2520 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2521 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2522 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2523 * transient state is passed around in struct bpf_link_primer.
2524 * This is preferred way to create and initialize bpf_link, especially when
2525 * there are complicated and expensive operations inbetween creating bpf_link
2526 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2527 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2528 * expensive (and potentially failing) roll back operations in a rare case
2529 * that file, FD, or ID can't be allocated.
2531 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2536 fd = get_unused_fd_flags(O_CLOEXEC);
2541 id = bpf_link_alloc_id(link);
2547 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2549 bpf_link_free_id(id);
2551 return PTR_ERR(file);
2554 primer->link = link;
2555 primer->file = file;
2561 int bpf_link_settle(struct bpf_link_primer *primer)
2563 /* make bpf_link fetchable by ID */
2564 spin_lock_bh(&link_idr_lock);
2565 primer->link->id = primer->id;
2566 spin_unlock_bh(&link_idr_lock);
2567 /* make bpf_link fetchable by FD */
2568 fd_install(primer->fd, primer->file);
2569 /* pass through installed FD */
2573 int bpf_link_new_fd(struct bpf_link *link)
2575 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2578 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2580 struct fd f = fdget(ufd);
2581 struct bpf_link *link;
2584 return ERR_PTR(-EBADF);
2585 if (f.file->f_op != &bpf_link_fops) {
2587 return ERR_PTR(-EINVAL);
2590 link = f.file->private_data;
2597 struct bpf_tracing_link {
2598 struct bpf_link link;
2599 enum bpf_attach_type attach_type;
2600 struct bpf_trampoline *trampoline;
2601 struct bpf_prog *tgt_prog;
2604 static void bpf_tracing_link_release(struct bpf_link *link)
2606 struct bpf_tracing_link *tr_link =
2607 container_of(link, struct bpf_tracing_link, link);
2609 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2610 tr_link->trampoline));
2612 bpf_trampoline_put(tr_link->trampoline);
2614 /* tgt_prog is NULL if target is a kernel function */
2615 if (tr_link->tgt_prog)
2616 bpf_prog_put(tr_link->tgt_prog);
2619 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2621 struct bpf_tracing_link *tr_link =
2622 container_of(link, struct bpf_tracing_link, link);
2627 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2628 struct seq_file *seq)
2630 struct bpf_tracing_link *tr_link =
2631 container_of(link, struct bpf_tracing_link, link);
2634 "attach_type:\t%d\n",
2635 tr_link->attach_type);
2638 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2639 struct bpf_link_info *info)
2641 struct bpf_tracing_link *tr_link =
2642 container_of(link, struct bpf_tracing_link, link);
2644 info->tracing.attach_type = tr_link->attach_type;
2645 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2646 &info->tracing.target_obj_id,
2647 &info->tracing.target_btf_id);
2652 static const struct bpf_link_ops bpf_tracing_link_lops = {
2653 .release = bpf_tracing_link_release,
2654 .dealloc = bpf_tracing_link_dealloc,
2655 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2656 .fill_link_info = bpf_tracing_link_fill_link_info,
2659 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2663 struct bpf_link_primer link_primer;
2664 struct bpf_prog *tgt_prog = NULL;
2665 struct bpf_trampoline *tr = NULL;
2666 struct bpf_tracing_link *link;
2670 switch (prog->type) {
2671 case BPF_PROG_TYPE_TRACING:
2672 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2673 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2674 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2679 case BPF_PROG_TYPE_EXT:
2680 if (prog->expected_attach_type != 0) {
2685 case BPF_PROG_TYPE_LSM:
2686 if (prog->expected_attach_type != BPF_LSM_MAC) {
2696 if (!!tgt_prog_fd != !!btf_id) {
2702 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2703 if (prog->type != BPF_PROG_TYPE_EXT) {
2708 tgt_prog = bpf_prog_get(tgt_prog_fd);
2709 if (IS_ERR(tgt_prog)) {
2710 err = PTR_ERR(tgt_prog);
2715 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
2718 link = kzalloc(sizeof(*link), GFP_USER);
2723 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2724 &bpf_tracing_link_lops, prog);
2725 link->attach_type = prog->expected_attach_type;
2727 mutex_lock(&prog->aux->dst_mutex);
2729 /* There are a few possible cases here:
2731 * - if prog->aux->dst_trampoline is set, the program was just loaded
2732 * and not yet attached to anything, so we can use the values stored
2735 * - if prog->aux->dst_trampoline is NULL, the program has already been
2736 * attached to a target and its initial target was cleared (below)
2738 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2739 * target_btf_id using the link_create API.
2741 * - if tgt_prog == NULL when this function was called using the old
2742 * raw_tracepoint_open API, and we need a target from prog->aux
2744 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
2745 * was detached and is going for re-attachment.
2747 if (!prog->aux->dst_trampoline && !tgt_prog) {
2749 * Allow re-attach for TRACING and LSM programs. If it's
2750 * currently linked, bpf_trampoline_link_prog will fail.
2751 * EXT programs need to specify tgt_prog_fd, so they
2752 * re-attach in separate code path.
2754 if (prog->type != BPF_PROG_TYPE_TRACING &&
2755 prog->type != BPF_PROG_TYPE_LSM) {
2759 btf_id = prog->aux->attach_btf_id;
2760 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
2763 if (!prog->aux->dst_trampoline ||
2764 (key && key != prog->aux->dst_trampoline->key)) {
2765 /* If there is no saved target, or the specified target is
2766 * different from the destination specified at load time, we
2767 * need a new trampoline and a check for compatibility
2769 struct bpf_attach_target_info tgt_info = {};
2771 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2776 tr = bpf_trampoline_get(key, &tgt_info);
2782 /* The caller didn't specify a target, or the target was the
2783 * same as the destination supplied during program load. This
2784 * means we can reuse the trampoline and reference from program
2785 * load time, and there is no need to allocate a new one. This
2786 * can only happen once for any program, as the saved values in
2787 * prog->aux are cleared below.
2789 tr = prog->aux->dst_trampoline;
2790 tgt_prog = prog->aux->dst_prog;
2793 err = bpf_link_prime(&link->link, &link_primer);
2797 err = bpf_trampoline_link_prog(prog, tr);
2799 bpf_link_cleanup(&link_primer);
2804 link->tgt_prog = tgt_prog;
2805 link->trampoline = tr;
2807 /* Always clear the trampoline and target prog from prog->aux to make
2808 * sure the original attach destination is not kept alive after a
2809 * program is (re-)attached to another target.
2811 if (prog->aux->dst_prog &&
2812 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2813 /* got extra prog ref from syscall, or attaching to different prog */
2814 bpf_prog_put(prog->aux->dst_prog);
2815 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2816 /* we allocated a new trampoline, so free the old one */
2817 bpf_trampoline_put(prog->aux->dst_trampoline);
2819 prog->aux->dst_prog = NULL;
2820 prog->aux->dst_trampoline = NULL;
2821 mutex_unlock(&prog->aux->dst_mutex);
2823 return bpf_link_settle(&link_primer);
2825 if (tr && tr != prog->aux->dst_trampoline)
2826 bpf_trampoline_put(tr);
2827 mutex_unlock(&prog->aux->dst_mutex);
2830 if (tgt_prog_fd && tgt_prog)
2831 bpf_prog_put(tgt_prog);
2835 struct bpf_raw_tp_link {
2836 struct bpf_link link;
2837 struct bpf_raw_event_map *btp;
2840 static void bpf_raw_tp_link_release(struct bpf_link *link)
2842 struct bpf_raw_tp_link *raw_tp =
2843 container_of(link, struct bpf_raw_tp_link, link);
2845 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2846 bpf_put_raw_tracepoint(raw_tp->btp);
2849 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2851 struct bpf_raw_tp_link *raw_tp =
2852 container_of(link, struct bpf_raw_tp_link, link);
2857 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2858 struct seq_file *seq)
2860 struct bpf_raw_tp_link *raw_tp_link =
2861 container_of(link, struct bpf_raw_tp_link, link);
2865 raw_tp_link->btp->tp->name);
2868 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2869 struct bpf_link_info *info)
2871 struct bpf_raw_tp_link *raw_tp_link =
2872 container_of(link, struct bpf_raw_tp_link, link);
2873 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2874 const char *tp_name = raw_tp_link->btp->tp->name;
2875 u32 ulen = info->raw_tracepoint.tp_name_len;
2876 size_t tp_len = strlen(tp_name);
2881 info->raw_tracepoint.tp_name_len = tp_len + 1;
2886 if (ulen >= tp_len + 1) {
2887 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2892 if (copy_to_user(ubuf, tp_name, ulen - 1))
2894 if (put_user(zero, ubuf + ulen - 1))
2902 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
2903 .release = bpf_raw_tp_link_release,
2904 .dealloc = bpf_raw_tp_link_dealloc,
2905 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2906 .fill_link_info = bpf_raw_tp_link_fill_link_info,
2909 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2911 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2913 struct bpf_link_primer link_primer;
2914 struct bpf_raw_tp_link *link;
2915 struct bpf_raw_event_map *btp;
2916 struct bpf_prog *prog;
2917 const char *tp_name;
2921 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2924 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2926 return PTR_ERR(prog);
2928 switch (prog->type) {
2929 case BPF_PROG_TYPE_TRACING:
2930 case BPF_PROG_TYPE_EXT:
2931 case BPF_PROG_TYPE_LSM:
2932 if (attr->raw_tracepoint.name) {
2933 /* The attach point for this category of programs
2934 * should be specified via btf_id during program load.
2939 if (prog->type == BPF_PROG_TYPE_TRACING &&
2940 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
2941 tp_name = prog->aux->attach_func_name;
2944 err = bpf_tracing_prog_attach(prog, 0, 0);
2948 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2949 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2950 if (strncpy_from_user(buf,
2951 u64_to_user_ptr(attr->raw_tracepoint.name),
2952 sizeof(buf) - 1) < 0) {
2956 buf[sizeof(buf) - 1] = 0;
2964 btp = bpf_get_raw_tracepoint(tp_name);
2970 link = kzalloc(sizeof(*link), GFP_USER);
2975 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2976 &bpf_raw_tp_link_lops, prog);
2979 err = bpf_link_prime(&link->link, &link_primer);
2985 err = bpf_probe_register(link->btp, prog);
2987 bpf_link_cleanup(&link_primer);
2991 return bpf_link_settle(&link_primer);
2994 bpf_put_raw_tracepoint(btp);
3000 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
3001 enum bpf_attach_type attach_type)
3003 switch (prog->type) {
3004 case BPF_PROG_TYPE_CGROUP_SOCK:
3005 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3006 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3007 case BPF_PROG_TYPE_SK_LOOKUP:
3008 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
3009 case BPF_PROG_TYPE_CGROUP_SKB:
3010 if (!capable(CAP_NET_ADMIN))
3011 /* cg-skb progs can be loaded by unpriv user.
3012 * check permissions at attach time.
3015 return prog->enforce_expected_attach_type &&
3016 prog->expected_attach_type != attach_type ?
3023 static enum bpf_prog_type
3024 attach_type_to_prog_type(enum bpf_attach_type attach_type)
3026 switch (attach_type) {
3027 case BPF_CGROUP_INET_INGRESS:
3028 case BPF_CGROUP_INET_EGRESS:
3029 return BPF_PROG_TYPE_CGROUP_SKB;
3030 case BPF_CGROUP_INET_SOCK_CREATE:
3031 case BPF_CGROUP_INET_SOCK_RELEASE:
3032 case BPF_CGROUP_INET4_POST_BIND:
3033 case BPF_CGROUP_INET6_POST_BIND:
3034 return BPF_PROG_TYPE_CGROUP_SOCK;
3035 case BPF_CGROUP_INET4_BIND:
3036 case BPF_CGROUP_INET6_BIND:
3037 case BPF_CGROUP_INET4_CONNECT:
3038 case BPF_CGROUP_INET6_CONNECT:
3039 case BPF_CGROUP_INET4_GETPEERNAME:
3040 case BPF_CGROUP_INET6_GETPEERNAME:
3041 case BPF_CGROUP_INET4_GETSOCKNAME:
3042 case BPF_CGROUP_INET6_GETSOCKNAME:
3043 case BPF_CGROUP_UDP4_SENDMSG:
3044 case BPF_CGROUP_UDP6_SENDMSG:
3045 case BPF_CGROUP_UDP4_RECVMSG:
3046 case BPF_CGROUP_UDP6_RECVMSG:
3047 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3048 case BPF_CGROUP_SOCK_OPS:
3049 return BPF_PROG_TYPE_SOCK_OPS;
3050 case BPF_CGROUP_DEVICE:
3051 return BPF_PROG_TYPE_CGROUP_DEVICE;
3052 case BPF_SK_MSG_VERDICT:
3053 return BPF_PROG_TYPE_SK_MSG;
3054 case BPF_SK_SKB_STREAM_PARSER:
3055 case BPF_SK_SKB_STREAM_VERDICT:
3056 case BPF_SK_SKB_VERDICT:
3057 return BPF_PROG_TYPE_SK_SKB;
3058 case BPF_LIRC_MODE2:
3059 return BPF_PROG_TYPE_LIRC_MODE2;
3060 case BPF_FLOW_DISSECTOR:
3061 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3062 case BPF_CGROUP_SYSCTL:
3063 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3064 case BPF_CGROUP_GETSOCKOPT:
3065 case BPF_CGROUP_SETSOCKOPT:
3066 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
3067 case BPF_TRACE_ITER:
3068 return BPF_PROG_TYPE_TRACING;
3070 return BPF_PROG_TYPE_SK_LOOKUP;
3072 return BPF_PROG_TYPE_XDP;
3074 return BPF_PROG_TYPE_UNSPEC;
3078 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
3080 #define BPF_F_ATTACH_MASK \
3081 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
3083 static int bpf_prog_attach(const union bpf_attr *attr)
3085 enum bpf_prog_type ptype;
3086 struct bpf_prog *prog;
3089 if (CHECK_ATTR(BPF_PROG_ATTACH))
3092 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
3095 ptype = attach_type_to_prog_type(attr->attach_type);
3096 if (ptype == BPF_PROG_TYPE_UNSPEC)
3099 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3101 return PTR_ERR(prog);
3103 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3109 case BPF_PROG_TYPE_SK_SKB:
3110 case BPF_PROG_TYPE_SK_MSG:
3111 ret = sock_map_get_from_fd(attr, prog);
3113 case BPF_PROG_TYPE_LIRC_MODE2:
3114 ret = lirc_prog_attach(attr, prog);
3116 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3117 ret = netns_bpf_prog_attach(attr, prog);
3119 case BPF_PROG_TYPE_CGROUP_DEVICE:
3120 case BPF_PROG_TYPE_CGROUP_SKB:
3121 case BPF_PROG_TYPE_CGROUP_SOCK:
3122 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3123 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3124 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3125 case BPF_PROG_TYPE_SOCK_OPS:
3126 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3137 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3139 static int bpf_prog_detach(const union bpf_attr *attr)
3141 enum bpf_prog_type ptype;
3143 if (CHECK_ATTR(BPF_PROG_DETACH))
3146 ptype = attach_type_to_prog_type(attr->attach_type);
3149 case BPF_PROG_TYPE_SK_MSG:
3150 case BPF_PROG_TYPE_SK_SKB:
3151 return sock_map_prog_detach(attr, ptype);
3152 case BPF_PROG_TYPE_LIRC_MODE2:
3153 return lirc_prog_detach(attr);
3154 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3155 return netns_bpf_prog_detach(attr, ptype);
3156 case BPF_PROG_TYPE_CGROUP_DEVICE:
3157 case BPF_PROG_TYPE_CGROUP_SKB:
3158 case BPF_PROG_TYPE_CGROUP_SOCK:
3159 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3160 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3161 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3162 case BPF_PROG_TYPE_SOCK_OPS:
3163 return cgroup_bpf_prog_detach(attr, ptype);
3169 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3171 static int bpf_prog_query(const union bpf_attr *attr,
3172 union bpf_attr __user *uattr)
3174 if (!capable(CAP_NET_ADMIN))
3176 if (CHECK_ATTR(BPF_PROG_QUERY))
3178 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3181 switch (attr->query.attach_type) {
3182 case BPF_CGROUP_INET_INGRESS:
3183 case BPF_CGROUP_INET_EGRESS:
3184 case BPF_CGROUP_INET_SOCK_CREATE:
3185 case BPF_CGROUP_INET_SOCK_RELEASE:
3186 case BPF_CGROUP_INET4_BIND:
3187 case BPF_CGROUP_INET6_BIND:
3188 case BPF_CGROUP_INET4_POST_BIND:
3189 case BPF_CGROUP_INET6_POST_BIND:
3190 case BPF_CGROUP_INET4_CONNECT:
3191 case BPF_CGROUP_INET6_CONNECT:
3192 case BPF_CGROUP_INET4_GETPEERNAME:
3193 case BPF_CGROUP_INET6_GETPEERNAME:
3194 case BPF_CGROUP_INET4_GETSOCKNAME:
3195 case BPF_CGROUP_INET6_GETSOCKNAME:
3196 case BPF_CGROUP_UDP4_SENDMSG:
3197 case BPF_CGROUP_UDP6_SENDMSG:
3198 case BPF_CGROUP_UDP4_RECVMSG:
3199 case BPF_CGROUP_UDP6_RECVMSG:
3200 case BPF_CGROUP_SOCK_OPS:
3201 case BPF_CGROUP_DEVICE:
3202 case BPF_CGROUP_SYSCTL:
3203 case BPF_CGROUP_GETSOCKOPT:
3204 case BPF_CGROUP_SETSOCKOPT:
3205 return cgroup_bpf_prog_query(attr, uattr);
3206 case BPF_LIRC_MODE2:
3207 return lirc_prog_query(attr, uattr);
3208 case BPF_FLOW_DISSECTOR:
3210 return netns_bpf_prog_query(attr, uattr);
3216 #define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
3218 static int bpf_prog_test_run(const union bpf_attr *attr,
3219 union bpf_attr __user *uattr)
3221 struct bpf_prog *prog;
3222 int ret = -ENOTSUPP;
3224 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3227 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3228 (!attr->test.ctx_size_in && attr->test.ctx_in))
3231 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3232 (!attr->test.ctx_size_out && attr->test.ctx_out))
3235 prog = bpf_prog_get(attr->test.prog_fd);
3237 return PTR_ERR(prog);
3239 if (prog->aux->ops->test_run)
3240 ret = prog->aux->ops->test_run(prog, attr, uattr);
3246 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3248 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3249 union bpf_attr __user *uattr,
3253 u32 next_id = attr->start_id;
3256 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3259 if (!capable(CAP_SYS_ADMIN))
3264 if (!idr_get_next(idr, &next_id))
3266 spin_unlock_bh(lock);
3269 err = put_user(next_id, &uattr->next_id);
3274 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3276 struct bpf_map *map;
3278 spin_lock_bh(&map_idr_lock);
3280 map = idr_get_next(&map_idr, id);
3282 map = __bpf_map_inc_not_zero(map, false);
3288 spin_unlock_bh(&map_idr_lock);
3293 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3295 struct bpf_prog *prog;
3297 spin_lock_bh(&prog_idr_lock);
3299 prog = idr_get_next(&prog_idr, id);
3301 prog = bpf_prog_inc_not_zero(prog);
3307 spin_unlock_bh(&prog_idr_lock);
3312 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3314 struct bpf_prog *bpf_prog_by_id(u32 id)
3316 struct bpf_prog *prog;
3319 return ERR_PTR(-ENOENT);
3321 spin_lock_bh(&prog_idr_lock);
3322 prog = idr_find(&prog_idr, id);
3324 prog = bpf_prog_inc_not_zero(prog);
3326 prog = ERR_PTR(-ENOENT);
3327 spin_unlock_bh(&prog_idr_lock);
3331 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3333 struct bpf_prog *prog;
3334 u32 id = attr->prog_id;
3337 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3340 if (!capable(CAP_SYS_ADMIN))
3343 prog = bpf_prog_by_id(id);
3345 return PTR_ERR(prog);
3347 fd = bpf_prog_new_fd(prog);
3354 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3356 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3358 struct bpf_map *map;
3359 u32 id = attr->map_id;
3363 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3364 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3367 if (!capable(CAP_SYS_ADMIN))
3370 f_flags = bpf_get_file_flag(attr->open_flags);
3374 spin_lock_bh(&map_idr_lock);
3375 map = idr_find(&map_idr, id);
3377 map = __bpf_map_inc_not_zero(map, true);
3379 map = ERR_PTR(-ENOENT);
3380 spin_unlock_bh(&map_idr_lock);
3383 return PTR_ERR(map);
3385 fd = bpf_map_new_fd(map, f_flags);
3387 bpf_map_put_with_uref(map);
3392 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3393 unsigned long addr, u32 *off,
3396 const struct bpf_map *map;
3399 mutex_lock(&prog->aux->used_maps_mutex);
3400 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3401 map = prog->aux->used_maps[i];
3402 if (map == (void *)addr) {
3403 *type = BPF_PSEUDO_MAP_FD;
3406 if (!map->ops->map_direct_value_meta)
3408 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3409 *type = BPF_PSEUDO_MAP_VALUE;
3416 mutex_unlock(&prog->aux->used_maps_mutex);
3420 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3421 const struct cred *f_cred)
3423 const struct bpf_map *map;
3424 struct bpf_insn *insns;
3430 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3435 for (i = 0; i < prog->len; i++) {
3436 code = insns[i].code;
3438 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3439 insns[i].code = BPF_JMP | BPF_CALL;
3440 insns[i].imm = BPF_FUNC_tail_call;
3443 if (code == (BPF_JMP | BPF_CALL) ||
3444 code == (BPF_JMP | BPF_CALL_ARGS)) {
3445 if (code == (BPF_JMP | BPF_CALL_ARGS))
3446 insns[i].code = BPF_JMP | BPF_CALL;
3447 if (!bpf_dump_raw_ok(f_cred))
3451 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3452 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3456 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3459 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3460 map = bpf_map_from_imm(prog, imm, &off, &type);
3462 insns[i].src_reg = type;
3463 insns[i].imm = map->id;
3464 insns[i + 1].imm = off;
3472 static int set_info_rec_size(struct bpf_prog_info *info)
3475 * Ensure info.*_rec_size is the same as kernel expected size
3479 * Only allow zero *_rec_size if both _rec_size and _cnt are
3480 * zero. In this case, the kernel will set the expected
3481 * _rec_size back to the info.
3484 if ((info->nr_func_info || info->func_info_rec_size) &&
3485 info->func_info_rec_size != sizeof(struct bpf_func_info))
3488 if ((info->nr_line_info || info->line_info_rec_size) &&
3489 info->line_info_rec_size != sizeof(struct bpf_line_info))
3492 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3493 info->jited_line_info_rec_size != sizeof(__u64))
3496 info->func_info_rec_size = sizeof(struct bpf_func_info);
3497 info->line_info_rec_size = sizeof(struct bpf_line_info);
3498 info->jited_line_info_rec_size = sizeof(__u64);
3503 static int bpf_prog_get_info_by_fd(struct file *file,
3504 struct bpf_prog *prog,
3505 const union bpf_attr *attr,
3506 union bpf_attr __user *uattr)
3508 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3509 struct bpf_prog_info info;
3510 u32 info_len = attr->info.info_len;
3511 struct bpf_prog_stats stats;
3512 char __user *uinsns;
3516 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3519 info_len = min_t(u32, sizeof(info), info_len);
3521 memset(&info, 0, sizeof(info));
3522 if (copy_from_user(&info, uinfo, info_len))
3525 info.type = prog->type;
3526 info.id = prog->aux->id;
3527 info.load_time = prog->aux->load_time;
3528 info.created_by_uid = from_kuid_munged(current_user_ns(),
3529 prog->aux->user->uid);
3530 info.gpl_compatible = prog->gpl_compatible;
3532 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3533 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3535 mutex_lock(&prog->aux->used_maps_mutex);
3536 ulen = info.nr_map_ids;
3537 info.nr_map_ids = prog->aux->used_map_cnt;
3538 ulen = min_t(u32, info.nr_map_ids, ulen);
3540 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3543 for (i = 0; i < ulen; i++)
3544 if (put_user(prog->aux->used_maps[i]->id,
3545 &user_map_ids[i])) {
3546 mutex_unlock(&prog->aux->used_maps_mutex);
3550 mutex_unlock(&prog->aux->used_maps_mutex);
3552 err = set_info_rec_size(&info);
3556 bpf_prog_get_stats(prog, &stats);
3557 info.run_time_ns = stats.nsecs;
3558 info.run_cnt = stats.cnt;
3559 info.recursion_misses = stats.misses;
3561 if (!bpf_capable()) {
3562 info.jited_prog_len = 0;
3563 info.xlated_prog_len = 0;
3564 info.nr_jited_ksyms = 0;
3565 info.nr_jited_func_lens = 0;
3566 info.nr_func_info = 0;
3567 info.nr_line_info = 0;
3568 info.nr_jited_line_info = 0;
3572 ulen = info.xlated_prog_len;
3573 info.xlated_prog_len = bpf_prog_insn_size(prog);
3574 if (info.xlated_prog_len && ulen) {
3575 struct bpf_insn *insns_sanitized;
3578 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3579 info.xlated_prog_insns = 0;
3582 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3583 if (!insns_sanitized)
3585 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3586 ulen = min_t(u32, info.xlated_prog_len, ulen);
3587 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3588 kfree(insns_sanitized);
3593 if (bpf_prog_is_dev_bound(prog->aux)) {
3594 err = bpf_prog_offload_info_fill(&info, prog);
3600 /* NOTE: the following code is supposed to be skipped for offload.
3601 * bpf_prog_offload_info_fill() is the place to fill similar fields
3604 ulen = info.jited_prog_len;
3605 if (prog->aux->func_cnt) {
3608 info.jited_prog_len = 0;
3609 for (i = 0; i < prog->aux->func_cnt; i++)
3610 info.jited_prog_len += prog->aux->func[i]->jited_len;
3612 info.jited_prog_len = prog->jited_len;
3615 if (info.jited_prog_len && ulen) {
3616 if (bpf_dump_raw_ok(file->f_cred)) {
3617 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3618 ulen = min_t(u32, info.jited_prog_len, ulen);
3620 /* for multi-function programs, copy the JITed
3621 * instructions for all the functions
3623 if (prog->aux->func_cnt) {
3628 for (i = 0; i < prog->aux->func_cnt; i++) {
3629 len = prog->aux->func[i]->jited_len;
3630 len = min_t(u32, len, free);
3631 img = (u8 *) prog->aux->func[i]->bpf_func;
3632 if (copy_to_user(uinsns, img, len))
3640 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3644 info.jited_prog_insns = 0;
3648 ulen = info.nr_jited_ksyms;
3649 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
3651 if (bpf_dump_raw_ok(file->f_cred)) {
3652 unsigned long ksym_addr;
3653 u64 __user *user_ksyms;
3656 /* copy the address of the kernel symbol
3657 * corresponding to each function
3659 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3660 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3661 if (prog->aux->func_cnt) {
3662 for (i = 0; i < ulen; i++) {
3663 ksym_addr = (unsigned long)
3664 prog->aux->func[i]->bpf_func;
3665 if (put_user((u64) ksym_addr,
3670 ksym_addr = (unsigned long) prog->bpf_func;
3671 if (put_user((u64) ksym_addr, &user_ksyms[0]))
3675 info.jited_ksyms = 0;
3679 ulen = info.nr_jited_func_lens;
3680 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
3682 if (bpf_dump_raw_ok(file->f_cred)) {
3683 u32 __user *user_lens;
3686 /* copy the JITed image lengths for each function */
3687 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3688 user_lens = u64_to_user_ptr(info.jited_func_lens);
3689 if (prog->aux->func_cnt) {
3690 for (i = 0; i < ulen; i++) {
3692 prog->aux->func[i]->jited_len;
3693 if (put_user(func_len, &user_lens[i]))
3697 func_len = prog->jited_len;
3698 if (put_user(func_len, &user_lens[0]))
3702 info.jited_func_lens = 0;
3707 info.btf_id = btf_obj_id(prog->aux->btf);
3709 ulen = info.nr_func_info;
3710 info.nr_func_info = prog->aux->func_info_cnt;
3711 if (info.nr_func_info && ulen) {
3712 char __user *user_finfo;
3714 user_finfo = u64_to_user_ptr(info.func_info);
3715 ulen = min_t(u32, info.nr_func_info, ulen);
3716 if (copy_to_user(user_finfo, prog->aux->func_info,
3717 info.func_info_rec_size * ulen))
3721 ulen = info.nr_line_info;
3722 info.nr_line_info = prog->aux->nr_linfo;
3723 if (info.nr_line_info && ulen) {
3724 __u8 __user *user_linfo;
3726 user_linfo = u64_to_user_ptr(info.line_info);
3727 ulen = min_t(u32, info.nr_line_info, ulen);
3728 if (copy_to_user(user_linfo, prog->aux->linfo,
3729 info.line_info_rec_size * ulen))
3733 ulen = info.nr_jited_line_info;
3734 if (prog->aux->jited_linfo)
3735 info.nr_jited_line_info = prog->aux->nr_linfo;
3737 info.nr_jited_line_info = 0;
3738 if (info.nr_jited_line_info && ulen) {
3739 if (bpf_dump_raw_ok(file->f_cred)) {
3740 __u64 __user *user_linfo;
3743 user_linfo = u64_to_user_ptr(info.jited_line_info);
3744 ulen = min_t(u32, info.nr_jited_line_info, ulen);
3745 for (i = 0; i < ulen; i++) {
3746 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3751 info.jited_line_info = 0;
3755 ulen = info.nr_prog_tags;
3756 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3758 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3761 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3762 ulen = min_t(u32, info.nr_prog_tags, ulen);
3763 if (prog->aux->func_cnt) {
3764 for (i = 0; i < ulen; i++) {
3765 if (copy_to_user(user_prog_tags[i],
3766 prog->aux->func[i]->tag,
3771 if (copy_to_user(user_prog_tags[0],
3772 prog->tag, BPF_TAG_SIZE))
3778 if (copy_to_user(uinfo, &info, info_len) ||
3779 put_user(info_len, &uattr->info.info_len))
3785 static int bpf_map_get_info_by_fd(struct file *file,
3786 struct bpf_map *map,
3787 const union bpf_attr *attr,
3788 union bpf_attr __user *uattr)
3790 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3791 struct bpf_map_info info;
3792 u32 info_len = attr->info.info_len;
3795 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3798 info_len = min_t(u32, sizeof(info), info_len);
3800 memset(&info, 0, sizeof(info));
3801 info.type = map->map_type;
3803 info.key_size = map->key_size;
3804 info.value_size = map->value_size;
3805 info.max_entries = map->max_entries;
3806 info.map_flags = map->map_flags;
3807 memcpy(info.name, map->name, sizeof(map->name));
3810 info.btf_id = btf_obj_id(map->btf);
3811 info.btf_key_type_id = map->btf_key_type_id;
3812 info.btf_value_type_id = map->btf_value_type_id;
3814 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
3816 if (bpf_map_is_dev_bound(map)) {
3817 err = bpf_map_offload_info_fill(&info, map);
3822 if (copy_to_user(uinfo, &info, info_len) ||
3823 put_user(info_len, &uattr->info.info_len))
3829 static int bpf_btf_get_info_by_fd(struct file *file,
3831 const union bpf_attr *attr,
3832 union bpf_attr __user *uattr)
3834 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3835 u32 info_len = attr->info.info_len;
3838 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
3842 return btf_get_info_by_fd(btf, attr, uattr);
3845 static int bpf_link_get_info_by_fd(struct file *file,
3846 struct bpf_link *link,
3847 const union bpf_attr *attr,
3848 union bpf_attr __user *uattr)
3850 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3851 struct bpf_link_info info;
3852 u32 info_len = attr->info.info_len;
3855 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3858 info_len = min_t(u32, sizeof(info), info_len);
3860 memset(&info, 0, sizeof(info));
3861 if (copy_from_user(&info, uinfo, info_len))
3864 info.type = link->type;
3866 info.prog_id = link->prog->aux->id;
3868 if (link->ops->fill_link_info) {
3869 err = link->ops->fill_link_info(link, &info);
3874 if (copy_to_user(uinfo, &info, info_len) ||
3875 put_user(info_len, &uattr->info.info_len))
3882 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3884 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3885 union bpf_attr __user *uattr)
3887 int ufd = attr->info.bpf_fd;
3891 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3898 if (f.file->f_op == &bpf_prog_fops)
3899 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
3901 else if (f.file->f_op == &bpf_map_fops)
3902 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
3904 else if (f.file->f_op == &btf_fops)
3905 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
3906 else if (f.file->f_op == &bpf_link_fops)
3907 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
3916 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3918 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
3920 if (CHECK_ATTR(BPF_BTF_LOAD))
3926 return btf_new_fd(attr, uattr);
3929 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3931 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3933 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3936 if (!capable(CAP_SYS_ADMIN))
3939 return btf_get_fd_by_id(attr->btf_id);
3942 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3943 union bpf_attr __user *uattr,
3944 u32 prog_id, u32 fd_type,
3945 const char *buf, u64 probe_offset,
3948 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3949 u32 len = buf ? strlen(buf) : 0, input_len;
3952 if (put_user(len, &uattr->task_fd_query.buf_len))
3954 input_len = attr->task_fd_query.buf_len;
3955 if (input_len && ubuf) {
3957 /* nothing to copy, just make ubuf NULL terminated */
3960 if (put_user(zero, ubuf))
3962 } else if (input_len >= len + 1) {
3963 /* ubuf can hold the string with NULL terminator */
3964 if (copy_to_user(ubuf, buf, len + 1))
3967 /* ubuf cannot hold the string with NULL terminator,
3968 * do a partial copy with NULL terminator.
3973 if (copy_to_user(ubuf, buf, input_len - 1))
3975 if (put_user(zero, ubuf + input_len - 1))
3980 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3981 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3982 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3983 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3989 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3991 static int bpf_task_fd_query(const union bpf_attr *attr,
3992 union bpf_attr __user *uattr)
3994 pid_t pid = attr->task_fd_query.pid;
3995 u32 fd = attr->task_fd_query.fd;
3996 const struct perf_event *event;
3997 struct task_struct *task;
4001 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
4004 if (!capable(CAP_SYS_ADMIN))
4007 if (attr->task_fd_query.flags != 0)
4010 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
4015 file = fget_task(task, fd);
4016 put_task_struct(task);
4020 if (file->f_op == &bpf_link_fops) {
4021 struct bpf_link *link = file->private_data;
4023 if (link->ops == &bpf_raw_tp_link_lops) {
4024 struct bpf_raw_tp_link *raw_tp =
4025 container_of(link, struct bpf_raw_tp_link, link);
4026 struct bpf_raw_event_map *btp = raw_tp->btp;
4028 err = bpf_task_fd_query_copy(attr, uattr,
4029 raw_tp->link.prog->aux->id,
4030 BPF_FD_TYPE_RAW_TRACEPOINT,
4031 btp->tp->name, 0, 0);
4037 event = perf_get_event(file);
4038 if (!IS_ERR(event)) {
4039 u64 probe_offset, probe_addr;
4040 u32 prog_id, fd_type;
4043 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4044 &buf, &probe_offset,
4047 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4061 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
4063 #define BPF_DO_BATCH(fn) \
4069 err = fn(map, attr, uattr); \
4072 static int bpf_map_do_batch(const union bpf_attr *attr,
4073 union bpf_attr __user *uattr,
4076 struct bpf_map *map;
4080 if (CHECK_ATTR(BPF_MAP_BATCH))
4083 ufd = attr->batch.map_fd;
4085 map = __bpf_map_get(f);
4087 return PTR_ERR(map);
4089 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
4090 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
4091 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4096 if (cmd != BPF_MAP_LOOKUP_BATCH &&
4097 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4102 if (cmd == BPF_MAP_LOOKUP_BATCH)
4103 BPF_DO_BATCH(map->ops->map_lookup_batch);
4104 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4105 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4106 else if (cmd == BPF_MAP_UPDATE_BATCH)
4107 BPF_DO_BATCH(map->ops->map_update_batch);
4109 BPF_DO_BATCH(map->ops->map_delete_batch);
4116 static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
4117 struct bpf_prog *prog)
4119 if (attr->link_create.attach_type != prog->expected_attach_type)
4122 if (prog->expected_attach_type == BPF_TRACE_ITER)
4123 return bpf_iter_link_attach(attr, uattr, prog);
4124 else if (prog->type == BPF_PROG_TYPE_EXT)
4125 return bpf_tracing_prog_attach(prog,
4126 attr->link_create.target_fd,
4127 attr->link_create.target_btf_id);
4131 #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
4132 static int link_create(union bpf_attr *attr, bpfptr_t uattr)
4134 enum bpf_prog_type ptype;
4135 struct bpf_prog *prog;
4138 if (CHECK_ATTR(BPF_LINK_CREATE))
4141 prog = bpf_prog_get(attr->link_create.prog_fd);
4143 return PTR_ERR(prog);
4145 ret = bpf_prog_attach_check_attach_type(prog,
4146 attr->link_create.attach_type);
4150 if (prog->type == BPF_PROG_TYPE_EXT) {
4151 ret = tracing_bpf_link_attach(attr, uattr, prog);
4155 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4156 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4162 case BPF_PROG_TYPE_CGROUP_SKB:
4163 case BPF_PROG_TYPE_CGROUP_SOCK:
4164 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4165 case BPF_PROG_TYPE_SOCK_OPS:
4166 case BPF_PROG_TYPE_CGROUP_DEVICE:
4167 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4168 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4169 ret = cgroup_bpf_link_attach(attr, prog);
4171 case BPF_PROG_TYPE_TRACING:
4172 ret = tracing_bpf_link_attach(attr, uattr, prog);
4174 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4175 case BPF_PROG_TYPE_SK_LOOKUP:
4176 ret = netns_bpf_link_create(attr, prog);
4179 case BPF_PROG_TYPE_XDP:
4180 ret = bpf_xdp_link_attach(attr, prog);
4193 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4195 static int link_update(union bpf_attr *attr)
4197 struct bpf_prog *old_prog = NULL, *new_prog;
4198 struct bpf_link *link;
4202 if (CHECK_ATTR(BPF_LINK_UPDATE))
4205 flags = attr->link_update.flags;
4206 if (flags & ~BPF_F_REPLACE)
4209 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4211 return PTR_ERR(link);
4213 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4214 if (IS_ERR(new_prog)) {
4215 ret = PTR_ERR(new_prog);
4219 if (flags & BPF_F_REPLACE) {
4220 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4221 if (IS_ERR(old_prog)) {
4222 ret = PTR_ERR(old_prog);
4226 } else if (attr->link_update.old_prog_fd) {
4231 if (link->ops->update_prog)
4232 ret = link->ops->update_prog(link, new_prog, old_prog);
4238 bpf_prog_put(old_prog);
4240 bpf_prog_put(new_prog);
4246 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4248 static int link_detach(union bpf_attr *attr)
4250 struct bpf_link *link;
4253 if (CHECK_ATTR(BPF_LINK_DETACH))
4256 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4258 return PTR_ERR(link);
4260 if (link->ops->detach)
4261 ret = link->ops->detach(link);
4269 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4271 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4274 struct bpf_link *bpf_link_by_id(u32 id)
4276 struct bpf_link *link;
4279 return ERR_PTR(-ENOENT);
4281 spin_lock_bh(&link_idr_lock);
4282 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4283 link = idr_find(&link_idr, id);
4286 link = bpf_link_inc_not_zero(link);
4288 link = ERR_PTR(-EAGAIN);
4290 link = ERR_PTR(-ENOENT);
4292 spin_unlock_bh(&link_idr_lock);
4296 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4298 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4300 struct bpf_link *link;
4301 u32 id = attr->link_id;
4304 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4307 if (!capable(CAP_SYS_ADMIN))
4310 link = bpf_link_by_id(id);
4312 return PTR_ERR(link);
4314 fd = bpf_link_new_fd(link);
4321 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4323 static int bpf_stats_release(struct inode *inode, struct file *file)
4325 mutex_lock(&bpf_stats_enabled_mutex);
4326 static_key_slow_dec(&bpf_stats_enabled_key.key);
4327 mutex_unlock(&bpf_stats_enabled_mutex);
4331 static const struct file_operations bpf_stats_fops = {
4332 .release = bpf_stats_release,
4335 static int bpf_enable_runtime_stats(void)
4339 mutex_lock(&bpf_stats_enabled_mutex);
4341 /* Set a very high limit to avoid overflow */
4342 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4343 mutex_unlock(&bpf_stats_enabled_mutex);
4347 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4349 static_key_slow_inc(&bpf_stats_enabled_key.key);
4351 mutex_unlock(&bpf_stats_enabled_mutex);
4355 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4357 static int bpf_enable_stats(union bpf_attr *attr)
4360 if (CHECK_ATTR(BPF_ENABLE_STATS))
4363 if (!capable(CAP_SYS_ADMIN))
4366 switch (attr->enable_stats.type) {
4367 case BPF_STATS_RUN_TIME:
4368 return bpf_enable_runtime_stats();
4375 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4377 static int bpf_iter_create(union bpf_attr *attr)
4379 struct bpf_link *link;
4382 if (CHECK_ATTR(BPF_ITER_CREATE))
4385 if (attr->iter_create.flags)
4388 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4390 return PTR_ERR(link);
4392 err = bpf_iter_new_fd(link);
4398 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4400 static int bpf_prog_bind_map(union bpf_attr *attr)
4402 struct bpf_prog *prog;
4403 struct bpf_map *map;
4404 struct bpf_map **used_maps_old, **used_maps_new;
4407 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4410 if (attr->prog_bind_map.flags)
4413 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4415 return PTR_ERR(prog);
4417 map = bpf_map_get(attr->prog_bind_map.map_fd);
4423 mutex_lock(&prog->aux->used_maps_mutex);
4425 used_maps_old = prog->aux->used_maps;
4427 for (i = 0; i < prog->aux->used_map_cnt; i++)
4428 if (used_maps_old[i] == map) {
4433 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4434 sizeof(used_maps_new[0]),
4436 if (!used_maps_new) {
4441 memcpy(used_maps_new, used_maps_old,
4442 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4443 used_maps_new[prog->aux->used_map_cnt] = map;
4445 prog->aux->used_map_cnt++;
4446 prog->aux->used_maps = used_maps_new;
4448 kfree(used_maps_old);
4451 mutex_unlock(&prog->aux->used_maps_mutex);
4460 static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
4462 union bpf_attr attr;
4465 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
4468 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4471 size = min_t(u32, size, sizeof(attr));
4473 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4474 memset(&attr, 0, sizeof(attr));
4475 if (copy_from_bpfptr(&attr, uattr, size) != 0)
4478 err = security_bpf(cmd, &attr, size);
4483 case BPF_MAP_CREATE:
4484 err = map_create(&attr);
4486 case BPF_MAP_LOOKUP_ELEM:
4487 err = map_lookup_elem(&attr);
4489 case BPF_MAP_UPDATE_ELEM:
4490 err = map_update_elem(&attr, uattr);
4492 case BPF_MAP_DELETE_ELEM:
4493 err = map_delete_elem(&attr);
4495 case BPF_MAP_GET_NEXT_KEY:
4496 err = map_get_next_key(&attr);
4498 case BPF_MAP_FREEZE:
4499 err = map_freeze(&attr);
4502 err = bpf_prog_load(&attr, uattr);
4505 err = bpf_obj_pin(&attr);
4508 err = bpf_obj_get(&attr);
4510 case BPF_PROG_ATTACH:
4511 err = bpf_prog_attach(&attr);
4513 case BPF_PROG_DETACH:
4514 err = bpf_prog_detach(&attr);
4516 case BPF_PROG_QUERY:
4517 err = bpf_prog_query(&attr, uattr.user);
4519 case BPF_PROG_TEST_RUN:
4520 err = bpf_prog_test_run(&attr, uattr.user);
4522 case BPF_PROG_GET_NEXT_ID:
4523 err = bpf_obj_get_next_id(&attr, uattr.user,
4524 &prog_idr, &prog_idr_lock);
4526 case BPF_MAP_GET_NEXT_ID:
4527 err = bpf_obj_get_next_id(&attr, uattr.user,
4528 &map_idr, &map_idr_lock);
4530 case BPF_BTF_GET_NEXT_ID:
4531 err = bpf_obj_get_next_id(&attr, uattr.user,
4532 &btf_idr, &btf_idr_lock);
4534 case BPF_PROG_GET_FD_BY_ID:
4535 err = bpf_prog_get_fd_by_id(&attr);
4537 case BPF_MAP_GET_FD_BY_ID:
4538 err = bpf_map_get_fd_by_id(&attr);
4540 case BPF_OBJ_GET_INFO_BY_FD:
4541 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
4543 case BPF_RAW_TRACEPOINT_OPEN:
4544 err = bpf_raw_tracepoint_open(&attr);
4547 err = bpf_btf_load(&attr, uattr);
4549 case BPF_BTF_GET_FD_BY_ID:
4550 err = bpf_btf_get_fd_by_id(&attr);
4552 case BPF_TASK_FD_QUERY:
4553 err = bpf_task_fd_query(&attr, uattr.user);
4555 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4556 err = map_lookup_and_delete_elem(&attr);
4558 case BPF_MAP_LOOKUP_BATCH:
4559 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
4561 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4562 err = bpf_map_do_batch(&attr, uattr.user,
4563 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4565 case BPF_MAP_UPDATE_BATCH:
4566 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
4568 case BPF_MAP_DELETE_BATCH:
4569 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
4571 case BPF_LINK_CREATE:
4572 err = link_create(&attr, uattr);
4574 case BPF_LINK_UPDATE:
4575 err = link_update(&attr);
4577 case BPF_LINK_GET_FD_BY_ID:
4578 err = bpf_link_get_fd_by_id(&attr);
4580 case BPF_LINK_GET_NEXT_ID:
4581 err = bpf_obj_get_next_id(&attr, uattr.user,
4582 &link_idr, &link_idr_lock);
4584 case BPF_ENABLE_STATS:
4585 err = bpf_enable_stats(&attr);
4587 case BPF_ITER_CREATE:
4588 err = bpf_iter_create(&attr);
4590 case BPF_LINK_DETACH:
4591 err = link_detach(&attr);
4593 case BPF_PROG_BIND_MAP:
4594 err = bpf_prog_bind_map(&attr);
4604 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4606 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
4609 static bool syscall_prog_is_valid_access(int off, int size,
4610 enum bpf_access_type type,
4611 const struct bpf_prog *prog,
4612 struct bpf_insn_access_aux *info)
4614 if (off < 0 || off >= U16_MAX)
4616 if (off % size != 0)
4621 BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size)
4624 case BPF_MAP_CREATE:
4625 case BPF_MAP_UPDATE_ELEM:
4626 case BPF_MAP_FREEZE:
4630 /* case BPF_PROG_TEST_RUN:
4631 * is not part of this list to prevent recursive test_run
4636 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
4639 static const struct bpf_func_proto bpf_sys_bpf_proto = {
4640 .func = bpf_sys_bpf,
4642 .ret_type = RET_INTEGER,
4643 .arg1_type = ARG_ANYTHING,
4644 .arg2_type = ARG_PTR_TO_MEM,
4645 .arg3_type = ARG_CONST_SIZE,
4648 const struct bpf_func_proto * __weak
4649 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4651 return bpf_base_func_proto(func_id);
4654 BPF_CALL_1(bpf_sys_close, u32, fd)
4656 /* When bpf program calls this helper there should not be
4657 * an fdget() without matching completed fdput().
4658 * This helper is allowed in the following callchain only:
4659 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
4661 return close_fd(fd);
4664 static const struct bpf_func_proto bpf_sys_close_proto = {
4665 .func = bpf_sys_close,
4667 .ret_type = RET_INTEGER,
4668 .arg1_type = ARG_ANYTHING,
4671 static const struct bpf_func_proto *
4672 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4675 case BPF_FUNC_sys_bpf:
4676 return &bpf_sys_bpf_proto;
4677 case BPF_FUNC_btf_find_by_name_kind:
4678 return &bpf_btf_find_by_name_kind_proto;
4679 case BPF_FUNC_sys_close:
4680 return &bpf_sys_close_proto;
4682 return tracing_prog_func_proto(func_id, prog);
4686 const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
4687 .get_func_proto = syscall_prog_func_proto,
4688 .is_valid_access = syscall_prog_is_valid_access,
4691 const struct bpf_prog_ops bpf_syscall_prog_ops = {
4692 .test_run = bpf_prog_test_run_syscall,