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 static LIST_HEAD(bpf_map_types
);
23 static struct bpf_map
*find_and_alloc_map(union bpf_attr
*attr
)
25 struct bpf_map_type_list
*tl
;
28 list_for_each_entry(tl
, &bpf_map_types
, list_node
) {
29 if (tl
->type
== attr
->map_type
) {
30 map
= tl
->ops
->map_alloc(attr
);
34 map
->map_type
= attr
->map_type
;
38 return ERR_PTR(-EINVAL
);
41 /* boot time registration of different map implementations */
42 void bpf_register_map_type(struct bpf_map_type_list
*tl
)
44 list_add(&tl
->list_node
, &bpf_map_types
);
47 /* called from workqueue */
48 static void bpf_map_free_deferred(struct work_struct
*work
)
50 struct bpf_map
*map
= container_of(work
, struct bpf_map
, work
);
52 /* implementation dependent freeing */
53 map
->ops
->map_free(map
);
56 /* decrement map refcnt and schedule it for freeing via workqueue
57 * (unrelying map implementation ops->map_free() might sleep)
59 void bpf_map_put(struct bpf_map
*map
)
61 if (atomic_dec_and_test(&map
->refcnt
)) {
62 INIT_WORK(&map
->work
, bpf_map_free_deferred
);
63 schedule_work(&map
->work
);
67 static int bpf_map_release(struct inode
*inode
, struct file
*filp
)
69 struct bpf_map
*map
= filp
->private_data
;
75 static const struct file_operations bpf_map_fops
= {
76 .release
= bpf_map_release
,
79 /* helper macro to check that unused fields 'union bpf_attr' are zero */
80 #define CHECK_ATTR(CMD) \
81 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
82 sizeof(attr->CMD##_LAST_FIELD), 0, \
84 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
85 sizeof(attr->CMD##_LAST_FIELD)) != NULL
87 #define BPF_MAP_CREATE_LAST_FIELD max_entries
88 /* called via syscall */
89 static int map_create(union bpf_attr
*attr
)
94 err
= CHECK_ATTR(BPF_MAP_CREATE
);
98 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
99 map
= find_and_alloc_map(attr
);
103 atomic_set(&map
->refcnt
, 1);
105 err
= anon_inode_getfd("bpf-map", &bpf_map_fops
, map
, O_RDWR
| O_CLOEXEC
);
108 /* failed to allocate fd */
114 map
->ops
->map_free(map
);
118 /* if error is returned, fd is released.
119 * On success caller should complete fd access with matching fdput()
121 struct bpf_map
*bpf_map_get(struct fd f
)
126 return ERR_PTR(-EBADF
);
128 if (f
.file
->f_op
!= &bpf_map_fops
) {
130 return ERR_PTR(-EINVAL
);
133 map
= f
.file
->private_data
;
138 /* helper to convert user pointers passed inside __aligned_u64 fields */
139 static void __user
*u64_to_ptr(__u64 val
)
141 return (void __user
*) (unsigned long) val
;
144 /* last field in 'union bpf_attr' used by this command */
145 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
147 static int map_lookup_elem(union bpf_attr
*attr
)
149 void __user
*ukey
= u64_to_ptr(attr
->key
);
150 void __user
*uvalue
= u64_to_ptr(attr
->value
);
151 int ufd
= attr
->map_fd
;
152 struct fd f
= fdget(ufd
);
154 void *key
, *value
, *ptr
;
157 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM
))
160 map
= bpf_map_get(f
);
165 key
= kmalloc(map
->key_size
, GFP_USER
);
170 if (copy_from_user(key
, ukey
, map
->key_size
) != 0)
174 value
= kmalloc(map
->value_size
, GFP_USER
);
179 ptr
= map
->ops
->map_lookup_elem(map
, key
);
181 memcpy(value
, ptr
, map
->value_size
);
189 if (copy_to_user(uvalue
, value
, map
->value_size
) != 0)
203 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
205 static int map_update_elem(union bpf_attr
*attr
)
207 void __user
*ukey
= u64_to_ptr(attr
->key
);
208 void __user
*uvalue
= u64_to_ptr(attr
->value
);
209 int ufd
= attr
->map_fd
;
210 struct fd f
= fdget(ufd
);
215 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM
))
218 map
= bpf_map_get(f
);
223 key
= kmalloc(map
->key_size
, GFP_USER
);
228 if (copy_from_user(key
, ukey
, map
->key_size
) != 0)
232 value
= kmalloc(map
->value_size
, GFP_USER
);
237 if (copy_from_user(value
, uvalue
, map
->value_size
) != 0)
240 /* eBPF program that use maps are running under rcu_read_lock(),
241 * therefore all map accessors rely on this fact, so do the same here
244 err
= map
->ops
->map_update_elem(map
, key
, value
, attr
->flags
);
256 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
258 static int map_delete_elem(union bpf_attr
*attr
)
260 void __user
*ukey
= u64_to_ptr(attr
->key
);
261 int ufd
= attr
->map_fd
;
262 struct fd f
= fdget(ufd
);
267 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM
))
270 map
= bpf_map_get(f
);
275 key
= kmalloc(map
->key_size
, GFP_USER
);
280 if (copy_from_user(key
, ukey
, map
->key_size
) != 0)
284 err
= map
->ops
->map_delete_elem(map
, key
);
294 /* last field in 'union bpf_attr' used by this command */
295 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
297 static int map_get_next_key(union bpf_attr
*attr
)
299 void __user
*ukey
= u64_to_ptr(attr
->key
);
300 void __user
*unext_key
= u64_to_ptr(attr
->next_key
);
301 int ufd
= attr
->map_fd
;
302 struct fd f
= fdget(ufd
);
304 void *key
, *next_key
;
307 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY
))
310 map
= bpf_map_get(f
);
315 key
= kmalloc(map
->key_size
, GFP_USER
);
320 if (copy_from_user(key
, ukey
, map
->key_size
) != 0)
324 next_key
= kmalloc(map
->key_size
, GFP_USER
);
329 err
= map
->ops
->map_get_next_key(map
, key
, next_key
);
335 if (copy_to_user(unext_key
, next_key
, map
->key_size
) != 0)
349 static LIST_HEAD(bpf_prog_types
);
351 static int find_prog_type(enum bpf_prog_type type
, struct bpf_prog
*prog
)
353 struct bpf_prog_type_list
*tl
;
355 list_for_each_entry(tl
, &bpf_prog_types
, list_node
) {
356 if (tl
->type
== type
) {
357 prog
->aux
->ops
= tl
->ops
;
366 void bpf_register_prog_type(struct bpf_prog_type_list
*tl
)
368 list_add(&tl
->list_node
, &bpf_prog_types
);
371 /* fixup insn->imm field of bpf_call instructions:
372 * if (insn->imm == BPF_FUNC_map_lookup_elem)
373 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
374 * else if (insn->imm == BPF_FUNC_map_update_elem)
375 * insn->imm = bpf_map_update_elem - __bpf_call_base;
378 * this function is called after eBPF program passed verification
380 static void fixup_bpf_calls(struct bpf_prog
*prog
)
382 const struct bpf_func_proto
*fn
;
385 for (i
= 0; i
< prog
->len
; i
++) {
386 struct bpf_insn
*insn
= &prog
->insnsi
[i
];
388 if (insn
->code
== (BPF_JMP
| BPF_CALL
)) {
389 /* we reach here when program has bpf_call instructions
390 * and it passed bpf_check(), means that
391 * ops->get_func_proto must have been supplied, check it
393 BUG_ON(!prog
->aux
->ops
->get_func_proto
);
395 fn
= prog
->aux
->ops
->get_func_proto(insn
->imm
);
396 /* all functions that have prototype and verifier allowed
397 * programs to call them, must be real in-kernel functions
400 insn
->imm
= fn
->func
- __bpf_call_base
;
405 /* drop refcnt on maps used by eBPF program and free auxilary data */
406 static void free_used_maps(struct bpf_prog_aux
*aux
)
410 for (i
= 0; i
< aux
->used_map_cnt
; i
++)
411 bpf_map_put(aux
->used_maps
[i
]);
413 kfree(aux
->used_maps
);
416 void bpf_prog_put(struct bpf_prog
*prog
)
418 if (atomic_dec_and_test(&prog
->aux
->refcnt
)) {
419 free_used_maps(prog
->aux
);
423 EXPORT_SYMBOL_GPL(bpf_prog_put
);
425 static int bpf_prog_release(struct inode
*inode
, struct file
*filp
)
427 struct bpf_prog
*prog
= filp
->private_data
;
433 static const struct file_operations bpf_prog_fops
= {
434 .release
= bpf_prog_release
,
437 static struct bpf_prog
*get_prog(struct fd f
)
439 struct bpf_prog
*prog
;
442 return ERR_PTR(-EBADF
);
444 if (f
.file
->f_op
!= &bpf_prog_fops
) {
446 return ERR_PTR(-EINVAL
);
449 prog
= f
.file
->private_data
;
454 /* called by sockets/tracing/seccomp before attaching program to an event
455 * pairs with bpf_prog_put()
457 struct bpf_prog
*bpf_prog_get(u32 ufd
)
459 struct fd f
= fdget(ufd
);
460 struct bpf_prog
*prog
;
467 atomic_inc(&prog
->aux
->refcnt
);
471 EXPORT_SYMBOL_GPL(bpf_prog_get
);
473 /* last field in 'union bpf_attr' used by this command */
474 #define BPF_PROG_LOAD_LAST_FIELD kern_version
476 static int bpf_prog_load(union bpf_attr
*attr
)
478 enum bpf_prog_type type
= attr
->prog_type
;
479 struct bpf_prog
*prog
;
484 if (CHECK_ATTR(BPF_PROG_LOAD
))
487 /* copy eBPF program license from user space */
488 if (strncpy_from_user(license
, u64_to_ptr(attr
->license
),
489 sizeof(license
) - 1) < 0)
491 license
[sizeof(license
) - 1] = 0;
493 /* eBPF programs must be GPL compatible to use GPL-ed functions */
494 is_gpl
= license_is_gpl_compatible(license
);
496 if (attr
->insn_cnt
>= BPF_MAXINSNS
)
499 if (type
== BPF_PROG_TYPE_KPROBE
&&
500 attr
->kern_version
!= LINUX_VERSION_CODE
)
503 /* plain bpf_prog allocation */
504 prog
= bpf_prog_alloc(bpf_prog_size(attr
->insn_cnt
), GFP_USER
);
508 prog
->len
= attr
->insn_cnt
;
511 if (copy_from_user(prog
->insns
, u64_to_ptr(attr
->insns
),
512 prog
->len
* sizeof(struct bpf_insn
)) != 0)
515 prog
->orig_prog
= NULL
;
518 atomic_set(&prog
->aux
->refcnt
, 1);
519 prog
->gpl_compatible
= is_gpl
;
521 /* find program type: socket_filter vs tracing_filter */
522 err
= find_prog_type(type
, prog
);
526 /* run eBPF verifier */
527 err
= bpf_check(&prog
, attr
);
531 /* fixup BPF_CALL->imm field */
532 fixup_bpf_calls(prog
);
534 /* eBPF program is ready to be JITed */
535 bpf_prog_select_runtime(prog
);
537 err
= anon_inode_getfd("bpf-prog", &bpf_prog_fops
, prog
, O_RDWR
| O_CLOEXEC
);
539 /* failed to allocate fd */
545 free_used_maps(prog
->aux
);
551 SYSCALL_DEFINE3(bpf
, int, cmd
, union bpf_attr __user
*, uattr
, unsigned int, size
)
553 union bpf_attr attr
= {};
556 /* the syscall is limited to root temporarily. This restriction will be
557 * lifted when security audit is clean. Note that eBPF+tracing must have
558 * this restriction, since it may pass kernel data to user space
560 if (!capable(CAP_SYS_ADMIN
))
563 if (!access_ok(VERIFY_READ
, uattr
, 1))
566 if (size
> PAGE_SIZE
) /* silly large */
569 /* If we're handed a bigger struct than we know of,
570 * ensure all the unknown bits are 0 - i.e. new
571 * user-space does not rely on any kernel feature
572 * extensions we dont know about yet.
574 if (size
> sizeof(attr
)) {
575 unsigned char __user
*addr
;
576 unsigned char __user
*end
;
579 addr
= (void __user
*)uattr
+ sizeof(attr
);
580 end
= (void __user
*)uattr
+ size
;
582 for (; addr
< end
; addr
++) {
583 err
= get_user(val
, addr
);
592 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
593 if (copy_from_user(&attr
, uattr
, size
) != 0)
598 err
= map_create(&attr
);
600 case BPF_MAP_LOOKUP_ELEM
:
601 err
= map_lookup_elem(&attr
);
603 case BPF_MAP_UPDATE_ELEM
:
604 err
= map_update_elem(&attr
);
606 case BPF_MAP_DELETE_ELEM
:
607 err
= map_delete_elem(&attr
);
609 case BPF_MAP_GET_NEXT_KEY
:
610 err
= map_get_next_key(&attr
);
613 err
= bpf_prog_load(&attr
);