blob: 27760627370db821a5fc2741f73130e4e2bedb2f [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>
Jakub Kicinskiaef2fed2021-12-15 18:55:37 -08005#include <linux/bpf-cgroup.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01006#include <linux/bpf_trace.h>
Sean Youngf4364dc2018-05-27 12:24:09 +01007#include <linux/bpf_lirc.h>
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02008#include <linux/bpf_verifier.h>
Kumar Kartikeya Dwivedi61df10c2022-04-25 03:18:49 +05309#include <linux/bsearch.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -070010#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070011#include <linux/syscalls.h>
12#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010013#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010014#include <linux/vmalloc.h>
15#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070016#include <linux/anon_inodes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070017#include <linux/fdtable.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070018#include <linux/file.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070019#include <linux/fs.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070020#include <linux/license.h>
21#include <linux/filter.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010022#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070023#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070024#include <linux/cred.h>
25#include <linux/timekeeping.h>
26#include <linux/ctype.h>
Mark Rutland9ef09e32018-05-03 17:04:59 +010027#include <linux/nospec.h>
Daniel Borkmannbae141f2019-12-06 22:49:34 +010028#include <linux/audit.h>
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -070029#include <uapi/linux/btf.h>
Mike Rapoportca5999f2020-06-08 21:32:38 -070030#include <linux/pgtable.h>
KP Singh9e4e01d2020-03-29 01:43:52 +010031#include <linux/bpf_lsm.h>
Andrii Nakryiko457f44362020-05-29 00:54:20 -070032#include <linux/poll.h>
Kumar Kartikeya Dwivedi4d7d7f62022-04-25 03:18:53 +053033#include <linux/sort.h>
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +020034#include <linux/bpf-netns.h>
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -070035#include <linux/rcupdate_trace.h>
Roman Gushchin48edc1f2020-12-01 13:58:32 -080036#include <linux/memcontrol.h>
Jiri Olsa0dcac272022-03-16 13:24:09 +010037#include <linux/trace_events.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070038
Daniel Borkmannda765a22019-11-22 21:07:58 +010039#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
40 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
41 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
42#define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070043#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
Daniel Borkmannda765a22019-11-22 21:07:58 +010044#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
45 IS_FD_HASH(map))
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070046
Chenbo Feng6e71b042017-10-18 13:00:22 -070047#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
48
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080049DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070050static DEFINE_IDR(prog_idr);
51static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070052static DEFINE_IDR(map_idr);
53static DEFINE_SPINLOCK(map_idr_lock);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -070054static DEFINE_IDR(link_idr);
55static DEFINE_SPINLOCK(link_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080056
Daniel Borkmann08389d82021-05-11 22:35:17 +020057int sysctl_unprivileged_bpf_disabled __read_mostly =
58 IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070059
Johannes Berg40077e02017-04-11 15:34:58 +020060static const struct bpf_map_ops * const bpf_map_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -080061#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
Johannes Berg40077e02017-04-11 15:34:58 +020062#define BPF_MAP_TYPE(_id, _ops) \
63 [_id] = &_ops,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -070064#define BPF_LINK_TYPE(_id, _name)
Johannes Berg40077e02017-04-11 15:34:58 +020065#include <linux/bpf_types.h>
66#undef BPF_PROG_TYPE
67#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -070068#undef BPF_LINK_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +020069};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070070
Mickaël Salaün752ba562017-08-07 20:45:20 +020071/*
72 * If we're handed a bigger struct than we know of, ensure all the unknown bits
73 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
74 * we don't know about yet.
75 *
76 * There is a ToCToU between this function call and the following
77 * copy_from_user() call. However, this is not a concern since this function is
78 * meant to be a future-proofing of bits.
79 */
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -070080int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070081 size_t expected_size,
82 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020083{
Al Virob7e4b652020-05-08 00:16:31 -040084 int res;
Mickaël Salaün58291a72017-08-07 20:45:19 +020085
Mickaël Salaün752ba562017-08-07 20:45:20 +020086 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
87 return -E2BIG;
88
Mickaël Salaün58291a72017-08-07 20:45:19 +020089 if (actual_size <= expected_size)
90 return 0;
91
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -070092 if (uaddr.is_kernel)
93 res = memchr_inv(uaddr.kernel + expected_size, 0,
94 actual_size - expected_size) == NULL;
95 else
96 res = check_zeroed_user(uaddr.user + expected_size,
97 actual_size - expected_size);
Al Virob7e4b652020-05-08 00:16:31 -040098 if (res < 0)
99 return res;
100 return res ? 0 : -E2BIG;
Mickaël Salaün58291a72017-08-07 20:45:19 +0200101}
102
Jakub Kicinskia3884572018-01-11 20:29:09 -0800103const struct bpf_map_ops bpf_map_offload_ops = {
Martin KaFai Lauf4d05252020-08-27 18:18:06 -0700104 .map_meta_equal = bpf_map_meta_equal,
Jakub Kicinskia3884572018-01-11 20:29:09 -0800105 .map_alloc = bpf_map_offload_map_alloc,
106 .map_free = bpf_map_offload_map_free,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200107 .map_check_btf = map_check_no_btf,
Jakub Kicinskia3884572018-01-11 20:29:09 -0800108};
109
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700110static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
111{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800112 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100113 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700114 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800115 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700116
Mark Rutland9ef09e32018-05-03 17:04:59 +0100117 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800118 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100119 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
120 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800121 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200122 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700123
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800124 if (ops->map_alloc_check) {
125 err = ops->map_alloc_check(attr);
126 if (err)
127 return ERR_PTR(err);
128 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800129 if (attr->map_ifindex)
130 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800131 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200132 if (IS_ERR(map))
133 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800134 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100135 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200136 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700137}
138
Daniel Borkmann353050b2021-11-09 18:48:08 +0000139static void bpf_map_write_active_inc(struct bpf_map *map)
140{
141 atomic64_inc(&map->writecnt);
142}
143
144static void bpf_map_write_active_dec(struct bpf_map *map)
145{
146 atomic64_dec(&map->writecnt);
147}
148
149bool bpf_map_write_active(const struct bpf_map *map)
150{
151 return atomic64_read(&map->writecnt) != 0;
152}
153
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800154static u32 bpf_map_value_size(const struct bpf_map *map)
Brian Vazquez15c14a32020-01-15 10:43:00 -0800155{
156 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
157 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
158 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
159 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
160 return round_up(map->value_size, 8) * num_possible_cpus();
161 else if (IS_FD_MAP(map))
162 return sizeof(u32);
163 else
164 return map->value_size;
165}
166
167static void maybe_wait_bpf_programs(struct bpf_map *map)
168{
169 /* Wait for any running BPF programs to complete so that
170 * userspace, when we return to it, knows that all programs
171 * that could be running use the new map value.
172 */
173 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
174 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
175 synchronize_rcu();
176}
177
178static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
179 void *value, __u64 flags)
180{
181 int err;
182
183 /* Need to create a kthread, thus must support schedule */
184 if (bpf_map_is_dev_bound(map)) {
185 return bpf_map_offload_update_elem(map, key, value, flags);
186 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
Brian Vazquez15c14a32020-01-15 10:43:00 -0800187 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
188 return map->ops->map_update_elem(map, key, value, flags);
Lorenz Bauer13b79d32020-08-21 11:29:45 +0100189 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
190 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
191 return sock_map_update_elem_sys(map, key, value, flags);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800192 } else if (IS_FD_PROG_ARRAY(map)) {
193 return bpf_fd_array_map_update_elem(map, f.file, key, value,
194 flags);
195 }
196
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100197 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800198 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
199 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
200 err = bpf_percpu_hash_update(map, key, value, flags);
201 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
202 err = bpf_percpu_array_update(map, key, value, flags);
203 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
204 err = bpf_percpu_cgroup_storage_update(map, key, value,
205 flags);
206 } else if (IS_FD_ARRAY(map)) {
207 rcu_read_lock();
208 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
209 flags);
210 rcu_read_unlock();
211 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
212 rcu_read_lock();
213 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
214 flags);
215 rcu_read_unlock();
216 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
217 /* rcu_read_lock() is not needed */
218 err = bpf_fd_reuseport_array_update_elem(map, key, value,
219 flags);
220 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
Joanne Koong93309862021-10-27 16:45:00 -0700221 map->map_type == BPF_MAP_TYPE_STACK ||
222 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
Brian Vazquez15c14a32020-01-15 10:43:00 -0800223 err = map->ops->map_push_elem(map, value, flags);
224 } else {
225 rcu_read_lock();
226 err = map->ops->map_update_elem(map, key, value, flags);
227 rcu_read_unlock();
228 }
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100229 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800230 maybe_wait_bpf_programs(map);
231
232 return err;
233}
234
235static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
236 __u64 flags)
237{
238 void *ptr;
239 int err;
240
Brian Vazquezcb4d03a2020-01-15 10:43:01 -0800241 if (bpf_map_is_dev_bound(map))
242 return bpf_map_offload_lookup_elem(map, key, value);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800243
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100244 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800245 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
246 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
247 err = bpf_percpu_hash_copy(map, key, value);
248 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
249 err = bpf_percpu_array_copy(map, key, value);
250 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
251 err = bpf_percpu_cgroup_storage_copy(map, key, value);
252 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
253 err = bpf_stackmap_copy(map, key, value);
254 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
255 err = bpf_fd_array_map_lookup_elem(map, key, value);
256 } else if (IS_FD_HASH(map)) {
257 err = bpf_fd_htab_map_lookup_elem(map, key, value);
258 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
259 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
260 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
Joanne Koong93309862021-10-27 16:45:00 -0700261 map->map_type == BPF_MAP_TYPE_STACK ||
262 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
Brian Vazquez15c14a32020-01-15 10:43:00 -0800263 err = map->ops->map_peek_elem(map, value);
264 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
265 /* struct_ops map requires directly updating "value" */
266 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
267 } else {
268 rcu_read_lock();
269 if (map->ops->map_lookup_elem_sys_only)
270 ptr = map->ops->map_lookup_elem_sys_only(map, key);
271 else
272 ptr = map->ops->map_lookup_elem(map, key);
273 if (IS_ERR(ptr)) {
274 err = PTR_ERR(ptr);
275 } else if (!ptr) {
276 err = -ENOENT;
277 } else {
278 err = 0;
279 if (flags & BPF_F_LOCK)
280 /* lock 'ptr' and copy everything but lock */
281 copy_map_value_locked(map, value, ptr, true);
282 else
283 copy_map_value(map, value, ptr);
Alexei Starovoitov68134662021-07-14 17:54:10 -0700284 /* mask lock and timer, since value wasn't zero inited */
285 check_and_init_map_value(map, value);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800286 }
287 rcu_read_unlock();
288 }
289
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100290 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800291 maybe_wait_bpf_programs(map);
292
293 return err;
294}
295
Roman Gushchind5299b62020-12-01 13:58:33 -0800296/* Please, do not use this function outside from the map creation path
297 * (e.g. in map update path) without taking care of setting the active
298 * memory cgroup (see at bpf_map_kmalloc_node() for example).
299 */
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100300static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100301{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100302 /* We really just want to fail instead of triggering OOM killer
303 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
304 * which is used for lower order allocation requests.
305 *
306 * It has been observed that higher order allocation requests done by
307 * vmalloc with __GFP_NORETRY being set might fail due to not trying
308 * to reclaim memory from the page cache, thus we set
309 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100310 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100311
Roman Gushchind5299b62020-12-01 13:58:33 -0800312 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
Christoph Hellwig041de932020-06-01 21:52:02 -0700313 unsigned int flags = 0;
314 unsigned long align = 1;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100315 void *area;
316
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100317 if (size >= SIZE_MAX)
318 return NULL;
319
Andrii Nakryikofc970222019-11-17 09:28:04 -0800320 /* kmalloc()'ed memory can't be mmap()'ed */
Christoph Hellwig041de932020-06-01 21:52:02 -0700321 if (mmapable) {
322 BUG_ON(!PAGE_ALIGNED(size));
323 align = SHMLBA;
324 flags = VM_USERMAP;
325 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
326 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100327 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100328 if (area != NULL)
329 return area;
330 }
Christoph Hellwig041de932020-06-01 21:52:02 -0700331
332 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
333 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
334 flags, numa_node, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100335}
336
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100337void *bpf_map_area_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800338{
339 return __bpf_map_area_alloc(size, numa_node, false);
340}
341
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100342void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800343{
344 return __bpf_map_area_alloc(size, numa_node, true);
345}
346
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100347void bpf_map_area_free(void *area)
348{
349 kvfree(area);
350}
351
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200352static u32 bpf_map_flags_retain_permanent(u32 flags)
353{
354 /* Some map creation flags are not tied to the map object but
355 * rather to the map fd instead, so they have no meaning upon
356 * map object inspection since multiple file descriptors with
357 * different (access) properties can exist here. Thus, given
358 * this has zero meaning for the map itself, lets clear these
359 * from here.
360 */
361 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
362}
363
Jakub Kicinskibd475642018-01-11 20:29:06 -0800364void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
365{
366 map->map_type = attr->map_type;
367 map->key_size = attr->key_size;
368 map->value_size = attr->value_size;
369 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200370 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800371 map->numa_node = bpf_map_attr_numa_node(attr);
Joanne Koong93309862021-10-27 16:45:00 -0700372 map->map_extra = attr->map_extra;
Jakub Kicinskibd475642018-01-11 20:29:06 -0800373}
374
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700375static int bpf_map_alloc_id(struct bpf_map *map)
376{
377 int id;
378
Shaohua Lib76354c2018-03-27 11:53:21 -0700379 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700380 spin_lock_bh(&map_idr_lock);
381 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
382 if (id > 0)
383 map->id = id;
384 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700385 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700386
387 if (WARN_ON_ONCE(!id))
388 return -ENOSPC;
389
390 return id > 0 ? 0 : id;
391}
392
Jakub Kicinskia3884572018-01-11 20:29:09 -0800393void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700394{
Eric Dumazet930651a2017-09-19 09:15:59 -0700395 unsigned long flags;
396
Jakub Kicinskia3884572018-01-11 20:29:09 -0800397 /* Offloaded maps are removed from the IDR store when their device
398 * disappears - even if someone holds an fd to them they are unusable,
399 * the memory is gone, all ops will fail; they are simply waiting for
400 * refcnt to drop to be freed.
401 */
402 if (!map->id)
403 return;
404
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700405 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700406 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700407 else
408 __acquire(&map_idr_lock);
409
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700410 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800411 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700412
413 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700414 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700415 else
416 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700417}
418
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800419#ifdef CONFIG_MEMCG_KMEM
420static void bpf_map_save_memcg(struct bpf_map *map)
421{
Roman Gushchin4201d9ab2022-07-11 09:28:27 -0700422 /* Currently if a map is created by a process belonging to the root
423 * memory cgroup, get_obj_cgroup_from_current() will return NULL.
424 * So we have to check map->objcg for being NULL each time it's
425 * being used.
426 */
427 map->objcg = get_obj_cgroup_from_current();
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800428}
429
430static void bpf_map_release_memcg(struct bpf_map *map)
431{
Roman Gushchin4201d9ab2022-07-11 09:28:27 -0700432 if (map->objcg)
433 obj_cgroup_put(map->objcg);
434}
435
436static struct mem_cgroup *bpf_map_get_memcg(const struct bpf_map *map)
437{
438 if (map->objcg)
439 return get_mem_cgroup_from_objcg(map->objcg);
440
441 return root_mem_cgroup;
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800442}
443
444void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
445 int node)
446{
Roman Gushchin4201d9ab2022-07-11 09:28:27 -0700447 struct mem_cgroup *memcg, *old_memcg;
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800448 void *ptr;
449
Roman Gushchin4201d9ab2022-07-11 09:28:27 -0700450 memcg = bpf_map_get_memcg(map);
451 old_memcg = set_active_memcg(memcg);
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800452 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
453 set_active_memcg(old_memcg);
Roman Gushchin4201d9ab2022-07-11 09:28:27 -0700454 mem_cgroup_put(memcg);
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800455
456 return ptr;
457}
458
459void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
460{
Roman Gushchin4201d9ab2022-07-11 09:28:27 -0700461 struct mem_cgroup *memcg, *old_memcg;
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800462 void *ptr;
463
Roman Gushchin4201d9ab2022-07-11 09:28:27 -0700464 memcg = bpf_map_get_memcg(map);
465 old_memcg = set_active_memcg(memcg);
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800466 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
467 set_active_memcg(old_memcg);
Roman Gushchin4201d9ab2022-07-11 09:28:27 -0700468 mem_cgroup_put(memcg);
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800469
470 return ptr;
471}
472
473void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
474 size_t align, gfp_t flags)
475{
Roman Gushchin4201d9ab2022-07-11 09:28:27 -0700476 struct mem_cgroup *memcg, *old_memcg;
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800477 void __percpu *ptr;
478
Roman Gushchin4201d9ab2022-07-11 09:28:27 -0700479 memcg = bpf_map_get_memcg(map);
480 old_memcg = set_active_memcg(memcg);
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800481 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
482 set_active_memcg(old_memcg);
Roman Gushchin4201d9ab2022-07-11 09:28:27 -0700483 mem_cgroup_put(memcg);
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800484
485 return ptr;
486}
487
488#else
489static void bpf_map_save_memcg(struct bpf_map *map)
490{
491}
492
493static void bpf_map_release_memcg(struct bpf_map *map)
494{
495}
496#endif
497
Kumar Kartikeya Dwivedi61df10c2022-04-25 03:18:49 +0530498static int bpf_map_kptr_off_cmp(const void *a, const void *b)
499{
500 const struct bpf_map_value_off_desc *off_desc1 = a, *off_desc2 = b;
501
502 if (off_desc1->offset < off_desc2->offset)
503 return -1;
504 else if (off_desc1->offset > off_desc2->offset)
505 return 1;
506 return 0;
507}
508
509struct bpf_map_value_off_desc *bpf_map_kptr_off_contains(struct bpf_map *map, u32 offset)
510{
511 /* Since members are iterated in btf_find_field in increasing order,
512 * offsets appended to kptr_off_tab are in increasing order, so we can
513 * do bsearch to find exact match.
514 */
515 struct bpf_map_value_off *tab;
516
517 if (!map_value_has_kptrs(map))
518 return NULL;
519 tab = map->kptr_off_tab;
520 return bsearch(&offset, tab->off, tab->nr_off, sizeof(tab->off[0]), bpf_map_kptr_off_cmp);
521}
522
523void bpf_map_free_kptr_off_tab(struct bpf_map *map)
524{
525 struct bpf_map_value_off *tab = map->kptr_off_tab;
526 int i;
527
528 if (!map_value_has_kptrs(map))
529 return;
Kumar Kartikeya Dwivedi14a324f2022-04-25 03:18:55 +0530530 for (i = 0; i < tab->nr_off; i++) {
531 if (tab->off[i].kptr.module)
532 module_put(tab->off[i].kptr.module);
Kumar Kartikeya Dwivedi61df10c2022-04-25 03:18:49 +0530533 btf_put(tab->off[i].kptr.btf);
Kumar Kartikeya Dwivedi14a324f2022-04-25 03:18:55 +0530534 }
Kumar Kartikeya Dwivedi61df10c2022-04-25 03:18:49 +0530535 kfree(tab);
536 map->kptr_off_tab = NULL;
537}
538
539struct bpf_map_value_off *bpf_map_copy_kptr_off_tab(const struct bpf_map *map)
540{
541 struct bpf_map_value_off *tab = map->kptr_off_tab, *new_tab;
542 int size, i;
543
544 if (!map_value_has_kptrs(map))
545 return ERR_PTR(-ENOENT);
546 size = offsetof(struct bpf_map_value_off, off[tab->nr_off]);
547 new_tab = kmemdup(tab, size, GFP_KERNEL | __GFP_NOWARN);
548 if (!new_tab)
549 return ERR_PTR(-ENOMEM);
550 /* Do a deep copy of the kptr_off_tab */
Kumar Kartikeya Dwivedi14a324f2022-04-25 03:18:55 +0530551 for (i = 0; i < tab->nr_off; i++) {
Kumar Kartikeya Dwivedi61df10c2022-04-25 03:18:49 +0530552 btf_get(tab->off[i].kptr.btf);
Kumar Kartikeya Dwivedi14a324f2022-04-25 03:18:55 +0530553 if (tab->off[i].kptr.module && !try_module_get(tab->off[i].kptr.module)) {
554 while (i--) {
555 if (tab->off[i].kptr.module)
556 module_put(tab->off[i].kptr.module);
557 btf_put(tab->off[i].kptr.btf);
558 }
559 kfree(new_tab);
560 return ERR_PTR(-ENXIO);
561 }
562 }
Kumar Kartikeya Dwivedi61df10c2022-04-25 03:18:49 +0530563 return new_tab;
564}
565
566bool bpf_map_equal_kptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b)
567{
568 struct bpf_map_value_off *tab_a = map_a->kptr_off_tab, *tab_b = map_b->kptr_off_tab;
569 bool a_has_kptr = map_value_has_kptrs(map_a), b_has_kptr = map_value_has_kptrs(map_b);
570 int size;
571
572 if (!a_has_kptr && !b_has_kptr)
573 return true;
574 if (a_has_kptr != b_has_kptr)
575 return false;
576 if (tab_a->nr_off != tab_b->nr_off)
577 return false;
578 size = offsetof(struct bpf_map_value_off, off[tab_a->nr_off]);
579 return !memcmp(tab_a, tab_b, size);
580}
581
Kumar Kartikeya Dwivedi14a324f2022-04-25 03:18:55 +0530582/* Caller must ensure map_value_has_kptrs is true. Note that this function can
583 * be called on a map value while the map_value is visible to BPF programs, as
584 * it ensures the correct synchronization, and we already enforce the same using
585 * the bpf_kptr_xchg helper on the BPF program side for referenced kptrs.
586 */
587void bpf_map_free_kptrs(struct bpf_map *map, void *map_value)
588{
589 struct bpf_map_value_off *tab = map->kptr_off_tab;
590 unsigned long *btf_id_ptr;
591 int i;
592
593 for (i = 0; i < tab->nr_off; i++) {
594 struct bpf_map_value_off_desc *off_desc = &tab->off[i];
595 unsigned long old_ptr;
596
597 btf_id_ptr = map_value + off_desc->offset;
598 if (off_desc->type == BPF_KPTR_UNREF) {
599 u64 *p = (u64 *)btf_id_ptr;
600
601 WRITE_ONCE(p, 0);
602 continue;
603 }
604 old_ptr = xchg(btf_id_ptr, 0);
605 off_desc->kptr.dtor((void *)old_ptr);
606 }
607}
608
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700609/* called from workqueue */
610static void bpf_map_free_deferred(struct work_struct *work)
611{
612 struct bpf_map *map = container_of(work, struct bpf_map, work);
613
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700614 security_bpf_map_free(map);
Kumar Kartikeya Dwivedi4d7d7f62022-04-25 03:18:53 +0530615 kfree(map->off_arr);
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800616 bpf_map_release_memcg(map);
Kumar Kartikeya Dwivedi14a324f2022-04-25 03:18:55 +0530617 /* implementation dependent freeing, map_free callback also does
618 * bpf_map_free_kptr_off_tab, if needed.
619 */
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700620 map->ops->map_free(map);
621}
622
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100623static void bpf_map_put_uref(struct bpf_map *map)
624{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800625 if (atomic64_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700626 if (map->ops->map_release_uref)
627 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100628 }
629}
630
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700631/* decrement map refcnt and schedule it for freeing via workqueue
632 * (unrelying map implementation ops->map_free() might sleep)
633 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700634static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700635{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800636 if (atomic64_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700637 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700638 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700639 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700640 INIT_WORK(&map->work, bpf_map_free_deferred);
641 schedule_work(&map->work);
642 }
643}
644
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700645void bpf_map_put(struct bpf_map *map)
646{
647 __bpf_map_put(map, true);
648}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700649EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700650
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100651void bpf_map_put_with_uref(struct bpf_map *map)
652{
653 bpf_map_put_uref(map);
654 bpf_map_put(map);
655}
656
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700657static int bpf_map_release(struct inode *inode, struct file *filp)
658{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200659 struct bpf_map *map = filp->private_data;
660
661 if (map->ops->map_release)
662 map->ops->map_release(map, filp);
663
664 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700665 return 0;
666}
667
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200668static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
669{
670 fmode_t mode = f.file->f_mode;
671
672 /* Our file permissions may have been overridden by global
673 * map permissions facing syscall side.
674 */
675 if (READ_ONCE(map->frozen))
676 mode &= ~FMODE_CAN_WRITE;
677 return mode;
678}
679
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100680#ifdef CONFIG_PROC_FS
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800681/* Provides an approximation of the map's memory footprint.
682 * Used only to provide a backward compatibility and display
683 * a reasonable "memlock" info.
684 */
685static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
686{
687 unsigned long size;
688
689 size = round_up(map->key_size + bpf_map_value_size(map), 8);
690
691 return round_up(map->max_entries * size, PAGE_SIZE);
692}
693
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100694static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
695{
Toke Hoiland-Jorgensenf45d5b62022-01-21 11:10:02 +0100696 struct bpf_map *map = filp->private_data;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100697 u32 type = 0, jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100698
Toke Hoiland-Jorgensenf45d5b62022-01-21 11:10:02 +0100699 if (map_type_contains_progs(map)) {
700 spin_lock(&map->owner.lock);
701 type = map->owner.type;
702 jited = map->owner.jited;
703 spin_unlock(&map->owner.lock);
Daniel Borkmann21116b72016-11-26 01:28:07 +0100704 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100705
706 seq_printf(m,
707 "map_type:\t%u\n"
708 "key_size:\t%u\n"
709 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100710 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100711 "map_flags:\t%#x\n"
Joanne Koong93309862021-10-27 16:45:00 -0700712 "map_extra:\t%#llx\n"
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800713 "memlock:\t%lu\n"
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200714 "map_id:\t%u\n"
715 "frozen:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100716 map->map_type,
717 map->key_size,
718 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100719 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100720 map->map_flags,
Joanne Koong93309862021-10-27 16:45:00 -0700721 (unsigned long long)map->map_extra,
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800722 bpf_map_memory_footprint(map),
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200723 map->id,
724 READ_ONCE(map->frozen));
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100725 if (type) {
726 seq_printf(m, "owner_prog_type:\t%u\n", type);
727 seq_printf(m, "owner_jited:\t%u\n", jited);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200728 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100729}
730#endif
731
Chenbo Feng6e71b042017-10-18 13:00:22 -0700732static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
733 loff_t *ppos)
734{
735 /* We need this handler such that alloc_file() enables
736 * f_mode with FMODE_CAN_READ.
737 */
738 return -EINVAL;
739}
740
741static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
742 size_t siz, loff_t *ppos)
743{
744 /* We need this handler such that alloc_file() enables
745 * f_mode with FMODE_CAN_WRITE.
746 */
747 return -EINVAL;
748}
749
Andrii Nakryikofc970222019-11-17 09:28:04 -0800750/* called for any extra memory-mapped regions (except initial) */
751static void bpf_map_mmap_open(struct vm_area_struct *vma)
752{
753 struct bpf_map *map = vma->vm_file->private_data;
754
Daniel Borkmann353050b2021-11-09 18:48:08 +0000755 if (vma->vm_flags & VM_MAYWRITE)
756 bpf_map_write_active_inc(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800757}
758
759/* called for all unmapped memory region (including initial) */
760static void bpf_map_mmap_close(struct vm_area_struct *vma)
761{
762 struct bpf_map *map = vma->vm_file->private_data;
763
Daniel Borkmann353050b2021-11-09 18:48:08 +0000764 if (vma->vm_flags & VM_MAYWRITE)
765 bpf_map_write_active_dec(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800766}
767
768static const struct vm_operations_struct bpf_map_default_vmops = {
769 .open = bpf_map_mmap_open,
770 .close = bpf_map_mmap_close,
771};
772
773static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
774{
775 struct bpf_map *map = filp->private_data;
776 int err;
777
Alexei Starovoitov68134662021-07-14 17:54:10 -0700778 if (!map->ops->map_mmap || map_value_has_spin_lock(map) ||
Kumar Kartikeya Dwivedi61df10c2022-04-25 03:18:49 +0530779 map_value_has_timer(map) || map_value_has_kptrs(map))
Andrii Nakryikofc970222019-11-17 09:28:04 -0800780 return -ENOTSUPP;
781
782 if (!(vma->vm_flags & VM_SHARED))
783 return -EINVAL;
784
785 mutex_lock(&map->freeze_mutex);
786
Andrii Nakryikodfeb3762020-05-18 22:38:24 -0700787 if (vma->vm_flags & VM_WRITE) {
788 if (map->frozen) {
789 err = -EPERM;
790 goto out;
791 }
792 /* map is meant to be read-only, so do not allow mapping as
793 * writable, because it's possible to leak a writable page
794 * reference and allows user-space to still modify it after
795 * freezing, while verifier will assume contents do not change
796 */
797 if (map->map_flags & BPF_F_RDONLY_PROG) {
798 err = -EACCES;
799 goto out;
800 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800801 }
802
803 /* set default open/close callbacks */
804 vma->vm_ops = &bpf_map_default_vmops;
805 vma->vm_private_data = map;
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700806 vma->vm_flags &= ~VM_MAYEXEC;
807 if (!(vma->vm_flags & VM_WRITE))
808 /* disallow re-mapping with PROT_WRITE */
809 vma->vm_flags &= ~VM_MAYWRITE;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800810
811 err = map->ops->map_mmap(map, vma);
812 if (err)
813 goto out;
814
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700815 if (vma->vm_flags & VM_MAYWRITE)
Daniel Borkmann353050b2021-11-09 18:48:08 +0000816 bpf_map_write_active_inc(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800817out:
818 mutex_unlock(&map->freeze_mutex);
819 return err;
820}
821
Andrii Nakryiko457f44362020-05-29 00:54:20 -0700822static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
823{
824 struct bpf_map *map = filp->private_data;
825
826 if (map->ops->map_poll)
827 return map->ops->map_poll(map, filp, pts);
828
829 return EPOLLERR;
830}
831
Chenbo Fengf66e4482017-10-18 13:00:26 -0700832const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100833#ifdef CONFIG_PROC_FS
834 .show_fdinfo = bpf_map_show_fdinfo,
835#endif
836 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700837 .read = bpf_dummy_read,
838 .write = bpf_dummy_write,
Andrii Nakryikofc970222019-11-17 09:28:04 -0800839 .mmap = bpf_map_mmap,
Andrii Nakryiko457f44362020-05-29 00:54:20 -0700840 .poll = bpf_map_poll,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700841};
842
Chenbo Feng6e71b042017-10-18 13:00:22 -0700843int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100844{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700845 int ret;
846
847 ret = security_bpf_map(map, OPEN_FMODE(flags));
848 if (ret < 0)
849 return ret;
850
Daniel Borkmannaa797812015-10-29 14:58:06 +0100851 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700852 flags | O_CLOEXEC);
853}
854
855int bpf_get_file_flag(int flags)
856{
857 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
858 return -EINVAL;
859 if (flags & BPF_F_RDONLY)
860 return O_RDONLY;
861 if (flags & BPF_F_WRONLY)
862 return O_WRONLY;
863 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100864}
865
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700866/* helper macro to check that unused fields 'union bpf_attr' are zero */
867#define CHECK_ATTR(CMD) \
868 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
869 sizeof(attr->CMD##_LAST_FIELD), 0, \
870 sizeof(*attr) - \
871 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
872 sizeof(attr->CMD##_LAST_FIELD)) != NULL
873
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700874/* dst and src must have at least "size" number of bytes.
875 * Return strlen on success and < 0 on error.
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700876 */
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700877int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700878{
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700879 const char *end = src + size;
880 const char *orig_src = src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700881
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700882 memset(dst, 0, size);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200883 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700884 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200885 if (!isalnum(*src) &&
886 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700887 return -EINVAL;
888 *dst++ = *src++;
889 }
890
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700891 /* No '\0' found in "size" number of bytes */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700892 if (src == end)
893 return -EINVAL;
894
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700895 return src - orig_src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700896}
897
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200898int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800899 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200900 const struct btf_type *key_type,
901 const struct btf_type *value_type)
902{
903 return -ENOTSUPP;
904}
905
Kumar Kartikeya Dwivedi4d7d7f62022-04-25 03:18:53 +0530906static int map_off_arr_cmp(const void *_a, const void *_b, const void *priv)
907{
908 const u32 a = *(const u32 *)_a;
909 const u32 b = *(const u32 *)_b;
910
911 if (a < b)
912 return -1;
913 else if (a > b)
914 return 1;
915 return 0;
916}
917
918static void map_off_arr_swap(void *_a, void *_b, int size, const void *priv)
919{
920 struct bpf_map *map = (struct bpf_map *)priv;
921 u32 *off_base = map->off_arr->field_off;
922 u32 *a = _a, *b = _b;
923 u8 *sz_a, *sz_b;
924
925 sz_a = map->off_arr->field_sz + (a - off_base);
926 sz_b = map->off_arr->field_sz + (b - off_base);
927
928 swap(*a, *b);
929 swap(*sz_a, *sz_b);
930}
931
932static int bpf_map_alloc_off_arr(struct bpf_map *map)
933{
934 bool has_spin_lock = map_value_has_spin_lock(map);
935 bool has_timer = map_value_has_timer(map);
936 bool has_kptrs = map_value_has_kptrs(map);
937 struct bpf_map_off_arr *off_arr;
938 u32 i;
939
940 if (!has_spin_lock && !has_timer && !has_kptrs) {
941 map->off_arr = NULL;
942 return 0;
943 }
944
945 off_arr = kmalloc(sizeof(*map->off_arr), GFP_KERNEL | __GFP_NOWARN);
946 if (!off_arr)
947 return -ENOMEM;
948 map->off_arr = off_arr;
949
950 off_arr->cnt = 0;
951 if (has_spin_lock) {
952 i = off_arr->cnt;
953
954 off_arr->field_off[i] = map->spin_lock_off;
955 off_arr->field_sz[i] = sizeof(struct bpf_spin_lock);
956 off_arr->cnt++;
957 }
958 if (has_timer) {
959 i = off_arr->cnt;
960
961 off_arr->field_off[i] = map->timer_off;
962 off_arr->field_sz[i] = sizeof(struct bpf_timer);
963 off_arr->cnt++;
964 }
965 if (has_kptrs) {
966 struct bpf_map_value_off *tab = map->kptr_off_tab;
967 u32 *off = &off_arr->field_off[off_arr->cnt];
968 u8 *sz = &off_arr->field_sz[off_arr->cnt];
969
970 for (i = 0; i < tab->nr_off; i++) {
971 *off++ = tab->off[i].offset;
972 *sz++ = sizeof(u64);
973 }
974 off_arr->cnt += tab->nr_off;
975 }
976
977 if (off_arr->cnt == 1)
978 return 0;
979 sort_r(off_arr->field_off, off_arr->cnt, sizeof(off_arr->field_off[0]),
980 map_off_arr_cmp, map_off_arr_swap, map);
981 return 0;
982}
983
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800984static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200985 u32 btf_key_id, u32 btf_value_id)
986{
987 const struct btf_type *key_type, *value_type;
988 u32 key_size, value_size;
989 int ret = 0;
990
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200991 /* Some maps allow key to be unspecified. */
992 if (btf_key_id) {
993 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
994 if (!key_type || key_size != map->key_size)
995 return -EINVAL;
996 } else {
997 key_type = btf_type_by_id(btf, 0);
998 if (!map->ops->map_check_btf)
999 return -EINVAL;
1000 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +02001001
1002 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
1003 if (!value_type || value_size != map->value_size)
1004 return -EINVAL;
1005
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08001006 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
1007
1008 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +02001009 if (map->map_flags & BPF_F_RDONLY_PROG)
1010 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08001011 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -08001012 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -07001013 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
KP Singh8ea63682020-08-25 20:29:17 +02001014 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
KP Singh4cf1bc12020-11-06 10:37:40 +00001015 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
1016 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -08001017 return -ENOTSUPP;
1018 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
1019 map->value_size) {
1020 WARN_ONCE(1,
1021 "verifier bug spin_lock_off %d value_size %d\n",
1022 map->spin_lock_off, map->value_size);
1023 return -EFAULT;
1024 }
1025 }
1026
Alexei Starovoitov68134662021-07-14 17:54:10 -07001027 map->timer_off = btf_find_timer(btf, value_type);
1028 if (map_value_has_timer(map)) {
1029 if (map->map_flags & BPF_F_RDONLY_PROG)
1030 return -EACCES;
1031 if (map->map_type != BPF_MAP_TYPE_HASH &&
1032 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1033 map->map_type != BPF_MAP_TYPE_ARRAY)
1034 return -EOPNOTSUPP;
1035 }
1036
Kumar Kartikeya Dwivedi61df10c2022-04-25 03:18:49 +05301037 map->kptr_off_tab = btf_parse_kptrs(btf, value_type);
1038 if (map_value_has_kptrs(map)) {
1039 if (!bpf_capable()) {
1040 ret = -EPERM;
1041 goto free_map_tab;
1042 }
1043 if (map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) {
1044 ret = -EACCES;
1045 goto free_map_tab;
1046 }
1047 if (map->map_type != BPF_MAP_TYPE_HASH &&
1048 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1049 map->map_type != BPF_MAP_TYPE_ARRAY) {
1050 ret = -EOPNOTSUPP;
1051 goto free_map_tab;
1052 }
1053 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +02001054
Kumar Kartikeya Dwivedi61df10c2022-04-25 03:18:49 +05301055 if (map->ops->map_check_btf) {
1056 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
1057 if (ret < 0)
1058 goto free_map_tab;
1059 }
1060
1061 return ret;
1062free_map_tab:
1063 bpf_map_free_kptr_off_tab(map);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +02001064 return ret;
1065}
1066
Joanne Koong93309862021-10-27 16:45:00 -07001067#define BPF_MAP_CREATE_LAST_FIELD map_extra
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001068/* called via syscall */
1069static int map_create(union bpf_attr *attr)
1070{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -07001071 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001072 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -07001073 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001074 int err;
1075
1076 err = CHECK_ATTR(BPF_MAP_CREATE);
1077 if (err)
1078 return -EINVAL;
1079
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001080 if (attr->btf_vmlinux_value_type_id) {
1081 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
1082 attr->btf_key_type_id || attr->btf_value_type_id)
1083 return -EINVAL;
1084 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
1085 return -EINVAL;
1086 }
1087
Joanne Koong93309862021-10-27 16:45:00 -07001088 if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER &&
1089 attr->map_extra != 0)
1090 return -EINVAL;
1091
Chenbo Feng6e71b042017-10-18 13:00:22 -07001092 f_flags = bpf_get_file_flag(attr->map_flags);
1093 if (f_flags < 0)
1094 return f_flags;
1095
Martin KaFai Lau96eabe72017-08-18 11:28:00 -07001096 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -07001097 ((unsigned int)numa_node >= nr_node_ids ||
1098 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -07001099 return -EINVAL;
1100
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001101 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
1102 map = find_and_alloc_map(attr);
1103 if (IS_ERR(map))
1104 return PTR_ERR(map);
1105
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -07001106 err = bpf_obj_name_cpy(map->name, attr->map_name,
1107 sizeof(attr->map_name));
1108 if (err < 0)
Roman Gushchinb936ca62019-05-29 18:03:58 -07001109 goto free_map;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07001110
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001111 atomic64_set(&map->refcnt, 1);
1112 atomic64_set(&map->usercnt, 1);
Andrii Nakryikofc970222019-11-17 09:28:04 -08001113 mutex_init(&map->freeze_mutex);
Toke Hoiland-Jorgensenf45d5b62022-01-21 11:10:02 +01001114 spin_lock_init(&map->owner.lock);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001115
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001116 map->spin_lock_off = -EINVAL;
Alexei Starovoitov68134662021-07-14 17:54:10 -07001117 map->timer_off = -EINVAL;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001118 if (attr->btf_key_type_id || attr->btf_value_type_id ||
1119 /* Even the map's value is a kernel's struct,
1120 * the bpf_prog.o must have BTF to begin with
1121 * to figure out the corresponding kernel's
1122 * counter part. Thus, attr->btf_fd has
1123 * to be valid also.
1124 */
1125 attr->btf_vmlinux_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -07001126 struct btf *btf;
1127
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -07001128 btf = btf_get_by_fd(attr->btf_fd);
1129 if (IS_ERR(btf)) {
1130 err = PTR_ERR(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -07001131 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -07001132 }
Alexei Starovoitov350a5c42021-03-07 14:52:48 -08001133 if (btf_is_kernel(btf)) {
1134 btf_put(btf);
1135 err = -EACCES;
1136 goto free_map;
1137 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001138 map->btf = btf;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -07001139
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001140 if (attr->btf_value_type_id) {
1141 err = map_check_btf(map, btf, attr->btf_key_type_id,
1142 attr->btf_value_type_id);
1143 if (err)
1144 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -07001145 }
1146
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07001147 map->btf_key_type_id = attr->btf_key_type_id;
1148 map->btf_value_type_id = attr->btf_value_type_id;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001149 map->btf_vmlinux_value_type_id =
1150 attr->btf_vmlinux_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -07001151 }
1152
Kumar Kartikeya Dwivedi4d7d7f62022-04-25 03:18:53 +05301153 err = bpf_map_alloc_off_arr(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001154 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -07001155 goto free_map;
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001156
Kumar Kartikeya Dwivedi4d7d7f62022-04-25 03:18:53 +05301157 err = security_bpf_map_alloc(map);
1158 if (err)
1159 goto free_map_off_arr;
1160
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -07001161 err = bpf_map_alloc_id(map);
1162 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -07001163 goto free_map_sec;
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -07001164
Roman Gushchin48edc1f2020-12-01 13:58:32 -08001165 bpf_map_save_memcg(map);
1166
Chenbo Feng6e71b042017-10-18 13:00:22 -07001167 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001168 if (err < 0) {
1169 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +08001170 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001171 * bpf_map_alloc_id() has published the map
1172 * to the userspace and the userspace may
1173 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
1174 */
Peng Sun352d20d2019-02-27 22:36:25 +08001175 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001176 return err;
1177 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001178
1179 return err;
1180
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001181free_map_sec:
1182 security_bpf_map_free(map);
Kumar Kartikeya Dwivedi4d7d7f62022-04-25 03:18:53 +05301183free_map_off_arr:
1184 kfree(map->off_arr);
Roman Gushchinb936ca62019-05-29 18:03:58 -07001185free_map:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -07001186 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07001187 map->ops->map_free(map);
1188 return err;
1189}
1190
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001191/* if error is returned, fd is released.
1192 * On success caller should complete fd access with matching fdput()
1193 */
Daniel Borkmannc2101292015-10-29 14:58:07 +01001194struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001195{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001196 if (!f.file)
1197 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001198 if (f.file->f_op != &bpf_map_fops) {
1199 fdput(f);
1200 return ERR_PTR(-EINVAL);
1201 }
1202
Daniel Borkmannc2101292015-10-29 14:58:07 +01001203 return f.file->private_data;
1204}
1205
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001206void bpf_map_inc(struct bpf_map *map)
Daniel Borkmannc9da1612015-11-24 21:28:15 +01001207{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001208 atomic64_inc(&map->refcnt);
Daniel Borkmannc9da1612015-11-24 21:28:15 +01001209}
Jakub Kicinski630a4d32018-05-03 18:37:09 -07001210EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +01001211
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001212void bpf_map_inc_with_uref(struct bpf_map *map)
1213{
1214 atomic64_inc(&map->refcnt);
1215 atomic64_inc(&map->usercnt);
1216}
1217EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
1218
Martin KaFai Lau1ed4d922020-02-25 15:04:21 -08001219struct bpf_map *bpf_map_get(u32 ufd)
1220{
1221 struct fd f = fdget(ufd);
1222 struct bpf_map *map;
1223
1224 map = __bpf_map_get(f);
1225 if (IS_ERR(map))
1226 return map;
1227
1228 bpf_map_inc(map);
1229 fdput(f);
1230
1231 return map;
1232}
Alexei Starovoitovb1d18a72022-02-09 15:19:57 -08001233EXPORT_SYMBOL(bpf_map_get);
Martin KaFai Lau1ed4d922020-02-25 15:04:21 -08001234
Daniel Borkmannc9da1612015-11-24 21:28:15 +01001235struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +01001236{
1237 struct fd f = fdget(ufd);
1238 struct bpf_map *map;
1239
1240 map = __bpf_map_get(f);
1241 if (IS_ERR(map))
1242 return map;
1243
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001244 bpf_map_inc_with_uref(map);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001245 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001246
1247 return map;
1248}
1249
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001250/* map_idr_lock should have been held */
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001251static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001252{
1253 int refold;
1254
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001255 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001256 if (!refold)
1257 return ERR_PTR(-ENOENT);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001258 if (uref)
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001259 atomic64_inc(&map->usercnt);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07001260
1261 return map;
1262}
1263
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001264struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
Stanislav Fomichevb0e4701c2019-08-14 10:37:48 -07001265{
1266 spin_lock_bh(&map_idr_lock);
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -08001267 map = __bpf_map_inc_not_zero(map, false);
Stanislav Fomichevb0e4701c2019-08-14 10:37:48 -07001268 spin_unlock_bh(&map_idr_lock);
1269
1270 return map;
1271}
1272EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
1273
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -08001274int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1275{
1276 return -ENOTSUPP;
1277}
1278
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001279static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1280{
1281 if (key_size)
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001282 return vmemdup_user(ukey, key_size);
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001283
1284 if (ukey)
1285 return ERR_PTR(-EINVAL);
1286
1287 return NULL;
1288}
1289
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001290static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1291{
1292 if (key_size)
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001293 return kvmemdup_bpfptr(ukey, key_size);
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001294
1295 if (!bpfptr_is_null(ukey))
1296 return ERR_PTR(-EINVAL);
1297
1298 return NULL;
1299}
1300
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001301/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001302#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001303
1304static int map_lookup_elem(union bpf_attr *attr)
1305{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001306 void __user *ukey = u64_to_user_ptr(attr->key);
1307 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001308 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001309 struct bpf_map *map;
Brian Vazquez15c14a32020-01-15 10:43:00 -08001310 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001311 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001312 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001313 int err;
1314
1315 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1316 return -EINVAL;
1317
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001318 if (attr->flags & ~BPF_F_LOCK)
1319 return -EINVAL;
1320
Daniel Borkmann592867b2015-09-08 18:00:09 +02001321 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001322 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001323 if (IS_ERR(map))
1324 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001325 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001326 err = -EPERM;
1327 goto err_put;
1328 }
1329
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001330 if ((attr->flags & BPF_F_LOCK) &&
1331 !map_value_has_spin_lock(map)) {
1332 err = -EINVAL;
1333 goto err_put;
1334 }
1335
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001336 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001337 if (IS_ERR(key)) {
1338 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001339 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001340 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001341
Brian Vazquez15c14a32020-01-15 10:43:00 -08001342 value_size = bpf_map_value_size(map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001343
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001344 err = -ENOMEM;
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001345 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001346 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001347 goto free_key;
1348
Joanne Koong93309862021-10-27 16:45:00 -07001349 if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
1350 if (copy_from_user(value, uvalue, value_size))
1351 err = -EFAULT;
1352 else
1353 err = bpf_map_copy_value(map, key, value, attr->flags);
1354 goto free_value;
1355 }
1356
Brian Vazquez15c14a32020-01-15 10:43:00 -08001357 err = bpf_map_copy_value(map, key, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001358 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001359 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001360
1361 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001362 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001363 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001364
1365 err = 0;
1366
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001367free_value:
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001368 kvfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001369free_key:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001370 kvfree(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001371err_put:
1372 fdput(f);
1373 return err;
1374}
1375
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001376
Alexei Starovoitov3274f522014-11-13 17:36:44 -08001377#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001378
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001379static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001380{
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001381 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1382 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001383 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001384 struct bpf_map *map;
1385 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001386 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001387 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001388 int err;
1389
1390 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1391 return -EINVAL;
1392
Daniel Borkmann592867b2015-09-08 18:00:09 +02001393 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001394 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001395 if (IS_ERR(map))
1396 return PTR_ERR(map);
Daniel Borkmann353050b2021-11-09 18:48:08 +00001397 bpf_map_write_active_inc(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001398 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001399 err = -EPERM;
1400 goto err_put;
1401 }
1402
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001403 if ((attr->flags & BPF_F_LOCK) &&
1404 !map_value_has_spin_lock(map)) {
1405 err = -EINVAL;
1406 goto err_put;
1407 }
1408
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001409 key = ___bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001410 if (IS_ERR(key)) {
1411 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001412 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001413 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001414
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001415 value_size = bpf_map_value_size(map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001416
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001417 err = -ENOMEM;
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001418 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001419 if (!value)
1420 goto free_key;
1421
1422 err = -EFAULT;
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001423 if (copy_from_bpfptr(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001424 goto free_value;
1425
Brian Vazquez15c14a32020-01-15 10:43:00 -08001426 err = bpf_map_update_value(map, f, key, value, attr->flags);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001427
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001428free_value:
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001429 kvfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001430free_key:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001431 kvfree(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001432err_put:
Daniel Borkmann353050b2021-11-09 18:48:08 +00001433 bpf_map_write_active_dec(map);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001434 fdput(f);
1435 return err;
1436}
1437
1438#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1439
1440static int map_delete_elem(union bpf_attr *attr)
1441{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001442 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001443 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001444 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001445 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001446 void *key;
1447 int err;
1448
1449 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1450 return -EINVAL;
1451
Daniel Borkmann592867b2015-09-08 18:00:09 +02001452 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001453 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001454 if (IS_ERR(map))
1455 return PTR_ERR(map);
Daniel Borkmann353050b2021-11-09 18:48:08 +00001456 bpf_map_write_active_inc(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001457 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001458 err = -EPERM;
1459 goto err_put;
1460 }
1461
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001462 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001463 if (IS_ERR(key)) {
1464 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001465 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001466 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001467
Jakub Kicinskia3884572018-01-11 20:29:09 -08001468 if (bpf_map_is_dev_bound(map)) {
1469 err = bpf_map_offload_delete_elem(map, key);
1470 goto out;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001471 } else if (IS_FD_PROG_ARRAY(map) ||
1472 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1473 /* These maps require sleepable context */
Daniel Borkmannda765a22019-11-22 21:07:58 +01001474 err = map->ops->map_delete_elem(map, key);
1475 goto out;
Jakub Kicinskia3884572018-01-11 20:29:09 -08001476 }
1477
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001478 bpf_disable_instrumentation();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001479 rcu_read_lock();
1480 err = map->ops->map_delete_elem(map, key);
1481 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001482 bpf_enable_instrumentation();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001483 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001484out:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001485 kvfree(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001486err_put:
Daniel Borkmann353050b2021-11-09 18:48:08 +00001487 bpf_map_write_active_dec(map);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001488 fdput(f);
1489 return err;
1490}
1491
1492/* last field in 'union bpf_attr' used by this command */
1493#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1494
1495static int map_get_next_key(union bpf_attr *attr)
1496{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001497 void __user *ukey = u64_to_user_ptr(attr->key);
1498 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001499 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001500 struct bpf_map *map;
1501 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001502 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001503 int err;
1504
1505 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1506 return -EINVAL;
1507
Daniel Borkmann592867b2015-09-08 18:00:09 +02001508 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001509 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001510 if (IS_ERR(map))
1511 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001512 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001513 err = -EPERM;
1514 goto err_put;
1515 }
1516
Teng Qin8fe45922017-04-24 19:00:37 -07001517 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001518 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001519 if (IS_ERR(key)) {
1520 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001521 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001522 }
Teng Qin8fe45922017-04-24 19:00:37 -07001523 } else {
1524 key = NULL;
1525 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001526
1527 err = -ENOMEM;
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001528 next_key = kvmalloc(map->key_size, GFP_USER);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001529 if (!next_key)
1530 goto free_key;
1531
Jakub Kicinskia3884572018-01-11 20:29:09 -08001532 if (bpf_map_is_dev_bound(map)) {
1533 err = bpf_map_offload_get_next_key(map, key, next_key);
1534 goto out;
1535 }
1536
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001537 rcu_read_lock();
1538 err = map->ops->map_get_next_key(map, key, next_key);
1539 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001540out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001541 if (err)
1542 goto free_next_key;
1543
1544 err = -EFAULT;
1545 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1546 goto free_next_key;
1547
1548 err = 0;
1549
1550free_next_key:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001551 kvfree(next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001552free_key:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001553 kvfree(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001554err_put:
1555 fdput(f);
1556 return err;
1557}
1558
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001559int generic_map_delete_batch(struct bpf_map *map,
1560 const union bpf_attr *attr,
1561 union bpf_attr __user *uattr)
1562{
1563 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1564 u32 cp, max_count;
1565 int err = 0;
1566 void *key;
1567
1568 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1569 return -EINVAL;
1570
1571 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1572 !map_value_has_spin_lock(map)) {
1573 return -EINVAL;
1574 }
1575
1576 max_count = attr->batch.count;
1577 if (!max_count)
1578 return 0;
1579
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001580 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001581 if (!key)
1582 return -ENOMEM;
1583
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001584 for (cp = 0; cp < max_count; cp++) {
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001585 err = -EFAULT;
1586 if (copy_from_user(key, keys + cp * map->key_size,
1587 map->key_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001588 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001589
1590 if (bpf_map_is_dev_bound(map)) {
1591 err = bpf_map_offload_delete_elem(map, key);
1592 break;
1593 }
1594
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001595 bpf_disable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001596 rcu_read_lock();
1597 err = map->ops->map_delete_elem(map, key);
1598 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001599 bpf_enable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001600 if (err)
1601 break;
Eric Dumazet75134f12022-02-17 10:19:02 -08001602 cond_resched();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001603 }
1604 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1605 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001606
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001607 kvfree(key);
Eric Dumazet9087c6f2022-02-18 10:18:01 -08001608
1609 maybe_wait_bpf_programs(map);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001610 return err;
1611}
1612
1613int generic_map_update_batch(struct bpf_map *map,
1614 const union bpf_attr *attr,
1615 union bpf_attr __user *uattr)
1616{
1617 void __user *values = u64_to_user_ptr(attr->batch.values);
1618 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1619 u32 value_size, cp, max_count;
Xu Kuohaifda7a382021-10-19 03:29:34 +00001620 int ufd = attr->batch.map_fd;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001621 void *key, *value;
1622 struct fd f;
1623 int err = 0;
1624
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001625 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1626 return -EINVAL;
1627
1628 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1629 !map_value_has_spin_lock(map)) {
1630 return -EINVAL;
1631 }
1632
1633 value_size = bpf_map_value_size(map);
1634
1635 max_count = attr->batch.count;
1636 if (!max_count)
1637 return 0;
1638
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001639 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001640 if (!key)
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001641 return -ENOMEM;
1642
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001643 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001644 if (!value) {
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001645 kvfree(key);
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001646 return -ENOMEM;
1647 }
1648
Xu Kuohaifda7a382021-10-19 03:29:34 +00001649 f = fdget(ufd); /* bpf_map_do_batch() guarantees ufd is valid */
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001650 for (cp = 0; cp < max_count; cp++) {
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001651 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001652 if (copy_from_user(key, keys + cp * map->key_size,
1653 map->key_size) ||
1654 copy_from_user(value, values + cp * value_size, value_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001655 break;
1656
1657 err = bpf_map_update_value(map, f, key, value,
1658 attr->batch.elem_flags);
1659
1660 if (err)
1661 break;
Eric Dumazet75134f12022-02-17 10:19:02 -08001662 cond_resched();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001663 }
1664
1665 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1666 err = -EFAULT;
1667
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001668 kvfree(value);
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001669 kvfree(key);
Xu Kuohaifda7a382021-10-19 03:29:34 +00001670 fdput(f);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001671 return err;
1672}
1673
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001674#define MAP_LOOKUP_RETRIES 3
1675
1676int generic_map_lookup_batch(struct bpf_map *map,
1677 const union bpf_attr *attr,
1678 union bpf_attr __user *uattr)
1679{
1680 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1681 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1682 void __user *values = u64_to_user_ptr(attr->batch.values);
1683 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1684 void *buf, *buf_prevkey, *prev_key, *key, *value;
1685 int err, retry = MAP_LOOKUP_RETRIES;
1686 u32 value_size, cp, max_count;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001687
1688 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1689 return -EINVAL;
1690
1691 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1692 !map_value_has_spin_lock(map))
1693 return -EINVAL;
1694
1695 value_size = bpf_map_value_size(map);
1696
1697 max_count = attr->batch.count;
1698 if (!max_count)
1699 return 0;
1700
1701 if (put_user(0, &uattr->batch.count))
1702 return -EFAULT;
1703
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001704 buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001705 if (!buf_prevkey)
1706 return -ENOMEM;
1707
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001708 buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001709 if (!buf) {
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001710 kvfree(buf_prevkey);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001711 return -ENOMEM;
1712 }
1713
1714 err = -EFAULT;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001715 prev_key = NULL;
1716 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1717 goto free_buf;
1718 key = buf;
1719 value = key + map->key_size;
1720 if (ubatch)
1721 prev_key = buf_prevkey;
1722
1723 for (cp = 0; cp < max_count;) {
1724 rcu_read_lock();
1725 err = map->ops->map_get_next_key(map, prev_key, key);
1726 rcu_read_unlock();
1727 if (err)
1728 break;
1729 err = bpf_map_copy_value(map, key, value,
1730 attr->batch.elem_flags);
1731
1732 if (err == -ENOENT) {
1733 if (retry) {
1734 retry--;
1735 continue;
1736 }
1737 err = -EINTR;
1738 break;
1739 }
1740
1741 if (err)
1742 goto free_buf;
1743
1744 if (copy_to_user(keys + cp * map->key_size, key,
1745 map->key_size)) {
1746 err = -EFAULT;
1747 goto free_buf;
1748 }
1749 if (copy_to_user(values + cp * value_size, value, value_size)) {
1750 err = -EFAULT;
1751 goto free_buf;
1752 }
1753
1754 if (!prev_key)
1755 prev_key = buf_prevkey;
1756
1757 swap(prev_key, key);
1758 retry = MAP_LOOKUP_RETRIES;
1759 cp++;
Eric Dumazet75134f12022-02-17 10:19:02 -08001760 cond_resched();
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001761 }
1762
1763 if (err == -EFAULT)
1764 goto free_buf;
1765
1766 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1767 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1768 err = -EFAULT;
1769
1770free_buf:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001771 kvfree(buf_prevkey);
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001772 kvfree(buf);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001773 return err;
1774}
1775
Denis Salopek3e87f192021-05-11 23:00:04 +02001776#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001777
1778static int map_lookup_and_delete_elem(union bpf_attr *attr)
1779{
1780 void __user *ukey = u64_to_user_ptr(attr->key);
1781 void __user *uvalue = u64_to_user_ptr(attr->value);
1782 int ufd = attr->map_fd;
1783 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001784 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001785 u32 value_size;
1786 struct fd f;
1787 int err;
1788
1789 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1790 return -EINVAL;
1791
Denis Salopek3e87f192021-05-11 23:00:04 +02001792 if (attr->flags & ~BPF_F_LOCK)
1793 return -EINVAL;
1794
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001795 f = fdget(ufd);
1796 map = __bpf_map_get(f);
1797 if (IS_ERR(map))
1798 return PTR_ERR(map);
Daniel Borkmann353050b2021-11-09 18:48:08 +00001799 bpf_map_write_active_inc(map);
Anton Protopopov1ea0f912020-05-27 18:56:59 +00001800 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1801 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001802 err = -EPERM;
1803 goto err_put;
1804 }
1805
Denis Salopek3e87f192021-05-11 23:00:04 +02001806 if (attr->flags &&
1807 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1808 map->map_type == BPF_MAP_TYPE_STACK)) {
1809 err = -EINVAL;
1810 goto err_put;
1811 }
1812
1813 if ((attr->flags & BPF_F_LOCK) &&
1814 !map_value_has_spin_lock(map)) {
1815 err = -EINVAL;
1816 goto err_put;
1817 }
1818
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001819 key = __bpf_copy_key(ukey, map->key_size);
1820 if (IS_ERR(key)) {
1821 err = PTR_ERR(key);
1822 goto err_put;
1823 }
1824
Denis Salopek3e87f192021-05-11 23:00:04 +02001825 value_size = bpf_map_value_size(map);
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001826
1827 err = -ENOMEM;
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001828 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001829 if (!value)
1830 goto free_key;
1831
Denis Salopek3e87f192021-05-11 23:00:04 +02001832 err = -ENOTSUPP;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001833 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1834 map->map_type == BPF_MAP_TYPE_STACK) {
1835 err = map->ops->map_pop_elem(map, value);
Denis Salopek3e87f192021-05-11 23:00:04 +02001836 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1837 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1838 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1839 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1840 if (!bpf_map_is_dev_bound(map)) {
1841 bpf_disable_instrumentation();
1842 rcu_read_lock();
1843 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1844 rcu_read_unlock();
1845 bpf_enable_instrumentation();
1846 }
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001847 }
1848
1849 if (err)
1850 goto free_value;
1851
Wei Yongjun7f645462020-04-30 08:18:51 +00001852 if (copy_to_user(uvalue, value, value_size) != 0) {
1853 err = -EFAULT;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001854 goto free_value;
Wei Yongjun7f645462020-04-30 08:18:51 +00001855 }
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001856
1857 err = 0;
1858
1859free_value:
Stanislav Fomichevf0dce1d2021-08-18 16:52:15 -07001860 kvfree(value);
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001861free_key:
Stanislav Fomichev44779a4b2021-08-18 16:52:16 -07001862 kvfree(key);
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001863err_put:
Daniel Borkmann353050b2021-11-09 18:48:08 +00001864 bpf_map_write_active_dec(map);
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001865 fdput(f);
1866 return err;
1867}
1868
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001869#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1870
1871static int map_freeze(const union bpf_attr *attr)
1872{
1873 int err = 0, ufd = attr->map_fd;
1874 struct bpf_map *map;
1875 struct fd f;
1876
1877 if (CHECK_ATTR(BPF_MAP_FREEZE))
1878 return -EINVAL;
1879
1880 f = fdget(ufd);
1881 map = __bpf_map_get(f);
1882 if (IS_ERR(map))
1883 return PTR_ERR(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -08001884
Alexei Starovoitov68134662021-07-14 17:54:10 -07001885 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS ||
Kumar Kartikeya Dwivedi61df10c2022-04-25 03:18:49 +05301886 map_value_has_timer(map) || map_value_has_kptrs(map)) {
Martin KaFai Lau849b4d92020-03-04 17:34:54 -08001887 fdput(f);
1888 return -ENOTSUPP;
1889 }
1890
Andrii Nakryikofc970222019-11-17 09:28:04 -08001891 mutex_lock(&map->freeze_mutex);
Daniel Borkmann353050b2021-11-09 18:48:08 +00001892 if (bpf_map_write_active(map)) {
Andrii Nakryikofc970222019-11-17 09:28:04 -08001893 err = -EBUSY;
1894 goto err_put;
1895 }
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001896 if (READ_ONCE(map->frozen)) {
1897 err = -EBUSY;
1898 goto err_put;
1899 }
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07001900 if (!bpf_capable()) {
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001901 err = -EPERM;
1902 goto err_put;
1903 }
1904
1905 WRITE_ONCE(map->frozen, true);
1906err_put:
Andrii Nakryikofc970222019-11-17 09:28:04 -08001907 mutex_unlock(&map->freeze_mutex);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001908 fdput(f);
1909 return err;
1910}
1911
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001912static const struct bpf_prog_ops * const bpf_prog_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001913#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001914 [_id] = & _name ## _prog_ops,
1915#define BPF_MAP_TYPE(_id, _ops)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001916#define BPF_LINK_TYPE(_id, _name)
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001917#include <linux/bpf_types.h>
1918#undef BPF_PROG_TYPE
1919#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001920#undef BPF_LINK_TYPE
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001921};
1922
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001923static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1924{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001925 const struct bpf_prog_ops *ops;
1926
1927 if (type >= ARRAY_SIZE(bpf_prog_types))
1928 return -EINVAL;
1929 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1930 ops = bpf_prog_types[type];
1931 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001932 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001933
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001934 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001935 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001936 else
1937 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001938 prog->type = type;
1939 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001940}
1941
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001942enum bpf_audit {
1943 BPF_AUDIT_LOAD,
1944 BPF_AUDIT_UNLOAD,
1945 BPF_AUDIT_MAX,
1946};
1947
1948static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1949 [BPF_AUDIT_LOAD] = "LOAD",
1950 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1951};
1952
1953static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1954{
1955 struct audit_context *ctx = NULL;
1956 struct audit_buffer *ab;
1957
1958 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1959 return;
1960 if (audit_enabled == AUDIT_OFF)
1961 return;
1962 if (op == BPF_AUDIT_LOAD)
1963 ctx = audit_context();
1964 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1965 if (unlikely(!ab))
1966 return;
1967 audit_log_format(ab, "prog-id=%u op=%s",
1968 prog->aux->id, bpf_audit_str[op]);
1969 audit_log_end(ab);
1970}
1971
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001972static int bpf_prog_alloc_id(struct bpf_prog *prog)
1973{
1974 int id;
1975
Shaohua Lib76354c2018-03-27 11:53:21 -07001976 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001977 spin_lock_bh(&prog_idr_lock);
1978 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1979 if (id > 0)
1980 prog->aux->id = id;
1981 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001982 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001983
1984 /* id is in [1, INT_MAX) */
1985 if (WARN_ON_ONCE(!id))
1986 return -ENOSPC;
1987
1988 return id > 0 ? 0 : id;
1989}
1990
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001991void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001992{
Alexei Starovoitovd809e132021-07-14 17:54:07 -07001993 unsigned long flags;
1994
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001995 /* cBPF to eBPF migrations are currently not in the idr store.
1996 * Offloaded programs are removed from the store when their device
1997 * disappears - even if someone grabs an fd to them they are unusable,
1998 * simply waiting for refcnt to drop to be freed.
1999 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07002000 if (!prog->aux->id)
2001 return;
2002
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002003 if (do_idr_lock)
Alexei Starovoitovd809e132021-07-14 17:54:07 -07002004 spin_lock_irqsave(&prog_idr_lock, flags);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002005 else
2006 __acquire(&prog_idr_lock);
2007
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07002008 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08002009 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002010
2011 if (do_idr_lock)
Alexei Starovoitovd809e132021-07-14 17:54:07 -07002012 spin_unlock_irqrestore(&prog_idr_lock, flags);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002013 else
2014 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07002015}
2016
Daniel Borkmann1aacde32016-06-30 17:24:43 +02002017static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07002018{
2019 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
2020
Daniel Borkmann3b4d9eb2019-10-22 23:30:38 +02002021 kvfree(aux->func_info);
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08002022 kfree(aux->func_info_aux);
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002023 free_uid(aux->user);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002024 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07002025 bpf_prog_free(aux->prog);
2026}
2027
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002028static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
2029{
2030 bpf_prog_kallsyms_del_all(prog);
2031 btf_put(prog->aux->btf);
Martin KaFai Laue16301f2021-03-24 18:51:30 -07002032 kvfree(prog->aux->jited_linfo);
2033 kvfree(prog->aux->linfo);
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07002034 kfree(prog->aux->kfunc_tab);
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08002035 if (prog->aux->attach_btf)
2036 btf_put(prog->aux->attach_btf);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002037
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002038 if (deferred) {
2039 if (prog->aux->sleepable)
2040 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
2041 else
2042 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
2043 } else {
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002044 __bpf_prog_put_rcu(&prog->aux->rcu);
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002045 }
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002046}
2047
Alexei Starovoitovd809e132021-07-14 17:54:07 -07002048static void bpf_prog_put_deferred(struct work_struct *work)
2049{
2050 struct bpf_prog_aux *aux;
2051 struct bpf_prog *prog;
2052
2053 aux = container_of(work, struct bpf_prog_aux, work);
2054 prog = aux->prog;
2055 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
2056 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
2057 __bpf_prog_put_noref(prog, true);
2058}
2059
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002060static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002061{
Alexei Starovoitovd809e132021-07-14 17:54:07 -07002062 struct bpf_prog_aux *aux = prog->aux;
2063
2064 if (atomic64_dec_and_test(&aux->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002065 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002066 bpf_prog_free_id(prog, do_idr_lock);
Alexei Starovoitovd809e132021-07-14 17:54:07 -07002067
2068 if (in_irq() || irqs_disabled()) {
2069 INIT_WORK(&aux->work, bpf_prog_put_deferred);
2070 schedule_work(&aux->work);
2071 } else {
2072 bpf_prog_put_deferred(&aux->work);
2073 }
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01002074 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002075}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002076
2077void bpf_prog_put(struct bpf_prog *prog)
2078{
2079 __bpf_prog_put(prog, true);
2080}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01002081EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002082
2083static int bpf_prog_release(struct inode *inode, struct file *filp)
2084{
2085 struct bpf_prog *prog = filp->private_data;
2086
Daniel Borkmann1aacde32016-06-30 17:24:43 +02002087 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002088 return 0;
2089}
2090
Eric Dumazet61a0aba2021-10-26 14:41:33 -07002091struct bpf_prog_kstats {
2092 u64 nsecs;
2093 u64 cnt;
2094 u64 misses;
2095};
2096
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08002097static void bpf_prog_get_stats(const struct bpf_prog *prog,
Eric Dumazet61a0aba2021-10-26 14:41:33 -07002098 struct bpf_prog_kstats *stats)
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08002099{
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08002100 u64 nsecs = 0, cnt = 0, misses = 0;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08002101 int cpu;
2102
2103 for_each_possible_cpu(cpu) {
2104 const struct bpf_prog_stats *st;
2105 unsigned int start;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08002106 u64 tnsecs, tcnt, tmisses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08002107
Alexei Starovoitov700d4792021-02-09 19:36:26 -08002108 st = per_cpu_ptr(prog->stats, cpu);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08002109 do {
2110 start = u64_stats_fetch_begin_irq(&st->syncp);
Eric Dumazet61a0aba2021-10-26 14:41:33 -07002111 tnsecs = u64_stats_read(&st->nsecs);
2112 tcnt = u64_stats_read(&st->cnt);
2113 tmisses = u64_stats_read(&st->misses);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08002114 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
2115 nsecs += tnsecs;
2116 cnt += tcnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08002117 misses += tmisses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08002118 }
2119 stats->nsecs = nsecs;
2120 stats->cnt = cnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08002121 stats->misses = misses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08002122}
2123
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01002124#ifdef CONFIG_PROC_FS
2125static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
2126{
2127 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01002128 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Eric Dumazet61a0aba2021-10-26 14:41:33 -07002129 struct bpf_prog_kstats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01002130
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08002131 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01002132 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01002133 seq_printf(m,
2134 "prog_type:\t%u\n"
2135 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01002136 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02002137 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08002138 "prog_id:\t%u\n"
2139 "run_time_ns:\t%llu\n"
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08002140 "run_cnt:\t%llu\n"
Dave Marchevskyaba64c72021-10-20 00:48:17 -07002141 "recursion_misses:\t%llu\n"
2142 "verified_insns:\t%u\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01002143 prog->type,
2144 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01002145 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02002146 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08002147 prog->aux->id,
2148 stats.nsecs,
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08002149 stats.cnt,
Dave Marchevskyaba64c72021-10-20 00:48:17 -07002150 stats.misses,
2151 prog->aux->verified_insns);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01002152}
2153#endif
2154
Chenbo Fengf66e4482017-10-18 13:00:26 -07002155const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01002156#ifdef CONFIG_PROC_FS
2157 .show_fdinfo = bpf_prog_show_fdinfo,
2158#endif
2159 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07002160 .read = bpf_dummy_read,
2161 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002162};
2163
Daniel Borkmannb2197752015-10-29 14:58:09 +01002164int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01002165{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002166 int ret;
2167
2168 ret = security_bpf_prog(prog);
2169 if (ret < 0)
2170 return ret;
2171
Daniel Borkmannaa797812015-10-29 14:58:06 +01002172 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
2173 O_RDWR | O_CLOEXEC);
2174}
2175
Daniel Borkmann113214b2016-06-30 17:24:44 +02002176static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002177{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002178 if (!f.file)
2179 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002180 if (f.file->f_op != &bpf_prog_fops) {
2181 fdput(f);
2182 return ERR_PTR(-EINVAL);
2183 }
2184
Daniel Borkmannc2101292015-10-29 14:58:07 +01002185 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002186}
2187
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002188void bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07002189{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002190 atomic64_add(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07002191}
Brenden Blanco59d36562016-07-19 12:16:46 -07002192EXPORT_SYMBOL_GPL(bpf_prog_add);
2193
Daniel Borkmannc5405942016-11-09 22:02:34 +01002194void bpf_prog_sub(struct bpf_prog *prog, int i)
2195{
2196 /* Only to be used for undoing previous bpf_prog_add() in some
2197 * error path. We still know that another entity in our call
2198 * path holds a reference to the program, thus atomic_sub() can
2199 * be safely used in such cases!
2200 */
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002201 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
Daniel Borkmannc5405942016-11-09 22:02:34 +01002202}
2203EXPORT_SYMBOL_GPL(bpf_prog_sub);
2204
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002205void bpf_prog_inc(struct bpf_prog *prog)
Brenden Blanco59d36562016-07-19 12:16:46 -07002206{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002207 atomic64_inc(&prog->aux->refcnt);
Brenden Blanco59d36562016-07-19 12:16:46 -07002208}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01002209EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07002210
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002211/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07002212struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002213{
2214 int refold;
2215
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002216 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002217
2218 if (!refold)
2219 return ERR_PTR(-ENOENT);
2220
2221 return prog;
2222}
John Fastabenda6f6df62017-08-15 22:32:22 -07002223EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002224
Al Viro040ee692017-12-02 20:20:38 -05002225bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08002226 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07002227{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08002228 /* not an attachment, just a refcount inc, always allow */
2229 if (!attach_type)
2230 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07002231
2232 if (prog->type != *attach_type)
2233 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08002234 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07002235 return false;
2236
2237 return true;
2238}
2239
2240static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08002241 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002242{
2243 struct fd f = fdget(ufd);
2244 struct bpf_prog *prog;
2245
Daniel Borkmann113214b2016-06-30 17:24:44 +02002246 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002247 if (IS_ERR(prog))
2248 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08002249 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02002250 prog = ERR_PTR(-EINVAL);
2251 goto out;
2252 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002253
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002254 bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02002255out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002256 fdput(f);
2257 return prog;
2258}
Daniel Borkmann113214b2016-06-30 17:24:44 +02002259
2260struct bpf_prog *bpf_prog_get(u32 ufd)
2261{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08002262 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02002263}
2264
Jakub Kicinski248f3462017-11-03 13:56:20 -07002265struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08002266 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07002267{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07002268 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07002269}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07002270EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07002271
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002272/* Initially all BPF programs could be loaded w/o specifying
2273 * expected_attach_type. Later for some of them specifying expected_attach_type
2274 * at load time became required so that program could be validated properly.
2275 * Programs of types that are allowed to be loaded both w/ and w/o (for
2276 * backward compatibility) expected_attach_type, should have the default attach
2277 * type assigned to expected_attach_type for the latter case, so that it can be
2278 * validated later at attach time.
2279 *
2280 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
2281 * prog type requires it but has some attach types that have to be backward
2282 * compatible.
2283 */
2284static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
2285{
2286 switch (attr->prog_type) {
2287 case BPF_PROG_TYPE_CGROUP_SOCK:
2288 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
2289 * exist so checking for non-zero is the way to go here.
2290 */
2291 if (!attr->expected_attach_type)
2292 attr->expected_attach_type =
2293 BPF_CGROUP_INET_SOCK_CREATE;
2294 break;
Kuniyuki Iwashimad5e4dda2021-06-12 21:32:22 +09002295 case BPF_PROG_TYPE_SK_REUSEPORT:
2296 if (!attr->expected_attach_type)
2297 attr->expected_attach_type =
2298 BPF_SK_REUSEPORT_SELECT;
2299 break;
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002300 }
2301}
2302
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002303static int
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002304bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2305 enum bpf_attach_type expected_attach_type,
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002306 struct btf *attach_btf, u32 btf_id,
2307 struct bpf_prog *dst_prog)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002308{
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002309 if (btf_id) {
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07002310 if (btf_id > BTF_MAX_TYPE)
2311 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002312
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002313 if (!attach_btf && !dst_prog)
2314 return -EINVAL;
2315
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002316 switch (prog_type) {
2317 case BPF_PROG_TYPE_TRACING:
KP Singh9e4e01d2020-03-29 01:43:52 +01002318 case BPF_PROG_TYPE_LSM:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002319 case BPF_PROG_TYPE_STRUCT_OPS:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002320 case BPF_PROG_TYPE_EXT:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002321 break;
2322 default:
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07002323 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002324 }
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07002325 }
2326
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002327 if (attach_btf && (!btf_id || dst_prog))
2328 return -EINVAL;
2329
2330 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002331 prog_type != BPF_PROG_TYPE_EXT)
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08002332 return -EINVAL;
2333
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07002334 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002335 case BPF_PROG_TYPE_CGROUP_SOCK:
2336 switch (expected_attach_type) {
2337 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07002338 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002339 case BPF_CGROUP_INET4_POST_BIND:
2340 case BPF_CGROUP_INET6_POST_BIND:
2341 return 0;
2342 default:
2343 return -EINVAL;
2344 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002345 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2346 switch (expected_attach_type) {
2347 case BPF_CGROUP_INET4_BIND:
2348 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002349 case BPF_CGROUP_INET4_CONNECT:
2350 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02002351 case BPF_CGROUP_INET4_GETPEERNAME:
2352 case BPF_CGROUP_INET6_GETPEERNAME:
2353 case BPF_CGROUP_INET4_GETSOCKNAME:
2354 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002355 case BPF_CGROUP_UDP4_SENDMSG:
2356 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002357 case BPF_CGROUP_UDP4_RECVMSG:
2358 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002359 return 0;
2360 default:
2361 return -EINVAL;
2362 }
brakmo5cf1e912019-05-28 16:59:36 -07002363 case BPF_PROG_TYPE_CGROUP_SKB:
2364 switch (expected_attach_type) {
2365 case BPF_CGROUP_INET_INGRESS:
2366 case BPF_CGROUP_INET_EGRESS:
2367 return 0;
2368 default:
2369 return -EINVAL;
2370 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002371 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2372 switch (expected_attach_type) {
2373 case BPF_CGROUP_SETSOCKOPT:
2374 case BPF_CGROUP_GETSOCKOPT:
2375 return 0;
2376 default:
2377 return -EINVAL;
2378 }
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02002379 case BPF_PROG_TYPE_SK_LOOKUP:
2380 if (expected_attach_type == BPF_SK_LOOKUP)
2381 return 0;
2382 return -EINVAL;
Kuniyuki Iwashimad5e4dda2021-06-12 21:32:22 +09002383 case BPF_PROG_TYPE_SK_REUSEPORT:
2384 switch (expected_attach_type) {
2385 case BPF_SK_REUSEPORT_SELECT:
2386 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2387 return 0;
2388 default:
2389 return -EINVAL;
2390 }
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07002391 case BPF_PROG_TYPE_SYSCALL:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002392 case BPF_PROG_TYPE_EXT:
2393 if (expected_attach_type)
2394 return -EINVAL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002395 fallthrough;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002396 default:
2397 return 0;
2398 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002399}
2400
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002401static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2402{
2403 switch (prog_type) {
2404 case BPF_PROG_TYPE_SCHED_CLS:
2405 case BPF_PROG_TYPE_SCHED_ACT:
2406 case BPF_PROG_TYPE_XDP:
2407 case BPF_PROG_TYPE_LWT_IN:
2408 case BPF_PROG_TYPE_LWT_OUT:
2409 case BPF_PROG_TYPE_LWT_XMIT:
2410 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2411 case BPF_PROG_TYPE_SK_SKB:
2412 case BPF_PROG_TYPE_SK_MSG:
2413 case BPF_PROG_TYPE_LIRC_MODE2:
2414 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2415 case BPF_PROG_TYPE_CGROUP_DEVICE:
2416 case BPF_PROG_TYPE_CGROUP_SOCK:
2417 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2418 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2419 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2420 case BPF_PROG_TYPE_SOCK_OPS:
2421 case BPF_PROG_TYPE_EXT: /* extends any prog */
2422 return true;
2423 case BPF_PROG_TYPE_CGROUP_SKB:
2424 /* always unpriv */
2425 case BPF_PROG_TYPE_SK_REUSEPORT:
2426 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2427 default:
2428 return false;
2429 }
2430}
2431
2432static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2433{
2434 switch (prog_type) {
2435 case BPF_PROG_TYPE_KPROBE:
2436 case BPF_PROG_TYPE_TRACEPOINT:
2437 case BPF_PROG_TYPE_PERF_EVENT:
2438 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2439 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2440 case BPF_PROG_TYPE_TRACING:
2441 case BPF_PROG_TYPE_LSM:
2442 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2443 case BPF_PROG_TYPE_EXT: /* extends any prog */
2444 return true;
2445 default:
2446 return false;
2447 }
2448}
2449
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002450/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitovfbd94c72021-12-01 10:10:28 -08002451#define BPF_PROG_LOAD_LAST_FIELD core_relo_rec_size
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002452
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07002453static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002454{
2455 enum bpf_prog_type type = attr->prog_type;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002456 struct bpf_prog *prog, *dst_prog = NULL;
2457 struct btf *attach_btf = NULL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002458 int err;
2459 char license[128];
2460 bool is_gpl;
2461
2462 if (CHECK_ATTR(BPF_PROG_LOAD))
2463 return -EINVAL;
2464
Jiong Wangc240eff2019-05-24 23:25:16 +01002465 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2466 BPF_F_ANY_ALIGNMENT |
Alexei Starovoitov10d274e2019-08-22 22:52:12 -07002467 BPF_F_TEST_STATE_FREQ |
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002468 BPF_F_SLEEPABLE |
Lorenzo Bianconic2f2cdb2022-01-21 11:09:52 +01002469 BPF_F_TEST_RND_HI32 |
2470 BPF_F_XDP_HAS_FRAGS))
David S. Millere07b98d2017-05-10 11:38:07 -07002471 return -EINVAL;
2472
David Millere9ee9ef2018-11-30 21:08:14 -08002473 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2474 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002475 !bpf_capable())
David Millere9ee9ef2018-11-30 21:08:14 -08002476 return -EPERM;
2477
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002478 /* copy eBPF program license from user space */
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07002479 if (strncpy_from_bpfptr(license,
2480 make_bpfptr(attr->license, uattr.is_kernel),
2481 sizeof(license) - 1) < 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002482 return -EFAULT;
2483 license[sizeof(license) - 1] = 0;
2484
2485 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2486 is_gpl = license_is_gpl_compatible(license);
2487
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07002488 if (attr->insn_cnt == 0 ||
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002489 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01002490 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07002491 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2492 type != BPF_PROG_TYPE_CGROUP_SKB &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002493 !bpf_capable())
2494 return -EPERM;
2495
Maciej Żenczykowskib338cb92020-06-20 14:26:16 -07002496 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002497 return -EPERM;
2498 if (is_perfmon_prog_type(type) && !perfmon_capable())
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002499 return -EPERM;
2500
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002501 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2502 * or btf, we need to check which one it is
2503 */
2504 if (attr->attach_prog_fd) {
2505 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2506 if (IS_ERR(dst_prog)) {
2507 dst_prog = NULL;
2508 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2509 if (IS_ERR(attach_btf))
2510 return -EINVAL;
2511 if (!btf_is_kernel(attach_btf)) {
Andrii Nakryiko8bdd8e22020-12-07 22:43:26 -08002512 /* attaching through specifying bpf_prog's BTF
2513 * objects directly might be supported eventually
2514 */
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002515 btf_put(attach_btf);
Andrii Nakryiko8bdd8e22020-12-07 22:43:26 -08002516 return -ENOTSUPP;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002517 }
2518 }
2519 } else if (attr->attach_btf_id) {
2520 /* fall back to vmlinux BTF, if BTF type ID is specified */
2521 attach_btf = bpf_get_btf_vmlinux();
2522 if (IS_ERR(attach_btf))
2523 return PTR_ERR(attach_btf);
2524 if (!attach_btf)
2525 return -EINVAL;
2526 btf_get(attach_btf);
2527 }
2528
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002529 bpf_prog_load_fixup_attach_type(attr);
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002530 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002531 attach_btf, attr->attach_btf_id,
2532 dst_prog)) {
2533 if (dst_prog)
2534 bpf_prog_put(dst_prog);
2535 if (attach_btf)
2536 btf_put(attach_btf);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002537 return -EINVAL;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002538 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002539
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002540 /* plain bpf_prog allocation */
2541 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002542 if (!prog) {
2543 if (dst_prog)
2544 bpf_prog_put(dst_prog);
2545 if (attach_btf)
2546 btf_put(attach_btf);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002547 return -ENOMEM;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002548 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002549
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002550 prog->expected_attach_type = attr->expected_attach_type;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002551 prog->aux->attach_btf = attach_btf;
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002552 prog->aux->attach_btf_id = attr->attach_btf_id;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002553 prog->aux->dst_prog = dst_prog;
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002554 prog->aux->offload_requested = !!attr->prog_ifindex;
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002555 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
Lorenzo Bianconic2f2cdb2022-01-21 11:09:52 +01002556 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002557
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002558 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002559 if (err)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002560 goto free_prog;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002561
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002562 prog->aux->user = get_current_user();
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002563 prog->len = attr->insn_cnt;
2564
2565 err = -EFAULT;
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07002566 if (copy_from_bpfptr(prog->insns,
2567 make_bpfptr(attr->insns, uattr.is_kernel),
2568 bpf_prog_insn_size(prog)) != 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002569 goto free_prog_sec;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002570
2571 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002572 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002573
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002574 atomic64_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002575 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002576
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002577 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002578 err = bpf_prog_offload_init(prog, attr);
2579 if (err)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002580 goto free_prog_sec;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002581 }
2582
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002583 /* find program type: socket_filter vs tracing_filter */
2584 err = find_prog_type(type, prog);
2585 if (err < 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002586 goto free_prog_sec;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002587
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002588 prog->aux->load_time = ktime_get_boottime_ns();
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -07002589 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2590 sizeof(attr->prog_name));
2591 if (err < 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002592 goto free_prog_sec;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002593
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002594 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08002595 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002596 if (err < 0)
2597 goto free_used_maps;
2598
Daniel Borkmann9facc332018-06-15 02:30:48 +02002599 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002600 if (err < 0)
2601 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002602
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07002603 err = bpf_prog_alloc_id(prog);
2604 if (err)
2605 goto free_used_maps;
2606
Daniel Borkmannc7517982019-08-23 22:14:23 +02002607 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2608 * effectively publicly exposed. However, retrieving via
2609 * bpf_prog_get_fd_by_id() will take another reference,
2610 * therefore it cannot be gone underneath us.
2611 *
2612 * Only for the time /after/ successful bpf_prog_new_fd()
2613 * and before returning to userspace, we might just hold
2614 * one reference and any parallel close on that fd could
2615 * rip everything out. Hence, below notifications must
2616 * happen before bpf_prog_new_fd().
2617 *
2618 * Also, any failure handling from this point onwards must
2619 * be using bpf_prog_put() given the program is exposed.
2620 */
Daniel Borkmann74451e662017-02-16 22:24:50 +01002621 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08002622 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01002623 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
Daniel Borkmannc7517982019-08-23 22:14:23 +02002624
2625 err = bpf_prog_new_fd(prog);
2626 if (err < 0)
2627 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002628 return err;
2629
2630free_used_maps:
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002631 /* In case we have subprogs, we need to wait for a grace
2632 * period before we can tear down JIT memory since symbols
2633 * are already exposed under kallsyms.
2634 */
2635 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2636 return err;
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002637free_prog_sec:
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002638 free_uid(prog->aux->user);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002639 security_bpf_prog_free(prog->aux);
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002640free_prog:
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08002641 if (prog->aux->attach_btf)
2642 btf_put(prog->aux->attach_btf);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002643 bpf_prog_free(prog);
2644 return err;
2645}
2646
Chenbo Feng6e71b042017-10-18 13:00:22 -07002647#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01002648
2649static int bpf_obj_pin(const union bpf_attr *attr)
2650{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002651 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002652 return -EINVAL;
2653
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002654 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01002655}
2656
2657static int bpf_obj_get(const union bpf_attr *attr)
2658{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002659 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2660 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002661 return -EINVAL;
2662
Chenbo Feng6e71b042017-10-18 13:00:22 -07002663 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2664 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01002665}
2666
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002667void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002668 const struct bpf_link_ops *ops, struct bpf_prog *prog)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002669{
2670 atomic64_set(&link->refcnt, 1);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002671 link->type = type;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002672 link->id = 0;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002673 link->ops = ops;
2674 link->prog = prog;
2675}
2676
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002677static void bpf_link_free_id(int id)
2678{
2679 if (!id)
2680 return;
2681
2682 spin_lock_bh(&link_idr_lock);
2683 idr_remove(&link_idr, id);
2684 spin_unlock_bh(&link_idr_lock);
2685}
2686
Andrii Nakryiko98868662020-03-12 17:21:28 -07002687/* Clean up bpf_link and corresponding anon_inode file and FD. After
2688 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002689 * anon_inode's release() call. This helper marksbpf_link as
2690 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2691 * is not decremented, it's the responsibility of a calling code that failed
2692 * to complete bpf_link initialization.
Andrii Nakryiko98868662020-03-12 17:21:28 -07002693 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002694void bpf_link_cleanup(struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002695{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002696 primer->link->prog = NULL;
2697 bpf_link_free_id(primer->id);
2698 fput(primer->file);
2699 put_unused_fd(primer->fd);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002700}
2701
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002702void bpf_link_inc(struct bpf_link *link)
2703{
2704 atomic64_inc(&link->refcnt);
2705}
2706
2707/* bpf_link_free is guaranteed to be called from process context */
2708static void bpf_link_free(struct bpf_link *link)
2709{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002710 bpf_link_free_id(link->id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002711 if (link->prog) {
2712 /* detach BPF program, clean up used resources */
2713 link->ops->release(link);
2714 bpf_prog_put(link->prog);
2715 }
2716 /* free bpf_link and its containing memory */
2717 link->ops->dealloc(link);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002718}
2719
2720static void bpf_link_put_deferred(struct work_struct *work)
2721{
2722 struct bpf_link *link = container_of(work, struct bpf_link, work);
2723
2724 bpf_link_free(link);
2725}
2726
2727/* bpf_link_put can be called from atomic context, but ensures that resources
2728 * are freed from process context
2729 */
2730void bpf_link_put(struct bpf_link *link)
2731{
2732 if (!atomic64_dec_and_test(&link->refcnt))
2733 return;
2734
Alexei Starovoitovf00f2f72020-09-23 19:10:38 -07002735 if (in_atomic()) {
2736 INIT_WORK(&link->work, bpf_link_put_deferred);
2737 schedule_work(&link->work);
2738 } else {
2739 bpf_link_free(link);
2740 }
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002741}
Alexei Starovoitovcb80ddc2022-02-09 15:20:01 -08002742EXPORT_SYMBOL(bpf_link_put);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002743
2744static int bpf_link_release(struct inode *inode, struct file *filp)
2745{
2746 struct bpf_link *link = filp->private_data;
2747
2748 bpf_link_put(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002749 return 0;
2750}
2751
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002752#ifdef CONFIG_PROC_FS
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002753#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2754#define BPF_MAP_TYPE(_id, _ops)
2755#define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2756static const char *bpf_link_type_strs[] = {
2757 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2758#include <linux/bpf_types.h>
2759};
2760#undef BPF_PROG_TYPE
2761#undef BPF_MAP_TYPE
2762#undef BPF_LINK_TYPE
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002763
2764static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2765{
2766 const struct bpf_link *link = filp->private_data;
2767 const struct bpf_prog *prog = link->prog;
2768 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002769
2770 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2771 seq_printf(m,
2772 "link_type:\t%s\n"
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002773 "link_id:\t%u\n"
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002774 "prog_tag:\t%s\n"
2775 "prog_id:\t%u\n",
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002776 bpf_link_type_strs[link->type],
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002777 link->id,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002778 prog_tag,
2779 prog->aux->id);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002780 if (link->ops->show_fdinfo)
2781 link->ops->show_fdinfo(link, m);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002782}
2783#endif
2784
Zou Wei6f302bf2020-04-23 10:32:40 +08002785static const struct file_operations bpf_link_fops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002786#ifdef CONFIG_PROC_FS
2787 .show_fdinfo = bpf_link_show_fdinfo,
2788#endif
2789 .release = bpf_link_release,
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002790 .read = bpf_dummy_read,
2791 .write = bpf_dummy_write,
2792};
2793
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002794static int bpf_link_alloc_id(struct bpf_link *link)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002795{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002796 int id;
2797
2798 idr_preload(GFP_KERNEL);
2799 spin_lock_bh(&link_idr_lock);
2800 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2801 spin_unlock_bh(&link_idr_lock);
2802 idr_preload_end();
2803
2804 return id;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002805}
2806
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002807/* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2808 * reserving unused FD and allocating ID from link_idr. This is to be paired
2809 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2810 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2811 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2812 * transient state is passed around in struct bpf_link_primer.
2813 * This is preferred way to create and initialize bpf_link, especially when
Tom Rixc561d112022-02-20 10:40:55 -08002814 * there are complicated and expensive operations in between creating bpf_link
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002815 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2816 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2817 * expensive (and potentially failing) roll back operations in a rare case
2818 * that file, FD, or ID can't be allocated.
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002819 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002820int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002821{
2822 struct file *file;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002823 int fd, id;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002824
2825 fd = get_unused_fd_flags(O_CLOEXEC);
2826 if (fd < 0)
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002827 return fd;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002828
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002829
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002830 id = bpf_link_alloc_id(link);
2831 if (id < 0) {
2832 put_unused_fd(fd);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002833 return id;
2834 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002835
2836 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2837 if (IS_ERR(file)) {
Andrii Nakryiko138c6762020-05-01 11:56:22 -07002838 bpf_link_free_id(id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002839 put_unused_fd(fd);
Andrii Nakryiko138c6762020-05-01 11:56:22 -07002840 return PTR_ERR(file);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002841 }
2842
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002843 primer->link = link;
2844 primer->file = file;
2845 primer->fd = fd;
2846 primer->id = id;
2847 return 0;
2848}
2849
2850int bpf_link_settle(struct bpf_link_primer *primer)
2851{
2852 /* make bpf_link fetchable by ID */
2853 spin_lock_bh(&link_idr_lock);
2854 primer->link->id = primer->id;
2855 spin_unlock_bh(&link_idr_lock);
2856 /* make bpf_link fetchable by FD */
2857 fd_install(primer->fd, primer->file);
2858 /* pass through installed FD */
2859 return primer->fd;
2860}
2861
2862int bpf_link_new_fd(struct bpf_link *link)
2863{
2864 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002865}
2866
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002867struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2868{
2869 struct fd f = fdget(ufd);
2870 struct bpf_link *link;
2871
2872 if (!f.file)
2873 return ERR_PTR(-EBADF);
2874 if (f.file->f_op != &bpf_link_fops) {
2875 fdput(f);
2876 return ERR_PTR(-EINVAL);
2877 }
2878
2879 link = f.file->private_data;
2880 bpf_link_inc(link);
2881 fdput(f);
2882
2883 return link;
2884}
Alexei Starovoitovcb80ddc2022-02-09 15:20:01 -08002885EXPORT_SYMBOL(bpf_link_get_from_fd);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002886
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002887static void bpf_tracing_link_release(struct bpf_link *link)
2888{
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002889 struct bpf_tracing_link *tr_link =
Kui-Feng Leef7e0bea2022-05-10 13:59:19 -07002890 container_of(link, struct bpf_tracing_link, link.link);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002891
Kui-Feng Leef7e0bea2022-05-10 13:59:19 -07002892 WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link,
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002893 tr_link->trampoline));
2894
2895 bpf_trampoline_put(tr_link->trampoline);
2896
2897 /* tgt_prog is NULL if target is a kernel function */
2898 if (tr_link->tgt_prog)
2899 bpf_prog_put(tr_link->tgt_prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002900}
2901
2902static void bpf_tracing_link_dealloc(struct bpf_link *link)
2903{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002904 struct bpf_tracing_link *tr_link =
Kui-Feng Leef7e0bea2022-05-10 13:59:19 -07002905 container_of(link, struct bpf_tracing_link, link.link);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002906
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002907 kfree(tr_link);
2908}
2909
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002910static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2911 struct seq_file *seq)
2912{
2913 struct bpf_tracing_link *tr_link =
Kui-Feng Leef7e0bea2022-05-10 13:59:19 -07002914 container_of(link, struct bpf_tracing_link, link.link);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002915
2916 seq_printf(seq,
2917 "attach_type:\t%d\n",
2918 tr_link->attach_type);
2919}
2920
2921static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2922 struct bpf_link_info *info)
2923{
2924 struct bpf_tracing_link *tr_link =
Kui-Feng Leef7e0bea2022-05-10 13:59:19 -07002925 container_of(link, struct bpf_tracing_link, link.link);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002926
2927 info->tracing.attach_type = tr_link->attach_type;
Toke Høiland-Jørgensen441e8c62021-04-13 11:16:06 +02002928 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2929 &info->tracing.target_obj_id,
2930 &info->tracing.target_btf_id);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002931
2932 return 0;
2933}
2934
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002935static const struct bpf_link_ops bpf_tracing_link_lops = {
2936 .release = bpf_tracing_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002937 .dealloc = bpf_tracing_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002938 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2939 .fill_link_info = bpf_tracing_link_fill_link_info,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002940};
2941
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002942static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2943 int tgt_prog_fd,
Kui-Feng Lee2fcc8242022-05-10 13:59:21 -07002944 u32 btf_id,
2945 u64 bpf_cookie)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002946{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002947 struct bpf_link_primer link_primer;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002948 struct bpf_prog *tgt_prog = NULL;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002949 struct bpf_trampoline *tr = NULL;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002950 struct bpf_tracing_link *link;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002951 u64 key = 0;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002952 int err;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002953
KP Singh9e4e01d2020-03-29 01:43:52 +01002954 switch (prog->type) {
2955 case BPF_PROG_TYPE_TRACING:
2956 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2957 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2958 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2959 err = -EINVAL;
2960 goto out_put_prog;
2961 }
2962 break;
2963 case BPF_PROG_TYPE_EXT:
2964 if (prog->expected_attach_type != 0) {
2965 err = -EINVAL;
2966 goto out_put_prog;
2967 }
2968 break;
2969 case BPF_PROG_TYPE_LSM:
2970 if (prog->expected_attach_type != BPF_LSM_MAC) {
2971 err = -EINVAL;
2972 goto out_put_prog;
2973 }
2974 break;
2975 default:
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002976 err = -EINVAL;
2977 goto out_put_prog;
2978 }
2979
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002980 if (!!tgt_prog_fd != !!btf_id) {
2981 err = -EINVAL;
2982 goto out_put_prog;
2983 }
2984
2985 if (tgt_prog_fd) {
2986 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2987 if (prog->type != BPF_PROG_TYPE_EXT) {
2988 err = -EINVAL;
2989 goto out_put_prog;
2990 }
2991
2992 tgt_prog = bpf_prog_get(tgt_prog_fd);
2993 if (IS_ERR(tgt_prog)) {
2994 err = PTR_ERR(tgt_prog);
2995 tgt_prog = NULL;
2996 goto out_put_prog;
2997 }
2998
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08002999 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02003000 }
3001
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003002 link = kzalloc(sizeof(*link), GFP_USER);
3003 if (!link) {
3004 err = -ENOMEM;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08003005 goto out_put_prog;
3006 }
Kui-Feng Leef7e0bea2022-05-10 13:59:19 -07003007 bpf_link_init(&link->link.link, BPF_LINK_TYPE_TRACING,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003008 &bpf_tracing_link_lops, prog);
3009 link->attach_type = prog->expected_attach_type;
Kui-Feng Lee2fcc8242022-05-10 13:59:21 -07003010 link->link.cookie = bpf_cookie;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08003011
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02003012 mutex_lock(&prog->aux->dst_mutex);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003013
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02003014 /* There are a few possible cases here:
3015 *
3016 * - if prog->aux->dst_trampoline is set, the program was just loaded
3017 * and not yet attached to anything, so we can use the values stored
3018 * in prog->aux
3019 *
3020 * - if prog->aux->dst_trampoline is NULL, the program has already been
3021 * attached to a target and its initial target was cleared (below)
3022 *
3023 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
3024 * target_btf_id using the link_create API.
3025 *
3026 * - if tgt_prog == NULL when this function was called using the old
Jiri Olsaf3a95072021-04-14 21:51:41 +02003027 * raw_tracepoint_open API, and we need a target from prog->aux
3028 *
3029 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
3030 * was detached and is going for re-attachment.
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02003031 */
3032 if (!prog->aux->dst_trampoline && !tgt_prog) {
Jiri Olsaf3a95072021-04-14 21:51:41 +02003033 /*
3034 * Allow re-attach for TRACING and LSM programs. If it's
3035 * currently linked, bpf_trampoline_link_prog will fail.
3036 * EXT programs need to specify tgt_prog_fd, so they
3037 * re-attach in separate code path.
3038 */
3039 if (prog->type != BPF_PROG_TYPE_TRACING &&
3040 prog->type != BPF_PROG_TYPE_LSM) {
3041 err = -EINVAL;
3042 goto out_unlock;
3043 }
3044 btf_id = prog->aux->attach_btf_id;
3045 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02003046 }
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02003047
3048 if (!prog->aux->dst_trampoline ||
3049 (key && key != prog->aux->dst_trampoline->key)) {
3050 /* If there is no saved target, or the specified target is
3051 * different from the destination specified at load time, we
3052 * need a new trampoline and a check for compatibility
3053 */
3054 struct bpf_attach_target_info tgt_info = {};
3055
3056 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
3057 &tgt_info);
3058 if (err)
3059 goto out_unlock;
3060
3061 tr = bpf_trampoline_get(key, &tgt_info);
3062 if (!tr) {
3063 err = -ENOMEM;
3064 goto out_unlock;
3065 }
3066 } else {
3067 /* The caller didn't specify a target, or the target was the
3068 * same as the destination supplied during program load. This
3069 * means we can reuse the trampoline and reference from program
3070 * load time, and there is no need to allocate a new one. This
3071 * can only happen once for any program, as the saved values in
3072 * prog->aux are cleared below.
3073 */
3074 tr = prog->aux->dst_trampoline;
3075 tgt_prog = prog->aux->dst_prog;
3076 }
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02003077
Kui-Feng Leef7e0bea2022-05-10 13:59:19 -07003078 err = bpf_link_prime(&link->link.link, &link_primer);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02003079 if (err)
3080 goto out_unlock;
3081
Kui-Feng Leef7e0bea2022-05-10 13:59:19 -07003082 err = bpf_trampoline_link_prog(&link->link, tr);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003083 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003084 bpf_link_cleanup(&link_primer);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02003085 link = NULL;
3086 goto out_unlock;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003087 }
3088
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02003089 link->tgt_prog = tgt_prog;
3090 link->trampoline = tr;
3091
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02003092 /* Always clear the trampoline and target prog from prog->aux to make
3093 * sure the original attach destination is not kept alive after a
3094 * program is (re-)attached to another target.
3095 */
3096 if (prog->aux->dst_prog &&
3097 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
3098 /* got extra prog ref from syscall, or attaching to different prog */
3099 bpf_prog_put(prog->aux->dst_prog);
3100 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
3101 /* we allocated a new trampoline, so free the old one */
3102 bpf_trampoline_put(prog->aux->dst_trampoline);
3103
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02003104 prog->aux->dst_prog = NULL;
3105 prog->aux->dst_trampoline = NULL;
3106 mutex_unlock(&prog->aux->dst_mutex);
3107
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003108 return bpf_link_settle(&link_primer);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02003109out_unlock:
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02003110 if (tr && tr != prog->aux->dst_trampoline)
3111 bpf_trampoline_put(tr);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02003112 mutex_unlock(&prog->aux->dst_mutex);
3113 kfree(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08003114out_put_prog:
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02003115 if (tgt_prog_fd && tgt_prog)
3116 bpf_prog_put(tgt_prog);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08003117 return err;
3118}
3119
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003120struct bpf_raw_tp_link {
3121 struct bpf_link link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003122 struct bpf_raw_event_map *btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003123};
3124
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003125static void bpf_raw_tp_link_release(struct bpf_link *link)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003126{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003127 struct bpf_raw_tp_link *raw_tp =
3128 container_of(link, struct bpf_raw_tp_link, link);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003129
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003130 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
Matt Mullinsa38d1102018-12-12 16:42:37 -08003131 bpf_put_raw_tracepoint(raw_tp->btp);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003132}
3133
3134static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
3135{
3136 struct bpf_raw_tp_link *raw_tp =
3137 container_of(link, struct bpf_raw_tp_link, link);
3138
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003139 kfree(raw_tp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003140}
3141
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003142static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
3143 struct seq_file *seq)
3144{
3145 struct bpf_raw_tp_link *raw_tp_link =
3146 container_of(link, struct bpf_raw_tp_link, link);
3147
3148 seq_printf(seq,
3149 "tp_name:\t%s\n",
3150 raw_tp_link->btp->tp->name);
3151}
3152
3153static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
3154 struct bpf_link_info *info)
3155{
3156 struct bpf_raw_tp_link *raw_tp_link =
3157 container_of(link, struct bpf_raw_tp_link, link);
3158 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
3159 const char *tp_name = raw_tp_link->btp->tp->name;
3160 u32 ulen = info->raw_tracepoint.tp_name_len;
3161 size_t tp_len = strlen(tp_name);
3162
Yonghong Songb4749592020-08-21 12:10:54 -07003163 if (!ulen ^ !ubuf)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003164 return -EINVAL;
3165
3166 info->raw_tracepoint.tp_name_len = tp_len + 1;
3167
3168 if (!ubuf)
3169 return 0;
3170
3171 if (ulen >= tp_len + 1) {
3172 if (copy_to_user(ubuf, tp_name, tp_len + 1))
3173 return -EFAULT;
3174 } else {
3175 char zero = '\0';
3176
3177 if (copy_to_user(ubuf, tp_name, ulen - 1))
3178 return -EFAULT;
3179 if (put_user(zero, ubuf + ulen - 1))
3180 return -EFAULT;
3181 return -ENOSPC;
3182 }
3183
3184 return 0;
3185}
3186
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003187static const struct bpf_link_ops bpf_raw_tp_link_lops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003188 .release = bpf_raw_tp_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003189 .dealloc = bpf_raw_tp_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003190 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
3191 .fill_link_info = bpf_raw_tp_link_fill_link_info,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003192};
3193
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07003194#ifdef CONFIG_PERF_EVENTS
3195struct bpf_perf_link {
3196 struct bpf_link link;
3197 struct file *perf_file;
3198};
3199
3200static void bpf_perf_link_release(struct bpf_link *link)
3201{
3202 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3203 struct perf_event *event = perf_link->perf_file->private_data;
3204
3205 perf_event_free_bpf_prog(event);
3206 fput(perf_link->perf_file);
3207}
3208
3209static void bpf_perf_link_dealloc(struct bpf_link *link)
3210{
3211 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3212
3213 kfree(perf_link);
3214}
3215
3216static const struct bpf_link_ops bpf_perf_link_lops = {
3217 .release = bpf_perf_link_release,
3218 .dealloc = bpf_perf_link_dealloc,
3219};
3220
3221static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3222{
3223 struct bpf_link_primer link_primer;
3224 struct bpf_perf_link *link;
3225 struct perf_event *event;
3226 struct file *perf_file;
3227 int err;
3228
3229 if (attr->link_create.flags)
3230 return -EINVAL;
3231
3232 perf_file = perf_event_get(attr->link_create.target_fd);
3233 if (IS_ERR(perf_file))
3234 return PTR_ERR(perf_file);
3235
3236 link = kzalloc(sizeof(*link), GFP_USER);
3237 if (!link) {
3238 err = -ENOMEM;
3239 goto out_put_file;
3240 }
3241 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog);
3242 link->perf_file = perf_file;
3243
3244 err = bpf_link_prime(&link->link, &link_primer);
3245 if (err) {
3246 kfree(link);
3247 goto out_put_file;
3248 }
3249
3250 event = perf_file->private_data;
Andrii Nakryiko82e6b1e2021-08-15 00:05:58 -07003251 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07003252 if (err) {
3253 bpf_link_cleanup(&link_primer);
3254 goto out_put_file;
3255 }
3256 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
3257 bpf_prog_inc(prog);
3258
3259 return bpf_link_settle(&link_primer);
3260
3261out_put_file:
3262 fput(perf_file);
3263 return err;
3264}
Jiri Olsa0dcac272022-03-16 13:24:09 +01003265#else
3266static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3267{
3268 return -EOPNOTSUPP;
3269}
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07003270#endif /* CONFIG_PERF_EVENTS */
3271
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07003272static int bpf_raw_tp_link_attach(struct bpf_prog *prog,
3273 const char __user *user_tp_name)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003274{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003275 struct bpf_link_primer link_primer;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003276 struct bpf_raw_tp_link *link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003277 struct bpf_raw_event_map *btp;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07003278 const char *tp_name;
3279 char buf[128];
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003280 int err;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003281
KP Singh9e4e01d2020-03-29 01:43:52 +01003282 switch (prog->type) {
3283 case BPF_PROG_TYPE_TRACING:
3284 case BPF_PROG_TYPE_EXT:
3285 case BPF_PROG_TYPE_LSM:
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07003286 if (user_tp_name)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08003287 /* The attach point for this category of programs
3288 * should be specified via btf_id during program load.
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07003289 */
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07003290 return -EINVAL;
KP Singh9e4e01d2020-03-29 01:43:52 +01003291 if (prog->type == BPF_PROG_TYPE_TRACING &&
3292 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08003293 tp_name = prog->aux->attach_func_name;
KP Singh9e4e01d2020-03-29 01:43:52 +01003294 break;
3295 }
Kui-Feng Lee2fcc8242022-05-10 13:59:21 -07003296 return bpf_tracing_prog_attach(prog, 0, 0, 0);
KP Singh9e4e01d2020-03-29 01:43:52 +01003297 case BPF_PROG_TYPE_RAW_TRACEPOINT:
3298 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07003299 if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0)
3300 return -EFAULT;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07003301 buf[sizeof(buf) - 1] = 0;
3302 tp_name = buf;
KP Singh9e4e01d2020-03-29 01:43:52 +01003303 break;
3304 default:
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07003305 return -EINVAL;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07003306 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003307
Matt Mullinsa38d1102018-12-12 16:42:37 -08003308 btp = bpf_get_raw_tracepoint(tp_name);
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07003309 if (!btp)
3310 return -ENOENT;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003311
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003312 link = kzalloc(sizeof(*link), GFP_USER);
3313 if (!link) {
Matt Mullinsa38d1102018-12-12 16:42:37 -08003314 err = -ENOMEM;
3315 goto out_put_btp;
3316 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003317 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
3318 &bpf_raw_tp_link_lops, prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003319 link->btp = btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003320
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003321 err = bpf_link_prime(&link->link, &link_primer);
3322 if (err) {
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003323 kfree(link);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003324 goto out_put_btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003325 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003326
3327 err = bpf_probe_register(link->btp, prog);
3328 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003329 bpf_link_cleanup(&link_primer);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07003330 goto out_put_btp;
3331 }
3332
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003333 return bpf_link_settle(&link_primer);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003334
Matt Mullinsa38d1102018-12-12 16:42:37 -08003335out_put_btp:
3336 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003337 return err;
3338}
3339
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07003340#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
3341
3342static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
3343{
3344 struct bpf_prog *prog;
3345 int fd;
3346
3347 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
3348 return -EINVAL;
3349
3350 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
3351 if (IS_ERR(prog))
3352 return PTR_ERR(prog);
3353
3354 fd = bpf_raw_tp_link_attach(prog, u64_to_user_ptr(attr->raw_tracepoint.name));
3355 if (fd < 0)
3356 bpf_prog_put(prog);
3357 return fd;
3358}
3359
Anders Roxell33491582018-04-03 14:09:47 +02003360static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
3361 enum bpf_attach_type attach_type)
3362{
3363 switch (prog->type) {
3364 case BPF_PROG_TYPE_CGROUP_SOCK:
3365 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07003366 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02003367 case BPF_PROG_TYPE_SK_LOOKUP:
Anders Roxell33491582018-04-03 14:09:47 +02003368 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
brakmo5cf1e912019-05-28 16:59:36 -07003369 case BPF_PROG_TYPE_CGROUP_SKB:
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003370 if (!capable(CAP_NET_ADMIN))
3371 /* cg-skb progs can be loaded by unpriv user.
3372 * check permissions at attach time.
3373 */
3374 return -EPERM;
brakmo5cf1e912019-05-28 16:59:36 -07003375 return prog->enforce_expected_attach_type &&
3376 prog->expected_attach_type != attach_type ?
3377 -EINVAL : 0;
Anders Roxell33491582018-04-03 14:09:47 +02003378 default:
3379 return 0;
3380 }
3381}
3382
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003383static enum bpf_prog_type
3384attach_type_to_prog_type(enum bpf_attach_type attach_type)
3385{
3386 switch (attach_type) {
3387 case BPF_CGROUP_INET_INGRESS:
3388 case BPF_CGROUP_INET_EGRESS:
3389 return BPF_PROG_TYPE_CGROUP_SKB;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003390 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07003391 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003392 case BPF_CGROUP_INET4_POST_BIND:
3393 case BPF_CGROUP_INET6_POST_BIND:
3394 return BPF_PROG_TYPE_CGROUP_SOCK;
3395 case BPF_CGROUP_INET4_BIND:
3396 case BPF_CGROUP_INET6_BIND:
3397 case BPF_CGROUP_INET4_CONNECT:
3398 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02003399 case BPF_CGROUP_INET4_GETPEERNAME:
3400 case BPF_CGROUP_INET6_GETPEERNAME:
3401 case BPF_CGROUP_INET4_GETSOCKNAME:
3402 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003403 case BPF_CGROUP_UDP4_SENDMSG:
3404 case BPF_CGROUP_UDP6_SENDMSG:
3405 case BPF_CGROUP_UDP4_RECVMSG:
3406 case BPF_CGROUP_UDP6_RECVMSG:
3407 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
3408 case BPF_CGROUP_SOCK_OPS:
3409 return BPF_PROG_TYPE_SOCK_OPS;
3410 case BPF_CGROUP_DEVICE:
3411 return BPF_PROG_TYPE_CGROUP_DEVICE;
3412 case BPF_SK_MSG_VERDICT:
3413 return BPF_PROG_TYPE_SK_MSG;
3414 case BPF_SK_SKB_STREAM_PARSER:
3415 case BPF_SK_SKB_STREAM_VERDICT:
Cong Wanga7ba4552021-03-30 19:32:30 -07003416 case BPF_SK_SKB_VERDICT:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003417 return BPF_PROG_TYPE_SK_SKB;
3418 case BPF_LIRC_MODE2:
3419 return BPF_PROG_TYPE_LIRC_MODE2;
3420 case BPF_FLOW_DISSECTOR:
3421 return BPF_PROG_TYPE_FLOW_DISSECTOR;
3422 case BPF_CGROUP_SYSCTL:
3423 return BPF_PROG_TYPE_CGROUP_SYSCTL;
3424 case BPF_CGROUP_GETSOCKOPT:
3425 case BPF_CGROUP_SETSOCKOPT:
3426 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
Yonghong Songde4e05c2020-05-09 10:59:01 -07003427 case BPF_TRACE_ITER:
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07003428 case BPF_TRACE_RAW_TP:
3429 case BPF_TRACE_FENTRY:
3430 case BPF_TRACE_FEXIT:
3431 case BPF_MODIFY_RETURN:
Yonghong Songde4e05c2020-05-09 10:59:01 -07003432 return BPF_PROG_TYPE_TRACING;
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07003433 case BPF_LSM_MAC:
3434 return BPF_PROG_TYPE_LSM;
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02003435 case BPF_SK_LOOKUP:
3436 return BPF_PROG_TYPE_SK_LOOKUP;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07003437 case BPF_XDP:
3438 return BPF_PROG_TYPE_XDP;
Stanislav Fomichev69fd3372022-06-28 10:43:06 -07003439 case BPF_LSM_CGROUP:
3440 return BPF_PROG_TYPE_LSM;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003441 default:
3442 return BPF_PROG_TYPE_UNSPEC;
3443 }
3444}
3445
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08003446#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
John Fastabend174a79f2017-08-15 22:32:47 -07003447
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003448#define BPF_F_ATTACH_MASK \
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08003449 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003450
Daniel Mackf4324552016-11-23 16:52:27 +01003451static int bpf_prog_attach(const union bpf_attr *attr)
3452{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003453 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01003454 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003455 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01003456
Daniel Mackf4324552016-11-23 16:52:27 +01003457 if (CHECK_ATTR(BPF_PROG_ATTACH))
3458 return -EINVAL;
3459
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003460 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003461 return -EINVAL;
3462
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003463 ptype = attach_type_to_prog_type(attr->attach_type);
3464 if (ptype == BPF_PROG_TYPE_UNSPEC)
Daniel Mackf4324552016-11-23 16:52:27 +01003465 return -EINVAL;
Daniel Mackf4324552016-11-23 16:52:27 +01003466
David Ahernb2cd1252016-12-01 08:48:03 -08003467 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3468 if (IS_ERR(prog))
3469 return PTR_ERR(prog);
3470
Andrey Ignatov5e43f892018-03-30 15:08:00 -07003471 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3472 bpf_prog_put(prog);
3473 return -EINVAL;
3474 }
3475
Sean Youngfdb5c452018-06-19 00:04:24 +01003476 switch (ptype) {
3477 case BPF_PROG_TYPE_SK_SKB:
3478 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02003479 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01003480 break;
3481 case BPF_PROG_TYPE_LIRC_MODE2:
3482 ret = lirc_prog_attach(attr, prog);
3483 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07003484 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +02003485 ret = netns_bpf_prog_attach(attr, prog);
Petar Penkovd58e4682018-09-14 07:46:18 -07003486 break;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003487 case BPF_PROG_TYPE_CGROUP_DEVICE:
3488 case BPF_PROG_TYPE_CGROUP_SKB:
3489 case BPF_PROG_TYPE_CGROUP_SOCK:
3490 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3491 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3492 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3493 case BPF_PROG_TYPE_SOCK_OPS:
Stanislav Fomichev69fd3372022-06-28 10:43:06 -07003494 case BPF_PROG_TYPE_LSM:
3495 if (ptype == BPF_PROG_TYPE_LSM &&
3496 prog->expected_attach_type != BPF_LSM_CGROUP)
3497 return -EINVAL;
3498
Sean Youngfdb5c452018-06-19 00:04:24 +01003499 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003500 break;
3501 default:
3502 ret = -EINVAL;
David Ahernb2cd1252016-12-01 08:48:03 -08003503 }
3504
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003505 if (ret)
3506 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003507 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01003508}
3509
3510#define BPF_PROG_DETACH_LAST_FIELD attach_type
3511
3512static int bpf_prog_detach(const union bpf_attr *attr)
3513{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003514 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01003515
Daniel Mackf4324552016-11-23 16:52:27 +01003516 if (CHECK_ATTR(BPF_PROG_DETACH))
3517 return -EINVAL;
3518
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003519 ptype = attach_type_to_prog_type(attr->attach_type);
3520
3521 switch (ptype) {
3522 case BPF_PROG_TYPE_SK_MSG:
3523 case BPF_PROG_TYPE_SK_SKB:
Lorenz Bauerbb0de312020-06-29 10:56:28 +01003524 return sock_map_prog_detach(attr, ptype);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003525 case BPF_PROG_TYPE_LIRC_MODE2:
Sean Youngf4364dc2018-05-27 12:24:09 +01003526 return lirc_prog_detach(attr);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003527 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Lorenz Bauer4ac2add2020-06-29 10:56:26 +01003528 return netns_bpf_prog_detach(attr, ptype);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003529 case BPF_PROG_TYPE_CGROUP_DEVICE:
3530 case BPF_PROG_TYPE_CGROUP_SKB:
3531 case BPF_PROG_TYPE_CGROUP_SOCK:
3532 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3533 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3534 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3535 case BPF_PROG_TYPE_SOCK_OPS:
Stanislav Fomichev69fd3372022-06-28 10:43:06 -07003536 case BPF_PROG_TYPE_LSM:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003537 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01003538 default:
3539 return -EINVAL;
3540 }
Daniel Mackf4324552016-11-23 16:52:27 +01003541}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07003542
Stanislav Fomichevb79c9fc2022-06-28 10:43:08 -07003543#define BPF_PROG_QUERY_LAST_FIELD query.prog_attach_flags
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003544
3545static int bpf_prog_query(const union bpf_attr *attr,
3546 union bpf_attr __user *uattr)
3547{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003548 if (!capable(CAP_NET_ADMIN))
3549 return -EPERM;
3550 if (CHECK_ATTR(BPF_PROG_QUERY))
3551 return -EINVAL;
3552 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3553 return -EINVAL;
3554
3555 switch (attr->query.attach_type) {
3556 case BPF_CGROUP_INET_INGRESS:
3557 case BPF_CGROUP_INET_EGRESS:
3558 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07003559 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07003560 case BPF_CGROUP_INET4_BIND:
3561 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07003562 case BPF_CGROUP_INET4_POST_BIND:
3563 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07003564 case BPF_CGROUP_INET4_CONNECT:
3565 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02003566 case BPF_CGROUP_INET4_GETPEERNAME:
3567 case BPF_CGROUP_INET6_GETPEERNAME:
3568 case BPF_CGROUP_INET4_GETSOCKNAME:
3569 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07003570 case BPF_CGROUP_UDP4_SENDMSG:
3571 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02003572 case BPF_CGROUP_UDP4_RECVMSG:
3573 case BPF_CGROUP_UDP6_RECVMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003574 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05003575 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08003576 case BPF_CGROUP_SYSCTL:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07003577 case BPF_CGROUP_GETSOCKOPT:
3578 case BPF_CGROUP_SETSOCKOPT:
Stanislav Fomichevb79c9fc2022-06-28 10:43:08 -07003579 case BPF_LSM_CGROUP:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003580 return cgroup_bpf_prog_query(attr, uattr);
Sean Youngf4364dc2018-05-27 12:24:09 +01003581 case BPF_LIRC_MODE2:
3582 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07003583 case BPF_FLOW_DISSECTOR:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02003584 case BPF_SK_LOOKUP:
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +02003585 return netns_bpf_prog_query(attr, uattr);
Di Zhu748cd572022-01-19 09:40:04 +08003586 case BPF_SK_SKB_STREAM_PARSER:
3587 case BPF_SK_SKB_STREAM_VERDICT:
3588 case BPF_SK_MSG_VERDICT:
3589 case BPF_SK_SKB_VERDICT:
3590 return sock_map_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003591 default:
3592 return -EINVAL;
3593 }
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003594}
Daniel Mackf4324552016-11-23 16:52:27 +01003595
Toke Høiland-Jørgensenb530e9e2022-03-09 11:53:42 +01003596#define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003597
3598static int bpf_prog_test_run(const union bpf_attr *attr,
3599 union bpf_attr __user *uattr)
3600{
3601 struct bpf_prog *prog;
3602 int ret = -ENOTSUPP;
3603
3604 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3605 return -EINVAL;
3606
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07003607 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3608 (!attr->test.ctx_size_in && attr->test.ctx_in))
3609 return -EINVAL;
3610
3611 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3612 (!attr->test.ctx_size_out && attr->test.ctx_out))
3613 return -EINVAL;
3614
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003615 prog = bpf_prog_get(attr->test.prog_fd);
3616 if (IS_ERR(prog))
3617 return PTR_ERR(prog);
3618
3619 if (prog->aux->ops->test_run)
3620 ret = prog->aux->ops->test_run(prog, attr, uattr);
3621
3622 bpf_prog_put(prog);
3623 return ret;
3624}
3625
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07003626#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3627
3628static int bpf_obj_get_next_id(const union bpf_attr *attr,
3629 union bpf_attr __user *uattr,
3630 struct idr *idr,
3631 spinlock_t *lock)
3632{
3633 u32 next_id = attr->start_id;
3634 int err = 0;
3635
3636 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3637 return -EINVAL;
3638
3639 if (!capable(CAP_SYS_ADMIN))
3640 return -EPERM;
3641
3642 next_id++;
3643 spin_lock_bh(lock);
3644 if (!idr_get_next(idr, &next_id))
3645 err = -ENOENT;
3646 spin_unlock_bh(lock);
3647
3648 if (!err)
3649 err = put_user(next_id, &uattr->next_id);
3650
3651 return err;
3652}
3653
Yonghong Song6086d292020-05-09 10:59:09 -07003654struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3655{
3656 struct bpf_map *map;
3657
3658 spin_lock_bh(&map_idr_lock);
3659again:
3660 map = idr_get_next(&map_idr, id);
3661 if (map) {
3662 map = __bpf_map_inc_not_zero(map, false);
3663 if (IS_ERR(map)) {
3664 (*id)++;
3665 goto again;
3666 }
3667 }
3668 spin_unlock_bh(&map_idr_lock);
3669
3670 return map;
3671}
3672
Alexei Starovoitova228a642020-07-01 18:10:18 -07003673struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3674{
3675 struct bpf_prog *prog;
3676
3677 spin_lock_bh(&prog_idr_lock);
3678again:
3679 prog = idr_get_next(&prog_idr, id);
3680 if (prog) {
3681 prog = bpf_prog_inc_not_zero(prog);
3682 if (IS_ERR(prog)) {
3683 (*id)++;
3684 goto again;
3685 }
3686 }
3687 spin_unlock_bh(&prog_idr_lock);
3688
3689 return prog;
3690}
3691
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003692#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3693
Björn Töpel7e6897f2019-12-13 18:51:09 +01003694struct bpf_prog *bpf_prog_by_id(u32 id)
3695{
3696 struct bpf_prog *prog;
3697
3698 if (!id)
3699 return ERR_PTR(-ENOENT);
3700
3701 spin_lock_bh(&prog_idr_lock);
3702 prog = idr_find(&prog_idr, id);
3703 if (prog)
3704 prog = bpf_prog_inc_not_zero(prog);
3705 else
3706 prog = ERR_PTR(-ENOENT);
3707 spin_unlock_bh(&prog_idr_lock);
3708 return prog;
3709}
3710
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003711static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3712{
3713 struct bpf_prog *prog;
3714 u32 id = attr->prog_id;
3715 int fd;
3716
3717 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3718 return -EINVAL;
3719
3720 if (!capable(CAP_SYS_ADMIN))
3721 return -EPERM;
3722
Björn Töpel7e6897f2019-12-13 18:51:09 +01003723 prog = bpf_prog_by_id(id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003724 if (IS_ERR(prog))
3725 return PTR_ERR(prog);
3726
3727 fd = bpf_prog_new_fd(prog);
3728 if (fd < 0)
3729 bpf_prog_put(prog);
3730
3731 return fd;
3732}
3733
Chenbo Feng6e71b042017-10-18 13:00:22 -07003734#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003735
3736static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3737{
3738 struct bpf_map *map;
3739 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07003740 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003741 int fd;
3742
Chenbo Feng6e71b042017-10-18 13:00:22 -07003743 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3744 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003745 return -EINVAL;
3746
3747 if (!capable(CAP_SYS_ADMIN))
3748 return -EPERM;
3749
Chenbo Feng6e71b042017-10-18 13:00:22 -07003750 f_flags = bpf_get_file_flag(attr->open_flags);
3751 if (f_flags < 0)
3752 return f_flags;
3753
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003754 spin_lock_bh(&map_idr_lock);
3755 map = idr_find(&map_idr, id);
3756 if (map)
Stanislav Fomichevb0e4701c2019-08-14 10:37:48 -07003757 map = __bpf_map_inc_not_zero(map, true);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003758 else
3759 map = ERR_PTR(-ENOENT);
3760 spin_unlock_bh(&map_idr_lock);
3761
3762 if (IS_ERR(map))
3763 return PTR_ERR(map);
3764
Chenbo Feng6e71b042017-10-18 13:00:22 -07003765 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003766 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08003767 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003768
3769 return fd;
3770}
3771
Daniel Borkmann7105e822017-12-20 13:42:57 +01003772static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003773 unsigned long addr, u32 *off,
3774 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01003775{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003776 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003777 int i;
3778
YiFei Zhu984fe942020-09-15 16:45:39 -07003779 mutex_lock(&prog->aux->used_maps_mutex);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003780 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3781 map = prog->aux->used_maps[i];
3782 if (map == (void *)addr) {
3783 *type = BPF_PSEUDO_MAP_FD;
YiFei Zhu984fe942020-09-15 16:45:39 -07003784 goto out;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003785 }
3786 if (!map->ops->map_direct_value_meta)
3787 continue;
3788 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3789 *type = BPF_PSEUDO_MAP_VALUE;
YiFei Zhu984fe942020-09-15 16:45:39 -07003790 goto out;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003791 }
3792 }
YiFei Zhu984fe942020-09-15 16:45:39 -07003793 map = NULL;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003794
YiFei Zhu984fe942020-09-15 16:45:39 -07003795out:
3796 mutex_unlock(&prog->aux->used_maps_mutex);
3797 return map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003798}
3799
Kees Cook63960262020-07-02 15:45:23 -07003800static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3801 const struct cred *f_cred)
Daniel Borkmann7105e822017-12-20 13:42:57 +01003802{
3803 const struct bpf_map *map;
3804 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003805 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003806 u64 imm;
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003807 u8 code;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003808 int i;
3809
3810 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3811 GFP_USER);
3812 if (!insns)
3813 return insns;
3814
3815 for (i = 0; i < prog->len; i++) {
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003816 code = insns[i].code;
3817
3818 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003819 insns[i].code = BPF_JMP | BPF_CALL;
3820 insns[i].imm = BPF_FUNC_tail_call;
3821 /* fall-through */
3822 }
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003823 if (code == (BPF_JMP | BPF_CALL) ||
3824 code == (BPF_JMP | BPF_CALL_ARGS)) {
3825 if (code == (BPF_JMP | BPF_CALL_ARGS))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003826 insns[i].code = BPF_JMP | BPF_CALL;
Kees Cook63960262020-07-02 15:45:23 -07003827 if (!bpf_dump_raw_ok(f_cred))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003828 insns[i].imm = 0;
3829 continue;
3830 }
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003831 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3832 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3833 continue;
3834 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01003835
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003836 if (code != (BPF_LD | BPF_IMM | BPF_DW))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003837 continue;
3838
3839 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003840 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003841 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003842 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003843 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003844 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003845 continue;
3846 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01003847 }
3848
3849 return insns;
3850}
3851
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003852static int set_info_rec_size(struct bpf_prog_info *info)
3853{
3854 /*
3855 * Ensure info.*_rec_size is the same as kernel expected size
3856 *
3857 * or
3858 *
3859 * Only allow zero *_rec_size if both _rec_size and _cnt are
3860 * zero. In this case, the kernel will set the expected
3861 * _rec_size back to the info.
3862 */
3863
Yonghong Song11d8b822018-12-10 14:14:08 -08003864 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003865 info->func_info_rec_size != sizeof(struct bpf_func_info))
3866 return -EINVAL;
3867
Yonghong Song11d8b822018-12-10 14:14:08 -08003868 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003869 info->line_info_rec_size != sizeof(struct bpf_line_info))
3870 return -EINVAL;
3871
Yonghong Song11d8b822018-12-10 14:14:08 -08003872 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003873 info->jited_line_info_rec_size != sizeof(__u64))
3874 return -EINVAL;
3875
3876 info->func_info_rec_size = sizeof(struct bpf_func_info);
3877 info->line_info_rec_size = sizeof(struct bpf_line_info);
3878 info->jited_line_info_rec_size = sizeof(__u64);
3879
3880 return 0;
3881}
3882
Kees Cook63960262020-07-02 15:45:23 -07003883static int bpf_prog_get_info_by_fd(struct file *file,
3884 struct bpf_prog *prog,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003885 const union bpf_attr *attr,
3886 union bpf_attr __user *uattr)
3887{
3888 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Stanislav Fomichev6644aab2022-08-04 13:11:39 -07003889 struct btf *attach_btf = bpf_prog_get_target_btf(prog);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003890 struct bpf_prog_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003891 u32 info_len = attr->info.info_len;
Eric Dumazet61a0aba2021-10-26 14:41:33 -07003892 struct bpf_prog_kstats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003893 char __user *uinsns;
3894 u32 ulen;
3895 int err;
3896
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07003897 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003898 if (err)
3899 return err;
3900 info_len = min_t(u32, sizeof(info), info_len);
3901
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003902 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003903 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02003904 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003905
3906 info.type = prog->type;
3907 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003908 info.load_time = prog->aux->load_time;
3909 info.created_by_uid = from_kuid_munged(current_user_ns(),
3910 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02003911 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003912
3913 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003914 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3915
YiFei Zhu984fe942020-09-15 16:45:39 -07003916 mutex_lock(&prog->aux->used_maps_mutex);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003917 ulen = info.nr_map_ids;
3918 info.nr_map_ids = prog->aux->used_map_cnt;
3919 ulen = min_t(u32, info.nr_map_ids, ulen);
3920 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07003921 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003922 u32 i;
3923
3924 for (i = 0; i < ulen; i++)
3925 if (put_user(prog->aux->used_maps[i]->id,
YiFei Zhu984fe942020-09-15 16:45:39 -07003926 &user_map_ids[i])) {
3927 mutex_unlock(&prog->aux->used_maps_mutex);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003928 return -EFAULT;
YiFei Zhu984fe942020-09-15 16:45:39 -07003929 }
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003930 }
YiFei Zhu984fe942020-09-15 16:45:39 -07003931 mutex_unlock(&prog->aux->used_maps_mutex);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003932
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003933 err = set_info_rec_size(&info);
3934 if (err)
3935 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08003936
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003937 bpf_prog_get_stats(prog, &stats);
3938 info.run_time_ns = stats.nsecs;
3939 info.run_cnt = stats.cnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08003940 info.recursion_misses = stats.misses;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003941
Dave Marchevskyaba64c72021-10-20 00:48:17 -07003942 info.verified_insns = prog->aux->verified_insns;
3943
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003944 if (!bpf_capable()) {
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003945 info.jited_prog_len = 0;
3946 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303947 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01003948 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08003949 info.nr_func_info = 0;
3950 info.nr_line_info = 0;
3951 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003952 goto done;
3953 }
3954
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003955 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02003956 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003957 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003958 struct bpf_insn *insns_sanitized;
3959 bool fault;
3960
Kees Cook63960262020-07-02 15:45:23 -07003961 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003962 info.xlated_prog_insns = 0;
3963 goto done;
3964 }
Kees Cook63960262020-07-02 15:45:23 -07003965 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003966 if (!insns_sanitized)
3967 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003968 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3969 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003970 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3971 kfree(insns_sanitized);
3972 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003973 return -EFAULT;
3974 }
3975
Jakub Kicinski675fc272017-12-27 18:39:09 -08003976 if (bpf_prog_is_dev_bound(prog->aux)) {
3977 err = bpf_prog_offload_info_fill(&info, prog);
3978 if (err)
3979 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08003980 goto done;
3981 }
3982
3983 /* NOTE: the following code is supposed to be skipped for offload.
3984 * bpf_prog_offload_info_fill() is the place to fill similar fields
3985 * for offload.
3986 */
3987 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05303988 if (prog->aux->func_cnt) {
3989 u32 i;
3990
3991 info.jited_prog_len = 0;
3992 for (i = 0; i < prog->aux->func_cnt; i++)
3993 info.jited_prog_len += prog->aux->func[i]->jited_len;
3994 } else {
3995 info.jited_prog_len = prog->jited_len;
3996 }
3997
Jiong Wangfcfb1262018-01-16 16:05:19 -08003998 if (info.jited_prog_len && ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003999 if (bpf_dump_raw_ok(file->f_cred)) {
Jiong Wangfcfb1262018-01-16 16:05:19 -08004000 uinsns = u64_to_user_ptr(info.jited_prog_insns);
4001 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05304002
4003 /* for multi-function programs, copy the JITed
4004 * instructions for all the functions
4005 */
4006 if (prog->aux->func_cnt) {
4007 u32 len, free, i;
4008 u8 *img;
4009
4010 free = ulen;
4011 for (i = 0; i < prog->aux->func_cnt; i++) {
4012 len = prog->aux->func[i]->jited_len;
4013 len = min_t(u32, len, free);
4014 img = (u8 *) prog->aux->func[i]->bpf_func;
4015 if (copy_to_user(uinsns, img, len))
4016 return -EFAULT;
4017 uinsns += len;
4018 free -= len;
4019 if (!free)
4020 break;
4021 }
4022 } else {
4023 if (copy_to_user(uinsns, prog->bpf_func, ulen))
4024 return -EFAULT;
4025 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08004026 } else {
4027 info.jited_prog_insns = 0;
4028 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08004029 }
4030
Sandipan Dasdbecd732018-05-24 12:26:48 +05304031 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07004032 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08004033 if (ulen) {
Kees Cook63960262020-07-02 15:45:23 -07004034 if (bpf_dump_raw_ok(file->f_cred)) {
Song Liuff1889f2018-11-02 10:16:17 -07004035 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05304036 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05304037 u32 i;
4038
4039 /* copy the address of the kernel symbol
4040 * corresponding to each function
4041 */
4042 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
4043 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07004044 if (prog->aux->func_cnt) {
4045 for (i = 0; i < ulen; i++) {
4046 ksym_addr = (unsigned long)
4047 prog->aux->func[i]->bpf_func;
4048 if (put_user((u64) ksym_addr,
4049 &user_ksyms[i]))
4050 return -EFAULT;
4051 }
4052 } else {
4053 ksym_addr = (unsigned long) prog->bpf_func;
4054 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05304055 return -EFAULT;
4056 }
4057 } else {
4058 info.jited_ksyms = 0;
4059 }
4060 }
4061
Sandipan Das815581c2018-05-24 12:26:52 +05304062 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07004063 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08004064 if (ulen) {
Kees Cook63960262020-07-02 15:45:23 -07004065 if (bpf_dump_raw_ok(file->f_cred)) {
Sandipan Das815581c2018-05-24 12:26:52 +05304066 u32 __user *user_lens;
4067 u32 func_len, i;
4068
4069 /* copy the JITed image lengths for each function */
4070 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
4071 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07004072 if (prog->aux->func_cnt) {
4073 for (i = 0; i < ulen; i++) {
4074 func_len =
4075 prog->aux->func[i]->jited_len;
4076 if (put_user(func_len, &user_lens[i]))
4077 return -EFAULT;
4078 }
4079 } else {
4080 func_len = prog->jited_len;
4081 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05304082 return -EFAULT;
4083 }
4084 } else {
4085 info.jited_func_lens = 0;
4086 }
4087 }
4088
Martin KaFai Lau73372242018-12-05 17:35:43 -08004089 if (prog->aux->btf)
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08004090 info.btf_id = btf_obj_id(prog->aux->btf);
Stanislav Fomichevb79c9fc2022-06-28 10:43:08 -07004091 info.attach_btf_id = prog->aux->attach_btf_id;
Stanislav Fomichev6644aab2022-08-04 13:11:39 -07004092 if (attach_btf)
4093 info.attach_btf_obj_id = btf_obj_id(attach_btf);
Yonghong Song838e9692018-11-19 15:29:11 -08004094
Yonghong Song11d8b822018-12-10 14:14:08 -08004095 ulen = info.nr_func_info;
4096 info.nr_func_info = prog->aux->func_info_cnt;
4097 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08004098 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08004099
Martin KaFai Lau9e794162018-12-12 10:18:21 -08004100 user_finfo = u64_to_user_ptr(info.func_info);
4101 ulen = min_t(u32, info.nr_func_info, ulen);
4102 if (copy_to_user(user_finfo, prog->aux->func_info,
4103 info.func_info_rec_size * ulen))
4104 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08004105 }
4106
Yonghong Song11d8b822018-12-10 14:14:08 -08004107 ulen = info.nr_line_info;
4108 info.nr_line_info = prog->aux->nr_linfo;
4109 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08004110 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004111
Martin KaFai Lau9e794162018-12-12 10:18:21 -08004112 user_linfo = u64_to_user_ptr(info.line_info);
4113 ulen = min_t(u32, info.nr_line_info, ulen);
4114 if (copy_to_user(user_linfo, prog->aux->linfo,
4115 info.line_info_rec_size * ulen))
4116 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004117 }
4118
Yonghong Song11d8b822018-12-10 14:14:08 -08004119 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004120 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08004121 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004122 else
Yonghong Song11d8b822018-12-10 14:14:08 -08004123 info.nr_jited_line_info = 0;
4124 if (info.nr_jited_line_info && ulen) {
Kees Cook63960262020-07-02 15:45:23 -07004125 if (bpf_dump_raw_ok(file->f_cred)) {
Pu Lehui2cd00852022-05-30 17:28:10 +08004126 unsigned long line_addr;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004127 __u64 __user *user_linfo;
4128 u32 i;
4129
4130 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08004131 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004132 for (i = 0; i < ulen; i++) {
Pu Lehui2cd00852022-05-30 17:28:10 +08004133 line_addr = (unsigned long)prog->aux->jited_linfo[i];
4134 if (put_user((__u64)line_addr, &user_linfo[i]))
Martin KaFai Lauc454a462018-12-07 16:42:25 -08004135 return -EFAULT;
4136 }
4137 } else {
4138 info.jited_line_info = 0;
4139 }
4140 }
4141
Song Liuc872bdb2018-12-12 09:37:46 -08004142 ulen = info.nr_prog_tags;
4143 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
4144 if (ulen) {
4145 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
4146 u32 i;
4147
4148 user_prog_tags = u64_to_user_ptr(info.prog_tags);
4149 ulen = min_t(u32, info.nr_prog_tags, ulen);
4150 if (prog->aux->func_cnt) {
4151 for (i = 0; i < ulen; i++) {
4152 if (copy_to_user(user_prog_tags[i],
4153 prog->aux->func[i]->tag,
4154 BPF_TAG_SIZE))
4155 return -EFAULT;
4156 }
4157 } else {
4158 if (copy_to_user(user_prog_tags[0],
4159 prog->tag, BPF_TAG_SIZE))
4160 return -EFAULT;
4161 }
4162 }
4163
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004164done:
4165 if (copy_to_user(uinfo, &info, info_len) ||
4166 put_user(info_len, &uattr->info.info_len))
4167 return -EFAULT;
4168
4169 return 0;
4170}
4171
Kees Cook63960262020-07-02 15:45:23 -07004172static int bpf_map_get_info_by_fd(struct file *file,
4173 struct bpf_map *map,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004174 const union bpf_attr *attr,
4175 union bpf_attr __user *uattr)
4176{
4177 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01004178 struct bpf_map_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004179 u32 info_len = attr->info.info_len;
4180 int err;
4181
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004182 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004183 if (err)
4184 return err;
4185 info_len = min_t(u32, sizeof(info), info_len);
4186
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01004187 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004188 info.type = map->map_type;
4189 info.id = map->id;
4190 info.key_size = map->key_size;
4191 info.value_size = map->value_size;
4192 info.max_entries = map->max_entries;
4193 info.map_flags = map->map_flags;
Joanne Koong93309862021-10-27 16:45:00 -07004194 info.map_extra = map->map_extra;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07004195 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004196
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07004197 if (map->btf) {
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08004198 info.btf_id = btf_obj_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07004199 info.btf_key_type_id = map->btf_key_type_id;
4200 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07004201 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08004202 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07004203
Jakub Kicinski52775b32018-01-17 19:13:28 -08004204 if (bpf_map_is_dev_bound(map)) {
4205 err = bpf_map_offload_info_fill(&info, map);
4206 if (err)
4207 return err;
4208 }
4209
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004210 if (copy_to_user(uinfo, &info, info_len) ||
4211 put_user(info_len, &uattr->info.info_len))
4212 return -EFAULT;
4213
4214 return 0;
4215}
4216
Kees Cook63960262020-07-02 15:45:23 -07004217static int bpf_btf_get_info_by_fd(struct file *file,
4218 struct btf *btf,
Martin KaFai Lau62dab842018-05-04 14:49:52 -07004219 const union bpf_attr *attr,
4220 union bpf_attr __user *uattr)
4221{
4222 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4223 u32 info_len = attr->info.info_len;
4224 int err;
4225
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004226 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07004227 if (err)
4228 return err;
4229
4230 return btf_get_info_by_fd(btf, attr, uattr);
4231}
4232
Kees Cook63960262020-07-02 15:45:23 -07004233static int bpf_link_get_info_by_fd(struct file *file,
4234 struct bpf_link *link,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07004235 const union bpf_attr *attr,
4236 union bpf_attr __user *uattr)
4237{
4238 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4239 struct bpf_link_info info;
4240 u32 info_len = attr->info.info_len;
4241 int err;
4242
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004243 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07004244 if (err)
4245 return err;
4246 info_len = min_t(u32, sizeof(info), info_len);
4247
4248 memset(&info, 0, sizeof(info));
4249 if (copy_from_user(&info, uinfo, info_len))
4250 return -EFAULT;
4251
4252 info.type = link->type;
4253 info.id = link->id;
4254 info.prog_id = link->prog->aux->id;
4255
4256 if (link->ops->fill_link_info) {
4257 err = link->ops->fill_link_info(link, &info);
4258 if (err)
4259 return err;
4260 }
4261
4262 if (copy_to_user(uinfo, &info, info_len) ||
4263 put_user(info_len, &uattr->info.info_len))
4264 return -EFAULT;
4265
4266 return 0;
4267}
4268
4269
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004270#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
4271
4272static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
4273 union bpf_attr __user *uattr)
4274{
4275 int ufd = attr->info.bpf_fd;
4276 struct fd f;
4277 int err;
4278
4279 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
4280 return -EINVAL;
4281
4282 f = fdget(ufd);
4283 if (!f.file)
4284 return -EBADFD;
4285
4286 if (f.file->f_op == &bpf_prog_fops)
Kees Cook63960262020-07-02 15:45:23 -07004287 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004288 uattr);
4289 else if (f.file->f_op == &bpf_map_fops)
Kees Cook63960262020-07-02 15:45:23 -07004290 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004291 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07004292 else if (f.file->f_op == &btf_fops)
Kees Cook63960262020-07-02 15:45:23 -07004293 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07004294 else if (f.file->f_op == &bpf_link_fops)
Kees Cook63960262020-07-02 15:45:23 -07004295 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07004296 attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004297 else
4298 err = -EINVAL;
4299
4300 fdput(f);
4301 return err;
4302}
4303
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004304#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
4305
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07004306static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004307{
4308 if (CHECK_ATTR(BPF_BTF_LOAD))
4309 return -EINVAL;
4310
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07004311 if (!bpf_capable())
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004312 return -EPERM;
4313
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07004314 return btf_new_fd(attr, uattr);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004315}
4316
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07004317#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
4318
4319static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
4320{
4321 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
4322 return -EINVAL;
4323
4324 if (!capable(CAP_SYS_ADMIN))
4325 return -EPERM;
4326
4327 return btf_get_fd_by_id(attr->btf_id);
4328}
4329
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004330static int bpf_task_fd_query_copy(const union bpf_attr *attr,
4331 union bpf_attr __user *uattr,
4332 u32 prog_id, u32 fd_type,
4333 const char *buf, u64 probe_offset,
4334 u64 probe_addr)
4335{
4336 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
4337 u32 len = buf ? strlen(buf) : 0, input_len;
4338 int err = 0;
4339
4340 if (put_user(len, &uattr->task_fd_query.buf_len))
4341 return -EFAULT;
4342 input_len = attr->task_fd_query.buf_len;
4343 if (input_len && ubuf) {
4344 if (!len) {
4345 /* nothing to copy, just make ubuf NULL terminated */
4346 char zero = '\0';
4347
4348 if (put_user(zero, ubuf))
4349 return -EFAULT;
4350 } else if (input_len >= len + 1) {
4351 /* ubuf can hold the string with NULL terminator */
4352 if (copy_to_user(ubuf, buf, len + 1))
4353 return -EFAULT;
4354 } else {
4355 /* ubuf cannot hold the string with NULL terminator,
4356 * do a partial copy with NULL terminator.
4357 */
4358 char zero = '\0';
4359
4360 err = -ENOSPC;
4361 if (copy_to_user(ubuf, buf, input_len - 1))
4362 return -EFAULT;
4363 if (put_user(zero, ubuf + input_len - 1))
4364 return -EFAULT;
4365 }
4366 }
4367
4368 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
4369 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
4370 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
4371 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
4372 return -EFAULT;
4373
4374 return err;
4375}
4376
4377#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
4378
4379static int bpf_task_fd_query(const union bpf_attr *attr,
4380 union bpf_attr __user *uattr)
4381{
4382 pid_t pid = attr->task_fd_query.pid;
4383 u32 fd = attr->task_fd_query.fd;
4384 const struct perf_event *event;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004385 struct task_struct *task;
4386 struct file *file;
4387 int err;
4388
4389 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
4390 return -EINVAL;
4391
4392 if (!capable(CAP_SYS_ADMIN))
4393 return -EPERM;
4394
4395 if (attr->task_fd_query.flags != 0)
4396 return -EINVAL;
4397
4398 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
4399 if (!task)
4400 return -ENOENT;
4401
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004402 err = 0;
Eric W. Biedermanb48845a2020-11-20 17:14:22 -06004403 file = fget_task(task, fd);
4404 put_task_struct(task);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004405 if (!file)
Eric W. Biedermanb48845a2020-11-20 17:14:22 -06004406 return -EBADF;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004407
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08004408 if (file->f_op == &bpf_link_fops) {
4409 struct bpf_link *link = file->private_data;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004410
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07004411 if (link->ops == &bpf_raw_tp_link_lops) {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08004412 struct bpf_raw_tp_link *raw_tp =
4413 container_of(link, struct bpf_raw_tp_link, link);
4414 struct bpf_raw_event_map *btp = raw_tp->btp;
4415
4416 err = bpf_task_fd_query_copy(attr, uattr,
4417 raw_tp->link.prog->aux->id,
4418 BPF_FD_TYPE_RAW_TRACEPOINT,
4419 btp->tp->name, 0, 0);
4420 goto put_file;
4421 }
4422 goto out_not_supp;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004423 }
4424
4425 event = perf_get_event(file);
4426 if (!IS_ERR(event)) {
4427 u64 probe_offset, probe_addr;
4428 u32 prog_id, fd_type;
4429 const char *buf;
4430
4431 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4432 &buf, &probe_offset,
4433 &probe_addr);
4434 if (!err)
4435 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4436 fd_type, buf,
4437 probe_offset,
4438 probe_addr);
4439 goto put_file;
4440 }
4441
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08004442out_not_supp:
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004443 err = -ENOTSUPP;
4444put_file:
4445 fput(file);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004446 return err;
4447}
4448
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004449#define BPF_MAP_BATCH_LAST_FIELD batch.flags
4450
4451#define BPF_DO_BATCH(fn) \
4452 do { \
4453 if (!fn) { \
4454 err = -ENOTSUPP; \
4455 goto err_put; \
4456 } \
4457 err = fn(map, attr, uattr); \
4458 } while (0)
4459
4460static int bpf_map_do_batch(const union bpf_attr *attr,
4461 union bpf_attr __user *uattr,
4462 int cmd)
4463{
Daniel Borkmann353050b2021-11-09 18:48:08 +00004464 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH ||
4465 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
4466 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004467 struct bpf_map *map;
4468 int err, ufd;
4469 struct fd f;
4470
4471 if (CHECK_ATTR(BPF_MAP_BATCH))
4472 return -EINVAL;
4473
4474 ufd = attr->batch.map_fd;
4475 f = fdget(ufd);
4476 map = __bpf_map_get(f);
4477 if (IS_ERR(map))
4478 return PTR_ERR(map);
Daniel Borkmann353050b2021-11-09 18:48:08 +00004479 if (has_write)
4480 bpf_map_write_active_inc(map);
4481 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004482 err = -EPERM;
4483 goto err_put;
4484 }
Daniel Borkmann353050b2021-11-09 18:48:08 +00004485 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004486 err = -EPERM;
4487 goto err_put;
4488 }
4489
4490 if (cmd == BPF_MAP_LOOKUP_BATCH)
4491 BPF_DO_BATCH(map->ops->map_lookup_batch);
Yonghong Song05799632020-01-15 10:43:04 -08004492 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4493 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004494 else if (cmd == BPF_MAP_UPDATE_BATCH)
4495 BPF_DO_BATCH(map->ops->map_update_batch);
4496 else
4497 BPF_DO_BATCH(map->ops->map_delete_batch);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004498err_put:
Daniel Borkmann353050b2021-11-09 18:48:08 +00004499 if (has_write)
4500 bpf_map_write_active_dec(map);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004501 fdput(f);
4502 return err;
4503}
4504
Jiri Olsaca748232022-03-16 13:24:12 +01004505#define BPF_LINK_CREATE_LAST_FIELD link_create.kprobe_multi.cookies
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004506static int link_create(union bpf_attr *attr, bpfptr_t uattr)
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004507{
4508 enum bpf_prog_type ptype;
4509 struct bpf_prog *prog;
4510 int ret;
4511
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004512 if (CHECK_ATTR(BPF_LINK_CREATE))
4513 return -EINVAL;
4514
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004515 prog = bpf_prog_get(attr->link_create.prog_fd);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004516 if (IS_ERR(prog))
4517 return PTR_ERR(prog);
4518
4519 ret = bpf_prog_attach_check_attach_type(prog,
4520 attr->link_create.attach_type);
4521 if (ret)
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004522 goto out;
4523
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07004524 switch (prog->type) {
4525 case BPF_PROG_TYPE_EXT:
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07004526 break;
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07004527 case BPF_PROG_TYPE_PERF_EVENT:
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07004528 case BPF_PROG_TYPE_TRACEPOINT:
4529 if (attr->link_create.attach_type != BPF_PERF_EVENT) {
4530 ret = -EINVAL;
4531 goto out;
4532 }
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07004533 break;
Jiri Olsa0dcac272022-03-16 13:24:09 +01004534 case BPF_PROG_TYPE_KPROBE:
4535 if (attr->link_create.attach_type != BPF_PERF_EVENT &&
4536 attr->link_create.attach_type != BPF_TRACE_KPROBE_MULTI) {
4537 ret = -EINVAL;
4538 goto out;
4539 }
Jiri Olsa0dcac272022-03-16 13:24:09 +01004540 break;
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07004541 default:
4542 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4543 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4544 ret = -EINVAL;
4545 goto out;
4546 }
4547 break;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004548 }
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004549
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07004550 switch (prog->type) {
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004551 case BPF_PROG_TYPE_CGROUP_SKB:
4552 case BPF_PROG_TYPE_CGROUP_SOCK:
4553 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4554 case BPF_PROG_TYPE_SOCK_OPS:
4555 case BPF_PROG_TYPE_CGROUP_DEVICE:
4556 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4557 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4558 ret = cgroup_bpf_link_attach(attr, prog);
4559 break;
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07004560 case BPF_PROG_TYPE_EXT:
4561 ret = bpf_tracing_prog_attach(prog,
4562 attr->link_create.target_fd,
Kui-Feng Lee2fcc8242022-05-10 13:59:21 -07004563 attr->link_create.target_btf_id,
4564 attr->link_create.tracing.cookie);
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07004565 break;
4566 case BPF_PROG_TYPE_LSM:
Yonghong Songde4e05c2020-05-09 10:59:01 -07004567 case BPF_PROG_TYPE_TRACING:
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07004568 if (attr->link_create.attach_type != prog->expected_attach_type) {
4569 ret = -EINVAL;
4570 goto out;
4571 }
4572 if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
4573 ret = bpf_raw_tp_link_attach(prog, NULL);
4574 else if (prog->expected_attach_type == BPF_TRACE_ITER)
4575 ret = bpf_iter_link_attach(attr, uattr, prog);
Stanislav Fomichev69fd3372022-06-28 10:43:06 -07004576 else if (prog->expected_attach_type == BPF_LSM_CGROUP)
4577 ret = cgroup_bpf_link_attach(attr, prog);
Andrii Nakryikodf86ca02022-04-20 20:39:43 -07004578 else
4579 ret = bpf_tracing_prog_attach(prog,
4580 attr->link_create.target_fd,
Kui-Feng Lee2fcc8242022-05-10 13:59:21 -07004581 attr->link_create.target_btf_id,
4582 attr->link_create.tracing.cookie);
Yonghong Songde4e05c2020-05-09 10:59:01 -07004583 break;
Jakub Sitnicki7f045a42020-05-31 10:28:38 +02004584 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02004585 case BPF_PROG_TYPE_SK_LOOKUP:
Jakub Sitnicki7f045a42020-05-31 10:28:38 +02004586 ret = netns_bpf_link_create(attr, prog);
4587 break;
Andrii Nakryiko310ad792020-07-28 12:05:27 -07004588#ifdef CONFIG_NET
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07004589 case BPF_PROG_TYPE_XDP:
4590 ret = bpf_xdp_link_attach(attr, prog);
4591 break;
Andrii Nakryiko310ad792020-07-28 12:05:27 -07004592#endif
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07004593 case BPF_PROG_TYPE_PERF_EVENT:
4594 case BPF_PROG_TYPE_TRACEPOINT:
Andrii Nakryikob89fbfb2021-08-15 00:05:57 -07004595 ret = bpf_perf_link_attach(attr, prog);
4596 break;
Jiri Olsa0dcac272022-03-16 13:24:09 +01004597 case BPF_PROG_TYPE_KPROBE:
4598 if (attr->link_create.attach_type == BPF_PERF_EVENT)
4599 ret = bpf_perf_link_attach(attr, prog);
4600 else
4601 ret = bpf_kprobe_multi_link_attach(attr, prog);
4602 break;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004603 default:
4604 ret = -EINVAL;
4605 }
4606
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004607out:
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004608 if (ret < 0)
4609 bpf_prog_put(prog);
4610 return ret;
4611}
4612
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004613#define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4614
4615static int link_update(union bpf_attr *attr)
4616{
4617 struct bpf_prog *old_prog = NULL, *new_prog;
4618 struct bpf_link *link;
4619 u32 flags;
4620 int ret;
4621
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004622 if (CHECK_ATTR(BPF_LINK_UPDATE))
4623 return -EINVAL;
4624
4625 flags = attr->link_update.flags;
4626 if (flags & ~BPF_F_REPLACE)
4627 return -EINVAL;
4628
4629 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4630 if (IS_ERR(link))
4631 return PTR_ERR(link);
4632
4633 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004634 if (IS_ERR(new_prog)) {
4635 ret = PTR_ERR(new_prog);
4636 goto out_put_link;
4637 }
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004638
4639 if (flags & BPF_F_REPLACE) {
4640 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4641 if (IS_ERR(old_prog)) {
4642 ret = PTR_ERR(old_prog);
4643 old_prog = NULL;
4644 goto out_put_progs;
4645 }
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004646 } else if (attr->link_update.old_prog_fd) {
4647 ret = -EINVAL;
4648 goto out_put_progs;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004649 }
4650
Andrii Nakryikof9d0412712020-04-28 17:16:05 -07004651 if (link->ops->update_prog)
4652 ret = link->ops->update_prog(link, new_prog, old_prog);
4653 else
Jakub Sitnickife537392020-05-25 14:29:28 +02004654 ret = -EINVAL;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004655
4656out_put_progs:
4657 if (old_prog)
4658 bpf_prog_put(old_prog);
4659 if (ret)
4660 bpf_prog_put(new_prog);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004661out_put_link:
4662 bpf_link_put(link);
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004663 return ret;
4664}
4665
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07004666#define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4667
4668static int link_detach(union bpf_attr *attr)
4669{
4670 struct bpf_link *link;
4671 int ret;
4672
4673 if (CHECK_ATTR(BPF_LINK_DETACH))
4674 return -EINVAL;
4675
4676 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4677 if (IS_ERR(link))
4678 return PTR_ERR(link);
4679
4680 if (link->ops->detach)
4681 ret = link->ops->detach(link);
4682 else
4683 ret = -EOPNOTSUPP;
4684
4685 bpf_link_put(link);
4686 return ret;
4687}
4688
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004689static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004690{
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004691 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4692}
4693
4694struct bpf_link *bpf_link_by_id(u32 id)
4695{
4696 struct bpf_link *link;
4697
4698 if (!id)
4699 return ERR_PTR(-ENOENT);
4700
4701 spin_lock_bh(&link_idr_lock);
4702 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4703 link = idr_find(&link_idr, id);
4704 if (link) {
4705 if (link->id)
4706 link = bpf_link_inc_not_zero(link);
4707 else
4708 link = ERR_PTR(-EAGAIN);
4709 } else {
4710 link = ERR_PTR(-ENOENT);
4711 }
4712 spin_unlock_bh(&link_idr_lock);
4713 return link;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004714}
4715
Dmitrii Dolgov9f883612022-05-10 17:52:30 +02004716struct bpf_link *bpf_link_get_curr_or_next(u32 *id)
4717{
4718 struct bpf_link *link;
4719
4720 spin_lock_bh(&link_idr_lock);
4721again:
4722 link = idr_get_next(&link_idr, id);
4723 if (link) {
4724 link = bpf_link_inc_not_zero(link);
4725 if (IS_ERR(link)) {
4726 (*id)++;
4727 goto again;
4728 }
4729 }
4730 spin_unlock_bh(&link_idr_lock);
4731
4732 return link;
4733}
4734
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004735#define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4736
4737static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4738{
4739 struct bpf_link *link;
4740 u32 id = attr->link_id;
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004741 int fd;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004742
4743 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4744 return -EINVAL;
4745
4746 if (!capable(CAP_SYS_ADMIN))
4747 return -EPERM;
4748
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004749 link = bpf_link_by_id(id);
4750 if (IS_ERR(link))
4751 return PTR_ERR(link);
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004752
4753 fd = bpf_link_new_fd(link);
4754 if (fd < 0)
4755 bpf_link_put(link);
4756
4757 return fd;
4758}
4759
Song Liud46edd62020-04-30 00:15:04 -07004760DEFINE_MUTEX(bpf_stats_enabled_mutex);
4761
4762static int bpf_stats_release(struct inode *inode, struct file *file)
4763{
4764 mutex_lock(&bpf_stats_enabled_mutex);
4765 static_key_slow_dec(&bpf_stats_enabled_key.key);
4766 mutex_unlock(&bpf_stats_enabled_mutex);
4767 return 0;
4768}
4769
4770static const struct file_operations bpf_stats_fops = {
4771 .release = bpf_stats_release,
4772};
4773
4774static int bpf_enable_runtime_stats(void)
4775{
4776 int fd;
4777
4778 mutex_lock(&bpf_stats_enabled_mutex);
4779
4780 /* Set a very high limit to avoid overflow */
4781 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4782 mutex_unlock(&bpf_stats_enabled_mutex);
4783 return -EBUSY;
4784 }
4785
4786 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4787 if (fd >= 0)
4788 static_key_slow_inc(&bpf_stats_enabled_key.key);
4789
4790 mutex_unlock(&bpf_stats_enabled_mutex);
4791 return fd;
4792}
4793
4794#define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4795
4796static int bpf_enable_stats(union bpf_attr *attr)
4797{
4798
4799 if (CHECK_ATTR(BPF_ENABLE_STATS))
4800 return -EINVAL;
4801
4802 if (!capable(CAP_SYS_ADMIN))
4803 return -EPERM;
4804
4805 switch (attr->enable_stats.type) {
4806 case BPF_STATS_RUN_TIME:
4807 return bpf_enable_runtime_stats();
4808 default:
4809 break;
4810 }
4811 return -EINVAL;
4812}
4813
Yonghong Songac51d992020-05-09 10:59:05 -07004814#define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4815
4816static int bpf_iter_create(union bpf_attr *attr)
4817{
4818 struct bpf_link *link;
4819 int err;
4820
4821 if (CHECK_ATTR(BPF_ITER_CREATE))
4822 return -EINVAL;
4823
4824 if (attr->iter_create.flags)
4825 return -EINVAL;
4826
4827 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4828 if (IS_ERR(link))
4829 return PTR_ERR(link);
4830
4831 err = bpf_iter_new_fd(link);
4832 bpf_link_put(link);
4833
4834 return err;
4835}
4836
YiFei Zhuef153142020-09-15 16:45:40 -07004837#define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4838
4839static int bpf_prog_bind_map(union bpf_attr *attr)
4840{
4841 struct bpf_prog *prog;
4842 struct bpf_map *map;
4843 struct bpf_map **used_maps_old, **used_maps_new;
4844 int i, ret = 0;
4845
4846 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4847 return -EINVAL;
4848
4849 if (attr->prog_bind_map.flags)
4850 return -EINVAL;
4851
4852 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4853 if (IS_ERR(prog))
4854 return PTR_ERR(prog);
4855
4856 map = bpf_map_get(attr->prog_bind_map.map_fd);
4857 if (IS_ERR(map)) {
4858 ret = PTR_ERR(map);
4859 goto out_prog_put;
4860 }
4861
4862 mutex_lock(&prog->aux->used_maps_mutex);
4863
4864 used_maps_old = prog->aux->used_maps;
4865
4866 for (i = 0; i < prog->aux->used_map_cnt; i++)
Stanislav Fomichev1028ae42020-10-02 17:25:44 -07004867 if (used_maps_old[i] == map) {
4868 bpf_map_put(map);
YiFei Zhuef153142020-09-15 16:45:40 -07004869 goto out_unlock;
Stanislav Fomichev1028ae42020-10-02 17:25:44 -07004870 }
YiFei Zhuef153142020-09-15 16:45:40 -07004871
4872 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4873 sizeof(used_maps_new[0]),
4874 GFP_KERNEL);
4875 if (!used_maps_new) {
4876 ret = -ENOMEM;
4877 goto out_unlock;
4878 }
4879
4880 memcpy(used_maps_new, used_maps_old,
4881 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4882 used_maps_new[prog->aux->used_map_cnt] = map;
4883
4884 prog->aux->used_map_cnt++;
4885 prog->aux->used_maps = used_maps_new;
4886
4887 kfree(used_maps_old);
4888
4889out_unlock:
4890 mutex_unlock(&prog->aux->used_maps_mutex);
4891
4892 if (ret)
4893 bpf_map_put(map);
4894out_prog_put:
4895 bpf_prog_put(prog);
4896 return ret;
4897}
4898
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004899static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004900{
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004901 union bpf_attr attr;
Alan Maguirec8644cd2022-05-19 15:25:33 +01004902 bool capable;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004903 int err;
4904
Alan Maguirec8644cd2022-05-19 15:25:33 +01004905 capable = bpf_capable() || !sysctl_unprivileged_bpf_disabled;
4906
4907 /* Intent here is for unprivileged_bpf_disabled to block key object
4908 * creation commands for unprivileged users; other actions depend
4909 * of fd availability and access to bpffs, so are dependent on
4910 * object creation success. Capabilities are later verified for
4911 * operations such as load and map create, so even with unprivileged
4912 * BPF disabled, capability checks are still carried out for these
4913 * and other operations.
4914 */
4915 if (!capable &&
4916 (cmd == BPF_MAP_CREATE || cmd == BPF_PROG_LOAD))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004917 return -EPERM;
4918
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07004919 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004920 if (err)
4921 return err;
4922 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004923
4924 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004925 memset(&attr, 0, sizeof(attr));
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004926 if (copy_from_bpfptr(&attr, uattr, size) != 0)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004927 return -EFAULT;
4928
Chenbo Fengafdb09c2017-10-18 13:00:24 -07004929 err = security_bpf(cmd, &attr, size);
4930 if (err < 0)
4931 return err;
4932
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004933 switch (cmd) {
4934 case BPF_MAP_CREATE:
4935 err = map_create(&attr);
4936 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07004937 case BPF_MAP_LOOKUP_ELEM:
4938 err = map_lookup_elem(&attr);
4939 break;
4940 case BPF_MAP_UPDATE_ELEM:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004941 err = map_update_elem(&attr, uattr);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07004942 break;
4943 case BPF_MAP_DELETE_ELEM:
4944 err = map_delete_elem(&attr);
4945 break;
4946 case BPF_MAP_GET_NEXT_KEY:
4947 err = map_get_next_key(&attr);
4948 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02004949 case BPF_MAP_FREEZE:
4950 err = map_freeze(&attr);
4951 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004952 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08004953 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004954 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01004955 case BPF_OBJ_PIN:
4956 err = bpf_obj_pin(&attr);
4957 break;
4958 case BPF_OBJ_GET:
4959 err = bpf_obj_get(&attr);
4960 break;
Daniel Mackf4324552016-11-23 16:52:27 +01004961 case BPF_PROG_ATTACH:
4962 err = bpf_prog_attach(&attr);
4963 break;
4964 case BPF_PROG_DETACH:
4965 err = bpf_prog_detach(&attr);
4966 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07004967 case BPF_PROG_QUERY:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004968 err = bpf_prog_query(&attr, uattr.user);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07004969 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07004970 case BPF_PROG_TEST_RUN:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004971 err = bpf_prog_test_run(&attr, uattr.user);
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07004972 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004973 case BPF_PROG_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004974 err = bpf_obj_get_next_id(&attr, uattr.user,
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004975 &prog_idr, &prog_idr_lock);
4976 break;
4977 case BPF_MAP_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004978 err = bpf_obj_get_next_id(&attr, uattr.user,
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004979 &map_idr, &map_idr_lock);
4980 break;
Quentin Monnet1b9ed842019-08-20 10:31:50 +01004981 case BPF_BTF_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004982 err = bpf_obj_get_next_id(&attr, uattr.user,
Quentin Monnet1b9ed842019-08-20 10:31:50 +01004983 &btf_idr, &btf_idr_lock);
4984 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07004985 case BPF_PROG_GET_FD_BY_ID:
4986 err = bpf_prog_get_fd_by_id(&attr);
4987 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07004988 case BPF_MAP_GET_FD_BY_ID:
4989 err = bpf_map_get_fd_by_id(&attr);
4990 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004991 case BPF_OBJ_GET_INFO_BY_FD:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004992 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004993 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07004994 case BPF_RAW_TRACEPOINT_OPEN:
4995 err = bpf_raw_tracepoint_open(&attr);
4996 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004997 case BPF_BTF_LOAD:
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07004998 err = bpf_btf_load(&attr, uattr);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004999 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07005000 case BPF_BTF_GET_FD_BY_ID:
5001 err = bpf_btf_get_fd_by_id(&attr);
5002 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07005003 case BPF_TASK_FD_QUERY:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07005004 err = bpf_task_fd_query(&attr, uattr.user);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07005005 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02005006 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
5007 err = map_lookup_and_delete_elem(&attr);
5008 break;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08005009 case BPF_MAP_LOOKUP_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07005010 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08005011 break;
Yonghong Song05799632020-01-15 10:43:04 -08005012 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07005013 err = bpf_map_do_batch(&attr, uattr.user,
Yonghong Song05799632020-01-15 10:43:04 -08005014 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
5015 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08005016 case BPF_MAP_UPDATE_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07005017 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08005018 break;
5019 case BPF_MAP_DELETE_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07005020 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08005021 break;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07005022 case BPF_LINK_CREATE:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07005023 err = link_create(&attr, uattr);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07005024 break;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07005025 case BPF_LINK_UPDATE:
5026 err = link_update(&attr);
5027 break;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07005028 case BPF_LINK_GET_FD_BY_ID:
5029 err = bpf_link_get_fd_by_id(&attr);
5030 break;
5031 case BPF_LINK_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07005032 err = bpf_obj_get_next_id(&attr, uattr.user,
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07005033 &link_idr, &link_idr_lock);
5034 break;
Song Liud46edd62020-04-30 00:15:04 -07005035 case BPF_ENABLE_STATS:
5036 err = bpf_enable_stats(&attr);
5037 break;
Yonghong Songac51d992020-05-09 10:59:05 -07005038 case BPF_ITER_CREATE:
5039 err = bpf_iter_create(&attr);
5040 break;
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07005041 case BPF_LINK_DETACH:
5042 err = link_detach(&attr);
5043 break;
YiFei Zhuef153142020-09-15 16:45:40 -07005044 case BPF_PROG_BIND_MAP:
5045 err = bpf_prog_bind_map(&attr);
5046 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07005047 default:
5048 err = -EINVAL;
5049 break;
5050 }
5051
5052 return err;
5053}
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07005054
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07005055SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
5056{
5057 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
5058}
5059
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07005060static bool syscall_prog_is_valid_access(int off, int size,
5061 enum bpf_access_type type,
5062 const struct bpf_prog *prog,
5063 struct bpf_insn_access_aux *info)
5064{
5065 if (off < 0 || off >= U16_MAX)
5066 return false;
5067 if (off % size != 0)
5068 return false;
5069 return true;
5070}
5071
Alexei Starovoitovb1d18a72022-02-09 15:19:57 -08005072BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07005073{
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07005074 switch (cmd) {
5075 case BPF_MAP_CREATE:
5076 case BPF_MAP_UPDATE_ELEM:
5077 case BPF_MAP_FREEZE:
5078 case BPF_PROG_LOAD:
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07005079 case BPF_BTF_LOAD:
Alexei Starovoitovb1d18a72022-02-09 15:19:57 -08005080 case BPF_LINK_CREATE:
5081 case BPF_RAW_TRACEPOINT_OPEN:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07005082 break;
Alexei Starovoitov86f44fc2022-08-08 20:58:09 -07005083 default:
5084 return -EINVAL;
5085 }
5086 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
5087}
5088
Alexei Starovoitov4e4588f2022-08-10 23:52:28 -07005089
5090/* To shut up -Wmissing-prototypes.
5091 * This function is used by the kernel light skeleton
5092 * to load bpf programs when modules are loaded or during kernel boot.
5093 * See tools/lib/bpf/skel_internal.h
5094 */
5095int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size);
5096
Alexei Starovoitov86f44fc2022-08-08 20:58:09 -07005097int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5098{
5099 struct bpf_prog * __maybe_unused prog;
5100 struct bpf_tramp_run_ctx __maybe_unused run_ctx;
5101
5102 switch (cmd) {
Alexei Starovoitovb1d18a72022-02-09 15:19:57 -08005103#ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */
5104 case BPF_PROG_TEST_RUN:
5105 if (attr->test.data_in || attr->test.data_out ||
5106 attr->test.ctx_out || attr->test.duration ||
5107 attr->test.repeat || attr->test.flags)
5108 return -EINVAL;
5109
5110 prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL);
5111 if (IS_ERR(prog))
5112 return PTR_ERR(prog);
5113
5114 if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
5115 attr->test.ctx_size_in > U16_MAX) {
5116 bpf_prog_put(prog);
5117 return -EINVAL;
5118 }
5119
Kui-Feng Leee384c7b2022-05-10 13:59:20 -07005120 run_ctx.bpf_cookie = 0;
5121 run_ctx.saved_run_ctx = NULL;
5122 if (!__bpf_prog_enter_sleepable(prog, &run_ctx)) {
Alexei Starovoitovb1d18a72022-02-09 15:19:57 -08005123 /* recursion detected */
5124 bpf_prog_put(prog);
5125 return -EBUSY;
5126 }
5127 attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
Kui-Feng Leee384c7b2022-05-10 13:59:20 -07005128 __bpf_prog_exit_sleepable(prog, 0 /* bpf_prog_run does runtime stats */, &run_ctx);
Alexei Starovoitovb1d18a72022-02-09 15:19:57 -08005129 bpf_prog_put(prog);
5130 return 0;
5131#endif
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07005132 default:
Alexei Starovoitov86f44fc2022-08-08 20:58:09 -07005133 return ____bpf_sys_bpf(cmd, attr, size);
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07005134 }
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07005135}
Alexei Starovoitov86f44fc2022-08-08 20:58:09 -07005136EXPORT_SYMBOL(kern_sys_bpf);
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07005137
Pu Lehui3a2daa72021-05-19 14:41:16 +08005138static const struct bpf_func_proto bpf_sys_bpf_proto = {
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07005139 .func = bpf_sys_bpf,
5140 .gpl_only = false,
5141 .ret_type = RET_INTEGER,
5142 .arg1_type = ARG_ANYTHING,
Hao Luo216e3cd2021-12-16 16:31:51 -08005143 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07005144 .arg3_type = ARG_CONST_SIZE,
5145};
5146
5147const struct bpf_func_proto * __weak
5148tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5149{
5150 return bpf_base_func_proto(func_id);
5151}
5152
Alexei Starovoitov3abea082021-05-13 17:36:12 -07005153BPF_CALL_1(bpf_sys_close, u32, fd)
5154{
5155 /* When bpf program calls this helper there should not be
5156 * an fdget() without matching completed fdput().
5157 * This helper is allowed in the following callchain only:
5158 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
5159 */
5160 return close_fd(fd);
5161}
5162
Pu Lehui3a2daa72021-05-19 14:41:16 +08005163static const struct bpf_func_proto bpf_sys_close_proto = {
Alexei Starovoitov3abea082021-05-13 17:36:12 -07005164 .func = bpf_sys_close,
5165 .gpl_only = false,
5166 .ret_type = RET_INTEGER,
5167 .arg1_type = ARG_ANYTHING,
5168};
5169
Kumar Kartikeya Dwivedid6aef082021-10-28 12:04:54 +05305170BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res)
5171{
5172 if (flags)
5173 return -EINVAL;
5174
5175 if (name_sz <= 1 || name[name_sz - 1])
5176 return -EINVAL;
5177
5178 if (!bpf_dump_raw_ok(current_cred()))
5179 return -EPERM;
5180
5181 *res = kallsyms_lookup_name(name);
5182 return *res ? 0 : -ENOENT;
5183}
5184
Joanne Koongdc368e12022-06-16 15:54:07 -07005185static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = {
Kumar Kartikeya Dwivedid6aef082021-10-28 12:04:54 +05305186 .func = bpf_kallsyms_lookup_name,
5187 .gpl_only = false,
5188 .ret_type = RET_INTEGER,
5189 .arg1_type = ARG_PTR_TO_MEM,
Kumar Kartikeya Dwivedid4efb172021-11-23 05:27:31 +05305190 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
Kumar Kartikeya Dwivedid6aef082021-10-28 12:04:54 +05305191 .arg3_type = ARG_ANYTHING,
5192 .arg4_type = ARG_PTR_TO_LONG,
5193};
5194
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07005195static const struct bpf_func_proto *
5196syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5197{
5198 switch (func_id) {
5199 case BPF_FUNC_sys_bpf:
YiFei Zhu14b20b72022-08-16 20:55:16 +00005200 return !perfmon_capable() ? NULL : &bpf_sys_bpf_proto;
Alexei Starovoitov3d784172021-05-13 17:36:11 -07005201 case BPF_FUNC_btf_find_by_name_kind:
5202 return &bpf_btf_find_by_name_kind_proto;
Alexei Starovoitov3abea082021-05-13 17:36:12 -07005203 case BPF_FUNC_sys_close:
5204 return &bpf_sys_close_proto;
Kumar Kartikeya Dwivedid6aef082021-10-28 12:04:54 +05305205 case BPF_FUNC_kallsyms_lookup_name:
5206 return &bpf_kallsyms_lookup_name_proto;
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07005207 default:
5208 return tracing_prog_func_proto(func_id, prog);
5209 }
5210}
5211
5212const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
5213 .get_func_proto = syscall_prog_func_proto,
5214 .is_valid_access = syscall_prog_is_valid_access,
5215};
5216
5217const struct bpf_prog_ops bpf_syscall_prog_ops = {
5218 .test_run = bpf_prog_test_run_syscall,
5219};
Yan Zhu29000052022-04-07 15:07:59 +08005220
5221#ifdef CONFIG_SYSCTL
5222static int bpf_stats_handler(struct ctl_table *table, int write,
5223 void *buffer, size_t *lenp, loff_t *ppos)
5224{
5225 struct static_key *key = (struct static_key *)table->data;
5226 static int saved_val;
5227 int val, ret;
5228 struct ctl_table tmp = {
5229 .data = &val,
5230 .maxlen = sizeof(val),
5231 .mode = table->mode,
5232 .extra1 = SYSCTL_ZERO,
5233 .extra2 = SYSCTL_ONE,
5234 };
5235
5236 if (write && !capable(CAP_SYS_ADMIN))
5237 return -EPERM;
5238
5239 mutex_lock(&bpf_stats_enabled_mutex);
5240 val = saved_val;
5241 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
5242 if (write && !ret && val != saved_val) {
5243 if (val)
5244 static_key_slow_inc(key);
5245 else
5246 static_key_slow_dec(key);
5247 saved_val = val;
5248 }
5249 mutex_unlock(&bpf_stats_enabled_mutex);
5250 return ret;
5251}
5252
5253void __weak unpriv_ebpf_notify(int new_state)
5254{
5255}
5256
5257static int bpf_unpriv_handler(struct ctl_table *table, int write,
5258 void *buffer, size_t *lenp, loff_t *ppos)
5259{
5260 int ret, unpriv_enable = *(int *)table->data;
5261 bool locked_state = unpriv_enable == 1;
5262 struct ctl_table tmp = *table;
5263
5264 if (write && !capable(CAP_SYS_ADMIN))
5265 return -EPERM;
5266
5267 tmp.data = &unpriv_enable;
5268 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
5269 if (write && !ret) {
5270 if (locked_state && unpriv_enable != 1)
5271 return -EPERM;
5272 *(int *)table->data = unpriv_enable;
5273 }
5274
5275 unpriv_ebpf_notify(unpriv_enable);
5276
5277 return ret;
5278}
5279
5280static struct ctl_table bpf_syscall_table[] = {
5281 {
5282 .procname = "unprivileged_bpf_disabled",
5283 .data = &sysctl_unprivileged_bpf_disabled,
5284 .maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
5285 .mode = 0644,
5286 .proc_handler = bpf_unpriv_handler,
5287 .extra1 = SYSCTL_ZERO,
5288 .extra2 = SYSCTL_TWO,
5289 },
5290 {
5291 .procname = "bpf_stats_enabled",
5292 .data = &bpf_stats_enabled_key.key,
5293 .maxlen = sizeof(bpf_stats_enabled_key),
5294 .mode = 0644,
5295 .proc_handler = bpf_stats_handler,
5296 },
5297 { }
5298};
5299
5300static int __init bpf_syscall_sysctl_init(void)
5301{
5302 register_sysctl_init("kernel", bpf_syscall_table);
5303 return 0;
5304}
5305late_initcall(bpf_syscall_sysctl_init);
5306#endif /* CONFIG_SYSCTL */