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 static void bpf_map_put_uref(struct bpf_map *map)
87 if (atomic_dec_and_test(&map->usercnt)) {
88 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
89 bpf_fd_array_map_clear(map);
93 /* decrement map refcnt and schedule it for freeing via workqueue
94 * (unrelying map implementation ops->map_free() might sleep)
96 void bpf_map_put(struct bpf_map *map)
98 if (atomic_dec_and_test(&map->refcnt)) {
99 INIT_WORK(&map->work, bpf_map_free_deferred);
100 schedule_work(&map->work);
104 void bpf_map_put_with_uref(struct bpf_map *map)
106 bpf_map_put_uref(map);
110 static int bpf_map_release(struct inode *inode, struct file *filp)
112 bpf_map_put_with_uref(filp->private_data);
116 static const struct file_operations bpf_map_fops = {
117 .release = bpf_map_release,
120 int bpf_map_new_fd(struct bpf_map *map)
122 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
126 /* helper macro to check that unused fields 'union bpf_attr' are zero */
127 #define CHECK_ATTR(CMD) \
128 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
129 sizeof(attr->CMD##_LAST_FIELD), 0, \
131 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
132 sizeof(attr->CMD##_LAST_FIELD)) != NULL
134 #define BPF_MAP_CREATE_LAST_FIELD max_entries
135 /* called via syscall */
136 static int map_create(union bpf_attr *attr)
141 err = CHECK_ATTR(BPF_MAP_CREATE);
145 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
146 map = find_and_alloc_map(attr);
150 atomic_set(&map->refcnt, 1);
151 atomic_set(&map->usercnt, 1);
153 err = bpf_map_charge_memlock(map);
155 goto free_map_nouncharge;
157 err = bpf_map_new_fd(map);
159 /* failed to allocate fd */
165 bpf_map_uncharge_memlock(map);
167 map->ops->map_free(map);
171 /* if error is returned, fd is released.
172 * On success caller should complete fd access with matching fdput()
174 struct bpf_map *__bpf_map_get(struct fd f)
177 return ERR_PTR(-EBADF);
178 if (f.file->f_op != &bpf_map_fops) {
180 return ERR_PTR(-EINVAL);
183 return f.file->private_data;
186 /* prog's and map's refcnt limit */
187 #define BPF_MAX_REFCNT 32768
189 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
191 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
192 atomic_dec(&map->refcnt);
193 return ERR_PTR(-EBUSY);
196 atomic_inc(&map->usercnt);
200 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
202 struct fd f = fdget(ufd);
205 map = __bpf_map_get(f);
209 map = bpf_map_inc(map, true);
215 /* helper to convert user pointers passed inside __aligned_u64 fields */
216 static void __user *u64_to_ptr(__u64 val)
218 return (void __user *) (unsigned long) val;
221 /* last field in 'union bpf_attr' used by this command */
222 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
224 static int map_lookup_elem(union bpf_attr *attr)
226 void __user *ukey = u64_to_ptr(attr->key);
227 void __user *uvalue = u64_to_ptr(attr->value);
228 int ufd = attr->map_fd;
230 void *key, *value, *ptr;
234 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
238 map = __bpf_map_get(f);
243 key = kmalloc(map->key_size, GFP_USER);
248 if (copy_from_user(key, ukey, map->key_size) != 0)
252 value = kmalloc(map->value_size, GFP_USER | __GFP_NOWARN);
257 ptr = map->ops->map_lookup_elem(map, key);
259 memcpy(value, ptr, map->value_size);
267 if (copy_to_user(uvalue, value, map->value_size) != 0)
281 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
283 static int map_update_elem(union bpf_attr *attr)
285 void __user *ukey = u64_to_ptr(attr->key);
286 void __user *uvalue = u64_to_ptr(attr->value);
287 int ufd = attr->map_fd;
293 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
297 map = __bpf_map_get(f);
302 key = kmalloc(map->key_size, GFP_USER);
307 if (copy_from_user(key, ukey, map->key_size) != 0)
311 value = kmalloc(map->value_size, GFP_USER | __GFP_NOWARN);
316 if (copy_from_user(value, uvalue, map->value_size) != 0)
319 /* eBPF program that use maps are running under rcu_read_lock(),
320 * therefore all map accessors rely on this fact, so do the same here
323 err = map->ops->map_update_elem(map, key, value, attr->flags);
335 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
337 static int map_delete_elem(union bpf_attr *attr)
339 void __user *ukey = u64_to_ptr(attr->key);
340 int ufd = attr->map_fd;
346 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
350 map = __bpf_map_get(f);
355 key = kmalloc(map->key_size, GFP_USER);
360 if (copy_from_user(key, ukey, map->key_size) != 0)
364 err = map->ops->map_delete_elem(map, key);
374 /* last field in 'union bpf_attr' used by this command */
375 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
377 static int map_get_next_key(union bpf_attr *attr)
379 void __user *ukey = u64_to_ptr(attr->key);
380 void __user *unext_key = u64_to_ptr(attr->next_key);
381 int ufd = attr->map_fd;
383 void *key, *next_key;
387 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
391 map = __bpf_map_get(f);
397 key = kmalloc(map->key_size, GFP_USER);
402 if (copy_from_user(key, ukey, map->key_size) != 0)
409 next_key = kmalloc(map->key_size, GFP_USER);
414 err = map->ops->map_get_next_key(map, key, next_key);
420 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
434 static LIST_HEAD(bpf_prog_types);
436 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
438 struct bpf_prog_type_list *tl;
440 list_for_each_entry(tl, &bpf_prog_types, list_node) {
441 if (tl->type == type) {
442 prog->aux->ops = tl->ops;
451 void bpf_register_prog_type(struct bpf_prog_type_list *tl)
453 list_add(&tl->list_node, &bpf_prog_types);
456 /* drop refcnt on maps used by eBPF program and free auxilary data */
457 static void free_used_maps(struct bpf_prog_aux *aux)
461 for (i = 0; i < aux->used_map_cnt; i++)
462 bpf_map_put(aux->used_maps[i]);
464 kfree(aux->used_maps);
467 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
469 struct user_struct *user = get_current_user();
470 unsigned long memlock_limit;
472 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
474 atomic_long_add(prog->pages, &user->locked_vm);
475 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
476 atomic_long_sub(prog->pages, &user->locked_vm);
480 prog->aux->user = user;
484 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
486 struct user_struct *user = prog->aux->user;
488 atomic_long_sub(prog->pages, &user->locked_vm);
492 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
494 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
497 bpf_prog_uncharge_memlock(aux->prog);
498 bpf_prog_free(aux->prog);
501 void bpf_prog_put(struct bpf_prog *prog)
503 if (atomic_dec_and_test(&prog->aux->refcnt))
504 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
506 EXPORT_SYMBOL_GPL(bpf_prog_put);
508 static int bpf_prog_release(struct inode *inode, struct file *filp)
510 struct bpf_prog *prog = filp->private_data;
516 static const struct file_operations bpf_prog_fops = {
517 .release = bpf_prog_release,
520 int bpf_prog_new_fd(struct bpf_prog *prog)
522 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
526 static struct bpf_prog *__bpf_prog_get(struct fd f)
529 return ERR_PTR(-EBADF);
530 if (f.file->f_op != &bpf_prog_fops) {
532 return ERR_PTR(-EINVAL);
535 return f.file->private_data;
538 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
540 if (atomic_inc_return(&prog->aux->refcnt) > BPF_MAX_REFCNT) {
541 atomic_dec(&prog->aux->refcnt);
542 return ERR_PTR(-EBUSY);
547 /* called by sockets/tracing/seccomp before attaching program to an event
548 * pairs with bpf_prog_put()
550 struct bpf_prog *bpf_prog_get(u32 ufd)
552 struct fd f = fdget(ufd);
553 struct bpf_prog *prog;
555 prog = __bpf_prog_get(f);
559 prog = bpf_prog_inc(prog);
564 EXPORT_SYMBOL_GPL(bpf_prog_get);
566 /* last field in 'union bpf_attr' used by this command */
567 #define BPF_PROG_LOAD_LAST_FIELD kern_version
569 static int bpf_prog_load(union bpf_attr *attr)
571 enum bpf_prog_type type = attr->prog_type;
572 struct bpf_prog *prog;
577 if (CHECK_ATTR(BPF_PROG_LOAD))
580 /* copy eBPF program license from user space */
581 if (strncpy_from_user(license, u64_to_ptr(attr->license),
582 sizeof(license) - 1) < 0)
584 license[sizeof(license) - 1] = 0;
586 /* eBPF programs must be GPL compatible to use GPL-ed functions */
587 is_gpl = license_is_gpl_compatible(license);
589 if (attr->insn_cnt >= BPF_MAXINSNS)
592 if (type == BPF_PROG_TYPE_KPROBE &&
593 attr->kern_version != LINUX_VERSION_CODE)
596 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
599 /* plain bpf_prog allocation */
600 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
604 err = bpf_prog_charge_memlock(prog);
606 goto free_prog_nouncharge;
608 prog->len = attr->insn_cnt;
611 if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
612 prog->len * sizeof(struct bpf_insn)) != 0)
615 prog->orig_prog = NULL;
618 atomic_set(&prog->aux->refcnt, 1);
619 prog->gpl_compatible = is_gpl ? 1 : 0;
621 /* find program type: socket_filter vs tracing_filter */
622 err = find_prog_type(type, prog);
626 /* run eBPF verifier */
627 err = bpf_check(&prog, attr);
631 /* eBPF program is ready to be JITed */
632 err = bpf_prog_select_runtime(prog);
636 err = bpf_prog_new_fd(prog);
638 /* failed to allocate fd */
644 free_used_maps(prog->aux);
646 bpf_prog_uncharge_memlock(prog);
647 free_prog_nouncharge:
652 #define BPF_OBJ_LAST_FIELD bpf_fd
654 static int bpf_obj_pin(const union bpf_attr *attr)
656 if (CHECK_ATTR(BPF_OBJ))
659 return bpf_obj_pin_user(attr->bpf_fd, u64_to_ptr(attr->pathname));
662 static int bpf_obj_get(const union bpf_attr *attr)
664 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
667 return bpf_obj_get_user(u64_to_ptr(attr->pathname));
670 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
675 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
678 if (!access_ok(VERIFY_READ, uattr, 1))
681 if (size > PAGE_SIZE) /* silly large */
684 /* If we're handed a bigger struct than we know of,
685 * ensure all the unknown bits are 0 - i.e. new
686 * user-space does not rely on any kernel feature
687 * extensions we dont know about yet.
689 if (size > sizeof(attr)) {
690 unsigned char __user *addr;
691 unsigned char __user *end;
694 addr = (void __user *)uattr + sizeof(attr);
695 end = (void __user *)uattr + size;
697 for (; addr < end; addr++) {
698 err = get_user(val, addr);
707 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
708 memset(&attr, 0, sizeof(attr));
709 if (copy_from_user(&attr, uattr, size) != 0)
714 err = map_create(&attr);
716 case BPF_MAP_LOOKUP_ELEM:
717 err = map_lookup_elem(&attr);
719 case BPF_MAP_UPDATE_ELEM:
720 err = map_update_elem(&attr);
722 case BPF_MAP_DELETE_ELEM:
723 err = map_delete_elem(&attr);
725 case BPF_MAP_GET_NEXT_KEY:
726 err = map_get_next_key(&attr);
729 err = bpf_prog_load(&attr);
732 err = bpf_obj_pin(&attr);
735 err = bpf_obj_get(&attr);