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/syscalls.h>
14 #include <linux/slab.h>
15 #include <linux/anon_inodes.h>
16 #include <linux/file.h>
17 #include <linux/license.h>
18 #include <linux/filter.h>
19 #include <linux/version.h>
21 DEFINE_PER_CPU(int, bpf_prog_active);
23 int sysctl_unprivileged_bpf_disabled __read_mostly;
25 static LIST_HEAD(bpf_map_types);
27 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
29 struct bpf_map_type_list *tl;
32 list_for_each_entry(tl, &bpf_map_types, list_node) {
33 if (tl->type == attr->map_type) {
34 map = tl->ops->map_alloc(attr);
38 map->map_type = attr->map_type;
42 return ERR_PTR(-EINVAL);
45 /* boot time registration of different map implementations */
46 void bpf_register_map_type(struct bpf_map_type_list *tl)
48 list_add(&tl->list_node, &bpf_map_types);
51 int bpf_map_precharge_memlock(u32 pages)
53 struct user_struct *user = get_current_user();
54 unsigned long memlock_limit, cur;
56 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
57 cur = atomic_long_read(&user->locked_vm);
59 if (cur + pages > memlock_limit)
64 static int bpf_map_charge_memlock(struct bpf_map *map)
66 struct user_struct *user = get_current_user();
67 unsigned long memlock_limit;
69 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
71 atomic_long_add(map->pages, &user->locked_vm);
73 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
74 atomic_long_sub(map->pages, &user->locked_vm);
82 static void bpf_map_uncharge_memlock(struct bpf_map *map)
84 struct user_struct *user = map->user;
86 atomic_long_sub(map->pages, &user->locked_vm);
90 /* called from workqueue */
91 static void bpf_map_free_deferred(struct work_struct *work)
93 struct bpf_map *map = container_of(work, struct bpf_map, work);
95 bpf_map_uncharge_memlock(map);
96 /* implementation dependent freeing */
97 map->ops->map_free(map);
100 static void bpf_map_put_uref(struct bpf_map *map)
102 if (atomic_dec_and_test(&map->usercnt)) {
103 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
104 bpf_fd_array_map_clear(map);
108 /* decrement map refcnt and schedule it for freeing via workqueue
109 * (unrelying map implementation ops->map_free() might sleep)
111 void bpf_map_put(struct bpf_map *map)
113 if (atomic_dec_and_test(&map->refcnt)) {
114 INIT_WORK(&map->work, bpf_map_free_deferred);
115 schedule_work(&map->work);
119 void bpf_map_put_with_uref(struct bpf_map *map)
121 bpf_map_put_uref(map);
125 static int bpf_map_release(struct inode *inode, struct file *filp)
127 bpf_map_put_with_uref(filp->private_data);
131 #ifdef CONFIG_PROC_FS
132 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
134 const struct bpf_map *map = filp->private_data;
150 static const struct file_operations bpf_map_fops = {
151 #ifdef CONFIG_PROC_FS
152 .show_fdinfo = bpf_map_show_fdinfo,
154 .release = bpf_map_release,
157 int bpf_map_new_fd(struct bpf_map *map)
159 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
163 /* helper macro to check that unused fields 'union bpf_attr' are zero */
164 #define CHECK_ATTR(CMD) \
165 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
166 sizeof(attr->CMD##_LAST_FIELD), 0, \
168 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
169 sizeof(attr->CMD##_LAST_FIELD)) != NULL
171 #define BPF_MAP_CREATE_LAST_FIELD map_flags
172 /* called via syscall */
173 static int map_create(union bpf_attr *attr)
178 err = CHECK_ATTR(BPF_MAP_CREATE);
182 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
183 map = find_and_alloc_map(attr);
187 atomic_set(&map->refcnt, 1);
188 atomic_set(&map->usercnt, 1);
190 err = bpf_map_charge_memlock(map);
194 err = bpf_map_new_fd(map);
196 /* failed to allocate fd */
202 map->ops->map_free(map);
206 /* if error is returned, fd is released.
207 * On success caller should complete fd access with matching fdput()
209 struct bpf_map *__bpf_map_get(struct fd f)
212 return ERR_PTR(-EBADF);
213 if (f.file->f_op != &bpf_map_fops) {
215 return ERR_PTR(-EINVAL);
218 return f.file->private_data;
221 /* prog's and map's refcnt limit */
222 #define BPF_MAX_REFCNT 32768
224 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
226 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
227 atomic_dec(&map->refcnt);
228 return ERR_PTR(-EBUSY);
231 atomic_inc(&map->usercnt);
235 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
237 struct fd f = fdget(ufd);
240 map = __bpf_map_get(f);
244 map = bpf_map_inc(map, true);
250 /* helper to convert user pointers passed inside __aligned_u64 fields */
251 static void __user *u64_to_ptr(__u64 val)
253 return (void __user *) (unsigned long) val;
256 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
261 /* last field in 'union bpf_attr' used by this command */
262 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
264 static int map_lookup_elem(union bpf_attr *attr)
266 void __user *ukey = u64_to_ptr(attr->key);
267 void __user *uvalue = u64_to_ptr(attr->value);
268 int ufd = attr->map_fd;
270 void *key, *value, *ptr;
275 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
279 map = __bpf_map_get(f);
284 key = kmalloc(map->key_size, GFP_USER);
289 if (copy_from_user(key, ukey, map->key_size) != 0)
292 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
293 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
294 value_size = round_up(map->value_size, 8) * num_possible_cpus();
296 value_size = map->value_size;
299 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
303 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
304 err = bpf_percpu_hash_copy(map, key, value);
305 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
306 err = bpf_percpu_array_copy(map, key, value);
307 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
308 err = bpf_stackmap_copy(map, key, value);
311 ptr = map->ops->map_lookup_elem(map, key);
313 memcpy(value, ptr, value_size);
315 err = ptr ? 0 : -ENOENT;
322 if (copy_to_user(uvalue, value, value_size) != 0)
336 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
338 static int map_update_elem(union bpf_attr *attr)
340 void __user *ukey = u64_to_ptr(attr->key);
341 void __user *uvalue = u64_to_ptr(attr->value);
342 int ufd = attr->map_fd;
349 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
353 map = __bpf_map_get(f);
358 key = kmalloc(map->key_size, GFP_USER);
363 if (copy_from_user(key, ukey, map->key_size) != 0)
366 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
367 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
368 value_size = round_up(map->value_size, 8) * num_possible_cpus();
370 value_size = map->value_size;
373 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
378 if (copy_from_user(value, uvalue, value_size) != 0)
381 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
382 * inside bpf map update or delete otherwise deadlocks are possible
385 __this_cpu_inc(bpf_prog_active);
386 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
387 err = bpf_percpu_hash_update(map, key, value, attr->flags);
388 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
389 err = bpf_percpu_array_update(map, key, value, attr->flags);
392 err = map->ops->map_update_elem(map, key, value, attr->flags);
395 __this_cpu_dec(bpf_prog_active);
407 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
409 static int map_delete_elem(union bpf_attr *attr)
411 void __user *ukey = u64_to_ptr(attr->key);
412 int ufd = attr->map_fd;
418 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
422 map = __bpf_map_get(f);
427 key = kmalloc(map->key_size, GFP_USER);
432 if (copy_from_user(key, ukey, map->key_size) != 0)
436 __this_cpu_inc(bpf_prog_active);
438 err = map->ops->map_delete_elem(map, key);
440 __this_cpu_dec(bpf_prog_active);
450 /* last field in 'union bpf_attr' used by this command */
451 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
453 static int map_get_next_key(union bpf_attr *attr)
455 void __user *ukey = u64_to_ptr(attr->key);
456 void __user *unext_key = u64_to_ptr(attr->next_key);
457 int ufd = attr->map_fd;
459 void *key, *next_key;
463 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
467 map = __bpf_map_get(f);
472 key = kmalloc(map->key_size, GFP_USER);
477 if (copy_from_user(key, ukey, map->key_size) != 0)
481 next_key = kmalloc(map->key_size, GFP_USER);
486 err = map->ops->map_get_next_key(map, key, next_key);
492 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
506 static LIST_HEAD(bpf_prog_types);
508 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
510 struct bpf_prog_type_list *tl;
512 list_for_each_entry(tl, &bpf_prog_types, list_node) {
513 if (tl->type == type) {
514 prog->aux->ops = tl->ops;
523 void bpf_register_prog_type(struct bpf_prog_type_list *tl)
525 list_add(&tl->list_node, &bpf_prog_types);
528 /* fixup insn->imm field of bpf_call instructions:
529 * if (insn->imm == BPF_FUNC_map_lookup_elem)
530 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
531 * else if (insn->imm == BPF_FUNC_map_update_elem)
532 * insn->imm = bpf_map_update_elem - __bpf_call_base;
535 * this function is called after eBPF program passed verification
537 static void fixup_bpf_calls(struct bpf_prog *prog)
539 const struct bpf_func_proto *fn;
542 for (i = 0; i < prog->len; i++) {
543 struct bpf_insn *insn = &prog->insnsi[i];
545 if (insn->code == (BPF_JMP | BPF_CALL)) {
546 /* we reach here when program has bpf_call instructions
547 * and it passed bpf_check(), means that
548 * ops->get_func_proto must have been supplied, check it
550 BUG_ON(!prog->aux->ops->get_func_proto);
552 if (insn->imm == BPF_FUNC_get_route_realm)
553 prog->dst_needed = 1;
554 if (insn->imm == BPF_FUNC_get_prandom_u32)
555 bpf_user_rnd_init_once();
556 if (insn->imm == BPF_FUNC_tail_call) {
557 /* mark bpf_tail_call as different opcode
558 * to avoid conditional branch in
559 * interpeter for every normal call
560 * and to prevent accidental JITing by
561 * JIT compiler that doesn't support
569 fn = prog->aux->ops->get_func_proto(insn->imm);
570 /* all functions that have prototype and verifier allowed
571 * programs to call them, must be real in-kernel functions
574 insn->imm = fn->func - __bpf_call_base;
579 /* drop refcnt on maps used by eBPF program and free auxilary data */
580 static void free_used_maps(struct bpf_prog_aux *aux)
584 for (i = 0; i < aux->used_map_cnt; i++)
585 bpf_map_put(aux->used_maps[i]);
587 kfree(aux->used_maps);
590 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
592 struct user_struct *user = get_current_user();
593 unsigned long memlock_limit;
595 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
597 atomic_long_add(prog->pages, &user->locked_vm);
598 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
599 atomic_long_sub(prog->pages, &user->locked_vm);
603 prog->aux->user = user;
607 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
609 struct user_struct *user = prog->aux->user;
611 atomic_long_sub(prog->pages, &user->locked_vm);
615 static void __prog_put_common(struct rcu_head *rcu)
617 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
620 bpf_prog_uncharge_memlock(aux->prog);
621 bpf_prog_free(aux->prog);
624 /* version of bpf_prog_put() that is called after a grace period */
625 void bpf_prog_put_rcu(struct bpf_prog *prog)
627 if (atomic_dec_and_test(&prog->aux->refcnt))
628 call_rcu(&prog->aux->rcu, __prog_put_common);
631 void bpf_prog_put(struct bpf_prog *prog)
633 if (atomic_dec_and_test(&prog->aux->refcnt))
634 __prog_put_common(&prog->aux->rcu);
636 EXPORT_SYMBOL_GPL(bpf_prog_put);
638 static int bpf_prog_release(struct inode *inode, struct file *filp)
640 struct bpf_prog *prog = filp->private_data;
642 bpf_prog_put_rcu(prog);
646 static const struct file_operations bpf_prog_fops = {
647 .release = bpf_prog_release,
650 int bpf_prog_new_fd(struct bpf_prog *prog)
652 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
656 static struct bpf_prog *__bpf_prog_get(struct fd f)
659 return ERR_PTR(-EBADF);
660 if (f.file->f_op != &bpf_prog_fops) {
662 return ERR_PTR(-EINVAL);
665 return f.file->private_data;
668 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
670 if (atomic_inc_return(&prog->aux->refcnt) > BPF_MAX_REFCNT) {
671 atomic_dec(&prog->aux->refcnt);
672 return ERR_PTR(-EBUSY);
677 /* called by sockets/tracing/seccomp before attaching program to an event
678 * pairs with bpf_prog_put()
680 struct bpf_prog *bpf_prog_get(u32 ufd)
682 struct fd f = fdget(ufd);
683 struct bpf_prog *prog;
685 prog = __bpf_prog_get(f);
689 prog = bpf_prog_inc(prog);
694 EXPORT_SYMBOL_GPL(bpf_prog_get);
696 /* last field in 'union bpf_attr' used by this command */
697 #define BPF_PROG_LOAD_LAST_FIELD kern_version
699 static int bpf_prog_load(union bpf_attr *attr)
701 enum bpf_prog_type type = attr->prog_type;
702 struct bpf_prog *prog;
707 if (CHECK_ATTR(BPF_PROG_LOAD))
710 /* copy eBPF program license from user space */
711 if (strncpy_from_user(license, u64_to_ptr(attr->license),
712 sizeof(license) - 1) < 0)
714 license[sizeof(license) - 1] = 0;
716 /* eBPF programs must be GPL compatible to use GPL-ed functions */
717 is_gpl = license_is_gpl_compatible(license);
719 if (attr->insn_cnt >= BPF_MAXINSNS)
722 if (type == BPF_PROG_TYPE_KPROBE &&
723 attr->kern_version != LINUX_VERSION_CODE)
726 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
729 /* plain bpf_prog allocation */
730 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
734 err = bpf_prog_charge_memlock(prog);
736 goto free_prog_nouncharge;
738 prog->len = attr->insn_cnt;
741 if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
742 prog->len * sizeof(struct bpf_insn)) != 0)
745 prog->orig_prog = NULL;
748 atomic_set(&prog->aux->refcnt, 1);
749 prog->gpl_compatible = is_gpl ? 1 : 0;
751 /* find program type: socket_filter vs tracing_filter */
752 err = find_prog_type(type, prog);
756 /* run eBPF verifier */
757 err = bpf_check(&prog, attr);
761 /* fixup BPF_CALL->imm field */
762 fixup_bpf_calls(prog);
764 /* eBPF program is ready to be JITed */
765 prog = bpf_prog_select_runtime(prog, &err);
769 err = bpf_prog_new_fd(prog);
771 /* failed to allocate fd */
777 free_used_maps(prog->aux);
779 bpf_prog_uncharge_memlock(prog);
780 free_prog_nouncharge:
785 #define BPF_OBJ_LAST_FIELD bpf_fd
787 static int bpf_obj_pin(const union bpf_attr *attr)
789 if (CHECK_ATTR(BPF_OBJ))
792 return bpf_obj_pin_user(attr->bpf_fd, u64_to_ptr(attr->pathname));
795 static int bpf_obj_get(const union bpf_attr *attr)
797 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
800 return bpf_obj_get_user(u64_to_ptr(attr->pathname));
803 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
805 union bpf_attr attr = {};
808 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
811 if (!access_ok(VERIFY_READ, uattr, 1))
814 if (size > PAGE_SIZE) /* silly large */
817 /* If we're handed a bigger struct than we know of,
818 * ensure all the unknown bits are 0 - i.e. new
819 * user-space does not rely on any kernel feature
820 * extensions we dont know about yet.
822 if (size > sizeof(attr)) {
823 unsigned char __user *addr;
824 unsigned char __user *end;
827 addr = (void __user *)uattr + sizeof(attr);
828 end = (void __user *)uattr + size;
830 for (; addr < end; addr++) {
831 err = get_user(val, addr);
840 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
841 if (copy_from_user(&attr, uattr, size) != 0)
846 err = map_create(&attr);
848 case BPF_MAP_LOOKUP_ELEM:
849 err = map_lookup_elem(&attr);
851 case BPF_MAP_UPDATE_ELEM:
852 err = map_update_elem(&attr);
854 case BPF_MAP_DELETE_ELEM:
855 err = map_delete_elem(&attr);
857 case BPF_MAP_GET_NEXT_KEY:
858 err = map_get_next_key(&attr);
861 err = bpf_prog_load(&attr);
864 err = bpf_obj_pin(&attr);
867 err = bpf_obj_get(&attr);