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>
26 #include <linux/cred.h>
27 #include <linux/timekeeping.h>
28 #include <linux/ctype.h>
30 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
31 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
32 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
33 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
34 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
35 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
37 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
39 DEFINE_PER_CPU(int, bpf_prog_active);
40 static DEFINE_IDR(prog_idr);
41 static DEFINE_SPINLOCK(prog_idr_lock);
42 static DEFINE_IDR(map_idr);
43 static DEFINE_SPINLOCK(map_idr_lock);
45 int sysctl_unprivileged_bpf_disabled __read_mostly;
47 static const struct bpf_map_ops * const bpf_map_types[] = {
48 #define BPF_PROG_TYPE(_id, _ops)
49 #define BPF_MAP_TYPE(_id, _ops) \
51 #include <linux/bpf_types.h>
57 * If we're handed a bigger struct than we know of, ensure all the unknown bits
58 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
59 * we don't know about yet.
61 * There is a ToCToU between this function call and the following
62 * copy_from_user() call. However, this is not a concern since this function is
63 * meant to be a future-proofing of bits.
65 static int check_uarg_tail_zero(void __user *uaddr,
69 unsigned char __user *addr;
70 unsigned char __user *end;
74 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
77 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
80 if (actual_size <= expected_size)
83 addr = uaddr + expected_size;
84 end = uaddr + actual_size;
86 for (; addr < end; addr++) {
87 err = get_user(val, addr);
97 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
101 if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
102 !bpf_map_types[attr->map_type])
103 return ERR_PTR(-EINVAL);
105 map = bpf_map_types[attr->map_type]->map_alloc(attr);
108 map->ops = bpf_map_types[attr->map_type];
109 map->map_type = attr->map_type;
113 void *bpf_map_area_alloc(size_t size, int numa_node)
115 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
116 * trigger under memory pressure as we really just want to
119 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
122 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
123 area = kmalloc_node(size, GFP_USER | flags, numa_node);
128 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
129 __builtin_return_address(0));
132 void bpf_map_area_free(void *area)
137 int bpf_map_precharge_memlock(u32 pages)
139 struct user_struct *user = get_current_user();
140 unsigned long memlock_limit, cur;
142 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
143 cur = atomic_long_read(&user->locked_vm);
145 if (cur + pages > memlock_limit)
150 static int bpf_map_charge_memlock(struct bpf_map *map)
152 struct user_struct *user = get_current_user();
153 unsigned long memlock_limit;
155 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
157 atomic_long_add(map->pages, &user->locked_vm);
159 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
160 atomic_long_sub(map->pages, &user->locked_vm);
168 static void bpf_map_uncharge_memlock(struct bpf_map *map)
170 struct user_struct *user = map->user;
172 atomic_long_sub(map->pages, &user->locked_vm);
176 static int bpf_map_alloc_id(struct bpf_map *map)
180 spin_lock_bh(&map_idr_lock);
181 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
184 spin_unlock_bh(&map_idr_lock);
186 if (WARN_ON_ONCE(!id))
189 return id > 0 ? 0 : id;
192 static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
197 spin_lock_irqsave(&map_idr_lock, flags);
199 __acquire(&map_idr_lock);
201 idr_remove(&map_idr, map->id);
204 spin_unlock_irqrestore(&map_idr_lock, flags);
206 __release(&map_idr_lock);
209 /* called from workqueue */
210 static void bpf_map_free_deferred(struct work_struct *work)
212 struct bpf_map *map = container_of(work, struct bpf_map, work);
214 bpf_map_uncharge_memlock(map);
215 security_bpf_map_free(map);
216 /* implementation dependent freeing */
217 map->ops->map_free(map);
220 static void bpf_map_put_uref(struct bpf_map *map)
222 if (atomic_dec_and_test(&map->usercnt)) {
223 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
224 bpf_fd_array_map_clear(map);
228 /* decrement map refcnt and schedule it for freeing via workqueue
229 * (unrelying map implementation ops->map_free() might sleep)
231 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
233 if (atomic_dec_and_test(&map->refcnt)) {
234 /* bpf_map_free_id() must be called first */
235 bpf_map_free_id(map, do_idr_lock);
236 INIT_WORK(&map->work, bpf_map_free_deferred);
237 schedule_work(&map->work);
241 void bpf_map_put(struct bpf_map *map)
243 __bpf_map_put(map, true);
246 void bpf_map_put_with_uref(struct bpf_map *map)
248 bpf_map_put_uref(map);
252 static int bpf_map_release(struct inode *inode, struct file *filp)
254 struct bpf_map *map = filp->private_data;
256 if (map->ops->map_release)
257 map->ops->map_release(map, filp);
259 bpf_map_put_with_uref(map);
263 #ifdef CONFIG_PROC_FS
264 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
266 const struct bpf_map *map = filp->private_data;
267 const struct bpf_array *array;
268 u32 owner_prog_type = 0;
271 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
272 array = container_of(map, struct bpf_array, map);
273 owner_prog_type = array->owner_prog_type;
274 owner_jited = array->owner_jited;
289 map->pages * 1ULL << PAGE_SHIFT);
291 if (owner_prog_type) {
292 seq_printf(m, "owner_prog_type:\t%u\n",
294 seq_printf(m, "owner_jited:\t%u\n",
300 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
303 /* We need this handler such that alloc_file() enables
304 * f_mode with FMODE_CAN_READ.
309 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
310 size_t siz, loff_t *ppos)
312 /* We need this handler such that alloc_file() enables
313 * f_mode with FMODE_CAN_WRITE.
318 const struct file_operations bpf_map_fops = {
319 #ifdef CONFIG_PROC_FS
320 .show_fdinfo = bpf_map_show_fdinfo,
322 .release = bpf_map_release,
323 .read = bpf_dummy_read,
324 .write = bpf_dummy_write,
327 int bpf_map_new_fd(struct bpf_map *map, int flags)
331 ret = security_bpf_map(map, OPEN_FMODE(flags));
335 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
339 int bpf_get_file_flag(int flags)
341 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
343 if (flags & BPF_F_RDONLY)
345 if (flags & BPF_F_WRONLY)
350 /* helper macro to check that unused fields 'union bpf_attr' are zero */
351 #define CHECK_ATTR(CMD) \
352 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
353 sizeof(attr->CMD##_LAST_FIELD), 0, \
355 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
356 sizeof(attr->CMD##_LAST_FIELD)) != NULL
358 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
359 * Return 0 on success and < 0 on error.
361 static int bpf_obj_name_cpy(char *dst, const char *src)
363 const char *end = src + BPF_OBJ_NAME_LEN;
365 memset(dst, 0, BPF_OBJ_NAME_LEN);
367 /* Copy all isalnum() and '_' char */
368 while (src < end && *src) {
369 if (!isalnum(*src) && *src != '_')
374 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
381 #define BPF_MAP_CREATE_LAST_FIELD map_name
382 /* called via syscall */
383 static int map_create(union bpf_attr *attr)
385 int numa_node = bpf_map_attr_numa_node(attr);
390 err = CHECK_ATTR(BPF_MAP_CREATE);
394 f_flags = bpf_get_file_flag(attr->map_flags);
398 if (numa_node != NUMA_NO_NODE &&
399 ((unsigned int)numa_node >= nr_node_ids ||
400 !node_online(numa_node)))
403 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
404 map = find_and_alloc_map(attr);
408 err = bpf_obj_name_cpy(map->name, attr->map_name);
410 goto free_map_nouncharge;
412 atomic_set(&map->refcnt, 1);
413 atomic_set(&map->usercnt, 1);
415 err = security_bpf_map_alloc(map);
417 goto free_map_nouncharge;
419 err = bpf_map_charge_memlock(map);
423 err = bpf_map_alloc_id(map);
427 err = bpf_map_new_fd(map, f_flags);
429 /* failed to allocate fd.
430 * bpf_map_put() is needed because the above
431 * bpf_map_alloc_id() has published the map
432 * to the userspace and the userspace may
433 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
439 trace_bpf_map_create(map, err);
443 bpf_map_uncharge_memlock(map);
445 security_bpf_map_free(map);
447 map->ops->map_free(map);
451 /* if error is returned, fd is released.
452 * On success caller should complete fd access with matching fdput()
454 struct bpf_map *__bpf_map_get(struct fd f)
457 return ERR_PTR(-EBADF);
458 if (f.file->f_op != &bpf_map_fops) {
460 return ERR_PTR(-EINVAL);
463 return f.file->private_data;
466 /* prog's and map's refcnt limit */
467 #define BPF_MAX_REFCNT 32768
469 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
471 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
472 atomic_dec(&map->refcnt);
473 return ERR_PTR(-EBUSY);
476 atomic_inc(&map->usercnt);
480 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
482 struct fd f = fdget(ufd);
485 map = __bpf_map_get(f);
489 map = bpf_map_inc(map, true);
495 /* map_idr_lock should have been held */
496 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
501 refold = __atomic_add_unless(&map->refcnt, 1, 0);
503 if (refold >= BPF_MAX_REFCNT) {
504 __bpf_map_put(map, false);
505 return ERR_PTR(-EBUSY);
509 return ERR_PTR(-ENOENT);
512 atomic_inc(&map->usercnt);
517 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
522 /* last field in 'union bpf_attr' used by this command */
523 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
525 static int map_lookup_elem(union bpf_attr *attr)
527 void __user *ukey = u64_to_user_ptr(attr->key);
528 void __user *uvalue = u64_to_user_ptr(attr->value);
529 int ufd = attr->map_fd;
531 void *key, *value, *ptr;
536 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
540 map = __bpf_map_get(f);
544 if (!(f.file->f_mode & FMODE_CAN_READ)) {
549 key = memdup_user(ukey, map->key_size);
555 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
556 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
557 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
558 value_size = round_up(map->value_size, 8) * num_possible_cpus();
559 else if (IS_FD_MAP(map))
560 value_size = sizeof(u32);
562 value_size = map->value_size;
565 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
569 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
570 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
571 err = bpf_percpu_hash_copy(map, key, value);
572 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
573 err = bpf_percpu_array_copy(map, key, value);
574 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
575 err = bpf_stackmap_copy(map, key, value);
576 } else if (IS_FD_ARRAY(map)) {
577 err = bpf_fd_array_map_lookup_elem(map, key, value);
578 } else if (IS_FD_HASH(map)) {
579 err = bpf_fd_htab_map_lookup_elem(map, key, value);
582 ptr = map->ops->map_lookup_elem(map, key);
584 memcpy(value, ptr, value_size);
586 err = ptr ? 0 : -ENOENT;
593 if (copy_to_user(uvalue, value, value_size) != 0)
596 trace_bpf_map_lookup_elem(map, ufd, key, value);
608 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
610 static int map_update_elem(union bpf_attr *attr)
612 void __user *ukey = u64_to_user_ptr(attr->key);
613 void __user *uvalue = u64_to_user_ptr(attr->value);
614 int ufd = attr->map_fd;
621 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
625 map = __bpf_map_get(f);
629 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
634 key = memdup_user(ukey, map->key_size);
640 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
641 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
642 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
643 value_size = round_up(map->value_size, 8) * num_possible_cpus();
645 value_size = map->value_size;
648 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
653 if (copy_from_user(value, uvalue, value_size) != 0)
656 /* Need to create a kthread, thus must support schedule */
657 if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
658 err = map->ops->map_update_elem(map, key, value, attr->flags);
662 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
663 * inside bpf map update or delete otherwise deadlocks are possible
666 __this_cpu_inc(bpf_prog_active);
667 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
668 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
669 err = bpf_percpu_hash_update(map, key, value, attr->flags);
670 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
671 err = bpf_percpu_array_update(map, key, value, attr->flags);
672 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
673 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
674 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
675 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
677 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
680 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
682 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
687 err = map->ops->map_update_elem(map, key, value, attr->flags);
690 __this_cpu_dec(bpf_prog_active);
694 trace_bpf_map_update_elem(map, ufd, key, value);
704 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
706 static int map_delete_elem(union bpf_attr *attr)
708 void __user *ukey = u64_to_user_ptr(attr->key);
709 int ufd = attr->map_fd;
715 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
719 map = __bpf_map_get(f);
723 if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
728 key = memdup_user(ukey, map->key_size);
735 __this_cpu_inc(bpf_prog_active);
737 err = map->ops->map_delete_elem(map, key);
739 __this_cpu_dec(bpf_prog_active);
743 trace_bpf_map_delete_elem(map, ufd, key);
750 /* last field in 'union bpf_attr' used by this command */
751 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
753 static int map_get_next_key(union bpf_attr *attr)
755 void __user *ukey = u64_to_user_ptr(attr->key);
756 void __user *unext_key = u64_to_user_ptr(attr->next_key);
757 int ufd = attr->map_fd;
759 void *key, *next_key;
763 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
767 map = __bpf_map_get(f);
771 if (!(f.file->f_mode & FMODE_CAN_READ)) {
777 key = memdup_user(ukey, map->key_size);
787 next_key = kmalloc(map->key_size, GFP_USER);
792 err = map->ops->map_get_next_key(map, key, next_key);
798 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
801 trace_bpf_map_next_key(map, ufd, key, next_key);
813 static const struct bpf_prog_ops * const bpf_prog_types[] = {
814 #define BPF_PROG_TYPE(_id, _name) \
815 [_id] = & _name ## _prog_ops,
816 #define BPF_MAP_TYPE(_id, _ops)
817 #include <linux/bpf_types.h>
822 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
824 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
827 if (!bpf_prog_is_dev_bound(prog->aux))
828 prog->aux->ops = bpf_prog_types[type];
830 prog->aux->ops = &bpf_offload_prog_ops;
835 /* drop refcnt on maps used by eBPF program and free auxilary data */
836 static void free_used_maps(struct bpf_prog_aux *aux)
840 for (i = 0; i < aux->used_map_cnt; i++)
841 bpf_map_put(aux->used_maps[i]);
843 kfree(aux->used_maps);
846 int __bpf_prog_charge(struct user_struct *user, u32 pages)
848 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
849 unsigned long user_bufs;
852 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
853 if (user_bufs > memlock_limit) {
854 atomic_long_sub(pages, &user->locked_vm);
862 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
865 atomic_long_sub(pages, &user->locked_vm);
868 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
870 struct user_struct *user = get_current_user();
873 ret = __bpf_prog_charge(user, prog->pages);
879 prog->aux->user = user;
883 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
885 struct user_struct *user = prog->aux->user;
887 __bpf_prog_uncharge(user, prog->pages);
891 static int bpf_prog_alloc_id(struct bpf_prog *prog)
895 spin_lock_bh(&prog_idr_lock);
896 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
899 spin_unlock_bh(&prog_idr_lock);
901 /* id is in [1, INT_MAX) */
902 if (WARN_ON_ONCE(!id))
905 return id > 0 ? 0 : id;
908 static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
910 /* cBPF to eBPF migrations are currently not in the idr store. */
915 spin_lock_bh(&prog_idr_lock);
917 __acquire(&prog_idr_lock);
919 idr_remove(&prog_idr, prog->aux->id);
922 spin_unlock_bh(&prog_idr_lock);
924 __release(&prog_idr_lock);
927 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
929 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
932 bpf_prog_uncharge_memlock(aux->prog);
933 security_bpf_prog_free(aux);
934 bpf_prog_free(aux->prog);
937 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
939 if (atomic_dec_and_test(&prog->aux->refcnt)) {
940 trace_bpf_prog_put_rcu(prog);
941 /* bpf_prog_free_id() must be called first */
942 bpf_prog_free_id(prog, do_idr_lock);
943 bpf_prog_kallsyms_del(prog);
944 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
948 void bpf_prog_put(struct bpf_prog *prog)
950 __bpf_prog_put(prog, true);
952 EXPORT_SYMBOL_GPL(bpf_prog_put);
954 static int bpf_prog_release(struct inode *inode, struct file *filp)
956 struct bpf_prog *prog = filp->private_data;
962 #ifdef CONFIG_PROC_FS
963 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
965 const struct bpf_prog *prog = filp->private_data;
966 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
968 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
977 prog->pages * 1ULL << PAGE_SHIFT);
981 const struct file_operations bpf_prog_fops = {
982 #ifdef CONFIG_PROC_FS
983 .show_fdinfo = bpf_prog_show_fdinfo,
985 .release = bpf_prog_release,
986 .read = bpf_dummy_read,
987 .write = bpf_dummy_write,
990 int bpf_prog_new_fd(struct bpf_prog *prog)
994 ret = security_bpf_prog(prog);
998 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1002 static struct bpf_prog *____bpf_prog_get(struct fd f)
1005 return ERR_PTR(-EBADF);
1006 if (f.file->f_op != &bpf_prog_fops) {
1008 return ERR_PTR(-EINVAL);
1011 return f.file->private_data;
1014 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
1016 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1017 atomic_sub(i, &prog->aux->refcnt);
1018 return ERR_PTR(-EBUSY);
1022 EXPORT_SYMBOL_GPL(bpf_prog_add);
1024 void bpf_prog_sub(struct bpf_prog *prog, int i)
1026 /* Only to be used for undoing previous bpf_prog_add() in some
1027 * error path. We still know that another entity in our call
1028 * path holds a reference to the program, thus atomic_sub() can
1029 * be safely used in such cases!
1031 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1033 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1035 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1037 return bpf_prog_add(prog, 1);
1039 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1041 /* prog_idr_lock should have been held */
1042 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1046 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1048 if (refold >= BPF_MAX_REFCNT) {
1049 __bpf_prog_put(prog, false);
1050 return ERR_PTR(-EBUSY);
1054 return ERR_PTR(-ENOENT);
1058 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1060 static bool bpf_prog_can_attach(struct bpf_prog *prog,
1061 enum bpf_prog_type *attach_type,
1062 struct net_device *netdev)
1064 struct bpf_dev_offload *offload = prog->aux->offload;
1066 if (prog->type != *attach_type)
1068 if (offload && offload->netdev != netdev)
1074 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1075 struct net_device *netdev)
1077 struct fd f = fdget(ufd);
1078 struct bpf_prog *prog;
1080 prog = ____bpf_prog_get(f);
1083 if (attach_type && !bpf_prog_can_attach(prog, attach_type, netdev)) {
1084 prog = ERR_PTR(-EINVAL);
1088 prog = bpf_prog_inc(prog);
1094 struct bpf_prog *bpf_prog_get(u32 ufd)
1096 return __bpf_prog_get(ufd, NULL, NULL);
1099 struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
1101 struct bpf_prog *prog = __bpf_prog_get(ufd, &type, NULL);
1104 trace_bpf_prog_get_type(prog);
1107 EXPORT_SYMBOL_GPL(bpf_prog_get_type);
1109 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1110 struct net_device *netdev)
1112 struct bpf_prog *prog = __bpf_prog_get(ufd, &type, netdev);
1115 trace_bpf_prog_get_type(prog);
1118 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1120 /* last field in 'union bpf_attr' used by this command */
1121 #define BPF_PROG_LOAD_LAST_FIELD prog_target_ifindex
1123 static int bpf_prog_load(union bpf_attr *attr)
1125 enum bpf_prog_type type = attr->prog_type;
1126 struct bpf_prog *prog;
1131 if (CHECK_ATTR(BPF_PROG_LOAD))
1134 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1137 /* copy eBPF program license from user space */
1138 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1139 sizeof(license) - 1) < 0)
1141 license[sizeof(license) - 1] = 0;
1143 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1144 is_gpl = license_is_gpl_compatible(license);
1146 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1149 if (type == BPF_PROG_TYPE_KPROBE &&
1150 attr->kern_version != LINUX_VERSION_CODE)
1153 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1154 type != BPF_PROG_TYPE_CGROUP_SKB &&
1155 !capable(CAP_SYS_ADMIN))
1158 /* plain bpf_prog allocation */
1159 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1163 err = security_bpf_prog_alloc(prog->aux);
1165 goto free_prog_nouncharge;
1167 err = bpf_prog_charge_memlock(prog);
1171 prog->len = attr->insn_cnt;
1174 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1175 bpf_prog_insn_size(prog)) != 0)
1178 prog->orig_prog = NULL;
1181 atomic_set(&prog->aux->refcnt, 1);
1182 prog->gpl_compatible = is_gpl ? 1 : 0;
1184 if (attr->prog_target_ifindex) {
1185 err = bpf_prog_offload_init(prog, attr);
1190 /* find program type: socket_filter vs tracing_filter */
1191 err = find_prog_type(type, prog);
1195 prog->aux->load_time = ktime_get_boot_ns();
1196 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1200 /* run eBPF verifier */
1201 err = bpf_check(&prog, attr);
1203 goto free_used_maps;
1205 /* eBPF program is ready to be JITed */
1206 prog = bpf_prog_select_runtime(prog, &err);
1208 goto free_used_maps;
1210 err = bpf_prog_alloc_id(prog);
1212 goto free_used_maps;
1214 err = bpf_prog_new_fd(prog);
1216 /* failed to allocate fd.
1217 * bpf_prog_put() is needed because the above
1218 * bpf_prog_alloc_id() has published the prog
1219 * to the userspace and the userspace may
1220 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1226 bpf_prog_kallsyms_add(prog);
1227 trace_bpf_prog_load(prog, err);
1231 free_used_maps(prog->aux);
1233 bpf_prog_uncharge_memlock(prog);
1235 security_bpf_prog_free(prog->aux);
1236 free_prog_nouncharge:
1237 bpf_prog_free(prog);
1241 #define BPF_OBJ_LAST_FIELD file_flags
1243 static int bpf_obj_pin(const union bpf_attr *attr)
1245 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1248 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1251 static int bpf_obj_get(const union bpf_attr *attr)
1253 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1254 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1257 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1261 #ifdef CONFIG_CGROUP_BPF
1263 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1265 static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
1267 struct bpf_prog *prog = NULL;
1268 int ufd = attr->target_fd;
1269 struct bpf_map *map;
1274 map = __bpf_map_get(f);
1276 return PTR_ERR(map);
1279 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1280 BPF_PROG_TYPE_SK_SKB);
1283 return PTR_ERR(prog);
1287 err = sock_map_prog(map, prog, attr->attach_type);
1299 #define BPF_F_ATTACH_MASK \
1300 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1302 static int bpf_prog_attach(const union bpf_attr *attr)
1304 enum bpf_prog_type ptype;
1305 struct bpf_prog *prog;
1306 struct cgroup *cgrp;
1309 if (!capable(CAP_NET_ADMIN))
1312 if (CHECK_ATTR(BPF_PROG_ATTACH))
1315 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
1318 switch (attr->attach_type) {
1319 case BPF_CGROUP_INET_INGRESS:
1320 case BPF_CGROUP_INET_EGRESS:
1321 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1323 case BPF_CGROUP_INET_SOCK_CREATE:
1324 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1326 case BPF_CGROUP_SOCK_OPS:
1327 ptype = BPF_PROG_TYPE_SOCK_OPS;
1329 case BPF_CGROUP_DEVICE:
1330 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1332 case BPF_SK_SKB_STREAM_PARSER:
1333 case BPF_SK_SKB_STREAM_VERDICT:
1334 return sockmap_get_from_fd(attr, true);
1339 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1341 return PTR_ERR(prog);
1343 cgrp = cgroup_get_from_fd(attr->target_fd);
1346 return PTR_ERR(cgrp);
1349 ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1350 attr->attach_flags);
1358 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1360 static int bpf_prog_detach(const union bpf_attr *attr)
1362 enum bpf_prog_type ptype;
1363 struct bpf_prog *prog;
1364 struct cgroup *cgrp;
1367 if (!capable(CAP_NET_ADMIN))
1370 if (CHECK_ATTR(BPF_PROG_DETACH))
1373 switch (attr->attach_type) {
1374 case BPF_CGROUP_INET_INGRESS:
1375 case BPF_CGROUP_INET_EGRESS:
1376 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1378 case BPF_CGROUP_INET_SOCK_CREATE:
1379 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1381 case BPF_CGROUP_SOCK_OPS:
1382 ptype = BPF_PROG_TYPE_SOCK_OPS;
1384 case BPF_CGROUP_DEVICE:
1385 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1387 case BPF_SK_SKB_STREAM_PARSER:
1388 case BPF_SK_SKB_STREAM_VERDICT:
1389 return sockmap_get_from_fd(attr, false);
1394 cgrp = cgroup_get_from_fd(attr->target_fd);
1396 return PTR_ERR(cgrp);
1398 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1402 ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1409 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1411 static int bpf_prog_query(const union bpf_attr *attr,
1412 union bpf_attr __user *uattr)
1414 struct cgroup *cgrp;
1417 if (!capable(CAP_NET_ADMIN))
1419 if (CHECK_ATTR(BPF_PROG_QUERY))
1421 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1424 switch (attr->query.attach_type) {
1425 case BPF_CGROUP_INET_INGRESS:
1426 case BPF_CGROUP_INET_EGRESS:
1427 case BPF_CGROUP_INET_SOCK_CREATE:
1428 case BPF_CGROUP_SOCK_OPS:
1429 case BPF_CGROUP_DEVICE:
1434 cgrp = cgroup_get_from_fd(attr->query.target_fd);
1436 return PTR_ERR(cgrp);
1437 ret = cgroup_bpf_query(cgrp, attr, uattr);
1441 #endif /* CONFIG_CGROUP_BPF */
1443 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1445 static int bpf_prog_test_run(const union bpf_attr *attr,
1446 union bpf_attr __user *uattr)
1448 struct bpf_prog *prog;
1449 int ret = -ENOTSUPP;
1451 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1454 prog = bpf_prog_get(attr->test.prog_fd);
1456 return PTR_ERR(prog);
1458 if (prog->aux->ops->test_run)
1459 ret = prog->aux->ops->test_run(prog, attr, uattr);
1465 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1467 static int bpf_obj_get_next_id(const union bpf_attr *attr,
1468 union bpf_attr __user *uattr,
1472 u32 next_id = attr->start_id;
1475 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1478 if (!capable(CAP_SYS_ADMIN))
1483 if (!idr_get_next(idr, &next_id))
1485 spin_unlock_bh(lock);
1488 err = put_user(next_id, &uattr->next_id);
1493 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1495 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1497 struct bpf_prog *prog;
1498 u32 id = attr->prog_id;
1501 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1504 if (!capable(CAP_SYS_ADMIN))
1507 spin_lock_bh(&prog_idr_lock);
1508 prog = idr_find(&prog_idr, id);
1510 prog = bpf_prog_inc_not_zero(prog);
1512 prog = ERR_PTR(-ENOENT);
1513 spin_unlock_bh(&prog_idr_lock);
1516 return PTR_ERR(prog);
1518 fd = bpf_prog_new_fd(prog);
1525 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
1527 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1529 struct bpf_map *map;
1530 u32 id = attr->map_id;
1534 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1535 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
1538 if (!capable(CAP_SYS_ADMIN))
1541 f_flags = bpf_get_file_flag(attr->open_flags);
1545 spin_lock_bh(&map_idr_lock);
1546 map = idr_find(&map_idr, id);
1548 map = bpf_map_inc_not_zero(map, true);
1550 map = ERR_PTR(-ENOENT);
1551 spin_unlock_bh(&map_idr_lock);
1554 return PTR_ERR(map);
1556 fd = bpf_map_new_fd(map, f_flags);
1563 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1564 const union bpf_attr *attr,
1565 union bpf_attr __user *uattr)
1567 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1568 struct bpf_prog_info info = {};
1569 u32 info_len = attr->info.info_len;
1570 char __user *uinsns;
1574 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1577 info_len = min_t(u32, sizeof(info), info_len);
1579 if (copy_from_user(&info, uinfo, info_len))
1582 info.type = prog->type;
1583 info.id = prog->aux->id;
1584 info.load_time = prog->aux->load_time;
1585 info.created_by_uid = from_kuid_munged(current_user_ns(),
1586 prog->aux->user->uid);
1588 memcpy(info.tag, prog->tag, sizeof(prog->tag));
1589 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1591 ulen = info.nr_map_ids;
1592 info.nr_map_ids = prog->aux->used_map_cnt;
1593 ulen = min_t(u32, info.nr_map_ids, ulen);
1595 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
1598 for (i = 0; i < ulen; i++)
1599 if (put_user(prog->aux->used_maps[i]->id,
1604 if (!capable(CAP_SYS_ADMIN)) {
1605 info.jited_prog_len = 0;
1606 info.xlated_prog_len = 0;
1610 ulen = info.jited_prog_len;
1611 info.jited_prog_len = prog->jited_len;
1612 if (info.jited_prog_len && ulen) {
1613 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1614 ulen = min_t(u32, info.jited_prog_len, ulen);
1615 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1619 ulen = info.xlated_prog_len;
1620 info.xlated_prog_len = bpf_prog_insn_size(prog);
1621 if (info.xlated_prog_len && ulen) {
1622 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1623 ulen = min_t(u32, info.xlated_prog_len, ulen);
1624 if (copy_to_user(uinsns, prog->insnsi, ulen))
1628 if (bpf_prog_is_dev_bound(prog->aux)) {
1629 info.status |= BPF_PROG_STATUS_DEV_BOUND;
1630 info.ifindex = bpf_prog_offload_ifindex(prog);
1634 if (copy_to_user(uinfo, &info, info_len) ||
1635 put_user(info_len, &uattr->info.info_len))
1641 static int bpf_map_get_info_by_fd(struct bpf_map *map,
1642 const union bpf_attr *attr,
1643 union bpf_attr __user *uattr)
1645 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1646 struct bpf_map_info info = {};
1647 u32 info_len = attr->info.info_len;
1650 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1653 info_len = min_t(u32, sizeof(info), info_len);
1655 info.type = map->map_type;
1657 info.key_size = map->key_size;
1658 info.value_size = map->value_size;
1659 info.max_entries = map->max_entries;
1660 info.map_flags = map->map_flags;
1661 memcpy(info.name, map->name, sizeof(map->name));
1663 if (copy_to_user(uinfo, &info, info_len) ||
1664 put_user(info_len, &uattr->info.info_len))
1670 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1672 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1673 union bpf_attr __user *uattr)
1675 int ufd = attr->info.bpf_fd;
1679 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1686 if (f.file->f_op == &bpf_prog_fops)
1687 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1689 else if (f.file->f_op == &bpf_map_fops)
1690 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1699 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1701 union bpf_attr attr = {};
1704 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
1707 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1710 size = min_t(u32, size, sizeof(attr));
1712 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1713 if (copy_from_user(&attr, uattr, size) != 0)
1716 err = security_bpf(cmd, &attr, size);
1721 case BPF_MAP_CREATE:
1722 err = map_create(&attr);
1724 case BPF_MAP_LOOKUP_ELEM:
1725 err = map_lookup_elem(&attr);
1727 case BPF_MAP_UPDATE_ELEM:
1728 err = map_update_elem(&attr);
1730 case BPF_MAP_DELETE_ELEM:
1731 err = map_delete_elem(&attr);
1733 case BPF_MAP_GET_NEXT_KEY:
1734 err = map_get_next_key(&attr);
1737 err = bpf_prog_load(&attr);
1740 err = bpf_obj_pin(&attr);
1743 err = bpf_obj_get(&attr);
1745 #ifdef CONFIG_CGROUP_BPF
1746 case BPF_PROG_ATTACH:
1747 err = bpf_prog_attach(&attr);
1749 case BPF_PROG_DETACH:
1750 err = bpf_prog_detach(&attr);
1752 case BPF_PROG_QUERY:
1753 err = bpf_prog_query(&attr, uattr);
1756 case BPF_PROG_TEST_RUN:
1757 err = bpf_prog_test_run(&attr, uattr);
1759 case BPF_PROG_GET_NEXT_ID:
1760 err = bpf_obj_get_next_id(&attr, uattr,
1761 &prog_idr, &prog_idr_lock);
1763 case BPF_MAP_GET_NEXT_ID:
1764 err = bpf_obj_get_next_id(&attr, uattr,
1765 &map_idr, &map_idr_lock);
1767 case BPF_PROG_GET_FD_BY_ID:
1768 err = bpf_prog_get_fd_by_id(&attr);
1770 case BPF_MAP_GET_FD_BY_ID:
1771 err = bpf_map_get_fd_by_id(&attr);
1773 case BPF_OBJ_GET_INFO_BY_FD:
1774 err = bpf_obj_get_info_by_fd(&attr, uattr);