1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 #include <linux/bpf.h>
13 #include <linux/bpf_trace.h>
14 #include <linux/bpf_lirc.h>
15 #include <linux/btf.h>
16 #include <linux/syscalls.h>
17 #include <linux/slab.h>
18 #include <linux/sched/signal.h>
19 #include <linux/vmalloc.h>
20 #include <linux/mmzone.h>
21 #include <linux/anon_inodes.h>
22 #include <linux/fdtable.h>
23 #include <linux/file.h>
25 #include <linux/license.h>
26 #include <linux/filter.h>
27 #include <linux/version.h>
28 #include <linux/kernel.h>
29 #include <linux/idr.h>
30 #include <linux/cred.h>
31 #include <linux/timekeeping.h>
32 #include <linux/ctype.h>
33 #include <linux/nospec.h>
35 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
36 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
38 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
39 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
40 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
42 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
44 DEFINE_PER_CPU(int, bpf_prog_active);
45 static DEFINE_IDR(prog_idr);
46 static DEFINE_SPINLOCK(prog_idr_lock);
47 static DEFINE_IDR(map_idr);
48 static DEFINE_SPINLOCK(map_idr_lock);
50 int sysctl_unprivileged_bpf_disabled __read_mostly;
52 static const struct bpf_map_ops * const bpf_map_types[] = {
53 #define BPF_PROG_TYPE(_id, _ops)
54 #define BPF_MAP_TYPE(_id, _ops) \
56 #include <linux/bpf_types.h>
62 * If we're handed a bigger struct than we know of, ensure all the unknown bits
63 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
64 * we don't know about yet.
66 * There is a ToCToU between this function call and the following
67 * copy_from_user() call. However, this is not a concern since this function is
68 * meant to be a future-proofing of bits.
70 int bpf_check_uarg_tail_zero(void __user *uaddr,
74 unsigned char __user *addr;
75 unsigned char __user *end;
79 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
82 if (unlikely(!access_ok(uaddr, actual_size)))
85 if (actual_size <= expected_size)
88 addr = uaddr + expected_size;
89 end = uaddr + actual_size;
91 for (; addr < end; addr++) {
92 err = get_user(val, addr);
102 const struct bpf_map_ops bpf_map_offload_ops = {
103 .map_alloc = bpf_map_offload_map_alloc,
104 .map_free = bpf_map_offload_map_free,
105 .map_check_btf = map_check_no_btf,
108 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
110 const struct bpf_map_ops *ops;
111 u32 type = attr->map_type;
115 if (type >= ARRAY_SIZE(bpf_map_types))
116 return ERR_PTR(-EINVAL);
117 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
118 ops = bpf_map_types[type];
120 return ERR_PTR(-EINVAL);
122 if (ops->map_alloc_check) {
123 err = ops->map_alloc_check(attr);
127 if (attr->map_ifindex)
128 ops = &bpf_map_offload_ops;
129 map = ops->map_alloc(attr);
133 map->map_type = type;
137 void *bpf_map_area_alloc(size_t size, int numa_node)
139 /* We really just want to fail instead of triggering OOM killer
140 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
141 * which is used for lower order allocation requests.
143 * It has been observed that higher order allocation requests done by
144 * vmalloc with __GFP_NORETRY being set might fail due to not trying
145 * to reclaim memory from the page cache, thus we set
146 * __GFP_RETRY_MAYFAIL to avoid such situations.
149 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
152 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
153 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
159 return __vmalloc_node_flags_caller(size, numa_node,
160 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
161 flags, __builtin_return_address(0));
164 void bpf_map_area_free(void *area)
169 static u32 bpf_map_flags_retain_permanent(u32 flags)
171 /* Some map creation flags are not tied to the map object but
172 * rather to the map fd instead, so they have no meaning upon
173 * map object inspection since multiple file descriptors with
174 * different (access) properties can exist here. Thus, given
175 * this has zero meaning for the map itself, lets clear these
178 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
181 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
183 map->map_type = attr->map_type;
184 map->key_size = attr->key_size;
185 map->value_size = attr->value_size;
186 map->max_entries = attr->max_entries;
187 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
188 map->numa_node = bpf_map_attr_numa_node(attr);
191 int bpf_map_precharge_memlock(u32 pages)
193 struct user_struct *user = get_current_user();
194 unsigned long memlock_limit, cur;
196 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
197 cur = atomic_long_read(&user->locked_vm);
199 if (cur + pages > memlock_limit)
204 static int bpf_charge_memlock(struct user_struct *user, u32 pages)
206 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
208 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
209 atomic_long_sub(pages, &user->locked_vm);
215 static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
217 atomic_long_sub(pages, &user->locked_vm);
220 static int bpf_map_init_memlock(struct bpf_map *map)
222 struct user_struct *user = get_current_user();
225 ret = bpf_charge_memlock(user, map->pages);
234 static void bpf_map_release_memlock(struct bpf_map *map)
236 struct user_struct *user = map->user;
237 bpf_uncharge_memlock(user, map->pages);
241 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
245 ret = bpf_charge_memlock(map->user, pages);
252 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
254 bpf_uncharge_memlock(map->user, pages);
258 static int bpf_map_alloc_id(struct bpf_map *map)
262 idr_preload(GFP_KERNEL);
263 spin_lock_bh(&map_idr_lock);
264 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
267 spin_unlock_bh(&map_idr_lock);
270 if (WARN_ON_ONCE(!id))
273 return id > 0 ? 0 : id;
276 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
280 /* Offloaded maps are removed from the IDR store when their device
281 * disappears - even if someone holds an fd to them they are unusable,
282 * the memory is gone, all ops will fail; they are simply waiting for
283 * refcnt to drop to be freed.
289 spin_lock_irqsave(&map_idr_lock, flags);
291 __acquire(&map_idr_lock);
293 idr_remove(&map_idr, map->id);
297 spin_unlock_irqrestore(&map_idr_lock, flags);
299 __release(&map_idr_lock);
302 /* called from workqueue */
303 static void bpf_map_free_deferred(struct work_struct *work)
305 struct bpf_map *map = container_of(work, struct bpf_map, work);
307 bpf_map_release_memlock(map);
308 security_bpf_map_free(map);
309 /* implementation dependent freeing */
310 map->ops->map_free(map);
313 static void bpf_map_put_uref(struct bpf_map *map)
315 if (atomic_dec_and_test(&map->usercnt)) {
316 if (map->ops->map_release_uref)
317 map->ops->map_release_uref(map);
321 /* decrement map refcnt and schedule it for freeing via workqueue
322 * (unrelying map implementation ops->map_free() might sleep)
324 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
326 if (atomic_dec_and_test(&map->refcnt)) {
327 /* bpf_map_free_id() must be called first */
328 bpf_map_free_id(map, do_idr_lock);
330 INIT_WORK(&map->work, bpf_map_free_deferred);
331 schedule_work(&map->work);
335 void bpf_map_put(struct bpf_map *map)
337 __bpf_map_put(map, true);
339 EXPORT_SYMBOL_GPL(bpf_map_put);
341 void bpf_map_put_with_uref(struct bpf_map *map)
343 bpf_map_put_uref(map);
347 static int bpf_map_release(struct inode *inode, struct file *filp)
349 struct bpf_map *map = filp->private_data;
351 if (map->ops->map_release)
352 map->ops->map_release(map, filp);
354 bpf_map_put_with_uref(map);
358 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
360 fmode_t mode = f.file->f_mode;
362 /* Our file permissions may have been overridden by global
363 * map permissions facing syscall side.
365 if (READ_ONCE(map->frozen))
366 mode &= ~FMODE_CAN_WRITE;
370 #ifdef CONFIG_PROC_FS
371 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
373 const struct bpf_map *map = filp->private_data;
374 const struct bpf_array *array;
375 u32 owner_prog_type = 0;
378 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
379 array = container_of(map, struct bpf_array, map);
380 owner_prog_type = array->owner_prog_type;
381 owner_jited = array->owner_jited;
398 map->pages * 1ULL << PAGE_SHIFT,
400 READ_ONCE(map->frozen));
402 if (owner_prog_type) {
403 seq_printf(m, "owner_prog_type:\t%u\n",
405 seq_printf(m, "owner_jited:\t%u\n",
411 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
414 /* We need this handler such that alloc_file() enables
415 * f_mode with FMODE_CAN_READ.
420 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
421 size_t siz, loff_t *ppos)
423 /* We need this handler such that alloc_file() enables
424 * f_mode with FMODE_CAN_WRITE.
429 const struct file_operations bpf_map_fops = {
430 #ifdef CONFIG_PROC_FS
431 .show_fdinfo = bpf_map_show_fdinfo,
433 .release = bpf_map_release,
434 .read = bpf_dummy_read,
435 .write = bpf_dummy_write,
438 int bpf_map_new_fd(struct bpf_map *map, int flags)
442 ret = security_bpf_map(map, OPEN_FMODE(flags));
446 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
450 int bpf_get_file_flag(int flags)
452 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
454 if (flags & BPF_F_RDONLY)
456 if (flags & BPF_F_WRONLY)
461 /* helper macro to check that unused fields 'union bpf_attr' are zero */
462 #define CHECK_ATTR(CMD) \
463 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
464 sizeof(attr->CMD##_LAST_FIELD), 0, \
466 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
467 sizeof(attr->CMD##_LAST_FIELD)) != NULL
469 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
470 * Return 0 on success and < 0 on error.
472 static int bpf_obj_name_cpy(char *dst, const char *src)
474 const char *end = src + BPF_OBJ_NAME_LEN;
476 memset(dst, 0, BPF_OBJ_NAME_LEN);
477 /* Copy all isalnum(), '_' and '.' chars. */
478 while (src < end && *src) {
479 if (!isalnum(*src) &&
480 *src != '_' && *src != '.')
485 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
492 int map_check_no_btf(const struct bpf_map *map,
493 const struct btf *btf,
494 const struct btf_type *key_type,
495 const struct btf_type *value_type)
500 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
501 u32 btf_key_id, u32 btf_value_id)
503 const struct btf_type *key_type, *value_type;
504 u32 key_size, value_size;
507 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
508 if (!key_type || key_size != map->key_size)
511 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
512 if (!value_type || value_size != map->value_size)
515 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
517 if (map_value_has_spin_lock(map)) {
518 if (map->map_flags & BPF_F_RDONLY_PROG)
520 if (map->map_type != BPF_MAP_TYPE_HASH &&
521 map->map_type != BPF_MAP_TYPE_ARRAY &&
522 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE)
524 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
527 "verifier bug spin_lock_off %d value_size %d\n",
528 map->spin_lock_off, map->value_size);
533 if (map->ops->map_check_btf)
534 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
539 #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
540 /* called via syscall */
541 static int map_create(union bpf_attr *attr)
543 int numa_node = bpf_map_attr_numa_node(attr);
548 err = CHECK_ATTR(BPF_MAP_CREATE);
552 f_flags = bpf_get_file_flag(attr->map_flags);
556 if (numa_node != NUMA_NO_NODE &&
557 ((unsigned int)numa_node >= nr_node_ids ||
558 !node_online(numa_node)))
561 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
562 map = find_and_alloc_map(attr);
566 err = bpf_obj_name_cpy(map->name, attr->map_name);
568 goto free_map_nouncharge;
570 atomic_set(&map->refcnt, 1);
571 atomic_set(&map->usercnt, 1);
573 if (attr->btf_key_type_id || attr->btf_value_type_id) {
576 if (!attr->btf_key_type_id || !attr->btf_value_type_id) {
578 goto free_map_nouncharge;
581 btf = btf_get_by_fd(attr->btf_fd);
584 goto free_map_nouncharge;
587 err = map_check_btf(map, btf, attr->btf_key_type_id,
588 attr->btf_value_type_id);
591 goto free_map_nouncharge;
595 map->btf_key_type_id = attr->btf_key_type_id;
596 map->btf_value_type_id = attr->btf_value_type_id;
598 map->spin_lock_off = -EINVAL;
601 err = security_bpf_map_alloc(map);
603 goto free_map_nouncharge;
605 err = bpf_map_init_memlock(map);
609 err = bpf_map_alloc_id(map);
613 err = bpf_map_new_fd(map, f_flags);
615 /* failed to allocate fd.
616 * bpf_map_put_with_uref() is needed because the above
617 * bpf_map_alloc_id() has published the map
618 * to the userspace and the userspace may
619 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
621 bpf_map_put_with_uref(map);
628 bpf_map_release_memlock(map);
630 security_bpf_map_free(map);
633 map->ops->map_free(map);
637 /* if error is returned, fd is released.
638 * On success caller should complete fd access with matching fdput()
640 struct bpf_map *__bpf_map_get(struct fd f)
643 return ERR_PTR(-EBADF);
644 if (f.file->f_op != &bpf_map_fops) {
646 return ERR_PTR(-EINVAL);
649 return f.file->private_data;
652 /* prog's and map's refcnt limit */
653 #define BPF_MAX_REFCNT 32768
655 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
657 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
658 atomic_dec(&map->refcnt);
659 return ERR_PTR(-EBUSY);
662 atomic_inc(&map->usercnt);
665 EXPORT_SYMBOL_GPL(bpf_map_inc);
667 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
669 struct fd f = fdget(ufd);
672 map = __bpf_map_get(f);
676 map = bpf_map_inc(map, true);
682 /* map_idr_lock should have been held */
683 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
688 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
690 if (refold >= BPF_MAX_REFCNT) {
691 __bpf_map_put(map, false);
692 return ERR_PTR(-EBUSY);
696 return ERR_PTR(-ENOENT);
699 atomic_inc(&map->usercnt);
704 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
709 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
712 return memdup_user(ukey, key_size);
715 return ERR_PTR(-EINVAL);
720 /* last field in 'union bpf_attr' used by this command */
721 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
723 static int map_lookup_elem(union bpf_attr *attr)
725 void __user *ukey = u64_to_user_ptr(attr->key);
726 void __user *uvalue = u64_to_user_ptr(attr->value);
727 int ufd = attr->map_fd;
729 void *key, *value, *ptr;
734 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
737 if (attr->flags & ~BPF_F_LOCK)
741 map = __bpf_map_get(f);
744 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
749 if ((attr->flags & BPF_F_LOCK) &&
750 !map_value_has_spin_lock(map)) {
755 key = __bpf_copy_key(ukey, map->key_size);
761 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
762 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
763 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
764 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
765 value_size = round_up(map->value_size, 8) * num_possible_cpus();
766 else if (IS_FD_MAP(map))
767 value_size = sizeof(u32);
769 value_size = map->value_size;
772 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
776 if (bpf_map_is_dev_bound(map)) {
777 err = bpf_map_offload_lookup_elem(map, key, value);
782 this_cpu_inc(bpf_prog_active);
783 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
784 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
785 err = bpf_percpu_hash_copy(map, key, value);
786 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
787 err = bpf_percpu_array_copy(map, key, value);
788 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
789 err = bpf_percpu_cgroup_storage_copy(map, key, value);
790 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
791 err = bpf_stackmap_copy(map, key, value);
792 } else if (IS_FD_ARRAY(map)) {
793 err = bpf_fd_array_map_lookup_elem(map, key, value);
794 } else if (IS_FD_HASH(map)) {
795 err = bpf_fd_htab_map_lookup_elem(map, key, value);
796 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
797 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
798 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
799 map->map_type == BPF_MAP_TYPE_STACK) {
800 err = map->ops->map_peek_elem(map, value);
803 ptr = map->ops->map_lookup_elem(map, key);
810 if (attr->flags & BPF_F_LOCK)
811 /* lock 'ptr' and copy everything but lock */
812 copy_map_value_locked(map, value, ptr, true);
814 copy_map_value(map, value, ptr);
815 /* mask lock, since value wasn't zero inited */
816 check_and_init_map_lock(map, value);
820 this_cpu_dec(bpf_prog_active);
828 if (copy_to_user(uvalue, value, value_size) != 0)
842 static void maybe_wait_bpf_programs(struct bpf_map *map)
844 /* Wait for any running BPF programs to complete so that
845 * userspace, when we return to it, knows that all programs
846 * that could be running use the new map value.
848 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
849 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
853 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
855 static int map_update_elem(union bpf_attr *attr)
857 void __user *ukey = u64_to_user_ptr(attr->key);
858 void __user *uvalue = u64_to_user_ptr(attr->value);
859 int ufd = attr->map_fd;
866 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
870 map = __bpf_map_get(f);
873 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
878 if ((attr->flags & BPF_F_LOCK) &&
879 !map_value_has_spin_lock(map)) {
884 key = __bpf_copy_key(ukey, map->key_size);
890 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
891 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
892 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
893 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
894 value_size = round_up(map->value_size, 8) * num_possible_cpus();
896 value_size = map->value_size;
899 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
904 if (copy_from_user(value, uvalue, value_size) != 0)
907 /* Need to create a kthread, thus must support schedule */
908 if (bpf_map_is_dev_bound(map)) {
909 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
911 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
912 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
913 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
914 err = map->ops->map_update_elem(map, key, value, attr->flags);
918 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
919 * inside bpf map update or delete otherwise deadlocks are possible
922 __this_cpu_inc(bpf_prog_active);
923 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
924 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
925 err = bpf_percpu_hash_update(map, key, value, attr->flags);
926 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
927 err = bpf_percpu_array_update(map, key, value, attr->flags);
928 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
929 err = bpf_percpu_cgroup_storage_update(map, key, value,
931 } else if (IS_FD_ARRAY(map)) {
933 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
936 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
938 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
941 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
942 /* rcu_read_lock() is not needed */
943 err = bpf_fd_reuseport_array_update_elem(map, key, value,
945 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
946 map->map_type == BPF_MAP_TYPE_STACK) {
947 err = map->ops->map_push_elem(map, value, attr->flags);
950 err = map->ops->map_update_elem(map, key, value, attr->flags);
953 __this_cpu_dec(bpf_prog_active);
955 maybe_wait_bpf_programs(map);
966 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
968 static int map_delete_elem(union bpf_attr *attr)
970 void __user *ukey = u64_to_user_ptr(attr->key);
971 int ufd = attr->map_fd;
977 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
981 map = __bpf_map_get(f);
984 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
989 key = __bpf_copy_key(ukey, map->key_size);
995 if (bpf_map_is_dev_bound(map)) {
996 err = bpf_map_offload_delete_elem(map, key);
1001 __this_cpu_inc(bpf_prog_active);
1003 err = map->ops->map_delete_elem(map, key);
1005 __this_cpu_dec(bpf_prog_active);
1007 maybe_wait_bpf_programs(map);
1015 /* last field in 'union bpf_attr' used by this command */
1016 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1018 static int map_get_next_key(union bpf_attr *attr)
1020 void __user *ukey = u64_to_user_ptr(attr->key);
1021 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1022 int ufd = attr->map_fd;
1023 struct bpf_map *map;
1024 void *key, *next_key;
1028 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1032 map = __bpf_map_get(f);
1034 return PTR_ERR(map);
1035 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1041 key = __bpf_copy_key(ukey, map->key_size);
1051 next_key = kmalloc(map->key_size, GFP_USER);
1055 if (bpf_map_is_dev_bound(map)) {
1056 err = bpf_map_offload_get_next_key(map, key, next_key);
1061 err = map->ops->map_get_next_key(map, key, next_key);
1068 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1082 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1084 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1086 void __user *ukey = u64_to_user_ptr(attr->key);
1087 void __user *uvalue = u64_to_user_ptr(attr->value);
1088 int ufd = attr->map_fd;
1089 struct bpf_map *map;
1095 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1099 map = __bpf_map_get(f);
1101 return PTR_ERR(map);
1102 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1107 key = __bpf_copy_key(ukey, map->key_size);
1113 value_size = map->value_size;
1116 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1120 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1121 map->map_type == BPF_MAP_TYPE_STACK) {
1122 err = map->ops->map_pop_elem(map, value);
1130 if (copy_to_user(uvalue, value, value_size) != 0)
1144 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1146 static int map_freeze(const union bpf_attr *attr)
1148 int err = 0, ufd = attr->map_fd;
1149 struct bpf_map *map;
1152 if (CHECK_ATTR(BPF_MAP_FREEZE))
1156 map = __bpf_map_get(f);
1158 return PTR_ERR(map);
1159 if (READ_ONCE(map->frozen)) {
1163 if (!capable(CAP_SYS_ADMIN)) {
1168 WRITE_ONCE(map->frozen, true);
1174 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1175 #define BPF_PROG_TYPE(_id, _name) \
1176 [_id] = & _name ## _prog_ops,
1177 #define BPF_MAP_TYPE(_id, _ops)
1178 #include <linux/bpf_types.h>
1179 #undef BPF_PROG_TYPE
1183 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1185 const struct bpf_prog_ops *ops;
1187 if (type >= ARRAY_SIZE(bpf_prog_types))
1189 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1190 ops = bpf_prog_types[type];
1194 if (!bpf_prog_is_dev_bound(prog->aux))
1195 prog->aux->ops = ops;
1197 prog->aux->ops = &bpf_offload_prog_ops;
1202 /* drop refcnt on maps used by eBPF program and free auxilary data */
1203 static void free_used_maps(struct bpf_prog_aux *aux)
1205 enum bpf_cgroup_storage_type stype;
1208 for_each_cgroup_storage_type(stype) {
1209 if (!aux->cgroup_storage[stype])
1211 bpf_cgroup_storage_release(aux->prog,
1212 aux->cgroup_storage[stype]);
1215 for (i = 0; i < aux->used_map_cnt; i++)
1216 bpf_map_put(aux->used_maps[i]);
1218 kfree(aux->used_maps);
1221 int __bpf_prog_charge(struct user_struct *user, u32 pages)
1223 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1224 unsigned long user_bufs;
1227 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1228 if (user_bufs > memlock_limit) {
1229 atomic_long_sub(pages, &user->locked_vm);
1237 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1240 atomic_long_sub(pages, &user->locked_vm);
1243 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1245 struct user_struct *user = get_current_user();
1248 ret = __bpf_prog_charge(user, prog->pages);
1254 prog->aux->user = user;
1258 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1260 struct user_struct *user = prog->aux->user;
1262 __bpf_prog_uncharge(user, prog->pages);
1266 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1270 idr_preload(GFP_KERNEL);
1271 spin_lock_bh(&prog_idr_lock);
1272 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1275 spin_unlock_bh(&prog_idr_lock);
1278 /* id is in [1, INT_MAX) */
1279 if (WARN_ON_ONCE(!id))
1282 return id > 0 ? 0 : id;
1285 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1287 /* cBPF to eBPF migrations are currently not in the idr store.
1288 * Offloaded programs are removed from the store when their device
1289 * disappears - even if someone grabs an fd to them they are unusable,
1290 * simply waiting for refcnt to drop to be freed.
1296 spin_lock_bh(&prog_idr_lock);
1298 __acquire(&prog_idr_lock);
1300 idr_remove(&prog_idr, prog->aux->id);
1304 spin_unlock_bh(&prog_idr_lock);
1306 __release(&prog_idr_lock);
1309 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1311 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1313 free_used_maps(aux);
1314 bpf_prog_uncharge_memlock(aux->prog);
1315 security_bpf_prog_free(aux);
1316 bpf_prog_free(aux->prog);
1319 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1321 if (atomic_dec_and_test(&prog->aux->refcnt)) {
1322 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1323 /* bpf_prog_free_id() must be called first */
1324 bpf_prog_free_id(prog, do_idr_lock);
1325 bpf_prog_kallsyms_del_all(prog);
1326 btf_put(prog->aux->btf);
1327 kvfree(prog->aux->func_info);
1328 bpf_prog_free_linfo(prog);
1330 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1334 void bpf_prog_put(struct bpf_prog *prog)
1336 __bpf_prog_put(prog, true);
1338 EXPORT_SYMBOL_GPL(bpf_prog_put);
1340 static int bpf_prog_release(struct inode *inode, struct file *filp)
1342 struct bpf_prog *prog = filp->private_data;
1348 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1349 struct bpf_prog_stats *stats)
1351 u64 nsecs = 0, cnt = 0;
1354 for_each_possible_cpu(cpu) {
1355 const struct bpf_prog_stats *st;
1359 st = per_cpu_ptr(prog->aux->stats, cpu);
1361 start = u64_stats_fetch_begin_irq(&st->syncp);
1364 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1368 stats->nsecs = nsecs;
1372 #ifdef CONFIG_PROC_FS
1373 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1375 const struct bpf_prog *prog = filp->private_data;
1376 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1377 struct bpf_prog_stats stats;
1379 bpf_prog_get_stats(prog, &stats);
1380 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1387 "run_time_ns:\t%llu\n"
1392 prog->pages * 1ULL << PAGE_SHIFT,
1399 const struct file_operations bpf_prog_fops = {
1400 #ifdef CONFIG_PROC_FS
1401 .show_fdinfo = bpf_prog_show_fdinfo,
1403 .release = bpf_prog_release,
1404 .read = bpf_dummy_read,
1405 .write = bpf_dummy_write,
1408 int bpf_prog_new_fd(struct bpf_prog *prog)
1412 ret = security_bpf_prog(prog);
1416 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1417 O_RDWR | O_CLOEXEC);
1420 static struct bpf_prog *____bpf_prog_get(struct fd f)
1423 return ERR_PTR(-EBADF);
1424 if (f.file->f_op != &bpf_prog_fops) {
1426 return ERR_PTR(-EINVAL);
1429 return f.file->private_data;
1432 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
1434 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1435 atomic_sub(i, &prog->aux->refcnt);
1436 return ERR_PTR(-EBUSY);
1440 EXPORT_SYMBOL_GPL(bpf_prog_add);
1442 void bpf_prog_sub(struct bpf_prog *prog, int i)
1444 /* Only to be used for undoing previous bpf_prog_add() in some
1445 * error path. We still know that another entity in our call
1446 * path holds a reference to the program, thus atomic_sub() can
1447 * be safely used in such cases!
1449 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1451 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1453 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1455 return bpf_prog_add(prog, 1);
1457 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1459 /* prog_idr_lock should have been held */
1460 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1464 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1466 if (refold >= BPF_MAX_REFCNT) {
1467 __bpf_prog_put(prog, false);
1468 return ERR_PTR(-EBUSY);
1472 return ERR_PTR(-ENOENT);
1476 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1478 bool bpf_prog_get_ok(struct bpf_prog *prog,
1479 enum bpf_prog_type *attach_type, bool attach_drv)
1481 /* not an attachment, just a refcount inc, always allow */
1485 if (prog->type != *attach_type)
1487 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1493 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1496 struct fd f = fdget(ufd);
1497 struct bpf_prog *prog;
1499 prog = ____bpf_prog_get(f);
1502 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1503 prog = ERR_PTR(-EINVAL);
1507 prog = bpf_prog_inc(prog);
1513 struct bpf_prog *bpf_prog_get(u32 ufd)
1515 return __bpf_prog_get(ufd, NULL, false);
1518 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1521 return __bpf_prog_get(ufd, &type, attach_drv);
1523 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1525 /* Initially all BPF programs could be loaded w/o specifying
1526 * expected_attach_type. Later for some of them specifying expected_attach_type
1527 * at load time became required so that program could be validated properly.
1528 * Programs of types that are allowed to be loaded both w/ and w/o (for
1529 * backward compatibility) expected_attach_type, should have the default attach
1530 * type assigned to expected_attach_type for the latter case, so that it can be
1531 * validated later at attach time.
1533 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1534 * prog type requires it but has some attach types that have to be backward
1537 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1539 switch (attr->prog_type) {
1540 case BPF_PROG_TYPE_CGROUP_SOCK:
1541 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1542 * exist so checking for non-zero is the way to go here.
1544 if (!attr->expected_attach_type)
1545 attr->expected_attach_type =
1546 BPF_CGROUP_INET_SOCK_CREATE;
1552 bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1553 enum bpf_attach_type expected_attach_type)
1555 switch (prog_type) {
1556 case BPF_PROG_TYPE_CGROUP_SOCK:
1557 switch (expected_attach_type) {
1558 case BPF_CGROUP_INET_SOCK_CREATE:
1559 case BPF_CGROUP_INET4_POST_BIND:
1560 case BPF_CGROUP_INET6_POST_BIND:
1565 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1566 switch (expected_attach_type) {
1567 case BPF_CGROUP_INET4_BIND:
1568 case BPF_CGROUP_INET6_BIND:
1569 case BPF_CGROUP_INET4_CONNECT:
1570 case BPF_CGROUP_INET6_CONNECT:
1571 case BPF_CGROUP_UDP4_SENDMSG:
1572 case BPF_CGROUP_UDP6_SENDMSG:
1582 /* last field in 'union bpf_attr' used by this command */
1583 #define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
1585 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
1587 enum bpf_prog_type type = attr->prog_type;
1588 struct bpf_prog *prog;
1593 if (CHECK_ATTR(BPF_PROG_LOAD))
1596 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | BPF_F_ANY_ALIGNMENT))
1599 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1600 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1601 !capable(CAP_SYS_ADMIN))
1604 /* copy eBPF program license from user space */
1605 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1606 sizeof(license) - 1) < 0)
1608 license[sizeof(license) - 1] = 0;
1610 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1611 is_gpl = license_is_gpl_compatible(license);
1613 if (attr->insn_cnt == 0 ||
1614 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
1616 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1617 type != BPF_PROG_TYPE_CGROUP_SKB &&
1618 !capable(CAP_SYS_ADMIN))
1621 bpf_prog_load_fixup_attach_type(attr);
1622 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1625 /* plain bpf_prog allocation */
1626 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1630 prog->expected_attach_type = attr->expected_attach_type;
1632 prog->aux->offload_requested = !!attr->prog_ifindex;
1634 err = security_bpf_prog_alloc(prog->aux);
1636 goto free_prog_nouncharge;
1638 err = bpf_prog_charge_memlock(prog);
1642 prog->len = attr->insn_cnt;
1645 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1646 bpf_prog_insn_size(prog)) != 0)
1649 prog->orig_prog = NULL;
1652 atomic_set(&prog->aux->refcnt, 1);
1653 prog->gpl_compatible = is_gpl ? 1 : 0;
1655 if (bpf_prog_is_dev_bound(prog->aux)) {
1656 err = bpf_prog_offload_init(prog, attr);
1661 /* find program type: socket_filter vs tracing_filter */
1662 err = find_prog_type(type, prog);
1666 prog->aux->load_time = ktime_get_boot_ns();
1667 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1671 /* run eBPF verifier */
1672 err = bpf_check(&prog, attr, uattr);
1674 goto free_used_maps;
1676 prog = bpf_prog_select_runtime(prog, &err);
1678 goto free_used_maps;
1680 err = bpf_prog_alloc_id(prog);
1682 goto free_used_maps;
1684 err = bpf_prog_new_fd(prog);
1686 /* failed to allocate fd.
1687 * bpf_prog_put() is needed because the above
1688 * bpf_prog_alloc_id() has published the prog
1689 * to the userspace and the userspace may
1690 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1696 bpf_prog_kallsyms_add(prog);
1697 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
1701 bpf_prog_free_linfo(prog);
1702 kvfree(prog->aux->func_info);
1703 btf_put(prog->aux->btf);
1704 bpf_prog_kallsyms_del_subprogs(prog);
1705 free_used_maps(prog->aux);
1707 bpf_prog_uncharge_memlock(prog);
1709 security_bpf_prog_free(prog->aux);
1710 free_prog_nouncharge:
1711 bpf_prog_free(prog);
1715 #define BPF_OBJ_LAST_FIELD file_flags
1717 static int bpf_obj_pin(const union bpf_attr *attr)
1719 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1722 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1725 static int bpf_obj_get(const union bpf_attr *attr)
1727 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1728 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1731 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1735 struct bpf_raw_tracepoint {
1736 struct bpf_raw_event_map *btp;
1737 struct bpf_prog *prog;
1740 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1742 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1745 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1746 bpf_prog_put(raw_tp->prog);
1748 bpf_put_raw_tracepoint(raw_tp->btp);
1753 static const struct file_operations bpf_raw_tp_fops = {
1754 .release = bpf_raw_tracepoint_release,
1755 .read = bpf_dummy_read,
1756 .write = bpf_dummy_write,
1759 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1761 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1763 struct bpf_raw_tracepoint *raw_tp;
1764 struct bpf_raw_event_map *btp;
1765 struct bpf_prog *prog;
1769 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1770 sizeof(tp_name) - 1) < 0)
1772 tp_name[sizeof(tp_name) - 1] = 0;
1774 btp = bpf_get_raw_tracepoint(tp_name);
1778 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1785 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1786 BPF_PROG_TYPE_RAW_TRACEPOINT);
1788 err = PTR_ERR(prog);
1792 err = bpf_probe_register(raw_tp->btp, prog);
1796 raw_tp->prog = prog;
1797 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1800 bpf_probe_unregister(raw_tp->btp, prog);
1811 bpf_put_raw_tracepoint(btp);
1815 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1816 enum bpf_attach_type attach_type)
1818 switch (prog->type) {
1819 case BPF_PROG_TYPE_CGROUP_SOCK:
1820 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1821 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1827 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1829 #define BPF_F_ATTACH_MASK \
1830 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1832 static int bpf_prog_attach(const union bpf_attr *attr)
1834 enum bpf_prog_type ptype;
1835 struct bpf_prog *prog;
1838 if (!capable(CAP_NET_ADMIN))
1841 if (CHECK_ATTR(BPF_PROG_ATTACH))
1844 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
1847 switch (attr->attach_type) {
1848 case BPF_CGROUP_INET_INGRESS:
1849 case BPF_CGROUP_INET_EGRESS:
1850 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1852 case BPF_CGROUP_INET_SOCK_CREATE:
1853 case BPF_CGROUP_INET4_POST_BIND:
1854 case BPF_CGROUP_INET6_POST_BIND:
1855 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1857 case BPF_CGROUP_INET4_BIND:
1858 case BPF_CGROUP_INET6_BIND:
1859 case BPF_CGROUP_INET4_CONNECT:
1860 case BPF_CGROUP_INET6_CONNECT:
1861 case BPF_CGROUP_UDP4_SENDMSG:
1862 case BPF_CGROUP_UDP6_SENDMSG:
1863 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1865 case BPF_CGROUP_SOCK_OPS:
1866 ptype = BPF_PROG_TYPE_SOCK_OPS;
1868 case BPF_CGROUP_DEVICE:
1869 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1871 case BPF_SK_MSG_VERDICT:
1872 ptype = BPF_PROG_TYPE_SK_MSG;
1874 case BPF_SK_SKB_STREAM_PARSER:
1875 case BPF_SK_SKB_STREAM_VERDICT:
1876 ptype = BPF_PROG_TYPE_SK_SKB;
1878 case BPF_LIRC_MODE2:
1879 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1881 case BPF_FLOW_DISSECTOR:
1882 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1888 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1890 return PTR_ERR(prog);
1892 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1898 case BPF_PROG_TYPE_SK_SKB:
1899 case BPF_PROG_TYPE_SK_MSG:
1900 ret = sock_map_get_from_fd(attr, prog);
1902 case BPF_PROG_TYPE_LIRC_MODE2:
1903 ret = lirc_prog_attach(attr, prog);
1905 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1906 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1909 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
1917 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1919 static int bpf_prog_detach(const union bpf_attr *attr)
1921 enum bpf_prog_type ptype;
1923 if (!capable(CAP_NET_ADMIN))
1926 if (CHECK_ATTR(BPF_PROG_DETACH))
1929 switch (attr->attach_type) {
1930 case BPF_CGROUP_INET_INGRESS:
1931 case BPF_CGROUP_INET_EGRESS:
1932 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1934 case BPF_CGROUP_INET_SOCK_CREATE:
1935 case BPF_CGROUP_INET4_POST_BIND:
1936 case BPF_CGROUP_INET6_POST_BIND:
1937 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1939 case BPF_CGROUP_INET4_BIND:
1940 case BPF_CGROUP_INET6_BIND:
1941 case BPF_CGROUP_INET4_CONNECT:
1942 case BPF_CGROUP_INET6_CONNECT:
1943 case BPF_CGROUP_UDP4_SENDMSG:
1944 case BPF_CGROUP_UDP6_SENDMSG:
1945 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1947 case BPF_CGROUP_SOCK_OPS:
1948 ptype = BPF_PROG_TYPE_SOCK_OPS;
1950 case BPF_CGROUP_DEVICE:
1951 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1953 case BPF_SK_MSG_VERDICT:
1954 return sock_map_get_from_fd(attr, NULL);
1955 case BPF_SK_SKB_STREAM_PARSER:
1956 case BPF_SK_SKB_STREAM_VERDICT:
1957 return sock_map_get_from_fd(attr, NULL);
1958 case BPF_LIRC_MODE2:
1959 return lirc_prog_detach(attr);
1960 case BPF_FLOW_DISSECTOR:
1961 return skb_flow_dissector_bpf_prog_detach(attr);
1966 return cgroup_bpf_prog_detach(attr, ptype);
1969 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1971 static int bpf_prog_query(const union bpf_attr *attr,
1972 union bpf_attr __user *uattr)
1974 if (!capable(CAP_NET_ADMIN))
1976 if (CHECK_ATTR(BPF_PROG_QUERY))
1978 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1981 switch (attr->query.attach_type) {
1982 case BPF_CGROUP_INET_INGRESS:
1983 case BPF_CGROUP_INET_EGRESS:
1984 case BPF_CGROUP_INET_SOCK_CREATE:
1985 case BPF_CGROUP_INET4_BIND:
1986 case BPF_CGROUP_INET6_BIND:
1987 case BPF_CGROUP_INET4_POST_BIND:
1988 case BPF_CGROUP_INET6_POST_BIND:
1989 case BPF_CGROUP_INET4_CONNECT:
1990 case BPF_CGROUP_INET6_CONNECT:
1991 case BPF_CGROUP_UDP4_SENDMSG:
1992 case BPF_CGROUP_UDP6_SENDMSG:
1993 case BPF_CGROUP_SOCK_OPS:
1994 case BPF_CGROUP_DEVICE:
1996 case BPF_LIRC_MODE2:
1997 return lirc_prog_query(attr, uattr);
2002 return cgroup_bpf_prog_query(attr, uattr);
2005 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
2007 static int bpf_prog_test_run(const union bpf_attr *attr,
2008 union bpf_attr __user *uattr)
2010 struct bpf_prog *prog;
2011 int ret = -ENOTSUPP;
2013 if (!capable(CAP_SYS_ADMIN))
2015 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2018 prog = bpf_prog_get(attr->test.prog_fd);
2020 return PTR_ERR(prog);
2022 if (prog->aux->ops->test_run)
2023 ret = prog->aux->ops->test_run(prog, attr, uattr);
2029 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2031 static int bpf_obj_get_next_id(const union bpf_attr *attr,
2032 union bpf_attr __user *uattr,
2036 u32 next_id = attr->start_id;
2039 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2042 if (!capable(CAP_SYS_ADMIN))
2047 if (!idr_get_next(idr, &next_id))
2049 spin_unlock_bh(lock);
2052 err = put_user(next_id, &uattr->next_id);
2057 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2059 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2061 struct bpf_prog *prog;
2062 u32 id = attr->prog_id;
2065 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2068 if (!capable(CAP_SYS_ADMIN))
2071 spin_lock_bh(&prog_idr_lock);
2072 prog = idr_find(&prog_idr, id);
2074 prog = bpf_prog_inc_not_zero(prog);
2076 prog = ERR_PTR(-ENOENT);
2077 spin_unlock_bh(&prog_idr_lock);
2080 return PTR_ERR(prog);
2082 fd = bpf_prog_new_fd(prog);
2089 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2091 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2093 struct bpf_map *map;
2094 u32 id = attr->map_id;
2098 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2099 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
2102 if (!capable(CAP_SYS_ADMIN))
2105 f_flags = bpf_get_file_flag(attr->open_flags);
2109 spin_lock_bh(&map_idr_lock);
2110 map = idr_find(&map_idr, id);
2112 map = bpf_map_inc_not_zero(map, true);
2114 map = ERR_PTR(-ENOENT);
2115 spin_unlock_bh(&map_idr_lock);
2118 return PTR_ERR(map);
2120 fd = bpf_map_new_fd(map, f_flags);
2122 bpf_map_put_with_uref(map);
2127 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
2128 unsigned long addr, u32 *off,
2131 const struct bpf_map *map;
2134 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2135 map = prog->aux->used_maps[i];
2136 if (map == (void *)addr) {
2137 *type = BPF_PSEUDO_MAP_FD;
2140 if (!map->ops->map_direct_value_meta)
2142 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2143 *type = BPF_PSEUDO_MAP_VALUE;
2151 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2153 const struct bpf_map *map;
2154 struct bpf_insn *insns;
2159 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2164 for (i = 0; i < prog->len; i++) {
2165 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2166 insns[i].code = BPF_JMP | BPF_CALL;
2167 insns[i].imm = BPF_FUNC_tail_call;
2170 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2171 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2172 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2173 insns[i].code = BPF_JMP | BPF_CALL;
2174 if (!bpf_dump_raw_ok())
2179 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2182 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
2183 map = bpf_map_from_imm(prog, imm, &off, &type);
2185 insns[i].src_reg = type;
2186 insns[i].imm = map->id;
2187 insns[i + 1].imm = off;
2195 static int set_info_rec_size(struct bpf_prog_info *info)
2198 * Ensure info.*_rec_size is the same as kernel expected size
2202 * Only allow zero *_rec_size if both _rec_size and _cnt are
2203 * zero. In this case, the kernel will set the expected
2204 * _rec_size back to the info.
2207 if ((info->nr_func_info || info->func_info_rec_size) &&
2208 info->func_info_rec_size != sizeof(struct bpf_func_info))
2211 if ((info->nr_line_info || info->line_info_rec_size) &&
2212 info->line_info_rec_size != sizeof(struct bpf_line_info))
2215 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
2216 info->jited_line_info_rec_size != sizeof(__u64))
2219 info->func_info_rec_size = sizeof(struct bpf_func_info);
2220 info->line_info_rec_size = sizeof(struct bpf_line_info);
2221 info->jited_line_info_rec_size = sizeof(__u64);
2226 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2227 const union bpf_attr *attr,
2228 union bpf_attr __user *uattr)
2230 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2231 struct bpf_prog_info info = {};
2232 u32 info_len = attr->info.info_len;
2233 struct bpf_prog_stats stats;
2234 char __user *uinsns;
2238 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2241 info_len = min_t(u32, sizeof(info), info_len);
2243 if (copy_from_user(&info, uinfo, info_len))
2246 info.type = prog->type;
2247 info.id = prog->aux->id;
2248 info.load_time = prog->aux->load_time;
2249 info.created_by_uid = from_kuid_munged(current_user_ns(),
2250 prog->aux->user->uid);
2251 info.gpl_compatible = prog->gpl_compatible;
2253 memcpy(info.tag, prog->tag, sizeof(prog->tag));
2254 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2256 ulen = info.nr_map_ids;
2257 info.nr_map_ids = prog->aux->used_map_cnt;
2258 ulen = min_t(u32, info.nr_map_ids, ulen);
2260 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
2263 for (i = 0; i < ulen; i++)
2264 if (put_user(prog->aux->used_maps[i]->id,
2269 err = set_info_rec_size(&info);
2273 bpf_prog_get_stats(prog, &stats);
2274 info.run_time_ns = stats.nsecs;
2275 info.run_cnt = stats.cnt;
2277 if (!capable(CAP_SYS_ADMIN)) {
2278 info.jited_prog_len = 0;
2279 info.xlated_prog_len = 0;
2280 info.nr_jited_ksyms = 0;
2281 info.nr_jited_func_lens = 0;
2282 info.nr_func_info = 0;
2283 info.nr_line_info = 0;
2284 info.nr_jited_line_info = 0;
2288 ulen = info.xlated_prog_len;
2289 info.xlated_prog_len = bpf_prog_insn_size(prog);
2290 if (info.xlated_prog_len && ulen) {
2291 struct bpf_insn *insns_sanitized;
2294 if (prog->blinded && !bpf_dump_raw_ok()) {
2295 info.xlated_prog_insns = 0;
2298 insns_sanitized = bpf_insn_prepare_dump(prog);
2299 if (!insns_sanitized)
2301 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2302 ulen = min_t(u32, info.xlated_prog_len, ulen);
2303 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2304 kfree(insns_sanitized);
2309 if (bpf_prog_is_dev_bound(prog->aux)) {
2310 err = bpf_prog_offload_info_fill(&info, prog);
2316 /* NOTE: the following code is supposed to be skipped for offload.
2317 * bpf_prog_offload_info_fill() is the place to fill similar fields
2320 ulen = info.jited_prog_len;
2321 if (prog->aux->func_cnt) {
2324 info.jited_prog_len = 0;
2325 for (i = 0; i < prog->aux->func_cnt; i++)
2326 info.jited_prog_len += prog->aux->func[i]->jited_len;
2328 info.jited_prog_len = prog->jited_len;
2331 if (info.jited_prog_len && ulen) {
2332 if (bpf_dump_raw_ok()) {
2333 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2334 ulen = min_t(u32, info.jited_prog_len, ulen);
2336 /* for multi-function programs, copy the JITed
2337 * instructions for all the functions
2339 if (prog->aux->func_cnt) {
2344 for (i = 0; i < prog->aux->func_cnt; i++) {
2345 len = prog->aux->func[i]->jited_len;
2346 len = min_t(u32, len, free);
2347 img = (u8 *) prog->aux->func[i]->bpf_func;
2348 if (copy_to_user(uinsns, img, len))
2356 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2360 info.jited_prog_insns = 0;
2364 ulen = info.nr_jited_ksyms;
2365 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
2367 if (bpf_dump_raw_ok()) {
2368 unsigned long ksym_addr;
2369 u64 __user *user_ksyms;
2372 /* copy the address of the kernel symbol
2373 * corresponding to each function
2375 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2376 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2377 if (prog->aux->func_cnt) {
2378 for (i = 0; i < ulen; i++) {
2379 ksym_addr = (unsigned long)
2380 prog->aux->func[i]->bpf_func;
2381 if (put_user((u64) ksym_addr,
2386 ksym_addr = (unsigned long) prog->bpf_func;
2387 if (put_user((u64) ksym_addr, &user_ksyms[0]))
2391 info.jited_ksyms = 0;
2395 ulen = info.nr_jited_func_lens;
2396 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
2398 if (bpf_dump_raw_ok()) {
2399 u32 __user *user_lens;
2402 /* copy the JITed image lengths for each function */
2403 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2404 user_lens = u64_to_user_ptr(info.jited_func_lens);
2405 if (prog->aux->func_cnt) {
2406 for (i = 0; i < ulen; i++) {
2408 prog->aux->func[i]->jited_len;
2409 if (put_user(func_len, &user_lens[i]))
2413 func_len = prog->jited_len;
2414 if (put_user(func_len, &user_lens[0]))
2418 info.jited_func_lens = 0;
2423 info.btf_id = btf_id(prog->aux->btf);
2425 ulen = info.nr_func_info;
2426 info.nr_func_info = prog->aux->func_info_cnt;
2427 if (info.nr_func_info && ulen) {
2428 char __user *user_finfo;
2430 user_finfo = u64_to_user_ptr(info.func_info);
2431 ulen = min_t(u32, info.nr_func_info, ulen);
2432 if (copy_to_user(user_finfo, prog->aux->func_info,
2433 info.func_info_rec_size * ulen))
2437 ulen = info.nr_line_info;
2438 info.nr_line_info = prog->aux->nr_linfo;
2439 if (info.nr_line_info && ulen) {
2440 __u8 __user *user_linfo;
2442 user_linfo = u64_to_user_ptr(info.line_info);
2443 ulen = min_t(u32, info.nr_line_info, ulen);
2444 if (copy_to_user(user_linfo, prog->aux->linfo,
2445 info.line_info_rec_size * ulen))
2449 ulen = info.nr_jited_line_info;
2450 if (prog->aux->jited_linfo)
2451 info.nr_jited_line_info = prog->aux->nr_linfo;
2453 info.nr_jited_line_info = 0;
2454 if (info.nr_jited_line_info && ulen) {
2455 if (bpf_dump_raw_ok()) {
2456 __u64 __user *user_linfo;
2459 user_linfo = u64_to_user_ptr(info.jited_line_info);
2460 ulen = min_t(u32, info.nr_jited_line_info, ulen);
2461 for (i = 0; i < ulen; i++) {
2462 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2467 info.jited_line_info = 0;
2471 ulen = info.nr_prog_tags;
2472 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2474 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2477 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2478 ulen = min_t(u32, info.nr_prog_tags, ulen);
2479 if (prog->aux->func_cnt) {
2480 for (i = 0; i < ulen; i++) {
2481 if (copy_to_user(user_prog_tags[i],
2482 prog->aux->func[i]->tag,
2487 if (copy_to_user(user_prog_tags[0],
2488 prog->tag, BPF_TAG_SIZE))
2494 if (copy_to_user(uinfo, &info, info_len) ||
2495 put_user(info_len, &uattr->info.info_len))
2501 static int bpf_map_get_info_by_fd(struct bpf_map *map,
2502 const union bpf_attr *attr,
2503 union bpf_attr __user *uattr)
2505 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2506 struct bpf_map_info info = {};
2507 u32 info_len = attr->info.info_len;
2510 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2513 info_len = min_t(u32, sizeof(info), info_len);
2515 info.type = map->map_type;
2517 info.key_size = map->key_size;
2518 info.value_size = map->value_size;
2519 info.max_entries = map->max_entries;
2520 info.map_flags = map->map_flags;
2521 memcpy(info.name, map->name, sizeof(map->name));
2524 info.btf_id = btf_id(map->btf);
2525 info.btf_key_type_id = map->btf_key_type_id;
2526 info.btf_value_type_id = map->btf_value_type_id;
2529 if (bpf_map_is_dev_bound(map)) {
2530 err = bpf_map_offload_info_fill(&info, map);
2535 if (copy_to_user(uinfo, &info, info_len) ||
2536 put_user(info_len, &uattr->info.info_len))
2542 static int bpf_btf_get_info_by_fd(struct btf *btf,
2543 const union bpf_attr *attr,
2544 union bpf_attr __user *uattr)
2546 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2547 u32 info_len = attr->info.info_len;
2550 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
2554 return btf_get_info_by_fd(btf, attr, uattr);
2557 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2559 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2560 union bpf_attr __user *uattr)
2562 int ufd = attr->info.bpf_fd;
2566 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2573 if (f.file->f_op == &bpf_prog_fops)
2574 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2576 else if (f.file->f_op == &bpf_map_fops)
2577 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2579 else if (f.file->f_op == &btf_fops)
2580 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
2588 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2590 static int bpf_btf_load(const union bpf_attr *attr)
2592 if (CHECK_ATTR(BPF_BTF_LOAD))
2595 if (!capable(CAP_SYS_ADMIN))
2598 return btf_new_fd(attr);
2601 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2603 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2605 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2608 if (!capable(CAP_SYS_ADMIN))
2611 return btf_get_fd_by_id(attr->btf_id);
2614 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2615 union bpf_attr __user *uattr,
2616 u32 prog_id, u32 fd_type,
2617 const char *buf, u64 probe_offset,
2620 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2621 u32 len = buf ? strlen(buf) : 0, input_len;
2624 if (put_user(len, &uattr->task_fd_query.buf_len))
2626 input_len = attr->task_fd_query.buf_len;
2627 if (input_len && ubuf) {
2629 /* nothing to copy, just make ubuf NULL terminated */
2632 if (put_user(zero, ubuf))
2634 } else if (input_len >= len + 1) {
2635 /* ubuf can hold the string with NULL terminator */
2636 if (copy_to_user(ubuf, buf, len + 1))
2639 /* ubuf cannot hold the string with NULL terminator,
2640 * do a partial copy with NULL terminator.
2645 if (copy_to_user(ubuf, buf, input_len - 1))
2647 if (put_user(zero, ubuf + input_len - 1))
2652 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2653 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2654 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2655 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2661 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2663 static int bpf_task_fd_query(const union bpf_attr *attr,
2664 union bpf_attr __user *uattr)
2666 pid_t pid = attr->task_fd_query.pid;
2667 u32 fd = attr->task_fd_query.fd;
2668 const struct perf_event *event;
2669 struct files_struct *files;
2670 struct task_struct *task;
2674 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2677 if (!capable(CAP_SYS_ADMIN))
2680 if (attr->task_fd_query.flags != 0)
2683 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2687 files = get_files_struct(task);
2688 put_task_struct(task);
2693 spin_lock(&files->file_lock);
2694 file = fcheck_files(files, fd);
2699 spin_unlock(&files->file_lock);
2700 put_files_struct(files);
2705 if (file->f_op == &bpf_raw_tp_fops) {
2706 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2707 struct bpf_raw_event_map *btp = raw_tp->btp;
2709 err = bpf_task_fd_query_copy(attr, uattr,
2710 raw_tp->prog->aux->id,
2711 BPF_FD_TYPE_RAW_TRACEPOINT,
2712 btp->tp->name, 0, 0);
2716 event = perf_get_event(file);
2717 if (!IS_ERR(event)) {
2718 u64 probe_offset, probe_addr;
2719 u32 prog_id, fd_type;
2722 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2723 &buf, &probe_offset,
2726 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2740 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2742 union bpf_attr attr = {};
2745 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
2748 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
2751 size = min_t(u32, size, sizeof(attr));
2753 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2754 if (copy_from_user(&attr, uattr, size) != 0)
2757 err = security_bpf(cmd, &attr, size);
2762 case BPF_MAP_CREATE:
2763 err = map_create(&attr);
2765 case BPF_MAP_LOOKUP_ELEM:
2766 err = map_lookup_elem(&attr);
2768 case BPF_MAP_UPDATE_ELEM:
2769 err = map_update_elem(&attr);
2771 case BPF_MAP_DELETE_ELEM:
2772 err = map_delete_elem(&attr);
2774 case BPF_MAP_GET_NEXT_KEY:
2775 err = map_get_next_key(&attr);
2777 case BPF_MAP_FREEZE:
2778 err = map_freeze(&attr);
2781 err = bpf_prog_load(&attr, uattr);
2784 err = bpf_obj_pin(&attr);
2787 err = bpf_obj_get(&attr);
2789 case BPF_PROG_ATTACH:
2790 err = bpf_prog_attach(&attr);
2792 case BPF_PROG_DETACH:
2793 err = bpf_prog_detach(&attr);
2795 case BPF_PROG_QUERY:
2796 err = bpf_prog_query(&attr, uattr);
2798 case BPF_PROG_TEST_RUN:
2799 err = bpf_prog_test_run(&attr, uattr);
2801 case BPF_PROG_GET_NEXT_ID:
2802 err = bpf_obj_get_next_id(&attr, uattr,
2803 &prog_idr, &prog_idr_lock);
2805 case BPF_MAP_GET_NEXT_ID:
2806 err = bpf_obj_get_next_id(&attr, uattr,
2807 &map_idr, &map_idr_lock);
2809 case BPF_PROG_GET_FD_BY_ID:
2810 err = bpf_prog_get_fd_by_id(&attr);
2812 case BPF_MAP_GET_FD_BY_ID:
2813 err = bpf_map_get_fd_by_id(&attr);
2815 case BPF_OBJ_GET_INFO_BY_FD:
2816 err = bpf_obj_get_info_by_fd(&attr, uattr);
2818 case BPF_RAW_TRACEPOINT_OPEN:
2819 err = bpf_raw_tracepoint_open(&attr);
2822 err = bpf_btf_load(&attr);
2824 case BPF_BTF_GET_FD_BY_ID:
2825 err = bpf_btf_get_fd_by_id(&attr);
2827 case BPF_TASK_FD_QUERY:
2828 err = bpf_task_fd_query(&attr, uattr);
2830 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2831 err = map_lookup_and_delete_elem(&attr);