xref: /linux/kernel/bpf/syscall.c (revision 3539b96e041c06e4317082816d90ec09160aeb11)
199c55f7dSAlexei Starovoitov /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
299c55f7dSAlexei Starovoitov  *
399c55f7dSAlexei Starovoitov  * This program is free software; you can redistribute it and/or
499c55f7dSAlexei Starovoitov  * modify it under the terms of version 2 of the GNU General Public
599c55f7dSAlexei Starovoitov  * License as published by the Free Software Foundation.
699c55f7dSAlexei Starovoitov  *
799c55f7dSAlexei Starovoitov  * This program is distributed in the hope that it will be useful, but
899c55f7dSAlexei Starovoitov  * WITHOUT ANY WARRANTY; without even the implied warranty of
999c55f7dSAlexei Starovoitov  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1099c55f7dSAlexei Starovoitov  * General Public License for more details.
1199c55f7dSAlexei Starovoitov  */
1299c55f7dSAlexei Starovoitov #include <linux/bpf.h>
13a67edbf4SDaniel Borkmann #include <linux/bpf_trace.h>
14f4364dcfSSean Young #include <linux/bpf_lirc.h>
15f56a653cSMartin KaFai Lau #include <linux/btf.h>
1699c55f7dSAlexei Starovoitov #include <linux/syscalls.h>
1799c55f7dSAlexei Starovoitov #include <linux/slab.h>
183f07c014SIngo Molnar #include <linux/sched/signal.h>
19d407bd25SDaniel Borkmann #include <linux/vmalloc.h>
20d407bd25SDaniel Borkmann #include <linux/mmzone.h>
2199c55f7dSAlexei Starovoitov #include <linux/anon_inodes.h>
2241bdc4b4SYonghong Song #include <linux/fdtable.h>
23db20fd2bSAlexei Starovoitov #include <linux/file.h>
2441bdc4b4SYonghong Song #include <linux/fs.h>
2509756af4SAlexei Starovoitov #include <linux/license.h>
2609756af4SAlexei Starovoitov #include <linux/filter.h>
272541517cSAlexei Starovoitov #include <linux/version.h>
28535e7b4bSMickaël Salaün #include <linux/kernel.h>
29dc4bb0e2SMartin KaFai Lau #include <linux/idr.h>
30cb4d2b3fSMartin KaFai Lau #include <linux/cred.h>
31cb4d2b3fSMartin KaFai Lau #include <linux/timekeeping.h>
32cb4d2b3fSMartin KaFai Lau #include <linux/ctype.h>
339ef09e35SMark Rutland #include <linux/nospec.h>
3499c55f7dSAlexei Starovoitov 
3514dc6f04SMartin KaFai Lau #define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY || \
3614dc6f04SMartin KaFai Lau 			   (map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
3714dc6f04SMartin KaFai Lau 			   (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
3814dc6f04SMartin KaFai Lau 			   (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
3914dc6f04SMartin KaFai Lau #define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
4014dc6f04SMartin KaFai Lau #define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_HASH(map))
4114dc6f04SMartin KaFai Lau 
426e71b04aSChenbo Feng #define BPF_OBJ_FLAG_MASK   (BPF_F_RDONLY | BPF_F_WRONLY)
436e71b04aSChenbo Feng 
44b121d1e7SAlexei Starovoitov DEFINE_PER_CPU(int, bpf_prog_active);
45dc4bb0e2SMartin KaFai Lau static DEFINE_IDR(prog_idr);
46dc4bb0e2SMartin KaFai Lau static DEFINE_SPINLOCK(prog_idr_lock);
47f3f1c054SMartin KaFai Lau static DEFINE_IDR(map_idr);
48f3f1c054SMartin KaFai Lau static DEFINE_SPINLOCK(map_idr_lock);
49b121d1e7SAlexei Starovoitov 
501be7f75dSAlexei Starovoitov int sysctl_unprivileged_bpf_disabled __read_mostly;
511be7f75dSAlexei Starovoitov 
5240077e0cSJohannes Berg static const struct bpf_map_ops * const bpf_map_types[] = {
5340077e0cSJohannes Berg #define BPF_PROG_TYPE(_id, _ops)
5440077e0cSJohannes Berg #define BPF_MAP_TYPE(_id, _ops) \
5540077e0cSJohannes Berg 	[_id] = &_ops,
5640077e0cSJohannes Berg #include <linux/bpf_types.h>
5740077e0cSJohannes Berg #undef BPF_PROG_TYPE
5840077e0cSJohannes Berg #undef BPF_MAP_TYPE
5940077e0cSJohannes Berg };
6099c55f7dSAlexei Starovoitov 
61752ba56fSMickaël Salaün /*
62752ba56fSMickaël Salaün  * If we're handed a bigger struct than we know of, ensure all the unknown bits
63752ba56fSMickaël Salaün  * are 0 - i.e. new user-space does not rely on any kernel feature extensions
64752ba56fSMickaël Salaün  * we don't know about yet.
65752ba56fSMickaël Salaün  *
66752ba56fSMickaël Salaün  * There is a ToCToU between this function call and the following
67752ba56fSMickaël Salaün  * copy_from_user() call. However, this is not a concern since this function is
68752ba56fSMickaël Salaün  * meant to be a future-proofing of bits.
69752ba56fSMickaël Salaün  */
70dcab51f1SMartin KaFai Lau int bpf_check_uarg_tail_zero(void __user *uaddr,
7158291a74SMickaël Salaün 			     size_t expected_size,
7258291a74SMickaël Salaün 			     size_t actual_size)
7358291a74SMickaël Salaün {
7458291a74SMickaël Salaün 	unsigned char __user *addr;
7558291a74SMickaël Salaün 	unsigned char __user *end;
7658291a74SMickaël Salaün 	unsigned char val;
7758291a74SMickaël Salaün 	int err;
7858291a74SMickaël Salaün 
79752ba56fSMickaël Salaün 	if (unlikely(actual_size > PAGE_SIZE))	/* silly large */
80752ba56fSMickaël Salaün 		return -E2BIG;
81752ba56fSMickaël Salaün 
8296d4f267SLinus Torvalds 	if (unlikely(!access_ok(uaddr, actual_size)))
83752ba56fSMickaël Salaün 		return -EFAULT;
84752ba56fSMickaël Salaün 
8558291a74SMickaël Salaün 	if (actual_size <= expected_size)
8658291a74SMickaël Salaün 		return 0;
8758291a74SMickaël Salaün 
8858291a74SMickaël Salaün 	addr = uaddr + expected_size;
8958291a74SMickaël Salaün 	end  = uaddr + actual_size;
9058291a74SMickaël Salaün 
9158291a74SMickaël Salaün 	for (; addr < end; addr++) {
9258291a74SMickaël Salaün 		err = get_user(val, addr);
9358291a74SMickaël Salaün 		if (err)
9458291a74SMickaël Salaün 			return err;
9558291a74SMickaël Salaün 		if (val)
9658291a74SMickaël Salaün 			return -E2BIG;
9758291a74SMickaël Salaün 	}
9858291a74SMickaël Salaün 
9958291a74SMickaël Salaün 	return 0;
10058291a74SMickaël Salaün }
10158291a74SMickaël Salaün 
102a3884572SJakub Kicinski const struct bpf_map_ops bpf_map_offload_ops = {
103a3884572SJakub Kicinski 	.map_alloc = bpf_map_offload_map_alloc,
104a3884572SJakub Kicinski 	.map_free = bpf_map_offload_map_free,
105e8d2bec0SDaniel Borkmann 	.map_check_btf = map_check_no_btf,
106a3884572SJakub Kicinski };
107a3884572SJakub Kicinski 
10899c55f7dSAlexei Starovoitov static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
10999c55f7dSAlexei Starovoitov {
1101110f3a9SJakub Kicinski 	const struct bpf_map_ops *ops;
1119ef09e35SMark Rutland 	u32 type = attr->map_type;
11299c55f7dSAlexei Starovoitov 	struct bpf_map *map;
1131110f3a9SJakub Kicinski 	int err;
11499c55f7dSAlexei Starovoitov 
1159ef09e35SMark Rutland 	if (type >= ARRAY_SIZE(bpf_map_types))
1161110f3a9SJakub Kicinski 		return ERR_PTR(-EINVAL);
1179ef09e35SMark Rutland 	type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
1189ef09e35SMark Rutland 	ops = bpf_map_types[type];
1191110f3a9SJakub Kicinski 	if (!ops)
12040077e0cSJohannes Berg 		return ERR_PTR(-EINVAL);
12140077e0cSJohannes Berg 
1221110f3a9SJakub Kicinski 	if (ops->map_alloc_check) {
1231110f3a9SJakub Kicinski 		err = ops->map_alloc_check(attr);
1241110f3a9SJakub Kicinski 		if (err)
1251110f3a9SJakub Kicinski 			return ERR_PTR(err);
1261110f3a9SJakub Kicinski 	}
127a3884572SJakub Kicinski 	if (attr->map_ifindex)
128a3884572SJakub Kicinski 		ops = &bpf_map_offload_ops;
1291110f3a9SJakub Kicinski 	map = ops->map_alloc(attr);
13099c55f7dSAlexei Starovoitov 	if (IS_ERR(map))
13199c55f7dSAlexei Starovoitov 		return map;
1321110f3a9SJakub Kicinski 	map->ops = ops;
1339ef09e35SMark Rutland 	map->map_type = type;
13499c55f7dSAlexei Starovoitov 	return map;
13599c55f7dSAlexei Starovoitov }
13699c55f7dSAlexei Starovoitov 
13796eabe7aSMartin KaFai Lau void *bpf_map_area_alloc(size_t size, int numa_node)
138d407bd25SDaniel Borkmann {
139f01a7dbeSMartynas Pumputis 	/* We really just want to fail instead of triggering OOM killer
140f01a7dbeSMartynas Pumputis 	 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
141f01a7dbeSMartynas Pumputis 	 * which is used for lower order allocation requests.
142f01a7dbeSMartynas Pumputis 	 *
143f01a7dbeSMartynas Pumputis 	 * It has been observed that higher order allocation requests done by
144f01a7dbeSMartynas Pumputis 	 * vmalloc with __GFP_NORETRY being set might fail due to not trying
145f01a7dbeSMartynas Pumputis 	 * to reclaim memory from the page cache, thus we set
146f01a7dbeSMartynas Pumputis 	 * __GFP_RETRY_MAYFAIL to avoid such situations.
147d407bd25SDaniel Borkmann 	 */
148f01a7dbeSMartynas Pumputis 
149f01a7dbeSMartynas Pumputis 	const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
150d407bd25SDaniel Borkmann 	void *area;
151d407bd25SDaniel Borkmann 
152d407bd25SDaniel Borkmann 	if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
153f01a7dbeSMartynas Pumputis 		area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
154f01a7dbeSMartynas Pumputis 				    numa_node);
155d407bd25SDaniel Borkmann 		if (area != NULL)
156d407bd25SDaniel Borkmann 			return area;
157d407bd25SDaniel Borkmann 	}
158d407bd25SDaniel Borkmann 
159f01a7dbeSMartynas Pumputis 	return __vmalloc_node_flags_caller(size, numa_node,
160f01a7dbeSMartynas Pumputis 					   GFP_KERNEL | __GFP_RETRY_MAYFAIL |
161f01a7dbeSMartynas Pumputis 					   flags, __builtin_return_address(0));
162d407bd25SDaniel Borkmann }
163d407bd25SDaniel Borkmann 
164d407bd25SDaniel Borkmann void bpf_map_area_free(void *area)
165d407bd25SDaniel Borkmann {
166d407bd25SDaniel Borkmann 	kvfree(area);
167d407bd25SDaniel Borkmann }
168d407bd25SDaniel Borkmann 
169be70bcd5SDaniel Borkmann static u32 bpf_map_flags_retain_permanent(u32 flags)
170be70bcd5SDaniel Borkmann {
171be70bcd5SDaniel Borkmann 	/* Some map creation flags are not tied to the map object but
172be70bcd5SDaniel Borkmann 	 * rather to the map fd instead, so they have no meaning upon
173be70bcd5SDaniel Borkmann 	 * map object inspection since multiple file descriptors with
174be70bcd5SDaniel Borkmann 	 * different (access) properties can exist here. Thus, given
175be70bcd5SDaniel Borkmann 	 * this has zero meaning for the map itself, lets clear these
176be70bcd5SDaniel Borkmann 	 * from here.
177be70bcd5SDaniel Borkmann 	 */
178be70bcd5SDaniel Borkmann 	return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
179be70bcd5SDaniel Borkmann }
180be70bcd5SDaniel Borkmann 
181bd475643SJakub Kicinski void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
182bd475643SJakub Kicinski {
183bd475643SJakub Kicinski 	map->map_type = attr->map_type;
184bd475643SJakub Kicinski 	map->key_size = attr->key_size;
185bd475643SJakub Kicinski 	map->value_size = attr->value_size;
186bd475643SJakub Kicinski 	map->max_entries = attr->max_entries;
187be70bcd5SDaniel Borkmann 	map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
188bd475643SJakub Kicinski 	map->numa_node = bpf_map_attr_numa_node(attr);
189bd475643SJakub Kicinski }
190bd475643SJakub Kicinski 
1916c905981SAlexei Starovoitov int bpf_map_precharge_memlock(u32 pages)
1926c905981SAlexei Starovoitov {
1936c905981SAlexei Starovoitov 	struct user_struct *user = get_current_user();
1946c905981SAlexei Starovoitov 	unsigned long memlock_limit, cur;
1956c905981SAlexei Starovoitov 
1966c905981SAlexei Starovoitov 	memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1976c905981SAlexei Starovoitov 	cur = atomic_long_read(&user->locked_vm);
1986c905981SAlexei Starovoitov 	free_uid(user);
1996c905981SAlexei Starovoitov 	if (cur + pages > memlock_limit)
2006c905981SAlexei Starovoitov 		return -EPERM;
2016c905981SAlexei Starovoitov 	return 0;
2026c905981SAlexei Starovoitov }
2036c905981SAlexei Starovoitov 
2040a4c58f5SRoman Gushchin static int bpf_charge_memlock(struct user_struct *user, u32 pages)
205aaac3ba9SAlexei Starovoitov {
2060a4c58f5SRoman Gushchin 	unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
207aaac3ba9SAlexei Starovoitov 
2080a4c58f5SRoman Gushchin 	if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
2090a4c58f5SRoman Gushchin 		atomic_long_sub(pages, &user->locked_vm);
210aaac3ba9SAlexei Starovoitov 		return -EPERM;
211aaac3ba9SAlexei Starovoitov 	}
212aaac3ba9SAlexei Starovoitov 	return 0;
213aaac3ba9SAlexei Starovoitov }
214aaac3ba9SAlexei Starovoitov 
2150a4c58f5SRoman Gushchin static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
2160a4c58f5SRoman Gushchin {
2170a4c58f5SRoman Gushchin 	atomic_long_sub(pages, &user->locked_vm);
2180a4c58f5SRoman Gushchin }
2190a4c58f5SRoman Gushchin 
2200a4c58f5SRoman Gushchin static int bpf_map_init_memlock(struct bpf_map *map)
2210a4c58f5SRoman Gushchin {
2220a4c58f5SRoman Gushchin 	struct user_struct *user = get_current_user();
2230a4c58f5SRoman Gushchin 	int ret;
2240a4c58f5SRoman Gushchin 
225*3539b96eSRoman Gushchin 	ret = bpf_charge_memlock(user, map->memory.pages);
2260a4c58f5SRoman Gushchin 	if (ret) {
2270a4c58f5SRoman Gushchin 		free_uid(user);
2280a4c58f5SRoman Gushchin 		return ret;
2290a4c58f5SRoman Gushchin 	}
230*3539b96eSRoman Gushchin 	map->memory.user = user;
2310a4c58f5SRoman Gushchin 	return ret;
2320a4c58f5SRoman Gushchin }
2330a4c58f5SRoman Gushchin 
2340a4c58f5SRoman Gushchin static void bpf_map_release_memlock(struct bpf_map *map)
235aaac3ba9SAlexei Starovoitov {
236*3539b96eSRoman Gushchin 	struct user_struct *user = map->memory.user;
237*3539b96eSRoman Gushchin 
238*3539b96eSRoman Gushchin 	bpf_uncharge_memlock(user, map->memory.pages);
239aaac3ba9SAlexei Starovoitov 	free_uid(user);
240aaac3ba9SAlexei Starovoitov }
241aaac3ba9SAlexei Starovoitov 
2420a4c58f5SRoman Gushchin int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
2430a4c58f5SRoman Gushchin {
2440a4c58f5SRoman Gushchin 	int ret;
2450a4c58f5SRoman Gushchin 
246*3539b96eSRoman Gushchin 	ret = bpf_charge_memlock(map->memory.user, pages);
2470a4c58f5SRoman Gushchin 	if (ret)
2480a4c58f5SRoman Gushchin 		return ret;
249*3539b96eSRoman Gushchin 	map->memory.pages += pages;
2500a4c58f5SRoman Gushchin 	return ret;
2510a4c58f5SRoman Gushchin }
2520a4c58f5SRoman Gushchin 
2530a4c58f5SRoman Gushchin void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
2540a4c58f5SRoman Gushchin {
255*3539b96eSRoman Gushchin 	bpf_uncharge_memlock(map->memory.user, pages);
256*3539b96eSRoman Gushchin 	map->memory.pages -= pages;
2570a4c58f5SRoman Gushchin }
2580a4c58f5SRoman Gushchin 
259f3f1c054SMartin KaFai Lau static int bpf_map_alloc_id(struct bpf_map *map)
260f3f1c054SMartin KaFai Lau {
261f3f1c054SMartin KaFai Lau 	int id;
262f3f1c054SMartin KaFai Lau 
263b76354cdSShaohua Li 	idr_preload(GFP_KERNEL);
264f3f1c054SMartin KaFai Lau 	spin_lock_bh(&map_idr_lock);
265f3f1c054SMartin KaFai Lau 	id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
266f3f1c054SMartin KaFai Lau 	if (id > 0)
267f3f1c054SMartin KaFai Lau 		map->id = id;
268f3f1c054SMartin KaFai Lau 	spin_unlock_bh(&map_idr_lock);
269b76354cdSShaohua Li 	idr_preload_end();
270f3f1c054SMartin KaFai Lau 
271f3f1c054SMartin KaFai Lau 	if (WARN_ON_ONCE(!id))
272f3f1c054SMartin KaFai Lau 		return -ENOSPC;
273f3f1c054SMartin KaFai Lau 
274f3f1c054SMartin KaFai Lau 	return id > 0 ? 0 : id;
275f3f1c054SMartin KaFai Lau }
276f3f1c054SMartin KaFai Lau 
277a3884572SJakub Kicinski void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
278f3f1c054SMartin KaFai Lau {
279930651a7SEric Dumazet 	unsigned long flags;
280930651a7SEric Dumazet 
281a3884572SJakub Kicinski 	/* Offloaded maps are removed from the IDR store when their device
282a3884572SJakub Kicinski 	 * disappears - even if someone holds an fd to them they are unusable,
283a3884572SJakub Kicinski 	 * the memory is gone, all ops will fail; they are simply waiting for
284a3884572SJakub Kicinski 	 * refcnt to drop to be freed.
285a3884572SJakub Kicinski 	 */
286a3884572SJakub Kicinski 	if (!map->id)
287a3884572SJakub Kicinski 		return;
288a3884572SJakub Kicinski 
289bd5f5f4eSMartin KaFai Lau 	if (do_idr_lock)
290930651a7SEric Dumazet 		spin_lock_irqsave(&map_idr_lock, flags);
291bd5f5f4eSMartin KaFai Lau 	else
292bd5f5f4eSMartin KaFai Lau 		__acquire(&map_idr_lock);
293bd5f5f4eSMartin KaFai Lau 
294f3f1c054SMartin KaFai Lau 	idr_remove(&map_idr, map->id);
295a3884572SJakub Kicinski 	map->id = 0;
296bd5f5f4eSMartin KaFai Lau 
297bd5f5f4eSMartin KaFai Lau 	if (do_idr_lock)
298930651a7SEric Dumazet 		spin_unlock_irqrestore(&map_idr_lock, flags);
299bd5f5f4eSMartin KaFai Lau 	else
300bd5f5f4eSMartin KaFai Lau 		__release(&map_idr_lock);
301f3f1c054SMartin KaFai Lau }
302f3f1c054SMartin KaFai Lau 
30399c55f7dSAlexei Starovoitov /* called from workqueue */
30499c55f7dSAlexei Starovoitov static void bpf_map_free_deferred(struct work_struct *work)
30599c55f7dSAlexei Starovoitov {
30699c55f7dSAlexei Starovoitov 	struct bpf_map *map = container_of(work, struct bpf_map, work);
30799c55f7dSAlexei Starovoitov 
3080a4c58f5SRoman Gushchin 	bpf_map_release_memlock(map);
309afdb09c7SChenbo Feng 	security_bpf_map_free(map);
31099c55f7dSAlexei Starovoitov 	/* implementation dependent freeing */
31199c55f7dSAlexei Starovoitov 	map->ops->map_free(map);
31299c55f7dSAlexei Starovoitov }
31399c55f7dSAlexei Starovoitov 
314c9da161cSDaniel Borkmann static void bpf_map_put_uref(struct bpf_map *map)
315c9da161cSDaniel Borkmann {
316c9da161cSDaniel Borkmann 	if (atomic_dec_and_test(&map->usercnt)) {
317ba6b8de4SJohn Fastabend 		if (map->ops->map_release_uref)
318ba6b8de4SJohn Fastabend 			map->ops->map_release_uref(map);
319c9da161cSDaniel Borkmann 	}
320c9da161cSDaniel Borkmann }
321c9da161cSDaniel Borkmann 
32299c55f7dSAlexei Starovoitov /* decrement map refcnt and schedule it for freeing via workqueue
32399c55f7dSAlexei Starovoitov  * (unrelying map implementation ops->map_free() might sleep)
32499c55f7dSAlexei Starovoitov  */
325bd5f5f4eSMartin KaFai Lau static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
32699c55f7dSAlexei Starovoitov {
32799c55f7dSAlexei Starovoitov 	if (atomic_dec_and_test(&map->refcnt)) {
32834ad5580SMartin KaFai Lau 		/* bpf_map_free_id() must be called first */
329bd5f5f4eSMartin KaFai Lau 		bpf_map_free_id(map, do_idr_lock);
33078958fcaSMartin KaFai Lau 		btf_put(map->btf);
33199c55f7dSAlexei Starovoitov 		INIT_WORK(&map->work, bpf_map_free_deferred);
33299c55f7dSAlexei Starovoitov 		schedule_work(&map->work);
33399c55f7dSAlexei Starovoitov 	}
33499c55f7dSAlexei Starovoitov }
33599c55f7dSAlexei Starovoitov 
336bd5f5f4eSMartin KaFai Lau void bpf_map_put(struct bpf_map *map)
337bd5f5f4eSMartin KaFai Lau {
338bd5f5f4eSMartin KaFai Lau 	__bpf_map_put(map, true);
339bd5f5f4eSMartin KaFai Lau }
340630a4d38SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_map_put);
341bd5f5f4eSMartin KaFai Lau 
342c9da161cSDaniel Borkmann void bpf_map_put_with_uref(struct bpf_map *map)
343c9da161cSDaniel Borkmann {
344c9da161cSDaniel Borkmann 	bpf_map_put_uref(map);
345c9da161cSDaniel Borkmann 	bpf_map_put(map);
346c9da161cSDaniel Borkmann }
347c9da161cSDaniel Borkmann 
34899c55f7dSAlexei Starovoitov static int bpf_map_release(struct inode *inode, struct file *filp)
34999c55f7dSAlexei Starovoitov {
35061d1b6a4SDaniel Borkmann 	struct bpf_map *map = filp->private_data;
35161d1b6a4SDaniel Borkmann 
35261d1b6a4SDaniel Borkmann 	if (map->ops->map_release)
35361d1b6a4SDaniel Borkmann 		map->ops->map_release(map, filp);
35461d1b6a4SDaniel Borkmann 
35561d1b6a4SDaniel Borkmann 	bpf_map_put_with_uref(map);
35699c55f7dSAlexei Starovoitov 	return 0;
35799c55f7dSAlexei Starovoitov }
35899c55f7dSAlexei Starovoitov 
35987df15deSDaniel Borkmann static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
36087df15deSDaniel Borkmann {
36187df15deSDaniel Borkmann 	fmode_t mode = f.file->f_mode;
36287df15deSDaniel Borkmann 
36387df15deSDaniel Borkmann 	/* Our file permissions may have been overridden by global
36487df15deSDaniel Borkmann 	 * map permissions facing syscall side.
36587df15deSDaniel Borkmann 	 */
36687df15deSDaniel Borkmann 	if (READ_ONCE(map->frozen))
36787df15deSDaniel Borkmann 		mode &= ~FMODE_CAN_WRITE;
36887df15deSDaniel Borkmann 	return mode;
36987df15deSDaniel Borkmann }
37087df15deSDaniel Borkmann 
371f99bf205SDaniel Borkmann #ifdef CONFIG_PROC_FS
372f99bf205SDaniel Borkmann static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
373f99bf205SDaniel Borkmann {
374f99bf205SDaniel Borkmann 	const struct bpf_map *map = filp->private_data;
37521116b70SDaniel Borkmann 	const struct bpf_array *array;
37621116b70SDaniel Borkmann 	u32 owner_prog_type = 0;
3779780c0abSDaniel Borkmann 	u32 owner_jited = 0;
37821116b70SDaniel Borkmann 
37921116b70SDaniel Borkmann 	if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
38021116b70SDaniel Borkmann 		array = container_of(map, struct bpf_array, map);
38121116b70SDaniel Borkmann 		owner_prog_type = array->owner_prog_type;
3829780c0abSDaniel Borkmann 		owner_jited = array->owner_jited;
38321116b70SDaniel Borkmann 	}
384f99bf205SDaniel Borkmann 
385f99bf205SDaniel Borkmann 	seq_printf(m,
386f99bf205SDaniel Borkmann 		   "map_type:\t%u\n"
387f99bf205SDaniel Borkmann 		   "key_size:\t%u\n"
388f99bf205SDaniel Borkmann 		   "value_size:\t%u\n"
389322cea2fSDaniel Borkmann 		   "max_entries:\t%u\n"
39021116b70SDaniel Borkmann 		   "map_flags:\t%#x\n"
3914316b409SDaniel Borkmann 		   "memlock:\t%llu\n"
39287df15deSDaniel Borkmann 		   "map_id:\t%u\n"
39387df15deSDaniel Borkmann 		   "frozen:\t%u\n",
394f99bf205SDaniel Borkmann 		   map->map_type,
395f99bf205SDaniel Borkmann 		   map->key_size,
396f99bf205SDaniel Borkmann 		   map->value_size,
397322cea2fSDaniel Borkmann 		   map->max_entries,
39821116b70SDaniel Borkmann 		   map->map_flags,
399*3539b96eSRoman Gushchin 		   map->memory.pages * 1ULL << PAGE_SHIFT,
40087df15deSDaniel Borkmann 		   map->id,
40187df15deSDaniel Borkmann 		   READ_ONCE(map->frozen));
40221116b70SDaniel Borkmann 
4039780c0abSDaniel Borkmann 	if (owner_prog_type) {
40421116b70SDaniel Borkmann 		seq_printf(m, "owner_prog_type:\t%u\n",
40521116b70SDaniel Borkmann 			   owner_prog_type);
4069780c0abSDaniel Borkmann 		seq_printf(m, "owner_jited:\t%u\n",
4079780c0abSDaniel Borkmann 			   owner_jited);
4089780c0abSDaniel Borkmann 	}
409f99bf205SDaniel Borkmann }
410f99bf205SDaniel Borkmann #endif
411f99bf205SDaniel Borkmann 
4126e71b04aSChenbo Feng static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
4136e71b04aSChenbo Feng 			      loff_t *ppos)
4146e71b04aSChenbo Feng {
4156e71b04aSChenbo Feng 	/* We need this handler such that alloc_file() enables
4166e71b04aSChenbo Feng 	 * f_mode with FMODE_CAN_READ.
4176e71b04aSChenbo Feng 	 */
4186e71b04aSChenbo Feng 	return -EINVAL;
4196e71b04aSChenbo Feng }
4206e71b04aSChenbo Feng 
4216e71b04aSChenbo Feng static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
4226e71b04aSChenbo Feng 			       size_t siz, loff_t *ppos)
4236e71b04aSChenbo Feng {
4246e71b04aSChenbo Feng 	/* We need this handler such that alloc_file() enables
4256e71b04aSChenbo Feng 	 * f_mode with FMODE_CAN_WRITE.
4266e71b04aSChenbo Feng 	 */
4276e71b04aSChenbo Feng 	return -EINVAL;
4286e71b04aSChenbo Feng }
4296e71b04aSChenbo Feng 
430f66e448cSChenbo Feng const struct file_operations bpf_map_fops = {
431f99bf205SDaniel Borkmann #ifdef CONFIG_PROC_FS
432f99bf205SDaniel Borkmann 	.show_fdinfo	= bpf_map_show_fdinfo,
433f99bf205SDaniel Borkmann #endif
43499c55f7dSAlexei Starovoitov 	.release	= bpf_map_release,
4356e71b04aSChenbo Feng 	.read		= bpf_dummy_read,
4366e71b04aSChenbo Feng 	.write		= bpf_dummy_write,
43799c55f7dSAlexei Starovoitov };
43899c55f7dSAlexei Starovoitov 
4396e71b04aSChenbo Feng int bpf_map_new_fd(struct bpf_map *map, int flags)
440aa79781bSDaniel Borkmann {
441afdb09c7SChenbo Feng 	int ret;
442afdb09c7SChenbo Feng 
443afdb09c7SChenbo Feng 	ret = security_bpf_map(map, OPEN_FMODE(flags));
444afdb09c7SChenbo Feng 	if (ret < 0)
445afdb09c7SChenbo Feng 		return ret;
446afdb09c7SChenbo Feng 
447aa79781bSDaniel Borkmann 	return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
4486e71b04aSChenbo Feng 				flags | O_CLOEXEC);
4496e71b04aSChenbo Feng }
4506e71b04aSChenbo Feng 
4516e71b04aSChenbo Feng int bpf_get_file_flag(int flags)
4526e71b04aSChenbo Feng {
4536e71b04aSChenbo Feng 	if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
4546e71b04aSChenbo Feng 		return -EINVAL;
4556e71b04aSChenbo Feng 	if (flags & BPF_F_RDONLY)
4566e71b04aSChenbo Feng 		return O_RDONLY;
4576e71b04aSChenbo Feng 	if (flags & BPF_F_WRONLY)
4586e71b04aSChenbo Feng 		return O_WRONLY;
4596e71b04aSChenbo Feng 	return O_RDWR;
460aa79781bSDaniel Borkmann }
461aa79781bSDaniel Borkmann 
46299c55f7dSAlexei Starovoitov /* helper macro to check that unused fields 'union bpf_attr' are zero */
46399c55f7dSAlexei Starovoitov #define CHECK_ATTR(CMD) \
46499c55f7dSAlexei Starovoitov 	memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
46599c55f7dSAlexei Starovoitov 		   sizeof(attr->CMD##_LAST_FIELD), 0, \
46699c55f7dSAlexei Starovoitov 		   sizeof(*attr) - \
46799c55f7dSAlexei Starovoitov 		   offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
46899c55f7dSAlexei Starovoitov 		   sizeof(attr->CMD##_LAST_FIELD)) != NULL
46999c55f7dSAlexei Starovoitov 
470cb4d2b3fSMartin KaFai Lau /* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
471cb4d2b3fSMartin KaFai Lau  * Return 0 on success and < 0 on error.
472cb4d2b3fSMartin KaFai Lau  */
473cb4d2b3fSMartin KaFai Lau static int bpf_obj_name_cpy(char *dst, const char *src)
474cb4d2b3fSMartin KaFai Lau {
475cb4d2b3fSMartin KaFai Lau 	const char *end = src + BPF_OBJ_NAME_LEN;
476cb4d2b3fSMartin KaFai Lau 
477473d9734SMartin KaFai Lau 	memset(dst, 0, BPF_OBJ_NAME_LEN);
4783e0ddc4fSDaniel Borkmann 	/* Copy all isalnum(), '_' and '.' chars. */
479cb4d2b3fSMartin KaFai Lau 	while (src < end && *src) {
4803e0ddc4fSDaniel Borkmann 		if (!isalnum(*src) &&
4813e0ddc4fSDaniel Borkmann 		    *src != '_' && *src != '.')
482cb4d2b3fSMartin KaFai Lau 			return -EINVAL;
483cb4d2b3fSMartin KaFai Lau 		*dst++ = *src++;
484cb4d2b3fSMartin KaFai Lau 	}
485cb4d2b3fSMartin KaFai Lau 
486cb4d2b3fSMartin KaFai Lau 	/* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
487cb4d2b3fSMartin KaFai Lau 	if (src == end)
488cb4d2b3fSMartin KaFai Lau 		return -EINVAL;
489cb4d2b3fSMartin KaFai Lau 
490cb4d2b3fSMartin KaFai Lau 	return 0;
491cb4d2b3fSMartin KaFai Lau }
492cb4d2b3fSMartin KaFai Lau 
493e8d2bec0SDaniel Borkmann int map_check_no_btf(const struct bpf_map *map,
4941b2b234bSRoman Gushchin 		     const struct btf *btf,
495e8d2bec0SDaniel Borkmann 		     const struct btf_type *key_type,
496e8d2bec0SDaniel Borkmann 		     const struct btf_type *value_type)
497e8d2bec0SDaniel Borkmann {
498e8d2bec0SDaniel Borkmann 	return -ENOTSUPP;
499e8d2bec0SDaniel Borkmann }
500e8d2bec0SDaniel Borkmann 
501d83525caSAlexei Starovoitov static int map_check_btf(struct bpf_map *map, const struct btf *btf,
502e8d2bec0SDaniel Borkmann 			 u32 btf_key_id, u32 btf_value_id)
503e8d2bec0SDaniel Borkmann {
504e8d2bec0SDaniel Borkmann 	const struct btf_type *key_type, *value_type;
505e8d2bec0SDaniel Borkmann 	u32 key_size, value_size;
506e8d2bec0SDaniel Borkmann 	int ret = 0;
507e8d2bec0SDaniel Borkmann 
5082824ecb7SDaniel Borkmann 	/* Some maps allow key to be unspecified. */
5092824ecb7SDaniel Borkmann 	if (btf_key_id) {
510e8d2bec0SDaniel Borkmann 		key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
511e8d2bec0SDaniel Borkmann 		if (!key_type || key_size != map->key_size)
512e8d2bec0SDaniel Borkmann 			return -EINVAL;
5132824ecb7SDaniel Borkmann 	} else {
5142824ecb7SDaniel Borkmann 		key_type = btf_type_by_id(btf, 0);
5152824ecb7SDaniel Borkmann 		if (!map->ops->map_check_btf)
5162824ecb7SDaniel Borkmann 			return -EINVAL;
5172824ecb7SDaniel Borkmann 	}
518e8d2bec0SDaniel Borkmann 
519e8d2bec0SDaniel Borkmann 	value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
520e8d2bec0SDaniel Borkmann 	if (!value_type || value_size != map->value_size)
521e8d2bec0SDaniel Borkmann 		return -EINVAL;
522e8d2bec0SDaniel Borkmann 
523d83525caSAlexei Starovoitov 	map->spin_lock_off = btf_find_spin_lock(btf, value_type);
524d83525caSAlexei Starovoitov 
525d83525caSAlexei Starovoitov 	if (map_value_has_spin_lock(map)) {
526591fe988SDaniel Borkmann 		if (map->map_flags & BPF_F_RDONLY_PROG)
527591fe988SDaniel Borkmann 			return -EACCES;
528d83525caSAlexei Starovoitov 		if (map->map_type != BPF_MAP_TYPE_HASH &&
529e16d2f1aSAlexei Starovoitov 		    map->map_type != BPF_MAP_TYPE_ARRAY &&
5306ac99e8fSMartin KaFai Lau 		    map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
5316ac99e8fSMartin KaFai Lau 		    map->map_type != BPF_MAP_TYPE_SK_STORAGE)
532d83525caSAlexei Starovoitov 			return -ENOTSUPP;
533d83525caSAlexei Starovoitov 		if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
534d83525caSAlexei Starovoitov 		    map->value_size) {
535d83525caSAlexei Starovoitov 			WARN_ONCE(1,
536d83525caSAlexei Starovoitov 				  "verifier bug spin_lock_off %d value_size %d\n",
537d83525caSAlexei Starovoitov 				  map->spin_lock_off, map->value_size);
538d83525caSAlexei Starovoitov 			return -EFAULT;
539d83525caSAlexei Starovoitov 		}
540d83525caSAlexei Starovoitov 	}
541d83525caSAlexei Starovoitov 
542e8d2bec0SDaniel Borkmann 	if (map->ops->map_check_btf)
5431b2b234bSRoman Gushchin 		ret = map->ops->map_check_btf(map, btf, key_type, value_type);
544e8d2bec0SDaniel Borkmann 
545e8d2bec0SDaniel Borkmann 	return ret;
546e8d2bec0SDaniel Borkmann }
547e8d2bec0SDaniel Borkmann 
5489b2cf328SMartin KaFai Lau #define BPF_MAP_CREATE_LAST_FIELD btf_value_type_id
54999c55f7dSAlexei Starovoitov /* called via syscall */
55099c55f7dSAlexei Starovoitov static int map_create(union bpf_attr *attr)
55199c55f7dSAlexei Starovoitov {
55296eabe7aSMartin KaFai Lau 	int numa_node = bpf_map_attr_numa_node(attr);
55399c55f7dSAlexei Starovoitov 	struct bpf_map *map;
5546e71b04aSChenbo Feng 	int f_flags;
55599c55f7dSAlexei Starovoitov 	int err;
55699c55f7dSAlexei Starovoitov 
55799c55f7dSAlexei Starovoitov 	err = CHECK_ATTR(BPF_MAP_CREATE);
55899c55f7dSAlexei Starovoitov 	if (err)
55999c55f7dSAlexei Starovoitov 		return -EINVAL;
56099c55f7dSAlexei Starovoitov 
5616e71b04aSChenbo Feng 	f_flags = bpf_get_file_flag(attr->map_flags);
5626e71b04aSChenbo Feng 	if (f_flags < 0)
5636e71b04aSChenbo Feng 		return f_flags;
5646e71b04aSChenbo Feng 
56596eabe7aSMartin KaFai Lau 	if (numa_node != NUMA_NO_NODE &&
56696e5ae4eSEric Dumazet 	    ((unsigned int)numa_node >= nr_node_ids ||
56796e5ae4eSEric Dumazet 	     !node_online(numa_node)))
56896eabe7aSMartin KaFai Lau 		return -EINVAL;
56996eabe7aSMartin KaFai Lau 
57099c55f7dSAlexei Starovoitov 	/* find map type and init map: hashtable vs rbtree vs bloom vs ... */
57199c55f7dSAlexei Starovoitov 	map = find_and_alloc_map(attr);
57299c55f7dSAlexei Starovoitov 	if (IS_ERR(map))
57399c55f7dSAlexei Starovoitov 		return PTR_ERR(map);
57499c55f7dSAlexei Starovoitov 
575ad5b177bSMartin KaFai Lau 	err = bpf_obj_name_cpy(map->name, attr->map_name);
576ad5b177bSMartin KaFai Lau 	if (err)
577ad5b177bSMartin KaFai Lau 		goto free_map_nouncharge;
578ad5b177bSMartin KaFai Lau 
57999c55f7dSAlexei Starovoitov 	atomic_set(&map->refcnt, 1);
580c9da161cSDaniel Borkmann 	atomic_set(&map->usercnt, 1);
58199c55f7dSAlexei Starovoitov 
582e8d2bec0SDaniel Borkmann 	if (attr->btf_key_type_id || attr->btf_value_type_id) {
583a26ca7c9SMartin KaFai Lau 		struct btf *btf;
584a26ca7c9SMartin KaFai Lau 
5852824ecb7SDaniel Borkmann 		if (!attr->btf_value_type_id) {
586a26ca7c9SMartin KaFai Lau 			err = -EINVAL;
587a26ca7c9SMartin KaFai Lau 			goto free_map_nouncharge;
588a26ca7c9SMartin KaFai Lau 		}
589a26ca7c9SMartin KaFai Lau 
590a26ca7c9SMartin KaFai Lau 		btf = btf_get_by_fd(attr->btf_fd);
591a26ca7c9SMartin KaFai Lau 		if (IS_ERR(btf)) {
592a26ca7c9SMartin KaFai Lau 			err = PTR_ERR(btf);
593a26ca7c9SMartin KaFai Lau 			goto free_map_nouncharge;
594a26ca7c9SMartin KaFai Lau 		}
595a26ca7c9SMartin KaFai Lau 
596e8d2bec0SDaniel Borkmann 		err = map_check_btf(map, btf, attr->btf_key_type_id,
5979b2cf328SMartin KaFai Lau 				    attr->btf_value_type_id);
598a26ca7c9SMartin KaFai Lau 		if (err) {
599a26ca7c9SMartin KaFai Lau 			btf_put(btf);
600a26ca7c9SMartin KaFai Lau 			goto free_map_nouncharge;
601a26ca7c9SMartin KaFai Lau 		}
602a26ca7c9SMartin KaFai Lau 
603a26ca7c9SMartin KaFai Lau 		map->btf = btf;
6049b2cf328SMartin KaFai Lau 		map->btf_key_type_id = attr->btf_key_type_id;
6059b2cf328SMartin KaFai Lau 		map->btf_value_type_id = attr->btf_value_type_id;
606d83525caSAlexei Starovoitov 	} else {
607d83525caSAlexei Starovoitov 		map->spin_lock_off = -EINVAL;
608a26ca7c9SMartin KaFai Lau 	}
609a26ca7c9SMartin KaFai Lau 
610afdb09c7SChenbo Feng 	err = security_bpf_map_alloc(map);
611aaac3ba9SAlexei Starovoitov 	if (err)
61220b2b24fSDaniel Borkmann 		goto free_map_nouncharge;
613aaac3ba9SAlexei Starovoitov 
6140a4c58f5SRoman Gushchin 	err = bpf_map_init_memlock(map);
615afdb09c7SChenbo Feng 	if (err)
616afdb09c7SChenbo Feng 		goto free_map_sec;
617afdb09c7SChenbo Feng 
618f3f1c054SMartin KaFai Lau 	err = bpf_map_alloc_id(map);
619f3f1c054SMartin KaFai Lau 	if (err)
620f3f1c054SMartin KaFai Lau 		goto free_map;
621f3f1c054SMartin KaFai Lau 
6226e71b04aSChenbo Feng 	err = bpf_map_new_fd(map, f_flags);
623bd5f5f4eSMartin KaFai Lau 	if (err < 0) {
624bd5f5f4eSMartin KaFai Lau 		/* failed to allocate fd.
625352d20d6SPeng Sun 		 * bpf_map_put_with_uref() is needed because the above
626bd5f5f4eSMartin KaFai Lau 		 * bpf_map_alloc_id() has published the map
627bd5f5f4eSMartin KaFai Lau 		 * to the userspace and the userspace may
628bd5f5f4eSMartin KaFai Lau 		 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
629bd5f5f4eSMartin KaFai Lau 		 */
630352d20d6SPeng Sun 		bpf_map_put_with_uref(map);
631bd5f5f4eSMartin KaFai Lau 		return err;
632bd5f5f4eSMartin KaFai Lau 	}
63399c55f7dSAlexei Starovoitov 
63499c55f7dSAlexei Starovoitov 	return err;
63599c55f7dSAlexei Starovoitov 
63699c55f7dSAlexei Starovoitov free_map:
6370a4c58f5SRoman Gushchin 	bpf_map_release_memlock(map);
638afdb09c7SChenbo Feng free_map_sec:
639afdb09c7SChenbo Feng 	security_bpf_map_free(map);
64020b2b24fSDaniel Borkmann free_map_nouncharge:
641a26ca7c9SMartin KaFai Lau 	btf_put(map->btf);
64299c55f7dSAlexei Starovoitov 	map->ops->map_free(map);
64399c55f7dSAlexei Starovoitov 	return err;
64499c55f7dSAlexei Starovoitov }
64599c55f7dSAlexei Starovoitov 
646db20fd2bSAlexei Starovoitov /* if error is returned, fd is released.
647db20fd2bSAlexei Starovoitov  * On success caller should complete fd access with matching fdput()
648db20fd2bSAlexei Starovoitov  */
649c2101297SDaniel Borkmann struct bpf_map *__bpf_map_get(struct fd f)
650db20fd2bSAlexei Starovoitov {
651db20fd2bSAlexei Starovoitov 	if (!f.file)
652db20fd2bSAlexei Starovoitov 		return ERR_PTR(-EBADF);
653db20fd2bSAlexei Starovoitov 	if (f.file->f_op != &bpf_map_fops) {
654db20fd2bSAlexei Starovoitov 		fdput(f);
655db20fd2bSAlexei Starovoitov 		return ERR_PTR(-EINVAL);
656db20fd2bSAlexei Starovoitov 	}
657db20fd2bSAlexei Starovoitov 
658c2101297SDaniel Borkmann 	return f.file->private_data;
659c2101297SDaniel Borkmann }
660c2101297SDaniel Borkmann 
66192117d84SAlexei Starovoitov /* prog's and map's refcnt limit */
66292117d84SAlexei Starovoitov #define BPF_MAX_REFCNT 32768
66392117d84SAlexei Starovoitov 
66492117d84SAlexei Starovoitov struct bpf_map *bpf_map_inc(struct bpf_map *map, bool uref)
665c9da161cSDaniel Borkmann {
66692117d84SAlexei Starovoitov 	if (atomic_inc_return(&map->refcnt) > BPF_MAX_REFCNT) {
66792117d84SAlexei Starovoitov 		atomic_dec(&map->refcnt);
66892117d84SAlexei Starovoitov 		return ERR_PTR(-EBUSY);
66992117d84SAlexei Starovoitov 	}
670c9da161cSDaniel Borkmann 	if (uref)
671c9da161cSDaniel Borkmann 		atomic_inc(&map->usercnt);
67292117d84SAlexei Starovoitov 	return map;
673c9da161cSDaniel Borkmann }
674630a4d38SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_map_inc);
675c9da161cSDaniel Borkmann 
676c9da161cSDaniel Borkmann struct bpf_map *bpf_map_get_with_uref(u32 ufd)
677c2101297SDaniel Borkmann {
678c2101297SDaniel Borkmann 	struct fd f = fdget(ufd);
679c2101297SDaniel Borkmann 	struct bpf_map *map;
680c2101297SDaniel Borkmann 
681c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
682c2101297SDaniel Borkmann 	if (IS_ERR(map))
683c2101297SDaniel Borkmann 		return map;
684c2101297SDaniel Borkmann 
68592117d84SAlexei Starovoitov 	map = bpf_map_inc(map, true);
686c2101297SDaniel Borkmann 	fdput(f);
687db20fd2bSAlexei Starovoitov 
688db20fd2bSAlexei Starovoitov 	return map;
689db20fd2bSAlexei Starovoitov }
690db20fd2bSAlexei Starovoitov 
691bd5f5f4eSMartin KaFai Lau /* map_idr_lock should have been held */
692bd5f5f4eSMartin KaFai Lau static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map,
693bd5f5f4eSMartin KaFai Lau 					    bool uref)
694bd5f5f4eSMartin KaFai Lau {
695bd5f5f4eSMartin KaFai Lau 	int refold;
696bd5f5f4eSMartin KaFai Lau 
697bfc18e38SMark Rutland 	refold = atomic_fetch_add_unless(&map->refcnt, 1, 0);
698bd5f5f4eSMartin KaFai Lau 
699bd5f5f4eSMartin KaFai Lau 	if (refold >= BPF_MAX_REFCNT) {
700bd5f5f4eSMartin KaFai Lau 		__bpf_map_put(map, false);
701bd5f5f4eSMartin KaFai Lau 		return ERR_PTR(-EBUSY);
702bd5f5f4eSMartin KaFai Lau 	}
703bd5f5f4eSMartin KaFai Lau 
704bd5f5f4eSMartin KaFai Lau 	if (!refold)
705bd5f5f4eSMartin KaFai Lau 		return ERR_PTR(-ENOENT);
706bd5f5f4eSMartin KaFai Lau 
707bd5f5f4eSMartin KaFai Lau 	if (uref)
708bd5f5f4eSMartin KaFai Lau 		atomic_inc(&map->usercnt);
709bd5f5f4eSMartin KaFai Lau 
710bd5f5f4eSMartin KaFai Lau 	return map;
711bd5f5f4eSMartin KaFai Lau }
712bd5f5f4eSMartin KaFai Lau 
713b8cdc051SAlexei Starovoitov int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
714b8cdc051SAlexei Starovoitov {
715b8cdc051SAlexei Starovoitov 	return -ENOTSUPP;
716b8cdc051SAlexei Starovoitov }
717b8cdc051SAlexei Starovoitov 
718c9d29f46SMauricio Vasquez B static void *__bpf_copy_key(void __user *ukey, u64 key_size)
719c9d29f46SMauricio Vasquez B {
720c9d29f46SMauricio Vasquez B 	if (key_size)
721c9d29f46SMauricio Vasquez B 		return memdup_user(ukey, key_size);
722c9d29f46SMauricio Vasquez B 
723c9d29f46SMauricio Vasquez B 	if (ukey)
724c9d29f46SMauricio Vasquez B 		return ERR_PTR(-EINVAL);
725c9d29f46SMauricio Vasquez B 
726c9d29f46SMauricio Vasquez B 	return NULL;
727c9d29f46SMauricio Vasquez B }
728c9d29f46SMauricio Vasquez B 
729db20fd2bSAlexei Starovoitov /* last field in 'union bpf_attr' used by this command */
73096049f3aSAlexei Starovoitov #define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
731db20fd2bSAlexei Starovoitov 
732db20fd2bSAlexei Starovoitov static int map_lookup_elem(union bpf_attr *attr)
733db20fd2bSAlexei Starovoitov {
734535e7b4bSMickaël Salaün 	void __user *ukey = u64_to_user_ptr(attr->key);
735535e7b4bSMickaël Salaün 	void __user *uvalue = u64_to_user_ptr(attr->value);
736db20fd2bSAlexei Starovoitov 	int ufd = attr->map_fd;
737db20fd2bSAlexei Starovoitov 	struct bpf_map *map;
7388ebe667cSAlexei Starovoitov 	void *key, *value, *ptr;
73915a07b33SAlexei Starovoitov 	u32 value_size;
740592867bfSDaniel Borkmann 	struct fd f;
741db20fd2bSAlexei Starovoitov 	int err;
742db20fd2bSAlexei Starovoitov 
743db20fd2bSAlexei Starovoitov 	if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
744db20fd2bSAlexei Starovoitov 		return -EINVAL;
745db20fd2bSAlexei Starovoitov 
74696049f3aSAlexei Starovoitov 	if (attr->flags & ~BPF_F_LOCK)
74796049f3aSAlexei Starovoitov 		return -EINVAL;
74896049f3aSAlexei Starovoitov 
749592867bfSDaniel Borkmann 	f = fdget(ufd);
750c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
751db20fd2bSAlexei Starovoitov 	if (IS_ERR(map))
752db20fd2bSAlexei Starovoitov 		return PTR_ERR(map);
75387df15deSDaniel Borkmann 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
7546e71b04aSChenbo Feng 		err = -EPERM;
7556e71b04aSChenbo Feng 		goto err_put;
7566e71b04aSChenbo Feng 	}
7576e71b04aSChenbo Feng 
75896049f3aSAlexei Starovoitov 	if ((attr->flags & BPF_F_LOCK) &&
75996049f3aSAlexei Starovoitov 	    !map_value_has_spin_lock(map)) {
76096049f3aSAlexei Starovoitov 		err = -EINVAL;
76196049f3aSAlexei Starovoitov 		goto err_put;
76296049f3aSAlexei Starovoitov 	}
76396049f3aSAlexei Starovoitov 
764c9d29f46SMauricio Vasquez B 	key = __bpf_copy_key(ukey, map->key_size);
765e4448ed8SAl Viro 	if (IS_ERR(key)) {
766e4448ed8SAl Viro 		err = PTR_ERR(key);
767db20fd2bSAlexei Starovoitov 		goto err_put;
768e4448ed8SAl Viro 	}
769db20fd2bSAlexei Starovoitov 
77015a07b33SAlexei Starovoitov 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
7718f844938SMartin KaFai Lau 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
772b741f163SRoman Gushchin 	    map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
773b741f163SRoman Gushchin 	    map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
77415a07b33SAlexei Starovoitov 		value_size = round_up(map->value_size, 8) * num_possible_cpus();
77514dc6f04SMartin KaFai Lau 	else if (IS_FD_MAP(map))
77614dc6f04SMartin KaFai Lau 		value_size = sizeof(u32);
77715a07b33SAlexei Starovoitov 	else
77815a07b33SAlexei Starovoitov 		value_size = map->value_size;
77915a07b33SAlexei Starovoitov 
7808ebe667cSAlexei Starovoitov 	err = -ENOMEM;
78115a07b33SAlexei Starovoitov 	value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
782db20fd2bSAlexei Starovoitov 	if (!value)
7838ebe667cSAlexei Starovoitov 		goto free_key;
7848ebe667cSAlexei Starovoitov 
785a3884572SJakub Kicinski 	if (bpf_map_is_dev_bound(map)) {
786a3884572SJakub Kicinski 		err = bpf_map_offload_lookup_elem(map, key, value);
7877c4cd051SMartin KaFai Lau 		goto done;
7887c4cd051SMartin KaFai Lau 	}
7897c4cd051SMartin KaFai Lau 
7907c4cd051SMartin KaFai Lau 	preempt_disable();
7917c4cd051SMartin KaFai Lau 	this_cpu_inc(bpf_prog_active);
7927c4cd051SMartin KaFai Lau 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
7938f844938SMartin KaFai Lau 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
79415a07b33SAlexei Starovoitov 		err = bpf_percpu_hash_copy(map, key, value);
79515a07b33SAlexei Starovoitov 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
79615a07b33SAlexei Starovoitov 		err = bpf_percpu_array_copy(map, key, value);
797b741f163SRoman Gushchin 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
798b741f163SRoman Gushchin 		err = bpf_percpu_cgroup_storage_copy(map, key, value);
799557c0c6eSAlexei Starovoitov 	} else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
800557c0c6eSAlexei Starovoitov 		err = bpf_stackmap_copy(map, key, value);
80114dc6f04SMartin KaFai Lau 	} else if (IS_FD_ARRAY(map)) {
80214dc6f04SMartin KaFai Lau 		err = bpf_fd_array_map_lookup_elem(map, key, value);
80314dc6f04SMartin KaFai Lau 	} else if (IS_FD_HASH(map)) {
80414dc6f04SMartin KaFai Lau 		err = bpf_fd_htab_map_lookup_elem(map, key, value);
8055dc4c4b7SMartin KaFai Lau 	} else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
8065dc4c4b7SMartin KaFai Lau 		err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
807f1a2e44aSMauricio Vasquez B 	} else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
808f1a2e44aSMauricio Vasquez B 		   map->map_type == BPF_MAP_TYPE_STACK) {
809f1a2e44aSMauricio Vasquez B 		err = map->ops->map_peek_elem(map, value);
81015a07b33SAlexei Starovoitov 	} else {
8118ebe667cSAlexei Starovoitov 		rcu_read_lock();
812c6110222SDaniel Borkmann 		if (map->ops->map_lookup_elem_sys_only)
813c6110222SDaniel Borkmann 			ptr = map->ops->map_lookup_elem_sys_only(map, key);
814c6110222SDaniel Borkmann 		else
8158ebe667cSAlexei Starovoitov 			ptr = map->ops->map_lookup_elem(map, key);
816509db283SPrashant Bhole 		if (IS_ERR(ptr)) {
817509db283SPrashant Bhole 			err = PTR_ERR(ptr);
818509db283SPrashant Bhole 		} else if (!ptr) {
819509db283SPrashant Bhole 			err = -ENOENT;
820509db283SPrashant Bhole 		} else {
821509db283SPrashant Bhole 			err = 0;
82296049f3aSAlexei Starovoitov 			if (attr->flags & BPF_F_LOCK)
82396049f3aSAlexei Starovoitov 				/* lock 'ptr' and copy everything but lock */
82496049f3aSAlexei Starovoitov 				copy_map_value_locked(map, value, ptr, true);
82596049f3aSAlexei Starovoitov 			else
826d83525caSAlexei Starovoitov 				copy_map_value(map, value, ptr);
82796049f3aSAlexei Starovoitov 			/* mask lock, since value wasn't zero inited */
82896049f3aSAlexei Starovoitov 			check_and_init_map_lock(map, value);
829509db283SPrashant Bhole 		}
8308ebe667cSAlexei Starovoitov 		rcu_read_unlock();
83115a07b33SAlexei Starovoitov 	}
8327c4cd051SMartin KaFai Lau 	this_cpu_dec(bpf_prog_active);
8337c4cd051SMartin KaFai Lau 	preempt_enable();
8348ebe667cSAlexei Starovoitov 
8357c4cd051SMartin KaFai Lau done:
83615a07b33SAlexei Starovoitov 	if (err)
8378ebe667cSAlexei Starovoitov 		goto free_value;
838db20fd2bSAlexei Starovoitov 
839db20fd2bSAlexei Starovoitov 	err = -EFAULT;
84015a07b33SAlexei Starovoitov 	if (copy_to_user(uvalue, value, value_size) != 0)
8418ebe667cSAlexei Starovoitov 		goto free_value;
842db20fd2bSAlexei Starovoitov 
843db20fd2bSAlexei Starovoitov 	err = 0;
844db20fd2bSAlexei Starovoitov 
8458ebe667cSAlexei Starovoitov free_value:
8468ebe667cSAlexei Starovoitov 	kfree(value);
847db20fd2bSAlexei Starovoitov free_key:
848db20fd2bSAlexei Starovoitov 	kfree(key);
849db20fd2bSAlexei Starovoitov err_put:
850db20fd2bSAlexei Starovoitov 	fdput(f);
851db20fd2bSAlexei Starovoitov 	return err;
852db20fd2bSAlexei Starovoitov }
853db20fd2bSAlexei Starovoitov 
8541ae80cf3SDaniel Colascione static void maybe_wait_bpf_programs(struct bpf_map *map)
8551ae80cf3SDaniel Colascione {
8561ae80cf3SDaniel Colascione 	/* Wait for any running BPF programs to complete so that
8571ae80cf3SDaniel Colascione 	 * userspace, when we return to it, knows that all programs
8581ae80cf3SDaniel Colascione 	 * that could be running use the new map value.
8591ae80cf3SDaniel Colascione 	 */
8601ae80cf3SDaniel Colascione 	if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
8611ae80cf3SDaniel Colascione 	    map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
8621ae80cf3SDaniel Colascione 		synchronize_rcu();
8631ae80cf3SDaniel Colascione }
8641ae80cf3SDaniel Colascione 
8653274f520SAlexei Starovoitov #define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
866db20fd2bSAlexei Starovoitov 
867db20fd2bSAlexei Starovoitov static int map_update_elem(union bpf_attr *attr)
868db20fd2bSAlexei Starovoitov {
869535e7b4bSMickaël Salaün 	void __user *ukey = u64_to_user_ptr(attr->key);
870535e7b4bSMickaël Salaün 	void __user *uvalue = u64_to_user_ptr(attr->value);
871db20fd2bSAlexei Starovoitov 	int ufd = attr->map_fd;
872db20fd2bSAlexei Starovoitov 	struct bpf_map *map;
873db20fd2bSAlexei Starovoitov 	void *key, *value;
87415a07b33SAlexei Starovoitov 	u32 value_size;
875592867bfSDaniel Borkmann 	struct fd f;
876db20fd2bSAlexei Starovoitov 	int err;
877db20fd2bSAlexei Starovoitov 
878db20fd2bSAlexei Starovoitov 	if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
879db20fd2bSAlexei Starovoitov 		return -EINVAL;
880db20fd2bSAlexei Starovoitov 
881592867bfSDaniel Borkmann 	f = fdget(ufd);
882c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
883db20fd2bSAlexei Starovoitov 	if (IS_ERR(map))
884db20fd2bSAlexei Starovoitov 		return PTR_ERR(map);
88587df15deSDaniel Borkmann 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
8866e71b04aSChenbo Feng 		err = -EPERM;
8876e71b04aSChenbo Feng 		goto err_put;
8886e71b04aSChenbo Feng 	}
8896e71b04aSChenbo Feng 
89096049f3aSAlexei Starovoitov 	if ((attr->flags & BPF_F_LOCK) &&
89196049f3aSAlexei Starovoitov 	    !map_value_has_spin_lock(map)) {
89296049f3aSAlexei Starovoitov 		err = -EINVAL;
89396049f3aSAlexei Starovoitov 		goto err_put;
89496049f3aSAlexei Starovoitov 	}
89596049f3aSAlexei Starovoitov 
896c9d29f46SMauricio Vasquez B 	key = __bpf_copy_key(ukey, map->key_size);
897e4448ed8SAl Viro 	if (IS_ERR(key)) {
898e4448ed8SAl Viro 		err = PTR_ERR(key);
899db20fd2bSAlexei Starovoitov 		goto err_put;
900e4448ed8SAl Viro 	}
901db20fd2bSAlexei Starovoitov 
90215a07b33SAlexei Starovoitov 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
9038f844938SMartin KaFai Lau 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
904b741f163SRoman Gushchin 	    map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
905b741f163SRoman Gushchin 	    map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
90615a07b33SAlexei Starovoitov 		value_size = round_up(map->value_size, 8) * num_possible_cpus();
90715a07b33SAlexei Starovoitov 	else
90815a07b33SAlexei Starovoitov 		value_size = map->value_size;
90915a07b33SAlexei Starovoitov 
910db20fd2bSAlexei Starovoitov 	err = -ENOMEM;
91115a07b33SAlexei Starovoitov 	value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
912db20fd2bSAlexei Starovoitov 	if (!value)
913db20fd2bSAlexei Starovoitov 		goto free_key;
914db20fd2bSAlexei Starovoitov 
915db20fd2bSAlexei Starovoitov 	err = -EFAULT;
91615a07b33SAlexei Starovoitov 	if (copy_from_user(value, uvalue, value_size) != 0)
917db20fd2bSAlexei Starovoitov 		goto free_value;
918db20fd2bSAlexei Starovoitov 
9196710e112SJesper Dangaard Brouer 	/* Need to create a kthread, thus must support schedule */
920a3884572SJakub Kicinski 	if (bpf_map_is_dev_bound(map)) {
921a3884572SJakub Kicinski 		err = bpf_map_offload_update_elem(map, key, value, attr->flags);
922a3884572SJakub Kicinski 		goto out;
92399ba2b5aSJohn Fastabend 	} else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
92499ba2b5aSJohn Fastabend 		   map->map_type == BPF_MAP_TYPE_SOCKHASH ||
92599ba2b5aSJohn Fastabend 		   map->map_type == BPF_MAP_TYPE_SOCKMAP) {
9266710e112SJesper Dangaard Brouer 		err = map->ops->map_update_elem(map, key, value, attr->flags);
9276710e112SJesper Dangaard Brouer 		goto out;
9286710e112SJesper Dangaard Brouer 	}
9296710e112SJesper Dangaard Brouer 
930b121d1e7SAlexei Starovoitov 	/* must increment bpf_prog_active to avoid kprobe+bpf triggering from
931b121d1e7SAlexei Starovoitov 	 * inside bpf map update or delete otherwise deadlocks are possible
932b121d1e7SAlexei Starovoitov 	 */
933b121d1e7SAlexei Starovoitov 	preempt_disable();
934b121d1e7SAlexei Starovoitov 	__this_cpu_inc(bpf_prog_active);
9358f844938SMartin KaFai Lau 	if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
9368f844938SMartin KaFai Lau 	    map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
93715a07b33SAlexei Starovoitov 		err = bpf_percpu_hash_update(map, key, value, attr->flags);
93815a07b33SAlexei Starovoitov 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
93915a07b33SAlexei Starovoitov 		err = bpf_percpu_array_update(map, key, value, attr->flags);
940b741f163SRoman Gushchin 	} else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
941b741f163SRoman Gushchin 		err = bpf_percpu_cgroup_storage_update(map, key, value,
942b741f163SRoman Gushchin 						       attr->flags);
9439c147b56SMickaël Salaün 	} else if (IS_FD_ARRAY(map)) {
944d056a788SDaniel Borkmann 		rcu_read_lock();
945d056a788SDaniel Borkmann 		err = bpf_fd_array_map_update_elem(map, f.file, key, value,
946d056a788SDaniel Borkmann 						   attr->flags);
947d056a788SDaniel Borkmann 		rcu_read_unlock();
948bcc6b1b7SMartin KaFai Lau 	} else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
949bcc6b1b7SMartin KaFai Lau 		rcu_read_lock();
950bcc6b1b7SMartin KaFai Lau 		err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
951bcc6b1b7SMartin KaFai Lau 						  attr->flags);
952bcc6b1b7SMartin KaFai Lau 		rcu_read_unlock();
9535dc4c4b7SMartin KaFai Lau 	} else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
9545dc4c4b7SMartin KaFai Lau 		/* rcu_read_lock() is not needed */
9555dc4c4b7SMartin KaFai Lau 		err = bpf_fd_reuseport_array_update_elem(map, key, value,
9565dc4c4b7SMartin KaFai Lau 							 attr->flags);
957f1a2e44aSMauricio Vasquez B 	} else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
958f1a2e44aSMauricio Vasquez B 		   map->map_type == BPF_MAP_TYPE_STACK) {
959f1a2e44aSMauricio Vasquez B 		err = map->ops->map_push_elem(map, value, attr->flags);
96015a07b33SAlexei Starovoitov 	} else {
961db20fd2bSAlexei Starovoitov 		rcu_read_lock();
9623274f520SAlexei Starovoitov 		err = map->ops->map_update_elem(map, key, value, attr->flags);
963db20fd2bSAlexei Starovoitov 		rcu_read_unlock();
96415a07b33SAlexei Starovoitov 	}
965b121d1e7SAlexei Starovoitov 	__this_cpu_dec(bpf_prog_active);
966b121d1e7SAlexei Starovoitov 	preempt_enable();
9671ae80cf3SDaniel Colascione 	maybe_wait_bpf_programs(map);
9686710e112SJesper Dangaard Brouer out:
969db20fd2bSAlexei Starovoitov free_value:
970db20fd2bSAlexei Starovoitov 	kfree(value);
971db20fd2bSAlexei Starovoitov free_key:
972db20fd2bSAlexei Starovoitov 	kfree(key);
973db20fd2bSAlexei Starovoitov err_put:
974db20fd2bSAlexei Starovoitov 	fdput(f);
975db20fd2bSAlexei Starovoitov 	return err;
976db20fd2bSAlexei Starovoitov }
977db20fd2bSAlexei Starovoitov 
978db20fd2bSAlexei Starovoitov #define BPF_MAP_DELETE_ELEM_LAST_FIELD key
979db20fd2bSAlexei Starovoitov 
980db20fd2bSAlexei Starovoitov static int map_delete_elem(union bpf_attr *attr)
981db20fd2bSAlexei Starovoitov {
982535e7b4bSMickaël Salaün 	void __user *ukey = u64_to_user_ptr(attr->key);
983db20fd2bSAlexei Starovoitov 	int ufd = attr->map_fd;
984db20fd2bSAlexei Starovoitov 	struct bpf_map *map;
985592867bfSDaniel Borkmann 	struct fd f;
986db20fd2bSAlexei Starovoitov 	void *key;
987db20fd2bSAlexei Starovoitov 	int err;
988db20fd2bSAlexei Starovoitov 
989db20fd2bSAlexei Starovoitov 	if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
990db20fd2bSAlexei Starovoitov 		return -EINVAL;
991db20fd2bSAlexei Starovoitov 
992592867bfSDaniel Borkmann 	f = fdget(ufd);
993c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
994db20fd2bSAlexei Starovoitov 	if (IS_ERR(map))
995db20fd2bSAlexei Starovoitov 		return PTR_ERR(map);
99687df15deSDaniel Borkmann 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
9976e71b04aSChenbo Feng 		err = -EPERM;
9986e71b04aSChenbo Feng 		goto err_put;
9996e71b04aSChenbo Feng 	}
10006e71b04aSChenbo Feng 
1001c9d29f46SMauricio Vasquez B 	key = __bpf_copy_key(ukey, map->key_size);
1002e4448ed8SAl Viro 	if (IS_ERR(key)) {
1003e4448ed8SAl Viro 		err = PTR_ERR(key);
1004db20fd2bSAlexei Starovoitov 		goto err_put;
1005e4448ed8SAl Viro 	}
1006db20fd2bSAlexei Starovoitov 
1007a3884572SJakub Kicinski 	if (bpf_map_is_dev_bound(map)) {
1008a3884572SJakub Kicinski 		err = bpf_map_offload_delete_elem(map, key);
1009a3884572SJakub Kicinski 		goto out;
1010a3884572SJakub Kicinski 	}
1011a3884572SJakub Kicinski 
1012b121d1e7SAlexei Starovoitov 	preempt_disable();
1013b121d1e7SAlexei Starovoitov 	__this_cpu_inc(bpf_prog_active);
1014db20fd2bSAlexei Starovoitov 	rcu_read_lock();
1015db20fd2bSAlexei Starovoitov 	err = map->ops->map_delete_elem(map, key);
1016db20fd2bSAlexei Starovoitov 	rcu_read_unlock();
1017b121d1e7SAlexei Starovoitov 	__this_cpu_dec(bpf_prog_active);
1018b121d1e7SAlexei Starovoitov 	preempt_enable();
10191ae80cf3SDaniel Colascione 	maybe_wait_bpf_programs(map);
1020a3884572SJakub Kicinski out:
1021db20fd2bSAlexei Starovoitov 	kfree(key);
1022db20fd2bSAlexei Starovoitov err_put:
1023db20fd2bSAlexei Starovoitov 	fdput(f);
1024db20fd2bSAlexei Starovoitov 	return err;
1025db20fd2bSAlexei Starovoitov }
1026db20fd2bSAlexei Starovoitov 
1027db20fd2bSAlexei Starovoitov /* last field in 'union bpf_attr' used by this command */
1028db20fd2bSAlexei Starovoitov #define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1029db20fd2bSAlexei Starovoitov 
1030db20fd2bSAlexei Starovoitov static int map_get_next_key(union bpf_attr *attr)
1031db20fd2bSAlexei Starovoitov {
1032535e7b4bSMickaël Salaün 	void __user *ukey = u64_to_user_ptr(attr->key);
1033535e7b4bSMickaël Salaün 	void __user *unext_key = u64_to_user_ptr(attr->next_key);
1034db20fd2bSAlexei Starovoitov 	int ufd = attr->map_fd;
1035db20fd2bSAlexei Starovoitov 	struct bpf_map *map;
1036db20fd2bSAlexei Starovoitov 	void *key, *next_key;
1037592867bfSDaniel Borkmann 	struct fd f;
1038db20fd2bSAlexei Starovoitov 	int err;
1039db20fd2bSAlexei Starovoitov 
1040db20fd2bSAlexei Starovoitov 	if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1041db20fd2bSAlexei Starovoitov 		return -EINVAL;
1042db20fd2bSAlexei Starovoitov 
1043592867bfSDaniel Borkmann 	f = fdget(ufd);
1044c2101297SDaniel Borkmann 	map = __bpf_map_get(f);
1045db20fd2bSAlexei Starovoitov 	if (IS_ERR(map))
1046db20fd2bSAlexei Starovoitov 		return PTR_ERR(map);
104787df15deSDaniel Borkmann 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
10486e71b04aSChenbo Feng 		err = -EPERM;
10496e71b04aSChenbo Feng 		goto err_put;
10506e71b04aSChenbo Feng 	}
10516e71b04aSChenbo Feng 
10528fe45924STeng Qin 	if (ukey) {
1053c9d29f46SMauricio Vasquez B 		key = __bpf_copy_key(ukey, map->key_size);
1054e4448ed8SAl Viro 		if (IS_ERR(key)) {
1055e4448ed8SAl Viro 			err = PTR_ERR(key);
1056db20fd2bSAlexei Starovoitov 			goto err_put;
1057e4448ed8SAl Viro 		}
10588fe45924STeng Qin 	} else {
10598fe45924STeng Qin 		key = NULL;
10608fe45924STeng Qin 	}
1061db20fd2bSAlexei Starovoitov 
1062db20fd2bSAlexei Starovoitov 	err = -ENOMEM;
1063db20fd2bSAlexei Starovoitov 	next_key = kmalloc(map->key_size, GFP_USER);
1064db20fd2bSAlexei Starovoitov 	if (!next_key)
1065db20fd2bSAlexei Starovoitov 		goto free_key;
1066db20fd2bSAlexei Starovoitov 
1067a3884572SJakub Kicinski 	if (bpf_map_is_dev_bound(map)) {
1068a3884572SJakub Kicinski 		err = bpf_map_offload_get_next_key(map, key, next_key);
1069a3884572SJakub Kicinski 		goto out;
1070a3884572SJakub Kicinski 	}
1071a3884572SJakub Kicinski 
1072db20fd2bSAlexei Starovoitov 	rcu_read_lock();
1073db20fd2bSAlexei Starovoitov 	err = map->ops->map_get_next_key(map, key, next_key);
1074db20fd2bSAlexei Starovoitov 	rcu_read_unlock();
1075a3884572SJakub Kicinski out:
1076db20fd2bSAlexei Starovoitov 	if (err)
1077db20fd2bSAlexei Starovoitov 		goto free_next_key;
1078db20fd2bSAlexei Starovoitov 
1079db20fd2bSAlexei Starovoitov 	err = -EFAULT;
1080db20fd2bSAlexei Starovoitov 	if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1081db20fd2bSAlexei Starovoitov 		goto free_next_key;
1082db20fd2bSAlexei Starovoitov 
1083db20fd2bSAlexei Starovoitov 	err = 0;
1084db20fd2bSAlexei Starovoitov 
1085db20fd2bSAlexei Starovoitov free_next_key:
1086db20fd2bSAlexei Starovoitov 	kfree(next_key);
1087db20fd2bSAlexei Starovoitov free_key:
1088db20fd2bSAlexei Starovoitov 	kfree(key);
1089db20fd2bSAlexei Starovoitov err_put:
1090db20fd2bSAlexei Starovoitov 	fdput(f);
1091db20fd2bSAlexei Starovoitov 	return err;
1092db20fd2bSAlexei Starovoitov }
1093db20fd2bSAlexei Starovoitov 
1094bd513cd0SMauricio Vasquez B #define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1095bd513cd0SMauricio Vasquez B 
1096bd513cd0SMauricio Vasquez B static int map_lookup_and_delete_elem(union bpf_attr *attr)
1097bd513cd0SMauricio Vasquez B {
1098bd513cd0SMauricio Vasquez B 	void __user *ukey = u64_to_user_ptr(attr->key);
1099bd513cd0SMauricio Vasquez B 	void __user *uvalue = u64_to_user_ptr(attr->value);
1100bd513cd0SMauricio Vasquez B 	int ufd = attr->map_fd;
1101bd513cd0SMauricio Vasquez B 	struct bpf_map *map;
1102540fefc0SAlexei Starovoitov 	void *key, *value;
1103bd513cd0SMauricio Vasquez B 	u32 value_size;
1104bd513cd0SMauricio Vasquez B 	struct fd f;
1105bd513cd0SMauricio Vasquez B 	int err;
1106bd513cd0SMauricio Vasquez B 
1107bd513cd0SMauricio Vasquez B 	if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1108bd513cd0SMauricio Vasquez B 		return -EINVAL;
1109bd513cd0SMauricio Vasquez B 
1110bd513cd0SMauricio Vasquez B 	f = fdget(ufd);
1111bd513cd0SMauricio Vasquez B 	map = __bpf_map_get(f);
1112bd513cd0SMauricio Vasquez B 	if (IS_ERR(map))
1113bd513cd0SMauricio Vasquez B 		return PTR_ERR(map);
111487df15deSDaniel Borkmann 	if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
1115bd513cd0SMauricio Vasquez B 		err = -EPERM;
1116bd513cd0SMauricio Vasquez B 		goto err_put;
1117bd513cd0SMauricio Vasquez B 	}
1118bd513cd0SMauricio Vasquez B 
1119bd513cd0SMauricio Vasquez B 	key = __bpf_copy_key(ukey, map->key_size);
1120bd513cd0SMauricio Vasquez B 	if (IS_ERR(key)) {
1121bd513cd0SMauricio Vasquez B 		err = PTR_ERR(key);
1122bd513cd0SMauricio Vasquez B 		goto err_put;
1123bd513cd0SMauricio Vasquez B 	}
1124bd513cd0SMauricio Vasquez B 
1125bd513cd0SMauricio Vasquez B 	value_size = map->value_size;
1126bd513cd0SMauricio Vasquez B 
1127bd513cd0SMauricio Vasquez B 	err = -ENOMEM;
1128bd513cd0SMauricio Vasquez B 	value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1129bd513cd0SMauricio Vasquez B 	if (!value)
1130bd513cd0SMauricio Vasquez B 		goto free_key;
1131bd513cd0SMauricio Vasquez B 
1132bd513cd0SMauricio Vasquez B 	if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1133bd513cd0SMauricio Vasquez B 	    map->map_type == BPF_MAP_TYPE_STACK) {
1134bd513cd0SMauricio Vasquez B 		err = map->ops->map_pop_elem(map, value);
1135bd513cd0SMauricio Vasquez B 	} else {
1136bd513cd0SMauricio Vasquez B 		err = -ENOTSUPP;
1137bd513cd0SMauricio Vasquez B 	}
1138bd513cd0SMauricio Vasquez B 
1139bd513cd0SMauricio Vasquez B 	if (err)
1140bd513cd0SMauricio Vasquez B 		goto free_value;
1141bd513cd0SMauricio Vasquez B 
1142bd513cd0SMauricio Vasquez B 	if (copy_to_user(uvalue, value, value_size) != 0)
1143bd513cd0SMauricio Vasquez B 		goto free_value;
1144bd513cd0SMauricio Vasquez B 
1145bd513cd0SMauricio Vasquez B 	err = 0;
1146bd513cd0SMauricio Vasquez B 
1147bd513cd0SMauricio Vasquez B free_value:
1148bd513cd0SMauricio Vasquez B 	kfree(value);
1149bd513cd0SMauricio Vasquez B free_key:
1150bd513cd0SMauricio Vasquez B 	kfree(key);
1151bd513cd0SMauricio Vasquez B err_put:
1152bd513cd0SMauricio Vasquez B 	fdput(f);
1153bd513cd0SMauricio Vasquez B 	return err;
1154bd513cd0SMauricio Vasquez B }
1155bd513cd0SMauricio Vasquez B 
115687df15deSDaniel Borkmann #define BPF_MAP_FREEZE_LAST_FIELD map_fd
115787df15deSDaniel Borkmann 
115887df15deSDaniel Borkmann static int map_freeze(const union bpf_attr *attr)
115987df15deSDaniel Borkmann {
116087df15deSDaniel Borkmann 	int err = 0, ufd = attr->map_fd;
116187df15deSDaniel Borkmann 	struct bpf_map *map;
116287df15deSDaniel Borkmann 	struct fd f;
116387df15deSDaniel Borkmann 
116487df15deSDaniel Borkmann 	if (CHECK_ATTR(BPF_MAP_FREEZE))
116587df15deSDaniel Borkmann 		return -EINVAL;
116687df15deSDaniel Borkmann 
116787df15deSDaniel Borkmann 	f = fdget(ufd);
116887df15deSDaniel Borkmann 	map = __bpf_map_get(f);
116987df15deSDaniel Borkmann 	if (IS_ERR(map))
117087df15deSDaniel Borkmann 		return PTR_ERR(map);
117187df15deSDaniel Borkmann 	if (READ_ONCE(map->frozen)) {
117287df15deSDaniel Borkmann 		err = -EBUSY;
117387df15deSDaniel Borkmann 		goto err_put;
117487df15deSDaniel Borkmann 	}
117587df15deSDaniel Borkmann 	if (!capable(CAP_SYS_ADMIN)) {
117687df15deSDaniel Borkmann 		err = -EPERM;
117787df15deSDaniel Borkmann 		goto err_put;
117887df15deSDaniel Borkmann 	}
117987df15deSDaniel Borkmann 
118087df15deSDaniel Borkmann 	WRITE_ONCE(map->frozen, true);
118187df15deSDaniel Borkmann err_put:
118287df15deSDaniel Borkmann 	fdput(f);
118387df15deSDaniel Borkmann 	return err;
118487df15deSDaniel Borkmann }
118587df15deSDaniel Borkmann 
11867de16e3aSJakub Kicinski static const struct bpf_prog_ops * const bpf_prog_types[] = {
11877de16e3aSJakub Kicinski #define BPF_PROG_TYPE(_id, _name) \
11887de16e3aSJakub Kicinski 	[_id] = & _name ## _prog_ops,
11897de16e3aSJakub Kicinski #define BPF_MAP_TYPE(_id, _ops)
11907de16e3aSJakub Kicinski #include <linux/bpf_types.h>
11917de16e3aSJakub Kicinski #undef BPF_PROG_TYPE
11927de16e3aSJakub Kicinski #undef BPF_MAP_TYPE
11937de16e3aSJakub Kicinski };
11947de16e3aSJakub Kicinski 
119509756af4SAlexei Starovoitov static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
119609756af4SAlexei Starovoitov {
1197d0f1a451SDaniel Borkmann 	const struct bpf_prog_ops *ops;
1198d0f1a451SDaniel Borkmann 
1199d0f1a451SDaniel Borkmann 	if (type >= ARRAY_SIZE(bpf_prog_types))
1200d0f1a451SDaniel Borkmann 		return -EINVAL;
1201d0f1a451SDaniel Borkmann 	type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1202d0f1a451SDaniel Borkmann 	ops = bpf_prog_types[type];
1203d0f1a451SDaniel Borkmann 	if (!ops)
1204be9370a7SJohannes Berg 		return -EINVAL;
120509756af4SAlexei Starovoitov 
1206ab3f0063SJakub Kicinski 	if (!bpf_prog_is_dev_bound(prog->aux))
1207d0f1a451SDaniel Borkmann 		prog->aux->ops = ops;
1208ab3f0063SJakub Kicinski 	else
1209ab3f0063SJakub Kicinski 		prog->aux->ops = &bpf_offload_prog_ops;
121024701eceSDaniel Borkmann 	prog->type = type;
121109756af4SAlexei Starovoitov 	return 0;
121209756af4SAlexei Starovoitov }
121309756af4SAlexei Starovoitov 
121409756af4SAlexei Starovoitov /* drop refcnt on maps used by eBPF program and free auxilary data */
121509756af4SAlexei Starovoitov static void free_used_maps(struct bpf_prog_aux *aux)
121609756af4SAlexei Starovoitov {
12178bad74f9SRoman Gushchin 	enum bpf_cgroup_storage_type stype;
121809756af4SAlexei Starovoitov 	int i;
121909756af4SAlexei Starovoitov 
12208bad74f9SRoman Gushchin 	for_each_cgroup_storage_type(stype) {
12218bad74f9SRoman Gushchin 		if (!aux->cgroup_storage[stype])
12228bad74f9SRoman Gushchin 			continue;
12238bad74f9SRoman Gushchin 		bpf_cgroup_storage_release(aux->prog,
12248bad74f9SRoman Gushchin 					   aux->cgroup_storage[stype]);
12258bad74f9SRoman Gushchin 	}
1226de9cbbaaSRoman Gushchin 
122709756af4SAlexei Starovoitov 	for (i = 0; i < aux->used_map_cnt; i++)
122809756af4SAlexei Starovoitov 		bpf_map_put(aux->used_maps[i]);
122909756af4SAlexei Starovoitov 
123009756af4SAlexei Starovoitov 	kfree(aux->used_maps);
123109756af4SAlexei Starovoitov }
123209756af4SAlexei Starovoitov 
12335ccb071eSDaniel Borkmann int __bpf_prog_charge(struct user_struct *user, u32 pages)
12345ccb071eSDaniel Borkmann {
12355ccb071eSDaniel Borkmann 	unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
12365ccb071eSDaniel Borkmann 	unsigned long user_bufs;
12375ccb071eSDaniel Borkmann 
12385ccb071eSDaniel Borkmann 	if (user) {
12395ccb071eSDaniel Borkmann 		user_bufs = atomic_long_add_return(pages, &user->locked_vm);
12405ccb071eSDaniel Borkmann 		if (user_bufs > memlock_limit) {
12415ccb071eSDaniel Borkmann 			atomic_long_sub(pages, &user->locked_vm);
12425ccb071eSDaniel Borkmann 			return -EPERM;
12435ccb071eSDaniel Borkmann 		}
12445ccb071eSDaniel Borkmann 	}
12455ccb071eSDaniel Borkmann 
12465ccb071eSDaniel Borkmann 	return 0;
12475ccb071eSDaniel Borkmann }
12485ccb071eSDaniel Borkmann 
12495ccb071eSDaniel Borkmann void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
12505ccb071eSDaniel Borkmann {
12515ccb071eSDaniel Borkmann 	if (user)
12525ccb071eSDaniel Borkmann 		atomic_long_sub(pages, &user->locked_vm);
12535ccb071eSDaniel Borkmann }
12545ccb071eSDaniel Borkmann 
1255aaac3ba9SAlexei Starovoitov static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1256aaac3ba9SAlexei Starovoitov {
1257aaac3ba9SAlexei Starovoitov 	struct user_struct *user = get_current_user();
12585ccb071eSDaniel Borkmann 	int ret;
1259aaac3ba9SAlexei Starovoitov 
12605ccb071eSDaniel Borkmann 	ret = __bpf_prog_charge(user, prog->pages);
12615ccb071eSDaniel Borkmann 	if (ret) {
1262aaac3ba9SAlexei Starovoitov 		free_uid(user);
12635ccb071eSDaniel Borkmann 		return ret;
1264aaac3ba9SAlexei Starovoitov 	}
12655ccb071eSDaniel Borkmann 
1266aaac3ba9SAlexei Starovoitov 	prog->aux->user = user;
1267aaac3ba9SAlexei Starovoitov 	return 0;
1268aaac3ba9SAlexei Starovoitov }
1269aaac3ba9SAlexei Starovoitov 
1270aaac3ba9SAlexei Starovoitov static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1271aaac3ba9SAlexei Starovoitov {
1272aaac3ba9SAlexei Starovoitov 	struct user_struct *user = prog->aux->user;
1273aaac3ba9SAlexei Starovoitov 
12745ccb071eSDaniel Borkmann 	__bpf_prog_uncharge(user, prog->pages);
1275aaac3ba9SAlexei Starovoitov 	free_uid(user);
1276aaac3ba9SAlexei Starovoitov }
1277aaac3ba9SAlexei Starovoitov 
1278dc4bb0e2SMartin KaFai Lau static int bpf_prog_alloc_id(struct bpf_prog *prog)
1279dc4bb0e2SMartin KaFai Lau {
1280dc4bb0e2SMartin KaFai Lau 	int id;
1281dc4bb0e2SMartin KaFai Lau 
1282b76354cdSShaohua Li 	idr_preload(GFP_KERNEL);
1283dc4bb0e2SMartin KaFai Lau 	spin_lock_bh(&prog_idr_lock);
1284dc4bb0e2SMartin KaFai Lau 	id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1285dc4bb0e2SMartin KaFai Lau 	if (id > 0)
1286dc4bb0e2SMartin KaFai Lau 		prog->aux->id = id;
1287dc4bb0e2SMartin KaFai Lau 	spin_unlock_bh(&prog_idr_lock);
1288b76354cdSShaohua Li 	idr_preload_end();
1289dc4bb0e2SMartin KaFai Lau 
1290dc4bb0e2SMartin KaFai Lau 	/* id is in [1, INT_MAX) */
1291dc4bb0e2SMartin KaFai Lau 	if (WARN_ON_ONCE(!id))
1292dc4bb0e2SMartin KaFai Lau 		return -ENOSPC;
1293dc4bb0e2SMartin KaFai Lau 
1294dc4bb0e2SMartin KaFai Lau 	return id > 0 ? 0 : id;
1295dc4bb0e2SMartin KaFai Lau }
1296dc4bb0e2SMartin KaFai Lau 
1297ad8ad79fSJakub Kicinski void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
1298dc4bb0e2SMartin KaFai Lau {
1299ad8ad79fSJakub Kicinski 	/* cBPF to eBPF migrations are currently not in the idr store.
1300ad8ad79fSJakub Kicinski 	 * Offloaded programs are removed from the store when their device
1301ad8ad79fSJakub Kicinski 	 * disappears - even if someone grabs an fd to them they are unusable,
1302ad8ad79fSJakub Kicinski 	 * simply waiting for refcnt to drop to be freed.
1303ad8ad79fSJakub Kicinski 	 */
1304dc4bb0e2SMartin KaFai Lau 	if (!prog->aux->id)
1305dc4bb0e2SMartin KaFai Lau 		return;
1306dc4bb0e2SMartin KaFai Lau 
1307b16d9aa4SMartin KaFai Lau 	if (do_idr_lock)
1308dc4bb0e2SMartin KaFai Lau 		spin_lock_bh(&prog_idr_lock);
1309b16d9aa4SMartin KaFai Lau 	else
1310b16d9aa4SMartin KaFai Lau 		__acquire(&prog_idr_lock);
1311b16d9aa4SMartin KaFai Lau 
1312dc4bb0e2SMartin KaFai Lau 	idr_remove(&prog_idr, prog->aux->id);
1313ad8ad79fSJakub Kicinski 	prog->aux->id = 0;
1314b16d9aa4SMartin KaFai Lau 
1315b16d9aa4SMartin KaFai Lau 	if (do_idr_lock)
1316dc4bb0e2SMartin KaFai Lau 		spin_unlock_bh(&prog_idr_lock);
1317b16d9aa4SMartin KaFai Lau 	else
1318b16d9aa4SMartin KaFai Lau 		__release(&prog_idr_lock);
1319dc4bb0e2SMartin KaFai Lau }
1320dc4bb0e2SMartin KaFai Lau 
13211aacde3dSDaniel Borkmann static void __bpf_prog_put_rcu(struct rcu_head *rcu)
1322abf2e7d6SAlexei Starovoitov {
1323abf2e7d6SAlexei Starovoitov 	struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1324abf2e7d6SAlexei Starovoitov 
1325abf2e7d6SAlexei Starovoitov 	free_used_maps(aux);
1326aaac3ba9SAlexei Starovoitov 	bpf_prog_uncharge_memlock(aux->prog);
1327afdb09c7SChenbo Feng 	security_bpf_prog_free(aux);
1328abf2e7d6SAlexei Starovoitov 	bpf_prog_free(aux->prog);
1329abf2e7d6SAlexei Starovoitov }
1330abf2e7d6SAlexei Starovoitov 
1331b16d9aa4SMartin KaFai Lau static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
133209756af4SAlexei Starovoitov {
1333a67edbf4SDaniel Borkmann 	if (atomic_dec_and_test(&prog->aux->refcnt)) {
13346ee52e2aSSong Liu 		perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
133534ad5580SMartin KaFai Lau 		/* bpf_prog_free_id() must be called first */
1336b16d9aa4SMartin KaFai Lau 		bpf_prog_free_id(prog, do_idr_lock);
13377d1982b4SDaniel Borkmann 		bpf_prog_kallsyms_del_all(prog);
1338838e9690SYonghong Song 		btf_put(prog->aux->btf);
1339ba64e7d8SYonghong Song 		kvfree(prog->aux->func_info);
1340c454a46bSMartin KaFai Lau 		bpf_prog_free_linfo(prog);
13414f74d809SDaniel Borkmann 
13421aacde3dSDaniel Borkmann 		call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
134309756af4SAlexei Starovoitov 	}
1344a67edbf4SDaniel Borkmann }
1345b16d9aa4SMartin KaFai Lau 
1346b16d9aa4SMartin KaFai Lau void bpf_prog_put(struct bpf_prog *prog)
1347b16d9aa4SMartin KaFai Lau {
1348b16d9aa4SMartin KaFai Lau 	__bpf_prog_put(prog, true);
1349b16d9aa4SMartin KaFai Lau }
1350e2e9b654SDaniel Borkmann EXPORT_SYMBOL_GPL(bpf_prog_put);
135109756af4SAlexei Starovoitov 
135209756af4SAlexei Starovoitov static int bpf_prog_release(struct inode *inode, struct file *filp)
135309756af4SAlexei Starovoitov {
135409756af4SAlexei Starovoitov 	struct bpf_prog *prog = filp->private_data;
135509756af4SAlexei Starovoitov 
13561aacde3dSDaniel Borkmann 	bpf_prog_put(prog);
135709756af4SAlexei Starovoitov 	return 0;
135809756af4SAlexei Starovoitov }
135909756af4SAlexei Starovoitov 
1360492ecee8SAlexei Starovoitov static void bpf_prog_get_stats(const struct bpf_prog *prog,
1361492ecee8SAlexei Starovoitov 			       struct bpf_prog_stats *stats)
1362492ecee8SAlexei Starovoitov {
1363492ecee8SAlexei Starovoitov 	u64 nsecs = 0, cnt = 0;
1364492ecee8SAlexei Starovoitov 	int cpu;
1365492ecee8SAlexei Starovoitov 
1366492ecee8SAlexei Starovoitov 	for_each_possible_cpu(cpu) {
1367492ecee8SAlexei Starovoitov 		const struct bpf_prog_stats *st;
1368492ecee8SAlexei Starovoitov 		unsigned int start;
1369492ecee8SAlexei Starovoitov 		u64 tnsecs, tcnt;
1370492ecee8SAlexei Starovoitov 
1371492ecee8SAlexei Starovoitov 		st = per_cpu_ptr(prog->aux->stats, cpu);
1372492ecee8SAlexei Starovoitov 		do {
1373492ecee8SAlexei Starovoitov 			start = u64_stats_fetch_begin_irq(&st->syncp);
1374492ecee8SAlexei Starovoitov 			tnsecs = st->nsecs;
1375492ecee8SAlexei Starovoitov 			tcnt = st->cnt;
1376492ecee8SAlexei Starovoitov 		} while (u64_stats_fetch_retry_irq(&st->syncp, start));
1377492ecee8SAlexei Starovoitov 		nsecs += tnsecs;
1378492ecee8SAlexei Starovoitov 		cnt += tcnt;
1379492ecee8SAlexei Starovoitov 	}
1380492ecee8SAlexei Starovoitov 	stats->nsecs = nsecs;
1381492ecee8SAlexei Starovoitov 	stats->cnt = cnt;
1382492ecee8SAlexei Starovoitov }
1383492ecee8SAlexei Starovoitov 
13847bd509e3SDaniel Borkmann #ifdef CONFIG_PROC_FS
13857bd509e3SDaniel Borkmann static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
13867bd509e3SDaniel Borkmann {
13877bd509e3SDaniel Borkmann 	const struct bpf_prog *prog = filp->private_data;
1388f1f7714eSDaniel Borkmann 	char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
1389492ecee8SAlexei Starovoitov 	struct bpf_prog_stats stats;
13907bd509e3SDaniel Borkmann 
1391492ecee8SAlexei Starovoitov 	bpf_prog_get_stats(prog, &stats);
1392f1f7714eSDaniel Borkmann 	bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
13937bd509e3SDaniel Borkmann 	seq_printf(m,
13947bd509e3SDaniel Borkmann 		   "prog_type:\t%u\n"
13957bd509e3SDaniel Borkmann 		   "prog_jited:\t%u\n"
1396f1f7714eSDaniel Borkmann 		   "prog_tag:\t%s\n"
13974316b409SDaniel Borkmann 		   "memlock:\t%llu\n"
1398492ecee8SAlexei Starovoitov 		   "prog_id:\t%u\n"
1399492ecee8SAlexei Starovoitov 		   "run_time_ns:\t%llu\n"
1400492ecee8SAlexei Starovoitov 		   "run_cnt:\t%llu\n",
14017bd509e3SDaniel Borkmann 		   prog->type,
14027bd509e3SDaniel Borkmann 		   prog->jited,
1403f1f7714eSDaniel Borkmann 		   prog_tag,
14044316b409SDaniel Borkmann 		   prog->pages * 1ULL << PAGE_SHIFT,
1405492ecee8SAlexei Starovoitov 		   prog->aux->id,
1406492ecee8SAlexei Starovoitov 		   stats.nsecs,
1407492ecee8SAlexei Starovoitov 		   stats.cnt);
14087bd509e3SDaniel Borkmann }
14097bd509e3SDaniel Borkmann #endif
14107bd509e3SDaniel Borkmann 
1411f66e448cSChenbo Feng const struct file_operations bpf_prog_fops = {
14127bd509e3SDaniel Borkmann #ifdef CONFIG_PROC_FS
14137bd509e3SDaniel Borkmann 	.show_fdinfo	= bpf_prog_show_fdinfo,
14147bd509e3SDaniel Borkmann #endif
141509756af4SAlexei Starovoitov 	.release	= bpf_prog_release,
14166e71b04aSChenbo Feng 	.read		= bpf_dummy_read,
14176e71b04aSChenbo Feng 	.write		= bpf_dummy_write,
141809756af4SAlexei Starovoitov };
141909756af4SAlexei Starovoitov 
1420b2197755SDaniel Borkmann int bpf_prog_new_fd(struct bpf_prog *prog)
1421aa79781bSDaniel Borkmann {
1422afdb09c7SChenbo Feng 	int ret;
1423afdb09c7SChenbo Feng 
1424afdb09c7SChenbo Feng 	ret = security_bpf_prog(prog);
1425afdb09c7SChenbo Feng 	if (ret < 0)
1426afdb09c7SChenbo Feng 		return ret;
1427afdb09c7SChenbo Feng 
1428aa79781bSDaniel Borkmann 	return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1429aa79781bSDaniel Borkmann 				O_RDWR | O_CLOEXEC);
1430aa79781bSDaniel Borkmann }
1431aa79781bSDaniel Borkmann 
1432113214beSDaniel Borkmann static struct bpf_prog *____bpf_prog_get(struct fd f)
143309756af4SAlexei Starovoitov {
143409756af4SAlexei Starovoitov 	if (!f.file)
143509756af4SAlexei Starovoitov 		return ERR_PTR(-EBADF);
143609756af4SAlexei Starovoitov 	if (f.file->f_op != &bpf_prog_fops) {
143709756af4SAlexei Starovoitov 		fdput(f);
143809756af4SAlexei Starovoitov 		return ERR_PTR(-EINVAL);
143909756af4SAlexei Starovoitov 	}
144009756af4SAlexei Starovoitov 
1441c2101297SDaniel Borkmann 	return f.file->private_data;
144209756af4SAlexei Starovoitov }
144309756af4SAlexei Starovoitov 
144459d3656dSBrenden Blanco struct bpf_prog *bpf_prog_add(struct bpf_prog *prog, int i)
144592117d84SAlexei Starovoitov {
144659d3656dSBrenden Blanco 	if (atomic_add_return(i, &prog->aux->refcnt) > BPF_MAX_REFCNT) {
144759d3656dSBrenden Blanco 		atomic_sub(i, &prog->aux->refcnt);
144892117d84SAlexei Starovoitov 		return ERR_PTR(-EBUSY);
144992117d84SAlexei Starovoitov 	}
145092117d84SAlexei Starovoitov 	return prog;
145192117d84SAlexei Starovoitov }
145259d3656dSBrenden Blanco EXPORT_SYMBOL_GPL(bpf_prog_add);
145359d3656dSBrenden Blanco 
1454c540594fSDaniel Borkmann void bpf_prog_sub(struct bpf_prog *prog, int i)
1455c540594fSDaniel Borkmann {
1456c540594fSDaniel Borkmann 	/* Only to be used for undoing previous bpf_prog_add() in some
1457c540594fSDaniel Borkmann 	 * error path. We still know that another entity in our call
1458c540594fSDaniel Borkmann 	 * path holds a reference to the program, thus atomic_sub() can
1459c540594fSDaniel Borkmann 	 * be safely used in such cases!
1460c540594fSDaniel Borkmann 	 */
1461c540594fSDaniel Borkmann 	WARN_ON(atomic_sub_return(i, &prog->aux->refcnt) == 0);
1462c540594fSDaniel Borkmann }
1463c540594fSDaniel Borkmann EXPORT_SYMBOL_GPL(bpf_prog_sub);
1464c540594fSDaniel Borkmann 
146559d3656dSBrenden Blanco struct bpf_prog *bpf_prog_inc(struct bpf_prog *prog)
146659d3656dSBrenden Blanco {
146759d3656dSBrenden Blanco 	return bpf_prog_add(prog, 1);
146859d3656dSBrenden Blanco }
146997bc402dSDaniel Borkmann EXPORT_SYMBOL_GPL(bpf_prog_inc);
147092117d84SAlexei Starovoitov 
1471b16d9aa4SMartin KaFai Lau /* prog_idr_lock should have been held */
1472a6f6df69SJohn Fastabend struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
1473b16d9aa4SMartin KaFai Lau {
1474b16d9aa4SMartin KaFai Lau 	int refold;
1475b16d9aa4SMartin KaFai Lau 
1476bfc18e38SMark Rutland 	refold = atomic_fetch_add_unless(&prog->aux->refcnt, 1, 0);
1477b16d9aa4SMartin KaFai Lau 
1478b16d9aa4SMartin KaFai Lau 	if (refold >= BPF_MAX_REFCNT) {
1479b16d9aa4SMartin KaFai Lau 		__bpf_prog_put(prog, false);
1480b16d9aa4SMartin KaFai Lau 		return ERR_PTR(-EBUSY);
1481b16d9aa4SMartin KaFai Lau 	}
1482b16d9aa4SMartin KaFai Lau 
1483b16d9aa4SMartin KaFai Lau 	if (!refold)
1484b16d9aa4SMartin KaFai Lau 		return ERR_PTR(-ENOENT);
1485b16d9aa4SMartin KaFai Lau 
1486b16d9aa4SMartin KaFai Lau 	return prog;
1487b16d9aa4SMartin KaFai Lau }
1488a6f6df69SJohn Fastabend EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
1489b16d9aa4SMartin KaFai Lau 
1490040ee692SAl Viro bool bpf_prog_get_ok(struct bpf_prog *prog,
1491288b3de5SJakub Kicinski 			    enum bpf_prog_type *attach_type, bool attach_drv)
1492248f346fSJakub Kicinski {
1493288b3de5SJakub Kicinski 	/* not an attachment, just a refcount inc, always allow */
1494288b3de5SJakub Kicinski 	if (!attach_type)
1495288b3de5SJakub Kicinski 		return true;
1496248f346fSJakub Kicinski 
1497248f346fSJakub Kicinski 	if (prog->type != *attach_type)
1498248f346fSJakub Kicinski 		return false;
1499288b3de5SJakub Kicinski 	if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
1500248f346fSJakub Kicinski 		return false;
1501248f346fSJakub Kicinski 
1502248f346fSJakub Kicinski 	return true;
1503248f346fSJakub Kicinski }
1504248f346fSJakub Kicinski 
1505248f346fSJakub Kicinski static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
1506288b3de5SJakub Kicinski 				       bool attach_drv)
150709756af4SAlexei Starovoitov {
150809756af4SAlexei Starovoitov 	struct fd f = fdget(ufd);
150909756af4SAlexei Starovoitov 	struct bpf_prog *prog;
151009756af4SAlexei Starovoitov 
1511113214beSDaniel Borkmann 	prog = ____bpf_prog_get(f);
151209756af4SAlexei Starovoitov 	if (IS_ERR(prog))
151309756af4SAlexei Starovoitov 		return prog;
1514288b3de5SJakub Kicinski 	if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
1515113214beSDaniel Borkmann 		prog = ERR_PTR(-EINVAL);
1516113214beSDaniel Borkmann 		goto out;
1517113214beSDaniel Borkmann 	}
151809756af4SAlexei Starovoitov 
151992117d84SAlexei Starovoitov 	prog = bpf_prog_inc(prog);
1520113214beSDaniel Borkmann out:
152109756af4SAlexei Starovoitov 	fdput(f);
152209756af4SAlexei Starovoitov 	return prog;
152309756af4SAlexei Starovoitov }
1524113214beSDaniel Borkmann 
1525113214beSDaniel Borkmann struct bpf_prog *bpf_prog_get(u32 ufd)
1526113214beSDaniel Borkmann {
1527288b3de5SJakub Kicinski 	return __bpf_prog_get(ufd, NULL, false);
1528113214beSDaniel Borkmann }
1529113214beSDaniel Borkmann 
1530248f346fSJakub Kicinski struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
1531288b3de5SJakub Kicinski 				       bool attach_drv)
1532248f346fSJakub Kicinski {
15334d220ed0SAlexei Starovoitov 	return __bpf_prog_get(ufd, &type, attach_drv);
1534248f346fSJakub Kicinski }
15356c8dfe21SJakub Kicinski EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
1536248f346fSJakub Kicinski 
1537aac3fc32SAndrey Ignatov /* Initially all BPF programs could be loaded w/o specifying
1538aac3fc32SAndrey Ignatov  * expected_attach_type. Later for some of them specifying expected_attach_type
1539aac3fc32SAndrey Ignatov  * at load time became required so that program could be validated properly.
1540aac3fc32SAndrey Ignatov  * Programs of types that are allowed to be loaded both w/ and w/o (for
1541aac3fc32SAndrey Ignatov  * backward compatibility) expected_attach_type, should have the default attach
1542aac3fc32SAndrey Ignatov  * type assigned to expected_attach_type for the latter case, so that it can be
1543aac3fc32SAndrey Ignatov  * validated later at attach time.
1544aac3fc32SAndrey Ignatov  *
1545aac3fc32SAndrey Ignatov  * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1546aac3fc32SAndrey Ignatov  * prog type requires it but has some attach types that have to be backward
1547aac3fc32SAndrey Ignatov  * compatible.
1548aac3fc32SAndrey Ignatov  */
1549aac3fc32SAndrey Ignatov static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1550aac3fc32SAndrey Ignatov {
1551aac3fc32SAndrey Ignatov 	switch (attr->prog_type) {
1552aac3fc32SAndrey Ignatov 	case BPF_PROG_TYPE_CGROUP_SOCK:
1553aac3fc32SAndrey Ignatov 		/* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1554aac3fc32SAndrey Ignatov 		 * exist so checking for non-zero is the way to go here.
1555aac3fc32SAndrey Ignatov 		 */
1556aac3fc32SAndrey Ignatov 		if (!attr->expected_attach_type)
1557aac3fc32SAndrey Ignatov 			attr->expected_attach_type =
1558aac3fc32SAndrey Ignatov 				BPF_CGROUP_INET_SOCK_CREATE;
1559aac3fc32SAndrey Ignatov 		break;
1560aac3fc32SAndrey Ignatov 	}
1561aac3fc32SAndrey Ignatov }
1562aac3fc32SAndrey Ignatov 
15635e43f899SAndrey Ignatov static int
15645e43f899SAndrey Ignatov bpf_prog_load_check_attach_type(enum bpf_prog_type prog_type,
15655e43f899SAndrey Ignatov 				enum bpf_attach_type expected_attach_type)
15665e43f899SAndrey Ignatov {
15674fbac77dSAndrey Ignatov 	switch (prog_type) {
1568aac3fc32SAndrey Ignatov 	case BPF_PROG_TYPE_CGROUP_SOCK:
1569aac3fc32SAndrey Ignatov 		switch (expected_attach_type) {
1570aac3fc32SAndrey Ignatov 		case BPF_CGROUP_INET_SOCK_CREATE:
1571aac3fc32SAndrey Ignatov 		case BPF_CGROUP_INET4_POST_BIND:
1572aac3fc32SAndrey Ignatov 		case BPF_CGROUP_INET6_POST_BIND:
1573aac3fc32SAndrey Ignatov 			return 0;
1574aac3fc32SAndrey Ignatov 		default:
1575aac3fc32SAndrey Ignatov 			return -EINVAL;
1576aac3fc32SAndrey Ignatov 		}
15774fbac77dSAndrey Ignatov 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
15784fbac77dSAndrey Ignatov 		switch (expected_attach_type) {
15794fbac77dSAndrey Ignatov 		case BPF_CGROUP_INET4_BIND:
15804fbac77dSAndrey Ignatov 		case BPF_CGROUP_INET6_BIND:
1581d74bad4eSAndrey Ignatov 		case BPF_CGROUP_INET4_CONNECT:
1582d74bad4eSAndrey Ignatov 		case BPF_CGROUP_INET6_CONNECT:
15831cedee13SAndrey Ignatov 		case BPF_CGROUP_UDP4_SENDMSG:
15841cedee13SAndrey Ignatov 		case BPF_CGROUP_UDP6_SENDMSG:
15855e43f899SAndrey Ignatov 			return 0;
15864fbac77dSAndrey Ignatov 		default:
15874fbac77dSAndrey Ignatov 			return -EINVAL;
15884fbac77dSAndrey Ignatov 		}
15895cf1e914Sbrakmo 	case BPF_PROG_TYPE_CGROUP_SKB:
15905cf1e914Sbrakmo 		switch (expected_attach_type) {
15915cf1e914Sbrakmo 		case BPF_CGROUP_INET_INGRESS:
15925cf1e914Sbrakmo 		case BPF_CGROUP_INET_EGRESS:
15935cf1e914Sbrakmo 			return 0;
15945cf1e914Sbrakmo 		default:
15955cf1e914Sbrakmo 			return -EINVAL;
15965cf1e914Sbrakmo 		}
15974fbac77dSAndrey Ignatov 	default:
15984fbac77dSAndrey Ignatov 		return 0;
15994fbac77dSAndrey Ignatov 	}
16005e43f899SAndrey Ignatov }
16015e43f899SAndrey Ignatov 
160209756af4SAlexei Starovoitov /* last field in 'union bpf_attr' used by this command */
1603c454a46bSMartin KaFai Lau #define	BPF_PROG_LOAD_LAST_FIELD line_info_cnt
160409756af4SAlexei Starovoitov 
1605838e9690SYonghong Song static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
160609756af4SAlexei Starovoitov {
160709756af4SAlexei Starovoitov 	enum bpf_prog_type type = attr->prog_type;
160809756af4SAlexei Starovoitov 	struct bpf_prog *prog;
160909756af4SAlexei Starovoitov 	int err;
161009756af4SAlexei Starovoitov 	char license[128];
161109756af4SAlexei Starovoitov 	bool is_gpl;
161209756af4SAlexei Starovoitov 
161309756af4SAlexei Starovoitov 	if (CHECK_ATTR(BPF_PROG_LOAD))
161409756af4SAlexei Starovoitov 		return -EINVAL;
161509756af4SAlexei Starovoitov 
1616c240eff6SJiong Wang 	if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
1617c240eff6SJiong Wang 				 BPF_F_ANY_ALIGNMENT |
1618c240eff6SJiong Wang 				 BPF_F_TEST_RND_HI32))
1619e07b98d9SDavid S. Miller 		return -EINVAL;
1620e07b98d9SDavid S. Miller 
1621e9ee9efcSDavid Miller 	if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
1622e9ee9efcSDavid Miller 	    (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
1623e9ee9efcSDavid Miller 	    !capable(CAP_SYS_ADMIN))
1624e9ee9efcSDavid Miller 		return -EPERM;
1625e9ee9efcSDavid Miller 
162609756af4SAlexei Starovoitov 	/* copy eBPF program license from user space */
1627535e7b4bSMickaël Salaün 	if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
162809756af4SAlexei Starovoitov 			      sizeof(license) - 1) < 0)
162909756af4SAlexei Starovoitov 		return -EFAULT;
163009756af4SAlexei Starovoitov 	license[sizeof(license) - 1] = 0;
163109756af4SAlexei Starovoitov 
163209756af4SAlexei Starovoitov 	/* eBPF programs must be GPL compatible to use GPL-ed functions */
163309756af4SAlexei Starovoitov 	is_gpl = license_is_gpl_compatible(license);
163409756af4SAlexei Starovoitov 
1635c04c0d2bSAlexei Starovoitov 	if (attr->insn_cnt == 0 ||
1636c04c0d2bSAlexei Starovoitov 	    attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
1637ef0915caSDaniel Borkmann 		return -E2BIG;
163880b7d819SChenbo Feng 	if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
163980b7d819SChenbo Feng 	    type != BPF_PROG_TYPE_CGROUP_SKB &&
164080b7d819SChenbo Feng 	    !capable(CAP_SYS_ADMIN))
16411be7f75dSAlexei Starovoitov 		return -EPERM;
16421be7f75dSAlexei Starovoitov 
1643aac3fc32SAndrey Ignatov 	bpf_prog_load_fixup_attach_type(attr);
16445e43f899SAndrey Ignatov 	if (bpf_prog_load_check_attach_type(type, attr->expected_attach_type))
16455e43f899SAndrey Ignatov 		return -EINVAL;
16465e43f899SAndrey Ignatov 
164709756af4SAlexei Starovoitov 	/* plain bpf_prog allocation */
164809756af4SAlexei Starovoitov 	prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
164909756af4SAlexei Starovoitov 	if (!prog)
165009756af4SAlexei Starovoitov 		return -ENOMEM;
165109756af4SAlexei Starovoitov 
16525e43f899SAndrey Ignatov 	prog->expected_attach_type = attr->expected_attach_type;
16535e43f899SAndrey Ignatov 
16549a18eedbSJakub Kicinski 	prog->aux->offload_requested = !!attr->prog_ifindex;
16559a18eedbSJakub Kicinski 
1656afdb09c7SChenbo Feng 	err = security_bpf_prog_alloc(prog->aux);
1657aaac3ba9SAlexei Starovoitov 	if (err)
1658aaac3ba9SAlexei Starovoitov 		goto free_prog_nouncharge;
1659aaac3ba9SAlexei Starovoitov 
1660afdb09c7SChenbo Feng 	err = bpf_prog_charge_memlock(prog);
1661afdb09c7SChenbo Feng 	if (err)
1662afdb09c7SChenbo Feng 		goto free_prog_sec;
1663afdb09c7SChenbo Feng 
166409756af4SAlexei Starovoitov 	prog->len = attr->insn_cnt;
166509756af4SAlexei Starovoitov 
166609756af4SAlexei Starovoitov 	err = -EFAULT;
1667535e7b4bSMickaël Salaün 	if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
1668aafe6ae9SDaniel Borkmann 			   bpf_prog_insn_size(prog)) != 0)
166909756af4SAlexei Starovoitov 		goto free_prog;
167009756af4SAlexei Starovoitov 
167109756af4SAlexei Starovoitov 	prog->orig_prog = NULL;
1672a91263d5SDaniel Borkmann 	prog->jited = 0;
167309756af4SAlexei Starovoitov 
167409756af4SAlexei Starovoitov 	atomic_set(&prog->aux->refcnt, 1);
1675a91263d5SDaniel Borkmann 	prog->gpl_compatible = is_gpl ? 1 : 0;
167609756af4SAlexei Starovoitov 
16779a18eedbSJakub Kicinski 	if (bpf_prog_is_dev_bound(prog->aux)) {
1678ab3f0063SJakub Kicinski 		err = bpf_prog_offload_init(prog, attr);
1679ab3f0063SJakub Kicinski 		if (err)
1680ab3f0063SJakub Kicinski 			goto free_prog;
1681ab3f0063SJakub Kicinski 	}
1682ab3f0063SJakub Kicinski 
168309756af4SAlexei Starovoitov 	/* find program type: socket_filter vs tracing_filter */
168409756af4SAlexei Starovoitov 	err = find_prog_type(type, prog);
168509756af4SAlexei Starovoitov 	if (err < 0)
168609756af4SAlexei Starovoitov 		goto free_prog;
168709756af4SAlexei Starovoitov 
1688cb4d2b3fSMartin KaFai Lau 	prog->aux->load_time = ktime_get_boot_ns();
1689cb4d2b3fSMartin KaFai Lau 	err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
1690cb4d2b3fSMartin KaFai Lau 	if (err)
1691cb4d2b3fSMartin KaFai Lau 		goto free_prog;
1692cb4d2b3fSMartin KaFai Lau 
169309756af4SAlexei Starovoitov 	/* run eBPF verifier */
1694838e9690SYonghong Song 	err = bpf_check(&prog, attr, uattr);
169509756af4SAlexei Starovoitov 	if (err < 0)
169609756af4SAlexei Starovoitov 		goto free_used_maps;
169709756af4SAlexei Starovoitov 
1698d1c55ab5SDaniel Borkmann 	prog = bpf_prog_select_runtime(prog, &err);
169904fd61abSAlexei Starovoitov 	if (err < 0)
170004fd61abSAlexei Starovoitov 		goto free_used_maps;
170109756af4SAlexei Starovoitov 
1702dc4bb0e2SMartin KaFai Lau 	err = bpf_prog_alloc_id(prog);
1703dc4bb0e2SMartin KaFai Lau 	if (err)
1704dc4bb0e2SMartin KaFai Lau 		goto free_used_maps;
1705dc4bb0e2SMartin KaFai Lau 
1706aa79781bSDaniel Borkmann 	err = bpf_prog_new_fd(prog);
1707b16d9aa4SMartin KaFai Lau 	if (err < 0) {
1708b16d9aa4SMartin KaFai Lau 		/* failed to allocate fd.
1709b16d9aa4SMartin KaFai Lau 		 * bpf_prog_put() is needed because the above
1710b16d9aa4SMartin KaFai Lau 		 * bpf_prog_alloc_id() has published the prog
1711b16d9aa4SMartin KaFai Lau 		 * to the userspace and the userspace may
1712b16d9aa4SMartin KaFai Lau 		 * have refcnt-ed it through BPF_PROG_GET_FD_BY_ID.
1713b16d9aa4SMartin KaFai Lau 		 */
1714b16d9aa4SMartin KaFai Lau 		bpf_prog_put(prog);
1715b16d9aa4SMartin KaFai Lau 		return err;
1716b16d9aa4SMartin KaFai Lau 	}
171709756af4SAlexei Starovoitov 
171874451e66SDaniel Borkmann 	bpf_prog_kallsyms_add(prog);
17196ee52e2aSSong Liu 	perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
172009756af4SAlexei Starovoitov 	return err;
172109756af4SAlexei Starovoitov 
172209756af4SAlexei Starovoitov free_used_maps:
1723c454a46bSMartin KaFai Lau 	bpf_prog_free_linfo(prog);
17245482e9a9SMartin KaFai Lau 	kvfree(prog->aux->func_info);
17255482e9a9SMartin KaFai Lau 	btf_put(prog->aux->btf);
17267d1982b4SDaniel Borkmann 	bpf_prog_kallsyms_del_subprogs(prog);
172709756af4SAlexei Starovoitov 	free_used_maps(prog->aux);
172809756af4SAlexei Starovoitov free_prog:
1729aaac3ba9SAlexei Starovoitov 	bpf_prog_uncharge_memlock(prog);
1730afdb09c7SChenbo Feng free_prog_sec:
1731afdb09c7SChenbo Feng 	security_bpf_prog_free(prog->aux);
1732aaac3ba9SAlexei Starovoitov free_prog_nouncharge:
173309756af4SAlexei Starovoitov 	bpf_prog_free(prog);
173409756af4SAlexei Starovoitov 	return err;
173509756af4SAlexei Starovoitov }
173609756af4SAlexei Starovoitov 
17376e71b04aSChenbo Feng #define BPF_OBJ_LAST_FIELD file_flags
1738b2197755SDaniel Borkmann 
1739b2197755SDaniel Borkmann static int bpf_obj_pin(const union bpf_attr *attr)
1740b2197755SDaniel Borkmann {
17416e71b04aSChenbo Feng 	if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
1742b2197755SDaniel Borkmann 		return -EINVAL;
1743b2197755SDaniel Borkmann 
1744535e7b4bSMickaël Salaün 	return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
1745b2197755SDaniel Borkmann }
1746b2197755SDaniel Borkmann 
1747b2197755SDaniel Borkmann static int bpf_obj_get(const union bpf_attr *attr)
1748b2197755SDaniel Borkmann {
17496e71b04aSChenbo Feng 	if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
17506e71b04aSChenbo Feng 	    attr->file_flags & ~BPF_OBJ_FLAG_MASK)
1751b2197755SDaniel Borkmann 		return -EINVAL;
1752b2197755SDaniel Borkmann 
17536e71b04aSChenbo Feng 	return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
17546e71b04aSChenbo Feng 				attr->file_flags);
1755b2197755SDaniel Borkmann }
1756b2197755SDaniel Borkmann 
1757c4f6699dSAlexei Starovoitov struct bpf_raw_tracepoint {
1758c4f6699dSAlexei Starovoitov 	struct bpf_raw_event_map *btp;
1759c4f6699dSAlexei Starovoitov 	struct bpf_prog *prog;
1760c4f6699dSAlexei Starovoitov };
1761c4f6699dSAlexei Starovoitov 
1762c4f6699dSAlexei Starovoitov static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
1763c4f6699dSAlexei Starovoitov {
1764c4f6699dSAlexei Starovoitov 	struct bpf_raw_tracepoint *raw_tp = filp->private_data;
1765c4f6699dSAlexei Starovoitov 
1766c4f6699dSAlexei Starovoitov 	if (raw_tp->prog) {
1767c4f6699dSAlexei Starovoitov 		bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
1768c4f6699dSAlexei Starovoitov 		bpf_prog_put(raw_tp->prog);
1769c4f6699dSAlexei Starovoitov 	}
1770a38d1107SMatt Mullins 	bpf_put_raw_tracepoint(raw_tp->btp);
1771c4f6699dSAlexei Starovoitov 	kfree(raw_tp);
1772c4f6699dSAlexei Starovoitov 	return 0;
1773c4f6699dSAlexei Starovoitov }
1774c4f6699dSAlexei Starovoitov 
1775c4f6699dSAlexei Starovoitov static const struct file_operations bpf_raw_tp_fops = {
1776c4f6699dSAlexei Starovoitov 	.release	= bpf_raw_tracepoint_release,
1777c4f6699dSAlexei Starovoitov 	.read		= bpf_dummy_read,
1778c4f6699dSAlexei Starovoitov 	.write		= bpf_dummy_write,
1779c4f6699dSAlexei Starovoitov };
1780c4f6699dSAlexei Starovoitov 
1781c4f6699dSAlexei Starovoitov #define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
1782c4f6699dSAlexei Starovoitov 
1783c4f6699dSAlexei Starovoitov static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
1784c4f6699dSAlexei Starovoitov {
1785c4f6699dSAlexei Starovoitov 	struct bpf_raw_tracepoint *raw_tp;
1786c4f6699dSAlexei Starovoitov 	struct bpf_raw_event_map *btp;
1787c4f6699dSAlexei Starovoitov 	struct bpf_prog *prog;
1788c4f6699dSAlexei Starovoitov 	char tp_name[128];
1789c4f6699dSAlexei Starovoitov 	int tp_fd, err;
1790c4f6699dSAlexei Starovoitov 
1791c4f6699dSAlexei Starovoitov 	if (strncpy_from_user(tp_name, u64_to_user_ptr(attr->raw_tracepoint.name),
1792c4f6699dSAlexei Starovoitov 			      sizeof(tp_name) - 1) < 0)
1793c4f6699dSAlexei Starovoitov 		return -EFAULT;
1794c4f6699dSAlexei Starovoitov 	tp_name[sizeof(tp_name) - 1] = 0;
1795c4f6699dSAlexei Starovoitov 
1796a38d1107SMatt Mullins 	btp = bpf_get_raw_tracepoint(tp_name);
1797c4f6699dSAlexei Starovoitov 	if (!btp)
1798c4f6699dSAlexei Starovoitov 		return -ENOENT;
1799c4f6699dSAlexei Starovoitov 
1800c4f6699dSAlexei Starovoitov 	raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
1801a38d1107SMatt Mullins 	if (!raw_tp) {
1802a38d1107SMatt Mullins 		err = -ENOMEM;
1803a38d1107SMatt Mullins 		goto out_put_btp;
1804a38d1107SMatt Mullins 	}
1805c4f6699dSAlexei Starovoitov 	raw_tp->btp = btp;
1806c4f6699dSAlexei Starovoitov 
18079df1c28bSMatt Mullins 	prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
1808c4f6699dSAlexei Starovoitov 	if (IS_ERR(prog)) {
1809c4f6699dSAlexei Starovoitov 		err = PTR_ERR(prog);
1810c4f6699dSAlexei Starovoitov 		goto out_free_tp;
1811c4f6699dSAlexei Starovoitov 	}
18129df1c28bSMatt Mullins 	if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
18139df1c28bSMatt Mullins 	    prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
18149df1c28bSMatt Mullins 		err = -EINVAL;
18159df1c28bSMatt Mullins 		goto out_put_prog;
18169df1c28bSMatt Mullins 	}
1817c4f6699dSAlexei Starovoitov 
1818c4f6699dSAlexei Starovoitov 	err = bpf_probe_register(raw_tp->btp, prog);
1819c4f6699dSAlexei Starovoitov 	if (err)
1820c4f6699dSAlexei Starovoitov 		goto out_put_prog;
1821c4f6699dSAlexei Starovoitov 
1822c4f6699dSAlexei Starovoitov 	raw_tp->prog = prog;
1823c4f6699dSAlexei Starovoitov 	tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
1824c4f6699dSAlexei Starovoitov 				 O_CLOEXEC);
1825c4f6699dSAlexei Starovoitov 	if (tp_fd < 0) {
1826c4f6699dSAlexei Starovoitov 		bpf_probe_unregister(raw_tp->btp, prog);
1827c4f6699dSAlexei Starovoitov 		err = tp_fd;
1828c4f6699dSAlexei Starovoitov 		goto out_put_prog;
1829c4f6699dSAlexei Starovoitov 	}
1830c4f6699dSAlexei Starovoitov 	return tp_fd;
1831c4f6699dSAlexei Starovoitov 
1832c4f6699dSAlexei Starovoitov out_put_prog:
1833c4f6699dSAlexei Starovoitov 	bpf_prog_put(prog);
1834c4f6699dSAlexei Starovoitov out_free_tp:
1835c4f6699dSAlexei Starovoitov 	kfree(raw_tp);
1836a38d1107SMatt Mullins out_put_btp:
1837a38d1107SMatt Mullins 	bpf_put_raw_tracepoint(btp);
1838c4f6699dSAlexei Starovoitov 	return err;
1839c4f6699dSAlexei Starovoitov }
1840c4f6699dSAlexei Starovoitov 
184133491588SAnders Roxell static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
184233491588SAnders Roxell 					     enum bpf_attach_type attach_type)
184333491588SAnders Roxell {
184433491588SAnders Roxell 	switch (prog->type) {
184533491588SAnders Roxell 	case BPF_PROG_TYPE_CGROUP_SOCK:
184633491588SAnders Roxell 	case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
184733491588SAnders Roxell 		return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
18485cf1e914Sbrakmo 	case BPF_PROG_TYPE_CGROUP_SKB:
18495cf1e914Sbrakmo 		return prog->enforce_expected_attach_type &&
18505cf1e914Sbrakmo 			prog->expected_attach_type != attach_type ?
18515cf1e914Sbrakmo 			-EINVAL : 0;
185233491588SAnders Roxell 	default:
185333491588SAnders Roxell 		return 0;
185433491588SAnders Roxell 	}
185533491588SAnders Roxell }
185633491588SAnders Roxell 
1857464bc0fdSJohn Fastabend #define BPF_PROG_ATTACH_LAST_FIELD attach_flags
1858174a79ffSJohn Fastabend 
1859324bda9eSAlexei Starovoitov #define BPF_F_ATTACH_MASK \
1860324bda9eSAlexei Starovoitov 	(BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI)
1861324bda9eSAlexei Starovoitov 
1862f4324551SDaniel Mack static int bpf_prog_attach(const union bpf_attr *attr)
1863f4324551SDaniel Mack {
18647f677633SAlexei Starovoitov 	enum bpf_prog_type ptype;
1865f4324551SDaniel Mack 	struct bpf_prog *prog;
18667f677633SAlexei Starovoitov 	int ret;
1867f4324551SDaniel Mack 
1868f4324551SDaniel Mack 	if (!capable(CAP_NET_ADMIN))
1869f4324551SDaniel Mack 		return -EPERM;
1870f4324551SDaniel Mack 
1871f4324551SDaniel Mack 	if (CHECK_ATTR(BPF_PROG_ATTACH))
1872f4324551SDaniel Mack 		return -EINVAL;
1873f4324551SDaniel Mack 
1874324bda9eSAlexei Starovoitov 	if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
18757f677633SAlexei Starovoitov 		return -EINVAL;
18767f677633SAlexei Starovoitov 
1877f4324551SDaniel Mack 	switch (attr->attach_type) {
1878f4324551SDaniel Mack 	case BPF_CGROUP_INET_INGRESS:
1879f4324551SDaniel Mack 	case BPF_CGROUP_INET_EGRESS:
1880b2cd1257SDavid Ahern 		ptype = BPF_PROG_TYPE_CGROUP_SKB;
1881b2cd1257SDavid Ahern 		break;
188261023658SDavid Ahern 	case BPF_CGROUP_INET_SOCK_CREATE:
1883aac3fc32SAndrey Ignatov 	case BPF_CGROUP_INET4_POST_BIND:
1884aac3fc32SAndrey Ignatov 	case BPF_CGROUP_INET6_POST_BIND:
188561023658SDavid Ahern 		ptype = BPF_PROG_TYPE_CGROUP_SOCK;
188661023658SDavid Ahern 		break;
18874fbac77dSAndrey Ignatov 	case BPF_CGROUP_INET4_BIND:
18884fbac77dSAndrey Ignatov 	case BPF_CGROUP_INET6_BIND:
1889d74bad4eSAndrey Ignatov 	case BPF_CGROUP_INET4_CONNECT:
1890d74bad4eSAndrey Ignatov 	case BPF_CGROUP_INET6_CONNECT:
18911cedee13SAndrey Ignatov 	case BPF_CGROUP_UDP4_SENDMSG:
18921cedee13SAndrey Ignatov 	case BPF_CGROUP_UDP6_SENDMSG:
18934fbac77dSAndrey Ignatov 		ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
18944fbac77dSAndrey Ignatov 		break;
189540304b2aSLawrence Brakmo 	case BPF_CGROUP_SOCK_OPS:
189640304b2aSLawrence Brakmo 		ptype = BPF_PROG_TYPE_SOCK_OPS;
189740304b2aSLawrence Brakmo 		break;
1898ebc614f6SRoman Gushchin 	case BPF_CGROUP_DEVICE:
1899ebc614f6SRoman Gushchin 		ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1900ebc614f6SRoman Gushchin 		break;
19014f738adbSJohn Fastabend 	case BPF_SK_MSG_VERDICT:
1902fdb5c453SSean Young 		ptype = BPF_PROG_TYPE_SK_MSG;
1903fdb5c453SSean Young 		break;
1904464bc0fdSJohn Fastabend 	case BPF_SK_SKB_STREAM_PARSER:
1905464bc0fdSJohn Fastabend 	case BPF_SK_SKB_STREAM_VERDICT:
1906fdb5c453SSean Young 		ptype = BPF_PROG_TYPE_SK_SKB;
1907fdb5c453SSean Young 		break;
1908f4364dcfSSean Young 	case BPF_LIRC_MODE2:
1909fdb5c453SSean Young 		ptype = BPF_PROG_TYPE_LIRC_MODE2;
1910fdb5c453SSean Young 		break;
1911d58e468bSPetar Penkov 	case BPF_FLOW_DISSECTOR:
1912d58e468bSPetar Penkov 		ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
1913d58e468bSPetar Penkov 		break;
19147b146cebSAndrey Ignatov 	case BPF_CGROUP_SYSCTL:
19157b146cebSAndrey Ignatov 		ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
19167b146cebSAndrey Ignatov 		break;
1917b2cd1257SDavid Ahern 	default:
1918b2cd1257SDavid Ahern 		return -EINVAL;
1919b2cd1257SDavid Ahern 	}
1920b2cd1257SDavid Ahern 
1921b2cd1257SDavid Ahern 	prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
1922f4324551SDaniel Mack 	if (IS_ERR(prog))
1923f4324551SDaniel Mack 		return PTR_ERR(prog);
1924f4324551SDaniel Mack 
19255e43f899SAndrey Ignatov 	if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
19265e43f899SAndrey Ignatov 		bpf_prog_put(prog);
19275e43f899SAndrey Ignatov 		return -EINVAL;
19285e43f899SAndrey Ignatov 	}
19295e43f899SAndrey Ignatov 
1930fdb5c453SSean Young 	switch (ptype) {
1931fdb5c453SSean Young 	case BPF_PROG_TYPE_SK_SKB:
1932fdb5c453SSean Young 	case BPF_PROG_TYPE_SK_MSG:
1933604326b4SDaniel Borkmann 		ret = sock_map_get_from_fd(attr, prog);
1934fdb5c453SSean Young 		break;
1935fdb5c453SSean Young 	case BPF_PROG_TYPE_LIRC_MODE2:
1936fdb5c453SSean Young 		ret = lirc_prog_attach(attr, prog);
1937fdb5c453SSean Young 		break;
1938d58e468bSPetar Penkov 	case BPF_PROG_TYPE_FLOW_DISSECTOR:
1939d58e468bSPetar Penkov 		ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
1940d58e468bSPetar Penkov 		break;
1941fdb5c453SSean Young 	default:
1942fdb5c453SSean Young 		ret = cgroup_bpf_prog_attach(attr, ptype, prog);
1943f4324551SDaniel Mack 	}
1944f4324551SDaniel Mack 
19457f677633SAlexei Starovoitov 	if (ret)
19467f677633SAlexei Starovoitov 		bpf_prog_put(prog);
19477f677633SAlexei Starovoitov 	return ret;
1948f4324551SDaniel Mack }
1949f4324551SDaniel Mack 
1950f4324551SDaniel Mack #define BPF_PROG_DETACH_LAST_FIELD attach_type
1951f4324551SDaniel Mack 
1952f4324551SDaniel Mack static int bpf_prog_detach(const union bpf_attr *attr)
1953f4324551SDaniel Mack {
1954324bda9eSAlexei Starovoitov 	enum bpf_prog_type ptype;
1955f4324551SDaniel Mack 
1956f4324551SDaniel Mack 	if (!capable(CAP_NET_ADMIN))
1957f4324551SDaniel Mack 		return -EPERM;
1958f4324551SDaniel Mack 
1959f4324551SDaniel Mack 	if (CHECK_ATTR(BPF_PROG_DETACH))
1960f4324551SDaniel Mack 		return -EINVAL;
1961f4324551SDaniel Mack 
1962f4324551SDaniel Mack 	switch (attr->attach_type) {
1963f4324551SDaniel Mack 	case BPF_CGROUP_INET_INGRESS:
1964f4324551SDaniel Mack 	case BPF_CGROUP_INET_EGRESS:
1965324bda9eSAlexei Starovoitov 		ptype = BPF_PROG_TYPE_CGROUP_SKB;
1966324bda9eSAlexei Starovoitov 		break;
196761023658SDavid Ahern 	case BPF_CGROUP_INET_SOCK_CREATE:
1968aac3fc32SAndrey Ignatov 	case BPF_CGROUP_INET4_POST_BIND:
1969aac3fc32SAndrey Ignatov 	case BPF_CGROUP_INET6_POST_BIND:
1970324bda9eSAlexei Starovoitov 		ptype = BPF_PROG_TYPE_CGROUP_SOCK;
1971324bda9eSAlexei Starovoitov 		break;
19724fbac77dSAndrey Ignatov 	case BPF_CGROUP_INET4_BIND:
19734fbac77dSAndrey Ignatov 	case BPF_CGROUP_INET6_BIND:
1974d74bad4eSAndrey Ignatov 	case BPF_CGROUP_INET4_CONNECT:
1975d74bad4eSAndrey Ignatov 	case BPF_CGROUP_INET6_CONNECT:
19761cedee13SAndrey Ignatov 	case BPF_CGROUP_UDP4_SENDMSG:
19771cedee13SAndrey Ignatov 	case BPF_CGROUP_UDP6_SENDMSG:
19784fbac77dSAndrey Ignatov 		ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
19794fbac77dSAndrey Ignatov 		break;
198040304b2aSLawrence Brakmo 	case BPF_CGROUP_SOCK_OPS:
1981324bda9eSAlexei Starovoitov 		ptype = BPF_PROG_TYPE_SOCK_OPS;
1982f4324551SDaniel Mack 		break;
1983ebc614f6SRoman Gushchin 	case BPF_CGROUP_DEVICE:
1984ebc614f6SRoman Gushchin 		ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
1985ebc614f6SRoman Gushchin 		break;
19864f738adbSJohn Fastabend 	case BPF_SK_MSG_VERDICT:
1987604326b4SDaniel Borkmann 		return sock_map_get_from_fd(attr, NULL);
19885a67da2aSJohn Fastabend 	case BPF_SK_SKB_STREAM_PARSER:
19895a67da2aSJohn Fastabend 	case BPF_SK_SKB_STREAM_VERDICT:
1990604326b4SDaniel Borkmann 		return sock_map_get_from_fd(attr, NULL);
1991f4364dcfSSean Young 	case BPF_LIRC_MODE2:
1992f4364dcfSSean Young 		return lirc_prog_detach(attr);
1993d58e468bSPetar Penkov 	case BPF_FLOW_DISSECTOR:
1994d58e468bSPetar Penkov 		return skb_flow_dissector_bpf_prog_detach(attr);
19957b146cebSAndrey Ignatov 	case BPF_CGROUP_SYSCTL:
19967b146cebSAndrey Ignatov 		ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
19977b146cebSAndrey Ignatov 		break;
1998f4324551SDaniel Mack 	default:
1999f4324551SDaniel Mack 		return -EINVAL;
2000f4324551SDaniel Mack 	}
2001f4324551SDaniel Mack 
2002fdb5c453SSean Young 	return cgroup_bpf_prog_detach(attr, ptype);
2003f4324551SDaniel Mack }
200440304b2aSLawrence Brakmo 
2005468e2f64SAlexei Starovoitov #define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2006468e2f64SAlexei Starovoitov 
2007468e2f64SAlexei Starovoitov static int bpf_prog_query(const union bpf_attr *attr,
2008468e2f64SAlexei Starovoitov 			  union bpf_attr __user *uattr)
2009468e2f64SAlexei Starovoitov {
2010468e2f64SAlexei Starovoitov 	if (!capable(CAP_NET_ADMIN))
2011468e2f64SAlexei Starovoitov 		return -EPERM;
2012468e2f64SAlexei Starovoitov 	if (CHECK_ATTR(BPF_PROG_QUERY))
2013468e2f64SAlexei Starovoitov 		return -EINVAL;
2014468e2f64SAlexei Starovoitov 	if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2015468e2f64SAlexei Starovoitov 		return -EINVAL;
2016468e2f64SAlexei Starovoitov 
2017468e2f64SAlexei Starovoitov 	switch (attr->query.attach_type) {
2018468e2f64SAlexei Starovoitov 	case BPF_CGROUP_INET_INGRESS:
2019468e2f64SAlexei Starovoitov 	case BPF_CGROUP_INET_EGRESS:
2020468e2f64SAlexei Starovoitov 	case BPF_CGROUP_INET_SOCK_CREATE:
20214fbac77dSAndrey Ignatov 	case BPF_CGROUP_INET4_BIND:
20224fbac77dSAndrey Ignatov 	case BPF_CGROUP_INET6_BIND:
2023aac3fc32SAndrey Ignatov 	case BPF_CGROUP_INET4_POST_BIND:
2024aac3fc32SAndrey Ignatov 	case BPF_CGROUP_INET6_POST_BIND:
2025d74bad4eSAndrey Ignatov 	case BPF_CGROUP_INET4_CONNECT:
2026d74bad4eSAndrey Ignatov 	case BPF_CGROUP_INET6_CONNECT:
20271cedee13SAndrey Ignatov 	case BPF_CGROUP_UDP4_SENDMSG:
20281cedee13SAndrey Ignatov 	case BPF_CGROUP_UDP6_SENDMSG:
2029468e2f64SAlexei Starovoitov 	case BPF_CGROUP_SOCK_OPS:
2030ebc614f6SRoman Gushchin 	case BPF_CGROUP_DEVICE:
20317b146cebSAndrey Ignatov 	case BPF_CGROUP_SYSCTL:
2032468e2f64SAlexei Starovoitov 		break;
2033f4364dcfSSean Young 	case BPF_LIRC_MODE2:
2034f4364dcfSSean Young 		return lirc_prog_query(attr, uattr);
2035118c8e9aSStanislav Fomichev 	case BPF_FLOW_DISSECTOR:
2036118c8e9aSStanislav Fomichev 		return skb_flow_dissector_prog_query(attr, uattr);
2037468e2f64SAlexei Starovoitov 	default:
2038468e2f64SAlexei Starovoitov 		return -EINVAL;
2039468e2f64SAlexei Starovoitov 	}
2040fdb5c453SSean Young 
2041fdb5c453SSean Young 	return cgroup_bpf_prog_query(attr, uattr);
2042468e2f64SAlexei Starovoitov }
2043f4324551SDaniel Mack 
2044b0b9395dSStanislav Fomichev #define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
20451cf1cae9SAlexei Starovoitov 
20461cf1cae9SAlexei Starovoitov static int bpf_prog_test_run(const union bpf_attr *attr,
20471cf1cae9SAlexei Starovoitov 			     union bpf_attr __user *uattr)
20481cf1cae9SAlexei Starovoitov {
20491cf1cae9SAlexei Starovoitov 	struct bpf_prog *prog;
20501cf1cae9SAlexei Starovoitov 	int ret = -ENOTSUPP;
20511cf1cae9SAlexei Starovoitov 
205261f3c964SAlexei Starovoitov 	if (!capable(CAP_SYS_ADMIN))
205361f3c964SAlexei Starovoitov 		return -EPERM;
20541cf1cae9SAlexei Starovoitov 	if (CHECK_ATTR(BPF_PROG_TEST_RUN))
20551cf1cae9SAlexei Starovoitov 		return -EINVAL;
20561cf1cae9SAlexei Starovoitov 
2057b0b9395dSStanislav Fomichev 	if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2058b0b9395dSStanislav Fomichev 	    (!attr->test.ctx_size_in && attr->test.ctx_in))
2059b0b9395dSStanislav Fomichev 		return -EINVAL;
2060b0b9395dSStanislav Fomichev 
2061b0b9395dSStanislav Fomichev 	if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2062b0b9395dSStanislav Fomichev 	    (!attr->test.ctx_size_out && attr->test.ctx_out))
2063b0b9395dSStanislav Fomichev 		return -EINVAL;
2064b0b9395dSStanislav Fomichev 
20651cf1cae9SAlexei Starovoitov 	prog = bpf_prog_get(attr->test.prog_fd);
20661cf1cae9SAlexei Starovoitov 	if (IS_ERR(prog))
20671cf1cae9SAlexei Starovoitov 		return PTR_ERR(prog);
20681cf1cae9SAlexei Starovoitov 
20691cf1cae9SAlexei Starovoitov 	if (prog->aux->ops->test_run)
20701cf1cae9SAlexei Starovoitov 		ret = prog->aux->ops->test_run(prog, attr, uattr);
20711cf1cae9SAlexei Starovoitov 
20721cf1cae9SAlexei Starovoitov 	bpf_prog_put(prog);
20731cf1cae9SAlexei Starovoitov 	return ret;
20741cf1cae9SAlexei Starovoitov }
20751cf1cae9SAlexei Starovoitov 
207634ad5580SMartin KaFai Lau #define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
207734ad5580SMartin KaFai Lau 
207834ad5580SMartin KaFai Lau static int bpf_obj_get_next_id(const union bpf_attr *attr,
207934ad5580SMartin KaFai Lau 			       union bpf_attr __user *uattr,
208034ad5580SMartin KaFai Lau 			       struct idr *idr,
208134ad5580SMartin KaFai Lau 			       spinlock_t *lock)
208234ad5580SMartin KaFai Lau {
208334ad5580SMartin KaFai Lau 	u32 next_id = attr->start_id;
208434ad5580SMartin KaFai Lau 	int err = 0;
208534ad5580SMartin KaFai Lau 
208634ad5580SMartin KaFai Lau 	if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
208734ad5580SMartin KaFai Lau 		return -EINVAL;
208834ad5580SMartin KaFai Lau 
208934ad5580SMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN))
209034ad5580SMartin KaFai Lau 		return -EPERM;
209134ad5580SMartin KaFai Lau 
209234ad5580SMartin KaFai Lau 	next_id++;
209334ad5580SMartin KaFai Lau 	spin_lock_bh(lock);
209434ad5580SMartin KaFai Lau 	if (!idr_get_next(idr, &next_id))
209534ad5580SMartin KaFai Lau 		err = -ENOENT;
209634ad5580SMartin KaFai Lau 	spin_unlock_bh(lock);
209734ad5580SMartin KaFai Lau 
209834ad5580SMartin KaFai Lau 	if (!err)
209934ad5580SMartin KaFai Lau 		err = put_user(next_id, &uattr->next_id);
210034ad5580SMartin KaFai Lau 
210134ad5580SMartin KaFai Lau 	return err;
210234ad5580SMartin KaFai Lau }
210334ad5580SMartin KaFai Lau 
2104b16d9aa4SMartin KaFai Lau #define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2105b16d9aa4SMartin KaFai Lau 
2106b16d9aa4SMartin KaFai Lau static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2107b16d9aa4SMartin KaFai Lau {
2108b16d9aa4SMartin KaFai Lau 	struct bpf_prog *prog;
2109b16d9aa4SMartin KaFai Lau 	u32 id = attr->prog_id;
2110b16d9aa4SMartin KaFai Lau 	int fd;
2111b16d9aa4SMartin KaFai Lau 
2112b16d9aa4SMartin KaFai Lau 	if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2113b16d9aa4SMartin KaFai Lau 		return -EINVAL;
2114b16d9aa4SMartin KaFai Lau 
2115b16d9aa4SMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN))
2116b16d9aa4SMartin KaFai Lau 		return -EPERM;
2117b16d9aa4SMartin KaFai Lau 
2118b16d9aa4SMartin KaFai Lau 	spin_lock_bh(&prog_idr_lock);
2119b16d9aa4SMartin KaFai Lau 	prog = idr_find(&prog_idr, id);
2120b16d9aa4SMartin KaFai Lau 	if (prog)
2121b16d9aa4SMartin KaFai Lau 		prog = bpf_prog_inc_not_zero(prog);
2122b16d9aa4SMartin KaFai Lau 	else
2123b16d9aa4SMartin KaFai Lau 		prog = ERR_PTR(-ENOENT);
2124b16d9aa4SMartin KaFai Lau 	spin_unlock_bh(&prog_idr_lock);
2125b16d9aa4SMartin KaFai Lau 
2126b16d9aa4SMartin KaFai Lau 	if (IS_ERR(prog))
2127b16d9aa4SMartin KaFai Lau 		return PTR_ERR(prog);
2128b16d9aa4SMartin KaFai Lau 
2129b16d9aa4SMartin KaFai Lau 	fd = bpf_prog_new_fd(prog);
2130b16d9aa4SMartin KaFai Lau 	if (fd < 0)
2131b16d9aa4SMartin KaFai Lau 		bpf_prog_put(prog);
2132b16d9aa4SMartin KaFai Lau 
2133b16d9aa4SMartin KaFai Lau 	return fd;
2134b16d9aa4SMartin KaFai Lau }
2135b16d9aa4SMartin KaFai Lau 
21366e71b04aSChenbo Feng #define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
2137bd5f5f4eSMartin KaFai Lau 
2138bd5f5f4eSMartin KaFai Lau static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2139bd5f5f4eSMartin KaFai Lau {
2140bd5f5f4eSMartin KaFai Lau 	struct bpf_map *map;
2141bd5f5f4eSMartin KaFai Lau 	u32 id = attr->map_id;
21426e71b04aSChenbo Feng 	int f_flags;
2143bd5f5f4eSMartin KaFai Lau 	int fd;
2144bd5f5f4eSMartin KaFai Lau 
21456e71b04aSChenbo Feng 	if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
21466e71b04aSChenbo Feng 	    attr->open_flags & ~BPF_OBJ_FLAG_MASK)
2147bd5f5f4eSMartin KaFai Lau 		return -EINVAL;
2148bd5f5f4eSMartin KaFai Lau 
2149bd5f5f4eSMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN))
2150bd5f5f4eSMartin KaFai Lau 		return -EPERM;
2151bd5f5f4eSMartin KaFai Lau 
21526e71b04aSChenbo Feng 	f_flags = bpf_get_file_flag(attr->open_flags);
21536e71b04aSChenbo Feng 	if (f_flags < 0)
21546e71b04aSChenbo Feng 		return f_flags;
21556e71b04aSChenbo Feng 
2156bd5f5f4eSMartin KaFai Lau 	spin_lock_bh(&map_idr_lock);
2157bd5f5f4eSMartin KaFai Lau 	map = idr_find(&map_idr, id);
2158bd5f5f4eSMartin KaFai Lau 	if (map)
2159bd5f5f4eSMartin KaFai Lau 		map = bpf_map_inc_not_zero(map, true);
2160bd5f5f4eSMartin KaFai Lau 	else
2161bd5f5f4eSMartin KaFai Lau 		map = ERR_PTR(-ENOENT);
2162bd5f5f4eSMartin KaFai Lau 	spin_unlock_bh(&map_idr_lock);
2163bd5f5f4eSMartin KaFai Lau 
2164bd5f5f4eSMartin KaFai Lau 	if (IS_ERR(map))
2165bd5f5f4eSMartin KaFai Lau 		return PTR_ERR(map);
2166bd5f5f4eSMartin KaFai Lau 
21676e71b04aSChenbo Feng 	fd = bpf_map_new_fd(map, f_flags);
2168bd5f5f4eSMartin KaFai Lau 	if (fd < 0)
2169781e6282SPeng Sun 		bpf_map_put_with_uref(map);
2170bd5f5f4eSMartin KaFai Lau 
2171bd5f5f4eSMartin KaFai Lau 	return fd;
2172bd5f5f4eSMartin KaFai Lau }
2173bd5f5f4eSMartin KaFai Lau 
21747105e828SDaniel Borkmann static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
2175d8eca5bbSDaniel Borkmann 					      unsigned long addr, u32 *off,
2176d8eca5bbSDaniel Borkmann 					      u32 *type)
21777105e828SDaniel Borkmann {
2178d8eca5bbSDaniel Borkmann 	const struct bpf_map *map;
21797105e828SDaniel Borkmann 	int i;
21807105e828SDaniel Borkmann 
2181d8eca5bbSDaniel Borkmann 	for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2182d8eca5bbSDaniel Borkmann 		map = prog->aux->used_maps[i];
2183d8eca5bbSDaniel Borkmann 		if (map == (void *)addr) {
2184d8eca5bbSDaniel Borkmann 			*type = BPF_PSEUDO_MAP_FD;
2185d8eca5bbSDaniel Borkmann 			return map;
2186d8eca5bbSDaniel Borkmann 		}
2187d8eca5bbSDaniel Borkmann 		if (!map->ops->map_direct_value_meta)
2188d8eca5bbSDaniel Borkmann 			continue;
2189d8eca5bbSDaniel Borkmann 		if (!map->ops->map_direct_value_meta(map, addr, off)) {
2190d8eca5bbSDaniel Borkmann 			*type = BPF_PSEUDO_MAP_VALUE;
2191d8eca5bbSDaniel Borkmann 			return map;
2192d8eca5bbSDaniel Borkmann 		}
2193d8eca5bbSDaniel Borkmann 	}
2194d8eca5bbSDaniel Borkmann 
21957105e828SDaniel Borkmann 	return NULL;
21967105e828SDaniel Borkmann }
21977105e828SDaniel Borkmann 
21987105e828SDaniel Borkmann static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
21997105e828SDaniel Borkmann {
22007105e828SDaniel Borkmann 	const struct bpf_map *map;
22017105e828SDaniel Borkmann 	struct bpf_insn *insns;
2202d8eca5bbSDaniel Borkmann 	u32 off, type;
22037105e828SDaniel Borkmann 	u64 imm;
22047105e828SDaniel Borkmann 	int i;
22057105e828SDaniel Borkmann 
22067105e828SDaniel Borkmann 	insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
22077105e828SDaniel Borkmann 			GFP_USER);
22087105e828SDaniel Borkmann 	if (!insns)
22097105e828SDaniel Borkmann 		return insns;
22107105e828SDaniel Borkmann 
22117105e828SDaniel Borkmann 	for (i = 0; i < prog->len; i++) {
22127105e828SDaniel Borkmann 		if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
22137105e828SDaniel Borkmann 			insns[i].code = BPF_JMP | BPF_CALL;
22147105e828SDaniel Borkmann 			insns[i].imm = BPF_FUNC_tail_call;
22157105e828SDaniel Borkmann 			/* fall-through */
22167105e828SDaniel Borkmann 		}
22177105e828SDaniel Borkmann 		if (insns[i].code == (BPF_JMP | BPF_CALL) ||
22187105e828SDaniel Borkmann 		    insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
22197105e828SDaniel Borkmann 			if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
22207105e828SDaniel Borkmann 				insns[i].code = BPF_JMP | BPF_CALL;
22217105e828SDaniel Borkmann 			if (!bpf_dump_raw_ok())
22227105e828SDaniel Borkmann 				insns[i].imm = 0;
22237105e828SDaniel Borkmann 			continue;
22247105e828SDaniel Borkmann 		}
22257105e828SDaniel Borkmann 
22267105e828SDaniel Borkmann 		if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
22277105e828SDaniel Borkmann 			continue;
22287105e828SDaniel Borkmann 
22297105e828SDaniel Borkmann 		imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
2230d8eca5bbSDaniel Borkmann 		map = bpf_map_from_imm(prog, imm, &off, &type);
22317105e828SDaniel Borkmann 		if (map) {
2232d8eca5bbSDaniel Borkmann 			insns[i].src_reg = type;
22337105e828SDaniel Borkmann 			insns[i].imm = map->id;
2234d8eca5bbSDaniel Borkmann 			insns[i + 1].imm = off;
22357105e828SDaniel Borkmann 			continue;
22367105e828SDaniel Borkmann 		}
22377105e828SDaniel Borkmann 	}
22387105e828SDaniel Borkmann 
22397105e828SDaniel Borkmann 	return insns;
22407105e828SDaniel Borkmann }
22417105e828SDaniel Borkmann 
2242c454a46bSMartin KaFai Lau static int set_info_rec_size(struct bpf_prog_info *info)
2243c454a46bSMartin KaFai Lau {
2244c454a46bSMartin KaFai Lau 	/*
2245c454a46bSMartin KaFai Lau 	 * Ensure info.*_rec_size is the same as kernel expected size
2246c454a46bSMartin KaFai Lau 	 *
2247c454a46bSMartin KaFai Lau 	 * or
2248c454a46bSMartin KaFai Lau 	 *
2249c454a46bSMartin KaFai Lau 	 * Only allow zero *_rec_size if both _rec_size and _cnt are
2250c454a46bSMartin KaFai Lau 	 * zero.  In this case, the kernel will set the expected
2251c454a46bSMartin KaFai Lau 	 * _rec_size back to the info.
2252c454a46bSMartin KaFai Lau 	 */
2253c454a46bSMartin KaFai Lau 
225411d8b82dSYonghong Song 	if ((info->nr_func_info || info->func_info_rec_size) &&
2255c454a46bSMartin KaFai Lau 	    info->func_info_rec_size != sizeof(struct bpf_func_info))
2256c454a46bSMartin KaFai Lau 		return -EINVAL;
2257c454a46bSMartin KaFai Lau 
225811d8b82dSYonghong Song 	if ((info->nr_line_info || info->line_info_rec_size) &&
2259c454a46bSMartin KaFai Lau 	    info->line_info_rec_size != sizeof(struct bpf_line_info))
2260c454a46bSMartin KaFai Lau 		return -EINVAL;
2261c454a46bSMartin KaFai Lau 
226211d8b82dSYonghong Song 	if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
2263c454a46bSMartin KaFai Lau 	    info->jited_line_info_rec_size != sizeof(__u64))
2264c454a46bSMartin KaFai Lau 		return -EINVAL;
2265c454a46bSMartin KaFai Lau 
2266c454a46bSMartin KaFai Lau 	info->func_info_rec_size = sizeof(struct bpf_func_info);
2267c454a46bSMartin KaFai Lau 	info->line_info_rec_size = sizeof(struct bpf_line_info);
2268c454a46bSMartin KaFai Lau 	info->jited_line_info_rec_size = sizeof(__u64);
2269c454a46bSMartin KaFai Lau 
2270c454a46bSMartin KaFai Lau 	return 0;
2271c454a46bSMartin KaFai Lau }
2272c454a46bSMartin KaFai Lau 
22731e270976SMartin KaFai Lau static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
22741e270976SMartin KaFai Lau 				   const union bpf_attr *attr,
22751e270976SMartin KaFai Lau 				   union bpf_attr __user *uattr)
22761e270976SMartin KaFai Lau {
22771e270976SMartin KaFai Lau 	struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
22781e270976SMartin KaFai Lau 	struct bpf_prog_info info = {};
22791e270976SMartin KaFai Lau 	u32 info_len = attr->info.info_len;
22805f8f8b93SAlexei Starovoitov 	struct bpf_prog_stats stats;
22811e270976SMartin KaFai Lau 	char __user *uinsns;
22821e270976SMartin KaFai Lau 	u32 ulen;
22831e270976SMartin KaFai Lau 	int err;
22841e270976SMartin KaFai Lau 
2285dcab51f1SMartin KaFai Lau 	err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
22861e270976SMartin KaFai Lau 	if (err)
22871e270976SMartin KaFai Lau 		return err;
22881e270976SMartin KaFai Lau 	info_len = min_t(u32, sizeof(info), info_len);
22891e270976SMartin KaFai Lau 
22901e270976SMartin KaFai Lau 	if (copy_from_user(&info, uinfo, info_len))
229189b09689SDaniel Borkmann 		return -EFAULT;
22921e270976SMartin KaFai Lau 
22931e270976SMartin KaFai Lau 	info.type = prog->type;
22941e270976SMartin KaFai Lau 	info.id = prog->aux->id;
2295cb4d2b3fSMartin KaFai Lau 	info.load_time = prog->aux->load_time;
2296cb4d2b3fSMartin KaFai Lau 	info.created_by_uid = from_kuid_munged(current_user_ns(),
2297cb4d2b3fSMartin KaFai Lau 					       prog->aux->user->uid);
2298b85fab0eSJiri Olsa 	info.gpl_compatible = prog->gpl_compatible;
22991e270976SMartin KaFai Lau 
23001e270976SMartin KaFai Lau 	memcpy(info.tag, prog->tag, sizeof(prog->tag));
2301cb4d2b3fSMartin KaFai Lau 	memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2302cb4d2b3fSMartin KaFai Lau 
2303cb4d2b3fSMartin KaFai Lau 	ulen = info.nr_map_ids;
2304cb4d2b3fSMartin KaFai Lau 	info.nr_map_ids = prog->aux->used_map_cnt;
2305cb4d2b3fSMartin KaFai Lau 	ulen = min_t(u32, info.nr_map_ids, ulen);
2306cb4d2b3fSMartin KaFai Lau 	if (ulen) {
2307721e08daSMartin KaFai Lau 		u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
2308cb4d2b3fSMartin KaFai Lau 		u32 i;
2309cb4d2b3fSMartin KaFai Lau 
2310cb4d2b3fSMartin KaFai Lau 		for (i = 0; i < ulen; i++)
2311cb4d2b3fSMartin KaFai Lau 			if (put_user(prog->aux->used_maps[i]->id,
2312cb4d2b3fSMartin KaFai Lau 				     &user_map_ids[i]))
2313cb4d2b3fSMartin KaFai Lau 				return -EFAULT;
2314cb4d2b3fSMartin KaFai Lau 	}
23151e270976SMartin KaFai Lau 
2316c454a46bSMartin KaFai Lau 	err = set_info_rec_size(&info);
2317c454a46bSMartin KaFai Lau 	if (err)
2318c454a46bSMartin KaFai Lau 		return err;
23197337224fSMartin KaFai Lau 
23205f8f8b93SAlexei Starovoitov 	bpf_prog_get_stats(prog, &stats);
23215f8f8b93SAlexei Starovoitov 	info.run_time_ns = stats.nsecs;
23225f8f8b93SAlexei Starovoitov 	info.run_cnt = stats.cnt;
23235f8f8b93SAlexei Starovoitov 
23241e270976SMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN)) {
23251e270976SMartin KaFai Lau 		info.jited_prog_len = 0;
23261e270976SMartin KaFai Lau 		info.xlated_prog_len = 0;
2327dbecd738SSandipan Das 		info.nr_jited_ksyms = 0;
232828c2fae7SDaniel Borkmann 		info.nr_jited_func_lens = 0;
232911d8b82dSYonghong Song 		info.nr_func_info = 0;
233011d8b82dSYonghong Song 		info.nr_line_info = 0;
233111d8b82dSYonghong Song 		info.nr_jited_line_info = 0;
23321e270976SMartin KaFai Lau 		goto done;
23331e270976SMartin KaFai Lau 	}
23341e270976SMartin KaFai Lau 
23351e270976SMartin KaFai Lau 	ulen = info.xlated_prog_len;
23369975a54bSDaniel Borkmann 	info.xlated_prog_len = bpf_prog_insn_size(prog);
23371e270976SMartin KaFai Lau 	if (info.xlated_prog_len && ulen) {
23387105e828SDaniel Borkmann 		struct bpf_insn *insns_sanitized;
23397105e828SDaniel Borkmann 		bool fault;
23407105e828SDaniel Borkmann 
23417105e828SDaniel Borkmann 		if (prog->blinded && !bpf_dump_raw_ok()) {
23427105e828SDaniel Borkmann 			info.xlated_prog_insns = 0;
23437105e828SDaniel Borkmann 			goto done;
23447105e828SDaniel Borkmann 		}
23457105e828SDaniel Borkmann 		insns_sanitized = bpf_insn_prepare_dump(prog);
23467105e828SDaniel Borkmann 		if (!insns_sanitized)
23477105e828SDaniel Borkmann 			return -ENOMEM;
23481e270976SMartin KaFai Lau 		uinsns = u64_to_user_ptr(info.xlated_prog_insns);
23491e270976SMartin KaFai Lau 		ulen = min_t(u32, info.xlated_prog_len, ulen);
23507105e828SDaniel Borkmann 		fault = copy_to_user(uinsns, insns_sanitized, ulen);
23517105e828SDaniel Borkmann 		kfree(insns_sanitized);
23527105e828SDaniel Borkmann 		if (fault)
23531e270976SMartin KaFai Lau 			return -EFAULT;
23541e270976SMartin KaFai Lau 	}
23551e270976SMartin KaFai Lau 
2356675fc275SJakub Kicinski 	if (bpf_prog_is_dev_bound(prog->aux)) {
2357675fc275SJakub Kicinski 		err = bpf_prog_offload_info_fill(&info, prog);
2358675fc275SJakub Kicinski 		if (err)
2359675fc275SJakub Kicinski 			return err;
2360fcfb126dSJiong Wang 		goto done;
2361fcfb126dSJiong Wang 	}
2362fcfb126dSJiong Wang 
2363fcfb126dSJiong Wang 	/* NOTE: the following code is supposed to be skipped for offload.
2364fcfb126dSJiong Wang 	 * bpf_prog_offload_info_fill() is the place to fill similar fields
2365fcfb126dSJiong Wang 	 * for offload.
2366fcfb126dSJiong Wang 	 */
2367fcfb126dSJiong Wang 	ulen = info.jited_prog_len;
23684d56a76eSSandipan Das 	if (prog->aux->func_cnt) {
23694d56a76eSSandipan Das 		u32 i;
23704d56a76eSSandipan Das 
23714d56a76eSSandipan Das 		info.jited_prog_len = 0;
23724d56a76eSSandipan Das 		for (i = 0; i < prog->aux->func_cnt; i++)
23734d56a76eSSandipan Das 			info.jited_prog_len += prog->aux->func[i]->jited_len;
23744d56a76eSSandipan Das 	} else {
2375fcfb126dSJiong Wang 		info.jited_prog_len = prog->jited_len;
23764d56a76eSSandipan Das 	}
23774d56a76eSSandipan Das 
2378fcfb126dSJiong Wang 	if (info.jited_prog_len && ulen) {
2379fcfb126dSJiong Wang 		if (bpf_dump_raw_ok()) {
2380fcfb126dSJiong Wang 			uinsns = u64_to_user_ptr(info.jited_prog_insns);
2381fcfb126dSJiong Wang 			ulen = min_t(u32, info.jited_prog_len, ulen);
23824d56a76eSSandipan Das 
23834d56a76eSSandipan Das 			/* for multi-function programs, copy the JITed
23844d56a76eSSandipan Das 			 * instructions for all the functions
23854d56a76eSSandipan Das 			 */
23864d56a76eSSandipan Das 			if (prog->aux->func_cnt) {
23874d56a76eSSandipan Das 				u32 len, free, i;
23884d56a76eSSandipan Das 				u8 *img;
23894d56a76eSSandipan Das 
23904d56a76eSSandipan Das 				free = ulen;
23914d56a76eSSandipan Das 				for (i = 0; i < prog->aux->func_cnt; i++) {
23924d56a76eSSandipan Das 					len = prog->aux->func[i]->jited_len;
23934d56a76eSSandipan Das 					len = min_t(u32, len, free);
23944d56a76eSSandipan Das 					img = (u8 *) prog->aux->func[i]->bpf_func;
23954d56a76eSSandipan Das 					if (copy_to_user(uinsns, img, len))
23964d56a76eSSandipan Das 						return -EFAULT;
23974d56a76eSSandipan Das 					uinsns += len;
23984d56a76eSSandipan Das 					free -= len;
23994d56a76eSSandipan Das 					if (!free)
24004d56a76eSSandipan Das 						break;
24014d56a76eSSandipan Das 				}
24024d56a76eSSandipan Das 			} else {
2403fcfb126dSJiong Wang 				if (copy_to_user(uinsns, prog->bpf_func, ulen))
2404fcfb126dSJiong Wang 					return -EFAULT;
24054d56a76eSSandipan Das 			}
2406fcfb126dSJiong Wang 		} else {
2407fcfb126dSJiong Wang 			info.jited_prog_insns = 0;
2408fcfb126dSJiong Wang 		}
2409675fc275SJakub Kicinski 	}
2410675fc275SJakub Kicinski 
2411dbecd738SSandipan Das 	ulen = info.nr_jited_ksyms;
2412ff1889fcSSong Liu 	info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
24137a5725ddSSong Liu 	if (ulen) {
2414dbecd738SSandipan Das 		if (bpf_dump_raw_ok()) {
2415ff1889fcSSong Liu 			unsigned long ksym_addr;
2416dbecd738SSandipan Das 			u64 __user *user_ksyms;
2417dbecd738SSandipan Das 			u32 i;
2418dbecd738SSandipan Das 
2419dbecd738SSandipan Das 			/* copy the address of the kernel symbol
2420dbecd738SSandipan Das 			 * corresponding to each function
2421dbecd738SSandipan Das 			 */
2422dbecd738SSandipan Das 			ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2423dbecd738SSandipan Das 			user_ksyms = u64_to_user_ptr(info.jited_ksyms);
2424ff1889fcSSong Liu 			if (prog->aux->func_cnt) {
2425dbecd738SSandipan Das 				for (i = 0; i < ulen; i++) {
2426ff1889fcSSong Liu 					ksym_addr = (unsigned long)
2427ff1889fcSSong Liu 						prog->aux->func[i]->bpf_func;
2428ff1889fcSSong Liu 					if (put_user((u64) ksym_addr,
2429ff1889fcSSong Liu 						     &user_ksyms[i]))
2430ff1889fcSSong Liu 						return -EFAULT;
2431ff1889fcSSong Liu 				}
2432ff1889fcSSong Liu 			} else {
2433ff1889fcSSong Liu 				ksym_addr = (unsigned long) prog->bpf_func;
2434ff1889fcSSong Liu 				if (put_user((u64) ksym_addr, &user_ksyms[0]))
2435dbecd738SSandipan Das 					return -EFAULT;
2436dbecd738SSandipan Das 			}
2437dbecd738SSandipan Das 		} else {
2438dbecd738SSandipan Das 			info.jited_ksyms = 0;
2439dbecd738SSandipan Das 		}
2440dbecd738SSandipan Das 	}
2441dbecd738SSandipan Das 
2442815581c1SSandipan Das 	ulen = info.nr_jited_func_lens;
2443ff1889fcSSong Liu 	info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
24447a5725ddSSong Liu 	if (ulen) {
2445815581c1SSandipan Das 		if (bpf_dump_raw_ok()) {
2446815581c1SSandipan Das 			u32 __user *user_lens;
2447815581c1SSandipan Das 			u32 func_len, i;
2448815581c1SSandipan Das 
2449815581c1SSandipan Das 			/* copy the JITed image lengths for each function */
2450815581c1SSandipan Das 			ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2451815581c1SSandipan Das 			user_lens = u64_to_user_ptr(info.jited_func_lens);
2452ff1889fcSSong Liu 			if (prog->aux->func_cnt) {
2453815581c1SSandipan Das 				for (i = 0; i < ulen; i++) {
2454ff1889fcSSong Liu 					func_len =
2455ff1889fcSSong Liu 						prog->aux->func[i]->jited_len;
2456815581c1SSandipan Das 					if (put_user(func_len, &user_lens[i]))
2457815581c1SSandipan Das 						return -EFAULT;
2458815581c1SSandipan Das 				}
2459815581c1SSandipan Das 			} else {
2460ff1889fcSSong Liu 				func_len = prog->jited_len;
2461ff1889fcSSong Liu 				if (put_user(func_len, &user_lens[0]))
2462ff1889fcSSong Liu 					return -EFAULT;
2463ff1889fcSSong Liu 			}
2464ff1889fcSSong Liu 		} else {
2465815581c1SSandipan Das 			info.jited_func_lens = 0;
2466815581c1SSandipan Das 		}
2467815581c1SSandipan Das 	}
2468815581c1SSandipan Das 
24697337224fSMartin KaFai Lau 	if (prog->aux->btf)
2470838e9690SYonghong Song 		info.btf_id = btf_id(prog->aux->btf);
2471838e9690SYonghong Song 
247211d8b82dSYonghong Song 	ulen = info.nr_func_info;
247311d8b82dSYonghong Song 	info.nr_func_info = prog->aux->func_info_cnt;
247411d8b82dSYonghong Song 	if (info.nr_func_info && ulen) {
2475838e9690SYonghong Song 		char __user *user_finfo;
2476838e9690SYonghong Song 
2477838e9690SYonghong Song 		user_finfo = u64_to_user_ptr(info.func_info);
247811d8b82dSYonghong Song 		ulen = min_t(u32, info.nr_func_info, ulen);
2479ba64e7d8SYonghong Song 		if (copy_to_user(user_finfo, prog->aux->func_info,
24807337224fSMartin KaFai Lau 				 info.func_info_rec_size * ulen))
2481838e9690SYonghong Song 			return -EFAULT;
2482838e9690SYonghong Song 	}
2483838e9690SYonghong Song 
248411d8b82dSYonghong Song 	ulen = info.nr_line_info;
248511d8b82dSYonghong Song 	info.nr_line_info = prog->aux->nr_linfo;
248611d8b82dSYonghong Song 	if (info.nr_line_info && ulen) {
2487c454a46bSMartin KaFai Lau 		__u8 __user *user_linfo;
2488c454a46bSMartin KaFai Lau 
2489c454a46bSMartin KaFai Lau 		user_linfo = u64_to_user_ptr(info.line_info);
249011d8b82dSYonghong Song 		ulen = min_t(u32, info.nr_line_info, ulen);
2491c454a46bSMartin KaFai Lau 		if (copy_to_user(user_linfo, prog->aux->linfo,
2492c454a46bSMartin KaFai Lau 				 info.line_info_rec_size * ulen))
2493c454a46bSMartin KaFai Lau 			return -EFAULT;
2494c454a46bSMartin KaFai Lau 	}
2495c454a46bSMartin KaFai Lau 
249611d8b82dSYonghong Song 	ulen = info.nr_jited_line_info;
2497c454a46bSMartin KaFai Lau 	if (prog->aux->jited_linfo)
249811d8b82dSYonghong Song 		info.nr_jited_line_info = prog->aux->nr_linfo;
2499c454a46bSMartin KaFai Lau 	else
250011d8b82dSYonghong Song 		info.nr_jited_line_info = 0;
250111d8b82dSYonghong Song 	if (info.nr_jited_line_info && ulen) {
2502c454a46bSMartin KaFai Lau 		if (bpf_dump_raw_ok()) {
2503c454a46bSMartin KaFai Lau 			__u64 __user *user_linfo;
2504c454a46bSMartin KaFai Lau 			u32 i;
2505c454a46bSMartin KaFai Lau 
2506c454a46bSMartin KaFai Lau 			user_linfo = u64_to_user_ptr(info.jited_line_info);
250711d8b82dSYonghong Song 			ulen = min_t(u32, info.nr_jited_line_info, ulen);
2508c454a46bSMartin KaFai Lau 			for (i = 0; i < ulen; i++) {
2509c454a46bSMartin KaFai Lau 				if (put_user((__u64)(long)prog->aux->jited_linfo[i],
2510c454a46bSMartin KaFai Lau 					     &user_linfo[i]))
2511c454a46bSMartin KaFai Lau 					return -EFAULT;
2512c454a46bSMartin KaFai Lau 			}
2513c454a46bSMartin KaFai Lau 		} else {
2514c454a46bSMartin KaFai Lau 			info.jited_line_info = 0;
2515c454a46bSMartin KaFai Lau 		}
2516c454a46bSMartin KaFai Lau 	}
2517c454a46bSMartin KaFai Lau 
2518c872bdb3SSong Liu 	ulen = info.nr_prog_tags;
2519c872bdb3SSong Liu 	info.nr_prog_tags = prog->aux->func_cnt ? : 1;
2520c872bdb3SSong Liu 	if (ulen) {
2521c872bdb3SSong Liu 		__u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
2522c872bdb3SSong Liu 		u32 i;
2523c872bdb3SSong Liu 
2524c872bdb3SSong Liu 		user_prog_tags = u64_to_user_ptr(info.prog_tags);
2525c872bdb3SSong Liu 		ulen = min_t(u32, info.nr_prog_tags, ulen);
2526c872bdb3SSong Liu 		if (prog->aux->func_cnt) {
2527c872bdb3SSong Liu 			for (i = 0; i < ulen; i++) {
2528c872bdb3SSong Liu 				if (copy_to_user(user_prog_tags[i],
2529c872bdb3SSong Liu 						 prog->aux->func[i]->tag,
2530c872bdb3SSong Liu 						 BPF_TAG_SIZE))
2531c872bdb3SSong Liu 					return -EFAULT;
2532c872bdb3SSong Liu 			}
2533c872bdb3SSong Liu 		} else {
2534c872bdb3SSong Liu 			if (copy_to_user(user_prog_tags[0],
2535c872bdb3SSong Liu 					 prog->tag, BPF_TAG_SIZE))
2536c872bdb3SSong Liu 				return -EFAULT;
2537c872bdb3SSong Liu 		}
2538c872bdb3SSong Liu 	}
2539c872bdb3SSong Liu 
25401e270976SMartin KaFai Lau done:
25411e270976SMartin KaFai Lau 	if (copy_to_user(uinfo, &info, info_len) ||
25421e270976SMartin KaFai Lau 	    put_user(info_len, &uattr->info.info_len))
25431e270976SMartin KaFai Lau 		return -EFAULT;
25441e270976SMartin KaFai Lau 
25451e270976SMartin KaFai Lau 	return 0;
25461e270976SMartin KaFai Lau }
25471e270976SMartin KaFai Lau 
25481e270976SMartin KaFai Lau static int bpf_map_get_info_by_fd(struct bpf_map *map,
25491e270976SMartin KaFai Lau 				  const union bpf_attr *attr,
25501e270976SMartin KaFai Lau 				  union bpf_attr __user *uattr)
25511e270976SMartin KaFai Lau {
25521e270976SMartin KaFai Lau 	struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
25531e270976SMartin KaFai Lau 	struct bpf_map_info info = {};
25541e270976SMartin KaFai Lau 	u32 info_len = attr->info.info_len;
25551e270976SMartin KaFai Lau 	int err;
25561e270976SMartin KaFai Lau 
2557dcab51f1SMartin KaFai Lau 	err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
25581e270976SMartin KaFai Lau 	if (err)
25591e270976SMartin KaFai Lau 		return err;
25601e270976SMartin KaFai Lau 	info_len = min_t(u32, sizeof(info), info_len);
25611e270976SMartin KaFai Lau 
25621e270976SMartin KaFai Lau 	info.type = map->map_type;
25631e270976SMartin KaFai Lau 	info.id = map->id;
25641e270976SMartin KaFai Lau 	info.key_size = map->key_size;
25651e270976SMartin KaFai Lau 	info.value_size = map->value_size;
25661e270976SMartin KaFai Lau 	info.max_entries = map->max_entries;
25671e270976SMartin KaFai Lau 	info.map_flags = map->map_flags;
2568ad5b177bSMartin KaFai Lau 	memcpy(info.name, map->name, sizeof(map->name));
25691e270976SMartin KaFai Lau 
257078958fcaSMartin KaFai Lau 	if (map->btf) {
257178958fcaSMartin KaFai Lau 		info.btf_id = btf_id(map->btf);
25729b2cf328SMartin KaFai Lau 		info.btf_key_type_id = map->btf_key_type_id;
25739b2cf328SMartin KaFai Lau 		info.btf_value_type_id = map->btf_value_type_id;
257478958fcaSMartin KaFai Lau 	}
257578958fcaSMartin KaFai Lau 
257652775b33SJakub Kicinski 	if (bpf_map_is_dev_bound(map)) {
257752775b33SJakub Kicinski 		err = bpf_map_offload_info_fill(&info, map);
257852775b33SJakub Kicinski 		if (err)
257952775b33SJakub Kicinski 			return err;
258052775b33SJakub Kicinski 	}
258152775b33SJakub Kicinski 
25821e270976SMartin KaFai Lau 	if (copy_to_user(uinfo, &info, info_len) ||
25831e270976SMartin KaFai Lau 	    put_user(info_len, &uattr->info.info_len))
25841e270976SMartin KaFai Lau 		return -EFAULT;
25851e270976SMartin KaFai Lau 
25861e270976SMartin KaFai Lau 	return 0;
25871e270976SMartin KaFai Lau }
25881e270976SMartin KaFai Lau 
258962dab84cSMartin KaFai Lau static int bpf_btf_get_info_by_fd(struct btf *btf,
259062dab84cSMartin KaFai Lau 				  const union bpf_attr *attr,
259162dab84cSMartin KaFai Lau 				  union bpf_attr __user *uattr)
259262dab84cSMartin KaFai Lau {
259362dab84cSMartin KaFai Lau 	struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
259462dab84cSMartin KaFai Lau 	u32 info_len = attr->info.info_len;
259562dab84cSMartin KaFai Lau 	int err;
259662dab84cSMartin KaFai Lau 
2597dcab51f1SMartin KaFai Lau 	err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
259862dab84cSMartin KaFai Lau 	if (err)
259962dab84cSMartin KaFai Lau 		return err;
260062dab84cSMartin KaFai Lau 
260162dab84cSMartin KaFai Lau 	return btf_get_info_by_fd(btf, attr, uattr);
260262dab84cSMartin KaFai Lau }
260362dab84cSMartin KaFai Lau 
26041e270976SMartin KaFai Lau #define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
26051e270976SMartin KaFai Lau 
26061e270976SMartin KaFai Lau static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
26071e270976SMartin KaFai Lau 				  union bpf_attr __user *uattr)
26081e270976SMartin KaFai Lau {
26091e270976SMartin KaFai Lau 	int ufd = attr->info.bpf_fd;
26101e270976SMartin KaFai Lau 	struct fd f;
26111e270976SMartin KaFai Lau 	int err;
26121e270976SMartin KaFai Lau 
26131e270976SMartin KaFai Lau 	if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
26141e270976SMartin KaFai Lau 		return -EINVAL;
26151e270976SMartin KaFai Lau 
26161e270976SMartin KaFai Lau 	f = fdget(ufd);
26171e270976SMartin KaFai Lau 	if (!f.file)
26181e270976SMartin KaFai Lau 		return -EBADFD;
26191e270976SMartin KaFai Lau 
26201e270976SMartin KaFai Lau 	if (f.file->f_op == &bpf_prog_fops)
26211e270976SMartin KaFai Lau 		err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
26221e270976SMartin KaFai Lau 					      uattr);
26231e270976SMartin KaFai Lau 	else if (f.file->f_op == &bpf_map_fops)
26241e270976SMartin KaFai Lau 		err = bpf_map_get_info_by_fd(f.file->private_data, attr,
26251e270976SMartin KaFai Lau 					     uattr);
262660197cfbSMartin KaFai Lau 	else if (f.file->f_op == &btf_fops)
262762dab84cSMartin KaFai Lau 		err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
26281e270976SMartin KaFai Lau 	else
26291e270976SMartin KaFai Lau 		err = -EINVAL;
26301e270976SMartin KaFai Lau 
26311e270976SMartin KaFai Lau 	fdput(f);
26321e270976SMartin KaFai Lau 	return err;
26331e270976SMartin KaFai Lau }
26341e270976SMartin KaFai Lau 
2635f56a653cSMartin KaFai Lau #define BPF_BTF_LOAD_LAST_FIELD btf_log_level
2636f56a653cSMartin KaFai Lau 
2637f56a653cSMartin KaFai Lau static int bpf_btf_load(const union bpf_attr *attr)
2638f56a653cSMartin KaFai Lau {
2639f56a653cSMartin KaFai Lau 	if (CHECK_ATTR(BPF_BTF_LOAD))
2640f56a653cSMartin KaFai Lau 		return -EINVAL;
2641f56a653cSMartin KaFai Lau 
2642f56a653cSMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN))
2643f56a653cSMartin KaFai Lau 		return -EPERM;
2644f56a653cSMartin KaFai Lau 
2645f56a653cSMartin KaFai Lau 	return btf_new_fd(attr);
2646f56a653cSMartin KaFai Lau }
2647f56a653cSMartin KaFai Lau 
264878958fcaSMartin KaFai Lau #define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
264978958fcaSMartin KaFai Lau 
265078958fcaSMartin KaFai Lau static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
265178958fcaSMartin KaFai Lau {
265278958fcaSMartin KaFai Lau 	if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
265378958fcaSMartin KaFai Lau 		return -EINVAL;
265478958fcaSMartin KaFai Lau 
265578958fcaSMartin KaFai Lau 	if (!capable(CAP_SYS_ADMIN))
265678958fcaSMartin KaFai Lau 		return -EPERM;
265778958fcaSMartin KaFai Lau 
265878958fcaSMartin KaFai Lau 	return btf_get_fd_by_id(attr->btf_id);
265978958fcaSMartin KaFai Lau }
266078958fcaSMartin KaFai Lau 
266141bdc4b4SYonghong Song static int bpf_task_fd_query_copy(const union bpf_attr *attr,
266241bdc4b4SYonghong Song 				    union bpf_attr __user *uattr,
266341bdc4b4SYonghong Song 				    u32 prog_id, u32 fd_type,
266441bdc4b4SYonghong Song 				    const char *buf, u64 probe_offset,
266541bdc4b4SYonghong Song 				    u64 probe_addr)
266641bdc4b4SYonghong Song {
266741bdc4b4SYonghong Song 	char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
266841bdc4b4SYonghong Song 	u32 len = buf ? strlen(buf) : 0, input_len;
266941bdc4b4SYonghong Song 	int err = 0;
267041bdc4b4SYonghong Song 
267141bdc4b4SYonghong Song 	if (put_user(len, &uattr->task_fd_query.buf_len))
267241bdc4b4SYonghong Song 		return -EFAULT;
267341bdc4b4SYonghong Song 	input_len = attr->task_fd_query.buf_len;
267441bdc4b4SYonghong Song 	if (input_len && ubuf) {
267541bdc4b4SYonghong Song 		if (!len) {
267641bdc4b4SYonghong Song 			/* nothing to copy, just make ubuf NULL terminated */
267741bdc4b4SYonghong Song 			char zero = '\0';
267841bdc4b4SYonghong Song 
267941bdc4b4SYonghong Song 			if (put_user(zero, ubuf))
268041bdc4b4SYonghong Song 				return -EFAULT;
268141bdc4b4SYonghong Song 		} else if (input_len >= len + 1) {
268241bdc4b4SYonghong Song 			/* ubuf can hold the string with NULL terminator */
268341bdc4b4SYonghong Song 			if (copy_to_user(ubuf, buf, len + 1))
268441bdc4b4SYonghong Song 				return -EFAULT;
268541bdc4b4SYonghong Song 		} else {
268641bdc4b4SYonghong Song 			/* ubuf cannot hold the string with NULL terminator,
268741bdc4b4SYonghong Song 			 * do a partial copy with NULL terminator.
268841bdc4b4SYonghong Song 			 */
268941bdc4b4SYonghong Song 			char zero = '\0';
269041bdc4b4SYonghong Song 
269141bdc4b4SYonghong Song 			err = -ENOSPC;
269241bdc4b4SYonghong Song 			if (copy_to_user(ubuf, buf, input_len - 1))
269341bdc4b4SYonghong Song 				return -EFAULT;
269441bdc4b4SYonghong Song 			if (put_user(zero, ubuf + input_len - 1))
269541bdc4b4SYonghong Song 				return -EFAULT;
269641bdc4b4SYonghong Song 		}
269741bdc4b4SYonghong Song 	}
269841bdc4b4SYonghong Song 
269941bdc4b4SYonghong Song 	if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
270041bdc4b4SYonghong Song 	    put_user(fd_type, &uattr->task_fd_query.fd_type) ||
270141bdc4b4SYonghong Song 	    put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
270241bdc4b4SYonghong Song 	    put_user(probe_addr, &uattr->task_fd_query.probe_addr))
270341bdc4b4SYonghong Song 		return -EFAULT;
270441bdc4b4SYonghong Song 
270541bdc4b4SYonghong Song 	return err;
270641bdc4b4SYonghong Song }
270741bdc4b4SYonghong Song 
270841bdc4b4SYonghong Song #define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
270941bdc4b4SYonghong Song 
271041bdc4b4SYonghong Song static int bpf_task_fd_query(const union bpf_attr *attr,
271141bdc4b4SYonghong Song 			     union bpf_attr __user *uattr)
271241bdc4b4SYonghong Song {
271341bdc4b4SYonghong Song 	pid_t pid = attr->task_fd_query.pid;
271441bdc4b4SYonghong Song 	u32 fd = attr->task_fd_query.fd;
271541bdc4b4SYonghong Song 	const struct perf_event *event;
271641bdc4b4SYonghong Song 	struct files_struct *files;
271741bdc4b4SYonghong Song 	struct task_struct *task;
271841bdc4b4SYonghong Song 	struct file *file;
271941bdc4b4SYonghong Song 	int err;
272041bdc4b4SYonghong Song 
272141bdc4b4SYonghong Song 	if (CHECK_ATTR(BPF_TASK_FD_QUERY))
272241bdc4b4SYonghong Song 		return -EINVAL;
272341bdc4b4SYonghong Song 
272441bdc4b4SYonghong Song 	if (!capable(CAP_SYS_ADMIN))
272541bdc4b4SYonghong Song 		return -EPERM;
272641bdc4b4SYonghong Song 
272741bdc4b4SYonghong Song 	if (attr->task_fd_query.flags != 0)
272841bdc4b4SYonghong Song 		return -EINVAL;
272941bdc4b4SYonghong Song 
273041bdc4b4SYonghong Song 	task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
273141bdc4b4SYonghong Song 	if (!task)
273241bdc4b4SYonghong Song 		return -ENOENT;
273341bdc4b4SYonghong Song 
273441bdc4b4SYonghong Song 	files = get_files_struct(task);
273541bdc4b4SYonghong Song 	put_task_struct(task);
273641bdc4b4SYonghong Song 	if (!files)
273741bdc4b4SYonghong Song 		return -ENOENT;
273841bdc4b4SYonghong Song 
273941bdc4b4SYonghong Song 	err = 0;
274041bdc4b4SYonghong Song 	spin_lock(&files->file_lock);
274141bdc4b4SYonghong Song 	file = fcheck_files(files, fd);
274241bdc4b4SYonghong Song 	if (!file)
274341bdc4b4SYonghong Song 		err = -EBADF;
274441bdc4b4SYonghong Song 	else
274541bdc4b4SYonghong Song 		get_file(file);
274641bdc4b4SYonghong Song 	spin_unlock(&files->file_lock);
274741bdc4b4SYonghong Song 	put_files_struct(files);
274841bdc4b4SYonghong Song 
274941bdc4b4SYonghong Song 	if (err)
275041bdc4b4SYonghong Song 		goto out;
275141bdc4b4SYonghong Song 
275241bdc4b4SYonghong Song 	if (file->f_op == &bpf_raw_tp_fops) {
275341bdc4b4SYonghong Song 		struct bpf_raw_tracepoint *raw_tp = file->private_data;
275441bdc4b4SYonghong Song 		struct bpf_raw_event_map *btp = raw_tp->btp;
275541bdc4b4SYonghong Song 
275641bdc4b4SYonghong Song 		err = bpf_task_fd_query_copy(attr, uattr,
275741bdc4b4SYonghong Song 					     raw_tp->prog->aux->id,
275841bdc4b4SYonghong Song 					     BPF_FD_TYPE_RAW_TRACEPOINT,
275941bdc4b4SYonghong Song 					     btp->tp->name, 0, 0);
276041bdc4b4SYonghong Song 		goto put_file;
276141bdc4b4SYonghong Song 	}
276241bdc4b4SYonghong Song 
276341bdc4b4SYonghong Song 	event = perf_get_event(file);
276441bdc4b4SYonghong Song 	if (!IS_ERR(event)) {
276541bdc4b4SYonghong Song 		u64 probe_offset, probe_addr;
276641bdc4b4SYonghong Song 		u32 prog_id, fd_type;
276741bdc4b4SYonghong Song 		const char *buf;
276841bdc4b4SYonghong Song 
276941bdc4b4SYonghong Song 		err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
277041bdc4b4SYonghong Song 					      &buf, &probe_offset,
277141bdc4b4SYonghong Song 					      &probe_addr);
277241bdc4b4SYonghong Song 		if (!err)
277341bdc4b4SYonghong Song 			err = bpf_task_fd_query_copy(attr, uattr, prog_id,
277441bdc4b4SYonghong Song 						     fd_type, buf,
277541bdc4b4SYonghong Song 						     probe_offset,
277641bdc4b4SYonghong Song 						     probe_addr);
277741bdc4b4SYonghong Song 		goto put_file;
277841bdc4b4SYonghong Song 	}
277941bdc4b4SYonghong Song 
278041bdc4b4SYonghong Song 	err = -ENOTSUPP;
278141bdc4b4SYonghong Song put_file:
278241bdc4b4SYonghong Song 	fput(file);
278341bdc4b4SYonghong Song out:
278441bdc4b4SYonghong Song 	return err;
278541bdc4b4SYonghong Song }
278641bdc4b4SYonghong Song 
278799c55f7dSAlexei Starovoitov SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
278899c55f7dSAlexei Starovoitov {
278999c55f7dSAlexei Starovoitov 	union bpf_attr attr = {};
279099c55f7dSAlexei Starovoitov 	int err;
279199c55f7dSAlexei Starovoitov 
27920fa4fe85SChenbo Feng 	if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
279399c55f7dSAlexei Starovoitov 		return -EPERM;
279499c55f7dSAlexei Starovoitov 
2795dcab51f1SMartin KaFai Lau 	err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
279699c55f7dSAlexei Starovoitov 	if (err)
279799c55f7dSAlexei Starovoitov 		return err;
27981e270976SMartin KaFai Lau 	size = min_t(u32, size, sizeof(attr));
279999c55f7dSAlexei Starovoitov 
280099c55f7dSAlexei Starovoitov 	/* copy attributes from user space, may be less than sizeof(bpf_attr) */
280199c55f7dSAlexei Starovoitov 	if (copy_from_user(&attr, uattr, size) != 0)
280299c55f7dSAlexei Starovoitov 		return -EFAULT;
280399c55f7dSAlexei Starovoitov 
2804afdb09c7SChenbo Feng 	err = security_bpf(cmd, &attr, size);
2805afdb09c7SChenbo Feng 	if (err < 0)
2806afdb09c7SChenbo Feng 		return err;
2807afdb09c7SChenbo Feng 
280899c55f7dSAlexei Starovoitov 	switch (cmd) {
280999c55f7dSAlexei Starovoitov 	case BPF_MAP_CREATE:
281099c55f7dSAlexei Starovoitov 		err = map_create(&attr);
281199c55f7dSAlexei Starovoitov 		break;
2812db20fd2bSAlexei Starovoitov 	case BPF_MAP_LOOKUP_ELEM:
2813db20fd2bSAlexei Starovoitov 		err = map_lookup_elem(&attr);
2814db20fd2bSAlexei Starovoitov 		break;
2815db20fd2bSAlexei Starovoitov 	case BPF_MAP_UPDATE_ELEM:
2816db20fd2bSAlexei Starovoitov 		err = map_update_elem(&attr);
2817db20fd2bSAlexei Starovoitov 		break;
2818db20fd2bSAlexei Starovoitov 	case BPF_MAP_DELETE_ELEM:
2819db20fd2bSAlexei Starovoitov 		err = map_delete_elem(&attr);
2820db20fd2bSAlexei Starovoitov 		break;
2821db20fd2bSAlexei Starovoitov 	case BPF_MAP_GET_NEXT_KEY:
2822db20fd2bSAlexei Starovoitov 		err = map_get_next_key(&attr);
2823db20fd2bSAlexei Starovoitov 		break;
282487df15deSDaniel Borkmann 	case BPF_MAP_FREEZE:
282587df15deSDaniel Borkmann 		err = map_freeze(&attr);
282687df15deSDaniel Borkmann 		break;
282709756af4SAlexei Starovoitov 	case BPF_PROG_LOAD:
2828838e9690SYonghong Song 		err = bpf_prog_load(&attr, uattr);
282909756af4SAlexei Starovoitov 		break;
2830b2197755SDaniel Borkmann 	case BPF_OBJ_PIN:
2831b2197755SDaniel Borkmann 		err = bpf_obj_pin(&attr);
2832b2197755SDaniel Borkmann 		break;
2833b2197755SDaniel Borkmann 	case BPF_OBJ_GET:
2834b2197755SDaniel Borkmann 		err = bpf_obj_get(&attr);
2835b2197755SDaniel Borkmann 		break;
2836f4324551SDaniel Mack 	case BPF_PROG_ATTACH:
2837f4324551SDaniel Mack 		err = bpf_prog_attach(&attr);
2838f4324551SDaniel Mack 		break;
2839f4324551SDaniel Mack 	case BPF_PROG_DETACH:
2840f4324551SDaniel Mack 		err = bpf_prog_detach(&attr);
2841f4324551SDaniel Mack 		break;
2842468e2f64SAlexei Starovoitov 	case BPF_PROG_QUERY:
2843468e2f64SAlexei Starovoitov 		err = bpf_prog_query(&attr, uattr);
2844468e2f64SAlexei Starovoitov 		break;
28451cf1cae9SAlexei Starovoitov 	case BPF_PROG_TEST_RUN:
28461cf1cae9SAlexei Starovoitov 		err = bpf_prog_test_run(&attr, uattr);
28471cf1cae9SAlexei Starovoitov 		break;
284834ad5580SMartin KaFai Lau 	case BPF_PROG_GET_NEXT_ID:
284934ad5580SMartin KaFai Lau 		err = bpf_obj_get_next_id(&attr, uattr,
285034ad5580SMartin KaFai Lau 					  &prog_idr, &prog_idr_lock);
285134ad5580SMartin KaFai Lau 		break;
285234ad5580SMartin KaFai Lau 	case BPF_MAP_GET_NEXT_ID:
285334ad5580SMartin KaFai Lau 		err = bpf_obj_get_next_id(&attr, uattr,
285434ad5580SMartin KaFai Lau 					  &map_idr, &map_idr_lock);
285534ad5580SMartin KaFai Lau 		break;
2856b16d9aa4SMartin KaFai Lau 	case BPF_PROG_GET_FD_BY_ID:
2857b16d9aa4SMartin KaFai Lau 		err = bpf_prog_get_fd_by_id(&attr);
2858b16d9aa4SMartin KaFai Lau 		break;
2859bd5f5f4eSMartin KaFai Lau 	case BPF_MAP_GET_FD_BY_ID:
2860bd5f5f4eSMartin KaFai Lau 		err = bpf_map_get_fd_by_id(&attr);
2861bd5f5f4eSMartin KaFai Lau 		break;
28621e270976SMartin KaFai Lau 	case BPF_OBJ_GET_INFO_BY_FD:
28631e270976SMartin KaFai Lau 		err = bpf_obj_get_info_by_fd(&attr, uattr);
28641e270976SMartin KaFai Lau 		break;
2865c4f6699dSAlexei Starovoitov 	case BPF_RAW_TRACEPOINT_OPEN:
2866c4f6699dSAlexei Starovoitov 		err = bpf_raw_tracepoint_open(&attr);
2867c4f6699dSAlexei Starovoitov 		break;
2868f56a653cSMartin KaFai Lau 	case BPF_BTF_LOAD:
2869f56a653cSMartin KaFai Lau 		err = bpf_btf_load(&attr);
2870f56a653cSMartin KaFai Lau 		break;
287178958fcaSMartin KaFai Lau 	case BPF_BTF_GET_FD_BY_ID:
287278958fcaSMartin KaFai Lau 		err = bpf_btf_get_fd_by_id(&attr);
287378958fcaSMartin KaFai Lau 		break;
287441bdc4b4SYonghong Song 	case BPF_TASK_FD_QUERY:
287541bdc4b4SYonghong Song 		err = bpf_task_fd_query(&attr, uattr);
287641bdc4b4SYonghong Song 		break;
2877bd513cd0SMauricio Vasquez B 	case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
2878bd513cd0SMauricio Vasquez B 		err = map_lookup_and_delete_elem(&attr);
2879bd513cd0SMauricio Vasquez B 		break;
288099c55f7dSAlexei Starovoitov 	default:
288199c55f7dSAlexei Starovoitov 		err = -EINVAL;
288299c55f7dSAlexei Starovoitov 		break;
288399c55f7dSAlexei Starovoitov 	}
288499c55f7dSAlexei Starovoitov 
288599c55f7dSAlexei Starovoitov 	return err;
288699c55f7dSAlexei Starovoitov }
2887