Merge branch 'for-linus' of git://git.kernel.dk/linux-block
[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>
a67edbf4 13#include <linux/bpf_trace.h>
99c55f7d
AS
14#include <linux/syscalls.h>
15#include <linux/slab.h>
3f07c014 16#include <linux/sched/signal.h>
d407bd25
DB
17#include <linux/vmalloc.h>
18#include <linux/mmzone.h>
99c55f7d 19#include <linux/anon_inodes.h>
db20fd2b 20#include <linux/file.h>
09756af4
AS
21#include <linux/license.h>
22#include <linux/filter.h>
2541517c 23#include <linux/version.h>
535e7b4b 24#include <linux/kernel.h>
dc4bb0e2 25#include <linux/idr.h>
99c55f7d 26
14dc6f04
MKL
27#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
28 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
29 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
31#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
32#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
33
b121d1e7 34DEFINE_PER_CPU(int, bpf_prog_active);
dc4bb0e2
MKL
35static DEFINE_IDR(prog_idr);
36static DEFINE_SPINLOCK(prog_idr_lock);
f3f1c054
MKL
37static DEFINE_IDR(map_idr);
38static DEFINE_SPINLOCK(map_idr_lock);
b121d1e7 39
1be7f75d
AS
40int sysctl_unprivileged_bpf_disabled __read_mostly;
41
40077e0c
JB
42static const struct bpf_map_ops * const bpf_map_types[] = {
43#define BPF_PROG_TYPE(_id, _ops)
44#define BPF_MAP_TYPE(_id, _ops) \
45 [_id] = &_ops,
46#include <linux/bpf_types.h>
47#undef BPF_PROG_TYPE
48#undef BPF_MAP_TYPE
49};
99c55f7d 50
752ba56f
MS
51/*
52 * If we're handed a bigger struct than we know of, ensure all the unknown bits
53 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
54 * we don't know about yet.
55 *
56 * There is a ToCToU between this function call and the following
57 * copy_from_user() call. However, this is not a concern since this function is
58 * meant to be a future-proofing of bits.
59 */
58291a74
MS
60static int check_uarg_tail_zero(void __user *uaddr,
61 size_t expected_size,
62 size_t actual_size)
63{
64 unsigned char __user *addr;
65 unsigned char __user *end;
66 unsigned char val;
67 int err;
68
752ba56f
MS
69 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
70 return -E2BIG;
71
72 if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
73 return -EFAULT;
74
58291a74
MS
75 if (actual_size <= expected_size)
76 return 0;
77
78 addr = uaddr + expected_size;
79 end = uaddr + actual_size;
80
81 for (; addr < end; addr++) {
82 err = get_user(val, addr);
83 if (err)
84 return err;
85 if (val)
86 return -E2BIG;
87 }
88
89 return 0;
90}
91
99c55f7d
AS
92static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
93{
99c55f7d
AS
94 struct bpf_map *map;
95
40077e0c
JB
96 if (attr->map_type >= ARRAY_SIZE(bpf_map_types) ||
97 !bpf_map_types[attr->map_type])
98 return ERR_PTR(-EINVAL);
99c55f7d 99
40077e0c
JB
100 map = bpf_map_types[attr->map_type]->map_alloc(attr);
101 if (IS_ERR(map))
102 return map;
103 map->ops = bpf_map_types[attr->map_type];
104 map->map_type = attr->map_type;
105 return map;
99c55f7d
AS
106}
107
96eabe7a 108void *bpf_map_area_alloc(size_t size, int numa_node)
d407bd25
DB
109{
110 /* We definitely need __GFP_NORETRY, so OOM killer doesn't
111 * trigger under memory pressure as we really just want to
112 * fail instead.
113 */
114 const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
115 void *area;
116
117 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
96eabe7a 118 area = kmalloc_node(size, GFP_USER | flags, numa_node);
d407bd25
DB
119 if (area != NULL)
120 return area;
121 }
122
96eabe7a
MKL
123 return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
124 __builtin_return_address(0));
d407bd25
DB
125}
126
127void bpf_map_area_free(void *area)
128{
129 kvfree(area);
130}
131
6c905981
AS
132int bpf_map_precharge_memlock(u32 pages)
133{
134 struct user_struct *user = get_current_user();
135 unsigned long memlock_limit, cur;
136
137 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
138 cur = atomic_long_read(&user->locked_vm);
139 free_uid(user);
140 if (cur + pages > memlock_limit)
141 return -EPERM;
142 return 0;
143}
144
aaac3ba9
AS
145static int bpf_map_charge_memlock(struct bpf_map *map)
146{
147 struct user_struct *user = get_current_user();
148 unsigned long memlock_limit;
149
150 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
151
152 atomic_long_add(map->pages, &user->locked_vm);
153
154 if (atomic_long_read(&user->locked_vm) > memlock_limit) {
155 atomic_long_sub(map->pages, &user->locked_vm);
156 free_uid(user);
157 return -EPERM;
158 }
159 map->user = user;
160 return 0;
161}
162
163static void bpf_map_uncharge_memlock(struct bpf_map *map)
164{
165 struct user_struct *user = map->user;
166
167 atomic_long_sub(map->pages, &user->locked_vm);
168 free_uid(user);
169}
170
f3f1c054
MKL
171static int bpf_map_alloc_id(struct bpf_map *map)
172{
173 int id;
174
175 spin_lock_bh(&map_idr_lock);
176 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
177 if (id > 0)
178 map->id = id;
179 spin_unlock_bh(&map_idr_lock);
180
181 if (WARN_ON_ONCE(!id))
182 return -ENOSPC;
183
184 return id > 0 ? 0 : id;
185}
186
bd5f5f4e 187static void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
f3f1c054 188{
930651a7
ED
189 unsigned long flags;
190
bd5f5f4e 191 if (do_idr_lock)
930651a7 192 spin_lock_irqsave(&map_idr_lock, flags);
bd5f5f4e
MKL
193 else
194 __acquire(&map_idr_lock);
195
f3f1c054 196 idr_remove(&map_idr, map->id);
bd5f5f4e
MKL
197
198 if (do_idr_lock)
930651a7 199 spin_unlock_irqrestore(&map_idr_lock, flags);
bd5f5f4e
MKL
200 else
201 __release(&map_idr_lock);
f3f1c054
MKL
202}
203
99c55f7d
AS
204/* called from workqueue */
205static void bpf_map_free_deferred(struct work_struct *work)
206{
207 struct bpf_map *map = container_of(work, struct bpf_map, work);
208
aaac3ba9 209 bpf_map_uncharge_memlock(map);
99c55f7d
AS
210 /* implementation dependent freeing */
211 map->ops->map_free(map);
212}
213
c9da161c
DB
214static void bpf_map_put_uref(struct bpf_map *map)
215{
216 if (atomic_dec_and_test(&map->usercnt)) {
217 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
218 bpf_fd_array_map_clear(map);
219 }
220}
221
99c55f7d
AS
222/* decrement map refcnt and schedule it for freeing via workqueue
223 * (unrelying map implementation ops->map_free() might sleep)
224 */
bd5f5f4e 225static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
99c55f7d
AS
226{
227 if (atomic_dec_and_test(&map->refcnt)) {
34ad5580 228 /* bpf_map_free_id() must be called first */
bd5f5f4e 229 bpf_map_free_id(map, do_idr_lock);
99c55f7d
AS
230 INIT_WORK(&map->work, bpf_map_free_deferred);
231 schedule_work(&map->work);
232 }
233}
234
bd5f5f4e
MKL
235void bpf_map_put(struct bpf_map *map)
236{
237 __bpf_map_put(map, true);
238}
239
c9da161c 240void bpf_map_put_with_uref(struct bpf_map *map)
99c55f7d 241{
c9da161c 242 bpf_map_put_uref(map);
99c55f7d 243 bpf_map_put(map);
c9da161c
DB
244}
245
246static int bpf_map_release(struct inode *inode, struct file *filp)
247{
61d1b6a4
DB
248 struct bpf_map *map = filp->private_data;
249
250 if (map->ops->map_release)
251 map->ops->map_release(map, filp);
252
253 bpf_map_put_with_uref(map);
99c55f7d
AS
254 return 0;
255}
256
f99bf205
DB
257#ifdef CONFIG_PROC_FS
258static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
259{
260 const struct bpf_map *map = filp->private_data;
21116b70
DB
261 const struct bpf_array *array;
262 u32 owner_prog_type = 0;
9780c0ab 263 u32 owner_jited = 0;
21116b70
DB
264
265 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
266 array = container_of(map, struct bpf_array, map);
267 owner_prog_type = array->owner_prog_type;
9780c0ab 268 owner_jited = array->owner_jited;
21116b70 269 }
f99bf205
DB
270
271 seq_printf(m,
272 "map_type:\t%u\n"
273 "key_size:\t%u\n"
274 "value_size:\t%u\n"
322cea2f 275 "max_entries:\t%u\n"
21116b70
DB
276 "map_flags:\t%#x\n"
277 "memlock:\t%llu\n",
f99bf205
DB
278 map->map_type,
279 map->key_size,
280 map->value_size,
322cea2f 281 map->max_entries,
21116b70
DB
282 map->map_flags,
283 map->pages * 1ULL << PAGE_SHIFT);
284
9780c0ab 285 if (owner_prog_type) {
21116b70
DB
286 seq_printf(m, "owner_prog_type:\t%u\n",
287 owner_prog_type);
9780c0ab
DB
288 seq_printf(m, "owner_jited:\t%u\n",
289 owner_jited);
290 }
f99bf205
DB
291}
292#endif
293
99c55f7d 294static const struct file_operations bpf_map_fops = {
f99bf205
DB
295#ifdef CONFIG_PROC_FS
296 .show_fdinfo = bpf_map_show_fdinfo,
297#endif
298 .release = bpf_map_release,
99c55f7d
AS
299};
300
b2197755 301int bpf_map_new_fd(struct bpf_map *map)
aa79781b
DB
302{
303 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
304 O_RDWR | O_CLOEXEC);
305}
306
99c55f7d
AS
307/* helper macro to check that unused fields 'union bpf_attr' are zero */
308#define CHECK_ATTR(CMD) \
309 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
310 sizeof(attr->CMD##_LAST_FIELD), 0, \
311 sizeof(*attr) - \
312 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
313 sizeof(attr->CMD##_LAST_FIELD)) != NULL
314
96eabe7a 315#define BPF_MAP_CREATE_LAST_FIELD numa_node
99c55f7d
AS
316/* called via syscall */
317static int map_create(union bpf_attr *attr)
318{
96eabe7a 319 int numa_node = bpf_map_attr_numa_node(attr);
99c55f7d
AS
320 struct bpf_map *map;
321 int err;
322
323 err = CHECK_ATTR(BPF_MAP_CREATE);
324 if (err)
325 return -EINVAL;
326
96eabe7a 327 if (numa_node != NUMA_NO_NODE &&
96e5ae4e
ED
328 ((unsigned int)numa_node >= nr_node_ids ||
329 !node_online(numa_node)))
96eabe7a
MKL
330 return -EINVAL;
331
99c55f7d
AS
332 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
333 map = find_and_alloc_map(attr);
334 if (IS_ERR(map))
335 return PTR_ERR(map);
336
337 atomic_set(&map->refcnt, 1);
c9da161c 338 atomic_set(&map->usercnt, 1);
99c55f7d 339
aaac3ba9
AS
340 err = bpf_map_charge_memlock(map);
341 if (err)
20b2b24f 342 goto free_map_nouncharge;
aaac3ba9 343
f3f1c054
MKL
344 err = bpf_map_alloc_id(map);
345 if (err)
346 goto free_map;
347
aa79781b 348 err = bpf_map_new_fd(map);
bd5f5f4e
MKL
349 if (err < 0) {
350 /* failed to allocate fd.
351 * bpf_map_put() is needed because the above
352 * bpf_map_alloc_id() has published the map
353 * to the userspace and the userspace may
354 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
355 */
356 bpf_map_put(map);
357 return err;
358 }
99c55f7d 359
a67edbf4 360 trace_bpf_map_create(map, err);
99c55f7d
AS
361 return err;
362
363free_map:
20b2b24f
DB
364 bpf_map_uncharge_memlock(map);
365free_map_nouncharge:
99c55f7d
AS
366 map->ops->map_free(map);
367 return err;
368}
369
db20fd2b
AS
370/* if error is returned, fd is released.
371 * On success caller should complete fd access with matching fdput()
372 */
c2101297 373struct bpf_map *__bpf_map_get(struct fd f)
db20fd2b 374{
db20fd2b
AS
375 if (!f.file)
376 return ERR_PTR(-EBADF);
db20fd2b
AS
377 if (f.file->f_op != &bpf_map_fops) {
378 fdput(f);
379 return ERR_PTR(-EINVAL);
380 }
381
c2101297
DB
382 return f.file->private_data;
383}
384
92117d84
AS
385/* prog's and map's refcnt limit */
386#define BPF_MAX_REFCNT 32768
387
388struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
c9da161c 389{
92117d84
AS
390 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
391 atomic_dec(&map->refcnt);
392 return ERR_PTR(-EBUSY);
393 }
c9da161c
DB
394 if (uref)
395 atomic_inc(&map->usercnt);
92117d84 396 return map;
c9da161c
DB
397}
398
399struct bpf_map *bpf_map_get_with_uref(u32 ufd)
c2101297
DB
400{
401 struct fd f = fdget(ufd);
402 struct bpf_map *map;
403
404 map = __bpf_map_get(f);
405 if (IS_ERR(map))
406 return map;
407
92117d84 408 map = bpf_map_inc(map, true);
c2101297 409 fdput(f);
db20fd2b
AS
410
411 return map;
412}
413
bd5f5f4e
MKL
414/* map_idr_lock should have been held */
415static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
416 bool uref)
417{
418 int refold;
419
420 refold = __atomic_add_unless(&map->refcnt, 1, 0);
421
422 if (refold >= BPF_MAX_REFCNT) {
423 __bpf_map_put(map, false);
424 return ERR_PTR(-EBUSY);
425 }
426
427 if (!refold)
428 return ERR_PTR(-ENOENT);
429
430 if (uref)
431 atomic_inc(&map->usercnt);
432
433 return map;
434}
435
b8cdc051
AS
436int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
437{
438 return -ENOTSUPP;
439}
440
db20fd2b
AS
441/* last field in 'union bpf_attr' used by this command */
442#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
443
444static int map_lookup_elem(union bpf_attr *attr)
445{
535e7b4b
MS
446 void __user *ukey = u64_to_user_ptr(attr->key);
447 void __user *uvalue = u64_to_user_ptr(attr->value);
db20fd2b 448 int ufd = attr->map_fd;
db20fd2b 449 struct bpf_map *map;
8ebe667c 450 void *key, *value, *ptr;
15a07b33 451 u32 value_size;
592867bf 452 struct fd f;
db20fd2b
AS
453 int err;
454
455 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
456 return -EINVAL;
457
592867bf 458 f = fdget(ufd);
c2101297 459 map = __bpf_map_get(f);
db20fd2b
AS
460 if (IS_ERR(map))
461 return PTR_ERR(map);
462
e4448ed8
AV
463 key = memdup_user(ukey, map->key_size);
464 if (IS_ERR(key)) {
465 err = PTR_ERR(key);
db20fd2b 466 goto err_put;
e4448ed8 467 }
db20fd2b 468
15a07b33 469 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
8f844938 470 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
15a07b33
AS
471 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
472 value_size = round_up(map->value_size, 8) * num_possible_cpus();
14dc6f04
MKL
473 else if (IS_FD_MAP(map))
474 value_size = sizeof(u32);
15a07b33
AS
475 else
476 value_size = map->value_size;
477
8ebe667c 478 err = -ENOMEM;
15a07b33 479 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
db20fd2b 480 if (!value)
8ebe667c
AS
481 goto free_key;
482
8f844938
MKL
483 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
484 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
15a07b33
AS
485 err = bpf_percpu_hash_copy(map, key, value);
486 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
487 err = bpf_percpu_array_copy(map, key, value);
557c0c6e
AS
488 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
489 err = bpf_stackmap_copy(map, key, value);
14dc6f04
MKL
490 } else if (IS_FD_ARRAY(map)) {
491 err = bpf_fd_array_map_lookup_elem(map, key, value);
492 } else if (IS_FD_HASH(map)) {
493 err = bpf_fd_htab_map_lookup_elem(map, key, value);
15a07b33
AS
494 } else {
495 rcu_read_lock();
496 ptr = map->ops->map_lookup_elem(map, key);
497 if (ptr)
498 memcpy(value, ptr, value_size);
499 rcu_read_unlock();
500 err = ptr ? 0 : -ENOENT;
501 }
8ebe667c 502
15a07b33 503 if (err)
8ebe667c 504 goto free_value;
db20fd2b
AS
505
506 err = -EFAULT;
15a07b33 507 if (copy_to_user(uvalue, value, value_size) != 0)
8ebe667c 508 goto free_value;
db20fd2b 509
a67edbf4 510 trace_bpf_map_lookup_elem(map, ufd, key, value);
db20fd2b
AS
511 err = 0;
512
8ebe667c
AS
513free_value:
514 kfree(value);
db20fd2b
AS
515free_key:
516 kfree(key);
517err_put:
518 fdput(f);
519 return err;
520}
521
3274f520 522#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
db20fd2b
AS
523
524static int map_update_elem(union bpf_attr *attr)
525{
535e7b4b
MS
526 void __user *ukey = u64_to_user_ptr(attr->key);
527 void __user *uvalue = u64_to_user_ptr(attr->value);
db20fd2b 528 int ufd = attr->map_fd;
db20fd2b
AS
529 struct bpf_map *map;
530 void *key, *value;
15a07b33 531 u32 value_size;
592867bf 532 struct fd f;
db20fd2b
AS
533 int err;
534
535 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
536 return -EINVAL;
537
592867bf 538 f = fdget(ufd);
c2101297 539 map = __bpf_map_get(f);
db20fd2b
AS
540 if (IS_ERR(map))
541 return PTR_ERR(map);
542
e4448ed8
AV
543 key = memdup_user(ukey, map->key_size);
544 if (IS_ERR(key)) {
545 err = PTR_ERR(key);
db20fd2b 546 goto err_put;
e4448ed8 547 }
db20fd2b 548
15a07b33 549 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
8f844938 550 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
15a07b33
AS
551 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
552 value_size = round_up(map->value_size, 8) * num_possible_cpus();
553 else
554 value_size = map->value_size;
555
db20fd2b 556 err = -ENOMEM;
15a07b33 557 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
db20fd2b
AS
558 if (!value)
559 goto free_key;
560
561 err = -EFAULT;
15a07b33 562 if (copy_from_user(value, uvalue, value_size) != 0)
db20fd2b
AS
563 goto free_value;
564
b121d1e7
AS
565 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
566 * inside bpf map update or delete otherwise deadlocks are possible
567 */
568 preempt_disable();
569 __this_cpu_inc(bpf_prog_active);
8f844938
MKL
570 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
571 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
15a07b33
AS
572 err = bpf_percpu_hash_update(map, key, value, attr->flags);
573 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
574 err = bpf_percpu_array_update(map, key, value, attr->flags);
d056a788 575 } else if (map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY ||
4ed8ec52 576 map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
56f668df
MKL
577 map->map_type == BPF_MAP_TYPE_CGROUP_ARRAY ||
578 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS) {
d056a788
DB
579 rcu_read_lock();
580 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
581 attr->flags);
582 rcu_read_unlock();
bcc6b1b7
MKL
583 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
584 rcu_read_lock();
585 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
586 attr->flags);
587 rcu_read_unlock();
15a07b33
AS
588 } else {
589 rcu_read_lock();
590 err = map->ops->map_update_elem(map, key, value, attr->flags);
591 rcu_read_unlock();
592 }
b121d1e7
AS
593 __this_cpu_dec(bpf_prog_active);
594 preempt_enable();
db20fd2b 595
a67edbf4
DB
596 if (!err)
597 trace_bpf_map_update_elem(map, ufd, key, value);
db20fd2b
AS
598free_value:
599 kfree(value);
600free_key:
601 kfree(key);
602err_put:
603 fdput(f);
604 return err;
605}
606
607#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
608
609static int map_delete_elem(union bpf_attr *attr)
610{
535e7b4b 611 void __user *ukey = u64_to_user_ptr(attr->key);
db20fd2b 612 int ufd = attr->map_fd;
db20fd2b 613 struct bpf_map *map;
592867bf 614 struct fd f;
db20fd2b
AS
615 void *key;
616 int err;
617
618 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
619 return -EINVAL;
620
592867bf 621 f = fdget(ufd);
c2101297 622 map = __bpf_map_get(f);
db20fd2b
AS
623 if (IS_ERR(map))
624 return PTR_ERR(map);
625
e4448ed8
AV
626 key = memdup_user(ukey, map->key_size);
627 if (IS_ERR(key)) {
628 err = PTR_ERR(key);
db20fd2b 629 goto err_put;
e4448ed8 630 }
db20fd2b 631
b121d1e7
AS
632 preempt_disable();
633 __this_cpu_inc(bpf_prog_active);
db20fd2b
AS
634 rcu_read_lock();
635 err = map->ops->map_delete_elem(map, key);
636 rcu_read_unlock();
b121d1e7
AS
637 __this_cpu_dec(bpf_prog_active);
638 preempt_enable();
db20fd2b 639
a67edbf4
DB
640 if (!err)
641 trace_bpf_map_delete_elem(map, ufd, key);
db20fd2b
AS
642 kfree(key);
643err_put:
644 fdput(f);
645 return err;
646}
647
648/* last field in 'union bpf_attr' used by this command */
649#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
650
651static int map_get_next_key(union bpf_attr *attr)
652{
535e7b4b
MS
653 void __user *ukey = u64_to_user_ptr(attr->key);
654 void __user *unext_key = u64_to_user_ptr(attr->next_key);
db20fd2b 655 int ufd = attr->map_fd;
db20fd2b
AS
656 struct bpf_map *map;
657 void *key, *next_key;
592867bf 658 struct fd f;
db20fd2b
AS
659 int err;
660
661 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
662 return -EINVAL;
663
592867bf 664 f = fdget(ufd);
c2101297 665 map = __bpf_map_get(f);
db20fd2b
AS
666 if (IS_ERR(map))
667 return PTR_ERR(map);
668
8fe45924 669 if (ukey) {
e4448ed8
AV
670 key = memdup_user(ukey, map->key_size);
671 if (IS_ERR(key)) {
672 err = PTR_ERR(key);
8fe45924 673 goto err_put;
e4448ed8 674 }
8fe45924
TQ
675 } else {
676 key = NULL;
677 }
db20fd2b
AS
678
679 err = -ENOMEM;
680 next_key = kmalloc(map->key_size, GFP_USER);
681 if (!next_key)
682 goto free_key;
683
684 rcu_read_lock();
685 err = map->ops->map_get_next_key(map, key, next_key);
686 rcu_read_unlock();
687 if (err)
688 goto free_next_key;
689
690 err = -EFAULT;
691 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
692 goto free_next_key;
693
a67edbf4 694 trace_bpf_map_next_key(map, ufd, key, next_key);
db20fd2b
AS
695 err = 0;
696
697free_next_key:
698 kfree(next_key);
699free_key:
700 kfree(key);
701err_put:
702 fdput(f);
703 return err;
704}
705
be9370a7
JB
706static const struct bpf_verifier_ops * const bpf_prog_types[] = {
707#define BPF_PROG_TYPE(_id, _ops) \
708 [_id] = &_ops,
40077e0c 709#define BPF_MAP_TYPE(_id, _ops)
be9370a7
JB
710#include <linux/bpf_types.h>
711#undef BPF_PROG_TYPE
40077e0c 712#undef BPF_MAP_TYPE
be9370a7 713};
09756af4
AS
714
715static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
716{
be9370a7
JB
717 if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
718 return -EINVAL;
09756af4 719
be9370a7
JB
720 prog->aux->ops = bpf_prog_types[type];
721 prog->type = type;
722 return 0;
09756af4
AS
723}
724
725/* drop refcnt on maps used by eBPF program and free auxilary data */
726static void free_used_maps(struct bpf_prog_aux *aux)
727{
728 int i;
729
730 for (i = 0; i < aux->used_map_cnt; i++)
731 bpf_map_put(aux->used_maps[i]);
732
733 kfree(aux->used_maps);
734}
735
5ccb071e
DB
736int __bpf_prog_charge(struct user_struct *user, u32 pages)
737{
738 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
739 unsigned long user_bufs;
740
741 if (user) {
742 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
743 if (user_bufs > memlock_limit) {
744 atomic_long_sub(pages, &user->locked_vm);
745 return -EPERM;
746 }
747 }
748
749 return 0;
750}
751
752void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
753{
754 if (user)
755 atomic_long_sub(pages, &user->locked_vm);
756}
757
aaac3ba9
AS
758static int bpf_prog_charge_memlock(struct bpf_prog *prog)
759{
760 struct user_struct *user = get_current_user();
5ccb071e 761 int ret;
aaac3ba9 762
5ccb071e
DB
763 ret = __bpf_prog_charge(user, prog->pages);
764 if (ret) {
aaac3ba9 765 free_uid(user);
5ccb071e 766 return ret;
aaac3ba9 767 }
5ccb071e 768
aaac3ba9
AS
769 prog->aux->user = user;
770 return 0;
771}
772
773static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
774{
775 struct user_struct *user = prog->aux->user;
776
5ccb071e 777 __bpf_prog_uncharge(user, prog->pages);
aaac3ba9
AS
778 free_uid(user);
779}
780
dc4bb0e2
MKL
781static int bpf_prog_alloc_id(struct bpf_prog *prog)
782{
783 int id;
784
785 spin_lock_bh(&prog_idr_lock);
786 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
787 if (id > 0)
788 prog->aux->id = id;
789 spin_unlock_bh(&prog_idr_lock);
790
791 /* id is in [1, INT_MAX) */
792 if (WARN_ON_ONCE(!id))
793 return -ENOSPC;
794
795 return id > 0 ? 0 : id;
796}
797
b16d9aa4 798static void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
dc4bb0e2
MKL
799{
800 /* cBPF to eBPF migrations are currently not in the idr store. */
801 if (!prog->aux->id)
802 return;
803
b16d9aa4
MKL
804 if (do_idr_lock)
805 spin_lock_bh(&prog_idr_lock);
806 else
807 __acquire(&prog_idr_lock);
808
dc4bb0e2 809 idr_remove(&prog_idr, prog->aux->id);
b16d9aa4
MKL
810
811 if (do_idr_lock)
812 spin_unlock_bh(&prog_idr_lock);
813 else
814 __release(&prog_idr_lock);
dc4bb0e2
MKL
815}
816
1aacde3d 817static void __bpf_prog_put_rcu(struct rcu_head *rcu)
abf2e7d6
AS
818{
819 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
820
821 free_used_maps(aux);
aaac3ba9 822 bpf_prog_uncharge_memlock(aux->prog);
abf2e7d6
AS
823 bpf_prog_free(aux->prog);
824}
825
b16d9aa4 826static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
09756af4 827{
a67edbf4
DB
828 if (atomic_dec_and_test(&prog->aux->refcnt)) {
829 trace_bpf_prog_put_rcu(prog);
34ad5580 830 /* bpf_prog_free_id() must be called first */
b16d9aa4 831 bpf_prog_free_id(prog, do_idr_lock);
74451e66 832 bpf_prog_kallsyms_del(prog);
1aacde3d 833 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
a67edbf4 834 }
09756af4 835}
b16d9aa4
MKL
836
837void bpf_prog_put(struct bpf_prog *prog)
838{
839 __bpf_prog_put(prog, true);
840}
e2e9b654 841EXPORT_SYMBOL_GPL(bpf_prog_put);
09756af4
AS
842
843static int bpf_prog_release(struct inode *inode, struct file *filp)
844{
845 struct bpf_prog *prog = filp->private_data;
846
1aacde3d 847 bpf_prog_put(prog);
09756af4
AS
848 return 0;
849}
850
7bd509e3
DB
851#ifdef CONFIG_PROC_FS
852static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
853{
854 const struct bpf_prog *prog = filp->private_data;
f1f7714e 855 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
7bd509e3 856
f1f7714e 857 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
7bd509e3
DB
858 seq_printf(m,
859 "prog_type:\t%u\n"
860 "prog_jited:\t%u\n"
f1f7714e 861 "prog_tag:\t%s\n"
7bd509e3
DB
862 "memlock:\t%llu\n",
863 prog->type,
864 prog->jited,
f1f7714e 865 prog_tag,
7bd509e3
DB
866 prog->pages * 1ULL << PAGE_SHIFT);
867}
868#endif
869
09756af4 870static const struct file_operations bpf_prog_fops = {
7bd509e3
DB
871#ifdef CONFIG_PROC_FS
872 .show_fdinfo = bpf_prog_show_fdinfo,
873#endif
874 .release = bpf_prog_release,
09756af4
AS
875};
876
b2197755 877int bpf_prog_new_fd(struct bpf_prog *prog)
aa79781b
DB
878{
879 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
880 O_RDWR | O_CLOEXEC);
881}
882
113214be 883static struct bpf_prog *____bpf_prog_get(struct fd f)
09756af4 884{
09756af4
AS
885 if (!f.file)
886 return ERR_PTR(-EBADF);
09756af4
AS
887 if (f.file->f_op != &bpf_prog_fops) {
888 fdput(f);
889 return ERR_PTR(-EINVAL);
890 }
891
c2101297 892 return f.file->private_data;
09756af4
AS
893}
894
59d3656d 895struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
92117d84 896{
59d3656d
BB
897 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
898 atomic_sub(i, &prog->aux->refcnt);
92117d84
AS
899 return ERR_PTR(-EBUSY);
900 }
901 return prog;
902}
59d3656d
BB
903EXPORT_SYMBOL_GPL(bpf_prog_add);
904
c540594f
DB
905void bpf_prog_sub(struct bpf_prog *prog, int i)
906{
907 /* Only to be used for undoing previous bpf_prog_add() in some
908 * error path. We still know that another entity in our call
909 * path holds a reference to the program, thus atomic_sub() can
910 * be safely used in such cases!
911 */
912 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
913}
914EXPORT_SYMBOL_GPL(bpf_prog_sub);
915
59d3656d
BB
916struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
917{
918 return bpf_prog_add(prog, 1);
919}
97bc402d 920EXPORT_SYMBOL_GPL(bpf_prog_inc);
92117d84 921
b16d9aa4 922/* prog_idr_lock should have been held */
a6f6df69 923struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
b16d9aa4
MKL
924{
925 int refold;
926
927 refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
928
929 if (refold >= BPF_MAX_REFCNT) {
930 __bpf_prog_put(prog, false);
931 return ERR_PTR(-EBUSY);
932 }
933
934 if (!refold)
935 return ERR_PTR(-ENOENT);
936
937 return prog;
938}
a6f6df69 939EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
b16d9aa4 940
113214be 941static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *type)
09756af4
AS
942{
943 struct fd f = fdget(ufd);
944 struct bpf_prog *prog;
945
113214be 946 prog = ____bpf_prog_get(f);
09756af4
AS
947 if (IS_ERR(prog))
948 return prog;
113214be
DB
949 if (type && prog->type != *type) {
950 prog = ERR_PTR(-EINVAL);
951 goto out;
952 }
09756af4 953
92117d84 954 prog = bpf_prog_inc(prog);
113214be 955out:
09756af4
AS
956 fdput(f);
957 return prog;
958}
113214be
DB
959
960struct bpf_prog *bpf_prog_get(u32 ufd)
961{
962 return __bpf_prog_get(ufd, NULL);
963}
964
965struct bpf_prog *bpf_prog_get_type(u32 ufd, enum bpf_prog_type type)
966{
a67edbf4
DB
967 struct bpf_prog *prog = __bpf_prog_get(ufd, &type);
968
969 if (!IS_ERR(prog))
970 trace_bpf_prog_get_type(prog);
971 return prog;
113214be
DB
972}
973EXPORT_SYMBOL_GPL(bpf_prog_get_type);
09756af4
AS
974
975/* last field in 'union bpf_attr' used by this command */
e07b98d9 976#define BPF_PROG_LOAD_LAST_FIELD prog_flags
09756af4
AS
977
978static int bpf_prog_load(union bpf_attr *attr)
979{
980 enum bpf_prog_type type = attr->prog_type;
981 struct bpf_prog *prog;
982 int err;
983 char license[128];
984 bool is_gpl;
985
986 if (CHECK_ATTR(BPF_PROG_LOAD))
987 return -EINVAL;
988
e07b98d9
DM
989 if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
990 return -EINVAL;
991
09756af4 992 /* copy eBPF program license from user space */
535e7b4b 993 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
09756af4
AS
994 sizeof(license) - 1) < 0)
995 return -EFAULT;
996 license[sizeof(license) - 1] = 0;
997
998 /* eBPF programs must be GPL compatible to use GPL-ed functions */
999 is_gpl = license_is_gpl_compatible(license);
1000
ef0915ca
DB
1001 if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1002 return -E2BIG;
09756af4 1003
2541517c
AS
1004 if (type == BPF_PROG_TYPE_KPROBE &&
1005 attr->kern_version != LINUX_VERSION_CODE)
1006 return -EINVAL;
1007
80b7d819
CF
1008 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1009 type != BPF_PROG_TYPE_CGROUP_SKB &&
1010 !capable(CAP_SYS_ADMIN))
1be7f75d
AS
1011 return -EPERM;
1012
09756af4
AS
1013 /* plain bpf_prog allocation */
1014 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1015 if (!prog)
1016 return -ENOMEM;
1017
aaac3ba9
AS
1018 err = bpf_prog_charge_memlock(prog);
1019 if (err)
1020 goto free_prog_nouncharge;
1021
09756af4
AS
1022 prog->len = attr->insn_cnt;
1023
1024 err = -EFAULT;
535e7b4b 1025 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
aafe6ae9 1026 bpf_prog_insn_size(prog)) != 0)
09756af4
AS
1027 goto free_prog;
1028
1029 prog->orig_prog = NULL;
a91263d5 1030 prog->jited = 0;
09756af4
AS
1031
1032 atomic_set(&prog->aux->refcnt, 1);
a91263d5 1033 prog->gpl_compatible = is_gpl ? 1 : 0;
09756af4
AS
1034
1035 /* find program type: socket_filter vs tracing_filter */
1036 err = find_prog_type(type, prog);
1037 if (err < 0)
1038 goto free_prog;
1039
1040 /* run eBPF verifier */
9bac3d6d 1041 err = bpf_check(&prog, attr);
09756af4
AS
1042 if (err < 0)
1043 goto free_used_maps;
1044
1045 /* eBPF program is ready to be JITed */
d1c55ab5 1046 prog = bpf_prog_select_runtime(prog, &err);
04fd61ab
AS
1047 if (err < 0)
1048 goto free_used_maps;
09756af4 1049
dc4bb0e2
MKL
1050 err = bpf_prog_alloc_id(prog);
1051 if (err)
1052 goto free_used_maps;
1053
aa79781b 1054 err = bpf_prog_new_fd(prog);
b16d9aa4
MKL
1055 if (err < 0) {
1056 /* failed to allocate fd.
1057 * bpf_prog_put() is needed because the above
1058 * bpf_prog_alloc_id() has published the prog
1059 * to the userspace and the userspace may
1060 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1061 */
1062 bpf_prog_put(prog);
1063 return err;
1064 }
09756af4 1065
74451e66 1066 bpf_prog_kallsyms_add(prog);
a67edbf4 1067 trace_bpf_prog_load(prog, err);
09756af4
AS
1068 return err;
1069
1070free_used_maps:
1071 free_used_maps(prog->aux);
1072free_prog:
aaac3ba9
AS
1073 bpf_prog_uncharge_memlock(prog);
1074free_prog_nouncharge:
09756af4
AS
1075 bpf_prog_free(prog);
1076 return err;
1077}
1078
b2197755
DB
1079#define BPF_OBJ_LAST_FIELD bpf_fd
1080
1081static int bpf_obj_pin(const union bpf_attr *attr)
1082{
1083 if (CHECK_ATTR(BPF_OBJ))
1084 return -EINVAL;
1085
535e7b4b 1086 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
b2197755
DB
1087}
1088
1089static int bpf_obj_get(const union bpf_attr *attr)
1090{
1091 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0)
1092 return -EINVAL;
1093
535e7b4b 1094 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname));
b2197755
DB
1095}
1096
f4324551
DM
1097#ifdef CONFIG_CGROUP_BPF
1098
464bc0fd 1099#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
174a79ff 1100
5a67da2a 1101static int sockmap_get_from_fd(const union bpf_attr *attr, bool attach)
174a79ff 1102{
5a67da2a 1103 struct bpf_prog *prog = NULL;
174a79ff
JF
1104 int ufd = attr->target_fd;
1105 struct bpf_map *map;
1106 struct fd f;
1107 int err;
1108
1109 f = fdget(ufd);
1110 map = __bpf_map_get(f);
1111 if (IS_ERR(map))
1112 return PTR_ERR(map);
1113
5a67da2a
JF
1114 if (attach) {
1115 prog = bpf_prog_get_type(attr->attach_bpf_fd,
1116 BPF_PROG_TYPE_SK_SKB);
1117 if (IS_ERR(prog)) {
1118 fdput(f);
1119 return PTR_ERR(prog);
1120 }
174a79ff
JF
1121 }
1122
5a67da2a 1123 err = sock_map_prog(map, prog, attr->attach_type);
174a79ff
JF
1124 if (err) {
1125 fdput(f);
5a67da2a
JF
1126 if (prog)
1127 bpf_prog_put(prog);
ae2b27b8 1128 return err;
174a79ff
JF
1129 }
1130
1131 fdput(f);
ae2b27b8 1132 return 0;
174a79ff 1133}
f4324551
DM
1134
1135static int bpf_prog_attach(const union bpf_attr *attr)
1136{
7f677633 1137 enum bpf_prog_type ptype;
f4324551
DM
1138 struct bpf_prog *prog;
1139 struct cgroup *cgrp;
7f677633 1140 int ret;
f4324551
DM
1141
1142 if (!capable(CAP_NET_ADMIN))
1143 return -EPERM;
1144
1145 if (CHECK_ATTR(BPF_PROG_ATTACH))
1146 return -EINVAL;
1147
7f677633
AS
1148 if (attr->attach_flags & ~BPF_F_ALLOW_OVERRIDE)
1149 return -EINVAL;
1150
f4324551
DM
1151 switch (attr->attach_type) {
1152 case BPF_CGROUP_INET_INGRESS:
1153 case BPF_CGROUP_INET_EGRESS:
b2cd1257 1154 ptype = BPF_PROG_TYPE_CGROUP_SKB;
f4324551 1155 break;
61023658
DA
1156 case BPF_CGROUP_INET_SOCK_CREATE:
1157 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1158 break;
40304b2a
LB
1159 case BPF_CGROUP_SOCK_OPS:
1160 ptype = BPF_PROG_TYPE_SOCK_OPS;
1161 break;
464bc0fd
JF
1162 case BPF_SK_SKB_STREAM_PARSER:
1163 case BPF_SK_SKB_STREAM_VERDICT:
5a67da2a 1164 return sockmap_get_from_fd(attr, true);
f4324551
DM
1165 default:
1166 return -EINVAL;
1167 }
1168
b2cd1257
DA
1169 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1170 if (IS_ERR(prog))
1171 return PTR_ERR(prog);
1172
1173 cgrp = cgroup_get_from_fd(attr->target_fd);
1174 if (IS_ERR(cgrp)) {
1175 bpf_prog_put(prog);
1176 return PTR_ERR(cgrp);
1177 }
1178
7f677633
AS
1179 ret = cgroup_bpf_update(cgrp, prog, attr->attach_type,
1180 attr->attach_flags & BPF_F_ALLOW_OVERRIDE);
1181 if (ret)
1182 bpf_prog_put(prog);
b2cd1257
DA
1183 cgroup_put(cgrp);
1184
7f677633 1185 return ret;
f4324551
DM
1186}
1187
1188#define BPF_PROG_DETACH_LAST_FIELD attach_type
1189
1190static int bpf_prog_detach(const union bpf_attr *attr)
1191{
1192 struct cgroup *cgrp;
7f677633 1193 int ret;
f4324551
DM
1194
1195 if (!capable(CAP_NET_ADMIN))
1196 return -EPERM;
1197
1198 if (CHECK_ATTR(BPF_PROG_DETACH))
1199 return -EINVAL;
1200
1201 switch (attr->attach_type) {
1202 case BPF_CGROUP_INET_INGRESS:
1203 case BPF_CGROUP_INET_EGRESS:
61023658 1204 case BPF_CGROUP_INET_SOCK_CREATE:
40304b2a 1205 case BPF_CGROUP_SOCK_OPS:
f4324551
DM
1206 cgrp = cgroup_get_from_fd(attr->target_fd);
1207 if (IS_ERR(cgrp))
1208 return PTR_ERR(cgrp);
1209
7f677633 1210 ret = cgroup_bpf_update(cgrp, NULL, attr->attach_type, false);
f4324551
DM
1211 cgroup_put(cgrp);
1212 break;
5a67da2a
JF
1213 case BPF_SK_SKB_STREAM_PARSER:
1214 case BPF_SK_SKB_STREAM_VERDICT:
1215 ret = sockmap_get_from_fd(attr, false);
1216 break;
f4324551
DM
1217 default:
1218 return -EINVAL;
1219 }
1220
7f677633 1221 return ret;
f4324551 1222}
40304b2a 1223
f4324551
DM
1224#endif /* CONFIG_CGROUP_BPF */
1225
1cf1cae9
AS
1226#define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1227
1228static int bpf_prog_test_run(const union bpf_attr *attr,
1229 union bpf_attr __user *uattr)
1230{
1231 struct bpf_prog *prog;
1232 int ret = -ENOTSUPP;
1233
1234 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1235 return -EINVAL;
1236
1237 prog = bpf_prog_get(attr->test.prog_fd);
1238 if (IS_ERR(prog))
1239 return PTR_ERR(prog);
1240
1241 if (prog->aux->ops->test_run)
1242 ret = prog->aux->ops->test_run(prog, attr, uattr);
1243
1244 bpf_prog_put(prog);
1245 return ret;
1246}
1247
34ad5580
MKL
1248#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1249
1250static int bpf_obj_get_next_id(const union bpf_attr *attr,
1251 union bpf_attr __user *uattr,
1252 struct idr *idr,
1253 spinlock_t *lock)
1254{
1255 u32 next_id = attr->start_id;
1256 int err = 0;
1257
1258 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1259 return -EINVAL;
1260
1261 if (!capable(CAP_SYS_ADMIN))
1262 return -EPERM;
1263
1264 next_id++;
1265 spin_lock_bh(lock);
1266 if (!idr_get_next(idr, &next_id))
1267 err = -ENOENT;
1268 spin_unlock_bh(lock);
1269
1270 if (!err)
1271 err = put_user(next_id, &uattr->next_id);
1272
1273 return err;
1274}
1275
b16d9aa4
MKL
1276#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1277
1278static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1279{
1280 struct bpf_prog *prog;
1281 u32 id = attr->prog_id;
1282 int fd;
1283
1284 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1285 return -EINVAL;
1286
1287 if (!capable(CAP_SYS_ADMIN))
1288 return -EPERM;
1289
1290 spin_lock_bh(&prog_idr_lock);
1291 prog = idr_find(&prog_idr, id);
1292 if (prog)
1293 prog = bpf_prog_inc_not_zero(prog);
1294 else
1295 prog = ERR_PTR(-ENOENT);
1296 spin_unlock_bh(&prog_idr_lock);
1297
1298 if (IS_ERR(prog))
1299 return PTR_ERR(prog);
1300
1301 fd = bpf_prog_new_fd(prog);
1302 if (fd < 0)
1303 bpf_prog_put(prog);
1304
1305 return fd;
1306}
1307
bd5f5f4e
MKL
1308#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD map_id
1309
1310static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1311{
1312 struct bpf_map *map;
1313 u32 id = attr->map_id;
1314 int fd;
1315
1316 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID))
1317 return -EINVAL;
1318
1319 if (!capable(CAP_SYS_ADMIN))
1320 return -EPERM;
1321
1322 spin_lock_bh(&map_idr_lock);
1323 map = idr_find(&map_idr, id);
1324 if (map)
1325 map = bpf_map_inc_not_zero(map, true);
1326 else
1327 map = ERR_PTR(-ENOENT);
1328 spin_unlock_bh(&map_idr_lock);
1329
1330 if (IS_ERR(map))
1331 return PTR_ERR(map);
1332
1333 fd = bpf_map_new_fd(map);
1334 if (fd < 0)
1335 bpf_map_put(map);
1336
1337 return fd;
1338}
1339
1e270976
MKL
1340static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1341 const union bpf_attr *attr,
1342 union bpf_attr __user *uattr)
1343{
1344 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1345 struct bpf_prog_info info = {};
1346 u32 info_len = attr->info.info_len;
1347 char __user *uinsns;
1348 u32 ulen;
1349 int err;
1350
1351 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1352 if (err)
1353 return err;
1354 info_len = min_t(u32, sizeof(info), info_len);
1355
1356 if (copy_from_user(&info, uinfo, info_len))
89b09689 1357 return -EFAULT;
1e270976
MKL
1358
1359 info.type = prog->type;
1360 info.id = prog->aux->id;
1361
1362 memcpy(info.tag, prog->tag, sizeof(prog->tag));
1363
1364 if (!capable(CAP_SYS_ADMIN)) {
1365 info.jited_prog_len = 0;
1366 info.xlated_prog_len = 0;
1367 goto done;
1368 }
1369
1370 ulen = info.jited_prog_len;
1371 info.jited_prog_len = prog->jited_len;
1372 if (info.jited_prog_len && ulen) {
1373 uinsns = u64_to_user_ptr(info.jited_prog_insns);
1374 ulen = min_t(u32, info.jited_prog_len, ulen);
1375 if (copy_to_user(uinsns, prog->bpf_func, ulen))
1376 return -EFAULT;
1377 }
1378
1379 ulen = info.xlated_prog_len;
9975a54b 1380 info.xlated_prog_len = bpf_prog_insn_size(prog);
1e270976
MKL
1381 if (info.xlated_prog_len && ulen) {
1382 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1383 ulen = min_t(u32, info.xlated_prog_len, ulen);
1384 if (copy_to_user(uinsns, prog->insnsi, ulen))
1385 return -EFAULT;
1386 }
1387
1388done:
1389 if (copy_to_user(uinfo, &info, info_len) ||
1390 put_user(info_len, &uattr->info.info_len))
1391 return -EFAULT;
1392
1393 return 0;
1394}
1395
1396static int bpf_map_get_info_by_fd(struct bpf_map *map,
1397 const union bpf_attr *attr,
1398 union bpf_attr __user *uattr)
1399{
1400 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1401 struct bpf_map_info info = {};
1402 u32 info_len = attr->info.info_len;
1403 int err;
1404
1405 err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1406 if (err)
1407 return err;
1408 info_len = min_t(u32, sizeof(info), info_len);
1409
1410 info.type = map->map_type;
1411 info.id = map->id;
1412 info.key_size = map->key_size;
1413 info.value_size = map->value_size;
1414 info.max_entries = map->max_entries;
1415 info.map_flags = map->map_flags;
1416
1417 if (copy_to_user(uinfo, &info, info_len) ||
1418 put_user(info_len, &uattr->info.info_len))
1419 return -EFAULT;
1420
1421 return 0;
1422}
1423
1424#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
1425
1426static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
1427 union bpf_attr __user *uattr)
1428{
1429 int ufd = attr->info.bpf_fd;
1430 struct fd f;
1431 int err;
1432
1433 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
1434 return -EINVAL;
1435
1436 f = fdget(ufd);
1437 if (!f.file)
1438 return -EBADFD;
1439
1440 if (f.file->f_op == &bpf_prog_fops)
1441 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
1442 uattr);
1443 else if (f.file->f_op == &bpf_map_fops)
1444 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
1445 uattr);
1446 else
1447 err = -EINVAL;
1448
1449 fdput(f);
1450 return err;
1451}
1452
99c55f7d
AS
1453SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
1454{
1455 union bpf_attr attr = {};
1456 int err;
1457
1be7f75d 1458 if (!capable(CAP_SYS_ADMIN) && sysctl_unprivileged_bpf_disabled)
99c55f7d
AS
1459 return -EPERM;
1460
1e270976
MKL
1461 err = check_uarg_tail_zero(uattr, sizeof(attr), size);
1462 if (err)
1463 return err;
1464 size = min_t(u32, size, sizeof(attr));
99c55f7d
AS
1465
1466 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
1467 if (copy_from_user(&attr, uattr, size) != 0)
1468 return -EFAULT;
1469
1470 switch (cmd) {
1471 case BPF_MAP_CREATE:
1472 err = map_create(&attr);
1473 break;
db20fd2b
AS
1474 case BPF_MAP_LOOKUP_ELEM:
1475 err = map_lookup_elem(&attr);
1476 break;
1477 case BPF_MAP_UPDATE_ELEM:
1478 err = map_update_elem(&attr);
1479 break;
1480 case BPF_MAP_DELETE_ELEM:
1481 err = map_delete_elem(&attr);
1482 break;
1483 case BPF_MAP_GET_NEXT_KEY:
1484 err = map_get_next_key(&attr);
1485 break;
09756af4
AS
1486 case BPF_PROG_LOAD:
1487 err = bpf_prog_load(&attr);
1488 break;
b2197755
DB
1489 case BPF_OBJ_PIN:
1490 err = bpf_obj_pin(&attr);
1491 break;
1492 case BPF_OBJ_GET:
1493 err = bpf_obj_get(&attr);
1494 break;
f4324551
DM
1495#ifdef CONFIG_CGROUP_BPF
1496 case BPF_PROG_ATTACH:
1497 err = bpf_prog_attach(&attr);
1498 break;
1499 case BPF_PROG_DETACH:
1500 err = bpf_prog_detach(&attr);
1501 break;
1502#endif
1cf1cae9
AS
1503 case BPF_PROG_TEST_RUN:
1504 err = bpf_prog_test_run(&attr, uattr);
1505 break;
34ad5580
MKL
1506 case BPF_PROG_GET_NEXT_ID:
1507 err = bpf_obj_get_next_id(&attr, uattr,
1508 &prog_idr, &prog_idr_lock);
1509 break;
1510 case BPF_MAP_GET_NEXT_ID:
1511 err = bpf_obj_get_next_id(&attr, uattr,
1512 &map_idr, &map_idr_lock);
1513 break;
b16d9aa4
MKL
1514 case BPF_PROG_GET_FD_BY_ID:
1515 err = bpf_prog_get_fd_by_id(&attr);
1516 break;
bd5f5f4e
MKL
1517 case BPF_MAP_GET_FD_BY_ID:
1518 err = bpf_map_get_fd_by_id(&attr);
1519 break;
1e270976
MKL
1520 case BPF_OBJ_GET_INFO_BY_FD:
1521 err = bpf_obj_get_info_by_fd(&attr, uattr);
1522 break;
99c55f7d
AS
1523 default:
1524 err = -EINVAL;
1525 break;
1526 }
1527
1528 return err;
1529}