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