blob: 5439e05e3d258eeb092b7068bf00601bc62ab074 [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>
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07007#include <linux/btf.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07008#include <linux/syscalls.h>
9#include <linux/slab.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010010#include <linux/sched/signal.h>
Daniel Borkmannd407bd22017-01-18 15:14:17 +010011#include <linux/vmalloc.h>
12#include <linux/mmzone.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070013#include <linux/anon_inodes.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070014#include <linux/fdtable.h>
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -070015#include <linux/file.h>
Yonghong Song41bdc4b2018-05-24 11:21:09 -070016#include <linux/fs.h>
Alexei Starovoitov09756af2014-09-26 00:17:00 -070017#include <linux/license.h>
18#include <linux/filter.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070019#include <linux/version.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>
KP Singh9e4e01d2020-03-29 01:43:52 +010028#include <linux/bpf_lsm.h>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070029
Daniel Borkmannda765a22019-11-22 21:07:58 +010030#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
31 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
32 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
33#define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070034#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
Daniel Borkmannda765a22019-11-22 21:07:58 +010035#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
36 IS_FD_HASH(map))
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070037
Chenbo Feng6e71b042017-10-18 13:00:22 -070038#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
39
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080040DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070041static DEFINE_IDR(prog_idr);
42static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070043static DEFINE_IDR(map_idr);
44static DEFINE_SPINLOCK(map_idr_lock);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -070045static DEFINE_IDR(link_idr);
46static DEFINE_SPINLOCK(link_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080047
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070048int sysctl_unprivileged_bpf_disabled __read_mostly;
49
Johannes Berg40077e02017-04-11 15:34:58 +020050static const struct bpf_map_ops * const bpf_map_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -080051#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
Johannes Berg40077e02017-04-11 15:34:58 +020052#define BPF_MAP_TYPE(_id, _ops) \
53 [_id] = &_ops,
54#include <linux/bpf_types.h>
55#undef BPF_PROG_TYPE
56#undef BPF_MAP_TYPE
57};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070058
Mickaël Salaün752ba562017-08-07 20:45:20 +020059/*
60 * If we're handed a bigger struct than we know of, ensure all the unknown bits
61 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
62 * we don't know about yet.
63 *
64 * There is a ToCToU between this function call and the following
65 * copy_from_user() call. However, this is not a concern since this function is
66 * meant to be a future-proofing of bits.
67 */
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070068int bpf_check_uarg_tail_zero(void __user *uaddr,
69 size_t expected_size,
70 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020071{
72 unsigned char __user *addr;
73 unsigned char __user *end;
74 unsigned char val;
75 int err;
76
Mickaël Salaün752ba562017-08-07 20:45:20 +020077 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
78 return -E2BIG;
79
Linus Torvalds96d4f262019-01-03 18:57:57 -080080 if (unlikely(!access_ok(uaddr, actual_size)))
Mickaël Salaün752ba562017-08-07 20:45:20 +020081 return -EFAULT;
82
Mickaël Salaün58291a72017-08-07 20:45:19 +020083 if (actual_size <= expected_size)
84 return 0;
85
86 addr = uaddr + expected_size;
87 end = uaddr + actual_size;
88
89 for (; addr < end; addr++) {
90 err = get_user(val, addr);
91 if (err)
92 return err;
93 if (val)
94 return -E2BIG;
95 }
96
97 return 0;
98}
99
Jakub Kicinskia3884572018-01-11 20:29:09 -0800100const struct bpf_map_ops bpf_map_offload_ops = {
101 .map_alloc = bpf_map_offload_map_alloc,
102 .map_free = bpf_map_offload_map_free,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200103 .map_check_btf = map_check_no_btf,
Jakub Kicinskia3884572018-01-11 20:29:09 -0800104};
105
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700106static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
107{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800108 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100109 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700110 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800111 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700112
Mark Rutland9ef09e32018-05-03 17:04:59 +0100113 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800114 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100115 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
116 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800117 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200118 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700119
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800120 if (ops->map_alloc_check) {
121 err = ops->map_alloc_check(attr);
122 if (err)
123 return ERR_PTR(err);
124 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800125 if (attr->map_ifindex)
126 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800127 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200128 if (IS_ERR(map))
129 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800130 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100131 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200132 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700133}
134
Brian Vazquez15c14a32020-01-15 10:43:00 -0800135static u32 bpf_map_value_size(struct bpf_map *map)
136{
137 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
138 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
139 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
140 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
141 return round_up(map->value_size, 8) * num_possible_cpus();
142 else if (IS_FD_MAP(map))
143 return sizeof(u32);
144 else
145 return map->value_size;
146}
147
148static void maybe_wait_bpf_programs(struct bpf_map *map)
149{
150 /* Wait for any running BPF programs to complete so that
151 * userspace, when we return to it, knows that all programs
152 * that could be running use the new map value.
153 */
154 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
155 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
156 synchronize_rcu();
157}
158
159static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
160 void *value, __u64 flags)
161{
162 int err;
163
164 /* Need to create a kthread, thus must support schedule */
165 if (bpf_map_is_dev_bound(map)) {
166 return bpf_map_offload_update_elem(map, key, value, flags);
167 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
168 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
169 map->map_type == BPF_MAP_TYPE_SOCKMAP ||
170 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
171 return map->ops->map_update_elem(map, key, value, flags);
172 } 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
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100274static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100275{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100276 /* We really just want to fail instead of triggering OOM killer
277 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
278 * which is used for lower order allocation requests.
279 *
280 * It has been observed that higher order allocation requests done by
281 * vmalloc with __GFP_NORETRY being set might fail due to not trying
282 * to reclaim memory from the page cache, thus we set
283 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100284 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100285
286 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100287 void *area;
288
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100289 if (size >= SIZE_MAX)
290 return NULL;
291
Andrii Nakryikofc970222019-11-17 09:28:04 -0800292 /* kmalloc()'ed memory can't be mmap()'ed */
293 if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100294 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
295 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100296 if (area != NULL)
297 return area;
298 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800299 if (mmapable) {
300 BUG_ON(!PAGE_ALIGNED(size));
301 return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL |
302 __GFP_RETRY_MAYFAIL | flags);
303 }
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100304 return __vmalloc_node_flags_caller(size, numa_node,
305 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
306 flags, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100307}
308
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100309void *bpf_map_area_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800310{
311 return __bpf_map_area_alloc(size, numa_node, false);
312}
313
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100314void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800315{
316 return __bpf_map_area_alloc(size, numa_node, true);
317}
318
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100319void bpf_map_area_free(void *area)
320{
321 kvfree(area);
322}
323
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200324static u32 bpf_map_flags_retain_permanent(u32 flags)
325{
326 /* Some map creation flags are not tied to the map object but
327 * rather to the map fd instead, so they have no meaning upon
328 * map object inspection since multiple file descriptors with
329 * different (access) properties can exist here. Thus, given
330 * this has zero meaning for the map itself, lets clear these
331 * from here.
332 */
333 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
334}
335
Jakub Kicinskibd475642018-01-11 20:29:06 -0800336void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
337{
338 map->map_type = attr->map_type;
339 map->key_size = attr->key_size;
340 map->value_size = attr->value_size;
341 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200342 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800343 map->numa_node = bpf_map_attr_numa_node(attr);
344}
345
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700346static int bpf_charge_memlock(struct user_struct *user, u32 pages)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700347{
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700348 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700349
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700350 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
351 atomic_long_sub(pages, &user->locked_vm);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700352 return -EPERM;
353 }
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700354 return 0;
355}
356
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700357static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
358{
Roman Gushchinb936ca62019-05-29 18:03:58 -0700359 if (user)
360 atomic_long_sub(pages, &user->locked_vm);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700361}
362
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100363int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700364{
Roman Gushchinc85d6912019-05-29 18:03:59 -0700365 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
366 struct user_struct *user;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700367 int ret;
368
Roman Gushchinc85d6912019-05-29 18:03:59 -0700369 if (size >= U32_MAX - PAGE_SIZE)
370 return -E2BIG;
371
372 user = get_current_user();
Roman Gushchinb936ca62019-05-29 18:03:58 -0700373 ret = bpf_charge_memlock(user, pages);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700374 if (ret) {
375 free_uid(user);
376 return ret;
377 }
Roman Gushchinb936ca62019-05-29 18:03:58 -0700378
379 mem->pages = pages;
380 mem->user = user;
381
382 return 0;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700383}
384
Roman Gushchinb936ca62019-05-29 18:03:58 -0700385void bpf_map_charge_finish(struct bpf_map_memory *mem)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700386{
Roman Gushchinb936ca62019-05-29 18:03:58 -0700387 bpf_uncharge_memlock(mem->user, mem->pages);
388 free_uid(mem->user);
389}
Roman Gushchin3539b962019-05-29 18:03:57 -0700390
Roman Gushchinb936ca62019-05-29 18:03:58 -0700391void bpf_map_charge_move(struct bpf_map_memory *dst,
392 struct bpf_map_memory *src)
393{
394 *dst = *src;
395
396 /* Make sure src will not be used for the redundant uncharging. */
397 memset(src, 0, sizeof(struct bpf_map_memory));
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700398}
399
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700400int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
401{
402 int ret;
403
Roman Gushchin3539b962019-05-29 18:03:57 -0700404 ret = bpf_charge_memlock(map->memory.user, pages);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700405 if (ret)
406 return ret;
Roman Gushchin3539b962019-05-29 18:03:57 -0700407 map->memory.pages += pages;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700408 return ret;
409}
410
411void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
412{
Roman Gushchin3539b962019-05-29 18:03:57 -0700413 bpf_uncharge_memlock(map->memory.user, pages);
414 map->memory.pages -= pages;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700415}
416
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700417static int bpf_map_alloc_id(struct bpf_map *map)
418{
419 int id;
420
Shaohua Lib76354c2018-03-27 11:53:21 -0700421 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700422 spin_lock_bh(&map_idr_lock);
423 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
424 if (id > 0)
425 map->id = id;
426 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700427 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700428
429 if (WARN_ON_ONCE(!id))
430 return -ENOSPC;
431
432 return id > 0 ? 0 : id;
433}
434
Jakub Kicinskia3884572018-01-11 20:29:09 -0800435void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700436{
Eric Dumazet930651a2017-09-19 09:15:59 -0700437 unsigned long flags;
438
Jakub Kicinskia3884572018-01-11 20:29:09 -0800439 /* Offloaded maps are removed from the IDR store when their device
440 * disappears - even if someone holds an fd to them they are unusable,
441 * the memory is gone, all ops will fail; they are simply waiting for
442 * refcnt to drop to be freed.
443 */
444 if (!map->id)
445 return;
446
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700447 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700448 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700449 else
450 __acquire(&map_idr_lock);
451
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700452 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800453 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700454
455 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700456 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700457 else
458 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700459}
460
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700461/* called from workqueue */
462static void bpf_map_free_deferred(struct work_struct *work)
463{
464 struct bpf_map *map = container_of(work, struct bpf_map, work);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700465 struct bpf_map_memory mem;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700466
Roman Gushchinb936ca62019-05-29 18:03:58 -0700467 bpf_map_charge_move(&mem, &map->memory);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700468 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700469 /* implementation dependent freeing */
470 map->ops->map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700471 bpf_map_charge_finish(&mem);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700472}
473
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100474static void bpf_map_put_uref(struct bpf_map *map)
475{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800476 if (atomic64_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700477 if (map->ops->map_release_uref)
478 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100479 }
480}
481
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700482/* decrement map refcnt and schedule it for freeing via workqueue
483 * (unrelying map implementation ops->map_free() might sleep)
484 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700485static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700486{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800487 if (atomic64_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700488 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700489 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700490 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700491 INIT_WORK(&map->work, bpf_map_free_deferred);
492 schedule_work(&map->work);
493 }
494}
495
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700496void bpf_map_put(struct bpf_map *map)
497{
498 __bpf_map_put(map, true);
499}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700500EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700501
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100502void bpf_map_put_with_uref(struct bpf_map *map)
503{
504 bpf_map_put_uref(map);
505 bpf_map_put(map);
506}
507
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700508static int bpf_map_release(struct inode *inode, struct file *filp)
509{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200510 struct bpf_map *map = filp->private_data;
511
512 if (map->ops->map_release)
513 map->ops->map_release(map, filp);
514
515 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700516 return 0;
517}
518
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200519static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
520{
521 fmode_t mode = f.file->f_mode;
522
523 /* Our file permissions may have been overridden by global
524 * map permissions facing syscall side.
525 */
526 if (READ_ONCE(map->frozen))
527 mode &= ~FMODE_CAN_WRITE;
528 return mode;
529}
530
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100531#ifdef CONFIG_PROC_FS
532static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
533{
534 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100535 const struct bpf_array *array;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100536 u32 type = 0, jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100537
538 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
539 array = container_of(map, struct bpf_array, map);
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100540 type = array->aux->type;
541 jited = array->aux->jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100542 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100543
544 seq_printf(m,
545 "map_type:\t%u\n"
546 "key_size:\t%u\n"
547 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100548 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100549 "map_flags:\t%#x\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +0200550 "memlock:\t%llu\n"
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200551 "map_id:\t%u\n"
552 "frozen:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100553 map->map_type,
554 map->key_size,
555 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100556 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100557 map->map_flags,
Roman Gushchin3539b962019-05-29 18:03:57 -0700558 map->memory.pages * 1ULL << PAGE_SHIFT,
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200559 map->id,
560 READ_ONCE(map->frozen));
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100561 if (type) {
562 seq_printf(m, "owner_prog_type:\t%u\n", type);
563 seq_printf(m, "owner_jited:\t%u\n", jited);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200564 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100565}
566#endif
567
Chenbo Feng6e71b042017-10-18 13:00:22 -0700568static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
569 loff_t *ppos)
570{
571 /* We need this handler such that alloc_file() enables
572 * f_mode with FMODE_CAN_READ.
573 */
574 return -EINVAL;
575}
576
577static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
578 size_t siz, loff_t *ppos)
579{
580 /* We need this handler such that alloc_file() enables
581 * f_mode with FMODE_CAN_WRITE.
582 */
583 return -EINVAL;
584}
585
Andrii Nakryikofc970222019-11-17 09:28:04 -0800586/* called for any extra memory-mapped regions (except initial) */
587static void bpf_map_mmap_open(struct vm_area_struct *vma)
588{
589 struct bpf_map *map = vma->vm_file->private_data;
590
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700591 if (vma->vm_flags & VM_MAYWRITE) {
Andrii Nakryikofc970222019-11-17 09:28:04 -0800592 mutex_lock(&map->freeze_mutex);
593 map->writecnt++;
594 mutex_unlock(&map->freeze_mutex);
595 }
596}
597
598/* called for all unmapped memory region (including initial) */
599static void bpf_map_mmap_close(struct vm_area_struct *vma)
600{
601 struct bpf_map *map = vma->vm_file->private_data;
602
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700603 if (vma->vm_flags & VM_MAYWRITE) {
Andrii Nakryikofc970222019-11-17 09:28:04 -0800604 mutex_lock(&map->freeze_mutex);
605 map->writecnt--;
606 mutex_unlock(&map->freeze_mutex);
607 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800608}
609
610static const struct vm_operations_struct bpf_map_default_vmops = {
611 .open = bpf_map_mmap_open,
612 .close = bpf_map_mmap_close,
613};
614
615static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
616{
617 struct bpf_map *map = filp->private_data;
618 int err;
619
620 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
621 return -ENOTSUPP;
622
623 if (!(vma->vm_flags & VM_SHARED))
624 return -EINVAL;
625
626 mutex_lock(&map->freeze_mutex);
627
628 if ((vma->vm_flags & VM_WRITE) && map->frozen) {
629 err = -EPERM;
630 goto out;
631 }
632
633 /* set default open/close callbacks */
634 vma->vm_ops = &bpf_map_default_vmops;
635 vma->vm_private_data = map;
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700636 vma->vm_flags &= ~VM_MAYEXEC;
637 if (!(vma->vm_flags & VM_WRITE))
638 /* disallow re-mapping with PROT_WRITE */
639 vma->vm_flags &= ~VM_MAYWRITE;
Andrii Nakryikofc970222019-11-17 09:28:04 -0800640
641 err = map->ops->map_mmap(map, vma);
642 if (err)
643 goto out;
644
Andrii Nakryiko1f6cb192020-04-10 13:26:12 -0700645 if (vma->vm_flags & VM_MAYWRITE)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800646 map->writecnt++;
647out:
648 mutex_unlock(&map->freeze_mutex);
649 return err;
650}
651
Chenbo Fengf66e4482017-10-18 13:00:26 -0700652const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100653#ifdef CONFIG_PROC_FS
654 .show_fdinfo = bpf_map_show_fdinfo,
655#endif
656 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700657 .read = bpf_dummy_read,
658 .write = bpf_dummy_write,
Andrii Nakryikofc970222019-11-17 09:28:04 -0800659 .mmap = bpf_map_mmap,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700660};
661
Chenbo Feng6e71b042017-10-18 13:00:22 -0700662int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100663{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700664 int ret;
665
666 ret = security_bpf_map(map, OPEN_FMODE(flags));
667 if (ret < 0)
668 return ret;
669
Daniel Borkmannaa797812015-10-29 14:58:06 +0100670 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700671 flags | O_CLOEXEC);
672}
673
674int bpf_get_file_flag(int flags)
675{
676 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
677 return -EINVAL;
678 if (flags & BPF_F_RDONLY)
679 return O_RDONLY;
680 if (flags & BPF_F_WRONLY)
681 return O_WRONLY;
682 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100683}
684
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700685/* helper macro to check that unused fields 'union bpf_attr' are zero */
686#define CHECK_ATTR(CMD) \
687 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
688 sizeof(attr->CMD##_LAST_FIELD), 0, \
689 sizeof(*attr) - \
690 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
691 sizeof(attr->CMD##_LAST_FIELD)) != NULL
692
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700693/* dst and src must have at least "size" number of bytes.
694 * Return strlen on success and < 0 on error.
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700695 */
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700696int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700697{
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700698 const char *end = src + size;
699 const char *orig_src = src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700700
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700701 memset(dst, 0, size);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200702 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700703 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200704 if (!isalnum(*src) &&
705 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700706 return -EINVAL;
707 *dst++ = *src++;
708 }
709
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700710 /* No '\0' found in "size" number of bytes */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700711 if (src == end)
712 return -EINVAL;
713
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700714 return src - orig_src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700715}
716
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200717int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800718 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200719 const struct btf_type *key_type,
720 const struct btf_type *value_type)
721{
722 return -ENOTSUPP;
723}
724
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800725static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200726 u32 btf_key_id, u32 btf_value_id)
727{
728 const struct btf_type *key_type, *value_type;
729 u32 key_size, value_size;
730 int ret = 0;
731
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200732 /* Some maps allow key to be unspecified. */
733 if (btf_key_id) {
734 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
735 if (!key_type || key_size != map->key_size)
736 return -EINVAL;
737 } else {
738 key_type = btf_type_by_id(btf, 0);
739 if (!map->ops->map_check_btf)
740 return -EINVAL;
741 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200742
743 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
744 if (!value_type || value_size != map->value_size)
745 return -EINVAL;
746
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800747 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
748
749 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200750 if (map->map_flags & BPF_F_RDONLY_PROG)
751 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800752 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -0800753 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700754 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
755 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800756 return -ENOTSUPP;
757 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
758 map->value_size) {
759 WARN_ONCE(1,
760 "verifier bug spin_lock_off %d value_size %d\n",
761 map->spin_lock_off, map->value_size);
762 return -EFAULT;
763 }
764 }
765
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200766 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800767 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200768
769 return ret;
770}
771
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800772#define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700773/* called via syscall */
774static int map_create(union bpf_attr *attr)
775{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700776 int numa_node = bpf_map_attr_numa_node(attr);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700777 struct bpf_map_memory mem;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700778 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700779 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700780 int err;
781
782 err = CHECK_ATTR(BPF_MAP_CREATE);
783 if (err)
784 return -EINVAL;
785
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800786 if (attr->btf_vmlinux_value_type_id) {
787 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
788 attr->btf_key_type_id || attr->btf_value_type_id)
789 return -EINVAL;
790 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
791 return -EINVAL;
792 }
793
Chenbo Feng6e71b042017-10-18 13:00:22 -0700794 f_flags = bpf_get_file_flag(attr->map_flags);
795 if (f_flags < 0)
796 return f_flags;
797
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700798 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700799 ((unsigned int)numa_node >= nr_node_ids ||
800 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700801 return -EINVAL;
802
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700803 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
804 map = find_and_alloc_map(attr);
805 if (IS_ERR(map))
806 return PTR_ERR(map);
807
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700808 err = bpf_obj_name_cpy(map->name, attr->map_name,
809 sizeof(attr->map_name));
810 if (err < 0)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700811 goto free_map;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700812
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800813 atomic64_set(&map->refcnt, 1);
814 atomic64_set(&map->usercnt, 1);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800815 mutex_init(&map->freeze_mutex);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700816
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800817 map->spin_lock_off = -EINVAL;
818 if (attr->btf_key_type_id || attr->btf_value_type_id ||
819 /* Even the map's value is a kernel's struct,
820 * the bpf_prog.o must have BTF to begin with
821 * to figure out the corresponding kernel's
822 * counter part. Thus, attr->btf_fd has
823 * to be valid also.
824 */
825 attr->btf_vmlinux_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700826 struct btf *btf;
827
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700828 btf = btf_get_by_fd(attr->btf_fd);
829 if (IS_ERR(btf)) {
830 err = PTR_ERR(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700831 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700832 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800833 map->btf = btf;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700834
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800835 if (attr->btf_value_type_id) {
836 err = map_check_btf(map, btf, attr->btf_key_type_id,
837 attr->btf_value_type_id);
838 if (err)
839 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700840 }
841
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700842 map->btf_key_type_id = attr->btf_key_type_id;
843 map->btf_value_type_id = attr->btf_value_type_id;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800844 map->btf_vmlinux_value_type_id =
845 attr->btf_vmlinux_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700846 }
847
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700848 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700849 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700850 goto free_map;
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700851
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700852 err = bpf_map_alloc_id(map);
853 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700854 goto free_map_sec;
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700855
Chenbo Feng6e71b042017-10-18 13:00:22 -0700856 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700857 if (err < 0) {
858 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800859 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700860 * bpf_map_alloc_id() has published the map
861 * to the userspace and the userspace may
862 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
863 */
Peng Sun352d20d2019-02-27 22:36:25 +0800864 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700865 return err;
866 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700867
868 return err;
869
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700870free_map_sec:
871 security_bpf_map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700872free_map:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700873 btf_put(map->btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700874 bpf_map_charge_move(&mem, &map->memory);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700875 map->ops->map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700876 bpf_map_charge_finish(&mem);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700877 return err;
878}
879
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700880/* if error is returned, fd is released.
881 * On success caller should complete fd access with matching fdput()
882 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100883struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700884{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700885 if (!f.file)
886 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700887 if (f.file->f_op != &bpf_map_fops) {
888 fdput(f);
889 return ERR_PTR(-EINVAL);
890 }
891
Daniel Borkmannc2101292015-10-29 14:58:07 +0100892 return f.file->private_data;
893}
894
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800895void bpf_map_inc(struct bpf_map *map)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100896{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800897 atomic64_inc(&map->refcnt);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100898}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700899EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100900
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800901void bpf_map_inc_with_uref(struct bpf_map *map)
902{
903 atomic64_inc(&map->refcnt);
904 atomic64_inc(&map->usercnt);
905}
906EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
907
Martin KaFai Lau1ed4d922020-02-25 15:04:21 -0800908struct bpf_map *bpf_map_get(u32 ufd)
909{
910 struct fd f = fdget(ufd);
911 struct bpf_map *map;
912
913 map = __bpf_map_get(f);
914 if (IS_ERR(map))
915 return map;
916
917 bpf_map_inc(map);
918 fdput(f);
919
920 return map;
921}
922
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100923struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100924{
925 struct fd f = fdget(ufd);
926 struct bpf_map *map;
927
928 map = __bpf_map_get(f);
929 if (IS_ERR(map))
930 return map;
931
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800932 bpf_map_inc_with_uref(map);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100933 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700934
935 return map;
936}
937
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700938/* map_idr_lock should have been held */
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800939static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700940{
941 int refold;
942
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800943 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700944 if (!refold)
945 return ERR_PTR(-ENOENT);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700946 if (uref)
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800947 atomic64_inc(&map->usercnt);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700948
949 return map;
950}
951
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800952struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700953{
954 spin_lock_bh(&map_idr_lock);
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800955 map = __bpf_map_inc_not_zero(map, false);
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700956 spin_unlock_bh(&map_idr_lock);
957
958 return map;
959}
960EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
961
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800962int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
963{
964 return -ENOTSUPP;
965}
966
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200967static void *__bpf_copy_key(void __user *ukey, u64 key_size)
968{
969 if (key_size)
970 return memdup_user(ukey, key_size);
971
972 if (ukey)
973 return ERR_PTR(-EINVAL);
974
975 return NULL;
976}
977
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700978/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800979#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700980
981static int map_lookup_elem(union bpf_attr *attr)
982{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100983 void __user *ukey = u64_to_user_ptr(attr->key);
984 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700985 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700986 struct bpf_map *map;
Brian Vazquez15c14a32020-01-15 10:43:00 -0800987 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800988 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200989 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700990 int err;
991
992 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
993 return -EINVAL;
994
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800995 if (attr->flags & ~BPF_F_LOCK)
996 return -EINVAL;
997
Daniel Borkmann592867b2015-09-08 18:00:09 +0200998 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100999 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001000 if (IS_ERR(map))
1001 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001002 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001003 err = -EPERM;
1004 goto err_put;
1005 }
1006
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001007 if ((attr->flags & BPF_F_LOCK) &&
1008 !map_value_has_spin_lock(map)) {
1009 err = -EINVAL;
1010 goto err_put;
1011 }
1012
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001013 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001014 if (IS_ERR(key)) {
1015 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001016 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001017 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001018
Brian Vazquez15c14a32020-01-15 10:43:00 -08001019 value_size = bpf_map_value_size(map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001020
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001021 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001022 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001023 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001024 goto free_key;
1025
Brian Vazquez15c14a32020-01-15 10:43:00 -08001026 err = bpf_map_copy_value(map, key, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001027 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001028 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001029
1030 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001031 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001032 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001033
1034 err = 0;
1035
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001036free_value:
1037 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001038free_key:
1039 kfree(key);
1040err_put:
1041 fdput(f);
1042 return err;
1043}
1044
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001045
Alexei Starovoitov3274f522014-11-13 17:36:44 -08001046#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001047
1048static int map_update_elem(union bpf_attr *attr)
1049{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001050 void __user *ukey = u64_to_user_ptr(attr->key);
1051 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001052 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001053 struct bpf_map *map;
1054 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001055 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001056 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001057 int err;
1058
1059 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1060 return -EINVAL;
1061
Daniel Borkmann592867b2015-09-08 18:00:09 +02001062 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001063 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001064 if (IS_ERR(map))
1065 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001066 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001067 err = -EPERM;
1068 goto err_put;
1069 }
1070
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001071 if ((attr->flags & BPF_F_LOCK) &&
1072 !map_value_has_spin_lock(map)) {
1073 err = -EINVAL;
1074 goto err_put;
1075 }
1076
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001077 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001078 if (IS_ERR(key)) {
1079 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001080 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001081 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001082
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001083 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001084 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +00001085 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1086 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001087 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1088 else
1089 value_size = map->value_size;
1090
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001091 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001092 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001093 if (!value)
1094 goto free_key;
1095
1096 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001097 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001098 goto free_value;
1099
Brian Vazquez15c14a32020-01-15 10:43:00 -08001100 err = bpf_map_update_value(map, f, key, value, attr->flags);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001101
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001102free_value:
1103 kfree(value);
1104free_key:
1105 kfree(key);
1106err_put:
1107 fdput(f);
1108 return err;
1109}
1110
1111#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1112
1113static int map_delete_elem(union bpf_attr *attr)
1114{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001115 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001116 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001117 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001118 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001119 void *key;
1120 int err;
1121
1122 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1123 return -EINVAL;
1124
Daniel Borkmann592867b2015-09-08 18:00:09 +02001125 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001126 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001127 if (IS_ERR(map))
1128 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001129 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001130 err = -EPERM;
1131 goto err_put;
1132 }
1133
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001134 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001135 if (IS_ERR(key)) {
1136 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001137 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001138 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001139
Jakub Kicinskia3884572018-01-11 20:29:09 -08001140 if (bpf_map_is_dev_bound(map)) {
1141 err = bpf_map_offload_delete_elem(map, key);
1142 goto out;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001143 } else if (IS_FD_PROG_ARRAY(map) ||
1144 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1145 /* These maps require sleepable context */
Daniel Borkmannda765a22019-11-22 21:07:58 +01001146 err = map->ops->map_delete_elem(map, key);
1147 goto out;
Jakub Kicinskia3884572018-01-11 20:29:09 -08001148 }
1149
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001150 bpf_disable_instrumentation();
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001151 rcu_read_lock();
1152 err = map->ops->map_delete_elem(map, key);
1153 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001154 bpf_enable_instrumentation();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001155 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001156out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001157 kfree(key);
1158err_put:
1159 fdput(f);
1160 return err;
1161}
1162
1163/* last field in 'union bpf_attr' used by this command */
1164#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1165
1166static int map_get_next_key(union bpf_attr *attr)
1167{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001168 void __user *ukey = u64_to_user_ptr(attr->key);
1169 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001170 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001171 struct bpf_map *map;
1172 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001173 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001174 int err;
1175
1176 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1177 return -EINVAL;
1178
Daniel Borkmann592867b2015-09-08 18:00:09 +02001179 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001180 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001181 if (IS_ERR(map))
1182 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001183 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001184 err = -EPERM;
1185 goto err_put;
1186 }
1187
Teng Qin8fe45922017-04-24 19:00:37 -07001188 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001189 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001190 if (IS_ERR(key)) {
1191 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001192 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001193 }
Teng Qin8fe45922017-04-24 19:00:37 -07001194 } else {
1195 key = NULL;
1196 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001197
1198 err = -ENOMEM;
1199 next_key = kmalloc(map->key_size, GFP_USER);
1200 if (!next_key)
1201 goto free_key;
1202
Jakub Kicinskia3884572018-01-11 20:29:09 -08001203 if (bpf_map_is_dev_bound(map)) {
1204 err = bpf_map_offload_get_next_key(map, key, next_key);
1205 goto out;
1206 }
1207
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001208 rcu_read_lock();
1209 err = map->ops->map_get_next_key(map, key, next_key);
1210 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001211out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001212 if (err)
1213 goto free_next_key;
1214
1215 err = -EFAULT;
1216 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1217 goto free_next_key;
1218
1219 err = 0;
1220
1221free_next_key:
1222 kfree(next_key);
1223free_key:
1224 kfree(key);
1225err_put:
1226 fdput(f);
1227 return err;
1228}
1229
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001230int generic_map_delete_batch(struct bpf_map *map,
1231 const union bpf_attr *attr,
1232 union bpf_attr __user *uattr)
1233{
1234 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1235 u32 cp, max_count;
1236 int err = 0;
1237 void *key;
1238
1239 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1240 return -EINVAL;
1241
1242 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1243 !map_value_has_spin_lock(map)) {
1244 return -EINVAL;
1245 }
1246
1247 max_count = attr->batch.count;
1248 if (!max_count)
1249 return 0;
1250
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001251 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1252 if (!key)
1253 return -ENOMEM;
1254
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001255 for (cp = 0; cp < max_count; cp++) {
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001256 err = -EFAULT;
1257 if (copy_from_user(key, keys + cp * map->key_size,
1258 map->key_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001259 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001260
1261 if (bpf_map_is_dev_bound(map)) {
1262 err = bpf_map_offload_delete_elem(map, key);
1263 break;
1264 }
1265
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001266 bpf_disable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001267 rcu_read_lock();
1268 err = map->ops->map_delete_elem(map, key);
1269 rcu_read_unlock();
Thomas Gleixnerb6e5dae2020-02-24 15:01:49 +01001270 bpf_enable_instrumentation();
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001271 maybe_wait_bpf_programs(map);
1272 if (err)
1273 break;
1274 }
1275 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1276 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001277
1278 kfree(key);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001279 return err;
1280}
1281
1282int generic_map_update_batch(struct bpf_map *map,
1283 const union bpf_attr *attr,
1284 union bpf_attr __user *uattr)
1285{
1286 void __user *values = u64_to_user_ptr(attr->batch.values);
1287 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1288 u32 value_size, cp, max_count;
1289 int ufd = attr->map_fd;
1290 void *key, *value;
1291 struct fd f;
1292 int err = 0;
1293
1294 f = fdget(ufd);
1295 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1296 return -EINVAL;
1297
1298 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1299 !map_value_has_spin_lock(map)) {
1300 return -EINVAL;
1301 }
1302
1303 value_size = bpf_map_value_size(map);
1304
1305 max_count = attr->batch.count;
1306 if (!max_count)
1307 return 0;
1308
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001309 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1310 if (!key)
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001311 return -ENOMEM;
1312
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001313 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1314 if (!value) {
1315 kfree(key);
1316 return -ENOMEM;
1317 }
1318
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001319 for (cp = 0; cp < max_count; cp++) {
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001320 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001321 if (copy_from_user(key, keys + cp * map->key_size,
1322 map->key_size) ||
1323 copy_from_user(value, values + cp * value_size, value_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001324 break;
1325
1326 err = bpf_map_update_value(map, f, key, value,
1327 attr->batch.elem_flags);
1328
1329 if (err)
1330 break;
1331 }
1332
1333 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1334 err = -EFAULT;
1335
1336 kfree(value);
1337 kfree(key);
1338 return err;
1339}
1340
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001341#define MAP_LOOKUP_RETRIES 3
1342
1343int generic_map_lookup_batch(struct bpf_map *map,
1344 const union bpf_attr *attr,
1345 union bpf_attr __user *uattr)
1346{
1347 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1348 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1349 void __user *values = u64_to_user_ptr(attr->batch.values);
1350 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1351 void *buf, *buf_prevkey, *prev_key, *key, *value;
1352 int err, retry = MAP_LOOKUP_RETRIES;
1353 u32 value_size, cp, max_count;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001354
1355 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1356 return -EINVAL;
1357
1358 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1359 !map_value_has_spin_lock(map))
1360 return -EINVAL;
1361
1362 value_size = bpf_map_value_size(map);
1363
1364 max_count = attr->batch.count;
1365 if (!max_count)
1366 return 0;
1367
1368 if (put_user(0, &uattr->batch.count))
1369 return -EFAULT;
1370
1371 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1372 if (!buf_prevkey)
1373 return -ENOMEM;
1374
1375 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1376 if (!buf) {
1377 kvfree(buf_prevkey);
1378 return -ENOMEM;
1379 }
1380
1381 err = -EFAULT;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001382 prev_key = NULL;
1383 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1384 goto free_buf;
1385 key = buf;
1386 value = key + map->key_size;
1387 if (ubatch)
1388 prev_key = buf_prevkey;
1389
1390 for (cp = 0; cp < max_count;) {
1391 rcu_read_lock();
1392 err = map->ops->map_get_next_key(map, prev_key, key);
1393 rcu_read_unlock();
1394 if (err)
1395 break;
1396 err = bpf_map_copy_value(map, key, value,
1397 attr->batch.elem_flags);
1398
1399 if (err == -ENOENT) {
1400 if (retry) {
1401 retry--;
1402 continue;
1403 }
1404 err = -EINTR;
1405 break;
1406 }
1407
1408 if (err)
1409 goto free_buf;
1410
1411 if (copy_to_user(keys + cp * map->key_size, key,
1412 map->key_size)) {
1413 err = -EFAULT;
1414 goto free_buf;
1415 }
1416 if (copy_to_user(values + cp * value_size, value, value_size)) {
1417 err = -EFAULT;
1418 goto free_buf;
1419 }
1420
1421 if (!prev_key)
1422 prev_key = buf_prevkey;
1423
1424 swap(prev_key, key);
1425 retry = MAP_LOOKUP_RETRIES;
1426 cp++;
1427 }
1428
1429 if (err == -EFAULT)
1430 goto free_buf;
1431
1432 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1433 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1434 err = -EFAULT;
1435
1436free_buf:
1437 kfree(buf_prevkey);
1438 kfree(buf);
1439 return err;
1440}
1441
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001442#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1443
1444static int map_lookup_and_delete_elem(union bpf_attr *attr)
1445{
1446 void __user *ukey = u64_to_user_ptr(attr->key);
1447 void __user *uvalue = u64_to_user_ptr(attr->value);
1448 int ufd = attr->map_fd;
1449 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001450 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001451 u32 value_size;
1452 struct fd f;
1453 int err;
1454
1455 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1456 return -EINVAL;
1457
1458 f = fdget(ufd);
1459 map = __bpf_map_get(f);
1460 if (IS_ERR(map))
1461 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001462 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001463 err = -EPERM;
1464 goto err_put;
1465 }
1466
1467 key = __bpf_copy_key(ukey, map->key_size);
1468 if (IS_ERR(key)) {
1469 err = PTR_ERR(key);
1470 goto err_put;
1471 }
1472
1473 value_size = map->value_size;
1474
1475 err = -ENOMEM;
1476 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1477 if (!value)
1478 goto free_key;
1479
1480 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1481 map->map_type == BPF_MAP_TYPE_STACK) {
1482 err = map->ops->map_pop_elem(map, value);
1483 } else {
1484 err = -ENOTSUPP;
1485 }
1486
1487 if (err)
1488 goto free_value;
1489
1490 if (copy_to_user(uvalue, value, value_size) != 0)
1491 goto free_value;
1492
1493 err = 0;
1494
1495free_value:
1496 kfree(value);
1497free_key:
1498 kfree(key);
1499err_put:
1500 fdput(f);
1501 return err;
1502}
1503
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001504#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1505
1506static int map_freeze(const union bpf_attr *attr)
1507{
1508 int err = 0, ufd = attr->map_fd;
1509 struct bpf_map *map;
1510 struct fd f;
1511
1512 if (CHECK_ATTR(BPF_MAP_FREEZE))
1513 return -EINVAL;
1514
1515 f = fdget(ufd);
1516 map = __bpf_map_get(f);
1517 if (IS_ERR(map))
1518 return PTR_ERR(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -08001519
Martin KaFai Lau849b4d92020-03-04 17:34:54 -08001520 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1521 fdput(f);
1522 return -ENOTSUPP;
1523 }
1524
Andrii Nakryikofc970222019-11-17 09:28:04 -08001525 mutex_lock(&map->freeze_mutex);
1526
1527 if (map->writecnt) {
1528 err = -EBUSY;
1529 goto err_put;
1530 }
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001531 if (READ_ONCE(map->frozen)) {
1532 err = -EBUSY;
1533 goto err_put;
1534 }
1535 if (!capable(CAP_SYS_ADMIN)) {
1536 err = -EPERM;
1537 goto err_put;
1538 }
1539
1540 WRITE_ONCE(map->frozen, true);
1541err_put:
Andrii Nakryikofc970222019-11-17 09:28:04 -08001542 mutex_unlock(&map->freeze_mutex);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001543 fdput(f);
1544 return err;
1545}
1546
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001547static const struct bpf_prog_ops * const bpf_prog_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001548#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001549 [_id] = & _name ## _prog_ops,
1550#define BPF_MAP_TYPE(_id, _ops)
1551#include <linux/bpf_types.h>
1552#undef BPF_PROG_TYPE
1553#undef BPF_MAP_TYPE
1554};
1555
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001556static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1557{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001558 const struct bpf_prog_ops *ops;
1559
1560 if (type >= ARRAY_SIZE(bpf_prog_types))
1561 return -EINVAL;
1562 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1563 ops = bpf_prog_types[type];
1564 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001565 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001566
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001567 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001568 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001569 else
1570 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001571 prog->type = type;
1572 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001573}
1574
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001575enum bpf_audit {
1576 BPF_AUDIT_LOAD,
1577 BPF_AUDIT_UNLOAD,
1578 BPF_AUDIT_MAX,
1579};
1580
1581static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1582 [BPF_AUDIT_LOAD] = "LOAD",
1583 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1584};
1585
1586static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1587{
1588 struct audit_context *ctx = NULL;
1589 struct audit_buffer *ab;
1590
1591 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1592 return;
1593 if (audit_enabled == AUDIT_OFF)
1594 return;
1595 if (op == BPF_AUDIT_LOAD)
1596 ctx = audit_context();
1597 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1598 if (unlikely(!ab))
1599 return;
1600 audit_log_format(ab, "prog-id=%u op=%s",
1601 prog->aux->id, bpf_audit_str[op]);
1602 audit_log_end(ab);
1603}
1604
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001605int __bpf_prog_charge(struct user_struct *user, u32 pages)
1606{
1607 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1608 unsigned long user_bufs;
1609
1610 if (user) {
1611 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1612 if (user_bufs > memlock_limit) {
1613 atomic_long_sub(pages, &user->locked_vm);
1614 return -EPERM;
1615 }
1616 }
1617
1618 return 0;
1619}
1620
1621void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1622{
1623 if (user)
1624 atomic_long_sub(pages, &user->locked_vm);
1625}
1626
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001627static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1628{
1629 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001630 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001631
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001632 ret = __bpf_prog_charge(user, prog->pages);
1633 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001634 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001635 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001636 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001637
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001638 prog->aux->user = user;
1639 return 0;
1640}
1641
1642static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1643{
1644 struct user_struct *user = prog->aux->user;
1645
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001646 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001647 free_uid(user);
1648}
1649
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001650static int bpf_prog_alloc_id(struct bpf_prog *prog)
1651{
1652 int id;
1653
Shaohua Lib76354c2018-03-27 11:53:21 -07001654 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001655 spin_lock_bh(&prog_idr_lock);
1656 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1657 if (id > 0)
1658 prog->aux->id = id;
1659 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001660 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001661
1662 /* id is in [1, INT_MAX) */
1663 if (WARN_ON_ONCE(!id))
1664 return -ENOSPC;
1665
1666 return id > 0 ? 0 : id;
1667}
1668
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001669void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001670{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001671 /* cBPF to eBPF migrations are currently not in the idr store.
1672 * Offloaded programs are removed from the store when their device
1673 * disappears - even if someone grabs an fd to them they are unusable,
1674 * simply waiting for refcnt to drop to be freed.
1675 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001676 if (!prog->aux->id)
1677 return;
1678
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001679 if (do_idr_lock)
1680 spin_lock_bh(&prog_idr_lock);
1681 else
1682 __acquire(&prog_idr_lock);
1683
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001684 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001685 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001686
1687 if (do_idr_lock)
1688 spin_unlock_bh(&prog_idr_lock);
1689 else
1690 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001691}
1692
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001693static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001694{
1695 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1696
Daniel Borkmann3b4d9eb2019-10-22 23:30:38 +02001697 kvfree(aux->func_info);
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08001698 kfree(aux->func_info_aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001699 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001700 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001701 bpf_prog_free(aux->prog);
1702}
1703
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001704static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1705{
1706 bpf_prog_kallsyms_del_all(prog);
1707 btf_put(prog->aux->btf);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001708 bpf_prog_free_linfo(prog);
1709
1710 if (deferred)
1711 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1712 else
1713 __bpf_prog_put_rcu(&prog->aux->rcu);
1714}
1715
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001716static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001717{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001718 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001719 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001720 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001721 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001722 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001723 __bpf_prog_put_noref(prog, true);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001724 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001725}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001726
1727void bpf_prog_put(struct bpf_prog *prog)
1728{
1729 __bpf_prog_put(prog, true);
1730}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001731EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001732
1733static int bpf_prog_release(struct inode *inode, struct file *filp)
1734{
1735 struct bpf_prog *prog = filp->private_data;
1736
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001737 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001738 return 0;
1739}
1740
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001741static void bpf_prog_get_stats(const struct bpf_prog *prog,
1742 struct bpf_prog_stats *stats)
1743{
1744 u64 nsecs = 0, cnt = 0;
1745 int cpu;
1746
1747 for_each_possible_cpu(cpu) {
1748 const struct bpf_prog_stats *st;
1749 unsigned int start;
1750 u64 tnsecs, tcnt;
1751
1752 st = per_cpu_ptr(prog->aux->stats, cpu);
1753 do {
1754 start = u64_stats_fetch_begin_irq(&st->syncp);
1755 tnsecs = st->nsecs;
1756 tcnt = st->cnt;
1757 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1758 nsecs += tnsecs;
1759 cnt += tcnt;
1760 }
1761 stats->nsecs = nsecs;
1762 stats->cnt = cnt;
1763}
1764
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001765#ifdef CONFIG_PROC_FS
1766static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1767{
1768 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001769 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001770 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001771
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001772 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001773 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001774 seq_printf(m,
1775 "prog_type:\t%u\n"
1776 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001777 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001778 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001779 "prog_id:\t%u\n"
1780 "run_time_ns:\t%llu\n"
1781 "run_cnt:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001782 prog->type,
1783 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001784 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001785 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001786 prog->aux->id,
1787 stats.nsecs,
1788 stats.cnt);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001789}
1790#endif
1791
Chenbo Fengf66e4482017-10-18 13:00:26 -07001792const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001793#ifdef CONFIG_PROC_FS
1794 .show_fdinfo = bpf_prog_show_fdinfo,
1795#endif
1796 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001797 .read = bpf_dummy_read,
1798 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001799};
1800
Daniel Borkmannb2197752015-10-29 14:58:09 +01001801int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001802{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001803 int ret;
1804
1805 ret = security_bpf_prog(prog);
1806 if (ret < 0)
1807 return ret;
1808
Daniel Borkmannaa797812015-10-29 14:58:06 +01001809 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1810 O_RDWR | O_CLOEXEC);
1811}
1812
Daniel Borkmann113214b2016-06-30 17:24:44 +02001813static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001814{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001815 if (!f.file)
1816 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001817 if (f.file->f_op != &bpf_prog_fops) {
1818 fdput(f);
1819 return ERR_PTR(-EINVAL);
1820 }
1821
Daniel Borkmannc2101292015-10-29 14:58:07 +01001822 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001823}
1824
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001825void bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001826{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001827 atomic64_add(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001828}
Brenden Blanco59d36562016-07-19 12:16:46 -07001829EXPORT_SYMBOL_GPL(bpf_prog_add);
1830
Daniel Borkmannc5405942016-11-09 22:02:34 +01001831void bpf_prog_sub(struct bpf_prog *prog, int i)
1832{
1833 /* Only to be used for undoing previous bpf_prog_add() in some
1834 * error path. We still know that another entity in our call
1835 * path holds a reference to the program, thus atomic_sub() can
1836 * be safely used in such cases!
1837 */
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001838 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
Daniel Borkmannc5405942016-11-09 22:02:34 +01001839}
1840EXPORT_SYMBOL_GPL(bpf_prog_sub);
1841
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001842void bpf_prog_inc(struct bpf_prog *prog)
Brenden Blanco59d36562016-07-19 12:16:46 -07001843{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001844 atomic64_inc(&prog->aux->refcnt);
Brenden Blanco59d36562016-07-19 12:16:46 -07001845}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001846EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001847
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001848/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001849struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001850{
1851 int refold;
1852
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001853 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001854
1855 if (!refold)
1856 return ERR_PTR(-ENOENT);
1857
1858 return prog;
1859}
John Fastabenda6f6df62017-08-15 22:32:22 -07001860EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001861
Al Viro040ee692017-12-02 20:20:38 -05001862bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001863 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001864{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001865 /* not an attachment, just a refcount inc, always allow */
1866 if (!attach_type)
1867 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001868
1869 if (prog->type != *attach_type)
1870 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001871 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001872 return false;
1873
1874 return true;
1875}
1876
1877static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001878 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001879{
1880 struct fd f = fdget(ufd);
1881 struct bpf_prog *prog;
1882
Daniel Borkmann113214b2016-06-30 17:24:44 +02001883 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001884 if (IS_ERR(prog))
1885 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001886 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001887 prog = ERR_PTR(-EINVAL);
1888 goto out;
1889 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001890
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001891 bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001892out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001893 fdput(f);
1894 return prog;
1895}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001896
1897struct bpf_prog *bpf_prog_get(u32 ufd)
1898{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001899 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001900}
1901
Jakub Kicinski248f3462017-11-03 13:56:20 -07001902struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001903 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001904{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001905 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001906}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001907EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001908
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001909/* Initially all BPF programs could be loaded w/o specifying
1910 * expected_attach_type. Later for some of them specifying expected_attach_type
1911 * at load time became required so that program could be validated properly.
1912 * Programs of types that are allowed to be loaded both w/ and w/o (for
1913 * backward compatibility) expected_attach_type, should have the default attach
1914 * type assigned to expected_attach_type for the latter case, so that it can be
1915 * validated later at attach time.
1916 *
1917 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1918 * prog type requires it but has some attach types that have to be backward
1919 * compatible.
1920 */
1921static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1922{
1923 switch (attr->prog_type) {
1924 case BPF_PROG_TYPE_CGROUP_SOCK:
1925 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1926 * exist so checking for non-zero is the way to go here.
1927 */
1928 if (!attr->expected_attach_type)
1929 attr->expected_attach_type =
1930 BPF_CGROUP_INET_SOCK_CREATE;
1931 break;
1932 }
1933}
1934
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001935static int
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07001936bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1937 enum bpf_attach_type expected_attach_type,
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08001938 u32 btf_id, u32 prog_fd)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001939{
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001940 if (btf_id) {
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001941 if (btf_id > BTF_MAX_TYPE)
1942 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001943
1944 switch (prog_type) {
1945 case BPF_PROG_TYPE_TRACING:
KP Singh9e4e01d2020-03-29 01:43:52 +01001946 case BPF_PROG_TYPE_LSM:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001947 case BPF_PROG_TYPE_STRUCT_OPS:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001948 case BPF_PROG_TYPE_EXT:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001949 break;
1950 default:
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001951 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001952 }
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001953 }
1954
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001955 if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING &&
1956 prog_type != BPF_PROG_TYPE_EXT)
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001957 return -EINVAL;
1958
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001959 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001960 case BPF_PROG_TYPE_CGROUP_SOCK:
1961 switch (expected_attach_type) {
1962 case BPF_CGROUP_INET_SOCK_CREATE:
1963 case BPF_CGROUP_INET4_POST_BIND:
1964 case BPF_CGROUP_INET6_POST_BIND:
1965 return 0;
1966 default:
1967 return -EINVAL;
1968 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001969 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1970 switch (expected_attach_type) {
1971 case BPF_CGROUP_INET4_BIND:
1972 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001973 case BPF_CGROUP_INET4_CONNECT:
1974 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001975 case BPF_CGROUP_UDP4_SENDMSG:
1976 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02001977 case BPF_CGROUP_UDP4_RECVMSG:
1978 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001979 return 0;
1980 default:
1981 return -EINVAL;
1982 }
brakmo5cf1e912019-05-28 16:59:36 -07001983 case BPF_PROG_TYPE_CGROUP_SKB:
1984 switch (expected_attach_type) {
1985 case BPF_CGROUP_INET_INGRESS:
1986 case BPF_CGROUP_INET_EGRESS:
1987 return 0;
1988 default:
1989 return -EINVAL;
1990 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001991 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1992 switch (expected_attach_type) {
1993 case BPF_CGROUP_SETSOCKOPT:
1994 case BPF_CGROUP_GETSOCKOPT:
1995 return 0;
1996 default:
1997 return -EINVAL;
1998 }
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001999 case BPF_PROG_TYPE_EXT:
2000 if (expected_attach_type)
2001 return -EINVAL;
2002 /* fallthrough */
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002003 default:
2004 return 0;
2005 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002006}
2007
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002008/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002009#define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002010
Yonghong Song838e9692018-11-19 15:29:11 -08002011static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002012{
2013 enum bpf_prog_type type = attr->prog_type;
2014 struct bpf_prog *prog;
2015 int err;
2016 char license[128];
2017 bool is_gpl;
2018
2019 if (CHECK_ATTR(BPF_PROG_LOAD))
2020 return -EINVAL;
2021
Jiong Wangc240eff2019-05-24 23:25:16 +01002022 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2023 BPF_F_ANY_ALIGNMENT |
Alexei Starovoitov10d274e2019-08-22 22:52:12 -07002024 BPF_F_TEST_STATE_FREQ |
Jiong Wangc240eff2019-05-24 23:25:16 +01002025 BPF_F_TEST_RND_HI32))
David S. Millere07b98d2017-05-10 11:38:07 -07002026 return -EINVAL;
2027
David Millere9ee9ef2018-11-30 21:08:14 -08002028 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2029 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2030 !capable(CAP_SYS_ADMIN))
2031 return -EPERM;
2032
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002033 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002034 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002035 sizeof(license) - 1) < 0)
2036 return -EFAULT;
2037 license[sizeof(license) - 1] = 0;
2038
2039 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2040 is_gpl = license_is_gpl_compatible(license);
2041
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07002042 if (attr->insn_cnt == 0 ||
2043 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01002044 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07002045 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2046 type != BPF_PROG_TYPE_CGROUP_SKB &&
2047 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002048 return -EPERM;
2049
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002050 bpf_prog_load_fixup_attach_type(attr);
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002051 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002052 attr->attach_btf_id,
2053 attr->attach_prog_fd))
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002054 return -EINVAL;
2055
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002056 /* plain bpf_prog allocation */
2057 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2058 if (!prog)
2059 return -ENOMEM;
2060
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002061 prog->expected_attach_type = attr->expected_attach_type;
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002062 prog->aux->attach_btf_id = attr->attach_btf_id;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002063 if (attr->attach_prog_fd) {
2064 struct bpf_prog *tgt_prog;
2065
2066 tgt_prog = bpf_prog_get(attr->attach_prog_fd);
2067 if (IS_ERR(tgt_prog)) {
2068 err = PTR_ERR(tgt_prog);
2069 goto free_prog_nouncharge;
2070 }
2071 prog->aux->linked_prog = tgt_prog;
2072 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002073
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002074 prog->aux->offload_requested = !!attr->prog_ifindex;
2075
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002076 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002077 if (err)
2078 goto free_prog_nouncharge;
2079
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002080 err = bpf_prog_charge_memlock(prog);
2081 if (err)
2082 goto free_prog_sec;
2083
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002084 prog->len = attr->insn_cnt;
2085
2086 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002087 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01002088 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002089 goto free_prog;
2090
2091 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002092 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002093
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002094 atomic64_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002095 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002096
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002097 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002098 err = bpf_prog_offload_init(prog, attr);
2099 if (err)
2100 goto free_prog;
2101 }
2102
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002103 /* find program type: socket_filter vs tracing_filter */
2104 err = find_prog_type(type, prog);
2105 if (err < 0)
2106 goto free_prog;
2107
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002108 prog->aux->load_time = ktime_get_boottime_ns();
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -07002109 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2110 sizeof(attr->prog_name));
2111 if (err < 0)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002112 goto free_prog;
2113
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002114 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08002115 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002116 if (err < 0)
2117 goto free_used_maps;
2118
Daniel Borkmann9facc332018-06-15 02:30:48 +02002119 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002120 if (err < 0)
2121 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002122
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07002123 err = bpf_prog_alloc_id(prog);
2124 if (err)
2125 goto free_used_maps;
2126
Daniel Borkmannc7517982019-08-23 22:14:23 +02002127 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2128 * effectively publicly exposed. However, retrieving via
2129 * bpf_prog_get_fd_by_id() will take another reference,
2130 * therefore it cannot be gone underneath us.
2131 *
2132 * Only for the time /after/ successful bpf_prog_new_fd()
2133 * and before returning to userspace, we might just hold
2134 * one reference and any parallel close on that fd could
2135 * rip everything out. Hence, below notifications must
2136 * happen before bpf_prog_new_fd().
2137 *
2138 * Also, any failure handling from this point onwards must
2139 * be using bpf_prog_put() given the program is exposed.
2140 */
Daniel Borkmann74451e662017-02-16 22:24:50 +01002141 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08002142 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01002143 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
Daniel Borkmannc7517982019-08-23 22:14:23 +02002144
2145 err = bpf_prog_new_fd(prog);
2146 if (err < 0)
2147 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002148 return err;
2149
2150free_used_maps:
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002151 /* In case we have subprogs, we need to wait for a grace
2152 * period before we can tear down JIT memory since symbols
2153 * are already exposed under kallsyms.
2154 */
2155 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2156 return err;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002157free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002158 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002159free_prog_sec:
2160 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002161free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002162 bpf_prog_free(prog);
2163 return err;
2164}
2165
Chenbo Feng6e71b042017-10-18 13:00:22 -07002166#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01002167
2168static int bpf_obj_pin(const union bpf_attr *attr)
2169{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002170 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002171 return -EINVAL;
2172
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002173 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01002174}
2175
2176static int bpf_obj_get(const union bpf_attr *attr)
2177{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002178 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2179 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002180 return -EINVAL;
2181
Chenbo Feng6e71b042017-10-18 13:00:22 -07002182 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2183 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01002184}
2185
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002186void bpf_link_init(struct bpf_link *link,
2187 const struct bpf_link_ops *ops, struct bpf_prog *prog)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002188{
2189 atomic64_set(&link->refcnt, 1);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002190 link->id = 0;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002191 link->ops = ops;
2192 link->prog = prog;
2193}
2194
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002195static void bpf_link_free_id(int id)
2196{
2197 if (!id)
2198 return;
2199
2200 spin_lock_bh(&link_idr_lock);
2201 idr_remove(&link_idr, id);
2202 spin_unlock_bh(&link_idr_lock);
2203}
2204
Andrii Nakryiko98868662020-03-12 17:21:28 -07002205/* Clean up bpf_link and corresponding anon_inode file and FD. After
2206 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002207 * anon_inode's release() call. This helper marksbpf_link as
2208 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2209 * is not decremented, it's the responsibility of a calling code that failed
2210 * to complete bpf_link initialization.
Andrii Nakryiko98868662020-03-12 17:21:28 -07002211 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002212void bpf_link_cleanup(struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002213{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002214 primer->link->prog = NULL;
2215 bpf_link_free_id(primer->id);
2216 fput(primer->file);
2217 put_unused_fd(primer->fd);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002218}
2219
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002220void bpf_link_inc(struct bpf_link *link)
2221{
2222 atomic64_inc(&link->refcnt);
2223}
2224
2225/* bpf_link_free is guaranteed to be called from process context */
2226static void bpf_link_free(struct bpf_link *link)
2227{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002228 bpf_link_free_id(link->id);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002229 if (link->prog) {
2230 /* detach BPF program, clean up used resources */
2231 link->ops->release(link);
2232 bpf_prog_put(link->prog);
2233 }
2234 /* free bpf_link and its containing memory */
2235 link->ops->dealloc(link);
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002236}
2237
2238static void bpf_link_put_deferred(struct work_struct *work)
2239{
2240 struct bpf_link *link = container_of(work, struct bpf_link, work);
2241
2242 bpf_link_free(link);
2243}
2244
2245/* bpf_link_put can be called from atomic context, but ensures that resources
2246 * are freed from process context
2247 */
2248void bpf_link_put(struct bpf_link *link)
2249{
2250 if (!atomic64_dec_and_test(&link->refcnt))
2251 return;
2252
2253 if (in_atomic()) {
2254 INIT_WORK(&link->work, bpf_link_put_deferred);
2255 schedule_work(&link->work);
2256 } else {
2257 bpf_link_free(link);
2258 }
2259}
2260
2261static int bpf_link_release(struct inode *inode, struct file *filp)
2262{
2263 struct bpf_link *link = filp->private_data;
2264
2265 bpf_link_put(link);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002266 return 0;
2267}
2268
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002269#ifdef CONFIG_PROC_FS
2270static const struct bpf_link_ops bpf_raw_tp_lops;
2271static const struct bpf_link_ops bpf_tracing_link_lops;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002272
2273static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2274{
2275 const struct bpf_link *link = filp->private_data;
2276 const struct bpf_prog *prog = link->prog;
2277 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
2278 const char *link_type;
2279
2280 if (link->ops == &bpf_raw_tp_lops)
2281 link_type = "raw_tracepoint";
2282 else if (link->ops == &bpf_tracing_link_lops)
2283 link_type = "tracing";
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07002284#ifdef CONFIG_CGROUP_BPF
2285 else if (link->ops == &bpf_cgroup_link_lops)
2286 link_type = "cgroup";
2287#endif
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002288 else
2289 link_type = "unknown";
2290
2291 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2292 seq_printf(m,
2293 "link_type:\t%s\n"
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002294 "link_id:\t%u\n"
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002295 "prog_tag:\t%s\n"
2296 "prog_id:\t%u\n",
2297 link_type,
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002298 link->id,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002299 prog_tag,
2300 prog->aux->id);
2301}
2302#endif
2303
Zou Wei6f302bf2020-04-23 10:32:40 +08002304static const struct file_operations bpf_link_fops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002305#ifdef CONFIG_PROC_FS
2306 .show_fdinfo = bpf_link_show_fdinfo,
2307#endif
2308 .release = bpf_link_release,
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002309 .read = bpf_dummy_read,
2310 .write = bpf_dummy_write,
2311};
2312
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002313static int bpf_link_alloc_id(struct bpf_link *link)
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002314{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002315 int id;
2316
2317 idr_preload(GFP_KERNEL);
2318 spin_lock_bh(&link_idr_lock);
2319 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2320 spin_unlock_bh(&link_idr_lock);
2321 idr_preload_end();
2322
2323 return id;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002324}
2325
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002326/* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2327 * reserving unused FD and allocating ID from link_idr. This is to be paired
2328 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2329 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2330 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2331 * transient state is passed around in struct bpf_link_primer.
2332 * This is preferred way to create and initialize bpf_link, especially when
2333 * there are complicated and expensive operations inbetween creating bpf_link
2334 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2335 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2336 * expensive (and potentially failing) roll back operations in a rare case
2337 * that file, FD, or ID can't be allocated.
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002338 */
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002339int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002340{
2341 struct file *file;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002342 int fd, id;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002343
2344 fd = get_unused_fd_flags(O_CLOEXEC);
2345 if (fd < 0)
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002346 return fd;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002347
2348 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2349 if (IS_ERR(file)) {
2350 put_unused_fd(fd);
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002351 return PTR_ERR(file);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002352 }
2353
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002354 id = bpf_link_alloc_id(link);
2355 if (id < 0) {
2356 put_unused_fd(fd);
2357 fput(file);
2358 return id;
2359 }
2360
2361 primer->link = link;
2362 primer->file = file;
2363 primer->fd = fd;
2364 primer->id = id;
2365 return 0;
2366}
2367
2368int bpf_link_settle(struct bpf_link_primer *primer)
2369{
2370 /* make bpf_link fetchable by ID */
2371 spin_lock_bh(&link_idr_lock);
2372 primer->link->id = primer->id;
2373 spin_unlock_bh(&link_idr_lock);
2374 /* make bpf_link fetchable by FD */
2375 fd_install(primer->fd, primer->file);
2376 /* pass through installed FD */
2377 return primer->fd;
2378}
2379
2380int bpf_link_new_fd(struct bpf_link *link)
2381{
2382 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002383}
2384
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002385struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2386{
2387 struct fd f = fdget(ufd);
2388 struct bpf_link *link;
2389
2390 if (!f.file)
2391 return ERR_PTR(-EBADF);
2392 if (f.file->f_op != &bpf_link_fops) {
2393 fdput(f);
2394 return ERR_PTR(-EINVAL);
2395 }
2396
2397 link = f.file->private_data;
2398 bpf_link_inc(link);
2399 fdput(f);
2400
2401 return link;
2402}
2403
2404struct bpf_tracing_link {
2405 struct bpf_link link;
2406};
2407
2408static void bpf_tracing_link_release(struct bpf_link *link)
2409{
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002410 WARN_ON_ONCE(bpf_trampoline_unlink_prog(link->prog));
2411}
2412
2413static void bpf_tracing_link_dealloc(struct bpf_link *link)
2414{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002415 struct bpf_tracing_link *tr_link =
2416 container_of(link, struct bpf_tracing_link, link);
2417
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002418 kfree(tr_link);
2419}
2420
2421static const struct bpf_link_ops bpf_tracing_link_lops = {
2422 .release = bpf_tracing_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002423 .dealloc = bpf_tracing_link_dealloc,
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002424};
2425
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002426static int bpf_tracing_prog_attach(struct bpf_prog *prog)
2427{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002428 struct bpf_link_primer link_primer;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002429 struct bpf_tracing_link *link;
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002430 int err;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002431
KP Singh9e4e01d2020-03-29 01:43:52 +01002432 switch (prog->type) {
2433 case BPF_PROG_TYPE_TRACING:
2434 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2435 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2436 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2437 err = -EINVAL;
2438 goto out_put_prog;
2439 }
2440 break;
2441 case BPF_PROG_TYPE_EXT:
2442 if (prog->expected_attach_type != 0) {
2443 err = -EINVAL;
2444 goto out_put_prog;
2445 }
2446 break;
2447 case BPF_PROG_TYPE_LSM:
2448 if (prog->expected_attach_type != BPF_LSM_MAC) {
2449 err = -EINVAL;
2450 goto out_put_prog;
2451 }
2452 break;
2453 default:
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002454 err = -EINVAL;
2455 goto out_put_prog;
2456 }
2457
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002458 link = kzalloc(sizeof(*link), GFP_USER);
2459 if (!link) {
2460 err = -ENOMEM;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002461 goto out_put_prog;
2462 }
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002463 bpf_link_init(&link->link, &bpf_tracing_link_lops, prog);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002464
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002465 err = bpf_link_prime(&link->link, &link_primer);
2466 if (err) {
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002467 kfree(link);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002468 goto out_put_prog;
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002469 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002470
2471 err = bpf_trampoline_link_prog(prog);
2472 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002473 bpf_link_cleanup(&link_primer);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002474 goto out_put_prog;
2475 }
2476
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002477 return bpf_link_settle(&link_primer);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002478out_put_prog:
2479 bpf_prog_put(prog);
2480 return err;
2481}
2482
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002483struct bpf_raw_tp_link {
2484 struct bpf_link link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002485 struct bpf_raw_event_map *btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002486};
2487
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002488static void bpf_raw_tp_link_release(struct bpf_link *link)
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002489{
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002490 struct bpf_raw_tp_link *raw_tp =
2491 container_of(link, struct bpf_raw_tp_link, link);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002492
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002493 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
Matt Mullinsa38d1102018-12-12 16:42:37 -08002494 bpf_put_raw_tracepoint(raw_tp->btp);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002495}
2496
2497static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
2498{
2499 struct bpf_raw_tp_link *raw_tp =
2500 container_of(link, struct bpf_raw_tp_link, link);
2501
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002502 kfree(raw_tp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002503}
2504
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002505static const struct bpf_link_ops bpf_raw_tp_link_lops = {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08002506 .release = bpf_raw_tp_link_release,
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002507 .dealloc = bpf_raw_tp_link_dealloc,
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002508};
2509
2510#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2511
2512static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2513{
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002514 struct bpf_link_primer link_primer;
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002515 struct bpf_raw_tp_link *link;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002516 struct bpf_raw_event_map *btp;
2517 struct bpf_prog *prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002518 const char *tp_name;
2519 char buf[128];
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002520 int err;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002521
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002522 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2523 return -EINVAL;
2524
2525 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2526 if (IS_ERR(prog))
2527 return PTR_ERR(prog);
2528
KP Singh9e4e01d2020-03-29 01:43:52 +01002529 switch (prog->type) {
2530 case BPF_PROG_TYPE_TRACING:
2531 case BPF_PROG_TYPE_EXT:
2532 case BPF_PROG_TYPE_LSM:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002533 if (attr->raw_tracepoint.name) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002534 /* The attach point for this category of programs
2535 * should be specified via btf_id during program load.
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002536 */
2537 err = -EINVAL;
2538 goto out_put_prog;
2539 }
KP Singh9e4e01d2020-03-29 01:43:52 +01002540 if (prog->type == BPF_PROG_TYPE_TRACING &&
2541 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002542 tp_name = prog->aux->attach_func_name;
KP Singh9e4e01d2020-03-29 01:43:52 +01002543 break;
2544 }
2545 return bpf_tracing_prog_attach(prog);
2546 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2547 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002548 if (strncpy_from_user(buf,
2549 u64_to_user_ptr(attr->raw_tracepoint.name),
2550 sizeof(buf) - 1) < 0) {
2551 err = -EFAULT;
2552 goto out_put_prog;
2553 }
2554 buf[sizeof(buf) - 1] = 0;
2555 tp_name = buf;
KP Singh9e4e01d2020-03-29 01:43:52 +01002556 break;
2557 default:
2558 err = -EINVAL;
2559 goto out_put_prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002560 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002561
Matt Mullinsa38d1102018-12-12 16:42:37 -08002562 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002563 if (!btp) {
2564 err = -ENOENT;
2565 goto out_put_prog;
2566 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002567
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002568 link = kzalloc(sizeof(*link), GFP_USER);
2569 if (!link) {
Matt Mullinsa38d1102018-12-12 16:42:37 -08002570 err = -ENOMEM;
2571 goto out_put_btp;
2572 }
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002573 bpf_link_init(&link->link, &bpf_raw_tp_link_lops, prog);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002574 link->btp = btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002575
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002576 err = bpf_link_prime(&link->link, &link_primer);
2577 if (err) {
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002578 kfree(link);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002579 goto out_put_btp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002580 }
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002581
2582 err = bpf_probe_register(link->btp, prog);
2583 if (err) {
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002584 bpf_link_cleanup(&link_primer);
Andrii Nakryikobabf3162020-03-09 16:10:51 -07002585 goto out_put_btp;
2586 }
2587
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07002588 return bpf_link_settle(&link_primer);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002589
Matt Mullinsa38d1102018-12-12 16:42:37 -08002590out_put_btp:
2591 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002592out_put_prog:
2593 bpf_prog_put(prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002594 return err;
2595}
2596
Anders Roxell33491582018-04-03 14:09:47 +02002597static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2598 enum bpf_attach_type attach_type)
2599{
2600 switch (prog->type) {
2601 case BPF_PROG_TYPE_CGROUP_SOCK:
2602 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002603 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Anders Roxell33491582018-04-03 14:09:47 +02002604 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
brakmo5cf1e912019-05-28 16:59:36 -07002605 case BPF_PROG_TYPE_CGROUP_SKB:
2606 return prog->enforce_expected_attach_type &&
2607 prog->expected_attach_type != attach_type ?
2608 -EINVAL : 0;
Anders Roxell33491582018-04-03 14:09:47 +02002609 default:
2610 return 0;
2611 }
2612}
2613
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002614static enum bpf_prog_type
2615attach_type_to_prog_type(enum bpf_attach_type attach_type)
2616{
2617 switch (attach_type) {
2618 case BPF_CGROUP_INET_INGRESS:
2619 case BPF_CGROUP_INET_EGRESS:
2620 return BPF_PROG_TYPE_CGROUP_SKB;
2621 break;
2622 case BPF_CGROUP_INET_SOCK_CREATE:
2623 case BPF_CGROUP_INET4_POST_BIND:
2624 case BPF_CGROUP_INET6_POST_BIND:
2625 return BPF_PROG_TYPE_CGROUP_SOCK;
2626 case BPF_CGROUP_INET4_BIND:
2627 case BPF_CGROUP_INET6_BIND:
2628 case BPF_CGROUP_INET4_CONNECT:
2629 case BPF_CGROUP_INET6_CONNECT:
2630 case BPF_CGROUP_UDP4_SENDMSG:
2631 case BPF_CGROUP_UDP6_SENDMSG:
2632 case BPF_CGROUP_UDP4_RECVMSG:
2633 case BPF_CGROUP_UDP6_RECVMSG:
2634 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2635 case BPF_CGROUP_SOCK_OPS:
2636 return BPF_PROG_TYPE_SOCK_OPS;
2637 case BPF_CGROUP_DEVICE:
2638 return BPF_PROG_TYPE_CGROUP_DEVICE;
2639 case BPF_SK_MSG_VERDICT:
2640 return BPF_PROG_TYPE_SK_MSG;
2641 case BPF_SK_SKB_STREAM_PARSER:
2642 case BPF_SK_SKB_STREAM_VERDICT:
2643 return BPF_PROG_TYPE_SK_SKB;
2644 case BPF_LIRC_MODE2:
2645 return BPF_PROG_TYPE_LIRC_MODE2;
2646 case BPF_FLOW_DISSECTOR:
2647 return BPF_PROG_TYPE_FLOW_DISSECTOR;
2648 case BPF_CGROUP_SYSCTL:
2649 return BPF_PROG_TYPE_CGROUP_SYSCTL;
2650 case BPF_CGROUP_GETSOCKOPT:
2651 case BPF_CGROUP_SETSOCKOPT:
2652 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
2653 default:
2654 return BPF_PROG_TYPE_UNSPEC;
2655 }
2656}
2657
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08002658#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
John Fastabend174a79f2017-08-15 22:32:47 -07002659
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002660#define BPF_F_ATTACH_MASK \
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08002661 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002662
Daniel Mackf4324552016-11-23 16:52:27 +01002663static int bpf_prog_attach(const union bpf_attr *attr)
2664{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002665 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01002666 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002667 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01002668
2669 if (!capable(CAP_NET_ADMIN))
2670 return -EPERM;
2671
2672 if (CHECK_ATTR(BPF_PROG_ATTACH))
2673 return -EINVAL;
2674
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002675 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002676 return -EINVAL;
2677
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002678 ptype = attach_type_to_prog_type(attr->attach_type);
2679 if (ptype == BPF_PROG_TYPE_UNSPEC)
Daniel Mackf4324552016-11-23 16:52:27 +01002680 return -EINVAL;
Daniel Mackf4324552016-11-23 16:52:27 +01002681
David Ahernb2cd1252016-12-01 08:48:03 -08002682 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2683 if (IS_ERR(prog))
2684 return PTR_ERR(prog);
2685
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002686 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
2687 bpf_prog_put(prog);
2688 return -EINVAL;
2689 }
2690
Sean Youngfdb5c452018-06-19 00:04:24 +01002691 switch (ptype) {
2692 case BPF_PROG_TYPE_SK_SKB:
2693 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002694 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01002695 break;
2696 case BPF_PROG_TYPE_LIRC_MODE2:
2697 ret = lirc_prog_attach(attr, prog);
2698 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07002699 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2700 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
2701 break;
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002702 case BPF_PROG_TYPE_CGROUP_DEVICE:
2703 case BPF_PROG_TYPE_CGROUP_SKB:
2704 case BPF_PROG_TYPE_CGROUP_SOCK:
2705 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2706 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2707 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2708 case BPF_PROG_TYPE_SOCK_OPS:
Sean Youngfdb5c452018-06-19 00:04:24 +01002709 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002710 break;
2711 default:
2712 ret = -EINVAL;
David Ahernb2cd1252016-12-01 08:48:03 -08002713 }
2714
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002715 if (ret)
2716 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002717 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01002718}
2719
2720#define BPF_PROG_DETACH_LAST_FIELD attach_type
2721
2722static int bpf_prog_detach(const union bpf_attr *attr)
2723{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002724 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01002725
2726 if (!capable(CAP_NET_ADMIN))
2727 return -EPERM;
2728
2729 if (CHECK_ATTR(BPF_PROG_DETACH))
2730 return -EINVAL;
2731
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002732 ptype = attach_type_to_prog_type(attr->attach_type);
2733
2734 switch (ptype) {
2735 case BPF_PROG_TYPE_SK_MSG:
2736 case BPF_PROG_TYPE_SK_SKB:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002737 return sock_map_get_from_fd(attr, NULL);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002738 case BPF_PROG_TYPE_LIRC_MODE2:
Sean Youngf4364dc2018-05-27 12:24:09 +01002739 return lirc_prog_detach(attr);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002740 case BPF_PROG_TYPE_FLOW_DISSECTOR:
Petar Penkovd58e4682018-09-14 07:46:18 -07002741 return skb_flow_dissector_bpf_prog_detach(attr);
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002742 case BPF_PROG_TYPE_CGROUP_DEVICE:
2743 case BPF_PROG_TYPE_CGROUP_SKB:
2744 case BPF_PROG_TYPE_CGROUP_SOCK:
2745 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2746 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2747 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2748 case BPF_PROG_TYPE_SOCK_OPS:
2749 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01002750 default:
2751 return -EINVAL;
2752 }
Daniel Mackf4324552016-11-23 16:52:27 +01002753}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002754
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002755#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2756
2757static int bpf_prog_query(const union bpf_attr *attr,
2758 union bpf_attr __user *uattr)
2759{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002760 if (!capable(CAP_NET_ADMIN))
2761 return -EPERM;
2762 if (CHECK_ATTR(BPF_PROG_QUERY))
2763 return -EINVAL;
2764 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2765 return -EINVAL;
2766
2767 switch (attr->query.attach_type) {
2768 case BPF_CGROUP_INET_INGRESS:
2769 case BPF_CGROUP_INET_EGRESS:
2770 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002771 case BPF_CGROUP_INET4_BIND:
2772 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002773 case BPF_CGROUP_INET4_POST_BIND:
2774 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002775 case BPF_CGROUP_INET4_CONNECT:
2776 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002777 case BPF_CGROUP_UDP4_SENDMSG:
2778 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002779 case BPF_CGROUP_UDP4_RECVMSG:
2780 case BPF_CGROUP_UDP6_RECVMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002781 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05002782 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08002783 case BPF_CGROUP_SYSCTL:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002784 case BPF_CGROUP_GETSOCKOPT:
2785 case BPF_CGROUP_SETSOCKOPT:
Andrii Nakryikoe28784e2020-03-24 23:57:42 -07002786 return cgroup_bpf_prog_query(attr, uattr);
Sean Youngf4364dc2018-05-27 12:24:09 +01002787 case BPF_LIRC_MODE2:
2788 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07002789 case BPF_FLOW_DISSECTOR:
2790 return skb_flow_dissector_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002791 default:
2792 return -EINVAL;
2793 }
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002794}
Daniel Mackf4324552016-11-23 16:52:27 +01002795
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002796#define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002797
2798static int bpf_prog_test_run(const union bpf_attr *attr,
2799 union bpf_attr __user *uattr)
2800{
2801 struct bpf_prog *prog;
2802 int ret = -ENOTSUPP;
2803
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08002804 if (!capable(CAP_SYS_ADMIN))
2805 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002806 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2807 return -EINVAL;
2808
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002809 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2810 (!attr->test.ctx_size_in && attr->test.ctx_in))
2811 return -EINVAL;
2812
2813 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2814 (!attr->test.ctx_size_out && attr->test.ctx_out))
2815 return -EINVAL;
2816
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002817 prog = bpf_prog_get(attr->test.prog_fd);
2818 if (IS_ERR(prog))
2819 return PTR_ERR(prog);
2820
2821 if (prog->aux->ops->test_run)
2822 ret = prog->aux->ops->test_run(prog, attr, uattr);
2823
2824 bpf_prog_put(prog);
2825 return ret;
2826}
2827
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002828#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2829
2830static int bpf_obj_get_next_id(const union bpf_attr *attr,
2831 union bpf_attr __user *uattr,
2832 struct idr *idr,
2833 spinlock_t *lock)
2834{
2835 u32 next_id = attr->start_id;
2836 int err = 0;
2837
2838 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2839 return -EINVAL;
2840
2841 if (!capable(CAP_SYS_ADMIN))
2842 return -EPERM;
2843
2844 next_id++;
2845 spin_lock_bh(lock);
2846 if (!idr_get_next(idr, &next_id))
2847 err = -ENOENT;
2848 spin_unlock_bh(lock);
2849
2850 if (!err)
2851 err = put_user(next_id, &uattr->next_id);
2852
2853 return err;
2854}
2855
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002856#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2857
Björn Töpel7e6897f2019-12-13 18:51:09 +01002858struct bpf_prog *bpf_prog_by_id(u32 id)
2859{
2860 struct bpf_prog *prog;
2861
2862 if (!id)
2863 return ERR_PTR(-ENOENT);
2864
2865 spin_lock_bh(&prog_idr_lock);
2866 prog = idr_find(&prog_idr, id);
2867 if (prog)
2868 prog = bpf_prog_inc_not_zero(prog);
2869 else
2870 prog = ERR_PTR(-ENOENT);
2871 spin_unlock_bh(&prog_idr_lock);
2872 return prog;
2873}
2874
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002875static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2876{
2877 struct bpf_prog *prog;
2878 u32 id = attr->prog_id;
2879 int fd;
2880
2881 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2882 return -EINVAL;
2883
2884 if (!capable(CAP_SYS_ADMIN))
2885 return -EPERM;
2886
Björn Töpel7e6897f2019-12-13 18:51:09 +01002887 prog = bpf_prog_by_id(id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002888 if (IS_ERR(prog))
2889 return PTR_ERR(prog);
2890
2891 fd = bpf_prog_new_fd(prog);
2892 if (fd < 0)
2893 bpf_prog_put(prog);
2894
2895 return fd;
2896}
2897
Chenbo Feng6e71b042017-10-18 13:00:22 -07002898#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002899
2900static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2901{
2902 struct bpf_map *map;
2903 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07002904 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002905 int fd;
2906
Chenbo Feng6e71b042017-10-18 13:00:22 -07002907 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2908 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002909 return -EINVAL;
2910
2911 if (!capable(CAP_SYS_ADMIN))
2912 return -EPERM;
2913
Chenbo Feng6e71b042017-10-18 13:00:22 -07002914 f_flags = bpf_get_file_flag(attr->open_flags);
2915 if (f_flags < 0)
2916 return f_flags;
2917
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002918 spin_lock_bh(&map_idr_lock);
2919 map = idr_find(&map_idr, id);
2920 if (map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -07002921 map = __bpf_map_inc_not_zero(map, true);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002922 else
2923 map = ERR_PTR(-ENOENT);
2924 spin_unlock_bh(&map_idr_lock);
2925
2926 if (IS_ERR(map))
2927 return PTR_ERR(map);
2928
Chenbo Feng6e71b042017-10-18 13:00:22 -07002929 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002930 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08002931 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002932
2933 return fd;
2934}
2935
Daniel Borkmann7105e822017-12-20 13:42:57 +01002936static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002937 unsigned long addr, u32 *off,
2938 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01002939{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002940 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002941 int i;
2942
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002943 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2944 map = prog->aux->used_maps[i];
2945 if (map == (void *)addr) {
2946 *type = BPF_PSEUDO_MAP_FD;
2947 return map;
2948 }
2949 if (!map->ops->map_direct_value_meta)
2950 continue;
2951 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2952 *type = BPF_PSEUDO_MAP_VALUE;
2953 return map;
2954 }
2955 }
2956
Daniel Borkmann7105e822017-12-20 13:42:57 +01002957 return NULL;
2958}
2959
2960static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2961{
2962 const struct bpf_map *map;
2963 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002964 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002965 u64 imm;
2966 int i;
2967
2968 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2969 GFP_USER);
2970 if (!insns)
2971 return insns;
2972
2973 for (i = 0; i < prog->len; i++) {
2974 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2975 insns[i].code = BPF_JMP | BPF_CALL;
2976 insns[i].imm = BPF_FUNC_tail_call;
2977 /* fall-through */
2978 }
2979 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2980 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2981 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2982 insns[i].code = BPF_JMP | BPF_CALL;
2983 if (!bpf_dump_raw_ok())
2984 insns[i].imm = 0;
2985 continue;
2986 }
2987
2988 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2989 continue;
2990
2991 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002992 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002993 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002994 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002995 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002996 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002997 continue;
2998 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01002999 }
3000
3001 return insns;
3002}
3003
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003004static int set_info_rec_size(struct bpf_prog_info *info)
3005{
3006 /*
3007 * Ensure info.*_rec_size is the same as kernel expected size
3008 *
3009 * or
3010 *
3011 * Only allow zero *_rec_size if both _rec_size and _cnt are
3012 * zero. In this case, the kernel will set the expected
3013 * _rec_size back to the info.
3014 */
3015
Yonghong Song11d8b822018-12-10 14:14:08 -08003016 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003017 info->func_info_rec_size != sizeof(struct bpf_func_info))
3018 return -EINVAL;
3019
Yonghong Song11d8b822018-12-10 14:14:08 -08003020 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003021 info->line_info_rec_size != sizeof(struct bpf_line_info))
3022 return -EINVAL;
3023
Yonghong Song11d8b822018-12-10 14:14:08 -08003024 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003025 info->jited_line_info_rec_size != sizeof(__u64))
3026 return -EINVAL;
3027
3028 info->func_info_rec_size = sizeof(struct bpf_func_info);
3029 info->line_info_rec_size = sizeof(struct bpf_line_info);
3030 info->jited_line_info_rec_size = sizeof(__u64);
3031
3032 return 0;
3033}
3034
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003035static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
3036 const union bpf_attr *attr,
3037 union bpf_attr __user *uattr)
3038{
3039 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003040 struct bpf_prog_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003041 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003042 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003043 char __user *uinsns;
3044 u32 ulen;
3045 int err;
3046
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003047 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003048 if (err)
3049 return err;
3050 info_len = min_t(u32, sizeof(info), info_len);
3051
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003052 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003053 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02003054 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003055
3056 info.type = prog->type;
3057 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003058 info.load_time = prog->aux->load_time;
3059 info.created_by_uid = from_kuid_munged(current_user_ns(),
3060 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02003061 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003062
3063 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003064 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3065
3066 ulen = info.nr_map_ids;
3067 info.nr_map_ids = prog->aux->used_map_cnt;
3068 ulen = min_t(u32, info.nr_map_ids, ulen);
3069 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07003070 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07003071 u32 i;
3072
3073 for (i = 0; i < ulen; i++)
3074 if (put_user(prog->aux->used_maps[i]->id,
3075 &user_map_ids[i]))
3076 return -EFAULT;
3077 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003078
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003079 err = set_info_rec_size(&info);
3080 if (err)
3081 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08003082
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08003083 bpf_prog_get_stats(prog, &stats);
3084 info.run_time_ns = stats.nsecs;
3085 info.run_cnt = stats.cnt;
3086
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003087 if (!capable(CAP_SYS_ADMIN)) {
3088 info.jited_prog_len = 0;
3089 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303090 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01003091 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08003092 info.nr_func_info = 0;
3093 info.nr_line_info = 0;
3094 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003095 goto done;
3096 }
3097
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003098 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02003099 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003100 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01003101 struct bpf_insn *insns_sanitized;
3102 bool fault;
3103
3104 if (prog->blinded && !bpf_dump_raw_ok()) {
3105 info.xlated_prog_insns = 0;
3106 goto done;
3107 }
3108 insns_sanitized = bpf_insn_prepare_dump(prog);
3109 if (!insns_sanitized)
3110 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003111 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3112 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01003113 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3114 kfree(insns_sanitized);
3115 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003116 return -EFAULT;
3117 }
3118
Jakub Kicinski675fc272017-12-27 18:39:09 -08003119 if (bpf_prog_is_dev_bound(prog->aux)) {
3120 err = bpf_prog_offload_info_fill(&info, prog);
3121 if (err)
3122 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08003123 goto done;
3124 }
3125
3126 /* NOTE: the following code is supposed to be skipped for offload.
3127 * bpf_prog_offload_info_fill() is the place to fill similar fields
3128 * for offload.
3129 */
3130 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05303131 if (prog->aux->func_cnt) {
3132 u32 i;
3133
3134 info.jited_prog_len = 0;
3135 for (i = 0; i < prog->aux->func_cnt; i++)
3136 info.jited_prog_len += prog->aux->func[i]->jited_len;
3137 } else {
3138 info.jited_prog_len = prog->jited_len;
3139 }
3140
Jiong Wangfcfb1262018-01-16 16:05:19 -08003141 if (info.jited_prog_len && ulen) {
3142 if (bpf_dump_raw_ok()) {
3143 uinsns = u64_to_user_ptr(info.jited_prog_insns);
3144 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05303145
3146 /* for multi-function programs, copy the JITed
3147 * instructions for all the functions
3148 */
3149 if (prog->aux->func_cnt) {
3150 u32 len, free, i;
3151 u8 *img;
3152
3153 free = ulen;
3154 for (i = 0; i < prog->aux->func_cnt; i++) {
3155 len = prog->aux->func[i]->jited_len;
3156 len = min_t(u32, len, free);
3157 img = (u8 *) prog->aux->func[i]->bpf_func;
3158 if (copy_to_user(uinsns, img, len))
3159 return -EFAULT;
3160 uinsns += len;
3161 free -= len;
3162 if (!free)
3163 break;
3164 }
3165 } else {
3166 if (copy_to_user(uinsns, prog->bpf_func, ulen))
3167 return -EFAULT;
3168 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08003169 } else {
3170 info.jited_prog_insns = 0;
3171 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08003172 }
3173
Sandipan Dasdbecd732018-05-24 12:26:48 +05303174 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07003175 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003176 if (ulen) {
Sandipan Dasdbecd732018-05-24 12:26:48 +05303177 if (bpf_dump_raw_ok()) {
Song Liuff1889f2018-11-02 10:16:17 -07003178 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303179 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05303180 u32 i;
3181
3182 /* copy the address of the kernel symbol
3183 * corresponding to each function
3184 */
3185 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
3186 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07003187 if (prog->aux->func_cnt) {
3188 for (i = 0; i < ulen; i++) {
3189 ksym_addr = (unsigned long)
3190 prog->aux->func[i]->bpf_func;
3191 if (put_user((u64) ksym_addr,
3192 &user_ksyms[i]))
3193 return -EFAULT;
3194 }
3195 } else {
3196 ksym_addr = (unsigned long) prog->bpf_func;
3197 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05303198 return -EFAULT;
3199 }
3200 } else {
3201 info.jited_ksyms = 0;
3202 }
3203 }
3204
Sandipan Das815581c2018-05-24 12:26:52 +05303205 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07003206 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08003207 if (ulen) {
Sandipan Das815581c2018-05-24 12:26:52 +05303208 if (bpf_dump_raw_ok()) {
3209 u32 __user *user_lens;
3210 u32 func_len, i;
3211
3212 /* copy the JITed image lengths for each function */
3213 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
3214 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07003215 if (prog->aux->func_cnt) {
3216 for (i = 0; i < ulen; i++) {
3217 func_len =
3218 prog->aux->func[i]->jited_len;
3219 if (put_user(func_len, &user_lens[i]))
3220 return -EFAULT;
3221 }
3222 } else {
3223 func_len = prog->jited_len;
3224 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05303225 return -EFAULT;
3226 }
3227 } else {
3228 info.jited_func_lens = 0;
3229 }
3230 }
3231
Martin KaFai Lau73372242018-12-05 17:35:43 -08003232 if (prog->aux->btf)
Yonghong Song838e9692018-11-19 15:29:11 -08003233 info.btf_id = btf_id(prog->aux->btf);
3234
Yonghong Song11d8b822018-12-10 14:14:08 -08003235 ulen = info.nr_func_info;
3236 info.nr_func_info = prog->aux->func_info_cnt;
3237 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003238 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08003239
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003240 user_finfo = u64_to_user_ptr(info.func_info);
3241 ulen = min_t(u32, info.nr_func_info, ulen);
3242 if (copy_to_user(user_finfo, prog->aux->func_info,
3243 info.func_info_rec_size * ulen))
3244 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08003245 }
3246
Yonghong Song11d8b822018-12-10 14:14:08 -08003247 ulen = info.nr_line_info;
3248 info.nr_line_info = prog->aux->nr_linfo;
3249 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003250 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003251
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003252 user_linfo = u64_to_user_ptr(info.line_info);
3253 ulen = min_t(u32, info.nr_line_info, ulen);
3254 if (copy_to_user(user_linfo, prog->aux->linfo,
3255 info.line_info_rec_size * ulen))
3256 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003257 }
3258
Yonghong Song11d8b822018-12-10 14:14:08 -08003259 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003260 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08003261 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003262 else
Yonghong Song11d8b822018-12-10 14:14:08 -08003263 info.nr_jited_line_info = 0;
3264 if (info.nr_jited_line_info && ulen) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003265 if (bpf_dump_raw_ok()) {
3266 __u64 __user *user_linfo;
3267 u32 i;
3268
3269 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08003270 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003271 for (i = 0; i < ulen; i++) {
3272 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3273 &user_linfo[i]))
3274 return -EFAULT;
3275 }
3276 } else {
3277 info.jited_line_info = 0;
3278 }
3279 }
3280
Song Liuc872bdb2018-12-12 09:37:46 -08003281 ulen = info.nr_prog_tags;
3282 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3283 if (ulen) {
3284 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3285 u32 i;
3286
3287 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3288 ulen = min_t(u32, info.nr_prog_tags, ulen);
3289 if (prog->aux->func_cnt) {
3290 for (i = 0; i < ulen; i++) {
3291 if (copy_to_user(user_prog_tags[i],
3292 prog->aux->func[i]->tag,
3293 BPF_TAG_SIZE))
3294 return -EFAULT;
3295 }
3296 } else {
3297 if (copy_to_user(user_prog_tags[0],
3298 prog->tag, BPF_TAG_SIZE))
3299 return -EFAULT;
3300 }
3301 }
3302
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003303done:
3304 if (copy_to_user(uinfo, &info, info_len) ||
3305 put_user(info_len, &uattr->info.info_len))
3306 return -EFAULT;
3307
3308 return 0;
3309}
3310
3311static int bpf_map_get_info_by_fd(struct bpf_map *map,
3312 const union bpf_attr *attr,
3313 union bpf_attr __user *uattr)
3314{
3315 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003316 struct bpf_map_info info;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003317 u32 info_len = attr->info.info_len;
3318 int err;
3319
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003320 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003321 if (err)
3322 return err;
3323 info_len = min_t(u32, sizeof(info), info_len);
3324
Greg Kroah-Hartman5c6f2582020-03-20 17:22:58 +01003325 memset(&info, 0, sizeof(info));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003326 info.type = map->map_type;
3327 info.id = map->id;
3328 info.key_size = map->key_size;
3329 info.value_size = map->value_size;
3330 info.max_entries = map->max_entries;
3331 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07003332 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003333
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003334 if (map->btf) {
3335 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07003336 info.btf_key_type_id = map->btf_key_type_id;
3337 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003338 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08003339 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003340
Jakub Kicinski52775b32018-01-17 19:13:28 -08003341 if (bpf_map_is_dev_bound(map)) {
3342 err = bpf_map_offload_info_fill(&info, map);
3343 if (err)
3344 return err;
3345 }
3346
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003347 if (copy_to_user(uinfo, &info, info_len) ||
3348 put_user(info_len, &uattr->info.info_len))
3349 return -EFAULT;
3350
3351 return 0;
3352}
3353
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003354static int bpf_btf_get_info_by_fd(struct btf *btf,
3355 const union bpf_attr *attr,
3356 union bpf_attr __user *uattr)
3357{
3358 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3359 u32 info_len = attr->info.info_len;
3360 int err;
3361
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003362 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003363 if (err)
3364 return err;
3365
3366 return btf_get_info_by_fd(btf, attr, uattr);
3367}
3368
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003369#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3370
3371static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3372 union bpf_attr __user *uattr)
3373{
3374 int ufd = attr->info.bpf_fd;
3375 struct fd f;
3376 int err;
3377
3378 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3379 return -EINVAL;
3380
3381 f = fdget(ufd);
3382 if (!f.file)
3383 return -EBADFD;
3384
3385 if (f.file->f_op == &bpf_prog_fops)
3386 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
3387 uattr);
3388 else if (f.file->f_op == &bpf_map_fops)
3389 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
3390 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07003391 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003392 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003393 else
3394 err = -EINVAL;
3395
3396 fdput(f);
3397 return err;
3398}
3399
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003400#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3401
3402static int bpf_btf_load(const union bpf_attr *attr)
3403{
3404 if (CHECK_ATTR(BPF_BTF_LOAD))
3405 return -EINVAL;
3406
3407 if (!capable(CAP_SYS_ADMIN))
3408 return -EPERM;
3409
3410 return btf_new_fd(attr);
3411}
3412
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003413#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3414
3415static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3416{
3417 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3418 return -EINVAL;
3419
3420 if (!capable(CAP_SYS_ADMIN))
3421 return -EPERM;
3422
3423 return btf_get_fd_by_id(attr->btf_id);
3424}
3425
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003426static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3427 union bpf_attr __user *uattr,
3428 u32 prog_id, u32 fd_type,
3429 const char *buf, u64 probe_offset,
3430 u64 probe_addr)
3431{
3432 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3433 u32 len = buf ? strlen(buf) : 0, input_len;
3434 int err = 0;
3435
3436 if (put_user(len, &uattr->task_fd_query.buf_len))
3437 return -EFAULT;
3438 input_len = attr->task_fd_query.buf_len;
3439 if (input_len && ubuf) {
3440 if (!len) {
3441 /* nothing to copy, just make ubuf NULL terminated */
3442 char zero = '\0';
3443
3444 if (put_user(zero, ubuf))
3445 return -EFAULT;
3446 } else if (input_len >= len + 1) {
3447 /* ubuf can hold the string with NULL terminator */
3448 if (copy_to_user(ubuf, buf, len + 1))
3449 return -EFAULT;
3450 } else {
3451 /* ubuf cannot hold the string with NULL terminator,
3452 * do a partial copy with NULL terminator.
3453 */
3454 char zero = '\0';
3455
3456 err = -ENOSPC;
3457 if (copy_to_user(ubuf, buf, input_len - 1))
3458 return -EFAULT;
3459 if (put_user(zero, ubuf + input_len - 1))
3460 return -EFAULT;
3461 }
3462 }
3463
3464 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3465 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3466 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3467 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3468 return -EFAULT;
3469
3470 return err;
3471}
3472
3473#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3474
3475static int bpf_task_fd_query(const union bpf_attr *attr,
3476 union bpf_attr __user *uattr)
3477{
3478 pid_t pid = attr->task_fd_query.pid;
3479 u32 fd = attr->task_fd_query.fd;
3480 const struct perf_event *event;
3481 struct files_struct *files;
3482 struct task_struct *task;
3483 struct file *file;
3484 int err;
3485
3486 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3487 return -EINVAL;
3488
3489 if (!capable(CAP_SYS_ADMIN))
3490 return -EPERM;
3491
3492 if (attr->task_fd_query.flags != 0)
3493 return -EINVAL;
3494
3495 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3496 if (!task)
3497 return -ENOENT;
3498
3499 files = get_files_struct(task);
3500 put_task_struct(task);
3501 if (!files)
3502 return -ENOENT;
3503
3504 err = 0;
3505 spin_lock(&files->file_lock);
3506 file = fcheck_files(files, fd);
3507 if (!file)
3508 err = -EBADF;
3509 else
3510 get_file(file);
3511 spin_unlock(&files->file_lock);
3512 put_files_struct(files);
3513
3514 if (err)
3515 goto out;
3516
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003517 if (file->f_op == &bpf_link_fops) {
3518 struct bpf_link *link = file->private_data;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003519
Andrii Nakryikoa3b80e12020-04-28 17:16:06 -07003520 if (link->ops == &bpf_raw_tp_link_lops) {
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003521 struct bpf_raw_tp_link *raw_tp =
3522 container_of(link, struct bpf_raw_tp_link, link);
3523 struct bpf_raw_event_map *btp = raw_tp->btp;
3524
3525 err = bpf_task_fd_query_copy(attr, uattr,
3526 raw_tp->link.prog->aux->id,
3527 BPF_FD_TYPE_RAW_TRACEPOINT,
3528 btp->tp->name, 0, 0);
3529 goto put_file;
3530 }
3531 goto out_not_supp;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003532 }
3533
3534 event = perf_get_event(file);
3535 if (!IS_ERR(event)) {
3536 u64 probe_offset, probe_addr;
3537 u32 prog_id, fd_type;
3538 const char *buf;
3539
3540 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3541 &buf, &probe_offset,
3542 &probe_addr);
3543 if (!err)
3544 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3545 fd_type, buf,
3546 probe_offset,
3547 probe_addr);
3548 goto put_file;
3549 }
3550
Andrii Nakryiko70ed5062020-03-02 20:31:57 -08003551out_not_supp:
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003552 err = -ENOTSUPP;
3553put_file:
3554 fput(file);
3555out:
3556 return err;
3557}
3558
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003559#define BPF_MAP_BATCH_LAST_FIELD batch.flags
3560
3561#define BPF_DO_BATCH(fn) \
3562 do { \
3563 if (!fn) { \
3564 err = -ENOTSUPP; \
3565 goto err_put; \
3566 } \
3567 err = fn(map, attr, uattr); \
3568 } while (0)
3569
3570static int bpf_map_do_batch(const union bpf_attr *attr,
3571 union bpf_attr __user *uattr,
3572 int cmd)
3573{
3574 struct bpf_map *map;
3575 int err, ufd;
3576 struct fd f;
3577
3578 if (CHECK_ATTR(BPF_MAP_BATCH))
3579 return -EINVAL;
3580
3581 ufd = attr->batch.map_fd;
3582 f = fdget(ufd);
3583 map = __bpf_map_get(f);
3584 if (IS_ERR(map))
3585 return PTR_ERR(map);
3586
Yonghong Song05799632020-01-15 10:43:04 -08003587 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
3588 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003589 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
3590 err = -EPERM;
3591 goto err_put;
3592 }
3593
3594 if (cmd != BPF_MAP_LOOKUP_BATCH &&
3595 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
3596 err = -EPERM;
3597 goto err_put;
3598 }
3599
3600 if (cmd == BPF_MAP_LOOKUP_BATCH)
3601 BPF_DO_BATCH(map->ops->map_lookup_batch);
Yonghong Song05799632020-01-15 10:43:04 -08003602 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
3603 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08003604 else if (cmd == BPF_MAP_UPDATE_BATCH)
3605 BPF_DO_BATCH(map->ops->map_update_batch);
3606 else
3607 BPF_DO_BATCH(map->ops->map_delete_batch);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003608
3609err_put:
3610 fdput(f);
3611 return err;
3612}
3613
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07003614#define BPF_LINK_CREATE_LAST_FIELD link_create.flags
3615static int link_create(union bpf_attr *attr)
3616{
3617 enum bpf_prog_type ptype;
3618 struct bpf_prog *prog;
3619 int ret;
3620
3621 if (!capable(CAP_NET_ADMIN))
3622 return -EPERM;
3623
3624 if (CHECK_ATTR(BPF_LINK_CREATE))
3625 return -EINVAL;
3626
3627 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
3628 if (ptype == BPF_PROG_TYPE_UNSPEC)
3629 return -EINVAL;
3630
3631 prog = bpf_prog_get_type(attr->link_create.prog_fd, ptype);
3632 if (IS_ERR(prog))
3633 return PTR_ERR(prog);
3634
3635 ret = bpf_prog_attach_check_attach_type(prog,
3636 attr->link_create.attach_type);
3637 if (ret)
3638 goto err_out;
3639
3640 switch (ptype) {
3641 case BPF_PROG_TYPE_CGROUP_SKB:
3642 case BPF_PROG_TYPE_CGROUP_SOCK:
3643 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3644 case BPF_PROG_TYPE_SOCK_OPS:
3645 case BPF_PROG_TYPE_CGROUP_DEVICE:
3646 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3647 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3648 ret = cgroup_bpf_link_attach(attr, prog);
3649 break;
3650 default:
3651 ret = -EINVAL;
3652 }
3653
3654err_out:
3655 if (ret < 0)
3656 bpf_prog_put(prog);
3657 return ret;
3658}
3659
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003660#define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
3661
3662static int link_update(union bpf_attr *attr)
3663{
3664 struct bpf_prog *old_prog = NULL, *new_prog;
3665 struct bpf_link *link;
3666 u32 flags;
3667 int ret;
3668
3669 if (!capable(CAP_NET_ADMIN))
3670 return -EPERM;
3671
3672 if (CHECK_ATTR(BPF_LINK_UPDATE))
3673 return -EINVAL;
3674
3675 flags = attr->link_update.flags;
3676 if (flags & ~BPF_F_REPLACE)
3677 return -EINVAL;
3678
3679 link = bpf_link_get_from_fd(attr->link_update.link_fd);
3680 if (IS_ERR(link))
3681 return PTR_ERR(link);
3682
3683 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07003684 if (IS_ERR(new_prog)) {
3685 ret = PTR_ERR(new_prog);
3686 goto out_put_link;
3687 }
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003688
3689 if (flags & BPF_F_REPLACE) {
3690 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
3691 if (IS_ERR(old_prog)) {
3692 ret = PTR_ERR(old_prog);
3693 old_prog = NULL;
3694 goto out_put_progs;
3695 }
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07003696 } else if (attr->link_update.old_prog_fd) {
3697 ret = -EINVAL;
3698 goto out_put_progs;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003699 }
3700
Andrii Nakryikof9d04122020-04-28 17:16:05 -07003701 if (link->ops->update_prog)
3702 ret = link->ops->update_prog(link, new_prog, old_prog);
3703 else
3704 ret = EINVAL;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003705
3706out_put_progs:
3707 if (old_prog)
3708 bpf_prog_put(old_prog);
3709 if (ret)
3710 bpf_prog_put(new_prog);
Andrii Nakryiko4adb7a42020-04-23 22:20:44 -07003711out_put_link:
3712 bpf_link_put(link);
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003713 return ret;
3714}
3715
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003716SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
3717{
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01003718 union bpf_attr attr;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003719 int err;
3720
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07003721 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003722 return -EPERM;
3723
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003724 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003725 if (err)
3726 return err;
3727 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003728
3729 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
Greg Kroah-Hartman8096f222020-03-20 10:48:13 +01003730 memset(&attr, 0, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003731 if (copy_from_user(&attr, uattr, size) != 0)
3732 return -EFAULT;
3733
Chenbo Fengafdb09c2017-10-18 13:00:24 -07003734 err = security_bpf(cmd, &attr, size);
3735 if (err < 0)
3736 return err;
3737
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003738 switch (cmd) {
3739 case BPF_MAP_CREATE:
3740 err = map_create(&attr);
3741 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07003742 case BPF_MAP_LOOKUP_ELEM:
3743 err = map_lookup_elem(&attr);
3744 break;
3745 case BPF_MAP_UPDATE_ELEM:
3746 err = map_update_elem(&attr);
3747 break;
3748 case BPF_MAP_DELETE_ELEM:
3749 err = map_delete_elem(&attr);
3750 break;
3751 case BPF_MAP_GET_NEXT_KEY:
3752 err = map_get_next_key(&attr);
3753 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02003754 case BPF_MAP_FREEZE:
3755 err = map_freeze(&attr);
3756 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07003757 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08003758 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07003759 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01003760 case BPF_OBJ_PIN:
3761 err = bpf_obj_pin(&attr);
3762 break;
3763 case BPF_OBJ_GET:
3764 err = bpf_obj_get(&attr);
3765 break;
Daniel Mackf4324552016-11-23 16:52:27 +01003766 case BPF_PROG_ATTACH:
3767 err = bpf_prog_attach(&attr);
3768 break;
3769 case BPF_PROG_DETACH:
3770 err = bpf_prog_detach(&attr);
3771 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003772 case BPF_PROG_QUERY:
3773 err = bpf_prog_query(&attr, uattr);
3774 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003775 case BPF_PROG_TEST_RUN:
3776 err = bpf_prog_test_run(&attr, uattr);
3777 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07003778 case BPF_PROG_GET_NEXT_ID:
3779 err = bpf_obj_get_next_id(&attr, uattr,
3780 &prog_idr, &prog_idr_lock);
3781 break;
3782 case BPF_MAP_GET_NEXT_ID:
3783 err = bpf_obj_get_next_id(&attr, uattr,
3784 &map_idr, &map_idr_lock);
3785 break;
Quentin Monnet1b9ed842019-08-20 10:31:50 +01003786 case BPF_BTF_GET_NEXT_ID:
3787 err = bpf_obj_get_next_id(&attr, uattr,
3788 &btf_idr, &btf_idr_lock);
3789 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003790 case BPF_PROG_GET_FD_BY_ID:
3791 err = bpf_prog_get_fd_by_id(&attr);
3792 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003793 case BPF_MAP_GET_FD_BY_ID:
3794 err = bpf_map_get_fd_by_id(&attr);
3795 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003796 case BPF_OBJ_GET_INFO_BY_FD:
3797 err = bpf_obj_get_info_by_fd(&attr, uattr);
3798 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003799 case BPF_RAW_TRACEPOINT_OPEN:
3800 err = bpf_raw_tracepoint_open(&attr);
3801 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003802 case BPF_BTF_LOAD:
3803 err = bpf_btf_load(&attr);
3804 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003805 case BPF_BTF_GET_FD_BY_ID:
3806 err = bpf_btf_get_fd_by_id(&attr);
3807 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003808 case BPF_TASK_FD_QUERY:
3809 err = bpf_task_fd_query(&attr, uattr);
3810 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02003811 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
3812 err = map_lookup_and_delete_elem(&attr);
3813 break;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003814 case BPF_MAP_LOOKUP_BATCH:
3815 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
3816 break;
Yonghong Song05799632020-01-15 10:43:04 -08003817 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
3818 err = bpf_map_do_batch(&attr, uattr,
3819 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
3820 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08003821 case BPF_MAP_UPDATE_BATCH:
3822 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
3823 break;
3824 case BPF_MAP_DELETE_BATCH:
3825 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
3826 break;
Andrii Nakryikoaf6eea52020-03-29 19:59:58 -07003827 case BPF_LINK_CREATE:
3828 err = link_create(&attr);
3829 break;
Andrii Nakryiko0c991eb2020-03-29 19:59:59 -07003830 case BPF_LINK_UPDATE:
3831 err = link_update(&attr);
3832 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003833 default:
3834 err = -EINVAL;
3835 break;
3836 }
3837
3838 return err;
3839}