xref: /linux/kernel/bpf/syscall.c (revision 82e6b1eee6a8875ef4eacfd60711cce6965c6b04)
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>
74a1e7c0cSToke Høiland-Jørgensen #include <linux/bpf_verifier.h>
8f56a653cSMartin KaFai Lau #include <linux/btf.h>
999c55f7dSAlexei Starovoitov #include <linux/syscalls.h>
1099c55f7dSAlexei Starovoitov #include <linux/slab.h>
113f07c014SIngo Molnar #include <linux/sched/signal.h>
12d407bd25SDaniel Borkmann #include <linux/vmalloc.h>
13d407bd25SDaniel Borkmann #include <linux/mmzone.h>
1499c55f7dSAlexei Starovoitov #include <linux/anon_inodes.h>
1541bdc4b4SYonghong Song #include <linux/fdtable.h>
16db20fd2bSAlexei Starovoitov #include <linux/file.h>
1741bdc4b4SYonghong Song #include <linux/fs.h>
1809756af4SAlexei Starovoitov #include <linux/license.h>
1909756af4SAlexei Starovoitov #include <linux/filter.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>
321e6c62a8SAlexei Starovoitov #include <linux/rcupdate_trace.h>
3348edc1f7SRoman Gushchin #include <linux/memcontrol.h>
3499c55f7dSAlexei Starovoitov 
35da765a2fSDaniel Borkmann #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
3614dc6f04SMartin KaFai Lau 			  (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
3714dc6f04SMartin KaFai Lau 			  (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
38da765a2fSDaniel Borkmann #define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
3914dc6f04SMartin KaFai Lau #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
40da765a2fSDaniel Borkmann #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
41da765a2fSDaniel Borkmann 			IS_FD_HASH(map))
4214dc6f04SMartin KaFai Lau 
436e71b04aSChenbo Feng #define BPF_OBJ_FLAG_MASK   (BPF_F_RDONLY | BPF_F_WRONLY)
446e71b04aSChenbo Feng 
45b121d1e7SAlexei Starovoitov DEFINE_PER_CPU(int, bpf_prog_active);
46dc4bb0e2SMartin KaFai Lau static DEFINE_IDR(prog_idr);
47dc4bb0e2SMartin KaFai Lau static DEFINE_SPINLOCK(prog_idr_lock);
48f3f1c054SMartin KaFai Lau static DEFINE_IDR(map_idr);
49f3f1c054SMartin KaFai Lau static DEFINE_SPINLOCK(map_idr_lock);
50a3b80e10SAndrii Nakryiko static DEFINE_IDR(link_idr);
51a3b80e10SAndrii Nakryiko static DEFINE_SPINLOCK(link_idr_lock);
52b121d1e7SAlexei Starovoitov 
5308389d88SDaniel Borkmann int sysctl_unprivileged_bpf_disabled __read_mostly =
5408389d88SDaniel Borkmann 	IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
551be7f75dSAlexei Starovoitov 
5640077e0cSJohannes Berg static const struct bpf_map_ops * const bpf_map_types[] = {
5791cc1a99SAlexei Starovoitov #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
5840077e0cSJohannes Berg #define BPF_MAP_TYPE(_id, _ops) \
5940077e0cSJohannes Berg 	[_id] = &_ops,
60f2e10bffSAndrii Nakryiko #define BPF_LINK_TYPE(_id, _name)
6140077e0cSJohannes Berg #include <linux/bpf_types.h>
6240077e0cSJohannes Berg #undef BPF_PROG_TYPE
6340077e0cSJohannes Berg #undef BPF_MAP_TYPE
64f2e10bffSAndrii Nakryiko #undef BPF_LINK_TYPE
6540077e0cSJohannes Berg };
6699c55f7dSAlexei Starovoitov 
67752ba56fSMickaël Salaün /*
68752ba56fSMickaël Salaün  * If we're handed a bigger struct than we know of, ensure all the unknown bits
69752ba56fSMickaël Salaün  * are 0 - i.e. new user-space does not rely on any kernel feature extensions
70752ba56fSMickaël Salaün  * we don't know about yet.
71752ba56fSMickaël Salaün  *
72752ba56fSMickaël Salaün  * There is a ToCToU between this function call and the following
73752ba56fSMickaël Salaün  * copy_from_user() call. However, this is not a concern since this function is
74752ba56fSMickaël Salaün  * meant to be a future-proofing of bits.
75752ba56fSMickaël Salaün  */
76af2ac3e1SAlexei Starovoitov int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
7758291a74SMickaël Salaün 			     size_t expected_size,
7858291a74SMickaël Salaün 			     size_t actual_size)
7958291a74SMickaël Salaün {
80b7e4b65fSAl Viro 	int res;
8158291a74SMickaël Salaün 
82752ba56fSMickaël Salaün 	if (unlikely(actual_size > PAGE_SIZE))	/* silly large */
83752ba56fSMickaël Salaün 		return -E2BIG;
84752ba56fSMickaël Salaün 
8558291a74SMickaël Salaün 	if (actual_size <= expected_size)
8658291a74SMickaël Salaün 		return 0;
8758291a74SMickaël Salaün 
88af2ac3e1SAlexei Starovoitov 	if (uaddr.is_kernel)
89af2ac3e1SAlexei Starovoitov 		res = memchr_inv(uaddr.kernel + expected_size, 0,
90af2ac3e1SAlexei Starovoitov 				 actual_size - expected_size) == NULL;
91af2ac3e1SAlexei Starovoitov 	else
92af2ac3e1SAlexei Starovoitov 		res = check_zeroed_user(uaddr.user + expected_size,
93af2ac3e1SAlexei Starovoitov 					actual_size - expected_size);
94b7e4b65fSAl Viro 	if (res < 0)
95b7e4b65fSAl Viro 		return res;
96b7e4b65fSAl Viro 	return res ? 0 : -E2BIG;
9758291a74SMickaël Salaün }
9858291a74SMickaël Salaün 
99a3884572SJakub Kicinski const struct bpf_map_ops bpf_map_offload_ops = {
100f4d05259SMartin KaFai Lau 	.map_meta_equal = bpf_map_meta_equal,
101a3884572SJakub Kicinski 	.map_alloc = bpf_map_offload_map_alloc,
102a3884572SJakub Kicinski 	.map_free = bpf_map_offload_map_free,
103e8d2bec0SDaniel Borkmann 	.map_check_btf = map_check_no_btf,
104a3884572SJakub Kicinski };
105a3884572SJakub Kicinski 
10699c55f7dSAlexei Starovoitov static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
10799c55f7dSAlexei Starovoitov {
1081110f3a9SJakub Kicinski 	const struct bpf_map_ops *ops;
1099ef09e35SMark Rutland 	u32 type = attr->map_type;
11099c55f7dSAlexei Starovoitov 	struct bpf_map *map;
1111110f3a9SJakub Kicinski 	int err;
11299c55f7dSAlexei Starovoitov 
1139ef09e35SMark Rutland 	if (type >= ARRAY_SIZE(bpf_map_types))
1141110f3a9SJakub Kicinski 		return ERR_PTR(-EINVAL);
1159ef09e35SMark Rutland 	type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
1169ef09e35SMark Rutland 	ops = bpf_map_types[type];
1171110f3a9SJakub Kicinski 	if (!ops)
11840077e0cSJohannes Berg 		return ERR_PTR(-EINVAL);
11940077e0cSJohannes Berg 
1201110f3a9SJakub Kicinski 	if (ops->map_alloc_check) {
1211110f3a9SJakub Kicinski 		err = ops->map_alloc_check(attr);
1221110f3a9SJakub Kicinski 		if (err)
1231110f3a9SJakub Kicinski 			return ERR_PTR(err);
1241110f3a9SJakub Kicinski 	}
125a3884572SJakub Kicinski 	if (attr->map_ifindex)
126a3884572SJakub Kicinski 		ops = &bpf_map_offload_ops;
1271110f3a9SJakub Kicinski 	map = ops->map_alloc(attr);
12899c55f7dSAlexei Starovoitov 	if (IS_ERR(map))
12999c55f7dSAlexei Starovoitov 		return map;
1301110f3a9SJakub Kicinski 	map->ops = ops;
1319ef09e35SMark Rutland 	map->map_type = type;
13299c55f7dSAlexei Starovoitov 	return map;
13399c55f7dSAlexei Starovoitov }
13499c55f7dSAlexei Starovoitov 
13580ee81e0SRoman Gushchin static u32 bpf_map_value_size(const struct bpf_map *map)
13615c14a3dSBrian Vazquez {
13715c14a3dSBrian Vazquez 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
13815c14a3dSBrian Vazquez 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
13915c14a3dSBrian Vazquez 	    map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
14015c14a3dSBrian Vazquez 	    map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
14115c14a3dSBrian Vazquez 		return round_up(map->value_size, 8) * num_possible_cpus();
14215c14a3dSBrian Vazquez 	else if (IS_FD_MAP(map))
14315c14a3dSBrian Vazquez 		return sizeof(u32);
14415c14a3dSBrian Vazquez 	else
14515c14a3dSBrian Vazquez 		return  map->value_size;
14615c14a3dSBrian Vazquez }
14715c14a3dSBrian Vazquez 
14815c14a3dSBrian Vazquez static void maybe_wait_bpf_programs(struct bpf_map *map)
14915c14a3dSBrian Vazquez {
15015c14a3dSBrian Vazquez 	/* Wait for any running BPF programs to complete so that
15115c14a3dSBrian Vazquez 	 * userspace, when we return to it, knows that all programs
15215c14a3dSBrian Vazquez 	 * that could be running use the new map value.
15315c14a3dSBrian Vazquez 	 */
15415c14a3dSBrian Vazquez 	if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
15515c14a3dSBrian Vazquez 	    map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
15615c14a3dSBrian Vazquez 		synchronize_rcu();
15715c14a3dSBrian Vazquez }
15815c14a3dSBrian Vazquez 
15915c14a3dSBrian Vazquez static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
16015c14a3dSBrian Vazquez 				void *value, __u64 flags)
16115c14a3dSBrian Vazquez {
16215c14a3dSBrian Vazquez 	int err;
16315c14a3dSBrian Vazquez 
16415c14a3dSBrian Vazquez 	/* Need to create a kthread, thus must support schedule */
16515c14a3dSBrian Vazquez 	if (bpf_map_is_dev_bound(map)) {
16615c14a3dSBrian Vazquez 		return bpf_map_offload_update_elem(map, key, value, flags);
16715c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
16815c14a3dSBrian Vazquez 		   map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
16915c14a3dSBrian Vazquez 		return map->ops->map_update_elem(map, key, value, flags);
17013b79d3fSLorenz Bauer 	} else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
17113b79d3fSLorenz Bauer 		   map->map_type == BPF_MAP_TYPE_SOCKMAP) {
17213b79d3fSLorenz Bauer 		return sock_map_update_elem_sys(map, key, value, flags);
17315c14a3dSBrian Vazquez 	} else if (IS_FD_PROG_ARRAY(map)) {
17415c14a3dSBrian Vazquez 		return bpf_fd_array_map_update_elem(map, f.file, key, value,
17515c14a3dSBrian Vazquez 						    flags);
17615c14a3dSBrian Vazquez 	}
17715c14a3dSBrian Vazquez 
178b6e5dae1SThomas Gleixner 	bpf_disable_instrumentation();
17915c14a3dSBrian Vazquez 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
18015c14a3dSBrian Vazquez 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
18115c14a3dSBrian Vazquez 		err = bpf_percpu_hash_update(map, key, value, flags);
18215c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
18315c14a3dSBrian Vazquez 		err = bpf_percpu_array_update(map, key, value, flags);
18415c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
18515c14a3dSBrian Vazquez 		err = bpf_percpu_cgroup_storage_update(map, key, value,
18615c14a3dSBrian Vazquez 						       flags);
18715c14a3dSBrian Vazquez 	} else if (IS_FD_ARRAY(map)) {
18815c14a3dSBrian Vazquez 		rcu_read_lock();
18915c14a3dSBrian Vazquez 		err = bpf_fd_array_map_update_elem(map, f.file, key, value,
19015c14a3dSBrian Vazquez 						   flags);
19115c14a3dSBrian Vazquez 		rcu_read_unlock();
19215c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
19315c14a3dSBrian Vazquez 		rcu_read_lock();
19415c14a3dSBrian Vazquez 		err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
19515c14a3dSBrian Vazquez 						  flags);
19615c14a3dSBrian Vazquez 		rcu_read_unlock();
19715c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
19815c14a3dSBrian Vazquez 		/* rcu_read_lock() is not needed */
19915c14a3dSBrian Vazquez 		err = bpf_fd_reuseport_array_update_elem(map, key, value,
20015c14a3dSBrian Vazquez 							 flags);
20115c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
20215c14a3dSBrian Vazquez 		   map->map_type == BPF_MAP_TYPE_STACK) {
20315c14a3dSBrian Vazquez 		err = map->ops->map_push_elem(map, value, flags);
20415c14a3dSBrian Vazquez 	} else {
20515c14a3dSBrian Vazquez 		rcu_read_lock();
20615c14a3dSBrian Vazquez 		err = map->ops->map_update_elem(map, key, value, flags);
20715c14a3dSBrian Vazquez 		rcu_read_unlock();
20815c14a3dSBrian Vazquez 	}
209b6e5dae1SThomas Gleixner 	bpf_enable_instrumentation();
21015c14a3dSBrian Vazquez 	maybe_wait_bpf_programs(map);
21115c14a3dSBrian Vazquez 
21215c14a3dSBrian Vazquez 	return err;
21315c14a3dSBrian Vazquez }
21415c14a3dSBrian Vazquez 
21515c14a3dSBrian Vazquez static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
21615c14a3dSBrian Vazquez 			      __u64 flags)
21715c14a3dSBrian Vazquez {
21815c14a3dSBrian Vazquez 	void *ptr;
21915c14a3dSBrian Vazquez 	int err;
22015c14a3dSBrian Vazquez 
221cb4d03abSBrian Vazquez 	if (bpf_map_is_dev_bound(map))
222cb4d03abSBrian Vazquez 		return bpf_map_offload_lookup_elem(map, key, value);
22315c14a3dSBrian Vazquez 
224b6e5dae1SThomas Gleixner 	bpf_disable_instrumentation();
22515c14a3dSBrian Vazquez 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
22615c14a3dSBrian Vazquez 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
22715c14a3dSBrian Vazquez 		err = bpf_percpu_hash_copy(map, key, value);
22815c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
22915c14a3dSBrian Vazquez 		err = bpf_percpu_array_copy(map, key, value);
23015c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
23115c14a3dSBrian Vazquez 		err = bpf_percpu_cgroup_storage_copy(map, key, value);
23215c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
23315c14a3dSBrian Vazquez 		err = bpf_stackmap_copy(map, key, value);
23415c14a3dSBrian Vazquez 	} else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
23515c14a3dSBrian Vazquez 		err = bpf_fd_array_map_lookup_elem(map, key, value);
23615c14a3dSBrian Vazquez 	} else if (IS_FD_HASH(map)) {
23715c14a3dSBrian Vazquez 		err = bpf_fd_htab_map_lookup_elem(map, key, value);
23815c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
23915c14a3dSBrian Vazquez 		err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
24015c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
24115c14a3dSBrian Vazquez 		   map->map_type == BPF_MAP_TYPE_STACK) {
24215c14a3dSBrian Vazquez 		err = map->ops->map_peek_elem(map, value);
24315c14a3dSBrian Vazquez 	} else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
24415c14a3dSBrian Vazquez 		/* struct_ops map requires directly updating "value" */
24515c14a3dSBrian Vazquez 		err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
24615c14a3dSBrian Vazquez 	} else {
24715c14a3dSBrian Vazquez 		rcu_read_lock();
24815c14a3dSBrian Vazquez 		if (map->ops->map_lookup_elem_sys_only)
24915c14a3dSBrian Vazquez 			ptr = map->ops->map_lookup_elem_sys_only(map, key);
25015c14a3dSBrian Vazquez 		else
25115c14a3dSBrian Vazquez 			ptr = map->ops->map_lookup_elem(map, key);
25215c14a3dSBrian Vazquez 		if (IS_ERR(ptr)) {
25315c14a3dSBrian Vazquez 			err = PTR_ERR(ptr);
25415c14a3dSBrian Vazquez 		} else if (!ptr) {
25515c14a3dSBrian Vazquez 			err = -ENOENT;
25615c14a3dSBrian Vazquez 		} else {
25715c14a3dSBrian Vazquez 			err = 0;
25815c14a3dSBrian Vazquez 			if (flags & BPF_F_LOCK)
25915c14a3dSBrian Vazquez 				/* lock 'ptr' and copy everything but lock */
26015c14a3dSBrian Vazquez 				copy_map_value_locked(map, value, ptr, true);
26115c14a3dSBrian Vazquez 			else
26215c14a3dSBrian Vazquez 				copy_map_value(map, value, ptr);
26368134668SAlexei Starovoitov 			/* mask lock and timer, since value wasn't zero inited */
26468134668SAlexei Starovoitov 			check_and_init_map_value(map, value);
26515c14a3dSBrian Vazquez 		}
26615c14a3dSBrian Vazquez 		rcu_read_unlock();
26715c14a3dSBrian Vazquez 	}
26815c14a3dSBrian Vazquez 
269b6e5dae1SThomas Gleixner 	bpf_enable_instrumentation();
27015c14a3dSBrian Vazquez 	maybe_wait_bpf_programs(map);
27115c14a3dSBrian Vazquez 
27215c14a3dSBrian Vazquez 	return err;
27315c14a3dSBrian Vazquez }
27415c14a3dSBrian Vazquez 
275d5299b67SRoman Gushchin /* Please, do not use this function outside from the map creation path
276d5299b67SRoman Gushchin  * (e.g. in map update path) without taking care of setting the active
277d5299b67SRoman Gushchin  * memory cgroup (see at bpf_map_kmalloc_node() for example).
278d5299b67SRoman Gushchin  */
279196e8ca7SDaniel Borkmann static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
280d407bd25SDaniel Borkmann {
281f01a7dbeSMartynas Pumputis 	/* We really just want to fail instead of triggering OOM killer
282f01a7dbeSMartynas Pumputis 	 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
283f01a7dbeSMartynas Pumputis 	 * which is used for lower order allocation requests.
284f01a7dbeSMartynas Pumputis 	 *
285f01a7dbeSMartynas Pumputis 	 * It has been observed that higher order allocation requests done by
286f01a7dbeSMartynas Pumputis 	 * vmalloc with __GFP_NORETRY being set might fail due to not trying
287f01a7dbeSMartynas Pumputis 	 * to reclaim memory from the page cache, thus we set
288f01a7dbeSMartynas Pumputis 	 * __GFP_RETRY_MAYFAIL to avoid such situations.
289d407bd25SDaniel Borkmann 	 */
290f01a7dbeSMartynas Pumputis 
291d5299b67SRoman Gushchin 	const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
292041de93fSChristoph Hellwig 	unsigned int flags = 0;
293041de93fSChristoph Hellwig 	unsigned long align = 1;
294d407bd25SDaniel Borkmann 	void *area;
295d407bd25SDaniel Borkmann 
296196e8ca7SDaniel Borkmann 	if (size >= SIZE_MAX)
297196e8ca7SDaniel Borkmann 		return NULL;
298196e8ca7SDaniel Borkmann 
299fc970227SAndrii Nakryiko 	/* kmalloc()'ed memory can't be mmap()'ed */
300041de93fSChristoph Hellwig 	if (mmapable) {
301041de93fSChristoph Hellwig 		BUG_ON(!PAGE_ALIGNED(size));
302041de93fSChristoph Hellwig 		align = SHMLBA;
303041de93fSChristoph Hellwig 		flags = VM_USERMAP;
304041de93fSChristoph Hellwig 	} else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
305041de93fSChristoph Hellwig 		area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
306f01a7dbeSMartynas Pumputis 				    numa_node);
307d407bd25SDaniel Borkmann 		if (area != NULL)
308d407bd25SDaniel Borkmann 			return area;
309d407bd25SDaniel Borkmann 	}
310041de93fSChristoph Hellwig 
311041de93fSChristoph Hellwig 	return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
312041de93fSChristoph Hellwig 			gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
313041de93fSChristoph Hellwig 			flags, numa_node, __builtin_return_address(0));
314d407bd25SDaniel Borkmann }
315d407bd25SDaniel Borkmann 
316196e8ca7SDaniel Borkmann void *bpf_map_area_alloc(u64 size, int numa_node)
317fc970227SAndrii Nakryiko {
318fc970227SAndrii Nakryiko 	return __bpf_map_area_alloc(size, numa_node, false);
319fc970227SAndrii Nakryiko }
320fc970227SAndrii Nakryiko 
321196e8ca7SDaniel Borkmann void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
322fc970227SAndrii Nakryiko {
323fc970227SAndrii Nakryiko 	return __bpf_map_area_alloc(size, numa_node, true);
324fc970227SAndrii Nakryiko }
325fc970227SAndrii Nakryiko 
326d407bd25SDaniel Borkmann void bpf_map_area_free(void *area)
327d407bd25SDaniel Borkmann {
328d407bd25SDaniel Borkmann 	kvfree(area);
329d407bd25SDaniel Borkmann }
330d407bd25SDaniel Borkmann 
331be70bcd5SDaniel Borkmann static u32 bpf_map_flags_retain_permanent(u32 flags)
332be70bcd5SDaniel Borkmann {
333be70bcd5SDaniel Borkmann 	/* Some map creation flags are not tied to the map object but
334be70bcd5SDaniel Borkmann 	 * rather to the map fd instead, so they have no meaning upon
335be70bcd5SDaniel Borkmann 	 * map object inspection since multiple file descriptors with
336be70bcd5SDaniel Borkmann 	 * different (access) properties can exist here. Thus, given
337be70bcd5SDaniel Borkmann 	 * this has zero meaning for the map itself, lets clear these
338be70bcd5SDaniel Borkmann 	 * from here.
339be70bcd5SDaniel Borkmann 	 */
340be70bcd5SDaniel Borkmann 	return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
341be70bcd5SDaniel Borkmann }
342be70bcd5SDaniel Borkmann 
343bd475643SJakub Kicinski void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
344bd475643SJakub Kicinski {
345bd475643SJakub Kicinski 	map->map_type = attr->map_type;
346bd475643SJakub Kicinski 	map->key_size = attr->key_size;
347bd475643SJakub Kicinski 	map->value_size = attr->value_size;
348bd475643SJakub Kicinski 	map->max_entries = attr->max_entries;
349be70bcd5SDaniel Borkmann 	map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
350bd475643SJakub Kicinski 	map->numa_node = bpf_map_attr_numa_node(attr);
351bd475643SJakub Kicinski }
352bd475643SJakub Kicinski 
353f3f1c054SMartin KaFai Lau static int bpf_map_alloc_id(struct bpf_map *map)
354f3f1c054SMartin KaFai Lau {
355f3f1c054SMartin KaFai Lau 	int id;
356f3f1c054SMartin KaFai Lau 
357b76354cdSShaohua Li 	idr_preload(GFP_KERNEL);
358f3f1c054SMartin KaFai Lau 	spin_lock_bh(&map_idr_lock);
359f3f1c054SMartin KaFai Lau 	id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
360f3f1c054SMartin KaFai Lau 	if (id > 0)
361f3f1c054SMartin KaFai Lau 		map->id = id;
362f3f1c054SMartin KaFai Lau 	spin_unlock_bh(&map_idr_lock);
363b76354cdSShaohua Li 	idr_preload_end();
364f3f1c054SMartin KaFai Lau 
365f3f1c054SMartin KaFai Lau 	if (WARN_ON_ONCE(!id))
366f3f1c054SMartin KaFai Lau 		return -ENOSPC;
367f3f1c054SMartin KaFai Lau 
368f3f1c054SMartin KaFai Lau 	return id > 0 ? 0 : id;
369f3f1c054SMartin KaFai Lau }
370f3f1c054SMartin KaFai Lau 
371a3884572SJakub Kicinski void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
372f3f1c054SMartin KaFai Lau {
373930651a7SEric Dumazet 	unsigned long flags;
374930651a7SEric Dumazet 
375a3884572SJakub Kicinski 	/* Offloaded maps are removed from the IDR store when their device
376a3884572SJakub Kicinski 	 * disappears - even if someone holds an fd to them they are unusable,
377a3884572SJakub Kicinski 	 * the memory is gone, all ops will fail; they are simply waiting for
378a3884572SJakub Kicinski 	 * refcnt to drop to be freed.
379a3884572SJakub Kicinski 	 */
380a3884572SJakub Kicinski 	if (!map->id)
381a3884572SJakub Kicinski 		return;
382a3884572SJakub Kicinski 
383bd5f5f4eSMartin KaFai Lau 	if (do_idr_lock)
384930651a7SEric Dumazet 		spin_lock_irqsave(&map_idr_lock, flags);
385bd5f5f4eSMartin KaFai Lau 	else
386bd5f5f4eSMartin KaFai Lau 		__acquire(&map_idr_lock);
387bd5f5f4eSMartin KaFai Lau 
388f3f1c054SMartin KaFai Lau 	idr_remove(&map_idr, map->id);
389a3884572SJakub Kicinski 	map->id = 0;
390bd5f5f4eSMartin KaFai Lau 
391bd5f5f4eSMartin KaFai Lau 	if (do_idr_lock)
392930651a7SEric Dumazet 		spin_unlock_irqrestore(&map_idr_lock, flags);
393bd5f5f4eSMartin KaFai Lau 	else
394bd5f5f4eSMartin KaFai Lau 		__release(&map_idr_lock);
395f3f1c054SMartin KaFai Lau }
396f3f1c054SMartin KaFai Lau 
39748edc1f7SRoman Gushchin #ifdef CONFIG_MEMCG_KMEM
39848edc1f7SRoman Gushchin static void bpf_map_save_memcg(struct bpf_map *map)
39948edc1f7SRoman Gushchin {
40048edc1f7SRoman Gushchin 	map->memcg = get_mem_cgroup_from_mm(current->mm);
40148edc1f7SRoman Gushchin }
40248edc1f7SRoman Gushchin 
40348edc1f7SRoman Gushchin static void bpf_map_release_memcg(struct bpf_map *map)
40448edc1f7SRoman Gushchin {
40548edc1f7SRoman Gushchin 	mem_cgroup_put(map->memcg);
40648edc1f7SRoman Gushchin }
40748edc1f7SRoman Gushchin 
40848edc1f7SRoman Gushchin void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
40948edc1f7SRoman Gushchin 			   int node)
41048edc1f7SRoman Gushchin {
41148edc1f7SRoman Gushchin 	struct mem_cgroup *old_memcg;
41248edc1f7SRoman Gushchin 	void *ptr;
41348edc1f7SRoman Gushchin 
41448edc1f7SRoman Gushchin 	old_memcg = set_active_memcg(map->memcg);
41548edc1f7SRoman Gushchin 	ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
41648edc1f7SRoman Gushchin 	set_active_memcg(old_memcg);
41748edc1f7SRoman Gushchin 
41848edc1f7SRoman Gushchin 	return ptr;
41948edc1f7SRoman Gushchin }
42048edc1f7SRoman Gushchin 
42148edc1f7SRoman Gushchin void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
42248edc1f7SRoman Gushchin {
42348edc1f7SRoman Gushchin 	struct mem_cgroup *old_memcg;
42448edc1f7SRoman Gushchin 	void *ptr;
42548edc1f7SRoman Gushchin 
42648edc1f7SRoman Gushchin 	old_memcg = set_active_memcg(map->memcg);
42748edc1f7SRoman Gushchin 	ptr = kzalloc(size, flags | __GFP_ACCOUNT);
42848edc1f7SRoman Gushchin 	set_active_memcg(old_memcg);
42948edc1f7SRoman Gushchin 
43048edc1f7SRoman Gushchin 	return ptr;
43148edc1f7SRoman Gushchin }
43248edc1f7SRoman Gushchin 
43348edc1f7SRoman Gushchin void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
43448edc1f7SRoman Gushchin 				    size_t align, gfp_t flags)
43548edc1f7SRoman Gushchin {
43648edc1f7SRoman Gushchin 	struct mem_cgroup *old_memcg;
43748edc1f7SRoman Gushchin 	void __percpu *ptr;
43848edc1f7SRoman Gushchin 
43948edc1f7SRoman Gushchin 	old_memcg = set_active_memcg(map->memcg);
44048edc1f7SRoman Gushchin 	ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
44148edc1f7SRoman Gushchin 	set_active_memcg(old_memcg);
44248edc1f7SRoman Gushchin 
44348edc1f7SRoman Gushchin 	return ptr;
44448edc1f7SRoman Gushchin }
44548edc1f7SRoman Gushchin 
44648edc1f7SRoman Gushchin #else
44748edc1f7SRoman Gushchin static void bpf_map_save_memcg(struct bpf_map *map)
44848edc1f7SRoman Gushchin {
44948edc1f7SRoman Gushchin }
45048edc1f7SRoman Gushchin 
45148edc1f7SRoman Gushchin static void bpf_map_release_memcg(struct bpf_map *map)
45248edc1f7SRoman Gushchin {
45348edc1f7SRoman Gushchin }
45448edc1f7SRoman Gushchin #endif
45548edc1f7SRoman Gushchin 
45699c55f7dSAlexei Starovoitov /* called from workqueue */
45799c55f7dSAlexei Starovoitov static void bpf_map_free_deferred(struct work_struct *work)
45899c55f7dSAlexei Starovoitov {
45999c55f7dSAlexei Starovoitov 	struct bpf_map *map = container_of(work, struct bpf_map, work);
46099c55f7dSAlexei Starovoitov 
461afdb09c7SChenbo Feng 	security_bpf_map_free(map);
46248edc1f7SRoman Gushchin 	bpf_map_release_memcg(map);
46399c55f7dSAlexei Starovoitov 	/* implementation dependent freeing */
46499c55f7dSAlexei Starovoitov 	map->ops->map_free(map);
46599c55f7dSAlexei Starovoitov }
46699c55f7dSAlexei Starovoitov 
467c9da161cSDaniel Borkmann static void bpf_map_put_uref(struct bpf_map *map)
468c9da161cSDaniel Borkmann {
4691e0bd5a0SAndrii Nakryiko 	if (atomic64_dec_and_test(&map->usercnt)) {
470ba6b8de4SJohn Fastabend 		if (map->ops->map_release_uref)
471ba6b8de4SJohn Fastabend 			map->ops->map_release_uref(map);
472c9da161cSDaniel Borkmann 	}
473c9da161cSDaniel Borkmann }
474c9da161cSDaniel Borkmann 
47599c55f7dSAlexei Starovoitov /* decrement map refcnt and schedule it for freeing via workqueue
47699c55f7dSAlexei Starovoitov  * (unrelying map implementation ops->map_free() might sleep)
47799c55f7dSAlexei Starovoitov  */
478bd5f5f4eSMartin KaFai Lau static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
47999c55f7dSAlexei Starovoitov {
4801e0bd5a0SAndrii Nakryiko 	if (atomic64_dec_and_test(&map->refcnt)) {
48134ad5580SMartin KaFai Lau 		/* bpf_map_free_id() must be called first */
482bd5f5f4eSMartin KaFai Lau 		bpf_map_free_id(map, do_idr_lock);
48378958fcaSMartin KaFai Lau 		btf_put(map->btf);
48499c55f7dSAlexei Starovoitov 		INIT_WORK(&map->work, bpf_map_free_deferred);
48599c55f7dSAlexei Starovoitov 		schedule_work(&map->work);
48699c55f7dSAlexei Starovoitov 	}
48799c55f7dSAlexei Starovoitov }
48899c55f7dSAlexei Starovoitov 
489bd5f5f4eSMartin KaFai Lau void bpf_map_put(struct bpf_map *map)
490bd5f5f4eSMartin KaFai Lau {
491bd5f5f4eSMartin KaFai Lau 	__bpf_map_put(map, true);
492bd5f5f4eSMartin KaFai Lau }
493630a4d38SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_map_put);
494bd5f5f4eSMartin KaFai Lau 
495c9da161cSDaniel Borkmann void bpf_map_put_with_uref(struct bpf_map *map)
496c9da161cSDaniel Borkmann {
497c9da161cSDaniel Borkmann 	bpf_map_put_uref(map);
498c9da161cSDaniel Borkmann 	bpf_map_put(map);
499c9da161cSDaniel Borkmann }
500c9da161cSDaniel Borkmann 
50199c55f7dSAlexei Starovoitov static int bpf_map_release(struct inode *inode, struct file *filp)
50299c55f7dSAlexei Starovoitov {
50361d1b6a4SDaniel Borkmann 	struct bpf_map *map = filp->private_data;
50461d1b6a4SDaniel Borkmann 
50561d1b6a4SDaniel Borkmann 	if (map->ops->map_release)
50661d1b6a4SDaniel Borkmann 		map->ops->map_release(map, filp);
50761d1b6a4SDaniel Borkmann 
50861d1b6a4SDaniel Borkmann 	bpf_map_put_with_uref(map);
50999c55f7dSAlexei Starovoitov 	return 0;
51099c55f7dSAlexei Starovoitov }
51199c55f7dSAlexei Starovoitov 
51287df15deSDaniel Borkmann static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
51387df15deSDaniel Borkmann {
51487df15deSDaniel Borkmann 	fmode_t mode = f.file->f_mode;
51587df15deSDaniel Borkmann 
51687df15deSDaniel Borkmann 	/* Our file permissions may have been overridden by global
51787df15deSDaniel Borkmann 	 * map permissions facing syscall side.
51887df15deSDaniel Borkmann 	 */
51987df15deSDaniel Borkmann 	if (READ_ONCE(map->frozen))
52087df15deSDaniel Borkmann 		mode &= ~FMODE_CAN_WRITE;
52187df15deSDaniel Borkmann 	return mode;
52287df15deSDaniel Borkmann }
52387df15deSDaniel Borkmann 
524f99bf205SDaniel Borkmann #ifdef CONFIG_PROC_FS
52580ee81e0SRoman Gushchin /* Provides an approximation of the map's memory footprint.
52680ee81e0SRoman Gushchin  * Used only to provide a backward compatibility and display
52780ee81e0SRoman Gushchin  * a reasonable "memlock" info.
52880ee81e0SRoman Gushchin  */
52980ee81e0SRoman Gushchin static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
53080ee81e0SRoman Gushchin {
53180ee81e0SRoman Gushchin 	unsigned long size;
53280ee81e0SRoman Gushchin 
53380ee81e0SRoman Gushchin 	size = round_up(map->key_size + bpf_map_value_size(map), 8);
53480ee81e0SRoman Gushchin 
53580ee81e0SRoman Gushchin 	return round_up(map->max_entries * size, PAGE_SIZE);
53680ee81e0SRoman Gushchin }
53780ee81e0SRoman Gushchin 
538f99bf205SDaniel Borkmann static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
539f99bf205SDaniel Borkmann {
540f99bf205SDaniel Borkmann 	const struct bpf_map *map = filp->private_data;
54121116b70SDaniel Borkmann 	const struct bpf_array *array;
5422beee5f5SDaniel Borkmann 	u32 type = 0, jited = 0;
54321116b70SDaniel Borkmann 
54421116b70SDaniel Borkmann 	if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
54521116b70SDaniel Borkmann 		array = container_of(map, struct bpf_array, map);
5462beee5f5SDaniel Borkmann 		type  = array->aux->type;
5472beee5f5SDaniel Borkmann 		jited = array->aux->jited;
54821116b70SDaniel Borkmann 	}
549f99bf205SDaniel Borkmann 
550f99bf205SDaniel Borkmann 	seq_printf(m,
551f99bf205SDaniel Borkmann 		   "map_type:\t%u\n"
552f99bf205SDaniel Borkmann 		   "key_size:\t%u\n"
553f99bf205SDaniel Borkmann 		   "value_size:\t%u\n"
554322cea2fSDaniel Borkmann 		   "max_entries:\t%u\n"
55521116b70SDaniel Borkmann 		   "map_flags:\t%#x\n"
55680ee81e0SRoman Gushchin 		   "memlock:\t%lu\n"
55787df15deSDaniel Borkmann 		   "map_id:\t%u\n"
55887df15deSDaniel Borkmann 		   "frozen:\t%u\n",
559f99bf205SDaniel Borkmann 		   map->map_type,
560f99bf205SDaniel Borkmann 		   map->key_size,
561f99bf205SDaniel Borkmann 		   map->value_size,
562322cea2fSDaniel Borkmann 		   map->max_entries,
56321116b70SDaniel Borkmann 		   map->map_flags,
56480ee81e0SRoman Gushchin 		   bpf_map_memory_footprint(map),
56587df15deSDaniel Borkmann 		   map->id,
56687df15deSDaniel Borkmann 		   READ_ONCE(map->frozen));
5672beee5f5SDaniel Borkmann 	if (type) {
5682beee5f5SDaniel Borkmann 		seq_printf(m, "owner_prog_type:\t%u\n", type);
5692beee5f5SDaniel Borkmann 		seq_printf(m, "owner_jited:\t%u\n", jited);
5709780c0abSDaniel Borkmann 	}
571f99bf205SDaniel Borkmann }
572f99bf205SDaniel Borkmann #endif
573f99bf205SDaniel Borkmann 
5746e71b04aSChenbo Feng static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
5756e71b04aSChenbo Feng 			      loff_t *ppos)
5766e71b04aSChenbo Feng {
5776e71b04aSChenbo Feng 	/* We need this handler such that alloc_file() enables
5786e71b04aSChenbo Feng 	 * f_mode with FMODE_CAN_READ.
5796e71b04aSChenbo Feng 	 */
5806e71b04aSChenbo Feng 	return -EINVAL;
5816e71b04aSChenbo Feng }
5826e71b04aSChenbo Feng 
5836e71b04aSChenbo Feng static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
5846e71b04aSChenbo Feng 			       size_t siz, loff_t *ppos)
5856e71b04aSChenbo Feng {
5866e71b04aSChenbo Feng 	/* We need this handler such that alloc_file() enables
5876e71b04aSChenbo Feng 	 * f_mode with FMODE_CAN_WRITE.
5886e71b04aSChenbo Feng 	 */
5896e71b04aSChenbo Feng 	return -EINVAL;
5906e71b04aSChenbo Feng }
5916e71b04aSChenbo Feng 
592fc970227SAndrii Nakryiko /* called for any extra memory-mapped regions (except initial) */
593fc970227SAndrii Nakryiko static void bpf_map_mmap_open(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 /* called for all unmapped memory region (including initial) */
605fc970227SAndrii Nakryiko static void bpf_map_mmap_close(struct vm_area_struct *vma)
606fc970227SAndrii Nakryiko {
607fc970227SAndrii Nakryiko 	struct bpf_map *map = vma->vm_file->private_data;
608fc970227SAndrii Nakryiko 
6091f6cb19bSAndrii Nakryiko 	if (vma->vm_flags & VM_MAYWRITE) {
610fc970227SAndrii Nakryiko 		mutex_lock(&map->freeze_mutex);
611fc970227SAndrii Nakryiko 		map->writecnt--;
612fc970227SAndrii Nakryiko 		mutex_unlock(&map->freeze_mutex);
613fc970227SAndrii Nakryiko 	}
614fc970227SAndrii Nakryiko }
615fc970227SAndrii Nakryiko 
616fc970227SAndrii Nakryiko static const struct vm_operations_struct bpf_map_default_vmops = {
617fc970227SAndrii Nakryiko 	.open		= bpf_map_mmap_open,
618fc970227SAndrii Nakryiko 	.close		= bpf_map_mmap_close,
619fc970227SAndrii Nakryiko };
620fc970227SAndrii Nakryiko 
621fc970227SAndrii Nakryiko static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
622fc970227SAndrii Nakryiko {
623fc970227SAndrii Nakryiko 	struct bpf_map *map = filp->private_data;
624fc970227SAndrii Nakryiko 	int err;
625fc970227SAndrii Nakryiko 
62668134668SAlexei Starovoitov 	if (!map->ops->map_mmap || map_value_has_spin_lock(map) ||
62768134668SAlexei Starovoitov 	    map_value_has_timer(map))
628fc970227SAndrii Nakryiko 		return -ENOTSUPP;
629fc970227SAndrii Nakryiko 
630fc970227SAndrii Nakryiko 	if (!(vma->vm_flags & VM_SHARED))
631fc970227SAndrii Nakryiko 		return -EINVAL;
632fc970227SAndrii Nakryiko 
633fc970227SAndrii Nakryiko 	mutex_lock(&map->freeze_mutex);
634fc970227SAndrii Nakryiko 
635dfeb376dSAndrii Nakryiko 	if (vma->vm_flags & VM_WRITE) {
636dfeb376dSAndrii Nakryiko 		if (map->frozen) {
637fc970227SAndrii Nakryiko 			err = -EPERM;
638fc970227SAndrii Nakryiko 			goto out;
639fc970227SAndrii Nakryiko 		}
640dfeb376dSAndrii Nakryiko 		/* map is meant to be read-only, so do not allow mapping as
641dfeb376dSAndrii Nakryiko 		 * writable, because it's possible to leak a writable page
642dfeb376dSAndrii Nakryiko 		 * reference and allows user-space to still modify it after
643dfeb376dSAndrii Nakryiko 		 * freezing, while verifier will assume contents do not change
644dfeb376dSAndrii Nakryiko 		 */
645dfeb376dSAndrii Nakryiko 		if (map->map_flags & BPF_F_RDONLY_PROG) {
646dfeb376dSAndrii Nakryiko 			err = -EACCES;
647dfeb376dSAndrii Nakryiko 			goto out;
648dfeb376dSAndrii Nakryiko 		}
649dfeb376dSAndrii Nakryiko 	}
650fc970227SAndrii Nakryiko 
651fc970227SAndrii Nakryiko 	/* set default open/close callbacks */
652fc970227SAndrii Nakryiko 	vma->vm_ops = &bpf_map_default_vmops;
653fc970227SAndrii Nakryiko 	vma->vm_private_data = map;
6541f6cb19bSAndrii Nakryiko 	vma->vm_flags &= ~VM_MAYEXEC;
6551f6cb19bSAndrii Nakryiko 	if (!(vma->vm_flags & VM_WRITE))
6561f6cb19bSAndrii Nakryiko 		/* disallow re-mapping with PROT_WRITE */
6571f6cb19bSAndrii Nakryiko 		vma->vm_flags &= ~VM_MAYWRITE;
658fc970227SAndrii Nakryiko 
659fc970227SAndrii Nakryiko 	err = map->ops->map_mmap(map, vma);
660fc970227SAndrii Nakryiko 	if (err)
661fc970227SAndrii Nakryiko 		goto out;
662fc970227SAndrii Nakryiko 
6631f6cb19bSAndrii Nakryiko 	if (vma->vm_flags & VM_MAYWRITE)
664fc970227SAndrii Nakryiko 		map->writecnt++;
665fc970227SAndrii Nakryiko out:
666fc970227SAndrii Nakryiko 	mutex_unlock(&map->freeze_mutex);
667fc970227SAndrii Nakryiko 	return err;
668fc970227SAndrii Nakryiko }
669fc970227SAndrii Nakryiko 
670457f4436SAndrii Nakryiko static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
671457f4436SAndrii Nakryiko {
672457f4436SAndrii Nakryiko 	struct bpf_map *map = filp->private_data;
673457f4436SAndrii Nakryiko 
674457f4436SAndrii Nakryiko 	if (map->ops->map_poll)
675457f4436SAndrii Nakryiko 		return map->ops->map_poll(map, filp, pts);
676457f4436SAndrii Nakryiko 
677457f4436SAndrii Nakryiko 	return EPOLLERR;
678457f4436SAndrii Nakryiko }
679457f4436SAndrii Nakryiko 
680f66e448cSChenbo Feng const struct file_operations bpf_map_fops = {
681f99bf205SDaniel Borkmann #ifdef CONFIG_PROC_FS
682f99bf205SDaniel Borkmann 	.show_fdinfo	= bpf_map_show_fdinfo,
683f99bf205SDaniel Borkmann #endif
68499c55f7dSAlexei Starovoitov 	.release	= bpf_map_release,
6856e71b04aSChenbo Feng 	.read		= bpf_dummy_read,
6866e71b04aSChenbo Feng 	.write		= bpf_dummy_write,
687fc970227SAndrii Nakryiko 	.mmap		= bpf_map_mmap,
688457f4436SAndrii Nakryiko 	.poll		= bpf_map_poll,
68999c55f7dSAlexei Starovoitov };
69099c55f7dSAlexei Starovoitov 
6916e71b04aSChenbo Feng int bpf_map_new_fd(struct bpf_map *map, int flags)
692aa79781bSDaniel Borkmann {
693afdb09c7SChenbo Feng 	int ret;
694afdb09c7SChenbo Feng 
695afdb09c7SChenbo Feng 	ret = security_bpf_map(map, OPEN_FMODE(flags));
696afdb09c7SChenbo Feng 	if (ret < 0)
697afdb09c7SChenbo Feng 		return ret;
698afdb09c7SChenbo Feng 
699aa79781bSDaniel Borkmann 	return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
7006e71b04aSChenbo Feng 				flags | O_CLOEXEC);
7016e71b04aSChenbo Feng }
7026e71b04aSChenbo Feng 
7036e71b04aSChenbo Feng int bpf_get_file_flag(int flags)
7046e71b04aSChenbo Feng {
7056e71b04aSChenbo Feng 	if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
7066e71b04aSChenbo Feng 		return -EINVAL;
7076e71b04aSChenbo Feng 	if (flags & BPF_F_RDONLY)
7086e71b04aSChenbo Feng 		return O_RDONLY;
7096e71b04aSChenbo Feng 	if (flags & BPF_F_WRONLY)
7106e71b04aSChenbo Feng 		return O_WRONLY;
7116e71b04aSChenbo Feng 	return O_RDWR;
712aa79781bSDaniel Borkmann }
713aa79781bSDaniel Borkmann 
71499c55f7dSAlexei Starovoitov /* helper macro to check that unused fields 'union bpf_attr' are zero */
71599c55f7dSAlexei Starovoitov #define CHECK_ATTR(CMD) \
71699c55f7dSAlexei Starovoitov 	memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
71799c55f7dSAlexei Starovoitov 		   sizeof(attr->CMD##_LAST_FIELD), 0, \
71899c55f7dSAlexei Starovoitov 		   sizeof(*attr) - \
71999c55f7dSAlexei Starovoitov 		   offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
72099c55f7dSAlexei Starovoitov 		   sizeof(attr->CMD##_LAST_FIELD)) != NULL
72199c55f7dSAlexei Starovoitov 
7228e7ae251SMartin KaFai Lau /* dst and src must have at least "size" number of bytes.
7238e7ae251SMartin KaFai Lau  * Return strlen on success and < 0 on error.
724cb4d2b3fSMartin KaFai Lau  */
7258e7ae251SMartin KaFai Lau int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
726cb4d2b3fSMartin KaFai Lau {
7278e7ae251SMartin KaFai Lau 	const char *end = src + size;
7288e7ae251SMartin KaFai Lau 	const char *orig_src = src;
729cb4d2b3fSMartin KaFai Lau 
7308e7ae251SMartin KaFai Lau 	memset(dst, 0, size);
7313e0ddc4fSDaniel Borkmann 	/* Copy all isalnum(), '_' and '.' chars. */
732cb4d2b3fSMartin KaFai Lau 	while (src < end && *src) {
7333e0ddc4fSDaniel Borkmann 		if (!isalnum(*src) &&
7343e0ddc4fSDaniel Borkmann 		    *src != '_' && *src != '.')
735cb4d2b3fSMartin KaFai Lau 			return -EINVAL;
736cb4d2b3fSMartin KaFai Lau 		*dst++ = *src++;
737cb4d2b3fSMartin KaFai Lau 	}
738cb4d2b3fSMartin KaFai Lau 
7398e7ae251SMartin KaFai Lau 	/* No '\0' found in "size" number of bytes */
740cb4d2b3fSMartin KaFai Lau 	if (src == end)
741cb4d2b3fSMartin KaFai Lau 		return -EINVAL;
742cb4d2b3fSMartin KaFai Lau 
7438e7ae251SMartin KaFai Lau 	return src - orig_src;
744cb4d2b3fSMartin KaFai Lau }
745cb4d2b3fSMartin KaFai Lau 
746e8d2bec0SDaniel Borkmann int map_check_no_btf(const struct bpf_map *map,
7471b2b234bSRoman Gushchin 		     const struct btf *btf,
748e8d2bec0SDaniel Borkmann 		     const struct btf_type *key_type,
749e8d2bec0SDaniel Borkmann 		     const struct btf_type *value_type)
750e8d2bec0SDaniel Borkmann {
751e8d2bec0SDaniel Borkmann 	return -ENOTSUPP;
752e8d2bec0SDaniel Borkmann }
753e8d2bec0SDaniel Borkmann 
754d83525caSAlexei Starovoitov static int map_check_btf(struct bpf_map *map, const struct btf *btf,
755e8d2bec0SDaniel Borkmann 			 u32 btf_key_id, u32 btf_value_id)
756e8d2bec0SDaniel Borkmann {
757e8d2bec0SDaniel Borkmann 	const struct btf_type *key_type, *value_type;
758e8d2bec0SDaniel Borkmann 	u32 key_size, value_size;
759e8d2bec0SDaniel Borkmann 	int ret = 0;
760e8d2bec0SDaniel Borkmann 
7612824ecb7SDaniel Borkmann 	/* Some maps allow key to be unspecified. */
7622824ecb7SDaniel Borkmann 	if (btf_key_id) {
763e8d2bec0SDaniel Borkmann 		key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
764e8d2bec0SDaniel Borkmann 		if (!key_type || key_size != map->key_size)
765e8d2bec0SDaniel Borkmann 			return -EINVAL;
7662824ecb7SDaniel Borkmann 	} else {
7672824ecb7SDaniel Borkmann 		key_type = btf_type_by_id(btf, 0);
7682824ecb7SDaniel Borkmann 		if (!map->ops->map_check_btf)
7692824ecb7SDaniel Borkmann 			return -EINVAL;
7702824ecb7SDaniel Borkmann 	}
771e8d2bec0SDaniel Borkmann 
772e8d2bec0SDaniel Borkmann 	value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
773e8d2bec0SDaniel Borkmann 	if (!value_type || value_size != map->value_size)
774e8d2bec0SDaniel Borkmann 		return -EINVAL;
775e8d2bec0SDaniel Borkmann 
776d83525caSAlexei Starovoitov 	map->spin_lock_off = btf_find_spin_lock(btf, value_type);
777d83525caSAlexei Starovoitov 
778d83525caSAlexei Starovoitov 	if (map_value_has_spin_lock(map)) {
779591fe988SDaniel Borkmann 		if (map->map_flags & BPF_F_RDONLY_PROG)
780591fe988SDaniel Borkmann 			return -EACCES;
781d83525caSAlexei Starovoitov 		if (map->map_type != BPF_MAP_TYPE_HASH &&
782e16d2f1aSAlexei Starovoitov 		    map->map_type != BPF_MAP_TYPE_ARRAY &&
7836ac99e8fSMartin KaFai Lau 		    map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
7848ea63684SKP Singh 		    map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
7854cf1bc1fSKP Singh 		    map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
7864cf1bc1fSKP Singh 		    map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
787d83525caSAlexei Starovoitov 			return -ENOTSUPP;
788d83525caSAlexei Starovoitov 		if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
789d83525caSAlexei Starovoitov 		    map->value_size) {
790d83525caSAlexei Starovoitov 			WARN_ONCE(1,
791d83525caSAlexei Starovoitov 				  "verifier bug spin_lock_off %d value_size %d\n",
792d83525caSAlexei Starovoitov 				  map->spin_lock_off, map->value_size);
793d83525caSAlexei Starovoitov 			return -EFAULT;
794d83525caSAlexei Starovoitov 		}
795d83525caSAlexei Starovoitov 	}
796d83525caSAlexei Starovoitov 
79768134668SAlexei Starovoitov 	map->timer_off = btf_find_timer(btf, value_type);
79868134668SAlexei Starovoitov 	if (map_value_has_timer(map)) {
79968134668SAlexei Starovoitov 		if (map->map_flags & BPF_F_RDONLY_PROG)
80068134668SAlexei Starovoitov 			return -EACCES;
80168134668SAlexei Starovoitov 		if (map->map_type != BPF_MAP_TYPE_HASH &&
80268134668SAlexei Starovoitov 		    map->map_type != BPF_MAP_TYPE_LRU_HASH &&
80368134668SAlexei Starovoitov 		    map->map_type != BPF_MAP_TYPE_ARRAY)
80468134668SAlexei Starovoitov 			return -EOPNOTSUPP;
80568134668SAlexei Starovoitov 	}
80668134668SAlexei Starovoitov 
807e8d2bec0SDaniel Borkmann 	if (map->ops->map_check_btf)
8081b2b234bSRoman Gushchin 		ret = map->ops->map_check_btf(map, btf, key_type, value_type);
809e8d2bec0SDaniel Borkmann 
810e8d2bec0SDaniel Borkmann 	return ret;
811e8d2bec0SDaniel Borkmann }
812e8d2bec0SDaniel Borkmann 
81385d33df3SMartin KaFai Lau #define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
81499c55f7dSAlexei Starovoitov /* called via syscall */
81599c55f7dSAlexei Starovoitov static int map_create(union bpf_attr *attr)
81699c55f7dSAlexei Starovoitov {
81796eabe7aSMartin KaFai Lau 	int numa_node = bpf_map_attr_numa_node(attr);
81899c55f7dSAlexei Starovoitov 	struct bpf_map *map;
8196e71b04aSChenbo Feng 	int f_flags;
82099c55f7dSAlexei Starovoitov 	int err;
82199c55f7dSAlexei Starovoitov 
82299c55f7dSAlexei Starovoitov 	err = CHECK_ATTR(BPF_MAP_CREATE);
82399c55f7dSAlexei Starovoitov 	if (err)
82499c55f7dSAlexei Starovoitov 		return -EINVAL;
82599c55f7dSAlexei Starovoitov 
82685d33df3SMartin KaFai Lau 	if (attr->btf_vmlinux_value_type_id) {
82785d33df3SMartin KaFai Lau 		if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
82885d33df3SMartin KaFai Lau 		    attr->btf_key_type_id || attr->btf_value_type_id)
82985d33df3SMartin KaFai Lau 			return -EINVAL;
83085d33df3SMartin KaFai Lau 	} else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
83185d33df3SMartin KaFai Lau 		return -EINVAL;
83285d33df3SMartin KaFai Lau 	}
83385d33df3SMartin KaFai Lau 
8346e71b04aSChenbo Feng 	f_flags = bpf_get_file_flag(attr->map_flags);
8356e71b04aSChenbo Feng 	if (f_flags < 0)
8366e71b04aSChenbo Feng 		return f_flags;
8376e71b04aSChenbo Feng 
83896eabe7aSMartin KaFai Lau 	if (numa_node != NUMA_NO_NODE &&
83996e5ae4eSEric Dumazet 	    ((unsigned int)numa_node >= nr_node_ids ||
84096e5ae4eSEric Dumazet 	     !node_online(numa_node)))
84196eabe7aSMartin KaFai Lau 		return -EINVAL;
84296eabe7aSMartin KaFai Lau 
84399c55f7dSAlexei Starovoitov 	/* find map type and init map: hashtable vs rbtree vs bloom vs ... */
84499c55f7dSAlexei Starovoitov 	map = find_and_alloc_map(attr);
84599c55f7dSAlexei Starovoitov 	if (IS_ERR(map))
84699c55f7dSAlexei Starovoitov 		return PTR_ERR(map);
84799c55f7dSAlexei Starovoitov 
8488e7ae251SMartin KaFai Lau 	err = bpf_obj_name_cpy(map->name, attr->map_name,
8498e7ae251SMartin KaFai Lau 			       sizeof(attr->map_name));
8508e7ae251SMartin KaFai Lau 	if (err < 0)
851b936ca64SRoman Gushchin 		goto free_map;
852ad5b177bSMartin KaFai Lau 
8531e0bd5a0SAndrii Nakryiko 	atomic64_set(&map->refcnt, 1);
8541e0bd5a0SAndrii Nakryiko 	atomic64_set(&map->usercnt, 1);
855fc970227SAndrii Nakryiko 	mutex_init(&map->freeze_mutex);
85699c55f7dSAlexei Starovoitov 
85785d33df3SMartin KaFai Lau 	map->spin_lock_off = -EINVAL;
85868134668SAlexei Starovoitov 	map->timer_off = -EINVAL;
85985d33df3SMartin KaFai Lau 	if (attr->btf_key_type_id || attr->btf_value_type_id ||
86085d33df3SMartin KaFai Lau 	    /* Even the map's value is a kernel's struct,
86185d33df3SMartin KaFai Lau 	     * the bpf_prog.o must have BTF to begin with
86285d33df3SMartin KaFai Lau 	     * to figure out the corresponding kernel's
86385d33df3SMartin KaFai Lau 	     * counter part.  Thus, attr->btf_fd has
86485d33df3SMartin KaFai Lau 	     * to be valid also.
86585d33df3SMartin KaFai Lau 	     */
86685d33df3SMartin KaFai Lau 	    attr->btf_vmlinux_value_type_id) {
867a26ca7c9SMartin KaFai Lau 		struct btf *btf;
868a26ca7c9SMartin KaFai Lau 
869a26ca7c9SMartin KaFai Lau 		btf = btf_get_by_fd(attr->btf_fd);
870a26ca7c9SMartin KaFai Lau 		if (IS_ERR(btf)) {
871a26ca7c9SMartin KaFai Lau 			err = PTR_ERR(btf);
872b936ca64SRoman Gushchin 			goto free_map;
873a26ca7c9SMartin KaFai Lau 		}
874350a5c4dSAlexei Starovoitov 		if (btf_is_kernel(btf)) {
875350a5c4dSAlexei Starovoitov 			btf_put(btf);
876350a5c4dSAlexei Starovoitov 			err = -EACCES;
877350a5c4dSAlexei Starovoitov 			goto free_map;
878350a5c4dSAlexei Starovoitov 		}
87985d33df3SMartin KaFai Lau 		map->btf = btf;
880a26ca7c9SMartin KaFai Lau 
88185d33df3SMartin KaFai Lau 		if (attr->btf_value_type_id) {
882e8d2bec0SDaniel Borkmann 			err = map_check_btf(map, btf, attr->btf_key_type_id,
8839b2cf328SMartin KaFai Lau 					    attr->btf_value_type_id);
88485d33df3SMartin KaFai Lau 			if (err)
885b936ca64SRoman Gushchin 				goto free_map;
886a26ca7c9SMartin KaFai Lau 		}
887a26ca7c9SMartin KaFai Lau 
8889b2cf328SMartin KaFai Lau 		map->btf_key_type_id = attr->btf_key_type_id;
8899b2cf328SMartin KaFai Lau 		map->btf_value_type_id = attr->btf_value_type_id;
89085d33df3SMartin KaFai Lau 		map->btf_vmlinux_value_type_id =
89185d33df3SMartin KaFai Lau 			attr->btf_vmlinux_value_type_id;
892a26ca7c9SMartin KaFai Lau 	}
893a26ca7c9SMartin KaFai Lau 
894afdb09c7SChenbo Feng 	err = security_bpf_map_alloc(map);
895aaac3ba9SAlexei Starovoitov 	if (err)
896b936ca64SRoman Gushchin 		goto free_map;
897afdb09c7SChenbo Feng 
898f3f1c054SMartin KaFai Lau 	err = bpf_map_alloc_id(map);
899f3f1c054SMartin KaFai Lau 	if (err)
900b936ca64SRoman Gushchin 		goto free_map_sec;
901f3f1c054SMartin KaFai Lau 
90248edc1f7SRoman Gushchin 	bpf_map_save_memcg(map);
90348edc1f7SRoman Gushchin 
9046e71b04aSChenbo Feng 	err = bpf_map_new_fd(map, f_flags);
905bd5f5f4eSMartin KaFai Lau 	if (err < 0) {
906bd5f5f4eSMartin KaFai Lau 		/* failed to allocate fd.
907352d20d6SPeng Sun 		 * bpf_map_put_with_uref() is needed because the above
908bd5f5f4eSMartin KaFai Lau 		 * bpf_map_alloc_id() has published the map
909bd5f5f4eSMartin KaFai Lau 		 * to the userspace and the userspace may
910bd5f5f4eSMartin KaFai Lau 		 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
911bd5f5f4eSMartin KaFai Lau 		 */
912352d20d6SPeng Sun 		bpf_map_put_with_uref(map);
913bd5f5f4eSMartin KaFai Lau 		return err;
914bd5f5f4eSMartin KaFai Lau 	}
91599c55f7dSAlexei Starovoitov 
91699c55f7dSAlexei Starovoitov 	return err;
91799c55f7dSAlexei Starovoitov 
918afdb09c7SChenbo Feng free_map_sec:
919afdb09c7SChenbo Feng 	security_bpf_map_free(map);
920b936ca64SRoman Gushchin free_map:
921a26ca7c9SMartin KaFai Lau 	btf_put(map->btf);
92299c55f7dSAlexei Starovoitov 	map->ops->map_free(map);
92399c55f7dSAlexei Starovoitov 	return err;
92499c55f7dSAlexei Starovoitov }
92599c55f7dSAlexei Starovoitov 
926db20fd2bSAlexei Starovoitov /* if error is returned, fd is released.
927db20fd2bSAlexei Starovoitov  * On success caller should complete fd access with matching fdput()
928db20fd2bSAlexei Starovoitov  */
929c2101297SDaniel Borkmann struct bpf_map *__bpf_map_get(struct fd f)
930db20fd2bSAlexei Starovoitov {
931db20fd2bSAlexei Starovoitov 	if (!f.file)
932db20fd2bSAlexei Starovoitov 		return ERR_PTR(-EBADF);
933db20fd2bSAlexei Starovoitov 	if (f.file->f_op != &bpf_map_fops) {
934db20fd2bSAlexei Starovoitov 		fdput(f);
935db20fd2bSAlexei Starovoitov 		return ERR_PTR(-EINVAL);
936db20fd2bSAlexei Starovoitov 	}
937db20fd2bSAlexei Starovoitov 
938c2101297SDaniel Borkmann 	return f.file->private_data;
939c2101297SDaniel Borkmann }
940c2101297SDaniel Borkmann 
9411e0bd5a0SAndrii Nakryiko void bpf_map_inc(struct bpf_map *map)
942c9da161cSDaniel Borkmann {
9431e0bd5a0SAndrii Nakryiko 	atomic64_inc(&map->refcnt);
944c9da161cSDaniel Borkmann }
945630a4d38SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_map_inc);
946c9da161cSDaniel Borkmann 
9471e0bd5a0SAndrii Nakryiko void bpf_map_inc_with_uref(struct bpf_map *map)
9481e0bd5a0SAndrii Nakryiko {
9491e0bd5a0SAndrii Nakryiko 	atomic64_inc(&map->refcnt);
9501e0bd5a0SAndrii Nakryiko 	atomic64_inc(&map->usercnt);
9511e0bd5a0SAndrii Nakryiko }
9521e0bd5a0SAndrii Nakryiko EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
9531e0bd5a0SAndrii Nakryiko 
9541ed4d924SMartin KaFai Lau struct bpf_map *bpf_map_get(u32 ufd)
9551ed4d924SMartin KaFai Lau {
9561ed4d924SMartin KaFai Lau 	struct fd f = fdget(ufd);
9571ed4d924SMartin KaFai Lau 	struct bpf_map *map;
9581ed4d924SMartin KaFai Lau 
9591ed4d924SMartin KaFai Lau 	map = __bpf_map_get(f);
9601ed4d924SMartin KaFai Lau 	if (IS_ERR(map))
9611ed4d924SMartin KaFai Lau 		return map;
9621ed4d924SMartin KaFai Lau 
9631ed4d924SMartin KaFai Lau 	bpf_map_inc(map);
9641ed4d924SMartin KaFai Lau 	fdput(f);
9651ed4d924SMartin KaFai Lau 
9661ed4d924SMartin KaFai Lau 	return map;
9671ed4d924SMartin KaFai Lau }
9681ed4d924SMartin KaFai Lau 
969c9da161cSDaniel Borkmann struct bpf_map *bpf_map_get_with_uref(u32 ufd)
970c2101297SDaniel Borkmann {
971c2101297SDaniel Borkmann 	struct fd f = fdget(ufd);
972c2101297SDaniel Borkmann 	struct bpf_map *map;
973c2101297SDaniel Borkmann 
974c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
975c2101297SDaniel Borkmann 	if (IS_ERR(map))
976c2101297SDaniel Borkmann 		return map;
977c2101297SDaniel Borkmann 
9781e0bd5a0SAndrii Nakryiko 	bpf_map_inc_with_uref(map);
979c2101297SDaniel Borkmann 	fdput(f);
980db20fd2bSAlexei Starovoitov 
981db20fd2bSAlexei Starovoitov 	return map;
982db20fd2bSAlexei Starovoitov }
983db20fd2bSAlexei Starovoitov 
984bd5f5f4eSMartin KaFai Lau /* map_idr_lock should have been held */
9851e0bd5a0SAndrii Nakryiko static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
986bd5f5f4eSMartin KaFai Lau {
987bd5f5f4eSMartin KaFai Lau 	int refold;
988bd5f5f4eSMartin KaFai Lau 
9891e0bd5a0SAndrii Nakryiko 	refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
990bd5f5f4eSMartin KaFai Lau 	if (!refold)
991bd5f5f4eSMartin KaFai Lau 		return ERR_PTR(-ENOENT);
992bd5f5f4eSMartin KaFai Lau 	if (uref)
9931e0bd5a0SAndrii Nakryiko 		atomic64_inc(&map->usercnt);
994bd5f5f4eSMartin KaFai Lau 
995bd5f5f4eSMartin KaFai Lau 	return map;
996bd5f5f4eSMartin KaFai Lau }
997bd5f5f4eSMartin KaFai Lau 
9981e0bd5a0SAndrii Nakryiko struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
999b0e4701cSStanislav Fomichev {
1000b0e4701cSStanislav Fomichev 	spin_lock_bh(&map_idr_lock);
10011e0bd5a0SAndrii Nakryiko 	map = __bpf_map_inc_not_zero(map, false);
1002b0e4701cSStanislav Fomichev 	spin_unlock_bh(&map_idr_lock);
1003b0e4701cSStanislav Fomichev 
1004b0e4701cSStanislav Fomichev 	return map;
1005b0e4701cSStanislav Fomichev }
1006b0e4701cSStanislav Fomichev EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
1007b0e4701cSStanislav Fomichev 
1008b8cdc051SAlexei Starovoitov int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1009b8cdc051SAlexei Starovoitov {
1010b8cdc051SAlexei Starovoitov 	return -ENOTSUPP;
1011b8cdc051SAlexei Starovoitov }
1012b8cdc051SAlexei Starovoitov 
1013c9d29f46SMauricio Vasquez B static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1014c9d29f46SMauricio Vasquez B {
1015c9d29f46SMauricio Vasquez B 	if (key_size)
1016c9d29f46SMauricio Vasquez B 		return memdup_user(ukey, key_size);
1017c9d29f46SMauricio Vasquez B 
1018c9d29f46SMauricio Vasquez B 	if (ukey)
1019c9d29f46SMauricio Vasquez B 		return ERR_PTR(-EINVAL);
1020c9d29f46SMauricio Vasquez B 
1021c9d29f46SMauricio Vasquez B 	return NULL;
1022c9d29f46SMauricio Vasquez B }
1023c9d29f46SMauricio Vasquez B 
1024af2ac3e1SAlexei Starovoitov static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1025af2ac3e1SAlexei Starovoitov {
1026af2ac3e1SAlexei Starovoitov 	if (key_size)
1027af2ac3e1SAlexei Starovoitov 		return memdup_bpfptr(ukey, key_size);
1028af2ac3e1SAlexei Starovoitov 
1029af2ac3e1SAlexei Starovoitov 	if (!bpfptr_is_null(ukey))
1030af2ac3e1SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
1031af2ac3e1SAlexei Starovoitov 
1032af2ac3e1SAlexei Starovoitov 	return NULL;
1033af2ac3e1SAlexei Starovoitov }
1034af2ac3e1SAlexei Starovoitov 
1035db20fd2bSAlexei Starovoitov /* last field in 'union bpf_attr' used by this command */
103696049f3aSAlexei Starovoitov #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
1037db20fd2bSAlexei Starovoitov 
1038db20fd2bSAlexei Starovoitov static int map_lookup_elem(union bpf_attr *attr)
1039db20fd2bSAlexei Starovoitov {
1040535e7b4bSMickaël Salaün 	void __user *ukey = u64_to_user_ptr(attr->key);
1041535e7b4bSMickaël Salaün 	void __user *uvalue = u64_to_user_ptr(attr->value);
1042db20fd2bSAlexei Starovoitov 	int ufd = attr->map_fd;
1043db20fd2bSAlexei Starovoitov 	struct bpf_map *map;
104415c14a3dSBrian Vazquez 	void *key, *value;
104515a07b33SAlexei Starovoitov 	u32 value_size;
1046592867bfSDaniel Borkmann 	struct fd f;
1047db20fd2bSAlexei Starovoitov 	int err;
1048db20fd2bSAlexei Starovoitov 
1049db20fd2bSAlexei Starovoitov 	if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1050db20fd2bSAlexei Starovoitov 		return -EINVAL;
1051db20fd2bSAlexei Starovoitov 
105296049f3aSAlexei Starovoitov 	if (attr->flags & ~BPF_F_LOCK)
105396049f3aSAlexei Starovoitov 		return -EINVAL;
105496049f3aSAlexei Starovoitov 
1055592867bfSDaniel Borkmann 	f = fdget(ufd);
1056c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
1057db20fd2bSAlexei Starovoitov 	if (IS_ERR(map))
1058db20fd2bSAlexei Starovoitov 		return PTR_ERR(map);
105987df15deSDaniel Borkmann 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
10606e71b04aSChenbo Feng 		err = -EPERM;
10616e71b04aSChenbo Feng 		goto err_put;
10626e71b04aSChenbo Feng 	}
10636e71b04aSChenbo Feng 
106496049f3aSAlexei Starovoitov 	if ((attr->flags & BPF_F_LOCK) &&
106596049f3aSAlexei Starovoitov 	    !map_value_has_spin_lock(map)) {
106696049f3aSAlexei Starovoitov 		err = -EINVAL;
106796049f3aSAlexei Starovoitov 		goto err_put;
106896049f3aSAlexei Starovoitov 	}
106996049f3aSAlexei Starovoitov 
1070c9d29f46SMauricio Vasquez B 	key = __bpf_copy_key(ukey, map->key_size);
1071e4448ed8SAl Viro 	if (IS_ERR(key)) {
1072e4448ed8SAl Viro 		err = PTR_ERR(key);
1073db20fd2bSAlexei Starovoitov 		goto err_put;
1074e4448ed8SAl Viro 	}
1075db20fd2bSAlexei Starovoitov 
107615c14a3dSBrian Vazquez 	value_size = bpf_map_value_size(map);
107715a07b33SAlexei Starovoitov 
10788ebe667cSAlexei Starovoitov 	err = -ENOMEM;
107915a07b33SAlexei Starovoitov 	value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1080db20fd2bSAlexei Starovoitov 	if (!value)
10818ebe667cSAlexei Starovoitov 		goto free_key;
10828ebe667cSAlexei Starovoitov 
108315c14a3dSBrian Vazquez 	err = bpf_map_copy_value(map, key, value, attr->flags);
108415a07b33SAlexei Starovoitov 	if (err)
10858ebe667cSAlexei Starovoitov 		goto free_value;
1086db20fd2bSAlexei Starovoitov 
1087db20fd2bSAlexei Starovoitov 	err = -EFAULT;
108815a07b33SAlexei Starovoitov 	if (copy_to_user(uvalue, value, value_size) != 0)
10898ebe667cSAlexei Starovoitov 		goto free_value;
1090db20fd2bSAlexei Starovoitov 
1091db20fd2bSAlexei Starovoitov 	err = 0;
1092db20fd2bSAlexei Starovoitov 
10938ebe667cSAlexei Starovoitov free_value:
10948ebe667cSAlexei Starovoitov 	kfree(value);
1095db20fd2bSAlexei Starovoitov free_key:
1096db20fd2bSAlexei Starovoitov 	kfree(key);
1097db20fd2bSAlexei Starovoitov err_put:
1098db20fd2bSAlexei Starovoitov 	fdput(f);
1099db20fd2bSAlexei Starovoitov 	return err;
1100db20fd2bSAlexei Starovoitov }
1101db20fd2bSAlexei Starovoitov 
11021ae80cf3SDaniel Colascione 
11033274f520SAlexei Starovoitov #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
1104db20fd2bSAlexei Starovoitov 
1105af2ac3e1SAlexei Starovoitov static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
1106db20fd2bSAlexei Starovoitov {
1107af2ac3e1SAlexei Starovoitov 	bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1108af2ac3e1SAlexei Starovoitov 	bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
1109db20fd2bSAlexei Starovoitov 	int ufd = attr->map_fd;
1110db20fd2bSAlexei Starovoitov 	struct bpf_map *map;
1111db20fd2bSAlexei Starovoitov 	void *key, *value;
111215a07b33SAlexei Starovoitov 	u32 value_size;
1113592867bfSDaniel Borkmann 	struct fd f;
1114db20fd2bSAlexei Starovoitov 	int err;
1115db20fd2bSAlexei Starovoitov 
1116db20fd2bSAlexei Starovoitov 	if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1117db20fd2bSAlexei Starovoitov 		return -EINVAL;
1118db20fd2bSAlexei Starovoitov 
1119592867bfSDaniel Borkmann 	f = fdget(ufd);
1120c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
1121db20fd2bSAlexei Starovoitov 	if (IS_ERR(map))
1122db20fd2bSAlexei Starovoitov 		return PTR_ERR(map);
112387df15deSDaniel Borkmann 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
11246e71b04aSChenbo Feng 		err = -EPERM;
11256e71b04aSChenbo Feng 		goto err_put;
11266e71b04aSChenbo Feng 	}
11276e71b04aSChenbo Feng 
112896049f3aSAlexei Starovoitov 	if ((attr->flags & BPF_F_LOCK) &&
112996049f3aSAlexei Starovoitov 	    !map_value_has_spin_lock(map)) {
113096049f3aSAlexei Starovoitov 		err = -EINVAL;
113196049f3aSAlexei Starovoitov 		goto err_put;
113296049f3aSAlexei Starovoitov 	}
113396049f3aSAlexei Starovoitov 
1134af2ac3e1SAlexei Starovoitov 	key = ___bpf_copy_key(ukey, map->key_size);
1135e4448ed8SAl Viro 	if (IS_ERR(key)) {
1136e4448ed8SAl Viro 		err = PTR_ERR(key);
1137db20fd2bSAlexei Starovoitov 		goto err_put;
1138e4448ed8SAl Viro 	}
1139db20fd2bSAlexei Starovoitov 
114015a07b33SAlexei Starovoitov 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
11418f844938SMartin KaFai Lau 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
1142b741f163SRoman Gushchin 	    map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1143b741f163SRoman Gushchin 	    map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
114415a07b33SAlexei Starovoitov 		value_size = round_up(map->value_size, 8) * num_possible_cpus();
114515a07b33SAlexei Starovoitov 	else
114615a07b33SAlexei Starovoitov 		value_size = map->value_size;
114715a07b33SAlexei Starovoitov 
1148db20fd2bSAlexei Starovoitov 	err = -ENOMEM;
114915a07b33SAlexei Starovoitov 	value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1150db20fd2bSAlexei Starovoitov 	if (!value)
1151db20fd2bSAlexei Starovoitov 		goto free_key;
1152db20fd2bSAlexei Starovoitov 
1153db20fd2bSAlexei Starovoitov 	err = -EFAULT;
1154af2ac3e1SAlexei Starovoitov 	if (copy_from_bpfptr(value, uvalue, value_size) != 0)
1155db20fd2bSAlexei Starovoitov 		goto free_value;
1156db20fd2bSAlexei Starovoitov 
115715c14a3dSBrian Vazquez 	err = bpf_map_update_value(map, f, key, value, attr->flags);
11586710e112SJesper Dangaard Brouer 
1159db20fd2bSAlexei Starovoitov free_value:
1160db20fd2bSAlexei Starovoitov 	kfree(value);
1161db20fd2bSAlexei Starovoitov free_key:
1162db20fd2bSAlexei Starovoitov 	kfree(key);
1163db20fd2bSAlexei Starovoitov err_put:
1164db20fd2bSAlexei Starovoitov 	fdput(f);
1165db20fd2bSAlexei Starovoitov 	return err;
1166db20fd2bSAlexei Starovoitov }
1167db20fd2bSAlexei Starovoitov 
1168db20fd2bSAlexei Starovoitov #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1169db20fd2bSAlexei Starovoitov 
1170db20fd2bSAlexei Starovoitov static int map_delete_elem(union bpf_attr *attr)
1171db20fd2bSAlexei Starovoitov {
1172535e7b4bSMickaël Salaün 	void __user *ukey = u64_to_user_ptr(attr->key);
1173db20fd2bSAlexei Starovoitov 	int ufd = attr->map_fd;
1174db20fd2bSAlexei Starovoitov 	struct bpf_map *map;
1175592867bfSDaniel Borkmann 	struct fd f;
1176db20fd2bSAlexei Starovoitov 	void *key;
1177db20fd2bSAlexei Starovoitov 	int err;
1178db20fd2bSAlexei Starovoitov 
1179db20fd2bSAlexei Starovoitov 	if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1180db20fd2bSAlexei Starovoitov 		return -EINVAL;
1181db20fd2bSAlexei Starovoitov 
1182592867bfSDaniel Borkmann 	f = fdget(ufd);
1183c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
1184db20fd2bSAlexei Starovoitov 	if (IS_ERR(map))
1185db20fd2bSAlexei Starovoitov 		return PTR_ERR(map);
118687df15deSDaniel Borkmann 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
11876e71b04aSChenbo Feng 		err = -EPERM;
11886e71b04aSChenbo Feng 		goto err_put;
11896e71b04aSChenbo Feng 	}
11906e71b04aSChenbo Feng 
1191c9d29f46SMauricio Vasquez B 	key = __bpf_copy_key(ukey, map->key_size);
1192e4448ed8SAl Viro 	if (IS_ERR(key)) {
1193e4448ed8SAl Viro 		err = PTR_ERR(key);
1194db20fd2bSAlexei Starovoitov 		goto err_put;
1195e4448ed8SAl Viro 	}
1196db20fd2bSAlexei Starovoitov 
1197a3884572SJakub Kicinski 	if (bpf_map_is_dev_bound(map)) {
1198a3884572SJakub Kicinski 		err = bpf_map_offload_delete_elem(map, key);
1199a3884572SJakub Kicinski 		goto out;
120085d33df3SMartin KaFai Lau 	} else if (IS_FD_PROG_ARRAY(map) ||
120185d33df3SMartin KaFai Lau 		   map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
120285d33df3SMartin KaFai Lau 		/* These maps require sleepable context */
1203da765a2fSDaniel Borkmann 		err = map->ops->map_delete_elem(map, key);
1204da765a2fSDaniel Borkmann 		goto out;
1205a3884572SJakub Kicinski 	}
1206a3884572SJakub Kicinski 
1207b6e5dae1SThomas Gleixner 	bpf_disable_instrumentation();
1208db20fd2bSAlexei Starovoitov 	rcu_read_lock();
1209db20fd2bSAlexei Starovoitov 	err = map->ops->map_delete_elem(map, key);
1210db20fd2bSAlexei Starovoitov 	rcu_read_unlock();
1211b6e5dae1SThomas Gleixner 	bpf_enable_instrumentation();
12121ae80cf3SDaniel Colascione 	maybe_wait_bpf_programs(map);
1213a3884572SJakub Kicinski out:
1214db20fd2bSAlexei Starovoitov 	kfree(key);
1215db20fd2bSAlexei Starovoitov err_put:
1216db20fd2bSAlexei Starovoitov 	fdput(f);
1217db20fd2bSAlexei Starovoitov 	return err;
1218db20fd2bSAlexei Starovoitov }
1219db20fd2bSAlexei Starovoitov 
1220db20fd2bSAlexei Starovoitov /* last field in 'union bpf_attr' used by this command */
1221db20fd2bSAlexei Starovoitov #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1222db20fd2bSAlexei Starovoitov 
1223db20fd2bSAlexei Starovoitov static int map_get_next_key(union bpf_attr *attr)
1224db20fd2bSAlexei Starovoitov {
1225535e7b4bSMickaël Salaün 	void __user *ukey = u64_to_user_ptr(attr->key);
1226535e7b4bSMickaël Salaün 	void __user *unext_key = u64_to_user_ptr(attr->next_key);
1227db20fd2bSAlexei Starovoitov 	int ufd = attr->map_fd;
1228db20fd2bSAlexei Starovoitov 	struct bpf_map *map;
1229db20fd2bSAlexei Starovoitov 	void *key, *next_key;
1230592867bfSDaniel Borkmann 	struct fd f;
1231db20fd2bSAlexei Starovoitov 	int err;
1232db20fd2bSAlexei Starovoitov 
1233db20fd2bSAlexei Starovoitov 	if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1234db20fd2bSAlexei Starovoitov 		return -EINVAL;
1235db20fd2bSAlexei Starovoitov 
1236592867bfSDaniel Borkmann 	f = fdget(ufd);
1237c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
1238db20fd2bSAlexei Starovoitov 	if (IS_ERR(map))
1239db20fd2bSAlexei Starovoitov 		return PTR_ERR(map);
124087df15deSDaniel Borkmann 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
12416e71b04aSChenbo Feng 		err = -EPERM;
12426e71b04aSChenbo Feng 		goto err_put;
12436e71b04aSChenbo Feng 	}
12446e71b04aSChenbo Feng 
12458fe45924STeng Qin 	if (ukey) {
1246c9d29f46SMauricio Vasquez B 		key = __bpf_copy_key(ukey, map->key_size);
1247e4448ed8SAl Viro 		if (IS_ERR(key)) {
1248e4448ed8SAl Viro 			err = PTR_ERR(key);
1249db20fd2bSAlexei Starovoitov 			goto err_put;
1250e4448ed8SAl Viro 		}
12518fe45924STeng Qin 	} else {
12528fe45924STeng Qin 		key = NULL;
12538fe45924STeng Qin 	}
1254db20fd2bSAlexei Starovoitov 
1255db20fd2bSAlexei Starovoitov 	err = -ENOMEM;
1256db20fd2bSAlexei Starovoitov 	next_key = kmalloc(map->key_size, GFP_USER);
1257db20fd2bSAlexei Starovoitov 	if (!next_key)
1258db20fd2bSAlexei Starovoitov 		goto free_key;
1259db20fd2bSAlexei Starovoitov 
1260a3884572SJakub Kicinski 	if (bpf_map_is_dev_bound(map)) {
1261a3884572SJakub Kicinski 		err = bpf_map_offload_get_next_key(map, key, next_key);
1262a3884572SJakub Kicinski 		goto out;
1263a3884572SJakub Kicinski 	}
1264a3884572SJakub Kicinski 
1265db20fd2bSAlexei Starovoitov 	rcu_read_lock();
1266db20fd2bSAlexei Starovoitov 	err = map->ops->map_get_next_key(map, key, next_key);
1267db20fd2bSAlexei Starovoitov 	rcu_read_unlock();
1268a3884572SJakub Kicinski out:
1269db20fd2bSAlexei Starovoitov 	if (err)
1270db20fd2bSAlexei Starovoitov 		goto free_next_key;
1271db20fd2bSAlexei Starovoitov 
1272db20fd2bSAlexei Starovoitov 	err = -EFAULT;
1273db20fd2bSAlexei Starovoitov 	if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1274db20fd2bSAlexei Starovoitov 		goto free_next_key;
1275db20fd2bSAlexei Starovoitov 
1276db20fd2bSAlexei Starovoitov 	err = 0;
1277db20fd2bSAlexei Starovoitov 
1278db20fd2bSAlexei Starovoitov free_next_key:
1279db20fd2bSAlexei Starovoitov 	kfree(next_key);
1280db20fd2bSAlexei Starovoitov free_key:
1281db20fd2bSAlexei Starovoitov 	kfree(key);
1282db20fd2bSAlexei Starovoitov err_put:
1283db20fd2bSAlexei Starovoitov 	fdput(f);
1284db20fd2bSAlexei Starovoitov 	return err;
1285db20fd2bSAlexei Starovoitov }
1286db20fd2bSAlexei Starovoitov 
1287aa2e93b8SBrian Vazquez int generic_map_delete_batch(struct bpf_map *map,
1288aa2e93b8SBrian Vazquez 			     const union bpf_attr *attr,
1289aa2e93b8SBrian Vazquez 			     union bpf_attr __user *uattr)
1290aa2e93b8SBrian Vazquez {
1291aa2e93b8SBrian Vazquez 	void __user *keys = u64_to_user_ptr(attr->batch.keys);
1292aa2e93b8SBrian Vazquez 	u32 cp, max_count;
1293aa2e93b8SBrian Vazquez 	int err = 0;
1294aa2e93b8SBrian Vazquez 	void *key;
1295aa2e93b8SBrian Vazquez 
1296aa2e93b8SBrian Vazquez 	if (attr->batch.elem_flags & ~BPF_F_LOCK)
1297aa2e93b8SBrian Vazquez 		return -EINVAL;
1298aa2e93b8SBrian Vazquez 
1299aa2e93b8SBrian Vazquez 	if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1300aa2e93b8SBrian Vazquez 	    !map_value_has_spin_lock(map)) {
1301aa2e93b8SBrian Vazquez 		return -EINVAL;
1302aa2e93b8SBrian Vazquez 	}
1303aa2e93b8SBrian Vazquez 
1304aa2e93b8SBrian Vazquez 	max_count = attr->batch.count;
1305aa2e93b8SBrian Vazquez 	if (!max_count)
1306aa2e93b8SBrian Vazquez 		return 0;
1307aa2e93b8SBrian Vazquez 
13082e3a94aaSBrian Vazquez 	key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
13092e3a94aaSBrian Vazquez 	if (!key)
13102e3a94aaSBrian Vazquez 		return -ENOMEM;
13112e3a94aaSBrian Vazquez 
1312aa2e93b8SBrian Vazquez 	for (cp = 0; cp < max_count; cp++) {
13132e3a94aaSBrian Vazquez 		err = -EFAULT;
13142e3a94aaSBrian Vazquez 		if (copy_from_user(key, keys + cp * map->key_size,
13152e3a94aaSBrian Vazquez 				   map->key_size))
1316aa2e93b8SBrian Vazquez 			break;
1317aa2e93b8SBrian Vazquez 
1318aa2e93b8SBrian Vazquez 		if (bpf_map_is_dev_bound(map)) {
1319aa2e93b8SBrian Vazquez 			err = bpf_map_offload_delete_elem(map, key);
1320aa2e93b8SBrian Vazquez 			break;
1321aa2e93b8SBrian Vazquez 		}
1322aa2e93b8SBrian Vazquez 
1323b6e5dae1SThomas Gleixner 		bpf_disable_instrumentation();
1324aa2e93b8SBrian Vazquez 		rcu_read_lock();
1325aa2e93b8SBrian Vazquez 		err = map->ops->map_delete_elem(map, key);
1326aa2e93b8SBrian Vazquez 		rcu_read_unlock();
1327b6e5dae1SThomas Gleixner 		bpf_enable_instrumentation();
1328aa2e93b8SBrian Vazquez 		maybe_wait_bpf_programs(map);
1329aa2e93b8SBrian Vazquez 		if (err)
1330aa2e93b8SBrian Vazquez 			break;
1331aa2e93b8SBrian Vazquez 	}
1332aa2e93b8SBrian Vazquez 	if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1333aa2e93b8SBrian Vazquez 		err = -EFAULT;
13342e3a94aaSBrian Vazquez 
13352e3a94aaSBrian Vazquez 	kfree(key);
1336aa2e93b8SBrian Vazquez 	return err;
1337aa2e93b8SBrian Vazquez }
1338aa2e93b8SBrian Vazquez 
1339aa2e93b8SBrian Vazquez int generic_map_update_batch(struct bpf_map *map,
1340aa2e93b8SBrian Vazquez 			     const union bpf_attr *attr,
1341aa2e93b8SBrian Vazquez 			     union bpf_attr __user *uattr)
1342aa2e93b8SBrian Vazquez {
1343aa2e93b8SBrian Vazquez 	void __user *values = u64_to_user_ptr(attr->batch.values);
1344aa2e93b8SBrian Vazquez 	void __user *keys = u64_to_user_ptr(attr->batch.keys);
1345aa2e93b8SBrian Vazquez 	u32 value_size, cp, max_count;
1346aa2e93b8SBrian Vazquez 	int ufd = attr->map_fd;
1347aa2e93b8SBrian Vazquez 	void *key, *value;
1348aa2e93b8SBrian Vazquez 	struct fd f;
1349aa2e93b8SBrian Vazquez 	int err = 0;
1350aa2e93b8SBrian Vazquez 
1351aa2e93b8SBrian Vazquez 	f = fdget(ufd);
1352aa2e93b8SBrian Vazquez 	if (attr->batch.elem_flags & ~BPF_F_LOCK)
1353aa2e93b8SBrian Vazquez 		return -EINVAL;
1354aa2e93b8SBrian Vazquez 
1355aa2e93b8SBrian Vazquez 	if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1356aa2e93b8SBrian Vazquez 	    !map_value_has_spin_lock(map)) {
1357aa2e93b8SBrian Vazquez 		return -EINVAL;
1358aa2e93b8SBrian Vazquez 	}
1359aa2e93b8SBrian Vazquez 
1360aa2e93b8SBrian Vazquez 	value_size = bpf_map_value_size(map);
1361aa2e93b8SBrian Vazquez 
1362aa2e93b8SBrian Vazquez 	max_count = attr->batch.count;
1363aa2e93b8SBrian Vazquez 	if (!max_count)
1364aa2e93b8SBrian Vazquez 		return 0;
1365aa2e93b8SBrian Vazquez 
13662e3a94aaSBrian Vazquez 	key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
13672e3a94aaSBrian Vazquez 	if (!key)
1368aa2e93b8SBrian Vazquez 		return -ENOMEM;
1369aa2e93b8SBrian Vazquez 
13702e3a94aaSBrian Vazquez 	value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
13712e3a94aaSBrian Vazquez 	if (!value) {
13722e3a94aaSBrian Vazquez 		kfree(key);
13732e3a94aaSBrian Vazquez 		return -ENOMEM;
1374aa2e93b8SBrian Vazquez 	}
13752e3a94aaSBrian Vazquez 
13762e3a94aaSBrian Vazquez 	for (cp = 0; cp < max_count; cp++) {
1377aa2e93b8SBrian Vazquez 		err = -EFAULT;
13782e3a94aaSBrian Vazquez 		if (copy_from_user(key, keys + cp * map->key_size,
13792e3a94aaSBrian Vazquez 		    map->key_size) ||
13802e3a94aaSBrian Vazquez 		    copy_from_user(value, values + cp * value_size, value_size))
1381aa2e93b8SBrian Vazquez 			break;
1382aa2e93b8SBrian Vazquez 
1383aa2e93b8SBrian Vazquez 		err = bpf_map_update_value(map, f, key, value,
1384aa2e93b8SBrian Vazquez 					   attr->batch.elem_flags);
1385aa2e93b8SBrian Vazquez 
1386aa2e93b8SBrian Vazquez 		if (err)
1387aa2e93b8SBrian Vazquez 			break;
1388aa2e93b8SBrian Vazquez 	}
1389aa2e93b8SBrian Vazquez 
1390aa2e93b8SBrian Vazquez 	if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1391aa2e93b8SBrian Vazquez 		err = -EFAULT;
1392aa2e93b8SBrian Vazquez 
1393aa2e93b8SBrian Vazquez 	kfree(value);
1394aa2e93b8SBrian Vazquez 	kfree(key);
1395aa2e93b8SBrian Vazquez 	return err;
1396aa2e93b8SBrian Vazquez }
1397aa2e93b8SBrian Vazquez 
1398cb4d03abSBrian Vazquez #define MAP_LOOKUP_RETRIES 3
1399cb4d03abSBrian Vazquez 
1400cb4d03abSBrian Vazquez int generic_map_lookup_batch(struct bpf_map *map,
1401cb4d03abSBrian Vazquez 				    const union bpf_attr *attr,
1402cb4d03abSBrian Vazquez 				    union bpf_attr __user *uattr)
1403cb4d03abSBrian Vazquez {
1404cb4d03abSBrian Vazquez 	void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1405cb4d03abSBrian Vazquez 	void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1406cb4d03abSBrian Vazquez 	void __user *values = u64_to_user_ptr(attr->batch.values);
1407cb4d03abSBrian Vazquez 	void __user *keys = u64_to_user_ptr(attr->batch.keys);
1408cb4d03abSBrian Vazquez 	void *buf, *buf_prevkey, *prev_key, *key, *value;
1409cb4d03abSBrian Vazquez 	int err, retry = MAP_LOOKUP_RETRIES;
1410cb4d03abSBrian Vazquez 	u32 value_size, cp, max_count;
1411cb4d03abSBrian Vazquez 
1412cb4d03abSBrian Vazquez 	if (attr->batch.elem_flags & ~BPF_F_LOCK)
1413cb4d03abSBrian Vazquez 		return -EINVAL;
1414cb4d03abSBrian Vazquez 
1415cb4d03abSBrian Vazquez 	if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1416cb4d03abSBrian Vazquez 	    !map_value_has_spin_lock(map))
1417cb4d03abSBrian Vazquez 		return -EINVAL;
1418cb4d03abSBrian Vazquez 
1419cb4d03abSBrian Vazquez 	value_size = bpf_map_value_size(map);
1420cb4d03abSBrian Vazquez 
1421cb4d03abSBrian Vazquez 	max_count = attr->batch.count;
1422cb4d03abSBrian Vazquez 	if (!max_count)
1423cb4d03abSBrian Vazquez 		return 0;
1424cb4d03abSBrian Vazquez 
1425cb4d03abSBrian Vazquez 	if (put_user(0, &uattr->batch.count))
1426cb4d03abSBrian Vazquez 		return -EFAULT;
1427cb4d03abSBrian Vazquez 
1428cb4d03abSBrian Vazquez 	buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1429cb4d03abSBrian Vazquez 	if (!buf_prevkey)
1430cb4d03abSBrian Vazquez 		return -ENOMEM;
1431cb4d03abSBrian Vazquez 
1432cb4d03abSBrian Vazquez 	buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1433cb4d03abSBrian Vazquez 	if (!buf) {
1434bb2359f4SDenis Efremov 		kfree(buf_prevkey);
1435cb4d03abSBrian Vazquez 		return -ENOMEM;
1436cb4d03abSBrian Vazquez 	}
1437cb4d03abSBrian Vazquez 
1438cb4d03abSBrian Vazquez 	err = -EFAULT;
1439cb4d03abSBrian Vazquez 	prev_key = NULL;
1440cb4d03abSBrian Vazquez 	if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1441cb4d03abSBrian Vazquez 		goto free_buf;
1442cb4d03abSBrian Vazquez 	key = buf;
1443cb4d03abSBrian Vazquez 	value = key + map->key_size;
1444cb4d03abSBrian Vazquez 	if (ubatch)
1445cb4d03abSBrian Vazquez 		prev_key = buf_prevkey;
1446cb4d03abSBrian Vazquez 
1447cb4d03abSBrian Vazquez 	for (cp = 0; cp < max_count;) {
1448cb4d03abSBrian Vazquez 		rcu_read_lock();
1449cb4d03abSBrian Vazquez 		err = map->ops->map_get_next_key(map, prev_key, key);
1450cb4d03abSBrian Vazquez 		rcu_read_unlock();
1451cb4d03abSBrian Vazquez 		if (err)
1452cb4d03abSBrian Vazquez 			break;
1453cb4d03abSBrian Vazquez 		err = bpf_map_copy_value(map, key, value,
1454cb4d03abSBrian Vazquez 					 attr->batch.elem_flags);
1455cb4d03abSBrian Vazquez 
1456cb4d03abSBrian Vazquez 		if (err == -ENOENT) {
1457cb4d03abSBrian Vazquez 			if (retry) {
1458cb4d03abSBrian Vazquez 				retry--;
1459cb4d03abSBrian Vazquez 				continue;
1460cb4d03abSBrian Vazquez 			}
1461cb4d03abSBrian Vazquez 			err = -EINTR;
1462cb4d03abSBrian Vazquez 			break;
1463cb4d03abSBrian Vazquez 		}
1464cb4d03abSBrian Vazquez 
1465cb4d03abSBrian Vazquez 		if (err)
1466cb4d03abSBrian Vazquez 			goto free_buf;
1467cb4d03abSBrian Vazquez 
1468cb4d03abSBrian Vazquez 		if (copy_to_user(keys + cp * map->key_size, key,
1469cb4d03abSBrian Vazquez 				 map->key_size)) {
1470cb4d03abSBrian Vazquez 			err = -EFAULT;
1471cb4d03abSBrian Vazquez 			goto free_buf;
1472cb4d03abSBrian Vazquez 		}
1473cb4d03abSBrian Vazquez 		if (copy_to_user(values + cp * value_size, value, value_size)) {
1474cb4d03abSBrian Vazquez 			err = -EFAULT;
1475cb4d03abSBrian Vazquez 			goto free_buf;
1476cb4d03abSBrian Vazquez 		}
1477cb4d03abSBrian Vazquez 
1478cb4d03abSBrian Vazquez 		if (!prev_key)
1479cb4d03abSBrian Vazquez 			prev_key = buf_prevkey;
1480cb4d03abSBrian Vazquez 
1481cb4d03abSBrian Vazquez 		swap(prev_key, key);
1482cb4d03abSBrian Vazquez 		retry = MAP_LOOKUP_RETRIES;
1483cb4d03abSBrian Vazquez 		cp++;
1484cb4d03abSBrian Vazquez 	}
1485cb4d03abSBrian Vazquez 
1486cb4d03abSBrian Vazquez 	if (err == -EFAULT)
1487cb4d03abSBrian Vazquez 		goto free_buf;
1488cb4d03abSBrian Vazquez 
1489cb4d03abSBrian Vazquez 	if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1490cb4d03abSBrian Vazquez 		    (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1491cb4d03abSBrian Vazquez 		err = -EFAULT;
1492cb4d03abSBrian Vazquez 
1493cb4d03abSBrian Vazquez free_buf:
1494cb4d03abSBrian Vazquez 	kfree(buf_prevkey);
1495cb4d03abSBrian Vazquez 	kfree(buf);
1496cb4d03abSBrian Vazquez 	return err;
1497cb4d03abSBrian Vazquez }
1498cb4d03abSBrian Vazquez 
14993e87f192SDenis Salopek #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
1500bd513cd0SMauricio Vasquez B 
1501bd513cd0SMauricio Vasquez B static int map_lookup_and_delete_elem(union bpf_attr *attr)
1502bd513cd0SMauricio Vasquez B {
1503bd513cd0SMauricio Vasquez B 	void __user *ukey = u64_to_user_ptr(attr->key);
1504bd513cd0SMauricio Vasquez B 	void __user *uvalue = u64_to_user_ptr(attr->value);
1505bd513cd0SMauricio Vasquez B 	int ufd = attr->map_fd;
1506bd513cd0SMauricio Vasquez B 	struct bpf_map *map;
1507540fefc0SAlexei Starovoitov 	void *key, *value;
1508bd513cd0SMauricio Vasquez B 	u32 value_size;
1509bd513cd0SMauricio Vasquez B 	struct fd f;
1510bd513cd0SMauricio Vasquez B 	int err;
1511bd513cd0SMauricio Vasquez B 
1512bd513cd0SMauricio Vasquez B 	if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1513bd513cd0SMauricio Vasquez B 		return -EINVAL;
1514bd513cd0SMauricio Vasquez B 
15153e87f192SDenis Salopek 	if (attr->flags & ~BPF_F_LOCK)
15163e87f192SDenis Salopek 		return -EINVAL;
15173e87f192SDenis Salopek 
1518bd513cd0SMauricio Vasquez B 	f = fdget(ufd);
1519bd513cd0SMauricio Vasquez B 	map = __bpf_map_get(f);
1520bd513cd0SMauricio Vasquez B 	if (IS_ERR(map))
1521bd513cd0SMauricio Vasquez B 		return PTR_ERR(map);
15221ea0f912SAnton Protopopov 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
15231ea0f912SAnton Protopopov 	    !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1524bd513cd0SMauricio Vasquez B 		err = -EPERM;
1525bd513cd0SMauricio Vasquez B 		goto err_put;
1526bd513cd0SMauricio Vasquez B 	}
1527bd513cd0SMauricio Vasquez B 
15283e87f192SDenis Salopek 	if (attr->flags &&
15293e87f192SDenis Salopek 	    (map->map_type == BPF_MAP_TYPE_QUEUE ||
15303e87f192SDenis Salopek 	     map->map_type == BPF_MAP_TYPE_STACK)) {
15313e87f192SDenis Salopek 		err = -EINVAL;
15323e87f192SDenis Salopek 		goto err_put;
15333e87f192SDenis Salopek 	}
15343e87f192SDenis Salopek 
15353e87f192SDenis Salopek 	if ((attr->flags & BPF_F_LOCK) &&
15363e87f192SDenis Salopek 	    !map_value_has_spin_lock(map)) {
15373e87f192SDenis Salopek 		err = -EINVAL;
15383e87f192SDenis Salopek 		goto err_put;
15393e87f192SDenis Salopek 	}
15403e87f192SDenis Salopek 
1541bd513cd0SMauricio Vasquez B 	key = __bpf_copy_key(ukey, map->key_size);
1542bd513cd0SMauricio Vasquez B 	if (IS_ERR(key)) {
1543bd513cd0SMauricio Vasquez B 		err = PTR_ERR(key);
1544bd513cd0SMauricio Vasquez B 		goto err_put;
1545bd513cd0SMauricio Vasquez B 	}
1546bd513cd0SMauricio Vasquez B 
15473e87f192SDenis Salopek 	value_size = bpf_map_value_size(map);
1548bd513cd0SMauricio Vasquez B 
1549bd513cd0SMauricio Vasquez B 	err = -ENOMEM;
1550bd513cd0SMauricio Vasquez B 	value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1551bd513cd0SMauricio Vasquez B 	if (!value)
1552bd513cd0SMauricio Vasquez B 		goto free_key;
1553bd513cd0SMauricio Vasquez B 
15543e87f192SDenis Salopek 	err = -ENOTSUPP;
1555bd513cd0SMauricio Vasquez B 	if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1556bd513cd0SMauricio Vasquez B 	    map->map_type == BPF_MAP_TYPE_STACK) {
1557bd513cd0SMauricio Vasquez B 		err = map->ops->map_pop_elem(map, value);
15583e87f192SDenis Salopek 	} else if (map->map_type == BPF_MAP_TYPE_HASH ||
15593e87f192SDenis Salopek 		   map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
15603e87f192SDenis Salopek 		   map->map_type == BPF_MAP_TYPE_LRU_HASH ||
15613e87f192SDenis Salopek 		   map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
15623e87f192SDenis Salopek 		if (!bpf_map_is_dev_bound(map)) {
15633e87f192SDenis Salopek 			bpf_disable_instrumentation();
15643e87f192SDenis Salopek 			rcu_read_lock();
15653e87f192SDenis Salopek 			err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
15663e87f192SDenis Salopek 			rcu_read_unlock();
15673e87f192SDenis Salopek 			bpf_enable_instrumentation();
15683e87f192SDenis Salopek 		}
1569bd513cd0SMauricio Vasquez B 	}
1570bd513cd0SMauricio Vasquez B 
1571bd513cd0SMauricio Vasquez B 	if (err)
1572bd513cd0SMauricio Vasquez B 		goto free_value;
1573bd513cd0SMauricio Vasquez B 
15747f645462SWei Yongjun 	if (copy_to_user(uvalue, value, value_size) != 0) {
15757f645462SWei Yongjun 		err = -EFAULT;
1576bd513cd0SMauricio Vasquez B 		goto free_value;
15777f645462SWei Yongjun 	}
1578bd513cd0SMauricio Vasquez B 
1579bd513cd0SMauricio Vasquez B 	err = 0;
1580bd513cd0SMauricio Vasquez B 
1581bd513cd0SMauricio Vasquez B free_value:
1582bd513cd0SMauricio Vasquez B 	kfree(value);
1583bd513cd0SMauricio Vasquez B free_key:
1584bd513cd0SMauricio Vasquez B 	kfree(key);
1585bd513cd0SMauricio Vasquez B err_put:
1586bd513cd0SMauricio Vasquez B 	fdput(f);
1587bd513cd0SMauricio Vasquez B 	return err;
1588bd513cd0SMauricio Vasquez B }
1589bd513cd0SMauricio Vasquez B 
159087df15deSDaniel Borkmann #define BPF_MAP_FREEZE_LAST_FIELD map_fd
159187df15deSDaniel Borkmann 
159287df15deSDaniel Borkmann static int map_freeze(const union bpf_attr *attr)
159387df15deSDaniel Borkmann {
159487df15deSDaniel Borkmann 	int err = 0, ufd = attr->map_fd;
159587df15deSDaniel Borkmann 	struct bpf_map *map;
159687df15deSDaniel Borkmann 	struct fd f;
159787df15deSDaniel Borkmann 
159887df15deSDaniel Borkmann 	if (CHECK_ATTR(BPF_MAP_FREEZE))
159987df15deSDaniel Borkmann 		return -EINVAL;
160087df15deSDaniel Borkmann 
160187df15deSDaniel Borkmann 	f = fdget(ufd);
160287df15deSDaniel Borkmann 	map = __bpf_map_get(f);
160387df15deSDaniel Borkmann 	if (IS_ERR(map))
160487df15deSDaniel Borkmann 		return PTR_ERR(map);
1605fc970227SAndrii Nakryiko 
160668134668SAlexei Starovoitov 	if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS ||
160768134668SAlexei Starovoitov 	    map_value_has_timer(map)) {
1608849b4d94SMartin KaFai Lau 		fdput(f);
1609849b4d94SMartin KaFai Lau 		return -ENOTSUPP;
1610849b4d94SMartin KaFai Lau 	}
1611849b4d94SMartin KaFai Lau 
1612fc970227SAndrii Nakryiko 	mutex_lock(&map->freeze_mutex);
1613fc970227SAndrii Nakryiko 
1614fc970227SAndrii Nakryiko 	if (map->writecnt) {
1615fc970227SAndrii Nakryiko 		err = -EBUSY;
1616fc970227SAndrii Nakryiko 		goto err_put;
1617fc970227SAndrii Nakryiko 	}
161887df15deSDaniel Borkmann 	if (READ_ONCE(map->frozen)) {
161987df15deSDaniel Borkmann 		err = -EBUSY;
162087df15deSDaniel Borkmann 		goto err_put;
162187df15deSDaniel Borkmann 	}
16222c78ee89SAlexei Starovoitov 	if (!bpf_capable()) {
162387df15deSDaniel Borkmann 		err = -EPERM;
162487df15deSDaniel Borkmann 		goto err_put;
162587df15deSDaniel Borkmann 	}
162687df15deSDaniel Borkmann 
162787df15deSDaniel Borkmann 	WRITE_ONCE(map->frozen, true);
162887df15deSDaniel Borkmann err_put:
1629fc970227SAndrii Nakryiko 	mutex_unlock(&map->freeze_mutex);
163087df15deSDaniel Borkmann 	fdput(f);
163187df15deSDaniel Borkmann 	return err;
163287df15deSDaniel Borkmann }
163387df15deSDaniel Borkmann 
16347de16e3aSJakub Kicinski static const struct bpf_prog_ops * const bpf_prog_types[] = {
163591cc1a99SAlexei Starovoitov #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
16367de16e3aSJakub Kicinski 	[_id] = & _name ## _prog_ops,
16377de16e3aSJakub Kicinski #define BPF_MAP_TYPE(_id, _ops)
1638f2e10bffSAndrii Nakryiko #define BPF_LINK_TYPE(_id, _name)
16397de16e3aSJakub Kicinski #include <linux/bpf_types.h>
16407de16e3aSJakub Kicinski #undef BPF_PROG_TYPE
16417de16e3aSJakub Kicinski #undef BPF_MAP_TYPE
1642f2e10bffSAndrii Nakryiko #undef BPF_LINK_TYPE
16437de16e3aSJakub Kicinski };
16447de16e3aSJakub Kicinski 
164509756af4SAlexei Starovoitov static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
164609756af4SAlexei Starovoitov {
1647d0f1a451SDaniel Borkmann 	const struct bpf_prog_ops *ops;
1648d0f1a451SDaniel Borkmann 
1649d0f1a451SDaniel Borkmann 	if (type >= ARRAY_SIZE(bpf_prog_types))
1650d0f1a451SDaniel Borkmann 		return -EINVAL;
1651d0f1a451SDaniel Borkmann 	type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1652d0f1a451SDaniel Borkmann 	ops = bpf_prog_types[type];
1653d0f1a451SDaniel Borkmann 	if (!ops)
1654be9370a7SJohannes Berg 		return -EINVAL;
165509756af4SAlexei Starovoitov 
1656ab3f0063SJakub Kicinski 	if (!bpf_prog_is_dev_bound(prog->aux))
1657d0f1a451SDaniel Borkmann 		prog->aux->ops = ops;
1658ab3f0063SJakub Kicinski 	else
1659ab3f0063SJakub Kicinski 		prog->aux->ops = &bpf_offload_prog_ops;
166024701eceSDaniel Borkmann 	prog->type = type;
166109756af4SAlexei Starovoitov 	return 0;
166209756af4SAlexei Starovoitov }
166309756af4SAlexei Starovoitov 
1664bae141f5SDaniel Borkmann enum bpf_audit {
1665bae141f5SDaniel Borkmann 	BPF_AUDIT_LOAD,
1666bae141f5SDaniel Borkmann 	BPF_AUDIT_UNLOAD,
1667bae141f5SDaniel Borkmann 	BPF_AUDIT_MAX,
1668bae141f5SDaniel Borkmann };
1669bae141f5SDaniel Borkmann 
1670bae141f5SDaniel Borkmann static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1671bae141f5SDaniel Borkmann 	[BPF_AUDIT_LOAD]   = "LOAD",
1672bae141f5SDaniel Borkmann 	[BPF_AUDIT_UNLOAD] = "UNLOAD",
1673bae141f5SDaniel Borkmann };
1674bae141f5SDaniel Borkmann 
1675bae141f5SDaniel Borkmann static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1676bae141f5SDaniel Borkmann {
1677bae141f5SDaniel Borkmann 	struct audit_context *ctx = NULL;
1678bae141f5SDaniel Borkmann 	struct audit_buffer *ab;
1679bae141f5SDaniel Borkmann 
1680bae141f5SDaniel Borkmann 	if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1681bae141f5SDaniel Borkmann 		return;
1682bae141f5SDaniel Borkmann 	if (audit_enabled == AUDIT_OFF)
1683bae141f5SDaniel Borkmann 		return;
1684bae141f5SDaniel Borkmann 	if (op == BPF_AUDIT_LOAD)
1685bae141f5SDaniel Borkmann 		ctx = audit_context();
1686bae141f5SDaniel Borkmann 	ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1687bae141f5SDaniel Borkmann 	if (unlikely(!ab))
1688bae141f5SDaniel Borkmann 		return;
1689bae141f5SDaniel Borkmann 	audit_log_format(ab, "prog-id=%u op=%s",
1690bae141f5SDaniel Borkmann 			 prog->aux->id, bpf_audit_str[op]);
1691bae141f5SDaniel Borkmann 	audit_log_end(ab);
1692bae141f5SDaniel Borkmann }
1693bae141f5SDaniel Borkmann 
1694dc4bb0e2SMartin KaFai Lau static int bpf_prog_alloc_id(struct bpf_prog *prog)
1695dc4bb0e2SMartin KaFai Lau {
1696dc4bb0e2SMartin KaFai Lau 	int id;
1697dc4bb0e2SMartin KaFai Lau 
1698b76354cdSShaohua Li 	idr_preload(GFP_KERNEL);
1699dc4bb0e2SMartin KaFai Lau 	spin_lock_bh(&prog_idr_lock);
1700dc4bb0e2SMartin KaFai Lau 	id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1701dc4bb0e2SMartin KaFai Lau 	if (id > 0)
1702dc4bb0e2SMartin KaFai Lau 		prog->aux->id = id;
1703dc4bb0e2SMartin KaFai Lau 	spin_unlock_bh(&prog_idr_lock);
1704b76354cdSShaohua Li 	idr_preload_end();
1705dc4bb0e2SMartin KaFai Lau 
1706dc4bb0e2SMartin KaFai Lau 	/* id is in [1, INT_MAX) */
1707dc4bb0e2SMartin KaFai Lau 	if (WARN_ON_ONCE(!id))
1708dc4bb0e2SMartin KaFai Lau 		return -ENOSPC;
1709dc4bb0e2SMartin KaFai Lau 
1710dc4bb0e2SMartin KaFai Lau 	return id > 0 ? 0 : id;
1711dc4bb0e2SMartin KaFai Lau }
1712dc4bb0e2SMartin KaFai Lau 
1713ad8ad79fSJakub Kicinski void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1714dc4bb0e2SMartin KaFai Lau {
1715d809e134SAlexei Starovoitov 	unsigned long flags;
1716d809e134SAlexei Starovoitov 
1717ad8ad79fSJakub Kicinski 	/* cBPF to eBPF migrations are currently not in the idr store.
1718ad8ad79fSJakub Kicinski 	 * Offloaded programs are removed from the store when their device
1719ad8ad79fSJakub Kicinski 	 * disappears - even if someone grabs an fd to them they are unusable,
1720ad8ad79fSJakub Kicinski 	 * simply waiting for refcnt to drop to be freed.
1721ad8ad79fSJakub Kicinski 	 */
1722dc4bb0e2SMartin KaFai Lau 	if (!prog->aux->id)
1723dc4bb0e2SMartin KaFai Lau 		return;
1724dc4bb0e2SMartin KaFai Lau 
1725b16d9aa4SMartin KaFai Lau 	if (do_idr_lock)
1726d809e134SAlexei Starovoitov 		spin_lock_irqsave(&prog_idr_lock, flags);
1727b16d9aa4SMartin KaFai Lau 	else
1728b16d9aa4SMartin KaFai Lau 		__acquire(&prog_idr_lock);
1729b16d9aa4SMartin KaFai Lau 
1730dc4bb0e2SMartin KaFai Lau 	idr_remove(&prog_idr, prog->aux->id);
1731ad8ad79fSJakub Kicinski 	prog->aux->id = 0;
1732b16d9aa4SMartin KaFai Lau 
1733b16d9aa4SMartin KaFai Lau 	if (do_idr_lock)
1734d809e134SAlexei Starovoitov 		spin_unlock_irqrestore(&prog_idr_lock, flags);
1735b16d9aa4SMartin KaFai Lau 	else
1736b16d9aa4SMartin KaFai Lau 		__release(&prog_idr_lock);
1737dc4bb0e2SMartin KaFai Lau }
1738dc4bb0e2SMartin KaFai Lau 
17391aacde3dSDaniel Borkmann static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1740abf2e7d6SAlexei Starovoitov {
1741abf2e7d6SAlexei Starovoitov 	struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1742abf2e7d6SAlexei Starovoitov 
17433b4d9eb2SDaniel Borkmann 	kvfree(aux->func_info);
17448c1b6e69SAlexei Starovoitov 	kfree(aux->func_info_aux);
17453ac1f01bSRoman Gushchin 	free_uid(aux->user);
1746afdb09c7SChenbo Feng 	security_bpf_prog_free(aux);
1747abf2e7d6SAlexei Starovoitov 	bpf_prog_free(aux->prog);
1748abf2e7d6SAlexei Starovoitov }
1749abf2e7d6SAlexei Starovoitov 
1750cd7455f1SDaniel Borkmann static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1751cd7455f1SDaniel Borkmann {
1752cd7455f1SDaniel Borkmann 	bpf_prog_kallsyms_del_all(prog);
1753cd7455f1SDaniel Borkmann 	btf_put(prog->aux->btf);
1754e16301fbSMartin KaFai Lau 	kvfree(prog->aux->jited_linfo);
1755e16301fbSMartin KaFai Lau 	kvfree(prog->aux->linfo);
1756e6ac2450SMartin KaFai Lau 	kfree(prog->aux->kfunc_tab);
175722dc4a0fSAndrii Nakryiko 	if (prog->aux->attach_btf)
175822dc4a0fSAndrii Nakryiko 		btf_put(prog->aux->attach_btf);
1759cd7455f1SDaniel Borkmann 
17601e6c62a8SAlexei Starovoitov 	if (deferred) {
17611e6c62a8SAlexei Starovoitov 		if (prog->aux->sleepable)
17621e6c62a8SAlexei Starovoitov 			call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1763cd7455f1SDaniel Borkmann 		else
17641e6c62a8SAlexei Starovoitov 			call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
17651e6c62a8SAlexei Starovoitov 	} else {
1766cd7455f1SDaniel Borkmann 		__bpf_prog_put_rcu(&prog->aux->rcu);
1767cd7455f1SDaniel Borkmann 	}
17681e6c62a8SAlexei Starovoitov }
1769cd7455f1SDaniel Borkmann 
1770d809e134SAlexei Starovoitov static void bpf_prog_put_deferred(struct work_struct *work)
177109756af4SAlexei Starovoitov {
1772d809e134SAlexei Starovoitov 	struct bpf_prog_aux *aux;
1773d809e134SAlexei Starovoitov 	struct bpf_prog *prog;
1774d809e134SAlexei Starovoitov 
1775d809e134SAlexei Starovoitov 	aux = container_of(work, struct bpf_prog_aux, work);
1776d809e134SAlexei Starovoitov 	prog = aux->prog;
17776ee52e2aSSong Liu 	perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1778bae141f5SDaniel Borkmann 	bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1779d809e134SAlexei Starovoitov 	__bpf_prog_put_noref(prog, true);
1780d809e134SAlexei Starovoitov }
1781d809e134SAlexei Starovoitov 
1782d809e134SAlexei Starovoitov static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
1783d809e134SAlexei Starovoitov {
1784d809e134SAlexei Starovoitov 	struct bpf_prog_aux *aux = prog->aux;
1785d809e134SAlexei Starovoitov 
1786d809e134SAlexei Starovoitov 	if (atomic64_dec_and_test(&aux->refcnt)) {
178734ad5580SMartin KaFai Lau 		/* bpf_prog_free_id() must be called first */
1788b16d9aa4SMartin KaFai Lau 		bpf_prog_free_id(prog, do_idr_lock);
1789d809e134SAlexei Starovoitov 
1790d809e134SAlexei Starovoitov 		if (in_irq() || irqs_disabled()) {
1791d809e134SAlexei Starovoitov 			INIT_WORK(&aux->work, bpf_prog_put_deferred);
1792d809e134SAlexei Starovoitov 			schedule_work(&aux->work);
1793d809e134SAlexei Starovoitov 		} else {
1794d809e134SAlexei Starovoitov 			bpf_prog_put_deferred(&aux->work);
1795d809e134SAlexei Starovoitov 		}
179609756af4SAlexei Starovoitov 	}
1797a67edbf4SDaniel Borkmann }
1798b16d9aa4SMartin KaFai Lau 
1799b16d9aa4SMartin KaFai Lau void bpf_prog_put(struct bpf_prog *prog)
1800b16d9aa4SMartin KaFai Lau {
1801b16d9aa4SMartin KaFai Lau 	__bpf_prog_put(prog, true);
1802b16d9aa4SMartin KaFai Lau }
1803e2e9b654SDaniel Borkmann EXPORT_SYMBOL_GPL(bpf_prog_put);
180409756af4SAlexei Starovoitov 
180509756af4SAlexei Starovoitov static int bpf_prog_release(struct inode *inode, struct file *filp)
180609756af4SAlexei Starovoitov {
180709756af4SAlexei Starovoitov 	struct bpf_prog *prog = filp->private_data;
180809756af4SAlexei Starovoitov 
18091aacde3dSDaniel Borkmann 	bpf_prog_put(prog);
181009756af4SAlexei Starovoitov 	return 0;
181109756af4SAlexei Starovoitov }
181209756af4SAlexei Starovoitov 
1813492ecee8SAlexei Starovoitov static void bpf_prog_get_stats(const struct bpf_prog *prog,
1814492ecee8SAlexei Starovoitov 			       struct bpf_prog_stats *stats)
1815492ecee8SAlexei Starovoitov {
18169ed9e9baSAlexei Starovoitov 	u64 nsecs = 0, cnt = 0, misses = 0;
1817492ecee8SAlexei Starovoitov 	int cpu;
1818492ecee8SAlexei Starovoitov 
1819492ecee8SAlexei Starovoitov 	for_each_possible_cpu(cpu) {
1820492ecee8SAlexei Starovoitov 		const struct bpf_prog_stats *st;
1821492ecee8SAlexei Starovoitov 		unsigned int start;
18229ed9e9baSAlexei Starovoitov 		u64 tnsecs, tcnt, tmisses;
1823492ecee8SAlexei Starovoitov 
1824700d4796SAlexei Starovoitov 		st = per_cpu_ptr(prog->stats, cpu);
1825492ecee8SAlexei Starovoitov 		do {
1826492ecee8SAlexei Starovoitov 			start = u64_stats_fetch_begin_irq(&st->syncp);
1827492ecee8SAlexei Starovoitov 			tnsecs = st->nsecs;
1828492ecee8SAlexei Starovoitov 			tcnt = st->cnt;
18299ed9e9baSAlexei Starovoitov 			tmisses = st->misses;
1830492ecee8SAlexei Starovoitov 		} while (u64_stats_fetch_retry_irq(&st->syncp, start));
1831492ecee8SAlexei Starovoitov 		nsecs += tnsecs;
1832492ecee8SAlexei Starovoitov 		cnt += tcnt;
18339ed9e9baSAlexei Starovoitov 		misses += tmisses;
1834492ecee8SAlexei Starovoitov 	}
1835492ecee8SAlexei Starovoitov 	stats->nsecs = nsecs;
1836492ecee8SAlexei Starovoitov 	stats->cnt = cnt;
18379ed9e9baSAlexei Starovoitov 	stats->misses = misses;
1838492ecee8SAlexei Starovoitov }
1839492ecee8SAlexei Starovoitov 
18407bd509e3SDaniel Borkmann #ifdef CONFIG_PROC_FS
18417bd509e3SDaniel Borkmann static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
18427bd509e3SDaniel Borkmann {
18437bd509e3SDaniel Borkmann 	const struct bpf_prog *prog = filp->private_data;
1844f1f7714eSDaniel Borkmann 	char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1845492ecee8SAlexei Starovoitov 	struct bpf_prog_stats stats;
18467bd509e3SDaniel Borkmann 
1847492ecee8SAlexei Starovoitov 	bpf_prog_get_stats(prog, &stats);
1848f1f7714eSDaniel Borkmann 	bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
18497bd509e3SDaniel Borkmann 	seq_printf(m,
18507bd509e3SDaniel Borkmann 		   "prog_type:\t%u\n"
18517bd509e3SDaniel Borkmann 		   "prog_jited:\t%u\n"
1852f1f7714eSDaniel Borkmann 		   "prog_tag:\t%s\n"
18534316b409SDaniel Borkmann 		   "memlock:\t%llu\n"
1854492ecee8SAlexei Starovoitov 		   "prog_id:\t%u\n"
1855492ecee8SAlexei Starovoitov 		   "run_time_ns:\t%llu\n"
18569ed9e9baSAlexei Starovoitov 		   "run_cnt:\t%llu\n"
18579ed9e9baSAlexei Starovoitov 		   "recursion_misses:\t%llu\n",
18587bd509e3SDaniel Borkmann 		   prog->type,
18597bd509e3SDaniel Borkmann 		   prog->jited,
1860f1f7714eSDaniel Borkmann 		   prog_tag,
18614316b409SDaniel Borkmann 		   prog->pages * 1ULL << PAGE_SHIFT,
1862492ecee8SAlexei Starovoitov 		   prog->aux->id,
1863492ecee8SAlexei Starovoitov 		   stats.nsecs,
18649ed9e9baSAlexei Starovoitov 		   stats.cnt,
18659ed9e9baSAlexei Starovoitov 		   stats.misses);
18667bd509e3SDaniel Borkmann }
18677bd509e3SDaniel Borkmann #endif
18687bd509e3SDaniel Borkmann 
1869f66e448cSChenbo Feng const struct file_operations bpf_prog_fops = {
18707bd509e3SDaniel Borkmann #ifdef CONFIG_PROC_FS
18717bd509e3SDaniel Borkmann 	.show_fdinfo	= bpf_prog_show_fdinfo,
18727bd509e3SDaniel Borkmann #endif
187309756af4SAlexei Starovoitov 	.release	= bpf_prog_release,
18746e71b04aSChenbo Feng 	.read		= bpf_dummy_read,
18756e71b04aSChenbo Feng 	.write		= bpf_dummy_write,
187609756af4SAlexei Starovoitov };
187709756af4SAlexei Starovoitov 
1878b2197755SDaniel Borkmann int bpf_prog_new_fd(struct bpf_prog *prog)
1879aa79781bSDaniel Borkmann {
1880afdb09c7SChenbo Feng 	int ret;
1881afdb09c7SChenbo Feng 
1882afdb09c7SChenbo Feng 	ret = security_bpf_prog(prog);
1883afdb09c7SChenbo Feng 	if (ret < 0)
1884afdb09c7SChenbo Feng 		return ret;
1885afdb09c7SChenbo Feng 
1886aa79781bSDaniel Borkmann 	return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1887aa79781bSDaniel Borkmann 				O_RDWR | O_CLOEXEC);
1888aa79781bSDaniel Borkmann }
1889aa79781bSDaniel Borkmann 
1890113214beSDaniel Borkmann static struct bpf_prog *____bpf_prog_get(struct fd f)
189109756af4SAlexei Starovoitov {
189209756af4SAlexei Starovoitov 	if (!f.file)
189309756af4SAlexei Starovoitov 		return ERR_PTR(-EBADF);
189409756af4SAlexei Starovoitov 	if (f.file->f_op != &bpf_prog_fops) {
189509756af4SAlexei Starovoitov 		fdput(f);
189609756af4SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
189709756af4SAlexei Starovoitov 	}
189809756af4SAlexei Starovoitov 
1899c2101297SDaniel Borkmann 	return f.file->private_data;
190009756af4SAlexei Starovoitov }
190109756af4SAlexei Starovoitov 
190285192dbfSAndrii Nakryiko void bpf_prog_add(struct bpf_prog *prog, int i)
190392117d84SAlexei Starovoitov {
190485192dbfSAndrii Nakryiko 	atomic64_add(i, &prog->aux->refcnt);
190592117d84SAlexei Starovoitov }
190659d3656dSBrenden Blanco EXPORT_SYMBOL_GPL(bpf_prog_add);
190759d3656dSBrenden Blanco 
1908c540594fSDaniel Borkmann void bpf_prog_sub(struct bpf_prog *prog, int i)
1909c540594fSDaniel Borkmann {
1910c540594fSDaniel Borkmann 	/* Only to be used for undoing previous bpf_prog_add() in some
1911c540594fSDaniel Borkmann 	 * error path. We still know that another entity in our call
1912c540594fSDaniel Borkmann 	 * path holds a reference to the program, thus atomic_sub() can
1913c540594fSDaniel Borkmann 	 * be safely used in such cases!
1914c540594fSDaniel Borkmann 	 */
191585192dbfSAndrii Nakryiko 	WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
1916c540594fSDaniel Borkmann }
1917c540594fSDaniel Borkmann EXPORT_SYMBOL_GPL(bpf_prog_sub);
1918c540594fSDaniel Borkmann 
191985192dbfSAndrii Nakryiko void bpf_prog_inc(struct bpf_prog *prog)
192059d3656dSBrenden Blanco {
192185192dbfSAndrii Nakryiko 	atomic64_inc(&prog->aux->refcnt);
192259d3656dSBrenden Blanco }
192397bc402dSDaniel Borkmann EXPORT_SYMBOL_GPL(bpf_prog_inc);
192492117d84SAlexei Starovoitov 
1925b16d9aa4SMartin KaFai Lau /* prog_idr_lock should have been held */
1926a6f6df69SJohn Fastabend struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1927b16d9aa4SMartin KaFai Lau {
1928b16d9aa4SMartin KaFai Lau 	int refold;
1929b16d9aa4SMartin KaFai Lau 
193085192dbfSAndrii Nakryiko 	refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1931b16d9aa4SMartin KaFai Lau 
1932b16d9aa4SMartin KaFai Lau 	if (!refold)
1933b16d9aa4SMartin KaFai Lau 		return ERR_PTR(-ENOENT);
1934b16d9aa4SMartin KaFai Lau 
1935b16d9aa4SMartin KaFai Lau 	return prog;
1936b16d9aa4SMartin KaFai Lau }
1937a6f6df69SJohn Fastabend EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1938b16d9aa4SMartin KaFai Lau 
1939040ee692SAl Viro bool bpf_prog_get_ok(struct bpf_prog *prog,
1940288b3de5SJakub Kicinski 			    enum bpf_prog_type *attach_type, bool attach_drv)
1941248f346fSJakub Kicinski {
1942288b3de5SJakub Kicinski 	/* not an attachment, just a refcount inc, always allow */
1943288b3de5SJakub Kicinski 	if (!attach_type)
1944288b3de5SJakub Kicinski 		return true;
1945248f346fSJakub Kicinski 
1946248f346fSJakub Kicinski 	if (prog->type != *attach_type)
1947248f346fSJakub Kicinski 		return false;
1948288b3de5SJakub Kicinski 	if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1949248f346fSJakub Kicinski 		return false;
1950248f346fSJakub Kicinski 
1951248f346fSJakub Kicinski 	return true;
1952248f346fSJakub Kicinski }
1953248f346fSJakub Kicinski 
1954248f346fSJakub Kicinski static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1955288b3de5SJakub Kicinski 				       bool attach_drv)
195609756af4SAlexei Starovoitov {
195709756af4SAlexei Starovoitov 	struct fd f = fdget(ufd);
195809756af4SAlexei Starovoitov 	struct bpf_prog *prog;
195909756af4SAlexei Starovoitov 
1960113214beSDaniel Borkmann 	prog = ____bpf_prog_get(f);
196109756af4SAlexei Starovoitov 	if (IS_ERR(prog))
196209756af4SAlexei Starovoitov 		return prog;
1963288b3de5SJakub Kicinski 	if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1964113214beSDaniel Borkmann 		prog = ERR_PTR(-EINVAL);
1965113214beSDaniel Borkmann 		goto out;
1966113214beSDaniel Borkmann 	}
196709756af4SAlexei Starovoitov 
196885192dbfSAndrii Nakryiko 	bpf_prog_inc(prog);
1969113214beSDaniel Borkmann out:
197009756af4SAlexei Starovoitov 	fdput(f);
197109756af4SAlexei Starovoitov 	return prog;
197209756af4SAlexei Starovoitov }
1973113214beSDaniel Borkmann 
1974113214beSDaniel Borkmann struct bpf_prog *bpf_prog_get(u32 ufd)
1975113214beSDaniel Borkmann {
1976288b3de5SJakub Kicinski 	return __bpf_prog_get(ufd, NULL, false);
1977113214beSDaniel Borkmann }
1978113214beSDaniel Borkmann 
1979248f346fSJakub Kicinski struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1980288b3de5SJakub Kicinski 				       bool attach_drv)
1981248f346fSJakub Kicinski {
19824d220ed0SAlexei Starovoitov 	return __bpf_prog_get(ufd, &type, attach_drv);
1983248f346fSJakub Kicinski }
19846c8dfe21SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1985248f346fSJakub Kicinski 
1986aac3fc32SAndrey Ignatov /* Initially all BPF programs could be loaded w/o specifying
1987aac3fc32SAndrey Ignatov  * expected_attach_type. Later for some of them specifying expected_attach_type
1988aac3fc32SAndrey Ignatov  * at load time became required so that program could be validated properly.
1989aac3fc32SAndrey Ignatov  * Programs of types that are allowed to be loaded both w/ and w/o (for
1990aac3fc32SAndrey Ignatov  * backward compatibility) expected_attach_type, should have the default attach
1991aac3fc32SAndrey Ignatov  * type assigned to expected_attach_type for the latter case, so that it can be
1992aac3fc32SAndrey Ignatov  * validated later at attach time.
1993aac3fc32SAndrey Ignatov  *
1994aac3fc32SAndrey Ignatov  * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1995aac3fc32SAndrey Ignatov  * prog type requires it but has some attach types that have to be backward
1996aac3fc32SAndrey Ignatov  * compatible.
1997aac3fc32SAndrey Ignatov  */
1998aac3fc32SAndrey Ignatov static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1999aac3fc32SAndrey Ignatov {
2000aac3fc32SAndrey Ignatov 	switch (attr->prog_type) {
2001aac3fc32SAndrey Ignatov 	case BPF_PROG_TYPE_CGROUP_SOCK:
2002aac3fc32SAndrey Ignatov 		/* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
2003aac3fc32SAndrey Ignatov 		 * exist so checking for non-zero is the way to go here.
2004aac3fc32SAndrey Ignatov 		 */
2005aac3fc32SAndrey Ignatov 		if (!attr->expected_attach_type)
2006aac3fc32SAndrey Ignatov 			attr->expected_attach_type =
2007aac3fc32SAndrey Ignatov 				BPF_CGROUP_INET_SOCK_CREATE;
2008aac3fc32SAndrey Ignatov 		break;
2009d5e4ddaeSKuniyuki Iwashima 	case BPF_PROG_TYPE_SK_REUSEPORT:
2010d5e4ddaeSKuniyuki Iwashima 		if (!attr->expected_attach_type)
2011d5e4ddaeSKuniyuki Iwashima 			attr->expected_attach_type =
2012d5e4ddaeSKuniyuki Iwashima 				BPF_SK_REUSEPORT_SELECT;
2013d5e4ddaeSKuniyuki Iwashima 		break;
2014aac3fc32SAndrey Ignatov 	}
2015aac3fc32SAndrey Ignatov }
2016aac3fc32SAndrey Ignatov 
20175e43f899SAndrey Ignatov static int
2018ccfe29ebSAlexei Starovoitov bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2019ccfe29ebSAlexei Starovoitov 			   enum bpf_attach_type expected_attach_type,
2020290248a5SAndrii Nakryiko 			   struct btf *attach_btf, u32 btf_id,
2021290248a5SAndrii Nakryiko 			   struct bpf_prog *dst_prog)
20225e43f899SAndrey Ignatov {
202327ae7997SMartin KaFai Lau 	if (btf_id) {
2024c108e3c1SAlexei Starovoitov 		if (btf_id > BTF_MAX_TYPE)
2025c108e3c1SAlexei Starovoitov 			return -EINVAL;
202627ae7997SMartin KaFai Lau 
2027290248a5SAndrii Nakryiko 		if (!attach_btf && !dst_prog)
2028290248a5SAndrii Nakryiko 			return -EINVAL;
2029290248a5SAndrii Nakryiko 
203027ae7997SMartin KaFai Lau 		switch (prog_type) {
203127ae7997SMartin KaFai Lau 		case BPF_PROG_TYPE_TRACING:
20329e4e01dfSKP Singh 		case BPF_PROG_TYPE_LSM:
203327ae7997SMartin KaFai Lau 		case BPF_PROG_TYPE_STRUCT_OPS:
2034be8704ffSAlexei Starovoitov 		case BPF_PROG_TYPE_EXT:
2035c108e3c1SAlexei Starovoitov 			break;
2036c108e3c1SAlexei Starovoitov 		default:
2037c108e3c1SAlexei Starovoitov 			return -EINVAL;
2038c108e3c1SAlexei Starovoitov 		}
203927ae7997SMartin KaFai Lau 	}
204027ae7997SMartin KaFai Lau 
2041290248a5SAndrii Nakryiko 	if (attach_btf && (!btf_id || dst_prog))
2042290248a5SAndrii Nakryiko 		return -EINVAL;
2043290248a5SAndrii Nakryiko 
2044290248a5SAndrii Nakryiko 	if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
2045be8704ffSAlexei Starovoitov 	    prog_type != BPF_PROG_TYPE_EXT)
204627ae7997SMartin KaFai Lau 		return -EINVAL;
2047c108e3c1SAlexei Starovoitov 
2048c108e3c1SAlexei Starovoitov 	switch (prog_type) {
2049aac3fc32SAndrey Ignatov 	case BPF_PROG_TYPE_CGROUP_SOCK:
2050aac3fc32SAndrey Ignatov 		switch (expected_attach_type) {
2051aac3fc32SAndrey Ignatov 		case BPF_CGROUP_INET_SOCK_CREATE:
2052f5836749SStanislav Fomichev 		case BPF_CGROUP_INET_SOCK_RELEASE:
2053aac3fc32SAndrey Ignatov 		case BPF_CGROUP_INET4_POST_BIND:
2054aac3fc32SAndrey Ignatov 		case BPF_CGROUP_INET6_POST_BIND:
2055aac3fc32SAndrey Ignatov 			return 0;
2056aac3fc32SAndrey Ignatov 		default:
2057aac3fc32SAndrey Ignatov 			return -EINVAL;
2058aac3fc32SAndrey Ignatov 		}
20594fbac77dSAndrey Ignatov 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
20604fbac77dSAndrey Ignatov 		switch (expected_attach_type) {
20614fbac77dSAndrey Ignatov 		case BPF_CGROUP_INET4_BIND:
20624fbac77dSAndrey Ignatov 		case BPF_CGROUP_INET6_BIND:
2063d74bad4eSAndrey Ignatov 		case BPF_CGROUP_INET4_CONNECT:
2064d74bad4eSAndrey Ignatov 		case BPF_CGROUP_INET6_CONNECT:
20651b66d253SDaniel Borkmann 		case BPF_CGROUP_INET4_GETPEERNAME:
20661b66d253SDaniel Borkmann 		case BPF_CGROUP_INET6_GETPEERNAME:
20671b66d253SDaniel Borkmann 		case BPF_CGROUP_INET4_GETSOCKNAME:
20681b66d253SDaniel Borkmann 		case BPF_CGROUP_INET6_GETSOCKNAME:
20691cedee13SAndrey Ignatov 		case BPF_CGROUP_UDP4_SENDMSG:
20701cedee13SAndrey Ignatov 		case BPF_CGROUP_UDP6_SENDMSG:
2071983695faSDaniel Borkmann 		case BPF_CGROUP_UDP4_RECVMSG:
2072983695faSDaniel Borkmann 		case BPF_CGROUP_UDP6_RECVMSG:
20735e43f899SAndrey Ignatov 			return 0;
20744fbac77dSAndrey Ignatov 		default:
20754fbac77dSAndrey Ignatov 			return -EINVAL;
20764fbac77dSAndrey Ignatov 		}
20775cf1e914Sbrakmo 	case BPF_PROG_TYPE_CGROUP_SKB:
20785cf1e914Sbrakmo 		switch (expected_attach_type) {
20795cf1e914Sbrakmo 		case BPF_CGROUP_INET_INGRESS:
20805cf1e914Sbrakmo 		case BPF_CGROUP_INET_EGRESS:
20815cf1e914Sbrakmo 			return 0;
20825cf1e914Sbrakmo 		default:
20835cf1e914Sbrakmo 			return -EINVAL;
20845cf1e914Sbrakmo 		}
20850d01da6aSStanislav Fomichev 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
20860d01da6aSStanislav Fomichev 		switch (expected_attach_type) {
20870d01da6aSStanislav Fomichev 		case BPF_CGROUP_SETSOCKOPT:
20880d01da6aSStanislav Fomichev 		case BPF_CGROUP_GETSOCKOPT:
20890d01da6aSStanislav Fomichev 			return 0;
20900d01da6aSStanislav Fomichev 		default:
20910d01da6aSStanislav Fomichev 			return -EINVAL;
20920d01da6aSStanislav Fomichev 		}
2093e9ddbb77SJakub Sitnicki 	case BPF_PROG_TYPE_SK_LOOKUP:
2094e9ddbb77SJakub Sitnicki 		if (expected_attach_type == BPF_SK_LOOKUP)
2095e9ddbb77SJakub Sitnicki 			return 0;
2096e9ddbb77SJakub Sitnicki 		return -EINVAL;
2097d5e4ddaeSKuniyuki Iwashima 	case BPF_PROG_TYPE_SK_REUSEPORT:
2098d5e4ddaeSKuniyuki Iwashima 		switch (expected_attach_type) {
2099d5e4ddaeSKuniyuki Iwashima 		case BPF_SK_REUSEPORT_SELECT:
2100d5e4ddaeSKuniyuki Iwashima 		case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2101d5e4ddaeSKuniyuki Iwashima 			return 0;
2102d5e4ddaeSKuniyuki Iwashima 		default:
2103d5e4ddaeSKuniyuki Iwashima 			return -EINVAL;
2104d5e4ddaeSKuniyuki Iwashima 		}
210579a7f8bdSAlexei Starovoitov 	case BPF_PROG_TYPE_SYSCALL:
2106be8704ffSAlexei Starovoitov 	case BPF_PROG_TYPE_EXT:
2107be8704ffSAlexei Starovoitov 		if (expected_attach_type)
2108be8704ffSAlexei Starovoitov 			return -EINVAL;
2109df561f66SGustavo A. R. Silva 		fallthrough;
21104fbac77dSAndrey Ignatov 	default:
21114fbac77dSAndrey Ignatov 		return 0;
21124fbac77dSAndrey Ignatov 	}
21135e43f899SAndrey Ignatov }
21145e43f899SAndrey Ignatov 
21152c78ee89SAlexei Starovoitov static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
21162c78ee89SAlexei Starovoitov {
21172c78ee89SAlexei Starovoitov 	switch (prog_type) {
21182c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_SCHED_CLS:
21192c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_SCHED_ACT:
21202c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_XDP:
21212c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_IN:
21222c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_OUT:
21232c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_XMIT:
21242c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_LWT_SEG6LOCAL:
21252c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_SK_SKB:
21262c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_SK_MSG:
21272c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_LIRC_MODE2:
21282c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
21292c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_CGROUP_DEVICE:
21302c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_CGROUP_SOCK:
21312c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
21322c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
21332c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
21342c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_SOCK_OPS:
21352c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_EXT: /* extends any prog */
21362c78ee89SAlexei Starovoitov 		return true;
21372c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_CGROUP_SKB:
21382c78ee89SAlexei Starovoitov 		/* always unpriv */
21392c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_SK_REUSEPORT:
21402c78ee89SAlexei Starovoitov 		/* equivalent to SOCKET_FILTER. need CAP_BPF only */
21412c78ee89SAlexei Starovoitov 	default:
21422c78ee89SAlexei Starovoitov 		return false;
21432c78ee89SAlexei Starovoitov 	}
21442c78ee89SAlexei Starovoitov }
21452c78ee89SAlexei Starovoitov 
21462c78ee89SAlexei Starovoitov static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
21472c78ee89SAlexei Starovoitov {
21482c78ee89SAlexei Starovoitov 	switch (prog_type) {
21492c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_KPROBE:
21502c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_TRACEPOINT:
21512c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_PERF_EVENT:
21522c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_RAW_TRACEPOINT:
21532c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
21542c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_TRACING:
21552c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_LSM:
21562c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
21572c78ee89SAlexei Starovoitov 	case BPF_PROG_TYPE_EXT: /* extends any prog */
21582c78ee89SAlexei Starovoitov 		return true;
21592c78ee89SAlexei Starovoitov 	default:
21602c78ee89SAlexei Starovoitov 		return false;
21612c78ee89SAlexei Starovoitov 	}
21622c78ee89SAlexei Starovoitov }
21632c78ee89SAlexei Starovoitov 
216409756af4SAlexei Starovoitov /* last field in 'union bpf_attr' used by this command */
2165387544bfSAlexei Starovoitov #define	BPF_PROG_LOAD_LAST_FIELD fd_array
216609756af4SAlexei Starovoitov 
2167af2ac3e1SAlexei Starovoitov static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
216809756af4SAlexei Starovoitov {
216909756af4SAlexei Starovoitov 	enum bpf_prog_type type = attr->prog_type;
2170290248a5SAndrii Nakryiko 	struct bpf_prog *prog, *dst_prog = NULL;
2171290248a5SAndrii Nakryiko 	struct btf *attach_btf = NULL;
217209756af4SAlexei Starovoitov 	int err;
217309756af4SAlexei Starovoitov 	char license[128];
217409756af4SAlexei Starovoitov 	bool is_gpl;
217509756af4SAlexei Starovoitov 
217609756af4SAlexei Starovoitov 	if (CHECK_ATTR(BPF_PROG_LOAD))
217709756af4SAlexei Starovoitov 		return -EINVAL;
217809756af4SAlexei Starovoitov 
2179c240eff6SJiong Wang 	if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2180c240eff6SJiong Wang 				 BPF_F_ANY_ALIGNMENT |
218110d274e8SAlexei Starovoitov 				 BPF_F_TEST_STATE_FREQ |
21821e6c62a8SAlexei Starovoitov 				 BPF_F_SLEEPABLE |
2183c240eff6SJiong Wang 				 BPF_F_TEST_RND_HI32))
2184e07b98d9SDavid S. Miller 		return -EINVAL;
2185e07b98d9SDavid S. Miller 
2186e9ee9efcSDavid Miller 	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2187e9ee9efcSDavid Miller 	    (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
21882c78ee89SAlexei Starovoitov 	    !bpf_capable())
2189e9ee9efcSDavid Miller 		return -EPERM;
2190e9ee9efcSDavid Miller 
219109756af4SAlexei Starovoitov 	/* copy eBPF program license from user space */
2192af2ac3e1SAlexei Starovoitov 	if (strncpy_from_bpfptr(license,
2193af2ac3e1SAlexei Starovoitov 				make_bpfptr(attr->license, uattr.is_kernel),
219409756af4SAlexei Starovoitov 				sizeof(license) - 1) < 0)
219509756af4SAlexei Starovoitov 		return -EFAULT;
219609756af4SAlexei Starovoitov 	license[sizeof(license) - 1] = 0;
219709756af4SAlexei Starovoitov 
219809756af4SAlexei Starovoitov 	/* eBPF programs must be GPL compatible to use GPL-ed functions */
219909756af4SAlexei Starovoitov 	is_gpl = license_is_gpl_compatible(license);
220009756af4SAlexei Starovoitov 
2201c04c0d2bSAlexei Starovoitov 	if (attr->insn_cnt == 0 ||
22022c78ee89SAlexei Starovoitov 	    attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
2203ef0915caSDaniel Borkmann 		return -E2BIG;
220480b7d819SChenbo Feng 	if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
220580b7d819SChenbo Feng 	    type != BPF_PROG_TYPE_CGROUP_SKB &&
22062c78ee89SAlexei Starovoitov 	    !bpf_capable())
22072c78ee89SAlexei Starovoitov 		return -EPERM;
22082c78ee89SAlexei Starovoitov 
2209b338cb92SMaciej Żenczykowski 	if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
22102c78ee89SAlexei Starovoitov 		return -EPERM;
22112c78ee89SAlexei Starovoitov 	if (is_perfmon_prog_type(type) && !perfmon_capable())
22121be7f75dSAlexei Starovoitov 		return -EPERM;
22131be7f75dSAlexei Starovoitov 
2214290248a5SAndrii Nakryiko 	/* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2215290248a5SAndrii Nakryiko 	 * or btf, we need to check which one it is
2216290248a5SAndrii Nakryiko 	 */
2217290248a5SAndrii Nakryiko 	if (attr->attach_prog_fd) {
2218290248a5SAndrii Nakryiko 		dst_prog = bpf_prog_get(attr->attach_prog_fd);
2219290248a5SAndrii Nakryiko 		if (IS_ERR(dst_prog)) {
2220290248a5SAndrii Nakryiko 			dst_prog = NULL;
2221290248a5SAndrii Nakryiko 			attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2222290248a5SAndrii Nakryiko 			if (IS_ERR(attach_btf))
2223290248a5SAndrii Nakryiko 				return -EINVAL;
2224290248a5SAndrii Nakryiko 			if (!btf_is_kernel(attach_btf)) {
22258bdd8e27SAndrii Nakryiko 				/* attaching through specifying bpf_prog's BTF
22268bdd8e27SAndrii Nakryiko 				 * objects directly might be supported eventually
22278bdd8e27SAndrii Nakryiko 				 */
2228290248a5SAndrii Nakryiko 				btf_put(attach_btf);
22298bdd8e27SAndrii Nakryiko 				return -ENOTSUPP;
2230290248a5SAndrii Nakryiko 			}
2231290248a5SAndrii Nakryiko 		}
2232290248a5SAndrii Nakryiko 	} else if (attr->attach_btf_id) {
2233290248a5SAndrii Nakryiko 		/* fall back to vmlinux BTF, if BTF type ID is specified */
2234290248a5SAndrii Nakryiko 		attach_btf = bpf_get_btf_vmlinux();
2235290248a5SAndrii Nakryiko 		if (IS_ERR(attach_btf))
2236290248a5SAndrii Nakryiko 			return PTR_ERR(attach_btf);
2237290248a5SAndrii Nakryiko 		if (!attach_btf)
2238290248a5SAndrii Nakryiko 			return -EINVAL;
2239290248a5SAndrii Nakryiko 		btf_get(attach_btf);
2240290248a5SAndrii Nakryiko 	}
2241290248a5SAndrii Nakryiko 
2242aac3fc32SAndrey Ignatov 	bpf_prog_load_fixup_attach_type(attr);
2243ccfe29ebSAlexei Starovoitov 	if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
2244290248a5SAndrii Nakryiko 				       attach_btf, attr->attach_btf_id,
2245290248a5SAndrii Nakryiko 				       dst_prog)) {
2246290248a5SAndrii Nakryiko 		if (dst_prog)
2247290248a5SAndrii Nakryiko 			bpf_prog_put(dst_prog);
2248290248a5SAndrii Nakryiko 		if (attach_btf)
2249290248a5SAndrii Nakryiko 			btf_put(attach_btf);
22505e43f899SAndrey Ignatov 		return -EINVAL;
2251290248a5SAndrii Nakryiko 	}
22525e43f899SAndrey Ignatov 
225309756af4SAlexei Starovoitov 	/* plain bpf_prog allocation */
225409756af4SAlexei Starovoitov 	prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2255290248a5SAndrii Nakryiko 	if (!prog) {
2256290248a5SAndrii Nakryiko 		if (dst_prog)
2257290248a5SAndrii Nakryiko 			bpf_prog_put(dst_prog);
2258290248a5SAndrii Nakryiko 		if (attach_btf)
2259290248a5SAndrii Nakryiko 			btf_put(attach_btf);
226009756af4SAlexei Starovoitov 		return -ENOMEM;
2261290248a5SAndrii Nakryiko 	}
226209756af4SAlexei Starovoitov 
22635e43f899SAndrey Ignatov 	prog->expected_attach_type = attr->expected_attach_type;
2264290248a5SAndrii Nakryiko 	prog->aux->attach_btf = attach_btf;
2265ccfe29ebSAlexei Starovoitov 	prog->aux->attach_btf_id = attr->attach_btf_id;
22663aac1eadSToke Høiland-Jørgensen 	prog->aux->dst_prog = dst_prog;
22679a18eedbSJakub Kicinski 	prog->aux->offload_requested = !!attr->prog_ifindex;
22681e6c62a8SAlexei Starovoitov 	prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
22699a18eedbSJakub Kicinski 
2270afdb09c7SChenbo Feng 	err = security_bpf_prog_alloc(prog->aux);
2271aaac3ba9SAlexei Starovoitov 	if (err)
22723ac1f01bSRoman Gushchin 		goto free_prog;
2273aaac3ba9SAlexei Starovoitov 
22743ac1f01bSRoman Gushchin 	prog->aux->user = get_current_user();
227509756af4SAlexei Starovoitov 	prog->len = attr->insn_cnt;
227609756af4SAlexei Starovoitov 
227709756af4SAlexei Starovoitov 	err = -EFAULT;
2278af2ac3e1SAlexei Starovoitov 	if (copy_from_bpfptr(prog->insns,
2279af2ac3e1SAlexei Starovoitov 			     make_bpfptr(attr->insns, uattr.is_kernel),
2280aafe6ae9SDaniel Borkmann 			     bpf_prog_insn_size(prog)) != 0)
22813ac1f01bSRoman Gushchin 		goto free_prog_sec;
228209756af4SAlexei Starovoitov 
228309756af4SAlexei Starovoitov 	prog->orig_prog = NULL;
2284a91263d5SDaniel Borkmann 	prog->jited = 0;
228509756af4SAlexei Starovoitov 
228685192dbfSAndrii Nakryiko 	atomic64_set(&prog->aux->refcnt, 1);
2287a91263d5SDaniel Borkmann 	prog->gpl_compatible = is_gpl ? 1 : 0;
228809756af4SAlexei Starovoitov 
22899a18eedbSJakub Kicinski 	if (bpf_prog_is_dev_bound(prog->aux)) {
2290ab3f0063SJakub Kicinski 		err = bpf_prog_offload_init(prog, attr);
2291ab3f0063SJakub Kicinski 		if (err)
22923ac1f01bSRoman Gushchin 			goto free_prog_sec;
2293ab3f0063SJakub Kicinski 	}
2294ab3f0063SJakub Kicinski 
229509756af4SAlexei Starovoitov 	/* find program type: socket_filter vs tracing_filter */
229609756af4SAlexei Starovoitov 	err = find_prog_type(type, prog);
229709756af4SAlexei Starovoitov 	if (err < 0)
22983ac1f01bSRoman Gushchin 		goto free_prog_sec;
229909756af4SAlexei Starovoitov 
23009285ec4cSJason A. Donenfeld 	prog->aux->load_time = ktime_get_boottime_ns();
23018e7ae251SMartin KaFai Lau 	err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
23028e7ae251SMartin KaFai Lau 			       sizeof(attr->prog_name));
23038e7ae251SMartin KaFai Lau 	if (err < 0)
23043ac1f01bSRoman Gushchin 		goto free_prog_sec;
2305cb4d2b3fSMartin KaFai Lau 
230609756af4SAlexei Starovoitov 	/* run eBPF verifier */
2307838e9690SYonghong Song 	err = bpf_check(&prog, attr, uattr);
230809756af4SAlexei Starovoitov 	if (err < 0)
230909756af4SAlexei Starovoitov 		goto free_used_maps;
231009756af4SAlexei Starovoitov 
2311d1c55ab5SDaniel Borkmann 	prog = bpf_prog_select_runtime(prog, &err);
231204fd61abSAlexei Starovoitov 	if (err < 0)
231304fd61abSAlexei Starovoitov 		goto free_used_maps;
231409756af4SAlexei Starovoitov 
2315dc4bb0e2SMartin KaFai Lau 	err = bpf_prog_alloc_id(prog);
2316dc4bb0e2SMartin KaFai Lau 	if (err)
2317dc4bb0e2SMartin KaFai Lau 		goto free_used_maps;
2318dc4bb0e2SMartin KaFai Lau 
2319c751798aSDaniel Borkmann 	/* Upon success of bpf_prog_alloc_id(), the BPF prog is
2320c751798aSDaniel Borkmann 	 * effectively publicly exposed. However, retrieving via
2321c751798aSDaniel Borkmann 	 * bpf_prog_get_fd_by_id() will take another reference,
2322c751798aSDaniel Borkmann 	 * therefore it cannot be gone underneath us.
2323c751798aSDaniel Borkmann 	 *
2324c751798aSDaniel Borkmann 	 * Only for the time /after/ successful bpf_prog_new_fd()
2325c751798aSDaniel Borkmann 	 * and before returning to userspace, we might just hold
2326c751798aSDaniel Borkmann 	 * one reference and any parallel close on that fd could
2327c751798aSDaniel Borkmann 	 * rip everything out. Hence, below notifications must
2328c751798aSDaniel Borkmann 	 * happen before bpf_prog_new_fd().
2329c751798aSDaniel Borkmann 	 *
2330c751798aSDaniel Borkmann 	 * Also, any failure handling from this point onwards must
2331c751798aSDaniel Borkmann 	 * be using bpf_prog_put() given the program is exposed.
2332b16d9aa4SMartin KaFai Lau 	 */
233374451e66SDaniel Borkmann 	bpf_prog_kallsyms_add(prog);
23346ee52e2aSSong Liu 	perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
2335bae141f5SDaniel Borkmann 	bpf_audit_prog(prog, BPF_AUDIT_LOAD);
2336c751798aSDaniel Borkmann 
2337c751798aSDaniel Borkmann 	err = bpf_prog_new_fd(prog);
2338c751798aSDaniel Borkmann 	if (err < 0)
2339c751798aSDaniel Borkmann 		bpf_prog_put(prog);
234009756af4SAlexei Starovoitov 	return err;
234109756af4SAlexei Starovoitov 
234209756af4SAlexei Starovoitov free_used_maps:
2343cd7455f1SDaniel Borkmann 	/* In case we have subprogs, we need to wait for a grace
2344cd7455f1SDaniel Borkmann 	 * period before we can tear down JIT memory since symbols
2345cd7455f1SDaniel Borkmann 	 * are already exposed under kallsyms.
2346cd7455f1SDaniel Borkmann 	 */
2347cd7455f1SDaniel Borkmann 	__bpf_prog_put_noref(prog, prog->aux->func_cnt);
2348cd7455f1SDaniel Borkmann 	return err;
2349afdb09c7SChenbo Feng free_prog_sec:
23503ac1f01bSRoman Gushchin 	free_uid(prog->aux->user);
2351afdb09c7SChenbo Feng 	security_bpf_prog_free(prog->aux);
23523ac1f01bSRoman Gushchin free_prog:
235322dc4a0fSAndrii Nakryiko 	if (prog->aux->attach_btf)
235422dc4a0fSAndrii Nakryiko 		btf_put(prog->aux->attach_btf);
235509756af4SAlexei Starovoitov 	bpf_prog_free(prog);
235609756af4SAlexei Starovoitov 	return err;
235709756af4SAlexei Starovoitov }
235809756af4SAlexei Starovoitov 
23596e71b04aSChenbo Feng #define BPF_OBJ_LAST_FIELD file_flags
2360b2197755SDaniel Borkmann 
2361b2197755SDaniel Borkmann static int bpf_obj_pin(const union bpf_attr *attr)
2362b2197755SDaniel Borkmann {
23636e71b04aSChenbo Feng 	if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
2364b2197755SDaniel Borkmann 		return -EINVAL;
2365b2197755SDaniel Borkmann 
2366535e7b4bSMickaël Salaün 	return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
2367b2197755SDaniel Borkmann }
2368b2197755SDaniel Borkmann 
2369b2197755SDaniel Borkmann static int bpf_obj_get(const union bpf_attr *attr)
2370b2197755SDaniel Borkmann {
23716e71b04aSChenbo Feng 	if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
23726e71b04aSChenbo Feng 	    attr->file_flags & ~BPF_OBJ_FLAG_MASK)
2373b2197755SDaniel Borkmann 		return -EINVAL;
2374b2197755SDaniel Borkmann 
23756e71b04aSChenbo Feng 	return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
23766e71b04aSChenbo Feng 				attr->file_flags);
2377b2197755SDaniel Borkmann }
2378b2197755SDaniel Borkmann 
2379f2e10bffSAndrii Nakryiko void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2380a3b80e10SAndrii Nakryiko 		   const struct bpf_link_ops *ops, struct bpf_prog *prog)
238170ed506cSAndrii Nakryiko {
238270ed506cSAndrii Nakryiko 	atomic64_set(&link->refcnt, 1);
2383f2e10bffSAndrii Nakryiko 	link->type = type;
2384a3b80e10SAndrii Nakryiko 	link->id = 0;
238570ed506cSAndrii Nakryiko 	link->ops = ops;
238670ed506cSAndrii Nakryiko 	link->prog = prog;
238770ed506cSAndrii Nakryiko }
238870ed506cSAndrii Nakryiko 
2389a3b80e10SAndrii Nakryiko static void bpf_link_free_id(int id)
2390a3b80e10SAndrii Nakryiko {
2391a3b80e10SAndrii Nakryiko 	if (!id)
2392a3b80e10SAndrii Nakryiko 		return;
2393a3b80e10SAndrii Nakryiko 
2394a3b80e10SAndrii Nakryiko 	spin_lock_bh(&link_idr_lock);
2395a3b80e10SAndrii Nakryiko 	idr_remove(&link_idr, id);
2396a3b80e10SAndrii Nakryiko 	spin_unlock_bh(&link_idr_lock);
2397a3b80e10SAndrii Nakryiko }
2398a3b80e10SAndrii Nakryiko 
239998868668SAndrii Nakryiko /* Clean up bpf_link and corresponding anon_inode file and FD. After
240098868668SAndrii Nakryiko  * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
2401a3b80e10SAndrii Nakryiko  * anon_inode's release() call. This helper marksbpf_link as
2402a3b80e10SAndrii Nakryiko  * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2403a3b80e10SAndrii Nakryiko  * is not decremented, it's the responsibility of a calling code that failed
2404a3b80e10SAndrii Nakryiko  * to complete bpf_link initialization.
240598868668SAndrii Nakryiko  */
2406a3b80e10SAndrii Nakryiko void bpf_link_cleanup(struct bpf_link_primer *primer)
2407babf3164SAndrii Nakryiko {
2408a3b80e10SAndrii Nakryiko 	primer->link->prog = NULL;
2409a3b80e10SAndrii Nakryiko 	bpf_link_free_id(primer->id);
2410a3b80e10SAndrii Nakryiko 	fput(primer->file);
2411a3b80e10SAndrii Nakryiko 	put_unused_fd(primer->fd);
2412babf3164SAndrii Nakryiko }
2413babf3164SAndrii Nakryiko 
241470ed506cSAndrii Nakryiko void bpf_link_inc(struct bpf_link *link)
241570ed506cSAndrii Nakryiko {
241670ed506cSAndrii Nakryiko 	atomic64_inc(&link->refcnt);
241770ed506cSAndrii Nakryiko }
241870ed506cSAndrii Nakryiko 
241970ed506cSAndrii Nakryiko /* bpf_link_free is guaranteed to be called from process context */
242070ed506cSAndrii Nakryiko static void bpf_link_free(struct bpf_link *link)
242170ed506cSAndrii Nakryiko {
2422a3b80e10SAndrii Nakryiko 	bpf_link_free_id(link->id);
2423babf3164SAndrii Nakryiko 	if (link->prog) {
2424babf3164SAndrii Nakryiko 		/* detach BPF program, clean up used resources */
242570ed506cSAndrii Nakryiko 		link->ops->release(link);
2426babf3164SAndrii Nakryiko 		bpf_prog_put(link->prog);
2427babf3164SAndrii Nakryiko 	}
2428babf3164SAndrii Nakryiko 	/* free bpf_link and its containing memory */
2429babf3164SAndrii Nakryiko 	link->ops->dealloc(link);
243070ed506cSAndrii Nakryiko }
243170ed506cSAndrii Nakryiko 
243270ed506cSAndrii Nakryiko static void bpf_link_put_deferred(struct work_struct *work)
243370ed506cSAndrii Nakryiko {
243470ed506cSAndrii Nakryiko 	struct bpf_link *link = container_of(work, struct bpf_link, work);
243570ed506cSAndrii Nakryiko 
243670ed506cSAndrii Nakryiko 	bpf_link_free(link);
243770ed506cSAndrii Nakryiko }
243870ed506cSAndrii Nakryiko 
243970ed506cSAndrii Nakryiko /* bpf_link_put can be called from atomic context, but ensures that resources
244070ed506cSAndrii Nakryiko  * are freed from process context
244170ed506cSAndrii Nakryiko  */
244270ed506cSAndrii Nakryiko void bpf_link_put(struct bpf_link *link)
244370ed506cSAndrii Nakryiko {
244470ed506cSAndrii Nakryiko 	if (!atomic64_dec_and_test(&link->refcnt))
244570ed506cSAndrii Nakryiko 		return;
244670ed506cSAndrii Nakryiko 
2447f00f2f7fSAlexei Starovoitov 	if (in_atomic()) {
244870ed506cSAndrii Nakryiko 		INIT_WORK(&link->work, bpf_link_put_deferred);
244970ed506cSAndrii Nakryiko 		schedule_work(&link->work);
2450f00f2f7fSAlexei Starovoitov 	} else {
2451f00f2f7fSAlexei Starovoitov 		bpf_link_free(link);
2452f00f2f7fSAlexei Starovoitov 	}
245370ed506cSAndrii Nakryiko }
245470ed506cSAndrii Nakryiko 
245570ed506cSAndrii Nakryiko static int bpf_link_release(struct inode *inode, struct file *filp)
245670ed506cSAndrii Nakryiko {
245770ed506cSAndrii Nakryiko 	struct bpf_link *link = filp->private_data;
245870ed506cSAndrii Nakryiko 
245970ed506cSAndrii Nakryiko 	bpf_link_put(link);
2460fec56f58SAlexei Starovoitov 	return 0;
2461fec56f58SAlexei Starovoitov }
2462fec56f58SAlexei Starovoitov 
246370ed506cSAndrii Nakryiko #ifdef CONFIG_PROC_FS
2464f2e10bffSAndrii Nakryiko #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2465f2e10bffSAndrii Nakryiko #define BPF_MAP_TYPE(_id, _ops)
2466f2e10bffSAndrii Nakryiko #define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2467f2e10bffSAndrii Nakryiko static const char *bpf_link_type_strs[] = {
2468f2e10bffSAndrii Nakryiko 	[BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2469f2e10bffSAndrii Nakryiko #include <linux/bpf_types.h>
2470f2e10bffSAndrii Nakryiko };
2471f2e10bffSAndrii Nakryiko #undef BPF_PROG_TYPE
2472f2e10bffSAndrii Nakryiko #undef BPF_MAP_TYPE
2473f2e10bffSAndrii Nakryiko #undef BPF_LINK_TYPE
247470ed506cSAndrii Nakryiko 
247570ed506cSAndrii Nakryiko static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
247670ed506cSAndrii Nakryiko {
247770ed506cSAndrii Nakryiko 	const struct bpf_link *link = filp->private_data;
247870ed506cSAndrii Nakryiko 	const struct bpf_prog *prog = link->prog;
247970ed506cSAndrii Nakryiko 	char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
248070ed506cSAndrii Nakryiko 
248170ed506cSAndrii Nakryiko 	bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
248270ed506cSAndrii Nakryiko 	seq_printf(m,
248370ed506cSAndrii Nakryiko 		   "link_type:\t%s\n"
2484a3b80e10SAndrii Nakryiko 		   "link_id:\t%u\n"
248570ed506cSAndrii Nakryiko 		   "prog_tag:\t%s\n"
248670ed506cSAndrii Nakryiko 		   "prog_id:\t%u\n",
2487f2e10bffSAndrii Nakryiko 		   bpf_link_type_strs[link->type],
2488a3b80e10SAndrii Nakryiko 		   link->id,
248970ed506cSAndrii Nakryiko 		   prog_tag,
249070ed506cSAndrii Nakryiko 		   prog->aux->id);
2491f2e10bffSAndrii Nakryiko 	if (link->ops->show_fdinfo)
2492f2e10bffSAndrii Nakryiko 		link->ops->show_fdinfo(link, m);
249370ed506cSAndrii Nakryiko }
249470ed506cSAndrii Nakryiko #endif
249570ed506cSAndrii Nakryiko 
24966f302bfbSZou Wei static const struct file_operations bpf_link_fops = {
249770ed506cSAndrii Nakryiko #ifdef CONFIG_PROC_FS
249870ed506cSAndrii Nakryiko 	.show_fdinfo	= bpf_link_show_fdinfo,
249970ed506cSAndrii Nakryiko #endif
250070ed506cSAndrii Nakryiko 	.release	= bpf_link_release,
2501fec56f58SAlexei Starovoitov 	.read		= bpf_dummy_read,
2502fec56f58SAlexei Starovoitov 	.write		= bpf_dummy_write,
2503fec56f58SAlexei Starovoitov };
2504fec56f58SAlexei Starovoitov 
2505a3b80e10SAndrii Nakryiko static int bpf_link_alloc_id(struct bpf_link *link)
250670ed506cSAndrii Nakryiko {
2507a3b80e10SAndrii Nakryiko 	int id;
2508a3b80e10SAndrii Nakryiko 
2509a3b80e10SAndrii Nakryiko 	idr_preload(GFP_KERNEL);
2510a3b80e10SAndrii Nakryiko 	spin_lock_bh(&link_idr_lock);
2511a3b80e10SAndrii Nakryiko 	id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2512a3b80e10SAndrii Nakryiko 	spin_unlock_bh(&link_idr_lock);
2513a3b80e10SAndrii Nakryiko 	idr_preload_end();
2514a3b80e10SAndrii Nakryiko 
2515a3b80e10SAndrii Nakryiko 	return id;
251670ed506cSAndrii Nakryiko }
251770ed506cSAndrii Nakryiko 
2518a3b80e10SAndrii Nakryiko /* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2519a3b80e10SAndrii Nakryiko  * reserving unused FD and allocating ID from link_idr. This is to be paired
2520a3b80e10SAndrii Nakryiko  * with bpf_link_settle() to install FD and ID and expose bpf_link to
2521a3b80e10SAndrii Nakryiko  * user-space, if bpf_link is successfully attached. If not, bpf_link and
2522a3b80e10SAndrii Nakryiko  * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2523a3b80e10SAndrii Nakryiko  * transient state is passed around in struct bpf_link_primer.
2524a3b80e10SAndrii Nakryiko  * This is preferred way to create and initialize bpf_link, especially when
2525a3b80e10SAndrii Nakryiko  * there are complicated and expensive operations inbetween creating bpf_link
2526a3b80e10SAndrii Nakryiko  * itself and attaching it to BPF hook. By using bpf_link_prime() and
2527a3b80e10SAndrii Nakryiko  * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2528a3b80e10SAndrii Nakryiko  * expensive (and potentially failing) roll back operations in a rare case
2529a3b80e10SAndrii Nakryiko  * that file, FD, or ID can't be allocated.
2530babf3164SAndrii Nakryiko  */
2531a3b80e10SAndrii Nakryiko int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
2532babf3164SAndrii Nakryiko {
2533babf3164SAndrii Nakryiko 	struct file *file;
2534a3b80e10SAndrii Nakryiko 	int fd, id;
2535babf3164SAndrii Nakryiko 
2536babf3164SAndrii Nakryiko 	fd = get_unused_fd_flags(O_CLOEXEC);
2537babf3164SAndrii Nakryiko 	if (fd < 0)
2538a3b80e10SAndrii Nakryiko 		return fd;
2539babf3164SAndrii Nakryiko 
2540babf3164SAndrii Nakryiko 
2541a3b80e10SAndrii Nakryiko 	id = bpf_link_alloc_id(link);
2542a3b80e10SAndrii Nakryiko 	if (id < 0) {
2543a3b80e10SAndrii Nakryiko 		put_unused_fd(fd);
2544a3b80e10SAndrii Nakryiko 		return id;
2545a3b80e10SAndrii Nakryiko 	}
2546babf3164SAndrii Nakryiko 
2547babf3164SAndrii Nakryiko 	file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2548babf3164SAndrii Nakryiko 	if (IS_ERR(file)) {
2549138c6767SAndrii Nakryiko 		bpf_link_free_id(id);
2550babf3164SAndrii Nakryiko 		put_unused_fd(fd);
2551138c6767SAndrii Nakryiko 		return PTR_ERR(file);
2552babf3164SAndrii Nakryiko 	}
2553babf3164SAndrii Nakryiko 
2554a3b80e10SAndrii Nakryiko 	primer->link = link;
2555a3b80e10SAndrii Nakryiko 	primer->file = file;
2556a3b80e10SAndrii Nakryiko 	primer->fd = fd;
2557a3b80e10SAndrii Nakryiko 	primer->id = id;
2558a3b80e10SAndrii Nakryiko 	return 0;
2559a3b80e10SAndrii Nakryiko }
2560a3b80e10SAndrii Nakryiko 
2561a3b80e10SAndrii Nakryiko int bpf_link_settle(struct bpf_link_primer *primer)
2562a3b80e10SAndrii Nakryiko {
2563a3b80e10SAndrii Nakryiko 	/* make bpf_link fetchable by ID */
2564a3b80e10SAndrii Nakryiko 	spin_lock_bh(&link_idr_lock);
2565a3b80e10SAndrii Nakryiko 	primer->link->id = primer->id;
2566a3b80e10SAndrii Nakryiko 	spin_unlock_bh(&link_idr_lock);
2567a3b80e10SAndrii Nakryiko 	/* make bpf_link fetchable by FD */
2568a3b80e10SAndrii Nakryiko 	fd_install(primer->fd, primer->file);
2569a3b80e10SAndrii Nakryiko 	/* pass through installed FD */
2570a3b80e10SAndrii Nakryiko 	return primer->fd;
2571a3b80e10SAndrii Nakryiko }
2572a3b80e10SAndrii Nakryiko 
2573a3b80e10SAndrii Nakryiko int bpf_link_new_fd(struct bpf_link *link)
2574a3b80e10SAndrii Nakryiko {
2575a3b80e10SAndrii Nakryiko 	return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
2576babf3164SAndrii Nakryiko }
2577babf3164SAndrii Nakryiko 
257870ed506cSAndrii Nakryiko struct bpf_link *bpf_link_get_from_fd(u32 ufd)
257970ed506cSAndrii Nakryiko {
258070ed506cSAndrii Nakryiko 	struct fd f = fdget(ufd);
258170ed506cSAndrii Nakryiko 	struct bpf_link *link;
258270ed506cSAndrii Nakryiko 
258370ed506cSAndrii Nakryiko 	if (!f.file)
258470ed506cSAndrii Nakryiko 		return ERR_PTR(-EBADF);
258570ed506cSAndrii Nakryiko 	if (f.file->f_op != &bpf_link_fops) {
258670ed506cSAndrii Nakryiko 		fdput(f);
258770ed506cSAndrii Nakryiko 		return ERR_PTR(-EINVAL);
258870ed506cSAndrii Nakryiko 	}
258970ed506cSAndrii Nakryiko 
259070ed506cSAndrii Nakryiko 	link = f.file->private_data;
259170ed506cSAndrii Nakryiko 	bpf_link_inc(link);
259270ed506cSAndrii Nakryiko 	fdput(f);
259370ed506cSAndrii Nakryiko 
259470ed506cSAndrii Nakryiko 	return link;
259570ed506cSAndrii Nakryiko }
259670ed506cSAndrii Nakryiko 
259770ed506cSAndrii Nakryiko struct bpf_tracing_link {
259870ed506cSAndrii Nakryiko 	struct bpf_link link;
2599f2e10bffSAndrii Nakryiko 	enum bpf_attach_type attach_type;
26003aac1eadSToke Høiland-Jørgensen 	struct bpf_trampoline *trampoline;
26013aac1eadSToke Høiland-Jørgensen 	struct bpf_prog *tgt_prog;
260270ed506cSAndrii Nakryiko };
260370ed506cSAndrii Nakryiko 
260470ed506cSAndrii Nakryiko static void bpf_tracing_link_release(struct bpf_link *link)
260570ed506cSAndrii Nakryiko {
26063aac1eadSToke Høiland-Jørgensen 	struct bpf_tracing_link *tr_link =
26073aac1eadSToke Høiland-Jørgensen 		container_of(link, struct bpf_tracing_link, link);
26083aac1eadSToke Høiland-Jørgensen 
26093aac1eadSToke Høiland-Jørgensen 	WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
26103aac1eadSToke Høiland-Jørgensen 						tr_link->trampoline));
26113aac1eadSToke Høiland-Jørgensen 
26123aac1eadSToke Høiland-Jørgensen 	bpf_trampoline_put(tr_link->trampoline);
26133aac1eadSToke Høiland-Jørgensen 
26143aac1eadSToke Høiland-Jørgensen 	/* tgt_prog is NULL if target is a kernel function */
26153aac1eadSToke Høiland-Jørgensen 	if (tr_link->tgt_prog)
26163aac1eadSToke Høiland-Jørgensen 		bpf_prog_put(tr_link->tgt_prog);
2617babf3164SAndrii Nakryiko }
2618babf3164SAndrii Nakryiko 
2619babf3164SAndrii Nakryiko static void bpf_tracing_link_dealloc(struct bpf_link *link)
2620babf3164SAndrii Nakryiko {
262170ed506cSAndrii Nakryiko 	struct bpf_tracing_link *tr_link =
262270ed506cSAndrii Nakryiko 		container_of(link, struct bpf_tracing_link, link);
262370ed506cSAndrii Nakryiko 
262470ed506cSAndrii Nakryiko 	kfree(tr_link);
262570ed506cSAndrii Nakryiko }
262670ed506cSAndrii Nakryiko 
2627f2e10bffSAndrii Nakryiko static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2628f2e10bffSAndrii Nakryiko 					 struct seq_file *seq)
2629f2e10bffSAndrii Nakryiko {
2630f2e10bffSAndrii Nakryiko 	struct bpf_tracing_link *tr_link =
2631f2e10bffSAndrii Nakryiko 		container_of(link, struct bpf_tracing_link, link);
2632f2e10bffSAndrii Nakryiko 
2633f2e10bffSAndrii Nakryiko 	seq_printf(seq,
2634f2e10bffSAndrii Nakryiko 		   "attach_type:\t%d\n",
2635f2e10bffSAndrii Nakryiko 		   tr_link->attach_type);
2636f2e10bffSAndrii Nakryiko }
2637f2e10bffSAndrii Nakryiko 
2638f2e10bffSAndrii Nakryiko static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2639f2e10bffSAndrii Nakryiko 					   struct bpf_link_info *info)
2640f2e10bffSAndrii Nakryiko {
2641f2e10bffSAndrii Nakryiko 	struct bpf_tracing_link *tr_link =
2642f2e10bffSAndrii Nakryiko 		container_of(link, struct bpf_tracing_link, link);
2643f2e10bffSAndrii Nakryiko 
2644f2e10bffSAndrii Nakryiko 	info->tracing.attach_type = tr_link->attach_type;
2645441e8c66SToke Høiland-Jørgensen 	bpf_trampoline_unpack_key(tr_link->trampoline->key,
2646441e8c66SToke Høiland-Jørgensen 				  &info->tracing.target_obj_id,
2647441e8c66SToke Høiland-Jørgensen 				  &info->tracing.target_btf_id);
2648f2e10bffSAndrii Nakryiko 
2649f2e10bffSAndrii Nakryiko 	return 0;
2650f2e10bffSAndrii Nakryiko }
2651f2e10bffSAndrii Nakryiko 
265270ed506cSAndrii Nakryiko static const struct bpf_link_ops bpf_tracing_link_lops = {
265370ed506cSAndrii Nakryiko 	.release = bpf_tracing_link_release,
2654babf3164SAndrii Nakryiko 	.dealloc = bpf_tracing_link_dealloc,
2655f2e10bffSAndrii Nakryiko 	.show_fdinfo = bpf_tracing_link_show_fdinfo,
2656f2e10bffSAndrii Nakryiko 	.fill_link_info = bpf_tracing_link_fill_link_info,
265770ed506cSAndrii Nakryiko };
265870ed506cSAndrii Nakryiko 
26594a1e7c0cSToke Høiland-Jørgensen static int bpf_tracing_prog_attach(struct bpf_prog *prog,
26604a1e7c0cSToke Høiland-Jørgensen 				   int tgt_prog_fd,
26614a1e7c0cSToke Høiland-Jørgensen 				   u32 btf_id)
2662fec56f58SAlexei Starovoitov {
2663a3b80e10SAndrii Nakryiko 	struct bpf_link_primer link_primer;
26643aac1eadSToke Høiland-Jørgensen 	struct bpf_prog *tgt_prog = NULL;
26654a1e7c0cSToke Høiland-Jørgensen 	struct bpf_trampoline *tr = NULL;
266670ed506cSAndrii Nakryiko 	struct bpf_tracing_link *link;
26674a1e7c0cSToke Høiland-Jørgensen 	u64 key = 0;
2668a3b80e10SAndrii Nakryiko 	int err;
2669fec56f58SAlexei Starovoitov 
26709e4e01dfSKP Singh 	switch (prog->type) {
26719e4e01dfSKP Singh 	case BPF_PROG_TYPE_TRACING:
2672fec56f58SAlexei Starovoitov 		if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2673be8704ffSAlexei Starovoitov 		    prog->expected_attach_type != BPF_TRACE_FEXIT &&
26749e4e01dfSKP Singh 		    prog->expected_attach_type != BPF_MODIFY_RETURN) {
26759e4e01dfSKP Singh 			err = -EINVAL;
26769e4e01dfSKP Singh 			goto out_put_prog;
26779e4e01dfSKP Singh 		}
26789e4e01dfSKP Singh 		break;
26799e4e01dfSKP Singh 	case BPF_PROG_TYPE_EXT:
26809e4e01dfSKP Singh 		if (prog->expected_attach_type != 0) {
26819e4e01dfSKP Singh 			err = -EINVAL;
26829e4e01dfSKP Singh 			goto out_put_prog;
26839e4e01dfSKP Singh 		}
26849e4e01dfSKP Singh 		break;
26859e4e01dfSKP Singh 	case BPF_PROG_TYPE_LSM:
26869e4e01dfSKP Singh 		if (prog->expected_attach_type != BPF_LSM_MAC) {
26879e4e01dfSKP Singh 			err = -EINVAL;
26889e4e01dfSKP Singh 			goto out_put_prog;
26899e4e01dfSKP Singh 		}
26909e4e01dfSKP Singh 		break;
26919e4e01dfSKP Singh 	default:
2692fec56f58SAlexei Starovoitov 		err = -EINVAL;
2693fec56f58SAlexei Starovoitov 		goto out_put_prog;
2694fec56f58SAlexei Starovoitov 	}
2695fec56f58SAlexei Starovoitov 
26964a1e7c0cSToke Høiland-Jørgensen 	if (!!tgt_prog_fd != !!btf_id) {
26974a1e7c0cSToke Høiland-Jørgensen 		err = -EINVAL;
26984a1e7c0cSToke Høiland-Jørgensen 		goto out_put_prog;
26994a1e7c0cSToke Høiland-Jørgensen 	}
27004a1e7c0cSToke Høiland-Jørgensen 
27014a1e7c0cSToke Høiland-Jørgensen 	if (tgt_prog_fd) {
27024a1e7c0cSToke Høiland-Jørgensen 		/* For now we only allow new targets for BPF_PROG_TYPE_EXT */
27034a1e7c0cSToke Høiland-Jørgensen 		if (prog->type != BPF_PROG_TYPE_EXT) {
27044a1e7c0cSToke Høiland-Jørgensen 			err = -EINVAL;
27054a1e7c0cSToke Høiland-Jørgensen 			goto out_put_prog;
27064a1e7c0cSToke Høiland-Jørgensen 		}
27074a1e7c0cSToke Høiland-Jørgensen 
27084a1e7c0cSToke Høiland-Jørgensen 		tgt_prog = bpf_prog_get(tgt_prog_fd);
27094a1e7c0cSToke Høiland-Jørgensen 		if (IS_ERR(tgt_prog)) {
27104a1e7c0cSToke Høiland-Jørgensen 			err = PTR_ERR(tgt_prog);
27114a1e7c0cSToke Høiland-Jørgensen 			tgt_prog = NULL;
27124a1e7c0cSToke Høiland-Jørgensen 			goto out_put_prog;
27134a1e7c0cSToke Høiland-Jørgensen 		}
27144a1e7c0cSToke Høiland-Jørgensen 
271522dc4a0fSAndrii Nakryiko 		key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
27164a1e7c0cSToke Høiland-Jørgensen 	}
27174a1e7c0cSToke Høiland-Jørgensen 
271870ed506cSAndrii Nakryiko 	link = kzalloc(sizeof(*link), GFP_USER);
271970ed506cSAndrii Nakryiko 	if (!link) {
272070ed506cSAndrii Nakryiko 		err = -ENOMEM;
2721fec56f58SAlexei Starovoitov 		goto out_put_prog;
2722fec56f58SAlexei Starovoitov 	}
2723f2e10bffSAndrii Nakryiko 	bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2724f2e10bffSAndrii Nakryiko 		      &bpf_tracing_link_lops, prog);
2725f2e10bffSAndrii Nakryiko 	link->attach_type = prog->expected_attach_type;
2726fec56f58SAlexei Starovoitov 
27273aac1eadSToke Høiland-Jørgensen 	mutex_lock(&prog->aux->dst_mutex);
2728babf3164SAndrii Nakryiko 
27294a1e7c0cSToke Høiland-Jørgensen 	/* There are a few possible cases here:
27304a1e7c0cSToke Høiland-Jørgensen 	 *
27314a1e7c0cSToke Høiland-Jørgensen 	 * - if prog->aux->dst_trampoline is set, the program was just loaded
27324a1e7c0cSToke Høiland-Jørgensen 	 *   and not yet attached to anything, so we can use the values stored
27334a1e7c0cSToke Høiland-Jørgensen 	 *   in prog->aux
27344a1e7c0cSToke Høiland-Jørgensen 	 *
27354a1e7c0cSToke Høiland-Jørgensen 	 * - if prog->aux->dst_trampoline is NULL, the program has already been
27364a1e7c0cSToke Høiland-Jørgensen          *   attached to a target and its initial target was cleared (below)
27374a1e7c0cSToke Høiland-Jørgensen 	 *
27384a1e7c0cSToke Høiland-Jørgensen 	 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
27394a1e7c0cSToke Høiland-Jørgensen 	 *   target_btf_id using the link_create API.
27404a1e7c0cSToke Høiland-Jørgensen 	 *
27414a1e7c0cSToke Høiland-Jørgensen 	 * - if tgt_prog == NULL when this function was called using the old
27424a1e7c0cSToke Høiland-Jørgensen 	 *   raw_tracepoint_open API, and we need a target from prog->aux
27434a1e7c0cSToke Høiland-Jørgensen 	 *
2744f3a95075SJiri Olsa 	 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
2745f3a95075SJiri Olsa 	 *   was detached and is going for re-attachment.
27464a1e7c0cSToke Høiland-Jørgensen 	 */
27474a1e7c0cSToke Høiland-Jørgensen 	if (!prog->aux->dst_trampoline && !tgt_prog) {
2748f3a95075SJiri Olsa 		/*
2749f3a95075SJiri Olsa 		 * Allow re-attach for TRACING and LSM programs. If it's
2750f3a95075SJiri Olsa 		 * currently linked, bpf_trampoline_link_prog will fail.
2751f3a95075SJiri Olsa 		 * EXT programs need to specify tgt_prog_fd, so they
2752f3a95075SJiri Olsa 		 * re-attach in separate code path.
2753f3a95075SJiri Olsa 		 */
2754f3a95075SJiri Olsa 		if (prog->type != BPF_PROG_TYPE_TRACING &&
2755f3a95075SJiri Olsa 		    prog->type != BPF_PROG_TYPE_LSM) {
2756f3a95075SJiri Olsa 			err = -EINVAL;
27573aac1eadSToke Høiland-Jørgensen 			goto out_unlock;
27583aac1eadSToke Høiland-Jørgensen 		}
2759f3a95075SJiri Olsa 		btf_id = prog->aux->attach_btf_id;
2760f3a95075SJiri Olsa 		key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
2761f3a95075SJiri Olsa 	}
27624a1e7c0cSToke Høiland-Jørgensen 
27634a1e7c0cSToke Høiland-Jørgensen 	if (!prog->aux->dst_trampoline ||
27644a1e7c0cSToke Høiland-Jørgensen 	    (key && key != prog->aux->dst_trampoline->key)) {
27654a1e7c0cSToke Høiland-Jørgensen 		/* If there is no saved target, or the specified target is
27664a1e7c0cSToke Høiland-Jørgensen 		 * different from the destination specified at load time, we
27674a1e7c0cSToke Høiland-Jørgensen 		 * need a new trampoline and a check for compatibility
27684a1e7c0cSToke Høiland-Jørgensen 		 */
27694a1e7c0cSToke Høiland-Jørgensen 		struct bpf_attach_target_info tgt_info = {};
27704a1e7c0cSToke Høiland-Jørgensen 
27714a1e7c0cSToke Høiland-Jørgensen 		err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
27724a1e7c0cSToke Høiland-Jørgensen 					      &tgt_info);
27734a1e7c0cSToke Høiland-Jørgensen 		if (err)
27744a1e7c0cSToke Høiland-Jørgensen 			goto out_unlock;
27754a1e7c0cSToke Høiland-Jørgensen 
27764a1e7c0cSToke Høiland-Jørgensen 		tr = bpf_trampoline_get(key, &tgt_info);
27774a1e7c0cSToke Høiland-Jørgensen 		if (!tr) {
27784a1e7c0cSToke Høiland-Jørgensen 			err = -ENOMEM;
27794a1e7c0cSToke Høiland-Jørgensen 			goto out_unlock;
27804a1e7c0cSToke Høiland-Jørgensen 		}
27814a1e7c0cSToke Høiland-Jørgensen 	} else {
27824a1e7c0cSToke Høiland-Jørgensen 		/* The caller didn't specify a target, or the target was the
27834a1e7c0cSToke Høiland-Jørgensen 		 * same as the destination supplied during program load. This
27844a1e7c0cSToke Høiland-Jørgensen 		 * means we can reuse the trampoline and reference from program
27854a1e7c0cSToke Høiland-Jørgensen 		 * load time, and there is no need to allocate a new one. This
27864a1e7c0cSToke Høiland-Jørgensen 		 * can only happen once for any program, as the saved values in
27874a1e7c0cSToke Høiland-Jørgensen 		 * prog->aux are cleared below.
27884a1e7c0cSToke Høiland-Jørgensen 		 */
27893aac1eadSToke Høiland-Jørgensen 		tr = prog->aux->dst_trampoline;
27903aac1eadSToke Høiland-Jørgensen 		tgt_prog = prog->aux->dst_prog;
27914a1e7c0cSToke Høiland-Jørgensen 	}
27923aac1eadSToke Høiland-Jørgensen 
27933aac1eadSToke Høiland-Jørgensen 	err = bpf_link_prime(&link->link, &link_primer);
27943aac1eadSToke Høiland-Jørgensen 	if (err)
27953aac1eadSToke Høiland-Jørgensen 		goto out_unlock;
27963aac1eadSToke Høiland-Jørgensen 
27973aac1eadSToke Høiland-Jørgensen 	err = bpf_trampoline_link_prog(prog, tr);
2798babf3164SAndrii Nakryiko 	if (err) {
2799a3b80e10SAndrii Nakryiko 		bpf_link_cleanup(&link_primer);
28003aac1eadSToke Høiland-Jørgensen 		link = NULL;
28013aac1eadSToke Høiland-Jørgensen 		goto out_unlock;
2802babf3164SAndrii Nakryiko 	}
2803babf3164SAndrii Nakryiko 
28043aac1eadSToke Høiland-Jørgensen 	link->tgt_prog = tgt_prog;
28053aac1eadSToke Høiland-Jørgensen 	link->trampoline = tr;
28063aac1eadSToke Høiland-Jørgensen 
28074a1e7c0cSToke Høiland-Jørgensen 	/* Always clear the trampoline and target prog from prog->aux to make
28084a1e7c0cSToke Høiland-Jørgensen 	 * sure the original attach destination is not kept alive after a
28094a1e7c0cSToke Høiland-Jørgensen 	 * program is (re-)attached to another target.
28104a1e7c0cSToke Høiland-Jørgensen 	 */
28114a1e7c0cSToke Høiland-Jørgensen 	if (prog->aux->dst_prog &&
28124a1e7c0cSToke Høiland-Jørgensen 	    (tgt_prog_fd || tr != prog->aux->dst_trampoline))
28134a1e7c0cSToke Høiland-Jørgensen 		/* got extra prog ref from syscall, or attaching to different prog */
28144a1e7c0cSToke Høiland-Jørgensen 		bpf_prog_put(prog->aux->dst_prog);
28154a1e7c0cSToke Høiland-Jørgensen 	if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
28164a1e7c0cSToke Høiland-Jørgensen 		/* we allocated a new trampoline, so free the old one */
28174a1e7c0cSToke Høiland-Jørgensen 		bpf_trampoline_put(prog->aux->dst_trampoline);
28184a1e7c0cSToke Høiland-Jørgensen 
28193aac1eadSToke Høiland-Jørgensen 	prog->aux->dst_prog = NULL;
28203aac1eadSToke Høiland-Jørgensen 	prog->aux->dst_trampoline = NULL;
28213aac1eadSToke Høiland-Jørgensen 	mutex_unlock(&prog->aux->dst_mutex);
28223aac1eadSToke Høiland-Jørgensen 
2823a3b80e10SAndrii Nakryiko 	return bpf_link_settle(&link_primer);
28243aac1eadSToke Høiland-Jørgensen out_unlock:
28254a1e7c0cSToke Høiland-Jørgensen 	if (tr && tr != prog->aux->dst_trampoline)
28264a1e7c0cSToke Høiland-Jørgensen 		bpf_trampoline_put(tr);
28273aac1eadSToke Høiland-Jørgensen 	mutex_unlock(&prog->aux->dst_mutex);
28283aac1eadSToke Høiland-Jørgensen 	kfree(link);
2829fec56f58SAlexei Starovoitov out_put_prog:
28304a1e7c0cSToke Høiland-Jørgensen 	if (tgt_prog_fd && tgt_prog)
28314a1e7c0cSToke Høiland-Jørgensen 		bpf_prog_put(tgt_prog);
2832fec56f58SAlexei Starovoitov 	return err;
2833fec56f58SAlexei Starovoitov }
2834fec56f58SAlexei Starovoitov 
283570ed506cSAndrii Nakryiko struct bpf_raw_tp_link {
283670ed506cSAndrii Nakryiko 	struct bpf_link link;
2837c4f6699dSAlexei Starovoitov 	struct bpf_raw_event_map *btp;
2838c4f6699dSAlexei Starovoitov };
2839c4f6699dSAlexei Starovoitov 
284070ed506cSAndrii Nakryiko static void bpf_raw_tp_link_release(struct bpf_link *link)
2841c4f6699dSAlexei Starovoitov {
284270ed506cSAndrii Nakryiko 	struct bpf_raw_tp_link *raw_tp =
284370ed506cSAndrii Nakryiko 		container_of(link, struct bpf_raw_tp_link, link);
2844c4f6699dSAlexei Starovoitov 
284570ed506cSAndrii Nakryiko 	bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
2846a38d1107SMatt Mullins 	bpf_put_raw_tracepoint(raw_tp->btp);
2847babf3164SAndrii Nakryiko }
2848babf3164SAndrii Nakryiko 
2849babf3164SAndrii Nakryiko static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2850babf3164SAndrii Nakryiko {
2851babf3164SAndrii Nakryiko 	struct bpf_raw_tp_link *raw_tp =
2852babf3164SAndrii Nakryiko 		container_of(link, struct bpf_raw_tp_link, link);
2853babf3164SAndrii Nakryiko 
2854c4f6699dSAlexei Starovoitov 	kfree(raw_tp);
2855c4f6699dSAlexei Starovoitov }
2856c4f6699dSAlexei Starovoitov 
2857f2e10bffSAndrii Nakryiko static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2858f2e10bffSAndrii Nakryiko 					struct seq_file *seq)
2859f2e10bffSAndrii Nakryiko {
2860f2e10bffSAndrii Nakryiko 	struct bpf_raw_tp_link *raw_tp_link =
2861f2e10bffSAndrii Nakryiko 		container_of(link, struct bpf_raw_tp_link, link);
2862f2e10bffSAndrii Nakryiko 
2863f2e10bffSAndrii Nakryiko 	seq_printf(seq,
2864f2e10bffSAndrii Nakryiko 		   "tp_name:\t%s\n",
2865f2e10bffSAndrii Nakryiko 		   raw_tp_link->btp->tp->name);
2866f2e10bffSAndrii Nakryiko }
2867f2e10bffSAndrii Nakryiko 
2868f2e10bffSAndrii Nakryiko static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2869f2e10bffSAndrii Nakryiko 					  struct bpf_link_info *info)
2870f2e10bffSAndrii Nakryiko {
2871f2e10bffSAndrii Nakryiko 	struct bpf_raw_tp_link *raw_tp_link =
2872f2e10bffSAndrii Nakryiko 		container_of(link, struct bpf_raw_tp_link, link);
2873f2e10bffSAndrii Nakryiko 	char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2874f2e10bffSAndrii Nakryiko 	const char *tp_name = raw_tp_link->btp->tp->name;
2875f2e10bffSAndrii Nakryiko 	u32 ulen = info->raw_tracepoint.tp_name_len;
2876f2e10bffSAndrii Nakryiko 	size_t tp_len = strlen(tp_name);
2877f2e10bffSAndrii Nakryiko 
2878b474959dSYonghong Song 	if (!ulen ^ !ubuf)
2879f2e10bffSAndrii Nakryiko 		return -EINVAL;
2880f2e10bffSAndrii Nakryiko 
2881f2e10bffSAndrii Nakryiko 	info->raw_tracepoint.tp_name_len = tp_len + 1;
2882f2e10bffSAndrii Nakryiko 
2883f2e10bffSAndrii Nakryiko 	if (!ubuf)
2884f2e10bffSAndrii Nakryiko 		return 0;
2885f2e10bffSAndrii Nakryiko 
2886f2e10bffSAndrii Nakryiko 	if (ulen >= tp_len + 1) {
2887f2e10bffSAndrii Nakryiko 		if (copy_to_user(ubuf, tp_name, tp_len + 1))
2888f2e10bffSAndrii Nakryiko 			return -EFAULT;
2889f2e10bffSAndrii Nakryiko 	} else {
2890f2e10bffSAndrii Nakryiko 		char zero = '\0';
2891f2e10bffSAndrii Nakryiko 
2892f2e10bffSAndrii Nakryiko 		if (copy_to_user(ubuf, tp_name, ulen - 1))
2893f2e10bffSAndrii Nakryiko 			return -EFAULT;
2894f2e10bffSAndrii Nakryiko 		if (put_user(zero, ubuf + ulen - 1))
2895f2e10bffSAndrii Nakryiko 			return -EFAULT;
2896f2e10bffSAndrii Nakryiko 		return -ENOSPC;
2897f2e10bffSAndrii Nakryiko 	}
2898f2e10bffSAndrii Nakryiko 
2899f2e10bffSAndrii Nakryiko 	return 0;
2900f2e10bffSAndrii Nakryiko }
2901f2e10bffSAndrii Nakryiko 
2902a3b80e10SAndrii Nakryiko static const struct bpf_link_ops bpf_raw_tp_link_lops = {
290370ed506cSAndrii Nakryiko 	.release = bpf_raw_tp_link_release,
2904babf3164SAndrii Nakryiko 	.dealloc = bpf_raw_tp_link_dealloc,
2905f2e10bffSAndrii Nakryiko 	.show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2906f2e10bffSAndrii Nakryiko 	.fill_link_info = bpf_raw_tp_link_fill_link_info,
2907c4f6699dSAlexei Starovoitov };
2908c4f6699dSAlexei Starovoitov 
2909b89fbfbbSAndrii Nakryiko #ifdef CONFIG_PERF_EVENTS
2910b89fbfbbSAndrii Nakryiko struct bpf_perf_link {
2911b89fbfbbSAndrii Nakryiko 	struct bpf_link link;
2912b89fbfbbSAndrii Nakryiko 	struct file *perf_file;
2913b89fbfbbSAndrii Nakryiko };
2914b89fbfbbSAndrii Nakryiko 
2915b89fbfbbSAndrii Nakryiko static void bpf_perf_link_release(struct bpf_link *link)
2916b89fbfbbSAndrii Nakryiko {
2917b89fbfbbSAndrii Nakryiko 	struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
2918b89fbfbbSAndrii Nakryiko 	struct perf_event *event = perf_link->perf_file->private_data;
2919b89fbfbbSAndrii Nakryiko 
2920b89fbfbbSAndrii Nakryiko 	perf_event_free_bpf_prog(event);
2921b89fbfbbSAndrii Nakryiko 	fput(perf_link->perf_file);
2922b89fbfbbSAndrii Nakryiko }
2923b89fbfbbSAndrii Nakryiko 
2924b89fbfbbSAndrii Nakryiko static void bpf_perf_link_dealloc(struct bpf_link *link)
2925b89fbfbbSAndrii Nakryiko {
2926b89fbfbbSAndrii Nakryiko 	struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
2927b89fbfbbSAndrii Nakryiko 
2928b89fbfbbSAndrii Nakryiko 	kfree(perf_link);
2929b89fbfbbSAndrii Nakryiko }
2930b89fbfbbSAndrii Nakryiko 
2931b89fbfbbSAndrii Nakryiko static const struct bpf_link_ops bpf_perf_link_lops = {
2932b89fbfbbSAndrii Nakryiko 	.release = bpf_perf_link_release,
2933b89fbfbbSAndrii Nakryiko 	.dealloc = bpf_perf_link_dealloc,
2934b89fbfbbSAndrii Nakryiko };
2935b89fbfbbSAndrii Nakryiko 
2936b89fbfbbSAndrii Nakryiko static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2937b89fbfbbSAndrii Nakryiko {
2938b89fbfbbSAndrii Nakryiko 	struct bpf_link_primer link_primer;
2939b89fbfbbSAndrii Nakryiko 	struct bpf_perf_link *link;
2940b89fbfbbSAndrii Nakryiko 	struct perf_event *event;
2941b89fbfbbSAndrii Nakryiko 	struct file *perf_file;
2942b89fbfbbSAndrii Nakryiko 	int err;
2943b89fbfbbSAndrii Nakryiko 
2944b89fbfbbSAndrii Nakryiko 	if (attr->link_create.flags)
2945b89fbfbbSAndrii Nakryiko 		return -EINVAL;
2946b89fbfbbSAndrii Nakryiko 
2947b89fbfbbSAndrii Nakryiko 	perf_file = perf_event_get(attr->link_create.target_fd);
2948b89fbfbbSAndrii Nakryiko 	if (IS_ERR(perf_file))
2949b89fbfbbSAndrii Nakryiko 		return PTR_ERR(perf_file);
2950b89fbfbbSAndrii Nakryiko 
2951b89fbfbbSAndrii Nakryiko 	link = kzalloc(sizeof(*link), GFP_USER);
2952b89fbfbbSAndrii Nakryiko 	if (!link) {
2953b89fbfbbSAndrii Nakryiko 		err = -ENOMEM;
2954b89fbfbbSAndrii Nakryiko 		goto out_put_file;
2955b89fbfbbSAndrii Nakryiko 	}
2956b89fbfbbSAndrii Nakryiko 	bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog);
2957b89fbfbbSAndrii Nakryiko 	link->perf_file = perf_file;
2958b89fbfbbSAndrii Nakryiko 
2959b89fbfbbSAndrii Nakryiko 	err = bpf_link_prime(&link->link, &link_primer);
2960b89fbfbbSAndrii Nakryiko 	if (err) {
2961b89fbfbbSAndrii Nakryiko 		kfree(link);
2962b89fbfbbSAndrii Nakryiko 		goto out_put_file;
2963b89fbfbbSAndrii Nakryiko 	}
2964b89fbfbbSAndrii Nakryiko 
2965b89fbfbbSAndrii Nakryiko 	event = perf_file->private_data;
2966*82e6b1eeSAndrii Nakryiko 	err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
2967b89fbfbbSAndrii Nakryiko 	if (err) {
2968b89fbfbbSAndrii Nakryiko 		bpf_link_cleanup(&link_primer);
2969b89fbfbbSAndrii Nakryiko 		goto out_put_file;
2970b89fbfbbSAndrii Nakryiko 	}
2971b89fbfbbSAndrii Nakryiko 	/* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
2972b89fbfbbSAndrii Nakryiko 	bpf_prog_inc(prog);
2973b89fbfbbSAndrii Nakryiko 
2974b89fbfbbSAndrii Nakryiko 	return bpf_link_settle(&link_primer);
2975b89fbfbbSAndrii Nakryiko 
2976b89fbfbbSAndrii Nakryiko out_put_file:
2977b89fbfbbSAndrii Nakryiko 	fput(perf_file);
2978b89fbfbbSAndrii Nakryiko 	return err;
2979b89fbfbbSAndrii Nakryiko }
2980b89fbfbbSAndrii Nakryiko #endif /* CONFIG_PERF_EVENTS */
2981b89fbfbbSAndrii Nakryiko 
2982c4f6699dSAlexei Starovoitov #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2983c4f6699dSAlexei Starovoitov 
2984c4f6699dSAlexei Starovoitov static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2985c4f6699dSAlexei Starovoitov {
2986a3b80e10SAndrii Nakryiko 	struct bpf_link_primer link_primer;
2987babf3164SAndrii Nakryiko 	struct bpf_raw_tp_link *link;
2988c4f6699dSAlexei Starovoitov 	struct bpf_raw_event_map *btp;
2989c4f6699dSAlexei Starovoitov 	struct bpf_prog *prog;
2990ac4414b5SAlexei Starovoitov 	const char *tp_name;
2991ac4414b5SAlexei Starovoitov 	char buf[128];
2992a3b80e10SAndrii Nakryiko 	int err;
2993c4f6699dSAlexei Starovoitov 
2994ac4414b5SAlexei Starovoitov 	if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2995ac4414b5SAlexei Starovoitov 		return -EINVAL;
2996ac4414b5SAlexei Starovoitov 
2997ac4414b5SAlexei Starovoitov 	prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2998ac4414b5SAlexei Starovoitov 	if (IS_ERR(prog))
2999ac4414b5SAlexei Starovoitov 		return PTR_ERR(prog);
3000ac4414b5SAlexei Starovoitov 
30019e4e01dfSKP Singh 	switch (prog->type) {
30029e4e01dfSKP Singh 	case BPF_PROG_TYPE_TRACING:
30039e4e01dfSKP Singh 	case BPF_PROG_TYPE_EXT:
30049e4e01dfSKP Singh 	case BPF_PROG_TYPE_LSM:
3005ac4414b5SAlexei Starovoitov 		if (attr->raw_tracepoint.name) {
3006fec56f58SAlexei Starovoitov 			/* The attach point for this category of programs
3007fec56f58SAlexei Starovoitov 			 * should be specified via btf_id during program load.
3008ac4414b5SAlexei Starovoitov 			 */
3009ac4414b5SAlexei Starovoitov 			err = -EINVAL;
3010ac4414b5SAlexei Starovoitov 			goto out_put_prog;
3011ac4414b5SAlexei Starovoitov 		}
30129e4e01dfSKP Singh 		if (prog->type == BPF_PROG_TYPE_TRACING &&
30139e4e01dfSKP Singh 		    prog->expected_attach_type == BPF_TRACE_RAW_TP) {
301438207291SMartin KaFai Lau 			tp_name = prog->aux->attach_func_name;
30159e4e01dfSKP Singh 			break;
30169e4e01dfSKP Singh 		}
30175541075aSJiri Olsa 		err = bpf_tracing_prog_attach(prog, 0, 0);
30185541075aSJiri Olsa 		if (err >= 0)
30195541075aSJiri Olsa 			return err;
30205541075aSJiri Olsa 		goto out_put_prog;
30219e4e01dfSKP Singh 	case BPF_PROG_TYPE_RAW_TRACEPOINT:
30229e4e01dfSKP Singh 	case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
3023ac4414b5SAlexei Starovoitov 		if (strncpy_from_user(buf,
3024ac4414b5SAlexei Starovoitov 				      u64_to_user_ptr(attr->raw_tracepoint.name),
3025ac4414b5SAlexei Starovoitov 				      sizeof(buf) - 1) < 0) {
3026ac4414b5SAlexei Starovoitov 			err = -EFAULT;
3027ac4414b5SAlexei Starovoitov 			goto out_put_prog;
3028ac4414b5SAlexei Starovoitov 		}
3029ac4414b5SAlexei Starovoitov 		buf[sizeof(buf) - 1] = 0;
3030ac4414b5SAlexei Starovoitov 		tp_name = buf;
30319e4e01dfSKP Singh 		break;
30329e4e01dfSKP Singh 	default:
30339e4e01dfSKP Singh 		err = -EINVAL;
30349e4e01dfSKP Singh 		goto out_put_prog;
3035ac4414b5SAlexei Starovoitov 	}
3036c4f6699dSAlexei Starovoitov 
3037a38d1107SMatt Mullins 	btp = bpf_get_raw_tracepoint(tp_name);
3038ac4414b5SAlexei Starovoitov 	if (!btp) {
3039ac4414b5SAlexei Starovoitov 		err = -ENOENT;
3040ac4414b5SAlexei Starovoitov 		goto out_put_prog;
3041ac4414b5SAlexei Starovoitov 	}
3042c4f6699dSAlexei Starovoitov 
3043babf3164SAndrii Nakryiko 	link = kzalloc(sizeof(*link), GFP_USER);
3044babf3164SAndrii Nakryiko 	if (!link) {
3045a38d1107SMatt Mullins 		err = -ENOMEM;
3046a38d1107SMatt Mullins 		goto out_put_btp;
3047a38d1107SMatt Mullins 	}
3048f2e10bffSAndrii Nakryiko 	bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
3049f2e10bffSAndrii Nakryiko 		      &bpf_raw_tp_link_lops, prog);
3050babf3164SAndrii Nakryiko 	link->btp = btp;
3051c4f6699dSAlexei Starovoitov 
3052a3b80e10SAndrii Nakryiko 	err = bpf_link_prime(&link->link, &link_primer);
3053a3b80e10SAndrii Nakryiko 	if (err) {
3054babf3164SAndrii Nakryiko 		kfree(link);
3055babf3164SAndrii Nakryiko 		goto out_put_btp;
3056c4f6699dSAlexei Starovoitov 	}
3057babf3164SAndrii Nakryiko 
3058babf3164SAndrii Nakryiko 	err = bpf_probe_register(link->btp, prog);
3059babf3164SAndrii Nakryiko 	if (err) {
3060a3b80e10SAndrii Nakryiko 		bpf_link_cleanup(&link_primer);
3061babf3164SAndrii Nakryiko 		goto out_put_btp;
3062babf3164SAndrii Nakryiko 	}
3063babf3164SAndrii Nakryiko 
3064a3b80e10SAndrii Nakryiko 	return bpf_link_settle(&link_primer);
3065c4f6699dSAlexei Starovoitov 
3066a38d1107SMatt Mullins out_put_btp:
3067a38d1107SMatt Mullins 	bpf_put_raw_tracepoint(btp);
3068ac4414b5SAlexei Starovoitov out_put_prog:
3069ac4414b5SAlexei Starovoitov 	bpf_prog_put(prog);
3070c4f6699dSAlexei Starovoitov 	return err;
3071c4f6699dSAlexei Starovoitov }
3072c4f6699dSAlexei Starovoitov 
307333491588SAnders Roxell static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
307433491588SAnders Roxell 					     enum bpf_attach_type attach_type)
307533491588SAnders Roxell {
307633491588SAnders Roxell 	switch (prog->type) {
307733491588SAnders Roxell 	case BPF_PROG_TYPE_CGROUP_SOCK:
307833491588SAnders Roxell 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
30790d01da6aSStanislav Fomichev 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3080e9ddbb77SJakub Sitnicki 	case BPF_PROG_TYPE_SK_LOOKUP:
308133491588SAnders Roxell 		return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
30825cf1e914Sbrakmo 	case BPF_PROG_TYPE_CGROUP_SKB:
30832c78ee89SAlexei Starovoitov 		if (!capable(CAP_NET_ADMIN))
30842c78ee89SAlexei Starovoitov 			/* cg-skb progs can be loaded by unpriv user.
30852c78ee89SAlexei Starovoitov 			 * check permissions at attach time.
30862c78ee89SAlexei Starovoitov 			 */
30872c78ee89SAlexei Starovoitov 			return -EPERM;
30885cf1e914Sbrakmo 		return prog->enforce_expected_attach_type &&
30895cf1e914Sbrakmo 			prog->expected_attach_type != attach_type ?
30905cf1e914Sbrakmo 			-EINVAL : 0;
309133491588SAnders Roxell 	default:
309233491588SAnders Roxell 		return 0;
309333491588SAnders Roxell 	}
309433491588SAnders Roxell }
309533491588SAnders Roxell 
3096e28784e3SAndrii Nakryiko static enum bpf_prog_type
3097e28784e3SAndrii Nakryiko attach_type_to_prog_type(enum bpf_attach_type attach_type)
3098e28784e3SAndrii Nakryiko {
3099e28784e3SAndrii Nakryiko 	switch (attach_type) {
3100e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET_INGRESS:
3101e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET_EGRESS:
3102e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_CGROUP_SKB;
3103e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET_SOCK_CREATE:
3104f5836749SStanislav Fomichev 	case BPF_CGROUP_INET_SOCK_RELEASE:
3105e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET4_POST_BIND:
3106e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET6_POST_BIND:
3107e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_CGROUP_SOCK;
3108e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET4_BIND:
3109e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET6_BIND:
3110e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET4_CONNECT:
3111e28784e3SAndrii Nakryiko 	case BPF_CGROUP_INET6_CONNECT:
31121b66d253SDaniel Borkmann 	case BPF_CGROUP_INET4_GETPEERNAME:
31131b66d253SDaniel Borkmann 	case BPF_CGROUP_INET6_GETPEERNAME:
31141b66d253SDaniel Borkmann 	case BPF_CGROUP_INET4_GETSOCKNAME:
31151b66d253SDaniel Borkmann 	case BPF_CGROUP_INET6_GETSOCKNAME:
3116e28784e3SAndrii Nakryiko 	case BPF_CGROUP_UDP4_SENDMSG:
3117e28784e3SAndrii Nakryiko 	case BPF_CGROUP_UDP6_SENDMSG:
3118e28784e3SAndrii Nakryiko 	case BPF_CGROUP_UDP4_RECVMSG:
3119e28784e3SAndrii Nakryiko 	case BPF_CGROUP_UDP6_RECVMSG:
3120e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3121e28784e3SAndrii Nakryiko 	case BPF_CGROUP_SOCK_OPS:
3122e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_SOCK_OPS;
3123e28784e3SAndrii Nakryiko 	case BPF_CGROUP_DEVICE:
3124e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_CGROUP_DEVICE;
3125e28784e3SAndrii Nakryiko 	case BPF_SK_MSG_VERDICT:
3126e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_SK_MSG;
3127e28784e3SAndrii Nakryiko 	case BPF_SK_SKB_STREAM_PARSER:
3128e28784e3SAndrii Nakryiko 	case BPF_SK_SKB_STREAM_VERDICT:
3129a7ba4558SCong Wang 	case BPF_SK_SKB_VERDICT:
3130e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_SK_SKB;
3131e28784e3SAndrii Nakryiko 	case BPF_LIRC_MODE2:
3132e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_LIRC_MODE2;
3133e28784e3SAndrii Nakryiko 	case BPF_FLOW_DISSECTOR:
3134e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_FLOW_DISSECTOR;
3135e28784e3SAndrii Nakryiko 	case BPF_CGROUP_SYSCTL:
3136e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_CGROUP_SYSCTL;
3137e28784e3SAndrii Nakryiko 	case BPF_CGROUP_GETSOCKOPT:
3138e28784e3SAndrii Nakryiko 	case BPF_CGROUP_SETSOCKOPT:
3139e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_CGROUP_SOCKOPT;
3140de4e05caSYonghong Song 	case BPF_TRACE_ITER:
3141de4e05caSYonghong Song 		return BPF_PROG_TYPE_TRACING;
3142e9ddbb77SJakub Sitnicki 	case BPF_SK_LOOKUP:
3143e9ddbb77SJakub Sitnicki 		return BPF_PROG_TYPE_SK_LOOKUP;
3144aa8d3a71SAndrii Nakryiko 	case BPF_XDP:
3145aa8d3a71SAndrii Nakryiko 		return BPF_PROG_TYPE_XDP;
3146e28784e3SAndrii Nakryiko 	default:
3147e28784e3SAndrii Nakryiko 		return BPF_PROG_TYPE_UNSPEC;
3148e28784e3SAndrii Nakryiko 	}
3149e28784e3SAndrii Nakryiko }
3150e28784e3SAndrii Nakryiko 
31517dd68b32SAndrey Ignatov #define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
3152174a79ffSJohn Fastabend 
3153324bda9eSAlexei Starovoitov #define BPF_F_ATTACH_MASK \
31547dd68b32SAndrey Ignatov 	(BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
3155324bda9eSAlexei Starovoitov 
3156f4324551SDaniel Mack static int bpf_prog_attach(const union bpf_attr *attr)
3157f4324551SDaniel Mack {
31587f677633SAlexei Starovoitov 	enum bpf_prog_type ptype;
3159f4324551SDaniel Mack 	struct bpf_prog *prog;
31607f677633SAlexei Starovoitov 	int ret;
3161f4324551SDaniel Mack 
3162f4324551SDaniel Mack 	if (CHECK_ATTR(BPF_PROG_ATTACH))
3163f4324551SDaniel Mack 		return -EINVAL;
3164f4324551SDaniel Mack 
3165324bda9eSAlexei Starovoitov 	if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
31667f677633SAlexei Starovoitov 		return -EINVAL;
31677f677633SAlexei Starovoitov 
3168e28784e3SAndrii Nakryiko 	ptype = attach_type_to_prog_type(attr->attach_type);
3169e28784e3SAndrii Nakryiko 	if (ptype == BPF_PROG_TYPE_UNSPEC)
3170b2cd1257SDavid Ahern 		return -EINVAL;
3171b2cd1257SDavid Ahern 
3172b2cd1257SDavid Ahern 	prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3173f4324551SDaniel Mack 	if (IS_ERR(prog))
3174f4324551SDaniel Mack 		return PTR_ERR(prog);
3175f4324551SDaniel Mack 
31765e43f899SAndrey Ignatov 	if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
31775e43f899SAndrey Ignatov 		bpf_prog_put(prog);
31785e43f899SAndrey Ignatov 		return -EINVAL;
31795e43f899SAndrey Ignatov 	}
31805e43f899SAndrey Ignatov 
3181fdb5c453SSean Young 	switch (ptype) {
3182fdb5c453SSean Young 	case BPF_PROG_TYPE_SK_SKB:
3183fdb5c453SSean Young 	case BPF_PROG_TYPE_SK_MSG:
3184604326b4SDaniel Borkmann 		ret = sock_map_get_from_fd(attr, prog);
3185fdb5c453SSean Young 		break;
3186fdb5c453SSean Young 	case BPF_PROG_TYPE_LIRC_MODE2:
3187fdb5c453SSean Young 		ret = lirc_prog_attach(attr, prog);
3188fdb5c453SSean Young 		break;
3189d58e468bSPetar Penkov 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
3190a3fd7ceeSJakub Sitnicki 		ret = netns_bpf_prog_attach(attr, prog);
3191d58e468bSPetar Penkov 		break;
3192e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_DEVICE:
3193e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SKB:
3194e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCK:
3195e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3196e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3197e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
3198e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_SOCK_OPS:
3199fdb5c453SSean Young 		ret = cgroup_bpf_prog_attach(attr, ptype, prog);
3200e28784e3SAndrii Nakryiko 		break;
3201e28784e3SAndrii Nakryiko 	default:
3202e28784e3SAndrii Nakryiko 		ret = -EINVAL;
3203f4324551SDaniel Mack 	}
3204f4324551SDaniel Mack 
32057f677633SAlexei Starovoitov 	if (ret)
32067f677633SAlexei Starovoitov 		bpf_prog_put(prog);
32077f677633SAlexei Starovoitov 	return ret;
3208f4324551SDaniel Mack }
3209f4324551SDaniel Mack 
3210f4324551SDaniel Mack #define BPF_PROG_DETACH_LAST_FIELD attach_type
3211f4324551SDaniel Mack 
3212f4324551SDaniel Mack static int bpf_prog_detach(const union bpf_attr *attr)
3213f4324551SDaniel Mack {
3214324bda9eSAlexei Starovoitov 	enum bpf_prog_type ptype;
3215f4324551SDaniel Mack 
3216f4324551SDaniel Mack 	if (CHECK_ATTR(BPF_PROG_DETACH))
3217f4324551SDaniel Mack 		return -EINVAL;
3218f4324551SDaniel Mack 
3219e28784e3SAndrii Nakryiko 	ptype = attach_type_to_prog_type(attr->attach_type);
3220e28784e3SAndrii Nakryiko 
3221e28784e3SAndrii Nakryiko 	switch (ptype) {
3222e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_SK_MSG:
3223e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_SK_SKB:
3224bb0de313SLorenz Bauer 		return sock_map_prog_detach(attr, ptype);
3225e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_LIRC_MODE2:
3226f4364dcfSSean Young 		return lirc_prog_detach(attr);
3227e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
32284ac2add6SLorenz Bauer 		return netns_bpf_prog_detach(attr, ptype);
3229e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_DEVICE:
3230e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SKB:
3231e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCK:
3232e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3233e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3234e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
3235e28784e3SAndrii Nakryiko 	case BPF_PROG_TYPE_SOCK_OPS:
3236e28784e3SAndrii Nakryiko 		return cgroup_bpf_prog_detach(attr, ptype);
3237f4324551SDaniel Mack 	default:
3238f4324551SDaniel Mack 		return -EINVAL;
3239f4324551SDaniel Mack 	}
3240f4324551SDaniel Mack }
324140304b2aSLawrence Brakmo 
3242468e2f64SAlexei Starovoitov #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3243468e2f64SAlexei Starovoitov 
3244468e2f64SAlexei Starovoitov static int bpf_prog_query(const union bpf_attr *attr,
3245468e2f64SAlexei Starovoitov 			  union bpf_attr __user *uattr)
3246468e2f64SAlexei Starovoitov {
3247468e2f64SAlexei Starovoitov 	if (!capable(CAP_NET_ADMIN))
3248468e2f64SAlexei Starovoitov 		return -EPERM;
3249468e2f64SAlexei Starovoitov 	if (CHECK_ATTR(BPF_PROG_QUERY))
3250468e2f64SAlexei Starovoitov 		return -EINVAL;
3251468e2f64SAlexei Starovoitov 	if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3252468e2f64SAlexei Starovoitov 		return -EINVAL;
3253468e2f64SAlexei Starovoitov 
3254468e2f64SAlexei Starovoitov 	switch (attr->query.attach_type) {
3255468e2f64SAlexei Starovoitov 	case BPF_CGROUP_INET_INGRESS:
3256468e2f64SAlexei Starovoitov 	case BPF_CGROUP_INET_EGRESS:
3257468e2f64SAlexei Starovoitov 	case BPF_CGROUP_INET_SOCK_CREATE:
3258f5836749SStanislav Fomichev 	case BPF_CGROUP_INET_SOCK_RELEASE:
32594fbac77dSAndrey Ignatov 	case BPF_CGROUP_INET4_BIND:
32604fbac77dSAndrey Ignatov 	case BPF_CGROUP_INET6_BIND:
3261aac3fc32SAndrey Ignatov 	case BPF_CGROUP_INET4_POST_BIND:
3262aac3fc32SAndrey Ignatov 	case BPF_CGROUP_INET6_POST_BIND:
3263d74bad4eSAndrey Ignatov 	case BPF_CGROUP_INET4_CONNECT:
3264d74bad4eSAndrey Ignatov 	case BPF_CGROUP_INET6_CONNECT:
32651b66d253SDaniel Borkmann 	case BPF_CGROUP_INET4_GETPEERNAME:
32661b66d253SDaniel Borkmann 	case BPF_CGROUP_INET6_GETPEERNAME:
32671b66d253SDaniel Borkmann 	case BPF_CGROUP_INET4_GETSOCKNAME:
32681b66d253SDaniel Borkmann 	case BPF_CGROUP_INET6_GETSOCKNAME:
32691cedee13SAndrey Ignatov 	case BPF_CGROUP_UDP4_SENDMSG:
32701cedee13SAndrey Ignatov 	case BPF_CGROUP_UDP6_SENDMSG:
3271983695faSDaniel Borkmann 	case BPF_CGROUP_UDP4_RECVMSG:
3272983695faSDaniel Borkmann 	case BPF_CGROUP_UDP6_RECVMSG:
3273468e2f64SAlexei Starovoitov 	case BPF_CGROUP_SOCK_OPS:
3274ebc614f6SRoman Gushchin 	case BPF_CGROUP_DEVICE:
32757b146cebSAndrey Ignatov 	case BPF_CGROUP_SYSCTL:
32760d01da6aSStanislav Fomichev 	case BPF_CGROUP_GETSOCKOPT:
32770d01da6aSStanislav Fomichev 	case BPF_CGROUP_SETSOCKOPT:
3278e28784e3SAndrii Nakryiko 		return cgroup_bpf_prog_query(attr, uattr);
3279f4364dcfSSean Young 	case BPF_LIRC_MODE2:
3280f4364dcfSSean Young 		return lirc_prog_query(attr, uattr);
3281118c8e9aSStanislav Fomichev 	case BPF_FLOW_DISSECTOR:
3282e9ddbb77SJakub Sitnicki 	case BPF_SK_LOOKUP:
3283a3fd7ceeSJakub Sitnicki 		return netns_bpf_prog_query(attr, uattr);
3284468e2f64SAlexei Starovoitov 	default:
3285468e2f64SAlexei Starovoitov 		return -EINVAL;
3286468e2f64SAlexei Starovoitov 	}
3287468e2f64SAlexei Starovoitov }
3288f4324551SDaniel Mack 
32891b4d60ecSSong Liu #define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
32901cf1cae9SAlexei Starovoitov 
32911cf1cae9SAlexei Starovoitov static int bpf_prog_test_run(const union bpf_attr *attr,
32921cf1cae9SAlexei Starovoitov 			     union bpf_attr __user *uattr)
32931cf1cae9SAlexei Starovoitov {
32941cf1cae9SAlexei Starovoitov 	struct bpf_prog *prog;
32951cf1cae9SAlexei Starovoitov 	int ret = -ENOTSUPP;
32961cf1cae9SAlexei Starovoitov 
32971cf1cae9SAlexei Starovoitov 	if (CHECK_ATTR(BPF_PROG_TEST_RUN))
32981cf1cae9SAlexei Starovoitov 		return -EINVAL;
32991cf1cae9SAlexei Starovoitov 
3300b0b9395dSStanislav Fomichev 	if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3301b0b9395dSStanislav Fomichev 	    (!attr->test.ctx_size_in && attr->test.ctx_in))
3302b0b9395dSStanislav Fomichev 		return -EINVAL;
3303b0b9395dSStanislav Fomichev 
3304b0b9395dSStanislav Fomichev 	if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3305b0b9395dSStanislav Fomichev 	    (!attr->test.ctx_size_out && attr->test.ctx_out))
3306b0b9395dSStanislav Fomichev 		return -EINVAL;
3307b0b9395dSStanislav Fomichev 
33081cf1cae9SAlexei Starovoitov 	prog = bpf_prog_get(attr->test.prog_fd);
33091cf1cae9SAlexei Starovoitov 	if (IS_ERR(prog))
33101cf1cae9SAlexei Starovoitov 		return PTR_ERR(prog);
33111cf1cae9SAlexei Starovoitov 
33121cf1cae9SAlexei Starovoitov 	if (prog->aux->ops->test_run)
33131cf1cae9SAlexei Starovoitov 		ret = prog->aux->ops->test_run(prog, attr, uattr);
33141cf1cae9SAlexei Starovoitov 
33151cf1cae9SAlexei Starovoitov 	bpf_prog_put(prog);
33161cf1cae9SAlexei Starovoitov 	return ret;
33171cf1cae9SAlexei Starovoitov }
33181cf1cae9SAlexei Starovoitov 
331934ad5580SMartin KaFai Lau #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
332034ad5580SMartin KaFai Lau 
332134ad5580SMartin KaFai Lau static int bpf_obj_get_next_id(const union bpf_attr *attr,
332234ad5580SMartin KaFai Lau 			       union bpf_attr __user *uattr,
332334ad5580SMartin KaFai Lau 			       struct idr *idr,
332434ad5580SMartin KaFai Lau 			       spinlock_t *lock)
332534ad5580SMartin KaFai Lau {
332634ad5580SMartin KaFai Lau 	u32 next_id = attr->start_id;
332734ad5580SMartin KaFai Lau 	int err = 0;
332834ad5580SMartin KaFai Lau 
332934ad5580SMartin KaFai Lau 	if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
333034ad5580SMartin KaFai Lau 		return -EINVAL;
333134ad5580SMartin KaFai Lau 
333234ad5580SMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN))
333334ad5580SMartin KaFai Lau 		return -EPERM;
333434ad5580SMartin KaFai Lau 
333534ad5580SMartin KaFai Lau 	next_id++;
333634ad5580SMartin KaFai Lau 	spin_lock_bh(lock);
333734ad5580SMartin KaFai Lau 	if (!idr_get_next(idr, &next_id))
333834ad5580SMartin KaFai Lau 		err = -ENOENT;
333934ad5580SMartin KaFai Lau 	spin_unlock_bh(lock);
334034ad5580SMartin KaFai Lau 
334134ad5580SMartin KaFai Lau 	if (!err)
334234ad5580SMartin KaFai Lau 		err = put_user(next_id, &uattr->next_id);
334334ad5580SMartin KaFai Lau 
334434ad5580SMartin KaFai Lau 	return err;
334534ad5580SMartin KaFai Lau }
334634ad5580SMartin KaFai Lau 
33476086d29dSYonghong Song struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
33486086d29dSYonghong Song {
33496086d29dSYonghong Song 	struct bpf_map *map;
33506086d29dSYonghong Song 
33516086d29dSYonghong Song 	spin_lock_bh(&map_idr_lock);
33526086d29dSYonghong Song again:
33536086d29dSYonghong Song 	map = idr_get_next(&map_idr, id);
33546086d29dSYonghong Song 	if (map) {
33556086d29dSYonghong Song 		map = __bpf_map_inc_not_zero(map, false);
33566086d29dSYonghong Song 		if (IS_ERR(map)) {
33576086d29dSYonghong Song 			(*id)++;
33586086d29dSYonghong Song 			goto again;
33596086d29dSYonghong Song 		}
33606086d29dSYonghong Song 	}
33616086d29dSYonghong Song 	spin_unlock_bh(&map_idr_lock);
33626086d29dSYonghong Song 
33636086d29dSYonghong Song 	return map;
33646086d29dSYonghong Song }
33656086d29dSYonghong Song 
3366a228a64fSAlexei Starovoitov struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3367a228a64fSAlexei Starovoitov {
3368a228a64fSAlexei Starovoitov 	struct bpf_prog *prog;
3369a228a64fSAlexei Starovoitov 
3370a228a64fSAlexei Starovoitov 	spin_lock_bh(&prog_idr_lock);
3371a228a64fSAlexei Starovoitov again:
3372a228a64fSAlexei Starovoitov 	prog = idr_get_next(&prog_idr, id);
3373a228a64fSAlexei Starovoitov 	if (prog) {
3374a228a64fSAlexei Starovoitov 		prog = bpf_prog_inc_not_zero(prog);
3375a228a64fSAlexei Starovoitov 		if (IS_ERR(prog)) {
3376a228a64fSAlexei Starovoitov 			(*id)++;
3377a228a64fSAlexei Starovoitov 			goto again;
3378a228a64fSAlexei Starovoitov 		}
3379a228a64fSAlexei Starovoitov 	}
3380a228a64fSAlexei Starovoitov 	spin_unlock_bh(&prog_idr_lock);
3381a228a64fSAlexei Starovoitov 
3382a228a64fSAlexei Starovoitov 	return prog;
3383a228a64fSAlexei Starovoitov }
3384a228a64fSAlexei Starovoitov 
3385b16d9aa4SMartin KaFai Lau #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3386b16d9aa4SMartin KaFai Lau 
33877e6897f9SBjörn Töpel struct bpf_prog *bpf_prog_by_id(u32 id)
33887e6897f9SBjörn Töpel {
33897e6897f9SBjörn Töpel 	struct bpf_prog *prog;
33907e6897f9SBjörn Töpel 
33917e6897f9SBjörn Töpel 	if (!id)
33927e6897f9SBjörn Töpel 		return ERR_PTR(-ENOENT);
33937e6897f9SBjörn Töpel 
33947e6897f9SBjörn Töpel 	spin_lock_bh(&prog_idr_lock);
33957e6897f9SBjörn Töpel 	prog = idr_find(&prog_idr, id);
33967e6897f9SBjörn Töpel 	if (prog)
33977e6897f9SBjörn Töpel 		prog = bpf_prog_inc_not_zero(prog);
33987e6897f9SBjörn Töpel 	else
33997e6897f9SBjörn Töpel 		prog = ERR_PTR(-ENOENT);
34007e6897f9SBjörn Töpel 	spin_unlock_bh(&prog_idr_lock);
34017e6897f9SBjörn Töpel 	return prog;
34027e6897f9SBjörn Töpel }
34037e6897f9SBjörn Töpel 
3404b16d9aa4SMartin KaFai Lau static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3405b16d9aa4SMartin KaFai Lau {
3406b16d9aa4SMartin KaFai Lau 	struct bpf_prog *prog;
3407b16d9aa4SMartin KaFai Lau 	u32 id = attr->prog_id;
3408b16d9aa4SMartin KaFai Lau 	int fd;
3409b16d9aa4SMartin KaFai Lau 
3410b16d9aa4SMartin KaFai Lau 	if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3411b16d9aa4SMartin KaFai Lau 		return -EINVAL;
3412b16d9aa4SMartin KaFai Lau 
3413b16d9aa4SMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN))
3414b16d9aa4SMartin KaFai Lau 		return -EPERM;
3415b16d9aa4SMartin KaFai Lau 
34167e6897f9SBjörn Töpel 	prog = bpf_prog_by_id(id);
3417b16d9aa4SMartin KaFai Lau 	if (IS_ERR(prog))
3418b16d9aa4SMartin KaFai Lau 		return PTR_ERR(prog);
3419b16d9aa4SMartin KaFai Lau 
3420b16d9aa4SMartin KaFai Lau 	fd = bpf_prog_new_fd(prog);
3421b16d9aa4SMartin KaFai Lau 	if (fd < 0)
3422b16d9aa4SMartin KaFai Lau 		bpf_prog_put(prog);
3423b16d9aa4SMartin KaFai Lau 
3424b16d9aa4SMartin KaFai Lau 	return fd;
3425b16d9aa4SMartin KaFai Lau }
3426b16d9aa4SMartin KaFai Lau 
34276e71b04aSChenbo Feng #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
3428bd5f5f4eSMartin KaFai Lau 
3429bd5f5f4eSMartin KaFai Lau static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3430bd5f5f4eSMartin KaFai Lau {
3431bd5f5f4eSMartin KaFai Lau 	struct bpf_map *map;
3432bd5f5f4eSMartin KaFai Lau 	u32 id = attr->map_id;
34336e71b04aSChenbo Feng 	int f_flags;
3434bd5f5f4eSMartin KaFai Lau 	int fd;
3435bd5f5f4eSMartin KaFai Lau 
34366e71b04aSChenbo Feng 	if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
34376e71b04aSChenbo Feng 	    attr->open_flags & ~BPF_OBJ_FLAG_MASK)
3438bd5f5f4eSMartin KaFai Lau 		return -EINVAL;
3439bd5f5f4eSMartin KaFai Lau 
3440bd5f5f4eSMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN))
3441bd5f5f4eSMartin KaFai Lau 		return -EPERM;
3442bd5f5f4eSMartin KaFai Lau 
34436e71b04aSChenbo Feng 	f_flags = bpf_get_file_flag(attr->open_flags);
34446e71b04aSChenbo Feng 	if (f_flags < 0)
34456e71b04aSChenbo Feng 		return f_flags;
34466e71b04aSChenbo Feng 
3447bd5f5f4eSMartin KaFai Lau 	spin_lock_bh(&map_idr_lock);
3448bd5f5f4eSMartin KaFai Lau 	map = idr_find(&map_idr, id);
3449bd5f5f4eSMartin KaFai Lau 	if (map)
3450b0e4701cSStanislav Fomichev 		map = __bpf_map_inc_not_zero(map, true);
3451bd5f5f4eSMartin KaFai Lau 	else
3452bd5f5f4eSMartin KaFai Lau 		map = ERR_PTR(-ENOENT);
3453bd5f5f4eSMartin KaFai Lau 	spin_unlock_bh(&map_idr_lock);
3454bd5f5f4eSMartin KaFai Lau 
3455bd5f5f4eSMartin KaFai Lau 	if (IS_ERR(map))
3456bd5f5f4eSMartin KaFai Lau 		return PTR_ERR(map);
3457bd5f5f4eSMartin KaFai Lau 
34586e71b04aSChenbo Feng 	fd = bpf_map_new_fd(map, f_flags);
3459bd5f5f4eSMartin KaFai Lau 	if (fd < 0)
3460781e6282SPeng Sun 		bpf_map_put_with_uref(map);
3461bd5f5f4eSMartin KaFai Lau 
3462bd5f5f4eSMartin KaFai Lau 	return fd;
3463bd5f5f4eSMartin KaFai Lau }
3464bd5f5f4eSMartin KaFai Lau 
34657105e828SDaniel Borkmann static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
3466d8eca5bbSDaniel Borkmann 					      unsigned long addr, u32 *off,
3467d8eca5bbSDaniel Borkmann 					      u32 *type)
34687105e828SDaniel Borkmann {
3469d8eca5bbSDaniel Borkmann 	const struct bpf_map *map;
34707105e828SDaniel Borkmann 	int i;
34717105e828SDaniel Borkmann 
3472984fe94fSYiFei Zhu 	mutex_lock(&prog->aux->used_maps_mutex);
3473d8eca5bbSDaniel Borkmann 	for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3474d8eca5bbSDaniel Borkmann 		map = prog->aux->used_maps[i];
3475d8eca5bbSDaniel Borkmann 		if (map == (void *)addr) {
3476d8eca5bbSDaniel Borkmann 			*type = BPF_PSEUDO_MAP_FD;
3477984fe94fSYiFei Zhu 			goto out;
3478d8eca5bbSDaniel Borkmann 		}
3479d8eca5bbSDaniel Borkmann 		if (!map->ops->map_direct_value_meta)
3480d8eca5bbSDaniel Borkmann 			continue;
3481d8eca5bbSDaniel Borkmann 		if (!map->ops->map_direct_value_meta(map, addr, off)) {
3482d8eca5bbSDaniel Borkmann 			*type = BPF_PSEUDO_MAP_VALUE;
3483984fe94fSYiFei Zhu 			goto out;
3484d8eca5bbSDaniel Borkmann 		}
3485d8eca5bbSDaniel Borkmann 	}
3486984fe94fSYiFei Zhu 	map = NULL;
3487d8eca5bbSDaniel Borkmann 
3488984fe94fSYiFei Zhu out:
3489984fe94fSYiFei Zhu 	mutex_unlock(&prog->aux->used_maps_mutex);
3490984fe94fSYiFei Zhu 	return map;
34917105e828SDaniel Borkmann }
34927105e828SDaniel Borkmann 
349363960260SKees Cook static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
349463960260SKees Cook 					      const struct cred *f_cred)
34957105e828SDaniel Borkmann {
34967105e828SDaniel Borkmann 	const struct bpf_map *map;
34977105e828SDaniel Borkmann 	struct bpf_insn *insns;
3498d8eca5bbSDaniel Borkmann 	u32 off, type;
34997105e828SDaniel Borkmann 	u64 imm;
350029fcb05bSAndrii Nakryiko 	u8 code;
35017105e828SDaniel Borkmann 	int i;
35027105e828SDaniel Borkmann 
35037105e828SDaniel Borkmann 	insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
35047105e828SDaniel Borkmann 			GFP_USER);
35057105e828SDaniel Borkmann 	if (!insns)
35067105e828SDaniel Borkmann 		return insns;
35077105e828SDaniel Borkmann 
35087105e828SDaniel Borkmann 	for (i = 0; i < prog->len; i++) {
350929fcb05bSAndrii Nakryiko 		code = insns[i].code;
351029fcb05bSAndrii Nakryiko 
351129fcb05bSAndrii Nakryiko 		if (code == (BPF_JMP | BPF_TAIL_CALL)) {
35127105e828SDaniel Borkmann 			insns[i].code = BPF_JMP | BPF_CALL;
35137105e828SDaniel Borkmann 			insns[i].imm = BPF_FUNC_tail_call;
35147105e828SDaniel Borkmann 			/* fall-through */
35157105e828SDaniel Borkmann 		}
351629fcb05bSAndrii Nakryiko 		if (code == (BPF_JMP | BPF_CALL) ||
351729fcb05bSAndrii Nakryiko 		    code == (BPF_JMP | BPF_CALL_ARGS)) {
351829fcb05bSAndrii Nakryiko 			if (code == (BPF_JMP | BPF_CALL_ARGS))
35197105e828SDaniel Borkmann 				insns[i].code = BPF_JMP | BPF_CALL;
352063960260SKees Cook 			if (!bpf_dump_raw_ok(f_cred))
35217105e828SDaniel Borkmann 				insns[i].imm = 0;
35227105e828SDaniel Borkmann 			continue;
35237105e828SDaniel Borkmann 		}
352429fcb05bSAndrii Nakryiko 		if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
352529fcb05bSAndrii Nakryiko 			insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
352629fcb05bSAndrii Nakryiko 			continue;
352729fcb05bSAndrii Nakryiko 		}
35287105e828SDaniel Borkmann 
352929fcb05bSAndrii Nakryiko 		if (code != (BPF_LD | BPF_IMM | BPF_DW))
35307105e828SDaniel Borkmann 			continue;
35317105e828SDaniel Borkmann 
35327105e828SDaniel Borkmann 		imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
3533d8eca5bbSDaniel Borkmann 		map = bpf_map_from_imm(prog, imm, &off, &type);
35347105e828SDaniel Borkmann 		if (map) {
3535d8eca5bbSDaniel Borkmann 			insns[i].src_reg = type;
35367105e828SDaniel Borkmann 			insns[i].imm = map->id;
3537d8eca5bbSDaniel Borkmann 			insns[i + 1].imm = off;
35387105e828SDaniel Borkmann 			continue;
35397105e828SDaniel Borkmann 		}
35407105e828SDaniel Borkmann 	}
35417105e828SDaniel Borkmann 
35427105e828SDaniel Borkmann 	return insns;
35437105e828SDaniel Borkmann }
35447105e828SDaniel Borkmann 
3545c454a46bSMartin KaFai Lau static int set_info_rec_size(struct bpf_prog_info *info)
3546c454a46bSMartin KaFai Lau {
3547c454a46bSMartin KaFai Lau 	/*
3548c454a46bSMartin KaFai Lau 	 * Ensure info.*_rec_size is the same as kernel expected size
3549c454a46bSMartin KaFai Lau 	 *
3550c454a46bSMartin KaFai Lau 	 * or
3551c454a46bSMartin KaFai Lau 	 *
3552c454a46bSMartin KaFai Lau 	 * Only allow zero *_rec_size if both _rec_size and _cnt are
3553c454a46bSMartin KaFai Lau 	 * zero.  In this case, the kernel will set the expected
3554c454a46bSMartin KaFai Lau 	 * _rec_size back to the info.
3555c454a46bSMartin KaFai Lau 	 */
3556c454a46bSMartin KaFai Lau 
355711d8b82dSYonghong Song 	if ((info->nr_func_info || info->func_info_rec_size) &&
3558c454a46bSMartin KaFai Lau 	    info->func_info_rec_size != sizeof(struct bpf_func_info))
3559c454a46bSMartin KaFai Lau 		return -EINVAL;
3560c454a46bSMartin KaFai Lau 
356111d8b82dSYonghong Song 	if ((info->nr_line_info || info->line_info_rec_size) &&
3562c454a46bSMartin KaFai Lau 	    info->line_info_rec_size != sizeof(struct bpf_line_info))
3563c454a46bSMartin KaFai Lau 		return -EINVAL;
3564c454a46bSMartin KaFai Lau 
356511d8b82dSYonghong Song 	if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
3566c454a46bSMartin KaFai Lau 	    info->jited_line_info_rec_size != sizeof(__u64))
3567c454a46bSMartin KaFai Lau 		return -EINVAL;
3568c454a46bSMartin KaFai Lau 
3569c454a46bSMartin KaFai Lau 	info->func_info_rec_size = sizeof(struct bpf_func_info);
3570c454a46bSMartin KaFai Lau 	info->line_info_rec_size = sizeof(struct bpf_line_info);
3571c454a46bSMartin KaFai Lau 	info->jited_line_info_rec_size = sizeof(__u64);
3572c454a46bSMartin KaFai Lau 
3573c454a46bSMartin KaFai Lau 	return 0;
3574c454a46bSMartin KaFai Lau }
3575c454a46bSMartin KaFai Lau 
357663960260SKees Cook static int bpf_prog_get_info_by_fd(struct file *file,
357763960260SKees Cook 				   struct bpf_prog *prog,
35781e270976SMartin KaFai Lau 				   const union bpf_attr *attr,
35791e270976SMartin KaFai Lau 				   union bpf_attr __user *uattr)
35801e270976SMartin KaFai Lau {
35811e270976SMartin KaFai Lau 	struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
35825c6f2588SGreg Kroah-Hartman 	struct bpf_prog_info info;
35831e270976SMartin KaFai Lau 	u32 info_len = attr->info.info_len;
35845f8f8b93SAlexei Starovoitov 	struct bpf_prog_stats stats;
35851e270976SMartin KaFai Lau 	char __user *uinsns;
35861e270976SMartin KaFai Lau 	u32 ulen;
35871e270976SMartin KaFai Lau 	int err;
35881e270976SMartin KaFai Lau 
3589af2ac3e1SAlexei Starovoitov 	err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
35901e270976SMartin KaFai Lau 	if (err)
35911e270976SMartin KaFai Lau 		return err;
35921e270976SMartin KaFai Lau 	info_len = min_t(u32, sizeof(info), info_len);
35931e270976SMartin KaFai Lau 
35945c6f2588SGreg Kroah-Hartman 	memset(&info, 0, sizeof(info));
35951e270976SMartin KaFai Lau 	if (copy_from_user(&info, uinfo, info_len))
359689b09689SDaniel Borkmann 		return -EFAULT;
35971e270976SMartin KaFai Lau 
35981e270976SMartin KaFai Lau 	info.type = prog->type;
35991e270976SMartin KaFai Lau 	info.id = prog->aux->id;
3600cb4d2b3fSMartin KaFai Lau 	info.load_time = prog->aux->load_time;
3601cb4d2b3fSMartin KaFai Lau 	info.created_by_uid = from_kuid_munged(current_user_ns(),
3602cb4d2b3fSMartin KaFai Lau 					       prog->aux->user->uid);
3603b85fab0eSJiri Olsa 	info.gpl_compatible = prog->gpl_compatible;
36041e270976SMartin KaFai Lau 
36051e270976SMartin KaFai Lau 	memcpy(info.tag, prog->tag, sizeof(prog->tag));
3606cb4d2b3fSMartin KaFai Lau 	memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3607cb4d2b3fSMartin KaFai Lau 
3608984fe94fSYiFei Zhu 	mutex_lock(&prog->aux->used_maps_mutex);
3609cb4d2b3fSMartin KaFai Lau 	ulen = info.nr_map_ids;
3610cb4d2b3fSMartin KaFai Lau 	info.nr_map_ids = prog->aux->used_map_cnt;
3611cb4d2b3fSMartin KaFai Lau 	ulen = min_t(u32, info.nr_map_ids, ulen);
3612cb4d2b3fSMartin KaFai Lau 	if (ulen) {
3613721e08daSMartin KaFai Lau 		u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
3614cb4d2b3fSMartin KaFai Lau 		u32 i;
3615cb4d2b3fSMartin KaFai Lau 
3616cb4d2b3fSMartin KaFai Lau 		for (i = 0; i < ulen; i++)
3617cb4d2b3fSMartin KaFai Lau 			if (put_user(prog->aux->used_maps[i]->id,
3618984fe94fSYiFei Zhu 				     &user_map_ids[i])) {
3619984fe94fSYiFei Zhu 				mutex_unlock(&prog->aux->used_maps_mutex);
3620cb4d2b3fSMartin KaFai Lau 				return -EFAULT;
3621cb4d2b3fSMartin KaFai Lau 			}
3622984fe94fSYiFei Zhu 	}
3623984fe94fSYiFei Zhu 	mutex_unlock(&prog->aux->used_maps_mutex);
36241e270976SMartin KaFai Lau 
3625c454a46bSMartin KaFai Lau 	err = set_info_rec_size(&info);
3626c454a46bSMartin KaFai Lau 	if (err)
3627c454a46bSMartin KaFai Lau 		return err;
36287337224fSMartin KaFai Lau 
36295f8f8b93SAlexei Starovoitov 	bpf_prog_get_stats(prog, &stats);
36305f8f8b93SAlexei Starovoitov 	info.run_time_ns = stats.nsecs;
36315f8f8b93SAlexei Starovoitov 	info.run_cnt = stats.cnt;
36329ed9e9baSAlexei Starovoitov 	info.recursion_misses = stats.misses;
36335f8f8b93SAlexei Starovoitov 
36342c78ee89SAlexei Starovoitov 	if (!bpf_capable()) {
36351e270976SMartin KaFai Lau 		info.jited_prog_len = 0;
36361e270976SMartin KaFai Lau 		info.xlated_prog_len = 0;
3637dbecd738SSandipan Das 		info.nr_jited_ksyms = 0;
363828c2fae7SDaniel Borkmann 		info.nr_jited_func_lens = 0;
363911d8b82dSYonghong Song 		info.nr_func_info = 0;
364011d8b82dSYonghong Song 		info.nr_line_info = 0;
364111d8b82dSYonghong Song 		info.nr_jited_line_info = 0;
36421e270976SMartin KaFai Lau 		goto done;
36431e270976SMartin KaFai Lau 	}
36441e270976SMartin KaFai Lau 
36451e270976SMartin KaFai Lau 	ulen = info.xlated_prog_len;
36469975a54bSDaniel Borkmann 	info.xlated_prog_len = bpf_prog_insn_size(prog);
36471e270976SMartin KaFai Lau 	if (info.xlated_prog_len && ulen) {
36487105e828SDaniel Borkmann 		struct bpf_insn *insns_sanitized;
36497105e828SDaniel Borkmann 		bool fault;
36507105e828SDaniel Borkmann 
365163960260SKees Cook 		if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
36527105e828SDaniel Borkmann 			info.xlated_prog_insns = 0;
36537105e828SDaniel Borkmann 			goto done;
36547105e828SDaniel Borkmann 		}
365563960260SKees Cook 		insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
36567105e828SDaniel Borkmann 		if (!insns_sanitized)
36577105e828SDaniel Borkmann 			return -ENOMEM;
36581e270976SMartin KaFai Lau 		uinsns = u64_to_user_ptr(info.xlated_prog_insns);
36591e270976SMartin KaFai Lau 		ulen = min_t(u32, info.xlated_prog_len, ulen);
36607105e828SDaniel Borkmann 		fault = copy_to_user(uinsns, insns_sanitized, ulen);
36617105e828SDaniel Borkmann 		kfree(insns_sanitized);
36627105e828SDaniel Borkmann 		if (fault)
36631e270976SMartin KaFai Lau 			return -EFAULT;
36641e270976SMartin KaFai Lau 	}
36651e270976SMartin KaFai Lau 
3666675fc275SJakub Kicinski 	if (bpf_prog_is_dev_bound(prog->aux)) {
3667675fc275SJakub Kicinski 		err = bpf_prog_offload_info_fill(&info, prog);
3668675fc275SJakub Kicinski 		if (err)
3669675fc275SJakub Kicinski 			return err;
3670fcfb126dSJiong Wang 		goto done;
3671fcfb126dSJiong Wang 	}
3672fcfb126dSJiong Wang 
3673fcfb126dSJiong Wang 	/* NOTE: the following code is supposed to be skipped for offload.
3674fcfb126dSJiong Wang 	 * bpf_prog_offload_info_fill() is the place to fill similar fields
3675fcfb126dSJiong Wang 	 * for offload.
3676fcfb126dSJiong Wang 	 */
3677fcfb126dSJiong Wang 	ulen = info.jited_prog_len;
36784d56a76eSSandipan Das 	if (prog->aux->func_cnt) {
36794d56a76eSSandipan Das 		u32 i;
36804d56a76eSSandipan Das 
36814d56a76eSSandipan Das 		info.jited_prog_len = 0;
36824d56a76eSSandipan Das 		for (i = 0; i < prog->aux->func_cnt; i++)
36834d56a76eSSandipan Das 			info.jited_prog_len += prog->aux->func[i]->jited_len;
36844d56a76eSSandipan Das 	} else {
3685fcfb126dSJiong Wang 		info.jited_prog_len = prog->jited_len;
36864d56a76eSSandipan Das 	}
36874d56a76eSSandipan Das 
3688fcfb126dSJiong Wang 	if (info.jited_prog_len && ulen) {
368963960260SKees Cook 		if (bpf_dump_raw_ok(file->f_cred)) {
3690fcfb126dSJiong Wang 			uinsns = u64_to_user_ptr(info.jited_prog_insns);
3691fcfb126dSJiong Wang 			ulen = min_t(u32, info.jited_prog_len, ulen);
36924d56a76eSSandipan Das 
36934d56a76eSSandipan Das 			/* for multi-function programs, copy the JITed
36944d56a76eSSandipan Das 			 * instructions for all the functions
36954d56a76eSSandipan Das 			 */
36964d56a76eSSandipan Das 			if (prog->aux->func_cnt) {
36974d56a76eSSandipan Das 				u32 len, free, i;
36984d56a76eSSandipan Das 				u8 *img;
36994d56a76eSSandipan Das 
37004d56a76eSSandipan Das 				free = ulen;
37014d56a76eSSandipan Das 				for (i = 0; i < prog->aux->func_cnt; i++) {
37024d56a76eSSandipan Das 					len = prog->aux->func[i]->jited_len;
37034d56a76eSSandipan Das 					len = min_t(u32, len, free);
37044d56a76eSSandipan Das 					img = (u8 *) prog->aux->func[i]->bpf_func;
37054d56a76eSSandipan Das 					if (copy_to_user(uinsns, img, len))
37064d56a76eSSandipan Das 						return -EFAULT;
37074d56a76eSSandipan Das 					uinsns += len;
37084d56a76eSSandipan Das 					free -= len;
37094d56a76eSSandipan Das 					if (!free)
37104d56a76eSSandipan Das 						break;
37114d56a76eSSandipan Das 				}
37124d56a76eSSandipan Das 			} else {
3713fcfb126dSJiong Wang 				if (copy_to_user(uinsns, prog->bpf_func, ulen))
3714fcfb126dSJiong Wang 					return -EFAULT;
37154d56a76eSSandipan Das 			}
3716fcfb126dSJiong Wang 		} else {
3717fcfb126dSJiong Wang 			info.jited_prog_insns = 0;
3718fcfb126dSJiong Wang 		}
3719675fc275SJakub Kicinski 	}
3720675fc275SJakub Kicinski 
3721dbecd738SSandipan Das 	ulen = info.nr_jited_ksyms;
3722ff1889fcSSong Liu 	info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
37237a5725ddSSong Liu 	if (ulen) {
372463960260SKees Cook 		if (bpf_dump_raw_ok(file->f_cred)) {
3725ff1889fcSSong Liu 			unsigned long ksym_addr;
3726dbecd738SSandipan Das 			u64 __user *user_ksyms;
3727dbecd738SSandipan Das 			u32 i;
3728dbecd738SSandipan Das 
3729dbecd738SSandipan Das 			/* copy the address of the kernel symbol
3730dbecd738SSandipan Das 			 * corresponding to each function
3731dbecd738SSandipan Das 			 */
3732dbecd738SSandipan Das 			ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3733dbecd738SSandipan Das 			user_ksyms = u64_to_user_ptr(info.jited_ksyms);
3734ff1889fcSSong Liu 			if (prog->aux->func_cnt) {
3735dbecd738SSandipan Das 				for (i = 0; i < ulen; i++) {
3736ff1889fcSSong Liu 					ksym_addr = (unsigned long)
3737ff1889fcSSong Liu 						prog->aux->func[i]->bpf_func;
3738ff1889fcSSong Liu 					if (put_user((u64) ksym_addr,
3739ff1889fcSSong Liu 						     &user_ksyms[i]))
3740ff1889fcSSong Liu 						return -EFAULT;
3741ff1889fcSSong Liu 				}
3742ff1889fcSSong Liu 			} else {
3743ff1889fcSSong Liu 				ksym_addr = (unsigned long) prog->bpf_func;
3744ff1889fcSSong Liu 				if (put_user((u64) ksym_addr, &user_ksyms[0]))
3745dbecd738SSandipan Das 					return -EFAULT;
3746dbecd738SSandipan Das 			}
3747dbecd738SSandipan Das 		} else {
3748dbecd738SSandipan Das 			info.jited_ksyms = 0;
3749dbecd738SSandipan Das 		}
3750dbecd738SSandipan Das 	}
3751dbecd738SSandipan Das 
3752815581c1SSandipan Das 	ulen = info.nr_jited_func_lens;
3753ff1889fcSSong Liu 	info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
37547a5725ddSSong Liu 	if (ulen) {
375563960260SKees Cook 		if (bpf_dump_raw_ok(file->f_cred)) {
3756815581c1SSandipan Das 			u32 __user *user_lens;
3757815581c1SSandipan Das 			u32 func_len, i;
3758815581c1SSandipan Das 
3759815581c1SSandipan Das 			/* copy the JITed image lengths for each function */
3760815581c1SSandipan Das 			ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3761815581c1SSandipan Das 			user_lens = u64_to_user_ptr(info.jited_func_lens);
3762ff1889fcSSong Liu 			if (prog->aux->func_cnt) {
3763815581c1SSandipan Das 				for (i = 0; i < ulen; i++) {
3764ff1889fcSSong Liu 					func_len =
3765ff1889fcSSong Liu 						prog->aux->func[i]->jited_len;
3766815581c1SSandipan Das 					if (put_user(func_len, &user_lens[i]))
3767815581c1SSandipan Das 						return -EFAULT;
3768815581c1SSandipan Das 				}
3769815581c1SSandipan Das 			} else {
3770ff1889fcSSong Liu 				func_len = prog->jited_len;
3771ff1889fcSSong Liu 				if (put_user(func_len, &user_lens[0]))
3772ff1889fcSSong Liu 					return -EFAULT;
3773ff1889fcSSong Liu 			}
3774ff1889fcSSong Liu 		} else {
3775815581c1SSandipan Das 			info.jited_func_lens = 0;
3776815581c1SSandipan Das 		}
3777815581c1SSandipan Das 	}
3778815581c1SSandipan Das 
37797337224fSMartin KaFai Lau 	if (prog->aux->btf)
378022dc4a0fSAndrii Nakryiko 		info.btf_id = btf_obj_id(prog->aux->btf);
3781838e9690SYonghong Song 
378211d8b82dSYonghong Song 	ulen = info.nr_func_info;
378311d8b82dSYonghong Song 	info.nr_func_info = prog->aux->func_info_cnt;
378411d8b82dSYonghong Song 	if (info.nr_func_info && ulen) {
3785838e9690SYonghong Song 		char __user *user_finfo;
3786838e9690SYonghong Song 
3787838e9690SYonghong Song 		user_finfo = u64_to_user_ptr(info.func_info);
378811d8b82dSYonghong Song 		ulen = min_t(u32, info.nr_func_info, ulen);
3789ba64e7d8SYonghong Song 		if (copy_to_user(user_finfo, prog->aux->func_info,
37907337224fSMartin KaFai Lau 				 info.func_info_rec_size * ulen))
3791838e9690SYonghong Song 			return -EFAULT;
3792838e9690SYonghong Song 	}
3793838e9690SYonghong Song 
379411d8b82dSYonghong Song 	ulen = info.nr_line_info;
379511d8b82dSYonghong Song 	info.nr_line_info = prog->aux->nr_linfo;
379611d8b82dSYonghong Song 	if (info.nr_line_info && ulen) {
3797c454a46bSMartin KaFai Lau 		__u8 __user *user_linfo;
3798c454a46bSMartin KaFai Lau 
3799c454a46bSMartin KaFai Lau 		user_linfo = u64_to_user_ptr(info.line_info);
380011d8b82dSYonghong Song 		ulen = min_t(u32, info.nr_line_info, ulen);
3801c454a46bSMartin KaFai Lau 		if (copy_to_user(user_linfo, prog->aux->linfo,
3802c454a46bSMartin KaFai Lau 				 info.line_info_rec_size * ulen))
3803c454a46bSMartin KaFai Lau 			return -EFAULT;
3804c454a46bSMartin KaFai Lau 	}
3805c454a46bSMartin KaFai Lau 
380611d8b82dSYonghong Song 	ulen = info.nr_jited_line_info;
3807c454a46bSMartin KaFai Lau 	if (prog->aux->jited_linfo)
380811d8b82dSYonghong Song 		info.nr_jited_line_info = prog->aux->nr_linfo;
3809c454a46bSMartin KaFai Lau 	else
381011d8b82dSYonghong Song 		info.nr_jited_line_info = 0;
381111d8b82dSYonghong Song 	if (info.nr_jited_line_info && ulen) {
381263960260SKees Cook 		if (bpf_dump_raw_ok(file->f_cred)) {
3813c454a46bSMartin KaFai Lau 			__u64 __user *user_linfo;
3814c454a46bSMartin KaFai Lau 			u32 i;
3815c454a46bSMartin KaFai Lau 
3816c454a46bSMartin KaFai Lau 			user_linfo = u64_to_user_ptr(info.jited_line_info);
381711d8b82dSYonghong Song 			ulen = min_t(u32, info.nr_jited_line_info, ulen);
3818c454a46bSMartin KaFai Lau 			for (i = 0; i < ulen; i++) {
3819c454a46bSMartin KaFai Lau 				if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3820c454a46bSMartin KaFai Lau 					     &user_linfo[i]))
3821c454a46bSMartin KaFai Lau 					return -EFAULT;
3822c454a46bSMartin KaFai Lau 			}
3823c454a46bSMartin KaFai Lau 		} else {
3824c454a46bSMartin KaFai Lau 			info.jited_line_info = 0;
3825c454a46bSMartin KaFai Lau 		}
3826c454a46bSMartin KaFai Lau 	}
3827c454a46bSMartin KaFai Lau 
3828c872bdb3SSong Liu 	ulen = info.nr_prog_tags;
3829c872bdb3SSong Liu 	info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3830c872bdb3SSong Liu 	if (ulen) {
3831c872bdb3SSong Liu 		__u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3832c872bdb3SSong Liu 		u32 i;
3833c872bdb3SSong Liu 
3834c872bdb3SSong Liu 		user_prog_tags = u64_to_user_ptr(info.prog_tags);
3835c872bdb3SSong Liu 		ulen = min_t(u32, info.nr_prog_tags, ulen);
3836c872bdb3SSong Liu 		if (prog->aux->func_cnt) {
3837c872bdb3SSong Liu 			for (i = 0; i < ulen; i++) {
3838c872bdb3SSong Liu 				if (copy_to_user(user_prog_tags[i],
3839c872bdb3SSong Liu 						 prog->aux->func[i]->tag,
3840c872bdb3SSong Liu 						 BPF_TAG_SIZE))
3841c872bdb3SSong Liu 					return -EFAULT;
3842c872bdb3SSong Liu 			}
3843c872bdb3SSong Liu 		} else {
3844c872bdb3SSong Liu 			if (copy_to_user(user_prog_tags[0],
3845c872bdb3SSong Liu 					 prog->tag, BPF_TAG_SIZE))
3846c872bdb3SSong Liu 				return -EFAULT;
3847c872bdb3SSong Liu 		}
3848c872bdb3SSong Liu 	}
3849c872bdb3SSong Liu 
38501e270976SMartin KaFai Lau done:
38511e270976SMartin KaFai Lau 	if (copy_to_user(uinfo, &info, info_len) ||
38521e270976SMartin KaFai Lau 	    put_user(info_len, &uattr->info.info_len))
38531e270976SMartin KaFai Lau 		return -EFAULT;
38541e270976SMartin KaFai Lau 
38551e270976SMartin KaFai Lau 	return 0;
38561e270976SMartin KaFai Lau }
38571e270976SMartin KaFai Lau 
385863960260SKees Cook static int bpf_map_get_info_by_fd(struct file *file,
385963960260SKees Cook 				  struct bpf_map *map,
38601e270976SMartin KaFai Lau 				  const union bpf_attr *attr,
38611e270976SMartin KaFai Lau 				  union bpf_attr __user *uattr)
38621e270976SMartin KaFai Lau {
38631e270976SMartin KaFai Lau 	struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
38645c6f2588SGreg Kroah-Hartman 	struct bpf_map_info info;
38651e270976SMartin KaFai Lau 	u32 info_len = attr->info.info_len;
38661e270976SMartin KaFai Lau 	int err;
38671e270976SMartin KaFai Lau 
3868af2ac3e1SAlexei Starovoitov 	err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
38691e270976SMartin KaFai Lau 	if (err)
38701e270976SMartin KaFai Lau 		return err;
38711e270976SMartin KaFai Lau 	info_len = min_t(u32, sizeof(info), info_len);
38721e270976SMartin KaFai Lau 
38735c6f2588SGreg Kroah-Hartman 	memset(&info, 0, sizeof(info));
38741e270976SMartin KaFai Lau 	info.type = map->map_type;
38751e270976SMartin KaFai Lau 	info.id = map->id;
38761e270976SMartin KaFai Lau 	info.key_size = map->key_size;
38771e270976SMartin KaFai Lau 	info.value_size = map->value_size;
38781e270976SMartin KaFai Lau 	info.max_entries = map->max_entries;
38791e270976SMartin KaFai Lau 	info.map_flags = map->map_flags;
3880ad5b177bSMartin KaFai Lau 	memcpy(info.name, map->name, sizeof(map->name));
38811e270976SMartin KaFai Lau 
388278958fcaSMartin KaFai Lau 	if (map->btf) {
388322dc4a0fSAndrii Nakryiko 		info.btf_id = btf_obj_id(map->btf);
38849b2cf328SMartin KaFai Lau 		info.btf_key_type_id = map->btf_key_type_id;
38859b2cf328SMartin KaFai Lau 		info.btf_value_type_id = map->btf_value_type_id;
388678958fcaSMartin KaFai Lau 	}
388785d33df3SMartin KaFai Lau 	info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
388878958fcaSMartin KaFai Lau 
388952775b33SJakub Kicinski 	if (bpf_map_is_dev_bound(map)) {
389052775b33SJakub Kicinski 		err = bpf_map_offload_info_fill(&info, map);
389152775b33SJakub Kicinski 		if (err)
389252775b33SJakub Kicinski 			return err;
389352775b33SJakub Kicinski 	}
389452775b33SJakub Kicinski 
38951e270976SMartin KaFai Lau 	if (copy_to_user(uinfo, &info, info_len) ||
38961e270976SMartin KaFai Lau 	    put_user(info_len, &uattr->info.info_len))
38971e270976SMartin KaFai Lau 		return -EFAULT;
38981e270976SMartin KaFai Lau 
38991e270976SMartin KaFai Lau 	return 0;
39001e270976SMartin KaFai Lau }
39011e270976SMartin KaFai Lau 
390263960260SKees Cook static int bpf_btf_get_info_by_fd(struct file *file,
390363960260SKees Cook 				  struct btf *btf,
390462dab84cSMartin KaFai Lau 				  const union bpf_attr *attr,
390562dab84cSMartin KaFai Lau 				  union bpf_attr __user *uattr)
390662dab84cSMartin KaFai Lau {
390762dab84cSMartin KaFai Lau 	struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
390862dab84cSMartin KaFai Lau 	u32 info_len = attr->info.info_len;
390962dab84cSMartin KaFai Lau 	int err;
391062dab84cSMartin KaFai Lau 
3911af2ac3e1SAlexei Starovoitov 	err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
391262dab84cSMartin KaFai Lau 	if (err)
391362dab84cSMartin KaFai Lau 		return err;
391462dab84cSMartin KaFai Lau 
391562dab84cSMartin KaFai Lau 	return btf_get_info_by_fd(btf, attr, uattr);
391662dab84cSMartin KaFai Lau }
391762dab84cSMartin KaFai Lau 
391863960260SKees Cook static int bpf_link_get_info_by_fd(struct file *file,
391963960260SKees Cook 				  struct bpf_link *link,
3920f2e10bffSAndrii Nakryiko 				  const union bpf_attr *attr,
3921f2e10bffSAndrii Nakryiko 				  union bpf_attr __user *uattr)
3922f2e10bffSAndrii Nakryiko {
3923f2e10bffSAndrii Nakryiko 	struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3924f2e10bffSAndrii Nakryiko 	struct bpf_link_info info;
3925f2e10bffSAndrii Nakryiko 	u32 info_len = attr->info.info_len;
3926f2e10bffSAndrii Nakryiko 	int err;
3927f2e10bffSAndrii Nakryiko 
3928af2ac3e1SAlexei Starovoitov 	err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
3929f2e10bffSAndrii Nakryiko 	if (err)
3930f2e10bffSAndrii Nakryiko 		return err;
3931f2e10bffSAndrii Nakryiko 	info_len = min_t(u32, sizeof(info), info_len);
3932f2e10bffSAndrii Nakryiko 
3933f2e10bffSAndrii Nakryiko 	memset(&info, 0, sizeof(info));
3934f2e10bffSAndrii Nakryiko 	if (copy_from_user(&info, uinfo, info_len))
3935f2e10bffSAndrii Nakryiko 		return -EFAULT;
3936f2e10bffSAndrii Nakryiko 
3937f2e10bffSAndrii Nakryiko 	info.type = link->type;
3938f2e10bffSAndrii Nakryiko 	info.id = link->id;
3939f2e10bffSAndrii Nakryiko 	info.prog_id = link->prog->aux->id;
3940f2e10bffSAndrii Nakryiko 
3941f2e10bffSAndrii Nakryiko 	if (link->ops->fill_link_info) {
3942f2e10bffSAndrii Nakryiko 		err = link->ops->fill_link_info(link, &info);
3943f2e10bffSAndrii Nakryiko 		if (err)
3944f2e10bffSAndrii Nakryiko 			return err;
3945f2e10bffSAndrii Nakryiko 	}
3946f2e10bffSAndrii Nakryiko 
3947f2e10bffSAndrii Nakryiko 	if (copy_to_user(uinfo, &info, info_len) ||
3948f2e10bffSAndrii Nakryiko 	    put_user(info_len, &uattr->info.info_len))
3949f2e10bffSAndrii Nakryiko 		return -EFAULT;
3950f2e10bffSAndrii Nakryiko 
3951f2e10bffSAndrii Nakryiko 	return 0;
3952f2e10bffSAndrii Nakryiko }
3953f2e10bffSAndrii Nakryiko 
3954f2e10bffSAndrii Nakryiko 
39551e270976SMartin KaFai Lau #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
39561e270976SMartin KaFai Lau 
39571e270976SMartin KaFai Lau static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
39581e270976SMartin KaFai Lau 				  union bpf_attr __user *uattr)
39591e270976SMartin KaFai Lau {
39601e270976SMartin KaFai Lau 	int ufd = attr->info.bpf_fd;
39611e270976SMartin KaFai Lau 	struct fd f;
39621e270976SMartin KaFai Lau 	int err;
39631e270976SMartin KaFai Lau 
39641e270976SMartin KaFai Lau 	if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
39651e270976SMartin KaFai Lau 		return -EINVAL;
39661e270976SMartin KaFai Lau 
39671e270976SMartin KaFai Lau 	f = fdget(ufd);
39681e270976SMartin KaFai Lau 	if (!f.file)
39691e270976SMartin KaFai Lau 		return -EBADFD;
39701e270976SMartin KaFai Lau 
39711e270976SMartin KaFai Lau 	if (f.file->f_op == &bpf_prog_fops)
397263960260SKees Cook 		err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
39731e270976SMartin KaFai Lau 					      uattr);
39741e270976SMartin KaFai Lau 	else if (f.file->f_op == &bpf_map_fops)
397563960260SKees Cook 		err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
39761e270976SMartin KaFai Lau 					     uattr);
397760197cfbSMartin KaFai Lau 	else if (f.file->f_op == &btf_fops)
397863960260SKees Cook 		err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
3979f2e10bffSAndrii Nakryiko 	else if (f.file->f_op == &bpf_link_fops)
398063960260SKees Cook 		err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
3981f2e10bffSAndrii Nakryiko 					      attr, uattr);
39821e270976SMartin KaFai Lau 	else
39831e270976SMartin KaFai Lau 		err = -EINVAL;
39841e270976SMartin KaFai Lau 
39851e270976SMartin KaFai Lau 	fdput(f);
39861e270976SMartin KaFai Lau 	return err;
39871e270976SMartin KaFai Lau }
39881e270976SMartin KaFai Lau 
3989f56a653cSMartin KaFai Lau #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3990f56a653cSMartin KaFai Lau 
3991c571bd75SAlexei Starovoitov static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
3992f56a653cSMartin KaFai Lau {
3993f56a653cSMartin KaFai Lau 	if (CHECK_ATTR(BPF_BTF_LOAD))
3994f56a653cSMartin KaFai Lau 		return -EINVAL;
3995f56a653cSMartin KaFai Lau 
39962c78ee89SAlexei Starovoitov 	if (!bpf_capable())
3997f56a653cSMartin KaFai Lau 		return -EPERM;
3998f56a653cSMartin KaFai Lau 
3999c571bd75SAlexei Starovoitov 	return btf_new_fd(attr, uattr);
4000f56a653cSMartin KaFai Lau }
4001f56a653cSMartin KaFai Lau 
400278958fcaSMartin KaFai Lau #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
400378958fcaSMartin KaFai Lau 
400478958fcaSMartin KaFai Lau static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
400578958fcaSMartin KaFai Lau {
400678958fcaSMartin KaFai Lau 	if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
400778958fcaSMartin KaFai Lau 		return -EINVAL;
400878958fcaSMartin KaFai Lau 
400978958fcaSMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN))
401078958fcaSMartin KaFai Lau 		return -EPERM;
401178958fcaSMartin KaFai Lau 
401278958fcaSMartin KaFai Lau 	return btf_get_fd_by_id(attr->btf_id);
401378958fcaSMartin KaFai Lau }
401478958fcaSMartin KaFai Lau 
401541bdc4b4SYonghong Song static int bpf_task_fd_query_copy(const union bpf_attr *attr,
401641bdc4b4SYonghong Song 				    union bpf_attr __user *uattr,
401741bdc4b4SYonghong Song 				    u32 prog_id, u32 fd_type,
401841bdc4b4SYonghong Song 				    const char *buf, u64 probe_offset,
401941bdc4b4SYonghong Song 				    u64 probe_addr)
402041bdc4b4SYonghong Song {
402141bdc4b4SYonghong Song 	char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
402241bdc4b4SYonghong Song 	u32 len = buf ? strlen(buf) : 0, input_len;
402341bdc4b4SYonghong Song 	int err = 0;
402441bdc4b4SYonghong Song 
402541bdc4b4SYonghong Song 	if (put_user(len, &uattr->task_fd_query.buf_len))
402641bdc4b4SYonghong Song 		return -EFAULT;
402741bdc4b4SYonghong Song 	input_len = attr->task_fd_query.buf_len;
402841bdc4b4SYonghong Song 	if (input_len && ubuf) {
402941bdc4b4SYonghong Song 		if (!len) {
403041bdc4b4SYonghong Song 			/* nothing to copy, just make ubuf NULL terminated */
403141bdc4b4SYonghong Song 			char zero = '\0';
403241bdc4b4SYonghong Song 
403341bdc4b4SYonghong Song 			if (put_user(zero, ubuf))
403441bdc4b4SYonghong Song 				return -EFAULT;
403541bdc4b4SYonghong Song 		} else if (input_len >= len + 1) {
403641bdc4b4SYonghong Song 			/* ubuf can hold the string with NULL terminator */
403741bdc4b4SYonghong Song 			if (copy_to_user(ubuf, buf, len + 1))
403841bdc4b4SYonghong Song 				return -EFAULT;
403941bdc4b4SYonghong Song 		} else {
404041bdc4b4SYonghong Song 			/* ubuf cannot hold the string with NULL terminator,
404141bdc4b4SYonghong Song 			 * do a partial copy with NULL terminator.
404241bdc4b4SYonghong Song 			 */
404341bdc4b4SYonghong Song 			char zero = '\0';
404441bdc4b4SYonghong Song 
404541bdc4b4SYonghong Song 			err = -ENOSPC;
404641bdc4b4SYonghong Song 			if (copy_to_user(ubuf, buf, input_len - 1))
404741bdc4b4SYonghong Song 				return -EFAULT;
404841bdc4b4SYonghong Song 			if (put_user(zero, ubuf + input_len - 1))
404941bdc4b4SYonghong Song 				return -EFAULT;
405041bdc4b4SYonghong Song 		}
405141bdc4b4SYonghong Song 	}
405241bdc4b4SYonghong Song 
405341bdc4b4SYonghong Song 	if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
405441bdc4b4SYonghong Song 	    put_user(fd_type, &uattr->task_fd_query.fd_type) ||
405541bdc4b4SYonghong Song 	    put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
405641bdc4b4SYonghong Song 	    put_user(probe_addr, &uattr->task_fd_query.probe_addr))
405741bdc4b4SYonghong Song 		return -EFAULT;
405841bdc4b4SYonghong Song 
405941bdc4b4SYonghong Song 	return err;
406041bdc4b4SYonghong Song }
406141bdc4b4SYonghong Song 
406241bdc4b4SYonghong Song #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
406341bdc4b4SYonghong Song 
406441bdc4b4SYonghong Song static int bpf_task_fd_query(const union bpf_attr *attr,
406541bdc4b4SYonghong Song 			     union bpf_attr __user *uattr)
406641bdc4b4SYonghong Song {
406741bdc4b4SYonghong Song 	pid_t pid = attr->task_fd_query.pid;
406841bdc4b4SYonghong Song 	u32 fd = attr->task_fd_query.fd;
406941bdc4b4SYonghong Song 	const struct perf_event *event;
407041bdc4b4SYonghong Song 	struct task_struct *task;
407141bdc4b4SYonghong Song 	struct file *file;
407241bdc4b4SYonghong Song 	int err;
407341bdc4b4SYonghong Song 
407441bdc4b4SYonghong Song 	if (CHECK_ATTR(BPF_TASK_FD_QUERY))
407541bdc4b4SYonghong Song 		return -EINVAL;
407641bdc4b4SYonghong Song 
407741bdc4b4SYonghong Song 	if (!capable(CAP_SYS_ADMIN))
407841bdc4b4SYonghong Song 		return -EPERM;
407941bdc4b4SYonghong Song 
408041bdc4b4SYonghong Song 	if (attr->task_fd_query.flags != 0)
408141bdc4b4SYonghong Song 		return -EINVAL;
408241bdc4b4SYonghong Song 
408341bdc4b4SYonghong Song 	task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
408441bdc4b4SYonghong Song 	if (!task)
408541bdc4b4SYonghong Song 		return -ENOENT;
408641bdc4b4SYonghong Song 
408741bdc4b4SYonghong Song 	err = 0;
4088b48845afSEric W. Biederman 	file = fget_task(task, fd);
4089b48845afSEric W. Biederman 	put_task_struct(task);
409041bdc4b4SYonghong Song 	if (!file)
4091b48845afSEric W. Biederman 		return -EBADF;
409241bdc4b4SYonghong Song 
409370ed506cSAndrii Nakryiko 	if (file->f_op == &bpf_link_fops) {
409470ed506cSAndrii Nakryiko 		struct bpf_link *link = file->private_data;
409570ed506cSAndrii Nakryiko 
4096a3b80e10SAndrii Nakryiko 		if (link->ops == &bpf_raw_tp_link_lops) {
409770ed506cSAndrii Nakryiko 			struct bpf_raw_tp_link *raw_tp =
409870ed506cSAndrii Nakryiko 				container_of(link, struct bpf_raw_tp_link, link);
409941bdc4b4SYonghong Song 			struct bpf_raw_event_map *btp = raw_tp->btp;
410041bdc4b4SYonghong Song 
410141bdc4b4SYonghong Song 			err = bpf_task_fd_query_copy(attr, uattr,
410270ed506cSAndrii Nakryiko 						     raw_tp->link.prog->aux->id,
410341bdc4b4SYonghong Song 						     BPF_FD_TYPE_RAW_TRACEPOINT,
410441bdc4b4SYonghong Song 						     btp->tp->name, 0, 0);
410541bdc4b4SYonghong Song 			goto put_file;
410641bdc4b4SYonghong Song 		}
410770ed506cSAndrii Nakryiko 		goto out_not_supp;
410870ed506cSAndrii Nakryiko 	}
410941bdc4b4SYonghong Song 
411041bdc4b4SYonghong Song 	event = perf_get_event(file);
411141bdc4b4SYonghong Song 	if (!IS_ERR(event)) {
411241bdc4b4SYonghong Song 		u64 probe_offset, probe_addr;
411341bdc4b4SYonghong Song 		u32 prog_id, fd_type;
411441bdc4b4SYonghong Song 		const char *buf;
411541bdc4b4SYonghong Song 
411641bdc4b4SYonghong Song 		err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
411741bdc4b4SYonghong Song 					      &buf, &probe_offset,
411841bdc4b4SYonghong Song 					      &probe_addr);
411941bdc4b4SYonghong Song 		if (!err)
412041bdc4b4SYonghong Song 			err = bpf_task_fd_query_copy(attr, uattr, prog_id,
412141bdc4b4SYonghong Song 						     fd_type, buf,
412241bdc4b4SYonghong Song 						     probe_offset,
412341bdc4b4SYonghong Song 						     probe_addr);
412441bdc4b4SYonghong Song 		goto put_file;
412541bdc4b4SYonghong Song 	}
412641bdc4b4SYonghong Song 
412770ed506cSAndrii Nakryiko out_not_supp:
412841bdc4b4SYonghong Song 	err = -ENOTSUPP;
412941bdc4b4SYonghong Song put_file:
413041bdc4b4SYonghong Song 	fput(file);
413141bdc4b4SYonghong Song 	return err;
413241bdc4b4SYonghong Song }
413341bdc4b4SYonghong Song 
4134cb4d03abSBrian Vazquez #define BPF_MAP_BATCH_LAST_FIELD batch.flags
4135cb4d03abSBrian Vazquez 
4136cb4d03abSBrian Vazquez #define BPF_DO_BATCH(fn)			\
4137cb4d03abSBrian Vazquez 	do {					\
4138cb4d03abSBrian Vazquez 		if (!fn) {			\
4139cb4d03abSBrian Vazquez 			err = -ENOTSUPP;	\
4140cb4d03abSBrian Vazquez 			goto err_put;		\
4141cb4d03abSBrian Vazquez 		}				\
4142cb4d03abSBrian Vazquez 		err = fn(map, attr, uattr);	\
4143cb4d03abSBrian Vazquez 	} while (0)
4144cb4d03abSBrian Vazquez 
4145cb4d03abSBrian Vazquez static int bpf_map_do_batch(const union bpf_attr *attr,
4146cb4d03abSBrian Vazquez 			    union bpf_attr __user *uattr,
4147cb4d03abSBrian Vazquez 			    int cmd)
4148cb4d03abSBrian Vazquez {
4149cb4d03abSBrian Vazquez 	struct bpf_map *map;
4150cb4d03abSBrian Vazquez 	int err, ufd;
4151cb4d03abSBrian Vazquez 	struct fd f;
4152cb4d03abSBrian Vazquez 
4153cb4d03abSBrian Vazquez 	if (CHECK_ATTR(BPF_MAP_BATCH))
4154cb4d03abSBrian Vazquez 		return -EINVAL;
4155cb4d03abSBrian Vazquez 
4156cb4d03abSBrian Vazquez 	ufd = attr->batch.map_fd;
4157cb4d03abSBrian Vazquez 	f = fdget(ufd);
4158cb4d03abSBrian Vazquez 	map = __bpf_map_get(f);
4159cb4d03abSBrian Vazquez 	if (IS_ERR(map))
4160cb4d03abSBrian Vazquez 		return PTR_ERR(map);
4161cb4d03abSBrian Vazquez 
416205799638SYonghong Song 	if ((cmd == BPF_MAP_LOOKUP_BATCH ||
416305799638SYonghong Song 	     cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
4164cb4d03abSBrian Vazquez 	    !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4165cb4d03abSBrian Vazquez 		err = -EPERM;
4166cb4d03abSBrian Vazquez 		goto err_put;
4167cb4d03abSBrian Vazquez 	}
4168cb4d03abSBrian Vazquez 
4169cb4d03abSBrian Vazquez 	if (cmd != BPF_MAP_LOOKUP_BATCH &&
4170cb4d03abSBrian Vazquez 	    !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4171cb4d03abSBrian Vazquez 		err = -EPERM;
4172cb4d03abSBrian Vazquez 		goto err_put;
4173cb4d03abSBrian Vazquez 	}
4174cb4d03abSBrian Vazquez 
4175cb4d03abSBrian Vazquez 	if (cmd == BPF_MAP_LOOKUP_BATCH)
4176cb4d03abSBrian Vazquez 		BPF_DO_BATCH(map->ops->map_lookup_batch);
417705799638SYonghong Song 	else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
417805799638SYonghong Song 		BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
4179aa2e93b8SBrian Vazquez 	else if (cmd == BPF_MAP_UPDATE_BATCH)
4180aa2e93b8SBrian Vazquez 		BPF_DO_BATCH(map->ops->map_update_batch);
4181aa2e93b8SBrian Vazquez 	else
4182aa2e93b8SBrian Vazquez 		BPF_DO_BATCH(map->ops->map_delete_batch);
4183cb4d03abSBrian Vazquez 
4184cb4d03abSBrian Vazquez err_put:
4185cb4d03abSBrian Vazquez 	fdput(f);
4186cb4d03abSBrian Vazquez 	return err;
4187cb4d03abSBrian Vazquez }
4188cb4d03abSBrian Vazquez 
4189af2ac3e1SAlexei Starovoitov static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
4190af2ac3e1SAlexei Starovoitov 				   struct bpf_prog *prog)
4191de4e05caSYonghong Song {
41924a1e7c0cSToke Høiland-Jørgensen 	if (attr->link_create.attach_type != prog->expected_attach_type)
41934a1e7c0cSToke Høiland-Jørgensen 		return -EINVAL;
4194de4e05caSYonghong Song 
41954a1e7c0cSToke Høiland-Jørgensen 	if (prog->expected_attach_type == BPF_TRACE_ITER)
4196af2ac3e1SAlexei Starovoitov 		return bpf_iter_link_attach(attr, uattr, prog);
41974a1e7c0cSToke Høiland-Jørgensen 	else if (prog->type == BPF_PROG_TYPE_EXT)
41984a1e7c0cSToke Høiland-Jørgensen 		return bpf_tracing_prog_attach(prog,
41994a1e7c0cSToke Høiland-Jørgensen 					       attr->link_create.target_fd,
42004a1e7c0cSToke Høiland-Jørgensen 					       attr->link_create.target_btf_id);
4201de4e05caSYonghong Song 	return -EINVAL;
4202de4e05caSYonghong Song }
4203de4e05caSYonghong Song 
42045e7b3020SYonghong Song #define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
4205af2ac3e1SAlexei Starovoitov static int link_create(union bpf_attr *attr, bpfptr_t uattr)
4206af6eea57SAndrii Nakryiko {
4207af6eea57SAndrii Nakryiko 	enum bpf_prog_type ptype;
4208af6eea57SAndrii Nakryiko 	struct bpf_prog *prog;
4209af6eea57SAndrii Nakryiko 	int ret;
4210af6eea57SAndrii Nakryiko 
4211af6eea57SAndrii Nakryiko 	if (CHECK_ATTR(BPF_LINK_CREATE))
4212af6eea57SAndrii Nakryiko 		return -EINVAL;
4213af6eea57SAndrii Nakryiko 
42144a1e7c0cSToke Høiland-Jørgensen 	prog = bpf_prog_get(attr->link_create.prog_fd);
4215af6eea57SAndrii Nakryiko 	if (IS_ERR(prog))
4216af6eea57SAndrii Nakryiko 		return PTR_ERR(prog);
4217af6eea57SAndrii Nakryiko 
4218af6eea57SAndrii Nakryiko 	ret = bpf_prog_attach_check_attach_type(prog,
4219af6eea57SAndrii Nakryiko 						attr->link_create.attach_type);
4220af6eea57SAndrii Nakryiko 	if (ret)
42214a1e7c0cSToke Høiland-Jørgensen 		goto out;
42224a1e7c0cSToke Høiland-Jørgensen 
4223b89fbfbbSAndrii Nakryiko 	switch (prog->type) {
4224b89fbfbbSAndrii Nakryiko 	case BPF_PROG_TYPE_EXT:
4225af2ac3e1SAlexei Starovoitov 		ret = tracing_bpf_link_attach(attr, uattr, prog);
42264a1e7c0cSToke Høiland-Jørgensen 		goto out;
4227b89fbfbbSAndrii Nakryiko 	case BPF_PROG_TYPE_PERF_EVENT:
4228b89fbfbbSAndrii Nakryiko 	case BPF_PROG_TYPE_KPROBE:
4229b89fbfbbSAndrii Nakryiko 	case BPF_PROG_TYPE_TRACEPOINT:
4230b89fbfbbSAndrii Nakryiko 		if (attr->link_create.attach_type != BPF_PERF_EVENT) {
4231b89fbfbbSAndrii Nakryiko 			ret = -EINVAL;
4232b89fbfbbSAndrii Nakryiko 			goto out;
42334a1e7c0cSToke Høiland-Jørgensen 		}
4234b89fbfbbSAndrii Nakryiko 		ptype = prog->type;
4235b89fbfbbSAndrii Nakryiko 		break;
4236b89fbfbbSAndrii Nakryiko 	default:
42374a1e7c0cSToke Høiland-Jørgensen 		ptype = attach_type_to_prog_type(attr->link_create.attach_type);
42384a1e7c0cSToke Høiland-Jørgensen 		if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
42394a1e7c0cSToke Høiland-Jørgensen 			ret = -EINVAL;
42404a1e7c0cSToke Høiland-Jørgensen 			goto out;
42414a1e7c0cSToke Høiland-Jørgensen 		}
4242b89fbfbbSAndrii Nakryiko 		break;
4243b89fbfbbSAndrii Nakryiko 	}
4244af6eea57SAndrii Nakryiko 
4245af6eea57SAndrii Nakryiko 	switch (ptype) {
4246af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SKB:
4247af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCK:
4248af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4249af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_SOCK_OPS:
4250af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_DEVICE:
4251af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SYSCTL:
4252af6eea57SAndrii Nakryiko 	case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4253af6eea57SAndrii Nakryiko 		ret = cgroup_bpf_link_attach(attr, prog);
4254af6eea57SAndrii Nakryiko 		break;
4255de4e05caSYonghong Song 	case BPF_PROG_TYPE_TRACING:
4256af2ac3e1SAlexei Starovoitov 		ret = tracing_bpf_link_attach(attr, uattr, prog);
4257de4e05caSYonghong Song 		break;
42587f045a49SJakub Sitnicki 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
4259e9ddbb77SJakub Sitnicki 	case BPF_PROG_TYPE_SK_LOOKUP:
42607f045a49SJakub Sitnicki 		ret = netns_bpf_link_create(attr, prog);
42617f045a49SJakub Sitnicki 		break;
4262310ad797SAndrii Nakryiko #ifdef CONFIG_NET
4263aa8d3a71SAndrii Nakryiko 	case BPF_PROG_TYPE_XDP:
4264aa8d3a71SAndrii Nakryiko 		ret = bpf_xdp_link_attach(attr, prog);
4265aa8d3a71SAndrii Nakryiko 		break;
4266310ad797SAndrii Nakryiko #endif
4267b89fbfbbSAndrii Nakryiko #ifdef CONFIG_PERF_EVENTS
4268b89fbfbbSAndrii Nakryiko 	case BPF_PROG_TYPE_PERF_EVENT:
4269b89fbfbbSAndrii Nakryiko 	case BPF_PROG_TYPE_TRACEPOINT:
4270b89fbfbbSAndrii Nakryiko 	case BPF_PROG_TYPE_KPROBE:
4271b89fbfbbSAndrii Nakryiko 		ret = bpf_perf_link_attach(attr, prog);
4272b89fbfbbSAndrii Nakryiko 		break;
4273b89fbfbbSAndrii Nakryiko #endif
4274af6eea57SAndrii Nakryiko 	default:
4275af6eea57SAndrii Nakryiko 		ret = -EINVAL;
4276af6eea57SAndrii Nakryiko 	}
4277af6eea57SAndrii Nakryiko 
42784a1e7c0cSToke Høiland-Jørgensen out:
4279af6eea57SAndrii Nakryiko 	if (ret < 0)
4280af6eea57SAndrii Nakryiko 		bpf_prog_put(prog);
4281af6eea57SAndrii Nakryiko 	return ret;
4282af6eea57SAndrii Nakryiko }
4283af6eea57SAndrii Nakryiko 
42840c991ebcSAndrii Nakryiko #define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
42850c991ebcSAndrii Nakryiko 
42860c991ebcSAndrii Nakryiko static int link_update(union bpf_attr *attr)
42870c991ebcSAndrii Nakryiko {
42880c991ebcSAndrii Nakryiko 	struct bpf_prog *old_prog = NULL, *new_prog;
42890c991ebcSAndrii Nakryiko 	struct bpf_link *link;
42900c991ebcSAndrii Nakryiko 	u32 flags;
42910c991ebcSAndrii Nakryiko 	int ret;
42920c991ebcSAndrii Nakryiko 
42930c991ebcSAndrii Nakryiko 	if (CHECK_ATTR(BPF_LINK_UPDATE))
42940c991ebcSAndrii Nakryiko 		return -EINVAL;
42950c991ebcSAndrii Nakryiko 
42960c991ebcSAndrii Nakryiko 	flags = attr->link_update.flags;
42970c991ebcSAndrii Nakryiko 	if (flags & ~BPF_F_REPLACE)
42980c991ebcSAndrii Nakryiko 		return -EINVAL;
42990c991ebcSAndrii Nakryiko 
43000c991ebcSAndrii Nakryiko 	link = bpf_link_get_from_fd(attr->link_update.link_fd);
43010c991ebcSAndrii Nakryiko 	if (IS_ERR(link))
43020c991ebcSAndrii Nakryiko 		return PTR_ERR(link);
43030c991ebcSAndrii Nakryiko 
43040c991ebcSAndrii Nakryiko 	new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
43054adb7a4aSAndrii Nakryiko 	if (IS_ERR(new_prog)) {
43064adb7a4aSAndrii Nakryiko 		ret = PTR_ERR(new_prog);
43074adb7a4aSAndrii Nakryiko 		goto out_put_link;
43084adb7a4aSAndrii Nakryiko 	}
43090c991ebcSAndrii Nakryiko 
43100c991ebcSAndrii Nakryiko 	if (flags & BPF_F_REPLACE) {
43110c991ebcSAndrii Nakryiko 		old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
43120c991ebcSAndrii Nakryiko 		if (IS_ERR(old_prog)) {
43130c991ebcSAndrii Nakryiko 			ret = PTR_ERR(old_prog);
43140c991ebcSAndrii Nakryiko 			old_prog = NULL;
43150c991ebcSAndrii Nakryiko 			goto out_put_progs;
43160c991ebcSAndrii Nakryiko 		}
43174adb7a4aSAndrii Nakryiko 	} else if (attr->link_update.old_prog_fd) {
43184adb7a4aSAndrii Nakryiko 		ret = -EINVAL;
43194adb7a4aSAndrii Nakryiko 		goto out_put_progs;
43200c991ebcSAndrii Nakryiko 	}
43210c991ebcSAndrii Nakryiko 
4322f9d04127SAndrii Nakryiko 	if (link->ops->update_prog)
4323f9d04127SAndrii Nakryiko 		ret = link->ops->update_prog(link, new_prog, old_prog);
4324f9d04127SAndrii Nakryiko 	else
43250c991ebcSAndrii Nakryiko 		ret = -EINVAL;
43260c991ebcSAndrii Nakryiko 
43270c991ebcSAndrii Nakryiko out_put_progs:
43280c991ebcSAndrii Nakryiko 	if (old_prog)
43290c991ebcSAndrii Nakryiko 		bpf_prog_put(old_prog);
43300c991ebcSAndrii Nakryiko 	if (ret)
43310c991ebcSAndrii Nakryiko 		bpf_prog_put(new_prog);
43324adb7a4aSAndrii Nakryiko out_put_link:
43334adb7a4aSAndrii Nakryiko 	bpf_link_put(link);
43340c991ebcSAndrii Nakryiko 	return ret;
43350c991ebcSAndrii Nakryiko }
43360c991ebcSAndrii Nakryiko 
433773b11c2aSAndrii Nakryiko #define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
433873b11c2aSAndrii Nakryiko 
433973b11c2aSAndrii Nakryiko static int link_detach(union bpf_attr *attr)
434073b11c2aSAndrii Nakryiko {
434173b11c2aSAndrii Nakryiko 	struct bpf_link *link;
434273b11c2aSAndrii Nakryiko 	int ret;
434373b11c2aSAndrii Nakryiko 
434473b11c2aSAndrii Nakryiko 	if (CHECK_ATTR(BPF_LINK_DETACH))
434573b11c2aSAndrii Nakryiko 		return -EINVAL;
434673b11c2aSAndrii Nakryiko 
434773b11c2aSAndrii Nakryiko 	link = bpf_link_get_from_fd(attr->link_detach.link_fd);
434873b11c2aSAndrii Nakryiko 	if (IS_ERR(link))
434973b11c2aSAndrii Nakryiko 		return PTR_ERR(link);
435073b11c2aSAndrii Nakryiko 
435173b11c2aSAndrii Nakryiko 	if (link->ops->detach)
435273b11c2aSAndrii Nakryiko 		ret = link->ops->detach(link);
435373b11c2aSAndrii Nakryiko 	else
435473b11c2aSAndrii Nakryiko 		ret = -EOPNOTSUPP;
435573b11c2aSAndrii Nakryiko 
435673b11c2aSAndrii Nakryiko 	bpf_link_put(link);
435773b11c2aSAndrii Nakryiko 	return ret;
435873b11c2aSAndrii Nakryiko }
435973b11c2aSAndrii Nakryiko 
4360005142b8SAlexei Starovoitov static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
43612d602c8cSAndrii Nakryiko {
4362005142b8SAlexei Starovoitov 	return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4363005142b8SAlexei Starovoitov }
4364005142b8SAlexei Starovoitov 
4365005142b8SAlexei Starovoitov struct bpf_link *bpf_link_by_id(u32 id)
4366005142b8SAlexei Starovoitov {
4367005142b8SAlexei Starovoitov 	struct bpf_link *link;
4368005142b8SAlexei Starovoitov 
4369005142b8SAlexei Starovoitov 	if (!id)
4370005142b8SAlexei Starovoitov 		return ERR_PTR(-ENOENT);
4371005142b8SAlexei Starovoitov 
4372005142b8SAlexei Starovoitov 	spin_lock_bh(&link_idr_lock);
4373005142b8SAlexei Starovoitov 	/* before link is "settled", ID is 0, pretend it doesn't exist yet */
4374005142b8SAlexei Starovoitov 	link = idr_find(&link_idr, id);
4375005142b8SAlexei Starovoitov 	if (link) {
4376005142b8SAlexei Starovoitov 		if (link->id)
4377005142b8SAlexei Starovoitov 			link = bpf_link_inc_not_zero(link);
4378005142b8SAlexei Starovoitov 		else
4379005142b8SAlexei Starovoitov 			link = ERR_PTR(-EAGAIN);
4380005142b8SAlexei Starovoitov 	} else {
4381005142b8SAlexei Starovoitov 		link = ERR_PTR(-ENOENT);
4382005142b8SAlexei Starovoitov 	}
4383005142b8SAlexei Starovoitov 	spin_unlock_bh(&link_idr_lock);
4384005142b8SAlexei Starovoitov 	return link;
43852d602c8cSAndrii Nakryiko }
43862d602c8cSAndrii Nakryiko 
43872d602c8cSAndrii Nakryiko #define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
43882d602c8cSAndrii Nakryiko 
43892d602c8cSAndrii Nakryiko static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
43902d602c8cSAndrii Nakryiko {
43912d602c8cSAndrii Nakryiko 	struct bpf_link *link;
43922d602c8cSAndrii Nakryiko 	u32 id = attr->link_id;
4393005142b8SAlexei Starovoitov 	int fd;
43942d602c8cSAndrii Nakryiko 
43952d602c8cSAndrii Nakryiko 	if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
43962d602c8cSAndrii Nakryiko 		return -EINVAL;
43972d602c8cSAndrii Nakryiko 
43982d602c8cSAndrii Nakryiko 	if (!capable(CAP_SYS_ADMIN))
43992d602c8cSAndrii Nakryiko 		return -EPERM;
44002d602c8cSAndrii Nakryiko 
4401005142b8SAlexei Starovoitov 	link = bpf_link_by_id(id);
4402005142b8SAlexei Starovoitov 	if (IS_ERR(link))
4403005142b8SAlexei Starovoitov 		return PTR_ERR(link);
44042d602c8cSAndrii Nakryiko 
44052d602c8cSAndrii Nakryiko 	fd = bpf_link_new_fd(link);
44062d602c8cSAndrii Nakryiko 	if (fd < 0)
44072d602c8cSAndrii Nakryiko 		bpf_link_put(link);
44082d602c8cSAndrii Nakryiko 
44092d602c8cSAndrii Nakryiko 	return fd;
44102d602c8cSAndrii Nakryiko }
44112d602c8cSAndrii Nakryiko 
4412d46edd67SSong Liu DEFINE_MUTEX(bpf_stats_enabled_mutex);
4413d46edd67SSong Liu 
4414d46edd67SSong Liu static int bpf_stats_release(struct inode *inode, struct file *file)
4415d46edd67SSong Liu {
4416d46edd67SSong Liu 	mutex_lock(&bpf_stats_enabled_mutex);
4417d46edd67SSong Liu 	static_key_slow_dec(&bpf_stats_enabled_key.key);
4418d46edd67SSong Liu 	mutex_unlock(&bpf_stats_enabled_mutex);
4419d46edd67SSong Liu 	return 0;
4420d46edd67SSong Liu }
4421d46edd67SSong Liu 
4422d46edd67SSong Liu static const struct file_operations bpf_stats_fops = {
4423d46edd67SSong Liu 	.release = bpf_stats_release,
4424d46edd67SSong Liu };
4425d46edd67SSong Liu 
4426d46edd67SSong Liu static int bpf_enable_runtime_stats(void)
4427d46edd67SSong Liu {
4428d46edd67SSong Liu 	int fd;
4429d46edd67SSong Liu 
4430d46edd67SSong Liu 	mutex_lock(&bpf_stats_enabled_mutex);
4431d46edd67SSong Liu 
4432d46edd67SSong Liu 	/* Set a very high limit to avoid overflow */
4433d46edd67SSong Liu 	if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4434d46edd67SSong Liu 		mutex_unlock(&bpf_stats_enabled_mutex);
4435d46edd67SSong Liu 		return -EBUSY;
4436d46edd67SSong Liu 	}
4437d46edd67SSong Liu 
4438d46edd67SSong Liu 	fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4439d46edd67SSong Liu 	if (fd >= 0)
4440d46edd67SSong Liu 		static_key_slow_inc(&bpf_stats_enabled_key.key);
4441d46edd67SSong Liu 
4442d46edd67SSong Liu 	mutex_unlock(&bpf_stats_enabled_mutex);
4443d46edd67SSong Liu 	return fd;
4444d46edd67SSong Liu }
4445d46edd67SSong Liu 
4446d46edd67SSong Liu #define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4447d46edd67SSong Liu 
4448d46edd67SSong Liu static int bpf_enable_stats(union bpf_attr *attr)
4449d46edd67SSong Liu {
4450d46edd67SSong Liu 
4451d46edd67SSong Liu 	if (CHECK_ATTR(BPF_ENABLE_STATS))
4452d46edd67SSong Liu 		return -EINVAL;
4453d46edd67SSong Liu 
4454d46edd67SSong Liu 	if (!capable(CAP_SYS_ADMIN))
4455d46edd67SSong Liu 		return -EPERM;
4456d46edd67SSong Liu 
4457d46edd67SSong Liu 	switch (attr->enable_stats.type) {
4458d46edd67SSong Liu 	case BPF_STATS_RUN_TIME:
4459d46edd67SSong Liu 		return bpf_enable_runtime_stats();
4460d46edd67SSong Liu 	default:
4461d46edd67SSong Liu 		break;
4462d46edd67SSong Liu 	}
4463d46edd67SSong Liu 	return -EINVAL;
4464d46edd67SSong Liu }
4465d46edd67SSong Liu 
4466ac51d99bSYonghong Song #define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4467ac51d99bSYonghong Song 
4468ac51d99bSYonghong Song static int bpf_iter_create(union bpf_attr *attr)
4469ac51d99bSYonghong Song {
4470ac51d99bSYonghong Song 	struct bpf_link *link;
4471ac51d99bSYonghong Song 	int err;
4472ac51d99bSYonghong Song 
4473ac51d99bSYonghong Song 	if (CHECK_ATTR(BPF_ITER_CREATE))
4474ac51d99bSYonghong Song 		return -EINVAL;
4475ac51d99bSYonghong Song 
4476ac51d99bSYonghong Song 	if (attr->iter_create.flags)
4477ac51d99bSYonghong Song 		return -EINVAL;
4478ac51d99bSYonghong Song 
4479ac51d99bSYonghong Song 	link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4480ac51d99bSYonghong Song 	if (IS_ERR(link))
4481ac51d99bSYonghong Song 		return PTR_ERR(link);
4482ac51d99bSYonghong Song 
4483ac51d99bSYonghong Song 	err = bpf_iter_new_fd(link);
4484ac51d99bSYonghong Song 	bpf_link_put(link);
4485ac51d99bSYonghong Song 
4486ac51d99bSYonghong Song 	return err;
4487ac51d99bSYonghong Song }
4488ac51d99bSYonghong Song 
4489ef15314aSYiFei Zhu #define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4490ef15314aSYiFei Zhu 
4491ef15314aSYiFei Zhu static int bpf_prog_bind_map(union bpf_attr *attr)
4492ef15314aSYiFei Zhu {
4493ef15314aSYiFei Zhu 	struct bpf_prog *prog;
4494ef15314aSYiFei Zhu 	struct bpf_map *map;
4495ef15314aSYiFei Zhu 	struct bpf_map **used_maps_old, **used_maps_new;
4496ef15314aSYiFei Zhu 	int i, ret = 0;
4497ef15314aSYiFei Zhu 
4498ef15314aSYiFei Zhu 	if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4499ef15314aSYiFei Zhu 		return -EINVAL;
4500ef15314aSYiFei Zhu 
4501ef15314aSYiFei Zhu 	if (attr->prog_bind_map.flags)
4502ef15314aSYiFei Zhu 		return -EINVAL;
4503ef15314aSYiFei Zhu 
4504ef15314aSYiFei Zhu 	prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4505ef15314aSYiFei Zhu 	if (IS_ERR(prog))
4506ef15314aSYiFei Zhu 		return PTR_ERR(prog);
4507ef15314aSYiFei Zhu 
4508ef15314aSYiFei Zhu 	map = bpf_map_get(attr->prog_bind_map.map_fd);
4509ef15314aSYiFei Zhu 	if (IS_ERR(map)) {
4510ef15314aSYiFei Zhu 		ret = PTR_ERR(map);
4511ef15314aSYiFei Zhu 		goto out_prog_put;
4512ef15314aSYiFei Zhu 	}
4513ef15314aSYiFei Zhu 
4514ef15314aSYiFei Zhu 	mutex_lock(&prog->aux->used_maps_mutex);
4515ef15314aSYiFei Zhu 
4516ef15314aSYiFei Zhu 	used_maps_old = prog->aux->used_maps;
4517ef15314aSYiFei Zhu 
4518ef15314aSYiFei Zhu 	for (i = 0; i < prog->aux->used_map_cnt; i++)
45191028ae40SStanislav Fomichev 		if (used_maps_old[i] == map) {
45201028ae40SStanislav Fomichev 			bpf_map_put(map);
4521ef15314aSYiFei Zhu 			goto out_unlock;
45221028ae40SStanislav Fomichev 		}
4523ef15314aSYiFei Zhu 
4524ef15314aSYiFei Zhu 	used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4525ef15314aSYiFei Zhu 				      sizeof(used_maps_new[0]),
4526ef15314aSYiFei Zhu 				      GFP_KERNEL);
4527ef15314aSYiFei Zhu 	if (!used_maps_new) {
4528ef15314aSYiFei Zhu 		ret = -ENOMEM;
4529ef15314aSYiFei Zhu 		goto out_unlock;
4530ef15314aSYiFei Zhu 	}
4531ef15314aSYiFei Zhu 
4532ef15314aSYiFei Zhu 	memcpy(used_maps_new, used_maps_old,
4533ef15314aSYiFei Zhu 	       sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4534ef15314aSYiFei Zhu 	used_maps_new[prog->aux->used_map_cnt] = map;
4535ef15314aSYiFei Zhu 
4536ef15314aSYiFei Zhu 	prog->aux->used_map_cnt++;
4537ef15314aSYiFei Zhu 	prog->aux->used_maps = used_maps_new;
4538ef15314aSYiFei Zhu 
4539ef15314aSYiFei Zhu 	kfree(used_maps_old);
4540ef15314aSYiFei Zhu 
4541ef15314aSYiFei Zhu out_unlock:
4542ef15314aSYiFei Zhu 	mutex_unlock(&prog->aux->used_maps_mutex);
4543ef15314aSYiFei Zhu 
4544ef15314aSYiFei Zhu 	if (ret)
4545ef15314aSYiFei Zhu 		bpf_map_put(map);
4546ef15314aSYiFei Zhu out_prog_put:
4547ef15314aSYiFei Zhu 	bpf_prog_put(prog);
4548ef15314aSYiFei Zhu 	return ret;
4549ef15314aSYiFei Zhu }
4550ef15314aSYiFei Zhu 
4551af2ac3e1SAlexei Starovoitov static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
455299c55f7dSAlexei Starovoitov {
45538096f229SGreg Kroah-Hartman 	union bpf_attr attr;
455499c55f7dSAlexei Starovoitov 	int err;
455599c55f7dSAlexei Starovoitov 
45562c78ee89SAlexei Starovoitov 	if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
455799c55f7dSAlexei Starovoitov 		return -EPERM;
455899c55f7dSAlexei Starovoitov 
4559dcab51f1SMartin KaFai Lau 	err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
456099c55f7dSAlexei Starovoitov 	if (err)
456199c55f7dSAlexei Starovoitov 		return err;
45621e270976SMartin KaFai Lau 	size = min_t(u32, size, sizeof(attr));
456399c55f7dSAlexei Starovoitov 
456499c55f7dSAlexei Starovoitov 	/* copy attributes from user space, may be less than sizeof(bpf_attr) */
45658096f229SGreg Kroah-Hartman 	memset(&attr, 0, sizeof(attr));
4566af2ac3e1SAlexei Starovoitov 	if (copy_from_bpfptr(&attr, uattr, size) != 0)
456799c55f7dSAlexei Starovoitov 		return -EFAULT;
456899c55f7dSAlexei Starovoitov 
4569afdb09c7SChenbo Feng 	err = security_bpf(cmd, &attr, size);
4570afdb09c7SChenbo Feng 	if (err < 0)
4571afdb09c7SChenbo Feng 		return err;
4572afdb09c7SChenbo Feng 
457399c55f7dSAlexei Starovoitov 	switch (cmd) {
457499c55f7dSAlexei Starovoitov 	case BPF_MAP_CREATE:
457599c55f7dSAlexei Starovoitov 		err = map_create(&attr);
457699c55f7dSAlexei Starovoitov 		break;
4577db20fd2bSAlexei Starovoitov 	case BPF_MAP_LOOKUP_ELEM:
4578db20fd2bSAlexei Starovoitov 		err = map_lookup_elem(&attr);
4579db20fd2bSAlexei Starovoitov 		break;
4580db20fd2bSAlexei Starovoitov 	case BPF_MAP_UPDATE_ELEM:
4581af2ac3e1SAlexei Starovoitov 		err = map_update_elem(&attr, uattr);
4582db20fd2bSAlexei Starovoitov 		break;
4583db20fd2bSAlexei Starovoitov 	case BPF_MAP_DELETE_ELEM:
4584db20fd2bSAlexei Starovoitov 		err = map_delete_elem(&attr);
4585db20fd2bSAlexei Starovoitov 		break;
4586db20fd2bSAlexei Starovoitov 	case BPF_MAP_GET_NEXT_KEY:
4587db20fd2bSAlexei Starovoitov 		err = map_get_next_key(&attr);
4588db20fd2bSAlexei Starovoitov 		break;
458987df15deSDaniel Borkmann 	case BPF_MAP_FREEZE:
459087df15deSDaniel Borkmann 		err = map_freeze(&attr);
459187df15deSDaniel Borkmann 		break;
459209756af4SAlexei Starovoitov 	case BPF_PROG_LOAD:
4593838e9690SYonghong Song 		err = bpf_prog_load(&attr, uattr);
459409756af4SAlexei Starovoitov 		break;
4595b2197755SDaniel Borkmann 	case BPF_OBJ_PIN:
4596b2197755SDaniel Borkmann 		err = bpf_obj_pin(&attr);
4597b2197755SDaniel Borkmann 		break;
4598b2197755SDaniel Borkmann 	case BPF_OBJ_GET:
4599b2197755SDaniel Borkmann 		err = bpf_obj_get(&attr);
4600b2197755SDaniel Borkmann 		break;
4601f4324551SDaniel Mack 	case BPF_PROG_ATTACH:
4602f4324551SDaniel Mack 		err = bpf_prog_attach(&attr);
4603f4324551SDaniel Mack 		break;
4604f4324551SDaniel Mack 	case BPF_PROG_DETACH:
4605f4324551SDaniel Mack 		err = bpf_prog_detach(&attr);
4606f4324551SDaniel Mack 		break;
4607468e2f64SAlexei Starovoitov 	case BPF_PROG_QUERY:
4608af2ac3e1SAlexei Starovoitov 		err = bpf_prog_query(&attr, uattr.user);
4609468e2f64SAlexei Starovoitov 		break;
46101cf1cae9SAlexei Starovoitov 	case BPF_PROG_TEST_RUN:
4611af2ac3e1SAlexei Starovoitov 		err = bpf_prog_test_run(&attr, uattr.user);
46121cf1cae9SAlexei Starovoitov 		break;
461334ad5580SMartin KaFai Lau 	case BPF_PROG_GET_NEXT_ID:
4614af2ac3e1SAlexei Starovoitov 		err = bpf_obj_get_next_id(&attr, uattr.user,
461534ad5580SMartin KaFai Lau 					  &prog_idr, &prog_idr_lock);
461634ad5580SMartin KaFai Lau 		break;
461734ad5580SMartin KaFai Lau 	case BPF_MAP_GET_NEXT_ID:
4618af2ac3e1SAlexei Starovoitov 		err = bpf_obj_get_next_id(&attr, uattr.user,
461934ad5580SMartin KaFai Lau 					  &map_idr, &map_idr_lock);
462034ad5580SMartin KaFai Lau 		break;
46211b9ed84eSQuentin Monnet 	case BPF_BTF_GET_NEXT_ID:
4622af2ac3e1SAlexei Starovoitov 		err = bpf_obj_get_next_id(&attr, uattr.user,
46231b9ed84eSQuentin Monnet 					  &btf_idr, &btf_idr_lock);
46241b9ed84eSQuentin Monnet 		break;
4625b16d9aa4SMartin KaFai Lau 	case BPF_PROG_GET_FD_BY_ID:
4626b16d9aa4SMartin KaFai Lau 		err = bpf_prog_get_fd_by_id(&attr);
4627b16d9aa4SMartin KaFai Lau 		break;
4628bd5f5f4eSMartin KaFai Lau 	case BPF_MAP_GET_FD_BY_ID:
4629bd5f5f4eSMartin KaFai Lau 		err = bpf_map_get_fd_by_id(&attr);
4630bd5f5f4eSMartin KaFai Lau 		break;
46311e270976SMartin KaFai Lau 	case BPF_OBJ_GET_INFO_BY_FD:
4632af2ac3e1SAlexei Starovoitov 		err = bpf_obj_get_info_by_fd(&attr, uattr.user);
46331e270976SMartin KaFai Lau 		break;
4634c4f6699dSAlexei Starovoitov 	case BPF_RAW_TRACEPOINT_OPEN:
4635c4f6699dSAlexei Starovoitov 		err = bpf_raw_tracepoint_open(&attr);
4636c4f6699dSAlexei Starovoitov 		break;
4637f56a653cSMartin KaFai Lau 	case BPF_BTF_LOAD:
4638c571bd75SAlexei Starovoitov 		err = bpf_btf_load(&attr, uattr);
4639f56a653cSMartin KaFai Lau 		break;
464078958fcaSMartin KaFai Lau 	case BPF_BTF_GET_FD_BY_ID:
464178958fcaSMartin KaFai Lau 		err = bpf_btf_get_fd_by_id(&attr);
464278958fcaSMartin KaFai Lau 		break;
464341bdc4b4SYonghong Song 	case BPF_TASK_FD_QUERY:
4644af2ac3e1SAlexei Starovoitov 		err = bpf_task_fd_query(&attr, uattr.user);
464541bdc4b4SYonghong Song 		break;
4646bd513cd0SMauricio Vasquez B 	case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4647bd513cd0SMauricio Vasquez B 		err = map_lookup_and_delete_elem(&attr);
4648bd513cd0SMauricio Vasquez B 		break;
4649cb4d03abSBrian Vazquez 	case BPF_MAP_LOOKUP_BATCH:
4650af2ac3e1SAlexei Starovoitov 		err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
4651cb4d03abSBrian Vazquez 		break;
465205799638SYonghong Song 	case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
4653af2ac3e1SAlexei Starovoitov 		err = bpf_map_do_batch(&attr, uattr.user,
465405799638SYonghong Song 				       BPF_MAP_LOOKUP_AND_DELETE_BATCH);
465505799638SYonghong Song 		break;
4656aa2e93b8SBrian Vazquez 	case BPF_MAP_UPDATE_BATCH:
4657af2ac3e1SAlexei Starovoitov 		err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
4658aa2e93b8SBrian Vazquez 		break;
4659aa2e93b8SBrian Vazquez 	case BPF_MAP_DELETE_BATCH:
4660af2ac3e1SAlexei Starovoitov 		err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
4661aa2e93b8SBrian Vazquez 		break;
4662af6eea57SAndrii Nakryiko 	case BPF_LINK_CREATE:
4663af2ac3e1SAlexei Starovoitov 		err = link_create(&attr, uattr);
4664af6eea57SAndrii Nakryiko 		break;
46650c991ebcSAndrii Nakryiko 	case BPF_LINK_UPDATE:
46660c991ebcSAndrii Nakryiko 		err = link_update(&attr);
46670c991ebcSAndrii Nakryiko 		break;
46682d602c8cSAndrii Nakryiko 	case BPF_LINK_GET_FD_BY_ID:
46692d602c8cSAndrii Nakryiko 		err = bpf_link_get_fd_by_id(&attr);
46702d602c8cSAndrii Nakryiko 		break;
46712d602c8cSAndrii Nakryiko 	case BPF_LINK_GET_NEXT_ID:
4672af2ac3e1SAlexei Starovoitov 		err = bpf_obj_get_next_id(&attr, uattr.user,
46732d602c8cSAndrii Nakryiko 					  &link_idr, &link_idr_lock);
46742d602c8cSAndrii Nakryiko 		break;
4675d46edd67SSong Liu 	case BPF_ENABLE_STATS:
4676d46edd67SSong Liu 		err = bpf_enable_stats(&attr);
4677d46edd67SSong Liu 		break;
4678ac51d99bSYonghong Song 	case BPF_ITER_CREATE:
4679ac51d99bSYonghong Song 		err = bpf_iter_create(&attr);
4680ac51d99bSYonghong Song 		break;
468173b11c2aSAndrii Nakryiko 	case BPF_LINK_DETACH:
468273b11c2aSAndrii Nakryiko 		err = link_detach(&attr);
468373b11c2aSAndrii Nakryiko 		break;
4684ef15314aSYiFei Zhu 	case BPF_PROG_BIND_MAP:
4685ef15314aSYiFei Zhu 		err = bpf_prog_bind_map(&attr);
4686ef15314aSYiFei Zhu 		break;
468799c55f7dSAlexei Starovoitov 	default:
468899c55f7dSAlexei Starovoitov 		err = -EINVAL;
468999c55f7dSAlexei Starovoitov 		break;
469099c55f7dSAlexei Starovoitov 	}
469199c55f7dSAlexei Starovoitov 
469299c55f7dSAlexei Starovoitov 	return err;
469399c55f7dSAlexei Starovoitov }
469479a7f8bdSAlexei Starovoitov 
4695af2ac3e1SAlexei Starovoitov SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4696af2ac3e1SAlexei Starovoitov {
4697af2ac3e1SAlexei Starovoitov 	return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
4698af2ac3e1SAlexei Starovoitov }
4699af2ac3e1SAlexei Starovoitov 
470079a7f8bdSAlexei Starovoitov static bool syscall_prog_is_valid_access(int off, int size,
470179a7f8bdSAlexei Starovoitov 					 enum bpf_access_type type,
470279a7f8bdSAlexei Starovoitov 					 const struct bpf_prog *prog,
470379a7f8bdSAlexei Starovoitov 					 struct bpf_insn_access_aux *info)
470479a7f8bdSAlexei Starovoitov {
470579a7f8bdSAlexei Starovoitov 	if (off < 0 || off >= U16_MAX)
470679a7f8bdSAlexei Starovoitov 		return false;
470779a7f8bdSAlexei Starovoitov 	if (off % size != 0)
470879a7f8bdSAlexei Starovoitov 		return false;
470979a7f8bdSAlexei Starovoitov 	return true;
471079a7f8bdSAlexei Starovoitov }
471179a7f8bdSAlexei Starovoitov 
471279a7f8bdSAlexei Starovoitov BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size)
471379a7f8bdSAlexei Starovoitov {
4714af2ac3e1SAlexei Starovoitov 	switch (cmd) {
4715af2ac3e1SAlexei Starovoitov 	case BPF_MAP_CREATE:
4716af2ac3e1SAlexei Starovoitov 	case BPF_MAP_UPDATE_ELEM:
4717af2ac3e1SAlexei Starovoitov 	case BPF_MAP_FREEZE:
4718af2ac3e1SAlexei Starovoitov 	case BPF_PROG_LOAD:
4719c571bd75SAlexei Starovoitov 	case BPF_BTF_LOAD:
4720af2ac3e1SAlexei Starovoitov 		break;
4721af2ac3e1SAlexei Starovoitov 	/* case BPF_PROG_TEST_RUN:
4722af2ac3e1SAlexei Starovoitov 	 * is not part of this list to prevent recursive test_run
4723af2ac3e1SAlexei Starovoitov 	 */
4724af2ac3e1SAlexei Starovoitov 	default:
472579a7f8bdSAlexei Starovoitov 		return -EINVAL;
472679a7f8bdSAlexei Starovoitov 	}
4727af2ac3e1SAlexei Starovoitov 	return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
4728af2ac3e1SAlexei Starovoitov }
472979a7f8bdSAlexei Starovoitov 
47303a2daa72SPu Lehui static const struct bpf_func_proto bpf_sys_bpf_proto = {
473179a7f8bdSAlexei Starovoitov 	.func		= bpf_sys_bpf,
473279a7f8bdSAlexei Starovoitov 	.gpl_only	= false,
473379a7f8bdSAlexei Starovoitov 	.ret_type	= RET_INTEGER,
473479a7f8bdSAlexei Starovoitov 	.arg1_type	= ARG_ANYTHING,
473579a7f8bdSAlexei Starovoitov 	.arg2_type	= ARG_PTR_TO_MEM,
473679a7f8bdSAlexei Starovoitov 	.arg3_type	= ARG_CONST_SIZE,
473779a7f8bdSAlexei Starovoitov };
473879a7f8bdSAlexei Starovoitov 
473979a7f8bdSAlexei Starovoitov const struct bpf_func_proto * __weak
474079a7f8bdSAlexei Starovoitov tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
474179a7f8bdSAlexei Starovoitov {
474279a7f8bdSAlexei Starovoitov 	return bpf_base_func_proto(func_id);
474379a7f8bdSAlexei Starovoitov }
474479a7f8bdSAlexei Starovoitov 
47453abea089SAlexei Starovoitov BPF_CALL_1(bpf_sys_close, u32, fd)
47463abea089SAlexei Starovoitov {
47473abea089SAlexei Starovoitov 	/* When bpf program calls this helper there should not be
47483abea089SAlexei Starovoitov 	 * an fdget() without matching completed fdput().
47493abea089SAlexei Starovoitov 	 * This helper is allowed in the following callchain only:
47503abea089SAlexei Starovoitov 	 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
47513abea089SAlexei Starovoitov 	 */
47523abea089SAlexei Starovoitov 	return close_fd(fd);
47533abea089SAlexei Starovoitov }
47543abea089SAlexei Starovoitov 
47553a2daa72SPu Lehui static const struct bpf_func_proto bpf_sys_close_proto = {
47563abea089SAlexei Starovoitov 	.func		= bpf_sys_close,
47573abea089SAlexei Starovoitov 	.gpl_only	= false,
47583abea089SAlexei Starovoitov 	.ret_type	= RET_INTEGER,
47593abea089SAlexei Starovoitov 	.arg1_type	= ARG_ANYTHING,
47603abea089SAlexei Starovoitov };
47613abea089SAlexei Starovoitov 
476279a7f8bdSAlexei Starovoitov static const struct bpf_func_proto *
476379a7f8bdSAlexei Starovoitov syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
476479a7f8bdSAlexei Starovoitov {
476579a7f8bdSAlexei Starovoitov 	switch (func_id) {
476679a7f8bdSAlexei Starovoitov 	case BPF_FUNC_sys_bpf:
476779a7f8bdSAlexei Starovoitov 		return &bpf_sys_bpf_proto;
47683d78417bSAlexei Starovoitov 	case BPF_FUNC_btf_find_by_name_kind:
47693d78417bSAlexei Starovoitov 		return &bpf_btf_find_by_name_kind_proto;
47703abea089SAlexei Starovoitov 	case BPF_FUNC_sys_close:
47713abea089SAlexei Starovoitov 		return &bpf_sys_close_proto;
477279a7f8bdSAlexei Starovoitov 	default:
477379a7f8bdSAlexei Starovoitov 		return tracing_prog_func_proto(func_id, prog);
477479a7f8bdSAlexei Starovoitov 	}
477579a7f8bdSAlexei Starovoitov }
477679a7f8bdSAlexei Starovoitov 
477779a7f8bdSAlexei Starovoitov const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
477879a7f8bdSAlexei Starovoitov 	.get_func_proto  = syscall_prog_func_proto,
477979a7f8bdSAlexei Starovoitov 	.is_valid_access = syscall_prog_is_valid_access,
478079a7f8bdSAlexei Starovoitov };
478179a7f8bdSAlexei Starovoitov 
478279a7f8bdSAlexei Starovoitov const struct bpf_prog_ops bpf_syscall_prog_ops = {
478379a7f8bdSAlexei Starovoitov 	.test_run = bpf_prog_test_run_syscall,
478479a7f8bdSAlexei Starovoitov };
4785