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