]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/bpf/syscall.c
Merge tag 'tty-5.6-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/tty
[mirror_ubuntu-jammy-kernel.git] / kernel / bpf / syscall.c
CommitLineData
5b497af4 1// SPDX-License-Identifier: GPL-2.0-only
99c55f7d 2/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
99c55f7d
AS
3 */
4#include <linux/bpf.h>
a67edbf4 5#include <linux/bpf_trace.h>
f4364dcf 6#include <linux/bpf_lirc.h>
f56a653c 7#include <linux/btf.h>
99c55f7d
AS
8#include <linux/syscalls.h>
9#include <linux/slab.h>
3f07c014 10#include <linux/sched/signal.h>
d407bd25
DB
11#include <linux/vmalloc.h>
12#include <linux/mmzone.h>
99c55f7d 13#include <linux/anon_inodes.h>
41bdc4b4 14#include <linux/fdtable.h>
db20fd2b 15#include <linux/file.h>
41bdc4b4 16#include <linux/fs.h>
09756af4
AS
17#include <linux/license.h>
18#include <linux/filter.h>
2541517c 19#include <linux/version.h>
535e7b4b 20#include <linux/kernel.h>
dc4bb0e2 21#include <linux/idr.h>
cb4d2b3f
MKL
22#include <linux/cred.h>
23#include <linux/timekeeping.h>
24#include <linux/ctype.h>
9ef09e35 25#include <linux/nospec.h>
bae141f5 26#include <linux/audit.h>
ccfe29eb 27#include <uapi/linux/btf.h>
99c55f7d 28
da765a2f
DB
29#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)
14dc6f04 33#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
da765a2f
DB
34#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
35 IS_FD_HASH(map))
14dc6f04 36
6e71b04a
CF
37#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
38
b121d1e7 39DEFINE_PER_CPU(int, bpf_prog_active);
dc4bb0e2
MKL
40static DEFINE_IDR(prog_idr);
41static DEFINE_SPINLOCK(prog_idr_lock);
f3f1c054
MKL
42static DEFINE_IDR(map_idr);
43static DEFINE_SPINLOCK(map_idr_lock);
b121d1e7 44
1be7f75d
AS
45int sysctl_unprivileged_bpf_disabled __read_mostly;
46
40077e0c 47static const struct bpf_map_ops * const bpf_map_types[] = {
91cc1a99 48#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
40077e0c
JB
49#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};
99c55f7d 55
752ba56f
MS
56/*
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 */
dcab51f1
MKL
65int bpf_check_uarg_tail_zero(void __user *uaddr,
66 size_t expected_size,
67 size_t actual_size)
58291a74
MS
68{
69 unsigned char __user *addr;
70 unsigned char __user *end;
71 unsigned char val;
72 int err;
73
752ba56f
MS
74 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
75 return -E2BIG;
76
96d4f267 77 if (unlikely(!access_ok(uaddr, actual_size)))
752ba56f
MS
78 return -EFAULT;
79
58291a74
MS
80 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
a3884572
JK
97const struct bpf_map_ops bpf_map_offload_ops = {
98 .map_alloc = bpf_map_offload_map_alloc,
99 .map_free = bpf_map_offload_map_free,
e8d2bec0 100 .map_check_btf = map_check_no_btf,
a3884572
JK
101};
102
99c55f7d
AS
103static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
104{
1110f3a9 105 const struct bpf_map_ops *ops;
9ef09e35 106 u32 type = attr->map_type;
99c55f7d 107 struct bpf_map *map;
1110f3a9 108 int err;
99c55f7d 109
9ef09e35 110 if (type >= ARRAY_SIZE(bpf_map_types))
1110f3a9 111 return ERR_PTR(-EINVAL);
9ef09e35
MR
112 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
113 ops = bpf_map_types[type];
1110f3a9 114 if (!ops)
40077e0c 115 return ERR_PTR(-EINVAL);
99c55f7d 116
1110f3a9
JK
117 if (ops->map_alloc_check) {
118 err = ops->map_alloc_check(attr);
119 if (err)
120 return ERR_PTR(err);
121 }
a3884572
JK
122 if (attr->map_ifindex)
123 ops = &bpf_map_offload_ops;
1110f3a9 124 map = ops->map_alloc(attr);
40077e0c
JB
125 if (IS_ERR(map))
126 return map;
1110f3a9 127 map->ops = ops;
9ef09e35 128 map->map_type = type;
40077e0c 129 return map;
99c55f7d
AS
130}
131
15c14a3d
BV
132static 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
cb4d03ab
BV
222 if (bpf_map_is_dev_bound(map))
223 return bpf_map_offload_lookup_elem(map, key, value);
15c14a3d
BV
224
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
196e8ca7 278static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
d407bd25 279{
f01a7dbe
MP
280 /* 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.
d407bd25 288 */
f01a7dbe
MP
289
290 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
d407bd25
DB
291 void *area;
292
196e8ca7
DB
293 if (size >= SIZE_MAX)
294 return NULL;
295
fc970227
AN
296 /* kmalloc()'ed memory can't be mmap()'ed */
297 if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
f01a7dbe
MP
298 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
299 numa_node);
d407bd25
DB
300 if (area != NULL)
301 return area;
302 }
fc970227
AN
303 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 }
f01a7dbe
MP
308 return __vmalloc_node_flags_caller(size, numa_node,
309 GFP_KERNEL | __GFP_RETRY_MAYFAIL |
310 flags, __builtin_return_address(0));
d407bd25
DB
311}
312
196e8ca7 313void *bpf_map_area_alloc(u64 size, int numa_node)
fc970227
AN
314{
315 return __bpf_map_area_alloc(size, numa_node, false);
316}
317
196e8ca7 318void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
fc970227
AN
319{
320 return __bpf_map_area_alloc(size, numa_node, true);
321}
322
d407bd25
DB
323void bpf_map_area_free(void *area)
324{
325 kvfree(area);
326}
327
be70bcd5
DB
328static 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
bd475643
JK
340void 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;
be70bcd5 346 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
bd475643
JK
347 map->numa_node = bpf_map_attr_numa_node(attr);
348}
349
0a4c58f5 350static int bpf_charge_memlock(struct user_struct *user, u32 pages)
aaac3ba9 351{
0a4c58f5 352 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
aaac3ba9 353
0a4c58f5
RG
354 if (atomic_long_add_return(pages, &user->locked_vm) > memlock_limit) {
355 atomic_long_sub(pages, &user->locked_vm);
356 return -EPERM;
357 }
358 return 0;
359}
aaac3ba9 360
0a4c58f5
RG
361static void bpf_uncharge_memlock(struct user_struct *user, u32 pages)
362{
b936ca64
RG
363 if (user)
364 atomic_long_sub(pages, &user->locked_vm);
0a4c58f5 365}
aaac3ba9 366
196e8ca7 367int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
0a4c58f5 368{
c85d6913
RG
369 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
370 struct user_struct *user;
0a4c58f5 371 int ret;
aaac3ba9 372
c85d6913
RG
373 if (size >= U32_MAX - PAGE_SIZE)
374 return -E2BIG;
375
376 user = get_current_user();
b936ca64 377 ret = bpf_charge_memlock(user, pages);
0a4c58f5 378 if (ret) {
aaac3ba9 379 free_uid(user);
0a4c58f5 380 return ret;
aaac3ba9 381 }
b936ca64
RG
382
383 mem->pages = pages;
384 mem->user = user;
385
386 return 0;
aaac3ba9
AS
387}
388
b936ca64 389void bpf_map_charge_finish(struct bpf_map_memory *mem)
aaac3ba9 390{
b936ca64
RG
391 bpf_uncharge_memlock(mem->user, mem->pages);
392 free_uid(mem->user);
393}
394
395void bpf_map_charge_move(struct bpf_map_memory *dst,
396 struct bpf_map_memory *src)
397{
398 *dst = *src;
3539b96e 399
b936ca64
RG
400 /* Make sure src will not be used for the redundant uncharging. */
401 memset(src, 0, sizeof(struct bpf_map_memory));
aaac3ba9
AS
402}
403
0a4c58f5
RG
404int bpf_map_charge_memlock(struct bpf_map *map, u32 pages)
405{
406 int ret;
407
3539b96e 408 ret = bpf_charge_memlock(map->memory.user, pages);
0a4c58f5
RG
409 if (ret)
410 return ret;
3539b96e 411 map->memory.pages += pages;
0a4c58f5
RG
412 return ret;
413}
414
415void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages)
416{
3539b96e
RG
417 bpf_uncharge_memlock(map->memory.user, pages);
418 map->memory.pages -= pages;
0a4c58f5
RG
419}
420
f3f1c054
MKL
421static int bpf_map_alloc_id(struct bpf_map *map)
422{
423 int id;
424
b76354cd 425 idr_preload(GFP_KERNEL);
f3f1c054
MKL
426 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);
b76354cd 431 idr_preload_end();
f3f1c054
MKL
432
433 if (WARN_ON_ONCE(!id))
434 return -ENOSPC;
435
436 return id > 0 ? 0 : id;
437}
438
a3884572 439void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
f3f1c054 440{
930651a7
ED
441 unsigned long flags;
442
a3884572
JK
443 /* 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
bd5f5f4e 451 if (do_idr_lock)
930651a7 452 spin_lock_irqsave(&map_idr_lock, flags);
bd5f5f4e
MKL
453 else
454 __acquire(&map_idr_lock);
455
f3f1c054 456 idr_remove(&map_idr, map->id);
a3884572 457 map->id = 0;
bd5f5f4e
MKL
458
459 if (do_idr_lock)
930651a7 460 spin_unlock_irqrestore(&map_idr_lock, flags);
bd5f5f4e
MKL
461 else
462 __release(&map_idr_lock);
f3f1c054
MKL
463}
464
99c55f7d
AS
465/* 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);
b936ca64 469 struct bpf_map_memory mem;
99c55f7d 470
b936ca64 471 bpf_map_charge_move(&mem, &map->memory);
afdb09c7 472 security_bpf_map_free(map);
99c55f7d
AS
473 /* implementation dependent freeing */
474 map->ops->map_free(map);
b936ca64 475 bpf_map_charge_finish(&mem);
99c55f7d
AS
476}
477
c9da161c
DB
478static void bpf_map_put_uref(struct bpf_map *map)
479{
1e0bd5a0 480 if (atomic64_dec_and_test(&map->usercnt)) {
ba6b8de4
JF
481 if (map->ops->map_release_uref)
482 map->ops->map_release_uref(map);
c9da161c
DB
483 }
484}
485
99c55f7d
AS
486/* decrement map refcnt and schedule it for freeing via workqueue
487 * (unrelying map implementation ops->map_free() might sleep)
488 */
bd5f5f4e 489static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
99c55f7d 490{
1e0bd5a0 491 if (atomic64_dec_and_test(&map->refcnt)) {
34ad5580 492 /* bpf_map_free_id() must be called first */
bd5f5f4e 493 bpf_map_free_id(map, do_idr_lock);
78958fca 494 btf_put(map->btf);
99c55f7d
AS
495 INIT_WORK(&map->work, bpf_map_free_deferred);
496 schedule_work(&map->work);
497 }
498}
499
bd5f5f4e
MKL
500void bpf_map_put(struct bpf_map *map)
501{
502 __bpf_map_put(map, true);
503}
630a4d38 504EXPORT_SYMBOL_GPL(bpf_map_put);
bd5f5f4e 505
c9da161c 506void bpf_map_put_with_uref(struct bpf_map *map)
99c55f7d 507{
c9da161c 508 bpf_map_put_uref(map);
99c55f7d 509 bpf_map_put(map);
c9da161c
DB
510}
511
512static int bpf_map_release(struct inode *inode, struct file *filp)
513{
61d1b6a4
DB
514 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);
99c55f7d
AS
520 return 0;
521}
522
87df15de
DB
523static 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
f99bf205
DB
535#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;
21116b70 539 const struct bpf_array *array;
2beee5f5 540 u32 type = 0, jited = 0;
21116b70
DB
541
542 if (map->map_type == BPF_MAP_TYPE_PROG_ARRAY) {
543 array = container_of(map, struct bpf_array, map);
2beee5f5
DB
544 type = array->aux->type;
545 jited = array->aux->jited;
21116b70 546 }
f99bf205
DB
547
548 seq_printf(m,
549 "map_type:\t%u\n"
550 "key_size:\t%u\n"
551 "value_size:\t%u\n"
322cea2f 552 "max_entries:\t%u\n"
21116b70 553 "map_flags:\t%#x\n"
4316b409 554 "memlock:\t%llu\n"
87df15de
DB
555 "map_id:\t%u\n"
556 "frozen:\t%u\n",
f99bf205
DB
557 map->map_type,
558 map->key_size,
559 map->value_size,
322cea2f 560 map->max_entries,
21116b70 561 map->map_flags,
3539b96e 562 map->memory.pages * 1ULL << PAGE_SHIFT,
87df15de
DB
563 map->id,
564 READ_ONCE(map->frozen));
2beee5f5
DB
565 if (type) {
566 seq_printf(m, "owner_prog_type:\t%u\n", type);
567 seq_printf(m, "owner_jited:\t%u\n", jited);
9780c0ab 568 }
f99bf205
DB
569}
570#endif
571
6e71b04a
CF
572static 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
fc970227
AN
590/* 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
f66e448c 658const struct file_operations bpf_map_fops = {
f99bf205
DB
659#ifdef CONFIG_PROC_FS
660 .show_fdinfo = bpf_map_show_fdinfo,
661#endif
662 .release = bpf_map_release,
6e71b04a
CF
663 .read = bpf_dummy_read,
664 .write = bpf_dummy_write,
fc970227 665 .mmap = bpf_map_mmap,
99c55f7d
AS
666};
667
6e71b04a 668int bpf_map_new_fd(struct bpf_map *map, int flags)
aa79781b 669{
afdb09c7
CF
670 int ret;
671
672 ret = security_bpf_map(map, OPEN_FMODE(flags));
673 if (ret < 0)
674 return ret;
675
aa79781b 676 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
6e71b04a
CF
677 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;
aa79781b
DB
689}
690
99c55f7d
AS
691/* 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
cb4d2b3f
MKL
699/* dst and src must have at least BPF_OBJ_NAME_LEN number of bytes.
700 * Return 0 on success and < 0 on error.
701 */
702static int bpf_obj_name_cpy(char *dst, const char *src)
703{
704 const char *end = src + BPF_OBJ_NAME_LEN;
705
473d9734 706 memset(dst, 0, BPF_OBJ_NAME_LEN);
3e0ddc4f 707 /* Copy all isalnum(), '_' and '.' chars. */
cb4d2b3f 708 while (src < end && *src) {
3e0ddc4f
DB
709 if (!isalnum(*src) &&
710 *src != '_' && *src != '.')
cb4d2b3f
MKL
711 return -EINVAL;
712 *dst++ = *src++;
713 }
714
715 /* No '\0' found in BPF_OBJ_NAME_LEN number of bytes */
716 if (src == end)
717 return -EINVAL;
718
cb4d2b3f
MKL
719 return 0;
720}
721
e8d2bec0 722int map_check_no_btf(const struct bpf_map *map,
1b2b234b 723 const struct btf *btf,
e8d2bec0
DB
724 const struct btf_type *key_type,
725 const struct btf_type *value_type)
726{
727 return -ENOTSUPP;
728}
729
d83525ca 730static int map_check_btf(struct bpf_map *map, const struct btf *btf,
e8d2bec0
DB
731 u32 btf_key_id, u32 btf_value_id)
732{
733 const struct btf_type *key_type, *value_type;
734 u32 key_size, value_size;
735 int ret = 0;
736
2824ecb7
DB
737 /* Some maps allow key to be unspecified. */
738 if (btf_key_id) {
739 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
740 if (!key_type || key_size != map->key_size)
741 return -EINVAL;
742 } else {
743 key_type = btf_type_by_id(btf, 0);
744 if (!map->ops->map_check_btf)
745 return -EINVAL;
746 }
e8d2bec0
DB
747
748 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
749 if (!value_type || value_size != map->value_size)
750 return -EINVAL;
751
d83525ca
AS
752 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
753
754 if (map_value_has_spin_lock(map)) {
591fe988
DB
755 if (map->map_flags & BPF_F_RDONLY_PROG)
756 return -EACCES;
d83525ca 757 if (map->map_type != BPF_MAP_TYPE_HASH &&
e16d2f1a 758 map->map_type != BPF_MAP_TYPE_ARRAY &&
6ac99e8f
MKL
759 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
760 map->map_type != BPF_MAP_TYPE_SK_STORAGE)
d83525ca
AS
761 return -ENOTSUPP;
762 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
763 map->value_size) {
764 WARN_ONCE(1,
765 "verifier bug spin_lock_off %d value_size %d\n",
766 map->spin_lock_off, map->value_size);
767 return -EFAULT;
768 }
769 }
770
e8d2bec0 771 if (map->ops->map_check_btf)
1b2b234b 772 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
e8d2bec0
DB
773
774 return ret;
775}
776
85d33df3 777#define BPF_MAP_CREATE_LAST_FIELD btf_vmlinux_value_type_id
99c55f7d
AS
778/* called via syscall */
779static int map_create(union bpf_attr *attr)
780{
96eabe7a 781 int numa_node = bpf_map_attr_numa_node(attr);
b936ca64 782 struct bpf_map_memory mem;
99c55f7d 783 struct bpf_map *map;
6e71b04a 784 int f_flags;
99c55f7d
AS
785 int err;
786
787 err = CHECK_ATTR(BPF_MAP_CREATE);
788 if (err)
789 return -EINVAL;
790
85d33df3
MKL
791 if (attr->btf_vmlinux_value_type_id) {
792 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
793 attr->btf_key_type_id || attr->btf_value_type_id)
794 return -EINVAL;
795 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
796 return -EINVAL;
797 }
798
6e71b04a
CF
799 f_flags = bpf_get_file_flag(attr->map_flags);
800 if (f_flags < 0)
801 return f_flags;
802
96eabe7a 803 if (numa_node != NUMA_NO_NODE &&
96e5ae4e
ED
804 ((unsigned int)numa_node >= nr_node_ids ||
805 !node_online(numa_node)))
96eabe7a
MKL
806 return -EINVAL;
807
99c55f7d
AS
808 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
809 map = find_and_alloc_map(attr);
810 if (IS_ERR(map))
811 return PTR_ERR(map);
812
ad5b177b
MKL
813 err = bpf_obj_name_cpy(map->name, attr->map_name);
814 if (err)
b936ca64 815 goto free_map;
ad5b177b 816
1e0bd5a0
AN
817 atomic64_set(&map->refcnt, 1);
818 atomic64_set(&map->usercnt, 1);
fc970227 819 mutex_init(&map->freeze_mutex);
99c55f7d 820
85d33df3
MKL
821 map->spin_lock_off = -EINVAL;
822 if (attr->btf_key_type_id || attr->btf_value_type_id ||
823 /* Even the map's value is a kernel's struct,
824 * the bpf_prog.o must have BTF to begin with
825 * to figure out the corresponding kernel's
826 * counter part. Thus, attr->btf_fd has
827 * to be valid also.
828 */
829 attr->btf_vmlinux_value_type_id) {
a26ca7c9
MKL
830 struct btf *btf;
831
a26ca7c9
MKL
832 btf = btf_get_by_fd(attr->btf_fd);
833 if (IS_ERR(btf)) {
834 err = PTR_ERR(btf);
b936ca64 835 goto free_map;
a26ca7c9 836 }
85d33df3 837 map->btf = btf;
a26ca7c9 838
85d33df3
MKL
839 if (attr->btf_value_type_id) {
840 err = map_check_btf(map, btf, attr->btf_key_type_id,
841 attr->btf_value_type_id);
842 if (err)
843 goto free_map;
a26ca7c9
MKL
844 }
845
9b2cf328
MKL
846 map->btf_key_type_id = attr->btf_key_type_id;
847 map->btf_value_type_id = attr->btf_value_type_id;
85d33df3
MKL
848 map->btf_vmlinux_value_type_id =
849 attr->btf_vmlinux_value_type_id;
a26ca7c9
MKL
850 }
851
afdb09c7 852 err = security_bpf_map_alloc(map);
aaac3ba9 853 if (err)
b936ca64 854 goto free_map;
afdb09c7 855
f3f1c054
MKL
856 err = bpf_map_alloc_id(map);
857 if (err)
b936ca64 858 goto free_map_sec;
f3f1c054 859
6e71b04a 860 err = bpf_map_new_fd(map, f_flags);
bd5f5f4e
MKL
861 if (err < 0) {
862 /* failed to allocate fd.
352d20d6 863 * bpf_map_put_with_uref() is needed because the above
bd5f5f4e
MKL
864 * bpf_map_alloc_id() has published the map
865 * to the userspace and the userspace may
866 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
867 */
352d20d6 868 bpf_map_put_with_uref(map);
bd5f5f4e
MKL
869 return err;
870 }
99c55f7d
AS
871
872 return err;
873
afdb09c7
CF
874free_map_sec:
875 security_bpf_map_free(map);
b936ca64 876free_map:
a26ca7c9 877 btf_put(map->btf);
b936ca64 878 bpf_map_charge_move(&mem, &map->memory);
99c55f7d 879 map->ops->map_free(map);
b936ca64 880 bpf_map_charge_finish(&mem);
99c55f7d
AS
881 return err;
882}
883
db20fd2b
AS
884/* if error is returned, fd is released.
885 * On success caller should complete fd access with matching fdput()
886 */
c2101297 887struct bpf_map *__bpf_map_get(struct fd f)
db20fd2b 888{
db20fd2b
AS
889 if (!f.file)
890 return ERR_PTR(-EBADF);
db20fd2b
AS
891 if (f.file->f_op != &bpf_map_fops) {
892 fdput(f);
893 return ERR_PTR(-EINVAL);
894 }
895
c2101297
DB
896 return f.file->private_data;
897}
898
1e0bd5a0 899void bpf_map_inc(struct bpf_map *map)
c9da161c 900{
1e0bd5a0 901 atomic64_inc(&map->refcnt);
c9da161c 902}
630a4d38 903EXPORT_SYMBOL_GPL(bpf_map_inc);
c9da161c 904
1e0bd5a0
AN
905void bpf_map_inc_with_uref(struct bpf_map *map)
906{
907 atomic64_inc(&map->refcnt);
908 atomic64_inc(&map->usercnt);
909}
910EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
911
c9da161c 912struct bpf_map *bpf_map_get_with_uref(u32 ufd)
c2101297
DB
913{
914 struct fd f = fdget(ufd);
915 struct bpf_map *map;
916
917 map = __bpf_map_get(f);
918 if (IS_ERR(map))
919 return map;
920
1e0bd5a0 921 bpf_map_inc_with_uref(map);
c2101297 922 fdput(f);
db20fd2b
AS
923
924 return map;
925}
926
bd5f5f4e 927/* map_idr_lock should have been held */
1e0bd5a0 928static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
bd5f5f4e
MKL
929{
930 int refold;
931
1e0bd5a0 932 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
bd5f5f4e
MKL
933 if (!refold)
934 return ERR_PTR(-ENOENT);
bd5f5f4e 935 if (uref)
1e0bd5a0 936 atomic64_inc(&map->usercnt);
bd5f5f4e
MKL
937
938 return map;
939}
940
1e0bd5a0 941struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
b0e4701c
SF
942{
943 spin_lock_bh(&map_idr_lock);
1e0bd5a0 944 map = __bpf_map_inc_not_zero(map, false);
b0e4701c
SF
945 spin_unlock_bh(&map_idr_lock);
946
947 return map;
948}
949EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
950
b8cdc051
AS
951int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
952{
953 return -ENOTSUPP;
954}
955
c9d29f46
MV
956static void *__bpf_copy_key(void __user *ukey, u64 key_size)
957{
958 if (key_size)
959 return memdup_user(ukey, key_size);
960
961 if (ukey)
962 return ERR_PTR(-EINVAL);
963
964 return NULL;
965}
966
db20fd2b 967/* last field in 'union bpf_attr' used by this command */
96049f3a 968#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
db20fd2b
AS
969
970static int map_lookup_elem(union bpf_attr *attr)
971{
535e7b4b
MS
972 void __user *ukey = u64_to_user_ptr(attr->key);
973 void __user *uvalue = u64_to_user_ptr(attr->value);
db20fd2b 974 int ufd = attr->map_fd;
db20fd2b 975 struct bpf_map *map;
15c14a3d 976 void *key, *value;
15a07b33 977 u32 value_size;
592867bf 978 struct fd f;
db20fd2b
AS
979 int err;
980
981 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
982 return -EINVAL;
983
96049f3a
AS
984 if (attr->flags & ~BPF_F_LOCK)
985 return -EINVAL;
986
592867bf 987 f = fdget(ufd);
c2101297 988 map = __bpf_map_get(f);
db20fd2b
AS
989 if (IS_ERR(map))
990 return PTR_ERR(map);
87df15de 991 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
6e71b04a
CF
992 err = -EPERM;
993 goto err_put;
994 }
995
96049f3a
AS
996 if ((attr->flags & BPF_F_LOCK) &&
997 !map_value_has_spin_lock(map)) {
998 err = -EINVAL;
999 goto err_put;
1000 }
1001
c9d29f46 1002 key = __bpf_copy_key(ukey, map->key_size);
e4448ed8
AV
1003 if (IS_ERR(key)) {
1004 err = PTR_ERR(key);
db20fd2b 1005 goto err_put;
e4448ed8 1006 }
db20fd2b 1007
15c14a3d 1008 value_size = bpf_map_value_size(map);
15a07b33 1009
8ebe667c 1010 err = -ENOMEM;
15a07b33 1011 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
db20fd2b 1012 if (!value)
8ebe667c
AS
1013 goto free_key;
1014
15c14a3d 1015 err = bpf_map_copy_value(map, key, value, attr->flags);
15a07b33 1016 if (err)
8ebe667c 1017 goto free_value;
db20fd2b
AS
1018
1019 err = -EFAULT;
15a07b33 1020 if (copy_to_user(uvalue, value, value_size) != 0)
8ebe667c 1021 goto free_value;
db20fd2b
AS
1022
1023 err = 0;
1024
8ebe667c
AS
1025free_value:
1026 kfree(value);
db20fd2b
AS
1027free_key:
1028 kfree(key);
1029err_put:
1030 fdput(f);
1031 return err;
1032}
1033
1ae80cf3 1034
3274f520 1035#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
db20fd2b
AS
1036
1037static int map_update_elem(union bpf_attr *attr)
1038{
535e7b4b
MS
1039 void __user *ukey = u64_to_user_ptr(attr->key);
1040 void __user *uvalue = u64_to_user_ptr(attr->value);
db20fd2b 1041 int ufd = attr->map_fd;
db20fd2b
AS
1042 struct bpf_map *map;
1043 void *key, *value;
15a07b33 1044 u32 value_size;
592867bf 1045 struct fd f;
db20fd2b
AS
1046 int err;
1047
1048 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1049 return -EINVAL;
1050
592867bf 1051 f = fdget(ufd);
c2101297 1052 map = __bpf_map_get(f);
db20fd2b
AS
1053 if (IS_ERR(map))
1054 return PTR_ERR(map);
87df15de 1055 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
6e71b04a
CF
1056 err = -EPERM;
1057 goto err_put;
1058 }
1059
96049f3a
AS
1060 if ((attr->flags & BPF_F_LOCK) &&
1061 !map_value_has_spin_lock(map)) {
1062 err = -EINVAL;
1063 goto err_put;
1064 }
1065
c9d29f46 1066 key = __bpf_copy_key(ukey, map->key_size);
e4448ed8
AV
1067 if (IS_ERR(key)) {
1068 err = PTR_ERR(key);
db20fd2b 1069 goto err_put;
e4448ed8 1070 }
db20fd2b 1071
15a07b33 1072 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
8f844938 1073 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
b741f163
RG
1074 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
1075 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
15a07b33
AS
1076 value_size = round_up(map->value_size, 8) * num_possible_cpus();
1077 else
1078 value_size = map->value_size;
1079
db20fd2b 1080 err = -ENOMEM;
15a07b33 1081 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
db20fd2b
AS
1082 if (!value)
1083 goto free_key;
1084
1085 err = -EFAULT;
15a07b33 1086 if (copy_from_user(value, uvalue, value_size) != 0)
db20fd2b
AS
1087 goto free_value;
1088
15c14a3d 1089 err = bpf_map_update_value(map, f, key, value, attr->flags);
6710e112 1090
db20fd2b
AS
1091free_value:
1092 kfree(value);
1093free_key:
1094 kfree(key);
1095err_put:
1096 fdput(f);
1097 return err;
1098}
1099
1100#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1101
1102static int map_delete_elem(union bpf_attr *attr)
1103{
535e7b4b 1104 void __user *ukey = u64_to_user_ptr(attr->key);
db20fd2b 1105 int ufd = attr->map_fd;
db20fd2b 1106 struct bpf_map *map;
592867bf 1107 struct fd f;
db20fd2b
AS
1108 void *key;
1109 int err;
1110
1111 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1112 return -EINVAL;
1113
592867bf 1114 f = fdget(ufd);
c2101297 1115 map = __bpf_map_get(f);
db20fd2b
AS
1116 if (IS_ERR(map))
1117 return PTR_ERR(map);
87df15de 1118 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
6e71b04a
CF
1119 err = -EPERM;
1120 goto err_put;
1121 }
1122
c9d29f46 1123 key = __bpf_copy_key(ukey, map->key_size);
e4448ed8
AV
1124 if (IS_ERR(key)) {
1125 err = PTR_ERR(key);
db20fd2b 1126 goto err_put;
e4448ed8 1127 }
db20fd2b 1128
a3884572
JK
1129 if (bpf_map_is_dev_bound(map)) {
1130 err = bpf_map_offload_delete_elem(map, key);
1131 goto out;
85d33df3
MKL
1132 } else if (IS_FD_PROG_ARRAY(map) ||
1133 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1134 /* These maps require sleepable context */
da765a2f
DB
1135 err = map->ops->map_delete_elem(map, key);
1136 goto out;
a3884572
JK
1137 }
1138
b121d1e7
AS
1139 preempt_disable();
1140 __this_cpu_inc(bpf_prog_active);
db20fd2b
AS
1141 rcu_read_lock();
1142 err = map->ops->map_delete_elem(map, key);
1143 rcu_read_unlock();
b121d1e7
AS
1144 __this_cpu_dec(bpf_prog_active);
1145 preempt_enable();
1ae80cf3 1146 maybe_wait_bpf_programs(map);
a3884572 1147out:
db20fd2b
AS
1148 kfree(key);
1149err_put:
1150 fdput(f);
1151 return err;
1152}
1153
1154/* last field in 'union bpf_attr' used by this command */
1155#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1156
1157static int map_get_next_key(union bpf_attr *attr)
1158{
535e7b4b
MS
1159 void __user *ukey = u64_to_user_ptr(attr->key);
1160 void __user *unext_key = u64_to_user_ptr(attr->next_key);
db20fd2b 1161 int ufd = attr->map_fd;
db20fd2b
AS
1162 struct bpf_map *map;
1163 void *key, *next_key;
592867bf 1164 struct fd f;
db20fd2b
AS
1165 int err;
1166
1167 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1168 return -EINVAL;
1169
592867bf 1170 f = fdget(ufd);
c2101297 1171 map = __bpf_map_get(f);
db20fd2b
AS
1172 if (IS_ERR(map))
1173 return PTR_ERR(map);
87df15de 1174 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
6e71b04a
CF
1175 err = -EPERM;
1176 goto err_put;
1177 }
1178
8fe45924 1179 if (ukey) {
c9d29f46 1180 key = __bpf_copy_key(ukey, map->key_size);
e4448ed8
AV
1181 if (IS_ERR(key)) {
1182 err = PTR_ERR(key);
8fe45924 1183 goto err_put;
e4448ed8 1184 }
8fe45924
TQ
1185 } else {
1186 key = NULL;
1187 }
db20fd2b
AS
1188
1189 err = -ENOMEM;
1190 next_key = kmalloc(map->key_size, GFP_USER);
1191 if (!next_key)
1192 goto free_key;
1193
a3884572
JK
1194 if (bpf_map_is_dev_bound(map)) {
1195 err = bpf_map_offload_get_next_key(map, key, next_key);
1196 goto out;
1197 }
1198
db20fd2b
AS
1199 rcu_read_lock();
1200 err = map->ops->map_get_next_key(map, key, next_key);
1201 rcu_read_unlock();
a3884572 1202out:
db20fd2b
AS
1203 if (err)
1204 goto free_next_key;
1205
1206 err = -EFAULT;
1207 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1208 goto free_next_key;
1209
1210 err = 0;
1211
1212free_next_key:
1213 kfree(next_key);
1214free_key:
1215 kfree(key);
1216err_put:
1217 fdput(f);
1218 return err;
1219}
1220
aa2e93b8
BV
1221int generic_map_delete_batch(struct bpf_map *map,
1222 const union bpf_attr *attr,
1223 union bpf_attr __user *uattr)
1224{
1225 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1226 u32 cp, max_count;
1227 int err = 0;
1228 void *key;
1229
1230 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1231 return -EINVAL;
1232
1233 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1234 !map_value_has_spin_lock(map)) {
1235 return -EINVAL;
1236 }
1237
1238 max_count = attr->batch.count;
1239 if (!max_count)
1240 return 0;
1241
2e3a94aa
BV
1242 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1243 if (!key)
1244 return -ENOMEM;
1245
aa2e93b8 1246 for (cp = 0; cp < max_count; cp++) {
2e3a94aa
BV
1247 err = -EFAULT;
1248 if (copy_from_user(key, keys + cp * map->key_size,
1249 map->key_size))
aa2e93b8 1250 break;
aa2e93b8
BV
1251
1252 if (bpf_map_is_dev_bound(map)) {
1253 err = bpf_map_offload_delete_elem(map, key);
1254 break;
1255 }
1256
1257 preempt_disable();
1258 __this_cpu_inc(bpf_prog_active);
1259 rcu_read_lock();
1260 err = map->ops->map_delete_elem(map, key);
1261 rcu_read_unlock();
1262 __this_cpu_dec(bpf_prog_active);
1263 preempt_enable();
1264 maybe_wait_bpf_programs(map);
1265 if (err)
1266 break;
1267 }
1268 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1269 err = -EFAULT;
2e3a94aa
BV
1270
1271 kfree(key);
aa2e93b8
BV
1272 return err;
1273}
1274
1275int generic_map_update_batch(struct bpf_map *map,
1276 const union bpf_attr *attr,
1277 union bpf_attr __user *uattr)
1278{
1279 void __user *values = u64_to_user_ptr(attr->batch.values);
1280 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1281 u32 value_size, cp, max_count;
1282 int ufd = attr->map_fd;
1283 void *key, *value;
1284 struct fd f;
1285 int err = 0;
1286
1287 f = fdget(ufd);
1288 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1289 return -EINVAL;
1290
1291 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1292 !map_value_has_spin_lock(map)) {
1293 return -EINVAL;
1294 }
1295
1296 value_size = bpf_map_value_size(map);
1297
1298 max_count = attr->batch.count;
1299 if (!max_count)
1300 return 0;
1301
2e3a94aa
BV
1302 key = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1303 if (!key)
1304 return -ENOMEM;
1305
aa2e93b8 1306 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
2e3a94aa
BV
1307 if (!value) {
1308 kfree(key);
aa2e93b8 1309 return -ENOMEM;
2e3a94aa 1310 }
aa2e93b8
BV
1311
1312 for (cp = 0; cp < max_count; cp++) {
aa2e93b8 1313 err = -EFAULT;
2e3a94aa
BV
1314 if (copy_from_user(key, keys + cp * map->key_size,
1315 map->key_size) ||
1316 copy_from_user(value, values + cp * value_size, value_size))
aa2e93b8
BV
1317 break;
1318
1319 err = bpf_map_update_value(map, f, key, value,
1320 attr->batch.elem_flags);
1321
1322 if (err)
1323 break;
1324 }
1325
1326 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1327 err = -EFAULT;
1328
1329 kfree(value);
1330 kfree(key);
1331 return err;
1332}
1333
cb4d03ab
BV
1334#define MAP_LOOKUP_RETRIES 3
1335
1336int generic_map_lookup_batch(struct bpf_map *map,
1337 const union bpf_attr *attr,
1338 union bpf_attr __user *uattr)
1339{
1340 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1341 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1342 void __user *values = u64_to_user_ptr(attr->batch.values);
1343 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1344 void *buf, *buf_prevkey, *prev_key, *key, *value;
1345 int err, retry = MAP_LOOKUP_RETRIES;
1346 u32 value_size, cp, max_count;
cb4d03ab
BV
1347
1348 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1349 return -EINVAL;
1350
1351 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1352 !map_value_has_spin_lock(map))
1353 return -EINVAL;
1354
1355 value_size = bpf_map_value_size(map);
1356
1357 max_count = attr->batch.count;
1358 if (!max_count)
1359 return 0;
1360
1361 if (put_user(0, &uattr->batch.count))
1362 return -EFAULT;
1363
1364 buf_prevkey = kmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
1365 if (!buf_prevkey)
1366 return -ENOMEM;
1367
1368 buf = kmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
1369 if (!buf) {
1370 kvfree(buf_prevkey);
1371 return -ENOMEM;
1372 }
1373
1374 err = -EFAULT;
cb4d03ab
BV
1375 prev_key = NULL;
1376 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1377 goto free_buf;
1378 key = buf;
1379 value = key + map->key_size;
1380 if (ubatch)
1381 prev_key = buf_prevkey;
1382
1383 for (cp = 0; cp < max_count;) {
1384 rcu_read_lock();
1385 err = map->ops->map_get_next_key(map, prev_key, key);
1386 rcu_read_unlock();
1387 if (err)
1388 break;
1389 err = bpf_map_copy_value(map, key, value,
1390 attr->batch.elem_flags);
1391
1392 if (err == -ENOENT) {
1393 if (retry) {
1394 retry--;
1395 continue;
1396 }
1397 err = -EINTR;
1398 break;
1399 }
1400
1401 if (err)
1402 goto free_buf;
1403
1404 if (copy_to_user(keys + cp * map->key_size, key,
1405 map->key_size)) {
1406 err = -EFAULT;
1407 goto free_buf;
1408 }
1409 if (copy_to_user(values + cp * value_size, value, value_size)) {
1410 err = -EFAULT;
1411 goto free_buf;
1412 }
1413
1414 if (!prev_key)
1415 prev_key = buf_prevkey;
1416
1417 swap(prev_key, key);
1418 retry = MAP_LOOKUP_RETRIES;
1419 cp++;
1420 }
1421
1422 if (err == -EFAULT)
1423 goto free_buf;
1424
1425 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1426 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1427 err = -EFAULT;
1428
1429free_buf:
1430 kfree(buf_prevkey);
1431 kfree(buf);
1432 return err;
1433}
1434
bd513cd0
MV
1435#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD value
1436
1437static int map_lookup_and_delete_elem(union bpf_attr *attr)
1438{
1439 void __user *ukey = u64_to_user_ptr(attr->key);
1440 void __user *uvalue = u64_to_user_ptr(attr->value);
1441 int ufd = attr->map_fd;
1442 struct bpf_map *map;
540fefc0 1443 void *key, *value;
bd513cd0
MV
1444 u32 value_size;
1445 struct fd f;
1446 int err;
1447
1448 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1449 return -EINVAL;
1450
1451 f = fdget(ufd);
1452 map = __bpf_map_get(f);
1453 if (IS_ERR(map))
1454 return PTR_ERR(map);
87df15de 1455 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
bd513cd0
MV
1456 err = -EPERM;
1457 goto err_put;
1458 }
1459
1460 key = __bpf_copy_key(ukey, map->key_size);
1461 if (IS_ERR(key)) {
1462 err = PTR_ERR(key);
1463 goto err_put;
1464 }
1465
1466 value_size = map->value_size;
1467
1468 err = -ENOMEM;
1469 value = kmalloc(value_size, GFP_USER | __GFP_NOWARN);
1470 if (!value)
1471 goto free_key;
1472
1473 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1474 map->map_type == BPF_MAP_TYPE_STACK) {
1475 err = map->ops->map_pop_elem(map, value);
1476 } else {
1477 err = -ENOTSUPP;
1478 }
1479
1480 if (err)
1481 goto free_value;
1482
1483 if (copy_to_user(uvalue, value, value_size) != 0)
1484 goto free_value;
1485
1486 err = 0;
1487
1488free_value:
1489 kfree(value);
1490free_key:
1491 kfree(key);
1492err_put:
1493 fdput(f);
1494 return err;
1495}
1496
87df15de
DB
1497#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1498
1499static int map_freeze(const union bpf_attr *attr)
1500{
1501 int err = 0, ufd = attr->map_fd;
1502 struct bpf_map *map;
1503 struct fd f;
1504
1505 if (CHECK_ATTR(BPF_MAP_FREEZE))
1506 return -EINVAL;
1507
1508 f = fdget(ufd);
1509 map = __bpf_map_get(f);
1510 if (IS_ERR(map))
1511 return PTR_ERR(map);
fc970227
AN
1512
1513 mutex_lock(&map->freeze_mutex);
1514
1515 if (map->writecnt) {
1516 err = -EBUSY;
1517 goto err_put;
1518 }
87df15de
DB
1519 if (READ_ONCE(map->frozen)) {
1520 err = -EBUSY;
1521 goto err_put;
1522 }
1523 if (!capable(CAP_SYS_ADMIN)) {
1524 err = -EPERM;
1525 goto err_put;
1526 }
1527
1528 WRITE_ONCE(map->frozen, true);
1529err_put:
fc970227 1530 mutex_unlock(&map->freeze_mutex);
87df15de
DB
1531 fdput(f);
1532 return err;
1533}
1534
7de16e3a 1535static const struct bpf_prog_ops * const bpf_prog_types[] = {
91cc1a99 1536#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
7de16e3a
JK
1537 [_id] = & _name ## _prog_ops,
1538#define BPF_MAP_TYPE(_id, _ops)
1539#include <linux/bpf_types.h>
1540#undef BPF_PROG_TYPE
1541#undef BPF_MAP_TYPE
1542};
1543
09756af4
AS
1544static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1545{
d0f1a451
DB
1546 const struct bpf_prog_ops *ops;
1547
1548 if (type >= ARRAY_SIZE(bpf_prog_types))
1549 return -EINVAL;
1550 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1551 ops = bpf_prog_types[type];
1552 if (!ops)
be9370a7 1553 return -EINVAL;
09756af4 1554
ab3f0063 1555 if (!bpf_prog_is_dev_bound(prog->aux))
d0f1a451 1556 prog->aux->ops = ops;
ab3f0063
JK
1557 else
1558 prog->aux->ops = &bpf_offload_prog_ops;
be9370a7
JB
1559 prog->type = type;
1560 return 0;
09756af4
AS
1561}
1562
bae141f5
DB
1563enum bpf_audit {
1564 BPF_AUDIT_LOAD,
1565 BPF_AUDIT_UNLOAD,
1566 BPF_AUDIT_MAX,
1567};
1568
1569static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1570 [BPF_AUDIT_LOAD] = "LOAD",
1571 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1572};
1573
1574static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1575{
1576 struct audit_context *ctx = NULL;
1577 struct audit_buffer *ab;
1578
1579 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1580 return;
1581 if (audit_enabled == AUDIT_OFF)
1582 return;
1583 if (op == BPF_AUDIT_LOAD)
1584 ctx = audit_context();
1585 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1586 if (unlikely(!ab))
1587 return;
1588 audit_log_format(ab, "prog-id=%u op=%s",
1589 prog->aux->id, bpf_audit_str[op]);
1590 audit_log_end(ab);
1591}
1592
5ccb071e
DB
1593int __bpf_prog_charge(struct user_struct *user, u32 pages)
1594{
1595 unsigned long memlock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1596 unsigned long user_bufs;
1597
1598 if (user) {
1599 user_bufs = atomic_long_add_return(pages, &user->locked_vm);
1600 if (user_bufs > memlock_limit) {
1601 atomic_long_sub(pages, &user->locked_vm);
1602 return -EPERM;
1603 }
1604 }
1605
1606 return 0;
1607}
1608
1609void __bpf_prog_uncharge(struct user_struct *user, u32 pages)
1610{
1611 if (user)
1612 atomic_long_sub(pages, &user->locked_vm);
1613}
1614
aaac3ba9
AS
1615static int bpf_prog_charge_memlock(struct bpf_prog *prog)
1616{
1617 struct user_struct *user = get_current_user();
5ccb071e 1618 int ret;
aaac3ba9 1619
5ccb071e
DB
1620 ret = __bpf_prog_charge(user, prog->pages);
1621 if (ret) {
aaac3ba9 1622 free_uid(user);
5ccb071e 1623 return ret;
aaac3ba9 1624 }
5ccb071e 1625
aaac3ba9
AS
1626 prog->aux->user = user;
1627 return 0;
1628}
1629
1630static void bpf_prog_uncharge_memlock(struct bpf_prog *prog)
1631{
1632 struct user_struct *user = prog->aux->user;
1633
5ccb071e 1634 __bpf_prog_uncharge(user, prog->pages);
aaac3ba9
AS
1635 free_uid(user);
1636}
1637
dc4bb0e2
MKL
1638static int bpf_prog_alloc_id(struct bpf_prog *prog)
1639{
1640 int id;
1641
b76354cd 1642 idr_preload(GFP_KERNEL);
dc4bb0e2
MKL
1643 spin_lock_bh(&prog_idr_lock);
1644 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1645 if (id > 0)
1646 prog->aux->id = id;
1647 spin_unlock_bh(&prog_idr_lock);
b76354cd 1648 idr_preload_end();
dc4bb0e2
MKL
1649
1650 /* id is in [1, INT_MAX) */
1651 if (WARN_ON_ONCE(!id))
1652 return -ENOSPC;
1653
1654 return id > 0 ? 0 : id;
1655}
1656
ad8ad79f 1657void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
dc4bb0e2 1658{
ad8ad79f
JK
1659 /* cBPF to eBPF migrations are currently not in the idr store.
1660 * Offloaded programs are removed from the store when their device
1661 * disappears - even if someone grabs an fd to them they are unusable,
1662 * simply waiting for refcnt to drop to be freed.
1663 */
dc4bb0e2
MKL
1664 if (!prog->aux->id)
1665 return;
1666
b16d9aa4
MKL
1667 if (do_idr_lock)
1668 spin_lock_bh(&prog_idr_lock);
1669 else
1670 __acquire(&prog_idr_lock);
1671
dc4bb0e2 1672 idr_remove(&prog_idr, prog->aux->id);
ad8ad79f 1673 prog->aux->id = 0;
b16d9aa4
MKL
1674
1675 if (do_idr_lock)
1676 spin_unlock_bh(&prog_idr_lock);
1677 else
1678 __release(&prog_idr_lock);
dc4bb0e2
MKL
1679}
1680
1aacde3d 1681static void __bpf_prog_put_rcu(struct rcu_head *rcu)
abf2e7d6
AS
1682{
1683 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
1684
3b4d9eb2 1685 kvfree(aux->func_info);
8c1b6e69 1686 kfree(aux->func_info_aux);
aaac3ba9 1687 bpf_prog_uncharge_memlock(aux->prog);
afdb09c7 1688 security_bpf_prog_free(aux);
abf2e7d6
AS
1689 bpf_prog_free(aux->prog);
1690}
1691
cd7455f1
DB
1692static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
1693{
1694 bpf_prog_kallsyms_del_all(prog);
1695 btf_put(prog->aux->btf);
cd7455f1
DB
1696 bpf_prog_free_linfo(prog);
1697
1698 if (deferred)
1699 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
1700 else
1701 __bpf_prog_put_rcu(&prog->aux->rcu);
1702}
1703
b16d9aa4 1704static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
09756af4 1705{
85192dbf 1706 if (atomic64_dec_and_test(&prog->aux->refcnt)) {
6ee52e2a 1707 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
bae141f5 1708 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
34ad5580 1709 /* bpf_prog_free_id() must be called first */
b16d9aa4 1710 bpf_prog_free_id(prog, do_idr_lock);
cd7455f1 1711 __bpf_prog_put_noref(prog, true);
a67edbf4 1712 }
09756af4 1713}
b16d9aa4
MKL
1714
1715void bpf_prog_put(struct bpf_prog *prog)
1716{
1717 __bpf_prog_put(prog, true);
1718}
e2e9b654 1719EXPORT_SYMBOL_GPL(bpf_prog_put);
09756af4
AS
1720
1721static int bpf_prog_release(struct inode *inode, struct file *filp)
1722{
1723 struct bpf_prog *prog = filp->private_data;
1724
1aacde3d 1725 bpf_prog_put(prog);
09756af4
AS
1726 return 0;
1727}
1728
492ecee8
AS
1729static void bpf_prog_get_stats(const struct bpf_prog *prog,
1730 struct bpf_prog_stats *stats)
1731{
1732 u64 nsecs = 0, cnt = 0;
1733 int cpu;
1734
1735 for_each_possible_cpu(cpu) {
1736 const struct bpf_prog_stats *st;
1737 unsigned int start;
1738 u64 tnsecs, tcnt;
1739
1740 st = per_cpu_ptr(prog->aux->stats, cpu);
1741 do {
1742 start = u64_stats_fetch_begin_irq(&st->syncp);
1743 tnsecs = st->nsecs;
1744 tcnt = st->cnt;
1745 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
1746 nsecs += tnsecs;
1747 cnt += tcnt;
1748 }
1749 stats->nsecs = nsecs;
1750 stats->cnt = cnt;
1751}
1752
7bd509e3
DB
1753#ifdef CONFIG_PROC_FS
1754static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
1755{
1756 const struct bpf_prog *prog = filp->private_data;
f1f7714e 1757 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
492ecee8 1758 struct bpf_prog_stats stats;
7bd509e3 1759
492ecee8 1760 bpf_prog_get_stats(prog, &stats);
f1f7714e 1761 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
7bd509e3
DB
1762 seq_printf(m,
1763 "prog_type:\t%u\n"
1764 "prog_jited:\t%u\n"
f1f7714e 1765 "prog_tag:\t%s\n"
4316b409 1766 "memlock:\t%llu\n"
492ecee8
AS
1767 "prog_id:\t%u\n"
1768 "run_time_ns:\t%llu\n"
1769 "run_cnt:\t%llu\n",
7bd509e3
DB
1770 prog->type,
1771 prog->jited,
f1f7714e 1772 prog_tag,
4316b409 1773 prog->pages * 1ULL << PAGE_SHIFT,
492ecee8
AS
1774 prog->aux->id,
1775 stats.nsecs,
1776 stats.cnt);
7bd509e3
DB
1777}
1778#endif
1779
f66e448c 1780const struct file_operations bpf_prog_fops = {
7bd509e3
DB
1781#ifdef CONFIG_PROC_FS
1782 .show_fdinfo = bpf_prog_show_fdinfo,
1783#endif
1784 .release = bpf_prog_release,
6e71b04a
CF
1785 .read = bpf_dummy_read,
1786 .write = bpf_dummy_write,
09756af4
AS
1787};
1788
b2197755 1789int bpf_prog_new_fd(struct bpf_prog *prog)
aa79781b 1790{
afdb09c7
CF
1791 int ret;
1792
1793 ret = security_bpf_prog(prog);
1794 if (ret < 0)
1795 return ret;
1796
aa79781b
DB
1797 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
1798 O_RDWR | O_CLOEXEC);
1799}
1800
113214be 1801static struct bpf_prog *____bpf_prog_get(struct fd f)
09756af4 1802{
09756af4
AS
1803 if (!f.file)
1804 return ERR_PTR(-EBADF);
09756af4
AS
1805 if (f.file->f_op != &bpf_prog_fops) {
1806 fdput(f);
1807 return ERR_PTR(-EINVAL);
1808 }
1809
c2101297 1810 return f.file->private_data;
09756af4
AS
1811}
1812
85192dbf 1813void bpf_prog_add(struct bpf_prog *prog, int i)
92117d84 1814{
85192dbf 1815 atomic64_add(i, &prog->aux->refcnt);
92117d84 1816}
59d3656d
BB
1817EXPORT_SYMBOL_GPL(bpf_prog_add);
1818
c540594f
DB
1819void bpf_prog_sub(struct bpf_prog *prog, int i)
1820{
1821 /* Only to be used for undoing previous bpf_prog_add() in some
1822 * error path. We still know that another entity in our call
1823 * path holds a reference to the program, thus atomic_sub() can
1824 * be safely used in such cases!
1825 */
85192dbf 1826 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
c540594f
DB
1827}
1828EXPORT_SYMBOL_GPL(bpf_prog_sub);
1829
85192dbf 1830void bpf_prog_inc(struct bpf_prog *prog)
59d3656d 1831{
85192dbf 1832 atomic64_inc(&prog->aux->refcnt);
59d3656d 1833}
97bc402d 1834EXPORT_SYMBOL_GPL(bpf_prog_inc);
92117d84 1835
b16d9aa4 1836/* prog_idr_lock should have been held */
a6f6df69 1837struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
b16d9aa4
MKL
1838{
1839 int refold;
1840
85192dbf 1841 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
b16d9aa4
MKL
1842
1843 if (!refold)
1844 return ERR_PTR(-ENOENT);
1845
1846 return prog;
1847}
a6f6df69 1848EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
b16d9aa4 1849
040ee692 1850bool bpf_prog_get_ok(struct bpf_prog *prog,
288b3de5 1851 enum bpf_prog_type *attach_type, bool attach_drv)
248f346f 1852{
288b3de5
JK
1853 /* not an attachment, just a refcount inc, always allow */
1854 if (!attach_type)
1855 return true;
248f346f
JK
1856
1857 if (prog->type != *attach_type)
1858 return false;
288b3de5 1859 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
248f346f
JK
1860 return false;
1861
1862 return true;
1863}
1864
1865static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
288b3de5 1866 bool attach_drv)
09756af4
AS
1867{
1868 struct fd f = fdget(ufd);
1869 struct bpf_prog *prog;
1870
113214be 1871 prog = ____bpf_prog_get(f);
09756af4
AS
1872 if (IS_ERR(prog))
1873 return prog;
288b3de5 1874 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
113214be
DB
1875 prog = ERR_PTR(-EINVAL);
1876 goto out;
1877 }
09756af4 1878
85192dbf 1879 bpf_prog_inc(prog);
113214be 1880out:
09756af4
AS
1881 fdput(f);
1882 return prog;
1883}
113214be
DB
1884
1885struct bpf_prog *bpf_prog_get(u32 ufd)
1886{
288b3de5 1887 return __bpf_prog_get(ufd, NULL, false);
113214be
DB
1888}
1889
248f346f 1890struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
288b3de5 1891 bool attach_drv)
248f346f 1892{
4d220ed0 1893 return __bpf_prog_get(ufd, &type, attach_drv);
248f346f 1894}
6c8dfe21 1895EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
248f346f 1896
aac3fc32
AI
1897/* Initially all BPF programs could be loaded w/o specifying
1898 * expected_attach_type. Later for some of them specifying expected_attach_type
1899 * at load time became required so that program could be validated properly.
1900 * Programs of types that are allowed to be loaded both w/ and w/o (for
1901 * backward compatibility) expected_attach_type, should have the default attach
1902 * type assigned to expected_attach_type for the latter case, so that it can be
1903 * validated later at attach time.
1904 *
1905 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
1906 * prog type requires it but has some attach types that have to be backward
1907 * compatible.
1908 */
1909static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
1910{
1911 switch (attr->prog_type) {
1912 case BPF_PROG_TYPE_CGROUP_SOCK:
1913 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
1914 * exist so checking for non-zero is the way to go here.
1915 */
1916 if (!attr->expected_attach_type)
1917 attr->expected_attach_type =
1918 BPF_CGROUP_INET_SOCK_CREATE;
1919 break;
1920 }
1921}
1922
5e43f899 1923static int
ccfe29eb
AS
1924bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
1925 enum bpf_attach_type expected_attach_type,
5b92a28a 1926 u32 btf_id, u32 prog_fd)
5e43f899 1927{
27ae7997 1928 if (btf_id) {
c108e3c1
AS
1929 if (btf_id > BTF_MAX_TYPE)
1930 return -EINVAL;
27ae7997
MKL
1931
1932 switch (prog_type) {
1933 case BPF_PROG_TYPE_TRACING:
1934 case BPF_PROG_TYPE_STRUCT_OPS:
be8704ff 1935 case BPF_PROG_TYPE_EXT:
27ae7997
MKL
1936 break;
1937 default:
c108e3c1 1938 return -EINVAL;
27ae7997 1939 }
c108e3c1
AS
1940 }
1941
be8704ff
AS
1942 if (prog_fd && prog_type != BPF_PROG_TYPE_TRACING &&
1943 prog_type != BPF_PROG_TYPE_EXT)
27ae7997
MKL
1944 return -EINVAL;
1945
4fbac77d 1946 switch (prog_type) {
aac3fc32
AI
1947 case BPF_PROG_TYPE_CGROUP_SOCK:
1948 switch (expected_attach_type) {
1949 case BPF_CGROUP_INET_SOCK_CREATE:
1950 case BPF_CGROUP_INET4_POST_BIND:
1951 case BPF_CGROUP_INET6_POST_BIND:
1952 return 0;
1953 default:
1954 return -EINVAL;
1955 }
4fbac77d
AI
1956 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1957 switch (expected_attach_type) {
1958 case BPF_CGROUP_INET4_BIND:
1959 case BPF_CGROUP_INET6_BIND:
d74bad4e
AI
1960 case BPF_CGROUP_INET4_CONNECT:
1961 case BPF_CGROUP_INET6_CONNECT:
1cedee13
AI
1962 case BPF_CGROUP_UDP4_SENDMSG:
1963 case BPF_CGROUP_UDP6_SENDMSG:
983695fa
DB
1964 case BPF_CGROUP_UDP4_RECVMSG:
1965 case BPF_CGROUP_UDP6_RECVMSG:
4fbac77d
AI
1966 return 0;
1967 default:
1968 return -EINVAL;
1969 }
5cf1e914 1970 case BPF_PROG_TYPE_CGROUP_SKB:
1971 switch (expected_attach_type) {
1972 case BPF_CGROUP_INET_INGRESS:
1973 case BPF_CGROUP_INET_EGRESS:
1974 return 0;
1975 default:
1976 return -EINVAL;
1977 }
0d01da6a
SF
1978 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
1979 switch (expected_attach_type) {
1980 case BPF_CGROUP_SETSOCKOPT:
1981 case BPF_CGROUP_GETSOCKOPT:
1982 return 0;
1983 default:
1984 return -EINVAL;
1985 }
be8704ff
AS
1986 case BPF_PROG_TYPE_EXT:
1987 if (expected_attach_type)
1988 return -EINVAL;
1989 /* fallthrough */
4fbac77d
AI
1990 default:
1991 return 0;
1992 }
5e43f899
AI
1993}
1994
09756af4 1995/* last field in 'union bpf_attr' used by this command */
5b92a28a 1996#define BPF_PROG_LOAD_LAST_FIELD attach_prog_fd
09756af4 1997
838e9690 1998static int bpf_prog_load(union bpf_attr *attr, union bpf_attr __user *uattr)
09756af4
AS
1999{
2000 enum bpf_prog_type type = attr->prog_type;
2001 struct bpf_prog *prog;
2002 int err;
2003 char license[128];
2004 bool is_gpl;
2005
2006 if (CHECK_ATTR(BPF_PROG_LOAD))
2007 return -EINVAL;
2008
c240eff6
JW
2009 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2010 BPF_F_ANY_ALIGNMENT |
10d274e8 2011 BPF_F_TEST_STATE_FREQ |
c240eff6 2012 BPF_F_TEST_RND_HI32))
e07b98d9
DM
2013 return -EINVAL;
2014
e9ee9efc
DM
2015 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2016 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2017 !capable(CAP_SYS_ADMIN))
2018 return -EPERM;
2019
09756af4 2020 /* copy eBPF program license from user space */
535e7b4b 2021 if (strncpy_from_user(license, u64_to_user_ptr(attr->license),
09756af4
AS
2022 sizeof(license) - 1) < 0)
2023 return -EFAULT;
2024 license[sizeof(license) - 1] = 0;
2025
2026 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2027 is_gpl = license_is_gpl_compatible(license);
2028
c04c0d2b
AS
2029 if (attr->insn_cnt == 0 ||
2030 attr->insn_cnt > (capable(CAP_SYS_ADMIN) ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
ef0915ca 2031 return -E2BIG;
80b7d819
CF
2032 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2033 type != BPF_PROG_TYPE_CGROUP_SKB &&
2034 !capable(CAP_SYS_ADMIN))
1be7f75d
AS
2035 return -EPERM;
2036
aac3fc32 2037 bpf_prog_load_fixup_attach_type(attr);
ccfe29eb 2038 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
5b92a28a
AS
2039 attr->attach_btf_id,
2040 attr->attach_prog_fd))
5e43f899
AI
2041 return -EINVAL;
2042
09756af4
AS
2043 /* plain bpf_prog allocation */
2044 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
2045 if (!prog)
2046 return -ENOMEM;
2047
5e43f899 2048 prog->expected_attach_type = attr->expected_attach_type;
ccfe29eb 2049 prog->aux->attach_btf_id = attr->attach_btf_id;
5b92a28a
AS
2050 if (attr->attach_prog_fd) {
2051 struct bpf_prog *tgt_prog;
2052
2053 tgt_prog = bpf_prog_get(attr->attach_prog_fd);
2054 if (IS_ERR(tgt_prog)) {
2055 err = PTR_ERR(tgt_prog);
2056 goto free_prog_nouncharge;
2057 }
2058 prog->aux->linked_prog = tgt_prog;
2059 }
5e43f899 2060
9a18eedb
JK
2061 prog->aux->offload_requested = !!attr->prog_ifindex;
2062
afdb09c7 2063 err = security_bpf_prog_alloc(prog->aux);
aaac3ba9
AS
2064 if (err)
2065 goto free_prog_nouncharge;
2066
afdb09c7
CF
2067 err = bpf_prog_charge_memlock(prog);
2068 if (err)
2069 goto free_prog_sec;
2070
09756af4
AS
2071 prog->len = attr->insn_cnt;
2072
2073 err = -EFAULT;
535e7b4b 2074 if (copy_from_user(prog->insns, u64_to_user_ptr(attr->insns),
aafe6ae9 2075 bpf_prog_insn_size(prog)) != 0)
09756af4
AS
2076 goto free_prog;
2077
2078 prog->orig_prog = NULL;
a91263d5 2079 prog->jited = 0;
09756af4 2080
85192dbf 2081 atomic64_set(&prog->aux->refcnt, 1);
a91263d5 2082 prog->gpl_compatible = is_gpl ? 1 : 0;
09756af4 2083
9a18eedb 2084 if (bpf_prog_is_dev_bound(prog->aux)) {
ab3f0063
JK
2085 err = bpf_prog_offload_init(prog, attr);
2086 if (err)
2087 goto free_prog;
2088 }
2089
09756af4
AS
2090 /* find program type: socket_filter vs tracing_filter */
2091 err = find_prog_type(type, prog);
2092 if (err < 0)
2093 goto free_prog;
2094
9285ec4c 2095 prog->aux->load_time = ktime_get_boottime_ns();
cb4d2b3f
MKL
2096 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name);
2097 if (err)
2098 goto free_prog;
2099
09756af4 2100 /* run eBPF verifier */
838e9690 2101 err = bpf_check(&prog, attr, uattr);
09756af4
AS
2102 if (err < 0)
2103 goto free_used_maps;
2104
9facc336 2105 prog = bpf_prog_select_runtime(prog, &err);
04fd61ab
AS
2106 if (err < 0)
2107 goto free_used_maps;
09756af4 2108
dc4bb0e2
MKL
2109 err = bpf_prog_alloc_id(prog);
2110 if (err)
2111 goto free_used_maps;
2112
c751798a
DB
2113 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2114 * effectively publicly exposed. However, retrieving via
2115 * bpf_prog_get_fd_by_id() will take another reference,
2116 * therefore it cannot be gone underneath us.
2117 *
2118 * Only for the time /after/ successful bpf_prog_new_fd()
2119 * and before returning to userspace, we might just hold
2120 * one reference and any parallel close on that fd could
2121 * rip everything out. Hence, below notifications must
2122 * happen before bpf_prog_new_fd().
2123 *
2124 * Also, any failure handling from this point onwards must
2125 * be using bpf_prog_put() given the program is exposed.
2126 */
74451e66 2127 bpf_prog_kallsyms_add(prog);
6ee52e2a 2128 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
bae141f5 2129 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
c751798a
DB
2130
2131 err = bpf_prog_new_fd(prog);
2132 if (err < 0)
2133 bpf_prog_put(prog);
09756af4
AS
2134 return err;
2135
2136free_used_maps:
cd7455f1
DB
2137 /* In case we have subprogs, we need to wait for a grace
2138 * period before we can tear down JIT memory since symbols
2139 * are already exposed under kallsyms.
2140 */
2141 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2142 return err;
09756af4 2143free_prog:
aaac3ba9 2144 bpf_prog_uncharge_memlock(prog);
afdb09c7
CF
2145free_prog_sec:
2146 security_bpf_prog_free(prog->aux);
aaac3ba9 2147free_prog_nouncharge:
09756af4
AS
2148 bpf_prog_free(prog);
2149 return err;
2150}
2151
6e71b04a 2152#define BPF_OBJ_LAST_FIELD file_flags
b2197755
DB
2153
2154static int bpf_obj_pin(const union bpf_attr *attr)
2155{
6e71b04a 2156 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
b2197755
DB
2157 return -EINVAL;
2158
535e7b4b 2159 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
b2197755
DB
2160}
2161
2162static int bpf_obj_get(const union bpf_attr *attr)
2163{
6e71b04a
CF
2164 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2165 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
b2197755
DB
2166 return -EINVAL;
2167
6e71b04a
CF
2168 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2169 attr->file_flags);
b2197755
DB
2170}
2171
fec56f58
AS
2172static int bpf_tracing_prog_release(struct inode *inode, struct file *filp)
2173{
2174 struct bpf_prog *prog = filp->private_data;
2175
2176 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
2177 bpf_prog_put(prog);
2178 return 0;
2179}
2180
2181static const struct file_operations bpf_tracing_prog_fops = {
2182 .release = bpf_tracing_prog_release,
2183 .read = bpf_dummy_read,
2184 .write = bpf_dummy_write,
2185};
2186
2187static int bpf_tracing_prog_attach(struct bpf_prog *prog)
2188{
2189 int tr_fd, err;
2190
2191 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
be8704ff
AS
2192 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2193 prog->type != BPF_PROG_TYPE_EXT) {
fec56f58
AS
2194 err = -EINVAL;
2195 goto out_put_prog;
2196 }
2197
2198 err = bpf_trampoline_link_prog(prog);
2199 if (err)
2200 goto out_put_prog;
2201
2202 tr_fd = anon_inode_getfd("bpf-tracing-prog", &bpf_tracing_prog_fops,
2203 prog, O_CLOEXEC);
2204 if (tr_fd < 0) {
2205 WARN_ON_ONCE(bpf_trampoline_unlink_prog(prog));
2206 err = tr_fd;
2207 goto out_put_prog;
2208 }
2209 return tr_fd;
2210
2211out_put_prog:
2212 bpf_prog_put(prog);
2213 return err;
2214}
2215
c4f6699d
AS
2216struct bpf_raw_tracepoint {
2217 struct bpf_raw_event_map *btp;
2218 struct bpf_prog *prog;
2219};
2220
2221static int bpf_raw_tracepoint_release(struct inode *inode, struct file *filp)
2222{
2223 struct bpf_raw_tracepoint *raw_tp = filp->private_data;
2224
2225 if (raw_tp->prog) {
2226 bpf_probe_unregister(raw_tp->btp, raw_tp->prog);
2227 bpf_prog_put(raw_tp->prog);
2228 }
a38d1107 2229 bpf_put_raw_tracepoint(raw_tp->btp);
c4f6699d
AS
2230 kfree(raw_tp);
2231 return 0;
2232}
2233
2234static const struct file_operations bpf_raw_tp_fops = {
2235 .release = bpf_raw_tracepoint_release,
2236 .read = bpf_dummy_read,
2237 .write = bpf_dummy_write,
2238};
2239
2240#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
2241
2242static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
2243{
2244 struct bpf_raw_tracepoint *raw_tp;
2245 struct bpf_raw_event_map *btp;
2246 struct bpf_prog *prog;
ac4414b5
AS
2247 const char *tp_name;
2248 char buf[128];
c4f6699d
AS
2249 int tp_fd, err;
2250
ac4414b5
AS
2251 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
2252 return -EINVAL;
2253
2254 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
2255 if (IS_ERR(prog))
2256 return PTR_ERR(prog);
2257
2258 if (prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT &&
f1b9509c 2259 prog->type != BPF_PROG_TYPE_TRACING &&
be8704ff 2260 prog->type != BPF_PROG_TYPE_EXT &&
ac4414b5
AS
2261 prog->type != BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE) {
2262 err = -EINVAL;
2263 goto out_put_prog;
2264 }
2265
be8704ff
AS
2266 if (prog->type == BPF_PROG_TYPE_TRACING ||
2267 prog->type == BPF_PROG_TYPE_EXT) {
ac4414b5 2268 if (attr->raw_tracepoint.name) {
fec56f58
AS
2269 /* The attach point for this category of programs
2270 * should be specified via btf_id during program load.
ac4414b5
AS
2271 */
2272 err = -EINVAL;
2273 goto out_put_prog;
2274 }
fec56f58
AS
2275 if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
2276 tp_name = prog->aux->attach_func_name;
2277 else
2278 return bpf_tracing_prog_attach(prog);
ac4414b5
AS
2279 } else {
2280 if (strncpy_from_user(buf,
2281 u64_to_user_ptr(attr->raw_tracepoint.name),
2282 sizeof(buf) - 1) < 0) {
2283 err = -EFAULT;
2284 goto out_put_prog;
2285 }
2286 buf[sizeof(buf) - 1] = 0;
2287 tp_name = buf;
2288 }
c4f6699d 2289
a38d1107 2290 btp = bpf_get_raw_tracepoint(tp_name);
ac4414b5
AS
2291 if (!btp) {
2292 err = -ENOENT;
2293 goto out_put_prog;
2294 }
c4f6699d
AS
2295
2296 raw_tp = kzalloc(sizeof(*raw_tp), GFP_USER);
a38d1107
MM
2297 if (!raw_tp) {
2298 err = -ENOMEM;
2299 goto out_put_btp;
2300 }
c4f6699d 2301 raw_tp->btp = btp;
ac4414b5 2302 raw_tp->prog = prog;
c4f6699d
AS
2303
2304 err = bpf_probe_register(raw_tp->btp, prog);
2305 if (err)
ac4414b5 2306 goto out_free_tp;
c4f6699d 2307
c4f6699d
AS
2308 tp_fd = anon_inode_getfd("bpf-raw-tracepoint", &bpf_raw_tp_fops, raw_tp,
2309 O_CLOEXEC);
2310 if (tp_fd < 0) {
2311 bpf_probe_unregister(raw_tp->btp, prog);
2312 err = tp_fd;
ac4414b5 2313 goto out_free_tp;
c4f6699d
AS
2314 }
2315 return tp_fd;
2316
c4f6699d
AS
2317out_free_tp:
2318 kfree(raw_tp);
a38d1107
MM
2319out_put_btp:
2320 bpf_put_raw_tracepoint(btp);
ac4414b5
AS
2321out_put_prog:
2322 bpf_prog_put(prog);
c4f6699d
AS
2323 return err;
2324}
2325
33491588
AR
2326static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
2327 enum bpf_attach_type attach_type)
2328{
2329 switch (prog->type) {
2330 case BPF_PROG_TYPE_CGROUP_SOCK:
2331 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
0d01da6a 2332 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
33491588 2333 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
5cf1e914 2334 case BPF_PROG_TYPE_CGROUP_SKB:
2335 return prog->enforce_expected_attach_type &&
2336 prog->expected_attach_type != attach_type ?
2337 -EINVAL : 0;
33491588
AR
2338 default:
2339 return 0;
2340 }
2341}
2342
7dd68b32 2343#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
174a79ff 2344
324bda9e 2345#define BPF_F_ATTACH_MASK \
7dd68b32 2346 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
324bda9e 2347
f4324551
DM
2348static int bpf_prog_attach(const union bpf_attr *attr)
2349{
7f677633 2350 enum bpf_prog_type ptype;
f4324551 2351 struct bpf_prog *prog;
7f677633 2352 int ret;
f4324551
DM
2353
2354 if (!capable(CAP_NET_ADMIN))
2355 return -EPERM;
2356
2357 if (CHECK_ATTR(BPF_PROG_ATTACH))
2358 return -EINVAL;
2359
324bda9e 2360 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
7f677633
AS
2361 return -EINVAL;
2362
f4324551
DM
2363 switch (attr->attach_type) {
2364 case BPF_CGROUP_INET_INGRESS:
2365 case BPF_CGROUP_INET_EGRESS:
b2cd1257 2366 ptype = BPF_PROG_TYPE_CGROUP_SKB;
f4324551 2367 break;
61023658 2368 case BPF_CGROUP_INET_SOCK_CREATE:
aac3fc32
AI
2369 case BPF_CGROUP_INET4_POST_BIND:
2370 case BPF_CGROUP_INET6_POST_BIND:
61023658
DA
2371 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2372 break;
4fbac77d
AI
2373 case BPF_CGROUP_INET4_BIND:
2374 case BPF_CGROUP_INET6_BIND:
d74bad4e
AI
2375 case BPF_CGROUP_INET4_CONNECT:
2376 case BPF_CGROUP_INET6_CONNECT:
1cedee13
AI
2377 case BPF_CGROUP_UDP4_SENDMSG:
2378 case BPF_CGROUP_UDP6_SENDMSG:
983695fa
DB
2379 case BPF_CGROUP_UDP4_RECVMSG:
2380 case BPF_CGROUP_UDP6_RECVMSG:
4fbac77d
AI
2381 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2382 break;
40304b2a
LB
2383 case BPF_CGROUP_SOCK_OPS:
2384 ptype = BPF_PROG_TYPE_SOCK_OPS;
2385 break;
ebc614f6
RG
2386 case BPF_CGROUP_DEVICE:
2387 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2388 break;
4f738adb 2389 case BPF_SK_MSG_VERDICT:
fdb5c453
SY
2390 ptype = BPF_PROG_TYPE_SK_MSG;
2391 break;
464bc0fd
JF
2392 case BPF_SK_SKB_STREAM_PARSER:
2393 case BPF_SK_SKB_STREAM_VERDICT:
fdb5c453
SY
2394 ptype = BPF_PROG_TYPE_SK_SKB;
2395 break;
f4364dcf 2396 case BPF_LIRC_MODE2:
fdb5c453
SY
2397 ptype = BPF_PROG_TYPE_LIRC_MODE2;
2398 break;
d58e468b
PP
2399 case BPF_FLOW_DISSECTOR:
2400 ptype = BPF_PROG_TYPE_FLOW_DISSECTOR;
2401 break;
7b146ceb
AI
2402 case BPF_CGROUP_SYSCTL:
2403 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2404 break;
0d01da6a
SF
2405 case BPF_CGROUP_GETSOCKOPT:
2406 case BPF_CGROUP_SETSOCKOPT:
2407 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2408 break;
f4324551
DM
2409 default:
2410 return -EINVAL;
2411 }
2412
b2cd1257
DA
2413 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
2414 if (IS_ERR(prog))
2415 return PTR_ERR(prog);
2416
5e43f899
AI
2417 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
2418 bpf_prog_put(prog);
2419 return -EINVAL;
2420 }
2421
fdb5c453
SY
2422 switch (ptype) {
2423 case BPF_PROG_TYPE_SK_SKB:
2424 case BPF_PROG_TYPE_SK_MSG:
604326b4 2425 ret = sock_map_get_from_fd(attr, prog);
fdb5c453
SY
2426 break;
2427 case BPF_PROG_TYPE_LIRC_MODE2:
2428 ret = lirc_prog_attach(attr, prog);
2429 break;
d58e468b
PP
2430 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2431 ret = skb_flow_dissector_bpf_prog_attach(attr, prog);
2432 break;
fdb5c453
SY
2433 default:
2434 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
b2cd1257
DA
2435 }
2436
7f677633
AS
2437 if (ret)
2438 bpf_prog_put(prog);
7f677633 2439 return ret;
f4324551
DM
2440}
2441
2442#define BPF_PROG_DETACH_LAST_FIELD attach_type
2443
2444static int bpf_prog_detach(const union bpf_attr *attr)
2445{
324bda9e 2446 enum bpf_prog_type ptype;
f4324551
DM
2447
2448 if (!capable(CAP_NET_ADMIN))
2449 return -EPERM;
2450
2451 if (CHECK_ATTR(BPF_PROG_DETACH))
2452 return -EINVAL;
2453
2454 switch (attr->attach_type) {
2455 case BPF_CGROUP_INET_INGRESS:
2456 case BPF_CGROUP_INET_EGRESS:
324bda9e
AS
2457 ptype = BPF_PROG_TYPE_CGROUP_SKB;
2458 break;
61023658 2459 case BPF_CGROUP_INET_SOCK_CREATE:
aac3fc32
AI
2460 case BPF_CGROUP_INET4_POST_BIND:
2461 case BPF_CGROUP_INET6_POST_BIND:
324bda9e
AS
2462 ptype = BPF_PROG_TYPE_CGROUP_SOCK;
2463 break;
4fbac77d
AI
2464 case BPF_CGROUP_INET4_BIND:
2465 case BPF_CGROUP_INET6_BIND:
d74bad4e
AI
2466 case BPF_CGROUP_INET4_CONNECT:
2467 case BPF_CGROUP_INET6_CONNECT:
1cedee13
AI
2468 case BPF_CGROUP_UDP4_SENDMSG:
2469 case BPF_CGROUP_UDP6_SENDMSG:
983695fa
DB
2470 case BPF_CGROUP_UDP4_RECVMSG:
2471 case BPF_CGROUP_UDP6_RECVMSG:
4fbac77d
AI
2472 ptype = BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
2473 break;
40304b2a 2474 case BPF_CGROUP_SOCK_OPS:
324bda9e 2475 ptype = BPF_PROG_TYPE_SOCK_OPS;
f4324551 2476 break;
ebc614f6
RG
2477 case BPF_CGROUP_DEVICE:
2478 ptype = BPF_PROG_TYPE_CGROUP_DEVICE;
2479 break;
4f738adb 2480 case BPF_SK_MSG_VERDICT:
604326b4 2481 return sock_map_get_from_fd(attr, NULL);
5a67da2a
JF
2482 case BPF_SK_SKB_STREAM_PARSER:
2483 case BPF_SK_SKB_STREAM_VERDICT:
604326b4 2484 return sock_map_get_from_fd(attr, NULL);
f4364dcf
SY
2485 case BPF_LIRC_MODE2:
2486 return lirc_prog_detach(attr);
d58e468b
PP
2487 case BPF_FLOW_DISSECTOR:
2488 return skb_flow_dissector_bpf_prog_detach(attr);
7b146ceb
AI
2489 case BPF_CGROUP_SYSCTL:
2490 ptype = BPF_PROG_TYPE_CGROUP_SYSCTL;
2491 break;
0d01da6a
SF
2492 case BPF_CGROUP_GETSOCKOPT:
2493 case BPF_CGROUP_SETSOCKOPT:
2494 ptype = BPF_PROG_TYPE_CGROUP_SOCKOPT;
2495 break;
f4324551
DM
2496 default:
2497 return -EINVAL;
2498 }
2499
fdb5c453 2500 return cgroup_bpf_prog_detach(attr, ptype);
f4324551 2501}
40304b2a 2502
468e2f64
AS
2503#define BPF_PROG_QUERY_LAST_FIELD query.prog_cnt
2504
2505static int bpf_prog_query(const union bpf_attr *attr,
2506 union bpf_attr __user *uattr)
2507{
468e2f64
AS
2508 if (!capable(CAP_NET_ADMIN))
2509 return -EPERM;
2510 if (CHECK_ATTR(BPF_PROG_QUERY))
2511 return -EINVAL;
2512 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
2513 return -EINVAL;
2514
2515 switch (attr->query.attach_type) {
2516 case BPF_CGROUP_INET_INGRESS:
2517 case BPF_CGROUP_INET_EGRESS:
2518 case BPF_CGROUP_INET_SOCK_CREATE:
4fbac77d
AI
2519 case BPF_CGROUP_INET4_BIND:
2520 case BPF_CGROUP_INET6_BIND:
aac3fc32
AI
2521 case BPF_CGROUP_INET4_POST_BIND:
2522 case BPF_CGROUP_INET6_POST_BIND:
d74bad4e
AI
2523 case BPF_CGROUP_INET4_CONNECT:
2524 case BPF_CGROUP_INET6_CONNECT:
1cedee13
AI
2525 case BPF_CGROUP_UDP4_SENDMSG:
2526 case BPF_CGROUP_UDP6_SENDMSG:
983695fa
DB
2527 case BPF_CGROUP_UDP4_RECVMSG:
2528 case BPF_CGROUP_UDP6_RECVMSG:
468e2f64 2529 case BPF_CGROUP_SOCK_OPS:
ebc614f6 2530 case BPF_CGROUP_DEVICE:
7b146ceb 2531 case BPF_CGROUP_SYSCTL:
0d01da6a
SF
2532 case BPF_CGROUP_GETSOCKOPT:
2533 case BPF_CGROUP_SETSOCKOPT:
468e2f64 2534 break;
f4364dcf
SY
2535 case BPF_LIRC_MODE2:
2536 return lirc_prog_query(attr, uattr);
118c8e9a
SF
2537 case BPF_FLOW_DISSECTOR:
2538 return skb_flow_dissector_prog_query(attr, uattr);
468e2f64
AS
2539 default:
2540 return -EINVAL;
2541 }
fdb5c453
SY
2542
2543 return cgroup_bpf_prog_query(attr, uattr);
468e2f64 2544}
f4324551 2545
b0b9395d 2546#define BPF_PROG_TEST_RUN_LAST_FIELD test.ctx_out
1cf1cae9
AS
2547
2548static int bpf_prog_test_run(const union bpf_attr *attr,
2549 union bpf_attr __user *uattr)
2550{
2551 struct bpf_prog *prog;
2552 int ret = -ENOTSUPP;
2553
61f3c964
AS
2554 if (!capable(CAP_SYS_ADMIN))
2555 return -EPERM;
1cf1cae9
AS
2556 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
2557 return -EINVAL;
2558
b0b9395d
SF
2559 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
2560 (!attr->test.ctx_size_in && attr->test.ctx_in))
2561 return -EINVAL;
2562
2563 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
2564 (!attr->test.ctx_size_out && attr->test.ctx_out))
2565 return -EINVAL;
2566
1cf1cae9
AS
2567 prog = bpf_prog_get(attr->test.prog_fd);
2568 if (IS_ERR(prog))
2569 return PTR_ERR(prog);
2570
2571 if (prog->aux->ops->test_run)
2572 ret = prog->aux->ops->test_run(prog, attr, uattr);
2573
2574 bpf_prog_put(prog);
2575 return ret;
2576}
2577
34ad5580
MKL
2578#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
2579
2580static int bpf_obj_get_next_id(const union bpf_attr *attr,
2581 union bpf_attr __user *uattr,
2582 struct idr *idr,
2583 spinlock_t *lock)
2584{
2585 u32 next_id = attr->start_id;
2586 int err = 0;
2587
2588 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
2589 return -EINVAL;
2590
2591 if (!capable(CAP_SYS_ADMIN))
2592 return -EPERM;
2593
2594 next_id++;
2595 spin_lock_bh(lock);
2596 if (!idr_get_next(idr, &next_id))
2597 err = -ENOENT;
2598 spin_unlock_bh(lock);
2599
2600 if (!err)
2601 err = put_user(next_id, &uattr->next_id);
2602
2603 return err;
2604}
2605
b16d9aa4
MKL
2606#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
2607
7e6897f9 2608struct bpf_prog *bpf_prog_by_id(u32 id)
b16d9aa4
MKL
2609{
2610 struct bpf_prog *prog;
b16d9aa4 2611
7e6897f9
BT
2612 if (!id)
2613 return ERR_PTR(-ENOENT);
b16d9aa4
MKL
2614
2615 spin_lock_bh(&prog_idr_lock);
2616 prog = idr_find(&prog_idr, id);
2617 if (prog)
2618 prog = bpf_prog_inc_not_zero(prog);
2619 else
2620 prog = ERR_PTR(-ENOENT);
2621 spin_unlock_bh(&prog_idr_lock);
7e6897f9
BT
2622 return prog;
2623}
2624
2625static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
2626{
2627 struct bpf_prog *prog;
2628 u32 id = attr->prog_id;
2629 int fd;
2630
2631 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
2632 return -EINVAL;
2633
2634 if (!capable(CAP_SYS_ADMIN))
2635 return -EPERM;
b16d9aa4 2636
7e6897f9 2637 prog = bpf_prog_by_id(id);
b16d9aa4
MKL
2638 if (IS_ERR(prog))
2639 return PTR_ERR(prog);
2640
2641 fd = bpf_prog_new_fd(prog);
2642 if (fd < 0)
2643 bpf_prog_put(prog);
2644
2645 return fd;
2646}
2647
6e71b04a 2648#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
bd5f5f4e
MKL
2649
2650static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
2651{
2652 struct bpf_map *map;
2653 u32 id = attr->map_id;
6e71b04a 2654 int f_flags;
bd5f5f4e
MKL
2655 int fd;
2656
6e71b04a
CF
2657 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
2658 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
bd5f5f4e
MKL
2659 return -EINVAL;
2660
2661 if (!capable(CAP_SYS_ADMIN))
2662 return -EPERM;
2663
6e71b04a
CF
2664 f_flags = bpf_get_file_flag(attr->open_flags);
2665 if (f_flags < 0)
2666 return f_flags;
2667
bd5f5f4e
MKL
2668 spin_lock_bh(&map_idr_lock);
2669 map = idr_find(&map_idr, id);
2670 if (map)
b0e4701c 2671 map = __bpf_map_inc_not_zero(map, true);
bd5f5f4e
MKL
2672 else
2673 map = ERR_PTR(-ENOENT);
2674 spin_unlock_bh(&map_idr_lock);
2675
2676 if (IS_ERR(map))
2677 return PTR_ERR(map);
2678
6e71b04a 2679 fd = bpf_map_new_fd(map, f_flags);
bd5f5f4e 2680 if (fd < 0)
781e6282 2681 bpf_map_put_with_uref(map);
bd5f5f4e
MKL
2682
2683 return fd;
2684}
2685
7105e828 2686static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
d8eca5bb
DB
2687 unsigned long addr, u32 *off,
2688 u32 *type)
7105e828 2689{
d8eca5bb 2690 const struct bpf_map *map;
7105e828
DB
2691 int i;
2692
d8eca5bb
DB
2693 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
2694 map = prog->aux->used_maps[i];
2695 if (map == (void *)addr) {
2696 *type = BPF_PSEUDO_MAP_FD;
2697 return map;
2698 }
2699 if (!map->ops->map_direct_value_meta)
2700 continue;
2701 if (!map->ops->map_direct_value_meta(map, addr, off)) {
2702 *type = BPF_PSEUDO_MAP_VALUE;
2703 return map;
2704 }
2705 }
2706
7105e828
DB
2707 return NULL;
2708}
2709
2710static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
2711{
2712 const struct bpf_map *map;
2713 struct bpf_insn *insns;
d8eca5bb 2714 u32 off, type;
7105e828
DB
2715 u64 imm;
2716 int i;
2717
2718 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
2719 GFP_USER);
2720 if (!insns)
2721 return insns;
2722
2723 for (i = 0; i < prog->len; i++) {
2724 if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
2725 insns[i].code = BPF_JMP | BPF_CALL;
2726 insns[i].imm = BPF_FUNC_tail_call;
2727 /* fall-through */
2728 }
2729 if (insns[i].code == (BPF_JMP | BPF_CALL) ||
2730 insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
2731 if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
2732 insns[i].code = BPF_JMP | BPF_CALL;
2733 if (!bpf_dump_raw_ok())
2734 insns[i].imm = 0;
2735 continue;
2736 }
2737
2738 if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
2739 continue;
2740
2741 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
d8eca5bb 2742 map = bpf_map_from_imm(prog, imm, &off, &type);
7105e828 2743 if (map) {
d8eca5bb 2744 insns[i].src_reg = type;
7105e828 2745 insns[i].imm = map->id;
d8eca5bb 2746 insns[i + 1].imm = off;
7105e828
DB
2747 continue;
2748 }
7105e828
DB
2749 }
2750
2751 return insns;
2752}
2753
c454a46b
MKL
2754static int set_info_rec_size(struct bpf_prog_info *info)
2755{
2756 /*
2757 * Ensure info.*_rec_size is the same as kernel expected size
2758 *
2759 * or
2760 *
2761 * Only allow zero *_rec_size if both _rec_size and _cnt are
2762 * zero. In this case, the kernel will set the expected
2763 * _rec_size back to the info.
2764 */
2765
11d8b82d 2766 if ((info->nr_func_info || info->func_info_rec_size) &&
c454a46b
MKL
2767 info->func_info_rec_size != sizeof(struct bpf_func_info))
2768 return -EINVAL;
2769
11d8b82d 2770 if ((info->nr_line_info || info->line_info_rec_size) &&
c454a46b
MKL
2771 info->line_info_rec_size != sizeof(struct bpf_line_info))
2772 return -EINVAL;
2773
11d8b82d 2774 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
c454a46b
MKL
2775 info->jited_line_info_rec_size != sizeof(__u64))
2776 return -EINVAL;
2777
2778 info->func_info_rec_size = sizeof(struct bpf_func_info);
2779 info->line_info_rec_size = sizeof(struct bpf_line_info);
2780 info->jited_line_info_rec_size = sizeof(__u64);
2781
2782 return 0;
2783}
2784
1e270976
MKL
2785static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
2786 const union bpf_attr *attr,
2787 union bpf_attr __user *uattr)
2788{
2789 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
2790 struct bpf_prog_info info = {};
2791 u32 info_len = attr->info.info_len;
5f8f8b93 2792 struct bpf_prog_stats stats;
1e270976
MKL
2793 char __user *uinsns;
2794 u32 ulen;
2795 int err;
2796
dcab51f1 2797 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1e270976
MKL
2798 if (err)
2799 return err;
2800 info_len = min_t(u32, sizeof(info), info_len);
2801
2802 if (copy_from_user(&info, uinfo, info_len))
89b09689 2803 return -EFAULT;
1e270976
MKL
2804
2805 info.type = prog->type;
2806 info.id = prog->aux->id;
cb4d2b3f
MKL
2807 info.load_time = prog->aux->load_time;
2808 info.created_by_uid = from_kuid_munged(current_user_ns(),
2809 prog->aux->user->uid);
b85fab0e 2810 info.gpl_compatible = prog->gpl_compatible;
1e270976
MKL
2811
2812 memcpy(info.tag, prog->tag, sizeof(prog->tag));
cb4d2b3f
MKL
2813 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
2814
2815 ulen = info.nr_map_ids;
2816 info.nr_map_ids = prog->aux->used_map_cnt;
2817 ulen = min_t(u32, info.nr_map_ids, ulen);
2818 if (ulen) {
721e08da 2819 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
cb4d2b3f
MKL
2820 u32 i;
2821
2822 for (i = 0; i < ulen; i++)
2823 if (put_user(prog->aux->used_maps[i]->id,
2824 &user_map_ids[i]))
2825 return -EFAULT;
2826 }
1e270976 2827
c454a46b
MKL
2828 err = set_info_rec_size(&info);
2829 if (err)
2830 return err;
7337224f 2831
5f8f8b93
AS
2832 bpf_prog_get_stats(prog, &stats);
2833 info.run_time_ns = stats.nsecs;
2834 info.run_cnt = stats.cnt;
2835
1e270976
MKL
2836 if (!capable(CAP_SYS_ADMIN)) {
2837 info.jited_prog_len = 0;
2838 info.xlated_prog_len = 0;
dbecd738 2839 info.nr_jited_ksyms = 0;
28c2fae7 2840 info.nr_jited_func_lens = 0;
11d8b82d
YS
2841 info.nr_func_info = 0;
2842 info.nr_line_info = 0;
2843 info.nr_jited_line_info = 0;
1e270976
MKL
2844 goto done;
2845 }
2846
1e270976 2847 ulen = info.xlated_prog_len;
9975a54b 2848 info.xlated_prog_len = bpf_prog_insn_size(prog);
1e270976 2849 if (info.xlated_prog_len && ulen) {
7105e828
DB
2850 struct bpf_insn *insns_sanitized;
2851 bool fault;
2852
2853 if (prog->blinded && !bpf_dump_raw_ok()) {
2854 info.xlated_prog_insns = 0;
2855 goto done;
2856 }
2857 insns_sanitized = bpf_insn_prepare_dump(prog);
2858 if (!insns_sanitized)
2859 return -ENOMEM;
1e270976
MKL
2860 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
2861 ulen = min_t(u32, info.xlated_prog_len, ulen);
7105e828
DB
2862 fault = copy_to_user(uinsns, insns_sanitized, ulen);
2863 kfree(insns_sanitized);
2864 if (fault)
1e270976
MKL
2865 return -EFAULT;
2866 }
2867
675fc275
JK
2868 if (bpf_prog_is_dev_bound(prog->aux)) {
2869 err = bpf_prog_offload_info_fill(&info, prog);
2870 if (err)
2871 return err;
fcfb126d
JW
2872 goto done;
2873 }
2874
2875 /* NOTE: the following code is supposed to be skipped for offload.
2876 * bpf_prog_offload_info_fill() is the place to fill similar fields
2877 * for offload.
2878 */
2879 ulen = info.jited_prog_len;
4d56a76e
SD
2880 if (prog->aux->func_cnt) {
2881 u32 i;
2882
2883 info.jited_prog_len = 0;
2884 for (i = 0; i < prog->aux->func_cnt; i++)
2885 info.jited_prog_len += prog->aux->func[i]->jited_len;
2886 } else {
2887 info.jited_prog_len = prog->jited_len;
2888 }
2889
fcfb126d
JW
2890 if (info.jited_prog_len && ulen) {
2891 if (bpf_dump_raw_ok()) {
2892 uinsns = u64_to_user_ptr(info.jited_prog_insns);
2893 ulen = min_t(u32, info.jited_prog_len, ulen);
4d56a76e
SD
2894
2895 /* for multi-function programs, copy the JITed
2896 * instructions for all the functions
2897 */
2898 if (prog->aux->func_cnt) {
2899 u32 len, free, i;
2900 u8 *img;
2901
2902 free = ulen;
2903 for (i = 0; i < prog->aux->func_cnt; i++) {
2904 len = prog->aux->func[i]->jited_len;
2905 len = min_t(u32, len, free);
2906 img = (u8 *) prog->aux->func[i]->bpf_func;
2907 if (copy_to_user(uinsns, img, len))
2908 return -EFAULT;
2909 uinsns += len;
2910 free -= len;
2911 if (!free)
2912 break;
2913 }
2914 } else {
2915 if (copy_to_user(uinsns, prog->bpf_func, ulen))
2916 return -EFAULT;
2917 }
fcfb126d
JW
2918 } else {
2919 info.jited_prog_insns = 0;
2920 }
675fc275
JK
2921 }
2922
dbecd738 2923 ulen = info.nr_jited_ksyms;
ff1889fc 2924 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
7a5725dd 2925 if (ulen) {
dbecd738 2926 if (bpf_dump_raw_ok()) {
ff1889fc 2927 unsigned long ksym_addr;
dbecd738 2928 u64 __user *user_ksyms;
dbecd738
SD
2929 u32 i;
2930
2931 /* copy the address of the kernel symbol
2932 * corresponding to each function
2933 */
2934 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
2935 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
ff1889fc
SL
2936 if (prog->aux->func_cnt) {
2937 for (i = 0; i < ulen; i++) {
2938 ksym_addr = (unsigned long)
2939 prog->aux->func[i]->bpf_func;
2940 if (put_user((u64) ksym_addr,
2941 &user_ksyms[i]))
2942 return -EFAULT;
2943 }
2944 } else {
2945 ksym_addr = (unsigned long) prog->bpf_func;
2946 if (put_user((u64) ksym_addr, &user_ksyms[0]))
dbecd738
SD
2947 return -EFAULT;
2948 }
2949 } else {
2950 info.jited_ksyms = 0;
2951 }
2952 }
2953
815581c1 2954 ulen = info.nr_jited_func_lens;
ff1889fc 2955 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
7a5725dd 2956 if (ulen) {
815581c1
SD
2957 if (bpf_dump_raw_ok()) {
2958 u32 __user *user_lens;
2959 u32 func_len, i;
2960
2961 /* copy the JITed image lengths for each function */
2962 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
2963 user_lens = u64_to_user_ptr(info.jited_func_lens);
ff1889fc
SL
2964 if (prog->aux->func_cnt) {
2965 for (i = 0; i < ulen; i++) {
2966 func_len =
2967 prog->aux->func[i]->jited_len;
2968 if (put_user(func_len, &user_lens[i]))
2969 return -EFAULT;
2970 }
2971 } else {
2972 func_len = prog->jited_len;
2973 if (put_user(func_len, &user_lens[0]))
815581c1
SD
2974 return -EFAULT;
2975 }
2976 } else {
2977 info.jited_func_lens = 0;
2978 }
2979 }
2980
7337224f 2981 if (prog->aux->btf)
838e9690
YS
2982 info.btf_id = btf_id(prog->aux->btf);
2983
11d8b82d
YS
2984 ulen = info.nr_func_info;
2985 info.nr_func_info = prog->aux->func_info_cnt;
2986 if (info.nr_func_info && ulen) {
9e794163 2987 char __user *user_finfo;
7337224f 2988
9e794163
MKL
2989 user_finfo = u64_to_user_ptr(info.func_info);
2990 ulen = min_t(u32, info.nr_func_info, ulen);
2991 if (copy_to_user(user_finfo, prog->aux->func_info,
2992 info.func_info_rec_size * ulen))
2993 return -EFAULT;
838e9690
YS
2994 }
2995
11d8b82d
YS
2996 ulen = info.nr_line_info;
2997 info.nr_line_info = prog->aux->nr_linfo;
2998 if (info.nr_line_info && ulen) {
9e794163 2999 __u8 __user *user_linfo;
c454a46b 3000
9e794163
MKL
3001 user_linfo = u64_to_user_ptr(info.line_info);
3002 ulen = min_t(u32, info.nr_line_info, ulen);
3003 if (copy_to_user(user_linfo, prog->aux->linfo,
3004 info.line_info_rec_size * ulen))
3005 return -EFAULT;
c454a46b
MKL
3006 }
3007
11d8b82d 3008 ulen = info.nr_jited_line_info;
c454a46b 3009 if (prog->aux->jited_linfo)
11d8b82d 3010 info.nr_jited_line_info = prog->aux->nr_linfo;
c454a46b 3011 else
11d8b82d
YS
3012 info.nr_jited_line_info = 0;
3013 if (info.nr_jited_line_info && ulen) {
c454a46b
MKL
3014 if (bpf_dump_raw_ok()) {
3015 __u64 __user *user_linfo;
3016 u32 i;
3017
3018 user_linfo = u64_to_user_ptr(info.jited_line_info);
11d8b82d 3019 ulen = min_t(u32, info.nr_jited_line_info, ulen);
c454a46b
MKL
3020 for (i = 0; i < ulen; i++) {
3021 if (put_user((__u64)(long)prog->aux->jited_linfo[i],
3022 &user_linfo[i]))
3023 return -EFAULT;
3024 }
3025 } else {
3026 info.jited_line_info = 0;
3027 }
3028 }
3029
c872bdb3
SL
3030 ulen = info.nr_prog_tags;
3031 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
3032 if (ulen) {
3033 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
3034 u32 i;
3035
3036 user_prog_tags = u64_to_user_ptr(info.prog_tags);
3037 ulen = min_t(u32, info.nr_prog_tags, ulen);
3038 if (prog->aux->func_cnt) {
3039 for (i = 0; i < ulen; i++) {
3040 if (copy_to_user(user_prog_tags[i],
3041 prog->aux->func[i]->tag,
3042 BPF_TAG_SIZE))
3043 return -EFAULT;
3044 }
3045 } else {
3046 if (copy_to_user(user_prog_tags[0],
3047 prog->tag, BPF_TAG_SIZE))
3048 return -EFAULT;
3049 }
3050 }
3051
1e270976
MKL
3052done:
3053 if (copy_to_user(uinfo, &info, info_len) ||
3054 put_user(info_len, &uattr->info.info_len))
3055 return -EFAULT;
3056
3057 return 0;
3058}
3059
3060static int bpf_map_get_info_by_fd(struct bpf_map *map,
3061 const union bpf_attr *attr,
3062 union bpf_attr __user *uattr)
3063{
3064 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3065 struct bpf_map_info info = {};
3066 u32 info_len = attr->info.info_len;
3067 int err;
3068
dcab51f1 3069 err = bpf_check_uarg_tail_zero(uinfo, sizeof(info), info_len);
1e270976
MKL
3070 if (err)
3071 return err;
3072 info_len = min_t(u32, sizeof(info), info_len);
3073
3074 info.type = map->map_type;
3075 info.id = map->id;
3076 info.key_size = map->key_size;
3077 info.value_size = map->value_size;
3078 info.max_entries = map->max_entries;
3079 info.map_flags = map->map_flags;
ad5b177b 3080 memcpy(info.name, map->name, sizeof(map->name));
1e270976 3081
78958fca
MKL
3082 if (map->btf) {
3083 info.btf_id = btf_id(map->btf);
9b2cf328
MKL
3084 info.btf_key_type_id = map->btf_key_type_id;
3085 info.btf_value_type_id = map->btf_value_type_id;
78958fca 3086 }
85d33df3 3087 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
78958fca 3088
52775b33
JK
3089 if (bpf_map_is_dev_bound(map)) {
3090 err = bpf_map_offload_info_fill(&info, map);
3091 if (err)
3092 return err;
3093 }
3094
1e270976
MKL
3095 if (copy_to_user(uinfo, &info, info_len) ||
3096 put_user(info_len, &uattr->info.info_len))
3097 return -EFAULT;
3098
3099 return 0;
3100}
3101
62dab84c
MKL
3102static int bpf_btf_get_info_by_fd(struct btf *btf,
3103 const union bpf_attr *attr,
3104 union bpf_attr __user *uattr)
3105{
3106 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
3107 u32 info_len = attr->info.info_len;
3108 int err;
3109
dcab51f1 3110 err = bpf_check_uarg_tail_zero(uinfo, sizeof(*uinfo), info_len);
62dab84c
MKL
3111 if (err)
3112 return err;
3113
3114 return btf_get_info_by_fd(btf, attr, uattr);
3115}
3116
1e270976
MKL
3117#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
3118
3119static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
3120 union bpf_attr __user *uattr)
3121{
3122 int ufd = attr->info.bpf_fd;
3123 struct fd f;
3124 int err;
3125
3126 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
3127 return -EINVAL;
3128
3129 f = fdget(ufd);
3130 if (!f.file)
3131 return -EBADFD;
3132
3133 if (f.file->f_op == &bpf_prog_fops)
3134 err = bpf_prog_get_info_by_fd(f.file->private_data, attr,
3135 uattr);
3136 else if (f.file->f_op == &bpf_map_fops)
3137 err = bpf_map_get_info_by_fd(f.file->private_data, attr,
3138 uattr);
60197cfb 3139 else if (f.file->f_op == &btf_fops)
62dab84c 3140 err = bpf_btf_get_info_by_fd(f.file->private_data, attr, uattr);
1e270976
MKL
3141 else
3142 err = -EINVAL;
3143
3144 fdput(f);
3145 return err;
3146}
3147
f56a653c
MKL
3148#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
3149
3150static int bpf_btf_load(const union bpf_attr *attr)
3151{
3152 if (CHECK_ATTR(BPF_BTF_LOAD))
3153 return -EINVAL;
3154
3155 if (!capable(CAP_SYS_ADMIN))
3156 return -EPERM;
3157
3158 return btf_new_fd(attr);
3159}
3160
78958fca
MKL
3161#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
3162
3163static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
3164{
3165 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
3166 return -EINVAL;
3167
3168 if (!capable(CAP_SYS_ADMIN))
3169 return -EPERM;
3170
3171 return btf_get_fd_by_id(attr->btf_id);
3172}
3173
41bdc4b4
YS
3174static int bpf_task_fd_query_copy(const union bpf_attr *attr,
3175 union bpf_attr __user *uattr,
3176 u32 prog_id, u32 fd_type,
3177 const char *buf, u64 probe_offset,
3178 u64 probe_addr)
3179{
3180 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
3181 u32 len = buf ? strlen(buf) : 0, input_len;
3182 int err = 0;
3183
3184 if (put_user(len, &uattr->task_fd_query.buf_len))
3185 return -EFAULT;
3186 input_len = attr->task_fd_query.buf_len;
3187 if (input_len && ubuf) {
3188 if (!len) {
3189 /* nothing to copy, just make ubuf NULL terminated */
3190 char zero = '\0';
3191
3192 if (put_user(zero, ubuf))
3193 return -EFAULT;
3194 } else if (input_len >= len + 1) {
3195 /* ubuf can hold the string with NULL terminator */
3196 if (copy_to_user(ubuf, buf, len + 1))
3197 return -EFAULT;
3198 } else {
3199 /* ubuf cannot hold the string with NULL terminator,
3200 * do a partial copy with NULL terminator.
3201 */
3202 char zero = '\0';
3203
3204 err = -ENOSPC;
3205 if (copy_to_user(ubuf, buf, input_len - 1))
3206 return -EFAULT;
3207 if (put_user(zero, ubuf + input_len - 1))
3208 return -EFAULT;
3209 }
3210 }
3211
3212 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
3213 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
3214 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
3215 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
3216 return -EFAULT;
3217
3218 return err;
3219}
3220
3221#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
3222
3223static int bpf_task_fd_query(const union bpf_attr *attr,
3224 union bpf_attr __user *uattr)
3225{
3226 pid_t pid = attr->task_fd_query.pid;
3227 u32 fd = attr->task_fd_query.fd;
3228 const struct perf_event *event;
3229 struct files_struct *files;
3230 struct task_struct *task;
3231 struct file *file;
3232 int err;
3233
3234 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
3235 return -EINVAL;
3236
3237 if (!capable(CAP_SYS_ADMIN))
3238 return -EPERM;
3239
3240 if (attr->task_fd_query.flags != 0)
3241 return -EINVAL;
3242
3243 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
3244 if (!task)
3245 return -ENOENT;
3246
3247 files = get_files_struct(task);
3248 put_task_struct(task);
3249 if (!files)
3250 return -ENOENT;
3251
3252 err = 0;
3253 spin_lock(&files->file_lock);
3254 file = fcheck_files(files, fd);
3255 if (!file)
3256 err = -EBADF;
3257 else
3258 get_file(file);
3259 spin_unlock(&files->file_lock);
3260 put_files_struct(files);
3261
3262 if (err)
3263 goto out;
3264
3265 if (file->f_op == &bpf_raw_tp_fops) {
3266 struct bpf_raw_tracepoint *raw_tp = file->private_data;
3267 struct bpf_raw_event_map *btp = raw_tp->btp;
3268
3269 err = bpf_task_fd_query_copy(attr, uattr,
3270 raw_tp->prog->aux->id,
3271 BPF_FD_TYPE_RAW_TRACEPOINT,
3272 btp->tp->name, 0, 0);
3273 goto put_file;
3274 }
3275
3276 event = perf_get_event(file);
3277 if (!IS_ERR(event)) {
3278 u64 probe_offset, probe_addr;
3279 u32 prog_id, fd_type;
3280 const char *buf;
3281
3282 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
3283 &buf, &probe_offset,
3284 &probe_addr);
3285 if (!err)
3286 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
3287 fd_type, buf,
3288 probe_offset,
3289 probe_addr);
3290 goto put_file;
3291 }
3292
3293 err = -ENOTSUPP;
3294put_file:
3295 fput(file);
3296out:
3297 return err;
3298}
3299
cb4d03ab
BV
3300#define BPF_MAP_BATCH_LAST_FIELD batch.flags
3301
3302#define BPF_DO_BATCH(fn) \
3303 do { \
3304 if (!fn) { \
3305 err = -ENOTSUPP; \
3306 goto err_put; \
3307 } \
3308 err = fn(map, attr, uattr); \
3309 } while (0)
3310
3311static int bpf_map_do_batch(const union bpf_attr *attr,
3312 union bpf_attr __user *uattr,
3313 int cmd)
3314{
3315 struct bpf_map *map;
3316 int err, ufd;
3317 struct fd f;
3318
3319 if (CHECK_ATTR(BPF_MAP_BATCH))
3320 return -EINVAL;
3321
3322 ufd = attr->batch.map_fd;
3323 f = fdget(ufd);
3324 map = __bpf_map_get(f);
3325 if (IS_ERR(map))
3326 return PTR_ERR(map);
3327
05799638
YS
3328 if ((cmd == BPF_MAP_LOOKUP_BATCH ||
3329 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH) &&
cb4d03ab
BV
3330 !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
3331 err = -EPERM;
3332 goto err_put;
3333 }
3334
3335 if (cmd != BPF_MAP_LOOKUP_BATCH &&
3336 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
3337 err = -EPERM;
3338 goto err_put;
3339 }
3340
3341 if (cmd == BPF_MAP_LOOKUP_BATCH)
3342 BPF_DO_BATCH(map->ops->map_lookup_batch);
05799638
YS
3343 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
3344 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
aa2e93b8
BV
3345 else if (cmd == BPF_MAP_UPDATE_BATCH)
3346 BPF_DO_BATCH(map->ops->map_update_batch);
3347 else
3348 BPF_DO_BATCH(map->ops->map_delete_batch);
cb4d03ab
BV
3349
3350err_put:
3351 fdput(f);
3352 return err;
3353}
3354
99c55f7d
AS
3355SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
3356{
3357 union bpf_attr attr = {};
3358 int err;
3359
0fa4fe85 3360 if (sysctl_unprivileged_bpf_disabled && !capable(CAP_SYS_ADMIN))
99c55f7d
AS
3361 return -EPERM;
3362
dcab51f1 3363 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
1e270976
MKL
3364 if (err)
3365 return err;
3366 size = min_t(u32, size, sizeof(attr));
99c55f7d
AS
3367
3368 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
3369 if (copy_from_user(&attr, uattr, size) != 0)
3370 return -EFAULT;
3371
afdb09c7
CF
3372 err = security_bpf(cmd, &attr, size);
3373 if (err < 0)
3374 return err;
3375
99c55f7d
AS
3376 switch (cmd) {
3377 case BPF_MAP_CREATE:
3378 err = map_create(&attr);
3379 break;
db20fd2b
AS
3380 case BPF_MAP_LOOKUP_ELEM:
3381 err = map_lookup_elem(&attr);
3382 break;
3383 case BPF_MAP_UPDATE_ELEM:
3384 err = map_update_elem(&attr);
3385 break;
3386 case BPF_MAP_DELETE_ELEM:
3387 err = map_delete_elem(&attr);
3388 break;
3389 case BPF_MAP_GET_NEXT_KEY:
3390 err = map_get_next_key(&attr);
3391 break;
87df15de
DB
3392 case BPF_MAP_FREEZE:
3393 err = map_freeze(&attr);
3394 break;
09756af4 3395 case BPF_PROG_LOAD:
838e9690 3396 err = bpf_prog_load(&attr, uattr);
09756af4 3397 break;
b2197755
DB
3398 case BPF_OBJ_PIN:
3399 err = bpf_obj_pin(&attr);
3400 break;
3401 case BPF_OBJ_GET:
3402 err = bpf_obj_get(&attr);
3403 break;
f4324551
DM
3404 case BPF_PROG_ATTACH:
3405 err = bpf_prog_attach(&attr);
3406 break;
3407 case BPF_PROG_DETACH:
3408 err = bpf_prog_detach(&attr);
3409 break;
468e2f64
AS
3410 case BPF_PROG_QUERY:
3411 err = bpf_prog_query(&attr, uattr);
3412 break;
1cf1cae9
AS
3413 case BPF_PROG_TEST_RUN:
3414 err = bpf_prog_test_run(&attr, uattr);
3415 break;
34ad5580
MKL
3416 case BPF_PROG_GET_NEXT_ID:
3417 err = bpf_obj_get_next_id(&attr, uattr,
3418 &prog_idr, &prog_idr_lock);
3419 break;
3420 case BPF_MAP_GET_NEXT_ID:
3421 err = bpf_obj_get_next_id(&attr, uattr,
3422 &map_idr, &map_idr_lock);
3423 break;
1b9ed84e
QM
3424 case BPF_BTF_GET_NEXT_ID:
3425 err = bpf_obj_get_next_id(&attr, uattr,
3426 &btf_idr, &btf_idr_lock);
3427 break;
b16d9aa4
MKL
3428 case BPF_PROG_GET_FD_BY_ID:
3429 err = bpf_prog_get_fd_by_id(&attr);
3430 break;
bd5f5f4e
MKL
3431 case BPF_MAP_GET_FD_BY_ID:
3432 err = bpf_map_get_fd_by_id(&attr);
3433 break;
1e270976
MKL
3434 case BPF_OBJ_GET_INFO_BY_FD:
3435 err = bpf_obj_get_info_by_fd(&attr, uattr);
3436 break;
c4f6699d
AS
3437 case BPF_RAW_TRACEPOINT_OPEN:
3438 err = bpf_raw_tracepoint_open(&attr);
3439 break;
f56a653c
MKL
3440 case BPF_BTF_LOAD:
3441 err = bpf_btf_load(&attr);
3442 break;
78958fca
MKL
3443 case BPF_BTF_GET_FD_BY_ID:
3444 err = bpf_btf_get_fd_by_id(&attr);
3445 break;
41bdc4b4
YS
3446 case BPF_TASK_FD_QUERY:
3447 err = bpf_task_fd_query(&attr, uattr);
3448 break;
bd513cd0
MV
3449 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
3450 err = map_lookup_and_delete_elem(&attr);
3451 break;
cb4d03ab
BV
3452 case BPF_MAP_LOOKUP_BATCH:
3453 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_LOOKUP_BATCH);
3454 break;
05799638
YS
3455 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
3456 err = bpf_map_do_batch(&attr, uattr,
3457 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
3458 break;
aa2e93b8
BV
3459 case BPF_MAP_UPDATE_BATCH:
3460 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_UPDATE_BATCH);
3461 break;
3462 case BPF_MAP_DELETE_BATCH:
3463 err = bpf_map_do_batch(&attr, uattr, BPF_MAP_DELETE_BATCH);
3464 break;
99c55f7d
AS
3465 default:
3466 err = -EINVAL;
3467 break;
3468 }
3469
3470 return err;
3471}