blob: c868cafbc00c659939a6ec8ac9b808db5f8cd1f6 [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 Starovoitov25415172015-03-25 12:49:20 -070019#include <linux/version.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070020
21static LIST_HEAD(bpf_map_types);
22
23static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
24{
25 struct bpf_map_type_list *tl;
26 struct bpf_map *map;
27
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);
31 if (IS_ERR(map))
32 return map;
33 map->ops = tl->ops;
34 map->map_type = attr->map_type;
35 return map;
36 }
37 }
38 return ERR_PTR(-EINVAL);
39}
40
41/* boot time registration of different map implementations */
42void bpf_register_map_type(struct bpf_map_type_list *tl)
43{
44 list_add(&tl->list_node, &bpf_map_types);
45}
46
47/* called from workqueue */
48static void bpf_map_free_deferred(struct work_struct *work)
49{
50 struct bpf_map *map = container_of(work, struct bpf_map, work);
51
52 /* implementation dependent freeing */
53 map->ops->map_free(map);
54}
55
56/* decrement map refcnt and schedule it for freeing via workqueue
57 * (unrelying map implementation ops->map_free() might sleep)
58 */
59void bpf_map_put(struct bpf_map *map)
60{
61 if (atomic_dec_and_test(&map->refcnt)) {
62 INIT_WORK(&map->work, bpf_map_free_deferred);
63 schedule_work(&map->work);
64 }
65}
66
67static int bpf_map_release(struct inode *inode, struct file *filp)
68{
69 struct bpf_map *map = filp->private_data;
70
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -070071 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
72 /* prog_array stores refcnt-ed bpf_prog pointers
73 * release them all when user space closes prog_array_fd
74 */
Wang Nan2a36f0b2015-08-06 07:02:33 +000075 bpf_fd_array_map_clear(map);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -070076
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070077 bpf_map_put(map);
78 return 0;
79}
80
81static const struct file_operations bpf_map_fops = {
82 .release = bpf_map_release,
83};
84
85/* helper macro to check that unused fields 'union bpf_attr' are zero */
86#define CHECK_ATTR(CMD) \
87 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
88 sizeof(attr->CMD##_LAST_FIELD), 0, \
89 sizeof(*attr) - \
90 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
91 sizeof(attr->CMD##_LAST_FIELD)) != NULL
92
93#define BPF_MAP_CREATE_LAST_FIELD max_entries
94/* called via syscall */
95static int map_create(union bpf_attr *attr)
96{
97 struct bpf_map *map;
98 int err;
99
100 err = CHECK_ATTR(BPF_MAP_CREATE);
101 if (err)
102 return -EINVAL;
103
104 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
105 map = find_and_alloc_map(attr);
106 if (IS_ERR(map))
107 return PTR_ERR(map);
108
109 atomic_set(&map->refcnt, 1);
110
111 err = anon_inode_getfd("bpf-map", &bpf_map_fops, map, O_RDWR | O_CLOEXEC);
112
113 if (err < 0)
114 /* failed to allocate fd */
115 goto free_map;
116
117 return err;
118
119free_map:
120 map->ops->map_free(map);
121 return err;
122}
123
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700124/* if error is returned, fd is released.
125 * On success caller should complete fd access with matching fdput()
126 */
127struct bpf_map *bpf_map_get(struct fd f)
128{
129 struct bpf_map *map;
130
131 if (!f.file)
132 return ERR_PTR(-EBADF);
133
134 if (f.file->f_op != &bpf_map_fops) {
135 fdput(f);
136 return ERR_PTR(-EINVAL);
137 }
138
139 map = f.file->private_data;
140
141 return map;
142}
143
144/* helper to convert user pointers passed inside __aligned_u64 fields */
145static void __user *u64_to_ptr(__u64 val)
146{
147 return (void __user *) (unsigned long) val;
148}
149
150/* last field in 'union bpf_attr' used by this command */
151#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
152
153static int map_lookup_elem(union bpf_attr *attr)
154{
155 void __user *ukey = u64_to_ptr(attr->key);
156 void __user *uvalue = u64_to_ptr(attr->value);
157 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700158 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800159 void *key, *value, *ptr;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200160 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700161 int err;
162
163 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
164 return -EINVAL;
165
Daniel Borkmann592867b2015-09-08 18:00:09 +0200166 f = fdget(ufd);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700167 map = bpf_map_get(f);
168 if (IS_ERR(map))
169 return PTR_ERR(map);
170
171 err = -ENOMEM;
172 key = kmalloc(map->key_size, GFP_USER);
173 if (!key)
174 goto err_put;
175
176 err = -EFAULT;
177 if (copy_from_user(key, ukey, map->key_size) != 0)
178 goto free_key;
179
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800180 err = -ENOMEM;
181 value = kmalloc(map->value_size, GFP_USER);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700182 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800183 goto free_key;
184
185 rcu_read_lock();
186 ptr = map->ops->map_lookup_elem(map, key);
187 if (ptr)
188 memcpy(value, ptr, map->value_size);
189 rcu_read_unlock();
190
191 err = -ENOENT;
192 if (!ptr)
193 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700194
195 err = -EFAULT;
196 if (copy_to_user(uvalue, value, map->value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800197 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700198
199 err = 0;
200
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800201free_value:
202 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700203free_key:
204 kfree(key);
205err_put:
206 fdput(f);
207 return err;
208}
209
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800210#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700211
212static int map_update_elem(union bpf_attr *attr)
213{
214 void __user *ukey = u64_to_ptr(attr->key);
215 void __user *uvalue = u64_to_ptr(attr->value);
216 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700217 struct bpf_map *map;
218 void *key, *value;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200219 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700220 int err;
221
222 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
223 return -EINVAL;
224
Daniel Borkmann592867b2015-09-08 18:00:09 +0200225 f = fdget(ufd);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700226 map = bpf_map_get(f);
227 if (IS_ERR(map))
228 return PTR_ERR(map);
229
230 err = -ENOMEM;
231 key = kmalloc(map->key_size, GFP_USER);
232 if (!key)
233 goto err_put;
234
235 err = -EFAULT;
236 if (copy_from_user(key, ukey, map->key_size) != 0)
237 goto free_key;
238
239 err = -ENOMEM;
240 value = kmalloc(map->value_size, GFP_USER);
241 if (!value)
242 goto free_key;
243
244 err = -EFAULT;
245 if (copy_from_user(value, uvalue, map->value_size) != 0)
246 goto free_value;
247
248 /* eBPF program that use maps are running under rcu_read_lock(),
249 * therefore all map accessors rely on this fact, so do the same here
250 */
251 rcu_read_lock();
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800252 err = map->ops->map_update_elem(map, key, value, attr->flags);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700253 rcu_read_unlock();
254
255free_value:
256 kfree(value);
257free_key:
258 kfree(key);
259err_put:
260 fdput(f);
261 return err;
262}
263
264#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
265
266static int map_delete_elem(union bpf_attr *attr)
267{
268 void __user *ukey = u64_to_ptr(attr->key);
269 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700270 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200271 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700272 void *key;
273 int err;
274
275 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
276 return -EINVAL;
277
Daniel Borkmann592867b2015-09-08 18:00:09 +0200278 f = fdget(ufd);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700279 map = bpf_map_get(f);
280 if (IS_ERR(map))
281 return PTR_ERR(map);
282
283 err = -ENOMEM;
284 key = kmalloc(map->key_size, GFP_USER);
285 if (!key)
286 goto err_put;
287
288 err = -EFAULT;
289 if (copy_from_user(key, ukey, map->key_size) != 0)
290 goto free_key;
291
292 rcu_read_lock();
293 err = map->ops->map_delete_elem(map, key);
294 rcu_read_unlock();
295
296free_key:
297 kfree(key);
298err_put:
299 fdput(f);
300 return err;
301}
302
303/* last field in 'union bpf_attr' used by this command */
304#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
305
306static int map_get_next_key(union bpf_attr *attr)
307{
308 void __user *ukey = u64_to_ptr(attr->key);
309 void __user *unext_key = u64_to_ptr(attr->next_key);
310 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700311 struct bpf_map *map;
312 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200313 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700314 int err;
315
316 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
317 return -EINVAL;
318
Daniel Borkmann592867b2015-09-08 18:00:09 +0200319 f = fdget(ufd);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700320 map = bpf_map_get(f);
321 if (IS_ERR(map))
322 return PTR_ERR(map);
323
324 err = -ENOMEM;
325 key = kmalloc(map->key_size, GFP_USER);
326 if (!key)
327 goto err_put;
328
329 err = -EFAULT;
330 if (copy_from_user(key, ukey, map->key_size) != 0)
331 goto free_key;
332
333 err = -ENOMEM;
334 next_key = kmalloc(map->key_size, GFP_USER);
335 if (!next_key)
336 goto free_key;
337
338 rcu_read_lock();
339 err = map->ops->map_get_next_key(map, key, next_key);
340 rcu_read_unlock();
341 if (err)
342 goto free_next_key;
343
344 err = -EFAULT;
345 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
346 goto free_next_key;
347
348 err = 0;
349
350free_next_key:
351 kfree(next_key);
352free_key:
353 kfree(key);
354err_put:
355 fdput(f);
356 return err;
357}
358
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700359static LIST_HEAD(bpf_prog_types);
360
361static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
362{
363 struct bpf_prog_type_list *tl;
364
365 list_for_each_entry(tl, &bpf_prog_types, list_node) {
366 if (tl->type == type) {
367 prog->aux->ops = tl->ops;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100368 prog->type = type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700369 return 0;
370 }
371 }
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100372
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700373 return -EINVAL;
374}
375
376void bpf_register_prog_type(struct bpf_prog_type_list *tl)
377{
378 list_add(&tl->list_node, &bpf_prog_types);
379}
380
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700381/* fixup insn->imm field of bpf_call instructions:
382 * if (insn->imm == BPF_FUNC_map_lookup_elem)
383 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
384 * else if (insn->imm == BPF_FUNC_map_update_elem)
385 * insn->imm = bpf_map_update_elem - __bpf_call_base;
386 * else ...
387 *
388 * this function is called after eBPF program passed verification
389 */
390static void fixup_bpf_calls(struct bpf_prog *prog)
391{
392 const struct bpf_func_proto *fn;
393 int i;
394
395 for (i = 0; i < prog->len; i++) {
396 struct bpf_insn *insn = &prog->insnsi[i];
397
398 if (insn->code == (BPF_JMP | BPF_CALL)) {
399 /* we reach here when program has bpf_call instructions
400 * and it passed bpf_check(), means that
401 * ops->get_func_proto must have been supplied, check it
402 */
403 BUG_ON(!prog->aux->ops->get_func_proto);
404
Daniel Borkmannc46646d2015-09-30 01:41:51 +0200405 if (insn->imm == BPF_FUNC_get_route_realm)
406 prog->dst_needed = 1;
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200407 if (insn->imm == BPF_FUNC_get_prandom_u32)
408 bpf_user_rnd_init_once();
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700409 if (insn->imm == BPF_FUNC_tail_call) {
410 /* mark bpf_tail_call as different opcode
411 * to avoid conditional branch in
412 * interpeter for every normal call
413 * and to prevent accidental JITing by
414 * JIT compiler that doesn't support
415 * bpf_tail_call yet
416 */
417 insn->imm = 0;
418 insn->code |= BPF_X;
419 continue;
420 }
421
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700422 fn = prog->aux->ops->get_func_proto(insn->imm);
423 /* all functions that have prototype and verifier allowed
424 * programs to call them, must be real in-kernel functions
425 */
426 BUG_ON(!fn->func);
427 insn->imm = fn->func - __bpf_call_base;
428 }
429 }
430}
431
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700432/* drop refcnt on maps used by eBPF program and free auxilary data */
433static void free_used_maps(struct bpf_prog_aux *aux)
434{
435 int i;
436
437 for (i = 0; i < aux->used_map_cnt; i++)
438 bpf_map_put(aux->used_maps[i]);
439
440 kfree(aux->used_maps);
441}
442
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700443static void __prog_put_rcu(struct rcu_head *rcu)
444{
445 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
446
447 free_used_maps(aux);
448 bpf_prog_free(aux->prog);
449}
450
451/* version of bpf_prog_put() that is called after a grace period */
452void bpf_prog_put_rcu(struct bpf_prog *prog)
453{
454 if (atomic_dec_and_test(&prog->aux->refcnt)) {
455 prog->aux->prog = prog;
456 call_rcu(&prog->aux->rcu, __prog_put_rcu);
457 }
458}
459
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700460void bpf_prog_put(struct bpf_prog *prog)
461{
462 if (atomic_dec_and_test(&prog->aux->refcnt)) {
463 free_used_maps(prog->aux);
464 bpf_prog_free(prog);
465 }
466}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100467EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700468
469static int bpf_prog_release(struct inode *inode, struct file *filp)
470{
471 struct bpf_prog *prog = filp->private_data;
472
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700473 bpf_prog_put_rcu(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700474 return 0;
475}
476
477static const struct file_operations bpf_prog_fops = {
478 .release = bpf_prog_release,
479};
480
481static struct bpf_prog *get_prog(struct fd f)
482{
483 struct bpf_prog *prog;
484
485 if (!f.file)
486 return ERR_PTR(-EBADF);
487
488 if (f.file->f_op != &bpf_prog_fops) {
489 fdput(f);
490 return ERR_PTR(-EINVAL);
491 }
492
493 prog = f.file->private_data;
494
495 return prog;
496}
497
498/* called by sockets/tracing/seccomp before attaching program to an event
499 * pairs with bpf_prog_put()
500 */
501struct bpf_prog *bpf_prog_get(u32 ufd)
502{
503 struct fd f = fdget(ufd);
504 struct bpf_prog *prog;
505
506 prog = get_prog(f);
507
508 if (IS_ERR(prog))
509 return prog;
510
511 atomic_inc(&prog->aux->refcnt);
512 fdput(f);
513 return prog;
514}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100515EXPORT_SYMBOL_GPL(bpf_prog_get);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700516
517/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700518#define BPF_PROG_LOAD_LAST_FIELD kern_version
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700519
520static int bpf_prog_load(union bpf_attr *attr)
521{
522 enum bpf_prog_type type = attr->prog_type;
523 struct bpf_prog *prog;
524 int err;
525 char license[128];
526 bool is_gpl;
527
528 if (CHECK_ATTR(BPF_PROG_LOAD))
529 return -EINVAL;
530
531 /* copy eBPF program license from user space */
532 if (strncpy_from_user(license, u64_to_ptr(attr->license),
533 sizeof(license) - 1) < 0)
534 return -EFAULT;
535 license[sizeof(license) - 1] = 0;
536
537 /* eBPF programs must be GPL compatible to use GPL-ed functions */
538 is_gpl = license_is_gpl_compatible(license);
539
540 if (attr->insn_cnt >= BPF_MAXINSNS)
541 return -EINVAL;
542
Alexei Starovoitov25415172015-03-25 12:49:20 -0700543 if (type == BPF_PROG_TYPE_KPROBE &&
544 attr->kern_version != LINUX_VERSION_CODE)
545 return -EINVAL;
546
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700547 /* plain bpf_prog allocation */
548 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
549 if (!prog)
550 return -ENOMEM;
551
552 prog->len = attr->insn_cnt;
553
554 err = -EFAULT;
555 if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
556 prog->len * sizeof(struct bpf_insn)) != 0)
557 goto free_prog;
558
559 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200560 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700561
562 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200563 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700564
565 /* find program type: socket_filter vs tracing_filter */
566 err = find_prog_type(type, prog);
567 if (err < 0)
568 goto free_prog;
569
570 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700571 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700572 if (err < 0)
573 goto free_used_maps;
574
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700575 /* fixup BPF_CALL->imm field */
576 fixup_bpf_calls(prog);
577
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700578 /* eBPF program is ready to be JITed */
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -0700579 err = bpf_prog_select_runtime(prog);
580 if (err < 0)
581 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700582
583 err = anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, O_RDWR | O_CLOEXEC);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700584 if (err < 0)
585 /* failed to allocate fd */
586 goto free_used_maps;
587
588 return err;
589
590free_used_maps:
591 free_used_maps(prog->aux);
592free_prog:
593 bpf_prog_free(prog);
594 return err;
595}
596
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700597SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
598{
599 union bpf_attr attr = {};
600 int err;
601
602 /* the syscall is limited to root temporarily. This restriction will be
603 * lifted when security audit is clean. Note that eBPF+tracing must have
604 * this restriction, since it may pass kernel data to user space
605 */
606 if (!capable(CAP_SYS_ADMIN))
607 return -EPERM;
608
609 if (!access_ok(VERIFY_READ, uattr, 1))
610 return -EFAULT;
611
612 if (size > PAGE_SIZE) /* silly large */
613 return -E2BIG;
614
615 /* If we're handed a bigger struct than we know of,
616 * ensure all the unknown bits are 0 - i.e. new
617 * user-space does not rely on any kernel feature
618 * extensions we dont know about yet.
619 */
620 if (size > sizeof(attr)) {
621 unsigned char __user *addr;
622 unsigned char __user *end;
623 unsigned char val;
624
625 addr = (void __user *)uattr + sizeof(attr);
626 end = (void __user *)uattr + size;
627
628 for (; addr < end; addr++) {
629 err = get_user(val, addr);
630 if (err)
631 return err;
632 if (val)
633 return -E2BIG;
634 }
635 size = sizeof(attr);
636 }
637
638 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
639 if (copy_from_user(&attr, uattr, size) != 0)
640 return -EFAULT;
641
642 switch (cmd) {
643 case BPF_MAP_CREATE:
644 err = map_create(&attr);
645 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700646 case BPF_MAP_LOOKUP_ELEM:
647 err = map_lookup_elem(&attr);
648 break;
649 case BPF_MAP_UPDATE_ELEM:
650 err = map_update_elem(&attr);
651 break;
652 case BPF_MAP_DELETE_ELEM:
653 err = map_delete_elem(&attr);
654 break;
655 case BPF_MAP_GET_NEXT_KEY:
656 err = map_get_next_key(&attr);
657 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700658 case BPF_PROG_LOAD:
659 err = bpf_prog_load(&attr);
660 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700661 default:
662 err = -EINVAL;
663 break;
664 }
665
666 return err;
667}