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>
8 #include <linux/syscalls.h>
9 #include <linux/slab.h>
10 #include <linux/sched/signal.h>
11 #include <linux/vmalloc.h>
12 #include <linux/mmzone.h>
13 #include <linux/anon_inodes.h>
14 #include <linux/fdtable.h>
15 #include <linux/file.h>
17 #include <linux/license.h>
18 #include <linux/filter.h>
19 #include <linux/version.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 <uapi/linux/btf.h>
28 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
29 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
31 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
32 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
33 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
36 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
38 DEFINE_PER_CPU(int, bpf_prog_active
);
39 static DEFINE_IDR(prog_idr
);
40 static DEFINE_SPINLOCK(prog_idr_lock
);
41 static DEFINE_IDR(map_idr
);
42 static DEFINE_SPINLOCK(map_idr_lock
);
44 int sysctl_unprivileged_bpf_disabled __read_mostly
;
46 static const struct bpf_map_ops
* const bpf_map_types
[] = {
47 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
48 #define BPF_MAP_TYPE(_id, _ops) \
50 #include <linux/bpf_types.h>
56 * If we're handed a bigger struct than we know of, ensure all the unknown bits
57 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
58 * we don't know about yet.
60 * There is a ToCToU between this function call and the following
61 * copy_from_user() call. However, this is not a concern since this function is
62 * meant to be a future-proofing of bits.
64 int bpf_check_uarg_tail_zero(void __user
*uaddr
,
68 unsigned char __user
*addr
;
69 unsigned char __user
*end
;
73 if (unlikely(actual_size
> PAGE_SIZE
)) /* silly large */
76 if (unlikely(!access_ok(uaddr
, actual_size
)))
79 if (actual_size
<= expected_size
)
82 addr
= uaddr
+ expected_size
;
83 end
= uaddr
+ actual_size
;
85 for (; addr
< end
; addr
++) {
86 err
= get_user(val
, addr
);
96 const struct bpf_map_ops bpf_map_offload_ops
= {
97 .map_alloc
= bpf_map_offload_map_alloc
,
98 .map_free
= bpf_map_offload_map_free
,
99 .map_check_btf
= map_check_no_btf
,
102 static struct bpf_map
*find_and_alloc_map(union bpf_attr
*attr
)
104 const struct bpf_map_ops
*ops
;
105 u32 type
= attr
->map_type
;
109 if (type
>= ARRAY_SIZE(bpf_map_types
))
110 return ERR_PTR(-EINVAL
);
111 type
= array_index_nospec(type
, ARRAY_SIZE(bpf_map_types
));
112 ops
= bpf_map_types
[type
];
114 return ERR_PTR(-EINVAL
);
116 if (ops
->map_alloc_check
) {
117 err
= ops
->map_alloc_check(attr
);
121 if (attr
->map_ifindex
)
122 ops
= &bpf_map_offload_ops
;
123 map
= ops
->map_alloc(attr
);
127 map
->map_type
= type
;
131 static void *__bpf_map_area_alloc(u64 size
, int numa_node
, bool mmapable
)
133 /* We really just want to fail instead of triggering OOM killer
134 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
135 * which is used for lower order allocation requests.
137 * It has been observed that higher order allocation requests done by
138 * vmalloc with __GFP_NORETRY being set might fail due to not trying
139 * to reclaim memory from the page cache, thus we set
140 * __GFP_RETRY_MAYFAIL to avoid such situations.
143 const gfp_t flags
= __GFP_NOWARN
| __GFP_ZERO
;
146 if (size
>= SIZE_MAX
)
149 /* kmalloc()'ed memory can't be mmap()'ed */
150 if (!mmapable
&& size
<= (PAGE_SIZE
<< PAGE_ALLOC_COSTLY_ORDER
)) {
151 area
= kmalloc_node(size
, GFP_USER
| __GFP_NORETRY
| flags
,
157 BUG_ON(!PAGE_ALIGNED(size
));
158 return vmalloc_user_node_flags(size
, numa_node
, GFP_KERNEL
|
159 __GFP_RETRY_MAYFAIL
| flags
);
161 return __vmalloc_node_flags_caller(size
, numa_node
,
162 GFP_KERNEL
| __GFP_RETRY_MAYFAIL
|
163 flags
, __builtin_return_address(0));
166 void *bpf_map_area_alloc(u64 size
, int numa_node
)
168 return __bpf_map_area_alloc(size
, numa_node
, false);
171 void *bpf_map_area_mmapable_alloc(u64 size
, int numa_node
)
173 return __bpf_map_area_alloc(size
, numa_node
, true);
176 void bpf_map_area_free(void *area
)
181 static u32
bpf_map_flags_retain_permanent(u32 flags
)
183 /* Some map creation flags are not tied to the map object but
184 * rather to the map fd instead, so they have no meaning upon
185 * map object inspection since multiple file descriptors with
186 * different (access) properties can exist here. Thus, given
187 * this has zero meaning for the map itself, lets clear these
190 return flags
& ~(BPF_F_RDONLY
| BPF_F_WRONLY
);
193 void bpf_map_init_from_attr(struct bpf_map
*map
, union bpf_attr
*attr
)
195 map
->map_type
= attr
->map_type
;
196 map
->key_size
= attr
->key_size
;
197 map
->value_size
= attr
->value_size
;
198 map
->max_entries
= attr
->max_entries
;
199 map
->map_flags
= bpf_map_flags_retain_permanent(attr
->map_flags
);
200 map
->numa_node
= bpf_map_attr_numa_node(attr
);
203 static int bpf_charge_memlock(struct user_struct
*user
, u32 pages
)
205 unsigned long memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
207 if (atomic_long_add_return(pages
, &user
->locked_vm
) > memlock_limit
) {
208 atomic_long_sub(pages
, &user
->locked_vm
);
214 static void bpf_uncharge_memlock(struct user_struct
*user
, u32 pages
)
217 atomic_long_sub(pages
, &user
->locked_vm
);
220 int bpf_map_charge_init(struct bpf_map_memory
*mem
, u64 size
)
222 u32 pages
= round_up(size
, PAGE_SIZE
) >> PAGE_SHIFT
;
223 struct user_struct
*user
;
226 if (size
>= U32_MAX
- PAGE_SIZE
)
229 user
= get_current_user();
230 ret
= bpf_charge_memlock(user
, pages
);
242 void bpf_map_charge_finish(struct bpf_map_memory
*mem
)
244 bpf_uncharge_memlock(mem
->user
, mem
->pages
);
248 void bpf_map_charge_move(struct bpf_map_memory
*dst
,
249 struct bpf_map_memory
*src
)
253 /* Make sure src will not be used for the redundant uncharging. */
254 memset(src
, 0, sizeof(struct bpf_map_memory
));
257 int bpf_map_charge_memlock(struct bpf_map
*map
, u32 pages
)
261 ret
= bpf_charge_memlock(map
->memory
.user
, pages
);
264 map
->memory
.pages
+= pages
;
268 void bpf_map_uncharge_memlock(struct bpf_map
*map
, u32 pages
)
270 bpf_uncharge_memlock(map
->memory
.user
, pages
);
271 map
->memory
.pages
-= pages
;
274 static int bpf_map_alloc_id(struct bpf_map
*map
)
278 idr_preload(GFP_KERNEL
);
279 spin_lock_bh(&map_idr_lock
);
280 id
= idr_alloc_cyclic(&map_idr
, map
, 1, INT_MAX
, GFP_ATOMIC
);
283 spin_unlock_bh(&map_idr_lock
);
286 if (WARN_ON_ONCE(!id
))
289 return id
> 0 ? 0 : id
;
292 void bpf_map_free_id(struct bpf_map
*map
, bool do_idr_lock
)
296 /* Offloaded maps are removed from the IDR store when their device
297 * disappears - even if someone holds an fd to them they are unusable,
298 * the memory is gone, all ops will fail; they are simply waiting for
299 * refcnt to drop to be freed.
305 spin_lock_irqsave(&map_idr_lock
, flags
);
307 __acquire(&map_idr_lock
);
309 idr_remove(&map_idr
, map
->id
);
313 spin_unlock_irqrestore(&map_idr_lock
, flags
);
315 __release(&map_idr_lock
);
318 /* called from workqueue */
319 static void bpf_map_free_deferred(struct work_struct
*work
)
321 struct bpf_map
*map
= container_of(work
, struct bpf_map
, work
);
322 struct bpf_map_memory mem
;
324 bpf_map_charge_move(&mem
, &map
->memory
);
325 security_bpf_map_free(map
);
326 /* implementation dependent freeing */
327 map
->ops
->map_free(map
);
328 bpf_map_charge_finish(&mem
);
331 static void bpf_map_put_uref(struct bpf_map
*map
)
333 if (atomic64_dec_and_test(&map
->usercnt
)) {
334 if (map
->ops
->map_release_uref
)
335 map
->ops
->map_release_uref(map
);
339 /* decrement map refcnt and schedule it for freeing via workqueue
340 * (unrelying map implementation ops->map_free() might sleep)
342 static void __bpf_map_put(struct bpf_map
*map
, bool do_idr_lock
)
344 if (atomic64_dec_and_test(&map
->refcnt
)) {
345 /* bpf_map_free_id() must be called first */
346 bpf_map_free_id(map
, do_idr_lock
);
348 INIT_WORK(&map
->work
, bpf_map_free_deferred
);
349 schedule_work(&map
->work
);
353 void bpf_map_put(struct bpf_map
*map
)
355 __bpf_map_put(map
, true);
357 EXPORT_SYMBOL_GPL(bpf_map_put
);
359 void bpf_map_put_with_uref(struct bpf_map
*map
)
361 bpf_map_put_uref(map
);
365 static int bpf_map_release(struct inode
*inode
, struct file
*filp
)
367 struct bpf_map
*map
= filp
->private_data
;
369 if (map
->ops
->map_release
)
370 map
->ops
->map_release(map
, filp
);
372 bpf_map_put_with_uref(map
);
376 static fmode_t
map_get_sys_perms(struct bpf_map
*map
, struct fd f
)
378 fmode_t mode
= f
.file
->f_mode
;
380 /* Our file permissions may have been overridden by global
381 * map permissions facing syscall side.
383 if (READ_ONCE(map
->frozen
))
384 mode
&= ~FMODE_CAN_WRITE
;
388 #ifdef CONFIG_PROC_FS
389 static void bpf_map_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
391 const struct bpf_map
*map
= filp
->private_data
;
392 const struct bpf_array
*array
;
393 u32 type
= 0, jited
= 0;
395 if (map
->map_type
== BPF_MAP_TYPE_PROG_ARRAY
) {
396 array
= container_of(map
, struct bpf_array
, map
);
397 type
= array
->aux
->type
;
398 jited
= array
->aux
->jited
;
415 map
->memory
.pages
* 1ULL << PAGE_SHIFT
,
417 READ_ONCE(map
->frozen
));
419 seq_printf(m
, "owner_prog_type:\t%u\n", type
);
420 seq_printf(m
, "owner_jited:\t%u\n", jited
);
425 static ssize_t
bpf_dummy_read(struct file
*filp
, char __user
*buf
, size_t siz
,
428 /* We need this handler such that alloc_file() enables
429 * f_mode with FMODE_CAN_READ.
434 static ssize_t
bpf_dummy_write(struct file
*filp
, const char __user
*buf
,
435 size_t siz
, loff_t
*ppos
)
437 /* We need this handler such that alloc_file() enables
438 * f_mode with FMODE_CAN_WRITE.
443 /* called for any extra memory-mapped regions (except initial) */
444 static void bpf_map_mmap_open(struct vm_area_struct
*vma
)
446 struct bpf_map
*map
= vma
->vm_file
->private_data
;
448 bpf_map_inc_with_uref(map
);
450 if (vma
->vm_flags
& VM_WRITE
) {
451 mutex_lock(&map
->freeze_mutex
);
453 mutex_unlock(&map
->freeze_mutex
);
457 /* called for all unmapped memory region (including initial) */
458 static void bpf_map_mmap_close(struct vm_area_struct
*vma
)
460 struct bpf_map
*map
= vma
->vm_file
->private_data
;
462 if (vma
->vm_flags
& VM_WRITE
) {
463 mutex_lock(&map
->freeze_mutex
);
465 mutex_unlock(&map
->freeze_mutex
);
468 bpf_map_put_with_uref(map
);
471 static const struct vm_operations_struct bpf_map_default_vmops
= {
472 .open
= bpf_map_mmap_open
,
473 .close
= bpf_map_mmap_close
,
476 static int bpf_map_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
478 struct bpf_map
*map
= filp
->private_data
;
481 if (!map
->ops
->map_mmap
|| map_value_has_spin_lock(map
))
484 if (!(vma
->vm_flags
& VM_SHARED
))
487 mutex_lock(&map
->freeze_mutex
);
489 if ((vma
->vm_flags
& VM_WRITE
) && map
->frozen
) {
494 /* set default open/close callbacks */
495 vma
->vm_ops
= &bpf_map_default_vmops
;
496 vma
->vm_private_data
= map
;
498 err
= map
->ops
->map_mmap(map
, vma
);
502 bpf_map_inc_with_uref(map
);
504 if (vma
->vm_flags
& VM_WRITE
)
507 mutex_unlock(&map
->freeze_mutex
);
511 const struct file_operations bpf_map_fops
= {
512 #ifdef CONFIG_PROC_FS
513 .show_fdinfo
= bpf_map_show_fdinfo
,
515 .release
= bpf_map_release
,
516 .read
= bpf_dummy_read
,
517 .write
= bpf_dummy_write
,
518 .mmap
= bpf_map_mmap
,
521 int bpf_map_new_fd(struct bpf_map
*map
, int flags
)
525 ret
= security_bpf_map(map
, OPEN_FMODE(flags
));
529 return anon_inode_getfd("bpf-map", &bpf_map_fops
, map
,
533 int bpf_get_file_flag(int flags
)
535 if ((flags
& BPF_F_RDONLY
) && (flags
& BPF_F_WRONLY
))
537 if (flags
& BPF_F_RDONLY
)
539 if (flags
& BPF_F_WRONLY
)
544 /* helper macro to check that unused fields 'union bpf_attr' are zero */
545 #define CHECK_ATTR(CMD) \
546 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
547 sizeof(attr->CMD##_LAST_FIELD), 0, \
549 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
550 sizeof(attr->CMD##_LAST_FIELD)) != NULL
552 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
553 * Return 0 on success and < 0 on error.
555 static int bpf_obj_name_cpy(char *dst
, const char *src
)
557 const char *end
= src
+ BPF_OBJ_NAME_LEN
;
559 memset(dst
, 0, BPF_OBJ_NAME_LEN
);
560 /* Copy all isalnum(), '_' and '.' chars. */
561 while (src
< end
&& *src
) {
562 if (!isalnum(*src
) &&
563 *src
!= '_' && *src
!= '.')
568 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
575 int map_check_no_btf(const struct bpf_map
*map
,
576 const struct btf
*btf
,
577 const struct btf_type
*key_type
,
578 const struct btf_type
*value_type
)
583 static int map_check_btf(struct bpf_map
*map
, const struct btf
*btf
,
584 u32 btf_key_id
, u32 btf_value_id
)
586 const struct btf_type
*key_type
, *value_type
;
587 u32 key_size
, value_size
;
590 /* Some maps allow key to be unspecified. */
592 key_type
= btf_type_id_size(btf
, &btf_key_id
, &key_size
);
593 if (!key_type
|| key_size
!= map
->key_size
)
596 key_type
= btf_type_by_id(btf
, 0);
597 if (!map
->ops
->map_check_btf
)
601 value_type
= btf_type_id_size(btf
, &btf_value_id
, &value_size
);
602 if (!value_type
|| value_size
!= map
->value_size
)
605 map
->spin_lock_off
= btf_find_spin_lock(btf
, value_type
);
607 if (map_value_has_spin_lock(map
)) {
608 if (map
->map_flags
& BPF_F_RDONLY_PROG
)
610 if (map
->map_type
!= BPF_MAP_TYPE_HASH
&&
611 map
->map_type
!= BPF_MAP_TYPE_ARRAY
&&
612 map
->map_type
!= BPF_MAP_TYPE_CGROUP_STORAGE
&&
613 map
->map_type
!= BPF_MAP_TYPE_SK_STORAGE
)
615 if (map
->spin_lock_off
+ sizeof(struct bpf_spin_lock
) >
618 "verifier bug spin_lock_off %d value_size %d\n",
619 map
->spin_lock_off
, map
->value_size
);
624 if (map
->ops
->map_check_btf
)
625 ret
= map
->ops
->map_check_btf(map
, btf
, key_type
, value_type
);
630 #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
631 /* called via syscall */
632 static int map_create(union bpf_attr
*attr
)
634 int numa_node
= bpf_map_attr_numa_node(attr
);
635 struct bpf_map_memory mem
;
640 err
= CHECK_ATTR(BPF_MAP_CREATE
);
644 f_flags
= bpf_get_file_flag(attr
->map_flags
);
648 if (numa_node
!= NUMA_NO_NODE
&&
649 ((unsigned int)numa_node
>= nr_node_ids
||
650 !node_online(numa_node
)))
653 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
654 map
= find_and_alloc_map(attr
);
658 err
= bpf_obj_name_cpy(map
->name
, attr
->map_name
);
662 atomic64_set(&map
->refcnt
, 1);
663 atomic64_set(&map
->usercnt
, 1);
664 mutex_init(&map
->freeze_mutex
);
666 if (attr
->btf_key_type_id
|| attr
->btf_value_type_id
) {
669 if (!attr
->btf_value_type_id
) {
674 btf
= btf_get_by_fd(attr
->btf_fd
);
680 err
= map_check_btf(map
, btf
, attr
->btf_key_type_id
,
681 attr
->btf_value_type_id
);
688 map
->btf_key_type_id
= attr
->btf_key_type_id
;
689 map
->btf_value_type_id
= attr
->btf_value_type_id
;
691 map
->spin_lock_off
= -EINVAL
;
694 err
= security_bpf_map_alloc(map
);
698 err
= bpf_map_alloc_id(map
);
702 err
= bpf_map_new_fd(map
, f_flags
);
704 /* failed to allocate fd.
705 * bpf_map_put_with_uref() is needed because the above
706 * bpf_map_alloc_id() has published the map
707 * to the userspace and the userspace may
708 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
710 bpf_map_put_with_uref(map
);
717 security_bpf_map_free(map
);
720 bpf_map_charge_move(&mem
, &map
->memory
);
721 map
->ops
->map_free(map
);
722 bpf_map_charge_finish(&mem
);
726 /* if error is returned, fd is released.
727 * On success caller should complete fd access with matching fdput()
729 struct bpf_map
*__bpf_map_get(struct fd f
)
732 return ERR_PTR(-EBADF
);
733 if (f
.file
->f_op
!= &bpf_map_fops
) {
735 return ERR_PTR(-EINVAL
);
738 return f
.file
->private_data
;
741 void bpf_map_inc(struct bpf_map
*map
)
743 atomic64_inc(&map
->refcnt
);
745 EXPORT_SYMBOL_GPL(bpf_map_inc
);
747 void bpf_map_inc_with_uref(struct bpf_map
*map
)
749 atomic64_inc(&map
->refcnt
);
750 atomic64_inc(&map
->usercnt
);
752 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref
);
754 struct bpf_map
*bpf_map_get_with_uref(u32 ufd
)
756 struct fd f
= fdget(ufd
);
759 map
= __bpf_map_get(f
);
763 bpf_map_inc_with_uref(map
);
769 /* map_idr_lock should have been held */
770 static struct bpf_map
*__bpf_map_inc_not_zero(struct bpf_map
*map
, bool uref
)
774 refold
= atomic64_fetch_add_unless(&map
->refcnt
, 1, 0);
776 return ERR_PTR(-ENOENT
);
778 atomic64_inc(&map
->usercnt
);
783 struct bpf_map
*bpf_map_inc_not_zero(struct bpf_map
*map
)
785 spin_lock_bh(&map_idr_lock
);
786 map
= __bpf_map_inc_not_zero(map
, false);
787 spin_unlock_bh(&map_idr_lock
);
791 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero
);
793 int __weak
bpf_stackmap_copy(struct bpf_map
*map
, void *key
, void *value
)
798 static void *__bpf_copy_key(void __user
*ukey
, u64 key_size
)
801 return memdup_user(ukey
, key_size
);
804 return ERR_PTR(-EINVAL
);
809 /* last field in 'union bpf_attr' used by this command */
810 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
812 static int map_lookup_elem(union bpf_attr
*attr
)
814 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
815 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
816 int ufd
= attr
->map_fd
;
818 void *key
, *value
, *ptr
;
823 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM
))
826 if (attr
->flags
& ~BPF_F_LOCK
)
830 map
= __bpf_map_get(f
);
833 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_READ
)) {
838 if ((attr
->flags
& BPF_F_LOCK
) &&
839 !map_value_has_spin_lock(map
)) {
844 key
= __bpf_copy_key(ukey
, map
->key_size
);
850 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
851 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
852 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
||
853 map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
)
854 value_size
= round_up(map
->value_size
, 8) * num_possible_cpus();
855 else if (IS_FD_MAP(map
))
856 value_size
= sizeof(u32
);
858 value_size
= map
->value_size
;
861 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
865 if (bpf_map_is_dev_bound(map
)) {
866 err
= bpf_map_offload_lookup_elem(map
, key
, value
);
871 this_cpu_inc(bpf_prog_active
);
872 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
873 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
874 err
= bpf_percpu_hash_copy(map
, key
, value
);
875 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
876 err
= bpf_percpu_array_copy(map
, key
, value
);
877 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
) {
878 err
= bpf_percpu_cgroup_storage_copy(map
, key
, value
);
879 } else if (map
->map_type
== BPF_MAP_TYPE_STACK_TRACE
) {
880 err
= bpf_stackmap_copy(map
, key
, value
);
881 } else if (IS_FD_ARRAY(map
) || IS_FD_PROG_ARRAY(map
)) {
882 err
= bpf_fd_array_map_lookup_elem(map
, key
, value
);
883 } else if (IS_FD_HASH(map
)) {
884 err
= bpf_fd_htab_map_lookup_elem(map
, key
, value
);
885 } else if (map
->map_type
== BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
) {
886 err
= bpf_fd_reuseport_array_lookup_elem(map
, key
, value
);
887 } else if (map
->map_type
== BPF_MAP_TYPE_QUEUE
||
888 map
->map_type
== BPF_MAP_TYPE_STACK
) {
889 err
= map
->ops
->map_peek_elem(map
, value
);
892 if (map
->ops
->map_lookup_elem_sys_only
)
893 ptr
= map
->ops
->map_lookup_elem_sys_only(map
, key
);
895 ptr
= map
->ops
->map_lookup_elem(map
, key
);
902 if (attr
->flags
& BPF_F_LOCK
)
903 /* lock 'ptr' and copy everything but lock */
904 copy_map_value_locked(map
, value
, ptr
, true);
906 copy_map_value(map
, value
, ptr
);
907 /* mask lock, since value wasn't zero inited */
908 check_and_init_map_lock(map
, value
);
912 this_cpu_dec(bpf_prog_active
);
920 if (copy_to_user(uvalue
, value
, value_size
) != 0)
934 static void maybe_wait_bpf_programs(struct bpf_map
*map
)
936 /* Wait for any running BPF programs to complete so that
937 * userspace, when we return to it, knows that all programs
938 * that could be running use the new map value.
940 if (map
->map_type
== BPF_MAP_TYPE_HASH_OF_MAPS
||
941 map
->map_type
== BPF_MAP_TYPE_ARRAY_OF_MAPS
)
945 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
947 static int map_update_elem(union bpf_attr
*attr
)
949 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
950 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
951 int ufd
= attr
->map_fd
;
958 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM
))
962 map
= __bpf_map_get(f
);
965 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_WRITE
)) {
970 if ((attr
->flags
& BPF_F_LOCK
) &&
971 !map_value_has_spin_lock(map
)) {
976 key
= __bpf_copy_key(ukey
, map
->key_size
);
982 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
983 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
984 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
||
985 map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
)
986 value_size
= round_up(map
->value_size
, 8) * num_possible_cpus();
988 value_size
= map
->value_size
;
991 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
996 if (copy_from_user(value
, uvalue
, value_size
) != 0)
999 /* Need to create a kthread, thus must support schedule */
1000 if (bpf_map_is_dev_bound(map
)) {
1001 err
= bpf_map_offload_update_elem(map
, key
, value
, attr
->flags
);
1003 } else if (map
->map_type
== BPF_MAP_TYPE_CPUMAP
||
1004 map
->map_type
== BPF_MAP_TYPE_SOCKHASH
||
1005 map
->map_type
== BPF_MAP_TYPE_SOCKMAP
) {
1006 err
= map
->ops
->map_update_elem(map
, key
, value
, attr
->flags
);
1008 } else if (IS_FD_PROG_ARRAY(map
)) {
1009 err
= bpf_fd_array_map_update_elem(map
, f
.file
, key
, value
,
1014 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
1015 * inside bpf map update or delete otherwise deadlocks are possible
1018 __this_cpu_inc(bpf_prog_active
);
1019 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
1020 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
1021 err
= bpf_percpu_hash_update(map
, key
, value
, attr
->flags
);
1022 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
1023 err
= bpf_percpu_array_update(map
, key
, value
, attr
->flags
);
1024 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
) {
1025 err
= bpf_percpu_cgroup_storage_update(map
, key
, value
,
1027 } else if (IS_FD_ARRAY(map
)) {
1029 err
= bpf_fd_array_map_update_elem(map
, f
.file
, key
, value
,
1032 } else if (map
->map_type
== BPF_MAP_TYPE_HASH_OF_MAPS
) {
1034 err
= bpf_fd_htab_map_update_elem(map
, f
.file
, key
, value
,
1037 } else if (map
->map_type
== BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
) {
1038 /* rcu_read_lock() is not needed */
1039 err
= bpf_fd_reuseport_array_update_elem(map
, key
, value
,
1041 } else if (map
->map_type
== BPF_MAP_TYPE_QUEUE
||
1042 map
->map_type
== BPF_MAP_TYPE_STACK
) {
1043 err
= map
->ops
->map_push_elem(map
, value
, attr
->flags
);
1046 err
= map
->ops
->map_update_elem(map
, key
, value
, attr
->flags
);
1049 __this_cpu_dec(bpf_prog_active
);
1051 maybe_wait_bpf_programs(map
);
1062 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1064 static int map_delete_elem(union bpf_attr
*attr
)
1066 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
1067 int ufd
= attr
->map_fd
;
1068 struct bpf_map
*map
;
1073 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM
))
1077 map
= __bpf_map_get(f
);
1079 return PTR_ERR(map
);
1080 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_WRITE
)) {
1085 key
= __bpf_copy_key(ukey
, map
->key_size
);
1091 if (bpf_map_is_dev_bound(map
)) {
1092 err
= bpf_map_offload_delete_elem(map
, key
);
1094 } else if (IS_FD_PROG_ARRAY(map
)) {
1095 err
= map
->ops
->map_delete_elem(map
, key
);
1100 __this_cpu_inc(bpf_prog_active
);
1102 err
= map
->ops
->map_delete_elem(map
, key
);
1104 __this_cpu_dec(bpf_prog_active
);
1106 maybe_wait_bpf_programs(map
);
1114 /* last field in 'union bpf_attr' used by this command */
1115 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1117 static int map_get_next_key(union bpf_attr
*attr
)
1119 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
1120 void __user
*unext_key
= u64_to_user_ptr(attr
->next_key
);
1121 int ufd
= attr
->map_fd
;
1122 struct bpf_map
*map
;
1123 void *key
, *next_key
;
1127 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY
))
1131 map
= __bpf_map_get(f
);
1133 return PTR_ERR(map
);
1134 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_READ
)) {
1140 key
= __bpf_copy_key(ukey
, map
->key_size
);
1150 next_key
= kmalloc(map
->key_size
, GFP_USER
);
1154 if (bpf_map_is_dev_bound(map
)) {
1155 err
= bpf_map_offload_get_next_key(map
, key
, next_key
);
1160 err
= map
->ops
->map_get_next_key(map
, key
, next_key
);
1167 if (copy_to_user(unext_key
, next_key
, map
->key_size
) != 0)
1181 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1183 static int map_lookup_and_delete_elem(union bpf_attr
*attr
)
1185 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
1186 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
1187 int ufd
= attr
->map_fd
;
1188 struct bpf_map
*map
;
1194 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM
))
1198 map
= __bpf_map_get(f
);
1200 return PTR_ERR(map
);
1201 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_WRITE
)) {
1206 key
= __bpf_copy_key(ukey
, map
->key_size
);
1212 value_size
= map
->value_size
;
1215 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
1219 if (map
->map_type
== BPF_MAP_TYPE_QUEUE
||
1220 map
->map_type
== BPF_MAP_TYPE_STACK
) {
1221 err
= map
->ops
->map_pop_elem(map
, value
);
1229 if (copy_to_user(uvalue
, value
, value_size
) != 0)
1243 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1245 static int map_freeze(const union bpf_attr
*attr
)
1247 int err
= 0, ufd
= attr
->map_fd
;
1248 struct bpf_map
*map
;
1251 if (CHECK_ATTR(BPF_MAP_FREEZE
))
1255 map
= __bpf_map_get(f
);
1257 return PTR_ERR(map
);
1259 mutex_lock(&map
->freeze_mutex
);
1261 if (map
->writecnt
) {
1265 if (READ_ONCE(map
->frozen
)) {
1269 if (!capable(CAP_SYS_ADMIN
)) {
1274 WRITE_ONCE(map
->frozen
, true);
1276 mutex_unlock(&map
->freeze_mutex
);
1281 static const struct bpf_prog_ops
* const bpf_prog_types
[] = {
1282 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1283 [_id] = & _name ## _prog_ops,
1284 #define BPF_MAP_TYPE(_id, _ops)
1285 #include <linux/bpf_types.h>
1286 #undef BPF_PROG_TYPE
1290 static int find_prog_type(enum bpf_prog_type type
, struct bpf_prog
*prog
)
1292 const struct bpf_prog_ops
*ops
;
1294 if (type
>= ARRAY_SIZE(bpf_prog_types
))
1296 type
= array_index_nospec(type
, ARRAY_SIZE(bpf_prog_types
));
1297 ops
= bpf_prog_types
[type
];
1301 if (!bpf_prog_is_dev_bound(prog
->aux
))
1302 prog
->aux
->ops
= ops
;
1304 prog
->aux
->ops
= &bpf_offload_prog_ops
;
1309 int __bpf_prog_charge(struct user_struct
*user
, u32 pages
)
1311 unsigned long memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
1312 unsigned long user_bufs
;
1315 user_bufs
= atomic_long_add_return(pages
, &user
->locked_vm
);
1316 if (user_bufs
> memlock_limit
) {
1317 atomic_long_sub(pages
, &user
->locked_vm
);
1325 void __bpf_prog_uncharge(struct user_struct
*user
, u32 pages
)
1328 atomic_long_sub(pages
, &user
->locked_vm
);
1331 static int bpf_prog_charge_memlock(struct bpf_prog
*prog
)
1333 struct user_struct
*user
= get_current_user();
1336 ret
= __bpf_prog_charge(user
, prog
->pages
);
1342 prog
->aux
->user
= user
;
1346 static void bpf_prog_uncharge_memlock(struct bpf_prog
*prog
)
1348 struct user_struct
*user
= prog
->aux
->user
;
1350 __bpf_prog_uncharge(user
, prog
->pages
);
1354 static int bpf_prog_alloc_id(struct bpf_prog
*prog
)
1358 idr_preload(GFP_KERNEL
);
1359 spin_lock_bh(&prog_idr_lock
);
1360 id
= idr_alloc_cyclic(&prog_idr
, prog
, 1, INT_MAX
, GFP_ATOMIC
);
1363 spin_unlock_bh(&prog_idr_lock
);
1366 /* id is in [1, INT_MAX) */
1367 if (WARN_ON_ONCE(!id
))
1370 return id
> 0 ? 0 : id
;
1373 void bpf_prog_free_id(struct bpf_prog
*prog
, bool do_idr_lock
)
1375 /* cBPF to eBPF migrations are currently not in the idr store.
1376 * Offloaded programs are removed from the store when their device
1377 * disappears - even if someone grabs an fd to them they are unusable,
1378 * simply waiting for refcnt to drop to be freed.
1384 spin_lock_bh(&prog_idr_lock
);
1386 __acquire(&prog_idr_lock
);
1388 idr_remove(&prog_idr
, prog
->aux
->id
);
1392 spin_unlock_bh(&prog_idr_lock
);
1394 __release(&prog_idr_lock
);
1397 static void __bpf_prog_put_rcu(struct rcu_head
*rcu
)
1399 struct bpf_prog_aux
*aux
= container_of(rcu
, struct bpf_prog_aux
, rcu
);
1401 kvfree(aux
->func_info
);
1402 kfree(aux
->func_info_aux
);
1403 bpf_prog_uncharge_memlock(aux
->prog
);
1404 security_bpf_prog_free(aux
);
1405 bpf_prog_free(aux
->prog
);
1408 static void __bpf_prog_put_noref(struct bpf_prog
*prog
, bool deferred
)
1410 bpf_prog_kallsyms_del_all(prog
);
1411 btf_put(prog
->aux
->btf
);
1412 bpf_prog_free_linfo(prog
);
1415 call_rcu(&prog
->aux
->rcu
, __bpf_prog_put_rcu
);
1417 __bpf_prog_put_rcu(&prog
->aux
->rcu
);
1420 static void __bpf_prog_put(struct bpf_prog
*prog
, bool do_idr_lock
)
1422 if (atomic64_dec_and_test(&prog
->aux
->refcnt
)) {
1423 perf_event_bpf_event(prog
, PERF_BPF_EVENT_PROG_UNLOAD
, 0);
1424 /* bpf_prog_free_id() must be called first */
1425 bpf_prog_free_id(prog
, do_idr_lock
);
1426 __bpf_prog_put_noref(prog
, true);
1430 void bpf_prog_put(struct bpf_prog
*prog
)
1432 __bpf_prog_put(prog
, true);
1434 EXPORT_SYMBOL_GPL(bpf_prog_put
);
1436 static int bpf_prog_release(struct inode
*inode
, struct file
*filp
)
1438 struct bpf_prog
*prog
= filp
->private_data
;
1444 static void bpf_prog_get_stats(const struct bpf_prog
*prog
,
1445 struct bpf_prog_stats
*stats
)
1447 u64 nsecs
= 0, cnt
= 0;
1450 for_each_possible_cpu(cpu
) {
1451 const struct bpf_prog_stats
*st
;
1455 st
= per_cpu_ptr(prog
->aux
->stats
, cpu
);
1457 start
= u64_stats_fetch_begin_irq(&st
->syncp
);
1460 } while (u64_stats_fetch_retry_irq(&st
->syncp
, start
));
1464 stats
->nsecs
= nsecs
;
1468 #ifdef CONFIG_PROC_FS
1469 static void bpf_prog_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
1471 const struct bpf_prog
*prog
= filp
->private_data
;
1472 char prog_tag
[sizeof(prog
->tag
) * 2 + 1] = { };
1473 struct bpf_prog_stats stats
;
1475 bpf_prog_get_stats(prog
, &stats
);
1476 bin2hex(prog_tag
, prog
->tag
, sizeof(prog
->tag
));
1483 "run_time_ns:\t%llu\n"
1488 prog
->pages
* 1ULL << PAGE_SHIFT
,
1495 const struct file_operations bpf_prog_fops
= {
1496 #ifdef CONFIG_PROC_FS
1497 .show_fdinfo
= bpf_prog_show_fdinfo
,
1499 .release
= bpf_prog_release
,
1500 .read
= bpf_dummy_read
,
1501 .write
= bpf_dummy_write
,
1504 int bpf_prog_new_fd(struct bpf_prog
*prog
)
1508 ret
= security_bpf_prog(prog
);
1512 return anon_inode_getfd("bpf-prog", &bpf_prog_fops
, prog
,
1513 O_RDWR
| O_CLOEXEC
);
1516 static struct bpf_prog
*____bpf_prog_get(struct fd f
)
1519 return ERR_PTR(-EBADF
);
1520 if (f
.file
->f_op
!= &bpf_prog_fops
) {
1522 return ERR_PTR(-EINVAL
);
1525 return f
.file
->private_data
;
1528 void bpf_prog_add(struct bpf_prog
*prog
, int i
)
1530 atomic64_add(i
, &prog
->aux
->refcnt
);
1532 EXPORT_SYMBOL_GPL(bpf_prog_add
);
1534 void bpf_prog_sub(struct bpf_prog
*prog
, int i
)
1536 /* Only to be used for undoing previous bpf_prog_add() in some
1537 * error path. We still know that another entity in our call
1538 * path holds a reference to the program, thus atomic_sub() can
1539 * be safely used in such cases!
1541 WARN_ON(atomic64_sub_return(i
, &prog
->aux
->refcnt
) == 0);
1543 EXPORT_SYMBOL_GPL(bpf_prog_sub
);
1545 void bpf_prog_inc(struct bpf_prog
*prog
)
1547 atomic64_inc(&prog
->aux
->refcnt
);
1549 EXPORT_SYMBOL_GPL(bpf_prog_inc
);
1551 /* prog_idr_lock should have been held */
1552 struct bpf_prog
*bpf_prog_inc_not_zero(struct bpf_prog
*prog
)
1556 refold
= atomic64_fetch_add_unless(&prog
->aux
->refcnt
, 1, 0);
1559 return ERR_PTR(-ENOENT
);
1563 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero
);
1565 bool bpf_prog_get_ok(struct bpf_prog
*prog
,
1566 enum bpf_prog_type
*attach_type
, bool attach_drv
)
1568 /* not an attachment, just a refcount inc, always allow */
1572 if (prog
->type
!= *attach_type
)
1574 if (bpf_prog_is_dev_bound(prog
->aux
) && !attach_drv
)
1580 static struct bpf_prog
*__bpf_prog_get(u32 ufd
, enum bpf_prog_type
*attach_type
,
1583 struct fd f
= fdget(ufd
);
1584 struct bpf_prog
*prog
;
1586 prog
= ____bpf_prog_get(f
);
1589 if (!bpf_prog_get_ok(prog
, attach_type
, attach_drv
)) {
1590 prog
= ERR_PTR(-EINVAL
);
1600 struct bpf_prog
*bpf_prog_get(u32 ufd
)
1602 return __bpf_prog_get(ufd
, NULL
, false);
1605 struct bpf_prog
*bpf_prog_get_type_dev(u32 ufd
, enum bpf_prog_type type
,
1608 return __bpf_prog_get(ufd
, &type
, attach_drv
);
1610 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev
);
1612 /* Initially all BPF programs could be loaded w/o specifying
1613 * expected_attach_type. Later for some of them specifying expected_attach_type
1614 * at load time became required so that program could be validated properly.
1615 * Programs of types that are allowed to be loaded both w/ and w/o (for
1616 * backward compatibility) expected_attach_type, should have the default attach
1617 * type assigned to expected_attach_type for the latter case, so that it can be
1618 * validated later at attach time.
1620 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1621 * prog type requires it but has some attach types that have to be backward
1624 static void bpf_prog_load_fixup_attach_type(union bpf_attr
*attr
)
1626 switch (attr
->prog_type
) {
1627 case BPF_PROG_TYPE_CGROUP_SOCK
:
1628 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1629 * exist so checking for non-zero is the way to go here.
1631 if (!attr
->expected_attach_type
)
1632 attr
->expected_attach_type
=
1633 BPF_CGROUP_INET_SOCK_CREATE
;
1639 bpf_prog_load_check_attach(enum bpf_prog_type prog_type
,
1640 enum bpf_attach_type expected_attach_type
,
1641 u32 btf_id
, u32 prog_fd
)
1643 switch (prog_type
) {
1644 case BPF_PROG_TYPE_TRACING
:
1645 if (btf_id
> BTF_MAX_TYPE
)
1649 if (btf_id
|| prog_fd
)
1654 switch (prog_type
) {
1655 case BPF_PROG_TYPE_CGROUP_SOCK
:
1656 switch (expected_attach_type
) {
1657 case BPF_CGROUP_INET_SOCK_CREATE
:
1658 case BPF_CGROUP_INET4_POST_BIND
:
1659 case BPF_CGROUP_INET6_POST_BIND
:
1664 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR
:
1665 switch (expected_attach_type
) {
1666 case BPF_CGROUP_INET4_BIND
:
1667 case BPF_CGROUP_INET6_BIND
:
1668 case BPF_CGROUP_INET4_CONNECT
:
1669 case BPF_CGROUP_INET6_CONNECT
:
1670 case BPF_CGROUP_UDP4_SENDMSG
:
1671 case BPF_CGROUP_UDP6_SENDMSG
:
1672 case BPF_CGROUP_UDP4_RECVMSG
:
1673 case BPF_CGROUP_UDP6_RECVMSG
:
1678 case BPF_PROG_TYPE_CGROUP_SKB
:
1679 switch (expected_attach_type
) {
1680 case BPF_CGROUP_INET_INGRESS
:
1681 case BPF_CGROUP_INET_EGRESS
:
1686 case BPF_PROG_TYPE_CGROUP_SOCKOPT
:
1687 switch (expected_attach_type
) {
1688 case BPF_CGROUP_SETSOCKOPT
:
1689 case BPF_CGROUP_GETSOCKOPT
:
1699 /* last field in 'union bpf_attr' used by this command */
1700 #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
1702 static int bpf_prog_load(union bpf_attr
*attr
, union bpf_attr __user
*uattr
)
1704 enum bpf_prog_type type
= attr
->prog_type
;
1705 struct bpf_prog
*prog
;
1710 if (CHECK_ATTR(BPF_PROG_LOAD
))
1713 if (attr
->prog_flags
& ~(BPF_F_STRICT_ALIGNMENT
|
1714 BPF_F_ANY_ALIGNMENT
|
1715 BPF_F_TEST_STATE_FREQ
|
1716 BPF_F_TEST_RND_HI32
))
1719 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
) &&
1720 (attr
->prog_flags
& BPF_F_ANY_ALIGNMENT
) &&
1721 !capable(CAP_SYS_ADMIN
))
1724 /* copy eBPF program license from user space */
1725 if (strncpy_from_user(license
, u64_to_user_ptr(attr
->license
),
1726 sizeof(license
) - 1) < 0)
1728 license
[sizeof(license
) - 1] = 0;
1730 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1731 is_gpl
= license_is_gpl_compatible(license
);
1733 if (attr
->insn_cnt
== 0 ||
1734 attr
->insn_cnt
> (capable(CAP_SYS_ADMIN
) ? BPF_COMPLEXITY_LIMIT_INSNS
: BPF_MAXINSNS
))
1736 if (type
!= BPF_PROG_TYPE_SOCKET_FILTER
&&
1737 type
!= BPF_PROG_TYPE_CGROUP_SKB
&&
1738 !capable(CAP_SYS_ADMIN
))
1741 bpf_prog_load_fixup_attach_type(attr
);
1742 if (bpf_prog_load_check_attach(type
, attr
->expected_attach_type
,
1743 attr
->attach_btf_id
,
1744 attr
->attach_prog_fd
))
1747 /* plain bpf_prog allocation */
1748 prog
= bpf_prog_alloc(bpf_prog_size(attr
->insn_cnt
), GFP_USER
);
1752 prog
->expected_attach_type
= attr
->expected_attach_type
;
1753 prog
->aux
->attach_btf_id
= attr
->attach_btf_id
;
1754 if (attr
->attach_prog_fd
) {
1755 struct bpf_prog
*tgt_prog
;
1757 tgt_prog
= bpf_prog_get(attr
->attach_prog_fd
);
1758 if (IS_ERR(tgt_prog
)) {
1759 err
= PTR_ERR(tgt_prog
);
1760 goto free_prog_nouncharge
;
1762 prog
->aux
->linked_prog
= tgt_prog
;
1765 prog
->aux
->offload_requested
= !!attr
->prog_ifindex
;
1767 err
= security_bpf_prog_alloc(prog
->aux
);
1769 goto free_prog_nouncharge
;
1771 err
= bpf_prog_charge_memlock(prog
);
1775 prog
->len
= attr
->insn_cnt
;
1778 if (copy_from_user(prog
->insns
, u64_to_user_ptr(attr
->insns
),
1779 bpf_prog_insn_size(prog
)) != 0)
1782 prog
->orig_prog
= NULL
;
1785 atomic64_set(&prog
->aux
->refcnt
, 1);
1786 prog
->gpl_compatible
= is_gpl
? 1 : 0;
1788 if (bpf_prog_is_dev_bound(prog
->aux
)) {
1789 err
= bpf_prog_offload_init(prog
, attr
);
1794 /* find program type: socket_filter vs tracing_filter */
1795 err
= find_prog_type(type
, prog
);
1799 prog
->aux
->load_time
= ktime_get_boottime_ns();
1800 err
= bpf_obj_name_cpy(prog
->aux
->name
, attr
->prog_name
);
1804 /* run eBPF verifier */
1805 err
= bpf_check(&prog
, attr
, uattr
);
1807 goto free_used_maps
;
1809 prog
= bpf_prog_select_runtime(prog
, &err
);
1811 goto free_used_maps
;
1813 err
= bpf_prog_alloc_id(prog
);
1815 goto free_used_maps
;
1817 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
1818 * effectively publicly exposed. However, retrieving via
1819 * bpf_prog_get_fd_by_id() will take another reference,
1820 * therefore it cannot be gone underneath us.
1822 * Only for the time /after/ successful bpf_prog_new_fd()
1823 * and before returning to userspace, we might just hold
1824 * one reference and any parallel close on that fd could
1825 * rip everything out. Hence, below notifications must
1826 * happen before bpf_prog_new_fd().
1828 * Also, any failure handling from this point onwards must
1829 * be using bpf_prog_put() given the program is exposed.
1831 bpf_prog_kallsyms_add(prog
);
1832 perf_event_bpf_event(prog
, PERF_BPF_EVENT_PROG_LOAD
, 0);
1834 err
= bpf_prog_new_fd(prog
);
1840 /* In case we have subprogs, we need to wait for a grace
1841 * period before we can tear down JIT memory since symbols
1842 * are already exposed under kallsyms.
1844 __bpf_prog_put_noref(prog
, prog
->aux
->func_cnt
);
1847 bpf_prog_uncharge_memlock(prog
);
1849 security_bpf_prog_free(prog
->aux
);
1850 free_prog_nouncharge
:
1851 bpf_prog_free(prog
);
1855 #define BPF_OBJ_LAST_FIELD file_flags
1857 static int bpf_obj_pin(const union bpf_attr
*attr
)
1859 if (CHECK_ATTR(BPF_OBJ
) || attr
->file_flags
!= 0)
1862 return bpf_obj_pin_user(attr
->bpf_fd
, u64_to_user_ptr(attr
->pathname
));
1865 static int bpf_obj_get(const union bpf_attr
*attr
)
1867 if (CHECK_ATTR(BPF_OBJ
) || attr
->bpf_fd
!= 0 ||
1868 attr
->file_flags
& ~BPF_OBJ_FLAG_MASK
)
1871 return bpf_obj_get_user(u64_to_user_ptr(attr
->pathname
),
1875 static int bpf_tracing_prog_release(struct inode
*inode
, struct file
*filp
)
1877 struct bpf_prog
*prog
= filp
->private_data
;
1879 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog
));
1884 static const struct file_operations bpf_tracing_prog_fops
= {
1885 .release
= bpf_tracing_prog_release
,
1886 .read
= bpf_dummy_read
,
1887 .write
= bpf_dummy_write
,
1890 static int bpf_tracing_prog_attach(struct bpf_prog
*prog
)
1894 if (prog
->expected_attach_type
!= BPF_TRACE_FENTRY
&&
1895 prog
->expected_attach_type
!= BPF_TRACE_FEXIT
) {
1900 err
= bpf_trampoline_link_prog(prog
);
1904 tr_fd
= anon_inode_getfd("bpf-tracing-prog", &bpf_tracing_prog_fops
,
1907 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog
));
1918 struct bpf_raw_tracepoint
{
1919 struct bpf_raw_event_map
*btp
;
1920 struct bpf_prog
*prog
;
1923 static int bpf_raw_tracepoint_release(struct inode
*inode
, struct file
*filp
)
1925 struct bpf_raw_tracepoint
*raw_tp
= filp
->private_data
;
1928 bpf_probe_unregister(raw_tp
->btp
, raw_tp
->prog
);
1929 bpf_prog_put(raw_tp
->prog
);
1931 bpf_put_raw_tracepoint(raw_tp
->btp
);
1936 static const struct file_operations bpf_raw_tp_fops
= {
1937 .release
= bpf_raw_tracepoint_release
,
1938 .read
= bpf_dummy_read
,
1939 .write
= bpf_dummy_write
,
1942 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1944 static int bpf_raw_tracepoint_open(const union bpf_attr
*attr
)
1946 struct bpf_raw_tracepoint
*raw_tp
;
1947 struct bpf_raw_event_map
*btp
;
1948 struct bpf_prog
*prog
;
1949 const char *tp_name
;
1953 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN
))
1956 prog
= bpf_prog_get(attr
->raw_tracepoint
.prog_fd
);
1958 return PTR_ERR(prog
);
1960 if (prog
->type
!= BPF_PROG_TYPE_RAW_TRACEPOINT
&&
1961 prog
->type
!= BPF_PROG_TYPE_TRACING
&&
1962 prog
->type
!= BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE
) {
1967 if (prog
->type
== BPF_PROG_TYPE_TRACING
) {
1968 if (attr
->raw_tracepoint
.name
) {
1969 /* The attach point for this category of programs
1970 * should be specified via btf_id during program load.
1975 if (prog
->expected_attach_type
== BPF_TRACE_RAW_TP
)
1976 tp_name
= prog
->aux
->attach_func_name
;
1978 return bpf_tracing_prog_attach(prog
);
1980 if (strncpy_from_user(buf
,
1981 u64_to_user_ptr(attr
->raw_tracepoint
.name
),
1982 sizeof(buf
) - 1) < 0) {
1986 buf
[sizeof(buf
) - 1] = 0;
1990 btp
= bpf_get_raw_tracepoint(tp_name
);
1996 raw_tp
= kzalloc(sizeof(*raw_tp
), GFP_USER
);
2002 raw_tp
->prog
= prog
;
2004 err
= bpf_probe_register(raw_tp
->btp
, prog
);
2008 tp_fd
= anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops
, raw_tp
,
2011 bpf_probe_unregister(raw_tp
->btp
, prog
);
2020 bpf_put_raw_tracepoint(btp
);
2026 static int bpf_prog_attach_check_attach_type(const struct bpf_prog
*prog
,
2027 enum bpf_attach_type attach_type
)
2029 switch (prog
->type
) {
2030 case BPF_PROG_TYPE_CGROUP_SOCK
:
2031 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR
:
2032 case BPF_PROG_TYPE_CGROUP_SOCKOPT
:
2033 return attach_type
== prog
->expected_attach_type
? 0 : -EINVAL
;
2034 case BPF_PROG_TYPE_CGROUP_SKB
:
2035 return prog
->enforce_expected_attach_type
&&
2036 prog
->expected_attach_type
!= attach_type
?
2043 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
2045 #define BPF_F_ATTACH_MASK \
2046 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
2048 static int bpf_prog_attach(const union bpf_attr
*attr
)
2050 enum bpf_prog_type ptype
;
2051 struct bpf_prog
*prog
;
2054 if (!capable(CAP_NET_ADMIN
))
2057 if (CHECK_ATTR(BPF_PROG_ATTACH
))
2060 if (attr
->attach_flags
& ~BPF_F_ATTACH_MASK
)
2063 switch (attr
->attach_type
) {
2064 case BPF_CGROUP_INET_INGRESS
:
2065 case BPF_CGROUP_INET_EGRESS
:
2066 ptype
= BPF_PROG_TYPE_CGROUP_SKB
;
2068 case BPF_CGROUP_INET_SOCK_CREATE
:
2069 case BPF_CGROUP_INET4_POST_BIND
:
2070 case BPF_CGROUP_INET6_POST_BIND
:
2071 ptype
= BPF_PROG_TYPE_CGROUP_SOCK
;
2073 case BPF_CGROUP_INET4_BIND
:
2074 case BPF_CGROUP_INET6_BIND
:
2075 case BPF_CGROUP_INET4_CONNECT
:
2076 case BPF_CGROUP_INET6_CONNECT
:
2077 case BPF_CGROUP_UDP4_SENDMSG
:
2078 case BPF_CGROUP_UDP6_SENDMSG
:
2079 case BPF_CGROUP_UDP4_RECVMSG
:
2080 case BPF_CGROUP_UDP6_RECVMSG
:
2081 ptype
= BPF_PROG_TYPE_CGROUP_SOCK_ADDR
;
2083 case BPF_CGROUP_SOCK_OPS
:
2084 ptype
= BPF_PROG_TYPE_SOCK_OPS
;
2086 case BPF_CGROUP_DEVICE
:
2087 ptype
= BPF_PROG_TYPE_CGROUP_DEVICE
;
2089 case BPF_SK_MSG_VERDICT
:
2090 ptype
= BPF_PROG_TYPE_SK_MSG
;
2092 case BPF_SK_SKB_STREAM_PARSER
:
2093 case BPF_SK_SKB_STREAM_VERDICT
:
2094 ptype
= BPF_PROG_TYPE_SK_SKB
;
2096 case BPF_LIRC_MODE2
:
2097 ptype
= BPF_PROG_TYPE_LIRC_MODE2
;
2099 case BPF_FLOW_DISSECTOR
:
2100 ptype
= BPF_PROG_TYPE_FLOW_DISSECTOR
;
2102 case BPF_CGROUP_SYSCTL
:
2103 ptype
= BPF_PROG_TYPE_CGROUP_SYSCTL
;
2105 case BPF_CGROUP_GETSOCKOPT
:
2106 case BPF_CGROUP_SETSOCKOPT
:
2107 ptype
= BPF_PROG_TYPE_CGROUP_SOCKOPT
;
2113 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
, ptype
);
2115 return PTR_ERR(prog
);
2117 if (bpf_prog_attach_check_attach_type(prog
, attr
->attach_type
)) {
2123 case BPF_PROG_TYPE_SK_SKB
:
2124 case BPF_PROG_TYPE_SK_MSG
:
2125 ret
= sock_map_get_from_fd(attr
, prog
);
2127 case BPF_PROG_TYPE_LIRC_MODE2
:
2128 ret
= lirc_prog_attach(attr
, prog
);
2130 case BPF_PROG_TYPE_FLOW_DISSECTOR
:
2131 ret
= skb_flow_dissector_bpf_prog_attach(attr
, prog
);
2134 ret
= cgroup_bpf_prog_attach(attr
, ptype
, prog
);
2142 #define BPF_PROG_DETACH_LAST_FIELD attach_type
2144 static int bpf_prog_detach(const union bpf_attr
*attr
)
2146 enum bpf_prog_type ptype
;
2148 if (!capable(CAP_NET_ADMIN
))
2151 if (CHECK_ATTR(BPF_PROG_DETACH
))
2154 switch (attr
->attach_type
) {
2155 case BPF_CGROUP_INET_INGRESS
:
2156 case BPF_CGROUP_INET_EGRESS
:
2157 ptype
= BPF_PROG_TYPE_CGROUP_SKB
;
2159 case BPF_CGROUP_INET_SOCK_CREATE
:
2160 case BPF_CGROUP_INET4_POST_BIND
:
2161 case BPF_CGROUP_INET6_POST_BIND
:
2162 ptype
= BPF_PROG_TYPE_CGROUP_SOCK
;
2164 case BPF_CGROUP_INET4_BIND
:
2165 case BPF_CGROUP_INET6_BIND
:
2166 case BPF_CGROUP_INET4_CONNECT
:
2167 case BPF_CGROUP_INET6_CONNECT
:
2168 case BPF_CGROUP_UDP4_SENDMSG
:
2169 case BPF_CGROUP_UDP6_SENDMSG
:
2170 case BPF_CGROUP_UDP4_RECVMSG
:
2171 case BPF_CGROUP_UDP6_RECVMSG
:
2172 ptype
= BPF_PROG_TYPE_CGROUP_SOCK_ADDR
;
2174 case BPF_CGROUP_SOCK_OPS
:
2175 ptype
= BPF_PROG_TYPE_SOCK_OPS
;
2177 case BPF_CGROUP_DEVICE
:
2178 ptype
= BPF_PROG_TYPE_CGROUP_DEVICE
;
2180 case BPF_SK_MSG_VERDICT
:
2181 return sock_map_get_from_fd(attr
, NULL
);
2182 case BPF_SK_SKB_STREAM_PARSER
:
2183 case BPF_SK_SKB_STREAM_VERDICT
:
2184 return sock_map_get_from_fd(attr
, NULL
);
2185 case BPF_LIRC_MODE2
:
2186 return lirc_prog_detach(attr
);
2187 case BPF_FLOW_DISSECTOR
:
2188 return skb_flow_dissector_bpf_prog_detach(attr
);
2189 case BPF_CGROUP_SYSCTL
:
2190 ptype
= BPF_PROG_TYPE_CGROUP_SYSCTL
;
2192 case BPF_CGROUP_GETSOCKOPT
:
2193 case BPF_CGROUP_SETSOCKOPT
:
2194 ptype
= BPF_PROG_TYPE_CGROUP_SOCKOPT
;
2200 return cgroup_bpf_prog_detach(attr
, ptype
);
2203 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2205 static int bpf_prog_query(const union bpf_attr
*attr
,
2206 union bpf_attr __user
*uattr
)
2208 if (!capable(CAP_NET_ADMIN
))
2210 if (CHECK_ATTR(BPF_PROG_QUERY
))
2212 if (attr
->query
.query_flags
& ~BPF_F_QUERY_EFFECTIVE
)
2215 switch (attr
->query
.attach_type
) {
2216 case BPF_CGROUP_INET_INGRESS
:
2217 case BPF_CGROUP_INET_EGRESS
:
2218 case BPF_CGROUP_INET_SOCK_CREATE
:
2219 case BPF_CGROUP_INET4_BIND
:
2220 case BPF_CGROUP_INET6_BIND
:
2221 case BPF_CGROUP_INET4_POST_BIND
:
2222 case BPF_CGROUP_INET6_POST_BIND
:
2223 case BPF_CGROUP_INET4_CONNECT
:
2224 case BPF_CGROUP_INET6_CONNECT
:
2225 case BPF_CGROUP_UDP4_SENDMSG
:
2226 case BPF_CGROUP_UDP6_SENDMSG
:
2227 case BPF_CGROUP_UDP4_RECVMSG
:
2228 case BPF_CGROUP_UDP6_RECVMSG
:
2229 case BPF_CGROUP_SOCK_OPS
:
2230 case BPF_CGROUP_DEVICE
:
2231 case BPF_CGROUP_SYSCTL
:
2232 case BPF_CGROUP_GETSOCKOPT
:
2233 case BPF_CGROUP_SETSOCKOPT
:
2235 case BPF_LIRC_MODE2
:
2236 return lirc_prog_query(attr
, uattr
);
2237 case BPF_FLOW_DISSECTOR
:
2238 return skb_flow_dissector_prog_query(attr
, uattr
);
2243 return cgroup_bpf_prog_query(attr
, uattr
);
2246 #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
2248 static int bpf_prog_test_run(const union bpf_attr
*attr
,
2249 union bpf_attr __user
*uattr
)
2251 struct bpf_prog
*prog
;
2252 int ret
= -ENOTSUPP
;
2254 if (!capable(CAP_SYS_ADMIN
))
2256 if (CHECK_ATTR(BPF_PROG_TEST_RUN
))
2259 if ((attr
->test
.ctx_size_in
&& !attr
->test
.ctx_in
) ||
2260 (!attr
->test
.ctx_size_in
&& attr
->test
.ctx_in
))
2263 if ((attr
->test
.ctx_size_out
&& !attr
->test
.ctx_out
) ||
2264 (!attr
->test
.ctx_size_out
&& attr
->test
.ctx_out
))
2267 prog
= bpf_prog_get(attr
->test
.prog_fd
);
2269 return PTR_ERR(prog
);
2271 if (prog
->aux
->ops
->test_run
)
2272 ret
= prog
->aux
->ops
->test_run(prog
, attr
, uattr
);
2278 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2280 static int bpf_obj_get_next_id(const union bpf_attr
*attr
,
2281 union bpf_attr __user
*uattr
,
2285 u32 next_id
= attr
->start_id
;
2288 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID
) || next_id
>= INT_MAX
)
2291 if (!capable(CAP_SYS_ADMIN
))
2296 if (!idr_get_next(idr
, &next_id
))
2298 spin_unlock_bh(lock
);
2301 err
= put_user(next_id
, &uattr
->next_id
);
2306 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2308 static int bpf_prog_get_fd_by_id(const union bpf_attr
*attr
)
2310 struct bpf_prog
*prog
;
2311 u32 id
= attr
->prog_id
;
2314 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID
))
2317 if (!capable(CAP_SYS_ADMIN
))
2320 spin_lock_bh(&prog_idr_lock
);
2321 prog
= idr_find(&prog_idr
, id
);
2323 prog
= bpf_prog_inc_not_zero(prog
);
2325 prog
= ERR_PTR(-ENOENT
);
2326 spin_unlock_bh(&prog_idr_lock
);
2329 return PTR_ERR(prog
);
2331 fd
= bpf_prog_new_fd(prog
);
2338 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2340 static int bpf_map_get_fd_by_id(const union bpf_attr
*attr
)
2342 struct bpf_map
*map
;
2343 u32 id
= attr
->map_id
;
2347 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID
) ||
2348 attr
->open_flags
& ~BPF_OBJ_FLAG_MASK
)
2351 if (!capable(CAP_SYS_ADMIN
))
2354 f_flags
= bpf_get_file_flag(attr
->open_flags
);
2358 spin_lock_bh(&map_idr_lock
);
2359 map
= idr_find(&map_idr
, id
);
2361 map
= __bpf_map_inc_not_zero(map
, true);
2363 map
= ERR_PTR(-ENOENT
);
2364 spin_unlock_bh(&map_idr_lock
);
2367 return PTR_ERR(map
);
2369 fd
= bpf_map_new_fd(map
, f_flags
);
2371 bpf_map_put_with_uref(map
);
2376 static const struct bpf_map
*bpf_map_from_imm(const struct bpf_prog
*prog
,
2377 unsigned long addr
, u32
*off
,
2380 const struct bpf_map
*map
;
2383 for (i
= 0, *off
= 0; i
< prog
->aux
->used_map_cnt
; i
++) {
2384 map
= prog
->aux
->used_maps
[i
];
2385 if (map
== (void *)addr
) {
2386 *type
= BPF_PSEUDO_MAP_FD
;
2389 if (!map
->ops
->map_direct_value_meta
)
2391 if (!map
->ops
->map_direct_value_meta(map
, addr
, off
)) {
2392 *type
= BPF_PSEUDO_MAP_VALUE
;
2400 static struct bpf_insn
*bpf_insn_prepare_dump(const struct bpf_prog
*prog
)
2402 const struct bpf_map
*map
;
2403 struct bpf_insn
*insns
;
2408 insns
= kmemdup(prog
->insnsi
, bpf_prog_insn_size(prog
),
2413 for (i
= 0; i
< prog
->len
; i
++) {
2414 if (insns
[i
].code
== (BPF_JMP
| BPF_TAIL_CALL
)) {
2415 insns
[i
].code
= BPF_JMP
| BPF_CALL
;
2416 insns
[i
].imm
= BPF_FUNC_tail_call
;
2419 if (insns
[i
].code
== (BPF_JMP
| BPF_CALL
) ||
2420 insns
[i
].code
== (BPF_JMP
| BPF_CALL_ARGS
)) {
2421 if (insns
[i
].code
== (BPF_JMP
| BPF_CALL_ARGS
))
2422 insns
[i
].code
= BPF_JMP
| BPF_CALL
;
2423 if (!bpf_dump_raw_ok())
2428 if (insns
[i
].code
!= (BPF_LD
| BPF_IMM
| BPF_DW
))
2431 imm
= ((u64
)insns
[i
+ 1].imm
<< 32) | (u32
)insns
[i
].imm
;
2432 map
= bpf_map_from_imm(prog
, imm
, &off
, &type
);
2434 insns
[i
].src_reg
= type
;
2435 insns
[i
].imm
= map
->id
;
2436 insns
[i
+ 1].imm
= off
;
2444 static int set_info_rec_size(struct bpf_prog_info
*info
)
2447 * Ensure info.*_rec_size is the same as kernel expected size
2451 * Only allow zero *_rec_size if both _rec_size and _cnt are
2452 * zero. In this case, the kernel will set the expected
2453 * _rec_size back to the info.
2456 if ((info
->nr_func_info
|| info
->func_info_rec_size
) &&
2457 info
->func_info_rec_size
!= sizeof(struct bpf_func_info
))
2460 if ((info
->nr_line_info
|| info
->line_info_rec_size
) &&
2461 info
->line_info_rec_size
!= sizeof(struct bpf_line_info
))
2464 if ((info
->nr_jited_line_info
|| info
->jited_line_info_rec_size
) &&
2465 info
->jited_line_info_rec_size
!= sizeof(__u64
))
2468 info
->func_info_rec_size
= sizeof(struct bpf_func_info
);
2469 info
->line_info_rec_size
= sizeof(struct bpf_line_info
);
2470 info
->jited_line_info_rec_size
= sizeof(__u64
);
2475 static int bpf_prog_get_info_by_fd(struct bpf_prog
*prog
,
2476 const union bpf_attr
*attr
,
2477 union bpf_attr __user
*uattr
)
2479 struct bpf_prog_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
2480 struct bpf_prog_info info
= {};
2481 u32 info_len
= attr
->info
.info_len
;
2482 struct bpf_prog_stats stats
;
2483 char __user
*uinsns
;
2487 err
= bpf_check_uarg_tail_zero(uinfo
, sizeof(info
), info_len
);
2490 info_len
= min_t(u32
, sizeof(info
), info_len
);
2492 if (copy_from_user(&info
, uinfo
, info_len
))
2495 info
.type
= prog
->type
;
2496 info
.id
= prog
->aux
->id
;
2497 info
.load_time
= prog
->aux
->load_time
;
2498 info
.created_by_uid
= from_kuid_munged(current_user_ns(),
2499 prog
->aux
->user
->uid
);
2500 info
.gpl_compatible
= prog
->gpl_compatible
;
2502 memcpy(info
.tag
, prog
->tag
, sizeof(prog
->tag
));
2503 memcpy(info
.name
, prog
->aux
->name
, sizeof(prog
->aux
->name
));
2505 ulen
= info
.nr_map_ids
;
2506 info
.nr_map_ids
= prog
->aux
->used_map_cnt
;
2507 ulen
= min_t(u32
, info
.nr_map_ids
, ulen
);
2509 u32 __user
*user_map_ids
= u64_to_user_ptr(info
.map_ids
);
2512 for (i
= 0; i
< ulen
; i
++)
2513 if (put_user(prog
->aux
->used_maps
[i
]->id
,
2518 err
= set_info_rec_size(&info
);
2522 bpf_prog_get_stats(prog
, &stats
);
2523 info
.run_time_ns
= stats
.nsecs
;
2524 info
.run_cnt
= stats
.cnt
;
2526 if (!capable(CAP_SYS_ADMIN
)) {
2527 info
.jited_prog_len
= 0;
2528 info
.xlated_prog_len
= 0;
2529 info
.nr_jited_ksyms
= 0;
2530 info
.nr_jited_func_lens
= 0;
2531 info
.nr_func_info
= 0;
2532 info
.nr_line_info
= 0;
2533 info
.nr_jited_line_info
= 0;
2537 ulen
= info
.xlated_prog_len
;
2538 info
.xlated_prog_len
= bpf_prog_insn_size(prog
);
2539 if (info
.xlated_prog_len
&& ulen
) {
2540 struct bpf_insn
*insns_sanitized
;
2543 if (prog
->blinded
&& !bpf_dump_raw_ok()) {
2544 info
.xlated_prog_insns
= 0;
2547 insns_sanitized
= bpf_insn_prepare_dump(prog
);
2548 if (!insns_sanitized
)
2550 uinsns
= u64_to_user_ptr(info
.xlated_prog_insns
);
2551 ulen
= min_t(u32
, info
.xlated_prog_len
, ulen
);
2552 fault
= copy_to_user(uinsns
, insns_sanitized
, ulen
);
2553 kfree(insns_sanitized
);
2558 if (bpf_prog_is_dev_bound(prog
->aux
)) {
2559 err
= bpf_prog_offload_info_fill(&info
, prog
);
2565 /* NOTE: the following code is supposed to be skipped for offload.
2566 * bpf_prog_offload_info_fill() is the place to fill similar fields
2569 ulen
= info
.jited_prog_len
;
2570 if (prog
->aux
->func_cnt
) {
2573 info
.jited_prog_len
= 0;
2574 for (i
= 0; i
< prog
->aux
->func_cnt
; i
++)
2575 info
.jited_prog_len
+= prog
->aux
->func
[i
]->jited_len
;
2577 info
.jited_prog_len
= prog
->jited_len
;
2580 if (info
.jited_prog_len
&& ulen
) {
2581 if (bpf_dump_raw_ok()) {
2582 uinsns
= u64_to_user_ptr(info
.jited_prog_insns
);
2583 ulen
= min_t(u32
, info
.jited_prog_len
, ulen
);
2585 /* for multi-function programs, copy the JITed
2586 * instructions for all the functions
2588 if (prog
->aux
->func_cnt
) {
2593 for (i
= 0; i
< prog
->aux
->func_cnt
; i
++) {
2594 len
= prog
->aux
->func
[i
]->jited_len
;
2595 len
= min_t(u32
, len
, free
);
2596 img
= (u8
*) prog
->aux
->func
[i
]->bpf_func
;
2597 if (copy_to_user(uinsns
, img
, len
))
2605 if (copy_to_user(uinsns
, prog
->bpf_func
, ulen
))
2609 info
.jited_prog_insns
= 0;
2613 ulen
= info
.nr_jited_ksyms
;
2614 info
.nr_jited_ksyms
= prog
->aux
->func_cnt
? : 1;
2616 if (bpf_dump_raw_ok()) {
2617 unsigned long ksym_addr
;
2618 u64 __user
*user_ksyms
;
2621 /* copy the address of the kernel symbol
2622 * corresponding to each function
2624 ulen
= min_t(u32
, info
.nr_jited_ksyms
, ulen
);
2625 user_ksyms
= u64_to_user_ptr(info
.jited_ksyms
);
2626 if (prog
->aux
->func_cnt
) {
2627 for (i
= 0; i
< ulen
; i
++) {
2628 ksym_addr
= (unsigned long)
2629 prog
->aux
->func
[i
]->bpf_func
;
2630 if (put_user((u64
) ksym_addr
,
2635 ksym_addr
= (unsigned long) prog
->bpf_func
;
2636 if (put_user((u64
) ksym_addr
, &user_ksyms
[0]))
2640 info
.jited_ksyms
= 0;
2644 ulen
= info
.nr_jited_func_lens
;
2645 info
.nr_jited_func_lens
= prog
->aux
->func_cnt
? : 1;
2647 if (bpf_dump_raw_ok()) {
2648 u32 __user
*user_lens
;
2651 /* copy the JITed image lengths for each function */
2652 ulen
= min_t(u32
, info
.nr_jited_func_lens
, ulen
);
2653 user_lens
= u64_to_user_ptr(info
.jited_func_lens
);
2654 if (prog
->aux
->func_cnt
) {
2655 for (i
= 0; i
< ulen
; i
++) {
2657 prog
->aux
->func
[i
]->jited_len
;
2658 if (put_user(func_len
, &user_lens
[i
]))
2662 func_len
= prog
->jited_len
;
2663 if (put_user(func_len
, &user_lens
[0]))
2667 info
.jited_func_lens
= 0;
2672 info
.btf_id
= btf_id(prog
->aux
->btf
);
2674 ulen
= info
.nr_func_info
;
2675 info
.nr_func_info
= prog
->aux
->func_info_cnt
;
2676 if (info
.nr_func_info
&& ulen
) {
2677 char __user
*user_finfo
;
2679 user_finfo
= u64_to_user_ptr(info
.func_info
);
2680 ulen
= min_t(u32
, info
.nr_func_info
, ulen
);
2681 if (copy_to_user(user_finfo
, prog
->aux
->func_info
,
2682 info
.func_info_rec_size
* ulen
))
2686 ulen
= info
.nr_line_info
;
2687 info
.nr_line_info
= prog
->aux
->nr_linfo
;
2688 if (info
.nr_line_info
&& ulen
) {
2689 __u8 __user
*user_linfo
;
2691 user_linfo
= u64_to_user_ptr(info
.line_info
);
2692 ulen
= min_t(u32
, info
.nr_line_info
, ulen
);
2693 if (copy_to_user(user_linfo
, prog
->aux
->linfo
,
2694 info
.line_info_rec_size
* ulen
))
2698 ulen
= info
.nr_jited_line_info
;
2699 if (prog
->aux
->jited_linfo
)
2700 info
.nr_jited_line_info
= prog
->aux
->nr_linfo
;
2702 info
.nr_jited_line_info
= 0;
2703 if (info
.nr_jited_line_info
&& ulen
) {
2704 if (bpf_dump_raw_ok()) {
2705 __u64 __user
*user_linfo
;
2708 user_linfo
= u64_to_user_ptr(info
.jited_line_info
);
2709 ulen
= min_t(u32
, info
.nr_jited_line_info
, ulen
);
2710 for (i
= 0; i
< ulen
; i
++) {
2711 if (put_user((__u64
)(long)prog
->aux
->jited_linfo
[i
],
2716 info
.jited_line_info
= 0;
2720 ulen
= info
.nr_prog_tags
;
2721 info
.nr_prog_tags
= prog
->aux
->func_cnt
? : 1;
2723 __u8
__user (*user_prog_tags
)[BPF_TAG_SIZE
];
2726 user_prog_tags
= u64_to_user_ptr(info
.prog_tags
);
2727 ulen
= min_t(u32
, info
.nr_prog_tags
, ulen
);
2728 if (prog
->aux
->func_cnt
) {
2729 for (i
= 0; i
< ulen
; i
++) {
2730 if (copy_to_user(user_prog_tags
[i
],
2731 prog
->aux
->func
[i
]->tag
,
2736 if (copy_to_user(user_prog_tags
[0],
2737 prog
->tag
, BPF_TAG_SIZE
))
2743 if (copy_to_user(uinfo
, &info
, info_len
) ||
2744 put_user(info_len
, &uattr
->info
.info_len
))
2750 static int bpf_map_get_info_by_fd(struct bpf_map
*map
,
2751 const union bpf_attr
*attr
,
2752 union bpf_attr __user
*uattr
)
2754 struct bpf_map_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
2755 struct bpf_map_info info
= {};
2756 u32 info_len
= attr
->info
.info_len
;
2759 err
= bpf_check_uarg_tail_zero(uinfo
, sizeof(info
), info_len
);
2762 info_len
= min_t(u32
, sizeof(info
), info_len
);
2764 info
.type
= map
->map_type
;
2766 info
.key_size
= map
->key_size
;
2767 info
.value_size
= map
->value_size
;
2768 info
.max_entries
= map
->max_entries
;
2769 info
.map_flags
= map
->map_flags
;
2770 memcpy(info
.name
, map
->name
, sizeof(map
->name
));
2773 info
.btf_id
= btf_id(map
->btf
);
2774 info
.btf_key_type_id
= map
->btf_key_type_id
;
2775 info
.btf_value_type_id
= map
->btf_value_type_id
;
2778 if (bpf_map_is_dev_bound(map
)) {
2779 err
= bpf_map_offload_info_fill(&info
, map
);
2784 if (copy_to_user(uinfo
, &info
, info_len
) ||
2785 put_user(info_len
, &uattr
->info
.info_len
))
2791 static int bpf_btf_get_info_by_fd(struct btf
*btf
,
2792 const union bpf_attr
*attr
,
2793 union bpf_attr __user
*uattr
)
2795 struct bpf_btf_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
2796 u32 info_len
= attr
->info
.info_len
;
2799 err
= bpf_check_uarg_tail_zero(uinfo
, sizeof(*uinfo
), info_len
);
2803 return btf_get_info_by_fd(btf
, attr
, uattr
);
2806 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2808 static int bpf_obj_get_info_by_fd(const union bpf_attr
*attr
,
2809 union bpf_attr __user
*uattr
)
2811 int ufd
= attr
->info
.bpf_fd
;
2815 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD
))
2822 if (f
.file
->f_op
== &bpf_prog_fops
)
2823 err
= bpf_prog_get_info_by_fd(f
.file
->private_data
, attr
,
2825 else if (f
.file
->f_op
== &bpf_map_fops
)
2826 err
= bpf_map_get_info_by_fd(f
.file
->private_data
, attr
,
2828 else if (f
.file
->f_op
== &btf_fops
)
2829 err
= bpf_btf_get_info_by_fd(f
.file
->private_data
, attr
, uattr
);
2837 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2839 static int bpf_btf_load(const union bpf_attr
*attr
)
2841 if (CHECK_ATTR(BPF_BTF_LOAD
))
2844 if (!capable(CAP_SYS_ADMIN
))
2847 return btf_new_fd(attr
);
2850 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2852 static int bpf_btf_get_fd_by_id(const union bpf_attr
*attr
)
2854 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID
))
2857 if (!capable(CAP_SYS_ADMIN
))
2860 return btf_get_fd_by_id(attr
->btf_id
);
2863 static int bpf_task_fd_query_copy(const union bpf_attr
*attr
,
2864 union bpf_attr __user
*uattr
,
2865 u32 prog_id
, u32 fd_type
,
2866 const char *buf
, u64 probe_offset
,
2869 char __user
*ubuf
= u64_to_user_ptr(attr
->task_fd_query
.buf
);
2870 u32 len
= buf
? strlen(buf
) : 0, input_len
;
2873 if (put_user(len
, &uattr
->task_fd_query
.buf_len
))
2875 input_len
= attr
->task_fd_query
.buf_len
;
2876 if (input_len
&& ubuf
) {
2878 /* nothing to copy, just make ubuf NULL terminated */
2881 if (put_user(zero
, ubuf
))
2883 } else if (input_len
>= len
+ 1) {
2884 /* ubuf can hold the string with NULL terminator */
2885 if (copy_to_user(ubuf
, buf
, len
+ 1))
2888 /* ubuf cannot hold the string with NULL terminator,
2889 * do a partial copy with NULL terminator.
2894 if (copy_to_user(ubuf
, buf
, input_len
- 1))
2896 if (put_user(zero
, ubuf
+ input_len
- 1))
2901 if (put_user(prog_id
, &uattr
->task_fd_query
.prog_id
) ||
2902 put_user(fd_type
, &uattr
->task_fd_query
.fd_type
) ||
2903 put_user(probe_offset
, &uattr
->task_fd_query
.probe_offset
) ||
2904 put_user(probe_addr
, &uattr
->task_fd_query
.probe_addr
))
2910 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2912 static int bpf_task_fd_query(const union bpf_attr
*attr
,
2913 union bpf_attr __user
*uattr
)
2915 pid_t pid
= attr
->task_fd_query
.pid
;
2916 u32 fd
= attr
->task_fd_query
.fd
;
2917 const struct perf_event
*event
;
2918 struct files_struct
*files
;
2919 struct task_struct
*task
;
2923 if (CHECK_ATTR(BPF_TASK_FD_QUERY
))
2926 if (!capable(CAP_SYS_ADMIN
))
2929 if (attr
->task_fd_query
.flags
!= 0)
2932 task
= get_pid_task(find_vpid(pid
), PIDTYPE_PID
);
2936 files
= get_files_struct(task
);
2937 put_task_struct(task
);
2942 spin_lock(&files
->file_lock
);
2943 file
= fcheck_files(files
, fd
);
2948 spin_unlock(&files
->file_lock
);
2949 put_files_struct(files
);
2954 if (file
->f_op
== &bpf_raw_tp_fops
) {
2955 struct bpf_raw_tracepoint
*raw_tp
= file
->private_data
;
2956 struct bpf_raw_event_map
*btp
= raw_tp
->btp
;
2958 err
= bpf_task_fd_query_copy(attr
, uattr
,
2959 raw_tp
->prog
->aux
->id
,
2960 BPF_FD_TYPE_RAW_TRACEPOINT
,
2961 btp
->tp
->name
, 0, 0);
2965 event
= perf_get_event(file
);
2966 if (!IS_ERR(event
)) {
2967 u64 probe_offset
, probe_addr
;
2968 u32 prog_id
, fd_type
;
2971 err
= bpf_get_perf_event_info(event
, &prog_id
, &fd_type
,
2972 &buf
, &probe_offset
,
2975 err
= bpf_task_fd_query_copy(attr
, uattr
, prog_id
,
2989 SYSCALL_DEFINE3(bpf
, int, cmd
, union bpf_attr __user
*, uattr
, unsigned int, size
)
2991 union bpf_attr attr
= {};
2994 if (sysctl_unprivileged_bpf_disabled
&& !capable(CAP_SYS_ADMIN
))
2997 err
= bpf_check_uarg_tail_zero(uattr
, sizeof(attr
), size
);
3000 size
= min_t(u32
, size
, sizeof(attr
));
3002 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
3003 if (copy_from_user(&attr
, uattr
, size
) != 0)
3006 err
= security_bpf(cmd
, &attr
, size
);
3011 case BPF_MAP_CREATE
:
3012 err
= map_create(&attr
);
3014 case BPF_MAP_LOOKUP_ELEM
:
3015 err
= map_lookup_elem(&attr
);
3017 case BPF_MAP_UPDATE_ELEM
:
3018 err
= map_update_elem(&attr
);
3020 case BPF_MAP_DELETE_ELEM
:
3021 err
= map_delete_elem(&attr
);
3023 case BPF_MAP_GET_NEXT_KEY
:
3024 err
= map_get_next_key(&attr
);
3026 case BPF_MAP_FREEZE
:
3027 err
= map_freeze(&attr
);
3030 err
= bpf_prog_load(&attr
, uattr
);
3033 err
= bpf_obj_pin(&attr
);
3036 err
= bpf_obj_get(&attr
);
3038 case BPF_PROG_ATTACH
:
3039 err
= bpf_prog_attach(&attr
);
3041 case BPF_PROG_DETACH
:
3042 err
= bpf_prog_detach(&attr
);
3044 case BPF_PROG_QUERY
:
3045 err
= bpf_prog_query(&attr
, uattr
);
3047 case BPF_PROG_TEST_RUN
:
3048 err
= bpf_prog_test_run(&attr
, uattr
);
3050 case BPF_PROG_GET_NEXT_ID
:
3051 err
= bpf_obj_get_next_id(&attr
, uattr
,
3052 &prog_idr
, &prog_idr_lock
);
3054 case BPF_MAP_GET_NEXT_ID
:
3055 err
= bpf_obj_get_next_id(&attr
, uattr
,
3056 &map_idr
, &map_idr_lock
);
3058 case BPF_BTF_GET_NEXT_ID
:
3059 err
= bpf_obj_get_next_id(&attr
, uattr
,
3060 &btf_idr
, &btf_idr_lock
);
3062 case BPF_PROG_GET_FD_BY_ID
:
3063 err
= bpf_prog_get_fd_by_id(&attr
);
3065 case BPF_MAP_GET_FD_BY_ID
:
3066 err
= bpf_map_get_fd_by_id(&attr
);
3068 case BPF_OBJ_GET_INFO_BY_FD
:
3069 err
= bpf_obj_get_info_by_fd(&attr
, uattr
);
3071 case BPF_RAW_TRACEPOINT_OPEN
:
3072 err
= bpf_raw_tracepoint_open(&attr
);
3075 err
= bpf_btf_load(&attr
);
3077 case BPF_BTF_GET_FD_BY_ID
:
3078 err
= bpf_btf_get_fd_by_id(&attr
);
3080 case BPF_TASK_FD_QUERY
:
3081 err
= bpf_task_fd_query(&attr
, uattr
);
3083 case BPF_MAP_LOOKUP_AND_DELETE_ELEM
:
3084 err
= map_lookup_and_delete_elem(&attr
);