Merge branch 'variable-length-ll-headers'
[deliverable/linux.git] / kernel / bpf / syscall.c
CommitLineData
99c55f7d
AS
1/* 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>
db20fd2b 16#include <linux/file.h>
09756af4
AS
17#include <linux/license.h>
18#include <linux/filter.h>
2541517c 19#include <linux/version.h>
99c55f7d 20
b121d1e7
AS
21DEFINE_PER_CPU(int, bpf_prog_active);
22
1be7f75d
AS
23int sysctl_unprivileged_bpf_disabled __read_mostly;
24
99c55f7d
AS
25static LIST_HEAD(bpf_map_types);
26
27static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
28{
29 struct bpf_map_type_list *tl;
30 struct bpf_map *map;
31
32 list_for_each_entry(tl, &bpf_map_types, list_node) {
33 if (tl->type == attr->map_type) {
34 map = tl->ops->map_alloc(attr);
35 if (IS_ERR(map))
36 return map;
37 map->ops = tl->ops;
38 map->map_type = attr->map_type;
39 return map;
40 }
41 }
42 return ERR_PTR(-EINVAL);
43}
44
45/* boot time registration of different map implementations */
46void bpf_register_map_type(struct bpf_map_type_list *tl)
47{
48 list_add(&tl->list_node, &bpf_map_types);
49}
50
6c905981
AS
51int bpf_map_precharge_memlock(u32 pages)
52{
53 struct user_struct *user = get_current_user();
54 unsigned long memlock_limit, cur;
55
56 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
57 cur = atomic_long_read(&user->locked_vm);
58 free_uid(user);
59 if (cur + pages > memlock_limit)
60 return -EPERM;
61 return 0;
62}
63
aaac3ba9
AS
64static int bpf_map_charge_memlock(struct bpf_map *map)
65{
66 struct user_struct *user = get_current_user();
67 unsigned long memlock_limit;
68
69 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
70
71 atomic_long_add(map->pages, &user->locked_vm);
72
73 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
74 atomic_long_sub(map->pages, &user->locked_vm);
75 free_uid(user);
76 return -EPERM;
77 }
78 map->user = user;
79 return 0;
80}
81
82static void bpf_map_uncharge_memlock(struct bpf_map *map)
83{
84 struct user_struct *user = map->user;
85
86 atomic_long_sub(map->pages, &user->locked_vm);
87 free_uid(user);
88}
89
99c55f7d
AS
90/* called from workqueue */
91static void bpf_map_free_deferred(struct work_struct *work)
92{
93 struct bpf_map *map = container_of(work, struct bpf_map, work);
94
aaac3ba9 95 bpf_map_uncharge_memlock(map);
99c55f7d
AS
96 /* implementation dependent freeing */
97 map->ops->map_free(map);
98}
99
c9da161c
DB
100static void bpf_map_put_uref(struct bpf_map *map)
101{
102 if (atomic_dec_and_test(&map->usercnt)) {
103 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
104 bpf_fd_array_map_clear(map);
105 }
106}
107
99c55f7d
AS
108/* decrement map refcnt and schedule it for freeing via workqueue
109 * (unrelying map implementation ops->map_free() might sleep)
110 */
111void bpf_map_put(struct bpf_map *map)
112{
113 if (atomic_dec_and_test(&map->refcnt)) {
114 INIT_WORK(&map->work, bpf_map_free_deferred);
115 schedule_work(&map->work);
116 }
117}
118
c9da161c 119void bpf_map_put_with_uref(struct bpf_map *map)
99c55f7d 120{
c9da161c 121 bpf_map_put_uref(map);
99c55f7d 122 bpf_map_put(map);
c9da161c
DB
123}
124
125static int bpf_map_release(struct inode *inode, struct file *filp)
126{
127 bpf_map_put_with_uref(filp->private_data);
99c55f7d
AS
128 return 0;
129}
130
f99bf205
DB
131#ifdef CONFIG_PROC_FS
132static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
133{
134 const struct bpf_map *map = filp->private_data;
135
136 seq_printf(m,
137 "map_type:\t%u\n"
138 "key_size:\t%u\n"
139 "value_size:\t%u\n"
140 "max_entries:\t%u\n",
141 map->map_type,
142 map->key_size,
143 map->value_size,
144 map->max_entries);
145}
146#endif
147
99c55f7d 148static const struct file_operations bpf_map_fops = {
f99bf205
DB
149#ifdef CONFIG_PROC_FS
150 .show_fdinfo = bpf_map_show_fdinfo,
151#endif
152 .release = bpf_map_release,
99c55f7d
AS
153};
154
b2197755 155int bpf_map_new_fd(struct bpf_map *map)
aa79781b
DB
156{
157 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
158 O_RDWR | O_CLOEXEC);
159}
160
99c55f7d
AS
161/* helper macro to check that unused fields 'union bpf_attr' are zero */
162#define CHECK_ATTR(CMD) \
163 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
164 sizeof(attr->CMD##_LAST_FIELD), 0, \
165 sizeof(*attr) - \
166 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
167 sizeof(attr->CMD##_LAST_FIELD)) != NULL
168
6c905981 169#define BPF_MAP_CREATE_LAST_FIELD map_flags
99c55f7d
AS
170/* called via syscall */
171static int map_create(union bpf_attr *attr)
172{
173 struct bpf_map *map;
174 int err;
175
176 err = CHECK_ATTR(BPF_MAP_CREATE);
177 if (err)
178 return -EINVAL;
179
180 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
181 map = find_and_alloc_map(attr);
182 if (IS_ERR(map))
183 return PTR_ERR(map);
184
185 atomic_set(&map->refcnt, 1);
c9da161c 186 atomic_set(&map->usercnt, 1);
99c55f7d 187
aaac3ba9
AS
188 err = bpf_map_charge_memlock(map);
189 if (err)
190 goto free_map;
191
aa79781b 192 err = bpf_map_new_fd(map);
99c55f7d
AS
193 if (err < 0)
194 /* failed to allocate fd */
195 goto free_map;
196
197 return err;
198
199free_map:
200 map->ops->map_free(map);
201 return err;
202}
203
db20fd2b
AS
204/* if error is returned, fd is released.
205 * On success caller should complete fd access with matching fdput()
206 */
c2101297 207struct bpf_map *__bpf_map_get(struct fd f)
db20fd2b 208{
db20fd2b
AS
209 if (!f.file)
210 return ERR_PTR(-EBADF);
db20fd2b
AS
211 if (f.file->f_op != &bpf_map_fops) {
212 fdput(f);
213 return ERR_PTR(-EINVAL);
214 }
215
c2101297
DB
216 return f.file->private_data;
217}
218
c9da161c
DB
219void bpf_map_inc(struct bpf_map *map, bool uref)
220{
221 atomic_inc(&map->refcnt);
222 if (uref)
223 atomic_inc(&map->usercnt);
224}
225
226struct bpf_map *bpf_map_get_with_uref(u32 ufd)
c2101297
DB
227{
228 struct fd f = fdget(ufd);
229 struct bpf_map *map;
230
231 map = __bpf_map_get(f);
232 if (IS_ERR(map))
233 return map;
234
c9da161c 235 bpf_map_inc(map, true);
c2101297 236 fdput(f);
db20fd2b
AS
237
238 return map;
239}
240
241/* helper to convert user pointers passed inside __aligned_u64 fields */
242static void __user *u64_to_ptr(__u64 val)
243{
244 return (void __user *) (unsigned long) val;
245}
246
247/* last field in 'union bpf_attr' used by this command */
248#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
249
250static int map_lookup_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;
db20fd2b 255 struct bpf_map *map;
8ebe667c 256 void *key, *value, *ptr;
15a07b33 257 u32 value_size;
592867bf 258 struct fd f;
db20fd2b
AS
259 int err;
260
261 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
262 return -EINVAL;
263
592867bf 264 f = fdget(ufd);
c2101297 265 map = __bpf_map_get(f);
db20fd2b
AS
266 if (IS_ERR(map))
267 return PTR_ERR(map);
268
269 err = -ENOMEM;
270 key = kmalloc(map->key_size, GFP_USER);
271 if (!key)
272 goto err_put;
273
274 err = -EFAULT;
275 if (copy_from_user(key, ukey, map->key_size) != 0)
276 goto free_key;
277
15a07b33
AS
278 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
279 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
280 value_size = round_up(map->value_size, 8) * num_possible_cpus();
281 else
282 value_size = map->value_size;
283
8ebe667c 284 err = -ENOMEM;
15a07b33 285 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
db20fd2b 286 if (!value)
8ebe667c
AS
287 goto free_key;
288
15a07b33
AS
289 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
290 err = bpf_percpu_hash_copy(map, key, value);
291 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
292 err = bpf_percpu_array_copy(map, key, value);
557c0c6e
AS
293 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
294 err = bpf_stackmap_copy(map, key, value);
15a07b33
AS
295 } else {
296 rcu_read_lock();
297 ptr = map->ops->map_lookup_elem(map, key);
298 if (ptr)
299 memcpy(value, ptr, value_size);
300 rcu_read_unlock();
301 err = ptr ? 0 : -ENOENT;
302 }
8ebe667c 303
15a07b33 304 if (err)
8ebe667c 305 goto free_value;
db20fd2b
AS
306
307 err = -EFAULT;
15a07b33 308 if (copy_to_user(uvalue, value, value_size) != 0)
8ebe667c 309 goto free_value;
db20fd2b
AS
310
311 err = 0;
312
8ebe667c
AS
313free_value:
314 kfree(value);
db20fd2b
AS
315free_key:
316 kfree(key);
317err_put:
318 fdput(f);
319 return err;
320}
321
3274f520 322#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
db20fd2b
AS
323
324static int map_update_elem(union bpf_attr *attr)
325{
326 void __user *ukey = u64_to_ptr(attr->key);
327 void __user *uvalue = u64_to_ptr(attr->value);
328 int ufd = attr->map_fd;
db20fd2b
AS
329 struct bpf_map *map;
330 void *key, *value;
15a07b33 331 u32 value_size;
592867bf 332 struct fd f;
db20fd2b
AS
333 int err;
334
335 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
336 return -EINVAL;
337
592867bf 338 f = fdget(ufd);
c2101297 339 map = __bpf_map_get(f);
db20fd2b
AS
340 if (IS_ERR(map))
341 return PTR_ERR(map);
342
343 err = -ENOMEM;
344 key = kmalloc(map->key_size, GFP_USER);
345 if (!key)
346 goto err_put;
347
348 err = -EFAULT;
349 if (copy_from_user(key, ukey, map->key_size) != 0)
350 goto free_key;
351
15a07b33
AS
352 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
353 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
354 value_size = round_up(map->value_size, 8) * num_possible_cpus();
355 else
356 value_size = map->value_size;
357
db20fd2b 358 err = -ENOMEM;
15a07b33 359 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
db20fd2b
AS
360 if (!value)
361 goto free_key;
362
363 err = -EFAULT;
15a07b33 364 if (copy_from_user(value, uvalue, value_size) != 0)
db20fd2b
AS
365 goto free_value;
366
b121d1e7
AS
367 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
368 * inside bpf map update or delete otherwise deadlocks are possible
369 */
370 preempt_disable();
371 __this_cpu_inc(bpf_prog_active);
15a07b33
AS
372 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH) {
373 err = bpf_percpu_hash_update(map, key, value, attr->flags);
374 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
375 err = bpf_percpu_array_update(map, key, value, attr->flags);
376 } else {
377 rcu_read_lock();
378 err = map->ops->map_update_elem(map, key, value, attr->flags);
379 rcu_read_unlock();
380 }
b121d1e7
AS
381 __this_cpu_dec(bpf_prog_active);
382 preempt_enable();
db20fd2b
AS
383
384free_value:
385 kfree(value);
386free_key:
387 kfree(key);
388err_put:
389 fdput(f);
390 return err;
391}
392
393#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
394
395static int map_delete_elem(union bpf_attr *attr)
396{
397 void __user *ukey = u64_to_ptr(attr->key);
398 int ufd = attr->map_fd;
db20fd2b 399 struct bpf_map *map;
592867bf 400 struct fd f;
db20fd2b
AS
401 void *key;
402 int err;
403
404 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
405 return -EINVAL;
406
592867bf 407 f = fdget(ufd);
c2101297 408 map = __bpf_map_get(f);
db20fd2b
AS
409 if (IS_ERR(map))
410 return PTR_ERR(map);
411
412 err = -ENOMEM;
413 key = kmalloc(map->key_size, GFP_USER);
414 if (!key)
415 goto err_put;
416
417 err = -EFAULT;
418 if (copy_from_user(key, ukey, map->key_size) != 0)
419 goto free_key;
420
b121d1e7
AS
421 preempt_disable();
422 __this_cpu_inc(bpf_prog_active);
db20fd2b
AS
423 rcu_read_lock();
424 err = map->ops->map_delete_elem(map, key);
425 rcu_read_unlock();
b121d1e7
AS
426 __this_cpu_dec(bpf_prog_active);
427 preempt_enable();
db20fd2b
AS
428
429free_key:
430 kfree(key);
431err_put:
432 fdput(f);
433 return err;
434}
435
436/* last field in 'union bpf_attr' used by this command */
437#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
438
439static int map_get_next_key(union bpf_attr *attr)
440{
441 void __user *ukey = u64_to_ptr(attr->key);
442 void __user *unext_key = u64_to_ptr(attr->next_key);
443 int ufd = attr->map_fd;
db20fd2b
AS
444 struct bpf_map *map;
445 void *key, *next_key;
592867bf 446 struct fd f;
db20fd2b
AS
447 int err;
448
449 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
450 return -EINVAL;
451
592867bf 452 f = fdget(ufd);
c2101297 453 map = __bpf_map_get(f);
db20fd2b
AS
454 if (IS_ERR(map))
455 return PTR_ERR(map);
456
457 err = -ENOMEM;
458 key = kmalloc(map->key_size, GFP_USER);
459 if (!key)
460 goto err_put;
461
462 err = -EFAULT;
463 if (copy_from_user(key, ukey, map->key_size) != 0)
464 goto free_key;
465
466 err = -ENOMEM;
467 next_key = kmalloc(map->key_size, GFP_USER);
468 if (!next_key)
469 goto free_key;
470
471 rcu_read_lock();
472 err = map->ops->map_get_next_key(map, key, next_key);
473 rcu_read_unlock();
474 if (err)
475 goto free_next_key;
476
477 err = -EFAULT;
478 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
479 goto free_next_key;
480
481 err = 0;
482
483free_next_key:
484 kfree(next_key);
485free_key:
486 kfree(key);
487err_put:
488 fdput(f);
489 return err;
490}
491
09756af4
AS
492static LIST_HEAD(bpf_prog_types);
493
494static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
495{
496 struct bpf_prog_type_list *tl;
497
498 list_for_each_entry(tl, &bpf_prog_types, list_node) {
499 if (tl->type == type) {
500 prog->aux->ops = tl->ops;
24701ece 501 prog->type = type;
09756af4
AS
502 return 0;
503 }
504 }
24701ece 505
09756af4
AS
506 return -EINVAL;
507}
508
509void bpf_register_prog_type(struct bpf_prog_type_list *tl)
510{
511 list_add(&tl->list_node, &bpf_prog_types);
512}
513
0a542a86
AS
514/* fixup insn->imm field of bpf_call instructions:
515 * if (insn->imm == BPF_FUNC_map_lookup_elem)
516 * insn->imm = bpf_map_lookup_elem - __bpf_call_base;
517 * else if (insn->imm == BPF_FUNC_map_update_elem)
518 * insn->imm = bpf_map_update_elem - __bpf_call_base;
519 * else ...
520 *
521 * this function is called after eBPF program passed verification
522 */
523static void fixup_bpf_calls(struct bpf_prog *prog)
524{
525 const struct bpf_func_proto *fn;
526 int i;
527
528 for (i = 0; i < prog->len; i++) {
529 struct bpf_insn *insn = &prog->insnsi[i];
530
531 if (insn->code == (BPF_JMP | BPF_CALL)) {
532 /* we reach here when program has bpf_call instructions
533 * and it passed bpf_check(), means that
534 * ops->get_func_proto must have been supplied, check it
535 */
536 BUG_ON(!prog->aux->ops->get_func_proto);
537
c46646d0
DB
538 if (insn->imm == BPF_FUNC_get_route_realm)
539 prog->dst_needed = 1;
3ad00405
DB
540 if (insn->imm == BPF_FUNC_get_prandom_u32)
541 bpf_user_rnd_init_once();
04fd61ab
AS
542 if (insn->imm == BPF_FUNC_tail_call) {
543 /* mark bpf_tail_call as different opcode
544 * to avoid conditional branch in
545 * interpeter for every normal call
546 * and to prevent accidental JITing by
547 * JIT compiler that doesn't support
548 * bpf_tail_call yet
549 */
550 insn->imm = 0;
551 insn->code |= BPF_X;
552 continue;
553 }
554
0a542a86
AS
555 fn = prog->aux->ops->get_func_proto(insn->imm);
556 /* all functions that have prototype and verifier allowed
557 * programs to call them, must be real in-kernel functions
558 */
559 BUG_ON(!fn->func);
560 insn->imm = fn->func - __bpf_call_base;
561 }
562 }
563}
564
09756af4
AS
565/* drop refcnt on maps used by eBPF program and free auxilary data */
566static void free_used_maps(struct bpf_prog_aux *aux)
567{
568 int i;
569
570 for (i = 0; i < aux->used_map_cnt; i++)
571 bpf_map_put(aux->used_maps[i]);
572
573 kfree(aux->used_maps);
574}
575
aaac3ba9
AS
576static int bpf_prog_charge_memlock(struct bpf_prog *prog)
577{
578 struct user_struct *user = get_current_user();
579 unsigned long memlock_limit;
580
581 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
582
583 atomic_long_add(prog->pages, &user->locked_vm);
584 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
585 atomic_long_sub(prog->pages, &user->locked_vm);
586 free_uid(user);
587 return -EPERM;
588 }
589 prog->aux->user = user;
590 return 0;
591}
592
593static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
594{
595 struct user_struct *user = prog->aux->user;
596
597 atomic_long_sub(prog->pages, &user->locked_vm);
598 free_uid(user);
599}
600
e9d8afa9 601static void __prog_put_common(struct rcu_head *rcu)
abf2e7d6
AS
602{
603 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
604
605 free_used_maps(aux);
aaac3ba9 606 bpf_prog_uncharge_memlock(aux->prog);
abf2e7d6
AS
607 bpf_prog_free(aux->prog);
608}
609
610/* version of bpf_prog_put() that is called after a grace period */
611void bpf_prog_put_rcu(struct bpf_prog *prog)
612{
e9d8afa9
DB
613 if (atomic_dec_and_test(&prog->aux->refcnt))
614 call_rcu(&prog->aux->rcu, __prog_put_common);
abf2e7d6
AS
615}
616
09756af4
AS
617void bpf_prog_put(struct bpf_prog *prog)
618{
e9d8afa9
DB
619 if (atomic_dec_and_test(&prog->aux->refcnt))
620 __prog_put_common(&prog->aux->rcu);
09756af4 621}
e2e9b654 622EXPORT_SYMBOL_GPL(bpf_prog_put);
09756af4
AS
623
624static int bpf_prog_release(struct inode *inode, struct file *filp)
625{
626 struct bpf_prog *prog = filp->private_data;
627
abf2e7d6 628 bpf_prog_put_rcu(prog);
09756af4
AS
629 return 0;
630}
631
632static const struct file_operations bpf_prog_fops = {
633 .release = bpf_prog_release,
634};
635
b2197755 636int bpf_prog_new_fd(struct bpf_prog *prog)
aa79781b
DB
637{
638 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
639 O_RDWR | O_CLOEXEC);
640}
641
c2101297 642static struct bpf_prog *__bpf_prog_get(struct fd f)
09756af4 643{
09756af4
AS
644 if (!f.file)
645 return ERR_PTR(-EBADF);
09756af4
AS
646 if (f.file->f_op != &bpf_prog_fops) {
647 fdput(f);
648 return ERR_PTR(-EINVAL);
649 }
650
c2101297 651 return f.file->private_data;
09756af4
AS
652}
653
654/* called by sockets/tracing/seccomp before attaching program to an event
655 * pairs with bpf_prog_put()
656 */
657struct bpf_prog *bpf_prog_get(u32 ufd)
658{
659 struct fd f = fdget(ufd);
660 struct bpf_prog *prog;
661
c2101297 662 prog = __bpf_prog_get(f);
09756af4
AS
663 if (IS_ERR(prog))
664 return prog;
665
666 atomic_inc(&prog->aux->refcnt);
667 fdput(f);
c2101297 668
09756af4
AS
669 return prog;
670}
e2e9b654 671EXPORT_SYMBOL_GPL(bpf_prog_get);
09756af4
AS
672
673/* last field in 'union bpf_attr' used by this command */
2541517c 674#define BPF_PROG_LOAD_LAST_FIELD kern_version
09756af4
AS
675
676static int bpf_prog_load(union bpf_attr *attr)
677{
678 enum bpf_prog_type type = attr->prog_type;
679 struct bpf_prog *prog;
680 int err;
681 char license[128];
682 bool is_gpl;
683
684 if (CHECK_ATTR(BPF_PROG_LOAD))
685 return -EINVAL;
686
687 /* copy eBPF program license from user space */
688 if (strncpy_from_user(license, u64_to_ptr(attr->license),
689 sizeof(license) - 1) < 0)
690 return -EFAULT;
691 license[sizeof(license) - 1] = 0;
692
693 /* eBPF programs must be GPL compatible to use GPL-ed functions */
694 is_gpl = license_is_gpl_compatible(license);
695
696 if (attr->insn_cnt >= BPF_MAXINSNS)
697 return -EINVAL;
698
2541517c
AS
699 if (type == BPF_PROG_TYPE_KPROBE &&
700 attr->kern_version != LINUX_VERSION_CODE)
701 return -EINVAL;
702
1be7f75d
AS
703 if (type != BPF_PROG_TYPE_SOCKET_FILTER && !capable(CAP_SYS_ADMIN))
704 return -EPERM;
705
09756af4
AS
706 /* plain bpf_prog allocation */
707 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
708 if (!prog)
709 return -ENOMEM;
710
aaac3ba9
AS
711 err = bpf_prog_charge_memlock(prog);
712 if (err)
713 goto free_prog_nouncharge;
714
09756af4
AS
715 prog->len = attr->insn_cnt;
716
717 err = -EFAULT;
718 if (copy_from_user(prog->insns, u64_to_ptr(attr->insns),
719 prog->len * sizeof(struct bpf_insn)) != 0)
720 goto free_prog;
721
722 prog->orig_prog = NULL;
a91263d5 723 prog->jited = 0;
09756af4
AS
724
725 atomic_set(&prog->aux->refcnt, 1);
a91263d5 726 prog->gpl_compatible = is_gpl ? 1 : 0;
09756af4
AS
727
728 /* find program type: socket_filter vs tracing_filter */
729 err = find_prog_type(type, prog);
730 if (err < 0)
731 goto free_prog;
732
733 /* run eBPF verifier */
9bac3d6d 734 err = bpf_check(&prog, attr);
09756af4
AS
735 if (err < 0)
736 goto free_used_maps;
737
0a542a86
AS
738 /* fixup BPF_CALL->imm field */
739 fixup_bpf_calls(prog);
740
09756af4 741 /* eBPF program is ready to be JITed */
04fd61ab
AS
742 err = bpf_prog_select_runtime(prog);
743 if (err < 0)
744 goto free_used_maps;
09756af4 745
aa79781b 746 err = bpf_prog_new_fd(prog);
09756af4
AS
747 if (err < 0)
748 /* failed to allocate fd */
749 goto free_used_maps;
750
751 return err;
752
753free_used_maps:
754 free_used_maps(prog->aux);
755free_prog:
aaac3ba9
AS
756 bpf_prog_uncharge_memlock(prog);
757free_prog_nouncharge:
09756af4
AS
758 bpf_prog_free(prog);
759 return err;
760}
761
b2197755
DB
762#define BPF_OBJ_LAST_FIELD bpf_fd
763
764static int bpf_obj_pin(const union bpf_attr *attr)
765{
766 if (CHECK_ATTR(BPF_OBJ))
767 return -EINVAL;
768
769 return bpf_obj_pin_user(attr->bpf_fd, u64_to_ptr(attr->pathname));
770}
771
772static int bpf_obj_get(const union bpf_attr *attr)
773{
774 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
775 return -EINVAL;
776
777 return bpf_obj_get_user(u64_to_ptr(attr->pathname));
778}
779
99c55f7d
AS
780SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
781{
782 union bpf_attr attr = {};
783 int err;
784
1be7f75d 785 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
99c55f7d
AS
786 return -EPERM;
787
788 if (!access_ok(VERIFY_READ, uattr, 1))
789 return -EFAULT;
790
791 if (size > PAGE_SIZE) /* silly large */
792 return -E2BIG;
793
794 /* If we're handed a bigger struct than we know of,
795 * ensure all the unknown bits are 0 - i.e. new
796 * user-space does not rely on any kernel feature
797 * extensions we dont know about yet.
798 */
799 if (size > sizeof(attr)) {
800 unsigned char __user *addr;
801 unsigned char __user *end;
802 unsigned char val;
803
804 addr = (void __user *)uattr + sizeof(attr);
805 end = (void __user *)uattr + size;
806
807 for (; addr < end; addr++) {
808 err = get_user(val, addr);
809 if (err)
810 return err;
811 if (val)
812 return -E2BIG;
813 }
814 size = sizeof(attr);
815 }
816
817 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
818 if (copy_from_user(&attr, uattr, size) != 0)
819 return -EFAULT;
820
821 switch (cmd) {
822 case BPF_MAP_CREATE:
823 err = map_create(&attr);
824 break;
db20fd2b
AS
825 case BPF_MAP_LOOKUP_ELEM:
826 err = map_lookup_elem(&attr);
827 break;
828 case BPF_MAP_UPDATE_ELEM:
829 err = map_update_elem(&attr);
830 break;
831 case BPF_MAP_DELETE_ELEM:
832 err = map_delete_elem(&attr);
833 break;
834 case BPF_MAP_GET_NEXT_KEY:
835 err = map_get_next_key(&attr);
836 break;
09756af4
AS
837 case BPF_PROG_LOAD:
838 err = bpf_prog_load(&attr);
839 break;
b2197755
DB
840 case BPF_OBJ_PIN:
841 err = bpf_obj_pin(&attr);
842 break;
843 case BPF_OBJ_GET:
844 err = bpf_obj_get(&attr);
845 break;
99c55f7d
AS
846 default:
847 err = -EINVAL;
848 break;
849 }
850
851 return err;
852}
This page took 0.123443 seconds and 5 git commands to generate.