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;
42 static const struct bpf_map_ops * const bpf_map_types[] = {
43 #define BPF_PROG_TYPE(_id, _ops)
44 #define BPF_MAP_TYPE(_id, _ops) \
46 #include <linux/bpf_types.h>
51 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
55 if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
56 !bpf_map_types[attr->map_type])
57 return ERR_PTR(-EINVAL);
59 map = bpf_map_types[attr->map_type]->map_alloc(attr);
62 map->ops = bpf_map_types[attr->map_type];
63 map->map_type = attr->map_type;
67 void *bpf_map_area_alloc(size_t size)
69 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
70 * trigger under memory pressure as we really just want to
73 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
76 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
77 area = kmalloc(size, GFP_USER | flags);
82 return __vmalloc(size, GFP_KERNEL | flags, PAGE_KERNEL);
85 void bpf_map_area_free(void *area)
90 int bpf_map_precharge_memlock(u32 pages)
92 struct user_struct *user = get_current_user();
93 unsigned long memlock_limit, cur;
95 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
96 cur = atomic_long_read(&user->locked_vm);
98 if (cur + pages > memlock_limit)
103 static int bpf_map_charge_memlock(struct bpf_map *map)
105 struct user_struct *user = get_current_user();
106 unsigned long memlock_limit;
108 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
110 atomic_long_add(map->pages, &user->locked_vm);
112 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
113 atomic_long_sub(map->pages, &user->locked_vm);
121 static void bpf_map_uncharge_memlock(struct bpf_map *map)
123 struct user_struct *user = map->user;
125 atomic_long_sub(map->pages, &user->locked_vm);
129 static int bpf_map_alloc_id(struct bpf_map *map)
133 spin_lock_bh(&map_idr_lock);
134 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
137 spin_unlock_bh(&map_idr_lock);
139 if (WARN_ON_ONCE(!id))
142 return id > 0 ? 0 : id;
145 static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
148 spin_lock_bh(&map_idr_lock);
150 __acquire(&map_idr_lock);
152 idr_remove(&map_idr, map->id);
155 spin_unlock_bh(&map_idr_lock);
157 __release(&map_idr_lock);
160 /* called from workqueue */
161 static void bpf_map_free_deferred(struct work_struct *work)
163 struct bpf_map *map = container_of(work, struct bpf_map, work);
165 bpf_map_uncharge_memlock(map);
166 /* implementation dependent freeing */
167 map->ops->map_free(map);
170 static void bpf_map_put_uref(struct bpf_map *map)
172 if (atomic_dec_and_test(&map->usercnt)) {
173 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
174 bpf_fd_array_map_clear(map);
178 /* decrement map refcnt and schedule it for freeing via workqueue
179 * (unrelying map implementation ops->map_free() might sleep)
181 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
183 if (atomic_dec_and_test(&map->refcnt)) {
184 /* bpf_map_free_id() must be called first */
185 bpf_map_free_id(map, do_idr_lock);
186 INIT_WORK(&map->work, bpf_map_free_deferred);
187 schedule_work(&map->work);
191 void bpf_map_put(struct bpf_map *map)
193 __bpf_map_put(map, true);
196 void bpf_map_put_with_uref(struct bpf_map *map)
198 bpf_map_put_uref(map);
202 static int bpf_map_release(struct inode *inode, struct file *filp)
204 struct bpf_map *map = filp->private_data;
206 if (map->ops->map_release)
207 map->ops->map_release(map, filp);
209 bpf_map_put_with_uref(map);
213 #ifdef CONFIG_PROC_FS
214 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
216 const struct bpf_map *map = filp->private_data;
217 const struct bpf_array *array;
218 u32 owner_prog_type = 0;
220 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
221 array = container_of(map, struct bpf_array, map);
222 owner_prog_type = array->owner_prog_type;
237 map->pages * 1ULL << PAGE_SHIFT);
240 seq_printf(m, "owner_prog_type:\t%u\n",
245 static const struct file_operations bpf_map_fops = {
246 #ifdef CONFIG_PROC_FS
247 .show_fdinfo = bpf_map_show_fdinfo,
249 .release = bpf_map_release,
252 int bpf_map_new_fd(struct bpf_map *map)
254 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
258 /* helper macro to check that unused fields 'union bpf_attr' are zero */
259 #define CHECK_ATTR(CMD) \
260 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
261 sizeof(attr->CMD##_LAST_FIELD), 0, \
263 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
264 sizeof(attr->CMD##_LAST_FIELD)) != NULL
266 #define BPF_MAP_CREATE_LAST_FIELD inner_map_fd
267 /* called via syscall */
268 static int map_create(union bpf_attr *attr)
273 err = CHECK_ATTR(BPF_MAP_CREATE);
277 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
278 map = find_and_alloc_map(attr);
282 atomic_set(&map->refcnt, 1);
283 atomic_set(&map->usercnt, 1);
285 err = bpf_map_charge_memlock(map);
287 goto free_map_nouncharge;
289 err = bpf_map_alloc_id(map);
293 err = bpf_map_new_fd(map);
295 /* failed to allocate fd.
296 * bpf_map_put() is needed because the above
297 * bpf_map_alloc_id() has published the map
298 * to the userspace and the userspace may
299 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
305 trace_bpf_map_create(map, err);
309 bpf_map_uncharge_memlock(map);
311 map->ops->map_free(map);
315 /* if error is returned, fd is released.
316 * On success caller should complete fd access with matching fdput()
318 struct bpf_map *__bpf_map_get(struct fd f)
321 return ERR_PTR(-EBADF);
322 if (f.file->f_op != &bpf_map_fops) {
324 return ERR_PTR(-EINVAL);
327 return f.file->private_data;
330 /* prog's and map's refcnt limit */
331 #define BPF_MAX_REFCNT 32768
333 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
335 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
336 atomic_dec(&map->refcnt);
337 return ERR_PTR(-EBUSY);
340 atomic_inc(&map->usercnt);
344 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
346 struct fd f = fdget(ufd);
349 map = __bpf_map_get(f);
353 map = bpf_map_inc(map, true);
359 /* map_idr_lock should have been held */
360 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
365 refold = __atomic_add_unless(&map->refcnt, 1, 0);
367 if (refold >= BPF_MAX_REFCNT) {
368 __bpf_map_put(map, false);
369 return ERR_PTR(-EBUSY);
373 return ERR_PTR(-ENOENT);
376 atomic_inc(&map->usercnt);
381 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
386 /* last field in 'union bpf_attr' used by this command */
387 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
389 static int map_lookup_elem(union bpf_attr *attr)
391 void __user *ukey = u64_to_user_ptr(attr->key);
392 void __user *uvalue = u64_to_user_ptr(attr->value);
393 int ufd = attr->map_fd;
395 void *key, *value, *ptr;
400 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
404 map = __bpf_map_get(f);
409 key = kmalloc(map->key_size, GFP_USER);
414 if (copy_from_user(key, ukey, map->key_size) != 0)
417 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
418 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
419 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
420 value_size = round_up(map->value_size, 8) * num_possible_cpus();
421 else if (IS_FD_MAP(map))
422 value_size = sizeof(u32);
424 value_size = map->value_size;
427 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
431 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
432 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
433 err = bpf_percpu_hash_copy(map, key, value);
434 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
435 err = bpf_percpu_array_copy(map, key, value);
436 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
437 err = bpf_stackmap_copy(map, key, value);
438 } else if (IS_FD_ARRAY(map)) {
439 err = bpf_fd_array_map_lookup_elem(map, key, value);
440 } else if (IS_FD_HASH(map)) {
441 err = bpf_fd_htab_map_lookup_elem(map, key, value);
444 ptr = map->ops->map_lookup_elem(map, key);
446 memcpy(value, ptr, value_size);
448 err = ptr ? 0 : -ENOENT;
455 if (copy_to_user(uvalue, value, value_size) != 0)
458 trace_bpf_map_lookup_elem(map, ufd, key, value);
470 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
472 static int map_update_elem(union bpf_attr *attr)
474 void __user *ukey = u64_to_user_ptr(attr->key);
475 void __user *uvalue = u64_to_user_ptr(attr->value);
476 int ufd = attr->map_fd;
483 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
487 map = __bpf_map_get(f);
492 key = kmalloc(map->key_size, GFP_USER);
497 if (copy_from_user(key, ukey, map->key_size) != 0)
500 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
501 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
502 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
503 value_size = round_up(map->value_size, 8) * num_possible_cpus();
505 value_size = map->value_size;
508 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
513 if (copy_from_user(value, uvalue, value_size) != 0)
516 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
517 * inside bpf map update or delete otherwise deadlocks are possible
520 __this_cpu_inc(bpf_prog_active);
521 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
522 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
523 err = bpf_percpu_hash_update(map, key, value, attr->flags);
524 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
525 err = bpf_percpu_array_update(map, key, value, attr->flags);
526 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
527 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
528 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
529 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
531 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
534 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
536 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
541 err = map->ops->map_update_elem(map, key, value, attr->flags);
544 __this_cpu_dec(bpf_prog_active);
548 trace_bpf_map_update_elem(map, ufd, key, value);
558 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
560 static int map_delete_elem(union bpf_attr *attr)
562 void __user *ukey = u64_to_user_ptr(attr->key);
563 int ufd = attr->map_fd;
569 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
573 map = __bpf_map_get(f);
578 key = kmalloc(map->key_size, GFP_USER);
583 if (copy_from_user(key, ukey, map->key_size) != 0)
587 __this_cpu_inc(bpf_prog_active);
589 err = map->ops->map_delete_elem(map, key);
591 __this_cpu_dec(bpf_prog_active);
595 trace_bpf_map_delete_elem(map, ufd, key);
603 /* last field in 'union bpf_attr' used by this command */
604 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
606 static int map_get_next_key(union bpf_attr *attr)
608 void __user *ukey = u64_to_user_ptr(attr->key);
609 void __user *unext_key = u64_to_user_ptr(attr->next_key);
610 int ufd = attr->map_fd;
612 void *key, *next_key;
616 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
620 map = __bpf_map_get(f);
626 key = kmalloc(map->key_size, GFP_USER);
631 if (copy_from_user(key, ukey, map->key_size) != 0)
638 next_key = kmalloc(map->key_size, GFP_USER);
643 err = map->ops->map_get_next_key(map, key, next_key);
649 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
652 trace_bpf_map_next_key(map, ufd, key, next_key);
664 static const struct bpf_verifier_ops * const bpf_prog_types[] = {
665 #define BPF_PROG_TYPE(_id, _ops) \
667 #define BPF_MAP_TYPE(_id, _ops)
668 #include <linux/bpf_types.h>
673 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
675 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
678 prog->aux->ops = bpf_prog_types[type];
683 /* drop refcnt on maps used by eBPF program and free auxilary data */
684 static void free_used_maps(struct bpf_prog_aux *aux)
688 for (i = 0; i < aux->used_map_cnt; i++)
689 bpf_map_put(aux->used_maps[i]);
691 kfree(aux->used_maps);
694 int __bpf_prog_charge(struct user_struct *user, u32 pages)
696 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
697 unsigned long user_bufs;
700 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
701 if (user_bufs > memlock_limit) {
702 atomic_long_sub(pages, &user->locked_vm);
710 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
713 atomic_long_sub(pages, &user->locked_vm);
716 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
718 struct user_struct *user = get_current_user();
721 ret = __bpf_prog_charge(user, prog->pages);
727 prog->aux->user = user;
731 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
733 struct user_struct *user = prog->aux->user;
735 __bpf_prog_uncharge(user, prog->pages);
739 static int bpf_prog_alloc_id(struct bpf_prog *prog)
743 spin_lock_bh(&prog_idr_lock);
744 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
747 spin_unlock_bh(&prog_idr_lock);
749 /* id is in [1, INT_MAX) */
750 if (WARN_ON_ONCE(!id))
753 return id > 0 ? 0 : id;
756 static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
758 /* cBPF to eBPF migrations are currently not in the idr store. */
763 spin_lock_bh(&prog_idr_lock);
765 __acquire(&prog_idr_lock);
767 idr_remove(&prog_idr, prog->aux->id);
770 spin_unlock_bh(&prog_idr_lock);
772 __release(&prog_idr_lock);
775 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
777 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
780 bpf_prog_uncharge_memlock(aux->prog);
781 bpf_prog_free(aux->prog);
784 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
786 if (atomic_dec_and_test(&prog->aux->refcnt)) {
787 trace_bpf_prog_put_rcu(prog);
788 /* bpf_prog_free_id() must be called first */
789 bpf_prog_free_id(prog, do_idr_lock);
790 bpf_prog_kallsyms_del(prog);
791 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
795 void bpf_prog_put(struct bpf_prog *prog)
797 __bpf_prog_put(prog, true);
799 EXPORT_SYMBOL_GPL(bpf_prog_put);
801 static int bpf_prog_release(struct inode *inode, struct file *filp)
803 struct bpf_prog *prog = filp->private_data;
809 #ifdef CONFIG_PROC_FS
810 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
812 const struct bpf_prog *prog = filp->private_data;
813 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
815 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
824 prog->pages * 1ULL << PAGE_SHIFT);
828 static const struct file_operations bpf_prog_fops = {
829 #ifdef CONFIG_PROC_FS
830 .show_fdinfo = bpf_prog_show_fdinfo,
832 .release = bpf_prog_release,
835 int bpf_prog_new_fd(struct bpf_prog *prog)
837 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
841 static struct bpf_prog *____bpf_prog_get(struct fd f)
844 return ERR_PTR(-EBADF);
845 if (f.file->f_op != &bpf_prog_fops) {
847 return ERR_PTR(-EINVAL);
850 return f.file->private_data;
853 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
855 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
856 atomic_sub(i, &prog->aux->refcnt);
857 return ERR_PTR(-EBUSY);
861 EXPORT_SYMBOL_GPL(bpf_prog_add);
863 void bpf_prog_sub(struct bpf_prog *prog, int i)
865 /* Only to be used for undoing previous bpf_prog_add() in some
866 * error path. We still know that another entity in our call
867 * path holds a reference to the program, thus atomic_sub() can
868 * be safely used in such cases!
870 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
872 EXPORT_SYMBOL_GPL(bpf_prog_sub);
874 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
876 return bpf_prog_add(prog, 1);
878 EXPORT_SYMBOL_GPL(bpf_prog_inc);
880 /* prog_idr_lock should have been held */
881 static struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
885 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
887 if (refold >= BPF_MAX_REFCNT) {
888 __bpf_prog_put(prog, false);
889 return ERR_PTR(-EBUSY);
893 return ERR_PTR(-ENOENT);
898 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
900 struct fd f = fdget(ufd);
901 struct bpf_prog *prog;
903 prog = ____bpf_prog_get(f);
906 if (type && prog->type != *type) {
907 prog = ERR_PTR(-EINVAL);
911 prog = bpf_prog_inc(prog);
917 struct bpf_prog *bpf_prog_get(u32 ufd)
919 return __bpf_prog_get(ufd, NULL);
922 struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
924 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
927 trace_bpf_prog_get_type(prog);
930 EXPORT_SYMBOL_GPL(bpf_prog_get_type);
932 /* last field in 'union bpf_attr' used by this command */
933 #define BPF_PROG_LOAD_LAST_FIELD prog_flags
935 static int bpf_prog_load(union bpf_attr *attr)
937 enum bpf_prog_type type = attr->prog_type;
938 struct bpf_prog *prog;
943 if (CHECK_ATTR(BPF_PROG_LOAD))
946 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
949 /* copy eBPF program license from user space */
950 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
951 sizeof(license) - 1) < 0)
953 license[sizeof(license) - 1] = 0;
955 /* eBPF programs must be GPL compatible to use GPL-ed functions */
956 is_gpl = license_is_gpl_compatible(license);
958 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
961 if (type == BPF_PROG_TYPE_KPROBE &&
962 attr->kern_version != LINUX_VERSION_CODE)
965 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
966 type != BPF_PROG_TYPE_CGROUP_SKB &&
967 !capable(CAP_SYS_ADMIN))
970 /* plain bpf_prog allocation */
971 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
975 err = bpf_prog_charge_memlock(prog);
977 goto free_prog_nouncharge;
979 prog->len = attr->insn_cnt;
982 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
983 bpf_prog_insn_size(prog)) != 0)
986 prog->orig_prog = NULL;
989 atomic_set(&prog->aux->refcnt, 1);
990 prog->gpl_compatible = is_gpl ? 1 : 0;
992 /* find program type: socket_filter vs tracing_filter */
993 err = find_prog_type(type, prog);
997 /* run eBPF verifier */
998 err = bpf_check(&prog, attr);
1000 goto free_used_maps;
1002 /* eBPF program is ready to be JITed */
1003 prog = bpf_prog_select_runtime(prog, &err);
1005 goto free_used_maps;
1007 err = bpf_prog_alloc_id(prog);
1009 goto free_used_maps;
1011 err = bpf_prog_new_fd(prog);
1013 /* failed to allocate fd.
1014 * bpf_prog_put() is needed because the above
1015 * bpf_prog_alloc_id() has published the prog
1016 * to the userspace and the userspace may
1017 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1023 bpf_prog_kallsyms_add(prog);
1024 trace_bpf_prog_load(prog, err);
1028 free_used_maps(prog->aux);
1030 bpf_prog_uncharge_memlock(prog);
1031 free_prog_nouncharge:
1032 bpf_prog_free(prog);
1036 #define BPF_OBJ_LAST_FIELD bpf_fd
1038 static int bpf_obj_pin(const union bpf_attr *attr)
1040 if (CHECK_ATTR(BPF_OBJ))
1043 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1046 static int bpf_obj_get(const union bpf_attr *attr)
1048 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
1051 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
1054 #ifdef CONFIG_CGROUP_BPF
1056 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1058 static int bpf_prog_attach(const union bpf_attr *attr)
1060 enum bpf_prog_type ptype;
1061 struct bpf_prog *prog;
1062 struct cgroup *cgrp;
1065 if (!capable(CAP_NET_ADMIN))
1068 if (CHECK_ATTR(BPF_PROG_ATTACH))
1071 if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
1074 switch (attr->attach_type) {
1075 case BPF_CGROUP_INET_INGRESS:
1076 case BPF_CGROUP_INET_EGRESS:
1077 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1079 case BPF_CGROUP_INET_SOCK_CREATE:
1080 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1082 case BPF_CGROUP_SOCK_OPS:
1083 ptype = BPF_PROG_TYPE_SOCK_OPS;
1089 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1091 return PTR_ERR(prog);
1093 cgrp = cgroup_get_from_fd(attr->target_fd);
1096 return PTR_ERR(cgrp);
1099 ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
1100 attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
1108 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1110 static int bpf_prog_detach(const union bpf_attr *attr)
1112 struct cgroup *cgrp;
1115 if (!capable(CAP_NET_ADMIN))
1118 if (CHECK_ATTR(BPF_PROG_DETACH))
1121 switch (attr->attach_type) {
1122 case BPF_CGROUP_INET_INGRESS:
1123 case BPF_CGROUP_INET_EGRESS:
1124 case BPF_CGROUP_INET_SOCK_CREATE:
1125 case BPF_CGROUP_SOCK_OPS:
1126 cgrp = cgroup_get_from_fd(attr->target_fd);
1128 return PTR_ERR(cgrp);
1130 ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
1141 #endif /* CONFIG_CGROUP_BPF */
1143 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1145 static int bpf_prog_test_run(const union bpf_attr *attr,
1146 union bpf_attr __user *uattr)
1148 struct bpf_prog *prog;
1149 int ret = -ENOTSUPP;
1151 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1154 prog = bpf_prog_get(attr->test.prog_fd);
1156 return PTR_ERR(prog);
1158 if (prog->aux->ops->test_run)
1159 ret = prog->aux->ops->test_run(prog, attr, uattr);
1165 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1167 static int bpf_obj_get_next_id(const union bpf_attr *attr,
1168 union bpf_attr __user *uattr,
1172 u32 next_id = attr->start_id;
1175 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1178 if (!capable(CAP_SYS_ADMIN))
1183 if (!idr_get_next(idr, &next_id))
1185 spin_unlock_bh(lock);
1188 err = put_user(next_id, &uattr->next_id);
1193 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1195 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1197 struct bpf_prog *prog;
1198 u32 id = attr->prog_id;
1201 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1204 if (!capable(CAP_SYS_ADMIN))
1207 spin_lock_bh(&prog_idr_lock);
1208 prog = idr_find(&prog_idr, id);
1210 prog = bpf_prog_inc_not_zero(prog);
1212 prog = ERR_PTR(-ENOENT);
1213 spin_unlock_bh(&prog_idr_lock);
1216 return PTR_ERR(prog);
1218 fd = bpf_prog_new_fd(prog);
1225 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD map_id
1227 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1229 struct bpf_map *map;
1230 u32 id = attr->map_id;
1233 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID))
1236 if (!capable(CAP_SYS_ADMIN))
1239 spin_lock_bh(&map_idr_lock);
1240 map = idr_find(&map_idr, id);
1242 map = bpf_map_inc_not_zero(map, true);
1244 map = ERR_PTR(-ENOENT);
1245 spin_unlock_bh(&map_idr_lock);
1248 return PTR_ERR(map);
1250 fd = bpf_map_new_fd(map);
1257 static int check_uarg_tail_zero(void __user *uaddr,
1258 size_t expected_size,
1261 unsigned char __user *addr;
1262 unsigned char __user *end;
1266 if (actual_size <= expected_size)
1269 addr = uaddr + expected_size;
1270 end = uaddr + actual_size;
1272 for (; addr < end; addr++) {
1273 err = get_user(val, addr);
1283 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1284 const union bpf_attr *attr,
1285 union bpf_attr __user *uattr)
1287 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1288 struct bpf_prog_info info = {};
1289 u32 info_len = attr->info.info_len;
1290 char __user *uinsns;
1294 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1297 info_len = min_t(u32, sizeof(info), info_len);
1299 if (copy_from_user(&info, uinfo, info_len))
1302 info.type = prog->type;
1303 info.id = prog->aux->id;
1305 memcpy(info.tag, prog->tag, sizeof(prog->tag));
1307 if (!capable(CAP_SYS_ADMIN)) {
1308 info.jited_prog_len = 0;
1309 info.xlated_prog_len = 0;
1313 ulen = info.jited_prog_len;
1314 info.jited_prog_len = prog->jited_len;
1315 if (info.jited_prog_len && ulen) {
1316 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1317 ulen = min_t(u32, info.jited_prog_len, ulen);
1318 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1322 ulen = info.xlated_prog_len;
1323 info.xlated_prog_len = bpf_prog_size(prog->len);
1324 if (info.xlated_prog_len && ulen) {
1325 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1326 ulen = min_t(u32, info.xlated_prog_len, ulen);
1327 if (copy_to_user(uinsns, prog->insnsi, ulen))
1332 if (copy_to_user(uinfo, &info, info_len) ||
1333 put_user(info_len, &uattr->info.info_len))
1339 static int bpf_map_get_info_by_fd(struct bpf_map *map,
1340 const union bpf_attr *attr,
1341 union bpf_attr __user *uattr)
1343 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1344 struct bpf_map_info info = {};
1345 u32 info_len = attr->info.info_len;
1348 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1351 info_len = min_t(u32, sizeof(info), info_len);
1353 info.type = map->map_type;
1355 info.key_size = map->key_size;
1356 info.value_size = map->value_size;
1357 info.max_entries = map->max_entries;
1358 info.map_flags = map->map_flags;
1360 if (copy_to_user(uinfo, &info, info_len) ||
1361 put_user(info_len, &uattr->info.info_len))
1367 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1369 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1370 union bpf_attr __user *uattr)
1372 int ufd = attr->info.bpf_fd;
1376 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1383 if (f.file->f_op == &bpf_prog_fops)
1384 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1386 else if (f.file->f_op == &bpf_map_fops)
1387 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1396 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1398 union bpf_attr attr = {};
1401 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
1404 if (!access_ok(VERIFY_READ, uattr, 1))
1407 if (size > PAGE_SIZE) /* silly large */
1410 /* If we're handed a bigger struct than we know of,
1411 * ensure all the unknown bits are 0 - i.e. new
1412 * user-space does not rely on any kernel feature
1413 * extensions we dont know about yet.
1415 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1418 size = min_t(u32, size, sizeof(attr));
1420 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1421 if (copy_from_user(&attr, uattr, size) != 0)
1425 case BPF_MAP_CREATE:
1426 err = map_create(&attr);
1428 case BPF_MAP_LOOKUP_ELEM:
1429 err = map_lookup_elem(&attr);
1431 case BPF_MAP_UPDATE_ELEM:
1432 err = map_update_elem(&attr);
1434 case BPF_MAP_DELETE_ELEM:
1435 err = map_delete_elem(&attr);
1437 case BPF_MAP_GET_NEXT_KEY:
1438 err = map_get_next_key(&attr);
1441 err = bpf_prog_load(&attr);
1444 err = bpf_obj_pin(&attr);
1447 err = bpf_obj_get(&attr);
1449 #ifdef CONFIG_CGROUP_BPF
1450 case BPF_PROG_ATTACH:
1451 err = bpf_prog_attach(&attr);
1453 case BPF_PROG_DETACH:
1454 err = bpf_prog_detach(&attr);
1457 case BPF_PROG_TEST_RUN:
1458 err = bpf_prog_test_run(&attr, uattr);
1460 case BPF_PROG_GET_NEXT_ID:
1461 err = bpf_obj_get_next_id(&attr, uattr,
1462 &prog_idr, &prog_idr_lock);
1464 case BPF_MAP_GET_NEXT_ID:
1465 err = bpf_obj_get_next_id(&attr, uattr,
1466 &map_idr, &map_idr_lock);
1468 case BPF_PROG_GET_FD_BY_ID:
1469 err = bpf_prog_get_fd_by_id(&attr);
1471 case BPF_MAP_GET_FD_BY_ID:
1472 err = bpf_map_get_fd_by_id(&attr);
1474 case BPF_OBJ_GET_INFO_BY_FD:
1475 err = bpf_obj_get_info_by_fd(&attr, uattr);