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/btf.h>
34 #include <linux/nospec.h>
36 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
38 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
39 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
40 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
41 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
43 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
45 DEFINE_PER_CPU(int, bpf_prog_active);
46 static DEFINE_IDR(prog_idr);
47 static DEFINE_SPINLOCK(prog_idr_lock);
48 static DEFINE_IDR(map_idr);
49 static DEFINE_SPINLOCK(map_idr_lock);
51 int sysctl_unprivileged_bpf_disabled __read_mostly;
53 static const struct bpf_map_ops * const bpf_map_types[] = {
54 #define BPF_PROG_TYPE(_id, _ops)
55 #define BPF_MAP_TYPE(_id, _ops) \
57 #include <linux/bpf_types.h>
63 * If we're handed a bigger struct than we know of, ensure all the unknown bits
64 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
65 * we don't know about yet.
67 * There is a ToCToU between this function call and the following
68 * copy_from_user() call. However, this is not a concern since this function is
69 * meant to be a future-proofing of bits.
71 int bpf_check_uarg_tail_zero(void __user *uaddr,
75 unsigned char __user *addr;
76 unsigned char __user *end;
80 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
83 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
86 if (actual_size <= expected_size)
89 addr = uaddr + expected_size;
90 end = uaddr + actual_size;
92 for (; addr < end; addr++) {
93 err = get_user(val, addr);
103 const struct bpf_map_ops bpf_map_offload_ops = {
104 .map_alloc = bpf_map_offload_map_alloc,
105 .map_free = bpf_map_offload_map_free,
106 .map_check_btf = map_check_no_btf,
109 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
111 const struct bpf_map_ops *ops;
112 u32 type = attr->map_type;
116 if (type >= ARRAY_SIZE(bpf_map_types))
117 return ERR_PTR(-EINVAL);
118 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
119 ops = bpf_map_types[type];
121 return ERR_PTR(-EINVAL);
123 if (ops->map_alloc_check) {
124 err = ops->map_alloc_check(attr);
128 if (attr->map_ifindex)
129 ops = &bpf_map_offload_ops;
130 map = ops->map_alloc(attr);
134 map->map_type = type;
138 void *bpf_map_area_alloc(size_t size, int numa_node)
140 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
141 * trigger under memory pressure as we really just want to
144 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
147 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
148 area = kmalloc_node(size, GFP_USER | flags, numa_node);
153 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
154 __builtin_return_address(0));
157 void bpf_map_area_free(void *area)
162 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
164 map->map_type = attr->map_type;
165 map->key_size = attr->key_size;
166 map->value_size = attr->value_size;
167 map->max_entries = attr->max_entries;
168 map->map_flags = attr->map_flags;
169 map->numa_node = bpf_map_attr_numa_node(attr);
172 int bpf_map_precharge_memlock(u32 pages)
174 struct user_struct *user = get_current_user();
175 unsigned long memlock_limit, cur;
177 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
178 cur = atomic_long_read(&user->locked_vm);
180 if (cur + pages > memlock_limit)
185 static int bpf_charge_memlock(struct user_struct *user, u32 pages)
187 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
189 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
190 atomic_long_sub(pages, &user->locked_vm);
196 static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
198 atomic_long_sub(pages, &user->locked_vm);
201 static int bpf_map_init_memlock(struct bpf_map *map)
203 struct user_struct *user = get_current_user();
206 ret = bpf_charge_memlock(user, map->pages);
215 static void bpf_map_release_memlock(struct bpf_map *map)
217 struct user_struct *user = map->user;
218 bpf_uncharge_memlock(user, map->pages);
222 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
226 ret = bpf_charge_memlock(map->user, pages);
233 void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
235 bpf_uncharge_memlock(map->user, pages);
239 static int bpf_map_alloc_id(struct bpf_map *map)
243 idr_preload(GFP_KERNEL);
244 spin_lock_bh(&map_idr_lock);
245 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
248 spin_unlock_bh(&map_idr_lock);
251 if (WARN_ON_ONCE(!id))
254 return id > 0 ? 0 : id;
257 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
261 /* Offloaded maps are removed from the IDR store when their device
262 * disappears - even if someone holds an fd to them they are unusable,
263 * the memory is gone, all ops will fail; they are simply waiting for
264 * refcnt to drop to be freed.
270 spin_lock_irqsave(&map_idr_lock, flags);
272 __acquire(&map_idr_lock);
274 idr_remove(&map_idr, map->id);
278 spin_unlock_irqrestore(&map_idr_lock, flags);
280 __release(&map_idr_lock);
283 /* called from workqueue */
284 static void bpf_map_free_deferred(struct work_struct *work)
286 struct bpf_map *map = container_of(work, struct bpf_map, work);
288 bpf_map_release_memlock(map);
289 security_bpf_map_free(map);
290 /* implementation dependent freeing */
291 map->ops->map_free(map);
294 static void bpf_map_put_uref(struct bpf_map *map)
296 if (atomic_dec_and_test(&map->usercnt)) {
297 if (map->ops->map_release_uref)
298 map->ops->map_release_uref(map);
302 /* decrement map refcnt and schedule it for freeing via workqueue
303 * (unrelying map implementation ops->map_free() might sleep)
305 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
307 if (atomic_dec_and_test(&map->refcnt)) {
308 /* bpf_map_free_id() must be called first */
309 bpf_map_free_id(map, do_idr_lock);
311 INIT_WORK(&map->work, bpf_map_free_deferred);
312 schedule_work(&map->work);
316 void bpf_map_put(struct bpf_map *map)
318 __bpf_map_put(map, true);
320 EXPORT_SYMBOL_GPL(bpf_map_put);
322 void bpf_map_put_with_uref(struct bpf_map *map)
324 bpf_map_put_uref(map);
328 static int bpf_map_release(struct inode *inode, struct file *filp)
330 struct bpf_map *map = filp->private_data;
332 if (map->ops->map_release)
333 map->ops->map_release(map, filp);
335 bpf_map_put_with_uref(map);
339 #ifdef CONFIG_PROC_FS
340 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
342 const struct bpf_map *map = filp->private_data;
343 const struct bpf_array *array;
344 u32 owner_prog_type = 0;
347 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
348 array = container_of(map, struct bpf_array, map);
349 owner_prog_type = array->owner_prog_type;
350 owner_jited = array->owner_jited;
366 map->pages * 1ULL << PAGE_SHIFT,
369 if (owner_prog_type) {
370 seq_printf(m, "owner_prog_type:\t%u\n",
372 seq_printf(m, "owner_jited:\t%u\n",
378 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
381 /* We need this handler such that alloc_file() enables
382 * f_mode with FMODE_CAN_READ.
387 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
388 size_t siz, loff_t *ppos)
390 /* We need this handler such that alloc_file() enables
391 * f_mode with FMODE_CAN_WRITE.
396 const struct file_operations bpf_map_fops = {
397 #ifdef CONFIG_PROC_FS
398 .show_fdinfo = bpf_map_show_fdinfo,
400 .release = bpf_map_release,
401 .read = bpf_dummy_read,
402 .write = bpf_dummy_write,
405 int bpf_map_new_fd(struct bpf_map *map, int flags)
409 ret = security_bpf_map(map, OPEN_FMODE(flags));
413 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
417 int bpf_get_file_flag(int flags)
419 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
421 if (flags & BPF_F_RDONLY)
423 if (flags & BPF_F_WRONLY)
428 /* helper macro to check that unused fields 'union bpf_attr' are zero */
429 #define CHECK_ATTR(CMD) \
430 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
431 sizeof(attr->CMD##_LAST_FIELD), 0, \
433 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
434 sizeof(attr->CMD##_LAST_FIELD)) != NULL
436 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
437 * Return 0 on success and < 0 on error.
439 static int bpf_obj_name_cpy(char *dst, const char *src)
441 const char *end = src + BPF_OBJ_NAME_LEN;
443 memset(dst, 0, BPF_OBJ_NAME_LEN);
445 /* Copy all isalnum() and '_' char */
446 while (src < end && *src) {
447 if (!isalnum(*src) && *src != '_')
452 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
459 int map_check_no_btf(const struct bpf_map *map,
460 const struct btf_type *key_type,
461 const struct btf_type *value_type)
466 static int map_check_btf(const struct bpf_map *map, const struct btf *btf,
467 u32 btf_key_id, u32 btf_value_id)
469 const struct btf_type *key_type, *value_type;
470 u32 key_size, value_size;
473 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
474 if (!key_type || key_size != map->key_size)
477 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
478 if (!value_type || value_size != map->value_size)
481 if (map->ops->map_check_btf)
482 ret = map->ops->map_check_btf(map, key_type, value_type);
487 #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
488 /* called via syscall */
489 static int map_create(union bpf_attr *attr)
491 int numa_node = bpf_map_attr_numa_node(attr);
496 err = CHECK_ATTR(BPF_MAP_CREATE);
500 f_flags = bpf_get_file_flag(attr->map_flags);
504 if (numa_node != NUMA_NO_NODE &&
505 ((unsigned int)numa_node >= nr_node_ids ||
506 !node_online(numa_node)))
509 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
510 map = find_and_alloc_map(attr);
514 err = bpf_obj_name_cpy(map->name, attr->map_name);
516 goto free_map_nouncharge;
518 atomic_set(&map->refcnt, 1);
519 atomic_set(&map->usercnt, 1);
521 if (attr->btf_key_type_id || attr->btf_value_type_id) {
524 if (!attr->btf_key_type_id || !attr->btf_value_type_id) {
526 goto free_map_nouncharge;
529 btf = btf_get_by_fd(attr->btf_fd);
532 goto free_map_nouncharge;
535 err = map_check_btf(map, btf, attr->btf_key_type_id,
536 attr->btf_value_type_id);
539 goto free_map_nouncharge;
543 map->btf_key_type_id = attr->btf_key_type_id;
544 map->btf_value_type_id = attr->btf_value_type_id;
547 err = security_bpf_map_alloc(map);
549 goto free_map_nouncharge;
551 err = bpf_map_init_memlock(map);
555 err = bpf_map_alloc_id(map);
559 err = bpf_map_new_fd(map, f_flags);
561 /* failed to allocate fd.
562 * bpf_map_put() is needed because the above
563 * bpf_map_alloc_id() has published the map
564 * to the userspace and the userspace may
565 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
574 bpf_map_release_memlock(map);
576 security_bpf_map_free(map);
579 map->ops->map_free(map);
583 /* if error is returned, fd is released.
584 * On success caller should complete fd access with matching fdput()
586 struct bpf_map *__bpf_map_get(struct fd f)
589 return ERR_PTR(-EBADF);
590 if (f.file->f_op != &bpf_map_fops) {
592 return ERR_PTR(-EINVAL);
595 return f.file->private_data;
598 /* prog's and map's refcnt limit */
599 #define BPF_MAX_REFCNT 32768
601 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
603 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
604 atomic_dec(&map->refcnt);
605 return ERR_PTR(-EBUSY);
608 atomic_inc(&map->usercnt);
611 EXPORT_SYMBOL_GPL(bpf_map_inc);
613 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
615 struct fd f = fdget(ufd);
618 map = __bpf_map_get(f);
622 map = bpf_map_inc(map, true);
628 /* map_idr_lock should have been held */
629 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
634 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
636 if (refold >= BPF_MAX_REFCNT) {
637 __bpf_map_put(map, false);
638 return ERR_PTR(-EBUSY);
642 return ERR_PTR(-ENOENT);
645 atomic_inc(&map->usercnt);
650 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
655 /* last field in 'union bpf_attr' used by this command */
656 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
658 static int map_lookup_elem(union bpf_attr *attr)
660 void __user *ukey = u64_to_user_ptr(attr->key);
661 void __user *uvalue = u64_to_user_ptr(attr->value);
662 int ufd = attr->map_fd;
664 void *key, *value, *ptr;
669 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
673 map = __bpf_map_get(f);
677 if (!(f.file->f_mode & FMODE_CAN_READ)) {
682 key = memdup_user(ukey, map->key_size);
688 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
689 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
690 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
691 value_size = round_up(map->value_size, 8) * num_possible_cpus();
692 else if (IS_FD_MAP(map))
693 value_size = sizeof(u32);
695 value_size = map->value_size;
698 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
702 if (bpf_map_is_dev_bound(map)) {
703 err = bpf_map_offload_lookup_elem(map, key, value);
708 this_cpu_inc(bpf_prog_active);
709 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
710 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
711 err = bpf_percpu_hash_copy(map, key, value);
712 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
713 err = bpf_percpu_array_copy(map, key, value);
714 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
715 err = bpf_stackmap_copy(map, key, value);
716 } else if (IS_FD_ARRAY(map)) {
717 err = bpf_fd_array_map_lookup_elem(map, key, value);
718 } else if (IS_FD_HASH(map)) {
719 err = bpf_fd_htab_map_lookup_elem(map, key, value);
720 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
721 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
724 if (map->ops->map_lookup_elem_sys_only)
725 ptr = map->ops->map_lookup_elem_sys_only(map, key);
727 ptr = map->ops->map_lookup_elem(map, key);
729 memcpy(value, ptr, value_size);
731 err = ptr ? 0 : -ENOENT;
733 this_cpu_dec(bpf_prog_active);
741 if (copy_to_user(uvalue, value, value_size) != 0)
755 static void maybe_wait_bpf_programs(struct bpf_map *map)
757 /* Wait for any running BPF programs to complete so that
758 * userspace, when we return to it, knows that all programs
759 * that could be running use the new map value.
761 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
762 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
766 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
768 static int map_update_elem(union bpf_attr *attr)
770 void __user *ukey = u64_to_user_ptr(attr->key);
771 void __user *uvalue = u64_to_user_ptr(attr->value);
772 int ufd = attr->map_fd;
779 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
783 map = __bpf_map_get(f);
787 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
792 key = memdup_user(ukey, map->key_size);
798 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
799 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
800 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
801 value_size = round_up(map->value_size, 8) * num_possible_cpus();
803 value_size = map->value_size;
806 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
811 if (copy_from_user(value, uvalue, value_size) != 0)
814 /* Need to create a kthread, thus must support schedule */
815 if (bpf_map_is_dev_bound(map)) {
816 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
818 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
819 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
820 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
821 err = map->ops->map_update_elem(map, key, value, attr->flags);
825 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
826 * inside bpf map update or delete otherwise deadlocks are possible
829 __this_cpu_inc(bpf_prog_active);
830 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
831 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
832 err = bpf_percpu_hash_update(map, key, value, attr->flags);
833 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
834 err = bpf_percpu_array_update(map, key, value, attr->flags);
835 } else if (IS_FD_ARRAY(map)) {
837 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
840 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
842 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
845 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
846 /* rcu_read_lock() is not needed */
847 err = bpf_fd_reuseport_array_update_elem(map, key, value,
851 err = map->ops->map_update_elem(map, key, value, attr->flags);
854 __this_cpu_dec(bpf_prog_active);
856 maybe_wait_bpf_programs(map);
867 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
869 static int map_delete_elem(union bpf_attr *attr)
871 void __user *ukey = u64_to_user_ptr(attr->key);
872 int ufd = attr->map_fd;
878 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
882 map = __bpf_map_get(f);
886 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
891 key = memdup_user(ukey, map->key_size);
897 if (bpf_map_is_dev_bound(map)) {
898 err = bpf_map_offload_delete_elem(map, key);
903 __this_cpu_inc(bpf_prog_active);
905 err = map->ops->map_delete_elem(map, key);
907 __this_cpu_dec(bpf_prog_active);
909 maybe_wait_bpf_programs(map);
917 /* last field in 'union bpf_attr' used by this command */
918 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
920 static int map_get_next_key(union bpf_attr *attr)
922 void __user *ukey = u64_to_user_ptr(attr->key);
923 void __user *unext_key = u64_to_user_ptr(attr->next_key);
924 int ufd = attr->map_fd;
926 void *key, *next_key;
930 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
934 map = __bpf_map_get(f);
938 if (!(f.file->f_mode & FMODE_CAN_READ)) {
944 key = memdup_user(ukey, map->key_size);
954 next_key = kmalloc(map->key_size, GFP_USER);
958 if (bpf_map_is_dev_bound(map)) {
959 err = bpf_map_offload_get_next_key(map, key, next_key);
964 err = map->ops->map_get_next_key(map, key, next_key);
971 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
985 static const struct bpf_prog_ops * const bpf_prog_types[] = {
986 #define BPF_PROG_TYPE(_id, _name) \
987 [_id] = & _name ## _prog_ops,
988 #define BPF_MAP_TYPE(_id, _ops)
989 #include <linux/bpf_types.h>
994 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
996 const struct bpf_prog_ops *ops;
998 if (type >= ARRAY_SIZE(bpf_prog_types))
1000 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1001 ops = bpf_prog_types[type];
1005 if (!bpf_prog_is_dev_bound(prog->aux))
1006 prog->aux->ops = ops;
1008 prog->aux->ops = &bpf_offload_prog_ops;
1013 /* drop refcnt on maps used by eBPF program and free auxilary data */
1014 static void free_used_maps(struct bpf_prog_aux *aux)
1018 if (aux->cgroup_storage)
1019 bpf_cgroup_storage_release(aux->prog, aux->cgroup_storage);
1021 for (i = 0; i < aux->used_map_cnt; i++)
1022 bpf_map_put(aux->used_maps[i]);
1024 kfree(aux->used_maps);
1027 int __bpf_prog_charge(struct user_struct *user, u32 pages)
1029 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1030 unsigned long user_bufs;
1033 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1034 if (user_bufs > memlock_limit) {
1035 atomic_long_sub(pages, &user->locked_vm);
1043 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1046 atomic_long_sub(pages, &user->locked_vm);
1049 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1051 struct user_struct *user = get_current_user();
1054 ret = __bpf_prog_charge(user, prog->pages);
1060 prog->aux->user = user;
1064 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1066 struct user_struct *user = prog->aux->user;
1068 __bpf_prog_uncharge(user, prog->pages);
1072 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1076 idr_preload(GFP_KERNEL);
1077 spin_lock_bh(&prog_idr_lock);
1078 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1081 spin_unlock_bh(&prog_idr_lock);
1084 /* id is in [1, INT_MAX) */
1085 if (WARN_ON_ONCE(!id))
1088 return id > 0 ? 0 : id;
1091 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1093 /* cBPF to eBPF migrations are currently not in the idr store.
1094 * Offloaded programs are removed from the store when their device
1095 * disappears - even if someone grabs an fd to them they are unusable,
1096 * simply waiting for refcnt to drop to be freed.
1102 spin_lock_bh(&prog_idr_lock);
1104 __acquire(&prog_idr_lock);
1106 idr_remove(&prog_idr, prog->aux->id);
1110 spin_unlock_bh(&prog_idr_lock);
1112 __release(&prog_idr_lock);
1115 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1117 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1119 free_used_maps(aux);
1120 bpf_prog_uncharge_memlock(aux->prog);
1121 security_bpf_prog_free(aux);
1122 bpf_prog_free(aux->prog);
1125 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1127 if (atomic_dec_and_test(&prog->aux->refcnt)) {
1128 /* bpf_prog_free_id() must be called first */
1129 bpf_prog_free_id(prog, do_idr_lock);
1130 bpf_prog_kallsyms_del_all(prog);
1132 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1136 void bpf_prog_put(struct bpf_prog *prog)
1138 __bpf_prog_put(prog, true);
1140 EXPORT_SYMBOL_GPL(bpf_prog_put);
1142 static int bpf_prog_release(struct inode *inode, struct file *filp)
1144 struct bpf_prog *prog = filp->private_data;
1150 #ifdef CONFIG_PROC_FS
1151 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1153 const struct bpf_prog *prog = filp->private_data;
1154 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1156 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1166 prog->pages * 1ULL << PAGE_SHIFT,
1171 const struct file_operations bpf_prog_fops = {
1172 #ifdef CONFIG_PROC_FS
1173 .show_fdinfo = bpf_prog_show_fdinfo,
1175 .release = bpf_prog_release,
1176 .read = bpf_dummy_read,
1177 .write = bpf_dummy_write,
1180 int bpf_prog_new_fd(struct bpf_prog *prog)
1184 ret = security_bpf_prog(prog);
1188 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1189 O_RDWR | O_CLOEXEC);
1192 static struct bpf_prog *____bpf_prog_get(struct fd f)
1195 return ERR_PTR(-EBADF);
1196 if (f.file->f_op != &bpf_prog_fops) {
1198 return ERR_PTR(-EINVAL);
1201 return f.file->private_data;
1204 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
1206 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1207 atomic_sub(i, &prog->aux->refcnt);
1208 return ERR_PTR(-EBUSY);
1212 EXPORT_SYMBOL_GPL(bpf_prog_add);
1214 void bpf_prog_sub(struct bpf_prog *prog, int i)
1216 /* Only to be used for undoing previous bpf_prog_add() in some
1217 * error path. We still know that another entity in our call
1218 * path holds a reference to the program, thus atomic_sub() can
1219 * be safely used in such cases!
1221 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1223 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1225 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1227 return bpf_prog_add(prog, 1);
1229 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1231 /* prog_idr_lock should have been held */
1232 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1236 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1238 if (refold >= BPF_MAX_REFCNT) {
1239 __bpf_prog_put(prog, false);
1240 return ERR_PTR(-EBUSY);
1244 return ERR_PTR(-ENOENT);
1248 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1250 bool bpf_prog_get_ok(struct bpf_prog *prog,
1251 enum bpf_prog_type *attach_type, bool attach_drv)
1253 /* not an attachment, just a refcount inc, always allow */
1257 if (prog->type != *attach_type)
1259 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1265 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1268 struct fd f = fdget(ufd);
1269 struct bpf_prog *prog;
1271 prog = ____bpf_prog_get(f);
1274 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1275 prog = ERR_PTR(-EINVAL);
1279 prog = bpf_prog_inc(prog);
1285 struct bpf_prog *bpf_prog_get(u32 ufd)
1287 return __bpf_prog_get(ufd, NULL, false);
1290 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1293 return __bpf_prog_get(ufd, &type, attach_drv);
1295 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1297 /* Initially all BPF programs could be loaded w/o specifying
1298 * expected_attach_type. Later for some of them specifying expected_attach_type
1299 * at load time became required so that program could be validated properly.
1300 * Programs of types that are allowed to be loaded both w/ and w/o (for
1301 * backward compatibility) expected_attach_type, should have the default attach
1302 * type assigned to expected_attach_type for the latter case, so that it can be
1303 * validated later at attach time.
1305 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1306 * prog type requires it but has some attach types that have to be backward
1309 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1311 switch (attr->prog_type) {
1312 case BPF_PROG_TYPE_CGROUP_SOCK:
1313 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1314 * exist so checking for non-zero is the way to go here.
1316 if (!attr->expected_attach_type)
1317 attr->expected_attach_type =
1318 BPF_CGROUP_INET_SOCK_CREATE;
1324 bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1325 enum bpf_attach_type expected_attach_type)
1327 switch (prog_type) {
1328 case BPF_PROG_TYPE_CGROUP_SOCK:
1329 switch (expected_attach_type) {
1330 case BPF_CGROUP_INET_SOCK_CREATE:
1331 case BPF_CGROUP_INET4_POST_BIND:
1332 case BPF_CGROUP_INET6_POST_BIND:
1337 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1338 switch (expected_attach_type) {
1339 case BPF_CGROUP_INET4_BIND:
1340 case BPF_CGROUP_INET6_BIND:
1341 case BPF_CGROUP_INET4_CONNECT:
1342 case BPF_CGROUP_INET6_CONNECT:
1343 case BPF_CGROUP_UDP4_SENDMSG:
1344 case BPF_CGROUP_UDP6_SENDMSG:
1345 case BPF_CGROUP_UDP4_RECVMSG:
1346 case BPF_CGROUP_UDP6_RECVMSG:
1356 /* last field in 'union bpf_attr' used by this command */
1357 #define BPF_PROG_LOAD_LAST_FIELD expected_attach_type
1359 static int bpf_prog_load(union bpf_attr *attr)
1361 enum bpf_prog_type type = attr->prog_type;
1362 struct bpf_prog *prog;
1367 if (CHECK_ATTR(BPF_PROG_LOAD))
1370 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1373 /* copy eBPF program license from user space */
1374 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1375 sizeof(license) - 1) < 0)
1377 license[sizeof(license) - 1] = 0;
1379 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1380 is_gpl = license_is_gpl_compatible(license);
1382 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1385 if (type == BPF_PROG_TYPE_KPROBE &&
1386 attr->kern_version != LINUX_VERSION_CODE)
1389 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1390 type != BPF_PROG_TYPE_CGROUP_SKB &&
1391 !capable(CAP_SYS_ADMIN))
1394 bpf_prog_load_fixup_attach_type(attr);
1395 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1398 /* plain bpf_prog allocation */
1399 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1403 prog->expected_attach_type = attr->expected_attach_type;
1405 prog->aux->offload_requested = !!attr->prog_ifindex;
1407 err = security_bpf_prog_alloc(prog->aux);
1409 goto free_prog_nouncharge;
1411 err = bpf_prog_charge_memlock(prog);
1415 prog->len = attr->insn_cnt;
1418 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1419 bpf_prog_insn_size(prog)) != 0)
1422 prog->orig_prog = NULL;
1425 atomic_set(&prog->aux->refcnt, 1);
1426 prog->gpl_compatible = is_gpl ? 1 : 0;
1428 if (bpf_prog_is_dev_bound(prog->aux)) {
1429 err = bpf_prog_offload_init(prog, attr);
1434 /* find program type: socket_filter vs tracing_filter */
1435 err = find_prog_type(type, prog);
1439 prog->aux->load_time = ktime_get_boot_ns();
1440 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1444 /* run eBPF verifier */
1445 err = bpf_check(&prog, attr);
1447 goto free_used_maps;
1449 prog = bpf_prog_select_runtime(prog, &err);
1451 goto free_used_maps;
1453 err = bpf_prog_alloc_id(prog);
1455 goto free_used_maps;
1457 err = bpf_prog_new_fd(prog);
1459 /* failed to allocate fd.
1460 * bpf_prog_put() is needed because the above
1461 * bpf_prog_alloc_id() has published the prog
1462 * to the userspace and the userspace may
1463 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1469 bpf_prog_kallsyms_add(prog);
1473 bpf_prog_kallsyms_del_subprogs(prog);
1474 free_used_maps(prog->aux);
1476 bpf_prog_uncharge_memlock(prog);
1478 security_bpf_prog_free(prog->aux);
1479 free_prog_nouncharge:
1480 bpf_prog_free(prog);
1484 #define BPF_OBJ_LAST_FIELD file_flags
1486 static int bpf_obj_pin(const union bpf_attr *attr)
1488 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1491 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1494 static int bpf_obj_get(const union bpf_attr *attr)
1496 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1497 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1500 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1504 struct bpf_raw_tracepoint {
1505 struct bpf_raw_event_map *btp;
1506 struct bpf_prog *prog;
1509 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1511 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1514 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1515 bpf_prog_put(raw_tp->prog);
1521 static const struct file_operations bpf_raw_tp_fops = {
1522 .release = bpf_raw_tracepoint_release,
1523 .read = bpf_dummy_read,
1524 .write = bpf_dummy_write,
1527 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1529 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1531 struct bpf_raw_tracepoint *raw_tp;
1532 struct bpf_raw_event_map *btp;
1533 struct bpf_prog *prog;
1537 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1538 sizeof(tp_name) - 1) < 0)
1540 tp_name[sizeof(tp_name) - 1] = 0;
1542 btp = bpf_find_raw_tracepoint(tp_name);
1546 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1551 prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1552 BPF_PROG_TYPE_RAW_TRACEPOINT);
1554 err = PTR_ERR(prog);
1558 err = bpf_probe_register(raw_tp->btp, prog);
1562 raw_tp->prog = prog;
1563 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1566 bpf_probe_unregister(raw_tp->btp, prog);
1579 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1580 enum bpf_attach_type attach_type)
1582 switch (prog->type) {
1583 case BPF_PROG_TYPE_CGROUP_SOCK:
1584 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1585 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1591 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1593 #define BPF_F_ATTACH_MASK \
1594 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1596 static int bpf_prog_attach(const union bpf_attr *attr)
1598 enum bpf_prog_type ptype;
1599 struct bpf_prog *prog;
1602 if (!capable(CAP_NET_ADMIN))
1605 if (CHECK_ATTR(BPF_PROG_ATTACH))
1608 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
1611 switch (attr->attach_type) {
1612 case BPF_CGROUP_INET_INGRESS:
1613 case BPF_CGROUP_INET_EGRESS:
1614 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1616 case BPF_CGROUP_INET_SOCK_CREATE:
1617 case BPF_CGROUP_INET4_POST_BIND:
1618 case BPF_CGROUP_INET6_POST_BIND:
1619 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1621 case BPF_CGROUP_INET4_BIND:
1622 case BPF_CGROUP_INET6_BIND:
1623 case BPF_CGROUP_INET4_CONNECT:
1624 case BPF_CGROUP_INET6_CONNECT:
1625 case BPF_CGROUP_UDP4_SENDMSG:
1626 case BPF_CGROUP_UDP6_SENDMSG:
1627 case BPF_CGROUP_UDP4_RECVMSG:
1628 case BPF_CGROUP_UDP6_RECVMSG:
1629 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1631 case BPF_CGROUP_SOCK_OPS:
1632 ptype = BPF_PROG_TYPE_SOCK_OPS;
1634 case BPF_CGROUP_DEVICE:
1635 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1637 case BPF_SK_MSG_VERDICT:
1638 ptype = BPF_PROG_TYPE_SK_MSG;
1640 case BPF_SK_SKB_STREAM_PARSER:
1641 case BPF_SK_SKB_STREAM_VERDICT:
1642 ptype = BPF_PROG_TYPE_SK_SKB;
1644 case BPF_LIRC_MODE2:
1645 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1651 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1653 return PTR_ERR(prog);
1655 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1661 case BPF_PROG_TYPE_SK_SKB:
1662 case BPF_PROG_TYPE_SK_MSG:
1663 ret = sockmap_get_from_fd(attr, ptype, prog);
1665 case BPF_PROG_TYPE_LIRC_MODE2:
1666 ret = lirc_prog_attach(attr, prog);
1669 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
1677 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1679 static int bpf_prog_detach(const union bpf_attr *attr)
1681 enum bpf_prog_type ptype;
1683 if (!capable(CAP_NET_ADMIN))
1686 if (CHECK_ATTR(BPF_PROG_DETACH))
1689 switch (attr->attach_type) {
1690 case BPF_CGROUP_INET_INGRESS:
1691 case BPF_CGROUP_INET_EGRESS:
1692 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1694 case BPF_CGROUP_INET_SOCK_CREATE:
1695 case BPF_CGROUP_INET4_POST_BIND:
1696 case BPF_CGROUP_INET6_POST_BIND:
1697 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1699 case BPF_CGROUP_INET4_BIND:
1700 case BPF_CGROUP_INET6_BIND:
1701 case BPF_CGROUP_INET4_CONNECT:
1702 case BPF_CGROUP_INET6_CONNECT:
1703 case BPF_CGROUP_UDP4_SENDMSG:
1704 case BPF_CGROUP_UDP6_SENDMSG:
1705 case BPF_CGROUP_UDP4_RECVMSG:
1706 case BPF_CGROUP_UDP6_RECVMSG:
1707 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1709 case BPF_CGROUP_SOCK_OPS:
1710 ptype = BPF_PROG_TYPE_SOCK_OPS;
1712 case BPF_CGROUP_DEVICE:
1713 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1715 case BPF_SK_MSG_VERDICT:
1716 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, NULL);
1717 case BPF_SK_SKB_STREAM_PARSER:
1718 case BPF_SK_SKB_STREAM_VERDICT:
1719 return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, NULL);
1720 case BPF_LIRC_MODE2:
1721 return lirc_prog_detach(attr);
1726 return cgroup_bpf_prog_detach(attr, ptype);
1729 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1731 static int bpf_prog_query(const union bpf_attr *attr,
1732 union bpf_attr __user *uattr)
1734 if (!capable(CAP_NET_ADMIN))
1736 if (CHECK_ATTR(BPF_PROG_QUERY))
1738 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1741 switch (attr->query.attach_type) {
1742 case BPF_CGROUP_INET_INGRESS:
1743 case BPF_CGROUP_INET_EGRESS:
1744 case BPF_CGROUP_INET_SOCK_CREATE:
1745 case BPF_CGROUP_INET4_BIND:
1746 case BPF_CGROUP_INET6_BIND:
1747 case BPF_CGROUP_INET4_POST_BIND:
1748 case BPF_CGROUP_INET6_POST_BIND:
1749 case BPF_CGROUP_INET4_CONNECT:
1750 case BPF_CGROUP_INET6_CONNECT:
1751 case BPF_CGROUP_UDP4_SENDMSG:
1752 case BPF_CGROUP_UDP6_SENDMSG:
1753 case BPF_CGROUP_UDP4_RECVMSG:
1754 case BPF_CGROUP_UDP6_RECVMSG:
1755 case BPF_CGROUP_SOCK_OPS:
1756 case BPF_CGROUP_DEVICE:
1758 case BPF_LIRC_MODE2:
1759 return lirc_prog_query(attr, uattr);
1764 return cgroup_bpf_prog_query(attr, uattr);
1767 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1769 static int bpf_prog_test_run(const union bpf_attr *attr,
1770 union bpf_attr __user *uattr)
1772 struct bpf_prog *prog;
1773 int ret = -ENOTSUPP;
1775 if (!capable(CAP_SYS_ADMIN))
1777 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1780 prog = bpf_prog_get(attr->test.prog_fd);
1782 return PTR_ERR(prog);
1784 if (prog->aux->ops->test_run)
1785 ret = prog->aux->ops->test_run(prog, attr, uattr);
1791 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1793 static int bpf_obj_get_next_id(const union bpf_attr *attr,
1794 union bpf_attr __user *uattr,
1798 u32 next_id = attr->start_id;
1801 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1804 if (!capable(CAP_SYS_ADMIN))
1809 if (!idr_get_next(idr, &next_id))
1811 spin_unlock_bh(lock);
1814 err = put_user(next_id, &uattr->next_id);
1819 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1821 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1823 struct bpf_prog *prog;
1824 u32 id = attr->prog_id;
1827 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1830 if (!capable(CAP_SYS_ADMIN))
1833 spin_lock_bh(&prog_idr_lock);
1834 prog = idr_find(&prog_idr, id);
1836 prog = bpf_prog_inc_not_zero(prog);
1838 prog = ERR_PTR(-ENOENT);
1839 spin_unlock_bh(&prog_idr_lock);
1842 return PTR_ERR(prog);
1844 fd = bpf_prog_new_fd(prog);
1851 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
1853 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1855 struct bpf_map *map;
1856 u32 id = attr->map_id;
1860 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1861 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
1864 if (!capable(CAP_SYS_ADMIN))
1867 f_flags = bpf_get_file_flag(attr->open_flags);
1871 spin_lock_bh(&map_idr_lock);
1872 map = idr_find(&map_idr, id);
1874 map = bpf_map_inc_not_zero(map, true);
1876 map = ERR_PTR(-ENOENT);
1877 spin_unlock_bh(&map_idr_lock);
1880 return PTR_ERR(map);
1882 fd = bpf_map_new_fd(map, f_flags);
1889 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1894 for (i = 0; i < prog->aux->used_map_cnt; i++)
1895 if (prog->aux->used_maps[i] == (void *)addr)
1896 return prog->aux->used_maps[i];
1900 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1902 const struct bpf_map *map;
1903 struct bpf_insn *insns;
1907 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1912 for (i = 0; i < prog->len; i++) {
1913 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1914 insns[i].code = BPF_JMP | BPF_CALL;
1915 insns[i].imm = BPF_FUNC_tail_call;
1918 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1919 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1920 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1921 insns[i].code = BPF_JMP | BPF_CALL;
1922 if (!bpf_dump_raw_ok())
1927 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1930 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1931 map = bpf_map_from_imm(prog, imm);
1933 insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1934 insns[i].imm = map->id;
1935 insns[i + 1].imm = 0;
1939 if (!bpf_dump_raw_ok() &&
1940 imm == (unsigned long)prog->aux) {
1942 insns[i + 1].imm = 0;
1950 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1951 const union bpf_attr *attr,
1952 union bpf_attr __user *uattr)
1954 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1955 struct bpf_prog_info info = {};
1956 u32 info_len = attr->info.info_len;
1957 char __user *uinsns;
1961 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1964 info_len = min_t(u32, sizeof(info), info_len);
1966 if (copy_from_user(&info, uinfo, info_len))
1969 info.type = prog->type;
1970 info.id = prog->aux->id;
1971 info.load_time = prog->aux->load_time;
1972 info.created_by_uid = from_kuid_munged(current_user_ns(),
1973 prog->aux->user->uid);
1974 info.gpl_compatible = prog->gpl_compatible;
1976 memcpy(info.tag, prog->tag, sizeof(prog->tag));
1977 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1979 ulen = info.nr_map_ids;
1980 info.nr_map_ids = prog->aux->used_map_cnt;
1981 ulen = min_t(u32, info.nr_map_ids, ulen);
1983 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
1986 for (i = 0; i < ulen; i++)
1987 if (put_user(prog->aux->used_maps[i]->id,
1992 if (!capable(CAP_SYS_ADMIN)) {
1993 info.jited_prog_len = 0;
1994 info.xlated_prog_len = 0;
1995 info.nr_jited_ksyms = 0;
1996 info.nr_jited_func_lens = 0;
2000 ulen = info.xlated_prog_len;
2001 info.xlated_prog_len = bpf_prog_insn_size(prog);
2002 if (info.xlated_prog_len && ulen) {
2003 struct bpf_insn *insns_sanitized;
2006 if (prog->blinded && !bpf_dump_raw_ok()) {
2007 info.xlated_prog_insns = 0;
2010 insns_sanitized = bpf_insn_prepare_dump(prog);
2011 if (!insns_sanitized)
2013 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2014 ulen = min_t(u32, info.xlated_prog_len, ulen);
2015 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2016 kfree(insns_sanitized);
2021 if (bpf_prog_is_dev_bound(prog->aux)) {
2022 err = bpf_prog_offload_info_fill(&info, prog);
2028 /* NOTE: the following code is supposed to be skipped for offload.
2029 * bpf_prog_offload_info_fill() is the place to fill similar fields
2032 ulen = info.jited_prog_len;
2033 if (prog->aux->func_cnt) {
2036 info.jited_prog_len = 0;
2037 for (i = 0; i < prog->aux->func_cnt; i++)
2038 info.jited_prog_len += prog->aux->func[i]->jited_len;
2040 info.jited_prog_len = prog->jited_len;
2043 if (info.jited_prog_len && ulen) {
2044 if (bpf_dump_raw_ok()) {
2045 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2046 ulen = min_t(u32, info.jited_prog_len, ulen);
2048 /* for multi-function programs, copy the JITed
2049 * instructions for all the functions
2051 if (prog->aux->func_cnt) {
2056 for (i = 0; i < prog->aux->func_cnt; i++) {
2057 len = prog->aux->func[i]->jited_len;
2058 len = min_t(u32, len, free);
2059 img = (u8 *) prog->aux->func[i]->bpf_func;
2060 if (copy_to_user(uinsns, img, len))
2068 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2072 info.jited_prog_insns = 0;
2076 ulen = info.nr_jited_ksyms;
2077 info.nr_jited_ksyms = prog->aux->func_cnt;
2078 if (info.nr_jited_ksyms && ulen) {
2079 if (bpf_dump_raw_ok()) {
2080 u64 __user *user_ksyms;
2084 /* copy the address of the kernel symbol
2085 * corresponding to each function
2087 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2088 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2089 for (i = 0; i < ulen; i++) {
2090 ksym_addr = (ulong) prog->aux->func[i]->bpf_func;
2091 ksym_addr &= PAGE_MASK;
2092 if (put_user((u64) ksym_addr, &user_ksyms[i]))
2096 info.jited_ksyms = 0;
2100 ulen = info.nr_jited_func_lens;
2101 info.nr_jited_func_lens = prog->aux->func_cnt;
2102 if (info.nr_jited_func_lens && ulen) {
2103 if (bpf_dump_raw_ok()) {
2104 u32 __user *user_lens;
2107 /* copy the JITed image lengths for each function */
2108 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2109 user_lens = u64_to_user_ptr(info.jited_func_lens);
2110 for (i = 0; i < ulen; i++) {
2111 func_len = prog->aux->func[i]->jited_len;
2112 if (put_user(func_len, &user_lens[i]))
2116 info.jited_func_lens = 0;
2121 if (copy_to_user(uinfo, &info, info_len) ||
2122 put_user(info_len, &uattr->info.info_len))
2128 static int bpf_map_get_info_by_fd(struct bpf_map *map,
2129 const union bpf_attr *attr,
2130 union bpf_attr __user *uattr)
2132 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2133 struct bpf_map_info info = {};
2134 u32 info_len = attr->info.info_len;
2137 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2140 info_len = min_t(u32, sizeof(info), info_len);
2142 info.type = map->map_type;
2144 info.key_size = map->key_size;
2145 info.value_size = map->value_size;
2146 info.max_entries = map->max_entries;
2147 info.map_flags = map->map_flags;
2148 memcpy(info.name, map->name, sizeof(map->name));
2151 info.btf_id = btf_id(map->btf);
2152 info.btf_key_type_id = map->btf_key_type_id;
2153 info.btf_value_type_id = map->btf_value_type_id;
2156 if (bpf_map_is_dev_bound(map)) {
2157 err = bpf_map_offload_info_fill(&info, map);
2162 if (copy_to_user(uinfo, &info, info_len) ||
2163 put_user(info_len, &uattr->info.info_len))
2169 static int bpf_btf_get_info_by_fd(struct btf *btf,
2170 const union bpf_attr *attr,
2171 union bpf_attr __user *uattr)
2173 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2174 u32 info_len = attr->info.info_len;
2177 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
2181 return btf_get_info_by_fd(btf, attr, uattr);
2184 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2186 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2187 union bpf_attr __user *uattr)
2189 int ufd = attr->info.bpf_fd;
2193 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2200 if (f.file->f_op == &bpf_prog_fops)
2201 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2203 else if (f.file->f_op == &bpf_map_fops)
2204 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2206 else if (f.file->f_op == &btf_fops)
2207 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
2215 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2217 static int bpf_btf_load(const union bpf_attr *attr)
2219 if (CHECK_ATTR(BPF_BTF_LOAD))
2222 if (!capable(CAP_SYS_ADMIN))
2225 return btf_new_fd(attr);
2228 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2230 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2232 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2235 if (!capable(CAP_SYS_ADMIN))
2238 return btf_get_fd_by_id(attr->btf_id);
2241 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2242 union bpf_attr __user *uattr,
2243 u32 prog_id, u32 fd_type,
2244 const char *buf, u64 probe_offset,
2247 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2248 u32 len = buf ? strlen(buf) : 0, input_len;
2251 if (put_user(len, &uattr->task_fd_query.buf_len))
2253 input_len = attr->task_fd_query.buf_len;
2254 if (input_len && ubuf) {
2256 /* nothing to copy, just make ubuf NULL terminated */
2259 if (put_user(zero, ubuf))
2261 } else if (input_len >= len + 1) {
2262 /* ubuf can hold the string with NULL terminator */
2263 if (copy_to_user(ubuf, buf, len + 1))
2266 /* ubuf cannot hold the string with NULL terminator,
2267 * do a partial copy with NULL terminator.
2272 if (copy_to_user(ubuf, buf, input_len - 1))
2274 if (put_user(zero, ubuf + input_len - 1))
2279 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2280 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2281 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2282 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2288 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2290 static int bpf_task_fd_query(const union bpf_attr *attr,
2291 union bpf_attr __user *uattr)
2293 pid_t pid = attr->task_fd_query.pid;
2294 u32 fd = attr->task_fd_query.fd;
2295 const struct perf_event *event;
2296 struct files_struct *files;
2297 struct task_struct *task;
2301 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2304 if (!capable(CAP_SYS_ADMIN))
2307 if (attr->task_fd_query.flags != 0)
2310 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2314 files = get_files_struct(task);
2315 put_task_struct(task);
2320 spin_lock(&files->file_lock);
2321 file = fcheck_files(files, fd);
2326 spin_unlock(&files->file_lock);
2327 put_files_struct(files);
2332 if (file->f_op == &bpf_raw_tp_fops) {
2333 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2334 struct bpf_raw_event_map *btp = raw_tp->btp;
2336 err = bpf_task_fd_query_copy(attr, uattr,
2337 raw_tp->prog->aux->id,
2338 BPF_FD_TYPE_RAW_TRACEPOINT,
2339 btp->tp->name, 0, 0);
2343 event = perf_get_event(file);
2344 if (!IS_ERR(event)) {
2345 u64 probe_offset, probe_addr;
2346 u32 prog_id, fd_type;
2349 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2350 &buf, &probe_offset,
2353 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2367 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2369 union bpf_attr attr = {};
2372 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
2375 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
2378 size = min_t(u32, size, sizeof(attr));
2380 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2381 if (copy_from_user(&attr, uattr, size) != 0)
2384 err = security_bpf(cmd, &attr, size);
2389 case BPF_MAP_CREATE:
2390 err = map_create(&attr);
2392 case BPF_MAP_LOOKUP_ELEM:
2393 err = map_lookup_elem(&attr);
2395 case BPF_MAP_UPDATE_ELEM:
2396 err = map_update_elem(&attr);
2398 case BPF_MAP_DELETE_ELEM:
2399 err = map_delete_elem(&attr);
2401 case BPF_MAP_GET_NEXT_KEY:
2402 err = map_get_next_key(&attr);
2405 err = bpf_prog_load(&attr);
2408 err = bpf_obj_pin(&attr);
2411 err = bpf_obj_get(&attr);
2413 case BPF_PROG_ATTACH:
2414 err = bpf_prog_attach(&attr);
2416 case BPF_PROG_DETACH:
2417 err = bpf_prog_detach(&attr);
2419 case BPF_PROG_QUERY:
2420 err = bpf_prog_query(&attr, uattr);
2422 case BPF_PROG_TEST_RUN:
2423 err = bpf_prog_test_run(&attr, uattr);
2425 case BPF_PROG_GET_NEXT_ID:
2426 err = bpf_obj_get_next_id(&attr, uattr,
2427 &prog_idr, &prog_idr_lock);
2429 case BPF_MAP_GET_NEXT_ID:
2430 err = bpf_obj_get_next_id(&attr, uattr,
2431 &map_idr, &map_idr_lock);
2433 case BPF_PROG_GET_FD_BY_ID:
2434 err = bpf_prog_get_fd_by_id(&attr);
2436 case BPF_MAP_GET_FD_BY_ID:
2437 err = bpf_map_get_fd_by_id(&attr);
2439 case BPF_OBJ_GET_INFO_BY_FD:
2440 err = bpf_obj_get_info_by_fd(&attr, uattr);
2442 case BPF_RAW_TRACEPOINT_OPEN:
2443 err = bpf_raw_tracepoint_open(&attr);
2446 err = bpf_btf_load(&attr);
2448 case BPF_BTF_GET_FD_BY_ID:
2449 err = bpf_btf_get_fd_by_id(&attr);
2451 case BPF_TASK_FD_QUERY:
2452 err = bpf_task_fd_query(&attr, uattr);