bpf: Create BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY
[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>
f4364dcf 14#include <linux/bpf_lirc.h>
f56a653c 15#include <linux/btf.h>
99c55f7d
AS
16#include <linux/syscalls.h>
17#include <linux/slab.h>
3f07c014 18#include <linux/sched/signal.h>
d407bd25
DB
19#include <linux/vmalloc.h>
20#include <linux/mmzone.h>
99c55f7d 21#include <linux/anon_inodes.h>
41bdc4b4 22#include <linux/fdtable.h>
db20fd2b 23#include <linux/file.h>
41bdc4b4 24#include <linux/fs.h>
09756af4
AS
25#include <linux/license.h>
26#include <linux/filter.h>
2541517c 27#include <linux/version.h>
535e7b4b 28#include <linux/kernel.h>
dc4bb0e2 29#include <linux/idr.h>
cb4d2b3f
MKL
30#include <linux/cred.h>
31#include <linux/timekeeping.h>
32#include <linux/ctype.h>
9ef09e35 33#include <linux/nospec.h>
99c55f7d 34
14dc6f04
MKL
35#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
36 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
38 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
39#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
40#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
41
6e71b04a
CF
42#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
43
b121d1e7 44DEFINE_PER_CPU(int, bpf_prog_active);
dc4bb0e2
MKL
45static DEFINE_IDR(prog_idr);
46static DEFINE_SPINLOCK(prog_idr_lock);
f3f1c054
MKL
47static DEFINE_IDR(map_idr);
48static DEFINE_SPINLOCK(map_idr_lock);
b121d1e7 49
1be7f75d
AS
50int sysctl_unprivileged_bpf_disabled __read_mostly;
51
40077e0c
JB
52static const struct bpf_map_ops * const bpf_map_types[] = {
53#define BPF_PROG_TYPE(_id, _ops)
54#define BPF_MAP_TYPE(_id, _ops) \
55 [_id] = &_ops,
56#include <linux/bpf_types.h>
57#undef BPF_PROG_TYPE
58#undef BPF_MAP_TYPE
59};
99c55f7d 60
752ba56f
MS
61/*
62 * If we're handed a bigger struct than we know of, ensure all the unknown bits
63 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
64 * we don't know about yet.
65 *
66 * There is a ToCToU between this function call and the following
67 * copy_from_user() call. However, this is not a concern since this function is
68 * meant to be a future-proofing of bits.
69 */
dcab51f1
MKL
70int bpf_check_uarg_tail_zero(void __user *uaddr,
71 size_t expected_size,
72 size_t actual_size)
58291a74
MS
73{
74 unsigned char __user *addr;
75 unsigned char __user *end;
76 unsigned char val;
77 int err;
78
752ba56f
MS
79 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
80 return -E2BIG;
81
96d4f267 82 if (unlikely(!access_ok(uaddr, actual_size)))
752ba56f
MS
83 return -EFAULT;
84
58291a74
MS
85 if (actual_size <= expected_size)
86 return 0;
87
88 addr = uaddr + expected_size;
89 end = uaddr + actual_size;
90
91 for (; addr < end; addr++) {
92 err = get_user(val, addr);
93 if (err)
94 return err;
95 if (val)
96 return -E2BIG;
97 }
98
99 return 0;
100}
101
a3884572
JK
102const struct bpf_map_ops bpf_map_offload_ops = {
103 .map_alloc = bpf_map_offload_map_alloc,
104 .map_free = bpf_map_offload_map_free,
e8d2bec0 105 .map_check_btf = map_check_no_btf,
a3884572
JK
106};
107
99c55f7d
AS
108static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
109{
1110f3a9 110 const struct bpf_map_ops *ops;
9ef09e35 111 u32 type = attr->map_type;
99c55f7d 112 struct bpf_map *map;
1110f3a9 113 int err;
99c55f7d 114
9ef09e35 115 if (type >= ARRAY_SIZE(bpf_map_types))
1110f3a9 116 return ERR_PTR(-EINVAL);
9ef09e35
MR
117 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
118 ops = bpf_map_types[type];
1110f3a9 119 if (!ops)
40077e0c 120 return ERR_PTR(-EINVAL);
99c55f7d 121
1110f3a9
JK
122 if (ops->map_alloc_check) {
123 err = ops->map_alloc_check(attr);
124 if (err)
125 return ERR_PTR(err);
126 }
a3884572
JK
127 if (attr->map_ifindex)
128 ops = &bpf_map_offload_ops;
1110f3a9 129 map = ops->map_alloc(attr);
40077e0c
JB
130 if (IS_ERR(map))
131 return map;
1110f3a9 132 map->ops = ops;
9ef09e35 133 map->map_type = type;
40077e0c 134 return map;
99c55f7d
AS
135}
136
96eabe7a 137void *bpf_map_area_alloc(size_t size, int numa_node)
d407bd25 138{
f01a7dbe
MP
139 /* We really just want to fail instead of triggering OOM killer
140 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
141 * which is used for lower order allocation requests.
142 *
143 * It has been observed that higher order allocation requests done by
144 * vmalloc with __GFP_NORETRY being set might fail due to not trying
145 * to reclaim memory from the page cache, thus we set
146 * __GFP_RETRY_MAYFAIL to avoid such situations.
d407bd25 147 */
f01a7dbe
MP
148
149 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
d407bd25
DB
150 void *area;
151
152 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
f01a7dbe
MP
153 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
154 numa_node);
d407bd25
DB
155 if (area != NULL)
156 return area;
157 }
158
f01a7dbe
MP
159 return __vmalloc_node_flags_caller(size, numa_node,
160 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
161 flags, __builtin_return_address(0));
d407bd25
DB
162}
163
164void bpf_map_area_free(void *area)
165{
166 kvfree(area);
167}
168
be70bcd5
DB
169static u32 bpf_map_flags_retain_permanent(u32 flags)
170{
171 /* Some map creation flags are not tied to the map object but
172 * rather to the map fd instead, so they have no meaning upon
173 * map object inspection since multiple file descriptors with
174 * different (access) properties can exist here. Thus, given
175 * this has zero meaning for the map itself, lets clear these
176 * from here.
177 */
178 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
179}
180
bd475643
JK
181void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
182{
183 map->map_type = attr->map_type;
184 map->key_size = attr->key_size;
185 map->value_size = attr->value_size;
186 map->max_entries = attr->max_entries;
be70bcd5 187 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
bd475643
JK
188 map->numa_node = bpf_map_attr_numa_node(attr);
189}
190
6c905981
AS
191int bpf_map_precharge_memlock(u32 pages)
192{
193 struct user_struct *user = get_current_user();
194 unsigned long memlock_limit, cur;
195
196 memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
197 cur = atomic_long_read(&user->locked_vm);
198 free_uid(user);
199 if (cur + pages > memlock_limit)
200 return -EPERM;
201 return 0;
202}
203
0a4c58f5 204static int bpf_charge_memlock(struct user_struct *user, u32 pages)
aaac3ba9 205{
0a4c58f5 206 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
aaac3ba9 207
0a4c58f5
RG
208 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
209 atomic_long_sub(pages, &user->locked_vm);
210 return -EPERM;
211 }
212 return 0;
213}
aaac3ba9 214
0a4c58f5
RG
215static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
216{
217 atomic_long_sub(pages, &user->locked_vm);
218}
aaac3ba9 219
0a4c58f5
RG
220static int bpf_map_init_memlock(struct bpf_map *map)
221{
222 struct user_struct *user = get_current_user();
223 int ret;
aaac3ba9 224
0a4c58f5
RG
225 ret = bpf_charge_memlock(user, map->pages);
226 if (ret) {
aaac3ba9 227 free_uid(user);
0a4c58f5 228 return ret;
aaac3ba9
AS
229 }
230 map->user = user;
0a4c58f5 231 return ret;
aaac3ba9
AS
232}
233
0a4c58f5 234static void bpf_map_release_memlock(struct bpf_map *map)
aaac3ba9
AS
235{
236 struct user_struct *user = map->user;
0a4c58f5 237 bpf_uncharge_memlock(user, map->pages);
aaac3ba9
AS
238 free_uid(user);
239}
240
0a4c58f5
RG
241int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
242{
243 int ret;
244
245 ret = bpf_charge_memlock(map->user, pages);
246 if (ret)
247 return ret;
248 map->pages += pages;
249 return ret;
250}
251
252void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
253{
254 bpf_uncharge_memlock(map->user, pages);
255 map->pages -= pages;
256}
257
f3f1c054
MKL
258static int bpf_map_alloc_id(struct bpf_map *map)
259{
260 int id;
261
b76354cd 262 idr_preload(GFP_KERNEL);
f3f1c054
MKL
263 spin_lock_bh(&map_idr_lock);
264 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
265 if (id > 0)
266 map->id = id;
267 spin_unlock_bh(&map_idr_lock);
b76354cd 268 idr_preload_end();
f3f1c054
MKL
269
270 if (WARN_ON_ONCE(!id))
271 return -ENOSPC;
272
273 return id > 0 ? 0 : id;
274}
275
a3884572 276void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
f3f1c054 277{
930651a7
ED
278 unsigned long flags;
279
a3884572
JK
280 /* Offloaded maps are removed from the IDR store when their device
281 * disappears - even if someone holds an fd to them they are unusable,
282 * the memory is gone, all ops will fail; they are simply waiting for
283 * refcnt to drop to be freed.
284 */
285 if (!map->id)
286 return;
287
bd5f5f4e 288 if (do_idr_lock)
930651a7 289 spin_lock_irqsave(&map_idr_lock, flags);
bd5f5f4e
MKL
290 else
291 __acquire(&map_idr_lock);
292
f3f1c054 293 idr_remove(&map_idr, map->id);
a3884572 294 map->id = 0;
bd5f5f4e
MKL
295
296 if (do_idr_lock)
930651a7 297 spin_unlock_irqrestore(&map_idr_lock, flags);
bd5f5f4e
MKL
298 else
299 __release(&map_idr_lock);
f3f1c054
MKL
300}
301
99c55f7d
AS
302/* called from workqueue */
303static void bpf_map_free_deferred(struct work_struct *work)
304{
305 struct bpf_map *map = container_of(work, struct bpf_map, work);
306
0a4c58f5 307 bpf_map_release_memlock(map);
afdb09c7 308 security_bpf_map_free(map);
99c55f7d
AS
309 /* implementation dependent freeing */
310 map->ops->map_free(map);
311}
312
c9da161c
DB
313static void bpf_map_put_uref(struct bpf_map *map)
314{
315 if (atomic_dec_and_test(&map->usercnt)) {
ba6b8de4
JF
316 if (map->ops->map_release_uref)
317 map->ops->map_release_uref(map);
c9da161c
DB
318 }
319}
320
99c55f7d
AS
321/* decrement map refcnt and schedule it for freeing via workqueue
322 * (unrelying map implementation ops->map_free() might sleep)
323 */
bd5f5f4e 324static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
99c55f7d
AS
325{
326 if (atomic_dec_and_test(&map->refcnt)) {
34ad5580 327 /* bpf_map_free_id() must be called first */
bd5f5f4e 328 bpf_map_free_id(map, do_idr_lock);
78958fca 329 btf_put(map->btf);
99c55f7d
AS
330 INIT_WORK(&map->work, bpf_map_free_deferred);
331 schedule_work(&map->work);
332 }
333}
334
bd5f5f4e
MKL
335void bpf_map_put(struct bpf_map *map)
336{
337 __bpf_map_put(map, true);
338}
630a4d38 339EXPORT_SYMBOL_GPL(bpf_map_put);
bd5f5f4e 340
c9da161c 341void bpf_map_put_with_uref(struct bpf_map *map)
99c55f7d 342{
c9da161c 343 bpf_map_put_uref(map);
99c55f7d 344 bpf_map_put(map);
c9da161c
DB
345}
346
347static int bpf_map_release(struct inode *inode, struct file *filp)
348{
61d1b6a4
DB
349 struct bpf_map *map = filp->private_data;
350
351 if (map->ops->map_release)
352 map->ops->map_release(map, filp);
353
354 bpf_map_put_with_uref(map);
99c55f7d
AS
355 return 0;
356}
357
87df15de
DB
358static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
359{
360 fmode_t mode = f.file->f_mode;
361
362 /* Our file permissions may have been overridden by global
363 * map permissions facing syscall side.
364 */
365 if (READ_ONCE(map->frozen))
366 mode &= ~FMODE_CAN_WRITE;
367 return mode;
368}
369
f99bf205
DB
370#ifdef CONFIG_PROC_FS
371static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
372{
373 const struct bpf_map *map = filp->private_data;
21116b70
DB
374 const struct bpf_array *array;
375 u32 owner_prog_type = 0;
9780c0ab 376 u32 owner_jited = 0;
21116b70
DB
377
378 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
379 array = container_of(map, struct bpf_array, map);
380 owner_prog_type = array->owner_prog_type;
9780c0ab 381 owner_jited = array->owner_jited;
21116b70 382 }
f99bf205
DB
383
384 seq_printf(m,
385 "map_type:\t%u\n"
386 "key_size:\t%u\n"
387 "value_size:\t%u\n"
322cea2f 388 "max_entries:\t%u\n"
21116b70 389 "map_flags:\t%#x\n"
4316b409 390 "memlock:\t%llu\n"
87df15de
DB
391 "map_id:\t%u\n"
392 "frozen:\t%u\n",
f99bf205
DB
393 map->map_type,
394 map->key_size,
395 map->value_size,
322cea2f 396 map->max_entries,
21116b70 397 map->map_flags,
4316b409 398 map->pages * 1ULL << PAGE_SHIFT,
87df15de
DB
399 map->id,
400 READ_ONCE(map->frozen));
21116b70 401
9780c0ab 402 if (owner_prog_type) {
21116b70
DB
403 seq_printf(m, "owner_prog_type:\t%u\n",
404 owner_prog_type);
9780c0ab
DB
405 seq_printf(m, "owner_jited:\t%u\n",
406 owner_jited);
407 }
f99bf205
DB
408}
409#endif
410
6e71b04a
CF
411static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
412 loff_t *ppos)
413{
414 /* We need this handler such that alloc_file() enables
415 * f_mode with FMODE_CAN_READ.
416 */
417 return -EINVAL;
418}
419
420static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
421 size_t siz, loff_t *ppos)
422{
423 /* We need this handler such that alloc_file() enables
424 * f_mode with FMODE_CAN_WRITE.
425 */
426 return -EINVAL;
427}
428
f66e448c 429const struct file_operations bpf_map_fops = {
f99bf205
DB
430#ifdef CONFIG_PROC_FS
431 .show_fdinfo = bpf_map_show_fdinfo,
432#endif
433 .release = bpf_map_release,
6e71b04a
CF
434 .read = bpf_dummy_read,
435 .write = bpf_dummy_write,
99c55f7d
AS
436};
437
6e71b04a 438int bpf_map_new_fd(struct bpf_map *map, int flags)
aa79781b 439{
afdb09c7
CF
440 int ret;
441
442 ret = security_bpf_map(map, OPEN_FMODE(flags));
443 if (ret < 0)
444 return ret;
445
aa79781b 446 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
6e71b04a
CF
447 flags | O_CLOEXEC);
448}
449
450int bpf_get_file_flag(int flags)
451{
452 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
453 return -EINVAL;
454 if (flags & BPF_F_RDONLY)
455 return O_RDONLY;
456 if (flags & BPF_F_WRONLY)
457 return O_WRONLY;
458 return O_RDWR;
aa79781b
DB
459}
460
99c55f7d
AS
461/* helper macro to check that unused fields 'union bpf_attr' are zero */
462#define CHECK_ATTR(CMD) \
463 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
464 sizeof(attr->CMD##_LAST_FIELD), 0, \
465 sizeof(*attr) - \
466 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
467 sizeof(attr->CMD##_LAST_FIELD)) != NULL
468
cb4d2b3f
MKL
469/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
470 * Return 0 on success and < 0 on error.
471 */
472static int bpf_obj_name_cpy(char *dst, const char *src)
473{
474 const char *end = src + BPF_OBJ_NAME_LEN;
475
473d9734 476 memset(dst, 0, BPF_OBJ_NAME_LEN);
3e0ddc4f 477 /* Copy all isalnum(), '_' and '.' chars. */
cb4d2b3f 478 while (src < end && *src) {
3e0ddc4f
DB
479 if (!isalnum(*src) &&
480 *src != '_' && *src != '.')
cb4d2b3f
MKL
481 return -EINVAL;
482 *dst++ = *src++;
483 }
484
485 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
486 if (src == end)
487 return -EINVAL;
488
cb4d2b3f
MKL
489 return 0;
490}
491
e8d2bec0 492int map_check_no_btf(const struct bpf_map *map,
1b2b234b 493 const struct btf *btf,
e8d2bec0
DB
494 const struct btf_type *key_type,
495 const struct btf_type *value_type)
496{
497 return -ENOTSUPP;
498}
499
d83525ca 500static int map_check_btf(struct bpf_map *map, const struct btf *btf,
e8d2bec0
DB
501 u32 btf_key_id, u32 btf_value_id)
502{
503 const struct btf_type *key_type, *value_type;
504 u32 key_size, value_size;
505 int ret = 0;
506
2824ecb7
DB
507 /* Some maps allow key to be unspecified. */
508 if (btf_key_id) {
509 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
510 if (!key_type || key_size != map->key_size)
511 return -EINVAL;
512 } else {
513 key_type = btf_type_by_id(btf, 0);
514 if (!map->ops->map_check_btf)
515 return -EINVAL;
516 }
e8d2bec0
DB
517
518 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
519 if (!value_type || value_size != map->value_size)
520 return -EINVAL;
521
d83525ca
AS
522 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
523
524 if (map_value_has_spin_lock(map)) {
591fe988
DB
525 if (map->map_flags & BPF_F_RDONLY_PROG)
526 return -EACCES;
d83525ca 527 if (map->map_type != BPF_MAP_TYPE_HASH &&
e16d2f1a 528 map->map_type != BPF_MAP_TYPE_ARRAY &&
6ac99e8f
MKL
529 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
530 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
d83525ca
AS
531 return -ENOTSUPP;
532 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
533 map->value_size) {
534 WARN_ONCE(1,
535 "verifier bug spin_lock_off %d value_size %d\n",
536 map->spin_lock_off, map->value_size);
537 return -EFAULT;
538 }
539 }
540
e8d2bec0 541 if (map->ops->map_check_btf)
1b2b234b 542 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
e8d2bec0
DB
543
544 return ret;
545}
546
9b2cf328 547#define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
99c55f7d
AS
548/* called via syscall */
549static int map_create(union bpf_attr *attr)
550{
96eabe7a 551 int numa_node = bpf_map_attr_numa_node(attr);
99c55f7d 552 struct bpf_map *map;
6e71b04a 553 int f_flags;
99c55f7d
AS
554 int err;
555
556 err = CHECK_ATTR(BPF_MAP_CREATE);
557 if (err)
558 return -EINVAL;
559
6e71b04a
CF
560 f_flags = bpf_get_file_flag(attr->map_flags);
561 if (f_flags < 0)
562 return f_flags;
563
96eabe7a 564 if (numa_node != NUMA_NO_NODE &&
96e5ae4e
ED
565 ((unsigned int)numa_node >= nr_node_ids ||
566 !node_online(numa_node)))
96eabe7a
MKL
567 return -EINVAL;
568
99c55f7d
AS
569 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
570 map = find_and_alloc_map(attr);
571 if (IS_ERR(map))
572 return PTR_ERR(map);
573
ad5b177b
MKL
574 err = bpf_obj_name_cpy(map->name, attr->map_name);
575 if (err)
576 goto free_map_nouncharge;
577
99c55f7d 578 atomic_set(&map->refcnt, 1);
c9da161c 579 atomic_set(&map->usercnt, 1);
99c55f7d 580
e8d2bec0 581 if (attr->btf_key_type_id || attr->btf_value_type_id) {
a26ca7c9
MKL
582 struct btf *btf;
583
2824ecb7 584 if (!attr->btf_value_type_id) {
a26ca7c9
MKL
585 err = -EINVAL;
586 goto free_map_nouncharge;
587 }
588
589 btf = btf_get_by_fd(attr->btf_fd);
590 if (IS_ERR(btf)) {
591 err = PTR_ERR(btf);
592 goto free_map_nouncharge;
593 }
594
e8d2bec0
DB
595 err = map_check_btf(map, btf, attr->btf_key_type_id,
596 attr->btf_value_type_id);
a26ca7c9
MKL
597 if (err) {
598 btf_put(btf);
599 goto free_map_nouncharge;
600 }
601
602 map->btf = btf;
9b2cf328
MKL
603 map->btf_key_type_id = attr->btf_key_type_id;
604 map->btf_value_type_id = attr->btf_value_type_id;
d83525ca
AS
605 } else {
606 map->spin_lock_off = -EINVAL;
a26ca7c9
MKL
607 }
608
afdb09c7 609 err = security_bpf_map_alloc(map);
aaac3ba9 610 if (err)
20b2b24f 611 goto free_map_nouncharge;
aaac3ba9 612
0a4c58f5 613 err = bpf_map_init_memlock(map);
afdb09c7
CF
614 if (err)
615 goto free_map_sec;
616
f3f1c054
MKL
617 err = bpf_map_alloc_id(map);
618 if (err)
619 goto free_map;
620
6e71b04a 621 err = bpf_map_new_fd(map, f_flags);
bd5f5f4e
MKL
622 if (err < 0) {
623 /* failed to allocate fd.
352d20d6 624 * bpf_map_put_with_uref() is needed because the above
bd5f5f4e
MKL
625 * bpf_map_alloc_id() has published the map
626 * to the userspace and the userspace may
627 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
628 */
352d20d6 629 bpf_map_put_with_uref(map);
bd5f5f4e
MKL
630 return err;
631 }
99c55f7d
AS
632
633 return err;
634
635free_map:
0a4c58f5 636 bpf_map_release_memlock(map);
afdb09c7
CF
637free_map_sec:
638 security_bpf_map_free(map);
20b2b24f 639free_map_nouncharge:
a26ca7c9 640 btf_put(map->btf);
99c55f7d
AS
641 map->ops->map_free(map);
642 return err;
643}
644
db20fd2b
AS
645/* if error is returned, fd is released.
646 * On success caller should complete fd access with matching fdput()
647 */
c2101297 648struct bpf_map *__bpf_map_get(struct fd f)
db20fd2b 649{
db20fd2b
AS
650 if (!f.file)
651 return ERR_PTR(-EBADF);
db20fd2b
AS
652 if (f.file->f_op != &bpf_map_fops) {
653 fdput(f);
654 return ERR_PTR(-EINVAL);
655 }
656
c2101297
DB
657 return f.file->private_data;
658}
659
92117d84
AS
660/* prog's and map's refcnt limit */
661#define BPF_MAX_REFCNT 32768
662
663struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
c9da161c 664{
92117d84
AS
665 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
666 atomic_dec(&map->refcnt);
667 return ERR_PTR(-EBUSY);
668 }
c9da161c
DB
669 if (uref)
670 atomic_inc(&map->usercnt);
92117d84 671 return map;
c9da161c 672}
630a4d38 673EXPORT_SYMBOL_GPL(bpf_map_inc);
c9da161c
DB
674
675struct bpf_map *bpf_map_get_with_uref(u32 ufd)
c2101297
DB
676{
677 struct fd f = fdget(ufd);
678 struct bpf_map *map;
679
680 map = __bpf_map_get(f);
681 if (IS_ERR(map))
682 return map;
683
92117d84 684 map = bpf_map_inc(map, true);
c2101297 685 fdput(f);
db20fd2b
AS
686
687 return map;
688}
689
bd5f5f4e
MKL
690/* map_idr_lock should have been held */
691static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
692 bool uref)
693{
694 int refold;
695
bfc18e38 696 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
bd5f5f4e
MKL
697
698 if (refold >= BPF_MAX_REFCNT) {
699 __bpf_map_put(map, false);
700 return ERR_PTR(-EBUSY);
701 }
702
703 if (!refold)
704 return ERR_PTR(-ENOENT);
705
706 if (uref)
707 atomic_inc(&map->usercnt);
708
709 return map;
710}
711
b8cdc051
AS
712int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
713{
714 return -ENOTSUPP;
715}
716
c9d29f46
MV
717static void *__bpf_copy_key(void __user *ukey, u64 key_size)
718{
719 if (key_size)
720 return memdup_user(ukey, key_size);
721
722 if (ukey)
723 return ERR_PTR(-EINVAL);
724
725 return NULL;
726}
727
db20fd2b 728/* last field in 'union bpf_attr' used by this command */
96049f3a 729#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
db20fd2b
AS
730
731static int map_lookup_elem(union bpf_attr *attr)
732{
535e7b4b
MS
733 void __user *ukey = u64_to_user_ptr(attr->key);
734 void __user *uvalue = u64_to_user_ptr(attr->value);
db20fd2b 735 int ufd = attr->map_fd;
db20fd2b 736 struct bpf_map *map;
8ebe667c 737 void *key, *value, *ptr;
15a07b33 738 u32 value_size;
592867bf 739 struct fd f;
db20fd2b
AS
740 int err;
741
742 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
743 return -EINVAL;
744
96049f3a
AS
745 if (attr->flags & ~BPF_F_LOCK)
746 return -EINVAL;
747
592867bf 748 f = fdget(ufd);
c2101297 749 map = __bpf_map_get(f);
db20fd2b
AS
750 if (IS_ERR(map))
751 return PTR_ERR(map);
87df15de 752 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
6e71b04a
CF
753 err = -EPERM;
754 goto err_put;
755 }
756
96049f3a
AS
757 if ((attr->flags & BPF_F_LOCK) &&
758 !map_value_has_spin_lock(map)) {
759 err = -EINVAL;
760 goto err_put;
761 }
762
c9d29f46 763 key = __bpf_copy_key(ukey, map->key_size);
e4448ed8
AV
764 if (IS_ERR(key)) {
765 err = PTR_ERR(key);
db20fd2b 766 goto err_put;
e4448ed8 767 }
db20fd2b 768
15a07b33 769 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
8f844938 770 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
b741f163
RG
771 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
772 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
15a07b33 773 value_size = round_up(map->value_size, 8) * num_possible_cpus();
14dc6f04
MKL
774 else if (IS_FD_MAP(map))
775 value_size = sizeof(u32);
15a07b33
AS
776 else
777 value_size = map->value_size;
778
8ebe667c 779 err = -ENOMEM;
15a07b33 780 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
db20fd2b 781 if (!value)
8ebe667c
AS
782 goto free_key;
783
a3884572
JK
784 if (bpf_map_is_dev_bound(map)) {
785 err = bpf_map_offload_lookup_elem(map, key, value);
7c4cd051
MKL
786 goto done;
787 }
788
789 preempt_disable();
790 this_cpu_inc(bpf_prog_active);
791 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
792 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
15a07b33
AS
793 err = bpf_percpu_hash_copy(map, key, value);
794 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
795 err = bpf_percpu_array_copy(map, key, value);
b741f163
RG
796 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
797 err = bpf_percpu_cgroup_storage_copy(map, key, value);
557c0c6e
AS
798 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
799 err = bpf_stackmap_copy(map, key, value);
14dc6f04
MKL
800 } else if (IS_FD_ARRAY(map)) {
801 err = bpf_fd_array_map_lookup_elem(map, key, value);
802 } else if (IS_FD_HASH(map)) {
803 err = bpf_fd_htab_map_lookup_elem(map, key, value);
5dc4c4b7
MKL
804 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
805 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
f1a2e44a
MV
806 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
807 map->map_type == BPF_MAP_TYPE_STACK) {
808 err = map->ops->map_peek_elem(map, value);
15a07b33
AS
809 } else {
810 rcu_read_lock();
c6110222
DB
811 if (map->ops->map_lookup_elem_sys_only)
812 ptr = map->ops->map_lookup_elem_sys_only(map, key);
813 else
814 ptr = map->ops->map_lookup_elem(map, key);
509db283
PB
815 if (IS_ERR(ptr)) {
816 err = PTR_ERR(ptr);
817 } else if (!ptr) {
818 err = -ENOENT;
819 } else {
820 err = 0;
96049f3a
AS
821 if (attr->flags & BPF_F_LOCK)
822 /* lock 'ptr' and copy everything but lock */
823 copy_map_value_locked(map, value, ptr, true);
824 else
825 copy_map_value(map, value, ptr);
826 /* mask lock, since value wasn't zero inited */
827 check_and_init_map_lock(map, value);
509db283 828 }
15a07b33 829 rcu_read_unlock();
15a07b33 830 }
7c4cd051
MKL
831 this_cpu_dec(bpf_prog_active);
832 preempt_enable();
8ebe667c 833
7c4cd051 834done:
15a07b33 835 if (err)
8ebe667c 836 goto free_value;
db20fd2b
AS
837
838 err = -EFAULT;
15a07b33 839 if (copy_to_user(uvalue, value, value_size) != 0)
8ebe667c 840 goto free_value;
db20fd2b
AS
841
842 err = 0;
843
8ebe667c
AS
844free_value:
845 kfree(value);
db20fd2b
AS
846free_key:
847 kfree(key);
848err_put:
849 fdput(f);
850 return err;
851}
852
1ae80cf3
DC
853static void maybe_wait_bpf_programs(struct bpf_map *map)
854{
855 /* Wait for any running BPF programs to complete so that
856 * userspace, when we return to it, knows that all programs
857 * that could be running use the new map value.
858 */
859 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
860 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
861 synchronize_rcu();
862}
863
3274f520 864#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
db20fd2b
AS
865
866static int map_update_elem(union bpf_attr *attr)
867{
535e7b4b
MS
868 void __user *ukey = u64_to_user_ptr(attr->key);
869 void __user *uvalue = u64_to_user_ptr(attr->value);
db20fd2b 870 int ufd = attr->map_fd;
db20fd2b
AS
871 struct bpf_map *map;
872 void *key, *value;
15a07b33 873 u32 value_size;
592867bf 874 struct fd f;
db20fd2b
AS
875 int err;
876
877 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
878 return -EINVAL;
879
592867bf 880 f = fdget(ufd);
c2101297 881 map = __bpf_map_get(f);
db20fd2b
AS
882 if (IS_ERR(map))
883 return PTR_ERR(map);
87df15de 884 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
6e71b04a
CF
885 err = -EPERM;
886 goto err_put;
887 }
888
96049f3a
AS
889 if ((attr->flags & BPF_F_LOCK) &&
890 !map_value_has_spin_lock(map)) {
891 err = -EINVAL;
892 goto err_put;
893 }
894
c9d29f46 895 key = __bpf_copy_key(ukey, map->key_size);
e4448ed8
AV
896 if (IS_ERR(key)) {
897 err = PTR_ERR(key);
db20fd2b 898 goto err_put;
e4448ed8 899 }
db20fd2b 900
15a07b33 901 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
8f844938 902 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
b741f163
RG
903 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
904 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
15a07b33
AS
905 value_size = round_up(map->value_size, 8) * num_possible_cpus();
906 else
907 value_size = map->value_size;
908
db20fd2b 909 err = -ENOMEM;
15a07b33 910 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
db20fd2b
AS
911 if (!value)
912 goto free_key;
913
914 err = -EFAULT;
15a07b33 915 if (copy_from_user(value, uvalue, value_size) != 0)
db20fd2b
AS
916 goto free_value;
917
6710e112 918 /* Need to create a kthread, thus must support schedule */
a3884572
JK
919 if (bpf_map_is_dev_bound(map)) {
920 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
921 goto out;
99ba2b5a
JF
922 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
923 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
924 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
6710e112
JDB
925 err = map->ops->map_update_elem(map, key, value, attr->flags);
926 goto out;
927 }
928
b121d1e7
AS
929 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
930 * inside bpf map update or delete otherwise deadlocks are possible
931 */
932 preempt_disable();
933 __this_cpu_inc(bpf_prog_active);
8f844938
MKL
934 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
935 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
15a07b33
AS
936 err = bpf_percpu_hash_update(map, key, value, attr->flags);
937 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
938 err = bpf_percpu_array_update(map, key, value, attr->flags);
b741f163
RG
939 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
940 err = bpf_percpu_cgroup_storage_update(map, key, value,
941 attr->flags);
9c147b56 942 } else if (IS_FD_ARRAY(map)) {
d056a788
DB
943 rcu_read_lock();
944 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
945 attr->flags);
946 rcu_read_unlock();
bcc6b1b7
MKL
947 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
948 rcu_read_lock();
949 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
950 attr->flags);
951 rcu_read_unlock();
5dc4c4b7
MKL
952 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
953 /* rcu_read_lock() is not needed */
954 err = bpf_fd_reuseport_array_update_elem(map, key, value,
955 attr->flags);
f1a2e44a
MV
956 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
957 map->map_type == BPF_MAP_TYPE_STACK) {
958 err = map->ops->map_push_elem(map, value, attr->flags);
15a07b33
AS
959 } else {
960 rcu_read_lock();
961 err = map->ops->map_update_elem(map, key, value, attr->flags);
962 rcu_read_unlock();
963 }
b121d1e7
AS
964 __this_cpu_dec(bpf_prog_active);
965 preempt_enable();
1ae80cf3 966 maybe_wait_bpf_programs(map);
6710e112 967out:
db20fd2b
AS
968free_value:
969 kfree(value);
970free_key:
971 kfree(key);
972err_put:
973 fdput(f);
974 return err;
975}
976
977#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
978
979static int map_delete_elem(union bpf_attr *attr)
980{
535e7b4b 981 void __user *ukey = u64_to_user_ptr(attr->key);
db20fd2b 982 int ufd = attr->map_fd;
db20fd2b 983 struct bpf_map *map;
592867bf 984 struct fd f;
db20fd2b
AS
985 void *key;
986 int err;
987
988 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
989 return -EINVAL;
990
592867bf 991 f = fdget(ufd);
c2101297 992 map = __bpf_map_get(f);
db20fd2b
AS
993 if (IS_ERR(map))
994 return PTR_ERR(map);
87df15de 995 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
6e71b04a
CF
996 err = -EPERM;
997 goto err_put;
998 }
999
c9d29f46 1000 key = __bpf_copy_key(ukey, map->key_size);
e4448ed8
AV
1001 if (IS_ERR(key)) {
1002 err = PTR_ERR(key);
db20fd2b 1003 goto err_put;
e4448ed8 1004 }
db20fd2b 1005
a3884572
JK
1006 if (bpf_map_is_dev_bound(map)) {
1007 err = bpf_map_offload_delete_elem(map, key);
1008 goto out;
1009 }
1010
b121d1e7
AS
1011 preempt_disable();
1012 __this_cpu_inc(bpf_prog_active);
db20fd2b
AS
1013 rcu_read_lock();
1014 err = map->ops->map_delete_elem(map, key);
1015 rcu_read_unlock();
b121d1e7
AS
1016 __this_cpu_dec(bpf_prog_active);
1017 preempt_enable();
1ae80cf3 1018 maybe_wait_bpf_programs(map);
a3884572 1019out:
db20fd2b
AS
1020 kfree(key);
1021err_put:
1022 fdput(f);
1023 return err;
1024}
1025
1026/* last field in 'union bpf_attr' used by this command */
1027#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1028
1029static int map_get_next_key(union bpf_attr *attr)
1030{
535e7b4b
MS
1031 void __user *ukey = u64_to_user_ptr(attr->key);
1032 void __user *unext_key = u64_to_user_ptr(attr->next_key);
db20fd2b 1033 int ufd = attr->map_fd;
db20fd2b
AS
1034 struct bpf_map *map;
1035 void *key, *next_key;
592867bf 1036 struct fd f;
db20fd2b
AS
1037 int err;
1038
1039 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1040 return -EINVAL;
1041
592867bf 1042 f = fdget(ufd);
c2101297 1043 map = __bpf_map_get(f);
db20fd2b
AS
1044 if (IS_ERR(map))
1045 return PTR_ERR(map);
87df15de 1046 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
6e71b04a
CF
1047 err = -EPERM;
1048 goto err_put;
1049 }
1050
8fe45924 1051 if (ukey) {
c9d29f46 1052 key = __bpf_copy_key(ukey, map->key_size);
e4448ed8
AV
1053 if (IS_ERR(key)) {
1054 err = PTR_ERR(key);
8fe45924 1055 goto err_put;
e4448ed8 1056 }
8fe45924
TQ
1057 } else {
1058 key = NULL;
1059 }
db20fd2b
AS
1060
1061 err = -ENOMEM;
1062 next_key = kmalloc(map->key_size, GFP_USER);
1063 if (!next_key)
1064 goto free_key;
1065
a3884572
JK
1066 if (bpf_map_is_dev_bound(map)) {
1067 err = bpf_map_offload_get_next_key(map, key, next_key);
1068 goto out;
1069 }
1070
db20fd2b
AS
1071 rcu_read_lock();
1072 err = map->ops->map_get_next_key(map, key, next_key);
1073 rcu_read_unlock();
a3884572 1074out:
db20fd2b
AS
1075 if (err)
1076 goto free_next_key;
1077
1078 err = -EFAULT;
1079 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1080 goto free_next_key;
1081
1082 err = 0;
1083
1084free_next_key:
1085 kfree(next_key);
1086free_key:
1087 kfree(key);
1088err_put:
1089 fdput(f);
1090 return err;
1091}
1092
bd513cd0
MV
1093#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1094
1095static int map_lookup_and_delete_elem(union bpf_attr *attr)
1096{
1097 void __user *ukey = u64_to_user_ptr(attr->key);
1098 void __user *uvalue = u64_to_user_ptr(attr->value);
1099 int ufd = attr->map_fd;
1100 struct bpf_map *map;
540fefc0 1101 void *key, *value;
bd513cd0
MV
1102 u32 value_size;
1103 struct fd f;
1104 int err;
1105
1106 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1107 return -EINVAL;
1108
1109 f = fdget(ufd);
1110 map = __bpf_map_get(f);
1111 if (IS_ERR(map))
1112 return PTR_ERR(map);
87df15de 1113 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
bd513cd0
MV
1114 err = -EPERM;
1115 goto err_put;
1116 }
1117
1118 key = __bpf_copy_key(ukey, map->key_size);
1119 if (IS_ERR(key)) {
1120 err = PTR_ERR(key);
1121 goto err_put;
1122 }
1123
1124 value_size = map->value_size;
1125
1126 err = -ENOMEM;
1127 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1128 if (!value)
1129 goto free_key;
1130
1131 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1132 map->map_type == BPF_MAP_TYPE_STACK) {
1133 err = map->ops->map_pop_elem(map, value);
1134 } else {
1135 err = -ENOTSUPP;
1136 }
1137
1138 if (err)
1139 goto free_value;
1140
1141 if (copy_to_user(uvalue, value, value_size) != 0)
1142 goto free_value;
1143
1144 err = 0;
1145
1146free_value:
1147 kfree(value);
1148free_key:
1149 kfree(key);
1150err_put:
1151 fdput(f);
1152 return err;
1153}
1154
87df15de
DB
1155#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1156
1157static int map_freeze(const union bpf_attr *attr)
1158{
1159 int err = 0, ufd = attr->map_fd;
1160 struct bpf_map *map;
1161 struct fd f;
1162
1163 if (CHECK_ATTR(BPF_MAP_FREEZE))
1164 return -EINVAL;
1165
1166 f = fdget(ufd);
1167 map = __bpf_map_get(f);
1168 if (IS_ERR(map))
1169 return PTR_ERR(map);
1170 if (READ_ONCE(map->frozen)) {
1171 err = -EBUSY;
1172 goto err_put;
1173 }
1174 if (!capable(CAP_SYS_ADMIN)) {
1175 err = -EPERM;
1176 goto err_put;
1177 }
1178
1179 WRITE_ONCE(map->frozen, true);
1180err_put:
1181 fdput(f);
1182 return err;
1183}
1184
7de16e3a
JK
1185static const struct bpf_prog_ops * const bpf_prog_types[] = {
1186#define BPF_PROG_TYPE(_id, _name) \
1187 [_id] = & _name ## _prog_ops,
1188#define BPF_MAP_TYPE(_id, _ops)
1189#include <linux/bpf_types.h>
1190#undef BPF_PROG_TYPE
1191#undef BPF_MAP_TYPE
1192};
1193
09756af4
AS
1194static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1195{
d0f1a451
DB
1196 const struct bpf_prog_ops *ops;
1197
1198 if (type >= ARRAY_SIZE(bpf_prog_types))
1199 return -EINVAL;
1200 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1201 ops = bpf_prog_types[type];
1202 if (!ops)
be9370a7 1203 return -EINVAL;
09756af4 1204
ab3f0063 1205 if (!bpf_prog_is_dev_bound(prog->aux))
d0f1a451 1206 prog->aux->ops = ops;
ab3f0063
JK
1207 else
1208 prog->aux->ops = &bpf_offload_prog_ops;
be9370a7
JB
1209 prog->type = type;
1210 return 0;
09756af4
AS
1211}
1212
1213/* drop refcnt on maps used by eBPF program and free auxilary data */
1214static void free_used_maps(struct bpf_prog_aux *aux)
1215{
8bad74f9 1216 enum bpf_cgroup_storage_type stype;
09756af4
AS
1217 int i;
1218
8bad74f9
RG
1219 for_each_cgroup_storage_type(stype) {
1220 if (!aux->cgroup_storage[stype])
1221 continue;
1222 bpf_cgroup_storage_release(aux->prog,
1223 aux->cgroup_storage[stype]);
1224 }
de9cbbaa 1225
09756af4
AS
1226 for (i = 0; i < aux->used_map_cnt; i++)
1227 bpf_map_put(aux->used_maps[i]);
1228
1229 kfree(aux->used_maps);
1230}
1231
5ccb071e
DB
1232int __bpf_prog_charge(struct user_struct *user, u32 pages)
1233{
1234 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1235 unsigned long user_bufs;
1236
1237 if (user) {
1238 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1239 if (user_bufs > memlock_limit) {
1240 atomic_long_sub(pages, &user->locked_vm);
1241 return -EPERM;
1242 }
1243 }
1244
1245 return 0;
1246}
1247
1248void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1249{
1250 if (user)
1251 atomic_long_sub(pages, &user->locked_vm);
1252}
1253
aaac3ba9
AS
1254static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1255{
1256 struct user_struct *user = get_current_user();
5ccb071e 1257 int ret;
aaac3ba9 1258
5ccb071e
DB
1259 ret = __bpf_prog_charge(user, prog->pages);
1260 if (ret) {
aaac3ba9 1261 free_uid(user);
5ccb071e 1262 return ret;
aaac3ba9 1263 }
5ccb071e 1264
aaac3ba9
AS
1265 prog->aux->user = user;
1266 return 0;
1267}
1268
1269static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1270{
1271 struct user_struct *user = prog->aux->user;
1272
5ccb071e 1273 __bpf_prog_uncharge(user, prog->pages);
aaac3ba9
AS
1274 free_uid(user);
1275}
1276
dc4bb0e2
MKL
1277static int bpf_prog_alloc_id(struct bpf_prog *prog)
1278{
1279 int id;
1280
b76354cd 1281 idr_preload(GFP_KERNEL);
dc4bb0e2
MKL
1282 spin_lock_bh(&prog_idr_lock);
1283 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1284 if (id > 0)
1285 prog->aux->id = id;
1286 spin_unlock_bh(&prog_idr_lock);
b76354cd 1287 idr_preload_end();
dc4bb0e2
MKL
1288
1289 /* id is in [1, INT_MAX) */
1290 if (WARN_ON_ONCE(!id))
1291 return -ENOSPC;
1292
1293 return id > 0 ? 0 : id;
1294}
1295
ad8ad79f 1296void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
dc4bb0e2 1297{
ad8ad79f
JK
1298 /* cBPF to eBPF migrations are currently not in the idr store.
1299 * Offloaded programs are removed from the store when their device
1300 * disappears - even if someone grabs an fd to them they are unusable,
1301 * simply waiting for refcnt to drop to be freed.
1302 */
dc4bb0e2
MKL
1303 if (!prog->aux->id)
1304 return;
1305
b16d9aa4
MKL
1306 if (do_idr_lock)
1307 spin_lock_bh(&prog_idr_lock);
1308 else
1309 __acquire(&prog_idr_lock);
1310
dc4bb0e2 1311 idr_remove(&prog_idr, prog->aux->id);
ad8ad79f 1312 prog->aux->id = 0;
b16d9aa4
MKL
1313
1314 if (do_idr_lock)
1315 spin_unlock_bh(&prog_idr_lock);
1316 else
1317 __release(&prog_idr_lock);
dc4bb0e2
MKL
1318}
1319
1aacde3d 1320static void __bpf_prog_put_rcu(struct rcu_head *rcu)
abf2e7d6
AS
1321{
1322 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1323
1324 free_used_maps(aux);
aaac3ba9 1325 bpf_prog_uncharge_memlock(aux->prog);
afdb09c7 1326 security_bpf_prog_free(aux);
abf2e7d6
AS
1327 bpf_prog_free(aux->prog);
1328}
1329
b16d9aa4 1330static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
09756af4 1331{
a67edbf4 1332 if (atomic_dec_and_test(&prog->aux->refcnt)) {
6ee52e2a 1333 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
34ad5580 1334 /* bpf_prog_free_id() must be called first */
b16d9aa4 1335 bpf_prog_free_id(prog, do_idr_lock);
7d1982b4 1336 bpf_prog_kallsyms_del_all(prog);
838e9690 1337 btf_put(prog->aux->btf);
ba64e7d8 1338 kvfree(prog->aux->func_info);
c454a46b 1339 bpf_prog_free_linfo(prog);
4f74d809 1340
1aacde3d 1341 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
a67edbf4 1342 }
09756af4 1343}
b16d9aa4
MKL
1344
1345void bpf_prog_put(struct bpf_prog *prog)
1346{
1347 __bpf_prog_put(prog, true);
1348}
e2e9b654 1349EXPORT_SYMBOL_GPL(bpf_prog_put);
09756af4
AS
1350
1351static int bpf_prog_release(struct inode *inode, struct file *filp)
1352{
1353 struct bpf_prog *prog = filp->private_data;
1354
1aacde3d 1355 bpf_prog_put(prog);
09756af4
AS
1356 return 0;
1357}
1358
492ecee8
AS
1359static void bpf_prog_get_stats(const struct bpf_prog *prog,
1360 struct bpf_prog_stats *stats)
1361{
1362 u64 nsecs = 0, cnt = 0;
1363 int cpu;
1364
1365 for_each_possible_cpu(cpu) {
1366 const struct bpf_prog_stats *st;
1367 unsigned int start;
1368 u64 tnsecs, tcnt;
1369
1370 st = per_cpu_ptr(prog->aux->stats, cpu);
1371 do {
1372 start = u64_stats_fetch_begin_irq(&st->syncp);
1373 tnsecs = st->nsecs;
1374 tcnt = st->cnt;
1375 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1376 nsecs += tnsecs;
1377 cnt += tcnt;
1378 }
1379 stats->nsecs = nsecs;
1380 stats->cnt = cnt;
1381}
1382
7bd509e3
DB
1383#ifdef CONFIG_PROC_FS
1384static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1385{
1386 const struct bpf_prog *prog = filp->private_data;
f1f7714e 1387 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
492ecee8 1388 struct bpf_prog_stats stats;
7bd509e3 1389
492ecee8 1390 bpf_prog_get_stats(prog, &stats);
f1f7714e 1391 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
7bd509e3
DB
1392 seq_printf(m,
1393 "prog_type:\t%u\n"
1394 "prog_jited:\t%u\n"
f1f7714e 1395 "prog_tag:\t%s\n"
4316b409 1396 "memlock:\t%llu\n"
492ecee8
AS
1397 "prog_id:\t%u\n"
1398 "run_time_ns:\t%llu\n"
1399 "run_cnt:\t%llu\n",
7bd509e3
DB
1400 prog->type,
1401 prog->jited,
f1f7714e 1402 prog_tag,
4316b409 1403 prog->pages * 1ULL << PAGE_SHIFT,
492ecee8
AS
1404 prog->aux->id,
1405 stats.nsecs,
1406 stats.cnt);
7bd509e3
DB
1407}
1408#endif
1409
f66e448c 1410const struct file_operations bpf_prog_fops = {
7bd509e3
DB
1411#ifdef CONFIG_PROC_FS
1412 .show_fdinfo = bpf_prog_show_fdinfo,
1413#endif
1414 .release = bpf_prog_release,
6e71b04a
CF
1415 .read = bpf_dummy_read,
1416 .write = bpf_dummy_write,
09756af4
AS
1417};
1418
b2197755 1419int bpf_prog_new_fd(struct bpf_prog *prog)
aa79781b 1420{
afdb09c7
CF
1421 int ret;
1422
1423 ret = security_bpf_prog(prog);
1424 if (ret < 0)
1425 return ret;
1426
aa79781b
DB
1427 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1428 O_RDWR | O_CLOEXEC);
1429}
1430
113214be 1431static struct bpf_prog *____bpf_prog_get(struct fd f)
09756af4 1432{
09756af4
AS
1433 if (!f.file)
1434 return ERR_PTR(-EBADF);
09756af4
AS
1435 if (f.file->f_op != &bpf_prog_fops) {
1436 fdput(f);
1437 return ERR_PTR(-EINVAL);
1438 }
1439
c2101297 1440 return f.file->private_data;
09756af4
AS
1441}
1442
59d3656d 1443struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
92117d84 1444{
59d3656d
BB
1445 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1446 atomic_sub(i, &prog->aux->refcnt);
92117d84
AS
1447 return ERR_PTR(-EBUSY);
1448 }
1449 return prog;
1450}
59d3656d
BB
1451EXPORT_SYMBOL_GPL(bpf_prog_add);
1452
c540594f
DB
1453void bpf_prog_sub(struct bpf_prog *prog, int i)
1454{
1455 /* Only to be used for undoing previous bpf_prog_add() in some
1456 * error path. We still know that another entity in our call
1457 * path holds a reference to the program, thus atomic_sub() can
1458 * be safely used in such cases!
1459 */
1460 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1461}
1462EXPORT_SYMBOL_GPL(bpf_prog_sub);
1463
59d3656d
BB
1464struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1465{
1466 return bpf_prog_add(prog, 1);
1467}
97bc402d 1468EXPORT_SYMBOL_GPL(bpf_prog_inc);
92117d84 1469
b16d9aa4 1470/* prog_idr_lock should have been held */
a6f6df69 1471struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
b16d9aa4
MKL
1472{
1473 int refold;
1474
bfc18e38 1475 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
b16d9aa4
MKL
1476
1477 if (refold >= BPF_MAX_REFCNT) {
1478 __bpf_prog_put(prog, false);
1479 return ERR_PTR(-EBUSY);
1480 }
1481
1482 if (!refold)
1483 return ERR_PTR(-ENOENT);
1484
1485 return prog;
1486}
a6f6df69 1487EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
b16d9aa4 1488
040ee692 1489bool bpf_prog_get_ok(struct bpf_prog *prog,
288b3de5 1490 enum bpf_prog_type *attach_type, bool attach_drv)
248f346f 1491{
288b3de5
JK
1492 /* not an attachment, just a refcount inc, always allow */
1493 if (!attach_type)
1494 return true;
248f346f
JK
1495
1496 if (prog->type != *attach_type)
1497 return false;
288b3de5 1498 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
248f346f
JK
1499 return false;
1500
1501 return true;
1502}
1503
1504static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
288b3de5 1505 bool attach_drv)
09756af4
AS
1506{
1507 struct fd f = fdget(ufd);
1508 struct bpf_prog *prog;
1509
113214be 1510 prog = ____bpf_prog_get(f);
09756af4
AS
1511 if (IS_ERR(prog))
1512 return prog;
288b3de5 1513 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
113214be
DB
1514 prog = ERR_PTR(-EINVAL);
1515 goto out;
1516 }
09756af4 1517
92117d84 1518 prog = bpf_prog_inc(prog);
113214be 1519out:
09756af4
AS
1520 fdput(f);
1521 return prog;
1522}
113214be
DB
1523
1524struct bpf_prog *bpf_prog_get(u32 ufd)
1525{
288b3de5 1526 return __bpf_prog_get(ufd, NULL, false);
113214be
DB
1527}
1528
248f346f 1529struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
288b3de5 1530 bool attach_drv)
248f346f 1531{
4d220ed0 1532 return __bpf_prog_get(ufd, &type, attach_drv);
248f346f 1533}
6c8dfe21 1534EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
248f346f 1535
aac3fc32
AI
1536/* Initially all BPF programs could be loaded w/o specifying
1537 * expected_attach_type. Later for some of them specifying expected_attach_type
1538 * at load time became required so that program could be validated properly.
1539 * Programs of types that are allowed to be loaded both w/ and w/o (for
1540 * backward compatibility) expected_attach_type, should have the default attach
1541 * type assigned to expected_attach_type for the latter case, so that it can be
1542 * validated later at attach time.
1543 *
1544 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1545 * prog type requires it but has some attach types that have to be backward
1546 * compatible.
1547 */
1548static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1549{
1550 switch (attr->prog_type) {
1551 case BPF_PROG_TYPE_CGROUP_SOCK:
1552 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1553 * exist so checking for non-zero is the way to go here.
1554 */
1555 if (!attr->expected_attach_type)
1556 attr->expected_attach_type =
1557 BPF_CGROUP_INET_SOCK_CREATE;
1558 break;
1559 }
1560}
1561
5e43f899
AI
1562static int
1563bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1564 enum bpf_attach_type expected_attach_type)
1565{
4fbac77d 1566 switch (prog_type) {
aac3fc32
AI
1567 case BPF_PROG_TYPE_CGROUP_SOCK:
1568 switch (expected_attach_type) {
1569 case BPF_CGROUP_INET_SOCK_CREATE:
1570 case BPF_CGROUP_INET4_POST_BIND:
1571 case BPF_CGROUP_INET6_POST_BIND:
1572 return 0;
1573 default:
1574 return -EINVAL;
1575 }
4fbac77d
AI
1576 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1577 switch (expected_attach_type) {
1578 case BPF_CGROUP_INET4_BIND:
1579 case BPF_CGROUP_INET6_BIND:
d74bad4e
AI
1580 case BPF_CGROUP_INET4_CONNECT:
1581 case BPF_CGROUP_INET6_CONNECT:
1cedee13
AI
1582 case BPF_CGROUP_UDP4_SENDMSG:
1583 case BPF_CGROUP_UDP6_SENDMSG:
4fbac77d
AI
1584 return 0;
1585 default:
1586 return -EINVAL;
1587 }
1588 default:
1589 return 0;
1590 }
5e43f899
AI
1591}
1592
09756af4 1593/* last field in 'union bpf_attr' used by this command */
c454a46b 1594#define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
09756af4 1595
838e9690 1596static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
09756af4
AS
1597{
1598 enum bpf_prog_type type = attr->prog_type;
1599 struct bpf_prog *prog;
1600 int err;
1601 char license[128];
1602 bool is_gpl;
1603
1604 if (CHECK_ATTR(BPF_PROG_LOAD))
1605 return -EINVAL;
1606
c240eff6
JW
1607 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
1608 BPF_F_ANY_ALIGNMENT |
1609 BPF_F_TEST_RND_HI32))
e07b98d9
DM
1610 return -EINVAL;
1611
e9ee9efc
DM
1612 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1613 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1614 !capable(CAP_SYS_ADMIN))
1615 return -EPERM;
1616
09756af4 1617 /* copy eBPF program license from user space */
535e7b4b 1618 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
09756af4
AS
1619 sizeof(license) - 1) < 0)
1620 return -EFAULT;
1621 license[sizeof(license) - 1] = 0;
1622
1623 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1624 is_gpl = license_is_gpl_compatible(license);
1625
c04c0d2b
AS
1626 if (attr->insn_cnt == 0 ||
1627 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
ef0915ca 1628 return -E2BIG;
80b7d819
CF
1629 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1630 type != BPF_PROG_TYPE_CGROUP_SKB &&
1631 !capable(CAP_SYS_ADMIN))
1be7f75d
AS
1632 return -EPERM;
1633
aac3fc32 1634 bpf_prog_load_fixup_attach_type(attr);
5e43f899
AI
1635 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1636 return -EINVAL;
1637
09756af4
AS
1638 /* plain bpf_prog allocation */
1639 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1640 if (!prog)
1641 return -ENOMEM;
1642
5e43f899
AI
1643 prog->expected_attach_type = attr->expected_attach_type;
1644
9a18eedb
JK
1645 prog->aux->offload_requested = !!attr->prog_ifindex;
1646
afdb09c7 1647 err = security_bpf_prog_alloc(prog->aux);
aaac3ba9
AS
1648 if (err)
1649 goto free_prog_nouncharge;
1650
afdb09c7
CF
1651 err = bpf_prog_charge_memlock(prog);
1652 if (err)
1653 goto free_prog_sec;
1654
09756af4
AS
1655 prog->len = attr->insn_cnt;
1656
1657 err = -EFAULT;
535e7b4b 1658 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
aafe6ae9 1659 bpf_prog_insn_size(prog)) != 0)
09756af4
AS
1660 goto free_prog;
1661
1662 prog->orig_prog = NULL;
a91263d5 1663 prog->jited = 0;
09756af4
AS
1664
1665 atomic_set(&prog->aux->refcnt, 1);
a91263d5 1666 prog->gpl_compatible = is_gpl ? 1 : 0;
09756af4 1667
9a18eedb 1668 if (bpf_prog_is_dev_bound(prog->aux)) {
ab3f0063
JK
1669 err = bpf_prog_offload_init(prog, attr);
1670 if (err)
1671 goto free_prog;
1672 }
1673
09756af4
AS
1674 /* find program type: socket_filter vs tracing_filter */
1675 err = find_prog_type(type, prog);
1676 if (err < 0)
1677 goto free_prog;
1678
cb4d2b3f
MKL
1679 prog->aux->load_time = ktime_get_boot_ns();
1680 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1681 if (err)
1682 goto free_prog;
1683
09756af4 1684 /* run eBPF verifier */
838e9690 1685 err = bpf_check(&prog, attr, uattr);
09756af4
AS
1686 if (err < 0)
1687 goto free_used_maps;
1688
9facc336 1689 prog = bpf_prog_select_runtime(prog, &err);
04fd61ab
AS
1690 if (err < 0)
1691 goto free_used_maps;
09756af4 1692
dc4bb0e2
MKL
1693 err = bpf_prog_alloc_id(prog);
1694 if (err)
1695 goto free_used_maps;
1696
aa79781b 1697 err = bpf_prog_new_fd(prog);
b16d9aa4
MKL
1698 if (err < 0) {
1699 /* failed to allocate fd.
1700 * bpf_prog_put() is needed because the above
1701 * bpf_prog_alloc_id() has published the prog
1702 * to the userspace and the userspace may
1703 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1704 */
1705 bpf_prog_put(prog);
1706 return err;
1707 }
09756af4 1708
74451e66 1709 bpf_prog_kallsyms_add(prog);
6ee52e2a 1710 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
09756af4
AS
1711 return err;
1712
1713free_used_maps:
c454a46b 1714 bpf_prog_free_linfo(prog);
5482e9a9
MKL
1715 kvfree(prog->aux->func_info);
1716 btf_put(prog->aux->btf);
7d1982b4 1717 bpf_prog_kallsyms_del_subprogs(prog);
09756af4
AS
1718 free_used_maps(prog->aux);
1719free_prog:
aaac3ba9 1720 bpf_prog_uncharge_memlock(prog);
afdb09c7
CF
1721free_prog_sec:
1722 security_bpf_prog_free(prog->aux);
aaac3ba9 1723free_prog_nouncharge:
09756af4
AS
1724 bpf_prog_free(prog);
1725 return err;
1726}
1727
6e71b04a 1728#define BPF_OBJ_LAST_FIELD file_flags
b2197755
DB
1729
1730static int bpf_obj_pin(const union bpf_attr *attr)
1731{
6e71b04a 1732 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
b2197755
DB
1733 return -EINVAL;
1734
535e7b4b 1735 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
b2197755
DB
1736}
1737
1738static int bpf_obj_get(const union bpf_attr *attr)
1739{
6e71b04a
CF
1740 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1741 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
b2197755
DB
1742 return -EINVAL;
1743
6e71b04a
CF
1744 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1745 attr->file_flags);
b2197755
DB
1746}
1747
c4f6699d
AS
1748struct bpf_raw_tracepoint {
1749 struct bpf_raw_event_map *btp;
1750 struct bpf_prog *prog;
1751};
1752
1753static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1754{
1755 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1756
1757 if (raw_tp->prog) {
1758 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1759 bpf_prog_put(raw_tp->prog);
1760 }
a38d1107 1761 bpf_put_raw_tracepoint(raw_tp->btp);
c4f6699d
AS
1762 kfree(raw_tp);
1763 return 0;
1764}
1765
1766static const struct file_operations bpf_raw_tp_fops = {
1767 .release = bpf_raw_tracepoint_release,
1768 .read = bpf_dummy_read,
1769 .write = bpf_dummy_write,
1770};
1771
1772#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1773
1774static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1775{
1776 struct bpf_raw_tracepoint *raw_tp;
1777 struct bpf_raw_event_map *btp;
1778 struct bpf_prog *prog;
1779 char tp_name[128];
1780 int tp_fd, err;
1781
1782 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1783 sizeof(tp_name) - 1) < 0)
1784 return -EFAULT;
1785 tp_name[sizeof(tp_name) - 1] = 0;
1786
a38d1107 1787 btp = bpf_get_raw_tracepoint(tp_name);
c4f6699d
AS
1788 if (!btp)
1789 return -ENOENT;
1790
1791 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
a38d1107
MM
1792 if (!raw_tp) {
1793 err = -ENOMEM;
1794 goto out_put_btp;
1795 }
c4f6699d
AS
1796 raw_tp->btp = btp;
1797
9df1c28b 1798 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
c4f6699d
AS
1799 if (IS_ERR(prog)) {
1800 err = PTR_ERR(prog);
1801 goto out_free_tp;
1802 }
9df1c28b
MM
1803 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1804 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
1805 err = -EINVAL;
1806 goto out_put_prog;
1807 }
c4f6699d
AS
1808
1809 err = bpf_probe_register(raw_tp->btp, prog);
1810 if (err)
1811 goto out_put_prog;
1812
1813 raw_tp->prog = prog;
1814 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1815 O_CLOEXEC);
1816 if (tp_fd < 0) {
1817 bpf_probe_unregister(raw_tp->btp, prog);
1818 err = tp_fd;
1819 goto out_put_prog;
1820 }
1821 return tp_fd;
1822
1823out_put_prog:
1824 bpf_prog_put(prog);
1825out_free_tp:
1826 kfree(raw_tp);
a38d1107
MM
1827out_put_btp:
1828 bpf_put_raw_tracepoint(btp);
c4f6699d
AS
1829 return err;
1830}
1831
33491588
AR
1832static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1833 enum bpf_attach_type attach_type)
1834{
1835 switch (prog->type) {
1836 case BPF_PROG_TYPE_CGROUP_SOCK:
1837 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1838 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1839 default:
1840 return 0;
1841 }
1842}
1843
464bc0fd 1844#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
174a79ff 1845
324bda9e
AS
1846#define BPF_F_ATTACH_MASK \
1847 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1848
f4324551
DM
1849static int bpf_prog_attach(const union bpf_attr *attr)
1850{
7f677633 1851 enum bpf_prog_type ptype;
f4324551 1852 struct bpf_prog *prog;
7f677633 1853 int ret;
f4324551
DM
1854
1855 if (!capable(CAP_NET_ADMIN))
1856 return -EPERM;
1857
1858 if (CHECK_ATTR(BPF_PROG_ATTACH))
1859 return -EINVAL;
1860
324bda9e 1861 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
7f677633
AS
1862 return -EINVAL;
1863
f4324551
DM
1864 switch (attr->attach_type) {
1865 case BPF_CGROUP_INET_INGRESS:
1866 case BPF_CGROUP_INET_EGRESS:
b2cd1257 1867 ptype = BPF_PROG_TYPE_CGROUP_SKB;
f4324551 1868 break;
61023658 1869 case BPF_CGROUP_INET_SOCK_CREATE:
aac3fc32
AI
1870 case BPF_CGROUP_INET4_POST_BIND:
1871 case BPF_CGROUP_INET6_POST_BIND:
61023658
DA
1872 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1873 break;
4fbac77d
AI
1874 case BPF_CGROUP_INET4_BIND:
1875 case BPF_CGROUP_INET6_BIND:
d74bad4e
AI
1876 case BPF_CGROUP_INET4_CONNECT:
1877 case BPF_CGROUP_INET6_CONNECT:
1cedee13
AI
1878 case BPF_CGROUP_UDP4_SENDMSG:
1879 case BPF_CGROUP_UDP6_SENDMSG:
4fbac77d
AI
1880 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1881 break;
40304b2a
LB
1882 case BPF_CGROUP_SOCK_OPS:
1883 ptype = BPF_PROG_TYPE_SOCK_OPS;
1884 break;
ebc614f6
RG
1885 case BPF_CGROUP_DEVICE:
1886 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1887 break;
4f738adb 1888 case BPF_SK_MSG_VERDICT:
fdb5c453
SY
1889 ptype = BPF_PROG_TYPE_SK_MSG;
1890 break;
464bc0fd
JF
1891 case BPF_SK_SKB_STREAM_PARSER:
1892 case BPF_SK_SKB_STREAM_VERDICT:
fdb5c453
SY
1893 ptype = BPF_PROG_TYPE_SK_SKB;
1894 break;
f4364dcf 1895 case BPF_LIRC_MODE2:
fdb5c453
SY
1896 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1897 break;
d58e468b
PP
1898 case BPF_FLOW_DISSECTOR:
1899 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1900 break;
7b146ceb
AI
1901 case BPF_CGROUP_SYSCTL:
1902 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1903 break;
f4324551
DM
1904 default:
1905 return -EINVAL;
1906 }
1907
b2cd1257
DA
1908 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1909 if (IS_ERR(prog))
1910 return PTR_ERR(prog);
1911
5e43f899
AI
1912 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1913 bpf_prog_put(prog);
1914 return -EINVAL;
1915 }
1916
fdb5c453
SY
1917 switch (ptype) {
1918 case BPF_PROG_TYPE_SK_SKB:
1919 case BPF_PROG_TYPE_SK_MSG:
604326b4 1920 ret = sock_map_get_from_fd(attr, prog);
fdb5c453
SY
1921 break;
1922 case BPF_PROG_TYPE_LIRC_MODE2:
1923 ret = lirc_prog_attach(attr, prog);
1924 break;
d58e468b
PP
1925 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1926 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1927 break;
fdb5c453
SY
1928 default:
1929 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
b2cd1257
DA
1930 }
1931
7f677633
AS
1932 if (ret)
1933 bpf_prog_put(prog);
7f677633 1934 return ret;
f4324551
DM
1935}
1936
1937#define BPF_PROG_DETACH_LAST_FIELD attach_type
1938
1939static int bpf_prog_detach(const union bpf_attr *attr)
1940{
324bda9e 1941 enum bpf_prog_type ptype;
f4324551
DM
1942
1943 if (!capable(CAP_NET_ADMIN))
1944 return -EPERM;
1945
1946 if (CHECK_ATTR(BPF_PROG_DETACH))
1947 return -EINVAL;
1948
1949 switch (attr->attach_type) {
1950 case BPF_CGROUP_INET_INGRESS:
1951 case BPF_CGROUP_INET_EGRESS:
324bda9e
AS
1952 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1953 break;
61023658 1954 case BPF_CGROUP_INET_SOCK_CREATE:
aac3fc32
AI
1955 case BPF_CGROUP_INET4_POST_BIND:
1956 case BPF_CGROUP_INET6_POST_BIND:
324bda9e
AS
1957 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1958 break;
4fbac77d
AI
1959 case BPF_CGROUP_INET4_BIND:
1960 case BPF_CGROUP_INET6_BIND:
d74bad4e
AI
1961 case BPF_CGROUP_INET4_CONNECT:
1962 case BPF_CGROUP_INET6_CONNECT:
1cedee13
AI
1963 case BPF_CGROUP_UDP4_SENDMSG:
1964 case BPF_CGROUP_UDP6_SENDMSG:
4fbac77d
AI
1965 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1966 break;
40304b2a 1967 case BPF_CGROUP_SOCK_OPS:
324bda9e 1968 ptype = BPF_PROG_TYPE_SOCK_OPS;
f4324551 1969 break;
ebc614f6
RG
1970 case BPF_CGROUP_DEVICE:
1971 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1972 break;
4f738adb 1973 case BPF_SK_MSG_VERDICT:
604326b4 1974 return sock_map_get_from_fd(attr, NULL);
5a67da2a
JF
1975 case BPF_SK_SKB_STREAM_PARSER:
1976 case BPF_SK_SKB_STREAM_VERDICT:
604326b4 1977 return sock_map_get_from_fd(attr, NULL);
f4364dcf
SY
1978 case BPF_LIRC_MODE2:
1979 return lirc_prog_detach(attr);
d58e468b
PP
1980 case BPF_FLOW_DISSECTOR:
1981 return skb_flow_dissector_bpf_prog_detach(attr);
7b146ceb
AI
1982 case BPF_CGROUP_SYSCTL:
1983 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1984 break;
f4324551
DM
1985 default:
1986 return -EINVAL;
1987 }
1988
fdb5c453 1989 return cgroup_bpf_prog_detach(attr, ptype);
f4324551 1990}
40304b2a 1991
468e2f64
AS
1992#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1993
1994static int bpf_prog_query(const union bpf_attr *attr,
1995 union bpf_attr __user *uattr)
1996{
468e2f64
AS
1997 if (!capable(CAP_NET_ADMIN))
1998 return -EPERM;
1999 if (CHECK_ATTR(BPF_PROG_QUERY))
2000 return -EINVAL;
2001 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2002 return -EINVAL;
2003
2004 switch (attr->query.attach_type) {
2005 case BPF_CGROUP_INET_INGRESS:
2006 case BPF_CGROUP_INET_EGRESS:
2007 case BPF_CGROUP_INET_SOCK_CREATE:
4fbac77d
AI
2008 case BPF_CGROUP_INET4_BIND:
2009 case BPF_CGROUP_INET6_BIND:
aac3fc32
AI
2010 case BPF_CGROUP_INET4_POST_BIND:
2011 case BPF_CGROUP_INET6_POST_BIND:
d74bad4e
AI
2012 case BPF_CGROUP_INET4_CONNECT:
2013 case BPF_CGROUP_INET6_CONNECT:
1cedee13
AI
2014 case BPF_CGROUP_UDP4_SENDMSG:
2015 case BPF_CGROUP_UDP6_SENDMSG:
468e2f64 2016 case BPF_CGROUP_SOCK_OPS:
ebc614f6 2017 case BPF_CGROUP_DEVICE:
7b146ceb 2018 case BPF_CGROUP_SYSCTL:
468e2f64 2019 break;
f4364dcf
SY
2020 case BPF_LIRC_MODE2:
2021 return lirc_prog_query(attr, uattr);
118c8e9a
SF
2022 case BPF_FLOW_DISSECTOR:
2023 return skb_flow_dissector_prog_query(attr, uattr);
468e2f64
AS
2024 default:
2025 return -EINVAL;
2026 }
fdb5c453
SY
2027
2028 return cgroup_bpf_prog_query(attr, uattr);
468e2f64 2029}
f4324551 2030
b0b9395d 2031#define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
1cf1cae9
AS
2032
2033static int bpf_prog_test_run(const union bpf_attr *attr,
2034 union bpf_attr __user *uattr)
2035{
2036 struct bpf_prog *prog;
2037 int ret = -ENOTSUPP;
2038
61f3c964
AS
2039 if (!capable(CAP_SYS_ADMIN))
2040 return -EPERM;
1cf1cae9
AS
2041 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2042 return -EINVAL;
2043
b0b9395d
SF
2044 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2045 (!attr->test.ctx_size_in && attr->test.ctx_in))
2046 return -EINVAL;
2047
2048 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2049 (!attr->test.ctx_size_out && attr->test.ctx_out))
2050 return -EINVAL;
2051
1cf1cae9
AS
2052 prog = bpf_prog_get(attr->test.prog_fd);
2053 if (IS_ERR(prog))
2054 return PTR_ERR(prog);
2055
2056 if (prog->aux->ops->test_run)
2057 ret = prog->aux->ops->test_run(prog, attr, uattr);
2058
2059 bpf_prog_put(prog);
2060 return ret;
2061}
2062
34ad5580
MKL
2063#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2064
2065static int bpf_obj_get_next_id(const union bpf_attr *attr,
2066 union bpf_attr __user *uattr,
2067 struct idr *idr,
2068 spinlock_t *lock)
2069{
2070 u32 next_id = attr->start_id;
2071 int err = 0;
2072
2073 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2074 return -EINVAL;
2075
2076 if (!capable(CAP_SYS_ADMIN))
2077 return -EPERM;
2078
2079 next_id++;
2080 spin_lock_bh(lock);
2081 if (!idr_get_next(idr, &next_id))
2082 err = -ENOENT;
2083 spin_unlock_bh(lock);
2084
2085 if (!err)
2086 err = put_user(next_id, &uattr->next_id);
2087
2088 return err;
2089}
2090
b16d9aa4
MKL
2091#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2092
2093static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2094{
2095 struct bpf_prog *prog;
2096 u32 id = attr->prog_id;
2097 int fd;
2098
2099 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2100 return -EINVAL;
2101
2102 if (!capable(CAP_SYS_ADMIN))
2103 return -EPERM;
2104
2105 spin_lock_bh(&prog_idr_lock);
2106 prog = idr_find(&prog_idr, id);
2107 if (prog)
2108 prog = bpf_prog_inc_not_zero(prog);
2109 else
2110 prog = ERR_PTR(-ENOENT);
2111 spin_unlock_bh(&prog_idr_lock);
2112
2113 if (IS_ERR(prog))
2114 return PTR_ERR(prog);
2115
2116 fd = bpf_prog_new_fd(prog);
2117 if (fd < 0)
2118 bpf_prog_put(prog);
2119
2120 return fd;
2121}
2122
6e71b04a 2123#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
bd5f5f4e
MKL
2124
2125static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2126{
2127 struct bpf_map *map;
2128 u32 id = attr->map_id;
6e71b04a 2129 int f_flags;
bd5f5f4e
MKL
2130 int fd;
2131
6e71b04a
CF
2132 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2133 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
bd5f5f4e
MKL
2134 return -EINVAL;
2135
2136 if (!capable(CAP_SYS_ADMIN))
2137 return -EPERM;
2138
6e71b04a
CF
2139 f_flags = bpf_get_file_flag(attr->open_flags);
2140 if (f_flags < 0)
2141 return f_flags;
2142
bd5f5f4e
MKL
2143 spin_lock_bh(&map_idr_lock);
2144 map = idr_find(&map_idr, id);
2145 if (map)
2146 map = bpf_map_inc_not_zero(map, true);
2147 else
2148 map = ERR_PTR(-ENOENT);
2149 spin_unlock_bh(&map_idr_lock);
2150
2151 if (IS_ERR(map))
2152 return PTR_ERR(map);
2153
6e71b04a 2154 fd = bpf_map_new_fd(map, f_flags);
bd5f5f4e 2155 if (fd < 0)
781e6282 2156 bpf_map_put_with_uref(map);
bd5f5f4e
MKL
2157
2158 return fd;
2159}
2160
7105e828 2161static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
d8eca5bb
DB
2162 unsigned long addr, u32 *off,
2163 u32 *type)
7105e828 2164{
d8eca5bb 2165 const struct bpf_map *map;
7105e828
DB
2166 int i;
2167
d8eca5bb
DB
2168 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2169 map = prog->aux->used_maps[i];
2170 if (map == (void *)addr) {
2171 *type = BPF_PSEUDO_MAP_FD;
2172 return map;
2173 }
2174 if (!map->ops->map_direct_value_meta)
2175 continue;
2176 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2177 *type = BPF_PSEUDO_MAP_VALUE;
2178 return map;
2179 }
2180 }
2181
7105e828
DB
2182 return NULL;
2183}
2184
2185static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2186{
2187 const struct bpf_map *map;
2188 struct bpf_insn *insns;
d8eca5bb 2189 u32 off, type;
7105e828
DB
2190 u64 imm;
2191 int i;
2192
2193 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2194 GFP_USER);
2195 if (!insns)
2196 return insns;
2197
2198 for (i = 0; i < prog->len; i++) {
2199 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2200 insns[i].code = BPF_JMP | BPF_CALL;
2201 insns[i].imm = BPF_FUNC_tail_call;
2202 /* fall-through */
2203 }
2204 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2205 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2206 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2207 insns[i].code = BPF_JMP | BPF_CALL;
2208 if (!bpf_dump_raw_ok())
2209 insns[i].imm = 0;
2210 continue;
2211 }
2212
2213 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2214 continue;
2215
2216 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
d8eca5bb 2217 map = bpf_map_from_imm(prog, imm, &off, &type);
7105e828 2218 if (map) {
d8eca5bb 2219 insns[i].src_reg = type;
7105e828 2220 insns[i].imm = map->id;
d8eca5bb 2221 insns[i + 1].imm = off;
7105e828
DB
2222 continue;
2223 }
7105e828
DB
2224 }
2225
2226 return insns;
2227}
2228
c454a46b
MKL
2229static int set_info_rec_size(struct bpf_prog_info *info)
2230{
2231 /*
2232 * Ensure info.*_rec_size is the same as kernel expected size
2233 *
2234 * or
2235 *
2236 * Only allow zero *_rec_size if both _rec_size and _cnt are
2237 * zero. In this case, the kernel will set the expected
2238 * _rec_size back to the info.
2239 */
2240
11d8b82d 2241 if ((info->nr_func_info || info->func_info_rec_size) &&
c454a46b
MKL
2242 info->func_info_rec_size != sizeof(struct bpf_func_info))
2243 return -EINVAL;
2244
11d8b82d 2245 if ((info->nr_line_info || info->line_info_rec_size) &&
c454a46b
MKL
2246 info->line_info_rec_size != sizeof(struct bpf_line_info))
2247 return -EINVAL;
2248
11d8b82d 2249 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
c454a46b
MKL
2250 info->jited_line_info_rec_size != sizeof(__u64))
2251 return -EINVAL;
2252
2253 info->func_info_rec_size = sizeof(struct bpf_func_info);
2254 info->line_info_rec_size = sizeof(struct bpf_line_info);
2255 info->jited_line_info_rec_size = sizeof(__u64);
2256
2257 return 0;
2258}
2259
1e270976
MKL
2260static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2261 const union bpf_attr *attr,
2262 union bpf_attr __user *uattr)
2263{
2264 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2265 struct bpf_prog_info info = {};
2266 u32 info_len = attr->info.info_len;
5f8f8b93 2267 struct bpf_prog_stats stats;
1e270976
MKL
2268 char __user *uinsns;
2269 u32 ulen;
2270 int err;
2271
dcab51f1 2272 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1e270976
MKL
2273 if (err)
2274 return err;
2275 info_len = min_t(u32, sizeof(info), info_len);
2276
2277 if (copy_from_user(&info, uinfo, info_len))
89b09689 2278 return -EFAULT;
1e270976
MKL
2279
2280 info.type = prog->type;
2281 info.id = prog->aux->id;
cb4d2b3f
MKL
2282 info.load_time = prog->aux->load_time;
2283 info.created_by_uid = from_kuid_munged(current_user_ns(),
2284 prog->aux->user->uid);
b85fab0e 2285 info.gpl_compatible = prog->gpl_compatible;
1e270976
MKL
2286
2287 memcpy(info.tag, prog->tag, sizeof(prog->tag));
cb4d2b3f
MKL
2288 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2289
2290 ulen = info.nr_map_ids;
2291 info.nr_map_ids = prog->aux->used_map_cnt;
2292 ulen = min_t(u32, info.nr_map_ids, ulen);
2293 if (ulen) {
721e08da 2294 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
cb4d2b3f
MKL
2295 u32 i;
2296
2297 for (i = 0; i < ulen; i++)
2298 if (put_user(prog->aux->used_maps[i]->id,
2299 &user_map_ids[i]))
2300 return -EFAULT;
2301 }
1e270976 2302
c454a46b
MKL
2303 err = set_info_rec_size(&info);
2304 if (err)
2305 return err;
7337224f 2306
5f8f8b93
AS
2307 bpf_prog_get_stats(prog, &stats);
2308 info.run_time_ns = stats.nsecs;
2309 info.run_cnt = stats.cnt;
2310
1e270976
MKL
2311 if (!capable(CAP_SYS_ADMIN)) {
2312 info.jited_prog_len = 0;
2313 info.xlated_prog_len = 0;
dbecd738 2314 info.nr_jited_ksyms = 0;
28c2fae7 2315 info.nr_jited_func_lens = 0;
11d8b82d
YS
2316 info.nr_func_info = 0;
2317 info.nr_line_info = 0;
2318 info.nr_jited_line_info = 0;
1e270976
MKL
2319 goto done;
2320 }
2321
1e270976 2322 ulen = info.xlated_prog_len;
9975a54b 2323 info.xlated_prog_len = bpf_prog_insn_size(prog);
1e270976 2324 if (info.xlated_prog_len && ulen) {
7105e828
DB
2325 struct bpf_insn *insns_sanitized;
2326 bool fault;
2327
2328 if (prog->blinded && !bpf_dump_raw_ok()) {
2329 info.xlated_prog_insns = 0;
2330 goto done;
2331 }
2332 insns_sanitized = bpf_insn_prepare_dump(prog);
2333 if (!insns_sanitized)
2334 return -ENOMEM;
1e270976
MKL
2335 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2336 ulen = min_t(u32, info.xlated_prog_len, ulen);
7105e828
DB
2337 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2338 kfree(insns_sanitized);
2339 if (fault)
1e270976
MKL
2340 return -EFAULT;
2341 }
2342
675fc275
JK
2343 if (bpf_prog_is_dev_bound(prog->aux)) {
2344 err = bpf_prog_offload_info_fill(&info, prog);
2345 if (err)
2346 return err;
fcfb126d
JW
2347 goto done;
2348 }
2349
2350 /* NOTE: the following code is supposed to be skipped for offload.
2351 * bpf_prog_offload_info_fill() is the place to fill similar fields
2352 * for offload.
2353 */
2354 ulen = info.jited_prog_len;
4d56a76e
SD
2355 if (prog->aux->func_cnt) {
2356 u32 i;
2357
2358 info.jited_prog_len = 0;
2359 for (i = 0; i < prog->aux->func_cnt; i++)
2360 info.jited_prog_len += prog->aux->func[i]->jited_len;
2361 } else {
2362 info.jited_prog_len = prog->jited_len;
2363 }
2364
fcfb126d
JW
2365 if (info.jited_prog_len && ulen) {
2366 if (bpf_dump_raw_ok()) {
2367 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2368 ulen = min_t(u32, info.jited_prog_len, ulen);
4d56a76e
SD
2369
2370 /* for multi-function programs, copy the JITed
2371 * instructions for all the functions
2372 */
2373 if (prog->aux->func_cnt) {
2374 u32 len, free, i;
2375 u8 *img;
2376
2377 free = ulen;
2378 for (i = 0; i < prog->aux->func_cnt; i++) {
2379 len = prog->aux->func[i]->jited_len;
2380 len = min_t(u32, len, free);
2381 img = (u8 *) prog->aux->func[i]->bpf_func;
2382 if (copy_to_user(uinsns, img, len))
2383 return -EFAULT;
2384 uinsns += len;
2385 free -= len;
2386 if (!free)
2387 break;
2388 }
2389 } else {
2390 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2391 return -EFAULT;
2392 }
fcfb126d
JW
2393 } else {
2394 info.jited_prog_insns = 0;
2395 }
675fc275
JK
2396 }
2397
dbecd738 2398 ulen = info.nr_jited_ksyms;
ff1889fc 2399 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
7a5725dd 2400 if (ulen) {
dbecd738 2401 if (bpf_dump_raw_ok()) {
ff1889fc 2402 unsigned long ksym_addr;
dbecd738 2403 u64 __user *user_ksyms;
dbecd738
SD
2404 u32 i;
2405
2406 /* copy the address of the kernel symbol
2407 * corresponding to each function
2408 */
2409 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2410 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
ff1889fc
SL
2411 if (prog->aux->func_cnt) {
2412 for (i = 0; i < ulen; i++) {
2413 ksym_addr = (unsigned long)
2414 prog->aux->func[i]->bpf_func;
2415 if (put_user((u64) ksym_addr,
2416 &user_ksyms[i]))
2417 return -EFAULT;
2418 }
2419 } else {
2420 ksym_addr = (unsigned long) prog->bpf_func;
2421 if (put_user((u64) ksym_addr, &user_ksyms[0]))
dbecd738
SD
2422 return -EFAULT;
2423 }
2424 } else {
2425 info.jited_ksyms = 0;
2426 }
2427 }
2428
815581c1 2429 ulen = info.nr_jited_func_lens;
ff1889fc 2430 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
7a5725dd 2431 if (ulen) {
815581c1
SD
2432 if (bpf_dump_raw_ok()) {
2433 u32 __user *user_lens;
2434 u32 func_len, i;
2435
2436 /* copy the JITed image lengths for each function */
2437 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2438 user_lens = u64_to_user_ptr(info.jited_func_lens);
ff1889fc
SL
2439 if (prog->aux->func_cnt) {
2440 for (i = 0; i < ulen; i++) {
2441 func_len =
2442 prog->aux->func[i]->jited_len;
2443 if (put_user(func_len, &user_lens[i]))
2444 return -EFAULT;
2445 }
2446 } else {
2447 func_len = prog->jited_len;
2448 if (put_user(func_len, &user_lens[0]))
815581c1
SD
2449 return -EFAULT;
2450 }
2451 } else {
2452 info.jited_func_lens = 0;
2453 }
2454 }
2455
7337224f 2456 if (prog->aux->btf)
838e9690
YS
2457 info.btf_id = btf_id(prog->aux->btf);
2458
11d8b82d
YS
2459 ulen = info.nr_func_info;
2460 info.nr_func_info = prog->aux->func_info_cnt;
2461 if (info.nr_func_info && ulen) {
9e794163 2462 char __user *user_finfo;
7337224f 2463
9e794163
MKL
2464 user_finfo = u64_to_user_ptr(info.func_info);
2465 ulen = min_t(u32, info.nr_func_info, ulen);
2466 if (copy_to_user(user_finfo, prog->aux->func_info,
2467 info.func_info_rec_size * ulen))
2468 return -EFAULT;
838e9690
YS
2469 }
2470
11d8b82d
YS
2471 ulen = info.nr_line_info;
2472 info.nr_line_info = prog->aux->nr_linfo;
2473 if (info.nr_line_info && ulen) {
9e794163 2474 __u8 __user *user_linfo;
c454a46b 2475
9e794163
MKL
2476 user_linfo = u64_to_user_ptr(info.line_info);
2477 ulen = min_t(u32, info.nr_line_info, ulen);
2478 if (copy_to_user(user_linfo, prog->aux->linfo,
2479 info.line_info_rec_size * ulen))
2480 return -EFAULT;
c454a46b
MKL
2481 }
2482
11d8b82d 2483 ulen = info.nr_jited_line_info;
c454a46b 2484 if (prog->aux->jited_linfo)
11d8b82d 2485 info.nr_jited_line_info = prog->aux->nr_linfo;
c454a46b 2486 else
11d8b82d
YS
2487 info.nr_jited_line_info = 0;
2488 if (info.nr_jited_line_info && ulen) {
c454a46b
MKL
2489 if (bpf_dump_raw_ok()) {
2490 __u64 __user *user_linfo;
2491 u32 i;
2492
2493 user_linfo = u64_to_user_ptr(info.jited_line_info);
11d8b82d 2494 ulen = min_t(u32, info.nr_jited_line_info, ulen);
c454a46b
MKL
2495 for (i = 0; i < ulen; i++) {
2496 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2497 &user_linfo[i]))
2498 return -EFAULT;
2499 }
2500 } else {
2501 info.jited_line_info = 0;
2502 }
2503 }
2504
c872bdb3
SL
2505 ulen = info.nr_prog_tags;
2506 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2507 if (ulen) {
2508 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2509 u32 i;
2510
2511 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2512 ulen = min_t(u32, info.nr_prog_tags, ulen);
2513 if (prog->aux->func_cnt) {
2514 for (i = 0; i < ulen; i++) {
2515 if (copy_to_user(user_prog_tags[i],
2516 prog->aux->func[i]->tag,
2517 BPF_TAG_SIZE))
2518 return -EFAULT;
2519 }
2520 } else {
2521 if (copy_to_user(user_prog_tags[0],
2522 prog->tag, BPF_TAG_SIZE))
2523 return -EFAULT;
2524 }
2525 }
2526
1e270976
MKL
2527done:
2528 if (copy_to_user(uinfo, &info, info_len) ||
2529 put_user(info_len, &uattr->info.info_len))
2530 return -EFAULT;
2531
2532 return 0;
2533}
2534
2535static int bpf_map_get_info_by_fd(struct bpf_map *map,
2536 const union bpf_attr *attr,
2537 union bpf_attr __user *uattr)
2538{
2539 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2540 struct bpf_map_info info = {};
2541 u32 info_len = attr->info.info_len;
2542 int err;
2543
dcab51f1 2544 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1e270976
MKL
2545 if (err)
2546 return err;
2547 info_len = min_t(u32, sizeof(info), info_len);
2548
2549 info.type = map->map_type;
2550 info.id = map->id;
2551 info.key_size = map->key_size;
2552 info.value_size = map->value_size;
2553 info.max_entries = map->max_entries;
2554 info.map_flags = map->map_flags;
ad5b177b 2555 memcpy(info.name, map->name, sizeof(map->name));
1e270976 2556
78958fca
MKL
2557 if (map->btf) {
2558 info.btf_id = btf_id(map->btf);
9b2cf328
MKL
2559 info.btf_key_type_id = map->btf_key_type_id;
2560 info.btf_value_type_id = map->btf_value_type_id;
78958fca
MKL
2561 }
2562
52775b33
JK
2563 if (bpf_map_is_dev_bound(map)) {
2564 err = bpf_map_offload_info_fill(&info, map);
2565 if (err)
2566 return err;
2567 }
2568
1e270976
MKL
2569 if (copy_to_user(uinfo, &info, info_len) ||
2570 put_user(info_len, &uattr->info.info_len))
2571 return -EFAULT;
2572
2573 return 0;
2574}
2575
62dab84c
MKL
2576static int bpf_btf_get_info_by_fd(struct btf *btf,
2577 const union bpf_attr *attr,
2578 union bpf_attr __user *uattr)
2579{
2580 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2581 u32 info_len = attr->info.info_len;
2582 int err;
2583
dcab51f1 2584 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
62dab84c
MKL
2585 if (err)
2586 return err;
2587
2588 return btf_get_info_by_fd(btf, attr, uattr);
2589}
2590
1e270976
MKL
2591#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2592
2593static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2594 union bpf_attr __user *uattr)
2595{
2596 int ufd = attr->info.bpf_fd;
2597 struct fd f;
2598 int err;
2599
2600 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2601 return -EINVAL;
2602
2603 f = fdget(ufd);
2604 if (!f.file)
2605 return -EBADFD;
2606
2607 if (f.file->f_op == &bpf_prog_fops)
2608 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2609 uattr);
2610 else if (f.file->f_op == &bpf_map_fops)
2611 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2612 uattr);
60197cfb 2613 else if (f.file->f_op == &btf_fops)
62dab84c 2614 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
1e270976
MKL
2615 else
2616 err = -EINVAL;
2617
2618 fdput(f);
2619 return err;
2620}
2621
f56a653c
MKL
2622#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2623
2624static int bpf_btf_load(const union bpf_attr *attr)
2625{
2626 if (CHECK_ATTR(BPF_BTF_LOAD))
2627 return -EINVAL;
2628
2629 if (!capable(CAP_SYS_ADMIN))
2630 return -EPERM;
2631
2632 return btf_new_fd(attr);
2633}
2634
78958fca
MKL
2635#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2636
2637static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2638{
2639 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2640 return -EINVAL;
2641
2642 if (!capable(CAP_SYS_ADMIN))
2643 return -EPERM;
2644
2645 return btf_get_fd_by_id(attr->btf_id);
2646}
2647
41bdc4b4
YS
2648static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2649 union bpf_attr __user *uattr,
2650 u32 prog_id, u32 fd_type,
2651 const char *buf, u64 probe_offset,
2652 u64 probe_addr)
2653{
2654 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2655 u32 len = buf ? strlen(buf) : 0, input_len;
2656 int err = 0;
2657
2658 if (put_user(len, &uattr->task_fd_query.buf_len))
2659 return -EFAULT;
2660 input_len = attr->task_fd_query.buf_len;
2661 if (input_len && ubuf) {
2662 if (!len) {
2663 /* nothing to copy, just make ubuf NULL terminated */
2664 char zero = '\0';
2665
2666 if (put_user(zero, ubuf))
2667 return -EFAULT;
2668 } else if (input_len >= len + 1) {
2669 /* ubuf can hold the string with NULL terminator */
2670 if (copy_to_user(ubuf, buf, len + 1))
2671 return -EFAULT;
2672 } else {
2673 /* ubuf cannot hold the string with NULL terminator,
2674 * do a partial copy with NULL terminator.
2675 */
2676 char zero = '\0';
2677
2678 err = -ENOSPC;
2679 if (copy_to_user(ubuf, buf, input_len - 1))
2680 return -EFAULT;
2681 if (put_user(zero, ubuf + input_len - 1))
2682 return -EFAULT;
2683 }
2684 }
2685
2686 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2687 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2688 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2689 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2690 return -EFAULT;
2691
2692 return err;
2693}
2694
2695#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2696
2697static int bpf_task_fd_query(const union bpf_attr *attr,
2698 union bpf_attr __user *uattr)
2699{
2700 pid_t pid = attr->task_fd_query.pid;
2701 u32 fd = attr->task_fd_query.fd;
2702 const struct perf_event *event;
2703 struct files_struct *files;
2704 struct task_struct *task;
2705 struct file *file;
2706 int err;
2707
2708 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2709 return -EINVAL;
2710
2711 if (!capable(CAP_SYS_ADMIN))
2712 return -EPERM;
2713
2714 if (attr->task_fd_query.flags != 0)
2715 return -EINVAL;
2716
2717 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2718 if (!task)
2719 return -ENOENT;
2720
2721 files = get_files_struct(task);
2722 put_task_struct(task);
2723 if (!files)
2724 return -ENOENT;
2725
2726 err = 0;
2727 spin_lock(&files->file_lock);
2728 file = fcheck_files(files, fd);
2729 if (!file)
2730 err = -EBADF;
2731 else
2732 get_file(file);
2733 spin_unlock(&files->file_lock);
2734 put_files_struct(files);
2735
2736 if (err)
2737 goto out;
2738
2739 if (file->f_op == &bpf_raw_tp_fops) {
2740 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2741 struct bpf_raw_event_map *btp = raw_tp->btp;
2742
2743 err = bpf_task_fd_query_copy(attr, uattr,
2744 raw_tp->prog->aux->id,
2745 BPF_FD_TYPE_RAW_TRACEPOINT,
2746 btp->tp->name, 0, 0);
2747 goto put_file;
2748 }
2749
2750 event = perf_get_event(file);
2751 if (!IS_ERR(event)) {
2752 u64 probe_offset, probe_addr;
2753 u32 prog_id, fd_type;
2754 const char *buf;
2755
2756 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2757 &buf, &probe_offset,
2758 &probe_addr);
2759 if (!err)
2760 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2761 fd_type, buf,
2762 probe_offset,
2763 probe_addr);
2764 goto put_file;
2765 }
2766
2767 err = -ENOTSUPP;
2768put_file:
2769 fput(file);
2770out:
2771 return err;
2772}
2773
99c55f7d
AS
2774SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2775{
2776 union bpf_attr attr = {};
2777 int err;
2778
0fa4fe85 2779 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
99c55f7d
AS
2780 return -EPERM;
2781
dcab51f1 2782 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
1e270976
MKL
2783 if (err)
2784 return err;
2785 size = min_t(u32, size, sizeof(attr));
99c55f7d
AS
2786
2787 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2788 if (copy_from_user(&attr, uattr, size) != 0)
2789 return -EFAULT;
2790
afdb09c7
CF
2791 err = security_bpf(cmd, &attr, size);
2792 if (err < 0)
2793 return err;
2794
99c55f7d
AS
2795 switch (cmd) {
2796 case BPF_MAP_CREATE:
2797 err = map_create(&attr);
2798 break;
db20fd2b
AS
2799 case BPF_MAP_LOOKUP_ELEM:
2800 err = map_lookup_elem(&attr);
2801 break;
2802 case BPF_MAP_UPDATE_ELEM:
2803 err = map_update_elem(&attr);
2804 break;
2805 case BPF_MAP_DELETE_ELEM:
2806 err = map_delete_elem(&attr);
2807 break;
2808 case BPF_MAP_GET_NEXT_KEY:
2809 err = map_get_next_key(&attr);
2810 break;
87df15de
DB
2811 case BPF_MAP_FREEZE:
2812 err = map_freeze(&attr);
2813 break;
09756af4 2814 case BPF_PROG_LOAD:
838e9690 2815 err = bpf_prog_load(&attr, uattr);
09756af4 2816 break;
b2197755
DB
2817 case BPF_OBJ_PIN:
2818 err = bpf_obj_pin(&attr);
2819 break;
2820 case BPF_OBJ_GET:
2821 err = bpf_obj_get(&attr);
2822 break;
f4324551
DM
2823 case BPF_PROG_ATTACH:
2824 err = bpf_prog_attach(&attr);
2825 break;
2826 case BPF_PROG_DETACH:
2827 err = bpf_prog_detach(&attr);
2828 break;
468e2f64
AS
2829 case BPF_PROG_QUERY:
2830 err = bpf_prog_query(&attr, uattr);
2831 break;
1cf1cae9
AS
2832 case BPF_PROG_TEST_RUN:
2833 err = bpf_prog_test_run(&attr, uattr);
2834 break;
34ad5580
MKL
2835 case BPF_PROG_GET_NEXT_ID:
2836 err = bpf_obj_get_next_id(&attr, uattr,
2837 &prog_idr, &prog_idr_lock);
2838 break;
2839 case BPF_MAP_GET_NEXT_ID:
2840 err = bpf_obj_get_next_id(&attr, uattr,
2841 &map_idr, &map_idr_lock);
2842 break;
b16d9aa4
MKL
2843 case BPF_PROG_GET_FD_BY_ID:
2844 err = bpf_prog_get_fd_by_id(&attr);
2845 break;
bd5f5f4e
MKL
2846 case BPF_MAP_GET_FD_BY_ID:
2847 err = bpf_map_get_fd_by_id(&attr);
2848 break;
1e270976
MKL
2849 case BPF_OBJ_GET_INFO_BY_FD:
2850 err = bpf_obj_get_info_by_fd(&attr, uattr);
2851 break;
c4f6699d
AS
2852 case BPF_RAW_TRACEPOINT_OPEN:
2853 err = bpf_raw_tracepoint_open(&attr);
2854 break;
f56a653c
MKL
2855 case BPF_BTF_LOAD:
2856 err = bpf_btf_load(&attr);
2857 break;
78958fca
MKL
2858 case BPF_BTF_GET_FD_BY_ID:
2859 err = bpf_btf_get_fd_by_id(&attr);
2860 break;
41bdc4b4
YS
2861 case BPF_TASK_FD_QUERY:
2862 err = bpf_task_fd_query(&attr, uattr);
2863 break;
bd513cd0
MV
2864 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2865 err = map_lookup_and_delete_elem(&attr);
2866 break;
99c55f7d
AS
2867 default:
2868 err = -EINVAL;
2869 break;
2870 }
2871
2872 return err;
2873}