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 /* Some maps allow key to be unspecified. */
509 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
510 if (!key_type || key_size != map->key_size)
513 key_type = btf_type_by_id(btf, 0);
514 if (!map->ops->map_check_btf)
518 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
519 if (!value_type || value_size != map->value_size)
522 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
524 if (map_value_has_spin_lock(map)) {
525 if (map->map_flags & BPF_F_RDONLY_PROG)
527 if (map->map_type != BPF_MAP_TYPE_HASH &&
528 map->map_type != BPF_MAP_TYPE_ARRAY &&
529 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
530 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
532 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
535 "verifier bug spin_lock_off %d value_size %d\n",
536 map->spin_lock_off, map->value_size);
541 if (map->ops->map_check_btf)
542 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
547 #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
548 /* called via syscall */
549 static int map_create(union bpf_attr *attr)
551 int numa_node = bpf_map_attr_numa_node(attr);
556 err = CHECK_ATTR(BPF_MAP_CREATE);
560 f_flags = bpf_get_file_flag(attr->map_flags);
564 if (numa_node != NUMA_NO_NODE &&
565 ((unsigned int)numa_node >= nr_node_ids ||
566 !node_online(numa_node)))
569 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
570 map = find_and_alloc_map(attr);
574 err = bpf_obj_name_cpy(map->name, attr->map_name);
576 goto free_map_nouncharge;
578 atomic_set(&map->refcnt, 1);
579 atomic_set(&map->usercnt, 1);
581 if (attr->btf_key_type_id || attr->btf_value_type_id) {
584 if (!attr->btf_value_type_id) {
586 goto free_map_nouncharge;
589 btf = btf_get_by_fd(attr->btf_fd);
592 goto free_map_nouncharge;
595 err = map_check_btf(map, btf, attr->btf_key_type_id,
596 attr->btf_value_type_id);
599 goto free_map_nouncharge;
603 map->btf_key_type_id = attr->btf_key_type_id;
604 map->btf_value_type_id = attr->btf_value_type_id;
606 map->spin_lock_off = -EINVAL;
609 err = security_bpf_map_alloc(map);
611 goto free_map_nouncharge;
613 err = bpf_map_init_memlock(map);
617 err = bpf_map_alloc_id(map);
621 err = bpf_map_new_fd(map, f_flags);
623 /* failed to allocate fd.
624 * bpf_map_put_with_uref() is needed because the above
625 * bpf_map_alloc_id() has published the map
626 * to the userspace and the userspace may
627 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
629 bpf_map_put_with_uref(map);
636 bpf_map_release_memlock(map);
638 security_bpf_map_free(map);
641 map->ops->map_free(map);
645 /* if error is returned, fd is released.
646 * On success caller should complete fd access with matching fdput()
648 struct bpf_map *__bpf_map_get(struct fd f)
651 return ERR_PTR(-EBADF);
652 if (f.file->f_op != &bpf_map_fops) {
654 return ERR_PTR(-EINVAL);
657 return f.file->private_data;
660 /* prog's and map's refcnt limit */
661 #define BPF_MAX_REFCNT 32768
663 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
665 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
666 atomic_dec(&map->refcnt);
667 return ERR_PTR(-EBUSY);
670 atomic_inc(&map->usercnt);
673 EXPORT_SYMBOL_GPL(bpf_map_inc);
675 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
677 struct fd f = fdget(ufd);
680 map = __bpf_map_get(f);
684 map = bpf_map_inc(map, true);
690 /* map_idr_lock should have been held */
691 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
696 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
698 if (refold >= BPF_MAX_REFCNT) {
699 __bpf_map_put(map, false);
700 return ERR_PTR(-EBUSY);
704 return ERR_PTR(-ENOENT);
707 atomic_inc(&map->usercnt);
712 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
717 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
720 return memdup_user(ukey, key_size);
723 return ERR_PTR(-EINVAL);
728 /* last field in 'union bpf_attr' used by this command */
729 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
731 static int map_lookup_elem(union bpf_attr *attr)
733 void __user *ukey = u64_to_user_ptr(attr->key);
734 void __user *uvalue = u64_to_user_ptr(attr->value);
735 int ufd = attr->map_fd;
737 void *key, *value, *ptr;
742 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
745 if (attr->flags & ~BPF_F_LOCK)
749 map = __bpf_map_get(f);
752 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
757 if ((attr->flags & BPF_F_LOCK) &&
758 !map_value_has_spin_lock(map)) {
763 key = __bpf_copy_key(ukey, map->key_size);
769 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
770 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
771 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
772 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
773 value_size = round_up(map->value_size, 8) * num_possible_cpus();
774 else if (IS_FD_MAP(map))
775 value_size = sizeof(u32);
777 value_size = map->value_size;
780 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
784 if (bpf_map_is_dev_bound(map)) {
785 err = bpf_map_offload_lookup_elem(map, key, value);
790 this_cpu_inc(bpf_prog_active);
791 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
792 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
793 err = bpf_percpu_hash_copy(map, key, value);
794 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
795 err = bpf_percpu_array_copy(map, key, value);
796 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
797 err = bpf_percpu_cgroup_storage_copy(map, key, value);
798 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
799 err = bpf_stackmap_copy(map, key, value);
800 } else if (IS_FD_ARRAY(map)) {
801 err = bpf_fd_array_map_lookup_elem(map, key, value);
802 } else if (IS_FD_HASH(map)) {
803 err = bpf_fd_htab_map_lookup_elem(map, key, value);
804 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
805 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
806 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
807 map->map_type == BPF_MAP_TYPE_STACK) {
808 err = map->ops->map_peek_elem(map, value);
811 ptr = map->ops->map_lookup_elem(map, key);
818 if (attr->flags & BPF_F_LOCK)
819 /* lock 'ptr' and copy everything but lock */
820 copy_map_value_locked(map, value, ptr, true);
822 copy_map_value(map, value, ptr);
823 /* mask lock, since value wasn't zero inited */
824 check_and_init_map_lock(map, value);
828 this_cpu_dec(bpf_prog_active);
836 if (copy_to_user(uvalue, value, value_size) != 0)
850 static void maybe_wait_bpf_programs(struct bpf_map *map)
852 /* Wait for any running BPF programs to complete so that
853 * userspace, when we return to it, knows that all programs
854 * that could be running use the new map value.
856 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
857 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
861 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
863 static int map_update_elem(union bpf_attr *attr)
865 void __user *ukey = u64_to_user_ptr(attr->key);
866 void __user *uvalue = u64_to_user_ptr(attr->value);
867 int ufd = attr->map_fd;
874 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
878 map = __bpf_map_get(f);
881 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
886 if ((attr->flags & BPF_F_LOCK) &&
887 !map_value_has_spin_lock(map)) {
892 key = __bpf_copy_key(ukey, map->key_size);
898 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
899 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
900 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
901 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
902 value_size = round_up(map->value_size, 8) * num_possible_cpus();
904 value_size = map->value_size;
907 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
912 if (copy_from_user(value, uvalue, value_size) != 0)
915 /* Need to create a kthread, thus must support schedule */
916 if (bpf_map_is_dev_bound(map)) {
917 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
919 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
920 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
921 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
922 err = map->ops->map_update_elem(map, key, value, attr->flags);
926 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
927 * inside bpf map update or delete otherwise deadlocks are possible
930 __this_cpu_inc(bpf_prog_active);
931 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
932 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
933 err = bpf_percpu_hash_update(map, key, value, attr->flags);
934 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
935 err = bpf_percpu_array_update(map, key, value, attr->flags);
936 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
937 err = bpf_percpu_cgroup_storage_update(map, key, value,
939 } else if (IS_FD_ARRAY(map)) {
941 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
944 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
946 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
949 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
950 /* rcu_read_lock() is not needed */
951 err = bpf_fd_reuseport_array_update_elem(map, key, value,
953 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
954 map->map_type == BPF_MAP_TYPE_STACK) {
955 err = map->ops->map_push_elem(map, value, attr->flags);
958 err = map->ops->map_update_elem(map, key, value, attr->flags);
961 __this_cpu_dec(bpf_prog_active);
963 maybe_wait_bpf_programs(map);
974 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
976 static int map_delete_elem(union bpf_attr *attr)
978 void __user *ukey = u64_to_user_ptr(attr->key);
979 int ufd = attr->map_fd;
985 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
989 map = __bpf_map_get(f);
992 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
997 key = __bpf_copy_key(ukey, map->key_size);
1003 if (bpf_map_is_dev_bound(map)) {
1004 err = bpf_map_offload_delete_elem(map, key);
1009 __this_cpu_inc(bpf_prog_active);
1011 err = map->ops->map_delete_elem(map, key);
1013 __this_cpu_dec(bpf_prog_active);
1015 maybe_wait_bpf_programs(map);
1023 /* last field in 'union bpf_attr' used by this command */
1024 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1026 static int map_get_next_key(union bpf_attr *attr)
1028 void __user *ukey = u64_to_user_ptr(attr->key);
1029 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1030 int ufd = attr->map_fd;
1031 struct bpf_map *map;
1032 void *key, *next_key;
1036 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1040 map = __bpf_map_get(f);
1042 return PTR_ERR(map);
1043 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1049 key = __bpf_copy_key(ukey, map->key_size);
1059 next_key = kmalloc(map->key_size, GFP_USER);
1063 if (bpf_map_is_dev_bound(map)) {
1064 err = bpf_map_offload_get_next_key(map, key, next_key);
1069 err = map->ops->map_get_next_key(map, key, next_key);
1076 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1090 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1092 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1094 void __user *ukey = u64_to_user_ptr(attr->key);
1095 void __user *uvalue = u64_to_user_ptr(attr->value);
1096 int ufd = attr->map_fd;
1097 struct bpf_map *map;
1103 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1107 map = __bpf_map_get(f);
1109 return PTR_ERR(map);
1110 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1115 key = __bpf_copy_key(ukey, map->key_size);
1121 value_size = map->value_size;
1124 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1128 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1129 map->map_type == BPF_MAP_TYPE_STACK) {
1130 err = map->ops->map_pop_elem(map, value);
1138 if (copy_to_user(uvalue, value, value_size) != 0)
1152 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1154 static int map_freeze(const union bpf_attr *attr)
1156 int err = 0, ufd = attr->map_fd;
1157 struct bpf_map *map;
1160 if (CHECK_ATTR(BPF_MAP_FREEZE))
1164 map = __bpf_map_get(f);
1166 return PTR_ERR(map);
1167 if (READ_ONCE(map->frozen)) {
1171 if (!capable(CAP_SYS_ADMIN)) {
1176 WRITE_ONCE(map->frozen, true);
1182 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1183 #define BPF_PROG_TYPE(_id, _name) \
1184 [_id] = & _name ## _prog_ops,
1185 #define BPF_MAP_TYPE(_id, _ops)
1186 #include <linux/bpf_types.h>
1187 #undef BPF_PROG_TYPE
1191 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1193 const struct bpf_prog_ops *ops;
1195 if (type >= ARRAY_SIZE(bpf_prog_types))
1197 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1198 ops = bpf_prog_types[type];
1202 if (!bpf_prog_is_dev_bound(prog->aux))
1203 prog->aux->ops = ops;
1205 prog->aux->ops = &bpf_offload_prog_ops;
1210 /* drop refcnt on maps used by eBPF program and free auxilary data */
1211 static void free_used_maps(struct bpf_prog_aux *aux)
1213 enum bpf_cgroup_storage_type stype;
1216 for_each_cgroup_storage_type(stype) {
1217 if (!aux->cgroup_storage[stype])
1219 bpf_cgroup_storage_release(aux->prog,
1220 aux->cgroup_storage[stype]);
1223 for (i = 0; i < aux->used_map_cnt; i++)
1224 bpf_map_put(aux->used_maps[i]);
1226 kfree(aux->used_maps);
1229 int __bpf_prog_charge(struct user_struct *user, u32 pages)
1231 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1232 unsigned long user_bufs;
1235 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1236 if (user_bufs > memlock_limit) {
1237 atomic_long_sub(pages, &user->locked_vm);
1245 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1248 atomic_long_sub(pages, &user->locked_vm);
1251 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1253 struct user_struct *user = get_current_user();
1256 ret = __bpf_prog_charge(user, prog->pages);
1262 prog->aux->user = user;
1266 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1268 struct user_struct *user = prog->aux->user;
1270 __bpf_prog_uncharge(user, prog->pages);
1274 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1278 idr_preload(GFP_KERNEL);
1279 spin_lock_bh(&prog_idr_lock);
1280 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1283 spin_unlock_bh(&prog_idr_lock);
1286 /* id is in [1, INT_MAX) */
1287 if (WARN_ON_ONCE(!id))
1290 return id > 0 ? 0 : id;
1293 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1295 /* cBPF to eBPF migrations are currently not in the idr store.
1296 * Offloaded programs are removed from the store when their device
1297 * disappears - even if someone grabs an fd to them they are unusable,
1298 * simply waiting for refcnt to drop to be freed.
1304 spin_lock_bh(&prog_idr_lock);
1306 __acquire(&prog_idr_lock);
1308 idr_remove(&prog_idr, prog->aux->id);
1312 spin_unlock_bh(&prog_idr_lock);
1314 __release(&prog_idr_lock);
1317 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1319 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1321 free_used_maps(aux);
1322 bpf_prog_uncharge_memlock(aux->prog);
1323 security_bpf_prog_free(aux);
1324 bpf_prog_free(aux->prog);
1327 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1329 if (atomic_dec_and_test(&prog->aux->refcnt)) {
1330 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1331 /* bpf_prog_free_id() must be called first */
1332 bpf_prog_free_id(prog, do_idr_lock);
1333 bpf_prog_kallsyms_del_all(prog);
1334 btf_put(prog->aux->btf);
1335 kvfree(prog->aux->func_info);
1336 bpf_prog_free_linfo(prog);
1338 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1342 void bpf_prog_put(struct bpf_prog *prog)
1344 __bpf_prog_put(prog, true);
1346 EXPORT_SYMBOL_GPL(bpf_prog_put);
1348 static int bpf_prog_release(struct inode *inode, struct file *filp)
1350 struct bpf_prog *prog = filp->private_data;
1356 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1357 struct bpf_prog_stats *stats)
1359 u64 nsecs = 0, cnt = 0;
1362 for_each_possible_cpu(cpu) {
1363 const struct bpf_prog_stats *st;
1367 st = per_cpu_ptr(prog->aux->stats, cpu);
1369 start = u64_stats_fetch_begin_irq(&st->syncp);
1372 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1376 stats->nsecs = nsecs;
1380 #ifdef CONFIG_PROC_FS
1381 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1383 const struct bpf_prog *prog = filp->private_data;
1384 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1385 struct bpf_prog_stats stats;
1387 bpf_prog_get_stats(prog, &stats);
1388 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1395 "run_time_ns:\t%llu\n"
1400 prog->pages * 1ULL << PAGE_SHIFT,
1407 const struct file_operations bpf_prog_fops = {
1408 #ifdef CONFIG_PROC_FS
1409 .show_fdinfo = bpf_prog_show_fdinfo,
1411 .release = bpf_prog_release,
1412 .read = bpf_dummy_read,
1413 .write = bpf_dummy_write,
1416 int bpf_prog_new_fd(struct bpf_prog *prog)
1420 ret = security_bpf_prog(prog);
1424 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1425 O_RDWR | O_CLOEXEC);
1428 static struct bpf_prog *____bpf_prog_get(struct fd f)
1431 return ERR_PTR(-EBADF);
1432 if (f.file->f_op != &bpf_prog_fops) {
1434 return ERR_PTR(-EINVAL);
1437 return f.file->private_data;
1440 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
1442 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1443 atomic_sub(i, &prog->aux->refcnt);
1444 return ERR_PTR(-EBUSY);
1448 EXPORT_SYMBOL_GPL(bpf_prog_add);
1450 void bpf_prog_sub(struct bpf_prog *prog, int i)
1452 /* Only to be used for undoing previous bpf_prog_add() in some
1453 * error path. We still know that another entity in our call
1454 * path holds a reference to the program, thus atomic_sub() can
1455 * be safely used in such cases!
1457 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1459 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1461 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1463 return bpf_prog_add(prog, 1);
1465 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1467 /* prog_idr_lock should have been held */
1468 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1472 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1474 if (refold >= BPF_MAX_REFCNT) {
1475 __bpf_prog_put(prog, false);
1476 return ERR_PTR(-EBUSY);
1480 return ERR_PTR(-ENOENT);
1484 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1486 bool bpf_prog_get_ok(struct bpf_prog *prog,
1487 enum bpf_prog_type *attach_type, bool attach_drv)
1489 /* not an attachment, just a refcount inc, always allow */
1493 if (prog->type != *attach_type)
1495 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1501 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1504 struct fd f = fdget(ufd);
1505 struct bpf_prog *prog;
1507 prog = ____bpf_prog_get(f);
1510 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1511 prog = ERR_PTR(-EINVAL);
1515 prog = bpf_prog_inc(prog);
1521 struct bpf_prog *bpf_prog_get(u32 ufd)
1523 return __bpf_prog_get(ufd, NULL, false);
1526 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1529 return __bpf_prog_get(ufd, &type, attach_drv);
1531 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1533 /* Initially all BPF programs could be loaded w/o specifying
1534 * expected_attach_type. Later for some of them specifying expected_attach_type
1535 * at load time became required so that program could be validated properly.
1536 * Programs of types that are allowed to be loaded both w/ and w/o (for
1537 * backward compatibility) expected_attach_type, should have the default attach
1538 * type assigned to expected_attach_type for the latter case, so that it can be
1539 * validated later at attach time.
1541 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1542 * prog type requires it but has some attach types that have to be backward
1545 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1547 switch (attr->prog_type) {
1548 case BPF_PROG_TYPE_CGROUP_SOCK:
1549 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1550 * exist so checking for non-zero is the way to go here.
1552 if (!attr->expected_attach_type)
1553 attr->expected_attach_type =
1554 BPF_CGROUP_INET_SOCK_CREATE;
1560 bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1561 enum bpf_attach_type expected_attach_type)
1563 switch (prog_type) {
1564 case BPF_PROG_TYPE_CGROUP_SOCK:
1565 switch (expected_attach_type) {
1566 case BPF_CGROUP_INET_SOCK_CREATE:
1567 case BPF_CGROUP_INET4_POST_BIND:
1568 case BPF_CGROUP_INET6_POST_BIND:
1573 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1574 switch (expected_attach_type) {
1575 case BPF_CGROUP_INET4_BIND:
1576 case BPF_CGROUP_INET6_BIND:
1577 case BPF_CGROUP_INET4_CONNECT:
1578 case BPF_CGROUP_INET6_CONNECT:
1579 case BPF_CGROUP_UDP4_SENDMSG:
1580 case BPF_CGROUP_UDP6_SENDMSG:
1590 /* last field in 'union bpf_attr' used by this command */
1591 #define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
1593 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
1595 enum bpf_prog_type type = attr->prog_type;
1596 struct bpf_prog *prog;
1601 if (CHECK_ATTR(BPF_PROG_LOAD))
1604 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT | BPF_F_ANY_ALIGNMENT))
1607 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1608 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1609 !capable(CAP_SYS_ADMIN))
1612 /* copy eBPF program license from user space */
1613 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1614 sizeof(license) - 1) < 0)
1616 license[sizeof(license) - 1] = 0;
1618 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1619 is_gpl = license_is_gpl_compatible(license);
1621 if (attr->insn_cnt == 0 ||
1622 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
1624 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1625 type != BPF_PROG_TYPE_CGROUP_SKB &&
1626 !capable(CAP_SYS_ADMIN))
1629 bpf_prog_load_fixup_attach_type(attr);
1630 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1633 /* plain bpf_prog allocation */
1634 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1638 prog->expected_attach_type = attr->expected_attach_type;
1640 prog->aux->offload_requested = !!attr->prog_ifindex;
1642 err = security_bpf_prog_alloc(prog->aux);
1644 goto free_prog_nouncharge;
1646 err = bpf_prog_charge_memlock(prog);
1650 prog->len = attr->insn_cnt;
1653 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1654 bpf_prog_insn_size(prog)) != 0)
1657 prog->orig_prog = NULL;
1660 atomic_set(&prog->aux->refcnt, 1);
1661 prog->gpl_compatible = is_gpl ? 1 : 0;
1663 if (bpf_prog_is_dev_bound(prog->aux)) {
1664 err = bpf_prog_offload_init(prog, attr);
1669 /* find program type: socket_filter vs tracing_filter */
1670 err = find_prog_type(type, prog);
1674 prog->aux->load_time = ktime_get_boot_ns();
1675 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1679 /* run eBPF verifier */
1680 err = bpf_check(&prog, attr, uattr);
1682 goto free_used_maps;
1684 prog = bpf_prog_select_runtime(prog, &err);
1686 goto free_used_maps;
1688 err = bpf_prog_alloc_id(prog);
1690 goto free_used_maps;
1692 err = bpf_prog_new_fd(prog);
1694 /* failed to allocate fd.
1695 * bpf_prog_put() is needed because the above
1696 * bpf_prog_alloc_id() has published the prog
1697 * to the userspace and the userspace may
1698 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1704 bpf_prog_kallsyms_add(prog);
1705 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
1709 bpf_prog_free_linfo(prog);
1710 kvfree(prog->aux->func_info);
1711 btf_put(prog->aux->btf);
1712 bpf_prog_kallsyms_del_subprogs(prog);
1713 free_used_maps(prog->aux);
1715 bpf_prog_uncharge_memlock(prog);
1717 security_bpf_prog_free(prog->aux);
1718 free_prog_nouncharge:
1719 bpf_prog_free(prog);
1723 #define BPF_OBJ_LAST_FIELD file_flags
1725 static int bpf_obj_pin(const union bpf_attr *attr)
1727 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1730 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1733 static int bpf_obj_get(const union bpf_attr *attr)
1735 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1736 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1739 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1743 struct bpf_raw_tracepoint {
1744 struct bpf_raw_event_map *btp;
1745 struct bpf_prog *prog;
1748 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1750 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1753 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1754 bpf_prog_put(raw_tp->prog);
1756 bpf_put_raw_tracepoint(raw_tp->btp);
1761 static const struct file_operations bpf_raw_tp_fops = {
1762 .release = bpf_raw_tracepoint_release,
1763 .read = bpf_dummy_read,
1764 .write = bpf_dummy_write,
1767 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1769 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1771 struct bpf_raw_tracepoint *raw_tp;
1772 struct bpf_raw_event_map *btp;
1773 struct bpf_prog *prog;
1777 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1778 sizeof(tp_name) - 1) < 0)
1780 tp_name[sizeof(tp_name) - 1] = 0;
1782 btp = bpf_get_raw_tracepoint(tp_name);
1786 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1793 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
1795 err = PTR_ERR(prog);
1798 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1799 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
1804 err = bpf_probe_register(raw_tp->btp, prog);
1808 raw_tp->prog = prog;
1809 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1812 bpf_probe_unregister(raw_tp->btp, prog);
1823 bpf_put_raw_tracepoint(btp);
1827 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1828 enum bpf_attach_type attach_type)
1830 switch (prog->type) {
1831 case BPF_PROG_TYPE_CGROUP_SOCK:
1832 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1833 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1839 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1841 #define BPF_F_ATTACH_MASK \
1842 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1844 static int bpf_prog_attach(const union bpf_attr *attr)
1846 enum bpf_prog_type ptype;
1847 struct bpf_prog *prog;
1850 if (!capable(CAP_NET_ADMIN))
1853 if (CHECK_ATTR(BPF_PROG_ATTACH))
1856 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
1859 switch (attr->attach_type) {
1860 case BPF_CGROUP_INET_INGRESS:
1861 case BPF_CGROUP_INET_EGRESS:
1862 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1864 case BPF_CGROUP_INET_SOCK_CREATE:
1865 case BPF_CGROUP_INET4_POST_BIND:
1866 case BPF_CGROUP_INET6_POST_BIND:
1867 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1869 case BPF_CGROUP_INET4_BIND:
1870 case BPF_CGROUP_INET6_BIND:
1871 case BPF_CGROUP_INET4_CONNECT:
1872 case BPF_CGROUP_INET6_CONNECT:
1873 case BPF_CGROUP_UDP4_SENDMSG:
1874 case BPF_CGROUP_UDP6_SENDMSG:
1875 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1877 case BPF_CGROUP_SOCK_OPS:
1878 ptype = BPF_PROG_TYPE_SOCK_OPS;
1880 case BPF_CGROUP_DEVICE:
1881 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1883 case BPF_SK_MSG_VERDICT:
1884 ptype = BPF_PROG_TYPE_SK_MSG;
1886 case BPF_SK_SKB_STREAM_PARSER:
1887 case BPF_SK_SKB_STREAM_VERDICT:
1888 ptype = BPF_PROG_TYPE_SK_SKB;
1890 case BPF_LIRC_MODE2:
1891 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1893 case BPF_FLOW_DISSECTOR:
1894 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1896 case BPF_CGROUP_SYSCTL:
1897 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1903 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1905 return PTR_ERR(prog);
1907 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1913 case BPF_PROG_TYPE_SK_SKB:
1914 case BPF_PROG_TYPE_SK_MSG:
1915 ret = sock_map_get_from_fd(attr, prog);
1917 case BPF_PROG_TYPE_LIRC_MODE2:
1918 ret = lirc_prog_attach(attr, prog);
1920 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1921 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1924 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
1932 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1934 static int bpf_prog_detach(const union bpf_attr *attr)
1936 enum bpf_prog_type ptype;
1938 if (!capable(CAP_NET_ADMIN))
1941 if (CHECK_ATTR(BPF_PROG_DETACH))
1944 switch (attr->attach_type) {
1945 case BPF_CGROUP_INET_INGRESS:
1946 case BPF_CGROUP_INET_EGRESS:
1947 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1949 case BPF_CGROUP_INET_SOCK_CREATE:
1950 case BPF_CGROUP_INET4_POST_BIND:
1951 case BPF_CGROUP_INET6_POST_BIND:
1952 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1954 case BPF_CGROUP_INET4_BIND:
1955 case BPF_CGROUP_INET6_BIND:
1956 case BPF_CGROUP_INET4_CONNECT:
1957 case BPF_CGROUP_INET6_CONNECT:
1958 case BPF_CGROUP_UDP4_SENDMSG:
1959 case BPF_CGROUP_UDP6_SENDMSG:
1960 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1962 case BPF_CGROUP_SOCK_OPS:
1963 ptype = BPF_PROG_TYPE_SOCK_OPS;
1965 case BPF_CGROUP_DEVICE:
1966 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1968 case BPF_SK_MSG_VERDICT:
1969 return sock_map_get_from_fd(attr, NULL);
1970 case BPF_SK_SKB_STREAM_PARSER:
1971 case BPF_SK_SKB_STREAM_VERDICT:
1972 return sock_map_get_from_fd(attr, NULL);
1973 case BPF_LIRC_MODE2:
1974 return lirc_prog_detach(attr);
1975 case BPF_FLOW_DISSECTOR:
1976 return skb_flow_dissector_bpf_prog_detach(attr);
1977 case BPF_CGROUP_SYSCTL:
1978 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1984 return cgroup_bpf_prog_detach(attr, ptype);
1987 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1989 static int bpf_prog_query(const union bpf_attr *attr,
1990 union bpf_attr __user *uattr)
1992 if (!capable(CAP_NET_ADMIN))
1994 if (CHECK_ATTR(BPF_PROG_QUERY))
1996 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1999 switch (attr->query.attach_type) {
2000 case BPF_CGROUP_INET_INGRESS:
2001 case BPF_CGROUP_INET_EGRESS:
2002 case BPF_CGROUP_INET_SOCK_CREATE:
2003 case BPF_CGROUP_INET4_BIND:
2004 case BPF_CGROUP_INET6_BIND:
2005 case BPF_CGROUP_INET4_POST_BIND:
2006 case BPF_CGROUP_INET6_POST_BIND:
2007 case BPF_CGROUP_INET4_CONNECT:
2008 case BPF_CGROUP_INET6_CONNECT:
2009 case BPF_CGROUP_UDP4_SENDMSG:
2010 case BPF_CGROUP_UDP6_SENDMSG:
2011 case BPF_CGROUP_SOCK_OPS:
2012 case BPF_CGROUP_DEVICE:
2013 case BPF_CGROUP_SYSCTL:
2015 case BPF_LIRC_MODE2:
2016 return lirc_prog_query(attr, uattr);
2017 case BPF_FLOW_DISSECTOR:
2018 return skb_flow_dissector_prog_query(attr, uattr);
2023 return cgroup_bpf_prog_query(attr, uattr);
2026 #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
2028 static int bpf_prog_test_run(const union bpf_attr *attr,
2029 union bpf_attr __user *uattr)
2031 struct bpf_prog *prog;
2032 int ret = -ENOTSUPP;
2034 if (!capable(CAP_SYS_ADMIN))
2036 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2039 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2040 (!attr->test.ctx_size_in && attr->test.ctx_in))
2043 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2044 (!attr->test.ctx_size_out && attr->test.ctx_out))
2047 prog = bpf_prog_get(attr->test.prog_fd);
2049 return PTR_ERR(prog);
2051 if (prog->aux->ops->test_run)
2052 ret = prog->aux->ops->test_run(prog, attr, uattr);
2058 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2060 static int bpf_obj_get_next_id(const union bpf_attr *attr,
2061 union bpf_attr __user *uattr,
2065 u32 next_id = attr->start_id;
2068 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2071 if (!capable(CAP_SYS_ADMIN))
2076 if (!idr_get_next(idr, &next_id))
2078 spin_unlock_bh(lock);
2081 err = put_user(next_id, &uattr->next_id);
2086 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2088 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2090 struct bpf_prog *prog;
2091 u32 id = attr->prog_id;
2094 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2097 if (!capable(CAP_SYS_ADMIN))
2100 spin_lock_bh(&prog_idr_lock);
2101 prog = idr_find(&prog_idr, id);
2103 prog = bpf_prog_inc_not_zero(prog);
2105 prog = ERR_PTR(-ENOENT);
2106 spin_unlock_bh(&prog_idr_lock);
2109 return PTR_ERR(prog);
2111 fd = bpf_prog_new_fd(prog);
2118 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2120 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2122 struct bpf_map *map;
2123 u32 id = attr->map_id;
2127 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2128 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
2131 if (!capable(CAP_SYS_ADMIN))
2134 f_flags = bpf_get_file_flag(attr->open_flags);
2138 spin_lock_bh(&map_idr_lock);
2139 map = idr_find(&map_idr, id);
2141 map = bpf_map_inc_not_zero(map, true);
2143 map = ERR_PTR(-ENOENT);
2144 spin_unlock_bh(&map_idr_lock);
2147 return PTR_ERR(map);
2149 fd = bpf_map_new_fd(map, f_flags);
2151 bpf_map_put_with_uref(map);
2156 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
2157 unsigned long addr, u32 *off,
2160 const struct bpf_map *map;
2163 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2164 map = prog->aux->used_maps[i];
2165 if (map == (void *)addr) {
2166 *type = BPF_PSEUDO_MAP_FD;
2169 if (!map->ops->map_direct_value_meta)
2171 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2172 *type = BPF_PSEUDO_MAP_VALUE;
2180 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2182 const struct bpf_map *map;
2183 struct bpf_insn *insns;
2188 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2193 for (i = 0; i < prog->len; i++) {
2194 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2195 insns[i].code = BPF_JMP | BPF_CALL;
2196 insns[i].imm = BPF_FUNC_tail_call;
2199 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2200 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2201 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2202 insns[i].code = BPF_JMP | BPF_CALL;
2203 if (!bpf_dump_raw_ok())
2208 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2211 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
2212 map = bpf_map_from_imm(prog, imm, &off, &type);
2214 insns[i].src_reg = type;
2215 insns[i].imm = map->id;
2216 insns[i + 1].imm = off;
2224 static int set_info_rec_size(struct bpf_prog_info *info)
2227 * Ensure info.*_rec_size is the same as kernel expected size
2231 * Only allow zero *_rec_size if both _rec_size and _cnt are
2232 * zero. In this case, the kernel will set the expected
2233 * _rec_size back to the info.
2236 if ((info->nr_func_info || info->func_info_rec_size) &&
2237 info->func_info_rec_size != sizeof(struct bpf_func_info))
2240 if ((info->nr_line_info || info->line_info_rec_size) &&
2241 info->line_info_rec_size != sizeof(struct bpf_line_info))
2244 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
2245 info->jited_line_info_rec_size != sizeof(__u64))
2248 info->func_info_rec_size = sizeof(struct bpf_func_info);
2249 info->line_info_rec_size = sizeof(struct bpf_line_info);
2250 info->jited_line_info_rec_size = sizeof(__u64);
2255 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2256 const union bpf_attr *attr,
2257 union bpf_attr __user *uattr)
2259 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2260 struct bpf_prog_info info = {};
2261 u32 info_len = attr->info.info_len;
2262 struct bpf_prog_stats stats;
2263 char __user *uinsns;
2267 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2270 info_len = min_t(u32, sizeof(info), info_len);
2272 if (copy_from_user(&info, uinfo, info_len))
2275 info.type = prog->type;
2276 info.id = prog->aux->id;
2277 info.load_time = prog->aux->load_time;
2278 info.created_by_uid = from_kuid_munged(current_user_ns(),
2279 prog->aux->user->uid);
2280 info.gpl_compatible = prog->gpl_compatible;
2282 memcpy(info.tag, prog->tag, sizeof(prog->tag));
2283 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2285 ulen = info.nr_map_ids;
2286 info.nr_map_ids = prog->aux->used_map_cnt;
2287 ulen = min_t(u32, info.nr_map_ids, ulen);
2289 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
2292 for (i = 0; i < ulen; i++)
2293 if (put_user(prog->aux->used_maps[i]->id,
2298 err = set_info_rec_size(&info);
2302 bpf_prog_get_stats(prog, &stats);
2303 info.run_time_ns = stats.nsecs;
2304 info.run_cnt = stats.cnt;
2306 if (!capable(CAP_SYS_ADMIN)) {
2307 info.jited_prog_len = 0;
2308 info.xlated_prog_len = 0;
2309 info.nr_jited_ksyms = 0;
2310 info.nr_jited_func_lens = 0;
2311 info.nr_func_info = 0;
2312 info.nr_line_info = 0;
2313 info.nr_jited_line_info = 0;
2317 ulen = info.xlated_prog_len;
2318 info.xlated_prog_len = bpf_prog_insn_size(prog);
2319 if (info.xlated_prog_len && ulen) {
2320 struct bpf_insn *insns_sanitized;
2323 if (prog->blinded && !bpf_dump_raw_ok()) {
2324 info.xlated_prog_insns = 0;
2327 insns_sanitized = bpf_insn_prepare_dump(prog);
2328 if (!insns_sanitized)
2330 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2331 ulen = min_t(u32, info.xlated_prog_len, ulen);
2332 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2333 kfree(insns_sanitized);
2338 if (bpf_prog_is_dev_bound(prog->aux)) {
2339 err = bpf_prog_offload_info_fill(&info, prog);
2345 /* NOTE: the following code is supposed to be skipped for offload.
2346 * bpf_prog_offload_info_fill() is the place to fill similar fields
2349 ulen = info.jited_prog_len;
2350 if (prog->aux->func_cnt) {
2353 info.jited_prog_len = 0;
2354 for (i = 0; i < prog->aux->func_cnt; i++)
2355 info.jited_prog_len += prog->aux->func[i]->jited_len;
2357 info.jited_prog_len = prog->jited_len;
2360 if (info.jited_prog_len && ulen) {
2361 if (bpf_dump_raw_ok()) {
2362 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2363 ulen = min_t(u32, info.jited_prog_len, ulen);
2365 /* for multi-function programs, copy the JITed
2366 * instructions for all the functions
2368 if (prog->aux->func_cnt) {
2373 for (i = 0; i < prog->aux->func_cnt; i++) {
2374 len = prog->aux->func[i]->jited_len;
2375 len = min_t(u32, len, free);
2376 img = (u8 *) prog->aux->func[i]->bpf_func;
2377 if (copy_to_user(uinsns, img, len))
2385 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2389 info.jited_prog_insns = 0;
2393 ulen = info.nr_jited_ksyms;
2394 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
2396 if (bpf_dump_raw_ok()) {
2397 unsigned long ksym_addr;
2398 u64 __user *user_ksyms;
2401 /* copy the address of the kernel symbol
2402 * corresponding to each function
2404 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2405 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2406 if (prog->aux->func_cnt) {
2407 for (i = 0; i < ulen; i++) {
2408 ksym_addr = (unsigned long)
2409 prog->aux->func[i]->bpf_func;
2410 if (put_user((u64) ksym_addr,
2415 ksym_addr = (unsigned long) prog->bpf_func;
2416 if (put_user((u64) ksym_addr, &user_ksyms[0]))
2420 info.jited_ksyms = 0;
2424 ulen = info.nr_jited_func_lens;
2425 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
2427 if (bpf_dump_raw_ok()) {
2428 u32 __user *user_lens;
2431 /* copy the JITed image lengths for each function */
2432 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2433 user_lens = u64_to_user_ptr(info.jited_func_lens);
2434 if (prog->aux->func_cnt) {
2435 for (i = 0; i < ulen; i++) {
2437 prog->aux->func[i]->jited_len;
2438 if (put_user(func_len, &user_lens[i]))
2442 func_len = prog->jited_len;
2443 if (put_user(func_len, &user_lens[0]))
2447 info.jited_func_lens = 0;
2452 info.btf_id = btf_id(prog->aux->btf);
2454 ulen = info.nr_func_info;
2455 info.nr_func_info = prog->aux->func_info_cnt;
2456 if (info.nr_func_info && ulen) {
2457 char __user *user_finfo;
2459 user_finfo = u64_to_user_ptr(info.func_info);
2460 ulen = min_t(u32, info.nr_func_info, ulen);
2461 if (copy_to_user(user_finfo, prog->aux->func_info,
2462 info.func_info_rec_size * ulen))
2466 ulen = info.nr_line_info;
2467 info.nr_line_info = prog->aux->nr_linfo;
2468 if (info.nr_line_info && ulen) {
2469 __u8 __user *user_linfo;
2471 user_linfo = u64_to_user_ptr(info.line_info);
2472 ulen = min_t(u32, info.nr_line_info, ulen);
2473 if (copy_to_user(user_linfo, prog->aux->linfo,
2474 info.line_info_rec_size * ulen))
2478 ulen = info.nr_jited_line_info;
2479 if (prog->aux->jited_linfo)
2480 info.nr_jited_line_info = prog->aux->nr_linfo;
2482 info.nr_jited_line_info = 0;
2483 if (info.nr_jited_line_info && ulen) {
2484 if (bpf_dump_raw_ok()) {
2485 __u64 __user *user_linfo;
2488 user_linfo = u64_to_user_ptr(info.jited_line_info);
2489 ulen = min_t(u32, info.nr_jited_line_info, ulen);
2490 for (i = 0; i < ulen; i++) {
2491 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2496 info.jited_line_info = 0;
2500 ulen = info.nr_prog_tags;
2501 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2503 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2506 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2507 ulen = min_t(u32, info.nr_prog_tags, ulen);
2508 if (prog->aux->func_cnt) {
2509 for (i = 0; i < ulen; i++) {
2510 if (copy_to_user(user_prog_tags[i],
2511 prog->aux->func[i]->tag,
2516 if (copy_to_user(user_prog_tags[0],
2517 prog->tag, BPF_TAG_SIZE))
2523 if (copy_to_user(uinfo, &info, info_len) ||
2524 put_user(info_len, &uattr->info.info_len))
2530 static int bpf_map_get_info_by_fd(struct bpf_map *map,
2531 const union bpf_attr *attr,
2532 union bpf_attr __user *uattr)
2534 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2535 struct bpf_map_info info = {};
2536 u32 info_len = attr->info.info_len;
2539 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2542 info_len = min_t(u32, sizeof(info), info_len);
2544 info.type = map->map_type;
2546 info.key_size = map->key_size;
2547 info.value_size = map->value_size;
2548 info.max_entries = map->max_entries;
2549 info.map_flags = map->map_flags;
2550 memcpy(info.name, map->name, sizeof(map->name));
2553 info.btf_id = btf_id(map->btf);
2554 info.btf_key_type_id = map->btf_key_type_id;
2555 info.btf_value_type_id = map->btf_value_type_id;
2558 if (bpf_map_is_dev_bound(map)) {
2559 err = bpf_map_offload_info_fill(&info, map);
2564 if (copy_to_user(uinfo, &info, info_len) ||
2565 put_user(info_len, &uattr->info.info_len))
2571 static int bpf_btf_get_info_by_fd(struct btf *btf,
2572 const union bpf_attr *attr,
2573 union bpf_attr __user *uattr)
2575 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2576 u32 info_len = attr->info.info_len;
2579 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
2583 return btf_get_info_by_fd(btf, attr, uattr);
2586 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2588 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2589 union bpf_attr __user *uattr)
2591 int ufd = attr->info.bpf_fd;
2595 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2602 if (f.file->f_op == &bpf_prog_fops)
2603 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2605 else if (f.file->f_op == &bpf_map_fops)
2606 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2608 else if (f.file->f_op == &btf_fops)
2609 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
2617 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2619 static int bpf_btf_load(const union bpf_attr *attr)
2621 if (CHECK_ATTR(BPF_BTF_LOAD))
2624 if (!capable(CAP_SYS_ADMIN))
2627 return btf_new_fd(attr);
2630 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2632 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2634 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2637 if (!capable(CAP_SYS_ADMIN))
2640 return btf_get_fd_by_id(attr->btf_id);
2643 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2644 union bpf_attr __user *uattr,
2645 u32 prog_id, u32 fd_type,
2646 const char *buf, u64 probe_offset,
2649 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2650 u32 len = buf ? strlen(buf) : 0, input_len;
2653 if (put_user(len, &uattr->task_fd_query.buf_len))
2655 input_len = attr->task_fd_query.buf_len;
2656 if (input_len && ubuf) {
2658 /* nothing to copy, just make ubuf NULL terminated */
2661 if (put_user(zero, ubuf))
2663 } else if (input_len >= len + 1) {
2664 /* ubuf can hold the string with NULL terminator */
2665 if (copy_to_user(ubuf, buf, len + 1))
2668 /* ubuf cannot hold the string with NULL terminator,
2669 * do a partial copy with NULL terminator.
2674 if (copy_to_user(ubuf, buf, input_len - 1))
2676 if (put_user(zero, ubuf + input_len - 1))
2681 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2682 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2683 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2684 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2690 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2692 static int bpf_task_fd_query(const union bpf_attr *attr,
2693 union bpf_attr __user *uattr)
2695 pid_t pid = attr->task_fd_query.pid;
2696 u32 fd = attr->task_fd_query.fd;
2697 const struct perf_event *event;
2698 struct files_struct *files;
2699 struct task_struct *task;
2703 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2706 if (!capable(CAP_SYS_ADMIN))
2709 if (attr->task_fd_query.flags != 0)
2712 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2716 files = get_files_struct(task);
2717 put_task_struct(task);
2722 spin_lock(&files->file_lock);
2723 file = fcheck_files(files, fd);
2728 spin_unlock(&files->file_lock);
2729 put_files_struct(files);
2734 if (file->f_op == &bpf_raw_tp_fops) {
2735 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2736 struct bpf_raw_event_map *btp = raw_tp->btp;
2738 err = bpf_task_fd_query_copy(attr, uattr,
2739 raw_tp->prog->aux->id,
2740 BPF_FD_TYPE_RAW_TRACEPOINT,
2741 btp->tp->name, 0, 0);
2745 event = perf_get_event(file);
2746 if (!IS_ERR(event)) {
2747 u64 probe_offset, probe_addr;
2748 u32 prog_id, fd_type;
2751 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2752 &buf, &probe_offset,
2755 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2769 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2771 union bpf_attr attr = {};
2774 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
2777 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
2780 size = min_t(u32, size, sizeof(attr));
2782 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2783 if (copy_from_user(&attr, uattr, size) != 0)
2786 err = security_bpf(cmd, &attr, size);
2791 case BPF_MAP_CREATE:
2792 err = map_create(&attr);
2794 case BPF_MAP_LOOKUP_ELEM:
2795 err = map_lookup_elem(&attr);
2797 case BPF_MAP_UPDATE_ELEM:
2798 err = map_update_elem(&attr);
2800 case BPF_MAP_DELETE_ELEM:
2801 err = map_delete_elem(&attr);
2803 case BPF_MAP_GET_NEXT_KEY:
2804 err = map_get_next_key(&attr);
2806 case BPF_MAP_FREEZE:
2807 err = map_freeze(&attr);
2810 err = bpf_prog_load(&attr, uattr);
2813 err = bpf_obj_pin(&attr);
2816 err = bpf_obj_get(&attr);
2818 case BPF_PROG_ATTACH:
2819 err = bpf_prog_attach(&attr);
2821 case BPF_PROG_DETACH:
2822 err = bpf_prog_detach(&attr);
2824 case BPF_PROG_QUERY:
2825 err = bpf_prog_query(&attr, uattr);
2827 case BPF_PROG_TEST_RUN:
2828 err = bpf_prog_test_run(&attr, uattr);
2830 case BPF_PROG_GET_NEXT_ID:
2831 err = bpf_obj_get_next_id(&attr, uattr,
2832 &prog_idr, &prog_idr_lock);
2834 case BPF_MAP_GET_NEXT_ID:
2835 err = bpf_obj_get_next_id(&attr, uattr,
2836 &map_idr, &map_idr_lock);
2838 case BPF_PROG_GET_FD_BY_ID:
2839 err = bpf_prog_get_fd_by_id(&attr);
2841 case BPF_MAP_GET_FD_BY_ID:
2842 err = bpf_map_get_fd_by_id(&attr);
2844 case BPF_OBJ_GET_INFO_BY_FD:
2845 err = bpf_obj_get_info_by_fd(&attr, uattr);
2847 case BPF_RAW_TRACEPOINT_OPEN:
2848 err = bpf_raw_tracepoint_open(&attr);
2851 err = bpf_btf_load(&attr);
2853 case BPF_BTF_GET_FD_BY_ID:
2854 err = bpf_btf_get_fd_by_id(&attr);
2856 case BPF_TASK_FD_QUERY:
2857 err = bpf_task_fd_query(&attr, uattr);
2859 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2860 err = map_lookup_and_delete_elem(&attr);