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