xref: /linux/kernel/bpf/syscall.c (revision 064223c1231ce508efaded6576ffdb07de9307b5)
1 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of version 2 of the GNU General Public
5  * License as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * General Public License for more details.
11  */
12 #include <linux/bpf.h>
13 #include <linux/bpf_trace.h>
14 #include <linux/btf.h>
15 #include <linux/syscalls.h>
16 #include <linux/slab.h>
17 #include <linux/sched/signal.h>
18 #include <linux/vmalloc.h>
19 #include <linux/mmzone.h>
20 #include <linux/anon_inodes.h>
21 #include <linux/file.h>
22 #include <linux/license.h>
23 #include <linux/filter.h>
24 #include <linux/version.h>
25 #include <linux/kernel.h>
26 #include <linux/idr.h>
27 #include <linux/cred.h>
28 #include <linux/timekeeping.h>
29 #include <linux/ctype.h>
30 #include <linux/btf.h>
31 
32 #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
33 			   (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
34 			   (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
35 			   (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
36 #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
37 #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
38 
39 #define BPF_OBJ_FLAG_MASK   (BPF_F_RDONLY | BPF_F_WRONLY)
40 
41 DEFINE_PER_CPU(int, bpf_prog_active);
42 static DEFINE_IDR(prog_idr);
43 static DEFINE_SPINLOCK(prog_idr_lock);
44 static DEFINE_IDR(map_idr);
45 static DEFINE_SPINLOCK(map_idr_lock);
46 
47 int sysctl_unprivileged_bpf_disabled __read_mostly;
48 
49 static const struct bpf_map_ops * const bpf_map_types[] = {
50 #define BPF_PROG_TYPE(_id, _ops)
51 #define BPF_MAP_TYPE(_id, _ops) \
52 	[_id] = &_ops,
53 #include <linux/bpf_types.h>
54 #undef BPF_PROG_TYPE
55 #undef BPF_MAP_TYPE
56 };
57 
58 /*
59  * If we're handed a bigger struct than we know of, ensure all the unknown bits
60  * are 0 - i.e. new user-space does not rely on any kernel feature extensions
61  * we don't know about yet.
62  *
63  * There is a ToCToU between this function call and the following
64  * copy_from_user() call. However, this is not a concern since this function is
65  * meant to be a future-proofing of bits.
66  */
67 static int check_uarg_tail_zero(void __user *uaddr,
68 				size_t expected_size,
69 				size_t actual_size)
70 {
71 	unsigned char __user *addr;
72 	unsigned char __user *end;
73 	unsigned char val;
74 	int err;
75 
76 	if (unlikely(actual_size > PAGE_SIZE))	/* silly large */
77 		return -E2BIG;
78 
79 	if (unlikely(!access_ok(VERIFY_READ, uaddr, actual_size)))
80 		return -EFAULT;
81 
82 	if (actual_size <= expected_size)
83 		return 0;
84 
85 	addr = uaddr + expected_size;
86 	end  = uaddr + actual_size;
87 
88 	for (; addr < end; addr++) {
89 		err = get_user(val, addr);
90 		if (err)
91 			return err;
92 		if (val)
93 			return -E2BIG;
94 	}
95 
96 	return 0;
97 }
98 
99 const struct bpf_map_ops bpf_map_offload_ops = {
100 	.map_alloc = bpf_map_offload_map_alloc,
101 	.map_free = bpf_map_offload_map_free,
102 };
103 
104 static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
105 {
106 	const struct bpf_map_ops *ops;
107 	struct bpf_map *map;
108 	int err;
109 
110 	if (attr->map_type >= ARRAY_SIZE(bpf_map_types))
111 		return ERR_PTR(-EINVAL);
112 	ops = bpf_map_types[attr->map_type];
113 	if (!ops)
114 		return ERR_PTR(-EINVAL);
115 
116 	if (ops->map_alloc_check) {
117 		err = ops->map_alloc_check(attr);
118 		if (err)
119 			return ERR_PTR(err);
120 	}
121 	if (attr->map_ifindex)
122 		ops = &bpf_map_offload_ops;
123 	map = ops->map_alloc(attr);
124 	if (IS_ERR(map))
125 		return map;
126 	map->ops = ops;
127 	map->map_type = attr->map_type;
128 	return map;
129 }
130 
131 void *bpf_map_area_alloc(size_t size, int numa_node)
132 {
133 	/* We definitely need __GFP_NORETRY, so OOM killer doesn't
134 	 * trigger under memory pressure as we really just want to
135 	 * fail instead.
136 	 */
137 	const gfp_t flags = __GFP_NOWARN | __GFP_NORETRY | __GFP_ZERO;
138 	void *area;
139 
140 	if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
141 		area = kmalloc_node(size, GFP_USER | flags, numa_node);
142 		if (area != NULL)
143 			return area;
144 	}
145 
146 	return __vmalloc_node_flags_caller(size, numa_node, GFP_KERNEL | flags,
147 					   __builtin_return_address(0));
148 }
149 
150 void bpf_map_area_free(void *area)
151 {
152 	kvfree(area);
153 }
154 
155 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
156 {
157 	map->map_type = attr->map_type;
158 	map->key_size = attr->key_size;
159 	map->value_size = attr->value_size;
160 	map->max_entries = attr->max_entries;
161 	map->map_flags = attr->map_flags;
162 	map->numa_node = bpf_map_attr_numa_node(attr);
163 }
164 
165 int bpf_map_precharge_memlock(u32 pages)
166 {
167 	struct user_struct *user = get_current_user();
168 	unsigned long memlock_limit, cur;
169 
170 	memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
171 	cur = atomic_long_read(&user->locked_vm);
172 	free_uid(user);
173 	if (cur + pages > memlock_limit)
174 		return -EPERM;
175 	return 0;
176 }
177 
178 static int bpf_map_charge_memlock(struct bpf_map *map)
179 {
180 	struct user_struct *user = get_current_user();
181 	unsigned long memlock_limit;
182 
183 	memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
184 
185 	atomic_long_add(map->pages, &user->locked_vm);
186 
187 	if (atomic_long_read(&user->locked_vm) > memlock_limit) {
188 		atomic_long_sub(map->pages, &user->locked_vm);
189 		free_uid(user);
190 		return -EPERM;
191 	}
192 	map->user = user;
193 	return 0;
194 }
195 
196 static void bpf_map_uncharge_memlock(struct bpf_map *map)
197 {
198 	struct user_struct *user = map->user;
199 
200 	atomic_long_sub(map->pages, &user->locked_vm);
201 	free_uid(user);
202 }
203 
204 static int bpf_map_alloc_id(struct bpf_map *map)
205 {
206 	int id;
207 
208 	idr_preload(GFP_KERNEL);
209 	spin_lock_bh(&map_idr_lock);
210 	id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
211 	if (id > 0)
212 		map->id = id;
213 	spin_unlock_bh(&map_idr_lock);
214 	idr_preload_end();
215 
216 	if (WARN_ON_ONCE(!id))
217 		return -ENOSPC;
218 
219 	return id > 0 ? 0 : id;
220 }
221 
222 void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
223 {
224 	unsigned long flags;
225 
226 	/* Offloaded maps are removed from the IDR store when their device
227 	 * disappears - even if someone holds an fd to them they are unusable,
228 	 * the memory is gone, all ops will fail; they are simply waiting for
229 	 * refcnt to drop to be freed.
230 	 */
231 	if (!map->id)
232 		return;
233 
234 	if (do_idr_lock)
235 		spin_lock_irqsave(&map_idr_lock, flags);
236 	else
237 		__acquire(&map_idr_lock);
238 
239 	idr_remove(&map_idr, map->id);
240 	map->id = 0;
241 
242 	if (do_idr_lock)
243 		spin_unlock_irqrestore(&map_idr_lock, flags);
244 	else
245 		__release(&map_idr_lock);
246 }
247 
248 /* called from workqueue */
249 static void bpf_map_free_deferred(struct work_struct *work)
250 {
251 	struct bpf_map *map = container_of(work, struct bpf_map, work);
252 
253 	bpf_map_uncharge_memlock(map);
254 	security_bpf_map_free(map);
255 	btf_put(map->btf);
256 	/* implementation dependent freeing */
257 	map->ops->map_free(map);
258 }
259 
260 static void bpf_map_put_uref(struct bpf_map *map)
261 {
262 	if (atomic_dec_and_test(&map->usercnt)) {
263 		if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY)
264 			bpf_fd_array_map_clear(map);
265 	}
266 }
267 
268 /* decrement map refcnt and schedule it for freeing via workqueue
269  * (unrelying map implementation ops->map_free() might sleep)
270  */
271 static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
272 {
273 	if (atomic_dec_and_test(&map->refcnt)) {
274 		/* bpf_map_free_id() must be called first */
275 		bpf_map_free_id(map, do_idr_lock);
276 		INIT_WORK(&map->work, bpf_map_free_deferred);
277 		schedule_work(&map->work);
278 	}
279 }
280 
281 void bpf_map_put(struct bpf_map *map)
282 {
283 	__bpf_map_put(map, true);
284 }
285 
286 void bpf_map_put_with_uref(struct bpf_map *map)
287 {
288 	bpf_map_put_uref(map);
289 	bpf_map_put(map);
290 }
291 
292 static int bpf_map_release(struct inode *inode, struct file *filp)
293 {
294 	struct bpf_map *map = filp->private_data;
295 
296 	if (map->ops->map_release)
297 		map->ops->map_release(map, filp);
298 
299 	bpf_map_put_with_uref(map);
300 	return 0;
301 }
302 
303 #ifdef CONFIG_PROC_FS
304 static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
305 {
306 	const struct bpf_map *map = filp->private_data;
307 	const struct bpf_array *array;
308 	u32 owner_prog_type = 0;
309 	u32 owner_jited = 0;
310 
311 	if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
312 		array = container_of(map, struct bpf_array, map);
313 		owner_prog_type = array->owner_prog_type;
314 		owner_jited = array->owner_jited;
315 	}
316 
317 	seq_printf(m,
318 		   "map_type:\t%u\n"
319 		   "key_size:\t%u\n"
320 		   "value_size:\t%u\n"
321 		   "max_entries:\t%u\n"
322 		   "map_flags:\t%#x\n"
323 		   "memlock:\t%llu\n",
324 		   map->map_type,
325 		   map->key_size,
326 		   map->value_size,
327 		   map->max_entries,
328 		   map->map_flags,
329 		   map->pages * 1ULL << PAGE_SHIFT);
330 
331 	if (owner_prog_type) {
332 		seq_printf(m, "owner_prog_type:\t%u\n",
333 			   owner_prog_type);
334 		seq_printf(m, "owner_jited:\t%u\n",
335 			   owner_jited);
336 	}
337 }
338 #endif
339 
340 static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
341 			      loff_t *ppos)
342 {
343 	/* We need this handler such that alloc_file() enables
344 	 * f_mode with FMODE_CAN_READ.
345 	 */
346 	return -EINVAL;
347 }
348 
349 static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
350 			       size_t siz, loff_t *ppos)
351 {
352 	/* We need this handler such that alloc_file() enables
353 	 * f_mode with FMODE_CAN_WRITE.
354 	 */
355 	return -EINVAL;
356 }
357 
358 const struct file_operations bpf_map_fops = {
359 #ifdef CONFIG_PROC_FS
360 	.show_fdinfo	= bpf_map_show_fdinfo,
361 #endif
362 	.release	= bpf_map_release,
363 	.read		= bpf_dummy_read,
364 	.write		= bpf_dummy_write,
365 };
366 
367 int bpf_map_new_fd(struct bpf_map *map, int flags)
368 {
369 	int ret;
370 
371 	ret = security_bpf_map(map, OPEN_FMODE(flags));
372 	if (ret < 0)
373 		return ret;
374 
375 	return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
376 				flags | O_CLOEXEC);
377 }
378 
379 int bpf_get_file_flag(int flags)
380 {
381 	if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
382 		return -EINVAL;
383 	if (flags & BPF_F_RDONLY)
384 		return O_RDONLY;
385 	if (flags & BPF_F_WRONLY)
386 		return O_WRONLY;
387 	return O_RDWR;
388 }
389 
390 /* helper macro to check that unused fields 'union bpf_attr' are zero */
391 #define CHECK_ATTR(CMD) \
392 	memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
393 		   sizeof(attr->CMD##_LAST_FIELD), 0, \
394 		   sizeof(*attr) - \
395 		   offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
396 		   sizeof(attr->CMD##_LAST_FIELD)) != NULL
397 
398 /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
399  * Return 0 on success and < 0 on error.
400  */
401 static int bpf_obj_name_cpy(char *dst, const char *src)
402 {
403 	const char *end = src + BPF_OBJ_NAME_LEN;
404 
405 	memset(dst, 0, BPF_OBJ_NAME_LEN);
406 
407 	/* Copy all isalnum() and '_' char */
408 	while (src < end && *src) {
409 		if (!isalnum(*src) && *src != '_')
410 			return -EINVAL;
411 		*dst++ = *src++;
412 	}
413 
414 	/* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
415 	if (src == end)
416 		return -EINVAL;
417 
418 	return 0;
419 }
420 
421 #define BPF_MAP_CREATE_LAST_FIELD btf_value_id
422 /* called via syscall */
423 static int map_create(union bpf_attr *attr)
424 {
425 	int numa_node = bpf_map_attr_numa_node(attr);
426 	struct bpf_map *map;
427 	int f_flags;
428 	int err;
429 
430 	err = CHECK_ATTR(BPF_MAP_CREATE);
431 	if (err)
432 		return -EINVAL;
433 
434 	f_flags = bpf_get_file_flag(attr->map_flags);
435 	if (f_flags < 0)
436 		return f_flags;
437 
438 	if (numa_node != NUMA_NO_NODE &&
439 	    ((unsigned int)numa_node >= nr_node_ids ||
440 	     !node_online(numa_node)))
441 		return -EINVAL;
442 
443 	/* find map type and init map: hashtable vs rbtree vs bloom vs ... */
444 	map = find_and_alloc_map(attr);
445 	if (IS_ERR(map))
446 		return PTR_ERR(map);
447 
448 	err = bpf_obj_name_cpy(map->name, attr->map_name);
449 	if (err)
450 		goto free_map_nouncharge;
451 
452 	atomic_set(&map->refcnt, 1);
453 	atomic_set(&map->usercnt, 1);
454 
455 	if (bpf_map_support_seq_show(map) &&
456 	    (attr->btf_key_id || attr->btf_value_id)) {
457 		struct btf *btf;
458 
459 		if (!attr->btf_key_id || !attr->btf_value_id) {
460 			err = -EINVAL;
461 			goto free_map_nouncharge;
462 		}
463 
464 		btf = btf_get_by_fd(attr->btf_fd);
465 		if (IS_ERR(btf)) {
466 			err = PTR_ERR(btf);
467 			goto free_map_nouncharge;
468 		}
469 
470 		err = map->ops->map_check_btf(map, btf, attr->btf_key_id,
471 					      attr->btf_value_id);
472 		if (err) {
473 			btf_put(btf);
474 			goto free_map_nouncharge;
475 		}
476 
477 		map->btf = btf;
478 		map->btf_key_id = attr->btf_key_id;
479 		map->btf_value_id = attr->btf_value_id;
480 	}
481 
482 	err = security_bpf_map_alloc(map);
483 	if (err)
484 		goto free_map_nouncharge;
485 
486 	err = bpf_map_charge_memlock(map);
487 	if (err)
488 		goto free_map_sec;
489 
490 	err = bpf_map_alloc_id(map);
491 	if (err)
492 		goto free_map;
493 
494 	err = bpf_map_new_fd(map, f_flags);
495 	if (err < 0) {
496 		/* failed to allocate fd.
497 		 * bpf_map_put() is needed because the above
498 		 * bpf_map_alloc_id() has published the map
499 		 * to the userspace and the userspace may
500 		 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
501 		 */
502 		bpf_map_put(map);
503 		return err;
504 	}
505 
506 	trace_bpf_map_create(map, err);
507 	return err;
508 
509 free_map:
510 	bpf_map_uncharge_memlock(map);
511 free_map_sec:
512 	security_bpf_map_free(map);
513 free_map_nouncharge:
514 	btf_put(map->btf);
515 	map->ops->map_free(map);
516 	return err;
517 }
518 
519 /* if error is returned, fd is released.
520  * On success caller should complete fd access with matching fdput()
521  */
522 struct bpf_map *__bpf_map_get(struct fd f)
523 {
524 	if (!f.file)
525 		return ERR_PTR(-EBADF);
526 	if (f.file->f_op != &bpf_map_fops) {
527 		fdput(f);
528 		return ERR_PTR(-EINVAL);
529 	}
530 
531 	return f.file->private_data;
532 }
533 
534 /* prog's and map's refcnt limit */
535 #define BPF_MAX_REFCNT 32768
536 
537 struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
538 {
539 	if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
540 		atomic_dec(&map->refcnt);
541 		return ERR_PTR(-EBUSY);
542 	}
543 	if (uref)
544 		atomic_inc(&map->usercnt);
545 	return map;
546 }
547 
548 struct bpf_map *bpf_map_get_with_uref(u32 ufd)
549 {
550 	struct fd f = fdget(ufd);
551 	struct bpf_map *map;
552 
553 	map = __bpf_map_get(f);
554 	if (IS_ERR(map))
555 		return map;
556 
557 	map = bpf_map_inc(map, true);
558 	fdput(f);
559 
560 	return map;
561 }
562 
563 /* map_idr_lock should have been held */
564 static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
565 					    bool uref)
566 {
567 	int refold;
568 
569 	refold = __atomic_add_unless(&map->refcnt, 1, 0);
570 
571 	if (refold >= BPF_MAX_REFCNT) {
572 		__bpf_map_put(map, false);
573 		return ERR_PTR(-EBUSY);
574 	}
575 
576 	if (!refold)
577 		return ERR_PTR(-ENOENT);
578 
579 	if (uref)
580 		atomic_inc(&map->usercnt);
581 
582 	return map;
583 }
584 
585 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
586 {
587 	return -ENOTSUPP;
588 }
589 
590 /* last field in 'union bpf_attr' used by this command */
591 #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD value
592 
593 static int map_lookup_elem(union bpf_attr *attr)
594 {
595 	void __user *ukey = u64_to_user_ptr(attr->key);
596 	void __user *uvalue = u64_to_user_ptr(attr->value);
597 	int ufd = attr->map_fd;
598 	struct bpf_map *map;
599 	void *key, *value, *ptr;
600 	u32 value_size;
601 	struct fd f;
602 	int err;
603 
604 	if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
605 		return -EINVAL;
606 
607 	f = fdget(ufd);
608 	map = __bpf_map_get(f);
609 	if (IS_ERR(map))
610 		return PTR_ERR(map);
611 
612 	if (!(f.file->f_mode & FMODE_CAN_READ)) {
613 		err = -EPERM;
614 		goto err_put;
615 	}
616 
617 	key = memdup_user(ukey, map->key_size);
618 	if (IS_ERR(key)) {
619 		err = PTR_ERR(key);
620 		goto err_put;
621 	}
622 
623 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
624 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
625 	    map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
626 		value_size = round_up(map->value_size, 8) * num_possible_cpus();
627 	else if (IS_FD_MAP(map))
628 		value_size = sizeof(u32);
629 	else
630 		value_size = map->value_size;
631 
632 	err = -ENOMEM;
633 	value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
634 	if (!value)
635 		goto free_key;
636 
637 	if (bpf_map_is_dev_bound(map)) {
638 		err = bpf_map_offload_lookup_elem(map, key, value);
639 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
640 		   map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
641 		err = bpf_percpu_hash_copy(map, key, value);
642 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
643 		err = bpf_percpu_array_copy(map, key, value);
644 	} else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
645 		err = bpf_stackmap_copy(map, key, value);
646 	} else if (IS_FD_ARRAY(map)) {
647 		err = bpf_fd_array_map_lookup_elem(map, key, value);
648 	} else if (IS_FD_HASH(map)) {
649 		err = bpf_fd_htab_map_lookup_elem(map, key, value);
650 	} else {
651 		rcu_read_lock();
652 		ptr = map->ops->map_lookup_elem(map, key);
653 		if (ptr)
654 			memcpy(value, ptr, value_size);
655 		rcu_read_unlock();
656 		err = ptr ? 0 : -ENOENT;
657 	}
658 
659 	if (err)
660 		goto free_value;
661 
662 	err = -EFAULT;
663 	if (copy_to_user(uvalue, value, value_size) != 0)
664 		goto free_value;
665 
666 	trace_bpf_map_lookup_elem(map, ufd, key, value);
667 	err = 0;
668 
669 free_value:
670 	kfree(value);
671 free_key:
672 	kfree(key);
673 err_put:
674 	fdput(f);
675 	return err;
676 }
677 
678 #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
679 
680 static int map_update_elem(union bpf_attr *attr)
681 {
682 	void __user *ukey = u64_to_user_ptr(attr->key);
683 	void __user *uvalue = u64_to_user_ptr(attr->value);
684 	int ufd = attr->map_fd;
685 	struct bpf_map *map;
686 	void *key, *value;
687 	u32 value_size;
688 	struct fd f;
689 	int err;
690 
691 	if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
692 		return -EINVAL;
693 
694 	f = fdget(ufd);
695 	map = __bpf_map_get(f);
696 	if (IS_ERR(map))
697 		return PTR_ERR(map);
698 
699 	if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
700 		err = -EPERM;
701 		goto err_put;
702 	}
703 
704 	key = memdup_user(ukey, map->key_size);
705 	if (IS_ERR(key)) {
706 		err = PTR_ERR(key);
707 		goto err_put;
708 	}
709 
710 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
711 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
712 	    map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY)
713 		value_size = round_up(map->value_size, 8) * num_possible_cpus();
714 	else
715 		value_size = map->value_size;
716 
717 	err = -ENOMEM;
718 	value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
719 	if (!value)
720 		goto free_key;
721 
722 	err = -EFAULT;
723 	if (copy_from_user(value, uvalue, value_size) != 0)
724 		goto free_value;
725 
726 	/* Need to create a kthread, thus must support schedule */
727 	if (bpf_map_is_dev_bound(map)) {
728 		err = bpf_map_offload_update_elem(map, key, value, attr->flags);
729 		goto out;
730 	} else if (map->map_type == BPF_MAP_TYPE_CPUMAP) {
731 		err = map->ops->map_update_elem(map, key, value, attr->flags);
732 		goto out;
733 	}
734 
735 	/* must increment bpf_prog_active to avoid kprobe+bpf triggering from
736 	 * inside bpf map update or delete otherwise deadlocks are possible
737 	 */
738 	preempt_disable();
739 	__this_cpu_inc(bpf_prog_active);
740 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
741 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
742 		err = bpf_percpu_hash_update(map, key, value, attr->flags);
743 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
744 		err = bpf_percpu_array_update(map, key, value, attr->flags);
745 	} else if (IS_FD_ARRAY(map)) {
746 		rcu_read_lock();
747 		err = bpf_fd_array_map_update_elem(map, f.file, key, value,
748 						   attr->flags);
749 		rcu_read_unlock();
750 	} else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
751 		rcu_read_lock();
752 		err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
753 						  attr->flags);
754 		rcu_read_unlock();
755 	} else {
756 		rcu_read_lock();
757 		err = map->ops->map_update_elem(map, key, value, attr->flags);
758 		rcu_read_unlock();
759 	}
760 	__this_cpu_dec(bpf_prog_active);
761 	preempt_enable();
762 out:
763 	if (!err)
764 		trace_bpf_map_update_elem(map, ufd, key, value);
765 free_value:
766 	kfree(value);
767 free_key:
768 	kfree(key);
769 err_put:
770 	fdput(f);
771 	return err;
772 }
773 
774 #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
775 
776 static int map_delete_elem(union bpf_attr *attr)
777 {
778 	void __user *ukey = u64_to_user_ptr(attr->key);
779 	int ufd = attr->map_fd;
780 	struct bpf_map *map;
781 	struct fd f;
782 	void *key;
783 	int err;
784 
785 	if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
786 		return -EINVAL;
787 
788 	f = fdget(ufd);
789 	map = __bpf_map_get(f);
790 	if (IS_ERR(map))
791 		return PTR_ERR(map);
792 
793 	if (!(f.file->f_mode & FMODE_CAN_WRITE)) {
794 		err = -EPERM;
795 		goto err_put;
796 	}
797 
798 	key = memdup_user(ukey, map->key_size);
799 	if (IS_ERR(key)) {
800 		err = PTR_ERR(key);
801 		goto err_put;
802 	}
803 
804 	if (bpf_map_is_dev_bound(map)) {
805 		err = bpf_map_offload_delete_elem(map, key);
806 		goto out;
807 	}
808 
809 	preempt_disable();
810 	__this_cpu_inc(bpf_prog_active);
811 	rcu_read_lock();
812 	err = map->ops->map_delete_elem(map, key);
813 	rcu_read_unlock();
814 	__this_cpu_dec(bpf_prog_active);
815 	preempt_enable();
816 out:
817 	if (!err)
818 		trace_bpf_map_delete_elem(map, ufd, key);
819 	kfree(key);
820 err_put:
821 	fdput(f);
822 	return err;
823 }
824 
825 /* last field in 'union bpf_attr' used by this command */
826 #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
827 
828 static int map_get_next_key(union bpf_attr *attr)
829 {
830 	void __user *ukey = u64_to_user_ptr(attr->key);
831 	void __user *unext_key = u64_to_user_ptr(attr->next_key);
832 	int ufd = attr->map_fd;
833 	struct bpf_map *map;
834 	void *key, *next_key;
835 	struct fd f;
836 	int err;
837 
838 	if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
839 		return -EINVAL;
840 
841 	f = fdget(ufd);
842 	map = __bpf_map_get(f);
843 	if (IS_ERR(map))
844 		return PTR_ERR(map);
845 
846 	if (!(f.file->f_mode & FMODE_CAN_READ)) {
847 		err = -EPERM;
848 		goto err_put;
849 	}
850 
851 	if (ukey) {
852 		key = memdup_user(ukey, map->key_size);
853 		if (IS_ERR(key)) {
854 			err = PTR_ERR(key);
855 			goto err_put;
856 		}
857 	} else {
858 		key = NULL;
859 	}
860 
861 	err = -ENOMEM;
862 	next_key = kmalloc(map->key_size, GFP_USER);
863 	if (!next_key)
864 		goto free_key;
865 
866 	if (bpf_map_is_dev_bound(map)) {
867 		err = bpf_map_offload_get_next_key(map, key, next_key);
868 		goto out;
869 	}
870 
871 	rcu_read_lock();
872 	err = map->ops->map_get_next_key(map, key, next_key);
873 	rcu_read_unlock();
874 out:
875 	if (err)
876 		goto free_next_key;
877 
878 	err = -EFAULT;
879 	if (copy_to_user(unext_key, next_key, map->key_size) != 0)
880 		goto free_next_key;
881 
882 	trace_bpf_map_next_key(map, ufd, key, next_key);
883 	err = 0;
884 
885 free_next_key:
886 	kfree(next_key);
887 free_key:
888 	kfree(key);
889 err_put:
890 	fdput(f);
891 	return err;
892 }
893 
894 static const struct bpf_prog_ops * const bpf_prog_types[] = {
895 #define BPF_PROG_TYPE(_id, _name) \
896 	[_id] = & _name ## _prog_ops,
897 #define BPF_MAP_TYPE(_id, _ops)
898 #include <linux/bpf_types.h>
899 #undef BPF_PROG_TYPE
900 #undef BPF_MAP_TYPE
901 };
902 
903 static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
904 {
905 	if (type >= ARRAY_SIZE(bpf_prog_types) || !bpf_prog_types[type])
906 		return -EINVAL;
907 
908 	if (!bpf_prog_is_dev_bound(prog->aux))
909 		prog->aux->ops = bpf_prog_types[type];
910 	else
911 		prog->aux->ops = &bpf_offload_prog_ops;
912 	prog->type = type;
913 	return 0;
914 }
915 
916 /* drop refcnt on maps used by eBPF program and free auxilary data */
917 static void free_used_maps(struct bpf_prog_aux *aux)
918 {
919 	int i;
920 
921 	for (i = 0; i < aux->used_map_cnt; i++)
922 		bpf_map_put(aux->used_maps[i]);
923 
924 	kfree(aux->used_maps);
925 }
926 
927 int __bpf_prog_charge(struct user_struct *user, u32 pages)
928 {
929 	unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
930 	unsigned long user_bufs;
931 
932 	if (user) {
933 		user_bufs = atomic_long_add_return(pages, &user->locked_vm);
934 		if (user_bufs > memlock_limit) {
935 			atomic_long_sub(pages, &user->locked_vm);
936 			return -EPERM;
937 		}
938 	}
939 
940 	return 0;
941 }
942 
943 void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
944 {
945 	if (user)
946 		atomic_long_sub(pages, &user->locked_vm);
947 }
948 
949 static int bpf_prog_charge_memlock(struct bpf_prog *prog)
950 {
951 	struct user_struct *user = get_current_user();
952 	int ret;
953 
954 	ret = __bpf_prog_charge(user, prog->pages);
955 	if (ret) {
956 		free_uid(user);
957 		return ret;
958 	}
959 
960 	prog->aux->user = user;
961 	return 0;
962 }
963 
964 static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
965 {
966 	struct user_struct *user = prog->aux->user;
967 
968 	__bpf_prog_uncharge(user, prog->pages);
969 	free_uid(user);
970 }
971 
972 static int bpf_prog_alloc_id(struct bpf_prog *prog)
973 {
974 	int id;
975 
976 	idr_preload(GFP_KERNEL);
977 	spin_lock_bh(&prog_idr_lock);
978 	id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
979 	if (id > 0)
980 		prog->aux->id = id;
981 	spin_unlock_bh(&prog_idr_lock);
982 	idr_preload_end();
983 
984 	/* id is in [1, INT_MAX) */
985 	if (WARN_ON_ONCE(!id))
986 		return -ENOSPC;
987 
988 	return id > 0 ? 0 : id;
989 }
990 
991 void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
992 {
993 	/* cBPF to eBPF migrations are currently not in the idr store.
994 	 * Offloaded programs are removed from the store when their device
995 	 * disappears - even if someone grabs an fd to them they are unusable,
996 	 * simply waiting for refcnt to drop to be freed.
997 	 */
998 	if (!prog->aux->id)
999 		return;
1000 
1001 	if (do_idr_lock)
1002 		spin_lock_bh(&prog_idr_lock);
1003 	else
1004 		__acquire(&prog_idr_lock);
1005 
1006 	idr_remove(&prog_idr, prog->aux->id);
1007 	prog->aux->id = 0;
1008 
1009 	if (do_idr_lock)
1010 		spin_unlock_bh(&prog_idr_lock);
1011 	else
1012 		__release(&prog_idr_lock);
1013 }
1014 
1015 static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1016 {
1017 	struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1018 
1019 	free_used_maps(aux);
1020 	bpf_prog_uncharge_memlock(aux->prog);
1021 	security_bpf_prog_free(aux);
1022 	bpf_prog_free(aux->prog);
1023 }
1024 
1025 static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1026 {
1027 	if (atomic_dec_and_test(&prog->aux->refcnt)) {
1028 		int i;
1029 
1030 		trace_bpf_prog_put_rcu(prog);
1031 		/* bpf_prog_free_id() must be called first */
1032 		bpf_prog_free_id(prog, do_idr_lock);
1033 
1034 		for (i = 0; i < prog->aux->func_cnt; i++)
1035 			bpf_prog_kallsyms_del(prog->aux->func[i]);
1036 		bpf_prog_kallsyms_del(prog);
1037 
1038 		call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1039 	}
1040 }
1041 
1042 void bpf_prog_put(struct bpf_prog *prog)
1043 {
1044 	__bpf_prog_put(prog, true);
1045 }
1046 EXPORT_SYMBOL_GPL(bpf_prog_put);
1047 
1048 static int bpf_prog_release(struct inode *inode, struct file *filp)
1049 {
1050 	struct bpf_prog *prog = filp->private_data;
1051 
1052 	bpf_prog_put(prog);
1053 	return 0;
1054 }
1055 
1056 #ifdef CONFIG_PROC_FS
1057 static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1058 {
1059 	const struct bpf_prog *prog = filp->private_data;
1060 	char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1061 
1062 	bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
1063 	seq_printf(m,
1064 		   "prog_type:\t%u\n"
1065 		   "prog_jited:\t%u\n"
1066 		   "prog_tag:\t%s\n"
1067 		   "memlock:\t%llu\n",
1068 		   prog->type,
1069 		   prog->jited,
1070 		   prog_tag,
1071 		   prog->pages * 1ULL << PAGE_SHIFT);
1072 }
1073 #endif
1074 
1075 const struct file_operations bpf_prog_fops = {
1076 #ifdef CONFIG_PROC_FS
1077 	.show_fdinfo	= bpf_prog_show_fdinfo,
1078 #endif
1079 	.release	= bpf_prog_release,
1080 	.read		= bpf_dummy_read,
1081 	.write		= bpf_dummy_write,
1082 };
1083 
1084 int bpf_prog_new_fd(struct bpf_prog *prog)
1085 {
1086 	int ret;
1087 
1088 	ret = security_bpf_prog(prog);
1089 	if (ret < 0)
1090 		return ret;
1091 
1092 	return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1093 				O_RDWR | O_CLOEXEC);
1094 }
1095 
1096 static struct bpf_prog *____bpf_prog_get(struct fd f)
1097 {
1098 	if (!f.file)
1099 		return ERR_PTR(-EBADF);
1100 	if (f.file->f_op != &bpf_prog_fops) {
1101 		fdput(f);
1102 		return ERR_PTR(-EINVAL);
1103 	}
1104 
1105 	return f.file->private_data;
1106 }
1107 
1108 struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
1109 {
1110 	if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
1111 		atomic_sub(i, &prog->aux->refcnt);
1112 		return ERR_PTR(-EBUSY);
1113 	}
1114 	return prog;
1115 }
1116 EXPORT_SYMBOL_GPL(bpf_prog_add);
1117 
1118 void bpf_prog_sub(struct bpf_prog *prog, int i)
1119 {
1120 	/* Only to be used for undoing previous bpf_prog_add() in some
1121 	 * error path. We still know that another entity in our call
1122 	 * path holds a reference to the program, thus atomic_sub() can
1123 	 * be safely used in such cases!
1124 	 */
1125 	WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1126 }
1127 EXPORT_SYMBOL_GPL(bpf_prog_sub);
1128 
1129 struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
1130 {
1131 	return bpf_prog_add(prog, 1);
1132 }
1133 EXPORT_SYMBOL_GPL(bpf_prog_inc);
1134 
1135 /* prog_idr_lock should have been held */
1136 struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1137 {
1138 	int refold;
1139 
1140 	refold = __atomic_add_unless(&prog->aux->refcnt, 1, 0);
1141 
1142 	if (refold >= BPF_MAX_REFCNT) {
1143 		__bpf_prog_put(prog, false);
1144 		return ERR_PTR(-EBUSY);
1145 	}
1146 
1147 	if (!refold)
1148 		return ERR_PTR(-ENOENT);
1149 
1150 	return prog;
1151 }
1152 EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1153 
1154 bool bpf_prog_get_ok(struct bpf_prog *prog,
1155 			    enum bpf_prog_type *attach_type, bool attach_drv)
1156 {
1157 	/* not an attachment, just a refcount inc, always allow */
1158 	if (!attach_type)
1159 		return true;
1160 
1161 	if (prog->type != *attach_type)
1162 		return false;
1163 	if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1164 		return false;
1165 
1166 	return true;
1167 }
1168 
1169 static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1170 				       bool attach_drv)
1171 {
1172 	struct fd f = fdget(ufd);
1173 	struct bpf_prog *prog;
1174 
1175 	prog = ____bpf_prog_get(f);
1176 	if (IS_ERR(prog))
1177 		return prog;
1178 	if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1179 		prog = ERR_PTR(-EINVAL);
1180 		goto out;
1181 	}
1182 
1183 	prog = bpf_prog_inc(prog);
1184 out:
1185 	fdput(f);
1186 	return prog;
1187 }
1188 
1189 struct bpf_prog *bpf_prog_get(u32 ufd)
1190 {
1191 	return __bpf_prog_get(ufd, NULL, false);
1192 }
1193 
1194 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1195 				       bool attach_drv)
1196 {
1197 	struct bpf_prog *prog = __bpf_prog_get(ufd, &type, attach_drv);
1198 
1199 	if (!IS_ERR(prog))
1200 		trace_bpf_prog_get_type(prog);
1201 	return prog;
1202 }
1203 EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1204 
1205 /* Initially all BPF programs could be loaded w/o specifying
1206  * expected_attach_type. Later for some of them specifying expected_attach_type
1207  * at load time became required so that program could be validated properly.
1208  * Programs of types that are allowed to be loaded both w/ and w/o (for
1209  * backward compatibility) expected_attach_type, should have the default attach
1210  * type assigned to expected_attach_type for the latter case, so that it can be
1211  * validated later at attach time.
1212  *
1213  * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1214  * prog type requires it but has some attach types that have to be backward
1215  * compatible.
1216  */
1217 static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1218 {
1219 	switch (attr->prog_type) {
1220 	case BPF_PROG_TYPE_CGROUP_SOCK:
1221 		/* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1222 		 * exist so checking for non-zero is the way to go here.
1223 		 */
1224 		if (!attr->expected_attach_type)
1225 			attr->expected_attach_type =
1226 				BPF_CGROUP_INET_SOCK_CREATE;
1227 		break;
1228 	}
1229 }
1230 
1231 static int
1232 bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
1233 				enum bpf_attach_type expected_attach_type)
1234 {
1235 	switch (prog_type) {
1236 	case BPF_PROG_TYPE_CGROUP_SOCK:
1237 		switch (expected_attach_type) {
1238 		case BPF_CGROUP_INET_SOCK_CREATE:
1239 		case BPF_CGROUP_INET4_POST_BIND:
1240 		case BPF_CGROUP_INET6_POST_BIND:
1241 			return 0;
1242 		default:
1243 			return -EINVAL;
1244 		}
1245 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1246 		switch (expected_attach_type) {
1247 		case BPF_CGROUP_INET4_BIND:
1248 		case BPF_CGROUP_INET6_BIND:
1249 		case BPF_CGROUP_INET4_CONNECT:
1250 		case BPF_CGROUP_INET6_CONNECT:
1251 			return 0;
1252 		default:
1253 			return -EINVAL;
1254 		}
1255 	default:
1256 		return 0;
1257 	}
1258 }
1259 
1260 /* last field in 'union bpf_attr' used by this command */
1261 #define	BPF_PROG_LOAD_LAST_FIELD expected_attach_type
1262 
1263 static int bpf_prog_load(union bpf_attr *attr)
1264 {
1265 	enum bpf_prog_type type = attr->prog_type;
1266 	struct bpf_prog *prog;
1267 	int err;
1268 	char license[128];
1269 	bool is_gpl;
1270 
1271 	if (CHECK_ATTR(BPF_PROG_LOAD))
1272 		return -EINVAL;
1273 
1274 	if (attr->prog_flags & ~BPF_F_STRICT_ALIGNMENT)
1275 		return -EINVAL;
1276 
1277 	/* copy eBPF program license from user space */
1278 	if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
1279 			      sizeof(license) - 1) < 0)
1280 		return -EFAULT;
1281 	license[sizeof(license) - 1] = 0;
1282 
1283 	/* eBPF programs must be GPL compatible to use GPL-ed functions */
1284 	is_gpl = license_is_gpl_compatible(license);
1285 
1286 	if (attr->insn_cnt == 0 || attr->insn_cnt > BPF_MAXINSNS)
1287 		return -E2BIG;
1288 
1289 	if (type == BPF_PROG_TYPE_KPROBE &&
1290 	    attr->kern_version != LINUX_VERSION_CODE)
1291 		return -EINVAL;
1292 
1293 	if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
1294 	    type != BPF_PROG_TYPE_CGROUP_SKB &&
1295 	    !capable(CAP_SYS_ADMIN))
1296 		return -EPERM;
1297 
1298 	bpf_prog_load_fixup_attach_type(attr);
1299 	if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
1300 		return -EINVAL;
1301 
1302 	/* plain bpf_prog allocation */
1303 	prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
1304 	if (!prog)
1305 		return -ENOMEM;
1306 
1307 	prog->expected_attach_type = attr->expected_attach_type;
1308 
1309 	prog->aux->offload_requested = !!attr->prog_ifindex;
1310 
1311 	err = security_bpf_prog_alloc(prog->aux);
1312 	if (err)
1313 		goto free_prog_nouncharge;
1314 
1315 	err = bpf_prog_charge_memlock(prog);
1316 	if (err)
1317 		goto free_prog_sec;
1318 
1319 	prog->len = attr->insn_cnt;
1320 
1321 	err = -EFAULT;
1322 	if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1323 			   bpf_prog_insn_size(prog)) != 0)
1324 		goto free_prog;
1325 
1326 	prog->orig_prog = NULL;
1327 	prog->jited = 0;
1328 
1329 	atomic_set(&prog->aux->refcnt, 1);
1330 	prog->gpl_compatible = is_gpl ? 1 : 0;
1331 
1332 	if (bpf_prog_is_dev_bound(prog->aux)) {
1333 		err = bpf_prog_offload_init(prog, attr);
1334 		if (err)
1335 			goto free_prog;
1336 	}
1337 
1338 	/* find program type: socket_filter vs tracing_filter */
1339 	err = find_prog_type(type, prog);
1340 	if (err < 0)
1341 		goto free_prog;
1342 
1343 	prog->aux->load_time = ktime_get_boot_ns();
1344 	err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1345 	if (err)
1346 		goto free_prog;
1347 
1348 	/* run eBPF verifier */
1349 	err = bpf_check(&prog, attr);
1350 	if (err < 0)
1351 		goto free_used_maps;
1352 
1353 	/* eBPF program is ready to be JITed */
1354 	if (!prog->bpf_func)
1355 		prog = bpf_prog_select_runtime(prog, &err);
1356 	if (err < 0)
1357 		goto free_used_maps;
1358 
1359 	err = bpf_prog_alloc_id(prog);
1360 	if (err)
1361 		goto free_used_maps;
1362 
1363 	err = bpf_prog_new_fd(prog);
1364 	if (err < 0) {
1365 		/* failed to allocate fd.
1366 		 * bpf_prog_put() is needed because the above
1367 		 * bpf_prog_alloc_id() has published the prog
1368 		 * to the userspace and the userspace may
1369 		 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1370 		 */
1371 		bpf_prog_put(prog);
1372 		return err;
1373 	}
1374 
1375 	bpf_prog_kallsyms_add(prog);
1376 	trace_bpf_prog_load(prog, err);
1377 	return err;
1378 
1379 free_used_maps:
1380 	free_used_maps(prog->aux);
1381 free_prog:
1382 	bpf_prog_uncharge_memlock(prog);
1383 free_prog_sec:
1384 	security_bpf_prog_free(prog->aux);
1385 free_prog_nouncharge:
1386 	bpf_prog_free(prog);
1387 	return err;
1388 }
1389 
1390 #define BPF_OBJ_LAST_FIELD file_flags
1391 
1392 static int bpf_obj_pin(const union bpf_attr *attr)
1393 {
1394 	if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1395 		return -EINVAL;
1396 
1397 	return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1398 }
1399 
1400 static int bpf_obj_get(const union bpf_attr *attr)
1401 {
1402 	if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
1403 	    attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1404 		return -EINVAL;
1405 
1406 	return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
1407 				attr->file_flags);
1408 }
1409 
1410 struct bpf_raw_tracepoint {
1411 	struct bpf_raw_event_map *btp;
1412 	struct bpf_prog *prog;
1413 };
1414 
1415 static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1416 {
1417 	struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1418 
1419 	if (raw_tp->prog) {
1420 		bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1421 		bpf_prog_put(raw_tp->prog);
1422 	}
1423 	kfree(raw_tp);
1424 	return 0;
1425 }
1426 
1427 static const struct file_operations bpf_raw_tp_fops = {
1428 	.release	= bpf_raw_tracepoint_release,
1429 	.read		= bpf_dummy_read,
1430 	.write		= bpf_dummy_write,
1431 };
1432 
1433 #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1434 
1435 static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1436 {
1437 	struct bpf_raw_tracepoint *raw_tp;
1438 	struct bpf_raw_event_map *btp;
1439 	struct bpf_prog *prog;
1440 	char tp_name[128];
1441 	int tp_fd, err;
1442 
1443 	if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1444 			      sizeof(tp_name) - 1) < 0)
1445 		return -EFAULT;
1446 	tp_name[sizeof(tp_name) - 1] = 0;
1447 
1448 	btp = bpf_find_raw_tracepoint(tp_name);
1449 	if (!btp)
1450 		return -ENOENT;
1451 
1452 	raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1453 	if (!raw_tp)
1454 		return -ENOMEM;
1455 	raw_tp->btp = btp;
1456 
1457 	prog = bpf_prog_get_type(attr->raw_tracepoint.prog_fd,
1458 				 BPF_PROG_TYPE_RAW_TRACEPOINT);
1459 	if (IS_ERR(prog)) {
1460 		err = PTR_ERR(prog);
1461 		goto out_free_tp;
1462 	}
1463 
1464 	err = bpf_probe_register(raw_tp->btp, prog);
1465 	if (err)
1466 		goto out_put_prog;
1467 
1468 	raw_tp->prog = prog;
1469 	tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1470 				 O_CLOEXEC);
1471 	if (tp_fd < 0) {
1472 		bpf_probe_unregister(raw_tp->btp, prog);
1473 		err = tp_fd;
1474 		goto out_put_prog;
1475 	}
1476 	return tp_fd;
1477 
1478 out_put_prog:
1479 	bpf_prog_put(prog);
1480 out_free_tp:
1481 	kfree(raw_tp);
1482 	return err;
1483 }
1484 
1485 #ifdef CONFIG_CGROUP_BPF
1486 
1487 static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
1488 					     enum bpf_attach_type attach_type)
1489 {
1490 	switch (prog->type) {
1491 	case BPF_PROG_TYPE_CGROUP_SOCK:
1492 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1493 		return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
1494 	default:
1495 		return 0;
1496 	}
1497 }
1498 
1499 #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1500 
1501 static int sockmap_get_from_fd(const union bpf_attr *attr,
1502 			       int type, bool attach)
1503 {
1504 	struct bpf_prog *prog = NULL;
1505 	int ufd = attr->target_fd;
1506 	struct bpf_map *map;
1507 	struct fd f;
1508 	int err;
1509 
1510 	f = fdget(ufd);
1511 	map = __bpf_map_get(f);
1512 	if (IS_ERR(map))
1513 		return PTR_ERR(map);
1514 
1515 	if (attach) {
1516 		prog = bpf_prog_get_type(attr->attach_bpf_fd, type);
1517 		if (IS_ERR(prog)) {
1518 			fdput(f);
1519 			return PTR_ERR(prog);
1520 		}
1521 	}
1522 
1523 	err = sock_map_prog(map, prog, attr->attach_type);
1524 	if (err) {
1525 		fdput(f);
1526 		if (prog)
1527 			bpf_prog_put(prog);
1528 		return err;
1529 	}
1530 
1531 	fdput(f);
1532 	return 0;
1533 }
1534 
1535 #define BPF_F_ATTACH_MASK \
1536 	(BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1537 
1538 static int bpf_prog_attach(const union bpf_attr *attr)
1539 {
1540 	enum bpf_prog_type ptype;
1541 	struct bpf_prog *prog;
1542 	struct cgroup *cgrp;
1543 	int ret;
1544 
1545 	if (!capable(CAP_NET_ADMIN))
1546 		return -EPERM;
1547 
1548 	if (CHECK_ATTR(BPF_PROG_ATTACH))
1549 		return -EINVAL;
1550 
1551 	if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
1552 		return -EINVAL;
1553 
1554 	switch (attr->attach_type) {
1555 	case BPF_CGROUP_INET_INGRESS:
1556 	case BPF_CGROUP_INET_EGRESS:
1557 		ptype = BPF_PROG_TYPE_CGROUP_SKB;
1558 		break;
1559 	case BPF_CGROUP_INET_SOCK_CREATE:
1560 	case BPF_CGROUP_INET4_POST_BIND:
1561 	case BPF_CGROUP_INET6_POST_BIND:
1562 		ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1563 		break;
1564 	case BPF_CGROUP_INET4_BIND:
1565 	case BPF_CGROUP_INET6_BIND:
1566 	case BPF_CGROUP_INET4_CONNECT:
1567 	case BPF_CGROUP_INET6_CONNECT:
1568 		ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1569 		break;
1570 	case BPF_CGROUP_SOCK_OPS:
1571 		ptype = BPF_PROG_TYPE_SOCK_OPS;
1572 		break;
1573 	case BPF_CGROUP_DEVICE:
1574 		ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1575 		break;
1576 	case BPF_SK_MSG_VERDICT:
1577 		return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, true);
1578 	case BPF_SK_SKB_STREAM_PARSER:
1579 	case BPF_SK_SKB_STREAM_VERDICT:
1580 		return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, true);
1581 	default:
1582 		return -EINVAL;
1583 	}
1584 
1585 	prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1586 	if (IS_ERR(prog))
1587 		return PTR_ERR(prog);
1588 
1589 	if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
1590 		bpf_prog_put(prog);
1591 		return -EINVAL;
1592 	}
1593 
1594 	cgrp = cgroup_get_from_fd(attr->target_fd);
1595 	if (IS_ERR(cgrp)) {
1596 		bpf_prog_put(prog);
1597 		return PTR_ERR(cgrp);
1598 	}
1599 
1600 	ret = cgroup_bpf_attach(cgrp, prog, attr->attach_type,
1601 				attr->attach_flags);
1602 	if (ret)
1603 		bpf_prog_put(prog);
1604 	cgroup_put(cgrp);
1605 
1606 	return ret;
1607 }
1608 
1609 #define BPF_PROG_DETACH_LAST_FIELD attach_type
1610 
1611 static int bpf_prog_detach(const union bpf_attr *attr)
1612 {
1613 	enum bpf_prog_type ptype;
1614 	struct bpf_prog *prog;
1615 	struct cgroup *cgrp;
1616 	int ret;
1617 
1618 	if (!capable(CAP_NET_ADMIN))
1619 		return -EPERM;
1620 
1621 	if (CHECK_ATTR(BPF_PROG_DETACH))
1622 		return -EINVAL;
1623 
1624 	switch (attr->attach_type) {
1625 	case BPF_CGROUP_INET_INGRESS:
1626 	case BPF_CGROUP_INET_EGRESS:
1627 		ptype = BPF_PROG_TYPE_CGROUP_SKB;
1628 		break;
1629 	case BPF_CGROUP_INET_SOCK_CREATE:
1630 	case BPF_CGROUP_INET4_POST_BIND:
1631 	case BPF_CGROUP_INET6_POST_BIND:
1632 		ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1633 		break;
1634 	case BPF_CGROUP_INET4_BIND:
1635 	case BPF_CGROUP_INET6_BIND:
1636 	case BPF_CGROUP_INET4_CONNECT:
1637 	case BPF_CGROUP_INET6_CONNECT:
1638 		ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
1639 		break;
1640 	case BPF_CGROUP_SOCK_OPS:
1641 		ptype = BPF_PROG_TYPE_SOCK_OPS;
1642 		break;
1643 	case BPF_CGROUP_DEVICE:
1644 		ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1645 		break;
1646 	case BPF_SK_MSG_VERDICT:
1647 		return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_MSG, false);
1648 	case BPF_SK_SKB_STREAM_PARSER:
1649 	case BPF_SK_SKB_STREAM_VERDICT:
1650 		return sockmap_get_from_fd(attr, BPF_PROG_TYPE_SK_SKB, false);
1651 	default:
1652 		return -EINVAL;
1653 	}
1654 
1655 	cgrp = cgroup_get_from_fd(attr->target_fd);
1656 	if (IS_ERR(cgrp))
1657 		return PTR_ERR(cgrp);
1658 
1659 	prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1660 	if (IS_ERR(prog))
1661 		prog = NULL;
1662 
1663 	ret = cgroup_bpf_detach(cgrp, prog, attr->attach_type, 0);
1664 	if (prog)
1665 		bpf_prog_put(prog);
1666 	cgroup_put(cgrp);
1667 	return ret;
1668 }
1669 
1670 #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
1671 
1672 static int bpf_prog_query(const union bpf_attr *attr,
1673 			  union bpf_attr __user *uattr)
1674 {
1675 	struct cgroup *cgrp;
1676 	int ret;
1677 
1678 	if (!capable(CAP_NET_ADMIN))
1679 		return -EPERM;
1680 	if (CHECK_ATTR(BPF_PROG_QUERY))
1681 		return -EINVAL;
1682 	if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
1683 		return -EINVAL;
1684 
1685 	switch (attr->query.attach_type) {
1686 	case BPF_CGROUP_INET_INGRESS:
1687 	case BPF_CGROUP_INET_EGRESS:
1688 	case BPF_CGROUP_INET_SOCK_CREATE:
1689 	case BPF_CGROUP_INET4_BIND:
1690 	case BPF_CGROUP_INET6_BIND:
1691 	case BPF_CGROUP_INET4_POST_BIND:
1692 	case BPF_CGROUP_INET6_POST_BIND:
1693 	case BPF_CGROUP_INET4_CONNECT:
1694 	case BPF_CGROUP_INET6_CONNECT:
1695 	case BPF_CGROUP_SOCK_OPS:
1696 	case BPF_CGROUP_DEVICE:
1697 		break;
1698 	default:
1699 		return -EINVAL;
1700 	}
1701 	cgrp = cgroup_get_from_fd(attr->query.target_fd);
1702 	if (IS_ERR(cgrp))
1703 		return PTR_ERR(cgrp);
1704 	ret = cgroup_bpf_query(cgrp, attr, uattr);
1705 	cgroup_put(cgrp);
1706 	return ret;
1707 }
1708 #endif /* CONFIG_CGROUP_BPF */
1709 
1710 #define BPF_PROG_TEST_RUN_LAST_FIELD test.duration
1711 
1712 static int bpf_prog_test_run(const union bpf_attr *attr,
1713 			     union bpf_attr __user *uattr)
1714 {
1715 	struct bpf_prog *prog;
1716 	int ret = -ENOTSUPP;
1717 
1718 	if (!capable(CAP_SYS_ADMIN))
1719 		return -EPERM;
1720 	if (CHECK_ATTR(BPF_PROG_TEST_RUN))
1721 		return -EINVAL;
1722 
1723 	prog = bpf_prog_get(attr->test.prog_fd);
1724 	if (IS_ERR(prog))
1725 		return PTR_ERR(prog);
1726 
1727 	if (prog->aux->ops->test_run)
1728 		ret = prog->aux->ops->test_run(prog, attr, uattr);
1729 
1730 	bpf_prog_put(prog);
1731 	return ret;
1732 }
1733 
1734 #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
1735 
1736 static int bpf_obj_get_next_id(const union bpf_attr *attr,
1737 			       union bpf_attr __user *uattr,
1738 			       struct idr *idr,
1739 			       spinlock_t *lock)
1740 {
1741 	u32 next_id = attr->start_id;
1742 	int err = 0;
1743 
1744 	if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
1745 		return -EINVAL;
1746 
1747 	if (!capable(CAP_SYS_ADMIN))
1748 		return -EPERM;
1749 
1750 	next_id++;
1751 	spin_lock_bh(lock);
1752 	if (!idr_get_next(idr, &next_id))
1753 		err = -ENOENT;
1754 	spin_unlock_bh(lock);
1755 
1756 	if (!err)
1757 		err = put_user(next_id, &uattr->next_id);
1758 
1759 	return err;
1760 }
1761 
1762 #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
1763 
1764 static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
1765 {
1766 	struct bpf_prog *prog;
1767 	u32 id = attr->prog_id;
1768 	int fd;
1769 
1770 	if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
1771 		return -EINVAL;
1772 
1773 	if (!capable(CAP_SYS_ADMIN))
1774 		return -EPERM;
1775 
1776 	spin_lock_bh(&prog_idr_lock);
1777 	prog = idr_find(&prog_idr, id);
1778 	if (prog)
1779 		prog = bpf_prog_inc_not_zero(prog);
1780 	else
1781 		prog = ERR_PTR(-ENOENT);
1782 	spin_unlock_bh(&prog_idr_lock);
1783 
1784 	if (IS_ERR(prog))
1785 		return PTR_ERR(prog);
1786 
1787 	fd = bpf_prog_new_fd(prog);
1788 	if (fd < 0)
1789 		bpf_prog_put(prog);
1790 
1791 	return fd;
1792 }
1793 
1794 #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
1795 
1796 static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
1797 {
1798 	struct bpf_map *map;
1799 	u32 id = attr->map_id;
1800 	int f_flags;
1801 	int fd;
1802 
1803 	if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
1804 	    attr->open_flags & ~BPF_OBJ_FLAG_MASK)
1805 		return -EINVAL;
1806 
1807 	if (!capable(CAP_SYS_ADMIN))
1808 		return -EPERM;
1809 
1810 	f_flags = bpf_get_file_flag(attr->open_flags);
1811 	if (f_flags < 0)
1812 		return f_flags;
1813 
1814 	spin_lock_bh(&map_idr_lock);
1815 	map = idr_find(&map_idr, id);
1816 	if (map)
1817 		map = bpf_map_inc_not_zero(map, true);
1818 	else
1819 		map = ERR_PTR(-ENOENT);
1820 	spin_unlock_bh(&map_idr_lock);
1821 
1822 	if (IS_ERR(map))
1823 		return PTR_ERR(map);
1824 
1825 	fd = bpf_map_new_fd(map, f_flags);
1826 	if (fd < 0)
1827 		bpf_map_put(map);
1828 
1829 	return fd;
1830 }
1831 
1832 static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1833 					      unsigned long addr)
1834 {
1835 	int i;
1836 
1837 	for (i = 0; i < prog->aux->used_map_cnt; i++)
1838 		if (prog->aux->used_maps[i] == (void *)addr)
1839 			return prog->aux->used_maps[i];
1840 	return NULL;
1841 }
1842 
1843 static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1844 {
1845 	const struct bpf_map *map;
1846 	struct bpf_insn *insns;
1847 	u64 imm;
1848 	int i;
1849 
1850 	insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1851 			GFP_USER);
1852 	if (!insns)
1853 		return insns;
1854 
1855 	for (i = 0; i < prog->len; i++) {
1856 		if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1857 			insns[i].code = BPF_JMP | BPF_CALL;
1858 			insns[i].imm = BPF_FUNC_tail_call;
1859 			/* fall-through */
1860 		}
1861 		if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1862 		    insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1863 			if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1864 				insns[i].code = BPF_JMP | BPF_CALL;
1865 			if (!bpf_dump_raw_ok())
1866 				insns[i].imm = 0;
1867 			continue;
1868 		}
1869 
1870 		if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1871 			continue;
1872 
1873 		imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1874 		map = bpf_map_from_imm(prog, imm);
1875 		if (map) {
1876 			insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1877 			insns[i].imm = map->id;
1878 			insns[i + 1].imm = 0;
1879 			continue;
1880 		}
1881 
1882 		if (!bpf_dump_raw_ok() &&
1883 		    imm == (unsigned long)prog->aux) {
1884 			insns[i].imm = 0;
1885 			insns[i + 1].imm = 0;
1886 			continue;
1887 		}
1888 	}
1889 
1890 	return insns;
1891 }
1892 
1893 static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
1894 				   const union bpf_attr *attr,
1895 				   union bpf_attr __user *uattr)
1896 {
1897 	struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1898 	struct bpf_prog_info info = {};
1899 	u32 info_len = attr->info.info_len;
1900 	char __user *uinsns;
1901 	u32 ulen;
1902 	int err;
1903 
1904 	err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1905 	if (err)
1906 		return err;
1907 	info_len = min_t(u32, sizeof(info), info_len);
1908 
1909 	if (copy_from_user(&info, uinfo, info_len))
1910 		return -EFAULT;
1911 
1912 	info.type = prog->type;
1913 	info.id = prog->aux->id;
1914 	info.load_time = prog->aux->load_time;
1915 	info.created_by_uid = from_kuid_munged(current_user_ns(),
1916 					       prog->aux->user->uid);
1917 
1918 	memcpy(info.tag, prog->tag, sizeof(prog->tag));
1919 	memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
1920 
1921 	ulen = info.nr_map_ids;
1922 	info.nr_map_ids = prog->aux->used_map_cnt;
1923 	ulen = min_t(u32, info.nr_map_ids, ulen);
1924 	if (ulen) {
1925 		u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
1926 		u32 i;
1927 
1928 		for (i = 0; i < ulen; i++)
1929 			if (put_user(prog->aux->used_maps[i]->id,
1930 				     &user_map_ids[i]))
1931 				return -EFAULT;
1932 	}
1933 
1934 	if (!capable(CAP_SYS_ADMIN)) {
1935 		info.jited_prog_len = 0;
1936 		info.xlated_prog_len = 0;
1937 		goto done;
1938 	}
1939 
1940 	ulen = info.xlated_prog_len;
1941 	info.xlated_prog_len = bpf_prog_insn_size(prog);
1942 	if (info.xlated_prog_len && ulen) {
1943 		struct bpf_insn *insns_sanitized;
1944 		bool fault;
1945 
1946 		if (prog->blinded && !bpf_dump_raw_ok()) {
1947 			info.xlated_prog_insns = 0;
1948 			goto done;
1949 		}
1950 		insns_sanitized = bpf_insn_prepare_dump(prog);
1951 		if (!insns_sanitized)
1952 			return -ENOMEM;
1953 		uinsns = u64_to_user_ptr(info.xlated_prog_insns);
1954 		ulen = min_t(u32, info.xlated_prog_len, ulen);
1955 		fault = copy_to_user(uinsns, insns_sanitized, ulen);
1956 		kfree(insns_sanitized);
1957 		if (fault)
1958 			return -EFAULT;
1959 	}
1960 
1961 	if (bpf_prog_is_dev_bound(prog->aux)) {
1962 		err = bpf_prog_offload_info_fill(&info, prog);
1963 		if (err)
1964 			return err;
1965 		goto done;
1966 	}
1967 
1968 	/* NOTE: the following code is supposed to be skipped for offload.
1969 	 * bpf_prog_offload_info_fill() is the place to fill similar fields
1970 	 * for offload.
1971 	 */
1972 	ulen = info.jited_prog_len;
1973 	info.jited_prog_len = prog->jited_len;
1974 	if (info.jited_prog_len && ulen) {
1975 		if (bpf_dump_raw_ok()) {
1976 			uinsns = u64_to_user_ptr(info.jited_prog_insns);
1977 			ulen = min_t(u32, info.jited_prog_len, ulen);
1978 			if (copy_to_user(uinsns, prog->bpf_func, ulen))
1979 				return -EFAULT;
1980 		} else {
1981 			info.jited_prog_insns = 0;
1982 		}
1983 	}
1984 
1985 done:
1986 	if (copy_to_user(uinfo, &info, info_len) ||
1987 	    put_user(info_len, &uattr->info.info_len))
1988 		return -EFAULT;
1989 
1990 	return 0;
1991 }
1992 
1993 static int bpf_map_get_info_by_fd(struct bpf_map *map,
1994 				  const union bpf_attr *attr,
1995 				  union bpf_attr __user *uattr)
1996 {
1997 	struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
1998 	struct bpf_map_info info = {};
1999 	u32 info_len = attr->info.info_len;
2000 	int err;
2001 
2002 	err = check_uarg_tail_zero(uinfo, sizeof(info), info_len);
2003 	if (err)
2004 		return err;
2005 	info_len = min_t(u32, sizeof(info), info_len);
2006 
2007 	info.type = map->map_type;
2008 	info.id = map->id;
2009 	info.key_size = map->key_size;
2010 	info.value_size = map->value_size;
2011 	info.max_entries = map->max_entries;
2012 	info.map_flags = map->map_flags;
2013 	memcpy(info.name, map->name, sizeof(map->name));
2014 
2015 	if (bpf_map_is_dev_bound(map)) {
2016 		err = bpf_map_offload_info_fill(&info, map);
2017 		if (err)
2018 			return err;
2019 	}
2020 
2021 	if (copy_to_user(uinfo, &info, info_len) ||
2022 	    put_user(info_len, &uattr->info.info_len))
2023 		return -EFAULT;
2024 
2025 	return 0;
2026 }
2027 
2028 #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
2029 
2030 static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
2031 				  union bpf_attr __user *uattr)
2032 {
2033 	int ufd = attr->info.bpf_fd;
2034 	struct fd f;
2035 	int err;
2036 
2037 	if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
2038 		return -EINVAL;
2039 
2040 	f = fdget(ufd);
2041 	if (!f.file)
2042 		return -EBADFD;
2043 
2044 	if (f.file->f_op == &bpf_prog_fops)
2045 		err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
2046 					      uattr);
2047 	else if (f.file->f_op == &bpf_map_fops)
2048 		err = bpf_map_get_info_by_fd(f.file->private_data, attr,
2049 					     uattr);
2050 	else if (f.file->f_op == &btf_fops)
2051 		err = btf_get_info_by_fd(f.file->private_data, attr, uattr);
2052 	else
2053 		err = -EINVAL;
2054 
2055 	fdput(f);
2056 	return err;
2057 }
2058 
2059 #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2060 
2061 static int bpf_btf_load(const union bpf_attr *attr)
2062 {
2063 	if (CHECK_ATTR(BPF_BTF_LOAD))
2064 		return -EINVAL;
2065 
2066 	if (!capable(CAP_SYS_ADMIN))
2067 		return -EPERM;
2068 
2069 	return btf_new_fd(attr);
2070 }
2071 
2072 SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
2073 {
2074 	union bpf_attr attr = {};
2075 	int err;
2076 
2077 	if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
2078 		return -EPERM;
2079 
2080 	err = check_uarg_tail_zero(uattr, sizeof(attr), size);
2081 	if (err)
2082 		return err;
2083 	size = min_t(u32, size, sizeof(attr));
2084 
2085 	/* copy attributes from user space, may be less than sizeof(bpf_attr) */
2086 	if (copy_from_user(&attr, uattr, size) != 0)
2087 		return -EFAULT;
2088 
2089 	err = security_bpf(cmd, &attr, size);
2090 	if (err < 0)
2091 		return err;
2092 
2093 	switch (cmd) {
2094 	case BPF_MAP_CREATE:
2095 		err = map_create(&attr);
2096 		break;
2097 	case BPF_MAP_LOOKUP_ELEM:
2098 		err = map_lookup_elem(&attr);
2099 		break;
2100 	case BPF_MAP_UPDATE_ELEM:
2101 		err = map_update_elem(&attr);
2102 		break;
2103 	case BPF_MAP_DELETE_ELEM:
2104 		err = map_delete_elem(&attr);
2105 		break;
2106 	case BPF_MAP_GET_NEXT_KEY:
2107 		err = map_get_next_key(&attr);
2108 		break;
2109 	case BPF_PROG_LOAD:
2110 		err = bpf_prog_load(&attr);
2111 		break;
2112 	case BPF_OBJ_PIN:
2113 		err = bpf_obj_pin(&attr);
2114 		break;
2115 	case BPF_OBJ_GET:
2116 		err = bpf_obj_get(&attr);
2117 		break;
2118 #ifdef CONFIG_CGROUP_BPF
2119 	case BPF_PROG_ATTACH:
2120 		err = bpf_prog_attach(&attr);
2121 		break;
2122 	case BPF_PROG_DETACH:
2123 		err = bpf_prog_detach(&attr);
2124 		break;
2125 	case BPF_PROG_QUERY:
2126 		err = bpf_prog_query(&attr, uattr);
2127 		break;
2128 #endif
2129 	case BPF_PROG_TEST_RUN:
2130 		err = bpf_prog_test_run(&attr, uattr);
2131 		break;
2132 	case BPF_PROG_GET_NEXT_ID:
2133 		err = bpf_obj_get_next_id(&attr, uattr,
2134 					  &prog_idr, &prog_idr_lock);
2135 		break;
2136 	case BPF_MAP_GET_NEXT_ID:
2137 		err = bpf_obj_get_next_id(&attr, uattr,
2138 					  &map_idr, &map_idr_lock);
2139 		break;
2140 	case BPF_PROG_GET_FD_BY_ID:
2141 		err = bpf_prog_get_fd_by_id(&attr);
2142 		break;
2143 	case BPF_MAP_GET_FD_BY_ID:
2144 		err = bpf_map_get_fd_by_id(&attr);
2145 		break;
2146 	case BPF_OBJ_GET_INFO_BY_FD:
2147 		err = bpf_obj_get_info_by_fd(&attr, uattr);
2148 		break;
2149 	case BPF_RAW_TRACEPOINT_OPEN:
2150 		err = bpf_raw_tracepoint_open(&attr);
2151 		break;
2152 	case BPF_BTF_LOAD:
2153 		err = bpf_btf_load(&attr);
2154 		break;
2155 	default:
2156 		err = -EINVAL;
2157 		break;
2158 	}
2159 
2160 	return err;
2161 }
2162