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 static int bpf_charge_memlock(struct user_struct *user, u32 pages)
193 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
195 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
196 atomic_long_sub(pages, &user->locked_vm);
202 static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
205 atomic_long_sub(pages, &user->locked_vm);
208 int bpf_map_charge_init(struct bpf_map_memory *mem, u32 pages)
210 struct user_struct *user = get_current_user();
213 ret = bpf_charge_memlock(user, pages);
225 void bpf_map_charge_finish(struct bpf_map_memory *mem)
227 bpf_uncharge_memlock(mem->user, mem->pages);
231 void bpf_map_charge_move(struct bpf_map_memory *dst,
232 struct bpf_map_memory *src)
236 /* Make sure src will not be used for the redundant uncharging. */
237 memset(src, 0, sizeof(struct bpf_map_memory));
240 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
244 ret = bpf_charge_memlock(map->memory.user, pages);
247 map->memory.pages += pages;
251 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
253 bpf_uncharge_memlock(map->memory.user, pages);
254 map->memory.pages -= pages;
257 static int bpf_map_alloc_id(struct bpf_map *map)
261 idr_preload(GFP_KERNEL);
262 spin_lock_bh(&map_idr_lock);
263 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
266 spin_unlock_bh(&map_idr_lock);
269 if (WARN_ON_ONCE(!id))
272 return id > 0 ? 0 : id;
275 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
279 /* Offloaded maps are removed from the IDR store when their device
280 * disappears - even if someone holds an fd to them they are unusable,
281 * the memory is gone, all ops will fail; they are simply waiting for
282 * refcnt to drop to be freed.
288 spin_lock_irqsave(&map_idr_lock, flags);
290 __acquire(&map_idr_lock);
292 idr_remove(&map_idr, map->id);
296 spin_unlock_irqrestore(&map_idr_lock, flags);
298 __release(&map_idr_lock);
301 /* called from workqueue */
302 static void bpf_map_free_deferred(struct work_struct *work)
304 struct bpf_map *map = container_of(work, struct bpf_map, work);
305 struct bpf_map_memory mem;
307 bpf_map_charge_move(&mem, &map->memory);
308 security_bpf_map_free(map);
309 /* implementation dependent freeing */
310 map->ops->map_free(map);
311 bpf_map_charge_finish(&mem);
314 static void bpf_map_put_uref(struct bpf_map *map)
316 if (atomic_dec_and_test(&map->usercnt)) {
317 if (map->ops->map_release_uref)
318 map->ops->map_release_uref(map);
322 /* decrement map refcnt and schedule it for freeing via workqueue
323 * (unrelying map implementation ops->map_free() might sleep)
325 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
327 if (atomic_dec_and_test(&map->refcnt)) {
328 /* bpf_map_free_id() must be called first */
329 bpf_map_free_id(map, do_idr_lock);
331 INIT_WORK(&map->work, bpf_map_free_deferred);
332 schedule_work(&map->work);
336 void bpf_map_put(struct bpf_map *map)
338 __bpf_map_put(map, true);
340 EXPORT_SYMBOL_GPL(bpf_map_put);
342 void bpf_map_put_with_uref(struct bpf_map *map)
344 bpf_map_put_uref(map);
348 static int bpf_map_release(struct inode *inode, struct file *filp)
350 struct bpf_map *map = filp->private_data;
352 if (map->ops->map_release)
353 map->ops->map_release(map, filp);
355 bpf_map_put_with_uref(map);
359 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
361 fmode_t mode = f.file->f_mode;
363 /* Our file permissions may have been overridden by global
364 * map permissions facing syscall side.
366 if (READ_ONCE(map->frozen))
367 mode &= ~FMODE_CAN_WRITE;
371 #ifdef CONFIG_PROC_FS
372 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
374 const struct bpf_map *map = filp->private_data;
375 const struct bpf_array *array;
376 u32 owner_prog_type = 0;
379 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
380 array = container_of(map, struct bpf_array, map);
381 owner_prog_type = array->owner_prog_type;
382 owner_jited = array->owner_jited;
399 map->memory.pages * 1ULL << PAGE_SHIFT,
401 READ_ONCE(map->frozen));
403 if (owner_prog_type) {
404 seq_printf(m, "owner_prog_type:\t%u\n",
406 seq_printf(m, "owner_jited:\t%u\n",
412 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
415 /* We need this handler such that alloc_file() enables
416 * f_mode with FMODE_CAN_READ.
421 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
422 size_t siz, loff_t *ppos)
424 /* We need this handler such that alloc_file() enables
425 * f_mode with FMODE_CAN_WRITE.
430 const struct file_operations bpf_map_fops = {
431 #ifdef CONFIG_PROC_FS
432 .show_fdinfo = bpf_map_show_fdinfo,
434 .release = bpf_map_release,
435 .read = bpf_dummy_read,
436 .write = bpf_dummy_write,
439 int bpf_map_new_fd(struct bpf_map *map, int flags)
443 ret = security_bpf_map(map, OPEN_FMODE(flags));
447 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
451 int bpf_get_file_flag(int flags)
453 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
455 if (flags & BPF_F_RDONLY)
457 if (flags & BPF_F_WRONLY)
462 /* helper macro to check that unused fields 'union bpf_attr' are zero */
463 #define CHECK_ATTR(CMD) \
464 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
465 sizeof(attr->CMD##_LAST_FIELD), 0, \
467 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
468 sizeof(attr->CMD##_LAST_FIELD)) != NULL
470 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
471 * Return 0 on success and < 0 on error.
473 static int bpf_obj_name_cpy(char *dst, const char *src)
475 const char *end = src + BPF_OBJ_NAME_LEN;
477 memset(dst, 0, BPF_OBJ_NAME_LEN);
478 /* Copy all isalnum(), '_' and '.' chars. */
479 while (src < end && *src) {
480 if (!isalnum(*src) &&
481 *src != '_' && *src != '.')
486 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
493 int map_check_no_btf(const struct bpf_map *map,
494 const struct btf *btf,
495 const struct btf_type *key_type,
496 const struct btf_type *value_type)
501 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
502 u32 btf_key_id, u32 btf_value_id)
504 const struct btf_type *key_type, *value_type;
505 u32 key_size, value_size;
508 /* Some maps allow key to be unspecified. */
510 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
511 if (!key_type || key_size != map->key_size)
514 key_type = btf_type_by_id(btf, 0);
515 if (!map->ops->map_check_btf)
519 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
520 if (!value_type || value_size != map->value_size)
523 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
525 if (map_value_has_spin_lock(map)) {
526 if (map->map_flags & BPF_F_RDONLY_PROG)
528 if (map->map_type != BPF_MAP_TYPE_HASH &&
529 map->map_type != BPF_MAP_TYPE_ARRAY &&
530 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
531 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
533 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
536 "verifier bug spin_lock_off %d value_size %d\n",
537 map->spin_lock_off, map->value_size);
542 if (map->ops->map_check_btf)
543 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
548 #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
549 /* called via syscall */
550 static int map_create(union bpf_attr *attr)
552 int numa_node = bpf_map_attr_numa_node(attr);
553 struct bpf_map_memory mem;
558 err = CHECK_ATTR(BPF_MAP_CREATE);
562 f_flags = bpf_get_file_flag(attr->map_flags);
566 if (numa_node != NUMA_NO_NODE &&
567 ((unsigned int)numa_node >= nr_node_ids ||
568 !node_online(numa_node)))
571 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
572 map = find_and_alloc_map(attr);
576 err = bpf_obj_name_cpy(map->name, attr->map_name);
580 atomic_set(&map->refcnt, 1);
581 atomic_set(&map->usercnt, 1);
583 if (attr->btf_key_type_id || attr->btf_value_type_id) {
586 if (!attr->btf_value_type_id) {
591 btf = btf_get_by_fd(attr->btf_fd);
597 err = map_check_btf(map, btf, attr->btf_key_type_id,
598 attr->btf_value_type_id);
605 map->btf_key_type_id = attr->btf_key_type_id;
606 map->btf_value_type_id = attr->btf_value_type_id;
608 map->spin_lock_off = -EINVAL;
611 err = security_bpf_map_alloc(map);
615 err = bpf_map_alloc_id(map);
619 err = bpf_map_new_fd(map, f_flags);
621 /* failed to allocate fd.
622 * bpf_map_put_with_uref() is needed because the above
623 * bpf_map_alloc_id() has published the map
624 * to the userspace and the userspace may
625 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
627 bpf_map_put_with_uref(map);
634 security_bpf_map_free(map);
637 bpf_map_charge_move(&mem, &map->memory);
638 map->ops->map_free(map);
639 bpf_map_charge_finish(&mem);
643 /* if error is returned, fd is released.
644 * On success caller should complete fd access with matching fdput()
646 struct bpf_map *__bpf_map_get(struct fd f)
649 return ERR_PTR(-EBADF);
650 if (f.file->f_op != &bpf_map_fops) {
652 return ERR_PTR(-EINVAL);
655 return f.file->private_data;
658 /* prog's and map's refcnt limit */
659 #define BPF_MAX_REFCNT 32768
661 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
663 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
664 atomic_dec(&map->refcnt);
665 return ERR_PTR(-EBUSY);
668 atomic_inc(&map->usercnt);
671 EXPORT_SYMBOL_GPL(bpf_map_inc);
673 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
675 struct fd f = fdget(ufd);
678 map = __bpf_map_get(f);
682 map = bpf_map_inc(map, true);
688 /* map_idr_lock should have been held */
689 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
694 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
696 if (refold >= BPF_MAX_REFCNT) {
697 __bpf_map_put(map, false);
698 return ERR_PTR(-EBUSY);
702 return ERR_PTR(-ENOENT);
705 atomic_inc(&map->usercnt);
710 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
715 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
718 return memdup_user(ukey, key_size);
721 return ERR_PTR(-EINVAL);
726 /* last field in 'union bpf_attr' used by this command */
727 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
729 static int map_lookup_elem(union bpf_attr *attr)
731 void __user *ukey = u64_to_user_ptr(attr->key);
732 void __user *uvalue = u64_to_user_ptr(attr->value);
733 int ufd = attr->map_fd;
735 void *key, *value, *ptr;
740 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
743 if (attr->flags & ~BPF_F_LOCK)
747 map = __bpf_map_get(f);
750 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
755 if ((attr->flags & BPF_F_LOCK) &&
756 !map_value_has_spin_lock(map)) {
761 key = __bpf_copy_key(ukey, map->key_size);
767 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
768 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
769 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
770 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
771 value_size = round_up(map->value_size, 8) * num_possible_cpus();
772 else if (IS_FD_MAP(map))
773 value_size = sizeof(u32);
775 value_size = map->value_size;
778 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
782 if (bpf_map_is_dev_bound(map)) {
783 err = bpf_map_offload_lookup_elem(map, key, value);
788 this_cpu_inc(bpf_prog_active);
789 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
790 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
791 err = bpf_percpu_hash_copy(map, key, value);
792 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
793 err = bpf_percpu_array_copy(map, key, value);
794 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
795 err = bpf_percpu_cgroup_storage_copy(map, key, value);
796 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
797 err = bpf_stackmap_copy(map, key, value);
798 } else if (IS_FD_ARRAY(map)) {
799 err = bpf_fd_array_map_lookup_elem(map, key, value);
800 } else if (IS_FD_HASH(map)) {
801 err = bpf_fd_htab_map_lookup_elem(map, key, value);
802 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
803 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
804 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
805 map->map_type == BPF_MAP_TYPE_STACK) {
806 err = map->ops->map_peek_elem(map, value);
809 if (map->ops->map_lookup_elem_sys_only)
810 ptr = map->ops->map_lookup_elem_sys_only(map, key);
812 ptr = map->ops->map_lookup_elem(map, key);
819 if (attr->flags & BPF_F_LOCK)
820 /* lock 'ptr' and copy everything but lock */
821 copy_map_value_locked(map, value, ptr, true);
823 copy_map_value(map, value, ptr);
824 /* mask lock, since value wasn't zero inited */
825 check_and_init_map_lock(map, value);
829 this_cpu_dec(bpf_prog_active);
837 if (copy_to_user(uvalue, value, value_size) != 0)
851 static void maybe_wait_bpf_programs(struct bpf_map *map)
853 /* Wait for any running BPF programs to complete so that
854 * userspace, when we return to it, knows that all programs
855 * that could be running use the new map value.
857 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
858 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
862 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
864 static int map_update_elem(union bpf_attr *attr)
866 void __user *ukey = u64_to_user_ptr(attr->key);
867 void __user *uvalue = u64_to_user_ptr(attr->value);
868 int ufd = attr->map_fd;
875 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
879 map = __bpf_map_get(f);
882 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
887 if ((attr->flags & BPF_F_LOCK) &&
888 !map_value_has_spin_lock(map)) {
893 key = __bpf_copy_key(ukey, map->key_size);
899 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
900 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
901 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
902 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
903 value_size = round_up(map->value_size, 8) * num_possible_cpus();
905 value_size = map->value_size;
908 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
913 if (copy_from_user(value, uvalue, value_size) != 0)
916 /* Need to create a kthread, thus must support schedule */
917 if (bpf_map_is_dev_bound(map)) {
918 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
920 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
921 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
922 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
923 err = map->ops->map_update_elem(map, key, value, attr->flags);
927 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
928 * inside bpf map update or delete otherwise deadlocks are possible
931 __this_cpu_inc(bpf_prog_active);
932 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
933 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
934 err = bpf_percpu_hash_update(map, key, value, attr->flags);
935 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
936 err = bpf_percpu_array_update(map, key, value, attr->flags);
937 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
938 err = bpf_percpu_cgroup_storage_update(map, key, value,
940 } else if (IS_FD_ARRAY(map)) {
942 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
945 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
947 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
950 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
951 /* rcu_read_lock() is not needed */
952 err = bpf_fd_reuseport_array_update_elem(map, key, value,
954 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
955 map->map_type == BPF_MAP_TYPE_STACK) {
956 err = map->ops->map_push_elem(map, value, attr->flags);
959 err = map->ops->map_update_elem(map, key, value, attr->flags);
962 __this_cpu_dec(bpf_prog_active);
964 maybe_wait_bpf_programs(map);
975 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
977 static int map_delete_elem(union bpf_attr *attr)
979 void __user *ukey = u64_to_user_ptr(attr->key);
980 int ufd = attr->map_fd;
986 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
990 map = __bpf_map_get(f);
993 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
998 key = __bpf_copy_key(ukey, map->key_size);
1004 if (bpf_map_is_dev_bound(map)) {
1005 err = bpf_map_offload_delete_elem(map, key);
1010 __this_cpu_inc(bpf_prog_active);
1012 err = map->ops->map_delete_elem(map, key);
1014 __this_cpu_dec(bpf_prog_active);
1016 maybe_wait_bpf_programs(map);
1024 /* last field in 'union bpf_attr' used by this command */
1025 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1027 static int map_get_next_key(union bpf_attr *attr)
1029 void __user *ukey = u64_to_user_ptr(attr->key);
1030 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1031 int ufd = attr->map_fd;
1032 struct bpf_map *map;
1033 void *key, *next_key;
1037 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1041 map = __bpf_map_get(f);
1043 return PTR_ERR(map);
1044 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1050 key = __bpf_copy_key(ukey, map->key_size);
1060 next_key = kmalloc(map->key_size, GFP_USER);
1064 if (bpf_map_is_dev_bound(map)) {
1065 err = bpf_map_offload_get_next_key(map, key, next_key);
1070 err = map->ops->map_get_next_key(map, key, next_key);
1077 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1091 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1093 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1095 void __user *ukey = u64_to_user_ptr(attr->key);
1096 void __user *uvalue = u64_to_user_ptr(attr->value);
1097 int ufd = attr->map_fd;
1098 struct bpf_map *map;
1104 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1108 map = __bpf_map_get(f);
1110 return PTR_ERR(map);
1111 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1116 key = __bpf_copy_key(ukey, map->key_size);
1122 value_size = map->value_size;
1125 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1129 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1130 map->map_type == BPF_MAP_TYPE_STACK) {
1131 err = map->ops->map_pop_elem(map, value);
1139 if (copy_to_user(uvalue, value, value_size) != 0)
1153 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1155 static int map_freeze(const union bpf_attr *attr)
1157 int err = 0, ufd = attr->map_fd;
1158 struct bpf_map *map;
1161 if (CHECK_ATTR(BPF_MAP_FREEZE))
1165 map = __bpf_map_get(f);
1167 return PTR_ERR(map);
1168 if (READ_ONCE(map->frozen)) {
1172 if (!capable(CAP_SYS_ADMIN)) {
1177 WRITE_ONCE(map->frozen, true);
1183 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1184 #define BPF_PROG_TYPE(_id, _name) \
1185 [_id] = & _name ## _prog_ops,
1186 #define BPF_MAP_TYPE(_id, _ops)
1187 #include <linux/bpf_types.h>
1188 #undef BPF_PROG_TYPE
1192 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1194 const struct bpf_prog_ops *ops;
1196 if (type >= ARRAY_SIZE(bpf_prog_types))
1198 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1199 ops = bpf_prog_types[type];
1203 if (!bpf_prog_is_dev_bound(prog->aux))
1204 prog->aux->ops = ops;
1206 prog->aux->ops = &bpf_offload_prog_ops;
1211 /* drop refcnt on maps used by eBPF program and free auxilary data */
1212 static void free_used_maps(struct bpf_prog_aux *aux)
1214 enum bpf_cgroup_storage_type stype;
1217 for_each_cgroup_storage_type(stype) {
1218 if (!aux->cgroup_storage[stype])
1220 bpf_cgroup_storage_release(aux->prog,
1221 aux->cgroup_storage[stype]);
1224 for (i = 0; i < aux->used_map_cnt; i++)
1225 bpf_map_put(aux->used_maps[i]);
1227 kfree(aux->used_maps);
1230 int __bpf_prog_charge(struct user_struct *user, u32 pages)
1232 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1233 unsigned long user_bufs;
1236 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1237 if (user_bufs > memlock_limit) {
1238 atomic_long_sub(pages, &user->locked_vm);
1246 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1249 atomic_long_sub(pages, &user->locked_vm);
1252 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1254 struct user_struct *user = get_current_user();
1257 ret = __bpf_prog_charge(user, prog->pages);
1263 prog->aux->user = user;
1267 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1269 struct user_struct *user = prog->aux->user;
1271 __bpf_prog_uncharge(user, prog->pages);
1275 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1279 idr_preload(GFP_KERNEL);
1280 spin_lock_bh(&prog_idr_lock);
1281 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1284 spin_unlock_bh(&prog_idr_lock);
1287 /* id is in [1, INT_MAX) */
1288 if (WARN_ON_ONCE(!id))
1291 return id > 0 ? 0 : id;
1294 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1296 /* cBPF to eBPF migrations are currently not in the idr store.
1297 * Offloaded programs are removed from the store when their device
1298 * disappears - even if someone grabs an fd to them they are unusable,
1299 * simply waiting for refcnt to drop to be freed.
1305 spin_lock_bh(&prog_idr_lock);
1307 __acquire(&prog_idr_lock);
1309 idr_remove(&prog_idr, prog->aux->id);
1313 spin_unlock_bh(&prog_idr_lock);
1315 __release(&prog_idr_lock);
1318 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1320 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1322 free_used_maps(aux);
1323 bpf_prog_uncharge_memlock(aux->prog);
1324 security_bpf_prog_free(aux);
1325 bpf_prog_free(aux->prog);
1328 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1330 if (atomic_dec_and_test(&prog->aux->refcnt)) {
1331 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1332 /* bpf_prog_free_id() must be called first */
1333 bpf_prog_free_id(prog, do_idr_lock);
1334 bpf_prog_kallsyms_del_all(prog);
1335 btf_put(prog->aux->btf);
1336 kvfree(prog->aux->func_info);
1337 bpf_prog_free_linfo(prog);
1339 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1343 void bpf_prog_put(struct bpf_prog *prog)
1345 __bpf_prog_put(prog, true);
1347 EXPORT_SYMBOL_GPL(bpf_prog_put);
1349 static int bpf_prog_release(struct inode *inode, struct file *filp)
1351 struct bpf_prog *prog = filp->private_data;
1357 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1358 struct bpf_prog_stats *stats)
1360 u64 nsecs = 0, cnt = 0;
1363 for_each_possible_cpu(cpu) {
1364 const struct bpf_prog_stats *st;
1368 st = per_cpu_ptr(prog->aux->stats, cpu);
1370 start = u64_stats_fetch_begin_irq(&st->syncp);
1373 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1377 stats->nsecs = nsecs;
1381 #ifdef CONFIG_PROC_FS
1382 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1384 const struct bpf_prog *prog = filp->private_data;
1385 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1386 struct bpf_prog_stats stats;
1388 bpf_prog_get_stats(prog, &stats);
1389 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1396 "run_time_ns:\t%llu\n"
1401 prog->pages * 1ULL << PAGE_SHIFT,
1408 const struct file_operations bpf_prog_fops = {
1409 #ifdef CONFIG_PROC_FS
1410 .show_fdinfo = bpf_prog_show_fdinfo,
1412 .release = bpf_prog_release,
1413 .read = bpf_dummy_read,
1414 .write = bpf_dummy_write,
1417 int bpf_prog_new_fd(struct bpf_prog *prog)
1421 ret = security_bpf_prog(prog);
1425 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1426 O_RDWR | O_CLOEXEC);
1429 static struct bpf_prog *____bpf_prog_get(struct fd f)
1432 return ERR_PTR(-EBADF);
1433 if (f.file->f_op != &bpf_prog_fops) {
1435 return ERR_PTR(-EINVAL);
1438 return f.file->private_data;
1441 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
1443 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1444 atomic_sub(i, &prog->aux->refcnt);
1445 return ERR_PTR(-EBUSY);
1449 EXPORT_SYMBOL_GPL(bpf_prog_add);
1451 void bpf_prog_sub(struct bpf_prog *prog, int i)
1453 /* Only to be used for undoing previous bpf_prog_add() in some
1454 * error path. We still know that another entity in our call
1455 * path holds a reference to the program, thus atomic_sub() can
1456 * be safely used in such cases!
1458 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1460 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1462 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1464 return bpf_prog_add(prog, 1);
1466 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1468 /* prog_idr_lock should have been held */
1469 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1473 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1475 if (refold >= BPF_MAX_REFCNT) {
1476 __bpf_prog_put(prog, false);
1477 return ERR_PTR(-EBUSY);
1481 return ERR_PTR(-ENOENT);
1485 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1487 bool bpf_prog_get_ok(struct bpf_prog *prog,
1488 enum bpf_prog_type *attach_type, bool attach_drv)
1490 /* not an attachment, just a refcount inc, always allow */
1494 if (prog->type != *attach_type)
1496 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1502 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1505 struct fd f = fdget(ufd);
1506 struct bpf_prog *prog;
1508 prog = ____bpf_prog_get(f);
1511 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1512 prog = ERR_PTR(-EINVAL);
1516 prog = bpf_prog_inc(prog);
1522 struct bpf_prog *bpf_prog_get(u32 ufd)
1524 return __bpf_prog_get(ufd, NULL, false);
1527 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1530 return __bpf_prog_get(ufd, &type, attach_drv);
1532 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1534 /* Initially all BPF programs could be loaded w/o specifying
1535 * expected_attach_type. Later for some of them specifying expected_attach_type
1536 * at load time became required so that program could be validated properly.
1537 * Programs of types that are allowed to be loaded both w/ and w/o (for
1538 * backward compatibility) expected_attach_type, should have the default attach
1539 * type assigned to expected_attach_type for the latter case, so that it can be
1540 * validated later at attach time.
1542 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1543 * prog type requires it but has some attach types that have to be backward
1546 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1548 switch (attr->prog_type) {
1549 case BPF_PROG_TYPE_CGROUP_SOCK:
1550 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1551 * exist so checking for non-zero is the way to go here.
1553 if (!attr->expected_attach_type)
1554 attr->expected_attach_type =
1555 BPF_CGROUP_INET_SOCK_CREATE;
1561 bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1562 enum bpf_attach_type expected_attach_type)
1564 switch (prog_type) {
1565 case BPF_PROG_TYPE_CGROUP_SOCK:
1566 switch (expected_attach_type) {
1567 case BPF_CGROUP_INET_SOCK_CREATE:
1568 case BPF_CGROUP_INET4_POST_BIND:
1569 case BPF_CGROUP_INET6_POST_BIND:
1574 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1575 switch (expected_attach_type) {
1576 case BPF_CGROUP_INET4_BIND:
1577 case BPF_CGROUP_INET6_BIND:
1578 case BPF_CGROUP_INET4_CONNECT:
1579 case BPF_CGROUP_INET6_CONNECT:
1580 case BPF_CGROUP_UDP4_SENDMSG:
1581 case BPF_CGROUP_UDP6_SENDMSG:
1586 case BPF_PROG_TYPE_CGROUP_SKB:
1587 switch (expected_attach_type) {
1588 case BPF_CGROUP_INET_INGRESS:
1589 case BPF_CGROUP_INET_EGRESS:
1599 /* last field in 'union bpf_attr' used by this command */
1600 #define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
1602 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
1604 enum bpf_prog_type type = attr->prog_type;
1605 struct bpf_prog *prog;
1610 if (CHECK_ATTR(BPF_PROG_LOAD))
1613 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
1614 BPF_F_ANY_ALIGNMENT |
1615 BPF_F_TEST_RND_HI32))
1618 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1619 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1620 !capable(CAP_SYS_ADMIN))
1623 /* copy eBPF program license from user space */
1624 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1625 sizeof(license) - 1) < 0)
1627 license[sizeof(license) - 1] = 0;
1629 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1630 is_gpl = license_is_gpl_compatible(license);
1632 if (attr->insn_cnt == 0 ||
1633 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
1635 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1636 type != BPF_PROG_TYPE_CGROUP_SKB &&
1637 !capable(CAP_SYS_ADMIN))
1640 bpf_prog_load_fixup_attach_type(attr);
1641 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1644 /* plain bpf_prog allocation */
1645 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1649 prog->expected_attach_type = attr->expected_attach_type;
1651 prog->aux->offload_requested = !!attr->prog_ifindex;
1653 err = security_bpf_prog_alloc(prog->aux);
1655 goto free_prog_nouncharge;
1657 err = bpf_prog_charge_memlock(prog);
1661 prog->len = attr->insn_cnt;
1664 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1665 bpf_prog_insn_size(prog)) != 0)
1668 prog->orig_prog = NULL;
1671 atomic_set(&prog->aux->refcnt, 1);
1672 prog->gpl_compatible = is_gpl ? 1 : 0;
1674 if (bpf_prog_is_dev_bound(prog->aux)) {
1675 err = bpf_prog_offload_init(prog, attr);
1680 /* find program type: socket_filter vs tracing_filter */
1681 err = find_prog_type(type, prog);
1685 prog->aux->load_time = ktime_get_boot_ns();
1686 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1690 /* run eBPF verifier */
1691 err = bpf_check(&prog, attr, uattr);
1693 goto free_used_maps;
1695 prog = bpf_prog_select_runtime(prog, &err);
1697 goto free_used_maps;
1699 err = bpf_prog_alloc_id(prog);
1701 goto free_used_maps;
1703 err = bpf_prog_new_fd(prog);
1705 /* failed to allocate fd.
1706 * bpf_prog_put() is needed because the above
1707 * bpf_prog_alloc_id() has published the prog
1708 * to the userspace and the userspace may
1709 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1715 bpf_prog_kallsyms_add(prog);
1716 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
1720 bpf_prog_free_linfo(prog);
1721 kvfree(prog->aux->func_info);
1722 btf_put(prog->aux->btf);
1723 bpf_prog_kallsyms_del_subprogs(prog);
1724 free_used_maps(prog->aux);
1726 bpf_prog_uncharge_memlock(prog);
1728 security_bpf_prog_free(prog->aux);
1729 free_prog_nouncharge:
1730 bpf_prog_free(prog);
1734 #define BPF_OBJ_LAST_FIELD file_flags
1736 static int bpf_obj_pin(const union bpf_attr *attr)
1738 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1741 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1744 static int bpf_obj_get(const union bpf_attr *attr)
1746 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1747 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1750 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1754 struct bpf_raw_tracepoint {
1755 struct bpf_raw_event_map *btp;
1756 struct bpf_prog *prog;
1759 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1761 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1764 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1765 bpf_prog_put(raw_tp->prog);
1767 bpf_put_raw_tracepoint(raw_tp->btp);
1772 static const struct file_operations bpf_raw_tp_fops = {
1773 .release = bpf_raw_tracepoint_release,
1774 .read = bpf_dummy_read,
1775 .write = bpf_dummy_write,
1778 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1780 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1782 struct bpf_raw_tracepoint *raw_tp;
1783 struct bpf_raw_event_map *btp;
1784 struct bpf_prog *prog;
1788 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1789 sizeof(tp_name) - 1) < 0)
1791 tp_name[sizeof(tp_name) - 1] = 0;
1793 btp = bpf_get_raw_tracepoint(tp_name);
1797 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1804 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
1806 err = PTR_ERR(prog);
1809 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1810 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
1815 err = bpf_probe_register(raw_tp->btp, prog);
1819 raw_tp->prog = prog;
1820 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1823 bpf_probe_unregister(raw_tp->btp, prog);
1834 bpf_put_raw_tracepoint(btp);
1838 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1839 enum bpf_attach_type attach_type)
1841 switch (prog->type) {
1842 case BPF_PROG_TYPE_CGROUP_SOCK:
1843 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1844 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1845 case BPF_PROG_TYPE_CGROUP_SKB:
1846 return prog->enforce_expected_attach_type &&
1847 prog->expected_attach_type != attach_type ?
1854 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1856 #define BPF_F_ATTACH_MASK \
1857 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1859 static int bpf_prog_attach(const union bpf_attr *attr)
1861 enum bpf_prog_type ptype;
1862 struct bpf_prog *prog;
1865 if (!capable(CAP_NET_ADMIN))
1868 if (CHECK_ATTR(BPF_PROG_ATTACH))
1871 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
1874 switch (attr->attach_type) {
1875 case BPF_CGROUP_INET_INGRESS:
1876 case BPF_CGROUP_INET_EGRESS:
1877 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1879 case BPF_CGROUP_INET_SOCK_CREATE:
1880 case BPF_CGROUP_INET4_POST_BIND:
1881 case BPF_CGROUP_INET6_POST_BIND:
1882 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1884 case BPF_CGROUP_INET4_BIND:
1885 case BPF_CGROUP_INET6_BIND:
1886 case BPF_CGROUP_INET4_CONNECT:
1887 case BPF_CGROUP_INET6_CONNECT:
1888 case BPF_CGROUP_UDP4_SENDMSG:
1889 case BPF_CGROUP_UDP6_SENDMSG:
1890 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1892 case BPF_CGROUP_SOCK_OPS:
1893 ptype = BPF_PROG_TYPE_SOCK_OPS;
1895 case BPF_CGROUP_DEVICE:
1896 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1898 case BPF_SK_MSG_VERDICT:
1899 ptype = BPF_PROG_TYPE_SK_MSG;
1901 case BPF_SK_SKB_STREAM_PARSER:
1902 case BPF_SK_SKB_STREAM_VERDICT:
1903 ptype = BPF_PROG_TYPE_SK_SKB;
1905 case BPF_LIRC_MODE2:
1906 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1908 case BPF_FLOW_DISSECTOR:
1909 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1911 case BPF_CGROUP_SYSCTL:
1912 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1918 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1920 return PTR_ERR(prog);
1922 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1928 case BPF_PROG_TYPE_SK_SKB:
1929 case BPF_PROG_TYPE_SK_MSG:
1930 ret = sock_map_get_from_fd(attr, prog);
1932 case BPF_PROG_TYPE_LIRC_MODE2:
1933 ret = lirc_prog_attach(attr, prog);
1935 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1936 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1939 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
1947 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1949 static int bpf_prog_detach(const union bpf_attr *attr)
1951 enum bpf_prog_type ptype;
1953 if (!capable(CAP_NET_ADMIN))
1956 if (CHECK_ATTR(BPF_PROG_DETACH))
1959 switch (attr->attach_type) {
1960 case BPF_CGROUP_INET_INGRESS:
1961 case BPF_CGROUP_INET_EGRESS:
1962 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1964 case BPF_CGROUP_INET_SOCK_CREATE:
1965 case BPF_CGROUP_INET4_POST_BIND:
1966 case BPF_CGROUP_INET6_POST_BIND:
1967 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1969 case BPF_CGROUP_INET4_BIND:
1970 case BPF_CGROUP_INET6_BIND:
1971 case BPF_CGROUP_INET4_CONNECT:
1972 case BPF_CGROUP_INET6_CONNECT:
1973 case BPF_CGROUP_UDP4_SENDMSG:
1974 case BPF_CGROUP_UDP6_SENDMSG:
1975 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1977 case BPF_CGROUP_SOCK_OPS:
1978 ptype = BPF_PROG_TYPE_SOCK_OPS;
1980 case BPF_CGROUP_DEVICE:
1981 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1983 case BPF_SK_MSG_VERDICT:
1984 return sock_map_get_from_fd(attr, NULL);
1985 case BPF_SK_SKB_STREAM_PARSER:
1986 case BPF_SK_SKB_STREAM_VERDICT:
1987 return sock_map_get_from_fd(attr, NULL);
1988 case BPF_LIRC_MODE2:
1989 return lirc_prog_detach(attr);
1990 case BPF_FLOW_DISSECTOR:
1991 return skb_flow_dissector_bpf_prog_detach(attr);
1992 case BPF_CGROUP_SYSCTL:
1993 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1999 return cgroup_bpf_prog_detach(attr, ptype);
2002 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2004 static int bpf_prog_query(const union bpf_attr *attr,
2005 union bpf_attr __user *uattr)
2007 if (!capable(CAP_NET_ADMIN))
2009 if (CHECK_ATTR(BPF_PROG_QUERY))
2011 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2014 switch (attr->query.attach_type) {
2015 case BPF_CGROUP_INET_INGRESS:
2016 case BPF_CGROUP_INET_EGRESS:
2017 case BPF_CGROUP_INET_SOCK_CREATE:
2018 case BPF_CGROUP_INET4_BIND:
2019 case BPF_CGROUP_INET6_BIND:
2020 case BPF_CGROUP_INET4_POST_BIND:
2021 case BPF_CGROUP_INET6_POST_BIND:
2022 case BPF_CGROUP_INET4_CONNECT:
2023 case BPF_CGROUP_INET6_CONNECT:
2024 case BPF_CGROUP_UDP4_SENDMSG:
2025 case BPF_CGROUP_UDP6_SENDMSG:
2026 case BPF_CGROUP_SOCK_OPS:
2027 case BPF_CGROUP_DEVICE:
2028 case BPF_CGROUP_SYSCTL:
2030 case BPF_LIRC_MODE2:
2031 return lirc_prog_query(attr, uattr);
2032 case BPF_FLOW_DISSECTOR:
2033 return skb_flow_dissector_prog_query(attr, uattr);
2038 return cgroup_bpf_prog_query(attr, uattr);
2041 #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
2043 static int bpf_prog_test_run(const union bpf_attr *attr,
2044 union bpf_attr __user *uattr)
2046 struct bpf_prog *prog;
2047 int ret = -ENOTSUPP;
2049 if (!capable(CAP_SYS_ADMIN))
2051 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2054 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2055 (!attr->test.ctx_size_in && attr->test.ctx_in))
2058 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2059 (!attr->test.ctx_size_out && attr->test.ctx_out))
2062 prog = bpf_prog_get(attr->test.prog_fd);
2064 return PTR_ERR(prog);
2066 if (prog->aux->ops->test_run)
2067 ret = prog->aux->ops->test_run(prog, attr, uattr);
2073 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2075 static int bpf_obj_get_next_id(const union bpf_attr *attr,
2076 union bpf_attr __user *uattr,
2080 u32 next_id = attr->start_id;
2083 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2086 if (!capable(CAP_SYS_ADMIN))
2091 if (!idr_get_next(idr, &next_id))
2093 spin_unlock_bh(lock);
2096 err = put_user(next_id, &uattr->next_id);
2101 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2103 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2105 struct bpf_prog *prog;
2106 u32 id = attr->prog_id;
2109 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2112 if (!capable(CAP_SYS_ADMIN))
2115 spin_lock_bh(&prog_idr_lock);
2116 prog = idr_find(&prog_idr, id);
2118 prog = bpf_prog_inc_not_zero(prog);
2120 prog = ERR_PTR(-ENOENT);
2121 spin_unlock_bh(&prog_idr_lock);
2124 return PTR_ERR(prog);
2126 fd = bpf_prog_new_fd(prog);
2133 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2135 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2137 struct bpf_map *map;
2138 u32 id = attr->map_id;
2142 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2143 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
2146 if (!capable(CAP_SYS_ADMIN))
2149 f_flags = bpf_get_file_flag(attr->open_flags);
2153 spin_lock_bh(&map_idr_lock);
2154 map = idr_find(&map_idr, id);
2156 map = bpf_map_inc_not_zero(map, true);
2158 map = ERR_PTR(-ENOENT);
2159 spin_unlock_bh(&map_idr_lock);
2162 return PTR_ERR(map);
2164 fd = bpf_map_new_fd(map, f_flags);
2166 bpf_map_put_with_uref(map);
2171 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
2172 unsigned long addr, u32 *off,
2175 const struct bpf_map *map;
2178 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2179 map = prog->aux->used_maps[i];
2180 if (map == (void *)addr) {
2181 *type = BPF_PSEUDO_MAP_FD;
2184 if (!map->ops->map_direct_value_meta)
2186 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2187 *type = BPF_PSEUDO_MAP_VALUE;
2195 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2197 const struct bpf_map *map;
2198 struct bpf_insn *insns;
2203 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2208 for (i = 0; i < prog->len; i++) {
2209 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2210 insns[i].code = BPF_JMP | BPF_CALL;
2211 insns[i].imm = BPF_FUNC_tail_call;
2214 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2215 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2216 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2217 insns[i].code = BPF_JMP | BPF_CALL;
2218 if (!bpf_dump_raw_ok())
2223 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2226 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
2227 map = bpf_map_from_imm(prog, imm, &off, &type);
2229 insns[i].src_reg = type;
2230 insns[i].imm = map->id;
2231 insns[i + 1].imm = off;
2239 static int set_info_rec_size(struct bpf_prog_info *info)
2242 * Ensure info.*_rec_size is the same as kernel expected size
2246 * Only allow zero *_rec_size if both _rec_size and _cnt are
2247 * zero. In this case, the kernel will set the expected
2248 * _rec_size back to the info.
2251 if ((info->nr_func_info || info->func_info_rec_size) &&
2252 info->func_info_rec_size != sizeof(struct bpf_func_info))
2255 if ((info->nr_line_info || info->line_info_rec_size) &&
2256 info->line_info_rec_size != sizeof(struct bpf_line_info))
2259 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
2260 info->jited_line_info_rec_size != sizeof(__u64))
2263 info->func_info_rec_size = sizeof(struct bpf_func_info);
2264 info->line_info_rec_size = sizeof(struct bpf_line_info);
2265 info->jited_line_info_rec_size = sizeof(__u64);
2270 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2271 const union bpf_attr *attr,
2272 union bpf_attr __user *uattr)
2274 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2275 struct bpf_prog_info info = {};
2276 u32 info_len = attr->info.info_len;
2277 struct bpf_prog_stats stats;
2278 char __user *uinsns;
2282 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2285 info_len = min_t(u32, sizeof(info), info_len);
2287 if (copy_from_user(&info, uinfo, info_len))
2290 info.type = prog->type;
2291 info.id = prog->aux->id;
2292 info.load_time = prog->aux->load_time;
2293 info.created_by_uid = from_kuid_munged(current_user_ns(),
2294 prog->aux->user->uid);
2295 info.gpl_compatible = prog->gpl_compatible;
2297 memcpy(info.tag, prog->tag, sizeof(prog->tag));
2298 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2300 ulen = info.nr_map_ids;
2301 info.nr_map_ids = prog->aux->used_map_cnt;
2302 ulen = min_t(u32, info.nr_map_ids, ulen);
2304 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
2307 for (i = 0; i < ulen; i++)
2308 if (put_user(prog->aux->used_maps[i]->id,
2313 err = set_info_rec_size(&info);
2317 bpf_prog_get_stats(prog, &stats);
2318 info.run_time_ns = stats.nsecs;
2319 info.run_cnt = stats.cnt;
2321 if (!capable(CAP_SYS_ADMIN)) {
2322 info.jited_prog_len = 0;
2323 info.xlated_prog_len = 0;
2324 info.nr_jited_ksyms = 0;
2325 info.nr_jited_func_lens = 0;
2326 info.nr_func_info = 0;
2327 info.nr_line_info = 0;
2328 info.nr_jited_line_info = 0;
2332 ulen = info.xlated_prog_len;
2333 info.xlated_prog_len = bpf_prog_insn_size(prog);
2334 if (info.xlated_prog_len && ulen) {
2335 struct bpf_insn *insns_sanitized;
2338 if (prog->blinded && !bpf_dump_raw_ok()) {
2339 info.xlated_prog_insns = 0;
2342 insns_sanitized = bpf_insn_prepare_dump(prog);
2343 if (!insns_sanitized)
2345 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2346 ulen = min_t(u32, info.xlated_prog_len, ulen);
2347 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2348 kfree(insns_sanitized);
2353 if (bpf_prog_is_dev_bound(prog->aux)) {
2354 err = bpf_prog_offload_info_fill(&info, prog);
2360 /* NOTE: the following code is supposed to be skipped for offload.
2361 * bpf_prog_offload_info_fill() is the place to fill similar fields
2364 ulen = info.jited_prog_len;
2365 if (prog->aux->func_cnt) {
2368 info.jited_prog_len = 0;
2369 for (i = 0; i < prog->aux->func_cnt; i++)
2370 info.jited_prog_len += prog->aux->func[i]->jited_len;
2372 info.jited_prog_len = prog->jited_len;
2375 if (info.jited_prog_len && ulen) {
2376 if (bpf_dump_raw_ok()) {
2377 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2378 ulen = min_t(u32, info.jited_prog_len, ulen);
2380 /* for multi-function programs, copy the JITed
2381 * instructions for all the functions
2383 if (prog->aux->func_cnt) {
2388 for (i = 0; i < prog->aux->func_cnt; i++) {
2389 len = prog->aux->func[i]->jited_len;
2390 len = min_t(u32, len, free);
2391 img = (u8 *) prog->aux->func[i]->bpf_func;
2392 if (copy_to_user(uinsns, img, len))
2400 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2404 info.jited_prog_insns = 0;
2408 ulen = info.nr_jited_ksyms;
2409 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
2411 if (bpf_dump_raw_ok()) {
2412 unsigned long ksym_addr;
2413 u64 __user *user_ksyms;
2416 /* copy the address of the kernel symbol
2417 * corresponding to each function
2419 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2420 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2421 if (prog->aux->func_cnt) {
2422 for (i = 0; i < ulen; i++) {
2423 ksym_addr = (unsigned long)
2424 prog->aux->func[i]->bpf_func;
2425 if (put_user((u64) ksym_addr,
2430 ksym_addr = (unsigned long) prog->bpf_func;
2431 if (put_user((u64) ksym_addr, &user_ksyms[0]))
2435 info.jited_ksyms = 0;
2439 ulen = info.nr_jited_func_lens;
2440 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
2442 if (bpf_dump_raw_ok()) {
2443 u32 __user *user_lens;
2446 /* copy the JITed image lengths for each function */
2447 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2448 user_lens = u64_to_user_ptr(info.jited_func_lens);
2449 if (prog->aux->func_cnt) {
2450 for (i = 0; i < ulen; i++) {
2452 prog->aux->func[i]->jited_len;
2453 if (put_user(func_len, &user_lens[i]))
2457 func_len = prog->jited_len;
2458 if (put_user(func_len, &user_lens[0]))
2462 info.jited_func_lens = 0;
2467 info.btf_id = btf_id(prog->aux->btf);
2469 ulen = info.nr_func_info;
2470 info.nr_func_info = prog->aux->func_info_cnt;
2471 if (info.nr_func_info && ulen) {
2472 char __user *user_finfo;
2474 user_finfo = u64_to_user_ptr(info.func_info);
2475 ulen = min_t(u32, info.nr_func_info, ulen);
2476 if (copy_to_user(user_finfo, prog->aux->func_info,
2477 info.func_info_rec_size * ulen))
2481 ulen = info.nr_line_info;
2482 info.nr_line_info = prog->aux->nr_linfo;
2483 if (info.nr_line_info && ulen) {
2484 __u8 __user *user_linfo;
2486 user_linfo = u64_to_user_ptr(info.line_info);
2487 ulen = min_t(u32, info.nr_line_info, ulen);
2488 if (copy_to_user(user_linfo, prog->aux->linfo,
2489 info.line_info_rec_size * ulen))
2493 ulen = info.nr_jited_line_info;
2494 if (prog->aux->jited_linfo)
2495 info.nr_jited_line_info = prog->aux->nr_linfo;
2497 info.nr_jited_line_info = 0;
2498 if (info.nr_jited_line_info && ulen) {
2499 if (bpf_dump_raw_ok()) {
2500 __u64 __user *user_linfo;
2503 user_linfo = u64_to_user_ptr(info.jited_line_info);
2504 ulen = min_t(u32, info.nr_jited_line_info, ulen);
2505 for (i = 0; i < ulen; i++) {
2506 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2511 info.jited_line_info = 0;
2515 ulen = info.nr_prog_tags;
2516 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2518 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2521 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2522 ulen = min_t(u32, info.nr_prog_tags, ulen);
2523 if (prog->aux->func_cnt) {
2524 for (i = 0; i < ulen; i++) {
2525 if (copy_to_user(user_prog_tags[i],
2526 prog->aux->func[i]->tag,
2531 if (copy_to_user(user_prog_tags[0],
2532 prog->tag, BPF_TAG_SIZE))
2538 if (copy_to_user(uinfo, &info, info_len) ||
2539 put_user(info_len, &uattr->info.info_len))
2545 static int bpf_map_get_info_by_fd(struct bpf_map *map,
2546 const union bpf_attr *attr,
2547 union bpf_attr __user *uattr)
2549 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2550 struct bpf_map_info info = {};
2551 u32 info_len = attr->info.info_len;
2554 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2557 info_len = min_t(u32, sizeof(info), info_len);
2559 info.type = map->map_type;
2561 info.key_size = map->key_size;
2562 info.value_size = map->value_size;
2563 info.max_entries = map->max_entries;
2564 info.map_flags = map->map_flags;
2565 memcpy(info.name, map->name, sizeof(map->name));
2568 info.btf_id = btf_id(map->btf);
2569 info.btf_key_type_id = map->btf_key_type_id;
2570 info.btf_value_type_id = map->btf_value_type_id;
2573 if (bpf_map_is_dev_bound(map)) {
2574 err = bpf_map_offload_info_fill(&info, map);
2579 if (copy_to_user(uinfo, &info, info_len) ||
2580 put_user(info_len, &uattr->info.info_len))
2586 static int bpf_btf_get_info_by_fd(struct btf *btf,
2587 const union bpf_attr *attr,
2588 union bpf_attr __user *uattr)
2590 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2591 u32 info_len = attr->info.info_len;
2594 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
2598 return btf_get_info_by_fd(btf, attr, uattr);
2601 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2603 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2604 union bpf_attr __user *uattr)
2606 int ufd = attr->info.bpf_fd;
2610 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2617 if (f.file->f_op == &bpf_prog_fops)
2618 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2620 else if (f.file->f_op == &bpf_map_fops)
2621 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2623 else if (f.file->f_op == &btf_fops)
2624 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
2632 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2634 static int bpf_btf_load(const union bpf_attr *attr)
2636 if (CHECK_ATTR(BPF_BTF_LOAD))
2639 if (!capable(CAP_SYS_ADMIN))
2642 return btf_new_fd(attr);
2645 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2647 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2649 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2652 if (!capable(CAP_SYS_ADMIN))
2655 return btf_get_fd_by_id(attr->btf_id);
2658 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2659 union bpf_attr __user *uattr,
2660 u32 prog_id, u32 fd_type,
2661 const char *buf, u64 probe_offset,
2664 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2665 u32 len = buf ? strlen(buf) : 0, input_len;
2668 if (put_user(len, &uattr->task_fd_query.buf_len))
2670 input_len = attr->task_fd_query.buf_len;
2671 if (input_len && ubuf) {
2673 /* nothing to copy, just make ubuf NULL terminated */
2676 if (put_user(zero, ubuf))
2678 } else if (input_len >= len + 1) {
2679 /* ubuf can hold the string with NULL terminator */
2680 if (copy_to_user(ubuf, buf, len + 1))
2683 /* ubuf cannot hold the string with NULL terminator,
2684 * do a partial copy with NULL terminator.
2689 if (copy_to_user(ubuf, buf, input_len - 1))
2691 if (put_user(zero, ubuf + input_len - 1))
2696 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2697 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2698 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2699 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2705 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2707 static int bpf_task_fd_query(const union bpf_attr *attr,
2708 union bpf_attr __user *uattr)
2710 pid_t pid = attr->task_fd_query.pid;
2711 u32 fd = attr->task_fd_query.fd;
2712 const struct perf_event *event;
2713 struct files_struct *files;
2714 struct task_struct *task;
2718 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2721 if (!capable(CAP_SYS_ADMIN))
2724 if (attr->task_fd_query.flags != 0)
2727 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2731 files = get_files_struct(task);
2732 put_task_struct(task);
2737 spin_lock(&files->file_lock);
2738 file = fcheck_files(files, fd);
2743 spin_unlock(&files->file_lock);
2744 put_files_struct(files);
2749 if (file->f_op == &bpf_raw_tp_fops) {
2750 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2751 struct bpf_raw_event_map *btp = raw_tp->btp;
2753 err = bpf_task_fd_query_copy(attr, uattr,
2754 raw_tp->prog->aux->id,
2755 BPF_FD_TYPE_RAW_TRACEPOINT,
2756 btp->tp->name, 0, 0);
2760 event = perf_get_event(file);
2761 if (!IS_ERR(event)) {
2762 u64 probe_offset, probe_addr;
2763 u32 prog_id, fd_type;
2766 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2767 &buf, &probe_offset,
2770 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2784 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2786 union bpf_attr attr = {};
2789 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
2792 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
2795 size = min_t(u32, size, sizeof(attr));
2797 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2798 if (copy_from_user(&attr, uattr, size) != 0)
2801 err = security_bpf(cmd, &attr, size);
2806 case BPF_MAP_CREATE:
2807 err = map_create(&attr);
2809 case BPF_MAP_LOOKUP_ELEM:
2810 err = map_lookup_elem(&attr);
2812 case BPF_MAP_UPDATE_ELEM:
2813 err = map_update_elem(&attr);
2815 case BPF_MAP_DELETE_ELEM:
2816 err = map_delete_elem(&attr);
2818 case BPF_MAP_GET_NEXT_KEY:
2819 err = map_get_next_key(&attr);
2821 case BPF_MAP_FREEZE:
2822 err = map_freeze(&attr);
2825 err = bpf_prog_load(&attr, uattr);
2828 err = bpf_obj_pin(&attr);
2831 err = bpf_obj_get(&attr);
2833 case BPF_PROG_ATTACH:
2834 err = bpf_prog_attach(&attr);
2836 case BPF_PROG_DETACH:
2837 err = bpf_prog_detach(&attr);
2839 case BPF_PROG_QUERY:
2840 err = bpf_prog_query(&attr, uattr);
2842 case BPF_PROG_TEST_RUN:
2843 err = bpf_prog_test_run(&attr, uattr);
2845 case BPF_PROG_GET_NEXT_ID:
2846 err = bpf_obj_get_next_id(&attr, uattr,
2847 &prog_idr, &prog_idr_lock);
2849 case BPF_MAP_GET_NEXT_ID:
2850 err = bpf_obj_get_next_id(&attr, uattr,
2851 &map_idr, &map_idr_lock);
2853 case BPF_PROG_GET_FD_BY_ID:
2854 err = bpf_prog_get_fd_by_id(&attr);
2856 case BPF_MAP_GET_FD_BY_ID:
2857 err = bpf_map_get_fd_by_id(&attr);
2859 case BPF_OBJ_GET_INFO_BY_FD:
2860 err = bpf_obj_get_info_by_fd(&attr, uattr);
2862 case BPF_RAW_TRACEPOINT_OPEN:
2863 err = bpf_raw_tracepoint_open(&attr);
2866 err = bpf_btf_load(&attr);
2868 case BPF_BTF_GET_FD_BY_ID:
2869 err = bpf_btf_get_fd_by_id(&attr);
2871 case BPF_TASK_FD_QUERY:
2872 err = bpf_task_fd_query(&attr, uattr);
2874 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2875 err = map_lookup_and_delete_elem(&attr);