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