blob: 1d1cd80a6e670421ec186dfc81a927e09b09c0df [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07002/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003 */
4#include <linux/bpf.h>
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01005#include <linux/bpf_trace.h>
Sean Youngf4364dc2018-05-27 12:24:09 +01006#include <linux/bpf_lirc.h>
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02007#include <linux/bpf_verifier.h>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07008#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07009#include <linux/syscalls.h>
10#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010011#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010012#include <linux/vmalloc.h>
13#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070014#include <linux/anon_inodes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070015#include <linux/fdtable.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070016#include <linux/file.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070017#include <linux/fs.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070018#include <linux/license.h>
19#include <linux/filter.h>
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +010020#include <linux/kernel.h>
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070021#include <linux/idr.h>
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -070022#include <linux/cred.h>
23#include <linux/timekeeping.h>
24#include <linux/ctype.h>
Mark Rutland9ef09e32018-05-03 17:04:59 +010025#include <linux/nospec.h>
Daniel Borkmannbae141f2019-12-06 22:49:34 +010026#include <linux/audit.h>
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -070027#include <uapi/linux/btf.h>
Mike Rapoportca5999f2020-06-08 21:32:38 -070028#include <linux/pgtable.h>
KP Singh9e4e01d2020-03-29 01:43:52 +010029#include <linux/bpf_lsm.h>
Andrii Nakryiko457f4432020-05-29 00:54:20 -070030#include <linux/poll.h>
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +020031#include <linux/bpf-netns.h>
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -070032#include <linux/rcupdate_trace.h>
Roman Gushchin48edc1f2020-12-01 13:58:32 -080033#include <linux/memcontrol.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070034
Daniel Borkmannda765a22019-11-22 21:07:58 +010035#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
36 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
37 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
38#define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070039#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
Daniel Borkmannda765a22019-11-22 21:07:58 +010040#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
41 IS_FD_HASH(map))
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070042
Chenbo Feng6e71b042017-10-18 13:00:22 -070043#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
44
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080045DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070046static DEFINE_IDR(prog_idr);
47static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070048static DEFINE_IDR(map_idr);
49static DEFINE_SPINLOCK(map_idr_lock);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -070050static DEFINE_IDR(link_idr);
51static DEFINE_SPINLOCK(link_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080052
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070053int sysctl_unprivileged_bpf_disabled __read_mostly;
54
Johannes Berg40077e02017-04-11 15:34:58 +020055static const struct bpf_map_ops * const bpf_map_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -080056#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
Johannes Berg40077e02017-04-11 15:34:58 +020057#define BPF_MAP_TYPE(_id, _ops) \
58 [_id] = &_ops,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -070059#define BPF_LINK_TYPE(_id, _name)
Johannes Berg40077e02017-04-11 15:34:58 +020060#include <linux/bpf_types.h>
61#undef BPF_PROG_TYPE
62#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -070063#undef BPF_LINK_TYPE
Johannes Berg40077e02017-04-11 15:34:58 +020064};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070065
Mickaël Salaün752ba562017-08-07 20:45:20 +020066/*
67 * If we're handed a bigger struct than we know of, ensure all the unknown bits
68 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
69 * we don't know about yet.
70 *
71 * There is a ToCToU between this function call and the following
72 * copy_from_user() call. However, this is not a concern since this function is
73 * meant to be a future-proofing of bits.
74 */
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -070075int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070076 size_t expected_size,
77 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020078{
Al Virob7e4b652020-05-08 00:16:31 -040079 int res;
Mickaël Salaün58291a72017-08-07 20:45:19 +020080
Mickaël Salaün752ba562017-08-07 20:45:20 +020081 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
82 return -E2BIG;
83
Mickaël Salaün58291a72017-08-07 20:45:19 +020084 if (actual_size <= expected_size)
85 return 0;
86
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -070087 if (uaddr.is_kernel)
88 res = memchr_inv(uaddr.kernel + expected_size, 0,
89 actual_size - expected_size) == NULL;
90 else
91 res = check_zeroed_user(uaddr.user + expected_size,
92 actual_size - expected_size);
Al Virob7e4b652020-05-08 00:16:31 -040093 if (res < 0)
94 return res;
95 return res ? 0 : -E2BIG;
Mickaël Salaün58291a72017-08-07 20:45:19 +020096}
97
Jakub Kicinskia3884572018-01-11 20:29:09 -080098const struct bpf_map_ops bpf_map_offload_ops = {
Martin KaFai Lauf4d05252020-08-27 18:18:06 -070099 .map_meta_equal = bpf_map_meta_equal,
Jakub Kicinskia3884572018-01-11 20:29:09 -0800100 .map_alloc = bpf_map_offload_map_alloc,
101 .map_free = bpf_map_offload_map_free,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200102 .map_check_btf = map_check_no_btf,
Jakub Kicinskia3884572018-01-11 20:29:09 -0800103};
104
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700105static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
106{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800107 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100108 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700109 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800110 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700111
Mark Rutland9ef09e32018-05-03 17:04:59 +0100112 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800113 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100114 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
115 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800116 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200117 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700118
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800119 if (ops->map_alloc_check) {
120 err = ops->map_alloc_check(attr);
121 if (err)
122 return ERR_PTR(err);
123 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800124 if (attr->map_ifindex)
125 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800126 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200127 if (IS_ERR(map))
128 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800129 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100130 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200131 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700132}
133
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800134static u32 bpf_map_value_size(const struct bpf_map *map)
Brian Vazquez15c14a32020-01-15 10:43:00 -0800135{
136 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
137 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
138 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
139 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
140 return round_up(map->value_size, 8) * num_possible_cpus();
141 else if (IS_FD_MAP(map))
142 return sizeof(u32);
143 else
144 return map->value_size;
145}
146
147static void maybe_wait_bpf_programs(struct bpf_map *map)
148{
149 /* Wait for any running BPF programs to complete so that
150 * userspace, when we return to it, knows that all programs
151 * that could be running use the new map value.
152 */
153 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
154 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
155 synchronize_rcu();
156}
157
158static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
159 void *value, __u64 flags)
160{
161 int err;
162
163 /* Need to create a kthread, thus must support schedule */
164 if (bpf_map_is_dev_bound(map)) {
165 return bpf_map_offload_update_elem(map, key, value, flags);
166 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
Brian Vazquez15c14a32020-01-15 10:43:00 -0800167 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
168 return map->ops->map_update_elem(map, key, value, flags);
Lorenz Bauer13b79d32020-08-21 11:29:45 +0100169 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
170 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
171 return sock_map_update_elem_sys(map, key, value, flags);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800172 } else if (IS_FD_PROG_ARRAY(map)) {
173 return bpf_fd_array_map_update_elem(map, f.file, key, value,
174 flags);
175 }
176
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100177 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800178 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
179 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
180 err = bpf_percpu_hash_update(map, key, value, flags);
181 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
182 err = bpf_percpu_array_update(map, key, value, flags);
183 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
184 err = bpf_percpu_cgroup_storage_update(map, key, value,
185 flags);
186 } else if (IS_FD_ARRAY(map)) {
187 rcu_read_lock();
188 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
189 flags);
190 rcu_read_unlock();
191 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
192 rcu_read_lock();
193 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
194 flags);
195 rcu_read_unlock();
196 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
197 /* rcu_read_lock() is not needed */
198 err = bpf_fd_reuseport_array_update_elem(map, key, value,
199 flags);
200 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
201 map->map_type == BPF_MAP_TYPE_STACK) {
202 err = map->ops->map_push_elem(map, value, flags);
203 } else {
204 rcu_read_lock();
205 err = map->ops->map_update_elem(map, key, value, flags);
206 rcu_read_unlock();
207 }
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100208 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800209 maybe_wait_bpf_programs(map);
210
211 return err;
212}
213
214static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
215 __u64 flags)
216{
217 void *ptr;
218 int err;
219
Brian Vazquezcb4d03a2020-01-15 10:43:01 -0800220 if (bpf_map_is_dev_bound(map))
221 return bpf_map_offload_lookup_elem(map, key, value);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800222
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100223 bpf_disable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800224 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
225 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
226 err = bpf_percpu_hash_copy(map, key, value);
227 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
228 err = bpf_percpu_array_copy(map, key, value);
229 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
230 err = bpf_percpu_cgroup_storage_copy(map, key, value);
231 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
232 err = bpf_stackmap_copy(map, key, value);
233 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
234 err = bpf_fd_array_map_lookup_elem(map, key, value);
235 } else if (IS_FD_HASH(map)) {
236 err = bpf_fd_htab_map_lookup_elem(map, key, value);
237 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
238 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
239 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
240 map->map_type == BPF_MAP_TYPE_STACK) {
241 err = map->ops->map_peek_elem(map, value);
242 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
243 /* struct_ops map requires directly updating "value" */
244 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
245 } else {
246 rcu_read_lock();
247 if (map->ops->map_lookup_elem_sys_only)
248 ptr = map->ops->map_lookup_elem_sys_only(map, key);
249 else
250 ptr = map->ops->map_lookup_elem(map, key);
251 if (IS_ERR(ptr)) {
252 err = PTR_ERR(ptr);
253 } else if (!ptr) {
254 err = -ENOENT;
255 } else {
256 err = 0;
257 if (flags & BPF_F_LOCK)
258 /* lock 'ptr' and copy everything but lock */
259 copy_map_value_locked(map, value, ptr, true);
260 else
261 copy_map_value(map, value, ptr);
262 /* mask lock, since value wasn't zero inited */
263 check_and_init_map_lock(map, value);
264 }
265 rcu_read_unlock();
266 }
267
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +0100268 bpf_enable_instrumentation();
Brian Vazquez15c14a32020-01-15 10:43:00 -0800269 maybe_wait_bpf_programs(map);
270
271 return err;
272}
273
Roman Gushchind5299b62020-12-01 13:58:33 -0800274/* Please, do not use this function outside from the map creation path
275 * (e.g. in map update path) without taking care of setting the active
276 * memory cgroup (see at bpf_map_kmalloc_node() for example).
277 */
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100278static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100279{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100280 /* We really just want to fail instead of triggering OOM killer
281 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
282 * which is used for lower order allocation requests.
283 *
284 * It has been observed that higher order allocation requests done by
285 * vmalloc with __GFP_NORETRY being set might fail due to not trying
286 * to reclaim memory from the page cache, thus we set
287 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100288 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100289
Roman Gushchind5299b62020-12-01 13:58:33 -0800290 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
Christoph Hellwig041de932020-06-01 21:52:02 -0700291 unsigned int flags = 0;
292 unsigned long align = 1;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100293 void *area;
294
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100295 if (size >= SIZE_MAX)
296 return NULL;
297
Andrii Nakryikofc970222019-11-17 09:28:04 -0800298 /* kmalloc()'ed memory can't be mmap()'ed */
Christoph Hellwig041de932020-06-01 21:52:02 -0700299 if (mmapable) {
300 BUG_ON(!PAGE_ALIGNED(size));
301 align = SHMLBA;
302 flags = VM_USERMAP;
303 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
304 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100305 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100306 if (area != NULL)
307 return area;
308 }
Christoph Hellwig041de932020-06-01 21:52:02 -0700309
310 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
311 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
312 flags, numa_node, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100313}
314
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100315void *bpf_map_area_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800316{
317 return __bpf_map_area_alloc(size, numa_node, false);
318}
319
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100320void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800321{
322 return __bpf_map_area_alloc(size, numa_node, true);
323}
324
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100325void bpf_map_area_free(void *area)
326{
327 kvfree(area);
328}
329
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200330static u32 bpf_map_flags_retain_permanent(u32 flags)
331{
332 /* Some map creation flags are not tied to the map object but
333 * rather to the map fd instead, so they have no meaning upon
334 * map object inspection since multiple file descriptors with
335 * different (access) properties can exist here. Thus, given
336 * this has zero meaning for the map itself, lets clear these
337 * from here.
338 */
339 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
340}
341
Jakub Kicinskibd475642018-01-11 20:29:06 -0800342void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
343{
344 map->map_type = attr->map_type;
345 map->key_size = attr->key_size;
346 map->value_size = attr->value_size;
347 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200348 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800349 map->numa_node = bpf_map_attr_numa_node(attr);
350}
351
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700352static int bpf_map_alloc_id(struct bpf_map *map)
353{
354 int id;
355
Shaohua Lib76354c2018-03-27 11:53:21 -0700356 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700357 spin_lock_bh(&map_idr_lock);
358 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
359 if (id > 0)
360 map->id = id;
361 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700362 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700363
364 if (WARN_ON_ONCE(!id))
365 return -ENOSPC;
366
367 return id > 0 ? 0 : id;
368}
369
Jakub Kicinskia3884572018-01-11 20:29:09 -0800370void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700371{
Eric Dumazet930651a2017-09-19 09:15:59 -0700372 unsigned long flags;
373
Jakub Kicinskia3884572018-01-11 20:29:09 -0800374 /* Offloaded maps are removed from the IDR store when their device
375 * disappears - even if someone holds an fd to them they are unusable,
376 * the memory is gone, all ops will fail; they are simply waiting for
377 * refcnt to drop to be freed.
378 */
379 if (!map->id)
380 return;
381
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700382 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700383 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700384 else
385 __acquire(&map_idr_lock);
386
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700387 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800388 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700389
390 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700391 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700392 else
393 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700394}
395
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800396#ifdef CONFIG_MEMCG_KMEM
397static void bpf_map_save_memcg(struct bpf_map *map)
398{
399 map->memcg = get_mem_cgroup_from_mm(current->mm);
400}
401
402static void bpf_map_release_memcg(struct bpf_map *map)
403{
404 mem_cgroup_put(map->memcg);
405}
406
407void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
408 int node)
409{
410 struct mem_cgroup *old_memcg;
411 void *ptr;
412
413 old_memcg = set_active_memcg(map->memcg);
414 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
415 set_active_memcg(old_memcg);
416
417 return ptr;
418}
419
420void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
421{
422 struct mem_cgroup *old_memcg;
423 void *ptr;
424
425 old_memcg = set_active_memcg(map->memcg);
426 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
427 set_active_memcg(old_memcg);
428
429 return ptr;
430}
431
432void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
433 size_t align, gfp_t flags)
434{
435 struct mem_cgroup *old_memcg;
436 void __percpu *ptr;
437
438 old_memcg = set_active_memcg(map->memcg);
439 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
440 set_active_memcg(old_memcg);
441
442 return ptr;
443}
444
445#else
446static void bpf_map_save_memcg(struct bpf_map *map)
447{
448}
449
450static void bpf_map_release_memcg(struct bpf_map *map)
451{
452}
453#endif
454
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700455/* called from workqueue */
456static void bpf_map_free_deferred(struct work_struct *work)
457{
458 struct bpf_map *map = container_of(work, struct bpf_map, work);
459
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700460 security_bpf_map_free(map);
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800461 bpf_map_release_memcg(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700462 /* implementation dependent freeing */
463 map->ops->map_free(map);
464}
465
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100466static void bpf_map_put_uref(struct bpf_map *map)
467{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800468 if (atomic64_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700469 if (map->ops->map_release_uref)
470 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100471 }
472}
473
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700474/* decrement map refcnt and schedule it for freeing via workqueue
475 * (unrelying map implementation ops->map_free() might sleep)
476 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700477static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700478{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800479 if (atomic64_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700480 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700481 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700482 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700483 INIT_WORK(&map->work, bpf_map_free_deferred);
484 schedule_work(&map->work);
485 }
486}
487
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700488void bpf_map_put(struct bpf_map *map)
489{
490 __bpf_map_put(map, true);
491}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700492EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700493
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100494void bpf_map_put_with_uref(struct bpf_map *map)
495{
496 bpf_map_put_uref(map);
497 bpf_map_put(map);
498}
499
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700500static int bpf_map_release(struct inode *inode, struct file *filp)
501{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200502 struct bpf_map *map = filp->private_data;
503
504 if (map->ops->map_release)
505 map->ops->map_release(map, filp);
506
507 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700508 return 0;
509}
510
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200511static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
512{
513 fmode_t mode = f.file->f_mode;
514
515 /* Our file permissions may have been overridden by global
516 * map permissions facing syscall side.
517 */
518 if (READ_ONCE(map->frozen))
519 mode &= ~FMODE_CAN_WRITE;
520 return mode;
521}
522
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100523#ifdef CONFIG_PROC_FS
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800524/* Provides an approximation of the map's memory footprint.
525 * Used only to provide a backward compatibility and display
526 * a reasonable "memlock" info.
527 */
528static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
529{
530 unsigned long size;
531
532 size = round_up(map->key_size + bpf_map_value_size(map), 8);
533
534 return round_up(map->max_entries * size, PAGE_SIZE);
535}
536
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100537static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
538{
539 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100540 const struct bpf_array *array;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100541 u32 type = 0, jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100542
543 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
544 array = container_of(map, struct bpf_array, map);
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100545 type = array->aux->type;
546 jited = array->aux->jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100547 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100548
549 seq_printf(m,
550 "map_type:\t%u\n"
551 "key_size:\t%u\n"
552 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100553 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100554 "map_flags:\t%#x\n"
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800555 "memlock:\t%lu\n"
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200556 "map_id:\t%u\n"
557 "frozen:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100558 map->map_type,
559 map->key_size,
560 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100561 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100562 map->map_flags,
Roman Gushchin80ee81e2020-12-01 13:58:58 -0800563 bpf_map_memory_footprint(map),
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200564 map->id,
565 READ_ONCE(map->frozen));
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100566 if (type) {
567 seq_printf(m, "owner_prog_type:\t%u\n", type);
568 seq_printf(m, "owner_jited:\t%u\n", jited);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200569 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100570}
571#endif
572
Chenbo Feng6e71b042017-10-18 13:00:22 -0700573static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
574 loff_t *ppos)
575{
576 /* We need this handler such that alloc_file() enables
577 * f_mode with FMODE_CAN_READ.
578 */
579 return -EINVAL;
580}
581
582static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
583 size_t siz, loff_t *ppos)
584{
585 /* We need this handler such that alloc_file() enables
586 * f_mode with FMODE_CAN_WRITE.
587 */
588 return -EINVAL;
589}
590
Andrii Nakryikofc970222019-11-17 09:28:04 -0800591/* called for any extra memory-mapped regions (except initial) */
592static void bpf_map_mmap_open(struct vm_area_struct *vma)
593{
594 struct bpf_map *map = vma->vm_file->private_data;
595
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700596 if (vma->vm_flags & VM_MAYWRITE) {
Andrii Nakryikofc970222019-11-17 09:28:04 -0800597 mutex_lock(&map->freeze_mutex);
598 map->writecnt++;
599 mutex_unlock(&map->freeze_mutex);
600 }
601}
602
603/* called for all unmapped memory region (including initial) */
604static void bpf_map_mmap_close(struct vm_area_struct *vma)
605{
606 struct bpf_map *map = vma->vm_file->private_data;
607
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700608 if (vma->vm_flags & VM_MAYWRITE) {
Andrii Nakryikofc970222019-11-17 09:28:04 -0800609 mutex_lock(&map->freeze_mutex);
610 map->writecnt--;
611 mutex_unlock(&map->freeze_mutex);
612 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800613}
614
615static const struct vm_operations_struct bpf_map_default_vmops = {
616 .open = bpf_map_mmap_open,
617 .close = bpf_map_mmap_close,
618};
619
620static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
621{
622 struct bpf_map *map = filp->private_data;
623 int err;
624
625 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
626 return -ENOTSUPP;
627
628 if (!(vma->vm_flags & VM_SHARED))
629 return -EINVAL;
630
631 mutex_lock(&map->freeze_mutex);
632
Andrii Nakryikodfeb3762020-05-18 22:38:24 -0700633 if (vma->vm_flags & VM_WRITE) {
634 if (map->frozen) {
635 err = -EPERM;
636 goto out;
637 }
638 /* map is meant to be read-only, so do not allow mapping as
639 * writable, because it's possible to leak a writable page
640 * reference and allows user-space to still modify it after
641 * freezing, while verifier will assume contents do not change
642 */
643 if (map->map_flags & BPF_F_RDONLY_PROG) {
644 err = -EACCES;
645 goto out;
646 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800647 }
648
649 /* set default open/close callbacks */
650 vma->vm_ops = &bpf_map_default_vmops;
651 vma->vm_private_data = map;
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700652 vma->vm_flags &= ~VM_MAYEXEC;
653 if (!(vma->vm_flags & VM_WRITE))
654 /* disallow re-mapping with PROT_WRITE */
655 vma->vm_flags &= ~VM_MAYWRITE;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800656
657 err = map->ops->map_mmap(map, vma);
658 if (err)
659 goto out;
660
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700661 if (vma->vm_flags & VM_MAYWRITE)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800662 map->writecnt++;
663out:
664 mutex_unlock(&map->freeze_mutex);
665 return err;
666}
667
Andrii Nakryiko457f4432020-05-29 00:54:20 -0700668static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
669{
670 struct bpf_map *map = filp->private_data;
671
672 if (map->ops->map_poll)
673 return map->ops->map_poll(map, filp, pts);
674
675 return EPOLLERR;
676}
677
Chenbo Fengf66e4482017-10-18 13:00:26 -0700678const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100679#ifdef CONFIG_PROC_FS
680 .show_fdinfo = bpf_map_show_fdinfo,
681#endif
682 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700683 .read = bpf_dummy_read,
684 .write = bpf_dummy_write,
Andrii Nakryikofc970222019-11-17 09:28:04 -0800685 .mmap = bpf_map_mmap,
Andrii Nakryiko457f4432020-05-29 00:54:20 -0700686 .poll = bpf_map_poll,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700687};
688
Chenbo Feng6e71b042017-10-18 13:00:22 -0700689int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100690{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700691 int ret;
692
693 ret = security_bpf_map(map, OPEN_FMODE(flags));
694 if (ret < 0)
695 return ret;
696
Daniel Borkmannaa797812015-10-29 14:58:06 +0100697 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700698 flags | O_CLOEXEC);
699}
700
701int bpf_get_file_flag(int flags)
702{
703 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
704 return -EINVAL;
705 if (flags & BPF_F_RDONLY)
706 return O_RDONLY;
707 if (flags & BPF_F_WRONLY)
708 return O_WRONLY;
709 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100710}
711
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700712/* helper macro to check that unused fields 'union bpf_attr' are zero */
713#define CHECK_ATTR(CMD) \
714 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
715 sizeof(attr->CMD##_LAST_FIELD), 0, \
716 sizeof(*attr) - \
717 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
718 sizeof(attr->CMD##_LAST_FIELD)) != NULL
719
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700720/* dst and src must have at least "size" number of bytes.
721 * Return strlen on success and < 0 on error.
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700722 */
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700723int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700724{
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700725 const char *end = src + size;
726 const char *orig_src = src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700727
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700728 memset(dst, 0, size);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200729 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700730 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200731 if (!isalnum(*src) &&
732 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700733 return -EINVAL;
734 *dst++ = *src++;
735 }
736
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700737 /* No '\0' found in "size" number of bytes */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700738 if (src == end)
739 return -EINVAL;
740
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700741 return src - orig_src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700742}
743
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200744int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800745 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200746 const struct btf_type *key_type,
747 const struct btf_type *value_type)
748{
749 return -ENOTSUPP;
750}
751
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800752static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200753 u32 btf_key_id, u32 btf_value_id)
754{
755 const struct btf_type *key_type, *value_type;
756 u32 key_size, value_size;
757 int ret = 0;
758
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200759 /* Some maps allow key to be unspecified. */
760 if (btf_key_id) {
761 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
762 if (!key_type || key_size != map->key_size)
763 return -EINVAL;
764 } else {
765 key_type = btf_type_by_id(btf, 0);
766 if (!map->ops->map_check_btf)
767 return -EINVAL;
768 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200769
770 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
771 if (!value_type || value_size != map->value_size)
772 return -EINVAL;
773
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800774 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
775
776 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200777 if (map->map_flags & BPF_F_RDONLY_PROG)
778 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800779 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -0800780 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700781 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
KP Singh8ea63682020-08-25 20:29:17 +0200782 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
KP Singh4cf1bc12020-11-06 10:37:40 +0000783 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
784 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800785 return -ENOTSUPP;
786 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
787 map->value_size) {
788 WARN_ONCE(1,
789 "verifier bug spin_lock_off %d value_size %d\n",
790 map->spin_lock_off, map->value_size);
791 return -EFAULT;
792 }
793 }
794
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200795 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800796 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200797
798 return ret;
799}
800
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800801#define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700802/* called via syscall */
803static int map_create(union bpf_attr *attr)
804{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700805 int numa_node = bpf_map_attr_numa_node(attr);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700806 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700807 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700808 int err;
809
810 err = CHECK_ATTR(BPF_MAP_CREATE);
811 if (err)
812 return -EINVAL;
813
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800814 if (attr->btf_vmlinux_value_type_id) {
815 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
816 attr->btf_key_type_id || attr->btf_value_type_id)
817 return -EINVAL;
818 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
819 return -EINVAL;
820 }
821
Chenbo Feng6e71b042017-10-18 13:00:22 -0700822 f_flags = bpf_get_file_flag(attr->map_flags);
823 if (f_flags < 0)
824 return f_flags;
825
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700826 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700827 ((unsigned int)numa_node >= nr_node_ids ||
828 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700829 return -EINVAL;
830
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700831 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
832 map = find_and_alloc_map(attr);
833 if (IS_ERR(map))
834 return PTR_ERR(map);
835
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700836 err = bpf_obj_name_cpy(map->name, attr->map_name,
837 sizeof(attr->map_name));
838 if (err < 0)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700839 goto free_map;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700840
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800841 atomic64_set(&map->refcnt, 1);
842 atomic64_set(&map->usercnt, 1);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800843 mutex_init(&map->freeze_mutex);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700844
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800845 map->spin_lock_off = -EINVAL;
846 if (attr->btf_key_type_id || attr->btf_value_type_id ||
847 /* Even the map's value is a kernel's struct,
848 * the bpf_prog.o must have BTF to begin with
849 * to figure out the corresponding kernel's
850 * counter part. Thus, attr->btf_fd has
851 * to be valid also.
852 */
853 attr->btf_vmlinux_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700854 struct btf *btf;
855
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700856 btf = btf_get_by_fd(attr->btf_fd);
857 if (IS_ERR(btf)) {
858 err = PTR_ERR(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700859 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700860 }
Alexei Starovoitov350a5c42021-03-07 14:52:48 -0800861 if (btf_is_kernel(btf)) {
862 btf_put(btf);
863 err = -EACCES;
864 goto free_map;
865 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800866 map->btf = btf;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700867
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800868 if (attr->btf_value_type_id) {
869 err = map_check_btf(map, btf, attr->btf_key_type_id,
870 attr->btf_value_type_id);
871 if (err)
872 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700873 }
874
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700875 map->btf_key_type_id = attr->btf_key_type_id;
876 map->btf_value_type_id = attr->btf_value_type_id;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800877 map->btf_vmlinux_value_type_id =
878 attr->btf_vmlinux_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700879 }
880
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700881 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700882 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700883 goto free_map;
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700884
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700885 err = bpf_map_alloc_id(map);
886 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700887 goto free_map_sec;
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700888
Roman Gushchin48edc1f2020-12-01 13:58:32 -0800889 bpf_map_save_memcg(map);
890
Chenbo Feng6e71b042017-10-18 13:00:22 -0700891 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700892 if (err < 0) {
893 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800894 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700895 * bpf_map_alloc_id() has published the map
896 * to the userspace and the userspace may
897 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
898 */
Peng Sun352d20d2019-02-27 22:36:25 +0800899 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700900 return err;
901 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700902
903 return err;
904
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700905free_map_sec:
906 security_bpf_map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700907free_map:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700908 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700909 map->ops->map_free(map);
910 return err;
911}
912
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700913/* if error is returned, fd is released.
914 * On success caller should complete fd access with matching fdput()
915 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100916struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700917{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700918 if (!f.file)
919 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700920 if (f.file->f_op != &bpf_map_fops) {
921 fdput(f);
922 return ERR_PTR(-EINVAL);
923 }
924
Daniel Borkmannc2101292015-10-29 14:58:07 +0100925 return f.file->private_data;
926}
927
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800928void bpf_map_inc(struct bpf_map *map)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100929{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800930 atomic64_inc(&map->refcnt);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100931}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700932EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100933
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800934void bpf_map_inc_with_uref(struct bpf_map *map)
935{
936 atomic64_inc(&map->refcnt);
937 atomic64_inc(&map->usercnt);
938}
939EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
940
Martin KaFai Lau1ed4d922020-02-25 15:04:21 -0800941struct bpf_map *bpf_map_get(u32 ufd)
942{
943 struct fd f = fdget(ufd);
944 struct bpf_map *map;
945
946 map = __bpf_map_get(f);
947 if (IS_ERR(map))
948 return map;
949
950 bpf_map_inc(map);
951 fdput(f);
952
953 return map;
954}
955
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100956struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100957{
958 struct fd f = fdget(ufd);
959 struct bpf_map *map;
960
961 map = __bpf_map_get(f);
962 if (IS_ERR(map))
963 return map;
964
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800965 bpf_map_inc_with_uref(map);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100966 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700967
968 return map;
969}
970
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700971/* map_idr_lock should have been held */
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800972static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700973{
974 int refold;
975
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800976 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700977 if (!refold)
978 return ERR_PTR(-ENOENT);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700979 if (uref)
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800980 atomic64_inc(&map->usercnt);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700981
982 return map;
983}
984
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800985struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700986{
987 spin_lock_bh(&map_idr_lock);
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800988 map = __bpf_map_inc_not_zero(map, false);
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700989 spin_unlock_bh(&map_idr_lock);
990
991 return map;
992}
993EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
994
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800995int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
996{
997 return -ENOTSUPP;
998}
999
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001000static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1001{
1002 if (key_size)
1003 return memdup_user(ukey, key_size);
1004
1005 if (ukey)
1006 return ERR_PTR(-EINVAL);
1007
1008 return NULL;
1009}
1010
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001011static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1012{
1013 if (key_size)
1014 return memdup_bpfptr(ukey, key_size);
1015
1016 if (!bpfptr_is_null(ukey))
1017 return ERR_PTR(-EINVAL);
1018
1019 return NULL;
1020}
1021
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001022/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001023#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001024
1025static int map_lookup_elem(union bpf_attr *attr)
1026{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001027 void __user *ukey = u64_to_user_ptr(attr->key);
1028 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001029 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001030 struct bpf_map *map;
Brian Vazquez15c14a32020-01-15 10:43:00 -08001031 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001032 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001033 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001034 int err;
1035
1036 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1037 return -EINVAL;
1038
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001039 if (attr->flags & ~BPF_F_LOCK)
1040 return -EINVAL;
1041
Daniel Borkmann592867b2015-09-08 18:00:09 +02001042 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001043 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001044 if (IS_ERR(map))
1045 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001046 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001047 err = -EPERM;
1048 goto err_put;
1049 }
1050
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001051 if ((attr->flags & BPF_F_LOCK) &&
1052 !map_value_has_spin_lock(map)) {
1053 err = -EINVAL;
1054 goto err_put;
1055 }
1056
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001057 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001058 if (IS_ERR(key)) {
1059 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001060 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001061 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001062
Brian Vazquez15c14a32020-01-15 10:43:00 -08001063 value_size = bpf_map_value_size(map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001064
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001065 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001066 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001067 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001068 goto free_key;
1069
Brian Vazquez15c14a32020-01-15 10:43:00 -08001070 err = bpf_map_copy_value(map, key, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001071 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001072 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001073
1074 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001075 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001076 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001077
1078 err = 0;
1079
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001080free_value:
1081 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001082free_key:
1083 kfree(key);
1084err_put:
1085 fdput(f);
1086 return err;
1087}
1088
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001089
Alexei Starovoitov3274f522014-11-13 17:36:44 -08001090#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001091
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001092static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001093{
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001094 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1095 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001096 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001097 struct bpf_map *map;
1098 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001099 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001100 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001101 int err;
1102
1103 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1104 return -EINVAL;
1105
Daniel Borkmann592867b2015-09-08 18:00:09 +02001106 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001107 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001108 if (IS_ERR(map))
1109 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001110 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001111 err = -EPERM;
1112 goto err_put;
1113 }
1114
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001115 if ((attr->flags & BPF_F_LOCK) &&
1116 !map_value_has_spin_lock(map)) {
1117 err = -EINVAL;
1118 goto err_put;
1119 }
1120
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001121 key = ___bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001122 if (IS_ERR(key)) {
1123 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001124 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001125 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001126
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001127 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001128 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +00001129 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1130 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001131 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1132 else
1133 value_size = map->value_size;
1134
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001135 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001136 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001137 if (!value)
1138 goto free_key;
1139
1140 err = -EFAULT;
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07001141 if (copy_from_bpfptr(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001142 goto free_value;
1143
Brian Vazquez15c14a32020-01-15 10:43:00 -08001144 err = bpf_map_update_value(map, f, key, value, attr->flags);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001145
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001146free_value:
1147 kfree(value);
1148free_key:
1149 kfree(key);
1150err_put:
1151 fdput(f);
1152 return err;
1153}
1154
1155#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1156
1157static int map_delete_elem(union bpf_attr *attr)
1158{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001159 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001160 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001161 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001162 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001163 void *key;
1164 int err;
1165
1166 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1167 return -EINVAL;
1168
Daniel Borkmann592867b2015-09-08 18:00:09 +02001169 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001170 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001171 if (IS_ERR(map))
1172 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001173 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001174 err = -EPERM;
1175 goto err_put;
1176 }
1177
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001178 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001179 if (IS_ERR(key)) {
1180 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001181 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001182 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001183
Jakub Kicinskia3884572018-01-11 20:29:09 -08001184 if (bpf_map_is_dev_bound(map)) {
1185 err = bpf_map_offload_delete_elem(map, key);
1186 goto out;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001187 } else if (IS_FD_PROG_ARRAY(map) ||
1188 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1189 /* These maps require sleepable context */
Daniel Borkmannda765a22019-11-22 21:07:58 +01001190 err = map->ops->map_delete_elem(map, key);
1191 goto out;
Jakub Kicinskia3884572018-01-11 20:29:09 -08001192 }
1193
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001194 bpf_disable_instrumentation();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001195 rcu_read_lock();
1196 err = map->ops->map_delete_elem(map, key);
1197 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001198 bpf_enable_instrumentation();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001199 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001200out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001201 kfree(key);
1202err_put:
1203 fdput(f);
1204 return err;
1205}
1206
1207/* last field in 'union bpf_attr' used by this command */
1208#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1209
1210static int map_get_next_key(union bpf_attr *attr)
1211{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001212 void __user *ukey = u64_to_user_ptr(attr->key);
1213 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001214 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001215 struct bpf_map *map;
1216 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001217 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001218 int err;
1219
1220 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1221 return -EINVAL;
1222
Daniel Borkmann592867b2015-09-08 18:00:09 +02001223 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001224 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001225 if (IS_ERR(map))
1226 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001227 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001228 err = -EPERM;
1229 goto err_put;
1230 }
1231
Teng Qin8fe45922017-04-24 19:00:37 -07001232 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001233 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001234 if (IS_ERR(key)) {
1235 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001236 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001237 }
Teng Qin8fe45922017-04-24 19:00:37 -07001238 } else {
1239 key = NULL;
1240 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001241
1242 err = -ENOMEM;
1243 next_key = kmalloc(map->key_size, GFP_USER);
1244 if (!next_key)
1245 goto free_key;
1246
Jakub Kicinskia3884572018-01-11 20:29:09 -08001247 if (bpf_map_is_dev_bound(map)) {
1248 err = bpf_map_offload_get_next_key(map, key, next_key);
1249 goto out;
1250 }
1251
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001252 rcu_read_lock();
1253 err = map->ops->map_get_next_key(map, key, next_key);
1254 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001255out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001256 if (err)
1257 goto free_next_key;
1258
1259 err = -EFAULT;
1260 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1261 goto free_next_key;
1262
1263 err = 0;
1264
1265free_next_key:
1266 kfree(next_key);
1267free_key:
1268 kfree(key);
1269err_put:
1270 fdput(f);
1271 return err;
1272}
1273
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001274int generic_map_delete_batch(struct bpf_map *map,
1275 const union bpf_attr *attr,
1276 union bpf_attr __user *uattr)
1277{
1278 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1279 u32 cp, max_count;
1280 int err = 0;
1281 void *key;
1282
1283 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1284 return -EINVAL;
1285
1286 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1287 !map_value_has_spin_lock(map)) {
1288 return -EINVAL;
1289 }
1290
1291 max_count = attr->batch.count;
1292 if (!max_count)
1293 return 0;
1294
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001295 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1296 if (!key)
1297 return -ENOMEM;
1298
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001299 for (cp = 0; cp < max_count; cp++) {
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001300 err = -EFAULT;
1301 if (copy_from_user(key, keys + cp * map->key_size,
1302 map->key_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001303 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001304
1305 if (bpf_map_is_dev_bound(map)) {
1306 err = bpf_map_offload_delete_elem(map, key);
1307 break;
1308 }
1309
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001310 bpf_disable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001311 rcu_read_lock();
1312 err = map->ops->map_delete_elem(map, key);
1313 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001314 bpf_enable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001315 maybe_wait_bpf_programs(map);
1316 if (err)
1317 break;
1318 }
1319 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1320 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001321
1322 kfree(key);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001323 return err;
1324}
1325
1326int generic_map_update_batch(struct bpf_map *map,
1327 const union bpf_attr *attr,
1328 union bpf_attr __user *uattr)
1329{
1330 void __user *values = u64_to_user_ptr(attr->batch.values);
1331 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1332 u32 value_size, cp, max_count;
1333 int ufd = attr->map_fd;
1334 void *key, *value;
1335 struct fd f;
1336 int err = 0;
1337
1338 f = fdget(ufd);
1339 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1340 return -EINVAL;
1341
1342 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1343 !map_value_has_spin_lock(map)) {
1344 return -EINVAL;
1345 }
1346
1347 value_size = bpf_map_value_size(map);
1348
1349 max_count = attr->batch.count;
1350 if (!max_count)
1351 return 0;
1352
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001353 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1354 if (!key)
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001355 return -ENOMEM;
1356
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001357 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1358 if (!value) {
1359 kfree(key);
1360 return -ENOMEM;
1361 }
1362
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001363 for (cp = 0; cp < max_count; cp++) {
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001364 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001365 if (copy_from_user(key, keys + cp * map->key_size,
1366 map->key_size) ||
1367 copy_from_user(value, values + cp * value_size, value_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001368 break;
1369
1370 err = bpf_map_update_value(map, f, key, value,
1371 attr->batch.elem_flags);
1372
1373 if (err)
1374 break;
1375 }
1376
1377 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1378 err = -EFAULT;
1379
1380 kfree(value);
1381 kfree(key);
1382 return err;
1383}
1384
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001385#define MAP_LOOKUP_RETRIES 3
1386
1387int generic_map_lookup_batch(struct bpf_map *map,
1388 const union bpf_attr *attr,
1389 union bpf_attr __user *uattr)
1390{
1391 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1392 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1393 void __user *values = u64_to_user_ptr(attr->batch.values);
1394 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1395 void *buf, *buf_prevkey, *prev_key, *key, *value;
1396 int err, retry = MAP_LOOKUP_RETRIES;
1397 u32 value_size, cp, max_count;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001398
1399 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1400 return -EINVAL;
1401
1402 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1403 !map_value_has_spin_lock(map))
1404 return -EINVAL;
1405
1406 value_size = bpf_map_value_size(map);
1407
1408 max_count = attr->batch.count;
1409 if (!max_count)
1410 return 0;
1411
1412 if (put_user(0, &uattr->batch.count))
1413 return -EFAULT;
1414
1415 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1416 if (!buf_prevkey)
1417 return -ENOMEM;
1418
1419 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1420 if (!buf) {
Denis Efremovbb2359f2020-06-01 19:28:14 +03001421 kfree(buf_prevkey);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001422 return -ENOMEM;
1423 }
1424
1425 err = -EFAULT;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001426 prev_key = NULL;
1427 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1428 goto free_buf;
1429 key = buf;
1430 value = key + map->key_size;
1431 if (ubatch)
1432 prev_key = buf_prevkey;
1433
1434 for (cp = 0; cp < max_count;) {
1435 rcu_read_lock();
1436 err = map->ops->map_get_next_key(map, prev_key, key);
1437 rcu_read_unlock();
1438 if (err)
1439 break;
1440 err = bpf_map_copy_value(map, key, value,
1441 attr->batch.elem_flags);
1442
1443 if (err == -ENOENT) {
1444 if (retry) {
1445 retry--;
1446 continue;
1447 }
1448 err = -EINTR;
1449 break;
1450 }
1451
1452 if (err)
1453 goto free_buf;
1454
1455 if (copy_to_user(keys + cp * map->key_size, key,
1456 map->key_size)) {
1457 err = -EFAULT;
1458 goto free_buf;
1459 }
1460 if (copy_to_user(values + cp * value_size, value, value_size)) {
1461 err = -EFAULT;
1462 goto free_buf;
1463 }
1464
1465 if (!prev_key)
1466 prev_key = buf_prevkey;
1467
1468 swap(prev_key, key);
1469 retry = MAP_LOOKUP_RETRIES;
1470 cp++;
1471 }
1472
1473 if (err == -EFAULT)
1474 goto free_buf;
1475
1476 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1477 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1478 err = -EFAULT;
1479
1480free_buf:
1481 kfree(buf_prevkey);
1482 kfree(buf);
1483 return err;
1484}
1485
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001486#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1487
1488static int map_lookup_and_delete_elem(union bpf_attr *attr)
1489{
1490 void __user *ukey = u64_to_user_ptr(attr->key);
1491 void __user *uvalue = u64_to_user_ptr(attr->value);
1492 int ufd = attr->map_fd;
1493 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001494 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001495 u32 value_size;
1496 struct fd f;
1497 int err;
1498
1499 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1500 return -EINVAL;
1501
1502 f = fdget(ufd);
1503 map = __bpf_map_get(f);
1504 if (IS_ERR(map))
1505 return PTR_ERR(map);
Anton Protopopov1ea0f912020-05-27 18:56:59 +00001506 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1507 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001508 err = -EPERM;
1509 goto err_put;
1510 }
1511
1512 key = __bpf_copy_key(ukey, map->key_size);
1513 if (IS_ERR(key)) {
1514 err = PTR_ERR(key);
1515 goto err_put;
1516 }
1517
1518 value_size = map->value_size;
1519
1520 err = -ENOMEM;
1521 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1522 if (!value)
1523 goto free_key;
1524
1525 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1526 map->map_type == BPF_MAP_TYPE_STACK) {
1527 err = map->ops->map_pop_elem(map, value);
1528 } else {
1529 err = -ENOTSUPP;
1530 }
1531
1532 if (err)
1533 goto free_value;
1534
Wei Yongjun7f645462020-04-30 08:18:51 +00001535 if (copy_to_user(uvalue, value, value_size) != 0) {
1536 err = -EFAULT;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001537 goto free_value;
Wei Yongjun7f645462020-04-30 08:18:51 +00001538 }
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001539
1540 err = 0;
1541
1542free_value:
1543 kfree(value);
1544free_key:
1545 kfree(key);
1546err_put:
1547 fdput(f);
1548 return err;
1549}
1550
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001551#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1552
1553static int map_freeze(const union bpf_attr *attr)
1554{
1555 int err = 0, ufd = attr->map_fd;
1556 struct bpf_map *map;
1557 struct fd f;
1558
1559 if (CHECK_ATTR(BPF_MAP_FREEZE))
1560 return -EINVAL;
1561
1562 f = fdget(ufd);
1563 map = __bpf_map_get(f);
1564 if (IS_ERR(map))
1565 return PTR_ERR(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -08001566
Martin KaFai Lau849b4d92020-03-04 17:34:54 -08001567 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1568 fdput(f);
1569 return -ENOTSUPP;
1570 }
1571
Andrii Nakryikofc970222019-11-17 09:28:04 -08001572 mutex_lock(&map->freeze_mutex);
1573
1574 if (map->writecnt) {
1575 err = -EBUSY;
1576 goto err_put;
1577 }
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001578 if (READ_ONCE(map->frozen)) {
1579 err = -EBUSY;
1580 goto err_put;
1581 }
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07001582 if (!bpf_capable()) {
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001583 err = -EPERM;
1584 goto err_put;
1585 }
1586
1587 WRITE_ONCE(map->frozen, true);
1588err_put:
Andrii Nakryikofc970222019-11-17 09:28:04 -08001589 mutex_unlock(&map->freeze_mutex);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001590 fdput(f);
1591 return err;
1592}
1593
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001594static const struct bpf_prog_ops * const bpf_prog_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001595#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001596 [_id] = & _name ## _prog_ops,
1597#define BPF_MAP_TYPE(_id, _ops)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001598#define BPF_LINK_TYPE(_id, _name)
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001599#include <linux/bpf_types.h>
1600#undef BPF_PROG_TYPE
1601#undef BPF_MAP_TYPE
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07001602#undef BPF_LINK_TYPE
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001603};
1604
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001605static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1606{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001607 const struct bpf_prog_ops *ops;
1608
1609 if (type >= ARRAY_SIZE(bpf_prog_types))
1610 return -EINVAL;
1611 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1612 ops = bpf_prog_types[type];
1613 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001614 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001615
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001616 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001617 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001618 else
1619 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001620 prog->type = type;
1621 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001622}
1623
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001624enum bpf_audit {
1625 BPF_AUDIT_LOAD,
1626 BPF_AUDIT_UNLOAD,
1627 BPF_AUDIT_MAX,
1628};
1629
1630static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1631 [BPF_AUDIT_LOAD] = "LOAD",
1632 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1633};
1634
1635static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1636{
1637 struct audit_context *ctx = NULL;
1638 struct audit_buffer *ab;
1639
1640 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1641 return;
1642 if (audit_enabled == AUDIT_OFF)
1643 return;
1644 if (op == BPF_AUDIT_LOAD)
1645 ctx = audit_context();
1646 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1647 if (unlikely(!ab))
1648 return;
1649 audit_log_format(ab, "prog-id=%u op=%s",
1650 prog->aux->id, bpf_audit_str[op]);
1651 audit_log_end(ab);
1652}
1653
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001654static int bpf_prog_alloc_id(struct bpf_prog *prog)
1655{
1656 int id;
1657
Shaohua Lib76354c2018-03-27 11:53:21 -07001658 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001659 spin_lock_bh(&prog_idr_lock);
1660 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1661 if (id > 0)
1662 prog->aux->id = id;
1663 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001664 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001665
1666 /* id is in [1, INT_MAX) */
1667 if (WARN_ON_ONCE(!id))
1668 return -ENOSPC;
1669
1670 return id > 0 ? 0 : id;
1671}
1672
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001673void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001674{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001675 /* cBPF to eBPF migrations are currently not in the idr store.
1676 * Offloaded programs are removed from the store when their device
1677 * disappears - even if someone grabs an fd to them they are unusable,
1678 * simply waiting for refcnt to drop to be freed.
1679 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001680 if (!prog->aux->id)
1681 return;
1682
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001683 if (do_idr_lock)
1684 spin_lock_bh(&prog_idr_lock);
1685 else
1686 __acquire(&prog_idr_lock);
1687
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001688 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001689 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001690
1691 if (do_idr_lock)
1692 spin_unlock_bh(&prog_idr_lock);
1693 else
1694 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001695}
1696
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001697static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001698{
1699 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1700
Daniel Borkmann3b4d9eb2019-10-22 23:30:38 +02001701 kvfree(aux->func_info);
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08001702 kfree(aux->func_info_aux);
Roman Gushchin3ac1f012020-12-01 13:58:59 -08001703 free_uid(aux->user);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001704 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001705 bpf_prog_free(aux->prog);
1706}
1707
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001708static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1709{
1710 bpf_prog_kallsyms_del_all(prog);
1711 btf_put(prog->aux->btf);
Martin KaFai Laue16301f2021-03-24 18:51:30 -07001712 kvfree(prog->aux->jited_linfo);
1713 kvfree(prog->aux->linfo);
Martin KaFai Laue6ac2452021-03-24 18:51:42 -07001714 kfree(prog->aux->kfunc_tab);
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08001715 if (prog->aux->attach_btf)
1716 btf_put(prog->aux->attach_btf);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001717
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07001718 if (deferred) {
1719 if (prog->aux->sleepable)
1720 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
1721 else
1722 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1723 } else {
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001724 __bpf_prog_put_rcu(&prog->aux->rcu);
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07001725 }
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001726}
1727
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001728static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001729{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001730 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001731 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001732 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001733 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001734 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001735 __bpf_prog_put_noref(prog, true);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001736 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001737}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001738
1739void bpf_prog_put(struct bpf_prog *prog)
1740{
1741 __bpf_prog_put(prog, true);
1742}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001743EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001744
1745static int bpf_prog_release(struct inode *inode, struct file *filp)
1746{
1747 struct bpf_prog *prog = filp->private_data;
1748
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001749 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001750 return 0;
1751}
1752
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001753static void bpf_prog_get_stats(const struct bpf_prog *prog,
1754 struct bpf_prog_stats *stats)
1755{
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001756 u64 nsecs = 0, cnt = 0, misses = 0;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001757 int cpu;
1758
1759 for_each_possible_cpu(cpu) {
1760 const struct bpf_prog_stats *st;
1761 unsigned int start;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001762 u64 tnsecs, tcnt, tmisses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001763
Alexei Starovoitov700d4792021-02-09 19:36:26 -08001764 st = per_cpu_ptr(prog->stats, cpu);
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001765 do {
1766 start = u64_stats_fetch_begin_irq(&st->syncp);
1767 tnsecs = st->nsecs;
1768 tcnt = st->cnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001769 tmisses = st->misses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001770 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1771 nsecs += tnsecs;
1772 cnt += tcnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001773 misses += tmisses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001774 }
1775 stats->nsecs = nsecs;
1776 stats->cnt = cnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001777 stats->misses = misses;
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001778}
1779
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001780#ifdef CONFIG_PROC_FS
1781static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1782{
1783 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001784 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001785 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001786
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001787 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001788 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001789 seq_printf(m,
1790 "prog_type:\t%u\n"
1791 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001792 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001793 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001794 "prog_id:\t%u\n"
1795 "run_time_ns:\t%llu\n"
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001796 "run_cnt:\t%llu\n"
1797 "recursion_misses:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001798 prog->type,
1799 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001800 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001801 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001802 prog->aux->id,
1803 stats.nsecs,
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08001804 stats.cnt,
1805 stats.misses);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001806}
1807#endif
1808
Chenbo Fengf66e4482017-10-18 13:00:26 -07001809const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001810#ifdef CONFIG_PROC_FS
1811 .show_fdinfo = bpf_prog_show_fdinfo,
1812#endif
1813 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001814 .read = bpf_dummy_read,
1815 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001816};
1817
Daniel Borkmannb2197752015-10-29 14:58:09 +01001818int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001819{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001820 int ret;
1821
1822 ret = security_bpf_prog(prog);
1823 if (ret < 0)
1824 return ret;
1825
Daniel Borkmannaa797812015-10-29 14:58:06 +01001826 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1827 O_RDWR | O_CLOEXEC);
1828}
1829
Daniel Borkmann113214b2016-06-30 17:24:44 +02001830static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001831{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001832 if (!f.file)
1833 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001834 if (f.file->f_op != &bpf_prog_fops) {
1835 fdput(f);
1836 return ERR_PTR(-EINVAL);
1837 }
1838
Daniel Borkmannc2101292015-10-29 14:58:07 +01001839 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001840}
1841
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001842void bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001843{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001844 atomic64_add(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001845}
Brenden Blanco59d36562016-07-19 12:16:46 -07001846EXPORT_SYMBOL_GPL(bpf_prog_add);
1847
Daniel Borkmannc5405942016-11-09 22:02:34 +01001848void bpf_prog_sub(struct bpf_prog *prog, int i)
1849{
1850 /* Only to be used for undoing previous bpf_prog_add() in some
1851 * error path. We still know that another entity in our call
1852 * path holds a reference to the program, thus atomic_sub() can
1853 * be safely used in such cases!
1854 */
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001855 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
Daniel Borkmannc5405942016-11-09 22:02:34 +01001856}
1857EXPORT_SYMBOL_GPL(bpf_prog_sub);
1858
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001859void bpf_prog_inc(struct bpf_prog *prog)
Brenden Blanco59d36562016-07-19 12:16:46 -07001860{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001861 atomic64_inc(&prog->aux->refcnt);
Brenden Blanco59d36562016-07-19 12:16:46 -07001862}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001863EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001864
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001865/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001866struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001867{
1868 int refold;
1869
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001870 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001871
1872 if (!refold)
1873 return ERR_PTR(-ENOENT);
1874
1875 return prog;
1876}
John Fastabenda6f6df62017-08-15 22:32:22 -07001877EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001878
Al Viro040ee692017-12-02 20:20:38 -05001879bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001880 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001881{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001882 /* not an attachment, just a refcount inc, always allow */
1883 if (!attach_type)
1884 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001885
1886 if (prog->type != *attach_type)
1887 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001888 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001889 return false;
1890
1891 return true;
1892}
1893
1894static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001895 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001896{
1897 struct fd f = fdget(ufd);
1898 struct bpf_prog *prog;
1899
Daniel Borkmann113214b2016-06-30 17:24:44 +02001900 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001901 if (IS_ERR(prog))
1902 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001903 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001904 prog = ERR_PTR(-EINVAL);
1905 goto out;
1906 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001907
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001908 bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001909out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001910 fdput(f);
1911 return prog;
1912}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001913
1914struct bpf_prog *bpf_prog_get(u32 ufd)
1915{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001916 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001917}
1918
Jakub Kicinski248f3462017-11-03 13:56:20 -07001919struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001920 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001921{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001922 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001923}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001924EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001925
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001926/* Initially all BPF programs could be loaded w/o specifying
1927 * expected_attach_type. Later for some of them specifying expected_attach_type
1928 * at load time became required so that program could be validated properly.
1929 * Programs of types that are allowed to be loaded both w/ and w/o (for
1930 * backward compatibility) expected_attach_type, should have the default attach
1931 * type assigned to expected_attach_type for the latter case, so that it can be
1932 * validated later at attach time.
1933 *
1934 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1935 * prog type requires it but has some attach types that have to be backward
1936 * compatible.
1937 */
1938static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1939{
1940 switch (attr->prog_type) {
1941 case BPF_PROG_TYPE_CGROUP_SOCK:
1942 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1943 * exist so checking for non-zero is the way to go here.
1944 */
1945 if (!attr->expected_attach_type)
1946 attr->expected_attach_type =
1947 BPF_CGROUP_INET_SOCK_CREATE;
1948 break;
1949 }
1950}
1951
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001952static int
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07001953bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1954 enum bpf_attach_type expected_attach_type,
Andrii Nakryiko290248a2020-12-03 12:46:30 -08001955 struct btf *attach_btf, u32 btf_id,
1956 struct bpf_prog *dst_prog)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001957{
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001958 if (btf_id) {
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001959 if (btf_id > BTF_MAX_TYPE)
1960 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001961
Andrii Nakryiko290248a2020-12-03 12:46:30 -08001962 if (!attach_btf && !dst_prog)
1963 return -EINVAL;
1964
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001965 switch (prog_type) {
1966 case BPF_PROG_TYPE_TRACING:
KP Singh9e4e01d2020-03-29 01:43:52 +01001967 case BPF_PROG_TYPE_LSM:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001968 case BPF_PROG_TYPE_STRUCT_OPS:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001969 case BPF_PROG_TYPE_EXT:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001970 break;
1971 default:
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001972 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001973 }
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001974 }
1975
Andrii Nakryiko290248a2020-12-03 12:46:30 -08001976 if (attach_btf && (!btf_id || dst_prog))
1977 return -EINVAL;
1978
1979 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001980 prog_type != BPF_PROG_TYPE_EXT)
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001981 return -EINVAL;
1982
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001983 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001984 case BPF_PROG_TYPE_CGROUP_SOCK:
1985 switch (expected_attach_type) {
1986 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07001987 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001988 case BPF_CGROUP_INET4_POST_BIND:
1989 case BPF_CGROUP_INET6_POST_BIND:
1990 return 0;
1991 default:
1992 return -EINVAL;
1993 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001994 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1995 switch (expected_attach_type) {
1996 case BPF_CGROUP_INET4_BIND:
1997 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001998 case BPF_CGROUP_INET4_CONNECT:
1999 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02002000 case BPF_CGROUP_INET4_GETPEERNAME:
2001 case BPF_CGROUP_INET6_GETPEERNAME:
2002 case BPF_CGROUP_INET4_GETSOCKNAME:
2003 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002004 case BPF_CGROUP_UDP4_SENDMSG:
2005 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002006 case BPF_CGROUP_UDP4_RECVMSG:
2007 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002008 return 0;
2009 default:
2010 return -EINVAL;
2011 }
brakmo5cf1e912019-05-28 16:59:36 -07002012 case BPF_PROG_TYPE_CGROUP_SKB:
2013 switch (expected_attach_type) {
2014 case BPF_CGROUP_INET_INGRESS:
2015 case BPF_CGROUP_INET_EGRESS:
2016 return 0;
2017 default:
2018 return -EINVAL;
2019 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002020 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2021 switch (expected_attach_type) {
2022 case BPF_CGROUP_SETSOCKOPT:
2023 case BPF_CGROUP_GETSOCKOPT:
2024 return 0;
2025 default:
2026 return -EINVAL;
2027 }
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02002028 case BPF_PROG_TYPE_SK_LOOKUP:
2029 if (expected_attach_type == BPF_SK_LOOKUP)
2030 return 0;
2031 return -EINVAL;
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07002032 case BPF_PROG_TYPE_SYSCALL:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002033 case BPF_PROG_TYPE_EXT:
2034 if (expected_attach_type)
2035 return -EINVAL;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002036 fallthrough;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002037 default:
2038 return 0;
2039 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002040}
2041
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002042static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2043{
2044 switch (prog_type) {
2045 case BPF_PROG_TYPE_SCHED_CLS:
2046 case BPF_PROG_TYPE_SCHED_ACT:
2047 case BPF_PROG_TYPE_XDP:
2048 case BPF_PROG_TYPE_LWT_IN:
2049 case BPF_PROG_TYPE_LWT_OUT:
2050 case BPF_PROG_TYPE_LWT_XMIT:
2051 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2052 case BPF_PROG_TYPE_SK_SKB:
2053 case BPF_PROG_TYPE_SK_MSG:
2054 case BPF_PROG_TYPE_LIRC_MODE2:
2055 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2056 case BPF_PROG_TYPE_CGROUP_DEVICE:
2057 case BPF_PROG_TYPE_CGROUP_SOCK:
2058 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2059 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2060 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2061 case BPF_PROG_TYPE_SOCK_OPS:
2062 case BPF_PROG_TYPE_EXT: /* extends any prog */
2063 return true;
2064 case BPF_PROG_TYPE_CGROUP_SKB:
2065 /* always unpriv */
2066 case BPF_PROG_TYPE_SK_REUSEPORT:
2067 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2068 default:
2069 return false;
2070 }
2071}
2072
2073static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2074{
2075 switch (prog_type) {
2076 case BPF_PROG_TYPE_KPROBE:
2077 case BPF_PROG_TYPE_TRACEPOINT:
2078 case BPF_PROG_TYPE_PERF_EVENT:
2079 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2080 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2081 case BPF_PROG_TYPE_TRACING:
2082 case BPF_PROG_TYPE_LSM:
2083 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2084 case BPF_PROG_TYPE_EXT: /* extends any prog */
2085 return true;
2086 default:
2087 return false;
2088 }
2089}
2090
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002091/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov387544b2021-05-13 17:36:10 -07002092#define BPF_PROG_LOAD_LAST_FIELD fd_array
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002093
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07002094static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002095{
2096 enum bpf_prog_type type = attr->prog_type;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002097 struct bpf_prog *prog, *dst_prog = NULL;
2098 struct btf *attach_btf = NULL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002099 int err;
2100 char license[128];
2101 bool is_gpl;
2102
2103 if (CHECK_ATTR(BPF_PROG_LOAD))
2104 return -EINVAL;
2105
Jiong Wangc240eff2019-05-24 23:25:16 +01002106 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2107 BPF_F_ANY_ALIGNMENT |
Alexei Starovoitov10d274e2019-08-22 22:52:12 -07002108 BPF_F_TEST_STATE_FREQ |
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002109 BPF_F_SLEEPABLE |
Jiong Wangc240eff2019-05-24 23:25:16 +01002110 BPF_F_TEST_RND_HI32))
David S. Millere07b98d2017-05-10 11:38:07 -07002111 return -EINVAL;
2112
David Millere9ee9ef2018-11-30 21:08:14 -08002113 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2114 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002115 !bpf_capable())
David Millere9ee9ef2018-11-30 21:08:14 -08002116 return -EPERM;
2117
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002118 /* copy eBPF program license from user space */
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07002119 if (strncpy_from_bpfptr(license,
2120 make_bpfptr(attr->license, uattr.is_kernel),
2121 sizeof(license) - 1) < 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002122 return -EFAULT;
2123 license[sizeof(license) - 1] = 0;
2124
2125 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2126 is_gpl = license_is_gpl_compatible(license);
2127
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07002128 if (attr->insn_cnt == 0 ||
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002129 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01002130 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07002131 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2132 type != BPF_PROG_TYPE_CGROUP_SKB &&
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002133 !bpf_capable())
2134 return -EPERM;
2135
Maciej Żenczykowskib338cb92020-06-20 14:26:16 -07002136 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002137 return -EPERM;
2138 if (is_perfmon_prog_type(type) && !perfmon_capable())
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002139 return -EPERM;
2140
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002141 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2142 * or btf, we need to check which one it is
2143 */
2144 if (attr->attach_prog_fd) {
2145 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2146 if (IS_ERR(dst_prog)) {
2147 dst_prog = NULL;
2148 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2149 if (IS_ERR(attach_btf))
2150 return -EINVAL;
2151 if (!btf_is_kernel(attach_btf)) {
Andrii Nakryiko8bdd8e22020-12-07 22:43:26 -08002152 /* attaching through specifying bpf_prog's BTF
2153 * objects directly might be supported eventually
2154 */
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002155 btf_put(attach_btf);
Andrii Nakryiko8bdd8e22020-12-07 22:43:26 -08002156 return -ENOTSUPP;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002157 }
2158 }
2159 } else if (attr->attach_btf_id) {
2160 /* fall back to vmlinux BTF, if BTF type ID is specified */
2161 attach_btf = bpf_get_btf_vmlinux();
2162 if (IS_ERR(attach_btf))
2163 return PTR_ERR(attach_btf);
2164 if (!attach_btf)
2165 return -EINVAL;
2166 btf_get(attach_btf);
2167 }
2168
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002169 bpf_prog_load_fixup_attach_type(attr);
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002170 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002171 attach_btf, attr->attach_btf_id,
2172 dst_prog)) {
2173 if (dst_prog)
2174 bpf_prog_put(dst_prog);
2175 if (attach_btf)
2176 btf_put(attach_btf);
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002177 return -EINVAL;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002178 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002179
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002180 /* plain bpf_prog allocation */
2181 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002182 if (!prog) {
2183 if (dst_prog)
2184 bpf_prog_put(dst_prog);
2185 if (attach_btf)
2186 btf_put(attach_btf);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002187 return -ENOMEM;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002188 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002189
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002190 prog->expected_attach_type = attr->expected_attach_type;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002191 prog->aux->attach_btf = attach_btf;
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002192 prog->aux->attach_btf_id = attr->attach_btf_id;
Andrii Nakryiko290248a2020-12-03 12:46:30 -08002193 prog->aux->dst_prog = dst_prog;
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002194 prog->aux->offload_requested = !!attr->prog_ifindex;
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -07002195 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002196
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002197 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002198 if (err)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002199 goto free_prog;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002200
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002201 prog->aux->user = get_current_user();
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002202 prog->len = attr->insn_cnt;
2203
2204 err = -EFAULT;
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07002205 if (copy_from_bpfptr(prog->insns,
2206 make_bpfptr(attr->insns, uattr.is_kernel),
2207 bpf_prog_insn_size(prog)) != 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002208 goto free_prog_sec;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002209
2210 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002211 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002212
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002213 atomic64_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002214 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002215
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002216 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002217 err = bpf_prog_offload_init(prog, attr);
2218 if (err)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002219 goto free_prog_sec;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002220 }
2221
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002222 /* find program type: socket_filter vs tracing_filter */
2223 err = find_prog_type(type, prog);
2224 if (err < 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002225 goto free_prog_sec;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002226
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002227 prog->aux->load_time = ktime_get_boottime_ns();
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -07002228 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2229 sizeof(attr->prog_name));
2230 if (err < 0)
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002231 goto free_prog_sec;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002232
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002233 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08002234 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002235 if (err < 0)
2236 goto free_used_maps;
2237
Daniel Borkmann9facc332018-06-15 02:30:48 +02002238 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002239 if (err < 0)
2240 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002241
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07002242 err = bpf_prog_alloc_id(prog);
2243 if (err)
2244 goto free_used_maps;
2245
Daniel Borkmannc7517982019-08-23 22:14:23 +02002246 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2247 * effectively publicly exposed. However, retrieving via
2248 * bpf_prog_get_fd_by_id() will take another reference,
2249 * therefore it cannot be gone underneath us.
2250 *
2251 * Only for the time /after/ successful bpf_prog_new_fd()
2252 * and before returning to userspace, we might just hold
2253 * one reference and any parallel close on that fd could
2254 * rip everything out. Hence, below notifications must
2255 * happen before bpf_prog_new_fd().
2256 *
2257 * Also, any failure handling from this point onwards must
2258 * be using bpf_prog_put() given the program is exposed.
2259 */
Daniel Borkmann74451e662017-02-16 22:24:50 +01002260 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08002261 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01002262 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
Daniel Borkmannc7517982019-08-23 22:14:23 +02002263
2264 err = bpf_prog_new_fd(prog);
2265 if (err < 0)
2266 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002267 return err;
2268
2269free_used_maps:
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002270 /* In case we have subprogs, we need to wait for a grace
2271 * period before we can tear down JIT memory since symbols
2272 * are already exposed under kallsyms.
2273 */
2274 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2275 return err;
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002276free_prog_sec:
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002277 free_uid(prog->aux->user);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002278 security_bpf_prog_free(prog->aux);
Roman Gushchin3ac1f012020-12-01 13:58:59 -08002279free_prog:
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08002280 if (prog->aux->attach_btf)
2281 btf_put(prog->aux->attach_btf);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002282 bpf_prog_free(prog);
2283 return err;
2284}
2285
Chenbo Feng6e71b042017-10-18 13:00:22 -07002286#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01002287
2288static int bpf_obj_pin(const union bpf_attr *attr)
2289{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002290 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002291 return -EINVAL;
2292
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002293 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01002294}
2295
2296static int bpf_obj_get(const union bpf_attr *attr)
2297{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002298 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2299 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002300 return -EINVAL;
2301
Chenbo Feng6e71b042017-10-18 13:00:22 -07002302 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2303 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01002304}
2305
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002306void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002307 const struct bpf_link_ops *ops, struct bpf_prog *prog)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002308{
2309 atomic64_set(&link->refcnt, 1);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002310 link->type = type;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002311 link->id = 0;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002312 link->ops = ops;
2313 link->prog = prog;
2314}
2315
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002316static void bpf_link_free_id(int id)
2317{
2318 if (!id)
2319 return;
2320
2321 spin_lock_bh(&link_idr_lock);
2322 idr_remove(&link_idr, id);
2323 spin_unlock_bh(&link_idr_lock);
2324}
2325
Andrii Nakryiko98868662020-03-12 17:21:28 -07002326/* Clean up bpf_link and corresponding anon_inode file and FD. After
2327 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002328 * anon_inode's release() call. This helper marksbpf_link as
2329 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2330 * is not decremented, it's the responsibility of a calling code that failed
2331 * to complete bpf_link initialization.
Andrii Nakryiko98868662020-03-12 17:21:28 -07002332 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002333void bpf_link_cleanup(struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002334{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002335 primer->link->prog = NULL;
2336 bpf_link_free_id(primer->id);
2337 fput(primer->file);
2338 put_unused_fd(primer->fd);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002339}
2340
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002341void bpf_link_inc(struct bpf_link *link)
2342{
2343 atomic64_inc(&link->refcnt);
2344}
2345
2346/* bpf_link_free is guaranteed to be called from process context */
2347static void bpf_link_free(struct bpf_link *link)
2348{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002349 bpf_link_free_id(link->id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002350 if (link->prog) {
2351 /* detach BPF program, clean up used resources */
2352 link->ops->release(link);
2353 bpf_prog_put(link->prog);
2354 }
2355 /* free bpf_link and its containing memory */
2356 link->ops->dealloc(link);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002357}
2358
2359static void bpf_link_put_deferred(struct work_struct *work)
2360{
2361 struct bpf_link *link = container_of(work, struct bpf_link, work);
2362
2363 bpf_link_free(link);
2364}
2365
2366/* bpf_link_put can be called from atomic context, but ensures that resources
2367 * are freed from process context
2368 */
2369void bpf_link_put(struct bpf_link *link)
2370{
2371 if (!atomic64_dec_and_test(&link->refcnt))
2372 return;
2373
Alexei Starovoitovf00f2f72020-09-23 19:10:38 -07002374 if (in_atomic()) {
2375 INIT_WORK(&link->work, bpf_link_put_deferred);
2376 schedule_work(&link->work);
2377 } else {
2378 bpf_link_free(link);
2379 }
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002380}
2381
2382static int bpf_link_release(struct inode *inode, struct file *filp)
2383{
2384 struct bpf_link *link = filp->private_data;
2385
2386 bpf_link_put(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002387 return 0;
2388}
2389
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002390#ifdef CONFIG_PROC_FS
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002391#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2392#define BPF_MAP_TYPE(_id, _ops)
2393#define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2394static const char *bpf_link_type_strs[] = {
2395 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2396#include <linux/bpf_types.h>
2397};
2398#undef BPF_PROG_TYPE
2399#undef BPF_MAP_TYPE
2400#undef BPF_LINK_TYPE
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002401
2402static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2403{
2404 const struct bpf_link *link = filp->private_data;
2405 const struct bpf_prog *prog = link->prog;
2406 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002407
2408 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2409 seq_printf(m,
2410 "link_type:\t%s\n"
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002411 "link_id:\t%u\n"
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002412 "prog_tag:\t%s\n"
2413 "prog_id:\t%u\n",
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002414 bpf_link_type_strs[link->type],
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002415 link->id,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002416 prog_tag,
2417 prog->aux->id);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002418 if (link->ops->show_fdinfo)
2419 link->ops->show_fdinfo(link, m);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002420}
2421#endif
2422
Zou Wei6f302bf2020-04-23 10:32:40 +08002423static const struct file_operations bpf_link_fops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002424#ifdef CONFIG_PROC_FS
2425 .show_fdinfo = bpf_link_show_fdinfo,
2426#endif
2427 .release = bpf_link_release,
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002428 .read = bpf_dummy_read,
2429 .write = bpf_dummy_write,
2430};
2431
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002432static int bpf_link_alloc_id(struct bpf_link *link)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002433{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002434 int id;
2435
2436 idr_preload(GFP_KERNEL);
2437 spin_lock_bh(&link_idr_lock);
2438 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2439 spin_unlock_bh(&link_idr_lock);
2440 idr_preload_end();
2441
2442 return id;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002443}
2444
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002445/* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2446 * reserving unused FD and allocating ID from link_idr. This is to be paired
2447 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2448 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2449 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2450 * transient state is passed around in struct bpf_link_primer.
2451 * This is preferred way to create and initialize bpf_link, especially when
2452 * there are complicated and expensive operations inbetween creating bpf_link
2453 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2454 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2455 * expensive (and potentially failing) roll back operations in a rare case
2456 * that file, FD, or ID can't be allocated.
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002457 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002458int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002459{
2460 struct file *file;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002461 int fd, id;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002462
2463 fd = get_unused_fd_flags(O_CLOEXEC);
2464 if (fd < 0)
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002465 return fd;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002466
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002467
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002468 id = bpf_link_alloc_id(link);
2469 if (id < 0) {
2470 put_unused_fd(fd);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002471 return id;
2472 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002473
2474 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2475 if (IS_ERR(file)) {
Andrii Nakryiko138c6762020-05-01 11:56:22 -07002476 bpf_link_free_id(id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002477 put_unused_fd(fd);
Andrii Nakryiko138c6762020-05-01 11:56:22 -07002478 return PTR_ERR(file);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002479 }
2480
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002481 primer->link = link;
2482 primer->file = file;
2483 primer->fd = fd;
2484 primer->id = id;
2485 return 0;
2486}
2487
2488int bpf_link_settle(struct bpf_link_primer *primer)
2489{
2490 /* make bpf_link fetchable by ID */
2491 spin_lock_bh(&link_idr_lock);
2492 primer->link->id = primer->id;
2493 spin_unlock_bh(&link_idr_lock);
2494 /* make bpf_link fetchable by FD */
2495 fd_install(primer->fd, primer->file);
2496 /* pass through installed FD */
2497 return primer->fd;
2498}
2499
2500int bpf_link_new_fd(struct bpf_link *link)
2501{
2502 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002503}
2504
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002505struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2506{
2507 struct fd f = fdget(ufd);
2508 struct bpf_link *link;
2509
2510 if (!f.file)
2511 return ERR_PTR(-EBADF);
2512 if (f.file->f_op != &bpf_link_fops) {
2513 fdput(f);
2514 return ERR_PTR(-EINVAL);
2515 }
2516
2517 link = f.file->private_data;
2518 bpf_link_inc(link);
2519 fdput(f);
2520
2521 return link;
2522}
2523
2524struct bpf_tracing_link {
2525 struct bpf_link link;
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002526 enum bpf_attach_type attach_type;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002527 struct bpf_trampoline *trampoline;
2528 struct bpf_prog *tgt_prog;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002529};
2530
2531static void bpf_tracing_link_release(struct bpf_link *link)
2532{
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002533 struct bpf_tracing_link *tr_link =
2534 container_of(link, struct bpf_tracing_link, link);
2535
2536 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog,
2537 tr_link->trampoline));
2538
2539 bpf_trampoline_put(tr_link->trampoline);
2540
2541 /* tgt_prog is NULL if target is a kernel function */
2542 if (tr_link->tgt_prog)
2543 bpf_prog_put(tr_link->tgt_prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002544}
2545
2546static void bpf_tracing_link_dealloc(struct bpf_link *link)
2547{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002548 struct bpf_tracing_link *tr_link =
2549 container_of(link, struct bpf_tracing_link, link);
2550
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002551 kfree(tr_link);
2552}
2553
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002554static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2555 struct seq_file *seq)
2556{
2557 struct bpf_tracing_link *tr_link =
2558 container_of(link, struct bpf_tracing_link, link);
2559
2560 seq_printf(seq,
2561 "attach_type:\t%d\n",
2562 tr_link->attach_type);
2563}
2564
2565static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2566 struct bpf_link_info *info)
2567{
2568 struct bpf_tracing_link *tr_link =
2569 container_of(link, struct bpf_tracing_link, link);
2570
2571 info->tracing.attach_type = tr_link->attach_type;
Toke Høiland-Jørgensen441e8c62021-04-13 11:16:06 +02002572 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2573 &info->tracing.target_obj_id,
2574 &info->tracing.target_btf_id);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002575
2576 return 0;
2577}
2578
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002579static const struct bpf_link_ops bpf_tracing_link_lops = {
2580 .release = bpf_tracing_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002581 .dealloc = bpf_tracing_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002582 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2583 .fill_link_info = bpf_tracing_link_fill_link_info,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002584};
2585
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002586static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2587 int tgt_prog_fd,
2588 u32 btf_id)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002589{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002590 struct bpf_link_primer link_primer;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002591 struct bpf_prog *tgt_prog = NULL;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002592 struct bpf_trampoline *tr = NULL;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002593 struct bpf_tracing_link *link;
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002594 u64 key = 0;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002595 int err;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002596
KP Singh9e4e01d2020-03-29 01:43:52 +01002597 switch (prog->type) {
2598 case BPF_PROG_TYPE_TRACING:
2599 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2600 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2601 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2602 err = -EINVAL;
2603 goto out_put_prog;
2604 }
2605 break;
2606 case BPF_PROG_TYPE_EXT:
2607 if (prog->expected_attach_type != 0) {
2608 err = -EINVAL;
2609 goto out_put_prog;
2610 }
2611 break;
2612 case BPF_PROG_TYPE_LSM:
2613 if (prog->expected_attach_type != BPF_LSM_MAC) {
2614 err = -EINVAL;
2615 goto out_put_prog;
2616 }
2617 break;
2618 default:
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002619 err = -EINVAL;
2620 goto out_put_prog;
2621 }
2622
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002623 if (!!tgt_prog_fd != !!btf_id) {
2624 err = -EINVAL;
2625 goto out_put_prog;
2626 }
2627
2628 if (tgt_prog_fd) {
2629 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2630 if (prog->type != BPF_PROG_TYPE_EXT) {
2631 err = -EINVAL;
2632 goto out_put_prog;
2633 }
2634
2635 tgt_prog = bpf_prog_get(tgt_prog_fd);
2636 if (IS_ERR(tgt_prog)) {
2637 err = PTR_ERR(tgt_prog);
2638 tgt_prog = NULL;
2639 goto out_put_prog;
2640 }
2641
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08002642 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002643 }
2644
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002645 link = kzalloc(sizeof(*link), GFP_USER);
2646 if (!link) {
2647 err = -ENOMEM;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002648 goto out_put_prog;
2649 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002650 bpf_link_init(&link->link, BPF_LINK_TYPE_TRACING,
2651 &bpf_tracing_link_lops, prog);
2652 link->attach_type = prog->expected_attach_type;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002653
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002654 mutex_lock(&prog->aux->dst_mutex);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002655
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002656 /* There are a few possible cases here:
2657 *
2658 * - if prog->aux->dst_trampoline is set, the program was just loaded
2659 * and not yet attached to anything, so we can use the values stored
2660 * in prog->aux
2661 *
2662 * - if prog->aux->dst_trampoline is NULL, the program has already been
2663 * attached to a target and its initial target was cleared (below)
2664 *
2665 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
2666 * target_btf_id using the link_create API.
2667 *
2668 * - if tgt_prog == NULL when this function was called using the old
Jiri Olsaf3a95072021-04-14 21:51:41 +02002669 * raw_tracepoint_open API, and we need a target from prog->aux
2670 *
2671 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
2672 * was detached and is going for re-attachment.
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002673 */
2674 if (!prog->aux->dst_trampoline && !tgt_prog) {
Jiri Olsaf3a95072021-04-14 21:51:41 +02002675 /*
2676 * Allow re-attach for TRACING and LSM programs. If it's
2677 * currently linked, bpf_trampoline_link_prog will fail.
2678 * EXT programs need to specify tgt_prog_fd, so they
2679 * re-attach in separate code path.
2680 */
2681 if (prog->type != BPF_PROG_TYPE_TRACING &&
2682 prog->type != BPF_PROG_TYPE_LSM) {
2683 err = -EINVAL;
2684 goto out_unlock;
2685 }
2686 btf_id = prog->aux->attach_btf_id;
2687 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002688 }
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002689
2690 if (!prog->aux->dst_trampoline ||
2691 (key && key != prog->aux->dst_trampoline->key)) {
2692 /* If there is no saved target, or the specified target is
2693 * different from the destination specified at load time, we
2694 * need a new trampoline and a check for compatibility
2695 */
2696 struct bpf_attach_target_info tgt_info = {};
2697
2698 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
2699 &tgt_info);
2700 if (err)
2701 goto out_unlock;
2702
2703 tr = bpf_trampoline_get(key, &tgt_info);
2704 if (!tr) {
2705 err = -ENOMEM;
2706 goto out_unlock;
2707 }
2708 } else {
2709 /* The caller didn't specify a target, or the target was the
2710 * same as the destination supplied during program load. This
2711 * means we can reuse the trampoline and reference from program
2712 * load time, and there is no need to allocate a new one. This
2713 * can only happen once for any program, as the saved values in
2714 * prog->aux are cleared below.
2715 */
2716 tr = prog->aux->dst_trampoline;
2717 tgt_prog = prog->aux->dst_prog;
2718 }
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002719
2720 err = bpf_link_prime(&link->link, &link_primer);
2721 if (err)
2722 goto out_unlock;
2723
2724 err = bpf_trampoline_link_prog(prog, tr);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002725 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002726 bpf_link_cleanup(&link_primer);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002727 link = NULL;
2728 goto out_unlock;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002729 }
2730
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002731 link->tgt_prog = tgt_prog;
2732 link->trampoline = tr;
2733
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002734 /* Always clear the trampoline and target prog from prog->aux to make
2735 * sure the original attach destination is not kept alive after a
2736 * program is (re-)attached to another target.
2737 */
2738 if (prog->aux->dst_prog &&
2739 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
2740 /* got extra prog ref from syscall, or attaching to different prog */
2741 bpf_prog_put(prog->aux->dst_prog);
2742 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
2743 /* we allocated a new trampoline, so free the old one */
2744 bpf_trampoline_put(prog->aux->dst_trampoline);
2745
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002746 prog->aux->dst_prog = NULL;
2747 prog->aux->dst_trampoline = NULL;
2748 mutex_unlock(&prog->aux->dst_mutex);
2749
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002750 return bpf_link_settle(&link_primer);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002751out_unlock:
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002752 if (tr && tr != prog->aux->dst_trampoline)
2753 bpf_trampoline_put(tr);
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +02002754 mutex_unlock(&prog->aux->dst_mutex);
2755 kfree(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002756out_put_prog:
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02002757 if (tgt_prog_fd && tgt_prog)
2758 bpf_prog_put(tgt_prog);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002759 return err;
2760}
2761
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002762struct bpf_raw_tp_link {
2763 struct bpf_link link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002764 struct bpf_raw_event_map *btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002765};
2766
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002767static void bpf_raw_tp_link_release(struct bpf_link *link)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002768{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002769 struct bpf_raw_tp_link *raw_tp =
2770 container_of(link, struct bpf_raw_tp_link, link);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002771
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002772 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
Matt Mullinsa38d1102018-12-12 16:42:37 -08002773 bpf_put_raw_tracepoint(raw_tp->btp);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002774}
2775
2776static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2777{
2778 struct bpf_raw_tp_link *raw_tp =
2779 container_of(link, struct bpf_raw_tp_link, link);
2780
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002781 kfree(raw_tp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002782}
2783
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002784static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
2785 struct seq_file *seq)
2786{
2787 struct bpf_raw_tp_link *raw_tp_link =
2788 container_of(link, struct bpf_raw_tp_link, link);
2789
2790 seq_printf(seq,
2791 "tp_name:\t%s\n",
2792 raw_tp_link->btp->tp->name);
2793}
2794
2795static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
2796 struct bpf_link_info *info)
2797{
2798 struct bpf_raw_tp_link *raw_tp_link =
2799 container_of(link, struct bpf_raw_tp_link, link);
2800 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
2801 const char *tp_name = raw_tp_link->btp->tp->name;
2802 u32 ulen = info->raw_tracepoint.tp_name_len;
2803 size_t tp_len = strlen(tp_name);
2804
Yonghong Songb4749592020-08-21 12:10:54 -07002805 if (!ulen ^ !ubuf)
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002806 return -EINVAL;
2807
2808 info->raw_tracepoint.tp_name_len = tp_len + 1;
2809
2810 if (!ubuf)
2811 return 0;
2812
2813 if (ulen >= tp_len + 1) {
2814 if (copy_to_user(ubuf, tp_name, tp_len + 1))
2815 return -EFAULT;
2816 } else {
2817 char zero = '\0';
2818
2819 if (copy_to_user(ubuf, tp_name, ulen - 1))
2820 return -EFAULT;
2821 if (put_user(zero, ubuf + ulen - 1))
2822 return -EFAULT;
2823 return -ENOSPC;
2824 }
2825
2826 return 0;
2827}
2828
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002829static const struct bpf_link_ops bpf_raw_tp_link_lops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002830 .release = bpf_raw_tp_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002831 .dealloc = bpf_raw_tp_link_dealloc,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002832 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
2833 .fill_link_info = bpf_raw_tp_link_fill_link_info,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002834};
2835
2836#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2837
2838static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2839{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002840 struct bpf_link_primer link_primer;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002841 struct bpf_raw_tp_link *link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002842 struct bpf_raw_event_map *btp;
2843 struct bpf_prog *prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002844 const char *tp_name;
2845 char buf[128];
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002846 int err;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002847
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002848 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2849 return -EINVAL;
2850
2851 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2852 if (IS_ERR(prog))
2853 return PTR_ERR(prog);
2854
KP Singh9e4e01d2020-03-29 01:43:52 +01002855 switch (prog->type) {
2856 case BPF_PROG_TYPE_TRACING:
2857 case BPF_PROG_TYPE_EXT:
2858 case BPF_PROG_TYPE_LSM:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002859 if (attr->raw_tracepoint.name) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002860 /* The attach point for this category of programs
2861 * should be specified via btf_id during program load.
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002862 */
2863 err = -EINVAL;
2864 goto out_put_prog;
2865 }
KP Singh9e4e01d2020-03-29 01:43:52 +01002866 if (prog->type == BPF_PROG_TYPE_TRACING &&
2867 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002868 tp_name = prog->aux->attach_func_name;
KP Singh9e4e01d2020-03-29 01:43:52 +01002869 break;
2870 }
Jiri Olsa55410752021-01-11 20:16:50 +01002871 err = bpf_tracing_prog_attach(prog, 0, 0);
2872 if (err >= 0)
2873 return err;
2874 goto out_put_prog;
KP Singh9e4e01d2020-03-29 01:43:52 +01002875 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2876 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002877 if (strncpy_from_user(buf,
2878 u64_to_user_ptr(attr->raw_tracepoint.name),
2879 sizeof(buf) - 1) < 0) {
2880 err = -EFAULT;
2881 goto out_put_prog;
2882 }
2883 buf[sizeof(buf) - 1] = 0;
2884 tp_name = buf;
KP Singh9e4e01d2020-03-29 01:43:52 +01002885 break;
2886 default:
2887 err = -EINVAL;
2888 goto out_put_prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002889 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002890
Matt Mullinsa38d1102018-12-12 16:42:37 -08002891 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002892 if (!btp) {
2893 err = -ENOENT;
2894 goto out_put_prog;
2895 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002896
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002897 link = kzalloc(sizeof(*link), GFP_USER);
2898 if (!link) {
Matt Mullinsa38d1102018-12-12 16:42:37 -08002899 err = -ENOMEM;
2900 goto out_put_btp;
2901 }
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07002902 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
2903 &bpf_raw_tp_link_lops, prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002904 link->btp = btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002905
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002906 err = bpf_link_prime(&link->link, &link_primer);
2907 if (err) {
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002908 kfree(link);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002909 goto out_put_btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002910 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002911
2912 err = bpf_probe_register(link->btp, prog);
2913 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002914 bpf_link_cleanup(&link_primer);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002915 goto out_put_btp;
2916 }
2917
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002918 return bpf_link_settle(&link_primer);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002919
Matt Mullinsa38d1102018-12-12 16:42:37 -08002920out_put_btp:
2921 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002922out_put_prog:
2923 bpf_prog_put(prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002924 return err;
2925}
2926
Anders Roxell33491582018-04-03 14:09:47 +02002927static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2928 enum bpf_attach_type attach_type)
2929{
2930 switch (prog->type) {
2931 case BPF_PROG_TYPE_CGROUP_SOCK:
2932 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002933 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02002934 case BPF_PROG_TYPE_SK_LOOKUP:
Anders Roxell33491582018-04-03 14:09:47 +02002935 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
brakmo5cf1e912019-05-28 16:59:36 -07002936 case BPF_PROG_TYPE_CGROUP_SKB:
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07002937 if (!capable(CAP_NET_ADMIN))
2938 /* cg-skb progs can be loaded by unpriv user.
2939 * check permissions at attach time.
2940 */
2941 return -EPERM;
brakmo5cf1e912019-05-28 16:59:36 -07002942 return prog->enforce_expected_attach_type &&
2943 prog->expected_attach_type != attach_type ?
2944 -EINVAL : 0;
Anders Roxell33491582018-04-03 14:09:47 +02002945 default:
2946 return 0;
2947 }
2948}
2949
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002950static enum bpf_prog_type
2951attach_type_to_prog_type(enum bpf_attach_type attach_type)
2952{
2953 switch (attach_type) {
2954 case BPF_CGROUP_INET_INGRESS:
2955 case BPF_CGROUP_INET_EGRESS:
2956 return BPF_PROG_TYPE_CGROUP_SKB;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002957 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07002958 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002959 case BPF_CGROUP_INET4_POST_BIND:
2960 case BPF_CGROUP_INET6_POST_BIND:
2961 return BPF_PROG_TYPE_CGROUP_SOCK;
2962 case BPF_CGROUP_INET4_BIND:
2963 case BPF_CGROUP_INET6_BIND:
2964 case BPF_CGROUP_INET4_CONNECT:
2965 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02002966 case BPF_CGROUP_INET4_GETPEERNAME:
2967 case BPF_CGROUP_INET6_GETPEERNAME:
2968 case BPF_CGROUP_INET4_GETSOCKNAME:
2969 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002970 case BPF_CGROUP_UDP4_SENDMSG:
2971 case BPF_CGROUP_UDP6_SENDMSG:
2972 case BPF_CGROUP_UDP4_RECVMSG:
2973 case BPF_CGROUP_UDP6_RECVMSG:
2974 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2975 case BPF_CGROUP_SOCK_OPS:
2976 return BPF_PROG_TYPE_SOCK_OPS;
2977 case BPF_CGROUP_DEVICE:
2978 return BPF_PROG_TYPE_CGROUP_DEVICE;
2979 case BPF_SK_MSG_VERDICT:
2980 return BPF_PROG_TYPE_SK_MSG;
2981 case BPF_SK_SKB_STREAM_PARSER:
2982 case BPF_SK_SKB_STREAM_VERDICT:
Cong Wanga7ba4552021-03-30 19:32:30 -07002983 case BPF_SK_SKB_VERDICT:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002984 return BPF_PROG_TYPE_SK_SKB;
2985 case BPF_LIRC_MODE2:
2986 return BPF_PROG_TYPE_LIRC_MODE2;
2987 case BPF_FLOW_DISSECTOR:
2988 return BPF_PROG_TYPE_FLOW_DISSECTOR;
2989 case BPF_CGROUP_SYSCTL:
2990 return BPF_PROG_TYPE_CGROUP_SYSCTL;
2991 case BPF_CGROUP_GETSOCKOPT:
2992 case BPF_CGROUP_SETSOCKOPT:
2993 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
Yonghong Songde4e05c2020-05-09 10:59:01 -07002994 case BPF_TRACE_ITER:
2995 return BPF_PROG_TYPE_TRACING;
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02002996 case BPF_SK_LOOKUP:
2997 return BPF_PROG_TYPE_SK_LOOKUP;
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07002998 case BPF_XDP:
2999 return BPF_PROG_TYPE_XDP;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003000 default:
3001 return BPF_PROG_TYPE_UNSPEC;
3002 }
3003}
3004
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08003005#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
John Fastabend174a79f2017-08-15 22:32:47 -07003006
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003007#define BPF_F_ATTACH_MASK \
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08003008 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003009
Daniel Mackf4324552016-11-23 16:52:27 +01003010static int bpf_prog_attach(const union bpf_attr *attr)
3011{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003012 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01003013 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003014 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01003015
Daniel Mackf4324552016-11-23 16:52:27 +01003016 if (CHECK_ATTR(BPF_PROG_ATTACH))
3017 return -EINVAL;
3018
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003019 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003020 return -EINVAL;
3021
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003022 ptype = attach_type_to_prog_type(attr->attach_type);
3023 if (ptype == BPF_PROG_TYPE_UNSPEC)
Daniel Mackf4324552016-11-23 16:52:27 +01003024 return -EINVAL;
Daniel Mackf4324552016-11-23 16:52:27 +01003025
David Ahernb2cd1252016-12-01 08:48:03 -08003026 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3027 if (IS_ERR(prog))
3028 return PTR_ERR(prog);
3029
Andrey Ignatov5e43f892018-03-30 15:08:00 -07003030 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3031 bpf_prog_put(prog);
3032 return -EINVAL;
3033 }
3034
Sean Youngfdb5c452018-06-19 00:04:24 +01003035 switch (ptype) {
3036 case BPF_PROG_TYPE_SK_SKB:
3037 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02003038 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01003039 break;
3040 case BPF_PROG_TYPE_LIRC_MODE2:
3041 ret = lirc_prog_attach(attr, prog);
3042 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07003043 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +02003044 ret = netns_bpf_prog_attach(attr, prog);
Petar Penkovd58e4682018-09-14 07:46:18 -07003045 break;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003046 case BPF_PROG_TYPE_CGROUP_DEVICE:
3047 case BPF_PROG_TYPE_CGROUP_SKB:
3048 case BPF_PROG_TYPE_CGROUP_SOCK:
3049 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3050 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3051 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3052 case BPF_PROG_TYPE_SOCK_OPS:
Sean Youngfdb5c452018-06-19 00:04:24 +01003053 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003054 break;
3055 default:
3056 ret = -EINVAL;
David Ahernb2cd1252016-12-01 08:48:03 -08003057 }
3058
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003059 if (ret)
3060 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08003061 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01003062}
3063
3064#define BPF_PROG_DETACH_LAST_FIELD attach_type
3065
3066static int bpf_prog_detach(const union bpf_attr *attr)
3067{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07003068 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01003069
Daniel Mackf4324552016-11-23 16:52:27 +01003070 if (CHECK_ATTR(BPF_PROG_DETACH))
3071 return -EINVAL;
3072
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003073 ptype = attach_type_to_prog_type(attr->attach_type);
3074
3075 switch (ptype) {
3076 case BPF_PROG_TYPE_SK_MSG:
3077 case BPF_PROG_TYPE_SK_SKB:
Lorenz Bauerbb0de312020-06-29 10:56:28 +01003078 return sock_map_prog_detach(attr, ptype);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003079 case BPF_PROG_TYPE_LIRC_MODE2:
Sean Youngf4364dc2018-05-27 12:24:09 +01003080 return lirc_prog_detach(attr);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003081 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Lorenz Bauer4ac2add2020-06-29 10:56:26 +01003082 return netns_bpf_prog_detach(attr, ptype);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003083 case BPF_PROG_TYPE_CGROUP_DEVICE:
3084 case BPF_PROG_TYPE_CGROUP_SKB:
3085 case BPF_PROG_TYPE_CGROUP_SOCK:
3086 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3087 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3088 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3089 case BPF_PROG_TYPE_SOCK_OPS:
3090 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01003091 default:
3092 return -EINVAL;
3093 }
Daniel Mackf4324552016-11-23 16:52:27 +01003094}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07003095
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003096#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
3097
3098static int bpf_prog_query(const union bpf_attr *attr,
3099 union bpf_attr __user *uattr)
3100{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003101 if (!capable(CAP_NET_ADMIN))
3102 return -EPERM;
3103 if (CHECK_ATTR(BPF_PROG_QUERY))
3104 return -EINVAL;
3105 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3106 return -EINVAL;
3107
3108 switch (attr->query.attach_type) {
3109 case BPF_CGROUP_INET_INGRESS:
3110 case BPF_CGROUP_INET_EGRESS:
3111 case BPF_CGROUP_INET_SOCK_CREATE:
Stanislav Fomichevf5836742020-07-06 16:01:25 -07003112 case BPF_CGROUP_INET_SOCK_RELEASE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07003113 case BPF_CGROUP_INET4_BIND:
3114 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07003115 case BPF_CGROUP_INET4_POST_BIND:
3116 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07003117 case BPF_CGROUP_INET4_CONNECT:
3118 case BPF_CGROUP_INET6_CONNECT:
Daniel Borkmann1b66d252020-05-19 00:45:45 +02003119 case BPF_CGROUP_INET4_GETPEERNAME:
3120 case BPF_CGROUP_INET6_GETPEERNAME:
3121 case BPF_CGROUP_INET4_GETSOCKNAME:
3122 case BPF_CGROUP_INET6_GETSOCKNAME:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07003123 case BPF_CGROUP_UDP4_SENDMSG:
3124 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02003125 case BPF_CGROUP_UDP4_RECVMSG:
3126 case BPF_CGROUP_UDP6_RECVMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003127 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05003128 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08003129 case BPF_CGROUP_SYSCTL:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07003130 case BPF_CGROUP_GETSOCKOPT:
3131 case BPF_CGROUP_SETSOCKOPT:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07003132 return cgroup_bpf_prog_query(attr, uattr);
Sean Youngf4364dc2018-05-27 12:24:09 +01003133 case BPF_LIRC_MODE2:
3134 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07003135 case BPF_FLOW_DISSECTOR:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02003136 case BPF_SK_LOOKUP:
Jakub Sitnickia3fd7ce2020-05-31 10:28:36 +02003137 return netns_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003138 default:
3139 return -EINVAL;
3140 }
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003141}
Daniel Mackf4324552016-11-23 16:52:27 +01003142
Song Liu1b4d60e2020-09-25 13:54:29 -07003143#define BPF_PROG_TEST_RUN_LAST_FIELD test.cpu
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003144
3145static int bpf_prog_test_run(const union bpf_attr *attr,
3146 union bpf_attr __user *uattr)
3147{
3148 struct bpf_prog *prog;
3149 int ret = -ENOTSUPP;
3150
3151 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3152 return -EINVAL;
3153
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07003154 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3155 (!attr->test.ctx_size_in && attr->test.ctx_in))
3156 return -EINVAL;
3157
3158 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3159 (!attr->test.ctx_size_out && attr->test.ctx_out))
3160 return -EINVAL;
3161
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003162 prog = bpf_prog_get(attr->test.prog_fd);
3163 if (IS_ERR(prog))
3164 return PTR_ERR(prog);
3165
3166 if (prog->aux->ops->test_run)
3167 ret = prog->aux->ops->test_run(prog, attr, uattr);
3168
3169 bpf_prog_put(prog);
3170 return ret;
3171}
3172
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07003173#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3174
3175static int bpf_obj_get_next_id(const union bpf_attr *attr,
3176 union bpf_attr __user *uattr,
3177 struct idr *idr,
3178 spinlock_t *lock)
3179{
3180 u32 next_id = attr->start_id;
3181 int err = 0;
3182
3183 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3184 return -EINVAL;
3185
3186 if (!capable(CAP_SYS_ADMIN))
3187 return -EPERM;
3188
3189 next_id++;
3190 spin_lock_bh(lock);
3191 if (!idr_get_next(idr, &next_id))
3192 err = -ENOENT;
3193 spin_unlock_bh(lock);
3194
3195 if (!err)
3196 err = put_user(next_id, &uattr->next_id);
3197
3198 return err;
3199}
3200
Yonghong Song6086d292020-05-09 10:59:09 -07003201struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3202{
3203 struct bpf_map *map;
3204
3205 spin_lock_bh(&map_idr_lock);
3206again:
3207 map = idr_get_next(&map_idr, id);
3208 if (map) {
3209 map = __bpf_map_inc_not_zero(map, false);
3210 if (IS_ERR(map)) {
3211 (*id)++;
3212 goto again;
3213 }
3214 }
3215 spin_unlock_bh(&map_idr_lock);
3216
3217 return map;
3218}
3219
Alexei Starovoitova228a642020-07-01 18:10:18 -07003220struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3221{
3222 struct bpf_prog *prog;
3223
3224 spin_lock_bh(&prog_idr_lock);
3225again:
3226 prog = idr_get_next(&prog_idr, id);
3227 if (prog) {
3228 prog = bpf_prog_inc_not_zero(prog);
3229 if (IS_ERR(prog)) {
3230 (*id)++;
3231 goto again;
3232 }
3233 }
3234 spin_unlock_bh(&prog_idr_lock);
3235
3236 return prog;
3237}
3238
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003239#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3240
Björn Töpel7e6897f2019-12-13 18:51:09 +01003241struct bpf_prog *bpf_prog_by_id(u32 id)
3242{
3243 struct bpf_prog *prog;
3244
3245 if (!id)
3246 return ERR_PTR(-ENOENT);
3247
3248 spin_lock_bh(&prog_idr_lock);
3249 prog = idr_find(&prog_idr, id);
3250 if (prog)
3251 prog = bpf_prog_inc_not_zero(prog);
3252 else
3253 prog = ERR_PTR(-ENOENT);
3254 spin_unlock_bh(&prog_idr_lock);
3255 return prog;
3256}
3257
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003258static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3259{
3260 struct bpf_prog *prog;
3261 u32 id = attr->prog_id;
3262 int fd;
3263
3264 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3265 return -EINVAL;
3266
3267 if (!capable(CAP_SYS_ADMIN))
3268 return -EPERM;
3269
Björn Töpel7e6897f2019-12-13 18:51:09 +01003270 prog = bpf_prog_by_id(id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003271 if (IS_ERR(prog))
3272 return PTR_ERR(prog);
3273
3274 fd = bpf_prog_new_fd(prog);
3275 if (fd < 0)
3276 bpf_prog_put(prog);
3277
3278 return fd;
3279}
3280
Chenbo Feng6e71b042017-10-18 13:00:22 -07003281#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003282
3283static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3284{
3285 struct bpf_map *map;
3286 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07003287 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003288 int fd;
3289
Chenbo Feng6e71b042017-10-18 13:00:22 -07003290 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3291 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003292 return -EINVAL;
3293
3294 if (!capable(CAP_SYS_ADMIN))
3295 return -EPERM;
3296
Chenbo Feng6e71b042017-10-18 13:00:22 -07003297 f_flags = bpf_get_file_flag(attr->open_flags);
3298 if (f_flags < 0)
3299 return f_flags;
3300
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003301 spin_lock_bh(&map_idr_lock);
3302 map = idr_find(&map_idr, id);
3303 if (map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -07003304 map = __bpf_map_inc_not_zero(map, true);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003305 else
3306 map = ERR_PTR(-ENOENT);
3307 spin_unlock_bh(&map_idr_lock);
3308
3309 if (IS_ERR(map))
3310 return PTR_ERR(map);
3311
Chenbo Feng6e71b042017-10-18 13:00:22 -07003312 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003313 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08003314 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003315
3316 return fd;
3317}
3318
Daniel Borkmann7105e822017-12-20 13:42:57 +01003319static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003320 unsigned long addr, u32 *off,
3321 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01003322{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003323 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003324 int i;
3325
YiFei Zhu984fe942020-09-15 16:45:39 -07003326 mutex_lock(&prog->aux->used_maps_mutex);
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003327 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3328 map = prog->aux->used_maps[i];
3329 if (map == (void *)addr) {
3330 *type = BPF_PSEUDO_MAP_FD;
YiFei Zhu984fe942020-09-15 16:45:39 -07003331 goto out;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003332 }
3333 if (!map->ops->map_direct_value_meta)
3334 continue;
3335 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3336 *type = BPF_PSEUDO_MAP_VALUE;
YiFei Zhu984fe942020-09-15 16:45:39 -07003337 goto out;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003338 }
3339 }
YiFei Zhu984fe942020-09-15 16:45:39 -07003340 map = NULL;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003341
YiFei Zhu984fe942020-09-15 16:45:39 -07003342out:
3343 mutex_unlock(&prog->aux->used_maps_mutex);
3344 return map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003345}
3346
Kees Cook63960262020-07-02 15:45:23 -07003347static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3348 const struct cred *f_cred)
Daniel Borkmann7105e822017-12-20 13:42:57 +01003349{
3350 const struct bpf_map *map;
3351 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003352 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003353 u64 imm;
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003354 u8 code;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003355 int i;
3356
3357 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3358 GFP_USER);
3359 if (!insns)
3360 return insns;
3361
3362 for (i = 0; i < prog->len; i++) {
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003363 code = insns[i].code;
3364
3365 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003366 insns[i].code = BPF_JMP | BPF_CALL;
3367 insns[i].imm = BPF_FUNC_tail_call;
3368 /* fall-through */
3369 }
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003370 if (code == (BPF_JMP | BPF_CALL) ||
3371 code == (BPF_JMP | BPF_CALL_ARGS)) {
3372 if (code == (BPF_JMP | BPF_CALL_ARGS))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003373 insns[i].code = BPF_JMP | BPF_CALL;
Kees Cook63960262020-07-02 15:45:23 -07003374 if (!bpf_dump_raw_ok(f_cred))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003375 insns[i].imm = 0;
3376 continue;
3377 }
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003378 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3379 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3380 continue;
3381 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01003382
Andrii Nakryiko29fcb052020-06-12 17:21:15 -07003383 if (code != (BPF_LD | BPF_IMM | BPF_DW))
Daniel Borkmann7105e822017-12-20 13:42:57 +01003384 continue;
3385
3386 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003387 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003388 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003389 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003390 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02003391 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01003392 continue;
3393 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01003394 }
3395
3396 return insns;
3397}
3398
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003399static int set_info_rec_size(struct bpf_prog_info *info)
3400{
3401 /*
3402 * Ensure info.*_rec_size is the same as kernel expected size
3403 *
3404 * or
3405 *
3406 * Only allow zero *_rec_size if both _rec_size and _cnt are
3407 * zero. In this case, the kernel will set the expected
3408 * _rec_size back to the info.
3409 */
3410
Yonghong Song11d8b822018-12-10 14:14:08 -08003411 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003412 info->func_info_rec_size != sizeof(struct bpf_func_info))
3413 return -EINVAL;
3414
Yonghong Song11d8b822018-12-10 14:14:08 -08003415 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003416 info->line_info_rec_size != sizeof(struct bpf_line_info))
3417 return -EINVAL;
3418
Yonghong Song11d8b822018-12-10 14:14:08 -08003419 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003420 info->jited_line_info_rec_size != sizeof(__u64))
3421 return -EINVAL;
3422
3423 info->func_info_rec_size = sizeof(struct bpf_func_info);
3424 info->line_info_rec_size = sizeof(struct bpf_line_info);
3425 info->jited_line_info_rec_size = sizeof(__u64);
3426
3427 return 0;
3428}
3429
Kees Cook63960262020-07-02 15:45:23 -07003430static int bpf_prog_get_info_by_fd(struct file *file,
3431 struct bpf_prog *prog,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003432 const union bpf_attr *attr,
3433 union bpf_attr __user *uattr)
3434{
3435 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003436 struct bpf_prog_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003437 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003438 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003439 char __user *uinsns;
3440 u32 ulen;
3441 int err;
3442
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07003443 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003444 if (err)
3445 return err;
3446 info_len = min_t(u32, sizeof(info), info_len);
3447
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003448 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003449 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02003450 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003451
3452 info.type = prog->type;
3453 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003454 info.load_time = prog->aux->load_time;
3455 info.created_by_uid = from_kuid_munged(current_user_ns(),
3456 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02003457 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003458
3459 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003460 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3461
YiFei Zhu984fe942020-09-15 16:45:39 -07003462 mutex_lock(&prog->aux->used_maps_mutex);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003463 ulen = info.nr_map_ids;
3464 info.nr_map_ids = prog->aux->used_map_cnt;
3465 ulen = min_t(u32, info.nr_map_ids, ulen);
3466 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07003467 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003468 u32 i;
3469
3470 for (i = 0; i < ulen; i++)
3471 if (put_user(prog->aux->used_maps[i]->id,
YiFei Zhu984fe942020-09-15 16:45:39 -07003472 &user_map_ids[i])) {
3473 mutex_unlock(&prog->aux->used_maps_mutex);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003474 return -EFAULT;
YiFei Zhu984fe942020-09-15 16:45:39 -07003475 }
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003476 }
YiFei Zhu984fe942020-09-15 16:45:39 -07003477 mutex_unlock(&prog->aux->used_maps_mutex);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003478
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003479 err = set_info_rec_size(&info);
3480 if (err)
3481 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08003482
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003483 bpf_prog_get_stats(prog, &stats);
3484 info.run_time_ns = stats.nsecs;
3485 info.run_cnt = stats.cnt;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -08003486 info.recursion_misses = stats.misses;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003487
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003488 if (!bpf_capable()) {
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003489 info.jited_prog_len = 0;
3490 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303491 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01003492 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08003493 info.nr_func_info = 0;
3494 info.nr_line_info = 0;
3495 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003496 goto done;
3497 }
3498
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003499 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02003500 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003501 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003502 struct bpf_insn *insns_sanitized;
3503 bool fault;
3504
Kees Cook63960262020-07-02 15:45:23 -07003505 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003506 info.xlated_prog_insns = 0;
3507 goto done;
3508 }
Kees Cook63960262020-07-02 15:45:23 -07003509 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003510 if (!insns_sanitized)
3511 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003512 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3513 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003514 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3515 kfree(insns_sanitized);
3516 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003517 return -EFAULT;
3518 }
3519
Jakub Kicinski675fc272017-12-27 18:39:09 -08003520 if (bpf_prog_is_dev_bound(prog->aux)) {
3521 err = bpf_prog_offload_info_fill(&info, prog);
3522 if (err)
3523 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08003524 goto done;
3525 }
3526
3527 /* NOTE: the following code is supposed to be skipped for offload.
3528 * bpf_prog_offload_info_fill() is the place to fill similar fields
3529 * for offload.
3530 */
3531 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05303532 if (prog->aux->func_cnt) {
3533 u32 i;
3534
3535 info.jited_prog_len = 0;
3536 for (i = 0; i < prog->aux->func_cnt; i++)
3537 info.jited_prog_len += prog->aux->func[i]->jited_len;
3538 } else {
3539 info.jited_prog_len = prog->jited_len;
3540 }
3541
Jiong Wangfcfb1262018-01-16 16:05:19 -08003542 if (info.jited_prog_len && ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003543 if (bpf_dump_raw_ok(file->f_cred)) {
Jiong Wangfcfb1262018-01-16 16:05:19 -08003544 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3545 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05303546
3547 /* for multi-function programs, copy the JITed
3548 * instructions for all the functions
3549 */
3550 if (prog->aux->func_cnt) {
3551 u32 len, free, i;
3552 u8 *img;
3553
3554 free = ulen;
3555 for (i = 0; i < prog->aux->func_cnt; i++) {
3556 len = prog->aux->func[i]->jited_len;
3557 len = min_t(u32, len, free);
3558 img = (u8 *) prog->aux->func[i]->bpf_func;
3559 if (copy_to_user(uinsns, img, len))
3560 return -EFAULT;
3561 uinsns += len;
3562 free -= len;
3563 if (!free)
3564 break;
3565 }
3566 } else {
3567 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3568 return -EFAULT;
3569 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08003570 } else {
3571 info.jited_prog_insns = 0;
3572 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08003573 }
3574
Sandipan Dasdbecd732018-05-24 12:26:48 +05303575 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07003576 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003577 if (ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003578 if (bpf_dump_raw_ok(file->f_cred)) {
Song Liuff1889f2018-11-02 10:16:17 -07003579 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303580 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303581 u32 i;
3582
3583 /* copy the address of the kernel symbol
3584 * corresponding to each function
3585 */
3586 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3587 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07003588 if (prog->aux->func_cnt) {
3589 for (i = 0; i < ulen; i++) {
3590 ksym_addr = (unsigned long)
3591 prog->aux->func[i]->bpf_func;
3592 if (put_user((u64) ksym_addr,
3593 &user_ksyms[i]))
3594 return -EFAULT;
3595 }
3596 } else {
3597 ksym_addr = (unsigned long) prog->bpf_func;
3598 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05303599 return -EFAULT;
3600 }
3601 } else {
3602 info.jited_ksyms = 0;
3603 }
3604 }
3605
Sandipan Das815581c2018-05-24 12:26:52 +05303606 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07003607 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003608 if (ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003609 if (bpf_dump_raw_ok(file->f_cred)) {
Sandipan Das815581c2018-05-24 12:26:52 +05303610 u32 __user *user_lens;
3611 u32 func_len, i;
3612
3613 /* copy the JITed image lengths for each function */
3614 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3615 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07003616 if (prog->aux->func_cnt) {
3617 for (i = 0; i < ulen; i++) {
3618 func_len =
3619 prog->aux->func[i]->jited_len;
3620 if (put_user(func_len, &user_lens[i]))
3621 return -EFAULT;
3622 }
3623 } else {
3624 func_len = prog->jited_len;
3625 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05303626 return -EFAULT;
3627 }
3628 } else {
3629 info.jited_func_lens = 0;
3630 }
3631 }
3632
Martin KaFai Lau73372242018-12-05 17:35:43 -08003633 if (prog->aux->btf)
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08003634 info.btf_id = btf_obj_id(prog->aux->btf);
Yonghong Song838e9692018-11-19 15:29:11 -08003635
Yonghong Song11d8b822018-12-10 14:14:08 -08003636 ulen = info.nr_func_info;
3637 info.nr_func_info = prog->aux->func_info_cnt;
3638 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003639 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08003640
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003641 user_finfo = u64_to_user_ptr(info.func_info);
3642 ulen = min_t(u32, info.nr_func_info, ulen);
3643 if (copy_to_user(user_finfo, prog->aux->func_info,
3644 info.func_info_rec_size * ulen))
3645 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08003646 }
3647
Yonghong Song11d8b822018-12-10 14:14:08 -08003648 ulen = info.nr_line_info;
3649 info.nr_line_info = prog->aux->nr_linfo;
3650 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003651 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003652
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003653 user_linfo = u64_to_user_ptr(info.line_info);
3654 ulen = min_t(u32, info.nr_line_info, ulen);
3655 if (copy_to_user(user_linfo, prog->aux->linfo,
3656 info.line_info_rec_size * ulen))
3657 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003658 }
3659
Yonghong Song11d8b822018-12-10 14:14:08 -08003660 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003661 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08003662 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003663 else
Yonghong Song11d8b822018-12-10 14:14:08 -08003664 info.nr_jited_line_info = 0;
3665 if (info.nr_jited_line_info && ulen) {
Kees Cook63960262020-07-02 15:45:23 -07003666 if (bpf_dump_raw_ok(file->f_cred)) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003667 __u64 __user *user_linfo;
3668 u32 i;
3669
3670 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08003671 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003672 for (i = 0; i < ulen; i++) {
3673 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3674 &user_linfo[i]))
3675 return -EFAULT;
3676 }
3677 } else {
3678 info.jited_line_info = 0;
3679 }
3680 }
3681
Song Liuc872bdb2018-12-12 09:37:46 -08003682 ulen = info.nr_prog_tags;
3683 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3684 if (ulen) {
3685 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3686 u32 i;
3687
3688 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3689 ulen = min_t(u32, info.nr_prog_tags, ulen);
3690 if (prog->aux->func_cnt) {
3691 for (i = 0; i < ulen; i++) {
3692 if (copy_to_user(user_prog_tags[i],
3693 prog->aux->func[i]->tag,
3694 BPF_TAG_SIZE))
3695 return -EFAULT;
3696 }
3697 } else {
3698 if (copy_to_user(user_prog_tags[0],
3699 prog->tag, BPF_TAG_SIZE))
3700 return -EFAULT;
3701 }
3702 }
3703
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003704done:
3705 if (copy_to_user(uinfo, &info, info_len) ||
3706 put_user(info_len, &uattr->info.info_len))
3707 return -EFAULT;
3708
3709 return 0;
3710}
3711
Kees Cook63960262020-07-02 15:45:23 -07003712static int bpf_map_get_info_by_fd(struct file *file,
3713 struct bpf_map *map,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003714 const union bpf_attr *attr,
3715 union bpf_attr __user *uattr)
3716{
3717 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003718 struct bpf_map_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003719 u32 info_len = attr->info.info_len;
3720 int err;
3721
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07003722 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003723 if (err)
3724 return err;
3725 info_len = min_t(u32, sizeof(info), info_len);
3726
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003727 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003728 info.type = map->map_type;
3729 info.id = map->id;
3730 info.key_size = map->key_size;
3731 info.value_size = map->value_size;
3732 info.max_entries = map->max_entries;
3733 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07003734 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003735
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003736 if (map->btf) {
Andrii Nakryiko22dc4a02020-12-03 12:46:29 -08003737 info.btf_id = btf_obj_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07003738 info.btf_key_type_id = map->btf_key_type_id;
3739 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003740 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08003741 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003742
Jakub Kicinski52775b32018-01-17 19:13:28 -08003743 if (bpf_map_is_dev_bound(map)) {
3744 err = bpf_map_offload_info_fill(&info, map);
3745 if (err)
3746 return err;
3747 }
3748
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003749 if (copy_to_user(uinfo, &info, info_len) ||
3750 put_user(info_len, &uattr->info.info_len))
3751 return -EFAULT;
3752
3753 return 0;
3754}
3755
Kees Cook63960262020-07-02 15:45:23 -07003756static int bpf_btf_get_info_by_fd(struct file *file,
3757 struct btf *btf,
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003758 const union bpf_attr *attr,
3759 union bpf_attr __user *uattr)
3760{
3761 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3762 u32 info_len = attr->info.info_len;
3763 int err;
3764
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07003765 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003766 if (err)
3767 return err;
3768
3769 return btf_get_info_by_fd(btf, attr, uattr);
3770}
3771
Kees Cook63960262020-07-02 15:45:23 -07003772static int bpf_link_get_info_by_fd(struct file *file,
3773 struct bpf_link *link,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003774 const union bpf_attr *attr,
3775 union bpf_attr __user *uattr)
3776{
3777 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3778 struct bpf_link_info info;
3779 u32 info_len = attr->info.info_len;
3780 int err;
3781
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07003782 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003783 if (err)
3784 return err;
3785 info_len = min_t(u32, sizeof(info), info_len);
3786
3787 memset(&info, 0, sizeof(info));
3788 if (copy_from_user(&info, uinfo, info_len))
3789 return -EFAULT;
3790
3791 info.type = link->type;
3792 info.id = link->id;
3793 info.prog_id = link->prog->aux->id;
3794
3795 if (link->ops->fill_link_info) {
3796 err = link->ops->fill_link_info(link, &info);
3797 if (err)
3798 return err;
3799 }
3800
3801 if (copy_to_user(uinfo, &info, info_len) ||
3802 put_user(info_len, &uattr->info.info_len))
3803 return -EFAULT;
3804
3805 return 0;
3806}
3807
3808
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003809#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3810
3811static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3812 union bpf_attr __user *uattr)
3813{
3814 int ufd = attr->info.bpf_fd;
3815 struct fd f;
3816 int err;
3817
3818 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3819 return -EINVAL;
3820
3821 f = fdget(ufd);
3822 if (!f.file)
3823 return -EBADFD;
3824
3825 if (f.file->f_op == &bpf_prog_fops)
Kees Cook63960262020-07-02 15:45:23 -07003826 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003827 uattr);
3828 else if (f.file->f_op == &bpf_map_fops)
Kees Cook63960262020-07-02 15:45:23 -07003829 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003830 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07003831 else if (f.file->f_op == &btf_fops)
Kees Cook63960262020-07-02 15:45:23 -07003832 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003833 else if (f.file->f_op == &bpf_link_fops)
Kees Cook63960262020-07-02 15:45:23 -07003834 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
Andrii Nakryikof2e10bf2020-04-28 17:16:08 -07003835 attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003836 else
3837 err = -EINVAL;
3838
3839 fdput(f);
3840 return err;
3841}
3842
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003843#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3844
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07003845static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003846{
3847 if (CHECK_ATTR(BPF_BTF_LOAD))
3848 return -EINVAL;
3849
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07003850 if (!bpf_capable())
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003851 return -EPERM;
3852
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07003853 return btf_new_fd(attr, uattr);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003854}
3855
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003856#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3857
3858static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3859{
3860 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3861 return -EINVAL;
3862
3863 if (!capable(CAP_SYS_ADMIN))
3864 return -EPERM;
3865
3866 return btf_get_fd_by_id(attr->btf_id);
3867}
3868
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003869static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3870 union bpf_attr __user *uattr,
3871 u32 prog_id, u32 fd_type,
3872 const char *buf, u64 probe_offset,
3873 u64 probe_addr)
3874{
3875 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3876 u32 len = buf ? strlen(buf) : 0, input_len;
3877 int err = 0;
3878
3879 if (put_user(len, &uattr->task_fd_query.buf_len))
3880 return -EFAULT;
3881 input_len = attr->task_fd_query.buf_len;
3882 if (input_len && ubuf) {
3883 if (!len) {
3884 /* nothing to copy, just make ubuf NULL terminated */
3885 char zero = '\0';
3886
3887 if (put_user(zero, ubuf))
3888 return -EFAULT;
3889 } else if (input_len >= len + 1) {
3890 /* ubuf can hold the string with NULL terminator */
3891 if (copy_to_user(ubuf, buf, len + 1))
3892 return -EFAULT;
3893 } else {
3894 /* ubuf cannot hold the string with NULL terminator,
3895 * do a partial copy with NULL terminator.
3896 */
3897 char zero = '\0';
3898
3899 err = -ENOSPC;
3900 if (copy_to_user(ubuf, buf, input_len - 1))
3901 return -EFAULT;
3902 if (put_user(zero, ubuf + input_len - 1))
3903 return -EFAULT;
3904 }
3905 }
3906
3907 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3908 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3909 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3910 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3911 return -EFAULT;
3912
3913 return err;
3914}
3915
3916#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3917
3918static int bpf_task_fd_query(const union bpf_attr *attr,
3919 union bpf_attr __user *uattr)
3920{
3921 pid_t pid = attr->task_fd_query.pid;
3922 u32 fd = attr->task_fd_query.fd;
3923 const struct perf_event *event;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003924 struct task_struct *task;
3925 struct file *file;
3926 int err;
3927
3928 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3929 return -EINVAL;
3930
3931 if (!capable(CAP_SYS_ADMIN))
3932 return -EPERM;
3933
3934 if (attr->task_fd_query.flags != 0)
3935 return -EINVAL;
3936
3937 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3938 if (!task)
3939 return -ENOENT;
3940
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003941 err = 0;
Eric W. Biedermanb48845a2020-11-20 17:14:22 -06003942 file = fget_task(task, fd);
3943 put_task_struct(task);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003944 if (!file)
Eric W. Biedermanb48845a2020-11-20 17:14:22 -06003945 return -EBADF;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003946
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003947 if (file->f_op == &bpf_link_fops) {
3948 struct bpf_link *link = file->private_data;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003949
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003950 if (link->ops == &bpf_raw_tp_link_lops) {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003951 struct bpf_raw_tp_link *raw_tp =
3952 container_of(link, struct bpf_raw_tp_link, link);
3953 struct bpf_raw_event_map *btp = raw_tp->btp;
3954
3955 err = bpf_task_fd_query_copy(attr, uattr,
3956 raw_tp->link.prog->aux->id,
3957 BPF_FD_TYPE_RAW_TRACEPOINT,
3958 btp->tp->name, 0, 0);
3959 goto put_file;
3960 }
3961 goto out_not_supp;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003962 }
3963
3964 event = perf_get_event(file);
3965 if (!IS_ERR(event)) {
3966 u64 probe_offset, probe_addr;
3967 u32 prog_id, fd_type;
3968 const char *buf;
3969
3970 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3971 &buf, &probe_offset,
3972 &probe_addr);
3973 if (!err)
3974 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3975 fd_type, buf,
3976 probe_offset,
3977 probe_addr);
3978 goto put_file;
3979 }
3980
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003981out_not_supp:
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003982 err = -ENOTSUPP;
3983put_file:
3984 fput(file);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003985 return err;
3986}
3987
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003988#define BPF_MAP_BATCH_LAST_FIELD batch.flags
3989
3990#define BPF_DO_BATCH(fn) \
3991 do { \
3992 if (!fn) { \
3993 err = -ENOTSUPP; \
3994 goto err_put; \
3995 } \
3996 err = fn(map, attr, uattr); \
3997 } while (0)
3998
3999static int bpf_map_do_batch(const union bpf_attr *attr,
4000 union bpf_attr __user *uattr,
4001 int cmd)
4002{
4003 struct bpf_map *map;
4004 int err, ufd;
4005 struct fd f;
4006
4007 if (CHECK_ATTR(BPF_MAP_BATCH))
4008 return -EINVAL;
4009
4010 ufd = attr->batch.map_fd;
4011 f = fdget(ufd);
4012 map = __bpf_map_get(f);
4013 if (IS_ERR(map))
4014 return PTR_ERR(map);
4015
Yonghong Song05799632020-01-15 10:43:04 -08004016 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
4017 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004018 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
4019 err = -EPERM;
4020 goto err_put;
4021 }
4022
4023 if (cmd != BPF_MAP_LOOKUP_BATCH &&
4024 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
4025 err = -EPERM;
4026 goto err_put;
4027 }
4028
4029 if (cmd == BPF_MAP_LOOKUP_BATCH)
4030 BPF_DO_BATCH(map->ops->map_lookup_batch);
Yonghong Song05799632020-01-15 10:43:04 -08004031 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4032 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004033 else if (cmd == BPF_MAP_UPDATE_BATCH)
4034 BPF_DO_BATCH(map->ops->map_update_batch);
4035 else
4036 BPF_DO_BATCH(map->ops->map_delete_batch);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004037
4038err_put:
4039 fdput(f);
4040 return err;
4041}
4042
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004043static int tracing_bpf_link_attach(const union bpf_attr *attr, bpfptr_t uattr,
4044 struct bpf_prog *prog)
Yonghong Songde4e05c2020-05-09 10:59:01 -07004045{
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004046 if (attr->link_create.attach_type != prog->expected_attach_type)
4047 return -EINVAL;
Yonghong Songde4e05c2020-05-09 10:59:01 -07004048
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004049 if (prog->expected_attach_type == BPF_TRACE_ITER)
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004050 return bpf_iter_link_attach(attr, uattr, prog);
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004051 else if (prog->type == BPF_PROG_TYPE_EXT)
4052 return bpf_tracing_prog_attach(prog,
4053 attr->link_create.target_fd,
4054 attr->link_create.target_btf_id);
Yonghong Songde4e05c2020-05-09 10:59:01 -07004055 return -EINVAL;
4056}
4057
Yonghong Song5e7b3022020-08-04 22:50:56 -07004058#define BPF_LINK_CREATE_LAST_FIELD link_create.iter_info_len
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004059static int link_create(union bpf_attr *attr, bpfptr_t uattr)
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004060{
4061 enum bpf_prog_type ptype;
4062 struct bpf_prog *prog;
4063 int ret;
4064
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004065 if (CHECK_ATTR(BPF_LINK_CREATE))
4066 return -EINVAL;
4067
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004068 prog = bpf_prog_get(attr->link_create.prog_fd);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004069 if (IS_ERR(prog))
4070 return PTR_ERR(prog);
4071
4072 ret = bpf_prog_attach_check_attach_type(prog,
4073 attr->link_create.attach_type);
4074 if (ret)
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004075 goto out;
4076
4077 if (prog->type == BPF_PROG_TYPE_EXT) {
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004078 ret = tracing_bpf_link_attach(attr, uattr, prog);
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004079 goto out;
4080 }
4081
4082 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4083 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4084 ret = -EINVAL;
4085 goto out;
4086 }
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004087
4088 switch (ptype) {
4089 case BPF_PROG_TYPE_CGROUP_SKB:
4090 case BPF_PROG_TYPE_CGROUP_SOCK:
4091 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4092 case BPF_PROG_TYPE_SOCK_OPS:
4093 case BPF_PROG_TYPE_CGROUP_DEVICE:
4094 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4095 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4096 ret = cgroup_bpf_link_attach(attr, prog);
4097 break;
Yonghong Songde4e05c2020-05-09 10:59:01 -07004098 case BPF_PROG_TYPE_TRACING:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004099 ret = tracing_bpf_link_attach(attr, uattr, prog);
Yonghong Songde4e05c2020-05-09 10:59:01 -07004100 break;
Jakub Sitnicki7f045a42020-05-31 10:28:38 +02004101 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Jakub Sitnickie9ddbb72020-07-17 12:35:23 +02004102 case BPF_PROG_TYPE_SK_LOOKUP:
Jakub Sitnicki7f045a42020-05-31 10:28:38 +02004103 ret = netns_bpf_link_create(attr, prog);
4104 break;
Andrii Nakryiko310ad792020-07-28 12:05:27 -07004105#ifdef CONFIG_NET
Andrii Nakryikoaa8d3a72020-07-21 23:45:57 -07004106 case BPF_PROG_TYPE_XDP:
4107 ret = bpf_xdp_link_attach(attr, prog);
4108 break;
Andrii Nakryiko310ad792020-07-28 12:05:27 -07004109#endif
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004110 default:
4111 ret = -EINVAL;
4112 }
4113
Toke Høiland-Jørgensen4a1e7c02020-09-29 14:45:51 +02004114out:
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004115 if (ret < 0)
4116 bpf_prog_put(prog);
4117 return ret;
4118}
4119
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004120#define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4121
4122static int link_update(union bpf_attr *attr)
4123{
4124 struct bpf_prog *old_prog = NULL, *new_prog;
4125 struct bpf_link *link;
4126 u32 flags;
4127 int ret;
4128
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004129 if (CHECK_ATTR(BPF_LINK_UPDATE))
4130 return -EINVAL;
4131
4132 flags = attr->link_update.flags;
4133 if (flags & ~BPF_F_REPLACE)
4134 return -EINVAL;
4135
4136 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4137 if (IS_ERR(link))
4138 return PTR_ERR(link);
4139
4140 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004141 if (IS_ERR(new_prog)) {
4142 ret = PTR_ERR(new_prog);
4143 goto out_put_link;
4144 }
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004145
4146 if (flags & BPF_F_REPLACE) {
4147 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4148 if (IS_ERR(old_prog)) {
4149 ret = PTR_ERR(old_prog);
4150 old_prog = NULL;
4151 goto out_put_progs;
4152 }
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004153 } else if (attr->link_update.old_prog_fd) {
4154 ret = -EINVAL;
4155 goto out_put_progs;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004156 }
4157
Andrii Nakryikof9d04122020-04-28 17:16:05 -07004158 if (link->ops->update_prog)
4159 ret = link->ops->update_prog(link, new_prog, old_prog);
4160 else
Jakub Sitnickife537392020-05-25 14:29:28 +02004161 ret = -EINVAL;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004162
4163out_put_progs:
4164 if (old_prog)
4165 bpf_prog_put(old_prog);
4166 if (ret)
4167 bpf_prog_put(new_prog);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07004168out_put_link:
4169 bpf_link_put(link);
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004170 return ret;
4171}
4172
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07004173#define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4174
4175static int link_detach(union bpf_attr *attr)
4176{
4177 struct bpf_link *link;
4178 int ret;
4179
4180 if (CHECK_ATTR(BPF_LINK_DETACH))
4181 return -EINVAL;
4182
4183 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4184 if (IS_ERR(link))
4185 return PTR_ERR(link);
4186
4187 if (link->ops->detach)
4188 ret = link->ops->detach(link);
4189 else
4190 ret = -EOPNOTSUPP;
4191
4192 bpf_link_put(link);
4193 return ret;
4194}
4195
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004196static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004197{
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004198 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
4199}
4200
4201struct bpf_link *bpf_link_by_id(u32 id)
4202{
4203 struct bpf_link *link;
4204
4205 if (!id)
4206 return ERR_PTR(-ENOENT);
4207
4208 spin_lock_bh(&link_idr_lock);
4209 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
4210 link = idr_find(&link_idr, id);
4211 if (link) {
4212 if (link->id)
4213 link = bpf_link_inc_not_zero(link);
4214 else
4215 link = ERR_PTR(-EAGAIN);
4216 } else {
4217 link = ERR_PTR(-ENOENT);
4218 }
4219 spin_unlock_bh(&link_idr_lock);
4220 return link;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004221}
4222
4223#define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4224
4225static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4226{
4227 struct bpf_link *link;
4228 u32 id = attr->link_id;
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004229 int fd;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004230
4231 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4232 return -EINVAL;
4233
4234 if (!capable(CAP_SYS_ADMIN))
4235 return -EPERM;
4236
Alexei Starovoitov005142b2020-08-18 21:27:56 -07004237 link = bpf_link_by_id(id);
4238 if (IS_ERR(link))
4239 return PTR_ERR(link);
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004240
4241 fd = bpf_link_new_fd(link);
4242 if (fd < 0)
4243 bpf_link_put(link);
4244
4245 return fd;
4246}
4247
Song Liud46edd62020-04-30 00:15:04 -07004248DEFINE_MUTEX(bpf_stats_enabled_mutex);
4249
4250static int bpf_stats_release(struct inode *inode, struct file *file)
4251{
4252 mutex_lock(&bpf_stats_enabled_mutex);
4253 static_key_slow_dec(&bpf_stats_enabled_key.key);
4254 mutex_unlock(&bpf_stats_enabled_mutex);
4255 return 0;
4256}
4257
4258static const struct file_operations bpf_stats_fops = {
4259 .release = bpf_stats_release,
4260};
4261
4262static int bpf_enable_runtime_stats(void)
4263{
4264 int fd;
4265
4266 mutex_lock(&bpf_stats_enabled_mutex);
4267
4268 /* Set a very high limit to avoid overflow */
4269 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4270 mutex_unlock(&bpf_stats_enabled_mutex);
4271 return -EBUSY;
4272 }
4273
4274 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4275 if (fd >= 0)
4276 static_key_slow_inc(&bpf_stats_enabled_key.key);
4277
4278 mutex_unlock(&bpf_stats_enabled_mutex);
4279 return fd;
4280}
4281
4282#define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4283
4284static int bpf_enable_stats(union bpf_attr *attr)
4285{
4286
4287 if (CHECK_ATTR(BPF_ENABLE_STATS))
4288 return -EINVAL;
4289
4290 if (!capable(CAP_SYS_ADMIN))
4291 return -EPERM;
4292
4293 switch (attr->enable_stats.type) {
4294 case BPF_STATS_RUN_TIME:
4295 return bpf_enable_runtime_stats();
4296 default:
4297 break;
4298 }
4299 return -EINVAL;
4300}
4301
Yonghong Songac51d992020-05-09 10:59:05 -07004302#define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4303
4304static int bpf_iter_create(union bpf_attr *attr)
4305{
4306 struct bpf_link *link;
4307 int err;
4308
4309 if (CHECK_ATTR(BPF_ITER_CREATE))
4310 return -EINVAL;
4311
4312 if (attr->iter_create.flags)
4313 return -EINVAL;
4314
4315 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4316 if (IS_ERR(link))
4317 return PTR_ERR(link);
4318
4319 err = bpf_iter_new_fd(link);
4320 bpf_link_put(link);
4321
4322 return err;
4323}
4324
YiFei Zhuef153142020-09-15 16:45:40 -07004325#define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4326
4327static int bpf_prog_bind_map(union bpf_attr *attr)
4328{
4329 struct bpf_prog *prog;
4330 struct bpf_map *map;
4331 struct bpf_map **used_maps_old, **used_maps_new;
4332 int i, ret = 0;
4333
4334 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4335 return -EINVAL;
4336
4337 if (attr->prog_bind_map.flags)
4338 return -EINVAL;
4339
4340 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4341 if (IS_ERR(prog))
4342 return PTR_ERR(prog);
4343
4344 map = bpf_map_get(attr->prog_bind_map.map_fd);
4345 if (IS_ERR(map)) {
4346 ret = PTR_ERR(map);
4347 goto out_prog_put;
4348 }
4349
4350 mutex_lock(&prog->aux->used_maps_mutex);
4351
4352 used_maps_old = prog->aux->used_maps;
4353
4354 for (i = 0; i < prog->aux->used_map_cnt; i++)
Stanislav Fomichev1028ae42020-10-02 17:25:44 -07004355 if (used_maps_old[i] == map) {
4356 bpf_map_put(map);
YiFei Zhuef153142020-09-15 16:45:40 -07004357 goto out_unlock;
Stanislav Fomichev1028ae42020-10-02 17:25:44 -07004358 }
YiFei Zhuef153142020-09-15 16:45:40 -07004359
4360 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4361 sizeof(used_maps_new[0]),
4362 GFP_KERNEL);
4363 if (!used_maps_new) {
4364 ret = -ENOMEM;
4365 goto out_unlock;
4366 }
4367
4368 memcpy(used_maps_new, used_maps_old,
4369 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4370 used_maps_new[prog->aux->used_map_cnt] = map;
4371
4372 prog->aux->used_map_cnt++;
4373 prog->aux->used_maps = used_maps_new;
4374
4375 kfree(used_maps_old);
4376
4377out_unlock:
4378 mutex_unlock(&prog->aux->used_maps_mutex);
4379
4380 if (ret)
4381 bpf_map_put(map);
4382out_prog_put:
4383 bpf_prog_put(prog);
4384 return ret;
4385}
4386
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004387static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004388{
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004389 union bpf_attr attr;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004390 int err;
4391
Alexei Starovoitov2c78ee82020-05-13 16:03:54 -07004392 if (sysctl_unprivileged_bpf_disabled && !bpf_capable())
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004393 return -EPERM;
4394
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07004395 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004396 if (err)
4397 return err;
4398 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004399
4400 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01004401 memset(&attr, 0, sizeof(attr));
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004402 if (copy_from_bpfptr(&attr, uattr, size) != 0)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004403 return -EFAULT;
4404
Chenbo Fengafdb09c2017-10-18 13:00:24 -07004405 err = security_bpf(cmd, &attr, size);
4406 if (err < 0)
4407 return err;
4408
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004409 switch (cmd) {
4410 case BPF_MAP_CREATE:
4411 err = map_create(&attr);
4412 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07004413 case BPF_MAP_LOOKUP_ELEM:
4414 err = map_lookup_elem(&attr);
4415 break;
4416 case BPF_MAP_UPDATE_ELEM:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004417 err = map_update_elem(&attr, uattr);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07004418 break;
4419 case BPF_MAP_DELETE_ELEM:
4420 err = map_delete_elem(&attr);
4421 break;
4422 case BPF_MAP_GET_NEXT_KEY:
4423 err = map_get_next_key(&attr);
4424 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02004425 case BPF_MAP_FREEZE:
4426 err = map_freeze(&attr);
4427 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004428 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08004429 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07004430 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01004431 case BPF_OBJ_PIN:
4432 err = bpf_obj_pin(&attr);
4433 break;
4434 case BPF_OBJ_GET:
4435 err = bpf_obj_get(&attr);
4436 break;
Daniel Mackf4324552016-11-23 16:52:27 +01004437 case BPF_PROG_ATTACH:
4438 err = bpf_prog_attach(&attr);
4439 break;
4440 case BPF_PROG_DETACH:
4441 err = bpf_prog_detach(&attr);
4442 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07004443 case BPF_PROG_QUERY:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004444 err = bpf_prog_query(&attr, uattr.user);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07004445 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07004446 case BPF_PROG_TEST_RUN:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004447 err = bpf_prog_test_run(&attr, uattr.user);
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07004448 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004449 case BPF_PROG_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004450 err = bpf_obj_get_next_id(&attr, uattr.user,
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004451 &prog_idr, &prog_idr_lock);
4452 break;
4453 case BPF_MAP_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004454 err = bpf_obj_get_next_id(&attr, uattr.user,
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07004455 &map_idr, &map_idr_lock);
4456 break;
Quentin Monnet1b9ed842019-08-20 10:31:50 +01004457 case BPF_BTF_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004458 err = bpf_obj_get_next_id(&attr, uattr.user,
Quentin Monnet1b9ed842019-08-20 10:31:50 +01004459 &btf_idr, &btf_idr_lock);
4460 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07004461 case BPF_PROG_GET_FD_BY_ID:
4462 err = bpf_prog_get_fd_by_id(&attr);
4463 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07004464 case BPF_MAP_GET_FD_BY_ID:
4465 err = bpf_map_get_fd_by_id(&attr);
4466 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004467 case BPF_OBJ_GET_INFO_BY_FD:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004468 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07004469 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07004470 case BPF_RAW_TRACEPOINT_OPEN:
4471 err = bpf_raw_tracepoint_open(&attr);
4472 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004473 case BPF_BTF_LOAD:
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07004474 err = bpf_btf_load(&attr, uattr);
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07004475 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07004476 case BPF_BTF_GET_FD_BY_ID:
4477 err = bpf_btf_get_fd_by_id(&attr);
4478 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004479 case BPF_TASK_FD_QUERY:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004480 err = bpf_task_fd_query(&attr, uattr.user);
Yonghong Song41bdc4b2018-05-24 11:21:09 -07004481 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02004482 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
4483 err = map_lookup_and_delete_elem(&attr);
4484 break;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004485 case BPF_MAP_LOOKUP_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004486 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08004487 break;
Yonghong Song05799632020-01-15 10:43:04 -08004488 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004489 err = bpf_map_do_batch(&attr, uattr.user,
Yonghong Song05799632020-01-15 10:43:04 -08004490 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
4491 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004492 case BPF_MAP_UPDATE_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004493 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004494 break;
4495 case BPF_MAP_DELETE_BATCH:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004496 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08004497 break;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004498 case BPF_LINK_CREATE:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004499 err = link_create(&attr, uattr);
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07004500 break;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07004501 case BPF_LINK_UPDATE:
4502 err = link_update(&attr);
4503 break;
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004504 case BPF_LINK_GET_FD_BY_ID:
4505 err = bpf_link_get_fd_by_id(&attr);
4506 break;
4507 case BPF_LINK_GET_NEXT_ID:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004508 err = bpf_obj_get_next_id(&attr, uattr.user,
Andrii Nakryiko2d602c82020-04-28 17:16:07 -07004509 &link_idr, &link_idr_lock);
4510 break;
Song Liud46edd62020-04-30 00:15:04 -07004511 case BPF_ENABLE_STATS:
4512 err = bpf_enable_stats(&attr);
4513 break;
Yonghong Songac51d992020-05-09 10:59:05 -07004514 case BPF_ITER_CREATE:
4515 err = bpf_iter_create(&attr);
4516 break;
Andrii Nakryiko73b11c2a2020-07-31 11:28:26 -07004517 case BPF_LINK_DETACH:
4518 err = link_detach(&attr);
4519 break;
YiFei Zhuef153142020-09-15 16:45:40 -07004520 case BPF_PROG_BIND_MAP:
4521 err = bpf_prog_bind_map(&attr);
4522 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07004523 default:
4524 err = -EINVAL;
4525 break;
4526 }
4527
4528 return err;
4529}
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004530
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004531SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
4532{
4533 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
4534}
4535
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004536static bool syscall_prog_is_valid_access(int off, int size,
4537 enum bpf_access_type type,
4538 const struct bpf_prog *prog,
4539 struct bpf_insn_access_aux *info)
4540{
4541 if (off < 0 || off >= U16_MAX)
4542 return false;
4543 if (off % size != 0)
4544 return false;
4545 return true;
4546}
4547
4548BPF_CALL_3(bpf_sys_bpf, int, cmd, void *, attr, u32, attr_size)
4549{
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004550 switch (cmd) {
4551 case BPF_MAP_CREATE:
4552 case BPF_MAP_UPDATE_ELEM:
4553 case BPF_MAP_FREEZE:
4554 case BPF_PROG_LOAD:
Alexei Starovoitovc571bd72021-05-13 17:36:08 -07004555 case BPF_BTF_LOAD:
Alexei Starovoitovaf2ac3e2021-05-13 17:36:05 -07004556 break;
4557 /* case BPF_PROG_TEST_RUN:
4558 * is not part of this list to prevent recursive test_run
4559 */
4560 default:
4561 return -EINVAL;
4562 }
4563 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004564}
4565
Pu Lehui3a2daa72021-05-19 14:41:16 +08004566static const struct bpf_func_proto bpf_sys_bpf_proto = {
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004567 .func = bpf_sys_bpf,
4568 .gpl_only = false,
4569 .ret_type = RET_INTEGER,
4570 .arg1_type = ARG_ANYTHING,
4571 .arg2_type = ARG_PTR_TO_MEM,
4572 .arg3_type = ARG_CONST_SIZE,
4573};
4574
4575const struct bpf_func_proto * __weak
4576tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4577{
4578 return bpf_base_func_proto(func_id);
4579}
4580
Alexei Starovoitov3abea082021-05-13 17:36:12 -07004581BPF_CALL_1(bpf_sys_close, u32, fd)
4582{
4583 /* When bpf program calls this helper there should not be
4584 * an fdget() without matching completed fdput().
4585 * This helper is allowed in the following callchain only:
4586 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
4587 */
4588 return close_fd(fd);
4589}
4590
Pu Lehui3a2daa72021-05-19 14:41:16 +08004591static const struct bpf_func_proto bpf_sys_close_proto = {
Alexei Starovoitov3abea082021-05-13 17:36:12 -07004592 .func = bpf_sys_close,
4593 .gpl_only = false,
4594 .ret_type = RET_INTEGER,
4595 .arg1_type = ARG_ANYTHING,
4596};
4597
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004598static const struct bpf_func_proto *
4599syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
4600{
4601 switch (func_id) {
4602 case BPF_FUNC_sys_bpf:
4603 return &bpf_sys_bpf_proto;
Alexei Starovoitov3d784172021-05-13 17:36:11 -07004604 case BPF_FUNC_btf_find_by_name_kind:
4605 return &bpf_btf_find_by_name_kind_proto;
Alexei Starovoitov3abea082021-05-13 17:36:12 -07004606 case BPF_FUNC_sys_close:
4607 return &bpf_sys_close_proto;
Alexei Starovoitov79a7f8b2021-05-13 17:36:03 -07004608 default:
4609 return tracing_prog_func_proto(func_id, prog);
4610 }
4611}
4612
4613const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
4614 .get_func_proto = syscall_prog_func_proto,
4615 .is_valid_access = syscall_prog_is_valid_access,
4616};
4617
4618const struct bpf_prog_ops bpf_syscall_prog_ops = {
4619 .test_run = bpf_prog_test_run_syscall,
4620};