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