blob: 2857b7dda382c1eb469ec21f944c1d8cebf4340f [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>
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070028
Daniel Borkmannda765a22019-11-22 21:07:58 +010029#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
30 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
31 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
32#define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070033#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
Daniel Borkmannda765a22019-11-22 21:07:58 +010034#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
35 IS_FD_HASH(map))
Martin KaFai Lau14dc6f02017-06-27 23:08:34 -070036
Chenbo Feng6e71b042017-10-18 13:00:22 -070037#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
38
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080039DEFINE_PER_CPU(int, bpf_prog_active);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -070040static DEFINE_IDR(prog_idr);
41static DEFINE_SPINLOCK(prog_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -070042static DEFINE_IDR(map_idr);
43static DEFINE_SPINLOCK(map_idr_lock);
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -080044
Alexei Starovoitov1be7f752015-10-07 22:23:21 -070045int sysctl_unprivileged_bpf_disabled __read_mostly;
46
Johannes Berg40077e02017-04-11 15:34:58 +020047static const struct bpf_map_ops * const bpf_map_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -080048#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
Johannes Berg40077e02017-04-11 15:34:58 +020049#define BPF_MAP_TYPE(_id, _ops) \
50 [_id] = &_ops,
51#include <linux/bpf_types.h>
52#undef BPF_PROG_TYPE
53#undef BPF_MAP_TYPE
54};
Alexei Starovoitov99c55f72014-09-26 00:16:57 -070055
Mickaël Salaün752ba562017-08-07 20:45:20 +020056/*
57 * If we're handed a bigger struct than we know of, ensure all the unknown bits
58 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
59 * we don't know about yet.
60 *
61 * There is a ToCToU between this function call and the following
62 * copy_from_user() call. However, this is not a concern since this function is
63 * meant to be a future-proofing of bits.
64 */
Martin KaFai Laudcab51f2018-05-22 15:03:31 -070065int bpf_check_uarg_tail_zero(void __user *uaddr,
66 size_t expected_size,
67 size_t actual_size)
Mickaël Salaün58291a72017-08-07 20:45:19 +020068{
69 unsigned char __user *addr;
70 unsigned char __user *end;
71 unsigned char val;
72 int err;
73
Mickaël Salaün752ba562017-08-07 20:45:20 +020074 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
75 return -E2BIG;
76
Linus Torvalds96d4f262019-01-03 18:57:57 -080077 if (unlikely(!access_ok(uaddr, actual_size)))
Mickaël Salaün752ba562017-08-07 20:45:20 +020078 return -EFAULT;
79
Mickaël Salaün58291a72017-08-07 20:45:19 +020080 if (actual_size <= expected_size)
81 return 0;
82
83 addr = uaddr + expected_size;
84 end = uaddr + actual_size;
85
86 for (; addr < end; addr++) {
87 err = get_user(val, addr);
88 if (err)
89 return err;
90 if (val)
91 return -E2BIG;
92 }
93
94 return 0;
95}
96
Jakub Kicinskia3884572018-01-11 20:29:09 -080097const struct bpf_map_ops bpf_map_offload_ops = {
98 .map_alloc = bpf_map_offload_map_alloc,
99 .map_free = bpf_map_offload_map_free,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200100 .map_check_btf = map_check_no_btf,
Jakub Kicinskia3884572018-01-11 20:29:09 -0800101};
102
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700103static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
104{
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800105 const struct bpf_map_ops *ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100106 u32 type = attr->map_type;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700107 struct bpf_map *map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800108 int err;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700109
Mark Rutland9ef09e32018-05-03 17:04:59 +0100110 if (type >= ARRAY_SIZE(bpf_map_types))
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800111 return ERR_PTR(-EINVAL);
Mark Rutland9ef09e32018-05-03 17:04:59 +0100112 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
113 ops = bpf_map_types[type];
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800114 if (!ops)
Johannes Berg40077e02017-04-11 15:34:58 +0200115 return ERR_PTR(-EINVAL);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700116
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800117 if (ops->map_alloc_check) {
118 err = ops->map_alloc_check(attr);
119 if (err)
120 return ERR_PTR(err);
121 }
Jakub Kicinskia3884572018-01-11 20:29:09 -0800122 if (attr->map_ifindex)
123 ops = &bpf_map_offload_ops;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800124 map = ops->map_alloc(attr);
Johannes Berg40077e02017-04-11 15:34:58 +0200125 if (IS_ERR(map))
126 return map;
Jakub Kicinski1110f3a2018-01-11 20:29:03 -0800127 map->ops = ops;
Mark Rutland9ef09e32018-05-03 17:04:59 +0100128 map->map_type = type;
Johannes Berg40077e02017-04-11 15:34:58 +0200129 return map;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700130}
131
Brian Vazquez15c14a32020-01-15 10:43:00 -0800132static u32 bpf_map_value_size(struct bpf_map *map)
133{
134 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
135 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
136 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
137 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
138 return round_up(map->value_size, 8) * num_possible_cpus();
139 else if (IS_FD_MAP(map))
140 return sizeof(u32);
141 else
142 return map->value_size;
143}
144
145static void maybe_wait_bpf_programs(struct bpf_map *map)
146{
147 /* Wait for any running BPF programs to complete so that
148 * userspace, when we return to it, knows that all programs
149 * that could be running use the new map value.
150 */
151 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
152 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
153 synchronize_rcu();
154}
155
156static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
157 void *value, __u64 flags)
158{
159 int err;
160
161 /* Need to create a kthread, thus must support schedule */
162 if (bpf_map_is_dev_bound(map)) {
163 return bpf_map_offload_update_elem(map, key, value, flags);
164 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
165 map->map_type == BPF_MAP_TYPE_SOCKHASH ||
166 map->map_type == BPF_MAP_TYPE_SOCKMAP ||
167 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
168 return map->ops->map_update_elem(map, key, value, flags);
169 } else if (IS_FD_PROG_ARRAY(map)) {
170 return bpf_fd_array_map_update_elem(map, f.file, key, value,
171 flags);
172 }
173
174 /* must increment bpf_prog_active to avoid kprobe+bpf triggering from
175 * inside bpf map update or delete otherwise deadlocks are possible
176 */
177 preempt_disable();
178 __this_cpu_inc(bpf_prog_active);
179 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
180 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
181 err = bpf_percpu_hash_update(map, key, value, flags);
182 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
183 err = bpf_percpu_array_update(map, key, value, flags);
184 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
185 err = bpf_percpu_cgroup_storage_update(map, key, value,
186 flags);
187 } else if (IS_FD_ARRAY(map)) {
188 rcu_read_lock();
189 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
190 flags);
191 rcu_read_unlock();
192 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
193 rcu_read_lock();
194 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
195 flags);
196 rcu_read_unlock();
197 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
198 /* rcu_read_lock() is not needed */
199 err = bpf_fd_reuseport_array_update_elem(map, key, value,
200 flags);
201 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
202 map->map_type == BPF_MAP_TYPE_STACK) {
203 err = map->ops->map_push_elem(map, value, flags);
204 } else {
205 rcu_read_lock();
206 err = map->ops->map_update_elem(map, key, value, flags);
207 rcu_read_unlock();
208 }
209 __this_cpu_dec(bpf_prog_active);
210 preempt_enable();
211 maybe_wait_bpf_programs(map);
212
213 return err;
214}
215
216static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
217 __u64 flags)
218{
219 void *ptr;
220 int err;
221
Brian Vazquezcb4d03a2020-01-15 10:43:01 -0800222 if (bpf_map_is_dev_bound(map))
223 return bpf_map_offload_lookup_elem(map, key, value);
Brian Vazquez15c14a32020-01-15 10:43:00 -0800224
225 preempt_disable();
226 this_cpu_inc(bpf_prog_active);
227 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
228 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
229 err = bpf_percpu_hash_copy(map, key, value);
230 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
231 err = bpf_percpu_array_copy(map, key, value);
232 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
233 err = bpf_percpu_cgroup_storage_copy(map, key, value);
234 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
235 err = bpf_stackmap_copy(map, key, value);
236 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
237 err = bpf_fd_array_map_lookup_elem(map, key, value);
238 } else if (IS_FD_HASH(map)) {
239 err = bpf_fd_htab_map_lookup_elem(map, key, value);
240 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
241 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
242 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
243 map->map_type == BPF_MAP_TYPE_STACK) {
244 err = map->ops->map_peek_elem(map, value);
245 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
246 /* struct_ops map requires directly updating "value" */
247 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
248 } else {
249 rcu_read_lock();
250 if (map->ops->map_lookup_elem_sys_only)
251 ptr = map->ops->map_lookup_elem_sys_only(map, key);
252 else
253 ptr = map->ops->map_lookup_elem(map, key);
254 if (IS_ERR(ptr)) {
255 err = PTR_ERR(ptr);
256 } else if (!ptr) {
257 err = -ENOENT;
258 } else {
259 err = 0;
260 if (flags & BPF_F_LOCK)
261 /* lock 'ptr' and copy everything but lock */
262 copy_map_value_locked(map, value, ptr, true);
263 else
264 copy_map_value(map, value, ptr);
265 /* mask lock, since value wasn't zero inited */
266 check_and_init_map_lock(map, value);
267 }
268 rcu_read_unlock();
269 }
270
271 this_cpu_dec(bpf_prog_active);
272 preempt_enable();
273 maybe_wait_bpf_programs(map);
274
275 return err;
276}
277
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100278static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100279{
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100280 /* We really just want to fail instead of triggering OOM killer
281 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
282 * which is used for lower order allocation requests.
283 *
284 * It has been observed that higher order allocation requests done by
285 * vmalloc with __GFP_NORETRY being set might fail due to not trying
286 * to reclaim memory from the page cache, thus we set
287 * __GFP_RETRY_MAYFAIL to avoid such situations.
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100288 */
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100289
290 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100291 void *area;
292
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100293 if (size >= SIZE_MAX)
294 return NULL;
295
Andrii Nakryikofc970222019-11-17 09:28:04 -0800296 /* kmalloc()'ed memory can't be mmap()'ed */
297 if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100298 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
299 numa_node);
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100300 if (area != NULL)
301 return area;
302 }
Andrii Nakryikofc970222019-11-17 09:28:04 -0800303 if (mmapable) {
304 BUG_ON(!PAGE_ALIGNED(size));
305 return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL |
306 __GFP_RETRY_MAYFAIL | flags);
307 }
Martynas Pumputisf01a7db2019-03-18 16:10:26 +0100308 return __vmalloc_node_flags_caller(size, numa_node,
309 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
310 flags, __builtin_return_address(0));
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100311}
312
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100313void *bpf_map_area_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800314{
315 return __bpf_map_area_alloc(size, numa_node, false);
316}
317
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100318void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
Andrii Nakryikofc970222019-11-17 09:28:04 -0800319{
320 return __bpf_map_area_alloc(size, numa_node, true);
321}
322
Daniel Borkmannd407bd22017-01-18 15:14:17 +0100323void bpf_map_area_free(void *area)
324{
325 kvfree(area);
326}
327
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200328static u32 bpf_map_flags_retain_permanent(u32 flags)
329{
330 /* Some map creation flags are not tied to the map object but
331 * rather to the map fd instead, so they have no meaning upon
332 * map object inspection since multiple file descriptors with
333 * different (access) properties can exist here. Thus, given
334 * this has zero meaning for the map itself, lets clear these
335 * from here.
336 */
337 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
338}
339
Jakub Kicinskibd475642018-01-11 20:29:06 -0800340void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
341{
342 map->map_type = attr->map_type;
343 map->key_size = attr->key_size;
344 map->value_size = attr->value_size;
345 map->max_entries = attr->max_entries;
Daniel Borkmannbe70bcd2019-04-09 23:20:04 +0200346 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
Jakub Kicinskibd475642018-01-11 20:29:06 -0800347 map->numa_node = bpf_map_attr_numa_node(attr);
348}
349
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700350static int bpf_charge_memlock(struct user_struct *user, u32 pages)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700351{
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700352 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700353
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700354 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
355 atomic_long_sub(pages, &user->locked_vm);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700356 return -EPERM;
357 }
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700358 return 0;
359}
360
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700361static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
362{
Roman Gushchinb936ca62019-05-29 18:03:58 -0700363 if (user)
364 atomic_long_sub(pages, &user->locked_vm);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700365}
366
Daniel Borkmann196e8ca2019-11-20 23:04:44 +0100367int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700368{
Roman Gushchinc85d6912019-05-29 18:03:59 -0700369 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
370 struct user_struct *user;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700371 int ret;
372
Roman Gushchinc85d6912019-05-29 18:03:59 -0700373 if (size >= U32_MAX - PAGE_SIZE)
374 return -E2BIG;
375
376 user = get_current_user();
Roman Gushchinb936ca62019-05-29 18:03:58 -0700377 ret = bpf_charge_memlock(user, pages);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700378 if (ret) {
379 free_uid(user);
380 return ret;
381 }
Roman Gushchinb936ca62019-05-29 18:03:58 -0700382
383 mem->pages = pages;
384 mem->user = user;
385
386 return 0;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700387}
388
Roman Gushchinb936ca62019-05-29 18:03:58 -0700389void bpf_map_charge_finish(struct bpf_map_memory *mem)
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700390{
Roman Gushchinb936ca62019-05-29 18:03:58 -0700391 bpf_uncharge_memlock(mem->user, mem->pages);
392 free_uid(mem->user);
393}
Roman Gushchin3539b962019-05-29 18:03:57 -0700394
Roman Gushchinb936ca62019-05-29 18:03:58 -0700395void bpf_map_charge_move(struct bpf_map_memory *dst,
396 struct bpf_map_memory *src)
397{
398 *dst = *src;
399
400 /* Make sure src will not be used for the redundant uncharging. */
401 memset(src, 0, sizeof(struct bpf_map_memory));
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700402}
403
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700404int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
405{
406 int ret;
407
Roman Gushchin3539b962019-05-29 18:03:57 -0700408 ret = bpf_charge_memlock(map->memory.user, pages);
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700409 if (ret)
410 return ret;
Roman Gushchin3539b962019-05-29 18:03:57 -0700411 map->memory.pages += pages;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700412 return ret;
413}
414
415void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
416{
Roman Gushchin3539b962019-05-29 18:03:57 -0700417 bpf_uncharge_memlock(map->memory.user, pages);
418 map->memory.pages -= pages;
Roman Gushchin0a4c58f2018-08-02 14:27:17 -0700419}
420
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700421static int bpf_map_alloc_id(struct bpf_map *map)
422{
423 int id;
424
Shaohua Lib76354c2018-03-27 11:53:21 -0700425 idr_preload(GFP_KERNEL);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700426 spin_lock_bh(&map_idr_lock);
427 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
428 if (id > 0)
429 map->id = id;
430 spin_unlock_bh(&map_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -0700431 idr_preload_end();
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700432
433 if (WARN_ON_ONCE(!id))
434 return -ENOSPC;
435
436 return id > 0 ? 0 : id;
437}
438
Jakub Kicinskia3884572018-01-11 20:29:09 -0800439void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700440{
Eric Dumazet930651a2017-09-19 09:15:59 -0700441 unsigned long flags;
442
Jakub Kicinskia3884572018-01-11 20:29:09 -0800443 /* Offloaded maps are removed from the IDR store when their device
444 * disappears - even if someone holds an fd to them they are unusable,
445 * the memory is gone, all ops will fail; they are simply waiting for
446 * refcnt to drop to be freed.
447 */
448 if (!map->id)
449 return;
450
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700451 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700452 spin_lock_irqsave(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700453 else
454 __acquire(&map_idr_lock);
455
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700456 idr_remove(&map_idr, map->id);
Jakub Kicinskia3884572018-01-11 20:29:09 -0800457 map->id = 0;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700458
459 if (do_idr_lock)
Eric Dumazet930651a2017-09-19 09:15:59 -0700460 spin_unlock_irqrestore(&map_idr_lock, flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700461 else
462 __release(&map_idr_lock);
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700463}
464
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700465/* called from workqueue */
466static void bpf_map_free_deferred(struct work_struct *work)
467{
468 struct bpf_map *map = container_of(work, struct bpf_map, work);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700469 struct bpf_map_memory mem;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700470
Roman Gushchinb936ca62019-05-29 18:03:58 -0700471 bpf_map_charge_move(&mem, &map->memory);
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700472 security_bpf_map_free(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700473 /* implementation dependent freeing */
474 map->ops->map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700475 bpf_map_charge_finish(&mem);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700476}
477
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100478static void bpf_map_put_uref(struct bpf_map *map)
479{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800480 if (atomic64_dec_and_test(&map->usercnt)) {
John Fastabendba6b8de2018-04-23 15:39:23 -0700481 if (map->ops->map_release_uref)
482 map->ops->map_release_uref(map);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100483 }
484}
485
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700486/* decrement map refcnt and schedule it for freeing via workqueue
487 * (unrelying map implementation ops->map_free() might sleep)
488 */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700489static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700490{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800491 if (atomic64_dec_and_test(&map->refcnt)) {
Martin KaFai Lau34ad5582017-06-05 12:15:48 -0700492 /* bpf_map_free_id() must be called first */
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700493 bpf_map_free_id(map, do_idr_lock);
Martin KaFai Lau78958fc2018-05-04 14:49:51 -0700494 btf_put(map->btf);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700495 INIT_WORK(&map->work, bpf_map_free_deferred);
496 schedule_work(&map->work);
497 }
498}
499
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700500void bpf_map_put(struct bpf_map *map)
501{
502 __bpf_map_put(map, true);
503}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700504EXPORT_SYMBOL_GPL(bpf_map_put);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700505
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100506void bpf_map_put_with_uref(struct bpf_map *map)
507{
508 bpf_map_put_uref(map);
509 bpf_map_put(map);
510}
511
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700512static int bpf_map_release(struct inode *inode, struct file *filp)
513{
Daniel Borkmann61d1b6a2016-06-15 22:47:12 +0200514 struct bpf_map *map = filp->private_data;
515
516 if (map->ops->map_release)
517 map->ops->map_release(map, filp);
518
519 bpf_map_put_with_uref(map);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700520 return 0;
521}
522
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200523static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
524{
525 fmode_t mode = f.file->f_mode;
526
527 /* Our file permissions may have been overridden by global
528 * map permissions facing syscall side.
529 */
530 if (READ_ONCE(map->frozen))
531 mode &= ~FMODE_CAN_WRITE;
532 return mode;
533}
534
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100535#ifdef CONFIG_PROC_FS
536static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
537{
538 const struct bpf_map *map = filp->private_data;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100539 const struct bpf_array *array;
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100540 u32 type = 0, jited = 0;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100541
542 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
543 array = container_of(map, struct bpf_array, map);
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100544 type = array->aux->type;
545 jited = array->aux->jited;
Daniel Borkmann21116b72016-11-26 01:28:07 +0100546 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100547
548 seq_printf(m,
549 "map_type:\t%u\n"
550 "key_size:\t%u\n"
551 "value_size:\t%u\n"
Daniel Borkmann322cea22016-03-25 00:30:25 +0100552 "max_entries:\t%u\n"
Daniel Borkmann21116b72016-11-26 01:28:07 +0100553 "map_flags:\t%#x\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +0200554 "memlock:\t%llu\n"
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200555 "map_id:\t%u\n"
556 "frozen:\t%u\n",
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100557 map->map_type,
558 map->key_size,
559 map->value_size,
Daniel Borkmann322cea22016-03-25 00:30:25 +0100560 map->max_entries,
Daniel Borkmann21116b72016-11-26 01:28:07 +0100561 map->map_flags,
Roman Gushchin3539b962019-05-29 18:03:57 -0700562 map->memory.pages * 1ULL << PAGE_SHIFT,
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200563 map->id,
564 READ_ONCE(map->frozen));
Daniel Borkmann2beee5f2019-11-22 21:07:56 +0100565 if (type) {
566 seq_printf(m, "owner_prog_type:\t%u\n", type);
567 seq_printf(m, "owner_jited:\t%u\n", jited);
Daniel Borkmann9780c0a2017-07-02 02:13:28 +0200568 }
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100569}
570#endif
571
Chenbo Feng6e71b042017-10-18 13:00:22 -0700572static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
573 loff_t *ppos)
574{
575 /* We need this handler such that alloc_file() enables
576 * f_mode with FMODE_CAN_READ.
577 */
578 return -EINVAL;
579}
580
581static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
582 size_t siz, loff_t *ppos)
583{
584 /* We need this handler such that alloc_file() enables
585 * f_mode with FMODE_CAN_WRITE.
586 */
587 return -EINVAL;
588}
589
Andrii Nakryikofc970222019-11-17 09:28:04 -0800590/* called for any extra memory-mapped regions (except initial) */
591static void bpf_map_mmap_open(struct vm_area_struct *vma)
592{
593 struct bpf_map *map = vma->vm_file->private_data;
594
595 bpf_map_inc_with_uref(map);
596
597 if (vma->vm_flags & VM_WRITE) {
598 mutex_lock(&map->freeze_mutex);
599 map->writecnt++;
600 mutex_unlock(&map->freeze_mutex);
601 }
602}
603
604/* called for all unmapped memory region (including initial) */
605static void bpf_map_mmap_close(struct vm_area_struct *vma)
606{
607 struct bpf_map *map = vma->vm_file->private_data;
608
609 if (vma->vm_flags & VM_WRITE) {
610 mutex_lock(&map->freeze_mutex);
611 map->writecnt--;
612 mutex_unlock(&map->freeze_mutex);
613 }
614
615 bpf_map_put_with_uref(map);
616}
617
618static const struct vm_operations_struct bpf_map_default_vmops = {
619 .open = bpf_map_mmap_open,
620 .close = bpf_map_mmap_close,
621};
622
623static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
624{
625 struct bpf_map *map = filp->private_data;
626 int err;
627
628 if (!map->ops->map_mmap || map_value_has_spin_lock(map))
629 return -ENOTSUPP;
630
631 if (!(vma->vm_flags & VM_SHARED))
632 return -EINVAL;
633
634 mutex_lock(&map->freeze_mutex);
635
636 if ((vma->vm_flags & VM_WRITE) && map->frozen) {
637 err = -EPERM;
638 goto out;
639 }
640
641 /* set default open/close callbacks */
642 vma->vm_ops = &bpf_map_default_vmops;
643 vma->vm_private_data = map;
644
645 err = map->ops->map_mmap(map, vma);
646 if (err)
647 goto out;
648
649 bpf_map_inc_with_uref(map);
650
651 if (vma->vm_flags & VM_WRITE)
652 map->writecnt++;
653out:
654 mutex_unlock(&map->freeze_mutex);
655 return err;
656}
657
Chenbo Fengf66e4482017-10-18 13:00:26 -0700658const struct file_operations bpf_map_fops = {
Daniel Borkmannf99bf202015-11-19 11:56:22 +0100659#ifdef CONFIG_PROC_FS
660 .show_fdinfo = bpf_map_show_fdinfo,
661#endif
662 .release = bpf_map_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700663 .read = bpf_dummy_read,
664 .write = bpf_dummy_write,
Andrii Nakryikofc970222019-11-17 09:28:04 -0800665 .mmap = bpf_map_mmap,
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700666};
667
Chenbo Feng6e71b042017-10-18 13:00:22 -0700668int bpf_map_new_fd(struct bpf_map *map, int flags)
Daniel Borkmannaa797812015-10-29 14:58:06 +0100669{
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700670 int ret;
671
672 ret = security_bpf_map(map, OPEN_FMODE(flags));
673 if (ret < 0)
674 return ret;
675
Daniel Borkmannaa797812015-10-29 14:58:06 +0100676 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
Chenbo Feng6e71b042017-10-18 13:00:22 -0700677 flags | O_CLOEXEC);
678}
679
680int bpf_get_file_flag(int flags)
681{
682 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
683 return -EINVAL;
684 if (flags & BPF_F_RDONLY)
685 return O_RDONLY;
686 if (flags & BPF_F_WRONLY)
687 return O_WRONLY;
688 return O_RDWR;
Daniel Borkmannaa797812015-10-29 14:58:06 +0100689}
690
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700691/* helper macro to check that unused fields 'union bpf_attr' are zero */
692#define CHECK_ATTR(CMD) \
693 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
694 sizeof(attr->CMD##_LAST_FIELD), 0, \
695 sizeof(*attr) - \
696 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
697 sizeof(attr->CMD##_LAST_FIELD)) != NULL
698
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700699/* dst and src must have at least "size" number of bytes.
700 * Return strlen on success and < 0 on error.
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700701 */
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700702int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700703{
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700704 const char *end = src + size;
705 const char *orig_src = src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700706
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700707 memset(dst, 0, size);
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200708 /* Copy all isalnum(), '_' and '.' chars. */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700709 while (src < end && *src) {
Daniel Borkmann3e0ddc4f2019-04-09 23:20:07 +0200710 if (!isalnum(*src) &&
711 *src != '_' && *src != '.')
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700712 return -EINVAL;
713 *dst++ = *src++;
714 }
715
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700716 /* No '\0' found in "size" number of bytes */
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700717 if (src == end)
718 return -EINVAL;
719
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700720 return src - orig_src;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -0700721}
722
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200723int map_check_no_btf(const struct bpf_map *map,
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800724 const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200725 const struct btf_type *key_type,
726 const struct btf_type *value_type)
727{
728 return -ENOTSUPP;
729}
730
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800731static int map_check_btf(struct bpf_map *map, const struct btf *btf,
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200732 u32 btf_key_id, u32 btf_value_id)
733{
734 const struct btf_type *key_type, *value_type;
735 u32 key_size, value_size;
736 int ret = 0;
737
Daniel Borkmann2824ecb2019-04-09 23:20:10 +0200738 /* Some maps allow key to be unspecified. */
739 if (btf_key_id) {
740 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
741 if (!key_type || key_size != map->key_size)
742 return -EINVAL;
743 } else {
744 key_type = btf_type_by_id(btf, 0);
745 if (!map->ops->map_check_btf)
746 return -EINVAL;
747 }
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200748
749 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
750 if (!value_type || value_size != map->value_size)
751 return -EINVAL;
752
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800753 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
754
755 if (map_value_has_spin_lock(map)) {
Daniel Borkmann591fe982019-04-09 23:20:05 +0200756 if (map->map_flags & BPF_F_RDONLY_PROG)
757 return -EACCES;
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800758 if (map->map_type != BPF_MAP_TYPE_HASH &&
Alexei Starovoitove16d2f12019-01-31 15:40:05 -0800759 map->map_type != BPF_MAP_TYPE_ARRAY &&
Martin KaFai Lau6ac99e82019-04-26 16:39:39 -0700760 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
761 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
Alexei Starovoitovd83525c2019-01-31 15:40:04 -0800762 return -ENOTSUPP;
763 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
764 map->value_size) {
765 WARN_ONCE(1,
766 "verifier bug spin_lock_off %d value_size %d\n",
767 map->spin_lock_off, map->value_size);
768 return -EFAULT;
769 }
770 }
771
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200772 if (map->ops->map_check_btf)
Roman Gushchin1b2b2342018-12-10 15:43:00 -0800773 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
Daniel Borkmanne8d2bec2018-08-12 01:59:17 +0200774
775 return ret;
776}
777
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800778#define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700779/* called via syscall */
780static int map_create(union bpf_attr *attr)
781{
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700782 int numa_node = bpf_map_attr_numa_node(attr);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700783 struct bpf_map_memory mem;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700784 struct bpf_map *map;
Chenbo Feng6e71b042017-10-18 13:00:22 -0700785 int f_flags;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700786 int err;
787
788 err = CHECK_ATTR(BPF_MAP_CREATE);
789 if (err)
790 return -EINVAL;
791
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800792 if (attr->btf_vmlinux_value_type_id) {
793 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
794 attr->btf_key_type_id || attr->btf_value_type_id)
795 return -EINVAL;
796 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
797 return -EINVAL;
798 }
799
Chenbo Feng6e71b042017-10-18 13:00:22 -0700800 f_flags = bpf_get_file_flag(attr->map_flags);
801 if (f_flags < 0)
802 return f_flags;
803
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700804 if (numa_node != NUMA_NO_NODE &&
Eric Dumazet96e5ae42017-09-04 22:41:02 -0700805 ((unsigned int)numa_node >= nr_node_ids ||
806 !node_online(numa_node)))
Martin KaFai Lau96eabe72017-08-18 11:28:00 -0700807 return -EINVAL;
808
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700809 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
810 map = find_and_alloc_map(attr);
811 if (IS_ERR(map))
812 return PTR_ERR(map);
813
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -0700814 err = bpf_obj_name_cpy(map->name, attr->map_name,
815 sizeof(attr->map_name));
816 if (err < 0)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700817 goto free_map;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -0700818
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800819 atomic64_set(&map->refcnt, 1);
820 atomic64_set(&map->usercnt, 1);
Andrii Nakryikofc970222019-11-17 09:28:04 -0800821 mutex_init(&map->freeze_mutex);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700822
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800823 map->spin_lock_off = -EINVAL;
824 if (attr->btf_key_type_id || attr->btf_value_type_id ||
825 /* Even the map's value is a kernel's struct,
826 * the bpf_prog.o must have BTF to begin with
827 * to figure out the corresponding kernel's
828 * counter part. Thus, attr->btf_fd has
829 * to be valid also.
830 */
831 attr->btf_vmlinux_value_type_id) {
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700832 struct btf *btf;
833
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700834 btf = btf_get_by_fd(attr->btf_fd);
835 if (IS_ERR(btf)) {
836 err = PTR_ERR(btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700837 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700838 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800839 map->btf = btf;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700840
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800841 if (attr->btf_value_type_id) {
842 err = map_check_btf(map, btf, attr->btf_key_type_id,
843 attr->btf_value_type_id);
844 if (err)
845 goto free_map;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700846 }
847
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -0700848 map->btf_key_type_id = attr->btf_key_type_id;
849 map->btf_value_type_id = attr->btf_value_type_id;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800850 map->btf_vmlinux_value_type_id =
851 attr->btf_vmlinux_value_type_id;
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700852 }
853
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700854 err = security_bpf_map_alloc(map);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -0700855 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700856 goto free_map;
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700857
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700858 err = bpf_map_alloc_id(map);
859 if (err)
Roman Gushchinb936ca62019-05-29 18:03:58 -0700860 goto free_map_sec;
Martin KaFai Lauf3f1c052017-06-05 12:15:47 -0700861
Chenbo Feng6e71b042017-10-18 13:00:22 -0700862 err = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700863 if (err < 0) {
864 /* failed to allocate fd.
Peng Sun352d20d2019-02-27 22:36:25 +0800865 * bpf_map_put_with_uref() is needed because the above
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700866 * bpf_map_alloc_id() has published the map
867 * to the userspace and the userspace may
868 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
869 */
Peng Sun352d20d2019-02-27 22:36:25 +0800870 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700871 return err;
872 }
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700873
874 return err;
875
Chenbo Fengafdb09c2017-10-18 13:00:24 -0700876free_map_sec:
877 security_bpf_map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700878free_map:
Martin KaFai Laua26ca7c2018-04-18 15:56:03 -0700879 btf_put(map->btf);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700880 bpf_map_charge_move(&mem, &map->memory);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700881 map->ops->map_free(map);
Roman Gushchinb936ca62019-05-29 18:03:58 -0700882 bpf_map_charge_finish(&mem);
Alexei Starovoitov99c55f72014-09-26 00:16:57 -0700883 return err;
884}
885
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700886/* if error is returned, fd is released.
887 * On success caller should complete fd access with matching fdput()
888 */
Daniel Borkmannc2101292015-10-29 14:58:07 +0100889struct bpf_map *__bpf_map_get(struct fd f)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700890{
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700891 if (!f.file)
892 return ERR_PTR(-EBADF);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700893 if (f.file->f_op != &bpf_map_fops) {
894 fdput(f);
895 return ERR_PTR(-EINVAL);
896 }
897
Daniel Borkmannc2101292015-10-29 14:58:07 +0100898 return f.file->private_data;
899}
900
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800901void bpf_map_inc(struct bpf_map *map)
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100902{
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800903 atomic64_inc(&map->refcnt);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100904}
Jakub Kicinski630a4d32018-05-03 18:37:09 -0700905EXPORT_SYMBOL_GPL(bpf_map_inc);
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100906
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800907void bpf_map_inc_with_uref(struct bpf_map *map)
908{
909 atomic64_inc(&map->refcnt);
910 atomic64_inc(&map->usercnt);
911}
912EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
913
Daniel Borkmannc9da1612015-11-24 21:28:15 +0100914struct bpf_map *bpf_map_get_with_uref(u32 ufd)
Daniel Borkmannc2101292015-10-29 14:58:07 +0100915{
916 struct fd f = fdget(ufd);
917 struct bpf_map *map;
918
919 map = __bpf_map_get(f);
920 if (IS_ERR(map))
921 return map;
922
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800923 bpf_map_inc_with_uref(map);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100924 fdput(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700925
926 return map;
927}
928
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700929/* map_idr_lock should have been held */
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800930static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700931{
932 int refold;
933
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800934 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700935 if (!refold)
936 return ERR_PTR(-ENOENT);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700937 if (uref)
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800938 atomic64_inc(&map->usercnt);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -0700939
940 return map;
941}
942
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800943struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700944{
945 spin_lock_bh(&map_idr_lock);
Andrii Nakryiko1e0bd5a2019-11-17 09:28:02 -0800946 map = __bpf_map_inc_not_zero(map, false);
Stanislav Fomichevb0e47012019-08-14 10:37:48 -0700947 spin_unlock_bh(&map_idr_lock);
948
949 return map;
950}
951EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
952
Alexei Starovoitovb8cdc052016-03-09 18:56:49 -0800953int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
954{
955 return -ENOTSUPP;
956}
957
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +0200958static void *__bpf_copy_key(void __user *ukey, u64 key_size)
959{
960 if (key_size)
961 return memdup_user(ukey, key_size);
962
963 if (ukey)
964 return ERR_PTR(-EINVAL);
965
966 return NULL;
967}
968
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700969/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800970#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700971
972static int map_lookup_elem(union bpf_attr *attr)
973{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +0100974 void __user *ukey = u64_to_user_ptr(attr->key);
975 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700976 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700977 struct bpf_map *map;
Brian Vazquez15c14a32020-01-15 10:43:00 -0800978 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -0800979 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +0200980 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700981 int err;
982
983 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
984 return -EINVAL;
985
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800986 if (attr->flags & ~BPF_F_LOCK)
987 return -EINVAL;
988
Daniel Borkmann592867b2015-09-08 18:00:09 +0200989 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +0100990 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -0700991 if (IS_ERR(map))
992 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +0200993 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -0700994 err = -EPERM;
995 goto err_put;
996 }
997
Alexei Starovoitov96049f32019-01-31 15:40:09 -0800998 if ((attr->flags & BPF_F_LOCK) &&
999 !map_value_has_spin_lock(map)) {
1000 err = -EINVAL;
1001 goto err_put;
1002 }
1003
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001004 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001005 if (IS_ERR(key)) {
1006 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001007 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001008 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001009
Brian Vazquez15c14a32020-01-15 10:43:00 -08001010 value_size = bpf_map_value_size(map);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001011
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001012 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001013 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001014 if (!value)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001015 goto free_key;
1016
Brian Vazquez15c14a32020-01-15 10:43:00 -08001017 err = bpf_map_copy_value(map, key, value, attr->flags);
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001018 if (err)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001019 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001020
1021 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001022 if (copy_to_user(uvalue, value, value_size) != 0)
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001023 goto free_value;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001024
1025 err = 0;
1026
Alexei Starovoitov8ebe6672015-01-22 17:11:08 -08001027free_value:
1028 kfree(value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001029free_key:
1030 kfree(key);
1031err_put:
1032 fdput(f);
1033 return err;
1034}
1035
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001036
Alexei Starovoitov3274f522014-11-13 17:36:44 -08001037#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001038
1039static int map_update_elem(union bpf_attr *attr)
1040{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001041 void __user *ukey = u64_to_user_ptr(attr->key);
1042 void __user *uvalue = u64_to_user_ptr(attr->value);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001043 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001044 struct bpf_map *map;
1045 void *key, *value;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001046 u32 value_size;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001047 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001048 int err;
1049
1050 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1051 return -EINVAL;
1052
Daniel Borkmann592867b2015-09-08 18:00:09 +02001053 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001054 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001055 if (IS_ERR(map))
1056 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001057 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001058 err = -EPERM;
1059 goto err_put;
1060 }
1061
Alexei Starovoitov96049f32019-01-31 15:40:09 -08001062 if ((attr->flags & BPF_F_LOCK) &&
1063 !map_value_has_spin_lock(map)) {
1064 err = -EINVAL;
1065 goto err_put;
1066 }
1067
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001068 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001069 if (IS_ERR(key)) {
1070 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001071 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001072 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001073
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001074 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
Martin KaFai Lau8f844932016-11-11 10:55:10 -08001075 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
Roman Gushchinb741f162018-09-28 14:45:43 +00001076 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1077 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001078 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1079 else
1080 value_size = map->value_size;
1081
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001082 err = -ENOMEM;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001083 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001084 if (!value)
1085 goto free_key;
1086
1087 err = -EFAULT;
Alexei Starovoitov15a07b32016-02-01 22:39:55 -08001088 if (copy_from_user(value, uvalue, value_size) != 0)
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001089 goto free_value;
1090
Brian Vazquez15c14a32020-01-15 10:43:00 -08001091 err = bpf_map_update_value(map, f, key, value, attr->flags);
Jesper Dangaard Brouer6710e112017-10-16 12:19:28 +02001092
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001093free_value:
1094 kfree(value);
1095free_key:
1096 kfree(key);
1097err_put:
1098 fdput(f);
1099 return err;
1100}
1101
1102#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1103
1104static int map_delete_elem(union bpf_attr *attr)
1105{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001106 void __user *ukey = u64_to_user_ptr(attr->key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001107 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001108 struct bpf_map *map;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001109 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001110 void *key;
1111 int err;
1112
1113 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1114 return -EINVAL;
1115
Daniel Borkmann592867b2015-09-08 18:00:09 +02001116 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001117 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001118 if (IS_ERR(map))
1119 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001120 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001121 err = -EPERM;
1122 goto err_put;
1123 }
1124
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001125 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001126 if (IS_ERR(key)) {
1127 err = PTR_ERR(key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001128 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001129 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001130
Jakub Kicinskia3884572018-01-11 20:29:09 -08001131 if (bpf_map_is_dev_bound(map)) {
1132 err = bpf_map_offload_delete_elem(map, key);
1133 goto out;
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08001134 } else if (IS_FD_PROG_ARRAY(map) ||
1135 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1136 /* These maps require sleepable context */
Daniel Borkmannda765a22019-11-22 21:07:58 +01001137 err = map->ops->map_delete_elem(map, key);
1138 goto out;
Jakub Kicinskia3884572018-01-11 20:29:09 -08001139 }
1140
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001141 preempt_disable();
1142 __this_cpu_inc(bpf_prog_active);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001143 rcu_read_lock();
1144 err = map->ops->map_delete_elem(map, key);
1145 rcu_read_unlock();
Alexei Starovoitovb121d1e2016-03-07 21:57:13 -08001146 __this_cpu_dec(bpf_prog_active);
1147 preempt_enable();
Daniel Colascione1ae80cf2018-10-12 03:54:27 -07001148 maybe_wait_bpf_programs(map);
Jakub Kicinskia3884572018-01-11 20:29:09 -08001149out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001150 kfree(key);
1151err_put:
1152 fdput(f);
1153 return err;
1154}
1155
1156/* last field in 'union bpf_attr' used by this command */
1157#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1158
1159static int map_get_next_key(union bpf_attr *attr)
1160{
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01001161 void __user *ukey = u64_to_user_ptr(attr->key);
1162 void __user *unext_key = u64_to_user_ptr(attr->next_key);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001163 int ufd = attr->map_fd;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001164 struct bpf_map *map;
1165 void *key, *next_key;
Daniel Borkmann592867b2015-09-08 18:00:09 +02001166 struct fd f;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001167 int err;
1168
1169 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1170 return -EINVAL;
1171
Daniel Borkmann592867b2015-09-08 18:00:09 +02001172 f = fdget(ufd);
Daniel Borkmannc2101292015-10-29 14:58:07 +01001173 map = __bpf_map_get(f);
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001174 if (IS_ERR(map))
1175 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001176 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
Chenbo Feng6e71b042017-10-18 13:00:22 -07001177 err = -EPERM;
1178 goto err_put;
1179 }
1180
Teng Qin8fe45922017-04-24 19:00:37 -07001181 if (ukey) {
Mauricio Vasquez Bc9d29f42018-10-18 15:16:14 +02001182 key = __bpf_copy_key(ukey, map->key_size);
Al Viroe4448ed2017-05-13 18:43:00 -04001183 if (IS_ERR(key)) {
1184 err = PTR_ERR(key);
Teng Qin8fe45922017-04-24 19:00:37 -07001185 goto err_put;
Al Viroe4448ed2017-05-13 18:43:00 -04001186 }
Teng Qin8fe45922017-04-24 19:00:37 -07001187 } else {
1188 key = NULL;
1189 }
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001190
1191 err = -ENOMEM;
1192 next_key = kmalloc(map->key_size, GFP_USER);
1193 if (!next_key)
1194 goto free_key;
1195
Jakub Kicinskia3884572018-01-11 20:29:09 -08001196 if (bpf_map_is_dev_bound(map)) {
1197 err = bpf_map_offload_get_next_key(map, key, next_key);
1198 goto out;
1199 }
1200
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001201 rcu_read_lock();
1202 err = map->ops->map_get_next_key(map, key, next_key);
1203 rcu_read_unlock();
Jakub Kicinskia3884572018-01-11 20:29:09 -08001204out:
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07001205 if (err)
1206 goto free_next_key;
1207
1208 err = -EFAULT;
1209 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1210 goto free_next_key;
1211
1212 err = 0;
1213
1214free_next_key:
1215 kfree(next_key);
1216free_key:
1217 kfree(key);
1218err_put:
1219 fdput(f);
1220 return err;
1221}
1222
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001223int generic_map_delete_batch(struct bpf_map *map,
1224 const union bpf_attr *attr,
1225 union bpf_attr __user *uattr)
1226{
1227 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1228 u32 cp, max_count;
1229 int err = 0;
1230 void *key;
1231
1232 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1233 return -EINVAL;
1234
1235 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1236 !map_value_has_spin_lock(map)) {
1237 return -EINVAL;
1238 }
1239
1240 max_count = attr->batch.count;
1241 if (!max_count)
1242 return 0;
1243
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001244 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1245 if (!key)
1246 return -ENOMEM;
1247
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001248 for (cp = 0; cp < max_count; cp++) {
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001249 err = -EFAULT;
1250 if (copy_from_user(key, keys + cp * map->key_size,
1251 map->key_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001252 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001253
1254 if (bpf_map_is_dev_bound(map)) {
1255 err = bpf_map_offload_delete_elem(map, key);
1256 break;
1257 }
1258
1259 preempt_disable();
1260 __this_cpu_inc(bpf_prog_active);
1261 rcu_read_lock();
1262 err = map->ops->map_delete_elem(map, key);
1263 rcu_read_unlock();
1264 __this_cpu_dec(bpf_prog_active);
1265 preempt_enable();
1266 maybe_wait_bpf_programs(map);
1267 if (err)
1268 break;
1269 }
1270 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1271 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001272
1273 kfree(key);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001274 return err;
1275}
1276
1277int generic_map_update_batch(struct bpf_map *map,
1278 const union bpf_attr *attr,
1279 union bpf_attr __user *uattr)
1280{
1281 void __user *values = u64_to_user_ptr(attr->batch.values);
1282 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1283 u32 value_size, cp, max_count;
1284 int ufd = attr->map_fd;
1285 void *key, *value;
1286 struct fd f;
1287 int err = 0;
1288
1289 f = fdget(ufd);
1290 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1291 return -EINVAL;
1292
1293 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1294 !map_value_has_spin_lock(map)) {
1295 return -EINVAL;
1296 }
1297
1298 value_size = bpf_map_value_size(map);
1299
1300 max_count = attr->batch.count;
1301 if (!max_count)
1302 return 0;
1303
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001304 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1305 if (!key)
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001306 return -ENOMEM;
1307
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001308 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1309 if (!value) {
1310 kfree(key);
1311 return -ENOMEM;
1312 }
1313
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001314 for (cp = 0; cp < max_count; cp++) {
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001315 err = -EFAULT;
Brian Vazquez2e3a94a2020-01-19 11:40:40 -08001316 if (copy_from_user(key, keys + cp * map->key_size,
1317 map->key_size) ||
1318 copy_from_user(value, values + cp * value_size, value_size))
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08001319 break;
1320
1321 err = bpf_map_update_value(map, f, key, value,
1322 attr->batch.elem_flags);
1323
1324 if (err)
1325 break;
1326 }
1327
1328 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1329 err = -EFAULT;
1330
1331 kfree(value);
1332 kfree(key);
1333 return err;
1334}
1335
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001336#define MAP_LOOKUP_RETRIES 3
1337
1338int generic_map_lookup_batch(struct bpf_map *map,
1339 const union bpf_attr *attr,
1340 union bpf_attr __user *uattr)
1341{
1342 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1343 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1344 void __user *values = u64_to_user_ptr(attr->batch.values);
1345 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1346 void *buf, *buf_prevkey, *prev_key, *key, *value;
1347 int err, retry = MAP_LOOKUP_RETRIES;
1348 u32 value_size, cp, max_count;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001349
1350 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1351 return -EINVAL;
1352
1353 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1354 !map_value_has_spin_lock(map))
1355 return -EINVAL;
1356
1357 value_size = bpf_map_value_size(map);
1358
1359 max_count = attr->batch.count;
1360 if (!max_count)
1361 return 0;
1362
1363 if (put_user(0, &uattr->batch.count))
1364 return -EFAULT;
1365
1366 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1367 if (!buf_prevkey)
1368 return -ENOMEM;
1369
1370 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1371 if (!buf) {
1372 kvfree(buf_prevkey);
1373 return -ENOMEM;
1374 }
1375
1376 err = -EFAULT;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08001377 prev_key = NULL;
1378 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1379 goto free_buf;
1380 key = buf;
1381 value = key + map->key_size;
1382 if (ubatch)
1383 prev_key = buf_prevkey;
1384
1385 for (cp = 0; cp < max_count;) {
1386 rcu_read_lock();
1387 err = map->ops->map_get_next_key(map, prev_key, key);
1388 rcu_read_unlock();
1389 if (err)
1390 break;
1391 err = bpf_map_copy_value(map, key, value,
1392 attr->batch.elem_flags);
1393
1394 if (err == -ENOENT) {
1395 if (retry) {
1396 retry--;
1397 continue;
1398 }
1399 err = -EINTR;
1400 break;
1401 }
1402
1403 if (err)
1404 goto free_buf;
1405
1406 if (copy_to_user(keys + cp * map->key_size, key,
1407 map->key_size)) {
1408 err = -EFAULT;
1409 goto free_buf;
1410 }
1411 if (copy_to_user(values + cp * value_size, value, value_size)) {
1412 err = -EFAULT;
1413 goto free_buf;
1414 }
1415
1416 if (!prev_key)
1417 prev_key = buf_prevkey;
1418
1419 swap(prev_key, key);
1420 retry = MAP_LOOKUP_RETRIES;
1421 cp++;
1422 }
1423
1424 if (err == -EFAULT)
1425 goto free_buf;
1426
1427 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1428 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1429 err = -EFAULT;
1430
1431free_buf:
1432 kfree(buf_prevkey);
1433 kfree(buf);
1434 return err;
1435}
1436
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001437#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1438
1439static int map_lookup_and_delete_elem(union bpf_attr *attr)
1440{
1441 void __user *ukey = u64_to_user_ptr(attr->key);
1442 void __user *uvalue = u64_to_user_ptr(attr->value);
1443 int ufd = attr->map_fd;
1444 struct bpf_map *map;
Alexei Starovoitov540fefc2018-10-19 13:52:38 -07001445 void *key, *value;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001446 u32 value_size;
1447 struct fd f;
1448 int err;
1449
1450 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1451 return -EINVAL;
1452
1453 f = fdget(ufd);
1454 map = __bpf_map_get(f);
1455 if (IS_ERR(map))
1456 return PTR_ERR(map);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001457 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02001458 err = -EPERM;
1459 goto err_put;
1460 }
1461
1462 key = __bpf_copy_key(ukey, map->key_size);
1463 if (IS_ERR(key)) {
1464 err = PTR_ERR(key);
1465 goto err_put;
1466 }
1467
1468 value_size = map->value_size;
1469
1470 err = -ENOMEM;
1471 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1472 if (!value)
1473 goto free_key;
1474
1475 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1476 map->map_type == BPF_MAP_TYPE_STACK) {
1477 err = map->ops->map_pop_elem(map, value);
1478 } else {
1479 err = -ENOTSUPP;
1480 }
1481
1482 if (err)
1483 goto free_value;
1484
1485 if (copy_to_user(uvalue, value, value_size) != 0)
1486 goto free_value;
1487
1488 err = 0;
1489
1490free_value:
1491 kfree(value);
1492free_key:
1493 kfree(key);
1494err_put:
1495 fdput(f);
1496 return err;
1497}
1498
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001499#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1500
1501static int map_freeze(const union bpf_attr *attr)
1502{
1503 int err = 0, ufd = attr->map_fd;
1504 struct bpf_map *map;
1505 struct fd f;
1506
1507 if (CHECK_ATTR(BPF_MAP_FREEZE))
1508 return -EINVAL;
1509
1510 f = fdget(ufd);
1511 map = __bpf_map_get(f);
1512 if (IS_ERR(map))
1513 return PTR_ERR(map);
Andrii Nakryikofc970222019-11-17 09:28:04 -08001514
Martin KaFai Lau849b4d92020-03-04 17:34:54 -08001515 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1516 fdput(f);
1517 return -ENOTSUPP;
1518 }
1519
Andrii Nakryikofc970222019-11-17 09:28:04 -08001520 mutex_lock(&map->freeze_mutex);
1521
1522 if (map->writecnt) {
1523 err = -EBUSY;
1524 goto err_put;
1525 }
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001526 if (READ_ONCE(map->frozen)) {
1527 err = -EBUSY;
1528 goto err_put;
1529 }
1530 if (!capable(CAP_SYS_ADMIN)) {
1531 err = -EPERM;
1532 goto err_put;
1533 }
1534
1535 WRITE_ONCE(map->frozen, true);
1536err_put:
Andrii Nakryikofc970222019-11-17 09:28:04 -08001537 mutex_unlock(&map->freeze_mutex);
Daniel Borkmann87df15d2019-04-09 23:20:06 +02001538 fdput(f);
1539 return err;
1540}
1541
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001542static const struct bpf_prog_ops * const bpf_prog_types[] = {
Alexei Starovoitov91cc1a92019-11-14 10:57:15 -08001543#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
Jakub Kicinski7de16e32017-10-16 16:40:53 -07001544 [_id] = & _name ## _prog_ops,
1545#define BPF_MAP_TYPE(_id, _ops)
1546#include <linux/bpf_types.h>
1547#undef BPF_PROG_TYPE
1548#undef BPF_MAP_TYPE
1549};
1550
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001551static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1552{
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001553 const struct bpf_prog_ops *ops;
1554
1555 if (type >= ARRAY_SIZE(bpf_prog_types))
1556 return -EINVAL;
1557 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1558 ops = bpf_prog_types[type];
1559 if (!ops)
Johannes Bergbe9370a2017-04-11 15:34:57 +02001560 return -EINVAL;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001561
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001562 if (!bpf_prog_is_dev_bound(prog->aux))
Daniel Borkmannd0f1a452018-05-04 02:13:57 +02001563 prog->aux->ops = ops;
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07001564 else
1565 prog->aux->ops = &bpf_offload_prog_ops;
Johannes Bergbe9370a2017-04-11 15:34:57 +02001566 prog->type = type;
1567 return 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001568}
1569
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001570enum bpf_audit {
1571 BPF_AUDIT_LOAD,
1572 BPF_AUDIT_UNLOAD,
1573 BPF_AUDIT_MAX,
1574};
1575
1576static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1577 [BPF_AUDIT_LOAD] = "LOAD",
1578 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1579};
1580
1581static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1582{
1583 struct audit_context *ctx = NULL;
1584 struct audit_buffer *ab;
1585
1586 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1587 return;
1588 if (audit_enabled == AUDIT_OFF)
1589 return;
1590 if (op == BPF_AUDIT_LOAD)
1591 ctx = audit_context();
1592 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1593 if (unlikely(!ab))
1594 return;
1595 audit_log_format(ab, "prog-id=%u op=%s",
1596 prog->aux->id, bpf_audit_str[op]);
1597 audit_log_end(ab);
1598}
1599
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001600int __bpf_prog_charge(struct user_struct *user, u32 pages)
1601{
1602 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1603 unsigned long user_bufs;
1604
1605 if (user) {
1606 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1607 if (user_bufs > memlock_limit) {
1608 atomic_long_sub(pages, &user->locked_vm);
1609 return -EPERM;
1610 }
1611 }
1612
1613 return 0;
1614}
1615
1616void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1617{
1618 if (user)
1619 atomic_long_sub(pages, &user->locked_vm);
1620}
1621
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001622static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1623{
1624 struct user_struct *user = get_current_user();
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001625 int ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001626
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001627 ret = __bpf_prog_charge(user, prog->pages);
1628 if (ret) {
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001629 free_uid(user);
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001630 return ret;
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001631 }
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001632
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001633 prog->aux->user = user;
1634 return 0;
1635}
1636
1637static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1638{
1639 struct user_struct *user = prog->aux->user;
1640
Daniel Borkmann5ccb0712016-12-18 01:52:58 +01001641 __bpf_prog_uncharge(user, prog->pages);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001642 free_uid(user);
1643}
1644
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001645static int bpf_prog_alloc_id(struct bpf_prog *prog)
1646{
1647 int id;
1648
Shaohua Lib76354c2018-03-27 11:53:21 -07001649 idr_preload(GFP_KERNEL);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001650 spin_lock_bh(&prog_idr_lock);
1651 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1652 if (id > 0)
1653 prog->aux->id = id;
1654 spin_unlock_bh(&prog_idr_lock);
Shaohua Lib76354c2018-03-27 11:53:21 -07001655 idr_preload_end();
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001656
1657 /* id is in [1, INT_MAX) */
1658 if (WARN_ON_ONCE(!id))
1659 return -ENOSPC;
1660
1661 return id > 0 ? 0 : id;
1662}
1663
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001664void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001665{
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001666 /* cBPF to eBPF migrations are currently not in the idr store.
1667 * Offloaded programs are removed from the store when their device
1668 * disappears - even if someone grabs an fd to them they are unusable,
1669 * simply waiting for refcnt to drop to be freed.
1670 */
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001671 if (!prog->aux->id)
1672 return;
1673
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001674 if (do_idr_lock)
1675 spin_lock_bh(&prog_idr_lock);
1676 else
1677 __acquire(&prog_idr_lock);
1678
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001679 idr_remove(&prog_idr, prog->aux->id);
Jakub Kicinskiad8ad792017-12-27 18:39:07 -08001680 prog->aux->id = 0;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001681
1682 if (do_idr_lock)
1683 spin_unlock_bh(&prog_idr_lock);
1684 else
1685 __release(&prog_idr_lock);
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07001686}
1687
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001688static void __bpf_prog_put_rcu(struct rcu_head *rcu)
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001689{
1690 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1691
Daniel Borkmann3b4d9eb2019-10-22 23:30:38 +02001692 kvfree(aux->func_info);
Alexei Starovoitov8c1b6e62019-11-14 10:57:16 -08001693 kfree(aux->func_info_aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07001694 bpf_prog_uncharge_memlock(aux->prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001695 security_bpf_prog_free(aux);
Alexei Starovoitovabf2e7d2015-05-28 19:26:02 -07001696 bpf_prog_free(aux->prog);
1697}
1698
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001699static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1700{
1701 bpf_prog_kallsyms_del_all(prog);
1702 btf_put(prog->aux->btf);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001703 bpf_prog_free_linfo(prog);
1704
1705 if (deferred)
1706 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1707 else
1708 __bpf_prog_put_rcu(&prog->aux->rcu);
1709}
1710
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001711static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001712{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001713 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
Song Liu6ee52e22019-01-17 08:15:15 -08001714 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01001715 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07001716 /* bpf_prog_free_id() must be called first */
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001717 bpf_prog_free_id(prog, do_idr_lock);
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02001718 __bpf_prog_put_noref(prog, true);
Daniel Borkmanna67edbf2017-01-25 02:28:18 +01001719 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001720}
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001721
1722void bpf_prog_put(struct bpf_prog *prog)
1723{
1724 __bpf_prog_put(prog, true);
1725}
Daniel Borkmanne2e9b652015-03-01 12:31:48 +01001726EXPORT_SYMBOL_GPL(bpf_prog_put);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001727
1728static int bpf_prog_release(struct inode *inode, struct file *filp)
1729{
1730 struct bpf_prog *prog = filp->private_data;
1731
Daniel Borkmann1aacde32016-06-30 17:24:43 +02001732 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001733 return 0;
1734}
1735
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001736static void bpf_prog_get_stats(const struct bpf_prog *prog,
1737 struct bpf_prog_stats *stats)
1738{
1739 u64 nsecs = 0, cnt = 0;
1740 int cpu;
1741
1742 for_each_possible_cpu(cpu) {
1743 const struct bpf_prog_stats *st;
1744 unsigned int start;
1745 u64 tnsecs, tcnt;
1746
1747 st = per_cpu_ptr(prog->aux->stats, cpu);
1748 do {
1749 start = u64_stats_fetch_begin_irq(&st->syncp);
1750 tnsecs = st->nsecs;
1751 tcnt = st->cnt;
1752 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1753 nsecs += tnsecs;
1754 cnt += tcnt;
1755 }
1756 stats->nsecs = nsecs;
1757 stats->cnt = cnt;
1758}
1759
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001760#ifdef CONFIG_PROC_FS
1761static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1762{
1763 const struct bpf_prog *prog = filp->private_data;
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001764 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001765 struct bpf_prog_stats stats;
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001766
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001767 bpf_prog_get_stats(prog, &stats);
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001768 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001769 seq_printf(m,
1770 "prog_type:\t%u\n"
1771 "prog_jited:\t%u\n"
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001772 "prog_tag:\t%s\n"
Daniel Borkmann4316b402018-06-02 23:06:34 +02001773 "memlock:\t%llu\n"
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001774 "prog_id:\t%u\n"
1775 "run_time_ns:\t%llu\n"
1776 "run_cnt:\t%llu\n",
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001777 prog->type,
1778 prog->jited,
Daniel Borkmannf1f77142017-01-13 23:38:15 +01001779 prog_tag,
Daniel Borkmann4316b402018-06-02 23:06:34 +02001780 prog->pages * 1ULL << PAGE_SHIFT,
Alexei Starovoitov492ecee2019-02-25 14:28:39 -08001781 prog->aux->id,
1782 stats.nsecs,
1783 stats.cnt);
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001784}
1785#endif
1786
Chenbo Fengf66e4482017-10-18 13:00:26 -07001787const struct file_operations bpf_prog_fops = {
Daniel Borkmann7bd509e2016-12-04 23:19:41 +01001788#ifdef CONFIG_PROC_FS
1789 .show_fdinfo = bpf_prog_show_fdinfo,
1790#endif
1791 .release = bpf_prog_release,
Chenbo Feng6e71b042017-10-18 13:00:22 -07001792 .read = bpf_dummy_read,
1793 .write = bpf_dummy_write,
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001794};
1795
Daniel Borkmannb2197752015-10-29 14:58:09 +01001796int bpf_prog_new_fd(struct bpf_prog *prog)
Daniel Borkmannaa797812015-10-29 14:58:06 +01001797{
Chenbo Fengafdb09c2017-10-18 13:00:24 -07001798 int ret;
1799
1800 ret = security_bpf_prog(prog);
1801 if (ret < 0)
1802 return ret;
1803
Daniel Borkmannaa797812015-10-29 14:58:06 +01001804 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1805 O_RDWR | O_CLOEXEC);
1806}
1807
Daniel Borkmann113214b2016-06-30 17:24:44 +02001808static struct bpf_prog *____bpf_prog_get(struct fd f)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001809{
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001810 if (!f.file)
1811 return ERR_PTR(-EBADF);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001812 if (f.file->f_op != &bpf_prog_fops) {
1813 fdput(f);
1814 return ERR_PTR(-EINVAL);
1815 }
1816
Daniel Borkmannc2101292015-10-29 14:58:07 +01001817 return f.file->private_data;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001818}
1819
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001820void bpf_prog_add(struct bpf_prog *prog, int i)
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001821{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001822 atomic64_add(i, &prog->aux->refcnt);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001823}
Brenden Blanco59d36562016-07-19 12:16:46 -07001824EXPORT_SYMBOL_GPL(bpf_prog_add);
1825
Daniel Borkmannc5405942016-11-09 22:02:34 +01001826void bpf_prog_sub(struct bpf_prog *prog, int i)
1827{
1828 /* Only to be used for undoing previous bpf_prog_add() in some
1829 * error path. We still know that another entity in our call
1830 * path holds a reference to the program, thus atomic_sub() can
1831 * be safely used in such cases!
1832 */
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001833 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
Daniel Borkmannc5405942016-11-09 22:02:34 +01001834}
1835EXPORT_SYMBOL_GPL(bpf_prog_sub);
1836
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001837void bpf_prog_inc(struct bpf_prog *prog)
Brenden Blanco59d36562016-07-19 12:16:46 -07001838{
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001839 atomic64_inc(&prog->aux->refcnt);
Brenden Blanco59d36562016-07-19 12:16:46 -07001840}
Daniel Borkmann97bc4022016-11-19 01:45:00 +01001841EXPORT_SYMBOL_GPL(bpf_prog_inc);
Alexei Starovoitov92117d82016-04-27 18:56:20 -07001842
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001843/* prog_idr_lock should have been held */
John Fastabenda6f6df62017-08-15 22:32:22 -07001844struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001845{
1846 int refold;
1847
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001848 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001849
1850 if (!refold)
1851 return ERR_PTR(-ENOENT);
1852
1853 return prog;
1854}
John Fastabenda6f6df62017-08-15 22:32:22 -07001855EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07001856
Al Viro040ee692017-12-02 20:20:38 -05001857bool bpf_prog_get_ok(struct bpf_prog *prog,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001858 enum bpf_prog_type *attach_type, bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001859{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001860 /* not an attachment, just a refcount inc, always allow */
1861 if (!attach_type)
1862 return true;
Jakub Kicinski248f3462017-11-03 13:56:20 -07001863
1864 if (prog->type != *attach_type)
1865 return false;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001866 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001867 return false;
1868
1869 return true;
1870}
1871
1872static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001873 bool attach_drv)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001874{
1875 struct fd f = fdget(ufd);
1876 struct bpf_prog *prog;
1877
Daniel Borkmann113214b2016-06-30 17:24:44 +02001878 prog = ____bpf_prog_get(f);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001879 if (IS_ERR(prog))
1880 return prog;
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001881 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
Daniel Borkmann113214b2016-06-30 17:24:44 +02001882 prog = ERR_PTR(-EINVAL);
1883 goto out;
1884 }
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001885
Andrii Nakryiko85192db2019-11-17 09:28:03 -08001886 bpf_prog_inc(prog);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001887out:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07001888 fdput(f);
1889 return prog;
1890}
Daniel Borkmann113214b2016-06-30 17:24:44 +02001891
1892struct bpf_prog *bpf_prog_get(u32 ufd)
1893{
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001894 return __bpf_prog_get(ufd, NULL, false);
Daniel Borkmann113214b2016-06-30 17:24:44 +02001895}
1896
Jakub Kicinski248f3462017-11-03 13:56:20 -07001897struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
Jakub Kicinski288b3de2017-11-20 15:21:54 -08001898 bool attach_drv)
Jakub Kicinski248f3462017-11-03 13:56:20 -07001899{
Alexei Starovoitov4d220ed2018-04-28 19:56:37 -07001900 return __bpf_prog_get(ufd, &type, attach_drv);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001901}
Jakub Kicinski6c8dfe22017-11-03 13:56:21 -07001902EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
Jakub Kicinski248f3462017-11-03 13:56:20 -07001903
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001904/* Initially all BPF programs could be loaded w/o specifying
1905 * expected_attach_type. Later for some of them specifying expected_attach_type
1906 * at load time became required so that program could be validated properly.
1907 * Programs of types that are allowed to be loaded both w/ and w/o (for
1908 * backward compatibility) expected_attach_type, should have the default attach
1909 * type assigned to expected_attach_type for the latter case, so that it can be
1910 * validated later at attach time.
1911 *
1912 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1913 * prog type requires it but has some attach types that have to be backward
1914 * compatible.
1915 */
1916static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1917{
1918 switch (attr->prog_type) {
1919 case BPF_PROG_TYPE_CGROUP_SOCK:
1920 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1921 * exist so checking for non-zero is the way to go here.
1922 */
1923 if (!attr->expected_attach_type)
1924 attr->expected_attach_type =
1925 BPF_CGROUP_INET_SOCK_CREATE;
1926 break;
1927 }
1928}
1929
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001930static int
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07001931bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1932 enum bpf_attach_type expected_attach_type,
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08001933 u32 btf_id, u32 prog_fd)
Andrey Ignatov5e43f892018-03-30 15:08:00 -07001934{
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001935 if (btf_id) {
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001936 if (btf_id > BTF_MAX_TYPE)
1937 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001938
1939 switch (prog_type) {
1940 case BPF_PROG_TYPE_TRACING:
1941 case BPF_PROG_TYPE_STRUCT_OPS:
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001942 case BPF_PROG_TYPE_EXT:
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001943 break;
1944 default:
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001945 return -EINVAL;
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001946 }
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001947 }
1948
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001949 if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING &&
1950 prog_type != BPF_PROG_TYPE_EXT)
Martin KaFai Lau27ae79972020-01-08 16:35:03 -08001951 return -EINVAL;
1952
Alexei Starovoitovc108e3c2019-10-17 23:09:33 -07001953 switch (prog_type) {
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07001954 case BPF_PROG_TYPE_CGROUP_SOCK:
1955 switch (expected_attach_type) {
1956 case BPF_CGROUP_INET_SOCK_CREATE:
1957 case BPF_CGROUP_INET4_POST_BIND:
1958 case BPF_CGROUP_INET6_POST_BIND:
1959 return 0;
1960 default:
1961 return -EINVAL;
1962 }
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001963 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1964 switch (expected_attach_type) {
1965 case BPF_CGROUP_INET4_BIND:
1966 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001967 case BPF_CGROUP_INET4_CONNECT:
1968 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001969 case BPF_CGROUP_UDP4_SENDMSG:
1970 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02001971 case BPF_CGROUP_UDP4_RECVMSG:
1972 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001973 return 0;
1974 default:
1975 return -EINVAL;
1976 }
brakmo5cf1e912019-05-28 16:59:36 -07001977 case BPF_PROG_TYPE_CGROUP_SKB:
1978 switch (expected_attach_type) {
1979 case BPF_CGROUP_INET_INGRESS:
1980 case BPF_CGROUP_INET_EGRESS:
1981 return 0;
1982 default:
1983 return -EINVAL;
1984 }
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07001985 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1986 switch (expected_attach_type) {
1987 case BPF_CGROUP_SETSOCKOPT:
1988 case BPF_CGROUP_GETSOCKOPT:
1989 return 0;
1990 default:
1991 return -EINVAL;
1992 }
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08001993 case BPF_PROG_TYPE_EXT:
1994 if (expected_attach_type)
1995 return -EINVAL;
1996 /* fallthrough */
Andrey Ignatov4fbac772018-03-30 15:08:02 -07001997 default:
1998 return 0;
1999 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002000}
2001
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002002/* last field in 'union bpf_attr' used by this command */
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002003#define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002004
Yonghong Song838e9692018-11-19 15:29:11 -08002005static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002006{
2007 enum bpf_prog_type type = attr->prog_type;
2008 struct bpf_prog *prog;
2009 int err;
2010 char license[128];
2011 bool is_gpl;
2012
2013 if (CHECK_ATTR(BPF_PROG_LOAD))
2014 return -EINVAL;
2015
Jiong Wangc240eff2019-05-24 23:25:16 +01002016 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2017 BPF_F_ANY_ALIGNMENT |
Alexei Starovoitov10d274e2019-08-22 22:52:12 -07002018 BPF_F_TEST_STATE_FREQ |
Jiong Wangc240eff2019-05-24 23:25:16 +01002019 BPF_F_TEST_RND_HI32))
David S. Millere07b98d2017-05-10 11:38:07 -07002020 return -EINVAL;
2021
David Millere9ee9ef2018-11-30 21:08:14 -08002022 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2023 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2024 !capable(CAP_SYS_ADMIN))
2025 return -EPERM;
2026
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002027 /* copy eBPF program license from user space */
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002028 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002029 sizeof(license) - 1) < 0)
2030 return -EFAULT;
2031 license[sizeof(license) - 1] = 0;
2032
2033 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2034 is_gpl = license_is_gpl_compatible(license);
2035
Alexei Starovoitovc04c0d22019-04-01 21:27:45 -07002036 if (attr->insn_cnt == 0 ||
2037 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
Daniel Borkmannef0915c2016-12-07 01:15:44 +01002038 return -E2BIG;
Chenbo Feng80b7d812017-05-31 18:16:00 -07002039 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2040 type != BPF_PROG_TYPE_CGROUP_SKB &&
2041 !capable(CAP_SYS_ADMIN))
Alexei Starovoitov1be7f752015-10-07 22:23:21 -07002042 return -EPERM;
2043
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002044 bpf_prog_load_fixup_attach_type(attr);
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002045 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002046 attr->attach_btf_id,
2047 attr->attach_prog_fd))
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002048 return -EINVAL;
2049
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002050 /* plain bpf_prog allocation */
2051 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2052 if (!prog)
2053 return -ENOMEM;
2054
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002055 prog->expected_attach_type = attr->expected_attach_type;
Alexei Starovoitovccfe29e2019-10-15 20:24:58 -07002056 prog->aux->attach_btf_id = attr->attach_btf_id;
Alexei Starovoitov5b92a282019-11-14 10:57:17 -08002057 if (attr->attach_prog_fd) {
2058 struct bpf_prog *tgt_prog;
2059
2060 tgt_prog = bpf_prog_get(attr->attach_prog_fd);
2061 if (IS_ERR(tgt_prog)) {
2062 err = PTR_ERR(tgt_prog);
2063 goto free_prog_nouncharge;
2064 }
2065 prog->aux->linked_prog = tgt_prog;
2066 }
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002067
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002068 prog->aux->offload_requested = !!attr->prog_ifindex;
2069
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002070 err = security_bpf_prog_alloc(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002071 if (err)
2072 goto free_prog_nouncharge;
2073
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002074 err = bpf_prog_charge_memlock(prog);
2075 if (err)
2076 goto free_prog_sec;
2077
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002078 prog->len = attr->insn_cnt;
2079
2080 err = -EFAULT;
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002081 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
Daniel Borkmannaafe6ae2016-12-18 01:52:57 +01002082 bpf_prog_insn_size(prog)) != 0)
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002083 goto free_prog;
2084
2085 prog->orig_prog = NULL;
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002086 prog->jited = 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002087
Andrii Nakryiko85192db2019-11-17 09:28:03 -08002088 atomic64_set(&prog->aux->refcnt, 1);
Daniel Borkmanna91263d2015-09-30 01:41:50 +02002089 prog->gpl_compatible = is_gpl ? 1 : 0;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002090
Jakub Kicinski9a18eed2017-12-27 18:39:04 -08002091 if (bpf_prog_is_dev_bound(prog->aux)) {
Jakub Kicinskiab3f0062017-11-03 13:56:17 -07002092 err = bpf_prog_offload_init(prog, attr);
2093 if (err)
2094 goto free_prog;
2095 }
2096
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002097 /* find program type: socket_filter vs tracing_filter */
2098 err = find_prog_type(type, prog);
2099 if (err < 0)
2100 goto free_prog;
2101
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +02002102 prog->aux->load_time = ktime_get_boottime_ns();
Martin KaFai Lau8e7ae252020-03-13 18:02:09 -07002103 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2104 sizeof(attr->prog_name));
2105 if (err < 0)
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002106 goto free_prog;
2107
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002108 /* run eBPF verifier */
Yonghong Song838e9692018-11-19 15:29:11 -08002109 err = bpf_check(&prog, attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002110 if (err < 0)
2111 goto free_used_maps;
2112
Daniel Borkmann9facc332018-06-15 02:30:48 +02002113 prog = bpf_prog_select_runtime(prog, &err);
Alexei Starovoitov04fd61ab2015-05-19 16:59:03 -07002114 if (err < 0)
2115 goto free_used_maps;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002116
Martin KaFai Laudc4bb0e2017-06-05 12:15:46 -07002117 err = bpf_prog_alloc_id(prog);
2118 if (err)
2119 goto free_used_maps;
2120
Daniel Borkmannc7517982019-08-23 22:14:23 +02002121 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2122 * effectively publicly exposed. However, retrieving via
2123 * bpf_prog_get_fd_by_id() will take another reference,
2124 * therefore it cannot be gone underneath us.
2125 *
2126 * Only for the time /after/ successful bpf_prog_new_fd()
2127 * and before returning to userspace, we might just hold
2128 * one reference and any parallel close on that fd could
2129 * rip everything out. Hence, below notifications must
2130 * happen before bpf_prog_new_fd().
2131 *
2132 * Also, any failure handling from this point onwards must
2133 * be using bpf_prog_put() given the program is exposed.
2134 */
Daniel Borkmann74451e662017-02-16 22:24:50 +01002135 bpf_prog_kallsyms_add(prog);
Song Liu6ee52e22019-01-17 08:15:15 -08002136 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
Daniel Borkmannbae141f2019-12-06 22:49:34 +01002137 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
Daniel Borkmannc7517982019-08-23 22:14:23 +02002138
2139 err = bpf_prog_new_fd(prog);
2140 if (err < 0)
2141 bpf_prog_put(prog);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002142 return err;
2143
2144free_used_maps:
Daniel Borkmanncd7455f2019-10-22 15:57:23 +02002145 /* In case we have subprogs, we need to wait for a grace
2146 * period before we can tear down JIT memory since symbols
2147 * are already exposed under kallsyms.
2148 */
2149 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2150 return err;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002151free_prog:
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002152 bpf_prog_uncharge_memlock(prog);
Chenbo Fengafdb09c2017-10-18 13:00:24 -07002153free_prog_sec:
2154 security_bpf_prog_free(prog->aux);
Alexei Starovoitovaaac3ba2015-10-07 22:23:22 -07002155free_prog_nouncharge:
Alexei Starovoitov09756af2014-09-26 00:17:00 -07002156 bpf_prog_free(prog);
2157 return err;
2158}
2159
Chenbo Feng6e71b042017-10-18 13:00:22 -07002160#define BPF_OBJ_LAST_FIELD file_flags
Daniel Borkmannb2197752015-10-29 14:58:09 +01002161
2162static int bpf_obj_pin(const union bpf_attr *attr)
2163{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002164 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002165 return -EINVAL;
2166
Mickaël Salaün535e7b4b2016-11-13 19:44:03 +01002167 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
Daniel Borkmannb2197752015-10-29 14:58:09 +01002168}
2169
2170static int bpf_obj_get(const union bpf_attr *attr)
2171{
Chenbo Feng6e71b042017-10-18 13:00:22 -07002172 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2173 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
Daniel Borkmannb2197752015-10-29 14:58:09 +01002174 return -EINVAL;
2175
Chenbo Feng6e71b042017-10-18 13:00:22 -07002176 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2177 attr->file_flags);
Daniel Borkmannb2197752015-10-29 14:58:09 +01002178}
2179
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002180static int bpf_tracing_prog_release(struct inode *inode, struct file *filp)
2181{
2182 struct bpf_prog *prog = filp->private_data;
2183
2184 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
2185 bpf_prog_put(prog);
2186 return 0;
2187}
2188
2189static const struct file_operations bpf_tracing_prog_fops = {
2190 .release = bpf_tracing_prog_release,
2191 .read = bpf_dummy_read,
2192 .write = bpf_dummy_write,
2193};
2194
2195static int bpf_tracing_prog_attach(struct bpf_prog *prog)
2196{
2197 int tr_fd, err;
2198
2199 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002200 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2201 prog->type != BPF_PROG_TYPE_EXT) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002202 err = -EINVAL;
2203 goto out_put_prog;
2204 }
2205
2206 err = bpf_trampoline_link_prog(prog);
2207 if (err)
2208 goto out_put_prog;
2209
2210 tr_fd = anon_inode_getfd("bpf-tracing-prog", &bpf_tracing_prog_fops,
2211 prog, O_CLOEXEC);
2212 if (tr_fd < 0) {
2213 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
2214 err = tr_fd;
2215 goto out_put_prog;
2216 }
2217 return tr_fd;
2218
2219out_put_prog:
2220 bpf_prog_put(prog);
2221 return err;
2222}
2223
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002224struct bpf_raw_tracepoint {
2225 struct bpf_raw_event_map *btp;
2226 struct bpf_prog *prog;
2227};
2228
2229static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
2230{
2231 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
2232
2233 if (raw_tp->prog) {
2234 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
2235 bpf_prog_put(raw_tp->prog);
2236 }
Matt Mullinsa38d1102018-12-12 16:42:37 -08002237 bpf_put_raw_tracepoint(raw_tp->btp);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002238 kfree(raw_tp);
2239 return 0;
2240}
2241
2242static const struct file_operations bpf_raw_tp_fops = {
2243 .release = bpf_raw_tracepoint_release,
2244 .read = bpf_dummy_read,
2245 .write = bpf_dummy_write,
2246};
2247
2248#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2249
2250static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2251{
2252 struct bpf_raw_tracepoint *raw_tp;
2253 struct bpf_raw_event_map *btp;
2254 struct bpf_prog *prog;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002255 const char *tp_name;
2256 char buf[128];
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002257 int tp_fd, err;
2258
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002259 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2260 return -EINVAL;
2261
2262 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2263 if (IS_ERR(prog))
2264 return PTR_ERR(prog);
2265
2266 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
Alexei Starovoitovf1b95092019-10-30 15:32:11 -07002267 prog->type != BPF_PROG_TYPE_TRACING &&
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002268 prog->type != BPF_PROG_TYPE_EXT &&
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002269 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
2270 err = -EINVAL;
2271 goto out_put_prog;
2272 }
2273
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -08002274 if (prog->type == BPF_PROG_TYPE_TRACING ||
2275 prog->type == BPF_PROG_TYPE_EXT) {
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002276 if (attr->raw_tracepoint.name) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002277 /* The attach point for this category of programs
2278 * should be specified via btf_id during program load.
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002279 */
2280 err = -EINVAL;
2281 goto out_put_prog;
2282 }
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08002283 if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
2284 tp_name = prog->aux->attach_func_name;
2285 else
2286 return bpf_tracing_prog_attach(prog);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002287 } else {
2288 if (strncpy_from_user(buf,
2289 u64_to_user_ptr(attr->raw_tracepoint.name),
2290 sizeof(buf) - 1) < 0) {
2291 err = -EFAULT;
2292 goto out_put_prog;
2293 }
2294 buf[sizeof(buf) - 1] = 0;
2295 tp_name = buf;
2296 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002297
Matt Mullinsa38d1102018-12-12 16:42:37 -08002298 btp = bpf_get_raw_tracepoint(tp_name);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002299 if (!btp) {
2300 err = -ENOENT;
2301 goto out_put_prog;
2302 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002303
2304 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
Matt Mullinsa38d1102018-12-12 16:42:37 -08002305 if (!raw_tp) {
2306 err = -ENOMEM;
2307 goto out_put_btp;
2308 }
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002309 raw_tp->btp = btp;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002310 raw_tp->prog = prog;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002311
2312 err = bpf_probe_register(raw_tp->btp, prog);
2313 if (err)
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002314 goto out_free_tp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002315
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002316 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
2317 O_CLOEXEC);
2318 if (tp_fd < 0) {
2319 bpf_probe_unregister(raw_tp->btp, prog);
2320 err = tp_fd;
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002321 goto out_free_tp;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002322 }
2323 return tp_fd;
2324
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002325out_free_tp:
2326 kfree(raw_tp);
Matt Mullinsa38d1102018-12-12 16:42:37 -08002327out_put_btp:
2328 bpf_put_raw_tracepoint(btp);
Alexei Starovoitovac4414b2019-10-15 20:25:01 -07002329out_put_prog:
2330 bpf_prog_put(prog);
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07002331 return err;
2332}
2333
Anders Roxell33491582018-04-03 14:09:47 +02002334static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2335 enum bpf_attach_type attach_type)
2336{
2337 switch (prog->type) {
2338 case BPF_PROG_TYPE_CGROUP_SOCK:
2339 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002340 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
Anders Roxell33491582018-04-03 14:09:47 +02002341 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
brakmo5cf1e912019-05-28 16:59:36 -07002342 case BPF_PROG_TYPE_CGROUP_SKB:
2343 return prog->enforce_expected_attach_type &&
2344 prog->expected_attach_type != attach_type ?
2345 -EINVAL : 0;
Anders Roxell33491582018-04-03 14:09:47 +02002346 default:
2347 return 0;
2348 }
2349}
2350
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08002351#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
John Fastabend174a79f2017-08-15 22:32:47 -07002352
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002353#define BPF_F_ATTACH_MASK \
Andrey Ignatov7dd68b32019-12-18 23:44:35 -08002354 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002355
Daniel Mackf4324552016-11-23 16:52:27 +01002356static int bpf_prog_attach(const union bpf_attr *attr)
2357{
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002358 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01002359 struct bpf_prog *prog;
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002360 int ret;
Daniel Mackf4324552016-11-23 16:52:27 +01002361
2362 if (!capable(CAP_NET_ADMIN))
2363 return -EPERM;
2364
2365 if (CHECK_ATTR(BPF_PROG_ATTACH))
2366 return -EINVAL;
2367
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002368 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002369 return -EINVAL;
2370
Daniel Mackf4324552016-11-23 16:52:27 +01002371 switch (attr->attach_type) {
2372 case BPF_CGROUP_INET_INGRESS:
2373 case BPF_CGROUP_INET_EGRESS:
David Ahernb2cd1252016-12-01 08:48:03 -08002374 ptype = BPF_PROG_TYPE_CGROUP_SKB;
Daniel Mackf4324552016-11-23 16:52:27 +01002375 break;
David Ahern610236582016-12-01 08:48:04 -08002376 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002377 case BPF_CGROUP_INET4_POST_BIND:
2378 case BPF_CGROUP_INET6_POST_BIND:
David Ahern610236582016-12-01 08:48:04 -08002379 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2380 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002381 case BPF_CGROUP_INET4_BIND:
2382 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002383 case BPF_CGROUP_INET4_CONNECT:
2384 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002385 case BPF_CGROUP_UDP4_SENDMSG:
2386 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002387 case BPF_CGROUP_UDP4_RECVMSG:
2388 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002389 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2390 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002391 case BPF_CGROUP_SOCK_OPS:
2392 ptype = BPF_PROG_TYPE_SOCK_OPS;
2393 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05002394 case BPF_CGROUP_DEVICE:
2395 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2396 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07002397 case BPF_SK_MSG_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01002398 ptype = BPF_PROG_TYPE_SK_MSG;
2399 break;
John Fastabend464bc0f2017-08-28 07:10:04 -07002400 case BPF_SK_SKB_STREAM_PARSER:
2401 case BPF_SK_SKB_STREAM_VERDICT:
Sean Youngfdb5c452018-06-19 00:04:24 +01002402 ptype = BPF_PROG_TYPE_SK_SKB;
2403 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01002404 case BPF_LIRC_MODE2:
Sean Youngfdb5c452018-06-19 00:04:24 +01002405 ptype = BPF_PROG_TYPE_LIRC_MODE2;
2406 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07002407 case BPF_FLOW_DISSECTOR:
2408 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
2409 break;
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08002410 case BPF_CGROUP_SYSCTL:
2411 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2412 break;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002413 case BPF_CGROUP_GETSOCKOPT:
2414 case BPF_CGROUP_SETSOCKOPT:
2415 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2416 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002417 default:
2418 return -EINVAL;
2419 }
2420
David Ahernb2cd1252016-12-01 08:48:03 -08002421 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2422 if (IS_ERR(prog))
2423 return PTR_ERR(prog);
2424
Andrey Ignatov5e43f892018-03-30 15:08:00 -07002425 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
2426 bpf_prog_put(prog);
2427 return -EINVAL;
2428 }
2429
Sean Youngfdb5c452018-06-19 00:04:24 +01002430 switch (ptype) {
2431 case BPF_PROG_TYPE_SK_SKB:
2432 case BPF_PROG_TYPE_SK_MSG:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002433 ret = sock_map_get_from_fd(attr, prog);
Sean Youngfdb5c452018-06-19 00:04:24 +01002434 break;
2435 case BPF_PROG_TYPE_LIRC_MODE2:
2436 ret = lirc_prog_attach(attr, prog);
2437 break;
Petar Penkovd58e4682018-09-14 07:46:18 -07002438 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2439 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
2440 break;
Sean Youngfdb5c452018-06-19 00:04:24 +01002441 default:
2442 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
David Ahernb2cd1252016-12-01 08:48:03 -08002443 }
2444
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002445 if (ret)
2446 bpf_prog_put(prog);
Alexei Starovoitov7f677632017-02-10 20:28:24 -08002447 return ret;
Daniel Mackf4324552016-11-23 16:52:27 +01002448}
2449
2450#define BPF_PROG_DETACH_LAST_FIELD attach_type
2451
2452static int bpf_prog_detach(const union bpf_attr *attr)
2453{
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002454 enum bpf_prog_type ptype;
Daniel Mackf4324552016-11-23 16:52:27 +01002455
2456 if (!capable(CAP_NET_ADMIN))
2457 return -EPERM;
2458
2459 if (CHECK_ATTR(BPF_PROG_DETACH))
2460 return -EINVAL;
2461
2462 switch (attr->attach_type) {
2463 case BPF_CGROUP_INET_INGRESS:
2464 case BPF_CGROUP_INET_EGRESS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002465 ptype = BPF_PROG_TYPE_CGROUP_SKB;
2466 break;
David Ahern610236582016-12-01 08:48:04 -08002467 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002468 case BPF_CGROUP_INET4_POST_BIND:
2469 case BPF_CGROUP_INET6_POST_BIND:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002470 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2471 break;
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002472 case BPF_CGROUP_INET4_BIND:
2473 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002474 case BPF_CGROUP_INET4_CONNECT:
2475 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002476 case BPF_CGROUP_UDP4_SENDMSG:
2477 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002478 case BPF_CGROUP_UDP4_RECVMSG:
2479 case BPF_CGROUP_UDP6_RECVMSG:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002480 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2481 break;
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002482 case BPF_CGROUP_SOCK_OPS:
Alexei Starovoitov324bda9e62017-10-02 22:50:21 -07002483 ptype = BPF_PROG_TYPE_SOCK_OPS;
Daniel Mackf4324552016-11-23 16:52:27 +01002484 break;
Roman Gushchinebc614f2017-11-05 08:15:32 -05002485 case BPF_CGROUP_DEVICE:
2486 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2487 break;
John Fastabend4f738ad2018-03-18 12:57:10 -07002488 case BPF_SK_MSG_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002489 return sock_map_get_from_fd(attr, NULL);
John Fastabend5a67da22017-09-08 14:00:49 -07002490 case BPF_SK_SKB_STREAM_PARSER:
2491 case BPF_SK_SKB_STREAM_VERDICT:
Daniel Borkmann604326b2018-10-13 02:45:58 +02002492 return sock_map_get_from_fd(attr, NULL);
Sean Youngf4364dc2018-05-27 12:24:09 +01002493 case BPF_LIRC_MODE2:
2494 return lirc_prog_detach(attr);
Petar Penkovd58e4682018-09-14 07:46:18 -07002495 case BPF_FLOW_DISSECTOR:
2496 return skb_flow_dissector_bpf_prog_detach(attr);
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08002497 case BPF_CGROUP_SYSCTL:
2498 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2499 break;
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002500 case BPF_CGROUP_GETSOCKOPT:
2501 case BPF_CGROUP_SETSOCKOPT:
2502 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2503 break;
Daniel Mackf4324552016-11-23 16:52:27 +01002504 default:
2505 return -EINVAL;
2506 }
2507
Sean Youngfdb5c452018-06-19 00:04:24 +01002508 return cgroup_bpf_prog_detach(attr, ptype);
Daniel Mackf4324552016-11-23 16:52:27 +01002509}
Lawrence Brakmo40304b22017-06-30 20:02:40 -07002510
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002511#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2512
2513static int bpf_prog_query(const union bpf_attr *attr,
2514 union bpf_attr __user *uattr)
2515{
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002516 if (!capable(CAP_NET_ADMIN))
2517 return -EPERM;
2518 if (CHECK_ATTR(BPF_PROG_QUERY))
2519 return -EINVAL;
2520 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2521 return -EINVAL;
2522
2523 switch (attr->query.attach_type) {
2524 case BPF_CGROUP_INET_INGRESS:
2525 case BPF_CGROUP_INET_EGRESS:
2526 case BPF_CGROUP_INET_SOCK_CREATE:
Andrey Ignatov4fbac772018-03-30 15:08:02 -07002527 case BPF_CGROUP_INET4_BIND:
2528 case BPF_CGROUP_INET6_BIND:
Andrey Ignatovaac3fc32018-03-30 15:08:07 -07002529 case BPF_CGROUP_INET4_POST_BIND:
2530 case BPF_CGROUP_INET6_POST_BIND:
Andrey Ignatovd74bad42018-03-30 15:08:05 -07002531 case BPF_CGROUP_INET4_CONNECT:
2532 case BPF_CGROUP_INET6_CONNECT:
Andrey Ignatov1cedee12018-05-25 08:55:23 -07002533 case BPF_CGROUP_UDP4_SENDMSG:
2534 case BPF_CGROUP_UDP6_SENDMSG:
Daniel Borkmann983695f2019-06-07 01:48:57 +02002535 case BPF_CGROUP_UDP4_RECVMSG:
2536 case BPF_CGROUP_UDP6_RECVMSG:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002537 case BPF_CGROUP_SOCK_OPS:
Roman Gushchinebc614f2017-11-05 08:15:32 -05002538 case BPF_CGROUP_DEVICE:
Andrey Ignatov7b146ce2019-02-27 12:59:24 -08002539 case BPF_CGROUP_SYSCTL:
Stanislav Fomichev0d01da62019-06-27 13:38:47 -07002540 case BPF_CGROUP_GETSOCKOPT:
2541 case BPF_CGROUP_SETSOCKOPT:
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002542 break;
Sean Youngf4364dc2018-05-27 12:24:09 +01002543 case BPF_LIRC_MODE2:
2544 return lirc_prog_query(attr, uattr);
Stanislav Fomichev118c8e92019-04-25 14:37:23 -07002545 case BPF_FLOW_DISSECTOR:
2546 return skb_flow_dissector_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002547 default:
2548 return -EINVAL;
2549 }
Sean Youngfdb5c452018-06-19 00:04:24 +01002550
2551 return cgroup_bpf_prog_query(attr, uattr);
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07002552}
Daniel Mackf4324552016-11-23 16:52:27 +01002553
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002554#define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002555
2556static int bpf_prog_test_run(const union bpf_attr *attr,
2557 union bpf_attr __user *uattr)
2558{
2559 struct bpf_prog *prog;
2560 int ret = -ENOTSUPP;
2561
Alexei Starovoitov61f3c962018-01-17 16:52:02 -08002562 if (!capable(CAP_SYS_ADMIN))
2563 return -EPERM;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002564 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2565 return -EINVAL;
2566
Stanislav Fomichevb0b93952019-04-09 11:49:09 -07002567 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2568 (!attr->test.ctx_size_in && attr->test.ctx_in))
2569 return -EINVAL;
2570
2571 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2572 (!attr->test.ctx_size_out && attr->test.ctx_out))
2573 return -EINVAL;
2574
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07002575 prog = bpf_prog_get(attr->test.prog_fd);
2576 if (IS_ERR(prog))
2577 return PTR_ERR(prog);
2578
2579 if (prog->aux->ops->test_run)
2580 ret = prog->aux->ops->test_run(prog, attr, uattr);
2581
2582 bpf_prog_put(prog);
2583 return ret;
2584}
2585
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07002586#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2587
2588static int bpf_obj_get_next_id(const union bpf_attr *attr,
2589 union bpf_attr __user *uattr,
2590 struct idr *idr,
2591 spinlock_t *lock)
2592{
2593 u32 next_id = attr->start_id;
2594 int err = 0;
2595
2596 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2597 return -EINVAL;
2598
2599 if (!capable(CAP_SYS_ADMIN))
2600 return -EPERM;
2601
2602 next_id++;
2603 spin_lock_bh(lock);
2604 if (!idr_get_next(idr, &next_id))
2605 err = -ENOENT;
2606 spin_unlock_bh(lock);
2607
2608 if (!err)
2609 err = put_user(next_id, &uattr->next_id);
2610
2611 return err;
2612}
2613
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002614#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2615
Björn Töpel7e6897f2019-12-13 18:51:09 +01002616struct bpf_prog *bpf_prog_by_id(u32 id)
2617{
2618 struct bpf_prog *prog;
2619
2620 if (!id)
2621 return ERR_PTR(-ENOENT);
2622
2623 spin_lock_bh(&prog_idr_lock);
2624 prog = idr_find(&prog_idr, id);
2625 if (prog)
2626 prog = bpf_prog_inc_not_zero(prog);
2627 else
2628 prog = ERR_PTR(-ENOENT);
2629 spin_unlock_bh(&prog_idr_lock);
2630 return prog;
2631}
2632
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002633static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2634{
2635 struct bpf_prog *prog;
2636 u32 id = attr->prog_id;
2637 int fd;
2638
2639 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2640 return -EINVAL;
2641
2642 if (!capable(CAP_SYS_ADMIN))
2643 return -EPERM;
2644
Björn Töpel7e6897f2019-12-13 18:51:09 +01002645 prog = bpf_prog_by_id(id);
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07002646 if (IS_ERR(prog))
2647 return PTR_ERR(prog);
2648
2649 fd = bpf_prog_new_fd(prog);
2650 if (fd < 0)
2651 bpf_prog_put(prog);
2652
2653 return fd;
2654}
2655
Chenbo Feng6e71b042017-10-18 13:00:22 -07002656#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002657
2658static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2659{
2660 struct bpf_map *map;
2661 u32 id = attr->map_id;
Chenbo Feng6e71b042017-10-18 13:00:22 -07002662 int f_flags;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002663 int fd;
2664
Chenbo Feng6e71b042017-10-18 13:00:22 -07002665 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2666 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002667 return -EINVAL;
2668
2669 if (!capable(CAP_SYS_ADMIN))
2670 return -EPERM;
2671
Chenbo Feng6e71b042017-10-18 13:00:22 -07002672 f_flags = bpf_get_file_flag(attr->open_flags);
2673 if (f_flags < 0)
2674 return f_flags;
2675
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002676 spin_lock_bh(&map_idr_lock);
2677 map = idr_find(&map_idr, id);
2678 if (map)
Stanislav Fomichevb0e47012019-08-14 10:37:48 -07002679 map = __bpf_map_inc_not_zero(map, true);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002680 else
2681 map = ERR_PTR(-ENOENT);
2682 spin_unlock_bh(&map_idr_lock);
2683
2684 if (IS_ERR(map))
2685 return PTR_ERR(map);
2686
Chenbo Feng6e71b042017-10-18 13:00:22 -07002687 fd = bpf_map_new_fd(map, f_flags);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002688 if (fd < 0)
Peng Sun781e6282019-02-26 22:15:37 +08002689 bpf_map_put_with_uref(map);
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07002690
2691 return fd;
2692}
2693
Daniel Borkmann7105e822017-12-20 13:42:57 +01002694static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002695 unsigned long addr, u32 *off,
2696 u32 *type)
Daniel Borkmann7105e822017-12-20 13:42:57 +01002697{
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002698 const struct bpf_map *map;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002699 int i;
2700
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002701 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2702 map = prog->aux->used_maps[i];
2703 if (map == (void *)addr) {
2704 *type = BPF_PSEUDO_MAP_FD;
2705 return map;
2706 }
2707 if (!map->ops->map_direct_value_meta)
2708 continue;
2709 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2710 *type = BPF_PSEUDO_MAP_VALUE;
2711 return map;
2712 }
2713 }
2714
Daniel Borkmann7105e822017-12-20 13:42:57 +01002715 return NULL;
2716}
2717
2718static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2719{
2720 const struct bpf_map *map;
2721 struct bpf_insn *insns;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002722 u32 off, type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002723 u64 imm;
2724 int i;
2725
2726 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2727 GFP_USER);
2728 if (!insns)
2729 return insns;
2730
2731 for (i = 0; i < prog->len; i++) {
2732 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2733 insns[i].code = BPF_JMP | BPF_CALL;
2734 insns[i].imm = BPF_FUNC_tail_call;
2735 /* fall-through */
2736 }
2737 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2738 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2739 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2740 insns[i].code = BPF_JMP | BPF_CALL;
2741 if (!bpf_dump_raw_ok())
2742 insns[i].imm = 0;
2743 continue;
2744 }
2745
2746 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2747 continue;
2748
2749 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002750 map = bpf_map_from_imm(prog, imm, &off, &type);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002751 if (map) {
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002752 insns[i].src_reg = type;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002753 insns[i].imm = map->id;
Daniel Borkmannd8eca5b2019-04-09 23:20:03 +02002754 insns[i + 1].imm = off;
Daniel Borkmann7105e822017-12-20 13:42:57 +01002755 continue;
2756 }
Daniel Borkmann7105e822017-12-20 13:42:57 +01002757 }
2758
2759 return insns;
2760}
2761
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002762static int set_info_rec_size(struct bpf_prog_info *info)
2763{
2764 /*
2765 * Ensure info.*_rec_size is the same as kernel expected size
2766 *
2767 * or
2768 *
2769 * Only allow zero *_rec_size if both _rec_size and _cnt are
2770 * zero. In this case, the kernel will set the expected
2771 * _rec_size back to the info.
2772 */
2773
Yonghong Song11d8b822018-12-10 14:14:08 -08002774 if ((info->nr_func_info || info->func_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002775 info->func_info_rec_size != sizeof(struct bpf_func_info))
2776 return -EINVAL;
2777
Yonghong Song11d8b822018-12-10 14:14:08 -08002778 if ((info->nr_line_info || info->line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002779 info->line_info_rec_size != sizeof(struct bpf_line_info))
2780 return -EINVAL;
2781
Yonghong Song11d8b822018-12-10 14:14:08 -08002782 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002783 info->jited_line_info_rec_size != sizeof(__u64))
2784 return -EINVAL;
2785
2786 info->func_info_rec_size = sizeof(struct bpf_func_info);
2787 info->line_info_rec_size = sizeof(struct bpf_line_info);
2788 info->jited_line_info_rec_size = sizeof(__u64);
2789
2790 return 0;
2791}
2792
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002793static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2794 const union bpf_attr *attr,
2795 union bpf_attr __user *uattr)
2796{
2797 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2798 struct bpf_prog_info info = {};
2799 u32 info_len = attr->info.info_len;
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002800 struct bpf_prog_stats stats;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002801 char __user *uinsns;
2802 u32 ulen;
2803 int err;
2804
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07002805 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002806 if (err)
2807 return err;
2808 info_len = min_t(u32, sizeof(info), info_len);
2809
2810 if (copy_from_user(&info, uinfo, info_len))
Daniel Borkmann89b09682017-07-27 21:02:46 +02002811 return -EFAULT;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002812
2813 info.type = prog->type;
2814 info.id = prog->aux->id;
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002815 info.load_time = prog->aux->load_time;
2816 info.created_by_uid = from_kuid_munged(current_user_ns(),
2817 prog->aux->user->uid);
Jiri Olsab85fab02018-04-25 19:41:06 +02002818 info.gpl_compatible = prog->gpl_compatible;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002819
2820 memcpy(info.tag, prog->tag, sizeof(prog->tag));
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002821 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2822
2823 ulen = info.nr_map_ids;
2824 info.nr_map_ids = prog->aux->used_map_cnt;
2825 ulen = min_t(u32, info.nr_map_ids, ulen);
2826 if (ulen) {
Martin KaFai Lau721e08d2017-09-29 10:52:17 -07002827 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
Martin KaFai Laucb4d2b32017-09-27 14:37:52 -07002828 u32 i;
2829
2830 for (i = 0; i < ulen; i++)
2831 if (put_user(prog->aux->used_maps[i]->id,
2832 &user_map_ids[i]))
2833 return -EFAULT;
2834 }
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002835
Martin KaFai Lauc454a462018-12-07 16:42:25 -08002836 err = set_info_rec_size(&info);
2837 if (err)
2838 return err;
Martin KaFai Lau73372242018-12-05 17:35:43 -08002839
Alexei Starovoitov5f8f8b92019-02-25 14:28:40 -08002840 bpf_prog_get_stats(prog, &stats);
2841 info.run_time_ns = stats.nsecs;
2842 info.run_cnt = stats.cnt;
2843
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002844 if (!capable(CAP_SYS_ADMIN)) {
2845 info.jited_prog_len = 0;
2846 info.xlated_prog_len = 0;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302847 info.nr_jited_ksyms = 0;
Daniel Borkmann28c2fae2018-11-02 11:35:46 +01002848 info.nr_jited_func_lens = 0;
Yonghong Song11d8b822018-12-10 14:14:08 -08002849 info.nr_func_info = 0;
2850 info.nr_line_info = 0;
2851 info.nr_jited_line_info = 0;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002852 goto done;
2853 }
2854
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002855 ulen = info.xlated_prog_len;
Daniel Borkmann9975a542017-07-28 17:05:25 +02002856 info.xlated_prog_len = bpf_prog_insn_size(prog);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002857 if (info.xlated_prog_len && ulen) {
Daniel Borkmann7105e822017-12-20 13:42:57 +01002858 struct bpf_insn *insns_sanitized;
2859 bool fault;
2860
2861 if (prog->blinded && !bpf_dump_raw_ok()) {
2862 info.xlated_prog_insns = 0;
2863 goto done;
2864 }
2865 insns_sanitized = bpf_insn_prepare_dump(prog);
2866 if (!insns_sanitized)
2867 return -ENOMEM;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002868 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2869 ulen = min_t(u32, info.xlated_prog_len, ulen);
Daniel Borkmann7105e822017-12-20 13:42:57 +01002870 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2871 kfree(insns_sanitized);
2872 if (fault)
Martin KaFai Lau1e270972017-06-05 12:15:52 -07002873 return -EFAULT;
2874 }
2875
Jakub Kicinski675fc272017-12-27 18:39:09 -08002876 if (bpf_prog_is_dev_bound(prog->aux)) {
2877 err = bpf_prog_offload_info_fill(&info, prog);
2878 if (err)
2879 return err;
Jiong Wangfcfb1262018-01-16 16:05:19 -08002880 goto done;
2881 }
2882
2883 /* NOTE: the following code is supposed to be skipped for offload.
2884 * bpf_prog_offload_info_fill() is the place to fill similar fields
2885 * for offload.
2886 */
2887 ulen = info.jited_prog_len;
Sandipan Das4d56a762018-05-24 12:26:51 +05302888 if (prog->aux->func_cnt) {
2889 u32 i;
2890
2891 info.jited_prog_len = 0;
2892 for (i = 0; i < prog->aux->func_cnt; i++)
2893 info.jited_prog_len += prog->aux->func[i]->jited_len;
2894 } else {
2895 info.jited_prog_len = prog->jited_len;
2896 }
2897
Jiong Wangfcfb1262018-01-16 16:05:19 -08002898 if (info.jited_prog_len && ulen) {
2899 if (bpf_dump_raw_ok()) {
2900 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2901 ulen = min_t(u32, info.jited_prog_len, ulen);
Sandipan Das4d56a762018-05-24 12:26:51 +05302902
2903 /* for multi-function programs, copy the JITed
2904 * instructions for all the functions
2905 */
2906 if (prog->aux->func_cnt) {
2907 u32 len, free, i;
2908 u8 *img;
2909
2910 free = ulen;
2911 for (i = 0; i < prog->aux->func_cnt; i++) {
2912 len = prog->aux->func[i]->jited_len;
2913 len = min_t(u32, len, free);
2914 img = (u8 *) prog->aux->func[i]->bpf_func;
2915 if (copy_to_user(uinsns, img, len))
2916 return -EFAULT;
2917 uinsns += len;
2918 free -= len;
2919 if (!free)
2920 break;
2921 }
2922 } else {
2923 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2924 return -EFAULT;
2925 }
Jiong Wangfcfb1262018-01-16 16:05:19 -08002926 } else {
2927 info.jited_prog_insns = 0;
2928 }
Jakub Kicinski675fc272017-12-27 18:39:09 -08002929 }
2930
Sandipan Dasdbecd732018-05-24 12:26:48 +05302931 ulen = info.nr_jited_ksyms;
Song Liuff1889f2018-11-02 10:16:17 -07002932 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002933 if (ulen) {
Sandipan Dasdbecd732018-05-24 12:26:48 +05302934 if (bpf_dump_raw_ok()) {
Song Liuff1889f2018-11-02 10:16:17 -07002935 unsigned long ksym_addr;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302936 u64 __user *user_ksyms;
Sandipan Dasdbecd732018-05-24 12:26:48 +05302937 u32 i;
2938
2939 /* copy the address of the kernel symbol
2940 * corresponding to each function
2941 */
2942 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2943 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
Song Liuff1889f2018-11-02 10:16:17 -07002944 if (prog->aux->func_cnt) {
2945 for (i = 0; i < ulen; i++) {
2946 ksym_addr = (unsigned long)
2947 prog->aux->func[i]->bpf_func;
2948 if (put_user((u64) ksym_addr,
2949 &user_ksyms[i]))
2950 return -EFAULT;
2951 }
2952 } else {
2953 ksym_addr = (unsigned long) prog->bpf_func;
2954 if (put_user((u64) ksym_addr, &user_ksyms[0]))
Sandipan Dasdbecd732018-05-24 12:26:48 +05302955 return -EFAULT;
2956 }
2957 } else {
2958 info.jited_ksyms = 0;
2959 }
2960 }
2961
Sandipan Das815581c2018-05-24 12:26:52 +05302962 ulen = info.nr_jited_func_lens;
Song Liuff1889f2018-11-02 10:16:17 -07002963 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
Song Liu7a5725d2018-12-10 11:17:50 -08002964 if (ulen) {
Sandipan Das815581c2018-05-24 12:26:52 +05302965 if (bpf_dump_raw_ok()) {
2966 u32 __user *user_lens;
2967 u32 func_len, i;
2968
2969 /* copy the JITed image lengths for each function */
2970 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2971 user_lens = u64_to_user_ptr(info.jited_func_lens);
Song Liuff1889f2018-11-02 10:16:17 -07002972 if (prog->aux->func_cnt) {
2973 for (i = 0; i < ulen; i++) {
2974 func_len =
2975 prog->aux->func[i]->jited_len;
2976 if (put_user(func_len, &user_lens[i]))
2977 return -EFAULT;
2978 }
2979 } else {
2980 func_len = prog->jited_len;
2981 if (put_user(func_len, &user_lens[0]))
Sandipan Das815581c2018-05-24 12:26:52 +05302982 return -EFAULT;
2983 }
2984 } else {
2985 info.jited_func_lens = 0;
2986 }
2987 }
2988
Martin KaFai Lau73372242018-12-05 17:35:43 -08002989 if (prog->aux->btf)
Yonghong Song838e9692018-11-19 15:29:11 -08002990 info.btf_id = btf_id(prog->aux->btf);
2991
Yonghong Song11d8b822018-12-10 14:14:08 -08002992 ulen = info.nr_func_info;
2993 info.nr_func_info = prog->aux->func_info_cnt;
2994 if (info.nr_func_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002995 char __user *user_finfo;
Yonghong Song838e9692018-11-19 15:29:11 -08002996
Martin KaFai Lau9e794162018-12-12 10:18:21 -08002997 user_finfo = u64_to_user_ptr(info.func_info);
2998 ulen = min_t(u32, info.nr_func_info, ulen);
2999 if (copy_to_user(user_finfo, prog->aux->func_info,
3000 info.func_info_rec_size * ulen))
3001 return -EFAULT;
Yonghong Song838e9692018-11-19 15:29:11 -08003002 }
3003
Yonghong Song11d8b822018-12-10 14:14:08 -08003004 ulen = info.nr_line_info;
3005 info.nr_line_info = prog->aux->nr_linfo;
3006 if (info.nr_line_info && ulen) {
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003007 __u8 __user *user_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003008
Martin KaFai Lau9e794162018-12-12 10:18:21 -08003009 user_linfo = u64_to_user_ptr(info.line_info);
3010 ulen = min_t(u32, info.nr_line_info, ulen);
3011 if (copy_to_user(user_linfo, prog->aux->linfo,
3012 info.line_info_rec_size * ulen))
3013 return -EFAULT;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003014 }
3015
Yonghong Song11d8b822018-12-10 14:14:08 -08003016 ulen = info.nr_jited_line_info;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003017 if (prog->aux->jited_linfo)
Yonghong Song11d8b822018-12-10 14:14:08 -08003018 info.nr_jited_line_info = prog->aux->nr_linfo;
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003019 else
Yonghong Song11d8b822018-12-10 14:14:08 -08003020 info.nr_jited_line_info = 0;
3021 if (info.nr_jited_line_info && ulen) {
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003022 if (bpf_dump_raw_ok()) {
3023 __u64 __user *user_linfo;
3024 u32 i;
3025
3026 user_linfo = u64_to_user_ptr(info.jited_line_info);
Yonghong Song11d8b822018-12-10 14:14:08 -08003027 ulen = min_t(u32, info.nr_jited_line_info, ulen);
Martin KaFai Lauc454a462018-12-07 16:42:25 -08003028 for (i = 0; i < ulen; i++) {
3029 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3030 &user_linfo[i]))
3031 return -EFAULT;
3032 }
3033 } else {
3034 info.jited_line_info = 0;
3035 }
3036 }
3037
Song Liuc872bdb2018-12-12 09:37:46 -08003038 ulen = info.nr_prog_tags;
3039 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3040 if (ulen) {
3041 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3042 u32 i;
3043
3044 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3045 ulen = min_t(u32, info.nr_prog_tags, ulen);
3046 if (prog->aux->func_cnt) {
3047 for (i = 0; i < ulen; i++) {
3048 if (copy_to_user(user_prog_tags[i],
3049 prog->aux->func[i]->tag,
3050 BPF_TAG_SIZE))
3051 return -EFAULT;
3052 }
3053 } else {
3054 if (copy_to_user(user_prog_tags[0],
3055 prog->tag, BPF_TAG_SIZE))
3056 return -EFAULT;
3057 }
3058 }
3059
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003060done:
3061 if (copy_to_user(uinfo, &info, info_len) ||
3062 put_user(info_len, &uattr->info.info_len))
3063 return -EFAULT;
3064
3065 return 0;
3066}
3067
3068static int bpf_map_get_info_by_fd(struct bpf_map *map,
3069 const union bpf_attr *attr,
3070 union bpf_attr __user *uattr)
3071{
3072 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3073 struct bpf_map_info info = {};
3074 u32 info_len = attr->info.info_len;
3075 int err;
3076
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003077 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003078 if (err)
3079 return err;
3080 info_len = min_t(u32, sizeof(info), info_len);
3081
3082 info.type = map->map_type;
3083 info.id = map->id;
3084 info.key_size = map->key_size;
3085 info.value_size = map->value_size;
3086 info.max_entries = map->max_entries;
3087 info.map_flags = map->map_flags;
Martin KaFai Lauad5b1772017-09-27 14:37:53 -07003088 memcpy(info.name, map->name, sizeof(map->name));
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003089
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003090 if (map->btf) {
3091 info.btf_id = btf_id(map->btf);
Martin KaFai Lau9b2cf322018-05-22 14:57:21 -07003092 info.btf_key_type_id = map->btf_key_type_id;
3093 info.btf_value_type_id = map->btf_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003094 }
Martin KaFai Lau85d33df2020-01-08 16:35:05 -08003095 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003096
Jakub Kicinski52775b32018-01-17 19:13:28 -08003097 if (bpf_map_is_dev_bound(map)) {
3098 err = bpf_map_offload_info_fill(&info, map);
3099 if (err)
3100 return err;
3101 }
3102
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003103 if (copy_to_user(uinfo, &info, info_len) ||
3104 put_user(info_len, &uattr->info.info_len))
3105 return -EFAULT;
3106
3107 return 0;
3108}
3109
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003110static int bpf_btf_get_info_by_fd(struct btf *btf,
3111 const union bpf_attr *attr,
3112 union bpf_attr __user *uattr)
3113{
3114 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3115 u32 info_len = attr->info.info_len;
3116 int err;
3117
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003118 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003119 if (err)
3120 return err;
3121
3122 return btf_get_info_by_fd(btf, attr, uattr);
3123}
3124
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003125#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3126
3127static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3128 union bpf_attr __user *uattr)
3129{
3130 int ufd = attr->info.bpf_fd;
3131 struct fd f;
3132 int err;
3133
3134 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3135 return -EINVAL;
3136
3137 f = fdget(ufd);
3138 if (!f.file)
3139 return -EBADFD;
3140
3141 if (f.file->f_op == &bpf_prog_fops)
3142 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
3143 uattr);
3144 else if (f.file->f_op == &bpf_map_fops)
3145 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
3146 uattr);
Martin KaFai Lau60197cf2018-04-18 15:56:02 -07003147 else if (f.file->f_op == &btf_fops)
Martin KaFai Lau62dab842018-05-04 14:49:52 -07003148 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003149 else
3150 err = -EINVAL;
3151
3152 fdput(f);
3153 return err;
3154}
3155
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003156#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3157
3158static int bpf_btf_load(const union bpf_attr *attr)
3159{
3160 if (CHECK_ATTR(BPF_BTF_LOAD))
3161 return -EINVAL;
3162
3163 if (!capable(CAP_SYS_ADMIN))
3164 return -EPERM;
3165
3166 return btf_new_fd(attr);
3167}
3168
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003169#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3170
3171static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3172{
3173 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3174 return -EINVAL;
3175
3176 if (!capable(CAP_SYS_ADMIN))
3177 return -EPERM;
3178
3179 return btf_get_fd_by_id(attr->btf_id);
3180}
3181
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003182static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3183 union bpf_attr __user *uattr,
3184 u32 prog_id, u32 fd_type,
3185 const char *buf, u64 probe_offset,
3186 u64 probe_addr)
3187{
3188 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3189 u32 len = buf ? strlen(buf) : 0, input_len;
3190 int err = 0;
3191
3192 if (put_user(len, &uattr->task_fd_query.buf_len))
3193 return -EFAULT;
3194 input_len = attr->task_fd_query.buf_len;
3195 if (input_len && ubuf) {
3196 if (!len) {
3197 /* nothing to copy, just make ubuf NULL terminated */
3198 char zero = '\0';
3199
3200 if (put_user(zero, ubuf))
3201 return -EFAULT;
3202 } else if (input_len >= len + 1) {
3203 /* ubuf can hold the string with NULL terminator */
3204 if (copy_to_user(ubuf, buf, len + 1))
3205 return -EFAULT;
3206 } else {
3207 /* ubuf cannot hold the string with NULL terminator,
3208 * do a partial copy with NULL terminator.
3209 */
3210 char zero = '\0';
3211
3212 err = -ENOSPC;
3213 if (copy_to_user(ubuf, buf, input_len - 1))
3214 return -EFAULT;
3215 if (put_user(zero, ubuf + input_len - 1))
3216 return -EFAULT;
3217 }
3218 }
3219
3220 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3221 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3222 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3223 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3224 return -EFAULT;
3225
3226 return err;
3227}
3228
3229#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3230
3231static int bpf_task_fd_query(const union bpf_attr *attr,
3232 union bpf_attr __user *uattr)
3233{
3234 pid_t pid = attr->task_fd_query.pid;
3235 u32 fd = attr->task_fd_query.fd;
3236 const struct perf_event *event;
3237 struct files_struct *files;
3238 struct task_struct *task;
3239 struct file *file;
3240 int err;
3241
3242 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3243 return -EINVAL;
3244
3245 if (!capable(CAP_SYS_ADMIN))
3246 return -EPERM;
3247
3248 if (attr->task_fd_query.flags != 0)
3249 return -EINVAL;
3250
3251 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3252 if (!task)
3253 return -ENOENT;
3254
3255 files = get_files_struct(task);
3256 put_task_struct(task);
3257 if (!files)
3258 return -ENOENT;
3259
3260 err = 0;
3261 spin_lock(&files->file_lock);
3262 file = fcheck_files(files, fd);
3263 if (!file)
3264 err = -EBADF;
3265 else
3266 get_file(file);
3267 spin_unlock(&files->file_lock);
3268 put_files_struct(files);
3269
3270 if (err)
3271 goto out;
3272
3273 if (file->f_op == &bpf_raw_tp_fops) {
3274 struct bpf_raw_tracepoint *raw_tp = file->private_data;
3275 struct bpf_raw_event_map *btp = raw_tp->btp;
3276
3277 err = bpf_task_fd_query_copy(attr, uattr,
3278 raw_tp->prog->aux->id,
3279 BPF_FD_TYPE_RAW_TRACEPOINT,
3280 btp->tp->name, 0, 0);
3281 goto put_file;
3282 }
3283
3284 event = perf_get_event(file);
3285 if (!IS_ERR(event)) {
3286 u64 probe_offset, probe_addr;
3287 u32 prog_id, fd_type;
3288 const char *buf;
3289
3290 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3291 &buf, &probe_offset,
3292 &probe_addr);
3293 if (!err)
3294 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3295 fd_type, buf,
3296 probe_offset,
3297 probe_addr);
3298 goto put_file;
3299 }
3300
3301 err = -ENOTSUPP;
3302put_file:
3303 fput(file);
3304out:
3305 return err;
3306}
3307
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003308#define BPF_MAP_BATCH_LAST_FIELD batch.flags
3309
3310#define BPF_DO_BATCH(fn) \
3311 do { \
3312 if (!fn) { \
3313 err = -ENOTSUPP; \
3314 goto err_put; \
3315 } \
3316 err = fn(map, attr, uattr); \
3317 } while (0)
3318
3319static int bpf_map_do_batch(const union bpf_attr *attr,
3320 union bpf_attr __user *uattr,
3321 int cmd)
3322{
3323 struct bpf_map *map;
3324 int err, ufd;
3325 struct fd f;
3326
3327 if (CHECK_ATTR(BPF_MAP_BATCH))
3328 return -EINVAL;
3329
3330 ufd = attr->batch.map_fd;
3331 f = fdget(ufd);
3332 map = __bpf_map_get(f);
3333 if (IS_ERR(map))
3334 return PTR_ERR(map);
3335
Yonghong Song05799632020-01-15 10:43:04 -08003336 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
3337 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003338 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
3339 err = -EPERM;
3340 goto err_put;
3341 }
3342
3343 if (cmd != BPF_MAP_LOOKUP_BATCH &&
3344 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
3345 err = -EPERM;
3346 goto err_put;
3347 }
3348
3349 if (cmd == BPF_MAP_LOOKUP_BATCH)
3350 BPF_DO_BATCH(map->ops->map_lookup_batch);
Yonghong Song05799632020-01-15 10:43:04 -08003351 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
3352 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08003353 else if (cmd == BPF_MAP_UPDATE_BATCH)
3354 BPF_DO_BATCH(map->ops->map_update_batch);
3355 else
3356 BPF_DO_BATCH(map->ops->map_delete_batch);
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003357
3358err_put:
3359 fdput(f);
3360 return err;
3361}
3362
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003363SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
3364{
3365 union bpf_attr attr = {};
3366 int err;
3367
Chenbo Feng0fa4fe82018-03-19 17:57:27 -07003368 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003369 return -EPERM;
3370
Martin KaFai Laudcab51f2018-05-22 15:03:31 -07003371 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003372 if (err)
3373 return err;
3374 size = min_t(u32, size, sizeof(attr));
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003375
3376 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
3377 if (copy_from_user(&attr, uattr, size) != 0)
3378 return -EFAULT;
3379
Chenbo Fengafdb09c2017-10-18 13:00:24 -07003380 err = security_bpf(cmd, &attr, size);
3381 if (err < 0)
3382 return err;
3383
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003384 switch (cmd) {
3385 case BPF_MAP_CREATE:
3386 err = map_create(&attr);
3387 break;
Alexei Starovoitovdb20fd22014-09-26 00:16:59 -07003388 case BPF_MAP_LOOKUP_ELEM:
3389 err = map_lookup_elem(&attr);
3390 break;
3391 case BPF_MAP_UPDATE_ELEM:
3392 err = map_update_elem(&attr);
3393 break;
3394 case BPF_MAP_DELETE_ELEM:
3395 err = map_delete_elem(&attr);
3396 break;
3397 case BPF_MAP_GET_NEXT_KEY:
3398 err = map_get_next_key(&attr);
3399 break;
Daniel Borkmann87df15d2019-04-09 23:20:06 +02003400 case BPF_MAP_FREEZE:
3401 err = map_freeze(&attr);
3402 break;
Alexei Starovoitov09756af2014-09-26 00:17:00 -07003403 case BPF_PROG_LOAD:
Yonghong Song838e9692018-11-19 15:29:11 -08003404 err = bpf_prog_load(&attr, uattr);
Alexei Starovoitov09756af2014-09-26 00:17:00 -07003405 break;
Daniel Borkmannb2197752015-10-29 14:58:09 +01003406 case BPF_OBJ_PIN:
3407 err = bpf_obj_pin(&attr);
3408 break;
3409 case BPF_OBJ_GET:
3410 err = bpf_obj_get(&attr);
3411 break;
Daniel Mackf4324552016-11-23 16:52:27 +01003412 case BPF_PROG_ATTACH:
3413 err = bpf_prog_attach(&attr);
3414 break;
3415 case BPF_PROG_DETACH:
3416 err = bpf_prog_detach(&attr);
3417 break;
Alexei Starovoitov468e2f62017-10-02 22:50:22 -07003418 case BPF_PROG_QUERY:
3419 err = bpf_prog_query(&attr, uattr);
3420 break;
Alexei Starovoitov1cf1cae2017-03-30 21:45:38 -07003421 case BPF_PROG_TEST_RUN:
3422 err = bpf_prog_test_run(&attr, uattr);
3423 break;
Martin KaFai Lau34ad5582017-06-05 12:15:48 -07003424 case BPF_PROG_GET_NEXT_ID:
3425 err = bpf_obj_get_next_id(&attr, uattr,
3426 &prog_idr, &prog_idr_lock);
3427 break;
3428 case BPF_MAP_GET_NEXT_ID:
3429 err = bpf_obj_get_next_id(&attr, uattr,
3430 &map_idr, &map_idr_lock);
3431 break;
Quentin Monnet1b9ed842019-08-20 10:31:50 +01003432 case BPF_BTF_GET_NEXT_ID:
3433 err = bpf_obj_get_next_id(&attr, uattr,
3434 &btf_idr, &btf_idr_lock);
3435 break;
Martin KaFai Laub16d9aa2017-06-05 12:15:49 -07003436 case BPF_PROG_GET_FD_BY_ID:
3437 err = bpf_prog_get_fd_by_id(&attr);
3438 break;
Martin KaFai Laubd5f5f4e2017-06-05 12:15:50 -07003439 case BPF_MAP_GET_FD_BY_ID:
3440 err = bpf_map_get_fd_by_id(&attr);
3441 break;
Martin KaFai Lau1e270972017-06-05 12:15:52 -07003442 case BPF_OBJ_GET_INFO_BY_FD:
3443 err = bpf_obj_get_info_by_fd(&attr, uattr);
3444 break;
Alexei Starovoitovc4f66992018-03-28 12:05:37 -07003445 case BPF_RAW_TRACEPOINT_OPEN:
3446 err = bpf_raw_tracepoint_open(&attr);
3447 break;
Martin KaFai Lauf56a6532018-04-18 15:56:01 -07003448 case BPF_BTF_LOAD:
3449 err = bpf_btf_load(&attr);
3450 break;
Martin KaFai Lau78958fc2018-05-04 14:49:51 -07003451 case BPF_BTF_GET_FD_BY_ID:
3452 err = bpf_btf_get_fd_by_id(&attr);
3453 break;
Yonghong Song41bdc4b2018-05-24 11:21:09 -07003454 case BPF_TASK_FD_QUERY:
3455 err = bpf_task_fd_query(&attr, uattr);
3456 break;
Mauricio Vasquez Bbd513cd2018-10-18 15:16:30 +02003457 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
3458 err = map_lookup_and_delete_elem(&attr);
3459 break;
Brian Vazquezcb4d03a2020-01-15 10:43:01 -08003460 case BPF_MAP_LOOKUP_BATCH:
3461 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
3462 break;
Yonghong Song05799632020-01-15 10:43:04 -08003463 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
3464 err = bpf_map_do_batch(&attr, uattr,
3465 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
3466 break;
Brian Vazquezaa2e93b2020-01-15 10:43:02 -08003467 case BPF_MAP_UPDATE_BATCH:
3468 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
3469 break;
3470 case BPF_MAP_DELETE_BATCH:
3471 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
3472 break;
Alexei Starovoitov99c55f72014-09-26 00:16:57 -07003473 default:
3474 err = -EINVAL;
3475 break;
3476 }
3477
3478 return err;
3479}