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 <linux/audit.h>
27 #include <uapi/linux/btf.h>
28 #include <linux/bpf_lsm.h>
30 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
31 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
32 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
33 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
34 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
35 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
38 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
40 DEFINE_PER_CPU(int, bpf_prog_active
);
41 static DEFINE_IDR(prog_idr
);
42 static DEFINE_SPINLOCK(prog_idr_lock
);
43 static DEFINE_IDR(map_idr
);
44 static DEFINE_SPINLOCK(map_idr_lock
);
46 int sysctl_unprivileged_bpf_disabled __read_mostly
;
48 static const struct bpf_map_ops
* const bpf_map_types
[] = {
49 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
50 #define BPF_MAP_TYPE(_id, _ops) \
52 #include <linux/bpf_types.h>
58 * If we're handed a bigger struct than we know of, ensure all the unknown bits
59 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
60 * we don't know about yet.
62 * There is a ToCToU between this function call and the following
63 * copy_from_user() call. However, this is not a concern since this function is
64 * meant to be a future-proofing of bits.
66 int bpf_check_uarg_tail_zero(void __user
*uaddr
,
70 unsigned char __user
*addr
;
71 unsigned char __user
*end
;
75 if (unlikely(actual_size
> PAGE_SIZE
)) /* silly large */
78 if (unlikely(!access_ok(uaddr
, actual_size
)))
81 if (actual_size
<= expected_size
)
84 addr
= uaddr
+ expected_size
;
85 end
= uaddr
+ actual_size
;
87 for (; addr
< end
; addr
++) {
88 err
= get_user(val
, addr
);
98 const struct bpf_map_ops bpf_map_offload_ops
= {
99 .map_alloc
= bpf_map_offload_map_alloc
,
100 .map_free
= bpf_map_offload_map_free
,
101 .map_check_btf
= map_check_no_btf
,
104 static struct bpf_map
*find_and_alloc_map(union bpf_attr
*attr
)
106 const struct bpf_map_ops
*ops
;
107 u32 type
= attr
->map_type
;
111 if (type
>= ARRAY_SIZE(bpf_map_types
))
112 return ERR_PTR(-EINVAL
);
113 type
= array_index_nospec(type
, ARRAY_SIZE(bpf_map_types
));
114 ops
= bpf_map_types
[type
];
116 return ERR_PTR(-EINVAL
);
118 if (ops
->map_alloc_check
) {
119 err
= ops
->map_alloc_check(attr
);
123 if (attr
->map_ifindex
)
124 ops
= &bpf_map_offload_ops
;
125 map
= ops
->map_alloc(attr
);
129 map
->map_type
= type
;
133 static u32
bpf_map_value_size(struct bpf_map
*map
)
135 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
136 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
137 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
||
138 map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
)
139 return round_up(map
->value_size
, 8) * num_possible_cpus();
140 else if (IS_FD_MAP(map
))
143 return map
->value_size
;
146 static void maybe_wait_bpf_programs(struct bpf_map
*map
)
148 /* Wait for any running BPF programs to complete so that
149 * userspace, when we return to it, knows that all programs
150 * that could be running use the new map value.
152 if (map
->map_type
== BPF_MAP_TYPE_HASH_OF_MAPS
||
153 map
->map_type
== BPF_MAP_TYPE_ARRAY_OF_MAPS
)
157 static int bpf_map_update_value(struct bpf_map
*map
, struct fd f
, void *key
,
158 void *value
, __u64 flags
)
162 /* Need to create a kthread, thus must support schedule */
163 if (bpf_map_is_dev_bound(map
)) {
164 return bpf_map_offload_update_elem(map
, key
, value
, flags
);
165 } else if (map
->map_type
== BPF_MAP_TYPE_CPUMAP
||
166 map
->map_type
== BPF_MAP_TYPE_SOCKHASH
||
167 map
->map_type
== BPF_MAP_TYPE_SOCKMAP
||
168 map
->map_type
== BPF_MAP_TYPE_STRUCT_OPS
) {
169 return map
->ops
->map_update_elem(map
, key
, value
, flags
);
170 } else if (IS_FD_PROG_ARRAY(map
)) {
171 return bpf_fd_array_map_update_elem(map
, f
.file
, key
, value
,
175 bpf_disable_instrumentation();
176 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
177 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
178 err
= bpf_percpu_hash_update(map
, key
, value
, flags
);
179 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
180 err
= bpf_percpu_array_update(map
, key
, value
, flags
);
181 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
) {
182 err
= bpf_percpu_cgroup_storage_update(map
, key
, value
,
184 } else if (IS_FD_ARRAY(map
)) {
186 err
= bpf_fd_array_map_update_elem(map
, f
.file
, key
, value
,
189 } else if (map
->map_type
== BPF_MAP_TYPE_HASH_OF_MAPS
) {
191 err
= bpf_fd_htab_map_update_elem(map
, f
.file
, key
, value
,
194 } else if (map
->map_type
== BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
) {
195 /* rcu_read_lock() is not needed */
196 err
= bpf_fd_reuseport_array_update_elem(map
, key
, value
,
198 } else if (map
->map_type
== BPF_MAP_TYPE_QUEUE
||
199 map
->map_type
== BPF_MAP_TYPE_STACK
) {
200 err
= map
->ops
->map_push_elem(map
, value
, flags
);
203 err
= map
->ops
->map_update_elem(map
, key
, value
, flags
);
206 bpf_enable_instrumentation();
207 maybe_wait_bpf_programs(map
);
212 static int bpf_map_copy_value(struct bpf_map
*map
, void *key
, void *value
,
218 if (bpf_map_is_dev_bound(map
))
219 return bpf_map_offload_lookup_elem(map
, key
, value
);
221 bpf_disable_instrumentation();
222 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
223 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
224 err
= bpf_percpu_hash_copy(map
, key
, value
);
225 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
226 err
= bpf_percpu_array_copy(map
, key
, value
);
227 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
) {
228 err
= bpf_percpu_cgroup_storage_copy(map
, key
, value
);
229 } else if (map
->map_type
== BPF_MAP_TYPE_STACK_TRACE
) {
230 err
= bpf_stackmap_copy(map
, key
, value
);
231 } else if (IS_FD_ARRAY(map
) || IS_FD_PROG_ARRAY(map
)) {
232 err
= bpf_fd_array_map_lookup_elem(map
, key
, value
);
233 } else if (IS_FD_HASH(map
)) {
234 err
= bpf_fd_htab_map_lookup_elem(map
, key
, value
);
235 } else if (map
->map_type
== BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
) {
236 err
= bpf_fd_reuseport_array_lookup_elem(map
, key
, value
);
237 } else if (map
->map_type
== BPF_MAP_TYPE_QUEUE
||
238 map
->map_type
== BPF_MAP_TYPE_STACK
) {
239 err
= map
->ops
->map_peek_elem(map
, value
);
240 } else if (map
->map_type
== BPF_MAP_TYPE_STRUCT_OPS
) {
241 /* struct_ops map requires directly updating "value" */
242 err
= bpf_struct_ops_map_sys_lookup_elem(map
, key
, value
);
245 if (map
->ops
->map_lookup_elem_sys_only
)
246 ptr
= map
->ops
->map_lookup_elem_sys_only(map
, key
);
248 ptr
= map
->ops
->map_lookup_elem(map
, key
);
255 if (flags
& BPF_F_LOCK
)
256 /* lock 'ptr' and copy everything but lock */
257 copy_map_value_locked(map
, value
, ptr
, true);
259 copy_map_value(map
, value
, ptr
);
260 /* mask lock, since value wasn't zero inited */
261 check_and_init_map_lock(map
, value
);
266 bpf_enable_instrumentation();
267 maybe_wait_bpf_programs(map
);
272 static void *__bpf_map_area_alloc(u64 size
, int numa_node
, bool mmapable
)
274 /* We really just want to fail instead of triggering OOM killer
275 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
276 * which is used for lower order allocation requests.
278 * It has been observed that higher order allocation requests done by
279 * vmalloc with __GFP_NORETRY being set might fail due to not trying
280 * to reclaim memory from the page cache, thus we set
281 * __GFP_RETRY_MAYFAIL to avoid such situations.
284 const gfp_t flags
= __GFP_NOWARN
| __GFP_ZERO
;
287 if (size
>= SIZE_MAX
)
290 /* kmalloc()'ed memory can't be mmap()'ed */
291 if (!mmapable
&& size
<= (PAGE_SIZE
<< PAGE_ALLOC_COSTLY_ORDER
)) {
292 area
= kmalloc_node(size
, GFP_USER
| __GFP_NORETRY
| flags
,
298 BUG_ON(!PAGE_ALIGNED(size
));
299 return vmalloc_user_node_flags(size
, numa_node
, GFP_KERNEL
|
300 __GFP_RETRY_MAYFAIL
| flags
);
302 return __vmalloc_node_flags_caller(size
, numa_node
,
303 GFP_KERNEL
| __GFP_RETRY_MAYFAIL
|
304 flags
, __builtin_return_address(0));
307 void *bpf_map_area_alloc(u64 size
, int numa_node
)
309 return __bpf_map_area_alloc(size
, numa_node
, false);
312 void *bpf_map_area_mmapable_alloc(u64 size
, int numa_node
)
314 return __bpf_map_area_alloc(size
, numa_node
, true);
317 void bpf_map_area_free(void *area
)
322 static u32
bpf_map_flags_retain_permanent(u32 flags
)
324 /* Some map creation flags are not tied to the map object but
325 * rather to the map fd instead, so they have no meaning upon
326 * map object inspection since multiple file descriptors with
327 * different (access) properties can exist here. Thus, given
328 * this has zero meaning for the map itself, lets clear these
331 return flags
& ~(BPF_F_RDONLY
| BPF_F_WRONLY
);
334 void bpf_map_init_from_attr(struct bpf_map
*map
, union bpf_attr
*attr
)
336 map
->map_type
= attr
->map_type
;
337 map
->key_size
= attr
->key_size
;
338 map
->value_size
= attr
->value_size
;
339 map
->max_entries
= attr
->max_entries
;
340 map
->map_flags
= bpf_map_flags_retain_permanent(attr
->map_flags
);
341 map
->numa_node
= bpf_map_attr_numa_node(attr
);
344 static int bpf_charge_memlock(struct user_struct
*user
, u32 pages
)
346 unsigned long memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
348 if (atomic_long_add_return(pages
, &user
->locked_vm
) > memlock_limit
) {
349 atomic_long_sub(pages
, &user
->locked_vm
);
355 static void bpf_uncharge_memlock(struct user_struct
*user
, u32 pages
)
358 atomic_long_sub(pages
, &user
->locked_vm
);
361 int bpf_map_charge_init(struct bpf_map_memory
*mem
, u64 size
)
363 u32 pages
= round_up(size
, PAGE_SIZE
) >> PAGE_SHIFT
;
364 struct user_struct
*user
;
367 if (size
>= U32_MAX
- PAGE_SIZE
)
370 user
= get_current_user();
371 ret
= bpf_charge_memlock(user
, pages
);
383 void bpf_map_charge_finish(struct bpf_map_memory
*mem
)
385 bpf_uncharge_memlock(mem
->user
, mem
->pages
);
389 void bpf_map_charge_move(struct bpf_map_memory
*dst
,
390 struct bpf_map_memory
*src
)
394 /* Make sure src will not be used for the redundant uncharging. */
395 memset(src
, 0, sizeof(struct bpf_map_memory
));
398 int bpf_map_charge_memlock(struct bpf_map
*map
, u32 pages
)
402 ret
= bpf_charge_memlock(map
->memory
.user
, pages
);
405 map
->memory
.pages
+= pages
;
409 void bpf_map_uncharge_memlock(struct bpf_map
*map
, u32 pages
)
411 bpf_uncharge_memlock(map
->memory
.user
, pages
);
412 map
->memory
.pages
-= pages
;
415 static int bpf_map_alloc_id(struct bpf_map
*map
)
419 idr_preload(GFP_KERNEL
);
420 spin_lock_bh(&map_idr_lock
);
421 id
= idr_alloc_cyclic(&map_idr
, map
, 1, INT_MAX
, GFP_ATOMIC
);
424 spin_unlock_bh(&map_idr_lock
);
427 if (WARN_ON_ONCE(!id
))
430 return id
> 0 ? 0 : id
;
433 void bpf_map_free_id(struct bpf_map
*map
, bool do_idr_lock
)
437 /* Offloaded maps are removed from the IDR store when their device
438 * disappears - even if someone holds an fd to them they are unusable,
439 * the memory is gone, all ops will fail; they are simply waiting for
440 * refcnt to drop to be freed.
446 spin_lock_irqsave(&map_idr_lock
, flags
);
448 __acquire(&map_idr_lock
);
450 idr_remove(&map_idr
, map
->id
);
454 spin_unlock_irqrestore(&map_idr_lock
, flags
);
456 __release(&map_idr_lock
);
459 /* called from workqueue */
460 static void bpf_map_free_deferred(struct work_struct
*work
)
462 struct bpf_map
*map
= container_of(work
, struct bpf_map
, work
);
463 struct bpf_map_memory mem
;
465 bpf_map_charge_move(&mem
, &map
->memory
);
466 security_bpf_map_free(map
);
467 /* implementation dependent freeing */
468 map
->ops
->map_free(map
);
469 bpf_map_charge_finish(&mem
);
472 static void bpf_map_put_uref(struct bpf_map
*map
)
474 if (atomic64_dec_and_test(&map
->usercnt
)) {
475 if (map
->ops
->map_release_uref
)
476 map
->ops
->map_release_uref(map
);
480 /* decrement map refcnt and schedule it for freeing via workqueue
481 * (unrelying map implementation ops->map_free() might sleep)
483 static void __bpf_map_put(struct bpf_map
*map
, bool do_idr_lock
)
485 if (atomic64_dec_and_test(&map
->refcnt
)) {
486 /* bpf_map_free_id() must be called first */
487 bpf_map_free_id(map
, do_idr_lock
);
489 INIT_WORK(&map
->work
, bpf_map_free_deferred
);
490 schedule_work(&map
->work
);
494 void bpf_map_put(struct bpf_map
*map
)
496 __bpf_map_put(map
, true);
498 EXPORT_SYMBOL_GPL(bpf_map_put
);
500 void bpf_map_put_with_uref(struct bpf_map
*map
)
502 bpf_map_put_uref(map
);
506 static int bpf_map_release(struct inode
*inode
, struct file
*filp
)
508 struct bpf_map
*map
= filp
->private_data
;
510 if (map
->ops
->map_release
)
511 map
->ops
->map_release(map
, filp
);
513 bpf_map_put_with_uref(map
);
517 static fmode_t
map_get_sys_perms(struct bpf_map
*map
, struct fd f
)
519 fmode_t mode
= f
.file
->f_mode
;
521 /* Our file permissions may have been overridden by global
522 * map permissions facing syscall side.
524 if (READ_ONCE(map
->frozen
))
525 mode
&= ~FMODE_CAN_WRITE
;
529 #ifdef CONFIG_PROC_FS
530 static void bpf_map_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
532 const struct bpf_map
*map
= filp
->private_data
;
533 const struct bpf_array
*array
;
534 u32 type
= 0, jited
= 0;
536 if (map
->map_type
== BPF_MAP_TYPE_PROG_ARRAY
) {
537 array
= container_of(map
, struct bpf_array
, map
);
538 type
= array
->aux
->type
;
539 jited
= array
->aux
->jited
;
556 map
->memory
.pages
* 1ULL << PAGE_SHIFT
,
558 READ_ONCE(map
->frozen
));
560 seq_printf(m
, "owner_prog_type:\t%u\n", type
);
561 seq_printf(m
, "owner_jited:\t%u\n", jited
);
566 static ssize_t
bpf_dummy_read(struct file
*filp
, char __user
*buf
, size_t siz
,
569 /* We need this handler such that alloc_file() enables
570 * f_mode with FMODE_CAN_READ.
575 static ssize_t
bpf_dummy_write(struct file
*filp
, const char __user
*buf
,
576 size_t siz
, loff_t
*ppos
)
578 /* We need this handler such that alloc_file() enables
579 * f_mode with FMODE_CAN_WRITE.
584 /* called for any extra memory-mapped regions (except initial) */
585 static void bpf_map_mmap_open(struct vm_area_struct
*vma
)
587 struct bpf_map
*map
= vma
->vm_file
->private_data
;
589 if (vma
->vm_flags
& VM_MAYWRITE
) {
590 mutex_lock(&map
->freeze_mutex
);
592 mutex_unlock(&map
->freeze_mutex
);
596 /* called for all unmapped memory region (including initial) */
597 static void bpf_map_mmap_close(struct vm_area_struct
*vma
)
599 struct bpf_map
*map
= vma
->vm_file
->private_data
;
601 if (vma
->vm_flags
& VM_MAYWRITE
) {
602 mutex_lock(&map
->freeze_mutex
);
604 mutex_unlock(&map
->freeze_mutex
);
608 static const struct vm_operations_struct bpf_map_default_vmops
= {
609 .open
= bpf_map_mmap_open
,
610 .close
= bpf_map_mmap_close
,
613 static int bpf_map_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
615 struct bpf_map
*map
= filp
->private_data
;
618 if (!map
->ops
->map_mmap
|| map_value_has_spin_lock(map
))
621 if (!(vma
->vm_flags
& VM_SHARED
))
624 mutex_lock(&map
->freeze_mutex
);
626 if ((vma
->vm_flags
& VM_WRITE
) && map
->frozen
) {
631 /* set default open/close callbacks */
632 vma
->vm_ops
= &bpf_map_default_vmops
;
633 vma
->vm_private_data
= map
;
634 vma
->vm_flags
&= ~VM_MAYEXEC
;
635 if (!(vma
->vm_flags
& VM_WRITE
))
636 /* disallow re-mapping with PROT_WRITE */
637 vma
->vm_flags
&= ~VM_MAYWRITE
;
639 err
= map
->ops
->map_mmap(map
, vma
);
643 if (vma
->vm_flags
& VM_MAYWRITE
)
646 mutex_unlock(&map
->freeze_mutex
);
650 const struct file_operations bpf_map_fops
= {
651 #ifdef CONFIG_PROC_FS
652 .show_fdinfo
= bpf_map_show_fdinfo
,
654 .release
= bpf_map_release
,
655 .read
= bpf_dummy_read
,
656 .write
= bpf_dummy_write
,
657 .mmap
= bpf_map_mmap
,
660 int bpf_map_new_fd(struct bpf_map
*map
, int flags
)
664 ret
= security_bpf_map(map
, OPEN_FMODE(flags
));
668 return anon_inode_getfd("bpf-map", &bpf_map_fops
, map
,
672 int bpf_get_file_flag(int flags
)
674 if ((flags
& BPF_F_RDONLY
) && (flags
& BPF_F_WRONLY
))
676 if (flags
& BPF_F_RDONLY
)
678 if (flags
& BPF_F_WRONLY
)
683 /* helper macro to check that unused fields 'union bpf_attr' are zero */
684 #define CHECK_ATTR(CMD) \
685 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
686 sizeof(attr->CMD##_LAST_FIELD), 0, \
688 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
689 sizeof(attr->CMD##_LAST_FIELD)) != NULL
691 /* dst and src must have at least "size" number of bytes.
692 * Return strlen on success and < 0 on error.
694 int bpf_obj_name_cpy(char *dst
, const char *src
, unsigned int size
)
696 const char *end
= src
+ size
;
697 const char *orig_src
= src
;
699 memset(dst
, 0, size
);
700 /* Copy all isalnum(), '_' and '.' chars. */
701 while (src
< end
&& *src
) {
702 if (!isalnum(*src
) &&
703 *src
!= '_' && *src
!= '.')
708 /* No '\0' found in "size" number of bytes */
712 return src
- orig_src
;
715 int map_check_no_btf(const struct bpf_map
*map
,
716 const struct btf
*btf
,
717 const struct btf_type
*key_type
,
718 const struct btf_type
*value_type
)
723 static int map_check_btf(struct bpf_map
*map
, const struct btf
*btf
,
724 u32 btf_key_id
, u32 btf_value_id
)
726 const struct btf_type
*key_type
, *value_type
;
727 u32 key_size
, value_size
;
730 /* Some maps allow key to be unspecified. */
732 key_type
= btf_type_id_size(btf
, &btf_key_id
, &key_size
);
733 if (!key_type
|| key_size
!= map
->key_size
)
736 key_type
= btf_type_by_id(btf
, 0);
737 if (!map
->ops
->map_check_btf
)
741 value_type
= btf_type_id_size(btf
, &btf_value_id
, &value_size
);
742 if (!value_type
|| value_size
!= map
->value_size
)
745 map
->spin_lock_off
= btf_find_spin_lock(btf
, value_type
);
747 if (map_value_has_spin_lock(map
)) {
748 if (map
->map_flags
& BPF_F_RDONLY_PROG
)
750 if (map
->map_type
!= BPF_MAP_TYPE_HASH
&&
751 map
->map_type
!= BPF_MAP_TYPE_ARRAY
&&
752 map
->map_type
!= BPF_MAP_TYPE_CGROUP_STORAGE
&&
753 map
->map_type
!= BPF_MAP_TYPE_SK_STORAGE
)
755 if (map
->spin_lock_off
+ sizeof(struct bpf_spin_lock
) >
758 "verifier bug spin_lock_off %d value_size %d\n",
759 map
->spin_lock_off
, map
->value_size
);
764 if (map
->ops
->map_check_btf
)
765 ret
= map
->ops
->map_check_btf(map
, btf
, key_type
, value_type
);
770 #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
771 /* called via syscall */
772 static int map_create(union bpf_attr
*attr
)
774 int numa_node
= bpf_map_attr_numa_node(attr
);
775 struct bpf_map_memory mem
;
780 err
= CHECK_ATTR(BPF_MAP_CREATE
);
784 if (attr
->btf_vmlinux_value_type_id
) {
785 if (attr
->map_type
!= BPF_MAP_TYPE_STRUCT_OPS
||
786 attr
->btf_key_type_id
|| attr
->btf_value_type_id
)
788 } else if (attr
->btf_key_type_id
&& !attr
->btf_value_type_id
) {
792 f_flags
= bpf_get_file_flag(attr
->map_flags
);
796 if (numa_node
!= NUMA_NO_NODE
&&
797 ((unsigned int)numa_node
>= nr_node_ids
||
798 !node_online(numa_node
)))
801 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
802 map
= find_and_alloc_map(attr
);
806 err
= bpf_obj_name_cpy(map
->name
, attr
->map_name
,
807 sizeof(attr
->map_name
));
811 atomic64_set(&map
->refcnt
, 1);
812 atomic64_set(&map
->usercnt
, 1);
813 mutex_init(&map
->freeze_mutex
);
815 map
->spin_lock_off
= -EINVAL
;
816 if (attr
->btf_key_type_id
|| attr
->btf_value_type_id
||
817 /* Even the map's value is a kernel's struct,
818 * the bpf_prog.o must have BTF to begin with
819 * to figure out the corresponding kernel's
820 * counter part. Thus, attr->btf_fd has
823 attr
->btf_vmlinux_value_type_id
) {
826 btf
= btf_get_by_fd(attr
->btf_fd
);
833 if (attr
->btf_value_type_id
) {
834 err
= map_check_btf(map
, btf
, attr
->btf_key_type_id
,
835 attr
->btf_value_type_id
);
840 map
->btf_key_type_id
= attr
->btf_key_type_id
;
841 map
->btf_value_type_id
= attr
->btf_value_type_id
;
842 map
->btf_vmlinux_value_type_id
=
843 attr
->btf_vmlinux_value_type_id
;
846 err
= security_bpf_map_alloc(map
);
850 err
= bpf_map_alloc_id(map
);
854 err
= bpf_map_new_fd(map
, f_flags
);
856 /* failed to allocate fd.
857 * bpf_map_put_with_uref() is needed because the above
858 * bpf_map_alloc_id() has published the map
859 * to the userspace and the userspace may
860 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
862 bpf_map_put_with_uref(map
);
869 security_bpf_map_free(map
);
872 bpf_map_charge_move(&mem
, &map
->memory
);
873 map
->ops
->map_free(map
);
874 bpf_map_charge_finish(&mem
);
878 /* if error is returned, fd is released.
879 * On success caller should complete fd access with matching fdput()
881 struct bpf_map
*__bpf_map_get(struct fd f
)
884 return ERR_PTR(-EBADF
);
885 if (f
.file
->f_op
!= &bpf_map_fops
) {
887 return ERR_PTR(-EINVAL
);
890 return f
.file
->private_data
;
893 void bpf_map_inc(struct bpf_map
*map
)
895 atomic64_inc(&map
->refcnt
);
897 EXPORT_SYMBOL_GPL(bpf_map_inc
);
899 void bpf_map_inc_with_uref(struct bpf_map
*map
)
901 atomic64_inc(&map
->refcnt
);
902 atomic64_inc(&map
->usercnt
);
904 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref
);
906 struct bpf_map
*bpf_map_get(u32 ufd
)
908 struct fd f
= fdget(ufd
);
911 map
= __bpf_map_get(f
);
921 struct bpf_map
*bpf_map_get_with_uref(u32 ufd
)
923 struct fd f
= fdget(ufd
);
926 map
= __bpf_map_get(f
);
930 bpf_map_inc_with_uref(map
);
936 /* map_idr_lock should have been held */
937 static struct bpf_map
*__bpf_map_inc_not_zero(struct bpf_map
*map
, bool uref
)
941 refold
= atomic64_fetch_add_unless(&map
->refcnt
, 1, 0);
943 return ERR_PTR(-ENOENT
);
945 atomic64_inc(&map
->usercnt
);
950 struct bpf_map
*bpf_map_inc_not_zero(struct bpf_map
*map
)
952 spin_lock_bh(&map_idr_lock
);
953 map
= __bpf_map_inc_not_zero(map
, false);
954 spin_unlock_bh(&map_idr_lock
);
958 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero
);
960 int __weak
bpf_stackmap_copy(struct bpf_map
*map
, void *key
, void *value
)
965 static void *__bpf_copy_key(void __user
*ukey
, u64 key_size
)
968 return memdup_user(ukey
, key_size
);
971 return ERR_PTR(-EINVAL
);
976 /* last field in 'union bpf_attr' used by this command */
977 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
979 static int map_lookup_elem(union bpf_attr
*attr
)
981 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
982 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
983 int ufd
= attr
->map_fd
;
990 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM
))
993 if (attr
->flags
& ~BPF_F_LOCK
)
997 map
= __bpf_map_get(f
);
1000 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_READ
)) {
1005 if ((attr
->flags
& BPF_F_LOCK
) &&
1006 !map_value_has_spin_lock(map
)) {
1011 key
= __bpf_copy_key(ukey
, map
->key_size
);
1017 value_size
= bpf_map_value_size(map
);
1020 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
1024 err
= bpf_map_copy_value(map
, key
, value
, attr
->flags
);
1029 if (copy_to_user(uvalue
, value
, value_size
) != 0)
1044 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1046 static int map_update_elem(union bpf_attr
*attr
)
1048 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
1049 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
1050 int ufd
= attr
->map_fd
;
1051 struct bpf_map
*map
;
1057 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM
))
1061 map
= __bpf_map_get(f
);
1063 return PTR_ERR(map
);
1064 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_WRITE
)) {
1069 if ((attr
->flags
& BPF_F_LOCK
) &&
1070 !map_value_has_spin_lock(map
)) {
1075 key
= __bpf_copy_key(ukey
, map
->key_size
);
1081 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
1082 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
1083 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
||
1084 map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
)
1085 value_size
= round_up(map
->value_size
, 8) * num_possible_cpus();
1087 value_size
= map
->value_size
;
1090 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
1095 if (copy_from_user(value
, uvalue
, value_size
) != 0)
1098 err
= bpf_map_update_value(map
, f
, key
, value
, attr
->flags
);
1109 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1111 static int map_delete_elem(union bpf_attr
*attr
)
1113 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
1114 int ufd
= attr
->map_fd
;
1115 struct bpf_map
*map
;
1120 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM
))
1124 map
= __bpf_map_get(f
);
1126 return PTR_ERR(map
);
1127 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_WRITE
)) {
1132 key
= __bpf_copy_key(ukey
, map
->key_size
);
1138 if (bpf_map_is_dev_bound(map
)) {
1139 err
= bpf_map_offload_delete_elem(map
, key
);
1141 } else if (IS_FD_PROG_ARRAY(map
) ||
1142 map
->map_type
== BPF_MAP_TYPE_STRUCT_OPS
) {
1143 /* These maps require sleepable context */
1144 err
= map
->ops
->map_delete_elem(map
, key
);
1148 bpf_disable_instrumentation();
1150 err
= map
->ops
->map_delete_elem(map
, key
);
1152 bpf_enable_instrumentation();
1153 maybe_wait_bpf_programs(map
);
1161 /* last field in 'union bpf_attr' used by this command */
1162 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1164 static int map_get_next_key(union bpf_attr
*attr
)
1166 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
1167 void __user
*unext_key
= u64_to_user_ptr(attr
->next_key
);
1168 int ufd
= attr
->map_fd
;
1169 struct bpf_map
*map
;
1170 void *key
, *next_key
;
1174 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY
))
1178 map
= __bpf_map_get(f
);
1180 return PTR_ERR(map
);
1181 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_READ
)) {
1187 key
= __bpf_copy_key(ukey
, map
->key_size
);
1197 next_key
= kmalloc(map
->key_size
, GFP_USER
);
1201 if (bpf_map_is_dev_bound(map
)) {
1202 err
= bpf_map_offload_get_next_key(map
, key
, next_key
);
1207 err
= map
->ops
->map_get_next_key(map
, key
, next_key
);
1214 if (copy_to_user(unext_key
, next_key
, map
->key_size
) != 0)
1228 int generic_map_delete_batch(struct bpf_map
*map
,
1229 const union bpf_attr
*attr
,
1230 union bpf_attr __user
*uattr
)
1232 void __user
*keys
= u64_to_user_ptr(attr
->batch
.keys
);
1237 if (attr
->batch
.elem_flags
& ~BPF_F_LOCK
)
1240 if ((attr
->batch
.elem_flags
& BPF_F_LOCK
) &&
1241 !map_value_has_spin_lock(map
)) {
1245 max_count
= attr
->batch
.count
;
1249 key
= kmalloc(map
->key_size
, GFP_USER
| __GFP_NOWARN
);
1253 for (cp
= 0; cp
< max_count
; cp
++) {
1255 if (copy_from_user(key
, keys
+ cp
* map
->key_size
,
1259 if (bpf_map_is_dev_bound(map
)) {
1260 err
= bpf_map_offload_delete_elem(map
, key
);
1264 bpf_disable_instrumentation();
1266 err
= map
->ops
->map_delete_elem(map
, key
);
1268 bpf_enable_instrumentation();
1269 maybe_wait_bpf_programs(map
);
1273 if (copy_to_user(&uattr
->batch
.count
, &cp
, sizeof(cp
)))
1280 int generic_map_update_batch(struct bpf_map
*map
,
1281 const union bpf_attr
*attr
,
1282 union bpf_attr __user
*uattr
)
1284 void __user
*values
= u64_to_user_ptr(attr
->batch
.values
);
1285 void __user
*keys
= u64_to_user_ptr(attr
->batch
.keys
);
1286 u32 value_size
, cp
, max_count
;
1287 int ufd
= attr
->map_fd
;
1293 if (attr
->batch
.elem_flags
& ~BPF_F_LOCK
)
1296 if ((attr
->batch
.elem_flags
& BPF_F_LOCK
) &&
1297 !map_value_has_spin_lock(map
)) {
1301 value_size
= bpf_map_value_size(map
);
1303 max_count
= attr
->batch
.count
;
1307 key
= kmalloc(map
->key_size
, GFP_USER
| __GFP_NOWARN
);
1311 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
1317 for (cp
= 0; cp
< max_count
; cp
++) {
1319 if (copy_from_user(key
, keys
+ cp
* map
->key_size
,
1321 copy_from_user(value
, values
+ cp
* value_size
, value_size
))
1324 err
= bpf_map_update_value(map
, f
, key
, value
,
1325 attr
->batch
.elem_flags
);
1331 if (copy_to_user(&uattr
->batch
.count
, &cp
, sizeof(cp
)))
1339 #define MAP_LOOKUP_RETRIES 3
1341 int generic_map_lookup_batch(struct bpf_map
*map
,
1342 const union bpf_attr
*attr
,
1343 union bpf_attr __user
*uattr
)
1345 void __user
*uobatch
= u64_to_user_ptr(attr
->batch
.out_batch
);
1346 void __user
*ubatch
= u64_to_user_ptr(attr
->batch
.in_batch
);
1347 void __user
*values
= u64_to_user_ptr(attr
->batch
.values
);
1348 void __user
*keys
= u64_to_user_ptr(attr
->batch
.keys
);
1349 void *buf
, *buf_prevkey
, *prev_key
, *key
, *value
;
1350 int err
, retry
= MAP_LOOKUP_RETRIES
;
1351 u32 value_size
, cp
, max_count
;
1353 if (attr
->batch
.elem_flags
& ~BPF_F_LOCK
)
1356 if ((attr
->batch
.elem_flags
& BPF_F_LOCK
) &&
1357 !map_value_has_spin_lock(map
))
1360 value_size
= bpf_map_value_size(map
);
1362 max_count
= attr
->batch
.count
;
1366 if (put_user(0, &uattr
->batch
.count
))
1369 buf_prevkey
= kmalloc(map
->key_size
, GFP_USER
| __GFP_NOWARN
);
1373 buf
= kmalloc(map
->key_size
+ value_size
, GFP_USER
| __GFP_NOWARN
);
1375 kvfree(buf_prevkey
);
1381 if (ubatch
&& copy_from_user(buf_prevkey
, ubatch
, map
->key_size
))
1384 value
= key
+ map
->key_size
;
1386 prev_key
= buf_prevkey
;
1388 for (cp
= 0; cp
< max_count
;) {
1390 err
= map
->ops
->map_get_next_key(map
, prev_key
, key
);
1394 err
= bpf_map_copy_value(map
, key
, value
,
1395 attr
->batch
.elem_flags
);
1397 if (err
== -ENOENT
) {
1409 if (copy_to_user(keys
+ cp
* map
->key_size
, key
,
1414 if (copy_to_user(values
+ cp
* value_size
, value
, value_size
)) {
1420 prev_key
= buf_prevkey
;
1422 swap(prev_key
, key
);
1423 retry
= MAP_LOOKUP_RETRIES
;
1430 if ((copy_to_user(&uattr
->batch
.count
, &cp
, sizeof(cp
)) ||
1431 (cp
&& copy_to_user(uobatch
, prev_key
, map
->key_size
))))
1440 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1442 static int map_lookup_and_delete_elem(union bpf_attr
*attr
)
1444 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
1445 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
1446 int ufd
= attr
->map_fd
;
1447 struct bpf_map
*map
;
1453 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM
))
1457 map
= __bpf_map_get(f
);
1459 return PTR_ERR(map
);
1460 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_WRITE
)) {
1465 key
= __bpf_copy_key(ukey
, map
->key_size
);
1471 value_size
= map
->value_size
;
1474 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
1478 if (map
->map_type
== BPF_MAP_TYPE_QUEUE
||
1479 map
->map_type
== BPF_MAP_TYPE_STACK
) {
1480 err
= map
->ops
->map_pop_elem(map
, value
);
1488 if (copy_to_user(uvalue
, value
, value_size
) != 0)
1502 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1504 static int map_freeze(const union bpf_attr
*attr
)
1506 int err
= 0, ufd
= attr
->map_fd
;
1507 struct bpf_map
*map
;
1510 if (CHECK_ATTR(BPF_MAP_FREEZE
))
1514 map
= __bpf_map_get(f
);
1516 return PTR_ERR(map
);
1518 if (map
->map_type
== BPF_MAP_TYPE_STRUCT_OPS
) {
1523 mutex_lock(&map
->freeze_mutex
);
1525 if (map
->writecnt
) {
1529 if (READ_ONCE(map
->frozen
)) {
1533 if (!capable(CAP_SYS_ADMIN
)) {
1538 WRITE_ONCE(map
->frozen
, true);
1540 mutex_unlock(&map
->freeze_mutex
);
1545 static const struct bpf_prog_ops
* const bpf_prog_types
[] = {
1546 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1547 [_id] = & _name ## _prog_ops,
1548 #define BPF_MAP_TYPE(_id, _ops)
1549 #include <linux/bpf_types.h>
1550 #undef BPF_PROG_TYPE
1554 static int find_prog_type(enum bpf_prog_type type
, struct bpf_prog
*prog
)
1556 const struct bpf_prog_ops
*ops
;
1558 if (type
>= ARRAY_SIZE(bpf_prog_types
))
1560 type
= array_index_nospec(type
, ARRAY_SIZE(bpf_prog_types
));
1561 ops
= bpf_prog_types
[type
];
1565 if (!bpf_prog_is_dev_bound(prog
->aux
))
1566 prog
->aux
->ops
= ops
;
1568 prog
->aux
->ops
= &bpf_offload_prog_ops
;
1579 static const char * const bpf_audit_str
[BPF_AUDIT_MAX
] = {
1580 [BPF_AUDIT_LOAD
] = "LOAD",
1581 [BPF_AUDIT_UNLOAD
] = "UNLOAD",
1584 static void bpf_audit_prog(const struct bpf_prog
*prog
, unsigned int op
)
1586 struct audit_context
*ctx
= NULL
;
1587 struct audit_buffer
*ab
;
1589 if (WARN_ON_ONCE(op
>= BPF_AUDIT_MAX
))
1591 if (audit_enabled
== AUDIT_OFF
)
1593 if (op
== BPF_AUDIT_LOAD
)
1594 ctx
= audit_context();
1595 ab
= audit_log_start(ctx
, GFP_ATOMIC
, AUDIT_BPF
);
1598 audit_log_format(ab
, "prog-id=%u op=%s",
1599 prog
->aux
->id
, bpf_audit_str
[op
]);
1603 int __bpf_prog_charge(struct user_struct
*user
, u32 pages
)
1605 unsigned long memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
1606 unsigned long user_bufs
;
1609 user_bufs
= atomic_long_add_return(pages
, &user
->locked_vm
);
1610 if (user_bufs
> memlock_limit
) {
1611 atomic_long_sub(pages
, &user
->locked_vm
);
1619 void __bpf_prog_uncharge(struct user_struct
*user
, u32 pages
)
1622 atomic_long_sub(pages
, &user
->locked_vm
);
1625 static int bpf_prog_charge_memlock(struct bpf_prog
*prog
)
1627 struct user_struct
*user
= get_current_user();
1630 ret
= __bpf_prog_charge(user
, prog
->pages
);
1636 prog
->aux
->user
= user
;
1640 static void bpf_prog_uncharge_memlock(struct bpf_prog
*prog
)
1642 struct user_struct
*user
= prog
->aux
->user
;
1644 __bpf_prog_uncharge(user
, prog
->pages
);
1648 static int bpf_prog_alloc_id(struct bpf_prog
*prog
)
1652 idr_preload(GFP_KERNEL
);
1653 spin_lock_bh(&prog_idr_lock
);
1654 id
= idr_alloc_cyclic(&prog_idr
, prog
, 1, INT_MAX
, GFP_ATOMIC
);
1657 spin_unlock_bh(&prog_idr_lock
);
1660 /* id is in [1, INT_MAX) */
1661 if (WARN_ON_ONCE(!id
))
1664 return id
> 0 ? 0 : id
;
1667 void bpf_prog_free_id(struct bpf_prog
*prog
, bool do_idr_lock
)
1669 /* cBPF to eBPF migrations are currently not in the idr store.
1670 * Offloaded programs are removed from the store when their device
1671 * disappears - even if someone grabs an fd to them they are unusable,
1672 * simply waiting for refcnt to drop to be freed.
1678 spin_lock_bh(&prog_idr_lock
);
1680 __acquire(&prog_idr_lock
);
1682 idr_remove(&prog_idr
, prog
->aux
->id
);
1686 spin_unlock_bh(&prog_idr_lock
);
1688 __release(&prog_idr_lock
);
1691 static void __bpf_prog_put_rcu(struct rcu_head
*rcu
)
1693 struct bpf_prog_aux
*aux
= container_of(rcu
, struct bpf_prog_aux
, rcu
);
1695 kvfree(aux
->func_info
);
1696 kfree(aux
->func_info_aux
);
1697 bpf_prog_uncharge_memlock(aux
->prog
);
1698 security_bpf_prog_free(aux
);
1699 bpf_prog_free(aux
->prog
);
1702 static void __bpf_prog_put_noref(struct bpf_prog
*prog
, bool deferred
)
1704 bpf_prog_kallsyms_del_all(prog
);
1705 btf_put(prog
->aux
->btf
);
1706 bpf_prog_free_linfo(prog
);
1709 call_rcu(&prog
->aux
->rcu
, __bpf_prog_put_rcu
);
1711 __bpf_prog_put_rcu(&prog
->aux
->rcu
);
1714 static void __bpf_prog_put(struct bpf_prog
*prog
, bool do_idr_lock
)
1716 if (atomic64_dec_and_test(&prog
->aux
->refcnt
)) {
1717 perf_event_bpf_event(prog
, PERF_BPF_EVENT_PROG_UNLOAD
, 0);
1718 bpf_audit_prog(prog
, BPF_AUDIT_UNLOAD
);
1719 /* bpf_prog_free_id() must be called first */
1720 bpf_prog_free_id(prog
, do_idr_lock
);
1721 __bpf_prog_put_noref(prog
, true);
1725 void bpf_prog_put(struct bpf_prog
*prog
)
1727 __bpf_prog_put(prog
, true);
1729 EXPORT_SYMBOL_GPL(bpf_prog_put
);
1731 static int bpf_prog_release(struct inode
*inode
, struct file
*filp
)
1733 struct bpf_prog
*prog
= filp
->private_data
;
1739 static void bpf_prog_get_stats(const struct bpf_prog
*prog
,
1740 struct bpf_prog_stats
*stats
)
1742 u64 nsecs
= 0, cnt
= 0;
1745 for_each_possible_cpu(cpu
) {
1746 const struct bpf_prog_stats
*st
;
1750 st
= per_cpu_ptr(prog
->aux
->stats
, cpu
);
1752 start
= u64_stats_fetch_begin_irq(&st
->syncp
);
1755 } while (u64_stats_fetch_retry_irq(&st
->syncp
, start
));
1759 stats
->nsecs
= nsecs
;
1763 #ifdef CONFIG_PROC_FS
1764 static void bpf_prog_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
1766 const struct bpf_prog
*prog
= filp
->private_data
;
1767 char prog_tag
[sizeof(prog
->tag
) * 2 + 1] = { };
1768 struct bpf_prog_stats stats
;
1770 bpf_prog_get_stats(prog
, &stats
);
1771 bin2hex(prog_tag
, prog
->tag
, sizeof(prog
->tag
));
1778 "run_time_ns:\t%llu\n"
1783 prog
->pages
* 1ULL << PAGE_SHIFT
,
1790 const struct file_operations bpf_prog_fops
= {
1791 #ifdef CONFIG_PROC_FS
1792 .show_fdinfo
= bpf_prog_show_fdinfo
,
1794 .release
= bpf_prog_release
,
1795 .read
= bpf_dummy_read
,
1796 .write
= bpf_dummy_write
,
1799 int bpf_prog_new_fd(struct bpf_prog
*prog
)
1803 ret
= security_bpf_prog(prog
);
1807 return anon_inode_getfd("bpf-prog", &bpf_prog_fops
, prog
,
1808 O_RDWR
| O_CLOEXEC
);
1811 static struct bpf_prog
*____bpf_prog_get(struct fd f
)
1814 return ERR_PTR(-EBADF
);
1815 if (f
.file
->f_op
!= &bpf_prog_fops
) {
1817 return ERR_PTR(-EINVAL
);
1820 return f
.file
->private_data
;
1823 void bpf_prog_add(struct bpf_prog
*prog
, int i
)
1825 atomic64_add(i
, &prog
->aux
->refcnt
);
1827 EXPORT_SYMBOL_GPL(bpf_prog_add
);
1829 void bpf_prog_sub(struct bpf_prog
*prog
, int i
)
1831 /* Only to be used for undoing previous bpf_prog_add() in some
1832 * error path. We still know that another entity in our call
1833 * path holds a reference to the program, thus atomic_sub() can
1834 * be safely used in such cases!
1836 WARN_ON(atomic64_sub_return(i
, &prog
->aux
->refcnt
) == 0);
1838 EXPORT_SYMBOL_GPL(bpf_prog_sub
);
1840 void bpf_prog_inc(struct bpf_prog
*prog
)
1842 atomic64_inc(&prog
->aux
->refcnt
);
1844 EXPORT_SYMBOL_GPL(bpf_prog_inc
);
1846 /* prog_idr_lock should have been held */
1847 struct bpf_prog
*bpf_prog_inc_not_zero(struct bpf_prog
*prog
)
1851 refold
= atomic64_fetch_add_unless(&prog
->aux
->refcnt
, 1, 0);
1854 return ERR_PTR(-ENOENT
);
1858 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero
);
1860 bool bpf_prog_get_ok(struct bpf_prog
*prog
,
1861 enum bpf_prog_type
*attach_type
, bool attach_drv
)
1863 /* not an attachment, just a refcount inc, always allow */
1867 if (prog
->type
!= *attach_type
)
1869 if (bpf_prog_is_dev_bound(prog
->aux
) && !attach_drv
)
1875 static struct bpf_prog
*__bpf_prog_get(u32 ufd
, enum bpf_prog_type
*attach_type
,
1878 struct fd f
= fdget(ufd
);
1879 struct bpf_prog
*prog
;
1881 prog
= ____bpf_prog_get(f
);
1884 if (!bpf_prog_get_ok(prog
, attach_type
, attach_drv
)) {
1885 prog
= ERR_PTR(-EINVAL
);
1895 struct bpf_prog
*bpf_prog_get(u32 ufd
)
1897 return __bpf_prog_get(ufd
, NULL
, false);
1900 struct bpf_prog
*bpf_prog_get_type_dev(u32 ufd
, enum bpf_prog_type type
,
1903 return __bpf_prog_get(ufd
, &type
, attach_drv
);
1905 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev
);
1907 /* Initially all BPF programs could be loaded w/o specifying
1908 * expected_attach_type. Later for some of them specifying expected_attach_type
1909 * at load time became required so that program could be validated properly.
1910 * Programs of types that are allowed to be loaded both w/ and w/o (for
1911 * backward compatibility) expected_attach_type, should have the default attach
1912 * type assigned to expected_attach_type for the latter case, so that it can be
1913 * validated later at attach time.
1915 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1916 * prog type requires it but has some attach types that have to be backward
1919 static void bpf_prog_load_fixup_attach_type(union bpf_attr
*attr
)
1921 switch (attr
->prog_type
) {
1922 case BPF_PROG_TYPE_CGROUP_SOCK
:
1923 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1924 * exist so checking for non-zero is the way to go here.
1926 if (!attr
->expected_attach_type
)
1927 attr
->expected_attach_type
=
1928 BPF_CGROUP_INET_SOCK_CREATE
;
1934 bpf_prog_load_check_attach(enum bpf_prog_type prog_type
,
1935 enum bpf_attach_type expected_attach_type
,
1936 u32 btf_id
, u32 prog_fd
)
1939 if (btf_id
> BTF_MAX_TYPE
)
1942 switch (prog_type
) {
1943 case BPF_PROG_TYPE_TRACING
:
1944 case BPF_PROG_TYPE_LSM
:
1945 case BPF_PROG_TYPE_STRUCT_OPS
:
1946 case BPF_PROG_TYPE_EXT
:
1953 if (prog_fd
&& prog_type
!= BPF_PROG_TYPE_TRACING
&&
1954 prog_type
!= BPF_PROG_TYPE_EXT
)
1957 switch (prog_type
) {
1958 case BPF_PROG_TYPE_CGROUP_SOCK
:
1959 switch (expected_attach_type
) {
1960 case BPF_CGROUP_INET_SOCK_CREATE
:
1961 case BPF_CGROUP_INET4_POST_BIND
:
1962 case BPF_CGROUP_INET6_POST_BIND
:
1967 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR
:
1968 switch (expected_attach_type
) {
1969 case BPF_CGROUP_INET4_BIND
:
1970 case BPF_CGROUP_INET6_BIND
:
1971 case BPF_CGROUP_INET4_CONNECT
:
1972 case BPF_CGROUP_INET6_CONNECT
:
1973 case BPF_CGROUP_UDP4_SENDMSG
:
1974 case BPF_CGROUP_UDP6_SENDMSG
:
1975 case BPF_CGROUP_UDP4_RECVMSG
:
1976 case BPF_CGROUP_UDP6_RECVMSG
:
1981 case BPF_PROG_TYPE_CGROUP_SKB
:
1982 switch (expected_attach_type
) {
1983 case BPF_CGROUP_INET_INGRESS
:
1984 case BPF_CGROUP_INET_EGRESS
:
1989 case BPF_PROG_TYPE_CGROUP_SOCKOPT
:
1990 switch (expected_attach_type
) {
1991 case BPF_CGROUP_SETSOCKOPT
:
1992 case BPF_CGROUP_GETSOCKOPT
:
1997 case BPF_PROG_TYPE_EXT
:
1998 if (expected_attach_type
)
2006 /* last field in 'union bpf_attr' used by this command */
2007 #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
2009 static int bpf_prog_load(union bpf_attr
*attr
, union bpf_attr __user
*uattr
)
2011 enum bpf_prog_type type
= attr
->prog_type
;
2012 struct bpf_prog
*prog
;
2017 if (CHECK_ATTR(BPF_PROG_LOAD
))
2020 if (attr
->prog_flags
& ~(BPF_F_STRICT_ALIGNMENT
|
2021 BPF_F_ANY_ALIGNMENT
|
2022 BPF_F_TEST_STATE_FREQ
|
2023 BPF_F_TEST_RND_HI32
))
2026 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
) &&
2027 (attr
->prog_flags
& BPF_F_ANY_ALIGNMENT
) &&
2028 !capable(CAP_SYS_ADMIN
))
2031 /* copy eBPF program license from user space */
2032 if (strncpy_from_user(license
, u64_to_user_ptr(attr
->license
),
2033 sizeof(license
) - 1) < 0)
2035 license
[sizeof(license
) - 1] = 0;
2037 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2038 is_gpl
= license_is_gpl_compatible(license
);
2040 if (attr
->insn_cnt
== 0 ||
2041 attr
->insn_cnt
> (capable(CAP_SYS_ADMIN
) ? BPF_COMPLEXITY_LIMIT_INSNS
: BPF_MAXINSNS
))
2043 if (type
!= BPF_PROG_TYPE_SOCKET_FILTER
&&
2044 type
!= BPF_PROG_TYPE_CGROUP_SKB
&&
2045 !capable(CAP_SYS_ADMIN
))
2048 bpf_prog_load_fixup_attach_type(attr
);
2049 if (bpf_prog_load_check_attach(type
, attr
->expected_attach_type
,
2050 attr
->attach_btf_id
,
2051 attr
->attach_prog_fd
))
2054 /* plain bpf_prog allocation */
2055 prog
= bpf_prog_alloc(bpf_prog_size(attr
->insn_cnt
), GFP_USER
);
2059 prog
->expected_attach_type
= attr
->expected_attach_type
;
2060 prog
->aux
->attach_btf_id
= attr
->attach_btf_id
;
2061 if (attr
->attach_prog_fd
) {
2062 struct bpf_prog
*tgt_prog
;
2064 tgt_prog
= bpf_prog_get(attr
->attach_prog_fd
);
2065 if (IS_ERR(tgt_prog
)) {
2066 err
= PTR_ERR(tgt_prog
);
2067 goto free_prog_nouncharge
;
2069 prog
->aux
->linked_prog
= tgt_prog
;
2072 prog
->aux
->offload_requested
= !!attr
->prog_ifindex
;
2074 err
= security_bpf_prog_alloc(prog
->aux
);
2076 goto free_prog_nouncharge
;
2078 err
= bpf_prog_charge_memlock(prog
);
2082 prog
->len
= attr
->insn_cnt
;
2085 if (copy_from_user(prog
->insns
, u64_to_user_ptr(attr
->insns
),
2086 bpf_prog_insn_size(prog
)) != 0)
2089 prog
->orig_prog
= NULL
;
2092 atomic64_set(&prog
->aux
->refcnt
, 1);
2093 prog
->gpl_compatible
= is_gpl
? 1 : 0;
2095 if (bpf_prog_is_dev_bound(prog
->aux
)) {
2096 err
= bpf_prog_offload_init(prog
, attr
);
2101 /* find program type: socket_filter vs tracing_filter */
2102 err
= find_prog_type(type
, prog
);
2106 prog
->aux
->load_time
= ktime_get_boottime_ns();
2107 err
= bpf_obj_name_cpy(prog
->aux
->name
, attr
->prog_name
,
2108 sizeof(attr
->prog_name
));
2112 /* run eBPF verifier */
2113 err
= bpf_check(&prog
, attr
, uattr
);
2115 goto free_used_maps
;
2117 prog
= bpf_prog_select_runtime(prog
, &err
);
2119 goto free_used_maps
;
2121 err
= bpf_prog_alloc_id(prog
);
2123 goto free_used_maps
;
2125 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2126 * effectively publicly exposed. However, retrieving via
2127 * bpf_prog_get_fd_by_id() will take another reference,
2128 * therefore it cannot be gone underneath us.
2130 * Only for the time /after/ successful bpf_prog_new_fd()
2131 * and before returning to userspace, we might just hold
2132 * one reference and any parallel close on that fd could
2133 * rip everything out. Hence, below notifications must
2134 * happen before bpf_prog_new_fd().
2136 * Also, any failure handling from this point onwards must
2137 * be using bpf_prog_put() given the program is exposed.
2139 bpf_prog_kallsyms_add(prog
);
2140 perf_event_bpf_event(prog
, PERF_BPF_EVENT_PROG_LOAD
, 0);
2141 bpf_audit_prog(prog
, BPF_AUDIT_LOAD
);
2143 err
= bpf_prog_new_fd(prog
);
2149 /* In case we have subprogs, we need to wait for a grace
2150 * period before we can tear down JIT memory since symbols
2151 * are already exposed under kallsyms.
2153 __bpf_prog_put_noref(prog
, prog
->aux
->func_cnt
);
2156 bpf_prog_uncharge_memlock(prog
);
2158 security_bpf_prog_free(prog
->aux
);
2159 free_prog_nouncharge
:
2160 bpf_prog_free(prog
);
2164 #define BPF_OBJ_LAST_FIELD file_flags
2166 static int bpf_obj_pin(const union bpf_attr
*attr
)
2168 if (CHECK_ATTR(BPF_OBJ
) || attr
->file_flags
!= 0)
2171 return bpf_obj_pin_user(attr
->bpf_fd
, u64_to_user_ptr(attr
->pathname
));
2174 static int bpf_obj_get(const union bpf_attr
*attr
)
2176 if (CHECK_ATTR(BPF_OBJ
) || attr
->bpf_fd
!= 0 ||
2177 attr
->file_flags
& ~BPF_OBJ_FLAG_MASK
)
2180 return bpf_obj_get_user(u64_to_user_ptr(attr
->pathname
),
2184 void bpf_link_init(struct bpf_link
*link
, const struct bpf_link_ops
*ops
,
2185 struct bpf_prog
*prog
)
2187 atomic64_set(&link
->refcnt
, 1);
2192 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2193 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2194 * anon_inode's release() call. This helper manages marking bpf_link as
2195 * defunct, releases anon_inode file and puts reserved FD.
2197 void bpf_link_cleanup(struct bpf_link
*link
, struct file
*link_file
,
2202 put_unused_fd(link_fd
);
2205 void bpf_link_inc(struct bpf_link
*link
)
2207 atomic64_inc(&link
->refcnt
);
2210 /* bpf_link_free is guaranteed to be called from process context */
2211 static void bpf_link_free(struct bpf_link
*link
)
2214 /* detach BPF program, clean up used resources */
2215 link
->ops
->release(link
);
2216 bpf_prog_put(link
->prog
);
2218 /* free bpf_link and its containing memory */
2219 link
->ops
->dealloc(link
);
2222 static void bpf_link_put_deferred(struct work_struct
*work
)
2224 struct bpf_link
*link
= container_of(work
, struct bpf_link
, work
);
2226 bpf_link_free(link
);
2229 /* bpf_link_put can be called from atomic context, but ensures that resources
2230 * are freed from process context
2232 void bpf_link_put(struct bpf_link
*link
)
2234 if (!atomic64_dec_and_test(&link
->refcnt
))
2238 INIT_WORK(&link
->work
, bpf_link_put_deferred
);
2239 schedule_work(&link
->work
);
2241 bpf_link_free(link
);
2245 static int bpf_link_release(struct inode
*inode
, struct file
*filp
)
2247 struct bpf_link
*link
= filp
->private_data
;
2253 #ifdef CONFIG_PROC_FS
2254 static const struct bpf_link_ops bpf_raw_tp_lops
;
2255 static const struct bpf_link_ops bpf_tracing_link_lops
;
2257 static void bpf_link_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
2259 const struct bpf_link
*link
= filp
->private_data
;
2260 const struct bpf_prog
*prog
= link
->prog
;
2261 char prog_tag
[sizeof(prog
->tag
) * 2 + 1] = { };
2262 const char *link_type
;
2264 if (link
->ops
== &bpf_raw_tp_lops
)
2265 link_type
= "raw_tracepoint";
2266 else if (link
->ops
== &bpf_tracing_link_lops
)
2267 link_type
= "tracing";
2268 #ifdef CONFIG_CGROUP_BPF
2269 else if (link
->ops
== &bpf_cgroup_link_lops
)
2270 link_type
= "cgroup";
2273 link_type
= "unknown";
2275 bin2hex(prog_tag
, prog
->tag
, sizeof(prog
->tag
));
2286 const struct file_operations bpf_link_fops
= {
2287 #ifdef CONFIG_PROC_FS
2288 .show_fdinfo
= bpf_link_show_fdinfo
,
2290 .release
= bpf_link_release
,
2291 .read
= bpf_dummy_read
,
2292 .write
= bpf_dummy_write
,
2295 int bpf_link_new_fd(struct bpf_link
*link
)
2297 return anon_inode_getfd("bpf-link", &bpf_link_fops
, link
, O_CLOEXEC
);
2300 /* Similar to bpf_link_new_fd, create anon_inode for given bpf_link, but
2301 * instead of immediately installing fd in fdtable, just reserve it and
2302 * return. Caller then need to either install it with fd_install(fd, file) or
2303 * release with put_unused_fd(fd).
2304 * This is useful for cases when bpf_link attachment/detachment are
2305 * complicated and expensive operations and should be delayed until all the fd
2306 * reservation and anon_inode creation succeeds.
2308 struct file
*bpf_link_new_file(struct bpf_link
*link
, int *reserved_fd
)
2313 fd
= get_unused_fd_flags(O_CLOEXEC
);
2317 file
= anon_inode_getfile("bpf_link", &bpf_link_fops
, link
, O_CLOEXEC
);
2327 struct bpf_link
*bpf_link_get_from_fd(u32 ufd
)
2329 struct fd f
= fdget(ufd
);
2330 struct bpf_link
*link
;
2333 return ERR_PTR(-EBADF
);
2334 if (f
.file
->f_op
!= &bpf_link_fops
) {
2336 return ERR_PTR(-EINVAL
);
2339 link
= f
.file
->private_data
;
2346 struct bpf_tracing_link
{
2347 struct bpf_link link
;
2350 static void bpf_tracing_link_release(struct bpf_link
*link
)
2352 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link
->prog
));
2355 static void bpf_tracing_link_dealloc(struct bpf_link
*link
)
2357 struct bpf_tracing_link
*tr_link
=
2358 container_of(link
, struct bpf_tracing_link
, link
);
2363 static const struct bpf_link_ops bpf_tracing_link_lops
= {
2364 .release
= bpf_tracing_link_release
,
2365 .dealloc
= bpf_tracing_link_dealloc
,
2368 static int bpf_tracing_prog_attach(struct bpf_prog
*prog
)
2370 struct bpf_tracing_link
*link
;
2371 struct file
*link_file
;
2374 switch (prog
->type
) {
2375 case BPF_PROG_TYPE_TRACING
:
2376 if (prog
->expected_attach_type
!= BPF_TRACE_FENTRY
&&
2377 prog
->expected_attach_type
!= BPF_TRACE_FEXIT
&&
2378 prog
->expected_attach_type
!= BPF_MODIFY_RETURN
) {
2383 case BPF_PROG_TYPE_EXT
:
2384 if (prog
->expected_attach_type
!= 0) {
2389 case BPF_PROG_TYPE_LSM
:
2390 if (prog
->expected_attach_type
!= BPF_LSM_MAC
) {
2400 link
= kzalloc(sizeof(*link
), GFP_USER
);
2405 bpf_link_init(&link
->link
, &bpf_tracing_link_lops
, prog
);
2407 link_file
= bpf_link_new_file(&link
->link
, &link_fd
);
2408 if (IS_ERR(link_file
)) {
2410 err
= PTR_ERR(link_file
);
2414 err
= bpf_trampoline_link_prog(prog
);
2416 bpf_link_cleanup(&link
->link
, link_file
, link_fd
);
2420 fd_install(link_fd
, link_file
);
2428 struct bpf_raw_tp_link
{
2429 struct bpf_link link
;
2430 struct bpf_raw_event_map
*btp
;
2433 static void bpf_raw_tp_link_release(struct bpf_link
*link
)
2435 struct bpf_raw_tp_link
*raw_tp
=
2436 container_of(link
, struct bpf_raw_tp_link
, link
);
2438 bpf_probe_unregister(raw_tp
->btp
, raw_tp
->link
.prog
);
2439 bpf_put_raw_tracepoint(raw_tp
->btp
);
2442 static void bpf_raw_tp_link_dealloc(struct bpf_link
*link
)
2444 struct bpf_raw_tp_link
*raw_tp
=
2445 container_of(link
, struct bpf_raw_tp_link
, link
);
2450 static const struct bpf_link_ops bpf_raw_tp_lops
= {
2451 .release
= bpf_raw_tp_link_release
,
2452 .dealloc
= bpf_raw_tp_link_dealloc
,
2455 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2457 static int bpf_raw_tracepoint_open(const union bpf_attr
*attr
)
2459 struct bpf_raw_tp_link
*link
;
2460 struct bpf_raw_event_map
*btp
;
2461 struct file
*link_file
;
2462 struct bpf_prog
*prog
;
2463 const char *tp_name
;
2467 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN
))
2470 prog
= bpf_prog_get(attr
->raw_tracepoint
.prog_fd
);
2472 return PTR_ERR(prog
);
2474 switch (prog
->type
) {
2475 case BPF_PROG_TYPE_TRACING
:
2476 case BPF_PROG_TYPE_EXT
:
2477 case BPF_PROG_TYPE_LSM
:
2478 if (attr
->raw_tracepoint
.name
) {
2479 /* The attach point for this category of programs
2480 * should be specified via btf_id during program load.
2485 if (prog
->type
== BPF_PROG_TYPE_TRACING
&&
2486 prog
->expected_attach_type
== BPF_TRACE_RAW_TP
) {
2487 tp_name
= prog
->aux
->attach_func_name
;
2490 return bpf_tracing_prog_attach(prog
);
2491 case BPF_PROG_TYPE_RAW_TRACEPOINT
:
2492 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE
:
2493 if (strncpy_from_user(buf
,
2494 u64_to_user_ptr(attr
->raw_tracepoint
.name
),
2495 sizeof(buf
) - 1) < 0) {
2499 buf
[sizeof(buf
) - 1] = 0;
2507 btp
= bpf_get_raw_tracepoint(tp_name
);
2513 link
= kzalloc(sizeof(*link
), GFP_USER
);
2518 bpf_link_init(&link
->link
, &bpf_raw_tp_lops
, prog
);
2521 link_file
= bpf_link_new_file(&link
->link
, &link_fd
);
2522 if (IS_ERR(link_file
)) {
2524 err
= PTR_ERR(link_file
);
2528 err
= bpf_probe_register(link
->btp
, prog
);
2530 bpf_link_cleanup(&link
->link
, link_file
, link_fd
);
2534 fd_install(link_fd
, link_file
);
2538 bpf_put_raw_tracepoint(btp
);
2544 static int bpf_prog_attach_check_attach_type(const struct bpf_prog
*prog
,
2545 enum bpf_attach_type attach_type
)
2547 switch (prog
->type
) {
2548 case BPF_PROG_TYPE_CGROUP_SOCK
:
2549 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR
:
2550 case BPF_PROG_TYPE_CGROUP_SOCKOPT
:
2551 return attach_type
== prog
->expected_attach_type
? 0 : -EINVAL
;
2552 case BPF_PROG_TYPE_CGROUP_SKB
:
2553 return prog
->enforce_expected_attach_type
&&
2554 prog
->expected_attach_type
!= attach_type
?
2561 static enum bpf_prog_type
2562 attach_type_to_prog_type(enum bpf_attach_type attach_type
)
2564 switch (attach_type
) {
2565 case BPF_CGROUP_INET_INGRESS
:
2566 case BPF_CGROUP_INET_EGRESS
:
2567 return BPF_PROG_TYPE_CGROUP_SKB
;
2569 case BPF_CGROUP_INET_SOCK_CREATE
:
2570 case BPF_CGROUP_INET4_POST_BIND
:
2571 case BPF_CGROUP_INET6_POST_BIND
:
2572 return BPF_PROG_TYPE_CGROUP_SOCK
;
2573 case BPF_CGROUP_INET4_BIND
:
2574 case BPF_CGROUP_INET6_BIND
:
2575 case BPF_CGROUP_INET4_CONNECT
:
2576 case BPF_CGROUP_INET6_CONNECT
:
2577 case BPF_CGROUP_UDP4_SENDMSG
:
2578 case BPF_CGROUP_UDP6_SENDMSG
:
2579 case BPF_CGROUP_UDP4_RECVMSG
:
2580 case BPF_CGROUP_UDP6_RECVMSG
:
2581 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR
;
2582 case BPF_CGROUP_SOCK_OPS
:
2583 return BPF_PROG_TYPE_SOCK_OPS
;
2584 case BPF_CGROUP_DEVICE
:
2585 return BPF_PROG_TYPE_CGROUP_DEVICE
;
2586 case BPF_SK_MSG_VERDICT
:
2587 return BPF_PROG_TYPE_SK_MSG
;
2588 case BPF_SK_SKB_STREAM_PARSER
:
2589 case BPF_SK_SKB_STREAM_VERDICT
:
2590 return BPF_PROG_TYPE_SK_SKB
;
2591 case BPF_LIRC_MODE2
:
2592 return BPF_PROG_TYPE_LIRC_MODE2
;
2593 case BPF_FLOW_DISSECTOR
:
2594 return BPF_PROG_TYPE_FLOW_DISSECTOR
;
2595 case BPF_CGROUP_SYSCTL
:
2596 return BPF_PROG_TYPE_CGROUP_SYSCTL
;
2597 case BPF_CGROUP_GETSOCKOPT
:
2598 case BPF_CGROUP_SETSOCKOPT
:
2599 return BPF_PROG_TYPE_CGROUP_SOCKOPT
;
2601 return BPF_PROG_TYPE_UNSPEC
;
2605 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
2607 #define BPF_F_ATTACH_MASK \
2608 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
2610 static int bpf_prog_attach(const union bpf_attr
*attr
)
2612 enum bpf_prog_type ptype
;
2613 struct bpf_prog
*prog
;
2616 if (!capable(CAP_NET_ADMIN
))
2619 if (CHECK_ATTR(BPF_PROG_ATTACH
))
2622 if (attr
->attach_flags
& ~BPF_F_ATTACH_MASK
)
2625 ptype
= attach_type_to_prog_type(attr
->attach_type
);
2626 if (ptype
== BPF_PROG_TYPE_UNSPEC
)
2629 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
, ptype
);
2631 return PTR_ERR(prog
);
2633 if (bpf_prog_attach_check_attach_type(prog
, attr
->attach_type
)) {
2639 case BPF_PROG_TYPE_SK_SKB
:
2640 case BPF_PROG_TYPE_SK_MSG
:
2641 ret
= sock_map_get_from_fd(attr
, prog
);
2643 case BPF_PROG_TYPE_LIRC_MODE2
:
2644 ret
= lirc_prog_attach(attr
, prog
);
2646 case BPF_PROG_TYPE_FLOW_DISSECTOR
:
2647 ret
= skb_flow_dissector_bpf_prog_attach(attr
, prog
);
2649 case BPF_PROG_TYPE_CGROUP_DEVICE
:
2650 case BPF_PROG_TYPE_CGROUP_SKB
:
2651 case BPF_PROG_TYPE_CGROUP_SOCK
:
2652 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR
:
2653 case BPF_PROG_TYPE_CGROUP_SOCKOPT
:
2654 case BPF_PROG_TYPE_CGROUP_SYSCTL
:
2655 case BPF_PROG_TYPE_SOCK_OPS
:
2656 ret
= cgroup_bpf_prog_attach(attr
, ptype
, prog
);
2667 #define BPF_PROG_DETACH_LAST_FIELD attach_type
2669 static int bpf_prog_detach(const union bpf_attr
*attr
)
2671 enum bpf_prog_type ptype
;
2673 if (!capable(CAP_NET_ADMIN
))
2676 if (CHECK_ATTR(BPF_PROG_DETACH
))
2679 ptype
= attach_type_to_prog_type(attr
->attach_type
);
2682 case BPF_PROG_TYPE_SK_MSG
:
2683 case BPF_PROG_TYPE_SK_SKB
:
2684 return sock_map_get_from_fd(attr
, NULL
);
2685 case BPF_PROG_TYPE_LIRC_MODE2
:
2686 return lirc_prog_detach(attr
);
2687 case BPF_PROG_TYPE_FLOW_DISSECTOR
:
2688 return skb_flow_dissector_bpf_prog_detach(attr
);
2689 case BPF_PROG_TYPE_CGROUP_DEVICE
:
2690 case BPF_PROG_TYPE_CGROUP_SKB
:
2691 case BPF_PROG_TYPE_CGROUP_SOCK
:
2692 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR
:
2693 case BPF_PROG_TYPE_CGROUP_SOCKOPT
:
2694 case BPF_PROG_TYPE_CGROUP_SYSCTL
:
2695 case BPF_PROG_TYPE_SOCK_OPS
:
2696 return cgroup_bpf_prog_detach(attr
, ptype
);
2702 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2704 static int bpf_prog_query(const union bpf_attr
*attr
,
2705 union bpf_attr __user
*uattr
)
2707 if (!capable(CAP_NET_ADMIN
))
2709 if (CHECK_ATTR(BPF_PROG_QUERY
))
2711 if (attr
->query
.query_flags
& ~BPF_F_QUERY_EFFECTIVE
)
2714 switch (attr
->query
.attach_type
) {
2715 case BPF_CGROUP_INET_INGRESS
:
2716 case BPF_CGROUP_INET_EGRESS
:
2717 case BPF_CGROUP_INET_SOCK_CREATE
:
2718 case BPF_CGROUP_INET4_BIND
:
2719 case BPF_CGROUP_INET6_BIND
:
2720 case BPF_CGROUP_INET4_POST_BIND
:
2721 case BPF_CGROUP_INET6_POST_BIND
:
2722 case BPF_CGROUP_INET4_CONNECT
:
2723 case BPF_CGROUP_INET6_CONNECT
:
2724 case BPF_CGROUP_UDP4_SENDMSG
:
2725 case BPF_CGROUP_UDP6_SENDMSG
:
2726 case BPF_CGROUP_UDP4_RECVMSG
:
2727 case BPF_CGROUP_UDP6_RECVMSG
:
2728 case BPF_CGROUP_SOCK_OPS
:
2729 case BPF_CGROUP_DEVICE
:
2730 case BPF_CGROUP_SYSCTL
:
2731 case BPF_CGROUP_GETSOCKOPT
:
2732 case BPF_CGROUP_SETSOCKOPT
:
2733 return cgroup_bpf_prog_query(attr
, uattr
);
2734 case BPF_LIRC_MODE2
:
2735 return lirc_prog_query(attr
, uattr
);
2736 case BPF_FLOW_DISSECTOR
:
2737 return skb_flow_dissector_prog_query(attr
, uattr
);
2743 #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
2745 static int bpf_prog_test_run(const union bpf_attr
*attr
,
2746 union bpf_attr __user
*uattr
)
2748 struct bpf_prog
*prog
;
2749 int ret
= -ENOTSUPP
;
2751 if (!capable(CAP_SYS_ADMIN
))
2753 if (CHECK_ATTR(BPF_PROG_TEST_RUN
))
2756 if ((attr
->test
.ctx_size_in
&& !attr
->test
.ctx_in
) ||
2757 (!attr
->test
.ctx_size_in
&& attr
->test
.ctx_in
))
2760 if ((attr
->test
.ctx_size_out
&& !attr
->test
.ctx_out
) ||
2761 (!attr
->test
.ctx_size_out
&& attr
->test
.ctx_out
))
2764 prog
= bpf_prog_get(attr
->test
.prog_fd
);
2766 return PTR_ERR(prog
);
2768 if (prog
->aux
->ops
->test_run
)
2769 ret
= prog
->aux
->ops
->test_run(prog
, attr
, uattr
);
2775 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2777 static int bpf_obj_get_next_id(const union bpf_attr
*attr
,
2778 union bpf_attr __user
*uattr
,
2782 u32 next_id
= attr
->start_id
;
2785 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID
) || next_id
>= INT_MAX
)
2788 if (!capable(CAP_SYS_ADMIN
))
2793 if (!idr_get_next(idr
, &next_id
))
2795 spin_unlock_bh(lock
);
2798 err
= put_user(next_id
, &uattr
->next_id
);
2803 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2805 struct bpf_prog
*bpf_prog_by_id(u32 id
)
2807 struct bpf_prog
*prog
;
2810 return ERR_PTR(-ENOENT
);
2812 spin_lock_bh(&prog_idr_lock
);
2813 prog
= idr_find(&prog_idr
, id
);
2815 prog
= bpf_prog_inc_not_zero(prog
);
2817 prog
= ERR_PTR(-ENOENT
);
2818 spin_unlock_bh(&prog_idr_lock
);
2822 static int bpf_prog_get_fd_by_id(const union bpf_attr
*attr
)
2824 struct bpf_prog
*prog
;
2825 u32 id
= attr
->prog_id
;
2828 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID
))
2831 if (!capable(CAP_SYS_ADMIN
))
2834 prog
= bpf_prog_by_id(id
);
2836 return PTR_ERR(prog
);
2838 fd
= bpf_prog_new_fd(prog
);
2845 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2847 static int bpf_map_get_fd_by_id(const union bpf_attr
*attr
)
2849 struct bpf_map
*map
;
2850 u32 id
= attr
->map_id
;
2854 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID
) ||
2855 attr
->open_flags
& ~BPF_OBJ_FLAG_MASK
)
2858 if (!capable(CAP_SYS_ADMIN
))
2861 f_flags
= bpf_get_file_flag(attr
->open_flags
);
2865 spin_lock_bh(&map_idr_lock
);
2866 map
= idr_find(&map_idr
, id
);
2868 map
= __bpf_map_inc_not_zero(map
, true);
2870 map
= ERR_PTR(-ENOENT
);
2871 spin_unlock_bh(&map_idr_lock
);
2874 return PTR_ERR(map
);
2876 fd
= bpf_map_new_fd(map
, f_flags
);
2878 bpf_map_put_with_uref(map
);
2883 static const struct bpf_map
*bpf_map_from_imm(const struct bpf_prog
*prog
,
2884 unsigned long addr
, u32
*off
,
2887 const struct bpf_map
*map
;
2890 for (i
= 0, *off
= 0; i
< prog
->aux
->used_map_cnt
; i
++) {
2891 map
= prog
->aux
->used_maps
[i
];
2892 if (map
== (void *)addr
) {
2893 *type
= BPF_PSEUDO_MAP_FD
;
2896 if (!map
->ops
->map_direct_value_meta
)
2898 if (!map
->ops
->map_direct_value_meta(map
, addr
, off
)) {
2899 *type
= BPF_PSEUDO_MAP_VALUE
;
2907 static struct bpf_insn
*bpf_insn_prepare_dump(const struct bpf_prog
*prog
)
2909 const struct bpf_map
*map
;
2910 struct bpf_insn
*insns
;
2915 insns
= kmemdup(prog
->insnsi
, bpf_prog_insn_size(prog
),
2920 for (i
= 0; i
< prog
->len
; i
++) {
2921 if (insns
[i
].code
== (BPF_JMP
| BPF_TAIL_CALL
)) {
2922 insns
[i
].code
= BPF_JMP
| BPF_CALL
;
2923 insns
[i
].imm
= BPF_FUNC_tail_call
;
2926 if (insns
[i
].code
== (BPF_JMP
| BPF_CALL
) ||
2927 insns
[i
].code
== (BPF_JMP
| BPF_CALL_ARGS
)) {
2928 if (insns
[i
].code
== (BPF_JMP
| BPF_CALL_ARGS
))
2929 insns
[i
].code
= BPF_JMP
| BPF_CALL
;
2930 if (!bpf_dump_raw_ok())
2935 if (insns
[i
].code
!= (BPF_LD
| BPF_IMM
| BPF_DW
))
2938 imm
= ((u64
)insns
[i
+ 1].imm
<< 32) | (u32
)insns
[i
].imm
;
2939 map
= bpf_map_from_imm(prog
, imm
, &off
, &type
);
2941 insns
[i
].src_reg
= type
;
2942 insns
[i
].imm
= map
->id
;
2943 insns
[i
+ 1].imm
= off
;
2951 static int set_info_rec_size(struct bpf_prog_info
*info
)
2954 * Ensure info.*_rec_size is the same as kernel expected size
2958 * Only allow zero *_rec_size if both _rec_size and _cnt are
2959 * zero. In this case, the kernel will set the expected
2960 * _rec_size back to the info.
2963 if ((info
->nr_func_info
|| info
->func_info_rec_size
) &&
2964 info
->func_info_rec_size
!= sizeof(struct bpf_func_info
))
2967 if ((info
->nr_line_info
|| info
->line_info_rec_size
) &&
2968 info
->line_info_rec_size
!= sizeof(struct bpf_line_info
))
2971 if ((info
->nr_jited_line_info
|| info
->jited_line_info_rec_size
) &&
2972 info
->jited_line_info_rec_size
!= sizeof(__u64
))
2975 info
->func_info_rec_size
= sizeof(struct bpf_func_info
);
2976 info
->line_info_rec_size
= sizeof(struct bpf_line_info
);
2977 info
->jited_line_info_rec_size
= sizeof(__u64
);
2982 static int bpf_prog_get_info_by_fd(struct bpf_prog
*prog
,
2983 const union bpf_attr
*attr
,
2984 union bpf_attr __user
*uattr
)
2986 struct bpf_prog_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
2987 struct bpf_prog_info info
;
2988 u32 info_len
= attr
->info
.info_len
;
2989 struct bpf_prog_stats stats
;
2990 char __user
*uinsns
;
2994 err
= bpf_check_uarg_tail_zero(uinfo
, sizeof(info
), info_len
);
2997 info_len
= min_t(u32
, sizeof(info
), info_len
);
2999 memset(&info
, 0, sizeof(info
));
3000 if (copy_from_user(&info
, uinfo
, info_len
))
3003 info
.type
= prog
->type
;
3004 info
.id
= prog
->aux
->id
;
3005 info
.load_time
= prog
->aux
->load_time
;
3006 info
.created_by_uid
= from_kuid_munged(current_user_ns(),
3007 prog
->aux
->user
->uid
);
3008 info
.gpl_compatible
= prog
->gpl_compatible
;
3010 memcpy(info
.tag
, prog
->tag
, sizeof(prog
->tag
));
3011 memcpy(info
.name
, prog
->aux
->name
, sizeof(prog
->aux
->name
));
3013 ulen
= info
.nr_map_ids
;
3014 info
.nr_map_ids
= prog
->aux
->used_map_cnt
;
3015 ulen
= min_t(u32
, info
.nr_map_ids
, ulen
);
3017 u32 __user
*user_map_ids
= u64_to_user_ptr(info
.map_ids
);
3020 for (i
= 0; i
< ulen
; i
++)
3021 if (put_user(prog
->aux
->used_maps
[i
]->id
,
3026 err
= set_info_rec_size(&info
);
3030 bpf_prog_get_stats(prog
, &stats
);
3031 info
.run_time_ns
= stats
.nsecs
;
3032 info
.run_cnt
= stats
.cnt
;
3034 if (!capable(CAP_SYS_ADMIN
)) {
3035 info
.jited_prog_len
= 0;
3036 info
.xlated_prog_len
= 0;
3037 info
.nr_jited_ksyms
= 0;
3038 info
.nr_jited_func_lens
= 0;
3039 info
.nr_func_info
= 0;
3040 info
.nr_line_info
= 0;
3041 info
.nr_jited_line_info
= 0;
3045 ulen
= info
.xlated_prog_len
;
3046 info
.xlated_prog_len
= bpf_prog_insn_size(prog
);
3047 if (info
.xlated_prog_len
&& ulen
) {
3048 struct bpf_insn
*insns_sanitized
;
3051 if (prog
->blinded
&& !bpf_dump_raw_ok()) {
3052 info
.xlated_prog_insns
= 0;
3055 insns_sanitized
= bpf_insn_prepare_dump(prog
);
3056 if (!insns_sanitized
)
3058 uinsns
= u64_to_user_ptr(info
.xlated_prog_insns
);
3059 ulen
= min_t(u32
, info
.xlated_prog_len
, ulen
);
3060 fault
= copy_to_user(uinsns
, insns_sanitized
, ulen
);
3061 kfree(insns_sanitized
);
3066 if (bpf_prog_is_dev_bound(prog
->aux
)) {
3067 err
= bpf_prog_offload_info_fill(&info
, prog
);
3073 /* NOTE: the following code is supposed to be skipped for offload.
3074 * bpf_prog_offload_info_fill() is the place to fill similar fields
3077 ulen
= info
.jited_prog_len
;
3078 if (prog
->aux
->func_cnt
) {
3081 info
.jited_prog_len
= 0;
3082 for (i
= 0; i
< prog
->aux
->func_cnt
; i
++)
3083 info
.jited_prog_len
+= prog
->aux
->func
[i
]->jited_len
;
3085 info
.jited_prog_len
= prog
->jited_len
;
3088 if (info
.jited_prog_len
&& ulen
) {
3089 if (bpf_dump_raw_ok()) {
3090 uinsns
= u64_to_user_ptr(info
.jited_prog_insns
);
3091 ulen
= min_t(u32
, info
.jited_prog_len
, ulen
);
3093 /* for multi-function programs, copy the JITed
3094 * instructions for all the functions
3096 if (prog
->aux
->func_cnt
) {
3101 for (i
= 0; i
< prog
->aux
->func_cnt
; i
++) {
3102 len
= prog
->aux
->func
[i
]->jited_len
;
3103 len
= min_t(u32
, len
, free
);
3104 img
= (u8
*) prog
->aux
->func
[i
]->bpf_func
;
3105 if (copy_to_user(uinsns
, img
, len
))
3113 if (copy_to_user(uinsns
, prog
->bpf_func
, ulen
))
3117 info
.jited_prog_insns
= 0;
3121 ulen
= info
.nr_jited_ksyms
;
3122 info
.nr_jited_ksyms
= prog
->aux
->func_cnt
? : 1;
3124 if (bpf_dump_raw_ok()) {
3125 unsigned long ksym_addr
;
3126 u64 __user
*user_ksyms
;
3129 /* copy the address of the kernel symbol
3130 * corresponding to each function
3132 ulen
= min_t(u32
, info
.nr_jited_ksyms
, ulen
);
3133 user_ksyms
= u64_to_user_ptr(info
.jited_ksyms
);
3134 if (prog
->aux
->func_cnt
) {
3135 for (i
= 0; i
< ulen
; i
++) {
3136 ksym_addr
= (unsigned long)
3137 prog
->aux
->func
[i
]->bpf_func
;
3138 if (put_user((u64
) ksym_addr
,
3143 ksym_addr
= (unsigned long) prog
->bpf_func
;
3144 if (put_user((u64
) ksym_addr
, &user_ksyms
[0]))
3148 info
.jited_ksyms
= 0;
3152 ulen
= info
.nr_jited_func_lens
;
3153 info
.nr_jited_func_lens
= prog
->aux
->func_cnt
? : 1;
3155 if (bpf_dump_raw_ok()) {
3156 u32 __user
*user_lens
;
3159 /* copy the JITed image lengths for each function */
3160 ulen
= min_t(u32
, info
.nr_jited_func_lens
, ulen
);
3161 user_lens
= u64_to_user_ptr(info
.jited_func_lens
);
3162 if (prog
->aux
->func_cnt
) {
3163 for (i
= 0; i
< ulen
; i
++) {
3165 prog
->aux
->func
[i
]->jited_len
;
3166 if (put_user(func_len
, &user_lens
[i
]))
3170 func_len
= prog
->jited_len
;
3171 if (put_user(func_len
, &user_lens
[0]))
3175 info
.jited_func_lens
= 0;
3180 info
.btf_id
= btf_id(prog
->aux
->btf
);
3182 ulen
= info
.nr_func_info
;
3183 info
.nr_func_info
= prog
->aux
->func_info_cnt
;
3184 if (info
.nr_func_info
&& ulen
) {
3185 char __user
*user_finfo
;
3187 user_finfo
= u64_to_user_ptr(info
.func_info
);
3188 ulen
= min_t(u32
, info
.nr_func_info
, ulen
);
3189 if (copy_to_user(user_finfo
, prog
->aux
->func_info
,
3190 info
.func_info_rec_size
* ulen
))
3194 ulen
= info
.nr_line_info
;
3195 info
.nr_line_info
= prog
->aux
->nr_linfo
;
3196 if (info
.nr_line_info
&& ulen
) {
3197 __u8 __user
*user_linfo
;
3199 user_linfo
= u64_to_user_ptr(info
.line_info
);
3200 ulen
= min_t(u32
, info
.nr_line_info
, ulen
);
3201 if (copy_to_user(user_linfo
, prog
->aux
->linfo
,
3202 info
.line_info_rec_size
* ulen
))
3206 ulen
= info
.nr_jited_line_info
;
3207 if (prog
->aux
->jited_linfo
)
3208 info
.nr_jited_line_info
= prog
->aux
->nr_linfo
;
3210 info
.nr_jited_line_info
= 0;
3211 if (info
.nr_jited_line_info
&& ulen
) {
3212 if (bpf_dump_raw_ok()) {
3213 __u64 __user
*user_linfo
;
3216 user_linfo
= u64_to_user_ptr(info
.jited_line_info
);
3217 ulen
= min_t(u32
, info
.nr_jited_line_info
, ulen
);
3218 for (i
= 0; i
< ulen
; i
++) {
3219 if (put_user((__u64
)(long)prog
->aux
->jited_linfo
[i
],
3224 info
.jited_line_info
= 0;
3228 ulen
= info
.nr_prog_tags
;
3229 info
.nr_prog_tags
= prog
->aux
->func_cnt
? : 1;
3231 __u8
__user (*user_prog_tags
)[BPF_TAG_SIZE
];
3234 user_prog_tags
= u64_to_user_ptr(info
.prog_tags
);
3235 ulen
= min_t(u32
, info
.nr_prog_tags
, ulen
);
3236 if (prog
->aux
->func_cnt
) {
3237 for (i
= 0; i
< ulen
; i
++) {
3238 if (copy_to_user(user_prog_tags
[i
],
3239 prog
->aux
->func
[i
]->tag
,
3244 if (copy_to_user(user_prog_tags
[0],
3245 prog
->tag
, BPF_TAG_SIZE
))
3251 if (copy_to_user(uinfo
, &info
, info_len
) ||
3252 put_user(info_len
, &uattr
->info
.info_len
))
3258 static int bpf_map_get_info_by_fd(struct bpf_map
*map
,
3259 const union bpf_attr
*attr
,
3260 union bpf_attr __user
*uattr
)
3262 struct bpf_map_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
3263 struct bpf_map_info info
;
3264 u32 info_len
= attr
->info
.info_len
;
3267 err
= bpf_check_uarg_tail_zero(uinfo
, sizeof(info
), info_len
);
3270 info_len
= min_t(u32
, sizeof(info
), info_len
);
3272 memset(&info
, 0, sizeof(info
));
3273 info
.type
= map
->map_type
;
3275 info
.key_size
= map
->key_size
;
3276 info
.value_size
= map
->value_size
;
3277 info
.max_entries
= map
->max_entries
;
3278 info
.map_flags
= map
->map_flags
;
3279 memcpy(info
.name
, map
->name
, sizeof(map
->name
));
3282 info
.btf_id
= btf_id(map
->btf
);
3283 info
.btf_key_type_id
= map
->btf_key_type_id
;
3284 info
.btf_value_type_id
= map
->btf_value_type_id
;
3286 info
.btf_vmlinux_value_type_id
= map
->btf_vmlinux_value_type_id
;
3288 if (bpf_map_is_dev_bound(map
)) {
3289 err
= bpf_map_offload_info_fill(&info
, map
);
3294 if (copy_to_user(uinfo
, &info
, info_len
) ||
3295 put_user(info_len
, &uattr
->info
.info_len
))
3301 static int bpf_btf_get_info_by_fd(struct btf
*btf
,
3302 const union bpf_attr
*attr
,
3303 union bpf_attr __user
*uattr
)
3305 struct bpf_btf_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
3306 u32 info_len
= attr
->info
.info_len
;
3309 err
= bpf_check_uarg_tail_zero(uinfo
, sizeof(*uinfo
), info_len
);
3313 return btf_get_info_by_fd(btf
, attr
, uattr
);
3316 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3318 static int bpf_obj_get_info_by_fd(const union bpf_attr
*attr
,
3319 union bpf_attr __user
*uattr
)
3321 int ufd
= attr
->info
.bpf_fd
;
3325 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD
))
3332 if (f
.file
->f_op
== &bpf_prog_fops
)
3333 err
= bpf_prog_get_info_by_fd(f
.file
->private_data
, attr
,
3335 else if (f
.file
->f_op
== &bpf_map_fops
)
3336 err
= bpf_map_get_info_by_fd(f
.file
->private_data
, attr
,
3338 else if (f
.file
->f_op
== &btf_fops
)
3339 err
= bpf_btf_get_info_by_fd(f
.file
->private_data
, attr
, uattr
);
3347 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3349 static int bpf_btf_load(const union bpf_attr
*attr
)
3351 if (CHECK_ATTR(BPF_BTF_LOAD
))
3354 if (!capable(CAP_SYS_ADMIN
))
3357 return btf_new_fd(attr
);
3360 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3362 static int bpf_btf_get_fd_by_id(const union bpf_attr
*attr
)
3364 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID
))
3367 if (!capable(CAP_SYS_ADMIN
))
3370 return btf_get_fd_by_id(attr
->btf_id
);
3373 static int bpf_task_fd_query_copy(const union bpf_attr
*attr
,
3374 union bpf_attr __user
*uattr
,
3375 u32 prog_id
, u32 fd_type
,
3376 const char *buf
, u64 probe_offset
,
3379 char __user
*ubuf
= u64_to_user_ptr(attr
->task_fd_query
.buf
);
3380 u32 len
= buf
? strlen(buf
) : 0, input_len
;
3383 if (put_user(len
, &uattr
->task_fd_query
.buf_len
))
3385 input_len
= attr
->task_fd_query
.buf_len
;
3386 if (input_len
&& ubuf
) {
3388 /* nothing to copy, just make ubuf NULL terminated */
3391 if (put_user(zero
, ubuf
))
3393 } else if (input_len
>= len
+ 1) {
3394 /* ubuf can hold the string with NULL terminator */
3395 if (copy_to_user(ubuf
, buf
, len
+ 1))
3398 /* ubuf cannot hold the string with NULL terminator,
3399 * do a partial copy with NULL terminator.
3404 if (copy_to_user(ubuf
, buf
, input_len
- 1))
3406 if (put_user(zero
, ubuf
+ input_len
- 1))
3411 if (put_user(prog_id
, &uattr
->task_fd_query
.prog_id
) ||
3412 put_user(fd_type
, &uattr
->task_fd_query
.fd_type
) ||
3413 put_user(probe_offset
, &uattr
->task_fd_query
.probe_offset
) ||
3414 put_user(probe_addr
, &uattr
->task_fd_query
.probe_addr
))
3420 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3422 static int bpf_task_fd_query(const union bpf_attr
*attr
,
3423 union bpf_attr __user
*uattr
)
3425 pid_t pid
= attr
->task_fd_query
.pid
;
3426 u32 fd
= attr
->task_fd_query
.fd
;
3427 const struct perf_event
*event
;
3428 struct files_struct
*files
;
3429 struct task_struct
*task
;
3433 if (CHECK_ATTR(BPF_TASK_FD_QUERY
))
3436 if (!capable(CAP_SYS_ADMIN
))
3439 if (attr
->task_fd_query
.flags
!= 0)
3442 task
= get_pid_task(find_vpid(pid
), PIDTYPE_PID
);
3446 files
= get_files_struct(task
);
3447 put_task_struct(task
);
3452 spin_lock(&files
->file_lock
);
3453 file
= fcheck_files(files
, fd
);
3458 spin_unlock(&files
->file_lock
);
3459 put_files_struct(files
);
3464 if (file
->f_op
== &bpf_link_fops
) {
3465 struct bpf_link
*link
= file
->private_data
;
3467 if (link
->ops
== &bpf_raw_tp_lops
) {
3468 struct bpf_raw_tp_link
*raw_tp
=
3469 container_of(link
, struct bpf_raw_tp_link
, link
);
3470 struct bpf_raw_event_map
*btp
= raw_tp
->btp
;
3472 err
= bpf_task_fd_query_copy(attr
, uattr
,
3473 raw_tp
->link
.prog
->aux
->id
,
3474 BPF_FD_TYPE_RAW_TRACEPOINT
,
3475 btp
->tp
->name
, 0, 0);
3481 event
= perf_get_event(file
);
3482 if (!IS_ERR(event
)) {
3483 u64 probe_offset
, probe_addr
;
3484 u32 prog_id
, fd_type
;
3487 err
= bpf_get_perf_event_info(event
, &prog_id
, &fd_type
,
3488 &buf
, &probe_offset
,
3491 err
= bpf_task_fd_query_copy(attr
, uattr
, prog_id
,
3506 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
3508 #define BPF_DO_BATCH(fn) \
3514 err = fn(map, attr, uattr); \
3517 static int bpf_map_do_batch(const union bpf_attr
*attr
,
3518 union bpf_attr __user
*uattr
,
3521 struct bpf_map
*map
;
3525 if (CHECK_ATTR(BPF_MAP_BATCH
))
3528 ufd
= attr
->batch
.map_fd
;
3530 map
= __bpf_map_get(f
);
3532 return PTR_ERR(map
);
3534 if ((cmd
== BPF_MAP_LOOKUP_BATCH
||
3535 cmd
== BPF_MAP_LOOKUP_AND_DELETE_BATCH
) &&
3536 !(map_get_sys_perms(map
, f
) & FMODE_CAN_READ
)) {
3541 if (cmd
!= BPF_MAP_LOOKUP_BATCH
&&
3542 !(map_get_sys_perms(map
, f
) & FMODE_CAN_WRITE
)) {
3547 if (cmd
== BPF_MAP_LOOKUP_BATCH
)
3548 BPF_DO_BATCH(map
->ops
->map_lookup_batch
);
3549 else if (cmd
== BPF_MAP_LOOKUP_AND_DELETE_BATCH
)
3550 BPF_DO_BATCH(map
->ops
->map_lookup_and_delete_batch
);
3551 else if (cmd
== BPF_MAP_UPDATE_BATCH
)
3552 BPF_DO_BATCH(map
->ops
->map_update_batch
);
3554 BPF_DO_BATCH(map
->ops
->map_delete_batch
);
3561 #define BPF_LINK_CREATE_LAST_FIELD link_create.flags
3562 static int link_create(union bpf_attr
*attr
)
3564 enum bpf_prog_type ptype
;
3565 struct bpf_prog
*prog
;
3568 if (!capable(CAP_NET_ADMIN
))
3571 if (CHECK_ATTR(BPF_LINK_CREATE
))
3574 ptype
= attach_type_to_prog_type(attr
->link_create
.attach_type
);
3575 if (ptype
== BPF_PROG_TYPE_UNSPEC
)
3578 prog
= bpf_prog_get_type(attr
->link_create
.prog_fd
, ptype
);
3580 return PTR_ERR(prog
);
3582 ret
= bpf_prog_attach_check_attach_type(prog
,
3583 attr
->link_create
.attach_type
);
3588 case BPF_PROG_TYPE_CGROUP_SKB
:
3589 case BPF_PROG_TYPE_CGROUP_SOCK
:
3590 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR
:
3591 case BPF_PROG_TYPE_SOCK_OPS
:
3592 case BPF_PROG_TYPE_CGROUP_DEVICE
:
3593 case BPF_PROG_TYPE_CGROUP_SYSCTL
:
3594 case BPF_PROG_TYPE_CGROUP_SOCKOPT
:
3595 ret
= cgroup_bpf_link_attach(attr
, prog
);
3607 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
3609 static int link_update(union bpf_attr
*attr
)
3611 struct bpf_prog
*old_prog
= NULL
, *new_prog
;
3612 struct bpf_link
*link
;
3616 if (!capable(CAP_NET_ADMIN
))
3619 if (CHECK_ATTR(BPF_LINK_UPDATE
))
3622 flags
= attr
->link_update
.flags
;
3623 if (flags
& ~BPF_F_REPLACE
)
3626 link
= bpf_link_get_from_fd(attr
->link_update
.link_fd
);
3628 return PTR_ERR(link
);
3630 new_prog
= bpf_prog_get(attr
->link_update
.new_prog_fd
);
3631 if (IS_ERR(new_prog
))
3632 return PTR_ERR(new_prog
);
3634 if (flags
& BPF_F_REPLACE
) {
3635 old_prog
= bpf_prog_get(attr
->link_update
.old_prog_fd
);
3636 if (IS_ERR(old_prog
)) {
3637 ret
= PTR_ERR(old_prog
);
3643 #ifdef CONFIG_CGROUP_BPF
3644 if (link
->ops
== &bpf_cgroup_link_lops
) {
3645 ret
= cgroup_bpf_replace(link
, old_prog
, new_prog
);
3653 bpf_prog_put(old_prog
);
3655 bpf_prog_put(new_prog
);
3659 SYSCALL_DEFINE3(bpf
, int, cmd
, union bpf_attr __user
*, uattr
, unsigned int, size
)
3661 union bpf_attr attr
;
3664 if (sysctl_unprivileged_bpf_disabled
&& !capable(CAP_SYS_ADMIN
))
3667 err
= bpf_check_uarg_tail_zero(uattr
, sizeof(attr
), size
);
3670 size
= min_t(u32
, size
, sizeof(attr
));
3672 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
3673 memset(&attr
, 0, sizeof(attr
));
3674 if (copy_from_user(&attr
, uattr
, size
) != 0)
3677 err
= security_bpf(cmd
, &attr
, size
);
3682 case BPF_MAP_CREATE
:
3683 err
= map_create(&attr
);
3685 case BPF_MAP_LOOKUP_ELEM
:
3686 err
= map_lookup_elem(&attr
);
3688 case BPF_MAP_UPDATE_ELEM
:
3689 err
= map_update_elem(&attr
);
3691 case BPF_MAP_DELETE_ELEM
:
3692 err
= map_delete_elem(&attr
);
3694 case BPF_MAP_GET_NEXT_KEY
:
3695 err
= map_get_next_key(&attr
);
3697 case BPF_MAP_FREEZE
:
3698 err
= map_freeze(&attr
);
3701 err
= bpf_prog_load(&attr
, uattr
);
3704 err
= bpf_obj_pin(&attr
);
3707 err
= bpf_obj_get(&attr
);
3709 case BPF_PROG_ATTACH
:
3710 err
= bpf_prog_attach(&attr
);
3712 case BPF_PROG_DETACH
:
3713 err
= bpf_prog_detach(&attr
);
3715 case BPF_PROG_QUERY
:
3716 err
= bpf_prog_query(&attr
, uattr
);
3718 case BPF_PROG_TEST_RUN
:
3719 err
= bpf_prog_test_run(&attr
, uattr
);
3721 case BPF_PROG_GET_NEXT_ID
:
3722 err
= bpf_obj_get_next_id(&attr
, uattr
,
3723 &prog_idr
, &prog_idr_lock
);
3725 case BPF_MAP_GET_NEXT_ID
:
3726 err
= bpf_obj_get_next_id(&attr
, uattr
,
3727 &map_idr
, &map_idr_lock
);
3729 case BPF_BTF_GET_NEXT_ID
:
3730 err
= bpf_obj_get_next_id(&attr
, uattr
,
3731 &btf_idr
, &btf_idr_lock
);
3733 case BPF_PROG_GET_FD_BY_ID
:
3734 err
= bpf_prog_get_fd_by_id(&attr
);
3736 case BPF_MAP_GET_FD_BY_ID
:
3737 err
= bpf_map_get_fd_by_id(&attr
);
3739 case BPF_OBJ_GET_INFO_BY_FD
:
3740 err
= bpf_obj_get_info_by_fd(&attr
, uattr
);
3742 case BPF_RAW_TRACEPOINT_OPEN
:
3743 err
= bpf_raw_tracepoint_open(&attr
);
3746 err
= bpf_btf_load(&attr
);
3748 case BPF_BTF_GET_FD_BY_ID
:
3749 err
= bpf_btf_get_fd_by_id(&attr
);
3751 case BPF_TASK_FD_QUERY
:
3752 err
= bpf_task_fd_query(&attr
, uattr
);
3754 case BPF_MAP_LOOKUP_AND_DELETE_ELEM
:
3755 err
= map_lookup_and_delete_elem(&attr
);
3757 case BPF_MAP_LOOKUP_BATCH
:
3758 err
= bpf_map_do_batch(&attr
, uattr
, BPF_MAP_LOOKUP_BATCH
);
3760 case BPF_MAP_LOOKUP_AND_DELETE_BATCH
:
3761 err
= bpf_map_do_batch(&attr
, uattr
,
3762 BPF_MAP_LOOKUP_AND_DELETE_BATCH
);
3764 case BPF_MAP_UPDATE_BATCH
:
3765 err
= bpf_map_do_batch(&attr
, uattr
, BPF_MAP_UPDATE_BATCH
);
3767 case BPF_MAP_DELETE_BATCH
:
3768 err
= bpf_map_do_batch(&attr
, uattr
, BPF_MAP_DELETE_BATCH
);
3770 case BPF_LINK_CREATE
:
3771 err
= link_create(&attr
);
3773 case BPF_LINK_UPDATE
:
3774 err
= link_update(&attr
);