blob: e343f158e55642f55014e503d339d964b48380f0 [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
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800135static u32 bpf_map_value_size(const struct bpf_map *map)
Brian Vazquez15c14a32020-01-15 10:43:00 -0800136{
137 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
138 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
139 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
140 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
141 return round_up(map->value_size, 8) * num_possible_cpus();
142 else if (IS_FD_MAP(map))
143 return sizeof(u32);
144 else
145 return map->value_size;
146}
147
148static void maybe_wait_bpf_programs(struct bpf_map *map)
149{
150 /* Wait for any running BPF programs to complete so that
151 * userspace, when we return to it, knows that all programs
152 * that could be running use the new map value.
153 */
154 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
155 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
156 synchronize_rcu();
157}
158
159static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
160 void *value, __u64 flags)
161{
162 int err;
163
164 /* Need to create a kthread, thus must support schedule */
165 if (bpf_map_is_dev_bound(map)) {
166 return bpf_map_offload_update_elem(map, key, value, flags);
167 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
Brian Vazquez15c14a32020-01-15 10:43:00 -0800168 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
169 return map->ops->map_update_elem(map, key, value, flags);
Lorenz Bauer13b79d32020-08-21 11:29:45 +0100170 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
171 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
172 return sock_map_update_elem_sys(map, key, value, flags);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800173 } else if (IS_FD_PROG_ARRAY(map)) {
174 return bpf_fd_array_map_update_elem(map, f.file, key, value,
175 flags);
176 }
177
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100178 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800179 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
180 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
181 err = bpf_percpu_hash_update(map, key, value, flags);
182 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
183 err = bpf_percpu_array_update(map, key, value, flags);
184 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
185 err = bpf_percpu_cgroup_storage_update(map, key, value,
186 flags);
187 } else if (IS_FD_ARRAY(map)) {
188 rcu_read_lock();
189 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
190 flags);
191 rcu_read_unlock();
192 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
193 rcu_read_lock();
194 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
195 flags);
196 rcu_read_unlock();
197 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
198 /* rcu_read_lock() is not needed */
199 err = bpf_fd_reuseport_array_update_elem(map, key, value,
200 flags);
201 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
202 map->map_type == BPF_MAP_TYPE_STACK) {
203 err = map->ops->map_push_elem(map, value, flags);
204 } else {
205 rcu_read_lock();
206 err = map->ops->map_update_elem(map, key, value, flags);
207 rcu_read_unlock();
208 }
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100209 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800210 maybe_wait_bpf_programs(map);
211
212 return err;
213}
214
215static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
216 __u64 flags)
217{
218 void *ptr;
219 int err;
220
Brian Vazquezcb4d03a2020-01-15 10:43:01 -0800221 if (bpf_map_is_dev_bound(map))
222 return bpf_map_offload_lookup_elem(map, key, value);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800223
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100224 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800225 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
226 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
227 err = bpf_percpu_hash_copy(map, key, value);
228 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
229 err = bpf_percpu_array_copy(map, key, value);
230 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
231 err = bpf_percpu_cgroup_storage_copy(map, key, value);
232 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
233 err = bpf_stackmap_copy(map, key, value);
234 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
235 err = bpf_fd_array_map_lookup_elem(map, key, value);
236 } else if (IS_FD_HASH(map)) {
237 err = bpf_fd_htab_map_lookup_elem(map, key, value);
238 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
239 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
240 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
241 map->map_type == BPF_MAP_TYPE_STACK) {
242 err = map->ops->map_peek_elem(map, value);
243 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
244 /* struct_ops map requires directly updating "value" */
245 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
246 } else {
247 rcu_read_lock();
248 if (map->ops->map_lookup_elem_sys_only)
249 ptr = map->ops->map_lookup_elem_sys_only(map, key);
250 else
251 ptr = map->ops->map_lookup_elem(map, key);
252 if (IS_ERR(ptr)) {
253 err = PTR_ERR(ptr);
254 } else if (!ptr) {
255 err = -ENOENT;
256 } else {
257 err = 0;
258 if (flags & BPF_F_LOCK)
259 /* lock 'ptr' and copy everything but lock */
260 copy_map_value_locked(map, value, ptr, true);
261 else
262 copy_map_value(map, value, ptr);
263 /* mask lock, since value wasn't zero inited */
264 check_and_init_map_lock(map, value);
265 }
266 rcu_read_unlock();
267 }
268
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100269 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800270 maybe_wait_bpf_programs(map);
271
272 return err;
273}
274
Roman Gushchind5299b62020-12-01 13:58:33 -0800275/* Please, do not use this function outside from the map creation path
276 * (e.g. in map update path) without taking care of setting the active
277 * memory cgroup (see at bpf_map_kmalloc_node() for example).
278 */
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100279static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100280{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100281 /* We really just want to fail instead of triggering OOM killer
282 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
283 * which is used for lower order allocation requests.
284 *
285 * It has been observed that higher order allocation requests done by
286 * vmalloc with __GFP_NORETRY being set might fail due to not trying
287 * to reclaim memory from the page cache, thus we set
288 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100289 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100290
Roman Gushchind5299b62020-12-01 13:58:33 -0800291 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
Christoph Hellwig041de932020-06-01 21:52:02 -0700292 unsigned int flags = 0;
293 unsigned long align = 1;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100294 void *area;
295
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100296 if (size >= SIZE_MAX)
297 return NULL;
298
Andrii Nakryikofc970222019-11-17 09:28:04 -0800299 /* kmalloc()'ed memory can't be mmap()'ed */
Christoph Hellwig041de932020-06-01 21:52:02 -0700300 if (mmapable) {
301 BUG_ON(!PAGE_ALIGNED(size));
302 align = SHMLBA;
303 flags = VM_USERMAP;
304 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
305 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100306 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100307 if (area != NULL)
308 return area;
309 }
Christoph Hellwig041de932020-06-01 21:52:02 -0700310
311 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
312 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
313 flags, numa_node, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100314}
315
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100316void *bpf_map_area_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800317{
318 return __bpf_map_area_alloc(size, numa_node, false);
319}
320
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100321void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800322{
323 return __bpf_map_area_alloc(size, numa_node, true);
324}
325
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100326void bpf_map_area_free(void *area)
327{
328 kvfree(area);
329}
330
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200331static u32 bpf_map_flags_retain_permanent(u32 flags)
332{
333 /* Some map creation flags are not tied to the map object but
334 * rather to the map fd instead, so they have no meaning upon
335 * map object inspection since multiple file descriptors with
336 * different (access) properties can exist here. Thus, given
337 * this has zero meaning for the map itself, lets clear these
338 * from here.
339 */
340 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
341}
342
Jakub Kicinskibd475642018-01-11 20:29:06 -0800343void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
344{
345 map->map_type = attr->map_type;
346 map->key_size = attr->key_size;
347 map->value_size = attr->value_size;
348 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200349 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800350 map->numa_node = bpf_map_attr_numa_node(attr);
351}
352
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700353static int bpf_map_alloc_id(struct bpf_map *map)
354{
355 int id;
356
Shaohua Lib76354c2018-03-27 11:53:21 -0700357 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700358 spin_lock_bh(&map_idr_lock);
359 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
360 if (id > 0)
361 map->id = id;
362 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700363 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700364
365 if (WARN_ON_ONCE(!id))
366 return -ENOSPC;
367
368 return id > 0 ? 0 : id;
369}
370
Jakub Kicinskia3884572018-01-11 20:29:09 -0800371void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700372{
Eric Dumazet930651a2017-09-19 09:15:59 -0700373 unsigned long flags;
374
Jakub Kicinskia3884572018-01-11 20:29:09 -0800375 /* Offloaded maps are removed from the IDR store when their device
376 * disappears - even if someone holds an fd to them they are unusable,
377 * the memory is gone, all ops will fail; they are simply waiting for
378 * refcnt to drop to be freed.
379 */
380 if (!map->id)
381 return;
382
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700383 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700384 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700385 else
386 __acquire(&map_idr_lock);
387
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700388 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800389 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700390
391 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700392 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700393 else
394 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700395}
396
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800397#ifdef CONFIG_MEMCG_KMEM
398static void bpf_map_save_memcg(struct bpf_map *map)
399{
400 map->memcg = get_mem_cgroup_from_mm(current->mm);
401}
402
403static void bpf_map_release_memcg(struct bpf_map *map)
404{
405 mem_cgroup_put(map->memcg);
406}
407
408void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
409 int node)
410{
411 struct mem_cgroup *old_memcg;
412 void *ptr;
413
414 old_memcg = set_active_memcg(map->memcg);
415 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
416 set_active_memcg(old_memcg);
417
418 return ptr;
419}
420
421void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
422{
423 struct mem_cgroup *old_memcg;
424 void *ptr;
425
426 old_memcg = set_active_memcg(map->memcg);
427 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
428 set_active_memcg(old_memcg);
429
430 return ptr;
431}
432
433void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
434 size_t align, gfp_t flags)
435{
436 struct mem_cgroup *old_memcg;
437 void __percpu *ptr;
438
439 old_memcg = set_active_memcg(map->memcg);
440 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
441 set_active_memcg(old_memcg);
442
443 return ptr;
444}
445
446#else
447static void bpf_map_save_memcg(struct bpf_map *map)
448{
449}
450
451static void bpf_map_release_memcg(struct bpf_map *map)
452{
453}
454#endif
455
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700456/* called from workqueue */
457static void bpf_map_free_deferred(struct work_struct *work)
458{
459 struct bpf_map *map = container_of(work, struct bpf_map, work);
460
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700461 security_bpf_map_free(map);
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800462 bpf_map_release_memcg(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700463 /* implementation dependent freeing */
464 map->ops->map_free(map);
465}
466
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100467static void bpf_map_put_uref(struct bpf_map *map)
468{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800469 if (atomic64_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700470 if (map->ops->map_release_uref)
471 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100472 }
473}
474
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700475/* decrement map refcnt and schedule it for freeing via workqueue
476 * (unrelying map implementation ops->map_free() might sleep)
477 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700478static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700479{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800480 if (atomic64_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700481 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700482 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700483 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700484 INIT_WORK(&map->work, bpf_map_free_deferred);
485 schedule_work(&map->work);
486 }
487}
488
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700489void bpf_map_put(struct bpf_map *map)
490{
491 __bpf_map_put(map, true);
492}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700493EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700494
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100495void bpf_map_put_with_uref(struct bpf_map *map)
496{
497 bpf_map_put_uref(map);
498 bpf_map_put(map);
499}
500
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700501static int bpf_map_release(struct inode *inode, struct file *filp)
502{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200503 struct bpf_map *map = filp->private_data;
504
505 if (map->ops->map_release)
506 map->ops->map_release(map, filp);
507
508 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700509 return 0;
510}
511
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200512static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
513{
514 fmode_t mode = f.file->f_mode;
515
516 /* Our file permissions may have been overridden by global
517 * map permissions facing syscall side.
518 */
519 if (READ_ONCE(map->frozen))
520 mode &= ~FMODE_CAN_WRITE;
521 return mode;
522}
523
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100524#ifdef CONFIG_PROC_FS
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800525/* Provides an approximation of the map's memory footprint.
526 * Used only to provide a backward compatibility and display
527 * a reasonable "memlock" info.
528 */
529static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
530{
531 unsigned long size;
532
533 size = round_up(map->key_size + bpf_map_value_size(map), 8);
534
535 return round_up(map->max_entries * size, PAGE_SIZE);
536}
537
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100538static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
539{
540 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100541 const struct bpf_array *array;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100542 u32 type = 0, jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100543
544 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
545 array = container_of(map, struct bpf_array, map);
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100546 type = array->aux->type;
547 jited = array->aux->jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100548 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100549
550 seq_printf(m,
551 "map_type:\t%u\n"
552 "key_size:\t%u\n"
553 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100554 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100555 "map_flags:\t%#x\n"
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800556 "memlock:\t%lu\n"
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200557 "map_id:\t%u\n"
558 "frozen:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100559 map->map_type,
560 map->key_size,
561 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100562 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100563 map->map_flags,
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800564 bpf_map_memory_footprint(map),
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200565 map->id,
566 READ_ONCE(map->frozen));
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100567 if (type) {
568 seq_printf(m, "owner_prog_type:\t%u\n", type);
569 seq_printf(m, "owner_jited:\t%u\n", jited);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200570 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100571}
572#endif
573
Chenbo Feng6e71b042017-10-18 13:00:22 -0700574static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
575 loff_t *ppos)
576{
577 /* We need this handler such that alloc_file() enables
578 * f_mode with FMODE_CAN_READ.
579 */
580 return -EINVAL;
581}
582
583static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
584 size_t siz, loff_t *ppos)
585{
586 /* We need this handler such that alloc_file() enables
587 * f_mode with FMODE_CAN_WRITE.
588 */
589 return -EINVAL;
590}
591
Andrii Nakryikofc970222019-11-17 09:28:04 -0800592/* called for any extra memory-mapped regions (except initial) */
593static void bpf_map_mmap_open(struct vm_area_struct *vma)
594{
595 struct bpf_map *map = vma->vm_file->private_data;
596
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700597 if (vma->vm_flags & VM_MAYWRITE) {
Andrii Nakryikofc970222019-11-17 09:28:04 -0800598 mutex_lock(&map->freeze_mutex);
599 map->writecnt++;
600 mutex_unlock(&map->freeze_mutex);
601 }
602}
603
604/* called for all unmapped memory region (including initial) */
605static void bpf_map_mmap_close(struct vm_area_struct *vma)
606{
607 struct bpf_map *map = vma->vm_file->private_data;
608
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700609 if (vma->vm_flags & VM_MAYWRITE) {
Andrii Nakryikofc970222019-11-17 09:28:04 -0800610 mutex_lock(&map->freeze_mutex);
611 map->writecnt--;
612 mutex_unlock(&map->freeze_mutex);
613 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800614}
615
616static const struct vm_operations_struct bpf_map_default_vmops = {
617 .open = bpf_map_mmap_open,
618 .close = bpf_map_mmap_close,
619};
620
621static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
622{
623 struct bpf_map *map = filp->private_data;
624 int err;
625
626 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
627 return -ENOTSUPP;
628
629 if (!(vma->vm_flags & VM_SHARED))
630 return -EINVAL;
631
632 mutex_lock(&map->freeze_mutex);
633
Andrii Nakryikodfeb3762020-05-18 22:38:24 -0700634 if (vma->vm_flags & VM_WRITE) {
635 if (map->frozen) {
636 err = -EPERM;
637 goto out;
638 }
639 /* map is meant to be read-only, so do not allow mapping as
640 * writable, because it's possible to leak a writable page
641 * reference and allows user-space to still modify it after
642 * freezing, while verifier will assume contents do not change
643 */
644 if (map->map_flags & BPF_F_RDONLY_PROG) {
645 err = -EACCES;
646 goto out;
647 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800648 }
649
650 /* set default open/close callbacks */
651 vma->vm_ops = &bpf_map_default_vmops;
652 vma->vm_private_data = map;
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700653 vma->vm_flags &= ~VM_MAYEXEC;
654 if (!(vma->vm_flags & VM_WRITE))
655 /* disallow re-mapping with PROT_WRITE */
656 vma->vm_flags &= ~VM_MAYWRITE;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800657
658 err = map->ops->map_mmap(map, vma);
659 if (err)
660 goto out;
661
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700662 if (vma->vm_flags & VM_MAYWRITE)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800663 map->writecnt++;
664out:
665 mutex_unlock(&map->freeze_mutex);
666 return err;
667}
668
Andrii Nakryiko457f4432020-05-29 00:54:20 -0700669static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
670{
671 struct bpf_map *map = filp->private_data;
672
673 if (map->ops->map_poll)
674 return map->ops->map_poll(map, filp, pts);
675
676 return EPOLLERR;
677}
678
Chenbo Fengf66e4482017-10-18 13:00:26 -0700679const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100680#ifdef CONFIG_PROC_FS
681 .show_fdinfo = bpf_map_show_fdinfo,
682#endif
683 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700684 .read = bpf_dummy_read,
685 .write = bpf_dummy_write,
Andrii Nakryikofc970222019-11-17 09:28:04 -0800686 .mmap = bpf_map_mmap,
Andrii Nakryiko457f4432020-05-29 00:54:20 -0700687 .poll = bpf_map_poll,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700688};
689
Chenbo Feng6e71b042017-10-18 13:00:22 -0700690int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100691{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700692 int ret;
693
694 ret = security_bpf_map(map, OPEN_FMODE(flags));
695 if (ret < 0)
696 return ret;
697
Daniel Borkmannaa797812015-10-29 14:58:06 +0100698 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700699 flags | O_CLOEXEC);
700}
701
702int bpf_get_file_flag(int flags)
703{
704 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
705 return -EINVAL;
706 if (flags & BPF_F_RDONLY)
707 return O_RDONLY;
708 if (flags & BPF_F_WRONLY)
709 return O_WRONLY;
710 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100711}
712
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700713/* helper macro to check that unused fields 'union bpf_attr' are zero */
714#define CHECK_ATTR(CMD) \
715 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
716 sizeof(attr->CMD##_LAST_FIELD), 0, \
717 sizeof(*attr) - \
718 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
719 sizeof(attr->CMD##_LAST_FIELD)) != NULL
720
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700721/* dst and src must have at least "size" number of bytes.
722 * Return strlen on success and < 0 on error.
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700723 */
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700724int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700725{
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700726 const char *end = src + size;
727 const char *orig_src = src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700728
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700729 memset(dst, 0, size);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200730 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700731 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200732 if (!isalnum(*src) &&
733 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700734 return -EINVAL;
735 *dst++ = *src++;
736 }
737
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700738 /* No '\0' found in "size" number of bytes */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700739 if (src == end)
740 return -EINVAL;
741
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700742 return src - orig_src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700743}
744
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200745int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800746 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200747 const struct btf_type *key_type,
748 const struct btf_type *value_type)
749{
750 return -ENOTSUPP;
751}
752
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800753static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200754 u32 btf_key_id, u32 btf_value_id)
755{
756 const struct btf_type *key_type, *value_type;
757 u32 key_size, value_size;
758 int ret = 0;
759
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200760 /* Some maps allow key to be unspecified. */
761 if (btf_key_id) {
762 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
763 if (!key_type || key_size != map->key_size)
764 return -EINVAL;
765 } else {
766 key_type = btf_type_by_id(btf, 0);
767 if (!map->ops->map_check_btf)
768 return -EINVAL;
769 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200770
771 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
772 if (!value_type || value_size != map->value_size)
773 return -EINVAL;
774
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800775 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
776
777 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200778 if (map->map_flags & BPF_F_RDONLY_PROG)
779 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800780 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -0800781 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700782 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
KP Singh8ea63682020-08-25 20:29:17 +0200783 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
KP Singh4cf1bc12020-11-06 10:37:40 +0000784 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
785 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800786 return -ENOTSUPP;
787 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
788 map->value_size) {
789 WARN_ONCE(1,
790 "verifier bug spin_lock_off %d value_size %d\n",
791 map->spin_lock_off, map->value_size);
792 return -EFAULT;
793 }
794 }
795
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200796 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800797 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200798
799 return ret;
800}
801
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800802#define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700803/* called via syscall */
804static int map_create(union bpf_attr *attr)
805{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700806 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700807 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700808 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700809 int err;
810
811 err = CHECK_ATTR(BPF_MAP_CREATE);
812 if (err)
813 return -EINVAL;
814
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800815 if (attr->btf_vmlinux_value_type_id) {
816 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
817 attr->btf_key_type_id || attr->btf_value_type_id)
818 return -EINVAL;
819 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
820 return -EINVAL;
821 }
822
Chenbo Feng6e71b042017-10-18 13:00:22 -0700823 f_flags = bpf_get_file_flag(attr->map_flags);
824 if (f_flags < 0)
825 return f_flags;
826
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700827 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700828 ((unsigned int)numa_node >= nr_node_ids ||
829 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700830 return -EINVAL;
831
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700832 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
833 map = find_and_alloc_map(attr);
834 if (IS_ERR(map))
835 return PTR_ERR(map);
836
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700837 err = bpf_obj_name_cpy(map->name, attr->map_name,
838 sizeof(attr->map_name));
839 if (err < 0)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700840 goto free_map;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700841
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800842 atomic64_set(&map->refcnt, 1);
843 atomic64_set(&map->usercnt, 1);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800844 mutex_init(&map->freeze_mutex);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700845
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800846 map->spin_lock_off = -EINVAL;
847 if (attr->btf_key_type_id || attr->btf_value_type_id ||
848 /* Even the map's value is a kernel's struct,
849 * the bpf_prog.o must have BTF to begin with
850 * to figure out the corresponding kernel's
851 * counter part. Thus, attr->btf_fd has
852 * to be valid also.
853 */
854 attr->btf_vmlinux_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700855 struct btf *btf;
856
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700857 btf = btf_get_by_fd(attr->btf_fd);
858 if (IS_ERR(btf)) {
859 err = PTR_ERR(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700860 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700861 }
Alexei Starovoitov350a5c42021-03-07 14:52:48 -0800862 if (btf_is_kernel(btf)) {
863 btf_put(btf);
864 err = -EACCES;
865 goto free_map;
866 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800867 map->btf = btf;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700868
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800869 if (attr->btf_value_type_id) {
870 err = map_check_btf(map, btf, attr->btf_key_type_id,
871 attr->btf_value_type_id);
872 if (err)
873 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700874 }
875
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700876 map->btf_key_type_id = attr->btf_key_type_id;
877 map->btf_value_type_id = attr->btf_value_type_id;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800878 map->btf_vmlinux_value_type_id =
879 attr->btf_vmlinux_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700880 }
881
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700882 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700883 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700884 goto free_map;
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700885
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700886 err = bpf_map_alloc_id(map);
887 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700888 goto free_map_sec;
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700889
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800890 bpf_map_save_memcg(map);
891
Chenbo Feng6e71b042017-10-18 13:00:22 -0700892 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700893 if (err < 0) {
894 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800895 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700896 * bpf_map_alloc_id() has published the map
897 * to the userspace and the userspace may
898 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
899 */
Peng Sun352d20d2019-02-27 22:36:25 +0800900 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700901 return err;
902 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700903
904 return err;
905
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700906free_map_sec:
907 security_bpf_map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700908free_map:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700909 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700910 map->ops->map_free(map);
911 return err;
912}
913
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700914/* if error is returned, fd is released.
915 * On success caller should complete fd access with matching fdput()
916 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100917struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700918{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700919 if (!f.file)
920 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700921 if (f.file->f_op != &bpf_map_fops) {
922 fdput(f);
923 return ERR_PTR(-EINVAL);
924 }
925
Daniel Borkmannc2101292015-10-29 14:58:07 +0100926 return f.file->private_data;
927}
928
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800929void bpf_map_inc(struct bpf_map *map)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100930{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800931 atomic64_inc(&map->refcnt);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100932}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700933EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100934
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800935void bpf_map_inc_with_uref(struct bpf_map *map)
936{
937 atomic64_inc(&map->refcnt);
938 atomic64_inc(&map->usercnt);
939}
940EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
941
Martin KaFai Lau1ed4d922020-02-25 15:04:21 -0800942struct bpf_map *bpf_map_get(u32 ufd)
943{
944 struct fd f = fdget(ufd);
945 struct bpf_map *map;
946
947 map = __bpf_map_get(f);
948 if (IS_ERR(map))
949 return map;
950
951 bpf_map_inc(map);
952 fdput(f);
953
954 return map;
955}
956
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100957struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100958{
959 struct fd f = fdget(ufd);
960 struct bpf_map *map;
961
962 map = __bpf_map_get(f);
963 if (IS_ERR(map))
964 return map;
965
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800966 bpf_map_inc_with_uref(map);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100967 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700968
969 return map;
970}
971
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700972/* map_idr_lock should have been held */
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800973static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700974{
975 int refold;
976
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800977 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700978 if (!refold)
979 return ERR_PTR(-ENOENT);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700980 if (uref)
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800981 atomic64_inc(&map->usercnt);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700982
983 return map;
984}
985
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800986struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700987{
988 spin_lock_bh(&map_idr_lock);
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800989 map = __bpf_map_inc_not_zero(map, false);
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700990 spin_unlock_bh(&map_idr_lock);
991
992 return map;
993}
994EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
995
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800996int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
997{
998 return -ENOTSUPP;
999}
1000
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001001static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1002{
1003 if (key_size)
1004 return memdup_user(ukey, key_size);
1005
1006 if (ukey)
1007 return ERR_PTR(-EINVAL);
1008
1009 return NULL;
1010}
1011
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001012static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1013{
1014 if (key_size)
1015 return memdup_bpfptr(ukey, key_size);
1016
1017 if (!bpfptr_is_null(ukey))
1018 return ERR_PTR(-EINVAL);
1019
1020 return NULL;
1021}
1022
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001023/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001024#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001025
1026static int map_lookup_elem(union bpf_attr *attr)
1027{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001028 void __user *ukey = u64_to_user_ptr(attr->key);
1029 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001030 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001031 struct bpf_map *map;
Brian Vazquez15c14a32020-01-15 10:43:00 -08001032 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001033 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001034 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001035 int err;
1036
1037 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1038 return -EINVAL;
1039
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001040 if (attr->flags & ~BPF_F_LOCK)
1041 return -EINVAL;
1042
Daniel Borkmann592867b2015-09-08 18:00:09 +02001043 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001044 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001045 if (IS_ERR(map))
1046 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001047 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001048 err = -EPERM;
1049 goto err_put;
1050 }
1051
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001052 if ((attr->flags & BPF_F_LOCK) &&
1053 !map_value_has_spin_lock(map)) {
1054 err = -EINVAL;
1055 goto err_put;
1056 }
1057
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001058 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001059 if (IS_ERR(key)) {
1060 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001061 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001062 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001063
Brian Vazquez15c14a32020-01-15 10:43:00 -08001064 value_size = bpf_map_value_size(map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001065
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001066 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001067 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001068 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001069 goto free_key;
1070
Brian Vazquez15c14a32020-01-15 10:43:00 -08001071 err = bpf_map_copy_value(map, key, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001072 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001073 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001074
1075 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001076 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001077 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001078
1079 err = 0;
1080
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001081free_value:
1082 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001083free_key:
1084 kfree(key);
1085err_put:
1086 fdput(f);
1087 return err;
1088}
1089
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001090
Alexei Starovoitov3274f522014-11-13 17:36:44 -08001091#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001092
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001093static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001094{
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001095 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1096 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001097 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001098 struct bpf_map *map;
1099 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001100 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001101 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001102 int err;
1103
1104 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1105 return -EINVAL;
1106
Daniel Borkmann592867b2015-09-08 18:00:09 +02001107 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001108 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001109 if (IS_ERR(map))
1110 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001111 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001112 err = -EPERM;
1113 goto err_put;
1114 }
1115
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001116 if ((attr->flags & BPF_F_LOCK) &&
1117 !map_value_has_spin_lock(map)) {
1118 err = -EINVAL;
1119 goto err_put;
1120 }
1121
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001122 key = ___bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001123 if (IS_ERR(key)) {
1124 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001125 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001126 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001127
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001128 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001129 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +00001130 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1131 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001132 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1133 else
1134 value_size = map->value_size;
1135
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001136 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001137 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001138 if (!value)
1139 goto free_key;
1140
1141 err = -EFAULT;
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001142 if (copy_from_bpfptr(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001143 goto free_value;
1144
Brian Vazquez15c14a32020-01-15 10:43:00 -08001145 err = bpf_map_update_value(map, f, key, value, attr->flags);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001146
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001147free_value:
1148 kfree(value);
1149free_key:
1150 kfree(key);
1151err_put:
1152 fdput(f);
1153 return err;
1154}
1155
1156#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1157
1158static int map_delete_elem(union bpf_attr *attr)
1159{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001160 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001161 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001162 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001163 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001164 void *key;
1165 int err;
1166
1167 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1168 return -EINVAL;
1169
Daniel Borkmann592867b2015-09-08 18:00:09 +02001170 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001171 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001172 if (IS_ERR(map))
1173 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001174 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001175 err = -EPERM;
1176 goto err_put;
1177 }
1178
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001179 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001180 if (IS_ERR(key)) {
1181 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001182 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001183 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001184
Jakub Kicinskia3884572018-01-11 20:29:09 -08001185 if (bpf_map_is_dev_bound(map)) {
1186 err = bpf_map_offload_delete_elem(map, key);
1187 goto out;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001188 } else if (IS_FD_PROG_ARRAY(map) ||
1189 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1190 /* These maps require sleepable context */
Daniel Borkmannda765a22019-11-22 21:07:58 +01001191 err = map->ops->map_delete_elem(map, key);
1192 goto out;
Jakub Kicinskia3884572018-01-11 20:29:09 -08001193 }
1194
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001195 bpf_disable_instrumentation();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001196 rcu_read_lock();
1197 err = map->ops->map_delete_elem(map, key);
1198 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001199 bpf_enable_instrumentation();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001200 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001201out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001202 kfree(key);
1203err_put:
1204 fdput(f);
1205 return err;
1206}
1207
1208/* last field in 'union bpf_attr' used by this command */
1209#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1210
1211static int map_get_next_key(union bpf_attr *attr)
1212{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001213 void __user *ukey = u64_to_user_ptr(attr->key);
1214 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001215 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001216 struct bpf_map *map;
1217 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001218 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001219 int err;
1220
1221 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1222 return -EINVAL;
1223
Daniel Borkmann592867b2015-09-08 18:00:09 +02001224 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001225 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001226 if (IS_ERR(map))
1227 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001228 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001229 err = -EPERM;
1230 goto err_put;
1231 }
1232
Teng Qin8fe45922017-04-24 19:00:37 -07001233 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001234 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001235 if (IS_ERR(key)) {
1236 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001237 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001238 }
Teng Qin8fe45922017-04-24 19:00:37 -07001239 } else {
1240 key = NULL;
1241 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001242
1243 err = -ENOMEM;
1244 next_key = kmalloc(map->key_size, GFP_USER);
1245 if (!next_key)
1246 goto free_key;
1247
Jakub Kicinskia3884572018-01-11 20:29:09 -08001248 if (bpf_map_is_dev_bound(map)) {
1249 err = bpf_map_offload_get_next_key(map, key, next_key);
1250 goto out;
1251 }
1252
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001253 rcu_read_lock();
1254 err = map->ops->map_get_next_key(map, key, next_key);
1255 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001256out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001257 if (err)
1258 goto free_next_key;
1259
1260 err = -EFAULT;
1261 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1262 goto free_next_key;
1263
1264 err = 0;
1265
1266free_next_key:
1267 kfree(next_key);
1268free_key:
1269 kfree(key);
1270err_put:
1271 fdput(f);
1272 return err;
1273}
1274
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001275int generic_map_delete_batch(struct bpf_map *map,
1276 const union bpf_attr *attr,
1277 union bpf_attr __user *uattr)
1278{
1279 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1280 u32 cp, max_count;
1281 int err = 0;
1282 void *key;
1283
1284 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1285 return -EINVAL;
1286
1287 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1288 !map_value_has_spin_lock(map)) {
1289 return -EINVAL;
1290 }
1291
1292 max_count = attr->batch.count;
1293 if (!max_count)
1294 return 0;
1295
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001296 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1297 if (!key)
1298 return -ENOMEM;
1299
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001300 for (cp = 0; cp < max_count; cp++) {
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001301 err = -EFAULT;
1302 if (copy_from_user(key, keys + cp * map->key_size,
1303 map->key_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001304 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001305
1306 if (bpf_map_is_dev_bound(map)) {
1307 err = bpf_map_offload_delete_elem(map, key);
1308 break;
1309 }
1310
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001311 bpf_disable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001312 rcu_read_lock();
1313 err = map->ops->map_delete_elem(map, key);
1314 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001315 bpf_enable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001316 maybe_wait_bpf_programs(map);
1317 if (err)
1318 break;
1319 }
1320 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1321 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001322
1323 kfree(key);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001324 return err;
1325}
1326
1327int generic_map_update_batch(struct bpf_map *map,
1328 const union bpf_attr *attr,
1329 union bpf_attr __user *uattr)
1330{
1331 void __user *values = u64_to_user_ptr(attr->batch.values);
1332 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1333 u32 value_size, cp, max_count;
1334 int ufd = attr->map_fd;
1335 void *key, *value;
1336 struct fd f;
1337 int err = 0;
1338
1339 f = fdget(ufd);
1340 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1341 return -EINVAL;
1342
1343 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1344 !map_value_has_spin_lock(map)) {
1345 return -EINVAL;
1346 }
1347
1348 value_size = bpf_map_value_size(map);
1349
1350 max_count = attr->batch.count;
1351 if (!max_count)
1352 return 0;
1353
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001354 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1355 if (!key)
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001356 return -ENOMEM;
1357
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001358 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1359 if (!value) {
1360 kfree(key);
1361 return -ENOMEM;
1362 }
1363
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001364 for (cp = 0; cp < max_count; cp++) {
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001365 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001366 if (copy_from_user(key, keys + cp * map->key_size,
1367 map->key_size) ||
1368 copy_from_user(value, values + cp * value_size, value_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001369 break;
1370
1371 err = bpf_map_update_value(map, f, key, value,
1372 attr->batch.elem_flags);
1373
1374 if (err)
1375 break;
1376 }
1377
1378 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1379 err = -EFAULT;
1380
1381 kfree(value);
1382 kfree(key);
1383 return err;
1384}
1385
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001386#define MAP_LOOKUP_RETRIES 3
1387
1388int generic_map_lookup_batch(struct bpf_map *map,
1389 const union bpf_attr *attr,
1390 union bpf_attr __user *uattr)
1391{
1392 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1393 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1394 void __user *values = u64_to_user_ptr(attr->batch.values);
1395 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1396 void *buf, *buf_prevkey, *prev_key, *key, *value;
1397 int err, retry = MAP_LOOKUP_RETRIES;
1398 u32 value_size, cp, max_count;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001399
1400 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1401 return -EINVAL;
1402
1403 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1404 !map_value_has_spin_lock(map))
1405 return -EINVAL;
1406
1407 value_size = bpf_map_value_size(map);
1408
1409 max_count = attr->batch.count;
1410 if (!max_count)
1411 return 0;
1412
1413 if (put_user(0, &uattr->batch.count))
1414 return -EFAULT;
1415
1416 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1417 if (!buf_prevkey)
1418 return -ENOMEM;
1419
1420 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1421 if (!buf) {
Denis Efremovbb2359f2020-06-01 19:28:14 +03001422 kfree(buf_prevkey);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001423 return -ENOMEM;
1424 }
1425
1426 err = -EFAULT;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001427 prev_key = NULL;
1428 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1429 goto free_buf;
1430 key = buf;
1431 value = key + map->key_size;
1432 if (ubatch)
1433 prev_key = buf_prevkey;
1434
1435 for (cp = 0; cp < max_count;) {
1436 rcu_read_lock();
1437 err = map->ops->map_get_next_key(map, prev_key, key);
1438 rcu_read_unlock();
1439 if (err)
1440 break;
1441 err = bpf_map_copy_value(map, key, value,
1442 attr->batch.elem_flags);
1443
1444 if (err == -ENOENT) {
1445 if (retry) {
1446 retry--;
1447 continue;
1448 }
1449 err = -EINTR;
1450 break;
1451 }
1452
1453 if (err)
1454 goto free_buf;
1455
1456 if (copy_to_user(keys + cp * map->key_size, key,
1457 map->key_size)) {
1458 err = -EFAULT;
1459 goto free_buf;
1460 }
1461 if (copy_to_user(values + cp * value_size, value, value_size)) {
1462 err = -EFAULT;
1463 goto free_buf;
1464 }
1465
1466 if (!prev_key)
1467 prev_key = buf_prevkey;
1468
1469 swap(prev_key, key);
1470 retry = MAP_LOOKUP_RETRIES;
1471 cp++;
1472 }
1473
1474 if (err == -EFAULT)
1475 goto free_buf;
1476
1477 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1478 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1479 err = -EFAULT;
1480
1481free_buf:
1482 kfree(buf_prevkey);
1483 kfree(buf);
1484 return err;
1485}
1486
Denis Salopek3e87f192021-05-11 23:00:04 +02001487#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001488
1489static int map_lookup_and_delete_elem(union bpf_attr *attr)
1490{
1491 void __user *ukey = u64_to_user_ptr(attr->key);
1492 void __user *uvalue = u64_to_user_ptr(attr->value);
1493 int ufd = attr->map_fd;
1494 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001495 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001496 u32 value_size;
1497 struct fd f;
1498 int err;
1499
1500 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1501 return -EINVAL;
1502
Denis Salopek3e87f192021-05-11 23:00:04 +02001503 if (attr->flags & ~BPF_F_LOCK)
1504 return -EINVAL;
1505
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001506 f = fdget(ufd);
1507 map = __bpf_map_get(f);
1508 if (IS_ERR(map))
1509 return PTR_ERR(map);
Anton Protopopov1ea0f912020-05-27 18:56:59 +00001510 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1511 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001512 err = -EPERM;
1513 goto err_put;
1514 }
1515
Denis Salopek3e87f192021-05-11 23:00:04 +02001516 if (attr->flags &&
1517 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1518 map->map_type == BPF_MAP_TYPE_STACK)) {
1519 err = -EINVAL;
1520 goto err_put;
1521 }
1522
1523 if ((attr->flags & BPF_F_LOCK) &&
1524 !map_value_has_spin_lock(map)) {
1525 err = -EINVAL;
1526 goto err_put;
1527 }
1528
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001529 key = __bpf_copy_key(ukey, map->key_size);
1530 if (IS_ERR(key)) {
1531 err = PTR_ERR(key);
1532 goto err_put;
1533 }
1534
Denis Salopek3e87f192021-05-11 23:00:04 +02001535 value_size = bpf_map_value_size(map);
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001536
1537 err = -ENOMEM;
1538 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1539 if (!value)
1540 goto free_key;
1541
Denis Salopek3e87f192021-05-11 23:00:04 +02001542 err = -ENOTSUPP;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001543 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1544 map->map_type == BPF_MAP_TYPE_STACK) {
1545 err = map->ops->map_pop_elem(map, value);
Denis Salopek3e87f192021-05-11 23:00:04 +02001546 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1547 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1548 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1549 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1550 if (!bpf_map_is_dev_bound(map)) {
1551 bpf_disable_instrumentation();
1552 rcu_read_lock();
1553 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1554 rcu_read_unlock();
1555 bpf_enable_instrumentation();
1556 }
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001557 }
1558
1559 if (err)
1560 goto free_value;
1561
Wei Yongjun7f645462020-04-30 08:18:51 +00001562 if (copy_to_user(uvalue, value, value_size) != 0) {
1563 err = -EFAULT;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001564 goto free_value;
Wei Yongjun7f645462020-04-30 08:18:51 +00001565 }
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001566
1567 err = 0;
1568
1569free_value:
1570 kfree(value);
1571free_key:
1572 kfree(key);
1573err_put:
1574 fdput(f);
1575 return err;
1576}
1577
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001578#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1579
1580static int map_freeze(const union bpf_attr *attr)
1581{
1582 int err = 0, ufd = attr->map_fd;
1583 struct bpf_map *map;
1584 struct fd f;
1585
1586 if (CHECK_ATTR(BPF_MAP_FREEZE))
1587 return -EINVAL;
1588
1589 f = fdget(ufd);
1590 map = __bpf_map_get(f);
1591 if (IS_ERR(map))
1592 return PTR_ERR(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -08001593
Martin KaFai Lau849b4d92020-03-04 17:34:54 -08001594 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1595 fdput(f);
1596 return -ENOTSUPP;
1597 }
1598
Andrii Nakryikofc970222019-11-17 09:28:04 -08001599 mutex_lock(&map->freeze_mutex);
1600
1601 if (map->writecnt) {
1602 err = -EBUSY;
1603 goto err_put;
1604 }
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001605 if (READ_ONCE(map->frozen)) {
1606 err = -EBUSY;
1607 goto err_put;
1608 }
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07001609 if (!bpf_capable()) {
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001610 err = -EPERM;
1611 goto err_put;
1612 }
1613
1614 WRITE_ONCE(map->frozen, true);
1615err_put:
Andrii Nakryikofc970222019-11-17 09:28:04 -08001616 mutex_unlock(&map->freeze_mutex);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001617 fdput(f);
1618 return err;
1619}
1620
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001621static const struct bpf_prog_ops * const bpf_prog_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001622#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001623 [_id] = & _name ## _prog_ops,
1624#define BPF_MAP_TYPE(_id, _ops)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001625#define BPF_LINK_TYPE(_id, _name)
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001626#include <linux/bpf_types.h>
1627#undef BPF_PROG_TYPE
1628#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001629#undef BPF_LINK_TYPE
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001630};
1631
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001632static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1633{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001634 const struct bpf_prog_ops *ops;
1635
1636 if (type >= ARRAY_SIZE(bpf_prog_types))
1637 return -EINVAL;
1638 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1639 ops = bpf_prog_types[type];
1640 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001641 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001642
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001643 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001644 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001645 else
1646 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001647 prog->type = type;
1648 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001649}
1650
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001651enum bpf_audit {
1652 BPF_AUDIT_LOAD,
1653 BPF_AUDIT_UNLOAD,
1654 BPF_AUDIT_MAX,
1655};
1656
1657static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1658 [BPF_AUDIT_LOAD] = "LOAD",
1659 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1660};
1661
1662static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1663{
1664 struct audit_context *ctx = NULL;
1665 struct audit_buffer *ab;
1666
1667 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1668 return;
1669 if (audit_enabled == AUDIT_OFF)
1670 return;
1671 if (op == BPF_AUDIT_LOAD)
1672 ctx = audit_context();
1673 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1674 if (unlikely(!ab))
1675 return;
1676 audit_log_format(ab, "prog-id=%u op=%s",
1677 prog->aux->id, bpf_audit_str[op]);
1678 audit_log_end(ab);
1679}
1680
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001681static int bpf_prog_alloc_id(struct bpf_prog *prog)
1682{
1683 int id;
1684
Shaohua Lib76354c2018-03-27 11:53:21 -07001685 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001686 spin_lock_bh(&prog_idr_lock);
1687 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1688 if (id > 0)
1689 prog->aux->id = id;
1690 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001691 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001692
1693 /* id is in [1, INT_MAX) */
1694 if (WARN_ON_ONCE(!id))
1695 return -ENOSPC;
1696
1697 return id > 0 ? 0 : id;
1698}
1699
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001700void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001701{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001702 /* cBPF to eBPF migrations are currently not in the idr store.
1703 * Offloaded programs are removed from the store when their device
1704 * disappears - even if someone grabs an fd to them they are unusable,
1705 * simply waiting for refcnt to drop to be freed.
1706 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001707 if (!prog->aux->id)
1708 return;
1709
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001710 if (do_idr_lock)
1711 spin_lock_bh(&prog_idr_lock);
1712 else
1713 __acquire(&prog_idr_lock);
1714
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001715 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001716 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001717
1718 if (do_idr_lock)
1719 spin_unlock_bh(&prog_idr_lock);
1720 else
1721 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001722}
1723
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001724static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001725{
1726 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1727
Daniel Borkmann3b4d9eb2019-10-22 23:30:38 +02001728 kvfree(aux->func_info);
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08001729 kfree(aux->func_info_aux);
Roman Gushchin3ac1f012020-12-01 13:58:59 -08001730 free_uid(aux->user);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001731 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001732 bpf_prog_free(aux->prog);
1733}
1734
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001735static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1736{
1737 bpf_prog_kallsyms_del_all(prog);
1738 btf_put(prog->aux->btf);
Martin KaFai Laue16301f2021-03-24 18:51:30 -07001739 kvfree(prog->aux->jited_linfo);
1740 kvfree(prog->aux->linfo);
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07001741 kfree(prog->aux->kfunc_tab);
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08001742 if (prog->aux->attach_btf)
1743 btf_put(prog->aux->attach_btf);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001744
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07001745 if (deferred) {
1746 if (prog->aux->sleepable)
1747 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1748 else
1749 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1750 } else {
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001751 __bpf_prog_put_rcu(&prog->aux->rcu);
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07001752 }
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001753}
1754
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001755static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001756{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001757 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001758 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001759 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001760 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001761 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001762 __bpf_prog_put_noref(prog, true);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001763 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001764}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001765
1766void bpf_prog_put(struct bpf_prog *prog)
1767{
1768 __bpf_prog_put(prog, true);
1769}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001770EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001771
1772static int bpf_prog_release(struct inode *inode, struct file *filp)
1773{
1774 struct bpf_prog *prog = filp->private_data;
1775
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001776 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001777 return 0;
1778}
1779
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001780static void bpf_prog_get_stats(const struct bpf_prog *prog,
1781 struct bpf_prog_stats *stats)
1782{
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001783 u64 nsecs = 0, cnt = 0, misses = 0;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001784 int cpu;
1785
1786 for_each_possible_cpu(cpu) {
1787 const struct bpf_prog_stats *st;
1788 unsigned int start;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001789 u64 tnsecs, tcnt, tmisses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001790
Alexei Starovoitov700d4792021-02-09 19:36:26 -08001791 st = per_cpu_ptr(prog->stats, cpu);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001792 do {
1793 start = u64_stats_fetch_begin_irq(&st->syncp);
1794 tnsecs = st->nsecs;
1795 tcnt = st->cnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001796 tmisses = st->misses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001797 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1798 nsecs += tnsecs;
1799 cnt += tcnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001800 misses += tmisses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001801 }
1802 stats->nsecs = nsecs;
1803 stats->cnt = cnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001804 stats->misses = misses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001805}
1806
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001807#ifdef CONFIG_PROC_FS
1808static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1809{
1810 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001811 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001812 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001813
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001814 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001815 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001816 seq_printf(m,
1817 "prog_type:\t%u\n"
1818 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001819 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001820 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001821 "prog_id:\t%u\n"
1822 "run_time_ns:\t%llu\n"
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001823 "run_cnt:\t%llu\n"
1824 "recursion_misses:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001825 prog->type,
1826 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001827 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001828 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001829 prog->aux->id,
1830 stats.nsecs,
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001831 stats.cnt,
1832 stats.misses);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001833}
1834#endif
1835
Chenbo Fengf66e4482017-10-18 13:00:26 -07001836const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001837#ifdef CONFIG_PROC_FS
1838 .show_fdinfo = bpf_prog_show_fdinfo,
1839#endif
1840 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001841 .read = bpf_dummy_read,
1842 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001843};
1844
Daniel Borkmannb2197752015-10-29 14:58:09 +01001845int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001846{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001847 int ret;
1848
1849 ret = security_bpf_prog(prog);
1850 if (ret < 0)
1851 return ret;
1852
Daniel Borkmannaa797812015-10-29 14:58:06 +01001853 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1854 O_RDWR | O_CLOEXEC);
1855}
1856
Daniel Borkmann113214b2016-06-30 17:24:44 +02001857static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001858{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001859 if (!f.file)
1860 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001861 if (f.file->f_op != &bpf_prog_fops) {
1862 fdput(f);
1863 return ERR_PTR(-EINVAL);
1864 }
1865
Daniel Borkmannc2101292015-10-29 14:58:07 +01001866 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001867}
1868
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001869void bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001870{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001871 atomic64_add(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001872}
Brenden Blanco59d36562016-07-19 12:16:46 -07001873EXPORT_SYMBOL_GPL(bpf_prog_add);
1874
Daniel Borkmannc5405942016-11-09 22:02:34 +01001875void bpf_prog_sub(struct bpf_prog *prog, int i)
1876{
1877 /* Only to be used for undoing previous bpf_prog_add() in some
1878 * error path. We still know that another entity in our call
1879 * path holds a reference to the program, thus atomic_sub() can
1880 * be safely used in such cases!
1881 */
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001882 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
Daniel Borkmannc5405942016-11-09 22:02:34 +01001883}
1884EXPORT_SYMBOL_GPL(bpf_prog_sub);
1885
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001886void bpf_prog_inc(struct bpf_prog *prog)
Brenden Blanco59d36562016-07-19 12:16:46 -07001887{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001888 atomic64_inc(&prog->aux->refcnt);
Brenden Blanco59d36562016-07-19 12:16:46 -07001889}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001890EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001891
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001892/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001893struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001894{
1895 int refold;
1896
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001897 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001898
1899 if (!refold)
1900 return ERR_PTR(-ENOENT);
1901
1902 return prog;
1903}
John Fastabenda6f6df62017-08-15 22:32:22 -07001904EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001905
Al Viro040ee692017-12-02 20:20:38 -05001906bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001907 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001908{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001909 /* not an attachment, just a refcount inc, always allow */
1910 if (!attach_type)
1911 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001912
1913 if (prog->type != *attach_type)
1914 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001915 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001916 return false;
1917
1918 return true;
1919}
1920
1921static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001922 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001923{
1924 struct fd f = fdget(ufd);
1925 struct bpf_prog *prog;
1926
Daniel Borkmann113214b2016-06-30 17:24:44 +02001927 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001928 if (IS_ERR(prog))
1929 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001930 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001931 prog = ERR_PTR(-EINVAL);
1932 goto out;
1933 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001934
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001935 bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001936out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001937 fdput(f);
1938 return prog;
1939}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001940
1941struct bpf_prog *bpf_prog_get(u32 ufd)
1942{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001943 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001944}
1945
Jakub Kicinski248f3462017-11-03 13:56:20 -07001946struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001947 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001948{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001949 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001950}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001951EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001952
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001953/* Initially all BPF programs could be loaded w/o specifying
1954 * expected_attach_type. Later for some of them specifying expected_attach_type
1955 * at load time became required so that program could be validated properly.
1956 * Programs of types that are allowed to be loaded both w/ and w/o (for
1957 * backward compatibility) expected_attach_type, should have the default attach
1958 * type assigned to expected_attach_type for the latter case, so that it can be
1959 * validated later at attach time.
1960 *
1961 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1962 * prog type requires it but has some attach types that have to be backward
1963 * compatible.
1964 */
1965static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1966{
1967 switch (attr->prog_type) {
1968 case BPF_PROG_TYPE_CGROUP_SOCK:
1969 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1970 * exist so checking for non-zero is the way to go here.
1971 */
1972 if (!attr->expected_attach_type)
1973 attr->expected_attach_type =
1974 BPF_CGROUP_INET_SOCK_CREATE;
1975 break;
Kuniyuki Iwashimad5e4dda2021-06-12 21:32:22 +09001976 case BPF_PROG_TYPE_SK_REUSEPORT:
1977 if (!attr->expected_attach_type)
1978 attr->expected_attach_type =
1979 BPF_SK_REUSEPORT_SELECT;
1980 break;
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001981 }
1982}
1983
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001984static int
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07001985bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1986 enum bpf_attach_type expected_attach_type,
Andrii Nakryiko290248a2020-12-03 12:46:30 -08001987 struct btf *attach_btf, u32 btf_id,
1988 struct bpf_prog *dst_prog)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001989{
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001990 if (btf_id) {
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001991 if (btf_id > BTF_MAX_TYPE)
1992 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001993
Andrii Nakryiko290248a2020-12-03 12:46:30 -08001994 if (!attach_btf && !dst_prog)
1995 return -EINVAL;
1996
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001997 switch (prog_type) {
1998 case BPF_PROG_TYPE_TRACING:
KP Singh9e4e01d2020-03-29 01:43:52 +01001999 case BPF_PROG_TYPE_LSM:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002000 case BPF_PROG_TYPE_STRUCT_OPS:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002001 case BPF_PROG_TYPE_EXT:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002002 break;
2003 default:
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07002004 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002005 }
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07002006 }
2007
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002008 if (attach_btf && (!btf_id || dst_prog))
2009 return -EINVAL;
2010
2011 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002012 prog_type != BPF_PROG_TYPE_EXT)
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002013 return -EINVAL;
2014
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07002015 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002016 case BPF_PROG_TYPE_CGROUP_SOCK:
2017 switch (expected_attach_type) {
2018 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07002019 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002020 case BPF_CGROUP_INET4_POST_BIND:
2021 case BPF_CGROUP_INET6_POST_BIND:
2022 return 0;
2023 default:
2024 return -EINVAL;
2025 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002026 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2027 switch (expected_attach_type) {
2028 case BPF_CGROUP_INET4_BIND:
2029 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002030 case BPF_CGROUP_INET4_CONNECT:
2031 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02002032 case BPF_CGROUP_INET4_GETPEERNAME:
2033 case BPF_CGROUP_INET6_GETPEERNAME:
2034 case BPF_CGROUP_INET4_GETSOCKNAME:
2035 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002036 case BPF_CGROUP_UDP4_SENDMSG:
2037 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002038 case BPF_CGROUP_UDP4_RECVMSG:
2039 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002040 return 0;
2041 default:
2042 return -EINVAL;
2043 }
brakmo5cf1e912019-05-28 16:59:36 -07002044 case BPF_PROG_TYPE_CGROUP_SKB:
2045 switch (expected_attach_type) {
2046 case BPF_CGROUP_INET_INGRESS:
2047 case BPF_CGROUP_INET_EGRESS:
2048 return 0;
2049 default:
2050 return -EINVAL;
2051 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002052 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2053 switch (expected_attach_type) {
2054 case BPF_CGROUP_SETSOCKOPT:
2055 case BPF_CGROUP_GETSOCKOPT:
2056 return 0;
2057 default:
2058 return -EINVAL;
2059 }
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02002060 case BPF_PROG_TYPE_SK_LOOKUP:
2061 if (expected_attach_type == BPF_SK_LOOKUP)
2062 return 0;
2063 return -EINVAL;
Kuniyuki Iwashimad5e4dda2021-06-12 21:32:22 +09002064 case BPF_PROG_TYPE_SK_REUSEPORT:
2065 switch (expected_attach_type) {
2066 case BPF_SK_REUSEPORT_SELECT:
2067 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2068 return 0;
2069 default:
2070 return -EINVAL;
2071 }
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07002072 case BPF_PROG_TYPE_SYSCALL:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002073 case BPF_PROG_TYPE_EXT:
2074 if (expected_attach_type)
2075 return -EINVAL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002076 fallthrough;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002077 default:
2078 return 0;
2079 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002080}
2081
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002082static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2083{
2084 switch (prog_type) {
2085 case BPF_PROG_TYPE_SCHED_CLS:
2086 case BPF_PROG_TYPE_SCHED_ACT:
2087 case BPF_PROG_TYPE_XDP:
2088 case BPF_PROG_TYPE_LWT_IN:
2089 case BPF_PROG_TYPE_LWT_OUT:
2090 case BPF_PROG_TYPE_LWT_XMIT:
2091 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2092 case BPF_PROG_TYPE_SK_SKB:
2093 case BPF_PROG_TYPE_SK_MSG:
2094 case BPF_PROG_TYPE_LIRC_MODE2:
2095 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2096 case BPF_PROG_TYPE_CGROUP_DEVICE:
2097 case BPF_PROG_TYPE_CGROUP_SOCK:
2098 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2099 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2100 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2101 case BPF_PROG_TYPE_SOCK_OPS:
2102 case BPF_PROG_TYPE_EXT: /* extends any prog */
2103 return true;
2104 case BPF_PROG_TYPE_CGROUP_SKB:
2105 /* always unpriv */
2106 case BPF_PROG_TYPE_SK_REUSEPORT:
2107 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2108 default:
2109 return false;
2110 }
2111}
2112
2113static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2114{
2115 switch (prog_type) {
2116 case BPF_PROG_TYPE_KPROBE:
2117 case BPF_PROG_TYPE_TRACEPOINT:
2118 case BPF_PROG_TYPE_PERF_EVENT:
2119 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2120 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2121 case BPF_PROG_TYPE_TRACING:
2122 case BPF_PROG_TYPE_LSM:
2123 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2124 case BPF_PROG_TYPE_EXT: /* extends any prog */
2125 return true;
2126 default:
2127 return false;
2128 }
2129}
2130
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002131/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov387544b2021-05-13 17:36:10 -07002132#define BPF_PROG_LOAD_LAST_FIELD fd_array
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002133
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07002134static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002135{
2136 enum bpf_prog_type type = attr->prog_type;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002137 struct bpf_prog *prog, *dst_prog = NULL;
2138 struct btf *attach_btf = NULL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002139 int err;
2140 char license[128];
2141 bool is_gpl;
2142
2143 if (CHECK_ATTR(BPF_PROG_LOAD))
2144 return -EINVAL;
2145
Jiong Wangc240eff2019-05-24 23:25:16 +01002146 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2147 BPF_F_ANY_ALIGNMENT |
Alexei Starovoitov10d274e2019-08-22 22:52:12 -07002148 BPF_F_TEST_STATE_FREQ |
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002149 BPF_F_SLEEPABLE |
Jiong Wangc240eff2019-05-24 23:25:16 +01002150 BPF_F_TEST_RND_HI32))
David S. Millere07b98d2017-05-10 11:38:07 -07002151 return -EINVAL;
2152
David Millere9ee9ef2018-11-30 21:08:14 -08002153 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2154 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002155 !bpf_capable())
David Millere9ee9ef2018-11-30 21:08:14 -08002156 return -EPERM;
2157
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002158 /* copy eBPF program license from user space */
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07002159 if (strncpy_from_bpfptr(license,
2160 make_bpfptr(attr->license, uattr.is_kernel),
2161 sizeof(license) - 1) < 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002162 return -EFAULT;
2163 license[sizeof(license) - 1] = 0;
2164
2165 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2166 is_gpl = license_is_gpl_compatible(license);
2167
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07002168 if (attr->insn_cnt == 0 ||
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002169 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01002170 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07002171 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2172 type != BPF_PROG_TYPE_CGROUP_SKB &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002173 !bpf_capable())
2174 return -EPERM;
2175
Maciej Żenczykowskib338cb92020-06-20 14:26:16 -07002176 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002177 return -EPERM;
2178 if (is_perfmon_prog_type(type) && !perfmon_capable())
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002179 return -EPERM;
2180
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002181 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2182 * or btf, we need to check which one it is
2183 */
2184 if (attr->attach_prog_fd) {
2185 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2186 if (IS_ERR(dst_prog)) {
2187 dst_prog = NULL;
2188 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2189 if (IS_ERR(attach_btf))
2190 return -EINVAL;
2191 if (!btf_is_kernel(attach_btf)) {
Andrii Nakryiko8bdd8e22020-12-07 22:43:26 -08002192 /* attaching through specifying bpf_prog's BTF
2193 * objects directly might be supported eventually
2194 */
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002195 btf_put(attach_btf);
Andrii Nakryiko8bdd8e22020-12-07 22:43:26 -08002196 return -ENOTSUPP;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002197 }
2198 }
2199 } else if (attr->attach_btf_id) {
2200 /* fall back to vmlinux BTF, if BTF type ID is specified */
2201 attach_btf = bpf_get_btf_vmlinux();
2202 if (IS_ERR(attach_btf))
2203 return PTR_ERR(attach_btf);
2204 if (!attach_btf)
2205 return -EINVAL;
2206 btf_get(attach_btf);
2207 }
2208
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002209 bpf_prog_load_fixup_attach_type(attr);
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002210 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002211 attach_btf, attr->attach_btf_id,
2212 dst_prog)) {
2213 if (dst_prog)
2214 bpf_prog_put(dst_prog);
2215 if (attach_btf)
2216 btf_put(attach_btf);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002217 return -EINVAL;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002218 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002219
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002220 /* plain bpf_prog allocation */
2221 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002222 if (!prog) {
2223 if (dst_prog)
2224 bpf_prog_put(dst_prog);
2225 if (attach_btf)
2226 btf_put(attach_btf);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002227 return -ENOMEM;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002228 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002229
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002230 prog->expected_attach_type = attr->expected_attach_type;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002231 prog->aux->attach_btf = attach_btf;
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002232 prog->aux->attach_btf_id = attr->attach_btf_id;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002233 prog->aux->dst_prog = dst_prog;
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002234 prog->aux->offload_requested = !!attr->prog_ifindex;
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002235 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002236
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002237 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002238 if (err)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002239 goto free_prog;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002240
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002241 prog->aux->user = get_current_user();
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002242 prog->len = attr->insn_cnt;
2243
2244 err = -EFAULT;
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07002245 if (copy_from_bpfptr(prog->insns,
2246 make_bpfptr(attr->insns, uattr.is_kernel),
2247 bpf_prog_insn_size(prog)) != 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002248 goto free_prog_sec;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002249
2250 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002251 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002252
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002253 atomic64_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002254 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002255
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002256 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002257 err = bpf_prog_offload_init(prog, attr);
2258 if (err)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002259 goto free_prog_sec;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002260 }
2261
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002262 /* find program type: socket_filter vs tracing_filter */
2263 err = find_prog_type(type, prog);
2264 if (err < 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002265 goto free_prog_sec;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002266
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002267 prog->aux->load_time = ktime_get_boottime_ns();
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -07002268 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2269 sizeof(attr->prog_name));
2270 if (err < 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002271 goto free_prog_sec;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002272
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002273 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08002274 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002275 if (err < 0)
2276 goto free_used_maps;
2277
Daniel Borkmann9facc332018-06-15 02:30:48 +02002278 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002279 if (err < 0)
2280 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002281
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07002282 err = bpf_prog_alloc_id(prog);
2283 if (err)
2284 goto free_used_maps;
2285
Daniel Borkmannc7517982019-08-23 22:14:23 +02002286 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2287 * effectively publicly exposed. However, retrieving via
2288 * bpf_prog_get_fd_by_id() will take another reference,
2289 * therefore it cannot be gone underneath us.
2290 *
2291 * Only for the time /after/ successful bpf_prog_new_fd()
2292 * and before returning to userspace, we might just hold
2293 * one reference and any parallel close on that fd could
2294 * rip everything out. Hence, below notifications must
2295 * happen before bpf_prog_new_fd().
2296 *
2297 * Also, any failure handling from this point onwards must
2298 * be using bpf_prog_put() given the program is exposed.
2299 */
Daniel Borkmann74451e662017-02-16 22:24:50 +01002300 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08002301 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01002302 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
Daniel Borkmannc7517982019-08-23 22:14:23 +02002303
2304 err = bpf_prog_new_fd(prog);
2305 if (err < 0)
2306 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002307 return err;
2308
2309free_used_maps:
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002310 /* In case we have subprogs, we need to wait for a grace
2311 * period before we can tear down JIT memory since symbols
2312 * are already exposed under kallsyms.
2313 */
2314 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2315 return err;
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002316free_prog_sec:
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002317 free_uid(prog->aux->user);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002318 security_bpf_prog_free(prog->aux);
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002319free_prog:
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08002320 if (prog->aux->attach_btf)
2321 btf_put(prog->aux->attach_btf);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002322 bpf_prog_free(prog);
2323 return err;
2324}
2325
Chenbo Feng6e71b042017-10-18 13:00:22 -07002326#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01002327
2328static int bpf_obj_pin(const union bpf_attr *attr)
2329{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002330 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002331 return -EINVAL;
2332
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002333 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01002334}
2335
2336static int bpf_obj_get(const union bpf_attr *attr)
2337{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002338 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2339 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002340 return -EINVAL;
2341
Chenbo Feng6e71b042017-10-18 13:00:22 -07002342 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2343 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01002344}
2345
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002346void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002347 const struct bpf_link_ops *ops, struct bpf_prog *prog)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002348{
2349 atomic64_set(&link->refcnt, 1);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002350 link->type = type;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002351 link->id = 0;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002352 link->ops = ops;
2353 link->prog = prog;
2354}
2355
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002356static void bpf_link_free_id(int id)
2357{
2358 if (!id)
2359 return;
2360
2361 spin_lock_bh(&link_idr_lock);
2362 idr_remove(&link_idr, id);
2363 spin_unlock_bh(&link_idr_lock);
2364}
2365
Andrii Nakryiko98868662020-03-12 17:21:28 -07002366/* Clean up bpf_link and corresponding anon_inode file and FD. After
2367 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002368 * anon_inode's release() call. This helper marksbpf_link as
2369 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2370 * is not decremented, it's the responsibility of a calling code that failed
2371 * to complete bpf_link initialization.
Andrii Nakryiko98868662020-03-12 17:21:28 -07002372 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002373void bpf_link_cleanup(struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002374{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002375 primer->link->prog = NULL;
2376 bpf_link_free_id(primer->id);
2377 fput(primer->file);
2378 put_unused_fd(primer->fd);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002379}
2380
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002381void bpf_link_inc(struct bpf_link *link)
2382{
2383 atomic64_inc(&link->refcnt);
2384}
2385
2386/* bpf_link_free is guaranteed to be called from process context */
2387static void bpf_link_free(struct bpf_link *link)
2388{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002389 bpf_link_free_id(link->id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002390 if (link->prog) {
2391 /* detach BPF program, clean up used resources */
2392 link->ops->release(link);
2393 bpf_prog_put(link->prog);
2394 }
2395 /* free bpf_link and its containing memory */
2396 link->ops->dealloc(link);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002397}
2398
2399static void bpf_link_put_deferred(struct work_struct *work)
2400{
2401 struct bpf_link *link = container_of(work, struct bpf_link, work);
2402
2403 bpf_link_free(link);
2404}
2405
2406/* bpf_link_put can be called from atomic context, but ensures that resources
2407 * are freed from process context
2408 */
2409void bpf_link_put(struct bpf_link *link)
2410{
2411 if (!atomic64_dec_and_test(&link->refcnt))
2412 return;
2413
Alexei Starovoitovf00f2f72020-09-23 19:10:38 -07002414 if (in_atomic()) {
2415 INIT_WORK(&link->work, bpf_link_put_deferred);
2416 schedule_work(&link->work);
2417 } else {
2418 bpf_link_free(link);
2419 }
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002420}
2421
2422static int bpf_link_release(struct inode *inode, struct file *filp)
2423{
2424 struct bpf_link *link = filp->private_data;
2425
2426 bpf_link_put(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002427 return 0;
2428}
2429
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002430#ifdef CONFIG_PROC_FS
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002431#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2432#define BPF_MAP_TYPE(_id, _ops)
2433#define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2434static const char *bpf_link_type_strs[] = {
2435 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2436#include <linux/bpf_types.h>
2437};
2438#undef BPF_PROG_TYPE
2439#undef BPF_MAP_TYPE
2440#undef BPF_LINK_TYPE
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002441
2442static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2443{
2444 const struct bpf_link *link = filp->private_data;
2445 const struct bpf_prog *prog = link->prog;
2446 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002447
2448 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2449 seq_printf(m,
2450 "link_type:\t%s\n"
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002451 "link_id:\t%u\n"
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002452 "prog_tag:\t%s\n"
2453 "prog_id:\t%u\n",
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002454 bpf_link_type_strs[link->type],
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002455 link->id,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002456 prog_tag,
2457 prog->aux->id);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002458 if (link->ops->show_fdinfo)
2459 link->ops->show_fdinfo(link, m);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002460}
2461#endif
2462
Zou Wei6f302bf2020-04-23 10:32:40 +08002463static const struct file_operations bpf_link_fops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002464#ifdef CONFIG_PROC_FS
2465 .show_fdinfo = bpf_link_show_fdinfo,
2466#endif
2467 .release = bpf_link_release,
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002468 .read = bpf_dummy_read,
2469 .write = bpf_dummy_write,
2470};
2471
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002472static int bpf_link_alloc_id(struct bpf_link *link)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002473{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002474 int id;
2475
2476 idr_preload(GFP_KERNEL);
2477 spin_lock_bh(&link_idr_lock);
2478 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2479 spin_unlock_bh(&link_idr_lock);
2480 idr_preload_end();
2481
2482 return id;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002483}
2484
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002485/* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2486 * reserving unused FD and allocating ID from link_idr. This is to be paired
2487 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2488 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2489 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2490 * transient state is passed around in struct bpf_link_primer.
2491 * This is preferred way to create and initialize bpf_link, especially when
2492 * there are complicated and expensive operations inbetween creating bpf_link
2493 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2494 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2495 * expensive (and potentially failing) roll back operations in a rare case
2496 * that file, FD, or ID can't be allocated.
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002497 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002498int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002499{
2500 struct file *file;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002501 int fd, id;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002502
2503 fd = get_unused_fd_flags(O_CLOEXEC);
2504 if (fd < 0)
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002505 return fd;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002506
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002507
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002508 id = bpf_link_alloc_id(link);
2509 if (id < 0) {
2510 put_unused_fd(fd);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002511 return id;
2512 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002513
2514 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2515 if (IS_ERR(file)) {
Andrii Nakryiko138c6762020-05-01 11:56:22 -07002516 bpf_link_free_id(id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002517 put_unused_fd(fd);
Andrii Nakryiko138c6762020-05-01 11:56:22 -07002518 return PTR_ERR(file);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002519 }
2520
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002521 primer->link = link;
2522 primer->file = file;
2523 primer->fd = fd;
2524 primer->id = id;
2525 return 0;
2526}
2527
2528int bpf_link_settle(struct bpf_link_primer *primer)
2529{
2530 /* make bpf_link fetchable by ID */
2531 spin_lock_bh(&link_idr_lock);
2532 primer->link->id = primer->id;
2533 spin_unlock_bh(&link_idr_lock);
2534 /* make bpf_link fetchable by FD */
2535 fd_install(primer->fd, primer->file);
2536 /* pass through installed FD */
2537 return primer->fd;
2538}
2539
2540int bpf_link_new_fd(struct bpf_link *link)
2541{
2542 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002543}
2544
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002545struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2546{
2547 struct fd f = fdget(ufd);
2548 struct bpf_link *link;
2549
2550 if (!f.file)
2551 return ERR_PTR(-EBADF);
2552 if (f.file->f_op != &bpf_link_fops) {
2553 fdput(f);
2554 return ERR_PTR(-EINVAL);
2555 }
2556
2557 link = f.file->private_data;
2558 bpf_link_inc(link);
2559 fdput(f);
2560
2561 return link;
2562}
2563
2564struct bpf_tracing_link {
2565 struct bpf_link link;
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002566 enum bpf_attach_type attach_type;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002567 struct bpf_trampoline *trampoline;
2568 struct bpf_prog *tgt_prog;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002569};
2570
2571static void bpf_tracing_link_release(struct bpf_link *link)
2572{
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002573 struct bpf_tracing_link *tr_link =
2574 container_of(link, struct bpf_tracing_link, link);
2575
2576 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2577 tr_link->trampoline));
2578
2579 bpf_trampoline_put(tr_link->trampoline);
2580
2581 /* tgt_prog is NULL if target is a kernel function */
2582 if (tr_link->tgt_prog)
2583 bpf_prog_put(tr_link->tgt_prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002584}
2585
2586static void bpf_tracing_link_dealloc(struct bpf_link *link)
2587{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002588 struct bpf_tracing_link *tr_link =
2589 container_of(link, struct bpf_tracing_link, link);
2590
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002591 kfree(tr_link);
2592}
2593
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002594static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2595 struct seq_file *seq)
2596{
2597 struct bpf_tracing_link *tr_link =
2598 container_of(link, struct bpf_tracing_link, link);
2599
2600 seq_printf(seq,
2601 "attach_type:\t%d\n",
2602 tr_link->attach_type);
2603}
2604
2605static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2606 struct bpf_link_info *info)
2607{
2608 struct bpf_tracing_link *tr_link =
2609 container_of(link, struct bpf_tracing_link, link);
2610
2611 info->tracing.attach_type = tr_link->attach_type;
Toke Høiland-Jørgensen441e8c62021-04-13 11:16:06 +02002612 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2613 &info->tracing.target_obj_id,
2614 &info->tracing.target_btf_id);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002615
2616 return 0;
2617}
2618
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002619static const struct bpf_link_ops bpf_tracing_link_lops = {
2620 .release = bpf_tracing_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002621 .dealloc = bpf_tracing_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002622 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2623 .fill_link_info = bpf_tracing_link_fill_link_info,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002624};
2625
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002626static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2627 int tgt_prog_fd,
2628 u32 btf_id)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002629{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002630 struct bpf_link_primer link_primer;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002631 struct bpf_prog *tgt_prog = NULL;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002632 struct bpf_trampoline *tr = NULL;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002633 struct bpf_tracing_link *link;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002634 u64 key = 0;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002635 int err;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002636
KP Singh9e4e01d2020-03-29 01:43:52 +01002637 switch (prog->type) {
2638 case BPF_PROG_TYPE_TRACING:
2639 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2640 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2641 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2642 err = -EINVAL;
2643 goto out_put_prog;
2644 }
2645 break;
2646 case BPF_PROG_TYPE_EXT:
2647 if (prog->expected_attach_type != 0) {
2648 err = -EINVAL;
2649 goto out_put_prog;
2650 }
2651 break;
2652 case BPF_PROG_TYPE_LSM:
2653 if (prog->expected_attach_type != BPF_LSM_MAC) {
2654 err = -EINVAL;
2655 goto out_put_prog;
2656 }
2657 break;
2658 default:
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002659 err = -EINVAL;
2660 goto out_put_prog;
2661 }
2662
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002663 if (!!tgt_prog_fd != !!btf_id) {
2664 err = -EINVAL;
2665 goto out_put_prog;
2666 }
2667
2668 if (tgt_prog_fd) {
2669 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2670 if (prog->type != BPF_PROG_TYPE_EXT) {
2671 err = -EINVAL;
2672 goto out_put_prog;
2673 }
2674
2675 tgt_prog = bpf_prog_get(tgt_prog_fd);
2676 if (IS_ERR(tgt_prog)) {
2677 err = PTR_ERR(tgt_prog);
2678 tgt_prog = NULL;
2679 goto out_put_prog;
2680 }
2681
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08002682 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002683 }
2684
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002685 link = kzalloc(sizeof(*link), GFP_USER);
2686 if (!link) {
2687 err = -ENOMEM;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002688 goto out_put_prog;
2689 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002690 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2691 &bpf_tracing_link_lops, prog);
2692 link->attach_type = prog->expected_attach_type;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002693
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002694 mutex_lock(&prog->aux->dst_mutex);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002695
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002696 /* There are a few possible cases here:
2697 *
2698 * - if prog->aux->dst_trampoline is set, the program was just loaded
2699 * and not yet attached to anything, so we can use the values stored
2700 * in prog->aux
2701 *
2702 * - if prog->aux->dst_trampoline is NULL, the program has already been
2703 * attached to a target and its initial target was cleared (below)
2704 *
2705 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2706 * target_btf_id using the link_create API.
2707 *
2708 * - if tgt_prog == NULL when this function was called using the old
Jiri Olsaf3a95072021-04-14 21:51:41 +02002709 * raw_tracepoint_open API, and we need a target from prog->aux
2710 *
2711 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
2712 * was detached and is going for re-attachment.
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002713 */
2714 if (!prog->aux->dst_trampoline && !tgt_prog) {
Jiri Olsaf3a95072021-04-14 21:51:41 +02002715 /*
2716 * Allow re-attach for TRACING and LSM programs. If it's
2717 * currently linked, bpf_trampoline_link_prog will fail.
2718 * EXT programs need to specify tgt_prog_fd, so they
2719 * re-attach in separate code path.
2720 */
2721 if (prog->type != BPF_PROG_TYPE_TRACING &&
2722 prog->type != BPF_PROG_TYPE_LSM) {
2723 err = -EINVAL;
2724 goto out_unlock;
2725 }
2726 btf_id = prog->aux->attach_btf_id;
2727 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002728 }
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002729
2730 if (!prog->aux->dst_trampoline ||
2731 (key && key != prog->aux->dst_trampoline->key)) {
2732 /* If there is no saved target, or the specified target is
2733 * different from the destination specified at load time, we
2734 * need a new trampoline and a check for compatibility
2735 */
2736 struct bpf_attach_target_info tgt_info = {};
2737
2738 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2739 &tgt_info);
2740 if (err)
2741 goto out_unlock;
2742
2743 tr = bpf_trampoline_get(key, &tgt_info);
2744 if (!tr) {
2745 err = -ENOMEM;
2746 goto out_unlock;
2747 }
2748 } else {
2749 /* The caller didn't specify a target, or the target was the
2750 * same as the destination supplied during program load. This
2751 * means we can reuse the trampoline and reference from program
2752 * load time, and there is no need to allocate a new one. This
2753 * can only happen once for any program, as the saved values in
2754 * prog->aux are cleared below.
2755 */
2756 tr = prog->aux->dst_trampoline;
2757 tgt_prog = prog->aux->dst_prog;
2758 }
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002759
2760 err = bpf_link_prime(&link->link, &link_primer);
2761 if (err)
2762 goto out_unlock;
2763
2764 err = bpf_trampoline_link_prog(prog, tr);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002765 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002766 bpf_link_cleanup(&link_primer);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002767 link = NULL;
2768 goto out_unlock;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002769 }
2770
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002771 link->tgt_prog = tgt_prog;
2772 link->trampoline = tr;
2773
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002774 /* Always clear the trampoline and target prog from prog->aux to make
2775 * sure the original attach destination is not kept alive after a
2776 * program is (re-)attached to another target.
2777 */
2778 if (prog->aux->dst_prog &&
2779 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2780 /* got extra prog ref from syscall, or attaching to different prog */
2781 bpf_prog_put(prog->aux->dst_prog);
2782 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2783 /* we allocated a new trampoline, so free the old one */
2784 bpf_trampoline_put(prog->aux->dst_trampoline);
2785
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002786 prog->aux->dst_prog = NULL;
2787 prog->aux->dst_trampoline = NULL;
2788 mutex_unlock(&prog->aux->dst_mutex);
2789
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002790 return bpf_link_settle(&link_primer);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002791out_unlock:
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002792 if (tr && tr != prog->aux->dst_trampoline)
2793 bpf_trampoline_put(tr);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002794 mutex_unlock(&prog->aux->dst_mutex);
2795 kfree(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002796out_put_prog:
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002797 if (tgt_prog_fd && tgt_prog)
2798 bpf_prog_put(tgt_prog);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002799 return err;
2800}
2801
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002802struct bpf_raw_tp_link {
2803 struct bpf_link link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002804 struct bpf_raw_event_map *btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002805};
2806
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002807static void bpf_raw_tp_link_release(struct bpf_link *link)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002808{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002809 struct bpf_raw_tp_link *raw_tp =
2810 container_of(link, struct bpf_raw_tp_link, link);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002811
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002812 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
Matt Mullinsa38d1102018-12-12 16:42:37 -08002813 bpf_put_raw_tracepoint(raw_tp->btp);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002814}
2815
2816static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2817{
2818 struct bpf_raw_tp_link *raw_tp =
2819 container_of(link, struct bpf_raw_tp_link, link);
2820
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002821 kfree(raw_tp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002822}
2823
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002824static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2825 struct seq_file *seq)
2826{
2827 struct bpf_raw_tp_link *raw_tp_link =
2828 container_of(link, struct bpf_raw_tp_link, link);
2829
2830 seq_printf(seq,
2831 "tp_name:\t%s\n",
2832 raw_tp_link->btp->tp->name);
2833}
2834
2835static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2836 struct bpf_link_info *info)
2837{
2838 struct bpf_raw_tp_link *raw_tp_link =
2839 container_of(link, struct bpf_raw_tp_link, link);
2840 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2841 const char *tp_name = raw_tp_link->btp->tp->name;
2842 u32 ulen = info->raw_tracepoint.tp_name_len;
2843 size_t tp_len = strlen(tp_name);
2844
Yonghong Songb4749592020-08-21 12:10:54 -07002845 if (!ulen ^ !ubuf)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002846 return -EINVAL;
2847
2848 info->raw_tracepoint.tp_name_len = tp_len + 1;
2849
2850 if (!ubuf)
2851 return 0;
2852
2853 if (ulen >= tp_len + 1) {
2854 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2855 return -EFAULT;
2856 } else {
2857 char zero = '\0';
2858
2859 if (copy_to_user(ubuf, tp_name, ulen - 1))
2860 return -EFAULT;
2861 if (put_user(zero, ubuf + ulen - 1))
2862 return -EFAULT;
2863 return -ENOSPC;
2864 }
2865
2866 return 0;
2867}
2868
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002869static const struct bpf_link_ops bpf_raw_tp_link_lops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002870 .release = bpf_raw_tp_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002871 .dealloc = bpf_raw_tp_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002872 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2873 .fill_link_info = bpf_raw_tp_link_fill_link_info,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002874};
2875
2876#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2877
2878static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2879{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002880 struct bpf_link_primer link_primer;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002881 struct bpf_raw_tp_link *link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002882 struct bpf_raw_event_map *btp;
2883 struct bpf_prog *prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002884 const char *tp_name;
2885 char buf[128];
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002886 int err;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002887
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002888 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2889 return -EINVAL;
2890
2891 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2892 if (IS_ERR(prog))
2893 return PTR_ERR(prog);
2894
KP Singh9e4e01d2020-03-29 01:43:52 +01002895 switch (prog->type) {
2896 case BPF_PROG_TYPE_TRACING:
2897 case BPF_PROG_TYPE_EXT:
2898 case BPF_PROG_TYPE_LSM:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002899 if (attr->raw_tracepoint.name) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002900 /* The attach point for this category of programs
2901 * should be specified via btf_id during program load.
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002902 */
2903 err = -EINVAL;
2904 goto out_put_prog;
2905 }
KP Singh9e4e01d2020-03-29 01:43:52 +01002906 if (prog->type == BPF_PROG_TYPE_TRACING &&
2907 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002908 tp_name = prog->aux->attach_func_name;
KP Singh9e4e01d2020-03-29 01:43:52 +01002909 break;
2910 }
Jiri Olsa55410752021-01-11 20:16:50 +01002911 err = bpf_tracing_prog_attach(prog, 0, 0);
2912 if (err >= 0)
2913 return err;
2914 goto out_put_prog;
KP Singh9e4e01d2020-03-29 01:43:52 +01002915 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2916 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002917 if (strncpy_from_user(buf,
2918 u64_to_user_ptr(attr->raw_tracepoint.name),
2919 sizeof(buf) - 1) < 0) {
2920 err = -EFAULT;
2921 goto out_put_prog;
2922 }
2923 buf[sizeof(buf) - 1] = 0;
2924 tp_name = buf;
KP Singh9e4e01d2020-03-29 01:43:52 +01002925 break;
2926 default:
2927 err = -EINVAL;
2928 goto out_put_prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002929 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002930
Matt Mullinsa38d1102018-12-12 16:42:37 -08002931 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002932 if (!btp) {
2933 err = -ENOENT;
2934 goto out_put_prog;
2935 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002936
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002937 link = kzalloc(sizeof(*link), GFP_USER);
2938 if (!link) {
Matt Mullinsa38d1102018-12-12 16:42:37 -08002939 err = -ENOMEM;
2940 goto out_put_btp;
2941 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002942 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2943 &bpf_raw_tp_link_lops, prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002944 link->btp = btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002945
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002946 err = bpf_link_prime(&link->link, &link_primer);
2947 if (err) {
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002948 kfree(link);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002949 goto out_put_btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002950 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002951
2952 err = bpf_probe_register(link->btp, prog);
2953 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002954 bpf_link_cleanup(&link_primer);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002955 goto out_put_btp;
2956 }
2957
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002958 return bpf_link_settle(&link_primer);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002959
Matt Mullinsa38d1102018-12-12 16:42:37 -08002960out_put_btp:
2961 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002962out_put_prog:
2963 bpf_prog_put(prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002964 return err;
2965}
2966
Anders Roxell33491582018-04-03 14:09:47 +02002967static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2968 enum bpf_attach_type attach_type)
2969{
2970 switch (prog->type) {
2971 case BPF_PROG_TYPE_CGROUP_SOCK:
2972 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002973 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02002974 case BPF_PROG_TYPE_SK_LOOKUP:
Anders Roxell33491582018-04-03 14:09:47 +02002975 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
brakmo5cf1e912019-05-28 16:59:36 -07002976 case BPF_PROG_TYPE_CGROUP_SKB:
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002977 if (!capable(CAP_NET_ADMIN))
2978 /* cg-skb progs can be loaded by unpriv user.
2979 * check permissions at attach time.
2980 */
2981 return -EPERM;
brakmo5cf1e912019-05-28 16:59:36 -07002982 return prog->enforce_expected_attach_type &&
2983 prog->expected_attach_type != attach_type ?
2984 -EINVAL : 0;
Anders Roxell33491582018-04-03 14:09:47 +02002985 default:
2986 return 0;
2987 }
2988}
2989
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002990static enum bpf_prog_type
2991attach_type_to_prog_type(enum bpf_attach_type attach_type)
2992{
2993 switch (attach_type) {
2994 case BPF_CGROUP_INET_INGRESS:
2995 case BPF_CGROUP_INET_EGRESS:
2996 return BPF_PROG_TYPE_CGROUP_SKB;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002997 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07002998 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002999 case BPF_CGROUP_INET4_POST_BIND:
3000 case BPF_CGROUP_INET6_POST_BIND:
3001 return BPF_PROG_TYPE_CGROUP_SOCK;
3002 case BPF_CGROUP_INET4_BIND:
3003 case BPF_CGROUP_INET6_BIND:
3004 case BPF_CGROUP_INET4_CONNECT:
3005 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02003006 case BPF_CGROUP_INET4_GETPEERNAME:
3007 case BPF_CGROUP_INET6_GETPEERNAME:
3008 case BPF_CGROUP_INET4_GETSOCKNAME:
3009 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003010 case BPF_CGROUP_UDP4_SENDMSG:
3011 case BPF_CGROUP_UDP6_SENDMSG:
3012 case BPF_CGROUP_UDP4_RECVMSG:
3013 case BPF_CGROUP_UDP6_RECVMSG:
3014 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3015 case BPF_CGROUP_SOCK_OPS:
3016 return BPF_PROG_TYPE_SOCK_OPS;
3017 case BPF_CGROUP_DEVICE:
3018 return BPF_PROG_TYPE_CGROUP_DEVICE;
3019 case BPF_SK_MSG_VERDICT:
3020 return BPF_PROG_TYPE_SK_MSG;
3021 case BPF_SK_SKB_STREAM_PARSER:
3022 case BPF_SK_SKB_STREAM_VERDICT:
Cong Wanga7ba4552021-03-30 19:32:30 -07003023 case BPF_SK_SKB_VERDICT:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003024 return BPF_PROG_TYPE_SK_SKB;
3025 case BPF_LIRC_MODE2:
3026 return BPF_PROG_TYPE_LIRC_MODE2;
3027 case BPF_FLOW_DISSECTOR:
3028 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3029 case BPF_CGROUP_SYSCTL:
3030 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3031 case BPF_CGROUP_GETSOCKOPT:
3032 case BPF_CGROUP_SETSOCKOPT:
3033 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
Yonghong Songde4e05c2020-05-09 10:59:01 -07003034 case BPF_TRACE_ITER:
3035 return BPF_PROG_TYPE_TRACING;
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02003036 case BPF_SK_LOOKUP:
3037 return BPF_PROG_TYPE_SK_LOOKUP;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07003038 case BPF_XDP:
3039 return BPF_PROG_TYPE_XDP;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003040 default:
3041 return BPF_PROG_TYPE_UNSPEC;
3042 }
3043}
3044
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08003045#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
John Fastabend174a79f2017-08-15 22:32:47 -07003046
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003047#define BPF_F_ATTACH_MASK \
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08003048 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003049
Daniel Mackf4324552016-11-23 16:52:27 +01003050static int bpf_prog_attach(const union bpf_attr *attr)
3051{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003052 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01003053 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003054 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01003055
Daniel Mackf4324552016-11-23 16:52:27 +01003056 if (CHECK_ATTR(BPF_PROG_ATTACH))
3057 return -EINVAL;
3058
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003059 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003060 return -EINVAL;
3061
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003062 ptype = attach_type_to_prog_type(attr->attach_type);
3063 if (ptype == BPF_PROG_TYPE_UNSPEC)
Daniel Mackf4324552016-11-23 16:52:27 +01003064 return -EINVAL;
Daniel Mackf4324552016-11-23 16:52:27 +01003065
David Ahernb2cd1252016-12-01 08:48:03 -08003066 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3067 if (IS_ERR(prog))
3068 return PTR_ERR(prog);
3069
Andrey Ignatov5e43f892018-03-30 15:08:00 -07003070 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3071 bpf_prog_put(prog);
3072 return -EINVAL;
3073 }
3074
Sean Youngfdb5c452018-06-19 00:04:24 +01003075 switch (ptype) {
3076 case BPF_PROG_TYPE_SK_SKB:
3077 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02003078 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01003079 break;
3080 case BPF_PROG_TYPE_LIRC_MODE2:
3081 ret = lirc_prog_attach(attr, prog);
3082 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07003083 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +02003084 ret = netns_bpf_prog_attach(attr, prog);
Petar Penkovd58e4682018-09-14 07:46:18 -07003085 break;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003086 case BPF_PROG_TYPE_CGROUP_DEVICE:
3087 case BPF_PROG_TYPE_CGROUP_SKB:
3088 case BPF_PROG_TYPE_CGROUP_SOCK:
3089 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3090 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3091 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3092 case BPF_PROG_TYPE_SOCK_OPS:
Sean Youngfdb5c452018-06-19 00:04:24 +01003093 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003094 break;
3095 default:
3096 ret = -EINVAL;
David Ahernb2cd1252016-12-01 08:48:03 -08003097 }
3098
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003099 if (ret)
3100 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003101 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01003102}
3103
3104#define BPF_PROG_DETACH_LAST_FIELD attach_type
3105
3106static int bpf_prog_detach(const union bpf_attr *attr)
3107{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003108 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01003109
Daniel Mackf4324552016-11-23 16:52:27 +01003110 if (CHECK_ATTR(BPF_PROG_DETACH))
3111 return -EINVAL;
3112
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003113 ptype = attach_type_to_prog_type(attr->attach_type);
3114
3115 switch (ptype) {
3116 case BPF_PROG_TYPE_SK_MSG:
3117 case BPF_PROG_TYPE_SK_SKB:
Lorenz Bauerbb0de312020-06-29 10:56:28 +01003118 return sock_map_prog_detach(attr, ptype);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003119 case BPF_PROG_TYPE_LIRC_MODE2:
Sean Youngf4364dc2018-05-27 12:24:09 +01003120 return lirc_prog_detach(attr);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003121 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Lorenz Bauer4ac2add2020-06-29 10:56:26 +01003122 return netns_bpf_prog_detach(attr, ptype);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003123 case BPF_PROG_TYPE_CGROUP_DEVICE:
3124 case BPF_PROG_TYPE_CGROUP_SKB:
3125 case BPF_PROG_TYPE_CGROUP_SOCK:
3126 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3127 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3128 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3129 case BPF_PROG_TYPE_SOCK_OPS:
3130 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01003131 default:
3132 return -EINVAL;
3133 }
Daniel Mackf4324552016-11-23 16:52:27 +01003134}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07003135
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003136#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3137
3138static int bpf_prog_query(const union bpf_attr *attr,
3139 union bpf_attr __user *uattr)
3140{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003141 if (!capable(CAP_NET_ADMIN))
3142 return -EPERM;
3143 if (CHECK_ATTR(BPF_PROG_QUERY))
3144 return -EINVAL;
3145 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3146 return -EINVAL;
3147
3148 switch (attr->query.attach_type) {
3149 case BPF_CGROUP_INET_INGRESS:
3150 case BPF_CGROUP_INET_EGRESS:
3151 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07003152 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07003153 case BPF_CGROUP_INET4_BIND:
3154 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07003155 case BPF_CGROUP_INET4_POST_BIND:
3156 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07003157 case BPF_CGROUP_INET4_CONNECT:
3158 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02003159 case BPF_CGROUP_INET4_GETPEERNAME:
3160 case BPF_CGROUP_INET6_GETPEERNAME:
3161 case BPF_CGROUP_INET4_GETSOCKNAME:
3162 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07003163 case BPF_CGROUP_UDP4_SENDMSG:
3164 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02003165 case BPF_CGROUP_UDP4_RECVMSG:
3166 case BPF_CGROUP_UDP6_RECVMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003167 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05003168 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08003169 case BPF_CGROUP_SYSCTL:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07003170 case BPF_CGROUP_GETSOCKOPT:
3171 case BPF_CGROUP_SETSOCKOPT:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003172 return cgroup_bpf_prog_query(attr, uattr);
Sean Youngf4364dc2018-05-27 12:24:09 +01003173 case BPF_LIRC_MODE2:
3174 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07003175 case BPF_FLOW_DISSECTOR:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02003176 case BPF_SK_LOOKUP:
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +02003177 return netns_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003178 default:
3179 return -EINVAL;
3180 }
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003181}
Daniel Mackf4324552016-11-23 16:52:27 +01003182
Song Liu1b4d60e2020-09-25 13:54:29 -07003183#define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003184
3185static int bpf_prog_test_run(const union bpf_attr *attr,
3186 union bpf_attr __user *uattr)
3187{
3188 struct bpf_prog *prog;
3189 int ret = -ENOTSUPP;
3190
3191 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3192 return -EINVAL;
3193
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07003194 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3195 (!attr->test.ctx_size_in && attr->test.ctx_in))
3196 return -EINVAL;
3197
3198 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3199 (!attr->test.ctx_size_out && attr->test.ctx_out))
3200 return -EINVAL;
3201
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003202 prog = bpf_prog_get(attr->test.prog_fd);
3203 if (IS_ERR(prog))
3204 return PTR_ERR(prog);
3205
3206 if (prog->aux->ops->test_run)
3207 ret = prog->aux->ops->test_run(prog, attr, uattr);
3208
3209 bpf_prog_put(prog);
3210 return ret;
3211}
3212
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07003213#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3214
3215static int bpf_obj_get_next_id(const union bpf_attr *attr,
3216 union bpf_attr __user *uattr,
3217 struct idr *idr,
3218 spinlock_t *lock)
3219{
3220 u32 next_id = attr->start_id;
3221 int err = 0;
3222
3223 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3224 return -EINVAL;
3225
3226 if (!capable(CAP_SYS_ADMIN))
3227 return -EPERM;
3228
3229 next_id++;
3230 spin_lock_bh(lock);
3231 if (!idr_get_next(idr, &next_id))
3232 err = -ENOENT;
3233 spin_unlock_bh(lock);
3234
3235 if (!err)
3236 err = put_user(next_id, &uattr->next_id);
3237
3238 return err;
3239}
3240
Yonghong Song6086d292020-05-09 10:59:09 -07003241struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3242{
3243 struct bpf_map *map;
3244
3245 spin_lock_bh(&map_idr_lock);
3246again:
3247 map = idr_get_next(&map_idr, id);
3248 if (map) {
3249 map = __bpf_map_inc_not_zero(map, false);
3250 if (IS_ERR(map)) {
3251 (*id)++;
3252 goto again;
3253 }
3254 }
3255 spin_unlock_bh(&map_idr_lock);
3256
3257 return map;
3258}
3259
Alexei Starovoitova228a642020-07-01 18:10:18 -07003260struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3261{
3262 struct bpf_prog *prog;
3263
3264 spin_lock_bh(&prog_idr_lock);
3265again:
3266 prog = idr_get_next(&prog_idr, id);
3267 if (prog) {
3268 prog = bpf_prog_inc_not_zero(prog);
3269 if (IS_ERR(prog)) {
3270 (*id)++;
3271 goto again;
3272 }
3273 }
3274 spin_unlock_bh(&prog_idr_lock);
3275
3276 return prog;
3277}
3278
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003279#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3280
Björn Töpel7e6897f2019-12-13 18:51:09 +01003281struct bpf_prog *bpf_prog_by_id(u32 id)
3282{
3283 struct bpf_prog *prog;
3284
3285 if (!id)
3286 return ERR_PTR(-ENOENT);
3287
3288 spin_lock_bh(&prog_idr_lock);
3289 prog = idr_find(&prog_idr, id);
3290 if (prog)
3291 prog = bpf_prog_inc_not_zero(prog);
3292 else
3293 prog = ERR_PTR(-ENOENT);
3294 spin_unlock_bh(&prog_idr_lock);
3295 return prog;
3296}
3297
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003298static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3299{
3300 struct bpf_prog *prog;
3301 u32 id = attr->prog_id;
3302 int fd;
3303
3304 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3305 return -EINVAL;
3306
3307 if (!capable(CAP_SYS_ADMIN))
3308 return -EPERM;
3309
Björn Töpel7e6897f2019-12-13 18:51:09 +01003310 prog = bpf_prog_by_id(id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003311 if (IS_ERR(prog))
3312 return PTR_ERR(prog);
3313
3314 fd = bpf_prog_new_fd(prog);
3315 if (fd < 0)
3316 bpf_prog_put(prog);
3317
3318 return fd;
3319}
3320
Chenbo Feng6e71b042017-10-18 13:00:22 -07003321#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003322
3323static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3324{
3325 struct bpf_map *map;
3326 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07003327 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003328 int fd;
3329
Chenbo Feng6e71b042017-10-18 13:00:22 -07003330 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3331 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003332 return -EINVAL;
3333
3334 if (!capable(CAP_SYS_ADMIN))
3335 return -EPERM;
3336
Chenbo Feng6e71b042017-10-18 13:00:22 -07003337 f_flags = bpf_get_file_flag(attr->open_flags);
3338 if (f_flags < 0)
3339 return f_flags;
3340
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003341 spin_lock_bh(&map_idr_lock);
3342 map = idr_find(&map_idr, id);
3343 if (map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -07003344 map = __bpf_map_inc_not_zero(map, true);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003345 else
3346 map = ERR_PTR(-ENOENT);
3347 spin_unlock_bh(&map_idr_lock);
3348
3349 if (IS_ERR(map))
3350 return PTR_ERR(map);
3351
Chenbo Feng6e71b042017-10-18 13:00:22 -07003352 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003353 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08003354 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003355
3356 return fd;
3357}
3358
Daniel Borkmann7105e822017-12-20 13:42:57 +01003359static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003360 unsigned long addr, u32 *off,
3361 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01003362{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003363 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003364 int i;
3365
YiFei Zhu984fe942020-09-15 16:45:39 -07003366 mutex_lock(&prog->aux->used_maps_mutex);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003367 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3368 map = prog->aux->used_maps[i];
3369 if (map == (void *)addr) {
3370 *type = BPF_PSEUDO_MAP_FD;
YiFei Zhu984fe942020-09-15 16:45:39 -07003371 goto out;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003372 }
3373 if (!map->ops->map_direct_value_meta)
3374 continue;
3375 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3376 *type = BPF_PSEUDO_MAP_VALUE;
YiFei Zhu984fe942020-09-15 16:45:39 -07003377 goto out;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003378 }
3379 }
YiFei Zhu984fe942020-09-15 16:45:39 -07003380 map = NULL;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003381
YiFei Zhu984fe942020-09-15 16:45:39 -07003382out:
3383 mutex_unlock(&prog->aux->used_maps_mutex);
3384 return map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003385}
3386
Kees Cook63960262020-07-02 15:45:23 -07003387static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3388 const struct cred *f_cred)
Daniel Borkmann7105e822017-12-20 13:42:57 +01003389{
3390 const struct bpf_map *map;
3391 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003392 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003393 u64 imm;
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003394 u8 code;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003395 int i;
3396
3397 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3398 GFP_USER);
3399 if (!insns)
3400 return insns;
3401
3402 for (i = 0; i < prog->len; i++) {
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003403 code = insns[i].code;
3404
3405 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003406 insns[i].code = BPF_JMP | BPF_CALL;
3407 insns[i].imm = BPF_FUNC_tail_call;
3408 /* fall-through */
3409 }
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003410 if (code == (BPF_JMP | BPF_CALL) ||
3411 code == (BPF_JMP | BPF_CALL_ARGS)) {
3412 if (code == (BPF_JMP | BPF_CALL_ARGS))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003413 insns[i].code = BPF_JMP | BPF_CALL;
Kees Cook63960262020-07-02 15:45:23 -07003414 if (!bpf_dump_raw_ok(f_cred))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003415 insns[i].imm = 0;
3416 continue;
3417 }
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003418 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3419 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3420 continue;
3421 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01003422
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003423 if (code != (BPF_LD | BPF_IMM | BPF_DW))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003424 continue;
3425
3426 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003427 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003428 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003429 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003430 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003431 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003432 continue;
3433 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01003434 }
3435
3436 return insns;
3437}
3438
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003439static int set_info_rec_size(struct bpf_prog_info *info)
3440{
3441 /*
3442 * Ensure info.*_rec_size is the same as kernel expected size
3443 *
3444 * or
3445 *
3446 * Only allow zero *_rec_size if both _rec_size and _cnt are
3447 * zero. In this case, the kernel will set the expected
3448 * _rec_size back to the info.
3449 */
3450
Yonghong Song11d8b822018-12-10 14:14:08 -08003451 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003452 info->func_info_rec_size != sizeof(struct bpf_func_info))
3453 return -EINVAL;
3454
Yonghong Song11d8b822018-12-10 14:14:08 -08003455 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003456 info->line_info_rec_size != sizeof(struct bpf_line_info))
3457 return -EINVAL;
3458
Yonghong Song11d8b822018-12-10 14:14:08 -08003459 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003460 info->jited_line_info_rec_size != sizeof(__u64))
3461 return -EINVAL;
3462
3463 info->func_info_rec_size = sizeof(struct bpf_func_info);
3464 info->line_info_rec_size = sizeof(struct bpf_line_info);
3465 info->jited_line_info_rec_size = sizeof(__u64);
3466
3467 return 0;
3468}
3469
Kees Cook63960262020-07-02 15:45:23 -07003470static int bpf_prog_get_info_by_fd(struct file *file,
3471 struct bpf_prog *prog,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003472 const union bpf_attr *attr,
3473 union bpf_attr __user *uattr)
3474{
3475 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003476 struct bpf_prog_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003477 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003478 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003479 char __user *uinsns;
3480 u32 ulen;
3481 int err;
3482
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07003483 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003484 if (err)
3485 return err;
3486 info_len = min_t(u32, sizeof(info), info_len);
3487
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003488 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003489 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02003490 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003491
3492 info.type = prog->type;
3493 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003494 info.load_time = prog->aux->load_time;
3495 info.created_by_uid = from_kuid_munged(current_user_ns(),
3496 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02003497 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003498
3499 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003500 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3501
YiFei Zhu984fe942020-09-15 16:45:39 -07003502 mutex_lock(&prog->aux->used_maps_mutex);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003503 ulen = info.nr_map_ids;
3504 info.nr_map_ids = prog->aux->used_map_cnt;
3505 ulen = min_t(u32, info.nr_map_ids, ulen);
3506 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07003507 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003508 u32 i;
3509
3510 for (i = 0; i < ulen; i++)
3511 if (put_user(prog->aux->used_maps[i]->id,
YiFei Zhu984fe942020-09-15 16:45:39 -07003512 &user_map_ids[i])) {
3513 mutex_unlock(&prog->aux->used_maps_mutex);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003514 return -EFAULT;
YiFei Zhu984fe942020-09-15 16:45:39 -07003515 }
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003516 }
YiFei Zhu984fe942020-09-15 16:45:39 -07003517 mutex_unlock(&prog->aux->used_maps_mutex);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003518
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003519 err = set_info_rec_size(&info);
3520 if (err)
3521 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08003522
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003523 bpf_prog_get_stats(prog, &stats);
3524 info.run_time_ns = stats.nsecs;
3525 info.run_cnt = stats.cnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08003526 info.recursion_misses = stats.misses;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003527
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003528 if (!bpf_capable()) {
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003529 info.jited_prog_len = 0;
3530 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303531 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01003532 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08003533 info.nr_func_info = 0;
3534 info.nr_line_info = 0;
3535 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003536 goto done;
3537 }
3538
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003539 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02003540 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003541 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003542 struct bpf_insn *insns_sanitized;
3543 bool fault;
3544
Kees Cook63960262020-07-02 15:45:23 -07003545 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003546 info.xlated_prog_insns = 0;
3547 goto done;
3548 }
Kees Cook63960262020-07-02 15:45:23 -07003549 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003550 if (!insns_sanitized)
3551 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003552 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3553 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003554 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3555 kfree(insns_sanitized);
3556 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003557 return -EFAULT;
3558 }
3559
Jakub Kicinski675fc272017-12-27 18:39:09 -08003560 if (bpf_prog_is_dev_bound(prog->aux)) {
3561 err = bpf_prog_offload_info_fill(&info, prog);
3562 if (err)
3563 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08003564 goto done;
3565 }
3566
3567 /* NOTE: the following code is supposed to be skipped for offload.
3568 * bpf_prog_offload_info_fill() is the place to fill similar fields
3569 * for offload.
3570 */
3571 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05303572 if (prog->aux->func_cnt) {
3573 u32 i;
3574
3575 info.jited_prog_len = 0;
3576 for (i = 0; i < prog->aux->func_cnt; i++)
3577 info.jited_prog_len += prog->aux->func[i]->jited_len;
3578 } else {
3579 info.jited_prog_len = prog->jited_len;
3580 }
3581
Jiong Wangfcfb1262018-01-16 16:05:19 -08003582 if (info.jited_prog_len && ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003583 if (bpf_dump_raw_ok(file->f_cred)) {
Jiong Wangfcfb1262018-01-16 16:05:19 -08003584 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3585 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05303586
3587 /* for multi-function programs, copy the JITed
3588 * instructions for all the functions
3589 */
3590 if (prog->aux->func_cnt) {
3591 u32 len, free, i;
3592 u8 *img;
3593
3594 free = ulen;
3595 for (i = 0; i < prog->aux->func_cnt; i++) {
3596 len = prog->aux->func[i]->jited_len;
3597 len = min_t(u32, len, free);
3598 img = (u8 *) prog->aux->func[i]->bpf_func;
3599 if (copy_to_user(uinsns, img, len))
3600 return -EFAULT;
3601 uinsns += len;
3602 free -= len;
3603 if (!free)
3604 break;
3605 }
3606 } else {
3607 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3608 return -EFAULT;
3609 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08003610 } else {
3611 info.jited_prog_insns = 0;
3612 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08003613 }
3614
Sandipan Dasdbecd732018-05-24 12:26:48 +05303615 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07003616 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003617 if (ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003618 if (bpf_dump_raw_ok(file->f_cred)) {
Song Liuff1889f2018-11-02 10:16:17 -07003619 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303620 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303621 u32 i;
3622
3623 /* copy the address of the kernel symbol
3624 * corresponding to each function
3625 */
3626 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3627 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07003628 if (prog->aux->func_cnt) {
3629 for (i = 0; i < ulen; i++) {
3630 ksym_addr = (unsigned long)
3631 prog->aux->func[i]->bpf_func;
3632 if (put_user((u64) ksym_addr,
3633 &user_ksyms[i]))
3634 return -EFAULT;
3635 }
3636 } else {
3637 ksym_addr = (unsigned long) prog->bpf_func;
3638 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05303639 return -EFAULT;
3640 }
3641 } else {
3642 info.jited_ksyms = 0;
3643 }
3644 }
3645
Sandipan Das815581c2018-05-24 12:26:52 +05303646 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07003647 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003648 if (ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003649 if (bpf_dump_raw_ok(file->f_cred)) {
Sandipan Das815581c2018-05-24 12:26:52 +05303650 u32 __user *user_lens;
3651 u32 func_len, i;
3652
3653 /* copy the JITed image lengths for each function */
3654 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3655 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07003656 if (prog->aux->func_cnt) {
3657 for (i = 0; i < ulen; i++) {
3658 func_len =
3659 prog->aux->func[i]->jited_len;
3660 if (put_user(func_len, &user_lens[i]))
3661 return -EFAULT;
3662 }
3663 } else {
3664 func_len = prog->jited_len;
3665 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05303666 return -EFAULT;
3667 }
3668 } else {
3669 info.jited_func_lens = 0;
3670 }
3671 }
3672
Martin KaFai Lau73372242018-12-05 17:35:43 -08003673 if (prog->aux->btf)
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08003674 info.btf_id = btf_obj_id(prog->aux->btf);
Yonghong Song838e9692018-11-19 15:29:11 -08003675
Yonghong Song11d8b822018-12-10 14:14:08 -08003676 ulen = info.nr_func_info;
3677 info.nr_func_info = prog->aux->func_info_cnt;
3678 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003679 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08003680
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003681 user_finfo = u64_to_user_ptr(info.func_info);
3682 ulen = min_t(u32, info.nr_func_info, ulen);
3683 if (copy_to_user(user_finfo, prog->aux->func_info,
3684 info.func_info_rec_size * ulen))
3685 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08003686 }
3687
Yonghong Song11d8b822018-12-10 14:14:08 -08003688 ulen = info.nr_line_info;
3689 info.nr_line_info = prog->aux->nr_linfo;
3690 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003691 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003692
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003693 user_linfo = u64_to_user_ptr(info.line_info);
3694 ulen = min_t(u32, info.nr_line_info, ulen);
3695 if (copy_to_user(user_linfo, prog->aux->linfo,
3696 info.line_info_rec_size * ulen))
3697 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003698 }
3699
Yonghong Song11d8b822018-12-10 14:14:08 -08003700 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003701 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08003702 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003703 else
Yonghong Song11d8b822018-12-10 14:14:08 -08003704 info.nr_jited_line_info = 0;
3705 if (info.nr_jited_line_info && ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003706 if (bpf_dump_raw_ok(file->f_cred)) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003707 __u64 __user *user_linfo;
3708 u32 i;
3709
3710 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08003711 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003712 for (i = 0; i < ulen; i++) {
3713 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3714 &user_linfo[i]))
3715 return -EFAULT;
3716 }
3717 } else {
3718 info.jited_line_info = 0;
3719 }
3720 }
3721
Song Liuc872bdb2018-12-12 09:37:46 -08003722 ulen = info.nr_prog_tags;
3723 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3724 if (ulen) {
3725 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3726 u32 i;
3727
3728 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3729 ulen = min_t(u32, info.nr_prog_tags, ulen);
3730 if (prog->aux->func_cnt) {
3731 for (i = 0; i < ulen; i++) {
3732 if (copy_to_user(user_prog_tags[i],
3733 prog->aux->func[i]->tag,
3734 BPF_TAG_SIZE))
3735 return -EFAULT;
3736 }
3737 } else {
3738 if (copy_to_user(user_prog_tags[0],
3739 prog->tag, BPF_TAG_SIZE))
3740 return -EFAULT;
3741 }
3742 }
3743
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003744done:
3745 if (copy_to_user(uinfo, &info, info_len) ||
3746 put_user(info_len, &uattr->info.info_len))
3747 return -EFAULT;
3748
3749 return 0;
3750}
3751
Kees Cook63960262020-07-02 15:45:23 -07003752static int bpf_map_get_info_by_fd(struct file *file,
3753 struct bpf_map *map,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003754 const union bpf_attr *attr,
3755 union bpf_attr __user *uattr)
3756{
3757 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003758 struct bpf_map_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003759 u32 info_len = attr->info.info_len;
3760 int err;
3761
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07003762 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003763 if (err)
3764 return err;
3765 info_len = min_t(u32, sizeof(info), info_len);
3766
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003767 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003768 info.type = map->map_type;
3769 info.id = map->id;
3770 info.key_size = map->key_size;
3771 info.value_size = map->value_size;
3772 info.max_entries = map->max_entries;
3773 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07003774 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003775
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003776 if (map->btf) {
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08003777 info.btf_id = btf_obj_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07003778 info.btf_key_type_id = map->btf_key_type_id;
3779 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003780 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08003781 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003782
Jakub Kicinski52775b32018-01-17 19:13:28 -08003783 if (bpf_map_is_dev_bound(map)) {
3784 err = bpf_map_offload_info_fill(&info, map);
3785 if (err)
3786 return err;
3787 }
3788
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003789 if (copy_to_user(uinfo, &info, info_len) ||
3790 put_user(info_len, &uattr->info.info_len))
3791 return -EFAULT;
3792
3793 return 0;
3794}
3795
Kees Cook63960262020-07-02 15:45:23 -07003796static int bpf_btf_get_info_by_fd(struct file *file,
3797 struct btf *btf,
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003798 const union bpf_attr *attr,
3799 union bpf_attr __user *uattr)
3800{
3801 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3802 u32 info_len = attr->info.info_len;
3803 int err;
3804
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07003805 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003806 if (err)
3807 return err;
3808
3809 return btf_get_info_by_fd(btf, attr, uattr);
3810}
3811
Kees Cook63960262020-07-02 15:45:23 -07003812static int bpf_link_get_info_by_fd(struct file *file,
3813 struct bpf_link *link,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003814 const union bpf_attr *attr,
3815 union bpf_attr __user *uattr)
3816{
3817 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3818 struct bpf_link_info info;
3819 u32 info_len = attr->info.info_len;
3820 int err;
3821
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07003822 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003823 if (err)
3824 return err;
3825 info_len = min_t(u32, sizeof(info), info_len);
3826
3827 memset(&info, 0, sizeof(info));
3828 if (copy_from_user(&info, uinfo, info_len))
3829 return -EFAULT;
3830
3831 info.type = link->type;
3832 info.id = link->id;
3833 info.prog_id = link->prog->aux->id;
3834
3835 if (link->ops->fill_link_info) {
3836 err = link->ops->fill_link_info(link, &info);
3837 if (err)
3838 return err;
3839 }
3840
3841 if (copy_to_user(uinfo, &info, info_len) ||
3842 put_user(info_len, &uattr->info.info_len))
3843 return -EFAULT;
3844
3845 return 0;
3846}
3847
3848
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003849#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3850
3851static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3852 union bpf_attr __user *uattr)
3853{
3854 int ufd = attr->info.bpf_fd;
3855 struct fd f;
3856 int err;
3857
3858 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3859 return -EINVAL;
3860
3861 f = fdget(ufd);
3862 if (!f.file)
3863 return -EBADFD;
3864
3865 if (f.file->f_op == &bpf_prog_fops)
Kees Cook63960262020-07-02 15:45:23 -07003866 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003867 uattr);
3868 else if (f.file->f_op == &bpf_map_fops)
Kees Cook63960262020-07-02 15:45:23 -07003869 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003870 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07003871 else if (f.file->f_op == &btf_fops)
Kees Cook63960262020-07-02 15:45:23 -07003872 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003873 else if (f.file->f_op == &bpf_link_fops)
Kees Cook63960262020-07-02 15:45:23 -07003874 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003875 attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003876 else
3877 err = -EINVAL;
3878
3879 fdput(f);
3880 return err;
3881}
3882
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003883#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3884
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07003885static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003886{
3887 if (CHECK_ATTR(BPF_BTF_LOAD))
3888 return -EINVAL;
3889
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003890 if (!bpf_capable())
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003891 return -EPERM;
3892
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07003893 return btf_new_fd(attr, uattr);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003894}
3895
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003896#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3897
3898static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3899{
3900 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3901 return -EINVAL;
3902
3903 if (!capable(CAP_SYS_ADMIN))
3904 return -EPERM;
3905
3906 return btf_get_fd_by_id(attr->btf_id);
3907}
3908
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003909static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3910 union bpf_attr __user *uattr,
3911 u32 prog_id, u32 fd_type,
3912 const char *buf, u64 probe_offset,
3913 u64 probe_addr)
3914{
3915 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3916 u32 len = buf ? strlen(buf) : 0, input_len;
3917 int err = 0;
3918
3919 if (put_user(len, &uattr->task_fd_query.buf_len))
3920 return -EFAULT;
3921 input_len = attr->task_fd_query.buf_len;
3922 if (input_len && ubuf) {
3923 if (!len) {
3924 /* nothing to copy, just make ubuf NULL terminated */
3925 char zero = '\0';
3926
3927 if (put_user(zero, ubuf))
3928 return -EFAULT;
3929 } else if (input_len >= len + 1) {
3930 /* ubuf can hold the string with NULL terminator */
3931 if (copy_to_user(ubuf, buf, len + 1))
3932 return -EFAULT;
3933 } else {
3934 /* ubuf cannot hold the string with NULL terminator,
3935 * do a partial copy with NULL terminator.
3936 */
3937 char zero = '\0';
3938
3939 err = -ENOSPC;
3940 if (copy_to_user(ubuf, buf, input_len - 1))
3941 return -EFAULT;
3942 if (put_user(zero, ubuf + input_len - 1))
3943 return -EFAULT;
3944 }
3945 }
3946
3947 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3948 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3949 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3950 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3951 return -EFAULT;
3952
3953 return err;
3954}
3955
3956#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3957
3958static int bpf_task_fd_query(const union bpf_attr *attr,
3959 union bpf_attr __user *uattr)
3960{
3961 pid_t pid = attr->task_fd_query.pid;
3962 u32 fd = attr->task_fd_query.fd;
3963 const struct perf_event *event;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003964 struct task_struct *task;
3965 struct file *file;
3966 int err;
3967
3968 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3969 return -EINVAL;
3970
3971 if (!capable(CAP_SYS_ADMIN))
3972 return -EPERM;
3973
3974 if (attr->task_fd_query.flags != 0)
3975 return -EINVAL;
3976
3977 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3978 if (!task)
3979 return -ENOENT;
3980
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003981 err = 0;
Eric W. Biedermanb48845a2020-11-20 17:14:22 -06003982 file = fget_task(task, fd);
3983 put_task_struct(task);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003984 if (!file)
Eric W. Biedermanb48845a2020-11-20 17:14:22 -06003985 return -EBADF;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003986
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003987 if (file->f_op == &bpf_link_fops) {
3988 struct bpf_link *link = file->private_data;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003989
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003990 if (link->ops == &bpf_raw_tp_link_lops) {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003991 struct bpf_raw_tp_link *raw_tp =
3992 container_of(link, struct bpf_raw_tp_link, link);
3993 struct bpf_raw_event_map *btp = raw_tp->btp;
3994
3995 err = bpf_task_fd_query_copy(attr, uattr,
3996 raw_tp->link.prog->aux->id,
3997 BPF_FD_TYPE_RAW_TRACEPOINT,
3998 btp->tp->name, 0, 0);
3999 goto put_file;
4000 }
4001 goto out_not_supp;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004002 }
4003
4004 event = perf_get_event(file);
4005 if (!IS_ERR(event)) {
4006 u64 probe_offset, probe_addr;
4007 u32 prog_id, fd_type;
4008 const char *buf;
4009
4010 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4011 &buf, &probe_offset,
4012 &probe_addr);
4013 if (!err)
4014 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4015 fd_type, buf,
4016 probe_offset,
4017 probe_addr);
4018 goto put_file;
4019 }
4020
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08004021out_not_supp:
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004022 err = -ENOTSUPP;
4023put_file:
4024 fput(file);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004025 return err;
4026}
4027
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004028#define BPF_MAP_BATCH_LAST_FIELD batch.flags
4029
4030#define BPF_DO_BATCH(fn) \
4031 do { \
4032 if (!fn) { \
4033 err = -ENOTSUPP; \
4034 goto err_put; \
4035 } \
4036 err = fn(map, attr, uattr); \
4037 } while (0)
4038
4039static int bpf_map_do_batch(const union bpf_attr *attr,
4040 union bpf_attr __user *uattr,
4041 int cmd)
4042{
4043 struct bpf_map *map;
4044 int err, ufd;
4045 struct fd f;
4046
4047 if (CHECK_ATTR(BPF_MAP_BATCH))
4048 return -EINVAL;
4049
4050 ufd = attr->batch.map_fd;
4051 f = fdget(ufd);
4052 map = __bpf_map_get(f);
4053 if (IS_ERR(map))
4054 return PTR_ERR(map);
4055
Yonghong Song05799632020-01-15 10:43:04 -08004056 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
4057 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004058 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4059 err = -EPERM;
4060 goto err_put;
4061 }
4062
4063 if (cmd != BPF_MAP_LOOKUP_BATCH &&
4064 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4065 err = -EPERM;
4066 goto err_put;
4067 }
4068
4069 if (cmd == BPF_MAP_LOOKUP_BATCH)
4070 BPF_DO_BATCH(map->ops->map_lookup_batch);
Yonghong Song05799632020-01-15 10:43:04 -08004071 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4072 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004073 else if (cmd == BPF_MAP_UPDATE_BATCH)
4074 BPF_DO_BATCH(map->ops->map_update_batch);
4075 else
4076 BPF_DO_BATCH(map->ops->map_delete_batch);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004077
4078err_put:
4079 fdput(f);
4080 return err;
4081}
4082
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004083static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
4084 struct bpf_prog *prog)
Yonghong Songde4e05c2020-05-09 10:59:01 -07004085{
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004086 if (attr->link_create.attach_type != prog->expected_attach_type)
4087 return -EINVAL;
Yonghong Songde4e05c2020-05-09 10:59:01 -07004088
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004089 if (prog->expected_attach_type == BPF_TRACE_ITER)
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004090 return bpf_iter_link_attach(attr, uattr, prog);
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004091 else if (prog->type == BPF_PROG_TYPE_EXT)
4092 return bpf_tracing_prog_attach(prog,
4093 attr->link_create.target_fd,
4094 attr->link_create.target_btf_id);
Yonghong Songde4e05c2020-05-09 10:59:01 -07004095 return -EINVAL;
4096}
4097
Yonghong Song5e7b3022020-08-04 22:50:56 -07004098#define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004099static int link_create(union bpf_attr *attr, bpfptr_t uattr)
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004100{
4101 enum bpf_prog_type ptype;
4102 struct bpf_prog *prog;
4103 int ret;
4104
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004105 if (CHECK_ATTR(BPF_LINK_CREATE))
4106 return -EINVAL;
4107
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004108 prog = bpf_prog_get(attr->link_create.prog_fd);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004109 if (IS_ERR(prog))
4110 return PTR_ERR(prog);
4111
4112 ret = bpf_prog_attach_check_attach_type(prog,
4113 attr->link_create.attach_type);
4114 if (ret)
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004115 goto out;
4116
4117 if (prog->type == BPF_PROG_TYPE_EXT) {
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004118 ret = tracing_bpf_link_attach(attr, uattr, prog);
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004119 goto out;
4120 }
4121
4122 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4123 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4124 ret = -EINVAL;
4125 goto out;
4126 }
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004127
4128 switch (ptype) {
4129 case BPF_PROG_TYPE_CGROUP_SKB:
4130 case BPF_PROG_TYPE_CGROUP_SOCK:
4131 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4132 case BPF_PROG_TYPE_SOCK_OPS:
4133 case BPF_PROG_TYPE_CGROUP_DEVICE:
4134 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4135 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4136 ret = cgroup_bpf_link_attach(attr, prog);
4137 break;
Yonghong Songde4e05c2020-05-09 10:59:01 -07004138 case BPF_PROG_TYPE_TRACING:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004139 ret = tracing_bpf_link_attach(attr, uattr, prog);
Yonghong Songde4e05c2020-05-09 10:59:01 -07004140 break;
Jakub Sitnicki7f045a42020-05-31 10:28:38 +02004141 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02004142 case BPF_PROG_TYPE_SK_LOOKUP:
Jakub Sitnicki7f045a42020-05-31 10:28:38 +02004143 ret = netns_bpf_link_create(attr, prog);
4144 break;
Andrii Nakryiko310ad792020-07-28 12:05:27 -07004145#ifdef CONFIG_NET
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07004146 case BPF_PROG_TYPE_XDP:
4147 ret = bpf_xdp_link_attach(attr, prog);
4148 break;
Andrii Nakryiko310ad792020-07-28 12:05:27 -07004149#endif
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004150 default:
4151 ret = -EINVAL;
4152 }
4153
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004154out:
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004155 if (ret < 0)
4156 bpf_prog_put(prog);
4157 return ret;
4158}
4159
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004160#define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4161
4162static int link_update(union bpf_attr *attr)
4163{
4164 struct bpf_prog *old_prog = NULL, *new_prog;
4165 struct bpf_link *link;
4166 u32 flags;
4167 int ret;
4168
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004169 if (CHECK_ATTR(BPF_LINK_UPDATE))
4170 return -EINVAL;
4171
4172 flags = attr->link_update.flags;
4173 if (flags & ~BPF_F_REPLACE)
4174 return -EINVAL;
4175
4176 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4177 if (IS_ERR(link))
4178 return PTR_ERR(link);
4179
4180 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004181 if (IS_ERR(new_prog)) {
4182 ret = PTR_ERR(new_prog);
4183 goto out_put_link;
4184 }
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004185
4186 if (flags & BPF_F_REPLACE) {
4187 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4188 if (IS_ERR(old_prog)) {
4189 ret = PTR_ERR(old_prog);
4190 old_prog = NULL;
4191 goto out_put_progs;
4192 }
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004193 } else if (attr->link_update.old_prog_fd) {
4194 ret = -EINVAL;
4195 goto out_put_progs;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004196 }
4197
Andrii Nakryikof9d04122020-04-28 17:16:05 -07004198 if (link->ops->update_prog)
4199 ret = link->ops->update_prog(link, new_prog, old_prog);
4200 else
Jakub Sitnickife537392020-05-25 14:29:28 +02004201 ret = -EINVAL;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004202
4203out_put_progs:
4204 if (old_prog)
4205 bpf_prog_put(old_prog);
4206 if (ret)
4207 bpf_prog_put(new_prog);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004208out_put_link:
4209 bpf_link_put(link);
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004210 return ret;
4211}
4212
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07004213#define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4214
4215static int link_detach(union bpf_attr *attr)
4216{
4217 struct bpf_link *link;
4218 int ret;
4219
4220 if (CHECK_ATTR(BPF_LINK_DETACH))
4221 return -EINVAL;
4222
4223 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4224 if (IS_ERR(link))
4225 return PTR_ERR(link);
4226
4227 if (link->ops->detach)
4228 ret = link->ops->detach(link);
4229 else
4230 ret = -EOPNOTSUPP;
4231
4232 bpf_link_put(link);
4233 return ret;
4234}
4235
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004236static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004237{
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004238 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4239}
4240
4241struct bpf_link *bpf_link_by_id(u32 id)
4242{
4243 struct bpf_link *link;
4244
4245 if (!id)
4246 return ERR_PTR(-ENOENT);
4247
4248 spin_lock_bh(&link_idr_lock);
4249 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4250 link = idr_find(&link_idr, id);
4251 if (link) {
4252 if (link->id)
4253 link = bpf_link_inc_not_zero(link);
4254 else
4255 link = ERR_PTR(-EAGAIN);
4256 } else {
4257 link = ERR_PTR(-ENOENT);
4258 }
4259 spin_unlock_bh(&link_idr_lock);
4260 return link;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004261}
4262
4263#define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4264
4265static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4266{
4267 struct bpf_link *link;
4268 u32 id = attr->link_id;
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004269 int fd;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004270
4271 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4272 return -EINVAL;
4273
4274 if (!capable(CAP_SYS_ADMIN))
4275 return -EPERM;
4276
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004277 link = bpf_link_by_id(id);
4278 if (IS_ERR(link))
4279 return PTR_ERR(link);
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004280
4281 fd = bpf_link_new_fd(link);
4282 if (fd < 0)
4283 bpf_link_put(link);
4284
4285 return fd;
4286}
4287
Song Liud46edd62020-04-30 00:15:04 -07004288DEFINE_MUTEX(bpf_stats_enabled_mutex);
4289
4290static int bpf_stats_release(struct inode *inode, struct file *file)
4291{
4292 mutex_lock(&bpf_stats_enabled_mutex);
4293 static_key_slow_dec(&bpf_stats_enabled_key.key);
4294 mutex_unlock(&bpf_stats_enabled_mutex);
4295 return 0;
4296}
4297
4298static const struct file_operations bpf_stats_fops = {
4299 .release = bpf_stats_release,
4300};
4301
4302static int bpf_enable_runtime_stats(void)
4303{
4304 int fd;
4305
4306 mutex_lock(&bpf_stats_enabled_mutex);
4307
4308 /* Set a very high limit to avoid overflow */
4309 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4310 mutex_unlock(&bpf_stats_enabled_mutex);
4311 return -EBUSY;
4312 }
4313
4314 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4315 if (fd >= 0)
4316 static_key_slow_inc(&bpf_stats_enabled_key.key);
4317
4318 mutex_unlock(&bpf_stats_enabled_mutex);
4319 return fd;
4320}
4321
4322#define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4323
4324static int bpf_enable_stats(union bpf_attr *attr)
4325{
4326
4327 if (CHECK_ATTR(BPF_ENABLE_STATS))
4328 return -EINVAL;
4329
4330 if (!capable(CAP_SYS_ADMIN))
4331 return -EPERM;
4332
4333 switch (attr->enable_stats.type) {
4334 case BPF_STATS_RUN_TIME:
4335 return bpf_enable_runtime_stats();
4336 default:
4337 break;
4338 }
4339 return -EINVAL;
4340}
4341
Yonghong Songac51d992020-05-09 10:59:05 -07004342#define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4343
4344static int bpf_iter_create(union bpf_attr *attr)
4345{
4346 struct bpf_link *link;
4347 int err;
4348
4349 if (CHECK_ATTR(BPF_ITER_CREATE))
4350 return -EINVAL;
4351
4352 if (attr->iter_create.flags)
4353 return -EINVAL;
4354
4355 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4356 if (IS_ERR(link))
4357 return PTR_ERR(link);
4358
4359 err = bpf_iter_new_fd(link);
4360 bpf_link_put(link);
4361
4362 return err;
4363}
4364
YiFei Zhuef153142020-09-15 16:45:40 -07004365#define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4366
4367static int bpf_prog_bind_map(union bpf_attr *attr)
4368{
4369 struct bpf_prog *prog;
4370 struct bpf_map *map;
4371 struct bpf_map **used_maps_old, **used_maps_new;
4372 int i, ret = 0;
4373
4374 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4375 return -EINVAL;
4376
4377 if (attr->prog_bind_map.flags)
4378 return -EINVAL;
4379
4380 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4381 if (IS_ERR(prog))
4382 return PTR_ERR(prog);
4383
4384 map = bpf_map_get(attr->prog_bind_map.map_fd);
4385 if (IS_ERR(map)) {
4386 ret = PTR_ERR(map);
4387 goto out_prog_put;
4388 }
4389
4390 mutex_lock(&prog->aux->used_maps_mutex);
4391
4392 used_maps_old = prog->aux->used_maps;
4393
4394 for (i = 0; i < prog->aux->used_map_cnt; i++)
Stanislav Fomichev1028ae42020-10-02 17:25:44 -07004395 if (used_maps_old[i] == map) {
4396 bpf_map_put(map);
YiFei Zhuef153142020-09-15 16:45:40 -07004397 goto out_unlock;
Stanislav Fomichev1028ae42020-10-02 17:25:44 -07004398 }
YiFei Zhuef153142020-09-15 16:45:40 -07004399
4400 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4401 sizeof(used_maps_new[0]),
4402 GFP_KERNEL);
4403 if (!used_maps_new) {
4404 ret = -ENOMEM;
4405 goto out_unlock;
4406 }
4407
4408 memcpy(used_maps_new, used_maps_old,
4409 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4410 used_maps_new[prog->aux->used_map_cnt] = map;
4411
4412 prog->aux->used_map_cnt++;
4413 prog->aux->used_maps = used_maps_new;
4414
4415 kfree(used_maps_old);
4416
4417out_unlock:
4418 mutex_unlock(&prog->aux->used_maps_mutex);
4419
4420 if (ret)
4421 bpf_map_put(map);
4422out_prog_put:
4423 bpf_prog_put(prog);
4424 return ret;
4425}
4426
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004427static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004428{
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004429 union bpf_attr attr;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004430 int err;
4431
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07004432 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004433 return -EPERM;
4434
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07004435 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004436 if (err)
4437 return err;
4438 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004439
4440 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004441 memset(&attr, 0, sizeof(attr));
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004442 if (copy_from_bpfptr(&attr, uattr, size) != 0)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004443 return -EFAULT;
4444
Chenbo Fengafdb09c2017-10-18 13:00:24 -07004445 err = security_bpf(cmd, &attr, size);
4446 if (err < 0)
4447 return err;
4448
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004449 switch (cmd) {
4450 case BPF_MAP_CREATE:
4451 err = map_create(&attr);
4452 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07004453 case BPF_MAP_LOOKUP_ELEM:
4454 err = map_lookup_elem(&attr);
4455 break;
4456 case BPF_MAP_UPDATE_ELEM:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004457 err = map_update_elem(&attr, uattr);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07004458 break;
4459 case BPF_MAP_DELETE_ELEM:
4460 err = map_delete_elem(&attr);
4461 break;
4462 case BPF_MAP_GET_NEXT_KEY:
4463 err = map_get_next_key(&attr);
4464 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02004465 case BPF_MAP_FREEZE:
4466 err = map_freeze(&attr);
4467 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004468 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08004469 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004470 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01004471 case BPF_OBJ_PIN:
4472 err = bpf_obj_pin(&attr);
4473 break;
4474 case BPF_OBJ_GET:
4475 err = bpf_obj_get(&attr);
4476 break;
Daniel Mackf4324552016-11-23 16:52:27 +01004477 case BPF_PROG_ATTACH:
4478 err = bpf_prog_attach(&attr);
4479 break;
4480 case BPF_PROG_DETACH:
4481 err = bpf_prog_detach(&attr);
4482 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07004483 case BPF_PROG_QUERY:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004484 err = bpf_prog_query(&attr, uattr.user);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07004485 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07004486 case BPF_PROG_TEST_RUN:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004487 err = bpf_prog_test_run(&attr, uattr.user);
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07004488 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004489 case BPF_PROG_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004490 err = bpf_obj_get_next_id(&attr, uattr.user,
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004491 &prog_idr, &prog_idr_lock);
4492 break;
4493 case BPF_MAP_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004494 err = bpf_obj_get_next_id(&attr, uattr.user,
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004495 &map_idr, &map_idr_lock);
4496 break;
Quentin Monnet1b9ed842019-08-20 10:31:50 +01004497 case BPF_BTF_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004498 err = bpf_obj_get_next_id(&attr, uattr.user,
Quentin Monnet1b9ed842019-08-20 10:31:50 +01004499 &btf_idr, &btf_idr_lock);
4500 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07004501 case BPF_PROG_GET_FD_BY_ID:
4502 err = bpf_prog_get_fd_by_id(&attr);
4503 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07004504 case BPF_MAP_GET_FD_BY_ID:
4505 err = bpf_map_get_fd_by_id(&attr);
4506 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004507 case BPF_OBJ_GET_INFO_BY_FD:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004508 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004509 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07004510 case BPF_RAW_TRACEPOINT_OPEN:
4511 err = bpf_raw_tracepoint_open(&attr);
4512 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004513 case BPF_BTF_LOAD:
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07004514 err = bpf_btf_load(&attr, uattr);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004515 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07004516 case BPF_BTF_GET_FD_BY_ID:
4517 err = bpf_btf_get_fd_by_id(&attr);
4518 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004519 case BPF_TASK_FD_QUERY:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004520 err = bpf_task_fd_query(&attr, uattr.user);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004521 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02004522 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4523 err = map_lookup_and_delete_elem(&attr);
4524 break;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004525 case BPF_MAP_LOOKUP_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004526 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004527 break;
Yonghong Song05799632020-01-15 10:43:04 -08004528 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004529 err = bpf_map_do_batch(&attr, uattr.user,
Yonghong Song05799632020-01-15 10:43:04 -08004530 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4531 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004532 case BPF_MAP_UPDATE_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004533 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004534 break;
4535 case BPF_MAP_DELETE_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004536 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004537 break;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004538 case BPF_LINK_CREATE:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004539 err = link_create(&attr, uattr);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004540 break;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004541 case BPF_LINK_UPDATE:
4542 err = link_update(&attr);
4543 break;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004544 case BPF_LINK_GET_FD_BY_ID:
4545 err = bpf_link_get_fd_by_id(&attr);
4546 break;
4547 case BPF_LINK_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004548 err = bpf_obj_get_next_id(&attr, uattr.user,
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004549 &link_idr, &link_idr_lock);
4550 break;
Song Liud46edd62020-04-30 00:15:04 -07004551 case BPF_ENABLE_STATS:
4552 err = bpf_enable_stats(&attr);
4553 break;
Yonghong Songac51d992020-05-09 10:59:05 -07004554 case BPF_ITER_CREATE:
4555 err = bpf_iter_create(&attr);
4556 break;
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07004557 case BPF_LINK_DETACH:
4558 err = link_detach(&attr);
4559 break;
YiFei Zhuef153142020-09-15 16:45:40 -07004560 case BPF_PROG_BIND_MAP:
4561 err = bpf_prog_bind_map(&attr);
4562 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004563 default:
4564 err = -EINVAL;
4565 break;
4566 }
4567
4568 return err;
4569}
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004570
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004571SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4572{
4573 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
4574}
4575
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004576static bool syscall_prog_is_valid_access(int off, int size,
4577 enum bpf_access_type type,
4578 const struct bpf_prog *prog,
4579 struct bpf_insn_access_aux *info)
4580{
4581 if (off < 0 || off >= U16_MAX)
4582 return false;
4583 if (off % size != 0)
4584 return false;
4585 return true;
4586}
4587
4588BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size)
4589{
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004590 switch (cmd) {
4591 case BPF_MAP_CREATE:
4592 case BPF_MAP_UPDATE_ELEM:
4593 case BPF_MAP_FREEZE:
4594 case BPF_PROG_LOAD:
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07004595 case BPF_BTF_LOAD:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004596 break;
4597 /* case BPF_PROG_TEST_RUN:
4598 * is not part of this list to prevent recursive test_run
4599 */
4600 default:
4601 return -EINVAL;
4602 }
4603 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004604}
4605
Pu Lehui3a2daa72021-05-19 14:41:16 +08004606static const struct bpf_func_proto bpf_sys_bpf_proto = {
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004607 .func = bpf_sys_bpf,
4608 .gpl_only = false,
4609 .ret_type = RET_INTEGER,
4610 .arg1_type = ARG_ANYTHING,
4611 .arg2_type = ARG_PTR_TO_MEM,
4612 .arg3_type = ARG_CONST_SIZE,
4613};
4614
4615const struct bpf_func_proto * __weak
4616tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4617{
4618 return bpf_base_func_proto(func_id);
4619}
4620
Alexei Starovoitov3abea082021-05-13 17:36:12 -07004621BPF_CALL_1(bpf_sys_close, u32, fd)
4622{
4623 /* When bpf program calls this helper there should not be
4624 * an fdget() without matching completed fdput().
4625 * This helper is allowed in the following callchain only:
4626 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
4627 */
4628 return close_fd(fd);
4629}
4630
Pu Lehui3a2daa72021-05-19 14:41:16 +08004631static const struct bpf_func_proto bpf_sys_close_proto = {
Alexei Starovoitov3abea082021-05-13 17:36:12 -07004632 .func = bpf_sys_close,
4633 .gpl_only = false,
4634 .ret_type = RET_INTEGER,
4635 .arg1_type = ARG_ANYTHING,
4636};
4637
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004638static const struct bpf_func_proto *
4639syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4640{
4641 switch (func_id) {
4642 case BPF_FUNC_sys_bpf:
4643 return &bpf_sys_bpf_proto;
Alexei Starovoitov3d784172021-05-13 17:36:11 -07004644 case BPF_FUNC_btf_find_by_name_kind:
4645 return &bpf_btf_find_by_name_kind_proto;
Alexei Starovoitov3abea082021-05-13 17:36:12 -07004646 case BPF_FUNC_sys_close:
4647 return &bpf_sys_close_proto;
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004648 default:
4649 return tracing_prog_func_proto(func_id, prog);
4650 }
4651}
4652
4653const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
4654 .get_func_proto = syscall_prog_func_proto,
4655 .is_valid_access = syscall_prog_is_valid_access,
4656};
4657
4658const struct bpf_prog_ops bpf_syscall_prog_ops = {
4659 .test_run = bpf_prog_test_run_syscall,
4660};