1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 #include <linux/bpf.h>
13 #include <linux/bpf_trace.h>
14 #include <linux/bpf_lirc.h>
15 #include <linux/btf.h>
16 #include <linux/syscalls.h>
17 #include <linux/slab.h>
18 #include <linux/sched/signal.h>
19 #include <linux/vmalloc.h>
20 #include <linux/mmzone.h>
21 #include <linux/anon_inodes.h>
22 #include <linux/fdtable.h>
23 #include <linux/file.h>
25 #include <linux/license.h>
26 #include <linux/filter.h>
27 #include <linux/version.h>
28 #include <linux/kernel.h>
29 #include <linux/idr.h>
30 #include <linux/cred.h>
31 #include <linux/timekeeping.h>
32 #include <linux/ctype.h>
33 #include <linux/nospec.h>
35 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
36 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
38 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
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_HASH(map))
42 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
44 DEFINE_PER_CPU(int, bpf_prog_active
);
45 static DEFINE_IDR(prog_idr
);
46 static DEFINE_SPINLOCK(prog_idr_lock
);
47 static DEFINE_IDR(map_idr
);
48 static DEFINE_SPINLOCK(map_idr_lock
);
50 int sysctl_unprivileged_bpf_disabled __read_mostly
;
52 static const struct bpf_map_ops
* const bpf_map_types
[] = {
53 #define BPF_PROG_TYPE(_id, _ops)
54 #define BPF_MAP_TYPE(_id, _ops) \
56 #include <linux/bpf_types.h>
62 * If we're handed a bigger struct than we know of, ensure all the unknown bits
63 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
64 * we don't know about yet.
66 * There is a ToCToU between this function call and the following
67 * copy_from_user() call. However, this is not a concern since this function is
68 * meant to be a future-proofing of bits.
70 int bpf_check_uarg_tail_zero(void __user
*uaddr
,
74 unsigned char __user
*addr
;
75 unsigned char __user
*end
;
79 if (unlikely(actual_size
> PAGE_SIZE
)) /* silly large */
82 if (unlikely(!access_ok(uaddr
, actual_size
)))
85 if (actual_size
<= expected_size
)
88 addr
= uaddr
+ expected_size
;
89 end
= uaddr
+ actual_size
;
91 for (; addr
< end
; addr
++) {
92 err
= get_user(val
, addr
);
102 const struct bpf_map_ops bpf_map_offload_ops
= {
103 .map_alloc
= bpf_map_offload_map_alloc
,
104 .map_free
= bpf_map_offload_map_free
,
105 .map_check_btf
= map_check_no_btf
,
108 static struct bpf_map
*find_and_alloc_map(union bpf_attr
*attr
)
110 const struct bpf_map_ops
*ops
;
111 u32 type
= attr
->map_type
;
115 if (type
>= ARRAY_SIZE(bpf_map_types
))
116 return ERR_PTR(-EINVAL
);
117 type
= array_index_nospec(type
, ARRAY_SIZE(bpf_map_types
));
118 ops
= bpf_map_types
[type
];
120 return ERR_PTR(-EINVAL
);
122 if (ops
->map_alloc_check
) {
123 err
= ops
->map_alloc_check(attr
);
127 if (attr
->map_ifindex
)
128 ops
= &bpf_map_offload_ops
;
129 map
= ops
->map_alloc(attr
);
133 map
->map_type
= type
;
137 void *bpf_map_area_alloc(size_t size
, int numa_node
)
139 /* We really just want to fail instead of triggering OOM killer
140 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
141 * which is used for lower order allocation requests.
143 * It has been observed that higher order allocation requests done by
144 * vmalloc with __GFP_NORETRY being set might fail due to not trying
145 * to reclaim memory from the page cache, thus we set
146 * __GFP_RETRY_MAYFAIL to avoid such situations.
149 const gfp_t flags
= __GFP_NOWARN
| __GFP_ZERO
;
152 if (size
<= (PAGE_SIZE
<< PAGE_ALLOC_COSTLY_ORDER
)) {
153 area
= kmalloc_node(size
, GFP_USER
| __GFP_NORETRY
| flags
,
159 return __vmalloc_node_flags_caller(size
, numa_node
,
160 GFP_KERNEL
| __GFP_RETRY_MAYFAIL
|
161 flags
, __builtin_return_address(0));
164 void bpf_map_area_free(void *area
)
169 void bpf_map_init_from_attr(struct bpf_map
*map
, union bpf_attr
*attr
)
171 map
->map_type
= attr
->map_type
;
172 map
->key_size
= attr
->key_size
;
173 map
->value_size
= attr
->value_size
;
174 map
->max_entries
= attr
->max_entries
;
175 map
->map_flags
= attr
->map_flags
;
176 map
->numa_node
= bpf_map_attr_numa_node(attr
);
179 int bpf_map_precharge_memlock(u32 pages
)
181 struct user_struct
*user
= get_current_user();
182 unsigned long memlock_limit
, cur
;
184 memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
185 cur
= atomic_long_read(&user
->locked_vm
);
187 if (cur
+ pages
> memlock_limit
)
192 static int bpf_charge_memlock(struct user_struct
*user
, u32 pages
)
194 unsigned long memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
196 if (atomic_long_add_return(pages
, &user
->locked_vm
) > memlock_limit
) {
197 atomic_long_sub(pages
, &user
->locked_vm
);
203 static void bpf_uncharge_memlock(struct user_struct
*user
, u32 pages
)
205 atomic_long_sub(pages
, &user
->locked_vm
);
208 static int bpf_map_init_memlock(struct bpf_map
*map
)
210 struct user_struct
*user
= get_current_user();
213 ret
= bpf_charge_memlock(user
, map
->pages
);
222 static void bpf_map_release_memlock(struct bpf_map
*map
)
224 struct user_struct
*user
= map
->user
;
225 bpf_uncharge_memlock(user
, map
->pages
);
229 int bpf_map_charge_memlock(struct bpf_map
*map
, u32 pages
)
233 ret
= bpf_charge_memlock(map
->user
, pages
);
240 void bpf_map_uncharge_memlock(struct bpf_map
*map
, u32 pages
)
242 bpf_uncharge_memlock(map
->user
, pages
);
246 static int bpf_map_alloc_id(struct bpf_map
*map
)
250 idr_preload(GFP_KERNEL
);
251 spin_lock_bh(&map_idr_lock
);
252 id
= idr_alloc_cyclic(&map_idr
, map
, 1, INT_MAX
, GFP_ATOMIC
);
255 spin_unlock_bh(&map_idr_lock
);
258 if (WARN_ON_ONCE(!id
))
261 return id
> 0 ? 0 : id
;
264 void bpf_map_free_id(struct bpf_map
*map
, bool do_idr_lock
)
268 /* Offloaded maps are removed from the IDR store when their device
269 * disappears - even if someone holds an fd to them they are unusable,
270 * the memory is gone, all ops will fail; they are simply waiting for
271 * refcnt to drop to be freed.
277 spin_lock_irqsave(&map_idr_lock
, flags
);
279 __acquire(&map_idr_lock
);
281 idr_remove(&map_idr
, map
->id
);
285 spin_unlock_irqrestore(&map_idr_lock
, flags
);
287 __release(&map_idr_lock
);
290 /* called from workqueue */
291 static void bpf_map_free_deferred(struct work_struct
*work
)
293 struct bpf_map
*map
= container_of(work
, struct bpf_map
, work
);
295 bpf_map_release_memlock(map
);
296 security_bpf_map_free(map
);
297 /* implementation dependent freeing */
298 map
->ops
->map_free(map
);
301 static void bpf_map_put_uref(struct bpf_map
*map
)
303 if (atomic_dec_and_test(&map
->usercnt
)) {
304 if (map
->ops
->map_release_uref
)
305 map
->ops
->map_release_uref(map
);
309 /* decrement map refcnt and schedule it for freeing via workqueue
310 * (unrelying map implementation ops->map_free() might sleep)
312 static void __bpf_map_put(struct bpf_map
*map
, bool do_idr_lock
)
314 if (atomic_dec_and_test(&map
->refcnt
)) {
315 /* bpf_map_free_id() must be called first */
316 bpf_map_free_id(map
, do_idr_lock
);
318 INIT_WORK(&map
->work
, bpf_map_free_deferred
);
319 schedule_work(&map
->work
);
323 void bpf_map_put(struct bpf_map
*map
)
325 __bpf_map_put(map
, true);
327 EXPORT_SYMBOL_GPL(bpf_map_put
);
329 void bpf_map_put_with_uref(struct bpf_map
*map
)
331 bpf_map_put_uref(map
);
335 static int bpf_map_release(struct inode
*inode
, struct file
*filp
)
337 struct bpf_map
*map
= filp
->private_data
;
339 if (map
->ops
->map_release
)
340 map
->ops
->map_release(map
, filp
);
342 bpf_map_put_with_uref(map
);
346 #ifdef CONFIG_PROC_FS
347 static void bpf_map_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
349 const struct bpf_map
*map
= filp
->private_data
;
350 const struct bpf_array
*array
;
351 u32 owner_prog_type
= 0;
354 if (map
->map_type
== BPF_MAP_TYPE_PROG_ARRAY
) {
355 array
= container_of(map
, struct bpf_array
, map
);
356 owner_prog_type
= array
->owner_prog_type
;
357 owner_jited
= array
->owner_jited
;
373 map
->pages
* 1ULL << PAGE_SHIFT
,
376 if (owner_prog_type
) {
377 seq_printf(m
, "owner_prog_type:\t%u\n",
379 seq_printf(m
, "owner_jited:\t%u\n",
385 static ssize_t
bpf_dummy_read(struct file
*filp
, char __user
*buf
, size_t siz
,
388 /* We need this handler such that alloc_file() enables
389 * f_mode with FMODE_CAN_READ.
394 static ssize_t
bpf_dummy_write(struct file
*filp
, const char __user
*buf
,
395 size_t siz
, loff_t
*ppos
)
397 /* We need this handler such that alloc_file() enables
398 * f_mode with FMODE_CAN_WRITE.
403 const struct file_operations bpf_map_fops
= {
404 #ifdef CONFIG_PROC_FS
405 .show_fdinfo
= bpf_map_show_fdinfo
,
407 .release
= bpf_map_release
,
408 .read
= bpf_dummy_read
,
409 .write
= bpf_dummy_write
,
412 int bpf_map_new_fd(struct bpf_map
*map
, int flags
)
416 ret
= security_bpf_map(map
, OPEN_FMODE(flags
));
420 return anon_inode_getfd("bpf-map", &bpf_map_fops
, map
,
424 int bpf_get_file_flag(int flags
)
426 if ((flags
& BPF_F_RDONLY
) && (flags
& BPF_F_WRONLY
))
428 if (flags
& BPF_F_RDONLY
)
430 if (flags
& BPF_F_WRONLY
)
435 /* helper macro to check that unused fields 'union bpf_attr' are zero */
436 #define CHECK_ATTR(CMD) \
437 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
438 sizeof(attr->CMD##_LAST_FIELD), 0, \
440 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
441 sizeof(attr->CMD##_LAST_FIELD)) != NULL
443 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
444 * Return 0 on success and < 0 on error.
446 static int bpf_obj_name_cpy(char *dst
, const char *src
)
448 const char *end
= src
+ BPF_OBJ_NAME_LEN
;
450 memset(dst
, 0, BPF_OBJ_NAME_LEN
);
452 /* Copy all isalnum() and '_' char */
453 while (src
< end
&& *src
) {
454 if (!isalnum(*src
) && *src
!= '_')
459 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
466 int map_check_no_btf(const struct bpf_map
*map
,
467 const struct btf
*btf
,
468 const struct btf_type
*key_type
,
469 const struct btf_type
*value_type
)
474 static int map_check_btf(struct bpf_map
*map
, const struct btf
*btf
,
475 u32 btf_key_id
, u32 btf_value_id
)
477 const struct btf_type
*key_type
, *value_type
;
478 u32 key_size
, value_size
;
481 key_type
= btf_type_id_size(btf
, &btf_key_id
, &key_size
);
482 if (!key_type
|| key_size
!= map
->key_size
)
485 value_type
= btf_type_id_size(btf
, &btf_value_id
, &value_size
);
486 if (!value_type
|| value_size
!= map
->value_size
)
489 map
->spin_lock_off
= btf_find_spin_lock(btf
, value_type
);
491 if (map_value_has_spin_lock(map
)) {
492 if (map
->map_type
!= BPF_MAP_TYPE_HASH
&&
493 map
->map_type
!= BPF_MAP_TYPE_ARRAY
&&
494 map
->map_type
!= BPF_MAP_TYPE_CGROUP_STORAGE
)
496 if (map
->spin_lock_off
+ sizeof(struct bpf_spin_lock
) >
499 "verifier bug spin_lock_off %d value_size %d\n",
500 map
->spin_lock_off
, map
->value_size
);
505 if (map
->ops
->map_check_btf
)
506 ret
= map
->ops
->map_check_btf(map
, btf
, key_type
, value_type
);
511 #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
512 /* called via syscall */
513 static int map_create(union bpf_attr
*attr
)
515 int numa_node
= bpf_map_attr_numa_node(attr
);
520 err
= CHECK_ATTR(BPF_MAP_CREATE
);
524 f_flags
= bpf_get_file_flag(attr
->map_flags
);
528 if (numa_node
!= NUMA_NO_NODE
&&
529 ((unsigned int)numa_node
>= nr_node_ids
||
530 !node_online(numa_node
)))
533 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
534 map
= find_and_alloc_map(attr
);
538 err
= bpf_obj_name_cpy(map
->name
, attr
->map_name
);
540 goto free_map_nouncharge
;
542 atomic_set(&map
->refcnt
, 1);
543 atomic_set(&map
->usercnt
, 1);
545 if (attr
->btf_key_type_id
|| attr
->btf_value_type_id
) {
548 if (!attr
->btf_key_type_id
|| !attr
->btf_value_type_id
) {
550 goto free_map_nouncharge
;
553 btf
= btf_get_by_fd(attr
->btf_fd
);
556 goto free_map_nouncharge
;
559 err
= map_check_btf(map
, btf
, attr
->btf_key_type_id
,
560 attr
->btf_value_type_id
);
563 goto free_map_nouncharge
;
567 map
->btf_key_type_id
= attr
->btf_key_type_id
;
568 map
->btf_value_type_id
= attr
->btf_value_type_id
;
570 map
->spin_lock_off
= -EINVAL
;
573 err
= security_bpf_map_alloc(map
);
575 goto free_map_nouncharge
;
577 err
= bpf_map_init_memlock(map
);
581 err
= bpf_map_alloc_id(map
);
585 err
= bpf_map_new_fd(map
, f_flags
);
587 /* failed to allocate fd.
588 * bpf_map_put_with_uref() is needed because the above
589 * bpf_map_alloc_id() has published the map
590 * to the userspace and the userspace may
591 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
593 bpf_map_put_with_uref(map
);
600 bpf_map_release_memlock(map
);
602 security_bpf_map_free(map
);
605 map
->ops
->map_free(map
);
609 /* if error is returned, fd is released.
610 * On success caller should complete fd access with matching fdput()
612 struct bpf_map
*__bpf_map_get(struct fd f
)
615 return ERR_PTR(-EBADF
);
616 if (f
.file
->f_op
!= &bpf_map_fops
) {
618 return ERR_PTR(-EINVAL
);
621 return f
.file
->private_data
;
624 /* prog's and map's refcnt limit */
625 #define BPF_MAX_REFCNT 32768
627 struct bpf_map
*bpf_map_inc(struct bpf_map
*map
, bool uref
)
629 if (atomic_inc_return(&map
->refcnt
) > BPF_MAX_REFCNT
) {
630 atomic_dec(&map
->refcnt
);
631 return ERR_PTR(-EBUSY
);
634 atomic_inc(&map
->usercnt
);
637 EXPORT_SYMBOL_GPL(bpf_map_inc
);
639 struct bpf_map
*bpf_map_get_with_uref(u32 ufd
)
641 struct fd f
= fdget(ufd
);
644 map
= __bpf_map_get(f
);
648 map
= bpf_map_inc(map
, true);
654 /* map_idr_lock should have been held */
655 static struct bpf_map
*bpf_map_inc_not_zero(struct bpf_map
*map
,
660 refold
= atomic_fetch_add_unless(&map
->refcnt
, 1, 0);
662 if (refold
>= BPF_MAX_REFCNT
) {
663 __bpf_map_put(map
, false);
664 return ERR_PTR(-EBUSY
);
668 return ERR_PTR(-ENOENT
);
671 atomic_inc(&map
->usercnt
);
676 int __weak
bpf_stackmap_copy(struct bpf_map
*map
, void *key
, void *value
)
681 static void *__bpf_copy_key(void __user
*ukey
, u64 key_size
)
684 return memdup_user(ukey
, key_size
);
687 return ERR_PTR(-EINVAL
);
692 /* last field in 'union bpf_attr' used by this command */
693 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
695 static int map_lookup_elem(union bpf_attr
*attr
)
697 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
698 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
699 int ufd
= attr
->map_fd
;
701 void *key
, *value
, *ptr
;
706 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM
))
709 if (attr
->flags
& ~BPF_F_LOCK
)
713 map
= __bpf_map_get(f
);
717 if (!(f
.file
->f_mode
& FMODE_CAN_READ
)) {
722 if ((attr
->flags
& BPF_F_LOCK
) &&
723 !map_value_has_spin_lock(map
)) {
728 key
= __bpf_copy_key(ukey
, map
->key_size
);
734 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
735 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
736 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
||
737 map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
)
738 value_size
= round_up(map
->value_size
, 8) * num_possible_cpus();
739 else if (IS_FD_MAP(map
))
740 value_size
= sizeof(u32
);
742 value_size
= map
->value_size
;
745 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
749 if (bpf_map_is_dev_bound(map
)) {
750 err
= bpf_map_offload_lookup_elem(map
, key
, value
);
755 this_cpu_inc(bpf_prog_active
);
756 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
757 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
758 err
= bpf_percpu_hash_copy(map
, key
, value
);
759 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
760 err
= bpf_percpu_array_copy(map
, key
, value
);
761 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
) {
762 err
= bpf_percpu_cgroup_storage_copy(map
, key
, value
);
763 } else if (map
->map_type
== BPF_MAP_TYPE_STACK_TRACE
) {
764 err
= bpf_stackmap_copy(map
, key
, value
);
765 } else if (IS_FD_ARRAY(map
)) {
766 err
= bpf_fd_array_map_lookup_elem(map
, key
, value
);
767 } else if (IS_FD_HASH(map
)) {
768 err
= bpf_fd_htab_map_lookup_elem(map
, key
, value
);
769 } else if (map
->map_type
== BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
) {
770 err
= bpf_fd_reuseport_array_lookup_elem(map
, key
, value
);
771 } else if (map
->map_type
== BPF_MAP_TYPE_QUEUE
||
772 map
->map_type
== BPF_MAP_TYPE_STACK
) {
773 err
= map
->ops
->map_peek_elem(map
, value
);
776 ptr
= map
->ops
->map_lookup_elem(map
, key
);
783 if (attr
->flags
& BPF_F_LOCK
)
784 /* lock 'ptr' and copy everything but lock */
785 copy_map_value_locked(map
, value
, ptr
, true);
787 copy_map_value(map
, value
, ptr
);
788 /* mask lock, since value wasn't zero inited */
789 check_and_init_map_lock(map
, value
);
793 this_cpu_dec(bpf_prog_active
);
801 if (copy_to_user(uvalue
, value
, value_size
) != 0)
815 static void maybe_wait_bpf_programs(struct bpf_map
*map
)
817 /* Wait for any running BPF programs to complete so that
818 * userspace, when we return to it, knows that all programs
819 * that could be running use the new map value.
821 if (map
->map_type
== BPF_MAP_TYPE_HASH_OF_MAPS
||
822 map
->map_type
== BPF_MAP_TYPE_ARRAY_OF_MAPS
)
826 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
828 static int map_update_elem(union bpf_attr
*attr
)
830 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
831 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
832 int ufd
= attr
->map_fd
;
839 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM
))
843 map
= __bpf_map_get(f
);
847 if (!(f
.file
->f_mode
& FMODE_CAN_WRITE
)) {
852 if ((attr
->flags
& BPF_F_LOCK
) &&
853 !map_value_has_spin_lock(map
)) {
858 key
= __bpf_copy_key(ukey
, map
->key_size
);
864 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
865 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
866 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
||
867 map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
)
868 value_size
= round_up(map
->value_size
, 8) * num_possible_cpus();
870 value_size
= map
->value_size
;
873 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
878 if (copy_from_user(value
, uvalue
, value_size
) != 0)
881 /* Need to create a kthread, thus must support schedule */
882 if (bpf_map_is_dev_bound(map
)) {
883 err
= bpf_map_offload_update_elem(map
, key
, value
, attr
->flags
);
885 } else if (map
->map_type
== BPF_MAP_TYPE_CPUMAP
||
886 map
->map_type
== BPF_MAP_TYPE_SOCKHASH
||
887 map
->map_type
== BPF_MAP_TYPE_SOCKMAP
) {
888 err
= map
->ops
->map_update_elem(map
, key
, value
, attr
->flags
);
892 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
893 * inside bpf map update or delete otherwise deadlocks are possible
896 __this_cpu_inc(bpf_prog_active
);
897 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
898 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
899 err
= bpf_percpu_hash_update(map
, key
, value
, attr
->flags
);
900 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
901 err
= bpf_percpu_array_update(map
, key
, value
, attr
->flags
);
902 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
) {
903 err
= bpf_percpu_cgroup_storage_update(map
, key
, value
,
905 } else if (IS_FD_ARRAY(map
)) {
907 err
= bpf_fd_array_map_update_elem(map
, f
.file
, key
, value
,
910 } else if (map
->map_type
== BPF_MAP_TYPE_HASH_OF_MAPS
) {
912 err
= bpf_fd_htab_map_update_elem(map
, f
.file
, key
, value
,
915 } else if (map
->map_type
== BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
) {
916 /* rcu_read_lock() is not needed */
917 err
= bpf_fd_reuseport_array_update_elem(map
, key
, value
,
919 } else if (map
->map_type
== BPF_MAP_TYPE_QUEUE
||
920 map
->map_type
== BPF_MAP_TYPE_STACK
) {
921 err
= map
->ops
->map_push_elem(map
, value
, attr
->flags
);
924 err
= map
->ops
->map_update_elem(map
, key
, value
, attr
->flags
);
927 __this_cpu_dec(bpf_prog_active
);
929 maybe_wait_bpf_programs(map
);
940 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
942 static int map_delete_elem(union bpf_attr
*attr
)
944 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
945 int ufd
= attr
->map_fd
;
951 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM
))
955 map
= __bpf_map_get(f
);
959 if (!(f
.file
->f_mode
& FMODE_CAN_WRITE
)) {
964 key
= __bpf_copy_key(ukey
, map
->key_size
);
970 if (bpf_map_is_dev_bound(map
)) {
971 err
= bpf_map_offload_delete_elem(map
, key
);
976 __this_cpu_inc(bpf_prog_active
);
978 err
= map
->ops
->map_delete_elem(map
, key
);
980 __this_cpu_dec(bpf_prog_active
);
982 maybe_wait_bpf_programs(map
);
990 /* last field in 'union bpf_attr' used by this command */
991 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
993 static int map_get_next_key(union bpf_attr
*attr
)
995 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
996 void __user
*unext_key
= u64_to_user_ptr(attr
->next_key
);
997 int ufd
= attr
->map_fd
;
999 void *key
, *next_key
;
1003 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY
))
1007 map
= __bpf_map_get(f
);
1009 return PTR_ERR(map
);
1011 if (!(f
.file
->f_mode
& FMODE_CAN_READ
)) {
1017 key
= __bpf_copy_key(ukey
, map
->key_size
);
1027 next_key
= kmalloc(map
->key_size
, GFP_USER
);
1031 if (bpf_map_is_dev_bound(map
)) {
1032 err
= bpf_map_offload_get_next_key(map
, key
, next_key
);
1037 err
= map
->ops
->map_get_next_key(map
, key
, next_key
);
1044 if (copy_to_user(unext_key
, next_key
, map
->key_size
) != 0)
1058 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1060 static int map_lookup_and_delete_elem(union bpf_attr
*attr
)
1062 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
1063 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
1064 int ufd
= attr
->map_fd
;
1065 struct bpf_map
*map
;
1071 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM
))
1075 map
= __bpf_map_get(f
);
1077 return PTR_ERR(map
);
1079 if (!(f
.file
->f_mode
& FMODE_CAN_WRITE
)) {
1084 key
= __bpf_copy_key(ukey
, map
->key_size
);
1090 value_size
= map
->value_size
;
1093 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
1097 if (map
->map_type
== BPF_MAP_TYPE_QUEUE
||
1098 map
->map_type
== BPF_MAP_TYPE_STACK
) {
1099 err
= map
->ops
->map_pop_elem(map
, value
);
1107 if (copy_to_user(uvalue
, value
, value_size
) != 0)
1121 static const struct bpf_prog_ops
* const bpf_prog_types
[] = {
1122 #define BPF_PROG_TYPE(_id, _name) \
1123 [_id] = & _name ## _prog_ops,
1124 #define BPF_MAP_TYPE(_id, _ops)
1125 #include <linux/bpf_types.h>
1126 #undef BPF_PROG_TYPE
1130 static int find_prog_type(enum bpf_prog_type type
, struct bpf_prog
*prog
)
1132 const struct bpf_prog_ops
*ops
;
1134 if (type
>= ARRAY_SIZE(bpf_prog_types
))
1136 type
= array_index_nospec(type
, ARRAY_SIZE(bpf_prog_types
));
1137 ops
= bpf_prog_types
[type
];
1141 if (!bpf_prog_is_dev_bound(prog
->aux
))
1142 prog
->aux
->ops
= ops
;
1144 prog
->aux
->ops
= &bpf_offload_prog_ops
;
1149 /* drop refcnt on maps used by eBPF program and free auxilary data */
1150 static void free_used_maps(struct bpf_prog_aux
*aux
)
1152 enum bpf_cgroup_storage_type stype
;
1155 for_each_cgroup_storage_type(stype
) {
1156 if (!aux
->cgroup_storage
[stype
])
1158 bpf_cgroup_storage_release(aux
->prog
,
1159 aux
->cgroup_storage
[stype
]);
1162 for (i
= 0; i
< aux
->used_map_cnt
; i
++)
1163 bpf_map_put(aux
->used_maps
[i
]);
1165 kfree(aux
->used_maps
);
1168 int __bpf_prog_charge(struct user_struct
*user
, u32 pages
)
1170 unsigned long memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
1171 unsigned long user_bufs
;
1174 user_bufs
= atomic_long_add_return(pages
, &user
->locked_vm
);
1175 if (user_bufs
> memlock_limit
) {
1176 atomic_long_sub(pages
, &user
->locked_vm
);
1184 void __bpf_prog_uncharge(struct user_struct
*user
, u32 pages
)
1187 atomic_long_sub(pages
, &user
->locked_vm
);
1190 static int bpf_prog_charge_memlock(struct bpf_prog
*prog
)
1192 struct user_struct
*user
= get_current_user();
1195 ret
= __bpf_prog_charge(user
, prog
->pages
);
1201 prog
->aux
->user
= user
;
1205 static void bpf_prog_uncharge_memlock(struct bpf_prog
*prog
)
1207 struct user_struct
*user
= prog
->aux
->user
;
1209 __bpf_prog_uncharge(user
, prog
->pages
);
1213 static int bpf_prog_alloc_id(struct bpf_prog
*prog
)
1217 idr_preload(GFP_KERNEL
);
1218 spin_lock_bh(&prog_idr_lock
);
1219 id
= idr_alloc_cyclic(&prog_idr
, prog
, 1, INT_MAX
, GFP_ATOMIC
);
1222 spin_unlock_bh(&prog_idr_lock
);
1225 /* id is in [1, INT_MAX) */
1226 if (WARN_ON_ONCE(!id
))
1229 return id
> 0 ? 0 : id
;
1232 void bpf_prog_free_id(struct bpf_prog
*prog
, bool do_idr_lock
)
1234 /* cBPF to eBPF migrations are currently not in the idr store.
1235 * Offloaded programs are removed from the store when their device
1236 * disappears - even if someone grabs an fd to them they are unusable,
1237 * simply waiting for refcnt to drop to be freed.
1243 spin_lock_bh(&prog_idr_lock
);
1245 __acquire(&prog_idr_lock
);
1247 idr_remove(&prog_idr
, prog
->aux
->id
);
1251 spin_unlock_bh(&prog_idr_lock
);
1253 __release(&prog_idr_lock
);
1256 static void __bpf_prog_put_rcu(struct rcu_head
*rcu
)
1258 struct bpf_prog_aux
*aux
= container_of(rcu
, struct bpf_prog_aux
, rcu
);
1260 free_used_maps(aux
);
1261 bpf_prog_uncharge_memlock(aux
->prog
);
1262 security_bpf_prog_free(aux
);
1263 bpf_prog_free(aux
->prog
);
1266 static void __bpf_prog_put(struct bpf_prog
*prog
, bool do_idr_lock
)
1268 if (atomic_dec_and_test(&prog
->aux
->refcnt
)) {
1269 perf_event_bpf_event(prog
, PERF_BPF_EVENT_PROG_UNLOAD
, 0);
1270 /* bpf_prog_free_id() must be called first */
1271 bpf_prog_free_id(prog
, do_idr_lock
);
1272 bpf_prog_kallsyms_del_all(prog
);
1273 btf_put(prog
->aux
->btf
);
1274 kvfree(prog
->aux
->func_info
);
1275 bpf_prog_free_linfo(prog
);
1277 call_rcu(&prog
->aux
->rcu
, __bpf_prog_put_rcu
);
1281 void bpf_prog_put(struct bpf_prog
*prog
)
1283 __bpf_prog_put(prog
, true);
1285 EXPORT_SYMBOL_GPL(bpf_prog_put
);
1287 static int bpf_prog_release(struct inode
*inode
, struct file
*filp
)
1289 struct bpf_prog
*prog
= filp
->private_data
;
1295 static void bpf_prog_get_stats(const struct bpf_prog
*prog
,
1296 struct bpf_prog_stats
*stats
)
1298 u64 nsecs
= 0, cnt
= 0;
1301 for_each_possible_cpu(cpu
) {
1302 const struct bpf_prog_stats
*st
;
1306 st
= per_cpu_ptr(prog
->aux
->stats
, cpu
);
1308 start
= u64_stats_fetch_begin_irq(&st
->syncp
);
1311 } while (u64_stats_fetch_retry_irq(&st
->syncp
, start
));
1315 stats
->nsecs
= nsecs
;
1319 #ifdef CONFIG_PROC_FS
1320 static void bpf_prog_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
1322 const struct bpf_prog
*prog
= filp
->private_data
;
1323 char prog_tag
[sizeof(prog
->tag
) * 2 + 1] = { };
1324 struct bpf_prog_stats stats
;
1326 bpf_prog_get_stats(prog
, &stats
);
1327 bin2hex(prog_tag
, prog
->tag
, sizeof(prog
->tag
));
1334 "run_time_ns:\t%llu\n"
1339 prog
->pages
* 1ULL << PAGE_SHIFT
,
1346 const struct file_operations bpf_prog_fops
= {
1347 #ifdef CONFIG_PROC_FS
1348 .show_fdinfo
= bpf_prog_show_fdinfo
,
1350 .release
= bpf_prog_release
,
1351 .read
= bpf_dummy_read
,
1352 .write
= bpf_dummy_write
,
1355 int bpf_prog_new_fd(struct bpf_prog
*prog
)
1359 ret
= security_bpf_prog(prog
);
1363 return anon_inode_getfd("bpf-prog", &bpf_prog_fops
, prog
,
1364 O_RDWR
| O_CLOEXEC
);
1367 static struct bpf_prog
*____bpf_prog_get(struct fd f
)
1370 return ERR_PTR(-EBADF
);
1371 if (f
.file
->f_op
!= &bpf_prog_fops
) {
1373 return ERR_PTR(-EINVAL
);
1376 return f
.file
->private_data
;
1379 struct bpf_prog
*bpf_prog_add(struct bpf_prog
*prog
, int i
)
1381 if (atomic_add_return(i
, &prog
->aux
->refcnt
) > BPF_MAX_REFCNT
) {
1382 atomic_sub(i
, &prog
->aux
->refcnt
);
1383 return ERR_PTR(-EBUSY
);
1387 EXPORT_SYMBOL_GPL(bpf_prog_add
);
1389 void bpf_prog_sub(struct bpf_prog
*prog
, int i
)
1391 /* Only to be used for undoing previous bpf_prog_add() in some
1392 * error path. We still know that another entity in our call
1393 * path holds a reference to the program, thus atomic_sub() can
1394 * be safely used in such cases!
1396 WARN_ON(atomic_sub_return(i
, &prog
->aux
->refcnt
) == 0);
1398 EXPORT_SYMBOL_GPL(bpf_prog_sub
);
1400 struct bpf_prog
*bpf_prog_inc(struct bpf_prog
*prog
)
1402 return bpf_prog_add(prog
, 1);
1404 EXPORT_SYMBOL_GPL(bpf_prog_inc
);
1406 /* prog_idr_lock should have been held */
1407 struct bpf_prog
*bpf_prog_inc_not_zero(struct bpf_prog
*prog
)
1411 refold
= atomic_fetch_add_unless(&prog
->aux
->refcnt
, 1, 0);
1413 if (refold
>= BPF_MAX_REFCNT
) {
1414 __bpf_prog_put(prog
, false);
1415 return ERR_PTR(-EBUSY
);
1419 return ERR_PTR(-ENOENT
);
1423 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero
);
1425 bool bpf_prog_get_ok(struct bpf_prog
*prog
,
1426 enum bpf_prog_type
*attach_type
, bool attach_drv
)
1428 /* not an attachment, just a refcount inc, always allow */
1432 if (prog
->type
!= *attach_type
)
1434 if (bpf_prog_is_dev_bound(prog
->aux
) && !attach_drv
)
1440 static struct bpf_prog
*__bpf_prog_get(u32 ufd
, enum bpf_prog_type
*attach_type
,
1443 struct fd f
= fdget(ufd
);
1444 struct bpf_prog
*prog
;
1446 prog
= ____bpf_prog_get(f
);
1449 if (!bpf_prog_get_ok(prog
, attach_type
, attach_drv
)) {
1450 prog
= ERR_PTR(-EINVAL
);
1454 prog
= bpf_prog_inc(prog
);
1460 struct bpf_prog
*bpf_prog_get(u32 ufd
)
1462 return __bpf_prog_get(ufd
, NULL
, false);
1465 struct bpf_prog
*bpf_prog_get_type_dev(u32 ufd
, enum bpf_prog_type type
,
1468 return __bpf_prog_get(ufd
, &type
, attach_drv
);
1470 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev
);
1472 /* Initially all BPF programs could be loaded w/o specifying
1473 * expected_attach_type. Later for some of them specifying expected_attach_type
1474 * at load time became required so that program could be validated properly.
1475 * Programs of types that are allowed to be loaded both w/ and w/o (for
1476 * backward compatibility) expected_attach_type, should have the default attach
1477 * type assigned to expected_attach_type for the latter case, so that it can be
1478 * validated later at attach time.
1480 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1481 * prog type requires it but has some attach types that have to be backward
1484 static void bpf_prog_load_fixup_attach_type(union bpf_attr
*attr
)
1486 switch (attr
->prog_type
) {
1487 case BPF_PROG_TYPE_CGROUP_SOCK
:
1488 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1489 * exist so checking for non-zero is the way to go here.
1491 if (!attr
->expected_attach_type
)
1492 attr
->expected_attach_type
=
1493 BPF_CGROUP_INET_SOCK_CREATE
;
1499 bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type
,
1500 enum bpf_attach_type expected_attach_type
)
1502 switch (prog_type
) {
1503 case BPF_PROG_TYPE_CGROUP_SOCK
:
1504 switch (expected_attach_type
) {
1505 case BPF_CGROUP_INET_SOCK_CREATE
:
1506 case BPF_CGROUP_INET4_POST_BIND
:
1507 case BPF_CGROUP_INET6_POST_BIND
:
1512 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR
:
1513 switch (expected_attach_type
) {
1514 case BPF_CGROUP_INET4_BIND
:
1515 case BPF_CGROUP_INET6_BIND
:
1516 case BPF_CGROUP_INET4_CONNECT
:
1517 case BPF_CGROUP_INET6_CONNECT
:
1518 case BPF_CGROUP_UDP4_SENDMSG
:
1519 case BPF_CGROUP_UDP6_SENDMSG
:
1529 /* last field in 'union bpf_attr' used by this command */
1530 #define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
1532 static int bpf_prog_load(union bpf_attr
*attr
, union bpf_attr __user
*uattr
)
1534 enum bpf_prog_type type
= attr
->prog_type
;
1535 struct bpf_prog
*prog
;
1540 if (CHECK_ATTR(BPF_PROG_LOAD
))
1543 if (attr
->prog_flags
& ~(BPF_F_STRICT_ALIGNMENT
| BPF_F_ANY_ALIGNMENT
))
1546 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
) &&
1547 (attr
->prog_flags
& BPF_F_ANY_ALIGNMENT
) &&
1548 !capable(CAP_SYS_ADMIN
))
1551 /* copy eBPF program license from user space */
1552 if (strncpy_from_user(license
, u64_to_user_ptr(attr
->license
),
1553 sizeof(license
) - 1) < 0)
1555 license
[sizeof(license
) - 1] = 0;
1557 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1558 is_gpl
= license_is_gpl_compatible(license
);
1560 if (attr
->insn_cnt
== 0 || attr
->insn_cnt
> BPF_MAXINSNS
)
1562 if (type
!= BPF_PROG_TYPE_SOCKET_FILTER
&&
1563 type
!= BPF_PROG_TYPE_CGROUP_SKB
&&
1564 !capable(CAP_SYS_ADMIN
))
1567 bpf_prog_load_fixup_attach_type(attr
);
1568 if (bpf_prog_load_check_attach_type(type
, attr
->expected_attach_type
))
1571 /* plain bpf_prog allocation */
1572 prog
= bpf_prog_alloc(bpf_prog_size(attr
->insn_cnt
), GFP_USER
);
1576 prog
->expected_attach_type
= attr
->expected_attach_type
;
1578 prog
->aux
->offload_requested
= !!attr
->prog_ifindex
;
1580 err
= security_bpf_prog_alloc(prog
->aux
);
1582 goto free_prog_nouncharge
;
1584 err
= bpf_prog_charge_memlock(prog
);
1588 prog
->len
= attr
->insn_cnt
;
1591 if (copy_from_user(prog
->insns
, u64_to_user_ptr(attr
->insns
),
1592 bpf_prog_insn_size(prog
)) != 0)
1595 prog
->orig_prog
= NULL
;
1598 atomic_set(&prog
->aux
->refcnt
, 1);
1599 prog
->gpl_compatible
= is_gpl
? 1 : 0;
1601 if (bpf_prog_is_dev_bound(prog
->aux
)) {
1602 err
= bpf_prog_offload_init(prog
, attr
);
1607 /* find program type: socket_filter vs tracing_filter */
1608 err
= find_prog_type(type
, prog
);
1612 prog
->aux
->load_time
= ktime_get_boot_ns();
1613 err
= bpf_obj_name_cpy(prog
->aux
->name
, attr
->prog_name
);
1617 /* run eBPF verifier */
1618 err
= bpf_check(&prog
, attr
, uattr
);
1620 goto free_used_maps
;
1622 prog
= bpf_prog_select_runtime(prog
, &err
);
1624 goto free_used_maps
;
1626 err
= bpf_prog_alloc_id(prog
);
1628 goto free_used_maps
;
1630 err
= bpf_prog_new_fd(prog
);
1632 /* failed to allocate fd.
1633 * bpf_prog_put() is needed because the above
1634 * bpf_prog_alloc_id() has published the prog
1635 * to the userspace and the userspace may
1636 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1642 bpf_prog_kallsyms_add(prog
);
1643 perf_event_bpf_event(prog
, PERF_BPF_EVENT_PROG_LOAD
, 0);
1647 bpf_prog_free_linfo(prog
);
1648 kvfree(prog
->aux
->func_info
);
1649 btf_put(prog
->aux
->btf
);
1650 bpf_prog_kallsyms_del_subprogs(prog
);
1651 free_used_maps(prog
->aux
);
1653 bpf_prog_uncharge_memlock(prog
);
1655 security_bpf_prog_free(prog
->aux
);
1656 free_prog_nouncharge
:
1657 bpf_prog_free(prog
);
1661 #define BPF_OBJ_LAST_FIELD file_flags
1663 static int bpf_obj_pin(const union bpf_attr
*attr
)
1665 if (CHECK_ATTR(BPF_OBJ
) || attr
->file_flags
!= 0)
1668 return bpf_obj_pin_user(attr
->bpf_fd
, u64_to_user_ptr(attr
->pathname
));
1671 static int bpf_obj_get(const union bpf_attr
*attr
)
1673 if (CHECK_ATTR(BPF_OBJ
) || attr
->bpf_fd
!= 0 ||
1674 attr
->file_flags
& ~BPF_OBJ_FLAG_MASK
)
1677 return bpf_obj_get_user(u64_to_user_ptr(attr
->pathname
),
1681 struct bpf_raw_tracepoint
{
1682 struct bpf_raw_event_map
*btp
;
1683 struct bpf_prog
*prog
;
1686 static int bpf_raw_tracepoint_release(struct inode
*inode
, struct file
*filp
)
1688 struct bpf_raw_tracepoint
*raw_tp
= filp
->private_data
;
1691 bpf_probe_unregister(raw_tp
->btp
, raw_tp
->prog
);
1692 bpf_prog_put(raw_tp
->prog
);
1694 bpf_put_raw_tracepoint(raw_tp
->btp
);
1699 static const struct file_operations bpf_raw_tp_fops
= {
1700 .release
= bpf_raw_tracepoint_release
,
1701 .read
= bpf_dummy_read
,
1702 .write
= bpf_dummy_write
,
1705 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1707 static int bpf_raw_tracepoint_open(const union bpf_attr
*attr
)
1709 struct bpf_raw_tracepoint
*raw_tp
;
1710 struct bpf_raw_event_map
*btp
;
1711 struct bpf_prog
*prog
;
1715 if (strncpy_from_user(tp_name
, u64_to_user_ptr(attr
->raw_tracepoint
.name
),
1716 sizeof(tp_name
) - 1) < 0)
1718 tp_name
[sizeof(tp_name
) - 1] = 0;
1720 btp
= bpf_get_raw_tracepoint(tp_name
);
1724 raw_tp
= kzalloc(sizeof(*raw_tp
), GFP_USER
);
1731 prog
= bpf_prog_get_type(attr
->raw_tracepoint
.prog_fd
,
1732 BPF_PROG_TYPE_RAW_TRACEPOINT
);
1734 err
= PTR_ERR(prog
);
1738 err
= bpf_probe_register(raw_tp
->btp
, prog
);
1742 raw_tp
->prog
= prog
;
1743 tp_fd
= anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops
, raw_tp
,
1746 bpf_probe_unregister(raw_tp
->btp
, prog
);
1757 bpf_put_raw_tracepoint(btp
);
1761 static int bpf_prog_attach_check_attach_type(const struct bpf_prog
*prog
,
1762 enum bpf_attach_type attach_type
)
1764 switch (prog
->type
) {
1765 case BPF_PROG_TYPE_CGROUP_SOCK
:
1766 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR
:
1767 return attach_type
== prog
->expected_attach_type
? 0 : -EINVAL
;
1773 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1775 #define BPF_F_ATTACH_MASK \
1776 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1778 static int bpf_prog_attach(const union bpf_attr
*attr
)
1780 enum bpf_prog_type ptype
;
1781 struct bpf_prog
*prog
;
1784 if (!capable(CAP_NET_ADMIN
))
1787 if (CHECK_ATTR(BPF_PROG_ATTACH
))
1790 if (attr
->attach_flags
& ~BPF_F_ATTACH_MASK
)
1793 switch (attr
->attach_type
) {
1794 case BPF_CGROUP_INET_INGRESS
:
1795 case BPF_CGROUP_INET_EGRESS
:
1796 ptype
= BPF_PROG_TYPE_CGROUP_SKB
;
1798 case BPF_CGROUP_INET_SOCK_CREATE
:
1799 case BPF_CGROUP_INET4_POST_BIND
:
1800 case BPF_CGROUP_INET6_POST_BIND
:
1801 ptype
= BPF_PROG_TYPE_CGROUP_SOCK
;
1803 case BPF_CGROUP_INET4_BIND
:
1804 case BPF_CGROUP_INET6_BIND
:
1805 case BPF_CGROUP_INET4_CONNECT
:
1806 case BPF_CGROUP_INET6_CONNECT
:
1807 case BPF_CGROUP_UDP4_SENDMSG
:
1808 case BPF_CGROUP_UDP6_SENDMSG
:
1809 ptype
= BPF_PROG_TYPE_CGROUP_SOCK_ADDR
;
1811 case BPF_CGROUP_SOCK_OPS
:
1812 ptype
= BPF_PROG_TYPE_SOCK_OPS
;
1814 case BPF_CGROUP_DEVICE
:
1815 ptype
= BPF_PROG_TYPE_CGROUP_DEVICE
;
1817 case BPF_SK_MSG_VERDICT
:
1818 ptype
= BPF_PROG_TYPE_SK_MSG
;
1820 case BPF_SK_SKB_STREAM_PARSER
:
1821 case BPF_SK_SKB_STREAM_VERDICT
:
1822 ptype
= BPF_PROG_TYPE_SK_SKB
;
1824 case BPF_LIRC_MODE2
:
1825 ptype
= BPF_PROG_TYPE_LIRC_MODE2
;
1827 case BPF_FLOW_DISSECTOR
:
1828 ptype
= BPF_PROG_TYPE_FLOW_DISSECTOR
;
1834 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
, ptype
);
1836 return PTR_ERR(prog
);
1838 if (bpf_prog_attach_check_attach_type(prog
, attr
->attach_type
)) {
1844 case BPF_PROG_TYPE_SK_SKB
:
1845 case BPF_PROG_TYPE_SK_MSG
:
1846 ret
= sock_map_get_from_fd(attr
, prog
);
1848 case BPF_PROG_TYPE_LIRC_MODE2
:
1849 ret
= lirc_prog_attach(attr
, prog
);
1851 case BPF_PROG_TYPE_FLOW_DISSECTOR
:
1852 ret
= skb_flow_dissector_bpf_prog_attach(attr
, prog
);
1855 ret
= cgroup_bpf_prog_attach(attr
, ptype
, prog
);
1863 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1865 static int bpf_prog_detach(const union bpf_attr
*attr
)
1867 enum bpf_prog_type ptype
;
1869 if (!capable(CAP_NET_ADMIN
))
1872 if (CHECK_ATTR(BPF_PROG_DETACH
))
1875 switch (attr
->attach_type
) {
1876 case BPF_CGROUP_INET_INGRESS
:
1877 case BPF_CGROUP_INET_EGRESS
:
1878 ptype
= BPF_PROG_TYPE_CGROUP_SKB
;
1880 case BPF_CGROUP_INET_SOCK_CREATE
:
1881 case BPF_CGROUP_INET4_POST_BIND
:
1882 case BPF_CGROUP_INET6_POST_BIND
:
1883 ptype
= BPF_PROG_TYPE_CGROUP_SOCK
;
1885 case BPF_CGROUP_INET4_BIND
:
1886 case BPF_CGROUP_INET6_BIND
:
1887 case BPF_CGROUP_INET4_CONNECT
:
1888 case BPF_CGROUP_INET6_CONNECT
:
1889 case BPF_CGROUP_UDP4_SENDMSG
:
1890 case BPF_CGROUP_UDP6_SENDMSG
:
1891 ptype
= BPF_PROG_TYPE_CGROUP_SOCK_ADDR
;
1893 case BPF_CGROUP_SOCK_OPS
:
1894 ptype
= BPF_PROG_TYPE_SOCK_OPS
;
1896 case BPF_CGROUP_DEVICE
:
1897 ptype
= BPF_PROG_TYPE_CGROUP_DEVICE
;
1899 case BPF_SK_MSG_VERDICT
:
1900 return sock_map_get_from_fd(attr
, NULL
);
1901 case BPF_SK_SKB_STREAM_PARSER
:
1902 case BPF_SK_SKB_STREAM_VERDICT
:
1903 return sock_map_get_from_fd(attr
, NULL
);
1904 case BPF_LIRC_MODE2
:
1905 return lirc_prog_detach(attr
);
1906 case BPF_FLOW_DISSECTOR
:
1907 return skb_flow_dissector_bpf_prog_detach(attr
);
1912 return cgroup_bpf_prog_detach(attr
, ptype
);
1915 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1917 static int bpf_prog_query(const union bpf_attr
*attr
,
1918 union bpf_attr __user
*uattr
)
1920 if (!capable(CAP_NET_ADMIN
))
1922 if (CHECK_ATTR(BPF_PROG_QUERY
))
1924 if (attr
->query
.query_flags
& ~BPF_F_QUERY_EFFECTIVE
)
1927 switch (attr
->query
.attach_type
) {
1928 case BPF_CGROUP_INET_INGRESS
:
1929 case BPF_CGROUP_INET_EGRESS
:
1930 case BPF_CGROUP_INET_SOCK_CREATE
:
1931 case BPF_CGROUP_INET4_BIND
:
1932 case BPF_CGROUP_INET6_BIND
:
1933 case BPF_CGROUP_INET4_POST_BIND
:
1934 case BPF_CGROUP_INET6_POST_BIND
:
1935 case BPF_CGROUP_INET4_CONNECT
:
1936 case BPF_CGROUP_INET6_CONNECT
:
1937 case BPF_CGROUP_UDP4_SENDMSG
:
1938 case BPF_CGROUP_UDP6_SENDMSG
:
1939 case BPF_CGROUP_SOCK_OPS
:
1940 case BPF_CGROUP_DEVICE
:
1942 case BPF_LIRC_MODE2
:
1943 return lirc_prog_query(attr
, uattr
);
1948 return cgroup_bpf_prog_query(attr
, uattr
);
1951 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1953 static int bpf_prog_test_run(const union bpf_attr
*attr
,
1954 union bpf_attr __user
*uattr
)
1956 struct bpf_prog
*prog
;
1957 int ret
= -ENOTSUPP
;
1959 if (!capable(CAP_SYS_ADMIN
))
1961 if (CHECK_ATTR(BPF_PROG_TEST_RUN
))
1964 prog
= bpf_prog_get(attr
->test
.prog_fd
);
1966 return PTR_ERR(prog
);
1968 if (prog
->aux
->ops
->test_run
)
1969 ret
= prog
->aux
->ops
->test_run(prog
, attr
, uattr
);
1975 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1977 static int bpf_obj_get_next_id(const union bpf_attr
*attr
,
1978 union bpf_attr __user
*uattr
,
1982 u32 next_id
= attr
->start_id
;
1985 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID
) || next_id
>= INT_MAX
)
1988 if (!capable(CAP_SYS_ADMIN
))
1993 if (!idr_get_next(idr
, &next_id
))
1995 spin_unlock_bh(lock
);
1998 err
= put_user(next_id
, &uattr
->next_id
);
2003 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2005 static int bpf_prog_get_fd_by_id(const union bpf_attr
*attr
)
2007 struct bpf_prog
*prog
;
2008 u32 id
= attr
->prog_id
;
2011 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID
))
2014 if (!capable(CAP_SYS_ADMIN
))
2017 spin_lock_bh(&prog_idr_lock
);
2018 prog
= idr_find(&prog_idr
, id
);
2020 prog
= bpf_prog_inc_not_zero(prog
);
2022 prog
= ERR_PTR(-ENOENT
);
2023 spin_unlock_bh(&prog_idr_lock
);
2026 return PTR_ERR(prog
);
2028 fd
= bpf_prog_new_fd(prog
);
2035 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2037 static int bpf_map_get_fd_by_id(const union bpf_attr
*attr
)
2039 struct bpf_map
*map
;
2040 u32 id
= attr
->map_id
;
2044 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID
) ||
2045 attr
->open_flags
& ~BPF_OBJ_FLAG_MASK
)
2048 if (!capable(CAP_SYS_ADMIN
))
2051 f_flags
= bpf_get_file_flag(attr
->open_flags
);
2055 spin_lock_bh(&map_idr_lock
);
2056 map
= idr_find(&map_idr
, id
);
2058 map
= bpf_map_inc_not_zero(map
, true);
2060 map
= ERR_PTR(-ENOENT
);
2061 spin_unlock_bh(&map_idr_lock
);
2064 return PTR_ERR(map
);
2066 fd
= bpf_map_new_fd(map
, f_flags
);
2068 bpf_map_put_with_uref(map
);
2073 static const struct bpf_map
*bpf_map_from_imm(const struct bpf_prog
*prog
,
2078 for (i
= 0; i
< prog
->aux
->used_map_cnt
; i
++)
2079 if (prog
->aux
->used_maps
[i
] == (void *)addr
)
2080 return prog
->aux
->used_maps
[i
];
2084 static struct bpf_insn
*bpf_insn_prepare_dump(const struct bpf_prog
*prog
)
2086 const struct bpf_map
*map
;
2087 struct bpf_insn
*insns
;
2091 insns
= kmemdup(prog
->insnsi
, bpf_prog_insn_size(prog
),
2096 for (i
= 0; i
< prog
->len
; i
++) {
2097 if (insns
[i
].code
== (BPF_JMP
| BPF_TAIL_CALL
)) {
2098 insns
[i
].code
= BPF_JMP
| BPF_CALL
;
2099 insns
[i
].imm
= BPF_FUNC_tail_call
;
2102 if (insns
[i
].code
== (BPF_JMP
| BPF_CALL
) ||
2103 insns
[i
].code
== (BPF_JMP
| BPF_CALL_ARGS
)) {
2104 if (insns
[i
].code
== (BPF_JMP
| BPF_CALL_ARGS
))
2105 insns
[i
].code
= BPF_JMP
| BPF_CALL
;
2106 if (!bpf_dump_raw_ok())
2111 if (insns
[i
].code
!= (BPF_LD
| BPF_IMM
| BPF_DW
))
2114 imm
= ((u64
)insns
[i
+ 1].imm
<< 32) | (u32
)insns
[i
].imm
;
2115 map
= bpf_map_from_imm(prog
, imm
);
2117 insns
[i
].src_reg
= BPF_PSEUDO_MAP_FD
;
2118 insns
[i
].imm
= map
->id
;
2119 insns
[i
+ 1].imm
= 0;
2127 static int set_info_rec_size(struct bpf_prog_info
*info
)
2130 * Ensure info.*_rec_size is the same as kernel expected size
2134 * Only allow zero *_rec_size if both _rec_size and _cnt are
2135 * zero. In this case, the kernel will set the expected
2136 * _rec_size back to the info.
2139 if ((info
->nr_func_info
|| info
->func_info_rec_size
) &&
2140 info
->func_info_rec_size
!= sizeof(struct bpf_func_info
))
2143 if ((info
->nr_line_info
|| info
->line_info_rec_size
) &&
2144 info
->line_info_rec_size
!= sizeof(struct bpf_line_info
))
2147 if ((info
->nr_jited_line_info
|| info
->jited_line_info_rec_size
) &&
2148 info
->jited_line_info_rec_size
!= sizeof(__u64
))
2151 info
->func_info_rec_size
= sizeof(struct bpf_func_info
);
2152 info
->line_info_rec_size
= sizeof(struct bpf_line_info
);
2153 info
->jited_line_info_rec_size
= sizeof(__u64
);
2158 static int bpf_prog_get_info_by_fd(struct bpf_prog
*prog
,
2159 const union bpf_attr
*attr
,
2160 union bpf_attr __user
*uattr
)
2162 struct bpf_prog_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
2163 struct bpf_prog_info info
= {};
2164 u32 info_len
= attr
->info
.info_len
;
2165 struct bpf_prog_stats stats
;
2166 char __user
*uinsns
;
2170 err
= bpf_check_uarg_tail_zero(uinfo
, sizeof(info
), info_len
);
2173 info_len
= min_t(u32
, sizeof(info
), info_len
);
2175 if (copy_from_user(&info
, uinfo
, info_len
))
2178 info
.type
= prog
->type
;
2179 info
.id
= prog
->aux
->id
;
2180 info
.load_time
= prog
->aux
->load_time
;
2181 info
.created_by_uid
= from_kuid_munged(current_user_ns(),
2182 prog
->aux
->user
->uid
);
2183 info
.gpl_compatible
= prog
->gpl_compatible
;
2185 memcpy(info
.tag
, prog
->tag
, sizeof(prog
->tag
));
2186 memcpy(info
.name
, prog
->aux
->name
, sizeof(prog
->aux
->name
));
2188 ulen
= info
.nr_map_ids
;
2189 info
.nr_map_ids
= prog
->aux
->used_map_cnt
;
2190 ulen
= min_t(u32
, info
.nr_map_ids
, ulen
);
2192 u32 __user
*user_map_ids
= u64_to_user_ptr(info
.map_ids
);
2195 for (i
= 0; i
< ulen
; i
++)
2196 if (put_user(prog
->aux
->used_maps
[i
]->id
,
2201 err
= set_info_rec_size(&info
);
2205 bpf_prog_get_stats(prog
, &stats
);
2206 info
.run_time_ns
= stats
.nsecs
;
2207 info
.run_cnt
= stats
.cnt
;
2209 if (!capable(CAP_SYS_ADMIN
)) {
2210 info
.jited_prog_len
= 0;
2211 info
.xlated_prog_len
= 0;
2212 info
.nr_jited_ksyms
= 0;
2213 info
.nr_jited_func_lens
= 0;
2214 info
.nr_func_info
= 0;
2215 info
.nr_line_info
= 0;
2216 info
.nr_jited_line_info
= 0;
2220 ulen
= info
.xlated_prog_len
;
2221 info
.xlated_prog_len
= bpf_prog_insn_size(prog
);
2222 if (info
.xlated_prog_len
&& ulen
) {
2223 struct bpf_insn
*insns_sanitized
;
2226 if (prog
->blinded
&& !bpf_dump_raw_ok()) {
2227 info
.xlated_prog_insns
= 0;
2230 insns_sanitized
= bpf_insn_prepare_dump(prog
);
2231 if (!insns_sanitized
)
2233 uinsns
= u64_to_user_ptr(info
.xlated_prog_insns
);
2234 ulen
= min_t(u32
, info
.xlated_prog_len
, ulen
);
2235 fault
= copy_to_user(uinsns
, insns_sanitized
, ulen
);
2236 kfree(insns_sanitized
);
2241 if (bpf_prog_is_dev_bound(prog
->aux
)) {
2242 err
= bpf_prog_offload_info_fill(&info
, prog
);
2248 /* NOTE: the following code is supposed to be skipped for offload.
2249 * bpf_prog_offload_info_fill() is the place to fill similar fields
2252 ulen
= info
.jited_prog_len
;
2253 if (prog
->aux
->func_cnt
) {
2256 info
.jited_prog_len
= 0;
2257 for (i
= 0; i
< prog
->aux
->func_cnt
; i
++)
2258 info
.jited_prog_len
+= prog
->aux
->func
[i
]->jited_len
;
2260 info
.jited_prog_len
= prog
->jited_len
;
2263 if (info
.jited_prog_len
&& ulen
) {
2264 if (bpf_dump_raw_ok()) {
2265 uinsns
= u64_to_user_ptr(info
.jited_prog_insns
);
2266 ulen
= min_t(u32
, info
.jited_prog_len
, ulen
);
2268 /* for multi-function programs, copy the JITed
2269 * instructions for all the functions
2271 if (prog
->aux
->func_cnt
) {
2276 for (i
= 0; i
< prog
->aux
->func_cnt
; i
++) {
2277 len
= prog
->aux
->func
[i
]->jited_len
;
2278 len
= min_t(u32
, len
, free
);
2279 img
= (u8
*) prog
->aux
->func
[i
]->bpf_func
;
2280 if (copy_to_user(uinsns
, img
, len
))
2288 if (copy_to_user(uinsns
, prog
->bpf_func
, ulen
))
2292 info
.jited_prog_insns
= 0;
2296 ulen
= info
.nr_jited_ksyms
;
2297 info
.nr_jited_ksyms
= prog
->aux
->func_cnt
? : 1;
2299 if (bpf_dump_raw_ok()) {
2300 unsigned long ksym_addr
;
2301 u64 __user
*user_ksyms
;
2304 /* copy the address of the kernel symbol
2305 * corresponding to each function
2307 ulen
= min_t(u32
, info
.nr_jited_ksyms
, ulen
);
2308 user_ksyms
= u64_to_user_ptr(info
.jited_ksyms
);
2309 if (prog
->aux
->func_cnt
) {
2310 for (i
= 0; i
< ulen
; i
++) {
2311 ksym_addr
= (unsigned long)
2312 prog
->aux
->func
[i
]->bpf_func
;
2313 if (put_user((u64
) ksym_addr
,
2318 ksym_addr
= (unsigned long) prog
->bpf_func
;
2319 if (put_user((u64
) ksym_addr
, &user_ksyms
[0]))
2323 info
.jited_ksyms
= 0;
2327 ulen
= info
.nr_jited_func_lens
;
2328 info
.nr_jited_func_lens
= prog
->aux
->func_cnt
? : 1;
2330 if (bpf_dump_raw_ok()) {
2331 u32 __user
*user_lens
;
2334 /* copy the JITed image lengths for each function */
2335 ulen
= min_t(u32
, info
.nr_jited_func_lens
, ulen
);
2336 user_lens
= u64_to_user_ptr(info
.jited_func_lens
);
2337 if (prog
->aux
->func_cnt
) {
2338 for (i
= 0; i
< ulen
; i
++) {
2340 prog
->aux
->func
[i
]->jited_len
;
2341 if (put_user(func_len
, &user_lens
[i
]))
2345 func_len
= prog
->jited_len
;
2346 if (put_user(func_len
, &user_lens
[0]))
2350 info
.jited_func_lens
= 0;
2355 info
.btf_id
= btf_id(prog
->aux
->btf
);
2357 ulen
= info
.nr_func_info
;
2358 info
.nr_func_info
= prog
->aux
->func_info_cnt
;
2359 if (info
.nr_func_info
&& ulen
) {
2360 char __user
*user_finfo
;
2362 user_finfo
= u64_to_user_ptr(info
.func_info
);
2363 ulen
= min_t(u32
, info
.nr_func_info
, ulen
);
2364 if (copy_to_user(user_finfo
, prog
->aux
->func_info
,
2365 info
.func_info_rec_size
* ulen
))
2369 ulen
= info
.nr_line_info
;
2370 info
.nr_line_info
= prog
->aux
->nr_linfo
;
2371 if (info
.nr_line_info
&& ulen
) {
2372 __u8 __user
*user_linfo
;
2374 user_linfo
= u64_to_user_ptr(info
.line_info
);
2375 ulen
= min_t(u32
, info
.nr_line_info
, ulen
);
2376 if (copy_to_user(user_linfo
, prog
->aux
->linfo
,
2377 info
.line_info_rec_size
* ulen
))
2381 ulen
= info
.nr_jited_line_info
;
2382 if (prog
->aux
->jited_linfo
)
2383 info
.nr_jited_line_info
= prog
->aux
->nr_linfo
;
2385 info
.nr_jited_line_info
= 0;
2386 if (info
.nr_jited_line_info
&& ulen
) {
2387 if (bpf_dump_raw_ok()) {
2388 __u64 __user
*user_linfo
;
2391 user_linfo
= u64_to_user_ptr(info
.jited_line_info
);
2392 ulen
= min_t(u32
, info
.nr_jited_line_info
, ulen
);
2393 for (i
= 0; i
< ulen
; i
++) {
2394 if (put_user((__u64
)(long)prog
->aux
->jited_linfo
[i
],
2399 info
.jited_line_info
= 0;
2403 ulen
= info
.nr_prog_tags
;
2404 info
.nr_prog_tags
= prog
->aux
->func_cnt
? : 1;
2406 __u8
__user (*user_prog_tags
)[BPF_TAG_SIZE
];
2409 user_prog_tags
= u64_to_user_ptr(info
.prog_tags
);
2410 ulen
= min_t(u32
, info
.nr_prog_tags
, ulen
);
2411 if (prog
->aux
->func_cnt
) {
2412 for (i
= 0; i
< ulen
; i
++) {
2413 if (copy_to_user(user_prog_tags
[i
],
2414 prog
->aux
->func
[i
]->tag
,
2419 if (copy_to_user(user_prog_tags
[0],
2420 prog
->tag
, BPF_TAG_SIZE
))
2426 if (copy_to_user(uinfo
, &info
, info_len
) ||
2427 put_user(info_len
, &uattr
->info
.info_len
))
2433 static int bpf_map_get_info_by_fd(struct bpf_map
*map
,
2434 const union bpf_attr
*attr
,
2435 union bpf_attr __user
*uattr
)
2437 struct bpf_map_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
2438 struct bpf_map_info info
= {};
2439 u32 info_len
= attr
->info
.info_len
;
2442 err
= bpf_check_uarg_tail_zero(uinfo
, sizeof(info
), info_len
);
2445 info_len
= min_t(u32
, sizeof(info
), info_len
);
2447 info
.type
= map
->map_type
;
2449 info
.key_size
= map
->key_size
;
2450 info
.value_size
= map
->value_size
;
2451 info
.max_entries
= map
->max_entries
;
2452 info
.map_flags
= map
->map_flags
;
2453 memcpy(info
.name
, map
->name
, sizeof(map
->name
));
2456 info
.btf_id
= btf_id(map
->btf
);
2457 info
.btf_key_type_id
= map
->btf_key_type_id
;
2458 info
.btf_value_type_id
= map
->btf_value_type_id
;
2461 if (bpf_map_is_dev_bound(map
)) {
2462 err
= bpf_map_offload_info_fill(&info
, map
);
2467 if (copy_to_user(uinfo
, &info
, info_len
) ||
2468 put_user(info_len
, &uattr
->info
.info_len
))
2474 static int bpf_btf_get_info_by_fd(struct btf
*btf
,
2475 const union bpf_attr
*attr
,
2476 union bpf_attr __user
*uattr
)
2478 struct bpf_btf_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
2479 u32 info_len
= attr
->info
.info_len
;
2482 err
= bpf_check_uarg_tail_zero(uinfo
, sizeof(*uinfo
), info_len
);
2486 return btf_get_info_by_fd(btf
, attr
, uattr
);
2489 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2491 static int bpf_obj_get_info_by_fd(const union bpf_attr
*attr
,
2492 union bpf_attr __user
*uattr
)
2494 int ufd
= attr
->info
.bpf_fd
;
2498 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD
))
2505 if (f
.file
->f_op
== &bpf_prog_fops
)
2506 err
= bpf_prog_get_info_by_fd(f
.file
->private_data
, attr
,
2508 else if (f
.file
->f_op
== &bpf_map_fops
)
2509 err
= bpf_map_get_info_by_fd(f
.file
->private_data
, attr
,
2511 else if (f
.file
->f_op
== &btf_fops
)
2512 err
= bpf_btf_get_info_by_fd(f
.file
->private_data
, attr
, uattr
);
2520 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2522 static int bpf_btf_load(const union bpf_attr
*attr
)
2524 if (CHECK_ATTR(BPF_BTF_LOAD
))
2527 if (!capable(CAP_SYS_ADMIN
))
2530 return btf_new_fd(attr
);
2533 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2535 static int bpf_btf_get_fd_by_id(const union bpf_attr
*attr
)
2537 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID
))
2540 if (!capable(CAP_SYS_ADMIN
))
2543 return btf_get_fd_by_id(attr
->btf_id
);
2546 static int bpf_task_fd_query_copy(const union bpf_attr
*attr
,
2547 union bpf_attr __user
*uattr
,
2548 u32 prog_id
, u32 fd_type
,
2549 const char *buf
, u64 probe_offset
,
2552 char __user
*ubuf
= u64_to_user_ptr(attr
->task_fd_query
.buf
);
2553 u32 len
= buf
? strlen(buf
) : 0, input_len
;
2556 if (put_user(len
, &uattr
->task_fd_query
.buf_len
))
2558 input_len
= attr
->task_fd_query
.buf_len
;
2559 if (input_len
&& ubuf
) {
2561 /* nothing to copy, just make ubuf NULL terminated */
2564 if (put_user(zero
, ubuf
))
2566 } else if (input_len
>= len
+ 1) {
2567 /* ubuf can hold the string with NULL terminator */
2568 if (copy_to_user(ubuf
, buf
, len
+ 1))
2571 /* ubuf cannot hold the string with NULL terminator,
2572 * do a partial copy with NULL terminator.
2577 if (copy_to_user(ubuf
, buf
, input_len
- 1))
2579 if (put_user(zero
, ubuf
+ input_len
- 1))
2584 if (put_user(prog_id
, &uattr
->task_fd_query
.prog_id
) ||
2585 put_user(fd_type
, &uattr
->task_fd_query
.fd_type
) ||
2586 put_user(probe_offset
, &uattr
->task_fd_query
.probe_offset
) ||
2587 put_user(probe_addr
, &uattr
->task_fd_query
.probe_addr
))
2593 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2595 static int bpf_task_fd_query(const union bpf_attr
*attr
,
2596 union bpf_attr __user
*uattr
)
2598 pid_t pid
= attr
->task_fd_query
.pid
;
2599 u32 fd
= attr
->task_fd_query
.fd
;
2600 const struct perf_event
*event
;
2601 struct files_struct
*files
;
2602 struct task_struct
*task
;
2606 if (CHECK_ATTR(BPF_TASK_FD_QUERY
))
2609 if (!capable(CAP_SYS_ADMIN
))
2612 if (attr
->task_fd_query
.flags
!= 0)
2615 task
= get_pid_task(find_vpid(pid
), PIDTYPE_PID
);
2619 files
= get_files_struct(task
);
2620 put_task_struct(task
);
2625 spin_lock(&files
->file_lock
);
2626 file
= fcheck_files(files
, fd
);
2631 spin_unlock(&files
->file_lock
);
2632 put_files_struct(files
);
2637 if (file
->f_op
== &bpf_raw_tp_fops
) {
2638 struct bpf_raw_tracepoint
*raw_tp
= file
->private_data
;
2639 struct bpf_raw_event_map
*btp
= raw_tp
->btp
;
2641 err
= bpf_task_fd_query_copy(attr
, uattr
,
2642 raw_tp
->prog
->aux
->id
,
2643 BPF_FD_TYPE_RAW_TRACEPOINT
,
2644 btp
->tp
->name
, 0, 0);
2648 event
= perf_get_event(file
);
2649 if (!IS_ERR(event
)) {
2650 u64 probe_offset
, probe_addr
;
2651 u32 prog_id
, fd_type
;
2654 err
= bpf_get_perf_event_info(event
, &prog_id
, &fd_type
,
2655 &buf
, &probe_offset
,
2658 err
= bpf_task_fd_query_copy(attr
, uattr
, prog_id
,
2672 SYSCALL_DEFINE3(bpf
, int, cmd
, union bpf_attr __user
*, uattr
, unsigned int, size
)
2674 union bpf_attr attr
= {};
2677 if (sysctl_unprivileged_bpf_disabled
&& !capable(CAP_SYS_ADMIN
))
2680 err
= bpf_check_uarg_tail_zero(uattr
, sizeof(attr
), size
);
2683 size
= min_t(u32
, size
, sizeof(attr
));
2685 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2686 if (copy_from_user(&attr
, uattr
, size
) != 0)
2689 err
= security_bpf(cmd
, &attr
, size
);
2694 case BPF_MAP_CREATE
:
2695 err
= map_create(&attr
);
2697 case BPF_MAP_LOOKUP_ELEM
:
2698 err
= map_lookup_elem(&attr
);
2700 case BPF_MAP_UPDATE_ELEM
:
2701 err
= map_update_elem(&attr
);
2703 case BPF_MAP_DELETE_ELEM
:
2704 err
= map_delete_elem(&attr
);
2706 case BPF_MAP_GET_NEXT_KEY
:
2707 err
= map_get_next_key(&attr
);
2710 err
= bpf_prog_load(&attr
, uattr
);
2713 err
= bpf_obj_pin(&attr
);
2716 err
= bpf_obj_get(&attr
);
2718 case BPF_PROG_ATTACH
:
2719 err
= bpf_prog_attach(&attr
);
2721 case BPF_PROG_DETACH
:
2722 err
= bpf_prog_detach(&attr
);
2724 case BPF_PROG_QUERY
:
2725 err
= bpf_prog_query(&attr
, uattr
);
2727 case BPF_PROG_TEST_RUN
:
2728 err
= bpf_prog_test_run(&attr
, uattr
);
2730 case BPF_PROG_GET_NEXT_ID
:
2731 err
= bpf_obj_get_next_id(&attr
, uattr
,
2732 &prog_idr
, &prog_idr_lock
);
2734 case BPF_MAP_GET_NEXT_ID
:
2735 err
= bpf_obj_get_next_id(&attr
, uattr
,
2736 &map_idr
, &map_idr_lock
);
2738 case BPF_PROG_GET_FD_BY_ID
:
2739 err
= bpf_prog_get_fd_by_id(&attr
);
2741 case BPF_MAP_GET_FD_BY_ID
:
2742 err
= bpf_map_get_fd_by_id(&attr
);
2744 case BPF_OBJ_GET_INFO_BY_FD
:
2745 err
= bpf_obj_get_info_by_fd(&attr
, uattr
);
2747 case BPF_RAW_TRACEPOINT_OPEN
:
2748 err
= bpf_raw_tracepoint_open(&attr
);
2751 err
= bpf_btf_load(&attr
);
2753 case BPF_BTF_GET_FD_BY_ID
:
2754 err
= bpf_btf_get_fd_by_id(&attr
);
2756 case BPF_TASK_FD_QUERY
:
2757 err
= bpf_task_fd_query(&attr
, uattr
);
2759 case BPF_MAP_LOOKUP_AND_DELETE_ELEM
:
2760 err
= map_lookup_and_delete_elem(&attr
);