Merge branch 'bpf: Add kfuncs for PKCS#7 signature verification'
[linux-block.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>
aef2feda 5#include <linux/bpf-cgroup.h>
a67edbf4 6#include <linux/bpf_trace.h>
f4364dcf 7#include <linux/bpf_lirc.h>
4a1e7c0c 8#include <linux/bpf_verifier.h>
61df10c7 9#include <linux/bsearch.h>
f56a653c 10#include <linux/btf.h>
99c55f7d
AS
11#include <linux/syscalls.h>
12#include <linux/slab.h>
3f07c014 13#include <linux/sched/signal.h>
d407bd25
DB
14#include <linux/vmalloc.h>
15#include <linux/mmzone.h>
99c55f7d 16#include <linux/anon_inodes.h>
41bdc4b4 17#include <linux/fdtable.h>
db20fd2b 18#include <linux/file.h>
41bdc4b4 19#include <linux/fs.h>
09756af4
AS
20#include <linux/license.h>
21#include <linux/filter.h>
535e7b4b 22#include <linux/kernel.h>
dc4bb0e2 23#include <linux/idr.h>
cb4d2b3f
MKL
24#include <linux/cred.h>
25#include <linux/timekeeping.h>
26#include <linux/ctype.h>
9ef09e35 27#include <linux/nospec.h>
bae141f5 28#include <linux/audit.h>
ccfe29eb 29#include <uapi/linux/btf.h>
ca5999fd 30#include <linux/pgtable.h>
9e4e01df 31#include <linux/bpf_lsm.h>
457f4436 32#include <linux/poll.h>
4d7d7f69 33#include <linux/sort.h>
a3fd7cee 34#include <linux/bpf-netns.h>
1e6c62a8 35#include <linux/rcupdate_trace.h>
48edc1f7 36#include <linux/memcontrol.h>
0dcac272 37#include <linux/trace_events.h>
99c55f7d 38
da765a2f
DB
39#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
40 (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
41 (map)->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
42#define IS_FD_PROG_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PROG_ARRAY)
14dc6f04 43#define IS_FD_HASH(map) ((map)->map_type == BPF_MAP_TYPE_HASH_OF_MAPS)
da765a2f
DB
44#define IS_FD_MAP(map) (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map) || \
45 IS_FD_HASH(map))
14dc6f04 46
6e71b04a
CF
47#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
48
b121d1e7 49DEFINE_PER_CPU(int, bpf_prog_active);
dc4bb0e2
MKL
50static DEFINE_IDR(prog_idr);
51static DEFINE_SPINLOCK(prog_idr_lock);
f3f1c054
MKL
52static DEFINE_IDR(map_idr);
53static DEFINE_SPINLOCK(map_idr_lock);
a3b80e10
AN
54static DEFINE_IDR(link_idr);
55static DEFINE_SPINLOCK(link_idr_lock);
b121d1e7 56
08389d88
DB
57int sysctl_unprivileged_bpf_disabled __read_mostly =
58 IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;
1be7f75d 59
40077e0c 60static const struct bpf_map_ops * const bpf_map_types[] = {
91cc1a99 61#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
40077e0c
JB
62#define BPF_MAP_TYPE(_id, _ops) \
63 [_id] = &_ops,
f2e10bff 64#define BPF_LINK_TYPE(_id, _name)
40077e0c
JB
65#include <linux/bpf_types.h>
66#undef BPF_PROG_TYPE
67#undef BPF_MAP_TYPE
f2e10bff 68#undef BPF_LINK_TYPE
40077e0c 69};
99c55f7d 70
752ba56f
MS
71/*
72 * If we're handed a bigger struct than we know of, ensure all the unknown bits
73 * are 0 - i.e. new user-space does not rely on any kernel feature extensions
74 * we don't know about yet.
75 *
76 * There is a ToCToU between this function call and the following
77 * copy_from_user() call. However, this is not a concern since this function is
78 * meant to be a future-proofing of bits.
79 */
af2ac3e1 80int bpf_check_uarg_tail_zero(bpfptr_t uaddr,
dcab51f1
MKL
81 size_t expected_size,
82 size_t actual_size)
58291a74 83{
b7e4b65f 84 int res;
58291a74 85
752ba56f
MS
86 if (unlikely(actual_size > PAGE_SIZE)) /* silly large */
87 return -E2BIG;
88
58291a74
MS
89 if (actual_size <= expected_size)
90 return 0;
91
af2ac3e1
AS
92 if (uaddr.is_kernel)
93 res = memchr_inv(uaddr.kernel + expected_size, 0,
94 actual_size - expected_size) == NULL;
95 else
96 res = check_zeroed_user(uaddr.user + expected_size,
97 actual_size - expected_size);
b7e4b65f
AV
98 if (res < 0)
99 return res;
100 return res ? 0 : -E2BIG;
58291a74
MS
101}
102
a3884572 103const struct bpf_map_ops bpf_map_offload_ops = {
f4d05259 104 .map_meta_equal = bpf_map_meta_equal,
a3884572
JK
105 .map_alloc = bpf_map_offload_map_alloc,
106 .map_free = bpf_map_offload_map_free,
e8d2bec0 107 .map_check_btf = map_check_no_btf,
a3884572
JK
108};
109
99c55f7d
AS
110static struct bpf_map *find_and_alloc_map(union bpf_attr *attr)
111{
1110f3a9 112 const struct bpf_map_ops *ops;
9ef09e35 113 u32 type = attr->map_type;
99c55f7d 114 struct bpf_map *map;
1110f3a9 115 int err;
99c55f7d 116
9ef09e35 117 if (type >= ARRAY_SIZE(bpf_map_types))
1110f3a9 118 return ERR_PTR(-EINVAL);
9ef09e35
MR
119 type = array_index_nospec(type, ARRAY_SIZE(bpf_map_types));
120 ops = bpf_map_types[type];
1110f3a9 121 if (!ops)
40077e0c 122 return ERR_PTR(-EINVAL);
99c55f7d 123
1110f3a9
JK
124 if (ops->map_alloc_check) {
125 err = ops->map_alloc_check(attr);
126 if (err)
127 return ERR_PTR(err);
128 }
a3884572
JK
129 if (attr->map_ifindex)
130 ops = &bpf_map_offload_ops;
1110f3a9 131 map = ops->map_alloc(attr);
40077e0c
JB
132 if (IS_ERR(map))
133 return map;
1110f3a9 134 map->ops = ops;
9ef09e35 135 map->map_type = type;
40077e0c 136 return map;
99c55f7d
AS
137}
138
353050be
DB
139static void bpf_map_write_active_inc(struct bpf_map *map)
140{
141 atomic64_inc(&map->writecnt);
142}
143
144static void bpf_map_write_active_dec(struct bpf_map *map)
145{
146 atomic64_dec(&map->writecnt);
147}
148
149bool bpf_map_write_active(const struct bpf_map *map)
150{
151 return atomic64_read(&map->writecnt) != 0;
152}
153
80ee81e0 154static u32 bpf_map_value_size(const struct bpf_map *map)
15c14a3d
BV
155{
156 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
157 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH ||
158 map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY ||
159 map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE)
160 return round_up(map->value_size, 8) * num_possible_cpus();
161 else if (IS_FD_MAP(map))
162 return sizeof(u32);
163 else
164 return map->value_size;
165}
166
167static void maybe_wait_bpf_programs(struct bpf_map *map)
168{
169 /* Wait for any running BPF programs to complete so that
170 * userspace, when we return to it, knows that all programs
171 * that could be running use the new map value.
172 */
173 if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS ||
174 map->map_type == BPF_MAP_TYPE_ARRAY_OF_MAPS)
175 synchronize_rcu();
176}
177
178static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key,
179 void *value, __u64 flags)
180{
181 int err;
182
183 /* Need to create a kthread, thus must support schedule */
184 if (bpf_map_is_dev_bound(map)) {
185 return bpf_map_offload_update_elem(map, key, value, flags);
186 } else if (map->map_type == BPF_MAP_TYPE_CPUMAP ||
15c14a3d
BV
187 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
188 return map->ops->map_update_elem(map, key, value, flags);
13b79d3f
LB
189 } else if (map->map_type == BPF_MAP_TYPE_SOCKHASH ||
190 map->map_type == BPF_MAP_TYPE_SOCKMAP) {
191 return sock_map_update_elem_sys(map, key, value, flags);
15c14a3d
BV
192 } else if (IS_FD_PROG_ARRAY(map)) {
193 return bpf_fd_array_map_update_elem(map, f.file, key, value,
194 flags);
195 }
196
b6e5dae1 197 bpf_disable_instrumentation();
15c14a3d
BV
198 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
199 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
200 err = bpf_percpu_hash_update(map, key, value, flags);
201 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
202 err = bpf_percpu_array_update(map, key, value, flags);
203 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
204 err = bpf_percpu_cgroup_storage_update(map, key, value,
205 flags);
206 } else if (IS_FD_ARRAY(map)) {
207 rcu_read_lock();
208 err = bpf_fd_array_map_update_elem(map, f.file, key, value,
209 flags);
210 rcu_read_unlock();
211 } else if (map->map_type == BPF_MAP_TYPE_HASH_OF_MAPS) {
212 rcu_read_lock();
213 err = bpf_fd_htab_map_update_elem(map, f.file, key, value,
214 flags);
215 rcu_read_unlock();
216 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
217 /* rcu_read_lock() is not needed */
218 err = bpf_fd_reuseport_array_update_elem(map, key, value,
219 flags);
220 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
9330986c
JK
221 map->map_type == BPF_MAP_TYPE_STACK ||
222 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
15c14a3d
BV
223 err = map->ops->map_push_elem(map, value, flags);
224 } else {
225 rcu_read_lock();
226 err = map->ops->map_update_elem(map, key, value, flags);
227 rcu_read_unlock();
228 }
b6e5dae1 229 bpf_enable_instrumentation();
15c14a3d
BV
230 maybe_wait_bpf_programs(map);
231
232 return err;
233}
234
235static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value,
236 __u64 flags)
237{
238 void *ptr;
239 int err;
240
cb4d03ab
BV
241 if (bpf_map_is_dev_bound(map))
242 return bpf_map_offload_lookup_elem(map, key, value);
15c14a3d 243
b6e5dae1 244 bpf_disable_instrumentation();
15c14a3d
BV
245 if (map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
246 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
247 err = bpf_percpu_hash_copy(map, key, value);
248 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_ARRAY) {
249 err = bpf_percpu_array_copy(map, key, value);
250 } else if (map->map_type == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE) {
251 err = bpf_percpu_cgroup_storage_copy(map, key, value);
252 } else if (map->map_type == BPF_MAP_TYPE_STACK_TRACE) {
253 err = bpf_stackmap_copy(map, key, value);
254 } else if (IS_FD_ARRAY(map) || IS_FD_PROG_ARRAY(map)) {
255 err = bpf_fd_array_map_lookup_elem(map, key, value);
256 } else if (IS_FD_HASH(map)) {
257 err = bpf_fd_htab_map_lookup_elem(map, key, value);
258 } else if (map->map_type == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY) {
259 err = bpf_fd_reuseport_array_lookup_elem(map, key, value);
260 } else if (map->map_type == BPF_MAP_TYPE_QUEUE ||
9330986c
JK
261 map->map_type == BPF_MAP_TYPE_STACK ||
262 map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
15c14a3d
BV
263 err = map->ops->map_peek_elem(map, value);
264 } else if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
265 /* struct_ops map requires directly updating "value" */
266 err = bpf_struct_ops_map_sys_lookup_elem(map, key, value);
267 } else {
268 rcu_read_lock();
269 if (map->ops->map_lookup_elem_sys_only)
270 ptr = map->ops->map_lookup_elem_sys_only(map, key);
271 else
272 ptr = map->ops->map_lookup_elem(map, key);
273 if (IS_ERR(ptr)) {
274 err = PTR_ERR(ptr);
275 } else if (!ptr) {
276 err = -ENOENT;
277 } else {
278 err = 0;
279 if (flags & BPF_F_LOCK)
280 /* lock 'ptr' and copy everything but lock */
281 copy_map_value_locked(map, value, ptr, true);
282 else
283 copy_map_value(map, value, ptr);
68134668
AS
284 /* mask lock and timer, since value wasn't zero inited */
285 check_and_init_map_value(map, value);
15c14a3d
BV
286 }
287 rcu_read_unlock();
288 }
289
b6e5dae1 290 bpf_enable_instrumentation();
15c14a3d
BV
291 maybe_wait_bpf_programs(map);
292
293 return err;
294}
295
d5299b67
RG
296/* Please, do not use this function outside from the map creation path
297 * (e.g. in map update path) without taking care of setting the active
298 * memory cgroup (see at bpf_map_kmalloc_node() for example).
299 */
196e8ca7 300static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
d407bd25 301{
f01a7dbe
MP
302 /* We really just want to fail instead of triggering OOM killer
303 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
304 * which is used for lower order allocation requests.
305 *
306 * It has been observed that higher order allocation requests done by
307 * vmalloc with __GFP_NORETRY being set might fail due to not trying
308 * to reclaim memory from the page cache, thus we set
309 * __GFP_RETRY_MAYFAIL to avoid such situations.
d407bd25 310 */
f01a7dbe 311
d5299b67 312 const gfp_t gfp = __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT;
041de93f
CH
313 unsigned int flags = 0;
314 unsigned long align = 1;
d407bd25
DB
315 void *area;
316
196e8ca7
DB
317 if (size >= SIZE_MAX)
318 return NULL;
319
fc970227 320 /* kmalloc()'ed memory can't be mmap()'ed */
041de93f
CH
321 if (mmapable) {
322 BUG_ON(!PAGE_ALIGNED(size));
323 align = SHMLBA;
324 flags = VM_USERMAP;
325 } else if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
326 area = kmalloc_node(size, gfp | GFP_USER | __GFP_NORETRY,
f01a7dbe 327 numa_node);
d407bd25
DB
328 if (area != NULL)
329 return area;
330 }
041de93f
CH
331
332 return __vmalloc_node_range(size, align, VMALLOC_START, VMALLOC_END,
333 gfp | GFP_KERNEL | __GFP_RETRY_MAYFAIL, PAGE_KERNEL,
334 flags, numa_node, __builtin_return_address(0));
d407bd25
DB
335}
336
196e8ca7 337void *bpf_map_area_alloc(u64 size, int numa_node)
fc970227
AN
338{
339 return __bpf_map_area_alloc(size, numa_node, false);
340}
341
196e8ca7 342void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
fc970227
AN
343{
344 return __bpf_map_area_alloc(size, numa_node, true);
345}
346
d407bd25
DB
347void bpf_map_area_free(void *area)
348{
349 kvfree(area);
350}
351
be70bcd5
DB
352static u32 bpf_map_flags_retain_permanent(u32 flags)
353{
354 /* Some map creation flags are not tied to the map object but
355 * rather to the map fd instead, so they have no meaning upon
356 * map object inspection since multiple file descriptors with
357 * different (access) properties can exist here. Thus, given
358 * this has zero meaning for the map itself, lets clear these
359 * from here.
360 */
361 return flags & ~(BPF_F_RDONLY | BPF_F_WRONLY);
362}
363
bd475643
JK
364void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr)
365{
366 map->map_type = attr->map_type;
367 map->key_size = attr->key_size;
368 map->value_size = attr->value_size;
369 map->max_entries = attr->max_entries;
be70bcd5 370 map->map_flags = bpf_map_flags_retain_permanent(attr->map_flags);
bd475643 371 map->numa_node = bpf_map_attr_numa_node(attr);
9330986c 372 map->map_extra = attr->map_extra;
bd475643
JK
373}
374
f3f1c054
MKL
375static int bpf_map_alloc_id(struct bpf_map *map)
376{
377 int id;
378
b76354cd 379 idr_preload(GFP_KERNEL);
f3f1c054
MKL
380 spin_lock_bh(&map_idr_lock);
381 id = idr_alloc_cyclic(&map_idr, map, 1, INT_MAX, GFP_ATOMIC);
382 if (id > 0)
383 map->id = id;
384 spin_unlock_bh(&map_idr_lock);
b76354cd 385 idr_preload_end();
f3f1c054
MKL
386
387 if (WARN_ON_ONCE(!id))
388 return -ENOSPC;
389
390 return id > 0 ? 0 : id;
391}
392
a3884572 393void bpf_map_free_id(struct bpf_map *map, bool do_idr_lock)
f3f1c054 394{
930651a7
ED
395 unsigned long flags;
396
a3884572
JK
397 /* Offloaded maps are removed from the IDR store when their device
398 * disappears - even if someone holds an fd to them they are unusable,
399 * the memory is gone, all ops will fail; they are simply waiting for
400 * refcnt to drop to be freed.
401 */
402 if (!map->id)
403 return;
404
bd5f5f4e 405 if (do_idr_lock)
930651a7 406 spin_lock_irqsave(&map_idr_lock, flags);
bd5f5f4e
MKL
407 else
408 __acquire(&map_idr_lock);
409
f3f1c054 410 idr_remove(&map_idr, map->id);
a3884572 411 map->id = 0;
bd5f5f4e
MKL
412
413 if (do_idr_lock)
930651a7 414 spin_unlock_irqrestore(&map_idr_lock, flags);
bd5f5f4e
MKL
415 else
416 __release(&map_idr_lock);
f3f1c054
MKL
417}
418
48edc1f7
RG
419#ifdef CONFIG_MEMCG_KMEM
420static void bpf_map_save_memcg(struct bpf_map *map)
421{
4201d9ab
RG
422 /* Currently if a map is created by a process belonging to the root
423 * memory cgroup, get_obj_cgroup_from_current() will return NULL.
424 * So we have to check map->objcg for being NULL each time it's
425 * being used.
426 */
427 map->objcg = get_obj_cgroup_from_current();
48edc1f7
RG
428}
429
430static void bpf_map_release_memcg(struct bpf_map *map)
431{
4201d9ab
RG
432 if (map->objcg)
433 obj_cgroup_put(map->objcg);
434}
435
436static struct mem_cgroup *bpf_map_get_memcg(const struct bpf_map *map)
437{
438 if (map->objcg)
439 return get_mem_cgroup_from_objcg(map->objcg);
440
441 return root_mem_cgroup;
48edc1f7
RG
442}
443
444void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
445 int node)
446{
4201d9ab 447 struct mem_cgroup *memcg, *old_memcg;
48edc1f7
RG
448 void *ptr;
449
4201d9ab
RG
450 memcg = bpf_map_get_memcg(map);
451 old_memcg = set_active_memcg(memcg);
48edc1f7
RG
452 ptr = kmalloc_node(size, flags | __GFP_ACCOUNT, node);
453 set_active_memcg(old_memcg);
4201d9ab 454 mem_cgroup_put(memcg);
48edc1f7
RG
455
456 return ptr;
457}
458
459void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags)
460{
4201d9ab 461 struct mem_cgroup *memcg, *old_memcg;
48edc1f7
RG
462 void *ptr;
463
4201d9ab
RG
464 memcg = bpf_map_get_memcg(map);
465 old_memcg = set_active_memcg(memcg);
48edc1f7
RG
466 ptr = kzalloc(size, flags | __GFP_ACCOUNT);
467 set_active_memcg(old_memcg);
4201d9ab 468 mem_cgroup_put(memcg);
48edc1f7
RG
469
470 return ptr;
471}
472
473void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
474 size_t align, gfp_t flags)
475{
4201d9ab 476 struct mem_cgroup *memcg, *old_memcg;
48edc1f7
RG
477 void __percpu *ptr;
478
4201d9ab
RG
479 memcg = bpf_map_get_memcg(map);
480 old_memcg = set_active_memcg(memcg);
48edc1f7
RG
481 ptr = __alloc_percpu_gfp(size, align, flags | __GFP_ACCOUNT);
482 set_active_memcg(old_memcg);
4201d9ab 483 mem_cgroup_put(memcg);
48edc1f7
RG
484
485 return ptr;
486}
487
488#else
489static void bpf_map_save_memcg(struct bpf_map *map)
490{
491}
492
493static void bpf_map_release_memcg(struct bpf_map *map)
494{
495}
496#endif
497
61df10c7
KKD
498static int bpf_map_kptr_off_cmp(const void *a, const void *b)
499{
500 const struct bpf_map_value_off_desc *off_desc1 = a, *off_desc2 = b;
501
502 if (off_desc1->offset < off_desc2->offset)
503 return -1;
504 else if (off_desc1->offset > off_desc2->offset)
505 return 1;
506 return 0;
507}
508
509struct bpf_map_value_off_desc *bpf_map_kptr_off_contains(struct bpf_map *map, u32 offset)
510{
511 /* Since members are iterated in btf_find_field in increasing order,
512 * offsets appended to kptr_off_tab are in increasing order, so we can
513 * do bsearch to find exact match.
514 */
515 struct bpf_map_value_off *tab;
516
517 if (!map_value_has_kptrs(map))
518 return NULL;
519 tab = map->kptr_off_tab;
520 return bsearch(&offset, tab->off, tab->nr_off, sizeof(tab->off[0]), bpf_map_kptr_off_cmp);
521}
522
523void bpf_map_free_kptr_off_tab(struct bpf_map *map)
524{
525 struct bpf_map_value_off *tab = map->kptr_off_tab;
526 int i;
527
528 if (!map_value_has_kptrs(map))
529 return;
14a324f6
KKD
530 for (i = 0; i < tab->nr_off; i++) {
531 if (tab->off[i].kptr.module)
532 module_put(tab->off[i].kptr.module);
61df10c7 533 btf_put(tab->off[i].kptr.btf);
14a324f6 534 }
61df10c7
KKD
535 kfree(tab);
536 map->kptr_off_tab = NULL;
537}
538
539struct bpf_map_value_off *bpf_map_copy_kptr_off_tab(const struct bpf_map *map)
540{
541 struct bpf_map_value_off *tab = map->kptr_off_tab, *new_tab;
542 int size, i;
543
544 if (!map_value_has_kptrs(map))
545 return ERR_PTR(-ENOENT);
546 size = offsetof(struct bpf_map_value_off, off[tab->nr_off]);
547 new_tab = kmemdup(tab, size, GFP_KERNEL | __GFP_NOWARN);
548 if (!new_tab)
549 return ERR_PTR(-ENOMEM);
550 /* Do a deep copy of the kptr_off_tab */
14a324f6 551 for (i = 0; i < tab->nr_off; i++) {
61df10c7 552 btf_get(tab->off[i].kptr.btf);
14a324f6
KKD
553 if (tab->off[i].kptr.module && !try_module_get(tab->off[i].kptr.module)) {
554 while (i--) {
555 if (tab->off[i].kptr.module)
556 module_put(tab->off[i].kptr.module);
557 btf_put(tab->off[i].kptr.btf);
558 }
559 kfree(new_tab);
560 return ERR_PTR(-ENXIO);
561 }
562 }
61df10c7
KKD
563 return new_tab;
564}
565
566bool bpf_map_equal_kptr_off_tab(const struct bpf_map *map_a, const struct bpf_map *map_b)
567{
568 struct bpf_map_value_off *tab_a = map_a->kptr_off_tab, *tab_b = map_b->kptr_off_tab;
569 bool a_has_kptr = map_value_has_kptrs(map_a), b_has_kptr = map_value_has_kptrs(map_b);
570 int size;
571
572 if (!a_has_kptr && !b_has_kptr)
573 return true;
574 if (a_has_kptr != b_has_kptr)
575 return false;
576 if (tab_a->nr_off != tab_b->nr_off)
577 return false;
578 size = offsetof(struct bpf_map_value_off, off[tab_a->nr_off]);
579 return !memcmp(tab_a, tab_b, size);
580}
581
14a324f6
KKD
582/* Caller must ensure map_value_has_kptrs is true. Note that this function can
583 * be called on a map value while the map_value is visible to BPF programs, as
584 * it ensures the correct synchronization, and we already enforce the same using
585 * the bpf_kptr_xchg helper on the BPF program side for referenced kptrs.
586 */
587void bpf_map_free_kptrs(struct bpf_map *map, void *map_value)
588{
589 struct bpf_map_value_off *tab = map->kptr_off_tab;
590 unsigned long *btf_id_ptr;
591 int i;
592
593 for (i = 0; i < tab->nr_off; i++) {
594 struct bpf_map_value_off_desc *off_desc = &tab->off[i];
595 unsigned long old_ptr;
596
597 btf_id_ptr = map_value + off_desc->offset;
598 if (off_desc->type == BPF_KPTR_UNREF) {
599 u64 *p = (u64 *)btf_id_ptr;
600
9fad7fe5 601 WRITE_ONCE(*p, 0);
14a324f6
KKD
602 continue;
603 }
604 old_ptr = xchg(btf_id_ptr, 0);
605 off_desc->kptr.dtor((void *)old_ptr);
606 }
607}
608
99c55f7d
AS
609/* called from workqueue */
610static void bpf_map_free_deferred(struct work_struct *work)
611{
612 struct bpf_map *map = container_of(work, struct bpf_map, work);
613
afdb09c7 614 security_bpf_map_free(map);
4d7d7f69 615 kfree(map->off_arr);
48edc1f7 616 bpf_map_release_memcg(map);
14a324f6
KKD
617 /* implementation dependent freeing, map_free callback also does
618 * bpf_map_free_kptr_off_tab, if needed.
619 */
99c55f7d
AS
620 map->ops->map_free(map);
621}
622
c9da161c
DB
623static void bpf_map_put_uref(struct bpf_map *map)
624{
1e0bd5a0 625 if (atomic64_dec_and_test(&map->usercnt)) {
ba6b8de4
JF
626 if (map->ops->map_release_uref)
627 map->ops->map_release_uref(map);
c9da161c
DB
628 }
629}
630
99c55f7d
AS
631/* decrement map refcnt and schedule it for freeing via workqueue
632 * (unrelying map implementation ops->map_free() might sleep)
633 */
bd5f5f4e 634static void __bpf_map_put(struct bpf_map *map, bool do_idr_lock)
99c55f7d 635{
1e0bd5a0 636 if (atomic64_dec_and_test(&map->refcnt)) {
34ad5580 637 /* bpf_map_free_id() must be called first */
bd5f5f4e 638 bpf_map_free_id(map, do_idr_lock);
78958fca 639 btf_put(map->btf);
99c55f7d 640 INIT_WORK(&map->work, bpf_map_free_deferred);
8d5a8011
AS
641 /* Avoid spawning kworkers, since they all might contend
642 * for the same mutex like slab_mutex.
643 */
644 queue_work(system_unbound_wq, &map->work);
99c55f7d
AS
645 }
646}
647
bd5f5f4e
MKL
648void bpf_map_put(struct bpf_map *map)
649{
650 __bpf_map_put(map, true);
651}
630a4d38 652EXPORT_SYMBOL_GPL(bpf_map_put);
bd5f5f4e 653
c9da161c 654void bpf_map_put_with_uref(struct bpf_map *map)
99c55f7d 655{
c9da161c 656 bpf_map_put_uref(map);
99c55f7d 657 bpf_map_put(map);
c9da161c
DB
658}
659
660static int bpf_map_release(struct inode *inode, struct file *filp)
661{
61d1b6a4
DB
662 struct bpf_map *map = filp->private_data;
663
664 if (map->ops->map_release)
665 map->ops->map_release(map, filp);
666
667 bpf_map_put_with_uref(map);
99c55f7d
AS
668 return 0;
669}
670
87df15de
DB
671static fmode_t map_get_sys_perms(struct bpf_map *map, struct fd f)
672{
673 fmode_t mode = f.file->f_mode;
674
675 /* Our file permissions may have been overridden by global
676 * map permissions facing syscall side.
677 */
678 if (READ_ONCE(map->frozen))
679 mode &= ~FMODE_CAN_WRITE;
680 return mode;
681}
682
f99bf205 683#ifdef CONFIG_PROC_FS
80ee81e0
RG
684/* Provides an approximation of the map's memory footprint.
685 * Used only to provide a backward compatibility and display
686 * a reasonable "memlock" info.
687 */
688static unsigned long bpf_map_memory_footprint(const struct bpf_map *map)
689{
690 unsigned long size;
691
692 size = round_up(map->key_size + bpf_map_value_size(map), 8);
693
694 return round_up(map->max_entries * size, PAGE_SIZE);
695}
696
f99bf205
DB
697static void bpf_map_show_fdinfo(struct seq_file *m, struct file *filp)
698{
f45d5b6c 699 struct bpf_map *map = filp->private_data;
2beee5f5 700 u32 type = 0, jited = 0;
21116b70 701
f45d5b6c
THJ
702 if (map_type_contains_progs(map)) {
703 spin_lock(&map->owner.lock);
704 type = map->owner.type;
705 jited = map->owner.jited;
706 spin_unlock(&map->owner.lock);
21116b70 707 }
f99bf205
DB
708
709 seq_printf(m,
710 "map_type:\t%u\n"
711 "key_size:\t%u\n"
712 "value_size:\t%u\n"
322cea2f 713 "max_entries:\t%u\n"
21116b70 714 "map_flags:\t%#x\n"
9330986c 715 "map_extra:\t%#llx\n"
80ee81e0 716 "memlock:\t%lu\n"
87df15de
DB
717 "map_id:\t%u\n"
718 "frozen:\t%u\n",
f99bf205
DB
719 map->map_type,
720 map->key_size,
721 map->value_size,
322cea2f 722 map->max_entries,
21116b70 723 map->map_flags,
9330986c 724 (unsigned long long)map->map_extra,
80ee81e0 725 bpf_map_memory_footprint(map),
87df15de
DB
726 map->id,
727 READ_ONCE(map->frozen));
2beee5f5
DB
728 if (type) {
729 seq_printf(m, "owner_prog_type:\t%u\n", type);
730 seq_printf(m, "owner_jited:\t%u\n", jited);
9780c0ab 731 }
f99bf205
DB
732}
733#endif
734
6e71b04a
CF
735static ssize_t bpf_dummy_read(struct file *filp, char __user *buf, size_t siz,
736 loff_t *ppos)
737{
738 /* We need this handler such that alloc_file() enables
739 * f_mode with FMODE_CAN_READ.
740 */
741 return -EINVAL;
742}
743
744static ssize_t bpf_dummy_write(struct file *filp, const char __user *buf,
745 size_t siz, loff_t *ppos)
746{
747 /* We need this handler such that alloc_file() enables
748 * f_mode with FMODE_CAN_WRITE.
749 */
750 return -EINVAL;
751}
752
fc970227
AN
753/* called for any extra memory-mapped regions (except initial) */
754static void bpf_map_mmap_open(struct vm_area_struct *vma)
755{
756 struct bpf_map *map = vma->vm_file->private_data;
757
353050be
DB
758 if (vma->vm_flags & VM_MAYWRITE)
759 bpf_map_write_active_inc(map);
fc970227
AN
760}
761
762/* called for all unmapped memory region (including initial) */
763static void bpf_map_mmap_close(struct vm_area_struct *vma)
764{
765 struct bpf_map *map = vma->vm_file->private_data;
766
353050be
DB
767 if (vma->vm_flags & VM_MAYWRITE)
768 bpf_map_write_active_dec(map);
fc970227
AN
769}
770
771static const struct vm_operations_struct bpf_map_default_vmops = {
772 .open = bpf_map_mmap_open,
773 .close = bpf_map_mmap_close,
774};
775
776static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
777{
778 struct bpf_map *map = filp->private_data;
779 int err;
780
68134668 781 if (!map->ops->map_mmap || map_value_has_spin_lock(map) ||
61df10c7 782 map_value_has_timer(map) || map_value_has_kptrs(map))
fc970227
AN
783 return -ENOTSUPP;
784
785 if (!(vma->vm_flags & VM_SHARED))
786 return -EINVAL;
787
788 mutex_lock(&map->freeze_mutex);
789
dfeb376d
AN
790 if (vma->vm_flags & VM_WRITE) {
791 if (map->frozen) {
792 err = -EPERM;
793 goto out;
794 }
795 /* map is meant to be read-only, so do not allow mapping as
796 * writable, because it's possible to leak a writable page
797 * reference and allows user-space to still modify it after
798 * freezing, while verifier will assume contents do not change
799 */
800 if (map->map_flags & BPF_F_RDONLY_PROG) {
801 err = -EACCES;
802 goto out;
803 }
fc970227
AN
804 }
805
806 /* set default open/close callbacks */
807 vma->vm_ops = &bpf_map_default_vmops;
808 vma->vm_private_data = map;
1f6cb19b
AN
809 vma->vm_flags &= ~VM_MAYEXEC;
810 if (!(vma->vm_flags & VM_WRITE))
811 /* disallow re-mapping with PROT_WRITE */
812 vma->vm_flags &= ~VM_MAYWRITE;
fc970227
AN
813
814 err = map->ops->map_mmap(map, vma);
815 if (err)
816 goto out;
817
1f6cb19b 818 if (vma->vm_flags & VM_MAYWRITE)
353050be 819 bpf_map_write_active_inc(map);
fc970227
AN
820out:
821 mutex_unlock(&map->freeze_mutex);
822 return err;
823}
824
457f4436
AN
825static __poll_t bpf_map_poll(struct file *filp, struct poll_table_struct *pts)
826{
827 struct bpf_map *map = filp->private_data;
828
829 if (map->ops->map_poll)
830 return map->ops->map_poll(map, filp, pts);
831
832 return EPOLLERR;
833}
834
f66e448c 835const struct file_operations bpf_map_fops = {
f99bf205
DB
836#ifdef CONFIG_PROC_FS
837 .show_fdinfo = bpf_map_show_fdinfo,
838#endif
839 .release = bpf_map_release,
6e71b04a
CF
840 .read = bpf_dummy_read,
841 .write = bpf_dummy_write,
fc970227 842 .mmap = bpf_map_mmap,
457f4436 843 .poll = bpf_map_poll,
99c55f7d
AS
844};
845
6e71b04a 846int bpf_map_new_fd(struct bpf_map *map, int flags)
aa79781b 847{
afdb09c7
CF
848 int ret;
849
850 ret = security_bpf_map(map, OPEN_FMODE(flags));
851 if (ret < 0)
852 return ret;
853
aa79781b 854 return anon_inode_getfd("bpf-map", &bpf_map_fops, map,
6e71b04a
CF
855 flags | O_CLOEXEC);
856}
857
858int bpf_get_file_flag(int flags)
859{
860 if ((flags & BPF_F_RDONLY) && (flags & BPF_F_WRONLY))
861 return -EINVAL;
862 if (flags & BPF_F_RDONLY)
863 return O_RDONLY;
864 if (flags & BPF_F_WRONLY)
865 return O_WRONLY;
866 return O_RDWR;
aa79781b
DB
867}
868
99c55f7d
AS
869/* helper macro to check that unused fields 'union bpf_attr' are zero */
870#define CHECK_ATTR(CMD) \
871 memchr_inv((void *) &attr->CMD##_LAST_FIELD + \
872 sizeof(attr->CMD##_LAST_FIELD), 0, \
873 sizeof(*attr) - \
874 offsetof(union bpf_attr, CMD##_LAST_FIELD) - \
875 sizeof(attr->CMD##_LAST_FIELD)) != NULL
876
8e7ae251
MKL
877/* dst and src must have at least "size" number of bytes.
878 * Return strlen on success and < 0 on error.
cb4d2b3f 879 */
8e7ae251 880int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size)
cb4d2b3f 881{
8e7ae251
MKL
882 const char *end = src + size;
883 const char *orig_src = src;
cb4d2b3f 884
8e7ae251 885 memset(dst, 0, size);
3e0ddc4f 886 /* Copy all isalnum(), '_' and '.' chars. */
cb4d2b3f 887 while (src < end && *src) {
3e0ddc4f
DB
888 if (!isalnum(*src) &&
889 *src != '_' && *src != '.')
cb4d2b3f
MKL
890 return -EINVAL;
891 *dst++ = *src++;
892 }
893
8e7ae251 894 /* No '\0' found in "size" number of bytes */
cb4d2b3f
MKL
895 if (src == end)
896 return -EINVAL;
897
8e7ae251 898 return src - orig_src;
cb4d2b3f
MKL
899}
900
e8d2bec0 901int map_check_no_btf(const struct bpf_map *map,
1b2b234b 902 const struct btf *btf,
e8d2bec0
DB
903 const struct btf_type *key_type,
904 const struct btf_type *value_type)
905{
906 return -ENOTSUPP;
907}
908
4d7d7f69
KKD
909static int map_off_arr_cmp(const void *_a, const void *_b, const void *priv)
910{
911 const u32 a = *(const u32 *)_a;
912 const u32 b = *(const u32 *)_b;
913
914 if (a < b)
915 return -1;
916 else if (a > b)
917 return 1;
918 return 0;
919}
920
921static void map_off_arr_swap(void *_a, void *_b, int size, const void *priv)
922{
923 struct bpf_map *map = (struct bpf_map *)priv;
924 u32 *off_base = map->off_arr->field_off;
925 u32 *a = _a, *b = _b;
926 u8 *sz_a, *sz_b;
927
928 sz_a = map->off_arr->field_sz + (a - off_base);
929 sz_b = map->off_arr->field_sz + (b - off_base);
930
931 swap(*a, *b);
932 swap(*sz_a, *sz_b);
933}
934
935static int bpf_map_alloc_off_arr(struct bpf_map *map)
936{
937 bool has_spin_lock = map_value_has_spin_lock(map);
938 bool has_timer = map_value_has_timer(map);
939 bool has_kptrs = map_value_has_kptrs(map);
940 struct bpf_map_off_arr *off_arr;
941 u32 i;
942
943 if (!has_spin_lock && !has_timer && !has_kptrs) {
944 map->off_arr = NULL;
945 return 0;
946 }
947
948 off_arr = kmalloc(sizeof(*map->off_arr), GFP_KERNEL | __GFP_NOWARN);
949 if (!off_arr)
950 return -ENOMEM;
951 map->off_arr = off_arr;
952
953 off_arr->cnt = 0;
954 if (has_spin_lock) {
955 i = off_arr->cnt;
956
957 off_arr->field_off[i] = map->spin_lock_off;
958 off_arr->field_sz[i] = sizeof(struct bpf_spin_lock);
959 off_arr->cnt++;
960 }
961 if (has_timer) {
962 i = off_arr->cnt;
963
964 off_arr->field_off[i] = map->timer_off;
965 off_arr->field_sz[i] = sizeof(struct bpf_timer);
966 off_arr->cnt++;
967 }
968 if (has_kptrs) {
969 struct bpf_map_value_off *tab = map->kptr_off_tab;
970 u32 *off = &off_arr->field_off[off_arr->cnt];
971 u8 *sz = &off_arr->field_sz[off_arr->cnt];
972
973 for (i = 0; i < tab->nr_off; i++) {
974 *off++ = tab->off[i].offset;
975 *sz++ = sizeof(u64);
976 }
977 off_arr->cnt += tab->nr_off;
978 }
979
980 if (off_arr->cnt == 1)
981 return 0;
982 sort_r(off_arr->field_off, off_arr->cnt, sizeof(off_arr->field_off[0]),
983 map_off_arr_cmp, map_off_arr_swap, map);
984 return 0;
985}
986
d83525ca 987static int map_check_btf(struct bpf_map *map, const struct btf *btf,
e8d2bec0
DB
988 u32 btf_key_id, u32 btf_value_id)
989{
990 const struct btf_type *key_type, *value_type;
991 u32 key_size, value_size;
992 int ret = 0;
993
2824ecb7
DB
994 /* Some maps allow key to be unspecified. */
995 if (btf_key_id) {
996 key_type = btf_type_id_size(btf, &btf_key_id, &key_size);
997 if (!key_type || key_size != map->key_size)
998 return -EINVAL;
999 } else {
1000 key_type = btf_type_by_id(btf, 0);
1001 if (!map->ops->map_check_btf)
1002 return -EINVAL;
1003 }
e8d2bec0
DB
1004
1005 value_type = btf_type_id_size(btf, &btf_value_id, &value_size);
1006 if (!value_type || value_size != map->value_size)
1007 return -EINVAL;
1008
d83525ca
AS
1009 map->spin_lock_off = btf_find_spin_lock(btf, value_type);
1010
1011 if (map_value_has_spin_lock(map)) {
591fe988
DB
1012 if (map->map_flags & BPF_F_RDONLY_PROG)
1013 return -EACCES;
d83525ca 1014 if (map->map_type != BPF_MAP_TYPE_HASH &&
e16d2f1a 1015 map->map_type != BPF_MAP_TYPE_ARRAY &&
6ac99e8f 1016 map->map_type != BPF_MAP_TYPE_CGROUP_STORAGE &&
8ea63684 1017 map->map_type != BPF_MAP_TYPE_SK_STORAGE &&
4cf1bc1f
KS
1018 map->map_type != BPF_MAP_TYPE_INODE_STORAGE &&
1019 map->map_type != BPF_MAP_TYPE_TASK_STORAGE)
d83525ca
AS
1020 return -ENOTSUPP;
1021 if (map->spin_lock_off + sizeof(struct bpf_spin_lock) >
1022 map->value_size) {
1023 WARN_ONCE(1,
1024 "verifier bug spin_lock_off %d value_size %d\n",
1025 map->spin_lock_off, map->value_size);
1026 return -EFAULT;
1027 }
1028 }
1029
68134668
AS
1030 map->timer_off = btf_find_timer(btf, value_type);
1031 if (map_value_has_timer(map)) {
1032 if (map->map_flags & BPF_F_RDONLY_PROG)
1033 return -EACCES;
1034 if (map->map_type != BPF_MAP_TYPE_HASH &&
1035 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
1036 map->map_type != BPF_MAP_TYPE_ARRAY)
1037 return -EOPNOTSUPP;
1038 }
1039
61df10c7
KKD
1040 map->kptr_off_tab = btf_parse_kptrs(btf, value_type);
1041 if (map_value_has_kptrs(map)) {
1042 if (!bpf_capable()) {
1043 ret = -EPERM;
1044 goto free_map_tab;
1045 }
1046 if (map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) {
1047 ret = -EACCES;
1048 goto free_map_tab;
1049 }
1050 if (map->map_type != BPF_MAP_TYPE_HASH &&
1051 map->map_type != BPF_MAP_TYPE_LRU_HASH &&
6df4ea1f
KKD
1052 map->map_type != BPF_MAP_TYPE_ARRAY &&
1053 map->map_type != BPF_MAP_TYPE_PERCPU_ARRAY) {
61df10c7
KKD
1054 ret = -EOPNOTSUPP;
1055 goto free_map_tab;
1056 }
1057 }
1058
1059 if (map->ops->map_check_btf) {
1b2b234b 1060 ret = map->ops->map_check_btf(map, btf, key_type, value_type);
61df10c7
KKD
1061 if (ret < 0)
1062 goto free_map_tab;
1063 }
e8d2bec0 1064
61df10c7
KKD
1065 return ret;
1066free_map_tab:
1067 bpf_map_free_kptr_off_tab(map);
e8d2bec0
DB
1068 return ret;
1069}
1070
9330986c 1071#define BPF_MAP_CREATE_LAST_FIELD map_extra
99c55f7d
AS
1072/* called via syscall */
1073static int map_create(union bpf_attr *attr)
1074{
96eabe7a 1075 int numa_node = bpf_map_attr_numa_node(attr);
99c55f7d 1076 struct bpf_map *map;
6e71b04a 1077 int f_flags;
99c55f7d
AS
1078 int err;
1079
1080 err = CHECK_ATTR(BPF_MAP_CREATE);
1081 if (err)
1082 return -EINVAL;
1083
85d33df3
MKL
1084 if (attr->btf_vmlinux_value_type_id) {
1085 if (attr->map_type != BPF_MAP_TYPE_STRUCT_OPS ||
1086 attr->btf_key_type_id || attr->btf_value_type_id)
1087 return -EINVAL;
1088 } else if (attr->btf_key_type_id && !attr->btf_value_type_id) {
1089 return -EINVAL;
1090 }
1091
9330986c
JK
1092 if (attr->map_type != BPF_MAP_TYPE_BLOOM_FILTER &&
1093 attr->map_extra != 0)
1094 return -EINVAL;
1095
6e71b04a
CF
1096 f_flags = bpf_get_file_flag(attr->map_flags);
1097 if (f_flags < 0)
1098 return f_flags;
1099
96eabe7a 1100 if (numa_node != NUMA_NO_NODE &&
96e5ae4e
ED
1101 ((unsigned int)numa_node >= nr_node_ids ||
1102 !node_online(numa_node)))
96eabe7a
MKL
1103 return -EINVAL;
1104
99c55f7d
AS
1105 /* find map type and init map: hashtable vs rbtree vs bloom vs ... */
1106 map = find_and_alloc_map(attr);
1107 if (IS_ERR(map))
1108 return PTR_ERR(map);
1109
8e7ae251
MKL
1110 err = bpf_obj_name_cpy(map->name, attr->map_name,
1111 sizeof(attr->map_name));
1112 if (err < 0)
b936ca64 1113 goto free_map;
ad5b177b 1114
1e0bd5a0
AN
1115 atomic64_set(&map->refcnt, 1);
1116 atomic64_set(&map->usercnt, 1);
fc970227 1117 mutex_init(&map->freeze_mutex);
f45d5b6c 1118 spin_lock_init(&map->owner.lock);
99c55f7d 1119
85d33df3 1120 map->spin_lock_off = -EINVAL;
68134668 1121 map->timer_off = -EINVAL;
85d33df3
MKL
1122 if (attr->btf_key_type_id || attr->btf_value_type_id ||
1123 /* Even the map's value is a kernel's struct,
1124 * the bpf_prog.o must have BTF to begin with
1125 * to figure out the corresponding kernel's
1126 * counter part. Thus, attr->btf_fd has
1127 * to be valid also.
1128 */
1129 attr->btf_vmlinux_value_type_id) {
a26ca7c9
MKL
1130 struct btf *btf;
1131
a26ca7c9
MKL
1132 btf = btf_get_by_fd(attr->btf_fd);
1133 if (IS_ERR(btf)) {
1134 err = PTR_ERR(btf);
b936ca64 1135 goto free_map;
a26ca7c9 1136 }
350a5c4d
AS
1137 if (btf_is_kernel(btf)) {
1138 btf_put(btf);
1139 err = -EACCES;
1140 goto free_map;
1141 }
85d33df3 1142 map->btf = btf;
a26ca7c9 1143
85d33df3
MKL
1144 if (attr->btf_value_type_id) {
1145 err = map_check_btf(map, btf, attr->btf_key_type_id,
1146 attr->btf_value_type_id);
1147 if (err)
1148 goto free_map;
a26ca7c9
MKL
1149 }
1150
9b2cf328
MKL
1151 map->btf_key_type_id = attr->btf_key_type_id;
1152 map->btf_value_type_id = attr->btf_value_type_id;
85d33df3
MKL
1153 map->btf_vmlinux_value_type_id =
1154 attr->btf_vmlinux_value_type_id;
a26ca7c9
MKL
1155 }
1156
4d7d7f69 1157 err = bpf_map_alloc_off_arr(map);
aaac3ba9 1158 if (err)
b936ca64 1159 goto free_map;
afdb09c7 1160
4d7d7f69
KKD
1161 err = security_bpf_map_alloc(map);
1162 if (err)
1163 goto free_map_off_arr;
1164
f3f1c054
MKL
1165 err = bpf_map_alloc_id(map);
1166 if (err)
b936ca64 1167 goto free_map_sec;
f3f1c054 1168
48edc1f7
RG
1169 bpf_map_save_memcg(map);
1170
6e71b04a 1171 err = bpf_map_new_fd(map, f_flags);
bd5f5f4e
MKL
1172 if (err < 0) {
1173 /* failed to allocate fd.
352d20d6 1174 * bpf_map_put_with_uref() is needed because the above
bd5f5f4e
MKL
1175 * bpf_map_alloc_id() has published the map
1176 * to the userspace and the userspace may
1177 * have refcnt-ed it through BPF_MAP_GET_FD_BY_ID.
1178 */
352d20d6 1179 bpf_map_put_with_uref(map);
bd5f5f4e
MKL
1180 return err;
1181 }
99c55f7d
AS
1182
1183 return err;
1184
afdb09c7
CF
1185free_map_sec:
1186 security_bpf_map_free(map);
4d7d7f69
KKD
1187free_map_off_arr:
1188 kfree(map->off_arr);
b936ca64 1189free_map:
a26ca7c9 1190 btf_put(map->btf);
99c55f7d
AS
1191 map->ops->map_free(map);
1192 return err;
1193}
1194
db20fd2b
AS
1195/* if error is returned, fd is released.
1196 * On success caller should complete fd access with matching fdput()
1197 */
c2101297 1198struct bpf_map *__bpf_map_get(struct fd f)
db20fd2b 1199{
db20fd2b
AS
1200 if (!f.file)
1201 return ERR_PTR(-EBADF);
db20fd2b
AS
1202 if (f.file->f_op != &bpf_map_fops) {
1203 fdput(f);
1204 return ERR_PTR(-EINVAL);
1205 }
1206
c2101297
DB
1207 return f.file->private_data;
1208}
1209
1e0bd5a0 1210void bpf_map_inc(struct bpf_map *map)
c9da161c 1211{
1e0bd5a0 1212 atomic64_inc(&map->refcnt);
c9da161c 1213}
630a4d38 1214EXPORT_SYMBOL_GPL(bpf_map_inc);
c9da161c 1215
1e0bd5a0
AN
1216void bpf_map_inc_with_uref(struct bpf_map *map)
1217{
1218 atomic64_inc(&map->refcnt);
1219 atomic64_inc(&map->usercnt);
1220}
1221EXPORT_SYMBOL_GPL(bpf_map_inc_with_uref);
1222
1ed4d924
MKL
1223struct bpf_map *bpf_map_get(u32 ufd)
1224{
1225 struct fd f = fdget(ufd);
1226 struct bpf_map *map;
1227
1228 map = __bpf_map_get(f);
1229 if (IS_ERR(map))
1230 return map;
1231
1232 bpf_map_inc(map);
1233 fdput(f);
1234
1235 return map;
1236}
b1d18a75 1237EXPORT_SYMBOL(bpf_map_get);
1ed4d924 1238
c9da161c 1239struct bpf_map *bpf_map_get_with_uref(u32 ufd)
c2101297
DB
1240{
1241 struct fd f = fdget(ufd);
1242 struct bpf_map *map;
1243
1244 map = __bpf_map_get(f);
1245 if (IS_ERR(map))
1246 return map;
1247
1e0bd5a0 1248 bpf_map_inc_with_uref(map);
c2101297 1249 fdput(f);
db20fd2b
AS
1250
1251 return map;
1252}
1253
bd5f5f4e 1254/* map_idr_lock should have been held */
1e0bd5a0 1255static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref)
bd5f5f4e
MKL
1256{
1257 int refold;
1258
1e0bd5a0 1259 refold = atomic64_fetch_add_unless(&map->refcnt, 1, 0);
bd5f5f4e
MKL
1260 if (!refold)
1261 return ERR_PTR(-ENOENT);
bd5f5f4e 1262 if (uref)
1e0bd5a0 1263 atomic64_inc(&map->usercnt);
bd5f5f4e
MKL
1264
1265 return map;
1266}
1267
1e0bd5a0 1268struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map)
b0e4701c
SF
1269{
1270 spin_lock_bh(&map_idr_lock);
1e0bd5a0 1271 map = __bpf_map_inc_not_zero(map, false);
b0e4701c
SF
1272 spin_unlock_bh(&map_idr_lock);
1273
1274 return map;
1275}
1276EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero);
1277
b8cdc051
AS
1278int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value)
1279{
1280 return -ENOTSUPP;
1281}
1282
c9d29f46
MV
1283static void *__bpf_copy_key(void __user *ukey, u64 key_size)
1284{
1285 if (key_size)
44779a4b 1286 return vmemdup_user(ukey, key_size);
c9d29f46
MV
1287
1288 if (ukey)
1289 return ERR_PTR(-EINVAL);
1290
1291 return NULL;
1292}
1293
af2ac3e1
AS
1294static void *___bpf_copy_key(bpfptr_t ukey, u64 key_size)
1295{
1296 if (key_size)
44779a4b 1297 return kvmemdup_bpfptr(ukey, key_size);
af2ac3e1
AS
1298
1299 if (!bpfptr_is_null(ukey))
1300 return ERR_PTR(-EINVAL);
1301
1302 return NULL;
1303}
1304
db20fd2b 1305/* last field in 'union bpf_attr' used by this command */
96049f3a 1306#define BPF_MAP_LOOKUP_ELEM_LAST_FIELD flags
db20fd2b
AS
1307
1308static int map_lookup_elem(union bpf_attr *attr)
1309{
535e7b4b
MS
1310 void __user *ukey = u64_to_user_ptr(attr->key);
1311 void __user *uvalue = u64_to_user_ptr(attr->value);
db20fd2b 1312 int ufd = attr->map_fd;
db20fd2b 1313 struct bpf_map *map;
15c14a3d 1314 void *key, *value;
15a07b33 1315 u32 value_size;
592867bf 1316 struct fd f;
db20fd2b
AS
1317 int err;
1318
1319 if (CHECK_ATTR(BPF_MAP_LOOKUP_ELEM))
1320 return -EINVAL;
1321
96049f3a
AS
1322 if (attr->flags & ~BPF_F_LOCK)
1323 return -EINVAL;
1324
592867bf 1325 f = fdget(ufd);
c2101297 1326 map = __bpf_map_get(f);
db20fd2b
AS
1327 if (IS_ERR(map))
1328 return PTR_ERR(map);
87df15de 1329 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
6e71b04a
CF
1330 err = -EPERM;
1331 goto err_put;
1332 }
1333
96049f3a
AS
1334 if ((attr->flags & BPF_F_LOCK) &&
1335 !map_value_has_spin_lock(map)) {
1336 err = -EINVAL;
1337 goto err_put;
1338 }
1339
c9d29f46 1340 key = __bpf_copy_key(ukey, map->key_size);
e4448ed8
AV
1341 if (IS_ERR(key)) {
1342 err = PTR_ERR(key);
db20fd2b 1343 goto err_put;
e4448ed8 1344 }
db20fd2b 1345
15c14a3d 1346 value_size = bpf_map_value_size(map);
15a07b33 1347
8ebe667c 1348 err = -ENOMEM;
f0dce1d9 1349 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
db20fd2b 1350 if (!value)
8ebe667c
AS
1351 goto free_key;
1352
9330986c
JK
1353 if (map->map_type == BPF_MAP_TYPE_BLOOM_FILTER) {
1354 if (copy_from_user(value, uvalue, value_size))
1355 err = -EFAULT;
1356 else
1357 err = bpf_map_copy_value(map, key, value, attr->flags);
1358 goto free_value;
1359 }
1360
15c14a3d 1361 err = bpf_map_copy_value(map, key, value, attr->flags);
15a07b33 1362 if (err)
8ebe667c 1363 goto free_value;
db20fd2b
AS
1364
1365 err = -EFAULT;
15a07b33 1366 if (copy_to_user(uvalue, value, value_size) != 0)
8ebe667c 1367 goto free_value;
db20fd2b
AS
1368
1369 err = 0;
1370
8ebe667c 1371free_value:
f0dce1d9 1372 kvfree(value);
db20fd2b 1373free_key:
44779a4b 1374 kvfree(key);
db20fd2b
AS
1375err_put:
1376 fdput(f);
1377 return err;
1378}
1379
1ae80cf3 1380
3274f520 1381#define BPF_MAP_UPDATE_ELEM_LAST_FIELD flags
db20fd2b 1382
af2ac3e1 1383static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
db20fd2b 1384{
af2ac3e1
AS
1385 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
1386 bpfptr_t uvalue = make_bpfptr(attr->value, uattr.is_kernel);
db20fd2b 1387 int ufd = attr->map_fd;
db20fd2b
AS
1388 struct bpf_map *map;
1389 void *key, *value;
15a07b33 1390 u32 value_size;
592867bf 1391 struct fd f;
db20fd2b
AS
1392 int err;
1393
1394 if (CHECK_ATTR(BPF_MAP_UPDATE_ELEM))
1395 return -EINVAL;
1396
592867bf 1397 f = fdget(ufd);
c2101297 1398 map = __bpf_map_get(f);
db20fd2b
AS
1399 if (IS_ERR(map))
1400 return PTR_ERR(map);
353050be 1401 bpf_map_write_active_inc(map);
87df15de 1402 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
6e71b04a
CF
1403 err = -EPERM;
1404 goto err_put;
1405 }
1406
96049f3a
AS
1407 if ((attr->flags & BPF_F_LOCK) &&
1408 !map_value_has_spin_lock(map)) {
1409 err = -EINVAL;
1410 goto err_put;
1411 }
1412
af2ac3e1 1413 key = ___bpf_copy_key(ukey, map->key_size);
e4448ed8
AV
1414 if (IS_ERR(key)) {
1415 err = PTR_ERR(key);
db20fd2b 1416 goto err_put;
e4448ed8 1417 }
db20fd2b 1418
f0dce1d9 1419 value_size = bpf_map_value_size(map);
a02c118e
WY
1420 value = kvmemdup_bpfptr(uvalue, value_size);
1421 if (IS_ERR(value)) {
1422 err = PTR_ERR(value);
db20fd2b 1423 goto free_key;
a02c118e 1424 }
db20fd2b 1425
15c14a3d 1426 err = bpf_map_update_value(map, f, key, value, attr->flags);
6710e112 1427
f0dce1d9 1428 kvfree(value);
db20fd2b 1429free_key:
44779a4b 1430 kvfree(key);
db20fd2b 1431err_put:
353050be 1432 bpf_map_write_active_dec(map);
db20fd2b
AS
1433 fdput(f);
1434 return err;
1435}
1436
1437#define BPF_MAP_DELETE_ELEM_LAST_FIELD key
1438
b88df697 1439static int map_delete_elem(union bpf_attr *attr, bpfptr_t uattr)
db20fd2b 1440{
b88df697 1441 bpfptr_t ukey = make_bpfptr(attr->key, uattr.is_kernel);
db20fd2b 1442 int ufd = attr->map_fd;
db20fd2b 1443 struct bpf_map *map;
592867bf 1444 struct fd f;
db20fd2b
AS
1445 void *key;
1446 int err;
1447
1448 if (CHECK_ATTR(BPF_MAP_DELETE_ELEM))
1449 return -EINVAL;
1450
592867bf 1451 f = fdget(ufd);
c2101297 1452 map = __bpf_map_get(f);
db20fd2b
AS
1453 if (IS_ERR(map))
1454 return PTR_ERR(map);
353050be 1455 bpf_map_write_active_inc(map);
87df15de 1456 if (!(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
6e71b04a
CF
1457 err = -EPERM;
1458 goto err_put;
1459 }
1460
b88df697 1461 key = ___bpf_copy_key(ukey, map->key_size);
e4448ed8
AV
1462 if (IS_ERR(key)) {
1463 err = PTR_ERR(key);
db20fd2b 1464 goto err_put;
e4448ed8 1465 }
db20fd2b 1466
a3884572
JK
1467 if (bpf_map_is_dev_bound(map)) {
1468 err = bpf_map_offload_delete_elem(map, key);
1469 goto out;
85d33df3
MKL
1470 } else if (IS_FD_PROG_ARRAY(map) ||
1471 map->map_type == BPF_MAP_TYPE_STRUCT_OPS) {
1472 /* These maps require sleepable context */
da765a2f
DB
1473 err = map->ops->map_delete_elem(map, key);
1474 goto out;
a3884572
JK
1475 }
1476
b6e5dae1 1477 bpf_disable_instrumentation();
db20fd2b
AS
1478 rcu_read_lock();
1479 err = map->ops->map_delete_elem(map, key);
1480 rcu_read_unlock();
b6e5dae1 1481 bpf_enable_instrumentation();
1ae80cf3 1482 maybe_wait_bpf_programs(map);
a3884572 1483out:
44779a4b 1484 kvfree(key);
db20fd2b 1485err_put:
353050be 1486 bpf_map_write_active_dec(map);
db20fd2b
AS
1487 fdput(f);
1488 return err;
1489}
1490
1491/* last field in 'union bpf_attr' used by this command */
1492#define BPF_MAP_GET_NEXT_KEY_LAST_FIELD next_key
1493
1494static int map_get_next_key(union bpf_attr *attr)
1495{
535e7b4b
MS
1496 void __user *ukey = u64_to_user_ptr(attr->key);
1497 void __user *unext_key = u64_to_user_ptr(attr->next_key);
db20fd2b 1498 int ufd = attr->map_fd;
db20fd2b
AS
1499 struct bpf_map *map;
1500 void *key, *next_key;
592867bf 1501 struct fd f;
db20fd2b
AS
1502 int err;
1503
1504 if (CHECK_ATTR(BPF_MAP_GET_NEXT_KEY))
1505 return -EINVAL;
1506
592867bf 1507 f = fdget(ufd);
c2101297 1508 map = __bpf_map_get(f);
db20fd2b
AS
1509 if (IS_ERR(map))
1510 return PTR_ERR(map);
87df15de 1511 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
6e71b04a
CF
1512 err = -EPERM;
1513 goto err_put;
1514 }
1515
8fe45924 1516 if (ukey) {
c9d29f46 1517 key = __bpf_copy_key(ukey, map->key_size);
e4448ed8
AV
1518 if (IS_ERR(key)) {
1519 err = PTR_ERR(key);
8fe45924 1520 goto err_put;
e4448ed8 1521 }
8fe45924
TQ
1522 } else {
1523 key = NULL;
1524 }
db20fd2b
AS
1525
1526 err = -ENOMEM;
44779a4b 1527 next_key = kvmalloc(map->key_size, GFP_USER);
db20fd2b
AS
1528 if (!next_key)
1529 goto free_key;
1530
a3884572
JK
1531 if (bpf_map_is_dev_bound(map)) {
1532 err = bpf_map_offload_get_next_key(map, key, next_key);
1533 goto out;
1534 }
1535
db20fd2b
AS
1536 rcu_read_lock();
1537 err = map->ops->map_get_next_key(map, key, next_key);
1538 rcu_read_unlock();
a3884572 1539out:
db20fd2b
AS
1540 if (err)
1541 goto free_next_key;
1542
1543 err = -EFAULT;
1544 if (copy_to_user(unext_key, next_key, map->key_size) != 0)
1545 goto free_next_key;
1546
1547 err = 0;
1548
1549free_next_key:
44779a4b 1550 kvfree(next_key);
db20fd2b 1551free_key:
44779a4b 1552 kvfree(key);
db20fd2b
AS
1553err_put:
1554 fdput(f);
1555 return err;
1556}
1557
aa2e93b8
BV
1558int generic_map_delete_batch(struct bpf_map *map,
1559 const union bpf_attr *attr,
1560 union bpf_attr __user *uattr)
1561{
1562 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1563 u32 cp, max_count;
1564 int err = 0;
1565 void *key;
1566
1567 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1568 return -EINVAL;
1569
1570 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1571 !map_value_has_spin_lock(map)) {
1572 return -EINVAL;
1573 }
1574
1575 max_count = attr->batch.count;
1576 if (!max_count)
1577 return 0;
1578
44779a4b 1579 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
2e3a94aa
BV
1580 if (!key)
1581 return -ENOMEM;
1582
aa2e93b8 1583 for (cp = 0; cp < max_count; cp++) {
2e3a94aa
BV
1584 err = -EFAULT;
1585 if (copy_from_user(key, keys + cp * map->key_size,
1586 map->key_size))
aa2e93b8 1587 break;
aa2e93b8
BV
1588
1589 if (bpf_map_is_dev_bound(map)) {
1590 err = bpf_map_offload_delete_elem(map, key);
1591 break;
1592 }
1593
b6e5dae1 1594 bpf_disable_instrumentation();
aa2e93b8
BV
1595 rcu_read_lock();
1596 err = map->ops->map_delete_elem(map, key);
1597 rcu_read_unlock();
b6e5dae1 1598 bpf_enable_instrumentation();
aa2e93b8
BV
1599 if (err)
1600 break;
75134f16 1601 cond_resched();
aa2e93b8
BV
1602 }
1603 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1604 err = -EFAULT;
2e3a94aa 1605
44779a4b 1606 kvfree(key);
9087c6ff
ED
1607
1608 maybe_wait_bpf_programs(map);
aa2e93b8
BV
1609 return err;
1610}
1611
1612int generic_map_update_batch(struct bpf_map *map,
1613 const union bpf_attr *attr,
1614 union bpf_attr __user *uattr)
1615{
1616 void __user *values = u64_to_user_ptr(attr->batch.values);
1617 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1618 u32 value_size, cp, max_count;
fda7a387 1619 int ufd = attr->batch.map_fd;
aa2e93b8
BV
1620 void *key, *value;
1621 struct fd f;
1622 int err = 0;
1623
aa2e93b8
BV
1624 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1625 return -EINVAL;
1626
1627 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1628 !map_value_has_spin_lock(map)) {
1629 return -EINVAL;
1630 }
1631
1632 value_size = bpf_map_value_size(map);
1633
1634 max_count = attr->batch.count;
1635 if (!max_count)
1636 return 0;
1637
44779a4b 1638 key = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
2e3a94aa
BV
1639 if (!key)
1640 return -ENOMEM;
1641
f0dce1d9 1642 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
2e3a94aa 1643 if (!value) {
44779a4b 1644 kvfree(key);
aa2e93b8 1645 return -ENOMEM;
2e3a94aa 1646 }
aa2e93b8 1647
fda7a387 1648 f = fdget(ufd); /* bpf_map_do_batch() guarantees ufd is valid */
aa2e93b8 1649 for (cp = 0; cp < max_count; cp++) {
aa2e93b8 1650 err = -EFAULT;
2e3a94aa
BV
1651 if (copy_from_user(key, keys + cp * map->key_size,
1652 map->key_size) ||
1653 copy_from_user(value, values + cp * value_size, value_size))
aa2e93b8
BV
1654 break;
1655
1656 err = bpf_map_update_value(map, f, key, value,
1657 attr->batch.elem_flags);
1658
1659 if (err)
1660 break;
75134f16 1661 cond_resched();
aa2e93b8
BV
1662 }
1663
1664 if (copy_to_user(&uattr->batch.count, &cp, sizeof(cp)))
1665 err = -EFAULT;
1666
f0dce1d9 1667 kvfree(value);
44779a4b 1668 kvfree(key);
fda7a387 1669 fdput(f);
aa2e93b8
BV
1670 return err;
1671}
1672
cb4d03ab
BV
1673#define MAP_LOOKUP_RETRIES 3
1674
1675int generic_map_lookup_batch(struct bpf_map *map,
1676 const union bpf_attr *attr,
1677 union bpf_attr __user *uattr)
1678{
1679 void __user *uobatch = u64_to_user_ptr(attr->batch.out_batch);
1680 void __user *ubatch = u64_to_user_ptr(attr->batch.in_batch);
1681 void __user *values = u64_to_user_ptr(attr->batch.values);
1682 void __user *keys = u64_to_user_ptr(attr->batch.keys);
1683 void *buf, *buf_prevkey, *prev_key, *key, *value;
1684 int err, retry = MAP_LOOKUP_RETRIES;
1685 u32 value_size, cp, max_count;
cb4d03ab
BV
1686
1687 if (attr->batch.elem_flags & ~BPF_F_LOCK)
1688 return -EINVAL;
1689
1690 if ((attr->batch.elem_flags & BPF_F_LOCK) &&
1691 !map_value_has_spin_lock(map))
1692 return -EINVAL;
1693
1694 value_size = bpf_map_value_size(map);
1695
1696 max_count = attr->batch.count;
1697 if (!max_count)
1698 return 0;
1699
1700 if (put_user(0, &uattr->batch.count))
1701 return -EFAULT;
1702
44779a4b 1703 buf_prevkey = kvmalloc(map->key_size, GFP_USER | __GFP_NOWARN);
cb4d03ab
BV
1704 if (!buf_prevkey)
1705 return -ENOMEM;
1706
f0dce1d9 1707 buf = kvmalloc(map->key_size + value_size, GFP_USER | __GFP_NOWARN);
cb4d03ab 1708 if (!buf) {
44779a4b 1709 kvfree(buf_prevkey);
cb4d03ab
BV
1710 return -ENOMEM;
1711 }
1712
1713 err = -EFAULT;
cb4d03ab
BV
1714 prev_key = NULL;
1715 if (ubatch && copy_from_user(buf_prevkey, ubatch, map->key_size))
1716 goto free_buf;
1717 key = buf;
1718 value = key + map->key_size;
1719 if (ubatch)
1720 prev_key = buf_prevkey;
1721
1722 for (cp = 0; cp < max_count;) {
1723 rcu_read_lock();
1724 err = map->ops->map_get_next_key(map, prev_key, key);
1725 rcu_read_unlock();
1726 if (err)
1727 break;
1728 err = bpf_map_copy_value(map, key, value,
1729 attr->batch.elem_flags);
1730
1731 if (err == -ENOENT) {
1732 if (retry) {
1733 retry--;
1734 continue;
1735 }
1736 err = -EINTR;
1737 break;
1738 }
1739
1740 if (err)
1741 goto free_buf;
1742
1743 if (copy_to_user(keys + cp * map->key_size, key,
1744 map->key_size)) {
1745 err = -EFAULT;
1746 goto free_buf;
1747 }
1748 if (copy_to_user(values + cp * value_size, value, value_size)) {
1749 err = -EFAULT;
1750 goto free_buf;
1751 }
1752
1753 if (!prev_key)
1754 prev_key = buf_prevkey;
1755
1756 swap(prev_key, key);
1757 retry = MAP_LOOKUP_RETRIES;
1758 cp++;
75134f16 1759 cond_resched();
cb4d03ab
BV
1760 }
1761
1762 if (err == -EFAULT)
1763 goto free_buf;
1764
1765 if ((copy_to_user(&uattr->batch.count, &cp, sizeof(cp)) ||
1766 (cp && copy_to_user(uobatch, prev_key, map->key_size))))
1767 err = -EFAULT;
1768
1769free_buf:
44779a4b 1770 kvfree(buf_prevkey);
f0dce1d9 1771 kvfree(buf);
cb4d03ab
BV
1772 return err;
1773}
1774
3e87f192 1775#define BPF_MAP_LOOKUP_AND_DELETE_ELEM_LAST_FIELD flags
bd513cd0
MV
1776
1777static int map_lookup_and_delete_elem(union bpf_attr *attr)
1778{
1779 void __user *ukey = u64_to_user_ptr(attr->key);
1780 void __user *uvalue = u64_to_user_ptr(attr->value);
1781 int ufd = attr->map_fd;
1782 struct bpf_map *map;
540fefc0 1783 void *key, *value;
bd513cd0
MV
1784 u32 value_size;
1785 struct fd f;
1786 int err;
1787
1788 if (CHECK_ATTR(BPF_MAP_LOOKUP_AND_DELETE_ELEM))
1789 return -EINVAL;
1790
3e87f192
DS
1791 if (attr->flags & ~BPF_F_LOCK)
1792 return -EINVAL;
1793
bd513cd0
MV
1794 f = fdget(ufd);
1795 map = __bpf_map_get(f);
1796 if (IS_ERR(map))
1797 return PTR_ERR(map);
353050be 1798 bpf_map_write_active_inc(map);
1ea0f912
AP
1799 if (!(map_get_sys_perms(map, f) & FMODE_CAN_READ) ||
1800 !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
bd513cd0
MV
1801 err = -EPERM;
1802 goto err_put;
1803 }
1804
3e87f192
DS
1805 if (attr->flags &&
1806 (map->map_type == BPF_MAP_TYPE_QUEUE ||
1807 map->map_type == BPF_MAP_TYPE_STACK)) {
1808 err = -EINVAL;
1809 goto err_put;
1810 }
1811
1812 if ((attr->flags & BPF_F_LOCK) &&
1813 !map_value_has_spin_lock(map)) {
1814 err = -EINVAL;
1815 goto err_put;
1816 }
1817
bd513cd0
MV
1818 key = __bpf_copy_key(ukey, map->key_size);
1819 if (IS_ERR(key)) {
1820 err = PTR_ERR(key);
1821 goto err_put;
1822 }
1823
3e87f192 1824 value_size = bpf_map_value_size(map);
bd513cd0
MV
1825
1826 err = -ENOMEM;
f0dce1d9 1827 value = kvmalloc(value_size, GFP_USER | __GFP_NOWARN);
bd513cd0
MV
1828 if (!value)
1829 goto free_key;
1830
3e87f192 1831 err = -ENOTSUPP;
bd513cd0
MV
1832 if (map->map_type == BPF_MAP_TYPE_QUEUE ||
1833 map->map_type == BPF_MAP_TYPE_STACK) {
1834 err = map->ops->map_pop_elem(map, value);
3e87f192
DS
1835 } else if (map->map_type == BPF_MAP_TYPE_HASH ||
1836 map->map_type == BPF_MAP_TYPE_PERCPU_HASH ||
1837 map->map_type == BPF_MAP_TYPE_LRU_HASH ||
1838 map->map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
1839 if (!bpf_map_is_dev_bound(map)) {
1840 bpf_disable_instrumentation();
1841 rcu_read_lock();
1842 err = map->ops->map_lookup_and_delete_elem(map, key, value, attr->flags);
1843 rcu_read_unlock();
1844 bpf_enable_instrumentation();
1845 }
bd513cd0
MV
1846 }
1847
1848 if (err)
1849 goto free_value;
1850
7f645462
WY
1851 if (copy_to_user(uvalue, value, value_size) != 0) {
1852 err = -EFAULT;
bd513cd0 1853 goto free_value;
7f645462 1854 }
bd513cd0
MV
1855
1856 err = 0;
1857
1858free_value:
f0dce1d9 1859 kvfree(value);
bd513cd0 1860free_key:
44779a4b 1861 kvfree(key);
bd513cd0 1862err_put:
353050be 1863 bpf_map_write_active_dec(map);
bd513cd0
MV
1864 fdput(f);
1865 return err;
1866}
1867
87df15de
DB
1868#define BPF_MAP_FREEZE_LAST_FIELD map_fd
1869
1870static int map_freeze(const union bpf_attr *attr)
1871{
1872 int err = 0, ufd = attr->map_fd;
1873 struct bpf_map *map;
1874 struct fd f;
1875
1876 if (CHECK_ATTR(BPF_MAP_FREEZE))
1877 return -EINVAL;
1878
1879 f = fdget(ufd);
1880 map = __bpf_map_get(f);
1881 if (IS_ERR(map))
1882 return PTR_ERR(map);
fc970227 1883
68134668 1884 if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS ||
61df10c7 1885 map_value_has_timer(map) || map_value_has_kptrs(map)) {
849b4d94
MKL
1886 fdput(f);
1887 return -ENOTSUPP;
1888 }
1889
fc970227 1890 mutex_lock(&map->freeze_mutex);
353050be 1891 if (bpf_map_write_active(map)) {
fc970227
AN
1892 err = -EBUSY;
1893 goto err_put;
1894 }
87df15de
DB
1895 if (READ_ONCE(map->frozen)) {
1896 err = -EBUSY;
1897 goto err_put;
1898 }
2c78ee89 1899 if (!bpf_capable()) {
87df15de
DB
1900 err = -EPERM;
1901 goto err_put;
1902 }
1903
1904 WRITE_ONCE(map->frozen, true);
1905err_put:
fc970227 1906 mutex_unlock(&map->freeze_mutex);
87df15de
DB
1907 fdput(f);
1908 return err;
1909}
1910
7de16e3a 1911static const struct bpf_prog_ops * const bpf_prog_types[] = {
91cc1a99 1912#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
7de16e3a
JK
1913 [_id] = & _name ## _prog_ops,
1914#define BPF_MAP_TYPE(_id, _ops)
f2e10bff 1915#define BPF_LINK_TYPE(_id, _name)
7de16e3a
JK
1916#include <linux/bpf_types.h>
1917#undef BPF_PROG_TYPE
1918#undef BPF_MAP_TYPE
f2e10bff 1919#undef BPF_LINK_TYPE
7de16e3a
JK
1920};
1921
09756af4
AS
1922static int find_prog_type(enum bpf_prog_type type, struct bpf_prog *prog)
1923{
d0f1a451
DB
1924 const struct bpf_prog_ops *ops;
1925
1926 if (type >= ARRAY_SIZE(bpf_prog_types))
1927 return -EINVAL;
1928 type = array_index_nospec(type, ARRAY_SIZE(bpf_prog_types));
1929 ops = bpf_prog_types[type];
1930 if (!ops)
be9370a7 1931 return -EINVAL;
09756af4 1932
ab3f0063 1933 if (!bpf_prog_is_dev_bound(prog->aux))
d0f1a451 1934 prog->aux->ops = ops;
ab3f0063
JK
1935 else
1936 prog->aux->ops = &bpf_offload_prog_ops;
be9370a7
JB
1937 prog->type = type;
1938 return 0;
09756af4
AS
1939}
1940
bae141f5
DB
1941enum bpf_audit {
1942 BPF_AUDIT_LOAD,
1943 BPF_AUDIT_UNLOAD,
1944 BPF_AUDIT_MAX,
1945};
1946
1947static const char * const bpf_audit_str[BPF_AUDIT_MAX] = {
1948 [BPF_AUDIT_LOAD] = "LOAD",
1949 [BPF_AUDIT_UNLOAD] = "UNLOAD",
1950};
1951
1952static void bpf_audit_prog(const struct bpf_prog *prog, unsigned int op)
1953{
1954 struct audit_context *ctx = NULL;
1955 struct audit_buffer *ab;
1956
1957 if (WARN_ON_ONCE(op >= BPF_AUDIT_MAX))
1958 return;
1959 if (audit_enabled == AUDIT_OFF)
1960 return;
1961 if (op == BPF_AUDIT_LOAD)
1962 ctx = audit_context();
1963 ab = audit_log_start(ctx, GFP_ATOMIC, AUDIT_BPF);
1964 if (unlikely(!ab))
1965 return;
1966 audit_log_format(ab, "prog-id=%u op=%s",
1967 prog->aux->id, bpf_audit_str[op]);
1968 audit_log_end(ab);
1969}
1970
dc4bb0e2
MKL
1971static int bpf_prog_alloc_id(struct bpf_prog *prog)
1972{
1973 int id;
1974
b76354cd 1975 idr_preload(GFP_KERNEL);
dc4bb0e2
MKL
1976 spin_lock_bh(&prog_idr_lock);
1977 id = idr_alloc_cyclic(&prog_idr, prog, 1, INT_MAX, GFP_ATOMIC);
1978 if (id > 0)
1979 prog->aux->id = id;
1980 spin_unlock_bh(&prog_idr_lock);
b76354cd 1981 idr_preload_end();
dc4bb0e2
MKL
1982
1983 /* id is in [1, INT_MAX) */
1984 if (WARN_ON_ONCE(!id))
1985 return -ENOSPC;
1986
1987 return id > 0 ? 0 : id;
1988}
1989
ad8ad79f 1990void bpf_prog_free_id(struct bpf_prog *prog, bool do_idr_lock)
dc4bb0e2 1991{
d809e134
AS
1992 unsigned long flags;
1993
ad8ad79f
JK
1994 /* cBPF to eBPF migrations are currently not in the idr store.
1995 * Offloaded programs are removed from the store when their device
1996 * disappears - even if someone grabs an fd to them they are unusable,
1997 * simply waiting for refcnt to drop to be freed.
1998 */
dc4bb0e2
MKL
1999 if (!prog->aux->id)
2000 return;
2001
b16d9aa4 2002 if (do_idr_lock)
d809e134 2003 spin_lock_irqsave(&prog_idr_lock, flags);
b16d9aa4
MKL
2004 else
2005 __acquire(&prog_idr_lock);
2006
dc4bb0e2 2007 idr_remove(&prog_idr, prog->aux->id);
ad8ad79f 2008 prog->aux->id = 0;
b16d9aa4
MKL
2009
2010 if (do_idr_lock)
d809e134 2011 spin_unlock_irqrestore(&prog_idr_lock, flags);
b16d9aa4
MKL
2012 else
2013 __release(&prog_idr_lock);
dc4bb0e2
MKL
2014}
2015
1aacde3d 2016static void __bpf_prog_put_rcu(struct rcu_head *rcu)
abf2e7d6
AS
2017{
2018 struct bpf_prog_aux *aux = container_of(rcu, struct bpf_prog_aux, rcu);
2019
3b4d9eb2 2020 kvfree(aux->func_info);
8c1b6e69 2021 kfree(aux->func_info_aux);
3ac1f01b 2022 free_uid(aux->user);
afdb09c7 2023 security_bpf_prog_free(aux);
abf2e7d6
AS
2024 bpf_prog_free(aux->prog);
2025}
2026
cd7455f1
DB
2027static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)
2028{
2029 bpf_prog_kallsyms_del_all(prog);
2030 btf_put(prog->aux->btf);
e16301fb
MKL
2031 kvfree(prog->aux->jited_linfo);
2032 kvfree(prog->aux->linfo);
e6ac2450 2033 kfree(prog->aux->kfunc_tab);
22dc4a0f
AN
2034 if (prog->aux->attach_btf)
2035 btf_put(prog->aux->attach_btf);
cd7455f1 2036
1e6c62a8
AS
2037 if (deferred) {
2038 if (prog->aux->sleepable)
2039 call_rcu_tasks_trace(&prog->aux->rcu, __bpf_prog_put_rcu);
2040 else
2041 call_rcu(&prog->aux->rcu, __bpf_prog_put_rcu);
2042 } else {
cd7455f1 2043 __bpf_prog_put_rcu(&prog->aux->rcu);
1e6c62a8 2044 }
cd7455f1
DB
2045}
2046
d809e134
AS
2047static void bpf_prog_put_deferred(struct work_struct *work)
2048{
2049 struct bpf_prog_aux *aux;
2050 struct bpf_prog *prog;
2051
2052 aux = container_of(work, struct bpf_prog_aux, work);
2053 prog = aux->prog;
2054 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_UNLOAD, 0);
2055 bpf_audit_prog(prog, BPF_AUDIT_UNLOAD);
2056 __bpf_prog_put_noref(prog, true);
2057}
2058
b16d9aa4 2059static void __bpf_prog_put(struct bpf_prog *prog, bool do_idr_lock)
09756af4 2060{
d809e134
AS
2061 struct bpf_prog_aux *aux = prog->aux;
2062
2063 if (atomic64_dec_and_test(&aux->refcnt)) {
34ad5580 2064 /* bpf_prog_free_id() must be called first */
b16d9aa4 2065 bpf_prog_free_id(prog, do_idr_lock);
d809e134
AS
2066
2067 if (in_irq() || irqs_disabled()) {
2068 INIT_WORK(&aux->work, bpf_prog_put_deferred);
2069 schedule_work(&aux->work);
2070 } else {
2071 bpf_prog_put_deferred(&aux->work);
2072 }
a67edbf4 2073 }
09756af4 2074}
b16d9aa4
MKL
2075
2076void bpf_prog_put(struct bpf_prog *prog)
2077{
2078 __bpf_prog_put(prog, true);
2079}
e2e9b654 2080EXPORT_SYMBOL_GPL(bpf_prog_put);
09756af4
AS
2081
2082static int bpf_prog_release(struct inode *inode, struct file *filp)
2083{
2084 struct bpf_prog *prog = filp->private_data;
2085
1aacde3d 2086 bpf_prog_put(prog);
09756af4
AS
2087 return 0;
2088}
2089
61a0abae
ED
2090struct bpf_prog_kstats {
2091 u64 nsecs;
2092 u64 cnt;
2093 u64 misses;
2094};
2095
492ecee8 2096static void bpf_prog_get_stats(const struct bpf_prog *prog,
61a0abae 2097 struct bpf_prog_kstats *stats)
492ecee8 2098{
9ed9e9ba 2099 u64 nsecs = 0, cnt = 0, misses = 0;
492ecee8
AS
2100 int cpu;
2101
2102 for_each_possible_cpu(cpu) {
2103 const struct bpf_prog_stats *st;
2104 unsigned int start;
9ed9e9ba 2105 u64 tnsecs, tcnt, tmisses;
492ecee8 2106
700d4796 2107 st = per_cpu_ptr(prog->stats, cpu);
492ecee8
AS
2108 do {
2109 start = u64_stats_fetch_begin_irq(&st->syncp);
61a0abae
ED
2110 tnsecs = u64_stats_read(&st->nsecs);
2111 tcnt = u64_stats_read(&st->cnt);
2112 tmisses = u64_stats_read(&st->misses);
492ecee8
AS
2113 } while (u64_stats_fetch_retry_irq(&st->syncp, start));
2114 nsecs += tnsecs;
2115 cnt += tcnt;
9ed9e9ba 2116 misses += tmisses;
492ecee8
AS
2117 }
2118 stats->nsecs = nsecs;
2119 stats->cnt = cnt;
9ed9e9ba 2120 stats->misses = misses;
492ecee8
AS
2121}
2122
7bd509e3
DB
2123#ifdef CONFIG_PROC_FS
2124static void bpf_prog_show_fdinfo(struct seq_file *m, struct file *filp)
2125{
2126 const struct bpf_prog *prog = filp->private_data;
f1f7714e 2127 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
61a0abae 2128 struct bpf_prog_kstats stats;
7bd509e3 2129
492ecee8 2130 bpf_prog_get_stats(prog, &stats);
f1f7714e 2131 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
7bd509e3
DB
2132 seq_printf(m,
2133 "prog_type:\t%u\n"
2134 "prog_jited:\t%u\n"
f1f7714e 2135 "prog_tag:\t%s\n"
4316b409 2136 "memlock:\t%llu\n"
492ecee8
AS
2137 "prog_id:\t%u\n"
2138 "run_time_ns:\t%llu\n"
9ed9e9ba 2139 "run_cnt:\t%llu\n"
aba64c7d
DM
2140 "recursion_misses:\t%llu\n"
2141 "verified_insns:\t%u\n",
7bd509e3
DB
2142 prog->type,
2143 prog->jited,
f1f7714e 2144 prog_tag,
4316b409 2145 prog->pages * 1ULL << PAGE_SHIFT,
492ecee8
AS
2146 prog->aux->id,
2147 stats.nsecs,
9ed9e9ba 2148 stats.cnt,
aba64c7d
DM
2149 stats.misses,
2150 prog->aux->verified_insns);
7bd509e3
DB
2151}
2152#endif
2153
f66e448c 2154const struct file_operations bpf_prog_fops = {
7bd509e3
DB
2155#ifdef CONFIG_PROC_FS
2156 .show_fdinfo = bpf_prog_show_fdinfo,
2157#endif
2158 .release = bpf_prog_release,
6e71b04a
CF
2159 .read = bpf_dummy_read,
2160 .write = bpf_dummy_write,
09756af4
AS
2161};
2162
b2197755 2163int bpf_prog_new_fd(struct bpf_prog *prog)
aa79781b 2164{
afdb09c7
CF
2165 int ret;
2166
2167 ret = security_bpf_prog(prog);
2168 if (ret < 0)
2169 return ret;
2170
aa79781b
DB
2171 return anon_inode_getfd("bpf-prog", &bpf_prog_fops, prog,
2172 O_RDWR | O_CLOEXEC);
2173}
2174
113214be 2175static struct bpf_prog *____bpf_prog_get(struct fd f)
09756af4 2176{
09756af4
AS
2177 if (!f.file)
2178 return ERR_PTR(-EBADF);
09756af4
AS
2179 if (f.file->f_op != &bpf_prog_fops) {
2180 fdput(f);
2181 return ERR_PTR(-EINVAL);
2182 }
2183
c2101297 2184 return f.file->private_data;
09756af4
AS
2185}
2186
85192dbf 2187void bpf_prog_add(struct bpf_prog *prog, int i)
92117d84 2188{
85192dbf 2189 atomic64_add(i, &prog->aux->refcnt);
92117d84 2190}
59d3656d
BB
2191EXPORT_SYMBOL_GPL(bpf_prog_add);
2192
c540594f
DB
2193void bpf_prog_sub(struct bpf_prog *prog, int i)
2194{
2195 /* Only to be used for undoing previous bpf_prog_add() in some
2196 * error path. We still know that another entity in our call
2197 * path holds a reference to the program, thus atomic_sub() can
2198 * be safely used in such cases!
2199 */
85192dbf 2200 WARN_ON(atomic64_sub_return(i, &prog->aux->refcnt) == 0);
c540594f
DB
2201}
2202EXPORT_SYMBOL_GPL(bpf_prog_sub);
2203
85192dbf 2204void bpf_prog_inc(struct bpf_prog *prog)
59d3656d 2205{
85192dbf 2206 atomic64_inc(&prog->aux->refcnt);
59d3656d 2207}
97bc402d 2208EXPORT_SYMBOL_GPL(bpf_prog_inc);
92117d84 2209
b16d9aa4 2210/* prog_idr_lock should have been held */
a6f6df69 2211struct bpf_prog *bpf_prog_inc_not_zero(struct bpf_prog *prog)
b16d9aa4
MKL
2212{
2213 int refold;
2214
85192dbf 2215 refold = atomic64_fetch_add_unless(&prog->aux->refcnt, 1, 0);
b16d9aa4
MKL
2216
2217 if (!refold)
2218 return ERR_PTR(-ENOENT);
2219
2220 return prog;
2221}
a6f6df69 2222EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero);
b16d9aa4 2223
040ee692 2224bool bpf_prog_get_ok(struct bpf_prog *prog,
288b3de5 2225 enum bpf_prog_type *attach_type, bool attach_drv)
248f346f 2226{
288b3de5
JK
2227 /* not an attachment, just a refcount inc, always allow */
2228 if (!attach_type)
2229 return true;
248f346f
JK
2230
2231 if (prog->type != *attach_type)
2232 return false;
288b3de5 2233 if (bpf_prog_is_dev_bound(prog->aux) && !attach_drv)
248f346f
JK
2234 return false;
2235
2236 return true;
2237}
2238
2239static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type,
288b3de5 2240 bool attach_drv)
09756af4
AS
2241{
2242 struct fd f = fdget(ufd);
2243 struct bpf_prog *prog;
2244
113214be 2245 prog = ____bpf_prog_get(f);
09756af4
AS
2246 if (IS_ERR(prog))
2247 return prog;
288b3de5 2248 if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) {
113214be
DB
2249 prog = ERR_PTR(-EINVAL);
2250 goto out;
2251 }
09756af4 2252
85192dbf 2253 bpf_prog_inc(prog);
113214be 2254out:
09756af4
AS
2255 fdput(f);
2256 return prog;
2257}
113214be
DB
2258
2259struct bpf_prog *bpf_prog_get(u32 ufd)
2260{
288b3de5 2261 return __bpf_prog_get(ufd, NULL, false);
113214be
DB
2262}
2263
248f346f 2264struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
288b3de5 2265 bool attach_drv)
248f346f 2266{
4d220ed0 2267 return __bpf_prog_get(ufd, &type, attach_drv);
248f346f 2268}
6c8dfe21 2269EXPORT_SYMBOL_GPL(bpf_prog_get_type_dev);
248f346f 2270
aac3fc32
AI
2271/* Initially all BPF programs could be loaded w/o specifying
2272 * expected_attach_type. Later for some of them specifying expected_attach_type
2273 * at load time became required so that program could be validated properly.
2274 * Programs of types that are allowed to be loaded both w/ and w/o (for
2275 * backward compatibility) expected_attach_type, should have the default attach
2276 * type assigned to expected_attach_type for the latter case, so that it can be
2277 * validated later at attach time.
2278 *
2279 * bpf_prog_load_fixup_attach_type() sets expected_attach_type in @attr if
2280 * prog type requires it but has some attach types that have to be backward
2281 * compatible.
2282 */
2283static void bpf_prog_load_fixup_attach_type(union bpf_attr *attr)
2284{
2285 switch (attr->prog_type) {
2286 case BPF_PROG_TYPE_CGROUP_SOCK:
2287 /* Unfortunately BPF_ATTACH_TYPE_UNSPEC enumeration doesn't
2288 * exist so checking for non-zero is the way to go here.
2289 */
2290 if (!attr->expected_attach_type)
2291 attr->expected_attach_type =
2292 BPF_CGROUP_INET_SOCK_CREATE;
2293 break;
d5e4ddae
KI
2294 case BPF_PROG_TYPE_SK_REUSEPORT:
2295 if (!attr->expected_attach_type)
2296 attr->expected_attach_type =
2297 BPF_SK_REUSEPORT_SELECT;
2298 break;
aac3fc32
AI
2299 }
2300}
2301
5e43f899 2302static int
ccfe29eb
AS
2303bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
2304 enum bpf_attach_type expected_attach_type,
290248a5
AN
2305 struct btf *attach_btf, u32 btf_id,
2306 struct bpf_prog *dst_prog)
5e43f899 2307{
27ae7997 2308 if (btf_id) {
c108e3c1
AS
2309 if (btf_id > BTF_MAX_TYPE)
2310 return -EINVAL;
27ae7997 2311
290248a5
AN
2312 if (!attach_btf && !dst_prog)
2313 return -EINVAL;
2314
27ae7997
MKL
2315 switch (prog_type) {
2316 case BPF_PROG_TYPE_TRACING:
9e4e01df 2317 case BPF_PROG_TYPE_LSM:
27ae7997 2318 case BPF_PROG_TYPE_STRUCT_OPS:
be8704ff 2319 case BPF_PROG_TYPE_EXT:
27ae7997
MKL
2320 break;
2321 default:
c108e3c1 2322 return -EINVAL;
27ae7997 2323 }
c108e3c1
AS
2324 }
2325
290248a5
AN
2326 if (attach_btf && (!btf_id || dst_prog))
2327 return -EINVAL;
2328
2329 if (dst_prog && prog_type != BPF_PROG_TYPE_TRACING &&
be8704ff 2330 prog_type != BPF_PROG_TYPE_EXT)
27ae7997
MKL
2331 return -EINVAL;
2332
4fbac77d 2333 switch (prog_type) {
aac3fc32
AI
2334 case BPF_PROG_TYPE_CGROUP_SOCK:
2335 switch (expected_attach_type) {
2336 case BPF_CGROUP_INET_SOCK_CREATE:
f5836749 2337 case BPF_CGROUP_INET_SOCK_RELEASE:
aac3fc32
AI
2338 case BPF_CGROUP_INET4_POST_BIND:
2339 case BPF_CGROUP_INET6_POST_BIND:
2340 return 0;
2341 default:
2342 return -EINVAL;
2343 }
4fbac77d
AI
2344 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2345 switch (expected_attach_type) {
2346 case BPF_CGROUP_INET4_BIND:
2347 case BPF_CGROUP_INET6_BIND:
d74bad4e
AI
2348 case BPF_CGROUP_INET4_CONNECT:
2349 case BPF_CGROUP_INET6_CONNECT:
1b66d253
DB
2350 case BPF_CGROUP_INET4_GETPEERNAME:
2351 case BPF_CGROUP_INET6_GETPEERNAME:
2352 case BPF_CGROUP_INET4_GETSOCKNAME:
2353 case BPF_CGROUP_INET6_GETSOCKNAME:
1cedee13
AI
2354 case BPF_CGROUP_UDP4_SENDMSG:
2355 case BPF_CGROUP_UDP6_SENDMSG:
983695fa
DB
2356 case BPF_CGROUP_UDP4_RECVMSG:
2357 case BPF_CGROUP_UDP6_RECVMSG:
4fbac77d
AI
2358 return 0;
2359 default:
2360 return -EINVAL;
2361 }
5cf1e914 2362 case BPF_PROG_TYPE_CGROUP_SKB:
2363 switch (expected_attach_type) {
2364 case BPF_CGROUP_INET_INGRESS:
2365 case BPF_CGROUP_INET_EGRESS:
2366 return 0;
2367 default:
2368 return -EINVAL;
2369 }
0d01da6a
SF
2370 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2371 switch (expected_attach_type) {
2372 case BPF_CGROUP_SETSOCKOPT:
2373 case BPF_CGROUP_GETSOCKOPT:
2374 return 0;
2375 default:
2376 return -EINVAL;
2377 }
e9ddbb77
JS
2378 case BPF_PROG_TYPE_SK_LOOKUP:
2379 if (expected_attach_type == BPF_SK_LOOKUP)
2380 return 0;
2381 return -EINVAL;
d5e4ddae
KI
2382 case BPF_PROG_TYPE_SK_REUSEPORT:
2383 switch (expected_attach_type) {
2384 case BPF_SK_REUSEPORT_SELECT:
2385 case BPF_SK_REUSEPORT_SELECT_OR_MIGRATE:
2386 return 0;
2387 default:
2388 return -EINVAL;
2389 }
79a7f8bd 2390 case BPF_PROG_TYPE_SYSCALL:
be8704ff
AS
2391 case BPF_PROG_TYPE_EXT:
2392 if (expected_attach_type)
2393 return -EINVAL;
df561f66 2394 fallthrough;
4fbac77d
AI
2395 default:
2396 return 0;
2397 }
5e43f899
AI
2398}
2399
2c78ee89
AS
2400static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
2401{
2402 switch (prog_type) {
2403 case BPF_PROG_TYPE_SCHED_CLS:
2404 case BPF_PROG_TYPE_SCHED_ACT:
2405 case BPF_PROG_TYPE_XDP:
2406 case BPF_PROG_TYPE_LWT_IN:
2407 case BPF_PROG_TYPE_LWT_OUT:
2408 case BPF_PROG_TYPE_LWT_XMIT:
2409 case BPF_PROG_TYPE_LWT_SEG6LOCAL:
2410 case BPF_PROG_TYPE_SK_SKB:
2411 case BPF_PROG_TYPE_SK_MSG:
2412 case BPF_PROG_TYPE_LIRC_MODE2:
2413 case BPF_PROG_TYPE_FLOW_DISSECTOR:
2414 case BPF_PROG_TYPE_CGROUP_DEVICE:
2415 case BPF_PROG_TYPE_CGROUP_SOCK:
2416 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
2417 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
2418 case BPF_PROG_TYPE_CGROUP_SYSCTL:
2419 case BPF_PROG_TYPE_SOCK_OPS:
2420 case BPF_PROG_TYPE_EXT: /* extends any prog */
2421 return true;
2422 case BPF_PROG_TYPE_CGROUP_SKB:
2423 /* always unpriv */
2424 case BPF_PROG_TYPE_SK_REUSEPORT:
2425 /* equivalent to SOCKET_FILTER. need CAP_BPF only */
2426 default:
2427 return false;
2428 }
2429}
2430
2431static bool is_perfmon_prog_type(enum bpf_prog_type prog_type)
2432{
2433 switch (prog_type) {
2434 case BPF_PROG_TYPE_KPROBE:
2435 case BPF_PROG_TYPE_TRACEPOINT:
2436 case BPF_PROG_TYPE_PERF_EVENT:
2437 case BPF_PROG_TYPE_RAW_TRACEPOINT:
2438 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
2439 case BPF_PROG_TYPE_TRACING:
2440 case BPF_PROG_TYPE_LSM:
2441 case BPF_PROG_TYPE_STRUCT_OPS: /* has access to struct sock */
2442 case BPF_PROG_TYPE_EXT: /* extends any prog */
2443 return true;
2444 default:
2445 return false;
2446 }
2447}
2448
09756af4 2449/* last field in 'union bpf_attr' used by this command */
fbd94c7a 2450#define BPF_PROG_LOAD_LAST_FIELD core_relo_rec_size
09756af4 2451
af2ac3e1 2452static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr)
09756af4
AS
2453{
2454 enum bpf_prog_type type = attr->prog_type;
290248a5
AN
2455 struct bpf_prog *prog, *dst_prog = NULL;
2456 struct btf *attach_btf = NULL;
09756af4
AS
2457 int err;
2458 char license[128];
2459 bool is_gpl;
2460
2461 if (CHECK_ATTR(BPF_PROG_LOAD))
2462 return -EINVAL;
2463
c240eff6
JW
2464 if (attr->prog_flags & ~(BPF_F_STRICT_ALIGNMENT |
2465 BPF_F_ANY_ALIGNMENT |
10d274e8 2466 BPF_F_TEST_STATE_FREQ |
1e6c62a8 2467 BPF_F_SLEEPABLE |
c2f2cdbe
LB
2468 BPF_F_TEST_RND_HI32 |
2469 BPF_F_XDP_HAS_FRAGS))
e07b98d9
DM
2470 return -EINVAL;
2471
e9ee9efc
DM
2472 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) &&
2473 (attr->prog_flags & BPF_F_ANY_ALIGNMENT) &&
2c78ee89 2474 !bpf_capable())
e9ee9efc
DM
2475 return -EPERM;
2476
09756af4 2477 /* copy eBPF program license from user space */
af2ac3e1
AS
2478 if (strncpy_from_bpfptr(license,
2479 make_bpfptr(attr->license, uattr.is_kernel),
2480 sizeof(license) - 1) < 0)
09756af4
AS
2481 return -EFAULT;
2482 license[sizeof(license) - 1] = 0;
2483
2484 /* eBPF programs must be GPL compatible to use GPL-ed functions */
2485 is_gpl = license_is_gpl_compatible(license);
2486
c04c0d2b 2487 if (attr->insn_cnt == 0 ||
2c78ee89 2488 attr->insn_cnt > (bpf_capable() ? BPF_COMPLEXITY_LIMIT_INSNS : BPF_MAXINSNS))
ef0915ca 2489 return -E2BIG;
80b7d819
CF
2490 if (type != BPF_PROG_TYPE_SOCKET_FILTER &&
2491 type != BPF_PROG_TYPE_CGROUP_SKB &&
2c78ee89
AS
2492 !bpf_capable())
2493 return -EPERM;
2494
b338cb92 2495 if (is_net_admin_prog_type(type) && !capable(CAP_NET_ADMIN) && !capable(CAP_SYS_ADMIN))
2c78ee89
AS
2496 return -EPERM;
2497 if (is_perfmon_prog_type(type) && !perfmon_capable())
1be7f75d
AS
2498 return -EPERM;
2499
290248a5
AN
2500 /* attach_prog_fd/attach_btf_obj_fd can specify fd of either bpf_prog
2501 * or btf, we need to check which one it is
2502 */
2503 if (attr->attach_prog_fd) {
2504 dst_prog = bpf_prog_get(attr->attach_prog_fd);
2505 if (IS_ERR(dst_prog)) {
2506 dst_prog = NULL;
2507 attach_btf = btf_get_by_fd(attr->attach_btf_obj_fd);
2508 if (IS_ERR(attach_btf))
2509 return -EINVAL;
2510 if (!btf_is_kernel(attach_btf)) {
8bdd8e27
AN
2511 /* attaching through specifying bpf_prog's BTF
2512 * objects directly might be supported eventually
2513 */
290248a5 2514 btf_put(attach_btf);
8bdd8e27 2515 return -ENOTSUPP;
290248a5
AN
2516 }
2517 }
2518 } else if (attr->attach_btf_id) {
2519 /* fall back to vmlinux BTF, if BTF type ID is specified */
2520 attach_btf = bpf_get_btf_vmlinux();
2521 if (IS_ERR(attach_btf))
2522 return PTR_ERR(attach_btf);
2523 if (!attach_btf)
2524 return -EINVAL;
2525 btf_get(attach_btf);
2526 }
2527
aac3fc32 2528 bpf_prog_load_fixup_attach_type(attr);
ccfe29eb 2529 if (bpf_prog_load_check_attach(type, attr->expected_attach_type,
290248a5
AN
2530 attach_btf, attr->attach_btf_id,
2531 dst_prog)) {
2532 if (dst_prog)
2533 bpf_prog_put(dst_prog);
2534 if (attach_btf)
2535 btf_put(attach_btf);
5e43f899 2536 return -EINVAL;
290248a5 2537 }
5e43f899 2538
09756af4
AS
2539 /* plain bpf_prog allocation */
2540 prog = bpf_prog_alloc(bpf_prog_size(attr->insn_cnt), GFP_USER);
290248a5
AN
2541 if (!prog) {
2542 if (dst_prog)
2543 bpf_prog_put(dst_prog);
2544 if (attach_btf)
2545 btf_put(attach_btf);
09756af4 2546 return -ENOMEM;
290248a5 2547 }
09756af4 2548
5e43f899 2549 prog->expected_attach_type = attr->expected_attach_type;
290248a5 2550 prog->aux->attach_btf = attach_btf;
ccfe29eb 2551 prog->aux->attach_btf_id = attr->attach_btf_id;
290248a5 2552 prog->aux->dst_prog = dst_prog;
9a18eedb 2553 prog->aux->offload_requested = !!attr->prog_ifindex;
1e6c62a8 2554 prog->aux->sleepable = attr->prog_flags & BPF_F_SLEEPABLE;
c2f2cdbe 2555 prog->aux->xdp_has_frags = attr->prog_flags & BPF_F_XDP_HAS_FRAGS;
9a18eedb 2556
afdb09c7 2557 err = security_bpf_prog_alloc(prog->aux);
aaac3ba9 2558 if (err)
3ac1f01b 2559 goto free_prog;
afdb09c7 2560
3ac1f01b 2561 prog->aux->user = get_current_user();
09756af4
AS
2562 prog->len = attr->insn_cnt;
2563
2564 err = -EFAULT;
af2ac3e1
AS
2565 if (copy_from_bpfptr(prog->insns,
2566 make_bpfptr(attr->insns, uattr.is_kernel),
2567 bpf_prog_insn_size(prog)) != 0)
3ac1f01b 2568 goto free_prog_sec;
09756af4
AS
2569
2570 prog->orig_prog = NULL;
a91263d5 2571 prog->jited = 0;
09756af4 2572
85192dbf 2573 atomic64_set(&prog->aux->refcnt, 1);
a91263d5 2574 prog->gpl_compatible = is_gpl ? 1 : 0;
09756af4 2575
9a18eedb 2576 if (bpf_prog_is_dev_bound(prog->aux)) {
ab3f0063
JK
2577 err = bpf_prog_offload_init(prog, attr);
2578 if (err)
3ac1f01b 2579 goto free_prog_sec;
ab3f0063
JK
2580 }
2581
09756af4
AS
2582 /* find program type: socket_filter vs tracing_filter */
2583 err = find_prog_type(type, prog);
2584 if (err < 0)
3ac1f01b 2585 goto free_prog_sec;
09756af4 2586
9285ec4c 2587 prog->aux->load_time = ktime_get_boottime_ns();
8e7ae251
MKL
2588 err = bpf_obj_name_cpy(prog->aux->name, attr->prog_name,
2589 sizeof(attr->prog_name));
2590 if (err < 0)
3ac1f01b 2591 goto free_prog_sec;
cb4d2b3f 2592
09756af4 2593 /* run eBPF verifier */
838e9690 2594 err = bpf_check(&prog, attr, uattr);
09756af4
AS
2595 if (err < 0)
2596 goto free_used_maps;
2597
9facc336 2598 prog = bpf_prog_select_runtime(prog, &err);
04fd61ab
AS
2599 if (err < 0)
2600 goto free_used_maps;
09756af4 2601
dc4bb0e2
MKL
2602 err = bpf_prog_alloc_id(prog);
2603 if (err)
2604 goto free_used_maps;
2605
c751798a
DB
2606 /* Upon success of bpf_prog_alloc_id(), the BPF prog is
2607 * effectively publicly exposed. However, retrieving via
2608 * bpf_prog_get_fd_by_id() will take another reference,
2609 * therefore it cannot be gone underneath us.
2610 *
2611 * Only for the time /after/ successful bpf_prog_new_fd()
2612 * and before returning to userspace, we might just hold
2613 * one reference and any parallel close on that fd could
2614 * rip everything out. Hence, below notifications must
2615 * happen before bpf_prog_new_fd().
2616 *
2617 * Also, any failure handling from this point onwards must
2618 * be using bpf_prog_put() given the program is exposed.
2619 */
74451e66 2620 bpf_prog_kallsyms_add(prog);
6ee52e2a 2621 perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0);
bae141f5 2622 bpf_audit_prog(prog, BPF_AUDIT_LOAD);
c751798a
DB
2623
2624 err = bpf_prog_new_fd(prog);
2625 if (err < 0)
2626 bpf_prog_put(prog);
09756af4
AS
2627 return err;
2628
2629free_used_maps:
cd7455f1
DB
2630 /* In case we have subprogs, we need to wait for a grace
2631 * period before we can tear down JIT memory since symbols
2632 * are already exposed under kallsyms.
2633 */
2634 __bpf_prog_put_noref(prog, prog->aux->func_cnt);
2635 return err;
afdb09c7 2636free_prog_sec:
3ac1f01b 2637 free_uid(prog->aux->user);
afdb09c7 2638 security_bpf_prog_free(prog->aux);
3ac1f01b 2639free_prog:
22dc4a0f
AN
2640 if (prog->aux->attach_btf)
2641 btf_put(prog->aux->attach_btf);
09756af4
AS
2642 bpf_prog_free(prog);
2643 return err;
2644}
2645
6e71b04a 2646#define BPF_OBJ_LAST_FIELD file_flags
b2197755
DB
2647
2648static int bpf_obj_pin(const union bpf_attr *attr)
2649{
6e71b04a 2650 if (CHECK_ATTR(BPF_OBJ) || attr->file_flags != 0)
b2197755
DB
2651 return -EINVAL;
2652
535e7b4b 2653 return bpf_obj_pin_user(attr->bpf_fd, u64_to_user_ptr(attr->pathname));
b2197755
DB
2654}
2655
2656static int bpf_obj_get(const union bpf_attr *attr)
2657{
6e71b04a
CF
2658 if (CHECK_ATTR(BPF_OBJ) || attr->bpf_fd != 0 ||
2659 attr->file_flags & ~BPF_OBJ_FLAG_MASK)
b2197755
DB
2660 return -EINVAL;
2661
6e71b04a
CF
2662 return bpf_obj_get_user(u64_to_user_ptr(attr->pathname),
2663 attr->file_flags);
b2197755
DB
2664}
2665
f2e10bff 2666void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
a3b80e10 2667 const struct bpf_link_ops *ops, struct bpf_prog *prog)
fec56f58 2668{
70ed506c 2669 atomic64_set(&link->refcnt, 1);
f2e10bff 2670 link->type = type;
a3b80e10 2671 link->id = 0;
70ed506c
AN
2672 link->ops = ops;
2673 link->prog = prog;
2674}
2675
a3b80e10
AN
2676static void bpf_link_free_id(int id)
2677{
2678 if (!id)
2679 return;
2680
2681 spin_lock_bh(&link_idr_lock);
2682 idr_remove(&link_idr, id);
2683 spin_unlock_bh(&link_idr_lock);
2684}
2685
98868668
AN
2686/* Clean up bpf_link and corresponding anon_inode file and FD. After
2687 * anon_inode is created, bpf_link can't be just kfree()'d due to deferred
a3b80e10
AN
2688 * anon_inode's release() call. This helper marksbpf_link as
2689 * defunct, releases anon_inode file and puts reserved FD. bpf_prog's refcnt
2690 * is not decremented, it's the responsibility of a calling code that failed
2691 * to complete bpf_link initialization.
98868668 2692 */
a3b80e10 2693void bpf_link_cleanup(struct bpf_link_primer *primer)
babf3164 2694{
a3b80e10
AN
2695 primer->link->prog = NULL;
2696 bpf_link_free_id(primer->id);
2697 fput(primer->file);
2698 put_unused_fd(primer->fd);
babf3164
AN
2699}
2700
70ed506c
AN
2701void bpf_link_inc(struct bpf_link *link)
2702{
2703 atomic64_inc(&link->refcnt);
2704}
2705
2706/* bpf_link_free is guaranteed to be called from process context */
2707static void bpf_link_free(struct bpf_link *link)
2708{
a3b80e10 2709 bpf_link_free_id(link->id);
babf3164
AN
2710 if (link->prog) {
2711 /* detach BPF program, clean up used resources */
2712 link->ops->release(link);
2713 bpf_prog_put(link->prog);
2714 }
2715 /* free bpf_link and its containing memory */
2716 link->ops->dealloc(link);
70ed506c
AN
2717}
2718
2719static void bpf_link_put_deferred(struct work_struct *work)
2720{
2721 struct bpf_link *link = container_of(work, struct bpf_link, work);
2722
2723 bpf_link_free(link);
2724}
2725
2726/* bpf_link_put can be called from atomic context, but ensures that resources
2727 * are freed from process context
2728 */
2729void bpf_link_put(struct bpf_link *link)
2730{
2731 if (!atomic64_dec_and_test(&link->refcnt))
2732 return;
2733
f00f2f7f
AS
2734 if (in_atomic()) {
2735 INIT_WORK(&link->work, bpf_link_put_deferred);
2736 schedule_work(&link->work);
2737 } else {
2738 bpf_link_free(link);
2739 }
70ed506c 2740}
cb80ddc6 2741EXPORT_SYMBOL(bpf_link_put);
70ed506c
AN
2742
2743static int bpf_link_release(struct inode *inode, struct file *filp)
2744{
2745 struct bpf_link *link = filp->private_data;
2746
2747 bpf_link_put(link);
fec56f58
AS
2748 return 0;
2749}
2750
70ed506c 2751#ifdef CONFIG_PROC_FS
f2e10bff
AN
2752#define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type)
2753#define BPF_MAP_TYPE(_id, _ops)
2754#define BPF_LINK_TYPE(_id, _name) [_id] = #_name,
2755static const char *bpf_link_type_strs[] = {
2756 [BPF_LINK_TYPE_UNSPEC] = "<invalid>",
2757#include <linux/bpf_types.h>
2758};
2759#undef BPF_PROG_TYPE
2760#undef BPF_MAP_TYPE
2761#undef BPF_LINK_TYPE
70ed506c
AN
2762
2763static void bpf_link_show_fdinfo(struct seq_file *m, struct file *filp)
2764{
2765 const struct bpf_link *link = filp->private_data;
2766 const struct bpf_prog *prog = link->prog;
2767 char prog_tag[sizeof(prog->tag) * 2 + 1] = { };
70ed506c
AN
2768
2769 bin2hex(prog_tag, prog->tag, sizeof(prog->tag));
2770 seq_printf(m,
2771 "link_type:\t%s\n"
a3b80e10 2772 "link_id:\t%u\n"
70ed506c
AN
2773 "prog_tag:\t%s\n"
2774 "prog_id:\t%u\n",
f2e10bff 2775 bpf_link_type_strs[link->type],
a3b80e10 2776 link->id,
70ed506c
AN
2777 prog_tag,
2778 prog->aux->id);
f2e10bff
AN
2779 if (link->ops->show_fdinfo)
2780 link->ops->show_fdinfo(link, m);
70ed506c
AN
2781}
2782#endif
2783
6f302bfb 2784static const struct file_operations bpf_link_fops = {
70ed506c
AN
2785#ifdef CONFIG_PROC_FS
2786 .show_fdinfo = bpf_link_show_fdinfo,
2787#endif
2788 .release = bpf_link_release,
fec56f58
AS
2789 .read = bpf_dummy_read,
2790 .write = bpf_dummy_write,
2791};
2792
a3b80e10 2793static int bpf_link_alloc_id(struct bpf_link *link)
70ed506c 2794{
a3b80e10 2795 int id;
70ed506c 2796
a3b80e10
AN
2797 idr_preload(GFP_KERNEL);
2798 spin_lock_bh(&link_idr_lock);
2799 id = idr_alloc_cyclic(&link_idr, link, 1, INT_MAX, GFP_ATOMIC);
2800 spin_unlock_bh(&link_idr_lock);
2801 idr_preload_end();
70ed506c 2802
a3b80e10
AN
2803 return id;
2804}
2805
2806/* Prepare bpf_link to be exposed to user-space by allocating anon_inode file,
2807 * reserving unused FD and allocating ID from link_idr. This is to be paired
2808 * with bpf_link_settle() to install FD and ID and expose bpf_link to
2809 * user-space, if bpf_link is successfully attached. If not, bpf_link and
2810 * pre-allocated resources are to be freed with bpf_cleanup() call. All the
2811 * transient state is passed around in struct bpf_link_primer.
2812 * This is preferred way to create and initialize bpf_link, especially when
c561d110 2813 * there are complicated and expensive operations in between creating bpf_link
a3b80e10
AN
2814 * itself and attaching it to BPF hook. By using bpf_link_prime() and
2815 * bpf_link_settle() kernel code using bpf_link doesn't have to perform
2816 * expensive (and potentially failing) roll back operations in a rare case
2817 * that file, FD, or ID can't be allocated.
babf3164 2818 */
a3b80e10 2819int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer)
babf3164
AN
2820{
2821 struct file *file;
a3b80e10 2822 int fd, id;
babf3164
AN
2823
2824 fd = get_unused_fd_flags(O_CLOEXEC);
2825 if (fd < 0)
a3b80e10 2826 return fd;
babf3164 2827
babf3164 2828
a3b80e10
AN
2829 id = bpf_link_alloc_id(link);
2830 if (id < 0) {
2831 put_unused_fd(fd);
a3b80e10
AN
2832 return id;
2833 }
babf3164
AN
2834
2835 file = anon_inode_getfile("bpf_link", &bpf_link_fops, link, O_CLOEXEC);
2836 if (IS_ERR(file)) {
138c6767 2837 bpf_link_free_id(id);
babf3164 2838 put_unused_fd(fd);
138c6767 2839 return PTR_ERR(file);
babf3164
AN
2840 }
2841
a3b80e10
AN
2842 primer->link = link;
2843 primer->file = file;
2844 primer->fd = fd;
2845 primer->id = id;
2846 return 0;
2847}
2848
2849int bpf_link_settle(struct bpf_link_primer *primer)
2850{
2851 /* make bpf_link fetchable by ID */
2852 spin_lock_bh(&link_idr_lock);
2853 primer->link->id = primer->id;
2854 spin_unlock_bh(&link_idr_lock);
2855 /* make bpf_link fetchable by FD */
2856 fd_install(primer->fd, primer->file);
2857 /* pass through installed FD */
2858 return primer->fd;
2859}
2860
2861int bpf_link_new_fd(struct bpf_link *link)
2862{
2863 return anon_inode_getfd("bpf-link", &bpf_link_fops, link, O_CLOEXEC);
babf3164
AN
2864}
2865
70ed506c
AN
2866struct bpf_link *bpf_link_get_from_fd(u32 ufd)
2867{
2868 struct fd f = fdget(ufd);
2869 struct bpf_link *link;
2870
2871 if (!f.file)
2872 return ERR_PTR(-EBADF);
2873 if (f.file->f_op != &bpf_link_fops) {
2874 fdput(f);
2875 return ERR_PTR(-EINVAL);
2876 }
2877
2878 link = f.file->private_data;
2879 bpf_link_inc(link);
2880 fdput(f);
2881
2882 return link;
2883}
cb80ddc6 2884EXPORT_SYMBOL(bpf_link_get_from_fd);
70ed506c 2885
70ed506c 2886static void bpf_tracing_link_release(struct bpf_link *link)
babf3164 2887{
3aac1ead 2888 struct bpf_tracing_link *tr_link =
f7e0beaf 2889 container_of(link, struct bpf_tracing_link, link.link);
3aac1ead 2890
f7e0beaf 2891 WARN_ON_ONCE(bpf_trampoline_unlink_prog(&tr_link->link,
3aac1ead
THJ
2892 tr_link->trampoline));
2893
2894 bpf_trampoline_put(tr_link->trampoline);
2895
2896 /* tgt_prog is NULL if target is a kernel function */
2897 if (tr_link->tgt_prog)
2898 bpf_prog_put(tr_link->tgt_prog);
babf3164
AN
2899}
2900
2901static void bpf_tracing_link_dealloc(struct bpf_link *link)
70ed506c
AN
2902{
2903 struct bpf_tracing_link *tr_link =
f7e0beaf 2904 container_of(link, struct bpf_tracing_link, link.link);
70ed506c 2905
70ed506c
AN
2906 kfree(tr_link);
2907}
2908
f2e10bff
AN
2909static void bpf_tracing_link_show_fdinfo(const struct bpf_link *link,
2910 struct seq_file *seq)
2911{
2912 struct bpf_tracing_link *tr_link =
f7e0beaf 2913 container_of(link, struct bpf_tracing_link, link.link);
f2e10bff
AN
2914
2915 seq_printf(seq,
2916 "attach_type:\t%d\n",
2917 tr_link->attach_type);
2918}
2919
2920static int bpf_tracing_link_fill_link_info(const struct bpf_link *link,
2921 struct bpf_link_info *info)
2922{
2923 struct bpf_tracing_link *tr_link =
f7e0beaf 2924 container_of(link, struct bpf_tracing_link, link.link);
f2e10bff
AN
2925
2926 info->tracing.attach_type = tr_link->attach_type;
441e8c66
THJ
2927 bpf_trampoline_unpack_key(tr_link->trampoline->key,
2928 &info->tracing.target_obj_id,
2929 &info->tracing.target_btf_id);
f2e10bff
AN
2930
2931 return 0;
2932}
2933
70ed506c
AN
2934static const struct bpf_link_ops bpf_tracing_link_lops = {
2935 .release = bpf_tracing_link_release,
babf3164 2936 .dealloc = bpf_tracing_link_dealloc,
f2e10bff
AN
2937 .show_fdinfo = bpf_tracing_link_show_fdinfo,
2938 .fill_link_info = bpf_tracing_link_fill_link_info,
70ed506c
AN
2939};
2940
4a1e7c0c
THJ
2941static int bpf_tracing_prog_attach(struct bpf_prog *prog,
2942 int tgt_prog_fd,
2fcc8241
KFL
2943 u32 btf_id,
2944 u64 bpf_cookie)
fec56f58 2945{
a3b80e10 2946 struct bpf_link_primer link_primer;
3aac1ead 2947 struct bpf_prog *tgt_prog = NULL;
4a1e7c0c 2948 struct bpf_trampoline *tr = NULL;
70ed506c 2949 struct bpf_tracing_link *link;
4a1e7c0c 2950 u64 key = 0;
a3b80e10 2951 int err;
fec56f58 2952
9e4e01df
KS
2953 switch (prog->type) {
2954 case BPF_PROG_TYPE_TRACING:
2955 if (prog->expected_attach_type != BPF_TRACE_FENTRY &&
2956 prog->expected_attach_type != BPF_TRACE_FEXIT &&
2957 prog->expected_attach_type != BPF_MODIFY_RETURN) {
2958 err = -EINVAL;
2959 goto out_put_prog;
2960 }
2961 break;
2962 case BPF_PROG_TYPE_EXT:
2963 if (prog->expected_attach_type != 0) {
2964 err = -EINVAL;
2965 goto out_put_prog;
2966 }
2967 break;
2968 case BPF_PROG_TYPE_LSM:
2969 if (prog->expected_attach_type != BPF_LSM_MAC) {
2970 err = -EINVAL;
2971 goto out_put_prog;
2972 }
2973 break;
2974 default:
fec56f58
AS
2975 err = -EINVAL;
2976 goto out_put_prog;
2977 }
2978
4a1e7c0c
THJ
2979 if (!!tgt_prog_fd != !!btf_id) {
2980 err = -EINVAL;
2981 goto out_put_prog;
2982 }
2983
2984 if (tgt_prog_fd) {
2985 /* For now we only allow new targets for BPF_PROG_TYPE_EXT */
2986 if (prog->type != BPF_PROG_TYPE_EXT) {
2987 err = -EINVAL;
2988 goto out_put_prog;
2989 }
2990
2991 tgt_prog = bpf_prog_get(tgt_prog_fd);
2992 if (IS_ERR(tgt_prog)) {
2993 err = PTR_ERR(tgt_prog);
2994 tgt_prog = NULL;
2995 goto out_put_prog;
2996 }
2997
22dc4a0f 2998 key = bpf_trampoline_compute_key(tgt_prog, NULL, btf_id);
4a1e7c0c
THJ
2999 }
3000
70ed506c
AN
3001 link = kzalloc(sizeof(*link), GFP_USER);
3002 if (!link) {
3003 err = -ENOMEM;
3004 goto out_put_prog;
3005 }
f7e0beaf 3006 bpf_link_init(&link->link.link, BPF_LINK_TYPE_TRACING,
f2e10bff
AN
3007 &bpf_tracing_link_lops, prog);
3008 link->attach_type = prog->expected_attach_type;
2fcc8241 3009 link->link.cookie = bpf_cookie;
70ed506c 3010
3aac1ead
THJ
3011 mutex_lock(&prog->aux->dst_mutex);
3012
4a1e7c0c
THJ
3013 /* There are a few possible cases here:
3014 *
3015 * - if prog->aux->dst_trampoline is set, the program was just loaded
3016 * and not yet attached to anything, so we can use the values stored
3017 * in prog->aux
3018 *
3019 * - if prog->aux->dst_trampoline is NULL, the program has already been
3020 * attached to a target and its initial target was cleared (below)
3021 *
3022 * - if tgt_prog != NULL, the caller specified tgt_prog_fd +
3023 * target_btf_id using the link_create API.
3024 *
3025 * - if tgt_prog == NULL when this function was called using the old
f3a95075
JO
3026 * raw_tracepoint_open API, and we need a target from prog->aux
3027 *
3028 * - if prog->aux->dst_trampoline and tgt_prog is NULL, the program
3029 * was detached and is going for re-attachment.
4a1e7c0c
THJ
3030 */
3031 if (!prog->aux->dst_trampoline && !tgt_prog) {
f3a95075
JO
3032 /*
3033 * Allow re-attach for TRACING and LSM programs. If it's
3034 * currently linked, bpf_trampoline_link_prog will fail.
3035 * EXT programs need to specify tgt_prog_fd, so they
3036 * re-attach in separate code path.
3037 */
3038 if (prog->type != BPF_PROG_TYPE_TRACING &&
3039 prog->type != BPF_PROG_TYPE_LSM) {
3040 err = -EINVAL;
3041 goto out_unlock;
3042 }
3043 btf_id = prog->aux->attach_btf_id;
3044 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, btf_id);
babf3164 3045 }
4a1e7c0c
THJ
3046
3047 if (!prog->aux->dst_trampoline ||
3048 (key && key != prog->aux->dst_trampoline->key)) {
3049 /* If there is no saved target, or the specified target is
3050 * different from the destination specified at load time, we
3051 * need a new trampoline and a check for compatibility
3052 */
3053 struct bpf_attach_target_info tgt_info = {};
3054
3055 err = bpf_check_attach_target(NULL, prog, tgt_prog, btf_id,
3056 &tgt_info);
3057 if (err)
3058 goto out_unlock;
3059
3060 tr = bpf_trampoline_get(key, &tgt_info);
3061 if (!tr) {
3062 err = -ENOMEM;
3063 goto out_unlock;
3064 }
3065 } else {
3066 /* The caller didn't specify a target, or the target was the
3067 * same as the destination supplied during program load. This
3068 * means we can reuse the trampoline and reference from program
3069 * load time, and there is no need to allocate a new one. This
3070 * can only happen once for any program, as the saved values in
3071 * prog->aux are cleared below.
3072 */
3073 tr = prog->aux->dst_trampoline;
3074 tgt_prog = prog->aux->dst_prog;
3075 }
3aac1ead 3076
f7e0beaf 3077 err = bpf_link_prime(&link->link.link, &link_primer);
3aac1ead
THJ
3078 if (err)
3079 goto out_unlock;
fec56f58 3080
f7e0beaf 3081 err = bpf_trampoline_link_prog(&link->link, tr);
babf3164 3082 if (err) {
a3b80e10 3083 bpf_link_cleanup(&link_primer);
3aac1ead
THJ
3084 link = NULL;
3085 goto out_unlock;
fec56f58 3086 }
babf3164 3087
3aac1ead
THJ
3088 link->tgt_prog = tgt_prog;
3089 link->trampoline = tr;
3090
4a1e7c0c
THJ
3091 /* Always clear the trampoline and target prog from prog->aux to make
3092 * sure the original attach destination is not kept alive after a
3093 * program is (re-)attached to another target.
3094 */
3095 if (prog->aux->dst_prog &&
3096 (tgt_prog_fd || tr != prog->aux->dst_trampoline))
3097 /* got extra prog ref from syscall, or attaching to different prog */
3098 bpf_prog_put(prog->aux->dst_prog);
3099 if (prog->aux->dst_trampoline && tr != prog->aux->dst_trampoline)
3100 /* we allocated a new trampoline, so free the old one */
3101 bpf_trampoline_put(prog->aux->dst_trampoline);
3102
3aac1ead
THJ
3103 prog->aux->dst_prog = NULL;
3104 prog->aux->dst_trampoline = NULL;
3105 mutex_unlock(&prog->aux->dst_mutex);
3106
a3b80e10 3107 return bpf_link_settle(&link_primer);
3aac1ead 3108out_unlock:
4a1e7c0c
THJ
3109 if (tr && tr != prog->aux->dst_trampoline)
3110 bpf_trampoline_put(tr);
3aac1ead
THJ
3111 mutex_unlock(&prog->aux->dst_mutex);
3112 kfree(link);
fec56f58 3113out_put_prog:
4a1e7c0c
THJ
3114 if (tgt_prog_fd && tgt_prog)
3115 bpf_prog_put(tgt_prog);
fec56f58
AS
3116 return err;
3117}
3118
70ed506c
AN
3119struct bpf_raw_tp_link {
3120 struct bpf_link link;
c4f6699d 3121 struct bpf_raw_event_map *btp;
c4f6699d
AS
3122};
3123
70ed506c 3124static void bpf_raw_tp_link_release(struct bpf_link *link)
c4f6699d 3125{
70ed506c
AN
3126 struct bpf_raw_tp_link *raw_tp =
3127 container_of(link, struct bpf_raw_tp_link, link);
c4f6699d 3128
70ed506c 3129 bpf_probe_unregister(raw_tp->btp, raw_tp->link.prog);
a38d1107 3130 bpf_put_raw_tracepoint(raw_tp->btp);
babf3164
AN
3131}
3132
3133static void bpf_raw_tp_link_dealloc(struct bpf_link *link)
3134{
3135 struct bpf_raw_tp_link *raw_tp =
3136 container_of(link, struct bpf_raw_tp_link, link);
3137
c4f6699d 3138 kfree(raw_tp);
c4f6699d
AS
3139}
3140
f2e10bff
AN
3141static void bpf_raw_tp_link_show_fdinfo(const struct bpf_link *link,
3142 struct seq_file *seq)
3143{
3144 struct bpf_raw_tp_link *raw_tp_link =
3145 container_of(link, struct bpf_raw_tp_link, link);
3146
3147 seq_printf(seq,
3148 "tp_name:\t%s\n",
3149 raw_tp_link->btp->tp->name);
3150}
3151
3152static int bpf_raw_tp_link_fill_link_info(const struct bpf_link *link,
3153 struct bpf_link_info *info)
3154{
3155 struct bpf_raw_tp_link *raw_tp_link =
3156 container_of(link, struct bpf_raw_tp_link, link);
3157 char __user *ubuf = u64_to_user_ptr(info->raw_tracepoint.tp_name);
3158 const char *tp_name = raw_tp_link->btp->tp->name;
3159 u32 ulen = info->raw_tracepoint.tp_name_len;
3160 size_t tp_len = strlen(tp_name);
3161
b474959d 3162 if (!ulen ^ !ubuf)
f2e10bff
AN
3163 return -EINVAL;
3164
3165 info->raw_tracepoint.tp_name_len = tp_len + 1;
3166
3167 if (!ubuf)
3168 return 0;
3169
3170 if (ulen >= tp_len + 1) {
3171 if (copy_to_user(ubuf, tp_name, tp_len + 1))
3172 return -EFAULT;
3173 } else {
3174 char zero = '\0';
3175
3176 if (copy_to_user(ubuf, tp_name, ulen - 1))
3177 return -EFAULT;
3178 if (put_user(zero, ubuf + ulen - 1))
3179 return -EFAULT;
3180 return -ENOSPC;
3181 }
3182
3183 return 0;
3184}
3185
a3b80e10 3186static const struct bpf_link_ops bpf_raw_tp_link_lops = {
70ed506c 3187 .release = bpf_raw_tp_link_release,
babf3164 3188 .dealloc = bpf_raw_tp_link_dealloc,
f2e10bff
AN
3189 .show_fdinfo = bpf_raw_tp_link_show_fdinfo,
3190 .fill_link_info = bpf_raw_tp_link_fill_link_info,
c4f6699d
AS
3191};
3192
b89fbfbb
AN
3193#ifdef CONFIG_PERF_EVENTS
3194struct bpf_perf_link {
3195 struct bpf_link link;
3196 struct file *perf_file;
3197};
3198
3199static void bpf_perf_link_release(struct bpf_link *link)
3200{
3201 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3202 struct perf_event *event = perf_link->perf_file->private_data;
3203
3204 perf_event_free_bpf_prog(event);
3205 fput(perf_link->perf_file);
3206}
3207
3208static void bpf_perf_link_dealloc(struct bpf_link *link)
3209{
3210 struct bpf_perf_link *perf_link = container_of(link, struct bpf_perf_link, link);
3211
3212 kfree(perf_link);
3213}
3214
3215static const struct bpf_link_ops bpf_perf_link_lops = {
3216 .release = bpf_perf_link_release,
3217 .dealloc = bpf_perf_link_dealloc,
3218};
3219
3220static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3221{
3222 struct bpf_link_primer link_primer;
3223 struct bpf_perf_link *link;
3224 struct perf_event *event;
3225 struct file *perf_file;
3226 int err;
3227
3228 if (attr->link_create.flags)
3229 return -EINVAL;
3230
3231 perf_file = perf_event_get(attr->link_create.target_fd);
3232 if (IS_ERR(perf_file))
3233 return PTR_ERR(perf_file);
3234
3235 link = kzalloc(sizeof(*link), GFP_USER);
3236 if (!link) {
3237 err = -ENOMEM;
3238 goto out_put_file;
3239 }
3240 bpf_link_init(&link->link, BPF_LINK_TYPE_PERF_EVENT, &bpf_perf_link_lops, prog);
3241 link->perf_file = perf_file;
3242
3243 err = bpf_link_prime(&link->link, &link_primer);
3244 if (err) {
3245 kfree(link);
3246 goto out_put_file;
3247 }
3248
3249 event = perf_file->private_data;
82e6b1ee 3250 err = perf_event_set_bpf_prog(event, prog, attr->link_create.perf_event.bpf_cookie);
b89fbfbb
AN
3251 if (err) {
3252 bpf_link_cleanup(&link_primer);
3253 goto out_put_file;
3254 }
3255 /* perf_event_set_bpf_prog() doesn't take its own refcnt on prog */
3256 bpf_prog_inc(prog);
3257
3258 return bpf_link_settle(&link_primer);
3259
3260out_put_file:
3261 fput(perf_file);
3262 return err;
3263}
0dcac272
JO
3264#else
3265static int bpf_perf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
3266{
3267 return -EOPNOTSUPP;
3268}
b89fbfbb
AN
3269#endif /* CONFIG_PERF_EVENTS */
3270
df86ca0d
AN
3271static int bpf_raw_tp_link_attach(struct bpf_prog *prog,
3272 const char __user *user_tp_name)
c4f6699d 3273{
a3b80e10 3274 struct bpf_link_primer link_primer;
babf3164 3275 struct bpf_raw_tp_link *link;
c4f6699d 3276 struct bpf_raw_event_map *btp;
ac4414b5
AS
3277 const char *tp_name;
3278 char buf[128];
a3b80e10 3279 int err;
c4f6699d 3280
9e4e01df
KS
3281 switch (prog->type) {
3282 case BPF_PROG_TYPE_TRACING:
3283 case BPF_PROG_TYPE_EXT:
3284 case BPF_PROG_TYPE_LSM:
df86ca0d 3285 if (user_tp_name)
fec56f58
AS
3286 /* The attach point for this category of programs
3287 * should be specified via btf_id during program load.
ac4414b5 3288 */
df86ca0d 3289 return -EINVAL;
9e4e01df
KS
3290 if (prog->type == BPF_PROG_TYPE_TRACING &&
3291 prog->expected_attach_type == BPF_TRACE_RAW_TP) {
fec56f58 3292 tp_name = prog->aux->attach_func_name;
9e4e01df
KS
3293 break;
3294 }
2fcc8241 3295 return bpf_tracing_prog_attach(prog, 0, 0, 0);
9e4e01df
KS
3296 case BPF_PROG_TYPE_RAW_TRACEPOINT:
3297 case BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE:
df86ca0d
AN
3298 if (strncpy_from_user(buf, user_tp_name, sizeof(buf) - 1) < 0)
3299 return -EFAULT;
ac4414b5
AS
3300 buf[sizeof(buf) - 1] = 0;
3301 tp_name = buf;
9e4e01df
KS
3302 break;
3303 default:
df86ca0d 3304 return -EINVAL;
ac4414b5 3305 }
c4f6699d 3306
a38d1107 3307 btp = bpf_get_raw_tracepoint(tp_name);
df86ca0d
AN
3308 if (!btp)
3309 return -ENOENT;
c4f6699d 3310
babf3164
AN
3311 link = kzalloc(sizeof(*link), GFP_USER);
3312 if (!link) {
a38d1107
MM
3313 err = -ENOMEM;
3314 goto out_put_btp;
3315 }
f2e10bff
AN
3316 bpf_link_init(&link->link, BPF_LINK_TYPE_RAW_TRACEPOINT,
3317 &bpf_raw_tp_link_lops, prog);
babf3164 3318 link->btp = btp;
c4f6699d 3319
a3b80e10
AN
3320 err = bpf_link_prime(&link->link, &link_primer);
3321 if (err) {
babf3164 3322 kfree(link);
babf3164
AN
3323 goto out_put_btp;
3324 }
c4f6699d 3325
babf3164
AN
3326 err = bpf_probe_register(link->btp, prog);
3327 if (err) {
a3b80e10 3328 bpf_link_cleanup(&link_primer);
babf3164 3329 goto out_put_btp;
c4f6699d 3330 }
babf3164 3331
a3b80e10 3332 return bpf_link_settle(&link_primer);
c4f6699d 3333
a38d1107
MM
3334out_put_btp:
3335 bpf_put_raw_tracepoint(btp);
c4f6699d
AS
3336 return err;
3337}
3338
df86ca0d
AN
3339#define BPF_RAW_TRACEPOINT_OPEN_LAST_FIELD raw_tracepoint.prog_fd
3340
3341static int bpf_raw_tracepoint_open(const union bpf_attr *attr)
3342{
3343 struct bpf_prog *prog;
3344 int fd;
3345
3346 if (CHECK_ATTR(BPF_RAW_TRACEPOINT_OPEN))
3347 return -EINVAL;
3348
3349 prog = bpf_prog_get(attr->raw_tracepoint.prog_fd);
3350 if (IS_ERR(prog))
3351 return PTR_ERR(prog);
3352
3353 fd = bpf_raw_tp_link_attach(prog, u64_to_user_ptr(attr->raw_tracepoint.name));
3354 if (fd < 0)
3355 bpf_prog_put(prog);
3356 return fd;
3357}
3358
33491588
AR
3359static int bpf_prog_attach_check_attach_type(const struct bpf_prog *prog,
3360 enum bpf_attach_type attach_type)
3361{
3362 switch (prog->type) {
3363 case BPF_PROG_TYPE_CGROUP_SOCK:
3364 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
0d01da6a 3365 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
e9ddbb77 3366 case BPF_PROG_TYPE_SK_LOOKUP:
33491588 3367 return attach_type == prog->expected_attach_type ? 0 : -EINVAL;
5cf1e914 3368 case BPF_PROG_TYPE_CGROUP_SKB:
2c78ee89
AS
3369 if (!capable(CAP_NET_ADMIN))
3370 /* cg-skb progs can be loaded by unpriv user.
3371 * check permissions at attach time.
3372 */
3373 return -EPERM;
5cf1e914 3374 return prog->enforce_expected_attach_type &&
3375 prog->expected_attach_type != attach_type ?
3376 -EINVAL : 0;
33491588
AR
3377 default:
3378 return 0;
3379 }
3380}
3381
e28784e3
AN
3382static enum bpf_prog_type
3383attach_type_to_prog_type(enum bpf_attach_type attach_type)
f4324551 3384{
e28784e3 3385 switch (attach_type) {
f4324551
DM
3386 case BPF_CGROUP_INET_INGRESS:
3387 case BPF_CGROUP_INET_EGRESS:
e28784e3 3388 return BPF_PROG_TYPE_CGROUP_SKB;
61023658 3389 case BPF_CGROUP_INET_SOCK_CREATE:
f5836749 3390 case BPF_CGROUP_INET_SOCK_RELEASE:
aac3fc32
AI
3391 case BPF_CGROUP_INET4_POST_BIND:
3392 case BPF_CGROUP_INET6_POST_BIND:
e28784e3 3393 return BPF_PROG_TYPE_CGROUP_SOCK;
4fbac77d
AI
3394 case BPF_CGROUP_INET4_BIND:
3395 case BPF_CGROUP_INET6_BIND:
d74bad4e
AI
3396 case BPF_CGROUP_INET4_CONNECT:
3397 case BPF_CGROUP_INET6_CONNECT:
1b66d253
DB
3398 case BPF_CGROUP_INET4_GETPEERNAME:
3399 case BPF_CGROUP_INET6_GETPEERNAME:
3400 case BPF_CGROUP_INET4_GETSOCKNAME:
3401 case BPF_CGROUP_INET6_GETSOCKNAME:
1cedee13
AI
3402 case BPF_CGROUP_UDP4_SENDMSG:
3403 case BPF_CGROUP_UDP6_SENDMSG:
983695fa
DB
3404 case BPF_CGROUP_UDP4_RECVMSG:
3405 case BPF_CGROUP_UDP6_RECVMSG:
e28784e3 3406 return BPF_PROG_TYPE_CGROUP_SOCK_ADDR;
40304b2a 3407 case BPF_CGROUP_SOCK_OPS:
e28784e3 3408 return BPF_PROG_TYPE_SOCK_OPS;
ebc614f6 3409 case BPF_CGROUP_DEVICE:
e28784e3 3410 return BPF_PROG_TYPE_CGROUP_DEVICE;
4f738adb 3411 case BPF_SK_MSG_VERDICT:
e28784e3 3412 return BPF_PROG_TYPE_SK_MSG;
464bc0fd
JF
3413 case BPF_SK_SKB_STREAM_PARSER:
3414 case BPF_SK_SKB_STREAM_VERDICT:
a7ba4558 3415 case BPF_SK_SKB_VERDICT:
e28784e3 3416 return BPF_PROG_TYPE_SK_SKB;
f4364dcf 3417 case BPF_LIRC_MODE2:
e28784e3 3418 return BPF_PROG_TYPE_LIRC_MODE2;
d58e468b 3419 case BPF_FLOW_DISSECTOR:
e28784e3 3420 return BPF_PROG_TYPE_FLOW_DISSECTOR;
7b146ceb 3421 case BPF_CGROUP_SYSCTL:
e28784e3 3422 return BPF_PROG_TYPE_CGROUP_SYSCTL;
0d01da6a
SF
3423 case BPF_CGROUP_GETSOCKOPT:
3424 case BPF_CGROUP_SETSOCKOPT:
e28784e3 3425 return BPF_PROG_TYPE_CGROUP_SOCKOPT;
de4e05ca 3426 case BPF_TRACE_ITER:
df86ca0d
AN
3427 case BPF_TRACE_RAW_TP:
3428 case BPF_TRACE_FENTRY:
3429 case BPF_TRACE_FEXIT:
3430 case BPF_MODIFY_RETURN:
de4e05ca 3431 return BPF_PROG_TYPE_TRACING;
df86ca0d
AN
3432 case BPF_LSM_MAC:
3433 return BPF_PROG_TYPE_LSM;
e9ddbb77
JS
3434 case BPF_SK_LOOKUP:
3435 return BPF_PROG_TYPE_SK_LOOKUP;
aa8d3a71
AN
3436 case BPF_XDP:
3437 return BPF_PROG_TYPE_XDP;
69fd337a
SF
3438 case BPF_LSM_CGROUP:
3439 return BPF_PROG_TYPE_LSM;
f4324551 3440 default:
e28784e3 3441 return BPF_PROG_TYPE_UNSPEC;
f4324551 3442 }
e28784e3
AN
3443}
3444
3445#define BPF_PROG_ATTACH_LAST_FIELD replace_bpf_fd
3446
3447#define BPF_F_ATTACH_MASK \
3448 (BPF_F_ALLOW_OVERRIDE | BPF_F_ALLOW_MULTI | BPF_F_REPLACE)
3449
3450static int bpf_prog_attach(const union bpf_attr *attr)
3451{
3452 enum bpf_prog_type ptype;
3453 struct bpf_prog *prog;
3454 int ret;
3455
e28784e3
AN
3456 if (CHECK_ATTR(BPF_PROG_ATTACH))
3457 return -EINVAL;
3458
3459 if (attr->attach_flags & ~BPF_F_ATTACH_MASK)
3460 return -EINVAL;
3461
3462 ptype = attach_type_to_prog_type(attr->attach_type);
3463 if (ptype == BPF_PROG_TYPE_UNSPEC)
3464 return -EINVAL;
f4324551 3465
b2cd1257
DA
3466 prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
3467 if (IS_ERR(prog))
3468 return PTR_ERR(prog);
3469
5e43f899
AI
3470 if (bpf_prog_attach_check_attach_type(prog, attr->attach_type)) {
3471 bpf_prog_put(prog);
3472 return -EINVAL;
3473 }
3474
fdb5c453
SY
3475 switch (ptype) {
3476 case BPF_PROG_TYPE_SK_SKB:
3477 case BPF_PROG_TYPE_SK_MSG:
604326b4 3478 ret = sock_map_get_from_fd(attr, prog);
fdb5c453
SY
3479 break;
3480 case BPF_PROG_TYPE_LIRC_MODE2:
3481 ret = lirc_prog_attach(attr, prog);
3482 break;
d58e468b 3483 case BPF_PROG_TYPE_FLOW_DISSECTOR:
a3fd7cee 3484 ret = netns_bpf_prog_attach(attr, prog);
d58e468b 3485 break;
e28784e3
AN
3486 case BPF_PROG_TYPE_CGROUP_DEVICE:
3487 case BPF_PROG_TYPE_CGROUP_SKB:
3488 case BPF_PROG_TYPE_CGROUP_SOCK:
3489 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3490 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3491 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3492 case BPF_PROG_TYPE_SOCK_OPS:
69fd337a
SF
3493 case BPF_PROG_TYPE_LSM:
3494 if (ptype == BPF_PROG_TYPE_LSM &&
3495 prog->expected_attach_type != BPF_LSM_CGROUP)
3496 return -EINVAL;
3497
fdb5c453 3498 ret = cgroup_bpf_prog_attach(attr, ptype, prog);
e28784e3
AN
3499 break;
3500 default:
3501 ret = -EINVAL;
b2cd1257
DA
3502 }
3503
7f677633
AS
3504 if (ret)
3505 bpf_prog_put(prog);
7f677633 3506 return ret;
f4324551
DM
3507}
3508
3509#define BPF_PROG_DETACH_LAST_FIELD attach_type
3510
3511static int bpf_prog_detach(const union bpf_attr *attr)
3512{
324bda9e 3513 enum bpf_prog_type ptype;
f4324551 3514
f4324551
DM
3515 if (CHECK_ATTR(BPF_PROG_DETACH))
3516 return -EINVAL;
3517
e28784e3
AN
3518 ptype = attach_type_to_prog_type(attr->attach_type);
3519
3520 switch (ptype) {
3521 case BPF_PROG_TYPE_SK_MSG:
3522 case BPF_PROG_TYPE_SK_SKB:
bb0de313 3523 return sock_map_prog_detach(attr, ptype);
e28784e3 3524 case BPF_PROG_TYPE_LIRC_MODE2:
f4364dcf 3525 return lirc_prog_detach(attr);
e28784e3 3526 case BPF_PROG_TYPE_FLOW_DISSECTOR:
4ac2add6 3527 return netns_bpf_prog_detach(attr, ptype);
e28784e3
AN
3528 case BPF_PROG_TYPE_CGROUP_DEVICE:
3529 case BPF_PROG_TYPE_CGROUP_SKB:
3530 case BPF_PROG_TYPE_CGROUP_SOCK:
3531 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
3532 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
3533 case BPF_PROG_TYPE_CGROUP_SYSCTL:
3534 case BPF_PROG_TYPE_SOCK_OPS:
69fd337a 3535 case BPF_PROG_TYPE_LSM:
e28784e3 3536 return cgroup_bpf_prog_detach(attr, ptype);
f4324551
DM
3537 default:
3538 return -EINVAL;
3539 }
f4324551 3540}
40304b2a 3541
b79c9fc9 3542#define BPF_PROG_QUERY_LAST_FIELD query.prog_attach_flags
468e2f64
AS
3543
3544static int bpf_prog_query(const union bpf_attr *attr,
3545 union bpf_attr __user *uattr)
3546{
468e2f64
AS
3547 if (!capable(CAP_NET_ADMIN))
3548 return -EPERM;
3549 if (CHECK_ATTR(BPF_PROG_QUERY))
3550 return -EINVAL;
3551 if (attr->query.query_flags & ~BPF_F_QUERY_EFFECTIVE)
3552 return -EINVAL;
3553
3554 switch (attr->query.attach_type) {
3555 case BPF_CGROUP_INET_INGRESS:
3556 case BPF_CGROUP_INET_EGRESS:
3557 case BPF_CGROUP_INET_SOCK_CREATE:
f5836749 3558 case BPF_CGROUP_INET_SOCK_RELEASE:
4fbac77d
AI
3559 case BPF_CGROUP_INET4_BIND:
3560 case BPF_CGROUP_INET6_BIND:
aac3fc32
AI
3561 case BPF_CGROUP_INET4_POST_BIND:
3562 case BPF_CGROUP_INET6_POST_BIND:
d74bad4e
AI
3563 case BPF_CGROUP_INET4_CONNECT:
3564 case BPF_CGROUP_INET6_CONNECT:
1b66d253
DB
3565 case BPF_CGROUP_INET4_GETPEERNAME:
3566 case BPF_CGROUP_INET6_GETPEERNAME:
3567 case BPF_CGROUP_INET4_GETSOCKNAME:
3568 case BPF_CGROUP_INET6_GETSOCKNAME:
1cedee13
AI
3569 case BPF_CGROUP_UDP4_SENDMSG:
3570 case BPF_CGROUP_UDP6_SENDMSG:
983695fa
DB
3571 case BPF_CGROUP_UDP4_RECVMSG:
3572 case BPF_CGROUP_UDP6_RECVMSG:
468e2f64 3573 case BPF_CGROUP_SOCK_OPS:
ebc614f6 3574 case BPF_CGROUP_DEVICE:
7b146ceb 3575 case BPF_CGROUP_SYSCTL:
0d01da6a
SF
3576 case BPF_CGROUP_GETSOCKOPT:
3577 case BPF_CGROUP_SETSOCKOPT:
b79c9fc9 3578 case BPF_LSM_CGROUP:
e28784e3 3579 return cgroup_bpf_prog_query(attr, uattr);
f4364dcf
SY
3580 case BPF_LIRC_MODE2:
3581 return lirc_prog_query(attr, uattr);
118c8e9a 3582 case BPF_FLOW_DISSECTOR:
e9ddbb77 3583 case BPF_SK_LOOKUP:
a3fd7cee 3584 return netns_bpf_prog_query(attr, uattr);
748cd572
DZ
3585 case BPF_SK_SKB_STREAM_PARSER:
3586 case BPF_SK_SKB_STREAM_VERDICT:
3587 case BPF_SK_MSG_VERDICT:
3588 case BPF_SK_SKB_VERDICT:
3589 return sock_map_bpf_prog_query(attr, uattr);
468e2f64
AS
3590 default:
3591 return -EINVAL;
3592 }
468e2f64 3593}
f4324551 3594
b530e9e1 3595#define BPF_PROG_TEST_RUN_LAST_FIELD test.batch_size
1cf1cae9
AS
3596
3597static int bpf_prog_test_run(const union bpf_attr *attr,
3598 union bpf_attr __user *uattr)
3599{
3600 struct bpf_prog *prog;
3601 int ret = -ENOTSUPP;
3602
3603 if (CHECK_ATTR(BPF_PROG_TEST_RUN))
3604 return -EINVAL;
3605
b0b9395d
SF
3606 if ((attr->test.ctx_size_in && !attr->test.ctx_in) ||
3607 (!attr->test.ctx_size_in && attr->test.ctx_in))
3608 return -EINVAL;
3609
3610 if ((attr->test.ctx_size_out && !attr->test.ctx_out) ||
3611 (!attr->test.ctx_size_out && attr->test.ctx_out))
3612 return -EINVAL;
3613
1cf1cae9
AS
3614 prog = bpf_prog_get(attr->test.prog_fd);
3615 if (IS_ERR(prog))
3616 return PTR_ERR(prog);
3617
3618 if (prog->aux->ops->test_run)
3619 ret = prog->aux->ops->test_run(prog, attr, uattr);
3620
3621 bpf_prog_put(prog);
3622 return ret;
3623}
3624
34ad5580
MKL
3625#define BPF_OBJ_GET_NEXT_ID_LAST_FIELD next_id
3626
3627static int bpf_obj_get_next_id(const union bpf_attr *attr,
3628 union bpf_attr __user *uattr,
3629 struct idr *idr,
3630 spinlock_t *lock)
3631{
3632 u32 next_id = attr->start_id;
3633 int err = 0;
3634
3635 if (CHECK_ATTR(BPF_OBJ_GET_NEXT_ID) || next_id >= INT_MAX)
3636 return -EINVAL;
3637
3638 if (!capable(CAP_SYS_ADMIN))
3639 return -EPERM;
3640
3641 next_id++;
3642 spin_lock_bh(lock);
3643 if (!idr_get_next(idr, &next_id))
3644 err = -ENOENT;
3645 spin_unlock_bh(lock);
3646
3647 if (!err)
3648 err = put_user(next_id, &uattr->next_id);
3649
3650 return err;
3651}
3652
6086d29d
YS
3653struct bpf_map *bpf_map_get_curr_or_next(u32 *id)
3654{
3655 struct bpf_map *map;
3656
3657 spin_lock_bh(&map_idr_lock);
3658again:
3659 map = idr_get_next(&map_idr, id);
3660 if (map) {
3661 map = __bpf_map_inc_not_zero(map, false);
3662 if (IS_ERR(map)) {
3663 (*id)++;
3664 goto again;
3665 }
3666 }
3667 spin_unlock_bh(&map_idr_lock);
3668
3669 return map;
3670}
3671
a228a64f
AS
3672struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id)
3673{
3674 struct bpf_prog *prog;
3675
3676 spin_lock_bh(&prog_idr_lock);
3677again:
3678 prog = idr_get_next(&prog_idr, id);
3679 if (prog) {
3680 prog = bpf_prog_inc_not_zero(prog);
3681 if (IS_ERR(prog)) {
3682 (*id)++;
3683 goto again;
3684 }
3685 }
3686 spin_unlock_bh(&prog_idr_lock);
3687
3688 return prog;
3689}
3690
b16d9aa4
MKL
3691#define BPF_PROG_GET_FD_BY_ID_LAST_FIELD prog_id
3692
7e6897f9 3693struct bpf_prog *bpf_prog_by_id(u32 id)
b16d9aa4
MKL
3694{
3695 struct bpf_prog *prog;
b16d9aa4 3696
7e6897f9
BT
3697 if (!id)
3698 return ERR_PTR(-ENOENT);
b16d9aa4
MKL
3699
3700 spin_lock_bh(&prog_idr_lock);
3701 prog = idr_find(&prog_idr, id);
3702 if (prog)
3703 prog = bpf_prog_inc_not_zero(prog);
3704 else
3705 prog = ERR_PTR(-ENOENT);
3706 spin_unlock_bh(&prog_idr_lock);
7e6897f9
BT
3707 return prog;
3708}
3709
3710static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
3711{
3712 struct bpf_prog *prog;
3713 u32 id = attr->prog_id;
3714 int fd;
3715
3716 if (CHECK_ATTR(BPF_PROG_GET_FD_BY_ID))
3717 return -EINVAL;
3718
3719 if (!capable(CAP_SYS_ADMIN))
3720 return -EPERM;
b16d9aa4 3721
7e6897f9 3722 prog = bpf_prog_by_id(id);
b16d9aa4
MKL
3723 if (IS_ERR(prog))
3724 return PTR_ERR(prog);
3725
3726 fd = bpf_prog_new_fd(prog);
3727 if (fd < 0)
3728 bpf_prog_put(prog);
3729
3730 return fd;
3731}
3732
6e71b04a 3733#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags
bd5f5f4e
MKL
3734
3735static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
3736{
3737 struct bpf_map *map;
3738 u32 id = attr->map_id;
6e71b04a 3739 int f_flags;
bd5f5f4e
MKL
3740 int fd;
3741
6e71b04a
CF
3742 if (CHECK_ATTR(BPF_MAP_GET_FD_BY_ID) ||
3743 attr->open_flags & ~BPF_OBJ_FLAG_MASK)
bd5f5f4e
MKL
3744 return -EINVAL;
3745
3746 if (!capable(CAP_SYS_ADMIN))
3747 return -EPERM;
3748
6e71b04a
CF
3749 f_flags = bpf_get_file_flag(attr->open_flags);
3750 if (f_flags < 0)
3751 return f_flags;
3752
bd5f5f4e
MKL
3753 spin_lock_bh(&map_idr_lock);
3754 map = idr_find(&map_idr, id);
3755 if (map)
b0e4701c 3756 map = __bpf_map_inc_not_zero(map, true);
bd5f5f4e
MKL
3757 else
3758 map = ERR_PTR(-ENOENT);
3759 spin_unlock_bh(&map_idr_lock);
3760
3761 if (IS_ERR(map))
3762 return PTR_ERR(map);
3763
6e71b04a 3764 fd = bpf_map_new_fd(map, f_flags);
bd5f5f4e 3765 if (fd < 0)
781e6282 3766 bpf_map_put_with_uref(map);
bd5f5f4e
MKL
3767
3768 return fd;
3769}
3770
7105e828 3771static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
d8eca5bb
DB
3772 unsigned long addr, u32 *off,
3773 u32 *type)
7105e828 3774{
d8eca5bb 3775 const struct bpf_map *map;
7105e828
DB
3776 int i;
3777
984fe94f 3778 mutex_lock(&prog->aux->used_maps_mutex);
d8eca5bb
DB
3779 for (i = 0, *off = 0; i < prog->aux->used_map_cnt; i++) {
3780 map = prog->aux->used_maps[i];
3781 if (map == (void *)addr) {
3782 *type = BPF_PSEUDO_MAP_FD;
984fe94f 3783 goto out;
d8eca5bb
DB
3784 }
3785 if (!map->ops->map_direct_value_meta)
3786 continue;
3787 if (!map->ops->map_direct_value_meta(map, addr, off)) {
3788 *type = BPF_PSEUDO_MAP_VALUE;
984fe94f 3789 goto out;
d8eca5bb
DB
3790 }
3791 }
984fe94f 3792 map = NULL;
d8eca5bb 3793
984fe94f
YZ
3794out:
3795 mutex_unlock(&prog->aux->used_maps_mutex);
3796 return map;
7105e828
DB
3797}
3798
63960260
KC
3799static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog,
3800 const struct cred *f_cred)
7105e828
DB
3801{
3802 const struct bpf_map *map;
3803 struct bpf_insn *insns;
d8eca5bb 3804 u32 off, type;
7105e828 3805 u64 imm;
29fcb05b 3806 u8 code;
7105e828
DB
3807 int i;
3808
3809 insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
3810 GFP_USER);
3811 if (!insns)
3812 return insns;
3813
3814 for (i = 0; i < prog->len; i++) {
29fcb05b
AN
3815 code = insns[i].code;
3816
3817 if (code == (BPF_JMP | BPF_TAIL_CALL)) {
7105e828
DB
3818 insns[i].code = BPF_JMP | BPF_CALL;
3819 insns[i].imm = BPF_FUNC_tail_call;
3820 /* fall-through */
3821 }
29fcb05b
AN
3822 if (code == (BPF_JMP | BPF_CALL) ||
3823 code == (BPF_JMP | BPF_CALL_ARGS)) {
3824 if (code == (BPF_JMP | BPF_CALL_ARGS))
7105e828 3825 insns[i].code = BPF_JMP | BPF_CALL;
63960260 3826 if (!bpf_dump_raw_ok(f_cred))
7105e828
DB
3827 insns[i].imm = 0;
3828 continue;
3829 }
29fcb05b
AN
3830 if (BPF_CLASS(code) == BPF_LDX && BPF_MODE(code) == BPF_PROBE_MEM) {
3831 insns[i].code = BPF_LDX | BPF_SIZE(code) | BPF_MEM;
3832 continue;
3833 }
7105e828 3834
29fcb05b 3835 if (code != (BPF_LD | BPF_IMM | BPF_DW))
7105e828
DB
3836 continue;
3837
3838 imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
d8eca5bb 3839 map = bpf_map_from_imm(prog, imm, &off, &type);
7105e828 3840 if (map) {
d8eca5bb 3841 insns[i].src_reg = type;
7105e828 3842 insns[i].imm = map->id;
d8eca5bb 3843 insns[i + 1].imm = off;
7105e828
DB
3844 continue;
3845 }
7105e828
DB
3846 }
3847
3848 return insns;
3849}
3850
c454a46b
MKL
3851static int set_info_rec_size(struct bpf_prog_info *info)
3852{
3853 /*
3854 * Ensure info.*_rec_size is the same as kernel expected size
3855 *
3856 * or
3857 *
3858 * Only allow zero *_rec_size if both _rec_size and _cnt are
3859 * zero. In this case, the kernel will set the expected
3860 * _rec_size back to the info.
3861 */
3862
11d8b82d 3863 if ((info->nr_func_info || info->func_info_rec_size) &&
c454a46b
MKL
3864 info->func_info_rec_size != sizeof(struct bpf_func_info))
3865 return -EINVAL;
3866
11d8b82d 3867 if ((info->nr_line_info || info->line_info_rec_size) &&
c454a46b
MKL
3868 info->line_info_rec_size != sizeof(struct bpf_line_info))
3869 return -EINVAL;
3870
11d8b82d 3871 if ((info->nr_jited_line_info || info->jited_line_info_rec_size) &&
c454a46b
MKL
3872 info->jited_line_info_rec_size != sizeof(__u64))
3873 return -EINVAL;
3874
3875 info->func_info_rec_size = sizeof(struct bpf_func_info);
3876 info->line_info_rec_size = sizeof(struct bpf_line_info);
3877 info->jited_line_info_rec_size = sizeof(__u64);
3878
3879 return 0;
3880}
3881
63960260
KC
3882static int bpf_prog_get_info_by_fd(struct file *file,
3883 struct bpf_prog *prog,
1e270976
MKL
3884 const union bpf_attr *attr,
3885 union bpf_attr __user *uattr)
3886{
3887 struct bpf_prog_info __user *uinfo = u64_to_user_ptr(attr->info.info);
6644aabb 3888 struct btf *attach_btf = bpf_prog_get_target_btf(prog);
5c6f2588 3889 struct bpf_prog_info info;
1e270976 3890 u32 info_len = attr->info.info_len;
61a0abae 3891 struct bpf_prog_kstats stats;
1e270976
MKL
3892 char __user *uinsns;
3893 u32 ulen;
3894 int err;
3895
af2ac3e1 3896 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
1e270976
MKL
3897 if (err)
3898 return err;
3899 info_len = min_t(u32, sizeof(info), info_len);
3900
5c6f2588 3901 memset(&info, 0, sizeof(info));
1e270976 3902 if (copy_from_user(&info, uinfo, info_len))
89b09689 3903 return -EFAULT;
1e270976
MKL
3904
3905 info.type = prog->type;
3906 info.id = prog->aux->id;
cb4d2b3f
MKL
3907 info.load_time = prog->aux->load_time;
3908 info.created_by_uid = from_kuid_munged(current_user_ns(),
3909 prog->aux->user->uid);
b85fab0e 3910 info.gpl_compatible = prog->gpl_compatible;
1e270976
MKL
3911
3912 memcpy(info.tag, prog->tag, sizeof(prog->tag));
cb4d2b3f
MKL
3913 memcpy(info.name, prog->aux->name, sizeof(prog->aux->name));
3914
984fe94f 3915 mutex_lock(&prog->aux->used_maps_mutex);
cb4d2b3f
MKL
3916 ulen = info.nr_map_ids;
3917 info.nr_map_ids = prog->aux->used_map_cnt;
3918 ulen = min_t(u32, info.nr_map_ids, ulen);
3919 if (ulen) {
721e08da 3920 u32 __user *user_map_ids = u64_to_user_ptr(info.map_ids);
cb4d2b3f
MKL
3921 u32 i;
3922
3923 for (i = 0; i < ulen; i++)
3924 if (put_user(prog->aux->used_maps[i]->id,
984fe94f
YZ
3925 &user_map_ids[i])) {
3926 mutex_unlock(&prog->aux->used_maps_mutex);
cb4d2b3f 3927 return -EFAULT;
984fe94f 3928 }
cb4d2b3f 3929 }
984fe94f 3930 mutex_unlock(&prog->aux->used_maps_mutex);
1e270976 3931
c454a46b
MKL
3932 err = set_info_rec_size(&info);
3933 if (err)
3934 return err;
7337224f 3935
5f8f8b93
AS
3936 bpf_prog_get_stats(prog, &stats);
3937 info.run_time_ns = stats.nsecs;
3938 info.run_cnt = stats.cnt;
9ed9e9ba 3939 info.recursion_misses = stats.misses;
5f8f8b93 3940
aba64c7d
DM
3941 info.verified_insns = prog->aux->verified_insns;
3942
2c78ee89 3943 if (!bpf_capable()) {
1e270976
MKL
3944 info.jited_prog_len = 0;
3945 info.xlated_prog_len = 0;
dbecd738 3946 info.nr_jited_ksyms = 0;
28c2fae7 3947 info.nr_jited_func_lens = 0;
11d8b82d
YS
3948 info.nr_func_info = 0;
3949 info.nr_line_info = 0;
3950 info.nr_jited_line_info = 0;
1e270976
MKL
3951 goto done;
3952 }
3953
1e270976 3954 ulen = info.xlated_prog_len;
9975a54b 3955 info.xlated_prog_len = bpf_prog_insn_size(prog);
1e270976 3956 if (info.xlated_prog_len && ulen) {
7105e828
DB
3957 struct bpf_insn *insns_sanitized;
3958 bool fault;
3959
63960260 3960 if (prog->blinded && !bpf_dump_raw_ok(file->f_cred)) {
7105e828
DB
3961 info.xlated_prog_insns = 0;
3962 goto done;
3963 }
63960260 3964 insns_sanitized = bpf_insn_prepare_dump(prog, file->f_cred);
7105e828
DB
3965 if (!insns_sanitized)
3966 return -ENOMEM;
1e270976
MKL
3967 uinsns = u64_to_user_ptr(info.xlated_prog_insns);
3968 ulen = min_t(u32, info.xlated_prog_len, ulen);
7105e828
DB
3969 fault = copy_to_user(uinsns, insns_sanitized, ulen);
3970 kfree(insns_sanitized);
3971 if (fault)
1e270976
MKL
3972 return -EFAULT;
3973 }
3974
675fc275
JK
3975 if (bpf_prog_is_dev_bound(prog->aux)) {
3976 err = bpf_prog_offload_info_fill(&info, prog);
3977 if (err)
3978 return err;
fcfb126d
JW
3979 goto done;
3980 }
3981
3982 /* NOTE: the following code is supposed to be skipped for offload.
3983 * bpf_prog_offload_info_fill() is the place to fill similar fields
3984 * for offload.
3985 */
3986 ulen = info.jited_prog_len;
4d56a76e
SD
3987 if (prog->aux->func_cnt) {
3988 u32 i;
3989
3990 info.jited_prog_len = 0;
3991 for (i = 0; i < prog->aux->func_cnt; i++)
3992 info.jited_prog_len += prog->aux->func[i]->jited_len;
3993 } else {
3994 info.jited_prog_len = prog->jited_len;
3995 }
3996
fcfb126d 3997 if (info.jited_prog_len && ulen) {
63960260 3998 if (bpf_dump_raw_ok(file->f_cred)) {
fcfb126d
JW
3999 uinsns = u64_to_user_ptr(info.jited_prog_insns);
4000 ulen = min_t(u32, info.jited_prog_len, ulen);
4d56a76e
SD
4001
4002 /* for multi-function programs, copy the JITed
4003 * instructions for all the functions
4004 */
4005 if (prog->aux->func_cnt) {
4006 u32 len, free, i;
4007 u8 *img;
4008
4009 free = ulen;
4010 for (i = 0; i < prog->aux->func_cnt; i++) {
4011 len = prog->aux->func[i]->jited_len;
4012 len = min_t(u32, len, free);
4013 img = (u8 *) prog->aux->func[i]->bpf_func;
4014 if (copy_to_user(uinsns, img, len))
4015 return -EFAULT;
4016 uinsns += len;
4017 free -= len;
4018 if (!free)
4019 break;
4020 }
4021 } else {
4022 if (copy_to_user(uinsns, prog->bpf_func, ulen))
4023 return -EFAULT;
4024 }
fcfb126d
JW
4025 } else {
4026 info.jited_prog_insns = 0;
4027 }
675fc275
JK
4028 }
4029
dbecd738 4030 ulen = info.nr_jited_ksyms;
ff1889fc 4031 info.nr_jited_ksyms = prog->aux->func_cnt ? : 1;
7a5725dd 4032 if (ulen) {
63960260 4033 if (bpf_dump_raw_ok(file->f_cred)) {
ff1889fc 4034 unsigned long ksym_addr;
dbecd738 4035 u64 __user *user_ksyms;
dbecd738
SD
4036 u32 i;
4037
4038 /* copy the address of the kernel symbol
4039 * corresponding to each function
4040 */
4041 ulen = min_t(u32, info.nr_jited_ksyms, ulen);
4042 user_ksyms = u64_to_user_ptr(info.jited_ksyms);
ff1889fc
SL
4043 if (prog->aux->func_cnt) {
4044 for (i = 0; i < ulen; i++) {
4045 ksym_addr = (unsigned long)
4046 prog->aux->func[i]->bpf_func;
4047 if (put_user((u64) ksym_addr,
4048 &user_ksyms[i]))
4049 return -EFAULT;
4050 }
4051 } else {
4052 ksym_addr = (unsigned long) prog->bpf_func;
4053 if (put_user((u64) ksym_addr, &user_ksyms[0]))
dbecd738
SD
4054 return -EFAULT;
4055 }
4056 } else {
4057 info.jited_ksyms = 0;
4058 }
4059 }
4060
815581c1 4061 ulen = info.nr_jited_func_lens;
ff1889fc 4062 info.nr_jited_func_lens = prog->aux->func_cnt ? : 1;
7a5725dd 4063 if (ulen) {
63960260 4064 if (bpf_dump_raw_ok(file->f_cred)) {
815581c1
SD
4065 u32 __user *user_lens;
4066 u32 func_len, i;
4067
4068 /* copy the JITed image lengths for each function */
4069 ulen = min_t(u32, info.nr_jited_func_lens, ulen);
4070 user_lens = u64_to_user_ptr(info.jited_func_lens);
ff1889fc
SL
4071 if (prog->aux->func_cnt) {
4072 for (i = 0; i < ulen; i++) {
4073 func_len =
4074 prog->aux->func[i]->jited_len;
4075 if (put_user(func_len, &user_lens[i]))
4076 return -EFAULT;
4077 }
4078 } else {
4079 func_len = prog->jited_len;
4080 if (put_user(func_len, &user_lens[0]))
815581c1
SD
4081 return -EFAULT;
4082 }
4083 } else {
4084 info.jited_func_lens = 0;
4085 }
4086 }
4087
7337224f 4088 if (prog->aux->btf)
22dc4a0f 4089 info.btf_id = btf_obj_id(prog->aux->btf);
b79c9fc9 4090 info.attach_btf_id = prog->aux->attach_btf_id;
6644aabb
SF
4091 if (attach_btf)
4092 info.attach_btf_obj_id = btf_obj_id(attach_btf);
838e9690 4093
11d8b82d
YS
4094 ulen = info.nr_func_info;
4095 info.nr_func_info = prog->aux->func_info_cnt;
4096 if (info.nr_func_info && ulen) {
9e794163 4097 char __user *user_finfo;
7337224f 4098
9e794163
MKL
4099 user_finfo = u64_to_user_ptr(info.func_info);
4100 ulen = min_t(u32, info.nr_func_info, ulen);
4101 if (copy_to_user(user_finfo, prog->aux->func_info,
4102 info.func_info_rec_size * ulen))
4103 return -EFAULT;
838e9690
YS
4104 }
4105
11d8b82d
YS
4106 ulen = info.nr_line_info;
4107 info.nr_line_info = prog->aux->nr_linfo;
4108 if (info.nr_line_info && ulen) {
9e794163 4109 __u8 __user *user_linfo;
c454a46b 4110
9e794163
MKL
4111 user_linfo = u64_to_user_ptr(info.line_info);
4112 ulen = min_t(u32, info.nr_line_info, ulen);
4113 if (copy_to_user(user_linfo, prog->aux->linfo,
4114 info.line_info_rec_size * ulen))
4115 return -EFAULT;
c454a46b
MKL
4116 }
4117
11d8b82d 4118 ulen = info.nr_jited_line_info;
c454a46b 4119 if (prog->aux->jited_linfo)
11d8b82d 4120 info.nr_jited_line_info = prog->aux->nr_linfo;
c454a46b 4121 else
11d8b82d
YS
4122 info.nr_jited_line_info = 0;
4123 if (info.nr_jited_line_info && ulen) {
63960260 4124 if (bpf_dump_raw_ok(file->f_cred)) {
2cd00852 4125 unsigned long line_addr;
c454a46b
MKL
4126 __u64 __user *user_linfo;
4127 u32 i;
4128
4129 user_linfo = u64_to_user_ptr(info.jited_line_info);
11d8b82d 4130 ulen = min_t(u32, info.nr_jited_line_info, ulen);
c454a46b 4131 for (i = 0; i < ulen; i++) {
2cd00852
PL
4132 line_addr = (unsigned long)prog->aux->jited_linfo[i];
4133 if (put_user((__u64)line_addr, &user_linfo[i]))
c454a46b
MKL
4134 return -EFAULT;
4135 }
4136 } else {
4137 info.jited_line_info = 0;
4138 }
4139 }
4140
c872bdb3
SL
4141 ulen = info.nr_prog_tags;
4142 info.nr_prog_tags = prog->aux->func_cnt ? : 1;
4143 if (ulen) {
4144 __u8 __user (*user_prog_tags)[BPF_TAG_SIZE];
4145 u32 i;
4146
4147 user_prog_tags = u64_to_user_ptr(info.prog_tags);
4148 ulen = min_t(u32, info.nr_prog_tags, ulen);
4149 if (prog->aux->func_cnt) {
4150 for (i = 0; i < ulen; i++) {
4151 if (copy_to_user(user_prog_tags[i],
4152 prog->aux->func[i]->tag,
4153 BPF_TAG_SIZE))
4154 return -EFAULT;
4155 }
4156 } else {
4157 if (copy_to_user(user_prog_tags[0],
4158 prog->tag, BPF_TAG_SIZE))
4159 return -EFAULT;
4160 }
4161 }
4162
1e270976
MKL
4163done:
4164 if (copy_to_user(uinfo, &info, info_len) ||
4165 put_user(info_len, &uattr->info.info_len))
4166 return -EFAULT;
4167
4168 return 0;
4169}
4170
63960260
KC
4171static int bpf_map_get_info_by_fd(struct file *file,
4172 struct bpf_map *map,
1e270976
MKL
4173 const union bpf_attr *attr,
4174 union bpf_attr __user *uattr)
4175{
4176 struct bpf_map_info __user *uinfo = u64_to_user_ptr(attr->info.info);
5c6f2588 4177 struct bpf_map_info info;
1e270976
MKL
4178 u32 info_len = attr->info.info_len;
4179 int err;
4180
af2ac3e1 4181 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
1e270976
MKL
4182 if (err)
4183 return err;
4184 info_len = min_t(u32, sizeof(info), info_len);
4185
5c6f2588 4186 memset(&info, 0, sizeof(info));
1e270976
MKL
4187 info.type = map->map_type;
4188 info.id = map->id;
4189 info.key_size = map->key_size;
4190 info.value_size = map->value_size;
4191 info.max_entries = map->max_entries;
4192 info.map_flags = map->map_flags;
9330986c 4193 info.map_extra = map->map_extra;
ad5b177b 4194 memcpy(info.name, map->name, sizeof(map->name));
1e270976 4195
78958fca 4196 if (map->btf) {
22dc4a0f 4197 info.btf_id = btf_obj_id(map->btf);
9b2cf328
MKL
4198 info.btf_key_type_id = map->btf_key_type_id;
4199 info.btf_value_type_id = map->btf_value_type_id;
78958fca 4200 }
85d33df3 4201 info.btf_vmlinux_value_type_id = map->btf_vmlinux_value_type_id;
78958fca 4202
52775b33
JK
4203 if (bpf_map_is_dev_bound(map)) {
4204 err = bpf_map_offload_info_fill(&info, map);
4205 if (err)
4206 return err;
4207 }
4208
1e270976
MKL
4209 if (copy_to_user(uinfo, &info, info_len) ||
4210 put_user(info_len, &uattr->info.info_len))
4211 return -EFAULT;
4212
4213 return 0;
4214}
4215
63960260
KC
4216static int bpf_btf_get_info_by_fd(struct file *file,
4217 struct btf *btf,
62dab84c
MKL
4218 const union bpf_attr *attr,
4219 union bpf_attr __user *uattr)
4220{
4221 struct bpf_btf_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4222 u32 info_len = attr->info.info_len;
4223 int err;
4224
af2ac3e1 4225 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(*uinfo), info_len);
62dab84c
MKL
4226 if (err)
4227 return err;
4228
4229 return btf_get_info_by_fd(btf, attr, uattr);
4230}
4231
63960260
KC
4232static int bpf_link_get_info_by_fd(struct file *file,
4233 struct bpf_link *link,
f2e10bff
AN
4234 const union bpf_attr *attr,
4235 union bpf_attr __user *uattr)
4236{
4237 struct bpf_link_info __user *uinfo = u64_to_user_ptr(attr->info.info);
4238 struct bpf_link_info info;
4239 u32 info_len = attr->info.info_len;
4240 int err;
4241
af2ac3e1 4242 err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), sizeof(info), info_len);
f2e10bff
AN
4243 if (err)
4244 return err;
4245 info_len = min_t(u32, sizeof(info), info_len);
4246
4247 memset(&info, 0, sizeof(info));
4248 if (copy_from_user(&info, uinfo, info_len))
4249 return -EFAULT;
4250
4251 info.type = link->type;
4252 info.id = link->id;
4253 info.prog_id = link->prog->aux->id;
4254
4255 if (link->ops->fill_link_info) {
4256 err = link->ops->fill_link_info(link, &info);
4257 if (err)
4258 return err;
4259 }
4260
4261 if (copy_to_user(uinfo, &info, info_len) ||
4262 put_user(info_len, &uattr->info.info_len))
4263 return -EFAULT;
4264
4265 return 0;
4266}
4267
4268
1e270976
MKL
4269#define BPF_OBJ_GET_INFO_BY_FD_LAST_FIELD info.info
4270
4271static int bpf_obj_get_info_by_fd(const union bpf_attr *attr,
4272 union bpf_attr __user *uattr)
4273{
4274 int ufd = attr->info.bpf_fd;
4275 struct fd f;
4276 int err;
4277
4278 if (CHECK_ATTR(BPF_OBJ_GET_INFO_BY_FD))
4279 return -EINVAL;
4280
4281 f = fdget(ufd);
4282 if (!f.file)
4283 return -EBADFD;
4284
4285 if (f.file->f_op == &bpf_prog_fops)
63960260 4286 err = bpf_prog_get_info_by_fd(f.file, f.file->private_data, attr,
1e270976
MKL
4287 uattr);
4288 else if (f.file->f_op == &bpf_map_fops)
63960260 4289 err = bpf_map_get_info_by_fd(f.file, f.file->private_data, attr,
1e270976 4290 uattr);
60197cfb 4291 else if (f.file->f_op == &btf_fops)
63960260 4292 err = bpf_btf_get_info_by_fd(f.file, f.file->private_data, attr, uattr);
f2e10bff 4293 else if (f.file->f_op == &bpf_link_fops)
63960260 4294 err = bpf_link_get_info_by_fd(f.file, f.file->private_data,
f2e10bff 4295 attr, uattr);
1e270976
MKL
4296 else
4297 err = -EINVAL;
4298
4299 fdput(f);
4300 return err;
4301}
4302
f56a653c
MKL
4303#define BPF_BTF_LOAD_LAST_FIELD btf_log_level
4304
c571bd75 4305static int bpf_btf_load(const union bpf_attr *attr, bpfptr_t uattr)
f56a653c
MKL
4306{
4307 if (CHECK_ATTR(BPF_BTF_LOAD))
4308 return -EINVAL;
4309
2c78ee89 4310 if (!bpf_capable())
f56a653c
MKL
4311 return -EPERM;
4312
c571bd75 4313 return btf_new_fd(attr, uattr);
f56a653c
MKL
4314}
4315
78958fca
MKL
4316#define BPF_BTF_GET_FD_BY_ID_LAST_FIELD btf_id
4317
4318static int bpf_btf_get_fd_by_id(const union bpf_attr *attr)
4319{
4320 if (CHECK_ATTR(BPF_BTF_GET_FD_BY_ID))
4321 return -EINVAL;
4322
4323 if (!capable(CAP_SYS_ADMIN))
4324 return -EPERM;
4325
4326 return btf_get_fd_by_id(attr->btf_id);
4327}
4328
41bdc4b4
YS
4329static int bpf_task_fd_query_copy(const union bpf_attr *attr,
4330 union bpf_attr __user *uattr,
4331 u32 prog_id, u32 fd_type,
4332 const char *buf, u64 probe_offset,
4333 u64 probe_addr)
4334{
4335 char __user *ubuf = u64_to_user_ptr(attr->task_fd_query.buf);
4336 u32 len = buf ? strlen(buf) : 0, input_len;
4337 int err = 0;
4338
4339 if (put_user(len, &uattr->task_fd_query.buf_len))
4340 return -EFAULT;
4341 input_len = attr->task_fd_query.buf_len;
4342 if (input_len && ubuf) {
4343 if (!len) {
4344 /* nothing to copy, just make ubuf NULL terminated */
4345 char zero = '\0';
4346
4347 if (put_user(zero, ubuf))
4348 return -EFAULT;
4349 } else if (input_len >= len + 1) {
4350 /* ubuf can hold the string with NULL terminator */
4351 if (copy_to_user(ubuf, buf, len + 1))
4352 return -EFAULT;
4353 } else {
4354 /* ubuf cannot hold the string with NULL terminator,
4355 * do a partial copy with NULL terminator.
4356 */
4357 char zero = '\0';
4358
4359 err = -ENOSPC;
4360 if (copy_to_user(ubuf, buf, input_len - 1))
4361 return -EFAULT;
4362 if (put_user(zero, ubuf + input_len - 1))
4363 return -EFAULT;
4364 }
4365 }
4366
4367 if (put_user(prog_id, &uattr->task_fd_query.prog_id) ||
4368 put_user(fd_type, &uattr->task_fd_query.fd_type) ||
4369 put_user(probe_offset, &uattr->task_fd_query.probe_offset) ||
4370 put_user(probe_addr, &uattr->task_fd_query.probe_addr))
4371 return -EFAULT;
4372
4373 return err;
4374}
4375
4376#define BPF_TASK_FD_QUERY_LAST_FIELD task_fd_query.probe_addr
4377
4378static int bpf_task_fd_query(const union bpf_attr *attr,
4379 union bpf_attr __user *uattr)
4380{
4381 pid_t pid = attr->task_fd_query.pid;
4382 u32 fd = attr->task_fd_query.fd;
4383 const struct perf_event *event;
41bdc4b4
YS
4384 struct task_struct *task;
4385 struct file *file;
4386 int err;
4387
4388 if (CHECK_ATTR(BPF_TASK_FD_QUERY))
4389 return -EINVAL;
4390
4391 if (!capable(CAP_SYS_ADMIN))
4392 return -EPERM;
4393
4394 if (attr->task_fd_query.flags != 0)
4395 return -EINVAL;
4396
4397 task = get_pid_task(find_vpid(pid), PIDTYPE_PID);
4398 if (!task)
4399 return -ENOENT;
4400
41bdc4b4 4401 err = 0;
b48845af
EB
4402 file = fget_task(task, fd);
4403 put_task_struct(task);
41bdc4b4 4404 if (!file)
b48845af 4405 return -EBADF;
41bdc4b4 4406
70ed506c
AN
4407 if (file->f_op == &bpf_link_fops) {
4408 struct bpf_link *link = file->private_data;
41bdc4b4 4409
a3b80e10 4410 if (link->ops == &bpf_raw_tp_link_lops) {
70ed506c
AN
4411 struct bpf_raw_tp_link *raw_tp =
4412 container_of(link, struct bpf_raw_tp_link, link);
4413 struct bpf_raw_event_map *btp = raw_tp->btp;
4414
4415 err = bpf_task_fd_query_copy(attr, uattr,
4416 raw_tp->link.prog->aux->id,
4417 BPF_FD_TYPE_RAW_TRACEPOINT,
4418 btp->tp->name, 0, 0);
4419 goto put_file;
4420 }
4421 goto out_not_supp;
41bdc4b4
YS
4422 }
4423
4424 event = perf_get_event(file);
4425 if (!IS_ERR(event)) {
4426 u64 probe_offset, probe_addr;
4427 u32 prog_id, fd_type;
4428 const char *buf;
4429
4430 err = bpf_get_perf_event_info(event, &prog_id, &fd_type,
4431 &buf, &probe_offset,
4432 &probe_addr);
4433 if (!err)
4434 err = bpf_task_fd_query_copy(attr, uattr, prog_id,
4435 fd_type, buf,
4436 probe_offset,
4437 probe_addr);
4438 goto put_file;
4439 }
4440
70ed506c 4441out_not_supp:
41bdc4b4
YS
4442 err = -ENOTSUPP;
4443put_file:
4444 fput(file);
41bdc4b4
YS
4445 return err;
4446}
4447
cb4d03ab
BV
4448#define BPF_MAP_BATCH_LAST_FIELD batch.flags
4449
4450#define BPF_DO_BATCH(fn) \
4451 do { \
4452 if (!fn) { \
4453 err = -ENOTSUPP; \
4454 goto err_put; \
4455 } \
4456 err = fn(map, attr, uattr); \
4457 } while (0)
4458
4459static int bpf_map_do_batch(const union bpf_attr *attr,
4460 union bpf_attr __user *uattr,
4461 int cmd)
4462{
353050be
DB
4463 bool has_read = cmd == BPF_MAP_LOOKUP_BATCH ||
4464 cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH;
4465 bool has_write = cmd != BPF_MAP_LOOKUP_BATCH;
cb4d03ab
BV
4466 struct bpf_map *map;
4467 int err, ufd;
4468 struct fd f;
4469
4470 if (CHECK_ATTR(BPF_MAP_BATCH))
4471 return -EINVAL;
4472
4473 ufd = attr->batch.map_fd;
4474 f = fdget(ufd);
4475 map = __bpf_map_get(f);
4476 if (IS_ERR(map))
4477 return PTR_ERR(map);
353050be
DB
4478 if (has_write)
4479 bpf_map_write_active_inc(map);
4480 if (has_read && !(map_get_sys_perms(map, f) & FMODE_CAN_READ)) {
cb4d03ab
BV
4481 err = -EPERM;
4482 goto err_put;
4483 }
353050be 4484 if (has_write && !(map_get_sys_perms(map, f) & FMODE_CAN_WRITE)) {
cb4d03ab
BV
4485 err = -EPERM;
4486 goto err_put;
4487 }
4488
4489 if (cmd == BPF_MAP_LOOKUP_BATCH)
4490 BPF_DO_BATCH(map->ops->map_lookup_batch);
05799638
YS
4491 else if (cmd == BPF_MAP_LOOKUP_AND_DELETE_BATCH)
4492 BPF_DO_BATCH(map->ops->map_lookup_and_delete_batch);
aa2e93b8
BV
4493 else if (cmd == BPF_MAP_UPDATE_BATCH)
4494 BPF_DO_BATCH(map->ops->map_update_batch);
4495 else
4496 BPF_DO_BATCH(map->ops->map_delete_batch);
cb4d03ab 4497err_put:
353050be
DB
4498 if (has_write)
4499 bpf_map_write_active_dec(map);
cb4d03ab
BV
4500 fdput(f);
4501 return err;
4502}
4503
ca74823c 4504#define BPF_LINK_CREATE_LAST_FIELD link_create.kprobe_multi.cookies
af2ac3e1 4505static int link_create(union bpf_attr *attr, bpfptr_t uattr)
af6eea57
AN
4506{
4507 enum bpf_prog_type ptype;
4508 struct bpf_prog *prog;
4509 int ret;
4510
af6eea57
AN
4511 if (CHECK_ATTR(BPF_LINK_CREATE))
4512 return -EINVAL;
4513
4a1e7c0c 4514 prog = bpf_prog_get(attr->link_create.prog_fd);
af6eea57
AN
4515 if (IS_ERR(prog))
4516 return PTR_ERR(prog);
4517
4518 ret = bpf_prog_attach_check_attach_type(prog,
4519 attr->link_create.attach_type);
4520 if (ret)
4a1e7c0c
THJ
4521 goto out;
4522
b89fbfbb
AN
4523 switch (prog->type) {
4524 case BPF_PROG_TYPE_EXT:
df86ca0d 4525 break;
b89fbfbb 4526 case BPF_PROG_TYPE_PERF_EVENT:
b89fbfbb
AN
4527 case BPF_PROG_TYPE_TRACEPOINT:
4528 if (attr->link_create.attach_type != BPF_PERF_EVENT) {
4529 ret = -EINVAL;
4530 goto out;
4531 }
b89fbfbb 4532 break;
0dcac272
JO
4533 case BPF_PROG_TYPE_KPROBE:
4534 if (attr->link_create.attach_type != BPF_PERF_EVENT &&
4535 attr->link_create.attach_type != BPF_TRACE_KPROBE_MULTI) {
4536 ret = -EINVAL;
4537 goto out;
4538 }
0dcac272 4539 break;
b89fbfbb
AN
4540 default:
4541 ptype = attach_type_to_prog_type(attr->link_create.attach_type);
4542 if (ptype == BPF_PROG_TYPE_UNSPEC || ptype != prog->type) {
4543 ret = -EINVAL;
4544 goto out;
4545 }
4546 break;
4a1e7c0c 4547 }
af6eea57 4548
df86ca0d 4549 switch (prog->type) {
af6eea57
AN
4550 case BPF_PROG_TYPE_CGROUP_SKB:
4551 case BPF_PROG_TYPE_CGROUP_SOCK:
4552 case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
4553 case BPF_PROG_TYPE_SOCK_OPS:
4554 case BPF_PROG_TYPE_CGROUP_DEVICE:
4555 case BPF_PROG_TYPE_CGROUP_SYSCTL:
4556 case BPF_PROG_TYPE_CGROUP_SOCKOPT:
4557 ret = cgroup_bpf_link_attach(attr, prog);
4558 break;
df86ca0d
AN
4559 case BPF_PROG_TYPE_EXT:
4560 ret = bpf_tracing_prog_attach(prog,
4561 attr->link_create.target_fd,
2fcc8241
KFL
4562 attr->link_create.target_btf_id,
4563 attr->link_create.tracing.cookie);
df86ca0d
AN
4564 break;
4565 case BPF_PROG_TYPE_LSM:
de4e05ca 4566 case BPF_PROG_TYPE_TRACING:
df86ca0d
AN
4567 if (attr->link_create.attach_type != prog->expected_attach_type) {
4568 ret = -EINVAL;
4569 goto out;
4570 }
4571 if (prog->expected_attach_type == BPF_TRACE_RAW_TP)
4572 ret = bpf_raw_tp_link_attach(prog, NULL);
4573 else if (prog->expected_attach_type == BPF_TRACE_ITER)
4574 ret = bpf_iter_link_attach(attr, uattr, prog);
69fd337a
SF
4575 else if (prog->expected_attach_type == BPF_LSM_CGROUP)
4576 ret = cgroup_bpf_link_attach(attr, prog);
df86ca0d
AN
4577 else
4578 ret = bpf_tracing_prog_attach(prog,
4579 attr->link_create.target_fd,
2fcc8241
KFL
4580 attr->link_create.target_btf_id,
4581 attr->link_create.tracing.cookie);
de4e05ca 4582 break;
7f045a49 4583 case BPF_PROG_TYPE_FLOW_DISSECTOR:
e9ddbb77 4584 case BPF_PROG_TYPE_SK_LOOKUP:
7f045a49
JS
4585 ret = netns_bpf_link_create(attr, prog);
4586 break;
310ad797 4587#ifdef CONFIG_NET
aa8d3a71
AN
4588 case BPF_PROG_TYPE_XDP:
4589 ret = bpf_xdp_link_attach(attr, prog);
4590 break;
b89fbfbb 4591#endif
b89fbfbb
AN
4592 case BPF_PROG_TYPE_PERF_EVENT:
4593 case BPF_PROG_TYPE_TRACEPOINT:
b89fbfbb
AN
4594 ret = bpf_perf_link_attach(attr, prog);
4595 break;
0dcac272
JO
4596 case BPF_PROG_TYPE_KPROBE:
4597 if (attr->link_create.attach_type == BPF_PERF_EVENT)
4598 ret = bpf_perf_link_attach(attr, prog);
4599 else
4600 ret = bpf_kprobe_multi_link_attach(attr, prog);
4601 break;
af6eea57
AN
4602 default:
4603 ret = -EINVAL;
4604 }
4605
4a1e7c0c 4606out:
af6eea57
AN
4607 if (ret < 0)
4608 bpf_prog_put(prog);
4609 return ret;
4610}
4611
0c991ebc
AN
4612#define BPF_LINK_UPDATE_LAST_FIELD link_update.old_prog_fd
4613
4614static int link_update(union bpf_attr *attr)
4615{
4616 struct bpf_prog *old_prog = NULL, *new_prog;
4617 struct bpf_link *link;
4618 u32 flags;
4619 int ret;
4620
0c991ebc
AN
4621 if (CHECK_ATTR(BPF_LINK_UPDATE))
4622 return -EINVAL;
4623
4624 flags = attr->link_update.flags;
4625 if (flags & ~BPF_F_REPLACE)
4626 return -EINVAL;
4627
4628 link = bpf_link_get_from_fd(attr->link_update.link_fd);
4629 if (IS_ERR(link))
4630 return PTR_ERR(link);
4631
4632 new_prog = bpf_prog_get(attr->link_update.new_prog_fd);
4adb7a4a
AN
4633 if (IS_ERR(new_prog)) {
4634 ret = PTR_ERR(new_prog);
4635 goto out_put_link;
4636 }
0c991ebc
AN
4637
4638 if (flags & BPF_F_REPLACE) {
4639 old_prog = bpf_prog_get(attr->link_update.old_prog_fd);
4640 if (IS_ERR(old_prog)) {
4641 ret = PTR_ERR(old_prog);
4642 old_prog = NULL;
4643 goto out_put_progs;
4644 }
4adb7a4a
AN
4645 } else if (attr->link_update.old_prog_fd) {
4646 ret = -EINVAL;
4647 goto out_put_progs;
0c991ebc
AN
4648 }
4649
f9d04127
AN
4650 if (link->ops->update_prog)
4651 ret = link->ops->update_prog(link, new_prog, old_prog);
4652 else
fe537393 4653 ret = -EINVAL;
0c991ebc
AN
4654
4655out_put_progs:
4656 if (old_prog)
4657 bpf_prog_put(old_prog);
4658 if (ret)
4659 bpf_prog_put(new_prog);
4adb7a4a
AN
4660out_put_link:
4661 bpf_link_put(link);
0c991ebc
AN
4662 return ret;
4663}
4664
73b11c2a
AN
4665#define BPF_LINK_DETACH_LAST_FIELD link_detach.link_fd
4666
4667static int link_detach(union bpf_attr *attr)
4668{
4669 struct bpf_link *link;
4670 int ret;
4671
4672 if (CHECK_ATTR(BPF_LINK_DETACH))
4673 return -EINVAL;
4674
4675 link = bpf_link_get_from_fd(attr->link_detach.link_fd);
4676 if (IS_ERR(link))
4677 return PTR_ERR(link);
4678
4679 if (link->ops->detach)
4680 ret = link->ops->detach(link);
4681 else
4682 ret = -EOPNOTSUPP;
4683
4684 bpf_link_put(link);
4685 return ret;
4686}
4687
005142b8 4688static struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
2d602c8c 4689{
005142b8 4690 return atomic64_fetch_add_unless(&link->refcnt, 1, 0) ? link : ERR_PTR(-ENOENT);
2d602c8c
AN
4691}
4692
005142b8 4693struct bpf_link *bpf_link_by_id(u32 id)
2d602c8c
AN
4694{
4695 struct bpf_link *link;
2d602c8c 4696
005142b8
AS
4697 if (!id)
4698 return ERR_PTR(-ENOENT);
2d602c8c
AN
4699
4700 spin_lock_bh(&link_idr_lock);
2d602c8c 4701 /* before link is "settled", ID is 0, pretend it doesn't exist yet */
005142b8 4702 link = idr_find(&link_idr, id);
2d602c8c
AN
4703 if (link) {
4704 if (link->id)
005142b8 4705 link = bpf_link_inc_not_zero(link);
2d602c8c 4706 else
005142b8 4707 link = ERR_PTR(-EAGAIN);
2d602c8c 4708 } else {
005142b8 4709 link = ERR_PTR(-ENOENT);
2d602c8c
AN
4710 }
4711 spin_unlock_bh(&link_idr_lock);
005142b8
AS
4712 return link;
4713}
2d602c8c 4714
9f883612
DD
4715struct bpf_link *bpf_link_get_curr_or_next(u32 *id)
4716{
4717 struct bpf_link *link;
4718
4719 spin_lock_bh(&link_idr_lock);
4720again:
4721 link = idr_get_next(&link_idr, id);
4722 if (link) {
4723 link = bpf_link_inc_not_zero(link);
4724 if (IS_ERR(link)) {
4725 (*id)++;
4726 goto again;
4727 }
4728 }
4729 spin_unlock_bh(&link_idr_lock);
4730
4731 return link;
4732}
4733
005142b8
AS
4734#define BPF_LINK_GET_FD_BY_ID_LAST_FIELD link_id
4735
4736static int bpf_link_get_fd_by_id(const union bpf_attr *attr)
4737{
4738 struct bpf_link *link;
4739 u32 id = attr->link_id;
4740 int fd;
4741
4742 if (CHECK_ATTR(BPF_LINK_GET_FD_BY_ID))
4743 return -EINVAL;
4744
4745 if (!capable(CAP_SYS_ADMIN))
4746 return -EPERM;
4747
4748 link = bpf_link_by_id(id);
4749 if (IS_ERR(link))
4750 return PTR_ERR(link);
2d602c8c
AN
4751
4752 fd = bpf_link_new_fd(link);
4753 if (fd < 0)
4754 bpf_link_put(link);
4755
4756 return fd;
4757}
4758
d46edd67
SL
4759DEFINE_MUTEX(bpf_stats_enabled_mutex);
4760
4761static int bpf_stats_release(struct inode *inode, struct file *file)
4762{
4763 mutex_lock(&bpf_stats_enabled_mutex);
4764 static_key_slow_dec(&bpf_stats_enabled_key.key);
4765 mutex_unlock(&bpf_stats_enabled_mutex);
4766 return 0;
4767}
4768
4769static const struct file_operations bpf_stats_fops = {
4770 .release = bpf_stats_release,
4771};
4772
4773static int bpf_enable_runtime_stats(void)
4774{
4775 int fd;
4776
4777 mutex_lock(&bpf_stats_enabled_mutex);
4778
4779 /* Set a very high limit to avoid overflow */
4780 if (static_key_count(&bpf_stats_enabled_key.key) > INT_MAX / 2) {
4781 mutex_unlock(&bpf_stats_enabled_mutex);
4782 return -EBUSY;
4783 }
4784
4785 fd = anon_inode_getfd("bpf-stats", &bpf_stats_fops, NULL, O_CLOEXEC);
4786 if (fd >= 0)
4787 static_key_slow_inc(&bpf_stats_enabled_key.key);
4788
4789 mutex_unlock(&bpf_stats_enabled_mutex);
4790 return fd;
4791}
4792
4793#define BPF_ENABLE_STATS_LAST_FIELD enable_stats.type
4794
4795static int bpf_enable_stats(union bpf_attr *attr)
4796{
4797
4798 if (CHECK_ATTR(BPF_ENABLE_STATS))
4799 return -EINVAL;
4800
4801 if (!capable(CAP_SYS_ADMIN))
4802 return -EPERM;
4803
4804 switch (attr->enable_stats.type) {
4805 case BPF_STATS_RUN_TIME:
4806 return bpf_enable_runtime_stats();
4807 default:
4808 break;
4809 }
4810 return -EINVAL;
4811}
4812
ac51d99b
YS
4813#define BPF_ITER_CREATE_LAST_FIELD iter_create.flags
4814
4815static int bpf_iter_create(union bpf_attr *attr)
4816{
4817 struct bpf_link *link;
4818 int err;
4819
4820 if (CHECK_ATTR(BPF_ITER_CREATE))
4821 return -EINVAL;
4822
4823 if (attr->iter_create.flags)
4824 return -EINVAL;
4825
4826 link = bpf_link_get_from_fd(attr->iter_create.link_fd);
4827 if (IS_ERR(link))
4828 return PTR_ERR(link);
4829
4830 err = bpf_iter_new_fd(link);
4831 bpf_link_put(link);
4832
4833 return err;
4834}
4835
ef15314a
YZ
4836#define BPF_PROG_BIND_MAP_LAST_FIELD prog_bind_map.flags
4837
4838static int bpf_prog_bind_map(union bpf_attr *attr)
4839{
4840 struct bpf_prog *prog;
4841 struct bpf_map *map;
4842 struct bpf_map **used_maps_old, **used_maps_new;
4843 int i, ret = 0;
4844
4845 if (CHECK_ATTR(BPF_PROG_BIND_MAP))
4846 return -EINVAL;
4847
4848 if (attr->prog_bind_map.flags)
4849 return -EINVAL;
4850
4851 prog = bpf_prog_get(attr->prog_bind_map.prog_fd);
4852 if (IS_ERR(prog))
4853 return PTR_ERR(prog);
4854
4855 map = bpf_map_get(attr->prog_bind_map.map_fd);
4856 if (IS_ERR(map)) {
4857 ret = PTR_ERR(map);
4858 goto out_prog_put;
4859 }
4860
4861 mutex_lock(&prog->aux->used_maps_mutex);
4862
4863 used_maps_old = prog->aux->used_maps;
4864
4865 for (i = 0; i < prog->aux->used_map_cnt; i++)
1028ae40
SF
4866 if (used_maps_old[i] == map) {
4867 bpf_map_put(map);
ef15314a 4868 goto out_unlock;
1028ae40 4869 }
ef15314a
YZ
4870
4871 used_maps_new = kmalloc_array(prog->aux->used_map_cnt + 1,
4872 sizeof(used_maps_new[0]),
4873 GFP_KERNEL);
4874 if (!used_maps_new) {
4875 ret = -ENOMEM;
4876 goto out_unlock;
4877 }
4878
4879 memcpy(used_maps_new, used_maps_old,
4880 sizeof(used_maps_old[0]) * prog->aux->used_map_cnt);
4881 used_maps_new[prog->aux->used_map_cnt] = map;
4882
4883 prog->aux->used_map_cnt++;
4884 prog->aux->used_maps = used_maps_new;
4885
4886 kfree(used_maps_old);
4887
4888out_unlock:
4889 mutex_unlock(&prog->aux->used_maps_mutex);
4890
4891 if (ret)
4892 bpf_map_put(map);
4893out_prog_put:
4894 bpf_prog_put(prog);
4895 return ret;
4896}
4897
af2ac3e1 4898static int __sys_bpf(int cmd, bpfptr_t uattr, unsigned int size)
99c55f7d 4899{
8096f229 4900 union bpf_attr attr;
c8644cd0 4901 bool capable;
99c55f7d
AS
4902 int err;
4903
c8644cd0
AM
4904 capable = bpf_capable() || !sysctl_unprivileged_bpf_disabled;
4905
4906 /* Intent here is for unprivileged_bpf_disabled to block key object
4907 * creation commands for unprivileged users; other actions depend
4908 * of fd availability and access to bpffs, so are dependent on
4909 * object creation success. Capabilities are later verified for
4910 * operations such as load and map create, so even with unprivileged
4911 * BPF disabled, capability checks are still carried out for these
4912 * and other operations.
4913 */
4914 if (!capable &&
4915 (cmd == BPF_MAP_CREATE || cmd == BPF_PROG_LOAD))
99c55f7d
AS
4916 return -EPERM;
4917
dcab51f1 4918 err = bpf_check_uarg_tail_zero(uattr, sizeof(attr), size);
1e270976
MKL
4919 if (err)
4920 return err;
4921 size = min_t(u32, size, sizeof(attr));
99c55f7d
AS
4922
4923 /* copy attributes from user space, may be less than sizeof(bpf_attr) */
8096f229 4924 memset(&attr, 0, sizeof(attr));
af2ac3e1 4925 if (copy_from_bpfptr(&attr, uattr, size) != 0)
99c55f7d
AS
4926 return -EFAULT;
4927
afdb09c7
CF
4928 err = security_bpf(cmd, &attr, size);
4929 if (err < 0)
4930 return err;
4931
99c55f7d
AS
4932 switch (cmd) {
4933 case BPF_MAP_CREATE:
4934 err = map_create(&attr);
4935 break;
db20fd2b
AS
4936 case BPF_MAP_LOOKUP_ELEM:
4937 err = map_lookup_elem(&attr);
4938 break;
4939 case BPF_MAP_UPDATE_ELEM:
af2ac3e1 4940 err = map_update_elem(&attr, uattr);
db20fd2b
AS
4941 break;
4942 case BPF_MAP_DELETE_ELEM:
b88df697 4943 err = map_delete_elem(&attr, uattr);
db20fd2b
AS
4944 break;
4945 case BPF_MAP_GET_NEXT_KEY:
4946 err = map_get_next_key(&attr);
4947 break;
87df15de
DB
4948 case BPF_MAP_FREEZE:
4949 err = map_freeze(&attr);
4950 break;
09756af4 4951 case BPF_PROG_LOAD:
838e9690 4952 err = bpf_prog_load(&attr, uattr);
09756af4 4953 break;
b2197755
DB
4954 case BPF_OBJ_PIN:
4955 err = bpf_obj_pin(&attr);
4956 break;
4957 case BPF_OBJ_GET:
4958 err = bpf_obj_get(&attr);
4959 break;
f4324551
DM
4960 case BPF_PROG_ATTACH:
4961 err = bpf_prog_attach(&attr);
4962 break;
4963 case BPF_PROG_DETACH:
4964 err = bpf_prog_detach(&attr);
4965 break;
468e2f64 4966 case BPF_PROG_QUERY:
af2ac3e1 4967 err = bpf_prog_query(&attr, uattr.user);
468e2f64 4968 break;
1cf1cae9 4969 case BPF_PROG_TEST_RUN:
af2ac3e1 4970 err = bpf_prog_test_run(&attr, uattr.user);
1cf1cae9 4971 break;
34ad5580 4972 case BPF_PROG_GET_NEXT_ID:
af2ac3e1 4973 err = bpf_obj_get_next_id(&attr, uattr.user,
34ad5580
MKL
4974 &prog_idr, &prog_idr_lock);
4975 break;
4976 case BPF_MAP_GET_NEXT_ID:
af2ac3e1 4977 err = bpf_obj_get_next_id(&attr, uattr.user,
34ad5580
MKL
4978 &map_idr, &map_idr_lock);
4979 break;
1b9ed84e 4980 case BPF_BTF_GET_NEXT_ID:
af2ac3e1 4981 err = bpf_obj_get_next_id(&attr, uattr.user,
1b9ed84e
QM
4982 &btf_idr, &btf_idr_lock);
4983 break;
b16d9aa4
MKL
4984 case BPF_PROG_GET_FD_BY_ID:
4985 err = bpf_prog_get_fd_by_id(&attr);
4986 break;
bd5f5f4e
MKL
4987 case BPF_MAP_GET_FD_BY_ID:
4988 err = bpf_map_get_fd_by_id(&attr);
4989 break;
1e270976 4990 case BPF_OBJ_GET_INFO_BY_FD:
af2ac3e1 4991 err = bpf_obj_get_info_by_fd(&attr, uattr.user);
1e270976 4992 break;
c4f6699d
AS
4993 case BPF_RAW_TRACEPOINT_OPEN:
4994 err = bpf_raw_tracepoint_open(&attr);
4995 break;
f56a653c 4996 case BPF_BTF_LOAD:
c571bd75 4997 err = bpf_btf_load(&attr, uattr);
f56a653c 4998 break;
78958fca
MKL
4999 case BPF_BTF_GET_FD_BY_ID:
5000 err = bpf_btf_get_fd_by_id(&attr);
5001 break;
41bdc4b4 5002 case BPF_TASK_FD_QUERY:
af2ac3e1 5003 err = bpf_task_fd_query(&attr, uattr.user);
41bdc4b4 5004 break;
bd513cd0
MV
5005 case BPF_MAP_LOOKUP_AND_DELETE_ELEM:
5006 err = map_lookup_and_delete_elem(&attr);
5007 break;
cb4d03ab 5008 case BPF_MAP_LOOKUP_BATCH:
af2ac3e1 5009 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_LOOKUP_BATCH);
cb4d03ab 5010 break;
05799638 5011 case BPF_MAP_LOOKUP_AND_DELETE_BATCH:
af2ac3e1 5012 err = bpf_map_do_batch(&attr, uattr.user,
05799638
YS
5013 BPF_MAP_LOOKUP_AND_DELETE_BATCH);
5014 break;
aa2e93b8 5015 case BPF_MAP_UPDATE_BATCH:
af2ac3e1 5016 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_UPDATE_BATCH);
aa2e93b8
BV
5017 break;
5018 case BPF_MAP_DELETE_BATCH:
af2ac3e1 5019 err = bpf_map_do_batch(&attr, uattr.user, BPF_MAP_DELETE_BATCH);
aa2e93b8 5020 break;
af6eea57 5021 case BPF_LINK_CREATE:
af2ac3e1 5022 err = link_create(&attr, uattr);
af6eea57 5023 break;
0c991ebc
AN
5024 case BPF_LINK_UPDATE:
5025 err = link_update(&attr);
5026 break;
2d602c8c
AN
5027 case BPF_LINK_GET_FD_BY_ID:
5028 err = bpf_link_get_fd_by_id(&attr);
5029 break;
5030 case BPF_LINK_GET_NEXT_ID:
af2ac3e1 5031 err = bpf_obj_get_next_id(&attr, uattr.user,
2d602c8c
AN
5032 &link_idr, &link_idr_lock);
5033 break;
d46edd67
SL
5034 case BPF_ENABLE_STATS:
5035 err = bpf_enable_stats(&attr);
5036 break;
ac51d99b
YS
5037 case BPF_ITER_CREATE:
5038 err = bpf_iter_create(&attr);
5039 break;
73b11c2a
AN
5040 case BPF_LINK_DETACH:
5041 err = link_detach(&attr);
5042 break;
ef15314a
YZ
5043 case BPF_PROG_BIND_MAP:
5044 err = bpf_prog_bind_map(&attr);
5045 break;
99c55f7d
AS
5046 default:
5047 err = -EINVAL;
5048 break;
5049 }
5050
5051 return err;
5052}
79a7f8bd 5053
af2ac3e1
AS
5054SYSCALL_DEFINE3(bpf, int, cmd, union bpf_attr __user *, uattr, unsigned int, size)
5055{
5056 return __sys_bpf(cmd, USER_BPFPTR(uattr), size);
5057}
5058
79a7f8bd
AS
5059static bool syscall_prog_is_valid_access(int off, int size,
5060 enum bpf_access_type type,
5061 const struct bpf_prog *prog,
5062 struct bpf_insn_access_aux *info)
5063{
5064 if (off < 0 || off >= U16_MAX)
5065 return false;
5066 if (off % size != 0)
5067 return false;
5068 return true;
5069}
5070
b1d18a75 5071BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size)
79a7f8bd 5072{
af2ac3e1
AS
5073 switch (cmd) {
5074 case BPF_MAP_CREATE:
b88df697 5075 case BPF_MAP_DELETE_ELEM:
af2ac3e1
AS
5076 case BPF_MAP_UPDATE_ELEM:
5077 case BPF_MAP_FREEZE:
b88df697 5078 case BPF_MAP_GET_FD_BY_ID:
af2ac3e1 5079 case BPF_PROG_LOAD:
c571bd75 5080 case BPF_BTF_LOAD:
b1d18a75
AS
5081 case BPF_LINK_CREATE:
5082 case BPF_RAW_TRACEPOINT_OPEN:
af2ac3e1 5083 break;
86f44fce
AS
5084 default:
5085 return -EINVAL;
5086 }
5087 return __sys_bpf(cmd, KERNEL_BPFPTR(attr), attr_size);
5088}
5089
4e4588f1
AS
5090
5091/* To shut up -Wmissing-prototypes.
5092 * This function is used by the kernel light skeleton
5093 * to load bpf programs when modules are loaded or during kernel boot.
5094 * See tools/lib/bpf/skel_internal.h
5095 */
5096int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size);
5097
86f44fce
AS
5098int kern_sys_bpf(int cmd, union bpf_attr *attr, unsigned int size)
5099{
5100 struct bpf_prog * __maybe_unused prog;
5101 struct bpf_tramp_run_ctx __maybe_unused run_ctx;
5102
5103 switch (cmd) {
b1d18a75
AS
5104#ifdef CONFIG_BPF_JIT /* __bpf_prog_enter_sleepable used by trampoline and JIT */
5105 case BPF_PROG_TEST_RUN:
5106 if (attr->test.data_in || attr->test.data_out ||
5107 attr->test.ctx_out || attr->test.duration ||
5108 attr->test.repeat || attr->test.flags)
5109 return -EINVAL;
5110
5111 prog = bpf_prog_get_type(attr->test.prog_fd, BPF_PROG_TYPE_SYSCALL);
5112 if (IS_ERR(prog))
5113 return PTR_ERR(prog);
5114
5115 if (attr->test.ctx_size_in < prog->aux->max_ctx_offset ||
5116 attr->test.ctx_size_in > U16_MAX) {
5117 bpf_prog_put(prog);
5118 return -EINVAL;
5119 }
5120
e384c7b7
KFL
5121 run_ctx.bpf_cookie = 0;
5122 run_ctx.saved_run_ctx = NULL;
5123 if (!__bpf_prog_enter_sleepable(prog, &run_ctx)) {
b1d18a75
AS
5124 /* recursion detected */
5125 bpf_prog_put(prog);
5126 return -EBUSY;
5127 }
5128 attr->test.retval = bpf_prog_run(prog, (void *) (long) attr->test.ctx_in);
e384c7b7 5129 __bpf_prog_exit_sleepable(prog, 0 /* bpf_prog_run does runtime stats */, &run_ctx);
b1d18a75
AS
5130 bpf_prog_put(prog);
5131 return 0;
5132#endif
af2ac3e1 5133 default:
86f44fce 5134 return ____bpf_sys_bpf(cmd, attr, size);
af2ac3e1 5135 }
79a7f8bd 5136}
86f44fce 5137EXPORT_SYMBOL(kern_sys_bpf);
79a7f8bd 5138
3a2daa72 5139static const struct bpf_func_proto bpf_sys_bpf_proto = {
79a7f8bd
AS
5140 .func = bpf_sys_bpf,
5141 .gpl_only = false,
5142 .ret_type = RET_INTEGER,
5143 .arg1_type = ARG_ANYTHING,
216e3cd2 5144 .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY,
79a7f8bd
AS
5145 .arg3_type = ARG_CONST_SIZE,
5146};
5147
5148const struct bpf_func_proto * __weak
5149tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5150{
5151 return bpf_base_func_proto(func_id);
5152}
5153
3abea089
AS
5154BPF_CALL_1(bpf_sys_close, u32, fd)
5155{
5156 /* When bpf program calls this helper there should not be
5157 * an fdget() without matching completed fdput().
5158 * This helper is allowed in the following callchain only:
5159 * sys_bpf->prog_test_run->bpf_prog->bpf_sys_close
5160 */
5161 return close_fd(fd);
5162}
5163
3a2daa72 5164static const struct bpf_func_proto bpf_sys_close_proto = {
3abea089
AS
5165 .func = bpf_sys_close,
5166 .gpl_only = false,
5167 .ret_type = RET_INTEGER,
5168 .arg1_type = ARG_ANYTHING,
5169};
5170
d6aef08a
KKD
5171BPF_CALL_4(bpf_kallsyms_lookup_name, const char *, name, int, name_sz, int, flags, u64 *, res)
5172{
5173 if (flags)
5174 return -EINVAL;
5175
5176 if (name_sz <= 1 || name[name_sz - 1])
5177 return -EINVAL;
5178
5179 if (!bpf_dump_raw_ok(current_cred()))
5180 return -EPERM;
5181
5182 *res = kallsyms_lookup_name(name);
5183 return *res ? 0 : -ENOENT;
5184}
5185
dc368e1c 5186static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = {
d6aef08a
KKD
5187 .func = bpf_kallsyms_lookup_name,
5188 .gpl_only = false,
5189 .ret_type = RET_INTEGER,
5190 .arg1_type = ARG_PTR_TO_MEM,
d4efb170 5191 .arg2_type = ARG_CONST_SIZE_OR_ZERO,
d6aef08a
KKD
5192 .arg3_type = ARG_ANYTHING,
5193 .arg4_type = ARG_PTR_TO_LONG,
5194};
5195
79a7f8bd
AS
5196static const struct bpf_func_proto *
5197syscall_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
5198{
5199 switch (func_id) {
5200 case BPF_FUNC_sys_bpf:
14b20b78 5201 return !perfmon_capable() ? NULL : &bpf_sys_bpf_proto;
3d78417b
AS
5202 case BPF_FUNC_btf_find_by_name_kind:
5203 return &bpf_btf_find_by_name_kind_proto;
3abea089
AS
5204 case BPF_FUNC_sys_close:
5205 return &bpf_sys_close_proto;
d6aef08a
KKD
5206 case BPF_FUNC_kallsyms_lookup_name:
5207 return &bpf_kallsyms_lookup_name_proto;
79a7f8bd
AS
5208 default:
5209 return tracing_prog_func_proto(func_id, prog);
5210 }
5211}
5212
5213const struct bpf_verifier_ops bpf_syscall_verifier_ops = {
5214 .get_func_proto = syscall_prog_func_proto,
5215 .is_valid_access = syscall_prog_is_valid_access,
5216};
5217
5218const struct bpf_prog_ops bpf_syscall_prog_ops = {
5219 .test_run = bpf_prog_test_run_syscall,
5220};
2900005e
YZ
5221
5222#ifdef CONFIG_SYSCTL
5223static int bpf_stats_handler(struct ctl_table *table, int write,
5224 void *buffer, size_t *lenp, loff_t *ppos)
5225{
5226 struct static_key *key = (struct static_key *)table->data;
5227 static int saved_val;
5228 int val, ret;
5229 struct ctl_table tmp = {
5230 .data = &val,
5231 .maxlen = sizeof(val),
5232 .mode = table->mode,
5233 .extra1 = SYSCTL_ZERO,
5234 .extra2 = SYSCTL_ONE,
5235 };
5236
5237 if (write && !capable(CAP_SYS_ADMIN))
5238 return -EPERM;
5239
5240 mutex_lock(&bpf_stats_enabled_mutex);
5241 val = saved_val;
5242 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
5243 if (write && !ret && val != saved_val) {
5244 if (val)
5245 static_key_slow_inc(key);
5246 else
5247 static_key_slow_dec(key);
5248 saved_val = val;
5249 }
5250 mutex_unlock(&bpf_stats_enabled_mutex);
5251 return ret;
5252}
5253
5254void __weak unpriv_ebpf_notify(int new_state)
5255{
5256}
5257
5258static int bpf_unpriv_handler(struct ctl_table *table, int write,
5259 void *buffer, size_t *lenp, loff_t *ppos)
5260{
5261 int ret, unpriv_enable = *(int *)table->data;
5262 bool locked_state = unpriv_enable == 1;
5263 struct ctl_table tmp = *table;
5264
5265 if (write && !capable(CAP_SYS_ADMIN))
5266 return -EPERM;
5267
5268 tmp.data = &unpriv_enable;
5269 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
5270 if (write && !ret) {
5271 if (locked_state && unpriv_enable != 1)
5272 return -EPERM;
5273 *(int *)table->data = unpriv_enable;
5274 }
5275
5276 unpriv_ebpf_notify(unpriv_enable);
5277
5278 return ret;
5279}
5280
5281static struct ctl_table bpf_syscall_table[] = {
5282 {
5283 .procname = "unprivileged_bpf_disabled",
5284 .data = &sysctl_unprivileged_bpf_disabled,
5285 .maxlen = sizeof(sysctl_unprivileged_bpf_disabled),
5286 .mode = 0644,
5287 .proc_handler = bpf_unpriv_handler,
5288 .extra1 = SYSCTL_ZERO,
5289 .extra2 = SYSCTL_TWO,
5290 },
5291 {
5292 .procname = "bpf_stats_enabled",
5293 .data = &bpf_stats_enabled_key.key,
5294 .maxlen = sizeof(bpf_stats_enabled_key),
5295 .mode = 0644,
5296 .proc_handler = bpf_stats_handler,
5297 },
5298 { }
5299};
5300
5301static int __init bpf_syscall_sysctl_init(void)
5302{
5303 register_sysctl_init("kernel", bpf_syscall_table);
5304 return 0;
5305}
5306late_initcall(bpf_syscall_sysctl_init);
5307#endif /* CONFIG_SYSCTL */