blob: f640e5f7afbd7cece735fcbc1fe34a6d0355337a [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
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070021int sysctl_unprivileged_bpf_disabled __read_mostly;
22
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070023static LIST_HEAD(bpf_map_types);
24
25static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
26{
27 struct bpf_map_type_list *tl;
28 struct bpf_map *map;
29
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);
33 if (IS_ERR(map))
34 return map;
35 map->ops = tl->ops;
36 map->map_type = attr->map_type;
37 return map;
38 }
39 }
40 return ERR_PTR(-EINVAL);
41}
42
43/* boot time registration of different map implementations */
44void bpf_register_map_type(struct bpf_map_type_list *tl)
45{
46 list_add(&tl->list_node, &bpf_map_types);
47}
48
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070049static int bpf_map_charge_memlock(struct bpf_map *map)
50{
51 struct user_struct *user = get_current_user();
52 unsigned long memlock_limit;
53
54 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
55
56 atomic_long_add(map->pages, &user->locked_vm);
57
58 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
59 atomic_long_sub(map->pages, &user->locked_vm);
60 free_uid(user);
61 return -EPERM;
62 }
63 map->user = user;
64 return 0;
65}
66
67static void bpf_map_uncharge_memlock(struct bpf_map *map)
68{
69 struct user_struct *user = map->user;
70
71 atomic_long_sub(map->pages, &user->locked_vm);
72 free_uid(user);
73}
74
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070075/* called from workqueue */
76static void bpf_map_free_deferred(struct work_struct *work)
77{
78 struct bpf_map *map = container_of(work, struct bpf_map, work);
79
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -070080 bpf_map_uncharge_memlock(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070081 /* implementation dependent freeing */
82 map->ops->map_free(map);
83}
84
85/* decrement map refcnt and schedule it for freeing via workqueue
86 * (unrelying map implementation ops->map_free() might sleep)
87 */
88void bpf_map_put(struct bpf_map *map)
89{
90 if (atomic_dec_and_test(&map->refcnt)) {
91 INIT_WORK(&map->work, bpf_map_free_deferred);
92 schedule_work(&map->work);
93 }
94}
95
96static int bpf_map_release(struct inode *inode, struct file *filp)
97{
98 struct bpf_map *map = filp->private_data;
99
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700100 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
103 */
Wang Nan2a36f0b2015-08-06 07:02:33 +0000104 bpf_fd_array_map_clear(map);
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700105
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700106 bpf_map_put(map);
107 return 0;
108}
109
110static const struct file_operations bpf_map_fops = {
111 .release = bpf_map_release,
112};
113
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, \
118 sizeof(*attr) - \
119 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
120 sizeof(attr->CMD##_LAST_FIELD)) != NULL
121
122#define BPF_MAP_CREATE_LAST_FIELD max_entries
123/* called via syscall */
124static int map_create(union bpf_attr *attr)
125{
126 struct bpf_map *map;
127 int err;
128
129 err = CHECK_ATTR(BPF_MAP_CREATE);
130 if (err)
131 return -EINVAL;
132
133 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
134 map = find_and_alloc_map(attr);
135 if (IS_ERR(map))
136 return PTR_ERR(map);
137
138 atomic_set(&map->refcnt, 1);
139
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700140 err = bpf_map_charge_memlock(map);
141 if (err)
142 goto free_map;
143
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700144 err = anon_inode_getfd("bpf-map", &bpf_map_fops, map, O_RDWR | O_CLOEXEC);
145
146 if (err < 0)
147 /* failed to allocate fd */
148 goto free_map;
149
150 return err;
151
152free_map:
153 map->ops->map_free(map);
154 return err;
155}
156
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700157/* if error is returned, fd is released.
158 * On success caller should complete fd access with matching fdput()
159 */
160struct bpf_map *bpf_map_get(struct fd f)
161{
162 struct bpf_map *map;
163
164 if (!f.file)
165 return ERR_PTR(-EBADF);
166
167 if (f.file->f_op != &bpf_map_fops) {
168 fdput(f);
169 return ERR_PTR(-EINVAL);
170 }
171
172 map = f.file->private_data;
173
174 return map;
175}
176
177/* helper to convert user pointers passed inside __aligned_u64 fields */
178static void __user *u64_to_ptr(__u64 val)
179{
180 return (void __user *) (unsigned long) val;
181}
182
183/* last field in 'union bpf_attr' used by this command */
184#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
185
186static int map_lookup_elem(union bpf_attr *attr)
187{
188 void __user *ukey = u64_to_ptr(attr->key);
189 void __user *uvalue = u64_to_ptr(attr->value);
190 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700191 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800192 void *key, *value, *ptr;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200193 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700194 int err;
195
196 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
197 return -EINVAL;
198
Daniel Borkmann592867b2015-09-08 18:00:09 +0200199 f = fdget(ufd);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700200 map = bpf_map_get(f);
201 if (IS_ERR(map))
202 return PTR_ERR(map);
203
204 err = -ENOMEM;
205 key = kmalloc(map->key_size, GFP_USER);
206 if (!key)
207 goto err_put;
208
209 err = -EFAULT;
210 if (copy_from_user(key, ukey, map->key_size) != 0)
211 goto free_key;
212
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800213 err = -ENOMEM;
214 value = kmalloc(map->value_size, GFP_USER);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700215 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800216 goto free_key;
217
218 rcu_read_lock();
219 ptr = map->ops->map_lookup_elem(map, key);
220 if (ptr)
221 memcpy(value, ptr, map->value_size);
222 rcu_read_unlock();
223
224 err = -ENOENT;
225 if (!ptr)
226 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700227
228 err = -EFAULT;
229 if (copy_to_user(uvalue, value, map->value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800230 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700231
232 err = 0;
233
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800234free_value:
235 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700236free_key:
237 kfree(key);
238err_put:
239 fdput(f);
240 return err;
241}
242
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800243#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700244
245static int map_update_elem(union bpf_attr *attr)
246{
247 void __user *ukey = u64_to_ptr(attr->key);
248 void __user *uvalue = u64_to_ptr(attr->value);
249 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700250 struct bpf_map *map;
251 void *key, *value;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200252 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700253 int err;
254
255 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
256 return -EINVAL;
257
Daniel Borkmann592867b2015-09-08 18:00:09 +0200258 f = fdget(ufd);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700259 map = bpf_map_get(f);
260 if (IS_ERR(map))
261 return PTR_ERR(map);
262
263 err = -ENOMEM;
264 key = kmalloc(map->key_size, GFP_USER);
265 if (!key)
266 goto err_put;
267
268 err = -EFAULT;
269 if (copy_from_user(key, ukey, map->key_size) != 0)
270 goto free_key;
271
272 err = -ENOMEM;
273 value = kmalloc(map->value_size, GFP_USER);
274 if (!value)
275 goto free_key;
276
277 err = -EFAULT;
278 if (copy_from_user(value, uvalue, map->value_size) != 0)
279 goto free_value;
280
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
283 */
284 rcu_read_lock();
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800285 err = map->ops->map_update_elem(map, key, value, attr->flags);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700286 rcu_read_unlock();
287
288free_value:
289 kfree(value);
290free_key:
291 kfree(key);
292err_put:
293 fdput(f);
294 return err;
295}
296
297#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
298
299static int map_delete_elem(union bpf_attr *attr)
300{
301 void __user *ukey = u64_to_ptr(attr->key);
302 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700303 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200304 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700305 void *key;
306 int err;
307
308 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
309 return -EINVAL;
310
Daniel Borkmann592867b2015-09-08 18:00:09 +0200311 f = fdget(ufd);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700312 map = bpf_map_get(f);
313 if (IS_ERR(map))
314 return PTR_ERR(map);
315
316 err = -ENOMEM;
317 key = kmalloc(map->key_size, GFP_USER);
318 if (!key)
319 goto err_put;
320
321 err = -EFAULT;
322 if (copy_from_user(key, ukey, map->key_size) != 0)
323 goto free_key;
324
325 rcu_read_lock();
326 err = map->ops->map_delete_elem(map, key);
327 rcu_read_unlock();
328
329free_key:
330 kfree(key);
331err_put:
332 fdput(f);
333 return err;
334}
335
336/* last field in 'union bpf_attr' used by this command */
337#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
338
339static int map_get_next_key(union bpf_attr *attr)
340{
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;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700344 struct bpf_map *map;
345 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200346 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700347 int err;
348
349 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
350 return -EINVAL;
351
Daniel Borkmann592867b2015-09-08 18:00:09 +0200352 f = fdget(ufd);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700353 map = bpf_map_get(f);
354 if (IS_ERR(map))
355 return PTR_ERR(map);
356
357 err = -ENOMEM;
358 key = kmalloc(map->key_size, GFP_USER);
359 if (!key)
360 goto err_put;
361
362 err = -EFAULT;
363 if (copy_from_user(key, ukey, map->key_size) != 0)
364 goto free_key;
365
366 err = -ENOMEM;
367 next_key = kmalloc(map->key_size, GFP_USER);
368 if (!next_key)
369 goto free_key;
370
371 rcu_read_lock();
372 err = map->ops->map_get_next_key(map, key, next_key);
373 rcu_read_unlock();
374 if (err)
375 goto free_next_key;
376
377 err = -EFAULT;
378 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
379 goto free_next_key;
380
381 err = 0;
382
383free_next_key:
384 kfree(next_key);
385free_key:
386 kfree(key);
387err_put:
388 fdput(f);
389 return err;
390}
391
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700392static LIST_HEAD(bpf_prog_types);
393
394static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
395{
396 struct bpf_prog_type_list *tl;
397
398 list_for_each_entry(tl, &bpf_prog_types, list_node) {
399 if (tl->type == type) {
400 prog->aux->ops = tl->ops;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100401 prog->type = type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700402 return 0;
403 }
404 }
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100405
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700406 return -EINVAL;
407}
408
409void bpf_register_prog_type(struct bpf_prog_type_list *tl)
410{
411 list_add(&tl->list_node, &bpf_prog_types);
412}
413
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700414/* 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;
419 * else ...
420 *
421 * this function is called after eBPF program passed verification
422 */
423static void fixup_bpf_calls(struct bpf_prog *prog)
424{
425 const struct bpf_func_proto *fn;
426 int i;
427
428 for (i = 0; i < prog->len; i++) {
429 struct bpf_insn *insn = &prog->insnsi[i];
430
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
435 */
436 BUG_ON(!prog->aux->ops->get_func_proto);
437
Daniel Borkmannc46646d2015-09-30 01:41:51 +0200438 if (insn->imm == BPF_FUNC_get_route_realm)
439 prog->dst_needed = 1;
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200440 if (insn->imm == BPF_FUNC_get_prandom_u32)
441 bpf_user_rnd_init_once();
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700442 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
448 * bpf_tail_call yet
449 */
450 insn->imm = 0;
451 insn->code |= BPF_X;
452 continue;
453 }
454
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700455 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
458 */
459 BUG_ON(!fn->func);
460 insn->imm = fn->func - __bpf_call_base;
461 }
462 }
463}
464
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700465/* drop refcnt on maps used by eBPF program and free auxilary data */
466static void free_used_maps(struct bpf_prog_aux *aux)
467{
468 int i;
469
470 for (i = 0; i < aux->used_map_cnt; i++)
471 bpf_map_put(aux->used_maps[i]);
472
473 kfree(aux->used_maps);
474}
475
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700476static int bpf_prog_charge_memlock(struct bpf_prog *prog)
477{
478 struct user_struct *user = get_current_user();
479 unsigned long memlock_limit;
480
481 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
482
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);
486 free_uid(user);
487 return -EPERM;
488 }
489 prog->aux->user = user;
490 return 0;
491}
492
493static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
494{
495 struct user_struct *user = prog->aux->user;
496
497 atomic_long_sub(prog->pages, &user->locked_vm);
498 free_uid(user);
499}
500
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700501static void __prog_put_rcu(struct rcu_head *rcu)
502{
503 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
504
505 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700506 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700507 bpf_prog_free(aux->prog);
508}
509
510/* version of bpf_prog_put() that is called after a grace period */
511void bpf_prog_put_rcu(struct bpf_prog *prog)
512{
513 if (atomic_dec_and_test(&prog->aux->refcnt)) {
514 prog->aux->prog = prog;
515 call_rcu(&prog->aux->rcu, __prog_put_rcu);
516 }
517}
518
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700519void bpf_prog_put(struct bpf_prog *prog)
520{
521 if (atomic_dec_and_test(&prog->aux->refcnt)) {
522 free_used_maps(prog->aux);
523 bpf_prog_free(prog);
524 }
525}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100526EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700527
528static int bpf_prog_release(struct inode *inode, struct file *filp)
529{
530 struct bpf_prog *prog = filp->private_data;
531
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700532 bpf_prog_put_rcu(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700533 return 0;
534}
535
536static const struct file_operations bpf_prog_fops = {
537 .release = bpf_prog_release,
538};
539
540static struct bpf_prog *get_prog(struct fd f)
541{
542 struct bpf_prog *prog;
543
544 if (!f.file)
545 return ERR_PTR(-EBADF);
546
547 if (f.file->f_op != &bpf_prog_fops) {
548 fdput(f);
549 return ERR_PTR(-EINVAL);
550 }
551
552 prog = f.file->private_data;
553
554 return prog;
555}
556
557/* called by sockets/tracing/seccomp before attaching program to an event
558 * pairs with bpf_prog_put()
559 */
560struct bpf_prog *bpf_prog_get(u32 ufd)
561{
562 struct fd f = fdget(ufd);
563 struct bpf_prog *prog;
564
565 prog = get_prog(f);
566
567 if (IS_ERR(prog))
568 return prog;
569
570 atomic_inc(&prog->aux->refcnt);
571 fdput(f);
572 return prog;
573}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100574EXPORT_SYMBOL_GPL(bpf_prog_get);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700575
576/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700577#define BPF_PROG_LOAD_LAST_FIELD kern_version
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700578
579static int bpf_prog_load(union bpf_attr *attr)
580{
581 enum bpf_prog_type type = attr->prog_type;
582 struct bpf_prog *prog;
583 int err;
584 char license[128];
585 bool is_gpl;
586
587 if (CHECK_ATTR(BPF_PROG_LOAD))
588 return -EINVAL;
589
590 /* copy eBPF program license from user space */
591 if (strncpy_from_user(license, u64_to_ptr(attr->license),
592 sizeof(license) - 1) < 0)
593 return -EFAULT;
594 license[sizeof(license) - 1] = 0;
595
596 /* eBPF programs must be GPL compatible to use GPL-ed functions */
597 is_gpl = license_is_gpl_compatible(license);
598
599 if (attr->insn_cnt >= BPF_MAXINSNS)
600 return -EINVAL;
601
Alexei Starovoitov25415172015-03-25 12:49:20 -0700602 if (type == BPF_PROG_TYPE_KPROBE &&
603 attr->kern_version != LINUX_VERSION_CODE)
604 return -EINVAL;
605
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700606 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
607 return -EPERM;
608
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700609 /* plain bpf_prog allocation */
610 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
611 if (!prog)
612 return -ENOMEM;
613
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700614 err = bpf_prog_charge_memlock(prog);
615 if (err)
616 goto free_prog_nouncharge;
617
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700618 prog->len = attr->insn_cnt;
619
620 err = -EFAULT;
621 if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
622 prog->len * sizeof(struct bpf_insn)) != 0)
623 goto free_prog;
624
625 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200626 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700627
628 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200629 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700630
631 /* find program type: socket_filter vs tracing_filter */
632 err = find_prog_type(type, prog);
633 if (err < 0)
634 goto free_prog;
635
636 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700637 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700638 if (err < 0)
639 goto free_used_maps;
640
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700641 /* fixup BPF_CALL->imm field */
642 fixup_bpf_calls(prog);
643
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700644 /* eBPF program is ready to be JITed */
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700645 err = bpf_prog_select_runtime(prog);
646 if (err < 0)
647 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700648
649 err = anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog, O_RDWR | O_CLOEXEC);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700650 if (err < 0)
651 /* failed to allocate fd */
652 goto free_used_maps;
653
654 return err;
655
656free_used_maps:
657 free_used_maps(prog->aux);
658free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700659 bpf_prog_uncharge_memlock(prog);
660free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700661 bpf_prog_free(prog);
662 return err;
663}
664
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700665SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
666{
667 union bpf_attr attr = {};
668 int err;
669
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700670 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700671 return -EPERM;
672
673 if (!access_ok(VERIFY_READ, uattr, 1))
674 return -EFAULT;
675
676 if (size > PAGE_SIZE) /* silly large */
677 return -E2BIG;
678
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.
683 */
684 if (size > sizeof(attr)) {
685 unsigned char __user *addr;
686 unsigned char __user *end;
687 unsigned char val;
688
689 addr = (void __user *)uattr + sizeof(attr);
690 end = (void __user *)uattr + size;
691
692 for (; addr < end; addr++) {
693 err = get_user(val, addr);
694 if (err)
695 return err;
696 if (val)
697 return -E2BIG;
698 }
699 size = sizeof(attr);
700 }
701
702 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
703 if (copy_from_user(&attr, uattr, size) != 0)
704 return -EFAULT;
705
706 switch (cmd) {
707 case BPF_MAP_CREATE:
708 err = map_create(&attr);
709 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700710 case BPF_MAP_LOOKUP_ELEM:
711 err = map_lookup_elem(&attr);
712 break;
713 case BPF_MAP_UPDATE_ELEM:
714 err = map_update_elem(&attr);
715 break;
716 case BPF_MAP_DELETE_ELEM:
717 err = map_delete_elem(&attr);
718 break;
719 case BPF_MAP_GET_NEXT_KEY:
720 err = map_get_next_key(&attr);
721 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700722 case BPF_PROG_LOAD:
723 err = bpf_prog_load(&attr);
724 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700725 default:
726 err = -EINVAL;
727 break;
728 }
729
730 return err;
731}