blob: 0d69449acbd015ce0ae1ab8a80606ee3ba104cf4 [file] [log] [blame]
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 *
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.
6 *
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.
11 */
12#include <linux/bpf.h>
13#include <linux/syscalls.h>
14#include <linux/slab.h>
15#include <linux/anon_inodes.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070016#include <linux/file.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070017#include <linux/license.h>
18#include <linux/filter.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070019
20static LIST_HEAD(bpf_map_types);
21
22static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
23{
24 struct bpf_map_type_list *tl;
25 struct bpf_map *map;
26
27 list_for_each_entry(tl, &bpf_map_types, list_node) {
28 if (tl->type == attr->map_type) {
29 map = tl->ops->map_alloc(attr);
30 if (IS_ERR(map))
31 return map;
32 map->ops = tl->ops;
33 map->map_type = attr->map_type;
34 return map;
35 }
36 }
37 return ERR_PTR(-EINVAL);
38}
39
40/* boot time registration of different map implementations */
41void bpf_register_map_type(struct bpf_map_type_list *tl)
42{
43 list_add(&tl->list_node, &bpf_map_types);
44}
45
46/* called from workqueue */
47static void bpf_map_free_deferred(struct work_struct *work)
48{
49 struct bpf_map *map = container_of(work, struct bpf_map, work);
50
51 /* implementation dependent freeing */
52 map->ops->map_free(map);
53}
54
55/* decrement map refcnt and schedule it for freeing via workqueue
56 * (unrelying map implementation ops->map_free() might sleep)
57 */
58void bpf_map_put(struct bpf_map *map)
59{
60 if (atomic_dec_and_test(&map->refcnt)) {
61 INIT_WORK(&map->work, bpf_map_free_deferred);
62 schedule_work(&map->work);
63 }
64}
65
66static int bpf_map_release(struct inode *inode, struct file *filp)
67{
68 struct bpf_map *map = filp->private_data;
69
70 bpf_map_put(map);
71 return 0;
72}
73
74static const struct file_operations bpf_map_fops = {
75 .release = bpf_map_release,
76};
77
78/* helper macro to check that unused fields 'union bpf_attr' are zero */
79#define CHECK_ATTR(CMD) \
80 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
81 sizeof(attr->CMD##_LAST_FIELD), 0, \
82 sizeof(*attr) - \
83 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
84 sizeof(attr->CMD##_LAST_FIELD)) != NULL
85
86#define BPF_MAP_CREATE_LAST_FIELD max_entries
87/* called via syscall */
88static int map_create(union bpf_attr *attr)
89{
90 struct bpf_map *map;
91 int err;
92
93 err = CHECK_ATTR(BPF_MAP_CREATE);
94 if (err)
95 return -EINVAL;
96
97 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
98 map = find_and_alloc_map(attr);
99 if (IS_ERR(map))
100 return PTR_ERR(map);
101
102 atomic_set(&map->refcnt, 1);
103
104 err = anon_inode_getfd("bpf-map", &bpf_map_fops, map, O_RDWR | O_CLOEXEC);
105
106 if (err < 0)
107 /* failed to allocate fd */
108 goto free_map;
109
110 return err;
111
112free_map:
113 map->ops->map_free(map);
114 return err;
115}
116
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700117/* if error is returned, fd is released.
118 * On success caller should complete fd access with matching fdput()
119 */
120struct bpf_map *bpf_map_get(struct fd f)
121{
122 struct bpf_map *map;
123
124 if (!f.file)
125 return ERR_PTR(-EBADF);
126
127 if (f.file->f_op != &bpf_map_fops) {
128 fdput(f);
129 return ERR_PTR(-EINVAL);
130 }
131
132 map = f.file->private_data;
133
134 return map;
135}
136
137/* helper to convert user pointers passed inside __aligned_u64 fields */
138static void __user *u64_to_ptr(__u64 val)
139{
140 return (void __user *) (unsigned long) val;
141}
142
143/* last field in 'union bpf_attr' used by this command */
144#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
145
146static int map_lookup_elem(union bpf_attr *attr)
147{
148 void __user *ukey = u64_to_ptr(attr->key);
149 void __user *uvalue = u64_to_ptr(attr->value);
150 int ufd = attr->map_fd;
151 struct fd f = fdget(ufd);
152 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800153 void *key, *value, *ptr;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700154 int err;
155
156 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
157 return -EINVAL;
158
159 map = bpf_map_get(f);
160 if (IS_ERR(map))
161 return PTR_ERR(map);
162
163 err = -ENOMEM;
164 key = kmalloc(map->key_size, GFP_USER);
165 if (!key)
166 goto err_put;
167
168 err = -EFAULT;
169 if (copy_from_user(key, ukey, map->key_size) != 0)
170 goto free_key;
171
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800172 err = -ENOMEM;
173 value = kmalloc(map->value_size, GFP_USER);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700174 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800175 goto free_key;
176
177 rcu_read_lock();
178 ptr = map->ops->map_lookup_elem(map, key);
179 if (ptr)
180 memcpy(value, ptr, map->value_size);
181 rcu_read_unlock();
182
183 err = -ENOENT;
184 if (!ptr)
185 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700186
187 err = -EFAULT;
188 if (copy_to_user(uvalue, value, map->value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800189 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700190
191 err = 0;
192
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800193free_value:
194 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700195free_key:
196 kfree(key);
197err_put:
198 fdput(f);
199 return err;
200}
201
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800202#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700203
204static int map_update_elem(union bpf_attr *attr)
205{
206 void __user *ukey = u64_to_ptr(attr->key);
207 void __user *uvalue = u64_to_ptr(attr->value);
208 int ufd = attr->map_fd;
209 struct fd f = fdget(ufd);
210 struct bpf_map *map;
211 void *key, *value;
212 int err;
213
214 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
215 return -EINVAL;
216
217 map = bpf_map_get(f);
218 if (IS_ERR(map))
219 return PTR_ERR(map);
220
221 err = -ENOMEM;
222 key = kmalloc(map->key_size, GFP_USER);
223 if (!key)
224 goto err_put;
225
226 err = -EFAULT;
227 if (copy_from_user(key, ukey, map->key_size) != 0)
228 goto free_key;
229
230 err = -ENOMEM;
231 value = kmalloc(map->value_size, GFP_USER);
232 if (!value)
233 goto free_key;
234
235 err = -EFAULT;
236 if (copy_from_user(value, uvalue, map->value_size) != 0)
237 goto free_value;
238
239 /* eBPF program that use maps are running under rcu_read_lock(),
240 * therefore all map accessors rely on this fact, so do the same here
241 */
242 rcu_read_lock();
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800243 err = map->ops->map_update_elem(map, key, value, attr->flags);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700244 rcu_read_unlock();
245
246free_value:
247 kfree(value);
248free_key:
249 kfree(key);
250err_put:
251 fdput(f);
252 return err;
253}
254
255#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
256
257static int map_delete_elem(union bpf_attr *attr)
258{
259 void __user *ukey = u64_to_ptr(attr->key);
260 int ufd = attr->map_fd;
261 struct fd f = fdget(ufd);
262 struct bpf_map *map;
263 void *key;
264 int err;
265
266 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
267 return -EINVAL;
268
269 map = bpf_map_get(f);
270 if (IS_ERR(map))
271 return PTR_ERR(map);
272
273 err = -ENOMEM;
274 key = kmalloc(map->key_size, GFP_USER);
275 if (!key)
276 goto err_put;
277
278 err = -EFAULT;
279 if (copy_from_user(key, ukey, map->key_size) != 0)
280 goto free_key;
281
282 rcu_read_lock();
283 err = map->ops->map_delete_elem(map, key);
284 rcu_read_unlock();
285
286free_key:
287 kfree(key);
288err_put:
289 fdput(f);
290 return err;
291}
292
293/* last field in 'union bpf_attr' used by this command */
294#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
295
296static int map_get_next_key(union bpf_attr *attr)
297{
298 void __user *ukey = u64_to_ptr(attr->key);
299 void __user *unext_key = u64_to_ptr(attr->next_key);
300 int ufd = attr->map_fd;
301 struct fd f = fdget(ufd);
302 struct bpf_map *map;
303 void *key, *next_key;
304 int err;
305
306 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
307 return -EINVAL;
308
309 map = bpf_map_get(f);
310 if (IS_ERR(map))
311 return PTR_ERR(map);
312
313 err = -ENOMEM;
314 key = kmalloc(map->key_size, GFP_USER);
315 if (!key)
316 goto err_put;
317
318 err = -EFAULT;
319 if (copy_from_user(key, ukey, map->key_size) != 0)
320 goto free_key;
321
322 err = -ENOMEM;
323 next_key = kmalloc(map->key_size, GFP_USER);
324 if (!next_key)
325 goto free_key;
326
327 rcu_read_lock();
328 err = map->ops->map_get_next_key(map, key, next_key);
329 rcu_read_unlock();
330 if (err)
331 goto free_next_key;
332
333 err = -EFAULT;
334 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
335 goto free_next_key;
336
337 err = 0;
338
339free_next_key:
340 kfree(next_key);
341free_key:
342 kfree(key);
343err_put:
344 fdput(f);
345 return err;
346}
347
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700348static LIST_HEAD(bpf_prog_types);
349
350static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
351{
352 struct bpf_prog_type_list *tl;
353
354 list_for_each_entry(tl, &bpf_prog_types, list_node) {
355 if (tl->type == type) {
356 prog->aux->ops = tl->ops;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100357 prog->type = type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700358 return 0;
359 }
360 }
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100361
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700362 return -EINVAL;
363}
364
365void bpf_register_prog_type(struct bpf_prog_type_list *tl)
366{
367 list_add(&tl->list_node, &bpf_prog_types);
368}
369
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700370/* fixup insn->imm field of bpf_call instructions:
371 * if (insn->imm == BPF_FUNC_map_lookup_elem)
372 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
373 * else if (insn->imm == BPF_FUNC_map_update_elem)
374 * insn->imm = bpf_map_update_elem - __bpf_call_base;
375 * else ...
376 *
377 * this function is called after eBPF program passed verification
378 */
379static void fixup_bpf_calls(struct bpf_prog *prog)
380{
381 const struct bpf_func_proto *fn;
382 int i;
383
384 for (i = 0; i < prog->len; i++) {
385 struct bpf_insn *insn = &prog->insnsi[i];
386
387 if (insn->code == (BPF_JMP | BPF_CALL)) {
388 /* we reach here when program has bpf_call instructions
389 * and it passed bpf_check(), means that
390 * ops->get_func_proto must have been supplied, check it
391 */
392 BUG_ON(!prog->aux->ops->get_func_proto);
393
394 fn = prog->aux->ops->get_func_proto(insn->imm);
395 /* all functions that have prototype and verifier allowed
396 * programs to call them, must be real in-kernel functions
397 */
398 BUG_ON(!fn->func);
399 insn->imm = fn->func - __bpf_call_base;
400 }
401 }
402}
403
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700404/* drop refcnt on maps used by eBPF program and free auxilary data */
405static void free_used_maps(struct bpf_prog_aux *aux)
406{
407 int i;
408
409 for (i = 0; i < aux->used_map_cnt; i++)
410 bpf_map_put(aux->used_maps[i]);
411
412 kfree(aux->used_maps);
413}
414
415void bpf_prog_put(struct bpf_prog *prog)
416{
417 if (atomic_dec_and_test(&prog->aux->refcnt)) {
418 free_used_maps(prog->aux);
419 bpf_prog_free(prog);
420 }
421}
422
423static int bpf_prog_release(struct inode *inode, struct file *filp)
424{
425 struct bpf_prog *prog = filp->private_data;
426
427 bpf_prog_put(prog);
428 return 0;
429}
430
431static const struct file_operations bpf_prog_fops = {
432 .release = bpf_prog_release,
433};
434
435static struct bpf_prog *get_prog(struct fd f)
436{
437 struct bpf_prog *prog;
438
439 if (!f.file)
440 return ERR_PTR(-EBADF);
441
442 if (f.file->f_op != &bpf_prog_fops) {
443 fdput(f);
444 return ERR_PTR(-EINVAL);
445 }
446
447 prog = f.file->private_data;
448
449 return prog;
450}
451
452/* called by sockets/tracing/seccomp before attaching program to an event
453 * pairs with bpf_prog_put()
454 */
455struct bpf_prog *bpf_prog_get(u32 ufd)
456{
457 struct fd f = fdget(ufd);
458 struct bpf_prog *prog;
459
460 prog = get_prog(f);
461
462 if (IS_ERR(prog))
463 return prog;
464
465 atomic_inc(&prog->aux->refcnt);
466 fdput(f);
467 return prog;
468}
469
470/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitovcbd35702014-09-26 00:17:03 -0700471#define BPF_PROG_LOAD_LAST_FIELD log_buf
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700472
473static int bpf_prog_load(union bpf_attr *attr)
474{
475 enum bpf_prog_type type = attr->prog_type;
476 struct bpf_prog *prog;
477 int err;
478 char license[128];
479 bool is_gpl;
480
481 if (CHECK_ATTR(BPF_PROG_LOAD))
482 return -EINVAL;
483
484 /* copy eBPF program license from user space */
485 if (strncpy_from_user(license, u64_to_ptr(attr->license),
486 sizeof(license) - 1) < 0)
487 return -EFAULT;
488 license[sizeof(license) - 1] = 0;
489
490 /* eBPF programs must be GPL compatible to use GPL-ed functions */
491 is_gpl = license_is_gpl_compatible(license);
492
493 if (attr->insn_cnt >= BPF_MAXINSNS)
494 return -EINVAL;
495
496 /* plain bpf_prog allocation */
497 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
498 if (!prog)
499 return -ENOMEM;
500
501 prog->len = attr->insn_cnt;
502
503 err = -EFAULT;
504 if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
505 prog->len * sizeof(struct bpf_insn)) != 0)
506 goto free_prog;
507
508 prog->orig_prog = NULL;
509 prog->jited = false;
510
511 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100512 prog->gpl_compatible = is_gpl;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700513
514 /* find program type: socket_filter vs tracing_filter */
515 err = find_prog_type(type, prog);
516 if (err < 0)
517 goto free_prog;
518
519 /* run eBPF verifier */
Alexei Starovoitov51580e72014-09-26 00:17:02 -0700520 err = bpf_check(prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700521 if (err < 0)
522 goto free_used_maps;
523
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700524 /* fixup BPF_CALL->imm field */
525 fixup_bpf_calls(prog);
526
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700527 /* eBPF program is ready to be JITed */
528 bpf_prog_select_runtime(prog);
529
530 err = anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, O_RDWR | O_CLOEXEC);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700531 if (err < 0)
532 /* failed to allocate fd */
533 goto free_used_maps;
534
535 return err;
536
537free_used_maps:
538 free_used_maps(prog->aux);
539free_prog:
540 bpf_prog_free(prog);
541 return err;
542}
543
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700544SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
545{
546 union bpf_attr attr = {};
547 int err;
548
549 /* the syscall is limited to root temporarily. This restriction will be
550 * lifted when security audit is clean. Note that eBPF+tracing must have
551 * this restriction, since it may pass kernel data to user space
552 */
553 if (!capable(CAP_SYS_ADMIN))
554 return -EPERM;
555
556 if (!access_ok(VERIFY_READ, uattr, 1))
557 return -EFAULT;
558
559 if (size > PAGE_SIZE) /* silly large */
560 return -E2BIG;
561
562 /* If we're handed a bigger struct than we know of,
563 * ensure all the unknown bits are 0 - i.e. new
564 * user-space does not rely on any kernel feature
565 * extensions we dont know about yet.
566 */
567 if (size > sizeof(attr)) {
568 unsigned char __user *addr;
569 unsigned char __user *end;
570 unsigned char val;
571
572 addr = (void __user *)uattr + sizeof(attr);
573 end = (void __user *)uattr + size;
574
575 for (; addr < end; addr++) {
576 err = get_user(val, addr);
577 if (err)
578 return err;
579 if (val)
580 return -E2BIG;
581 }
582 size = sizeof(attr);
583 }
584
585 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
586 if (copy_from_user(&attr, uattr, size) != 0)
587 return -EFAULT;
588
589 switch (cmd) {
590 case BPF_MAP_CREATE:
591 err = map_create(&attr);
592 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700593 case BPF_MAP_LOOKUP_ELEM:
594 err = map_lookup_elem(&attr);
595 break;
596 case BPF_MAP_UPDATE_ELEM:
597 err = map_update_elem(&attr);
598 break;
599 case BPF_MAP_DELETE_ELEM:
600 err = map_delete_elem(&attr);
601 break;
602 case BPF_MAP_GET_NEXT_KEY:
603 err = map_get_next_key(&attr);
604 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700605 case BPF_PROG_LOAD:
606 err = bpf_prog_load(&attr);
607 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700608 default:
609 err = -EINVAL;
610 break;
611 }
612
613 return err;
614}