blob: 2b89ef0a9757fbf104cd110bd5c64e6f4590cad6 [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
Daniel Borkmannaa797812015-10-29 14:58:06 +0100114static int bpf_map_new_fd(struct bpf_map *map)
115{
116 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
117 O_RDWR | O_CLOEXEC);
118}
119
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700120/* helper macro to check that unused fields 'union bpf_attr' are zero */
121#define CHECK_ATTR(CMD) \
122 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
123 sizeof(attr->CMD##_LAST_FIELD), 0, \
124 sizeof(*attr) - \
125 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
126 sizeof(attr->CMD##_LAST_FIELD)) != NULL
127
128#define BPF_MAP_CREATE_LAST_FIELD max_entries
129/* called via syscall */
130static int map_create(union bpf_attr *attr)
131{
132 struct bpf_map *map;
133 int err;
134
135 err = CHECK_ATTR(BPF_MAP_CREATE);
136 if (err)
137 return -EINVAL;
138
139 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
140 map = find_and_alloc_map(attr);
141 if (IS_ERR(map))
142 return PTR_ERR(map);
143
144 atomic_set(&map->refcnt, 1);
145
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700146 err = bpf_map_charge_memlock(map);
147 if (err)
148 goto free_map;
149
Daniel Borkmannaa797812015-10-29 14:58:06 +0100150 err = bpf_map_new_fd(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700151 if (err < 0)
152 /* failed to allocate fd */
153 goto free_map;
154
155 return err;
156
157free_map:
158 map->ops->map_free(map);
159 return err;
160}
161
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700162/* if error is returned, fd is released.
163 * On success caller should complete fd access with matching fdput()
164 */
165struct bpf_map *bpf_map_get(struct fd f)
166{
167 struct bpf_map *map;
168
169 if (!f.file)
170 return ERR_PTR(-EBADF);
171
172 if (f.file->f_op != &bpf_map_fops) {
173 fdput(f);
174 return ERR_PTR(-EINVAL);
175 }
176
177 map = f.file->private_data;
178
179 return map;
180}
181
182/* helper to convert user pointers passed inside __aligned_u64 fields */
183static void __user *u64_to_ptr(__u64 val)
184{
185 return (void __user *) (unsigned long) val;
186}
187
188/* last field in 'union bpf_attr' used by this command */
189#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
190
191static int map_lookup_elem(union bpf_attr *attr)
192{
193 void __user *ukey = u64_to_ptr(attr->key);
194 void __user *uvalue = u64_to_ptr(attr->value);
195 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700196 struct bpf_map *map;
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800197 void *key, *value, *ptr;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200198 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700199 int err;
200
201 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
202 return -EINVAL;
203
Daniel Borkmann592867b2015-09-08 18:00:09 +0200204 f = fdget(ufd);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700205 map = bpf_map_get(f);
206 if (IS_ERR(map))
207 return PTR_ERR(map);
208
209 err = -ENOMEM;
210 key = kmalloc(map->key_size, GFP_USER);
211 if (!key)
212 goto err_put;
213
214 err = -EFAULT;
215 if (copy_from_user(key, ukey, map->key_size) != 0)
216 goto free_key;
217
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800218 err = -ENOMEM;
219 value = kmalloc(map->value_size, GFP_USER);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700220 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800221 goto free_key;
222
223 rcu_read_lock();
224 ptr = map->ops->map_lookup_elem(map, key);
225 if (ptr)
226 memcpy(value, ptr, map->value_size);
227 rcu_read_unlock();
228
229 err = -ENOENT;
230 if (!ptr)
231 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700232
233 err = -EFAULT;
234 if (copy_to_user(uvalue, value, map->value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800235 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700236
237 err = 0;
238
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -0800239free_value:
240 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700241free_key:
242 kfree(key);
243err_put:
244 fdput(f);
245 return err;
246}
247
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800248#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700249
250static int map_update_elem(union bpf_attr *attr)
251{
252 void __user *ukey = u64_to_ptr(attr->key);
253 void __user *uvalue = u64_to_ptr(attr->value);
254 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700255 struct bpf_map *map;
256 void *key, *value;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200257 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700258 int err;
259
260 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
261 return -EINVAL;
262
Daniel Borkmann592867b2015-09-08 18:00:09 +0200263 f = fdget(ufd);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700264 map = bpf_map_get(f);
265 if (IS_ERR(map))
266 return PTR_ERR(map);
267
268 err = -ENOMEM;
269 key = kmalloc(map->key_size, GFP_USER);
270 if (!key)
271 goto err_put;
272
273 err = -EFAULT;
274 if (copy_from_user(key, ukey, map->key_size) != 0)
275 goto free_key;
276
277 err = -ENOMEM;
278 value = kmalloc(map->value_size, GFP_USER);
279 if (!value)
280 goto free_key;
281
282 err = -EFAULT;
283 if (copy_from_user(value, uvalue, map->value_size) != 0)
284 goto free_value;
285
286 /* eBPF program that use maps are running under rcu_read_lock(),
287 * therefore all map accessors rely on this fact, so do the same here
288 */
289 rcu_read_lock();
Alexei Starovoitov3274f522014-11-13 17:36:44 -0800290 err = map->ops->map_update_elem(map, key, value, attr->flags);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700291 rcu_read_unlock();
292
293free_value:
294 kfree(value);
295free_key:
296 kfree(key);
297err_put:
298 fdput(f);
299 return err;
300}
301
302#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
303
304static int map_delete_elem(union bpf_attr *attr)
305{
306 void __user *ukey = u64_to_ptr(attr->key);
307 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700308 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200309 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700310 void *key;
311 int err;
312
313 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
314 return -EINVAL;
315
Daniel Borkmann592867b2015-09-08 18:00:09 +0200316 f = fdget(ufd);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700317 map = bpf_map_get(f);
318 if (IS_ERR(map))
319 return PTR_ERR(map);
320
321 err = -ENOMEM;
322 key = kmalloc(map->key_size, GFP_USER);
323 if (!key)
324 goto err_put;
325
326 err = -EFAULT;
327 if (copy_from_user(key, ukey, map->key_size) != 0)
328 goto free_key;
329
330 rcu_read_lock();
331 err = map->ops->map_delete_elem(map, key);
332 rcu_read_unlock();
333
334free_key:
335 kfree(key);
336err_put:
337 fdput(f);
338 return err;
339}
340
341/* last field in 'union bpf_attr' used by this command */
342#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
343
344static int map_get_next_key(union bpf_attr *attr)
345{
346 void __user *ukey = u64_to_ptr(attr->key);
347 void __user *unext_key = u64_to_ptr(attr->next_key);
348 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700349 struct bpf_map *map;
350 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200351 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700352 int err;
353
354 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
355 return -EINVAL;
356
Daniel Borkmann592867b2015-09-08 18:00:09 +0200357 f = fdget(ufd);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700358 map = bpf_map_get(f);
359 if (IS_ERR(map))
360 return PTR_ERR(map);
361
362 err = -ENOMEM;
363 key = kmalloc(map->key_size, GFP_USER);
364 if (!key)
365 goto err_put;
366
367 err = -EFAULT;
368 if (copy_from_user(key, ukey, map->key_size) != 0)
369 goto free_key;
370
371 err = -ENOMEM;
372 next_key = kmalloc(map->key_size, GFP_USER);
373 if (!next_key)
374 goto free_key;
375
376 rcu_read_lock();
377 err = map->ops->map_get_next_key(map, key, next_key);
378 rcu_read_unlock();
379 if (err)
380 goto free_next_key;
381
382 err = -EFAULT;
383 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
384 goto free_next_key;
385
386 err = 0;
387
388free_next_key:
389 kfree(next_key);
390free_key:
391 kfree(key);
392err_put:
393 fdput(f);
394 return err;
395}
396
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700397static LIST_HEAD(bpf_prog_types);
398
399static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
400{
401 struct bpf_prog_type_list *tl;
402
403 list_for_each_entry(tl, &bpf_prog_types, list_node) {
404 if (tl->type == type) {
405 prog->aux->ops = tl->ops;
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100406 prog->type = type;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700407 return 0;
408 }
409 }
Daniel Borkmann24701ec2015-03-01 12:31:47 +0100410
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700411 return -EINVAL;
412}
413
414void bpf_register_prog_type(struct bpf_prog_type_list *tl)
415{
416 list_add(&tl->list_node, &bpf_prog_types);
417}
418
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700419/* fixup insn->imm field of bpf_call instructions:
420 * if (insn->imm == BPF_FUNC_map_lookup_elem)
421 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
422 * else if (insn->imm == BPF_FUNC_map_update_elem)
423 * insn->imm = bpf_map_update_elem - __bpf_call_base;
424 * else ...
425 *
426 * this function is called after eBPF program passed verification
427 */
428static void fixup_bpf_calls(struct bpf_prog *prog)
429{
430 const struct bpf_func_proto *fn;
431 int i;
432
433 for (i = 0; i < prog->len; i++) {
434 struct bpf_insn *insn = &prog->insnsi[i];
435
436 if (insn->code == (BPF_JMP | BPF_CALL)) {
437 /* we reach here when program has bpf_call instructions
438 * and it passed bpf_check(), means that
439 * ops->get_func_proto must have been supplied, check it
440 */
441 BUG_ON(!prog->aux->ops->get_func_proto);
442
Daniel Borkmannc46646d2015-09-30 01:41:51 +0200443 if (insn->imm == BPF_FUNC_get_route_realm)
444 prog->dst_needed = 1;
Daniel Borkmann3ad00402015-10-08 01:20:39 +0200445 if (insn->imm == BPF_FUNC_get_prandom_u32)
446 bpf_user_rnd_init_once();
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700447 if (insn->imm == BPF_FUNC_tail_call) {
448 /* mark bpf_tail_call as different opcode
449 * to avoid conditional branch in
450 * interpeter for every normal call
451 * and to prevent accidental JITing by
452 * JIT compiler that doesn't support
453 * bpf_tail_call yet
454 */
455 insn->imm = 0;
456 insn->code |= BPF_X;
457 continue;
458 }
459
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700460 fn = prog->aux->ops->get_func_proto(insn->imm);
461 /* all functions that have prototype and verifier allowed
462 * programs to call them, must be real in-kernel functions
463 */
464 BUG_ON(!fn->func);
465 insn->imm = fn->func - __bpf_call_base;
466 }
467 }
468}
469
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700470/* drop refcnt on maps used by eBPF program and free auxilary data */
471static void free_used_maps(struct bpf_prog_aux *aux)
472{
473 int i;
474
475 for (i = 0; i < aux->used_map_cnt; i++)
476 bpf_map_put(aux->used_maps[i]);
477
478 kfree(aux->used_maps);
479}
480
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700481static int bpf_prog_charge_memlock(struct bpf_prog *prog)
482{
483 struct user_struct *user = get_current_user();
484 unsigned long memlock_limit;
485
486 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
487
488 atomic_long_add(prog->pages, &user->locked_vm);
489 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
490 atomic_long_sub(prog->pages, &user->locked_vm);
491 free_uid(user);
492 return -EPERM;
493 }
494 prog->aux->user = user;
495 return 0;
496}
497
498static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
499{
500 struct user_struct *user = prog->aux->user;
501
502 atomic_long_sub(prog->pages, &user->locked_vm);
503 free_uid(user);
504}
505
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700506static void __prog_put_rcu(struct rcu_head *rcu)
507{
508 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
509
510 free_used_maps(aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700511 bpf_prog_uncharge_memlock(aux->prog);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700512 bpf_prog_free(aux->prog);
513}
514
515/* version of bpf_prog_put() that is called after a grace period */
516void bpf_prog_put_rcu(struct bpf_prog *prog)
517{
518 if (atomic_dec_and_test(&prog->aux->refcnt)) {
519 prog->aux->prog = prog;
520 call_rcu(&prog->aux->rcu, __prog_put_rcu);
521 }
522}
523
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700524void bpf_prog_put(struct bpf_prog *prog)
525{
526 if (atomic_dec_and_test(&prog->aux->refcnt)) {
527 free_used_maps(prog->aux);
Tom Herbertac007372015-10-14 14:40:44 -0700528 bpf_prog_uncharge_memlock(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700529 bpf_prog_free(prog);
530 }
531}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100532EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700533
534static int bpf_prog_release(struct inode *inode, struct file *filp)
535{
536 struct bpf_prog *prog = filp->private_data;
537
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -0700538 bpf_prog_put_rcu(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700539 return 0;
540}
541
542static const struct file_operations bpf_prog_fops = {
543 .release = bpf_prog_release,
544};
545
Daniel Borkmannaa797812015-10-29 14:58:06 +0100546static int bpf_prog_new_fd(struct bpf_prog *prog)
547{
548 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
549 O_RDWR | O_CLOEXEC);
550}
551
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700552static struct bpf_prog *get_prog(struct fd f)
553{
554 struct bpf_prog *prog;
555
556 if (!f.file)
557 return ERR_PTR(-EBADF);
558
559 if (f.file->f_op != &bpf_prog_fops) {
560 fdput(f);
561 return ERR_PTR(-EINVAL);
562 }
563
564 prog = f.file->private_data;
565
566 return prog;
567}
568
569/* called by sockets/tracing/seccomp before attaching program to an event
570 * pairs with bpf_prog_put()
571 */
572struct bpf_prog *bpf_prog_get(u32 ufd)
573{
574 struct fd f = fdget(ufd);
575 struct bpf_prog *prog;
576
577 prog = get_prog(f);
578
579 if (IS_ERR(prog))
580 return prog;
581
582 atomic_inc(&prog->aux->refcnt);
583 fdput(f);
584 return prog;
585}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +0100586EXPORT_SYMBOL_GPL(bpf_prog_get);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700587
588/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov25415172015-03-25 12:49:20 -0700589#define BPF_PROG_LOAD_LAST_FIELD kern_version
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700590
591static int bpf_prog_load(union bpf_attr *attr)
592{
593 enum bpf_prog_type type = attr->prog_type;
594 struct bpf_prog *prog;
595 int err;
596 char license[128];
597 bool is_gpl;
598
599 if (CHECK_ATTR(BPF_PROG_LOAD))
600 return -EINVAL;
601
602 /* copy eBPF program license from user space */
603 if (strncpy_from_user(license, u64_to_ptr(attr->license),
604 sizeof(license) - 1) < 0)
605 return -EFAULT;
606 license[sizeof(license) - 1] = 0;
607
608 /* eBPF programs must be GPL compatible to use GPL-ed functions */
609 is_gpl = license_is_gpl_compatible(license);
610
611 if (attr->insn_cnt >= BPF_MAXINSNS)
612 return -EINVAL;
613
Alexei Starovoitov25415172015-03-25 12:49:20 -0700614 if (type == BPF_PROG_TYPE_KPROBE &&
615 attr->kern_version != LINUX_VERSION_CODE)
616 return -EINVAL;
617
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700618 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
619 return -EPERM;
620
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700621 /* plain bpf_prog allocation */
622 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
623 if (!prog)
624 return -ENOMEM;
625
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700626 err = bpf_prog_charge_memlock(prog);
627 if (err)
628 goto free_prog_nouncharge;
629
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700630 prog->len = attr->insn_cnt;
631
632 err = -EFAULT;
633 if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
634 prog->len * sizeof(struct bpf_insn)) != 0)
635 goto free_prog;
636
637 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200638 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700639
640 atomic_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +0200641 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700642
643 /* find program type: socket_filter vs tracing_filter */
644 err = find_prog_type(type, prog);
645 if (err < 0)
646 goto free_prog;
647
648 /* run eBPF verifier */
Alexei Starovoitov9bac3d62015-03-13 11:57:42 -0700649 err = bpf_check(&prog, attr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700650 if (err < 0)
651 goto free_used_maps;
652
Alexei Starovoitov0a542a82014-09-26 00:17:01 -0700653 /* fixup BPF_CALL->imm field */
654 fixup_bpf_calls(prog);
655
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700656 /* eBPF program is ready to be JITed */
Alexei Starovoitov04fd61a2015-05-19 16:59:03 -0700657 err = bpf_prog_select_runtime(prog);
658 if (err < 0)
659 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700660
Daniel Borkmannaa797812015-10-29 14:58:06 +0100661 err = bpf_prog_new_fd(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700662 if (err < 0)
663 /* failed to allocate fd */
664 goto free_used_maps;
665
666 return err;
667
668free_used_maps:
669 free_used_maps(prog->aux);
670free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700671 bpf_prog_uncharge_memlock(prog);
672free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700673 bpf_prog_free(prog);
674 return err;
675}
676
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700677SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
678{
679 union bpf_attr attr = {};
680 int err;
681
Alexei Starovoitov1be7f752015-10-07 22:23:21 -0700682 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700683 return -EPERM;
684
685 if (!access_ok(VERIFY_READ, uattr, 1))
686 return -EFAULT;
687
688 if (size > PAGE_SIZE) /* silly large */
689 return -E2BIG;
690
691 /* If we're handed a bigger struct than we know of,
692 * ensure all the unknown bits are 0 - i.e. new
693 * user-space does not rely on any kernel feature
694 * extensions we dont know about yet.
695 */
696 if (size > sizeof(attr)) {
697 unsigned char __user *addr;
698 unsigned char __user *end;
699 unsigned char val;
700
701 addr = (void __user *)uattr + sizeof(attr);
702 end = (void __user *)uattr + size;
703
704 for (; addr < end; addr++) {
705 err = get_user(val, addr);
706 if (err)
707 return err;
708 if (val)
709 return -E2BIG;
710 }
711 size = sizeof(attr);
712 }
713
714 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
715 if (copy_from_user(&attr, uattr, size) != 0)
716 return -EFAULT;
717
718 switch (cmd) {
719 case BPF_MAP_CREATE:
720 err = map_create(&attr);
721 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700722 case BPF_MAP_LOOKUP_ELEM:
723 err = map_lookup_elem(&attr);
724 break;
725 case BPF_MAP_UPDATE_ELEM:
726 err = map_update_elem(&attr);
727 break;
728 case BPF_MAP_DELETE_ELEM:
729 err = map_delete_elem(&attr);
730 break;
731 case BPF_MAP_GET_NEXT_KEY:
732 err = map_get_next_key(&attr);
733 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -0700734 case BPF_PROG_LOAD:
735 err = bpf_prog_load(&attr);
736 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700737 default:
738 err = -EINVAL;
739 break;
740 }
741
742 return err;
743}