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