1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 #include <linux/bpf.h>
13 #include <linux/bpf_trace.h>
14 #include <linux/syscalls.h>
15 #include <linux/slab.h>
16 #include <linux/sched/signal.h>
17 #include <linux/vmalloc.h>
18 #include <linux/mmzone.h>
19 #include <linux/anon_inodes.h>
20 #include <linux/file.h>
21 #include <linux/license.h>
22 #include <linux/filter.h>
23 #include <linux/version.h>
24 #include <linux/kernel.h>
25 #include <linux/idr.h>
26 #include <linux/cred.h>
27 #include <linux/timekeeping.h>
28 #include <linux/ctype.h>
30 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
31 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
32 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
33 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
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_HASH(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, _ops)
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 static int 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(VERIFY_READ
, 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
,
102 static struct bpf_map
*find_and_alloc_map(union bpf_attr
*attr
)
104 const struct bpf_map_ops
*ops
;
108 if (attr
->map_type
>= ARRAY_SIZE(bpf_map_types
))
109 return ERR_PTR(-EINVAL
);
110 ops
= bpf_map_types
[attr
->map_type
];
112 return ERR_PTR(-EINVAL
);
114 if (ops
->map_alloc_check
) {
115 err
= ops
->map_alloc_check(attr
);
119 if (attr
->map_ifindex
)
120 ops
= &bpf_map_offload_ops
;
121 map
= ops
->map_alloc(attr
);
125 map
->map_type
= attr
->map_type
;
129 void *bpf_map_area_alloc(size_t size
, int numa_node
)
131 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
132 * trigger under memory pressure as we really just want to
135 const gfp_t flags
= __GFP_NOWARN
| __GFP_NORETRY
| __GFP_ZERO
;
138 if (size
<= (PAGE_SIZE
<< PAGE_ALLOC_COSTLY_ORDER
)) {
139 area
= kmalloc_node(size
, GFP_USER
| flags
, numa_node
);
144 return __vmalloc_node_flags_caller(size
, numa_node
, GFP_KERNEL
| flags
,
145 __builtin_return_address(0));
148 void bpf_map_area_free(void *area
)
153 void bpf_map_init_from_attr(struct bpf_map
*map
, union bpf_attr
*attr
)
155 map
->map_type
= attr
->map_type
;
156 map
->key_size
= attr
->key_size
;
157 map
->value_size
= attr
->value_size
;
158 map
->max_entries
= attr
->max_entries
;
159 map
->map_flags
= attr
->map_flags
;
160 map
->numa_node
= bpf_map_attr_numa_node(attr
);
163 int bpf_map_precharge_memlock(u32 pages
)
165 struct user_struct
*user
= get_current_user();
166 unsigned long memlock_limit
, cur
;
168 memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
169 cur
= atomic_long_read(&user
->locked_vm
);
171 if (cur
+ pages
> memlock_limit
)
176 static int bpf_map_charge_memlock(struct bpf_map
*map
)
178 struct user_struct
*user
= get_current_user();
179 unsigned long memlock_limit
;
181 memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
183 atomic_long_add(map
->pages
, &user
->locked_vm
);
185 if (atomic_long_read(&user
->locked_vm
) > memlock_limit
) {
186 atomic_long_sub(map
->pages
, &user
->locked_vm
);
194 static void bpf_map_uncharge_memlock(struct bpf_map
*map
)
196 struct user_struct
*user
= map
->user
;
198 atomic_long_sub(map
->pages
, &user
->locked_vm
);
202 static int bpf_map_alloc_id(struct bpf_map
*map
)
206 spin_lock_bh(&map_idr_lock
);
207 id
= idr_alloc_cyclic(&map_idr
, map
, 1, INT_MAX
, GFP_ATOMIC
);
210 spin_unlock_bh(&map_idr_lock
);
212 if (WARN_ON_ONCE(!id
))
215 return id
> 0 ? 0 : id
;
218 void bpf_map_free_id(struct bpf_map
*map
, bool do_idr_lock
)
222 /* Offloaded maps are removed from the IDR store when their device
223 * disappears - even if someone holds an fd to them they are unusable,
224 * the memory is gone, all ops will fail; they are simply waiting for
225 * refcnt to drop to be freed.
231 spin_lock_irqsave(&map_idr_lock
, flags
);
233 __acquire(&map_idr_lock
);
235 idr_remove(&map_idr
, map
->id
);
239 spin_unlock_irqrestore(&map_idr_lock
, flags
);
241 __release(&map_idr_lock
);
244 /* called from workqueue */
245 static void bpf_map_free_deferred(struct work_struct
*work
)
247 struct bpf_map
*map
= container_of(work
, struct bpf_map
, work
);
249 bpf_map_uncharge_memlock(map
);
250 security_bpf_map_free(map
);
251 /* implementation dependent freeing */
252 map
->ops
->map_free(map
);
255 static void bpf_map_put_uref(struct bpf_map
*map
)
257 if (atomic_dec_and_test(&map
->usercnt
)) {
258 if (map
->map_type
== BPF_MAP_TYPE_PROG_ARRAY
)
259 bpf_fd_array_map_clear(map
);
263 /* decrement map refcnt and schedule it for freeing via workqueue
264 * (unrelying map implementation ops->map_free() might sleep)
266 static void __bpf_map_put(struct bpf_map
*map
, bool do_idr_lock
)
268 if (atomic_dec_and_test(&map
->refcnt
)) {
269 /* bpf_map_free_id() must be called first */
270 bpf_map_free_id(map
, do_idr_lock
);
271 INIT_WORK(&map
->work
, bpf_map_free_deferred
);
272 schedule_work(&map
->work
);
276 void bpf_map_put(struct bpf_map
*map
)
278 __bpf_map_put(map
, true);
281 void bpf_map_put_with_uref(struct bpf_map
*map
)
283 bpf_map_put_uref(map
);
287 static int bpf_map_release(struct inode
*inode
, struct file
*filp
)
289 struct bpf_map
*map
= filp
->private_data
;
291 if (map
->ops
->map_release
)
292 map
->ops
->map_release(map
, filp
);
294 bpf_map_put_with_uref(map
);
298 #ifdef CONFIG_PROC_FS
299 static void bpf_map_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
301 const struct bpf_map
*map
= filp
->private_data
;
302 const struct bpf_array
*array
;
303 u32 owner_prog_type
= 0;
306 if (map
->map_type
== BPF_MAP_TYPE_PROG_ARRAY
) {
307 array
= container_of(map
, struct bpf_array
, map
);
308 owner_prog_type
= array
->owner_prog_type
;
309 owner_jited
= array
->owner_jited
;
324 map
->pages
* 1ULL << PAGE_SHIFT
);
326 if (owner_prog_type
) {
327 seq_printf(m
, "owner_prog_type:\t%u\n",
329 seq_printf(m
, "owner_jited:\t%u\n",
335 static ssize_t
bpf_dummy_read(struct file
*filp
, char __user
*buf
, size_t siz
,
338 /* We need this handler such that alloc_file() enables
339 * f_mode with FMODE_CAN_READ.
344 static ssize_t
bpf_dummy_write(struct file
*filp
, const char __user
*buf
,
345 size_t siz
, loff_t
*ppos
)
347 /* We need this handler such that alloc_file() enables
348 * f_mode with FMODE_CAN_WRITE.
353 const struct file_operations bpf_map_fops
= {
354 #ifdef CONFIG_PROC_FS
355 .show_fdinfo
= bpf_map_show_fdinfo
,
357 .release
= bpf_map_release
,
358 .read
= bpf_dummy_read
,
359 .write
= bpf_dummy_write
,
362 int bpf_map_new_fd(struct bpf_map
*map
, int flags
)
366 ret
= security_bpf_map(map
, OPEN_FMODE(flags
));
370 return anon_inode_getfd("bpf-map", &bpf_map_fops
, map
,
374 int bpf_get_file_flag(int flags
)
376 if ((flags
& BPF_F_RDONLY
) && (flags
& BPF_F_WRONLY
))
378 if (flags
& BPF_F_RDONLY
)
380 if (flags
& BPF_F_WRONLY
)
385 /* helper macro to check that unused fields 'union bpf_attr' are zero */
386 #define CHECK_ATTR(CMD) \
387 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
388 sizeof(attr->CMD##_LAST_FIELD), 0, \
390 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
391 sizeof(attr->CMD##_LAST_FIELD)) != NULL
393 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
394 * Return 0 on success and < 0 on error.
396 static int bpf_obj_name_cpy(char *dst
, const char *src
)
398 const char *end
= src
+ BPF_OBJ_NAME_LEN
;
400 memset(dst
, 0, BPF_OBJ_NAME_LEN
);
402 /* Copy all isalnum() and '_' char */
403 while (src
< end
&& *src
) {
404 if (!isalnum(*src
) && *src
!= '_')
409 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
416 #define BPF_MAP_CREATE_LAST_FIELD map_ifindex
417 /* called via syscall */
418 static int map_create(union bpf_attr
*attr
)
420 int numa_node
= bpf_map_attr_numa_node(attr
);
425 err
= CHECK_ATTR(BPF_MAP_CREATE
);
429 f_flags
= bpf_get_file_flag(attr
->map_flags
);
433 if (numa_node
!= NUMA_NO_NODE
&&
434 ((unsigned int)numa_node
>= nr_node_ids
||
435 !node_online(numa_node
)))
438 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
439 map
= find_and_alloc_map(attr
);
443 err
= bpf_obj_name_cpy(map
->name
, attr
->map_name
);
445 goto free_map_nouncharge
;
447 atomic_set(&map
->refcnt
, 1);
448 atomic_set(&map
->usercnt
, 1);
450 err
= security_bpf_map_alloc(map
);
452 goto free_map_nouncharge
;
454 err
= bpf_map_charge_memlock(map
);
458 err
= bpf_map_alloc_id(map
);
462 err
= bpf_map_new_fd(map
, f_flags
);
464 /* failed to allocate fd.
465 * bpf_map_put() is needed because the above
466 * bpf_map_alloc_id() has published the map
467 * to the userspace and the userspace may
468 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
474 trace_bpf_map_create(map
, err
);
478 bpf_map_uncharge_memlock(map
);
480 security_bpf_map_free(map
);
482 map
->ops
->map_free(map
);
486 /* if error is returned, fd is released.
487 * On success caller should complete fd access with matching fdput()
489 struct bpf_map
*__bpf_map_get(struct fd f
)
492 return ERR_PTR(-EBADF
);
493 if (f
.file
->f_op
!= &bpf_map_fops
) {
495 return ERR_PTR(-EINVAL
);
498 return f
.file
->private_data
;
501 /* prog's and map's refcnt limit */
502 #define BPF_MAX_REFCNT 32768
504 struct bpf_map
*bpf_map_inc(struct bpf_map
*map
, bool uref
)
506 if (atomic_inc_return(&map
->refcnt
) > BPF_MAX_REFCNT
) {
507 atomic_dec(&map
->refcnt
);
508 return ERR_PTR(-EBUSY
);
511 atomic_inc(&map
->usercnt
);
515 struct bpf_map
*bpf_map_get_with_uref(u32 ufd
)
517 struct fd f
= fdget(ufd
);
520 map
= __bpf_map_get(f
);
524 map
= bpf_map_inc(map
, true);
530 /* map_idr_lock should have been held */
531 static struct bpf_map
*bpf_map_inc_not_zero(struct bpf_map
*map
,
536 refold
= __atomic_add_unless(&map
->refcnt
, 1, 0);
538 if (refold
>= BPF_MAX_REFCNT
) {
539 __bpf_map_put(map
, false);
540 return ERR_PTR(-EBUSY
);
544 return ERR_PTR(-ENOENT
);
547 atomic_inc(&map
->usercnt
);
552 int __weak
bpf_stackmap_copy(struct bpf_map
*map
, void *key
, void *value
)
557 /* last field in 'union bpf_attr' used by this command */
558 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
560 static int map_lookup_elem(union bpf_attr
*attr
)
562 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
563 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
564 int ufd
= attr
->map_fd
;
566 void *key
, *value
, *ptr
;
571 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM
))
575 map
= __bpf_map_get(f
);
579 if (!(f
.file
->f_mode
& FMODE_CAN_READ
)) {
584 key
= memdup_user(ukey
, map
->key_size
);
590 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
591 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
592 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
)
593 value_size
= round_up(map
->value_size
, 8) * num_possible_cpus();
594 else if (IS_FD_MAP(map
))
595 value_size
= sizeof(u32
);
597 value_size
= map
->value_size
;
600 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
604 if (bpf_map_is_dev_bound(map
)) {
605 err
= bpf_map_offload_lookup_elem(map
, key
, value
);
606 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
607 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
608 err
= bpf_percpu_hash_copy(map
, key
, value
);
609 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
610 err
= bpf_percpu_array_copy(map
, key
, value
);
611 } else if (map
->map_type
== BPF_MAP_TYPE_STACK_TRACE
) {
612 err
= bpf_stackmap_copy(map
, key
, value
);
613 } else if (IS_FD_ARRAY(map
)) {
614 err
= bpf_fd_array_map_lookup_elem(map
, key
, value
);
615 } else if (IS_FD_HASH(map
)) {
616 err
= bpf_fd_htab_map_lookup_elem(map
, key
, value
);
619 ptr
= map
->ops
->map_lookup_elem(map
, key
);
621 memcpy(value
, ptr
, value_size
);
623 err
= ptr
? 0 : -ENOENT
;
630 if (copy_to_user(uvalue
, value
, value_size
) != 0)
633 trace_bpf_map_lookup_elem(map
, ufd
, key
, value
);
645 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
647 static int map_update_elem(union bpf_attr
*attr
)
649 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
650 void __user
*uvalue
= u64_to_user_ptr(attr
->value
);
651 int ufd
= attr
->map_fd
;
658 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM
))
662 map
= __bpf_map_get(f
);
666 if (!(f
.file
->f_mode
& FMODE_CAN_WRITE
)) {
671 key
= memdup_user(ukey
, map
->key_size
);
677 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
678 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
||
679 map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
)
680 value_size
= round_up(map
->value_size
, 8) * num_possible_cpus();
682 value_size
= map
->value_size
;
685 value
= kmalloc(value_size
, GFP_USER
| __GFP_NOWARN
);
690 if (copy_from_user(value
, uvalue
, value_size
) != 0)
693 /* Need to create a kthread, thus must support schedule */
694 if (bpf_map_is_dev_bound(map
)) {
695 err
= bpf_map_offload_update_elem(map
, key
, value
, attr
->flags
);
697 } else if (map
->map_type
== BPF_MAP_TYPE_CPUMAP
) {
698 err
= map
->ops
->map_update_elem(map
, key
, value
, attr
->flags
);
702 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
703 * inside bpf map update or delete otherwise deadlocks are possible
706 __this_cpu_inc(bpf_prog_active
);
707 if (map
->map_type
== BPF_MAP_TYPE_PERCPU_HASH
||
708 map
->map_type
== BPF_MAP_TYPE_LRU_PERCPU_HASH
) {
709 err
= bpf_percpu_hash_update(map
, key
, value
, attr
->flags
);
710 } else if (map
->map_type
== BPF_MAP_TYPE_PERCPU_ARRAY
) {
711 err
= bpf_percpu_array_update(map
, key
, value
, attr
->flags
);
712 } else if (IS_FD_ARRAY(map
)) {
714 err
= bpf_fd_array_map_update_elem(map
, f
.file
, key
, value
,
717 } else if (map
->map_type
== BPF_MAP_TYPE_HASH_OF_MAPS
) {
719 err
= bpf_fd_htab_map_update_elem(map
, f
.file
, key
, value
,
724 err
= map
->ops
->map_update_elem(map
, key
, value
, attr
->flags
);
727 __this_cpu_dec(bpf_prog_active
);
731 trace_bpf_map_update_elem(map
, ufd
, key
, value
);
741 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
743 static int map_delete_elem(union bpf_attr
*attr
)
745 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
746 int ufd
= attr
->map_fd
;
752 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM
))
756 map
= __bpf_map_get(f
);
760 if (!(f
.file
->f_mode
& FMODE_CAN_WRITE
)) {
765 key
= memdup_user(ukey
, map
->key_size
);
771 if (bpf_map_is_dev_bound(map
)) {
772 err
= bpf_map_offload_delete_elem(map
, key
);
777 __this_cpu_inc(bpf_prog_active
);
779 err
= map
->ops
->map_delete_elem(map
, key
);
781 __this_cpu_dec(bpf_prog_active
);
785 trace_bpf_map_delete_elem(map
, ufd
, key
);
792 /* last field in 'union bpf_attr' used by this command */
793 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
795 static int map_get_next_key(union bpf_attr
*attr
)
797 void __user
*ukey
= u64_to_user_ptr(attr
->key
);
798 void __user
*unext_key
= u64_to_user_ptr(attr
->next_key
);
799 int ufd
= attr
->map_fd
;
801 void *key
, *next_key
;
805 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY
))
809 map
= __bpf_map_get(f
);
813 if (!(f
.file
->f_mode
& FMODE_CAN_READ
)) {
819 key
= memdup_user(ukey
, map
->key_size
);
829 next_key
= kmalloc(map
->key_size
, GFP_USER
);
833 if (bpf_map_is_dev_bound(map
)) {
834 err
= bpf_map_offload_get_next_key(map
, key
, next_key
);
839 err
= map
->ops
->map_get_next_key(map
, key
, next_key
);
846 if (copy_to_user(unext_key
, next_key
, map
->key_size
) != 0)
849 trace_bpf_map_next_key(map
, ufd
, key
, next_key
);
861 static const struct bpf_prog_ops
* const bpf_prog_types
[] = {
862 #define BPF_PROG_TYPE(_id, _name) \
863 [_id] = & _name ## _prog_ops,
864 #define BPF_MAP_TYPE(_id, _ops)
865 #include <linux/bpf_types.h>
870 static int find_prog_type(enum bpf_prog_type type
, struct bpf_prog
*prog
)
872 if (type
>= ARRAY_SIZE(bpf_prog_types
) || !bpf_prog_types
[type
])
875 if (!bpf_prog_is_dev_bound(prog
->aux
))
876 prog
->aux
->ops
= bpf_prog_types
[type
];
878 prog
->aux
->ops
= &bpf_offload_prog_ops
;
883 /* drop refcnt on maps used by eBPF program and free auxilary data */
884 static void free_used_maps(struct bpf_prog_aux
*aux
)
888 for (i
= 0; i
< aux
->used_map_cnt
; i
++)
889 bpf_map_put(aux
->used_maps
[i
]);
891 kfree(aux
->used_maps
);
894 int __bpf_prog_charge(struct user_struct
*user
, u32 pages
)
896 unsigned long memlock_limit
= rlimit(RLIMIT_MEMLOCK
) >> PAGE_SHIFT
;
897 unsigned long user_bufs
;
900 user_bufs
= atomic_long_add_return(pages
, &user
->locked_vm
);
901 if (user_bufs
> memlock_limit
) {
902 atomic_long_sub(pages
, &user
->locked_vm
);
910 void __bpf_prog_uncharge(struct user_struct
*user
, u32 pages
)
913 atomic_long_sub(pages
, &user
->locked_vm
);
916 static int bpf_prog_charge_memlock(struct bpf_prog
*prog
)
918 struct user_struct
*user
= get_current_user();
921 ret
= __bpf_prog_charge(user
, prog
->pages
);
927 prog
->aux
->user
= user
;
931 static void bpf_prog_uncharge_memlock(struct bpf_prog
*prog
)
933 struct user_struct
*user
= prog
->aux
->user
;
935 __bpf_prog_uncharge(user
, prog
->pages
);
939 static int bpf_prog_alloc_id(struct bpf_prog
*prog
)
943 spin_lock_bh(&prog_idr_lock
);
944 id
= idr_alloc_cyclic(&prog_idr
, prog
, 1, INT_MAX
, GFP_ATOMIC
);
947 spin_unlock_bh(&prog_idr_lock
);
949 /* id is in [1, INT_MAX) */
950 if (WARN_ON_ONCE(!id
))
953 return id
> 0 ? 0 : id
;
956 void bpf_prog_free_id(struct bpf_prog
*prog
, bool do_idr_lock
)
958 /* cBPF to eBPF migrations are currently not in the idr store.
959 * Offloaded programs are removed from the store when their device
960 * disappears - even if someone grabs an fd to them they are unusable,
961 * simply waiting for refcnt to drop to be freed.
967 spin_lock_bh(&prog_idr_lock
);
969 __acquire(&prog_idr_lock
);
971 idr_remove(&prog_idr
, prog
->aux
->id
);
975 spin_unlock_bh(&prog_idr_lock
);
977 __release(&prog_idr_lock
);
980 static void __bpf_prog_put_rcu(struct rcu_head
*rcu
)
982 struct bpf_prog_aux
*aux
= container_of(rcu
, struct bpf_prog_aux
, rcu
);
985 bpf_prog_uncharge_memlock(aux
->prog
);
986 security_bpf_prog_free(aux
);
987 bpf_prog_free(aux
->prog
);
990 static void __bpf_prog_put(struct bpf_prog
*prog
, bool do_idr_lock
)
992 if (atomic_dec_and_test(&prog
->aux
->refcnt
)) {
995 trace_bpf_prog_put_rcu(prog
);
996 /* bpf_prog_free_id() must be called first */
997 bpf_prog_free_id(prog
, do_idr_lock
);
999 for (i
= 0; i
< prog
->aux
->func_cnt
; i
++)
1000 bpf_prog_kallsyms_del(prog
->aux
->func
[i
]);
1001 bpf_prog_kallsyms_del(prog
);
1003 call_rcu(&prog
->aux
->rcu
, __bpf_prog_put_rcu
);
1007 void bpf_prog_put(struct bpf_prog
*prog
)
1009 __bpf_prog_put(prog
, true);
1011 EXPORT_SYMBOL_GPL(bpf_prog_put
);
1013 static int bpf_prog_release(struct inode
*inode
, struct file
*filp
)
1015 struct bpf_prog
*prog
= filp
->private_data
;
1021 #ifdef CONFIG_PROC_FS
1022 static void bpf_prog_show_fdinfo(struct seq_file
*m
, struct file
*filp
)
1024 const struct bpf_prog
*prog
= filp
->private_data
;
1025 char prog_tag
[sizeof(prog
->tag
) * 2 + 1] = { };
1027 bin2hex(prog_tag
, prog
->tag
, sizeof(prog
->tag
));
1036 prog
->pages
* 1ULL << PAGE_SHIFT
);
1040 const struct file_operations bpf_prog_fops
= {
1041 #ifdef CONFIG_PROC_FS
1042 .show_fdinfo
= bpf_prog_show_fdinfo
,
1044 .release
= bpf_prog_release
,
1045 .read
= bpf_dummy_read
,
1046 .write
= bpf_dummy_write
,
1049 int bpf_prog_new_fd(struct bpf_prog
*prog
)
1053 ret
= security_bpf_prog(prog
);
1057 return anon_inode_getfd("bpf-prog", &bpf_prog_fops
, prog
,
1058 O_RDWR
| O_CLOEXEC
);
1061 static struct bpf_prog
*____bpf_prog_get(struct fd f
)
1064 return ERR_PTR(-EBADF
);
1065 if (f
.file
->f_op
!= &bpf_prog_fops
) {
1067 return ERR_PTR(-EINVAL
);
1070 return f
.file
->private_data
;
1073 struct bpf_prog
*bpf_prog_add(struct bpf_prog
*prog
, int i
)
1075 if (atomic_add_return(i
, &prog
->aux
->refcnt
) > BPF_MAX_REFCNT
) {
1076 atomic_sub(i
, &prog
->aux
->refcnt
);
1077 return ERR_PTR(-EBUSY
);
1081 EXPORT_SYMBOL_GPL(bpf_prog_add
);
1083 void bpf_prog_sub(struct bpf_prog
*prog
, int i
)
1085 /* Only to be used for undoing previous bpf_prog_add() in some
1086 * error path. We still know that another entity in our call
1087 * path holds a reference to the program, thus atomic_sub() can
1088 * be safely used in such cases!
1090 WARN_ON(atomic_sub_return(i
, &prog
->aux
->refcnt
) == 0);
1092 EXPORT_SYMBOL_GPL(bpf_prog_sub
);
1094 struct bpf_prog
*bpf_prog_inc(struct bpf_prog
*prog
)
1096 return bpf_prog_add(prog
, 1);
1098 EXPORT_SYMBOL_GPL(bpf_prog_inc
);
1100 /* prog_idr_lock should have been held */
1101 struct bpf_prog
*bpf_prog_inc_not_zero(struct bpf_prog
*prog
)
1105 refold
= __atomic_add_unless(&prog
->aux
->refcnt
, 1, 0);
1107 if (refold
>= BPF_MAX_REFCNT
) {
1108 __bpf_prog_put(prog
, false);
1109 return ERR_PTR(-EBUSY
);
1113 return ERR_PTR(-ENOENT
);
1117 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero
);
1119 bool bpf_prog_get_ok(struct bpf_prog
*prog
,
1120 enum bpf_prog_type
*attach_type
, bool attach_drv
)
1122 /* not an attachment, just a refcount inc, always allow */
1126 if (prog
->type
!= *attach_type
)
1128 if (bpf_prog_is_dev_bound(prog
->aux
) && !attach_drv
)
1134 static struct bpf_prog
*__bpf_prog_get(u32 ufd
, enum bpf_prog_type
*attach_type
,
1137 struct fd f
= fdget(ufd
);
1138 struct bpf_prog
*prog
;
1140 prog
= ____bpf_prog_get(f
);
1143 if (!bpf_prog_get_ok(prog
, attach_type
, attach_drv
)) {
1144 prog
= ERR_PTR(-EINVAL
);
1148 prog
= bpf_prog_inc(prog
);
1154 struct bpf_prog
*bpf_prog_get(u32 ufd
)
1156 return __bpf_prog_get(ufd
, NULL
, false);
1159 struct bpf_prog
*bpf_prog_get_type_dev(u32 ufd
, enum bpf_prog_type type
,
1162 struct bpf_prog
*prog
= __bpf_prog_get(ufd
, &type
, attach_drv
);
1165 trace_bpf_prog_get_type(prog
);
1168 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev
);
1170 /* last field in 'union bpf_attr' used by this command */
1171 #define BPF_PROG_LOAD_LAST_FIELD prog_ifindex
1173 static int bpf_prog_load(union bpf_attr
*attr
)
1175 enum bpf_prog_type type
= attr
->prog_type
;
1176 struct bpf_prog
*prog
;
1181 if (CHECK_ATTR(BPF_PROG_LOAD
))
1184 if (attr
->prog_flags
& ~BPF_F_STRICT_ALIGNMENT
)
1187 /* copy eBPF program license from user space */
1188 if (strncpy_from_user(license
, u64_to_user_ptr(attr
->license
),
1189 sizeof(license
) - 1) < 0)
1191 license
[sizeof(license
) - 1] = 0;
1193 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1194 is_gpl
= license_is_gpl_compatible(license
);
1196 if (attr
->insn_cnt
== 0 || attr
->insn_cnt
> BPF_MAXINSNS
)
1199 if (type
== BPF_PROG_TYPE_KPROBE
&&
1200 attr
->kern_version
!= LINUX_VERSION_CODE
)
1203 if (type
!= BPF_PROG_TYPE_SOCKET_FILTER
&&
1204 type
!= BPF_PROG_TYPE_CGROUP_SKB
&&
1205 !capable(CAP_SYS_ADMIN
))
1208 /* plain bpf_prog allocation */
1209 prog
= bpf_prog_alloc(bpf_prog_size(attr
->insn_cnt
), GFP_USER
);
1213 prog
->aux
->offload_requested
= !!attr
->prog_ifindex
;
1215 err
= security_bpf_prog_alloc(prog
->aux
);
1217 goto free_prog_nouncharge
;
1219 err
= bpf_prog_charge_memlock(prog
);
1223 prog
->len
= attr
->insn_cnt
;
1226 if (copy_from_user(prog
->insns
, u64_to_user_ptr(attr
->insns
),
1227 bpf_prog_insn_size(prog
)) != 0)
1230 prog
->orig_prog
= NULL
;
1233 atomic_set(&prog
->aux
->refcnt
, 1);
1234 prog
->gpl_compatible
= is_gpl
? 1 : 0;
1236 if (bpf_prog_is_dev_bound(prog
->aux
)) {
1237 err
= bpf_prog_offload_init(prog
, attr
);
1242 /* find program type: socket_filter vs tracing_filter */
1243 err
= find_prog_type(type
, prog
);
1247 prog
->aux
->load_time
= ktime_get_boot_ns();
1248 err
= bpf_obj_name_cpy(prog
->aux
->name
, attr
->prog_name
);
1252 /* run eBPF verifier */
1253 err
= bpf_check(&prog
, attr
);
1255 goto free_used_maps
;
1257 /* eBPF program is ready to be JITed */
1258 if (!prog
->bpf_func
)
1259 prog
= bpf_prog_select_runtime(prog
, &err
);
1261 goto free_used_maps
;
1263 err
= bpf_prog_alloc_id(prog
);
1265 goto free_used_maps
;
1267 err
= bpf_prog_new_fd(prog
);
1269 /* failed to allocate fd.
1270 * bpf_prog_put() is needed because the above
1271 * bpf_prog_alloc_id() has published the prog
1272 * to the userspace and the userspace may
1273 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1279 bpf_prog_kallsyms_add(prog
);
1280 trace_bpf_prog_load(prog
, err
);
1284 free_used_maps(prog
->aux
);
1286 bpf_prog_uncharge_memlock(prog
);
1288 security_bpf_prog_free(prog
->aux
);
1289 free_prog_nouncharge
:
1290 bpf_prog_free(prog
);
1294 #define BPF_OBJ_LAST_FIELD file_flags
1296 static int bpf_obj_pin(const union bpf_attr
*attr
)
1298 if (CHECK_ATTR(BPF_OBJ
) || attr
->file_flags
!= 0)
1301 return bpf_obj_pin_user(attr
->bpf_fd
, u64_to_user_ptr(attr
->pathname
));
1304 static int bpf_obj_get(const union bpf_attr
*attr
)
1306 if (CHECK_ATTR(BPF_OBJ
) || attr
->bpf_fd
!= 0 ||
1307 attr
->file_flags
& ~BPF_OBJ_FLAG_MASK
)
1310 return bpf_obj_get_user(u64_to_user_ptr(attr
->pathname
),
1314 #ifdef CONFIG_CGROUP_BPF
1316 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1318 static int sockmap_get_from_fd(const union bpf_attr
*attr
, bool attach
)
1320 struct bpf_prog
*prog
= NULL
;
1321 int ufd
= attr
->target_fd
;
1322 struct bpf_map
*map
;
1327 map
= __bpf_map_get(f
);
1329 return PTR_ERR(map
);
1332 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
,
1333 BPF_PROG_TYPE_SK_SKB
);
1336 return PTR_ERR(prog
);
1340 err
= sock_map_prog(map
, prog
, attr
->attach_type
);
1352 #define BPF_F_ATTACH_MASK \
1353 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1355 static int bpf_prog_attach(const union bpf_attr
*attr
)
1357 enum bpf_prog_type ptype
;
1358 struct bpf_prog
*prog
;
1359 struct cgroup
*cgrp
;
1362 if (!capable(CAP_NET_ADMIN
))
1365 if (CHECK_ATTR(BPF_PROG_ATTACH
))
1368 if (attr
->attach_flags
& ~BPF_F_ATTACH_MASK
)
1371 switch (attr
->attach_type
) {
1372 case BPF_CGROUP_INET_INGRESS
:
1373 case BPF_CGROUP_INET_EGRESS
:
1374 ptype
= BPF_PROG_TYPE_CGROUP_SKB
;
1376 case BPF_CGROUP_INET_SOCK_CREATE
:
1377 ptype
= BPF_PROG_TYPE_CGROUP_SOCK
;
1379 case BPF_CGROUP_SOCK_OPS
:
1380 ptype
= BPF_PROG_TYPE_SOCK_OPS
;
1382 case BPF_CGROUP_DEVICE
:
1383 ptype
= BPF_PROG_TYPE_CGROUP_DEVICE
;
1385 case BPF_SK_SKB_STREAM_PARSER
:
1386 case BPF_SK_SKB_STREAM_VERDICT
:
1387 return sockmap_get_from_fd(attr
, true);
1392 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
, ptype
);
1394 return PTR_ERR(prog
);
1396 cgrp
= cgroup_get_from_fd(attr
->target_fd
);
1399 return PTR_ERR(cgrp
);
1402 ret
= cgroup_bpf_attach(cgrp
, prog
, attr
->attach_type
,
1403 attr
->attach_flags
);
1411 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1413 static int bpf_prog_detach(const union bpf_attr
*attr
)
1415 enum bpf_prog_type ptype
;
1416 struct bpf_prog
*prog
;
1417 struct cgroup
*cgrp
;
1420 if (!capable(CAP_NET_ADMIN
))
1423 if (CHECK_ATTR(BPF_PROG_DETACH
))
1426 switch (attr
->attach_type
) {
1427 case BPF_CGROUP_INET_INGRESS
:
1428 case BPF_CGROUP_INET_EGRESS
:
1429 ptype
= BPF_PROG_TYPE_CGROUP_SKB
;
1431 case BPF_CGROUP_INET_SOCK_CREATE
:
1432 ptype
= BPF_PROG_TYPE_CGROUP_SOCK
;
1434 case BPF_CGROUP_SOCK_OPS
:
1435 ptype
= BPF_PROG_TYPE_SOCK_OPS
;
1437 case BPF_CGROUP_DEVICE
:
1438 ptype
= BPF_PROG_TYPE_CGROUP_DEVICE
;
1440 case BPF_SK_SKB_STREAM_PARSER
:
1441 case BPF_SK_SKB_STREAM_VERDICT
:
1442 return sockmap_get_from_fd(attr
, false);
1447 cgrp
= cgroup_get_from_fd(attr
->target_fd
);
1449 return PTR_ERR(cgrp
);
1451 prog
= bpf_prog_get_type(attr
->attach_bpf_fd
, ptype
);
1455 ret
= cgroup_bpf_detach(cgrp
, prog
, attr
->attach_type
, 0);
1462 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1464 static int bpf_prog_query(const union bpf_attr
*attr
,
1465 union bpf_attr __user
*uattr
)
1467 struct cgroup
*cgrp
;
1470 if (!capable(CAP_NET_ADMIN
))
1472 if (CHECK_ATTR(BPF_PROG_QUERY
))
1474 if (attr
->query
.query_flags
& ~BPF_F_QUERY_EFFECTIVE
)
1477 switch (attr
->query
.attach_type
) {
1478 case BPF_CGROUP_INET_INGRESS
:
1479 case BPF_CGROUP_INET_EGRESS
:
1480 case BPF_CGROUP_INET_SOCK_CREATE
:
1481 case BPF_CGROUP_SOCK_OPS
:
1482 case BPF_CGROUP_DEVICE
:
1487 cgrp
= cgroup_get_from_fd(attr
->query
.target_fd
);
1489 return PTR_ERR(cgrp
);
1490 ret
= cgroup_bpf_query(cgrp
, attr
, uattr
);
1494 #endif /* CONFIG_CGROUP_BPF */
1496 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1498 static int bpf_prog_test_run(const union bpf_attr
*attr
,
1499 union bpf_attr __user
*uattr
)
1501 struct bpf_prog
*prog
;
1502 int ret
= -ENOTSUPP
;
1504 if (!capable(CAP_SYS_ADMIN
))
1506 if (CHECK_ATTR(BPF_PROG_TEST_RUN
))
1509 prog
= bpf_prog_get(attr
->test
.prog_fd
);
1511 return PTR_ERR(prog
);
1513 if (prog
->aux
->ops
->test_run
)
1514 ret
= prog
->aux
->ops
->test_run(prog
, attr
, uattr
);
1520 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1522 static int bpf_obj_get_next_id(const union bpf_attr
*attr
,
1523 union bpf_attr __user
*uattr
,
1527 u32 next_id
= attr
->start_id
;
1530 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID
) || next_id
>= INT_MAX
)
1533 if (!capable(CAP_SYS_ADMIN
))
1538 if (!idr_get_next(idr
, &next_id
))
1540 spin_unlock_bh(lock
);
1543 err
= put_user(next_id
, &uattr
->next_id
);
1548 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1550 static int bpf_prog_get_fd_by_id(const union bpf_attr
*attr
)
1552 struct bpf_prog
*prog
;
1553 u32 id
= attr
->prog_id
;
1556 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID
))
1559 if (!capable(CAP_SYS_ADMIN
))
1562 spin_lock_bh(&prog_idr_lock
);
1563 prog
= idr_find(&prog_idr
, id
);
1565 prog
= bpf_prog_inc_not_zero(prog
);
1567 prog
= ERR_PTR(-ENOENT
);
1568 spin_unlock_bh(&prog_idr_lock
);
1571 return PTR_ERR(prog
);
1573 fd
= bpf_prog_new_fd(prog
);
1580 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
1582 static int bpf_map_get_fd_by_id(const union bpf_attr
*attr
)
1584 struct bpf_map
*map
;
1585 u32 id
= attr
->map_id
;
1589 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID
) ||
1590 attr
->open_flags
& ~BPF_OBJ_FLAG_MASK
)
1593 if (!capable(CAP_SYS_ADMIN
))
1596 f_flags
= bpf_get_file_flag(attr
->open_flags
);
1600 spin_lock_bh(&map_idr_lock
);
1601 map
= idr_find(&map_idr
, id
);
1603 map
= bpf_map_inc_not_zero(map
, true);
1605 map
= ERR_PTR(-ENOENT
);
1606 spin_unlock_bh(&map_idr_lock
);
1609 return PTR_ERR(map
);
1611 fd
= bpf_map_new_fd(map
, f_flags
);
1618 static const struct bpf_map
*bpf_map_from_imm(const struct bpf_prog
*prog
,
1623 for (i
= 0; i
< prog
->aux
->used_map_cnt
; i
++)
1624 if (prog
->aux
->used_maps
[i
] == (void *)addr
)
1625 return prog
->aux
->used_maps
[i
];
1629 static struct bpf_insn
*bpf_insn_prepare_dump(const struct bpf_prog
*prog
)
1631 const struct bpf_map
*map
;
1632 struct bpf_insn
*insns
;
1636 insns
= kmemdup(prog
->insnsi
, bpf_prog_insn_size(prog
),
1641 for (i
= 0; i
< prog
->len
; i
++) {
1642 if (insns
[i
].code
== (BPF_JMP
| BPF_TAIL_CALL
)) {
1643 insns
[i
].code
= BPF_JMP
| BPF_CALL
;
1644 insns
[i
].imm
= BPF_FUNC_tail_call
;
1647 if (insns
[i
].code
== (BPF_JMP
| BPF_CALL
) ||
1648 insns
[i
].code
== (BPF_JMP
| BPF_CALL_ARGS
)) {
1649 if (insns
[i
].code
== (BPF_JMP
| BPF_CALL_ARGS
))
1650 insns
[i
].code
= BPF_JMP
| BPF_CALL
;
1651 if (!bpf_dump_raw_ok())
1656 if (insns
[i
].code
!= (BPF_LD
| BPF_IMM
| BPF_DW
))
1659 imm
= ((u64
)insns
[i
+ 1].imm
<< 32) | (u32
)insns
[i
].imm
;
1660 map
= bpf_map_from_imm(prog
, imm
);
1662 insns
[i
].src_reg
= BPF_PSEUDO_MAP_FD
;
1663 insns
[i
].imm
= map
->id
;
1664 insns
[i
+ 1].imm
= 0;
1668 if (!bpf_dump_raw_ok() &&
1669 imm
== (unsigned long)prog
->aux
) {
1671 insns
[i
+ 1].imm
= 0;
1679 static int bpf_prog_get_info_by_fd(struct bpf_prog
*prog
,
1680 const union bpf_attr
*attr
,
1681 union bpf_attr __user
*uattr
)
1683 struct bpf_prog_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
1684 struct bpf_prog_info info
= {};
1685 u32 info_len
= attr
->info
.info_len
;
1686 char __user
*uinsns
;
1690 err
= check_uarg_tail_zero(uinfo
, sizeof(info
), info_len
);
1693 info_len
= min_t(u32
, sizeof(info
), info_len
);
1695 if (copy_from_user(&info
, uinfo
, info_len
))
1698 info
.type
= prog
->type
;
1699 info
.id
= prog
->aux
->id
;
1700 info
.load_time
= prog
->aux
->load_time
;
1701 info
.created_by_uid
= from_kuid_munged(current_user_ns(),
1702 prog
->aux
->user
->uid
);
1704 memcpy(info
.tag
, prog
->tag
, sizeof(prog
->tag
));
1705 memcpy(info
.name
, prog
->aux
->name
, sizeof(prog
->aux
->name
));
1707 ulen
= info
.nr_map_ids
;
1708 info
.nr_map_ids
= prog
->aux
->used_map_cnt
;
1709 ulen
= min_t(u32
, info
.nr_map_ids
, ulen
);
1711 u32 __user
*user_map_ids
= u64_to_user_ptr(info
.map_ids
);
1714 for (i
= 0; i
< ulen
; i
++)
1715 if (put_user(prog
->aux
->used_maps
[i
]->id
,
1720 if (!capable(CAP_SYS_ADMIN
)) {
1721 info
.jited_prog_len
= 0;
1722 info
.xlated_prog_len
= 0;
1726 ulen
= info
.xlated_prog_len
;
1727 info
.xlated_prog_len
= bpf_prog_insn_size(prog
);
1728 if (info
.xlated_prog_len
&& ulen
) {
1729 struct bpf_insn
*insns_sanitized
;
1732 if (prog
->blinded
&& !bpf_dump_raw_ok()) {
1733 info
.xlated_prog_insns
= 0;
1736 insns_sanitized
= bpf_insn_prepare_dump(prog
);
1737 if (!insns_sanitized
)
1739 uinsns
= u64_to_user_ptr(info
.xlated_prog_insns
);
1740 ulen
= min_t(u32
, info
.xlated_prog_len
, ulen
);
1741 fault
= copy_to_user(uinsns
, insns_sanitized
, ulen
);
1742 kfree(insns_sanitized
);
1747 if (bpf_prog_is_dev_bound(prog
->aux
)) {
1748 err
= bpf_prog_offload_info_fill(&info
, prog
);
1754 /* NOTE: the following code is supposed to be skipped for offload.
1755 * bpf_prog_offload_info_fill() is the place to fill similar fields
1758 ulen
= info
.jited_prog_len
;
1759 info
.jited_prog_len
= prog
->jited_len
;
1760 if (info
.jited_prog_len
&& ulen
) {
1761 if (bpf_dump_raw_ok()) {
1762 uinsns
= u64_to_user_ptr(info
.jited_prog_insns
);
1763 ulen
= min_t(u32
, info
.jited_prog_len
, ulen
);
1764 if (copy_to_user(uinsns
, prog
->bpf_func
, ulen
))
1767 info
.jited_prog_insns
= 0;
1772 if (copy_to_user(uinfo
, &info
, info_len
) ||
1773 put_user(info_len
, &uattr
->info
.info_len
))
1779 static int bpf_map_get_info_by_fd(struct bpf_map
*map
,
1780 const union bpf_attr
*attr
,
1781 union bpf_attr __user
*uattr
)
1783 struct bpf_map_info __user
*uinfo
= u64_to_user_ptr(attr
->info
.info
);
1784 struct bpf_map_info info
= {};
1785 u32 info_len
= attr
->info
.info_len
;
1788 err
= check_uarg_tail_zero(uinfo
, sizeof(info
), info_len
);
1791 info_len
= min_t(u32
, sizeof(info
), info_len
);
1793 info
.type
= map
->map_type
;
1795 info
.key_size
= map
->key_size
;
1796 info
.value_size
= map
->value_size
;
1797 info
.max_entries
= map
->max_entries
;
1798 info
.map_flags
= map
->map_flags
;
1799 memcpy(info
.name
, map
->name
, sizeof(map
->name
));
1801 if (bpf_map_is_dev_bound(map
)) {
1802 err
= bpf_map_offload_info_fill(&info
, map
);
1807 if (copy_to_user(uinfo
, &info
, info_len
) ||
1808 put_user(info_len
, &uattr
->info
.info_len
))
1814 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1816 static int bpf_obj_get_info_by_fd(const union bpf_attr
*attr
,
1817 union bpf_attr __user
*uattr
)
1819 int ufd
= attr
->info
.bpf_fd
;
1823 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD
))
1830 if (f
.file
->f_op
== &bpf_prog_fops
)
1831 err
= bpf_prog_get_info_by_fd(f
.file
->private_data
, attr
,
1833 else if (f
.file
->f_op
== &bpf_map_fops
)
1834 err
= bpf_map_get_info_by_fd(f
.file
->private_data
, attr
,
1843 SYSCALL_DEFINE3(bpf
, int, cmd
, union bpf_attr __user
*, uattr
, unsigned int, size
)
1845 union bpf_attr attr
= {};
1848 if (!capable(CAP_SYS_ADMIN
) && sysctl_unprivileged_bpf_disabled
)
1851 err
= check_uarg_tail_zero(uattr
, sizeof(attr
), size
);
1854 size
= min_t(u32
, size
, sizeof(attr
));
1856 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1857 if (copy_from_user(&attr
, uattr
, size
) != 0)
1860 err
= security_bpf(cmd
, &attr
, size
);
1865 case BPF_MAP_CREATE
:
1866 err
= map_create(&attr
);
1868 case BPF_MAP_LOOKUP_ELEM
:
1869 err
= map_lookup_elem(&attr
);
1871 case BPF_MAP_UPDATE_ELEM
:
1872 err
= map_update_elem(&attr
);
1874 case BPF_MAP_DELETE_ELEM
:
1875 err
= map_delete_elem(&attr
);
1877 case BPF_MAP_GET_NEXT_KEY
:
1878 err
= map_get_next_key(&attr
);
1881 err
= bpf_prog_load(&attr
);
1884 err
= bpf_obj_pin(&attr
);
1887 err
= bpf_obj_get(&attr
);
1889 #ifdef CONFIG_CGROUP_BPF
1890 case BPF_PROG_ATTACH
:
1891 err
= bpf_prog_attach(&attr
);
1893 case BPF_PROG_DETACH
:
1894 err
= bpf_prog_detach(&attr
);
1896 case BPF_PROG_QUERY
:
1897 err
= bpf_prog_query(&attr
, uattr
);
1900 case BPF_PROG_TEST_RUN
:
1901 err
= bpf_prog_test_run(&attr
, uattr
);
1903 case BPF_PROG_GET_NEXT_ID
:
1904 err
= bpf_obj_get_next_id(&attr
, uattr
,
1905 &prog_idr
, &prog_idr_lock
);
1907 case BPF_MAP_GET_NEXT_ID
:
1908 err
= bpf_obj_get_next_id(&attr
, uattr
,
1909 &map_idr
, &map_idr_lock
);
1911 case BPF_PROG_GET_FD_BY_ID
:
1912 err
= bpf_prog_get_fd_by_id(&attr
);
1914 case BPF_MAP_GET_FD_BY_ID
:
1915 err
= bpf_map_get_fd_by_id(&attr
);
1917 case BPF_OBJ_GET_INFO_BY_FD
:
1918 err
= bpf_obj_get_info_by_fd(&attr
, uattr
);