blob: 1033ee8c0caf019e002d86bf58681c0f259b1b93 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003 */
4#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01005#include <linux/bpf_trace.h>
Sean Youngf4364dc2018-05-27 12:24:09 +01006#include <linux/bpf_lirc.h>
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02007#include <linux/bpf_verifier.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07008#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07009#include <linux/syscalls.h>
10#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010011#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010012#include <linux/vmalloc.h>
13#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070014#include <linux/anon_inodes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070015#include <linux/fdtable.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070016#include <linux/file.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070017#include <linux/fs.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070018#include <linux/license.h>
19#include <linux/filter.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010020#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070021#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070022#include <linux/cred.h>
23#include <linux/timekeeping.h>
24#include <linux/ctype.h>
Mark Rutland9ef09e32018-05-03 17:04:59 +010025#include <linux/nospec.h>
Daniel Borkmannbae141f2019-12-06 22:49:34 +010026#include <linux/audit.h>
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -070027#include <uapi/linux/btf.h>
Mike Rapoportca5999f2020-06-08 21:32:38 -070028#include <linux/pgtable.h>
KP Singh9e4e01d2020-03-29 01:43:52 +010029#include <linux/bpf_lsm.h>
Andrii Nakryiko457f4432020-05-29 00:54:20 -070030#include <linux/poll.h>
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +020031#include <linux/bpf-netns.h>
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -070032#include <linux/rcupdate_trace.h>
Roman Gushchin48edc1f2020-12-01 13:58:32 -080033#include <linux/memcontrol.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070034
Daniel Borkmannda765a22019-11-22 21:07:58 +010035#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
36 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
38#define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070039#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
Daniel Borkmannda765a22019-11-22 21:07:58 +010040#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
41 IS_FD_HASH(map))
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070042
Chenbo Feng6e71b042017-10-18 13:00:22 -070043#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
44
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080045DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070046static DEFINE_IDR(prog_idr);
47static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070048static DEFINE_IDR(map_idr);
49static DEFINE_SPINLOCK(map_idr_lock);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -070050static DEFINE_IDR(link_idr);
51static DEFINE_SPINLOCK(link_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080052
Daniel Borkmann08389d82021-05-11 22:35:17 +020053int sysctl_unprivileged_bpf_disabled __read_mostly =
54 IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070055
Johannes Berg40077e02017-04-11 15:34:58 +020056static const struct bpf_map_ops * const bpf_map_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -080057#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
Johannes Berg40077e02017-04-11 15:34:58 +020058#define BPF_MAP_TYPE(_id, _ops) \
59 [_id] = &_ops,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -070060#define BPF_LINK_TYPE(_id, _name)
Johannes Berg40077e02017-04-11 15:34:58 +020061#include <linux/bpf_types.h>
62#undef BPF_PROG_TYPE
63#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -070064#undef BPF_LINK_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +020065};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070066
Mickaël Salaün752ba562017-08-07 20:45:20 +020067/*
68 * If we're handed a bigger struct than we know of, ensure all the unknown bits
69 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
70 * we don't know about yet.
71 *
72 * There is a ToCToU between this function call and the following
73 * copy_from_user() call. However, this is not a concern since this function is
74 * meant to be a future-proofing of bits.
75 */
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -070076int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070077 size_t expected_size,
78 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020079{
Al Virob7e4b652020-05-08 00:16:31 -040080 int res;
Mickaël Salaün58291a72017-08-07 20:45:19 +020081
Mickaël Salaün752ba562017-08-07 20:45:20 +020082 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
83 return -E2BIG;
84
Mickaël Salaün58291a72017-08-07 20:45:19 +020085 if (actual_size <= expected_size)
86 return 0;
87
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -070088 if (uaddr.is_kernel)
89 res = memchr_inv(uaddr.kernel + expected_size, 0,
90 actual_size - expected_size) == NULL;
91 else
92 res = check_zeroed_user(uaddr.user + expected_size,
93 actual_size - expected_size);
Al Virob7e4b652020-05-08 00:16:31 -040094 if (res < 0)
95 return res;
96 return res ? 0 : -E2BIG;
Mickaël Salaün58291a72017-08-07 20:45:19 +020097}
98
Jakub Kicinskia3884572018-01-11 20:29:09 -080099const struct bpf_map_ops bpf_map_offload_ops = {
Martin KaFai Lauf4d05252020-08-27 18:18:06 -0700100 .map_meta_equal = bpf_map_meta_equal,
Jakub Kicinskia3884572018-01-11 20:29:09 -0800101 .map_alloc = bpf_map_offload_map_alloc,
102 .map_free = bpf_map_offload_map_free,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200103 .map_check_btf = map_check_no_btf,
Jakub Kicinskia3884572018-01-11 20:29:09 -0800104};
105
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700106static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
107{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800108 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100109 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700110 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800111 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700112
Mark Rutland9ef09e32018-05-03 17:04:59 +0100113 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800114 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100115 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
116 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800117 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200118 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700119
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800120 if (ops->map_alloc_check) {
121 err = ops->map_alloc_check(attr);
122 if (err)
123 return ERR_PTR(err);
124 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800125 if (attr->map_ifindex)
126 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800127 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200128 if (IS_ERR(map))
129 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800130 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100131 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200132 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700133}
134
Daniel Borkmann353050b2021-11-09 18:48:08 +0000135static void bpf_map_write_active_inc(struct bpf_map *map)
136{
137 atomic64_inc(&map->writecnt);
138}
139
140static void bpf_map_write_active_dec(struct bpf_map *map)
141{
142 atomic64_dec(&map->writecnt);
143}
144
145bool bpf_map_write_active(const struct bpf_map *map)
146{
147 return atomic64_read(&map->writecnt) != 0;
148}
149
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800150static u32 bpf_map_value_size(const struct bpf_map *map)
Brian Vazquez15c14a32020-01-15 10:43:00 -0800151{
152 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
153 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
154 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
155 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
156 return round_up(map->value_size, 8) * num_possible_cpus();
157 else if (IS_FD_MAP(map))
158 return sizeof(u32);
159 else
160 return map->value_size;
161}
162
163static void maybe_wait_bpf_programs(struct bpf_map *map)
164{
165 /* Wait for any running BPF programs to complete so that
166 * userspace, when we return to it, knows that all programs
167 * that could be running use the new map value.
168 */
169 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
170 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
171 synchronize_rcu();
172}
173
174static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
175 void *value, __u64 flags)
176{
177 int err;
178
179 /* Need to create a kthread, thus must support schedule */
180 if (bpf_map_is_dev_bound(map)) {
181 return bpf_map_offload_update_elem(map, key, value, flags);
182 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
Brian Vazquez15c14a32020-01-15 10:43:00 -0800183 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
184 return map->ops->map_update_elem(map, key, value, flags);
Lorenz Bauer13b79d32020-08-21 11:29:45 +0100185 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
186 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
187 return sock_map_update_elem_sys(map, key, value, flags);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800188 } else if (IS_FD_PROG_ARRAY(map)) {
189 return bpf_fd_array_map_update_elem(map, f.file, key, value,
190 flags);
191 }
192
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100193 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800194 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
195 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
196 err = bpf_percpu_hash_update(map, key, value, flags);
197 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
198 err = bpf_percpu_array_update(map, key, value, flags);
199 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
200 err = bpf_percpu_cgroup_storage_update(map, key, value,
201 flags);
202 } else if (IS_FD_ARRAY(map)) {
203 rcu_read_lock();
204 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
205 flags);
206 rcu_read_unlock();
207 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
208 rcu_read_lock();
209 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
210 flags);
211 rcu_read_unlock();
212 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
213 /* rcu_read_lock() is not needed */
214 err = bpf_fd_reuseport_array_update_elem(map, key, value,
215 flags);
216 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
Joanne Koong93309862021-10-27 16:45:00 -0700217 map->map_type == BPF_MAP_TYPE_STACK ||
218 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
Brian Vazquez15c14a32020-01-15 10:43:00 -0800219 err = map->ops->map_push_elem(map, value, flags);
220 } else {
221 rcu_read_lock();
222 err = map->ops->map_update_elem(map, key, value, flags);
223 rcu_read_unlock();
224 }
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100225 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800226 maybe_wait_bpf_programs(map);
227
228 return err;
229}
230
231static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
232 __u64 flags)
233{
234 void *ptr;
235 int err;
236
Brian Vazquezcb4d03a2020-01-15 10:43:01 -0800237 if (bpf_map_is_dev_bound(map))
238 return bpf_map_offload_lookup_elem(map, key, value);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800239
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100240 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800241 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
242 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
243 err = bpf_percpu_hash_copy(map, key, value);
244 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
245 err = bpf_percpu_array_copy(map, key, value);
246 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
247 err = bpf_percpu_cgroup_storage_copy(map, key, value);
248 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
249 err = bpf_stackmap_copy(map, key, value);
250 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
251 err = bpf_fd_array_map_lookup_elem(map, key, value);
252 } else if (IS_FD_HASH(map)) {
253 err = bpf_fd_htab_map_lookup_elem(map, key, value);
254 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
255 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
256 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
Joanne Koong93309862021-10-27 16:45:00 -0700257 map->map_type == BPF_MAP_TYPE_STACK ||
258 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
Brian Vazquez15c14a32020-01-15 10:43:00 -0800259 err = map->ops->map_peek_elem(map, value);
260 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
261 /* struct_ops map requires directly updating "value" */
262 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
263 } else {
264 rcu_read_lock();
265 if (map->ops->map_lookup_elem_sys_only)
266 ptr = map->ops->map_lookup_elem_sys_only(map, key);
267 else
268 ptr = map->ops->map_lookup_elem(map, key);
269 if (IS_ERR(ptr)) {
270 err = PTR_ERR(ptr);
271 } else if (!ptr) {
272 err = -ENOENT;
273 } else {
274 err = 0;
275 if (flags & BPF_F_LOCK)
276 /* lock 'ptr' and copy everything but lock */
277 copy_map_value_locked(map, value, ptr, true);
278 else
279 copy_map_value(map, value, ptr);
Alexei Starovoitov68134662021-07-14 17:54:10 -0700280 /* mask lock and timer, since value wasn't zero inited */
281 check_and_init_map_value(map, value);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800282 }
283 rcu_read_unlock();
284 }
285
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100286 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800287 maybe_wait_bpf_programs(map);
288
289 return err;
290}
291
Roman Gushchind5299b62020-12-01 13:58:33 -0800292/* Please, do not use this function outside from the map creation path
293 * (e.g. in map update path) without taking care of setting the active
294 * memory cgroup (see at bpf_map_kmalloc_node() for example).
295 */
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100296static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100297{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100298 /* We really just want to fail instead of triggering OOM killer
299 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
300 * which is used for lower order allocation requests.
301 *
302 * It has been observed that higher order allocation requests done by
303 * vmalloc with __GFP_NORETRY being set might fail due to not trying
304 * to reclaim memory from the page cache, thus we set
305 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100306 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100307
Roman Gushchind5299b62020-12-01 13:58:33 -0800308 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
Christoph Hellwig041de932020-06-01 21:52:02 -0700309 unsigned int flags = 0;
310 unsigned long align = 1;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100311 void *area;
312
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100313 if (size >= SIZE_MAX)
314 return NULL;
315
Andrii Nakryikofc970222019-11-17 09:28:04 -0800316 /* kmalloc()'ed memory can't be mmap()'ed */
Christoph Hellwig041de932020-06-01 21:52:02 -0700317 if (mmapable) {
318 BUG_ON(!PAGE_ALIGNED(size));
319 align = SHMLBA;
320 flags = VM_USERMAP;
321 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
322 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100323 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100324 if (area != NULL)
325 return area;
326 }
Christoph Hellwig041de932020-06-01 21:52:02 -0700327
328 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
329 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
330 flags, numa_node, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100331}
332
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100333void *bpf_map_area_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800334{
335 return __bpf_map_area_alloc(size, numa_node, false);
336}
337
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100338void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800339{
340 return __bpf_map_area_alloc(size, numa_node, true);
341}
342
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100343void bpf_map_area_free(void *area)
344{
345 kvfree(area);
346}
347
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200348static u32 bpf_map_flags_retain_permanent(u32 flags)
349{
350 /* Some map creation flags are not tied to the map object but
351 * rather to the map fd instead, so they have no meaning upon
352 * map object inspection since multiple file descriptors with
353 * different (access) properties can exist here. Thus, given
354 * this has zero meaning for the map itself, lets clear these
355 * from here.
356 */
357 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
358}
359
Jakub Kicinskibd475642018-01-11 20:29:06 -0800360void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
361{
362 map->map_type = attr->map_type;
363 map->key_size = attr->key_size;
364 map->value_size = attr->value_size;
365 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200366 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800367 map->numa_node = bpf_map_attr_numa_node(attr);
Joanne Koong93309862021-10-27 16:45:00 -0700368 map->map_extra = attr->map_extra;
Jakub Kicinskibd475642018-01-11 20:29:06 -0800369}
370
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700371static int bpf_map_alloc_id(struct bpf_map *map)
372{
373 int id;
374
Shaohua Lib76354c2018-03-27 11:53:21 -0700375 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700376 spin_lock_bh(&map_idr_lock);
377 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
378 if (id > 0)
379 map->id = id;
380 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700381 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700382
383 if (WARN_ON_ONCE(!id))
384 return -ENOSPC;
385
386 return id > 0 ? 0 : id;
387}
388
Jakub Kicinskia3884572018-01-11 20:29:09 -0800389void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700390{
Eric Dumazet930651a2017-09-19 09:15:59 -0700391 unsigned long flags;
392
Jakub Kicinskia3884572018-01-11 20:29:09 -0800393 /* Offloaded maps are removed from the IDR store when their device
394 * disappears - even if someone holds an fd to them they are unusable,
395 * the memory is gone, all ops will fail; they are simply waiting for
396 * refcnt to drop to be freed.
397 */
398 if (!map->id)
399 return;
400
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700401 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700402 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700403 else
404 __acquire(&map_idr_lock);
405
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700406 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800407 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700408
409 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700410 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700411 else
412 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700413}
414
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800415#ifdef CONFIG_MEMCG_KMEM
416static void bpf_map_save_memcg(struct bpf_map *map)
417{
418 map->memcg = get_mem_cgroup_from_mm(current->mm);
419}
420
421static void bpf_map_release_memcg(struct bpf_map *map)
422{
423 mem_cgroup_put(map->memcg);
424}
425
426void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
427 int node)
428{
429 struct mem_cgroup *old_memcg;
430 void *ptr;
431
432 old_memcg = set_active_memcg(map->memcg);
433 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
434 set_active_memcg(old_memcg);
435
436 return ptr;
437}
438
439void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
440{
441 struct mem_cgroup *old_memcg;
442 void *ptr;
443
444 old_memcg = set_active_memcg(map->memcg);
445 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
446 set_active_memcg(old_memcg);
447
448 return ptr;
449}
450
451void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
452 size_t align, gfp_t flags)
453{
454 struct mem_cgroup *old_memcg;
455 void __percpu *ptr;
456
457 old_memcg = set_active_memcg(map->memcg);
458 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
459 set_active_memcg(old_memcg);
460
461 return ptr;
462}
463
464#else
465static void bpf_map_save_memcg(struct bpf_map *map)
466{
467}
468
469static void bpf_map_release_memcg(struct bpf_map *map)
470{
471}
472#endif
473
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700474/* called from workqueue */
475static void bpf_map_free_deferred(struct work_struct *work)
476{
477 struct bpf_map *map = container_of(work, struct bpf_map, work);
478
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700479 security_bpf_map_free(map);
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800480 bpf_map_release_memcg(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700481 /* implementation dependent freeing */
482 map->ops->map_free(map);
483}
484
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100485static void bpf_map_put_uref(struct bpf_map *map)
486{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800487 if (atomic64_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700488 if (map->ops->map_release_uref)
489 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100490 }
491}
492
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700493/* decrement map refcnt and schedule it for freeing via workqueue
494 * (unrelying map implementation ops->map_free() might sleep)
495 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700496static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700497{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800498 if (atomic64_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700499 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700500 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700501 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700502 INIT_WORK(&map->work, bpf_map_free_deferred);
503 schedule_work(&map->work);
504 }
505}
506
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700507void bpf_map_put(struct bpf_map *map)
508{
509 __bpf_map_put(map, true);
510}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700511EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700512
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100513void bpf_map_put_with_uref(struct bpf_map *map)
514{
515 bpf_map_put_uref(map);
516 bpf_map_put(map);
517}
518
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700519static int bpf_map_release(struct inode *inode, struct file *filp)
520{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200521 struct bpf_map *map = filp->private_data;
522
523 if (map->ops->map_release)
524 map->ops->map_release(map, filp);
525
526 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700527 return 0;
528}
529
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200530static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
531{
532 fmode_t mode = f.file->f_mode;
533
534 /* Our file permissions may have been overridden by global
535 * map permissions facing syscall side.
536 */
537 if (READ_ONCE(map->frozen))
538 mode &= ~FMODE_CAN_WRITE;
539 return mode;
540}
541
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100542#ifdef CONFIG_PROC_FS
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800543/* Provides an approximation of the map's memory footprint.
544 * Used only to provide a backward compatibility and display
545 * a reasonable "memlock" info.
546 */
547static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
548{
549 unsigned long size;
550
551 size = round_up(map->key_size + bpf_map_value_size(map), 8);
552
553 return round_up(map->max_entries * size, PAGE_SIZE);
554}
555
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100556static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
557{
558 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100559 const struct bpf_array *array;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100560 u32 type = 0, jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100561
562 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
563 array = container_of(map, struct bpf_array, map);
Toke Høiland-Jørgensen54713c82021-10-26 13:00:19 +0200564 spin_lock(&array->aux->owner.lock);
565 type = array->aux->owner.type;
566 jited = array->aux->owner.jited;
567 spin_unlock(&array->aux->owner.lock);
Daniel Borkmann21116b72016-11-26 01:28:07 +0100568 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100569
570 seq_printf(m,
571 "map_type:\t%u\n"
572 "key_size:\t%u\n"
573 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100574 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100575 "map_flags:\t%#x\n"
Joanne Koong93309862021-10-27 16:45:00 -0700576 "map_extra:\t%#llx\n"
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800577 "memlock:\t%lu\n"
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200578 "map_id:\t%u\n"
579 "frozen:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100580 map->map_type,
581 map->key_size,
582 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100583 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100584 map->map_flags,
Joanne Koong93309862021-10-27 16:45:00 -0700585 (unsigned long long)map->map_extra,
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800586 bpf_map_memory_footprint(map),
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200587 map->id,
588 READ_ONCE(map->frozen));
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100589 if (type) {
590 seq_printf(m, "owner_prog_type:\t%u\n", type);
591 seq_printf(m, "owner_jited:\t%u\n", jited);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200592 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100593}
594#endif
595
Chenbo Feng6e71b042017-10-18 13:00:22 -0700596static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
597 loff_t *ppos)
598{
599 /* We need this handler such that alloc_file() enables
600 * f_mode with FMODE_CAN_READ.
601 */
602 return -EINVAL;
603}
604
605static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
606 size_t siz, loff_t *ppos)
607{
608 /* We need this handler such that alloc_file() enables
609 * f_mode with FMODE_CAN_WRITE.
610 */
611 return -EINVAL;
612}
613
Andrii Nakryikofc970222019-11-17 09:28:04 -0800614/* called for any extra memory-mapped regions (except initial) */
615static void bpf_map_mmap_open(struct vm_area_struct *vma)
616{
617 struct bpf_map *map = vma->vm_file->private_data;
618
Daniel Borkmann353050b2021-11-09 18:48:08 +0000619 if (vma->vm_flags & VM_MAYWRITE)
620 bpf_map_write_active_inc(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800621}
622
623/* called for all unmapped memory region (including initial) */
624static void bpf_map_mmap_close(struct vm_area_struct *vma)
625{
626 struct bpf_map *map = vma->vm_file->private_data;
627
Daniel Borkmann353050b2021-11-09 18:48:08 +0000628 if (vma->vm_flags & VM_MAYWRITE)
629 bpf_map_write_active_dec(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800630}
631
632static const struct vm_operations_struct bpf_map_default_vmops = {
633 .open = bpf_map_mmap_open,
634 .close = bpf_map_mmap_close,
635};
636
637static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
638{
639 struct bpf_map *map = filp->private_data;
640 int err;
641
Alexei Starovoitov68134662021-07-14 17:54:10 -0700642 if (!map->ops->map_mmap || map_value_has_spin_lock(map) ||
643 map_value_has_timer(map))
Andrii Nakryikofc970222019-11-17 09:28:04 -0800644 return -ENOTSUPP;
645
646 if (!(vma->vm_flags & VM_SHARED))
647 return -EINVAL;
648
649 mutex_lock(&map->freeze_mutex);
650
Andrii Nakryikodfeb3762020-05-18 22:38:24 -0700651 if (vma->vm_flags & VM_WRITE) {
652 if (map->frozen) {
653 err = -EPERM;
654 goto out;
655 }
656 /* map is meant to be read-only, so do not allow mapping as
657 * writable, because it's possible to leak a writable page
658 * reference and allows user-space to still modify it after
659 * freezing, while verifier will assume contents do not change
660 */
661 if (map->map_flags & BPF_F_RDONLY_PROG) {
662 err = -EACCES;
663 goto out;
664 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800665 }
666
667 /* set default open/close callbacks */
668 vma->vm_ops = &bpf_map_default_vmops;
669 vma->vm_private_data = map;
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700670 vma->vm_flags &= ~VM_MAYEXEC;
671 if (!(vma->vm_flags & VM_WRITE))
672 /* disallow re-mapping with PROT_WRITE */
673 vma->vm_flags &= ~VM_MAYWRITE;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800674
675 err = map->ops->map_mmap(map, vma);
676 if (err)
677 goto out;
678
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700679 if (vma->vm_flags & VM_MAYWRITE)
Daniel Borkmann353050b2021-11-09 18:48:08 +0000680 bpf_map_write_active_inc(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800681out:
682 mutex_unlock(&map->freeze_mutex);
683 return err;
684}
685
Andrii Nakryiko457f4432020-05-29 00:54:20 -0700686static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
687{
688 struct bpf_map *map = filp->private_data;
689
690 if (map->ops->map_poll)
691 return map->ops->map_poll(map, filp, pts);
692
693 return EPOLLERR;
694}
695
Chenbo Fengf66e4482017-10-18 13:00:26 -0700696const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100697#ifdef CONFIG_PROC_FS
698 .show_fdinfo = bpf_map_show_fdinfo,
699#endif
700 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700701 .read = bpf_dummy_read,
702 .write = bpf_dummy_write,
Andrii Nakryikofc970222019-11-17 09:28:04 -0800703 .mmap = bpf_map_mmap,
Andrii Nakryiko457f4432020-05-29 00:54:20 -0700704 .poll = bpf_map_poll,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700705};
706
Chenbo Feng6e71b042017-10-18 13:00:22 -0700707int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100708{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700709 int ret;
710
711 ret = security_bpf_map(map, OPEN_FMODE(flags));
712 if (ret < 0)
713 return ret;
714
Daniel Borkmannaa797812015-10-29 14:58:06 +0100715 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700716 flags | O_CLOEXEC);
717}
718
719int bpf_get_file_flag(int flags)
720{
721 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
722 return -EINVAL;
723 if (flags & BPF_F_RDONLY)
724 return O_RDONLY;
725 if (flags & BPF_F_WRONLY)
726 return O_WRONLY;
727 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100728}
729
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700730/* helper macro to check that unused fields 'union bpf_attr' are zero */
731#define CHECK_ATTR(CMD) \
732 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
733 sizeof(attr->CMD##_LAST_FIELD), 0, \
734 sizeof(*attr) - \
735 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
736 sizeof(attr->CMD##_LAST_FIELD)) != NULL
737
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700738/* dst and src must have at least "size" number of bytes.
739 * Return strlen on success and < 0 on error.
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700740 */
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700741int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700742{
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700743 const char *end = src + size;
744 const char *orig_src = src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700745
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700746 memset(dst, 0, size);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200747 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700748 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200749 if (!isalnum(*src) &&
750 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700751 return -EINVAL;
752 *dst++ = *src++;
753 }
754
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700755 /* No '\0' found in "size" number of bytes */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700756 if (src == end)
757 return -EINVAL;
758
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700759 return src - orig_src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700760}
761
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200762int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800763 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200764 const struct btf_type *key_type,
765 const struct btf_type *value_type)
766{
767 return -ENOTSUPP;
768}
769
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800770static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200771 u32 btf_key_id, u32 btf_value_id)
772{
773 const struct btf_type *key_type, *value_type;
774 u32 key_size, value_size;
775 int ret = 0;
776
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200777 /* Some maps allow key to be unspecified. */
778 if (btf_key_id) {
779 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
780 if (!key_type || key_size != map->key_size)
781 return -EINVAL;
782 } else {
783 key_type = btf_type_by_id(btf, 0);
784 if (!map->ops->map_check_btf)
785 return -EINVAL;
786 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200787
788 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
789 if (!value_type || value_size != map->value_size)
790 return -EINVAL;
791
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800792 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
793
794 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200795 if (map->map_flags & BPF_F_RDONLY_PROG)
796 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800797 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -0800798 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700799 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
KP Singh8ea63682020-08-25 20:29:17 +0200800 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
KP Singh4cf1bc12020-11-06 10:37:40 +0000801 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
802 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800803 return -ENOTSUPP;
804 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
805 map->value_size) {
806 WARN_ONCE(1,
807 "verifier bug spin_lock_off %d value_size %d\n",
808 map->spin_lock_off, map->value_size);
809 return -EFAULT;
810 }
811 }
812
Alexei Starovoitov68134662021-07-14 17:54:10 -0700813 map->timer_off = btf_find_timer(btf, value_type);
814 if (map_value_has_timer(map)) {
815 if (map->map_flags & BPF_F_RDONLY_PROG)
816 return -EACCES;
817 if (map->map_type != BPF_MAP_TYPE_HASH &&
818 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
819 map->map_type != BPF_MAP_TYPE_ARRAY)
820 return -EOPNOTSUPP;
821 }
822
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200823 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800824 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200825
826 return ret;
827}
828
Joanne Koong93309862021-10-27 16:45:00 -0700829#define BPF_MAP_CREATE_LAST_FIELD map_extra
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700830/* called via syscall */
831static int map_create(union bpf_attr *attr)
832{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700833 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700834 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700835 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700836 int err;
837
838 err = CHECK_ATTR(BPF_MAP_CREATE);
839 if (err)
840 return -EINVAL;
841
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800842 if (attr->btf_vmlinux_value_type_id) {
843 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
844 attr->btf_key_type_id || attr->btf_value_type_id)
845 return -EINVAL;
846 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
847 return -EINVAL;
848 }
849
Joanne Koong93309862021-10-27 16:45:00 -0700850 if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER &&
851 attr->map_extra != 0)
852 return -EINVAL;
853
Chenbo Feng6e71b042017-10-18 13:00:22 -0700854 f_flags = bpf_get_file_flag(attr->map_flags);
855 if (f_flags < 0)
856 return f_flags;
857
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700858 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700859 ((unsigned int)numa_node >= nr_node_ids ||
860 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700861 return -EINVAL;
862
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700863 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
864 map = find_and_alloc_map(attr);
865 if (IS_ERR(map))
866 return PTR_ERR(map);
867
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700868 err = bpf_obj_name_cpy(map->name, attr->map_name,
869 sizeof(attr->map_name));
870 if (err < 0)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700871 goto free_map;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700872
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800873 atomic64_set(&map->refcnt, 1);
874 atomic64_set(&map->usercnt, 1);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800875 mutex_init(&map->freeze_mutex);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700876
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800877 map->spin_lock_off = -EINVAL;
Alexei Starovoitov68134662021-07-14 17:54:10 -0700878 map->timer_off = -EINVAL;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800879 if (attr->btf_key_type_id || attr->btf_value_type_id ||
880 /* Even the map's value is a kernel's struct,
881 * the bpf_prog.o must have BTF to begin with
882 * to figure out the corresponding kernel's
883 * counter part. Thus, attr->btf_fd has
884 * to be valid also.
885 */
886 attr->btf_vmlinux_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700887 struct btf *btf;
888
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700889 btf = btf_get_by_fd(attr->btf_fd);
890 if (IS_ERR(btf)) {
891 err = PTR_ERR(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700892 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700893 }
Alexei Starovoitov350a5c42021-03-07 14:52:48 -0800894 if (btf_is_kernel(btf)) {
895 btf_put(btf);
896 err = -EACCES;
897 goto free_map;
898 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800899 map->btf = btf;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700900
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800901 if (attr->btf_value_type_id) {
902 err = map_check_btf(map, btf, attr->btf_key_type_id,
903 attr->btf_value_type_id);
904 if (err)
905 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700906 }
907
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700908 map->btf_key_type_id = attr->btf_key_type_id;
909 map->btf_value_type_id = attr->btf_value_type_id;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800910 map->btf_vmlinux_value_type_id =
911 attr->btf_vmlinux_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700912 }
913
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700914 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700915 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700916 goto free_map;
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700917
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700918 err = bpf_map_alloc_id(map);
919 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700920 goto free_map_sec;
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700921
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800922 bpf_map_save_memcg(map);
923
Chenbo Feng6e71b042017-10-18 13:00:22 -0700924 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700925 if (err < 0) {
926 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800927 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700928 * bpf_map_alloc_id() has published the map
929 * to the userspace and the userspace may
930 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
931 */
Peng Sun352d20d2019-02-27 22:36:25 +0800932 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700933 return err;
934 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700935
936 return err;
937
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700938free_map_sec:
939 security_bpf_map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700940free_map:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700941 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700942 map->ops->map_free(map);
943 return err;
944}
945
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700946/* if error is returned, fd is released.
947 * On success caller should complete fd access with matching fdput()
948 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100949struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700950{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700951 if (!f.file)
952 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700953 if (f.file->f_op != &bpf_map_fops) {
954 fdput(f);
955 return ERR_PTR(-EINVAL);
956 }
957
Daniel Borkmannc2101292015-10-29 14:58:07 +0100958 return f.file->private_data;
959}
960
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800961void bpf_map_inc(struct bpf_map *map)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100962{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800963 atomic64_inc(&map->refcnt);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100964}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700965EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100966
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800967void bpf_map_inc_with_uref(struct bpf_map *map)
968{
969 atomic64_inc(&map->refcnt);
970 atomic64_inc(&map->usercnt);
971}
972EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
973
Martin KaFai Lau1ed4d922020-02-25 15:04:21 -0800974struct bpf_map *bpf_map_get(u32 ufd)
975{
976 struct fd f = fdget(ufd);
977 struct bpf_map *map;
978
979 map = __bpf_map_get(f);
980 if (IS_ERR(map))
981 return map;
982
983 bpf_map_inc(map);
984 fdput(f);
985
986 return map;
987}
988
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100989struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100990{
991 struct fd f = fdget(ufd);
992 struct bpf_map *map;
993
994 map = __bpf_map_get(f);
995 if (IS_ERR(map))
996 return map;
997
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800998 bpf_map_inc_with_uref(map);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100999 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001000
1001 return map;
1002}
1003
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001004/* map_idr_lock should have been held */
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001005static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001006{
1007 int refold;
1008
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001009 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001010 if (!refold)
1011 return ERR_PTR(-ENOENT);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001012 if (uref)
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001013 atomic64_inc(&map->usercnt);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001014
1015 return map;
1016}
1017
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001018struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -07001019{
1020 spin_lock_bh(&map_idr_lock);
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001021 map = __bpf_map_inc_not_zero(map, false);
Stanislav Fomichevb0e47012019-08-14 10:37:48 -07001022 spin_unlock_bh(&map_idr_lock);
1023
1024 return map;
1025}
1026EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
1027
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -08001028int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1029{
1030 return -ENOTSUPP;
1031}
1032
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001033static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1034{
1035 if (key_size)
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001036 return vmemdup_user(ukey, key_size);
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001037
1038 if (ukey)
1039 return ERR_PTR(-EINVAL);
1040
1041 return NULL;
1042}
1043
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001044static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1045{
1046 if (key_size)
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001047 return kvmemdup_bpfptr(ukey, key_size);
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001048
1049 if (!bpfptr_is_null(ukey))
1050 return ERR_PTR(-EINVAL);
1051
1052 return NULL;
1053}
1054
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001055/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001056#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001057
1058static int map_lookup_elem(union bpf_attr *attr)
1059{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001060 void __user *ukey = u64_to_user_ptr(attr->key);
1061 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001062 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001063 struct bpf_map *map;
Brian Vazquez15c14a32020-01-15 10:43:00 -08001064 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001065 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001066 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001067 int err;
1068
1069 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1070 return -EINVAL;
1071
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001072 if (attr->flags & ~BPF_F_LOCK)
1073 return -EINVAL;
1074
Daniel Borkmann592867b2015-09-08 18:00:09 +02001075 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001076 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001077 if (IS_ERR(map))
1078 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001079 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001080 err = -EPERM;
1081 goto err_put;
1082 }
1083
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001084 if ((attr->flags & BPF_F_LOCK) &&
1085 !map_value_has_spin_lock(map)) {
1086 err = -EINVAL;
1087 goto err_put;
1088 }
1089
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001090 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001091 if (IS_ERR(key)) {
1092 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001093 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001094 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001095
Brian Vazquez15c14a32020-01-15 10:43:00 -08001096 value_size = bpf_map_value_size(map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001097
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001098 err = -ENOMEM;
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001099 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001100 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001101 goto free_key;
1102
Joanne Koong93309862021-10-27 16:45:00 -07001103 if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
1104 if (copy_from_user(value, uvalue, value_size))
1105 err = -EFAULT;
1106 else
1107 err = bpf_map_copy_value(map, key, value, attr->flags);
1108 goto free_value;
1109 }
1110
Brian Vazquez15c14a32020-01-15 10:43:00 -08001111 err = bpf_map_copy_value(map, key, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001112 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001113 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001114
1115 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001116 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001117 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001118
1119 err = 0;
1120
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001121free_value:
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001122 kvfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001123free_key:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001124 kvfree(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001125err_put:
1126 fdput(f);
1127 return err;
1128}
1129
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001130
Alexei Starovoitov3274f522014-11-13 17:36:44 -08001131#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001132
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001133static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001134{
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001135 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1136 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001137 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001138 struct bpf_map *map;
1139 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001140 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001141 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001142 int err;
1143
1144 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1145 return -EINVAL;
1146
Daniel Borkmann592867b2015-09-08 18:00:09 +02001147 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001148 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001149 if (IS_ERR(map))
1150 return PTR_ERR(map);
Daniel Borkmann353050b2021-11-09 18:48:08 +00001151 bpf_map_write_active_inc(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001152 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001153 err = -EPERM;
1154 goto err_put;
1155 }
1156
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001157 if ((attr->flags & BPF_F_LOCK) &&
1158 !map_value_has_spin_lock(map)) {
1159 err = -EINVAL;
1160 goto err_put;
1161 }
1162
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001163 key = ___bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001164 if (IS_ERR(key)) {
1165 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001166 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001167 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001168
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001169 value_size = bpf_map_value_size(map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001170
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001171 err = -ENOMEM;
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001172 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001173 if (!value)
1174 goto free_key;
1175
1176 err = -EFAULT;
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001177 if (copy_from_bpfptr(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001178 goto free_value;
1179
Brian Vazquez15c14a32020-01-15 10:43:00 -08001180 err = bpf_map_update_value(map, f, key, value, attr->flags);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001181
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001182free_value:
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001183 kvfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001184free_key:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001185 kvfree(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001186err_put:
Daniel Borkmann353050b2021-11-09 18:48:08 +00001187 bpf_map_write_active_dec(map);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001188 fdput(f);
1189 return err;
1190}
1191
1192#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1193
1194static int map_delete_elem(union bpf_attr *attr)
1195{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001196 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001197 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001198 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001199 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001200 void *key;
1201 int err;
1202
1203 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1204 return -EINVAL;
1205
Daniel Borkmann592867b2015-09-08 18:00:09 +02001206 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001207 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001208 if (IS_ERR(map))
1209 return PTR_ERR(map);
Daniel Borkmann353050b2021-11-09 18:48:08 +00001210 bpf_map_write_active_inc(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001211 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001212 err = -EPERM;
1213 goto err_put;
1214 }
1215
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001216 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001217 if (IS_ERR(key)) {
1218 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001219 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001220 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001221
Jakub Kicinskia3884572018-01-11 20:29:09 -08001222 if (bpf_map_is_dev_bound(map)) {
1223 err = bpf_map_offload_delete_elem(map, key);
1224 goto out;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001225 } else if (IS_FD_PROG_ARRAY(map) ||
1226 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1227 /* These maps require sleepable context */
Daniel Borkmannda765a22019-11-22 21:07:58 +01001228 err = map->ops->map_delete_elem(map, key);
1229 goto out;
Jakub Kicinskia3884572018-01-11 20:29:09 -08001230 }
1231
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001232 bpf_disable_instrumentation();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001233 rcu_read_lock();
1234 err = map->ops->map_delete_elem(map, key);
1235 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001236 bpf_enable_instrumentation();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001237 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001238out:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001239 kvfree(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001240err_put:
Daniel Borkmann353050b2021-11-09 18:48:08 +00001241 bpf_map_write_active_dec(map);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001242 fdput(f);
1243 return err;
1244}
1245
1246/* last field in 'union bpf_attr' used by this command */
1247#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1248
1249static int map_get_next_key(union bpf_attr *attr)
1250{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001251 void __user *ukey = u64_to_user_ptr(attr->key);
1252 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001253 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001254 struct bpf_map *map;
1255 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001256 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001257 int err;
1258
1259 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1260 return -EINVAL;
1261
Daniel Borkmann592867b2015-09-08 18:00:09 +02001262 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001263 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001264 if (IS_ERR(map))
1265 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001266 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001267 err = -EPERM;
1268 goto err_put;
1269 }
1270
Teng Qin8fe45922017-04-24 19:00:37 -07001271 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001272 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001273 if (IS_ERR(key)) {
1274 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001275 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001276 }
Teng Qin8fe45922017-04-24 19:00:37 -07001277 } else {
1278 key = NULL;
1279 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001280
1281 err = -ENOMEM;
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001282 next_key = kvmalloc(map->key_size, GFP_USER);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001283 if (!next_key)
1284 goto free_key;
1285
Jakub Kicinskia3884572018-01-11 20:29:09 -08001286 if (bpf_map_is_dev_bound(map)) {
1287 err = bpf_map_offload_get_next_key(map, key, next_key);
1288 goto out;
1289 }
1290
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001291 rcu_read_lock();
1292 err = map->ops->map_get_next_key(map, key, next_key);
1293 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001294out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001295 if (err)
1296 goto free_next_key;
1297
1298 err = -EFAULT;
1299 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1300 goto free_next_key;
1301
1302 err = 0;
1303
1304free_next_key:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001305 kvfree(next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001306free_key:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001307 kvfree(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001308err_put:
1309 fdput(f);
1310 return err;
1311}
1312
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001313int generic_map_delete_batch(struct bpf_map *map,
1314 const union bpf_attr *attr,
1315 union bpf_attr __user *uattr)
1316{
1317 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1318 u32 cp, max_count;
1319 int err = 0;
1320 void *key;
1321
1322 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1323 return -EINVAL;
1324
1325 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1326 !map_value_has_spin_lock(map)) {
1327 return -EINVAL;
1328 }
1329
1330 max_count = attr->batch.count;
1331 if (!max_count)
1332 return 0;
1333
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001334 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001335 if (!key)
1336 return -ENOMEM;
1337
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001338 for (cp = 0; cp < max_count; cp++) {
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001339 err = -EFAULT;
1340 if (copy_from_user(key, keys + cp * map->key_size,
1341 map->key_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001342 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001343
1344 if (bpf_map_is_dev_bound(map)) {
1345 err = bpf_map_offload_delete_elem(map, key);
1346 break;
1347 }
1348
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001349 bpf_disable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001350 rcu_read_lock();
1351 err = map->ops->map_delete_elem(map, key);
1352 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001353 bpf_enable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001354 maybe_wait_bpf_programs(map);
1355 if (err)
1356 break;
1357 }
1358 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1359 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001360
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001361 kvfree(key);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001362 return err;
1363}
1364
1365int generic_map_update_batch(struct bpf_map *map,
1366 const union bpf_attr *attr,
1367 union bpf_attr __user *uattr)
1368{
1369 void __user *values = u64_to_user_ptr(attr->batch.values);
1370 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1371 u32 value_size, cp, max_count;
Xu Kuohaifda7a382021-10-19 03:29:34 +00001372 int ufd = attr->batch.map_fd;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001373 void *key, *value;
1374 struct fd f;
1375 int err = 0;
1376
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001377 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1378 return -EINVAL;
1379
1380 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1381 !map_value_has_spin_lock(map)) {
1382 return -EINVAL;
1383 }
1384
1385 value_size = bpf_map_value_size(map);
1386
1387 max_count = attr->batch.count;
1388 if (!max_count)
1389 return 0;
1390
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001391 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001392 if (!key)
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001393 return -ENOMEM;
1394
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001395 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001396 if (!value) {
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001397 kvfree(key);
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001398 return -ENOMEM;
1399 }
1400
Xu Kuohaifda7a382021-10-19 03:29:34 +00001401 f = fdget(ufd); /* bpf_map_do_batch() guarantees ufd is valid */
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001402 for (cp = 0; cp < max_count; cp++) {
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001403 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001404 if (copy_from_user(key, keys + cp * map->key_size,
1405 map->key_size) ||
1406 copy_from_user(value, values + cp * value_size, value_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001407 break;
1408
1409 err = bpf_map_update_value(map, f, key, value,
1410 attr->batch.elem_flags);
1411
1412 if (err)
1413 break;
1414 }
1415
1416 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1417 err = -EFAULT;
1418
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001419 kvfree(value);
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001420 kvfree(key);
Xu Kuohaifda7a382021-10-19 03:29:34 +00001421 fdput(f);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001422 return err;
1423}
1424
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001425#define MAP_LOOKUP_RETRIES 3
1426
1427int generic_map_lookup_batch(struct bpf_map *map,
1428 const union bpf_attr *attr,
1429 union bpf_attr __user *uattr)
1430{
1431 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1432 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1433 void __user *values = u64_to_user_ptr(attr->batch.values);
1434 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1435 void *buf, *buf_prevkey, *prev_key, *key, *value;
1436 int err, retry = MAP_LOOKUP_RETRIES;
1437 u32 value_size, cp, max_count;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001438
1439 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1440 return -EINVAL;
1441
1442 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1443 !map_value_has_spin_lock(map))
1444 return -EINVAL;
1445
1446 value_size = bpf_map_value_size(map);
1447
1448 max_count = attr->batch.count;
1449 if (!max_count)
1450 return 0;
1451
1452 if (put_user(0, &uattr->batch.count))
1453 return -EFAULT;
1454
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001455 buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001456 if (!buf_prevkey)
1457 return -ENOMEM;
1458
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001459 buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001460 if (!buf) {
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001461 kvfree(buf_prevkey);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001462 return -ENOMEM;
1463 }
1464
1465 err = -EFAULT;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001466 prev_key = NULL;
1467 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1468 goto free_buf;
1469 key = buf;
1470 value = key + map->key_size;
1471 if (ubatch)
1472 prev_key = buf_prevkey;
1473
1474 for (cp = 0; cp < max_count;) {
1475 rcu_read_lock();
1476 err = map->ops->map_get_next_key(map, prev_key, key);
1477 rcu_read_unlock();
1478 if (err)
1479 break;
1480 err = bpf_map_copy_value(map, key, value,
1481 attr->batch.elem_flags);
1482
1483 if (err == -ENOENT) {
1484 if (retry) {
1485 retry--;
1486 continue;
1487 }
1488 err = -EINTR;
1489 break;
1490 }
1491
1492 if (err)
1493 goto free_buf;
1494
1495 if (copy_to_user(keys + cp * map->key_size, key,
1496 map->key_size)) {
1497 err = -EFAULT;
1498 goto free_buf;
1499 }
1500 if (copy_to_user(values + cp * value_size, value, value_size)) {
1501 err = -EFAULT;
1502 goto free_buf;
1503 }
1504
1505 if (!prev_key)
1506 prev_key = buf_prevkey;
1507
1508 swap(prev_key, key);
1509 retry = MAP_LOOKUP_RETRIES;
1510 cp++;
1511 }
1512
1513 if (err == -EFAULT)
1514 goto free_buf;
1515
1516 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1517 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1518 err = -EFAULT;
1519
1520free_buf:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001521 kvfree(buf_prevkey);
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001522 kvfree(buf);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001523 return err;
1524}
1525
Denis Salopek3e87f192021-05-11 23:00:04 +02001526#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001527
1528static int map_lookup_and_delete_elem(union bpf_attr *attr)
1529{
1530 void __user *ukey = u64_to_user_ptr(attr->key);
1531 void __user *uvalue = u64_to_user_ptr(attr->value);
1532 int ufd = attr->map_fd;
1533 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001534 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001535 u32 value_size;
1536 struct fd f;
1537 int err;
1538
1539 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1540 return -EINVAL;
1541
Denis Salopek3e87f192021-05-11 23:00:04 +02001542 if (attr->flags & ~BPF_F_LOCK)
1543 return -EINVAL;
1544
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001545 f = fdget(ufd);
1546 map = __bpf_map_get(f);
1547 if (IS_ERR(map))
1548 return PTR_ERR(map);
Daniel Borkmann353050b2021-11-09 18:48:08 +00001549 bpf_map_write_active_inc(map);
Anton Protopopov1ea0f912020-05-27 18:56:59 +00001550 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1551 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001552 err = -EPERM;
1553 goto err_put;
1554 }
1555
Denis Salopek3e87f192021-05-11 23:00:04 +02001556 if (attr->flags &&
1557 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1558 map->map_type == BPF_MAP_TYPE_STACK)) {
1559 err = -EINVAL;
1560 goto err_put;
1561 }
1562
1563 if ((attr->flags & BPF_F_LOCK) &&
1564 !map_value_has_spin_lock(map)) {
1565 err = -EINVAL;
1566 goto err_put;
1567 }
1568
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001569 key = __bpf_copy_key(ukey, map->key_size);
1570 if (IS_ERR(key)) {
1571 err = PTR_ERR(key);
1572 goto err_put;
1573 }
1574
Denis Salopek3e87f192021-05-11 23:00:04 +02001575 value_size = bpf_map_value_size(map);
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001576
1577 err = -ENOMEM;
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001578 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001579 if (!value)
1580 goto free_key;
1581
Denis Salopek3e87f192021-05-11 23:00:04 +02001582 err = -ENOTSUPP;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001583 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1584 map->map_type == BPF_MAP_TYPE_STACK) {
1585 err = map->ops->map_pop_elem(map, value);
Denis Salopek3e87f192021-05-11 23:00:04 +02001586 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1587 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1588 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1589 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1590 if (!bpf_map_is_dev_bound(map)) {
1591 bpf_disable_instrumentation();
1592 rcu_read_lock();
1593 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1594 rcu_read_unlock();
1595 bpf_enable_instrumentation();
1596 }
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001597 }
1598
1599 if (err)
1600 goto free_value;
1601
Wei Yongjun7f645462020-04-30 08:18:51 +00001602 if (copy_to_user(uvalue, value, value_size) != 0) {
1603 err = -EFAULT;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001604 goto free_value;
Wei Yongjun7f645462020-04-30 08:18:51 +00001605 }
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001606
1607 err = 0;
1608
1609free_value:
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001610 kvfree(value);
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001611free_key:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001612 kvfree(key);
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001613err_put:
Daniel Borkmann353050b2021-11-09 18:48:08 +00001614 bpf_map_write_active_dec(map);
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001615 fdput(f);
1616 return err;
1617}
1618
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001619#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1620
1621static int map_freeze(const union bpf_attr *attr)
1622{
1623 int err = 0, ufd = attr->map_fd;
1624 struct bpf_map *map;
1625 struct fd f;
1626
1627 if (CHECK_ATTR(BPF_MAP_FREEZE))
1628 return -EINVAL;
1629
1630 f = fdget(ufd);
1631 map = __bpf_map_get(f);
1632 if (IS_ERR(map))
1633 return PTR_ERR(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -08001634
Alexei Starovoitov68134662021-07-14 17:54:10 -07001635 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS ||
1636 map_value_has_timer(map)) {
Martin KaFai Lau849b4d92020-03-04 17:34:54 -08001637 fdput(f);
1638 return -ENOTSUPP;
1639 }
1640
Andrii Nakryikofc970222019-11-17 09:28:04 -08001641 mutex_lock(&map->freeze_mutex);
Daniel Borkmann353050b2021-11-09 18:48:08 +00001642 if (bpf_map_write_active(map)) {
Andrii Nakryikofc970222019-11-17 09:28:04 -08001643 err = -EBUSY;
1644 goto err_put;
1645 }
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001646 if (READ_ONCE(map->frozen)) {
1647 err = -EBUSY;
1648 goto err_put;
1649 }
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07001650 if (!bpf_capable()) {
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001651 err = -EPERM;
1652 goto err_put;
1653 }
1654
1655 WRITE_ONCE(map->frozen, true);
1656err_put:
Andrii Nakryikofc970222019-11-17 09:28:04 -08001657 mutex_unlock(&map->freeze_mutex);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001658 fdput(f);
1659 return err;
1660}
1661
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001662static const struct bpf_prog_ops * const bpf_prog_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001663#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001664 [_id] = & _name ## _prog_ops,
1665#define BPF_MAP_TYPE(_id, _ops)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001666#define BPF_LINK_TYPE(_id, _name)
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001667#include <linux/bpf_types.h>
1668#undef BPF_PROG_TYPE
1669#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001670#undef BPF_LINK_TYPE
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001671};
1672
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001673static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1674{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001675 const struct bpf_prog_ops *ops;
1676
1677 if (type >= ARRAY_SIZE(bpf_prog_types))
1678 return -EINVAL;
1679 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1680 ops = bpf_prog_types[type];
1681 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001682 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001683
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001684 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001685 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001686 else
1687 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001688 prog->type = type;
1689 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001690}
1691
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001692enum bpf_audit {
1693 BPF_AUDIT_LOAD,
1694 BPF_AUDIT_UNLOAD,
1695 BPF_AUDIT_MAX,
1696};
1697
1698static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1699 [BPF_AUDIT_LOAD] = "LOAD",
1700 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1701};
1702
1703static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1704{
1705 struct audit_context *ctx = NULL;
1706 struct audit_buffer *ab;
1707
1708 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1709 return;
1710 if (audit_enabled == AUDIT_OFF)
1711 return;
1712 if (op == BPF_AUDIT_LOAD)
1713 ctx = audit_context();
1714 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1715 if (unlikely(!ab))
1716 return;
1717 audit_log_format(ab, "prog-id=%u op=%s",
1718 prog->aux->id, bpf_audit_str[op]);
1719 audit_log_end(ab);
1720}
1721
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001722static int bpf_prog_alloc_id(struct bpf_prog *prog)
1723{
1724 int id;
1725
Shaohua Lib76354c2018-03-27 11:53:21 -07001726 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001727 spin_lock_bh(&prog_idr_lock);
1728 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1729 if (id > 0)
1730 prog->aux->id = id;
1731 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001732 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001733
1734 /* id is in [1, INT_MAX) */
1735 if (WARN_ON_ONCE(!id))
1736 return -ENOSPC;
1737
1738 return id > 0 ? 0 : id;
1739}
1740
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001741void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001742{
Alexei Starovoitovd809e132021-07-14 17:54:07 -07001743 unsigned long flags;
1744
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001745 /* cBPF to eBPF migrations are currently not in the idr store.
1746 * Offloaded programs are removed from the store when their device
1747 * disappears - even if someone grabs an fd to them they are unusable,
1748 * simply waiting for refcnt to drop to be freed.
1749 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001750 if (!prog->aux->id)
1751 return;
1752
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001753 if (do_idr_lock)
Alexei Starovoitovd809e132021-07-14 17:54:07 -07001754 spin_lock_irqsave(&prog_idr_lock, flags);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001755 else
1756 __acquire(&prog_idr_lock);
1757
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001758 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001759 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001760
1761 if (do_idr_lock)
Alexei Starovoitovd809e132021-07-14 17:54:07 -07001762 spin_unlock_irqrestore(&prog_idr_lock, flags);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001763 else
1764 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001765}
1766
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001767static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001768{
1769 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1770
Daniel Borkmann3b4d9eb2019-10-22 23:30:38 +02001771 kvfree(aux->func_info);
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08001772 kfree(aux->func_info_aux);
Roman Gushchin3ac1f012020-12-01 13:58:59 -08001773 free_uid(aux->user);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001774 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001775 bpf_prog_free(aux->prog);
1776}
1777
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001778static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1779{
1780 bpf_prog_kallsyms_del_all(prog);
1781 btf_put(prog->aux->btf);
Martin KaFai Laue16301f2021-03-24 18:51:30 -07001782 kvfree(prog->aux->jited_linfo);
1783 kvfree(prog->aux->linfo);
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07001784 kfree(prog->aux->kfunc_tab);
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08001785 if (prog->aux->attach_btf)
1786 btf_put(prog->aux->attach_btf);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001787
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07001788 if (deferred) {
1789 if (prog->aux->sleepable)
1790 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1791 else
1792 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1793 } else {
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001794 __bpf_prog_put_rcu(&prog->aux->rcu);
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07001795 }
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001796}
1797
Alexei Starovoitovd809e132021-07-14 17:54:07 -07001798static void bpf_prog_put_deferred(struct work_struct *work)
1799{
1800 struct bpf_prog_aux *aux;
1801 struct bpf_prog *prog;
1802
1803 aux = container_of(work, struct bpf_prog_aux, work);
1804 prog = aux->prog;
1805 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
1806 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
1807 __bpf_prog_put_noref(prog, true);
1808}
1809
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001810static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001811{
Alexei Starovoitovd809e132021-07-14 17:54:07 -07001812 struct bpf_prog_aux *aux = prog->aux;
1813
1814 if (atomic64_dec_and_test(&aux->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001815 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001816 bpf_prog_free_id(prog, do_idr_lock);
Alexei Starovoitovd809e132021-07-14 17:54:07 -07001817
1818 if (in_irq() || irqs_disabled()) {
1819 INIT_WORK(&aux->work, bpf_prog_put_deferred);
1820 schedule_work(&aux->work);
1821 } else {
1822 bpf_prog_put_deferred(&aux->work);
1823 }
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001824 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001825}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001826
1827void bpf_prog_put(struct bpf_prog *prog)
1828{
1829 __bpf_prog_put(prog, true);
1830}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001831EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001832
1833static int bpf_prog_release(struct inode *inode, struct file *filp)
1834{
1835 struct bpf_prog *prog = filp->private_data;
1836
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001837 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001838 return 0;
1839}
1840
Eric Dumazet61a0aba2021-10-26 14:41:33 -07001841struct bpf_prog_kstats {
1842 u64 nsecs;
1843 u64 cnt;
1844 u64 misses;
1845};
1846
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001847static void bpf_prog_get_stats(const struct bpf_prog *prog,
Eric Dumazet61a0aba2021-10-26 14:41:33 -07001848 struct bpf_prog_kstats *stats)
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001849{
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001850 u64 nsecs = 0, cnt = 0, misses = 0;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001851 int cpu;
1852
1853 for_each_possible_cpu(cpu) {
1854 const struct bpf_prog_stats *st;
1855 unsigned int start;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001856 u64 tnsecs, tcnt, tmisses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001857
Alexei Starovoitov700d4792021-02-09 19:36:26 -08001858 st = per_cpu_ptr(prog->stats, cpu);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001859 do {
1860 start = u64_stats_fetch_begin_irq(&st->syncp);
Eric Dumazet61a0aba2021-10-26 14:41:33 -07001861 tnsecs = u64_stats_read(&st->nsecs);
1862 tcnt = u64_stats_read(&st->cnt);
1863 tmisses = u64_stats_read(&st->misses);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001864 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1865 nsecs += tnsecs;
1866 cnt += tcnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001867 misses += tmisses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001868 }
1869 stats->nsecs = nsecs;
1870 stats->cnt = cnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001871 stats->misses = misses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001872}
1873
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001874#ifdef CONFIG_PROC_FS
1875static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1876{
1877 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001878 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Eric Dumazet61a0aba2021-10-26 14:41:33 -07001879 struct bpf_prog_kstats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001880
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001881 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001882 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001883 seq_printf(m,
1884 "prog_type:\t%u\n"
1885 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001886 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001887 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001888 "prog_id:\t%u\n"
1889 "run_time_ns:\t%llu\n"
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001890 "run_cnt:\t%llu\n"
Dave Marchevskyaba64c72021-10-20 00:48:17 -07001891 "recursion_misses:\t%llu\n"
1892 "verified_insns:\t%u\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001893 prog->type,
1894 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001895 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001896 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001897 prog->aux->id,
1898 stats.nsecs,
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001899 stats.cnt,
Dave Marchevskyaba64c72021-10-20 00:48:17 -07001900 stats.misses,
1901 prog->aux->verified_insns);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001902}
1903#endif
1904
Chenbo Fengf66e4482017-10-18 13:00:26 -07001905const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001906#ifdef CONFIG_PROC_FS
1907 .show_fdinfo = bpf_prog_show_fdinfo,
1908#endif
1909 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001910 .read = bpf_dummy_read,
1911 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001912};
1913
Daniel Borkmannb2197752015-10-29 14:58:09 +01001914int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001915{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001916 int ret;
1917
1918 ret = security_bpf_prog(prog);
1919 if (ret < 0)
1920 return ret;
1921
Daniel Borkmannaa797812015-10-29 14:58:06 +01001922 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1923 O_RDWR | O_CLOEXEC);
1924}
1925
Daniel Borkmann113214b2016-06-30 17:24:44 +02001926static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001927{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001928 if (!f.file)
1929 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001930 if (f.file->f_op != &bpf_prog_fops) {
1931 fdput(f);
1932 return ERR_PTR(-EINVAL);
1933 }
1934
Daniel Borkmannc2101292015-10-29 14:58:07 +01001935 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001936}
1937
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001938void bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001939{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001940 atomic64_add(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001941}
Brenden Blanco59d36562016-07-19 12:16:46 -07001942EXPORT_SYMBOL_GPL(bpf_prog_add);
1943
Daniel Borkmannc5405942016-11-09 22:02:34 +01001944void bpf_prog_sub(struct bpf_prog *prog, int i)
1945{
1946 /* Only to be used for undoing previous bpf_prog_add() in some
1947 * error path. We still know that another entity in our call
1948 * path holds a reference to the program, thus atomic_sub() can
1949 * be safely used in such cases!
1950 */
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001951 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
Daniel Borkmannc5405942016-11-09 22:02:34 +01001952}
1953EXPORT_SYMBOL_GPL(bpf_prog_sub);
1954
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001955void bpf_prog_inc(struct bpf_prog *prog)
Brenden Blanco59d36562016-07-19 12:16:46 -07001956{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001957 atomic64_inc(&prog->aux->refcnt);
Brenden Blanco59d36562016-07-19 12:16:46 -07001958}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001959EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001960
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001961/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001962struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001963{
1964 int refold;
1965
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001966 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001967
1968 if (!refold)
1969 return ERR_PTR(-ENOENT);
1970
1971 return prog;
1972}
John Fastabenda6f6df62017-08-15 22:32:22 -07001973EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001974
Al Viro040ee692017-12-02 20:20:38 -05001975bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001976 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001977{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001978 /* not an attachment, just a refcount inc, always allow */
1979 if (!attach_type)
1980 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001981
1982 if (prog->type != *attach_type)
1983 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001984 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001985 return false;
1986
1987 return true;
1988}
1989
1990static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001991 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001992{
1993 struct fd f = fdget(ufd);
1994 struct bpf_prog *prog;
1995
Daniel Borkmann113214b2016-06-30 17:24:44 +02001996 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001997 if (IS_ERR(prog))
1998 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001999 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02002000 prog = ERR_PTR(-EINVAL);
2001 goto out;
2002 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002003
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002004 bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02002005out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002006 fdput(f);
2007 return prog;
2008}
Daniel Borkmann113214b2016-06-30 17:24:44 +02002009
2010struct bpf_prog *bpf_prog_get(u32 ufd)
2011{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08002012 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02002013}
2014
Jakub Kicinski248f3462017-11-03 13:56:20 -07002015struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08002016 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07002017{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07002018 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07002019}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07002020EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07002021
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002022/* Initially all BPF programs could be loaded w/o specifying
2023 * expected_attach_type. Later for some of them specifying expected_attach_type
2024 * at load time became required so that program could be validated properly.
2025 * Programs of types that are allowed to be loaded both w/ and w/o (for
2026 * backward compatibility) expected_attach_type, should have the default attach
2027 * type assigned to expected_attach_type for the latter case, so that it can be
2028 * validated later at attach time.
2029 *
2030 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
2031 * prog type requires it but has some attach types that have to be backward
2032 * compatible.
2033 */
2034static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
2035{
2036 switch (attr->prog_type) {
2037 case BPF_PROG_TYPE_CGROUP_SOCK:
2038 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
2039 * exist so checking for non-zero is the way to go here.
2040 */
2041 if (!attr->expected_attach_type)
2042 attr->expected_attach_type =
2043 BPF_CGROUP_INET_SOCK_CREATE;
2044 break;
Kuniyuki Iwashimad5e4dda2021-06-12 21:32:22 +09002045 case BPF_PROG_TYPE_SK_REUSEPORT:
2046 if (!attr->expected_attach_type)
2047 attr->expected_attach_type =
2048 BPF_SK_REUSEPORT_SELECT;
2049 break;
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002050 }
2051}
2052
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002053static int
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002054bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2055 enum bpf_attach_type expected_attach_type,
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002056 struct btf *attach_btf, u32 btf_id,
2057 struct bpf_prog *dst_prog)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002058{
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002059 if (btf_id) {
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07002060 if (btf_id > BTF_MAX_TYPE)
2061 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002062
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002063 if (!attach_btf && !dst_prog)
2064 return -EINVAL;
2065
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002066 switch (prog_type) {
2067 case BPF_PROG_TYPE_TRACING:
KP Singh9e4e01d2020-03-29 01:43:52 +01002068 case BPF_PROG_TYPE_LSM:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002069 case BPF_PROG_TYPE_STRUCT_OPS:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002070 case BPF_PROG_TYPE_EXT:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002071 break;
2072 default:
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07002073 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002074 }
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07002075 }
2076
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002077 if (attach_btf && (!btf_id || dst_prog))
2078 return -EINVAL;
2079
2080 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002081 prog_type != BPF_PROG_TYPE_EXT)
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002082 return -EINVAL;
2083
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07002084 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002085 case BPF_PROG_TYPE_CGROUP_SOCK:
2086 switch (expected_attach_type) {
2087 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07002088 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002089 case BPF_CGROUP_INET4_POST_BIND:
2090 case BPF_CGROUP_INET6_POST_BIND:
2091 return 0;
2092 default:
2093 return -EINVAL;
2094 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002095 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2096 switch (expected_attach_type) {
2097 case BPF_CGROUP_INET4_BIND:
2098 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002099 case BPF_CGROUP_INET4_CONNECT:
2100 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02002101 case BPF_CGROUP_INET4_GETPEERNAME:
2102 case BPF_CGROUP_INET6_GETPEERNAME:
2103 case BPF_CGROUP_INET4_GETSOCKNAME:
2104 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002105 case BPF_CGROUP_UDP4_SENDMSG:
2106 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002107 case BPF_CGROUP_UDP4_RECVMSG:
2108 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002109 return 0;
2110 default:
2111 return -EINVAL;
2112 }
brakmo5cf1e912019-05-28 16:59:36 -07002113 case BPF_PROG_TYPE_CGROUP_SKB:
2114 switch (expected_attach_type) {
2115 case BPF_CGROUP_INET_INGRESS:
2116 case BPF_CGROUP_INET_EGRESS:
2117 return 0;
2118 default:
2119 return -EINVAL;
2120 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002121 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2122 switch (expected_attach_type) {
2123 case BPF_CGROUP_SETSOCKOPT:
2124 case BPF_CGROUP_GETSOCKOPT:
2125 return 0;
2126 default:
2127 return -EINVAL;
2128 }
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02002129 case BPF_PROG_TYPE_SK_LOOKUP:
2130 if (expected_attach_type == BPF_SK_LOOKUP)
2131 return 0;
2132 return -EINVAL;
Kuniyuki Iwashimad5e4dda2021-06-12 21:32:22 +09002133 case BPF_PROG_TYPE_SK_REUSEPORT:
2134 switch (expected_attach_type) {
2135 case BPF_SK_REUSEPORT_SELECT:
2136 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2137 return 0;
2138 default:
2139 return -EINVAL;
2140 }
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07002141 case BPF_PROG_TYPE_SYSCALL:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002142 case BPF_PROG_TYPE_EXT:
2143 if (expected_attach_type)
2144 return -EINVAL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002145 fallthrough;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002146 default:
2147 return 0;
2148 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002149}
2150
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002151static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2152{
2153 switch (prog_type) {
2154 case BPF_PROG_TYPE_SCHED_CLS:
2155 case BPF_PROG_TYPE_SCHED_ACT:
2156 case BPF_PROG_TYPE_XDP:
2157 case BPF_PROG_TYPE_LWT_IN:
2158 case BPF_PROG_TYPE_LWT_OUT:
2159 case BPF_PROG_TYPE_LWT_XMIT:
2160 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2161 case BPF_PROG_TYPE_SK_SKB:
2162 case BPF_PROG_TYPE_SK_MSG:
2163 case BPF_PROG_TYPE_LIRC_MODE2:
2164 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2165 case BPF_PROG_TYPE_CGROUP_DEVICE:
2166 case BPF_PROG_TYPE_CGROUP_SOCK:
2167 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2168 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2169 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2170 case BPF_PROG_TYPE_SOCK_OPS:
2171 case BPF_PROG_TYPE_EXT: /* extends any prog */
2172 return true;
2173 case BPF_PROG_TYPE_CGROUP_SKB:
2174 /* always unpriv */
2175 case BPF_PROG_TYPE_SK_REUSEPORT:
2176 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2177 default:
2178 return false;
2179 }
2180}
2181
2182static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2183{
2184 switch (prog_type) {
2185 case BPF_PROG_TYPE_KPROBE:
2186 case BPF_PROG_TYPE_TRACEPOINT:
2187 case BPF_PROG_TYPE_PERF_EVENT:
2188 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2189 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2190 case BPF_PROG_TYPE_TRACING:
2191 case BPF_PROG_TYPE_LSM:
2192 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2193 case BPF_PROG_TYPE_EXT: /* extends any prog */
2194 return true;
2195 default:
2196 return false;
2197 }
2198}
2199
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002200/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov387544b2021-05-13 17:36:10 -07002201#define BPF_PROG_LOAD_LAST_FIELD fd_array
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002202
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07002203static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002204{
2205 enum bpf_prog_type type = attr->prog_type;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002206 struct bpf_prog *prog, *dst_prog = NULL;
2207 struct btf *attach_btf = NULL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002208 int err;
2209 char license[128];
2210 bool is_gpl;
2211
2212 if (CHECK_ATTR(BPF_PROG_LOAD))
2213 return -EINVAL;
2214
Jiong Wangc240eff2019-05-24 23:25:16 +01002215 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2216 BPF_F_ANY_ALIGNMENT |
Alexei Starovoitov10d274e2019-08-22 22:52:12 -07002217 BPF_F_TEST_STATE_FREQ |
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002218 BPF_F_SLEEPABLE |
Jiong Wangc240eff2019-05-24 23:25:16 +01002219 BPF_F_TEST_RND_HI32))
David S. Millere07b98d2017-05-10 11:38:07 -07002220 return -EINVAL;
2221
David Millere9ee9ef2018-11-30 21:08:14 -08002222 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2223 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002224 !bpf_capable())
David Millere9ee9ef2018-11-30 21:08:14 -08002225 return -EPERM;
2226
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002227 /* copy eBPF program license from user space */
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07002228 if (strncpy_from_bpfptr(license,
2229 make_bpfptr(attr->license, uattr.is_kernel),
2230 sizeof(license) - 1) < 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002231 return -EFAULT;
2232 license[sizeof(license) - 1] = 0;
2233
2234 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2235 is_gpl = license_is_gpl_compatible(license);
2236
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07002237 if (attr->insn_cnt == 0 ||
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002238 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01002239 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07002240 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2241 type != BPF_PROG_TYPE_CGROUP_SKB &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002242 !bpf_capable())
2243 return -EPERM;
2244
Maciej Żenczykowskib338cb92020-06-20 14:26:16 -07002245 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002246 return -EPERM;
2247 if (is_perfmon_prog_type(type) && !perfmon_capable())
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002248 return -EPERM;
2249
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002250 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2251 * or btf, we need to check which one it is
2252 */
2253 if (attr->attach_prog_fd) {
2254 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2255 if (IS_ERR(dst_prog)) {
2256 dst_prog = NULL;
2257 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2258 if (IS_ERR(attach_btf))
2259 return -EINVAL;
2260 if (!btf_is_kernel(attach_btf)) {
Andrii Nakryiko8bdd8e22020-12-07 22:43:26 -08002261 /* attaching through specifying bpf_prog's BTF
2262 * objects directly might be supported eventually
2263 */
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002264 btf_put(attach_btf);
Andrii Nakryiko8bdd8e22020-12-07 22:43:26 -08002265 return -ENOTSUPP;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002266 }
2267 }
2268 } else if (attr->attach_btf_id) {
2269 /* fall back to vmlinux BTF, if BTF type ID is specified */
2270 attach_btf = bpf_get_btf_vmlinux();
2271 if (IS_ERR(attach_btf))
2272 return PTR_ERR(attach_btf);
2273 if (!attach_btf)
2274 return -EINVAL;
2275 btf_get(attach_btf);
2276 }
2277
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002278 bpf_prog_load_fixup_attach_type(attr);
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002279 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002280 attach_btf, attr->attach_btf_id,
2281 dst_prog)) {
2282 if (dst_prog)
2283 bpf_prog_put(dst_prog);
2284 if (attach_btf)
2285 btf_put(attach_btf);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002286 return -EINVAL;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002287 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002288
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002289 /* plain bpf_prog allocation */
2290 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002291 if (!prog) {
2292 if (dst_prog)
2293 bpf_prog_put(dst_prog);
2294 if (attach_btf)
2295 btf_put(attach_btf);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002296 return -ENOMEM;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002297 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002298
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002299 prog->expected_attach_type = attr->expected_attach_type;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002300 prog->aux->attach_btf = attach_btf;
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002301 prog->aux->attach_btf_id = attr->attach_btf_id;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002302 prog->aux->dst_prog = dst_prog;
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002303 prog->aux->offload_requested = !!attr->prog_ifindex;
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002304 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002305
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002306 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002307 if (err)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002308 goto free_prog;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002309
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002310 prog->aux->user = get_current_user();
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002311 prog->len = attr->insn_cnt;
2312
2313 err = -EFAULT;
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07002314 if (copy_from_bpfptr(prog->insns,
2315 make_bpfptr(attr->insns, uattr.is_kernel),
2316 bpf_prog_insn_size(prog)) != 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002317 goto free_prog_sec;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002318
2319 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002320 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002321
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002322 atomic64_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002323 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002324
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002325 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002326 err = bpf_prog_offload_init(prog, attr);
2327 if (err)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002328 goto free_prog_sec;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002329 }
2330
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002331 /* find program type: socket_filter vs tracing_filter */
2332 err = find_prog_type(type, prog);
2333 if (err < 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002334 goto free_prog_sec;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002335
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002336 prog->aux->load_time = ktime_get_boottime_ns();
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -07002337 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2338 sizeof(attr->prog_name));
2339 if (err < 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002340 goto free_prog_sec;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002341
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002342 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08002343 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002344 if (err < 0)
2345 goto free_used_maps;
2346
Daniel Borkmann9facc332018-06-15 02:30:48 +02002347 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002348 if (err < 0)
2349 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002350
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07002351 err = bpf_prog_alloc_id(prog);
2352 if (err)
2353 goto free_used_maps;
2354
Daniel Borkmannc7517982019-08-23 22:14:23 +02002355 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2356 * effectively publicly exposed. However, retrieving via
2357 * bpf_prog_get_fd_by_id() will take another reference,
2358 * therefore it cannot be gone underneath us.
2359 *
2360 * Only for the time /after/ successful bpf_prog_new_fd()
2361 * and before returning to userspace, we might just hold
2362 * one reference and any parallel close on that fd could
2363 * rip everything out. Hence, below notifications must
2364 * happen before bpf_prog_new_fd().
2365 *
2366 * Also, any failure handling from this point onwards must
2367 * be using bpf_prog_put() given the program is exposed.
2368 */
Daniel Borkmann74451e662017-02-16 22:24:50 +01002369 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08002370 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01002371 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
Daniel Borkmannc7517982019-08-23 22:14:23 +02002372
2373 err = bpf_prog_new_fd(prog);
2374 if (err < 0)
2375 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002376 return err;
2377
2378free_used_maps:
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002379 /* In case we have subprogs, we need to wait for a grace
2380 * period before we can tear down JIT memory since symbols
2381 * are already exposed under kallsyms.
2382 */
2383 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2384 return err;
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002385free_prog_sec:
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002386 free_uid(prog->aux->user);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002387 security_bpf_prog_free(prog->aux);
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002388free_prog:
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08002389 if (prog->aux->attach_btf)
2390 btf_put(prog->aux->attach_btf);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002391 bpf_prog_free(prog);
2392 return err;
2393}
2394
Chenbo Feng6e71b042017-10-18 13:00:22 -07002395#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01002396
2397static int bpf_obj_pin(const union bpf_attr *attr)
2398{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002399 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002400 return -EINVAL;
2401
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002402 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01002403}
2404
2405static int bpf_obj_get(const union bpf_attr *attr)
2406{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002407 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2408 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002409 return -EINVAL;
2410
Chenbo Feng6e71b042017-10-18 13:00:22 -07002411 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2412 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01002413}
2414
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002415void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002416 const struct bpf_link_ops *ops, struct bpf_prog *prog)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002417{
2418 atomic64_set(&link->refcnt, 1);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002419 link->type = type;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002420 link->id = 0;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002421 link->ops = ops;
2422 link->prog = prog;
2423}
2424
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002425static void bpf_link_free_id(int id)
2426{
2427 if (!id)
2428 return;
2429
2430 spin_lock_bh(&link_idr_lock);
2431 idr_remove(&link_idr, id);
2432 spin_unlock_bh(&link_idr_lock);
2433}
2434
Andrii Nakryiko98868662020-03-12 17:21:28 -07002435/* Clean up bpf_link and corresponding anon_inode file and FD. After
2436 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002437 * anon_inode's release() call. This helper marksbpf_link as
2438 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2439 * is not decremented, it's the responsibility of a calling code that failed
2440 * to complete bpf_link initialization.
Andrii Nakryiko98868662020-03-12 17:21:28 -07002441 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002442void bpf_link_cleanup(struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002443{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002444 primer->link->prog = NULL;
2445 bpf_link_free_id(primer->id);
2446 fput(primer->file);
2447 put_unused_fd(primer->fd);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002448}
2449
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002450void bpf_link_inc(struct bpf_link *link)
2451{
2452 atomic64_inc(&link->refcnt);
2453}
2454
2455/* bpf_link_free is guaranteed to be called from process context */
2456static void bpf_link_free(struct bpf_link *link)
2457{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002458 bpf_link_free_id(link->id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002459 if (link->prog) {
2460 /* detach BPF program, clean up used resources */
2461 link->ops->release(link);
2462 bpf_prog_put(link->prog);
2463 }
2464 /* free bpf_link and its containing memory */
2465 link->ops->dealloc(link);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002466}
2467
2468static void bpf_link_put_deferred(struct work_struct *work)
2469{
2470 struct bpf_link *link = container_of(work, struct bpf_link, work);
2471
2472 bpf_link_free(link);
2473}
2474
2475/* bpf_link_put can be called from atomic context, but ensures that resources
2476 * are freed from process context
2477 */
2478void bpf_link_put(struct bpf_link *link)
2479{
2480 if (!atomic64_dec_and_test(&link->refcnt))
2481 return;
2482
Alexei Starovoitovf00f2f72020-09-23 19:10:38 -07002483 if (in_atomic()) {
2484 INIT_WORK(&link->work, bpf_link_put_deferred);
2485 schedule_work(&link->work);
2486 } else {
2487 bpf_link_free(link);
2488 }
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002489}
2490
2491static int bpf_link_release(struct inode *inode, struct file *filp)
2492{
2493 struct bpf_link *link = filp->private_data;
2494
2495 bpf_link_put(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002496 return 0;
2497}
2498
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002499#ifdef CONFIG_PROC_FS
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002500#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2501#define BPF_MAP_TYPE(_id, _ops)
2502#define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2503static const char *bpf_link_type_strs[] = {
2504 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2505#include <linux/bpf_types.h>
2506};
2507#undef BPF_PROG_TYPE
2508#undef BPF_MAP_TYPE
2509#undef BPF_LINK_TYPE
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002510
2511static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2512{
2513 const struct bpf_link *link = filp->private_data;
2514 const struct bpf_prog *prog = link->prog;
2515 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002516
2517 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2518 seq_printf(m,
2519 "link_type:\t%s\n"
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002520 "link_id:\t%u\n"
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002521 "prog_tag:\t%s\n"
2522 "prog_id:\t%u\n",
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002523 bpf_link_type_strs[link->type],
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002524 link->id,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002525 prog_tag,
2526 prog->aux->id);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002527 if (link->ops->show_fdinfo)
2528 link->ops->show_fdinfo(link, m);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002529}
2530#endif
2531
Zou Wei6f302bf2020-04-23 10:32:40 +08002532static const struct file_operations bpf_link_fops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002533#ifdef CONFIG_PROC_FS
2534 .show_fdinfo = bpf_link_show_fdinfo,
2535#endif
2536 .release = bpf_link_release,
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002537 .read = bpf_dummy_read,
2538 .write = bpf_dummy_write,
2539};
2540
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002541static int bpf_link_alloc_id(struct bpf_link *link)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002542{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002543 int id;
2544
2545 idr_preload(GFP_KERNEL);
2546 spin_lock_bh(&link_idr_lock);
2547 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2548 spin_unlock_bh(&link_idr_lock);
2549 idr_preload_end();
2550
2551 return id;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002552}
2553
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002554/* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2555 * reserving unused FD and allocating ID from link_idr. This is to be paired
2556 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2557 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2558 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2559 * transient state is passed around in struct bpf_link_primer.
2560 * This is preferred way to create and initialize bpf_link, especially when
2561 * there are complicated and expensive operations inbetween creating bpf_link
2562 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2563 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2564 * expensive (and potentially failing) roll back operations in a rare case
2565 * that file, FD, or ID can't be allocated.
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002566 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002567int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002568{
2569 struct file *file;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002570 int fd, id;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002571
2572 fd = get_unused_fd_flags(O_CLOEXEC);
2573 if (fd < 0)
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002574 return fd;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002575
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002576
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002577 id = bpf_link_alloc_id(link);
2578 if (id < 0) {
2579 put_unused_fd(fd);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002580 return id;
2581 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002582
2583 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2584 if (IS_ERR(file)) {
Andrii Nakryiko138c6762020-05-01 11:56:22 -07002585 bpf_link_free_id(id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002586 put_unused_fd(fd);
Andrii Nakryiko138c6762020-05-01 11:56:22 -07002587 return PTR_ERR(file);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002588 }
2589
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002590 primer->link = link;
2591 primer->file = file;
2592 primer->fd = fd;
2593 primer->id = id;
2594 return 0;
2595}
2596
2597int bpf_link_settle(struct bpf_link_primer *primer)
2598{
2599 /* make bpf_link fetchable by ID */
2600 spin_lock_bh(&link_idr_lock);
2601 primer->link->id = primer->id;
2602 spin_unlock_bh(&link_idr_lock);
2603 /* make bpf_link fetchable by FD */
2604 fd_install(primer->fd, primer->file);
2605 /* pass through installed FD */
2606 return primer->fd;
2607}
2608
2609int bpf_link_new_fd(struct bpf_link *link)
2610{
2611 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002612}
2613
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002614struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2615{
2616 struct fd f = fdget(ufd);
2617 struct bpf_link *link;
2618
2619 if (!f.file)
2620 return ERR_PTR(-EBADF);
2621 if (f.file->f_op != &bpf_link_fops) {
2622 fdput(f);
2623 return ERR_PTR(-EINVAL);
2624 }
2625
2626 link = f.file->private_data;
2627 bpf_link_inc(link);
2628 fdput(f);
2629
2630 return link;
2631}
2632
2633struct bpf_tracing_link {
2634 struct bpf_link link;
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002635 enum bpf_attach_type attach_type;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002636 struct bpf_trampoline *trampoline;
2637 struct bpf_prog *tgt_prog;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002638};
2639
2640static void bpf_tracing_link_release(struct bpf_link *link)
2641{
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002642 struct bpf_tracing_link *tr_link =
2643 container_of(link, struct bpf_tracing_link, link);
2644
2645 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2646 tr_link->trampoline));
2647
2648 bpf_trampoline_put(tr_link->trampoline);
2649
2650 /* tgt_prog is NULL if target is a kernel function */
2651 if (tr_link->tgt_prog)
2652 bpf_prog_put(tr_link->tgt_prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002653}
2654
2655static void bpf_tracing_link_dealloc(struct bpf_link *link)
2656{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002657 struct bpf_tracing_link *tr_link =
2658 container_of(link, struct bpf_tracing_link, link);
2659
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002660 kfree(tr_link);
2661}
2662
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002663static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2664 struct seq_file *seq)
2665{
2666 struct bpf_tracing_link *tr_link =
2667 container_of(link, struct bpf_tracing_link, link);
2668
2669 seq_printf(seq,
2670 "attach_type:\t%d\n",
2671 tr_link->attach_type);
2672}
2673
2674static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2675 struct bpf_link_info *info)
2676{
2677 struct bpf_tracing_link *tr_link =
2678 container_of(link, struct bpf_tracing_link, link);
2679
2680 info->tracing.attach_type = tr_link->attach_type;
Toke Høiland-Jørgensen441e8c62021-04-13 11:16:06 +02002681 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2682 &info->tracing.target_obj_id,
2683 &info->tracing.target_btf_id);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002684
2685 return 0;
2686}
2687
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002688static const struct bpf_link_ops bpf_tracing_link_lops = {
2689 .release = bpf_tracing_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002690 .dealloc = bpf_tracing_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002691 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2692 .fill_link_info = bpf_tracing_link_fill_link_info,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002693};
2694
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002695static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2696 int tgt_prog_fd,
2697 u32 btf_id)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002698{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002699 struct bpf_link_primer link_primer;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002700 struct bpf_prog *tgt_prog = NULL;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002701 struct bpf_trampoline *tr = NULL;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002702 struct bpf_tracing_link *link;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002703 u64 key = 0;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002704 int err;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002705
KP Singh9e4e01d2020-03-29 01:43:52 +01002706 switch (prog->type) {
2707 case BPF_PROG_TYPE_TRACING:
2708 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2709 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2710 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2711 err = -EINVAL;
2712 goto out_put_prog;
2713 }
2714 break;
2715 case BPF_PROG_TYPE_EXT:
2716 if (prog->expected_attach_type != 0) {
2717 err = -EINVAL;
2718 goto out_put_prog;
2719 }
2720 break;
2721 case BPF_PROG_TYPE_LSM:
2722 if (prog->expected_attach_type != BPF_LSM_MAC) {
2723 err = -EINVAL;
2724 goto out_put_prog;
2725 }
2726 break;
2727 default:
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002728 err = -EINVAL;
2729 goto out_put_prog;
2730 }
2731
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002732 if (!!tgt_prog_fd != !!btf_id) {
2733 err = -EINVAL;
2734 goto out_put_prog;
2735 }
2736
2737 if (tgt_prog_fd) {
2738 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2739 if (prog->type != BPF_PROG_TYPE_EXT) {
2740 err = -EINVAL;
2741 goto out_put_prog;
2742 }
2743
2744 tgt_prog = bpf_prog_get(tgt_prog_fd);
2745 if (IS_ERR(tgt_prog)) {
2746 err = PTR_ERR(tgt_prog);
2747 tgt_prog = NULL;
2748 goto out_put_prog;
2749 }
2750
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08002751 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002752 }
2753
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002754 link = kzalloc(sizeof(*link), GFP_USER);
2755 if (!link) {
2756 err = -ENOMEM;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002757 goto out_put_prog;
2758 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002759 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2760 &bpf_tracing_link_lops, prog);
2761 link->attach_type = prog->expected_attach_type;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002762
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002763 mutex_lock(&prog->aux->dst_mutex);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002764
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002765 /* There are a few possible cases here:
2766 *
2767 * - if prog->aux->dst_trampoline is set, the program was just loaded
2768 * and not yet attached to anything, so we can use the values stored
2769 * in prog->aux
2770 *
2771 * - if prog->aux->dst_trampoline is NULL, the program has already been
2772 * attached to a target and its initial target was cleared (below)
2773 *
2774 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2775 * target_btf_id using the link_create API.
2776 *
2777 * - if tgt_prog == NULL when this function was called using the old
Jiri Olsaf3a95072021-04-14 21:51:41 +02002778 * raw_tracepoint_open API, and we need a target from prog->aux
2779 *
2780 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
2781 * was detached and is going for re-attachment.
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002782 */
2783 if (!prog->aux->dst_trampoline && !tgt_prog) {
Jiri Olsaf3a95072021-04-14 21:51:41 +02002784 /*
2785 * Allow re-attach for TRACING and LSM programs. If it's
2786 * currently linked, bpf_trampoline_link_prog will fail.
2787 * EXT programs need to specify tgt_prog_fd, so they
2788 * re-attach in separate code path.
2789 */
2790 if (prog->type != BPF_PROG_TYPE_TRACING &&
2791 prog->type != BPF_PROG_TYPE_LSM) {
2792 err = -EINVAL;
2793 goto out_unlock;
2794 }
2795 btf_id = prog->aux->attach_btf_id;
2796 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002797 }
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002798
2799 if (!prog->aux->dst_trampoline ||
2800 (key && key != prog->aux->dst_trampoline->key)) {
2801 /* If there is no saved target, or the specified target is
2802 * different from the destination specified at load time, we
2803 * need a new trampoline and a check for compatibility
2804 */
2805 struct bpf_attach_target_info tgt_info = {};
2806
2807 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2808 &tgt_info);
2809 if (err)
2810 goto out_unlock;
2811
2812 tr = bpf_trampoline_get(key, &tgt_info);
2813 if (!tr) {
2814 err = -ENOMEM;
2815 goto out_unlock;
2816 }
2817 } else {
2818 /* The caller didn't specify a target, or the target was the
2819 * same as the destination supplied during program load. This
2820 * means we can reuse the trampoline and reference from program
2821 * load time, and there is no need to allocate a new one. This
2822 * can only happen once for any program, as the saved values in
2823 * prog->aux are cleared below.
2824 */
2825 tr = prog->aux->dst_trampoline;
2826 tgt_prog = prog->aux->dst_prog;
2827 }
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002828
2829 err = bpf_link_prime(&link->link, &link_primer);
2830 if (err)
2831 goto out_unlock;
2832
2833 err = bpf_trampoline_link_prog(prog, tr);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002834 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002835 bpf_link_cleanup(&link_primer);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002836 link = NULL;
2837 goto out_unlock;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002838 }
2839
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002840 link->tgt_prog = tgt_prog;
2841 link->trampoline = tr;
2842
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002843 /* Always clear the trampoline and target prog from prog->aux to make
2844 * sure the original attach destination is not kept alive after a
2845 * program is (re-)attached to another target.
2846 */
2847 if (prog->aux->dst_prog &&
2848 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2849 /* got extra prog ref from syscall, or attaching to different prog */
2850 bpf_prog_put(prog->aux->dst_prog);
2851 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2852 /* we allocated a new trampoline, so free the old one */
2853 bpf_trampoline_put(prog->aux->dst_trampoline);
2854
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002855 prog->aux->dst_prog = NULL;
2856 prog->aux->dst_trampoline = NULL;
2857 mutex_unlock(&prog->aux->dst_mutex);
2858
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002859 return bpf_link_settle(&link_primer);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002860out_unlock:
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002861 if (tr && tr != prog->aux->dst_trampoline)
2862 bpf_trampoline_put(tr);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002863 mutex_unlock(&prog->aux->dst_mutex);
2864 kfree(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002865out_put_prog:
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002866 if (tgt_prog_fd && tgt_prog)
2867 bpf_prog_put(tgt_prog);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002868 return err;
2869}
2870
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002871struct bpf_raw_tp_link {
2872 struct bpf_link link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002873 struct bpf_raw_event_map *btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002874};
2875
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002876static void bpf_raw_tp_link_release(struct bpf_link *link)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002877{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002878 struct bpf_raw_tp_link *raw_tp =
2879 container_of(link, struct bpf_raw_tp_link, link);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002880
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002881 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
Matt Mullinsa38d1102018-12-12 16:42:37 -08002882 bpf_put_raw_tracepoint(raw_tp->btp);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002883}
2884
2885static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2886{
2887 struct bpf_raw_tp_link *raw_tp =
2888 container_of(link, struct bpf_raw_tp_link, link);
2889
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002890 kfree(raw_tp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002891}
2892
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002893static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2894 struct seq_file *seq)
2895{
2896 struct bpf_raw_tp_link *raw_tp_link =
2897 container_of(link, struct bpf_raw_tp_link, link);
2898
2899 seq_printf(seq,
2900 "tp_name:\t%s\n",
2901 raw_tp_link->btp->tp->name);
2902}
2903
2904static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2905 struct bpf_link_info *info)
2906{
2907 struct bpf_raw_tp_link *raw_tp_link =
2908 container_of(link, struct bpf_raw_tp_link, link);
2909 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2910 const char *tp_name = raw_tp_link->btp->tp->name;
2911 u32 ulen = info->raw_tracepoint.tp_name_len;
2912 size_t tp_len = strlen(tp_name);
2913
Yonghong Songb4749592020-08-21 12:10:54 -07002914 if (!ulen ^ !ubuf)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002915 return -EINVAL;
2916
2917 info->raw_tracepoint.tp_name_len = tp_len + 1;
2918
2919 if (!ubuf)
2920 return 0;
2921
2922 if (ulen >= tp_len + 1) {
2923 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2924 return -EFAULT;
2925 } else {
2926 char zero = '\0';
2927
2928 if (copy_to_user(ubuf, tp_name, ulen - 1))
2929 return -EFAULT;
2930 if (put_user(zero, ubuf + ulen - 1))
2931 return -EFAULT;
2932 return -ENOSPC;
2933 }
2934
2935 return 0;
2936}
2937
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002938static const struct bpf_link_ops bpf_raw_tp_link_lops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002939 .release = bpf_raw_tp_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002940 .dealloc = bpf_raw_tp_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002941 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2942 .fill_link_info = bpf_raw_tp_link_fill_link_info,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002943};
2944
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07002945#ifdef CONFIG_PERF_EVENTS
2946struct bpf_perf_link {
2947 struct bpf_link link;
2948 struct file *perf_file;
2949};
2950
2951static void bpf_perf_link_release(struct bpf_link *link)
2952{
2953 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
2954 struct perf_event *event = perf_link->perf_file->private_data;
2955
2956 perf_event_free_bpf_prog(event);
2957 fput(perf_link->perf_file);
2958}
2959
2960static void bpf_perf_link_dealloc(struct bpf_link *link)
2961{
2962 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
2963
2964 kfree(perf_link);
2965}
2966
2967static const struct bpf_link_ops bpf_perf_link_lops = {
2968 .release = bpf_perf_link_release,
2969 .dealloc = bpf_perf_link_dealloc,
2970};
2971
2972static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
2973{
2974 struct bpf_link_primer link_primer;
2975 struct bpf_perf_link *link;
2976 struct perf_event *event;
2977 struct file *perf_file;
2978 int err;
2979
2980 if (attr->link_create.flags)
2981 return -EINVAL;
2982
2983 perf_file = perf_event_get(attr->link_create.target_fd);
2984 if (IS_ERR(perf_file))
2985 return PTR_ERR(perf_file);
2986
2987 link = kzalloc(sizeof(*link), GFP_USER);
2988 if (!link) {
2989 err = -ENOMEM;
2990 goto out_put_file;
2991 }
2992 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog);
2993 link->perf_file = perf_file;
2994
2995 err = bpf_link_prime(&link->link, &link_primer);
2996 if (err) {
2997 kfree(link);
2998 goto out_put_file;
2999 }
3000
3001 event = perf_file->private_data;
Andrii Nakryiko82e6b1e2021-08-15 00:05:58 -07003002 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07003003 if (err) {
3004 bpf_link_cleanup(&link_primer);
3005 goto out_put_file;
3006 }
3007 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
3008 bpf_prog_inc(prog);
3009
3010 return bpf_link_settle(&link_primer);
3011
3012out_put_file:
3013 fput(perf_file);
3014 return err;
3015}
3016#endif /* CONFIG_PERF_EVENTS */
3017
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003018#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
3019
3020static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
3021{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003022 struct bpf_link_primer link_primer;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003023 struct bpf_raw_tp_link *link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003024 struct bpf_raw_event_map *btp;
3025 struct bpf_prog *prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07003026 const char *tp_name;
3027 char buf[128];
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003028 int err;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003029
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07003030 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
3031 return -EINVAL;
3032
3033 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
3034 if (IS_ERR(prog))
3035 return PTR_ERR(prog);
3036
KP Singh9e4e01d2020-03-29 01:43:52 +01003037 switch (prog->type) {
3038 case BPF_PROG_TYPE_TRACING:
3039 case BPF_PROG_TYPE_EXT:
3040 case BPF_PROG_TYPE_LSM:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07003041 if (attr->raw_tracepoint.name) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08003042 /* The attach point for this category of programs
3043 * should be specified via btf_id during program load.
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07003044 */
3045 err = -EINVAL;
3046 goto out_put_prog;
3047 }
KP Singh9e4e01d2020-03-29 01:43:52 +01003048 if (prog->type == BPF_PROG_TYPE_TRACING &&
3049 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08003050 tp_name = prog->aux->attach_func_name;
KP Singh9e4e01d2020-03-29 01:43:52 +01003051 break;
3052 }
Jiri Olsa55410752021-01-11 20:16:50 +01003053 err = bpf_tracing_prog_attach(prog, 0, 0);
3054 if (err >= 0)
3055 return err;
3056 goto out_put_prog;
KP Singh9e4e01d2020-03-29 01:43:52 +01003057 case BPF_PROG_TYPE_RAW_TRACEPOINT:
3058 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07003059 if (strncpy_from_user(buf,
3060 u64_to_user_ptr(attr->raw_tracepoint.name),
3061 sizeof(buf) - 1) < 0) {
3062 err = -EFAULT;
3063 goto out_put_prog;
3064 }
3065 buf[sizeof(buf) - 1] = 0;
3066 tp_name = buf;
KP Singh9e4e01d2020-03-29 01:43:52 +01003067 break;
3068 default:
3069 err = -EINVAL;
3070 goto out_put_prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07003071 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003072
Matt Mullinsa38d1102018-12-12 16:42:37 -08003073 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07003074 if (!btp) {
3075 err = -ENOENT;
3076 goto out_put_prog;
3077 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003078
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003079 link = kzalloc(sizeof(*link), GFP_USER);
3080 if (!link) {
Matt Mullinsa38d1102018-12-12 16:42:37 -08003081 err = -ENOMEM;
3082 goto out_put_btp;
3083 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003084 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
3085 &bpf_raw_tp_link_lops, prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003086 link->btp = btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003087
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003088 err = bpf_link_prime(&link->link, &link_primer);
3089 if (err) {
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003090 kfree(link);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003091 goto out_put_btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003092 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003093
3094 err = bpf_probe_register(link->btp, prog);
3095 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003096 bpf_link_cleanup(&link_primer);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003097 goto out_put_btp;
3098 }
3099
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003100 return bpf_link_settle(&link_primer);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003101
Matt Mullinsa38d1102018-12-12 16:42:37 -08003102out_put_btp:
3103 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07003104out_put_prog:
3105 bpf_prog_put(prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003106 return err;
3107}
3108
Anders Roxell33491582018-04-03 14:09:47 +02003109static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
3110 enum bpf_attach_type attach_type)
3111{
3112 switch (prog->type) {
3113 case BPF_PROG_TYPE_CGROUP_SOCK:
3114 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07003115 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02003116 case BPF_PROG_TYPE_SK_LOOKUP:
Anders Roxell33491582018-04-03 14:09:47 +02003117 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
brakmo5cf1e912019-05-28 16:59:36 -07003118 case BPF_PROG_TYPE_CGROUP_SKB:
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003119 if (!capable(CAP_NET_ADMIN))
3120 /* cg-skb progs can be loaded by unpriv user.
3121 * check permissions at attach time.
3122 */
3123 return -EPERM;
brakmo5cf1e912019-05-28 16:59:36 -07003124 return prog->enforce_expected_attach_type &&
3125 prog->expected_attach_type != attach_type ?
3126 -EINVAL : 0;
Anders Roxell33491582018-04-03 14:09:47 +02003127 default:
3128 return 0;
3129 }
3130}
3131
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003132static enum bpf_prog_type
3133attach_type_to_prog_type(enum bpf_attach_type attach_type)
3134{
3135 switch (attach_type) {
3136 case BPF_CGROUP_INET_INGRESS:
3137 case BPF_CGROUP_INET_EGRESS:
3138 return BPF_PROG_TYPE_CGROUP_SKB;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003139 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07003140 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003141 case BPF_CGROUP_INET4_POST_BIND:
3142 case BPF_CGROUP_INET6_POST_BIND:
3143 return BPF_PROG_TYPE_CGROUP_SOCK;
3144 case BPF_CGROUP_INET4_BIND:
3145 case BPF_CGROUP_INET6_BIND:
3146 case BPF_CGROUP_INET4_CONNECT:
3147 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02003148 case BPF_CGROUP_INET4_GETPEERNAME:
3149 case BPF_CGROUP_INET6_GETPEERNAME:
3150 case BPF_CGROUP_INET4_GETSOCKNAME:
3151 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003152 case BPF_CGROUP_UDP4_SENDMSG:
3153 case BPF_CGROUP_UDP6_SENDMSG:
3154 case BPF_CGROUP_UDP4_RECVMSG:
3155 case BPF_CGROUP_UDP6_RECVMSG:
3156 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3157 case BPF_CGROUP_SOCK_OPS:
3158 return BPF_PROG_TYPE_SOCK_OPS;
3159 case BPF_CGROUP_DEVICE:
3160 return BPF_PROG_TYPE_CGROUP_DEVICE;
3161 case BPF_SK_MSG_VERDICT:
3162 return BPF_PROG_TYPE_SK_MSG;
3163 case BPF_SK_SKB_STREAM_PARSER:
3164 case BPF_SK_SKB_STREAM_VERDICT:
Cong Wanga7ba4552021-03-30 19:32:30 -07003165 case BPF_SK_SKB_VERDICT:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003166 return BPF_PROG_TYPE_SK_SKB;
3167 case BPF_LIRC_MODE2:
3168 return BPF_PROG_TYPE_LIRC_MODE2;
3169 case BPF_FLOW_DISSECTOR:
3170 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3171 case BPF_CGROUP_SYSCTL:
3172 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3173 case BPF_CGROUP_GETSOCKOPT:
3174 case BPF_CGROUP_SETSOCKOPT:
3175 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
Yonghong Songde4e05c2020-05-09 10:59:01 -07003176 case BPF_TRACE_ITER:
3177 return BPF_PROG_TYPE_TRACING;
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02003178 case BPF_SK_LOOKUP:
3179 return BPF_PROG_TYPE_SK_LOOKUP;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07003180 case BPF_XDP:
3181 return BPF_PROG_TYPE_XDP;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003182 default:
3183 return BPF_PROG_TYPE_UNSPEC;
3184 }
3185}
3186
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08003187#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
John Fastabend174a79f2017-08-15 22:32:47 -07003188
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003189#define BPF_F_ATTACH_MASK \
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08003190 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003191
Daniel Mackf4324552016-11-23 16:52:27 +01003192static int bpf_prog_attach(const union bpf_attr *attr)
3193{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003194 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01003195 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003196 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01003197
Daniel Mackf4324552016-11-23 16:52:27 +01003198 if (CHECK_ATTR(BPF_PROG_ATTACH))
3199 return -EINVAL;
3200
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003201 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003202 return -EINVAL;
3203
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003204 ptype = attach_type_to_prog_type(attr->attach_type);
3205 if (ptype == BPF_PROG_TYPE_UNSPEC)
Daniel Mackf4324552016-11-23 16:52:27 +01003206 return -EINVAL;
Daniel Mackf4324552016-11-23 16:52:27 +01003207
David Ahernb2cd1252016-12-01 08:48:03 -08003208 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3209 if (IS_ERR(prog))
3210 return PTR_ERR(prog);
3211
Andrey Ignatov5e43f892018-03-30 15:08:00 -07003212 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3213 bpf_prog_put(prog);
3214 return -EINVAL;
3215 }
3216
Sean Youngfdb5c452018-06-19 00:04:24 +01003217 switch (ptype) {
3218 case BPF_PROG_TYPE_SK_SKB:
3219 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02003220 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01003221 break;
3222 case BPF_PROG_TYPE_LIRC_MODE2:
3223 ret = lirc_prog_attach(attr, prog);
3224 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07003225 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +02003226 ret = netns_bpf_prog_attach(attr, prog);
Petar Penkovd58e4682018-09-14 07:46:18 -07003227 break;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003228 case BPF_PROG_TYPE_CGROUP_DEVICE:
3229 case BPF_PROG_TYPE_CGROUP_SKB:
3230 case BPF_PROG_TYPE_CGROUP_SOCK:
3231 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3232 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3233 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3234 case BPF_PROG_TYPE_SOCK_OPS:
Sean Youngfdb5c452018-06-19 00:04:24 +01003235 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003236 break;
3237 default:
3238 ret = -EINVAL;
David Ahernb2cd1252016-12-01 08:48:03 -08003239 }
3240
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003241 if (ret)
3242 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003243 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01003244}
3245
3246#define BPF_PROG_DETACH_LAST_FIELD attach_type
3247
3248static int bpf_prog_detach(const union bpf_attr *attr)
3249{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003250 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01003251
Daniel Mackf4324552016-11-23 16:52:27 +01003252 if (CHECK_ATTR(BPF_PROG_DETACH))
3253 return -EINVAL;
3254
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003255 ptype = attach_type_to_prog_type(attr->attach_type);
3256
3257 switch (ptype) {
3258 case BPF_PROG_TYPE_SK_MSG:
3259 case BPF_PROG_TYPE_SK_SKB:
Lorenz Bauerbb0de312020-06-29 10:56:28 +01003260 return sock_map_prog_detach(attr, ptype);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003261 case BPF_PROG_TYPE_LIRC_MODE2:
Sean Youngf4364dc2018-05-27 12:24:09 +01003262 return lirc_prog_detach(attr);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003263 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Lorenz Bauer4ac2add2020-06-29 10:56:26 +01003264 return netns_bpf_prog_detach(attr, ptype);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003265 case BPF_PROG_TYPE_CGROUP_DEVICE:
3266 case BPF_PROG_TYPE_CGROUP_SKB:
3267 case BPF_PROG_TYPE_CGROUP_SOCK:
3268 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3269 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3270 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3271 case BPF_PROG_TYPE_SOCK_OPS:
3272 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01003273 default:
3274 return -EINVAL;
3275 }
Daniel Mackf4324552016-11-23 16:52:27 +01003276}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07003277
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003278#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3279
3280static int bpf_prog_query(const union bpf_attr *attr,
3281 union bpf_attr __user *uattr)
3282{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003283 if (!capable(CAP_NET_ADMIN))
3284 return -EPERM;
3285 if (CHECK_ATTR(BPF_PROG_QUERY))
3286 return -EINVAL;
3287 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3288 return -EINVAL;
3289
3290 switch (attr->query.attach_type) {
3291 case BPF_CGROUP_INET_INGRESS:
3292 case BPF_CGROUP_INET_EGRESS:
3293 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07003294 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07003295 case BPF_CGROUP_INET4_BIND:
3296 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07003297 case BPF_CGROUP_INET4_POST_BIND:
3298 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07003299 case BPF_CGROUP_INET4_CONNECT:
3300 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02003301 case BPF_CGROUP_INET4_GETPEERNAME:
3302 case BPF_CGROUP_INET6_GETPEERNAME:
3303 case BPF_CGROUP_INET4_GETSOCKNAME:
3304 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07003305 case BPF_CGROUP_UDP4_SENDMSG:
3306 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02003307 case BPF_CGROUP_UDP4_RECVMSG:
3308 case BPF_CGROUP_UDP6_RECVMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003309 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05003310 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08003311 case BPF_CGROUP_SYSCTL:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07003312 case BPF_CGROUP_GETSOCKOPT:
3313 case BPF_CGROUP_SETSOCKOPT:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003314 return cgroup_bpf_prog_query(attr, uattr);
Sean Youngf4364dc2018-05-27 12:24:09 +01003315 case BPF_LIRC_MODE2:
3316 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07003317 case BPF_FLOW_DISSECTOR:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02003318 case BPF_SK_LOOKUP:
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +02003319 return netns_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003320 default:
3321 return -EINVAL;
3322 }
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003323}
Daniel Mackf4324552016-11-23 16:52:27 +01003324
Song Liu1b4d60e2020-09-25 13:54:29 -07003325#define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003326
3327static int bpf_prog_test_run(const union bpf_attr *attr,
3328 union bpf_attr __user *uattr)
3329{
3330 struct bpf_prog *prog;
3331 int ret = -ENOTSUPP;
3332
3333 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3334 return -EINVAL;
3335
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07003336 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3337 (!attr->test.ctx_size_in && attr->test.ctx_in))
3338 return -EINVAL;
3339
3340 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3341 (!attr->test.ctx_size_out && attr->test.ctx_out))
3342 return -EINVAL;
3343
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003344 prog = bpf_prog_get(attr->test.prog_fd);
3345 if (IS_ERR(prog))
3346 return PTR_ERR(prog);
3347
3348 if (prog->aux->ops->test_run)
3349 ret = prog->aux->ops->test_run(prog, attr, uattr);
3350
3351 bpf_prog_put(prog);
3352 return ret;
3353}
3354
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07003355#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3356
3357static int bpf_obj_get_next_id(const union bpf_attr *attr,
3358 union bpf_attr __user *uattr,
3359 struct idr *idr,
3360 spinlock_t *lock)
3361{
3362 u32 next_id = attr->start_id;
3363 int err = 0;
3364
3365 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3366 return -EINVAL;
3367
3368 if (!capable(CAP_SYS_ADMIN))
3369 return -EPERM;
3370
3371 next_id++;
3372 spin_lock_bh(lock);
3373 if (!idr_get_next(idr, &next_id))
3374 err = -ENOENT;
3375 spin_unlock_bh(lock);
3376
3377 if (!err)
3378 err = put_user(next_id, &uattr->next_id);
3379
3380 return err;
3381}
3382
Yonghong Song6086d292020-05-09 10:59:09 -07003383struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3384{
3385 struct bpf_map *map;
3386
3387 spin_lock_bh(&map_idr_lock);
3388again:
3389 map = idr_get_next(&map_idr, id);
3390 if (map) {
3391 map = __bpf_map_inc_not_zero(map, false);
3392 if (IS_ERR(map)) {
3393 (*id)++;
3394 goto again;
3395 }
3396 }
3397 spin_unlock_bh(&map_idr_lock);
3398
3399 return map;
3400}
3401
Alexei Starovoitova228a642020-07-01 18:10:18 -07003402struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3403{
3404 struct bpf_prog *prog;
3405
3406 spin_lock_bh(&prog_idr_lock);
3407again:
3408 prog = idr_get_next(&prog_idr, id);
3409 if (prog) {
3410 prog = bpf_prog_inc_not_zero(prog);
3411 if (IS_ERR(prog)) {
3412 (*id)++;
3413 goto again;
3414 }
3415 }
3416 spin_unlock_bh(&prog_idr_lock);
3417
3418 return prog;
3419}
3420
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003421#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3422
Björn Töpel7e6897f2019-12-13 18:51:09 +01003423struct bpf_prog *bpf_prog_by_id(u32 id)
3424{
3425 struct bpf_prog *prog;
3426
3427 if (!id)
3428 return ERR_PTR(-ENOENT);
3429
3430 spin_lock_bh(&prog_idr_lock);
3431 prog = idr_find(&prog_idr, id);
3432 if (prog)
3433 prog = bpf_prog_inc_not_zero(prog);
3434 else
3435 prog = ERR_PTR(-ENOENT);
3436 spin_unlock_bh(&prog_idr_lock);
3437 return prog;
3438}
3439
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003440static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3441{
3442 struct bpf_prog *prog;
3443 u32 id = attr->prog_id;
3444 int fd;
3445
3446 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3447 return -EINVAL;
3448
3449 if (!capable(CAP_SYS_ADMIN))
3450 return -EPERM;
3451
Björn Töpel7e6897f2019-12-13 18:51:09 +01003452 prog = bpf_prog_by_id(id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003453 if (IS_ERR(prog))
3454 return PTR_ERR(prog);
3455
3456 fd = bpf_prog_new_fd(prog);
3457 if (fd < 0)
3458 bpf_prog_put(prog);
3459
3460 return fd;
3461}
3462
Chenbo Feng6e71b042017-10-18 13:00:22 -07003463#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003464
3465static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3466{
3467 struct bpf_map *map;
3468 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07003469 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003470 int fd;
3471
Chenbo Feng6e71b042017-10-18 13:00:22 -07003472 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3473 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003474 return -EINVAL;
3475
3476 if (!capable(CAP_SYS_ADMIN))
3477 return -EPERM;
3478
Chenbo Feng6e71b042017-10-18 13:00:22 -07003479 f_flags = bpf_get_file_flag(attr->open_flags);
3480 if (f_flags < 0)
3481 return f_flags;
3482
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003483 spin_lock_bh(&map_idr_lock);
3484 map = idr_find(&map_idr, id);
3485 if (map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -07003486 map = __bpf_map_inc_not_zero(map, true);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003487 else
3488 map = ERR_PTR(-ENOENT);
3489 spin_unlock_bh(&map_idr_lock);
3490
3491 if (IS_ERR(map))
3492 return PTR_ERR(map);
3493
Chenbo Feng6e71b042017-10-18 13:00:22 -07003494 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003495 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08003496 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003497
3498 return fd;
3499}
3500
Daniel Borkmann7105e822017-12-20 13:42:57 +01003501static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003502 unsigned long addr, u32 *off,
3503 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01003504{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003505 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003506 int i;
3507
YiFei Zhu984fe942020-09-15 16:45:39 -07003508 mutex_lock(&prog->aux->used_maps_mutex);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003509 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3510 map = prog->aux->used_maps[i];
3511 if (map == (void *)addr) {
3512 *type = BPF_PSEUDO_MAP_FD;
YiFei Zhu984fe942020-09-15 16:45:39 -07003513 goto out;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003514 }
3515 if (!map->ops->map_direct_value_meta)
3516 continue;
3517 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3518 *type = BPF_PSEUDO_MAP_VALUE;
YiFei Zhu984fe942020-09-15 16:45:39 -07003519 goto out;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003520 }
3521 }
YiFei Zhu984fe942020-09-15 16:45:39 -07003522 map = NULL;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003523
YiFei Zhu984fe942020-09-15 16:45:39 -07003524out:
3525 mutex_unlock(&prog->aux->used_maps_mutex);
3526 return map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003527}
3528
Kees Cook63960262020-07-02 15:45:23 -07003529static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3530 const struct cred *f_cred)
Daniel Borkmann7105e822017-12-20 13:42:57 +01003531{
3532 const struct bpf_map *map;
3533 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003534 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003535 u64 imm;
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003536 u8 code;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003537 int i;
3538
3539 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3540 GFP_USER);
3541 if (!insns)
3542 return insns;
3543
3544 for (i = 0; i < prog->len; i++) {
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003545 code = insns[i].code;
3546
3547 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003548 insns[i].code = BPF_JMP | BPF_CALL;
3549 insns[i].imm = BPF_FUNC_tail_call;
3550 /* fall-through */
3551 }
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003552 if (code == (BPF_JMP | BPF_CALL) ||
3553 code == (BPF_JMP | BPF_CALL_ARGS)) {
3554 if (code == (BPF_JMP | BPF_CALL_ARGS))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003555 insns[i].code = BPF_JMP | BPF_CALL;
Kees Cook63960262020-07-02 15:45:23 -07003556 if (!bpf_dump_raw_ok(f_cred))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003557 insns[i].imm = 0;
3558 continue;
3559 }
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003560 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3561 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3562 continue;
3563 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01003564
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003565 if (code != (BPF_LD | BPF_IMM | BPF_DW))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003566 continue;
3567
3568 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003569 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003570 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003571 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003572 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003573 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003574 continue;
3575 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01003576 }
3577
3578 return insns;
3579}
3580
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003581static int set_info_rec_size(struct bpf_prog_info *info)
3582{
3583 /*
3584 * Ensure info.*_rec_size is the same as kernel expected size
3585 *
3586 * or
3587 *
3588 * Only allow zero *_rec_size if both _rec_size and _cnt are
3589 * zero. In this case, the kernel will set the expected
3590 * _rec_size back to the info.
3591 */
3592
Yonghong Song11d8b822018-12-10 14:14:08 -08003593 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003594 info->func_info_rec_size != sizeof(struct bpf_func_info))
3595 return -EINVAL;
3596
Yonghong Song11d8b822018-12-10 14:14:08 -08003597 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003598 info->line_info_rec_size != sizeof(struct bpf_line_info))
3599 return -EINVAL;
3600
Yonghong Song11d8b822018-12-10 14:14:08 -08003601 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003602 info->jited_line_info_rec_size != sizeof(__u64))
3603 return -EINVAL;
3604
3605 info->func_info_rec_size = sizeof(struct bpf_func_info);
3606 info->line_info_rec_size = sizeof(struct bpf_line_info);
3607 info->jited_line_info_rec_size = sizeof(__u64);
3608
3609 return 0;
3610}
3611
Kees Cook63960262020-07-02 15:45:23 -07003612static int bpf_prog_get_info_by_fd(struct file *file,
3613 struct bpf_prog *prog,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003614 const union bpf_attr *attr,
3615 union bpf_attr __user *uattr)
3616{
3617 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003618 struct bpf_prog_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003619 u32 info_len = attr->info.info_len;
Eric Dumazet61a0aba2021-10-26 14:41:33 -07003620 struct bpf_prog_kstats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003621 char __user *uinsns;
3622 u32 ulen;
3623 int err;
3624
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07003625 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003626 if (err)
3627 return err;
3628 info_len = min_t(u32, sizeof(info), info_len);
3629
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003630 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003631 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02003632 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003633
3634 info.type = prog->type;
3635 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003636 info.load_time = prog->aux->load_time;
3637 info.created_by_uid = from_kuid_munged(current_user_ns(),
3638 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02003639 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003640
3641 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003642 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3643
YiFei Zhu984fe942020-09-15 16:45:39 -07003644 mutex_lock(&prog->aux->used_maps_mutex);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003645 ulen = info.nr_map_ids;
3646 info.nr_map_ids = prog->aux->used_map_cnt;
3647 ulen = min_t(u32, info.nr_map_ids, ulen);
3648 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07003649 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003650 u32 i;
3651
3652 for (i = 0; i < ulen; i++)
3653 if (put_user(prog->aux->used_maps[i]->id,
YiFei Zhu984fe942020-09-15 16:45:39 -07003654 &user_map_ids[i])) {
3655 mutex_unlock(&prog->aux->used_maps_mutex);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003656 return -EFAULT;
YiFei Zhu984fe942020-09-15 16:45:39 -07003657 }
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003658 }
YiFei Zhu984fe942020-09-15 16:45:39 -07003659 mutex_unlock(&prog->aux->used_maps_mutex);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003660
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003661 err = set_info_rec_size(&info);
3662 if (err)
3663 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08003664
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003665 bpf_prog_get_stats(prog, &stats);
3666 info.run_time_ns = stats.nsecs;
3667 info.run_cnt = stats.cnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08003668 info.recursion_misses = stats.misses;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003669
Dave Marchevskyaba64c72021-10-20 00:48:17 -07003670 info.verified_insns = prog->aux->verified_insns;
3671
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003672 if (!bpf_capable()) {
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003673 info.jited_prog_len = 0;
3674 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303675 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01003676 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08003677 info.nr_func_info = 0;
3678 info.nr_line_info = 0;
3679 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003680 goto done;
3681 }
3682
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003683 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02003684 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003685 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003686 struct bpf_insn *insns_sanitized;
3687 bool fault;
3688
Kees Cook63960262020-07-02 15:45:23 -07003689 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003690 info.xlated_prog_insns = 0;
3691 goto done;
3692 }
Kees Cook63960262020-07-02 15:45:23 -07003693 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003694 if (!insns_sanitized)
3695 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003696 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3697 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003698 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3699 kfree(insns_sanitized);
3700 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003701 return -EFAULT;
3702 }
3703
Jakub Kicinski675fc272017-12-27 18:39:09 -08003704 if (bpf_prog_is_dev_bound(prog->aux)) {
3705 err = bpf_prog_offload_info_fill(&info, prog);
3706 if (err)
3707 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08003708 goto done;
3709 }
3710
3711 /* NOTE: the following code is supposed to be skipped for offload.
3712 * bpf_prog_offload_info_fill() is the place to fill similar fields
3713 * for offload.
3714 */
3715 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05303716 if (prog->aux->func_cnt) {
3717 u32 i;
3718
3719 info.jited_prog_len = 0;
3720 for (i = 0; i < prog->aux->func_cnt; i++)
3721 info.jited_prog_len += prog->aux->func[i]->jited_len;
3722 } else {
3723 info.jited_prog_len = prog->jited_len;
3724 }
3725
Jiong Wangfcfb1262018-01-16 16:05:19 -08003726 if (info.jited_prog_len && ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003727 if (bpf_dump_raw_ok(file->f_cred)) {
Jiong Wangfcfb1262018-01-16 16:05:19 -08003728 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3729 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05303730
3731 /* for multi-function programs, copy the JITed
3732 * instructions for all the functions
3733 */
3734 if (prog->aux->func_cnt) {
3735 u32 len, free, i;
3736 u8 *img;
3737
3738 free = ulen;
3739 for (i = 0; i < prog->aux->func_cnt; i++) {
3740 len = prog->aux->func[i]->jited_len;
3741 len = min_t(u32, len, free);
3742 img = (u8 *) prog->aux->func[i]->bpf_func;
3743 if (copy_to_user(uinsns, img, len))
3744 return -EFAULT;
3745 uinsns += len;
3746 free -= len;
3747 if (!free)
3748 break;
3749 }
3750 } else {
3751 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3752 return -EFAULT;
3753 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08003754 } else {
3755 info.jited_prog_insns = 0;
3756 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08003757 }
3758
Sandipan Dasdbecd732018-05-24 12:26:48 +05303759 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07003760 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003761 if (ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003762 if (bpf_dump_raw_ok(file->f_cred)) {
Song Liuff1889f2018-11-02 10:16:17 -07003763 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303764 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303765 u32 i;
3766
3767 /* copy the address of the kernel symbol
3768 * corresponding to each function
3769 */
3770 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3771 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07003772 if (prog->aux->func_cnt) {
3773 for (i = 0; i < ulen; i++) {
3774 ksym_addr = (unsigned long)
3775 prog->aux->func[i]->bpf_func;
3776 if (put_user((u64) ksym_addr,
3777 &user_ksyms[i]))
3778 return -EFAULT;
3779 }
3780 } else {
3781 ksym_addr = (unsigned long) prog->bpf_func;
3782 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05303783 return -EFAULT;
3784 }
3785 } else {
3786 info.jited_ksyms = 0;
3787 }
3788 }
3789
Sandipan Das815581c2018-05-24 12:26:52 +05303790 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07003791 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003792 if (ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003793 if (bpf_dump_raw_ok(file->f_cred)) {
Sandipan Das815581c2018-05-24 12:26:52 +05303794 u32 __user *user_lens;
3795 u32 func_len, i;
3796
3797 /* copy the JITed image lengths for each function */
3798 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3799 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07003800 if (prog->aux->func_cnt) {
3801 for (i = 0; i < ulen; i++) {
3802 func_len =
3803 prog->aux->func[i]->jited_len;
3804 if (put_user(func_len, &user_lens[i]))
3805 return -EFAULT;
3806 }
3807 } else {
3808 func_len = prog->jited_len;
3809 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05303810 return -EFAULT;
3811 }
3812 } else {
3813 info.jited_func_lens = 0;
3814 }
3815 }
3816
Martin KaFai Lau73372242018-12-05 17:35:43 -08003817 if (prog->aux->btf)
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08003818 info.btf_id = btf_obj_id(prog->aux->btf);
Yonghong Song838e9692018-11-19 15:29:11 -08003819
Yonghong Song11d8b822018-12-10 14:14:08 -08003820 ulen = info.nr_func_info;
3821 info.nr_func_info = prog->aux->func_info_cnt;
3822 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003823 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08003824
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003825 user_finfo = u64_to_user_ptr(info.func_info);
3826 ulen = min_t(u32, info.nr_func_info, ulen);
3827 if (copy_to_user(user_finfo, prog->aux->func_info,
3828 info.func_info_rec_size * ulen))
3829 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08003830 }
3831
Yonghong Song11d8b822018-12-10 14:14:08 -08003832 ulen = info.nr_line_info;
3833 info.nr_line_info = prog->aux->nr_linfo;
3834 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003835 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003836
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003837 user_linfo = u64_to_user_ptr(info.line_info);
3838 ulen = min_t(u32, info.nr_line_info, ulen);
3839 if (copy_to_user(user_linfo, prog->aux->linfo,
3840 info.line_info_rec_size * ulen))
3841 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003842 }
3843
Yonghong Song11d8b822018-12-10 14:14:08 -08003844 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003845 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08003846 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003847 else
Yonghong Song11d8b822018-12-10 14:14:08 -08003848 info.nr_jited_line_info = 0;
3849 if (info.nr_jited_line_info && ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003850 if (bpf_dump_raw_ok(file->f_cred)) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003851 __u64 __user *user_linfo;
3852 u32 i;
3853
3854 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08003855 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003856 for (i = 0; i < ulen; i++) {
3857 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3858 &user_linfo[i]))
3859 return -EFAULT;
3860 }
3861 } else {
3862 info.jited_line_info = 0;
3863 }
3864 }
3865
Song Liuc872bdb2018-12-12 09:37:46 -08003866 ulen = info.nr_prog_tags;
3867 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3868 if (ulen) {
3869 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3870 u32 i;
3871
3872 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3873 ulen = min_t(u32, info.nr_prog_tags, ulen);
3874 if (prog->aux->func_cnt) {
3875 for (i = 0; i < ulen; i++) {
3876 if (copy_to_user(user_prog_tags[i],
3877 prog->aux->func[i]->tag,
3878 BPF_TAG_SIZE))
3879 return -EFAULT;
3880 }
3881 } else {
3882 if (copy_to_user(user_prog_tags[0],
3883 prog->tag, BPF_TAG_SIZE))
3884 return -EFAULT;
3885 }
3886 }
3887
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003888done:
3889 if (copy_to_user(uinfo, &info, info_len) ||
3890 put_user(info_len, &uattr->info.info_len))
3891 return -EFAULT;
3892
3893 return 0;
3894}
3895
Kees Cook63960262020-07-02 15:45:23 -07003896static int bpf_map_get_info_by_fd(struct file *file,
3897 struct bpf_map *map,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003898 const union bpf_attr *attr,
3899 union bpf_attr __user *uattr)
3900{
3901 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003902 struct bpf_map_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003903 u32 info_len = attr->info.info_len;
3904 int err;
3905
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07003906 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003907 if (err)
3908 return err;
3909 info_len = min_t(u32, sizeof(info), info_len);
3910
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003911 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003912 info.type = map->map_type;
3913 info.id = map->id;
3914 info.key_size = map->key_size;
3915 info.value_size = map->value_size;
3916 info.max_entries = map->max_entries;
3917 info.map_flags = map->map_flags;
Joanne Koong93309862021-10-27 16:45:00 -07003918 info.map_extra = map->map_extra;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07003919 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003920
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003921 if (map->btf) {
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08003922 info.btf_id = btf_obj_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07003923 info.btf_key_type_id = map->btf_key_type_id;
3924 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003925 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08003926 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003927
Jakub Kicinski52775b32018-01-17 19:13:28 -08003928 if (bpf_map_is_dev_bound(map)) {
3929 err = bpf_map_offload_info_fill(&info, map);
3930 if (err)
3931 return err;
3932 }
3933
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003934 if (copy_to_user(uinfo, &info, info_len) ||
3935 put_user(info_len, &uattr->info.info_len))
3936 return -EFAULT;
3937
3938 return 0;
3939}
3940
Kees Cook63960262020-07-02 15:45:23 -07003941static int bpf_btf_get_info_by_fd(struct file *file,
3942 struct btf *btf,
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003943 const union bpf_attr *attr,
3944 union bpf_attr __user *uattr)
3945{
3946 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3947 u32 info_len = attr->info.info_len;
3948 int err;
3949
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07003950 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003951 if (err)
3952 return err;
3953
3954 return btf_get_info_by_fd(btf, attr, uattr);
3955}
3956
Kees Cook63960262020-07-02 15:45:23 -07003957static int bpf_link_get_info_by_fd(struct file *file,
3958 struct bpf_link *link,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003959 const union bpf_attr *attr,
3960 union bpf_attr __user *uattr)
3961{
3962 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3963 struct bpf_link_info info;
3964 u32 info_len = attr->info.info_len;
3965 int err;
3966
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07003967 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003968 if (err)
3969 return err;
3970 info_len = min_t(u32, sizeof(info), info_len);
3971
3972 memset(&info, 0, sizeof(info));
3973 if (copy_from_user(&info, uinfo, info_len))
3974 return -EFAULT;
3975
3976 info.type = link->type;
3977 info.id = link->id;
3978 info.prog_id = link->prog->aux->id;
3979
3980 if (link->ops->fill_link_info) {
3981 err = link->ops->fill_link_info(link, &info);
3982 if (err)
3983 return err;
3984 }
3985
3986 if (copy_to_user(uinfo, &info, info_len) ||
3987 put_user(info_len, &uattr->info.info_len))
3988 return -EFAULT;
3989
3990 return 0;
3991}
3992
3993
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003994#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3995
3996static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3997 union bpf_attr __user *uattr)
3998{
3999 int ufd = attr->info.bpf_fd;
4000 struct fd f;
4001 int err;
4002
4003 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
4004 return -EINVAL;
4005
4006 f = fdget(ufd);
4007 if (!f.file)
4008 return -EBADFD;
4009
4010 if (f.file->f_op == &bpf_prog_fops)
Kees Cook63960262020-07-02 15:45:23 -07004011 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004012 uattr);
4013 else if (f.file->f_op == &bpf_map_fops)
Kees Cook63960262020-07-02 15:45:23 -07004014 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004015 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07004016 else if (f.file->f_op == &btf_fops)
Kees Cook63960262020-07-02 15:45:23 -07004017 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07004018 else if (f.file->f_op == &bpf_link_fops)
Kees Cook63960262020-07-02 15:45:23 -07004019 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07004020 attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004021 else
4022 err = -EINVAL;
4023
4024 fdput(f);
4025 return err;
4026}
4027
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004028#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
4029
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07004030static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004031{
4032 if (CHECK_ATTR(BPF_BTF_LOAD))
4033 return -EINVAL;
4034
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07004035 if (!bpf_capable())
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004036 return -EPERM;
4037
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07004038 return btf_new_fd(attr, uattr);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004039}
4040
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07004041#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
4042
4043static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
4044{
4045 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
4046 return -EINVAL;
4047
4048 if (!capable(CAP_SYS_ADMIN))
4049 return -EPERM;
4050
4051 return btf_get_fd_by_id(attr->btf_id);
4052}
4053
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004054static int bpf_task_fd_query_copy(const union bpf_attr *attr,
4055 union bpf_attr __user *uattr,
4056 u32 prog_id, u32 fd_type,
4057 const char *buf, u64 probe_offset,
4058 u64 probe_addr)
4059{
4060 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
4061 u32 len = buf ? strlen(buf) : 0, input_len;
4062 int err = 0;
4063
4064 if (put_user(len, &uattr->task_fd_query.buf_len))
4065 return -EFAULT;
4066 input_len = attr->task_fd_query.buf_len;
4067 if (input_len && ubuf) {
4068 if (!len) {
4069 /* nothing to copy, just make ubuf NULL terminated */
4070 char zero = '\0';
4071
4072 if (put_user(zero, ubuf))
4073 return -EFAULT;
4074 } else if (input_len >= len + 1) {
4075 /* ubuf can hold the string with NULL terminator */
4076 if (copy_to_user(ubuf, buf, len + 1))
4077 return -EFAULT;
4078 } else {
4079 /* ubuf cannot hold the string with NULL terminator,
4080 * do a partial copy with NULL terminator.
4081 */
4082 char zero = '\0';
4083
4084 err = -ENOSPC;
4085 if (copy_to_user(ubuf, buf, input_len - 1))
4086 return -EFAULT;
4087 if (put_user(zero, ubuf + input_len - 1))
4088 return -EFAULT;
4089 }
4090 }
4091
4092 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
4093 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
4094 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
4095 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
4096 return -EFAULT;
4097
4098 return err;
4099}
4100
4101#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
4102
4103static int bpf_task_fd_query(const union bpf_attr *attr,
4104 union bpf_attr __user *uattr)
4105{
4106 pid_t pid = attr->task_fd_query.pid;
4107 u32 fd = attr->task_fd_query.fd;
4108 const struct perf_event *event;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004109 struct task_struct *task;
4110 struct file *file;
4111 int err;
4112
4113 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
4114 return -EINVAL;
4115
4116 if (!capable(CAP_SYS_ADMIN))
4117 return -EPERM;
4118
4119 if (attr->task_fd_query.flags != 0)
4120 return -EINVAL;
4121
4122 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
4123 if (!task)
4124 return -ENOENT;
4125
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004126 err = 0;
Eric W. Biedermanb48845a2020-11-20 17:14:22 -06004127 file = fget_task(task, fd);
4128 put_task_struct(task);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004129 if (!file)
Eric W. Biedermanb48845a2020-11-20 17:14:22 -06004130 return -EBADF;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004131
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08004132 if (file->f_op == &bpf_link_fops) {
4133 struct bpf_link *link = file->private_data;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004134
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07004135 if (link->ops == &bpf_raw_tp_link_lops) {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08004136 struct bpf_raw_tp_link *raw_tp =
4137 container_of(link, struct bpf_raw_tp_link, link);
4138 struct bpf_raw_event_map *btp = raw_tp->btp;
4139
4140 err = bpf_task_fd_query_copy(attr, uattr,
4141 raw_tp->link.prog->aux->id,
4142 BPF_FD_TYPE_RAW_TRACEPOINT,
4143 btp->tp->name, 0, 0);
4144 goto put_file;
4145 }
4146 goto out_not_supp;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004147 }
4148
4149 event = perf_get_event(file);
4150 if (!IS_ERR(event)) {
4151 u64 probe_offset, probe_addr;
4152 u32 prog_id, fd_type;
4153 const char *buf;
4154
4155 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4156 &buf, &probe_offset,
4157 &probe_addr);
4158 if (!err)
4159 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4160 fd_type, buf,
4161 probe_offset,
4162 probe_addr);
4163 goto put_file;
4164 }
4165
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08004166out_not_supp:
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004167 err = -ENOTSUPP;
4168put_file:
4169 fput(file);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004170 return err;
4171}
4172
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004173#define BPF_MAP_BATCH_LAST_FIELD batch.flags
4174
4175#define BPF_DO_BATCH(fn) \
4176 do { \
4177 if (!fn) { \
4178 err = -ENOTSUPP; \
4179 goto err_put; \
4180 } \
4181 err = fn(map, attr, uattr); \
4182 } while (0)
4183
4184static int bpf_map_do_batch(const union bpf_attr *attr,
4185 union bpf_attr __user *uattr,
4186 int cmd)
4187{
Daniel Borkmann353050b2021-11-09 18:48:08 +00004188 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH ||
4189 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
4190 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004191 struct bpf_map *map;
4192 int err, ufd;
4193 struct fd f;
4194
4195 if (CHECK_ATTR(BPF_MAP_BATCH))
4196 return -EINVAL;
4197
4198 ufd = attr->batch.map_fd;
4199 f = fdget(ufd);
4200 map = __bpf_map_get(f);
4201 if (IS_ERR(map))
4202 return PTR_ERR(map);
Daniel Borkmann353050b2021-11-09 18:48:08 +00004203 if (has_write)
4204 bpf_map_write_active_inc(map);
4205 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004206 err = -EPERM;
4207 goto err_put;
4208 }
Daniel Borkmann353050b2021-11-09 18:48:08 +00004209 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004210 err = -EPERM;
4211 goto err_put;
4212 }
4213
4214 if (cmd == BPF_MAP_LOOKUP_BATCH)
4215 BPF_DO_BATCH(map->ops->map_lookup_batch);
Yonghong Song05799632020-01-15 10:43:04 -08004216 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4217 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004218 else if (cmd == BPF_MAP_UPDATE_BATCH)
4219 BPF_DO_BATCH(map->ops->map_update_batch);
4220 else
4221 BPF_DO_BATCH(map->ops->map_delete_batch);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004222err_put:
Daniel Borkmann353050b2021-11-09 18:48:08 +00004223 if (has_write)
4224 bpf_map_write_active_dec(map);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004225 fdput(f);
4226 return err;
4227}
4228
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004229static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
4230 struct bpf_prog *prog)
Yonghong Songde4e05c2020-05-09 10:59:01 -07004231{
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004232 if (attr->link_create.attach_type != prog->expected_attach_type)
4233 return -EINVAL;
Yonghong Songde4e05c2020-05-09 10:59:01 -07004234
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004235 if (prog->expected_attach_type == BPF_TRACE_ITER)
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004236 return bpf_iter_link_attach(attr, uattr, prog);
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004237 else if (prog->type == BPF_PROG_TYPE_EXT)
4238 return bpf_tracing_prog_attach(prog,
4239 attr->link_create.target_fd,
4240 attr->link_create.target_btf_id);
Yonghong Songde4e05c2020-05-09 10:59:01 -07004241 return -EINVAL;
4242}
4243
Yonghong Song5e7b3022020-08-04 22:50:56 -07004244#define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004245static int link_create(union bpf_attr *attr, bpfptr_t uattr)
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004246{
4247 enum bpf_prog_type ptype;
4248 struct bpf_prog *prog;
4249 int ret;
4250
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004251 if (CHECK_ATTR(BPF_LINK_CREATE))
4252 return -EINVAL;
4253
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004254 prog = bpf_prog_get(attr->link_create.prog_fd);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004255 if (IS_ERR(prog))
4256 return PTR_ERR(prog);
4257
4258 ret = bpf_prog_attach_check_attach_type(prog,
4259 attr->link_create.attach_type);
4260 if (ret)
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004261 goto out;
4262
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07004263 switch (prog->type) {
4264 case BPF_PROG_TYPE_EXT:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004265 ret = tracing_bpf_link_attach(attr, uattr, prog);
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004266 goto out;
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07004267 case BPF_PROG_TYPE_PERF_EVENT:
4268 case BPF_PROG_TYPE_KPROBE:
4269 case BPF_PROG_TYPE_TRACEPOINT:
4270 if (attr->link_create.attach_type != BPF_PERF_EVENT) {
4271 ret = -EINVAL;
4272 goto out;
4273 }
4274 ptype = prog->type;
4275 break;
4276 default:
4277 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4278 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4279 ret = -EINVAL;
4280 goto out;
4281 }
4282 break;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004283 }
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004284
4285 switch (ptype) {
4286 case BPF_PROG_TYPE_CGROUP_SKB:
4287 case BPF_PROG_TYPE_CGROUP_SOCK:
4288 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4289 case BPF_PROG_TYPE_SOCK_OPS:
4290 case BPF_PROG_TYPE_CGROUP_DEVICE:
4291 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4292 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4293 ret = cgroup_bpf_link_attach(attr, prog);
4294 break;
Yonghong Songde4e05c2020-05-09 10:59:01 -07004295 case BPF_PROG_TYPE_TRACING:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004296 ret = tracing_bpf_link_attach(attr, uattr, prog);
Yonghong Songde4e05c2020-05-09 10:59:01 -07004297 break;
Jakub Sitnicki7f045a42020-05-31 10:28:38 +02004298 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02004299 case BPF_PROG_TYPE_SK_LOOKUP:
Jakub Sitnicki7f045a42020-05-31 10:28:38 +02004300 ret = netns_bpf_link_create(attr, prog);
4301 break;
Andrii Nakryiko310ad792020-07-28 12:05:27 -07004302#ifdef CONFIG_NET
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07004303 case BPF_PROG_TYPE_XDP:
4304 ret = bpf_xdp_link_attach(attr, prog);
4305 break;
Andrii Nakryiko310ad792020-07-28 12:05:27 -07004306#endif
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07004307#ifdef CONFIG_PERF_EVENTS
4308 case BPF_PROG_TYPE_PERF_EVENT:
4309 case BPF_PROG_TYPE_TRACEPOINT:
4310 case BPF_PROG_TYPE_KPROBE:
4311 ret = bpf_perf_link_attach(attr, prog);
4312 break;
4313#endif
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004314 default:
4315 ret = -EINVAL;
4316 }
4317
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004318out:
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004319 if (ret < 0)
4320 bpf_prog_put(prog);
4321 return ret;
4322}
4323
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004324#define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4325
4326static int link_update(union bpf_attr *attr)
4327{
4328 struct bpf_prog *old_prog = NULL, *new_prog;
4329 struct bpf_link *link;
4330 u32 flags;
4331 int ret;
4332
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004333 if (CHECK_ATTR(BPF_LINK_UPDATE))
4334 return -EINVAL;
4335
4336 flags = attr->link_update.flags;
4337 if (flags & ~BPF_F_REPLACE)
4338 return -EINVAL;
4339
4340 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4341 if (IS_ERR(link))
4342 return PTR_ERR(link);
4343
4344 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004345 if (IS_ERR(new_prog)) {
4346 ret = PTR_ERR(new_prog);
4347 goto out_put_link;
4348 }
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004349
4350 if (flags & BPF_F_REPLACE) {
4351 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4352 if (IS_ERR(old_prog)) {
4353 ret = PTR_ERR(old_prog);
4354 old_prog = NULL;
4355 goto out_put_progs;
4356 }
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004357 } else if (attr->link_update.old_prog_fd) {
4358 ret = -EINVAL;
4359 goto out_put_progs;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004360 }
4361
Andrii Nakryikof9d04122020-04-28 17:16:05 -07004362 if (link->ops->update_prog)
4363 ret = link->ops->update_prog(link, new_prog, old_prog);
4364 else
Jakub Sitnickife537392020-05-25 14:29:28 +02004365 ret = -EINVAL;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004366
4367out_put_progs:
4368 if (old_prog)
4369 bpf_prog_put(old_prog);
4370 if (ret)
4371 bpf_prog_put(new_prog);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004372out_put_link:
4373 bpf_link_put(link);
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004374 return ret;
4375}
4376
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07004377#define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4378
4379static int link_detach(union bpf_attr *attr)
4380{
4381 struct bpf_link *link;
4382 int ret;
4383
4384 if (CHECK_ATTR(BPF_LINK_DETACH))
4385 return -EINVAL;
4386
4387 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4388 if (IS_ERR(link))
4389 return PTR_ERR(link);
4390
4391 if (link->ops->detach)
4392 ret = link->ops->detach(link);
4393 else
4394 ret = -EOPNOTSUPP;
4395
4396 bpf_link_put(link);
4397 return ret;
4398}
4399
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004400static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004401{
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004402 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4403}
4404
4405struct bpf_link *bpf_link_by_id(u32 id)
4406{
4407 struct bpf_link *link;
4408
4409 if (!id)
4410 return ERR_PTR(-ENOENT);
4411
4412 spin_lock_bh(&link_idr_lock);
4413 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4414 link = idr_find(&link_idr, id);
4415 if (link) {
4416 if (link->id)
4417 link = bpf_link_inc_not_zero(link);
4418 else
4419 link = ERR_PTR(-EAGAIN);
4420 } else {
4421 link = ERR_PTR(-ENOENT);
4422 }
4423 spin_unlock_bh(&link_idr_lock);
4424 return link;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004425}
4426
4427#define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4428
4429static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4430{
4431 struct bpf_link *link;
4432 u32 id = attr->link_id;
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004433 int fd;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004434
4435 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4436 return -EINVAL;
4437
4438 if (!capable(CAP_SYS_ADMIN))
4439 return -EPERM;
4440
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004441 link = bpf_link_by_id(id);
4442 if (IS_ERR(link))
4443 return PTR_ERR(link);
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004444
4445 fd = bpf_link_new_fd(link);
4446 if (fd < 0)
4447 bpf_link_put(link);
4448
4449 return fd;
4450}
4451
Song Liud46edd62020-04-30 00:15:04 -07004452DEFINE_MUTEX(bpf_stats_enabled_mutex);
4453
4454static int bpf_stats_release(struct inode *inode, struct file *file)
4455{
4456 mutex_lock(&bpf_stats_enabled_mutex);
4457 static_key_slow_dec(&bpf_stats_enabled_key.key);
4458 mutex_unlock(&bpf_stats_enabled_mutex);
4459 return 0;
4460}
4461
4462static const struct file_operations bpf_stats_fops = {
4463 .release = bpf_stats_release,
4464};
4465
4466static int bpf_enable_runtime_stats(void)
4467{
4468 int fd;
4469
4470 mutex_lock(&bpf_stats_enabled_mutex);
4471
4472 /* Set a very high limit to avoid overflow */
4473 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4474 mutex_unlock(&bpf_stats_enabled_mutex);
4475 return -EBUSY;
4476 }
4477
4478 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4479 if (fd >= 0)
4480 static_key_slow_inc(&bpf_stats_enabled_key.key);
4481
4482 mutex_unlock(&bpf_stats_enabled_mutex);
4483 return fd;
4484}
4485
4486#define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4487
4488static int bpf_enable_stats(union bpf_attr *attr)
4489{
4490
4491 if (CHECK_ATTR(BPF_ENABLE_STATS))
4492 return -EINVAL;
4493
4494 if (!capable(CAP_SYS_ADMIN))
4495 return -EPERM;
4496
4497 switch (attr->enable_stats.type) {
4498 case BPF_STATS_RUN_TIME:
4499 return bpf_enable_runtime_stats();
4500 default:
4501 break;
4502 }
4503 return -EINVAL;
4504}
4505
Yonghong Songac51d992020-05-09 10:59:05 -07004506#define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4507
4508static int bpf_iter_create(union bpf_attr *attr)
4509{
4510 struct bpf_link *link;
4511 int err;
4512
4513 if (CHECK_ATTR(BPF_ITER_CREATE))
4514 return -EINVAL;
4515
4516 if (attr->iter_create.flags)
4517 return -EINVAL;
4518
4519 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4520 if (IS_ERR(link))
4521 return PTR_ERR(link);
4522
4523 err = bpf_iter_new_fd(link);
4524 bpf_link_put(link);
4525
4526 return err;
4527}
4528
YiFei Zhuef153142020-09-15 16:45:40 -07004529#define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4530
4531static int bpf_prog_bind_map(union bpf_attr *attr)
4532{
4533 struct bpf_prog *prog;
4534 struct bpf_map *map;
4535 struct bpf_map **used_maps_old, **used_maps_new;
4536 int i, ret = 0;
4537
4538 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4539 return -EINVAL;
4540
4541 if (attr->prog_bind_map.flags)
4542 return -EINVAL;
4543
4544 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4545 if (IS_ERR(prog))
4546 return PTR_ERR(prog);
4547
4548 map = bpf_map_get(attr->prog_bind_map.map_fd);
4549 if (IS_ERR(map)) {
4550 ret = PTR_ERR(map);
4551 goto out_prog_put;
4552 }
4553
4554 mutex_lock(&prog->aux->used_maps_mutex);
4555
4556 used_maps_old = prog->aux->used_maps;
4557
4558 for (i = 0; i < prog->aux->used_map_cnt; i++)
Stanislav Fomichev1028ae42020-10-02 17:25:44 -07004559 if (used_maps_old[i] == map) {
4560 bpf_map_put(map);
YiFei Zhuef153142020-09-15 16:45:40 -07004561 goto out_unlock;
Stanislav Fomichev1028ae42020-10-02 17:25:44 -07004562 }
YiFei Zhuef153142020-09-15 16:45:40 -07004563
4564 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4565 sizeof(used_maps_new[0]),
4566 GFP_KERNEL);
4567 if (!used_maps_new) {
4568 ret = -ENOMEM;
4569 goto out_unlock;
4570 }
4571
4572 memcpy(used_maps_new, used_maps_old,
4573 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4574 used_maps_new[prog->aux->used_map_cnt] = map;
4575
4576 prog->aux->used_map_cnt++;
4577 prog->aux->used_maps = used_maps_new;
4578
4579 kfree(used_maps_old);
4580
4581out_unlock:
4582 mutex_unlock(&prog->aux->used_maps_mutex);
4583
4584 if (ret)
4585 bpf_map_put(map);
4586out_prog_put:
4587 bpf_prog_put(prog);
4588 return ret;
4589}
4590
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004591static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004592{
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004593 union bpf_attr attr;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004594 int err;
4595
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07004596 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004597 return -EPERM;
4598
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07004599 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004600 if (err)
4601 return err;
4602 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004603
4604 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004605 memset(&attr, 0, sizeof(attr));
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004606 if (copy_from_bpfptr(&attr, uattr, size) != 0)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004607 return -EFAULT;
4608
Chenbo Fengafdb09c2017-10-18 13:00:24 -07004609 err = security_bpf(cmd, &attr, size);
4610 if (err < 0)
4611 return err;
4612
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004613 switch (cmd) {
4614 case BPF_MAP_CREATE:
4615 err = map_create(&attr);
4616 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07004617 case BPF_MAP_LOOKUP_ELEM:
4618 err = map_lookup_elem(&attr);
4619 break;
4620 case BPF_MAP_UPDATE_ELEM:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004621 err = map_update_elem(&attr, uattr);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07004622 break;
4623 case BPF_MAP_DELETE_ELEM:
4624 err = map_delete_elem(&attr);
4625 break;
4626 case BPF_MAP_GET_NEXT_KEY:
4627 err = map_get_next_key(&attr);
4628 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02004629 case BPF_MAP_FREEZE:
4630 err = map_freeze(&attr);
4631 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004632 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08004633 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004634 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01004635 case BPF_OBJ_PIN:
4636 err = bpf_obj_pin(&attr);
4637 break;
4638 case BPF_OBJ_GET:
4639 err = bpf_obj_get(&attr);
4640 break;
Daniel Mackf4324552016-11-23 16:52:27 +01004641 case BPF_PROG_ATTACH:
4642 err = bpf_prog_attach(&attr);
4643 break;
4644 case BPF_PROG_DETACH:
4645 err = bpf_prog_detach(&attr);
4646 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07004647 case BPF_PROG_QUERY:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004648 err = bpf_prog_query(&attr, uattr.user);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07004649 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07004650 case BPF_PROG_TEST_RUN:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004651 err = bpf_prog_test_run(&attr, uattr.user);
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07004652 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004653 case BPF_PROG_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004654 err = bpf_obj_get_next_id(&attr, uattr.user,
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004655 &prog_idr, &prog_idr_lock);
4656 break;
4657 case BPF_MAP_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004658 err = bpf_obj_get_next_id(&attr, uattr.user,
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004659 &map_idr, &map_idr_lock);
4660 break;
Quentin Monnet1b9ed842019-08-20 10:31:50 +01004661 case BPF_BTF_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004662 err = bpf_obj_get_next_id(&attr, uattr.user,
Quentin Monnet1b9ed842019-08-20 10:31:50 +01004663 &btf_idr, &btf_idr_lock);
4664 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07004665 case BPF_PROG_GET_FD_BY_ID:
4666 err = bpf_prog_get_fd_by_id(&attr);
4667 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07004668 case BPF_MAP_GET_FD_BY_ID:
4669 err = bpf_map_get_fd_by_id(&attr);
4670 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004671 case BPF_OBJ_GET_INFO_BY_FD:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004672 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004673 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07004674 case BPF_RAW_TRACEPOINT_OPEN:
4675 err = bpf_raw_tracepoint_open(&attr);
4676 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004677 case BPF_BTF_LOAD:
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07004678 err = bpf_btf_load(&attr, uattr);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004679 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07004680 case BPF_BTF_GET_FD_BY_ID:
4681 err = bpf_btf_get_fd_by_id(&attr);
4682 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004683 case BPF_TASK_FD_QUERY:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004684 err = bpf_task_fd_query(&attr, uattr.user);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004685 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02004686 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4687 err = map_lookup_and_delete_elem(&attr);
4688 break;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004689 case BPF_MAP_LOOKUP_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004690 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004691 break;
Yonghong Song05799632020-01-15 10:43:04 -08004692 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004693 err = bpf_map_do_batch(&attr, uattr.user,
Yonghong Song05799632020-01-15 10:43:04 -08004694 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4695 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004696 case BPF_MAP_UPDATE_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004697 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004698 break;
4699 case BPF_MAP_DELETE_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004700 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004701 break;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004702 case BPF_LINK_CREATE:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004703 err = link_create(&attr, uattr);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004704 break;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004705 case BPF_LINK_UPDATE:
4706 err = link_update(&attr);
4707 break;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004708 case BPF_LINK_GET_FD_BY_ID:
4709 err = bpf_link_get_fd_by_id(&attr);
4710 break;
4711 case BPF_LINK_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004712 err = bpf_obj_get_next_id(&attr, uattr.user,
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004713 &link_idr, &link_idr_lock);
4714 break;
Song Liud46edd62020-04-30 00:15:04 -07004715 case BPF_ENABLE_STATS:
4716 err = bpf_enable_stats(&attr);
4717 break;
Yonghong Songac51d992020-05-09 10:59:05 -07004718 case BPF_ITER_CREATE:
4719 err = bpf_iter_create(&attr);
4720 break;
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07004721 case BPF_LINK_DETACH:
4722 err = link_detach(&attr);
4723 break;
YiFei Zhuef153142020-09-15 16:45:40 -07004724 case BPF_PROG_BIND_MAP:
4725 err = bpf_prog_bind_map(&attr);
4726 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004727 default:
4728 err = -EINVAL;
4729 break;
4730 }
4731
4732 return err;
4733}
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004734
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004735SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4736{
4737 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
4738}
4739
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004740static bool syscall_prog_is_valid_access(int off, int size,
4741 enum bpf_access_type type,
4742 const struct bpf_prog *prog,
4743 struct bpf_insn_access_aux *info)
4744{
4745 if (off < 0 || off >= U16_MAX)
4746 return false;
4747 if (off % size != 0)
4748 return false;
4749 return true;
4750}
4751
4752BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size)
4753{
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004754 switch (cmd) {
4755 case BPF_MAP_CREATE:
4756 case BPF_MAP_UPDATE_ELEM:
4757 case BPF_MAP_FREEZE:
4758 case BPF_PROG_LOAD:
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07004759 case BPF_BTF_LOAD:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004760 break;
4761 /* case BPF_PROG_TEST_RUN:
4762 * is not part of this list to prevent recursive test_run
4763 */
4764 default:
4765 return -EINVAL;
4766 }
4767 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004768}
4769
Pu Lehui3a2daa72021-05-19 14:41:16 +08004770static const struct bpf_func_proto bpf_sys_bpf_proto = {
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004771 .func = bpf_sys_bpf,
4772 .gpl_only = false,
4773 .ret_type = RET_INTEGER,
4774 .arg1_type = ARG_ANYTHING,
4775 .arg2_type = ARG_PTR_TO_MEM,
4776 .arg3_type = ARG_CONST_SIZE,
4777};
4778
4779const struct bpf_func_proto * __weak
4780tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4781{
4782 return bpf_base_func_proto(func_id);
4783}
4784
Alexei Starovoitov3abea082021-05-13 17:36:12 -07004785BPF_CALL_1(bpf_sys_close, u32, fd)
4786{
4787 /* When bpf program calls this helper there should not be
4788 * an fdget() without matching completed fdput().
4789 * This helper is allowed in the following callchain only:
4790 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
4791 */
4792 return close_fd(fd);
4793}
4794
Pu Lehui3a2daa72021-05-19 14:41:16 +08004795static const struct bpf_func_proto bpf_sys_close_proto = {
Alexei Starovoitov3abea082021-05-13 17:36:12 -07004796 .func = bpf_sys_close,
4797 .gpl_only = false,
4798 .ret_type = RET_INTEGER,
4799 .arg1_type = ARG_ANYTHING,
4800};
4801
Kumar Kartikeya Dwivedid6aef082021-10-28 12:04:54 +05304802BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res)
4803{
4804 if (flags)
4805 return -EINVAL;
4806
4807 if (name_sz <= 1 || name[name_sz - 1])
4808 return -EINVAL;
4809
4810 if (!bpf_dump_raw_ok(current_cred()))
4811 return -EPERM;
4812
4813 *res = kallsyms_lookup_name(name);
4814 return *res ? 0 : -ENOENT;
4815}
4816
4817const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = {
4818 .func = bpf_kallsyms_lookup_name,
4819 .gpl_only = false,
4820 .ret_type = RET_INTEGER,
4821 .arg1_type = ARG_PTR_TO_MEM,
4822 .arg2_type = ARG_CONST_SIZE,
4823 .arg3_type = ARG_ANYTHING,
4824 .arg4_type = ARG_PTR_TO_LONG,
4825};
4826
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004827static const struct bpf_func_proto *
4828syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4829{
4830 switch (func_id) {
4831 case BPF_FUNC_sys_bpf:
4832 return &bpf_sys_bpf_proto;
Alexei Starovoitov3d784172021-05-13 17:36:11 -07004833 case BPF_FUNC_btf_find_by_name_kind:
4834 return &bpf_btf_find_by_name_kind_proto;
Alexei Starovoitov3abea082021-05-13 17:36:12 -07004835 case BPF_FUNC_sys_close:
4836 return &bpf_sys_close_proto;
Kumar Kartikeya Dwivedid6aef082021-10-28 12:04:54 +05304837 case BPF_FUNC_kallsyms_lookup_name:
4838 return &bpf_kallsyms_lookup_name_proto;
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004839 default:
4840 return tracing_prog_func_proto(func_id, prog);
4841 }
4842}
4843
4844const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
4845 .get_func_proto = syscall_prog_func_proto,
4846 .is_valid_access = syscall_prog_is_valid_access,
4847};
4848
4849const struct bpf_prog_ops bpf_syscall_prog_ops = {
4850 .test_run = bpf_prog_test_run_syscall,
4851};