xref: /linux/kernel/bpf/syscall.c (revision 260f6f4fda93c8485c8037865c941b42b9cba5d2)
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-cgroup.h>
6 #include <linux/bpf_trace.h>
7 #include <linux/bpf_lirc.h>
8 #include <linux/bpf_verifier.h>
9 #include <linux/bsearch.h>
10 #include <linux/btf.h>
11 #include <linux/syscalls.h>
12 #include <linux/slab.h>
13 #include <linux/sched/signal.h>
14 #include <linux/vmalloc.h>
15 #include <linux/mmzone.h>
16 #include <linux/anon_inodes.h>
17 #include <linux/fdtable.h>
18 #include <linux/file.h>
19 #include <linux/fs.h>
20 #include <linux/license.h>
21 #include <linux/filter.h>
22 #include <linux/kernel.h>
23 #include <linux/idr.h>
24 #include <linux/cred.h>
25 #include <linux/timekeeping.h>
26 #include <linux/ctype.h>
27 #include <linux/nospec.h>
28 #include <linux/audit.h>
29 #include <uapi/linux/btf.h>
30 #include <linux/pgtable.h>
31 #include <linux/bpf_lsm.h>
32 #include <linux/poll.h>
33 #include <linux/sort.h>
34 #include <linux/bpf-netns.h>
35 #include <linux/rcupdate_trace.h>
36 #include <linux/memcontrol.h>
37 #include <linux/trace_events.h>
38 #include <linux/tracepoint.h>
39 #include <linux/overflow.h>
40 
41 #include <net/netfilter/nf_bpf_link.h>
42 #include <net/netkit.h>
43 #include <net/tcx.h>
44 
45 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
46 			  (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
47 			  (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
48 #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
49 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
50 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
51 			IS_FD_HASH(map))
52 
53 #define BPF_OBJ_FLAG_MASK   (BPF_F_RDONLY | BPF_F_WRONLY)
54 
55 DEFINE_PER_CPU(int, bpf_prog_active);
56 static DEFINE_IDR(prog_idr);
57 static DEFINE_SPINLOCK(prog_idr_lock);
58 static DEFINE_IDR(map_idr);
59 static DEFINE_SPINLOCK(map_idr_lock);
60 static DEFINE_IDR(link_idr);
61 static DEFINE_SPINLOCK(link_idr_lock);
62 
63 int sysctl_unprivileged_bpf_disabled __read_mostly =
64 	IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
65 
66 static const struct bpf_map_ops * const bpf_map_types[] = {
67 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
68 #define BPF_MAP_TYPE(_id, _ops) \
69 	[_id] = &_ops,
70 #define BPF_LINK_TYPE(_id, _name)
71 #include <linux/bpf_types.h>
72 #undef BPF_PROG_TYPE
73 #undef BPF_MAP_TYPE
74 #undef BPF_LINK_TYPE
75 };
76 
77 /*
78  * If we're handed a bigger struct than we know of, ensure all the unknown bits
79  * are 0 - i.e. new user-space does not rely on any kernel feature extensions
80  * we don't know about yet.
81  *
82  * There is a ToCToU between this function call and the following
83  * copy_from_user() call. However, this is not a concern since this function is
84  * meant to be a future-proofing of bits.
85  */
86 int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
87 			     size_t expected_size,
88 			     size_t actual_size)
89 {
90 	int res;
91 
92 	if (unlikely(actual_size > PAGE_SIZE))	/* silly large */
93 		return -E2BIG;
94 
95 	if (actual_size <= expected_size)
96 		return 0;
97 
98 	if (uaddr.is_kernel)
99 		res = memchr_inv(uaddr.kernel + expected_size, 0,
100 				 actual_size - expected_size) == NULL;
101 	else
102 		res = check_zeroed_user(uaddr.user + expected_size,
103 					actual_size - expected_size);
104 	if (res < 0)
105 		return res;
106 	return res ? 0 : -E2BIG;
107 }
108 
109 const struct bpf_map_ops bpf_map_offload_ops = {
110 	.map_meta_equal = bpf_map_meta_equal,
111 	.map_alloc = bpf_map_offload_map_alloc,
112 	.map_free = bpf_map_offload_map_free,
113 	.map_check_btf = map_check_no_btf,
114 	.map_mem_usage = bpf_map_offload_map_mem_usage,
115 };
116 
117 static void bpf_map_write_active_inc(struct bpf_map *map)
118 {
119 	atomic64_inc(&map->writecnt);
120 }
121 
122 static void bpf_map_write_active_dec(struct bpf_map *map)
123 {
124 	atomic64_dec(&map->writecnt);
125 }
126 
127 bool bpf_map_write_active(const struct bpf_map *map)
128 {
129 	return atomic64_read(&map->writecnt) != 0;
130 }
131 
132 static u32 bpf_map_value_size(const struct bpf_map *map)
133 {
134 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
135 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
136 	    map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
137 	    map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
138 		return round_up(map->value_size, 8) * num_possible_cpus();
139 	else if (IS_FD_MAP(map))
140 		return sizeof(u32);
141 	else
142 		return  map->value_size;
143 }
144 
145 static void maybe_wait_bpf_programs(struct bpf_map *map)
146 {
147 	/* Wait for any running non-sleepable BPF programs to complete so that
148 	 * userspace, when we return to it, knows that all non-sleepable
149 	 * programs that could be running use the new map value. For sleepable
150 	 * BPF programs, synchronize_rcu_tasks_trace() should be used to wait
151 	 * for the completions of these programs, but considering the waiting
152 	 * time can be very long and userspace may think it will hang forever,
153 	 * so don't handle sleepable BPF programs now.
154 	 */
155 	if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
156 	    map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
157 		synchronize_rcu();
158 }
159 
160 static void unpin_uptr_kaddr(void *kaddr)
161 {
162 	if (kaddr)
163 		unpin_user_page(virt_to_page(kaddr));
164 }
165 
166 static void __bpf_obj_unpin_uptrs(struct btf_record *rec, u32 cnt, void *obj)
167 {
168 	const struct btf_field *field;
169 	void **uptr_addr;
170 	int i;
171 
172 	for (i = 0, field = rec->fields; i < cnt; i++, field++) {
173 		if (field->type != BPF_UPTR)
174 			continue;
175 
176 		uptr_addr = obj + field->offset;
177 		unpin_uptr_kaddr(*uptr_addr);
178 	}
179 }
180 
181 static void bpf_obj_unpin_uptrs(struct btf_record *rec, void *obj)
182 {
183 	if (!btf_record_has_field(rec, BPF_UPTR))
184 		return;
185 
186 	__bpf_obj_unpin_uptrs(rec, rec->cnt, obj);
187 }
188 
189 static int bpf_obj_pin_uptrs(struct btf_record *rec, void *obj)
190 {
191 	const struct btf_field *field;
192 	const struct btf_type *t;
193 	unsigned long start, end;
194 	struct page *page;
195 	void **uptr_addr;
196 	int i, err;
197 
198 	if (!btf_record_has_field(rec, BPF_UPTR))
199 		return 0;
200 
201 	for (i = 0, field = rec->fields; i < rec->cnt; i++, field++) {
202 		if (field->type != BPF_UPTR)
203 			continue;
204 
205 		uptr_addr = obj + field->offset;
206 		start = *(unsigned long *)uptr_addr;
207 		if (!start)
208 			continue;
209 
210 		t = btf_type_by_id(field->kptr.btf, field->kptr.btf_id);
211 		/* t->size was checked for zero before */
212 		if (check_add_overflow(start, t->size - 1, &end)) {
213 			err = -EFAULT;
214 			goto unpin_all;
215 		}
216 
217 		/* The uptr's struct cannot span across two pages */
218 		if ((start & PAGE_MASK) != (end & PAGE_MASK)) {
219 			err = -EOPNOTSUPP;
220 			goto unpin_all;
221 		}
222 
223 		err = pin_user_pages_fast(start, 1, FOLL_LONGTERM | FOLL_WRITE, &page);
224 		if (err != 1)
225 			goto unpin_all;
226 
227 		if (PageHighMem(page)) {
228 			err = -EOPNOTSUPP;
229 			unpin_user_page(page);
230 			goto unpin_all;
231 		}
232 
233 		*uptr_addr = page_address(page) + offset_in_page(start);
234 	}
235 
236 	return 0;
237 
238 unpin_all:
239 	__bpf_obj_unpin_uptrs(rec, i, obj);
240 	return err;
241 }
242 
243 static int bpf_map_update_value(struct bpf_map *map, struct file *map_file,
244 				void *key, void *value, __u64 flags)
245 {
246 	int err;
247 
248 	/* Need to create a kthread, thus must support schedule */
249 	if (bpf_map_is_offloaded(map)) {
250 		return bpf_map_offload_update_elem(map, key, value, flags);
251 	} else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
252 		   map->map_type == BPF_MAP_TYPE_ARENA ||
253 		   map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
254 		return map->ops->map_update_elem(map, key, value, flags);
255 	} else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
256 		   map->map_type == BPF_MAP_TYPE_SOCKMAP) {
257 		return sock_map_update_elem_sys(map, key, value, flags);
258 	} else if (IS_FD_PROG_ARRAY(map)) {
259 		return bpf_fd_array_map_update_elem(map, map_file, key, value,
260 						    flags);
261 	}
262 
263 	bpf_disable_instrumentation();
264 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
265 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
266 		err = bpf_percpu_hash_update(map, key, value, flags);
267 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
268 		err = bpf_percpu_array_update(map, key, value, flags);
269 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
270 		err = bpf_percpu_cgroup_storage_update(map, key, value,
271 						       flags);
272 	} else if (IS_FD_ARRAY(map)) {
273 		err = bpf_fd_array_map_update_elem(map, map_file, key, value,
274 						   flags);
275 	} else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
276 		err = bpf_fd_htab_map_update_elem(map, map_file, key, value,
277 						  flags);
278 	} else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
279 		/* rcu_read_lock() is not needed */
280 		err = bpf_fd_reuseport_array_update_elem(map, key, value,
281 							 flags);
282 	} else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
283 		   map->map_type == BPF_MAP_TYPE_STACK ||
284 		   map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
285 		err = map->ops->map_push_elem(map, value, flags);
286 	} else {
287 		err = bpf_obj_pin_uptrs(map->record, value);
288 		if (!err) {
289 			rcu_read_lock();
290 			err = map->ops->map_update_elem(map, key, value, flags);
291 			rcu_read_unlock();
292 			if (err)
293 				bpf_obj_unpin_uptrs(map->record, value);
294 		}
295 	}
296 	bpf_enable_instrumentation();
297 
298 	return err;
299 }
300 
301 static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
302 			      __u64 flags)
303 {
304 	void *ptr;
305 	int err;
306 
307 	if (bpf_map_is_offloaded(map))
308 		return bpf_map_offload_lookup_elem(map, key, value);
309 
310 	bpf_disable_instrumentation();
311 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
312 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
313 		err = bpf_percpu_hash_copy(map, key, value);
314 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
315 		err = bpf_percpu_array_copy(map, key, value);
316 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
317 		err = bpf_percpu_cgroup_storage_copy(map, key, value);
318 	} else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
319 		err = bpf_stackmap_copy(map, key, value);
320 	} else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
321 		err = bpf_fd_array_map_lookup_elem(map, key, value);
322 	} else if (IS_FD_HASH(map)) {
323 		err = bpf_fd_htab_map_lookup_elem(map, key, value);
324 	} else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
325 		err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
326 	} else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
327 		   map->map_type == BPF_MAP_TYPE_STACK ||
328 		   map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
329 		err = map->ops->map_peek_elem(map, value);
330 	} else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
331 		/* struct_ops map requires directly updating "value" */
332 		err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
333 	} else {
334 		rcu_read_lock();
335 		if (map->ops->map_lookup_elem_sys_only)
336 			ptr = map->ops->map_lookup_elem_sys_only(map, key);
337 		else
338 			ptr = map->ops->map_lookup_elem(map, key);
339 		if (IS_ERR(ptr)) {
340 			err = PTR_ERR(ptr);
341 		} else if (!ptr) {
342 			err = -ENOENT;
343 		} else {
344 			err = 0;
345 			if (flags & BPF_F_LOCK)
346 				/* lock 'ptr' and copy everything but lock */
347 				copy_map_value_locked(map, value, ptr, true);
348 			else
349 				copy_map_value(map, value, ptr);
350 			/* mask lock and timer, since value wasn't zero inited */
351 			check_and_init_map_value(map, value);
352 		}
353 		rcu_read_unlock();
354 	}
355 
356 	bpf_enable_instrumentation();
357 
358 	return err;
359 }
360 
361 /* Please, do not use this function outside from the map creation path
362  * (e.g. in map update path) without taking care of setting the active
363  * memory cgroup (see at bpf_map_kmalloc_node() for example).
364  */
365 static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
366 {
367 	/* We really just want to fail instead of triggering OOM killer
368 	 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
369 	 * which is used for lower order allocation requests.
370 	 *
371 	 * It has been observed that higher order allocation requests done by
372 	 * vmalloc with __GFP_NORETRY being set might fail due to not trying
373 	 * to reclaim memory from the page cache, thus we set
374 	 * __GFP_RETRY_MAYFAIL to avoid such situations.
375 	 */
376 
377 	gfp_t gfp = bpf_memcg_flags(__GFP_NOWARN | __GFP_ZERO);
378 	unsigned int flags = 0;
379 	unsigned long align = 1;
380 	void *area;
381 
382 	if (size >= SIZE_MAX)
383 		return NULL;
384 
385 	/* kmalloc()'ed memory can't be mmap()'ed */
386 	if (mmapable) {
387 		BUG_ON(!PAGE_ALIGNED(size));
388 		align = SHMLBA;
389 		flags = VM_USERMAP;
390 	} else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
391 		area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
392 				    numa_node);
393 		if (area != NULL)
394 			return area;
395 	}
396 
397 	return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
398 			gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
399 			flags, numa_node, __builtin_return_address(0));
400 }
401 
402 void *bpf_map_area_alloc(u64 size, int numa_node)
403 {
404 	return __bpf_map_area_alloc(size, numa_node, false);
405 }
406 
407 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
408 {
409 	return __bpf_map_area_alloc(size, numa_node, true);
410 }
411 
412 void bpf_map_area_free(void *area)
413 {
414 	kvfree(area);
415 }
416 
417 static u32 bpf_map_flags_retain_permanent(u32 flags)
418 {
419 	/* Some map creation flags are not tied to the map object but
420 	 * rather to the map fd instead, so they have no meaning upon
421 	 * map object inspection since multiple file descriptors with
422 	 * different (access) properties can exist here. Thus, given
423 	 * this has zero meaning for the map itself, lets clear these
424 	 * from here.
425 	 */
426 	return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
427 }
428 
429 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
430 {
431 	map->map_type = attr->map_type;
432 	map->key_size = attr->key_size;
433 	map->value_size = attr->value_size;
434 	map->max_entries = attr->max_entries;
435 	map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
436 	map->numa_node = bpf_map_attr_numa_node(attr);
437 	map->map_extra = attr->map_extra;
438 }
439 
440 static int bpf_map_alloc_id(struct bpf_map *map)
441 {
442 	int id;
443 
444 	idr_preload(GFP_KERNEL);
445 	spin_lock_bh(&map_idr_lock);
446 	id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
447 	if (id > 0)
448 		map->id = id;
449 	spin_unlock_bh(&map_idr_lock);
450 	idr_preload_end();
451 
452 	if (WARN_ON_ONCE(!id))
453 		return -ENOSPC;
454 
455 	return id > 0 ? 0 : id;
456 }
457 
458 void bpf_map_free_id(struct bpf_map *map)
459 {
460 	unsigned long flags;
461 
462 	/* Offloaded maps are removed from the IDR store when their device
463 	 * disappears - even if someone holds an fd to them they are unusable,
464 	 * the memory is gone, all ops will fail; they are simply waiting for
465 	 * refcnt to drop to be freed.
466 	 */
467 	if (!map->id)
468 		return;
469 
470 	spin_lock_irqsave(&map_idr_lock, flags);
471 
472 	idr_remove(&map_idr, map->id);
473 	map->id = 0;
474 
475 	spin_unlock_irqrestore(&map_idr_lock, flags);
476 }
477 
478 #ifdef CONFIG_MEMCG
479 static void bpf_map_save_memcg(struct bpf_map *map)
480 {
481 	/* Currently if a map is created by a process belonging to the root
482 	 * memory cgroup, get_obj_cgroup_from_current() will return NULL.
483 	 * So we have to check map->objcg for being NULL each time it's
484 	 * being used.
485 	 */
486 	if (memcg_bpf_enabled())
487 		map->objcg = get_obj_cgroup_from_current();
488 }
489 
490 static void bpf_map_release_memcg(struct bpf_map *map)
491 {
492 	if (map->objcg)
493 		obj_cgroup_put(map->objcg);
494 }
495 
496 static struct mem_cgroup *bpf_map_get_memcg(const struct bpf_map *map)
497 {
498 	if (map->objcg)
499 		return get_mem_cgroup_from_objcg(map->objcg);
500 
501 	return root_mem_cgroup;
502 }
503 
504 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
505 			   int node)
506 {
507 	struct mem_cgroup *memcg, *old_memcg;
508 	void *ptr;
509 
510 	memcg = bpf_map_get_memcg(map);
511 	old_memcg = set_active_memcg(memcg);
512 	ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
513 	set_active_memcg(old_memcg);
514 	mem_cgroup_put(memcg);
515 
516 	return ptr;
517 }
518 
519 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
520 {
521 	struct mem_cgroup *memcg, *old_memcg;
522 	void *ptr;
523 
524 	memcg = bpf_map_get_memcg(map);
525 	old_memcg = set_active_memcg(memcg);
526 	ptr = kzalloc(size, flags | __GFP_ACCOUNT);
527 	set_active_memcg(old_memcg);
528 	mem_cgroup_put(memcg);
529 
530 	return ptr;
531 }
532 
533 void *bpf_map_kvcalloc(struct bpf_map *map, size_t n, size_t size,
534 		       gfp_t flags)
535 {
536 	struct mem_cgroup *memcg, *old_memcg;
537 	void *ptr;
538 
539 	memcg = bpf_map_get_memcg(map);
540 	old_memcg = set_active_memcg(memcg);
541 	ptr = kvcalloc(n, size, flags | __GFP_ACCOUNT);
542 	set_active_memcg(old_memcg);
543 	mem_cgroup_put(memcg);
544 
545 	return ptr;
546 }
547 
548 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
549 				    size_t align, gfp_t flags)
550 {
551 	struct mem_cgroup *memcg, *old_memcg;
552 	void __percpu *ptr;
553 
554 	memcg = bpf_map_get_memcg(map);
555 	old_memcg = set_active_memcg(memcg);
556 	ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
557 	set_active_memcg(old_memcg);
558 	mem_cgroup_put(memcg);
559 
560 	return ptr;
561 }
562 
563 #else
564 static void bpf_map_save_memcg(struct bpf_map *map)
565 {
566 }
567 
568 static void bpf_map_release_memcg(struct bpf_map *map)
569 {
570 }
571 #endif
572 
573 static bool can_alloc_pages(void)
574 {
575 	return preempt_count() == 0 && !irqs_disabled() &&
576 		!IS_ENABLED(CONFIG_PREEMPT_RT);
577 }
578 
579 static struct page *__bpf_alloc_page(int nid)
580 {
581 	if (!can_alloc_pages())
582 		return alloc_pages_nolock(nid, 0);
583 
584 	return alloc_pages_node(nid,
585 				GFP_KERNEL | __GFP_ZERO | __GFP_ACCOUNT
586 				| __GFP_NOWARN,
587 				0);
588 }
589 
590 int bpf_map_alloc_pages(const struct bpf_map *map, int nid,
591 			unsigned long nr_pages, struct page **pages)
592 {
593 	unsigned long i, j;
594 	struct page *pg;
595 	int ret = 0;
596 #ifdef CONFIG_MEMCG
597 	struct mem_cgroup *memcg, *old_memcg;
598 
599 	memcg = bpf_map_get_memcg(map);
600 	old_memcg = set_active_memcg(memcg);
601 #endif
602 	for (i = 0; i < nr_pages; i++) {
603 		pg = __bpf_alloc_page(nid);
604 
605 		if (pg) {
606 			pages[i] = pg;
607 			continue;
608 		}
609 		for (j = 0; j < i; j++)
610 			free_pages_nolock(pages[j], 0);
611 		ret = -ENOMEM;
612 		break;
613 	}
614 
615 #ifdef CONFIG_MEMCG
616 	set_active_memcg(old_memcg);
617 	mem_cgroup_put(memcg);
618 #endif
619 	return ret;
620 }
621 
622 
623 static int btf_field_cmp(const void *a, const void *b)
624 {
625 	const struct btf_field *f1 = a, *f2 = b;
626 
627 	if (f1->offset < f2->offset)
628 		return -1;
629 	else if (f1->offset > f2->offset)
630 		return 1;
631 	return 0;
632 }
633 
634 struct btf_field *btf_record_find(const struct btf_record *rec, u32 offset,
635 				  u32 field_mask)
636 {
637 	struct btf_field *field;
638 
639 	if (IS_ERR_OR_NULL(rec) || !(rec->field_mask & field_mask))
640 		return NULL;
641 	field = bsearch(&offset, rec->fields, rec->cnt, sizeof(rec->fields[0]), btf_field_cmp);
642 	if (!field || !(field->type & field_mask))
643 		return NULL;
644 	return field;
645 }
646 
647 void btf_record_free(struct btf_record *rec)
648 {
649 	int i;
650 
651 	if (IS_ERR_OR_NULL(rec))
652 		return;
653 	for (i = 0; i < rec->cnt; i++) {
654 		switch (rec->fields[i].type) {
655 		case BPF_KPTR_UNREF:
656 		case BPF_KPTR_REF:
657 		case BPF_KPTR_PERCPU:
658 		case BPF_UPTR:
659 			if (rec->fields[i].kptr.module)
660 				module_put(rec->fields[i].kptr.module);
661 			if (btf_is_kernel(rec->fields[i].kptr.btf))
662 				btf_put(rec->fields[i].kptr.btf);
663 			break;
664 		case BPF_LIST_HEAD:
665 		case BPF_LIST_NODE:
666 		case BPF_RB_ROOT:
667 		case BPF_RB_NODE:
668 		case BPF_SPIN_LOCK:
669 		case BPF_RES_SPIN_LOCK:
670 		case BPF_TIMER:
671 		case BPF_REFCOUNT:
672 		case BPF_WORKQUEUE:
673 			/* Nothing to release */
674 			break;
675 		default:
676 			WARN_ON_ONCE(1);
677 			continue;
678 		}
679 	}
680 	kfree(rec);
681 }
682 
683 void bpf_map_free_record(struct bpf_map *map)
684 {
685 	btf_record_free(map->record);
686 	map->record = NULL;
687 }
688 
689 struct btf_record *btf_record_dup(const struct btf_record *rec)
690 {
691 	const struct btf_field *fields;
692 	struct btf_record *new_rec;
693 	int ret, size, i;
694 
695 	if (IS_ERR_OR_NULL(rec))
696 		return NULL;
697 	size = struct_size(rec, fields, rec->cnt);
698 	new_rec = kmemdup(rec, size, GFP_KERNEL | __GFP_NOWARN);
699 	if (!new_rec)
700 		return ERR_PTR(-ENOMEM);
701 	/* Do a deep copy of the btf_record */
702 	fields = rec->fields;
703 	new_rec->cnt = 0;
704 	for (i = 0; i < rec->cnt; i++) {
705 		switch (fields[i].type) {
706 		case BPF_KPTR_UNREF:
707 		case BPF_KPTR_REF:
708 		case BPF_KPTR_PERCPU:
709 		case BPF_UPTR:
710 			if (btf_is_kernel(fields[i].kptr.btf))
711 				btf_get(fields[i].kptr.btf);
712 			if (fields[i].kptr.module && !try_module_get(fields[i].kptr.module)) {
713 				ret = -ENXIO;
714 				goto free;
715 			}
716 			break;
717 		case BPF_LIST_HEAD:
718 		case BPF_LIST_NODE:
719 		case BPF_RB_ROOT:
720 		case BPF_RB_NODE:
721 		case BPF_SPIN_LOCK:
722 		case BPF_RES_SPIN_LOCK:
723 		case BPF_TIMER:
724 		case BPF_REFCOUNT:
725 		case BPF_WORKQUEUE:
726 			/* Nothing to acquire */
727 			break;
728 		default:
729 			ret = -EFAULT;
730 			WARN_ON_ONCE(1);
731 			goto free;
732 		}
733 		new_rec->cnt++;
734 	}
735 	return new_rec;
736 free:
737 	btf_record_free(new_rec);
738 	return ERR_PTR(ret);
739 }
740 
741 bool btf_record_equal(const struct btf_record *rec_a, const struct btf_record *rec_b)
742 {
743 	bool a_has_fields = !IS_ERR_OR_NULL(rec_a), b_has_fields = !IS_ERR_OR_NULL(rec_b);
744 	int size;
745 
746 	if (!a_has_fields && !b_has_fields)
747 		return true;
748 	if (a_has_fields != b_has_fields)
749 		return false;
750 	if (rec_a->cnt != rec_b->cnt)
751 		return false;
752 	size = struct_size(rec_a, fields, rec_a->cnt);
753 	/* btf_parse_fields uses kzalloc to allocate a btf_record, so unused
754 	 * members are zeroed out. So memcmp is safe to do without worrying
755 	 * about padding/unused fields.
756 	 *
757 	 * While spin_lock, timer, and kptr have no relation to map BTF,
758 	 * list_head metadata is specific to map BTF, the btf and value_rec
759 	 * members in particular. btf is the map BTF, while value_rec points to
760 	 * btf_record in that map BTF.
761 	 *
762 	 * So while by default, we don't rely on the map BTF (which the records
763 	 * were parsed from) matching for both records, which is not backwards
764 	 * compatible, in case list_head is part of it, we implicitly rely on
765 	 * that by way of depending on memcmp succeeding for it.
766 	 */
767 	return !memcmp(rec_a, rec_b, size);
768 }
769 
770 void bpf_obj_free_timer(const struct btf_record *rec, void *obj)
771 {
772 	if (WARN_ON_ONCE(!btf_record_has_field(rec, BPF_TIMER)))
773 		return;
774 	bpf_timer_cancel_and_free(obj + rec->timer_off);
775 }
776 
777 void bpf_obj_free_workqueue(const struct btf_record *rec, void *obj)
778 {
779 	if (WARN_ON_ONCE(!btf_record_has_field(rec, BPF_WORKQUEUE)))
780 		return;
781 	bpf_wq_cancel_and_free(obj + rec->wq_off);
782 }
783 
784 void bpf_obj_free_fields(const struct btf_record *rec, void *obj)
785 {
786 	const struct btf_field *fields;
787 	int i;
788 
789 	if (IS_ERR_OR_NULL(rec))
790 		return;
791 	fields = rec->fields;
792 	for (i = 0; i < rec->cnt; i++) {
793 		struct btf_struct_meta *pointee_struct_meta;
794 		const struct btf_field *field = &fields[i];
795 		void *field_ptr = obj + field->offset;
796 		void *xchgd_field;
797 
798 		switch (fields[i].type) {
799 		case BPF_SPIN_LOCK:
800 		case BPF_RES_SPIN_LOCK:
801 			break;
802 		case BPF_TIMER:
803 			bpf_timer_cancel_and_free(field_ptr);
804 			break;
805 		case BPF_WORKQUEUE:
806 			bpf_wq_cancel_and_free(field_ptr);
807 			break;
808 		case BPF_KPTR_UNREF:
809 			WRITE_ONCE(*(u64 *)field_ptr, 0);
810 			break;
811 		case BPF_KPTR_REF:
812 		case BPF_KPTR_PERCPU:
813 			xchgd_field = (void *)xchg((unsigned long *)field_ptr, 0);
814 			if (!xchgd_field)
815 				break;
816 
817 			if (!btf_is_kernel(field->kptr.btf)) {
818 				pointee_struct_meta = btf_find_struct_meta(field->kptr.btf,
819 									   field->kptr.btf_id);
820 				__bpf_obj_drop_impl(xchgd_field, pointee_struct_meta ?
821 								 pointee_struct_meta->record : NULL,
822 								 fields[i].type == BPF_KPTR_PERCPU);
823 			} else {
824 				field->kptr.dtor(xchgd_field);
825 			}
826 			break;
827 		case BPF_UPTR:
828 			/* The caller ensured that no one is using the uptr */
829 			unpin_uptr_kaddr(*(void **)field_ptr);
830 			break;
831 		case BPF_LIST_HEAD:
832 			if (WARN_ON_ONCE(rec->spin_lock_off < 0))
833 				continue;
834 			bpf_list_head_free(field, field_ptr, obj + rec->spin_lock_off);
835 			break;
836 		case BPF_RB_ROOT:
837 			if (WARN_ON_ONCE(rec->spin_lock_off < 0))
838 				continue;
839 			bpf_rb_root_free(field, field_ptr, obj + rec->spin_lock_off);
840 			break;
841 		case BPF_LIST_NODE:
842 		case BPF_RB_NODE:
843 		case BPF_REFCOUNT:
844 			break;
845 		default:
846 			WARN_ON_ONCE(1);
847 			continue;
848 		}
849 	}
850 }
851 
852 static void bpf_map_free(struct bpf_map *map)
853 {
854 	struct btf_record *rec = map->record;
855 	struct btf *btf = map->btf;
856 
857 	/* implementation dependent freeing. Disabling migration to simplify
858 	 * the free of values or special fields allocated from bpf memory
859 	 * allocator.
860 	 */
861 	migrate_disable();
862 	map->ops->map_free(map);
863 	migrate_enable();
864 
865 	/* Delay freeing of btf_record for maps, as map_free
866 	 * callback usually needs access to them. It is better to do it here
867 	 * than require each callback to do the free itself manually.
868 	 *
869 	 * Note that the btf_record stashed in map->inner_map_meta->record was
870 	 * already freed using the map_free callback for map in map case which
871 	 * eventually calls bpf_map_free_meta, since inner_map_meta is only a
872 	 * template bpf_map struct used during verification.
873 	 */
874 	btf_record_free(rec);
875 	/* Delay freeing of btf for maps, as map_free callback may need
876 	 * struct_meta info which will be freed with btf_put().
877 	 */
878 	btf_put(btf);
879 }
880 
881 /* called from workqueue */
882 static void bpf_map_free_deferred(struct work_struct *work)
883 {
884 	struct bpf_map *map = container_of(work, struct bpf_map, work);
885 
886 	security_bpf_map_free(map);
887 	bpf_map_release_memcg(map);
888 	bpf_map_free(map);
889 }
890 
891 static void bpf_map_put_uref(struct bpf_map *map)
892 {
893 	if (atomic64_dec_and_test(&map->usercnt)) {
894 		if (map->ops->map_release_uref)
895 			map->ops->map_release_uref(map);
896 	}
897 }
898 
899 static void bpf_map_free_in_work(struct bpf_map *map)
900 {
901 	INIT_WORK(&map->work, bpf_map_free_deferred);
902 	/* Avoid spawning kworkers, since they all might contend
903 	 * for the same mutex like slab_mutex.
904 	 */
905 	queue_work(system_unbound_wq, &map->work);
906 }
907 
908 static void bpf_map_free_rcu_gp(struct rcu_head *rcu)
909 {
910 	bpf_map_free_in_work(container_of(rcu, struct bpf_map, rcu));
911 }
912 
913 static void bpf_map_free_mult_rcu_gp(struct rcu_head *rcu)
914 {
915 	if (rcu_trace_implies_rcu_gp())
916 		bpf_map_free_rcu_gp(rcu);
917 	else
918 		call_rcu(rcu, bpf_map_free_rcu_gp);
919 }
920 
921 /* decrement map refcnt and schedule it for freeing via workqueue
922  * (underlying map implementation ops->map_free() might sleep)
923  */
924 void bpf_map_put(struct bpf_map *map)
925 {
926 	if (atomic64_dec_and_test(&map->refcnt)) {
927 		/* bpf_map_free_id() must be called first */
928 		bpf_map_free_id(map);
929 
930 		WARN_ON_ONCE(atomic64_read(&map->sleepable_refcnt));
931 		if (READ_ONCE(map->free_after_mult_rcu_gp))
932 			call_rcu_tasks_trace(&map->rcu, bpf_map_free_mult_rcu_gp);
933 		else if (READ_ONCE(map->free_after_rcu_gp))
934 			call_rcu(&map->rcu, bpf_map_free_rcu_gp);
935 		else
936 			bpf_map_free_in_work(map);
937 	}
938 }
939 EXPORT_SYMBOL_GPL(bpf_map_put);
940 
941 void bpf_map_put_with_uref(struct bpf_map *map)
942 {
943 	bpf_map_put_uref(map);
944 	bpf_map_put(map);
945 }
946 
947 static int bpf_map_release(struct inode *inode, struct file *filp)
948 {
949 	struct bpf_map *map = filp->private_data;
950 
951 	if (map->ops->map_release)
952 		map->ops->map_release(map, filp);
953 
954 	bpf_map_put_with_uref(map);
955 	return 0;
956 }
957 
958 static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
959 {
960 	fmode_t mode = fd_file(f)->f_mode;
961 
962 	/* Our file permissions may have been overridden by global
963 	 * map permissions facing syscall side.
964 	 */
965 	if (READ_ONCE(map->frozen))
966 		mode &= ~FMODE_CAN_WRITE;
967 	return mode;
968 }
969 
970 #ifdef CONFIG_PROC_FS
971 /* Show the memory usage of a bpf map */
972 static u64 bpf_map_memory_usage(const struct bpf_map *map)
973 {
974 	return map->ops->map_mem_usage(map);
975 }
976 
977 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
978 {
979 	struct bpf_map *map = filp->private_data;
980 	u32 type = 0, jited = 0;
981 
982 	if (map_type_contains_progs(map)) {
983 		spin_lock(&map->owner.lock);
984 		type  = map->owner.type;
985 		jited = map->owner.jited;
986 		spin_unlock(&map->owner.lock);
987 	}
988 
989 	seq_printf(m,
990 		   "map_type:\t%u\n"
991 		   "key_size:\t%u\n"
992 		   "value_size:\t%u\n"
993 		   "max_entries:\t%u\n"
994 		   "map_flags:\t%#x\n"
995 		   "map_extra:\t%#llx\n"
996 		   "memlock:\t%llu\n"
997 		   "map_id:\t%u\n"
998 		   "frozen:\t%u\n",
999 		   map->map_type,
1000 		   map->key_size,
1001 		   map->value_size,
1002 		   map->max_entries,
1003 		   map->map_flags,
1004 		   (unsigned long long)map->map_extra,
1005 		   bpf_map_memory_usage(map),
1006 		   map->id,
1007 		   READ_ONCE(map->frozen));
1008 	if (type) {
1009 		seq_printf(m, "owner_prog_type:\t%u\n", type);
1010 		seq_printf(m, "owner_jited:\t%u\n", jited);
1011 	}
1012 }
1013 #endif
1014 
1015 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
1016 			      loff_t *ppos)
1017 {
1018 	/* We need this handler such that alloc_file() enables
1019 	 * f_mode with FMODE_CAN_READ.
1020 	 */
1021 	return -EINVAL;
1022 }
1023 
1024 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
1025 			       size_t siz, loff_t *ppos)
1026 {
1027 	/* We need this handler such that alloc_file() enables
1028 	 * f_mode with FMODE_CAN_WRITE.
1029 	 */
1030 	return -EINVAL;
1031 }
1032 
1033 /* called for any extra memory-mapped regions (except initial) */
1034 static void bpf_map_mmap_open(struct vm_area_struct *vma)
1035 {
1036 	struct bpf_map *map = vma->vm_file->private_data;
1037 
1038 	if (vma->vm_flags & VM_MAYWRITE)
1039 		bpf_map_write_active_inc(map);
1040 }
1041 
1042 /* called for all unmapped memory region (including initial) */
1043 static void bpf_map_mmap_close(struct vm_area_struct *vma)
1044 {
1045 	struct bpf_map *map = vma->vm_file->private_data;
1046 
1047 	if (vma->vm_flags & VM_MAYWRITE)
1048 		bpf_map_write_active_dec(map);
1049 }
1050 
1051 static const struct vm_operations_struct bpf_map_default_vmops = {
1052 	.open		= bpf_map_mmap_open,
1053 	.close		= bpf_map_mmap_close,
1054 };
1055 
1056 static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
1057 {
1058 	struct bpf_map *map = filp->private_data;
1059 	int err = 0;
1060 
1061 	if (!map->ops->map_mmap || !IS_ERR_OR_NULL(map->record))
1062 		return -ENOTSUPP;
1063 
1064 	if (!(vma->vm_flags & VM_SHARED))
1065 		return -EINVAL;
1066 
1067 	mutex_lock(&map->freeze_mutex);
1068 
1069 	if (vma->vm_flags & VM_WRITE) {
1070 		if (map->frozen) {
1071 			err = -EPERM;
1072 			goto out;
1073 		}
1074 		/* map is meant to be read-only, so do not allow mapping as
1075 		 * writable, because it's possible to leak a writable page
1076 		 * reference and allows user-space to still modify it after
1077 		 * freezing, while verifier will assume contents do not change
1078 		 */
1079 		if (map->map_flags & BPF_F_RDONLY_PROG) {
1080 			err = -EACCES;
1081 			goto out;
1082 		}
1083 		bpf_map_write_active_inc(map);
1084 	}
1085 out:
1086 	mutex_unlock(&map->freeze_mutex);
1087 	if (err)
1088 		return err;
1089 
1090 	/* set default open/close callbacks */
1091 	vma->vm_ops = &bpf_map_default_vmops;
1092 	vma->vm_private_data = map;
1093 	vm_flags_clear(vma, VM_MAYEXEC);
1094 	/* If mapping is read-only, then disallow potentially re-mapping with
1095 	 * PROT_WRITE by dropping VM_MAYWRITE flag. This VM_MAYWRITE clearing
1096 	 * means that as far as BPF map's memory-mapped VMAs are concerned,
1097 	 * VM_WRITE and VM_MAYWRITE and equivalent, if one of them is set,
1098 	 * both should be set, so we can forget about VM_MAYWRITE and always
1099 	 * check just VM_WRITE
1100 	 */
1101 	if (!(vma->vm_flags & VM_WRITE))
1102 		vm_flags_clear(vma, VM_MAYWRITE);
1103 
1104 	err = map->ops->map_mmap(map, vma);
1105 	if (err) {
1106 		if (vma->vm_flags & VM_WRITE)
1107 			bpf_map_write_active_dec(map);
1108 	}
1109 
1110 	return err;
1111 }
1112 
1113 static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
1114 {
1115 	struct bpf_map *map = filp->private_data;
1116 
1117 	if (map->ops->map_poll)
1118 		return map->ops->map_poll(map, filp, pts);
1119 
1120 	return EPOLLERR;
1121 }
1122 
1123 static unsigned long bpf_get_unmapped_area(struct file *filp, unsigned long addr,
1124 					   unsigned long len, unsigned long pgoff,
1125 					   unsigned long flags)
1126 {
1127 	struct bpf_map *map = filp->private_data;
1128 
1129 	if (map->ops->map_get_unmapped_area)
1130 		return map->ops->map_get_unmapped_area(filp, addr, len, pgoff, flags);
1131 #ifdef CONFIG_MMU
1132 	return mm_get_unmapped_area(current->mm, filp, addr, len, pgoff, flags);
1133 #else
1134 	return addr;
1135 #endif
1136 }
1137 
1138 const struct file_operations bpf_map_fops = {
1139 #ifdef CONFIG_PROC_FS
1140 	.show_fdinfo	= bpf_map_show_fdinfo,
1141 #endif
1142 	.release	= bpf_map_release,
1143 	.read		= bpf_dummy_read,
1144 	.write		= bpf_dummy_write,
1145 	.mmap		= bpf_map_mmap,
1146 	.poll		= bpf_map_poll,
1147 	.get_unmapped_area = bpf_get_unmapped_area,
1148 };
1149 
1150 int bpf_map_new_fd(struct bpf_map *map, int flags)
1151 {
1152 	int ret;
1153 
1154 	ret = security_bpf_map(map, OPEN_FMODE(flags));
1155 	if (ret < 0)
1156 		return ret;
1157 
1158 	return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
1159 				flags | O_CLOEXEC);
1160 }
1161 
1162 int bpf_get_file_flag(int flags)
1163 {
1164 	if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
1165 		return -EINVAL;
1166 	if (flags & BPF_F_RDONLY)
1167 		return O_RDONLY;
1168 	if (flags & BPF_F_WRONLY)
1169 		return O_WRONLY;
1170 	return O_RDWR;
1171 }
1172 
1173 /* helper macro to check that unused fields 'union bpf_attr' are zero */
1174 #define CHECK_ATTR(CMD) \
1175 	memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
1176 		   sizeof(attr->CMD##_LAST_FIELD), 0, \
1177 		   sizeof(*attr) - \
1178 		   offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
1179 		   sizeof(attr->CMD##_LAST_FIELD)) != NULL
1180 
1181 /* dst and src must have at least "size" number of bytes.
1182  * Return strlen on success and < 0 on error.
1183  */
1184 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
1185 {
1186 	const char *end = src + size;
1187 	const char *orig_src = src;
1188 
1189 	memset(dst, 0, size);
1190 	/* Copy all isalnum(), '_' and '.' chars. */
1191 	while (src < end && *src) {
1192 		if (!isalnum(*src) &&
1193 		    *src != '_' && *src != '.')
1194 			return -EINVAL;
1195 		*dst++ = *src++;
1196 	}
1197 
1198 	/* No '\0' found in "size" number of bytes */
1199 	if (src == end)
1200 		return -EINVAL;
1201 
1202 	return src - orig_src;
1203 }
1204 
1205 int map_check_no_btf(const struct bpf_map *map,
1206 		     const struct btf *btf,
1207 		     const struct btf_type *key_type,
1208 		     const struct btf_type *value_type)
1209 {
1210 	return -ENOTSUPP;
1211 }
1212 
1213 static int map_check_btf(struct bpf_map *map, struct bpf_token *token,
1214 			 const struct btf *btf, u32 btf_key_id, u32 btf_value_id)
1215 {
1216 	const struct btf_type *key_type, *value_type;
1217 	u32 key_size, value_size;
1218 	int ret = 0;
1219 
1220 	/* Some maps allow key to be unspecified. */
1221 	if (btf_key_id) {
1222 		key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
1223 		if (!key_type || key_size != map->key_size)
1224 			return -EINVAL;
1225 	} else {
1226 		key_type = btf_type_by_id(btf, 0);
1227 		if (!map->ops->map_check_btf)
1228 			return -EINVAL;
1229 	}
1230 
1231 	value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
1232 	if (!value_type || value_size != map->value_size)
1233 		return -EINVAL;
1234 
1235 	map->record = btf_parse_fields(btf, value_type,
1236 				       BPF_SPIN_LOCK | BPF_RES_SPIN_LOCK | BPF_TIMER | BPF_KPTR | BPF_LIST_HEAD |
1237 				       BPF_RB_ROOT | BPF_REFCOUNT | BPF_WORKQUEUE | BPF_UPTR,
1238 				       map->value_size);
1239 	if (!IS_ERR_OR_NULL(map->record)) {
1240 		int i;
1241 
1242 		if (!bpf_token_capable(token, CAP_BPF)) {
1243 			ret = -EPERM;
1244 			goto free_map_tab;
1245 		}
1246 		if (map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) {
1247 			ret = -EACCES;
1248 			goto free_map_tab;
1249 		}
1250 		for (i = 0; i < sizeof(map->record->field_mask) * 8; i++) {
1251 			switch (map->record->field_mask & (1 << i)) {
1252 			case 0:
1253 				continue;
1254 			case BPF_SPIN_LOCK:
1255 			case BPF_RES_SPIN_LOCK:
1256 				if (map->map_type != BPF_MAP_TYPE_HASH &&
1257 				    map->map_type != BPF_MAP_TYPE_ARRAY &&
1258 				    map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
1259 				    map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
1260 				    map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
1261 				    map->map_type != BPF_MAP_TYPE_TASK_STORAGE &&
1262 				    map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) {
1263 					ret = -EOPNOTSUPP;
1264 					goto free_map_tab;
1265 				}
1266 				break;
1267 			case BPF_TIMER:
1268 			case BPF_WORKQUEUE:
1269 				if (map->map_type != BPF_MAP_TYPE_HASH &&
1270 				    map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1271 				    map->map_type != BPF_MAP_TYPE_ARRAY) {
1272 					ret = -EOPNOTSUPP;
1273 					goto free_map_tab;
1274 				}
1275 				break;
1276 			case BPF_KPTR_UNREF:
1277 			case BPF_KPTR_REF:
1278 			case BPF_KPTR_PERCPU:
1279 			case BPF_REFCOUNT:
1280 				if (map->map_type != BPF_MAP_TYPE_HASH &&
1281 				    map->map_type != BPF_MAP_TYPE_PERCPU_HASH &&
1282 				    map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1283 				    map->map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH &&
1284 				    map->map_type != BPF_MAP_TYPE_ARRAY &&
1285 				    map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY &&
1286 				    map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
1287 				    map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
1288 				    map->map_type != BPF_MAP_TYPE_TASK_STORAGE &&
1289 				    map->map_type != BPF_MAP_TYPE_CGRP_STORAGE) {
1290 					ret = -EOPNOTSUPP;
1291 					goto free_map_tab;
1292 				}
1293 				break;
1294 			case BPF_UPTR:
1295 				if (map->map_type != BPF_MAP_TYPE_TASK_STORAGE) {
1296 					ret = -EOPNOTSUPP;
1297 					goto free_map_tab;
1298 				}
1299 				break;
1300 			case BPF_LIST_HEAD:
1301 			case BPF_RB_ROOT:
1302 				if (map->map_type != BPF_MAP_TYPE_HASH &&
1303 				    map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1304 				    map->map_type != BPF_MAP_TYPE_ARRAY) {
1305 					ret = -EOPNOTSUPP;
1306 					goto free_map_tab;
1307 				}
1308 				break;
1309 			default:
1310 				/* Fail if map_type checks are missing for a field type */
1311 				ret = -EOPNOTSUPP;
1312 				goto free_map_tab;
1313 			}
1314 		}
1315 	}
1316 
1317 	ret = btf_check_and_fixup_fields(btf, map->record);
1318 	if (ret < 0)
1319 		goto free_map_tab;
1320 
1321 	if (map->ops->map_check_btf) {
1322 		ret = map->ops->map_check_btf(map, btf, key_type, value_type);
1323 		if (ret < 0)
1324 			goto free_map_tab;
1325 	}
1326 
1327 	return ret;
1328 free_map_tab:
1329 	bpf_map_free_record(map);
1330 	return ret;
1331 }
1332 
1333 static bool bpf_net_capable(void)
1334 {
1335 	return capable(CAP_NET_ADMIN) || capable(CAP_SYS_ADMIN);
1336 }
1337 
1338 #define BPF_MAP_CREATE_LAST_FIELD map_token_fd
1339 /* called via syscall */
1340 static int map_create(union bpf_attr *attr, bool kernel)
1341 {
1342 	const struct bpf_map_ops *ops;
1343 	struct bpf_token *token = NULL;
1344 	int numa_node = bpf_map_attr_numa_node(attr);
1345 	u32 map_type = attr->map_type;
1346 	struct bpf_map *map;
1347 	bool token_flag;
1348 	int f_flags;
1349 	int err;
1350 
1351 	err = CHECK_ATTR(BPF_MAP_CREATE);
1352 	if (err)
1353 		return -EINVAL;
1354 
1355 	/* check BPF_F_TOKEN_FD flag, remember if it's set, and then clear it
1356 	 * to avoid per-map type checks tripping on unknown flag
1357 	 */
1358 	token_flag = attr->map_flags & BPF_F_TOKEN_FD;
1359 	attr->map_flags &= ~BPF_F_TOKEN_FD;
1360 
1361 	if (attr->btf_vmlinux_value_type_id) {
1362 		if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
1363 		    attr->btf_key_type_id || attr->btf_value_type_id)
1364 			return -EINVAL;
1365 	} else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
1366 		return -EINVAL;
1367 	}
1368 
1369 	if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER &&
1370 	    attr->map_type != BPF_MAP_TYPE_ARENA &&
1371 	    attr->map_extra != 0)
1372 		return -EINVAL;
1373 
1374 	f_flags = bpf_get_file_flag(attr->map_flags);
1375 	if (f_flags < 0)
1376 		return f_flags;
1377 
1378 	if (numa_node != NUMA_NO_NODE &&
1379 	    ((unsigned int)numa_node >= nr_node_ids ||
1380 	     !node_online(numa_node)))
1381 		return -EINVAL;
1382 
1383 	/* find map type and init map: hashtable vs rbtree vs bloom vs ... */
1384 	map_type = attr->map_type;
1385 	if (map_type >= ARRAY_SIZE(bpf_map_types))
1386 		return -EINVAL;
1387 	map_type = array_index_nospec(map_type, ARRAY_SIZE(bpf_map_types));
1388 	ops = bpf_map_types[map_type];
1389 	if (!ops)
1390 		return -EINVAL;
1391 
1392 	if (ops->map_alloc_check) {
1393 		err = ops->map_alloc_check(attr);
1394 		if (err)
1395 			return err;
1396 	}
1397 	if (attr->map_ifindex)
1398 		ops = &bpf_map_offload_ops;
1399 	if (!ops->map_mem_usage)
1400 		return -EINVAL;
1401 
1402 	if (token_flag) {
1403 		token = bpf_token_get_from_fd(attr->map_token_fd);
1404 		if (IS_ERR(token))
1405 			return PTR_ERR(token);
1406 
1407 		/* if current token doesn't grant map creation permissions,
1408 		 * then we can't use this token, so ignore it and rely on
1409 		 * system-wide capabilities checks
1410 		 */
1411 		if (!bpf_token_allow_cmd(token, BPF_MAP_CREATE) ||
1412 		    !bpf_token_allow_map_type(token, attr->map_type)) {
1413 			bpf_token_put(token);
1414 			token = NULL;
1415 		}
1416 	}
1417 
1418 	err = -EPERM;
1419 
1420 	/* Intent here is for unprivileged_bpf_disabled to block BPF map
1421 	 * creation for unprivileged users; other actions depend
1422 	 * on fd availability and access to bpffs, so are dependent on
1423 	 * object creation success. Even with unprivileged BPF disabled,
1424 	 * capability checks are still carried out.
1425 	 */
1426 	if (sysctl_unprivileged_bpf_disabled && !bpf_token_capable(token, CAP_BPF))
1427 		goto put_token;
1428 
1429 	/* check privileged map type permissions */
1430 	switch (map_type) {
1431 	case BPF_MAP_TYPE_ARRAY:
1432 	case BPF_MAP_TYPE_PERCPU_ARRAY:
1433 	case BPF_MAP_TYPE_PROG_ARRAY:
1434 	case BPF_MAP_TYPE_PERF_EVENT_ARRAY:
1435 	case BPF_MAP_TYPE_CGROUP_ARRAY:
1436 	case BPF_MAP_TYPE_ARRAY_OF_MAPS:
1437 	case BPF_MAP_TYPE_HASH:
1438 	case BPF_MAP_TYPE_PERCPU_HASH:
1439 	case BPF_MAP_TYPE_HASH_OF_MAPS:
1440 	case BPF_MAP_TYPE_RINGBUF:
1441 	case BPF_MAP_TYPE_USER_RINGBUF:
1442 	case BPF_MAP_TYPE_CGROUP_STORAGE:
1443 	case BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE:
1444 		/* unprivileged */
1445 		break;
1446 	case BPF_MAP_TYPE_SK_STORAGE:
1447 	case BPF_MAP_TYPE_INODE_STORAGE:
1448 	case BPF_MAP_TYPE_TASK_STORAGE:
1449 	case BPF_MAP_TYPE_CGRP_STORAGE:
1450 	case BPF_MAP_TYPE_BLOOM_FILTER:
1451 	case BPF_MAP_TYPE_LPM_TRIE:
1452 	case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY:
1453 	case BPF_MAP_TYPE_STACK_TRACE:
1454 	case BPF_MAP_TYPE_QUEUE:
1455 	case BPF_MAP_TYPE_STACK:
1456 	case BPF_MAP_TYPE_LRU_HASH:
1457 	case BPF_MAP_TYPE_LRU_PERCPU_HASH:
1458 	case BPF_MAP_TYPE_STRUCT_OPS:
1459 	case BPF_MAP_TYPE_CPUMAP:
1460 	case BPF_MAP_TYPE_ARENA:
1461 		if (!bpf_token_capable(token, CAP_BPF))
1462 			goto put_token;
1463 		break;
1464 	case BPF_MAP_TYPE_SOCKMAP:
1465 	case BPF_MAP_TYPE_SOCKHASH:
1466 	case BPF_MAP_TYPE_DEVMAP:
1467 	case BPF_MAP_TYPE_DEVMAP_HASH:
1468 	case BPF_MAP_TYPE_XSKMAP:
1469 		if (!bpf_token_capable(token, CAP_NET_ADMIN))
1470 			goto put_token;
1471 		break;
1472 	default:
1473 		WARN(1, "unsupported map type %d", map_type);
1474 		goto put_token;
1475 	}
1476 
1477 	map = ops->map_alloc(attr);
1478 	if (IS_ERR(map)) {
1479 		err = PTR_ERR(map);
1480 		goto put_token;
1481 	}
1482 	map->ops = ops;
1483 	map->map_type = map_type;
1484 
1485 	err = bpf_obj_name_cpy(map->name, attr->map_name,
1486 			       sizeof(attr->map_name));
1487 	if (err < 0)
1488 		goto free_map;
1489 
1490 	atomic64_set(&map->refcnt, 1);
1491 	atomic64_set(&map->usercnt, 1);
1492 	mutex_init(&map->freeze_mutex);
1493 	spin_lock_init(&map->owner.lock);
1494 
1495 	if (attr->btf_key_type_id || attr->btf_value_type_id ||
1496 	    /* Even the map's value is a kernel's struct,
1497 	     * the bpf_prog.o must have BTF to begin with
1498 	     * to figure out the corresponding kernel's
1499 	     * counter part.  Thus, attr->btf_fd has
1500 	     * to be valid also.
1501 	     */
1502 	    attr->btf_vmlinux_value_type_id) {
1503 		struct btf *btf;
1504 
1505 		btf = btf_get_by_fd(attr->btf_fd);
1506 		if (IS_ERR(btf)) {
1507 			err = PTR_ERR(btf);
1508 			goto free_map;
1509 		}
1510 		if (btf_is_kernel(btf)) {
1511 			btf_put(btf);
1512 			err = -EACCES;
1513 			goto free_map;
1514 		}
1515 		map->btf = btf;
1516 
1517 		if (attr->btf_value_type_id) {
1518 			err = map_check_btf(map, token, btf, attr->btf_key_type_id,
1519 					    attr->btf_value_type_id);
1520 			if (err)
1521 				goto free_map;
1522 		}
1523 
1524 		map->btf_key_type_id = attr->btf_key_type_id;
1525 		map->btf_value_type_id = attr->btf_value_type_id;
1526 		map->btf_vmlinux_value_type_id =
1527 			attr->btf_vmlinux_value_type_id;
1528 	}
1529 
1530 	err = security_bpf_map_create(map, attr, token, kernel);
1531 	if (err)
1532 		goto free_map_sec;
1533 
1534 	err = bpf_map_alloc_id(map);
1535 	if (err)
1536 		goto free_map_sec;
1537 
1538 	bpf_map_save_memcg(map);
1539 	bpf_token_put(token);
1540 
1541 	err = bpf_map_new_fd(map, f_flags);
1542 	if (err < 0) {
1543 		/* failed to allocate fd.
1544 		 * bpf_map_put_with_uref() is needed because the above
1545 		 * bpf_map_alloc_id() has published the map
1546 		 * to the userspace and the userspace may
1547 		 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
1548 		 */
1549 		bpf_map_put_with_uref(map);
1550 		return err;
1551 	}
1552 
1553 	return err;
1554 
1555 free_map_sec:
1556 	security_bpf_map_free(map);
1557 free_map:
1558 	bpf_map_free(map);
1559 put_token:
1560 	bpf_token_put(token);
1561 	return err;
1562 }
1563 
1564 void bpf_map_inc(struct bpf_map *map)
1565 {
1566 	atomic64_inc(&map->refcnt);
1567 }
1568 EXPORT_SYMBOL_GPL(bpf_map_inc);
1569 
1570 void bpf_map_inc_with_uref(struct bpf_map *map)
1571 {
1572 	atomic64_inc(&map->refcnt);
1573 	atomic64_inc(&map->usercnt);
1574 }
1575 EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
1576 
1577 struct bpf_map *bpf_map_get(u32 ufd)
1578 {
1579 	CLASS(fd, f)(ufd);
1580 	struct bpf_map *map = __bpf_map_get(f);
1581 
1582 	if (!IS_ERR(map))
1583 		bpf_map_inc(map);
1584 
1585 	return map;
1586 }
1587 EXPORT_SYMBOL_NS(bpf_map_get, "BPF_INTERNAL");
1588 
1589 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
1590 {
1591 	CLASS(fd, f)(ufd);
1592 	struct bpf_map *map = __bpf_map_get(f);
1593 
1594 	if (!IS_ERR(map))
1595 		bpf_map_inc_with_uref(map);
1596 
1597 	return map;
1598 }
1599 
1600 /* map_idr_lock should have been held or the map should have been
1601  * protected by rcu read lock.
1602  */
1603 struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
1604 {
1605 	int refold;
1606 
1607 	refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
1608 	if (!refold)
1609 		return ERR_PTR(-ENOENT);
1610 	if (uref)
1611 		atomic64_inc(&map->usercnt);
1612 
1613 	return map;
1614 }
1615 
1616 struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
1617 {
1618 	lockdep_assert(rcu_read_lock_held());
1619 	return __bpf_map_inc_not_zero(map, false);
1620 }
1621 EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
1622 
1623 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1624 {
1625 	return -ENOTSUPP;
1626 }
1627 
1628 static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1629 {
1630 	if (key_size)
1631 		return vmemdup_user(ukey, key_size);
1632 
1633 	if (ukey)
1634 		return ERR_PTR(-EINVAL);
1635 
1636 	return NULL;
1637 }
1638 
1639 static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1640 {
1641 	if (key_size)
1642 		return kvmemdup_bpfptr(ukey, key_size);
1643 
1644 	if (!bpfptr_is_null(ukey))
1645 		return ERR_PTR(-EINVAL);
1646 
1647 	return NULL;
1648 }
1649 
1650 /* last field in 'union bpf_attr' used by this command */
1651 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1652 
1653 static int map_lookup_elem(union bpf_attr *attr)
1654 {
1655 	void __user *ukey = u64_to_user_ptr(attr->key);
1656 	void __user *uvalue = u64_to_user_ptr(attr->value);
1657 	struct bpf_map *map;
1658 	void *key, *value;
1659 	u32 value_size;
1660 	int err;
1661 
1662 	if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1663 		return -EINVAL;
1664 
1665 	if (attr->flags & ~BPF_F_LOCK)
1666 		return -EINVAL;
1667 
1668 	CLASS(fd, f)(attr->map_fd);
1669 	map = __bpf_map_get(f);
1670 	if (IS_ERR(map))
1671 		return PTR_ERR(map);
1672 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ))
1673 		return -EPERM;
1674 
1675 	if ((attr->flags & BPF_F_LOCK) &&
1676 	    !btf_record_has_field(map->record, BPF_SPIN_LOCK))
1677 		return -EINVAL;
1678 
1679 	key = __bpf_copy_key(ukey, map->key_size);
1680 	if (IS_ERR(key))
1681 		return PTR_ERR(key);
1682 
1683 	value_size = bpf_map_value_size(map);
1684 
1685 	err = -ENOMEM;
1686 	value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1687 	if (!value)
1688 		goto free_key;
1689 
1690 	if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
1691 		if (copy_from_user(value, uvalue, value_size))
1692 			err = -EFAULT;
1693 		else
1694 			err = bpf_map_copy_value(map, key, value, attr->flags);
1695 		goto free_value;
1696 	}
1697 
1698 	err = bpf_map_copy_value(map, key, value, attr->flags);
1699 	if (err)
1700 		goto free_value;
1701 
1702 	err = -EFAULT;
1703 	if (copy_to_user(uvalue, value, value_size) != 0)
1704 		goto free_value;
1705 
1706 	err = 0;
1707 
1708 free_value:
1709 	kvfree(value);
1710 free_key:
1711 	kvfree(key);
1712 	return err;
1713 }
1714 
1715 
1716 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1717 
1718 static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
1719 {
1720 	bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1721 	bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
1722 	struct bpf_map *map;
1723 	void *key, *value;
1724 	u32 value_size;
1725 	int err;
1726 
1727 	if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1728 		return -EINVAL;
1729 
1730 	CLASS(fd, f)(attr->map_fd);
1731 	map = __bpf_map_get(f);
1732 	if (IS_ERR(map))
1733 		return PTR_ERR(map);
1734 	bpf_map_write_active_inc(map);
1735 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1736 		err = -EPERM;
1737 		goto err_put;
1738 	}
1739 
1740 	if ((attr->flags & BPF_F_LOCK) &&
1741 	    !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1742 		err = -EINVAL;
1743 		goto err_put;
1744 	}
1745 
1746 	key = ___bpf_copy_key(ukey, map->key_size);
1747 	if (IS_ERR(key)) {
1748 		err = PTR_ERR(key);
1749 		goto err_put;
1750 	}
1751 
1752 	value_size = bpf_map_value_size(map);
1753 	value = kvmemdup_bpfptr(uvalue, value_size);
1754 	if (IS_ERR(value)) {
1755 		err = PTR_ERR(value);
1756 		goto free_key;
1757 	}
1758 
1759 	err = bpf_map_update_value(map, fd_file(f), key, value, attr->flags);
1760 	if (!err)
1761 		maybe_wait_bpf_programs(map);
1762 
1763 	kvfree(value);
1764 free_key:
1765 	kvfree(key);
1766 err_put:
1767 	bpf_map_write_active_dec(map);
1768 	return err;
1769 }
1770 
1771 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1772 
1773 static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr)
1774 {
1775 	bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1776 	struct bpf_map *map;
1777 	void *key;
1778 	int err;
1779 
1780 	if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1781 		return -EINVAL;
1782 
1783 	CLASS(fd, f)(attr->map_fd);
1784 	map = __bpf_map_get(f);
1785 	if (IS_ERR(map))
1786 		return PTR_ERR(map);
1787 	bpf_map_write_active_inc(map);
1788 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1789 		err = -EPERM;
1790 		goto err_put;
1791 	}
1792 
1793 	key = ___bpf_copy_key(ukey, map->key_size);
1794 	if (IS_ERR(key)) {
1795 		err = PTR_ERR(key);
1796 		goto err_put;
1797 	}
1798 
1799 	if (bpf_map_is_offloaded(map)) {
1800 		err = bpf_map_offload_delete_elem(map, key);
1801 		goto out;
1802 	} else if (IS_FD_PROG_ARRAY(map) ||
1803 		   map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1804 		/* These maps require sleepable context */
1805 		err = map->ops->map_delete_elem(map, key);
1806 		goto out;
1807 	}
1808 
1809 	bpf_disable_instrumentation();
1810 	rcu_read_lock();
1811 	err = map->ops->map_delete_elem(map, key);
1812 	rcu_read_unlock();
1813 	bpf_enable_instrumentation();
1814 	if (!err)
1815 		maybe_wait_bpf_programs(map);
1816 out:
1817 	kvfree(key);
1818 err_put:
1819 	bpf_map_write_active_dec(map);
1820 	return err;
1821 }
1822 
1823 /* last field in 'union bpf_attr' used by this command */
1824 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1825 
1826 static int map_get_next_key(union bpf_attr *attr)
1827 {
1828 	void __user *ukey = u64_to_user_ptr(attr->key);
1829 	void __user *unext_key = u64_to_user_ptr(attr->next_key);
1830 	struct bpf_map *map;
1831 	void *key, *next_key;
1832 	int err;
1833 
1834 	if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1835 		return -EINVAL;
1836 
1837 	CLASS(fd, f)(attr->map_fd);
1838 	map = __bpf_map_get(f);
1839 	if (IS_ERR(map))
1840 		return PTR_ERR(map);
1841 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ))
1842 		return -EPERM;
1843 
1844 	if (ukey) {
1845 		key = __bpf_copy_key(ukey, map->key_size);
1846 		if (IS_ERR(key))
1847 			return PTR_ERR(key);
1848 	} else {
1849 		key = NULL;
1850 	}
1851 
1852 	err = -ENOMEM;
1853 	next_key = kvmalloc(map->key_size, GFP_USER);
1854 	if (!next_key)
1855 		goto free_key;
1856 
1857 	if (bpf_map_is_offloaded(map)) {
1858 		err = bpf_map_offload_get_next_key(map, key, next_key);
1859 		goto out;
1860 	}
1861 
1862 	rcu_read_lock();
1863 	err = map->ops->map_get_next_key(map, key, next_key);
1864 	rcu_read_unlock();
1865 out:
1866 	if (err)
1867 		goto free_next_key;
1868 
1869 	err = -EFAULT;
1870 	if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1871 		goto free_next_key;
1872 
1873 	err = 0;
1874 
1875 free_next_key:
1876 	kvfree(next_key);
1877 free_key:
1878 	kvfree(key);
1879 	return err;
1880 }
1881 
1882 int generic_map_delete_batch(struct bpf_map *map,
1883 			     const union bpf_attr *attr,
1884 			     union bpf_attr __user *uattr)
1885 {
1886 	void __user *keys = u64_to_user_ptr(attr->batch.keys);
1887 	u32 cp, max_count;
1888 	int err = 0;
1889 	void *key;
1890 
1891 	if (attr->batch.elem_flags & ~BPF_F_LOCK)
1892 		return -EINVAL;
1893 
1894 	if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1895 	    !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1896 		return -EINVAL;
1897 	}
1898 
1899 	max_count = attr->batch.count;
1900 	if (!max_count)
1901 		return 0;
1902 
1903 	if (put_user(0, &uattr->batch.count))
1904 		return -EFAULT;
1905 
1906 	key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1907 	if (!key)
1908 		return -ENOMEM;
1909 
1910 	for (cp = 0; cp < max_count; cp++) {
1911 		err = -EFAULT;
1912 		if (copy_from_user(key, keys + cp * map->key_size,
1913 				   map->key_size))
1914 			break;
1915 
1916 		if (bpf_map_is_offloaded(map)) {
1917 			err = bpf_map_offload_delete_elem(map, key);
1918 			break;
1919 		}
1920 
1921 		bpf_disable_instrumentation();
1922 		rcu_read_lock();
1923 		err = map->ops->map_delete_elem(map, key);
1924 		rcu_read_unlock();
1925 		bpf_enable_instrumentation();
1926 		if (err)
1927 			break;
1928 		cond_resched();
1929 	}
1930 	if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1931 		err = -EFAULT;
1932 
1933 	kvfree(key);
1934 
1935 	return err;
1936 }
1937 
1938 int generic_map_update_batch(struct bpf_map *map, struct file *map_file,
1939 			     const union bpf_attr *attr,
1940 			     union bpf_attr __user *uattr)
1941 {
1942 	void __user *values = u64_to_user_ptr(attr->batch.values);
1943 	void __user *keys = u64_to_user_ptr(attr->batch.keys);
1944 	u32 value_size, cp, max_count;
1945 	void *key, *value;
1946 	int err = 0;
1947 
1948 	if (attr->batch.elem_flags & ~BPF_F_LOCK)
1949 		return -EINVAL;
1950 
1951 	if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1952 	    !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
1953 		return -EINVAL;
1954 	}
1955 
1956 	value_size = bpf_map_value_size(map);
1957 
1958 	max_count = attr->batch.count;
1959 	if (!max_count)
1960 		return 0;
1961 
1962 	if (put_user(0, &uattr->batch.count))
1963 		return -EFAULT;
1964 
1965 	key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1966 	if (!key)
1967 		return -ENOMEM;
1968 
1969 	value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
1970 	if (!value) {
1971 		kvfree(key);
1972 		return -ENOMEM;
1973 	}
1974 
1975 	for (cp = 0; cp < max_count; cp++) {
1976 		err = -EFAULT;
1977 		if (copy_from_user(key, keys + cp * map->key_size,
1978 		    map->key_size) ||
1979 		    copy_from_user(value, values + cp * value_size, value_size))
1980 			break;
1981 
1982 		err = bpf_map_update_value(map, map_file, key, value,
1983 					   attr->batch.elem_flags);
1984 
1985 		if (err)
1986 			break;
1987 		cond_resched();
1988 	}
1989 
1990 	if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1991 		err = -EFAULT;
1992 
1993 	kvfree(value);
1994 	kvfree(key);
1995 
1996 	return err;
1997 }
1998 
1999 int generic_map_lookup_batch(struct bpf_map *map,
2000 				    const union bpf_attr *attr,
2001 				    union bpf_attr __user *uattr)
2002 {
2003 	void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
2004 	void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
2005 	void __user *values = u64_to_user_ptr(attr->batch.values);
2006 	void __user *keys = u64_to_user_ptr(attr->batch.keys);
2007 	void *buf, *buf_prevkey, *prev_key, *key, *value;
2008 	u32 value_size, cp, max_count;
2009 	int err;
2010 
2011 	if (attr->batch.elem_flags & ~BPF_F_LOCK)
2012 		return -EINVAL;
2013 
2014 	if ((attr->batch.elem_flags & BPF_F_LOCK) &&
2015 	    !btf_record_has_field(map->record, BPF_SPIN_LOCK))
2016 		return -EINVAL;
2017 
2018 	value_size = bpf_map_value_size(map);
2019 
2020 	max_count = attr->batch.count;
2021 	if (!max_count)
2022 		return 0;
2023 
2024 	if (put_user(0, &uattr->batch.count))
2025 		return -EFAULT;
2026 
2027 	buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
2028 	if (!buf_prevkey)
2029 		return -ENOMEM;
2030 
2031 	buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
2032 	if (!buf) {
2033 		kvfree(buf_prevkey);
2034 		return -ENOMEM;
2035 	}
2036 
2037 	err = -EFAULT;
2038 	prev_key = NULL;
2039 	if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
2040 		goto free_buf;
2041 	key = buf;
2042 	value = key + map->key_size;
2043 	if (ubatch)
2044 		prev_key = buf_prevkey;
2045 
2046 	for (cp = 0; cp < max_count;) {
2047 		rcu_read_lock();
2048 		err = map->ops->map_get_next_key(map, prev_key, key);
2049 		rcu_read_unlock();
2050 		if (err)
2051 			break;
2052 		err = bpf_map_copy_value(map, key, value,
2053 					 attr->batch.elem_flags);
2054 
2055 		if (err == -ENOENT)
2056 			goto next_key;
2057 
2058 		if (err)
2059 			goto free_buf;
2060 
2061 		if (copy_to_user(keys + cp * map->key_size, key,
2062 				 map->key_size)) {
2063 			err = -EFAULT;
2064 			goto free_buf;
2065 		}
2066 		if (copy_to_user(values + cp * value_size, value, value_size)) {
2067 			err = -EFAULT;
2068 			goto free_buf;
2069 		}
2070 
2071 		cp++;
2072 next_key:
2073 		if (!prev_key)
2074 			prev_key = buf_prevkey;
2075 
2076 		swap(prev_key, key);
2077 		cond_resched();
2078 	}
2079 
2080 	if (err == -EFAULT)
2081 		goto free_buf;
2082 
2083 	if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
2084 		    (cp && copy_to_user(uobatch, prev_key, map->key_size))))
2085 		err = -EFAULT;
2086 
2087 free_buf:
2088 	kvfree(buf_prevkey);
2089 	kvfree(buf);
2090 	return err;
2091 }
2092 
2093 #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
2094 
2095 static int map_lookup_and_delete_elem(union bpf_attr *attr)
2096 {
2097 	void __user *ukey = u64_to_user_ptr(attr->key);
2098 	void __user *uvalue = u64_to_user_ptr(attr->value);
2099 	struct bpf_map *map;
2100 	void *key, *value;
2101 	u32 value_size;
2102 	int err;
2103 
2104 	if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
2105 		return -EINVAL;
2106 
2107 	if (attr->flags & ~BPF_F_LOCK)
2108 		return -EINVAL;
2109 
2110 	CLASS(fd, f)(attr->map_fd);
2111 	map = __bpf_map_get(f);
2112 	if (IS_ERR(map))
2113 		return PTR_ERR(map);
2114 	bpf_map_write_active_inc(map);
2115 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
2116 	    !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
2117 		err = -EPERM;
2118 		goto err_put;
2119 	}
2120 
2121 	if (attr->flags &&
2122 	    (map->map_type == BPF_MAP_TYPE_QUEUE ||
2123 	     map->map_type == BPF_MAP_TYPE_STACK)) {
2124 		err = -EINVAL;
2125 		goto err_put;
2126 	}
2127 
2128 	if ((attr->flags & BPF_F_LOCK) &&
2129 	    !btf_record_has_field(map->record, BPF_SPIN_LOCK)) {
2130 		err = -EINVAL;
2131 		goto err_put;
2132 	}
2133 
2134 	key = __bpf_copy_key(ukey, map->key_size);
2135 	if (IS_ERR(key)) {
2136 		err = PTR_ERR(key);
2137 		goto err_put;
2138 	}
2139 
2140 	value_size = bpf_map_value_size(map);
2141 
2142 	err = -ENOMEM;
2143 	value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
2144 	if (!value)
2145 		goto free_key;
2146 
2147 	err = -ENOTSUPP;
2148 	if (map->map_type == BPF_MAP_TYPE_QUEUE ||
2149 	    map->map_type == BPF_MAP_TYPE_STACK) {
2150 		err = map->ops->map_pop_elem(map, value);
2151 	} else if (map->map_type == BPF_MAP_TYPE_HASH ||
2152 		   map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
2153 		   map->map_type == BPF_MAP_TYPE_LRU_HASH ||
2154 		   map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
2155 		if (!bpf_map_is_offloaded(map)) {
2156 			bpf_disable_instrumentation();
2157 			rcu_read_lock();
2158 			err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
2159 			rcu_read_unlock();
2160 			bpf_enable_instrumentation();
2161 		}
2162 	}
2163 
2164 	if (err)
2165 		goto free_value;
2166 
2167 	if (copy_to_user(uvalue, value, value_size) != 0) {
2168 		err = -EFAULT;
2169 		goto free_value;
2170 	}
2171 
2172 	err = 0;
2173 
2174 free_value:
2175 	kvfree(value);
2176 free_key:
2177 	kvfree(key);
2178 err_put:
2179 	bpf_map_write_active_dec(map);
2180 	return err;
2181 }
2182 
2183 #define BPF_MAP_FREEZE_LAST_FIELD map_fd
2184 
2185 static int map_freeze(const union bpf_attr *attr)
2186 {
2187 	int err = 0;
2188 	struct bpf_map *map;
2189 
2190 	if (CHECK_ATTR(BPF_MAP_FREEZE))
2191 		return -EINVAL;
2192 
2193 	CLASS(fd, f)(attr->map_fd);
2194 	map = __bpf_map_get(f);
2195 	if (IS_ERR(map))
2196 		return PTR_ERR(map);
2197 
2198 	if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS || !IS_ERR_OR_NULL(map->record))
2199 		return -ENOTSUPP;
2200 
2201 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE))
2202 		return -EPERM;
2203 
2204 	mutex_lock(&map->freeze_mutex);
2205 	if (bpf_map_write_active(map)) {
2206 		err = -EBUSY;
2207 		goto err_put;
2208 	}
2209 	if (READ_ONCE(map->frozen)) {
2210 		err = -EBUSY;
2211 		goto err_put;
2212 	}
2213 
2214 	WRITE_ONCE(map->frozen, true);
2215 err_put:
2216 	mutex_unlock(&map->freeze_mutex);
2217 	return err;
2218 }
2219 
2220 static const struct bpf_prog_ops * const bpf_prog_types[] = {
2221 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
2222 	[_id] = & _name ## _prog_ops,
2223 #define BPF_MAP_TYPE(_id, _ops)
2224 #define BPF_LINK_TYPE(_id, _name)
2225 #include <linux/bpf_types.h>
2226 #undef BPF_PROG_TYPE
2227 #undef BPF_MAP_TYPE
2228 #undef BPF_LINK_TYPE
2229 };
2230 
2231 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
2232 {
2233 	const struct bpf_prog_ops *ops;
2234 
2235 	if (type >= ARRAY_SIZE(bpf_prog_types))
2236 		return -EINVAL;
2237 	type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
2238 	ops = bpf_prog_types[type];
2239 	if (!ops)
2240 		return -EINVAL;
2241 
2242 	if (!bpf_prog_is_offloaded(prog->aux))
2243 		prog->aux->ops = ops;
2244 	else
2245 		prog->aux->ops = &bpf_offload_prog_ops;
2246 	prog->type = type;
2247 	return 0;
2248 }
2249 
2250 enum bpf_audit {
2251 	BPF_AUDIT_LOAD,
2252 	BPF_AUDIT_UNLOAD,
2253 	BPF_AUDIT_MAX,
2254 };
2255 
2256 static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
2257 	[BPF_AUDIT_LOAD]   = "LOAD",
2258 	[BPF_AUDIT_UNLOAD] = "UNLOAD",
2259 };
2260 
2261 static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
2262 {
2263 	struct audit_context *ctx = NULL;
2264 	struct audit_buffer *ab;
2265 
2266 	if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
2267 		return;
2268 	if (audit_enabled == AUDIT_OFF)
2269 		return;
2270 	if (!in_irq() && !irqs_disabled())
2271 		ctx = audit_context();
2272 	ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
2273 	if (unlikely(!ab))
2274 		return;
2275 	audit_log_format(ab, "prog-id=%u op=%s",
2276 			 prog->aux->id, bpf_audit_str[op]);
2277 	audit_log_end(ab);
2278 }
2279 
2280 static int bpf_prog_alloc_id(struct bpf_prog *prog)
2281 {
2282 	int id;
2283 
2284 	idr_preload(GFP_KERNEL);
2285 	spin_lock_bh(&prog_idr_lock);
2286 	id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
2287 	if (id > 0)
2288 		prog->aux->id = id;
2289 	spin_unlock_bh(&prog_idr_lock);
2290 	idr_preload_end();
2291 
2292 	/* id is in [1, INT_MAX) */
2293 	if (WARN_ON_ONCE(!id))
2294 		return -ENOSPC;
2295 
2296 	return id > 0 ? 0 : id;
2297 }
2298 
2299 void bpf_prog_free_id(struct bpf_prog *prog)
2300 {
2301 	unsigned long flags;
2302 
2303 	/* cBPF to eBPF migrations are currently not in the idr store.
2304 	 * Offloaded programs are removed from the store when their device
2305 	 * disappears - even if someone grabs an fd to them they are unusable,
2306 	 * simply waiting for refcnt to drop to be freed.
2307 	 */
2308 	if (!prog->aux->id)
2309 		return;
2310 
2311 	spin_lock_irqsave(&prog_idr_lock, flags);
2312 	idr_remove(&prog_idr, prog->aux->id);
2313 	prog->aux->id = 0;
2314 	spin_unlock_irqrestore(&prog_idr_lock, flags);
2315 }
2316 
2317 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
2318 {
2319 	struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
2320 
2321 	kvfree(aux->func_info);
2322 	kfree(aux->func_info_aux);
2323 	free_uid(aux->user);
2324 	security_bpf_prog_free(aux->prog);
2325 	bpf_prog_free(aux->prog);
2326 }
2327 
2328 static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
2329 {
2330 	bpf_prog_kallsyms_del_all(prog);
2331 	btf_put(prog->aux->btf);
2332 	module_put(prog->aux->mod);
2333 	kvfree(prog->aux->jited_linfo);
2334 	kvfree(prog->aux->linfo);
2335 	kfree(prog->aux->kfunc_tab);
2336 	kfree(prog->aux->ctx_arg_info);
2337 	if (prog->aux->attach_btf)
2338 		btf_put(prog->aux->attach_btf);
2339 
2340 	if (deferred) {
2341 		if (prog->sleepable)
2342 			call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
2343 		else
2344 			call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
2345 	} else {
2346 		__bpf_prog_put_rcu(&prog->aux->rcu);
2347 	}
2348 }
2349 
2350 static void bpf_prog_put_deferred(struct work_struct *work)
2351 {
2352 	struct bpf_prog_aux *aux;
2353 	struct bpf_prog *prog;
2354 
2355 	aux = container_of(work, struct bpf_prog_aux, work);
2356 	prog = aux->prog;
2357 	perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
2358 	bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
2359 	bpf_prog_free_id(prog);
2360 	__bpf_prog_put_noref(prog, true);
2361 }
2362 
2363 static void __bpf_prog_put(struct bpf_prog *prog)
2364 {
2365 	struct bpf_prog_aux *aux = prog->aux;
2366 
2367 	if (atomic64_dec_and_test(&aux->refcnt)) {
2368 		if (in_irq() || irqs_disabled()) {
2369 			INIT_WORK(&aux->work, bpf_prog_put_deferred);
2370 			schedule_work(&aux->work);
2371 		} else {
2372 			bpf_prog_put_deferred(&aux->work);
2373 		}
2374 	}
2375 }
2376 
2377 void bpf_prog_put(struct bpf_prog *prog)
2378 {
2379 	__bpf_prog_put(prog);
2380 }
2381 EXPORT_SYMBOL_GPL(bpf_prog_put);
2382 
2383 static int bpf_prog_release(struct inode *inode, struct file *filp)
2384 {
2385 	struct bpf_prog *prog = filp->private_data;
2386 
2387 	bpf_prog_put(prog);
2388 	return 0;
2389 }
2390 
2391 struct bpf_prog_kstats {
2392 	u64 nsecs;
2393 	u64 cnt;
2394 	u64 misses;
2395 };
2396 
2397 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog)
2398 {
2399 	struct bpf_prog_stats *stats;
2400 	unsigned int flags;
2401 
2402 	stats = this_cpu_ptr(prog->stats);
2403 	flags = u64_stats_update_begin_irqsave(&stats->syncp);
2404 	u64_stats_inc(&stats->misses);
2405 	u64_stats_update_end_irqrestore(&stats->syncp, flags);
2406 }
2407 
2408 static void bpf_prog_get_stats(const struct bpf_prog *prog,
2409 			       struct bpf_prog_kstats *stats)
2410 {
2411 	u64 nsecs = 0, cnt = 0, misses = 0;
2412 	int cpu;
2413 
2414 	for_each_possible_cpu(cpu) {
2415 		const struct bpf_prog_stats *st;
2416 		unsigned int start;
2417 		u64 tnsecs, tcnt, tmisses;
2418 
2419 		st = per_cpu_ptr(prog->stats, cpu);
2420 		do {
2421 			start = u64_stats_fetch_begin(&st->syncp);
2422 			tnsecs = u64_stats_read(&st->nsecs);
2423 			tcnt = u64_stats_read(&st->cnt);
2424 			tmisses = u64_stats_read(&st->misses);
2425 		} while (u64_stats_fetch_retry(&st->syncp, start));
2426 		nsecs += tnsecs;
2427 		cnt += tcnt;
2428 		misses += tmisses;
2429 	}
2430 	stats->nsecs = nsecs;
2431 	stats->cnt = cnt;
2432 	stats->misses = misses;
2433 }
2434 
2435 #ifdef CONFIG_PROC_FS
2436 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
2437 {
2438 	const struct bpf_prog *prog = filp->private_data;
2439 	char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2440 	struct bpf_prog_kstats stats;
2441 
2442 	bpf_prog_get_stats(prog, &stats);
2443 	bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2444 	seq_printf(m,
2445 		   "prog_type:\t%u\n"
2446 		   "prog_jited:\t%u\n"
2447 		   "prog_tag:\t%s\n"
2448 		   "memlock:\t%llu\n"
2449 		   "prog_id:\t%u\n"
2450 		   "run_time_ns:\t%llu\n"
2451 		   "run_cnt:\t%llu\n"
2452 		   "recursion_misses:\t%llu\n"
2453 		   "verified_insns:\t%u\n",
2454 		   prog->type,
2455 		   prog->jited,
2456 		   prog_tag,
2457 		   prog->pages * 1ULL << PAGE_SHIFT,
2458 		   prog->aux->id,
2459 		   stats.nsecs,
2460 		   stats.cnt,
2461 		   stats.misses,
2462 		   prog->aux->verified_insns);
2463 }
2464 #endif
2465 
2466 const struct file_operations bpf_prog_fops = {
2467 #ifdef CONFIG_PROC_FS
2468 	.show_fdinfo	= bpf_prog_show_fdinfo,
2469 #endif
2470 	.release	= bpf_prog_release,
2471 	.read		= bpf_dummy_read,
2472 	.write		= bpf_dummy_write,
2473 };
2474 
2475 int bpf_prog_new_fd(struct bpf_prog *prog)
2476 {
2477 	int ret;
2478 
2479 	ret = security_bpf_prog(prog);
2480 	if (ret < 0)
2481 		return ret;
2482 
2483 	return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
2484 				O_RDWR | O_CLOEXEC);
2485 }
2486 
2487 void bpf_prog_add(struct bpf_prog *prog, int i)
2488 {
2489 	atomic64_add(i, &prog->aux->refcnt);
2490 }
2491 EXPORT_SYMBOL_GPL(bpf_prog_add);
2492 
2493 void bpf_prog_sub(struct bpf_prog *prog, int i)
2494 {
2495 	/* Only to be used for undoing previous bpf_prog_add() in some
2496 	 * error path. We still know that another entity in our call
2497 	 * path holds a reference to the program, thus atomic_sub() can
2498 	 * be safely used in such cases!
2499 	 */
2500 	WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
2501 }
2502 EXPORT_SYMBOL_GPL(bpf_prog_sub);
2503 
2504 void bpf_prog_inc(struct bpf_prog *prog)
2505 {
2506 	atomic64_inc(&prog->aux->refcnt);
2507 }
2508 EXPORT_SYMBOL_GPL(bpf_prog_inc);
2509 
2510 /* prog_idr_lock should have been held */
2511 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
2512 {
2513 	int refold;
2514 
2515 	refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
2516 
2517 	if (!refold)
2518 		return ERR_PTR(-ENOENT);
2519 
2520 	return prog;
2521 }
2522 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
2523 
2524 bool bpf_prog_get_ok(struct bpf_prog *prog,
2525 			    enum bpf_prog_type *attach_type, bool attach_drv)
2526 {
2527 	/* not an attachment, just a refcount inc, always allow */
2528 	if (!attach_type)
2529 		return true;
2530 
2531 	if (prog->type != *attach_type)
2532 		return false;
2533 	if (bpf_prog_is_offloaded(prog->aux) && !attach_drv)
2534 		return false;
2535 
2536 	return true;
2537 }
2538 
2539 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
2540 				       bool attach_drv)
2541 {
2542 	CLASS(fd, f)(ufd);
2543 	struct bpf_prog *prog;
2544 
2545 	if (fd_empty(f))
2546 		return ERR_PTR(-EBADF);
2547 	if (fd_file(f)->f_op != &bpf_prog_fops)
2548 		return ERR_PTR(-EINVAL);
2549 
2550 	prog = fd_file(f)->private_data;
2551 	if (!bpf_prog_get_ok(prog, attach_type, attach_drv))
2552 		return ERR_PTR(-EINVAL);
2553 
2554 	bpf_prog_inc(prog);
2555 	return prog;
2556 }
2557 
2558 struct bpf_prog *bpf_prog_get(u32 ufd)
2559 {
2560 	return __bpf_prog_get(ufd, NULL, false);
2561 }
2562 
2563 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
2564 				       bool attach_drv)
2565 {
2566 	return __bpf_prog_get(ufd, &type, attach_drv);
2567 }
2568 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
2569 
2570 /* Initially all BPF programs could be loaded w/o specifying
2571  * expected_attach_type. Later for some of them specifying expected_attach_type
2572  * at load time became required so that program could be validated properly.
2573  * Programs of types that are allowed to be loaded both w/ and w/o (for
2574  * backward compatibility) expected_attach_type, should have the default attach
2575  * type assigned to expected_attach_type for the latter case, so that it can be
2576  * validated later at attach time.
2577  *
2578  * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
2579  * prog type requires it but has some attach types that have to be backward
2580  * compatible.
2581  */
2582 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
2583 {
2584 	switch (attr->prog_type) {
2585 	case BPF_PROG_TYPE_CGROUP_SOCK:
2586 		/* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
2587 		 * exist so checking for non-zero is the way to go here.
2588 		 */
2589 		if (!attr->expected_attach_type)
2590 			attr->expected_attach_type =
2591 				BPF_CGROUP_INET_SOCK_CREATE;
2592 		break;
2593 	case BPF_PROG_TYPE_SK_REUSEPORT:
2594 		if (!attr->expected_attach_type)
2595 			attr->expected_attach_type =
2596 				BPF_SK_REUSEPORT_SELECT;
2597 		break;
2598 	}
2599 }
2600 
2601 static int
2602 bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2603 			   enum bpf_attach_type expected_attach_type,
2604 			   struct btf *attach_btf, u32 btf_id,
2605 			   struct bpf_prog *dst_prog)
2606 {
2607 	if (btf_id) {
2608 		if (btf_id > BTF_MAX_TYPE)
2609 			return -EINVAL;
2610 
2611 		if (!attach_btf && !dst_prog)
2612 			return -EINVAL;
2613 
2614 		switch (prog_type) {
2615 		case BPF_PROG_TYPE_TRACING:
2616 		case BPF_PROG_TYPE_LSM:
2617 		case BPF_PROG_TYPE_STRUCT_OPS:
2618 		case BPF_PROG_TYPE_EXT:
2619 			break;
2620 		default:
2621 			return -EINVAL;
2622 		}
2623 	}
2624 
2625 	if (attach_btf && (!btf_id || dst_prog))
2626 		return -EINVAL;
2627 
2628 	if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
2629 	    prog_type != BPF_PROG_TYPE_EXT)
2630 		return -EINVAL;
2631 
2632 	switch (prog_type) {
2633 	case BPF_PROG_TYPE_CGROUP_SOCK:
2634 		switch (expected_attach_type) {
2635 		case BPF_CGROUP_INET_SOCK_CREATE:
2636 		case BPF_CGROUP_INET_SOCK_RELEASE:
2637 		case BPF_CGROUP_INET4_POST_BIND:
2638 		case BPF_CGROUP_INET6_POST_BIND:
2639 			return 0;
2640 		default:
2641 			return -EINVAL;
2642 		}
2643 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2644 		switch (expected_attach_type) {
2645 		case BPF_CGROUP_INET4_BIND:
2646 		case BPF_CGROUP_INET6_BIND:
2647 		case BPF_CGROUP_INET4_CONNECT:
2648 		case BPF_CGROUP_INET6_CONNECT:
2649 		case BPF_CGROUP_UNIX_CONNECT:
2650 		case BPF_CGROUP_INET4_GETPEERNAME:
2651 		case BPF_CGROUP_INET6_GETPEERNAME:
2652 		case BPF_CGROUP_UNIX_GETPEERNAME:
2653 		case BPF_CGROUP_INET4_GETSOCKNAME:
2654 		case BPF_CGROUP_INET6_GETSOCKNAME:
2655 		case BPF_CGROUP_UNIX_GETSOCKNAME:
2656 		case BPF_CGROUP_UDP4_SENDMSG:
2657 		case BPF_CGROUP_UDP6_SENDMSG:
2658 		case BPF_CGROUP_UNIX_SENDMSG:
2659 		case BPF_CGROUP_UDP4_RECVMSG:
2660 		case BPF_CGROUP_UDP6_RECVMSG:
2661 		case BPF_CGROUP_UNIX_RECVMSG:
2662 			return 0;
2663 		default:
2664 			return -EINVAL;
2665 		}
2666 	case BPF_PROG_TYPE_CGROUP_SKB:
2667 		switch (expected_attach_type) {
2668 		case BPF_CGROUP_INET_INGRESS:
2669 		case BPF_CGROUP_INET_EGRESS:
2670 			return 0;
2671 		default:
2672 			return -EINVAL;
2673 		}
2674 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2675 		switch (expected_attach_type) {
2676 		case BPF_CGROUP_SETSOCKOPT:
2677 		case BPF_CGROUP_GETSOCKOPT:
2678 			return 0;
2679 		default:
2680 			return -EINVAL;
2681 		}
2682 	case BPF_PROG_TYPE_SK_LOOKUP:
2683 		if (expected_attach_type == BPF_SK_LOOKUP)
2684 			return 0;
2685 		return -EINVAL;
2686 	case BPF_PROG_TYPE_SK_REUSEPORT:
2687 		switch (expected_attach_type) {
2688 		case BPF_SK_REUSEPORT_SELECT:
2689 		case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2690 			return 0;
2691 		default:
2692 			return -EINVAL;
2693 		}
2694 	case BPF_PROG_TYPE_NETFILTER:
2695 		if (expected_attach_type == BPF_NETFILTER)
2696 			return 0;
2697 		return -EINVAL;
2698 	case BPF_PROG_TYPE_SYSCALL:
2699 	case BPF_PROG_TYPE_EXT:
2700 		if (expected_attach_type)
2701 			return -EINVAL;
2702 		fallthrough;
2703 	default:
2704 		return 0;
2705 	}
2706 }
2707 
2708 static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2709 {
2710 	switch (prog_type) {
2711 	case BPF_PROG_TYPE_SCHED_CLS:
2712 	case BPF_PROG_TYPE_SCHED_ACT:
2713 	case BPF_PROG_TYPE_XDP:
2714 	case BPF_PROG_TYPE_LWT_IN:
2715 	case BPF_PROG_TYPE_LWT_OUT:
2716 	case BPF_PROG_TYPE_LWT_XMIT:
2717 	case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2718 	case BPF_PROG_TYPE_SK_SKB:
2719 	case BPF_PROG_TYPE_SK_MSG:
2720 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
2721 	case BPF_PROG_TYPE_CGROUP_DEVICE:
2722 	case BPF_PROG_TYPE_CGROUP_SOCK:
2723 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2724 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2725 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
2726 	case BPF_PROG_TYPE_SOCK_OPS:
2727 	case BPF_PROG_TYPE_EXT: /* extends any prog */
2728 	case BPF_PROG_TYPE_NETFILTER:
2729 		return true;
2730 	case BPF_PROG_TYPE_CGROUP_SKB:
2731 		/* always unpriv */
2732 	case BPF_PROG_TYPE_SK_REUSEPORT:
2733 		/* equivalent to SOCKET_FILTER. need CAP_BPF only */
2734 	default:
2735 		return false;
2736 	}
2737 }
2738 
2739 static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2740 {
2741 	switch (prog_type) {
2742 	case BPF_PROG_TYPE_KPROBE:
2743 	case BPF_PROG_TYPE_TRACEPOINT:
2744 	case BPF_PROG_TYPE_PERF_EVENT:
2745 	case BPF_PROG_TYPE_RAW_TRACEPOINT:
2746 	case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2747 	case BPF_PROG_TYPE_TRACING:
2748 	case BPF_PROG_TYPE_LSM:
2749 	case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2750 	case BPF_PROG_TYPE_EXT: /* extends any prog */
2751 		return true;
2752 	default:
2753 		return false;
2754 	}
2755 }
2756 
2757 /* last field in 'union bpf_attr' used by this command */
2758 #define BPF_PROG_LOAD_LAST_FIELD fd_array_cnt
2759 
2760 static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size)
2761 {
2762 	enum bpf_prog_type type = attr->prog_type;
2763 	struct bpf_prog *prog, *dst_prog = NULL;
2764 	struct btf *attach_btf = NULL;
2765 	struct bpf_token *token = NULL;
2766 	bool bpf_cap;
2767 	int err;
2768 	char license[128];
2769 
2770 	if (CHECK_ATTR(BPF_PROG_LOAD))
2771 		return -EINVAL;
2772 
2773 	if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2774 				 BPF_F_ANY_ALIGNMENT |
2775 				 BPF_F_TEST_STATE_FREQ |
2776 				 BPF_F_SLEEPABLE |
2777 				 BPF_F_TEST_RND_HI32 |
2778 				 BPF_F_XDP_HAS_FRAGS |
2779 				 BPF_F_XDP_DEV_BOUND_ONLY |
2780 				 BPF_F_TEST_REG_INVARIANTS |
2781 				 BPF_F_TOKEN_FD))
2782 		return -EINVAL;
2783 
2784 	bpf_prog_load_fixup_attach_type(attr);
2785 
2786 	if (attr->prog_flags & BPF_F_TOKEN_FD) {
2787 		token = bpf_token_get_from_fd(attr->prog_token_fd);
2788 		if (IS_ERR(token))
2789 			return PTR_ERR(token);
2790 		/* if current token doesn't grant prog loading permissions,
2791 		 * then we can't use this token, so ignore it and rely on
2792 		 * system-wide capabilities checks
2793 		 */
2794 		if (!bpf_token_allow_cmd(token, BPF_PROG_LOAD) ||
2795 		    !bpf_token_allow_prog_type(token, attr->prog_type,
2796 					       attr->expected_attach_type)) {
2797 			bpf_token_put(token);
2798 			token = NULL;
2799 		}
2800 	}
2801 
2802 	bpf_cap = bpf_token_capable(token, CAP_BPF);
2803 	err = -EPERM;
2804 
2805 	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2806 	    (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2807 	    !bpf_cap)
2808 		goto put_token;
2809 
2810 	/* Intent here is for unprivileged_bpf_disabled to block BPF program
2811 	 * creation for unprivileged users; other actions depend
2812 	 * on fd availability and access to bpffs, so are dependent on
2813 	 * object creation success. Even with unprivileged BPF disabled,
2814 	 * capability checks are still carried out for these
2815 	 * and other operations.
2816 	 */
2817 	if (sysctl_unprivileged_bpf_disabled && !bpf_cap)
2818 		goto put_token;
2819 
2820 	if (attr->insn_cnt == 0 ||
2821 	    attr->insn_cnt > (bpf_cap ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS)) {
2822 		err = -E2BIG;
2823 		goto put_token;
2824 	}
2825 	if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2826 	    type != BPF_PROG_TYPE_CGROUP_SKB &&
2827 	    !bpf_cap)
2828 		goto put_token;
2829 
2830 	if (is_net_admin_prog_type(type) && !bpf_token_capable(token, CAP_NET_ADMIN))
2831 		goto put_token;
2832 	if (is_perfmon_prog_type(type) && !bpf_token_capable(token, CAP_PERFMON))
2833 		goto put_token;
2834 
2835 	/* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2836 	 * or btf, we need to check which one it is
2837 	 */
2838 	if (attr->attach_prog_fd) {
2839 		dst_prog = bpf_prog_get(attr->attach_prog_fd);
2840 		if (IS_ERR(dst_prog)) {
2841 			dst_prog = NULL;
2842 			attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2843 			if (IS_ERR(attach_btf)) {
2844 				err = -EINVAL;
2845 				goto put_token;
2846 			}
2847 			if (!btf_is_kernel(attach_btf)) {
2848 				/* attaching through specifying bpf_prog's BTF
2849 				 * objects directly might be supported eventually
2850 				 */
2851 				btf_put(attach_btf);
2852 				err = -ENOTSUPP;
2853 				goto put_token;
2854 			}
2855 		}
2856 	} else if (attr->attach_btf_id) {
2857 		/* fall back to vmlinux BTF, if BTF type ID is specified */
2858 		attach_btf = bpf_get_btf_vmlinux();
2859 		if (IS_ERR(attach_btf)) {
2860 			err = PTR_ERR(attach_btf);
2861 			goto put_token;
2862 		}
2863 		if (!attach_btf) {
2864 			err = -EINVAL;
2865 			goto put_token;
2866 		}
2867 		btf_get(attach_btf);
2868 	}
2869 
2870 	if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2871 				       attach_btf, attr->attach_btf_id,
2872 				       dst_prog)) {
2873 		if (dst_prog)
2874 			bpf_prog_put(dst_prog);
2875 		if (attach_btf)
2876 			btf_put(attach_btf);
2877 		err = -EINVAL;
2878 		goto put_token;
2879 	}
2880 
2881 	/* plain bpf_prog allocation */
2882 	prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2883 	if (!prog) {
2884 		if (dst_prog)
2885 			bpf_prog_put(dst_prog);
2886 		if (attach_btf)
2887 			btf_put(attach_btf);
2888 		err = -EINVAL;
2889 		goto put_token;
2890 	}
2891 
2892 	prog->expected_attach_type = attr->expected_attach_type;
2893 	prog->sleepable = !!(attr->prog_flags & BPF_F_SLEEPABLE);
2894 	prog->aux->attach_btf = attach_btf;
2895 	prog->aux->attach_btf_id = attr->attach_btf_id;
2896 	prog->aux->dst_prog = dst_prog;
2897 	prog->aux->dev_bound = !!attr->prog_ifindex;
2898 	prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
2899 
2900 	/* move token into prog->aux, reuse taken refcnt */
2901 	prog->aux->token = token;
2902 	token = NULL;
2903 
2904 	prog->aux->user = get_current_user();
2905 	prog->len = attr->insn_cnt;
2906 
2907 	err = -EFAULT;
2908 	if (copy_from_bpfptr(prog->insns,
2909 			     make_bpfptr(attr->insns, uattr.is_kernel),
2910 			     bpf_prog_insn_size(prog)) != 0)
2911 		goto free_prog;
2912 	/* copy eBPF program license from user space */
2913 	if (strncpy_from_bpfptr(license,
2914 				make_bpfptr(attr->license, uattr.is_kernel),
2915 				sizeof(license) - 1) < 0)
2916 		goto free_prog;
2917 	license[sizeof(license) - 1] = 0;
2918 
2919 	/* eBPF programs must be GPL compatible to use GPL-ed functions */
2920 	prog->gpl_compatible = license_is_gpl_compatible(license) ? 1 : 0;
2921 
2922 	prog->orig_prog = NULL;
2923 	prog->jited = 0;
2924 
2925 	atomic64_set(&prog->aux->refcnt, 1);
2926 
2927 	if (bpf_prog_is_dev_bound(prog->aux)) {
2928 		err = bpf_prog_dev_bound_init(prog, attr);
2929 		if (err)
2930 			goto free_prog;
2931 	}
2932 
2933 	if (type == BPF_PROG_TYPE_EXT && dst_prog &&
2934 	    bpf_prog_is_dev_bound(dst_prog->aux)) {
2935 		err = bpf_prog_dev_bound_inherit(prog, dst_prog);
2936 		if (err)
2937 			goto free_prog;
2938 	}
2939 
2940 	/*
2941 	 * Bookkeeping for managing the program attachment chain.
2942 	 *
2943 	 * It might be tempting to set attach_tracing_prog flag at the attachment
2944 	 * time, but this will not prevent from loading bunch of tracing prog
2945 	 * first, then attach them one to another.
2946 	 *
2947 	 * The flag attach_tracing_prog is set for the whole program lifecycle, and
2948 	 * doesn't have to be cleared in bpf_tracing_link_release, since tracing
2949 	 * programs cannot change attachment target.
2950 	 */
2951 	if (type == BPF_PROG_TYPE_TRACING && dst_prog &&
2952 	    dst_prog->type == BPF_PROG_TYPE_TRACING) {
2953 		prog->aux->attach_tracing_prog = true;
2954 	}
2955 
2956 	/* find program type: socket_filter vs tracing_filter */
2957 	err = find_prog_type(type, prog);
2958 	if (err < 0)
2959 		goto free_prog;
2960 
2961 	prog->aux->load_time = ktime_get_boottime_ns();
2962 	err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2963 			       sizeof(attr->prog_name));
2964 	if (err < 0)
2965 		goto free_prog;
2966 
2967 	err = security_bpf_prog_load(prog, attr, token, uattr.is_kernel);
2968 	if (err)
2969 		goto free_prog_sec;
2970 
2971 	/* run eBPF verifier */
2972 	err = bpf_check(&prog, attr, uattr, uattr_size);
2973 	if (err < 0)
2974 		goto free_used_maps;
2975 
2976 	prog = bpf_prog_select_runtime(prog, &err);
2977 	if (err < 0)
2978 		goto free_used_maps;
2979 
2980 	err = bpf_prog_alloc_id(prog);
2981 	if (err)
2982 		goto free_used_maps;
2983 
2984 	/* Upon success of bpf_prog_alloc_id(), the BPF prog is
2985 	 * effectively publicly exposed. However, retrieving via
2986 	 * bpf_prog_get_fd_by_id() will take another reference,
2987 	 * therefore it cannot be gone underneath us.
2988 	 *
2989 	 * Only for the time /after/ successful bpf_prog_new_fd()
2990 	 * and before returning to userspace, we might just hold
2991 	 * one reference and any parallel close on that fd could
2992 	 * rip everything out. Hence, below notifications must
2993 	 * happen before bpf_prog_new_fd().
2994 	 *
2995 	 * Also, any failure handling from this point onwards must
2996 	 * be using bpf_prog_put() given the program is exposed.
2997 	 */
2998 	bpf_prog_kallsyms_add(prog);
2999 	perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
3000 	bpf_audit_prog(prog, BPF_AUDIT_LOAD);
3001 
3002 	err = bpf_prog_new_fd(prog);
3003 	if (err < 0)
3004 		bpf_prog_put(prog);
3005 	return err;
3006 
3007 free_used_maps:
3008 	/* In case we have subprogs, we need to wait for a grace
3009 	 * period before we can tear down JIT memory since symbols
3010 	 * are already exposed under kallsyms.
3011 	 */
3012 	__bpf_prog_put_noref(prog, prog->aux->real_func_cnt);
3013 	return err;
3014 
3015 free_prog_sec:
3016 	security_bpf_prog_free(prog);
3017 free_prog:
3018 	free_uid(prog->aux->user);
3019 	if (prog->aux->attach_btf)
3020 		btf_put(prog->aux->attach_btf);
3021 	bpf_prog_free(prog);
3022 put_token:
3023 	bpf_token_put(token);
3024 	return err;
3025 }
3026 
3027 #define BPF_OBJ_LAST_FIELD path_fd
3028 
3029 static int bpf_obj_pin(const union bpf_attr *attr)
3030 {
3031 	int path_fd;
3032 
3033 	if (CHECK_ATTR(BPF_OBJ) || attr->file_flags & ~BPF_F_PATH_FD)
3034 		return -EINVAL;
3035 
3036 	/* path_fd has to be accompanied by BPF_F_PATH_FD flag */
3037 	if (!(attr->file_flags & BPF_F_PATH_FD) && attr->path_fd)
3038 		return -EINVAL;
3039 
3040 	path_fd = attr->file_flags & BPF_F_PATH_FD ? attr->path_fd : AT_FDCWD;
3041 	return bpf_obj_pin_user(attr->bpf_fd, path_fd,
3042 				u64_to_user_ptr(attr->pathname));
3043 }
3044 
3045 static int bpf_obj_get(const union bpf_attr *attr)
3046 {
3047 	int path_fd;
3048 
3049 	if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
3050 	    attr->file_flags & ~(BPF_OBJ_FLAG_MASK | BPF_F_PATH_FD))
3051 		return -EINVAL;
3052 
3053 	/* path_fd has to be accompanied by BPF_F_PATH_FD flag */
3054 	if (!(attr->file_flags & BPF_F_PATH_FD) && attr->path_fd)
3055 		return -EINVAL;
3056 
3057 	path_fd = attr->file_flags & BPF_F_PATH_FD ? attr->path_fd : AT_FDCWD;
3058 	return bpf_obj_get_user(path_fd, u64_to_user_ptr(attr->pathname),
3059 				attr->file_flags);
3060 }
3061 
3062 /* bpf_link_init_sleepable() allows to specify whether BPF link itself has
3063  * "sleepable" semantics, which normally would mean that BPF link's attach
3064  * hook can dereference link or link's underlying program for some time after
3065  * detachment due to RCU Tasks Trace-based lifetime protection scheme.
3066  * BPF program itself can be non-sleepable, yet, because it's transitively
3067  * reachable through BPF link, its freeing has to be delayed until after RCU
3068  * Tasks Trace GP.
3069  */
3070 void bpf_link_init_sleepable(struct bpf_link *link, enum bpf_link_type type,
3071 			     const struct bpf_link_ops *ops, struct bpf_prog *prog,
3072 			     enum bpf_attach_type attach_type, bool sleepable)
3073 {
3074 	WARN_ON(ops->dealloc && ops->dealloc_deferred);
3075 	atomic64_set(&link->refcnt, 1);
3076 	link->type = type;
3077 	link->sleepable = sleepable;
3078 	link->id = 0;
3079 	link->ops = ops;
3080 	link->prog = prog;
3081 	link->attach_type = attach_type;
3082 }
3083 
3084 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
3085 		   const struct bpf_link_ops *ops, struct bpf_prog *prog,
3086 		   enum bpf_attach_type attach_type)
3087 {
3088 	bpf_link_init_sleepable(link, type, ops, prog, attach_type, false);
3089 }
3090 
3091 static void bpf_link_free_id(int id)
3092 {
3093 	if (!id)
3094 		return;
3095 
3096 	spin_lock_bh(&link_idr_lock);
3097 	idr_remove(&link_idr, id);
3098 	spin_unlock_bh(&link_idr_lock);
3099 }
3100 
3101 /* Clean up bpf_link and corresponding anon_inode file and FD. After
3102  * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
3103  * anon_inode's release() call. This helper marks bpf_link as
3104  * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
3105  * is not decremented, it's the responsibility of a calling code that failed
3106  * to complete bpf_link initialization.
3107  * This helper eventually calls link's dealloc callback, but does not call
3108  * link's release callback.
3109  */
3110 void bpf_link_cleanup(struct bpf_link_primer *primer)
3111 {
3112 	primer->link->prog = NULL;
3113 	bpf_link_free_id(primer->id);
3114 	fput(primer->file);
3115 	put_unused_fd(primer->fd);
3116 }
3117 
3118 void bpf_link_inc(struct bpf_link *link)
3119 {
3120 	atomic64_inc(&link->refcnt);
3121 }
3122 
3123 static void bpf_link_dealloc(struct bpf_link *link)
3124 {
3125 	/* now that we know that bpf_link itself can't be reached, put underlying BPF program */
3126 	if (link->prog)
3127 		bpf_prog_put(link->prog);
3128 
3129 	/* free bpf_link and its containing memory */
3130 	if (link->ops->dealloc_deferred)
3131 		link->ops->dealloc_deferred(link);
3132 	else
3133 		link->ops->dealloc(link);
3134 }
3135 
3136 static void bpf_link_defer_dealloc_rcu_gp(struct rcu_head *rcu)
3137 {
3138 	struct bpf_link *link = container_of(rcu, struct bpf_link, rcu);
3139 
3140 	bpf_link_dealloc(link);
3141 }
3142 
3143 static void bpf_link_defer_dealloc_mult_rcu_gp(struct rcu_head *rcu)
3144 {
3145 	if (rcu_trace_implies_rcu_gp())
3146 		bpf_link_defer_dealloc_rcu_gp(rcu);
3147 	else
3148 		call_rcu(rcu, bpf_link_defer_dealloc_rcu_gp);
3149 }
3150 
3151 /* bpf_link_free is guaranteed to be called from process context */
3152 static void bpf_link_free(struct bpf_link *link)
3153 {
3154 	const struct bpf_link_ops *ops = link->ops;
3155 
3156 	bpf_link_free_id(link->id);
3157 	/* detach BPF program, clean up used resources */
3158 	if (link->prog)
3159 		ops->release(link);
3160 	if (ops->dealloc_deferred) {
3161 		/* Schedule BPF link deallocation, which will only then
3162 		 * trigger putting BPF program refcount.
3163 		 * If underlying BPF program is sleepable or BPF link's target
3164 		 * attach hookpoint is sleepable or otherwise requires RCU GPs
3165 		 * to ensure link and its underlying BPF program is not
3166 		 * reachable anymore, we need to first wait for RCU tasks
3167 		 * trace sync, and then go through "classic" RCU grace period
3168 		 */
3169 		if (link->sleepable || (link->prog && link->prog->sleepable))
3170 			call_rcu_tasks_trace(&link->rcu, bpf_link_defer_dealloc_mult_rcu_gp);
3171 		else
3172 			call_rcu(&link->rcu, bpf_link_defer_dealloc_rcu_gp);
3173 	} else if (ops->dealloc) {
3174 		bpf_link_dealloc(link);
3175 	}
3176 }
3177 
3178 static void bpf_link_put_deferred(struct work_struct *work)
3179 {
3180 	struct bpf_link *link = container_of(work, struct bpf_link, work);
3181 
3182 	bpf_link_free(link);
3183 }
3184 
3185 /* bpf_link_put might be called from atomic context. It needs to be called
3186  * from sleepable context in order to acquire sleeping locks during the process.
3187  */
3188 void bpf_link_put(struct bpf_link *link)
3189 {
3190 	if (!atomic64_dec_and_test(&link->refcnt))
3191 		return;
3192 
3193 	INIT_WORK(&link->work, bpf_link_put_deferred);
3194 	schedule_work(&link->work);
3195 }
3196 EXPORT_SYMBOL(bpf_link_put);
3197 
3198 static void bpf_link_put_direct(struct bpf_link *link)
3199 {
3200 	if (!atomic64_dec_and_test(&link->refcnt))
3201 		return;
3202 	bpf_link_free(link);
3203 }
3204 
3205 static int bpf_link_release(struct inode *inode, struct file *filp)
3206 {
3207 	struct bpf_link *link = filp->private_data;
3208 
3209 	bpf_link_put_direct(link);
3210 	return 0;
3211 }
3212 
3213 #ifdef CONFIG_PROC_FS
3214 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
3215 #define BPF_MAP_TYPE(_id, _ops)
3216 #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
3217 static const char *bpf_link_type_strs[] = {
3218 	[BPF_LINK_TYPE_UNSPEC] = "<invalid>",
3219 #include <linux/bpf_types.h>
3220 };
3221 #undef BPF_PROG_TYPE
3222 #undef BPF_MAP_TYPE
3223 #undef BPF_LINK_TYPE
3224 
3225 static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
3226 {
3227 	const struct bpf_link *link = filp->private_data;
3228 	const struct bpf_prog *prog = link->prog;
3229 	enum bpf_link_type type = link->type;
3230 	char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
3231 
3232 	if (type < ARRAY_SIZE(bpf_link_type_strs) && bpf_link_type_strs[type]) {
3233 		if (link->type == BPF_LINK_TYPE_KPROBE_MULTI)
3234 			seq_printf(m, "link_type:\t%s\n", link->flags == BPF_F_KPROBE_MULTI_RETURN ?
3235 				   "kretprobe_multi" : "kprobe_multi");
3236 		else if (link->type == BPF_LINK_TYPE_UPROBE_MULTI)
3237 			seq_printf(m, "link_type:\t%s\n", link->flags == BPF_F_UPROBE_MULTI_RETURN ?
3238 				   "uretprobe_multi" : "uprobe_multi");
3239 		else
3240 			seq_printf(m, "link_type:\t%s\n", bpf_link_type_strs[type]);
3241 	} else {
3242 		WARN_ONCE(1, "missing BPF_LINK_TYPE(...) for link type %u\n", type);
3243 		seq_printf(m, "link_type:\t<%u>\n", type);
3244 	}
3245 	seq_printf(m, "link_id:\t%u\n", link->id);
3246 
3247 	if (prog) {
3248 		bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
3249 		seq_printf(m,
3250 			   "prog_tag:\t%s\n"
3251 			   "prog_id:\t%u\n",
3252 			   prog_tag,
3253 			   prog->aux->id);
3254 	}
3255 	if (link->ops->show_fdinfo)
3256 		link->ops->show_fdinfo(link, m);
3257 }
3258 #endif
3259 
3260 static __poll_t bpf_link_poll(struct file *file, struct poll_table_struct *pts)
3261 {
3262 	struct bpf_link *link = file->private_data;
3263 
3264 	return link->ops->poll(file, pts);
3265 }
3266 
3267 static const struct file_operations bpf_link_fops = {
3268 #ifdef CONFIG_PROC_FS
3269 	.show_fdinfo	= bpf_link_show_fdinfo,
3270 #endif
3271 	.release	= bpf_link_release,
3272 	.read		= bpf_dummy_read,
3273 	.write		= bpf_dummy_write,
3274 };
3275 
3276 static const struct file_operations bpf_link_fops_poll = {
3277 #ifdef CONFIG_PROC_FS
3278 	.show_fdinfo	= bpf_link_show_fdinfo,
3279 #endif
3280 	.release	= bpf_link_release,
3281 	.read		= bpf_dummy_read,
3282 	.write		= bpf_dummy_write,
3283 	.poll		= bpf_link_poll,
3284 };
3285 
3286 static int bpf_link_alloc_id(struct bpf_link *link)
3287 {
3288 	int id;
3289 
3290 	idr_preload(GFP_KERNEL);
3291 	spin_lock_bh(&link_idr_lock);
3292 	id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
3293 	spin_unlock_bh(&link_idr_lock);
3294 	idr_preload_end();
3295 
3296 	return id;
3297 }
3298 
3299 /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
3300  * reserving unused FD and allocating ID from link_idr. This is to be paired
3301  * with bpf_link_settle() to install FD and ID and expose bpf_link to
3302  * user-space, if bpf_link is successfully attached. If not, bpf_link and
3303  * pre-allocated resources are to be freed with bpf_cleanup() call. All the
3304  * transient state is passed around in struct bpf_link_primer.
3305  * This is preferred way to create and initialize bpf_link, especially when
3306  * there are complicated and expensive operations in between creating bpf_link
3307  * itself and attaching it to BPF hook. By using bpf_link_prime() and
3308  * bpf_link_settle() kernel code using bpf_link doesn't have to perform
3309  * expensive (and potentially failing) roll back operations in a rare case
3310  * that file, FD, or ID can't be allocated.
3311  */
3312 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
3313 {
3314 	struct file *file;
3315 	int fd, id;
3316 
3317 	fd = get_unused_fd_flags(O_CLOEXEC);
3318 	if (fd < 0)
3319 		return fd;
3320 
3321 
3322 	id = bpf_link_alloc_id(link);
3323 	if (id < 0) {
3324 		put_unused_fd(fd);
3325 		return id;
3326 	}
3327 
3328 	file = anon_inode_getfile("bpf_link",
3329 				  link->ops->poll ? &bpf_link_fops_poll : &bpf_link_fops,
3330 				  link, O_CLOEXEC);
3331 	if (IS_ERR(file)) {
3332 		bpf_link_free_id(id);
3333 		put_unused_fd(fd);
3334 		return PTR_ERR(file);
3335 	}
3336 
3337 	primer->link = link;
3338 	primer->file = file;
3339 	primer->fd = fd;
3340 	primer->id = id;
3341 	return 0;
3342 }
3343 
3344 int bpf_link_settle(struct bpf_link_primer *primer)
3345 {
3346 	/* make bpf_link fetchable by ID */
3347 	spin_lock_bh(&link_idr_lock);
3348 	primer->link->id = primer->id;
3349 	spin_unlock_bh(&link_idr_lock);
3350 	/* make bpf_link fetchable by FD */
3351 	fd_install(primer->fd, primer->file);
3352 	/* pass through installed FD */
3353 	return primer->fd;
3354 }
3355 
3356 int bpf_link_new_fd(struct bpf_link *link)
3357 {
3358 	return anon_inode_getfd("bpf-link",
3359 				link->ops->poll ? &bpf_link_fops_poll : &bpf_link_fops,
3360 				link, O_CLOEXEC);
3361 }
3362 
3363 struct bpf_link *bpf_link_get_from_fd(u32 ufd)
3364 {
3365 	CLASS(fd, f)(ufd);
3366 	struct bpf_link *link;
3367 
3368 	if (fd_empty(f))
3369 		return ERR_PTR(-EBADF);
3370 	if (fd_file(f)->f_op != &bpf_link_fops && fd_file(f)->f_op != &bpf_link_fops_poll)
3371 		return ERR_PTR(-EINVAL);
3372 
3373 	link = fd_file(f)->private_data;
3374 	bpf_link_inc(link);
3375 	return link;
3376 }
3377 EXPORT_SYMBOL_NS(bpf_link_get_from_fd, "BPF_INTERNAL");
3378 
3379 static void bpf_tracing_link_release(struct bpf_link *link)
3380 {
3381 	struct bpf_tracing_link *tr_link =
3382 		container_of(link, struct bpf_tracing_link, link.link);
3383 
3384 	WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link,
3385 						tr_link->trampoline,
3386 						tr_link->tgt_prog));
3387 
3388 	bpf_trampoline_put(tr_link->trampoline);
3389 
3390 	/* tgt_prog is NULL if target is a kernel function */
3391 	if (tr_link->tgt_prog)
3392 		bpf_prog_put(tr_link->tgt_prog);
3393 }
3394 
3395 static void bpf_tracing_link_dealloc(struct bpf_link *link)
3396 {
3397 	struct bpf_tracing_link *tr_link =
3398 		container_of(link, struct bpf_tracing_link, link.link);
3399 
3400 	kfree(tr_link);
3401 }
3402 
3403 static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
3404 					 struct seq_file *seq)
3405 {
3406 	struct bpf_tracing_link *tr_link =
3407 		container_of(link, struct bpf_tracing_link, link.link);
3408 	u32 target_btf_id, target_obj_id;
3409 
3410 	bpf_trampoline_unpack_key(tr_link->trampoline->key,
3411 				  &target_obj_id, &target_btf_id);
3412 	seq_printf(seq,
3413 		   "attach_type:\t%d\n"
3414 		   "target_obj_id:\t%u\n"
3415 		   "target_btf_id:\t%u\n"
3416 		   "cookie:\t%llu\n",
3417 		   link->attach_type,
3418 		   target_obj_id,
3419 		   target_btf_id,
3420 		   tr_link->link.cookie);
3421 }
3422 
3423 static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
3424 					   struct bpf_link_info *info)
3425 {
3426 	struct bpf_tracing_link *tr_link =
3427 		container_of(link, struct bpf_tracing_link, link.link);
3428 
3429 	info->tracing.attach_type = link->attach_type;
3430 	info->tracing.cookie = tr_link->link.cookie;
3431 	bpf_trampoline_unpack_key(tr_link->trampoline->key,
3432 				  &info->tracing.target_obj_id,
3433 				  &info->tracing.target_btf_id);
3434 
3435 	return 0;
3436 }
3437 
3438 static const struct bpf_link_ops bpf_tracing_link_lops = {
3439 	.release = bpf_tracing_link_release,
3440 	.dealloc = bpf_tracing_link_dealloc,
3441 	.show_fdinfo = bpf_tracing_link_show_fdinfo,
3442 	.fill_link_info = bpf_tracing_link_fill_link_info,
3443 };
3444 
3445 static int bpf_tracing_prog_attach(struct bpf_prog *prog,
3446 				   int tgt_prog_fd,
3447 				   u32 btf_id,
3448 				   u64 bpf_cookie,
3449 				   enum bpf_attach_type attach_type)
3450 {
3451 	struct bpf_link_primer link_primer;
3452 	struct bpf_prog *tgt_prog = NULL;
3453 	struct bpf_trampoline *tr = NULL;
3454 	struct bpf_tracing_link *link;
3455 	u64 key = 0;
3456 	int err;
3457 
3458 	switch (prog->type) {
3459 	case BPF_PROG_TYPE_TRACING:
3460 		if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
3461 		    prog->expected_attach_type != BPF_TRACE_FEXIT &&
3462 		    prog->expected_attach_type != BPF_MODIFY_RETURN) {
3463 			err = -EINVAL;
3464 			goto out_put_prog;
3465 		}
3466 		break;
3467 	case BPF_PROG_TYPE_EXT:
3468 		if (prog->expected_attach_type != 0) {
3469 			err = -EINVAL;
3470 			goto out_put_prog;
3471 		}
3472 		break;
3473 	case BPF_PROG_TYPE_LSM:
3474 		if (prog->expected_attach_type != BPF_LSM_MAC) {
3475 			err = -EINVAL;
3476 			goto out_put_prog;
3477 		}
3478 		break;
3479 	default:
3480 		err = -EINVAL;
3481 		goto out_put_prog;
3482 	}
3483 
3484 	if (!!tgt_prog_fd != !!btf_id) {
3485 		err = -EINVAL;
3486 		goto out_put_prog;
3487 	}
3488 
3489 	if (tgt_prog_fd) {
3490 		/*
3491 		 * For now we only allow new targets for BPF_PROG_TYPE_EXT. If this
3492 		 * part would be changed to implement the same for
3493 		 * BPF_PROG_TYPE_TRACING, do not forget to update the way how
3494 		 * attach_tracing_prog flag is set.
3495 		 */
3496 		if (prog->type != BPF_PROG_TYPE_EXT) {
3497 			err = -EINVAL;
3498 			goto out_put_prog;
3499 		}
3500 
3501 		tgt_prog = bpf_prog_get(tgt_prog_fd);
3502 		if (IS_ERR(tgt_prog)) {
3503 			err = PTR_ERR(tgt_prog);
3504 			tgt_prog = NULL;
3505 			goto out_put_prog;
3506 		}
3507 
3508 		key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
3509 	}
3510 
3511 	link = kzalloc(sizeof(*link), GFP_USER);
3512 	if (!link) {
3513 		err = -ENOMEM;
3514 		goto out_put_prog;
3515 	}
3516 	bpf_link_init(&link->link.link, BPF_LINK_TYPE_TRACING,
3517 		      &bpf_tracing_link_lops, prog, attach_type);
3518 
3519 	link->link.cookie = bpf_cookie;
3520 
3521 	mutex_lock(&prog->aux->dst_mutex);
3522 
3523 	/* There are a few possible cases here:
3524 	 *
3525 	 * - if prog->aux->dst_trampoline is set, the program was just loaded
3526 	 *   and not yet attached to anything, so we can use the values stored
3527 	 *   in prog->aux
3528 	 *
3529 	 * - if prog->aux->dst_trampoline is NULL, the program has already been
3530 	 *   attached to a target and its initial target was cleared (below)
3531 	 *
3532 	 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
3533 	 *   target_btf_id using the link_create API.
3534 	 *
3535 	 * - if tgt_prog == NULL when this function was called using the old
3536 	 *   raw_tracepoint_open API, and we need a target from prog->aux
3537 	 *
3538 	 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
3539 	 *   was detached and is going for re-attachment.
3540 	 *
3541 	 * - if prog->aux->dst_trampoline is NULL and tgt_prog and prog->aux->attach_btf
3542 	 *   are NULL, then program was already attached and user did not provide
3543 	 *   tgt_prog_fd so we have no way to find out or create trampoline
3544 	 */
3545 	if (!prog->aux->dst_trampoline && !tgt_prog) {
3546 		/*
3547 		 * Allow re-attach for TRACING and LSM programs. If it's
3548 		 * currently linked, bpf_trampoline_link_prog will fail.
3549 		 * EXT programs need to specify tgt_prog_fd, so they
3550 		 * re-attach in separate code path.
3551 		 */
3552 		if (prog->type != BPF_PROG_TYPE_TRACING &&
3553 		    prog->type != BPF_PROG_TYPE_LSM) {
3554 			err = -EINVAL;
3555 			goto out_unlock;
3556 		}
3557 		/* We can allow re-attach only if we have valid attach_btf. */
3558 		if (!prog->aux->attach_btf) {
3559 			err = -EINVAL;
3560 			goto out_unlock;
3561 		}
3562 		btf_id = prog->aux->attach_btf_id;
3563 		key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
3564 	}
3565 
3566 	if (!prog->aux->dst_trampoline ||
3567 	    (key && key != prog->aux->dst_trampoline->key)) {
3568 		/* If there is no saved target, or the specified target is
3569 		 * different from the destination specified at load time, we
3570 		 * need a new trampoline and a check for compatibility
3571 		 */
3572 		struct bpf_attach_target_info tgt_info = {};
3573 
3574 		err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
3575 					      &tgt_info);
3576 		if (err)
3577 			goto out_unlock;
3578 
3579 		if (tgt_info.tgt_mod) {
3580 			module_put(prog->aux->mod);
3581 			prog->aux->mod = tgt_info.tgt_mod;
3582 		}
3583 
3584 		tr = bpf_trampoline_get(key, &tgt_info);
3585 		if (!tr) {
3586 			err = -ENOMEM;
3587 			goto out_unlock;
3588 		}
3589 	} else {
3590 		/* The caller didn't specify a target, or the target was the
3591 		 * same as the destination supplied during program load. This
3592 		 * means we can reuse the trampoline and reference from program
3593 		 * load time, and there is no need to allocate a new one. This
3594 		 * can only happen once for any program, as the saved values in
3595 		 * prog->aux are cleared below.
3596 		 */
3597 		tr = prog->aux->dst_trampoline;
3598 		tgt_prog = prog->aux->dst_prog;
3599 	}
3600 
3601 	err = bpf_link_prime(&link->link.link, &link_primer);
3602 	if (err)
3603 		goto out_unlock;
3604 
3605 	err = bpf_trampoline_link_prog(&link->link, tr, tgt_prog);
3606 	if (err) {
3607 		bpf_link_cleanup(&link_primer);
3608 		link = NULL;
3609 		goto out_unlock;
3610 	}
3611 
3612 	link->tgt_prog = tgt_prog;
3613 	link->trampoline = tr;
3614 
3615 	/* Always clear the trampoline and target prog from prog->aux to make
3616 	 * sure the original attach destination is not kept alive after a
3617 	 * program is (re-)attached to another target.
3618 	 */
3619 	if (prog->aux->dst_prog &&
3620 	    (tgt_prog_fd || tr != prog->aux->dst_trampoline))
3621 		/* got extra prog ref from syscall, or attaching to different prog */
3622 		bpf_prog_put(prog->aux->dst_prog);
3623 	if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
3624 		/* we allocated a new trampoline, so free the old one */
3625 		bpf_trampoline_put(prog->aux->dst_trampoline);
3626 
3627 	prog->aux->dst_prog = NULL;
3628 	prog->aux->dst_trampoline = NULL;
3629 	mutex_unlock(&prog->aux->dst_mutex);
3630 
3631 	return bpf_link_settle(&link_primer);
3632 out_unlock:
3633 	if (tr && tr != prog->aux->dst_trampoline)
3634 		bpf_trampoline_put(tr);
3635 	mutex_unlock(&prog->aux->dst_mutex);
3636 	kfree(link);
3637 out_put_prog:
3638 	if (tgt_prog_fd && tgt_prog)
3639 		bpf_prog_put(tgt_prog);
3640 	return err;
3641 }
3642 
3643 static void bpf_raw_tp_link_release(struct bpf_link *link)
3644 {
3645 	struct bpf_raw_tp_link *raw_tp =
3646 		container_of(link, struct bpf_raw_tp_link, link);
3647 
3648 	bpf_probe_unregister(raw_tp->btp, raw_tp);
3649 	bpf_put_raw_tracepoint(raw_tp->btp);
3650 }
3651 
3652 static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
3653 {
3654 	struct bpf_raw_tp_link *raw_tp =
3655 		container_of(link, struct bpf_raw_tp_link, link);
3656 
3657 	kfree(raw_tp);
3658 }
3659 
3660 static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
3661 					struct seq_file *seq)
3662 {
3663 	struct bpf_raw_tp_link *raw_tp_link =
3664 		container_of(link, struct bpf_raw_tp_link, link);
3665 
3666 	seq_printf(seq,
3667 		   "tp_name:\t%s\n"
3668 		   "cookie:\t%llu\n",
3669 		   raw_tp_link->btp->tp->name,
3670 		   raw_tp_link->cookie);
3671 }
3672 
3673 static int bpf_copy_to_user(char __user *ubuf, const char *buf, u32 ulen,
3674 			    u32 len)
3675 {
3676 	if (ulen >= len + 1) {
3677 		if (copy_to_user(ubuf, buf, len + 1))
3678 			return -EFAULT;
3679 	} else {
3680 		char zero = '\0';
3681 
3682 		if (copy_to_user(ubuf, buf, ulen - 1))
3683 			return -EFAULT;
3684 		if (put_user(zero, ubuf + ulen - 1))
3685 			return -EFAULT;
3686 		return -ENOSPC;
3687 	}
3688 
3689 	return 0;
3690 }
3691 
3692 static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
3693 					  struct bpf_link_info *info)
3694 {
3695 	struct bpf_raw_tp_link *raw_tp_link =
3696 		container_of(link, struct bpf_raw_tp_link, link);
3697 	char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
3698 	const char *tp_name = raw_tp_link->btp->tp->name;
3699 	u32 ulen = info->raw_tracepoint.tp_name_len;
3700 	size_t tp_len = strlen(tp_name);
3701 
3702 	if (!ulen ^ !ubuf)
3703 		return -EINVAL;
3704 
3705 	info->raw_tracepoint.tp_name_len = tp_len + 1;
3706 	info->raw_tracepoint.cookie = raw_tp_link->cookie;
3707 
3708 	if (!ubuf)
3709 		return 0;
3710 
3711 	return bpf_copy_to_user(ubuf, tp_name, ulen, tp_len);
3712 }
3713 
3714 static const struct bpf_link_ops bpf_raw_tp_link_lops = {
3715 	.release = bpf_raw_tp_link_release,
3716 	.dealloc_deferred = bpf_raw_tp_link_dealloc,
3717 	.show_fdinfo = bpf_raw_tp_link_show_fdinfo,
3718 	.fill_link_info = bpf_raw_tp_link_fill_link_info,
3719 };
3720 
3721 #ifdef CONFIG_PERF_EVENTS
3722 struct bpf_perf_link {
3723 	struct bpf_link link;
3724 	struct file *perf_file;
3725 };
3726 
3727 static void bpf_perf_link_release(struct bpf_link *link)
3728 {
3729 	struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3730 	struct perf_event *event = perf_link->perf_file->private_data;
3731 
3732 	perf_event_free_bpf_prog(event);
3733 	fput(perf_link->perf_file);
3734 }
3735 
3736 static void bpf_perf_link_dealloc(struct bpf_link *link)
3737 {
3738 	struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3739 
3740 	kfree(perf_link);
3741 }
3742 
3743 static int bpf_perf_link_fill_common(const struct perf_event *event,
3744 				     char __user *uname, u32 *ulenp,
3745 				     u64 *probe_offset, u64 *probe_addr,
3746 				     u32 *fd_type, unsigned long *missed)
3747 {
3748 	const char *buf;
3749 	u32 prog_id, ulen;
3750 	size_t len;
3751 	int err;
3752 
3753 	ulen = *ulenp;
3754 	if (!ulen ^ !uname)
3755 		return -EINVAL;
3756 
3757 	err = bpf_get_perf_event_info(event, &prog_id, fd_type, &buf,
3758 				      probe_offset, probe_addr, missed);
3759 	if (err)
3760 		return err;
3761 
3762 	if (buf) {
3763 		len = strlen(buf);
3764 		*ulenp = len + 1;
3765 	} else {
3766 		*ulenp = 1;
3767 	}
3768 	if (!uname)
3769 		return 0;
3770 
3771 	if (buf) {
3772 		err = bpf_copy_to_user(uname, buf, ulen, len);
3773 		if (err)
3774 			return err;
3775 	} else {
3776 		char zero = '\0';
3777 
3778 		if (put_user(zero, uname))
3779 			return -EFAULT;
3780 	}
3781 	return 0;
3782 }
3783 
3784 #ifdef CONFIG_KPROBE_EVENTS
3785 static int bpf_perf_link_fill_kprobe(const struct perf_event *event,
3786 				     struct bpf_link_info *info)
3787 {
3788 	unsigned long missed;
3789 	char __user *uname;
3790 	u64 addr, offset;
3791 	u32 ulen, type;
3792 	int err;
3793 
3794 	uname = u64_to_user_ptr(info->perf_event.kprobe.func_name);
3795 	ulen = info->perf_event.kprobe.name_len;
3796 	err = bpf_perf_link_fill_common(event, uname, &ulen, &offset, &addr,
3797 					&type, &missed);
3798 	if (err)
3799 		return err;
3800 	if (type == BPF_FD_TYPE_KRETPROBE)
3801 		info->perf_event.type = BPF_PERF_EVENT_KRETPROBE;
3802 	else
3803 		info->perf_event.type = BPF_PERF_EVENT_KPROBE;
3804 	info->perf_event.kprobe.name_len = ulen;
3805 	info->perf_event.kprobe.offset = offset;
3806 	info->perf_event.kprobe.missed = missed;
3807 	if (!kallsyms_show_value(current_cred()))
3808 		addr = 0;
3809 	info->perf_event.kprobe.addr = addr;
3810 	info->perf_event.kprobe.cookie = event->bpf_cookie;
3811 	return 0;
3812 }
3813 
3814 static void bpf_perf_link_fdinfo_kprobe(const struct perf_event *event,
3815 					struct seq_file *seq)
3816 {
3817 	const char *name;
3818 	int err;
3819 	u32 prog_id, type;
3820 	u64 offset, addr;
3821 	unsigned long missed;
3822 
3823 	err = bpf_get_perf_event_info(event, &prog_id, &type, &name,
3824 				      &offset, &addr, &missed);
3825 	if (err)
3826 		return;
3827 
3828 	seq_printf(seq,
3829 		   "name:\t%s\n"
3830 		   "offset:\t%#llx\n"
3831 		   "missed:\t%lu\n"
3832 		   "addr:\t%#llx\n"
3833 		   "event_type:\t%s\n"
3834 		   "cookie:\t%llu\n",
3835 		   name, offset, missed, addr,
3836 		   type == BPF_FD_TYPE_KRETPROBE ?  "kretprobe" : "kprobe",
3837 		   event->bpf_cookie);
3838 }
3839 #endif
3840 
3841 #ifdef CONFIG_UPROBE_EVENTS
3842 static int bpf_perf_link_fill_uprobe(const struct perf_event *event,
3843 				     struct bpf_link_info *info)
3844 {
3845 	u64 ref_ctr_offset, offset;
3846 	char __user *uname;
3847 	u32 ulen, type;
3848 	int err;
3849 
3850 	uname = u64_to_user_ptr(info->perf_event.uprobe.file_name);
3851 	ulen = info->perf_event.uprobe.name_len;
3852 	err = bpf_perf_link_fill_common(event, uname, &ulen, &offset, &ref_ctr_offset,
3853 					&type, NULL);
3854 	if (err)
3855 		return err;
3856 
3857 	if (type == BPF_FD_TYPE_URETPROBE)
3858 		info->perf_event.type = BPF_PERF_EVENT_URETPROBE;
3859 	else
3860 		info->perf_event.type = BPF_PERF_EVENT_UPROBE;
3861 	info->perf_event.uprobe.name_len = ulen;
3862 	info->perf_event.uprobe.offset = offset;
3863 	info->perf_event.uprobe.cookie = event->bpf_cookie;
3864 	info->perf_event.uprobe.ref_ctr_offset = ref_ctr_offset;
3865 	return 0;
3866 }
3867 
3868 static void bpf_perf_link_fdinfo_uprobe(const struct perf_event *event,
3869 					struct seq_file *seq)
3870 {
3871 	const char *name;
3872 	int err;
3873 	u32 prog_id, type;
3874 	u64 offset, ref_ctr_offset;
3875 	unsigned long missed;
3876 
3877 	err = bpf_get_perf_event_info(event, &prog_id, &type, &name,
3878 				      &offset, &ref_ctr_offset, &missed);
3879 	if (err)
3880 		return;
3881 
3882 	seq_printf(seq,
3883 		   "name:\t%s\n"
3884 		   "offset:\t%#llx\n"
3885 		   "ref_ctr_offset:\t%#llx\n"
3886 		   "event_type:\t%s\n"
3887 		   "cookie:\t%llu\n",
3888 		   name, offset, ref_ctr_offset,
3889 		   type == BPF_FD_TYPE_URETPROBE ?  "uretprobe" : "uprobe",
3890 		   event->bpf_cookie);
3891 }
3892 #endif
3893 
3894 static int bpf_perf_link_fill_probe(const struct perf_event *event,
3895 				    struct bpf_link_info *info)
3896 {
3897 #ifdef CONFIG_KPROBE_EVENTS
3898 	if (event->tp_event->flags & TRACE_EVENT_FL_KPROBE)
3899 		return bpf_perf_link_fill_kprobe(event, info);
3900 #endif
3901 #ifdef CONFIG_UPROBE_EVENTS
3902 	if (event->tp_event->flags & TRACE_EVENT_FL_UPROBE)
3903 		return bpf_perf_link_fill_uprobe(event, info);
3904 #endif
3905 	return -EOPNOTSUPP;
3906 }
3907 
3908 static int bpf_perf_link_fill_tracepoint(const struct perf_event *event,
3909 					 struct bpf_link_info *info)
3910 {
3911 	char __user *uname;
3912 	u32 ulen;
3913 	int err;
3914 
3915 	uname = u64_to_user_ptr(info->perf_event.tracepoint.tp_name);
3916 	ulen = info->perf_event.tracepoint.name_len;
3917 	err = bpf_perf_link_fill_common(event, uname, &ulen, NULL, NULL, NULL, NULL);
3918 	if (err)
3919 		return err;
3920 
3921 	info->perf_event.type = BPF_PERF_EVENT_TRACEPOINT;
3922 	info->perf_event.tracepoint.name_len = ulen;
3923 	info->perf_event.tracepoint.cookie = event->bpf_cookie;
3924 	return 0;
3925 }
3926 
3927 static int bpf_perf_link_fill_perf_event(const struct perf_event *event,
3928 					 struct bpf_link_info *info)
3929 {
3930 	info->perf_event.event.type = event->attr.type;
3931 	info->perf_event.event.config = event->attr.config;
3932 	info->perf_event.event.cookie = event->bpf_cookie;
3933 	info->perf_event.type = BPF_PERF_EVENT_EVENT;
3934 	return 0;
3935 }
3936 
3937 static int bpf_perf_link_fill_link_info(const struct bpf_link *link,
3938 					struct bpf_link_info *info)
3939 {
3940 	struct bpf_perf_link *perf_link;
3941 	const struct perf_event *event;
3942 
3943 	perf_link = container_of(link, struct bpf_perf_link, link);
3944 	event = perf_get_event(perf_link->perf_file);
3945 	if (IS_ERR(event))
3946 		return PTR_ERR(event);
3947 
3948 	switch (event->prog->type) {
3949 	case BPF_PROG_TYPE_PERF_EVENT:
3950 		return bpf_perf_link_fill_perf_event(event, info);
3951 	case BPF_PROG_TYPE_TRACEPOINT:
3952 		return bpf_perf_link_fill_tracepoint(event, info);
3953 	case BPF_PROG_TYPE_KPROBE:
3954 		return bpf_perf_link_fill_probe(event, info);
3955 	default:
3956 		return -EOPNOTSUPP;
3957 	}
3958 }
3959 
3960 static void bpf_perf_event_link_show_fdinfo(const struct perf_event *event,
3961 					    struct seq_file *seq)
3962 {
3963 	seq_printf(seq,
3964 		   "type:\t%u\n"
3965 		   "config:\t%llu\n"
3966 		   "event_type:\t%s\n"
3967 		   "cookie:\t%llu\n",
3968 		   event->attr.type, event->attr.config,
3969 		   "event", event->bpf_cookie);
3970 }
3971 
3972 static void bpf_tracepoint_link_show_fdinfo(const struct perf_event *event,
3973 					    struct seq_file *seq)
3974 {
3975 	int err;
3976 	const char *name;
3977 	u32 prog_id;
3978 
3979 	err = bpf_get_perf_event_info(event, &prog_id, NULL, &name, NULL,
3980 				      NULL, NULL);
3981 	if (err)
3982 		return;
3983 
3984 	seq_printf(seq,
3985 		   "tp_name:\t%s\n"
3986 		   "event_type:\t%s\n"
3987 		   "cookie:\t%llu\n",
3988 		   name, "tracepoint", event->bpf_cookie);
3989 }
3990 
3991 static void bpf_probe_link_show_fdinfo(const struct perf_event *event,
3992 				       struct seq_file *seq)
3993 {
3994 #ifdef CONFIG_KPROBE_EVENTS
3995 	if (event->tp_event->flags & TRACE_EVENT_FL_KPROBE)
3996 		return bpf_perf_link_fdinfo_kprobe(event, seq);
3997 #endif
3998 
3999 #ifdef CONFIG_UPROBE_EVENTS
4000 	if (event->tp_event->flags & TRACE_EVENT_FL_UPROBE)
4001 		return bpf_perf_link_fdinfo_uprobe(event, seq);
4002 #endif
4003 }
4004 
4005 static void bpf_perf_link_show_fdinfo(const struct bpf_link *link,
4006 				      struct seq_file *seq)
4007 {
4008 	struct bpf_perf_link *perf_link;
4009 	const struct perf_event *event;
4010 
4011 	perf_link = container_of(link, struct bpf_perf_link, link);
4012 	event = perf_get_event(perf_link->perf_file);
4013 	if (IS_ERR(event))
4014 		return;
4015 
4016 	switch (event->prog->type) {
4017 	case BPF_PROG_TYPE_PERF_EVENT:
4018 		return bpf_perf_event_link_show_fdinfo(event, seq);
4019 	case BPF_PROG_TYPE_TRACEPOINT:
4020 		return bpf_tracepoint_link_show_fdinfo(event, seq);
4021 	case BPF_PROG_TYPE_KPROBE:
4022 		return bpf_probe_link_show_fdinfo(event, seq);
4023 	default:
4024 		return;
4025 	}
4026 }
4027 
4028 static const struct bpf_link_ops bpf_perf_link_lops = {
4029 	.release = bpf_perf_link_release,
4030 	.dealloc = bpf_perf_link_dealloc,
4031 	.fill_link_info = bpf_perf_link_fill_link_info,
4032 	.show_fdinfo = bpf_perf_link_show_fdinfo,
4033 };
4034 
4035 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
4036 {
4037 	struct bpf_link_primer link_primer;
4038 	struct bpf_perf_link *link;
4039 	struct perf_event *event;
4040 	struct file *perf_file;
4041 	int err;
4042 
4043 	if (attr->link_create.flags)
4044 		return -EINVAL;
4045 
4046 	perf_file = perf_event_get(attr->link_create.target_fd);
4047 	if (IS_ERR(perf_file))
4048 		return PTR_ERR(perf_file);
4049 
4050 	link = kzalloc(sizeof(*link), GFP_USER);
4051 	if (!link) {
4052 		err = -ENOMEM;
4053 		goto out_put_file;
4054 	}
4055 	bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog,
4056 		      attr->link_create.attach_type);
4057 	link->perf_file = perf_file;
4058 
4059 	err = bpf_link_prime(&link->link, &link_primer);
4060 	if (err) {
4061 		kfree(link);
4062 		goto out_put_file;
4063 	}
4064 
4065 	event = perf_file->private_data;
4066 	err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
4067 	if (err) {
4068 		bpf_link_cleanup(&link_primer);
4069 		goto out_put_file;
4070 	}
4071 	/* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
4072 	bpf_prog_inc(prog);
4073 
4074 	return bpf_link_settle(&link_primer);
4075 
4076 out_put_file:
4077 	fput(perf_file);
4078 	return err;
4079 }
4080 #else
4081 static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
4082 {
4083 	return -EOPNOTSUPP;
4084 }
4085 #endif /* CONFIG_PERF_EVENTS */
4086 
4087 static int bpf_raw_tp_link_attach(struct bpf_prog *prog,
4088 				  const char __user *user_tp_name, u64 cookie,
4089 				  enum bpf_attach_type attach_type)
4090 {
4091 	struct bpf_link_primer link_primer;
4092 	struct bpf_raw_tp_link *link;
4093 	struct bpf_raw_event_map *btp;
4094 	const char *tp_name;
4095 	char buf[128];
4096 	int err;
4097 
4098 	switch (prog->type) {
4099 	case BPF_PROG_TYPE_TRACING:
4100 	case BPF_PROG_TYPE_EXT:
4101 	case BPF_PROG_TYPE_LSM:
4102 		if (user_tp_name)
4103 			/* The attach point for this category of programs
4104 			 * should be specified via btf_id during program load.
4105 			 */
4106 			return -EINVAL;
4107 		if (prog->type == BPF_PROG_TYPE_TRACING &&
4108 		    prog->expected_attach_type == BPF_TRACE_RAW_TP) {
4109 			tp_name = prog->aux->attach_func_name;
4110 			break;
4111 		}
4112 		return bpf_tracing_prog_attach(prog, 0, 0, 0, attach_type);
4113 	case BPF_PROG_TYPE_RAW_TRACEPOINT:
4114 	case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
4115 		if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0)
4116 			return -EFAULT;
4117 		buf[sizeof(buf) - 1] = 0;
4118 		tp_name = buf;
4119 		break;
4120 	default:
4121 		return -EINVAL;
4122 	}
4123 
4124 	btp = bpf_get_raw_tracepoint(tp_name);
4125 	if (!btp)
4126 		return -ENOENT;
4127 
4128 	link = kzalloc(sizeof(*link), GFP_USER);
4129 	if (!link) {
4130 		err = -ENOMEM;
4131 		goto out_put_btp;
4132 	}
4133 	bpf_link_init_sleepable(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
4134 				&bpf_raw_tp_link_lops, prog, attach_type,
4135 				tracepoint_is_faultable(btp->tp));
4136 	link->btp = btp;
4137 	link->cookie = cookie;
4138 
4139 	err = bpf_link_prime(&link->link, &link_primer);
4140 	if (err) {
4141 		kfree(link);
4142 		goto out_put_btp;
4143 	}
4144 
4145 	err = bpf_probe_register(link->btp, link);
4146 	if (err) {
4147 		bpf_link_cleanup(&link_primer);
4148 		goto out_put_btp;
4149 	}
4150 
4151 	return bpf_link_settle(&link_primer);
4152 
4153 out_put_btp:
4154 	bpf_put_raw_tracepoint(btp);
4155 	return err;
4156 }
4157 
4158 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.cookie
4159 
4160 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
4161 {
4162 	struct bpf_prog *prog;
4163 	void __user *tp_name;
4164 	__u64 cookie;
4165 	int fd;
4166 
4167 	if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
4168 		return -EINVAL;
4169 
4170 	prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
4171 	if (IS_ERR(prog))
4172 		return PTR_ERR(prog);
4173 
4174 	tp_name = u64_to_user_ptr(attr->raw_tracepoint.name);
4175 	cookie = attr->raw_tracepoint.cookie;
4176 	fd = bpf_raw_tp_link_attach(prog, tp_name, cookie, prog->expected_attach_type);
4177 	if (fd < 0)
4178 		bpf_prog_put(prog);
4179 	return fd;
4180 }
4181 
4182 static enum bpf_prog_type
4183 attach_type_to_prog_type(enum bpf_attach_type attach_type)
4184 {
4185 	switch (attach_type) {
4186 	case BPF_CGROUP_INET_INGRESS:
4187 	case BPF_CGROUP_INET_EGRESS:
4188 		return BPF_PROG_TYPE_CGROUP_SKB;
4189 	case BPF_CGROUP_INET_SOCK_CREATE:
4190 	case BPF_CGROUP_INET_SOCK_RELEASE:
4191 	case BPF_CGROUP_INET4_POST_BIND:
4192 	case BPF_CGROUP_INET6_POST_BIND:
4193 		return BPF_PROG_TYPE_CGROUP_SOCK;
4194 	case BPF_CGROUP_INET4_BIND:
4195 	case BPF_CGROUP_INET6_BIND:
4196 	case BPF_CGROUP_INET4_CONNECT:
4197 	case BPF_CGROUP_INET6_CONNECT:
4198 	case BPF_CGROUP_UNIX_CONNECT:
4199 	case BPF_CGROUP_INET4_GETPEERNAME:
4200 	case BPF_CGROUP_INET6_GETPEERNAME:
4201 	case BPF_CGROUP_UNIX_GETPEERNAME:
4202 	case BPF_CGROUP_INET4_GETSOCKNAME:
4203 	case BPF_CGROUP_INET6_GETSOCKNAME:
4204 	case BPF_CGROUP_UNIX_GETSOCKNAME:
4205 	case BPF_CGROUP_UDP4_SENDMSG:
4206 	case BPF_CGROUP_UDP6_SENDMSG:
4207 	case BPF_CGROUP_UNIX_SENDMSG:
4208 	case BPF_CGROUP_UDP4_RECVMSG:
4209 	case BPF_CGROUP_UDP6_RECVMSG:
4210 	case BPF_CGROUP_UNIX_RECVMSG:
4211 		return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
4212 	case BPF_CGROUP_SOCK_OPS:
4213 		return BPF_PROG_TYPE_SOCK_OPS;
4214 	case BPF_CGROUP_DEVICE:
4215 		return BPF_PROG_TYPE_CGROUP_DEVICE;
4216 	case BPF_SK_MSG_VERDICT:
4217 		return BPF_PROG_TYPE_SK_MSG;
4218 	case BPF_SK_SKB_STREAM_PARSER:
4219 	case BPF_SK_SKB_STREAM_VERDICT:
4220 	case BPF_SK_SKB_VERDICT:
4221 		return BPF_PROG_TYPE_SK_SKB;
4222 	case BPF_LIRC_MODE2:
4223 		return BPF_PROG_TYPE_LIRC_MODE2;
4224 	case BPF_FLOW_DISSECTOR:
4225 		return BPF_PROG_TYPE_FLOW_DISSECTOR;
4226 	case BPF_CGROUP_SYSCTL:
4227 		return BPF_PROG_TYPE_CGROUP_SYSCTL;
4228 	case BPF_CGROUP_GETSOCKOPT:
4229 	case BPF_CGROUP_SETSOCKOPT:
4230 		return BPF_PROG_TYPE_CGROUP_SOCKOPT;
4231 	case BPF_TRACE_ITER:
4232 	case BPF_TRACE_RAW_TP:
4233 	case BPF_TRACE_FENTRY:
4234 	case BPF_TRACE_FEXIT:
4235 	case BPF_MODIFY_RETURN:
4236 		return BPF_PROG_TYPE_TRACING;
4237 	case BPF_LSM_MAC:
4238 		return BPF_PROG_TYPE_LSM;
4239 	case BPF_SK_LOOKUP:
4240 		return BPF_PROG_TYPE_SK_LOOKUP;
4241 	case BPF_XDP:
4242 		return BPF_PROG_TYPE_XDP;
4243 	case BPF_LSM_CGROUP:
4244 		return BPF_PROG_TYPE_LSM;
4245 	case BPF_TCX_INGRESS:
4246 	case BPF_TCX_EGRESS:
4247 	case BPF_NETKIT_PRIMARY:
4248 	case BPF_NETKIT_PEER:
4249 		return BPF_PROG_TYPE_SCHED_CLS;
4250 	default:
4251 		return BPF_PROG_TYPE_UNSPEC;
4252 	}
4253 }
4254 
4255 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
4256 					     enum bpf_attach_type attach_type)
4257 {
4258 	enum bpf_prog_type ptype;
4259 
4260 	switch (prog->type) {
4261 	case BPF_PROG_TYPE_CGROUP_SOCK:
4262 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4263 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4264 	case BPF_PROG_TYPE_SK_LOOKUP:
4265 		return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
4266 	case BPF_PROG_TYPE_CGROUP_SKB:
4267 		if (!bpf_token_capable(prog->aux->token, CAP_NET_ADMIN))
4268 			/* cg-skb progs can be loaded by unpriv user.
4269 			 * check permissions at attach time.
4270 			 */
4271 			return -EPERM;
4272 
4273 		ptype = attach_type_to_prog_type(attach_type);
4274 		if (prog->type != ptype)
4275 			return -EINVAL;
4276 
4277 		return prog->enforce_expected_attach_type &&
4278 			prog->expected_attach_type != attach_type ?
4279 			-EINVAL : 0;
4280 	case BPF_PROG_TYPE_EXT:
4281 		return 0;
4282 	case BPF_PROG_TYPE_NETFILTER:
4283 		if (attach_type != BPF_NETFILTER)
4284 			return -EINVAL;
4285 		return 0;
4286 	case BPF_PROG_TYPE_PERF_EVENT:
4287 	case BPF_PROG_TYPE_TRACEPOINT:
4288 		if (attach_type != BPF_PERF_EVENT)
4289 			return -EINVAL;
4290 		return 0;
4291 	case BPF_PROG_TYPE_KPROBE:
4292 		if (prog->expected_attach_type == BPF_TRACE_KPROBE_MULTI &&
4293 		    attach_type != BPF_TRACE_KPROBE_MULTI)
4294 			return -EINVAL;
4295 		if (prog->expected_attach_type == BPF_TRACE_KPROBE_SESSION &&
4296 		    attach_type != BPF_TRACE_KPROBE_SESSION)
4297 			return -EINVAL;
4298 		if (prog->expected_attach_type == BPF_TRACE_UPROBE_MULTI &&
4299 		    attach_type != BPF_TRACE_UPROBE_MULTI)
4300 			return -EINVAL;
4301 		if (prog->expected_attach_type == BPF_TRACE_UPROBE_SESSION &&
4302 		    attach_type != BPF_TRACE_UPROBE_SESSION)
4303 			return -EINVAL;
4304 		if (attach_type != BPF_PERF_EVENT &&
4305 		    attach_type != BPF_TRACE_KPROBE_MULTI &&
4306 		    attach_type != BPF_TRACE_KPROBE_SESSION &&
4307 		    attach_type != BPF_TRACE_UPROBE_MULTI &&
4308 		    attach_type != BPF_TRACE_UPROBE_SESSION)
4309 			return -EINVAL;
4310 		return 0;
4311 	case BPF_PROG_TYPE_SCHED_CLS:
4312 		if (attach_type != BPF_TCX_INGRESS &&
4313 		    attach_type != BPF_TCX_EGRESS &&
4314 		    attach_type != BPF_NETKIT_PRIMARY &&
4315 		    attach_type != BPF_NETKIT_PEER)
4316 			return -EINVAL;
4317 		return 0;
4318 	default:
4319 		ptype = attach_type_to_prog_type(attach_type);
4320 		if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type)
4321 			return -EINVAL;
4322 		return 0;
4323 	}
4324 }
4325 
4326 static bool is_cgroup_prog_type(enum bpf_prog_type ptype, enum bpf_attach_type atype,
4327 				bool check_atype)
4328 {
4329 	switch (ptype) {
4330 	case BPF_PROG_TYPE_CGROUP_DEVICE:
4331 	case BPF_PROG_TYPE_CGROUP_SKB:
4332 	case BPF_PROG_TYPE_CGROUP_SOCK:
4333 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4334 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4335 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
4336 	case BPF_PROG_TYPE_SOCK_OPS:
4337 		return true;
4338 	case BPF_PROG_TYPE_LSM:
4339 		return check_atype ? atype == BPF_LSM_CGROUP : true;
4340 	default:
4341 		return false;
4342 	}
4343 }
4344 
4345 #define BPF_PROG_ATTACH_LAST_FIELD expected_revision
4346 
4347 #define BPF_F_ATTACH_MASK_BASE	\
4348 	(BPF_F_ALLOW_OVERRIDE |	\
4349 	 BPF_F_ALLOW_MULTI |	\
4350 	 BPF_F_REPLACE |	\
4351 	 BPF_F_PREORDER)
4352 
4353 #define BPF_F_ATTACH_MASK_MPROG	\
4354 	(BPF_F_REPLACE |	\
4355 	 BPF_F_BEFORE |		\
4356 	 BPF_F_AFTER |		\
4357 	 BPF_F_ID |		\
4358 	 BPF_F_LINK)
4359 
4360 static int bpf_prog_attach(const union bpf_attr *attr)
4361 {
4362 	enum bpf_prog_type ptype;
4363 	struct bpf_prog *prog;
4364 	int ret;
4365 
4366 	if (CHECK_ATTR(BPF_PROG_ATTACH))
4367 		return -EINVAL;
4368 
4369 	ptype = attach_type_to_prog_type(attr->attach_type);
4370 	if (ptype == BPF_PROG_TYPE_UNSPEC)
4371 		return -EINVAL;
4372 	if (bpf_mprog_supported(ptype)) {
4373 		if (attr->attach_flags & ~BPF_F_ATTACH_MASK_MPROG)
4374 			return -EINVAL;
4375 	} else if (is_cgroup_prog_type(ptype, 0, false)) {
4376 		if (attr->attach_flags & ~(BPF_F_ATTACH_MASK_BASE | BPF_F_ATTACH_MASK_MPROG))
4377 			return -EINVAL;
4378 	} else {
4379 		if (attr->attach_flags & ~BPF_F_ATTACH_MASK_BASE)
4380 			return -EINVAL;
4381 		if (attr->relative_fd ||
4382 		    attr->expected_revision)
4383 			return -EINVAL;
4384 	}
4385 
4386 	prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
4387 	if (IS_ERR(prog))
4388 		return PTR_ERR(prog);
4389 
4390 	if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
4391 		bpf_prog_put(prog);
4392 		return -EINVAL;
4393 	}
4394 
4395 	if (is_cgroup_prog_type(ptype, prog->expected_attach_type, true)) {
4396 		ret = cgroup_bpf_prog_attach(attr, ptype, prog);
4397 		goto out;
4398 	}
4399 
4400 	switch (ptype) {
4401 	case BPF_PROG_TYPE_SK_SKB:
4402 	case BPF_PROG_TYPE_SK_MSG:
4403 		ret = sock_map_get_from_fd(attr, prog);
4404 		break;
4405 	case BPF_PROG_TYPE_LIRC_MODE2:
4406 		ret = lirc_prog_attach(attr, prog);
4407 		break;
4408 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
4409 		ret = netns_bpf_prog_attach(attr, prog);
4410 		break;
4411 	case BPF_PROG_TYPE_SCHED_CLS:
4412 		if (attr->attach_type == BPF_TCX_INGRESS ||
4413 		    attr->attach_type == BPF_TCX_EGRESS)
4414 			ret = tcx_prog_attach(attr, prog);
4415 		else
4416 			ret = netkit_prog_attach(attr, prog);
4417 		break;
4418 	default:
4419 		ret = -EINVAL;
4420 	}
4421 out:
4422 	if (ret)
4423 		bpf_prog_put(prog);
4424 	return ret;
4425 }
4426 
4427 #define BPF_PROG_DETACH_LAST_FIELD expected_revision
4428 
4429 static int bpf_prog_detach(const union bpf_attr *attr)
4430 {
4431 	struct bpf_prog *prog = NULL;
4432 	enum bpf_prog_type ptype;
4433 	int ret;
4434 
4435 	if (CHECK_ATTR(BPF_PROG_DETACH))
4436 		return -EINVAL;
4437 
4438 	ptype = attach_type_to_prog_type(attr->attach_type);
4439 	if (bpf_mprog_supported(ptype)) {
4440 		if (ptype == BPF_PROG_TYPE_UNSPEC)
4441 			return -EINVAL;
4442 		if (attr->attach_flags & ~BPF_F_ATTACH_MASK_MPROG)
4443 			return -EINVAL;
4444 		if (attr->attach_bpf_fd) {
4445 			prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
4446 			if (IS_ERR(prog))
4447 				return PTR_ERR(prog);
4448 		}
4449 	} else if (is_cgroup_prog_type(ptype, 0, false)) {
4450 		if (attr->attach_flags || attr->relative_fd)
4451 			return -EINVAL;
4452 	} else if (attr->attach_flags ||
4453 		   attr->relative_fd ||
4454 		   attr->expected_revision) {
4455 		return -EINVAL;
4456 	}
4457 
4458 	switch (ptype) {
4459 	case BPF_PROG_TYPE_SK_MSG:
4460 	case BPF_PROG_TYPE_SK_SKB:
4461 		ret = sock_map_prog_detach(attr, ptype);
4462 		break;
4463 	case BPF_PROG_TYPE_LIRC_MODE2:
4464 		ret = lirc_prog_detach(attr);
4465 		break;
4466 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
4467 		ret = netns_bpf_prog_detach(attr, ptype);
4468 		break;
4469 	case BPF_PROG_TYPE_CGROUP_DEVICE:
4470 	case BPF_PROG_TYPE_CGROUP_SKB:
4471 	case BPF_PROG_TYPE_CGROUP_SOCK:
4472 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4473 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4474 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
4475 	case BPF_PROG_TYPE_SOCK_OPS:
4476 	case BPF_PROG_TYPE_LSM:
4477 		ret = cgroup_bpf_prog_detach(attr, ptype);
4478 		break;
4479 	case BPF_PROG_TYPE_SCHED_CLS:
4480 		if (attr->attach_type == BPF_TCX_INGRESS ||
4481 		    attr->attach_type == BPF_TCX_EGRESS)
4482 			ret = tcx_prog_detach(attr, prog);
4483 		else
4484 			ret = netkit_prog_detach(attr, prog);
4485 		break;
4486 	default:
4487 		ret = -EINVAL;
4488 	}
4489 
4490 	if (prog)
4491 		bpf_prog_put(prog);
4492 	return ret;
4493 }
4494 
4495 #define BPF_PROG_QUERY_LAST_FIELD query.revision
4496 
4497 static int bpf_prog_query(const union bpf_attr *attr,
4498 			  union bpf_attr __user *uattr)
4499 {
4500 	if (!bpf_net_capable())
4501 		return -EPERM;
4502 	if (CHECK_ATTR(BPF_PROG_QUERY))
4503 		return -EINVAL;
4504 	if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
4505 		return -EINVAL;
4506 
4507 	switch (attr->query.attach_type) {
4508 	case BPF_CGROUP_INET_INGRESS:
4509 	case BPF_CGROUP_INET_EGRESS:
4510 	case BPF_CGROUP_INET_SOCK_CREATE:
4511 	case BPF_CGROUP_INET_SOCK_RELEASE:
4512 	case BPF_CGROUP_INET4_BIND:
4513 	case BPF_CGROUP_INET6_BIND:
4514 	case BPF_CGROUP_INET4_POST_BIND:
4515 	case BPF_CGROUP_INET6_POST_BIND:
4516 	case BPF_CGROUP_INET4_CONNECT:
4517 	case BPF_CGROUP_INET6_CONNECT:
4518 	case BPF_CGROUP_UNIX_CONNECT:
4519 	case BPF_CGROUP_INET4_GETPEERNAME:
4520 	case BPF_CGROUP_INET6_GETPEERNAME:
4521 	case BPF_CGROUP_UNIX_GETPEERNAME:
4522 	case BPF_CGROUP_INET4_GETSOCKNAME:
4523 	case BPF_CGROUP_INET6_GETSOCKNAME:
4524 	case BPF_CGROUP_UNIX_GETSOCKNAME:
4525 	case BPF_CGROUP_UDP4_SENDMSG:
4526 	case BPF_CGROUP_UDP6_SENDMSG:
4527 	case BPF_CGROUP_UNIX_SENDMSG:
4528 	case BPF_CGROUP_UDP4_RECVMSG:
4529 	case BPF_CGROUP_UDP6_RECVMSG:
4530 	case BPF_CGROUP_UNIX_RECVMSG:
4531 	case BPF_CGROUP_SOCK_OPS:
4532 	case BPF_CGROUP_DEVICE:
4533 	case BPF_CGROUP_SYSCTL:
4534 	case BPF_CGROUP_GETSOCKOPT:
4535 	case BPF_CGROUP_SETSOCKOPT:
4536 	case BPF_LSM_CGROUP:
4537 		return cgroup_bpf_prog_query(attr, uattr);
4538 	case BPF_LIRC_MODE2:
4539 		return lirc_prog_query(attr, uattr);
4540 	case BPF_FLOW_DISSECTOR:
4541 	case BPF_SK_LOOKUP:
4542 		return netns_bpf_prog_query(attr, uattr);
4543 	case BPF_SK_SKB_STREAM_PARSER:
4544 	case BPF_SK_SKB_STREAM_VERDICT:
4545 	case BPF_SK_MSG_VERDICT:
4546 	case BPF_SK_SKB_VERDICT:
4547 		return sock_map_bpf_prog_query(attr, uattr);
4548 	case BPF_TCX_INGRESS:
4549 	case BPF_TCX_EGRESS:
4550 		return tcx_prog_query(attr, uattr);
4551 	case BPF_NETKIT_PRIMARY:
4552 	case BPF_NETKIT_PEER:
4553 		return netkit_prog_query(attr, uattr);
4554 	default:
4555 		return -EINVAL;
4556 	}
4557 }
4558 
4559 #define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size
4560 
4561 static int bpf_prog_test_run(const union bpf_attr *attr,
4562 			     union bpf_attr __user *uattr)
4563 {
4564 	struct bpf_prog *prog;
4565 	int ret = -ENOTSUPP;
4566 
4567 	if (CHECK_ATTR(BPF_PROG_TEST_RUN))
4568 		return -EINVAL;
4569 
4570 	if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
4571 	    (!attr->test.ctx_size_in && attr->test.ctx_in))
4572 		return -EINVAL;
4573 
4574 	if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
4575 	    (!attr->test.ctx_size_out && attr->test.ctx_out))
4576 		return -EINVAL;
4577 
4578 	prog = bpf_prog_get(attr->test.prog_fd);
4579 	if (IS_ERR(prog))
4580 		return PTR_ERR(prog);
4581 
4582 	if (prog->aux->ops->test_run)
4583 		ret = prog->aux->ops->test_run(prog, attr, uattr);
4584 
4585 	bpf_prog_put(prog);
4586 	return ret;
4587 }
4588 
4589 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
4590 
4591 static int bpf_obj_get_next_id(const union bpf_attr *attr,
4592 			       union bpf_attr __user *uattr,
4593 			       struct idr *idr,
4594 			       spinlock_t *lock)
4595 {
4596 	u32 next_id = attr->start_id;
4597 	int err = 0;
4598 
4599 	if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
4600 		return -EINVAL;
4601 
4602 	if (!capable(CAP_SYS_ADMIN))
4603 		return -EPERM;
4604 
4605 	next_id++;
4606 	spin_lock_bh(lock);
4607 	if (!idr_get_next(idr, &next_id))
4608 		err = -ENOENT;
4609 	spin_unlock_bh(lock);
4610 
4611 	if (!err)
4612 		err = put_user(next_id, &uattr->next_id);
4613 
4614 	return err;
4615 }
4616 
4617 struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
4618 {
4619 	struct bpf_map *map;
4620 
4621 	spin_lock_bh(&map_idr_lock);
4622 again:
4623 	map = idr_get_next(&map_idr, id);
4624 	if (map) {
4625 		map = __bpf_map_inc_not_zero(map, false);
4626 		if (IS_ERR(map)) {
4627 			(*id)++;
4628 			goto again;
4629 		}
4630 	}
4631 	spin_unlock_bh(&map_idr_lock);
4632 
4633 	return map;
4634 }
4635 
4636 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
4637 {
4638 	struct bpf_prog *prog;
4639 
4640 	spin_lock_bh(&prog_idr_lock);
4641 again:
4642 	prog = idr_get_next(&prog_idr, id);
4643 	if (prog) {
4644 		prog = bpf_prog_inc_not_zero(prog);
4645 		if (IS_ERR(prog)) {
4646 			(*id)++;
4647 			goto again;
4648 		}
4649 	}
4650 	spin_unlock_bh(&prog_idr_lock);
4651 
4652 	return prog;
4653 }
4654 
4655 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
4656 
4657 struct bpf_prog *bpf_prog_by_id(u32 id)
4658 {
4659 	struct bpf_prog *prog;
4660 
4661 	if (!id)
4662 		return ERR_PTR(-ENOENT);
4663 
4664 	spin_lock_bh(&prog_idr_lock);
4665 	prog = idr_find(&prog_idr, id);
4666 	if (prog)
4667 		prog = bpf_prog_inc_not_zero(prog);
4668 	else
4669 		prog = ERR_PTR(-ENOENT);
4670 	spin_unlock_bh(&prog_idr_lock);
4671 	return prog;
4672 }
4673 
4674 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
4675 {
4676 	struct bpf_prog *prog;
4677 	u32 id = attr->prog_id;
4678 	int fd;
4679 
4680 	if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
4681 		return -EINVAL;
4682 
4683 	if (!capable(CAP_SYS_ADMIN))
4684 		return -EPERM;
4685 
4686 	prog = bpf_prog_by_id(id);
4687 	if (IS_ERR(prog))
4688 		return PTR_ERR(prog);
4689 
4690 	fd = bpf_prog_new_fd(prog);
4691 	if (fd < 0)
4692 		bpf_prog_put(prog);
4693 
4694 	return fd;
4695 }
4696 
4697 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
4698 
4699 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
4700 {
4701 	struct bpf_map *map;
4702 	u32 id = attr->map_id;
4703 	int f_flags;
4704 	int fd;
4705 
4706 	if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
4707 	    attr->open_flags & ~BPF_OBJ_FLAG_MASK)
4708 		return -EINVAL;
4709 
4710 	if (!capable(CAP_SYS_ADMIN))
4711 		return -EPERM;
4712 
4713 	f_flags = bpf_get_file_flag(attr->open_flags);
4714 	if (f_flags < 0)
4715 		return f_flags;
4716 
4717 	spin_lock_bh(&map_idr_lock);
4718 	map = idr_find(&map_idr, id);
4719 	if (map)
4720 		map = __bpf_map_inc_not_zero(map, true);
4721 	else
4722 		map = ERR_PTR(-ENOENT);
4723 	spin_unlock_bh(&map_idr_lock);
4724 
4725 	if (IS_ERR(map))
4726 		return PTR_ERR(map);
4727 
4728 	fd = bpf_map_new_fd(map, f_flags);
4729 	if (fd < 0)
4730 		bpf_map_put_with_uref(map);
4731 
4732 	return fd;
4733 }
4734 
4735 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
4736 					      unsigned long addr, u32 *off,
4737 					      u32 *type)
4738 {
4739 	const struct bpf_map *map;
4740 	int i;
4741 
4742 	mutex_lock(&prog->aux->used_maps_mutex);
4743 	for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
4744 		map = prog->aux->used_maps[i];
4745 		if (map == (void *)addr) {
4746 			*type = BPF_PSEUDO_MAP_FD;
4747 			goto out;
4748 		}
4749 		if (!map->ops->map_direct_value_meta)
4750 			continue;
4751 		if (!map->ops->map_direct_value_meta(map, addr, off)) {
4752 			*type = BPF_PSEUDO_MAP_VALUE;
4753 			goto out;
4754 		}
4755 	}
4756 	map = NULL;
4757 
4758 out:
4759 	mutex_unlock(&prog->aux->used_maps_mutex);
4760 	return map;
4761 }
4762 
4763 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
4764 					      const struct cred *f_cred)
4765 {
4766 	const struct bpf_map *map;
4767 	struct bpf_insn *insns;
4768 	u32 off, type;
4769 	u64 imm;
4770 	u8 code;
4771 	int i;
4772 
4773 	insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
4774 			GFP_USER);
4775 	if (!insns)
4776 		return insns;
4777 
4778 	for (i = 0; i < prog->len; i++) {
4779 		code = insns[i].code;
4780 
4781 		if (code == (BPF_JMP | BPF_TAIL_CALL)) {
4782 			insns[i].code = BPF_JMP | BPF_CALL;
4783 			insns[i].imm = BPF_FUNC_tail_call;
4784 			/* fall-through */
4785 		}
4786 		if (code == (BPF_JMP | BPF_CALL) ||
4787 		    code == (BPF_JMP | BPF_CALL_ARGS)) {
4788 			if (code == (BPF_JMP | BPF_CALL_ARGS))
4789 				insns[i].code = BPF_JMP | BPF_CALL;
4790 			if (!bpf_dump_raw_ok(f_cred))
4791 				insns[i].imm = 0;
4792 			continue;
4793 		}
4794 		if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
4795 			insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
4796 			continue;
4797 		}
4798 
4799 		if ((BPF_CLASS(code) == BPF_LDX || BPF_CLASS(code) == BPF_STX ||
4800 		     BPF_CLASS(code) == BPF_ST) && BPF_MODE(code) == BPF_PROBE_MEM32) {
4801 			insns[i].code = BPF_CLASS(code) | BPF_SIZE(code) | BPF_MEM;
4802 			continue;
4803 		}
4804 
4805 		if (code != (BPF_LD | BPF_IMM | BPF_DW))
4806 			continue;
4807 
4808 		imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
4809 		map = bpf_map_from_imm(prog, imm, &off, &type);
4810 		if (map) {
4811 			insns[i].src_reg = type;
4812 			insns[i].imm = map->id;
4813 			insns[i + 1].imm = off;
4814 			continue;
4815 		}
4816 	}
4817 
4818 	return insns;
4819 }
4820 
4821 static int set_info_rec_size(struct bpf_prog_info *info)
4822 {
4823 	/*
4824 	 * Ensure info.*_rec_size is the same as kernel expected size
4825 	 *
4826 	 * or
4827 	 *
4828 	 * Only allow zero *_rec_size if both _rec_size and _cnt are
4829 	 * zero.  In this case, the kernel will set the expected
4830 	 * _rec_size back to the info.
4831 	 */
4832 
4833 	if ((info->nr_func_info || info->func_info_rec_size) &&
4834 	    info->func_info_rec_size != sizeof(struct bpf_func_info))
4835 		return -EINVAL;
4836 
4837 	if ((info->nr_line_info || info->line_info_rec_size) &&
4838 	    info->line_info_rec_size != sizeof(struct bpf_line_info))
4839 		return -EINVAL;
4840 
4841 	if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
4842 	    info->jited_line_info_rec_size != sizeof(__u64))
4843 		return -EINVAL;
4844 
4845 	info->func_info_rec_size = sizeof(struct bpf_func_info);
4846 	info->line_info_rec_size = sizeof(struct bpf_line_info);
4847 	info->jited_line_info_rec_size = sizeof(__u64);
4848 
4849 	return 0;
4850 }
4851 
4852 static int bpf_prog_get_info_by_fd(struct file *file,
4853 				   struct bpf_prog *prog,
4854 				   const union bpf_attr *attr,
4855 				   union bpf_attr __user *uattr)
4856 {
4857 	struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4858 	struct btf *attach_btf = bpf_prog_get_target_btf(prog);
4859 	struct bpf_prog_info info;
4860 	u32 info_len = attr->info.info_len;
4861 	struct bpf_prog_kstats stats;
4862 	char __user *uinsns;
4863 	u32 ulen;
4864 	int err;
4865 
4866 	err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
4867 	if (err)
4868 		return err;
4869 	info_len = min_t(u32, sizeof(info), info_len);
4870 
4871 	memset(&info, 0, sizeof(info));
4872 	if (copy_from_user(&info, uinfo, info_len))
4873 		return -EFAULT;
4874 
4875 	info.type = prog->type;
4876 	info.id = prog->aux->id;
4877 	info.load_time = prog->aux->load_time;
4878 	info.created_by_uid = from_kuid_munged(current_user_ns(),
4879 					       prog->aux->user->uid);
4880 	info.gpl_compatible = prog->gpl_compatible;
4881 
4882 	memcpy(info.tag, prog->tag, sizeof(prog->tag));
4883 	memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
4884 
4885 	mutex_lock(&prog->aux->used_maps_mutex);
4886 	ulen = info.nr_map_ids;
4887 	info.nr_map_ids = prog->aux->used_map_cnt;
4888 	ulen = min_t(u32, info.nr_map_ids, ulen);
4889 	if (ulen) {
4890 		u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
4891 		u32 i;
4892 
4893 		for (i = 0; i < ulen; i++)
4894 			if (put_user(prog->aux->used_maps[i]->id,
4895 				     &user_map_ids[i])) {
4896 				mutex_unlock(&prog->aux->used_maps_mutex);
4897 				return -EFAULT;
4898 			}
4899 	}
4900 	mutex_unlock(&prog->aux->used_maps_mutex);
4901 
4902 	err = set_info_rec_size(&info);
4903 	if (err)
4904 		return err;
4905 
4906 	bpf_prog_get_stats(prog, &stats);
4907 	info.run_time_ns = stats.nsecs;
4908 	info.run_cnt = stats.cnt;
4909 	info.recursion_misses = stats.misses;
4910 
4911 	info.verified_insns = prog->aux->verified_insns;
4912 	if (prog->aux->btf)
4913 		info.btf_id = btf_obj_id(prog->aux->btf);
4914 
4915 	if (!bpf_capable()) {
4916 		info.jited_prog_len = 0;
4917 		info.xlated_prog_len = 0;
4918 		info.nr_jited_ksyms = 0;
4919 		info.nr_jited_func_lens = 0;
4920 		info.nr_func_info = 0;
4921 		info.nr_line_info = 0;
4922 		info.nr_jited_line_info = 0;
4923 		goto done;
4924 	}
4925 
4926 	ulen = info.xlated_prog_len;
4927 	info.xlated_prog_len = bpf_prog_insn_size(prog);
4928 	if (info.xlated_prog_len && ulen) {
4929 		struct bpf_insn *insns_sanitized;
4930 		bool fault;
4931 
4932 		if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
4933 			info.xlated_prog_insns = 0;
4934 			goto done;
4935 		}
4936 		insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
4937 		if (!insns_sanitized)
4938 			return -ENOMEM;
4939 		uinsns = u64_to_user_ptr(info.xlated_prog_insns);
4940 		ulen = min_t(u32, info.xlated_prog_len, ulen);
4941 		fault = copy_to_user(uinsns, insns_sanitized, ulen);
4942 		kfree(insns_sanitized);
4943 		if (fault)
4944 			return -EFAULT;
4945 	}
4946 
4947 	if (bpf_prog_is_offloaded(prog->aux)) {
4948 		err = bpf_prog_offload_info_fill(&info, prog);
4949 		if (err)
4950 			return err;
4951 		goto done;
4952 	}
4953 
4954 	/* NOTE: the following code is supposed to be skipped for offload.
4955 	 * bpf_prog_offload_info_fill() is the place to fill similar fields
4956 	 * for offload.
4957 	 */
4958 	ulen = info.jited_prog_len;
4959 	if (prog->aux->func_cnt) {
4960 		u32 i;
4961 
4962 		info.jited_prog_len = 0;
4963 		for (i = 0; i < prog->aux->func_cnt; i++)
4964 			info.jited_prog_len += prog->aux->func[i]->jited_len;
4965 	} else {
4966 		info.jited_prog_len = prog->jited_len;
4967 	}
4968 
4969 	if (info.jited_prog_len && ulen) {
4970 		if (bpf_dump_raw_ok(file->f_cred)) {
4971 			uinsns = u64_to_user_ptr(info.jited_prog_insns);
4972 			ulen = min_t(u32, info.jited_prog_len, ulen);
4973 
4974 			/* for multi-function programs, copy the JITed
4975 			 * instructions for all the functions
4976 			 */
4977 			if (prog->aux->func_cnt) {
4978 				u32 len, free, i;
4979 				u8 *img;
4980 
4981 				free = ulen;
4982 				for (i = 0; i < prog->aux->func_cnt; i++) {
4983 					len = prog->aux->func[i]->jited_len;
4984 					len = min_t(u32, len, free);
4985 					img = (u8 *) prog->aux->func[i]->bpf_func;
4986 					if (copy_to_user(uinsns, img, len))
4987 						return -EFAULT;
4988 					uinsns += len;
4989 					free -= len;
4990 					if (!free)
4991 						break;
4992 				}
4993 			} else {
4994 				if (copy_to_user(uinsns, prog->bpf_func, ulen))
4995 					return -EFAULT;
4996 			}
4997 		} else {
4998 			info.jited_prog_insns = 0;
4999 		}
5000 	}
5001 
5002 	ulen = info.nr_jited_ksyms;
5003 	info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
5004 	if (ulen) {
5005 		if (bpf_dump_raw_ok(file->f_cred)) {
5006 			unsigned long ksym_addr;
5007 			u64 __user *user_ksyms;
5008 			u32 i;
5009 
5010 			/* copy the address of the kernel symbol
5011 			 * corresponding to each function
5012 			 */
5013 			ulen = min_t(u32, info.nr_jited_ksyms, ulen);
5014 			user_ksyms = u64_to_user_ptr(info.jited_ksyms);
5015 			if (prog->aux->func_cnt) {
5016 				for (i = 0; i < ulen; i++) {
5017 					ksym_addr = (unsigned long)
5018 						prog->aux->func[i]->bpf_func;
5019 					if (put_user((u64) ksym_addr,
5020 						     &user_ksyms[i]))
5021 						return -EFAULT;
5022 				}
5023 			} else {
5024 				ksym_addr = (unsigned long) prog->bpf_func;
5025 				if (put_user((u64) ksym_addr, &user_ksyms[0]))
5026 					return -EFAULT;
5027 			}
5028 		} else {
5029 			info.jited_ksyms = 0;
5030 		}
5031 	}
5032 
5033 	ulen = info.nr_jited_func_lens;
5034 	info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
5035 	if (ulen) {
5036 		if (bpf_dump_raw_ok(file->f_cred)) {
5037 			u32 __user *user_lens;
5038 			u32 func_len, i;
5039 
5040 			/* copy the JITed image lengths for each function */
5041 			ulen = min_t(u32, info.nr_jited_func_lens, ulen);
5042 			user_lens = u64_to_user_ptr(info.jited_func_lens);
5043 			if (prog->aux->func_cnt) {
5044 				for (i = 0; i < ulen; i++) {
5045 					func_len =
5046 						prog->aux->func[i]->jited_len;
5047 					if (put_user(func_len, &user_lens[i]))
5048 						return -EFAULT;
5049 				}
5050 			} else {
5051 				func_len = prog->jited_len;
5052 				if (put_user(func_len, &user_lens[0]))
5053 					return -EFAULT;
5054 			}
5055 		} else {
5056 			info.jited_func_lens = 0;
5057 		}
5058 	}
5059 
5060 	info.attach_btf_id = prog->aux->attach_btf_id;
5061 	if (attach_btf)
5062 		info.attach_btf_obj_id = btf_obj_id(attach_btf);
5063 
5064 	ulen = info.nr_func_info;
5065 	info.nr_func_info = prog->aux->func_info_cnt;
5066 	if (info.nr_func_info && ulen) {
5067 		char __user *user_finfo;
5068 
5069 		user_finfo = u64_to_user_ptr(info.func_info);
5070 		ulen = min_t(u32, info.nr_func_info, ulen);
5071 		if (copy_to_user(user_finfo, prog->aux->func_info,
5072 				 info.func_info_rec_size * ulen))
5073 			return -EFAULT;
5074 	}
5075 
5076 	ulen = info.nr_line_info;
5077 	info.nr_line_info = prog->aux->nr_linfo;
5078 	if (info.nr_line_info && ulen) {
5079 		__u8 __user *user_linfo;
5080 
5081 		user_linfo = u64_to_user_ptr(info.line_info);
5082 		ulen = min_t(u32, info.nr_line_info, ulen);
5083 		if (copy_to_user(user_linfo, prog->aux->linfo,
5084 				 info.line_info_rec_size * ulen))
5085 			return -EFAULT;
5086 	}
5087 
5088 	ulen = info.nr_jited_line_info;
5089 	if (prog->aux->jited_linfo)
5090 		info.nr_jited_line_info = prog->aux->nr_linfo;
5091 	else
5092 		info.nr_jited_line_info = 0;
5093 	if (info.nr_jited_line_info && ulen) {
5094 		if (bpf_dump_raw_ok(file->f_cred)) {
5095 			unsigned long line_addr;
5096 			__u64 __user *user_linfo;
5097 			u32 i;
5098 
5099 			user_linfo = u64_to_user_ptr(info.jited_line_info);
5100 			ulen = min_t(u32, info.nr_jited_line_info, ulen);
5101 			for (i = 0; i < ulen; i++) {
5102 				line_addr = (unsigned long)prog->aux->jited_linfo[i];
5103 				if (put_user((__u64)line_addr, &user_linfo[i]))
5104 					return -EFAULT;
5105 			}
5106 		} else {
5107 			info.jited_line_info = 0;
5108 		}
5109 	}
5110 
5111 	ulen = info.nr_prog_tags;
5112 	info.nr_prog_tags = prog->aux->func_cnt ? : 1;
5113 	if (ulen) {
5114 		__u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
5115 		u32 i;
5116 
5117 		user_prog_tags = u64_to_user_ptr(info.prog_tags);
5118 		ulen = min_t(u32, info.nr_prog_tags, ulen);
5119 		if (prog->aux->func_cnt) {
5120 			for (i = 0; i < ulen; i++) {
5121 				if (copy_to_user(user_prog_tags[i],
5122 						 prog->aux->func[i]->tag,
5123 						 BPF_TAG_SIZE))
5124 					return -EFAULT;
5125 			}
5126 		} else {
5127 			if (copy_to_user(user_prog_tags[0],
5128 					 prog->tag, BPF_TAG_SIZE))
5129 				return -EFAULT;
5130 		}
5131 	}
5132 
5133 done:
5134 	if (copy_to_user(uinfo, &info, info_len) ||
5135 	    put_user(info_len, &uattr->info.info_len))
5136 		return -EFAULT;
5137 
5138 	return 0;
5139 }
5140 
5141 static int bpf_map_get_info_by_fd(struct file *file,
5142 				  struct bpf_map *map,
5143 				  const union bpf_attr *attr,
5144 				  union bpf_attr __user *uattr)
5145 {
5146 	struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
5147 	struct bpf_map_info info;
5148 	u32 info_len = attr->info.info_len;
5149 	int err;
5150 
5151 	err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
5152 	if (err)
5153 		return err;
5154 	info_len = min_t(u32, sizeof(info), info_len);
5155 
5156 	memset(&info, 0, sizeof(info));
5157 	info.type = map->map_type;
5158 	info.id = map->id;
5159 	info.key_size = map->key_size;
5160 	info.value_size = map->value_size;
5161 	info.max_entries = map->max_entries;
5162 	info.map_flags = map->map_flags;
5163 	info.map_extra = map->map_extra;
5164 	memcpy(info.name, map->name, sizeof(map->name));
5165 
5166 	if (map->btf) {
5167 		info.btf_id = btf_obj_id(map->btf);
5168 		info.btf_key_type_id = map->btf_key_type_id;
5169 		info.btf_value_type_id = map->btf_value_type_id;
5170 	}
5171 	info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
5172 	if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS)
5173 		bpf_map_struct_ops_info_fill(&info, map);
5174 
5175 	if (bpf_map_is_offloaded(map)) {
5176 		err = bpf_map_offload_info_fill(&info, map);
5177 		if (err)
5178 			return err;
5179 	}
5180 
5181 	if (copy_to_user(uinfo, &info, info_len) ||
5182 	    put_user(info_len, &uattr->info.info_len))
5183 		return -EFAULT;
5184 
5185 	return 0;
5186 }
5187 
5188 static int bpf_btf_get_info_by_fd(struct file *file,
5189 				  struct btf *btf,
5190 				  const union bpf_attr *attr,
5191 				  union bpf_attr __user *uattr)
5192 {
5193 	struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
5194 	u32 info_len = attr->info.info_len;
5195 	int err;
5196 
5197 	err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
5198 	if (err)
5199 		return err;
5200 
5201 	return btf_get_info_by_fd(btf, attr, uattr);
5202 }
5203 
5204 static int bpf_link_get_info_by_fd(struct file *file,
5205 				  struct bpf_link *link,
5206 				  const union bpf_attr *attr,
5207 				  union bpf_attr __user *uattr)
5208 {
5209 	struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
5210 	struct bpf_link_info info;
5211 	u32 info_len = attr->info.info_len;
5212 	int err;
5213 
5214 	err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
5215 	if (err)
5216 		return err;
5217 	info_len = min_t(u32, sizeof(info), info_len);
5218 
5219 	memset(&info, 0, sizeof(info));
5220 	if (copy_from_user(&info, uinfo, info_len))
5221 		return -EFAULT;
5222 
5223 	info.type = link->type;
5224 	info.id = link->id;
5225 	if (link->prog)
5226 		info.prog_id = link->prog->aux->id;
5227 
5228 	if (link->ops->fill_link_info) {
5229 		err = link->ops->fill_link_info(link, &info);
5230 		if (err)
5231 			return err;
5232 	}
5233 
5234 	if (copy_to_user(uinfo, &info, info_len) ||
5235 	    put_user(info_len, &uattr->info.info_len))
5236 		return -EFAULT;
5237 
5238 	return 0;
5239 }
5240 
5241 
5242 static int token_get_info_by_fd(struct file *file,
5243 				struct bpf_token *token,
5244 				const union bpf_attr *attr,
5245 				union bpf_attr __user *uattr)
5246 {
5247 	struct bpf_token_info __user *uinfo = u64_to_user_ptr(attr->info.info);
5248 	u32 info_len = attr->info.info_len;
5249 	int err;
5250 
5251 	err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
5252 	if (err)
5253 		return err;
5254 	return bpf_token_get_info_by_fd(token, attr, uattr);
5255 }
5256 
5257 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
5258 
5259 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
5260 				  union bpf_attr __user *uattr)
5261 {
5262 	if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
5263 		return -EINVAL;
5264 
5265 	CLASS(fd, f)(attr->info.bpf_fd);
5266 	if (fd_empty(f))
5267 		return -EBADFD;
5268 
5269 	if (fd_file(f)->f_op == &bpf_prog_fops)
5270 		return bpf_prog_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr,
5271 					      uattr);
5272 	else if (fd_file(f)->f_op == &bpf_map_fops)
5273 		return bpf_map_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr,
5274 					     uattr);
5275 	else if (fd_file(f)->f_op == &btf_fops)
5276 		return bpf_btf_get_info_by_fd(fd_file(f), fd_file(f)->private_data, attr, uattr);
5277 	else if (fd_file(f)->f_op == &bpf_link_fops || fd_file(f)->f_op == &bpf_link_fops_poll)
5278 		return bpf_link_get_info_by_fd(fd_file(f), fd_file(f)->private_data,
5279 					      attr, uattr);
5280 	else if (fd_file(f)->f_op == &bpf_token_fops)
5281 		return token_get_info_by_fd(fd_file(f), fd_file(f)->private_data,
5282 					    attr, uattr);
5283 	return -EINVAL;
5284 }
5285 
5286 #define BPF_BTF_LOAD_LAST_FIELD btf_token_fd
5287 
5288 static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr, __u32 uattr_size)
5289 {
5290 	struct bpf_token *token = NULL;
5291 
5292 	if (CHECK_ATTR(BPF_BTF_LOAD))
5293 		return -EINVAL;
5294 
5295 	if (attr->btf_flags & ~BPF_F_TOKEN_FD)
5296 		return -EINVAL;
5297 
5298 	if (attr->btf_flags & BPF_F_TOKEN_FD) {
5299 		token = bpf_token_get_from_fd(attr->btf_token_fd);
5300 		if (IS_ERR(token))
5301 			return PTR_ERR(token);
5302 		if (!bpf_token_allow_cmd(token, BPF_BTF_LOAD)) {
5303 			bpf_token_put(token);
5304 			token = NULL;
5305 		}
5306 	}
5307 
5308 	if (!bpf_token_capable(token, CAP_BPF)) {
5309 		bpf_token_put(token);
5310 		return -EPERM;
5311 	}
5312 
5313 	bpf_token_put(token);
5314 
5315 	return btf_new_fd(attr, uattr, uattr_size);
5316 }
5317 
5318 #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD fd_by_id_token_fd
5319 
5320 static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
5321 {
5322 	struct bpf_token *token = NULL;
5323 
5324 	if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
5325 		return -EINVAL;
5326 
5327 	if (attr->open_flags & ~BPF_F_TOKEN_FD)
5328 		return -EINVAL;
5329 
5330 	if (attr->open_flags & BPF_F_TOKEN_FD) {
5331 		token = bpf_token_get_from_fd(attr->fd_by_id_token_fd);
5332 		if (IS_ERR(token))
5333 			return PTR_ERR(token);
5334 		if (!bpf_token_allow_cmd(token, BPF_BTF_GET_FD_BY_ID)) {
5335 			bpf_token_put(token);
5336 			token = NULL;
5337 		}
5338 	}
5339 
5340 	if (!bpf_token_capable(token, CAP_SYS_ADMIN)) {
5341 		bpf_token_put(token);
5342 		return -EPERM;
5343 	}
5344 
5345 	bpf_token_put(token);
5346 
5347 	return btf_get_fd_by_id(attr->btf_id);
5348 }
5349 
5350 static int bpf_task_fd_query_copy(const union bpf_attr *attr,
5351 				    union bpf_attr __user *uattr,
5352 				    u32 prog_id, u32 fd_type,
5353 				    const char *buf, u64 probe_offset,
5354 				    u64 probe_addr)
5355 {
5356 	char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
5357 	u32 len = buf ? strlen(buf) : 0, input_len;
5358 	int err = 0;
5359 
5360 	if (put_user(len, &uattr->task_fd_query.buf_len))
5361 		return -EFAULT;
5362 	input_len = attr->task_fd_query.buf_len;
5363 	if (input_len && ubuf) {
5364 		if (!len) {
5365 			/* nothing to copy, just make ubuf NULL terminated */
5366 			char zero = '\0';
5367 
5368 			if (put_user(zero, ubuf))
5369 				return -EFAULT;
5370 		} else {
5371 			err = bpf_copy_to_user(ubuf, buf, input_len, len);
5372 			if (err == -EFAULT)
5373 				return err;
5374 		}
5375 	}
5376 
5377 	if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
5378 	    put_user(fd_type, &uattr->task_fd_query.fd_type) ||
5379 	    put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
5380 	    put_user(probe_addr, &uattr->task_fd_query.probe_addr))
5381 		return -EFAULT;
5382 
5383 	return err;
5384 }
5385 
5386 #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
5387 
5388 static int bpf_task_fd_query(const union bpf_attr *attr,
5389 			     union bpf_attr __user *uattr)
5390 {
5391 	pid_t pid = attr->task_fd_query.pid;
5392 	u32 fd = attr->task_fd_query.fd;
5393 	const struct perf_event *event;
5394 	struct task_struct *task;
5395 	struct file *file;
5396 	int err;
5397 
5398 	if (CHECK_ATTR(BPF_TASK_FD_QUERY))
5399 		return -EINVAL;
5400 
5401 	if (!capable(CAP_SYS_ADMIN))
5402 		return -EPERM;
5403 
5404 	if (attr->task_fd_query.flags != 0)
5405 		return -EINVAL;
5406 
5407 	rcu_read_lock();
5408 	task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
5409 	rcu_read_unlock();
5410 	if (!task)
5411 		return -ENOENT;
5412 
5413 	err = 0;
5414 	file = fget_task(task, fd);
5415 	put_task_struct(task);
5416 	if (!file)
5417 		return -EBADF;
5418 
5419 	if (file->f_op == &bpf_link_fops || file->f_op == &bpf_link_fops_poll) {
5420 		struct bpf_link *link = file->private_data;
5421 
5422 		if (link->ops == &bpf_raw_tp_link_lops) {
5423 			struct bpf_raw_tp_link *raw_tp =
5424 				container_of(link, struct bpf_raw_tp_link, link);
5425 			struct bpf_raw_event_map *btp = raw_tp->btp;
5426 
5427 			err = bpf_task_fd_query_copy(attr, uattr,
5428 						     raw_tp->link.prog->aux->id,
5429 						     BPF_FD_TYPE_RAW_TRACEPOINT,
5430 						     btp->tp->name, 0, 0);
5431 			goto put_file;
5432 		}
5433 		goto out_not_supp;
5434 	}
5435 
5436 	event = perf_get_event(file);
5437 	if (!IS_ERR(event)) {
5438 		u64 probe_offset, probe_addr;
5439 		u32 prog_id, fd_type;
5440 		const char *buf;
5441 
5442 		err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
5443 					      &buf, &probe_offset,
5444 					      &probe_addr, NULL);
5445 		if (!err)
5446 			err = bpf_task_fd_query_copy(attr, uattr, prog_id,
5447 						     fd_type, buf,
5448 						     probe_offset,
5449 						     probe_addr);
5450 		goto put_file;
5451 	}
5452 
5453 out_not_supp:
5454 	err = -ENOTSUPP;
5455 put_file:
5456 	fput(file);
5457 	return err;
5458 }
5459 
5460 #define BPF_MAP_BATCH_LAST_FIELD batch.flags
5461 
5462 #define BPF_DO_BATCH(fn, ...)			\
5463 	do {					\
5464 		if (!fn) {			\
5465 			err = -ENOTSUPP;	\
5466 			goto err_put;		\
5467 		}				\
5468 		err = fn(__VA_ARGS__);		\
5469 	} while (0)
5470 
5471 static int bpf_map_do_batch(const union bpf_attr *attr,
5472 			    union bpf_attr __user *uattr,
5473 			    int cmd)
5474 {
5475 	bool has_read  = cmd == BPF_MAP_LOOKUP_BATCH ||
5476 			 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
5477 	bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
5478 	struct bpf_map *map;
5479 	int err;
5480 
5481 	if (CHECK_ATTR(BPF_MAP_BATCH))
5482 		return -EINVAL;
5483 
5484 	CLASS(fd, f)(attr->batch.map_fd);
5485 
5486 	map = __bpf_map_get(f);
5487 	if (IS_ERR(map))
5488 		return PTR_ERR(map);
5489 	if (has_write)
5490 		bpf_map_write_active_inc(map);
5491 	if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
5492 		err = -EPERM;
5493 		goto err_put;
5494 	}
5495 	if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
5496 		err = -EPERM;
5497 		goto err_put;
5498 	}
5499 
5500 	if (cmd == BPF_MAP_LOOKUP_BATCH)
5501 		BPF_DO_BATCH(map->ops->map_lookup_batch, map, attr, uattr);
5502 	else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
5503 		BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch, map, attr, uattr);
5504 	else if (cmd == BPF_MAP_UPDATE_BATCH)
5505 		BPF_DO_BATCH(map->ops->map_update_batch, map, fd_file(f), attr, uattr);
5506 	else
5507 		BPF_DO_BATCH(map->ops->map_delete_batch, map, attr, uattr);
5508 err_put:
5509 	if (has_write) {
5510 		maybe_wait_bpf_programs(map);
5511 		bpf_map_write_active_dec(map);
5512 	}
5513 	return err;
5514 }
5515 
5516 #define BPF_LINK_CREATE_LAST_FIELD link_create.uprobe_multi.pid
5517 static int link_create(union bpf_attr *attr, bpfptr_t uattr)
5518 {
5519 	struct bpf_prog *prog;
5520 	int ret;
5521 
5522 	if (CHECK_ATTR(BPF_LINK_CREATE))
5523 		return -EINVAL;
5524 
5525 	if (attr->link_create.attach_type == BPF_STRUCT_OPS)
5526 		return bpf_struct_ops_link_create(attr);
5527 
5528 	prog = bpf_prog_get(attr->link_create.prog_fd);
5529 	if (IS_ERR(prog))
5530 		return PTR_ERR(prog);
5531 
5532 	ret = bpf_prog_attach_check_attach_type(prog,
5533 						attr->link_create.attach_type);
5534 	if (ret)
5535 		goto out;
5536 
5537 	switch (prog->type) {
5538 	case BPF_PROG_TYPE_CGROUP_SKB:
5539 	case BPF_PROG_TYPE_CGROUP_SOCK:
5540 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
5541 	case BPF_PROG_TYPE_SOCK_OPS:
5542 	case BPF_PROG_TYPE_CGROUP_DEVICE:
5543 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
5544 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
5545 		ret = cgroup_bpf_link_attach(attr, prog);
5546 		break;
5547 	case BPF_PROG_TYPE_EXT:
5548 		ret = bpf_tracing_prog_attach(prog,
5549 					      attr->link_create.target_fd,
5550 					      attr->link_create.target_btf_id,
5551 					      attr->link_create.tracing.cookie,
5552 					      attr->link_create.attach_type);
5553 		break;
5554 	case BPF_PROG_TYPE_LSM:
5555 	case BPF_PROG_TYPE_TRACING:
5556 		if (attr->link_create.attach_type != prog->expected_attach_type) {
5557 			ret = -EINVAL;
5558 			goto out;
5559 		}
5560 		if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
5561 			ret = bpf_raw_tp_link_attach(prog, NULL, attr->link_create.tracing.cookie,
5562 						     attr->link_create.attach_type);
5563 		else if (prog->expected_attach_type == BPF_TRACE_ITER)
5564 			ret = bpf_iter_link_attach(attr, uattr, prog);
5565 		else if (prog->expected_attach_type == BPF_LSM_CGROUP)
5566 			ret = cgroup_bpf_link_attach(attr, prog);
5567 		else
5568 			ret = bpf_tracing_prog_attach(prog,
5569 						      attr->link_create.target_fd,
5570 						      attr->link_create.target_btf_id,
5571 						      attr->link_create.tracing.cookie,
5572 						      attr->link_create.attach_type);
5573 		break;
5574 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
5575 	case BPF_PROG_TYPE_SK_LOOKUP:
5576 		ret = netns_bpf_link_create(attr, prog);
5577 		break;
5578 	case BPF_PROG_TYPE_SK_MSG:
5579 	case BPF_PROG_TYPE_SK_SKB:
5580 		ret = sock_map_link_create(attr, prog);
5581 		break;
5582 #ifdef CONFIG_NET
5583 	case BPF_PROG_TYPE_XDP:
5584 		ret = bpf_xdp_link_attach(attr, prog);
5585 		break;
5586 	case BPF_PROG_TYPE_SCHED_CLS:
5587 		if (attr->link_create.attach_type == BPF_TCX_INGRESS ||
5588 		    attr->link_create.attach_type == BPF_TCX_EGRESS)
5589 			ret = tcx_link_attach(attr, prog);
5590 		else
5591 			ret = netkit_link_attach(attr, prog);
5592 		break;
5593 	case BPF_PROG_TYPE_NETFILTER:
5594 		ret = bpf_nf_link_attach(attr, prog);
5595 		break;
5596 #endif
5597 	case BPF_PROG_TYPE_PERF_EVENT:
5598 	case BPF_PROG_TYPE_TRACEPOINT:
5599 		ret = bpf_perf_link_attach(attr, prog);
5600 		break;
5601 	case BPF_PROG_TYPE_KPROBE:
5602 		if (attr->link_create.attach_type == BPF_PERF_EVENT)
5603 			ret = bpf_perf_link_attach(attr, prog);
5604 		else if (attr->link_create.attach_type == BPF_TRACE_KPROBE_MULTI ||
5605 			 attr->link_create.attach_type == BPF_TRACE_KPROBE_SESSION)
5606 			ret = bpf_kprobe_multi_link_attach(attr, prog);
5607 		else if (attr->link_create.attach_type == BPF_TRACE_UPROBE_MULTI ||
5608 			 attr->link_create.attach_type == BPF_TRACE_UPROBE_SESSION)
5609 			ret = bpf_uprobe_multi_link_attach(attr, prog);
5610 		break;
5611 	default:
5612 		ret = -EINVAL;
5613 	}
5614 
5615 out:
5616 	if (ret < 0)
5617 		bpf_prog_put(prog);
5618 	return ret;
5619 }
5620 
5621 static int link_update_map(struct bpf_link *link, union bpf_attr *attr)
5622 {
5623 	struct bpf_map *new_map, *old_map = NULL;
5624 	int ret;
5625 
5626 	new_map = bpf_map_get(attr->link_update.new_map_fd);
5627 	if (IS_ERR(new_map))
5628 		return PTR_ERR(new_map);
5629 
5630 	if (attr->link_update.flags & BPF_F_REPLACE) {
5631 		old_map = bpf_map_get(attr->link_update.old_map_fd);
5632 		if (IS_ERR(old_map)) {
5633 			ret = PTR_ERR(old_map);
5634 			goto out_put;
5635 		}
5636 	} else if (attr->link_update.old_map_fd) {
5637 		ret = -EINVAL;
5638 		goto out_put;
5639 	}
5640 
5641 	ret = link->ops->update_map(link, new_map, old_map);
5642 
5643 	if (old_map)
5644 		bpf_map_put(old_map);
5645 out_put:
5646 	bpf_map_put(new_map);
5647 	return ret;
5648 }
5649 
5650 #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
5651 
5652 static int link_update(union bpf_attr *attr)
5653 {
5654 	struct bpf_prog *old_prog = NULL, *new_prog;
5655 	struct bpf_link *link;
5656 	u32 flags;
5657 	int ret;
5658 
5659 	if (CHECK_ATTR(BPF_LINK_UPDATE))
5660 		return -EINVAL;
5661 
5662 	flags = attr->link_update.flags;
5663 	if (flags & ~BPF_F_REPLACE)
5664 		return -EINVAL;
5665 
5666 	link = bpf_link_get_from_fd(attr->link_update.link_fd);
5667 	if (IS_ERR(link))
5668 		return PTR_ERR(link);
5669 
5670 	if (link->ops->update_map) {
5671 		ret = link_update_map(link, attr);
5672 		goto out_put_link;
5673 	}
5674 
5675 	new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
5676 	if (IS_ERR(new_prog)) {
5677 		ret = PTR_ERR(new_prog);
5678 		goto out_put_link;
5679 	}
5680 
5681 	if (flags & BPF_F_REPLACE) {
5682 		old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
5683 		if (IS_ERR(old_prog)) {
5684 			ret = PTR_ERR(old_prog);
5685 			old_prog = NULL;
5686 			goto out_put_progs;
5687 		}
5688 	} else if (attr->link_update.old_prog_fd) {
5689 		ret = -EINVAL;
5690 		goto out_put_progs;
5691 	}
5692 
5693 	if (link->ops->update_prog)
5694 		ret = link->ops->update_prog(link, new_prog, old_prog);
5695 	else
5696 		ret = -EINVAL;
5697 
5698 out_put_progs:
5699 	if (old_prog)
5700 		bpf_prog_put(old_prog);
5701 	if (ret)
5702 		bpf_prog_put(new_prog);
5703 out_put_link:
5704 	bpf_link_put_direct(link);
5705 	return ret;
5706 }
5707 
5708 #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
5709 
5710 static int link_detach(union bpf_attr *attr)
5711 {
5712 	struct bpf_link *link;
5713 	int ret;
5714 
5715 	if (CHECK_ATTR(BPF_LINK_DETACH))
5716 		return -EINVAL;
5717 
5718 	link = bpf_link_get_from_fd(attr->link_detach.link_fd);
5719 	if (IS_ERR(link))
5720 		return PTR_ERR(link);
5721 
5722 	if (link->ops->detach)
5723 		ret = link->ops->detach(link);
5724 	else
5725 		ret = -EOPNOTSUPP;
5726 
5727 	bpf_link_put_direct(link);
5728 	return ret;
5729 }
5730 
5731 struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
5732 {
5733 	return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
5734 }
5735 EXPORT_SYMBOL(bpf_link_inc_not_zero);
5736 
5737 struct bpf_link *bpf_link_by_id(u32 id)
5738 {
5739 	struct bpf_link *link;
5740 
5741 	if (!id)
5742 		return ERR_PTR(-ENOENT);
5743 
5744 	spin_lock_bh(&link_idr_lock);
5745 	/* before link is "settled", ID is 0, pretend it doesn't exist yet */
5746 	link = idr_find(&link_idr, id);
5747 	if (link) {
5748 		if (link->id)
5749 			link = bpf_link_inc_not_zero(link);
5750 		else
5751 			link = ERR_PTR(-EAGAIN);
5752 	} else {
5753 		link = ERR_PTR(-ENOENT);
5754 	}
5755 	spin_unlock_bh(&link_idr_lock);
5756 	return link;
5757 }
5758 
5759 struct bpf_link *bpf_link_get_curr_or_next(u32 *id)
5760 {
5761 	struct bpf_link *link;
5762 
5763 	spin_lock_bh(&link_idr_lock);
5764 again:
5765 	link = idr_get_next(&link_idr, id);
5766 	if (link) {
5767 		link = bpf_link_inc_not_zero(link);
5768 		if (IS_ERR(link)) {
5769 			(*id)++;
5770 			goto again;
5771 		}
5772 	}
5773 	spin_unlock_bh(&link_idr_lock);
5774 
5775 	return link;
5776 }
5777 
5778 #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
5779 
5780 static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
5781 {
5782 	struct bpf_link *link;
5783 	u32 id = attr->link_id;
5784 	int fd;
5785 
5786 	if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
5787 		return -EINVAL;
5788 
5789 	if (!capable(CAP_SYS_ADMIN))
5790 		return -EPERM;
5791 
5792 	link = bpf_link_by_id(id);
5793 	if (IS_ERR(link))
5794 		return PTR_ERR(link);
5795 
5796 	fd = bpf_link_new_fd(link);
5797 	if (fd < 0)
5798 		bpf_link_put_direct(link);
5799 
5800 	return fd;
5801 }
5802 
5803 DEFINE_MUTEX(bpf_stats_enabled_mutex);
5804 
5805 static int bpf_stats_release(struct inode *inode, struct file *file)
5806 {
5807 	mutex_lock(&bpf_stats_enabled_mutex);
5808 	static_key_slow_dec(&bpf_stats_enabled_key.key);
5809 	mutex_unlock(&bpf_stats_enabled_mutex);
5810 	return 0;
5811 }
5812 
5813 static const struct file_operations bpf_stats_fops = {
5814 	.release = bpf_stats_release,
5815 };
5816 
5817 static int bpf_enable_runtime_stats(void)
5818 {
5819 	int fd;
5820 
5821 	mutex_lock(&bpf_stats_enabled_mutex);
5822 
5823 	/* Set a very high limit to avoid overflow */
5824 	if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
5825 		mutex_unlock(&bpf_stats_enabled_mutex);
5826 		return -EBUSY;
5827 	}
5828 
5829 	fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
5830 	if (fd >= 0)
5831 		static_key_slow_inc(&bpf_stats_enabled_key.key);
5832 
5833 	mutex_unlock(&bpf_stats_enabled_mutex);
5834 	return fd;
5835 }
5836 
5837 #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
5838 
5839 static int bpf_enable_stats(union bpf_attr *attr)
5840 {
5841 
5842 	if (CHECK_ATTR(BPF_ENABLE_STATS))
5843 		return -EINVAL;
5844 
5845 	if (!capable(CAP_SYS_ADMIN))
5846 		return -EPERM;
5847 
5848 	switch (attr->enable_stats.type) {
5849 	case BPF_STATS_RUN_TIME:
5850 		return bpf_enable_runtime_stats();
5851 	default:
5852 		break;
5853 	}
5854 	return -EINVAL;
5855 }
5856 
5857 #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
5858 
5859 static int bpf_iter_create(union bpf_attr *attr)
5860 {
5861 	struct bpf_link *link;
5862 	int err;
5863 
5864 	if (CHECK_ATTR(BPF_ITER_CREATE))
5865 		return -EINVAL;
5866 
5867 	if (attr->iter_create.flags)
5868 		return -EINVAL;
5869 
5870 	link = bpf_link_get_from_fd(attr->iter_create.link_fd);
5871 	if (IS_ERR(link))
5872 		return PTR_ERR(link);
5873 
5874 	err = bpf_iter_new_fd(link);
5875 	bpf_link_put_direct(link);
5876 
5877 	return err;
5878 }
5879 
5880 #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
5881 
5882 static int bpf_prog_bind_map(union bpf_attr *attr)
5883 {
5884 	struct bpf_prog *prog;
5885 	struct bpf_map *map;
5886 	struct bpf_map **used_maps_old, **used_maps_new;
5887 	int i, ret = 0;
5888 
5889 	if (CHECK_ATTR(BPF_PROG_BIND_MAP))
5890 		return -EINVAL;
5891 
5892 	if (attr->prog_bind_map.flags)
5893 		return -EINVAL;
5894 
5895 	prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
5896 	if (IS_ERR(prog))
5897 		return PTR_ERR(prog);
5898 
5899 	map = bpf_map_get(attr->prog_bind_map.map_fd);
5900 	if (IS_ERR(map)) {
5901 		ret = PTR_ERR(map);
5902 		goto out_prog_put;
5903 	}
5904 
5905 	mutex_lock(&prog->aux->used_maps_mutex);
5906 
5907 	used_maps_old = prog->aux->used_maps;
5908 
5909 	for (i = 0; i < prog->aux->used_map_cnt; i++)
5910 		if (used_maps_old[i] == map) {
5911 			bpf_map_put(map);
5912 			goto out_unlock;
5913 		}
5914 
5915 	used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
5916 				      sizeof(used_maps_new[0]),
5917 				      GFP_KERNEL);
5918 	if (!used_maps_new) {
5919 		ret = -ENOMEM;
5920 		goto out_unlock;
5921 	}
5922 
5923 	/* The bpf program will not access the bpf map, but for the sake of
5924 	 * simplicity, increase sleepable_refcnt for sleepable program as well.
5925 	 */
5926 	if (prog->sleepable)
5927 		atomic64_inc(&map->sleepable_refcnt);
5928 	memcpy(used_maps_new, used_maps_old,
5929 	       sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
5930 	used_maps_new[prog->aux->used_map_cnt] = map;
5931 
5932 	prog->aux->used_map_cnt++;
5933 	prog->aux->used_maps = used_maps_new;
5934 
5935 	kfree(used_maps_old);
5936 
5937 out_unlock:
5938 	mutex_unlock(&prog->aux->used_maps_mutex);
5939 
5940 	if (ret)
5941 		bpf_map_put(map);
5942 out_prog_put:
5943 	bpf_prog_put(prog);
5944 	return ret;
5945 }
5946 
5947 #define BPF_TOKEN_CREATE_LAST_FIELD token_create.bpffs_fd
5948 
5949 static int token_create(union bpf_attr *attr)
5950 {
5951 	if (CHECK_ATTR(BPF_TOKEN_CREATE))
5952 		return -EINVAL;
5953 
5954 	/* no flags are supported yet */
5955 	if (attr->token_create.flags)
5956 		return -EINVAL;
5957 
5958 	return bpf_token_create(attr);
5959 }
5960 
5961 #define BPF_PROG_STREAM_READ_BY_FD_LAST_FIELD prog_stream_read.prog_fd
5962 
5963 static int prog_stream_read(union bpf_attr *attr)
5964 {
5965 	char __user *buf = u64_to_user_ptr(attr->prog_stream_read.stream_buf);
5966 	u32 len = attr->prog_stream_read.stream_buf_len;
5967 	struct bpf_prog *prog;
5968 	int ret;
5969 
5970 	if (CHECK_ATTR(BPF_PROG_STREAM_READ_BY_FD))
5971 		return -EINVAL;
5972 
5973 	prog = bpf_prog_get(attr->prog_stream_read.prog_fd);
5974 	if (IS_ERR(prog))
5975 		return PTR_ERR(prog);
5976 
5977 	ret = bpf_prog_stream_read(prog, attr->prog_stream_read.stream_id, buf, len);
5978 	bpf_prog_put(prog);
5979 
5980 	return ret;
5981 }
5982 
5983 static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size)
5984 {
5985 	union bpf_attr attr;
5986 	int err;
5987 
5988 	err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
5989 	if (err)
5990 		return err;
5991 	size = min_t(u32, size, sizeof(attr));
5992 
5993 	/* copy attributes from user space, may be less than sizeof(bpf_attr) */
5994 	memset(&attr, 0, sizeof(attr));
5995 	if (copy_from_bpfptr(&attr, uattr, size) != 0)
5996 		return -EFAULT;
5997 
5998 	err = security_bpf(cmd, &attr, size, uattr.is_kernel);
5999 	if (err < 0)
6000 		return err;
6001 
6002 	switch (cmd) {
6003 	case BPF_MAP_CREATE:
6004 		err = map_create(&attr, uattr.is_kernel);
6005 		break;
6006 	case BPF_MAP_LOOKUP_ELEM:
6007 		err = map_lookup_elem(&attr);
6008 		break;
6009 	case BPF_MAP_UPDATE_ELEM:
6010 		err = map_update_elem(&attr, uattr);
6011 		break;
6012 	case BPF_MAP_DELETE_ELEM:
6013 		err = map_delete_elem(&attr, uattr);
6014 		break;
6015 	case BPF_MAP_GET_NEXT_KEY:
6016 		err = map_get_next_key(&attr);
6017 		break;
6018 	case BPF_MAP_FREEZE:
6019 		err = map_freeze(&attr);
6020 		break;
6021 	case BPF_PROG_LOAD:
6022 		err = bpf_prog_load(&attr, uattr, size);
6023 		break;
6024 	case BPF_OBJ_PIN:
6025 		err = bpf_obj_pin(&attr);
6026 		break;
6027 	case BPF_OBJ_GET:
6028 		err = bpf_obj_get(&attr);
6029 		break;
6030 	case BPF_PROG_ATTACH:
6031 		err = bpf_prog_attach(&attr);
6032 		break;
6033 	case BPF_PROG_DETACH:
6034 		err = bpf_prog_detach(&attr);
6035 		break;
6036 	case BPF_PROG_QUERY:
6037 		err = bpf_prog_query(&attr, uattr.user);
6038 		break;
6039 	case BPF_PROG_TEST_RUN:
6040 		err = bpf_prog_test_run(&attr, uattr.user);
6041 		break;
6042 	case BPF_PROG_GET_NEXT_ID:
6043 		err = bpf_obj_get_next_id(&attr, uattr.user,
6044 					  &prog_idr, &prog_idr_lock);
6045 		break;
6046 	case BPF_MAP_GET_NEXT_ID:
6047 		err = bpf_obj_get_next_id(&attr, uattr.user,
6048 					  &map_idr, &map_idr_lock);
6049 		break;
6050 	case BPF_BTF_GET_NEXT_ID:
6051 		err = bpf_obj_get_next_id(&attr, uattr.user,
6052 					  &btf_idr, &btf_idr_lock);
6053 		break;
6054 	case BPF_PROG_GET_FD_BY_ID:
6055 		err = bpf_prog_get_fd_by_id(&attr);
6056 		break;
6057 	case BPF_MAP_GET_FD_BY_ID:
6058 		err = bpf_map_get_fd_by_id(&attr);
6059 		break;
6060 	case BPF_OBJ_GET_INFO_BY_FD:
6061 		err = bpf_obj_get_info_by_fd(&attr, uattr.user);
6062 		break;
6063 	case BPF_RAW_TRACEPOINT_OPEN:
6064 		err = bpf_raw_tracepoint_open(&attr);
6065 		break;
6066 	case BPF_BTF_LOAD:
6067 		err = bpf_btf_load(&attr, uattr, size);
6068 		break;
6069 	case BPF_BTF_GET_FD_BY_ID:
6070 		err = bpf_btf_get_fd_by_id(&attr);
6071 		break;
6072 	case BPF_TASK_FD_QUERY:
6073 		err = bpf_task_fd_query(&attr, uattr.user);
6074 		break;
6075 	case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
6076 		err = map_lookup_and_delete_elem(&attr);
6077 		break;
6078 	case BPF_MAP_LOOKUP_BATCH:
6079 		err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
6080 		break;
6081 	case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
6082 		err = bpf_map_do_batch(&attr, uattr.user,
6083 				       BPF_MAP_LOOKUP_AND_DELETE_BATCH);
6084 		break;
6085 	case BPF_MAP_UPDATE_BATCH:
6086 		err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
6087 		break;
6088 	case BPF_MAP_DELETE_BATCH:
6089 		err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
6090 		break;
6091 	case BPF_LINK_CREATE:
6092 		err = link_create(&attr, uattr);
6093 		break;
6094 	case BPF_LINK_UPDATE:
6095 		err = link_update(&attr);
6096 		break;
6097 	case BPF_LINK_GET_FD_BY_ID:
6098 		err = bpf_link_get_fd_by_id(&attr);
6099 		break;
6100 	case BPF_LINK_GET_NEXT_ID:
6101 		err = bpf_obj_get_next_id(&attr, uattr.user,
6102 					  &link_idr, &link_idr_lock);
6103 		break;
6104 	case BPF_ENABLE_STATS:
6105 		err = bpf_enable_stats(&attr);
6106 		break;
6107 	case BPF_ITER_CREATE:
6108 		err = bpf_iter_create(&attr);
6109 		break;
6110 	case BPF_LINK_DETACH:
6111 		err = link_detach(&attr);
6112 		break;
6113 	case BPF_PROG_BIND_MAP:
6114 		err = bpf_prog_bind_map(&attr);
6115 		break;
6116 	case BPF_TOKEN_CREATE:
6117 		err = token_create(&attr);
6118 		break;
6119 	case BPF_PROG_STREAM_READ_BY_FD:
6120 		err = prog_stream_read(&attr);
6121 		break;
6122 	default:
6123 		err = -EINVAL;
6124 		break;
6125 	}
6126 
6127 	return err;
6128 }
6129 
6130 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
6131 {
6132 	return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
6133 }
6134 
6135 static bool syscall_prog_is_valid_access(int off, int size,
6136 					 enum bpf_access_type type,
6137 					 const struct bpf_prog *prog,
6138 					 struct bpf_insn_access_aux *info)
6139 {
6140 	if (off < 0 || off >= U16_MAX)
6141 		return false;
6142 	if (off % size != 0)
6143 		return false;
6144 	return true;
6145 }
6146 
6147 BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
6148 {
6149 	switch (cmd) {
6150 	case BPF_MAP_CREATE:
6151 	case BPF_MAP_DELETE_ELEM:
6152 	case BPF_MAP_UPDATE_ELEM:
6153 	case BPF_MAP_FREEZE:
6154 	case BPF_MAP_GET_FD_BY_ID:
6155 	case BPF_PROG_LOAD:
6156 	case BPF_BTF_LOAD:
6157 	case BPF_LINK_CREATE:
6158 	case BPF_RAW_TRACEPOINT_OPEN:
6159 		break;
6160 	default:
6161 		return -EINVAL;
6162 	}
6163 	return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
6164 }
6165 
6166 
6167 /* To shut up -Wmissing-prototypes.
6168  * This function is used by the kernel light skeleton
6169  * to load bpf programs when modules are loaded or during kernel boot.
6170  * See tools/lib/bpf/skel_internal.h
6171  */
6172 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size);
6173 
6174 int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
6175 {
6176 	struct bpf_prog * __maybe_unused prog;
6177 	struct bpf_tramp_run_ctx __maybe_unused run_ctx;
6178 
6179 	switch (cmd) {
6180 #ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */
6181 	case BPF_PROG_TEST_RUN:
6182 		if (attr->test.data_in || attr->test.data_out ||
6183 		    attr->test.ctx_out || attr->test.duration ||
6184 		    attr->test.repeat || attr->test.flags)
6185 			return -EINVAL;
6186 
6187 		prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL);
6188 		if (IS_ERR(prog))
6189 			return PTR_ERR(prog);
6190 
6191 		if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
6192 		    attr->test.ctx_size_in > U16_MAX) {
6193 			bpf_prog_put(prog);
6194 			return -EINVAL;
6195 		}
6196 
6197 		run_ctx.bpf_cookie = 0;
6198 		if (!__bpf_prog_enter_sleepable_recur(prog, &run_ctx)) {
6199 			/* recursion detected */
6200 			__bpf_prog_exit_sleepable_recur(prog, 0, &run_ctx);
6201 			bpf_prog_put(prog);
6202 			return -EBUSY;
6203 		}
6204 		attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
6205 		__bpf_prog_exit_sleepable_recur(prog, 0 /* bpf_prog_run does runtime stats */,
6206 						&run_ctx);
6207 		bpf_prog_put(prog);
6208 		return 0;
6209 #endif
6210 	default:
6211 		return ____bpf_sys_bpf(cmd, attr, size);
6212 	}
6213 }
6214 EXPORT_SYMBOL_NS(kern_sys_bpf, "BPF_INTERNAL");
6215 
6216 static const struct bpf_func_proto bpf_sys_bpf_proto = {
6217 	.func		= bpf_sys_bpf,
6218 	.gpl_only	= false,
6219 	.ret_type	= RET_INTEGER,
6220 	.arg1_type	= ARG_ANYTHING,
6221 	.arg2_type	= ARG_PTR_TO_MEM | MEM_RDONLY,
6222 	.arg3_type	= ARG_CONST_SIZE,
6223 };
6224 
6225 const struct bpf_func_proto * __weak
6226 tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6227 {
6228 	return bpf_base_func_proto(func_id, prog);
6229 }
6230 
6231 BPF_CALL_1(bpf_sys_close, u32, fd)
6232 {
6233 	/* When bpf program calls this helper there should not be
6234 	 * an fdget() without matching completed fdput().
6235 	 * This helper is allowed in the following callchain only:
6236 	 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
6237 	 */
6238 	return close_fd(fd);
6239 }
6240 
6241 static const struct bpf_func_proto bpf_sys_close_proto = {
6242 	.func		= bpf_sys_close,
6243 	.gpl_only	= false,
6244 	.ret_type	= RET_INTEGER,
6245 	.arg1_type	= ARG_ANYTHING,
6246 };
6247 
6248 BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res)
6249 {
6250 	*res = 0;
6251 	if (flags)
6252 		return -EINVAL;
6253 
6254 	if (name_sz <= 1 || name[name_sz - 1])
6255 		return -EINVAL;
6256 
6257 	if (!bpf_dump_raw_ok(current_cred()))
6258 		return -EPERM;
6259 
6260 	*res = kallsyms_lookup_name(name);
6261 	return *res ? 0 : -ENOENT;
6262 }
6263 
6264 static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = {
6265 	.func		= bpf_kallsyms_lookup_name,
6266 	.gpl_only	= false,
6267 	.ret_type	= RET_INTEGER,
6268 	.arg1_type	= ARG_PTR_TO_MEM,
6269 	.arg2_type	= ARG_CONST_SIZE_OR_ZERO,
6270 	.arg3_type	= ARG_ANYTHING,
6271 	.arg4_type	= ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED,
6272 	.arg4_size	= sizeof(u64),
6273 };
6274 
6275 static const struct bpf_func_proto *
6276 syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
6277 {
6278 	switch (func_id) {
6279 	case BPF_FUNC_sys_bpf:
6280 		return !bpf_token_capable(prog->aux->token, CAP_PERFMON)
6281 		       ? NULL : &bpf_sys_bpf_proto;
6282 	case BPF_FUNC_btf_find_by_name_kind:
6283 		return &bpf_btf_find_by_name_kind_proto;
6284 	case BPF_FUNC_sys_close:
6285 		return &bpf_sys_close_proto;
6286 	case BPF_FUNC_kallsyms_lookup_name:
6287 		return &bpf_kallsyms_lookup_name_proto;
6288 	default:
6289 		return tracing_prog_func_proto(func_id, prog);
6290 	}
6291 }
6292 
6293 const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
6294 	.get_func_proto  = syscall_prog_func_proto,
6295 	.is_valid_access = syscall_prog_is_valid_access,
6296 };
6297 
6298 const struct bpf_prog_ops bpf_syscall_prog_ops = {
6299 	.test_run = bpf_prog_test_run_syscall,
6300 };
6301 
6302 #ifdef CONFIG_SYSCTL
6303 static int bpf_stats_handler(const struct ctl_table *table, int write,
6304 			     void *buffer, size_t *lenp, loff_t *ppos)
6305 {
6306 	struct static_key *key = (struct static_key *)table->data;
6307 	static int saved_val;
6308 	int val, ret;
6309 	struct ctl_table tmp = {
6310 		.data   = &val,
6311 		.maxlen = sizeof(val),
6312 		.mode   = table->mode,
6313 		.extra1 = SYSCTL_ZERO,
6314 		.extra2 = SYSCTL_ONE,
6315 	};
6316 
6317 	if (write && !capable(CAP_SYS_ADMIN))
6318 		return -EPERM;
6319 
6320 	mutex_lock(&bpf_stats_enabled_mutex);
6321 	val = saved_val;
6322 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
6323 	if (write && !ret && val != saved_val) {
6324 		if (val)
6325 			static_key_slow_inc(key);
6326 		else
6327 			static_key_slow_dec(key);
6328 		saved_val = val;
6329 	}
6330 	mutex_unlock(&bpf_stats_enabled_mutex);
6331 	return ret;
6332 }
6333 
6334 void __weak unpriv_ebpf_notify(int new_state)
6335 {
6336 }
6337 
6338 static int bpf_unpriv_handler(const struct ctl_table *table, int write,
6339 			      void *buffer, size_t *lenp, loff_t *ppos)
6340 {
6341 	int ret, unpriv_enable = *(int *)table->data;
6342 	bool locked_state = unpriv_enable == 1;
6343 	struct ctl_table tmp = *table;
6344 
6345 	if (write && !capable(CAP_SYS_ADMIN))
6346 		return -EPERM;
6347 
6348 	tmp.data = &unpriv_enable;
6349 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
6350 	if (write && !ret) {
6351 		if (locked_state && unpriv_enable != 1)
6352 			return -EPERM;
6353 		*(int *)table->data = unpriv_enable;
6354 	}
6355 
6356 	if (write)
6357 		unpriv_ebpf_notify(unpriv_enable);
6358 
6359 	return ret;
6360 }
6361 
6362 static const struct ctl_table bpf_syscall_table[] = {
6363 	{
6364 		.procname	= "unprivileged_bpf_disabled",
6365 		.data		= &sysctl_unprivileged_bpf_disabled,
6366 		.maxlen		= sizeof(sysctl_unprivileged_bpf_disabled),
6367 		.mode		= 0644,
6368 		.proc_handler	= bpf_unpriv_handler,
6369 		.extra1		= SYSCTL_ZERO,
6370 		.extra2		= SYSCTL_TWO,
6371 	},
6372 	{
6373 		.procname	= "bpf_stats_enabled",
6374 		.data		= &bpf_stats_enabled_key.key,
6375 		.mode		= 0644,
6376 		.proc_handler	= bpf_stats_handler,
6377 	},
6378 };
6379 
6380 static int __init bpf_syscall_sysctl_init(void)
6381 {
6382 	register_sysctl_init("kernel", bpf_syscall_table);
6383 	return 0;
6384 }
6385 late_initcall(bpf_syscall_sysctl_init);
6386 #endif /* CONFIG_SYSCTL */
6387