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 int sysctl_unprivileged_bpf_disabled __read_mostly;
23 static LIST_HEAD(bpf_map_types);
25 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
27 struct bpf_map_type_list *tl;
30 list_for_each_entry(tl, &bpf_map_types, list_node) {
31 if (tl->type == attr->map_type) {
32 map = tl->ops->map_alloc(attr);
36 map->map_type = attr->map_type;
40 return ERR_PTR(-EINVAL);
43 /* boot time registration of different map implementations */
44 void bpf_register_map_type(struct bpf_map_type_list *tl)
46 list_add(&tl->list_node, &bpf_map_types);
49 static int bpf_map_charge_memlock(struct bpf_map *map)
51 struct user_struct *user = get_current_user();
52 unsigned long memlock_limit;
54 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
56 atomic_long_add(map->pages, &user->locked_vm);
58 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
59 atomic_long_sub(map->pages, &user->locked_vm);
67 static void bpf_map_uncharge_memlock(struct bpf_map *map)
69 struct user_struct *user = map->user;
71 atomic_long_sub(map->pages, &user->locked_vm);
75 /* called from workqueue */
76 static void bpf_map_free_deferred(struct work_struct *work)
78 struct bpf_map *map = container_of(work, struct bpf_map, work);
80 bpf_map_uncharge_memlock(map);
81 /* implementation dependent freeing */
82 map->ops->map_free(map);
85 /* decrement map refcnt and schedule it for freeing via workqueue
86 * (unrelying map implementation ops->map_free() might sleep)
88 void bpf_map_put(struct bpf_map *map)
90 if (atomic_dec_and_test(&map->refcnt)) {
91 INIT_WORK(&map->work, bpf_map_free_deferred);
92 schedule_work(&map->work);
96 static int bpf_map_release(struct inode *inode, struct file *filp)
98 struct bpf_map *map = filp->private_data;
100 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
101 /* prog_array stores refcnt-ed bpf_prog pointers
102 * release them all when user space closes prog_array_fd
104 bpf_fd_array_map_clear(map);
110 static const struct file_operations bpf_map_fops = {
111 .release = bpf_map_release,
114 /* helper macro to check that unused fields 'union bpf_attr' are zero */
115 #define CHECK_ATTR(CMD) \
116 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
117 sizeof(attr->CMD##_LAST_FIELD), 0, \
119 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
120 sizeof(attr->CMD##_LAST_FIELD)) != NULL
122 #define BPF_MAP_CREATE_LAST_FIELD max_entries
123 /* called via syscall */
124 static int map_create(union bpf_attr *attr)
129 err = CHECK_ATTR(BPF_MAP_CREATE);
133 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
134 map = find_and_alloc_map(attr);
138 atomic_set(&map->refcnt, 1);
140 err = bpf_map_charge_memlock(map);
144 err = anon_inode_getfd("bpf-map", &bpf_map_fops, map, O_RDWR | O_CLOEXEC);
147 /* failed to allocate fd */
153 map->ops->map_free(map);
157 /* if error is returned, fd is released.
158 * On success caller should complete fd access with matching fdput()
160 struct bpf_map *bpf_map_get(struct fd f)
165 return ERR_PTR(-EBADF);
167 if (f.file->f_op != &bpf_map_fops) {
169 return ERR_PTR(-EINVAL);
172 map = f.file->private_data;
177 /* helper to convert user pointers passed inside __aligned_u64 fields */
178 static void __user *u64_to_ptr(__u64 val)
180 return (void __user *) (unsigned long) val;
183 /* last field in 'union bpf_attr' used by this command */
184 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
186 static int map_lookup_elem(union bpf_attr *attr)
188 void __user *ukey = u64_to_ptr(attr->key);
189 void __user *uvalue = u64_to_ptr(attr->value);
190 int ufd = attr->map_fd;
192 void *key, *value, *ptr;
196 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
200 map = bpf_map_get(f);
205 key = kmalloc(map->key_size, GFP_USER);
210 if (copy_from_user(key, ukey, map->key_size) != 0)
214 value = kmalloc(map->value_size, GFP_USER);
219 ptr = map->ops->map_lookup_elem(map, key);
221 memcpy(value, ptr, map->value_size);
229 if (copy_to_user(uvalue, value, map->value_size) != 0)
243 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
245 static int map_update_elem(union bpf_attr *attr)
247 void __user *ukey = u64_to_ptr(attr->key);
248 void __user *uvalue = u64_to_ptr(attr->value);
249 int ufd = attr->map_fd;
255 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
259 map = bpf_map_get(f);
264 key = kmalloc(map->key_size, GFP_USER);
269 if (copy_from_user(key, ukey, map->key_size) != 0)
273 value = kmalloc(map->value_size, GFP_USER);
278 if (copy_from_user(value, uvalue, map->value_size) != 0)
281 /* eBPF program that use maps are running under rcu_read_lock(),
282 * therefore all map accessors rely on this fact, so do the same here
285 err = map->ops->map_update_elem(map, key, value, attr->flags);
297 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
299 static int map_delete_elem(union bpf_attr *attr)
301 void __user *ukey = u64_to_ptr(attr->key);
302 int ufd = attr->map_fd;
308 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
312 map = bpf_map_get(f);
317 key = kmalloc(map->key_size, GFP_USER);
322 if (copy_from_user(key, ukey, map->key_size) != 0)
326 err = map->ops->map_delete_elem(map, key);
336 /* last field in 'union bpf_attr' used by this command */
337 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
339 static int map_get_next_key(union bpf_attr *attr)
341 void __user *ukey = u64_to_ptr(attr->key);
342 void __user *unext_key = u64_to_ptr(attr->next_key);
343 int ufd = attr->map_fd;
345 void *key, *next_key;
349 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
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)
367 next_key = kmalloc(map->key_size, GFP_USER);
372 err = map->ops->map_get_next_key(map, key, next_key);
378 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
392 static LIST_HEAD(bpf_prog_types);
394 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
396 struct bpf_prog_type_list *tl;
398 list_for_each_entry(tl, &bpf_prog_types, list_node) {
399 if (tl->type == type) {
400 prog->aux->ops = tl->ops;
409 void bpf_register_prog_type(struct bpf_prog_type_list *tl)
411 list_add(&tl->list_node, &bpf_prog_types);
414 /* fixup insn->imm field of bpf_call instructions:
415 * if (insn->imm == BPF_FUNC_map_lookup_elem)
416 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
417 * else if (insn->imm == BPF_FUNC_map_update_elem)
418 * insn->imm = bpf_map_update_elem - __bpf_call_base;
421 * this function is called after eBPF program passed verification
423 static void fixup_bpf_calls(struct bpf_prog *prog)
425 const struct bpf_func_proto *fn;
428 for (i = 0; i < prog->len; i++) {
429 struct bpf_insn *insn = &prog->insnsi[i];
431 if (insn->code == (BPF_JMP | BPF_CALL)) {
432 /* we reach here when program has bpf_call instructions
433 * and it passed bpf_check(), means that
434 * ops->get_func_proto must have been supplied, check it
436 BUG_ON(!prog->aux->ops->get_func_proto);
438 if (insn->imm == BPF_FUNC_get_route_realm)
439 prog->dst_needed = 1;
440 if (insn->imm == BPF_FUNC_get_prandom_u32)
441 bpf_user_rnd_init_once();
442 if (insn->imm == BPF_FUNC_tail_call) {
443 /* mark bpf_tail_call as different opcode
444 * to avoid conditional branch in
445 * interpeter for every normal call
446 * and to prevent accidental JITing by
447 * JIT compiler that doesn't support
455 fn = prog->aux->ops->get_func_proto(insn->imm);
456 /* all functions that have prototype and verifier allowed
457 * programs to call them, must be real in-kernel functions
460 insn->imm = fn->func - __bpf_call_base;
465 /* drop refcnt on maps used by eBPF program and free auxilary data */
466 static void free_used_maps(struct bpf_prog_aux *aux)
470 for (i = 0; i < aux->used_map_cnt; i++)
471 bpf_map_put(aux->used_maps[i]);
473 kfree(aux->used_maps);
476 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
478 struct user_struct *user = get_current_user();
479 unsigned long memlock_limit;
481 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
483 atomic_long_add(prog->pages, &user->locked_vm);
484 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
485 atomic_long_sub(prog->pages, &user->locked_vm);
489 prog->aux->user = user;
493 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
495 struct user_struct *user = prog->aux->user;
497 atomic_long_sub(prog->pages, &user->locked_vm);
501 static void __prog_put_rcu(struct rcu_head *rcu)
503 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
506 bpf_prog_uncharge_memlock(aux->prog);
507 bpf_prog_free(aux->prog);
510 /* version of bpf_prog_put() that is called after a grace period */
511 void bpf_prog_put_rcu(struct bpf_prog *prog)
513 if (atomic_dec_and_test(&prog->aux->refcnt)) {
514 prog->aux->prog = prog;
515 call_rcu(&prog->aux->rcu, __prog_put_rcu);
519 void bpf_prog_put(struct bpf_prog *prog)
521 if (atomic_dec_and_test(&prog->aux->refcnt)) {
522 free_used_maps(prog->aux);
526 EXPORT_SYMBOL_GPL(bpf_prog_put);
528 static int bpf_prog_release(struct inode *inode, struct file *filp)
530 struct bpf_prog *prog = filp->private_data;
532 bpf_prog_put_rcu(prog);
536 static const struct file_operations bpf_prog_fops = {
537 .release = bpf_prog_release,
540 static struct bpf_prog *get_prog(struct fd f)
542 struct bpf_prog *prog;
545 return ERR_PTR(-EBADF);
547 if (f.file->f_op != &bpf_prog_fops) {
549 return ERR_PTR(-EINVAL);
552 prog = f.file->private_data;
557 /* called by sockets/tracing/seccomp before attaching program to an event
558 * pairs with bpf_prog_put()
560 struct bpf_prog *bpf_prog_get(u32 ufd)
562 struct fd f = fdget(ufd);
563 struct bpf_prog *prog;
570 atomic_inc(&prog->aux->refcnt);
574 EXPORT_SYMBOL_GPL(bpf_prog_get);
576 /* last field in 'union bpf_attr' used by this command */
577 #define BPF_PROG_LOAD_LAST_FIELD kern_version
579 static int bpf_prog_load(union bpf_attr *attr)
581 enum bpf_prog_type type = attr->prog_type;
582 struct bpf_prog *prog;
587 if (CHECK_ATTR(BPF_PROG_LOAD))
590 /* copy eBPF program license from user space */
591 if (strncpy_from_user(license, u64_to_ptr(attr->license),
592 sizeof(license) - 1) < 0)
594 license[sizeof(license) - 1] = 0;
596 /* eBPF programs must be GPL compatible to use GPL-ed functions */
597 is_gpl = license_is_gpl_compatible(license);
599 if (attr->insn_cnt >= BPF_MAXINSNS)
602 if (type == BPF_PROG_TYPE_KPROBE &&
603 attr->kern_version != LINUX_VERSION_CODE)
606 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
609 /* plain bpf_prog allocation */
610 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
614 err = bpf_prog_charge_memlock(prog);
616 goto free_prog_nouncharge;
618 prog->len = attr->insn_cnt;
621 if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
622 prog->len * sizeof(struct bpf_insn)) != 0)
625 prog->orig_prog = NULL;
628 atomic_set(&prog->aux->refcnt, 1);
629 prog->gpl_compatible = is_gpl ? 1 : 0;
631 /* find program type: socket_filter vs tracing_filter */
632 err = find_prog_type(type, prog);
636 /* run eBPF verifier */
637 err = bpf_check(&prog, attr);
641 /* fixup BPF_CALL->imm field */
642 fixup_bpf_calls(prog);
644 /* eBPF program is ready to be JITed */
645 err = bpf_prog_select_runtime(prog);
649 err = anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, O_RDWR | O_CLOEXEC);
651 /* failed to allocate fd */
657 free_used_maps(prog->aux);
659 bpf_prog_uncharge_memlock(prog);
660 free_prog_nouncharge:
665 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
667 union bpf_attr attr = {};
670 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
673 if (!access_ok(VERIFY_READ, uattr, 1))
676 if (size > PAGE_SIZE) /* silly large */
679 /* If we're handed a bigger struct than we know of,
680 * ensure all the unknown bits are 0 - i.e. new
681 * user-space does not rely on any kernel feature
682 * extensions we dont know about yet.
684 if (size > sizeof(attr)) {
685 unsigned char __user *addr;
686 unsigned char __user *end;
689 addr = (void __user *)uattr + sizeof(attr);
690 end = (void __user *)uattr + size;
692 for (; addr < end; addr++) {
693 err = get_user(val, addr);
702 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
703 if (copy_from_user(&attr, uattr, size) != 0)
708 err = map_create(&attr);
710 case BPF_MAP_LOOKUP_ELEM:
711 err = map_lookup_elem(&attr);
713 case BPF_MAP_UPDATE_ELEM:
714 err = map_update_elem(&attr);
716 case BPF_MAP_DELETE_ELEM:
717 err = map_delete_elem(&attr);
719 case BPF_MAP_GET_NEXT_KEY:
720 err = map_get_next_key(&attr);
723 err = bpf_prog_load(&attr);