Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / kernel / bpf / syscall.c
blobc3bb03c8371fc7aabc60245780582a6fed05b12a
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>
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))
43 #define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
45 DEFINE_PER_CPU(int, bpf_prog_active);
46 static DEFINE_IDR(prog_idr);
47 static DEFINE_SPINLOCK(prog_idr_lock);
48 static DEFINE_IDR(map_idr);
49 static DEFINE_SPINLOCK(map_idr_lock);
50 static DEFINE_IDR(link_idr);
51 static DEFINE_SPINLOCK(link_idr_lock);
53 int sysctl_unprivileged_bpf_disabled __read_mostly;
55 static const struct bpf_map_ops * const bpf_map_types[] = {
56 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
57 #define BPF_MAP_TYPE(_id, _ops) \
58 [_id] = &_ops,
59 #define BPF_LINK_TYPE(_id, _name)
60 #include <linux/bpf_types.h>
61 #undef BPF_PROG_TYPE
62 #undef BPF_MAP_TYPE
63 #undef BPF_LINK_TYPE
67 * If we're handed a bigger struct than we know of, ensure all the unknown bits
68 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
69 * we don't know about yet.
71 * There is a ToCToU between this function call and the following
72 * copy_from_user() call. However, this is not a concern since this function is
73 * meant to be a future-proofing of bits.
75 int bpf_check_uarg_tail_zero(void __user *uaddr,
76 size_t expected_size,
77 size_t actual_size)
79 unsigned char __user *addr = uaddr + expected_size;
80 int res;
82 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
83 return -E2BIG;
85 if (actual_size <= expected_size)
86 return 0;
88 res = check_zeroed_user(addr, actual_size - expected_size);
89 if (res < 0)
90 return res;
91 return res ? 0 : -E2BIG;
94 const struct bpf_map_ops bpf_map_offload_ops = {
95 .map_meta_equal = bpf_map_meta_equal,
96 .map_alloc = bpf_map_offload_map_alloc,
97 .map_free = bpf_map_offload_map_free,
98 .map_check_btf = map_check_no_btf,
101 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
103 const struct bpf_map_ops *ops;
104 u32 type = attr->map_type;
105 struct bpf_map *map;
106 int err;
108 if (type >= ARRAY_SIZE(bpf_map_types))
109 return ERR_PTR(-EINVAL);
110 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
111 ops = bpf_map_types[type];
112 if (!ops)
113 return ERR_PTR(-EINVAL);
115 if (ops->map_alloc_check) {
116 err = ops->map_alloc_check(attr);
117 if (err)
118 return ERR_PTR(err);
120 if (attr->map_ifindex)
121 ops = &bpf_map_offload_ops;
122 map = ops->map_alloc(attr);
123 if (IS_ERR(map))
124 return map;
125 map->ops = ops;
126 map->map_type = type;
127 return map;
130 static u32 bpf_map_value_size(const struct bpf_map *map)
132 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
133 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
134 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
135 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
136 return round_up(map->value_size, 8) * num_possible_cpus();
137 else if (IS_FD_MAP(map))
138 return sizeof(u32);
139 else
140 return map->value_size;
143 static void maybe_wait_bpf_programs(struct bpf_map *map)
145 /* Wait for any running BPF programs to complete so that
146 * userspace, when we return to it, knows that all programs
147 * that could be running use the new map value.
149 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
150 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
151 synchronize_rcu();
154 static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
155 void *value, __u64 flags)
157 int err;
159 /* Need to create a kthread, thus must support schedule */
160 if (bpf_map_is_dev_bound(map)) {
161 return bpf_map_offload_update_elem(map, key, value, flags);
162 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
163 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
164 return map->ops->map_update_elem(map, key, value, flags);
165 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
166 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
167 return sock_map_update_elem_sys(map, key, value, flags);
168 } else if (IS_FD_PROG_ARRAY(map)) {
169 return bpf_fd_array_map_update_elem(map, f.file, key, value,
170 flags);
173 bpf_disable_instrumentation();
174 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
175 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
176 err = bpf_percpu_hash_update(map, key, value, flags);
177 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
178 err = bpf_percpu_array_update(map, key, value, flags);
179 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
180 err = bpf_percpu_cgroup_storage_update(map, key, value,
181 flags);
182 } else if (IS_FD_ARRAY(map)) {
183 rcu_read_lock();
184 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
185 flags);
186 rcu_read_unlock();
187 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
188 rcu_read_lock();
189 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
190 flags);
191 rcu_read_unlock();
192 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
193 /* rcu_read_lock() is not needed */
194 err = bpf_fd_reuseport_array_update_elem(map, key, value,
195 flags);
196 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
197 map->map_type == BPF_MAP_TYPE_STACK) {
198 err = map->ops->map_push_elem(map, value, flags);
199 } else {
200 rcu_read_lock();
201 err = map->ops->map_update_elem(map, key, value, flags);
202 rcu_read_unlock();
204 bpf_enable_instrumentation();
205 maybe_wait_bpf_programs(map);
207 return err;
210 static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
211 __u64 flags)
213 void *ptr;
214 int err;
216 if (bpf_map_is_dev_bound(map))
217 return bpf_map_offload_lookup_elem(map, key, value);
219 bpf_disable_instrumentation();
220 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
221 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
222 err = bpf_percpu_hash_copy(map, key, value);
223 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
224 err = bpf_percpu_array_copy(map, key, value);
225 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
226 err = bpf_percpu_cgroup_storage_copy(map, key, value);
227 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
228 err = bpf_stackmap_copy(map, key, value);
229 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
230 err = bpf_fd_array_map_lookup_elem(map, key, value);
231 } else if (IS_FD_HASH(map)) {
232 err = bpf_fd_htab_map_lookup_elem(map, key, value);
233 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
234 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
235 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
236 map->map_type == BPF_MAP_TYPE_STACK) {
237 err = map->ops->map_peek_elem(map, value);
238 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
239 /* struct_ops map requires directly updating "value" */
240 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
241 } else {
242 rcu_read_lock();
243 if (map->ops->map_lookup_elem_sys_only)
244 ptr = map->ops->map_lookup_elem_sys_only(map, key);
245 else
246 ptr = map->ops->map_lookup_elem(map, key);
247 if (IS_ERR(ptr)) {
248 err = PTR_ERR(ptr);
249 } else if (!ptr) {
250 err = -ENOENT;
251 } else {
252 err = 0;
253 if (flags & BPF_F_LOCK)
254 /* lock 'ptr' and copy everything but lock */
255 copy_map_value_locked(map, value, ptr, true);
256 else
257 copy_map_value(map, value, ptr);
258 /* mask lock, since value wasn't zero inited */
259 check_and_init_map_lock(map, value);
261 rcu_read_unlock();
264 bpf_enable_instrumentation();
265 maybe_wait_bpf_programs(map);
267 return err;
270 /* Please, do not use this function outside from the map creation path
271 * (e.g. in map update path) without taking care of setting the active
272 * memory cgroup (see at bpf_map_kmalloc_node() for example).
274 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
276 /* We really just want to fail instead of triggering OOM killer
277 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
278 * which is used for lower order allocation requests.
280 * It has been observed that higher order allocation requests done by
281 * vmalloc with __GFP_NORETRY being set might fail due to not trying
282 * to reclaim memory from the page cache, thus we set
283 * __GFP_RETRY_MAYFAIL to avoid such situations.
286 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
287 unsigned int flags = 0;
288 unsigned long align = 1;
289 void *area;
291 if (size >= SIZE_MAX)
292 return NULL;
294 /* kmalloc()'ed memory can't be mmap()'ed */
295 if (mmapable) {
296 BUG_ON(!PAGE_ALIGNED(size));
297 align = SHMLBA;
298 flags = VM_USERMAP;
299 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
300 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
301 numa_node);
302 if (area != NULL)
303 return area;
306 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
307 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
308 flags, numa_node, __builtin_return_address(0));
311 void *bpf_map_area_alloc(u64 size, int numa_node)
313 return __bpf_map_area_alloc(size, numa_node, false);
316 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
318 return __bpf_map_area_alloc(size, numa_node, true);
321 void bpf_map_area_free(void *area)
323 kvfree(area);
326 static u32 bpf_map_flags_retain_permanent(u32 flags)
328 /* Some map creation flags are not tied to the map object but
329 * rather to the map fd instead, so they have no meaning upon
330 * map object inspection since multiple file descriptors with
331 * different (access) properties can exist here. Thus, given
332 * this has zero meaning for the map itself, lets clear these
333 * from here.
335 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
338 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
340 map->map_type = attr->map_type;
341 map->key_size = attr->key_size;
342 map->value_size = attr->value_size;
343 map->max_entries = attr->max_entries;
344 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
345 map->numa_node = bpf_map_attr_numa_node(attr);
348 static int bpf_map_alloc_id(struct bpf_map *map)
350 int id;
352 idr_preload(GFP_KERNEL);
353 spin_lock_bh(&map_idr_lock);
354 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
355 if (id > 0)
356 map->id = id;
357 spin_unlock_bh(&map_idr_lock);
358 idr_preload_end();
360 if (WARN_ON_ONCE(!id))
361 return -ENOSPC;
363 return id > 0 ? 0 : id;
366 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
368 unsigned long flags;
370 /* Offloaded maps are removed from the IDR store when their device
371 * disappears - even if someone holds an fd to them they are unusable,
372 * the memory is gone, all ops will fail; they are simply waiting for
373 * refcnt to drop to be freed.
375 if (!map->id)
376 return;
378 if (do_idr_lock)
379 spin_lock_irqsave(&map_idr_lock, flags);
380 else
381 __acquire(&map_idr_lock);
383 idr_remove(&map_idr, map->id);
384 map->id = 0;
386 if (do_idr_lock)
387 spin_unlock_irqrestore(&map_idr_lock, flags);
388 else
389 __release(&map_idr_lock);
392 #ifdef CONFIG_MEMCG_KMEM
393 static void bpf_map_save_memcg(struct bpf_map *map)
395 map->memcg = get_mem_cgroup_from_mm(current->mm);
398 static void bpf_map_release_memcg(struct bpf_map *map)
400 mem_cgroup_put(map->memcg);
403 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
404 int node)
406 struct mem_cgroup *old_memcg;
407 void *ptr;
409 old_memcg = set_active_memcg(map->memcg);
410 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
411 set_active_memcg(old_memcg);
413 return ptr;
416 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
418 struct mem_cgroup *old_memcg;
419 void *ptr;
421 old_memcg = set_active_memcg(map->memcg);
422 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
423 set_active_memcg(old_memcg);
425 return ptr;
428 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
429 size_t align, gfp_t flags)
431 struct mem_cgroup *old_memcg;
432 void __percpu *ptr;
434 old_memcg = set_active_memcg(map->memcg);
435 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
436 set_active_memcg(old_memcg);
438 return ptr;
441 #else
442 static void bpf_map_save_memcg(struct bpf_map *map)
446 static void bpf_map_release_memcg(struct bpf_map *map)
449 #endif
451 /* called from workqueue */
452 static void bpf_map_free_deferred(struct work_struct *work)
454 struct bpf_map *map = container_of(work, struct bpf_map, work);
456 security_bpf_map_free(map);
457 bpf_map_release_memcg(map);
458 /* implementation dependent freeing */
459 map->ops->map_free(map);
462 static void bpf_map_put_uref(struct bpf_map *map)
464 if (atomic64_dec_and_test(&map->usercnt)) {
465 if (map->ops->map_release_uref)
466 map->ops->map_release_uref(map);
470 /* decrement map refcnt and schedule it for freeing via workqueue
471 * (unrelying map implementation ops->map_free() might sleep)
473 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
475 if (atomic64_dec_and_test(&map->refcnt)) {
476 /* bpf_map_free_id() must be called first */
477 bpf_map_free_id(map, do_idr_lock);
478 btf_put(map->btf);
479 INIT_WORK(&map->work, bpf_map_free_deferred);
480 schedule_work(&map->work);
484 void bpf_map_put(struct bpf_map *map)
486 __bpf_map_put(map, true);
488 EXPORT_SYMBOL_GPL(bpf_map_put);
490 void bpf_map_put_with_uref(struct bpf_map *map)
492 bpf_map_put_uref(map);
493 bpf_map_put(map);
496 static int bpf_map_release(struct inode *inode, struct file *filp)
498 struct bpf_map *map = filp->private_data;
500 if (map->ops->map_release)
501 map->ops->map_release(map, filp);
503 bpf_map_put_with_uref(map);
504 return 0;
507 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
509 fmode_t mode = f.file->f_mode;
511 /* Our file permissions may have been overridden by global
512 * map permissions facing syscall side.
514 if (READ_ONCE(map->frozen))
515 mode &= ~FMODE_CAN_WRITE;
516 return mode;
519 #ifdef CONFIG_PROC_FS
520 /* Provides an approximation of the map's memory footprint.
521 * Used only to provide a backward compatibility and display
522 * a reasonable "memlock" info.
524 static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
526 unsigned long size;
528 size = round_up(map->key_size + bpf_map_value_size(map), 8);
530 return round_up(map->max_entries * size, PAGE_SIZE);
533 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
535 const struct bpf_map *map = filp->private_data;
536 const struct bpf_array *array;
537 u32 type = 0, jited = 0;
539 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
540 array = container_of(map, struct bpf_array, map);
541 type = array->aux->type;
542 jited = array->aux->jited;
545 seq_printf(m,
546 "map_type:\t%u\n"
547 "key_size:\t%u\n"
548 "value_size:\t%u\n"
549 "max_entries:\t%u\n"
550 "map_flags:\t%#x\n"
551 "memlock:\t%lu\n"
552 "map_id:\t%u\n"
553 "frozen:\t%u\n",
554 map->map_type,
555 map->key_size,
556 map->value_size,
557 map->max_entries,
558 map->map_flags,
559 bpf_map_memory_footprint(map),
560 map->id,
561 READ_ONCE(map->frozen));
562 if (type) {
563 seq_printf(m, "owner_prog_type:\t%u\n", type);
564 seq_printf(m, "owner_jited:\t%u\n", jited);
567 #endif
569 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
570 loff_t *ppos)
572 /* We need this handler such that alloc_file() enables
573 * f_mode with FMODE_CAN_READ.
575 return -EINVAL;
578 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
579 size_t siz, loff_t *ppos)
581 /* We need this handler such that alloc_file() enables
582 * f_mode with FMODE_CAN_WRITE.
584 return -EINVAL;
587 /* called for any extra memory-mapped regions (except initial) */
588 static void bpf_map_mmap_open(struct vm_area_struct *vma)
590 struct bpf_map *map = vma->vm_file->private_data;
592 if (vma->vm_flags & VM_MAYWRITE) {
593 mutex_lock(&map->freeze_mutex);
594 map->writecnt++;
595 mutex_unlock(&map->freeze_mutex);
599 /* called for all unmapped memory region (including initial) */
600 static void bpf_map_mmap_close(struct vm_area_struct *vma)
602 struct bpf_map *map = vma->vm_file->private_data;
604 if (vma->vm_flags & VM_MAYWRITE) {
605 mutex_lock(&map->freeze_mutex);
606 map->writecnt--;
607 mutex_unlock(&map->freeze_mutex);
611 static const struct vm_operations_struct bpf_map_default_vmops = {
612 .open = bpf_map_mmap_open,
613 .close = bpf_map_mmap_close,
616 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
618 struct bpf_map *map = filp->private_data;
619 int err;
621 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
622 return -ENOTSUPP;
624 if (!(vma->vm_flags & VM_SHARED))
625 return -EINVAL;
627 mutex_lock(&map->freeze_mutex);
629 if (vma->vm_flags & VM_WRITE) {
630 if (map->frozen) {
631 err = -EPERM;
632 goto out;
634 /* map is meant to be read-only, so do not allow mapping as
635 * writable, because it's possible to leak a writable page
636 * reference and allows user-space to still modify it after
637 * freezing, while verifier will assume contents do not change
639 if (map->map_flags & BPF_F_RDONLY_PROG) {
640 err = -EACCES;
641 goto out;
645 /* set default open/close callbacks */
646 vma->vm_ops = &bpf_map_default_vmops;
647 vma->vm_private_data = map;
648 vma->vm_flags &= ~VM_MAYEXEC;
649 if (!(vma->vm_flags & VM_WRITE))
650 /* disallow re-mapping with PROT_WRITE */
651 vma->vm_flags &= ~VM_MAYWRITE;
653 err = map->ops->map_mmap(map, vma);
654 if (err)
655 goto out;
657 if (vma->vm_flags & VM_MAYWRITE)
658 map->writecnt++;
659 out:
660 mutex_unlock(&map->freeze_mutex);
661 return err;
664 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
666 struct bpf_map *map = filp->private_data;
668 if (map->ops->map_poll)
669 return map->ops->map_poll(map, filp, pts);
671 return EPOLLERR;
674 const struct file_operations bpf_map_fops = {
675 #ifdef CONFIG_PROC_FS
676 .show_fdinfo = bpf_map_show_fdinfo,
677 #endif
678 .release = bpf_map_release,
679 .read = bpf_dummy_read,
680 .write = bpf_dummy_write,
681 .mmap = bpf_map_mmap,
682 .poll = bpf_map_poll,
685 int bpf_map_new_fd(struct bpf_map *map, int flags)
687 int ret;
689 ret = security_bpf_map(map, OPEN_FMODE(flags));
690 if (ret < 0)
691 return ret;
693 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
694 flags | O_CLOEXEC);
697 int bpf_get_file_flag(int flags)
699 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
700 return -EINVAL;
701 if (flags & BPF_F_RDONLY)
702 return O_RDONLY;
703 if (flags & BPF_F_WRONLY)
704 return O_WRONLY;
705 return O_RDWR;
708 /* helper macro to check that unused fields 'union bpf_attr' are zero */
709 #define CHECK_ATTR(CMD) \
710 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
711 sizeof(attr->CMD##_LAST_FIELD), 0, \
712 sizeof(*attr) - \
713 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
714 sizeof(attr->CMD##_LAST_FIELD)) != NULL
716 /* dst and src must have at least "size" number of bytes.
717 * Return strlen on success and < 0 on error.
719 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
721 const char *end = src + size;
722 const char *orig_src = src;
724 memset(dst, 0, size);
725 /* Copy all isalnum(), '_' and '.' chars. */
726 while (src < end && *src) {
727 if (!isalnum(*src) &&
728 *src != '_' && *src != '.')
729 return -EINVAL;
730 *dst++ = *src++;
733 /* No '\0' found in "size" number of bytes */
734 if (src == end)
735 return -EINVAL;
737 return src - orig_src;
740 int map_check_no_btf(const struct bpf_map *map,
741 const struct btf *btf,
742 const struct btf_type *key_type,
743 const struct btf_type *value_type)
745 return -ENOTSUPP;
748 static int map_check_btf(struct bpf_map *map, const struct btf *btf,
749 u32 btf_key_id, u32 btf_value_id)
751 const struct btf_type *key_type, *value_type;
752 u32 key_size, value_size;
753 int ret = 0;
755 /* Some maps allow key to be unspecified. */
756 if (btf_key_id) {
757 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
758 if (!key_type || key_size != map->key_size)
759 return -EINVAL;
760 } else {
761 key_type = btf_type_by_id(btf, 0);
762 if (!map->ops->map_check_btf)
763 return -EINVAL;
766 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
767 if (!value_type || value_size != map->value_size)
768 return -EINVAL;
770 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
772 if (map_value_has_spin_lock(map)) {
773 if (map->map_flags & BPF_F_RDONLY_PROG)
774 return -EACCES;
775 if (map->map_type != BPF_MAP_TYPE_HASH &&
776 map->map_type != BPF_MAP_TYPE_ARRAY &&
777 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
778 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
779 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
780 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
781 return -ENOTSUPP;
782 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
783 map->value_size) {
784 WARN_ONCE(1,
785 "verifier bug spin_lock_off %d value_size %d\n",
786 map->spin_lock_off, map->value_size);
787 return -EFAULT;
791 if (map->ops->map_check_btf)
792 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
794 return ret;
797 #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
798 /* called via syscall */
799 static int map_create(union bpf_attr *attr)
801 int numa_node = bpf_map_attr_numa_node(attr);
802 struct bpf_map *map;
803 int f_flags;
804 int err;
806 err = CHECK_ATTR(BPF_MAP_CREATE);
807 if (err)
808 return -EINVAL;
810 if (attr->btf_vmlinux_value_type_id) {
811 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
812 attr->btf_key_type_id || attr->btf_value_type_id)
813 return -EINVAL;
814 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
815 return -EINVAL;
818 f_flags = bpf_get_file_flag(attr->map_flags);
819 if (f_flags < 0)
820 return f_flags;
822 if (numa_node != NUMA_NO_NODE &&
823 ((unsigned int)numa_node >= nr_node_ids ||
824 !node_online(numa_node)))
825 return -EINVAL;
827 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
828 map = find_and_alloc_map(attr);
829 if (IS_ERR(map))
830 return PTR_ERR(map);
832 err = bpf_obj_name_cpy(map->name, attr->map_name,
833 sizeof(attr->map_name));
834 if (err < 0)
835 goto free_map;
837 atomic64_set(&map->refcnt, 1);
838 atomic64_set(&map->usercnt, 1);
839 mutex_init(&map->freeze_mutex);
841 map->spin_lock_off = -EINVAL;
842 if (attr->btf_key_type_id || attr->btf_value_type_id ||
843 /* Even the map's value is a kernel's struct,
844 * the bpf_prog.o must have BTF to begin with
845 * to figure out the corresponding kernel's
846 * counter part. Thus, attr->btf_fd has
847 * to be valid also.
849 attr->btf_vmlinux_value_type_id) {
850 struct btf *btf;
852 btf = btf_get_by_fd(attr->btf_fd);
853 if (IS_ERR(btf)) {
854 err = PTR_ERR(btf);
855 goto free_map;
857 map->btf = btf;
859 if (attr->btf_value_type_id) {
860 err = map_check_btf(map, btf, attr->btf_key_type_id,
861 attr->btf_value_type_id);
862 if (err)
863 goto free_map;
866 map->btf_key_type_id = attr->btf_key_type_id;
867 map->btf_value_type_id = attr->btf_value_type_id;
868 map->btf_vmlinux_value_type_id =
869 attr->btf_vmlinux_value_type_id;
872 err = security_bpf_map_alloc(map);
873 if (err)
874 goto free_map;
876 err = bpf_map_alloc_id(map);
877 if (err)
878 goto free_map_sec;
880 bpf_map_save_memcg(map);
882 err = bpf_map_new_fd(map, f_flags);
883 if (err < 0) {
884 /* failed to allocate fd.
885 * bpf_map_put_with_uref() is needed because the above
886 * bpf_map_alloc_id() has published the map
887 * to the userspace and the userspace may
888 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
890 bpf_map_put_with_uref(map);
891 return err;
894 return err;
896 free_map_sec:
897 security_bpf_map_free(map);
898 free_map:
899 btf_put(map->btf);
900 map->ops->map_free(map);
901 return err;
904 /* if error is returned, fd is released.
905 * On success caller should complete fd access with matching fdput()
907 struct bpf_map *__bpf_map_get(struct fd f)
909 if (!f.file)
910 return ERR_PTR(-EBADF);
911 if (f.file->f_op != &bpf_map_fops) {
912 fdput(f);
913 return ERR_PTR(-EINVAL);
916 return f.file->private_data;
919 void bpf_map_inc(struct bpf_map *map)
921 atomic64_inc(&map->refcnt);
923 EXPORT_SYMBOL_GPL(bpf_map_inc);
925 void bpf_map_inc_with_uref(struct bpf_map *map)
927 atomic64_inc(&map->refcnt);
928 atomic64_inc(&map->usercnt);
930 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
932 struct bpf_map *bpf_map_get(u32 ufd)
934 struct fd f = fdget(ufd);
935 struct bpf_map *map;
937 map = __bpf_map_get(f);
938 if (IS_ERR(map))
939 return map;
941 bpf_map_inc(map);
942 fdput(f);
944 return map;
947 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
949 struct fd f = fdget(ufd);
950 struct bpf_map *map;
952 map = __bpf_map_get(f);
953 if (IS_ERR(map))
954 return map;
956 bpf_map_inc_with_uref(map);
957 fdput(f);
959 return map;
962 /* map_idr_lock should have been held */
963 static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
965 int refold;
967 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
968 if (!refold)
969 return ERR_PTR(-ENOENT);
970 if (uref)
971 atomic64_inc(&map->usercnt);
973 return map;
976 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
978 spin_lock_bh(&map_idr_lock);
979 map = __bpf_map_inc_not_zero(map, false);
980 spin_unlock_bh(&map_idr_lock);
982 return map;
984 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
986 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
988 return -ENOTSUPP;
991 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
993 if (key_size)
994 return memdup_user(ukey, key_size);
996 if (ukey)
997 return ERR_PTR(-EINVAL);
999 return NULL;
1002 /* last field in 'union bpf_attr' used by this command */
1003 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1005 static int map_lookup_elem(union bpf_attr *attr)
1007 void __user *ukey = u64_to_user_ptr(attr->key);
1008 void __user *uvalue = u64_to_user_ptr(attr->value);
1009 int ufd = attr->map_fd;
1010 struct bpf_map *map;
1011 void *key, *value;
1012 u32 value_size;
1013 struct fd f;
1014 int err;
1016 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1017 return -EINVAL;
1019 if (attr->flags & ~BPF_F_LOCK)
1020 return -EINVAL;
1022 f = fdget(ufd);
1023 map = __bpf_map_get(f);
1024 if (IS_ERR(map))
1025 return PTR_ERR(map);
1026 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1027 err = -EPERM;
1028 goto err_put;
1031 if ((attr->flags & BPF_F_LOCK) &&
1032 !map_value_has_spin_lock(map)) {
1033 err = -EINVAL;
1034 goto err_put;
1037 key = __bpf_copy_key(ukey, map->key_size);
1038 if (IS_ERR(key)) {
1039 err = PTR_ERR(key);
1040 goto err_put;
1043 value_size = bpf_map_value_size(map);
1045 err = -ENOMEM;
1046 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1047 if (!value)
1048 goto free_key;
1050 err = bpf_map_copy_value(map, key, value, attr->flags);
1051 if (err)
1052 goto free_value;
1054 err = -EFAULT;
1055 if (copy_to_user(uvalue, value, value_size) != 0)
1056 goto free_value;
1058 err = 0;
1060 free_value:
1061 kfree(value);
1062 free_key:
1063 kfree(key);
1064 err_put:
1065 fdput(f);
1066 return err;
1070 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1072 static int map_update_elem(union bpf_attr *attr)
1074 void __user *ukey = u64_to_user_ptr(attr->key);
1075 void __user *uvalue = u64_to_user_ptr(attr->value);
1076 int ufd = attr->map_fd;
1077 struct bpf_map *map;
1078 void *key, *value;
1079 u32 value_size;
1080 struct fd f;
1081 int err;
1083 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1084 return -EINVAL;
1086 f = fdget(ufd);
1087 map = __bpf_map_get(f);
1088 if (IS_ERR(map))
1089 return PTR_ERR(map);
1090 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1091 err = -EPERM;
1092 goto err_put;
1095 if ((attr->flags & BPF_F_LOCK) &&
1096 !map_value_has_spin_lock(map)) {
1097 err = -EINVAL;
1098 goto err_put;
1101 key = __bpf_copy_key(ukey, map->key_size);
1102 if (IS_ERR(key)) {
1103 err = PTR_ERR(key);
1104 goto err_put;
1107 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1108 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
1109 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1110 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
1111 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1112 else
1113 value_size = map->value_size;
1115 err = -ENOMEM;
1116 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1117 if (!value)
1118 goto free_key;
1120 err = -EFAULT;
1121 if (copy_from_user(value, uvalue, value_size) != 0)
1122 goto free_value;
1124 err = bpf_map_update_value(map, f, key, value, attr->flags);
1126 free_value:
1127 kfree(value);
1128 free_key:
1129 kfree(key);
1130 err_put:
1131 fdput(f);
1132 return err;
1135 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1137 static int map_delete_elem(union bpf_attr *attr)
1139 void __user *ukey = u64_to_user_ptr(attr->key);
1140 int ufd = attr->map_fd;
1141 struct bpf_map *map;
1142 struct fd f;
1143 void *key;
1144 int err;
1146 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1147 return -EINVAL;
1149 f = fdget(ufd);
1150 map = __bpf_map_get(f);
1151 if (IS_ERR(map))
1152 return PTR_ERR(map);
1153 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1154 err = -EPERM;
1155 goto err_put;
1158 key = __bpf_copy_key(ukey, map->key_size);
1159 if (IS_ERR(key)) {
1160 err = PTR_ERR(key);
1161 goto err_put;
1164 if (bpf_map_is_dev_bound(map)) {
1165 err = bpf_map_offload_delete_elem(map, key);
1166 goto out;
1167 } else if (IS_FD_PROG_ARRAY(map) ||
1168 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1169 /* These maps require sleepable context */
1170 err = map->ops->map_delete_elem(map, key);
1171 goto out;
1174 bpf_disable_instrumentation();
1175 rcu_read_lock();
1176 err = map->ops->map_delete_elem(map, key);
1177 rcu_read_unlock();
1178 bpf_enable_instrumentation();
1179 maybe_wait_bpf_programs(map);
1180 out:
1181 kfree(key);
1182 err_put:
1183 fdput(f);
1184 return err;
1187 /* last field in 'union bpf_attr' used by this command */
1188 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1190 static int map_get_next_key(union bpf_attr *attr)
1192 void __user *ukey = u64_to_user_ptr(attr->key);
1193 void __user *unext_key = u64_to_user_ptr(attr->next_key);
1194 int ufd = attr->map_fd;
1195 struct bpf_map *map;
1196 void *key, *next_key;
1197 struct fd f;
1198 int err;
1200 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1201 return -EINVAL;
1203 f = fdget(ufd);
1204 map = __bpf_map_get(f);
1205 if (IS_ERR(map))
1206 return PTR_ERR(map);
1207 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
1208 err = -EPERM;
1209 goto err_put;
1212 if (ukey) {
1213 key = __bpf_copy_key(ukey, map->key_size);
1214 if (IS_ERR(key)) {
1215 err = PTR_ERR(key);
1216 goto err_put;
1218 } else {
1219 key = NULL;
1222 err = -ENOMEM;
1223 next_key = kmalloc(map->key_size, GFP_USER);
1224 if (!next_key)
1225 goto free_key;
1227 if (bpf_map_is_dev_bound(map)) {
1228 err = bpf_map_offload_get_next_key(map, key, next_key);
1229 goto out;
1232 rcu_read_lock();
1233 err = map->ops->map_get_next_key(map, key, next_key);
1234 rcu_read_unlock();
1235 out:
1236 if (err)
1237 goto free_next_key;
1239 err = -EFAULT;
1240 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1241 goto free_next_key;
1243 err = 0;
1245 free_next_key:
1246 kfree(next_key);
1247 free_key:
1248 kfree(key);
1249 err_put:
1250 fdput(f);
1251 return err;
1254 int generic_map_delete_batch(struct bpf_map *map,
1255 const union bpf_attr *attr,
1256 union bpf_attr __user *uattr)
1258 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1259 u32 cp, max_count;
1260 int err = 0;
1261 void *key;
1263 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1264 return -EINVAL;
1266 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1267 !map_value_has_spin_lock(map)) {
1268 return -EINVAL;
1271 max_count = attr->batch.count;
1272 if (!max_count)
1273 return 0;
1275 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1276 if (!key)
1277 return -ENOMEM;
1279 for (cp = 0; cp < max_count; cp++) {
1280 err = -EFAULT;
1281 if (copy_from_user(key, keys + cp * map->key_size,
1282 map->key_size))
1283 break;
1285 if (bpf_map_is_dev_bound(map)) {
1286 err = bpf_map_offload_delete_elem(map, key);
1287 break;
1290 bpf_disable_instrumentation();
1291 rcu_read_lock();
1292 err = map->ops->map_delete_elem(map, key);
1293 rcu_read_unlock();
1294 bpf_enable_instrumentation();
1295 maybe_wait_bpf_programs(map);
1296 if (err)
1297 break;
1299 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1300 err = -EFAULT;
1302 kfree(key);
1303 return err;
1306 int generic_map_update_batch(struct bpf_map *map,
1307 const union bpf_attr *attr,
1308 union bpf_attr __user *uattr)
1310 void __user *values = u64_to_user_ptr(attr->batch.values);
1311 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1312 u32 value_size, cp, max_count;
1313 int ufd = attr->map_fd;
1314 void *key, *value;
1315 struct fd f;
1316 int err = 0;
1318 f = fdget(ufd);
1319 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1320 return -EINVAL;
1322 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1323 !map_value_has_spin_lock(map)) {
1324 return -EINVAL;
1327 value_size = bpf_map_value_size(map);
1329 max_count = attr->batch.count;
1330 if (!max_count)
1331 return 0;
1333 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1334 if (!key)
1335 return -ENOMEM;
1337 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1338 if (!value) {
1339 kfree(key);
1340 return -ENOMEM;
1343 for (cp = 0; cp < max_count; cp++) {
1344 err = -EFAULT;
1345 if (copy_from_user(key, keys + cp * map->key_size,
1346 map->key_size) ||
1347 copy_from_user(value, values + cp * value_size, value_size))
1348 break;
1350 err = bpf_map_update_value(map, f, key, value,
1351 attr->batch.elem_flags);
1353 if (err)
1354 break;
1357 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1358 err = -EFAULT;
1360 kfree(value);
1361 kfree(key);
1362 return err;
1365 #define MAP_LOOKUP_RETRIES 3
1367 int generic_map_lookup_batch(struct bpf_map *map,
1368 const union bpf_attr *attr,
1369 union bpf_attr __user *uattr)
1371 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1372 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1373 void __user *values = u64_to_user_ptr(attr->batch.values);
1374 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1375 void *buf, *buf_prevkey, *prev_key, *key, *value;
1376 int err, retry = MAP_LOOKUP_RETRIES;
1377 u32 value_size, cp, max_count;
1379 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1380 return -EINVAL;
1382 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1383 !map_value_has_spin_lock(map))
1384 return -EINVAL;
1386 value_size = bpf_map_value_size(map);
1388 max_count = attr->batch.count;
1389 if (!max_count)
1390 return 0;
1392 if (put_user(0, &uattr->batch.count))
1393 return -EFAULT;
1395 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1396 if (!buf_prevkey)
1397 return -ENOMEM;
1399 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1400 if (!buf) {
1401 kfree(buf_prevkey);
1402 return -ENOMEM;
1405 err = -EFAULT;
1406 prev_key = NULL;
1407 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1408 goto free_buf;
1409 key = buf;
1410 value = key + map->key_size;
1411 if (ubatch)
1412 prev_key = buf_prevkey;
1414 for (cp = 0; cp < max_count;) {
1415 rcu_read_lock();
1416 err = map->ops->map_get_next_key(map, prev_key, key);
1417 rcu_read_unlock();
1418 if (err)
1419 break;
1420 err = bpf_map_copy_value(map, key, value,
1421 attr->batch.elem_flags);
1423 if (err == -ENOENT) {
1424 if (retry) {
1425 retry--;
1426 continue;
1428 err = -EINTR;
1429 break;
1432 if (err)
1433 goto free_buf;
1435 if (copy_to_user(keys + cp * map->key_size, key,
1436 map->key_size)) {
1437 err = -EFAULT;
1438 goto free_buf;
1440 if (copy_to_user(values + cp * value_size, value, value_size)) {
1441 err = -EFAULT;
1442 goto free_buf;
1445 if (!prev_key)
1446 prev_key = buf_prevkey;
1448 swap(prev_key, key);
1449 retry = MAP_LOOKUP_RETRIES;
1450 cp++;
1453 if (err == -EFAULT)
1454 goto free_buf;
1456 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1457 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1458 err = -EFAULT;
1460 free_buf:
1461 kfree(buf_prevkey);
1462 kfree(buf);
1463 return err;
1466 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1468 static int map_lookup_and_delete_elem(union bpf_attr *attr)
1470 void __user *ukey = u64_to_user_ptr(attr->key);
1471 void __user *uvalue = u64_to_user_ptr(attr->value);
1472 int ufd = attr->map_fd;
1473 struct bpf_map *map;
1474 void *key, *value;
1475 u32 value_size;
1476 struct fd f;
1477 int err;
1479 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1480 return -EINVAL;
1482 f = fdget(ufd);
1483 map = __bpf_map_get(f);
1484 if (IS_ERR(map))
1485 return PTR_ERR(map);
1486 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1487 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1488 err = -EPERM;
1489 goto err_put;
1492 key = __bpf_copy_key(ukey, map->key_size);
1493 if (IS_ERR(key)) {
1494 err = PTR_ERR(key);
1495 goto err_put;
1498 value_size = map->value_size;
1500 err = -ENOMEM;
1501 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1502 if (!value)
1503 goto free_key;
1505 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1506 map->map_type == BPF_MAP_TYPE_STACK) {
1507 err = map->ops->map_pop_elem(map, value);
1508 } else {
1509 err = -ENOTSUPP;
1512 if (err)
1513 goto free_value;
1515 if (copy_to_user(uvalue, value, value_size) != 0) {
1516 err = -EFAULT;
1517 goto free_value;
1520 err = 0;
1522 free_value:
1523 kfree(value);
1524 free_key:
1525 kfree(key);
1526 err_put:
1527 fdput(f);
1528 return err;
1531 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
1533 static int map_freeze(const union bpf_attr *attr)
1535 int err = 0, ufd = attr->map_fd;
1536 struct bpf_map *map;
1537 struct fd f;
1539 if (CHECK_ATTR(BPF_MAP_FREEZE))
1540 return -EINVAL;
1542 f = fdget(ufd);
1543 map = __bpf_map_get(f);
1544 if (IS_ERR(map))
1545 return PTR_ERR(map);
1547 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1548 fdput(f);
1549 return -ENOTSUPP;
1552 mutex_lock(&map->freeze_mutex);
1554 if (map->writecnt) {
1555 err = -EBUSY;
1556 goto err_put;
1558 if (READ_ONCE(map->frozen)) {
1559 err = -EBUSY;
1560 goto err_put;
1562 if (!bpf_capable()) {
1563 err = -EPERM;
1564 goto err_put;
1567 WRITE_ONCE(map->frozen, true);
1568 err_put:
1569 mutex_unlock(&map->freeze_mutex);
1570 fdput(f);
1571 return err;
1574 static const struct bpf_prog_ops * const bpf_prog_types[] = {
1575 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
1576 [_id] = & _name ## _prog_ops,
1577 #define BPF_MAP_TYPE(_id, _ops)
1578 #define BPF_LINK_TYPE(_id, _name)
1579 #include <linux/bpf_types.h>
1580 #undef BPF_PROG_TYPE
1581 #undef BPF_MAP_TYPE
1582 #undef BPF_LINK_TYPE
1585 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1587 const struct bpf_prog_ops *ops;
1589 if (type >= ARRAY_SIZE(bpf_prog_types))
1590 return -EINVAL;
1591 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1592 ops = bpf_prog_types[type];
1593 if (!ops)
1594 return -EINVAL;
1596 if (!bpf_prog_is_dev_bound(prog->aux))
1597 prog->aux->ops = ops;
1598 else
1599 prog->aux->ops = &bpf_offload_prog_ops;
1600 prog->type = type;
1601 return 0;
1604 enum bpf_audit {
1605 BPF_AUDIT_LOAD,
1606 BPF_AUDIT_UNLOAD,
1607 BPF_AUDIT_MAX,
1610 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1611 [BPF_AUDIT_LOAD] = "LOAD",
1612 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1615 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1617 struct audit_context *ctx = NULL;
1618 struct audit_buffer *ab;
1620 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1621 return;
1622 if (audit_enabled == AUDIT_OFF)
1623 return;
1624 if (op == BPF_AUDIT_LOAD)
1625 ctx = audit_context();
1626 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1627 if (unlikely(!ab))
1628 return;
1629 audit_log_format(ab, "prog-id=%u op=%s",
1630 prog->aux->id, bpf_audit_str[op]);
1631 audit_log_end(ab);
1634 static int bpf_prog_alloc_id(struct bpf_prog *prog)
1636 int id;
1638 idr_preload(GFP_KERNEL);
1639 spin_lock_bh(&prog_idr_lock);
1640 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1641 if (id > 0)
1642 prog->aux->id = id;
1643 spin_unlock_bh(&prog_idr_lock);
1644 idr_preload_end();
1646 /* id is in [1, INT_MAX) */
1647 if (WARN_ON_ONCE(!id))
1648 return -ENOSPC;
1650 return id > 0 ? 0 : id;
1653 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1655 /* cBPF to eBPF migrations are currently not in the idr store.
1656 * Offloaded programs are removed from the store when their device
1657 * disappears - even if someone grabs an fd to them they are unusable,
1658 * simply waiting for refcnt to drop to be freed.
1660 if (!prog->aux->id)
1661 return;
1663 if (do_idr_lock)
1664 spin_lock_bh(&prog_idr_lock);
1665 else
1666 __acquire(&prog_idr_lock);
1668 idr_remove(&prog_idr, prog->aux->id);
1669 prog->aux->id = 0;
1671 if (do_idr_lock)
1672 spin_unlock_bh(&prog_idr_lock);
1673 else
1674 __release(&prog_idr_lock);
1677 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1679 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1681 kvfree(aux->func_info);
1682 kfree(aux->func_info_aux);
1683 free_uid(aux->user);
1684 security_bpf_prog_free(aux);
1685 bpf_prog_free(aux->prog);
1688 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1690 bpf_prog_kallsyms_del_all(prog);
1691 btf_put(prog->aux->btf);
1692 bpf_prog_free_linfo(prog);
1693 if (prog->aux->attach_btf)
1694 btf_put(prog->aux->attach_btf);
1696 if (deferred) {
1697 if (prog->aux->sleepable)
1698 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1699 else
1700 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1701 } else {
1702 __bpf_prog_put_rcu(&prog->aux->rcu);
1706 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1708 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
1709 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1710 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1711 /* bpf_prog_free_id() must be called first */
1712 bpf_prog_free_id(prog, do_idr_lock);
1713 __bpf_prog_put_noref(prog, true);
1717 void bpf_prog_put(struct bpf_prog *prog)
1719 __bpf_prog_put(prog, true);
1721 EXPORT_SYMBOL_GPL(bpf_prog_put);
1723 static int bpf_prog_release(struct inode *inode, struct file *filp)
1725 struct bpf_prog *prog = filp->private_data;
1727 bpf_prog_put(prog);
1728 return 0;
1731 static void bpf_prog_get_stats(const struct bpf_prog *prog,
1732 struct bpf_prog_stats *stats)
1734 u64 nsecs = 0, cnt = 0;
1735 int cpu;
1737 for_each_possible_cpu(cpu) {
1738 const struct bpf_prog_stats *st;
1739 unsigned int start;
1740 u64 tnsecs, tcnt;
1742 st = per_cpu_ptr(prog->aux->stats, cpu);
1743 do {
1744 start = u64_stats_fetch_begin_irq(&st->syncp);
1745 tnsecs = st->nsecs;
1746 tcnt = st->cnt;
1747 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1748 nsecs += tnsecs;
1749 cnt += tcnt;
1751 stats->nsecs = nsecs;
1752 stats->cnt = cnt;
1755 #ifdef CONFIG_PROC_FS
1756 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1758 const struct bpf_prog *prog = filp->private_data;
1759 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1760 struct bpf_prog_stats stats;
1762 bpf_prog_get_stats(prog, &stats);
1763 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1764 seq_printf(m,
1765 "prog_type:\t%u\n"
1766 "prog_jited:\t%u\n"
1767 "prog_tag:\t%s\n"
1768 "memlock:\t%llu\n"
1769 "prog_id:\t%u\n"
1770 "run_time_ns:\t%llu\n"
1771 "run_cnt:\t%llu\n",
1772 prog->type,
1773 prog->jited,
1774 prog_tag,
1775 prog->pages * 1ULL << PAGE_SHIFT,
1776 prog->aux->id,
1777 stats.nsecs,
1778 stats.cnt);
1780 #endif
1782 const struct file_operations bpf_prog_fops = {
1783 #ifdef CONFIG_PROC_FS
1784 .show_fdinfo = bpf_prog_show_fdinfo,
1785 #endif
1786 .release = bpf_prog_release,
1787 .read = bpf_dummy_read,
1788 .write = bpf_dummy_write,
1791 int bpf_prog_new_fd(struct bpf_prog *prog)
1793 int ret;
1795 ret = security_bpf_prog(prog);
1796 if (ret < 0)
1797 return ret;
1799 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1800 O_RDWR | O_CLOEXEC);
1803 static struct bpf_prog *____bpf_prog_get(struct fd f)
1805 if (!f.file)
1806 return ERR_PTR(-EBADF);
1807 if (f.file->f_op != &bpf_prog_fops) {
1808 fdput(f);
1809 return ERR_PTR(-EINVAL);
1812 return f.file->private_data;
1815 void bpf_prog_add(struct bpf_prog *prog, int i)
1817 atomic64_add(i, &prog->aux->refcnt);
1819 EXPORT_SYMBOL_GPL(bpf_prog_add);
1821 void bpf_prog_sub(struct bpf_prog *prog, int i)
1823 /* Only to be used for undoing previous bpf_prog_add() in some
1824 * error path. We still know that another entity in our call
1825 * path holds a reference to the program, thus atomic_sub() can
1826 * be safely used in such cases!
1828 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1830 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1832 void bpf_prog_inc(struct bpf_prog *prog)
1834 atomic64_inc(&prog->aux->refcnt);
1836 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1838 /* prog_idr_lock should have been held */
1839 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1841 int refold;
1843 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1845 if (!refold)
1846 return ERR_PTR(-ENOENT);
1848 return prog;
1850 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1852 bool bpf_prog_get_ok(struct bpf_prog *prog,
1853 enum bpf_prog_type *attach_type, bool attach_drv)
1855 /* not an attachment, just a refcount inc, always allow */
1856 if (!attach_type)
1857 return true;
1859 if (prog->type != *attach_type)
1860 return false;
1861 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1862 return false;
1864 return true;
1867 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1868 bool attach_drv)
1870 struct fd f = fdget(ufd);
1871 struct bpf_prog *prog;
1873 prog = ____bpf_prog_get(f);
1874 if (IS_ERR(prog))
1875 return prog;
1876 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1877 prog = ERR_PTR(-EINVAL);
1878 goto out;
1881 bpf_prog_inc(prog);
1882 out:
1883 fdput(f);
1884 return prog;
1887 struct bpf_prog *bpf_prog_get(u32 ufd)
1889 return __bpf_prog_get(ufd, NULL, false);
1892 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1893 bool attach_drv)
1895 return __bpf_prog_get(ufd, &type, attach_drv);
1897 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1899 /* Initially all BPF programs could be loaded w/o specifying
1900 * expected_attach_type. Later for some of them specifying expected_attach_type
1901 * at load time became required so that program could be validated properly.
1902 * Programs of types that are allowed to be loaded both w/ and w/o (for
1903 * backward compatibility) expected_attach_type, should have the default attach
1904 * type assigned to expected_attach_type for the latter case, so that it can be
1905 * validated later at attach time.
1907 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1908 * prog type requires it but has some attach types that have to be backward
1909 * compatible.
1911 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1913 switch (attr->prog_type) {
1914 case BPF_PROG_TYPE_CGROUP_SOCK:
1915 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1916 * exist so checking for non-zero is the way to go here.
1918 if (!attr->expected_attach_type)
1919 attr->expected_attach_type =
1920 BPF_CGROUP_INET_SOCK_CREATE;
1921 break;
1925 static int
1926 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1927 enum bpf_attach_type expected_attach_type,
1928 struct btf *attach_btf, u32 btf_id,
1929 struct bpf_prog *dst_prog)
1931 if (btf_id) {
1932 if (btf_id > BTF_MAX_TYPE)
1933 return -EINVAL;
1935 if (!attach_btf && !dst_prog)
1936 return -EINVAL;
1938 switch (prog_type) {
1939 case BPF_PROG_TYPE_TRACING:
1940 case BPF_PROG_TYPE_LSM:
1941 case BPF_PROG_TYPE_STRUCT_OPS:
1942 case BPF_PROG_TYPE_EXT:
1943 break;
1944 default:
1945 return -EINVAL;
1949 if (attach_btf && (!btf_id || dst_prog))
1950 return -EINVAL;
1952 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
1953 prog_type != BPF_PROG_TYPE_EXT)
1954 return -EINVAL;
1956 switch (prog_type) {
1957 case BPF_PROG_TYPE_CGROUP_SOCK:
1958 switch (expected_attach_type) {
1959 case BPF_CGROUP_INET_SOCK_CREATE:
1960 case BPF_CGROUP_INET_SOCK_RELEASE:
1961 case BPF_CGROUP_INET4_POST_BIND:
1962 case BPF_CGROUP_INET6_POST_BIND:
1963 return 0;
1964 default:
1965 return -EINVAL;
1967 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1968 switch (expected_attach_type) {
1969 case BPF_CGROUP_INET4_BIND:
1970 case BPF_CGROUP_INET6_BIND:
1971 case BPF_CGROUP_INET4_CONNECT:
1972 case BPF_CGROUP_INET6_CONNECT:
1973 case BPF_CGROUP_INET4_GETPEERNAME:
1974 case BPF_CGROUP_INET6_GETPEERNAME:
1975 case BPF_CGROUP_INET4_GETSOCKNAME:
1976 case BPF_CGROUP_INET6_GETSOCKNAME:
1977 case BPF_CGROUP_UDP4_SENDMSG:
1978 case BPF_CGROUP_UDP6_SENDMSG:
1979 case BPF_CGROUP_UDP4_RECVMSG:
1980 case BPF_CGROUP_UDP6_RECVMSG:
1981 return 0;
1982 default:
1983 return -EINVAL;
1985 case BPF_PROG_TYPE_CGROUP_SKB:
1986 switch (expected_attach_type) {
1987 case BPF_CGROUP_INET_INGRESS:
1988 case BPF_CGROUP_INET_EGRESS:
1989 return 0;
1990 default:
1991 return -EINVAL;
1993 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1994 switch (expected_attach_type) {
1995 case BPF_CGROUP_SETSOCKOPT:
1996 case BPF_CGROUP_GETSOCKOPT:
1997 return 0;
1998 default:
1999 return -EINVAL;
2001 case BPF_PROG_TYPE_SK_LOOKUP:
2002 if (expected_attach_type == BPF_SK_LOOKUP)
2003 return 0;
2004 return -EINVAL;
2005 case BPF_PROG_TYPE_EXT:
2006 if (expected_attach_type)
2007 return -EINVAL;
2008 fallthrough;
2009 default:
2010 return 0;
2014 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2016 switch (prog_type) {
2017 case BPF_PROG_TYPE_SCHED_CLS:
2018 case BPF_PROG_TYPE_SCHED_ACT:
2019 case BPF_PROG_TYPE_XDP:
2020 case BPF_PROG_TYPE_LWT_IN:
2021 case BPF_PROG_TYPE_LWT_OUT:
2022 case BPF_PROG_TYPE_LWT_XMIT:
2023 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2024 case BPF_PROG_TYPE_SK_SKB:
2025 case BPF_PROG_TYPE_SK_MSG:
2026 case BPF_PROG_TYPE_LIRC_MODE2:
2027 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2028 case BPF_PROG_TYPE_CGROUP_DEVICE:
2029 case BPF_PROG_TYPE_CGROUP_SOCK:
2030 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2031 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2032 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2033 case BPF_PROG_TYPE_SOCK_OPS:
2034 case BPF_PROG_TYPE_EXT: /* extends any prog */
2035 return true;
2036 case BPF_PROG_TYPE_CGROUP_SKB:
2037 /* always unpriv */
2038 case BPF_PROG_TYPE_SK_REUSEPORT:
2039 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2040 default:
2041 return false;
2045 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2047 switch (prog_type) {
2048 case BPF_PROG_TYPE_KPROBE:
2049 case BPF_PROG_TYPE_TRACEPOINT:
2050 case BPF_PROG_TYPE_PERF_EVENT:
2051 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2052 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2053 case BPF_PROG_TYPE_TRACING:
2054 case BPF_PROG_TYPE_LSM:
2055 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2056 case BPF_PROG_TYPE_EXT: /* extends any prog */
2057 return true;
2058 default:
2059 return false;
2063 /* last field in 'union bpf_attr' used by this command */
2064 #define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
2066 static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
2068 enum bpf_prog_type type = attr->prog_type;
2069 struct bpf_prog *prog, *dst_prog = NULL;
2070 struct btf *attach_btf = NULL;
2071 int err;
2072 char license[128];
2073 bool is_gpl;
2075 if (CHECK_ATTR(BPF_PROG_LOAD))
2076 return -EINVAL;
2078 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2079 BPF_F_ANY_ALIGNMENT |
2080 BPF_F_TEST_STATE_FREQ |
2081 BPF_F_SLEEPABLE |
2082 BPF_F_TEST_RND_HI32))
2083 return -EINVAL;
2085 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2086 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2087 !bpf_capable())
2088 return -EPERM;
2090 /* copy eBPF program license from user space */
2091 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
2092 sizeof(license) - 1) < 0)
2093 return -EFAULT;
2094 license[sizeof(license) - 1] = 0;
2096 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2097 is_gpl = license_is_gpl_compatible(license);
2099 if (attr->insn_cnt == 0 ||
2100 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2101 return -E2BIG;
2102 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2103 type != BPF_PROG_TYPE_CGROUP_SKB &&
2104 !bpf_capable())
2105 return -EPERM;
2107 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2108 return -EPERM;
2109 if (is_perfmon_prog_type(type) && !perfmon_capable())
2110 return -EPERM;
2112 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2113 * or btf, we need to check which one it is
2115 if (attr->attach_prog_fd) {
2116 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2117 if (IS_ERR(dst_prog)) {
2118 dst_prog = NULL;
2119 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2120 if (IS_ERR(attach_btf))
2121 return -EINVAL;
2122 if (!btf_is_kernel(attach_btf)) {
2123 /* attaching through specifying bpf_prog's BTF
2124 * objects directly might be supported eventually
2126 btf_put(attach_btf);
2127 return -ENOTSUPP;
2130 } else if (attr->attach_btf_id) {
2131 /* fall back to vmlinux BTF, if BTF type ID is specified */
2132 attach_btf = bpf_get_btf_vmlinux();
2133 if (IS_ERR(attach_btf))
2134 return PTR_ERR(attach_btf);
2135 if (!attach_btf)
2136 return -EINVAL;
2137 btf_get(attach_btf);
2140 bpf_prog_load_fixup_attach_type(attr);
2141 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2142 attach_btf, attr->attach_btf_id,
2143 dst_prog)) {
2144 if (dst_prog)
2145 bpf_prog_put(dst_prog);
2146 if (attach_btf)
2147 btf_put(attach_btf);
2148 return -EINVAL;
2151 /* plain bpf_prog allocation */
2152 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2153 if (!prog) {
2154 if (dst_prog)
2155 bpf_prog_put(dst_prog);
2156 if (attach_btf)
2157 btf_put(attach_btf);
2158 return -ENOMEM;
2161 prog->expected_attach_type = attr->expected_attach_type;
2162 prog->aux->attach_btf = attach_btf;
2163 prog->aux->attach_btf_id = attr->attach_btf_id;
2164 prog->aux->dst_prog = dst_prog;
2165 prog->aux->offload_requested = !!attr->prog_ifindex;
2166 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
2168 err = security_bpf_prog_alloc(prog->aux);
2169 if (err)
2170 goto free_prog;
2172 prog->aux->user = get_current_user();
2173 prog->len = attr->insn_cnt;
2175 err = -EFAULT;
2176 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
2177 bpf_prog_insn_size(prog)) != 0)
2178 goto free_prog_sec;
2180 prog->orig_prog = NULL;
2181 prog->jited = 0;
2183 atomic64_set(&prog->aux->refcnt, 1);
2184 prog->gpl_compatible = is_gpl ? 1 : 0;
2186 if (bpf_prog_is_dev_bound(prog->aux)) {
2187 err = bpf_prog_offload_init(prog, attr);
2188 if (err)
2189 goto free_prog_sec;
2192 /* find program type: socket_filter vs tracing_filter */
2193 err = find_prog_type(type, prog);
2194 if (err < 0)
2195 goto free_prog_sec;
2197 prog->aux->load_time = ktime_get_boottime_ns();
2198 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2199 sizeof(attr->prog_name));
2200 if (err < 0)
2201 goto free_prog_sec;
2203 /* run eBPF verifier */
2204 err = bpf_check(&prog, attr, uattr);
2205 if (err < 0)
2206 goto free_used_maps;
2208 prog = bpf_prog_select_runtime(prog, &err);
2209 if (err < 0)
2210 goto free_used_maps;
2212 err = bpf_prog_alloc_id(prog);
2213 if (err)
2214 goto free_used_maps;
2216 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2217 * effectively publicly exposed. However, retrieving via
2218 * bpf_prog_get_fd_by_id() will take another reference,
2219 * therefore it cannot be gone underneath us.
2221 * Only for the time /after/ successful bpf_prog_new_fd()
2222 * and before returning to userspace, we might just hold
2223 * one reference and any parallel close on that fd could
2224 * rip everything out. Hence, below notifications must
2225 * happen before bpf_prog_new_fd().
2227 * Also, any failure handling from this point onwards must
2228 * be using bpf_prog_put() given the program is exposed.
2230 bpf_prog_kallsyms_add(prog);
2231 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2232 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2234 err = bpf_prog_new_fd(prog);
2235 if (err < 0)
2236 bpf_prog_put(prog);
2237 return err;
2239 free_used_maps:
2240 /* In case we have subprogs, we need to wait for a grace
2241 * period before we can tear down JIT memory since symbols
2242 * are already exposed under kallsyms.
2244 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2245 return err;
2246 free_prog_sec:
2247 free_uid(prog->aux->user);
2248 security_bpf_prog_free(prog->aux);
2249 free_prog:
2250 if (prog->aux->attach_btf)
2251 btf_put(prog->aux->attach_btf);
2252 bpf_prog_free(prog);
2253 return err;
2256 #define BPF_OBJ_LAST_FIELD file_flags
2258 static int bpf_obj_pin(const union bpf_attr *attr)
2260 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2261 return -EINVAL;
2263 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2266 static int bpf_obj_get(const union bpf_attr *attr)
2268 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2269 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2270 return -EINVAL;
2272 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2273 attr->file_flags);
2276 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2277 const struct bpf_link_ops *ops, struct bpf_prog *prog)
2279 atomic64_set(&link->refcnt, 1);
2280 link->type = type;
2281 link->id = 0;
2282 link->ops = ops;
2283 link->prog = prog;
2286 static void bpf_link_free_id(int id)
2288 if (!id)
2289 return;
2291 spin_lock_bh(&link_idr_lock);
2292 idr_remove(&link_idr, id);
2293 spin_unlock_bh(&link_idr_lock);
2296 /* Clean up bpf_link and corresponding anon_inode file and FD. After
2297 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2298 * anon_inode's release() call. This helper marksbpf_link as
2299 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2300 * is not decremented, it's the responsibility of a calling code that failed
2301 * to complete bpf_link initialization.
2303 void bpf_link_cleanup(struct bpf_link_primer *primer)
2305 primer->link->prog = NULL;
2306 bpf_link_free_id(primer->id);
2307 fput(primer->file);
2308 put_unused_fd(primer->fd);
2311 void bpf_link_inc(struct bpf_link *link)
2313 atomic64_inc(&link->refcnt);
2316 /* bpf_link_free is guaranteed to be called from process context */
2317 static void bpf_link_free(struct bpf_link *link)
2319 bpf_link_free_id(link->id);
2320 if (link->prog) {
2321 /* detach BPF program, clean up used resources */
2322 link->ops->release(link);
2323 bpf_prog_put(link->prog);
2325 /* free bpf_link and its containing memory */
2326 link->ops->dealloc(link);
2329 static void bpf_link_put_deferred(struct work_struct *work)
2331 struct bpf_link *link = container_of(work, struct bpf_link, work);
2333 bpf_link_free(link);
2336 /* bpf_link_put can be called from atomic context, but ensures that resources
2337 * are freed from process context
2339 void bpf_link_put(struct bpf_link *link)
2341 if (!atomic64_dec_and_test(&link->refcnt))
2342 return;
2344 if (in_atomic()) {
2345 INIT_WORK(&link->work, bpf_link_put_deferred);
2346 schedule_work(&link->work);
2347 } else {
2348 bpf_link_free(link);
2352 static int bpf_link_release(struct inode *inode, struct file *filp)
2354 struct bpf_link *link = filp->private_data;
2356 bpf_link_put(link);
2357 return 0;
2360 #ifdef CONFIG_PROC_FS
2361 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2362 #define BPF_MAP_TYPE(_id, _ops)
2363 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2364 static const char *bpf_link_type_strs[] = {
2365 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2366 #include <linux/bpf_types.h>
2368 #undef BPF_PROG_TYPE
2369 #undef BPF_MAP_TYPE
2370 #undef BPF_LINK_TYPE
2372 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2374 const struct bpf_link *link = filp->private_data;
2375 const struct bpf_prog *prog = link->prog;
2376 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2378 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2379 seq_printf(m,
2380 "link_type:\t%s\n"
2381 "link_id:\t%u\n"
2382 "prog_tag:\t%s\n"
2383 "prog_id:\t%u\n",
2384 bpf_link_type_strs[link->type],
2385 link->id,
2386 prog_tag,
2387 prog->aux->id);
2388 if (link->ops->show_fdinfo)
2389 link->ops->show_fdinfo(link, m);
2391 #endif
2393 static const struct file_operations bpf_link_fops = {
2394 #ifdef CONFIG_PROC_FS
2395 .show_fdinfo = bpf_link_show_fdinfo,
2396 #endif
2397 .release = bpf_link_release,
2398 .read = bpf_dummy_read,
2399 .write = bpf_dummy_write,
2402 static int bpf_link_alloc_id(struct bpf_link *link)
2404 int id;
2406 idr_preload(GFP_KERNEL);
2407 spin_lock_bh(&link_idr_lock);
2408 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2409 spin_unlock_bh(&link_idr_lock);
2410 idr_preload_end();
2412 return id;
2415 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2416 * reserving unused FD and allocating ID from link_idr. This is to be paired
2417 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2418 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2419 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2420 * transient state is passed around in struct bpf_link_primer.
2421 * This is preferred way to create and initialize bpf_link, especially when
2422 * there are complicated and expensive operations inbetween creating bpf_link
2423 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2424 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2425 * expensive (and potentially failing) roll back operations in a rare case
2426 * that file, FD, or ID can't be allocated.
2428 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2430 struct file *file;
2431 int fd, id;
2433 fd = get_unused_fd_flags(O_CLOEXEC);
2434 if (fd < 0)
2435 return fd;
2438 id = bpf_link_alloc_id(link);
2439 if (id < 0) {
2440 put_unused_fd(fd);
2441 return id;
2444 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2445 if (IS_ERR(file)) {
2446 bpf_link_free_id(id);
2447 put_unused_fd(fd);
2448 return PTR_ERR(file);
2451 primer->link = link;
2452 primer->file = file;
2453 primer->fd = fd;
2454 primer->id = id;
2455 return 0;
2458 int bpf_link_settle(struct bpf_link_primer *primer)
2460 /* make bpf_link fetchable by ID */
2461 spin_lock_bh(&link_idr_lock);
2462 primer->link->id = primer->id;
2463 spin_unlock_bh(&link_idr_lock);
2464 /* make bpf_link fetchable by FD */
2465 fd_install(primer->fd, primer->file);
2466 /* pass through installed FD */
2467 return primer->fd;
2470 int bpf_link_new_fd(struct bpf_link *link)
2472 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2475 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2477 struct fd f = fdget(ufd);
2478 struct bpf_link *link;
2480 if (!f.file)
2481 return ERR_PTR(-EBADF);
2482 if (f.file->f_op != &bpf_link_fops) {
2483 fdput(f);
2484 return ERR_PTR(-EINVAL);
2487 link = f.file->private_data;
2488 bpf_link_inc(link);
2489 fdput(f);
2491 return link;
2494 struct bpf_tracing_link {
2495 struct bpf_link link;
2496 enum bpf_attach_type attach_type;
2497 struct bpf_trampoline *trampoline;
2498 struct bpf_prog *tgt_prog;
2501 static void bpf_tracing_link_release(struct bpf_link *link)
2503 struct bpf_tracing_link *tr_link =
2504 container_of(link, struct bpf_tracing_link, link);
2506 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2507 tr_link->trampoline));
2509 bpf_trampoline_put(tr_link->trampoline);
2511 /* tgt_prog is NULL if target is a kernel function */
2512 if (tr_link->tgt_prog)
2513 bpf_prog_put(tr_link->tgt_prog);
2516 static void bpf_tracing_link_dealloc(struct bpf_link *link)
2518 struct bpf_tracing_link *tr_link =
2519 container_of(link, struct bpf_tracing_link, link);
2521 kfree(tr_link);
2524 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2525 struct seq_file *seq)
2527 struct bpf_tracing_link *tr_link =
2528 container_of(link, struct bpf_tracing_link, link);
2530 seq_printf(seq,
2531 "attach_type:\t%d\n",
2532 tr_link->attach_type);
2535 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2536 struct bpf_link_info *info)
2538 struct bpf_tracing_link *tr_link =
2539 container_of(link, struct bpf_tracing_link, link);
2541 info->tracing.attach_type = tr_link->attach_type;
2543 return 0;
2546 static const struct bpf_link_ops bpf_tracing_link_lops = {
2547 .release = bpf_tracing_link_release,
2548 .dealloc = bpf_tracing_link_dealloc,
2549 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2550 .fill_link_info = bpf_tracing_link_fill_link_info,
2553 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2554 int tgt_prog_fd,
2555 u32 btf_id)
2557 struct bpf_link_primer link_primer;
2558 struct bpf_prog *tgt_prog = NULL;
2559 struct bpf_trampoline *tr = NULL;
2560 struct bpf_tracing_link *link;
2561 u64 key = 0;
2562 int err;
2564 switch (prog->type) {
2565 case BPF_PROG_TYPE_TRACING:
2566 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2567 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2568 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2569 err = -EINVAL;
2570 goto out_put_prog;
2572 break;
2573 case BPF_PROG_TYPE_EXT:
2574 if (prog->expected_attach_type != 0) {
2575 err = -EINVAL;
2576 goto out_put_prog;
2578 break;
2579 case BPF_PROG_TYPE_LSM:
2580 if (prog->expected_attach_type != BPF_LSM_MAC) {
2581 err = -EINVAL;
2582 goto out_put_prog;
2584 break;
2585 default:
2586 err = -EINVAL;
2587 goto out_put_prog;
2590 if (!!tgt_prog_fd != !!btf_id) {
2591 err = -EINVAL;
2592 goto out_put_prog;
2595 if (tgt_prog_fd) {
2596 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2597 if (prog->type != BPF_PROG_TYPE_EXT) {
2598 err = -EINVAL;
2599 goto out_put_prog;
2602 tgt_prog = bpf_prog_get(tgt_prog_fd);
2603 if (IS_ERR(tgt_prog)) {
2604 err = PTR_ERR(tgt_prog);
2605 tgt_prog = NULL;
2606 goto out_put_prog;
2609 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
2612 link = kzalloc(sizeof(*link), GFP_USER);
2613 if (!link) {
2614 err = -ENOMEM;
2615 goto out_put_prog;
2617 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2618 &bpf_tracing_link_lops, prog);
2619 link->attach_type = prog->expected_attach_type;
2621 mutex_lock(&prog->aux->dst_mutex);
2623 /* There are a few possible cases here:
2625 * - if prog->aux->dst_trampoline is set, the program was just loaded
2626 * and not yet attached to anything, so we can use the values stored
2627 * in prog->aux
2629 * - if prog->aux->dst_trampoline is NULL, the program has already been
2630 * attached to a target and its initial target was cleared (below)
2632 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2633 * target_btf_id using the link_create API.
2635 * - if tgt_prog == NULL when this function was called using the old
2636 * raw_tracepoint_open API, and we need a target from prog->aux
2638 * The combination of no saved target in prog->aux, and no target
2639 * specified on load is illegal, and we reject that here.
2641 if (!prog->aux->dst_trampoline && !tgt_prog) {
2642 err = -ENOENT;
2643 goto out_unlock;
2646 if (!prog->aux->dst_trampoline ||
2647 (key && key != prog->aux->dst_trampoline->key)) {
2648 /* If there is no saved target, or the specified target is
2649 * different from the destination specified at load time, we
2650 * need a new trampoline and a check for compatibility
2652 struct bpf_attach_target_info tgt_info = {};
2654 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2655 &tgt_info);
2656 if (err)
2657 goto out_unlock;
2659 tr = bpf_trampoline_get(key, &tgt_info);
2660 if (!tr) {
2661 err = -ENOMEM;
2662 goto out_unlock;
2664 } else {
2665 /* The caller didn't specify a target, or the target was the
2666 * same as the destination supplied during program load. This
2667 * means we can reuse the trampoline and reference from program
2668 * load time, and there is no need to allocate a new one. This
2669 * can only happen once for any program, as the saved values in
2670 * prog->aux are cleared below.
2672 tr = prog->aux->dst_trampoline;
2673 tgt_prog = prog->aux->dst_prog;
2676 err = bpf_link_prime(&link->link, &link_primer);
2677 if (err)
2678 goto out_unlock;
2680 err = bpf_trampoline_link_prog(prog, tr);
2681 if (err) {
2682 bpf_link_cleanup(&link_primer);
2683 link = NULL;
2684 goto out_unlock;
2687 link->tgt_prog = tgt_prog;
2688 link->trampoline = tr;
2690 /* Always clear the trampoline and target prog from prog->aux to make
2691 * sure the original attach destination is not kept alive after a
2692 * program is (re-)attached to another target.
2694 if (prog->aux->dst_prog &&
2695 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2696 /* got extra prog ref from syscall, or attaching to different prog */
2697 bpf_prog_put(prog->aux->dst_prog);
2698 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2699 /* we allocated a new trampoline, so free the old one */
2700 bpf_trampoline_put(prog->aux->dst_trampoline);
2702 prog->aux->dst_prog = NULL;
2703 prog->aux->dst_trampoline = NULL;
2704 mutex_unlock(&prog->aux->dst_mutex);
2706 return bpf_link_settle(&link_primer);
2707 out_unlock:
2708 if (tr && tr != prog->aux->dst_trampoline)
2709 bpf_trampoline_put(tr);
2710 mutex_unlock(&prog->aux->dst_mutex);
2711 kfree(link);
2712 out_put_prog:
2713 if (tgt_prog_fd && tgt_prog)
2714 bpf_prog_put(tgt_prog);
2715 bpf_prog_put(prog);
2716 return err;
2719 struct bpf_raw_tp_link {
2720 struct bpf_link link;
2721 struct bpf_raw_event_map *btp;
2724 static void bpf_raw_tp_link_release(struct bpf_link *link)
2726 struct bpf_raw_tp_link *raw_tp =
2727 container_of(link, struct bpf_raw_tp_link, link);
2729 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2730 bpf_put_raw_tracepoint(raw_tp->btp);
2733 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2735 struct bpf_raw_tp_link *raw_tp =
2736 container_of(link, struct bpf_raw_tp_link, link);
2738 kfree(raw_tp);
2741 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2742 struct seq_file *seq)
2744 struct bpf_raw_tp_link *raw_tp_link =
2745 container_of(link, struct bpf_raw_tp_link, link);
2747 seq_printf(seq,
2748 "tp_name:\t%s\n",
2749 raw_tp_link->btp->tp->name);
2752 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2753 struct bpf_link_info *info)
2755 struct bpf_raw_tp_link *raw_tp_link =
2756 container_of(link, struct bpf_raw_tp_link, link);
2757 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2758 const char *tp_name = raw_tp_link->btp->tp->name;
2759 u32 ulen = info->raw_tracepoint.tp_name_len;
2760 size_t tp_len = strlen(tp_name);
2762 if (!ulen ^ !ubuf)
2763 return -EINVAL;
2765 info->raw_tracepoint.tp_name_len = tp_len + 1;
2767 if (!ubuf)
2768 return 0;
2770 if (ulen >= tp_len + 1) {
2771 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2772 return -EFAULT;
2773 } else {
2774 char zero = '\0';
2776 if (copy_to_user(ubuf, tp_name, ulen - 1))
2777 return -EFAULT;
2778 if (put_user(zero, ubuf + ulen - 1))
2779 return -EFAULT;
2780 return -ENOSPC;
2783 return 0;
2786 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
2787 .release = bpf_raw_tp_link_release,
2788 .dealloc = bpf_raw_tp_link_dealloc,
2789 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2790 .fill_link_info = bpf_raw_tp_link_fill_link_info,
2793 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2795 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2797 struct bpf_link_primer link_primer;
2798 struct bpf_raw_tp_link *link;
2799 struct bpf_raw_event_map *btp;
2800 struct bpf_prog *prog;
2801 const char *tp_name;
2802 char buf[128];
2803 int err;
2805 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2806 return -EINVAL;
2808 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2809 if (IS_ERR(prog))
2810 return PTR_ERR(prog);
2812 switch (prog->type) {
2813 case BPF_PROG_TYPE_TRACING:
2814 case BPF_PROG_TYPE_EXT:
2815 case BPF_PROG_TYPE_LSM:
2816 if (attr->raw_tracepoint.name) {
2817 /* The attach point for this category of programs
2818 * should be specified via btf_id during program load.
2820 err = -EINVAL;
2821 goto out_put_prog;
2823 if (prog->type == BPF_PROG_TYPE_TRACING &&
2824 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
2825 tp_name = prog->aux->attach_func_name;
2826 break;
2828 return bpf_tracing_prog_attach(prog, 0, 0);
2829 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2830 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2831 if (strncpy_from_user(buf,
2832 u64_to_user_ptr(attr->raw_tracepoint.name),
2833 sizeof(buf) - 1) < 0) {
2834 err = -EFAULT;
2835 goto out_put_prog;
2837 buf[sizeof(buf) - 1] = 0;
2838 tp_name = buf;
2839 break;
2840 default:
2841 err = -EINVAL;
2842 goto out_put_prog;
2845 btp = bpf_get_raw_tracepoint(tp_name);
2846 if (!btp) {
2847 err = -ENOENT;
2848 goto out_put_prog;
2851 link = kzalloc(sizeof(*link), GFP_USER);
2852 if (!link) {
2853 err = -ENOMEM;
2854 goto out_put_btp;
2856 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2857 &bpf_raw_tp_link_lops, prog);
2858 link->btp = btp;
2860 err = bpf_link_prime(&link->link, &link_primer);
2861 if (err) {
2862 kfree(link);
2863 goto out_put_btp;
2866 err = bpf_probe_register(link->btp, prog);
2867 if (err) {
2868 bpf_link_cleanup(&link_primer);
2869 goto out_put_btp;
2872 return bpf_link_settle(&link_primer);
2874 out_put_btp:
2875 bpf_put_raw_tracepoint(btp);
2876 out_put_prog:
2877 bpf_prog_put(prog);
2878 return err;
2881 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2882 enum bpf_attach_type attach_type)
2884 switch (prog->type) {
2885 case BPF_PROG_TYPE_CGROUP_SOCK:
2886 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2887 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2888 case BPF_PROG_TYPE_SK_LOOKUP:
2889 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
2890 case BPF_PROG_TYPE_CGROUP_SKB:
2891 if (!capable(CAP_NET_ADMIN))
2892 /* cg-skb progs can be loaded by unpriv user.
2893 * check permissions at attach time.
2895 return -EPERM;
2896 return prog->enforce_expected_attach_type &&
2897 prog->expected_attach_type != attach_type ?
2898 -EINVAL : 0;
2899 default:
2900 return 0;
2904 static enum bpf_prog_type
2905 attach_type_to_prog_type(enum bpf_attach_type attach_type)
2907 switch (attach_type) {
2908 case BPF_CGROUP_INET_INGRESS:
2909 case BPF_CGROUP_INET_EGRESS:
2910 return BPF_PROG_TYPE_CGROUP_SKB;
2911 case BPF_CGROUP_INET_SOCK_CREATE:
2912 case BPF_CGROUP_INET_SOCK_RELEASE:
2913 case BPF_CGROUP_INET4_POST_BIND:
2914 case BPF_CGROUP_INET6_POST_BIND:
2915 return BPF_PROG_TYPE_CGROUP_SOCK;
2916 case BPF_CGROUP_INET4_BIND:
2917 case BPF_CGROUP_INET6_BIND:
2918 case BPF_CGROUP_INET4_CONNECT:
2919 case BPF_CGROUP_INET6_CONNECT:
2920 case BPF_CGROUP_INET4_GETPEERNAME:
2921 case BPF_CGROUP_INET6_GETPEERNAME:
2922 case BPF_CGROUP_INET4_GETSOCKNAME:
2923 case BPF_CGROUP_INET6_GETSOCKNAME:
2924 case BPF_CGROUP_UDP4_SENDMSG:
2925 case BPF_CGROUP_UDP6_SENDMSG:
2926 case BPF_CGROUP_UDP4_RECVMSG:
2927 case BPF_CGROUP_UDP6_RECVMSG:
2928 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2929 case BPF_CGROUP_SOCK_OPS:
2930 return BPF_PROG_TYPE_SOCK_OPS;
2931 case BPF_CGROUP_DEVICE:
2932 return BPF_PROG_TYPE_CGROUP_DEVICE;
2933 case BPF_SK_MSG_VERDICT:
2934 return BPF_PROG_TYPE_SK_MSG;
2935 case BPF_SK_SKB_STREAM_PARSER:
2936 case BPF_SK_SKB_STREAM_VERDICT:
2937 return BPF_PROG_TYPE_SK_SKB;
2938 case BPF_LIRC_MODE2:
2939 return BPF_PROG_TYPE_LIRC_MODE2;
2940 case BPF_FLOW_DISSECTOR:
2941 return BPF_PROG_TYPE_FLOW_DISSECTOR;
2942 case BPF_CGROUP_SYSCTL:
2943 return BPF_PROG_TYPE_CGROUP_SYSCTL;
2944 case BPF_CGROUP_GETSOCKOPT:
2945 case BPF_CGROUP_SETSOCKOPT:
2946 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
2947 case BPF_TRACE_ITER:
2948 return BPF_PROG_TYPE_TRACING;
2949 case BPF_SK_LOOKUP:
2950 return BPF_PROG_TYPE_SK_LOOKUP;
2951 case BPF_XDP:
2952 return BPF_PROG_TYPE_XDP;
2953 default:
2954 return BPF_PROG_TYPE_UNSPEC;
2958 #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
2960 #define BPF_F_ATTACH_MASK \
2961 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
2963 static int bpf_prog_attach(const union bpf_attr *attr)
2965 enum bpf_prog_type ptype;
2966 struct bpf_prog *prog;
2967 int ret;
2969 if (CHECK_ATTR(BPF_PROG_ATTACH))
2970 return -EINVAL;
2972 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
2973 return -EINVAL;
2975 ptype = attach_type_to_prog_type(attr->attach_type);
2976 if (ptype == BPF_PROG_TYPE_UNSPEC)
2977 return -EINVAL;
2979 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2980 if (IS_ERR(prog))
2981 return PTR_ERR(prog);
2983 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
2984 bpf_prog_put(prog);
2985 return -EINVAL;
2988 switch (ptype) {
2989 case BPF_PROG_TYPE_SK_SKB:
2990 case BPF_PROG_TYPE_SK_MSG:
2991 ret = sock_map_get_from_fd(attr, prog);
2992 break;
2993 case BPF_PROG_TYPE_LIRC_MODE2:
2994 ret = lirc_prog_attach(attr, prog);
2995 break;
2996 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2997 ret = netns_bpf_prog_attach(attr, prog);
2998 break;
2999 case BPF_PROG_TYPE_CGROUP_DEVICE:
3000 case BPF_PROG_TYPE_CGROUP_SKB:
3001 case BPF_PROG_TYPE_CGROUP_SOCK:
3002 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3003 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3004 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3005 case BPF_PROG_TYPE_SOCK_OPS:
3006 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3007 break;
3008 default:
3009 ret = -EINVAL;
3012 if (ret)
3013 bpf_prog_put(prog);
3014 return ret;
3017 #define BPF_PROG_DETACH_LAST_FIELD attach_type
3019 static int bpf_prog_detach(const union bpf_attr *attr)
3021 enum bpf_prog_type ptype;
3023 if (CHECK_ATTR(BPF_PROG_DETACH))
3024 return -EINVAL;
3026 ptype = attach_type_to_prog_type(attr->attach_type);
3028 switch (ptype) {
3029 case BPF_PROG_TYPE_SK_MSG:
3030 case BPF_PROG_TYPE_SK_SKB:
3031 return sock_map_prog_detach(attr, ptype);
3032 case BPF_PROG_TYPE_LIRC_MODE2:
3033 return lirc_prog_detach(attr);
3034 case BPF_PROG_TYPE_FLOW_DISSECTOR:
3035 return netns_bpf_prog_detach(attr, ptype);
3036 case BPF_PROG_TYPE_CGROUP_DEVICE:
3037 case BPF_PROG_TYPE_CGROUP_SKB:
3038 case BPF_PROG_TYPE_CGROUP_SOCK:
3039 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3040 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3041 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3042 case BPF_PROG_TYPE_SOCK_OPS:
3043 return cgroup_bpf_prog_detach(attr, ptype);
3044 default:
3045 return -EINVAL;
3049 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3051 static int bpf_prog_query(const union bpf_attr *attr,
3052 union bpf_attr __user *uattr)
3054 if (!capable(CAP_NET_ADMIN))
3055 return -EPERM;
3056 if (CHECK_ATTR(BPF_PROG_QUERY))
3057 return -EINVAL;
3058 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3059 return -EINVAL;
3061 switch (attr->query.attach_type) {
3062 case BPF_CGROUP_INET_INGRESS:
3063 case BPF_CGROUP_INET_EGRESS:
3064 case BPF_CGROUP_INET_SOCK_CREATE:
3065 case BPF_CGROUP_INET_SOCK_RELEASE:
3066 case BPF_CGROUP_INET4_BIND:
3067 case BPF_CGROUP_INET6_BIND:
3068 case BPF_CGROUP_INET4_POST_BIND:
3069 case BPF_CGROUP_INET6_POST_BIND:
3070 case BPF_CGROUP_INET4_CONNECT:
3071 case BPF_CGROUP_INET6_CONNECT:
3072 case BPF_CGROUP_INET4_GETPEERNAME:
3073 case BPF_CGROUP_INET6_GETPEERNAME:
3074 case BPF_CGROUP_INET4_GETSOCKNAME:
3075 case BPF_CGROUP_INET6_GETSOCKNAME:
3076 case BPF_CGROUP_UDP4_SENDMSG:
3077 case BPF_CGROUP_UDP6_SENDMSG:
3078 case BPF_CGROUP_UDP4_RECVMSG:
3079 case BPF_CGROUP_UDP6_RECVMSG:
3080 case BPF_CGROUP_SOCK_OPS:
3081 case BPF_CGROUP_DEVICE:
3082 case BPF_CGROUP_SYSCTL:
3083 case BPF_CGROUP_GETSOCKOPT:
3084 case BPF_CGROUP_SETSOCKOPT:
3085 return cgroup_bpf_prog_query(attr, uattr);
3086 case BPF_LIRC_MODE2:
3087 return lirc_prog_query(attr, uattr);
3088 case BPF_FLOW_DISSECTOR:
3089 case BPF_SK_LOOKUP:
3090 return netns_bpf_prog_query(attr, uattr);
3091 default:
3092 return -EINVAL;
3096 #define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
3098 static int bpf_prog_test_run(const union bpf_attr *attr,
3099 union bpf_attr __user *uattr)
3101 struct bpf_prog *prog;
3102 int ret = -ENOTSUPP;
3104 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3105 return -EINVAL;
3107 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3108 (!attr->test.ctx_size_in && attr->test.ctx_in))
3109 return -EINVAL;
3111 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3112 (!attr->test.ctx_size_out && attr->test.ctx_out))
3113 return -EINVAL;
3115 prog = bpf_prog_get(attr->test.prog_fd);
3116 if (IS_ERR(prog))
3117 return PTR_ERR(prog);
3119 if (prog->aux->ops->test_run)
3120 ret = prog->aux->ops->test_run(prog, attr, uattr);
3122 bpf_prog_put(prog);
3123 return ret;
3126 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3128 static int bpf_obj_get_next_id(const union bpf_attr *attr,
3129 union bpf_attr __user *uattr,
3130 struct idr *idr,
3131 spinlock_t *lock)
3133 u32 next_id = attr->start_id;
3134 int err = 0;
3136 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3137 return -EINVAL;
3139 if (!capable(CAP_SYS_ADMIN))
3140 return -EPERM;
3142 next_id++;
3143 spin_lock_bh(lock);
3144 if (!idr_get_next(idr, &next_id))
3145 err = -ENOENT;
3146 spin_unlock_bh(lock);
3148 if (!err)
3149 err = put_user(next_id, &uattr->next_id);
3151 return err;
3154 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3156 struct bpf_map *map;
3158 spin_lock_bh(&map_idr_lock);
3159 again:
3160 map = idr_get_next(&map_idr, id);
3161 if (map) {
3162 map = __bpf_map_inc_not_zero(map, false);
3163 if (IS_ERR(map)) {
3164 (*id)++;
3165 goto again;
3168 spin_unlock_bh(&map_idr_lock);
3170 return map;
3173 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3175 struct bpf_prog *prog;
3177 spin_lock_bh(&prog_idr_lock);
3178 again:
3179 prog = idr_get_next(&prog_idr, id);
3180 if (prog) {
3181 prog = bpf_prog_inc_not_zero(prog);
3182 if (IS_ERR(prog)) {
3183 (*id)++;
3184 goto again;
3187 spin_unlock_bh(&prog_idr_lock);
3189 return prog;
3192 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3194 struct bpf_prog *bpf_prog_by_id(u32 id)
3196 struct bpf_prog *prog;
3198 if (!id)
3199 return ERR_PTR(-ENOENT);
3201 spin_lock_bh(&prog_idr_lock);
3202 prog = idr_find(&prog_idr, id);
3203 if (prog)
3204 prog = bpf_prog_inc_not_zero(prog);
3205 else
3206 prog = ERR_PTR(-ENOENT);
3207 spin_unlock_bh(&prog_idr_lock);
3208 return prog;
3211 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3213 struct bpf_prog *prog;
3214 u32 id = attr->prog_id;
3215 int fd;
3217 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3218 return -EINVAL;
3220 if (!capable(CAP_SYS_ADMIN))
3221 return -EPERM;
3223 prog = bpf_prog_by_id(id);
3224 if (IS_ERR(prog))
3225 return PTR_ERR(prog);
3227 fd = bpf_prog_new_fd(prog);
3228 if (fd < 0)
3229 bpf_prog_put(prog);
3231 return fd;
3234 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3236 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3238 struct bpf_map *map;
3239 u32 id = attr->map_id;
3240 int f_flags;
3241 int fd;
3243 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3244 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3245 return -EINVAL;
3247 if (!capable(CAP_SYS_ADMIN))
3248 return -EPERM;
3250 f_flags = bpf_get_file_flag(attr->open_flags);
3251 if (f_flags < 0)
3252 return f_flags;
3254 spin_lock_bh(&map_idr_lock);
3255 map = idr_find(&map_idr, id);
3256 if (map)
3257 map = __bpf_map_inc_not_zero(map, true);
3258 else
3259 map = ERR_PTR(-ENOENT);
3260 spin_unlock_bh(&map_idr_lock);
3262 if (IS_ERR(map))
3263 return PTR_ERR(map);
3265 fd = bpf_map_new_fd(map, f_flags);
3266 if (fd < 0)
3267 bpf_map_put_with_uref(map);
3269 return fd;
3272 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3273 unsigned long addr, u32 *off,
3274 u32 *type)
3276 const struct bpf_map *map;
3277 int i;
3279 mutex_lock(&prog->aux->used_maps_mutex);
3280 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3281 map = prog->aux->used_maps[i];
3282 if (map == (void *)addr) {
3283 *type = BPF_PSEUDO_MAP_FD;
3284 goto out;
3286 if (!map->ops->map_direct_value_meta)
3287 continue;
3288 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3289 *type = BPF_PSEUDO_MAP_VALUE;
3290 goto out;
3293 map = NULL;
3295 out:
3296 mutex_unlock(&prog->aux->used_maps_mutex);
3297 return map;
3300 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3301 const struct cred *f_cred)
3303 const struct bpf_map *map;
3304 struct bpf_insn *insns;
3305 u32 off, type;
3306 u64 imm;
3307 u8 code;
3308 int i;
3310 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3311 GFP_USER);
3312 if (!insns)
3313 return insns;
3315 for (i = 0; i < prog->len; i++) {
3316 code = insns[i].code;
3318 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
3319 insns[i].code = BPF_JMP | BPF_CALL;
3320 insns[i].imm = BPF_FUNC_tail_call;
3321 /* fall-through */
3323 if (code == (BPF_JMP | BPF_CALL) ||
3324 code == (BPF_JMP | BPF_CALL_ARGS)) {
3325 if (code == (BPF_JMP | BPF_CALL_ARGS))
3326 insns[i].code = BPF_JMP | BPF_CALL;
3327 if (!bpf_dump_raw_ok(f_cred))
3328 insns[i].imm = 0;
3329 continue;
3331 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3332 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3333 continue;
3336 if (code != (BPF_LD | BPF_IMM | BPF_DW))
3337 continue;
3339 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3340 map = bpf_map_from_imm(prog, imm, &off, &type);
3341 if (map) {
3342 insns[i].src_reg = type;
3343 insns[i].imm = map->id;
3344 insns[i + 1].imm = off;
3345 continue;
3349 return insns;
3352 static int set_info_rec_size(struct bpf_prog_info *info)
3355 * Ensure info.*_rec_size is the same as kernel expected size
3357 * or
3359 * Only allow zero *_rec_size if both _rec_size and _cnt are
3360 * zero. In this case, the kernel will set the expected
3361 * _rec_size back to the info.
3364 if ((info->nr_func_info || info->func_info_rec_size) &&
3365 info->func_info_rec_size != sizeof(struct bpf_func_info))
3366 return -EINVAL;
3368 if ((info->nr_line_info || info->line_info_rec_size) &&
3369 info->line_info_rec_size != sizeof(struct bpf_line_info))
3370 return -EINVAL;
3372 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3373 info->jited_line_info_rec_size != sizeof(__u64))
3374 return -EINVAL;
3376 info->func_info_rec_size = sizeof(struct bpf_func_info);
3377 info->line_info_rec_size = sizeof(struct bpf_line_info);
3378 info->jited_line_info_rec_size = sizeof(__u64);
3380 return 0;
3383 static int bpf_prog_get_info_by_fd(struct file *file,
3384 struct bpf_prog *prog,
3385 const union bpf_attr *attr,
3386 union bpf_attr __user *uattr)
3388 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3389 struct bpf_prog_info info;
3390 u32 info_len = attr->info.info_len;
3391 struct bpf_prog_stats stats;
3392 char __user *uinsns;
3393 u32 ulen;
3394 int err;
3396 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3397 if (err)
3398 return err;
3399 info_len = min_t(u32, sizeof(info), info_len);
3401 memset(&info, 0, sizeof(info));
3402 if (copy_from_user(&info, uinfo, info_len))
3403 return -EFAULT;
3405 info.type = prog->type;
3406 info.id = prog->aux->id;
3407 info.load_time = prog->aux->load_time;
3408 info.created_by_uid = from_kuid_munged(current_user_ns(),
3409 prog->aux->user->uid);
3410 info.gpl_compatible = prog->gpl_compatible;
3412 memcpy(info.tag, prog->tag, sizeof(prog->tag));
3413 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3415 mutex_lock(&prog->aux->used_maps_mutex);
3416 ulen = info.nr_map_ids;
3417 info.nr_map_ids = prog->aux->used_map_cnt;
3418 ulen = min_t(u32, info.nr_map_ids, ulen);
3419 if (ulen) {
3420 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3421 u32 i;
3423 for (i = 0; i < ulen; i++)
3424 if (put_user(prog->aux->used_maps[i]->id,
3425 &user_map_ids[i])) {
3426 mutex_unlock(&prog->aux->used_maps_mutex);
3427 return -EFAULT;
3430 mutex_unlock(&prog->aux->used_maps_mutex);
3432 err = set_info_rec_size(&info);
3433 if (err)
3434 return err;
3436 bpf_prog_get_stats(prog, &stats);
3437 info.run_time_ns = stats.nsecs;
3438 info.run_cnt = stats.cnt;
3440 if (!bpf_capable()) {
3441 info.jited_prog_len = 0;
3442 info.xlated_prog_len = 0;
3443 info.nr_jited_ksyms = 0;
3444 info.nr_jited_func_lens = 0;
3445 info.nr_func_info = 0;
3446 info.nr_line_info = 0;
3447 info.nr_jited_line_info = 0;
3448 goto done;
3451 ulen = info.xlated_prog_len;
3452 info.xlated_prog_len = bpf_prog_insn_size(prog);
3453 if (info.xlated_prog_len && ulen) {
3454 struct bpf_insn *insns_sanitized;
3455 bool fault;
3457 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
3458 info.xlated_prog_insns = 0;
3459 goto done;
3461 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
3462 if (!insns_sanitized)
3463 return -ENOMEM;
3464 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3465 ulen = min_t(u32, info.xlated_prog_len, ulen);
3466 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3467 kfree(insns_sanitized);
3468 if (fault)
3469 return -EFAULT;
3472 if (bpf_prog_is_dev_bound(prog->aux)) {
3473 err = bpf_prog_offload_info_fill(&info, prog);
3474 if (err)
3475 return err;
3476 goto done;
3479 /* NOTE: the following code is supposed to be skipped for offload.
3480 * bpf_prog_offload_info_fill() is the place to fill similar fields
3481 * for offload.
3483 ulen = info.jited_prog_len;
3484 if (prog->aux->func_cnt) {
3485 u32 i;
3487 info.jited_prog_len = 0;
3488 for (i = 0; i < prog->aux->func_cnt; i++)
3489 info.jited_prog_len += prog->aux->func[i]->jited_len;
3490 } else {
3491 info.jited_prog_len = prog->jited_len;
3494 if (info.jited_prog_len && ulen) {
3495 if (bpf_dump_raw_ok(file->f_cred)) {
3496 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3497 ulen = min_t(u32, info.jited_prog_len, ulen);
3499 /* for multi-function programs, copy the JITed
3500 * instructions for all the functions
3502 if (prog->aux->func_cnt) {
3503 u32 len, free, i;
3504 u8 *img;
3506 free = ulen;
3507 for (i = 0; i < prog->aux->func_cnt; i++) {
3508 len = prog->aux->func[i]->jited_len;
3509 len = min_t(u32, len, free);
3510 img = (u8 *) prog->aux->func[i]->bpf_func;
3511 if (copy_to_user(uinsns, img, len))
3512 return -EFAULT;
3513 uinsns += len;
3514 free -= len;
3515 if (!free)
3516 break;
3518 } else {
3519 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3520 return -EFAULT;
3522 } else {
3523 info.jited_prog_insns = 0;
3527 ulen = info.nr_jited_ksyms;
3528 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
3529 if (ulen) {
3530 if (bpf_dump_raw_ok(file->f_cred)) {
3531 unsigned long ksym_addr;
3532 u64 __user *user_ksyms;
3533 u32 i;
3535 /* copy the address of the kernel symbol
3536 * corresponding to each function
3538 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3539 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3540 if (prog->aux->func_cnt) {
3541 for (i = 0; i < ulen; i++) {
3542 ksym_addr = (unsigned long)
3543 prog->aux->func[i]->bpf_func;
3544 if (put_user((u64) ksym_addr,
3545 &user_ksyms[i]))
3546 return -EFAULT;
3548 } else {
3549 ksym_addr = (unsigned long) prog->bpf_func;
3550 if (put_user((u64) ksym_addr, &user_ksyms[0]))
3551 return -EFAULT;
3553 } else {
3554 info.jited_ksyms = 0;
3558 ulen = info.nr_jited_func_lens;
3559 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
3560 if (ulen) {
3561 if (bpf_dump_raw_ok(file->f_cred)) {
3562 u32 __user *user_lens;
3563 u32 func_len, i;
3565 /* copy the JITed image lengths for each function */
3566 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3567 user_lens = u64_to_user_ptr(info.jited_func_lens);
3568 if (prog->aux->func_cnt) {
3569 for (i = 0; i < ulen; i++) {
3570 func_len =
3571 prog->aux->func[i]->jited_len;
3572 if (put_user(func_len, &user_lens[i]))
3573 return -EFAULT;
3575 } else {
3576 func_len = prog->jited_len;
3577 if (put_user(func_len, &user_lens[0]))
3578 return -EFAULT;
3580 } else {
3581 info.jited_func_lens = 0;
3585 if (prog->aux->btf)
3586 info.btf_id = btf_obj_id(prog->aux->btf);
3588 ulen = info.nr_func_info;
3589 info.nr_func_info = prog->aux->func_info_cnt;
3590 if (info.nr_func_info && ulen) {
3591 char __user *user_finfo;
3593 user_finfo = u64_to_user_ptr(info.func_info);
3594 ulen = min_t(u32, info.nr_func_info, ulen);
3595 if (copy_to_user(user_finfo, prog->aux->func_info,
3596 info.func_info_rec_size * ulen))
3597 return -EFAULT;
3600 ulen = info.nr_line_info;
3601 info.nr_line_info = prog->aux->nr_linfo;
3602 if (info.nr_line_info && ulen) {
3603 __u8 __user *user_linfo;
3605 user_linfo = u64_to_user_ptr(info.line_info);
3606 ulen = min_t(u32, info.nr_line_info, ulen);
3607 if (copy_to_user(user_linfo, prog->aux->linfo,
3608 info.line_info_rec_size * ulen))
3609 return -EFAULT;
3612 ulen = info.nr_jited_line_info;
3613 if (prog->aux->jited_linfo)
3614 info.nr_jited_line_info = prog->aux->nr_linfo;
3615 else
3616 info.nr_jited_line_info = 0;
3617 if (info.nr_jited_line_info && ulen) {
3618 if (bpf_dump_raw_ok(file->f_cred)) {
3619 __u64 __user *user_linfo;
3620 u32 i;
3622 user_linfo = u64_to_user_ptr(info.jited_line_info);
3623 ulen = min_t(u32, info.nr_jited_line_info, ulen);
3624 for (i = 0; i < ulen; i++) {
3625 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3626 &user_linfo[i]))
3627 return -EFAULT;
3629 } else {
3630 info.jited_line_info = 0;
3634 ulen = info.nr_prog_tags;
3635 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3636 if (ulen) {
3637 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3638 u32 i;
3640 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3641 ulen = min_t(u32, info.nr_prog_tags, ulen);
3642 if (prog->aux->func_cnt) {
3643 for (i = 0; i < ulen; i++) {
3644 if (copy_to_user(user_prog_tags[i],
3645 prog->aux->func[i]->tag,
3646 BPF_TAG_SIZE))
3647 return -EFAULT;
3649 } else {
3650 if (copy_to_user(user_prog_tags[0],
3651 prog->tag, BPF_TAG_SIZE))
3652 return -EFAULT;
3656 done:
3657 if (copy_to_user(uinfo, &info, info_len) ||
3658 put_user(info_len, &uattr->info.info_len))
3659 return -EFAULT;
3661 return 0;
3664 static int bpf_map_get_info_by_fd(struct file *file,
3665 struct bpf_map *map,
3666 const union bpf_attr *attr,
3667 union bpf_attr __user *uattr)
3669 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3670 struct bpf_map_info info;
3671 u32 info_len = attr->info.info_len;
3672 int err;
3674 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3675 if (err)
3676 return err;
3677 info_len = min_t(u32, sizeof(info), info_len);
3679 memset(&info, 0, sizeof(info));
3680 info.type = map->map_type;
3681 info.id = map->id;
3682 info.key_size = map->key_size;
3683 info.value_size = map->value_size;
3684 info.max_entries = map->max_entries;
3685 info.map_flags = map->map_flags;
3686 memcpy(info.name, map->name, sizeof(map->name));
3688 if (map->btf) {
3689 info.btf_id = btf_obj_id(map->btf);
3690 info.btf_key_type_id = map->btf_key_type_id;
3691 info.btf_value_type_id = map->btf_value_type_id;
3693 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
3695 if (bpf_map_is_dev_bound(map)) {
3696 err = bpf_map_offload_info_fill(&info, map);
3697 if (err)
3698 return err;
3701 if (copy_to_user(uinfo, &info, info_len) ||
3702 put_user(info_len, &uattr->info.info_len))
3703 return -EFAULT;
3705 return 0;
3708 static int bpf_btf_get_info_by_fd(struct file *file,
3709 struct btf *btf,
3710 const union bpf_attr *attr,
3711 union bpf_attr __user *uattr)
3713 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3714 u32 info_len = attr->info.info_len;
3715 int err;
3717 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
3718 if (err)
3719 return err;
3721 return btf_get_info_by_fd(btf, attr, uattr);
3724 static int bpf_link_get_info_by_fd(struct file *file,
3725 struct bpf_link *link,
3726 const union bpf_attr *attr,
3727 union bpf_attr __user *uattr)
3729 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3730 struct bpf_link_info info;
3731 u32 info_len = attr->info.info_len;
3732 int err;
3734 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3735 if (err)
3736 return err;
3737 info_len = min_t(u32, sizeof(info), info_len);
3739 memset(&info, 0, sizeof(info));
3740 if (copy_from_user(&info, uinfo, info_len))
3741 return -EFAULT;
3743 info.type = link->type;
3744 info.id = link->id;
3745 info.prog_id = link->prog->aux->id;
3747 if (link->ops->fill_link_info) {
3748 err = link->ops->fill_link_info(link, &info);
3749 if (err)
3750 return err;
3753 if (copy_to_user(uinfo, &info, info_len) ||
3754 put_user(info_len, &uattr->info.info_len))
3755 return -EFAULT;
3757 return 0;
3761 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3763 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3764 union bpf_attr __user *uattr)
3766 int ufd = attr->info.bpf_fd;
3767 struct fd f;
3768 int err;
3770 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3771 return -EINVAL;
3773 f = fdget(ufd);
3774 if (!f.file)
3775 return -EBADFD;
3777 if (f.file->f_op == &bpf_prog_fops)
3778 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
3779 uattr);
3780 else if (f.file->f_op == &bpf_map_fops)
3781 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
3782 uattr);
3783 else if (f.file->f_op == &btf_fops)
3784 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
3785 else if (f.file->f_op == &bpf_link_fops)
3786 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
3787 attr, uattr);
3788 else
3789 err = -EINVAL;
3791 fdput(f);
3792 return err;
3795 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3797 static int bpf_btf_load(const union bpf_attr *attr)
3799 if (CHECK_ATTR(BPF_BTF_LOAD))
3800 return -EINVAL;
3802 if (!bpf_capable())
3803 return -EPERM;
3805 return btf_new_fd(attr);
3808 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3810 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3812 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3813 return -EINVAL;
3815 if (!capable(CAP_SYS_ADMIN))
3816 return -EPERM;
3818 return btf_get_fd_by_id(attr->btf_id);
3821 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3822 union bpf_attr __user *uattr,
3823 u32 prog_id, u32 fd_type,
3824 const char *buf, u64 probe_offset,
3825 u64 probe_addr)
3827 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3828 u32 len = buf ? strlen(buf) : 0, input_len;
3829 int err = 0;
3831 if (put_user(len, &uattr->task_fd_query.buf_len))
3832 return -EFAULT;
3833 input_len = attr->task_fd_query.buf_len;
3834 if (input_len && ubuf) {
3835 if (!len) {
3836 /* nothing to copy, just make ubuf NULL terminated */
3837 char zero = '\0';
3839 if (put_user(zero, ubuf))
3840 return -EFAULT;
3841 } else if (input_len >= len + 1) {
3842 /* ubuf can hold the string with NULL terminator */
3843 if (copy_to_user(ubuf, buf, len + 1))
3844 return -EFAULT;
3845 } else {
3846 /* ubuf cannot hold the string with NULL terminator,
3847 * do a partial copy with NULL terminator.
3849 char zero = '\0';
3851 err = -ENOSPC;
3852 if (copy_to_user(ubuf, buf, input_len - 1))
3853 return -EFAULT;
3854 if (put_user(zero, ubuf + input_len - 1))
3855 return -EFAULT;
3859 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3860 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3861 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3862 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3863 return -EFAULT;
3865 return err;
3868 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3870 static int bpf_task_fd_query(const union bpf_attr *attr,
3871 union bpf_attr __user *uattr)
3873 pid_t pid = attr->task_fd_query.pid;
3874 u32 fd = attr->task_fd_query.fd;
3875 const struct perf_event *event;
3876 struct task_struct *task;
3877 struct file *file;
3878 int err;
3880 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3881 return -EINVAL;
3883 if (!capable(CAP_SYS_ADMIN))
3884 return -EPERM;
3886 if (attr->task_fd_query.flags != 0)
3887 return -EINVAL;
3889 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3890 if (!task)
3891 return -ENOENT;
3893 err = 0;
3894 file = fget_task(task, fd);
3895 put_task_struct(task);
3896 if (!file)
3897 return -EBADF;
3899 if (file->f_op == &bpf_link_fops) {
3900 struct bpf_link *link = file->private_data;
3902 if (link->ops == &bpf_raw_tp_link_lops) {
3903 struct bpf_raw_tp_link *raw_tp =
3904 container_of(link, struct bpf_raw_tp_link, link);
3905 struct bpf_raw_event_map *btp = raw_tp->btp;
3907 err = bpf_task_fd_query_copy(attr, uattr,
3908 raw_tp->link.prog->aux->id,
3909 BPF_FD_TYPE_RAW_TRACEPOINT,
3910 btp->tp->name, 0, 0);
3911 goto put_file;
3913 goto out_not_supp;
3916 event = perf_get_event(file);
3917 if (!IS_ERR(event)) {
3918 u64 probe_offset, probe_addr;
3919 u32 prog_id, fd_type;
3920 const char *buf;
3922 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3923 &buf, &probe_offset,
3924 &probe_addr);
3925 if (!err)
3926 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3927 fd_type, buf,
3928 probe_offset,
3929 probe_addr);
3930 goto put_file;
3933 out_not_supp:
3934 err = -ENOTSUPP;
3935 put_file:
3936 fput(file);
3937 return err;
3940 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
3942 #define BPF_DO_BATCH(fn) \
3943 do { \
3944 if (!fn) { \
3945 err = -ENOTSUPP; \
3946 goto err_put; \
3948 err = fn(map, attr, uattr); \
3949 } while (0)
3951 static int bpf_map_do_batch(const union bpf_attr *attr,
3952 union bpf_attr __user *uattr,
3953 int cmd)
3955 struct bpf_map *map;
3956 int err, ufd;
3957 struct fd f;
3959 if (CHECK_ATTR(BPF_MAP_BATCH))
3960 return -EINVAL;
3962 ufd = attr->batch.map_fd;
3963 f = fdget(ufd);
3964 map = __bpf_map_get(f);
3965 if (IS_ERR(map))
3966 return PTR_ERR(map);
3968 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
3969 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
3970 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
3971 err = -EPERM;
3972 goto err_put;
3975 if (cmd != BPF_MAP_LOOKUP_BATCH &&
3976 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
3977 err = -EPERM;
3978 goto err_put;
3981 if (cmd == BPF_MAP_LOOKUP_BATCH)
3982 BPF_DO_BATCH(map->ops->map_lookup_batch);
3983 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
3984 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
3985 else if (cmd == BPF_MAP_UPDATE_BATCH)
3986 BPF_DO_BATCH(map->ops->map_update_batch);
3987 else
3988 BPF_DO_BATCH(map->ops->map_delete_batch);
3990 err_put:
3991 fdput(f);
3992 return err;
3995 static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3997 if (attr->link_create.attach_type != prog->expected_attach_type)
3998 return -EINVAL;
4000 if (prog->expected_attach_type == BPF_TRACE_ITER)
4001 return bpf_iter_link_attach(attr, prog);
4002 else if (prog->type == BPF_PROG_TYPE_EXT)
4003 return bpf_tracing_prog_attach(prog,
4004 attr->link_create.target_fd,
4005 attr->link_create.target_btf_id);
4006 return -EINVAL;
4009 #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
4010 static int link_create(union bpf_attr *attr)
4012 enum bpf_prog_type ptype;
4013 struct bpf_prog *prog;
4014 int ret;
4016 if (CHECK_ATTR(BPF_LINK_CREATE))
4017 return -EINVAL;
4019 prog = bpf_prog_get(attr->link_create.prog_fd);
4020 if (IS_ERR(prog))
4021 return PTR_ERR(prog);
4023 ret = bpf_prog_attach_check_attach_type(prog,
4024 attr->link_create.attach_type);
4025 if (ret)
4026 goto out;
4028 if (prog->type == BPF_PROG_TYPE_EXT) {
4029 ret = tracing_bpf_link_attach(attr, prog);
4030 goto out;
4033 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4034 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4035 ret = -EINVAL;
4036 goto out;
4039 switch (ptype) {
4040 case BPF_PROG_TYPE_CGROUP_SKB:
4041 case BPF_PROG_TYPE_CGROUP_SOCK:
4042 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4043 case BPF_PROG_TYPE_SOCK_OPS:
4044 case BPF_PROG_TYPE_CGROUP_DEVICE:
4045 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4046 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4047 ret = cgroup_bpf_link_attach(attr, prog);
4048 break;
4049 case BPF_PROG_TYPE_TRACING:
4050 ret = tracing_bpf_link_attach(attr, prog);
4051 break;
4052 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4053 case BPF_PROG_TYPE_SK_LOOKUP:
4054 ret = netns_bpf_link_create(attr, prog);
4055 break;
4056 #ifdef CONFIG_NET
4057 case BPF_PROG_TYPE_XDP:
4058 ret = bpf_xdp_link_attach(attr, prog);
4059 break;
4060 #endif
4061 default:
4062 ret = -EINVAL;
4065 out:
4066 if (ret < 0)
4067 bpf_prog_put(prog);
4068 return ret;
4071 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4073 static int link_update(union bpf_attr *attr)
4075 struct bpf_prog *old_prog = NULL, *new_prog;
4076 struct bpf_link *link;
4077 u32 flags;
4078 int ret;
4080 if (CHECK_ATTR(BPF_LINK_UPDATE))
4081 return -EINVAL;
4083 flags = attr->link_update.flags;
4084 if (flags & ~BPF_F_REPLACE)
4085 return -EINVAL;
4087 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4088 if (IS_ERR(link))
4089 return PTR_ERR(link);
4091 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4092 if (IS_ERR(new_prog)) {
4093 ret = PTR_ERR(new_prog);
4094 goto out_put_link;
4097 if (flags & BPF_F_REPLACE) {
4098 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4099 if (IS_ERR(old_prog)) {
4100 ret = PTR_ERR(old_prog);
4101 old_prog = NULL;
4102 goto out_put_progs;
4104 } else if (attr->link_update.old_prog_fd) {
4105 ret = -EINVAL;
4106 goto out_put_progs;
4109 if (link->ops->update_prog)
4110 ret = link->ops->update_prog(link, new_prog, old_prog);
4111 else
4112 ret = -EINVAL;
4114 out_put_progs:
4115 if (old_prog)
4116 bpf_prog_put(old_prog);
4117 if (ret)
4118 bpf_prog_put(new_prog);
4119 out_put_link:
4120 bpf_link_put(link);
4121 return ret;
4124 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4126 static int link_detach(union bpf_attr *attr)
4128 struct bpf_link *link;
4129 int ret;
4131 if (CHECK_ATTR(BPF_LINK_DETACH))
4132 return -EINVAL;
4134 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4135 if (IS_ERR(link))
4136 return PTR_ERR(link);
4138 if (link->ops->detach)
4139 ret = link->ops->detach(link);
4140 else
4141 ret = -EOPNOTSUPP;
4143 bpf_link_put(link);
4144 return ret;
4147 static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
4149 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4152 struct bpf_link *bpf_link_by_id(u32 id)
4154 struct bpf_link *link;
4156 if (!id)
4157 return ERR_PTR(-ENOENT);
4159 spin_lock_bh(&link_idr_lock);
4160 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4161 link = idr_find(&link_idr, id);
4162 if (link) {
4163 if (link->id)
4164 link = bpf_link_inc_not_zero(link);
4165 else
4166 link = ERR_PTR(-EAGAIN);
4167 } else {
4168 link = ERR_PTR(-ENOENT);
4170 spin_unlock_bh(&link_idr_lock);
4171 return link;
4174 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4176 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4178 struct bpf_link *link;
4179 u32 id = attr->link_id;
4180 int fd;
4182 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4183 return -EINVAL;
4185 if (!capable(CAP_SYS_ADMIN))
4186 return -EPERM;
4188 link = bpf_link_by_id(id);
4189 if (IS_ERR(link))
4190 return PTR_ERR(link);
4192 fd = bpf_link_new_fd(link);
4193 if (fd < 0)
4194 bpf_link_put(link);
4196 return fd;
4199 DEFINE_MUTEX(bpf_stats_enabled_mutex);
4201 static int bpf_stats_release(struct inode *inode, struct file *file)
4203 mutex_lock(&bpf_stats_enabled_mutex);
4204 static_key_slow_dec(&bpf_stats_enabled_key.key);
4205 mutex_unlock(&bpf_stats_enabled_mutex);
4206 return 0;
4209 static const struct file_operations bpf_stats_fops = {
4210 .release = bpf_stats_release,
4213 static int bpf_enable_runtime_stats(void)
4215 int fd;
4217 mutex_lock(&bpf_stats_enabled_mutex);
4219 /* Set a very high limit to avoid overflow */
4220 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4221 mutex_unlock(&bpf_stats_enabled_mutex);
4222 return -EBUSY;
4225 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4226 if (fd >= 0)
4227 static_key_slow_inc(&bpf_stats_enabled_key.key);
4229 mutex_unlock(&bpf_stats_enabled_mutex);
4230 return fd;
4233 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4235 static int bpf_enable_stats(union bpf_attr *attr)
4238 if (CHECK_ATTR(BPF_ENABLE_STATS))
4239 return -EINVAL;
4241 if (!capable(CAP_SYS_ADMIN))
4242 return -EPERM;
4244 switch (attr->enable_stats.type) {
4245 case BPF_STATS_RUN_TIME:
4246 return bpf_enable_runtime_stats();
4247 default:
4248 break;
4250 return -EINVAL;
4253 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4255 static int bpf_iter_create(union bpf_attr *attr)
4257 struct bpf_link *link;
4258 int err;
4260 if (CHECK_ATTR(BPF_ITER_CREATE))
4261 return -EINVAL;
4263 if (attr->iter_create.flags)
4264 return -EINVAL;
4266 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4267 if (IS_ERR(link))
4268 return PTR_ERR(link);
4270 err = bpf_iter_new_fd(link);
4271 bpf_link_put(link);
4273 return err;
4276 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4278 static int bpf_prog_bind_map(union bpf_attr *attr)
4280 struct bpf_prog *prog;
4281 struct bpf_map *map;
4282 struct bpf_map **used_maps_old, **used_maps_new;
4283 int i, ret = 0;
4285 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4286 return -EINVAL;
4288 if (attr->prog_bind_map.flags)
4289 return -EINVAL;
4291 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4292 if (IS_ERR(prog))
4293 return PTR_ERR(prog);
4295 map = bpf_map_get(attr->prog_bind_map.map_fd);
4296 if (IS_ERR(map)) {
4297 ret = PTR_ERR(map);
4298 goto out_prog_put;
4301 mutex_lock(&prog->aux->used_maps_mutex);
4303 used_maps_old = prog->aux->used_maps;
4305 for (i = 0; i < prog->aux->used_map_cnt; i++)
4306 if (used_maps_old[i] == map) {
4307 bpf_map_put(map);
4308 goto out_unlock;
4311 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4312 sizeof(used_maps_new[0]),
4313 GFP_KERNEL);
4314 if (!used_maps_new) {
4315 ret = -ENOMEM;
4316 goto out_unlock;
4319 memcpy(used_maps_new, used_maps_old,
4320 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4321 used_maps_new[prog->aux->used_map_cnt] = map;
4323 prog->aux->used_map_cnt++;
4324 prog->aux->used_maps = used_maps_new;
4326 kfree(used_maps_old);
4328 out_unlock:
4329 mutex_unlock(&prog->aux->used_maps_mutex);
4331 if (ret)
4332 bpf_map_put(map);
4333 out_prog_put:
4334 bpf_prog_put(prog);
4335 return ret;
4338 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4340 union bpf_attr attr;
4341 int err;
4343 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
4344 return -EPERM;
4346 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
4347 if (err)
4348 return err;
4349 size = min_t(u32, size, sizeof(attr));
4351 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
4352 memset(&attr, 0, sizeof(attr));
4353 if (copy_from_user(&attr, uattr, size) != 0)
4354 return -EFAULT;
4356 err = security_bpf(cmd, &attr, size);
4357 if (err < 0)
4358 return err;
4360 switch (cmd) {
4361 case BPF_MAP_CREATE:
4362 err = map_create(&attr);
4363 break;
4364 case BPF_MAP_LOOKUP_ELEM:
4365 err = map_lookup_elem(&attr);
4366 break;
4367 case BPF_MAP_UPDATE_ELEM:
4368 err = map_update_elem(&attr);
4369 break;
4370 case BPF_MAP_DELETE_ELEM:
4371 err = map_delete_elem(&attr);
4372 break;
4373 case BPF_MAP_GET_NEXT_KEY:
4374 err = map_get_next_key(&attr);
4375 break;
4376 case BPF_MAP_FREEZE:
4377 err = map_freeze(&attr);
4378 break;
4379 case BPF_PROG_LOAD:
4380 err = bpf_prog_load(&attr, uattr);
4381 break;
4382 case BPF_OBJ_PIN:
4383 err = bpf_obj_pin(&attr);
4384 break;
4385 case BPF_OBJ_GET:
4386 err = bpf_obj_get(&attr);
4387 break;
4388 case BPF_PROG_ATTACH:
4389 err = bpf_prog_attach(&attr);
4390 break;
4391 case BPF_PROG_DETACH:
4392 err = bpf_prog_detach(&attr);
4393 break;
4394 case BPF_PROG_QUERY:
4395 err = bpf_prog_query(&attr, uattr);
4396 break;
4397 case BPF_PROG_TEST_RUN:
4398 err = bpf_prog_test_run(&attr, uattr);
4399 break;
4400 case BPF_PROG_GET_NEXT_ID:
4401 err = bpf_obj_get_next_id(&attr, uattr,
4402 &prog_idr, &prog_idr_lock);
4403 break;
4404 case BPF_MAP_GET_NEXT_ID:
4405 err = bpf_obj_get_next_id(&attr, uattr,
4406 &map_idr, &map_idr_lock);
4407 break;
4408 case BPF_BTF_GET_NEXT_ID:
4409 err = bpf_obj_get_next_id(&attr, uattr,
4410 &btf_idr, &btf_idr_lock);
4411 break;
4412 case BPF_PROG_GET_FD_BY_ID:
4413 err = bpf_prog_get_fd_by_id(&attr);
4414 break;
4415 case BPF_MAP_GET_FD_BY_ID:
4416 err = bpf_map_get_fd_by_id(&attr);
4417 break;
4418 case BPF_OBJ_GET_INFO_BY_FD:
4419 err = bpf_obj_get_info_by_fd(&attr, uattr);
4420 break;
4421 case BPF_RAW_TRACEPOINT_OPEN:
4422 err = bpf_raw_tracepoint_open(&attr);
4423 break;
4424 case BPF_BTF_LOAD:
4425 err = bpf_btf_load(&attr);
4426 break;
4427 case BPF_BTF_GET_FD_BY_ID:
4428 err = bpf_btf_get_fd_by_id(&attr);
4429 break;
4430 case BPF_TASK_FD_QUERY:
4431 err = bpf_task_fd_query(&attr, uattr);
4432 break;
4433 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4434 err = map_lookup_and_delete_elem(&attr);
4435 break;
4436 case BPF_MAP_LOOKUP_BATCH:
4437 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
4438 break;
4439 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4440 err = bpf_map_do_batch(&attr, uattr,
4441 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4442 break;
4443 case BPF_MAP_UPDATE_BATCH:
4444 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
4445 break;
4446 case BPF_MAP_DELETE_BATCH:
4447 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
4448 break;
4449 case BPF_LINK_CREATE:
4450 err = link_create(&attr);
4451 break;
4452 case BPF_LINK_UPDATE:
4453 err = link_update(&attr);
4454 break;
4455 case BPF_LINK_GET_FD_BY_ID:
4456 err = bpf_link_get_fd_by_id(&attr);
4457 break;
4458 case BPF_LINK_GET_NEXT_ID:
4459 err = bpf_obj_get_next_id(&attr, uattr,
4460 &link_idr, &link_idr_lock);
4461 break;
4462 case BPF_ENABLE_STATS:
4463 err = bpf_enable_stats(&attr);
4464 break;
4465 case BPF_ITER_CREATE:
4466 err = bpf_iter_create(&attr);
4467 break;
4468 case BPF_LINK_DETACH:
4469 err = link_detach(&attr);
4470 break;
4471 case BPF_PROG_BIND_MAP:
4472 err = bpf_prog_bind_map(&attr);
4473 break;
4474 default:
4475 err = -EINVAL;
4476 break;
4479 return err;