xref: /linux/kernel/bpf/syscall.c (revision b338cb921e6739ff59ce32f43342779fe5ffa732)
15b497af4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
299c55f7dSAlexei Starovoitov /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
399c55f7dSAlexei Starovoitov  */
499c55f7dSAlexei Starovoitov #include <linux/bpf.h>
5a67edbf4SDaniel Borkmann #include <linux/bpf_trace.h>
6f4364dcfSSean Young #include <linux/bpf_lirc.h>
7f56a653cSMartin KaFai Lau #include <linux/btf.h>
899c55f7dSAlexei Starovoitov #include <linux/syscalls.h>
999c55f7dSAlexei Starovoitov #include <linux/slab.h>
103f07c014SIngo Molnar #include <linux/sched/signal.h>
11d407bd25SDaniel Borkmann #include <linux/vmalloc.h>
12d407bd25SDaniel Borkmann #include <linux/mmzone.h>
1399c55f7dSAlexei Starovoitov #include <linux/anon_inodes.h>
1441bdc4b4SYonghong Song #include <linux/fdtable.h>
15db20fd2bSAlexei Starovoitov #include <linux/file.h>
1641bdc4b4SYonghong Song #include <linux/fs.h>
1709756af4SAlexei Starovoitov #include <linux/license.h>
1809756af4SAlexei Starovoitov #include <linux/filter.h>
192541517cSAlexei Starovoitov #include <linux/version.h>
20535e7b4bSMickaël Salaün #include <linux/kernel.h>
21dc4bb0e2SMartin KaFai Lau #include <linux/idr.h>
22cb4d2b3fSMartin KaFai Lau #include <linux/cred.h>
23cb4d2b3fSMartin KaFai Lau #include <linux/timekeeping.h>
24cb4d2b3fSMartin KaFai Lau #include <linux/ctype.h>
259ef09e35SMark Rutland #include <linux/nospec.h>
26bae141f5SDaniel Borkmann #include <linux/audit.h>
27ccfe29ebSAlexei Starovoitov #include <uapi/linux/btf.h>
28ca5999fdSMike Rapoport #include <linux/pgtable.h>
299e4e01dfSKP Singh #include <linux/bpf_lsm.h>
30457f4436SAndrii Nakryiko #include <linux/poll.h>
31a3fd7ceeSJakub Sitnicki #include <linux/bpf-netns.h>
3299c55f7dSAlexei Starovoitov 
33da765a2fSDaniel Borkmann #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
3414dc6f04SMartin KaFai Lau 			  (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
3514dc6f04SMartin KaFai Lau 			  (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
36da765a2fSDaniel Borkmann #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
3714dc6f04SMartin KaFai Lau #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
38da765a2fSDaniel Borkmann #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
39da765a2fSDaniel Borkmann 			IS_FD_HASH(map))
4014dc6f04SMartin KaFai Lau 
416e71b04aSChenbo Feng #define BPF_OBJ_FLAG_MASK   (BPF_F_RDONLY | BPF_F_WRONLY)
426e71b04aSChenbo Feng 
43b121d1e7SAlexei Starovoitov DEFINE_PER_CPU(int, bpf_prog_active);
44dc4bb0e2SMartin KaFai Lau static DEFINE_IDR(prog_idr);
45dc4bb0e2SMartin KaFai Lau static DEFINE_SPINLOCK(prog_idr_lock);
46f3f1c054SMartin KaFai Lau static DEFINE_IDR(map_idr);
47f3f1c054SMartin KaFai Lau static DEFINE_SPINLOCK(map_idr_lock);
48a3b80e10SAndrii Nakryiko static DEFINE_IDR(link_idr);
49a3b80e10SAndrii Nakryiko static DEFINE_SPINLOCK(link_idr_lock);
50b121d1e7SAlexei Starovoitov 
511be7f75dSAlexei Starovoitov int sysctl_unprivileged_bpf_disabled __read_mostly;
521be7f75dSAlexei Starovoitov 
5340077e0cSJohannes Berg static const struct bpf_map_ops * const bpf_map_types[] = {
5491cc1a99SAlexei Starovoitov #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
5540077e0cSJohannes Berg #define BPF_MAP_TYPE(_id, _ops) \
5640077e0cSJohannes Berg 	[_id] = &_ops,
57f2e10bffSAndrii Nakryiko #define BPF_LINK_TYPE(_id, _name)
5840077e0cSJohannes Berg #include <linux/bpf_types.h>
5940077e0cSJohannes Berg #undef BPF_PROG_TYPE
6040077e0cSJohannes Berg #undef BPF_MAP_TYPE
61f2e10bffSAndrii Nakryiko #undef BPF_LINK_TYPE
6240077e0cSJohannes Berg };
6399c55f7dSAlexei Starovoitov 
64752ba56fSMickaël Salaün /*
65752ba56fSMickaël Salaün  * If we're handed a bigger struct than we know of, ensure all the unknown bits
66752ba56fSMickaël Salaün  * are 0 - i.e. new user-space does not rely on any kernel feature extensions
67752ba56fSMickaël Salaün  * we don't know about yet.
68752ba56fSMickaël Salaün  *
69752ba56fSMickaël Salaün  * There is a ToCToU between this function call and the following
70752ba56fSMickaël Salaün  * copy_from_user() call. However, this is not a concern since this function is
71752ba56fSMickaël Salaün  * meant to be a future-proofing of bits.
72752ba56fSMickaël Salaün  */
73dcab51f1SMartin KaFai Lau int bpf_check_uarg_tail_zero(void __user *uaddr,
7458291a74SMickaël Salaün 			     size_t expected_size,
7558291a74SMickaël Salaün 			     size_t actual_size)
7658291a74SMickaël Salaün {
77b7e4b65fSAl Viro 	unsigned char __user *addr = uaddr + expected_size;
78b7e4b65fSAl Viro 	int res;
7958291a74SMickaël Salaün 
80752ba56fSMickaël Salaün 	if (unlikely(actual_size > PAGE_SIZE))	/* silly large */
81752ba56fSMickaël Salaün 		return -E2BIG;
82752ba56fSMickaël Salaün 
8358291a74SMickaël Salaün 	if (actual_size <= expected_size)
8458291a74SMickaël Salaün 		return 0;
8558291a74SMickaël Salaün 
86b7e4b65fSAl Viro 	res = check_zeroed_user(addr, actual_size - expected_size);
87b7e4b65fSAl Viro 	if (res < 0)
88b7e4b65fSAl Viro 		return res;
89b7e4b65fSAl Viro 	return res ? 0 : -E2BIG;
9058291a74SMickaël Salaün }
9158291a74SMickaël Salaün 
92a3884572SJakub Kicinski const struct bpf_map_ops bpf_map_offload_ops = {
93a3884572SJakub Kicinski 	.map_alloc = bpf_map_offload_map_alloc,
94a3884572SJakub Kicinski 	.map_free = bpf_map_offload_map_free,
95e8d2bec0SDaniel Borkmann 	.map_check_btf = map_check_no_btf,
96a3884572SJakub Kicinski };
97a3884572SJakub Kicinski 
9899c55f7dSAlexei Starovoitov static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
9999c55f7dSAlexei Starovoitov {
1001110f3a9SJakub Kicinski 	const struct bpf_map_ops *ops;
1019ef09e35SMark Rutland 	u32 type = attr->map_type;
10299c55f7dSAlexei Starovoitov 	struct bpf_map *map;
1031110f3a9SJakub Kicinski 	int err;
10499c55f7dSAlexei Starovoitov 
1059ef09e35SMark Rutland 	if (type >= ARRAY_SIZE(bpf_map_types))
1061110f3a9SJakub Kicinski 		return ERR_PTR(-EINVAL);
1079ef09e35SMark Rutland 	type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
1089ef09e35SMark Rutland 	ops = bpf_map_types[type];
1091110f3a9SJakub Kicinski 	if (!ops)
11040077e0cSJohannes Berg 		return ERR_PTR(-EINVAL);
11140077e0cSJohannes Berg 
1121110f3a9SJakub Kicinski 	if (ops->map_alloc_check) {
1131110f3a9SJakub Kicinski 		err = ops->map_alloc_check(attr);
1141110f3a9SJakub Kicinski 		if (err)
1151110f3a9SJakub Kicinski 			return ERR_PTR(err);
1161110f3a9SJakub Kicinski 	}
117a3884572SJakub Kicinski 	if (attr->map_ifindex)
118a3884572SJakub Kicinski 		ops = &bpf_map_offload_ops;
1191110f3a9SJakub Kicinski 	map = ops->map_alloc(attr);
12099c55f7dSAlexei Starovoitov 	if (IS_ERR(map))
12199c55f7dSAlexei Starovoitov 		return map;
1221110f3a9SJakub Kicinski 	map->ops = ops;
1239ef09e35SMark Rutland 	map->map_type = type;
12499c55f7dSAlexei Starovoitov 	return map;
12599c55f7dSAlexei Starovoitov }
12699c55f7dSAlexei Starovoitov 
12715c14a3dSBrian Vazquez static u32 bpf_map_value_size(struct bpf_map *map)
12815c14a3dSBrian Vazquez {
12915c14a3dSBrian Vazquez 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
13015c14a3dSBrian Vazquez 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
13115c14a3dSBrian Vazquez 	    map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
13215c14a3dSBrian Vazquez 	    map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
13315c14a3dSBrian Vazquez 		return round_up(map->value_size, 8) * num_possible_cpus();
13415c14a3dSBrian Vazquez 	else if (IS_FD_MAP(map))
13515c14a3dSBrian Vazquez 		return sizeof(u32);
13615c14a3dSBrian Vazquez 	else
13715c14a3dSBrian Vazquez 		return  map->value_size;
13815c14a3dSBrian Vazquez }
13915c14a3dSBrian Vazquez 
14015c14a3dSBrian Vazquez static void maybe_wait_bpf_programs(struct bpf_map *map)
14115c14a3dSBrian Vazquez {
14215c14a3dSBrian Vazquez 	/* Wait for any running BPF programs to complete so that
14315c14a3dSBrian Vazquez 	 * userspace, when we return to it, knows that all programs
14415c14a3dSBrian Vazquez 	 * that could be running use the new map value.
14515c14a3dSBrian Vazquez 	 */
14615c14a3dSBrian Vazquez 	if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
14715c14a3dSBrian Vazquez 	    map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
14815c14a3dSBrian Vazquez 		synchronize_rcu();
14915c14a3dSBrian Vazquez }
15015c14a3dSBrian Vazquez 
15115c14a3dSBrian Vazquez static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
15215c14a3dSBrian Vazquez 				void *value, __u64 flags)
15315c14a3dSBrian Vazquez {
15415c14a3dSBrian Vazquez 	int err;
15515c14a3dSBrian Vazquez 
15615c14a3dSBrian Vazquez 	/* Need to create a kthread, thus must support schedule */
15715c14a3dSBrian Vazquez 	if (bpf_map_is_dev_bound(map)) {
15815c14a3dSBrian Vazquez 		return bpf_map_offload_update_elem(map, key, value, flags);
15915c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
16015c14a3dSBrian Vazquez 		   map->map_type == BPF_MAP_TYPE_SOCKHASH ||
16115c14a3dSBrian Vazquez 		   map->map_type == BPF_MAP_TYPE_SOCKMAP ||
16215c14a3dSBrian Vazquez 		   map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
16315c14a3dSBrian Vazquez 		return map->ops->map_update_elem(map, key, value, flags);
16415c14a3dSBrian Vazquez 	} else if (IS_FD_PROG_ARRAY(map)) {
16515c14a3dSBrian Vazquez 		return bpf_fd_array_map_update_elem(map, f.file, key, value,
16615c14a3dSBrian Vazquez 						    flags);
16715c14a3dSBrian Vazquez 	}
16815c14a3dSBrian Vazquez 
169b6e5dae1SThomas Gleixner 	bpf_disable_instrumentation();
17015c14a3dSBrian Vazquez 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
17115c14a3dSBrian Vazquez 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
17215c14a3dSBrian Vazquez 		err = bpf_percpu_hash_update(map, key, value, flags);
17315c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
17415c14a3dSBrian Vazquez 		err = bpf_percpu_array_update(map, key, value, flags);
17515c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
17615c14a3dSBrian Vazquez 		err = bpf_percpu_cgroup_storage_update(map, key, value,
17715c14a3dSBrian Vazquez 						       flags);
17815c14a3dSBrian Vazquez 	} else if (IS_FD_ARRAY(map)) {
17915c14a3dSBrian Vazquez 		rcu_read_lock();
18015c14a3dSBrian Vazquez 		err = bpf_fd_array_map_update_elem(map, f.file, key, value,
18115c14a3dSBrian Vazquez 						   flags);
18215c14a3dSBrian Vazquez 		rcu_read_unlock();
18315c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
18415c14a3dSBrian Vazquez 		rcu_read_lock();
18515c14a3dSBrian Vazquez 		err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
18615c14a3dSBrian Vazquez 						  flags);
18715c14a3dSBrian Vazquez 		rcu_read_unlock();
18815c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
18915c14a3dSBrian Vazquez 		/* rcu_read_lock() is not needed */
19015c14a3dSBrian Vazquez 		err = bpf_fd_reuseport_array_update_elem(map, key, value,
19115c14a3dSBrian Vazquez 							 flags);
19215c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
19315c14a3dSBrian Vazquez 		   map->map_type == BPF_MAP_TYPE_STACK) {
19415c14a3dSBrian Vazquez 		err = map->ops->map_push_elem(map, value, flags);
19515c14a3dSBrian Vazquez 	} else {
19615c14a3dSBrian Vazquez 		rcu_read_lock();
19715c14a3dSBrian Vazquez 		err = map->ops->map_update_elem(map, key, value, flags);
19815c14a3dSBrian Vazquez 		rcu_read_unlock();
19915c14a3dSBrian Vazquez 	}
200b6e5dae1SThomas Gleixner 	bpf_enable_instrumentation();
20115c14a3dSBrian Vazquez 	maybe_wait_bpf_programs(map);
20215c14a3dSBrian Vazquez 
20315c14a3dSBrian Vazquez 	return err;
20415c14a3dSBrian Vazquez }
20515c14a3dSBrian Vazquez 
20615c14a3dSBrian Vazquez static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
20715c14a3dSBrian Vazquez 			      __u64 flags)
20815c14a3dSBrian Vazquez {
20915c14a3dSBrian Vazquez 	void *ptr;
21015c14a3dSBrian Vazquez 	int err;
21115c14a3dSBrian Vazquez 
212cb4d03abSBrian Vazquez 	if (bpf_map_is_dev_bound(map))
213cb4d03abSBrian Vazquez 		return bpf_map_offload_lookup_elem(map, key, value);
21415c14a3dSBrian Vazquez 
215b6e5dae1SThomas Gleixner 	bpf_disable_instrumentation();
21615c14a3dSBrian Vazquez 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
21715c14a3dSBrian Vazquez 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
21815c14a3dSBrian Vazquez 		err = bpf_percpu_hash_copy(map, key, value);
21915c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
22015c14a3dSBrian Vazquez 		err = bpf_percpu_array_copy(map, key, value);
22115c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
22215c14a3dSBrian Vazquez 		err = bpf_percpu_cgroup_storage_copy(map, key, value);
22315c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
22415c14a3dSBrian Vazquez 		err = bpf_stackmap_copy(map, key, value);
22515c14a3dSBrian Vazquez 	} else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
22615c14a3dSBrian Vazquez 		err = bpf_fd_array_map_lookup_elem(map, key, value);
22715c14a3dSBrian Vazquez 	} else if (IS_FD_HASH(map)) {
22815c14a3dSBrian Vazquez 		err = bpf_fd_htab_map_lookup_elem(map, key, value);
22915c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
23015c14a3dSBrian Vazquez 		err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
23115c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
23215c14a3dSBrian Vazquez 		   map->map_type == BPF_MAP_TYPE_STACK) {
23315c14a3dSBrian Vazquez 		err = map->ops->map_peek_elem(map, value);
23415c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
23515c14a3dSBrian Vazquez 		/* struct_ops map requires directly updating "value" */
23615c14a3dSBrian Vazquez 		err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
23715c14a3dSBrian Vazquez 	} else {
23815c14a3dSBrian Vazquez 		rcu_read_lock();
23915c14a3dSBrian Vazquez 		if (map->ops->map_lookup_elem_sys_only)
24015c14a3dSBrian Vazquez 			ptr = map->ops->map_lookup_elem_sys_only(map, key);
24115c14a3dSBrian Vazquez 		else
24215c14a3dSBrian Vazquez 			ptr = map->ops->map_lookup_elem(map, key);
24315c14a3dSBrian Vazquez 		if (IS_ERR(ptr)) {
24415c14a3dSBrian Vazquez 			err = PTR_ERR(ptr);
24515c14a3dSBrian Vazquez 		} else if (!ptr) {
24615c14a3dSBrian Vazquez 			err = -ENOENT;
24715c14a3dSBrian Vazquez 		} else {
24815c14a3dSBrian Vazquez 			err = 0;
24915c14a3dSBrian Vazquez 			if (flags & BPF_F_LOCK)
25015c14a3dSBrian Vazquez 				/* lock 'ptr' and copy everything but lock */
25115c14a3dSBrian Vazquez 				copy_map_value_locked(map, value, ptr, true);
25215c14a3dSBrian Vazquez 			else
25315c14a3dSBrian Vazquez 				copy_map_value(map, value, ptr);
25415c14a3dSBrian Vazquez 			/* mask lock, since value wasn't zero inited */
25515c14a3dSBrian Vazquez 			check_and_init_map_lock(map, value);
25615c14a3dSBrian Vazquez 		}
25715c14a3dSBrian Vazquez 		rcu_read_unlock();
25815c14a3dSBrian Vazquez 	}
25915c14a3dSBrian Vazquez 
260b6e5dae1SThomas Gleixner 	bpf_enable_instrumentation();
26115c14a3dSBrian Vazquez 	maybe_wait_bpf_programs(map);
26215c14a3dSBrian Vazquez 
26315c14a3dSBrian Vazquez 	return err;
26415c14a3dSBrian Vazquez }
26515c14a3dSBrian Vazquez 
266196e8ca7SDaniel Borkmann static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
267d407bd25SDaniel Borkmann {
268f01a7dbeSMartynas Pumputis 	/* We really just want to fail instead of triggering OOM killer
269f01a7dbeSMartynas Pumputis 	 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
270f01a7dbeSMartynas Pumputis 	 * which is used for lower order allocation requests.
271f01a7dbeSMartynas Pumputis 	 *
272f01a7dbeSMartynas Pumputis 	 * It has been observed that higher order allocation requests done by
273f01a7dbeSMartynas Pumputis 	 * vmalloc with __GFP_NORETRY being set might fail due to not trying
274f01a7dbeSMartynas Pumputis 	 * to reclaim memory from the page cache, thus we set
275f01a7dbeSMartynas Pumputis 	 * __GFP_RETRY_MAYFAIL to avoid such situations.
276d407bd25SDaniel Borkmann 	 */
277f01a7dbeSMartynas Pumputis 
278041de93fSChristoph Hellwig 	const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO;
279041de93fSChristoph Hellwig 	unsigned int flags = 0;
280041de93fSChristoph Hellwig 	unsigned long align = 1;
281d407bd25SDaniel Borkmann 	void *area;
282d407bd25SDaniel Borkmann 
283196e8ca7SDaniel Borkmann 	if (size >= SIZE_MAX)
284196e8ca7SDaniel Borkmann 		return NULL;
285196e8ca7SDaniel Borkmann 
286fc970227SAndrii Nakryiko 	/* kmalloc()'ed memory can't be mmap()'ed */
287041de93fSChristoph Hellwig 	if (mmapable) {
288041de93fSChristoph Hellwig 		BUG_ON(!PAGE_ALIGNED(size));
289041de93fSChristoph Hellwig 		align = SHMLBA;
290041de93fSChristoph Hellwig 		flags = VM_USERMAP;
291041de93fSChristoph Hellwig 	} else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
292041de93fSChristoph Hellwig 		area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
293f01a7dbeSMartynas Pumputis 				    numa_node);
294d407bd25SDaniel Borkmann 		if (area != NULL)
295d407bd25SDaniel Borkmann 			return area;
296d407bd25SDaniel Borkmann 	}
297041de93fSChristoph Hellwig 
298041de93fSChristoph Hellwig 	return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
299041de93fSChristoph Hellwig 			gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
300041de93fSChristoph Hellwig 			flags, numa_node, __builtin_return_address(0));
301d407bd25SDaniel Borkmann }
302d407bd25SDaniel Borkmann 
303196e8ca7SDaniel Borkmann void *bpf_map_area_alloc(u64 size, int numa_node)
304fc970227SAndrii Nakryiko {
305fc970227SAndrii Nakryiko 	return __bpf_map_area_alloc(size, numa_node, false);
306fc970227SAndrii Nakryiko }
307fc970227SAndrii Nakryiko 
308196e8ca7SDaniel Borkmann void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
309fc970227SAndrii Nakryiko {
310fc970227SAndrii Nakryiko 	return __bpf_map_area_alloc(size, numa_node, true);
311fc970227SAndrii Nakryiko }
312fc970227SAndrii Nakryiko 
313d407bd25SDaniel Borkmann void bpf_map_area_free(void *area)
314d407bd25SDaniel Borkmann {
315d407bd25SDaniel Borkmann 	kvfree(area);
316d407bd25SDaniel Borkmann }
317d407bd25SDaniel Borkmann 
318be70bcd5SDaniel Borkmann static u32 bpf_map_flags_retain_permanent(u32 flags)
319be70bcd5SDaniel Borkmann {
320be70bcd5SDaniel Borkmann 	/* Some map creation flags are not tied to the map object but
321be70bcd5SDaniel Borkmann 	 * rather to the map fd instead, so they have no meaning upon
322be70bcd5SDaniel Borkmann 	 * map object inspection since multiple file descriptors with
323be70bcd5SDaniel Borkmann 	 * different (access) properties can exist here. Thus, given
324be70bcd5SDaniel Borkmann 	 * this has zero meaning for the map itself, lets clear these
325be70bcd5SDaniel Borkmann 	 * from here.
326be70bcd5SDaniel Borkmann 	 */
327be70bcd5SDaniel Borkmann 	return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
328be70bcd5SDaniel Borkmann }
329be70bcd5SDaniel Borkmann 
330bd475643SJakub Kicinski void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
331bd475643SJakub Kicinski {
332bd475643SJakub Kicinski 	map->map_type = attr->map_type;
333bd475643SJakub Kicinski 	map->key_size = attr->key_size;
334bd475643SJakub Kicinski 	map->value_size = attr->value_size;
335bd475643SJakub Kicinski 	map->max_entries = attr->max_entries;
336be70bcd5SDaniel Borkmann 	map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
337bd475643SJakub Kicinski 	map->numa_node = bpf_map_attr_numa_node(attr);
338bd475643SJakub Kicinski }
339bd475643SJakub Kicinski 
3400a4c58f5SRoman Gushchin static int bpf_charge_memlock(struct user_struct *user, u32 pages)
341aaac3ba9SAlexei Starovoitov {
3420a4c58f5SRoman Gushchin 	unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
343aaac3ba9SAlexei Starovoitov 
3440a4c58f5SRoman Gushchin 	if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
3450a4c58f5SRoman Gushchin 		atomic_long_sub(pages, &user->locked_vm);
346aaac3ba9SAlexei Starovoitov 		return -EPERM;
347aaac3ba9SAlexei Starovoitov 	}
348aaac3ba9SAlexei Starovoitov 	return 0;
349aaac3ba9SAlexei Starovoitov }
350aaac3ba9SAlexei Starovoitov 
3510a4c58f5SRoman Gushchin static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
3520a4c58f5SRoman Gushchin {
353b936ca64SRoman Gushchin 	if (user)
3540a4c58f5SRoman Gushchin 		atomic_long_sub(pages, &user->locked_vm);
3550a4c58f5SRoman Gushchin }
3560a4c58f5SRoman Gushchin 
357196e8ca7SDaniel Borkmann int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
3580a4c58f5SRoman Gushchin {
359c85d6913SRoman Gushchin 	u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
360c85d6913SRoman Gushchin 	struct user_struct *user;
3610a4c58f5SRoman Gushchin 	int ret;
3620a4c58f5SRoman Gushchin 
363c85d6913SRoman Gushchin 	if (size >= U32_MAX - PAGE_SIZE)
364c85d6913SRoman Gushchin 		return -E2BIG;
365c85d6913SRoman Gushchin 
366c85d6913SRoman Gushchin 	user = get_current_user();
367b936ca64SRoman Gushchin 	ret = bpf_charge_memlock(user, pages);
3680a4c58f5SRoman Gushchin 	if (ret) {
3690a4c58f5SRoman Gushchin 		free_uid(user);
3700a4c58f5SRoman Gushchin 		return ret;
3710a4c58f5SRoman Gushchin 	}
372b936ca64SRoman Gushchin 
373b936ca64SRoman Gushchin 	mem->pages = pages;
374b936ca64SRoman Gushchin 	mem->user = user;
375b936ca64SRoman Gushchin 
376b936ca64SRoman Gushchin 	return 0;
3770a4c58f5SRoman Gushchin }
3780a4c58f5SRoman Gushchin 
379b936ca64SRoman Gushchin void bpf_map_charge_finish(struct bpf_map_memory *mem)
380aaac3ba9SAlexei Starovoitov {
381b936ca64SRoman Gushchin 	bpf_uncharge_memlock(mem->user, mem->pages);
382b936ca64SRoman Gushchin 	free_uid(mem->user);
383b936ca64SRoman Gushchin }
3843539b96eSRoman Gushchin 
385b936ca64SRoman Gushchin void bpf_map_charge_move(struct bpf_map_memory *dst,
386b936ca64SRoman Gushchin 			 struct bpf_map_memory *src)
387b936ca64SRoman Gushchin {
388b936ca64SRoman Gushchin 	*dst = *src;
389b936ca64SRoman Gushchin 
390b936ca64SRoman Gushchin 	/* Make sure src will not be used for the redundant uncharging. */
391b936ca64SRoman Gushchin 	memset(src, 0, sizeof(struct bpf_map_memory));
392aaac3ba9SAlexei Starovoitov }
393aaac3ba9SAlexei Starovoitov 
3940a4c58f5SRoman Gushchin int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
3950a4c58f5SRoman Gushchin {
3960a4c58f5SRoman Gushchin 	int ret;
3970a4c58f5SRoman Gushchin 
3983539b96eSRoman Gushchin 	ret = bpf_charge_memlock(map->memory.user, pages);
3990a4c58f5SRoman Gushchin 	if (ret)
4000a4c58f5SRoman Gushchin 		return ret;
4013539b96eSRoman Gushchin 	map->memory.pages += pages;
4020a4c58f5SRoman Gushchin 	return ret;
4030a4c58f5SRoman Gushchin }
4040a4c58f5SRoman Gushchin 
4050a4c58f5SRoman Gushchin void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
4060a4c58f5SRoman Gushchin {
4073539b96eSRoman Gushchin 	bpf_uncharge_memlock(map->memory.user, pages);
4083539b96eSRoman Gushchin 	map->memory.pages -= pages;
4090a4c58f5SRoman Gushchin }
4100a4c58f5SRoman Gushchin 
411f3f1c054SMartin KaFai Lau static int bpf_map_alloc_id(struct bpf_map *map)
412f3f1c054SMartin KaFai Lau {
413f3f1c054SMartin KaFai Lau 	int id;
414f3f1c054SMartin KaFai Lau 
415b76354cdSShaohua Li 	idr_preload(GFP_KERNEL);
416f3f1c054SMartin KaFai Lau 	spin_lock_bh(&map_idr_lock);
417f3f1c054SMartin KaFai Lau 	id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
418f3f1c054SMartin KaFai Lau 	if (id > 0)
419f3f1c054SMartin KaFai Lau 		map->id = id;
420f3f1c054SMartin KaFai Lau 	spin_unlock_bh(&map_idr_lock);
421b76354cdSShaohua Li 	idr_preload_end();
422f3f1c054SMartin KaFai Lau 
423f3f1c054SMartin KaFai Lau 	if (WARN_ON_ONCE(!id))
424f3f1c054SMartin KaFai Lau 		return -ENOSPC;
425f3f1c054SMartin KaFai Lau 
426f3f1c054SMartin KaFai Lau 	return id > 0 ? 0 : id;
427f3f1c054SMartin KaFai Lau }
428f3f1c054SMartin KaFai Lau 
429a3884572SJakub Kicinski void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
430f3f1c054SMartin KaFai Lau {
431930651a7SEric Dumazet 	unsigned long flags;
432930651a7SEric Dumazet 
433a3884572SJakub Kicinski 	/* Offloaded maps are removed from the IDR store when their device
434a3884572SJakub Kicinski 	 * disappears - even if someone holds an fd to them they are unusable,
435a3884572SJakub Kicinski 	 * the memory is gone, all ops will fail; they are simply waiting for
436a3884572SJakub Kicinski 	 * refcnt to drop to be freed.
437a3884572SJakub Kicinski 	 */
438a3884572SJakub Kicinski 	if (!map->id)
439a3884572SJakub Kicinski 		return;
440a3884572SJakub Kicinski 
441bd5f5f4eSMartin KaFai Lau 	if (do_idr_lock)
442930651a7SEric Dumazet 		spin_lock_irqsave(&map_idr_lock, flags);
443bd5f5f4eSMartin KaFai Lau 	else
444bd5f5f4eSMartin KaFai Lau 		__acquire(&map_idr_lock);
445bd5f5f4eSMartin KaFai Lau 
446f3f1c054SMartin KaFai Lau 	idr_remove(&map_idr, map->id);
447a3884572SJakub Kicinski 	map->id = 0;
448bd5f5f4eSMartin KaFai Lau 
449bd5f5f4eSMartin KaFai Lau 	if (do_idr_lock)
450930651a7SEric Dumazet 		spin_unlock_irqrestore(&map_idr_lock, flags);
451bd5f5f4eSMartin KaFai Lau 	else
452bd5f5f4eSMartin KaFai Lau 		__release(&map_idr_lock);
453f3f1c054SMartin KaFai Lau }
454f3f1c054SMartin KaFai Lau 
45599c55f7dSAlexei Starovoitov /* called from workqueue */
45699c55f7dSAlexei Starovoitov static void bpf_map_free_deferred(struct work_struct *work)
45799c55f7dSAlexei Starovoitov {
45899c55f7dSAlexei Starovoitov 	struct bpf_map *map = container_of(work, struct bpf_map, work);
459b936ca64SRoman Gushchin 	struct bpf_map_memory mem;
46099c55f7dSAlexei Starovoitov 
461b936ca64SRoman Gushchin 	bpf_map_charge_move(&mem, &map->memory);
462afdb09c7SChenbo Feng 	security_bpf_map_free(map);
46399c55f7dSAlexei Starovoitov 	/* implementation dependent freeing */
46499c55f7dSAlexei Starovoitov 	map->ops->map_free(map);
465b936ca64SRoman Gushchin 	bpf_map_charge_finish(&mem);
46699c55f7dSAlexei Starovoitov }
46799c55f7dSAlexei Starovoitov 
468c9da161cSDaniel Borkmann static void bpf_map_put_uref(struct bpf_map *map)
469c9da161cSDaniel Borkmann {
4701e0bd5a0SAndrii Nakryiko 	if (atomic64_dec_and_test(&map->usercnt)) {
471ba6b8de4SJohn Fastabend 		if (map->ops->map_release_uref)
472ba6b8de4SJohn Fastabend 			map->ops->map_release_uref(map);
473c9da161cSDaniel Borkmann 	}
474c9da161cSDaniel Borkmann }
475c9da161cSDaniel Borkmann 
47699c55f7dSAlexei Starovoitov /* decrement map refcnt and schedule it for freeing via workqueue
47799c55f7dSAlexei Starovoitov  * (unrelying map implementation ops->map_free() might sleep)
47899c55f7dSAlexei Starovoitov  */
479bd5f5f4eSMartin KaFai Lau static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
48099c55f7dSAlexei Starovoitov {
4811e0bd5a0SAndrii Nakryiko 	if (atomic64_dec_and_test(&map->refcnt)) {
48234ad5580SMartin KaFai Lau 		/* bpf_map_free_id() must be called first */
483bd5f5f4eSMartin KaFai Lau 		bpf_map_free_id(map, do_idr_lock);
48478958fcaSMartin KaFai Lau 		btf_put(map->btf);
48599c55f7dSAlexei Starovoitov 		INIT_WORK(&map->work, bpf_map_free_deferred);
48699c55f7dSAlexei Starovoitov 		schedule_work(&map->work);
48799c55f7dSAlexei Starovoitov 	}
48899c55f7dSAlexei Starovoitov }
48999c55f7dSAlexei Starovoitov 
490bd5f5f4eSMartin KaFai Lau void bpf_map_put(struct bpf_map *map)
491bd5f5f4eSMartin KaFai Lau {
492bd5f5f4eSMartin KaFai Lau 	__bpf_map_put(map, true);
493bd5f5f4eSMartin KaFai Lau }
494630a4d38SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_map_put);
495bd5f5f4eSMartin KaFai Lau 
496c9da161cSDaniel Borkmann void bpf_map_put_with_uref(struct bpf_map *map)
497c9da161cSDaniel Borkmann {
498c9da161cSDaniel Borkmann 	bpf_map_put_uref(map);
499c9da161cSDaniel Borkmann 	bpf_map_put(map);
500c9da161cSDaniel Borkmann }
501c9da161cSDaniel Borkmann 
50299c55f7dSAlexei Starovoitov static int bpf_map_release(struct inode *inode, struct file *filp)
50399c55f7dSAlexei Starovoitov {
50461d1b6a4SDaniel Borkmann 	struct bpf_map *map = filp->private_data;
50561d1b6a4SDaniel Borkmann 
50661d1b6a4SDaniel Borkmann 	if (map->ops->map_release)
50761d1b6a4SDaniel Borkmann 		map->ops->map_release(map, filp);
50861d1b6a4SDaniel Borkmann 
50961d1b6a4SDaniel Borkmann 	bpf_map_put_with_uref(map);
51099c55f7dSAlexei Starovoitov 	return 0;
51199c55f7dSAlexei Starovoitov }
51299c55f7dSAlexei Starovoitov 
51387df15deSDaniel Borkmann static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
51487df15deSDaniel Borkmann {
51587df15deSDaniel Borkmann 	fmode_t mode = f.file->f_mode;
51687df15deSDaniel Borkmann 
51787df15deSDaniel Borkmann 	/* Our file permissions may have been overridden by global
51887df15deSDaniel Borkmann 	 * map permissions facing syscall side.
51987df15deSDaniel Borkmann 	 */
52087df15deSDaniel Borkmann 	if (READ_ONCE(map->frozen))
52187df15deSDaniel Borkmann 		mode &= ~FMODE_CAN_WRITE;
52287df15deSDaniel Borkmann 	return mode;
52387df15deSDaniel Borkmann }
52487df15deSDaniel Borkmann 
525f99bf205SDaniel Borkmann #ifdef CONFIG_PROC_FS
526f99bf205SDaniel Borkmann static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
527f99bf205SDaniel Borkmann {
528f99bf205SDaniel Borkmann 	const struct bpf_map *map = filp->private_data;
52921116b70SDaniel Borkmann 	const struct bpf_array *array;
5302beee5f5SDaniel Borkmann 	u32 type = 0, jited = 0;
53121116b70SDaniel Borkmann 
53221116b70SDaniel Borkmann 	if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
53321116b70SDaniel Borkmann 		array = container_of(map, struct bpf_array, map);
5342beee5f5SDaniel Borkmann 		type  = array->aux->type;
5352beee5f5SDaniel Borkmann 		jited = array->aux->jited;
53621116b70SDaniel Borkmann 	}
537f99bf205SDaniel Borkmann 
538f99bf205SDaniel Borkmann 	seq_printf(m,
539f99bf205SDaniel Borkmann 		   "map_type:\t%u\n"
540f99bf205SDaniel Borkmann 		   "key_size:\t%u\n"
541f99bf205SDaniel Borkmann 		   "value_size:\t%u\n"
542322cea2fSDaniel Borkmann 		   "max_entries:\t%u\n"
54321116b70SDaniel Borkmann 		   "map_flags:\t%#x\n"
5444316b409SDaniel Borkmann 		   "memlock:\t%llu\n"
54587df15deSDaniel Borkmann 		   "map_id:\t%u\n"
54687df15deSDaniel Borkmann 		   "frozen:\t%u\n",
547f99bf205SDaniel Borkmann 		   map->map_type,
548f99bf205SDaniel Borkmann 		   map->key_size,
549f99bf205SDaniel Borkmann 		   map->value_size,
550322cea2fSDaniel Borkmann 		   map->max_entries,
55121116b70SDaniel Borkmann 		   map->map_flags,
5523539b96eSRoman Gushchin 		   map->memory.pages * 1ULL << PAGE_SHIFT,
55387df15deSDaniel Borkmann 		   map->id,
55487df15deSDaniel Borkmann 		   READ_ONCE(map->frozen));
5552beee5f5SDaniel Borkmann 	if (type) {
5562beee5f5SDaniel Borkmann 		seq_printf(m, "owner_prog_type:\t%u\n", type);
5572beee5f5SDaniel Borkmann 		seq_printf(m, "owner_jited:\t%u\n", jited);
5589780c0abSDaniel Borkmann 	}
559f99bf205SDaniel Borkmann }
560f99bf205SDaniel Borkmann #endif
561f99bf205SDaniel Borkmann 
5626e71b04aSChenbo Feng static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
5636e71b04aSChenbo Feng 			      loff_t *ppos)
5646e71b04aSChenbo Feng {
5656e71b04aSChenbo Feng 	/* We need this handler such that alloc_file() enables
5666e71b04aSChenbo Feng 	 * f_mode with FMODE_CAN_READ.
5676e71b04aSChenbo Feng 	 */
5686e71b04aSChenbo Feng 	return -EINVAL;
5696e71b04aSChenbo Feng }
5706e71b04aSChenbo Feng 
5716e71b04aSChenbo Feng static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
5726e71b04aSChenbo Feng 			       size_t siz, loff_t *ppos)
5736e71b04aSChenbo Feng {
5746e71b04aSChenbo Feng 	/* We need this handler such that alloc_file() enables
5756e71b04aSChenbo Feng 	 * f_mode with FMODE_CAN_WRITE.
5766e71b04aSChenbo Feng 	 */
5776e71b04aSChenbo Feng 	return -EINVAL;
5786e71b04aSChenbo Feng }
5796e71b04aSChenbo Feng 
580fc970227SAndrii Nakryiko /* called for any extra memory-mapped regions (except initial) */
581fc970227SAndrii Nakryiko static void bpf_map_mmap_open(struct vm_area_struct *vma)
582fc970227SAndrii Nakryiko {
583fc970227SAndrii Nakryiko 	struct bpf_map *map = vma->vm_file->private_data;
584fc970227SAndrii Nakryiko 
5851f6cb19bSAndrii Nakryiko 	if (vma->vm_flags & VM_MAYWRITE) {
586fc970227SAndrii Nakryiko 		mutex_lock(&map->freeze_mutex);
587fc970227SAndrii Nakryiko 		map->writecnt++;
588fc970227SAndrii Nakryiko 		mutex_unlock(&map->freeze_mutex);
589fc970227SAndrii Nakryiko 	}
590fc970227SAndrii Nakryiko }
591fc970227SAndrii Nakryiko 
592fc970227SAndrii Nakryiko /* called for all unmapped memory region (including initial) */
593fc970227SAndrii Nakryiko static void bpf_map_mmap_close(struct vm_area_struct *vma)
594fc970227SAndrii Nakryiko {
595fc970227SAndrii Nakryiko 	struct bpf_map *map = vma->vm_file->private_data;
596fc970227SAndrii Nakryiko 
5971f6cb19bSAndrii Nakryiko 	if (vma->vm_flags & VM_MAYWRITE) {
598fc970227SAndrii Nakryiko 		mutex_lock(&map->freeze_mutex);
599fc970227SAndrii Nakryiko 		map->writecnt--;
600fc970227SAndrii Nakryiko 		mutex_unlock(&map->freeze_mutex);
601fc970227SAndrii Nakryiko 	}
602fc970227SAndrii Nakryiko }
603fc970227SAndrii Nakryiko 
604fc970227SAndrii Nakryiko static const struct vm_operations_struct bpf_map_default_vmops = {
605fc970227SAndrii Nakryiko 	.open		= bpf_map_mmap_open,
606fc970227SAndrii Nakryiko 	.close		= bpf_map_mmap_close,
607fc970227SAndrii Nakryiko };
608fc970227SAndrii Nakryiko 
609fc970227SAndrii Nakryiko static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
610fc970227SAndrii Nakryiko {
611fc970227SAndrii Nakryiko 	struct bpf_map *map = filp->private_data;
612fc970227SAndrii Nakryiko 	int err;
613fc970227SAndrii Nakryiko 
614fc970227SAndrii Nakryiko 	if (!map->ops->map_mmap || map_value_has_spin_lock(map))
615fc970227SAndrii Nakryiko 		return -ENOTSUPP;
616fc970227SAndrii Nakryiko 
617fc970227SAndrii Nakryiko 	if (!(vma->vm_flags & VM_SHARED))
618fc970227SAndrii Nakryiko 		return -EINVAL;
619fc970227SAndrii Nakryiko 
620fc970227SAndrii Nakryiko 	mutex_lock(&map->freeze_mutex);
621fc970227SAndrii Nakryiko 
622dfeb376dSAndrii Nakryiko 	if (vma->vm_flags & VM_WRITE) {
623dfeb376dSAndrii Nakryiko 		if (map->frozen) {
624fc970227SAndrii Nakryiko 			err = -EPERM;
625fc970227SAndrii Nakryiko 			goto out;
626fc970227SAndrii Nakryiko 		}
627dfeb376dSAndrii Nakryiko 		/* map is meant to be read-only, so do not allow mapping as
628dfeb376dSAndrii Nakryiko 		 * writable, because it's possible to leak a writable page
629dfeb376dSAndrii Nakryiko 		 * reference and allows user-space to still modify it after
630dfeb376dSAndrii Nakryiko 		 * freezing, while verifier will assume contents do not change
631dfeb376dSAndrii Nakryiko 		 */
632dfeb376dSAndrii Nakryiko 		if (map->map_flags & BPF_F_RDONLY_PROG) {
633dfeb376dSAndrii Nakryiko 			err = -EACCES;
634dfeb376dSAndrii Nakryiko 			goto out;
635dfeb376dSAndrii Nakryiko 		}
636dfeb376dSAndrii Nakryiko 	}
637fc970227SAndrii Nakryiko 
638fc970227SAndrii Nakryiko 	/* set default open/close callbacks */
639fc970227SAndrii Nakryiko 	vma->vm_ops = &bpf_map_default_vmops;
640fc970227SAndrii Nakryiko 	vma->vm_private_data = map;
6411f6cb19bSAndrii Nakryiko 	vma->vm_flags &= ~VM_MAYEXEC;
6421f6cb19bSAndrii Nakryiko 	if (!(vma->vm_flags & VM_WRITE))
6431f6cb19bSAndrii Nakryiko 		/* disallow re-mapping with PROT_WRITE */
6441f6cb19bSAndrii Nakryiko 		vma->vm_flags &= ~VM_MAYWRITE;
645fc970227SAndrii Nakryiko 
646fc970227SAndrii Nakryiko 	err = map->ops->map_mmap(map, vma);
647fc970227SAndrii Nakryiko 	if (err)
648fc970227SAndrii Nakryiko 		goto out;
649fc970227SAndrii Nakryiko 
6501f6cb19bSAndrii Nakryiko 	if (vma->vm_flags & VM_MAYWRITE)
651fc970227SAndrii Nakryiko 		map->writecnt++;
652fc970227SAndrii Nakryiko out:
653fc970227SAndrii Nakryiko 	mutex_unlock(&map->freeze_mutex);
654fc970227SAndrii Nakryiko 	return err;
655fc970227SAndrii Nakryiko }
656fc970227SAndrii Nakryiko 
657457f4436SAndrii Nakryiko static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
658457f4436SAndrii Nakryiko {
659457f4436SAndrii Nakryiko 	struct bpf_map *map = filp->private_data;
660457f4436SAndrii Nakryiko 
661457f4436SAndrii Nakryiko 	if (map->ops->map_poll)
662457f4436SAndrii Nakryiko 		return map->ops->map_poll(map, filp, pts);
663457f4436SAndrii Nakryiko 
664457f4436SAndrii Nakryiko 	return EPOLLERR;
665457f4436SAndrii Nakryiko }
666457f4436SAndrii Nakryiko 
667f66e448cSChenbo Feng const struct file_operations bpf_map_fops = {
668f99bf205SDaniel Borkmann #ifdef CONFIG_PROC_FS
669f99bf205SDaniel Borkmann 	.show_fdinfo	= bpf_map_show_fdinfo,
670f99bf205SDaniel Borkmann #endif
67199c55f7dSAlexei Starovoitov 	.release	= bpf_map_release,
6726e71b04aSChenbo Feng 	.read		= bpf_dummy_read,
6736e71b04aSChenbo Feng 	.write		= bpf_dummy_write,
674fc970227SAndrii Nakryiko 	.mmap		= bpf_map_mmap,
675457f4436SAndrii Nakryiko 	.poll		= bpf_map_poll,
67699c55f7dSAlexei Starovoitov };
67799c55f7dSAlexei Starovoitov 
6786e71b04aSChenbo Feng int bpf_map_new_fd(struct bpf_map *map, int flags)
679aa79781bSDaniel Borkmann {
680afdb09c7SChenbo Feng 	int ret;
681afdb09c7SChenbo Feng 
682afdb09c7SChenbo Feng 	ret = security_bpf_map(map, OPEN_FMODE(flags));
683afdb09c7SChenbo Feng 	if (ret < 0)
684afdb09c7SChenbo Feng 		return ret;
685afdb09c7SChenbo Feng 
686aa79781bSDaniel Borkmann 	return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
6876e71b04aSChenbo Feng 				flags | O_CLOEXEC);
6886e71b04aSChenbo Feng }
6896e71b04aSChenbo Feng 
6906e71b04aSChenbo Feng int bpf_get_file_flag(int flags)
6916e71b04aSChenbo Feng {
6926e71b04aSChenbo Feng 	if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
6936e71b04aSChenbo Feng 		return -EINVAL;
6946e71b04aSChenbo Feng 	if (flags & BPF_F_RDONLY)
6956e71b04aSChenbo Feng 		return O_RDONLY;
6966e71b04aSChenbo Feng 	if (flags & BPF_F_WRONLY)
6976e71b04aSChenbo Feng 		return O_WRONLY;
6986e71b04aSChenbo Feng 	return O_RDWR;
699aa79781bSDaniel Borkmann }
700aa79781bSDaniel Borkmann 
70199c55f7dSAlexei Starovoitov /* helper macro to check that unused fields 'union bpf_attr' are zero */
70299c55f7dSAlexei Starovoitov #define CHECK_ATTR(CMD) \
70399c55f7dSAlexei Starovoitov 	memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
70499c55f7dSAlexei Starovoitov 		   sizeof(attr->CMD##_LAST_FIELD), 0, \
70599c55f7dSAlexei Starovoitov 		   sizeof(*attr) - \
70699c55f7dSAlexei Starovoitov 		   offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
70799c55f7dSAlexei Starovoitov 		   sizeof(attr->CMD##_LAST_FIELD)) != NULL
70899c55f7dSAlexei Starovoitov 
7098e7ae251SMartin KaFai Lau /* dst and src must have at least "size" number of bytes.
7108e7ae251SMartin KaFai Lau  * Return strlen on success and < 0 on error.
711cb4d2b3fSMartin KaFai Lau  */
7128e7ae251SMartin KaFai Lau int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
713cb4d2b3fSMartin KaFai Lau {
7148e7ae251SMartin KaFai Lau 	const char *end = src + size;
7158e7ae251SMartin KaFai Lau 	const char *orig_src = src;
716cb4d2b3fSMartin KaFai Lau 
7178e7ae251SMartin KaFai Lau 	memset(dst, 0, size);
7183e0ddc4fSDaniel Borkmann 	/* Copy all isalnum(), '_' and '.' chars. */
719cb4d2b3fSMartin KaFai Lau 	while (src < end && *src) {
7203e0ddc4fSDaniel Borkmann 		if (!isalnum(*src) &&
7213e0ddc4fSDaniel Borkmann 		    *src != '_' && *src != '.')
722cb4d2b3fSMartin KaFai Lau 			return -EINVAL;
723cb4d2b3fSMartin KaFai Lau 		*dst++ = *src++;
724cb4d2b3fSMartin KaFai Lau 	}
725cb4d2b3fSMartin KaFai Lau 
7268e7ae251SMartin KaFai Lau 	/* No '\0' found in "size" number of bytes */
727cb4d2b3fSMartin KaFai Lau 	if (src == end)
728cb4d2b3fSMartin KaFai Lau 		return -EINVAL;
729cb4d2b3fSMartin KaFai Lau 
7308e7ae251SMartin KaFai Lau 	return src - orig_src;
731cb4d2b3fSMartin KaFai Lau }
732cb4d2b3fSMartin KaFai Lau 
733e8d2bec0SDaniel Borkmann int map_check_no_btf(const struct bpf_map *map,
7341b2b234bSRoman Gushchin 		     const struct btf *btf,
735e8d2bec0SDaniel Borkmann 		     const struct btf_type *key_type,
736e8d2bec0SDaniel Borkmann 		     const struct btf_type *value_type)
737e8d2bec0SDaniel Borkmann {
738e8d2bec0SDaniel Borkmann 	return -ENOTSUPP;
739e8d2bec0SDaniel Borkmann }
740e8d2bec0SDaniel Borkmann 
741d83525caSAlexei Starovoitov static int map_check_btf(struct bpf_map *map, const struct btf *btf,
742e8d2bec0SDaniel Borkmann 			 u32 btf_key_id, u32 btf_value_id)
743e8d2bec0SDaniel Borkmann {
744e8d2bec0SDaniel Borkmann 	const struct btf_type *key_type, *value_type;
745e8d2bec0SDaniel Borkmann 	u32 key_size, value_size;
746e8d2bec0SDaniel Borkmann 	int ret = 0;
747e8d2bec0SDaniel Borkmann 
7482824ecb7SDaniel Borkmann 	/* Some maps allow key to be unspecified. */
7492824ecb7SDaniel Borkmann 	if (btf_key_id) {
750e8d2bec0SDaniel Borkmann 		key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
751e8d2bec0SDaniel Borkmann 		if (!key_type || key_size != map->key_size)
752e8d2bec0SDaniel Borkmann 			return -EINVAL;
7532824ecb7SDaniel Borkmann 	} else {
7542824ecb7SDaniel Borkmann 		key_type = btf_type_by_id(btf, 0);
7552824ecb7SDaniel Borkmann 		if (!map->ops->map_check_btf)
7562824ecb7SDaniel Borkmann 			return -EINVAL;
7572824ecb7SDaniel Borkmann 	}
758e8d2bec0SDaniel Borkmann 
759e8d2bec0SDaniel Borkmann 	value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
760e8d2bec0SDaniel Borkmann 	if (!value_type || value_size != map->value_size)
761e8d2bec0SDaniel Borkmann 		return -EINVAL;
762e8d2bec0SDaniel Borkmann 
763d83525caSAlexei Starovoitov 	map->spin_lock_off = btf_find_spin_lock(btf, value_type);
764d83525caSAlexei Starovoitov 
765d83525caSAlexei Starovoitov 	if (map_value_has_spin_lock(map)) {
766591fe988SDaniel Borkmann 		if (map->map_flags & BPF_F_RDONLY_PROG)
767591fe988SDaniel Borkmann 			return -EACCES;
768d83525caSAlexei Starovoitov 		if (map->map_type != BPF_MAP_TYPE_HASH &&
769e16d2f1aSAlexei Starovoitov 		    map->map_type != BPF_MAP_TYPE_ARRAY &&
7706ac99e8fSMartin KaFai Lau 		    map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
7716ac99e8fSMartin KaFai Lau 		    map->map_type != BPF_MAP_TYPE_SK_STORAGE)
772d83525caSAlexei Starovoitov 			return -ENOTSUPP;
773d83525caSAlexei Starovoitov 		if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
774d83525caSAlexei Starovoitov 		    map->value_size) {
775d83525caSAlexei Starovoitov 			WARN_ONCE(1,
776d83525caSAlexei Starovoitov 				  "verifier bug spin_lock_off %d value_size %d\n",
777d83525caSAlexei Starovoitov 				  map->spin_lock_off, map->value_size);
778d83525caSAlexei Starovoitov 			return -EFAULT;
779d83525caSAlexei Starovoitov 		}
780d83525caSAlexei Starovoitov 	}
781d83525caSAlexei Starovoitov 
782e8d2bec0SDaniel Borkmann 	if (map->ops->map_check_btf)
7831b2b234bSRoman Gushchin 		ret = map->ops->map_check_btf(map, btf, key_type, value_type);
784e8d2bec0SDaniel Borkmann 
785e8d2bec0SDaniel Borkmann 	return ret;
786e8d2bec0SDaniel Borkmann }
787e8d2bec0SDaniel Borkmann 
78885d33df3SMartin KaFai Lau #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
78999c55f7dSAlexei Starovoitov /* called via syscall */
79099c55f7dSAlexei Starovoitov static int map_create(union bpf_attr *attr)
79199c55f7dSAlexei Starovoitov {
79296eabe7aSMartin KaFai Lau 	int numa_node = bpf_map_attr_numa_node(attr);
793b936ca64SRoman Gushchin 	struct bpf_map_memory mem;
79499c55f7dSAlexei Starovoitov 	struct bpf_map *map;
7956e71b04aSChenbo Feng 	int f_flags;
79699c55f7dSAlexei Starovoitov 	int err;
79799c55f7dSAlexei Starovoitov 
79899c55f7dSAlexei Starovoitov 	err = CHECK_ATTR(BPF_MAP_CREATE);
79999c55f7dSAlexei Starovoitov 	if (err)
80099c55f7dSAlexei Starovoitov 		return -EINVAL;
80199c55f7dSAlexei Starovoitov 
80285d33df3SMartin KaFai Lau 	if (attr->btf_vmlinux_value_type_id) {
80385d33df3SMartin KaFai Lau 		if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
80485d33df3SMartin KaFai Lau 		    attr->btf_key_type_id || attr->btf_value_type_id)
80585d33df3SMartin KaFai Lau 			return -EINVAL;
80685d33df3SMartin KaFai Lau 	} else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
80785d33df3SMartin KaFai Lau 		return -EINVAL;
80885d33df3SMartin KaFai Lau 	}
80985d33df3SMartin KaFai Lau 
8106e71b04aSChenbo Feng 	f_flags = bpf_get_file_flag(attr->map_flags);
8116e71b04aSChenbo Feng 	if (f_flags < 0)
8126e71b04aSChenbo Feng 		return f_flags;
8136e71b04aSChenbo Feng 
81496eabe7aSMartin KaFai Lau 	if (numa_node != NUMA_NO_NODE &&
81596e5ae4eSEric Dumazet 	    ((unsigned int)numa_node >= nr_node_ids ||
81696e5ae4eSEric Dumazet 	     !node_online(numa_node)))
81796eabe7aSMartin KaFai Lau 		return -EINVAL;
81896eabe7aSMartin KaFai Lau 
81999c55f7dSAlexei Starovoitov 	/* find map type and init map: hashtable vs rbtree vs bloom vs ... */
82099c55f7dSAlexei Starovoitov 	map = find_and_alloc_map(attr);
82199c55f7dSAlexei Starovoitov 	if (IS_ERR(map))
82299c55f7dSAlexei Starovoitov 		return PTR_ERR(map);
82399c55f7dSAlexei Starovoitov 
8248e7ae251SMartin KaFai Lau 	err = bpf_obj_name_cpy(map->name, attr->map_name,
8258e7ae251SMartin KaFai Lau 			       sizeof(attr->map_name));
8268e7ae251SMartin KaFai Lau 	if (err < 0)
827b936ca64SRoman Gushchin 		goto free_map;
828ad5b177bSMartin KaFai Lau 
8291e0bd5a0SAndrii Nakryiko 	atomic64_set(&map->refcnt, 1);
8301e0bd5a0SAndrii Nakryiko 	atomic64_set(&map->usercnt, 1);
831fc970227SAndrii Nakryiko 	mutex_init(&map->freeze_mutex);
83299c55f7dSAlexei Starovoitov 
83385d33df3SMartin KaFai Lau 	map->spin_lock_off = -EINVAL;
83485d33df3SMartin KaFai Lau 	if (attr->btf_key_type_id || attr->btf_value_type_id ||
83585d33df3SMartin KaFai Lau 	    /* Even the map's value is a kernel's struct,
83685d33df3SMartin KaFai Lau 	     * the bpf_prog.o must have BTF to begin with
83785d33df3SMartin KaFai Lau 	     * to figure out the corresponding kernel's
83885d33df3SMartin KaFai Lau 	     * counter part.  Thus, attr->btf_fd has
83985d33df3SMartin KaFai Lau 	     * to be valid also.
84085d33df3SMartin KaFai Lau 	     */
84185d33df3SMartin KaFai Lau 	    attr->btf_vmlinux_value_type_id) {
842a26ca7c9SMartin KaFai Lau 		struct btf *btf;
843a26ca7c9SMartin KaFai Lau 
844a26ca7c9SMartin KaFai Lau 		btf = btf_get_by_fd(attr->btf_fd);
845a26ca7c9SMartin KaFai Lau 		if (IS_ERR(btf)) {
846a26ca7c9SMartin KaFai Lau 			err = PTR_ERR(btf);
847b936ca64SRoman Gushchin 			goto free_map;
848a26ca7c9SMartin KaFai Lau 		}
84985d33df3SMartin KaFai Lau 		map->btf = btf;
850a26ca7c9SMartin KaFai Lau 
85185d33df3SMartin KaFai Lau 		if (attr->btf_value_type_id) {
852e8d2bec0SDaniel Borkmann 			err = map_check_btf(map, btf, attr->btf_key_type_id,
8539b2cf328SMartin KaFai Lau 					    attr->btf_value_type_id);
85485d33df3SMartin KaFai Lau 			if (err)
855b936ca64SRoman Gushchin 				goto free_map;
856a26ca7c9SMartin KaFai Lau 		}
857a26ca7c9SMartin KaFai Lau 
8589b2cf328SMartin KaFai Lau 		map->btf_key_type_id = attr->btf_key_type_id;
8599b2cf328SMartin KaFai Lau 		map->btf_value_type_id = attr->btf_value_type_id;
86085d33df3SMartin KaFai Lau 		map->btf_vmlinux_value_type_id =
86185d33df3SMartin KaFai Lau 			attr->btf_vmlinux_value_type_id;
862a26ca7c9SMartin KaFai Lau 	}
863a26ca7c9SMartin KaFai Lau 
864afdb09c7SChenbo Feng 	err = security_bpf_map_alloc(map);
865aaac3ba9SAlexei Starovoitov 	if (err)
866b936ca64SRoman Gushchin 		goto free_map;
867afdb09c7SChenbo Feng 
868f3f1c054SMartin KaFai Lau 	err = bpf_map_alloc_id(map);
869f3f1c054SMartin KaFai Lau 	if (err)
870b936ca64SRoman Gushchin 		goto free_map_sec;
871f3f1c054SMartin KaFai Lau 
8726e71b04aSChenbo Feng 	err = bpf_map_new_fd(map, f_flags);
873bd5f5f4eSMartin KaFai Lau 	if (err < 0) {
874bd5f5f4eSMartin KaFai Lau 		/* failed to allocate fd.
875352d20d6SPeng Sun 		 * bpf_map_put_with_uref() is needed because the above
876bd5f5f4eSMartin KaFai Lau 		 * bpf_map_alloc_id() has published the map
877bd5f5f4eSMartin KaFai Lau 		 * to the userspace and the userspace may
878bd5f5f4eSMartin KaFai Lau 		 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
879bd5f5f4eSMartin KaFai Lau 		 */
880352d20d6SPeng Sun 		bpf_map_put_with_uref(map);
881bd5f5f4eSMartin KaFai Lau 		return err;
882bd5f5f4eSMartin KaFai Lau 	}
88399c55f7dSAlexei Starovoitov 
88499c55f7dSAlexei Starovoitov 	return err;
88599c55f7dSAlexei Starovoitov 
886afdb09c7SChenbo Feng free_map_sec:
887afdb09c7SChenbo Feng 	security_bpf_map_free(map);
888b936ca64SRoman Gushchin free_map:
889a26ca7c9SMartin KaFai Lau 	btf_put(map->btf);
890b936ca64SRoman Gushchin 	bpf_map_charge_move(&mem, &map->memory);
89199c55f7dSAlexei Starovoitov 	map->ops->map_free(map);
892b936ca64SRoman Gushchin 	bpf_map_charge_finish(&mem);
89399c55f7dSAlexei Starovoitov 	return err;
89499c55f7dSAlexei Starovoitov }
89599c55f7dSAlexei Starovoitov 
896db20fd2bSAlexei Starovoitov /* if error is returned, fd is released.
897db20fd2bSAlexei Starovoitov  * On success caller should complete fd access with matching fdput()
898db20fd2bSAlexei Starovoitov  */
899c2101297SDaniel Borkmann struct bpf_map *__bpf_map_get(struct fd f)
900db20fd2bSAlexei Starovoitov {
901db20fd2bSAlexei Starovoitov 	if (!f.file)
902db20fd2bSAlexei Starovoitov 		return ERR_PTR(-EBADF);
903db20fd2bSAlexei Starovoitov 	if (f.file->f_op != &bpf_map_fops) {
904db20fd2bSAlexei Starovoitov 		fdput(f);
905db20fd2bSAlexei Starovoitov 		return ERR_PTR(-EINVAL);
906db20fd2bSAlexei Starovoitov 	}
907db20fd2bSAlexei Starovoitov 
908c2101297SDaniel Borkmann 	return f.file->private_data;
909c2101297SDaniel Borkmann }
910c2101297SDaniel Borkmann 
9111e0bd5a0SAndrii Nakryiko void bpf_map_inc(struct bpf_map *map)
912c9da161cSDaniel Borkmann {
9131e0bd5a0SAndrii Nakryiko 	atomic64_inc(&map->refcnt);
914c9da161cSDaniel Borkmann }
915630a4d38SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_map_inc);
916c9da161cSDaniel Borkmann 
9171e0bd5a0SAndrii Nakryiko void bpf_map_inc_with_uref(struct bpf_map *map)
9181e0bd5a0SAndrii Nakryiko {
9191e0bd5a0SAndrii Nakryiko 	atomic64_inc(&map->refcnt);
9201e0bd5a0SAndrii Nakryiko 	atomic64_inc(&map->usercnt);
9211e0bd5a0SAndrii Nakryiko }
9221e0bd5a0SAndrii Nakryiko EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
9231e0bd5a0SAndrii Nakryiko 
9241ed4d924SMartin KaFai Lau struct bpf_map *bpf_map_get(u32 ufd)
9251ed4d924SMartin KaFai Lau {
9261ed4d924SMartin KaFai Lau 	struct fd f = fdget(ufd);
9271ed4d924SMartin KaFai Lau 	struct bpf_map *map;
9281ed4d924SMartin KaFai Lau 
9291ed4d924SMartin KaFai Lau 	map = __bpf_map_get(f);
9301ed4d924SMartin KaFai Lau 	if (IS_ERR(map))
9311ed4d924SMartin KaFai Lau 		return map;
9321ed4d924SMartin KaFai Lau 
9331ed4d924SMartin KaFai Lau 	bpf_map_inc(map);
9341ed4d924SMartin KaFai Lau 	fdput(f);
9351ed4d924SMartin KaFai Lau 
9361ed4d924SMartin KaFai Lau 	return map;
9371ed4d924SMartin KaFai Lau }
9381ed4d924SMartin KaFai Lau 
939c9da161cSDaniel Borkmann struct bpf_map *bpf_map_get_with_uref(u32 ufd)
940c2101297SDaniel Borkmann {
941c2101297SDaniel Borkmann 	struct fd f = fdget(ufd);
942c2101297SDaniel Borkmann 	struct bpf_map *map;
943c2101297SDaniel Borkmann 
944c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
945c2101297SDaniel Borkmann 	if (IS_ERR(map))
946c2101297SDaniel Borkmann 		return map;
947c2101297SDaniel Borkmann 
9481e0bd5a0SAndrii Nakryiko 	bpf_map_inc_with_uref(map);
949c2101297SDaniel Borkmann 	fdput(f);
950db20fd2bSAlexei Starovoitov 
951db20fd2bSAlexei Starovoitov 	return map;
952db20fd2bSAlexei Starovoitov }
953db20fd2bSAlexei Starovoitov 
954bd5f5f4eSMartin KaFai Lau /* map_idr_lock should have been held */
9551e0bd5a0SAndrii Nakryiko static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
956bd5f5f4eSMartin KaFai Lau {
957bd5f5f4eSMartin KaFai Lau 	int refold;
958bd5f5f4eSMartin KaFai Lau 
9591e0bd5a0SAndrii Nakryiko 	refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
960bd5f5f4eSMartin KaFai Lau 	if (!refold)
961bd5f5f4eSMartin KaFai Lau 		return ERR_PTR(-ENOENT);
962bd5f5f4eSMartin KaFai Lau 	if (uref)
9631e0bd5a0SAndrii Nakryiko 		atomic64_inc(&map->usercnt);
964bd5f5f4eSMartin KaFai Lau 
965bd5f5f4eSMartin KaFai Lau 	return map;
966bd5f5f4eSMartin KaFai Lau }
967bd5f5f4eSMartin KaFai Lau 
9681e0bd5a0SAndrii Nakryiko struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
969b0e4701cSStanislav Fomichev {
970b0e4701cSStanislav Fomichev 	spin_lock_bh(&map_idr_lock);
9711e0bd5a0SAndrii Nakryiko 	map = __bpf_map_inc_not_zero(map, false);
972b0e4701cSStanislav Fomichev 	spin_unlock_bh(&map_idr_lock);
973b0e4701cSStanislav Fomichev 
974b0e4701cSStanislav Fomichev 	return map;
975b0e4701cSStanislav Fomichev }
976b0e4701cSStanislav Fomichev EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
977b0e4701cSStanislav Fomichev 
978b8cdc051SAlexei Starovoitov int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
979b8cdc051SAlexei Starovoitov {
980b8cdc051SAlexei Starovoitov 	return -ENOTSUPP;
981b8cdc051SAlexei Starovoitov }
982b8cdc051SAlexei Starovoitov 
983c9d29f46SMauricio Vasquez B static void *__bpf_copy_key(void __user *ukey, u64 key_size)
984c9d29f46SMauricio Vasquez B {
985c9d29f46SMauricio Vasquez B 	if (key_size)
986c9d29f46SMauricio Vasquez B 		return memdup_user(ukey, key_size);
987c9d29f46SMauricio Vasquez B 
988c9d29f46SMauricio Vasquez B 	if (ukey)
989c9d29f46SMauricio Vasquez B 		return ERR_PTR(-EINVAL);
990c9d29f46SMauricio Vasquez B 
991c9d29f46SMauricio Vasquez B 	return NULL;
992c9d29f46SMauricio Vasquez B }
993c9d29f46SMauricio Vasquez B 
994db20fd2bSAlexei Starovoitov /* last field in 'union bpf_attr' used by this command */
99596049f3aSAlexei Starovoitov #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
996db20fd2bSAlexei Starovoitov 
997db20fd2bSAlexei Starovoitov static int map_lookup_elem(union bpf_attr *attr)
998db20fd2bSAlexei Starovoitov {
999535e7b4bSMickaël Salaün 	void __user *ukey = u64_to_user_ptr(attr->key);
1000535e7b4bSMickaël Salaün 	void __user *uvalue = u64_to_user_ptr(attr->value);
1001db20fd2bSAlexei Starovoitov 	int ufd = attr->map_fd;
1002db20fd2bSAlexei Starovoitov 	struct bpf_map *map;
100315c14a3dSBrian Vazquez 	void *key, *value;
100415a07b33SAlexei Starovoitov 	u32 value_size;
1005592867bfSDaniel Borkmann 	struct fd f;
1006db20fd2bSAlexei Starovoitov 	int err;
1007db20fd2bSAlexei Starovoitov 
1008db20fd2bSAlexei Starovoitov 	if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1009db20fd2bSAlexei Starovoitov 		return -EINVAL;
1010db20fd2bSAlexei Starovoitov 
101196049f3aSAlexei Starovoitov 	if (attr->flags & ~BPF_F_LOCK)
101296049f3aSAlexei Starovoitov 		return -EINVAL;
101396049f3aSAlexei Starovoitov 
1014592867bfSDaniel Borkmann 	f = fdget(ufd);
1015c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
1016db20fd2bSAlexei Starovoitov 	if (IS_ERR(map))
1017db20fd2bSAlexei Starovoitov 		return PTR_ERR(map);
101887df15deSDaniel Borkmann 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
10196e71b04aSChenbo Feng 		err = -EPERM;
10206e71b04aSChenbo Feng 		goto err_put;
10216e71b04aSChenbo Feng 	}
10226e71b04aSChenbo Feng 
102396049f3aSAlexei Starovoitov 	if ((attr->flags & BPF_F_LOCK) &&
102496049f3aSAlexei Starovoitov 	    !map_value_has_spin_lock(map)) {
102596049f3aSAlexei Starovoitov 		err = -EINVAL;
102696049f3aSAlexei Starovoitov 		goto err_put;
102796049f3aSAlexei Starovoitov 	}
102896049f3aSAlexei Starovoitov 
1029c9d29f46SMauricio Vasquez B 	key = __bpf_copy_key(ukey, map->key_size);
1030e4448ed8SAl Viro 	if (IS_ERR(key)) {
1031e4448ed8SAl Viro 		err = PTR_ERR(key);
1032db20fd2bSAlexei Starovoitov 		goto err_put;
1033e4448ed8SAl Viro 	}
1034db20fd2bSAlexei Starovoitov 
103515c14a3dSBrian Vazquez 	value_size = bpf_map_value_size(map);
103615a07b33SAlexei Starovoitov 
10378ebe667cSAlexei Starovoitov 	err = -ENOMEM;
103815a07b33SAlexei Starovoitov 	value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1039db20fd2bSAlexei Starovoitov 	if (!value)
10408ebe667cSAlexei Starovoitov 		goto free_key;
10418ebe667cSAlexei Starovoitov 
104215c14a3dSBrian Vazquez 	err = bpf_map_copy_value(map, key, value, attr->flags);
104315a07b33SAlexei Starovoitov 	if (err)
10448ebe667cSAlexei Starovoitov 		goto free_value;
1045db20fd2bSAlexei Starovoitov 
1046db20fd2bSAlexei Starovoitov 	err = -EFAULT;
104715a07b33SAlexei Starovoitov 	if (copy_to_user(uvalue, value, value_size) != 0)
10488ebe667cSAlexei Starovoitov 		goto free_value;
1049db20fd2bSAlexei Starovoitov 
1050db20fd2bSAlexei Starovoitov 	err = 0;
1051db20fd2bSAlexei Starovoitov 
10528ebe667cSAlexei Starovoitov free_value:
10538ebe667cSAlexei Starovoitov 	kfree(value);
1054db20fd2bSAlexei Starovoitov free_key:
1055db20fd2bSAlexei Starovoitov 	kfree(key);
1056db20fd2bSAlexei Starovoitov err_put:
1057db20fd2bSAlexei Starovoitov 	fdput(f);
1058db20fd2bSAlexei Starovoitov 	return err;
1059db20fd2bSAlexei Starovoitov }
1060db20fd2bSAlexei Starovoitov 
10611ae80cf3SDaniel Colascione 
10623274f520SAlexei Starovoitov #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1063db20fd2bSAlexei Starovoitov 
1064db20fd2bSAlexei Starovoitov static int map_update_elem(union bpf_attr *attr)
1065db20fd2bSAlexei Starovoitov {
1066535e7b4bSMickaël Salaün 	void __user *ukey = u64_to_user_ptr(attr->key);
1067535e7b4bSMickaël Salaün 	void __user *uvalue = u64_to_user_ptr(attr->value);
1068db20fd2bSAlexei Starovoitov 	int ufd = attr->map_fd;
1069db20fd2bSAlexei Starovoitov 	struct bpf_map *map;
1070db20fd2bSAlexei Starovoitov 	void *key, *value;
107115a07b33SAlexei Starovoitov 	u32 value_size;
1072592867bfSDaniel Borkmann 	struct fd f;
1073db20fd2bSAlexei Starovoitov 	int err;
1074db20fd2bSAlexei Starovoitov 
1075db20fd2bSAlexei Starovoitov 	if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1076db20fd2bSAlexei Starovoitov 		return -EINVAL;
1077db20fd2bSAlexei Starovoitov 
1078592867bfSDaniel Borkmann 	f = fdget(ufd);
1079c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
1080db20fd2bSAlexei Starovoitov 	if (IS_ERR(map))
1081db20fd2bSAlexei Starovoitov 		return PTR_ERR(map);
108287df15deSDaniel Borkmann 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
10836e71b04aSChenbo Feng 		err = -EPERM;
10846e71b04aSChenbo Feng 		goto err_put;
10856e71b04aSChenbo Feng 	}
10866e71b04aSChenbo Feng 
108796049f3aSAlexei Starovoitov 	if ((attr->flags & BPF_F_LOCK) &&
108896049f3aSAlexei Starovoitov 	    !map_value_has_spin_lock(map)) {
108996049f3aSAlexei Starovoitov 		err = -EINVAL;
109096049f3aSAlexei Starovoitov 		goto err_put;
109196049f3aSAlexei Starovoitov 	}
109296049f3aSAlexei Starovoitov 
1093c9d29f46SMauricio Vasquez B 	key = __bpf_copy_key(ukey, map->key_size);
1094e4448ed8SAl Viro 	if (IS_ERR(key)) {
1095e4448ed8SAl Viro 		err = PTR_ERR(key);
1096db20fd2bSAlexei Starovoitov 		goto err_put;
1097e4448ed8SAl Viro 	}
1098db20fd2bSAlexei Starovoitov 
109915a07b33SAlexei Starovoitov 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
11008f844938SMartin KaFai Lau 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
1101b741f163SRoman Gushchin 	    map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1102b741f163SRoman Gushchin 	    map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
110315a07b33SAlexei Starovoitov 		value_size = round_up(map->value_size, 8) * num_possible_cpus();
110415a07b33SAlexei Starovoitov 	else
110515a07b33SAlexei Starovoitov 		value_size = map->value_size;
110615a07b33SAlexei Starovoitov 
1107db20fd2bSAlexei Starovoitov 	err = -ENOMEM;
110815a07b33SAlexei Starovoitov 	value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1109db20fd2bSAlexei Starovoitov 	if (!value)
1110db20fd2bSAlexei Starovoitov 		goto free_key;
1111db20fd2bSAlexei Starovoitov 
1112db20fd2bSAlexei Starovoitov 	err = -EFAULT;
111315a07b33SAlexei Starovoitov 	if (copy_from_user(value, uvalue, value_size) != 0)
1114db20fd2bSAlexei Starovoitov 		goto free_value;
1115db20fd2bSAlexei Starovoitov 
111615c14a3dSBrian Vazquez 	err = bpf_map_update_value(map, f, key, value, attr->flags);
11176710e112SJesper Dangaard Brouer 
1118db20fd2bSAlexei Starovoitov free_value:
1119db20fd2bSAlexei Starovoitov 	kfree(value);
1120db20fd2bSAlexei Starovoitov free_key:
1121db20fd2bSAlexei Starovoitov 	kfree(key);
1122db20fd2bSAlexei Starovoitov err_put:
1123db20fd2bSAlexei Starovoitov 	fdput(f);
1124db20fd2bSAlexei Starovoitov 	return err;
1125db20fd2bSAlexei Starovoitov }
1126db20fd2bSAlexei Starovoitov 
1127db20fd2bSAlexei Starovoitov #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1128db20fd2bSAlexei Starovoitov 
1129db20fd2bSAlexei Starovoitov static int map_delete_elem(union bpf_attr *attr)
1130db20fd2bSAlexei Starovoitov {
1131535e7b4bSMickaël Salaün 	void __user *ukey = u64_to_user_ptr(attr->key);
1132db20fd2bSAlexei Starovoitov 	int ufd = attr->map_fd;
1133db20fd2bSAlexei Starovoitov 	struct bpf_map *map;
1134592867bfSDaniel Borkmann 	struct fd f;
1135db20fd2bSAlexei Starovoitov 	void *key;
1136db20fd2bSAlexei Starovoitov 	int err;
1137db20fd2bSAlexei Starovoitov 
1138db20fd2bSAlexei Starovoitov 	if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1139db20fd2bSAlexei Starovoitov 		return -EINVAL;
1140db20fd2bSAlexei Starovoitov 
1141592867bfSDaniel Borkmann 	f = fdget(ufd);
1142c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
1143db20fd2bSAlexei Starovoitov 	if (IS_ERR(map))
1144db20fd2bSAlexei Starovoitov 		return PTR_ERR(map);
114587df15deSDaniel Borkmann 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
11466e71b04aSChenbo Feng 		err = -EPERM;
11476e71b04aSChenbo Feng 		goto err_put;
11486e71b04aSChenbo Feng 	}
11496e71b04aSChenbo Feng 
1150c9d29f46SMauricio Vasquez B 	key = __bpf_copy_key(ukey, map->key_size);
1151e4448ed8SAl Viro 	if (IS_ERR(key)) {
1152e4448ed8SAl Viro 		err = PTR_ERR(key);
1153db20fd2bSAlexei Starovoitov 		goto err_put;
1154e4448ed8SAl Viro 	}
1155db20fd2bSAlexei Starovoitov 
1156a3884572SJakub Kicinski 	if (bpf_map_is_dev_bound(map)) {
1157a3884572SJakub Kicinski 		err = bpf_map_offload_delete_elem(map, key);
1158a3884572SJakub Kicinski 		goto out;
115985d33df3SMartin KaFai Lau 	} else if (IS_FD_PROG_ARRAY(map) ||
116085d33df3SMartin KaFai Lau 		   map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
116185d33df3SMartin KaFai Lau 		/* These maps require sleepable context */
1162da765a2fSDaniel Borkmann 		err = map->ops->map_delete_elem(map, key);
1163da765a2fSDaniel Borkmann 		goto out;
1164a3884572SJakub Kicinski 	}
1165a3884572SJakub Kicinski 
1166b6e5dae1SThomas Gleixner 	bpf_disable_instrumentation();
1167db20fd2bSAlexei Starovoitov 	rcu_read_lock();
1168db20fd2bSAlexei Starovoitov 	err = map->ops->map_delete_elem(map, key);
1169db20fd2bSAlexei Starovoitov 	rcu_read_unlock();
1170b6e5dae1SThomas Gleixner 	bpf_enable_instrumentation();
11711ae80cf3SDaniel Colascione 	maybe_wait_bpf_programs(map);
1172a3884572SJakub Kicinski out:
1173db20fd2bSAlexei Starovoitov 	kfree(key);
1174db20fd2bSAlexei Starovoitov err_put:
1175db20fd2bSAlexei Starovoitov 	fdput(f);
1176db20fd2bSAlexei Starovoitov 	return err;
1177db20fd2bSAlexei Starovoitov }
1178db20fd2bSAlexei Starovoitov 
1179db20fd2bSAlexei Starovoitov /* last field in 'union bpf_attr' used by this command */
1180db20fd2bSAlexei Starovoitov #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1181db20fd2bSAlexei Starovoitov 
1182db20fd2bSAlexei Starovoitov static int map_get_next_key(union bpf_attr *attr)
1183db20fd2bSAlexei Starovoitov {
1184535e7b4bSMickaël Salaün 	void __user *ukey = u64_to_user_ptr(attr->key);
1185535e7b4bSMickaël Salaün 	void __user *unext_key = u64_to_user_ptr(attr->next_key);
1186db20fd2bSAlexei Starovoitov 	int ufd = attr->map_fd;
1187db20fd2bSAlexei Starovoitov 	struct bpf_map *map;
1188db20fd2bSAlexei Starovoitov 	void *key, *next_key;
1189592867bfSDaniel Borkmann 	struct fd f;
1190db20fd2bSAlexei Starovoitov 	int err;
1191db20fd2bSAlexei Starovoitov 
1192db20fd2bSAlexei Starovoitov 	if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1193db20fd2bSAlexei Starovoitov 		return -EINVAL;
1194db20fd2bSAlexei Starovoitov 
1195592867bfSDaniel Borkmann 	f = fdget(ufd);
1196c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
1197db20fd2bSAlexei Starovoitov 	if (IS_ERR(map))
1198db20fd2bSAlexei Starovoitov 		return PTR_ERR(map);
119987df15deSDaniel Borkmann 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
12006e71b04aSChenbo Feng 		err = -EPERM;
12016e71b04aSChenbo Feng 		goto err_put;
12026e71b04aSChenbo Feng 	}
12036e71b04aSChenbo Feng 
12048fe45924STeng Qin 	if (ukey) {
1205c9d29f46SMauricio Vasquez B 		key = __bpf_copy_key(ukey, map->key_size);
1206e4448ed8SAl Viro 		if (IS_ERR(key)) {
1207e4448ed8SAl Viro 			err = PTR_ERR(key);
1208db20fd2bSAlexei Starovoitov 			goto err_put;
1209e4448ed8SAl Viro 		}
12108fe45924STeng Qin 	} else {
12118fe45924STeng Qin 		key = NULL;
12128fe45924STeng Qin 	}
1213db20fd2bSAlexei Starovoitov 
1214db20fd2bSAlexei Starovoitov 	err = -ENOMEM;
1215db20fd2bSAlexei Starovoitov 	next_key = kmalloc(map->key_size, GFP_USER);
1216db20fd2bSAlexei Starovoitov 	if (!next_key)
1217db20fd2bSAlexei Starovoitov 		goto free_key;
1218db20fd2bSAlexei Starovoitov 
1219a3884572SJakub Kicinski 	if (bpf_map_is_dev_bound(map)) {
1220a3884572SJakub Kicinski 		err = bpf_map_offload_get_next_key(map, key, next_key);
1221a3884572SJakub Kicinski 		goto out;
1222a3884572SJakub Kicinski 	}
1223a3884572SJakub Kicinski 
1224db20fd2bSAlexei Starovoitov 	rcu_read_lock();
1225db20fd2bSAlexei Starovoitov 	err = map->ops->map_get_next_key(map, key, next_key);
1226db20fd2bSAlexei Starovoitov 	rcu_read_unlock();
1227a3884572SJakub Kicinski out:
1228db20fd2bSAlexei Starovoitov 	if (err)
1229db20fd2bSAlexei Starovoitov 		goto free_next_key;
1230db20fd2bSAlexei Starovoitov 
1231db20fd2bSAlexei Starovoitov 	err = -EFAULT;
1232db20fd2bSAlexei Starovoitov 	if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1233db20fd2bSAlexei Starovoitov 		goto free_next_key;
1234db20fd2bSAlexei Starovoitov 
1235db20fd2bSAlexei Starovoitov 	err = 0;
1236db20fd2bSAlexei Starovoitov 
1237db20fd2bSAlexei Starovoitov free_next_key:
1238db20fd2bSAlexei Starovoitov 	kfree(next_key);
1239db20fd2bSAlexei Starovoitov free_key:
1240db20fd2bSAlexei Starovoitov 	kfree(key);
1241db20fd2bSAlexei Starovoitov err_put:
1242db20fd2bSAlexei Starovoitov 	fdput(f);
1243db20fd2bSAlexei Starovoitov 	return err;
1244db20fd2bSAlexei Starovoitov }
1245db20fd2bSAlexei Starovoitov 
1246aa2e93b8SBrian Vazquez int generic_map_delete_batch(struct bpf_map *map,
1247aa2e93b8SBrian Vazquez 			     const union bpf_attr *attr,
1248aa2e93b8SBrian Vazquez 			     union bpf_attr __user *uattr)
1249aa2e93b8SBrian Vazquez {
1250aa2e93b8SBrian Vazquez 	void __user *keys = u64_to_user_ptr(attr->batch.keys);
1251aa2e93b8SBrian Vazquez 	u32 cp, max_count;
1252aa2e93b8SBrian Vazquez 	int err = 0;
1253aa2e93b8SBrian Vazquez 	void *key;
1254aa2e93b8SBrian Vazquez 
1255aa2e93b8SBrian Vazquez 	if (attr->batch.elem_flags & ~BPF_F_LOCK)
1256aa2e93b8SBrian Vazquez 		return -EINVAL;
1257aa2e93b8SBrian Vazquez 
1258aa2e93b8SBrian Vazquez 	if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1259aa2e93b8SBrian Vazquez 	    !map_value_has_spin_lock(map)) {
1260aa2e93b8SBrian Vazquez 		return -EINVAL;
1261aa2e93b8SBrian Vazquez 	}
1262aa2e93b8SBrian Vazquez 
1263aa2e93b8SBrian Vazquez 	max_count = attr->batch.count;
1264aa2e93b8SBrian Vazquez 	if (!max_count)
1265aa2e93b8SBrian Vazquez 		return 0;
1266aa2e93b8SBrian Vazquez 
12672e3a94aaSBrian Vazquez 	key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
12682e3a94aaSBrian Vazquez 	if (!key)
12692e3a94aaSBrian Vazquez 		return -ENOMEM;
12702e3a94aaSBrian Vazquez 
1271aa2e93b8SBrian Vazquez 	for (cp = 0; cp < max_count; cp++) {
12722e3a94aaSBrian Vazquez 		err = -EFAULT;
12732e3a94aaSBrian Vazquez 		if (copy_from_user(key, keys + cp * map->key_size,
12742e3a94aaSBrian Vazquez 				   map->key_size))
1275aa2e93b8SBrian Vazquez 			break;
1276aa2e93b8SBrian Vazquez 
1277aa2e93b8SBrian Vazquez 		if (bpf_map_is_dev_bound(map)) {
1278aa2e93b8SBrian Vazquez 			err = bpf_map_offload_delete_elem(map, key);
1279aa2e93b8SBrian Vazquez 			break;
1280aa2e93b8SBrian Vazquez 		}
1281aa2e93b8SBrian Vazquez 
1282b6e5dae1SThomas Gleixner 		bpf_disable_instrumentation();
1283aa2e93b8SBrian Vazquez 		rcu_read_lock();
1284aa2e93b8SBrian Vazquez 		err = map->ops->map_delete_elem(map, key);
1285aa2e93b8SBrian Vazquez 		rcu_read_unlock();
1286b6e5dae1SThomas Gleixner 		bpf_enable_instrumentation();
1287aa2e93b8SBrian Vazquez 		maybe_wait_bpf_programs(map);
1288aa2e93b8SBrian Vazquez 		if (err)
1289aa2e93b8SBrian Vazquez 			break;
1290aa2e93b8SBrian Vazquez 	}
1291aa2e93b8SBrian Vazquez 	if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1292aa2e93b8SBrian Vazquez 		err = -EFAULT;
12932e3a94aaSBrian Vazquez 
12942e3a94aaSBrian Vazquez 	kfree(key);
1295aa2e93b8SBrian Vazquez 	return err;
1296aa2e93b8SBrian Vazquez }
1297aa2e93b8SBrian Vazquez 
1298aa2e93b8SBrian Vazquez int generic_map_update_batch(struct bpf_map *map,
1299aa2e93b8SBrian Vazquez 			     const union bpf_attr *attr,
1300aa2e93b8SBrian Vazquez 			     union bpf_attr __user *uattr)
1301aa2e93b8SBrian Vazquez {
1302aa2e93b8SBrian Vazquez 	void __user *values = u64_to_user_ptr(attr->batch.values);
1303aa2e93b8SBrian Vazquez 	void __user *keys = u64_to_user_ptr(attr->batch.keys);
1304aa2e93b8SBrian Vazquez 	u32 value_size, cp, max_count;
1305aa2e93b8SBrian Vazquez 	int ufd = attr->map_fd;
1306aa2e93b8SBrian Vazquez 	void *key, *value;
1307aa2e93b8SBrian Vazquez 	struct fd f;
1308aa2e93b8SBrian Vazquez 	int err = 0;
1309aa2e93b8SBrian Vazquez 
1310aa2e93b8SBrian Vazquez 	f = fdget(ufd);
1311aa2e93b8SBrian Vazquez 	if (attr->batch.elem_flags & ~BPF_F_LOCK)
1312aa2e93b8SBrian Vazquez 		return -EINVAL;
1313aa2e93b8SBrian Vazquez 
1314aa2e93b8SBrian Vazquez 	if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1315aa2e93b8SBrian Vazquez 	    !map_value_has_spin_lock(map)) {
1316aa2e93b8SBrian Vazquez 		return -EINVAL;
1317aa2e93b8SBrian Vazquez 	}
1318aa2e93b8SBrian Vazquez 
1319aa2e93b8SBrian Vazquez 	value_size = bpf_map_value_size(map);
1320aa2e93b8SBrian Vazquez 
1321aa2e93b8SBrian Vazquez 	max_count = attr->batch.count;
1322aa2e93b8SBrian Vazquez 	if (!max_count)
1323aa2e93b8SBrian Vazquez 		return 0;
1324aa2e93b8SBrian Vazquez 
13252e3a94aaSBrian Vazquez 	key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
13262e3a94aaSBrian Vazquez 	if (!key)
1327aa2e93b8SBrian Vazquez 		return -ENOMEM;
1328aa2e93b8SBrian Vazquez 
13292e3a94aaSBrian Vazquez 	value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
13302e3a94aaSBrian Vazquez 	if (!value) {
13312e3a94aaSBrian Vazquez 		kfree(key);
13322e3a94aaSBrian Vazquez 		return -ENOMEM;
1333aa2e93b8SBrian Vazquez 	}
13342e3a94aaSBrian Vazquez 
13352e3a94aaSBrian Vazquez 	for (cp = 0; cp < max_count; cp++) {
1336aa2e93b8SBrian Vazquez 		err = -EFAULT;
13372e3a94aaSBrian Vazquez 		if (copy_from_user(key, keys + cp * map->key_size,
13382e3a94aaSBrian Vazquez 		    map->key_size) ||
13392e3a94aaSBrian Vazquez 		    copy_from_user(value, values + cp * value_size, value_size))
1340aa2e93b8SBrian Vazquez 			break;
1341aa2e93b8SBrian Vazquez 
1342aa2e93b8SBrian Vazquez 		err = bpf_map_update_value(map, f, key, value,
1343aa2e93b8SBrian Vazquez 					   attr->batch.elem_flags);
1344aa2e93b8SBrian Vazquez 
1345aa2e93b8SBrian Vazquez 		if (err)
1346aa2e93b8SBrian Vazquez 			break;
1347aa2e93b8SBrian Vazquez 	}
1348aa2e93b8SBrian Vazquez 
1349aa2e93b8SBrian Vazquez 	if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1350aa2e93b8SBrian Vazquez 		err = -EFAULT;
1351aa2e93b8SBrian Vazquez 
1352aa2e93b8SBrian Vazquez 	kfree(value);
1353aa2e93b8SBrian Vazquez 	kfree(key);
1354aa2e93b8SBrian Vazquez 	return err;
1355aa2e93b8SBrian Vazquez }
1356aa2e93b8SBrian Vazquez 
1357cb4d03abSBrian Vazquez #define MAP_LOOKUP_RETRIES 3
1358cb4d03abSBrian Vazquez 
1359cb4d03abSBrian Vazquez int generic_map_lookup_batch(struct bpf_map *map,
1360cb4d03abSBrian Vazquez 				    const union bpf_attr *attr,
1361cb4d03abSBrian Vazquez 				    union bpf_attr __user *uattr)
1362cb4d03abSBrian Vazquez {
1363cb4d03abSBrian Vazquez 	void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1364cb4d03abSBrian Vazquez 	void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1365cb4d03abSBrian Vazquez 	void __user *values = u64_to_user_ptr(attr->batch.values);
1366cb4d03abSBrian Vazquez 	void __user *keys = u64_to_user_ptr(attr->batch.keys);
1367cb4d03abSBrian Vazquez 	void *buf, *buf_prevkey, *prev_key, *key, *value;
1368cb4d03abSBrian Vazquez 	int err, retry = MAP_LOOKUP_RETRIES;
1369cb4d03abSBrian Vazquez 	u32 value_size, cp, max_count;
1370cb4d03abSBrian Vazquez 
1371cb4d03abSBrian Vazquez 	if (attr->batch.elem_flags & ~BPF_F_LOCK)
1372cb4d03abSBrian Vazquez 		return -EINVAL;
1373cb4d03abSBrian Vazquez 
1374cb4d03abSBrian Vazquez 	if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1375cb4d03abSBrian Vazquez 	    !map_value_has_spin_lock(map))
1376cb4d03abSBrian Vazquez 		return -EINVAL;
1377cb4d03abSBrian Vazquez 
1378cb4d03abSBrian Vazquez 	value_size = bpf_map_value_size(map);
1379cb4d03abSBrian Vazquez 
1380cb4d03abSBrian Vazquez 	max_count = attr->batch.count;
1381cb4d03abSBrian Vazquez 	if (!max_count)
1382cb4d03abSBrian Vazquez 		return 0;
1383cb4d03abSBrian Vazquez 
1384cb4d03abSBrian Vazquez 	if (put_user(0, &uattr->batch.count))
1385cb4d03abSBrian Vazquez 		return -EFAULT;
1386cb4d03abSBrian Vazquez 
1387cb4d03abSBrian Vazquez 	buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1388cb4d03abSBrian Vazquez 	if (!buf_prevkey)
1389cb4d03abSBrian Vazquez 		return -ENOMEM;
1390cb4d03abSBrian Vazquez 
1391cb4d03abSBrian Vazquez 	buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1392cb4d03abSBrian Vazquez 	if (!buf) {
1393bb2359f4SDenis Efremov 		kfree(buf_prevkey);
1394cb4d03abSBrian Vazquez 		return -ENOMEM;
1395cb4d03abSBrian Vazquez 	}
1396cb4d03abSBrian Vazquez 
1397cb4d03abSBrian Vazquez 	err = -EFAULT;
1398cb4d03abSBrian Vazquez 	prev_key = NULL;
1399cb4d03abSBrian Vazquez 	if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1400cb4d03abSBrian Vazquez 		goto free_buf;
1401cb4d03abSBrian Vazquez 	key = buf;
1402cb4d03abSBrian Vazquez 	value = key + map->key_size;
1403cb4d03abSBrian Vazquez 	if (ubatch)
1404cb4d03abSBrian Vazquez 		prev_key = buf_prevkey;
1405cb4d03abSBrian Vazquez 
1406cb4d03abSBrian Vazquez 	for (cp = 0; cp < max_count;) {
1407cb4d03abSBrian Vazquez 		rcu_read_lock();
1408cb4d03abSBrian Vazquez 		err = map->ops->map_get_next_key(map, prev_key, key);
1409cb4d03abSBrian Vazquez 		rcu_read_unlock();
1410cb4d03abSBrian Vazquez 		if (err)
1411cb4d03abSBrian Vazquez 			break;
1412cb4d03abSBrian Vazquez 		err = bpf_map_copy_value(map, key, value,
1413cb4d03abSBrian Vazquez 					 attr->batch.elem_flags);
1414cb4d03abSBrian Vazquez 
1415cb4d03abSBrian Vazquez 		if (err == -ENOENT) {
1416cb4d03abSBrian Vazquez 			if (retry) {
1417cb4d03abSBrian Vazquez 				retry--;
1418cb4d03abSBrian Vazquez 				continue;
1419cb4d03abSBrian Vazquez 			}
1420cb4d03abSBrian Vazquez 			err = -EINTR;
1421cb4d03abSBrian Vazquez 			break;
1422cb4d03abSBrian Vazquez 		}
1423cb4d03abSBrian Vazquez 
1424cb4d03abSBrian Vazquez 		if (err)
1425cb4d03abSBrian Vazquez 			goto free_buf;
1426cb4d03abSBrian Vazquez 
1427cb4d03abSBrian Vazquez 		if (copy_to_user(keys + cp * map->key_size, key,
1428cb4d03abSBrian Vazquez 				 map->key_size)) {
1429cb4d03abSBrian Vazquez 			err = -EFAULT;
1430cb4d03abSBrian Vazquez 			goto free_buf;
1431cb4d03abSBrian Vazquez 		}
1432cb4d03abSBrian Vazquez 		if (copy_to_user(values + cp * value_size, value, value_size)) {
1433cb4d03abSBrian Vazquez 			err = -EFAULT;
1434cb4d03abSBrian Vazquez 			goto free_buf;
1435cb4d03abSBrian Vazquez 		}
1436cb4d03abSBrian Vazquez 
1437cb4d03abSBrian Vazquez 		if (!prev_key)
1438cb4d03abSBrian Vazquez 			prev_key = buf_prevkey;
1439cb4d03abSBrian Vazquez 
1440cb4d03abSBrian Vazquez 		swap(prev_key, key);
1441cb4d03abSBrian Vazquez 		retry = MAP_LOOKUP_RETRIES;
1442cb4d03abSBrian Vazquez 		cp++;
1443cb4d03abSBrian Vazquez 	}
1444cb4d03abSBrian Vazquez 
1445cb4d03abSBrian Vazquez 	if (err == -EFAULT)
1446cb4d03abSBrian Vazquez 		goto free_buf;
1447cb4d03abSBrian Vazquez 
1448cb4d03abSBrian Vazquez 	if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1449cb4d03abSBrian Vazquez 		    (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1450cb4d03abSBrian Vazquez 		err = -EFAULT;
1451cb4d03abSBrian Vazquez 
1452cb4d03abSBrian Vazquez free_buf:
1453cb4d03abSBrian Vazquez 	kfree(buf_prevkey);
1454cb4d03abSBrian Vazquez 	kfree(buf);
1455cb4d03abSBrian Vazquez 	return err;
1456cb4d03abSBrian Vazquez }
1457cb4d03abSBrian Vazquez 
1458bd513cd0SMauricio Vasquez B #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1459bd513cd0SMauricio Vasquez B 
1460bd513cd0SMauricio Vasquez B static int map_lookup_and_delete_elem(union bpf_attr *attr)
1461bd513cd0SMauricio Vasquez B {
1462bd513cd0SMauricio Vasquez B 	void __user *ukey = u64_to_user_ptr(attr->key);
1463bd513cd0SMauricio Vasquez B 	void __user *uvalue = u64_to_user_ptr(attr->value);
1464bd513cd0SMauricio Vasquez B 	int ufd = attr->map_fd;
1465bd513cd0SMauricio Vasquez B 	struct bpf_map *map;
1466540fefc0SAlexei Starovoitov 	void *key, *value;
1467bd513cd0SMauricio Vasquez B 	u32 value_size;
1468bd513cd0SMauricio Vasquez B 	struct fd f;
1469bd513cd0SMauricio Vasquez B 	int err;
1470bd513cd0SMauricio Vasquez B 
1471bd513cd0SMauricio Vasquez B 	if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1472bd513cd0SMauricio Vasquez B 		return -EINVAL;
1473bd513cd0SMauricio Vasquez B 
1474bd513cd0SMauricio Vasquez B 	f = fdget(ufd);
1475bd513cd0SMauricio Vasquez B 	map = __bpf_map_get(f);
1476bd513cd0SMauricio Vasquez B 	if (IS_ERR(map))
1477bd513cd0SMauricio Vasquez B 		return PTR_ERR(map);
14781ea0f912SAnton Protopopov 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
14791ea0f912SAnton Protopopov 	    !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1480bd513cd0SMauricio Vasquez B 		err = -EPERM;
1481bd513cd0SMauricio Vasquez B 		goto err_put;
1482bd513cd0SMauricio Vasquez B 	}
1483bd513cd0SMauricio Vasquez B 
1484bd513cd0SMauricio Vasquez B 	key = __bpf_copy_key(ukey, map->key_size);
1485bd513cd0SMauricio Vasquez B 	if (IS_ERR(key)) {
1486bd513cd0SMauricio Vasquez B 		err = PTR_ERR(key);
1487bd513cd0SMauricio Vasquez B 		goto err_put;
1488bd513cd0SMauricio Vasquez B 	}
1489bd513cd0SMauricio Vasquez B 
1490bd513cd0SMauricio Vasquez B 	value_size = map->value_size;
1491bd513cd0SMauricio Vasquez B 
1492bd513cd0SMauricio Vasquez B 	err = -ENOMEM;
1493bd513cd0SMauricio Vasquez B 	value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1494bd513cd0SMauricio Vasquez B 	if (!value)
1495bd513cd0SMauricio Vasquez B 		goto free_key;
1496bd513cd0SMauricio Vasquez B 
1497bd513cd0SMauricio Vasquez B 	if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1498bd513cd0SMauricio Vasquez B 	    map->map_type == BPF_MAP_TYPE_STACK) {
1499bd513cd0SMauricio Vasquez B 		err = map->ops->map_pop_elem(map, value);
1500bd513cd0SMauricio Vasquez B 	} else {
1501bd513cd0SMauricio Vasquez B 		err = -ENOTSUPP;
1502bd513cd0SMauricio Vasquez B 	}
1503bd513cd0SMauricio Vasquez B 
1504bd513cd0SMauricio Vasquez B 	if (err)
1505bd513cd0SMauricio Vasquez B 		goto free_value;
1506bd513cd0SMauricio Vasquez B 
15077f645462SWei Yongjun 	if (copy_to_user(uvalue, value, value_size) != 0) {
15087f645462SWei Yongjun 		err = -EFAULT;
1509bd513cd0SMauricio Vasquez B 		goto free_value;
15107f645462SWei Yongjun 	}
1511bd513cd0SMauricio Vasquez B 
1512bd513cd0SMauricio Vasquez B 	err = 0;
1513bd513cd0SMauricio Vasquez B 
1514bd513cd0SMauricio Vasquez B free_value:
1515bd513cd0SMauricio Vasquez B 	kfree(value);
1516bd513cd0SMauricio Vasquez B free_key:
1517bd513cd0SMauricio Vasquez B 	kfree(key);
1518bd513cd0SMauricio Vasquez B err_put:
1519bd513cd0SMauricio Vasquez B 	fdput(f);
1520bd513cd0SMauricio Vasquez B 	return err;
1521bd513cd0SMauricio Vasquez B }
1522bd513cd0SMauricio Vasquez B 
152387df15deSDaniel Borkmann #define BPF_MAP_FREEZE_LAST_FIELD map_fd
152487df15deSDaniel Borkmann 
152587df15deSDaniel Borkmann static int map_freeze(const union bpf_attr *attr)
152687df15deSDaniel Borkmann {
152787df15deSDaniel Borkmann 	int err = 0, ufd = attr->map_fd;
152887df15deSDaniel Borkmann 	struct bpf_map *map;
152987df15deSDaniel Borkmann 	struct fd f;
153087df15deSDaniel Borkmann 
153187df15deSDaniel Borkmann 	if (CHECK_ATTR(BPF_MAP_FREEZE))
153287df15deSDaniel Borkmann 		return -EINVAL;
153387df15deSDaniel Borkmann 
153487df15deSDaniel Borkmann 	f = fdget(ufd);
153587df15deSDaniel Borkmann 	map = __bpf_map_get(f);
153687df15deSDaniel Borkmann 	if (IS_ERR(map))
153787df15deSDaniel Borkmann 		return PTR_ERR(map);
1538fc970227SAndrii Nakryiko 
1539849b4d94SMartin KaFai Lau 	if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1540849b4d94SMartin KaFai Lau 		fdput(f);
1541849b4d94SMartin KaFai Lau 		return -ENOTSUPP;
1542849b4d94SMartin KaFai Lau 	}
1543849b4d94SMartin KaFai Lau 
1544fc970227SAndrii Nakryiko 	mutex_lock(&map->freeze_mutex);
1545fc970227SAndrii Nakryiko 
1546fc970227SAndrii Nakryiko 	if (map->writecnt) {
1547fc970227SAndrii Nakryiko 		err = -EBUSY;
1548fc970227SAndrii Nakryiko 		goto err_put;
1549fc970227SAndrii Nakryiko 	}
155087df15deSDaniel Borkmann 	if (READ_ONCE(map->frozen)) {
155187df15deSDaniel Borkmann 		err = -EBUSY;
155287df15deSDaniel Borkmann 		goto err_put;
155387df15deSDaniel Borkmann 	}
15542c78ee89SAlexei Starovoitov 	if (!bpf_capable()) {
155587df15deSDaniel Borkmann 		err = -EPERM;
155687df15deSDaniel Borkmann 		goto err_put;
155787df15deSDaniel Borkmann 	}
155887df15deSDaniel Borkmann 
155987df15deSDaniel Borkmann 	WRITE_ONCE(map->frozen, true);
156087df15deSDaniel Borkmann err_put:
1561fc970227SAndrii Nakryiko 	mutex_unlock(&map->freeze_mutex);
156287df15deSDaniel Borkmann 	fdput(f);
156387df15deSDaniel Borkmann 	return err;
156487df15deSDaniel Borkmann }
156587df15deSDaniel Borkmann 
15667de16e3aSJakub Kicinski static const struct bpf_prog_ops * const bpf_prog_types[] = {
156791cc1a99SAlexei Starovoitov #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
15687de16e3aSJakub Kicinski 	[_id] = & _name ## _prog_ops,
15697de16e3aSJakub Kicinski #define BPF_MAP_TYPE(_id, _ops)
1570f2e10bffSAndrii Nakryiko #define BPF_LINK_TYPE(_id, _name)
15717de16e3aSJakub Kicinski #include <linux/bpf_types.h>
15727de16e3aSJakub Kicinski #undef BPF_PROG_TYPE
15737de16e3aSJakub Kicinski #undef BPF_MAP_TYPE
1574f2e10bffSAndrii Nakryiko #undef BPF_LINK_TYPE
15757de16e3aSJakub Kicinski };
15767de16e3aSJakub Kicinski 
157709756af4SAlexei Starovoitov static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
157809756af4SAlexei Starovoitov {
1579d0f1a451SDaniel Borkmann 	const struct bpf_prog_ops *ops;
1580d0f1a451SDaniel Borkmann 
1581d0f1a451SDaniel Borkmann 	if (type >= ARRAY_SIZE(bpf_prog_types))
1582d0f1a451SDaniel Borkmann 		return -EINVAL;
1583d0f1a451SDaniel Borkmann 	type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1584d0f1a451SDaniel Borkmann 	ops = bpf_prog_types[type];
1585d0f1a451SDaniel Borkmann 	if (!ops)
1586be9370a7SJohannes Berg 		return -EINVAL;
158709756af4SAlexei Starovoitov 
1588ab3f0063SJakub Kicinski 	if (!bpf_prog_is_dev_bound(prog->aux))
1589d0f1a451SDaniel Borkmann 		prog->aux->ops = ops;
1590ab3f0063SJakub Kicinski 	else
1591ab3f0063SJakub Kicinski 		prog->aux->ops = &bpf_offload_prog_ops;
159224701eceSDaniel Borkmann 	prog->type = type;
159309756af4SAlexei Starovoitov 	return 0;
159409756af4SAlexei Starovoitov }
159509756af4SAlexei Starovoitov 
1596bae141f5SDaniel Borkmann enum bpf_audit {
1597bae141f5SDaniel Borkmann 	BPF_AUDIT_LOAD,
1598bae141f5SDaniel Borkmann 	BPF_AUDIT_UNLOAD,
1599bae141f5SDaniel Borkmann 	BPF_AUDIT_MAX,
1600bae141f5SDaniel Borkmann };
1601bae141f5SDaniel Borkmann 
1602bae141f5SDaniel Borkmann static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1603bae141f5SDaniel Borkmann 	[BPF_AUDIT_LOAD]   = "LOAD",
1604bae141f5SDaniel Borkmann 	[BPF_AUDIT_UNLOAD] = "UNLOAD",
1605bae141f5SDaniel Borkmann };
1606bae141f5SDaniel Borkmann 
1607bae141f5SDaniel Borkmann static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1608bae141f5SDaniel Borkmann {
1609bae141f5SDaniel Borkmann 	struct audit_context *ctx = NULL;
1610bae141f5SDaniel Borkmann 	struct audit_buffer *ab;
1611bae141f5SDaniel Borkmann 
1612bae141f5SDaniel Borkmann 	if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1613bae141f5SDaniel Borkmann 		return;
1614bae141f5SDaniel Borkmann 	if (audit_enabled == AUDIT_OFF)
1615bae141f5SDaniel Borkmann 		return;
1616bae141f5SDaniel Borkmann 	if (op == BPF_AUDIT_LOAD)
1617bae141f5SDaniel Borkmann 		ctx = audit_context();
1618bae141f5SDaniel Borkmann 	ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1619bae141f5SDaniel Borkmann 	if (unlikely(!ab))
1620bae141f5SDaniel Borkmann 		return;
1621bae141f5SDaniel Borkmann 	audit_log_format(ab, "prog-id=%u op=%s",
1622bae141f5SDaniel Borkmann 			 prog->aux->id, bpf_audit_str[op]);
1623bae141f5SDaniel Borkmann 	audit_log_end(ab);
1624bae141f5SDaniel Borkmann }
1625bae141f5SDaniel Borkmann 
16265ccb071eSDaniel Borkmann int __bpf_prog_charge(struct user_struct *user, u32 pages)
16275ccb071eSDaniel Borkmann {
16285ccb071eSDaniel Borkmann 	unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
16295ccb071eSDaniel Borkmann 	unsigned long user_bufs;
16305ccb071eSDaniel Borkmann 
16315ccb071eSDaniel Borkmann 	if (user) {
16325ccb071eSDaniel Borkmann 		user_bufs = atomic_long_add_return(pages, &user->locked_vm);
16335ccb071eSDaniel Borkmann 		if (user_bufs > memlock_limit) {
16345ccb071eSDaniel Borkmann 			atomic_long_sub(pages, &user->locked_vm);
16355ccb071eSDaniel Borkmann 			return -EPERM;
16365ccb071eSDaniel Borkmann 		}
16375ccb071eSDaniel Borkmann 	}
16385ccb071eSDaniel Borkmann 
16395ccb071eSDaniel Borkmann 	return 0;
16405ccb071eSDaniel Borkmann }
16415ccb071eSDaniel Borkmann 
16425ccb071eSDaniel Borkmann void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
16435ccb071eSDaniel Borkmann {
16445ccb071eSDaniel Borkmann 	if (user)
16455ccb071eSDaniel Borkmann 		atomic_long_sub(pages, &user->locked_vm);
16465ccb071eSDaniel Borkmann }
16475ccb071eSDaniel Borkmann 
1648aaac3ba9SAlexei Starovoitov static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1649aaac3ba9SAlexei Starovoitov {
1650aaac3ba9SAlexei Starovoitov 	struct user_struct *user = get_current_user();
16515ccb071eSDaniel Borkmann 	int ret;
1652aaac3ba9SAlexei Starovoitov 
16535ccb071eSDaniel Borkmann 	ret = __bpf_prog_charge(user, prog->pages);
16545ccb071eSDaniel Borkmann 	if (ret) {
1655aaac3ba9SAlexei Starovoitov 		free_uid(user);
16565ccb071eSDaniel Borkmann 		return ret;
1657aaac3ba9SAlexei Starovoitov 	}
16585ccb071eSDaniel Borkmann 
1659aaac3ba9SAlexei Starovoitov 	prog->aux->user = user;
1660aaac3ba9SAlexei Starovoitov 	return 0;
1661aaac3ba9SAlexei Starovoitov }
1662aaac3ba9SAlexei Starovoitov 
1663aaac3ba9SAlexei Starovoitov static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1664aaac3ba9SAlexei Starovoitov {
1665aaac3ba9SAlexei Starovoitov 	struct user_struct *user = prog->aux->user;
1666aaac3ba9SAlexei Starovoitov 
16675ccb071eSDaniel Borkmann 	__bpf_prog_uncharge(user, prog->pages);
1668aaac3ba9SAlexei Starovoitov 	free_uid(user);
1669aaac3ba9SAlexei Starovoitov }
1670aaac3ba9SAlexei Starovoitov 
1671dc4bb0e2SMartin KaFai Lau static int bpf_prog_alloc_id(struct bpf_prog *prog)
1672dc4bb0e2SMartin KaFai Lau {
1673dc4bb0e2SMartin KaFai Lau 	int id;
1674dc4bb0e2SMartin KaFai Lau 
1675b76354cdSShaohua Li 	idr_preload(GFP_KERNEL);
1676dc4bb0e2SMartin KaFai Lau 	spin_lock_bh(&prog_idr_lock);
1677dc4bb0e2SMartin KaFai Lau 	id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1678dc4bb0e2SMartin KaFai Lau 	if (id > 0)
1679dc4bb0e2SMartin KaFai Lau 		prog->aux->id = id;
1680dc4bb0e2SMartin KaFai Lau 	spin_unlock_bh(&prog_idr_lock);
1681b76354cdSShaohua Li 	idr_preload_end();
1682dc4bb0e2SMartin KaFai Lau 
1683dc4bb0e2SMartin KaFai Lau 	/* id is in [1, INT_MAX) */
1684dc4bb0e2SMartin KaFai Lau 	if (WARN_ON_ONCE(!id))
1685dc4bb0e2SMartin KaFai Lau 		return -ENOSPC;
1686dc4bb0e2SMartin KaFai Lau 
1687dc4bb0e2SMartin KaFai Lau 	return id > 0 ? 0 : id;
1688dc4bb0e2SMartin KaFai Lau }
1689dc4bb0e2SMartin KaFai Lau 
1690ad8ad79fSJakub Kicinski void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1691dc4bb0e2SMartin KaFai Lau {
1692ad8ad79fSJakub Kicinski 	/* cBPF to eBPF migrations are currently not in the idr store.
1693ad8ad79fSJakub Kicinski 	 * Offloaded programs are removed from the store when their device
1694ad8ad79fSJakub Kicinski 	 * disappears - even if someone grabs an fd to them they are unusable,
1695ad8ad79fSJakub Kicinski 	 * simply waiting for refcnt to drop to be freed.
1696ad8ad79fSJakub Kicinski 	 */
1697dc4bb0e2SMartin KaFai Lau 	if (!prog->aux->id)
1698dc4bb0e2SMartin KaFai Lau 		return;
1699dc4bb0e2SMartin KaFai Lau 
1700b16d9aa4SMartin KaFai Lau 	if (do_idr_lock)
1701dc4bb0e2SMartin KaFai Lau 		spin_lock_bh(&prog_idr_lock);
1702b16d9aa4SMartin KaFai Lau 	else
1703b16d9aa4SMartin KaFai Lau 		__acquire(&prog_idr_lock);
1704b16d9aa4SMartin KaFai Lau 
1705dc4bb0e2SMartin KaFai Lau 	idr_remove(&prog_idr, prog->aux->id);
1706ad8ad79fSJakub Kicinski 	prog->aux->id = 0;
1707b16d9aa4SMartin KaFai Lau 
1708b16d9aa4SMartin KaFai Lau 	if (do_idr_lock)
1709dc4bb0e2SMartin KaFai Lau 		spin_unlock_bh(&prog_idr_lock);
1710b16d9aa4SMartin KaFai Lau 	else
1711b16d9aa4SMartin KaFai Lau 		__release(&prog_idr_lock);
1712dc4bb0e2SMartin KaFai Lau }
1713dc4bb0e2SMartin KaFai Lau 
17141aacde3dSDaniel Borkmann static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1715abf2e7d6SAlexei Starovoitov {
1716abf2e7d6SAlexei Starovoitov 	struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1717abf2e7d6SAlexei Starovoitov 
17183b4d9eb2SDaniel Borkmann 	kvfree(aux->func_info);
17198c1b6e69SAlexei Starovoitov 	kfree(aux->func_info_aux);
1720aaac3ba9SAlexei Starovoitov 	bpf_prog_uncharge_memlock(aux->prog);
1721afdb09c7SChenbo Feng 	security_bpf_prog_free(aux);
1722abf2e7d6SAlexei Starovoitov 	bpf_prog_free(aux->prog);
1723abf2e7d6SAlexei Starovoitov }
1724abf2e7d6SAlexei Starovoitov 
1725cd7455f1SDaniel Borkmann static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1726cd7455f1SDaniel Borkmann {
1727cd7455f1SDaniel Borkmann 	bpf_prog_kallsyms_del_all(prog);
1728cd7455f1SDaniel Borkmann 	btf_put(prog->aux->btf);
1729cd7455f1SDaniel Borkmann 	bpf_prog_free_linfo(prog);
1730cd7455f1SDaniel Borkmann 
1731cd7455f1SDaniel Borkmann 	if (deferred)
1732cd7455f1SDaniel Borkmann 		call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1733cd7455f1SDaniel Borkmann 	else
1734cd7455f1SDaniel Borkmann 		__bpf_prog_put_rcu(&prog->aux->rcu);
1735cd7455f1SDaniel Borkmann }
1736cd7455f1SDaniel Borkmann 
1737b16d9aa4SMartin KaFai Lau static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
173809756af4SAlexei Starovoitov {
173985192dbfSAndrii Nakryiko 	if (atomic64_dec_and_test(&prog->aux->refcnt)) {
17406ee52e2aSSong Liu 		perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1741bae141f5SDaniel Borkmann 		bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
174234ad5580SMartin KaFai Lau 		/* bpf_prog_free_id() must be called first */
1743b16d9aa4SMartin KaFai Lau 		bpf_prog_free_id(prog, do_idr_lock);
1744cd7455f1SDaniel Borkmann 		__bpf_prog_put_noref(prog, true);
174509756af4SAlexei Starovoitov 	}
1746a67edbf4SDaniel Borkmann }
1747b16d9aa4SMartin KaFai Lau 
1748b16d9aa4SMartin KaFai Lau void bpf_prog_put(struct bpf_prog *prog)
1749b16d9aa4SMartin KaFai Lau {
1750b16d9aa4SMartin KaFai Lau 	__bpf_prog_put(prog, true);
1751b16d9aa4SMartin KaFai Lau }
1752e2e9b654SDaniel Borkmann EXPORT_SYMBOL_GPL(bpf_prog_put);
175309756af4SAlexei Starovoitov 
175409756af4SAlexei Starovoitov static int bpf_prog_release(struct inode *inode, struct file *filp)
175509756af4SAlexei Starovoitov {
175609756af4SAlexei Starovoitov 	struct bpf_prog *prog = filp->private_data;
175709756af4SAlexei Starovoitov 
17581aacde3dSDaniel Borkmann 	bpf_prog_put(prog);
175909756af4SAlexei Starovoitov 	return 0;
176009756af4SAlexei Starovoitov }
176109756af4SAlexei Starovoitov 
1762492ecee8SAlexei Starovoitov static void bpf_prog_get_stats(const struct bpf_prog *prog,
1763492ecee8SAlexei Starovoitov 			       struct bpf_prog_stats *stats)
1764492ecee8SAlexei Starovoitov {
1765492ecee8SAlexei Starovoitov 	u64 nsecs = 0, cnt = 0;
1766492ecee8SAlexei Starovoitov 	int cpu;
1767492ecee8SAlexei Starovoitov 
1768492ecee8SAlexei Starovoitov 	for_each_possible_cpu(cpu) {
1769492ecee8SAlexei Starovoitov 		const struct bpf_prog_stats *st;
1770492ecee8SAlexei Starovoitov 		unsigned int start;
1771492ecee8SAlexei Starovoitov 		u64 tnsecs, tcnt;
1772492ecee8SAlexei Starovoitov 
1773492ecee8SAlexei Starovoitov 		st = per_cpu_ptr(prog->aux->stats, cpu);
1774492ecee8SAlexei Starovoitov 		do {
1775492ecee8SAlexei Starovoitov 			start = u64_stats_fetch_begin_irq(&st->syncp);
1776492ecee8SAlexei Starovoitov 			tnsecs = st->nsecs;
1777492ecee8SAlexei Starovoitov 			tcnt = st->cnt;
1778492ecee8SAlexei Starovoitov 		} while (u64_stats_fetch_retry_irq(&st->syncp, start));
1779492ecee8SAlexei Starovoitov 		nsecs += tnsecs;
1780492ecee8SAlexei Starovoitov 		cnt += tcnt;
1781492ecee8SAlexei Starovoitov 	}
1782492ecee8SAlexei Starovoitov 	stats->nsecs = nsecs;
1783492ecee8SAlexei Starovoitov 	stats->cnt = cnt;
1784492ecee8SAlexei Starovoitov }
1785492ecee8SAlexei Starovoitov 
17867bd509e3SDaniel Borkmann #ifdef CONFIG_PROC_FS
17877bd509e3SDaniel Borkmann static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
17887bd509e3SDaniel Borkmann {
17897bd509e3SDaniel Borkmann 	const struct bpf_prog *prog = filp->private_data;
1790f1f7714eSDaniel Borkmann 	char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1791492ecee8SAlexei Starovoitov 	struct bpf_prog_stats stats;
17927bd509e3SDaniel Borkmann 
1793492ecee8SAlexei Starovoitov 	bpf_prog_get_stats(prog, &stats);
1794f1f7714eSDaniel Borkmann 	bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
17957bd509e3SDaniel Borkmann 	seq_printf(m,
17967bd509e3SDaniel Borkmann 		   "prog_type:\t%u\n"
17977bd509e3SDaniel Borkmann 		   "prog_jited:\t%u\n"
1798f1f7714eSDaniel Borkmann 		   "prog_tag:\t%s\n"
17994316b409SDaniel Borkmann 		   "memlock:\t%llu\n"
1800492ecee8SAlexei Starovoitov 		   "prog_id:\t%u\n"
1801492ecee8SAlexei Starovoitov 		   "run_time_ns:\t%llu\n"
1802492ecee8SAlexei Starovoitov 		   "run_cnt:\t%llu\n",
18037bd509e3SDaniel Borkmann 		   prog->type,
18047bd509e3SDaniel Borkmann 		   prog->jited,
1805f1f7714eSDaniel Borkmann 		   prog_tag,
18064316b409SDaniel Borkmann 		   prog->pages * 1ULL << PAGE_SHIFT,
1807492ecee8SAlexei Starovoitov 		   prog->aux->id,
1808492ecee8SAlexei Starovoitov 		   stats.nsecs,
1809492ecee8SAlexei Starovoitov 		   stats.cnt);
18107bd509e3SDaniel Borkmann }
18117bd509e3SDaniel Borkmann #endif
18127bd509e3SDaniel Borkmann 
1813f66e448cSChenbo Feng const struct file_operations bpf_prog_fops = {
18147bd509e3SDaniel Borkmann #ifdef CONFIG_PROC_FS
18157bd509e3SDaniel Borkmann 	.show_fdinfo	= bpf_prog_show_fdinfo,
18167bd509e3SDaniel Borkmann #endif
181709756af4SAlexei Starovoitov 	.release	= bpf_prog_release,
18186e71b04aSChenbo Feng 	.read		= bpf_dummy_read,
18196e71b04aSChenbo Feng 	.write		= bpf_dummy_write,
182009756af4SAlexei Starovoitov };
182109756af4SAlexei Starovoitov 
1822b2197755SDaniel Borkmann int bpf_prog_new_fd(struct bpf_prog *prog)
1823aa79781bSDaniel Borkmann {
1824afdb09c7SChenbo Feng 	int ret;
1825afdb09c7SChenbo Feng 
1826afdb09c7SChenbo Feng 	ret = security_bpf_prog(prog);
1827afdb09c7SChenbo Feng 	if (ret < 0)
1828afdb09c7SChenbo Feng 		return ret;
1829afdb09c7SChenbo Feng 
1830aa79781bSDaniel Borkmann 	return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1831aa79781bSDaniel Borkmann 				O_RDWR | O_CLOEXEC);
1832aa79781bSDaniel Borkmann }
1833aa79781bSDaniel Borkmann 
1834113214beSDaniel Borkmann static struct bpf_prog *____bpf_prog_get(struct fd f)
183509756af4SAlexei Starovoitov {
183609756af4SAlexei Starovoitov 	if (!f.file)
183709756af4SAlexei Starovoitov 		return ERR_PTR(-EBADF);
183809756af4SAlexei Starovoitov 	if (f.file->f_op != &bpf_prog_fops) {
183909756af4SAlexei Starovoitov 		fdput(f);
184009756af4SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
184109756af4SAlexei Starovoitov 	}
184209756af4SAlexei Starovoitov 
1843c2101297SDaniel Borkmann 	return f.file->private_data;
184409756af4SAlexei Starovoitov }
184509756af4SAlexei Starovoitov 
184685192dbfSAndrii Nakryiko void bpf_prog_add(struct bpf_prog *prog, int i)
184792117d84SAlexei Starovoitov {
184885192dbfSAndrii Nakryiko 	atomic64_add(i, &prog->aux->refcnt);
184992117d84SAlexei Starovoitov }
185059d3656dSBrenden Blanco EXPORT_SYMBOL_GPL(bpf_prog_add);
185159d3656dSBrenden Blanco 
1852c540594fSDaniel Borkmann void bpf_prog_sub(struct bpf_prog *prog, int i)
1853c540594fSDaniel Borkmann {
1854c540594fSDaniel Borkmann 	/* Only to be used for undoing previous bpf_prog_add() in some
1855c540594fSDaniel Borkmann 	 * error path. We still know that another entity in our call
1856c540594fSDaniel Borkmann 	 * path holds a reference to the program, thus atomic_sub() can
1857c540594fSDaniel Borkmann 	 * be safely used in such cases!
1858c540594fSDaniel Borkmann 	 */
185985192dbfSAndrii Nakryiko 	WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1860c540594fSDaniel Borkmann }
1861c540594fSDaniel Borkmann EXPORT_SYMBOL_GPL(bpf_prog_sub);
1862c540594fSDaniel Borkmann 
186385192dbfSAndrii Nakryiko void bpf_prog_inc(struct bpf_prog *prog)
186459d3656dSBrenden Blanco {
186585192dbfSAndrii Nakryiko 	atomic64_inc(&prog->aux->refcnt);
186659d3656dSBrenden Blanco }
186797bc402dSDaniel Borkmann EXPORT_SYMBOL_GPL(bpf_prog_inc);
186892117d84SAlexei Starovoitov 
1869b16d9aa4SMartin KaFai Lau /* prog_idr_lock should have been held */
1870a6f6df69SJohn Fastabend struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1871b16d9aa4SMartin KaFai Lau {
1872b16d9aa4SMartin KaFai Lau 	int refold;
1873b16d9aa4SMartin KaFai Lau 
187485192dbfSAndrii Nakryiko 	refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1875b16d9aa4SMartin KaFai Lau 
1876b16d9aa4SMartin KaFai Lau 	if (!refold)
1877b16d9aa4SMartin KaFai Lau 		return ERR_PTR(-ENOENT);
1878b16d9aa4SMartin KaFai Lau 
1879b16d9aa4SMartin KaFai Lau 	return prog;
1880b16d9aa4SMartin KaFai Lau }
1881a6f6df69SJohn Fastabend EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1882b16d9aa4SMartin KaFai Lau 
1883040ee692SAl Viro bool bpf_prog_get_ok(struct bpf_prog *prog,
1884288b3de5SJakub Kicinski 			    enum bpf_prog_type *attach_type, bool attach_drv)
1885248f346fSJakub Kicinski {
1886288b3de5SJakub Kicinski 	/* not an attachment, just a refcount inc, always allow */
1887288b3de5SJakub Kicinski 	if (!attach_type)
1888288b3de5SJakub Kicinski 		return true;
1889248f346fSJakub Kicinski 
1890248f346fSJakub Kicinski 	if (prog->type != *attach_type)
1891248f346fSJakub Kicinski 		return false;
1892288b3de5SJakub Kicinski 	if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1893248f346fSJakub Kicinski 		return false;
1894248f346fSJakub Kicinski 
1895248f346fSJakub Kicinski 	return true;
1896248f346fSJakub Kicinski }
1897248f346fSJakub Kicinski 
1898248f346fSJakub Kicinski static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1899288b3de5SJakub Kicinski 				       bool attach_drv)
190009756af4SAlexei Starovoitov {
190109756af4SAlexei Starovoitov 	struct fd f = fdget(ufd);
190209756af4SAlexei Starovoitov 	struct bpf_prog *prog;
190309756af4SAlexei Starovoitov 
1904113214beSDaniel Borkmann 	prog = ____bpf_prog_get(f);
190509756af4SAlexei Starovoitov 	if (IS_ERR(prog))
190609756af4SAlexei Starovoitov 		return prog;
1907288b3de5SJakub Kicinski 	if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1908113214beSDaniel Borkmann 		prog = ERR_PTR(-EINVAL);
1909113214beSDaniel Borkmann 		goto out;
1910113214beSDaniel Borkmann 	}
191109756af4SAlexei Starovoitov 
191285192dbfSAndrii Nakryiko 	bpf_prog_inc(prog);
1913113214beSDaniel Borkmann out:
191409756af4SAlexei Starovoitov 	fdput(f);
191509756af4SAlexei Starovoitov 	return prog;
191609756af4SAlexei Starovoitov }
1917113214beSDaniel Borkmann 
1918113214beSDaniel Borkmann struct bpf_prog *bpf_prog_get(u32 ufd)
1919113214beSDaniel Borkmann {
1920288b3de5SJakub Kicinski 	return __bpf_prog_get(ufd, NULL, false);
1921113214beSDaniel Borkmann }
1922113214beSDaniel Borkmann 
1923248f346fSJakub Kicinski struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1924288b3de5SJakub Kicinski 				       bool attach_drv)
1925248f346fSJakub Kicinski {
19264d220ed0SAlexei Starovoitov 	return __bpf_prog_get(ufd, &type, attach_drv);
1927248f346fSJakub Kicinski }
19286c8dfe21SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1929248f346fSJakub Kicinski 
1930aac3fc32SAndrey Ignatov /* Initially all BPF programs could be loaded w/o specifying
1931aac3fc32SAndrey Ignatov  * expected_attach_type. Later for some of them specifying expected_attach_type
1932aac3fc32SAndrey Ignatov  * at load time became required so that program could be validated properly.
1933aac3fc32SAndrey Ignatov  * Programs of types that are allowed to be loaded both w/ and w/o (for
1934aac3fc32SAndrey Ignatov  * backward compatibility) expected_attach_type, should have the default attach
1935aac3fc32SAndrey Ignatov  * type assigned to expected_attach_type for the latter case, so that it can be
1936aac3fc32SAndrey Ignatov  * validated later at attach time.
1937aac3fc32SAndrey Ignatov  *
1938aac3fc32SAndrey Ignatov  * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1939aac3fc32SAndrey Ignatov  * prog type requires it but has some attach types that have to be backward
1940aac3fc32SAndrey Ignatov  * compatible.
1941aac3fc32SAndrey Ignatov  */
1942aac3fc32SAndrey Ignatov static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1943aac3fc32SAndrey Ignatov {
1944aac3fc32SAndrey Ignatov 	switch (attr->prog_type) {
1945aac3fc32SAndrey Ignatov 	case BPF_PROG_TYPE_CGROUP_SOCK:
1946aac3fc32SAndrey Ignatov 		/* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1947aac3fc32SAndrey Ignatov 		 * exist so checking for non-zero is the way to go here.
1948aac3fc32SAndrey Ignatov 		 */
1949aac3fc32SAndrey Ignatov 		if (!attr->expected_attach_type)
1950aac3fc32SAndrey Ignatov 			attr->expected_attach_type =
1951aac3fc32SAndrey Ignatov 				BPF_CGROUP_INET_SOCK_CREATE;
1952aac3fc32SAndrey Ignatov 		break;
1953aac3fc32SAndrey Ignatov 	}
1954aac3fc32SAndrey Ignatov }
1955aac3fc32SAndrey Ignatov 
19565e43f899SAndrey Ignatov static int
1957ccfe29ebSAlexei Starovoitov bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1958ccfe29ebSAlexei Starovoitov 			   enum bpf_attach_type expected_attach_type,
19595b92a28aSAlexei Starovoitov 			   u32 btf_id, u32 prog_fd)
19605e43f899SAndrey Ignatov {
196127ae7997SMartin KaFai Lau 	if (btf_id) {
1962c108e3c1SAlexei Starovoitov 		if (btf_id > BTF_MAX_TYPE)
1963c108e3c1SAlexei Starovoitov 			return -EINVAL;
196427ae7997SMartin KaFai Lau 
196527ae7997SMartin KaFai Lau 		switch (prog_type) {
196627ae7997SMartin KaFai Lau 		case BPF_PROG_TYPE_TRACING:
19679e4e01dfSKP Singh 		case BPF_PROG_TYPE_LSM:
196827ae7997SMartin KaFai Lau 		case BPF_PROG_TYPE_STRUCT_OPS:
1969be8704ffSAlexei Starovoitov 		case BPF_PROG_TYPE_EXT:
1970c108e3c1SAlexei Starovoitov 			break;
1971c108e3c1SAlexei Starovoitov 		default:
1972c108e3c1SAlexei Starovoitov 			return -EINVAL;
1973c108e3c1SAlexei Starovoitov 		}
197427ae7997SMartin KaFai Lau 	}
197527ae7997SMartin KaFai Lau 
1976be8704ffSAlexei Starovoitov 	if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING &&
1977be8704ffSAlexei Starovoitov 	    prog_type != BPF_PROG_TYPE_EXT)
197827ae7997SMartin KaFai Lau 		return -EINVAL;
1979c108e3c1SAlexei Starovoitov 
1980c108e3c1SAlexei Starovoitov 	switch (prog_type) {
1981aac3fc32SAndrey Ignatov 	case BPF_PROG_TYPE_CGROUP_SOCK:
1982aac3fc32SAndrey Ignatov 		switch (expected_attach_type) {
1983aac3fc32SAndrey Ignatov 		case BPF_CGROUP_INET_SOCK_CREATE:
1984aac3fc32SAndrey Ignatov 		case BPF_CGROUP_INET4_POST_BIND:
1985aac3fc32SAndrey Ignatov 		case BPF_CGROUP_INET6_POST_BIND:
1986aac3fc32SAndrey Ignatov 			return 0;
1987aac3fc32SAndrey Ignatov 		default:
1988aac3fc32SAndrey Ignatov 			return -EINVAL;
1989aac3fc32SAndrey Ignatov 		}
19904fbac77dSAndrey Ignatov 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
19914fbac77dSAndrey Ignatov 		switch (expected_attach_type) {
19924fbac77dSAndrey Ignatov 		case BPF_CGROUP_INET4_BIND:
19934fbac77dSAndrey Ignatov 		case BPF_CGROUP_INET6_BIND:
1994d74bad4eSAndrey Ignatov 		case BPF_CGROUP_INET4_CONNECT:
1995d74bad4eSAndrey Ignatov 		case BPF_CGROUP_INET6_CONNECT:
19961b66d253SDaniel Borkmann 		case BPF_CGROUP_INET4_GETPEERNAME:
19971b66d253SDaniel Borkmann 		case BPF_CGROUP_INET6_GETPEERNAME:
19981b66d253SDaniel Borkmann 		case BPF_CGROUP_INET4_GETSOCKNAME:
19991b66d253SDaniel Borkmann 		case BPF_CGROUP_INET6_GETSOCKNAME:
20001cedee13SAndrey Ignatov 		case BPF_CGROUP_UDP4_SENDMSG:
20011cedee13SAndrey Ignatov 		case BPF_CGROUP_UDP6_SENDMSG:
2002983695faSDaniel Borkmann 		case BPF_CGROUP_UDP4_RECVMSG:
2003983695faSDaniel Borkmann 		case BPF_CGROUP_UDP6_RECVMSG:
20045e43f899SAndrey Ignatov 			return 0;
20054fbac77dSAndrey Ignatov 		default:
20064fbac77dSAndrey Ignatov 			return -EINVAL;
20074fbac77dSAndrey Ignatov 		}
20085cf1e914Sbrakmo 	case BPF_PROG_TYPE_CGROUP_SKB:
20095cf1e914Sbrakmo 		switch (expected_attach_type) {
20105cf1e914Sbrakmo 		case BPF_CGROUP_INET_INGRESS:
20115cf1e914Sbrakmo 		case BPF_CGROUP_INET_EGRESS:
20125cf1e914Sbrakmo 			return 0;
20135cf1e914Sbrakmo 		default:
20145cf1e914Sbrakmo 			return -EINVAL;
20155cf1e914Sbrakmo 		}
20160d01da6aSStanislav Fomichev 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
20170d01da6aSStanislav Fomichev 		switch (expected_attach_type) {
20180d01da6aSStanislav Fomichev 		case BPF_CGROUP_SETSOCKOPT:
20190d01da6aSStanislav Fomichev 		case BPF_CGROUP_GETSOCKOPT:
20200d01da6aSStanislav Fomichev 			return 0;
20210d01da6aSStanislav Fomichev 		default:
20220d01da6aSStanislav Fomichev 			return -EINVAL;
20230d01da6aSStanislav Fomichev 		}
2024be8704ffSAlexei Starovoitov 	case BPF_PROG_TYPE_EXT:
2025be8704ffSAlexei Starovoitov 		if (expected_attach_type)
2026be8704ffSAlexei Starovoitov 			return -EINVAL;
2027be8704ffSAlexei Starovoitov 		/* fallthrough */
20284fbac77dSAndrey Ignatov 	default:
20294fbac77dSAndrey Ignatov 		return 0;
20304fbac77dSAndrey Ignatov 	}
20315e43f899SAndrey Ignatov }
20325e43f899SAndrey Ignatov 
20332c78ee89SAlexei Starovoitov static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
20342c78ee89SAlexei Starovoitov {
20352c78ee89SAlexei Starovoitov 	switch (prog_type) {
20362c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_SCHED_CLS:
20372c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_SCHED_ACT:
20382c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_XDP:
20392c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_IN:
20402c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_OUT:
20412c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_XMIT:
20422c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_SEG6LOCAL:
20432c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_SK_SKB:
20442c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_SK_MSG:
20452c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_LIRC_MODE2:
20462c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
20472c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_CGROUP_DEVICE:
20482c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_CGROUP_SOCK:
20492c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
20502c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
20512c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
20522c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_SOCK_OPS:
20532c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_EXT: /* extends any prog */
20542c78ee89SAlexei Starovoitov 		return true;
20552c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_CGROUP_SKB:
20562c78ee89SAlexei Starovoitov 		/* always unpriv */
20572c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_SK_REUSEPORT:
20582c78ee89SAlexei Starovoitov 		/* equivalent to SOCKET_FILTER. need CAP_BPF only */
20592c78ee89SAlexei Starovoitov 	default:
20602c78ee89SAlexei Starovoitov 		return false;
20612c78ee89SAlexei Starovoitov 	}
20622c78ee89SAlexei Starovoitov }
20632c78ee89SAlexei Starovoitov 
20642c78ee89SAlexei Starovoitov static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
20652c78ee89SAlexei Starovoitov {
20662c78ee89SAlexei Starovoitov 	switch (prog_type) {
20672c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_KPROBE:
20682c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_TRACEPOINT:
20692c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_PERF_EVENT:
20702c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_RAW_TRACEPOINT:
20712c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
20722c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_TRACING:
20732c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_LSM:
20742c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
20752c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_EXT: /* extends any prog */
20762c78ee89SAlexei Starovoitov 		return true;
20772c78ee89SAlexei Starovoitov 	default:
20782c78ee89SAlexei Starovoitov 		return false;
20792c78ee89SAlexei Starovoitov 	}
20802c78ee89SAlexei Starovoitov }
20812c78ee89SAlexei Starovoitov 
208209756af4SAlexei Starovoitov /* last field in 'union bpf_attr' used by this command */
20835b92a28aSAlexei Starovoitov #define	BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
208409756af4SAlexei Starovoitov 
2085838e9690SYonghong Song static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
208609756af4SAlexei Starovoitov {
208709756af4SAlexei Starovoitov 	enum bpf_prog_type type = attr->prog_type;
208809756af4SAlexei Starovoitov 	struct bpf_prog *prog;
208909756af4SAlexei Starovoitov 	int err;
209009756af4SAlexei Starovoitov 	char license[128];
209109756af4SAlexei Starovoitov 	bool is_gpl;
209209756af4SAlexei Starovoitov 
209309756af4SAlexei Starovoitov 	if (CHECK_ATTR(BPF_PROG_LOAD))
209409756af4SAlexei Starovoitov 		return -EINVAL;
209509756af4SAlexei Starovoitov 
2096c240eff6SJiong Wang 	if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2097c240eff6SJiong Wang 				 BPF_F_ANY_ALIGNMENT |
209810d274e8SAlexei Starovoitov 				 BPF_F_TEST_STATE_FREQ |
2099c240eff6SJiong Wang 				 BPF_F_TEST_RND_HI32))
2100e07b98d9SDavid S. Miller 		return -EINVAL;
2101e07b98d9SDavid S. Miller 
2102e9ee9efcSDavid Miller 	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2103e9ee9efcSDavid Miller 	    (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
21042c78ee89SAlexei Starovoitov 	    !bpf_capable())
2105e9ee9efcSDavid Miller 		return -EPERM;
2106e9ee9efcSDavid Miller 
210709756af4SAlexei Starovoitov 	/* copy eBPF program license from user space */
2108535e7b4bSMickaël Salaün 	if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
210909756af4SAlexei Starovoitov 			      sizeof(license) - 1) < 0)
211009756af4SAlexei Starovoitov 		return -EFAULT;
211109756af4SAlexei Starovoitov 	license[sizeof(license) - 1] = 0;
211209756af4SAlexei Starovoitov 
211309756af4SAlexei Starovoitov 	/* eBPF programs must be GPL compatible to use GPL-ed functions */
211409756af4SAlexei Starovoitov 	is_gpl = license_is_gpl_compatible(license);
211509756af4SAlexei Starovoitov 
2116c04c0d2bSAlexei Starovoitov 	if (attr->insn_cnt == 0 ||
21172c78ee89SAlexei Starovoitov 	    attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2118ef0915caSDaniel Borkmann 		return -E2BIG;
211980b7d819SChenbo Feng 	if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
212080b7d819SChenbo Feng 	    type != BPF_PROG_TYPE_CGROUP_SKB &&
21212c78ee89SAlexei Starovoitov 	    !bpf_capable())
21222c78ee89SAlexei Starovoitov 		return -EPERM;
21232c78ee89SAlexei Starovoitov 
2124*b338cb92SMaciej Żenczykowski 	if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
21252c78ee89SAlexei Starovoitov 		return -EPERM;
21262c78ee89SAlexei Starovoitov 	if (is_perfmon_prog_type(type) && !perfmon_capable())
21271be7f75dSAlexei Starovoitov 		return -EPERM;
21281be7f75dSAlexei Starovoitov 
2129aac3fc32SAndrey Ignatov 	bpf_prog_load_fixup_attach_type(attr);
2130ccfe29ebSAlexei Starovoitov 	if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
21315b92a28aSAlexei Starovoitov 				       attr->attach_btf_id,
21325b92a28aSAlexei Starovoitov 				       attr->attach_prog_fd))
21335e43f899SAndrey Ignatov 		return -EINVAL;
21345e43f899SAndrey Ignatov 
213509756af4SAlexei Starovoitov 	/* plain bpf_prog allocation */
213609756af4SAlexei Starovoitov 	prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
213709756af4SAlexei Starovoitov 	if (!prog)
213809756af4SAlexei Starovoitov 		return -ENOMEM;
213909756af4SAlexei Starovoitov 
21405e43f899SAndrey Ignatov 	prog->expected_attach_type = attr->expected_attach_type;
2141ccfe29ebSAlexei Starovoitov 	prog->aux->attach_btf_id = attr->attach_btf_id;
21425b92a28aSAlexei Starovoitov 	if (attr->attach_prog_fd) {
21435b92a28aSAlexei Starovoitov 		struct bpf_prog *tgt_prog;
21445b92a28aSAlexei Starovoitov 
21455b92a28aSAlexei Starovoitov 		tgt_prog = bpf_prog_get(attr->attach_prog_fd);
21465b92a28aSAlexei Starovoitov 		if (IS_ERR(tgt_prog)) {
21475b92a28aSAlexei Starovoitov 			err = PTR_ERR(tgt_prog);
21485b92a28aSAlexei Starovoitov 			goto free_prog_nouncharge;
21495b92a28aSAlexei Starovoitov 		}
21505b92a28aSAlexei Starovoitov 		prog->aux->linked_prog = tgt_prog;
21515b92a28aSAlexei Starovoitov 	}
21525e43f899SAndrey Ignatov 
21539a18eedbSJakub Kicinski 	prog->aux->offload_requested = !!attr->prog_ifindex;
21549a18eedbSJakub Kicinski 
2155afdb09c7SChenbo Feng 	err = security_bpf_prog_alloc(prog->aux);
2156aaac3ba9SAlexei Starovoitov 	if (err)
2157aaac3ba9SAlexei Starovoitov 		goto free_prog_nouncharge;
2158aaac3ba9SAlexei Starovoitov 
2159afdb09c7SChenbo Feng 	err = bpf_prog_charge_memlock(prog);
2160afdb09c7SChenbo Feng 	if (err)
2161afdb09c7SChenbo Feng 		goto free_prog_sec;
2162afdb09c7SChenbo Feng 
216309756af4SAlexei Starovoitov 	prog->len = attr->insn_cnt;
216409756af4SAlexei Starovoitov 
216509756af4SAlexei Starovoitov 	err = -EFAULT;
2166535e7b4bSMickaël Salaün 	if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
2167aafe6ae9SDaniel Borkmann 			   bpf_prog_insn_size(prog)) != 0)
216809756af4SAlexei Starovoitov 		goto free_prog;
216909756af4SAlexei Starovoitov 
217009756af4SAlexei Starovoitov 	prog->orig_prog = NULL;
2171a91263d5SDaniel Borkmann 	prog->jited = 0;
217209756af4SAlexei Starovoitov 
217385192dbfSAndrii Nakryiko 	atomic64_set(&prog->aux->refcnt, 1);
2174a91263d5SDaniel Borkmann 	prog->gpl_compatible = is_gpl ? 1 : 0;
217509756af4SAlexei Starovoitov 
21769a18eedbSJakub Kicinski 	if (bpf_prog_is_dev_bound(prog->aux)) {
2177ab3f0063SJakub Kicinski 		err = bpf_prog_offload_init(prog, attr);
2178ab3f0063SJakub Kicinski 		if (err)
2179ab3f0063SJakub Kicinski 			goto free_prog;
2180ab3f0063SJakub Kicinski 	}
2181ab3f0063SJakub Kicinski 
218209756af4SAlexei Starovoitov 	/* find program type: socket_filter vs tracing_filter */
218309756af4SAlexei Starovoitov 	err = find_prog_type(type, prog);
218409756af4SAlexei Starovoitov 	if (err < 0)
218509756af4SAlexei Starovoitov 		goto free_prog;
218609756af4SAlexei Starovoitov 
21879285ec4cSJason A. Donenfeld 	prog->aux->load_time = ktime_get_boottime_ns();
21888e7ae251SMartin KaFai Lau 	err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
21898e7ae251SMartin KaFai Lau 			       sizeof(attr->prog_name));
21908e7ae251SMartin KaFai Lau 	if (err < 0)
2191cb4d2b3fSMartin KaFai Lau 		goto free_prog;
2192cb4d2b3fSMartin KaFai Lau 
219309756af4SAlexei Starovoitov 	/* run eBPF verifier */
2194838e9690SYonghong Song 	err = bpf_check(&prog, attr, uattr);
219509756af4SAlexei Starovoitov 	if (err < 0)
219609756af4SAlexei Starovoitov 		goto free_used_maps;
219709756af4SAlexei Starovoitov 
2198d1c55ab5SDaniel Borkmann 	prog = bpf_prog_select_runtime(prog, &err);
219904fd61abSAlexei Starovoitov 	if (err < 0)
220004fd61abSAlexei Starovoitov 		goto free_used_maps;
220109756af4SAlexei Starovoitov 
2202dc4bb0e2SMartin KaFai Lau 	err = bpf_prog_alloc_id(prog);
2203dc4bb0e2SMartin KaFai Lau 	if (err)
2204dc4bb0e2SMartin KaFai Lau 		goto free_used_maps;
2205dc4bb0e2SMartin KaFai Lau 
2206c751798aSDaniel Borkmann 	/* Upon success of bpf_prog_alloc_id(), the BPF prog is
2207c751798aSDaniel Borkmann 	 * effectively publicly exposed. However, retrieving via
2208c751798aSDaniel Borkmann 	 * bpf_prog_get_fd_by_id() will take another reference,
2209c751798aSDaniel Borkmann 	 * therefore it cannot be gone underneath us.
2210c751798aSDaniel Borkmann 	 *
2211c751798aSDaniel Borkmann 	 * Only for the time /after/ successful bpf_prog_new_fd()
2212c751798aSDaniel Borkmann 	 * and before returning to userspace, we might just hold
2213c751798aSDaniel Borkmann 	 * one reference and any parallel close on that fd could
2214c751798aSDaniel Borkmann 	 * rip everything out. Hence, below notifications must
2215c751798aSDaniel Borkmann 	 * happen before bpf_prog_new_fd().
2216c751798aSDaniel Borkmann 	 *
2217c751798aSDaniel Borkmann 	 * Also, any failure handling from this point onwards must
2218c751798aSDaniel Borkmann 	 * be using bpf_prog_put() given the program is exposed.
2219b16d9aa4SMartin KaFai Lau 	 */
222074451e66SDaniel Borkmann 	bpf_prog_kallsyms_add(prog);
22216ee52e2aSSong Liu 	perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2222bae141f5SDaniel Borkmann 	bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2223c751798aSDaniel Borkmann 
2224c751798aSDaniel Borkmann 	err = bpf_prog_new_fd(prog);
2225c751798aSDaniel Borkmann 	if (err < 0)
2226c751798aSDaniel Borkmann 		bpf_prog_put(prog);
222709756af4SAlexei Starovoitov 	return err;
222809756af4SAlexei Starovoitov 
222909756af4SAlexei Starovoitov free_used_maps:
2230cd7455f1SDaniel Borkmann 	/* In case we have subprogs, we need to wait for a grace
2231cd7455f1SDaniel Borkmann 	 * period before we can tear down JIT memory since symbols
2232cd7455f1SDaniel Borkmann 	 * are already exposed under kallsyms.
2233cd7455f1SDaniel Borkmann 	 */
2234cd7455f1SDaniel Borkmann 	__bpf_prog_put_noref(prog, prog->aux->func_cnt);
2235cd7455f1SDaniel Borkmann 	return err;
223609756af4SAlexei Starovoitov free_prog:
2237aaac3ba9SAlexei Starovoitov 	bpf_prog_uncharge_memlock(prog);
2238afdb09c7SChenbo Feng free_prog_sec:
2239afdb09c7SChenbo Feng 	security_bpf_prog_free(prog->aux);
2240aaac3ba9SAlexei Starovoitov free_prog_nouncharge:
224109756af4SAlexei Starovoitov 	bpf_prog_free(prog);
224209756af4SAlexei Starovoitov 	return err;
224309756af4SAlexei Starovoitov }
224409756af4SAlexei Starovoitov 
22456e71b04aSChenbo Feng #define BPF_OBJ_LAST_FIELD file_flags
2246b2197755SDaniel Borkmann 
2247b2197755SDaniel Borkmann static int bpf_obj_pin(const union bpf_attr *attr)
2248b2197755SDaniel Borkmann {
22496e71b04aSChenbo Feng 	if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2250b2197755SDaniel Borkmann 		return -EINVAL;
2251b2197755SDaniel Borkmann 
2252535e7b4bSMickaël Salaün 	return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2253b2197755SDaniel Borkmann }
2254b2197755SDaniel Borkmann 
2255b2197755SDaniel Borkmann static int bpf_obj_get(const union bpf_attr *attr)
2256b2197755SDaniel Borkmann {
22576e71b04aSChenbo Feng 	if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
22586e71b04aSChenbo Feng 	    attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2259b2197755SDaniel Borkmann 		return -EINVAL;
2260b2197755SDaniel Borkmann 
22616e71b04aSChenbo Feng 	return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
22626e71b04aSChenbo Feng 				attr->file_flags);
2263b2197755SDaniel Borkmann }
2264b2197755SDaniel Borkmann 
2265f2e10bffSAndrii Nakryiko void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2266a3b80e10SAndrii Nakryiko 		   const struct bpf_link_ops *ops, struct bpf_prog *prog)
226770ed506cSAndrii Nakryiko {
226870ed506cSAndrii Nakryiko 	atomic64_set(&link->refcnt, 1);
2269f2e10bffSAndrii Nakryiko 	link->type = type;
2270a3b80e10SAndrii Nakryiko 	link->id = 0;
227170ed506cSAndrii Nakryiko 	link->ops = ops;
227270ed506cSAndrii Nakryiko 	link->prog = prog;
227370ed506cSAndrii Nakryiko }
227470ed506cSAndrii Nakryiko 
2275a3b80e10SAndrii Nakryiko static void bpf_link_free_id(int id)
2276a3b80e10SAndrii Nakryiko {
2277a3b80e10SAndrii Nakryiko 	if (!id)
2278a3b80e10SAndrii Nakryiko 		return;
2279a3b80e10SAndrii Nakryiko 
2280a3b80e10SAndrii Nakryiko 	spin_lock_bh(&link_idr_lock);
2281a3b80e10SAndrii Nakryiko 	idr_remove(&link_idr, id);
2282a3b80e10SAndrii Nakryiko 	spin_unlock_bh(&link_idr_lock);
2283a3b80e10SAndrii Nakryiko }
2284a3b80e10SAndrii Nakryiko 
228598868668SAndrii Nakryiko /* Clean up bpf_link and corresponding anon_inode file and FD. After
228698868668SAndrii Nakryiko  * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2287a3b80e10SAndrii Nakryiko  * anon_inode's release() call. This helper marksbpf_link as
2288a3b80e10SAndrii Nakryiko  * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2289a3b80e10SAndrii Nakryiko  * is not decremented, it's the responsibility of a calling code that failed
2290a3b80e10SAndrii Nakryiko  * to complete bpf_link initialization.
229198868668SAndrii Nakryiko  */
2292a3b80e10SAndrii Nakryiko void bpf_link_cleanup(struct bpf_link_primer *primer)
2293babf3164SAndrii Nakryiko {
2294a3b80e10SAndrii Nakryiko 	primer->link->prog = NULL;
2295a3b80e10SAndrii Nakryiko 	bpf_link_free_id(primer->id);
2296a3b80e10SAndrii Nakryiko 	fput(primer->file);
2297a3b80e10SAndrii Nakryiko 	put_unused_fd(primer->fd);
2298babf3164SAndrii Nakryiko }
2299babf3164SAndrii Nakryiko 
230070ed506cSAndrii Nakryiko void bpf_link_inc(struct bpf_link *link)
230170ed506cSAndrii Nakryiko {
230270ed506cSAndrii Nakryiko 	atomic64_inc(&link->refcnt);
230370ed506cSAndrii Nakryiko }
230470ed506cSAndrii Nakryiko 
230570ed506cSAndrii Nakryiko /* bpf_link_free is guaranteed to be called from process context */
230670ed506cSAndrii Nakryiko static void bpf_link_free(struct bpf_link *link)
230770ed506cSAndrii Nakryiko {
2308a3b80e10SAndrii Nakryiko 	bpf_link_free_id(link->id);
2309babf3164SAndrii Nakryiko 	if (link->prog) {
2310babf3164SAndrii Nakryiko 		/* detach BPF program, clean up used resources */
231170ed506cSAndrii Nakryiko 		link->ops->release(link);
2312babf3164SAndrii Nakryiko 		bpf_prog_put(link->prog);
2313babf3164SAndrii Nakryiko 	}
2314babf3164SAndrii Nakryiko 	/* free bpf_link and its containing memory */
2315babf3164SAndrii Nakryiko 	link->ops->dealloc(link);
231670ed506cSAndrii Nakryiko }
231770ed506cSAndrii Nakryiko 
231870ed506cSAndrii Nakryiko static void bpf_link_put_deferred(struct work_struct *work)
231970ed506cSAndrii Nakryiko {
232070ed506cSAndrii Nakryiko 	struct bpf_link *link = container_of(work, struct bpf_link, work);
232170ed506cSAndrii Nakryiko 
232270ed506cSAndrii Nakryiko 	bpf_link_free(link);
232370ed506cSAndrii Nakryiko }
232470ed506cSAndrii Nakryiko 
232570ed506cSAndrii Nakryiko /* bpf_link_put can be called from atomic context, but ensures that resources
232670ed506cSAndrii Nakryiko  * are freed from process context
232770ed506cSAndrii Nakryiko  */
232870ed506cSAndrii Nakryiko void bpf_link_put(struct bpf_link *link)
232970ed506cSAndrii Nakryiko {
233070ed506cSAndrii Nakryiko 	if (!atomic64_dec_and_test(&link->refcnt))
233170ed506cSAndrii Nakryiko 		return;
233270ed506cSAndrii Nakryiko 
233370ed506cSAndrii Nakryiko 	if (in_atomic()) {
233470ed506cSAndrii Nakryiko 		INIT_WORK(&link->work, bpf_link_put_deferred);
233570ed506cSAndrii Nakryiko 		schedule_work(&link->work);
233670ed506cSAndrii Nakryiko 	} else {
233770ed506cSAndrii Nakryiko 		bpf_link_free(link);
233870ed506cSAndrii Nakryiko 	}
233970ed506cSAndrii Nakryiko }
234070ed506cSAndrii Nakryiko 
234170ed506cSAndrii Nakryiko static int bpf_link_release(struct inode *inode, struct file *filp)
234270ed506cSAndrii Nakryiko {
234370ed506cSAndrii Nakryiko 	struct bpf_link *link = filp->private_data;
234470ed506cSAndrii Nakryiko 
234570ed506cSAndrii Nakryiko 	bpf_link_put(link);
2346fec56f58SAlexei Starovoitov 	return 0;
2347fec56f58SAlexei Starovoitov }
2348fec56f58SAlexei Starovoitov 
234970ed506cSAndrii Nakryiko #ifdef CONFIG_PROC_FS
2350f2e10bffSAndrii Nakryiko #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2351f2e10bffSAndrii Nakryiko #define BPF_MAP_TYPE(_id, _ops)
2352f2e10bffSAndrii Nakryiko #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2353f2e10bffSAndrii Nakryiko static const char *bpf_link_type_strs[] = {
2354f2e10bffSAndrii Nakryiko 	[BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2355f2e10bffSAndrii Nakryiko #include <linux/bpf_types.h>
2356f2e10bffSAndrii Nakryiko };
2357f2e10bffSAndrii Nakryiko #undef BPF_PROG_TYPE
2358f2e10bffSAndrii Nakryiko #undef BPF_MAP_TYPE
2359f2e10bffSAndrii Nakryiko #undef BPF_LINK_TYPE
236070ed506cSAndrii Nakryiko 
236170ed506cSAndrii Nakryiko static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
236270ed506cSAndrii Nakryiko {
236370ed506cSAndrii Nakryiko 	const struct bpf_link *link = filp->private_data;
236470ed506cSAndrii Nakryiko 	const struct bpf_prog *prog = link->prog;
236570ed506cSAndrii Nakryiko 	char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
236670ed506cSAndrii Nakryiko 
236770ed506cSAndrii Nakryiko 	bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
236870ed506cSAndrii Nakryiko 	seq_printf(m,
236970ed506cSAndrii Nakryiko 		   "link_type:\t%s\n"
2370a3b80e10SAndrii Nakryiko 		   "link_id:\t%u\n"
237170ed506cSAndrii Nakryiko 		   "prog_tag:\t%s\n"
237270ed506cSAndrii Nakryiko 		   "prog_id:\t%u\n",
2373f2e10bffSAndrii Nakryiko 		   bpf_link_type_strs[link->type],
2374a3b80e10SAndrii Nakryiko 		   link->id,
237570ed506cSAndrii Nakryiko 		   prog_tag,
237670ed506cSAndrii Nakryiko 		   prog->aux->id);
2377f2e10bffSAndrii Nakryiko 	if (link->ops->show_fdinfo)
2378f2e10bffSAndrii Nakryiko 		link->ops->show_fdinfo(link, m);
237970ed506cSAndrii Nakryiko }
238070ed506cSAndrii Nakryiko #endif
238170ed506cSAndrii Nakryiko 
23826f302bfbSZou Wei static const struct file_operations bpf_link_fops = {
238370ed506cSAndrii Nakryiko #ifdef CONFIG_PROC_FS
238470ed506cSAndrii Nakryiko 	.show_fdinfo	= bpf_link_show_fdinfo,
238570ed506cSAndrii Nakryiko #endif
238670ed506cSAndrii Nakryiko 	.release	= bpf_link_release,
2387fec56f58SAlexei Starovoitov 	.read		= bpf_dummy_read,
2388fec56f58SAlexei Starovoitov 	.write		= bpf_dummy_write,
2389fec56f58SAlexei Starovoitov };
2390fec56f58SAlexei Starovoitov 
2391a3b80e10SAndrii Nakryiko static int bpf_link_alloc_id(struct bpf_link *link)
239270ed506cSAndrii Nakryiko {
2393a3b80e10SAndrii Nakryiko 	int id;
2394a3b80e10SAndrii Nakryiko 
2395a3b80e10SAndrii Nakryiko 	idr_preload(GFP_KERNEL);
2396a3b80e10SAndrii Nakryiko 	spin_lock_bh(&link_idr_lock);
2397a3b80e10SAndrii Nakryiko 	id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2398a3b80e10SAndrii Nakryiko 	spin_unlock_bh(&link_idr_lock);
2399a3b80e10SAndrii Nakryiko 	idr_preload_end();
2400a3b80e10SAndrii Nakryiko 
2401a3b80e10SAndrii Nakryiko 	return id;
240270ed506cSAndrii Nakryiko }
240370ed506cSAndrii Nakryiko 
2404a3b80e10SAndrii Nakryiko /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2405a3b80e10SAndrii Nakryiko  * reserving unused FD and allocating ID from link_idr. This is to be paired
2406a3b80e10SAndrii Nakryiko  * with bpf_link_settle() to install FD and ID and expose bpf_link to
2407a3b80e10SAndrii Nakryiko  * user-space, if bpf_link is successfully attached. If not, bpf_link and
2408a3b80e10SAndrii Nakryiko  * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2409a3b80e10SAndrii Nakryiko  * transient state is passed around in struct bpf_link_primer.
2410a3b80e10SAndrii Nakryiko  * This is preferred way to create and initialize bpf_link, especially when
2411a3b80e10SAndrii Nakryiko  * there are complicated and expensive operations inbetween creating bpf_link
2412a3b80e10SAndrii Nakryiko  * itself and attaching it to BPF hook. By using bpf_link_prime() and
2413a3b80e10SAndrii Nakryiko  * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2414a3b80e10SAndrii Nakryiko  * expensive (and potentially failing) roll back operations in a rare case
2415a3b80e10SAndrii Nakryiko  * that file, FD, or ID can't be allocated.
2416babf3164SAndrii Nakryiko  */
2417a3b80e10SAndrii Nakryiko int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2418babf3164SAndrii Nakryiko {
2419babf3164SAndrii Nakryiko 	struct file *file;
2420a3b80e10SAndrii Nakryiko 	int fd, id;
2421babf3164SAndrii Nakryiko 
2422babf3164SAndrii Nakryiko 	fd = get_unused_fd_flags(O_CLOEXEC);
2423babf3164SAndrii Nakryiko 	if (fd < 0)
2424a3b80e10SAndrii Nakryiko 		return fd;
2425babf3164SAndrii Nakryiko 
2426babf3164SAndrii Nakryiko 
2427a3b80e10SAndrii Nakryiko 	id = bpf_link_alloc_id(link);
2428a3b80e10SAndrii Nakryiko 	if (id < 0) {
2429a3b80e10SAndrii Nakryiko 		put_unused_fd(fd);
2430a3b80e10SAndrii Nakryiko 		return id;
2431a3b80e10SAndrii Nakryiko 	}
2432babf3164SAndrii Nakryiko 
2433babf3164SAndrii Nakryiko 	file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2434babf3164SAndrii Nakryiko 	if (IS_ERR(file)) {
2435138c6767SAndrii Nakryiko 		bpf_link_free_id(id);
2436babf3164SAndrii Nakryiko 		put_unused_fd(fd);
2437138c6767SAndrii Nakryiko 		return PTR_ERR(file);
2438babf3164SAndrii Nakryiko 	}
2439babf3164SAndrii Nakryiko 
2440a3b80e10SAndrii Nakryiko 	primer->link = link;
2441a3b80e10SAndrii Nakryiko 	primer->file = file;
2442a3b80e10SAndrii Nakryiko 	primer->fd = fd;
2443a3b80e10SAndrii Nakryiko 	primer->id = id;
2444a3b80e10SAndrii Nakryiko 	return 0;
2445a3b80e10SAndrii Nakryiko }
2446a3b80e10SAndrii Nakryiko 
2447a3b80e10SAndrii Nakryiko int bpf_link_settle(struct bpf_link_primer *primer)
2448a3b80e10SAndrii Nakryiko {
2449a3b80e10SAndrii Nakryiko 	/* make bpf_link fetchable by ID */
2450a3b80e10SAndrii Nakryiko 	spin_lock_bh(&link_idr_lock);
2451a3b80e10SAndrii Nakryiko 	primer->link->id = primer->id;
2452a3b80e10SAndrii Nakryiko 	spin_unlock_bh(&link_idr_lock);
2453a3b80e10SAndrii Nakryiko 	/* make bpf_link fetchable by FD */
2454a3b80e10SAndrii Nakryiko 	fd_install(primer->fd, primer->file);
2455a3b80e10SAndrii Nakryiko 	/* pass through installed FD */
2456a3b80e10SAndrii Nakryiko 	return primer->fd;
2457a3b80e10SAndrii Nakryiko }
2458a3b80e10SAndrii Nakryiko 
2459a3b80e10SAndrii Nakryiko int bpf_link_new_fd(struct bpf_link *link)
2460a3b80e10SAndrii Nakryiko {
2461a3b80e10SAndrii Nakryiko 	return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2462babf3164SAndrii Nakryiko }
2463babf3164SAndrii Nakryiko 
246470ed506cSAndrii Nakryiko struct bpf_link *bpf_link_get_from_fd(u32 ufd)
246570ed506cSAndrii Nakryiko {
246670ed506cSAndrii Nakryiko 	struct fd f = fdget(ufd);
246770ed506cSAndrii Nakryiko 	struct bpf_link *link;
246870ed506cSAndrii Nakryiko 
246970ed506cSAndrii Nakryiko 	if (!f.file)
247070ed506cSAndrii Nakryiko 		return ERR_PTR(-EBADF);
247170ed506cSAndrii Nakryiko 	if (f.file->f_op != &bpf_link_fops) {
247270ed506cSAndrii Nakryiko 		fdput(f);
247370ed506cSAndrii Nakryiko 		return ERR_PTR(-EINVAL);
247470ed506cSAndrii Nakryiko 	}
247570ed506cSAndrii Nakryiko 
247670ed506cSAndrii Nakryiko 	link = f.file->private_data;
247770ed506cSAndrii Nakryiko 	bpf_link_inc(link);
247870ed506cSAndrii Nakryiko 	fdput(f);
247970ed506cSAndrii Nakryiko 
248070ed506cSAndrii Nakryiko 	return link;
248170ed506cSAndrii Nakryiko }
248270ed506cSAndrii Nakryiko 
248370ed506cSAndrii Nakryiko struct bpf_tracing_link {
248470ed506cSAndrii Nakryiko 	struct bpf_link link;
2485f2e10bffSAndrii Nakryiko 	enum bpf_attach_type attach_type;
248670ed506cSAndrii Nakryiko };
248770ed506cSAndrii Nakryiko 
248870ed506cSAndrii Nakryiko static void bpf_tracing_link_release(struct bpf_link *link)
248970ed506cSAndrii Nakryiko {
2490babf3164SAndrii Nakryiko 	WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog));
2491babf3164SAndrii Nakryiko }
2492babf3164SAndrii Nakryiko 
2493babf3164SAndrii Nakryiko static void bpf_tracing_link_dealloc(struct bpf_link *link)
2494babf3164SAndrii Nakryiko {
249570ed506cSAndrii Nakryiko 	struct bpf_tracing_link *tr_link =
249670ed506cSAndrii Nakryiko 		container_of(link, struct bpf_tracing_link, link);
249770ed506cSAndrii Nakryiko 
249870ed506cSAndrii Nakryiko 	kfree(tr_link);
249970ed506cSAndrii Nakryiko }
250070ed506cSAndrii Nakryiko 
2501f2e10bffSAndrii Nakryiko static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2502f2e10bffSAndrii Nakryiko 					 struct seq_file *seq)
2503f2e10bffSAndrii Nakryiko {
2504f2e10bffSAndrii Nakryiko 	struct bpf_tracing_link *tr_link =
2505f2e10bffSAndrii Nakryiko 		container_of(link, struct bpf_tracing_link, link);
2506f2e10bffSAndrii Nakryiko 
2507f2e10bffSAndrii Nakryiko 	seq_printf(seq,
2508f2e10bffSAndrii Nakryiko 		   "attach_type:\t%d\n",
2509f2e10bffSAndrii Nakryiko 		   tr_link->attach_type);
2510f2e10bffSAndrii Nakryiko }
2511f2e10bffSAndrii Nakryiko 
2512f2e10bffSAndrii Nakryiko static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2513f2e10bffSAndrii Nakryiko 					   struct bpf_link_info *info)
2514f2e10bffSAndrii Nakryiko {
2515f2e10bffSAndrii Nakryiko 	struct bpf_tracing_link *tr_link =
2516f2e10bffSAndrii Nakryiko 		container_of(link, struct bpf_tracing_link, link);
2517f2e10bffSAndrii Nakryiko 
2518f2e10bffSAndrii Nakryiko 	info->tracing.attach_type = tr_link->attach_type;
2519f2e10bffSAndrii Nakryiko 
2520f2e10bffSAndrii Nakryiko 	return 0;
2521f2e10bffSAndrii Nakryiko }
2522f2e10bffSAndrii Nakryiko 
252370ed506cSAndrii Nakryiko static const struct bpf_link_ops bpf_tracing_link_lops = {
252470ed506cSAndrii Nakryiko 	.release = bpf_tracing_link_release,
2525babf3164SAndrii Nakryiko 	.dealloc = bpf_tracing_link_dealloc,
2526f2e10bffSAndrii Nakryiko 	.show_fdinfo = bpf_tracing_link_show_fdinfo,
2527f2e10bffSAndrii Nakryiko 	.fill_link_info = bpf_tracing_link_fill_link_info,
252870ed506cSAndrii Nakryiko };
252970ed506cSAndrii Nakryiko 
2530fec56f58SAlexei Starovoitov static int bpf_tracing_prog_attach(struct bpf_prog *prog)
2531fec56f58SAlexei Starovoitov {
2532a3b80e10SAndrii Nakryiko 	struct bpf_link_primer link_primer;
253370ed506cSAndrii Nakryiko 	struct bpf_tracing_link *link;
2534a3b80e10SAndrii Nakryiko 	int err;
2535fec56f58SAlexei Starovoitov 
25369e4e01dfSKP Singh 	switch (prog->type) {
25379e4e01dfSKP Singh 	case BPF_PROG_TYPE_TRACING:
2538fec56f58SAlexei Starovoitov 		if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2539be8704ffSAlexei Starovoitov 		    prog->expected_attach_type != BPF_TRACE_FEXIT &&
25409e4e01dfSKP Singh 		    prog->expected_attach_type != BPF_MODIFY_RETURN) {
25419e4e01dfSKP Singh 			err = -EINVAL;
25429e4e01dfSKP Singh 			goto out_put_prog;
25439e4e01dfSKP Singh 		}
25449e4e01dfSKP Singh 		break;
25459e4e01dfSKP Singh 	case BPF_PROG_TYPE_EXT:
25469e4e01dfSKP Singh 		if (prog->expected_attach_type != 0) {
25479e4e01dfSKP Singh 			err = -EINVAL;
25489e4e01dfSKP Singh 			goto out_put_prog;
25499e4e01dfSKP Singh 		}
25509e4e01dfSKP Singh 		break;
25519e4e01dfSKP Singh 	case BPF_PROG_TYPE_LSM:
25529e4e01dfSKP Singh 		if (prog->expected_attach_type != BPF_LSM_MAC) {
25539e4e01dfSKP Singh 			err = -EINVAL;
25549e4e01dfSKP Singh 			goto out_put_prog;
25559e4e01dfSKP Singh 		}
25569e4e01dfSKP Singh 		break;
25579e4e01dfSKP Singh 	default:
2558fec56f58SAlexei Starovoitov 		err = -EINVAL;
2559fec56f58SAlexei Starovoitov 		goto out_put_prog;
2560fec56f58SAlexei Starovoitov 	}
2561fec56f58SAlexei Starovoitov 
256270ed506cSAndrii Nakryiko 	link = kzalloc(sizeof(*link), GFP_USER);
256370ed506cSAndrii Nakryiko 	if (!link) {
256470ed506cSAndrii Nakryiko 		err = -ENOMEM;
2565fec56f58SAlexei Starovoitov 		goto out_put_prog;
2566fec56f58SAlexei Starovoitov 	}
2567f2e10bffSAndrii Nakryiko 	bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2568f2e10bffSAndrii Nakryiko 		      &bpf_tracing_link_lops, prog);
2569f2e10bffSAndrii Nakryiko 	link->attach_type = prog->expected_attach_type;
2570fec56f58SAlexei Starovoitov 
2571a3b80e10SAndrii Nakryiko 	err = bpf_link_prime(&link->link, &link_primer);
2572a3b80e10SAndrii Nakryiko 	if (err) {
2573babf3164SAndrii Nakryiko 		kfree(link);
2574babf3164SAndrii Nakryiko 		goto out_put_prog;
257570ed506cSAndrii Nakryiko 	}
2576babf3164SAndrii Nakryiko 
2577babf3164SAndrii Nakryiko 	err = bpf_trampoline_link_prog(prog);
2578babf3164SAndrii Nakryiko 	if (err) {
2579a3b80e10SAndrii Nakryiko 		bpf_link_cleanup(&link_primer);
2580babf3164SAndrii Nakryiko 		goto out_put_prog;
2581babf3164SAndrii Nakryiko 	}
2582babf3164SAndrii Nakryiko 
2583a3b80e10SAndrii Nakryiko 	return bpf_link_settle(&link_primer);
2584fec56f58SAlexei Starovoitov out_put_prog:
2585fec56f58SAlexei Starovoitov 	bpf_prog_put(prog);
2586fec56f58SAlexei Starovoitov 	return err;
2587fec56f58SAlexei Starovoitov }
2588fec56f58SAlexei Starovoitov 
258970ed506cSAndrii Nakryiko struct bpf_raw_tp_link {
259070ed506cSAndrii Nakryiko 	struct bpf_link link;
2591c4f6699dSAlexei Starovoitov 	struct bpf_raw_event_map *btp;
2592c4f6699dSAlexei Starovoitov };
2593c4f6699dSAlexei Starovoitov 
259470ed506cSAndrii Nakryiko static void bpf_raw_tp_link_release(struct bpf_link *link)
2595c4f6699dSAlexei Starovoitov {
259670ed506cSAndrii Nakryiko 	struct bpf_raw_tp_link *raw_tp =
259770ed506cSAndrii Nakryiko 		container_of(link, struct bpf_raw_tp_link, link);
2598c4f6699dSAlexei Starovoitov 
259970ed506cSAndrii Nakryiko 	bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2600a38d1107SMatt Mullins 	bpf_put_raw_tracepoint(raw_tp->btp);
2601babf3164SAndrii Nakryiko }
2602babf3164SAndrii Nakryiko 
2603babf3164SAndrii Nakryiko static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2604babf3164SAndrii Nakryiko {
2605babf3164SAndrii Nakryiko 	struct bpf_raw_tp_link *raw_tp =
2606babf3164SAndrii Nakryiko 		container_of(link, struct bpf_raw_tp_link, link);
2607babf3164SAndrii Nakryiko 
2608c4f6699dSAlexei Starovoitov 	kfree(raw_tp);
2609c4f6699dSAlexei Starovoitov }
2610c4f6699dSAlexei Starovoitov 
2611f2e10bffSAndrii Nakryiko static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2612f2e10bffSAndrii Nakryiko 					struct seq_file *seq)
2613f2e10bffSAndrii Nakryiko {
2614f2e10bffSAndrii Nakryiko 	struct bpf_raw_tp_link *raw_tp_link =
2615f2e10bffSAndrii Nakryiko 		container_of(link, struct bpf_raw_tp_link, link);
2616f2e10bffSAndrii Nakryiko 
2617f2e10bffSAndrii Nakryiko 	seq_printf(seq,
2618f2e10bffSAndrii Nakryiko 		   "tp_name:\t%s\n",
2619f2e10bffSAndrii Nakryiko 		   raw_tp_link->btp->tp->name);
2620f2e10bffSAndrii Nakryiko }
2621f2e10bffSAndrii Nakryiko 
2622f2e10bffSAndrii Nakryiko static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2623f2e10bffSAndrii Nakryiko 					  struct bpf_link_info *info)
2624f2e10bffSAndrii Nakryiko {
2625f2e10bffSAndrii Nakryiko 	struct bpf_raw_tp_link *raw_tp_link =
2626f2e10bffSAndrii Nakryiko 		container_of(link, struct bpf_raw_tp_link, link);
2627f2e10bffSAndrii Nakryiko 	char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2628f2e10bffSAndrii Nakryiko 	const char *tp_name = raw_tp_link->btp->tp->name;
2629f2e10bffSAndrii Nakryiko 	u32 ulen = info->raw_tracepoint.tp_name_len;
2630f2e10bffSAndrii Nakryiko 	size_t tp_len = strlen(tp_name);
2631f2e10bffSAndrii Nakryiko 
2632f2e10bffSAndrii Nakryiko 	if (ulen && !ubuf)
2633f2e10bffSAndrii Nakryiko 		return -EINVAL;
2634f2e10bffSAndrii Nakryiko 
2635f2e10bffSAndrii Nakryiko 	info->raw_tracepoint.tp_name_len = tp_len + 1;
2636f2e10bffSAndrii Nakryiko 
2637f2e10bffSAndrii Nakryiko 	if (!ubuf)
2638f2e10bffSAndrii Nakryiko 		return 0;
2639f2e10bffSAndrii Nakryiko 
2640f2e10bffSAndrii Nakryiko 	if (ulen >= tp_len + 1) {
2641f2e10bffSAndrii Nakryiko 		if (copy_to_user(ubuf, tp_name, tp_len + 1))
2642f2e10bffSAndrii Nakryiko 			return -EFAULT;
2643f2e10bffSAndrii Nakryiko 	} else {
2644f2e10bffSAndrii Nakryiko 		char zero = '\0';
2645f2e10bffSAndrii Nakryiko 
2646f2e10bffSAndrii Nakryiko 		if (copy_to_user(ubuf, tp_name, ulen - 1))
2647f2e10bffSAndrii Nakryiko 			return -EFAULT;
2648f2e10bffSAndrii Nakryiko 		if (put_user(zero, ubuf + ulen - 1))
2649f2e10bffSAndrii Nakryiko 			return -EFAULT;
2650f2e10bffSAndrii Nakryiko 		return -ENOSPC;
2651f2e10bffSAndrii Nakryiko 	}
2652f2e10bffSAndrii Nakryiko 
2653f2e10bffSAndrii Nakryiko 	return 0;
2654f2e10bffSAndrii Nakryiko }
2655f2e10bffSAndrii Nakryiko 
2656a3b80e10SAndrii Nakryiko static const struct bpf_link_ops bpf_raw_tp_link_lops = {
265770ed506cSAndrii Nakryiko 	.release = bpf_raw_tp_link_release,
2658babf3164SAndrii Nakryiko 	.dealloc = bpf_raw_tp_link_dealloc,
2659f2e10bffSAndrii Nakryiko 	.show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2660f2e10bffSAndrii Nakryiko 	.fill_link_info = bpf_raw_tp_link_fill_link_info,
2661c4f6699dSAlexei Starovoitov };
2662c4f6699dSAlexei Starovoitov 
2663c4f6699dSAlexei Starovoitov #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2664c4f6699dSAlexei Starovoitov 
2665c4f6699dSAlexei Starovoitov static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2666c4f6699dSAlexei Starovoitov {
2667a3b80e10SAndrii Nakryiko 	struct bpf_link_primer link_primer;
2668babf3164SAndrii Nakryiko 	struct bpf_raw_tp_link *link;
2669c4f6699dSAlexei Starovoitov 	struct bpf_raw_event_map *btp;
2670c4f6699dSAlexei Starovoitov 	struct bpf_prog *prog;
2671ac4414b5SAlexei Starovoitov 	const char *tp_name;
2672ac4414b5SAlexei Starovoitov 	char buf[128];
2673a3b80e10SAndrii Nakryiko 	int err;
2674c4f6699dSAlexei Starovoitov 
2675ac4414b5SAlexei Starovoitov 	if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2676ac4414b5SAlexei Starovoitov 		return -EINVAL;
2677ac4414b5SAlexei Starovoitov 
2678ac4414b5SAlexei Starovoitov 	prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2679ac4414b5SAlexei Starovoitov 	if (IS_ERR(prog))
2680ac4414b5SAlexei Starovoitov 		return PTR_ERR(prog);
2681ac4414b5SAlexei Starovoitov 
26829e4e01dfSKP Singh 	switch (prog->type) {
26839e4e01dfSKP Singh 	case BPF_PROG_TYPE_TRACING:
26849e4e01dfSKP Singh 	case BPF_PROG_TYPE_EXT:
26859e4e01dfSKP Singh 	case BPF_PROG_TYPE_LSM:
2686ac4414b5SAlexei Starovoitov 		if (attr->raw_tracepoint.name) {
2687fec56f58SAlexei Starovoitov 			/* The attach point for this category of programs
2688fec56f58SAlexei Starovoitov 			 * should be specified via btf_id during program load.
2689ac4414b5SAlexei Starovoitov 			 */
2690ac4414b5SAlexei Starovoitov 			err = -EINVAL;
2691ac4414b5SAlexei Starovoitov 			goto out_put_prog;
2692ac4414b5SAlexei Starovoitov 		}
26939e4e01dfSKP Singh 		if (prog->type == BPF_PROG_TYPE_TRACING &&
26949e4e01dfSKP Singh 		    prog->expected_attach_type == BPF_TRACE_RAW_TP) {
269538207291SMartin KaFai Lau 			tp_name = prog->aux->attach_func_name;
26969e4e01dfSKP Singh 			break;
26979e4e01dfSKP Singh 		}
2698fec56f58SAlexei Starovoitov 		return bpf_tracing_prog_attach(prog);
26999e4e01dfSKP Singh 	case BPF_PROG_TYPE_RAW_TRACEPOINT:
27009e4e01dfSKP Singh 	case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2701ac4414b5SAlexei Starovoitov 		if (strncpy_from_user(buf,
2702ac4414b5SAlexei Starovoitov 				      u64_to_user_ptr(attr->raw_tracepoint.name),
2703ac4414b5SAlexei Starovoitov 				      sizeof(buf) - 1) < 0) {
2704ac4414b5SAlexei Starovoitov 			err = -EFAULT;
2705ac4414b5SAlexei Starovoitov 			goto out_put_prog;
2706ac4414b5SAlexei Starovoitov 		}
2707ac4414b5SAlexei Starovoitov 		buf[sizeof(buf) - 1] = 0;
2708ac4414b5SAlexei Starovoitov 		tp_name = buf;
27099e4e01dfSKP Singh 		break;
27109e4e01dfSKP Singh 	default:
27119e4e01dfSKP Singh 		err = -EINVAL;
27129e4e01dfSKP Singh 		goto out_put_prog;
2713ac4414b5SAlexei Starovoitov 	}
2714c4f6699dSAlexei Starovoitov 
2715a38d1107SMatt Mullins 	btp = bpf_get_raw_tracepoint(tp_name);
2716ac4414b5SAlexei Starovoitov 	if (!btp) {
2717ac4414b5SAlexei Starovoitov 		err = -ENOENT;
2718ac4414b5SAlexei Starovoitov 		goto out_put_prog;
2719ac4414b5SAlexei Starovoitov 	}
2720c4f6699dSAlexei Starovoitov 
2721babf3164SAndrii Nakryiko 	link = kzalloc(sizeof(*link), GFP_USER);
2722babf3164SAndrii Nakryiko 	if (!link) {
2723a38d1107SMatt Mullins 		err = -ENOMEM;
2724a38d1107SMatt Mullins 		goto out_put_btp;
2725a38d1107SMatt Mullins 	}
2726f2e10bffSAndrii Nakryiko 	bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2727f2e10bffSAndrii Nakryiko 		      &bpf_raw_tp_link_lops, prog);
2728babf3164SAndrii Nakryiko 	link->btp = btp;
2729c4f6699dSAlexei Starovoitov 
2730a3b80e10SAndrii Nakryiko 	err = bpf_link_prime(&link->link, &link_primer);
2731a3b80e10SAndrii Nakryiko 	if (err) {
2732babf3164SAndrii Nakryiko 		kfree(link);
2733babf3164SAndrii Nakryiko 		goto out_put_btp;
2734c4f6699dSAlexei Starovoitov 	}
2735babf3164SAndrii Nakryiko 
2736babf3164SAndrii Nakryiko 	err = bpf_probe_register(link->btp, prog);
2737babf3164SAndrii Nakryiko 	if (err) {
2738a3b80e10SAndrii Nakryiko 		bpf_link_cleanup(&link_primer);
2739babf3164SAndrii Nakryiko 		goto out_put_btp;
2740babf3164SAndrii Nakryiko 	}
2741babf3164SAndrii Nakryiko 
2742a3b80e10SAndrii Nakryiko 	return bpf_link_settle(&link_primer);
2743c4f6699dSAlexei Starovoitov 
2744a38d1107SMatt Mullins out_put_btp:
2745a38d1107SMatt Mullins 	bpf_put_raw_tracepoint(btp);
2746ac4414b5SAlexei Starovoitov out_put_prog:
2747ac4414b5SAlexei Starovoitov 	bpf_prog_put(prog);
2748c4f6699dSAlexei Starovoitov 	return err;
2749c4f6699dSAlexei Starovoitov }
2750c4f6699dSAlexei Starovoitov 
275133491588SAnders Roxell static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
275233491588SAnders Roxell 					     enum bpf_attach_type attach_type)
275333491588SAnders Roxell {
275433491588SAnders Roxell 	switch (prog->type) {
275533491588SAnders Roxell 	case BPF_PROG_TYPE_CGROUP_SOCK:
275633491588SAnders Roxell 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
27570d01da6aSStanislav Fomichev 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
275833491588SAnders Roxell 		return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
27595cf1e914Sbrakmo 	case BPF_PROG_TYPE_CGROUP_SKB:
27602c78ee89SAlexei Starovoitov 		if (!capable(CAP_NET_ADMIN))
27612c78ee89SAlexei Starovoitov 			/* cg-skb progs can be loaded by unpriv user.
27622c78ee89SAlexei Starovoitov 			 * check permissions at attach time.
27632c78ee89SAlexei Starovoitov 			 */
27642c78ee89SAlexei Starovoitov 			return -EPERM;
27655cf1e914Sbrakmo 		return prog->enforce_expected_attach_type &&
27665cf1e914Sbrakmo 			prog->expected_attach_type != attach_type ?
27675cf1e914Sbrakmo 			-EINVAL : 0;
276833491588SAnders Roxell 	default:
276933491588SAnders Roxell 		return 0;
277033491588SAnders Roxell 	}
277133491588SAnders Roxell }
277233491588SAnders Roxell 
2773e28784e3SAndrii Nakryiko static enum bpf_prog_type
2774e28784e3SAndrii Nakryiko attach_type_to_prog_type(enum bpf_attach_type attach_type)
2775e28784e3SAndrii Nakryiko {
2776e28784e3SAndrii Nakryiko 	switch (attach_type) {
2777e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET_INGRESS:
2778e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET_EGRESS:
2779e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_CGROUP_SKB;
2780e28784e3SAndrii Nakryiko 		break;
2781e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET_SOCK_CREATE:
2782e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET4_POST_BIND:
2783e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET6_POST_BIND:
2784e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_CGROUP_SOCK;
2785e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET4_BIND:
2786e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET6_BIND:
2787e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET4_CONNECT:
2788e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET6_CONNECT:
27891b66d253SDaniel Borkmann 	case BPF_CGROUP_INET4_GETPEERNAME:
27901b66d253SDaniel Borkmann 	case BPF_CGROUP_INET6_GETPEERNAME:
27911b66d253SDaniel Borkmann 	case BPF_CGROUP_INET4_GETSOCKNAME:
27921b66d253SDaniel Borkmann 	case BPF_CGROUP_INET6_GETSOCKNAME:
2793e28784e3SAndrii Nakryiko 	case BPF_CGROUP_UDP4_SENDMSG:
2794e28784e3SAndrii Nakryiko 	case BPF_CGROUP_UDP6_SENDMSG:
2795e28784e3SAndrii Nakryiko 	case BPF_CGROUP_UDP4_RECVMSG:
2796e28784e3SAndrii Nakryiko 	case BPF_CGROUP_UDP6_RECVMSG:
2797e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2798e28784e3SAndrii Nakryiko 	case BPF_CGROUP_SOCK_OPS:
2799e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_SOCK_OPS;
2800e28784e3SAndrii Nakryiko 	case BPF_CGROUP_DEVICE:
2801e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_CGROUP_DEVICE;
2802e28784e3SAndrii Nakryiko 	case BPF_SK_MSG_VERDICT:
2803e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_SK_MSG;
2804e28784e3SAndrii Nakryiko 	case BPF_SK_SKB_STREAM_PARSER:
2805e28784e3SAndrii Nakryiko 	case BPF_SK_SKB_STREAM_VERDICT:
2806e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_SK_SKB;
2807e28784e3SAndrii Nakryiko 	case BPF_LIRC_MODE2:
2808e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_LIRC_MODE2;
2809e28784e3SAndrii Nakryiko 	case BPF_FLOW_DISSECTOR:
2810e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_FLOW_DISSECTOR;
2811e28784e3SAndrii Nakryiko 	case BPF_CGROUP_SYSCTL:
2812e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_CGROUP_SYSCTL;
2813e28784e3SAndrii Nakryiko 	case BPF_CGROUP_GETSOCKOPT:
2814e28784e3SAndrii Nakryiko 	case BPF_CGROUP_SETSOCKOPT:
2815e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_CGROUP_SOCKOPT;
2816de4e05caSYonghong Song 	case BPF_TRACE_ITER:
2817de4e05caSYonghong Song 		return BPF_PROG_TYPE_TRACING;
2818e28784e3SAndrii Nakryiko 	default:
2819e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_UNSPEC;
2820e28784e3SAndrii Nakryiko 	}
2821e28784e3SAndrii Nakryiko }
2822e28784e3SAndrii Nakryiko 
28237dd68b32SAndrey Ignatov #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
2824174a79ffSJohn Fastabend 
2825324bda9eSAlexei Starovoitov #define BPF_F_ATTACH_MASK \
28267dd68b32SAndrey Ignatov 	(BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
2827324bda9eSAlexei Starovoitov 
2828f4324551SDaniel Mack static int bpf_prog_attach(const union bpf_attr *attr)
2829f4324551SDaniel Mack {
28307f677633SAlexei Starovoitov 	enum bpf_prog_type ptype;
2831f4324551SDaniel Mack 	struct bpf_prog *prog;
28327f677633SAlexei Starovoitov 	int ret;
2833f4324551SDaniel Mack 
2834f4324551SDaniel Mack 	if (CHECK_ATTR(BPF_PROG_ATTACH))
2835f4324551SDaniel Mack 		return -EINVAL;
2836f4324551SDaniel Mack 
2837324bda9eSAlexei Starovoitov 	if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
28387f677633SAlexei Starovoitov 		return -EINVAL;
28397f677633SAlexei Starovoitov 
2840e28784e3SAndrii Nakryiko 	ptype = attach_type_to_prog_type(attr->attach_type);
2841e28784e3SAndrii Nakryiko 	if (ptype == BPF_PROG_TYPE_UNSPEC)
2842b2cd1257SDavid Ahern 		return -EINVAL;
2843b2cd1257SDavid Ahern 
2844b2cd1257SDavid Ahern 	prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2845f4324551SDaniel Mack 	if (IS_ERR(prog))
2846f4324551SDaniel Mack 		return PTR_ERR(prog);
2847f4324551SDaniel Mack 
28485e43f899SAndrey Ignatov 	if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
28495e43f899SAndrey Ignatov 		bpf_prog_put(prog);
28505e43f899SAndrey Ignatov 		return -EINVAL;
28515e43f899SAndrey Ignatov 	}
28525e43f899SAndrey Ignatov 
2853fdb5c453SSean Young 	switch (ptype) {
2854fdb5c453SSean Young 	case BPF_PROG_TYPE_SK_SKB:
2855fdb5c453SSean Young 	case BPF_PROG_TYPE_SK_MSG:
2856604326b4SDaniel Borkmann 		ret = sock_map_get_from_fd(attr, prog);
2857fdb5c453SSean Young 		break;
2858fdb5c453SSean Young 	case BPF_PROG_TYPE_LIRC_MODE2:
2859fdb5c453SSean Young 		ret = lirc_prog_attach(attr, prog);
2860fdb5c453SSean Young 		break;
2861d58e468bSPetar Penkov 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
2862a3fd7ceeSJakub Sitnicki 		ret = netns_bpf_prog_attach(attr, prog);
2863d58e468bSPetar Penkov 		break;
2864e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_DEVICE:
2865e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SKB:
2866e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCK:
2867e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2868e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2869e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
2870e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_SOCK_OPS:
2871fdb5c453SSean Young 		ret = cgroup_bpf_prog_attach(attr, ptype, prog);
2872e28784e3SAndrii Nakryiko 		break;
2873e28784e3SAndrii Nakryiko 	default:
2874e28784e3SAndrii Nakryiko 		ret = -EINVAL;
2875f4324551SDaniel Mack 	}
2876f4324551SDaniel Mack 
28777f677633SAlexei Starovoitov 	if (ret)
28787f677633SAlexei Starovoitov 		bpf_prog_put(prog);
28797f677633SAlexei Starovoitov 	return ret;
2880f4324551SDaniel Mack }
2881f4324551SDaniel Mack 
2882f4324551SDaniel Mack #define BPF_PROG_DETACH_LAST_FIELD attach_type
2883f4324551SDaniel Mack 
2884f4324551SDaniel Mack static int bpf_prog_detach(const union bpf_attr *attr)
2885f4324551SDaniel Mack {
2886324bda9eSAlexei Starovoitov 	enum bpf_prog_type ptype;
2887f4324551SDaniel Mack 
2888f4324551SDaniel Mack 	if (CHECK_ATTR(BPF_PROG_DETACH))
2889f4324551SDaniel Mack 		return -EINVAL;
2890f4324551SDaniel Mack 
2891e28784e3SAndrii Nakryiko 	ptype = attach_type_to_prog_type(attr->attach_type);
2892e28784e3SAndrii Nakryiko 
2893e28784e3SAndrii Nakryiko 	switch (ptype) {
2894e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_SK_MSG:
2895e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_SK_SKB:
2896604326b4SDaniel Borkmann 		return sock_map_get_from_fd(attr, NULL);
2897e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_LIRC_MODE2:
2898f4364dcfSSean Young 		return lirc_prog_detach(attr);
2899e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
29002c78ee89SAlexei Starovoitov 		if (!capable(CAP_NET_ADMIN))
29012c78ee89SAlexei Starovoitov 			return -EPERM;
2902a3fd7ceeSJakub Sitnicki 		return netns_bpf_prog_detach(attr);
2903e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_DEVICE:
2904e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SKB:
2905e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCK:
2906e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2907e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2908e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
2909e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_SOCK_OPS:
2910e28784e3SAndrii Nakryiko 		return cgroup_bpf_prog_detach(attr, ptype);
2911f4324551SDaniel Mack 	default:
2912f4324551SDaniel Mack 		return -EINVAL;
2913f4324551SDaniel Mack 	}
2914f4324551SDaniel Mack }
291540304b2aSLawrence Brakmo 
2916468e2f64SAlexei Starovoitov #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2917468e2f64SAlexei Starovoitov 
2918468e2f64SAlexei Starovoitov static int bpf_prog_query(const union bpf_attr *attr,
2919468e2f64SAlexei Starovoitov 			  union bpf_attr __user *uattr)
2920468e2f64SAlexei Starovoitov {
2921468e2f64SAlexei Starovoitov 	if (!capable(CAP_NET_ADMIN))
2922468e2f64SAlexei Starovoitov 		return -EPERM;
2923468e2f64SAlexei Starovoitov 	if (CHECK_ATTR(BPF_PROG_QUERY))
2924468e2f64SAlexei Starovoitov 		return -EINVAL;
2925468e2f64SAlexei Starovoitov 	if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2926468e2f64SAlexei Starovoitov 		return -EINVAL;
2927468e2f64SAlexei Starovoitov 
2928468e2f64SAlexei Starovoitov 	switch (attr->query.attach_type) {
2929468e2f64SAlexei Starovoitov 	case BPF_CGROUP_INET_INGRESS:
2930468e2f64SAlexei Starovoitov 	case BPF_CGROUP_INET_EGRESS:
2931468e2f64SAlexei Starovoitov 	case BPF_CGROUP_INET_SOCK_CREATE:
29324fbac77dSAndrey Ignatov 	case BPF_CGROUP_INET4_BIND:
29334fbac77dSAndrey Ignatov 	case BPF_CGROUP_INET6_BIND:
2934aac3fc32SAndrey Ignatov 	case BPF_CGROUP_INET4_POST_BIND:
2935aac3fc32SAndrey Ignatov 	case BPF_CGROUP_INET6_POST_BIND:
2936d74bad4eSAndrey Ignatov 	case BPF_CGROUP_INET4_CONNECT:
2937d74bad4eSAndrey Ignatov 	case BPF_CGROUP_INET6_CONNECT:
29381b66d253SDaniel Borkmann 	case BPF_CGROUP_INET4_GETPEERNAME:
29391b66d253SDaniel Borkmann 	case BPF_CGROUP_INET6_GETPEERNAME:
29401b66d253SDaniel Borkmann 	case BPF_CGROUP_INET4_GETSOCKNAME:
29411b66d253SDaniel Borkmann 	case BPF_CGROUP_INET6_GETSOCKNAME:
29421cedee13SAndrey Ignatov 	case BPF_CGROUP_UDP4_SENDMSG:
29431cedee13SAndrey Ignatov 	case BPF_CGROUP_UDP6_SENDMSG:
2944983695faSDaniel Borkmann 	case BPF_CGROUP_UDP4_RECVMSG:
2945983695faSDaniel Borkmann 	case BPF_CGROUP_UDP6_RECVMSG:
2946468e2f64SAlexei Starovoitov 	case BPF_CGROUP_SOCK_OPS:
2947ebc614f6SRoman Gushchin 	case BPF_CGROUP_DEVICE:
29487b146cebSAndrey Ignatov 	case BPF_CGROUP_SYSCTL:
29490d01da6aSStanislav Fomichev 	case BPF_CGROUP_GETSOCKOPT:
29500d01da6aSStanislav Fomichev 	case BPF_CGROUP_SETSOCKOPT:
2951e28784e3SAndrii Nakryiko 		return cgroup_bpf_prog_query(attr, uattr);
2952f4364dcfSSean Young 	case BPF_LIRC_MODE2:
2953f4364dcfSSean Young 		return lirc_prog_query(attr, uattr);
2954118c8e9aSStanislav Fomichev 	case BPF_FLOW_DISSECTOR:
2955a3fd7ceeSJakub Sitnicki 		return netns_bpf_prog_query(attr, uattr);
2956468e2f64SAlexei Starovoitov 	default:
2957468e2f64SAlexei Starovoitov 		return -EINVAL;
2958468e2f64SAlexei Starovoitov 	}
2959468e2f64SAlexei Starovoitov }
2960f4324551SDaniel Mack 
2961b0b9395dSStanislav Fomichev #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
29621cf1cae9SAlexei Starovoitov 
29631cf1cae9SAlexei Starovoitov static int bpf_prog_test_run(const union bpf_attr *attr,
29641cf1cae9SAlexei Starovoitov 			     union bpf_attr __user *uattr)
29651cf1cae9SAlexei Starovoitov {
29661cf1cae9SAlexei Starovoitov 	struct bpf_prog *prog;
29671cf1cae9SAlexei Starovoitov 	int ret = -ENOTSUPP;
29681cf1cae9SAlexei Starovoitov 
29691cf1cae9SAlexei Starovoitov 	if (CHECK_ATTR(BPF_PROG_TEST_RUN))
29701cf1cae9SAlexei Starovoitov 		return -EINVAL;
29711cf1cae9SAlexei Starovoitov 
2972b0b9395dSStanislav Fomichev 	if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2973b0b9395dSStanislav Fomichev 	    (!attr->test.ctx_size_in && attr->test.ctx_in))
2974b0b9395dSStanislav Fomichev 		return -EINVAL;
2975b0b9395dSStanislav Fomichev 
2976b0b9395dSStanislav Fomichev 	if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2977b0b9395dSStanislav Fomichev 	    (!attr->test.ctx_size_out && attr->test.ctx_out))
2978b0b9395dSStanislav Fomichev 		return -EINVAL;
2979b0b9395dSStanislav Fomichev 
29801cf1cae9SAlexei Starovoitov 	prog = bpf_prog_get(attr->test.prog_fd);
29811cf1cae9SAlexei Starovoitov 	if (IS_ERR(prog))
29821cf1cae9SAlexei Starovoitov 		return PTR_ERR(prog);
29831cf1cae9SAlexei Starovoitov 
29841cf1cae9SAlexei Starovoitov 	if (prog->aux->ops->test_run)
29851cf1cae9SAlexei Starovoitov 		ret = prog->aux->ops->test_run(prog, attr, uattr);
29861cf1cae9SAlexei Starovoitov 
29871cf1cae9SAlexei Starovoitov 	bpf_prog_put(prog);
29881cf1cae9SAlexei Starovoitov 	return ret;
29891cf1cae9SAlexei Starovoitov }
29901cf1cae9SAlexei Starovoitov 
299134ad5580SMartin KaFai Lau #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
299234ad5580SMartin KaFai Lau 
299334ad5580SMartin KaFai Lau static int bpf_obj_get_next_id(const union bpf_attr *attr,
299434ad5580SMartin KaFai Lau 			       union bpf_attr __user *uattr,
299534ad5580SMartin KaFai Lau 			       struct idr *idr,
299634ad5580SMartin KaFai Lau 			       spinlock_t *lock)
299734ad5580SMartin KaFai Lau {
299834ad5580SMartin KaFai Lau 	u32 next_id = attr->start_id;
299934ad5580SMartin KaFai Lau 	int err = 0;
300034ad5580SMartin KaFai Lau 
300134ad5580SMartin KaFai Lau 	if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
300234ad5580SMartin KaFai Lau 		return -EINVAL;
300334ad5580SMartin KaFai Lau 
300434ad5580SMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN))
300534ad5580SMartin KaFai Lau 		return -EPERM;
300634ad5580SMartin KaFai Lau 
300734ad5580SMartin KaFai Lau 	next_id++;
300834ad5580SMartin KaFai Lau 	spin_lock_bh(lock);
300934ad5580SMartin KaFai Lau 	if (!idr_get_next(idr, &next_id))
301034ad5580SMartin KaFai Lau 		err = -ENOENT;
301134ad5580SMartin KaFai Lau 	spin_unlock_bh(lock);
301234ad5580SMartin KaFai Lau 
301334ad5580SMartin KaFai Lau 	if (!err)
301434ad5580SMartin KaFai Lau 		err = put_user(next_id, &uattr->next_id);
301534ad5580SMartin KaFai Lau 
301634ad5580SMartin KaFai Lau 	return err;
301734ad5580SMartin KaFai Lau }
301834ad5580SMartin KaFai Lau 
30196086d29dSYonghong Song struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
30206086d29dSYonghong Song {
30216086d29dSYonghong Song 	struct bpf_map *map;
30226086d29dSYonghong Song 
30236086d29dSYonghong Song 	spin_lock_bh(&map_idr_lock);
30246086d29dSYonghong Song again:
30256086d29dSYonghong Song 	map = idr_get_next(&map_idr, id);
30266086d29dSYonghong Song 	if (map) {
30276086d29dSYonghong Song 		map = __bpf_map_inc_not_zero(map, false);
30286086d29dSYonghong Song 		if (IS_ERR(map)) {
30296086d29dSYonghong Song 			(*id)++;
30306086d29dSYonghong Song 			goto again;
30316086d29dSYonghong Song 		}
30326086d29dSYonghong Song 	}
30336086d29dSYonghong Song 	spin_unlock_bh(&map_idr_lock);
30346086d29dSYonghong Song 
30356086d29dSYonghong Song 	return map;
30366086d29dSYonghong Song }
30376086d29dSYonghong Song 
3038b16d9aa4SMartin KaFai Lau #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3039b16d9aa4SMartin KaFai Lau 
30407e6897f9SBjörn Töpel struct bpf_prog *bpf_prog_by_id(u32 id)
30417e6897f9SBjörn Töpel {
30427e6897f9SBjörn Töpel 	struct bpf_prog *prog;
30437e6897f9SBjörn Töpel 
30447e6897f9SBjörn Töpel 	if (!id)
30457e6897f9SBjörn Töpel 		return ERR_PTR(-ENOENT);
30467e6897f9SBjörn Töpel 
30477e6897f9SBjörn Töpel 	spin_lock_bh(&prog_idr_lock);
30487e6897f9SBjörn Töpel 	prog = idr_find(&prog_idr, id);
30497e6897f9SBjörn Töpel 	if (prog)
30507e6897f9SBjörn Töpel 		prog = bpf_prog_inc_not_zero(prog);
30517e6897f9SBjörn Töpel 	else
30527e6897f9SBjörn Töpel 		prog = ERR_PTR(-ENOENT);
30537e6897f9SBjörn Töpel 	spin_unlock_bh(&prog_idr_lock);
30547e6897f9SBjörn Töpel 	return prog;
30557e6897f9SBjörn Töpel }
30567e6897f9SBjörn Töpel 
3057b16d9aa4SMartin KaFai Lau static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3058b16d9aa4SMartin KaFai Lau {
3059b16d9aa4SMartin KaFai Lau 	struct bpf_prog *prog;
3060b16d9aa4SMartin KaFai Lau 	u32 id = attr->prog_id;
3061b16d9aa4SMartin KaFai Lau 	int fd;
3062b16d9aa4SMartin KaFai Lau 
3063b16d9aa4SMartin KaFai Lau 	if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3064b16d9aa4SMartin KaFai Lau 		return -EINVAL;
3065b16d9aa4SMartin KaFai Lau 
3066b16d9aa4SMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN))
3067b16d9aa4SMartin KaFai Lau 		return -EPERM;
3068b16d9aa4SMartin KaFai Lau 
30697e6897f9SBjörn Töpel 	prog = bpf_prog_by_id(id);
3070b16d9aa4SMartin KaFai Lau 	if (IS_ERR(prog))
3071b16d9aa4SMartin KaFai Lau 		return PTR_ERR(prog);
3072b16d9aa4SMartin KaFai Lau 
3073b16d9aa4SMartin KaFai Lau 	fd = bpf_prog_new_fd(prog);
3074b16d9aa4SMartin KaFai Lau 	if (fd < 0)
3075b16d9aa4SMartin KaFai Lau 		bpf_prog_put(prog);
3076b16d9aa4SMartin KaFai Lau 
3077b16d9aa4SMartin KaFai Lau 	return fd;
3078b16d9aa4SMartin KaFai Lau }
3079b16d9aa4SMartin KaFai Lau 
30806e71b04aSChenbo Feng #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3081bd5f5f4eSMartin KaFai Lau 
3082bd5f5f4eSMartin KaFai Lau static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3083bd5f5f4eSMartin KaFai Lau {
3084bd5f5f4eSMartin KaFai Lau 	struct bpf_map *map;
3085bd5f5f4eSMartin KaFai Lau 	u32 id = attr->map_id;
30866e71b04aSChenbo Feng 	int f_flags;
3087bd5f5f4eSMartin KaFai Lau 	int fd;
3088bd5f5f4eSMartin KaFai Lau 
30896e71b04aSChenbo Feng 	if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
30906e71b04aSChenbo Feng 	    attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3091bd5f5f4eSMartin KaFai Lau 		return -EINVAL;
3092bd5f5f4eSMartin KaFai Lau 
3093bd5f5f4eSMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN))
3094bd5f5f4eSMartin KaFai Lau 		return -EPERM;
3095bd5f5f4eSMartin KaFai Lau 
30966e71b04aSChenbo Feng 	f_flags = bpf_get_file_flag(attr->open_flags);
30976e71b04aSChenbo Feng 	if (f_flags < 0)
30986e71b04aSChenbo Feng 		return f_flags;
30996e71b04aSChenbo Feng 
3100bd5f5f4eSMartin KaFai Lau 	spin_lock_bh(&map_idr_lock);
3101bd5f5f4eSMartin KaFai Lau 	map = idr_find(&map_idr, id);
3102bd5f5f4eSMartin KaFai Lau 	if (map)
3103b0e4701cSStanislav Fomichev 		map = __bpf_map_inc_not_zero(map, true);
3104bd5f5f4eSMartin KaFai Lau 	else
3105bd5f5f4eSMartin KaFai Lau 		map = ERR_PTR(-ENOENT);
3106bd5f5f4eSMartin KaFai Lau 	spin_unlock_bh(&map_idr_lock);
3107bd5f5f4eSMartin KaFai Lau 
3108bd5f5f4eSMartin KaFai Lau 	if (IS_ERR(map))
3109bd5f5f4eSMartin KaFai Lau 		return PTR_ERR(map);
3110bd5f5f4eSMartin KaFai Lau 
31116e71b04aSChenbo Feng 	fd = bpf_map_new_fd(map, f_flags);
3112bd5f5f4eSMartin KaFai Lau 	if (fd < 0)
3113781e6282SPeng Sun 		bpf_map_put_with_uref(map);
3114bd5f5f4eSMartin KaFai Lau 
3115bd5f5f4eSMartin KaFai Lau 	return fd;
3116bd5f5f4eSMartin KaFai Lau }
3117bd5f5f4eSMartin KaFai Lau 
31187105e828SDaniel Borkmann static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3119d8eca5bbSDaniel Borkmann 					      unsigned long addr, u32 *off,
3120d8eca5bbSDaniel Borkmann 					      u32 *type)
31217105e828SDaniel Borkmann {
3122d8eca5bbSDaniel Borkmann 	const struct bpf_map *map;
31237105e828SDaniel Borkmann 	int i;
31247105e828SDaniel Borkmann 
3125d8eca5bbSDaniel Borkmann 	for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3126d8eca5bbSDaniel Borkmann 		map = prog->aux->used_maps[i];
3127d8eca5bbSDaniel Borkmann 		if (map == (void *)addr) {
3128d8eca5bbSDaniel Borkmann 			*type = BPF_PSEUDO_MAP_FD;
3129d8eca5bbSDaniel Borkmann 			return map;
3130d8eca5bbSDaniel Borkmann 		}
3131d8eca5bbSDaniel Borkmann 		if (!map->ops->map_direct_value_meta)
3132d8eca5bbSDaniel Borkmann 			continue;
3133d8eca5bbSDaniel Borkmann 		if (!map->ops->map_direct_value_meta(map, addr, off)) {
3134d8eca5bbSDaniel Borkmann 			*type = BPF_PSEUDO_MAP_VALUE;
3135d8eca5bbSDaniel Borkmann 			return map;
3136d8eca5bbSDaniel Borkmann 		}
3137d8eca5bbSDaniel Borkmann 	}
3138d8eca5bbSDaniel Borkmann 
31397105e828SDaniel Borkmann 	return NULL;
31407105e828SDaniel Borkmann }
31417105e828SDaniel Borkmann 
31427105e828SDaniel Borkmann static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
31437105e828SDaniel Borkmann {
31447105e828SDaniel Borkmann 	const struct bpf_map *map;
31457105e828SDaniel Borkmann 	struct bpf_insn *insns;
3146d8eca5bbSDaniel Borkmann 	u32 off, type;
31477105e828SDaniel Borkmann 	u64 imm;
314829fcb05bSAndrii Nakryiko 	u8 code;
31497105e828SDaniel Borkmann 	int i;
31507105e828SDaniel Borkmann 
31517105e828SDaniel Borkmann 	insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
31527105e828SDaniel Borkmann 			GFP_USER);
31537105e828SDaniel Borkmann 	if (!insns)
31547105e828SDaniel Borkmann 		return insns;
31557105e828SDaniel Borkmann 
31567105e828SDaniel Borkmann 	for (i = 0; i < prog->len; i++) {
315729fcb05bSAndrii Nakryiko 		code = insns[i].code;
315829fcb05bSAndrii Nakryiko 
315929fcb05bSAndrii Nakryiko 		if (code == (BPF_JMP | BPF_TAIL_CALL)) {
31607105e828SDaniel Borkmann 			insns[i].code = BPF_JMP | BPF_CALL;
31617105e828SDaniel Borkmann 			insns[i].imm = BPF_FUNC_tail_call;
31627105e828SDaniel Borkmann 			/* fall-through */
31637105e828SDaniel Borkmann 		}
316429fcb05bSAndrii Nakryiko 		if (code == (BPF_JMP | BPF_CALL) ||
316529fcb05bSAndrii Nakryiko 		    code == (BPF_JMP | BPF_CALL_ARGS)) {
316629fcb05bSAndrii Nakryiko 			if (code == (BPF_JMP | BPF_CALL_ARGS))
31677105e828SDaniel Borkmann 				insns[i].code = BPF_JMP | BPF_CALL;
31687105e828SDaniel Borkmann 			if (!bpf_dump_raw_ok())
31697105e828SDaniel Borkmann 				insns[i].imm = 0;
31707105e828SDaniel Borkmann 			continue;
31717105e828SDaniel Borkmann 		}
317229fcb05bSAndrii Nakryiko 		if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
317329fcb05bSAndrii Nakryiko 			insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
317429fcb05bSAndrii Nakryiko 			continue;
317529fcb05bSAndrii Nakryiko 		}
31767105e828SDaniel Borkmann 
317729fcb05bSAndrii Nakryiko 		if (code != (BPF_LD | BPF_IMM | BPF_DW))
31787105e828SDaniel Borkmann 			continue;
31797105e828SDaniel Borkmann 
31807105e828SDaniel Borkmann 		imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3181d8eca5bbSDaniel Borkmann 		map = bpf_map_from_imm(prog, imm, &off, &type);
31827105e828SDaniel Borkmann 		if (map) {
3183d8eca5bbSDaniel Borkmann 			insns[i].src_reg = type;
31847105e828SDaniel Borkmann 			insns[i].imm = map->id;
3185d8eca5bbSDaniel Borkmann 			insns[i + 1].imm = off;
31867105e828SDaniel Borkmann 			continue;
31877105e828SDaniel Borkmann 		}
31887105e828SDaniel Borkmann 	}
31897105e828SDaniel Borkmann 
31907105e828SDaniel Borkmann 	return insns;
31917105e828SDaniel Borkmann }
31927105e828SDaniel Borkmann 
3193c454a46bSMartin KaFai Lau static int set_info_rec_size(struct bpf_prog_info *info)
3194c454a46bSMartin KaFai Lau {
3195c454a46bSMartin KaFai Lau 	/*
3196c454a46bSMartin KaFai Lau 	 * Ensure info.*_rec_size is the same as kernel expected size
3197c454a46bSMartin KaFai Lau 	 *
3198c454a46bSMartin KaFai Lau 	 * or
3199c454a46bSMartin KaFai Lau 	 *
3200c454a46bSMartin KaFai Lau 	 * Only allow zero *_rec_size if both _rec_size and _cnt are
3201c454a46bSMartin KaFai Lau 	 * zero.  In this case, the kernel will set the expected
3202c454a46bSMartin KaFai Lau 	 * _rec_size back to the info.
3203c454a46bSMartin KaFai Lau 	 */
3204c454a46bSMartin KaFai Lau 
320511d8b82dSYonghong Song 	if ((info->nr_func_info || info->func_info_rec_size) &&
3206c454a46bSMartin KaFai Lau 	    info->func_info_rec_size != sizeof(struct bpf_func_info))
3207c454a46bSMartin KaFai Lau 		return -EINVAL;
3208c454a46bSMartin KaFai Lau 
320911d8b82dSYonghong Song 	if ((info->nr_line_info || info->line_info_rec_size) &&
3210c454a46bSMartin KaFai Lau 	    info->line_info_rec_size != sizeof(struct bpf_line_info))
3211c454a46bSMartin KaFai Lau 		return -EINVAL;
3212c454a46bSMartin KaFai Lau 
321311d8b82dSYonghong Song 	if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3214c454a46bSMartin KaFai Lau 	    info->jited_line_info_rec_size != sizeof(__u64))
3215c454a46bSMartin KaFai Lau 		return -EINVAL;
3216c454a46bSMartin KaFai Lau 
3217c454a46bSMartin KaFai Lau 	info->func_info_rec_size = sizeof(struct bpf_func_info);
3218c454a46bSMartin KaFai Lau 	info->line_info_rec_size = sizeof(struct bpf_line_info);
3219c454a46bSMartin KaFai Lau 	info->jited_line_info_rec_size = sizeof(__u64);
3220c454a46bSMartin KaFai Lau 
3221c454a46bSMartin KaFai Lau 	return 0;
3222c454a46bSMartin KaFai Lau }
3223c454a46bSMartin KaFai Lau 
32241e270976SMartin KaFai Lau static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
32251e270976SMartin KaFai Lau 				   const union bpf_attr *attr,
32261e270976SMartin KaFai Lau 				   union bpf_attr __user *uattr)
32271e270976SMartin KaFai Lau {
32281e270976SMartin KaFai Lau 	struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
32295c6f2588SGreg Kroah-Hartman 	struct bpf_prog_info info;
32301e270976SMartin KaFai Lau 	u32 info_len = attr->info.info_len;
32315f8f8b93SAlexei Starovoitov 	struct bpf_prog_stats stats;
32321e270976SMartin KaFai Lau 	char __user *uinsns;
32331e270976SMartin KaFai Lau 	u32 ulen;
32341e270976SMartin KaFai Lau 	int err;
32351e270976SMartin KaFai Lau 
3236dcab51f1SMartin KaFai Lau 	err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
32371e270976SMartin KaFai Lau 	if (err)
32381e270976SMartin KaFai Lau 		return err;
32391e270976SMartin KaFai Lau 	info_len = min_t(u32, sizeof(info), info_len);
32401e270976SMartin KaFai Lau 
32415c6f2588SGreg Kroah-Hartman 	memset(&info, 0, sizeof(info));
32421e270976SMartin KaFai Lau 	if (copy_from_user(&info, uinfo, info_len))
324389b09689SDaniel Borkmann 		return -EFAULT;
32441e270976SMartin KaFai Lau 
32451e270976SMartin KaFai Lau 	info.type = prog->type;
32461e270976SMartin KaFai Lau 	info.id = prog->aux->id;
3247cb4d2b3fSMartin KaFai Lau 	info.load_time = prog->aux->load_time;
3248cb4d2b3fSMartin KaFai Lau 	info.created_by_uid = from_kuid_munged(current_user_ns(),
3249cb4d2b3fSMartin KaFai Lau 					       prog->aux->user->uid);
3250b85fab0eSJiri Olsa 	info.gpl_compatible = prog->gpl_compatible;
32511e270976SMartin KaFai Lau 
32521e270976SMartin KaFai Lau 	memcpy(info.tag, prog->tag, sizeof(prog->tag));
3253cb4d2b3fSMartin KaFai Lau 	memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3254cb4d2b3fSMartin KaFai Lau 
3255cb4d2b3fSMartin KaFai Lau 	ulen = info.nr_map_ids;
3256cb4d2b3fSMartin KaFai Lau 	info.nr_map_ids = prog->aux->used_map_cnt;
3257cb4d2b3fSMartin KaFai Lau 	ulen = min_t(u32, info.nr_map_ids, ulen);
3258cb4d2b3fSMartin KaFai Lau 	if (ulen) {
3259721e08daSMartin KaFai Lau 		u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3260cb4d2b3fSMartin KaFai Lau 		u32 i;
3261cb4d2b3fSMartin KaFai Lau 
3262cb4d2b3fSMartin KaFai Lau 		for (i = 0; i < ulen; i++)
3263cb4d2b3fSMartin KaFai Lau 			if (put_user(prog->aux->used_maps[i]->id,
3264cb4d2b3fSMartin KaFai Lau 				     &user_map_ids[i]))
3265cb4d2b3fSMartin KaFai Lau 				return -EFAULT;
3266cb4d2b3fSMartin KaFai Lau 	}
32671e270976SMartin KaFai Lau 
3268c454a46bSMartin KaFai Lau 	err = set_info_rec_size(&info);
3269c454a46bSMartin KaFai Lau 	if (err)
3270c454a46bSMartin KaFai Lau 		return err;
32717337224fSMartin KaFai Lau 
32725f8f8b93SAlexei Starovoitov 	bpf_prog_get_stats(prog, &stats);
32735f8f8b93SAlexei Starovoitov 	info.run_time_ns = stats.nsecs;
32745f8f8b93SAlexei Starovoitov 	info.run_cnt = stats.cnt;
32755f8f8b93SAlexei Starovoitov 
32762c78ee89SAlexei Starovoitov 	if (!bpf_capable()) {
32771e270976SMartin KaFai Lau 		info.jited_prog_len = 0;
32781e270976SMartin KaFai Lau 		info.xlated_prog_len = 0;
3279dbecd738SSandipan Das 		info.nr_jited_ksyms = 0;
328028c2fae7SDaniel Borkmann 		info.nr_jited_func_lens = 0;
328111d8b82dSYonghong Song 		info.nr_func_info = 0;
328211d8b82dSYonghong Song 		info.nr_line_info = 0;
328311d8b82dSYonghong Song 		info.nr_jited_line_info = 0;
32841e270976SMartin KaFai Lau 		goto done;
32851e270976SMartin KaFai Lau 	}
32861e270976SMartin KaFai Lau 
32871e270976SMartin KaFai Lau 	ulen = info.xlated_prog_len;
32889975a54bSDaniel Borkmann 	info.xlated_prog_len = bpf_prog_insn_size(prog);
32891e270976SMartin KaFai Lau 	if (info.xlated_prog_len && ulen) {
32907105e828SDaniel Borkmann 		struct bpf_insn *insns_sanitized;
32917105e828SDaniel Borkmann 		bool fault;
32927105e828SDaniel Borkmann 
32937105e828SDaniel Borkmann 		if (prog->blinded && !bpf_dump_raw_ok()) {
32947105e828SDaniel Borkmann 			info.xlated_prog_insns = 0;
32957105e828SDaniel Borkmann 			goto done;
32967105e828SDaniel Borkmann 		}
32977105e828SDaniel Borkmann 		insns_sanitized = bpf_insn_prepare_dump(prog);
32987105e828SDaniel Borkmann 		if (!insns_sanitized)
32997105e828SDaniel Borkmann 			return -ENOMEM;
33001e270976SMartin KaFai Lau 		uinsns = u64_to_user_ptr(info.xlated_prog_insns);
33011e270976SMartin KaFai Lau 		ulen = min_t(u32, info.xlated_prog_len, ulen);
33027105e828SDaniel Borkmann 		fault = copy_to_user(uinsns, insns_sanitized, ulen);
33037105e828SDaniel Borkmann 		kfree(insns_sanitized);
33047105e828SDaniel Borkmann 		if (fault)
33051e270976SMartin KaFai Lau 			return -EFAULT;
33061e270976SMartin KaFai Lau 	}
33071e270976SMartin KaFai Lau 
3308675fc275SJakub Kicinski 	if (bpf_prog_is_dev_bound(prog->aux)) {
3309675fc275SJakub Kicinski 		err = bpf_prog_offload_info_fill(&info, prog);
3310675fc275SJakub Kicinski 		if (err)
3311675fc275SJakub Kicinski 			return err;
3312fcfb126dSJiong Wang 		goto done;
3313fcfb126dSJiong Wang 	}
3314fcfb126dSJiong Wang 
3315fcfb126dSJiong Wang 	/* NOTE: the following code is supposed to be skipped for offload.
3316fcfb126dSJiong Wang 	 * bpf_prog_offload_info_fill() is the place to fill similar fields
3317fcfb126dSJiong Wang 	 * for offload.
3318fcfb126dSJiong Wang 	 */
3319fcfb126dSJiong Wang 	ulen = info.jited_prog_len;
33204d56a76eSSandipan Das 	if (prog->aux->func_cnt) {
33214d56a76eSSandipan Das 		u32 i;
33224d56a76eSSandipan Das 
33234d56a76eSSandipan Das 		info.jited_prog_len = 0;
33244d56a76eSSandipan Das 		for (i = 0; i < prog->aux->func_cnt; i++)
33254d56a76eSSandipan Das 			info.jited_prog_len += prog->aux->func[i]->jited_len;
33264d56a76eSSandipan Das 	} else {
3327fcfb126dSJiong Wang 		info.jited_prog_len = prog->jited_len;
33284d56a76eSSandipan Das 	}
33294d56a76eSSandipan Das 
3330fcfb126dSJiong Wang 	if (info.jited_prog_len && ulen) {
3331fcfb126dSJiong Wang 		if (bpf_dump_raw_ok()) {
3332fcfb126dSJiong Wang 			uinsns = u64_to_user_ptr(info.jited_prog_insns);
3333fcfb126dSJiong Wang 			ulen = min_t(u32, info.jited_prog_len, ulen);
33344d56a76eSSandipan Das 
33354d56a76eSSandipan Das 			/* for multi-function programs, copy the JITed
33364d56a76eSSandipan Das 			 * instructions for all the functions
33374d56a76eSSandipan Das 			 */
33384d56a76eSSandipan Das 			if (prog->aux->func_cnt) {
33394d56a76eSSandipan Das 				u32 len, free, i;
33404d56a76eSSandipan Das 				u8 *img;
33414d56a76eSSandipan Das 
33424d56a76eSSandipan Das 				free = ulen;
33434d56a76eSSandipan Das 				for (i = 0; i < prog->aux->func_cnt; i++) {
33444d56a76eSSandipan Das 					len = prog->aux->func[i]->jited_len;
33454d56a76eSSandipan Das 					len = min_t(u32, len, free);
33464d56a76eSSandipan Das 					img = (u8 *) prog->aux->func[i]->bpf_func;
33474d56a76eSSandipan Das 					if (copy_to_user(uinsns, img, len))
33484d56a76eSSandipan Das 						return -EFAULT;
33494d56a76eSSandipan Das 					uinsns += len;
33504d56a76eSSandipan Das 					free -= len;
33514d56a76eSSandipan Das 					if (!free)
33524d56a76eSSandipan Das 						break;
33534d56a76eSSandipan Das 				}
33544d56a76eSSandipan Das 			} else {
3355fcfb126dSJiong Wang 				if (copy_to_user(uinsns, prog->bpf_func, ulen))
3356fcfb126dSJiong Wang 					return -EFAULT;
33574d56a76eSSandipan Das 			}
3358fcfb126dSJiong Wang 		} else {
3359fcfb126dSJiong Wang 			info.jited_prog_insns = 0;
3360fcfb126dSJiong Wang 		}
3361675fc275SJakub Kicinski 	}
3362675fc275SJakub Kicinski 
3363dbecd738SSandipan Das 	ulen = info.nr_jited_ksyms;
3364ff1889fcSSong Liu 	info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
33657a5725ddSSong Liu 	if (ulen) {
3366dbecd738SSandipan Das 		if (bpf_dump_raw_ok()) {
3367ff1889fcSSong Liu 			unsigned long ksym_addr;
3368dbecd738SSandipan Das 			u64 __user *user_ksyms;
3369dbecd738SSandipan Das 			u32 i;
3370dbecd738SSandipan Das 
3371dbecd738SSandipan Das 			/* copy the address of the kernel symbol
3372dbecd738SSandipan Das 			 * corresponding to each function
3373dbecd738SSandipan Das 			 */
3374dbecd738SSandipan Das 			ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3375dbecd738SSandipan Das 			user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3376ff1889fcSSong Liu 			if (prog->aux->func_cnt) {
3377dbecd738SSandipan Das 				for (i = 0; i < ulen; i++) {
3378ff1889fcSSong Liu 					ksym_addr = (unsigned long)
3379ff1889fcSSong Liu 						prog->aux->func[i]->bpf_func;
3380ff1889fcSSong Liu 					if (put_user((u64) ksym_addr,
3381ff1889fcSSong Liu 						     &user_ksyms[i]))
3382ff1889fcSSong Liu 						return -EFAULT;
3383ff1889fcSSong Liu 				}
3384ff1889fcSSong Liu 			} else {
3385ff1889fcSSong Liu 				ksym_addr = (unsigned long) prog->bpf_func;
3386ff1889fcSSong Liu 				if (put_user((u64) ksym_addr, &user_ksyms[0]))
3387dbecd738SSandipan Das 					return -EFAULT;
3388dbecd738SSandipan Das 			}
3389dbecd738SSandipan Das 		} else {
3390dbecd738SSandipan Das 			info.jited_ksyms = 0;
3391dbecd738SSandipan Das 		}
3392dbecd738SSandipan Das 	}
3393dbecd738SSandipan Das 
3394815581c1SSandipan Das 	ulen = info.nr_jited_func_lens;
3395ff1889fcSSong Liu 	info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
33967a5725ddSSong Liu 	if (ulen) {
3397815581c1SSandipan Das 		if (bpf_dump_raw_ok()) {
3398815581c1SSandipan Das 			u32 __user *user_lens;
3399815581c1SSandipan Das 			u32 func_len, i;
3400815581c1SSandipan Das 
3401815581c1SSandipan Das 			/* copy the JITed image lengths for each function */
3402815581c1SSandipan Das 			ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3403815581c1SSandipan Das 			user_lens = u64_to_user_ptr(info.jited_func_lens);
3404ff1889fcSSong Liu 			if (prog->aux->func_cnt) {
3405815581c1SSandipan Das 				for (i = 0; i < ulen; i++) {
3406ff1889fcSSong Liu 					func_len =
3407ff1889fcSSong Liu 						prog->aux->func[i]->jited_len;
3408815581c1SSandipan Das 					if (put_user(func_len, &user_lens[i]))
3409815581c1SSandipan Das 						return -EFAULT;
3410815581c1SSandipan Das 				}
3411815581c1SSandipan Das 			} else {
3412ff1889fcSSong Liu 				func_len = prog->jited_len;
3413ff1889fcSSong Liu 				if (put_user(func_len, &user_lens[0]))
3414ff1889fcSSong Liu 					return -EFAULT;
3415ff1889fcSSong Liu 			}
3416ff1889fcSSong Liu 		} else {
3417815581c1SSandipan Das 			info.jited_func_lens = 0;
3418815581c1SSandipan Das 		}
3419815581c1SSandipan Das 	}
3420815581c1SSandipan Das 
34217337224fSMartin KaFai Lau 	if (prog->aux->btf)
3422838e9690SYonghong Song 		info.btf_id = btf_id(prog->aux->btf);
3423838e9690SYonghong Song 
342411d8b82dSYonghong Song 	ulen = info.nr_func_info;
342511d8b82dSYonghong Song 	info.nr_func_info = prog->aux->func_info_cnt;
342611d8b82dSYonghong Song 	if (info.nr_func_info && ulen) {
3427838e9690SYonghong Song 		char __user *user_finfo;
3428838e9690SYonghong Song 
3429838e9690SYonghong Song 		user_finfo = u64_to_user_ptr(info.func_info);
343011d8b82dSYonghong Song 		ulen = min_t(u32, info.nr_func_info, ulen);
3431ba64e7d8SYonghong Song 		if (copy_to_user(user_finfo, prog->aux->func_info,
34327337224fSMartin KaFai Lau 				 info.func_info_rec_size * ulen))
3433838e9690SYonghong Song 			return -EFAULT;
3434838e9690SYonghong Song 	}
3435838e9690SYonghong Song 
343611d8b82dSYonghong Song 	ulen = info.nr_line_info;
343711d8b82dSYonghong Song 	info.nr_line_info = prog->aux->nr_linfo;
343811d8b82dSYonghong Song 	if (info.nr_line_info && ulen) {
3439c454a46bSMartin KaFai Lau 		__u8 __user *user_linfo;
3440c454a46bSMartin KaFai Lau 
3441c454a46bSMartin KaFai Lau 		user_linfo = u64_to_user_ptr(info.line_info);
344211d8b82dSYonghong Song 		ulen = min_t(u32, info.nr_line_info, ulen);
3443c454a46bSMartin KaFai Lau 		if (copy_to_user(user_linfo, prog->aux->linfo,
3444c454a46bSMartin KaFai Lau 				 info.line_info_rec_size * ulen))
3445c454a46bSMartin KaFai Lau 			return -EFAULT;
3446c454a46bSMartin KaFai Lau 	}
3447c454a46bSMartin KaFai Lau 
344811d8b82dSYonghong Song 	ulen = info.nr_jited_line_info;
3449c454a46bSMartin KaFai Lau 	if (prog->aux->jited_linfo)
345011d8b82dSYonghong Song 		info.nr_jited_line_info = prog->aux->nr_linfo;
3451c454a46bSMartin KaFai Lau 	else
345211d8b82dSYonghong Song 		info.nr_jited_line_info = 0;
345311d8b82dSYonghong Song 	if (info.nr_jited_line_info && ulen) {
3454c454a46bSMartin KaFai Lau 		if (bpf_dump_raw_ok()) {
3455c454a46bSMartin KaFai Lau 			__u64 __user *user_linfo;
3456c454a46bSMartin KaFai Lau 			u32 i;
3457c454a46bSMartin KaFai Lau 
3458c454a46bSMartin KaFai Lau 			user_linfo = u64_to_user_ptr(info.jited_line_info);
345911d8b82dSYonghong Song 			ulen = min_t(u32, info.nr_jited_line_info, ulen);
3460c454a46bSMartin KaFai Lau 			for (i = 0; i < ulen; i++) {
3461c454a46bSMartin KaFai Lau 				if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3462c454a46bSMartin KaFai Lau 					     &user_linfo[i]))
3463c454a46bSMartin KaFai Lau 					return -EFAULT;
3464c454a46bSMartin KaFai Lau 			}
3465c454a46bSMartin KaFai Lau 		} else {
3466c454a46bSMartin KaFai Lau 			info.jited_line_info = 0;
3467c454a46bSMartin KaFai Lau 		}
3468c454a46bSMartin KaFai Lau 	}
3469c454a46bSMartin KaFai Lau 
3470c872bdb3SSong Liu 	ulen = info.nr_prog_tags;
3471c872bdb3SSong Liu 	info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3472c872bdb3SSong Liu 	if (ulen) {
3473c872bdb3SSong Liu 		__u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3474c872bdb3SSong Liu 		u32 i;
3475c872bdb3SSong Liu 
3476c872bdb3SSong Liu 		user_prog_tags = u64_to_user_ptr(info.prog_tags);
3477c872bdb3SSong Liu 		ulen = min_t(u32, info.nr_prog_tags, ulen);
3478c872bdb3SSong Liu 		if (prog->aux->func_cnt) {
3479c872bdb3SSong Liu 			for (i = 0; i < ulen; i++) {
3480c872bdb3SSong Liu 				if (copy_to_user(user_prog_tags[i],
3481c872bdb3SSong Liu 						 prog->aux->func[i]->tag,
3482c872bdb3SSong Liu 						 BPF_TAG_SIZE))
3483c872bdb3SSong Liu 					return -EFAULT;
3484c872bdb3SSong Liu 			}
3485c872bdb3SSong Liu 		} else {
3486c872bdb3SSong Liu 			if (copy_to_user(user_prog_tags[0],
3487c872bdb3SSong Liu 					 prog->tag, BPF_TAG_SIZE))
3488c872bdb3SSong Liu 				return -EFAULT;
3489c872bdb3SSong Liu 		}
3490c872bdb3SSong Liu 	}
3491c872bdb3SSong Liu 
34921e270976SMartin KaFai Lau done:
34931e270976SMartin KaFai Lau 	if (copy_to_user(uinfo, &info, info_len) ||
34941e270976SMartin KaFai Lau 	    put_user(info_len, &uattr->info.info_len))
34951e270976SMartin KaFai Lau 		return -EFAULT;
34961e270976SMartin KaFai Lau 
34971e270976SMartin KaFai Lau 	return 0;
34981e270976SMartin KaFai Lau }
34991e270976SMartin KaFai Lau 
35001e270976SMartin KaFai Lau static int bpf_map_get_info_by_fd(struct bpf_map *map,
35011e270976SMartin KaFai Lau 				  const union bpf_attr *attr,
35021e270976SMartin KaFai Lau 				  union bpf_attr __user *uattr)
35031e270976SMartin KaFai Lau {
35041e270976SMartin KaFai Lau 	struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
35055c6f2588SGreg Kroah-Hartman 	struct bpf_map_info info;
35061e270976SMartin KaFai Lau 	u32 info_len = attr->info.info_len;
35071e270976SMartin KaFai Lau 	int err;
35081e270976SMartin KaFai Lau 
3509dcab51f1SMartin KaFai Lau 	err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
35101e270976SMartin KaFai Lau 	if (err)
35111e270976SMartin KaFai Lau 		return err;
35121e270976SMartin KaFai Lau 	info_len = min_t(u32, sizeof(info), info_len);
35131e270976SMartin KaFai Lau 
35145c6f2588SGreg Kroah-Hartman 	memset(&info, 0, sizeof(info));
35151e270976SMartin KaFai Lau 	info.type = map->map_type;
35161e270976SMartin KaFai Lau 	info.id = map->id;
35171e270976SMartin KaFai Lau 	info.key_size = map->key_size;
35181e270976SMartin KaFai Lau 	info.value_size = map->value_size;
35191e270976SMartin KaFai Lau 	info.max_entries = map->max_entries;
35201e270976SMartin KaFai Lau 	info.map_flags = map->map_flags;
3521ad5b177bSMartin KaFai Lau 	memcpy(info.name, map->name, sizeof(map->name));
35221e270976SMartin KaFai Lau 
352378958fcaSMartin KaFai Lau 	if (map->btf) {
352478958fcaSMartin KaFai Lau 		info.btf_id = btf_id(map->btf);
35259b2cf328SMartin KaFai Lau 		info.btf_key_type_id = map->btf_key_type_id;
35269b2cf328SMartin KaFai Lau 		info.btf_value_type_id = map->btf_value_type_id;
352778958fcaSMartin KaFai Lau 	}
352885d33df3SMartin KaFai Lau 	info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
352978958fcaSMartin KaFai Lau 
353052775b33SJakub Kicinski 	if (bpf_map_is_dev_bound(map)) {
353152775b33SJakub Kicinski 		err = bpf_map_offload_info_fill(&info, map);
353252775b33SJakub Kicinski 		if (err)
353352775b33SJakub Kicinski 			return err;
353452775b33SJakub Kicinski 	}
353552775b33SJakub Kicinski 
35361e270976SMartin KaFai Lau 	if (copy_to_user(uinfo, &info, info_len) ||
35371e270976SMartin KaFai Lau 	    put_user(info_len, &uattr->info.info_len))
35381e270976SMartin KaFai Lau 		return -EFAULT;
35391e270976SMartin KaFai Lau 
35401e270976SMartin KaFai Lau 	return 0;
35411e270976SMartin KaFai Lau }
35421e270976SMartin KaFai Lau 
354362dab84cSMartin KaFai Lau static int bpf_btf_get_info_by_fd(struct btf *btf,
354462dab84cSMartin KaFai Lau 				  const union bpf_attr *attr,
354562dab84cSMartin KaFai Lau 				  union bpf_attr __user *uattr)
354662dab84cSMartin KaFai Lau {
354762dab84cSMartin KaFai Lau 	struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
354862dab84cSMartin KaFai Lau 	u32 info_len = attr->info.info_len;
354962dab84cSMartin KaFai Lau 	int err;
355062dab84cSMartin KaFai Lau 
3551dcab51f1SMartin KaFai Lau 	err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
355262dab84cSMartin KaFai Lau 	if (err)
355362dab84cSMartin KaFai Lau 		return err;
355462dab84cSMartin KaFai Lau 
355562dab84cSMartin KaFai Lau 	return btf_get_info_by_fd(btf, attr, uattr);
355662dab84cSMartin KaFai Lau }
355762dab84cSMartin KaFai Lau 
3558f2e10bffSAndrii Nakryiko static int bpf_link_get_info_by_fd(struct bpf_link *link,
3559f2e10bffSAndrii Nakryiko 				  const union bpf_attr *attr,
3560f2e10bffSAndrii Nakryiko 				  union bpf_attr __user *uattr)
3561f2e10bffSAndrii Nakryiko {
3562f2e10bffSAndrii Nakryiko 	struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3563f2e10bffSAndrii Nakryiko 	struct bpf_link_info info;
3564f2e10bffSAndrii Nakryiko 	u32 info_len = attr->info.info_len;
3565f2e10bffSAndrii Nakryiko 	int err;
3566f2e10bffSAndrii Nakryiko 
3567f2e10bffSAndrii Nakryiko 	err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
3568f2e10bffSAndrii Nakryiko 	if (err)
3569f2e10bffSAndrii Nakryiko 		return err;
3570f2e10bffSAndrii Nakryiko 	info_len = min_t(u32, sizeof(info), info_len);
3571f2e10bffSAndrii Nakryiko 
3572f2e10bffSAndrii Nakryiko 	memset(&info, 0, sizeof(info));
3573f2e10bffSAndrii Nakryiko 	if (copy_from_user(&info, uinfo, info_len))
3574f2e10bffSAndrii Nakryiko 		return -EFAULT;
3575f2e10bffSAndrii Nakryiko 
3576f2e10bffSAndrii Nakryiko 	info.type = link->type;
3577f2e10bffSAndrii Nakryiko 	info.id = link->id;
3578f2e10bffSAndrii Nakryiko 	info.prog_id = link->prog->aux->id;
3579f2e10bffSAndrii Nakryiko 
3580f2e10bffSAndrii Nakryiko 	if (link->ops->fill_link_info) {
3581f2e10bffSAndrii Nakryiko 		err = link->ops->fill_link_info(link, &info);
3582f2e10bffSAndrii Nakryiko 		if (err)
3583f2e10bffSAndrii Nakryiko 			return err;
3584f2e10bffSAndrii Nakryiko 	}
3585f2e10bffSAndrii Nakryiko 
3586f2e10bffSAndrii Nakryiko 	if (copy_to_user(uinfo, &info, info_len) ||
3587f2e10bffSAndrii Nakryiko 	    put_user(info_len, &uattr->info.info_len))
3588f2e10bffSAndrii Nakryiko 		return -EFAULT;
3589f2e10bffSAndrii Nakryiko 
3590f2e10bffSAndrii Nakryiko 	return 0;
3591f2e10bffSAndrii Nakryiko }
3592f2e10bffSAndrii Nakryiko 
3593f2e10bffSAndrii Nakryiko 
35941e270976SMartin KaFai Lau #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
35951e270976SMartin KaFai Lau 
35961e270976SMartin KaFai Lau static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
35971e270976SMartin KaFai Lau 				  union bpf_attr __user *uattr)
35981e270976SMartin KaFai Lau {
35991e270976SMartin KaFai Lau 	int ufd = attr->info.bpf_fd;
36001e270976SMartin KaFai Lau 	struct fd f;
36011e270976SMartin KaFai Lau 	int err;
36021e270976SMartin KaFai Lau 
36031e270976SMartin KaFai Lau 	if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
36041e270976SMartin KaFai Lau 		return -EINVAL;
36051e270976SMartin KaFai Lau 
36061e270976SMartin KaFai Lau 	f = fdget(ufd);
36071e270976SMartin KaFai Lau 	if (!f.file)
36081e270976SMartin KaFai Lau 		return -EBADFD;
36091e270976SMartin KaFai Lau 
36101e270976SMartin KaFai Lau 	if (f.file->f_op == &bpf_prog_fops)
36111e270976SMartin KaFai Lau 		err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
36121e270976SMartin KaFai Lau 					      uattr);
36131e270976SMartin KaFai Lau 	else if (f.file->f_op == &bpf_map_fops)
36141e270976SMartin KaFai Lau 		err = bpf_map_get_info_by_fd(f.file->private_data, attr,
36151e270976SMartin KaFai Lau 					     uattr);
361660197cfbSMartin KaFai Lau 	else if (f.file->f_op == &btf_fops)
361762dab84cSMartin KaFai Lau 		err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
3618f2e10bffSAndrii Nakryiko 	else if (f.file->f_op == &bpf_link_fops)
3619f2e10bffSAndrii Nakryiko 		err = bpf_link_get_info_by_fd(f.file->private_data,
3620f2e10bffSAndrii Nakryiko 					      attr, uattr);
36211e270976SMartin KaFai Lau 	else
36221e270976SMartin KaFai Lau 		err = -EINVAL;
36231e270976SMartin KaFai Lau 
36241e270976SMartin KaFai Lau 	fdput(f);
36251e270976SMartin KaFai Lau 	return err;
36261e270976SMartin KaFai Lau }
36271e270976SMartin KaFai Lau 
3628f56a653cSMartin KaFai Lau #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3629f56a653cSMartin KaFai Lau 
3630f56a653cSMartin KaFai Lau static int bpf_btf_load(const union bpf_attr *attr)
3631f56a653cSMartin KaFai Lau {
3632f56a653cSMartin KaFai Lau 	if (CHECK_ATTR(BPF_BTF_LOAD))
3633f56a653cSMartin KaFai Lau 		return -EINVAL;
3634f56a653cSMartin KaFai Lau 
36352c78ee89SAlexei Starovoitov 	if (!bpf_capable())
3636f56a653cSMartin KaFai Lau 		return -EPERM;
3637f56a653cSMartin KaFai Lau 
3638f56a653cSMartin KaFai Lau 	return btf_new_fd(attr);
3639f56a653cSMartin KaFai Lau }
3640f56a653cSMartin KaFai Lau 
364178958fcaSMartin KaFai Lau #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
364278958fcaSMartin KaFai Lau 
364378958fcaSMartin KaFai Lau static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
364478958fcaSMartin KaFai Lau {
364578958fcaSMartin KaFai Lau 	if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
364678958fcaSMartin KaFai Lau 		return -EINVAL;
364778958fcaSMartin KaFai Lau 
364878958fcaSMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN))
364978958fcaSMartin KaFai Lau 		return -EPERM;
365078958fcaSMartin KaFai Lau 
365178958fcaSMartin KaFai Lau 	return btf_get_fd_by_id(attr->btf_id);
365278958fcaSMartin KaFai Lau }
365378958fcaSMartin KaFai Lau 
365441bdc4b4SYonghong Song static int bpf_task_fd_query_copy(const union bpf_attr *attr,
365541bdc4b4SYonghong Song 				    union bpf_attr __user *uattr,
365641bdc4b4SYonghong Song 				    u32 prog_id, u32 fd_type,
365741bdc4b4SYonghong Song 				    const char *buf, u64 probe_offset,
365841bdc4b4SYonghong Song 				    u64 probe_addr)
365941bdc4b4SYonghong Song {
366041bdc4b4SYonghong Song 	char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
366141bdc4b4SYonghong Song 	u32 len = buf ? strlen(buf) : 0, input_len;
366241bdc4b4SYonghong Song 	int err = 0;
366341bdc4b4SYonghong Song 
366441bdc4b4SYonghong Song 	if (put_user(len, &uattr->task_fd_query.buf_len))
366541bdc4b4SYonghong Song 		return -EFAULT;
366641bdc4b4SYonghong Song 	input_len = attr->task_fd_query.buf_len;
366741bdc4b4SYonghong Song 	if (input_len && ubuf) {
366841bdc4b4SYonghong Song 		if (!len) {
366941bdc4b4SYonghong Song 			/* nothing to copy, just make ubuf NULL terminated */
367041bdc4b4SYonghong Song 			char zero = '\0';
367141bdc4b4SYonghong Song 
367241bdc4b4SYonghong Song 			if (put_user(zero, ubuf))
367341bdc4b4SYonghong Song 				return -EFAULT;
367441bdc4b4SYonghong Song 		} else if (input_len >= len + 1) {
367541bdc4b4SYonghong Song 			/* ubuf can hold the string with NULL terminator */
367641bdc4b4SYonghong Song 			if (copy_to_user(ubuf, buf, len + 1))
367741bdc4b4SYonghong Song 				return -EFAULT;
367841bdc4b4SYonghong Song 		} else {
367941bdc4b4SYonghong Song 			/* ubuf cannot hold the string with NULL terminator,
368041bdc4b4SYonghong Song 			 * do a partial copy with NULL terminator.
368141bdc4b4SYonghong Song 			 */
368241bdc4b4SYonghong Song 			char zero = '\0';
368341bdc4b4SYonghong Song 
368441bdc4b4SYonghong Song 			err = -ENOSPC;
368541bdc4b4SYonghong Song 			if (copy_to_user(ubuf, buf, input_len - 1))
368641bdc4b4SYonghong Song 				return -EFAULT;
368741bdc4b4SYonghong Song 			if (put_user(zero, ubuf + input_len - 1))
368841bdc4b4SYonghong Song 				return -EFAULT;
368941bdc4b4SYonghong Song 		}
369041bdc4b4SYonghong Song 	}
369141bdc4b4SYonghong Song 
369241bdc4b4SYonghong Song 	if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
369341bdc4b4SYonghong Song 	    put_user(fd_type, &uattr->task_fd_query.fd_type) ||
369441bdc4b4SYonghong Song 	    put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
369541bdc4b4SYonghong Song 	    put_user(probe_addr, &uattr->task_fd_query.probe_addr))
369641bdc4b4SYonghong Song 		return -EFAULT;
369741bdc4b4SYonghong Song 
369841bdc4b4SYonghong Song 	return err;
369941bdc4b4SYonghong Song }
370041bdc4b4SYonghong Song 
370141bdc4b4SYonghong Song #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
370241bdc4b4SYonghong Song 
370341bdc4b4SYonghong Song static int bpf_task_fd_query(const union bpf_attr *attr,
370441bdc4b4SYonghong Song 			     union bpf_attr __user *uattr)
370541bdc4b4SYonghong Song {
370641bdc4b4SYonghong Song 	pid_t pid = attr->task_fd_query.pid;
370741bdc4b4SYonghong Song 	u32 fd = attr->task_fd_query.fd;
370841bdc4b4SYonghong Song 	const struct perf_event *event;
370941bdc4b4SYonghong Song 	struct files_struct *files;
371041bdc4b4SYonghong Song 	struct task_struct *task;
371141bdc4b4SYonghong Song 	struct file *file;
371241bdc4b4SYonghong Song 	int err;
371341bdc4b4SYonghong Song 
371441bdc4b4SYonghong Song 	if (CHECK_ATTR(BPF_TASK_FD_QUERY))
371541bdc4b4SYonghong Song 		return -EINVAL;
371641bdc4b4SYonghong Song 
371741bdc4b4SYonghong Song 	if (!capable(CAP_SYS_ADMIN))
371841bdc4b4SYonghong Song 		return -EPERM;
371941bdc4b4SYonghong Song 
372041bdc4b4SYonghong Song 	if (attr->task_fd_query.flags != 0)
372141bdc4b4SYonghong Song 		return -EINVAL;
372241bdc4b4SYonghong Song 
372341bdc4b4SYonghong Song 	task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
372441bdc4b4SYonghong Song 	if (!task)
372541bdc4b4SYonghong Song 		return -ENOENT;
372641bdc4b4SYonghong Song 
372741bdc4b4SYonghong Song 	files = get_files_struct(task);
372841bdc4b4SYonghong Song 	put_task_struct(task);
372941bdc4b4SYonghong Song 	if (!files)
373041bdc4b4SYonghong Song 		return -ENOENT;
373141bdc4b4SYonghong Song 
373241bdc4b4SYonghong Song 	err = 0;
373341bdc4b4SYonghong Song 	spin_lock(&files->file_lock);
373441bdc4b4SYonghong Song 	file = fcheck_files(files, fd);
373541bdc4b4SYonghong Song 	if (!file)
373641bdc4b4SYonghong Song 		err = -EBADF;
373741bdc4b4SYonghong Song 	else
373841bdc4b4SYonghong Song 		get_file(file);
373941bdc4b4SYonghong Song 	spin_unlock(&files->file_lock);
374041bdc4b4SYonghong Song 	put_files_struct(files);
374141bdc4b4SYonghong Song 
374241bdc4b4SYonghong Song 	if (err)
374341bdc4b4SYonghong Song 		goto out;
374441bdc4b4SYonghong Song 
374570ed506cSAndrii Nakryiko 	if (file->f_op == &bpf_link_fops) {
374670ed506cSAndrii Nakryiko 		struct bpf_link *link = file->private_data;
374770ed506cSAndrii Nakryiko 
3748a3b80e10SAndrii Nakryiko 		if (link->ops == &bpf_raw_tp_link_lops) {
374970ed506cSAndrii Nakryiko 			struct bpf_raw_tp_link *raw_tp =
375070ed506cSAndrii Nakryiko 				container_of(link, struct bpf_raw_tp_link, link);
375141bdc4b4SYonghong Song 			struct bpf_raw_event_map *btp = raw_tp->btp;
375241bdc4b4SYonghong Song 
375341bdc4b4SYonghong Song 			err = bpf_task_fd_query_copy(attr, uattr,
375470ed506cSAndrii Nakryiko 						     raw_tp->link.prog->aux->id,
375541bdc4b4SYonghong Song 						     BPF_FD_TYPE_RAW_TRACEPOINT,
375641bdc4b4SYonghong Song 						     btp->tp->name, 0, 0);
375741bdc4b4SYonghong Song 			goto put_file;
375841bdc4b4SYonghong Song 		}
375970ed506cSAndrii Nakryiko 		goto out_not_supp;
376070ed506cSAndrii Nakryiko 	}
376141bdc4b4SYonghong Song 
376241bdc4b4SYonghong Song 	event = perf_get_event(file);
376341bdc4b4SYonghong Song 	if (!IS_ERR(event)) {
376441bdc4b4SYonghong Song 		u64 probe_offset, probe_addr;
376541bdc4b4SYonghong Song 		u32 prog_id, fd_type;
376641bdc4b4SYonghong Song 		const char *buf;
376741bdc4b4SYonghong Song 
376841bdc4b4SYonghong Song 		err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
376941bdc4b4SYonghong Song 					      &buf, &probe_offset,
377041bdc4b4SYonghong Song 					      &probe_addr);
377141bdc4b4SYonghong Song 		if (!err)
377241bdc4b4SYonghong Song 			err = bpf_task_fd_query_copy(attr, uattr, prog_id,
377341bdc4b4SYonghong Song 						     fd_type, buf,
377441bdc4b4SYonghong Song 						     probe_offset,
377541bdc4b4SYonghong Song 						     probe_addr);
377641bdc4b4SYonghong Song 		goto put_file;
377741bdc4b4SYonghong Song 	}
377841bdc4b4SYonghong Song 
377970ed506cSAndrii Nakryiko out_not_supp:
378041bdc4b4SYonghong Song 	err = -ENOTSUPP;
378141bdc4b4SYonghong Song put_file:
378241bdc4b4SYonghong Song 	fput(file);
378341bdc4b4SYonghong Song out:
378441bdc4b4SYonghong Song 	return err;
378541bdc4b4SYonghong Song }
378641bdc4b4SYonghong Song 
3787cb4d03abSBrian Vazquez #define BPF_MAP_BATCH_LAST_FIELD batch.flags
3788cb4d03abSBrian Vazquez 
3789cb4d03abSBrian Vazquez #define BPF_DO_BATCH(fn)			\
3790cb4d03abSBrian Vazquez 	do {					\
3791cb4d03abSBrian Vazquez 		if (!fn) {			\
3792cb4d03abSBrian Vazquez 			err = -ENOTSUPP;	\
3793cb4d03abSBrian Vazquez 			goto err_put;		\
3794cb4d03abSBrian Vazquez 		}				\
3795cb4d03abSBrian Vazquez 		err = fn(map, attr, uattr);	\
3796cb4d03abSBrian Vazquez 	} while (0)
3797cb4d03abSBrian Vazquez 
3798cb4d03abSBrian Vazquez static int bpf_map_do_batch(const union bpf_attr *attr,
3799cb4d03abSBrian Vazquez 			    union bpf_attr __user *uattr,
3800cb4d03abSBrian Vazquez 			    int cmd)
3801cb4d03abSBrian Vazquez {
3802cb4d03abSBrian Vazquez 	struct bpf_map *map;
3803cb4d03abSBrian Vazquez 	int err, ufd;
3804cb4d03abSBrian Vazquez 	struct fd f;
3805cb4d03abSBrian Vazquez 
3806cb4d03abSBrian Vazquez 	if (CHECK_ATTR(BPF_MAP_BATCH))
3807cb4d03abSBrian Vazquez 		return -EINVAL;
3808cb4d03abSBrian Vazquez 
3809cb4d03abSBrian Vazquez 	ufd = attr->batch.map_fd;
3810cb4d03abSBrian Vazquez 	f = fdget(ufd);
3811cb4d03abSBrian Vazquez 	map = __bpf_map_get(f);
3812cb4d03abSBrian Vazquez 	if (IS_ERR(map))
3813cb4d03abSBrian Vazquez 		return PTR_ERR(map);
3814cb4d03abSBrian Vazquez 
381505799638SYonghong Song 	if ((cmd == BPF_MAP_LOOKUP_BATCH ||
381605799638SYonghong Song 	     cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
3817cb4d03abSBrian Vazquez 	    !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
3818cb4d03abSBrian Vazquez 		err = -EPERM;
3819cb4d03abSBrian Vazquez 		goto err_put;
3820cb4d03abSBrian Vazquez 	}
3821cb4d03abSBrian Vazquez 
3822cb4d03abSBrian Vazquez 	if (cmd != BPF_MAP_LOOKUP_BATCH &&
3823cb4d03abSBrian Vazquez 	    !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
3824cb4d03abSBrian Vazquez 		err = -EPERM;
3825cb4d03abSBrian Vazquez 		goto err_put;
3826cb4d03abSBrian Vazquez 	}
3827cb4d03abSBrian Vazquez 
3828cb4d03abSBrian Vazquez 	if (cmd == BPF_MAP_LOOKUP_BATCH)
3829cb4d03abSBrian Vazquez 		BPF_DO_BATCH(map->ops->map_lookup_batch);
383005799638SYonghong Song 	else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
383105799638SYonghong Song 		BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
3832aa2e93b8SBrian Vazquez 	else if (cmd == BPF_MAP_UPDATE_BATCH)
3833aa2e93b8SBrian Vazquez 		BPF_DO_BATCH(map->ops->map_update_batch);
3834aa2e93b8SBrian Vazquez 	else
3835aa2e93b8SBrian Vazquez 		BPF_DO_BATCH(map->ops->map_delete_batch);
3836cb4d03abSBrian Vazquez 
3837cb4d03abSBrian Vazquez err_put:
3838cb4d03abSBrian Vazquez 	fdput(f);
3839cb4d03abSBrian Vazquez 	return err;
3840cb4d03abSBrian Vazquez }
3841cb4d03abSBrian Vazquez 
3842de4e05caSYonghong Song static int tracing_bpf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3843de4e05caSYonghong Song {
3844de4e05caSYonghong Song 	if (attr->link_create.attach_type == BPF_TRACE_ITER &&
3845de4e05caSYonghong Song 	    prog->expected_attach_type == BPF_TRACE_ITER)
3846de4e05caSYonghong Song 		return bpf_iter_link_attach(attr, prog);
3847de4e05caSYonghong Song 
3848de4e05caSYonghong Song 	return -EINVAL;
3849de4e05caSYonghong Song }
3850de4e05caSYonghong Song 
3851af6eea57SAndrii Nakryiko #define BPF_LINK_CREATE_LAST_FIELD link_create.flags
3852af6eea57SAndrii Nakryiko static int link_create(union bpf_attr *attr)
3853af6eea57SAndrii Nakryiko {
3854af6eea57SAndrii Nakryiko 	enum bpf_prog_type ptype;
3855af6eea57SAndrii Nakryiko 	struct bpf_prog *prog;
3856af6eea57SAndrii Nakryiko 	int ret;
3857af6eea57SAndrii Nakryiko 
3858af6eea57SAndrii Nakryiko 	if (CHECK_ATTR(BPF_LINK_CREATE))
3859af6eea57SAndrii Nakryiko 		return -EINVAL;
3860af6eea57SAndrii Nakryiko 
3861af6eea57SAndrii Nakryiko 	ptype = attach_type_to_prog_type(attr->link_create.attach_type);
3862af6eea57SAndrii Nakryiko 	if (ptype == BPF_PROG_TYPE_UNSPEC)
3863af6eea57SAndrii Nakryiko 		return -EINVAL;
3864af6eea57SAndrii Nakryiko 
3865af6eea57SAndrii Nakryiko 	prog = bpf_prog_get_type(attr->link_create.prog_fd, ptype);
3866af6eea57SAndrii Nakryiko 	if (IS_ERR(prog))
3867af6eea57SAndrii Nakryiko 		return PTR_ERR(prog);
3868af6eea57SAndrii Nakryiko 
3869af6eea57SAndrii Nakryiko 	ret = bpf_prog_attach_check_attach_type(prog,
3870af6eea57SAndrii Nakryiko 						attr->link_create.attach_type);
3871af6eea57SAndrii Nakryiko 	if (ret)
3872af6eea57SAndrii Nakryiko 		goto err_out;
3873af6eea57SAndrii Nakryiko 
3874af6eea57SAndrii Nakryiko 	switch (ptype) {
3875af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SKB:
3876af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCK:
3877af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3878af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_SOCK_OPS:
3879af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_DEVICE:
3880af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
3881af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3882af6eea57SAndrii Nakryiko 		ret = cgroup_bpf_link_attach(attr, prog);
3883af6eea57SAndrii Nakryiko 		break;
3884de4e05caSYonghong Song 	case BPF_PROG_TYPE_TRACING:
3885de4e05caSYonghong Song 		ret = tracing_bpf_link_attach(attr, prog);
3886de4e05caSYonghong Song 		break;
38877f045a49SJakub Sitnicki 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
38887f045a49SJakub Sitnicki 		ret = netns_bpf_link_create(attr, prog);
38897f045a49SJakub Sitnicki 		break;
3890af6eea57SAndrii Nakryiko 	default:
3891af6eea57SAndrii Nakryiko 		ret = -EINVAL;
3892af6eea57SAndrii Nakryiko 	}
3893af6eea57SAndrii Nakryiko 
3894af6eea57SAndrii Nakryiko err_out:
3895af6eea57SAndrii Nakryiko 	if (ret < 0)
3896af6eea57SAndrii Nakryiko 		bpf_prog_put(prog);
3897af6eea57SAndrii Nakryiko 	return ret;
3898af6eea57SAndrii Nakryiko }
3899af6eea57SAndrii Nakryiko 
39000c991ebcSAndrii Nakryiko #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
39010c991ebcSAndrii Nakryiko 
39020c991ebcSAndrii Nakryiko static int link_update(union bpf_attr *attr)
39030c991ebcSAndrii Nakryiko {
39040c991ebcSAndrii Nakryiko 	struct bpf_prog *old_prog = NULL, *new_prog;
39050c991ebcSAndrii Nakryiko 	struct bpf_link *link;
39060c991ebcSAndrii Nakryiko 	u32 flags;
39070c991ebcSAndrii Nakryiko 	int ret;
39080c991ebcSAndrii Nakryiko 
39090c991ebcSAndrii Nakryiko 	if (CHECK_ATTR(BPF_LINK_UPDATE))
39100c991ebcSAndrii Nakryiko 		return -EINVAL;
39110c991ebcSAndrii Nakryiko 
39120c991ebcSAndrii Nakryiko 	flags = attr->link_update.flags;
39130c991ebcSAndrii Nakryiko 	if (flags & ~BPF_F_REPLACE)
39140c991ebcSAndrii Nakryiko 		return -EINVAL;
39150c991ebcSAndrii Nakryiko 
39160c991ebcSAndrii Nakryiko 	link = bpf_link_get_from_fd(attr->link_update.link_fd);
39170c991ebcSAndrii Nakryiko 	if (IS_ERR(link))
39180c991ebcSAndrii Nakryiko 		return PTR_ERR(link);
39190c991ebcSAndrii Nakryiko 
39200c991ebcSAndrii Nakryiko 	new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
39214adb7a4aSAndrii Nakryiko 	if (IS_ERR(new_prog)) {
39224adb7a4aSAndrii Nakryiko 		ret = PTR_ERR(new_prog);
39234adb7a4aSAndrii Nakryiko 		goto out_put_link;
39244adb7a4aSAndrii Nakryiko 	}
39250c991ebcSAndrii Nakryiko 
39260c991ebcSAndrii Nakryiko 	if (flags & BPF_F_REPLACE) {
39270c991ebcSAndrii Nakryiko 		old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
39280c991ebcSAndrii Nakryiko 		if (IS_ERR(old_prog)) {
39290c991ebcSAndrii Nakryiko 			ret = PTR_ERR(old_prog);
39300c991ebcSAndrii Nakryiko 			old_prog = NULL;
39310c991ebcSAndrii Nakryiko 			goto out_put_progs;
39320c991ebcSAndrii Nakryiko 		}
39334adb7a4aSAndrii Nakryiko 	} else if (attr->link_update.old_prog_fd) {
39344adb7a4aSAndrii Nakryiko 		ret = -EINVAL;
39354adb7a4aSAndrii Nakryiko 		goto out_put_progs;
39360c991ebcSAndrii Nakryiko 	}
39370c991ebcSAndrii Nakryiko 
3938f9d04127SAndrii Nakryiko 	if (link->ops->update_prog)
3939f9d04127SAndrii Nakryiko 		ret = link->ops->update_prog(link, new_prog, old_prog);
3940f9d04127SAndrii Nakryiko 	else
39410c991ebcSAndrii Nakryiko 		ret = -EINVAL;
39420c991ebcSAndrii Nakryiko 
39430c991ebcSAndrii Nakryiko out_put_progs:
39440c991ebcSAndrii Nakryiko 	if (old_prog)
39450c991ebcSAndrii Nakryiko 		bpf_prog_put(old_prog);
39460c991ebcSAndrii Nakryiko 	if (ret)
39470c991ebcSAndrii Nakryiko 		bpf_prog_put(new_prog);
39484adb7a4aSAndrii Nakryiko out_put_link:
39494adb7a4aSAndrii Nakryiko 	bpf_link_put(link);
39500c991ebcSAndrii Nakryiko 	return ret;
39510c991ebcSAndrii Nakryiko }
39520c991ebcSAndrii Nakryiko 
39532d602c8cSAndrii Nakryiko static int bpf_link_inc_not_zero(struct bpf_link *link)
39542d602c8cSAndrii Nakryiko {
39552d602c8cSAndrii Nakryiko 	return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? 0 : -ENOENT;
39562d602c8cSAndrii Nakryiko }
39572d602c8cSAndrii Nakryiko 
39582d602c8cSAndrii Nakryiko #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
39592d602c8cSAndrii Nakryiko 
39602d602c8cSAndrii Nakryiko static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
39612d602c8cSAndrii Nakryiko {
39622d602c8cSAndrii Nakryiko 	struct bpf_link *link;
39632d602c8cSAndrii Nakryiko 	u32 id = attr->link_id;
39642d602c8cSAndrii Nakryiko 	int fd, err;
39652d602c8cSAndrii Nakryiko 
39662d602c8cSAndrii Nakryiko 	if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
39672d602c8cSAndrii Nakryiko 		return -EINVAL;
39682d602c8cSAndrii Nakryiko 
39692d602c8cSAndrii Nakryiko 	if (!capable(CAP_SYS_ADMIN))
39702d602c8cSAndrii Nakryiko 		return -EPERM;
39712d602c8cSAndrii Nakryiko 
39722d602c8cSAndrii Nakryiko 	spin_lock_bh(&link_idr_lock);
39732d602c8cSAndrii Nakryiko 	link = idr_find(&link_idr, id);
39742d602c8cSAndrii Nakryiko 	/* before link is "settled", ID is 0, pretend it doesn't exist yet */
39752d602c8cSAndrii Nakryiko 	if (link) {
39762d602c8cSAndrii Nakryiko 		if (link->id)
39772d602c8cSAndrii Nakryiko 			err = bpf_link_inc_not_zero(link);
39782d602c8cSAndrii Nakryiko 		else
39792d602c8cSAndrii Nakryiko 			err = -EAGAIN;
39802d602c8cSAndrii Nakryiko 	} else {
39812d602c8cSAndrii Nakryiko 		err = -ENOENT;
39822d602c8cSAndrii Nakryiko 	}
39832d602c8cSAndrii Nakryiko 	spin_unlock_bh(&link_idr_lock);
39842d602c8cSAndrii Nakryiko 
39852d602c8cSAndrii Nakryiko 	if (err)
39862d602c8cSAndrii Nakryiko 		return err;
39872d602c8cSAndrii Nakryiko 
39882d602c8cSAndrii Nakryiko 	fd = bpf_link_new_fd(link);
39892d602c8cSAndrii Nakryiko 	if (fd < 0)
39902d602c8cSAndrii Nakryiko 		bpf_link_put(link);
39912d602c8cSAndrii Nakryiko 
39922d602c8cSAndrii Nakryiko 	return fd;
39932d602c8cSAndrii Nakryiko }
39942d602c8cSAndrii Nakryiko 
3995d46edd67SSong Liu DEFINE_MUTEX(bpf_stats_enabled_mutex);
3996d46edd67SSong Liu 
3997d46edd67SSong Liu static int bpf_stats_release(struct inode *inode, struct file *file)
3998d46edd67SSong Liu {
3999d46edd67SSong Liu 	mutex_lock(&bpf_stats_enabled_mutex);
4000d46edd67SSong Liu 	static_key_slow_dec(&bpf_stats_enabled_key.key);
4001d46edd67SSong Liu 	mutex_unlock(&bpf_stats_enabled_mutex);
4002d46edd67SSong Liu 	return 0;
4003d46edd67SSong Liu }
4004d46edd67SSong Liu 
4005d46edd67SSong Liu static const struct file_operations bpf_stats_fops = {
4006d46edd67SSong Liu 	.release = bpf_stats_release,
4007d46edd67SSong Liu };
4008d46edd67SSong Liu 
4009d46edd67SSong Liu static int bpf_enable_runtime_stats(void)
4010d46edd67SSong Liu {
4011d46edd67SSong Liu 	int fd;
4012d46edd67SSong Liu 
4013d46edd67SSong Liu 	mutex_lock(&bpf_stats_enabled_mutex);
4014d46edd67SSong Liu 
4015d46edd67SSong Liu 	/* Set a very high limit to avoid overflow */
4016d46edd67SSong Liu 	if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4017d46edd67SSong Liu 		mutex_unlock(&bpf_stats_enabled_mutex);
4018d46edd67SSong Liu 		return -EBUSY;
4019d46edd67SSong Liu 	}
4020d46edd67SSong Liu 
4021d46edd67SSong Liu 	fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4022d46edd67SSong Liu 	if (fd >= 0)
4023d46edd67SSong Liu 		static_key_slow_inc(&bpf_stats_enabled_key.key);
4024d46edd67SSong Liu 
4025d46edd67SSong Liu 	mutex_unlock(&bpf_stats_enabled_mutex);
4026d46edd67SSong Liu 	return fd;
4027d46edd67SSong Liu }
4028d46edd67SSong Liu 
4029d46edd67SSong Liu #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4030d46edd67SSong Liu 
4031d46edd67SSong Liu static int bpf_enable_stats(union bpf_attr *attr)
4032d46edd67SSong Liu {
4033d46edd67SSong Liu 
4034d46edd67SSong Liu 	if (CHECK_ATTR(BPF_ENABLE_STATS))
4035d46edd67SSong Liu 		return -EINVAL;
4036d46edd67SSong Liu 
4037d46edd67SSong Liu 	if (!capable(CAP_SYS_ADMIN))
4038d46edd67SSong Liu 		return -EPERM;
4039d46edd67SSong Liu 
4040d46edd67SSong Liu 	switch (attr->enable_stats.type) {
4041d46edd67SSong Liu 	case BPF_STATS_RUN_TIME:
4042d46edd67SSong Liu 		return bpf_enable_runtime_stats();
4043d46edd67SSong Liu 	default:
4044d46edd67SSong Liu 		break;
4045d46edd67SSong Liu 	}
4046d46edd67SSong Liu 	return -EINVAL;
4047d46edd67SSong Liu }
4048d46edd67SSong Liu 
4049ac51d99bSYonghong Song #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4050ac51d99bSYonghong Song 
4051ac51d99bSYonghong Song static int bpf_iter_create(union bpf_attr *attr)
4052ac51d99bSYonghong Song {
4053ac51d99bSYonghong Song 	struct bpf_link *link;
4054ac51d99bSYonghong Song 	int err;
4055ac51d99bSYonghong Song 
4056ac51d99bSYonghong Song 	if (CHECK_ATTR(BPF_ITER_CREATE))
4057ac51d99bSYonghong Song 		return -EINVAL;
4058ac51d99bSYonghong Song 
4059ac51d99bSYonghong Song 	if (attr->iter_create.flags)
4060ac51d99bSYonghong Song 		return -EINVAL;
4061ac51d99bSYonghong Song 
4062ac51d99bSYonghong Song 	link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4063ac51d99bSYonghong Song 	if (IS_ERR(link))
4064ac51d99bSYonghong Song 		return PTR_ERR(link);
4065ac51d99bSYonghong Song 
4066ac51d99bSYonghong Song 	err = bpf_iter_new_fd(link);
4067ac51d99bSYonghong Song 	bpf_link_put(link);
4068ac51d99bSYonghong Song 
4069ac51d99bSYonghong Song 	return err;
4070ac51d99bSYonghong Song }
4071ac51d99bSYonghong Song 
407299c55f7dSAlexei Starovoitov SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
407399c55f7dSAlexei Starovoitov {
40748096f229SGreg Kroah-Hartman 	union bpf_attr attr;
407599c55f7dSAlexei Starovoitov 	int err;
407699c55f7dSAlexei Starovoitov 
40772c78ee89SAlexei Starovoitov 	if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
407899c55f7dSAlexei Starovoitov 		return -EPERM;
407999c55f7dSAlexei Starovoitov 
4080dcab51f1SMartin KaFai Lau 	err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
408199c55f7dSAlexei Starovoitov 	if (err)
408299c55f7dSAlexei Starovoitov 		return err;
40831e270976SMartin KaFai Lau 	size = min_t(u32, size, sizeof(attr));
408499c55f7dSAlexei Starovoitov 
408599c55f7dSAlexei Starovoitov 	/* copy attributes from user space, may be less than sizeof(bpf_attr) */
40868096f229SGreg Kroah-Hartman 	memset(&attr, 0, sizeof(attr));
408799c55f7dSAlexei Starovoitov 	if (copy_from_user(&attr, uattr, size) != 0)
408899c55f7dSAlexei Starovoitov 		return -EFAULT;
408999c55f7dSAlexei Starovoitov 
4090afdb09c7SChenbo Feng 	err = security_bpf(cmd, &attr, size);
4091afdb09c7SChenbo Feng 	if (err < 0)
4092afdb09c7SChenbo Feng 		return err;
4093afdb09c7SChenbo Feng 
409499c55f7dSAlexei Starovoitov 	switch (cmd) {
409599c55f7dSAlexei Starovoitov 	case BPF_MAP_CREATE:
409699c55f7dSAlexei Starovoitov 		err = map_create(&attr);
409799c55f7dSAlexei Starovoitov 		break;
4098db20fd2bSAlexei Starovoitov 	case BPF_MAP_LOOKUP_ELEM:
4099db20fd2bSAlexei Starovoitov 		err = map_lookup_elem(&attr);
4100db20fd2bSAlexei Starovoitov 		break;
4101db20fd2bSAlexei Starovoitov 	case BPF_MAP_UPDATE_ELEM:
4102db20fd2bSAlexei Starovoitov 		err = map_update_elem(&attr);
4103db20fd2bSAlexei Starovoitov 		break;
4104db20fd2bSAlexei Starovoitov 	case BPF_MAP_DELETE_ELEM:
4105db20fd2bSAlexei Starovoitov 		err = map_delete_elem(&attr);
4106db20fd2bSAlexei Starovoitov 		break;
4107db20fd2bSAlexei Starovoitov 	case BPF_MAP_GET_NEXT_KEY:
4108db20fd2bSAlexei Starovoitov 		err = map_get_next_key(&attr);
4109db20fd2bSAlexei Starovoitov 		break;
411087df15deSDaniel Borkmann 	case BPF_MAP_FREEZE:
411187df15deSDaniel Borkmann 		err = map_freeze(&attr);
411287df15deSDaniel Borkmann 		break;
411309756af4SAlexei Starovoitov 	case BPF_PROG_LOAD:
4114838e9690SYonghong Song 		err = bpf_prog_load(&attr, uattr);
411509756af4SAlexei Starovoitov 		break;
4116b2197755SDaniel Borkmann 	case BPF_OBJ_PIN:
4117b2197755SDaniel Borkmann 		err = bpf_obj_pin(&attr);
4118b2197755SDaniel Borkmann 		break;
4119b2197755SDaniel Borkmann 	case BPF_OBJ_GET:
4120b2197755SDaniel Borkmann 		err = bpf_obj_get(&attr);
4121b2197755SDaniel Borkmann 		break;
4122f4324551SDaniel Mack 	case BPF_PROG_ATTACH:
4123f4324551SDaniel Mack 		err = bpf_prog_attach(&attr);
4124f4324551SDaniel Mack 		break;
4125f4324551SDaniel Mack 	case BPF_PROG_DETACH:
4126f4324551SDaniel Mack 		err = bpf_prog_detach(&attr);
4127f4324551SDaniel Mack 		break;
4128468e2f64SAlexei Starovoitov 	case BPF_PROG_QUERY:
4129468e2f64SAlexei Starovoitov 		err = bpf_prog_query(&attr, uattr);
4130468e2f64SAlexei Starovoitov 		break;
41311cf1cae9SAlexei Starovoitov 	case BPF_PROG_TEST_RUN:
41321cf1cae9SAlexei Starovoitov 		err = bpf_prog_test_run(&attr, uattr);
41331cf1cae9SAlexei Starovoitov 		break;
413434ad5580SMartin KaFai Lau 	case BPF_PROG_GET_NEXT_ID:
413534ad5580SMartin KaFai Lau 		err = bpf_obj_get_next_id(&attr, uattr,
413634ad5580SMartin KaFai Lau 					  &prog_idr, &prog_idr_lock);
413734ad5580SMartin KaFai Lau 		break;
413834ad5580SMartin KaFai Lau 	case BPF_MAP_GET_NEXT_ID:
413934ad5580SMartin KaFai Lau 		err = bpf_obj_get_next_id(&attr, uattr,
414034ad5580SMartin KaFai Lau 					  &map_idr, &map_idr_lock);
414134ad5580SMartin KaFai Lau 		break;
41421b9ed84eSQuentin Monnet 	case BPF_BTF_GET_NEXT_ID:
41431b9ed84eSQuentin Monnet 		err = bpf_obj_get_next_id(&attr, uattr,
41441b9ed84eSQuentin Monnet 					  &btf_idr, &btf_idr_lock);
41451b9ed84eSQuentin Monnet 		break;
4146b16d9aa4SMartin KaFai Lau 	case BPF_PROG_GET_FD_BY_ID:
4147b16d9aa4SMartin KaFai Lau 		err = bpf_prog_get_fd_by_id(&attr);
4148b16d9aa4SMartin KaFai Lau 		break;
4149bd5f5f4eSMartin KaFai Lau 	case BPF_MAP_GET_FD_BY_ID:
4150bd5f5f4eSMartin KaFai Lau 		err = bpf_map_get_fd_by_id(&attr);
4151bd5f5f4eSMartin KaFai Lau 		break;
41521e270976SMartin KaFai Lau 	case BPF_OBJ_GET_INFO_BY_FD:
41531e270976SMartin KaFai Lau 		err = bpf_obj_get_info_by_fd(&attr, uattr);
41541e270976SMartin KaFai Lau 		break;
4155c4f6699dSAlexei Starovoitov 	case BPF_RAW_TRACEPOINT_OPEN:
4156c4f6699dSAlexei Starovoitov 		err = bpf_raw_tracepoint_open(&attr);
4157c4f6699dSAlexei Starovoitov 		break;
4158f56a653cSMartin KaFai Lau 	case BPF_BTF_LOAD:
4159f56a653cSMartin KaFai Lau 		err = bpf_btf_load(&attr);
4160f56a653cSMartin KaFai Lau 		break;
416178958fcaSMartin KaFai Lau 	case BPF_BTF_GET_FD_BY_ID:
416278958fcaSMartin KaFai Lau 		err = bpf_btf_get_fd_by_id(&attr);
416378958fcaSMartin KaFai Lau 		break;
416441bdc4b4SYonghong Song 	case BPF_TASK_FD_QUERY:
416541bdc4b4SYonghong Song 		err = bpf_task_fd_query(&attr, uattr);
416641bdc4b4SYonghong Song 		break;
4167bd513cd0SMauricio Vasquez B 	case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4168bd513cd0SMauricio Vasquez B 		err = map_lookup_and_delete_elem(&attr);
4169bd513cd0SMauricio Vasquez B 		break;
4170cb4d03abSBrian Vazquez 	case BPF_MAP_LOOKUP_BATCH:
4171cb4d03abSBrian Vazquez 		err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
4172cb4d03abSBrian Vazquez 		break;
417305799638SYonghong Song 	case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
417405799638SYonghong Song 		err = bpf_map_do_batch(&attr, uattr,
417505799638SYonghong Song 				       BPF_MAP_LOOKUP_AND_DELETE_BATCH);
417605799638SYonghong Song 		break;
4177aa2e93b8SBrian Vazquez 	case BPF_MAP_UPDATE_BATCH:
4178aa2e93b8SBrian Vazquez 		err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
4179aa2e93b8SBrian Vazquez 		break;
4180aa2e93b8SBrian Vazquez 	case BPF_MAP_DELETE_BATCH:
4181aa2e93b8SBrian Vazquez 		err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
4182aa2e93b8SBrian Vazquez 		break;
4183af6eea57SAndrii Nakryiko 	case BPF_LINK_CREATE:
4184af6eea57SAndrii Nakryiko 		err = link_create(&attr);
4185af6eea57SAndrii Nakryiko 		break;
41860c991ebcSAndrii Nakryiko 	case BPF_LINK_UPDATE:
41870c991ebcSAndrii Nakryiko 		err = link_update(&attr);
41880c991ebcSAndrii Nakryiko 		break;
41892d602c8cSAndrii Nakryiko 	case BPF_LINK_GET_FD_BY_ID:
41902d602c8cSAndrii Nakryiko 		err = bpf_link_get_fd_by_id(&attr);
41912d602c8cSAndrii Nakryiko 		break;
41922d602c8cSAndrii Nakryiko 	case BPF_LINK_GET_NEXT_ID:
41932d602c8cSAndrii Nakryiko 		err = bpf_obj_get_next_id(&attr, uattr,
41942d602c8cSAndrii Nakryiko 					  &link_idr, &link_idr_lock);
41952d602c8cSAndrii Nakryiko 		break;
4196d46edd67SSong Liu 	case BPF_ENABLE_STATS:
4197d46edd67SSong Liu 		err = bpf_enable_stats(&attr);
4198d46edd67SSong Liu 		break;
4199ac51d99bSYonghong Song 	case BPF_ITER_CREATE:
4200ac51d99bSYonghong Song 		err = bpf_iter_create(&attr);
4201ac51d99bSYonghong Song 		break;
420299c55f7dSAlexei Starovoitov 	default:
420399c55f7dSAlexei Starovoitov 		err = -EINVAL;
420499c55f7dSAlexei Starovoitov 		break;
420599c55f7dSAlexei Starovoitov 	}
420699c55f7dSAlexei Starovoitov 
420799c55f7dSAlexei Starovoitov 	return err;
420899c55f7dSAlexei Starovoitov }
4209