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>
29 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
31 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
32 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
33 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
34 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
37 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
39 DEFINE_PER_CPU(int, bpf_prog_active
);
40 static DEFINE_IDR(prog_idr
);
41 static DEFINE_SPINLOCK(prog_idr_lock
);
42 static DEFINE_IDR(map_idr
);
43 static DEFINE_SPINLOCK(map_idr_lock
);
45 int sysctl_unprivileged_bpf_disabled __read_mostly
;
47 static const struct bpf_map_ops
* const bpf_map_types
[] = {
48 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
49 #define BPF_MAP_TYPE(_id, _ops) \
51 #include <linux/bpf_types.h>
57 * If we're handed a bigger struct than we know of, ensure all the unknown bits
58 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
59 * we don't know about yet.
61 * There is a ToCToU between this function call and the following
62 * copy_from_user() call. However, this is not a concern since this function is
63 * meant to be a future-proofing of bits.
65 int bpf_check_uarg_tail_zero(void __user
*uaddr
,
69 unsigned char __user
*addr
;
70 unsigned char __user
*end
;
74 if (unlikely(actual_size
> PAGE_SIZE
)) /* silly large */
77 if (unlikely(!access_ok(uaddr
, actual_size
)))
80 if (actual_size
<= expected_size
)
83 addr
= uaddr
+ expected_size
;
84 end
= uaddr
+ actual_size
;
86 for (; addr
< end
; addr
++) {
87 err
= get_user(val
, addr
);
97 const struct bpf_map_ops bpf_map_offload_ops
= {
98 .map_alloc
= bpf_map_offload_map_alloc
,
99 .map_free
= bpf_map_offload_map_free
,
100 .map_check_btf
= map_check_no_btf
,
103 static struct bpf_map
*find_and_alloc_map(union bpf_attr
*attr
)
105 const struct bpf_map_ops
*ops
;
106 u32 type
= attr
->map_type
;
110 if (type
>= ARRAY_SIZE(bpf_map_types
))
111 return ERR_PTR(-EINVAL
);
112 type
= array_index_nospec(type
, ARRAY_SIZE(bpf_map_types
));
113 ops
= bpf_map_types
[type
];
115 return ERR_PTR(-EINVAL
);
117 if (ops
->map_alloc_check
) {
118 err
= ops
->map_alloc_check(attr
);
122 if (attr
->map_ifindex
)
123 ops
= &bpf_map_offload_ops
;
124 map
= ops
->map_alloc(attr
);
128 map
->map_type
= type
;
132 static u32
bpf_map_value_size(struct bpf_map
*map
)
134 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
135 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
136 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
||
137 map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
)
138 return round_up(map
->value_size
, 8) * num_possible_cpus();
139 else if (IS_FD_MAP(map
))
142 return map
->value_size
;
145 static void maybe_wait_bpf_programs(struct bpf_map
*map
)
147 /* Wait for any running BPF programs to complete so that
148 * userspace, when we return to it, knows that all programs
149 * that could be running use the new map value.
151 if (map
->map_type
== BPF_MAP_TYPE_HASH_OF_MAPS
||
152 map
->map_type
== BPF_MAP_TYPE_ARRAY_OF_MAPS
)
156 static int bpf_map_update_value(struct bpf_map
*map
, struct fd f
, void *key
,
157 void *value
, __u64 flags
)
161 /* Need to create a kthread, thus must support schedule */
162 if (bpf_map_is_dev_bound(map
)) {
163 return bpf_map_offload_update_elem(map
, key
, value
, flags
);
164 } else if (map
->map_type
== BPF_MAP_TYPE_CPUMAP
||
165 map
->map_type
== BPF_MAP_TYPE_SOCKHASH
||
166 map
->map_type
== BPF_MAP_TYPE_SOCKMAP
||
167 map
->map_type
== BPF_MAP_TYPE_STRUCT_OPS
) {
168 return map
->ops
->map_update_elem(map
, key
, value
, flags
);
169 } else if (IS_FD_PROG_ARRAY(map
)) {
170 return bpf_fd_array_map_update_elem(map
, f
.file
, key
, value
,
174 bpf_disable_instrumentation();
175 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
176 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
177 err
= bpf_percpu_hash_update(map
, key
, value
, flags
);
178 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
179 err
= bpf_percpu_array_update(map
, key
, value
, flags
);
180 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
) {
181 err
= bpf_percpu_cgroup_storage_update(map
, key
, value
,
183 } else if (IS_FD_ARRAY(map
)) {
185 err
= bpf_fd_array_map_update_elem(map
, f
.file
, key
, value
,
188 } else if (map
->map_type
== BPF_MAP_TYPE_HASH_OF_MAPS
) {
190 err
= bpf_fd_htab_map_update_elem(map
, f
.file
, key
, value
,
193 } else if (map
->map_type
== BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
) {
194 /* rcu_read_lock() is not needed */
195 err
= bpf_fd_reuseport_array_update_elem(map
, key
, value
,
197 } else if (map
->map_type
== BPF_MAP_TYPE_QUEUE
||
198 map
->map_type
== BPF_MAP_TYPE_STACK
) {
199 err
= map
->ops
->map_push_elem(map
, value
, flags
);
202 err
= map
->ops
->map_update_elem(map
, key
, value
, flags
);
205 bpf_enable_instrumentation();
206 maybe_wait_bpf_programs(map
);
211 static int bpf_map_copy_value(struct bpf_map
*map
, void *key
, void *value
,
217 if (bpf_map_is_dev_bound(map
))
218 return bpf_map_offload_lookup_elem(map
, key
, value
);
220 bpf_disable_instrumentation();
221 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
222 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
223 err
= bpf_percpu_hash_copy(map
, key
, value
);
224 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
225 err
= bpf_percpu_array_copy(map
, key
, value
);
226 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
) {
227 err
= bpf_percpu_cgroup_storage_copy(map
, key
, value
);
228 } else if (map
->map_type
== BPF_MAP_TYPE_STACK_TRACE
) {
229 err
= bpf_stackmap_copy(map
, key
, value
);
230 } else if (IS_FD_ARRAY(map
) || IS_FD_PROG_ARRAY(map
)) {
231 err
= bpf_fd_array_map_lookup_elem(map
, key
, value
);
232 } else if (IS_FD_HASH(map
)) {
233 err
= bpf_fd_htab_map_lookup_elem(map
, key
, value
);
234 } else if (map
->map_type
== BPF_MAP_TYPE_REUSEPORT_SOCKARRAY
) {
235 err
= bpf_fd_reuseport_array_lookup_elem(map
, key
, value
);
236 } else if (map
->map_type
== BPF_MAP_TYPE_QUEUE
||
237 map
->map_type
== BPF_MAP_TYPE_STACK
) {
238 err
= map
->ops
->map_peek_elem(map
, value
);
239 } else if (map
->map_type
== BPF_MAP_TYPE_STRUCT_OPS
) {
240 /* struct_ops map requires directly updating "value" */
241 err
= bpf_struct_ops_map_sys_lookup_elem(map
, key
, value
);
244 if (map
->ops
->map_lookup_elem_sys_only
)
245 ptr
= map
->ops
->map_lookup_elem_sys_only(map
, key
);
247 ptr
= map
->ops
->map_lookup_elem(map
, key
);
254 if (flags
& BPF_F_LOCK
)
255 /* lock 'ptr' and copy everything but lock */
256 copy_map_value_locked(map
, value
, ptr
, true);
258 copy_map_value(map
, value
, ptr
);
259 /* mask lock, since value wasn't zero inited */
260 check_and_init_map_lock(map
, value
);
265 bpf_enable_instrumentation();
266 maybe_wait_bpf_programs(map
);
271 static void *__bpf_map_area_alloc(u64 size
, int numa_node
, bool mmapable
)
273 /* We really just want to fail instead of triggering OOM killer
274 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
275 * which is used for lower order allocation requests.
277 * It has been observed that higher order allocation requests done by
278 * vmalloc with __GFP_NORETRY being set might fail due to not trying
279 * to reclaim memory from the page cache, thus we set
280 * __GFP_RETRY_MAYFAIL to avoid such situations.
283 const gfp_t flags
= __GFP_NOWARN
| __GFP_ZERO
;
286 if (size
>= SIZE_MAX
)
289 /* kmalloc()'ed memory can't be mmap()'ed */
290 if (!mmapable
&& size
<= (PAGE_SIZE
<< PAGE_ALLOC_COSTLY_ORDER
)) {
291 area
= kmalloc_node(size
, GFP_USER
| __GFP_NORETRY
| flags
,
297 BUG_ON(!PAGE_ALIGNED(size
));
298 return vmalloc_user_node_flags(size
, numa_node
, GFP_KERNEL
|
299 __GFP_RETRY_MAYFAIL
| flags
);
301 return __vmalloc_node_flags_caller(size
, numa_node
,
302 GFP_KERNEL
| __GFP_RETRY_MAYFAIL
|
303 flags
, __builtin_return_address(0));
306 void *bpf_map_area_alloc(u64 size
, int numa_node
)
308 return __bpf_map_area_alloc(size
, numa_node
, false);
311 void *bpf_map_area_mmapable_alloc(u64 size
, int numa_node
)
313 return __bpf_map_area_alloc(size
, numa_node
, true);
316 void bpf_map_area_free(void *area
)
321 static u32
bpf_map_flags_retain_permanent(u32 flags
)
323 /* Some map creation flags are not tied to the map object but
324 * rather to the map fd instead, so they have no meaning upon
325 * map object inspection since multiple file descriptors with
326 * different (access) properties can exist here. Thus, given
327 * this has zero meaning for the map itself, lets clear these
330 return flags
& ~(BPF_F_RDONLY
| BPF_F_WRONLY
);
333 void bpf_map_init_from_attr(struct bpf_map
*map
, union bpf_attr
*attr
)
335 map
->map_type
= attr
->map_type
;
336 map
->key_size
= attr
->key_size
;
337 map
->value_size
= attr
->value_size
;
338 map
->max_entries
= attr
->max_entries
;
339 map
->map_flags
= bpf_map_flags_retain_permanent(attr
->map_flags
);
340 map
->numa_node
= bpf_map_attr_numa_node(attr
);
343 static int bpf_charge_memlock(struct user_struct
*user
, u32 pages
)
345 unsigned long memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
347 if (atomic_long_add_return(pages
, &user
->locked_vm
) > memlock_limit
) {
348 atomic_long_sub(pages
, &user
->locked_vm
);
354 static void bpf_uncharge_memlock(struct user_struct
*user
, u32 pages
)
357 atomic_long_sub(pages
, &user
->locked_vm
);
360 int bpf_map_charge_init(struct bpf_map_memory
*mem
, u64 size
)
362 u32 pages
= round_up(size
, PAGE_SIZE
) >> PAGE_SHIFT
;
363 struct user_struct
*user
;
366 if (size
>= U32_MAX
- PAGE_SIZE
)
369 user
= get_current_user();
370 ret
= bpf_charge_memlock(user
, pages
);
382 void bpf_map_charge_finish(struct bpf_map_memory
*mem
)
384 bpf_uncharge_memlock(mem
->user
, mem
->pages
);
388 void bpf_map_charge_move(struct bpf_map_memory
*dst
,
389 struct bpf_map_memory
*src
)
393 /* Make sure src will not be used for the redundant uncharging. */
394 memset(src
, 0, sizeof(struct bpf_map_memory
));
397 int bpf_map_charge_memlock(struct bpf_map
*map
, u32 pages
)
401 ret
= bpf_charge_memlock(map
->memory
.user
, pages
);
404 map
->memory
.pages
+= pages
;
408 void bpf_map_uncharge_memlock(struct bpf_map
*map
, u32 pages
)
410 bpf_uncharge_memlock(map
->memory
.user
, pages
);
411 map
->memory
.pages
-= pages
;
414 static int bpf_map_alloc_id(struct bpf_map
*map
)
418 idr_preload(GFP_KERNEL
);
419 spin_lock_bh(&map_idr_lock
);
420 id
= idr_alloc_cyclic(&map_idr
, map
, 1, INT_MAX
, GFP_ATOMIC
);
423 spin_unlock_bh(&map_idr_lock
);
426 if (WARN_ON_ONCE(!id
))
429 return id
> 0 ? 0 : id
;
432 void bpf_map_free_id(struct bpf_map
*map
, bool do_idr_lock
)
436 /* Offloaded maps are removed from the IDR store when their device
437 * disappears - even if someone holds an fd to them they are unusable,
438 * the memory is gone, all ops will fail; they are simply waiting for
439 * refcnt to drop to be freed.
445 spin_lock_irqsave(&map_idr_lock
, flags
);
447 __acquire(&map_idr_lock
);
449 idr_remove(&map_idr
, map
->id
);
453 spin_unlock_irqrestore(&map_idr_lock
, flags
);
455 __release(&map_idr_lock
);
458 /* called from workqueue */
459 static void bpf_map_free_deferred(struct work_struct
*work
)
461 struct bpf_map
*map
= container_of(work
, struct bpf_map
, work
);
462 struct bpf_map_memory mem
;
464 bpf_map_charge_move(&mem
, &map
->memory
);
465 security_bpf_map_free(map
);
466 /* implementation dependent freeing */
467 map
->ops
->map_free(map
);
468 bpf_map_charge_finish(&mem
);
471 static void bpf_map_put_uref(struct bpf_map
*map
)
473 if (atomic64_dec_and_test(&map
->usercnt
)) {
474 if (map
->ops
->map_release_uref
)
475 map
->ops
->map_release_uref(map
);
479 /* decrement map refcnt and schedule it for freeing via workqueue
480 * (unrelying map implementation ops->map_free() might sleep)
482 static void __bpf_map_put(struct bpf_map
*map
, bool do_idr_lock
)
484 if (atomic64_dec_and_test(&map
->refcnt
)) {
485 /* bpf_map_free_id() must be called first */
486 bpf_map_free_id(map
, do_idr_lock
);
488 INIT_WORK(&map
->work
, bpf_map_free_deferred
);
489 schedule_work(&map
->work
);
493 void bpf_map_put(struct bpf_map
*map
)
495 __bpf_map_put(map
, true);
497 EXPORT_SYMBOL_GPL(bpf_map_put
);
499 void bpf_map_put_with_uref(struct bpf_map
*map
)
501 bpf_map_put_uref(map
);
505 static int bpf_map_release(struct inode
*inode
, struct file
*filp
)
507 struct bpf_map
*map
= filp
->private_data
;
509 if (map
->ops
->map_release
)
510 map
->ops
->map_release(map
, filp
);
512 bpf_map_put_with_uref(map
);
516 static fmode_t
map_get_sys_perms(struct bpf_map
*map
, struct fd f
)
518 fmode_t mode
= f
.file
->f_mode
;
520 /* Our file permissions may have been overridden by global
521 * map permissions facing syscall side.
523 if (READ_ONCE(map
->frozen
))
524 mode
&= ~FMODE_CAN_WRITE
;
528 #ifdef CONFIG_PROC_FS
529 static void bpf_map_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
531 const struct bpf_map
*map
= filp
->private_data
;
532 const struct bpf_array
*array
;
533 u32 type
= 0, jited
= 0;
535 if (map
->map_type
== BPF_MAP_TYPE_PROG_ARRAY
) {
536 array
= container_of(map
, struct bpf_array
, map
);
537 type
= array
->aux
->type
;
538 jited
= array
->aux
->jited
;
555 map
->memory
.pages
* 1ULL << PAGE_SHIFT
,
557 READ_ONCE(map
->frozen
));
559 seq_printf(m
, "owner_prog_type:\t%u\n", type
);
560 seq_printf(m
, "owner_jited:\t%u\n", jited
);
565 static ssize_t
bpf_dummy_read(struct file
*filp
, char __user
*buf
, size_t siz
,
568 /* We need this handler such that alloc_file() enables
569 * f_mode with FMODE_CAN_READ.
574 static ssize_t
bpf_dummy_write(struct file
*filp
, const char __user
*buf
,
575 size_t siz
, loff_t
*ppos
)
577 /* We need this handler such that alloc_file() enables
578 * f_mode with FMODE_CAN_WRITE.
583 /* called for any extra memory-mapped regions (except initial) */
584 static void bpf_map_mmap_open(struct vm_area_struct
*vma
)
586 struct bpf_map
*map
= vma
->vm_file
->private_data
;
588 bpf_map_inc_with_uref(map
);
590 if (vma
->vm_flags
& VM_WRITE
) {
591 mutex_lock(&map
->freeze_mutex
);
593 mutex_unlock(&map
->freeze_mutex
);
597 /* called for all unmapped memory region (including initial) */
598 static void bpf_map_mmap_close(struct vm_area_struct
*vma
)
600 struct bpf_map
*map
= vma
->vm_file
->private_data
;
602 if (vma
->vm_flags
& VM_WRITE
) {
603 mutex_lock(&map
->freeze_mutex
);
605 mutex_unlock(&map
->freeze_mutex
);
608 bpf_map_put_with_uref(map
);
611 static const struct vm_operations_struct bpf_map_default_vmops
= {
612 .open
= bpf_map_mmap_open
,
613 .close
= bpf_map_mmap_close
,
616 static int bpf_map_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
618 struct bpf_map
*map
= filp
->private_data
;
621 if (!map
->ops
->map_mmap
|| map_value_has_spin_lock(map
))
624 if (!(vma
->vm_flags
& VM_SHARED
))
627 mutex_lock(&map
->freeze_mutex
);
629 if ((vma
->vm_flags
& VM_WRITE
) && map
->frozen
) {
634 /* set default open/close callbacks */
635 vma
->vm_ops
= &bpf_map_default_vmops
;
636 vma
->vm_private_data
= map
;
638 err
= map
->ops
->map_mmap(map
, vma
);
642 bpf_map_inc_with_uref(map
);
644 if (vma
->vm_flags
& VM_WRITE
)
647 mutex_unlock(&map
->freeze_mutex
);
651 const struct file_operations bpf_map_fops
= {
652 #ifdef CONFIG_PROC_FS
653 .show_fdinfo
= bpf_map_show_fdinfo
,
655 .release
= bpf_map_release
,
656 .read
= bpf_dummy_read
,
657 .write
= bpf_dummy_write
,
658 .mmap
= bpf_map_mmap
,
661 int bpf_map_new_fd(struct bpf_map
*map
, int flags
)
665 ret
= security_bpf_map(map
, OPEN_FMODE(flags
));
669 return anon_inode_getfd("bpf-map", &bpf_map_fops
, map
,
673 int bpf_get_file_flag(int flags
)
675 if ((flags
& BPF_F_RDONLY
) && (flags
& BPF_F_WRONLY
))
677 if (flags
& BPF_F_RDONLY
)
679 if (flags
& BPF_F_WRONLY
)
684 /* helper macro to check that unused fields 'union bpf_attr' are zero */
685 #define CHECK_ATTR(CMD) \
686 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
687 sizeof(attr->CMD##_LAST_FIELD), 0, \
689 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
690 sizeof(attr->CMD##_LAST_FIELD)) != NULL
692 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
693 * Return 0 on success and < 0 on error.
695 static int bpf_obj_name_cpy(char *dst
, const char *src
)
697 const char *end
= src
+ BPF_OBJ_NAME_LEN
;
699 memset(dst
, 0, BPF_OBJ_NAME_LEN
);
700 /* Copy all isalnum(), '_' and '.' chars. */
701 while (src
< end
&& *src
) {
702 if (!isalnum(*src
) &&
703 *src
!= '_' && *src
!= '.')
708 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
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
);
810 atomic64_set(&map
->refcnt
, 1);
811 atomic64_set(&map
->usercnt
, 1);
812 mutex_init(&map
->freeze_mutex
);
814 map
->spin_lock_off
= -EINVAL
;
815 if (attr
->btf_key_type_id
|| attr
->btf_value_type_id
||
816 /* Even the map's value is a kernel's struct,
817 * the bpf_prog.o must have BTF to begin with
818 * to figure out the corresponding kernel's
819 * counter part. Thus, attr->btf_fd has
822 attr
->btf_vmlinux_value_type_id
) {
825 btf
= btf_get_by_fd(attr
->btf_fd
);
832 if (attr
->btf_value_type_id
) {
833 err
= map_check_btf(map
, btf
, attr
->btf_key_type_id
,
834 attr
->btf_value_type_id
);
839 map
->btf_key_type_id
= attr
->btf_key_type_id
;
840 map
->btf_value_type_id
= attr
->btf_value_type_id
;
841 map
->btf_vmlinux_value_type_id
=
842 attr
->btf_vmlinux_value_type_id
;
845 err
= security_bpf_map_alloc(map
);
849 err
= bpf_map_alloc_id(map
);
853 err
= bpf_map_new_fd(map
, f_flags
);
855 /* failed to allocate fd.
856 * bpf_map_put_with_uref() is needed because the above
857 * bpf_map_alloc_id() has published the map
858 * to the userspace and the userspace may
859 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
861 bpf_map_put_with_uref(map
);
868 security_bpf_map_free(map
);
871 bpf_map_charge_move(&mem
, &map
->memory
);
872 map
->ops
->map_free(map
);
873 bpf_map_charge_finish(&mem
);
877 /* if error is returned, fd is released.
878 * On success caller should complete fd access with matching fdput()
880 struct bpf_map
*__bpf_map_get(struct fd f
)
883 return ERR_PTR(-EBADF
);
884 if (f
.file
->f_op
!= &bpf_map_fops
) {
886 return ERR_PTR(-EINVAL
);
889 return f
.file
->private_data
;
892 void bpf_map_inc(struct bpf_map
*map
)
894 atomic64_inc(&map
->refcnt
);
896 EXPORT_SYMBOL_GPL(bpf_map_inc
);
898 void bpf_map_inc_with_uref(struct bpf_map
*map
)
900 atomic64_inc(&map
->refcnt
);
901 atomic64_inc(&map
->usercnt
);
903 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref
);
905 struct bpf_map
*bpf_map_get(u32 ufd
)
907 struct fd f
= fdget(ufd
);
910 map
= __bpf_map_get(f
);
920 struct bpf_map
*bpf_map_get_with_uref(u32 ufd
)
922 struct fd f
= fdget(ufd
);
925 map
= __bpf_map_get(f
);
929 bpf_map_inc_with_uref(map
);
935 /* map_idr_lock should have been held */
936 static struct bpf_map
*__bpf_map_inc_not_zero(struct bpf_map
*map
, bool uref
)
940 refold
= atomic64_fetch_add_unless(&map
->refcnt
, 1, 0);
942 return ERR_PTR(-ENOENT
);
944 atomic64_inc(&map
->usercnt
);
949 struct bpf_map
*bpf_map_inc_not_zero(struct bpf_map
*map
)
951 spin_lock_bh(&map_idr_lock
);
952 map
= __bpf_map_inc_not_zero(map
, false);
953 spin_unlock_bh(&map_idr_lock
);
957 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero
);
959 int __weak
bpf_stackmap_copy(struct bpf_map
*map
, void *key
, void *value
)
964 static void *__bpf_copy_key(void __user
*ukey
, u64 key_size
)
967 return memdup_user(ukey
, key_size
);
970 return ERR_PTR(-EINVAL
);
975 /* last field in 'union bpf_attr' used by this command */
976 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
978 static int map_lookup_elem(union bpf_attr
*attr
)
980 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
981 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
982 int ufd
= attr
->map_fd
;
989 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM
))
992 if (attr
->flags
& ~BPF_F_LOCK
)
996 map
= __bpf_map_get(f
);
999 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_READ
)) {
1004 if ((attr
->flags
& BPF_F_LOCK
) &&
1005 !map_value_has_spin_lock(map
)) {
1010 key
= __bpf_copy_key(ukey
, map
->key_size
);
1016 value_size
= bpf_map_value_size(map
);
1019 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
1023 err
= bpf_map_copy_value(map
, key
, value
, attr
->flags
);
1028 if (copy_to_user(uvalue
, value
, value_size
) != 0)
1043 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1045 static int map_update_elem(union bpf_attr
*attr
)
1047 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
1048 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
1049 int ufd
= attr
->map_fd
;
1050 struct bpf_map
*map
;
1056 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM
))
1060 map
= __bpf_map_get(f
);
1062 return PTR_ERR(map
);
1063 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_WRITE
)) {
1068 if ((attr
->flags
& BPF_F_LOCK
) &&
1069 !map_value_has_spin_lock(map
)) {
1074 key
= __bpf_copy_key(ukey
, map
->key_size
);
1080 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
1081 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
1082 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
||
1083 map
->map_type
== BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
)
1084 value_size
= round_up(map
->value_size
, 8) * num_possible_cpus();
1086 value_size
= map
->value_size
;
1089 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
1094 if (copy_from_user(value
, uvalue
, value_size
) != 0)
1097 err
= bpf_map_update_value(map
, f
, key
, value
, attr
->flags
);
1108 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1110 static int map_delete_elem(union bpf_attr
*attr
)
1112 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
1113 int ufd
= attr
->map_fd
;
1114 struct bpf_map
*map
;
1119 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM
))
1123 map
= __bpf_map_get(f
);
1125 return PTR_ERR(map
);
1126 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_WRITE
)) {
1131 key
= __bpf_copy_key(ukey
, map
->key_size
);
1137 if (bpf_map_is_dev_bound(map
)) {
1138 err
= bpf_map_offload_delete_elem(map
, key
);
1140 } else if (IS_FD_PROG_ARRAY(map
) ||
1141 map
->map_type
== BPF_MAP_TYPE_STRUCT_OPS
) {
1142 /* These maps require sleepable context */
1143 err
= map
->ops
->map_delete_elem(map
, key
);
1147 bpf_disable_instrumentation();
1149 err
= map
->ops
->map_delete_elem(map
, key
);
1151 bpf_enable_instrumentation();
1152 maybe_wait_bpf_programs(map
);
1160 /* last field in 'union bpf_attr' used by this command */
1161 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1163 static int map_get_next_key(union bpf_attr
*attr
)
1165 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
1166 void __user
*unext_key
= u64_to_user_ptr(attr
->next_key
);
1167 int ufd
= attr
->map_fd
;
1168 struct bpf_map
*map
;
1169 void *key
, *next_key
;
1173 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY
))
1177 map
= __bpf_map_get(f
);
1179 return PTR_ERR(map
);
1180 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_READ
)) {
1186 key
= __bpf_copy_key(ukey
, map
->key_size
);
1196 next_key
= kmalloc(map
->key_size
, GFP_USER
);
1200 if (bpf_map_is_dev_bound(map
)) {
1201 err
= bpf_map_offload_get_next_key(map
, key
, next_key
);
1206 err
= map
->ops
->map_get_next_key(map
, key
, next_key
);
1213 if (copy_to_user(unext_key
, next_key
, map
->key_size
) != 0)
1227 int generic_map_delete_batch(struct bpf_map
*map
,
1228 const union bpf_attr
*attr
,
1229 union bpf_attr __user
*uattr
)
1231 void __user
*keys
= u64_to_user_ptr(attr
->batch
.keys
);
1236 if (attr
->batch
.elem_flags
& ~BPF_F_LOCK
)
1239 if ((attr
->batch
.elem_flags
& BPF_F_LOCK
) &&
1240 !map_value_has_spin_lock(map
)) {
1244 max_count
= attr
->batch
.count
;
1248 key
= kmalloc(map
->key_size
, GFP_USER
| __GFP_NOWARN
);
1252 for (cp
= 0; cp
< max_count
; cp
++) {
1254 if (copy_from_user(key
, keys
+ cp
* map
->key_size
,
1258 if (bpf_map_is_dev_bound(map
)) {
1259 err
= bpf_map_offload_delete_elem(map
, key
);
1263 bpf_disable_instrumentation();
1265 err
= map
->ops
->map_delete_elem(map
, key
);
1267 bpf_enable_instrumentation();
1268 maybe_wait_bpf_programs(map
);
1272 if (copy_to_user(&uattr
->batch
.count
, &cp
, sizeof(cp
)))
1279 int generic_map_update_batch(struct bpf_map
*map
,
1280 const union bpf_attr
*attr
,
1281 union bpf_attr __user
*uattr
)
1283 void __user
*values
= u64_to_user_ptr(attr
->batch
.values
);
1284 void __user
*keys
= u64_to_user_ptr(attr
->batch
.keys
);
1285 u32 value_size
, cp
, max_count
;
1286 int ufd
= attr
->map_fd
;
1292 if (attr
->batch
.elem_flags
& ~BPF_F_LOCK
)
1295 if ((attr
->batch
.elem_flags
& BPF_F_LOCK
) &&
1296 !map_value_has_spin_lock(map
)) {
1300 value_size
= bpf_map_value_size(map
);
1302 max_count
= attr
->batch
.count
;
1306 key
= kmalloc(map
->key_size
, GFP_USER
| __GFP_NOWARN
);
1310 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
1316 for (cp
= 0; cp
< max_count
; cp
++) {
1318 if (copy_from_user(key
, keys
+ cp
* map
->key_size
,
1320 copy_from_user(value
, values
+ cp
* value_size
, value_size
))
1323 err
= bpf_map_update_value(map
, f
, key
, value
,
1324 attr
->batch
.elem_flags
);
1330 if (copy_to_user(&uattr
->batch
.count
, &cp
, sizeof(cp
)))
1338 #define MAP_LOOKUP_RETRIES 3
1340 int generic_map_lookup_batch(struct bpf_map
*map
,
1341 const union bpf_attr
*attr
,
1342 union bpf_attr __user
*uattr
)
1344 void __user
*uobatch
= u64_to_user_ptr(attr
->batch
.out_batch
);
1345 void __user
*ubatch
= u64_to_user_ptr(attr
->batch
.in_batch
);
1346 void __user
*values
= u64_to_user_ptr(attr
->batch
.values
);
1347 void __user
*keys
= u64_to_user_ptr(attr
->batch
.keys
);
1348 void *buf
, *buf_prevkey
, *prev_key
, *key
, *value
;
1349 int err
, retry
= MAP_LOOKUP_RETRIES
;
1350 u32 value_size
, cp
, max_count
;
1352 if (attr
->batch
.elem_flags
& ~BPF_F_LOCK
)
1355 if ((attr
->batch
.elem_flags
& BPF_F_LOCK
) &&
1356 !map_value_has_spin_lock(map
))
1359 value_size
= bpf_map_value_size(map
);
1361 max_count
= attr
->batch
.count
;
1365 if (put_user(0, &uattr
->batch
.count
))
1368 buf_prevkey
= kmalloc(map
->key_size
, GFP_USER
| __GFP_NOWARN
);
1372 buf
= kmalloc(map
->key_size
+ value_size
, GFP_USER
| __GFP_NOWARN
);
1374 kvfree(buf_prevkey
);
1380 if (ubatch
&& copy_from_user(buf_prevkey
, ubatch
, map
->key_size
))
1383 value
= key
+ map
->key_size
;
1385 prev_key
= buf_prevkey
;
1387 for (cp
= 0; cp
< max_count
;) {
1389 err
= map
->ops
->map_get_next_key(map
, prev_key
, key
);
1393 err
= bpf_map_copy_value(map
, key
, value
,
1394 attr
->batch
.elem_flags
);
1396 if (err
== -ENOENT
) {
1408 if (copy_to_user(keys
+ cp
* map
->key_size
, key
,
1413 if (copy_to_user(values
+ cp
* value_size
, value
, value_size
)) {
1419 prev_key
= buf_prevkey
;
1421 swap(prev_key
, key
);
1422 retry
= MAP_LOOKUP_RETRIES
;
1429 if ((copy_to_user(&uattr
->batch
.count
, &cp
, sizeof(cp
)) ||
1430 (cp
&& copy_to_user(uobatch
, prev_key
, map
->key_size
))))
1439 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1441 static int map_lookup_and_delete_elem(union bpf_attr
*attr
)
1443 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
1444 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
1445 int ufd
= attr
->map_fd
;
1446 struct bpf_map
*map
;
1452 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM
))
1456 map
= __bpf_map_get(f
);
1458 return PTR_ERR(map
);
1459 if (!(map_get_sys_perms(map
, f
) & FMODE_CAN_WRITE
)) {
1464 key
= __bpf_copy_key(ukey
, map
->key_size
);
1470 value_size
= map
->value_size
;
1473 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
1477 if (map
->map_type
== BPF_MAP_TYPE_QUEUE
||
1478 map
->map_type
== BPF_MAP_TYPE_STACK
) {
1479 err
= map
->ops
->map_pop_elem(map
, value
);
1487 if (copy_to_user(uvalue
, value
, value_size
) != 0)
1501 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1503 static int map_freeze(const union bpf_attr
*attr
)
1505 int err
= 0, ufd
= attr
->map_fd
;
1506 struct bpf_map
*map
;
1509 if (CHECK_ATTR(BPF_MAP_FREEZE
))
1513 map
= __bpf_map_get(f
);
1515 return PTR_ERR(map
);
1517 mutex_lock(&map
->freeze_mutex
);
1519 if (map
->writecnt
) {
1523 if (READ_ONCE(map
->frozen
)) {
1527 if (!capable(CAP_SYS_ADMIN
)) {
1532 WRITE_ONCE(map
->frozen
, true);
1534 mutex_unlock(&map
->freeze_mutex
);
1539 static const struct bpf_prog_ops
* const bpf_prog_types
[] = {
1540 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1541 [_id] = & _name ## _prog_ops,
1542 #define BPF_MAP_TYPE(_id, _ops)
1543 #include <linux/bpf_types.h>
1544 #undef BPF_PROG_TYPE
1548 static int find_prog_type(enum bpf_prog_type type
, struct bpf_prog
*prog
)
1550 const struct bpf_prog_ops
*ops
;
1552 if (type
>= ARRAY_SIZE(bpf_prog_types
))
1554 type
= array_index_nospec(type
, ARRAY_SIZE(bpf_prog_types
));
1555 ops
= bpf_prog_types
[type
];
1559 if (!bpf_prog_is_dev_bound(prog
->aux
))
1560 prog
->aux
->ops
= ops
;
1562 prog
->aux
->ops
= &bpf_offload_prog_ops
;
1573 static const char * const bpf_audit_str
[BPF_AUDIT_MAX
] = {
1574 [BPF_AUDIT_LOAD
] = "LOAD",
1575 [BPF_AUDIT_UNLOAD
] = "UNLOAD",
1578 static void bpf_audit_prog(const struct bpf_prog
*prog
, unsigned int op
)
1580 struct audit_context
*ctx
= NULL
;
1581 struct audit_buffer
*ab
;
1583 if (WARN_ON_ONCE(op
>= BPF_AUDIT_MAX
))
1585 if (audit_enabled
== AUDIT_OFF
)
1587 if (op
== BPF_AUDIT_LOAD
)
1588 ctx
= audit_context();
1589 ab
= audit_log_start(ctx
, GFP_ATOMIC
, AUDIT_BPF
);
1592 audit_log_format(ab
, "prog-id=%u op=%s",
1593 prog
->aux
->id
, bpf_audit_str
[op
]);
1597 int __bpf_prog_charge(struct user_struct
*user
, u32 pages
)
1599 unsigned long memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
1600 unsigned long user_bufs
;
1603 user_bufs
= atomic_long_add_return(pages
, &user
->locked_vm
);
1604 if (user_bufs
> memlock_limit
) {
1605 atomic_long_sub(pages
, &user
->locked_vm
);
1613 void __bpf_prog_uncharge(struct user_struct
*user
, u32 pages
)
1616 atomic_long_sub(pages
, &user
->locked_vm
);
1619 static int bpf_prog_charge_memlock(struct bpf_prog
*prog
)
1621 struct user_struct
*user
= get_current_user();
1624 ret
= __bpf_prog_charge(user
, prog
->pages
);
1630 prog
->aux
->user
= user
;
1634 static void bpf_prog_uncharge_memlock(struct bpf_prog
*prog
)
1636 struct user_struct
*user
= prog
->aux
->user
;
1638 __bpf_prog_uncharge(user
, prog
->pages
);
1642 static int bpf_prog_alloc_id(struct bpf_prog
*prog
)
1646 idr_preload(GFP_KERNEL
);
1647 spin_lock_bh(&prog_idr_lock
);
1648 id
= idr_alloc_cyclic(&prog_idr
, prog
, 1, INT_MAX
, GFP_ATOMIC
);
1651 spin_unlock_bh(&prog_idr_lock
);
1654 /* id is in [1, INT_MAX) */
1655 if (WARN_ON_ONCE(!id
))
1658 return id
> 0 ? 0 : id
;
1661 void bpf_prog_free_id(struct bpf_prog
*prog
, bool do_idr_lock
)
1663 /* cBPF to eBPF migrations are currently not in the idr store.
1664 * Offloaded programs are removed from the store when their device
1665 * disappears - even if someone grabs an fd to them they are unusable,
1666 * simply waiting for refcnt to drop to be freed.
1672 spin_lock_bh(&prog_idr_lock
);
1674 __acquire(&prog_idr_lock
);
1676 idr_remove(&prog_idr
, prog
->aux
->id
);
1680 spin_unlock_bh(&prog_idr_lock
);
1682 __release(&prog_idr_lock
);
1685 static void __bpf_prog_put_rcu(struct rcu_head
*rcu
)
1687 struct bpf_prog_aux
*aux
= container_of(rcu
, struct bpf_prog_aux
, rcu
);
1689 kvfree(aux
->func_info
);
1690 kfree(aux
->func_info_aux
);
1691 bpf_prog_uncharge_memlock(aux
->prog
);
1692 security_bpf_prog_free(aux
);
1693 bpf_prog_free(aux
->prog
);
1696 static void __bpf_prog_put_noref(struct bpf_prog
*prog
, bool deferred
)
1698 bpf_prog_kallsyms_del_all(prog
);
1699 btf_put(prog
->aux
->btf
);
1700 bpf_prog_free_linfo(prog
);
1703 call_rcu(&prog
->aux
->rcu
, __bpf_prog_put_rcu
);
1705 __bpf_prog_put_rcu(&prog
->aux
->rcu
);
1708 static void __bpf_prog_put(struct bpf_prog
*prog
, bool do_idr_lock
)
1710 if (atomic64_dec_and_test(&prog
->aux
->refcnt
)) {
1711 perf_event_bpf_event(prog
, PERF_BPF_EVENT_PROG_UNLOAD
, 0);
1712 bpf_audit_prog(prog
, BPF_AUDIT_UNLOAD
);
1713 /* bpf_prog_free_id() must be called first */
1714 bpf_prog_free_id(prog
, do_idr_lock
);
1715 __bpf_prog_put_noref(prog
, true);
1719 void bpf_prog_put(struct bpf_prog
*prog
)
1721 __bpf_prog_put(prog
, true);
1723 EXPORT_SYMBOL_GPL(bpf_prog_put
);
1725 static int bpf_prog_release(struct inode
*inode
, struct file
*filp
)
1727 struct bpf_prog
*prog
= filp
->private_data
;
1733 static void bpf_prog_get_stats(const struct bpf_prog
*prog
,
1734 struct bpf_prog_stats
*stats
)
1736 u64 nsecs
= 0, cnt
= 0;
1739 for_each_possible_cpu(cpu
) {
1740 const struct bpf_prog_stats
*st
;
1744 st
= per_cpu_ptr(prog
->aux
->stats
, cpu
);
1746 start
= u64_stats_fetch_begin_irq(&st
->syncp
);
1749 } while (u64_stats_fetch_retry_irq(&st
->syncp
, start
));
1753 stats
->nsecs
= nsecs
;
1757 #ifdef CONFIG_PROC_FS
1758 static void bpf_prog_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
1760 const struct bpf_prog
*prog
= filp
->private_data
;
1761 char prog_tag
[sizeof(prog
->tag
) * 2 + 1] = { };
1762 struct bpf_prog_stats stats
;
1764 bpf_prog_get_stats(prog
, &stats
);
1765 bin2hex(prog_tag
, prog
->tag
, sizeof(prog
->tag
));
1772 "run_time_ns:\t%llu\n"
1777 prog
->pages
* 1ULL << PAGE_SHIFT
,
1784 const struct file_operations bpf_prog_fops
= {
1785 #ifdef CONFIG_PROC_FS
1786 .show_fdinfo
= bpf_prog_show_fdinfo
,
1788 .release
= bpf_prog_release
,
1789 .read
= bpf_dummy_read
,
1790 .write
= bpf_dummy_write
,
1793 int bpf_prog_new_fd(struct bpf_prog
*prog
)
1797 ret
= security_bpf_prog(prog
);
1801 return anon_inode_getfd("bpf-prog", &bpf_prog_fops
, prog
,
1802 O_RDWR
| O_CLOEXEC
);
1805 static struct bpf_prog
*____bpf_prog_get(struct fd f
)
1808 return ERR_PTR(-EBADF
);
1809 if (f
.file
->f_op
!= &bpf_prog_fops
) {
1811 return ERR_PTR(-EINVAL
);
1814 return f
.file
->private_data
;
1817 void bpf_prog_add(struct bpf_prog
*prog
, int i
)
1819 atomic64_add(i
, &prog
->aux
->refcnt
);
1821 EXPORT_SYMBOL_GPL(bpf_prog_add
);
1823 void bpf_prog_sub(struct bpf_prog
*prog
, int i
)
1825 /* Only to be used for undoing previous bpf_prog_add() in some
1826 * error path. We still know that another entity in our call
1827 * path holds a reference to the program, thus atomic_sub() can
1828 * be safely used in such cases!
1830 WARN_ON(atomic64_sub_return(i
, &prog
->aux
->refcnt
) == 0);
1832 EXPORT_SYMBOL_GPL(bpf_prog_sub
);
1834 void bpf_prog_inc(struct bpf_prog
*prog
)
1836 atomic64_inc(&prog
->aux
->refcnt
);
1838 EXPORT_SYMBOL_GPL(bpf_prog_inc
);
1840 /* prog_idr_lock should have been held */
1841 struct bpf_prog
*bpf_prog_inc_not_zero(struct bpf_prog
*prog
)
1845 refold
= atomic64_fetch_add_unless(&prog
->aux
->refcnt
, 1, 0);
1848 return ERR_PTR(-ENOENT
);
1852 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero
);
1854 bool bpf_prog_get_ok(struct bpf_prog
*prog
,
1855 enum bpf_prog_type
*attach_type
, bool attach_drv
)
1857 /* not an attachment, just a refcount inc, always allow */
1861 if (prog
->type
!= *attach_type
)
1863 if (bpf_prog_is_dev_bound(prog
->aux
) && !attach_drv
)
1869 static struct bpf_prog
*__bpf_prog_get(u32 ufd
, enum bpf_prog_type
*attach_type
,
1872 struct fd f
= fdget(ufd
);
1873 struct bpf_prog
*prog
;
1875 prog
= ____bpf_prog_get(f
);
1878 if (!bpf_prog_get_ok(prog
, attach_type
, attach_drv
)) {
1879 prog
= ERR_PTR(-EINVAL
);
1889 struct bpf_prog
*bpf_prog_get(u32 ufd
)
1891 return __bpf_prog_get(ufd
, NULL
, false);
1894 struct bpf_prog
*bpf_prog_get_type_dev(u32 ufd
, enum bpf_prog_type type
,
1897 return __bpf_prog_get(ufd
, &type
, attach_drv
);
1899 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev
);
1901 /* Initially all BPF programs could be loaded w/o specifying
1902 * expected_attach_type. Later for some of them specifying expected_attach_type
1903 * at load time became required so that program could be validated properly.
1904 * Programs of types that are allowed to be loaded both w/ and w/o (for
1905 * backward compatibility) expected_attach_type, should have the default attach
1906 * type assigned to expected_attach_type for the latter case, so that it can be
1907 * validated later at attach time.
1909 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1910 * prog type requires it but has some attach types that have to be backward
1913 static void bpf_prog_load_fixup_attach_type(union bpf_attr
*attr
)
1915 switch (attr
->prog_type
) {
1916 case BPF_PROG_TYPE_CGROUP_SOCK
:
1917 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1918 * exist so checking for non-zero is the way to go here.
1920 if (!attr
->expected_attach_type
)
1921 attr
->expected_attach_type
=
1922 BPF_CGROUP_INET_SOCK_CREATE
;
1928 bpf_prog_load_check_attach(enum bpf_prog_type prog_type
,
1929 enum bpf_attach_type expected_attach_type
,
1930 u32 btf_id
, u32 prog_fd
)
1933 if (btf_id
> BTF_MAX_TYPE
)
1936 switch (prog_type
) {
1937 case BPF_PROG_TYPE_TRACING
:
1938 case BPF_PROG_TYPE_STRUCT_OPS
:
1939 case BPF_PROG_TYPE_EXT
:
1946 if (prog_fd
&& prog_type
!= BPF_PROG_TYPE_TRACING
&&
1947 prog_type
!= BPF_PROG_TYPE_EXT
)
1950 switch (prog_type
) {
1951 case BPF_PROG_TYPE_CGROUP_SOCK
:
1952 switch (expected_attach_type
) {
1953 case BPF_CGROUP_INET_SOCK_CREATE
:
1954 case BPF_CGROUP_INET4_POST_BIND
:
1955 case BPF_CGROUP_INET6_POST_BIND
:
1960 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR
:
1961 switch (expected_attach_type
) {
1962 case BPF_CGROUP_INET4_BIND
:
1963 case BPF_CGROUP_INET6_BIND
:
1964 case BPF_CGROUP_INET4_CONNECT
:
1965 case BPF_CGROUP_INET6_CONNECT
:
1966 case BPF_CGROUP_UDP4_SENDMSG
:
1967 case BPF_CGROUP_UDP6_SENDMSG
:
1968 case BPF_CGROUP_UDP4_RECVMSG
:
1969 case BPF_CGROUP_UDP6_RECVMSG
:
1974 case BPF_PROG_TYPE_CGROUP_SKB
:
1975 switch (expected_attach_type
) {
1976 case BPF_CGROUP_INET_INGRESS
:
1977 case BPF_CGROUP_INET_EGRESS
:
1982 case BPF_PROG_TYPE_CGROUP_SOCKOPT
:
1983 switch (expected_attach_type
) {
1984 case BPF_CGROUP_SETSOCKOPT
:
1985 case BPF_CGROUP_GETSOCKOPT
:
1990 case BPF_PROG_TYPE_EXT
:
1991 if (expected_attach_type
)
1999 /* last field in 'union bpf_attr' used by this command */
2000 #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
2002 static int bpf_prog_load(union bpf_attr
*attr
, union bpf_attr __user
*uattr
)
2004 enum bpf_prog_type type
= attr
->prog_type
;
2005 struct bpf_prog
*prog
;
2010 if (CHECK_ATTR(BPF_PROG_LOAD
))
2013 if (attr
->prog_flags
& ~(BPF_F_STRICT_ALIGNMENT
|
2014 BPF_F_ANY_ALIGNMENT
|
2015 BPF_F_TEST_STATE_FREQ
|
2016 BPF_F_TEST_RND_HI32
))
2019 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
) &&
2020 (attr
->prog_flags
& BPF_F_ANY_ALIGNMENT
) &&
2021 !capable(CAP_SYS_ADMIN
))
2024 /* copy eBPF program license from user space */
2025 if (strncpy_from_user(license
, u64_to_user_ptr(attr
->license
),
2026 sizeof(license
) - 1) < 0)
2028 license
[sizeof(license
) - 1] = 0;
2030 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2031 is_gpl
= license_is_gpl_compatible(license
);
2033 if (attr
->insn_cnt
== 0 ||
2034 attr
->insn_cnt
> (capable(CAP_SYS_ADMIN
) ? BPF_COMPLEXITY_LIMIT_INSNS
: BPF_MAXINSNS
))
2036 if (type
!= BPF_PROG_TYPE_SOCKET_FILTER
&&
2037 type
!= BPF_PROG_TYPE_CGROUP_SKB
&&
2038 !capable(CAP_SYS_ADMIN
))
2041 bpf_prog_load_fixup_attach_type(attr
);
2042 if (bpf_prog_load_check_attach(type
, attr
->expected_attach_type
,
2043 attr
->attach_btf_id
,
2044 attr
->attach_prog_fd
))
2047 /* plain bpf_prog allocation */
2048 prog
= bpf_prog_alloc(bpf_prog_size(attr
->insn_cnt
), GFP_USER
);
2052 prog
->expected_attach_type
= attr
->expected_attach_type
;
2053 prog
->aux
->attach_btf_id
= attr
->attach_btf_id
;
2054 if (attr
->attach_prog_fd
) {
2055 struct bpf_prog
*tgt_prog
;
2057 tgt_prog
= bpf_prog_get(attr
->attach_prog_fd
);
2058 if (IS_ERR(tgt_prog
)) {
2059 err
= PTR_ERR(tgt_prog
);
2060 goto free_prog_nouncharge
;
2062 prog
->aux
->linked_prog
= tgt_prog
;
2065 prog
->aux
->offload_requested
= !!attr
->prog_ifindex
;
2067 err
= security_bpf_prog_alloc(prog
->aux
);
2069 goto free_prog_nouncharge
;
2071 err
= bpf_prog_charge_memlock(prog
);
2075 prog
->len
= attr
->insn_cnt
;
2078 if (copy_from_user(prog
->insns
, u64_to_user_ptr(attr
->insns
),
2079 bpf_prog_insn_size(prog
)) != 0)
2082 prog
->orig_prog
= NULL
;
2085 atomic64_set(&prog
->aux
->refcnt
, 1);
2086 prog
->gpl_compatible
= is_gpl
? 1 : 0;
2088 if (bpf_prog_is_dev_bound(prog
->aux
)) {
2089 err
= bpf_prog_offload_init(prog
, attr
);
2094 /* find program type: socket_filter vs tracing_filter */
2095 err
= find_prog_type(type
, prog
);
2099 prog
->aux
->load_time
= ktime_get_boottime_ns();
2100 err
= bpf_obj_name_cpy(prog
->aux
->name
, attr
->prog_name
);
2104 /* run eBPF verifier */
2105 err
= bpf_check(&prog
, attr
, uattr
);
2107 goto free_used_maps
;
2109 prog
= bpf_prog_select_runtime(prog
, &err
);
2111 goto free_used_maps
;
2113 err
= bpf_prog_alloc_id(prog
);
2115 goto free_used_maps
;
2117 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2118 * effectively publicly exposed. However, retrieving via
2119 * bpf_prog_get_fd_by_id() will take another reference,
2120 * therefore it cannot be gone underneath us.
2122 * Only for the time /after/ successful bpf_prog_new_fd()
2123 * and before returning to userspace, we might just hold
2124 * one reference and any parallel close on that fd could
2125 * rip everything out. Hence, below notifications must
2126 * happen before bpf_prog_new_fd().
2128 * Also, any failure handling from this point onwards must
2129 * be using bpf_prog_put() given the program is exposed.
2131 bpf_prog_kallsyms_add(prog
);
2132 perf_event_bpf_event(prog
, PERF_BPF_EVENT_PROG_LOAD
, 0);
2133 bpf_audit_prog(prog
, BPF_AUDIT_LOAD
);
2135 err
= bpf_prog_new_fd(prog
);
2141 /* In case we have subprogs, we need to wait for a grace
2142 * period before we can tear down JIT memory since symbols
2143 * are already exposed under kallsyms.
2145 __bpf_prog_put_noref(prog
, prog
->aux
->func_cnt
);
2148 bpf_prog_uncharge_memlock(prog
);
2150 security_bpf_prog_free(prog
->aux
);
2151 free_prog_nouncharge
:
2152 bpf_prog_free(prog
);
2156 #define BPF_OBJ_LAST_FIELD file_flags
2158 static int bpf_obj_pin(const union bpf_attr
*attr
)
2160 if (CHECK_ATTR(BPF_OBJ
) || attr
->file_flags
!= 0)
2163 return bpf_obj_pin_user(attr
->bpf_fd
, u64_to_user_ptr(attr
->pathname
));
2166 static int bpf_obj_get(const union bpf_attr
*attr
)
2168 if (CHECK_ATTR(BPF_OBJ
) || attr
->bpf_fd
!= 0 ||
2169 attr
->file_flags
& ~BPF_OBJ_FLAG_MASK
)
2172 return bpf_obj_get_user(u64_to_user_ptr(attr
->pathname
),
2178 const struct bpf_link_ops
*ops
;
2179 struct bpf_prog
*prog
;
2180 struct work_struct work
;
2183 void bpf_link_init(struct bpf_link
*link
, const struct bpf_link_ops
*ops
,
2184 struct bpf_prog
*prog
)
2186 atomic64_set(&link
->refcnt
, 1);
2191 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2192 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2193 * anon_inode's release() call. This helper manages marking bpf_link as
2194 * defunct, releases anon_inode file and puts reserved FD.
2196 static void bpf_link_cleanup(struct bpf_link
*link
, struct file
*link_file
,
2201 put_unused_fd(link_fd
);
2204 void bpf_link_inc(struct bpf_link
*link
)
2206 atomic64_inc(&link
->refcnt
);
2209 /* bpf_link_free is guaranteed to be called from process context */
2210 static void bpf_link_free(struct bpf_link
*link
)
2213 /* detach BPF program, clean up used resources */
2214 link
->ops
->release(link
);
2215 bpf_prog_put(link
->prog
);
2217 /* free bpf_link and its containing memory */
2218 link
->ops
->dealloc(link
);
2221 static void bpf_link_put_deferred(struct work_struct
*work
)
2223 struct bpf_link
*link
= container_of(work
, struct bpf_link
, work
);
2225 bpf_link_free(link
);
2228 /* bpf_link_put can be called from atomic context, but ensures that resources
2229 * are freed from process context
2231 void bpf_link_put(struct bpf_link
*link
)
2233 if (!atomic64_dec_and_test(&link
->refcnt
))
2237 INIT_WORK(&link
->work
, bpf_link_put_deferred
);
2238 schedule_work(&link
->work
);
2240 bpf_link_free(link
);
2244 static int bpf_link_release(struct inode
*inode
, struct file
*filp
)
2246 struct bpf_link
*link
= filp
->private_data
;
2252 #ifdef CONFIG_PROC_FS
2253 static const struct bpf_link_ops bpf_raw_tp_lops
;
2254 static const struct bpf_link_ops bpf_tracing_link_lops
;
2255 static const struct bpf_link_ops bpf_xdp_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";
2269 link_type
= "unknown";
2271 bin2hex(prog_tag
, prog
->tag
, sizeof(prog
->tag
));
2282 const struct file_operations bpf_link_fops
= {
2283 #ifdef CONFIG_PROC_FS
2284 .show_fdinfo
= bpf_link_show_fdinfo
,
2286 .release
= bpf_link_release
,
2287 .read
= bpf_dummy_read
,
2288 .write
= bpf_dummy_write
,
2291 int bpf_link_new_fd(struct bpf_link
*link
)
2293 return anon_inode_getfd("bpf-link", &bpf_link_fops
, link
, O_CLOEXEC
);
2296 /* Similar to bpf_link_new_fd, create anon_inode for given bpf_link, but
2297 * instead of immediately installing fd in fdtable, just reserve it and
2298 * return. Caller then need to either install it with fd_install(fd, file) or
2299 * release with put_unused_fd(fd).
2300 * This is useful for cases when bpf_link attachment/detachment are
2301 * complicated and expensive operations and should be delayed until all the fd
2302 * reservation and anon_inode creation succeeds.
2304 struct file
*bpf_link_new_file(struct bpf_link
*link
, int *reserved_fd
)
2309 fd
= get_unused_fd_flags(O_CLOEXEC
);
2313 file
= anon_inode_getfile("bpf_link", &bpf_link_fops
, link
, O_CLOEXEC
);
2323 struct bpf_link
*bpf_link_get_from_fd(u32 ufd
)
2325 struct fd f
= fdget(ufd
);
2326 struct bpf_link
*link
;
2329 return ERR_PTR(-EBADF
);
2330 if (f
.file
->f_op
!= &bpf_link_fops
) {
2332 return ERR_PTR(-EINVAL
);
2335 link
= f
.file
->private_data
;
2342 struct bpf_tracing_link
{
2343 struct bpf_link link
;
2346 static void bpf_tracing_link_release(struct bpf_link
*link
)
2348 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link
->prog
));
2351 static void bpf_tracing_link_dealloc(struct bpf_link
*link
)
2353 struct bpf_tracing_link
*tr_link
=
2354 container_of(link
, struct bpf_tracing_link
, link
);
2359 static const struct bpf_link_ops bpf_tracing_link_lops
= {
2360 .release
= bpf_tracing_link_release
,
2361 .dealloc
= bpf_tracing_link_dealloc
,
2364 static int bpf_tracing_prog_attach(struct bpf_prog
*prog
)
2366 struct bpf_tracing_link
*link
;
2367 struct file
*link_file
;
2370 if (prog
->expected_attach_type
!= BPF_TRACE_FENTRY
&&
2371 prog
->expected_attach_type
!= BPF_TRACE_FEXIT
&&
2372 prog
->expected_attach_type
!= BPF_MODIFY_RETURN
&&
2373 prog
->type
!= BPF_PROG_TYPE_EXT
) {
2378 link
= kzalloc(sizeof(*link
), GFP_USER
);
2383 bpf_link_init(&link
->link
, &bpf_tracing_link_lops
, prog
);
2385 link_file
= bpf_link_new_file(&link
->link
, &link_fd
);
2386 if (IS_ERR(link_file
)) {
2388 err
= PTR_ERR(link_file
);
2392 err
= bpf_trampoline_link_prog(prog
);
2394 bpf_link_cleanup(&link
->link
, link_file
, link_fd
);
2398 fd_install(link_fd
, link_file
);
2406 struct bpf_raw_tp_link
{
2407 struct bpf_link link
;
2408 struct bpf_raw_event_map
*btp
;
2411 static void bpf_raw_tp_link_release(struct bpf_link
*link
)
2413 struct bpf_raw_tp_link
*raw_tp
=
2414 container_of(link
, struct bpf_raw_tp_link
, link
);
2416 bpf_probe_unregister(raw_tp
->btp
, raw_tp
->link
.prog
);
2417 bpf_put_raw_tracepoint(raw_tp
->btp
);
2420 static void bpf_raw_tp_link_dealloc(struct bpf_link
*link
)
2422 struct bpf_raw_tp_link
*raw_tp
=
2423 container_of(link
, struct bpf_raw_tp_link
, link
);
2428 static const struct bpf_link_ops bpf_raw_tp_lops
= {
2429 .release
= bpf_raw_tp_link_release
,
2430 .dealloc
= bpf_raw_tp_link_dealloc
,
2433 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2435 static int bpf_raw_tracepoint_open(const union bpf_attr
*attr
)
2437 struct bpf_raw_tp_link
*link
;
2438 struct bpf_raw_event_map
*btp
;
2439 struct file
*link_file
;
2440 struct bpf_prog
*prog
;
2441 const char *tp_name
;
2445 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN
))
2448 prog
= bpf_prog_get(attr
->raw_tracepoint
.prog_fd
);
2450 return PTR_ERR(prog
);
2452 if (prog
->type
!= BPF_PROG_TYPE_RAW_TRACEPOINT
&&
2453 prog
->type
!= BPF_PROG_TYPE_TRACING
&&
2454 prog
->type
!= BPF_PROG_TYPE_EXT
&&
2455 prog
->type
!= BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE
) {
2460 if (prog
->type
== BPF_PROG_TYPE_TRACING
||
2461 prog
->type
== BPF_PROG_TYPE_EXT
) {
2462 if (attr
->raw_tracepoint
.name
) {
2463 /* The attach point for this category of programs
2464 * should be specified via btf_id during program load.
2469 if (prog
->expected_attach_type
== BPF_TRACE_RAW_TP
)
2470 tp_name
= prog
->aux
->attach_func_name
;
2472 return bpf_tracing_prog_attach(prog
);
2474 if (strncpy_from_user(buf
,
2475 u64_to_user_ptr(attr
->raw_tracepoint
.name
),
2476 sizeof(buf
) - 1) < 0) {
2480 buf
[sizeof(buf
) - 1] = 0;
2484 btp
= bpf_get_raw_tracepoint(tp_name
);
2490 link
= kzalloc(sizeof(*link
), GFP_USER
);
2495 bpf_link_init(&link
->link
, &bpf_raw_tp_lops
, prog
);
2498 link_file
= bpf_link_new_file(&link
->link
, &link_fd
);
2499 if (IS_ERR(link_file
)) {
2501 err
= PTR_ERR(link_file
);
2505 err
= bpf_probe_register(link
->btp
, prog
);
2507 bpf_link_cleanup(&link
->link
, link_file
, link_fd
);
2511 fd_install(link_fd
, link_file
);
2515 bpf_put_raw_tracepoint(btp
);
2521 static int bpf_prog_attach_check_attach_type(const struct bpf_prog
*prog
,
2522 enum bpf_attach_type attach_type
)
2524 switch (prog
->type
) {
2525 case BPF_PROG_TYPE_CGROUP_SOCK
:
2526 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR
:
2527 case BPF_PROG_TYPE_CGROUP_SOCKOPT
:
2528 return attach_type
== prog
->expected_attach_type
? 0 : -EINVAL
;
2529 case BPF_PROG_TYPE_CGROUP_SKB
:
2530 return prog
->enforce_expected_attach_type
&&
2531 prog
->expected_attach_type
!= attach_type
?
2538 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
2540 #define BPF_F_ATTACH_MASK \
2541 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
2543 static int bpf_prog_attach(const union bpf_attr
*attr
)
2545 enum bpf_prog_type ptype
;
2546 struct bpf_prog
*prog
;
2549 if (!capable(CAP_NET_ADMIN
))
2552 if (CHECK_ATTR(BPF_PROG_ATTACH
))
2555 if (attr
->attach_flags
& ~BPF_F_ATTACH_MASK
)
2558 switch (attr
->attach_type
) {
2559 case BPF_CGROUP_INET_INGRESS
:
2560 case BPF_CGROUP_INET_EGRESS
:
2561 ptype
= BPF_PROG_TYPE_CGROUP_SKB
;
2563 case BPF_CGROUP_INET_SOCK_CREATE
:
2564 case BPF_CGROUP_INET4_POST_BIND
:
2565 case BPF_CGROUP_INET6_POST_BIND
:
2566 ptype
= BPF_PROG_TYPE_CGROUP_SOCK
;
2568 case BPF_CGROUP_INET4_BIND
:
2569 case BPF_CGROUP_INET6_BIND
:
2570 case BPF_CGROUP_INET4_CONNECT
:
2571 case BPF_CGROUP_INET6_CONNECT
:
2572 case BPF_CGROUP_UDP4_SENDMSG
:
2573 case BPF_CGROUP_UDP6_SENDMSG
:
2574 case BPF_CGROUP_UDP4_RECVMSG
:
2575 case BPF_CGROUP_UDP6_RECVMSG
:
2576 ptype
= BPF_PROG_TYPE_CGROUP_SOCK_ADDR
;
2578 case BPF_CGROUP_SOCK_OPS
:
2579 ptype
= BPF_PROG_TYPE_SOCK_OPS
;
2581 case BPF_CGROUP_DEVICE
:
2582 ptype
= BPF_PROG_TYPE_CGROUP_DEVICE
;
2584 case BPF_SK_MSG_VERDICT
:
2585 ptype
= BPF_PROG_TYPE_SK_MSG
;
2587 case BPF_SK_SKB_STREAM_PARSER
:
2588 case BPF_SK_SKB_STREAM_VERDICT
:
2589 ptype
= BPF_PROG_TYPE_SK_SKB
;
2591 case BPF_LIRC_MODE2
:
2592 ptype
= BPF_PROG_TYPE_LIRC_MODE2
;
2594 case BPF_FLOW_DISSECTOR
:
2595 ptype
= BPF_PROG_TYPE_FLOW_DISSECTOR
;
2597 case BPF_CGROUP_SYSCTL
:
2598 ptype
= BPF_PROG_TYPE_CGROUP_SYSCTL
;
2600 case BPF_CGROUP_GETSOCKOPT
:
2601 case BPF_CGROUP_SETSOCKOPT
:
2602 ptype
= BPF_PROG_TYPE_CGROUP_SOCKOPT
;
2608 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
, ptype
);
2610 return PTR_ERR(prog
);
2612 if (bpf_prog_attach_check_attach_type(prog
, attr
->attach_type
)) {
2618 case BPF_PROG_TYPE_SK_SKB
:
2619 case BPF_PROG_TYPE_SK_MSG
:
2620 ret
= sock_map_get_from_fd(attr
, prog
);
2622 case BPF_PROG_TYPE_LIRC_MODE2
:
2623 ret
= lirc_prog_attach(attr
, prog
);
2625 case BPF_PROG_TYPE_FLOW_DISSECTOR
:
2626 ret
= skb_flow_dissector_bpf_prog_attach(attr
, prog
);
2629 ret
= cgroup_bpf_prog_attach(attr
, ptype
, prog
);
2637 #define BPF_PROG_DETACH_LAST_FIELD attach_type
2639 static int bpf_prog_detach(const union bpf_attr
*attr
)
2641 enum bpf_prog_type ptype
;
2643 if (!capable(CAP_NET_ADMIN
))
2646 if (CHECK_ATTR(BPF_PROG_DETACH
))
2649 switch (attr
->attach_type
) {
2650 case BPF_CGROUP_INET_INGRESS
:
2651 case BPF_CGROUP_INET_EGRESS
:
2652 ptype
= BPF_PROG_TYPE_CGROUP_SKB
;
2654 case BPF_CGROUP_INET_SOCK_CREATE
:
2655 case BPF_CGROUP_INET4_POST_BIND
:
2656 case BPF_CGROUP_INET6_POST_BIND
:
2657 ptype
= BPF_PROG_TYPE_CGROUP_SOCK
;
2659 case BPF_CGROUP_INET4_BIND
:
2660 case BPF_CGROUP_INET6_BIND
:
2661 case BPF_CGROUP_INET4_CONNECT
:
2662 case BPF_CGROUP_INET6_CONNECT
:
2663 case BPF_CGROUP_UDP4_SENDMSG
:
2664 case BPF_CGROUP_UDP6_SENDMSG
:
2665 case BPF_CGROUP_UDP4_RECVMSG
:
2666 case BPF_CGROUP_UDP6_RECVMSG
:
2667 ptype
= BPF_PROG_TYPE_CGROUP_SOCK_ADDR
;
2669 case BPF_CGROUP_SOCK_OPS
:
2670 ptype
= BPF_PROG_TYPE_SOCK_OPS
;
2672 case BPF_CGROUP_DEVICE
:
2673 ptype
= BPF_PROG_TYPE_CGROUP_DEVICE
;
2675 case BPF_SK_MSG_VERDICT
:
2676 return sock_map_get_from_fd(attr
, NULL
);
2677 case BPF_SK_SKB_STREAM_PARSER
:
2678 case BPF_SK_SKB_STREAM_VERDICT
:
2679 return sock_map_get_from_fd(attr
, NULL
);
2680 case BPF_LIRC_MODE2
:
2681 return lirc_prog_detach(attr
);
2682 case BPF_FLOW_DISSECTOR
:
2683 return skb_flow_dissector_bpf_prog_detach(attr
);
2684 case BPF_CGROUP_SYSCTL
:
2685 ptype
= BPF_PROG_TYPE_CGROUP_SYSCTL
;
2687 case BPF_CGROUP_GETSOCKOPT
:
2688 case BPF_CGROUP_SETSOCKOPT
:
2689 ptype
= BPF_PROG_TYPE_CGROUP_SOCKOPT
;
2695 return cgroup_bpf_prog_detach(attr
, ptype
);
2698 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2700 static int bpf_prog_query(const union bpf_attr
*attr
,
2701 union bpf_attr __user
*uattr
)
2703 if (!capable(CAP_NET_ADMIN
))
2705 if (CHECK_ATTR(BPF_PROG_QUERY
))
2707 if (attr
->query
.query_flags
& ~BPF_F_QUERY_EFFECTIVE
)
2710 switch (attr
->query
.attach_type
) {
2711 case BPF_CGROUP_INET_INGRESS
:
2712 case BPF_CGROUP_INET_EGRESS
:
2713 case BPF_CGROUP_INET_SOCK_CREATE
:
2714 case BPF_CGROUP_INET4_BIND
:
2715 case BPF_CGROUP_INET6_BIND
:
2716 case BPF_CGROUP_INET4_POST_BIND
:
2717 case BPF_CGROUP_INET6_POST_BIND
:
2718 case BPF_CGROUP_INET4_CONNECT
:
2719 case BPF_CGROUP_INET6_CONNECT
:
2720 case BPF_CGROUP_UDP4_SENDMSG
:
2721 case BPF_CGROUP_UDP6_SENDMSG
:
2722 case BPF_CGROUP_UDP4_RECVMSG
:
2723 case BPF_CGROUP_UDP6_RECVMSG
:
2724 case BPF_CGROUP_SOCK_OPS
:
2725 case BPF_CGROUP_DEVICE
:
2726 case BPF_CGROUP_SYSCTL
:
2727 case BPF_CGROUP_GETSOCKOPT
:
2728 case BPF_CGROUP_SETSOCKOPT
:
2730 case BPF_LIRC_MODE2
:
2731 return lirc_prog_query(attr
, uattr
);
2732 case BPF_FLOW_DISSECTOR
:
2733 return skb_flow_dissector_prog_query(attr
, uattr
);
2738 return cgroup_bpf_prog_query(attr
, uattr
);
2741 #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
2743 static int bpf_prog_test_run(const union bpf_attr
*attr
,
2744 union bpf_attr __user
*uattr
)
2746 struct bpf_prog
*prog
;
2747 int ret
= -ENOTSUPP
;
2749 if (!capable(CAP_SYS_ADMIN
))
2751 if (CHECK_ATTR(BPF_PROG_TEST_RUN
))
2754 if ((attr
->test
.ctx_size_in
&& !attr
->test
.ctx_in
) ||
2755 (!attr
->test
.ctx_size_in
&& attr
->test
.ctx_in
))
2758 if ((attr
->test
.ctx_size_out
&& !attr
->test
.ctx_out
) ||
2759 (!attr
->test
.ctx_size_out
&& attr
->test
.ctx_out
))
2762 prog
= bpf_prog_get(attr
->test
.prog_fd
);
2764 return PTR_ERR(prog
);
2766 if (prog
->aux
->ops
->test_run
)
2767 ret
= prog
->aux
->ops
->test_run(prog
, attr
, uattr
);
2773 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2775 static int bpf_obj_get_next_id(const union bpf_attr
*attr
,
2776 union bpf_attr __user
*uattr
,
2780 u32 next_id
= attr
->start_id
;
2783 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID
) || next_id
>= INT_MAX
)
2786 if (!capable(CAP_SYS_ADMIN
))
2791 if (!idr_get_next(idr
, &next_id
))
2793 spin_unlock_bh(lock
);
2796 err
= put_user(next_id
, &uattr
->next_id
);
2801 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2803 struct bpf_prog
*bpf_prog_by_id(u32 id
)
2805 struct bpf_prog
*prog
;
2808 return ERR_PTR(-ENOENT
);
2810 spin_lock_bh(&prog_idr_lock
);
2811 prog
= idr_find(&prog_idr
, id
);
2813 prog
= bpf_prog_inc_not_zero(prog
);
2815 prog
= ERR_PTR(-ENOENT
);
2816 spin_unlock_bh(&prog_idr_lock
);
2820 static int bpf_prog_get_fd_by_id(const union bpf_attr
*attr
)
2822 struct bpf_prog
*prog
;
2823 u32 id
= attr
->prog_id
;
2826 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID
))
2829 if (!capable(CAP_SYS_ADMIN
))
2832 prog
= bpf_prog_by_id(id
);
2834 return PTR_ERR(prog
);
2836 fd
= bpf_prog_new_fd(prog
);
2843 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2845 static int bpf_map_get_fd_by_id(const union bpf_attr
*attr
)
2847 struct bpf_map
*map
;
2848 u32 id
= attr
->map_id
;
2852 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID
) ||
2853 attr
->open_flags
& ~BPF_OBJ_FLAG_MASK
)
2856 if (!capable(CAP_SYS_ADMIN
))
2859 f_flags
= bpf_get_file_flag(attr
->open_flags
);
2863 spin_lock_bh(&map_idr_lock
);
2864 map
= idr_find(&map_idr
, id
);
2866 map
= __bpf_map_inc_not_zero(map
, true);
2868 map
= ERR_PTR(-ENOENT
);
2869 spin_unlock_bh(&map_idr_lock
);
2872 return PTR_ERR(map
);
2874 fd
= bpf_map_new_fd(map
, f_flags
);
2876 bpf_map_put_with_uref(map
);
2881 static const struct bpf_map
*bpf_map_from_imm(const struct bpf_prog
*prog
,
2882 unsigned long addr
, u32
*off
,
2885 const struct bpf_map
*map
;
2888 for (i
= 0, *off
= 0; i
< prog
->aux
->used_map_cnt
; i
++) {
2889 map
= prog
->aux
->used_maps
[i
];
2890 if (map
== (void *)addr
) {
2891 *type
= BPF_PSEUDO_MAP_FD
;
2894 if (!map
->ops
->map_direct_value_meta
)
2896 if (!map
->ops
->map_direct_value_meta(map
, addr
, off
)) {
2897 *type
= BPF_PSEUDO_MAP_VALUE
;
2905 static struct bpf_insn
*bpf_insn_prepare_dump(const struct bpf_prog
*prog
)
2907 const struct bpf_map
*map
;
2908 struct bpf_insn
*insns
;
2913 insns
= kmemdup(prog
->insnsi
, bpf_prog_insn_size(prog
),
2918 for (i
= 0; i
< prog
->len
; i
++) {
2919 if (insns
[i
].code
== (BPF_JMP
| BPF_TAIL_CALL
)) {
2920 insns
[i
].code
= BPF_JMP
| BPF_CALL
;
2921 insns
[i
].imm
= BPF_FUNC_tail_call
;
2924 if (insns
[i
].code
== (BPF_JMP
| BPF_CALL
) ||
2925 insns
[i
].code
== (BPF_JMP
| BPF_CALL_ARGS
)) {
2926 if (insns
[i
].code
== (BPF_JMP
| BPF_CALL_ARGS
))
2927 insns
[i
].code
= BPF_JMP
| BPF_CALL
;
2928 if (!bpf_dump_raw_ok())
2933 if (insns
[i
].code
!= (BPF_LD
| BPF_IMM
| BPF_DW
))
2936 imm
= ((u64
)insns
[i
+ 1].imm
<< 32) | (u32
)insns
[i
].imm
;
2937 map
= bpf_map_from_imm(prog
, imm
, &off
, &type
);
2939 insns
[i
].src_reg
= type
;
2940 insns
[i
].imm
= map
->id
;
2941 insns
[i
+ 1].imm
= off
;
2949 static int set_info_rec_size(struct bpf_prog_info
*info
)
2952 * Ensure info.*_rec_size is the same as kernel expected size
2956 * Only allow zero *_rec_size if both _rec_size and _cnt are
2957 * zero. In this case, the kernel will set the expected
2958 * _rec_size back to the info.
2961 if ((info
->nr_func_info
|| info
->func_info_rec_size
) &&
2962 info
->func_info_rec_size
!= sizeof(struct bpf_func_info
))
2965 if ((info
->nr_line_info
|| info
->line_info_rec_size
) &&
2966 info
->line_info_rec_size
!= sizeof(struct bpf_line_info
))
2969 if ((info
->nr_jited_line_info
|| info
->jited_line_info_rec_size
) &&
2970 info
->jited_line_info_rec_size
!= sizeof(__u64
))
2973 info
->func_info_rec_size
= sizeof(struct bpf_func_info
);
2974 info
->line_info_rec_size
= sizeof(struct bpf_line_info
);
2975 info
->jited_line_info_rec_size
= sizeof(__u64
);
2980 static int bpf_prog_get_info_by_fd(struct bpf_prog
*prog
,
2981 const union bpf_attr
*attr
,
2982 union bpf_attr __user
*uattr
)
2984 struct bpf_prog_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
2985 struct bpf_prog_info info
= {};
2986 u32 info_len
= attr
->info
.info_len
;
2987 struct bpf_prog_stats stats
;
2988 char __user
*uinsns
;
2992 err
= bpf_check_uarg_tail_zero(uinfo
, sizeof(info
), info_len
);
2995 info_len
= min_t(u32
, sizeof(info
), info_len
);
2997 if (copy_from_user(&info
, uinfo
, info_len
))
3000 info
.type
= prog
->type
;
3001 info
.id
= prog
->aux
->id
;
3002 info
.load_time
= prog
->aux
->load_time
;
3003 info
.created_by_uid
= from_kuid_munged(current_user_ns(),
3004 prog
->aux
->user
->uid
);
3005 info
.gpl_compatible
= prog
->gpl_compatible
;
3007 memcpy(info
.tag
, prog
->tag
, sizeof(prog
->tag
));
3008 memcpy(info
.name
, prog
->aux
->name
, sizeof(prog
->aux
->name
));
3010 ulen
= info
.nr_map_ids
;
3011 info
.nr_map_ids
= prog
->aux
->used_map_cnt
;
3012 ulen
= min_t(u32
, info
.nr_map_ids
, ulen
);
3014 u32 __user
*user_map_ids
= u64_to_user_ptr(info
.map_ids
);
3017 for (i
= 0; i
< ulen
; i
++)
3018 if (put_user(prog
->aux
->used_maps
[i
]->id
,
3023 err
= set_info_rec_size(&info
);
3027 bpf_prog_get_stats(prog
, &stats
);
3028 info
.run_time_ns
= stats
.nsecs
;
3029 info
.run_cnt
= stats
.cnt
;
3031 if (!capable(CAP_SYS_ADMIN
)) {
3032 info
.jited_prog_len
= 0;
3033 info
.xlated_prog_len
= 0;
3034 info
.nr_jited_ksyms
= 0;
3035 info
.nr_jited_func_lens
= 0;
3036 info
.nr_func_info
= 0;
3037 info
.nr_line_info
= 0;
3038 info
.nr_jited_line_info
= 0;
3042 ulen
= info
.xlated_prog_len
;
3043 info
.xlated_prog_len
= bpf_prog_insn_size(prog
);
3044 if (info
.xlated_prog_len
&& ulen
) {
3045 struct bpf_insn
*insns_sanitized
;
3048 if (prog
->blinded
&& !bpf_dump_raw_ok()) {
3049 info
.xlated_prog_insns
= 0;
3052 insns_sanitized
= bpf_insn_prepare_dump(prog
);
3053 if (!insns_sanitized
)
3055 uinsns
= u64_to_user_ptr(info
.xlated_prog_insns
);
3056 ulen
= min_t(u32
, info
.xlated_prog_len
, ulen
);
3057 fault
= copy_to_user(uinsns
, insns_sanitized
, ulen
);
3058 kfree(insns_sanitized
);
3063 if (bpf_prog_is_dev_bound(prog
->aux
)) {
3064 err
= bpf_prog_offload_info_fill(&info
, prog
);
3070 /* NOTE: the following code is supposed to be skipped for offload.
3071 * bpf_prog_offload_info_fill() is the place to fill similar fields
3074 ulen
= info
.jited_prog_len
;
3075 if (prog
->aux
->func_cnt
) {
3078 info
.jited_prog_len
= 0;
3079 for (i
= 0; i
< prog
->aux
->func_cnt
; i
++)
3080 info
.jited_prog_len
+= prog
->aux
->func
[i
]->jited_len
;
3082 info
.jited_prog_len
= prog
->jited_len
;
3085 if (info
.jited_prog_len
&& ulen
) {
3086 if (bpf_dump_raw_ok()) {
3087 uinsns
= u64_to_user_ptr(info
.jited_prog_insns
);
3088 ulen
= min_t(u32
, info
.jited_prog_len
, ulen
);
3090 /* for multi-function programs, copy the JITed
3091 * instructions for all the functions
3093 if (prog
->aux
->func_cnt
) {
3098 for (i
= 0; i
< prog
->aux
->func_cnt
; i
++) {
3099 len
= prog
->aux
->func
[i
]->jited_len
;
3100 len
= min_t(u32
, len
, free
);
3101 img
= (u8
*) prog
->aux
->func
[i
]->bpf_func
;
3102 if (copy_to_user(uinsns
, img
, len
))
3110 if (copy_to_user(uinsns
, prog
->bpf_func
, ulen
))
3114 info
.jited_prog_insns
= 0;
3118 ulen
= info
.nr_jited_ksyms
;
3119 info
.nr_jited_ksyms
= prog
->aux
->func_cnt
? : 1;
3121 if (bpf_dump_raw_ok()) {
3122 unsigned long ksym_addr
;
3123 u64 __user
*user_ksyms
;
3126 /* copy the address of the kernel symbol
3127 * corresponding to each function
3129 ulen
= min_t(u32
, info
.nr_jited_ksyms
, ulen
);
3130 user_ksyms
= u64_to_user_ptr(info
.jited_ksyms
);
3131 if (prog
->aux
->func_cnt
) {
3132 for (i
= 0; i
< ulen
; i
++) {
3133 ksym_addr
= (unsigned long)
3134 prog
->aux
->func
[i
]->bpf_func
;
3135 if (put_user((u64
) ksym_addr
,
3140 ksym_addr
= (unsigned long) prog
->bpf_func
;
3141 if (put_user((u64
) ksym_addr
, &user_ksyms
[0]))
3145 info
.jited_ksyms
= 0;
3149 ulen
= info
.nr_jited_func_lens
;
3150 info
.nr_jited_func_lens
= prog
->aux
->func_cnt
? : 1;
3152 if (bpf_dump_raw_ok()) {
3153 u32 __user
*user_lens
;
3156 /* copy the JITed image lengths for each function */
3157 ulen
= min_t(u32
, info
.nr_jited_func_lens
, ulen
);
3158 user_lens
= u64_to_user_ptr(info
.jited_func_lens
);
3159 if (prog
->aux
->func_cnt
) {
3160 for (i
= 0; i
< ulen
; i
++) {
3162 prog
->aux
->func
[i
]->jited_len
;
3163 if (put_user(func_len
, &user_lens
[i
]))
3167 func_len
= prog
->jited_len
;
3168 if (put_user(func_len
, &user_lens
[0]))
3172 info
.jited_func_lens
= 0;
3177 info
.btf_id
= btf_id(prog
->aux
->btf
);
3179 ulen
= info
.nr_func_info
;
3180 info
.nr_func_info
= prog
->aux
->func_info_cnt
;
3181 if (info
.nr_func_info
&& ulen
) {
3182 char __user
*user_finfo
;
3184 user_finfo
= u64_to_user_ptr(info
.func_info
);
3185 ulen
= min_t(u32
, info
.nr_func_info
, ulen
);
3186 if (copy_to_user(user_finfo
, prog
->aux
->func_info
,
3187 info
.func_info_rec_size
* ulen
))
3191 ulen
= info
.nr_line_info
;
3192 info
.nr_line_info
= prog
->aux
->nr_linfo
;
3193 if (info
.nr_line_info
&& ulen
) {
3194 __u8 __user
*user_linfo
;
3196 user_linfo
= u64_to_user_ptr(info
.line_info
);
3197 ulen
= min_t(u32
, info
.nr_line_info
, ulen
);
3198 if (copy_to_user(user_linfo
, prog
->aux
->linfo
,
3199 info
.line_info_rec_size
* ulen
))
3203 ulen
= info
.nr_jited_line_info
;
3204 if (prog
->aux
->jited_linfo
)
3205 info
.nr_jited_line_info
= prog
->aux
->nr_linfo
;
3207 info
.nr_jited_line_info
= 0;
3208 if (info
.nr_jited_line_info
&& ulen
) {
3209 if (bpf_dump_raw_ok()) {
3210 __u64 __user
*user_linfo
;
3213 user_linfo
= u64_to_user_ptr(info
.jited_line_info
);
3214 ulen
= min_t(u32
, info
.nr_jited_line_info
, ulen
);
3215 for (i
= 0; i
< ulen
; i
++) {
3216 if (put_user((__u64
)(long)prog
->aux
->jited_linfo
[i
],
3221 info
.jited_line_info
= 0;
3225 ulen
= info
.nr_prog_tags
;
3226 info
.nr_prog_tags
= prog
->aux
->func_cnt
? : 1;
3228 __u8
__user (*user_prog_tags
)[BPF_TAG_SIZE
];
3231 user_prog_tags
= u64_to_user_ptr(info
.prog_tags
);
3232 ulen
= min_t(u32
, info
.nr_prog_tags
, ulen
);
3233 if (prog
->aux
->func_cnt
) {
3234 for (i
= 0; i
< ulen
; i
++) {
3235 if (copy_to_user(user_prog_tags
[i
],
3236 prog
->aux
->func
[i
]->tag
,
3241 if (copy_to_user(user_prog_tags
[0],
3242 prog
->tag
, BPF_TAG_SIZE
))
3248 if (copy_to_user(uinfo
, &info
, info_len
) ||
3249 put_user(info_len
, &uattr
->info
.info_len
))
3255 static int bpf_map_get_info_by_fd(struct bpf_map
*map
,
3256 const union bpf_attr
*attr
,
3257 union bpf_attr __user
*uattr
)
3259 struct bpf_map_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
3260 struct bpf_map_info info
= {};
3261 u32 info_len
= attr
->info
.info_len
;
3264 err
= bpf_check_uarg_tail_zero(uinfo
, sizeof(info
), info_len
);
3267 info_len
= min_t(u32
, sizeof(info
), info_len
);
3269 info
.type
= map
->map_type
;
3271 info
.key_size
= map
->key_size
;
3272 info
.value_size
= map
->value_size
;
3273 info
.max_entries
= map
->max_entries
;
3274 info
.map_flags
= map
->map_flags
;
3275 memcpy(info
.name
, map
->name
, sizeof(map
->name
));
3278 info
.btf_id
= btf_id(map
->btf
);
3279 info
.btf_key_type_id
= map
->btf_key_type_id
;
3280 info
.btf_value_type_id
= map
->btf_value_type_id
;
3282 info
.btf_vmlinux_value_type_id
= map
->btf_vmlinux_value_type_id
;
3284 if (bpf_map_is_dev_bound(map
)) {
3285 err
= bpf_map_offload_info_fill(&info
, map
);
3290 if (copy_to_user(uinfo
, &info
, info_len
) ||
3291 put_user(info_len
, &uattr
->info
.info_len
))
3297 static int bpf_btf_get_info_by_fd(struct btf
*btf
,
3298 const union bpf_attr
*attr
,
3299 union bpf_attr __user
*uattr
)
3301 struct bpf_btf_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
3302 u32 info_len
= attr
->info
.info_len
;
3305 err
= bpf_check_uarg_tail_zero(uinfo
, sizeof(*uinfo
), info_len
);
3309 return btf_get_info_by_fd(btf
, attr
, uattr
);
3312 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3314 static int bpf_obj_get_info_by_fd(const union bpf_attr
*attr
,
3315 union bpf_attr __user
*uattr
)
3317 int ufd
= attr
->info
.bpf_fd
;
3321 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD
))
3328 if (f
.file
->f_op
== &bpf_prog_fops
)
3329 err
= bpf_prog_get_info_by_fd(f
.file
->private_data
, attr
,
3331 else if (f
.file
->f_op
== &bpf_map_fops
)
3332 err
= bpf_map_get_info_by_fd(f
.file
->private_data
, attr
,
3334 else if (f
.file
->f_op
== &btf_fops
)
3335 err
= bpf_btf_get_info_by_fd(f
.file
->private_data
, attr
, uattr
);
3343 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3345 static int bpf_btf_load(const union bpf_attr
*attr
)
3347 if (CHECK_ATTR(BPF_BTF_LOAD
))
3350 if (!capable(CAP_SYS_ADMIN
))
3353 return btf_new_fd(attr
);
3356 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3358 static int bpf_btf_get_fd_by_id(const union bpf_attr
*attr
)
3360 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID
))
3363 if (!capable(CAP_SYS_ADMIN
))
3366 return btf_get_fd_by_id(attr
->btf_id
);
3369 static int bpf_task_fd_query_copy(const union bpf_attr
*attr
,
3370 union bpf_attr __user
*uattr
,
3371 u32 prog_id
, u32 fd_type
,
3372 const char *buf
, u64 probe_offset
,
3375 char __user
*ubuf
= u64_to_user_ptr(attr
->task_fd_query
.buf
);
3376 u32 len
= buf
? strlen(buf
) : 0, input_len
;
3379 if (put_user(len
, &uattr
->task_fd_query
.buf_len
))
3381 input_len
= attr
->task_fd_query
.buf_len
;
3382 if (input_len
&& ubuf
) {
3384 /* nothing to copy, just make ubuf NULL terminated */
3387 if (put_user(zero
, ubuf
))
3389 } else if (input_len
>= len
+ 1) {
3390 /* ubuf can hold the string with NULL terminator */
3391 if (copy_to_user(ubuf
, buf
, len
+ 1))
3394 /* ubuf cannot hold the string with NULL terminator,
3395 * do a partial copy with NULL terminator.
3400 if (copy_to_user(ubuf
, buf
, input_len
- 1))
3402 if (put_user(zero
, ubuf
+ input_len
- 1))
3407 if (put_user(prog_id
, &uattr
->task_fd_query
.prog_id
) ||
3408 put_user(fd_type
, &uattr
->task_fd_query
.fd_type
) ||
3409 put_user(probe_offset
, &uattr
->task_fd_query
.probe_offset
) ||
3410 put_user(probe_addr
, &uattr
->task_fd_query
.probe_addr
))
3416 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3418 static int bpf_task_fd_query(const union bpf_attr
*attr
,
3419 union bpf_attr __user
*uattr
)
3421 pid_t pid
= attr
->task_fd_query
.pid
;
3422 u32 fd
= attr
->task_fd_query
.fd
;
3423 const struct perf_event
*event
;
3424 struct files_struct
*files
;
3425 struct task_struct
*task
;
3429 if (CHECK_ATTR(BPF_TASK_FD_QUERY
))
3432 if (!capable(CAP_SYS_ADMIN
))
3435 if (attr
->task_fd_query
.flags
!= 0)
3438 task
= get_pid_task(find_vpid(pid
), PIDTYPE_PID
);
3442 files
= get_files_struct(task
);
3443 put_task_struct(task
);
3448 spin_lock(&files
->file_lock
);
3449 file
= fcheck_files(files
, fd
);
3454 spin_unlock(&files
->file_lock
);
3455 put_files_struct(files
);
3460 if (file
->f_op
== &bpf_link_fops
) {
3461 struct bpf_link
*link
= file
->private_data
;
3463 if (link
->ops
== &bpf_raw_tp_lops
) {
3464 struct bpf_raw_tp_link
*raw_tp
=
3465 container_of(link
, struct bpf_raw_tp_link
, link
);
3466 struct bpf_raw_event_map
*btp
= raw_tp
->btp
;
3468 err
= bpf_task_fd_query_copy(attr
, uattr
,
3469 raw_tp
->link
.prog
->aux
->id
,
3470 BPF_FD_TYPE_RAW_TRACEPOINT
,
3471 btp
->tp
->name
, 0, 0);
3477 event
= perf_get_event(file
);
3478 if (!IS_ERR(event
)) {
3479 u64 probe_offset
, probe_addr
;
3480 u32 prog_id
, fd_type
;
3483 err
= bpf_get_perf_event_info(event
, &prog_id
, &fd_type
,
3484 &buf
, &probe_offset
,
3487 err
= bpf_task_fd_query_copy(attr
, uattr
, prog_id
,
3502 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
3504 #define BPF_DO_BATCH(fn) \
3510 err = fn(map, attr, uattr); \
3513 static int bpf_map_do_batch(const union bpf_attr
*attr
,
3514 union bpf_attr __user
*uattr
,
3517 struct bpf_map
*map
;
3521 if (CHECK_ATTR(BPF_MAP_BATCH
))
3524 ufd
= attr
->batch
.map_fd
;
3526 map
= __bpf_map_get(f
);
3528 return PTR_ERR(map
);
3530 if ((cmd
== BPF_MAP_LOOKUP_BATCH
||
3531 cmd
== BPF_MAP_LOOKUP_AND_DELETE_BATCH
) &&
3532 !(map_get_sys_perms(map
, f
) & FMODE_CAN_READ
)) {
3537 if (cmd
!= BPF_MAP_LOOKUP_BATCH
&&
3538 !(map_get_sys_perms(map
, f
) & FMODE_CAN_WRITE
)) {
3543 if (cmd
== BPF_MAP_LOOKUP_BATCH
)
3544 BPF_DO_BATCH(map
->ops
->map_lookup_batch
);
3545 else if (cmd
== BPF_MAP_LOOKUP_AND_DELETE_BATCH
)
3546 BPF_DO_BATCH(map
->ops
->map_lookup_and_delete_batch
);
3547 else if (cmd
== BPF_MAP_UPDATE_BATCH
)
3548 BPF_DO_BATCH(map
->ops
->map_update_batch
);
3550 BPF_DO_BATCH(map
->ops
->map_delete_batch
);
3557 SYSCALL_DEFINE3(bpf
, int, cmd
, union bpf_attr __user
*, uattr
, unsigned int, size
)
3559 union bpf_attr attr
= {};
3562 if (sysctl_unprivileged_bpf_disabled
&& !capable(CAP_SYS_ADMIN
))
3565 err
= bpf_check_uarg_tail_zero(uattr
, sizeof(attr
), size
);
3568 size
= min_t(u32
, size
, sizeof(attr
));
3570 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
3571 if (copy_from_user(&attr
, uattr
, size
) != 0)
3574 err
= security_bpf(cmd
, &attr
, size
);
3579 case BPF_MAP_CREATE
:
3580 err
= map_create(&attr
);
3582 case BPF_MAP_LOOKUP_ELEM
:
3583 err
= map_lookup_elem(&attr
);
3585 case BPF_MAP_UPDATE_ELEM
:
3586 err
= map_update_elem(&attr
);
3588 case BPF_MAP_DELETE_ELEM
:
3589 err
= map_delete_elem(&attr
);
3591 case BPF_MAP_GET_NEXT_KEY
:
3592 err
= map_get_next_key(&attr
);
3594 case BPF_MAP_FREEZE
:
3595 err
= map_freeze(&attr
);
3598 err
= bpf_prog_load(&attr
, uattr
);
3601 err
= bpf_obj_pin(&attr
);
3604 err
= bpf_obj_get(&attr
);
3606 case BPF_PROG_ATTACH
:
3607 err
= bpf_prog_attach(&attr
);
3609 case BPF_PROG_DETACH
:
3610 err
= bpf_prog_detach(&attr
);
3612 case BPF_PROG_QUERY
:
3613 err
= bpf_prog_query(&attr
, uattr
);
3615 case BPF_PROG_TEST_RUN
:
3616 err
= bpf_prog_test_run(&attr
, uattr
);
3618 case BPF_PROG_GET_NEXT_ID
:
3619 err
= bpf_obj_get_next_id(&attr
, uattr
,
3620 &prog_idr
, &prog_idr_lock
);
3622 case BPF_MAP_GET_NEXT_ID
:
3623 err
= bpf_obj_get_next_id(&attr
, uattr
,
3624 &map_idr
, &map_idr_lock
);
3626 case BPF_BTF_GET_NEXT_ID
:
3627 err
= bpf_obj_get_next_id(&attr
, uattr
,
3628 &btf_idr
, &btf_idr_lock
);
3630 case BPF_PROG_GET_FD_BY_ID
:
3631 err
= bpf_prog_get_fd_by_id(&attr
);
3633 case BPF_MAP_GET_FD_BY_ID
:
3634 err
= bpf_map_get_fd_by_id(&attr
);
3636 case BPF_OBJ_GET_INFO_BY_FD
:
3637 err
= bpf_obj_get_info_by_fd(&attr
, uattr
);
3639 case BPF_RAW_TRACEPOINT_OPEN
:
3640 err
= bpf_raw_tracepoint_open(&attr
);
3643 err
= bpf_btf_load(&attr
);
3645 case BPF_BTF_GET_FD_BY_ID
:
3646 err
= bpf_btf_get_fd_by_id(&attr
);
3648 case BPF_TASK_FD_QUERY
:
3649 err
= bpf_task_fd_query(&attr
, uattr
);
3651 case BPF_MAP_LOOKUP_AND_DELETE_ELEM
:
3652 err
= map_lookup_and_delete_elem(&attr
);
3654 case BPF_MAP_LOOKUP_BATCH
:
3655 err
= bpf_map_do_batch(&attr
, uattr
, BPF_MAP_LOOKUP_BATCH
);
3657 case BPF_MAP_LOOKUP_AND_DELETE_BATCH
:
3658 err
= bpf_map_do_batch(&attr
, uattr
,
3659 BPF_MAP_LOOKUP_AND_DELETE_BATCH
);
3661 case BPF_MAP_UPDATE_BATCH
:
3662 err
= bpf_map_do_batch(&attr
, uattr
, BPF_MAP_UPDATE_BATCH
);
3664 case BPF_MAP_DELETE_BATCH
:
3665 err
= bpf_map_do_batch(&attr
, uattr
, BPF_MAP_DELETE_BATCH
);