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