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/syscalls.h>
15 #include <linux/slab.h>
16 #include <linux/sched/signal.h>
17 #include <linux/vmalloc.h>
18 #include <linux/mmzone.h>
19 #include <linux/anon_inodes.h>
20 #include <linux/file.h>
21 #include <linux/license.h>
22 #include <linux/filter.h>
23 #include <linux/version.h>
24 #include <linux/kernel.h>
25 #include <linux/idr.h>
27 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
28 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
29 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
31 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
32 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
34 DEFINE_PER_CPU(int, bpf_prog_active);
35 static DEFINE_IDR(prog_idr);
36 static DEFINE_SPINLOCK(prog_idr_lock);
37 static DEFINE_IDR(map_idr);
38 static DEFINE_SPINLOCK(map_idr_lock);
40 int sysctl_unprivileged_bpf_disabled __read_mostly =
41 IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
43 static const struct bpf_map_ops * const bpf_map_types[] = {
44 #define BPF_PROG_TYPE(_id, _ops)
45 #define BPF_MAP_TYPE(_id, _ops) \
47 #include <linux/bpf_types.h>
53 * If we're handed a bigger struct than we know of, ensure all the unknown bits
54 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
55 * we don't know about yet.
57 * There is a ToCToU between this function call and the following
58 * copy_from_user() call. However, this is not a concern since this function is
59 * meant to be a future-proofing of bits.
61 static int check_uarg_tail_zero(void __user *uaddr,
65 unsigned char __user *addr;
66 unsigned char __user *end;
70 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
73 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
76 if (actual_size <= expected_size)
79 addr = uaddr + expected_size;
80 end = uaddr + actual_size;
82 for (; addr < end; addr++) {
83 err = get_user(val, addr);
93 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
97 if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
98 !bpf_map_types[attr->map_type])
99 return ERR_PTR(-EINVAL);
101 map = bpf_map_types[attr->map_type]->map_alloc(attr);
104 map->ops = bpf_map_types[attr->map_type];
105 map->map_type = attr->map_type;
109 void *bpf_map_area_alloc(size_t size, int numa_node)
111 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
112 * trigger under memory pressure as we really just want to
115 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
118 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
119 area = kmalloc_node(size, GFP_USER | flags, numa_node);
124 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
125 __builtin_return_address(0));
128 void bpf_map_area_free(void *area)
133 int bpf_map_precharge_memlock(u32 pages)
135 struct user_struct *user = get_current_user();
136 unsigned long memlock_limit, cur;
138 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
139 cur = atomic_long_read(&user->locked_vm);
141 if (cur + pages > memlock_limit)
146 static int bpf_map_charge_memlock(struct bpf_map *map)
148 struct user_struct *user = get_current_user();
149 unsigned long memlock_limit;
151 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
153 atomic_long_add(map->pages, &user->locked_vm);
155 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
156 atomic_long_sub(map->pages, &user->locked_vm);
164 static void bpf_map_uncharge_memlock(struct bpf_map *map)
166 struct user_struct *user = map->user;
168 atomic_long_sub(map->pages, &user->locked_vm);
172 static int bpf_map_alloc_id(struct bpf_map *map)
176 spin_lock_bh(&map_idr_lock);
177 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
180 spin_unlock_bh(&map_idr_lock);
182 if (WARN_ON_ONCE(!id))
185 return id > 0 ? 0 : id;
188 static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
193 spin_lock_irqsave(&map_idr_lock, flags);
195 __acquire(&map_idr_lock);
197 idr_remove(&map_idr, map->id);
200 spin_unlock_irqrestore(&map_idr_lock, flags);
202 __release(&map_idr_lock);
205 /* called from workqueue */
206 static void bpf_map_free_deferred(struct work_struct *work)
208 struct bpf_map *map = container_of(work, struct bpf_map, work);
210 bpf_map_uncharge_memlock(map);
211 /* implementation dependent freeing */
212 map->ops->map_free(map);
215 static void bpf_map_put_uref(struct bpf_map *map)
217 if (atomic_dec_and_test(&map->usercnt)) {
218 if (map->ops->map_release_uref)
219 map->ops->map_release_uref(map);
223 /* decrement map refcnt and schedule it for freeing via workqueue
224 * (unrelying map implementation ops->map_free() might sleep)
226 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
228 if (atomic_dec_and_test(&map->refcnt)) {
229 /* bpf_map_free_id() must be called first */
230 bpf_map_free_id(map, do_idr_lock);
231 INIT_WORK(&map->work, bpf_map_free_deferred);
232 schedule_work(&map->work);
236 void bpf_map_put(struct bpf_map *map)
238 __bpf_map_put(map, true);
241 void bpf_map_put_with_uref(struct bpf_map *map)
243 bpf_map_put_uref(map);
247 static int bpf_map_release(struct inode *inode, struct file *filp)
249 struct bpf_map *map = filp->private_data;
251 if (map->ops->map_release)
252 map->ops->map_release(map, filp);
254 bpf_map_put_with_uref(map);
258 #ifdef CONFIG_PROC_FS
259 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
261 const struct bpf_map *map = filp->private_data;
262 const struct bpf_array *array;
263 u32 owner_prog_type = 0;
266 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
267 array = container_of(map, struct bpf_array, map);
268 owner_prog_type = array->owner_prog_type;
269 owner_jited = array->owner_jited;
284 map->pages * 1ULL << PAGE_SHIFT);
286 if (owner_prog_type) {
287 seq_printf(m, "owner_prog_type:\t%u\n",
289 seq_printf(m, "owner_jited:\t%u\n",
295 static const struct file_operations bpf_map_fops = {
296 #ifdef CONFIG_PROC_FS
297 .show_fdinfo = bpf_map_show_fdinfo,
299 .release = bpf_map_release,
302 int bpf_map_new_fd(struct bpf_map *map)
304 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
308 /* helper macro to check that unused fields 'union bpf_attr' are zero */
309 #define CHECK_ATTR(CMD) \
310 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
311 sizeof(attr->CMD##_LAST_FIELD), 0, \
313 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
314 sizeof(attr->CMD##_LAST_FIELD)) != NULL
316 #define BPF_MAP_CREATE_LAST_FIELD numa_node
317 /* called via syscall */
318 static int map_create(union bpf_attr *attr)
320 int numa_node = bpf_map_attr_numa_node(attr);
324 err = CHECK_ATTR(BPF_MAP_CREATE);
328 if (numa_node != NUMA_NO_NODE &&
329 ((unsigned int)numa_node >= nr_node_ids ||
330 !node_online(numa_node)))
333 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
334 map = find_and_alloc_map(attr);
338 atomic_set(&map->refcnt, 1);
339 atomic_set(&map->usercnt, 1);
341 err = bpf_map_charge_memlock(map);
343 goto free_map_nouncharge;
345 err = bpf_map_alloc_id(map);
349 err = bpf_map_new_fd(map);
351 /* failed to allocate fd.
352 * bpf_map_put_with_uref() is needed because the above
353 * bpf_map_alloc_id() has published the map
354 * to the userspace and the userspace may
355 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
357 bpf_map_put_with_uref(map);
361 trace_bpf_map_create(map, err);
365 bpf_map_uncharge_memlock(map);
367 map->ops->map_free(map);
371 /* if error is returned, fd is released.
372 * On success caller should complete fd access with matching fdput()
374 struct bpf_map *__bpf_map_get(struct fd f)
377 return ERR_PTR(-EBADF);
378 if (f.file->f_op != &bpf_map_fops) {
380 return ERR_PTR(-EINVAL);
383 return f.file->private_data;
386 /* prog's and map's refcnt limit */
387 #define BPF_MAX_REFCNT 32768
389 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
391 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
392 atomic_dec(&map->refcnt);
393 return ERR_PTR(-EBUSY);
396 atomic_inc(&map->usercnt);
400 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
402 struct fd f = fdget(ufd);
405 map = __bpf_map_get(f);
409 map = bpf_map_inc(map, true);
415 /* map_idr_lock should have been held */
416 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
421 refold = __atomic_add_unless(&map->refcnt, 1, 0);
423 if (refold >= BPF_MAX_REFCNT) {
424 __bpf_map_put(map, false);
425 return ERR_PTR(-EBUSY);
429 return ERR_PTR(-ENOENT);
432 atomic_inc(&map->usercnt);
437 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
442 /* last field in 'union bpf_attr' used by this command */
443 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
445 static int map_lookup_elem(union bpf_attr *attr)
447 void __user *ukey = u64_to_user_ptr(attr->key);
448 void __user *uvalue = u64_to_user_ptr(attr->value);
449 int ufd = attr->map_fd;
451 void *key, *value, *ptr;
456 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
460 map = __bpf_map_get(f);
464 key = memdup_user(ukey, map->key_size);
470 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
471 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
472 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
473 value_size = round_up(map->value_size, 8) * num_possible_cpus();
474 else if (IS_FD_MAP(map))
475 value_size = sizeof(u32);
477 value_size = map->value_size;
480 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
484 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
485 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
486 err = bpf_percpu_hash_copy(map, key, value);
487 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
488 err = bpf_percpu_array_copy(map, key, value);
489 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
490 err = bpf_stackmap_copy(map, key, value);
491 } else if (IS_FD_ARRAY(map)) {
492 err = bpf_fd_array_map_lookup_elem(map, key, value);
493 } else if (IS_FD_HASH(map)) {
494 err = bpf_fd_htab_map_lookup_elem(map, key, value);
497 if (map->ops->map_lookup_elem_sys_only)
498 ptr = map->ops->map_lookup_elem_sys_only(map, key);
500 ptr = map->ops->map_lookup_elem(map, key);
502 memcpy(value, ptr, value_size);
504 err = ptr ? 0 : -ENOENT;
511 if (copy_to_user(uvalue, value, value_size) != 0)
514 trace_bpf_map_lookup_elem(map, ufd, key, value);
526 static void maybe_wait_bpf_programs(struct bpf_map *map)
528 /* Wait for any running BPF programs to complete so that
529 * userspace, when we return to it, knows that all programs
530 * that could be running use the new map value.
532 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
533 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
537 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
539 static int map_update_elem(union bpf_attr *attr)
541 void __user *ukey = u64_to_user_ptr(attr->key);
542 void __user *uvalue = u64_to_user_ptr(attr->value);
543 int ufd = attr->map_fd;
550 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
554 map = __bpf_map_get(f);
558 key = memdup_user(ukey, map->key_size);
564 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
565 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
566 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
567 value_size = round_up(map->value_size, 8) * num_possible_cpus();
569 value_size = map->value_size;
572 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
577 if (copy_from_user(value, uvalue, value_size) != 0)
580 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
581 * inside bpf map update or delete otherwise deadlocks are possible
584 __this_cpu_inc(bpf_prog_active);
585 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
586 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
587 err = bpf_percpu_hash_update(map, key, value, attr->flags);
588 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
589 err = bpf_percpu_array_update(map, key, value, attr->flags);
590 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
591 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
592 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
593 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
595 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
598 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
600 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
605 err = map->ops->map_update_elem(map, key, value, attr->flags);
608 __this_cpu_dec(bpf_prog_active);
610 maybe_wait_bpf_programs(map);
613 trace_bpf_map_update_elem(map, ufd, key, value);
623 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
625 static int map_delete_elem(union bpf_attr *attr)
627 void __user *ukey = u64_to_user_ptr(attr->key);
628 int ufd = attr->map_fd;
634 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
638 map = __bpf_map_get(f);
642 key = memdup_user(ukey, map->key_size);
649 __this_cpu_inc(bpf_prog_active);
651 err = map->ops->map_delete_elem(map, key);
653 __this_cpu_dec(bpf_prog_active);
655 maybe_wait_bpf_programs(map);
658 trace_bpf_map_delete_elem(map, ufd, key);
665 /* last field in 'union bpf_attr' used by this command */
666 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
668 static int map_get_next_key(union bpf_attr *attr)
670 void __user *ukey = u64_to_user_ptr(attr->key);
671 void __user *unext_key = u64_to_user_ptr(attr->next_key);
672 int ufd = attr->map_fd;
674 void *key, *next_key;
678 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
682 map = __bpf_map_get(f);
687 key = memdup_user(ukey, map->key_size);
697 next_key = kmalloc(map->key_size, GFP_USER);
702 err = map->ops->map_get_next_key(map, key, next_key);
708 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
711 trace_bpf_map_next_key(map, ufd, key, next_key);
723 static const struct bpf_verifier_ops * const bpf_prog_types[] = {
724 #define BPF_PROG_TYPE(_id, _ops) \
726 #define BPF_MAP_TYPE(_id, _ops)
727 #include <linux/bpf_types.h>
732 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
734 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
737 prog->aux->ops = bpf_prog_types[type];
742 /* drop refcnt on maps used by eBPF program and free auxilary data */
743 static void free_used_maps(struct bpf_prog_aux *aux)
747 for (i = 0; i < aux->used_map_cnt; i++)
748 bpf_map_put(aux->used_maps[i]);
750 kfree(aux->used_maps);
753 int __bpf_prog_charge(struct user_struct *user, u32 pages)
755 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
756 unsigned long user_bufs;
759 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
760 if (user_bufs > memlock_limit) {
761 atomic_long_sub(pages, &user->locked_vm);
769 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
772 atomic_long_sub(pages, &user->locked_vm);
775 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
777 struct user_struct *user = get_current_user();
780 ret = __bpf_prog_charge(user, prog->pages);
786 prog->aux->user = user;
790 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
792 struct user_struct *user = prog->aux->user;
794 __bpf_prog_uncharge(user, prog->pages);
798 static int bpf_prog_alloc_id(struct bpf_prog *prog)
802 spin_lock_bh(&prog_idr_lock);
803 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
806 spin_unlock_bh(&prog_idr_lock);
808 /* id is in [1, INT_MAX) */
809 if (WARN_ON_ONCE(!id))
812 return id > 0 ? 0 : id;
815 static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
817 /* cBPF to eBPF migrations are currently not in the idr store. */
822 spin_lock_bh(&prog_idr_lock);
824 __acquire(&prog_idr_lock);
826 idr_remove(&prog_idr, prog->aux->id);
829 spin_unlock_bh(&prog_idr_lock);
831 __release(&prog_idr_lock);
834 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
836 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
839 bpf_prog_uncharge_memlock(aux->prog);
840 bpf_prog_free(aux->prog);
843 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
845 if (atomic_dec_and_test(&prog->aux->refcnt)) {
846 trace_bpf_prog_put_rcu(prog);
847 /* bpf_prog_free_id() must be called first */
848 bpf_prog_free_id(prog, do_idr_lock);
849 bpf_prog_kallsyms_del(prog);
850 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
854 void bpf_prog_put(struct bpf_prog *prog)
856 __bpf_prog_put(prog, true);
858 EXPORT_SYMBOL_GPL(bpf_prog_put);
860 static int bpf_prog_release(struct inode *inode, struct file *filp)
862 struct bpf_prog *prog = filp->private_data;
868 #ifdef CONFIG_PROC_FS
869 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
871 const struct bpf_prog *prog = filp->private_data;
872 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
874 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
883 prog->pages * 1ULL << PAGE_SHIFT);
887 static const struct file_operations bpf_prog_fops = {
888 #ifdef CONFIG_PROC_FS
889 .show_fdinfo = bpf_prog_show_fdinfo,
891 .release = bpf_prog_release,
894 int bpf_prog_new_fd(struct bpf_prog *prog)
896 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
900 static struct bpf_prog *____bpf_prog_get(struct fd f)
903 return ERR_PTR(-EBADF);
904 if (f.file->f_op != &bpf_prog_fops) {
906 return ERR_PTR(-EINVAL);
909 return f.file->private_data;
912 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
914 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
915 atomic_sub(i, &prog->aux->refcnt);
916 return ERR_PTR(-EBUSY);
920 EXPORT_SYMBOL_GPL(bpf_prog_add);
922 void bpf_prog_sub(struct bpf_prog *prog, int i)
924 /* Only to be used for undoing previous bpf_prog_add() in some
925 * error path. We still know that another entity in our call
926 * path holds a reference to the program, thus atomic_sub() can
927 * be safely used in such cases!
929 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
931 EXPORT_SYMBOL_GPL(bpf_prog_sub);
933 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
935 return bpf_prog_add(prog, 1);
937 EXPORT_SYMBOL_GPL(bpf_prog_inc);
939 /* prog_idr_lock should have been held */
940 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
944 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
946 if (refold >= BPF_MAX_REFCNT) {
947 __bpf_prog_put(prog, false);
948 return ERR_PTR(-EBUSY);
952 return ERR_PTR(-ENOENT);
956 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
958 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
960 struct fd f = fdget(ufd);
961 struct bpf_prog *prog;
963 prog = ____bpf_prog_get(f);
966 if (type && prog->type != *type) {
967 prog = ERR_PTR(-EINVAL);
971 prog = bpf_prog_inc(prog);
977 struct bpf_prog *bpf_prog_get(u32 ufd)
979 return __bpf_prog_get(ufd, NULL);
982 struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
984 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
987 trace_bpf_prog_get_type(prog);
990 EXPORT_SYMBOL_GPL(bpf_prog_get_type);
992 /* last field in 'union bpf_attr' used by this command */
993 #define BPF_PROG_LOAD_LAST_FIELD prog_flags
995 static int bpf_prog_load(union bpf_attr *attr)
997 enum bpf_prog_type type = attr->prog_type;
998 struct bpf_prog *prog;
1003 if (CHECK_ATTR(BPF_PROG_LOAD))
1006 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1009 /* copy eBPF program license from user space */
1010 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1011 sizeof(license) - 1) < 0)
1013 license[sizeof(license) - 1] = 0;
1015 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1016 is_gpl = license_is_gpl_compatible(license);
1018 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1021 if (type == BPF_PROG_TYPE_KPROBE &&
1022 attr->kern_version != LINUX_VERSION_CODE)
1025 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1026 type != BPF_PROG_TYPE_CGROUP_SKB &&
1027 !capable(CAP_SYS_ADMIN))
1030 /* plain bpf_prog allocation */
1031 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1035 err = bpf_prog_charge_memlock(prog);
1037 goto free_prog_nouncharge;
1039 prog->len = attr->insn_cnt;
1042 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1043 bpf_prog_insn_size(prog)) != 0)
1046 prog->orig_prog = NULL;
1049 atomic_set(&prog->aux->refcnt, 1);
1050 prog->gpl_compatible = is_gpl ? 1 : 0;
1052 /* find program type: socket_filter vs tracing_filter */
1053 err = find_prog_type(type, prog);
1057 /* run eBPF verifier */
1058 err = bpf_check(&prog, attr);
1060 goto free_used_maps;
1062 /* eBPF program is ready to be JITed */
1063 prog = bpf_prog_select_runtime(prog, &err);
1065 goto free_used_maps;
1067 err = bpf_prog_alloc_id(prog);
1069 goto free_used_maps;
1071 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
1072 * effectively publicly exposed. However, retrieving via
1073 * bpf_prog_get_fd_by_id() will take another reference,
1074 * therefore it cannot be gone underneath us.
1076 * Only for the time /after/ successful bpf_prog_new_fd()
1077 * and before returning to userspace, we might just hold
1078 * one reference and any parallel close on that fd could
1079 * rip everything out. Hence, below notifications must
1080 * happen before bpf_prog_new_fd().
1082 * Also, any failure handling from this point onwards must
1083 * be using bpf_prog_put() given the program is exposed.
1085 bpf_prog_kallsyms_add(prog);
1086 trace_bpf_prog_load(prog, err);
1088 err = bpf_prog_new_fd(prog);
1094 free_used_maps(prog->aux);
1096 bpf_prog_uncharge_memlock(prog);
1097 free_prog_nouncharge:
1098 bpf_prog_free(prog);
1102 #define BPF_OBJ_LAST_FIELD bpf_fd
1104 static int bpf_obj_pin(const union bpf_attr *attr)
1106 if (CHECK_ATTR(BPF_OBJ))
1109 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1112 static int bpf_obj_get(const union bpf_attr *attr)
1114 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
1117 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
1120 #ifdef CONFIG_CGROUP_BPF
1122 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1124 static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
1126 struct bpf_prog *prog = NULL;
1127 int ufd = attr->target_fd;
1128 struct bpf_map *map;
1133 map = __bpf_map_get(f);
1135 return PTR_ERR(map);
1138 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1139 BPF_PROG_TYPE_SK_SKB);
1142 return PTR_ERR(prog);
1146 err = sock_map_prog(map, prog, attr->attach_type);
1158 static int bpf_prog_attach(const union bpf_attr *attr)
1160 enum bpf_prog_type ptype;
1161 struct bpf_prog *prog;
1162 struct cgroup *cgrp;
1165 if (!capable(CAP_NET_ADMIN))
1168 if (CHECK_ATTR(BPF_PROG_ATTACH))
1171 if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
1174 switch (attr->attach_type) {
1175 case BPF_CGROUP_INET_INGRESS:
1176 case BPF_CGROUP_INET_EGRESS:
1177 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1179 case BPF_CGROUP_INET_SOCK_CREATE:
1180 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1182 case BPF_CGROUP_SOCK_OPS:
1183 ptype = BPF_PROG_TYPE_SOCK_OPS;
1185 case BPF_SK_SKB_STREAM_PARSER:
1186 case BPF_SK_SKB_STREAM_VERDICT:
1187 return sockmap_get_from_fd(attr, true);
1192 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1194 return PTR_ERR(prog);
1196 cgrp = cgroup_get_from_fd(attr->target_fd);
1199 return PTR_ERR(cgrp);
1202 ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
1203 attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
1211 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1213 static int bpf_prog_detach(const union bpf_attr *attr)
1215 struct cgroup *cgrp;
1218 if (!capable(CAP_NET_ADMIN))
1221 if (CHECK_ATTR(BPF_PROG_DETACH))
1224 switch (attr->attach_type) {
1225 case BPF_CGROUP_INET_INGRESS:
1226 case BPF_CGROUP_INET_EGRESS:
1227 case BPF_CGROUP_INET_SOCK_CREATE:
1228 case BPF_CGROUP_SOCK_OPS:
1229 cgrp = cgroup_get_from_fd(attr->target_fd);
1231 return PTR_ERR(cgrp);
1233 ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
1236 case BPF_SK_SKB_STREAM_PARSER:
1237 case BPF_SK_SKB_STREAM_VERDICT:
1238 ret = sockmap_get_from_fd(attr, false);
1247 #endif /* CONFIG_CGROUP_BPF */
1249 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1251 static int bpf_prog_test_run(const union bpf_attr *attr,
1252 union bpf_attr __user *uattr)
1254 struct bpf_prog *prog;
1255 int ret = -ENOTSUPP;
1257 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1260 prog = bpf_prog_get(attr->test.prog_fd);
1262 return PTR_ERR(prog);
1264 if (prog->aux->ops->test_run)
1265 ret = prog->aux->ops->test_run(prog, attr, uattr);
1271 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1273 static int bpf_obj_get_next_id(const union bpf_attr *attr,
1274 union bpf_attr __user *uattr,
1278 u32 next_id = attr->start_id;
1281 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1284 if (!capable(CAP_SYS_ADMIN))
1289 if (!idr_get_next(idr, &next_id))
1291 spin_unlock_bh(lock);
1294 err = put_user(next_id, &uattr->next_id);
1299 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1301 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1303 struct bpf_prog *prog;
1304 u32 id = attr->prog_id;
1307 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1310 if (!capable(CAP_SYS_ADMIN))
1313 spin_lock_bh(&prog_idr_lock);
1314 prog = idr_find(&prog_idr, id);
1316 prog = bpf_prog_inc_not_zero(prog);
1318 prog = ERR_PTR(-ENOENT);
1319 spin_unlock_bh(&prog_idr_lock);
1322 return PTR_ERR(prog);
1324 fd = bpf_prog_new_fd(prog);
1331 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD map_id
1333 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1335 struct bpf_map *map;
1336 u32 id = attr->map_id;
1339 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID))
1342 if (!capable(CAP_SYS_ADMIN))
1345 spin_lock_bh(&map_idr_lock);
1346 map = idr_find(&map_idr, id);
1348 map = bpf_map_inc_not_zero(map, true);
1350 map = ERR_PTR(-ENOENT);
1351 spin_unlock_bh(&map_idr_lock);
1354 return PTR_ERR(map);
1356 fd = bpf_map_new_fd(map);
1358 bpf_map_put_with_uref(map);
1363 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1364 const union bpf_attr *attr,
1365 union bpf_attr __user *uattr)
1367 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1368 struct bpf_prog_info info;
1369 u32 info_len = attr->info.info_len;
1370 char __user *uinsns;
1374 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1377 info_len = min_t(u32, sizeof(info), info_len);
1379 memset(&info, 0, sizeof(info));
1380 if (copy_from_user(&info, uinfo, info_len))
1383 info.type = prog->type;
1384 info.id = prog->aux->id;
1386 memcpy(info.tag, prog->tag, sizeof(prog->tag));
1388 if (!capable(CAP_SYS_ADMIN)) {
1389 info.jited_prog_len = 0;
1390 info.xlated_prog_len = 0;
1394 ulen = info.jited_prog_len;
1395 info.jited_prog_len = prog->jited_len;
1396 if (info.jited_prog_len && ulen) {
1397 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1398 ulen = min_t(u32, info.jited_prog_len, ulen);
1399 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1403 ulen = info.xlated_prog_len;
1404 info.xlated_prog_len = bpf_prog_insn_size(prog);
1405 if (info.xlated_prog_len && ulen) {
1406 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1407 ulen = min_t(u32, info.xlated_prog_len, ulen);
1408 if (copy_to_user(uinsns, prog->insnsi, ulen))
1413 if (copy_to_user(uinfo, &info, info_len) ||
1414 put_user(info_len, &uattr->info.info_len))
1420 static int bpf_map_get_info_by_fd(struct bpf_map *map,
1421 const union bpf_attr *attr,
1422 union bpf_attr __user *uattr)
1424 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1425 struct bpf_map_info info;
1426 u32 info_len = attr->info.info_len;
1429 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1432 info_len = min_t(u32, sizeof(info), info_len);
1434 memset(&info, 0, sizeof(info));
1435 info.type = map->map_type;
1437 info.key_size = map->key_size;
1438 info.value_size = map->value_size;
1439 info.max_entries = map->max_entries;
1440 info.map_flags = map->map_flags;
1442 if (copy_to_user(uinfo, &info, info_len) ||
1443 put_user(info_len, &uattr->info.info_len))
1449 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1451 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1452 union bpf_attr __user *uattr)
1454 int ufd = attr->info.bpf_fd;
1458 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1465 if (f.file->f_op == &bpf_prog_fops)
1466 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1468 else if (f.file->f_op == &bpf_map_fops)
1469 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1478 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1480 union bpf_attr attr;
1483 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
1486 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1489 size = min_t(u32, size, sizeof(attr));
1491 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1492 memset(&attr, 0, sizeof(attr));
1493 if (copy_from_user(&attr, uattr, size) != 0)
1497 case BPF_MAP_CREATE:
1498 err = map_create(&attr);
1500 case BPF_MAP_LOOKUP_ELEM:
1501 err = map_lookup_elem(&attr);
1503 case BPF_MAP_UPDATE_ELEM:
1504 err = map_update_elem(&attr);
1506 case BPF_MAP_DELETE_ELEM:
1507 err = map_delete_elem(&attr);
1509 case BPF_MAP_GET_NEXT_KEY:
1510 err = map_get_next_key(&attr);
1513 err = bpf_prog_load(&attr);
1516 err = bpf_obj_pin(&attr);
1519 err = bpf_obj_get(&attr);
1521 #ifdef CONFIG_CGROUP_BPF
1522 case BPF_PROG_ATTACH:
1523 err = bpf_prog_attach(&attr);
1525 case BPF_PROG_DETACH:
1526 err = bpf_prog_detach(&attr);
1529 case BPF_PROG_TEST_RUN:
1530 err = bpf_prog_test_run(&attr, uattr);
1532 case BPF_PROG_GET_NEXT_ID:
1533 err = bpf_obj_get_next_id(&attr, uattr,
1534 &prog_idr, &prog_idr_lock);
1536 case BPF_MAP_GET_NEXT_ID:
1537 err = bpf_obj_get_next_id(&attr, uattr,
1538 &map_idr, &map_idr_lock);
1540 case BPF_PROG_GET_FD_BY_ID:
1541 err = bpf_prog_get_fd_by_id(&attr);
1543 case BPF_MAP_GET_FD_BY_ID:
1544 err = bpf_map_get_fd_by_id(&attr);
1546 case BPF_OBJ_GET_INFO_BY_FD:
1547 err = bpf_obj_get_info_by_fd(&attr, uattr);