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/btf.h>
15 #include <linux/syscalls.h>
16 #include <linux/slab.h>
17 #include <linux/sched/signal.h>
18 #include <linux/vmalloc.h>
19 #include <linux/mmzone.h>
20 #include <linux/anon_inodes.h>
21 #include <linux/file.h>
22 #include <linux/license.h>
23 #include <linux/filter.h>
24 #include <linux/version.h>
25 #include <linux/kernel.h>
26 #include <linux/idr.h>
27 #include <linux/cred.h>
28 #include <linux/timekeeping.h>
29 #include <linux/ctype.h>
30 #include <linux/btf.h>
32 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
33 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
34 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
35 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
36 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
37 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
39 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
41 DEFINE_PER_CPU(int, bpf_prog_active);
42 static DEFINE_IDR(prog_idr);
43 static DEFINE_SPINLOCK(prog_idr_lock);
44 static DEFINE_IDR(map_idr);
45 static DEFINE_SPINLOCK(map_idr_lock);
47 int sysctl_unprivileged_bpf_disabled __read_mostly;
49 static const struct bpf_map_ops * const bpf_map_types[] = {
50 #define BPF_PROG_TYPE(_id, _ops)
51 #define BPF_MAP_TYPE(_id, _ops) \
53 #include <linux/bpf_types.h>
59 * If we're handed a bigger struct than we know of, ensure all the unknown bits
60 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
61 * we don't know about yet.
63 * There is a ToCToU between this function call and the following
64 * copy_from_user() call. However, this is not a concern since this function is
65 * meant to be a future-proofing of bits.
67 static int check_uarg_tail_zero(void __user *uaddr,
71 unsigned char __user *addr;
72 unsigned char __user *end;
76 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
79 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
82 if (actual_size <= expected_size)
85 addr = uaddr + expected_size;
86 end = uaddr + actual_size;
88 for (; addr < end; addr++) {
89 err = get_user(val, addr);
99 const struct bpf_map_ops bpf_map_offload_ops = {
100 .map_alloc = bpf_map_offload_map_alloc,
101 .map_free = bpf_map_offload_map_free,
104 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
106 const struct bpf_map_ops *ops;
110 if (attr->map_type >= ARRAY_SIZE(bpf_map_types))
111 return ERR_PTR(-EINVAL);
112 ops = bpf_map_types[attr->map_type];
114 return ERR_PTR(-EINVAL);
116 if (ops->map_alloc_check) {
117 err = ops->map_alloc_check(attr);
121 if (attr->map_ifindex)
122 ops = &bpf_map_offload_ops;
123 map = ops->map_alloc(attr);
127 map->map_type = attr->map_type;
131 void *bpf_map_area_alloc(size_t size, int numa_node)
133 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
134 * trigger under memory pressure as we really just want to
137 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
140 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
141 area = kmalloc_node(size, GFP_USER | flags, numa_node);
146 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
147 __builtin_return_address(0));
150 void bpf_map_area_free(void *area)
155 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
157 map->map_type = attr->map_type;
158 map->key_size = attr->key_size;
159 map->value_size = attr->value_size;
160 map->max_entries = attr->max_entries;
161 map->map_flags = attr->map_flags;
162 map->numa_node = bpf_map_attr_numa_node(attr);
165 int bpf_map_precharge_memlock(u32 pages)
167 struct user_struct *user = get_current_user();
168 unsigned long memlock_limit, cur;
170 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
171 cur = atomic_long_read(&user->locked_vm);
173 if (cur + pages > memlock_limit)
178 static int bpf_map_charge_memlock(struct bpf_map *map)
180 struct user_struct *user = get_current_user();
181 unsigned long memlock_limit;
183 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
185 atomic_long_add(map->pages, &user->locked_vm);
187 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
188 atomic_long_sub(map->pages, &user->locked_vm);
196 static void bpf_map_uncharge_memlock(struct bpf_map *map)
198 struct user_struct *user = map->user;
200 atomic_long_sub(map->pages, &user->locked_vm);
204 static int bpf_map_alloc_id(struct bpf_map *map)
208 idr_preload(GFP_KERNEL);
209 spin_lock_bh(&map_idr_lock);
210 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
213 spin_unlock_bh(&map_idr_lock);
216 if (WARN_ON_ONCE(!id))
219 return id > 0 ? 0 : id;
222 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
226 /* Offloaded maps are removed from the IDR store when their device
227 * disappears - even if someone holds an fd to them they are unusable,
228 * the memory is gone, all ops will fail; they are simply waiting for
229 * refcnt to drop to be freed.
235 spin_lock_irqsave(&map_idr_lock, flags);
237 __acquire(&map_idr_lock);
239 idr_remove(&map_idr, map->id);
243 spin_unlock_irqrestore(&map_idr_lock, flags);
245 __release(&map_idr_lock);
248 /* called from workqueue */
249 static void bpf_map_free_deferred(struct work_struct *work)
251 struct bpf_map *map = container_of(work, struct bpf_map, work);
253 bpf_map_uncharge_memlock(map);
254 security_bpf_map_free(map);
255 /* implementation dependent freeing */
256 map->ops->map_free(map);
259 static void bpf_map_put_uref(struct bpf_map *map)
261 if (atomic_dec_and_test(&map->usercnt)) {
262 if (map->ops->map_release_uref)
263 map->ops->map_release_uref(map);
267 /* decrement map refcnt and schedule it for freeing via workqueue
268 * (unrelying map implementation ops->map_free() might sleep)
270 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
272 if (atomic_dec_and_test(&map->refcnt)) {
273 /* bpf_map_free_id() must be called first */
274 bpf_map_free_id(map, do_idr_lock);
276 INIT_WORK(&map->work, bpf_map_free_deferred);
277 schedule_work(&map->work);
281 void bpf_map_put(struct bpf_map *map)
283 __bpf_map_put(map, true);
285 EXPORT_SYMBOL_GPL(bpf_map_put);
287 void bpf_map_put_with_uref(struct bpf_map *map)
289 bpf_map_put_uref(map);
293 static int bpf_map_release(struct inode *inode, struct file *filp)
295 struct bpf_map *map = filp->private_data;
297 if (map->ops->map_release)
298 map->ops->map_release(map, filp);
300 bpf_map_put_with_uref(map);
304 #ifdef CONFIG_PROC_FS
305 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
307 const struct bpf_map *map = filp->private_data;
308 const struct bpf_array *array;
309 u32 owner_prog_type = 0;
312 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
313 array = container_of(map, struct bpf_array, map);
314 owner_prog_type = array->owner_prog_type;
315 owner_jited = array->owner_jited;
330 map->pages * 1ULL << PAGE_SHIFT);
332 if (owner_prog_type) {
333 seq_printf(m, "owner_prog_type:\t%u\n",
335 seq_printf(m, "owner_jited:\t%u\n",
341 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
344 /* We need this handler such that alloc_file() enables
345 * f_mode with FMODE_CAN_READ.
350 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
351 size_t siz, loff_t *ppos)
353 /* We need this handler such that alloc_file() enables
354 * f_mode with FMODE_CAN_WRITE.
359 const struct file_operations bpf_map_fops = {
360 #ifdef CONFIG_PROC_FS
361 .show_fdinfo = bpf_map_show_fdinfo,
363 .release = bpf_map_release,
364 .read = bpf_dummy_read,
365 .write = bpf_dummy_write,
368 int bpf_map_new_fd(struct bpf_map *map, int flags)
372 ret = security_bpf_map(map, OPEN_FMODE(flags));
376 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
380 int bpf_get_file_flag(int flags)
382 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
384 if (flags & BPF_F_RDONLY)
386 if (flags & BPF_F_WRONLY)
391 /* helper macro to check that unused fields 'union bpf_attr' are zero */
392 #define CHECK_ATTR(CMD) \
393 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
394 sizeof(attr->CMD##_LAST_FIELD), 0, \
396 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
397 sizeof(attr->CMD##_LAST_FIELD)) != NULL
399 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
400 * Return 0 on success and < 0 on error.
402 static int bpf_obj_name_cpy(char *dst, const char *src)
404 const char *end = src + BPF_OBJ_NAME_LEN;
406 memset(dst, 0, BPF_OBJ_NAME_LEN);
408 /* Copy all isalnum() and '_' char */
409 while (src < end && *src) {
410 if (!isalnum(*src) && *src != '_')
415 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
422 #define BPF_MAP_CREATE_LAST_FIELD btf_value_id
423 /* called via syscall */
424 static int map_create(union bpf_attr *attr)
426 int numa_node = bpf_map_attr_numa_node(attr);
431 err = CHECK_ATTR(BPF_MAP_CREATE);
435 f_flags = bpf_get_file_flag(attr->map_flags);
439 if (numa_node != NUMA_NO_NODE &&
440 ((unsigned int)numa_node >= nr_node_ids ||
441 !node_online(numa_node)))
444 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
445 map = find_and_alloc_map(attr);
449 err = bpf_obj_name_cpy(map->name, attr->map_name);
451 goto free_map_nouncharge;
453 atomic_set(&map->refcnt, 1);
454 atomic_set(&map->usercnt, 1);
456 if (bpf_map_support_seq_show(map) &&
457 (attr->btf_key_id || attr->btf_value_id)) {
460 if (!attr->btf_key_id || !attr->btf_value_id) {
462 goto free_map_nouncharge;
465 btf = btf_get_by_fd(attr->btf_fd);
468 goto free_map_nouncharge;
471 err = map->ops->map_check_btf(map, btf, attr->btf_key_id,
475 goto free_map_nouncharge;
479 map->btf_key_id = attr->btf_key_id;
480 map->btf_value_id = attr->btf_value_id;
483 err = security_bpf_map_alloc(map);
485 goto free_map_nouncharge;
487 err = bpf_map_charge_memlock(map);
491 err = bpf_map_alloc_id(map);
495 err = bpf_map_new_fd(map, f_flags);
497 /* failed to allocate fd.
498 * bpf_map_put() is needed because the above
499 * bpf_map_alloc_id() has published the map
500 * to the userspace and the userspace may
501 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
510 bpf_map_uncharge_memlock(map);
512 security_bpf_map_free(map);
515 map->ops->map_free(map);
519 /* if error is returned, fd is released.
520 * On success caller should complete fd access with matching fdput()
522 struct bpf_map *__bpf_map_get(struct fd f)
525 return ERR_PTR(-EBADF);
526 if (f.file->f_op != &bpf_map_fops) {
528 return ERR_PTR(-EINVAL);
531 return f.file->private_data;
534 /* prog's and map's refcnt limit */
535 #define BPF_MAX_REFCNT 32768
537 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
539 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
540 atomic_dec(&map->refcnt);
541 return ERR_PTR(-EBUSY);
544 atomic_inc(&map->usercnt);
547 EXPORT_SYMBOL_GPL(bpf_map_inc);
549 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
551 struct fd f = fdget(ufd);
554 map = __bpf_map_get(f);
558 map = bpf_map_inc(map, true);
564 /* map_idr_lock should have been held */
565 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
570 refold = __atomic_add_unless(&map->refcnt, 1, 0);
572 if (refold >= BPF_MAX_REFCNT) {
573 __bpf_map_put(map, false);
574 return ERR_PTR(-EBUSY);
578 return ERR_PTR(-ENOENT);
581 atomic_inc(&map->usercnt);
586 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
591 /* last field in 'union bpf_attr' used by this command */
592 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
594 static int map_lookup_elem(union bpf_attr *attr)
596 void __user *ukey = u64_to_user_ptr(attr->key);
597 void __user *uvalue = u64_to_user_ptr(attr->value);
598 int ufd = attr->map_fd;
600 void *key, *value, *ptr;
605 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
609 map = __bpf_map_get(f);
613 if (!(f.file->f_mode & FMODE_CAN_READ)) {
618 key = memdup_user(ukey, map->key_size);
624 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
625 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
626 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
627 value_size = round_up(map->value_size, 8) * num_possible_cpus();
628 else if (IS_FD_MAP(map))
629 value_size = sizeof(u32);
631 value_size = map->value_size;
634 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
638 if (bpf_map_is_dev_bound(map)) {
639 err = bpf_map_offload_lookup_elem(map, key, value);
640 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
641 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
642 err = bpf_percpu_hash_copy(map, key, value);
643 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
644 err = bpf_percpu_array_copy(map, key, value);
645 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
646 err = bpf_stackmap_copy(map, key, value);
647 } else if (IS_FD_ARRAY(map)) {
648 err = bpf_fd_array_map_lookup_elem(map, key, value);
649 } else if (IS_FD_HASH(map)) {
650 err = bpf_fd_htab_map_lookup_elem(map, key, value);
653 ptr = map->ops->map_lookup_elem(map, key);
655 memcpy(value, ptr, value_size);
657 err = ptr ? 0 : -ENOENT;
664 if (copy_to_user(uvalue, value, value_size) != 0)
678 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
680 static int map_update_elem(union bpf_attr *attr)
682 void __user *ukey = u64_to_user_ptr(attr->key);
683 void __user *uvalue = u64_to_user_ptr(attr->value);
684 int ufd = attr->map_fd;
691 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
695 map = __bpf_map_get(f);
699 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
704 key = memdup_user(ukey, map->key_size);
710 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
711 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
712 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
713 value_size = round_up(map->value_size, 8) * num_possible_cpus();
715 value_size = map->value_size;
718 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
723 if (copy_from_user(value, uvalue, value_size) != 0)
726 /* Need to create a kthread, thus must support schedule */
727 if (bpf_map_is_dev_bound(map)) {
728 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
730 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
731 err = map->ops->map_update_elem(map, key, value, attr->flags);
735 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
736 * inside bpf map update or delete otherwise deadlocks are possible
739 __this_cpu_inc(bpf_prog_active);
740 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
741 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
742 err = bpf_percpu_hash_update(map, key, value, attr->flags);
743 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
744 err = bpf_percpu_array_update(map, key, value, attr->flags);
745 } else if (IS_FD_ARRAY(map)) {
747 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
750 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
752 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
757 err = map->ops->map_update_elem(map, key, value, attr->flags);
760 __this_cpu_dec(bpf_prog_active);
772 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
774 static int map_delete_elem(union bpf_attr *attr)
776 void __user *ukey = u64_to_user_ptr(attr->key);
777 int ufd = attr->map_fd;
783 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
787 map = __bpf_map_get(f);
791 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
796 key = memdup_user(ukey, map->key_size);
802 if (bpf_map_is_dev_bound(map)) {
803 err = bpf_map_offload_delete_elem(map, key);
808 __this_cpu_inc(bpf_prog_active);
810 err = map->ops->map_delete_elem(map, key);
812 __this_cpu_dec(bpf_prog_active);
821 /* last field in 'union bpf_attr' used by this command */
822 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
824 static int map_get_next_key(union bpf_attr *attr)
826 void __user *ukey = u64_to_user_ptr(attr->key);
827 void __user *unext_key = u64_to_user_ptr(attr->next_key);
828 int ufd = attr->map_fd;
830 void *key, *next_key;
834 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
838 map = __bpf_map_get(f);
842 if (!(f.file->f_mode & FMODE_CAN_READ)) {
848 key = memdup_user(ukey, map->key_size);
858 next_key = kmalloc(map->key_size, GFP_USER);
862 if (bpf_map_is_dev_bound(map)) {
863 err = bpf_map_offload_get_next_key(map, key, next_key);
868 err = map->ops->map_get_next_key(map, key, next_key);
875 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
889 static const struct bpf_prog_ops * const bpf_prog_types[] = {
890 #define BPF_PROG_TYPE(_id, _name) \
891 [_id] = & _name ## _prog_ops,
892 #define BPF_MAP_TYPE(_id, _ops)
893 #include <linux/bpf_types.h>
898 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
900 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
903 if (!bpf_prog_is_dev_bound(prog->aux))
904 prog->aux->ops = bpf_prog_types[type];
906 prog->aux->ops = &bpf_offload_prog_ops;
911 /* drop refcnt on maps used by eBPF program and free auxilary data */
912 static void free_used_maps(struct bpf_prog_aux *aux)
916 for (i = 0; i < aux->used_map_cnt; i++)
917 bpf_map_put(aux->used_maps[i]);
919 kfree(aux->used_maps);
922 int __bpf_prog_charge(struct user_struct *user, u32 pages)
924 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
925 unsigned long user_bufs;
928 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
929 if (user_bufs > memlock_limit) {
930 atomic_long_sub(pages, &user->locked_vm);
938 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
941 atomic_long_sub(pages, &user->locked_vm);
944 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
946 struct user_struct *user = get_current_user();
949 ret = __bpf_prog_charge(user, prog->pages);
955 prog->aux->user = user;
959 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
961 struct user_struct *user = prog->aux->user;
963 __bpf_prog_uncharge(user, prog->pages);
967 static int bpf_prog_alloc_id(struct bpf_prog *prog)
971 idr_preload(GFP_KERNEL);
972 spin_lock_bh(&prog_idr_lock);
973 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
976 spin_unlock_bh(&prog_idr_lock);
979 /* id is in [1, INT_MAX) */
980 if (WARN_ON_ONCE(!id))
983 return id > 0 ? 0 : id;
986 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
988 /* cBPF to eBPF migrations are currently not in the idr store.
989 * Offloaded programs are removed from the store when their device
990 * disappears - even if someone grabs an fd to them they are unusable,
991 * simply waiting for refcnt to drop to be freed.
997 spin_lock_bh(&prog_idr_lock);
999 __acquire(&prog_idr_lock);
1001 idr_remove(&prog_idr, prog->aux->id);
1005 spin_unlock_bh(&prog_idr_lock);
1007 __release(&prog_idr_lock);
1010 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1012 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1014 free_used_maps(aux);
1015 bpf_prog_uncharge_memlock(aux->prog);
1016 security_bpf_prog_free(aux);
1017 bpf_prog_free(aux->prog);
1020 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1022 if (atomic_dec_and_test(&prog->aux->refcnt)) {
1025 /* bpf_prog_free_id() must be called first */
1026 bpf_prog_free_id(prog, do_idr_lock);
1028 for (i = 0; i < prog->aux->func_cnt; i++)
1029 bpf_prog_kallsyms_del(prog->aux->func[i]);
1030 bpf_prog_kallsyms_del(prog);
1032 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1036 void bpf_prog_put(struct bpf_prog *prog)
1038 __bpf_prog_put(prog, true);
1040 EXPORT_SYMBOL_GPL(bpf_prog_put);
1042 static int bpf_prog_release(struct inode *inode, struct file *filp)
1044 struct bpf_prog *prog = filp->private_data;
1050 #ifdef CONFIG_PROC_FS
1051 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1053 const struct bpf_prog *prog = filp->private_data;
1054 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1056 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1065 prog->pages * 1ULL << PAGE_SHIFT);
1069 const struct file_operations bpf_prog_fops = {
1070 #ifdef CONFIG_PROC_FS
1071 .show_fdinfo = bpf_prog_show_fdinfo,
1073 .release = bpf_prog_release,
1074 .read = bpf_dummy_read,
1075 .write = bpf_dummy_write,
1078 int bpf_prog_new_fd(struct bpf_prog *prog)
1082 ret = security_bpf_prog(prog);
1086 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1087 O_RDWR | O_CLOEXEC);
1090 static struct bpf_prog *____bpf_prog_get(struct fd f)
1093 return ERR_PTR(-EBADF);
1094 if (f.file->f_op != &bpf_prog_fops) {
1096 return ERR_PTR(-EINVAL);
1099 return f.file->private_data;
1102 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
1104 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1105 atomic_sub(i, &prog->aux->refcnt);
1106 return ERR_PTR(-EBUSY);
1110 EXPORT_SYMBOL_GPL(bpf_prog_add);
1112 void bpf_prog_sub(struct bpf_prog *prog, int i)
1114 /* Only to be used for undoing previous bpf_prog_add() in some
1115 * error path. We still know that another entity in our call
1116 * path holds a reference to the program, thus atomic_sub() can
1117 * be safely used in such cases!
1119 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1121 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1123 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1125 return bpf_prog_add(prog, 1);
1127 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1129 /* prog_idr_lock should have been held */
1130 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1134 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1136 if (refold >= BPF_MAX_REFCNT) {
1137 __bpf_prog_put(prog, false);
1138 return ERR_PTR(-EBUSY);
1142 return ERR_PTR(-ENOENT);
1146 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1148 bool bpf_prog_get_ok(struct bpf_prog *prog,
1149 enum bpf_prog_type *attach_type, bool attach_drv)
1151 /* not an attachment, just a refcount inc, always allow */
1155 if (prog->type != *attach_type)
1157 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1163 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1166 struct fd f = fdget(ufd);
1167 struct bpf_prog *prog;
1169 prog = ____bpf_prog_get(f);
1172 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1173 prog = ERR_PTR(-EINVAL);
1177 prog = bpf_prog_inc(prog);
1183 struct bpf_prog *bpf_prog_get(u32 ufd)
1185 return __bpf_prog_get(ufd, NULL, false);
1188 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1191 return __bpf_prog_get(ufd, &type, attach_drv);
1193 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1195 /* Initially all BPF programs could be loaded w/o specifying
1196 * expected_attach_type. Later for some of them specifying expected_attach_type
1197 * at load time became required so that program could be validated properly.
1198 * Programs of types that are allowed to be loaded both w/ and w/o (for
1199 * backward compatibility) expected_attach_type, should have the default attach
1200 * type assigned to expected_attach_type for the latter case, so that it can be
1201 * validated later at attach time.
1203 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1204 * prog type requires it but has some attach types that have to be backward
1207 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1209 switch (attr->prog_type) {
1210 case BPF_PROG_TYPE_CGROUP_SOCK:
1211 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1212 * exist so checking for non-zero is the way to go here.
1214 if (!attr->expected_attach_type)
1215 attr->expected_attach_type =
1216 BPF_CGROUP_INET_SOCK_CREATE;
1222 bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1223 enum bpf_attach_type expected_attach_type)
1225 switch (prog_type) {
1226 case BPF_PROG_TYPE_CGROUP_SOCK:
1227 switch (expected_attach_type) {
1228 case BPF_CGROUP_INET_SOCK_CREATE:
1229 case BPF_CGROUP_INET4_POST_BIND:
1230 case BPF_CGROUP_INET6_POST_BIND:
1235 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1236 switch (expected_attach_type) {
1237 case BPF_CGROUP_INET4_BIND:
1238 case BPF_CGROUP_INET6_BIND:
1239 case BPF_CGROUP_INET4_CONNECT:
1240 case BPF_CGROUP_INET6_CONNECT:
1250 /* last field in 'union bpf_attr' used by this command */
1251 #define BPF_PROG_LOAD_LAST_FIELD expected_attach_type
1253 static int bpf_prog_load(union bpf_attr *attr)
1255 enum bpf_prog_type type = attr->prog_type;
1256 struct bpf_prog *prog;
1261 if (CHECK_ATTR(BPF_PROG_LOAD))
1264 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1267 /* copy eBPF program license from user space */
1268 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1269 sizeof(license) - 1) < 0)
1271 license[sizeof(license) - 1] = 0;
1273 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1274 is_gpl = license_is_gpl_compatible(license);
1276 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1279 if (type == BPF_PROG_TYPE_KPROBE &&
1280 attr->kern_version != LINUX_VERSION_CODE)
1283 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1284 type != BPF_PROG_TYPE_CGROUP_SKB &&
1285 !capable(CAP_SYS_ADMIN))
1288 bpf_prog_load_fixup_attach_type(attr);
1289 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1292 /* plain bpf_prog allocation */
1293 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1297 prog->expected_attach_type = attr->expected_attach_type;
1299 prog->aux->offload_requested = !!attr->prog_ifindex;
1301 err = security_bpf_prog_alloc(prog->aux);
1303 goto free_prog_nouncharge;
1305 err = bpf_prog_charge_memlock(prog);
1309 prog->len = attr->insn_cnt;
1312 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1313 bpf_prog_insn_size(prog)) != 0)
1316 prog->orig_prog = NULL;
1319 atomic_set(&prog->aux->refcnt, 1);
1320 prog->gpl_compatible = is_gpl ? 1 : 0;
1322 if (bpf_prog_is_dev_bound(prog->aux)) {
1323 err = bpf_prog_offload_init(prog, attr);
1328 /* find program type: socket_filter vs tracing_filter */
1329 err = find_prog_type(type, prog);
1333 prog->aux->load_time = ktime_get_boot_ns();
1334 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1338 /* run eBPF verifier */
1339 err = bpf_check(&prog, attr);
1341 goto free_used_maps;
1343 /* eBPF program is ready to be JITed */
1344 if (!prog->bpf_func)
1345 prog = bpf_prog_select_runtime(prog, &err);
1347 goto free_used_maps;
1349 err = bpf_prog_alloc_id(prog);
1351 goto free_used_maps;
1353 err = bpf_prog_new_fd(prog);
1355 /* failed to allocate fd.
1356 * bpf_prog_put() is needed because the above
1357 * bpf_prog_alloc_id() has published the prog
1358 * to the userspace and the userspace may
1359 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1365 bpf_prog_kallsyms_add(prog);
1369 free_used_maps(prog->aux);
1371 bpf_prog_uncharge_memlock(prog);
1373 security_bpf_prog_free(prog->aux);
1374 free_prog_nouncharge:
1375 bpf_prog_free(prog);
1379 #define BPF_OBJ_LAST_FIELD file_flags
1381 static int bpf_obj_pin(const union bpf_attr *attr)
1383 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1386 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1389 static int bpf_obj_get(const union bpf_attr *attr)
1391 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1392 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1395 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1399 struct bpf_raw_tracepoint {
1400 struct bpf_raw_event_map *btp;
1401 struct bpf_prog *prog;
1404 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1406 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1409 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1410 bpf_prog_put(raw_tp->prog);
1416 static const struct file_operations bpf_raw_tp_fops = {
1417 .release = bpf_raw_tracepoint_release,
1418 .read = bpf_dummy_read,
1419 .write = bpf_dummy_write,
1422 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1424 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1426 struct bpf_raw_tracepoint *raw_tp;
1427 struct bpf_raw_event_map *btp;
1428 struct bpf_prog *prog;
1432 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1433 sizeof(tp_name) - 1) < 0)
1435 tp_name[sizeof(tp_name) - 1] = 0;
1437 btp = bpf_find_raw_tracepoint(tp_name);
1441 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1446 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1447 BPF_PROG_TYPE_RAW_TRACEPOINT);
1449 err = PTR_ERR(prog);
1453 err = bpf_probe_register(raw_tp->btp, prog);
1457 raw_tp->prog = prog;
1458 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1461 bpf_probe_unregister(raw_tp->btp, prog);
1474 #ifdef CONFIG_CGROUP_BPF
1476 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1477 enum bpf_attach_type attach_type)
1479 switch (prog->type) {
1480 case BPF_PROG_TYPE_CGROUP_SOCK:
1481 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1482 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1488 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1490 static int sockmap_get_from_fd(const union bpf_attr *attr,
1491 int type, bool attach)
1493 struct bpf_prog *prog = NULL;
1494 int ufd = attr->target_fd;
1495 struct bpf_map *map;
1500 map = __bpf_map_get(f);
1502 return PTR_ERR(map);
1505 prog = bpf_prog_get_type(attr->attach_bpf_fd, type);
1508 return PTR_ERR(prog);
1512 err = sock_map_prog(map, prog, attr->attach_type);
1524 #define BPF_F_ATTACH_MASK \
1525 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1527 static int bpf_prog_attach(const union bpf_attr *attr)
1529 enum bpf_prog_type ptype;
1530 struct bpf_prog *prog;
1531 struct cgroup *cgrp;
1534 if (!capable(CAP_NET_ADMIN))
1537 if (CHECK_ATTR(BPF_PROG_ATTACH))
1540 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
1543 switch (attr->attach_type) {
1544 case BPF_CGROUP_INET_INGRESS:
1545 case BPF_CGROUP_INET_EGRESS:
1546 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1548 case BPF_CGROUP_INET_SOCK_CREATE:
1549 case BPF_CGROUP_INET4_POST_BIND:
1550 case BPF_CGROUP_INET6_POST_BIND:
1551 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1553 case BPF_CGROUP_INET4_BIND:
1554 case BPF_CGROUP_INET6_BIND:
1555 case BPF_CGROUP_INET4_CONNECT:
1556 case BPF_CGROUP_INET6_CONNECT:
1557 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1559 case BPF_CGROUP_SOCK_OPS:
1560 ptype = BPF_PROG_TYPE_SOCK_OPS;
1562 case BPF_CGROUP_DEVICE:
1563 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1565 case BPF_SK_MSG_VERDICT:
1566 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true);
1567 case BPF_SK_SKB_STREAM_PARSER:
1568 case BPF_SK_SKB_STREAM_VERDICT:
1569 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true);
1574 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1576 return PTR_ERR(prog);
1578 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1583 cgrp = cgroup_get_from_fd(attr->target_fd);
1586 return PTR_ERR(cgrp);
1589 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1590 attr->attach_flags);
1598 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1600 static int bpf_prog_detach(const union bpf_attr *attr)
1602 enum bpf_prog_type ptype;
1603 struct bpf_prog *prog;
1604 struct cgroup *cgrp;
1607 if (!capable(CAP_NET_ADMIN))
1610 if (CHECK_ATTR(BPF_PROG_DETACH))
1613 switch (attr->attach_type) {
1614 case BPF_CGROUP_INET_INGRESS:
1615 case BPF_CGROUP_INET_EGRESS:
1616 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1618 case BPF_CGROUP_INET_SOCK_CREATE:
1619 case BPF_CGROUP_INET4_POST_BIND:
1620 case BPF_CGROUP_INET6_POST_BIND:
1621 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1623 case BPF_CGROUP_INET4_BIND:
1624 case BPF_CGROUP_INET6_BIND:
1625 case BPF_CGROUP_INET4_CONNECT:
1626 case BPF_CGROUP_INET6_CONNECT:
1627 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1629 case BPF_CGROUP_SOCK_OPS:
1630 ptype = BPF_PROG_TYPE_SOCK_OPS;
1632 case BPF_CGROUP_DEVICE:
1633 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1635 case BPF_SK_MSG_VERDICT:
1636 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false);
1637 case BPF_SK_SKB_STREAM_PARSER:
1638 case BPF_SK_SKB_STREAM_VERDICT:
1639 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false);
1644 cgrp = cgroup_get_from_fd(attr->target_fd);
1646 return PTR_ERR(cgrp);
1648 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1652 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1659 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1661 static int bpf_prog_query(const union bpf_attr *attr,
1662 union bpf_attr __user *uattr)
1664 struct cgroup *cgrp;
1667 if (!capable(CAP_NET_ADMIN))
1669 if (CHECK_ATTR(BPF_PROG_QUERY))
1671 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1674 switch (attr->query.attach_type) {
1675 case BPF_CGROUP_INET_INGRESS:
1676 case BPF_CGROUP_INET_EGRESS:
1677 case BPF_CGROUP_INET_SOCK_CREATE:
1678 case BPF_CGROUP_INET4_BIND:
1679 case BPF_CGROUP_INET6_BIND:
1680 case BPF_CGROUP_INET4_POST_BIND:
1681 case BPF_CGROUP_INET6_POST_BIND:
1682 case BPF_CGROUP_INET4_CONNECT:
1683 case BPF_CGROUP_INET6_CONNECT:
1684 case BPF_CGROUP_SOCK_OPS:
1685 case BPF_CGROUP_DEVICE:
1690 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1692 return PTR_ERR(cgrp);
1693 ret = cgroup_bpf_query(cgrp, attr, uattr);
1697 #endif /* CONFIG_CGROUP_BPF */
1699 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1701 static int bpf_prog_test_run(const union bpf_attr *attr,
1702 union bpf_attr __user *uattr)
1704 struct bpf_prog *prog;
1705 int ret = -ENOTSUPP;
1707 if (!capable(CAP_SYS_ADMIN))
1709 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1712 prog = bpf_prog_get(attr->test.prog_fd);
1714 return PTR_ERR(prog);
1716 if (prog->aux->ops->test_run)
1717 ret = prog->aux->ops->test_run(prog, attr, uattr);
1723 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1725 static int bpf_obj_get_next_id(const union bpf_attr *attr,
1726 union bpf_attr __user *uattr,
1730 u32 next_id = attr->start_id;
1733 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1736 if (!capable(CAP_SYS_ADMIN))
1741 if (!idr_get_next(idr, &next_id))
1743 spin_unlock_bh(lock);
1746 err = put_user(next_id, &uattr->next_id);
1751 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1753 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1755 struct bpf_prog *prog;
1756 u32 id = attr->prog_id;
1759 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1762 if (!capable(CAP_SYS_ADMIN))
1765 spin_lock_bh(&prog_idr_lock);
1766 prog = idr_find(&prog_idr, id);
1768 prog = bpf_prog_inc_not_zero(prog);
1770 prog = ERR_PTR(-ENOENT);
1771 spin_unlock_bh(&prog_idr_lock);
1774 return PTR_ERR(prog);
1776 fd = bpf_prog_new_fd(prog);
1783 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
1785 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1787 struct bpf_map *map;
1788 u32 id = attr->map_id;
1792 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1793 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
1796 if (!capable(CAP_SYS_ADMIN))
1799 f_flags = bpf_get_file_flag(attr->open_flags);
1803 spin_lock_bh(&map_idr_lock);
1804 map = idr_find(&map_idr, id);
1806 map = bpf_map_inc_not_zero(map, true);
1808 map = ERR_PTR(-ENOENT);
1809 spin_unlock_bh(&map_idr_lock);
1812 return PTR_ERR(map);
1814 fd = bpf_map_new_fd(map, f_flags);
1821 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1826 for (i = 0; i < prog->aux->used_map_cnt; i++)
1827 if (prog->aux->used_maps[i] == (void *)addr)
1828 return prog->aux->used_maps[i];
1832 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1834 const struct bpf_map *map;
1835 struct bpf_insn *insns;
1839 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1844 for (i = 0; i < prog->len; i++) {
1845 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1846 insns[i].code = BPF_JMP | BPF_CALL;
1847 insns[i].imm = BPF_FUNC_tail_call;
1850 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1851 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1852 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1853 insns[i].code = BPF_JMP | BPF_CALL;
1854 if (!bpf_dump_raw_ok())
1859 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1862 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1863 map = bpf_map_from_imm(prog, imm);
1865 insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1866 insns[i].imm = map->id;
1867 insns[i + 1].imm = 0;
1871 if (!bpf_dump_raw_ok() &&
1872 imm == (unsigned long)prog->aux) {
1874 insns[i + 1].imm = 0;
1882 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1883 const union bpf_attr *attr,
1884 union bpf_attr __user *uattr)
1886 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1887 struct bpf_prog_info info = {};
1888 u32 info_len = attr->info.info_len;
1889 char __user *uinsns;
1893 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1896 info_len = min_t(u32, sizeof(info), info_len);
1898 if (copy_from_user(&info, uinfo, info_len))
1901 info.type = prog->type;
1902 info.id = prog->aux->id;
1903 info.load_time = prog->aux->load_time;
1904 info.created_by_uid = from_kuid_munged(current_user_ns(),
1905 prog->aux->user->uid);
1906 info.gpl_compatible = prog->gpl_compatible;
1908 memcpy(info.tag, prog->tag, sizeof(prog->tag));
1909 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1911 ulen = info.nr_map_ids;
1912 info.nr_map_ids = prog->aux->used_map_cnt;
1913 ulen = min_t(u32, info.nr_map_ids, ulen);
1915 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
1918 for (i = 0; i < ulen; i++)
1919 if (put_user(prog->aux->used_maps[i]->id,
1924 if (!capable(CAP_SYS_ADMIN)) {
1925 info.jited_prog_len = 0;
1926 info.xlated_prog_len = 0;
1930 ulen = info.xlated_prog_len;
1931 info.xlated_prog_len = bpf_prog_insn_size(prog);
1932 if (info.xlated_prog_len && ulen) {
1933 struct bpf_insn *insns_sanitized;
1936 if (prog->blinded && !bpf_dump_raw_ok()) {
1937 info.xlated_prog_insns = 0;
1940 insns_sanitized = bpf_insn_prepare_dump(prog);
1941 if (!insns_sanitized)
1943 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1944 ulen = min_t(u32, info.xlated_prog_len, ulen);
1945 fault = copy_to_user(uinsns, insns_sanitized, ulen);
1946 kfree(insns_sanitized);
1951 if (bpf_prog_is_dev_bound(prog->aux)) {
1952 err = bpf_prog_offload_info_fill(&info, prog);
1958 /* NOTE: the following code is supposed to be skipped for offload.
1959 * bpf_prog_offload_info_fill() is the place to fill similar fields
1962 ulen = info.jited_prog_len;
1963 info.jited_prog_len = prog->jited_len;
1964 if (info.jited_prog_len && ulen) {
1965 if (bpf_dump_raw_ok()) {
1966 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1967 ulen = min_t(u32, info.jited_prog_len, ulen);
1968 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1971 info.jited_prog_insns = 0;
1976 if (copy_to_user(uinfo, &info, info_len) ||
1977 put_user(info_len, &uattr->info.info_len))
1983 static int bpf_map_get_info_by_fd(struct bpf_map *map,
1984 const union bpf_attr *attr,
1985 union bpf_attr __user *uattr)
1987 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1988 struct bpf_map_info info = {};
1989 u32 info_len = attr->info.info_len;
1992 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1995 info_len = min_t(u32, sizeof(info), info_len);
1997 info.type = map->map_type;
1999 info.key_size = map->key_size;
2000 info.value_size = map->value_size;
2001 info.max_entries = map->max_entries;
2002 info.map_flags = map->map_flags;
2003 memcpy(info.name, map->name, sizeof(map->name));
2006 info.btf_id = btf_id(map->btf);
2007 info.btf_key_id = map->btf_key_id;
2008 info.btf_value_id = map->btf_value_id;
2011 if (bpf_map_is_dev_bound(map)) {
2012 err = bpf_map_offload_info_fill(&info, map);
2017 if (copy_to_user(uinfo, &info, info_len) ||
2018 put_user(info_len, &uattr->info.info_len))
2024 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2026 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2027 union bpf_attr __user *uattr)
2029 int ufd = attr->info.bpf_fd;
2033 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2040 if (f.file->f_op == &bpf_prog_fops)
2041 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2043 else if (f.file->f_op == &bpf_map_fops)
2044 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2046 else if (f.file->f_op == &btf_fops)
2047 err = btf_get_info_by_fd(f.file->private_data, attr, uattr);
2055 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2057 static int bpf_btf_load(const union bpf_attr *attr)
2059 if (CHECK_ATTR(BPF_BTF_LOAD))
2062 if (!capable(CAP_SYS_ADMIN))
2065 return btf_new_fd(attr);
2068 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2070 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2072 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2075 if (!capable(CAP_SYS_ADMIN))
2078 return btf_get_fd_by_id(attr->btf_id);
2081 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2083 union bpf_attr attr = {};
2086 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
2089 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
2092 size = min_t(u32, size, sizeof(attr));
2094 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2095 if (copy_from_user(&attr, uattr, size) != 0)
2098 err = security_bpf(cmd, &attr, size);
2103 case BPF_MAP_CREATE:
2104 err = map_create(&attr);
2106 case BPF_MAP_LOOKUP_ELEM:
2107 err = map_lookup_elem(&attr);
2109 case BPF_MAP_UPDATE_ELEM:
2110 err = map_update_elem(&attr);
2112 case BPF_MAP_DELETE_ELEM:
2113 err = map_delete_elem(&attr);
2115 case BPF_MAP_GET_NEXT_KEY:
2116 err = map_get_next_key(&attr);
2119 err = bpf_prog_load(&attr);
2122 err = bpf_obj_pin(&attr);
2125 err = bpf_obj_get(&attr);
2127 #ifdef CONFIG_CGROUP_BPF
2128 case BPF_PROG_ATTACH:
2129 err = bpf_prog_attach(&attr);
2131 case BPF_PROG_DETACH:
2132 err = bpf_prog_detach(&attr);
2134 case BPF_PROG_QUERY:
2135 err = bpf_prog_query(&attr, uattr);
2138 case BPF_PROG_TEST_RUN:
2139 err = bpf_prog_test_run(&attr, uattr);
2141 case BPF_PROG_GET_NEXT_ID:
2142 err = bpf_obj_get_next_id(&attr, uattr,
2143 &prog_idr, &prog_idr_lock);
2145 case BPF_MAP_GET_NEXT_ID:
2146 err = bpf_obj_get_next_id(&attr, uattr,
2147 &map_idr, &map_idr_lock);
2149 case BPF_PROG_GET_FD_BY_ID:
2150 err = bpf_prog_get_fd_by_id(&attr);
2152 case BPF_MAP_GET_FD_BY_ID:
2153 err = bpf_map_get_fd_by_id(&attr);
2155 case BPF_OBJ_GET_INFO_BY_FD:
2156 err = bpf_obj_get_info_by_fd(&attr, uattr);
2158 case BPF_RAW_TRACEPOINT_OPEN:
2159 err = bpf_raw_tracepoint_open(&attr);
2162 err = bpf_btf_load(&attr);
2164 case BPF_BTF_GET_FD_BY_ID:
2165 err = bpf_btf_get_fd_by_id(&attr);