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