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