Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 */
4#include <linux/bpf.h>
5#include <linux/bpf_trace.h>
6#include <linux/bpf_lirc.h>
7#include <linux/btf.h>
8#include <linux/syscalls.h>
9#include <linux/slab.h>
10#include <linux/sched/signal.h>
11#include <linux/vmalloc.h>
12#include <linux/mmzone.h>
13#include <linux/anon_inodes.h>
14#include <linux/fdtable.h>
15#include <linux/file.h>
16#include <linux/fs.h>
17#include <linux/license.h>
18#include <linux/filter.h>
19#include <linux/version.h>
20#include <linux/kernel.h>
21#include <linux/idr.h>
22#include <linux/cred.h>
23#include <linux/timekeeping.h>
24#include <linux/ctype.h>
25#include <linux/nospec.h>
26
27#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
28 (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
29 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
31#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
32#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
33
34#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
35
36DEFINE_PER_CPU(int, bpf_prog_active);
37static DEFINE_IDR(prog_idr);
38static DEFINE_SPINLOCK(prog_idr_lock);
39static DEFINE_IDR(map_idr);
40static DEFINE_SPINLOCK(map_idr_lock);
41
42int sysctl_unprivileged_bpf_disabled __read_mostly;
43
44static const struct bpf_map_ops * const bpf_map_types[] = {
45#define BPF_PROG_TYPE(_id, _ops)
46#define BPF_MAP_TYPE(_id, _ops) \
47 [_id] = &_ops,
48#include <linux/bpf_types.h>
49#undef BPF_PROG_TYPE
50#undef BPF_MAP_TYPE
51};
52
53/*
54 * If we're handed a bigger struct than we know of, ensure all the unknown bits
55 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
56 * we don't know about yet.
57 *
58 * There is a ToCToU between this function call and the following
59 * copy_from_user() call. However, this is not a concern since this function is
60 * meant to be a future-proofing of bits.
61 */
62int bpf_check_uarg_tail_zero(void __user *uaddr,
63 size_t expected_size,
64 size_t actual_size)
65{
66 unsigned char __user *addr;
67 unsigned char __user *end;
68 unsigned char val;
69 int err;
70
71 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
72 return -E2BIG;
73
74 if (unlikely(!access_ok(uaddr, actual_size)))
75 return -EFAULT;
76
77 if (actual_size <= expected_size)
78 return 0;
79
80 addr = uaddr + expected_size;
81 end = uaddr + actual_size;
82
83 for (; addr < end; addr++) {
84 err = get_user(val, addr);
85 if (err)
86 return err;
87 if (val)
88 return -E2BIG;
89 }
90
91 return 0;
92}
93
94const struct bpf_map_ops bpf_map_offload_ops = {
95 .map_alloc = bpf_map_offload_map_alloc,
96 .map_free = bpf_map_offload_map_free,
97 .map_check_btf = map_check_no_btf,
98};
99
100static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
101{
102 const struct bpf_map_ops *ops;
103 u32 type = attr->map_type;
104 struct bpf_map *map;
105 int err;
106
107 if (type >= ARRAY_SIZE(bpf_map_types))
108 return ERR_PTR(-EINVAL);
109 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
110 ops = bpf_map_types[type];
111 if (!ops)
112 return ERR_PTR(-EINVAL);
113
114 if (ops->map_alloc_check) {
115 err = ops->map_alloc_check(attr);
116 if (err)
117 return ERR_PTR(err);
118 }
119 if (attr->map_ifindex)
120 ops = &bpf_map_offload_ops;
121 map = ops->map_alloc(attr);
122 if (IS_ERR(map))
123 return map;
124 map->ops = ops;
125 map->map_type = type;
126 return map;
127}
128
129void *bpf_map_area_alloc(u64 size, int numa_node)
130{
131 /* We really just want to fail instead of triggering OOM killer
132 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
133 * which is used for lower order allocation requests.
134 *
135 * It has been observed that higher order allocation requests done by
136 * vmalloc with __GFP_NORETRY being set might fail due to not trying
137 * to reclaim memory from the page cache, thus we set
138 * __GFP_RETRY_MAYFAIL to avoid such situations.
139 */
140
141 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
142 void *area;
143
144 if (size >= SIZE_MAX)
145 return NULL;
146
147 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
148 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
149 numa_node);
150 if (area != NULL)
151 return area;
152 }
153
154 return __vmalloc_node_flags_caller(size, numa_node,
155 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
156 flags, __builtin_return_address(0));
157}
158
159void bpf_map_area_free(void *area)
160{
161 kvfree(area);
162}
163
164static u32 bpf_map_flags_retain_permanent(u32 flags)
165{
166 /* Some map creation flags are not tied to the map object but
167 * rather to the map fd instead, so they have no meaning upon
168 * map object inspection since multiple file descriptors with
169 * different (access) properties can exist here. Thus, given
170 * this has zero meaning for the map itself, lets clear these
171 * from here.
172 */
173 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
174}
175
176void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
177{
178 map->map_type = attr->map_type;
179 map->key_size = attr->key_size;
180 map->value_size = attr->value_size;
181 map->max_entries = attr->max_entries;
182 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
183 map->numa_node = bpf_map_attr_numa_node(attr);
184}
185
186static int bpf_charge_memlock(struct user_struct *user, u32 pages)
187{
188 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
189
190 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
191 atomic_long_sub(pages, &user->locked_vm);
192 return -EPERM;
193 }
194 return 0;
195}
196
197static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
198{
199 if (user)
200 atomic_long_sub(pages, &user->locked_vm);
201}
202
203int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
204{
205 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
206 struct user_struct *user;
207 int ret;
208
209 if (size >= U32_MAX - PAGE_SIZE)
210 return -E2BIG;
211
212 user = get_current_user();
213 ret = bpf_charge_memlock(user, pages);
214 if (ret) {
215 free_uid(user);
216 return ret;
217 }
218
219 mem->pages = pages;
220 mem->user = user;
221
222 return 0;
223}
224
225void bpf_map_charge_finish(struct bpf_map_memory *mem)
226{
227 bpf_uncharge_memlock(mem->user, mem->pages);
228 free_uid(mem->user);
229}
230
231void bpf_map_charge_move(struct bpf_map_memory *dst,
232 struct bpf_map_memory *src)
233{
234 *dst = *src;
235
236 /* Make sure src will not be used for the redundant uncharging. */
237 memset(src, 0, sizeof(struct bpf_map_memory));
238}
239
240int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
241{
242 int ret;
243
244 ret = bpf_charge_memlock(map->memory.user, pages);
245 if (ret)
246 return ret;
247 map->memory.pages += pages;
248 return ret;
249}
250
251void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
252{
253 bpf_uncharge_memlock(map->memory.user, pages);
254 map->memory.pages -= pages;
255}
256
257static int bpf_map_alloc_id(struct bpf_map *map)
258{
259 int id;
260
261 idr_preload(GFP_KERNEL);
262 spin_lock_bh(&map_idr_lock);
263 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
264 if (id > 0)
265 map->id = id;
266 spin_unlock_bh(&map_idr_lock);
267 idr_preload_end();
268
269 if (WARN_ON_ONCE(!id))
270 return -ENOSPC;
271
272 return id > 0 ? 0 : id;
273}
274
275void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
276{
277 unsigned long flags;
278
279 /* Offloaded maps are removed from the IDR store when their device
280 * disappears - even if someone holds an fd to them they are unusable,
281 * the memory is gone, all ops will fail; they are simply waiting for
282 * refcnt to drop to be freed.
283 */
284 if (!map->id)
285 return;
286
287 if (do_idr_lock)
288 spin_lock_irqsave(&map_idr_lock, flags);
289 else
290 __acquire(&map_idr_lock);
291
292 idr_remove(&map_idr, map->id);
293 map->id = 0;
294
295 if (do_idr_lock)
296 spin_unlock_irqrestore(&map_idr_lock, flags);
297 else
298 __release(&map_idr_lock);
299}
300
301/* called from workqueue */
302static void bpf_map_free_deferred(struct work_struct *work)
303{
304 struct bpf_map *map = container_of(work, struct bpf_map, work);
305 struct bpf_map_memory mem;
306
307 bpf_map_charge_move(&mem, &map->memory);
308 security_bpf_map_free(map);
309 /* implementation dependent freeing */
310 map->ops->map_free(map);
311 bpf_map_charge_finish(&mem);
312}
313
314static void bpf_map_put_uref(struct bpf_map *map)
315{
316 if (atomic_dec_and_test(&map->usercnt)) {
317 if (map->ops->map_release_uref)
318 map->ops->map_release_uref(map);
319 }
320}
321
322/* decrement map refcnt and schedule it for freeing via workqueue
323 * (unrelying map implementation ops->map_free() might sleep)
324 */
325static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
326{
327 if (atomic_dec_and_test(&map->refcnt)) {
328 /* bpf_map_free_id() must be called first */
329 bpf_map_free_id(map, do_idr_lock);
330 btf_put(map->btf);
331 INIT_WORK(&map->work, bpf_map_free_deferred);
332 schedule_work(&map->work);
333 }
334}
335
336void bpf_map_put(struct bpf_map *map)
337{
338 __bpf_map_put(map, true);
339}
340EXPORT_SYMBOL_GPL(bpf_map_put);
341
342void bpf_map_put_with_uref(struct bpf_map *map)
343{
344 bpf_map_put_uref(map);
345 bpf_map_put(map);
346}
347
348static int bpf_map_release(struct inode *inode, struct file *filp)
349{
350 struct bpf_map *map = filp->private_data;
351
352 if (map->ops->map_release)
353 map->ops->map_release(map, filp);
354
355 bpf_map_put_with_uref(map);
356 return 0;
357}
358
359static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
360{
361 fmode_t mode = f.file->f_mode;
362
363 /* Our file permissions may have been overridden by global
364 * map permissions facing syscall side.
365 */
366 if (READ_ONCE(map->frozen))
367 mode &= ~FMODE_CAN_WRITE;
368 return mode;
369}
370
371#ifdef CONFIG_PROC_FS
372static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
373{
374 const struct bpf_map *map = filp->private_data;
375 const struct bpf_array *array;
376 u32 owner_prog_type = 0;
377 u32 owner_jited = 0;
378
379 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
380 array = container_of(map, struct bpf_array, map);
381 owner_prog_type = array->owner_prog_type;
382 owner_jited = array->owner_jited;
383 }
384
385 seq_printf(m,
386 "map_type:\t%u\n"
387 "key_size:\t%u\n"
388 "value_size:\t%u\n"
389 "max_entries:\t%u\n"
390 "map_flags:\t%#x\n"
391 "memlock:\t%llu\n"
392 "map_id:\t%u\n"
393 "frozen:\t%u\n",
394 map->map_type,
395 map->key_size,
396 map->value_size,
397 map->max_entries,
398 map->map_flags,
399 map->memory.pages * 1ULL << PAGE_SHIFT,
400 map->id,
401 READ_ONCE(map->frozen));
402
403 if (owner_prog_type) {
404 seq_printf(m, "owner_prog_type:\t%u\n",
405 owner_prog_type);
406 seq_printf(m, "owner_jited:\t%u\n",
407 owner_jited);
408 }
409}
410#endif
411
412static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
413 loff_t *ppos)
414{
415 /* We need this handler such that alloc_file() enables
416 * f_mode with FMODE_CAN_READ.
417 */
418 return -EINVAL;
419}
420
421static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
422 size_t siz, loff_t *ppos)
423{
424 /* We need this handler such that alloc_file() enables
425 * f_mode with FMODE_CAN_WRITE.
426 */
427 return -EINVAL;
428}
429
430const struct file_operations bpf_map_fops = {
431#ifdef CONFIG_PROC_FS
432 .show_fdinfo = bpf_map_show_fdinfo,
433#endif
434 .release = bpf_map_release,
435 .read = bpf_dummy_read,
436 .write = bpf_dummy_write,
437};
438
439int bpf_map_new_fd(struct bpf_map *map, int flags)
440{
441 int ret;
442
443 ret = security_bpf_map(map, OPEN_FMODE(flags));
444 if (ret < 0)
445 return ret;
446
447 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
448 flags | O_CLOEXEC);
449}
450
451int bpf_get_file_flag(int flags)
452{
453 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
454 return -EINVAL;
455 if (flags & BPF_F_RDONLY)
456 return O_RDONLY;
457 if (flags & BPF_F_WRONLY)
458 return O_WRONLY;
459 return O_RDWR;
460}
461
462/* helper macro to check that unused fields 'union bpf_attr' are zero */
463#define CHECK_ATTR(CMD) \
464 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
465 sizeof(attr->CMD##_LAST_FIELD), 0, \
466 sizeof(*attr) - \
467 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
468 sizeof(attr->CMD##_LAST_FIELD)) != NULL
469
470/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
471 * Return 0 on success and < 0 on error.
472 */
473static int bpf_obj_name_cpy(char *dst, const char *src)
474{
475 const char *end = src + BPF_OBJ_NAME_LEN;
476
477 memset(dst, 0, BPF_OBJ_NAME_LEN);
478 /* Copy all isalnum(), '_' and '.' chars. */
479 while (src < end && *src) {
480 if (!isalnum(*src) &&
481 *src != '_' && *src != '.')
482 return -EINVAL;
483 *dst++ = *src++;
484 }
485
486 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
487 if (src == end)
488 return -EINVAL;
489
490 return 0;
491}
492
493int map_check_no_btf(const struct bpf_map *map,
494 const struct btf *btf,
495 const struct btf_type *key_type,
496 const struct btf_type *value_type)
497{
498 return -ENOTSUPP;
499}
500
501static int map_check_btf(struct bpf_map *map, const struct btf *btf,
502 u32 btf_key_id, u32 btf_value_id)
503{
504 const struct btf_type *key_type, *value_type;
505 u32 key_size, value_size;
506 int ret = 0;
507
508 /* Some maps allow key to be unspecified. */
509 if (btf_key_id) {
510 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
511 if (!key_type || key_size != map->key_size)
512 return -EINVAL;
513 } else {
514 key_type = btf_type_by_id(btf, 0);
515 if (!map->ops->map_check_btf)
516 return -EINVAL;
517 }
518
519 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
520 if (!value_type || value_size != map->value_size)
521 return -EINVAL;
522
523 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
524
525 if (map_value_has_spin_lock(map)) {
526 if (map->map_flags & BPF_F_RDONLY_PROG)
527 return -EACCES;
528 if (map->map_type != BPF_MAP_TYPE_HASH &&
529 map->map_type != BPF_MAP_TYPE_ARRAY &&
530 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
531 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
532 return -ENOTSUPP;
533 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
534 map->value_size) {
535 WARN_ONCE(1,
536 "verifier bug spin_lock_off %d value_size %d\n",
537 map->spin_lock_off, map->value_size);
538 return -EFAULT;
539 }
540 }
541
542 if (map->ops->map_check_btf)
543 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
544
545 return ret;
546}
547
548#define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
549/* called via syscall */
550static int map_create(union bpf_attr *attr)
551{
552 int numa_node = bpf_map_attr_numa_node(attr);
553 struct bpf_map_memory mem;
554 struct bpf_map *map;
555 int f_flags;
556 int err;
557
558 err = CHECK_ATTR(BPF_MAP_CREATE);
559 if (err)
560 return -EINVAL;
561
562 f_flags = bpf_get_file_flag(attr->map_flags);
563 if (f_flags < 0)
564 return f_flags;
565
566 if (numa_node != NUMA_NO_NODE &&
567 ((unsigned int)numa_node >= nr_node_ids ||
568 !node_online(numa_node)))
569 return -EINVAL;
570
571 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
572 map = find_and_alloc_map(attr);
573 if (IS_ERR(map))
574 return PTR_ERR(map);
575
576 err = bpf_obj_name_cpy(map->name, attr->map_name);
577 if (err)
578 goto free_map;
579
580 atomic_set(&map->refcnt, 1);
581 atomic_set(&map->usercnt, 1);
582
583 if (attr->btf_key_type_id || attr->btf_value_type_id) {
584 struct btf *btf;
585
586 if (!attr->btf_value_type_id) {
587 err = -EINVAL;
588 goto free_map;
589 }
590
591 btf = btf_get_by_fd(attr->btf_fd);
592 if (IS_ERR(btf)) {
593 err = PTR_ERR(btf);
594 goto free_map;
595 }
596
597 err = map_check_btf(map, btf, attr->btf_key_type_id,
598 attr->btf_value_type_id);
599 if (err) {
600 btf_put(btf);
601 goto free_map;
602 }
603
604 map->btf = btf;
605 map->btf_key_type_id = attr->btf_key_type_id;
606 map->btf_value_type_id = attr->btf_value_type_id;
607 } else {
608 map->spin_lock_off = -EINVAL;
609 }
610
611 err = security_bpf_map_alloc(map);
612 if (err)
613 goto free_map;
614
615 err = bpf_map_alloc_id(map);
616 if (err)
617 goto free_map_sec;
618
619 err = bpf_map_new_fd(map, f_flags);
620 if (err < 0) {
621 /* failed to allocate fd.
622 * bpf_map_put_with_uref() is needed because the above
623 * bpf_map_alloc_id() has published the map
624 * to the userspace and the userspace may
625 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
626 */
627 bpf_map_put_with_uref(map);
628 return err;
629 }
630
631 return err;
632
633free_map_sec:
634 security_bpf_map_free(map);
635free_map:
636 btf_put(map->btf);
637 bpf_map_charge_move(&mem, &map->memory);
638 map->ops->map_free(map);
639 bpf_map_charge_finish(&mem);
640 return err;
641}
642
643/* if error is returned, fd is released.
644 * On success caller should complete fd access with matching fdput()
645 */
646struct bpf_map *__bpf_map_get(struct fd f)
647{
648 if (!f.file)
649 return ERR_PTR(-EBADF);
650 if (f.file->f_op != &bpf_map_fops) {
651 fdput(f);
652 return ERR_PTR(-EINVAL);
653 }
654
655 return f.file->private_data;
656}
657
658/* prog's and map's refcnt limit */
659#define BPF_MAX_REFCNT 32768
660
661struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
662{
663 if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
664 atomic_dec(&map->refcnt);
665 return ERR_PTR(-EBUSY);
666 }
667 if (uref)
668 atomic_inc(&map->usercnt);
669 return map;
670}
671EXPORT_SYMBOL_GPL(bpf_map_inc);
672
673struct bpf_map *bpf_map_get_with_uref(u32 ufd)
674{
675 struct fd f = fdget(ufd);
676 struct bpf_map *map;
677
678 map = __bpf_map_get(f);
679 if (IS_ERR(map))
680 return map;
681
682 map = bpf_map_inc(map, true);
683 fdput(f);
684
685 return map;
686}
687
688/* map_idr_lock should have been held */
689static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map,
690 bool uref)
691{
692 int refold;
693
694 refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
695
696 if (refold >= BPF_MAX_REFCNT) {
697 __bpf_map_put(map, false);
698 return ERR_PTR(-EBUSY);
699 }
700
701 if (!refold)
702 return ERR_PTR(-ENOENT);
703
704 if (uref)
705 atomic_inc(&map->usercnt);
706
707 return map;
708}
709
710struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
711{
712 spin_lock_bh(&map_idr_lock);
713 map = __bpf_map_inc_not_zero(map, uref);
714 spin_unlock_bh(&map_idr_lock);
715
716 return map;
717}
718EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
719
720int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
721{
722 return -ENOTSUPP;
723}
724
725static void *__bpf_copy_key(void __user *ukey, u64 key_size)
726{
727 if (key_size)
728 return memdup_user(ukey, key_size);
729
730 if (ukey)
731 return ERR_PTR(-EINVAL);
732
733 return NULL;
734}
735
736/* last field in 'union bpf_attr' used by this command */
737#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
738
739static int map_lookup_elem(union bpf_attr *attr)
740{
741 void __user *ukey = u64_to_user_ptr(attr->key);
742 void __user *uvalue = u64_to_user_ptr(attr->value);
743 int ufd = attr->map_fd;
744 struct bpf_map *map;
745 void *key, *value, *ptr;
746 u32 value_size;
747 struct fd f;
748 int err;
749
750 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
751 return -EINVAL;
752
753 if (attr->flags & ~BPF_F_LOCK)
754 return -EINVAL;
755
756 f = fdget(ufd);
757 map = __bpf_map_get(f);
758 if (IS_ERR(map))
759 return PTR_ERR(map);
760 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
761 err = -EPERM;
762 goto err_put;
763 }
764
765 if ((attr->flags & BPF_F_LOCK) &&
766 !map_value_has_spin_lock(map)) {
767 err = -EINVAL;
768 goto err_put;
769 }
770
771 key = __bpf_copy_key(ukey, map->key_size);
772 if (IS_ERR(key)) {
773 err = PTR_ERR(key);
774 goto err_put;
775 }
776
777 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
778 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
779 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
780 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
781 value_size = round_up(map->value_size, 8) * num_possible_cpus();
782 else if (IS_FD_MAP(map))
783 value_size = sizeof(u32);
784 else
785 value_size = map->value_size;
786
787 err = -ENOMEM;
788 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
789 if (!value)
790 goto free_key;
791
792 if (bpf_map_is_dev_bound(map)) {
793 err = bpf_map_offload_lookup_elem(map, key, value);
794 goto done;
795 }
796
797 preempt_disable();
798 this_cpu_inc(bpf_prog_active);
799 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
800 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
801 err = bpf_percpu_hash_copy(map, key, value);
802 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
803 err = bpf_percpu_array_copy(map, key, value);
804 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
805 err = bpf_percpu_cgroup_storage_copy(map, key, value);
806 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
807 err = bpf_stackmap_copy(map, key, value);
808 } else if (IS_FD_ARRAY(map)) {
809 err = bpf_fd_array_map_lookup_elem(map, key, value);
810 } else if (IS_FD_HASH(map)) {
811 err = bpf_fd_htab_map_lookup_elem(map, key, value);
812 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
813 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
814 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
815 map->map_type == BPF_MAP_TYPE_STACK) {
816 err = map->ops->map_peek_elem(map, value);
817 } else {
818 rcu_read_lock();
819 if (map->ops->map_lookup_elem_sys_only)
820 ptr = map->ops->map_lookup_elem_sys_only(map, key);
821 else
822 ptr = map->ops->map_lookup_elem(map, key);
823 if (IS_ERR(ptr)) {
824 err = PTR_ERR(ptr);
825 } else if (!ptr) {
826 err = -ENOENT;
827 } else {
828 err = 0;
829 if (attr->flags & BPF_F_LOCK)
830 /* lock 'ptr' and copy everything but lock */
831 copy_map_value_locked(map, value, ptr, true);
832 else
833 copy_map_value(map, value, ptr);
834 /* mask lock, since value wasn't zero inited */
835 check_and_init_map_lock(map, value);
836 }
837 rcu_read_unlock();
838 }
839 this_cpu_dec(bpf_prog_active);
840 preempt_enable();
841
842done:
843 if (err)
844 goto free_value;
845
846 err = -EFAULT;
847 if (copy_to_user(uvalue, value, value_size) != 0)
848 goto free_value;
849
850 err = 0;
851
852free_value:
853 kfree(value);
854free_key:
855 kfree(key);
856err_put:
857 fdput(f);
858 return err;
859}
860
861static void maybe_wait_bpf_programs(struct bpf_map *map)
862{
863 /* Wait for any running BPF programs to complete so that
864 * userspace, when we return to it, knows that all programs
865 * that could be running use the new map value.
866 */
867 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
868 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
869 synchronize_rcu();
870}
871
872#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
873
874static int map_update_elem(union bpf_attr *attr)
875{
876 void __user *ukey = u64_to_user_ptr(attr->key);
877 void __user *uvalue = u64_to_user_ptr(attr->value);
878 int ufd = attr->map_fd;
879 struct bpf_map *map;
880 void *key, *value;
881 u32 value_size;
882 struct fd f;
883 int err;
884
885 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
886 return -EINVAL;
887
888 f = fdget(ufd);
889 map = __bpf_map_get(f);
890 if (IS_ERR(map))
891 return PTR_ERR(map);
892 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
893 err = -EPERM;
894 goto err_put;
895 }
896
897 if ((attr->flags & BPF_F_LOCK) &&
898 !map_value_has_spin_lock(map)) {
899 err = -EINVAL;
900 goto err_put;
901 }
902
903 key = __bpf_copy_key(ukey, map->key_size);
904 if (IS_ERR(key)) {
905 err = PTR_ERR(key);
906 goto err_put;
907 }
908
909 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
910 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
911 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
912 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
913 value_size = round_up(map->value_size, 8) * num_possible_cpus();
914 else
915 value_size = map->value_size;
916
917 err = -ENOMEM;
918 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
919 if (!value)
920 goto free_key;
921
922 err = -EFAULT;
923 if (copy_from_user(value, uvalue, value_size) != 0)
924 goto free_value;
925
926 /* Need to create a kthread, thus must support schedule */
927 if (bpf_map_is_dev_bound(map)) {
928 err = bpf_map_offload_update_elem(map, key, value, attr->flags);
929 goto out;
930 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
931 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
932 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
933 err = map->ops->map_update_elem(map, key, value, attr->flags);
934 goto out;
935 }
936
937 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
938 * inside bpf map update or delete otherwise deadlocks are possible
939 */
940 preempt_disable();
941 __this_cpu_inc(bpf_prog_active);
942 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
943 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
944 err = bpf_percpu_hash_update(map, key, value, attr->flags);
945 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
946 err = bpf_percpu_array_update(map, key, value, attr->flags);
947 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
948 err = bpf_percpu_cgroup_storage_update(map, key, value,
949 attr->flags);
950 } else if (IS_FD_ARRAY(map)) {
951 rcu_read_lock();
952 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
953 attr->flags);
954 rcu_read_unlock();
955 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
956 rcu_read_lock();
957 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
958 attr->flags);
959 rcu_read_unlock();
960 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
961 /* rcu_read_lock() is not needed */
962 err = bpf_fd_reuseport_array_update_elem(map, key, value,
963 attr->flags);
964 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
965 map->map_type == BPF_MAP_TYPE_STACK) {
966 err = map->ops->map_push_elem(map, value, attr->flags);
967 } else {
968 rcu_read_lock();
969 err = map->ops->map_update_elem(map, key, value, attr->flags);
970 rcu_read_unlock();
971 }
972 __this_cpu_dec(bpf_prog_active);
973 preempt_enable();
974 maybe_wait_bpf_programs(map);
975out:
976free_value:
977 kfree(value);
978free_key:
979 kfree(key);
980err_put:
981 fdput(f);
982 return err;
983}
984
985#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
986
987static int map_delete_elem(union bpf_attr *attr)
988{
989 void __user *ukey = u64_to_user_ptr(attr->key);
990 int ufd = attr->map_fd;
991 struct bpf_map *map;
992 struct fd f;
993 void *key;
994 int err;
995
996 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
997 return -EINVAL;
998
999 f = fdget(ufd);
1000 map = __bpf_map_get(f);
1001 if (IS_ERR(map))
1002 return PTR_ERR(map);
1003 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1004 err = -EPERM;
1005 goto err_put;
1006 }
1007
1008 key = __bpf_copy_key(ukey, map->key_size);
1009 if (IS_ERR(key)) {
1010 err = PTR_ERR(key);
1011 goto err_put;
1012 }
1013
1014 if (bpf_map_is_dev_bound(map)) {
1015 err = bpf_map_offload_delete_elem(map, key);
1016 goto out;
1017 }
1018
1019 preempt_disable();
1020 __this_cpu_inc(bpf_prog_active);
1021 rcu_read_lock();
1022 err = map->ops->map_delete_elem(map, key);
1023 rcu_read_unlock();
1024 __this_cpu_dec(bpf_prog_active);
1025 preempt_enable();
1026 maybe_wait_bpf_programs(map);
1027out:
1028 kfree(key);
1029err_put:
1030 fdput(f);
1031 return err;
1032}
1033
1034/* last field in 'union bpf_attr' used by this command */
1035#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1036
1037static int map_get_next_key(union bpf_attr *attr)
1038{
1039 void __user *ukey = u64_to_user_ptr(attr->key);
1040 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1041 int ufd = attr->map_fd;
1042 struct bpf_map *map;
1043 void *key, *next_key;
1044 struct fd f;
1045 int err;
1046
1047 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1048 return -EINVAL;
1049
1050 f = fdget(ufd);
1051 map = __bpf_map_get(f);
1052 if (IS_ERR(map))
1053 return PTR_ERR(map);
1054 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1055 err = -EPERM;
1056 goto err_put;
1057 }
1058
1059 if (ukey) {
1060 key = __bpf_copy_key(ukey, map->key_size);
1061 if (IS_ERR(key)) {
1062 err = PTR_ERR(key);
1063 goto err_put;
1064 }
1065 } else {
1066 key = NULL;
1067 }
1068
1069 err = -ENOMEM;
1070 next_key = kmalloc(map->key_size, GFP_USER);
1071 if (!next_key)
1072 goto free_key;
1073
1074 if (bpf_map_is_dev_bound(map)) {
1075 err = bpf_map_offload_get_next_key(map, key, next_key);
1076 goto out;
1077 }
1078
1079 rcu_read_lock();
1080 err = map->ops->map_get_next_key(map, key, next_key);
1081 rcu_read_unlock();
1082out:
1083 if (err)
1084 goto free_next_key;
1085
1086 err = -EFAULT;
1087 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1088 goto free_next_key;
1089
1090 err = 0;
1091
1092free_next_key:
1093 kfree(next_key);
1094free_key:
1095 kfree(key);
1096err_put:
1097 fdput(f);
1098 return err;
1099}
1100
1101#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1102
1103static int map_lookup_and_delete_elem(union bpf_attr *attr)
1104{
1105 void __user *ukey = u64_to_user_ptr(attr->key);
1106 void __user *uvalue = u64_to_user_ptr(attr->value);
1107 int ufd = attr->map_fd;
1108 struct bpf_map *map;
1109 void *key, *value;
1110 u32 value_size;
1111 struct fd f;
1112 int err;
1113
1114 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1115 return -EINVAL;
1116
1117 f = fdget(ufd);
1118 map = __bpf_map_get(f);
1119 if (IS_ERR(map))
1120 return PTR_ERR(map);
1121 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1122 err = -EPERM;
1123 goto err_put;
1124 }
1125
1126 key = __bpf_copy_key(ukey, map->key_size);
1127 if (IS_ERR(key)) {
1128 err = PTR_ERR(key);
1129 goto err_put;
1130 }
1131
1132 value_size = map->value_size;
1133
1134 err = -ENOMEM;
1135 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1136 if (!value)
1137 goto free_key;
1138
1139 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1140 map->map_type == BPF_MAP_TYPE_STACK) {
1141 err = map->ops->map_pop_elem(map, value);
1142 } else {
1143 err = -ENOTSUPP;
1144 }
1145
1146 if (err)
1147 goto free_value;
1148
1149 if (copy_to_user(uvalue, value, value_size) != 0)
1150 goto free_value;
1151
1152 err = 0;
1153
1154free_value:
1155 kfree(value);
1156free_key:
1157 kfree(key);
1158err_put:
1159 fdput(f);
1160 return err;
1161}
1162
1163#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1164
1165static int map_freeze(const union bpf_attr *attr)
1166{
1167 int err = 0, ufd = attr->map_fd;
1168 struct bpf_map *map;
1169 struct fd f;
1170
1171 if (CHECK_ATTR(BPF_MAP_FREEZE))
1172 return -EINVAL;
1173
1174 f = fdget(ufd);
1175 map = __bpf_map_get(f);
1176 if (IS_ERR(map))
1177 return PTR_ERR(map);
1178 if (READ_ONCE(map->frozen)) {
1179 err = -EBUSY;
1180 goto err_put;
1181 }
1182 if (!capable(CAP_SYS_ADMIN)) {
1183 err = -EPERM;
1184 goto err_put;
1185 }
1186
1187 WRITE_ONCE(map->frozen, true);
1188err_put:
1189 fdput(f);
1190 return err;
1191}
1192
1193static const struct bpf_prog_ops * const bpf_prog_types[] = {
1194#define BPF_PROG_TYPE(_id, _name) \
1195 [_id] = & _name ## _prog_ops,
1196#define BPF_MAP_TYPE(_id, _ops)
1197#include <linux/bpf_types.h>
1198#undef BPF_PROG_TYPE
1199#undef BPF_MAP_TYPE
1200};
1201
1202static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1203{
1204 const struct bpf_prog_ops *ops;
1205
1206 if (type >= ARRAY_SIZE(bpf_prog_types))
1207 return -EINVAL;
1208 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1209 ops = bpf_prog_types[type];
1210 if (!ops)
1211 return -EINVAL;
1212
1213 if (!bpf_prog_is_dev_bound(prog->aux))
1214 prog->aux->ops = ops;
1215 else
1216 prog->aux->ops = &bpf_offload_prog_ops;
1217 prog->type = type;
1218 return 0;
1219}
1220
1221/* drop refcnt on maps used by eBPF program and free auxilary data */
1222static void free_used_maps(struct bpf_prog_aux *aux)
1223{
1224 enum bpf_cgroup_storage_type stype;
1225 int i;
1226
1227 for_each_cgroup_storage_type(stype) {
1228 if (!aux->cgroup_storage[stype])
1229 continue;
1230 bpf_cgroup_storage_release(aux->prog,
1231 aux->cgroup_storage[stype]);
1232 }
1233
1234 for (i = 0; i < aux->used_map_cnt; i++)
1235 bpf_map_put(aux->used_maps[i]);
1236
1237 kfree(aux->used_maps);
1238}
1239
1240int __bpf_prog_charge(struct user_struct *user, u32 pages)
1241{
1242 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1243 unsigned long user_bufs;
1244
1245 if (user) {
1246 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1247 if (user_bufs > memlock_limit) {
1248 atomic_long_sub(pages, &user->locked_vm);
1249 return -EPERM;
1250 }
1251 }
1252
1253 return 0;
1254}
1255
1256void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1257{
1258 if (user)
1259 atomic_long_sub(pages, &user->locked_vm);
1260}
1261
1262static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1263{
1264 struct user_struct *user = get_current_user();
1265 int ret;
1266
1267 ret = __bpf_prog_charge(user, prog->pages);
1268 if (ret) {
1269 free_uid(user);
1270 return ret;
1271 }
1272
1273 prog->aux->user = user;
1274 return 0;
1275}
1276
1277static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1278{
1279 struct user_struct *user = prog->aux->user;
1280
1281 __bpf_prog_uncharge(user, prog->pages);
1282 free_uid(user);
1283}
1284
1285static int bpf_prog_alloc_id(struct bpf_prog *prog)
1286{
1287 int id;
1288
1289 idr_preload(GFP_KERNEL);
1290 spin_lock_bh(&prog_idr_lock);
1291 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1292 if (id > 0)
1293 prog->aux->id = id;
1294 spin_unlock_bh(&prog_idr_lock);
1295 idr_preload_end();
1296
1297 /* id is in [1, INT_MAX) */
1298 if (WARN_ON_ONCE(!id))
1299 return -ENOSPC;
1300
1301 return id > 0 ? 0 : id;
1302}
1303
1304void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1305{
1306 /* cBPF to eBPF migrations are currently not in the idr store.
1307 * Offloaded programs are removed from the store when their device
1308 * disappears - even if someone grabs an fd to them they are unusable,
1309 * simply waiting for refcnt to drop to be freed.
1310 */
1311 if (!prog->aux->id)
1312 return;
1313
1314 if (do_idr_lock)
1315 spin_lock_bh(&prog_idr_lock);
1316 else
1317 __acquire(&prog_idr_lock);
1318
1319 idr_remove(&prog_idr, prog->aux->id);
1320 prog->aux->id = 0;
1321
1322 if (do_idr_lock)
1323 spin_unlock_bh(&prog_idr_lock);
1324 else
1325 __release(&prog_idr_lock);
1326}
1327
1328static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1329{
1330 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1331
1332 kvfree(aux->func_info);
1333 free_used_maps(aux);
1334 bpf_prog_uncharge_memlock(aux->prog);
1335 security_bpf_prog_free(aux);
1336 bpf_prog_free(aux->prog);
1337}
1338
1339static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1340{
1341 bpf_prog_kallsyms_del_all(prog);
1342 btf_put(prog->aux->btf);
1343 bpf_prog_free_linfo(prog);
1344
1345 if (deferred)
1346 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1347 else
1348 __bpf_prog_put_rcu(&prog->aux->rcu);
1349}
1350
1351static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1352{
1353 if (atomic_dec_and_test(&prog->aux->refcnt)) {
1354 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1355 /* bpf_prog_free_id() must be called first */
1356 bpf_prog_free_id(prog, do_idr_lock);
1357 __bpf_prog_put_noref(prog, true);
1358 }
1359}
1360
1361void bpf_prog_put(struct bpf_prog *prog)
1362{
1363 __bpf_prog_put(prog, true);
1364}
1365EXPORT_SYMBOL_GPL(bpf_prog_put);
1366
1367static int bpf_prog_release(struct inode *inode, struct file *filp)
1368{
1369 struct bpf_prog *prog = filp->private_data;
1370
1371 bpf_prog_put(prog);
1372 return 0;
1373}
1374
1375static void bpf_prog_get_stats(const struct bpf_prog *prog,
1376 struct bpf_prog_stats *stats)
1377{
1378 u64 nsecs = 0, cnt = 0;
1379 int cpu;
1380
1381 for_each_possible_cpu(cpu) {
1382 const struct bpf_prog_stats *st;
1383 unsigned int start;
1384 u64 tnsecs, tcnt;
1385
1386 st = per_cpu_ptr(prog->aux->stats, cpu);
1387 do {
1388 start = u64_stats_fetch_begin_irq(&st->syncp);
1389 tnsecs = st->nsecs;
1390 tcnt = st->cnt;
1391 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1392 nsecs += tnsecs;
1393 cnt += tcnt;
1394 }
1395 stats->nsecs = nsecs;
1396 stats->cnt = cnt;
1397}
1398
1399#ifdef CONFIG_PROC_FS
1400static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1401{
1402 const struct bpf_prog *prog = filp->private_data;
1403 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1404 struct bpf_prog_stats stats;
1405
1406 bpf_prog_get_stats(prog, &stats);
1407 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1408 seq_printf(m,
1409 "prog_type:\t%u\n"
1410 "prog_jited:\t%u\n"
1411 "prog_tag:\t%s\n"
1412 "memlock:\t%llu\n"
1413 "prog_id:\t%u\n"
1414 "run_time_ns:\t%llu\n"
1415 "run_cnt:\t%llu\n",
1416 prog->type,
1417 prog->jited,
1418 prog_tag,
1419 prog->pages * 1ULL << PAGE_SHIFT,
1420 prog->aux->id,
1421 stats.nsecs,
1422 stats.cnt);
1423}
1424#endif
1425
1426const struct file_operations bpf_prog_fops = {
1427#ifdef CONFIG_PROC_FS
1428 .show_fdinfo = bpf_prog_show_fdinfo,
1429#endif
1430 .release = bpf_prog_release,
1431 .read = bpf_dummy_read,
1432 .write = bpf_dummy_write,
1433};
1434
1435int bpf_prog_new_fd(struct bpf_prog *prog)
1436{
1437 int ret;
1438
1439 ret = security_bpf_prog(prog);
1440 if (ret < 0)
1441 return ret;
1442
1443 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1444 O_RDWR | O_CLOEXEC);
1445}
1446
1447static struct bpf_prog *____bpf_prog_get(struct fd f)
1448{
1449 if (!f.file)
1450 return ERR_PTR(-EBADF);
1451 if (f.file->f_op != &bpf_prog_fops) {
1452 fdput(f);
1453 return ERR_PTR(-EINVAL);
1454 }
1455
1456 return f.file->private_data;
1457}
1458
1459struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
1460{
1461 if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1462 atomic_sub(i, &prog->aux->refcnt);
1463 return ERR_PTR(-EBUSY);
1464 }
1465 return prog;
1466}
1467EXPORT_SYMBOL_GPL(bpf_prog_add);
1468
1469void bpf_prog_sub(struct bpf_prog *prog, int i)
1470{
1471 /* Only to be used for undoing previous bpf_prog_add() in some
1472 * error path. We still know that another entity in our call
1473 * path holds a reference to the program, thus atomic_sub() can
1474 * be safely used in such cases!
1475 */
1476 WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1477}
1478EXPORT_SYMBOL_GPL(bpf_prog_sub);
1479
1480struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1481{
1482 return bpf_prog_add(prog, 1);
1483}
1484EXPORT_SYMBOL_GPL(bpf_prog_inc);
1485
1486/* prog_idr_lock should have been held */
1487struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1488{
1489 int refold;
1490
1491 refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1492
1493 if (refold >= BPF_MAX_REFCNT) {
1494 __bpf_prog_put(prog, false);
1495 return ERR_PTR(-EBUSY);
1496 }
1497
1498 if (!refold)
1499 return ERR_PTR(-ENOENT);
1500
1501 return prog;
1502}
1503EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1504
1505bool bpf_prog_get_ok(struct bpf_prog *prog,
1506 enum bpf_prog_type *attach_type, bool attach_drv)
1507{
1508 /* not an attachment, just a refcount inc, always allow */
1509 if (!attach_type)
1510 return true;
1511
1512 if (prog->type != *attach_type)
1513 return false;
1514 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1515 return false;
1516
1517 return true;
1518}
1519
1520static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1521 bool attach_drv)
1522{
1523 struct fd f = fdget(ufd);
1524 struct bpf_prog *prog;
1525
1526 prog = ____bpf_prog_get(f);
1527 if (IS_ERR(prog))
1528 return prog;
1529 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1530 prog = ERR_PTR(-EINVAL);
1531 goto out;
1532 }
1533
1534 prog = bpf_prog_inc(prog);
1535out:
1536 fdput(f);
1537 return prog;
1538}
1539
1540struct bpf_prog *bpf_prog_get(u32 ufd)
1541{
1542 return __bpf_prog_get(ufd, NULL, false);
1543}
1544
1545struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1546 bool attach_drv)
1547{
1548 return __bpf_prog_get(ufd, &type, attach_drv);
1549}
1550EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1551
1552/* Initially all BPF programs could be loaded w/o specifying
1553 * expected_attach_type. Later for some of them specifying expected_attach_type
1554 * at load time became required so that program could be validated properly.
1555 * Programs of types that are allowed to be loaded both w/ and w/o (for
1556 * backward compatibility) expected_attach_type, should have the default attach
1557 * type assigned to expected_attach_type for the latter case, so that it can be
1558 * validated later at attach time.
1559 *
1560 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1561 * prog type requires it but has some attach types that have to be backward
1562 * compatible.
1563 */
1564static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1565{
1566 switch (attr->prog_type) {
1567 case BPF_PROG_TYPE_CGROUP_SOCK:
1568 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1569 * exist so checking for non-zero is the way to go here.
1570 */
1571 if (!attr->expected_attach_type)
1572 attr->expected_attach_type =
1573 BPF_CGROUP_INET_SOCK_CREATE;
1574 break;
1575 }
1576}
1577
1578static int
1579bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1580 enum bpf_attach_type expected_attach_type)
1581{
1582 switch (prog_type) {
1583 case BPF_PROG_TYPE_CGROUP_SOCK:
1584 switch (expected_attach_type) {
1585 case BPF_CGROUP_INET_SOCK_CREATE:
1586 case BPF_CGROUP_INET4_POST_BIND:
1587 case BPF_CGROUP_INET6_POST_BIND:
1588 return 0;
1589 default:
1590 return -EINVAL;
1591 }
1592 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1593 switch (expected_attach_type) {
1594 case BPF_CGROUP_INET4_BIND:
1595 case BPF_CGROUP_INET6_BIND:
1596 case BPF_CGROUP_INET4_CONNECT:
1597 case BPF_CGROUP_INET6_CONNECT:
1598 case BPF_CGROUP_UDP4_SENDMSG:
1599 case BPF_CGROUP_UDP6_SENDMSG:
1600 case BPF_CGROUP_UDP4_RECVMSG:
1601 case BPF_CGROUP_UDP6_RECVMSG:
1602 return 0;
1603 default:
1604 return -EINVAL;
1605 }
1606 case BPF_PROG_TYPE_CGROUP_SKB:
1607 switch (expected_attach_type) {
1608 case BPF_CGROUP_INET_INGRESS:
1609 case BPF_CGROUP_INET_EGRESS:
1610 return 0;
1611 default:
1612 return -EINVAL;
1613 }
1614 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1615 switch (expected_attach_type) {
1616 case BPF_CGROUP_SETSOCKOPT:
1617 case BPF_CGROUP_GETSOCKOPT:
1618 return 0;
1619 default:
1620 return -EINVAL;
1621 }
1622 default:
1623 return 0;
1624 }
1625}
1626
1627/* last field in 'union bpf_attr' used by this command */
1628#define BPF_PROG_LOAD_LAST_FIELD line_info_cnt
1629
1630static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
1631{
1632 enum bpf_prog_type type = attr->prog_type;
1633 struct bpf_prog *prog;
1634 int err;
1635 char license[128];
1636 bool is_gpl;
1637
1638 if (CHECK_ATTR(BPF_PROG_LOAD))
1639 return -EINVAL;
1640
1641 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
1642 BPF_F_ANY_ALIGNMENT |
1643 BPF_F_TEST_STATE_FREQ |
1644 BPF_F_TEST_RND_HI32))
1645 return -EINVAL;
1646
1647 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1648 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1649 !capable(CAP_SYS_ADMIN))
1650 return -EPERM;
1651
1652 /* copy eBPF program license from user space */
1653 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1654 sizeof(license) - 1) < 0)
1655 return -EFAULT;
1656 license[sizeof(license) - 1] = 0;
1657
1658 /* eBPF programs must be GPL compatible to use GPL-ed functions */
1659 is_gpl = license_is_gpl_compatible(license);
1660
1661 if (attr->insn_cnt == 0 ||
1662 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
1663 return -E2BIG;
1664 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1665 type != BPF_PROG_TYPE_CGROUP_SKB &&
1666 !capable(CAP_SYS_ADMIN))
1667 return -EPERM;
1668
1669 bpf_prog_load_fixup_attach_type(attr);
1670 if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1671 return -EINVAL;
1672
1673 /* plain bpf_prog allocation */
1674 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1675 if (!prog)
1676 return -ENOMEM;
1677
1678 prog->expected_attach_type = attr->expected_attach_type;
1679
1680 prog->aux->offload_requested = !!attr->prog_ifindex;
1681
1682 err = security_bpf_prog_alloc(prog->aux);
1683 if (err)
1684 goto free_prog_nouncharge;
1685
1686 err = bpf_prog_charge_memlock(prog);
1687 if (err)
1688 goto free_prog_sec;
1689
1690 prog->len = attr->insn_cnt;
1691
1692 err = -EFAULT;
1693 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1694 bpf_prog_insn_size(prog)) != 0)
1695 goto free_prog;
1696
1697 prog->orig_prog = NULL;
1698 prog->jited = 0;
1699
1700 atomic_set(&prog->aux->refcnt, 1);
1701 prog->gpl_compatible = is_gpl ? 1 : 0;
1702
1703 if (bpf_prog_is_dev_bound(prog->aux)) {
1704 err = bpf_prog_offload_init(prog, attr);
1705 if (err)
1706 goto free_prog;
1707 }
1708
1709 /* find program type: socket_filter vs tracing_filter */
1710 err = find_prog_type(type, prog);
1711 if (err < 0)
1712 goto free_prog;
1713
1714 prog->aux->load_time = ktime_get_boottime_ns();
1715 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1716 if (err)
1717 goto free_prog;
1718
1719 /* run eBPF verifier */
1720 err = bpf_check(&prog, attr, uattr);
1721 if (err < 0)
1722 goto free_used_maps;
1723
1724 prog = bpf_prog_select_runtime(prog, &err);
1725 if (err < 0)
1726 goto free_used_maps;
1727
1728 err = bpf_prog_alloc_id(prog);
1729 if (err)
1730 goto free_used_maps;
1731
1732 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
1733 * effectively publicly exposed. However, retrieving via
1734 * bpf_prog_get_fd_by_id() will take another reference,
1735 * therefore it cannot be gone underneath us.
1736 *
1737 * Only for the time /after/ successful bpf_prog_new_fd()
1738 * and before returning to userspace, we might just hold
1739 * one reference and any parallel close on that fd could
1740 * rip everything out. Hence, below notifications must
1741 * happen before bpf_prog_new_fd().
1742 *
1743 * Also, any failure handling from this point onwards must
1744 * be using bpf_prog_put() given the program is exposed.
1745 */
1746 bpf_prog_kallsyms_add(prog);
1747 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
1748
1749 err = bpf_prog_new_fd(prog);
1750 if (err < 0)
1751 bpf_prog_put(prog);
1752 return err;
1753
1754free_used_maps:
1755 /* In case we have subprogs, we need to wait for a grace
1756 * period before we can tear down JIT memory since symbols
1757 * are already exposed under kallsyms.
1758 */
1759 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
1760 return err;
1761free_prog:
1762 bpf_prog_uncharge_memlock(prog);
1763free_prog_sec:
1764 security_bpf_prog_free(prog->aux);
1765free_prog_nouncharge:
1766 bpf_prog_free(prog);
1767 return err;
1768}
1769
1770#define BPF_OBJ_LAST_FIELD file_flags
1771
1772static int bpf_obj_pin(const union bpf_attr *attr)
1773{
1774 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1775 return -EINVAL;
1776
1777 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1778}
1779
1780static int bpf_obj_get(const union bpf_attr *attr)
1781{
1782 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1783 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1784 return -EINVAL;
1785
1786 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1787 attr->file_flags);
1788}
1789
1790struct bpf_raw_tracepoint {
1791 struct bpf_raw_event_map *btp;
1792 struct bpf_prog *prog;
1793};
1794
1795static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1796{
1797 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1798
1799 if (raw_tp->prog) {
1800 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1801 bpf_prog_put(raw_tp->prog);
1802 }
1803 bpf_put_raw_tracepoint(raw_tp->btp);
1804 kfree(raw_tp);
1805 return 0;
1806}
1807
1808static const struct file_operations bpf_raw_tp_fops = {
1809 .release = bpf_raw_tracepoint_release,
1810 .read = bpf_dummy_read,
1811 .write = bpf_dummy_write,
1812};
1813
1814#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1815
1816static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1817{
1818 struct bpf_raw_tracepoint *raw_tp;
1819 struct bpf_raw_event_map *btp;
1820 struct bpf_prog *prog;
1821 char tp_name[128];
1822 int tp_fd, err;
1823
1824 if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1825 sizeof(tp_name) - 1) < 0)
1826 return -EFAULT;
1827 tp_name[sizeof(tp_name) - 1] = 0;
1828
1829 btp = bpf_get_raw_tracepoint(tp_name);
1830 if (!btp)
1831 return -ENOENT;
1832
1833 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1834 if (!raw_tp) {
1835 err = -ENOMEM;
1836 goto out_put_btp;
1837 }
1838 raw_tp->btp = btp;
1839
1840 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
1841 if (IS_ERR(prog)) {
1842 err = PTR_ERR(prog);
1843 goto out_free_tp;
1844 }
1845 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
1846 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
1847 err = -EINVAL;
1848 goto out_put_prog;
1849 }
1850
1851 err = bpf_probe_register(raw_tp->btp, prog);
1852 if (err)
1853 goto out_put_prog;
1854
1855 raw_tp->prog = prog;
1856 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1857 O_CLOEXEC);
1858 if (tp_fd < 0) {
1859 bpf_probe_unregister(raw_tp->btp, prog);
1860 err = tp_fd;
1861 goto out_put_prog;
1862 }
1863 return tp_fd;
1864
1865out_put_prog:
1866 bpf_prog_put(prog);
1867out_free_tp:
1868 kfree(raw_tp);
1869out_put_btp:
1870 bpf_put_raw_tracepoint(btp);
1871 return err;
1872}
1873
1874static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1875 enum bpf_attach_type attach_type)
1876{
1877 switch (prog->type) {
1878 case BPF_PROG_TYPE_CGROUP_SOCK:
1879 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1880 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1881 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1882 case BPF_PROG_TYPE_CGROUP_SKB:
1883 return prog->enforce_expected_attach_type &&
1884 prog->expected_attach_type != attach_type ?
1885 -EINVAL : 0;
1886 default:
1887 return 0;
1888 }
1889}
1890
1891#define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1892
1893#define BPF_F_ATTACH_MASK \
1894 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1895
1896static int bpf_prog_attach(const union bpf_attr *attr)
1897{
1898 enum bpf_prog_type ptype;
1899 struct bpf_prog *prog;
1900 int ret;
1901
1902 if (!capable(CAP_NET_ADMIN))
1903 return -EPERM;
1904
1905 if (CHECK_ATTR(BPF_PROG_ATTACH))
1906 return -EINVAL;
1907
1908 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
1909 return -EINVAL;
1910
1911 switch (attr->attach_type) {
1912 case BPF_CGROUP_INET_INGRESS:
1913 case BPF_CGROUP_INET_EGRESS:
1914 ptype = BPF_PROG_TYPE_CGROUP_SKB;
1915 break;
1916 case BPF_CGROUP_INET_SOCK_CREATE:
1917 case BPF_CGROUP_INET4_POST_BIND:
1918 case BPF_CGROUP_INET6_POST_BIND:
1919 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1920 break;
1921 case BPF_CGROUP_INET4_BIND:
1922 case BPF_CGROUP_INET6_BIND:
1923 case BPF_CGROUP_INET4_CONNECT:
1924 case BPF_CGROUP_INET6_CONNECT:
1925 case BPF_CGROUP_UDP4_SENDMSG:
1926 case BPF_CGROUP_UDP6_SENDMSG:
1927 case BPF_CGROUP_UDP4_RECVMSG:
1928 case BPF_CGROUP_UDP6_RECVMSG:
1929 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1930 break;
1931 case BPF_CGROUP_SOCK_OPS:
1932 ptype = BPF_PROG_TYPE_SOCK_OPS;
1933 break;
1934 case BPF_CGROUP_DEVICE:
1935 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1936 break;
1937 case BPF_SK_MSG_VERDICT:
1938 ptype = BPF_PROG_TYPE_SK_MSG;
1939 break;
1940 case BPF_SK_SKB_STREAM_PARSER:
1941 case BPF_SK_SKB_STREAM_VERDICT:
1942 ptype = BPF_PROG_TYPE_SK_SKB;
1943 break;
1944 case BPF_LIRC_MODE2:
1945 ptype = BPF_PROG_TYPE_LIRC_MODE2;
1946 break;
1947 case BPF_FLOW_DISSECTOR:
1948 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1949 break;
1950 case BPF_CGROUP_SYSCTL:
1951 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
1952 break;
1953 case BPF_CGROUP_GETSOCKOPT:
1954 case BPF_CGROUP_SETSOCKOPT:
1955 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
1956 break;
1957 default:
1958 return -EINVAL;
1959 }
1960
1961 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1962 if (IS_ERR(prog))
1963 return PTR_ERR(prog);
1964
1965 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1966 bpf_prog_put(prog);
1967 return -EINVAL;
1968 }
1969
1970 switch (ptype) {
1971 case BPF_PROG_TYPE_SK_SKB:
1972 case BPF_PROG_TYPE_SK_MSG:
1973 ret = sock_map_get_from_fd(attr, prog);
1974 break;
1975 case BPF_PROG_TYPE_LIRC_MODE2:
1976 ret = lirc_prog_attach(attr, prog);
1977 break;
1978 case BPF_PROG_TYPE_FLOW_DISSECTOR:
1979 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1980 break;
1981 default:
1982 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
1983 }
1984
1985 if (ret)
1986 bpf_prog_put(prog);
1987 return ret;
1988}
1989
1990#define BPF_PROG_DETACH_LAST_FIELD attach_type
1991
1992static int bpf_prog_detach(const union bpf_attr *attr)
1993{
1994 enum bpf_prog_type ptype;
1995
1996 if (!capable(CAP_NET_ADMIN))
1997 return -EPERM;
1998
1999 if (CHECK_ATTR(BPF_PROG_DETACH))
2000 return -EINVAL;
2001
2002 switch (attr->attach_type) {
2003 case BPF_CGROUP_INET_INGRESS:
2004 case BPF_CGROUP_INET_EGRESS:
2005 ptype = BPF_PROG_TYPE_CGROUP_SKB;
2006 break;
2007 case BPF_CGROUP_INET_SOCK_CREATE:
2008 case BPF_CGROUP_INET4_POST_BIND:
2009 case BPF_CGROUP_INET6_POST_BIND:
2010 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2011 break;
2012 case BPF_CGROUP_INET4_BIND:
2013 case BPF_CGROUP_INET6_BIND:
2014 case BPF_CGROUP_INET4_CONNECT:
2015 case BPF_CGROUP_INET6_CONNECT:
2016 case BPF_CGROUP_UDP4_SENDMSG:
2017 case BPF_CGROUP_UDP6_SENDMSG:
2018 case BPF_CGROUP_UDP4_RECVMSG:
2019 case BPF_CGROUP_UDP6_RECVMSG:
2020 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2021 break;
2022 case BPF_CGROUP_SOCK_OPS:
2023 ptype = BPF_PROG_TYPE_SOCK_OPS;
2024 break;
2025 case BPF_CGROUP_DEVICE:
2026 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2027 break;
2028 case BPF_SK_MSG_VERDICT:
2029 return sock_map_get_from_fd(attr, NULL);
2030 case BPF_SK_SKB_STREAM_PARSER:
2031 case BPF_SK_SKB_STREAM_VERDICT:
2032 return sock_map_get_from_fd(attr, NULL);
2033 case BPF_LIRC_MODE2:
2034 return lirc_prog_detach(attr);
2035 case BPF_FLOW_DISSECTOR:
2036 return skb_flow_dissector_bpf_prog_detach(attr);
2037 case BPF_CGROUP_SYSCTL:
2038 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2039 break;
2040 case BPF_CGROUP_GETSOCKOPT:
2041 case BPF_CGROUP_SETSOCKOPT:
2042 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2043 break;
2044 default:
2045 return -EINVAL;
2046 }
2047
2048 return cgroup_bpf_prog_detach(attr, ptype);
2049}
2050
2051#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2052
2053static int bpf_prog_query(const union bpf_attr *attr,
2054 union bpf_attr __user *uattr)
2055{
2056 if (!capable(CAP_NET_ADMIN))
2057 return -EPERM;
2058 if (CHECK_ATTR(BPF_PROG_QUERY))
2059 return -EINVAL;
2060 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2061 return -EINVAL;
2062
2063 switch (attr->query.attach_type) {
2064 case BPF_CGROUP_INET_INGRESS:
2065 case BPF_CGROUP_INET_EGRESS:
2066 case BPF_CGROUP_INET_SOCK_CREATE:
2067 case BPF_CGROUP_INET4_BIND:
2068 case BPF_CGROUP_INET6_BIND:
2069 case BPF_CGROUP_INET4_POST_BIND:
2070 case BPF_CGROUP_INET6_POST_BIND:
2071 case BPF_CGROUP_INET4_CONNECT:
2072 case BPF_CGROUP_INET6_CONNECT:
2073 case BPF_CGROUP_UDP4_SENDMSG:
2074 case BPF_CGROUP_UDP6_SENDMSG:
2075 case BPF_CGROUP_UDP4_RECVMSG:
2076 case BPF_CGROUP_UDP6_RECVMSG:
2077 case BPF_CGROUP_SOCK_OPS:
2078 case BPF_CGROUP_DEVICE:
2079 case BPF_CGROUP_SYSCTL:
2080 case BPF_CGROUP_GETSOCKOPT:
2081 case BPF_CGROUP_SETSOCKOPT:
2082 break;
2083 case BPF_LIRC_MODE2:
2084 return lirc_prog_query(attr, uattr);
2085 case BPF_FLOW_DISSECTOR:
2086 return skb_flow_dissector_prog_query(attr, uattr);
2087 default:
2088 return -EINVAL;
2089 }
2090
2091 return cgroup_bpf_prog_query(attr, uattr);
2092}
2093
2094#define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
2095
2096static int bpf_prog_test_run(const union bpf_attr *attr,
2097 union bpf_attr __user *uattr)
2098{
2099 struct bpf_prog *prog;
2100 int ret = -ENOTSUPP;
2101
2102 if (!capable(CAP_SYS_ADMIN))
2103 return -EPERM;
2104 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2105 return -EINVAL;
2106
2107 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2108 (!attr->test.ctx_size_in && attr->test.ctx_in))
2109 return -EINVAL;
2110
2111 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2112 (!attr->test.ctx_size_out && attr->test.ctx_out))
2113 return -EINVAL;
2114
2115 prog = bpf_prog_get(attr->test.prog_fd);
2116 if (IS_ERR(prog))
2117 return PTR_ERR(prog);
2118
2119 if (prog->aux->ops->test_run)
2120 ret = prog->aux->ops->test_run(prog, attr, uattr);
2121
2122 bpf_prog_put(prog);
2123 return ret;
2124}
2125
2126#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2127
2128static int bpf_obj_get_next_id(const union bpf_attr *attr,
2129 union bpf_attr __user *uattr,
2130 struct idr *idr,
2131 spinlock_t *lock)
2132{
2133 u32 next_id = attr->start_id;
2134 int err = 0;
2135
2136 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2137 return -EINVAL;
2138
2139 if (!capable(CAP_SYS_ADMIN))
2140 return -EPERM;
2141
2142 next_id++;
2143 spin_lock_bh(lock);
2144 if (!idr_get_next(idr, &next_id))
2145 err = -ENOENT;
2146 spin_unlock_bh(lock);
2147
2148 if (!err)
2149 err = put_user(next_id, &uattr->next_id);
2150
2151 return err;
2152}
2153
2154#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2155
2156static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2157{
2158 struct bpf_prog *prog;
2159 u32 id = attr->prog_id;
2160 int fd;
2161
2162 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2163 return -EINVAL;
2164
2165 if (!capable(CAP_SYS_ADMIN))
2166 return -EPERM;
2167
2168 spin_lock_bh(&prog_idr_lock);
2169 prog = idr_find(&prog_idr, id);
2170 if (prog)
2171 prog = bpf_prog_inc_not_zero(prog);
2172 else
2173 prog = ERR_PTR(-ENOENT);
2174 spin_unlock_bh(&prog_idr_lock);
2175
2176 if (IS_ERR(prog))
2177 return PTR_ERR(prog);
2178
2179 fd = bpf_prog_new_fd(prog);
2180 if (fd < 0)
2181 bpf_prog_put(prog);
2182
2183 return fd;
2184}
2185
2186#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2187
2188static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2189{
2190 struct bpf_map *map;
2191 u32 id = attr->map_id;
2192 int f_flags;
2193 int fd;
2194
2195 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2196 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
2197 return -EINVAL;
2198
2199 if (!capable(CAP_SYS_ADMIN))
2200 return -EPERM;
2201
2202 f_flags = bpf_get_file_flag(attr->open_flags);
2203 if (f_flags < 0)
2204 return f_flags;
2205
2206 spin_lock_bh(&map_idr_lock);
2207 map = idr_find(&map_idr, id);
2208 if (map)
2209 map = __bpf_map_inc_not_zero(map, true);
2210 else
2211 map = ERR_PTR(-ENOENT);
2212 spin_unlock_bh(&map_idr_lock);
2213
2214 if (IS_ERR(map))
2215 return PTR_ERR(map);
2216
2217 fd = bpf_map_new_fd(map, f_flags);
2218 if (fd < 0)
2219 bpf_map_put_with_uref(map);
2220
2221 return fd;
2222}
2223
2224static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
2225 unsigned long addr, u32 *off,
2226 u32 *type)
2227{
2228 const struct bpf_map *map;
2229 int i;
2230
2231 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2232 map = prog->aux->used_maps[i];
2233 if (map == (void *)addr) {
2234 *type = BPF_PSEUDO_MAP_FD;
2235 return map;
2236 }
2237 if (!map->ops->map_direct_value_meta)
2238 continue;
2239 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2240 *type = BPF_PSEUDO_MAP_VALUE;
2241 return map;
2242 }
2243 }
2244
2245 return NULL;
2246}
2247
2248static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2249{
2250 const struct bpf_map *map;
2251 struct bpf_insn *insns;
2252 u32 off, type;
2253 u64 imm;
2254 int i;
2255
2256 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2257 GFP_USER);
2258 if (!insns)
2259 return insns;
2260
2261 for (i = 0; i < prog->len; i++) {
2262 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2263 insns[i].code = BPF_JMP | BPF_CALL;
2264 insns[i].imm = BPF_FUNC_tail_call;
2265 /* fall-through */
2266 }
2267 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2268 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2269 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2270 insns[i].code = BPF_JMP | BPF_CALL;
2271 if (!bpf_dump_raw_ok())
2272 insns[i].imm = 0;
2273 continue;
2274 }
2275
2276 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2277 continue;
2278
2279 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
2280 map = bpf_map_from_imm(prog, imm, &off, &type);
2281 if (map) {
2282 insns[i].src_reg = type;
2283 insns[i].imm = map->id;
2284 insns[i + 1].imm = off;
2285 continue;
2286 }
2287 }
2288
2289 return insns;
2290}
2291
2292static int set_info_rec_size(struct bpf_prog_info *info)
2293{
2294 /*
2295 * Ensure info.*_rec_size is the same as kernel expected size
2296 *
2297 * or
2298 *
2299 * Only allow zero *_rec_size if both _rec_size and _cnt are
2300 * zero. In this case, the kernel will set the expected
2301 * _rec_size back to the info.
2302 */
2303
2304 if ((info->nr_func_info || info->func_info_rec_size) &&
2305 info->func_info_rec_size != sizeof(struct bpf_func_info))
2306 return -EINVAL;
2307
2308 if ((info->nr_line_info || info->line_info_rec_size) &&
2309 info->line_info_rec_size != sizeof(struct bpf_line_info))
2310 return -EINVAL;
2311
2312 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
2313 info->jited_line_info_rec_size != sizeof(__u64))
2314 return -EINVAL;
2315
2316 info->func_info_rec_size = sizeof(struct bpf_func_info);
2317 info->line_info_rec_size = sizeof(struct bpf_line_info);
2318 info->jited_line_info_rec_size = sizeof(__u64);
2319
2320 return 0;
2321}
2322
2323static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2324 const union bpf_attr *attr,
2325 union bpf_attr __user *uattr)
2326{
2327 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2328 struct bpf_prog_info info = {};
2329 u32 info_len = attr->info.info_len;
2330 struct bpf_prog_stats stats;
2331 char __user *uinsns;
2332 u32 ulen;
2333 int err;
2334
2335 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2336 if (err)
2337 return err;
2338 info_len = min_t(u32, sizeof(info), info_len);
2339
2340 if (copy_from_user(&info, uinfo, info_len))
2341 return -EFAULT;
2342
2343 info.type = prog->type;
2344 info.id = prog->aux->id;
2345 info.load_time = prog->aux->load_time;
2346 info.created_by_uid = from_kuid_munged(current_user_ns(),
2347 prog->aux->user->uid);
2348 info.gpl_compatible = prog->gpl_compatible;
2349
2350 memcpy(info.tag, prog->tag, sizeof(prog->tag));
2351 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2352
2353 ulen = info.nr_map_ids;
2354 info.nr_map_ids = prog->aux->used_map_cnt;
2355 ulen = min_t(u32, info.nr_map_ids, ulen);
2356 if (ulen) {
2357 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
2358 u32 i;
2359
2360 for (i = 0; i < ulen; i++)
2361 if (put_user(prog->aux->used_maps[i]->id,
2362 &user_map_ids[i]))
2363 return -EFAULT;
2364 }
2365
2366 err = set_info_rec_size(&info);
2367 if (err)
2368 return err;
2369
2370 bpf_prog_get_stats(prog, &stats);
2371 info.run_time_ns = stats.nsecs;
2372 info.run_cnt = stats.cnt;
2373
2374 if (!capable(CAP_SYS_ADMIN)) {
2375 info.jited_prog_len = 0;
2376 info.xlated_prog_len = 0;
2377 info.nr_jited_ksyms = 0;
2378 info.nr_jited_func_lens = 0;
2379 info.nr_func_info = 0;
2380 info.nr_line_info = 0;
2381 info.nr_jited_line_info = 0;
2382 goto done;
2383 }
2384
2385 ulen = info.xlated_prog_len;
2386 info.xlated_prog_len = bpf_prog_insn_size(prog);
2387 if (info.xlated_prog_len && ulen) {
2388 struct bpf_insn *insns_sanitized;
2389 bool fault;
2390
2391 if (prog->blinded && !bpf_dump_raw_ok()) {
2392 info.xlated_prog_insns = 0;
2393 goto done;
2394 }
2395 insns_sanitized = bpf_insn_prepare_dump(prog);
2396 if (!insns_sanitized)
2397 return -ENOMEM;
2398 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2399 ulen = min_t(u32, info.xlated_prog_len, ulen);
2400 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2401 kfree(insns_sanitized);
2402 if (fault)
2403 return -EFAULT;
2404 }
2405
2406 if (bpf_prog_is_dev_bound(prog->aux)) {
2407 err = bpf_prog_offload_info_fill(&info, prog);
2408 if (err)
2409 return err;
2410 goto done;
2411 }
2412
2413 /* NOTE: the following code is supposed to be skipped for offload.
2414 * bpf_prog_offload_info_fill() is the place to fill similar fields
2415 * for offload.
2416 */
2417 ulen = info.jited_prog_len;
2418 if (prog->aux->func_cnt) {
2419 u32 i;
2420
2421 info.jited_prog_len = 0;
2422 for (i = 0; i < prog->aux->func_cnt; i++)
2423 info.jited_prog_len += prog->aux->func[i]->jited_len;
2424 } else {
2425 info.jited_prog_len = prog->jited_len;
2426 }
2427
2428 if (info.jited_prog_len && ulen) {
2429 if (bpf_dump_raw_ok()) {
2430 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2431 ulen = min_t(u32, info.jited_prog_len, ulen);
2432
2433 /* for multi-function programs, copy the JITed
2434 * instructions for all the functions
2435 */
2436 if (prog->aux->func_cnt) {
2437 u32 len, free, i;
2438 u8 *img;
2439
2440 free = ulen;
2441 for (i = 0; i < prog->aux->func_cnt; i++) {
2442 len = prog->aux->func[i]->jited_len;
2443 len = min_t(u32, len, free);
2444 img = (u8 *) prog->aux->func[i]->bpf_func;
2445 if (copy_to_user(uinsns, img, len))
2446 return -EFAULT;
2447 uinsns += len;
2448 free -= len;
2449 if (!free)
2450 break;
2451 }
2452 } else {
2453 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2454 return -EFAULT;
2455 }
2456 } else {
2457 info.jited_prog_insns = 0;
2458 }
2459 }
2460
2461 ulen = info.nr_jited_ksyms;
2462 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
2463 if (ulen) {
2464 if (bpf_dump_raw_ok()) {
2465 unsigned long ksym_addr;
2466 u64 __user *user_ksyms;
2467 u32 i;
2468
2469 /* copy the address of the kernel symbol
2470 * corresponding to each function
2471 */
2472 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2473 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2474 if (prog->aux->func_cnt) {
2475 for (i = 0; i < ulen; i++) {
2476 ksym_addr = (unsigned long)
2477 prog->aux->func[i]->bpf_func;
2478 if (put_user((u64) ksym_addr,
2479 &user_ksyms[i]))
2480 return -EFAULT;
2481 }
2482 } else {
2483 ksym_addr = (unsigned long) prog->bpf_func;
2484 if (put_user((u64) ksym_addr, &user_ksyms[0]))
2485 return -EFAULT;
2486 }
2487 } else {
2488 info.jited_ksyms = 0;
2489 }
2490 }
2491
2492 ulen = info.nr_jited_func_lens;
2493 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
2494 if (ulen) {
2495 if (bpf_dump_raw_ok()) {
2496 u32 __user *user_lens;
2497 u32 func_len, i;
2498
2499 /* copy the JITed image lengths for each function */
2500 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2501 user_lens = u64_to_user_ptr(info.jited_func_lens);
2502 if (prog->aux->func_cnt) {
2503 for (i = 0; i < ulen; i++) {
2504 func_len =
2505 prog->aux->func[i]->jited_len;
2506 if (put_user(func_len, &user_lens[i]))
2507 return -EFAULT;
2508 }
2509 } else {
2510 func_len = prog->jited_len;
2511 if (put_user(func_len, &user_lens[0]))
2512 return -EFAULT;
2513 }
2514 } else {
2515 info.jited_func_lens = 0;
2516 }
2517 }
2518
2519 if (prog->aux->btf)
2520 info.btf_id = btf_id(prog->aux->btf);
2521
2522 ulen = info.nr_func_info;
2523 info.nr_func_info = prog->aux->func_info_cnt;
2524 if (info.nr_func_info && ulen) {
2525 char __user *user_finfo;
2526
2527 user_finfo = u64_to_user_ptr(info.func_info);
2528 ulen = min_t(u32, info.nr_func_info, ulen);
2529 if (copy_to_user(user_finfo, prog->aux->func_info,
2530 info.func_info_rec_size * ulen))
2531 return -EFAULT;
2532 }
2533
2534 ulen = info.nr_line_info;
2535 info.nr_line_info = prog->aux->nr_linfo;
2536 if (info.nr_line_info && ulen) {
2537 __u8 __user *user_linfo;
2538
2539 user_linfo = u64_to_user_ptr(info.line_info);
2540 ulen = min_t(u32, info.nr_line_info, ulen);
2541 if (copy_to_user(user_linfo, prog->aux->linfo,
2542 info.line_info_rec_size * ulen))
2543 return -EFAULT;
2544 }
2545
2546 ulen = info.nr_jited_line_info;
2547 if (prog->aux->jited_linfo)
2548 info.nr_jited_line_info = prog->aux->nr_linfo;
2549 else
2550 info.nr_jited_line_info = 0;
2551 if (info.nr_jited_line_info && ulen) {
2552 if (bpf_dump_raw_ok()) {
2553 __u64 __user *user_linfo;
2554 u32 i;
2555
2556 user_linfo = u64_to_user_ptr(info.jited_line_info);
2557 ulen = min_t(u32, info.nr_jited_line_info, ulen);
2558 for (i = 0; i < ulen; i++) {
2559 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2560 &user_linfo[i]))
2561 return -EFAULT;
2562 }
2563 } else {
2564 info.jited_line_info = 0;
2565 }
2566 }
2567
2568 ulen = info.nr_prog_tags;
2569 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2570 if (ulen) {
2571 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2572 u32 i;
2573
2574 user_prog_tags = u64_to_user_ptr(info.prog_tags);
2575 ulen = min_t(u32, info.nr_prog_tags, ulen);
2576 if (prog->aux->func_cnt) {
2577 for (i = 0; i < ulen; i++) {
2578 if (copy_to_user(user_prog_tags[i],
2579 prog->aux->func[i]->tag,
2580 BPF_TAG_SIZE))
2581 return -EFAULT;
2582 }
2583 } else {
2584 if (copy_to_user(user_prog_tags[0],
2585 prog->tag, BPF_TAG_SIZE))
2586 return -EFAULT;
2587 }
2588 }
2589
2590done:
2591 if (copy_to_user(uinfo, &info, info_len) ||
2592 put_user(info_len, &uattr->info.info_len))
2593 return -EFAULT;
2594
2595 return 0;
2596}
2597
2598static int bpf_map_get_info_by_fd(struct bpf_map *map,
2599 const union bpf_attr *attr,
2600 union bpf_attr __user *uattr)
2601{
2602 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2603 struct bpf_map_info info = {};
2604 u32 info_len = attr->info.info_len;
2605 int err;
2606
2607 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2608 if (err)
2609 return err;
2610 info_len = min_t(u32, sizeof(info), info_len);
2611
2612 info.type = map->map_type;
2613 info.id = map->id;
2614 info.key_size = map->key_size;
2615 info.value_size = map->value_size;
2616 info.max_entries = map->max_entries;
2617 info.map_flags = map->map_flags;
2618 memcpy(info.name, map->name, sizeof(map->name));
2619
2620 if (map->btf) {
2621 info.btf_id = btf_id(map->btf);
2622 info.btf_key_type_id = map->btf_key_type_id;
2623 info.btf_value_type_id = map->btf_value_type_id;
2624 }
2625
2626 if (bpf_map_is_dev_bound(map)) {
2627 err = bpf_map_offload_info_fill(&info, map);
2628 if (err)
2629 return err;
2630 }
2631
2632 if (copy_to_user(uinfo, &info, info_len) ||
2633 put_user(info_len, &uattr->info.info_len))
2634 return -EFAULT;
2635
2636 return 0;
2637}
2638
2639static int bpf_btf_get_info_by_fd(struct btf *btf,
2640 const union bpf_attr *attr,
2641 union bpf_attr __user *uattr)
2642{
2643 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2644 u32 info_len = attr->info.info_len;
2645 int err;
2646
2647 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
2648 if (err)
2649 return err;
2650
2651 return btf_get_info_by_fd(btf, attr, uattr);
2652}
2653
2654#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2655
2656static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2657 union bpf_attr __user *uattr)
2658{
2659 int ufd = attr->info.bpf_fd;
2660 struct fd f;
2661 int err;
2662
2663 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2664 return -EINVAL;
2665
2666 f = fdget(ufd);
2667 if (!f.file)
2668 return -EBADFD;
2669
2670 if (f.file->f_op == &bpf_prog_fops)
2671 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2672 uattr);
2673 else if (f.file->f_op == &bpf_map_fops)
2674 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2675 uattr);
2676 else if (f.file->f_op == &btf_fops)
2677 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
2678 else
2679 err = -EINVAL;
2680
2681 fdput(f);
2682 return err;
2683}
2684
2685#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2686
2687static int bpf_btf_load(const union bpf_attr *attr)
2688{
2689 if (CHECK_ATTR(BPF_BTF_LOAD))
2690 return -EINVAL;
2691
2692 if (!capable(CAP_SYS_ADMIN))
2693 return -EPERM;
2694
2695 return btf_new_fd(attr);
2696}
2697
2698#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
2699
2700static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
2701{
2702 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
2703 return -EINVAL;
2704
2705 if (!capable(CAP_SYS_ADMIN))
2706 return -EPERM;
2707
2708 return btf_get_fd_by_id(attr->btf_id);
2709}
2710
2711static int bpf_task_fd_query_copy(const union bpf_attr *attr,
2712 union bpf_attr __user *uattr,
2713 u32 prog_id, u32 fd_type,
2714 const char *buf, u64 probe_offset,
2715 u64 probe_addr)
2716{
2717 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
2718 u32 len = buf ? strlen(buf) : 0, input_len;
2719 int err = 0;
2720
2721 if (put_user(len, &uattr->task_fd_query.buf_len))
2722 return -EFAULT;
2723 input_len = attr->task_fd_query.buf_len;
2724 if (input_len && ubuf) {
2725 if (!len) {
2726 /* nothing to copy, just make ubuf NULL terminated */
2727 char zero = '\0';
2728
2729 if (put_user(zero, ubuf))
2730 return -EFAULT;
2731 } else if (input_len >= len + 1) {
2732 /* ubuf can hold the string with NULL terminator */
2733 if (copy_to_user(ubuf, buf, len + 1))
2734 return -EFAULT;
2735 } else {
2736 /* ubuf cannot hold the string with NULL terminator,
2737 * do a partial copy with NULL terminator.
2738 */
2739 char zero = '\0';
2740
2741 err = -ENOSPC;
2742 if (copy_to_user(ubuf, buf, input_len - 1))
2743 return -EFAULT;
2744 if (put_user(zero, ubuf + input_len - 1))
2745 return -EFAULT;
2746 }
2747 }
2748
2749 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
2750 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
2751 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
2752 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
2753 return -EFAULT;
2754
2755 return err;
2756}
2757
2758#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
2759
2760static int bpf_task_fd_query(const union bpf_attr *attr,
2761 union bpf_attr __user *uattr)
2762{
2763 pid_t pid = attr->task_fd_query.pid;
2764 u32 fd = attr->task_fd_query.fd;
2765 const struct perf_event *event;
2766 struct files_struct *files;
2767 struct task_struct *task;
2768 struct file *file;
2769 int err;
2770
2771 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
2772 return -EINVAL;
2773
2774 if (!capable(CAP_SYS_ADMIN))
2775 return -EPERM;
2776
2777 if (attr->task_fd_query.flags != 0)
2778 return -EINVAL;
2779
2780 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
2781 if (!task)
2782 return -ENOENT;
2783
2784 files = get_files_struct(task);
2785 put_task_struct(task);
2786 if (!files)
2787 return -ENOENT;
2788
2789 err = 0;
2790 spin_lock(&files->file_lock);
2791 file = fcheck_files(files, fd);
2792 if (!file)
2793 err = -EBADF;
2794 else
2795 get_file(file);
2796 spin_unlock(&files->file_lock);
2797 put_files_struct(files);
2798
2799 if (err)
2800 goto out;
2801
2802 if (file->f_op == &bpf_raw_tp_fops) {
2803 struct bpf_raw_tracepoint *raw_tp = file->private_data;
2804 struct bpf_raw_event_map *btp = raw_tp->btp;
2805
2806 err = bpf_task_fd_query_copy(attr, uattr,
2807 raw_tp->prog->aux->id,
2808 BPF_FD_TYPE_RAW_TRACEPOINT,
2809 btp->tp->name, 0, 0);
2810 goto put_file;
2811 }
2812
2813 event = perf_get_event(file);
2814 if (!IS_ERR(event)) {
2815 u64 probe_offset, probe_addr;
2816 u32 prog_id, fd_type;
2817 const char *buf;
2818
2819 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
2820 &buf, &probe_offset,
2821 &probe_addr);
2822 if (!err)
2823 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
2824 fd_type, buf,
2825 probe_offset,
2826 probe_addr);
2827 goto put_file;
2828 }
2829
2830 err = -ENOTSUPP;
2831put_file:
2832 fput(file);
2833out:
2834 return err;
2835}
2836
2837SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2838{
2839 union bpf_attr attr = {};
2840 int err;
2841
2842 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
2843 return -EPERM;
2844
2845 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
2846 if (err)
2847 return err;
2848 size = min_t(u32, size, sizeof(attr));
2849
2850 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
2851 if (copy_from_user(&attr, uattr, size) != 0)
2852 return -EFAULT;
2853
2854 err = security_bpf(cmd, &attr, size);
2855 if (err < 0)
2856 return err;
2857
2858 switch (cmd) {
2859 case BPF_MAP_CREATE:
2860 err = map_create(&attr);
2861 break;
2862 case BPF_MAP_LOOKUP_ELEM:
2863 err = map_lookup_elem(&attr);
2864 break;
2865 case BPF_MAP_UPDATE_ELEM:
2866 err = map_update_elem(&attr);
2867 break;
2868 case BPF_MAP_DELETE_ELEM:
2869 err = map_delete_elem(&attr);
2870 break;
2871 case BPF_MAP_GET_NEXT_KEY:
2872 err = map_get_next_key(&attr);
2873 break;
2874 case BPF_MAP_FREEZE:
2875 err = map_freeze(&attr);
2876 break;
2877 case BPF_PROG_LOAD:
2878 err = bpf_prog_load(&attr, uattr);
2879 break;
2880 case BPF_OBJ_PIN:
2881 err = bpf_obj_pin(&attr);
2882 break;
2883 case BPF_OBJ_GET:
2884 err = bpf_obj_get(&attr);
2885 break;
2886 case BPF_PROG_ATTACH:
2887 err = bpf_prog_attach(&attr);
2888 break;
2889 case BPF_PROG_DETACH:
2890 err = bpf_prog_detach(&attr);
2891 break;
2892 case BPF_PROG_QUERY:
2893 err = bpf_prog_query(&attr, uattr);
2894 break;
2895 case BPF_PROG_TEST_RUN:
2896 err = bpf_prog_test_run(&attr, uattr);
2897 break;
2898 case BPF_PROG_GET_NEXT_ID:
2899 err = bpf_obj_get_next_id(&attr, uattr,
2900 &prog_idr, &prog_idr_lock);
2901 break;
2902 case BPF_MAP_GET_NEXT_ID:
2903 err = bpf_obj_get_next_id(&attr, uattr,
2904 &map_idr, &map_idr_lock);
2905 break;
2906 case BPF_BTF_GET_NEXT_ID:
2907 err = bpf_obj_get_next_id(&attr, uattr,
2908 &btf_idr, &btf_idr_lock);
2909 break;
2910 case BPF_PROG_GET_FD_BY_ID:
2911 err = bpf_prog_get_fd_by_id(&attr);
2912 break;
2913 case BPF_MAP_GET_FD_BY_ID:
2914 err = bpf_map_get_fd_by_id(&attr);
2915 break;
2916 case BPF_OBJ_GET_INFO_BY_FD:
2917 err = bpf_obj_get_info_by_fd(&attr, uattr);
2918 break;
2919 case BPF_RAW_TRACEPOINT_OPEN:
2920 err = bpf_raw_tracepoint_open(&attr);
2921 break;
2922 case BPF_BTF_LOAD:
2923 err = bpf_btf_load(&attr);
2924 break;
2925 case BPF_BTF_GET_FD_BY_ID:
2926 err = bpf_btf_get_fd_by_id(&attr);
2927 break;
2928 case BPF_TASK_FD_QUERY:
2929 err = bpf_task_fd_query(&attr, uattr);
2930 break;
2931 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2932 err = map_lookup_and_delete_elem(&attr);
2933 break;
2934 default:
2935 err = -EINVAL;
2936 break;
2937 }
2938
2939 return err;
2940}
1// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 */
4#include <linux/bpf.h>
5#include <linux/bpf_trace.h>
6#include <linux/bpf_lirc.h>
7#include <linux/bpf_verifier.h>
8#include <linux/btf.h>
9#include <linux/syscalls.h>
10#include <linux/slab.h>
11#include <linux/sched/signal.h>
12#include <linux/vmalloc.h>
13#include <linux/mmzone.h>
14#include <linux/anon_inodes.h>
15#include <linux/fdtable.h>
16#include <linux/file.h>
17#include <linux/fs.h>
18#include <linux/license.h>
19#include <linux/filter.h>
20#include <linux/kernel.h>
21#include <linux/idr.h>
22#include <linux/cred.h>
23#include <linux/timekeeping.h>
24#include <linux/ctype.h>
25#include <linux/nospec.h>
26#include <linux/audit.h>
27#include <uapi/linux/btf.h>
28#include <linux/pgtable.h>
29#include <linux/bpf_lsm.h>
30#include <linux/poll.h>
31#include <linux/bpf-netns.h>
32#include <linux/rcupdate_trace.h>
33#include <linux/memcontrol.h>
34
35#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
36 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
38#define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
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_PROG_ARRAY(map) || \
41 IS_FD_HASH(map))
42
43#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
44
45DEFINE_PER_CPU(int, bpf_prog_active);
46static DEFINE_IDR(prog_idr);
47static DEFINE_SPINLOCK(prog_idr_lock);
48static DEFINE_IDR(map_idr);
49static DEFINE_SPINLOCK(map_idr_lock);
50static DEFINE_IDR(link_idr);
51static DEFINE_SPINLOCK(link_idr_lock);
52
53int sysctl_unprivileged_bpf_disabled __read_mostly =
54 IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
55
56static const struct bpf_map_ops * const bpf_map_types[] = {
57#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
58#define BPF_MAP_TYPE(_id, _ops) \
59 [_id] = &_ops,
60#define BPF_LINK_TYPE(_id, _name)
61#include <linux/bpf_types.h>
62#undef BPF_PROG_TYPE
63#undef BPF_MAP_TYPE
64#undef BPF_LINK_TYPE
65};
66
67/*
68 * If we're handed a bigger struct than we know of, ensure all the unknown bits
69 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
70 * we don't know about yet.
71 *
72 * There is a ToCToU between this function call and the following
73 * copy_from_user() call. However, this is not a concern since this function is
74 * meant to be a future-proofing of bits.
75 */
76int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
77 size_t expected_size,
78 size_t actual_size)
79{
80 int res;
81
82 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
83 return -E2BIG;
84
85 if (actual_size <= expected_size)
86 return 0;
87
88 if (uaddr.is_kernel)
89 res = memchr_inv(uaddr.kernel + expected_size, 0,
90 actual_size - expected_size) == NULL;
91 else
92 res = check_zeroed_user(uaddr.user + expected_size,
93 actual_size - expected_size);
94 if (res < 0)
95 return res;
96 return res ? 0 : -E2BIG;
97}
98
99const struct bpf_map_ops bpf_map_offload_ops = {
100 .map_meta_equal = bpf_map_meta_equal,
101 .map_alloc = bpf_map_offload_map_alloc,
102 .map_free = bpf_map_offload_map_free,
103 .map_check_btf = map_check_no_btf,
104};
105
106static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
107{
108 const struct bpf_map_ops *ops;
109 u32 type = attr->map_type;
110 struct bpf_map *map;
111 int err;
112
113 if (type >= ARRAY_SIZE(bpf_map_types))
114 return ERR_PTR(-EINVAL);
115 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
116 ops = bpf_map_types[type];
117 if (!ops)
118 return ERR_PTR(-EINVAL);
119
120 if (ops->map_alloc_check) {
121 err = ops->map_alloc_check(attr);
122 if (err)
123 return ERR_PTR(err);
124 }
125 if (attr->map_ifindex)
126 ops = &bpf_map_offload_ops;
127 map = ops->map_alloc(attr);
128 if (IS_ERR(map))
129 return map;
130 map->ops = ops;
131 map->map_type = type;
132 return map;
133}
134
135static u32 bpf_map_value_size(const struct bpf_map *map)
136{
137 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
138 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
139 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
140 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
141 return round_up(map->value_size, 8) * num_possible_cpus();
142 else if (IS_FD_MAP(map))
143 return sizeof(u32);
144 else
145 return map->value_size;
146}
147
148static void maybe_wait_bpf_programs(struct bpf_map *map)
149{
150 /* Wait for any running BPF programs to complete so that
151 * userspace, when we return to it, knows that all programs
152 * that could be running use the new map value.
153 */
154 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
155 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
156 synchronize_rcu();
157}
158
159static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
160 void *value, __u64 flags)
161{
162 int err;
163
164 /* Need to create a kthread, thus must support schedule */
165 if (bpf_map_is_dev_bound(map)) {
166 return bpf_map_offload_update_elem(map, key, value, flags);
167 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
168 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
169 return map->ops->map_update_elem(map, key, value, flags);
170 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
171 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
172 return sock_map_update_elem_sys(map, key, value, flags);
173 } else if (IS_FD_PROG_ARRAY(map)) {
174 return bpf_fd_array_map_update_elem(map, f.file, key, value,
175 flags);
176 }
177
178 bpf_disable_instrumentation();
179 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
180 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
181 err = bpf_percpu_hash_update(map, key, value, flags);
182 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
183 err = bpf_percpu_array_update(map, key, value, flags);
184 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
185 err = bpf_percpu_cgroup_storage_update(map, key, value,
186 flags);
187 } else if (IS_FD_ARRAY(map)) {
188 rcu_read_lock();
189 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
190 flags);
191 rcu_read_unlock();
192 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
193 rcu_read_lock();
194 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
195 flags);
196 rcu_read_unlock();
197 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
198 /* rcu_read_lock() is not needed */
199 err = bpf_fd_reuseport_array_update_elem(map, key, value,
200 flags);
201 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
202 map->map_type == BPF_MAP_TYPE_STACK) {
203 err = map->ops->map_push_elem(map, value, flags);
204 } else {
205 rcu_read_lock();
206 err = map->ops->map_update_elem(map, key, value, flags);
207 rcu_read_unlock();
208 }
209 bpf_enable_instrumentation();
210 maybe_wait_bpf_programs(map);
211
212 return err;
213}
214
215static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
216 __u64 flags)
217{
218 void *ptr;
219 int err;
220
221 if (bpf_map_is_dev_bound(map))
222 return bpf_map_offload_lookup_elem(map, key, value);
223
224 bpf_disable_instrumentation();
225 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
226 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
227 err = bpf_percpu_hash_copy(map, key, value);
228 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
229 err = bpf_percpu_array_copy(map, key, value);
230 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
231 err = bpf_percpu_cgroup_storage_copy(map, key, value);
232 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
233 err = bpf_stackmap_copy(map, key, value);
234 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
235 err = bpf_fd_array_map_lookup_elem(map, key, value);
236 } else if (IS_FD_HASH(map)) {
237 err = bpf_fd_htab_map_lookup_elem(map, key, value);
238 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
239 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
240 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
241 map->map_type == BPF_MAP_TYPE_STACK) {
242 err = map->ops->map_peek_elem(map, value);
243 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
244 /* struct_ops map requires directly updating "value" */
245 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
246 } else {
247 rcu_read_lock();
248 if (map->ops->map_lookup_elem_sys_only)
249 ptr = map->ops->map_lookup_elem_sys_only(map, key);
250 else
251 ptr = map->ops->map_lookup_elem(map, key);
252 if (IS_ERR(ptr)) {
253 err = PTR_ERR(ptr);
254 } else if (!ptr) {
255 err = -ENOENT;
256 } else {
257 err = 0;
258 if (flags & BPF_F_LOCK)
259 /* lock 'ptr' and copy everything but lock */
260 copy_map_value_locked(map, value, ptr, true);
261 else
262 copy_map_value(map, value, ptr);
263 /* mask lock, since value wasn't zero inited */
264 check_and_init_map_lock(map, value);
265 }
266 rcu_read_unlock();
267 }
268
269 bpf_enable_instrumentation();
270 maybe_wait_bpf_programs(map);
271
272 return err;
273}
274
275/* Please, do not use this function outside from the map creation path
276 * (e.g. in map update path) without taking care of setting the active
277 * memory cgroup (see at bpf_map_kmalloc_node() for example).
278 */
279static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
280{
281 /* We really just want to fail instead of triggering OOM killer
282 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
283 * which is used for lower order allocation requests.
284 *
285 * It has been observed that higher order allocation requests done by
286 * vmalloc with __GFP_NORETRY being set might fail due to not trying
287 * to reclaim memory from the page cache, thus we set
288 * __GFP_RETRY_MAYFAIL to avoid such situations.
289 */
290
291 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
292 unsigned int flags = 0;
293 unsigned long align = 1;
294 void *area;
295
296 if (size >= SIZE_MAX)
297 return NULL;
298
299 /* kmalloc()'ed memory can't be mmap()'ed */
300 if (mmapable) {
301 BUG_ON(!PAGE_ALIGNED(size));
302 align = SHMLBA;
303 flags = VM_USERMAP;
304 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
305 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
306 numa_node);
307 if (area != NULL)
308 return area;
309 }
310
311 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
312 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
313 flags, numa_node, __builtin_return_address(0));
314}
315
316void *bpf_map_area_alloc(u64 size, int numa_node)
317{
318 return __bpf_map_area_alloc(size, numa_node, false);
319}
320
321void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
322{
323 return __bpf_map_area_alloc(size, numa_node, true);
324}
325
326void bpf_map_area_free(void *area)
327{
328 kvfree(area);
329}
330
331static u32 bpf_map_flags_retain_permanent(u32 flags)
332{
333 /* Some map creation flags are not tied to the map object but
334 * rather to the map fd instead, so they have no meaning upon
335 * map object inspection since multiple file descriptors with
336 * different (access) properties can exist here. Thus, given
337 * this has zero meaning for the map itself, lets clear these
338 * from here.
339 */
340 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
341}
342
343void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
344{
345 map->map_type = attr->map_type;
346 map->key_size = attr->key_size;
347 map->value_size = attr->value_size;
348 map->max_entries = attr->max_entries;
349 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
350 map->numa_node = bpf_map_attr_numa_node(attr);
351}
352
353static int bpf_map_alloc_id(struct bpf_map *map)
354{
355 int id;
356
357 idr_preload(GFP_KERNEL);
358 spin_lock_bh(&map_idr_lock);
359 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
360 if (id > 0)
361 map->id = id;
362 spin_unlock_bh(&map_idr_lock);
363 idr_preload_end();
364
365 if (WARN_ON_ONCE(!id))
366 return -ENOSPC;
367
368 return id > 0 ? 0 : id;
369}
370
371void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
372{
373 unsigned long flags;
374
375 /* Offloaded maps are removed from the IDR store when their device
376 * disappears - even if someone holds an fd to them they are unusable,
377 * the memory is gone, all ops will fail; they are simply waiting for
378 * refcnt to drop to be freed.
379 */
380 if (!map->id)
381 return;
382
383 if (do_idr_lock)
384 spin_lock_irqsave(&map_idr_lock, flags);
385 else
386 __acquire(&map_idr_lock);
387
388 idr_remove(&map_idr, map->id);
389 map->id = 0;
390
391 if (do_idr_lock)
392 spin_unlock_irqrestore(&map_idr_lock, flags);
393 else
394 __release(&map_idr_lock);
395}
396
397#ifdef CONFIG_MEMCG_KMEM
398static void bpf_map_save_memcg(struct bpf_map *map)
399{
400 map->memcg = get_mem_cgroup_from_mm(current->mm);
401}
402
403static void bpf_map_release_memcg(struct bpf_map *map)
404{
405 mem_cgroup_put(map->memcg);
406}
407
408void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
409 int node)
410{
411 struct mem_cgroup *old_memcg;
412 void *ptr;
413
414 old_memcg = set_active_memcg(map->memcg);
415 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
416 set_active_memcg(old_memcg);
417
418 return ptr;
419}
420
421void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
422{
423 struct mem_cgroup *old_memcg;
424 void *ptr;
425
426 old_memcg = set_active_memcg(map->memcg);
427 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
428 set_active_memcg(old_memcg);
429
430 return ptr;
431}
432
433void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
434 size_t align, gfp_t flags)
435{
436 struct mem_cgroup *old_memcg;
437 void __percpu *ptr;
438
439 old_memcg = set_active_memcg(map->memcg);
440 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
441 set_active_memcg(old_memcg);
442
443 return ptr;
444}
445
446#else
447static void bpf_map_save_memcg(struct bpf_map *map)
448{
449}
450
451static void bpf_map_release_memcg(struct bpf_map *map)
452{
453}
454#endif
455
456/* called from workqueue */
457static void bpf_map_free_deferred(struct work_struct *work)
458{
459 struct bpf_map *map = container_of(work, struct bpf_map, work);
460
461 security_bpf_map_free(map);
462 bpf_map_release_memcg(map);
463 /* implementation dependent freeing */
464 map->ops->map_free(map);
465}
466
467static void bpf_map_put_uref(struct bpf_map *map)
468{
469 if (atomic64_dec_and_test(&map->usercnt)) {
470 if (map->ops->map_release_uref)
471 map->ops->map_release_uref(map);
472 }
473}
474
475/* decrement map refcnt and schedule it for freeing via workqueue
476 * (unrelying map implementation ops->map_free() might sleep)
477 */
478static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
479{
480 if (atomic64_dec_and_test(&map->refcnt)) {
481 /* bpf_map_free_id() must be called first */
482 bpf_map_free_id(map, do_idr_lock);
483 btf_put(map->btf);
484 INIT_WORK(&map->work, bpf_map_free_deferred);
485 schedule_work(&map->work);
486 }
487}
488
489void bpf_map_put(struct bpf_map *map)
490{
491 __bpf_map_put(map, true);
492}
493EXPORT_SYMBOL_GPL(bpf_map_put);
494
495void bpf_map_put_with_uref(struct bpf_map *map)
496{
497 bpf_map_put_uref(map);
498 bpf_map_put(map);
499}
500
501static int bpf_map_release(struct inode *inode, struct file *filp)
502{
503 struct bpf_map *map = filp->private_data;
504
505 if (map->ops->map_release)
506 map->ops->map_release(map, filp);
507
508 bpf_map_put_with_uref(map);
509 return 0;
510}
511
512static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
513{
514 fmode_t mode = f.file->f_mode;
515
516 /* Our file permissions may have been overridden by global
517 * map permissions facing syscall side.
518 */
519 if (READ_ONCE(map->frozen))
520 mode &= ~FMODE_CAN_WRITE;
521 return mode;
522}
523
524#ifdef CONFIG_PROC_FS
525/* Provides an approximation of the map's memory footprint.
526 * Used only to provide a backward compatibility and display
527 * a reasonable "memlock" info.
528 */
529static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
530{
531 unsigned long size;
532
533 size = round_up(map->key_size + bpf_map_value_size(map), 8);
534
535 return round_up(map->max_entries * size, PAGE_SIZE);
536}
537
538static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
539{
540 const struct bpf_map *map = filp->private_data;
541 const struct bpf_array *array;
542 u32 type = 0, jited = 0;
543
544 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
545 array = container_of(map, struct bpf_array, map);
546 type = array->aux->type;
547 jited = array->aux->jited;
548 }
549
550 seq_printf(m,
551 "map_type:\t%u\n"
552 "key_size:\t%u\n"
553 "value_size:\t%u\n"
554 "max_entries:\t%u\n"
555 "map_flags:\t%#x\n"
556 "memlock:\t%lu\n"
557 "map_id:\t%u\n"
558 "frozen:\t%u\n",
559 map->map_type,
560 map->key_size,
561 map->value_size,
562 map->max_entries,
563 map->map_flags,
564 bpf_map_memory_footprint(map),
565 map->id,
566 READ_ONCE(map->frozen));
567 if (type) {
568 seq_printf(m, "owner_prog_type:\t%u\n", type);
569 seq_printf(m, "owner_jited:\t%u\n", jited);
570 }
571}
572#endif
573
574static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
575 loff_t *ppos)
576{
577 /* We need this handler such that alloc_file() enables
578 * f_mode with FMODE_CAN_READ.
579 */
580 return -EINVAL;
581}
582
583static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
584 size_t siz, loff_t *ppos)
585{
586 /* We need this handler such that alloc_file() enables
587 * f_mode with FMODE_CAN_WRITE.
588 */
589 return -EINVAL;
590}
591
592/* called for any extra memory-mapped regions (except initial) */
593static void bpf_map_mmap_open(struct vm_area_struct *vma)
594{
595 struct bpf_map *map = vma->vm_file->private_data;
596
597 if (vma->vm_flags & VM_MAYWRITE) {
598 mutex_lock(&map->freeze_mutex);
599 map->writecnt++;
600 mutex_unlock(&map->freeze_mutex);
601 }
602}
603
604/* called for all unmapped memory region (including initial) */
605static void bpf_map_mmap_close(struct vm_area_struct *vma)
606{
607 struct bpf_map *map = vma->vm_file->private_data;
608
609 if (vma->vm_flags & VM_MAYWRITE) {
610 mutex_lock(&map->freeze_mutex);
611 map->writecnt--;
612 mutex_unlock(&map->freeze_mutex);
613 }
614}
615
616static const struct vm_operations_struct bpf_map_default_vmops = {
617 .open = bpf_map_mmap_open,
618 .close = bpf_map_mmap_close,
619};
620
621static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
622{
623 struct bpf_map *map = filp->private_data;
624 int err;
625
626 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
627 return -ENOTSUPP;
628
629 if (!(vma->vm_flags & VM_SHARED))
630 return -EINVAL;
631
632 mutex_lock(&map->freeze_mutex);
633
634 if (vma->vm_flags & VM_WRITE) {
635 if (map->frozen) {
636 err = -EPERM;
637 goto out;
638 }
639 /* map is meant to be read-only, so do not allow mapping as
640 * writable, because it's possible to leak a writable page
641 * reference and allows user-space to still modify it after
642 * freezing, while verifier will assume contents do not change
643 */
644 if (map->map_flags & BPF_F_RDONLY_PROG) {
645 err = -EACCES;
646 goto out;
647 }
648 }
649
650 /* set default open/close callbacks */
651 vma->vm_ops = &bpf_map_default_vmops;
652 vma->vm_private_data = map;
653 vma->vm_flags &= ~VM_MAYEXEC;
654 if (!(vma->vm_flags & VM_WRITE))
655 /* disallow re-mapping with PROT_WRITE */
656 vma->vm_flags &= ~VM_MAYWRITE;
657
658 err = map->ops->map_mmap(map, vma);
659 if (err)
660 goto out;
661
662 if (vma->vm_flags & VM_MAYWRITE)
663 map->writecnt++;
664out:
665 mutex_unlock(&map->freeze_mutex);
666 return err;
667}
668
669static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
670{
671 struct bpf_map *map = filp->private_data;
672
673 if (map->ops->map_poll)
674 return map->ops->map_poll(map, filp, pts);
675
676 return EPOLLERR;
677}
678
679const struct file_operations bpf_map_fops = {
680#ifdef CONFIG_PROC_FS
681 .show_fdinfo = bpf_map_show_fdinfo,
682#endif
683 .release = bpf_map_release,
684 .read = bpf_dummy_read,
685 .write = bpf_dummy_write,
686 .mmap = bpf_map_mmap,
687 .poll = bpf_map_poll,
688};
689
690int bpf_map_new_fd(struct bpf_map *map, int flags)
691{
692 int ret;
693
694 ret = security_bpf_map(map, OPEN_FMODE(flags));
695 if (ret < 0)
696 return ret;
697
698 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
699 flags | O_CLOEXEC);
700}
701
702int bpf_get_file_flag(int flags)
703{
704 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
705 return -EINVAL;
706 if (flags & BPF_F_RDONLY)
707 return O_RDONLY;
708 if (flags & BPF_F_WRONLY)
709 return O_WRONLY;
710 return O_RDWR;
711}
712
713/* helper macro to check that unused fields 'union bpf_attr' are zero */
714#define CHECK_ATTR(CMD) \
715 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
716 sizeof(attr->CMD##_LAST_FIELD), 0, \
717 sizeof(*attr) - \
718 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
719 sizeof(attr->CMD##_LAST_FIELD)) != NULL
720
721/* dst and src must have at least "size" number of bytes.
722 * Return strlen on success and < 0 on error.
723 */
724int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
725{
726 const char *end = src + size;
727 const char *orig_src = src;
728
729 memset(dst, 0, size);
730 /* Copy all isalnum(), '_' and '.' chars. */
731 while (src < end && *src) {
732 if (!isalnum(*src) &&
733 *src != '_' && *src != '.')
734 return -EINVAL;
735 *dst++ = *src++;
736 }
737
738 /* No '\0' found in "size" number of bytes */
739 if (src == end)
740 return -EINVAL;
741
742 return src - orig_src;
743}
744
745int map_check_no_btf(const struct bpf_map *map,
746 const struct btf *btf,
747 const struct btf_type *key_type,
748 const struct btf_type *value_type)
749{
750 return -ENOTSUPP;
751}
752
753static int map_check_btf(struct bpf_map *map, const struct btf *btf,
754 u32 btf_key_id, u32 btf_value_id)
755{
756 const struct btf_type *key_type, *value_type;
757 u32 key_size, value_size;
758 int ret = 0;
759
760 /* Some maps allow key to be unspecified. */
761 if (btf_key_id) {
762 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
763 if (!key_type || key_size != map->key_size)
764 return -EINVAL;
765 } else {
766 key_type = btf_type_by_id(btf, 0);
767 if (!map->ops->map_check_btf)
768 return -EINVAL;
769 }
770
771 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
772 if (!value_type || value_size != map->value_size)
773 return -EINVAL;
774
775 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
776
777 if (map_value_has_spin_lock(map)) {
778 if (map->map_flags & BPF_F_RDONLY_PROG)
779 return -EACCES;
780 if (map->map_type != BPF_MAP_TYPE_HASH &&
781 map->map_type != BPF_MAP_TYPE_ARRAY &&
782 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
783 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
784 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
785 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
786 return -ENOTSUPP;
787 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
788 map->value_size) {
789 WARN_ONCE(1,
790 "verifier bug spin_lock_off %d value_size %d\n",
791 map->spin_lock_off, map->value_size);
792 return -EFAULT;
793 }
794 }
795
796 if (map->ops->map_check_btf)
797 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
798
799 return ret;
800}
801
802#define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
803/* called via syscall */
804static int map_create(union bpf_attr *attr)
805{
806 int numa_node = bpf_map_attr_numa_node(attr);
807 struct bpf_map *map;
808 int f_flags;
809 int err;
810
811 err = CHECK_ATTR(BPF_MAP_CREATE);
812 if (err)
813 return -EINVAL;
814
815 if (attr->btf_vmlinux_value_type_id) {
816 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
817 attr->btf_key_type_id || attr->btf_value_type_id)
818 return -EINVAL;
819 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
820 return -EINVAL;
821 }
822
823 f_flags = bpf_get_file_flag(attr->map_flags);
824 if (f_flags < 0)
825 return f_flags;
826
827 if (numa_node != NUMA_NO_NODE &&
828 ((unsigned int)numa_node >= nr_node_ids ||
829 !node_online(numa_node)))
830 return -EINVAL;
831
832 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
833 map = find_and_alloc_map(attr);
834 if (IS_ERR(map))
835 return PTR_ERR(map);
836
837 err = bpf_obj_name_cpy(map->name, attr->map_name,
838 sizeof(attr->map_name));
839 if (err < 0)
840 goto free_map;
841
842 atomic64_set(&map->refcnt, 1);
843 atomic64_set(&map->usercnt, 1);
844 mutex_init(&map->freeze_mutex);
845
846 map->spin_lock_off = -EINVAL;
847 if (attr->btf_key_type_id || attr->btf_value_type_id ||
848 /* Even the map's value is a kernel's struct,
849 * the bpf_prog.o must have BTF to begin with
850 * to figure out the corresponding kernel's
851 * counter part. Thus, attr->btf_fd has
852 * to be valid also.
853 */
854 attr->btf_vmlinux_value_type_id) {
855 struct btf *btf;
856
857 btf = btf_get_by_fd(attr->btf_fd);
858 if (IS_ERR(btf)) {
859 err = PTR_ERR(btf);
860 goto free_map;
861 }
862 if (btf_is_kernel(btf)) {
863 btf_put(btf);
864 err = -EACCES;
865 goto free_map;
866 }
867 map->btf = btf;
868
869 if (attr->btf_value_type_id) {
870 err = map_check_btf(map, btf, attr->btf_key_type_id,
871 attr->btf_value_type_id);
872 if (err)
873 goto free_map;
874 }
875
876 map->btf_key_type_id = attr->btf_key_type_id;
877 map->btf_value_type_id = attr->btf_value_type_id;
878 map->btf_vmlinux_value_type_id =
879 attr->btf_vmlinux_value_type_id;
880 }
881
882 err = security_bpf_map_alloc(map);
883 if (err)
884 goto free_map;
885
886 err = bpf_map_alloc_id(map);
887 if (err)
888 goto free_map_sec;
889
890 bpf_map_save_memcg(map);
891
892 err = bpf_map_new_fd(map, f_flags);
893 if (err < 0) {
894 /* failed to allocate fd.
895 * bpf_map_put_with_uref() is needed because the above
896 * bpf_map_alloc_id() has published the map
897 * to the userspace and the userspace may
898 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
899 */
900 bpf_map_put_with_uref(map);
901 return err;
902 }
903
904 return err;
905
906free_map_sec:
907 security_bpf_map_free(map);
908free_map:
909 btf_put(map->btf);
910 map->ops->map_free(map);
911 return err;
912}
913
914/* if error is returned, fd is released.
915 * On success caller should complete fd access with matching fdput()
916 */
917struct bpf_map *__bpf_map_get(struct fd f)
918{
919 if (!f.file)
920 return ERR_PTR(-EBADF);
921 if (f.file->f_op != &bpf_map_fops) {
922 fdput(f);
923 return ERR_PTR(-EINVAL);
924 }
925
926 return f.file->private_data;
927}
928
929void bpf_map_inc(struct bpf_map *map)
930{
931 atomic64_inc(&map->refcnt);
932}
933EXPORT_SYMBOL_GPL(bpf_map_inc);
934
935void bpf_map_inc_with_uref(struct bpf_map *map)
936{
937 atomic64_inc(&map->refcnt);
938 atomic64_inc(&map->usercnt);
939}
940EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
941
942struct bpf_map *bpf_map_get(u32 ufd)
943{
944 struct fd f = fdget(ufd);
945 struct bpf_map *map;
946
947 map = __bpf_map_get(f);
948 if (IS_ERR(map))
949 return map;
950
951 bpf_map_inc(map);
952 fdput(f);
953
954 return map;
955}
956
957struct bpf_map *bpf_map_get_with_uref(u32 ufd)
958{
959 struct fd f = fdget(ufd);
960 struct bpf_map *map;
961
962 map = __bpf_map_get(f);
963 if (IS_ERR(map))
964 return map;
965
966 bpf_map_inc_with_uref(map);
967 fdput(f);
968
969 return map;
970}
971
972/* map_idr_lock should have been held */
973static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
974{
975 int refold;
976
977 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
978 if (!refold)
979 return ERR_PTR(-ENOENT);
980 if (uref)
981 atomic64_inc(&map->usercnt);
982
983 return map;
984}
985
986struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
987{
988 spin_lock_bh(&map_idr_lock);
989 map = __bpf_map_inc_not_zero(map, false);
990 spin_unlock_bh(&map_idr_lock);
991
992 return map;
993}
994EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
995
996int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
997{
998 return -ENOTSUPP;
999}
1000
1001static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1002{
1003 if (key_size)
1004 return memdup_user(ukey, key_size);
1005
1006 if (ukey)
1007 return ERR_PTR(-EINVAL);
1008
1009 return NULL;
1010}
1011
1012static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1013{
1014 if (key_size)
1015 return memdup_bpfptr(ukey, key_size);
1016
1017 if (!bpfptr_is_null(ukey))
1018 return ERR_PTR(-EINVAL);
1019
1020 return NULL;
1021}
1022
1023/* last field in 'union bpf_attr' used by this command */
1024#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1025
1026static int map_lookup_elem(union bpf_attr *attr)
1027{
1028 void __user *ukey = u64_to_user_ptr(attr->key);
1029 void __user *uvalue = u64_to_user_ptr(attr->value);
1030 int ufd = attr->map_fd;
1031 struct bpf_map *map;
1032 void *key, *value;
1033 u32 value_size;
1034 struct fd f;
1035 int err;
1036
1037 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1038 return -EINVAL;
1039
1040 if (attr->flags & ~BPF_F_LOCK)
1041 return -EINVAL;
1042
1043 f = fdget(ufd);
1044 map = __bpf_map_get(f);
1045 if (IS_ERR(map))
1046 return PTR_ERR(map);
1047 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1048 err = -EPERM;
1049 goto err_put;
1050 }
1051
1052 if ((attr->flags & BPF_F_LOCK) &&
1053 !map_value_has_spin_lock(map)) {
1054 err = -EINVAL;
1055 goto err_put;
1056 }
1057
1058 key = __bpf_copy_key(ukey, map->key_size);
1059 if (IS_ERR(key)) {
1060 err = PTR_ERR(key);
1061 goto err_put;
1062 }
1063
1064 value_size = bpf_map_value_size(map);
1065
1066 err = -ENOMEM;
1067 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1068 if (!value)
1069 goto free_key;
1070
1071 err = bpf_map_copy_value(map, key, value, attr->flags);
1072 if (err)
1073 goto free_value;
1074
1075 err = -EFAULT;
1076 if (copy_to_user(uvalue, value, value_size) != 0)
1077 goto free_value;
1078
1079 err = 0;
1080
1081free_value:
1082 kfree(value);
1083free_key:
1084 kfree(key);
1085err_put:
1086 fdput(f);
1087 return err;
1088}
1089
1090
1091#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1092
1093static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
1094{
1095 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1096 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
1097 int ufd = attr->map_fd;
1098 struct bpf_map *map;
1099 void *key, *value;
1100 u32 value_size;
1101 struct fd f;
1102 int err;
1103
1104 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1105 return -EINVAL;
1106
1107 f = fdget(ufd);
1108 map = __bpf_map_get(f);
1109 if (IS_ERR(map))
1110 return PTR_ERR(map);
1111 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1112 err = -EPERM;
1113 goto err_put;
1114 }
1115
1116 if ((attr->flags & BPF_F_LOCK) &&
1117 !map_value_has_spin_lock(map)) {
1118 err = -EINVAL;
1119 goto err_put;
1120 }
1121
1122 key = ___bpf_copy_key(ukey, map->key_size);
1123 if (IS_ERR(key)) {
1124 err = PTR_ERR(key);
1125 goto err_put;
1126 }
1127
1128 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1129 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
1130 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1131 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
1132 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1133 else
1134 value_size = map->value_size;
1135
1136 err = -ENOMEM;
1137 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1138 if (!value)
1139 goto free_key;
1140
1141 err = -EFAULT;
1142 if (copy_from_bpfptr(value, uvalue, value_size) != 0)
1143 goto free_value;
1144
1145 err = bpf_map_update_value(map, f, key, value, attr->flags);
1146
1147free_value:
1148 kfree(value);
1149free_key:
1150 kfree(key);
1151err_put:
1152 fdput(f);
1153 return err;
1154}
1155
1156#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1157
1158static int map_delete_elem(union bpf_attr *attr)
1159{
1160 void __user *ukey = u64_to_user_ptr(attr->key);
1161 int ufd = attr->map_fd;
1162 struct bpf_map *map;
1163 struct fd f;
1164 void *key;
1165 int err;
1166
1167 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1168 return -EINVAL;
1169
1170 f = fdget(ufd);
1171 map = __bpf_map_get(f);
1172 if (IS_ERR(map))
1173 return PTR_ERR(map);
1174 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1175 err = -EPERM;
1176 goto err_put;
1177 }
1178
1179 key = __bpf_copy_key(ukey, map->key_size);
1180 if (IS_ERR(key)) {
1181 err = PTR_ERR(key);
1182 goto err_put;
1183 }
1184
1185 if (bpf_map_is_dev_bound(map)) {
1186 err = bpf_map_offload_delete_elem(map, key);
1187 goto out;
1188 } else if (IS_FD_PROG_ARRAY(map) ||
1189 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1190 /* These maps require sleepable context */
1191 err = map->ops->map_delete_elem(map, key);
1192 goto out;
1193 }
1194
1195 bpf_disable_instrumentation();
1196 rcu_read_lock();
1197 err = map->ops->map_delete_elem(map, key);
1198 rcu_read_unlock();
1199 bpf_enable_instrumentation();
1200 maybe_wait_bpf_programs(map);
1201out:
1202 kfree(key);
1203err_put:
1204 fdput(f);
1205 return err;
1206}
1207
1208/* last field in 'union bpf_attr' used by this command */
1209#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1210
1211static int map_get_next_key(union bpf_attr *attr)
1212{
1213 void __user *ukey = u64_to_user_ptr(attr->key);
1214 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1215 int ufd = attr->map_fd;
1216 struct bpf_map *map;
1217 void *key, *next_key;
1218 struct fd f;
1219 int err;
1220
1221 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1222 return -EINVAL;
1223
1224 f = fdget(ufd);
1225 map = __bpf_map_get(f);
1226 if (IS_ERR(map))
1227 return PTR_ERR(map);
1228 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1229 err = -EPERM;
1230 goto err_put;
1231 }
1232
1233 if (ukey) {
1234 key = __bpf_copy_key(ukey, map->key_size);
1235 if (IS_ERR(key)) {
1236 err = PTR_ERR(key);
1237 goto err_put;
1238 }
1239 } else {
1240 key = NULL;
1241 }
1242
1243 err = -ENOMEM;
1244 next_key = kmalloc(map->key_size, GFP_USER);
1245 if (!next_key)
1246 goto free_key;
1247
1248 if (bpf_map_is_dev_bound(map)) {
1249 err = bpf_map_offload_get_next_key(map, key, next_key);
1250 goto out;
1251 }
1252
1253 rcu_read_lock();
1254 err = map->ops->map_get_next_key(map, key, next_key);
1255 rcu_read_unlock();
1256out:
1257 if (err)
1258 goto free_next_key;
1259
1260 err = -EFAULT;
1261 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1262 goto free_next_key;
1263
1264 err = 0;
1265
1266free_next_key:
1267 kfree(next_key);
1268free_key:
1269 kfree(key);
1270err_put:
1271 fdput(f);
1272 return err;
1273}
1274
1275int generic_map_delete_batch(struct bpf_map *map,
1276 const union bpf_attr *attr,
1277 union bpf_attr __user *uattr)
1278{
1279 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1280 u32 cp, max_count;
1281 int err = 0;
1282 void *key;
1283
1284 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1285 return -EINVAL;
1286
1287 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1288 !map_value_has_spin_lock(map)) {
1289 return -EINVAL;
1290 }
1291
1292 max_count = attr->batch.count;
1293 if (!max_count)
1294 return 0;
1295
1296 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1297 if (!key)
1298 return -ENOMEM;
1299
1300 for (cp = 0; cp < max_count; cp++) {
1301 err = -EFAULT;
1302 if (copy_from_user(key, keys + cp * map->key_size,
1303 map->key_size))
1304 break;
1305
1306 if (bpf_map_is_dev_bound(map)) {
1307 err = bpf_map_offload_delete_elem(map, key);
1308 break;
1309 }
1310
1311 bpf_disable_instrumentation();
1312 rcu_read_lock();
1313 err = map->ops->map_delete_elem(map, key);
1314 rcu_read_unlock();
1315 bpf_enable_instrumentation();
1316 maybe_wait_bpf_programs(map);
1317 if (err)
1318 break;
1319 }
1320 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1321 err = -EFAULT;
1322
1323 kfree(key);
1324 return err;
1325}
1326
1327int generic_map_update_batch(struct bpf_map *map,
1328 const union bpf_attr *attr,
1329 union bpf_attr __user *uattr)
1330{
1331 void __user *values = u64_to_user_ptr(attr->batch.values);
1332 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1333 u32 value_size, cp, max_count;
1334 int ufd = attr->map_fd;
1335 void *key, *value;
1336 struct fd f;
1337 int err = 0;
1338
1339 f = fdget(ufd);
1340 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1341 return -EINVAL;
1342
1343 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1344 !map_value_has_spin_lock(map)) {
1345 return -EINVAL;
1346 }
1347
1348 value_size = bpf_map_value_size(map);
1349
1350 max_count = attr->batch.count;
1351 if (!max_count)
1352 return 0;
1353
1354 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1355 if (!key)
1356 return -ENOMEM;
1357
1358 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1359 if (!value) {
1360 kfree(key);
1361 return -ENOMEM;
1362 }
1363
1364 for (cp = 0; cp < max_count; cp++) {
1365 err = -EFAULT;
1366 if (copy_from_user(key, keys + cp * map->key_size,
1367 map->key_size) ||
1368 copy_from_user(value, values + cp * value_size, value_size))
1369 break;
1370
1371 err = bpf_map_update_value(map, f, key, value,
1372 attr->batch.elem_flags);
1373
1374 if (err)
1375 break;
1376 }
1377
1378 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1379 err = -EFAULT;
1380
1381 kfree(value);
1382 kfree(key);
1383 return err;
1384}
1385
1386#define MAP_LOOKUP_RETRIES 3
1387
1388int generic_map_lookup_batch(struct bpf_map *map,
1389 const union bpf_attr *attr,
1390 union bpf_attr __user *uattr)
1391{
1392 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1393 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1394 void __user *values = u64_to_user_ptr(attr->batch.values);
1395 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1396 void *buf, *buf_prevkey, *prev_key, *key, *value;
1397 int err, retry = MAP_LOOKUP_RETRIES;
1398 u32 value_size, cp, max_count;
1399
1400 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1401 return -EINVAL;
1402
1403 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1404 !map_value_has_spin_lock(map))
1405 return -EINVAL;
1406
1407 value_size = bpf_map_value_size(map);
1408
1409 max_count = attr->batch.count;
1410 if (!max_count)
1411 return 0;
1412
1413 if (put_user(0, &uattr->batch.count))
1414 return -EFAULT;
1415
1416 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1417 if (!buf_prevkey)
1418 return -ENOMEM;
1419
1420 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1421 if (!buf) {
1422 kfree(buf_prevkey);
1423 return -ENOMEM;
1424 }
1425
1426 err = -EFAULT;
1427 prev_key = NULL;
1428 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1429 goto free_buf;
1430 key = buf;
1431 value = key + map->key_size;
1432 if (ubatch)
1433 prev_key = buf_prevkey;
1434
1435 for (cp = 0; cp < max_count;) {
1436 rcu_read_lock();
1437 err = map->ops->map_get_next_key(map, prev_key, key);
1438 rcu_read_unlock();
1439 if (err)
1440 break;
1441 err = bpf_map_copy_value(map, key, value,
1442 attr->batch.elem_flags);
1443
1444 if (err == -ENOENT) {
1445 if (retry) {
1446 retry--;
1447 continue;
1448 }
1449 err = -EINTR;
1450 break;
1451 }
1452
1453 if (err)
1454 goto free_buf;
1455
1456 if (copy_to_user(keys + cp * map->key_size, key,
1457 map->key_size)) {
1458 err = -EFAULT;
1459 goto free_buf;
1460 }
1461 if (copy_to_user(values + cp * value_size, value, value_size)) {
1462 err = -EFAULT;
1463 goto free_buf;
1464 }
1465
1466 if (!prev_key)
1467 prev_key = buf_prevkey;
1468
1469 swap(prev_key, key);
1470 retry = MAP_LOOKUP_RETRIES;
1471 cp++;
1472 }
1473
1474 if (err == -EFAULT)
1475 goto free_buf;
1476
1477 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1478 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1479 err = -EFAULT;
1480
1481free_buf:
1482 kfree(buf_prevkey);
1483 kfree(buf);
1484 return err;
1485}
1486
1487#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
1488
1489static int map_lookup_and_delete_elem(union bpf_attr *attr)
1490{
1491 void __user *ukey = u64_to_user_ptr(attr->key);
1492 void __user *uvalue = u64_to_user_ptr(attr->value);
1493 int ufd = attr->map_fd;
1494 struct bpf_map *map;
1495 void *key, *value;
1496 u32 value_size;
1497 struct fd f;
1498 int err;
1499
1500 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1501 return -EINVAL;
1502
1503 if (attr->flags & ~BPF_F_LOCK)
1504 return -EINVAL;
1505
1506 f = fdget(ufd);
1507 map = __bpf_map_get(f);
1508 if (IS_ERR(map))
1509 return PTR_ERR(map);
1510 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1511 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1512 err = -EPERM;
1513 goto err_put;
1514 }
1515
1516 if (attr->flags &&
1517 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1518 map->map_type == BPF_MAP_TYPE_STACK)) {
1519 err = -EINVAL;
1520 goto err_put;
1521 }
1522
1523 if ((attr->flags & BPF_F_LOCK) &&
1524 !map_value_has_spin_lock(map)) {
1525 err = -EINVAL;
1526 goto err_put;
1527 }
1528
1529 key = __bpf_copy_key(ukey, map->key_size);
1530 if (IS_ERR(key)) {
1531 err = PTR_ERR(key);
1532 goto err_put;
1533 }
1534
1535 value_size = bpf_map_value_size(map);
1536
1537 err = -ENOMEM;
1538 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1539 if (!value)
1540 goto free_key;
1541
1542 err = -ENOTSUPP;
1543 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1544 map->map_type == BPF_MAP_TYPE_STACK) {
1545 err = map->ops->map_pop_elem(map, value);
1546 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1547 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1548 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1549 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1550 if (!bpf_map_is_dev_bound(map)) {
1551 bpf_disable_instrumentation();
1552 rcu_read_lock();
1553 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1554 rcu_read_unlock();
1555 bpf_enable_instrumentation();
1556 }
1557 }
1558
1559 if (err)
1560 goto free_value;
1561
1562 if (copy_to_user(uvalue, value, value_size) != 0) {
1563 err = -EFAULT;
1564 goto free_value;
1565 }
1566
1567 err = 0;
1568
1569free_value:
1570 kfree(value);
1571free_key:
1572 kfree(key);
1573err_put:
1574 fdput(f);
1575 return err;
1576}
1577
1578#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1579
1580static int map_freeze(const union bpf_attr *attr)
1581{
1582 int err = 0, ufd = attr->map_fd;
1583 struct bpf_map *map;
1584 struct fd f;
1585
1586 if (CHECK_ATTR(BPF_MAP_FREEZE))
1587 return -EINVAL;
1588
1589 f = fdget(ufd);
1590 map = __bpf_map_get(f);
1591 if (IS_ERR(map))
1592 return PTR_ERR(map);
1593
1594 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1595 fdput(f);
1596 return -ENOTSUPP;
1597 }
1598
1599 mutex_lock(&map->freeze_mutex);
1600
1601 if (map->writecnt) {
1602 err = -EBUSY;
1603 goto err_put;
1604 }
1605 if (READ_ONCE(map->frozen)) {
1606 err = -EBUSY;
1607 goto err_put;
1608 }
1609 if (!bpf_capable()) {
1610 err = -EPERM;
1611 goto err_put;
1612 }
1613
1614 WRITE_ONCE(map->frozen, true);
1615err_put:
1616 mutex_unlock(&map->freeze_mutex);
1617 fdput(f);
1618 return err;
1619}
1620
1621static const struct bpf_prog_ops * const bpf_prog_types[] = {
1622#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1623 [_id] = & _name ## _prog_ops,
1624#define BPF_MAP_TYPE(_id, _ops)
1625#define BPF_LINK_TYPE(_id, _name)
1626#include <linux/bpf_types.h>
1627#undef BPF_PROG_TYPE
1628#undef BPF_MAP_TYPE
1629#undef BPF_LINK_TYPE
1630};
1631
1632static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1633{
1634 const struct bpf_prog_ops *ops;
1635
1636 if (type >= ARRAY_SIZE(bpf_prog_types))
1637 return -EINVAL;
1638 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1639 ops = bpf_prog_types[type];
1640 if (!ops)
1641 return -EINVAL;
1642
1643 if (!bpf_prog_is_dev_bound(prog->aux))
1644 prog->aux->ops = ops;
1645 else
1646 prog->aux->ops = &bpf_offload_prog_ops;
1647 prog->type = type;
1648 return 0;
1649}
1650
1651enum bpf_audit {
1652 BPF_AUDIT_LOAD,
1653 BPF_AUDIT_UNLOAD,
1654 BPF_AUDIT_MAX,
1655};
1656
1657static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1658 [BPF_AUDIT_LOAD] = "LOAD",
1659 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1660};
1661
1662static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1663{
1664 struct audit_context *ctx = NULL;
1665 struct audit_buffer *ab;
1666
1667 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1668 return;
1669 if (audit_enabled == AUDIT_OFF)
1670 return;
1671 if (op == BPF_AUDIT_LOAD)
1672 ctx = audit_context();
1673 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1674 if (unlikely(!ab))
1675 return;
1676 audit_log_format(ab, "prog-id=%u op=%s",
1677 prog->aux->id, bpf_audit_str[op]);
1678 audit_log_end(ab);
1679}
1680
1681static int bpf_prog_alloc_id(struct bpf_prog *prog)
1682{
1683 int id;
1684
1685 idr_preload(GFP_KERNEL);
1686 spin_lock_bh(&prog_idr_lock);
1687 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1688 if (id > 0)
1689 prog->aux->id = id;
1690 spin_unlock_bh(&prog_idr_lock);
1691 idr_preload_end();
1692
1693 /* id is in [1, INT_MAX) */
1694 if (WARN_ON_ONCE(!id))
1695 return -ENOSPC;
1696
1697 return id > 0 ? 0 : id;
1698}
1699
1700void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1701{
1702 /* cBPF to eBPF migrations are currently not in the idr store.
1703 * Offloaded programs are removed from the store when their device
1704 * disappears - even if someone grabs an fd to them they are unusable,
1705 * simply waiting for refcnt to drop to be freed.
1706 */
1707 if (!prog->aux->id)
1708 return;
1709
1710 if (do_idr_lock)
1711 spin_lock_bh(&prog_idr_lock);
1712 else
1713 __acquire(&prog_idr_lock);
1714
1715 idr_remove(&prog_idr, prog->aux->id);
1716 prog->aux->id = 0;
1717
1718 if (do_idr_lock)
1719 spin_unlock_bh(&prog_idr_lock);
1720 else
1721 __release(&prog_idr_lock);
1722}
1723
1724static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1725{
1726 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1727
1728 kvfree(aux->func_info);
1729 kfree(aux->func_info_aux);
1730 free_uid(aux->user);
1731 security_bpf_prog_free(aux);
1732 bpf_prog_free(aux->prog);
1733}
1734
1735static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1736{
1737 bpf_prog_kallsyms_del_all(prog);
1738 btf_put(prog->aux->btf);
1739 kvfree(prog->aux->jited_linfo);
1740 kvfree(prog->aux->linfo);
1741 kfree(prog->aux->kfunc_tab);
1742 if (prog->aux->attach_btf)
1743 btf_put(prog->aux->attach_btf);
1744
1745 if (deferred) {
1746 if (prog->aux->sleepable)
1747 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1748 else
1749 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1750 } else {
1751 __bpf_prog_put_rcu(&prog->aux->rcu);
1752 }
1753}
1754
1755static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1756{
1757 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
1758 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1759 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1760 /* bpf_prog_free_id() must be called first */
1761 bpf_prog_free_id(prog, do_idr_lock);
1762 __bpf_prog_put_noref(prog, true);
1763 }
1764}
1765
1766void bpf_prog_put(struct bpf_prog *prog)
1767{
1768 __bpf_prog_put(prog, true);
1769}
1770EXPORT_SYMBOL_GPL(bpf_prog_put);
1771
1772static int bpf_prog_release(struct inode *inode, struct file *filp)
1773{
1774 struct bpf_prog *prog = filp->private_data;
1775
1776 bpf_prog_put(prog);
1777 return 0;
1778}
1779
1780static void bpf_prog_get_stats(const struct bpf_prog *prog,
1781 struct bpf_prog_stats *stats)
1782{
1783 u64 nsecs = 0, cnt = 0, misses = 0;
1784 int cpu;
1785
1786 for_each_possible_cpu(cpu) {
1787 const struct bpf_prog_stats *st;
1788 unsigned int start;
1789 u64 tnsecs, tcnt, tmisses;
1790
1791 st = per_cpu_ptr(prog->stats, cpu);
1792 do {
1793 start = u64_stats_fetch_begin_irq(&st->syncp);
1794 tnsecs = st->nsecs;
1795 tcnt = st->cnt;
1796 tmisses = st->misses;
1797 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1798 nsecs += tnsecs;
1799 cnt += tcnt;
1800 misses += tmisses;
1801 }
1802 stats->nsecs = nsecs;
1803 stats->cnt = cnt;
1804 stats->misses = misses;
1805}
1806
1807#ifdef CONFIG_PROC_FS
1808static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1809{
1810 const struct bpf_prog *prog = filp->private_data;
1811 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1812 struct bpf_prog_stats stats;
1813
1814 bpf_prog_get_stats(prog, &stats);
1815 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1816 seq_printf(m,
1817 "prog_type:\t%u\n"
1818 "prog_jited:\t%u\n"
1819 "prog_tag:\t%s\n"
1820 "memlock:\t%llu\n"
1821 "prog_id:\t%u\n"
1822 "run_time_ns:\t%llu\n"
1823 "run_cnt:\t%llu\n"
1824 "recursion_misses:\t%llu\n",
1825 prog->type,
1826 prog->jited,
1827 prog_tag,
1828 prog->pages * 1ULL << PAGE_SHIFT,
1829 prog->aux->id,
1830 stats.nsecs,
1831 stats.cnt,
1832 stats.misses);
1833}
1834#endif
1835
1836const struct file_operations bpf_prog_fops = {
1837#ifdef CONFIG_PROC_FS
1838 .show_fdinfo = bpf_prog_show_fdinfo,
1839#endif
1840 .release = bpf_prog_release,
1841 .read = bpf_dummy_read,
1842 .write = bpf_dummy_write,
1843};
1844
1845int bpf_prog_new_fd(struct bpf_prog *prog)
1846{
1847 int ret;
1848
1849 ret = security_bpf_prog(prog);
1850 if (ret < 0)
1851 return ret;
1852
1853 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1854 O_RDWR | O_CLOEXEC);
1855}
1856
1857static struct bpf_prog *____bpf_prog_get(struct fd f)
1858{
1859 if (!f.file)
1860 return ERR_PTR(-EBADF);
1861 if (f.file->f_op != &bpf_prog_fops) {
1862 fdput(f);
1863 return ERR_PTR(-EINVAL);
1864 }
1865
1866 return f.file->private_data;
1867}
1868
1869void bpf_prog_add(struct bpf_prog *prog, int i)
1870{
1871 atomic64_add(i, &prog->aux->refcnt);
1872}
1873EXPORT_SYMBOL_GPL(bpf_prog_add);
1874
1875void bpf_prog_sub(struct bpf_prog *prog, int i)
1876{
1877 /* Only to be used for undoing previous bpf_prog_add() in some
1878 * error path. We still know that another entity in our call
1879 * path holds a reference to the program, thus atomic_sub() can
1880 * be safely used in such cases!
1881 */
1882 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1883}
1884EXPORT_SYMBOL_GPL(bpf_prog_sub);
1885
1886void bpf_prog_inc(struct bpf_prog *prog)
1887{
1888 atomic64_inc(&prog->aux->refcnt);
1889}
1890EXPORT_SYMBOL_GPL(bpf_prog_inc);
1891
1892/* prog_idr_lock should have been held */
1893struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1894{
1895 int refold;
1896
1897 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1898
1899 if (!refold)
1900 return ERR_PTR(-ENOENT);
1901
1902 return prog;
1903}
1904EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1905
1906bool bpf_prog_get_ok(struct bpf_prog *prog,
1907 enum bpf_prog_type *attach_type, bool attach_drv)
1908{
1909 /* not an attachment, just a refcount inc, always allow */
1910 if (!attach_type)
1911 return true;
1912
1913 if (prog->type != *attach_type)
1914 return false;
1915 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1916 return false;
1917
1918 return true;
1919}
1920
1921static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1922 bool attach_drv)
1923{
1924 struct fd f = fdget(ufd);
1925 struct bpf_prog *prog;
1926
1927 prog = ____bpf_prog_get(f);
1928 if (IS_ERR(prog))
1929 return prog;
1930 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1931 prog = ERR_PTR(-EINVAL);
1932 goto out;
1933 }
1934
1935 bpf_prog_inc(prog);
1936out:
1937 fdput(f);
1938 return prog;
1939}
1940
1941struct bpf_prog *bpf_prog_get(u32 ufd)
1942{
1943 return __bpf_prog_get(ufd, NULL, false);
1944}
1945
1946struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1947 bool attach_drv)
1948{
1949 return __bpf_prog_get(ufd, &type, attach_drv);
1950}
1951EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1952
1953/* Initially all BPF programs could be loaded w/o specifying
1954 * expected_attach_type. Later for some of them specifying expected_attach_type
1955 * at load time became required so that program could be validated properly.
1956 * Programs of types that are allowed to be loaded both w/ and w/o (for
1957 * backward compatibility) expected_attach_type, should have the default attach
1958 * type assigned to expected_attach_type for the latter case, so that it can be
1959 * validated later at attach time.
1960 *
1961 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1962 * prog type requires it but has some attach types that have to be backward
1963 * compatible.
1964 */
1965static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1966{
1967 switch (attr->prog_type) {
1968 case BPF_PROG_TYPE_CGROUP_SOCK:
1969 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1970 * exist so checking for non-zero is the way to go here.
1971 */
1972 if (!attr->expected_attach_type)
1973 attr->expected_attach_type =
1974 BPF_CGROUP_INET_SOCK_CREATE;
1975 break;
1976 case BPF_PROG_TYPE_SK_REUSEPORT:
1977 if (!attr->expected_attach_type)
1978 attr->expected_attach_type =
1979 BPF_SK_REUSEPORT_SELECT;
1980 break;
1981 }
1982}
1983
1984static int
1985bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1986 enum bpf_attach_type expected_attach_type,
1987 struct btf *attach_btf, u32 btf_id,
1988 struct bpf_prog *dst_prog)
1989{
1990 if (btf_id) {
1991 if (btf_id > BTF_MAX_TYPE)
1992 return -EINVAL;
1993
1994 if (!attach_btf && !dst_prog)
1995 return -EINVAL;
1996
1997 switch (prog_type) {
1998 case BPF_PROG_TYPE_TRACING:
1999 case BPF_PROG_TYPE_LSM:
2000 case BPF_PROG_TYPE_STRUCT_OPS:
2001 case BPF_PROG_TYPE_EXT:
2002 break;
2003 default:
2004 return -EINVAL;
2005 }
2006 }
2007
2008 if (attach_btf && (!btf_id || dst_prog))
2009 return -EINVAL;
2010
2011 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
2012 prog_type != BPF_PROG_TYPE_EXT)
2013 return -EINVAL;
2014
2015 switch (prog_type) {
2016 case BPF_PROG_TYPE_CGROUP_SOCK:
2017 switch (expected_attach_type) {
2018 case BPF_CGROUP_INET_SOCK_CREATE:
2019 case BPF_CGROUP_INET_SOCK_RELEASE:
2020 case BPF_CGROUP_INET4_POST_BIND:
2021 case BPF_CGROUP_INET6_POST_BIND:
2022 return 0;
2023 default:
2024 return -EINVAL;
2025 }
2026 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2027 switch (expected_attach_type) {
2028 case BPF_CGROUP_INET4_BIND:
2029 case BPF_CGROUP_INET6_BIND:
2030 case BPF_CGROUP_INET4_CONNECT:
2031 case BPF_CGROUP_INET6_CONNECT:
2032 case BPF_CGROUP_INET4_GETPEERNAME:
2033 case BPF_CGROUP_INET6_GETPEERNAME:
2034 case BPF_CGROUP_INET4_GETSOCKNAME:
2035 case BPF_CGROUP_INET6_GETSOCKNAME:
2036 case BPF_CGROUP_UDP4_SENDMSG:
2037 case BPF_CGROUP_UDP6_SENDMSG:
2038 case BPF_CGROUP_UDP4_RECVMSG:
2039 case BPF_CGROUP_UDP6_RECVMSG:
2040 return 0;
2041 default:
2042 return -EINVAL;
2043 }
2044 case BPF_PROG_TYPE_CGROUP_SKB:
2045 switch (expected_attach_type) {
2046 case BPF_CGROUP_INET_INGRESS:
2047 case BPF_CGROUP_INET_EGRESS:
2048 return 0;
2049 default:
2050 return -EINVAL;
2051 }
2052 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2053 switch (expected_attach_type) {
2054 case BPF_CGROUP_SETSOCKOPT:
2055 case BPF_CGROUP_GETSOCKOPT:
2056 return 0;
2057 default:
2058 return -EINVAL;
2059 }
2060 case BPF_PROG_TYPE_SK_LOOKUP:
2061 if (expected_attach_type == BPF_SK_LOOKUP)
2062 return 0;
2063 return -EINVAL;
2064 case BPF_PROG_TYPE_SK_REUSEPORT:
2065 switch (expected_attach_type) {
2066 case BPF_SK_REUSEPORT_SELECT:
2067 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2068 return 0;
2069 default:
2070 return -EINVAL;
2071 }
2072 case BPF_PROG_TYPE_SYSCALL:
2073 case BPF_PROG_TYPE_EXT:
2074 if (expected_attach_type)
2075 return -EINVAL;
2076 fallthrough;
2077 default:
2078 return 0;
2079 }
2080}
2081
2082static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2083{
2084 switch (prog_type) {
2085 case BPF_PROG_TYPE_SCHED_CLS:
2086 case BPF_PROG_TYPE_SCHED_ACT:
2087 case BPF_PROG_TYPE_XDP:
2088 case BPF_PROG_TYPE_LWT_IN:
2089 case BPF_PROG_TYPE_LWT_OUT:
2090 case BPF_PROG_TYPE_LWT_XMIT:
2091 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2092 case BPF_PROG_TYPE_SK_SKB:
2093 case BPF_PROG_TYPE_SK_MSG:
2094 case BPF_PROG_TYPE_LIRC_MODE2:
2095 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2096 case BPF_PROG_TYPE_CGROUP_DEVICE:
2097 case BPF_PROG_TYPE_CGROUP_SOCK:
2098 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2099 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2100 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2101 case BPF_PROG_TYPE_SOCK_OPS:
2102 case BPF_PROG_TYPE_EXT: /* extends any prog */
2103 return true;
2104 case BPF_PROG_TYPE_CGROUP_SKB:
2105 /* always unpriv */
2106 case BPF_PROG_TYPE_SK_REUSEPORT:
2107 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2108 default:
2109 return false;
2110 }
2111}
2112
2113static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2114{
2115 switch (prog_type) {
2116 case BPF_PROG_TYPE_KPROBE:
2117 case BPF_PROG_TYPE_TRACEPOINT:
2118 case BPF_PROG_TYPE_PERF_EVENT:
2119 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2120 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2121 case BPF_PROG_TYPE_TRACING:
2122 case BPF_PROG_TYPE_LSM:
2123 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2124 case BPF_PROG_TYPE_EXT: /* extends any prog */
2125 return true;
2126 default:
2127 return false;
2128 }
2129}
2130
2131/* last field in 'union bpf_attr' used by this command */
2132#define BPF_PROG_LOAD_LAST_FIELD fd_array
2133
2134static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
2135{
2136 enum bpf_prog_type type = attr->prog_type;
2137 struct bpf_prog *prog, *dst_prog = NULL;
2138 struct btf *attach_btf = NULL;
2139 int err;
2140 char license[128];
2141 bool is_gpl;
2142
2143 if (CHECK_ATTR(BPF_PROG_LOAD))
2144 return -EINVAL;
2145
2146 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2147 BPF_F_ANY_ALIGNMENT |
2148 BPF_F_TEST_STATE_FREQ |
2149 BPF_F_SLEEPABLE |
2150 BPF_F_TEST_RND_HI32))
2151 return -EINVAL;
2152
2153 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2154 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2155 !bpf_capable())
2156 return -EPERM;
2157
2158 /* copy eBPF program license from user space */
2159 if (strncpy_from_bpfptr(license,
2160 make_bpfptr(attr->license, uattr.is_kernel),
2161 sizeof(license) - 1) < 0)
2162 return -EFAULT;
2163 license[sizeof(license) - 1] = 0;
2164
2165 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2166 is_gpl = license_is_gpl_compatible(license);
2167
2168 if (attr->insn_cnt == 0 ||
2169 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2170 return -E2BIG;
2171 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2172 type != BPF_PROG_TYPE_CGROUP_SKB &&
2173 !bpf_capable())
2174 return -EPERM;
2175
2176 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2177 return -EPERM;
2178 if (is_perfmon_prog_type(type) && !perfmon_capable())
2179 return -EPERM;
2180
2181 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2182 * or btf, we need to check which one it is
2183 */
2184 if (attr->attach_prog_fd) {
2185 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2186 if (IS_ERR(dst_prog)) {
2187 dst_prog = NULL;
2188 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2189 if (IS_ERR(attach_btf))
2190 return -EINVAL;
2191 if (!btf_is_kernel(attach_btf)) {
2192 /* attaching through specifying bpf_prog's BTF
2193 * objects directly might be supported eventually
2194 */
2195 btf_put(attach_btf);
2196 return -ENOTSUPP;
2197 }
2198 }
2199 } else if (attr->attach_btf_id) {
2200 /* fall back to vmlinux BTF, if BTF type ID is specified */
2201 attach_btf = bpf_get_btf_vmlinux();
2202 if (IS_ERR(attach_btf))
2203 return PTR_ERR(attach_btf);
2204 if (!attach_btf)
2205 return -EINVAL;
2206 btf_get(attach_btf);
2207 }
2208
2209 bpf_prog_load_fixup_attach_type(attr);
2210 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2211 attach_btf, attr->attach_btf_id,
2212 dst_prog)) {
2213 if (dst_prog)
2214 bpf_prog_put(dst_prog);
2215 if (attach_btf)
2216 btf_put(attach_btf);
2217 return -EINVAL;
2218 }
2219
2220 /* plain bpf_prog allocation */
2221 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2222 if (!prog) {
2223 if (dst_prog)
2224 bpf_prog_put(dst_prog);
2225 if (attach_btf)
2226 btf_put(attach_btf);
2227 return -ENOMEM;
2228 }
2229
2230 prog->expected_attach_type = attr->expected_attach_type;
2231 prog->aux->attach_btf = attach_btf;
2232 prog->aux->attach_btf_id = attr->attach_btf_id;
2233 prog->aux->dst_prog = dst_prog;
2234 prog->aux->offload_requested = !!attr->prog_ifindex;
2235 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2236
2237 err = security_bpf_prog_alloc(prog->aux);
2238 if (err)
2239 goto free_prog;
2240
2241 prog->aux->user = get_current_user();
2242 prog->len = attr->insn_cnt;
2243
2244 err = -EFAULT;
2245 if (copy_from_bpfptr(prog->insns,
2246 make_bpfptr(attr->insns, uattr.is_kernel),
2247 bpf_prog_insn_size(prog)) != 0)
2248 goto free_prog_sec;
2249
2250 prog->orig_prog = NULL;
2251 prog->jited = 0;
2252
2253 atomic64_set(&prog->aux->refcnt, 1);
2254 prog->gpl_compatible = is_gpl ? 1 : 0;
2255
2256 if (bpf_prog_is_dev_bound(prog->aux)) {
2257 err = bpf_prog_offload_init(prog, attr);
2258 if (err)
2259 goto free_prog_sec;
2260 }
2261
2262 /* find program type: socket_filter vs tracing_filter */
2263 err = find_prog_type(type, prog);
2264 if (err < 0)
2265 goto free_prog_sec;
2266
2267 prog->aux->load_time = ktime_get_boottime_ns();
2268 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2269 sizeof(attr->prog_name));
2270 if (err < 0)
2271 goto free_prog_sec;
2272
2273 /* run eBPF verifier */
2274 err = bpf_check(&prog, attr, uattr);
2275 if (err < 0)
2276 goto free_used_maps;
2277
2278 prog = bpf_prog_select_runtime(prog, &err);
2279 if (err < 0)
2280 goto free_used_maps;
2281
2282 err = bpf_prog_alloc_id(prog);
2283 if (err)
2284 goto free_used_maps;
2285
2286 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2287 * effectively publicly exposed. However, retrieving via
2288 * bpf_prog_get_fd_by_id() will take another reference,
2289 * therefore it cannot be gone underneath us.
2290 *
2291 * Only for the time /after/ successful bpf_prog_new_fd()
2292 * and before returning to userspace, we might just hold
2293 * one reference and any parallel close on that fd could
2294 * rip everything out. Hence, below notifications must
2295 * happen before bpf_prog_new_fd().
2296 *
2297 * Also, any failure handling from this point onwards must
2298 * be using bpf_prog_put() given the program is exposed.
2299 */
2300 bpf_prog_kallsyms_add(prog);
2301 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2302 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2303
2304 err = bpf_prog_new_fd(prog);
2305 if (err < 0)
2306 bpf_prog_put(prog);
2307 return err;
2308
2309free_used_maps:
2310 /* In case we have subprogs, we need to wait for a grace
2311 * period before we can tear down JIT memory since symbols
2312 * are already exposed under kallsyms.
2313 */
2314 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2315 return err;
2316free_prog_sec:
2317 free_uid(prog->aux->user);
2318 security_bpf_prog_free(prog->aux);
2319free_prog:
2320 if (prog->aux->attach_btf)
2321 btf_put(prog->aux->attach_btf);
2322 bpf_prog_free(prog);
2323 return err;
2324}
2325
2326#define BPF_OBJ_LAST_FIELD file_flags
2327
2328static int bpf_obj_pin(const union bpf_attr *attr)
2329{
2330 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2331 return -EINVAL;
2332
2333 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2334}
2335
2336static int bpf_obj_get(const union bpf_attr *attr)
2337{
2338 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2339 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2340 return -EINVAL;
2341
2342 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2343 attr->file_flags);
2344}
2345
2346void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2347 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2348{
2349 atomic64_set(&link->refcnt, 1);
2350 link->type = type;
2351 link->id = 0;
2352 link->ops = ops;
2353 link->prog = prog;
2354}
2355
2356static void bpf_link_free_id(int id)
2357{
2358 if (!id)
2359 return;
2360
2361 spin_lock_bh(&link_idr_lock);
2362 idr_remove(&link_idr, id);
2363 spin_unlock_bh(&link_idr_lock);
2364}
2365
2366/* Clean up bpf_link and corresponding anon_inode file and FD. After
2367 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2368 * anon_inode's release() call. This helper marksbpf_link as
2369 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2370 * is not decremented, it's the responsibility of a calling code that failed
2371 * to complete bpf_link initialization.
2372 */
2373void bpf_link_cleanup(struct bpf_link_primer *primer)
2374{
2375 primer->link->prog = NULL;
2376 bpf_link_free_id(primer->id);
2377 fput(primer->file);
2378 put_unused_fd(primer->fd);
2379}
2380
2381void bpf_link_inc(struct bpf_link *link)
2382{
2383 atomic64_inc(&link->refcnt);
2384}
2385
2386/* bpf_link_free is guaranteed to be called from process context */
2387static void bpf_link_free(struct bpf_link *link)
2388{
2389 bpf_link_free_id(link->id);
2390 if (link->prog) {
2391 /* detach BPF program, clean up used resources */
2392 link->ops->release(link);
2393 bpf_prog_put(link->prog);
2394 }
2395 /* free bpf_link and its containing memory */
2396 link->ops->dealloc(link);
2397}
2398
2399static void bpf_link_put_deferred(struct work_struct *work)
2400{
2401 struct bpf_link *link = container_of(work, struct bpf_link, work);
2402
2403 bpf_link_free(link);
2404}
2405
2406/* bpf_link_put can be called from atomic context, but ensures that resources
2407 * are freed from process context
2408 */
2409void bpf_link_put(struct bpf_link *link)
2410{
2411 if (!atomic64_dec_and_test(&link->refcnt))
2412 return;
2413
2414 if (in_atomic()) {
2415 INIT_WORK(&link->work, bpf_link_put_deferred);
2416 schedule_work(&link->work);
2417 } else {
2418 bpf_link_free(link);
2419 }
2420}
2421
2422static int bpf_link_release(struct inode *inode, struct file *filp)
2423{
2424 struct bpf_link *link = filp->private_data;
2425
2426 bpf_link_put(link);
2427 return 0;
2428}
2429
2430#ifdef CONFIG_PROC_FS
2431#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2432#define BPF_MAP_TYPE(_id, _ops)
2433#define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2434static const char *bpf_link_type_strs[] = {
2435 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2436#include <linux/bpf_types.h>
2437};
2438#undef BPF_PROG_TYPE
2439#undef BPF_MAP_TYPE
2440#undef BPF_LINK_TYPE
2441
2442static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2443{
2444 const struct bpf_link *link = filp->private_data;
2445 const struct bpf_prog *prog = link->prog;
2446 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2447
2448 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2449 seq_printf(m,
2450 "link_type:\t%s\n"
2451 "link_id:\t%u\n"
2452 "prog_tag:\t%s\n"
2453 "prog_id:\t%u\n",
2454 bpf_link_type_strs[link->type],
2455 link->id,
2456 prog_tag,
2457 prog->aux->id);
2458 if (link->ops->show_fdinfo)
2459 link->ops->show_fdinfo(link, m);
2460}
2461#endif
2462
2463static const struct file_operations bpf_link_fops = {
2464#ifdef CONFIG_PROC_FS
2465 .show_fdinfo = bpf_link_show_fdinfo,
2466#endif
2467 .release = bpf_link_release,
2468 .read = bpf_dummy_read,
2469 .write = bpf_dummy_write,
2470};
2471
2472static int bpf_link_alloc_id(struct bpf_link *link)
2473{
2474 int id;
2475
2476 idr_preload(GFP_KERNEL);
2477 spin_lock_bh(&link_idr_lock);
2478 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2479 spin_unlock_bh(&link_idr_lock);
2480 idr_preload_end();
2481
2482 return id;
2483}
2484
2485/* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2486 * reserving unused FD and allocating ID from link_idr. This is to be paired
2487 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2488 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2489 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2490 * transient state is passed around in struct bpf_link_primer.
2491 * This is preferred way to create and initialize bpf_link, especially when
2492 * there are complicated and expensive operations inbetween creating bpf_link
2493 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2494 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2495 * expensive (and potentially failing) roll back operations in a rare case
2496 * that file, FD, or ID can't be allocated.
2497 */
2498int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2499{
2500 struct file *file;
2501 int fd, id;
2502
2503 fd = get_unused_fd_flags(O_CLOEXEC);
2504 if (fd < 0)
2505 return fd;
2506
2507
2508 id = bpf_link_alloc_id(link);
2509 if (id < 0) {
2510 put_unused_fd(fd);
2511 return id;
2512 }
2513
2514 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2515 if (IS_ERR(file)) {
2516 bpf_link_free_id(id);
2517 put_unused_fd(fd);
2518 return PTR_ERR(file);
2519 }
2520
2521 primer->link = link;
2522 primer->file = file;
2523 primer->fd = fd;
2524 primer->id = id;
2525 return 0;
2526}
2527
2528int bpf_link_settle(struct bpf_link_primer *primer)
2529{
2530 /* make bpf_link fetchable by ID */
2531 spin_lock_bh(&link_idr_lock);
2532 primer->link->id = primer->id;
2533 spin_unlock_bh(&link_idr_lock);
2534 /* make bpf_link fetchable by FD */
2535 fd_install(primer->fd, primer->file);
2536 /* pass through installed FD */
2537 return primer->fd;
2538}
2539
2540int bpf_link_new_fd(struct bpf_link *link)
2541{
2542 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2543}
2544
2545struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2546{
2547 struct fd f = fdget(ufd);
2548 struct bpf_link *link;
2549
2550 if (!f.file)
2551 return ERR_PTR(-EBADF);
2552 if (f.file->f_op != &bpf_link_fops) {
2553 fdput(f);
2554 return ERR_PTR(-EINVAL);
2555 }
2556
2557 link = f.file->private_data;
2558 bpf_link_inc(link);
2559 fdput(f);
2560
2561 return link;
2562}
2563
2564struct bpf_tracing_link {
2565 struct bpf_link link;
2566 enum bpf_attach_type attach_type;
2567 struct bpf_trampoline *trampoline;
2568 struct bpf_prog *tgt_prog;
2569};
2570
2571static void bpf_tracing_link_release(struct bpf_link *link)
2572{
2573 struct bpf_tracing_link *tr_link =
2574 container_of(link, struct bpf_tracing_link, link);
2575
2576 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2577 tr_link->trampoline));
2578
2579 bpf_trampoline_put(tr_link->trampoline);
2580
2581 /* tgt_prog is NULL if target is a kernel function */
2582 if (tr_link->tgt_prog)
2583 bpf_prog_put(tr_link->tgt_prog);
2584}
2585
2586static void bpf_tracing_link_dealloc(struct bpf_link *link)
2587{
2588 struct bpf_tracing_link *tr_link =
2589 container_of(link, struct bpf_tracing_link, link);
2590
2591 kfree(tr_link);
2592}
2593
2594static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2595 struct seq_file *seq)
2596{
2597 struct bpf_tracing_link *tr_link =
2598 container_of(link, struct bpf_tracing_link, link);
2599
2600 seq_printf(seq,
2601 "attach_type:\t%d\n",
2602 tr_link->attach_type);
2603}
2604
2605static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2606 struct bpf_link_info *info)
2607{
2608 struct bpf_tracing_link *tr_link =
2609 container_of(link, struct bpf_tracing_link, link);
2610
2611 info->tracing.attach_type = tr_link->attach_type;
2612 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2613 &info->tracing.target_obj_id,
2614 &info->tracing.target_btf_id);
2615
2616 return 0;
2617}
2618
2619static const struct bpf_link_ops bpf_tracing_link_lops = {
2620 .release = bpf_tracing_link_release,
2621 .dealloc = bpf_tracing_link_dealloc,
2622 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2623 .fill_link_info = bpf_tracing_link_fill_link_info,
2624};
2625
2626static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2627 int tgt_prog_fd,
2628 u32 btf_id)
2629{
2630 struct bpf_link_primer link_primer;
2631 struct bpf_prog *tgt_prog = NULL;
2632 struct bpf_trampoline *tr = NULL;
2633 struct bpf_tracing_link *link;
2634 u64 key = 0;
2635 int err;
2636
2637 switch (prog->type) {
2638 case BPF_PROG_TYPE_TRACING:
2639 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2640 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2641 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2642 err = -EINVAL;
2643 goto out_put_prog;
2644 }
2645 break;
2646 case BPF_PROG_TYPE_EXT:
2647 if (prog->expected_attach_type != 0) {
2648 err = -EINVAL;
2649 goto out_put_prog;
2650 }
2651 break;
2652 case BPF_PROG_TYPE_LSM:
2653 if (prog->expected_attach_type != BPF_LSM_MAC) {
2654 err = -EINVAL;
2655 goto out_put_prog;
2656 }
2657 break;
2658 default:
2659 err = -EINVAL;
2660 goto out_put_prog;
2661 }
2662
2663 if (!!tgt_prog_fd != !!btf_id) {
2664 err = -EINVAL;
2665 goto out_put_prog;
2666 }
2667
2668 if (tgt_prog_fd) {
2669 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2670 if (prog->type != BPF_PROG_TYPE_EXT) {
2671 err = -EINVAL;
2672 goto out_put_prog;
2673 }
2674
2675 tgt_prog = bpf_prog_get(tgt_prog_fd);
2676 if (IS_ERR(tgt_prog)) {
2677 err = PTR_ERR(tgt_prog);
2678 tgt_prog = NULL;
2679 goto out_put_prog;
2680 }
2681
2682 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
2683 }
2684
2685 link = kzalloc(sizeof(*link), GFP_USER);
2686 if (!link) {
2687 err = -ENOMEM;
2688 goto out_put_prog;
2689 }
2690 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2691 &bpf_tracing_link_lops, prog);
2692 link->attach_type = prog->expected_attach_type;
2693
2694 mutex_lock(&prog->aux->dst_mutex);
2695
2696 /* There are a few possible cases here:
2697 *
2698 * - if prog->aux->dst_trampoline is set, the program was just loaded
2699 * and not yet attached to anything, so we can use the values stored
2700 * in prog->aux
2701 *
2702 * - if prog->aux->dst_trampoline is NULL, the program has already been
2703 * attached to a target and its initial target was cleared (below)
2704 *
2705 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2706 * target_btf_id using the link_create API.
2707 *
2708 * - if tgt_prog == NULL when this function was called using the old
2709 * raw_tracepoint_open API, and we need a target from prog->aux
2710 *
2711 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
2712 * was detached and is going for re-attachment.
2713 */
2714 if (!prog->aux->dst_trampoline && !tgt_prog) {
2715 /*
2716 * Allow re-attach for TRACING and LSM programs. If it's
2717 * currently linked, bpf_trampoline_link_prog will fail.
2718 * EXT programs need to specify tgt_prog_fd, so they
2719 * re-attach in separate code path.
2720 */
2721 if (prog->type != BPF_PROG_TYPE_TRACING &&
2722 prog->type != BPF_PROG_TYPE_LSM) {
2723 err = -EINVAL;
2724 goto out_unlock;
2725 }
2726 btf_id = prog->aux->attach_btf_id;
2727 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
2728 }
2729
2730 if (!prog->aux->dst_trampoline ||
2731 (key && key != prog->aux->dst_trampoline->key)) {
2732 /* If there is no saved target, or the specified target is
2733 * different from the destination specified at load time, we
2734 * need a new trampoline and a check for compatibility
2735 */
2736 struct bpf_attach_target_info tgt_info = {};
2737
2738 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2739 &tgt_info);
2740 if (err)
2741 goto out_unlock;
2742
2743 tr = bpf_trampoline_get(key, &tgt_info);
2744 if (!tr) {
2745 err = -ENOMEM;
2746 goto out_unlock;
2747 }
2748 } else {
2749 /* The caller didn't specify a target, or the target was the
2750 * same as the destination supplied during program load. This
2751 * means we can reuse the trampoline and reference from program
2752 * load time, and there is no need to allocate a new one. This
2753 * can only happen once for any program, as the saved values in
2754 * prog->aux are cleared below.
2755 */
2756 tr = prog->aux->dst_trampoline;
2757 tgt_prog = prog->aux->dst_prog;
2758 }
2759
2760 err = bpf_link_prime(&link->link, &link_primer);
2761 if (err)
2762 goto out_unlock;
2763
2764 err = bpf_trampoline_link_prog(prog, tr);
2765 if (err) {
2766 bpf_link_cleanup(&link_primer);
2767 link = NULL;
2768 goto out_unlock;
2769 }
2770
2771 link->tgt_prog = tgt_prog;
2772 link->trampoline = tr;
2773
2774 /* Always clear the trampoline and target prog from prog->aux to make
2775 * sure the original attach destination is not kept alive after a
2776 * program is (re-)attached to another target.
2777 */
2778 if (prog->aux->dst_prog &&
2779 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2780 /* got extra prog ref from syscall, or attaching to different prog */
2781 bpf_prog_put(prog->aux->dst_prog);
2782 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2783 /* we allocated a new trampoline, so free the old one */
2784 bpf_trampoline_put(prog->aux->dst_trampoline);
2785
2786 prog->aux->dst_prog = NULL;
2787 prog->aux->dst_trampoline = NULL;
2788 mutex_unlock(&prog->aux->dst_mutex);
2789
2790 return bpf_link_settle(&link_primer);
2791out_unlock:
2792 if (tr && tr != prog->aux->dst_trampoline)
2793 bpf_trampoline_put(tr);
2794 mutex_unlock(&prog->aux->dst_mutex);
2795 kfree(link);
2796out_put_prog:
2797 if (tgt_prog_fd && tgt_prog)
2798 bpf_prog_put(tgt_prog);
2799 return err;
2800}
2801
2802struct bpf_raw_tp_link {
2803 struct bpf_link link;
2804 struct bpf_raw_event_map *btp;
2805};
2806
2807static void bpf_raw_tp_link_release(struct bpf_link *link)
2808{
2809 struct bpf_raw_tp_link *raw_tp =
2810 container_of(link, struct bpf_raw_tp_link, link);
2811
2812 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2813 bpf_put_raw_tracepoint(raw_tp->btp);
2814}
2815
2816static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2817{
2818 struct bpf_raw_tp_link *raw_tp =
2819 container_of(link, struct bpf_raw_tp_link, link);
2820
2821 kfree(raw_tp);
2822}
2823
2824static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2825 struct seq_file *seq)
2826{
2827 struct bpf_raw_tp_link *raw_tp_link =
2828 container_of(link, struct bpf_raw_tp_link, link);
2829
2830 seq_printf(seq,
2831 "tp_name:\t%s\n",
2832 raw_tp_link->btp->tp->name);
2833}
2834
2835static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2836 struct bpf_link_info *info)
2837{
2838 struct bpf_raw_tp_link *raw_tp_link =
2839 container_of(link, struct bpf_raw_tp_link, link);
2840 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2841 const char *tp_name = raw_tp_link->btp->tp->name;
2842 u32 ulen = info->raw_tracepoint.tp_name_len;
2843 size_t tp_len = strlen(tp_name);
2844
2845 if (!ulen ^ !ubuf)
2846 return -EINVAL;
2847
2848 info->raw_tracepoint.tp_name_len = tp_len + 1;
2849
2850 if (!ubuf)
2851 return 0;
2852
2853 if (ulen >= tp_len + 1) {
2854 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2855 return -EFAULT;
2856 } else {
2857 char zero = '\0';
2858
2859 if (copy_to_user(ubuf, tp_name, ulen - 1))
2860 return -EFAULT;
2861 if (put_user(zero, ubuf + ulen - 1))
2862 return -EFAULT;
2863 return -ENOSPC;
2864 }
2865
2866 return 0;
2867}
2868
2869static const struct bpf_link_ops bpf_raw_tp_link_lops = {
2870 .release = bpf_raw_tp_link_release,
2871 .dealloc = bpf_raw_tp_link_dealloc,
2872 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2873 .fill_link_info = bpf_raw_tp_link_fill_link_info,
2874};
2875
2876#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2877
2878static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2879{
2880 struct bpf_link_primer link_primer;
2881 struct bpf_raw_tp_link *link;
2882 struct bpf_raw_event_map *btp;
2883 struct bpf_prog *prog;
2884 const char *tp_name;
2885 char buf[128];
2886 int err;
2887
2888 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2889 return -EINVAL;
2890
2891 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2892 if (IS_ERR(prog))
2893 return PTR_ERR(prog);
2894
2895 switch (prog->type) {
2896 case BPF_PROG_TYPE_TRACING:
2897 case BPF_PROG_TYPE_EXT:
2898 case BPF_PROG_TYPE_LSM:
2899 if (attr->raw_tracepoint.name) {
2900 /* The attach point for this category of programs
2901 * should be specified via btf_id during program load.
2902 */
2903 err = -EINVAL;
2904 goto out_put_prog;
2905 }
2906 if (prog->type == BPF_PROG_TYPE_TRACING &&
2907 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
2908 tp_name = prog->aux->attach_func_name;
2909 break;
2910 }
2911 err = bpf_tracing_prog_attach(prog, 0, 0);
2912 if (err >= 0)
2913 return err;
2914 goto out_put_prog;
2915 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2916 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2917 if (strncpy_from_user(buf,
2918 u64_to_user_ptr(attr->raw_tracepoint.name),
2919 sizeof(buf) - 1) < 0) {
2920 err = -EFAULT;
2921 goto out_put_prog;
2922 }
2923 buf[sizeof(buf) - 1] = 0;
2924 tp_name = buf;
2925 break;
2926 default:
2927 err = -EINVAL;
2928 goto out_put_prog;
2929 }
2930
2931 btp = bpf_get_raw_tracepoint(tp_name);
2932 if (!btp) {
2933 err = -ENOENT;
2934 goto out_put_prog;
2935 }
2936
2937 link = kzalloc(sizeof(*link), GFP_USER);
2938 if (!link) {
2939 err = -ENOMEM;
2940 goto out_put_btp;
2941 }
2942 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2943 &bpf_raw_tp_link_lops, prog);
2944 link->btp = btp;
2945
2946 err = bpf_link_prime(&link->link, &link_primer);
2947 if (err) {
2948 kfree(link);
2949 goto out_put_btp;
2950 }
2951
2952 err = bpf_probe_register(link->btp, prog);
2953 if (err) {
2954 bpf_link_cleanup(&link_primer);
2955 goto out_put_btp;
2956 }
2957
2958 return bpf_link_settle(&link_primer);
2959
2960out_put_btp:
2961 bpf_put_raw_tracepoint(btp);
2962out_put_prog:
2963 bpf_prog_put(prog);
2964 return err;
2965}
2966
2967static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2968 enum bpf_attach_type attach_type)
2969{
2970 switch (prog->type) {
2971 case BPF_PROG_TYPE_CGROUP_SOCK:
2972 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2973 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2974 case BPF_PROG_TYPE_SK_LOOKUP:
2975 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
2976 case BPF_PROG_TYPE_CGROUP_SKB:
2977 if (!capable(CAP_NET_ADMIN))
2978 /* cg-skb progs can be loaded by unpriv user.
2979 * check permissions at attach time.
2980 */
2981 return -EPERM;
2982 return prog->enforce_expected_attach_type &&
2983 prog->expected_attach_type != attach_type ?
2984 -EINVAL : 0;
2985 default:
2986 return 0;
2987 }
2988}
2989
2990static enum bpf_prog_type
2991attach_type_to_prog_type(enum bpf_attach_type attach_type)
2992{
2993 switch (attach_type) {
2994 case BPF_CGROUP_INET_INGRESS:
2995 case BPF_CGROUP_INET_EGRESS:
2996 return BPF_PROG_TYPE_CGROUP_SKB;
2997 case BPF_CGROUP_INET_SOCK_CREATE:
2998 case BPF_CGROUP_INET_SOCK_RELEASE:
2999 case BPF_CGROUP_INET4_POST_BIND:
3000 case BPF_CGROUP_INET6_POST_BIND:
3001 return BPF_PROG_TYPE_CGROUP_SOCK;
3002 case BPF_CGROUP_INET4_BIND:
3003 case BPF_CGROUP_INET6_BIND:
3004 case BPF_CGROUP_INET4_CONNECT:
3005 case BPF_CGROUP_INET6_CONNECT:
3006 case BPF_CGROUP_INET4_GETPEERNAME:
3007 case BPF_CGROUP_INET6_GETPEERNAME:
3008 case BPF_CGROUP_INET4_GETSOCKNAME:
3009 case BPF_CGROUP_INET6_GETSOCKNAME:
3010 case BPF_CGROUP_UDP4_SENDMSG:
3011 case BPF_CGROUP_UDP6_SENDMSG:
3012 case BPF_CGROUP_UDP4_RECVMSG:
3013 case BPF_CGROUP_UDP6_RECVMSG:
3014 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3015 case BPF_CGROUP_SOCK_OPS:
3016 return BPF_PROG_TYPE_SOCK_OPS;
3017 case BPF_CGROUP_DEVICE:
3018 return BPF_PROG_TYPE_CGROUP_DEVICE;
3019 case BPF_SK_MSG_VERDICT:
3020 return BPF_PROG_TYPE_SK_MSG;
3021 case BPF_SK_SKB_STREAM_PARSER:
3022 case BPF_SK_SKB_STREAM_VERDICT:
3023 case BPF_SK_SKB_VERDICT:
3024 return BPF_PROG_TYPE_SK_SKB;
3025 case BPF_LIRC_MODE2:
3026 return BPF_PROG_TYPE_LIRC_MODE2;
3027 case BPF_FLOW_DISSECTOR:
3028 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3029 case BPF_CGROUP_SYSCTL:
3030 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3031 case BPF_CGROUP_GETSOCKOPT:
3032 case BPF_CGROUP_SETSOCKOPT:
3033 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
3034 case BPF_TRACE_ITER:
3035 return BPF_PROG_TYPE_TRACING;
3036 case BPF_SK_LOOKUP:
3037 return BPF_PROG_TYPE_SK_LOOKUP;
3038 case BPF_XDP:
3039 return BPF_PROG_TYPE_XDP;
3040 default:
3041 return BPF_PROG_TYPE_UNSPEC;
3042 }
3043}
3044
3045#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
3046
3047#define BPF_F_ATTACH_MASK \
3048 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
3049
3050static int bpf_prog_attach(const union bpf_attr *attr)
3051{
3052 enum bpf_prog_type ptype;
3053 struct bpf_prog *prog;
3054 int ret;
3055
3056 if (CHECK_ATTR(BPF_PROG_ATTACH))
3057 return -EINVAL;
3058
3059 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
3060 return -EINVAL;
3061
3062 ptype = attach_type_to_prog_type(attr->attach_type);
3063 if (ptype == BPF_PROG_TYPE_UNSPEC)
3064 return -EINVAL;
3065
3066 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3067 if (IS_ERR(prog))
3068 return PTR_ERR(prog);
3069
3070 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3071 bpf_prog_put(prog);
3072 return -EINVAL;
3073 }
3074
3075 switch (ptype) {
3076 case BPF_PROG_TYPE_SK_SKB:
3077 case BPF_PROG_TYPE_SK_MSG:
3078 ret = sock_map_get_from_fd(attr, prog);
3079 break;
3080 case BPF_PROG_TYPE_LIRC_MODE2:
3081 ret = lirc_prog_attach(attr, prog);
3082 break;
3083 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3084 ret = netns_bpf_prog_attach(attr, prog);
3085 break;
3086 case BPF_PROG_TYPE_CGROUP_DEVICE:
3087 case BPF_PROG_TYPE_CGROUP_SKB:
3088 case BPF_PROG_TYPE_CGROUP_SOCK:
3089 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3090 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3091 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3092 case BPF_PROG_TYPE_SOCK_OPS:
3093 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3094 break;
3095 default:
3096 ret = -EINVAL;
3097 }
3098
3099 if (ret)
3100 bpf_prog_put(prog);
3101 return ret;
3102}
3103
3104#define BPF_PROG_DETACH_LAST_FIELD attach_type
3105
3106static int bpf_prog_detach(const union bpf_attr *attr)
3107{
3108 enum bpf_prog_type ptype;
3109
3110 if (CHECK_ATTR(BPF_PROG_DETACH))
3111 return -EINVAL;
3112
3113 ptype = attach_type_to_prog_type(attr->attach_type);
3114
3115 switch (ptype) {
3116 case BPF_PROG_TYPE_SK_MSG:
3117 case BPF_PROG_TYPE_SK_SKB:
3118 return sock_map_prog_detach(attr, ptype);
3119 case BPF_PROG_TYPE_LIRC_MODE2:
3120 return lirc_prog_detach(attr);
3121 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3122 return netns_bpf_prog_detach(attr, ptype);
3123 case BPF_PROG_TYPE_CGROUP_DEVICE:
3124 case BPF_PROG_TYPE_CGROUP_SKB:
3125 case BPF_PROG_TYPE_CGROUP_SOCK:
3126 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3127 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3128 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3129 case BPF_PROG_TYPE_SOCK_OPS:
3130 return cgroup_bpf_prog_detach(attr, ptype);
3131 default:
3132 return -EINVAL;
3133 }
3134}
3135
3136#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3137
3138static int bpf_prog_query(const union bpf_attr *attr,
3139 union bpf_attr __user *uattr)
3140{
3141 if (!capable(CAP_NET_ADMIN))
3142 return -EPERM;
3143 if (CHECK_ATTR(BPF_PROG_QUERY))
3144 return -EINVAL;
3145 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3146 return -EINVAL;
3147
3148 switch (attr->query.attach_type) {
3149 case BPF_CGROUP_INET_INGRESS:
3150 case BPF_CGROUP_INET_EGRESS:
3151 case BPF_CGROUP_INET_SOCK_CREATE:
3152 case BPF_CGROUP_INET_SOCK_RELEASE:
3153 case BPF_CGROUP_INET4_BIND:
3154 case BPF_CGROUP_INET6_BIND:
3155 case BPF_CGROUP_INET4_POST_BIND:
3156 case BPF_CGROUP_INET6_POST_BIND:
3157 case BPF_CGROUP_INET4_CONNECT:
3158 case BPF_CGROUP_INET6_CONNECT:
3159 case BPF_CGROUP_INET4_GETPEERNAME:
3160 case BPF_CGROUP_INET6_GETPEERNAME:
3161 case BPF_CGROUP_INET4_GETSOCKNAME:
3162 case BPF_CGROUP_INET6_GETSOCKNAME:
3163 case BPF_CGROUP_UDP4_SENDMSG:
3164 case BPF_CGROUP_UDP6_SENDMSG:
3165 case BPF_CGROUP_UDP4_RECVMSG:
3166 case BPF_CGROUP_UDP6_RECVMSG:
3167 case BPF_CGROUP_SOCK_OPS:
3168 case BPF_CGROUP_DEVICE:
3169 case BPF_CGROUP_SYSCTL:
3170 case BPF_CGROUP_GETSOCKOPT:
3171 case BPF_CGROUP_SETSOCKOPT:
3172 return cgroup_bpf_prog_query(attr, uattr);
3173 case BPF_LIRC_MODE2:
3174 return lirc_prog_query(attr, uattr);
3175 case BPF_FLOW_DISSECTOR:
3176 case BPF_SK_LOOKUP:
3177 return netns_bpf_prog_query(attr, uattr);
3178 default:
3179 return -EINVAL;
3180 }
3181}
3182
3183#define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
3184
3185static int bpf_prog_test_run(const union bpf_attr *attr,
3186 union bpf_attr __user *uattr)
3187{
3188 struct bpf_prog *prog;
3189 int ret = -ENOTSUPP;
3190
3191 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3192 return -EINVAL;
3193
3194 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3195 (!attr->test.ctx_size_in && attr->test.ctx_in))
3196 return -EINVAL;
3197
3198 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3199 (!attr->test.ctx_size_out && attr->test.ctx_out))
3200 return -EINVAL;
3201
3202 prog = bpf_prog_get(attr->test.prog_fd);
3203 if (IS_ERR(prog))
3204 return PTR_ERR(prog);
3205
3206 if (prog->aux->ops->test_run)
3207 ret = prog->aux->ops->test_run(prog, attr, uattr);
3208
3209 bpf_prog_put(prog);
3210 return ret;
3211}
3212
3213#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3214
3215static int bpf_obj_get_next_id(const union bpf_attr *attr,
3216 union bpf_attr __user *uattr,
3217 struct idr *idr,
3218 spinlock_t *lock)
3219{
3220 u32 next_id = attr->start_id;
3221 int err = 0;
3222
3223 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3224 return -EINVAL;
3225
3226 if (!capable(CAP_SYS_ADMIN))
3227 return -EPERM;
3228
3229 next_id++;
3230 spin_lock_bh(lock);
3231 if (!idr_get_next(idr, &next_id))
3232 err = -ENOENT;
3233 spin_unlock_bh(lock);
3234
3235 if (!err)
3236 err = put_user(next_id, &uattr->next_id);
3237
3238 return err;
3239}
3240
3241struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3242{
3243 struct bpf_map *map;
3244
3245 spin_lock_bh(&map_idr_lock);
3246again:
3247 map = idr_get_next(&map_idr, id);
3248 if (map) {
3249 map = __bpf_map_inc_not_zero(map, false);
3250 if (IS_ERR(map)) {
3251 (*id)++;
3252 goto again;
3253 }
3254 }
3255 spin_unlock_bh(&map_idr_lock);
3256
3257 return map;
3258}
3259
3260struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3261{
3262 struct bpf_prog *prog;
3263
3264 spin_lock_bh(&prog_idr_lock);
3265again:
3266 prog = idr_get_next(&prog_idr, id);
3267 if (prog) {
3268 prog = bpf_prog_inc_not_zero(prog);
3269 if (IS_ERR(prog)) {
3270 (*id)++;
3271 goto again;
3272 }
3273 }
3274 spin_unlock_bh(&prog_idr_lock);
3275
3276 return prog;
3277}
3278
3279#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3280
3281struct bpf_prog *bpf_prog_by_id(u32 id)
3282{
3283 struct bpf_prog *prog;
3284
3285 if (!id)
3286 return ERR_PTR(-ENOENT);
3287
3288 spin_lock_bh(&prog_idr_lock);
3289 prog = idr_find(&prog_idr, id);
3290 if (prog)
3291 prog = bpf_prog_inc_not_zero(prog);
3292 else
3293 prog = ERR_PTR(-ENOENT);
3294 spin_unlock_bh(&prog_idr_lock);
3295 return prog;
3296}
3297
3298static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3299{
3300 struct bpf_prog *prog;
3301 u32 id = attr->prog_id;
3302 int fd;
3303
3304 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3305 return -EINVAL;
3306
3307 if (!capable(CAP_SYS_ADMIN))
3308 return -EPERM;
3309
3310 prog = bpf_prog_by_id(id);
3311 if (IS_ERR(prog))
3312 return PTR_ERR(prog);
3313
3314 fd = bpf_prog_new_fd(prog);
3315 if (fd < 0)
3316 bpf_prog_put(prog);
3317
3318 return fd;
3319}
3320
3321#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3322
3323static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3324{
3325 struct bpf_map *map;
3326 u32 id = attr->map_id;
3327 int f_flags;
3328 int fd;
3329
3330 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3331 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3332 return -EINVAL;
3333
3334 if (!capable(CAP_SYS_ADMIN))
3335 return -EPERM;
3336
3337 f_flags = bpf_get_file_flag(attr->open_flags);
3338 if (f_flags < 0)
3339 return f_flags;
3340
3341 spin_lock_bh(&map_idr_lock);
3342 map = idr_find(&map_idr, id);
3343 if (map)
3344 map = __bpf_map_inc_not_zero(map, true);
3345 else
3346 map = ERR_PTR(-ENOENT);
3347 spin_unlock_bh(&map_idr_lock);
3348
3349 if (IS_ERR(map))
3350 return PTR_ERR(map);
3351
3352 fd = bpf_map_new_fd(map, f_flags);
3353 if (fd < 0)
3354 bpf_map_put_with_uref(map);
3355
3356 return fd;
3357}
3358
3359static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3360 unsigned long addr, u32 *off,
3361 u32 *type)
3362{
3363 const struct bpf_map *map;
3364 int i;
3365
3366 mutex_lock(&prog->aux->used_maps_mutex);
3367 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3368 map = prog->aux->used_maps[i];
3369 if (map == (void *)addr) {
3370 *type = BPF_PSEUDO_MAP_FD;
3371 goto out;
3372 }
3373 if (!map->ops->map_direct_value_meta)
3374 continue;
3375 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3376 *type = BPF_PSEUDO_MAP_VALUE;
3377 goto out;
3378 }
3379 }
3380 map = NULL;
3381
3382out:
3383 mutex_unlock(&prog->aux->used_maps_mutex);
3384 return map;
3385}
3386
3387static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3388 const struct cred *f_cred)
3389{
3390 const struct bpf_map *map;
3391 struct bpf_insn *insns;
3392 u32 off, type;
3393 u64 imm;
3394 u8 code;
3395 int i;
3396
3397 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3398 GFP_USER);
3399 if (!insns)
3400 return insns;
3401
3402 for (i = 0; i < prog->len; i++) {
3403 code = insns[i].code;
3404
3405 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3406 insns[i].code = BPF_JMP | BPF_CALL;
3407 insns[i].imm = BPF_FUNC_tail_call;
3408 /* fall-through */
3409 }
3410 if (code == (BPF_JMP | BPF_CALL) ||
3411 code == (BPF_JMP | BPF_CALL_ARGS)) {
3412 if (code == (BPF_JMP | BPF_CALL_ARGS))
3413 insns[i].code = BPF_JMP | BPF_CALL;
3414 if (!bpf_dump_raw_ok(f_cred))
3415 insns[i].imm = 0;
3416 continue;
3417 }
3418 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3419 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3420 continue;
3421 }
3422
3423 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3424 continue;
3425
3426 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3427 map = bpf_map_from_imm(prog, imm, &off, &type);
3428 if (map) {
3429 insns[i].src_reg = type;
3430 insns[i].imm = map->id;
3431 insns[i + 1].imm = off;
3432 continue;
3433 }
3434 }
3435
3436 return insns;
3437}
3438
3439static int set_info_rec_size(struct bpf_prog_info *info)
3440{
3441 /*
3442 * Ensure info.*_rec_size is the same as kernel expected size
3443 *
3444 * or
3445 *
3446 * Only allow zero *_rec_size if both _rec_size and _cnt are
3447 * zero. In this case, the kernel will set the expected
3448 * _rec_size back to the info.
3449 */
3450
3451 if ((info->nr_func_info || info->func_info_rec_size) &&
3452 info->func_info_rec_size != sizeof(struct bpf_func_info))
3453 return -EINVAL;
3454
3455 if ((info->nr_line_info || info->line_info_rec_size) &&
3456 info->line_info_rec_size != sizeof(struct bpf_line_info))
3457 return -EINVAL;
3458
3459 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3460 info->jited_line_info_rec_size != sizeof(__u64))
3461 return -EINVAL;
3462
3463 info->func_info_rec_size = sizeof(struct bpf_func_info);
3464 info->line_info_rec_size = sizeof(struct bpf_line_info);
3465 info->jited_line_info_rec_size = sizeof(__u64);
3466
3467 return 0;
3468}
3469
3470static int bpf_prog_get_info_by_fd(struct file *file,
3471 struct bpf_prog *prog,
3472 const union bpf_attr *attr,
3473 union bpf_attr __user *uattr)
3474{
3475 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3476 struct bpf_prog_info info;
3477 u32 info_len = attr->info.info_len;
3478 struct bpf_prog_stats stats;
3479 char __user *uinsns;
3480 u32 ulen;
3481 int err;
3482
3483 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3484 if (err)
3485 return err;
3486 info_len = min_t(u32, sizeof(info), info_len);
3487
3488 memset(&info, 0, sizeof(info));
3489 if (copy_from_user(&info, uinfo, info_len))
3490 return -EFAULT;
3491
3492 info.type = prog->type;
3493 info.id = prog->aux->id;
3494 info.load_time = prog->aux->load_time;
3495 info.created_by_uid = from_kuid_munged(current_user_ns(),
3496 prog->aux->user->uid);
3497 info.gpl_compatible = prog->gpl_compatible;
3498
3499 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3500 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3501
3502 mutex_lock(&prog->aux->used_maps_mutex);
3503 ulen = info.nr_map_ids;
3504 info.nr_map_ids = prog->aux->used_map_cnt;
3505 ulen = min_t(u32, info.nr_map_ids, ulen);
3506 if (ulen) {
3507 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3508 u32 i;
3509
3510 for (i = 0; i < ulen; i++)
3511 if (put_user(prog->aux->used_maps[i]->id,
3512 &user_map_ids[i])) {
3513 mutex_unlock(&prog->aux->used_maps_mutex);
3514 return -EFAULT;
3515 }
3516 }
3517 mutex_unlock(&prog->aux->used_maps_mutex);
3518
3519 err = set_info_rec_size(&info);
3520 if (err)
3521 return err;
3522
3523 bpf_prog_get_stats(prog, &stats);
3524 info.run_time_ns = stats.nsecs;
3525 info.run_cnt = stats.cnt;
3526 info.recursion_misses = stats.misses;
3527
3528 if (!bpf_capable()) {
3529 info.jited_prog_len = 0;
3530 info.xlated_prog_len = 0;
3531 info.nr_jited_ksyms = 0;
3532 info.nr_jited_func_lens = 0;
3533 info.nr_func_info = 0;
3534 info.nr_line_info = 0;
3535 info.nr_jited_line_info = 0;
3536 goto done;
3537 }
3538
3539 ulen = info.xlated_prog_len;
3540 info.xlated_prog_len = bpf_prog_insn_size(prog);
3541 if (info.xlated_prog_len && ulen) {
3542 struct bpf_insn *insns_sanitized;
3543 bool fault;
3544
3545 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3546 info.xlated_prog_insns = 0;
3547 goto done;
3548 }
3549 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3550 if (!insns_sanitized)
3551 return -ENOMEM;
3552 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3553 ulen = min_t(u32, info.xlated_prog_len, ulen);
3554 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3555 kfree(insns_sanitized);
3556 if (fault)
3557 return -EFAULT;
3558 }
3559
3560 if (bpf_prog_is_dev_bound(prog->aux)) {
3561 err = bpf_prog_offload_info_fill(&info, prog);
3562 if (err)
3563 return err;
3564 goto done;
3565 }
3566
3567 /* NOTE: the following code is supposed to be skipped for offload.
3568 * bpf_prog_offload_info_fill() is the place to fill similar fields
3569 * for offload.
3570 */
3571 ulen = info.jited_prog_len;
3572 if (prog->aux->func_cnt) {
3573 u32 i;
3574
3575 info.jited_prog_len = 0;
3576 for (i = 0; i < prog->aux->func_cnt; i++)
3577 info.jited_prog_len += prog->aux->func[i]->jited_len;
3578 } else {
3579 info.jited_prog_len = prog->jited_len;
3580 }
3581
3582 if (info.jited_prog_len && ulen) {
3583 if (bpf_dump_raw_ok(file->f_cred)) {
3584 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3585 ulen = min_t(u32, info.jited_prog_len, ulen);
3586
3587 /* for multi-function programs, copy the JITed
3588 * instructions for all the functions
3589 */
3590 if (prog->aux->func_cnt) {
3591 u32 len, free, i;
3592 u8 *img;
3593
3594 free = ulen;
3595 for (i = 0; i < prog->aux->func_cnt; i++) {
3596 len = prog->aux->func[i]->jited_len;
3597 len = min_t(u32, len, free);
3598 img = (u8 *) prog->aux->func[i]->bpf_func;
3599 if (copy_to_user(uinsns, img, len))
3600 return -EFAULT;
3601 uinsns += len;
3602 free -= len;
3603 if (!free)
3604 break;
3605 }
3606 } else {
3607 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3608 return -EFAULT;
3609 }
3610 } else {
3611 info.jited_prog_insns = 0;
3612 }
3613 }
3614
3615 ulen = info.nr_jited_ksyms;
3616 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
3617 if (ulen) {
3618 if (bpf_dump_raw_ok(file->f_cred)) {
3619 unsigned long ksym_addr;
3620 u64 __user *user_ksyms;
3621 u32 i;
3622
3623 /* copy the address of the kernel symbol
3624 * corresponding to each function
3625 */
3626 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3627 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3628 if (prog->aux->func_cnt) {
3629 for (i = 0; i < ulen; i++) {
3630 ksym_addr = (unsigned long)
3631 prog->aux->func[i]->bpf_func;
3632 if (put_user((u64) ksym_addr,
3633 &user_ksyms[i]))
3634 return -EFAULT;
3635 }
3636 } else {
3637 ksym_addr = (unsigned long) prog->bpf_func;
3638 if (put_user((u64) ksym_addr, &user_ksyms[0]))
3639 return -EFAULT;
3640 }
3641 } else {
3642 info.jited_ksyms = 0;
3643 }
3644 }
3645
3646 ulen = info.nr_jited_func_lens;
3647 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
3648 if (ulen) {
3649 if (bpf_dump_raw_ok(file->f_cred)) {
3650 u32 __user *user_lens;
3651 u32 func_len, i;
3652
3653 /* copy the JITed image lengths for each function */
3654 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3655 user_lens = u64_to_user_ptr(info.jited_func_lens);
3656 if (prog->aux->func_cnt) {
3657 for (i = 0; i < ulen; i++) {
3658 func_len =
3659 prog->aux->func[i]->jited_len;
3660 if (put_user(func_len, &user_lens[i]))
3661 return -EFAULT;
3662 }
3663 } else {
3664 func_len = prog->jited_len;
3665 if (put_user(func_len, &user_lens[0]))
3666 return -EFAULT;
3667 }
3668 } else {
3669 info.jited_func_lens = 0;
3670 }
3671 }
3672
3673 if (prog->aux->btf)
3674 info.btf_id = btf_obj_id(prog->aux->btf);
3675
3676 ulen = info.nr_func_info;
3677 info.nr_func_info = prog->aux->func_info_cnt;
3678 if (info.nr_func_info && ulen) {
3679 char __user *user_finfo;
3680
3681 user_finfo = u64_to_user_ptr(info.func_info);
3682 ulen = min_t(u32, info.nr_func_info, ulen);
3683 if (copy_to_user(user_finfo, prog->aux->func_info,
3684 info.func_info_rec_size * ulen))
3685 return -EFAULT;
3686 }
3687
3688 ulen = info.nr_line_info;
3689 info.nr_line_info = prog->aux->nr_linfo;
3690 if (info.nr_line_info && ulen) {
3691 __u8 __user *user_linfo;
3692
3693 user_linfo = u64_to_user_ptr(info.line_info);
3694 ulen = min_t(u32, info.nr_line_info, ulen);
3695 if (copy_to_user(user_linfo, prog->aux->linfo,
3696 info.line_info_rec_size * ulen))
3697 return -EFAULT;
3698 }
3699
3700 ulen = info.nr_jited_line_info;
3701 if (prog->aux->jited_linfo)
3702 info.nr_jited_line_info = prog->aux->nr_linfo;
3703 else
3704 info.nr_jited_line_info = 0;
3705 if (info.nr_jited_line_info && ulen) {
3706 if (bpf_dump_raw_ok(file->f_cred)) {
3707 __u64 __user *user_linfo;
3708 u32 i;
3709
3710 user_linfo = u64_to_user_ptr(info.jited_line_info);
3711 ulen = min_t(u32, info.nr_jited_line_info, ulen);
3712 for (i = 0; i < ulen; i++) {
3713 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3714 &user_linfo[i]))
3715 return -EFAULT;
3716 }
3717 } else {
3718 info.jited_line_info = 0;
3719 }
3720 }
3721
3722 ulen = info.nr_prog_tags;
3723 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3724 if (ulen) {
3725 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3726 u32 i;
3727
3728 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3729 ulen = min_t(u32, info.nr_prog_tags, ulen);
3730 if (prog->aux->func_cnt) {
3731 for (i = 0; i < ulen; i++) {
3732 if (copy_to_user(user_prog_tags[i],
3733 prog->aux->func[i]->tag,
3734 BPF_TAG_SIZE))
3735 return -EFAULT;
3736 }
3737 } else {
3738 if (copy_to_user(user_prog_tags[0],
3739 prog->tag, BPF_TAG_SIZE))
3740 return -EFAULT;
3741 }
3742 }
3743
3744done:
3745 if (copy_to_user(uinfo, &info, info_len) ||
3746 put_user(info_len, &uattr->info.info_len))
3747 return -EFAULT;
3748
3749 return 0;
3750}
3751
3752static int bpf_map_get_info_by_fd(struct file *file,
3753 struct bpf_map *map,
3754 const union bpf_attr *attr,
3755 union bpf_attr __user *uattr)
3756{
3757 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3758 struct bpf_map_info info;
3759 u32 info_len = attr->info.info_len;
3760 int err;
3761
3762 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3763 if (err)
3764 return err;
3765 info_len = min_t(u32, sizeof(info), info_len);
3766
3767 memset(&info, 0, sizeof(info));
3768 info.type = map->map_type;
3769 info.id = map->id;
3770 info.key_size = map->key_size;
3771 info.value_size = map->value_size;
3772 info.max_entries = map->max_entries;
3773 info.map_flags = map->map_flags;
3774 memcpy(info.name, map->name, sizeof(map->name));
3775
3776 if (map->btf) {
3777 info.btf_id = btf_obj_id(map->btf);
3778 info.btf_key_type_id = map->btf_key_type_id;
3779 info.btf_value_type_id = map->btf_value_type_id;
3780 }
3781 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
3782
3783 if (bpf_map_is_dev_bound(map)) {
3784 err = bpf_map_offload_info_fill(&info, map);
3785 if (err)
3786 return err;
3787 }
3788
3789 if (copy_to_user(uinfo, &info, info_len) ||
3790 put_user(info_len, &uattr->info.info_len))
3791 return -EFAULT;
3792
3793 return 0;
3794}
3795
3796static int bpf_btf_get_info_by_fd(struct file *file,
3797 struct btf *btf,
3798 const union bpf_attr *attr,
3799 union bpf_attr __user *uattr)
3800{
3801 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3802 u32 info_len = attr->info.info_len;
3803 int err;
3804
3805 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
3806 if (err)
3807 return err;
3808
3809 return btf_get_info_by_fd(btf, attr, uattr);
3810}
3811
3812static int bpf_link_get_info_by_fd(struct file *file,
3813 struct bpf_link *link,
3814 const union bpf_attr *attr,
3815 union bpf_attr __user *uattr)
3816{
3817 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3818 struct bpf_link_info info;
3819 u32 info_len = attr->info.info_len;
3820 int err;
3821
3822 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3823 if (err)
3824 return err;
3825 info_len = min_t(u32, sizeof(info), info_len);
3826
3827 memset(&info, 0, sizeof(info));
3828 if (copy_from_user(&info, uinfo, info_len))
3829 return -EFAULT;
3830
3831 info.type = link->type;
3832 info.id = link->id;
3833 info.prog_id = link->prog->aux->id;
3834
3835 if (link->ops->fill_link_info) {
3836 err = link->ops->fill_link_info(link, &info);
3837 if (err)
3838 return err;
3839 }
3840
3841 if (copy_to_user(uinfo, &info, info_len) ||
3842 put_user(info_len, &uattr->info.info_len))
3843 return -EFAULT;
3844
3845 return 0;
3846}
3847
3848
3849#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3850
3851static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3852 union bpf_attr __user *uattr)
3853{
3854 int ufd = attr->info.bpf_fd;
3855 struct fd f;
3856 int err;
3857
3858 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3859 return -EINVAL;
3860
3861 f = fdget(ufd);
3862 if (!f.file)
3863 return -EBADFD;
3864
3865 if (f.file->f_op == &bpf_prog_fops)
3866 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
3867 uattr);
3868 else if (f.file->f_op == &bpf_map_fops)
3869 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
3870 uattr);
3871 else if (f.file->f_op == &btf_fops)
3872 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
3873 else if (f.file->f_op == &bpf_link_fops)
3874 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
3875 attr, uattr);
3876 else
3877 err = -EINVAL;
3878
3879 fdput(f);
3880 return err;
3881}
3882
3883#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3884
3885static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
3886{
3887 if (CHECK_ATTR(BPF_BTF_LOAD))
3888 return -EINVAL;
3889
3890 if (!bpf_capable())
3891 return -EPERM;
3892
3893 return btf_new_fd(attr, uattr);
3894}
3895
3896#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3897
3898static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3899{
3900 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3901 return -EINVAL;
3902
3903 if (!capable(CAP_SYS_ADMIN))
3904 return -EPERM;
3905
3906 return btf_get_fd_by_id(attr->btf_id);
3907}
3908
3909static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3910 union bpf_attr __user *uattr,
3911 u32 prog_id, u32 fd_type,
3912 const char *buf, u64 probe_offset,
3913 u64 probe_addr)
3914{
3915 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3916 u32 len = buf ? strlen(buf) : 0, input_len;
3917 int err = 0;
3918
3919 if (put_user(len, &uattr->task_fd_query.buf_len))
3920 return -EFAULT;
3921 input_len = attr->task_fd_query.buf_len;
3922 if (input_len && ubuf) {
3923 if (!len) {
3924 /* nothing to copy, just make ubuf NULL terminated */
3925 char zero = '\0';
3926
3927 if (put_user(zero, ubuf))
3928 return -EFAULT;
3929 } else if (input_len >= len + 1) {
3930 /* ubuf can hold the string with NULL terminator */
3931 if (copy_to_user(ubuf, buf, len + 1))
3932 return -EFAULT;
3933 } else {
3934 /* ubuf cannot hold the string with NULL terminator,
3935 * do a partial copy with NULL terminator.
3936 */
3937 char zero = '\0';
3938
3939 err = -ENOSPC;
3940 if (copy_to_user(ubuf, buf, input_len - 1))
3941 return -EFAULT;
3942 if (put_user(zero, ubuf + input_len - 1))
3943 return -EFAULT;
3944 }
3945 }
3946
3947 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3948 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3949 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3950 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3951 return -EFAULT;
3952
3953 return err;
3954}
3955
3956#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3957
3958static int bpf_task_fd_query(const union bpf_attr *attr,
3959 union bpf_attr __user *uattr)
3960{
3961 pid_t pid = attr->task_fd_query.pid;
3962 u32 fd = attr->task_fd_query.fd;
3963 const struct perf_event *event;
3964 struct task_struct *task;
3965 struct file *file;
3966 int err;
3967
3968 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3969 return -EINVAL;
3970
3971 if (!capable(CAP_SYS_ADMIN))
3972 return -EPERM;
3973
3974 if (attr->task_fd_query.flags != 0)
3975 return -EINVAL;
3976
3977 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3978 if (!task)
3979 return -ENOENT;
3980
3981 err = 0;
3982 file = fget_task(task, fd);
3983 put_task_struct(task);
3984 if (!file)
3985 return -EBADF;
3986
3987 if (file->f_op == &bpf_link_fops) {
3988 struct bpf_link *link = file->private_data;
3989
3990 if (link->ops == &bpf_raw_tp_link_lops) {
3991 struct bpf_raw_tp_link *raw_tp =
3992 container_of(link, struct bpf_raw_tp_link, link);
3993 struct bpf_raw_event_map *btp = raw_tp->btp;
3994
3995 err = bpf_task_fd_query_copy(attr, uattr,
3996 raw_tp->link.prog->aux->id,
3997 BPF_FD_TYPE_RAW_TRACEPOINT,
3998 btp->tp->name, 0, 0);
3999 goto put_file;
4000 }
4001 goto out_not_supp;
4002 }
4003
4004 event = perf_get_event(file);
4005 if (!IS_ERR(event)) {
4006 u64 probe_offset, probe_addr;
4007 u32 prog_id, fd_type;
4008 const char *buf;
4009
4010 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4011 &buf, &probe_offset,
4012 &probe_addr);
4013 if (!err)
4014 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4015 fd_type, buf,
4016 probe_offset,
4017 probe_addr);
4018 goto put_file;
4019 }
4020
4021out_not_supp:
4022 err = -ENOTSUPP;
4023put_file:
4024 fput(file);
4025 return err;
4026}
4027
4028#define BPF_MAP_BATCH_LAST_FIELD batch.flags
4029
4030#define BPF_DO_BATCH(fn) \
4031 do { \
4032 if (!fn) { \
4033 err = -ENOTSUPP; \
4034 goto err_put; \
4035 } \
4036 err = fn(map, attr, uattr); \
4037 } while (0)
4038
4039static int bpf_map_do_batch(const union bpf_attr *attr,
4040 union bpf_attr __user *uattr,
4041 int cmd)
4042{
4043 struct bpf_map *map;
4044 int err, ufd;
4045 struct fd f;
4046
4047 if (CHECK_ATTR(BPF_MAP_BATCH))
4048 return -EINVAL;
4049
4050 ufd = attr->batch.map_fd;
4051 f = fdget(ufd);
4052 map = __bpf_map_get(f);
4053 if (IS_ERR(map))
4054 return PTR_ERR(map);
4055
4056 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
4057 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
4058 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4059 err = -EPERM;
4060 goto err_put;
4061 }
4062
4063 if (cmd != BPF_MAP_LOOKUP_BATCH &&
4064 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4065 err = -EPERM;
4066 goto err_put;
4067 }
4068
4069 if (cmd == BPF_MAP_LOOKUP_BATCH)
4070 BPF_DO_BATCH(map->ops->map_lookup_batch);
4071 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4072 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4073 else if (cmd == BPF_MAP_UPDATE_BATCH)
4074 BPF_DO_BATCH(map->ops->map_update_batch);
4075 else
4076 BPF_DO_BATCH(map->ops->map_delete_batch);
4077
4078err_put:
4079 fdput(f);
4080 return err;
4081}
4082
4083static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
4084 struct bpf_prog *prog)
4085{
4086 if (attr->link_create.attach_type != prog->expected_attach_type)
4087 return -EINVAL;
4088
4089 if (prog->expected_attach_type == BPF_TRACE_ITER)
4090 return bpf_iter_link_attach(attr, uattr, prog);
4091 else if (prog->type == BPF_PROG_TYPE_EXT)
4092 return bpf_tracing_prog_attach(prog,
4093 attr->link_create.target_fd,
4094 attr->link_create.target_btf_id);
4095 return -EINVAL;
4096}
4097
4098#define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
4099static int link_create(union bpf_attr *attr, bpfptr_t uattr)
4100{
4101 enum bpf_prog_type ptype;
4102 struct bpf_prog *prog;
4103 int ret;
4104
4105 if (CHECK_ATTR(BPF_LINK_CREATE))
4106 return -EINVAL;
4107
4108 prog = bpf_prog_get(attr->link_create.prog_fd);
4109 if (IS_ERR(prog))
4110 return PTR_ERR(prog);
4111
4112 ret = bpf_prog_attach_check_attach_type(prog,
4113 attr->link_create.attach_type);
4114 if (ret)
4115 goto out;
4116
4117 if (prog->type == BPF_PROG_TYPE_EXT) {
4118 ret = tracing_bpf_link_attach(attr, uattr, prog);
4119 goto out;
4120 }
4121
4122 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4123 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4124 ret = -EINVAL;
4125 goto out;
4126 }
4127
4128 switch (ptype) {
4129 case BPF_PROG_TYPE_CGROUP_SKB:
4130 case BPF_PROG_TYPE_CGROUP_SOCK:
4131 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4132 case BPF_PROG_TYPE_SOCK_OPS:
4133 case BPF_PROG_TYPE_CGROUP_DEVICE:
4134 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4135 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4136 ret = cgroup_bpf_link_attach(attr, prog);
4137 break;
4138 case BPF_PROG_TYPE_TRACING:
4139 ret = tracing_bpf_link_attach(attr, uattr, prog);
4140 break;
4141 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4142 case BPF_PROG_TYPE_SK_LOOKUP:
4143 ret = netns_bpf_link_create(attr, prog);
4144 break;
4145#ifdef CONFIG_NET
4146 case BPF_PROG_TYPE_XDP:
4147 ret = bpf_xdp_link_attach(attr, prog);
4148 break;
4149#endif
4150 default:
4151 ret = -EINVAL;
4152 }
4153
4154out:
4155 if (ret < 0)
4156 bpf_prog_put(prog);
4157 return ret;
4158}
4159
4160#define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4161
4162static int link_update(union bpf_attr *attr)
4163{
4164 struct bpf_prog *old_prog = NULL, *new_prog;
4165 struct bpf_link *link;
4166 u32 flags;
4167 int ret;
4168
4169 if (CHECK_ATTR(BPF_LINK_UPDATE))
4170 return -EINVAL;
4171
4172 flags = attr->link_update.flags;
4173 if (flags & ~BPF_F_REPLACE)
4174 return -EINVAL;
4175
4176 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4177 if (IS_ERR(link))
4178 return PTR_ERR(link);
4179
4180 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4181 if (IS_ERR(new_prog)) {
4182 ret = PTR_ERR(new_prog);
4183 goto out_put_link;
4184 }
4185
4186 if (flags & BPF_F_REPLACE) {
4187 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4188 if (IS_ERR(old_prog)) {
4189 ret = PTR_ERR(old_prog);
4190 old_prog = NULL;
4191 goto out_put_progs;
4192 }
4193 } else if (attr->link_update.old_prog_fd) {
4194 ret = -EINVAL;
4195 goto out_put_progs;
4196 }
4197
4198 if (link->ops->update_prog)
4199 ret = link->ops->update_prog(link, new_prog, old_prog);
4200 else
4201 ret = -EINVAL;
4202
4203out_put_progs:
4204 if (old_prog)
4205 bpf_prog_put(old_prog);
4206 if (ret)
4207 bpf_prog_put(new_prog);
4208out_put_link:
4209 bpf_link_put(link);
4210 return ret;
4211}
4212
4213#define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4214
4215static int link_detach(union bpf_attr *attr)
4216{
4217 struct bpf_link *link;
4218 int ret;
4219
4220 if (CHECK_ATTR(BPF_LINK_DETACH))
4221 return -EINVAL;
4222
4223 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4224 if (IS_ERR(link))
4225 return PTR_ERR(link);
4226
4227 if (link->ops->detach)
4228 ret = link->ops->detach(link);
4229 else
4230 ret = -EOPNOTSUPP;
4231
4232 bpf_link_put(link);
4233 return ret;
4234}
4235
4236static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4237{
4238 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4239}
4240
4241struct bpf_link *bpf_link_by_id(u32 id)
4242{
4243 struct bpf_link *link;
4244
4245 if (!id)
4246 return ERR_PTR(-ENOENT);
4247
4248 spin_lock_bh(&link_idr_lock);
4249 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4250 link = idr_find(&link_idr, id);
4251 if (link) {
4252 if (link->id)
4253 link = bpf_link_inc_not_zero(link);
4254 else
4255 link = ERR_PTR(-EAGAIN);
4256 } else {
4257 link = ERR_PTR(-ENOENT);
4258 }
4259 spin_unlock_bh(&link_idr_lock);
4260 return link;
4261}
4262
4263#define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4264
4265static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4266{
4267 struct bpf_link *link;
4268 u32 id = attr->link_id;
4269 int fd;
4270
4271 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4272 return -EINVAL;
4273
4274 if (!capable(CAP_SYS_ADMIN))
4275 return -EPERM;
4276
4277 link = bpf_link_by_id(id);
4278 if (IS_ERR(link))
4279 return PTR_ERR(link);
4280
4281 fd = bpf_link_new_fd(link);
4282 if (fd < 0)
4283 bpf_link_put(link);
4284
4285 return fd;
4286}
4287
4288DEFINE_MUTEX(bpf_stats_enabled_mutex);
4289
4290static int bpf_stats_release(struct inode *inode, struct file *file)
4291{
4292 mutex_lock(&bpf_stats_enabled_mutex);
4293 static_key_slow_dec(&bpf_stats_enabled_key.key);
4294 mutex_unlock(&bpf_stats_enabled_mutex);
4295 return 0;
4296}
4297
4298static const struct file_operations bpf_stats_fops = {
4299 .release = bpf_stats_release,
4300};
4301
4302static int bpf_enable_runtime_stats(void)
4303{
4304 int fd;
4305
4306 mutex_lock(&bpf_stats_enabled_mutex);
4307
4308 /* Set a very high limit to avoid overflow */
4309 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4310 mutex_unlock(&bpf_stats_enabled_mutex);
4311 return -EBUSY;
4312 }
4313
4314 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4315 if (fd >= 0)
4316 static_key_slow_inc(&bpf_stats_enabled_key.key);
4317
4318 mutex_unlock(&bpf_stats_enabled_mutex);
4319 return fd;
4320}
4321
4322#define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4323
4324static int bpf_enable_stats(union bpf_attr *attr)
4325{
4326
4327 if (CHECK_ATTR(BPF_ENABLE_STATS))
4328 return -EINVAL;
4329
4330 if (!capable(CAP_SYS_ADMIN))
4331 return -EPERM;
4332
4333 switch (attr->enable_stats.type) {
4334 case BPF_STATS_RUN_TIME:
4335 return bpf_enable_runtime_stats();
4336 default:
4337 break;
4338 }
4339 return -EINVAL;
4340}
4341
4342#define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4343
4344static int bpf_iter_create(union bpf_attr *attr)
4345{
4346 struct bpf_link *link;
4347 int err;
4348
4349 if (CHECK_ATTR(BPF_ITER_CREATE))
4350 return -EINVAL;
4351
4352 if (attr->iter_create.flags)
4353 return -EINVAL;
4354
4355 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4356 if (IS_ERR(link))
4357 return PTR_ERR(link);
4358
4359 err = bpf_iter_new_fd(link);
4360 bpf_link_put(link);
4361
4362 return err;
4363}
4364
4365#define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4366
4367static int bpf_prog_bind_map(union bpf_attr *attr)
4368{
4369 struct bpf_prog *prog;
4370 struct bpf_map *map;
4371 struct bpf_map **used_maps_old, **used_maps_new;
4372 int i, ret = 0;
4373
4374 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4375 return -EINVAL;
4376
4377 if (attr->prog_bind_map.flags)
4378 return -EINVAL;
4379
4380 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4381 if (IS_ERR(prog))
4382 return PTR_ERR(prog);
4383
4384 map = bpf_map_get(attr->prog_bind_map.map_fd);
4385 if (IS_ERR(map)) {
4386 ret = PTR_ERR(map);
4387 goto out_prog_put;
4388 }
4389
4390 mutex_lock(&prog->aux->used_maps_mutex);
4391
4392 used_maps_old = prog->aux->used_maps;
4393
4394 for (i = 0; i < prog->aux->used_map_cnt; i++)
4395 if (used_maps_old[i] == map) {
4396 bpf_map_put(map);
4397 goto out_unlock;
4398 }
4399
4400 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4401 sizeof(used_maps_new[0]),
4402 GFP_KERNEL);
4403 if (!used_maps_new) {
4404 ret = -ENOMEM;
4405 goto out_unlock;
4406 }
4407
4408 memcpy(used_maps_new, used_maps_old,
4409 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4410 used_maps_new[prog->aux->used_map_cnt] = map;
4411
4412 prog->aux->used_map_cnt++;
4413 prog->aux->used_maps = used_maps_new;
4414
4415 kfree(used_maps_old);
4416
4417out_unlock:
4418 mutex_unlock(&prog->aux->used_maps_mutex);
4419
4420 if (ret)
4421 bpf_map_put(map);
4422out_prog_put:
4423 bpf_prog_put(prog);
4424 return ret;
4425}
4426
4427static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
4428{
4429 union bpf_attr attr;
4430 int err;
4431
4432 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
4433 return -EPERM;
4434
4435 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4436 if (err)
4437 return err;
4438 size = min_t(u32, size, sizeof(attr));
4439
4440 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4441 memset(&attr, 0, sizeof(attr));
4442 if (copy_from_bpfptr(&attr, uattr, size) != 0)
4443 return -EFAULT;
4444
4445 err = security_bpf(cmd, &attr, size);
4446 if (err < 0)
4447 return err;
4448
4449 switch (cmd) {
4450 case BPF_MAP_CREATE:
4451 err = map_create(&attr);
4452 break;
4453 case BPF_MAP_LOOKUP_ELEM:
4454 err = map_lookup_elem(&attr);
4455 break;
4456 case BPF_MAP_UPDATE_ELEM:
4457 err = map_update_elem(&attr, uattr);
4458 break;
4459 case BPF_MAP_DELETE_ELEM:
4460 err = map_delete_elem(&attr);
4461 break;
4462 case BPF_MAP_GET_NEXT_KEY:
4463 err = map_get_next_key(&attr);
4464 break;
4465 case BPF_MAP_FREEZE:
4466 err = map_freeze(&attr);
4467 break;
4468 case BPF_PROG_LOAD:
4469 err = bpf_prog_load(&attr, uattr);
4470 break;
4471 case BPF_OBJ_PIN:
4472 err = bpf_obj_pin(&attr);
4473 break;
4474 case BPF_OBJ_GET:
4475 err = bpf_obj_get(&attr);
4476 break;
4477 case BPF_PROG_ATTACH:
4478 err = bpf_prog_attach(&attr);
4479 break;
4480 case BPF_PROG_DETACH:
4481 err = bpf_prog_detach(&attr);
4482 break;
4483 case BPF_PROG_QUERY:
4484 err = bpf_prog_query(&attr, uattr.user);
4485 break;
4486 case BPF_PROG_TEST_RUN:
4487 err = bpf_prog_test_run(&attr, uattr.user);
4488 break;
4489 case BPF_PROG_GET_NEXT_ID:
4490 err = bpf_obj_get_next_id(&attr, uattr.user,
4491 &prog_idr, &prog_idr_lock);
4492 break;
4493 case BPF_MAP_GET_NEXT_ID:
4494 err = bpf_obj_get_next_id(&attr, uattr.user,
4495 &map_idr, &map_idr_lock);
4496 break;
4497 case BPF_BTF_GET_NEXT_ID:
4498 err = bpf_obj_get_next_id(&attr, uattr.user,
4499 &btf_idr, &btf_idr_lock);
4500 break;
4501 case BPF_PROG_GET_FD_BY_ID:
4502 err = bpf_prog_get_fd_by_id(&attr);
4503 break;
4504 case BPF_MAP_GET_FD_BY_ID:
4505 err = bpf_map_get_fd_by_id(&attr);
4506 break;
4507 case BPF_OBJ_GET_INFO_BY_FD:
4508 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
4509 break;
4510 case BPF_RAW_TRACEPOINT_OPEN:
4511 err = bpf_raw_tracepoint_open(&attr);
4512 break;
4513 case BPF_BTF_LOAD:
4514 err = bpf_btf_load(&attr, uattr);
4515 break;
4516 case BPF_BTF_GET_FD_BY_ID:
4517 err = bpf_btf_get_fd_by_id(&attr);
4518 break;
4519 case BPF_TASK_FD_QUERY:
4520 err = bpf_task_fd_query(&attr, uattr.user);
4521 break;
4522 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4523 err = map_lookup_and_delete_elem(&attr);
4524 break;
4525 case BPF_MAP_LOOKUP_BATCH:
4526 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
4527 break;
4528 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4529 err = bpf_map_do_batch(&attr, uattr.user,
4530 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4531 break;
4532 case BPF_MAP_UPDATE_BATCH:
4533 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
4534 break;
4535 case BPF_MAP_DELETE_BATCH:
4536 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
4537 break;
4538 case BPF_LINK_CREATE:
4539 err = link_create(&attr, uattr);
4540 break;
4541 case BPF_LINK_UPDATE:
4542 err = link_update(&attr);
4543 break;
4544 case BPF_LINK_GET_FD_BY_ID:
4545 err = bpf_link_get_fd_by_id(&attr);
4546 break;
4547 case BPF_LINK_GET_NEXT_ID:
4548 err = bpf_obj_get_next_id(&attr, uattr.user,
4549 &link_idr, &link_idr_lock);
4550 break;
4551 case BPF_ENABLE_STATS:
4552 err = bpf_enable_stats(&attr);
4553 break;
4554 case BPF_ITER_CREATE:
4555 err = bpf_iter_create(&attr);
4556 break;
4557 case BPF_LINK_DETACH:
4558 err = link_detach(&attr);
4559 break;
4560 case BPF_PROG_BIND_MAP:
4561 err = bpf_prog_bind_map(&attr);
4562 break;
4563 default:
4564 err = -EINVAL;
4565 break;
4566 }
4567
4568 return err;
4569}
4570
4571SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4572{
4573 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
4574}
4575
4576static bool syscall_prog_is_valid_access(int off, int size,
4577 enum bpf_access_type type,
4578 const struct bpf_prog *prog,
4579 struct bpf_insn_access_aux *info)
4580{
4581 if (off < 0 || off >= U16_MAX)
4582 return false;
4583 if (off % size != 0)
4584 return false;
4585 return true;
4586}
4587
4588BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size)
4589{
4590 switch (cmd) {
4591 case BPF_MAP_CREATE:
4592 case BPF_MAP_UPDATE_ELEM:
4593 case BPF_MAP_FREEZE:
4594 case BPF_PROG_LOAD:
4595 case BPF_BTF_LOAD:
4596 break;
4597 /* case BPF_PROG_TEST_RUN:
4598 * is not part of this list to prevent recursive test_run
4599 */
4600 default:
4601 return -EINVAL;
4602 }
4603 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
4604}
4605
4606static const struct bpf_func_proto bpf_sys_bpf_proto = {
4607 .func = bpf_sys_bpf,
4608 .gpl_only = false,
4609 .ret_type = RET_INTEGER,
4610 .arg1_type = ARG_ANYTHING,
4611 .arg2_type = ARG_PTR_TO_MEM,
4612 .arg3_type = ARG_CONST_SIZE,
4613};
4614
4615const struct bpf_func_proto * __weak
4616tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4617{
4618 return bpf_base_func_proto(func_id);
4619}
4620
4621BPF_CALL_1(bpf_sys_close, u32, fd)
4622{
4623 /* When bpf program calls this helper there should not be
4624 * an fdget() without matching completed fdput().
4625 * This helper is allowed in the following callchain only:
4626 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
4627 */
4628 return close_fd(fd);
4629}
4630
4631static const struct bpf_func_proto bpf_sys_close_proto = {
4632 .func = bpf_sys_close,
4633 .gpl_only = false,
4634 .ret_type = RET_INTEGER,
4635 .arg1_type = ARG_ANYTHING,
4636};
4637
4638static const struct bpf_func_proto *
4639syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4640{
4641 switch (func_id) {
4642 case BPF_FUNC_sys_bpf:
4643 return &bpf_sys_bpf_proto;
4644 case BPF_FUNC_btf_find_by_name_kind:
4645 return &bpf_btf_find_by_name_kind_proto;
4646 case BPF_FUNC_sys_close:
4647 return &bpf_sys_close_proto;
4648 default:
4649 return tracing_prog_func_proto(func_id, prog);
4650 }
4651}
4652
4653const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
4654 .get_func_proto = syscall_prog_func_proto,
4655 .is_valid_access = syscall_prog_is_valid_access,
4656};
4657
4658const struct bpf_prog_ops bpf_syscall_prog_ops = {
4659 .test_run = bpf_prog_test_run_syscall,
4660};