1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
3 */
4 #ifndef _LINUX_BPF_H
5 #define _LINUX_BPF_H 1
6
7 #include <uapi/linux/bpf.h>
8 #include <uapi/linux/filter.h>
9
10 #include <linux/workqueue.h>
11 #include <linux/file.h>
12 #include <linux/percpu.h>
13 #include <linux/err.h>
14 #include <linux/rbtree_latch.h>
15 #include <linux/numa.h>
16 #include <linux/mm_types.h>
17 #include <linux/wait.h>
18 #include <linux/refcount.h>
19 #include <linux/mutex.h>
20 #include <linux/module.h>
21 #include <linux/kallsyms.h>
22 #include <linux/capability.h>
23 #include <linux/sched/mm.h>
24 #include <linux/slab.h>
25 #include <linux/percpu-refcount.h>
26 #include <linux/stddef.h>
27 #include <linux/bpfptr.h>
28 #include <linux/btf.h>
29 #include <linux/rcupdate_trace.h>
30 #include <linux/static_call.h>
31 #include <linux/memcontrol.h>
32 #include <linux/cfi.h>
33 #include <linux/android_kabi.h>
34
35 struct bpf_verifier_env;
36 struct bpf_verifier_log;
37 struct perf_event;
38 struct bpf_prog;
39 struct bpf_prog_aux;
40 struct bpf_map;
41 struct bpf_arena;
42 struct sock;
43 struct seq_file;
44 struct btf;
45 struct btf_type;
46 struct exception_table_entry;
47 struct seq_operations;
48 struct bpf_iter_aux_info;
49 struct bpf_local_storage;
50 struct bpf_local_storage_map;
51 struct kobject;
52 struct mem_cgroup;
53 struct module;
54 struct bpf_func_state;
55 struct ftrace_ops;
56 struct cgroup;
57 struct bpf_token;
58 struct user_namespace;
59 struct super_block;
60 struct inode;
61
62 extern struct idr btf_idr;
63 extern spinlock_t btf_idr_lock;
64 extern struct kobject *btf_kobj;
65 extern struct bpf_mem_alloc bpf_global_ma, bpf_global_percpu_ma;
66 extern bool bpf_global_ma_set;
67
68 typedef u64 (*bpf_callback_t)(u64, u64, u64, u64, u64);
69 typedef int (*bpf_iter_init_seq_priv_t)(void *private_data,
70 struct bpf_iter_aux_info *aux);
71 typedef void (*bpf_iter_fini_seq_priv_t)(void *private_data);
72 typedef unsigned int (*bpf_func_t)(const void *,
73 const struct bpf_insn *);
74 struct bpf_iter_seq_info {
75 const struct seq_operations *seq_ops;
76 bpf_iter_init_seq_priv_t init_seq_private;
77 bpf_iter_fini_seq_priv_t fini_seq_private;
78 u32 seq_priv_size;
79 };
80
81 /* map is generic key/value storage optionally accessible by eBPF programs */
82 struct bpf_map_ops {
83 /* funcs callable from userspace (via syscall) */
84 int (*map_alloc_check)(union bpf_attr *attr);
85 struct bpf_map *(*map_alloc)(union bpf_attr *attr);
86 void (*map_release)(struct bpf_map *map, struct file *map_file);
87 void (*map_free)(struct bpf_map *map);
88 int (*map_get_next_key)(struct bpf_map *map, void *key, void *next_key);
89 void (*map_release_uref)(struct bpf_map *map);
90 void *(*map_lookup_elem_sys_only)(struct bpf_map *map, void *key);
91 int (*map_lookup_batch)(struct bpf_map *map, const union bpf_attr *attr,
92 union bpf_attr __user *uattr);
93 int (*map_lookup_and_delete_elem)(struct bpf_map *map, void *key,
94 void *value, u64 flags);
95 int (*map_lookup_and_delete_batch)(struct bpf_map *map,
96 const union bpf_attr *attr,
97 union bpf_attr __user *uattr);
98 int (*map_update_batch)(struct bpf_map *map, struct file *map_file,
99 const union bpf_attr *attr,
100 union bpf_attr __user *uattr);
101 int (*map_delete_batch)(struct bpf_map *map, const union bpf_attr *attr,
102 union bpf_attr __user *uattr);
103
104 /* funcs callable from userspace and from eBPF programs */
105 void *(*map_lookup_elem)(struct bpf_map *map, void *key);
106 long (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
107 long (*map_delete_elem)(struct bpf_map *map, void *key);
108 long (*map_push_elem)(struct bpf_map *map, void *value, u64 flags);
109 long (*map_pop_elem)(struct bpf_map *map, void *value);
110 long (*map_peek_elem)(struct bpf_map *map, void *value);
111 void *(*map_lookup_percpu_elem)(struct bpf_map *map, void *key, u32 cpu);
112
113 /* funcs called by prog_array and perf_event_array map */
114 void *(*map_fd_get_ptr)(struct bpf_map *map, struct file *map_file,
115 int fd);
116 /* If need_defer is true, the implementation should guarantee that
117 * the to-be-put element is still alive before the bpf program, which
118 * may manipulate it, exists.
119 */
120 void (*map_fd_put_ptr)(struct bpf_map *map, void *ptr, bool need_defer);
121 int (*map_gen_lookup)(struct bpf_map *map, struct bpf_insn *insn_buf);
122 u32 (*map_fd_sys_lookup_elem)(void *ptr);
123 void (*map_seq_show_elem)(struct bpf_map *map, void *key,
124 struct seq_file *m);
125 int (*map_check_btf)(const struct bpf_map *map,
126 const struct btf *btf,
127 const struct btf_type *key_type,
128 const struct btf_type *value_type);
129
130 /* Prog poke tracking helpers. */
131 int (*map_poke_track)(struct bpf_map *map, struct bpf_prog_aux *aux);
132 void (*map_poke_untrack)(struct bpf_map *map, struct bpf_prog_aux *aux);
133 void (*map_poke_run)(struct bpf_map *map, u32 key, struct bpf_prog *old,
134 struct bpf_prog *new);
135
136 /* Direct value access helpers. */
137 int (*map_direct_value_addr)(const struct bpf_map *map,
138 u64 *imm, u32 off);
139 int (*map_direct_value_meta)(const struct bpf_map *map,
140 u64 imm, u32 *off);
141 int (*map_mmap)(struct bpf_map *map, struct vm_area_struct *vma);
142 __poll_t (*map_poll)(struct bpf_map *map, struct file *filp,
143 struct poll_table_struct *pts);
144 unsigned long (*map_get_unmapped_area)(struct file *filep, unsigned long addr,
145 unsigned long len, unsigned long pgoff,
146 unsigned long flags);
147
148 /* Functions called by bpf_local_storage maps */
149 int (*map_local_storage_charge)(struct bpf_local_storage_map *smap,
150 void *owner, u32 size);
151 void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap,
152 void *owner, u32 size);
153 struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner);
154
155 /* Misc helpers.*/
156 long (*map_redirect)(struct bpf_map *map, u64 key, u64 flags);
157
158 /* map_meta_equal must be implemented for maps that can be
159 * used as an inner map. It is a runtime check to ensure
160 * an inner map can be inserted to an outer map.
161 *
162 * Some properties of the inner map has been used during the
163 * verification time. When inserting an inner map at the runtime,
164 * map_meta_equal has to ensure the inserting map has the same
165 * properties that the verifier has used earlier.
166 */
167 bool (*map_meta_equal)(const struct bpf_map *meta0,
168 const struct bpf_map *meta1);
169
170
171 int (*map_set_for_each_callback_args)(struct bpf_verifier_env *env,
172 struct bpf_func_state *caller,
173 struct bpf_func_state *callee);
174 long (*map_for_each_callback)(struct bpf_map *map,
175 bpf_callback_t callback_fn,
176 void *callback_ctx, u64 flags);
177
178 u64 (*map_mem_usage)(const struct bpf_map *map);
179
180 /* BTF id of struct allocated by map_alloc */
181 int *map_btf_id;
182
183 /* bpf_iter info used to open a seq_file */
184 const struct bpf_iter_seq_info *iter_seq_info;
185
186 ANDROID_KABI_RESERVE(1);
187 ANDROID_KABI_RESERVE(2);
188 };
189
190 enum {
191 /* Support at most 11 fields in a BTF type */
192 BTF_FIELDS_MAX = 11,
193 };
194
195 enum btf_field_type {
196 BPF_SPIN_LOCK = (1 << 0),
197 BPF_TIMER = (1 << 1),
198 BPF_KPTR_UNREF = (1 << 2),
199 BPF_KPTR_REF = (1 << 3),
200 BPF_KPTR_PERCPU = (1 << 4),
201 BPF_KPTR = BPF_KPTR_UNREF | BPF_KPTR_REF | BPF_KPTR_PERCPU,
202 BPF_LIST_HEAD = (1 << 5),
203 BPF_LIST_NODE = (1 << 6),
204 BPF_RB_ROOT = (1 << 7),
205 BPF_RB_NODE = (1 << 8),
206 BPF_GRAPH_NODE = BPF_RB_NODE | BPF_LIST_NODE,
207 BPF_GRAPH_ROOT = BPF_RB_ROOT | BPF_LIST_HEAD,
208 BPF_REFCOUNT = (1 << 9),
209 BPF_WORKQUEUE = (1 << 10),
210 };
211
212 typedef void (*btf_dtor_kfunc_t)(void *);
213
214 struct btf_field_kptr {
215 struct btf *btf;
216 struct module *module;
217 /* dtor used if btf_is_kernel(btf), otherwise the type is
218 * program-allocated, dtor is NULL, and __bpf_obj_drop_impl is used
219 */
220 btf_dtor_kfunc_t dtor;
221 u32 btf_id;
222 };
223
224 struct btf_field_graph_root {
225 struct btf *btf;
226 u32 value_btf_id;
227 u32 node_offset;
228 struct btf_record *value_rec;
229 };
230
231 struct btf_field {
232 u32 offset;
233 u32 size;
234 enum btf_field_type type;
235 union {
236 struct btf_field_kptr kptr;
237 struct btf_field_graph_root graph_root;
238 };
239 };
240
241 struct btf_record {
242 u32 cnt;
243 u32 field_mask;
244 int spin_lock_off;
245 int timer_off;
246 int wq_off;
247 int refcount_off;
248 struct btf_field fields[];
249 };
250
251 /* Non-opaque version of bpf_rb_node in uapi/linux/bpf.h */
252 struct bpf_rb_node_kern {
253 struct rb_node rb_node;
254 void *owner;
255 } __attribute__((aligned(8)));
256
257 /* Non-opaque version of bpf_list_node in uapi/linux/bpf.h */
258 struct bpf_list_node_kern {
259 struct list_head list_head;
260 void *owner;
261 } __attribute__((aligned(8)));
262
263 struct bpf_map {
264 const struct bpf_map_ops *ops;
265 struct bpf_map *inner_map_meta;
266 #ifdef CONFIG_SECURITY
267 void *security;
268 #endif
269 enum bpf_map_type map_type;
270 u32 key_size;
271 u32 value_size;
272 u32 max_entries;
273 u64 map_extra; /* any per-map-type extra fields */
274 u32 map_flags;
275 u32 id;
276 struct btf_record *record;
277 int numa_node;
278 u32 btf_key_type_id;
279 u32 btf_value_type_id;
280 u32 btf_vmlinux_value_type_id;
281 struct btf *btf;
282 #ifdef CONFIG_MEMCG
283 struct obj_cgroup *objcg;
284 #endif
285 char name[BPF_OBJ_NAME_LEN];
286 struct mutex freeze_mutex;
287 atomic64_t refcnt;
288 atomic64_t usercnt;
289 /* rcu is used before freeing and work is only used during freeing */
290 union {
291 struct work_struct work;
292 struct rcu_head rcu;
293 };
294 atomic64_t writecnt;
295 /* 'Ownership' of program-containing map is claimed by the first program
296 * that is going to use this map or by the first program which FD is
297 * stored in the map to make sure that all callers and callees have the
298 * same prog type, JITed flag and xdp_has_frags flag.
299 */
300 struct {
301 const struct btf_type *attach_func_proto;
302 spinlock_t lock;
303 enum bpf_prog_type type;
304 bool jited;
305 bool xdp_has_frags;
306 } owner;
307 bool bypass_spec_v1;
308 bool frozen; /* write-once; write-protected by freeze_mutex */
309 bool free_after_mult_rcu_gp;
310 bool free_after_rcu_gp;
311 atomic64_t sleepable_refcnt;
312 s64 __percpu *elem_count;
313 };
314
btf_field_type_name(enum btf_field_type type)315 static inline const char *btf_field_type_name(enum btf_field_type type)
316 {
317 switch (type) {
318 case BPF_SPIN_LOCK:
319 return "bpf_spin_lock";
320 case BPF_TIMER:
321 return "bpf_timer";
322 case BPF_WORKQUEUE:
323 return "bpf_wq";
324 case BPF_KPTR_UNREF:
325 case BPF_KPTR_REF:
326 return "kptr";
327 case BPF_KPTR_PERCPU:
328 return "percpu_kptr";
329 case BPF_LIST_HEAD:
330 return "bpf_list_head";
331 case BPF_LIST_NODE:
332 return "bpf_list_node";
333 case BPF_RB_ROOT:
334 return "bpf_rb_root";
335 case BPF_RB_NODE:
336 return "bpf_rb_node";
337 case BPF_REFCOUNT:
338 return "bpf_refcount";
339 default:
340 WARN_ON_ONCE(1);
341 return "unknown";
342 }
343 }
344
btf_field_type_size(enum btf_field_type type)345 static inline u32 btf_field_type_size(enum btf_field_type type)
346 {
347 switch (type) {
348 case BPF_SPIN_LOCK:
349 return sizeof(struct bpf_spin_lock);
350 case BPF_TIMER:
351 return sizeof(struct bpf_timer);
352 case BPF_WORKQUEUE:
353 return sizeof(struct bpf_wq);
354 case BPF_KPTR_UNREF:
355 case BPF_KPTR_REF:
356 case BPF_KPTR_PERCPU:
357 return sizeof(u64);
358 case BPF_LIST_HEAD:
359 return sizeof(struct bpf_list_head);
360 case BPF_LIST_NODE:
361 return sizeof(struct bpf_list_node);
362 case BPF_RB_ROOT:
363 return sizeof(struct bpf_rb_root);
364 case BPF_RB_NODE:
365 return sizeof(struct bpf_rb_node);
366 case BPF_REFCOUNT:
367 return sizeof(struct bpf_refcount);
368 default:
369 WARN_ON_ONCE(1);
370 return 0;
371 }
372 }
373
btf_field_type_align(enum btf_field_type type)374 static inline u32 btf_field_type_align(enum btf_field_type type)
375 {
376 switch (type) {
377 case BPF_SPIN_LOCK:
378 return __alignof__(struct bpf_spin_lock);
379 case BPF_TIMER:
380 return __alignof__(struct bpf_timer);
381 case BPF_WORKQUEUE:
382 return __alignof__(struct bpf_wq);
383 case BPF_KPTR_UNREF:
384 case BPF_KPTR_REF:
385 case BPF_KPTR_PERCPU:
386 return __alignof__(u64);
387 case BPF_LIST_HEAD:
388 return __alignof__(struct bpf_list_head);
389 case BPF_LIST_NODE:
390 return __alignof__(struct bpf_list_node);
391 case BPF_RB_ROOT:
392 return __alignof__(struct bpf_rb_root);
393 case BPF_RB_NODE:
394 return __alignof__(struct bpf_rb_node);
395 case BPF_REFCOUNT:
396 return __alignof__(struct bpf_refcount);
397 default:
398 WARN_ON_ONCE(1);
399 return 0;
400 }
401 }
402
bpf_obj_init_field(const struct btf_field * field,void * addr)403 static inline void bpf_obj_init_field(const struct btf_field *field, void *addr)
404 {
405 memset(addr, 0, field->size);
406
407 switch (field->type) {
408 case BPF_REFCOUNT:
409 refcount_set((refcount_t *)addr, 1);
410 break;
411 case BPF_RB_NODE:
412 RB_CLEAR_NODE((struct rb_node *)addr);
413 break;
414 case BPF_LIST_HEAD:
415 case BPF_LIST_NODE:
416 INIT_LIST_HEAD((struct list_head *)addr);
417 break;
418 case BPF_RB_ROOT:
419 /* RB_ROOT_CACHED 0-inits, no need to do anything after memset */
420 case BPF_SPIN_LOCK:
421 case BPF_TIMER:
422 case BPF_WORKQUEUE:
423 case BPF_KPTR_UNREF:
424 case BPF_KPTR_REF:
425 case BPF_KPTR_PERCPU:
426 break;
427 default:
428 WARN_ON_ONCE(1);
429 return;
430 }
431 }
432
btf_record_has_field(const struct btf_record * rec,enum btf_field_type type)433 static inline bool btf_record_has_field(const struct btf_record *rec, enum btf_field_type type)
434 {
435 if (IS_ERR_OR_NULL(rec))
436 return false;
437 return rec->field_mask & type;
438 }
439
bpf_obj_init(const struct btf_record * rec,void * obj)440 static inline void bpf_obj_init(const struct btf_record *rec, void *obj)
441 {
442 int i;
443
444 if (IS_ERR_OR_NULL(rec))
445 return;
446 for (i = 0; i < rec->cnt; i++)
447 bpf_obj_init_field(&rec->fields[i], obj + rec->fields[i].offset);
448 }
449
450 /* 'dst' must be a temporary buffer and should not point to memory that is being
451 * used in parallel by a bpf program or bpf syscall, otherwise the access from
452 * the bpf program or bpf syscall may be corrupted by the reinitialization,
453 * leading to weird problems. Even 'dst' is newly-allocated from bpf memory
454 * allocator, it is still possible for 'dst' to be used in parallel by a bpf
455 * program or bpf syscall.
456 */
check_and_init_map_value(struct bpf_map * map,void * dst)457 static inline void check_and_init_map_value(struct bpf_map *map, void *dst)
458 {
459 bpf_obj_init(map->record, dst);
460 }
461
462 /* memcpy that is used with 8-byte aligned pointers, power-of-8 size and
463 * forced to use 'long' read/writes to try to atomically copy long counters.
464 * Best-effort only. No barriers here, since it _will_ race with concurrent
465 * updates from BPF programs. Called from bpf syscall and mostly used with
466 * size 8 or 16 bytes, so ask compiler to inline it.
467 */
bpf_long_memcpy(void * dst,const void * src,u32 size)468 static inline void bpf_long_memcpy(void *dst, const void *src, u32 size)
469 {
470 const long *lsrc = src;
471 long *ldst = dst;
472
473 size /= sizeof(long);
474 while (size--)
475 data_race(*ldst++ = *lsrc++);
476 }
477
478 /* copy everything but bpf_spin_lock, bpf_timer, and kptrs. There could be one of each. */
bpf_obj_memcpy(struct btf_record * rec,void * dst,void * src,u32 size,bool long_memcpy)479 static inline void bpf_obj_memcpy(struct btf_record *rec,
480 void *dst, void *src, u32 size,
481 bool long_memcpy)
482 {
483 u32 curr_off = 0;
484 int i;
485
486 if (IS_ERR_OR_NULL(rec)) {
487 if (long_memcpy)
488 bpf_long_memcpy(dst, src, round_up(size, 8));
489 else
490 memcpy(dst, src, size);
491 return;
492 }
493
494 for (i = 0; i < rec->cnt; i++) {
495 u32 next_off = rec->fields[i].offset;
496 u32 sz = next_off - curr_off;
497
498 memcpy(dst + curr_off, src + curr_off, sz);
499 curr_off += rec->fields[i].size + sz;
500 }
501 memcpy(dst + curr_off, src + curr_off, size - curr_off);
502 }
503
copy_map_value(struct bpf_map * map,void * dst,void * src)504 static inline void copy_map_value(struct bpf_map *map, void *dst, void *src)
505 {
506 bpf_obj_memcpy(map->record, dst, src, map->value_size, false);
507 }
508
copy_map_value_long(struct bpf_map * map,void * dst,void * src)509 static inline void copy_map_value_long(struct bpf_map *map, void *dst, void *src)
510 {
511 bpf_obj_memcpy(map->record, dst, src, map->value_size, true);
512 }
513
bpf_obj_memzero(struct btf_record * rec,void * dst,u32 size)514 static inline void bpf_obj_memzero(struct btf_record *rec, void *dst, u32 size)
515 {
516 u32 curr_off = 0;
517 int i;
518
519 if (IS_ERR_OR_NULL(rec)) {
520 memset(dst, 0, size);
521 return;
522 }
523
524 for (i = 0; i < rec->cnt; i++) {
525 u32 next_off = rec->fields[i].offset;
526 u32 sz = next_off - curr_off;
527
528 memset(dst + curr_off, 0, sz);
529 curr_off += rec->fields[i].size + sz;
530 }
531 memset(dst + curr_off, 0, size - curr_off);
532 }
533
zero_map_value(struct bpf_map * map,void * dst)534 static inline void zero_map_value(struct bpf_map *map, void *dst)
535 {
536 bpf_obj_memzero(map->record, dst, map->value_size);
537 }
538
539 void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
540 bool lock_src);
541 void bpf_timer_cancel_and_free(void *timer);
542 void bpf_wq_cancel_and_free(void *timer);
543 void bpf_list_head_free(const struct btf_field *field, void *list_head,
544 struct bpf_spin_lock *spin_lock);
545 void bpf_rb_root_free(const struct btf_field *field, void *rb_root,
546 struct bpf_spin_lock *spin_lock);
547 u64 bpf_arena_get_kern_vm_start(struct bpf_arena *arena);
548 u64 bpf_arena_get_user_vm_start(struct bpf_arena *arena);
549 int bpf_obj_name_cpy(char *dst, const char *src, unsigned int size);
550
551 struct bpf_offload_dev;
552 struct bpf_offloaded_map;
553
554 struct bpf_map_dev_ops {
555 int (*map_get_next_key)(struct bpf_offloaded_map *map,
556 void *key, void *next_key);
557 int (*map_lookup_elem)(struct bpf_offloaded_map *map,
558 void *key, void *value);
559 int (*map_update_elem)(struct bpf_offloaded_map *map,
560 void *key, void *value, u64 flags);
561 int (*map_delete_elem)(struct bpf_offloaded_map *map, void *key);
562
563 ANDROID_KABI_RESERVE(1);
564 };
565
566 struct bpf_offloaded_map {
567 struct bpf_map map;
568 struct net_device *netdev;
569 const struct bpf_map_dev_ops *dev_ops;
570 void *dev_priv;
571 struct list_head offloads;
572 };
573
map_to_offmap(struct bpf_map * map)574 static inline struct bpf_offloaded_map *map_to_offmap(struct bpf_map *map)
575 {
576 return container_of(map, struct bpf_offloaded_map, map);
577 }
578
bpf_map_offload_neutral(const struct bpf_map * map)579 static inline bool bpf_map_offload_neutral(const struct bpf_map *map)
580 {
581 return map->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
582 }
583
bpf_map_support_seq_show(const struct bpf_map * map)584 static inline bool bpf_map_support_seq_show(const struct bpf_map *map)
585 {
586 return (map->btf_value_type_id || map->btf_vmlinux_value_type_id) &&
587 map->ops->map_seq_show_elem;
588 }
589
590 int map_check_no_btf(const struct bpf_map *map,
591 const struct btf *btf,
592 const struct btf_type *key_type,
593 const struct btf_type *value_type);
594
595 bool bpf_map_meta_equal(const struct bpf_map *meta0,
596 const struct bpf_map *meta1);
597
598 extern const struct bpf_map_ops bpf_map_offload_ops;
599
600 /* bpf_type_flag contains a set of flags that are applicable to the values of
601 * arg_type, ret_type and reg_type. For example, a pointer value may be null,
602 * or a memory is read-only. We classify types into two categories: base types
603 * and extended types. Extended types are base types combined with a type flag.
604 *
605 * Currently there are no more than 32 base types in arg_type, ret_type and
606 * reg_types.
607 */
608 #define BPF_BASE_TYPE_BITS 8
609
610 enum bpf_type_flag {
611 /* PTR may be NULL. */
612 PTR_MAYBE_NULL = BIT(0 + BPF_BASE_TYPE_BITS),
613
614 /* MEM is read-only. When applied on bpf_arg, it indicates the arg is
615 * compatible with both mutable and immutable memory.
616 */
617 MEM_RDONLY = BIT(1 + BPF_BASE_TYPE_BITS),
618
619 /* MEM points to BPF ring buffer reservation. */
620 MEM_RINGBUF = BIT(2 + BPF_BASE_TYPE_BITS),
621
622 /* MEM is in user address space. */
623 MEM_USER = BIT(3 + BPF_BASE_TYPE_BITS),
624
625 /* MEM is a percpu memory. MEM_PERCPU tags PTR_TO_BTF_ID. When tagged
626 * with MEM_PERCPU, PTR_TO_BTF_ID _cannot_ be directly accessed. In
627 * order to drop this tag, it must be passed into bpf_per_cpu_ptr()
628 * or bpf_this_cpu_ptr(), which will return the pointer corresponding
629 * to the specified cpu.
630 */
631 MEM_PERCPU = BIT(4 + BPF_BASE_TYPE_BITS),
632
633 /* Indicates that the argument will be released. */
634 OBJ_RELEASE = BIT(5 + BPF_BASE_TYPE_BITS),
635
636 /* PTR is not trusted. This is only used with PTR_TO_BTF_ID, to mark
637 * unreferenced and referenced kptr loaded from map value using a load
638 * instruction, so that they can only be dereferenced but not escape the
639 * BPF program into the kernel (i.e. cannot be passed as arguments to
640 * kfunc or bpf helpers).
641 */
642 PTR_UNTRUSTED = BIT(6 + BPF_BASE_TYPE_BITS),
643
644 /* MEM can be uninitialized. */
645 MEM_UNINIT = BIT(7 + BPF_BASE_TYPE_BITS),
646
647 /* DYNPTR points to memory local to the bpf program. */
648 DYNPTR_TYPE_LOCAL = BIT(8 + BPF_BASE_TYPE_BITS),
649
650 /* DYNPTR points to a kernel-produced ringbuf record. */
651 DYNPTR_TYPE_RINGBUF = BIT(9 + BPF_BASE_TYPE_BITS),
652
653 /* Size is known at compile time. */
654 MEM_FIXED_SIZE = BIT(10 + BPF_BASE_TYPE_BITS),
655
656 /* MEM is of an allocated object of type in program BTF. This is used to
657 * tag PTR_TO_BTF_ID allocated using bpf_obj_new.
658 */
659 MEM_ALLOC = BIT(11 + BPF_BASE_TYPE_BITS),
660
661 /* PTR was passed from the kernel in a trusted context, and may be
662 * passed to KF_TRUSTED_ARGS kfuncs or BPF helper functions.
663 * Confusingly, this is _not_ the opposite of PTR_UNTRUSTED above.
664 * PTR_UNTRUSTED refers to a kptr that was read directly from a map
665 * without invoking bpf_kptr_xchg(). What we really need to know is
666 * whether a pointer is safe to pass to a kfunc or BPF helper function.
667 * While PTR_UNTRUSTED pointers are unsafe to pass to kfuncs and BPF
668 * helpers, they do not cover all possible instances of unsafe
669 * pointers. For example, a pointer that was obtained from walking a
670 * struct will _not_ get the PTR_UNTRUSTED type modifier, despite the
671 * fact that it may be NULL, invalid, etc. This is due to backwards
672 * compatibility requirements, as this was the behavior that was first
673 * introduced when kptrs were added. The behavior is now considered
674 * deprecated, and PTR_UNTRUSTED will eventually be removed.
675 *
676 * PTR_TRUSTED, on the other hand, is a pointer that the kernel
677 * guarantees to be valid and safe to pass to kfuncs and BPF helpers.
678 * For example, pointers passed to tracepoint arguments are considered
679 * PTR_TRUSTED, as are pointers that are passed to struct_ops
680 * callbacks. As alluded to above, pointers that are obtained from
681 * walking PTR_TRUSTED pointers are _not_ trusted. For example, if a
682 * struct task_struct *task is PTR_TRUSTED, then accessing
683 * task->last_wakee will lose the PTR_TRUSTED modifier when it's stored
684 * in a BPF register. Similarly, pointers passed to certain programs
685 * types such as kretprobes are not guaranteed to be valid, as they may
686 * for example contain an object that was recently freed.
687 */
688 PTR_TRUSTED = BIT(12 + BPF_BASE_TYPE_BITS),
689
690 /* MEM is tagged with rcu and memory access needs rcu_read_lock protection. */
691 MEM_RCU = BIT(13 + BPF_BASE_TYPE_BITS),
692
693 /* Used to tag PTR_TO_BTF_ID | MEM_ALLOC references which are non-owning.
694 * Currently only valid for linked-list and rbtree nodes. If the nodes
695 * have a bpf_refcount_field, they must be tagged MEM_RCU as well.
696 */
697 NON_OWN_REF = BIT(14 + BPF_BASE_TYPE_BITS),
698
699 /* DYNPTR points to sk_buff */
700 DYNPTR_TYPE_SKB = BIT(15 + BPF_BASE_TYPE_BITS),
701
702 /* DYNPTR points to xdp_buff */
703 DYNPTR_TYPE_XDP = BIT(16 + BPF_BASE_TYPE_BITS),
704
705 /* Memory must be aligned on some architectures, used in combination with
706 * MEM_FIXED_SIZE.
707 */
708 MEM_ALIGNED = BIT(17 + BPF_BASE_TYPE_BITS),
709
710 /* MEM is being written to, often combined with MEM_UNINIT. Non-presence
711 * of MEM_WRITE means that MEM is only being read. MEM_WRITE without the
712 * MEM_UNINIT means that memory needs to be initialized since it is also
713 * read.
714 */
715 MEM_WRITE = BIT(18 + BPF_BASE_TYPE_BITS),
716
717 __BPF_TYPE_FLAG_MAX,
718 __BPF_TYPE_LAST_FLAG = __BPF_TYPE_FLAG_MAX - 1,
719 };
720
721 #define DYNPTR_TYPE_FLAG_MASK (DYNPTR_TYPE_LOCAL | DYNPTR_TYPE_RINGBUF | DYNPTR_TYPE_SKB \
722 | DYNPTR_TYPE_XDP)
723
724 /* Max number of base types. */
725 #define BPF_BASE_TYPE_LIMIT (1UL << BPF_BASE_TYPE_BITS)
726
727 /* Max number of all types. */
728 #define BPF_TYPE_LIMIT (__BPF_TYPE_LAST_FLAG | (__BPF_TYPE_LAST_FLAG - 1))
729
730 /* function argument constraints */
731 enum bpf_arg_type {
732 ARG_DONTCARE = 0, /* unused argument in helper function */
733
734 /* the following constraints used to prototype
735 * bpf_map_lookup/update/delete_elem() functions
736 */
737 ARG_CONST_MAP_PTR, /* const argument used as pointer to bpf_map */
738 ARG_PTR_TO_MAP_KEY, /* pointer to stack used as map key */
739 ARG_PTR_TO_MAP_VALUE, /* pointer to stack used as map value */
740
741 /* Used to prototype bpf_memcmp() and other functions that access data
742 * on eBPF program stack
743 */
744 ARG_PTR_TO_MEM, /* pointer to valid memory (stack, packet, map value) */
745 ARG_PTR_TO_ARENA,
746
747 ARG_CONST_SIZE, /* number of bytes accessed from memory */
748 ARG_CONST_SIZE_OR_ZERO, /* number of bytes accessed from memory or 0 */
749
750 ARG_PTR_TO_CTX, /* pointer to context */
751 ARG_ANYTHING, /* any (initialized) argument is ok */
752 ARG_PTR_TO_SPIN_LOCK, /* pointer to bpf_spin_lock */
753 ARG_PTR_TO_SOCK_COMMON, /* pointer to sock_common */
754 ARG_PTR_TO_SOCKET, /* pointer to bpf_sock (fullsock) */
755 ARG_PTR_TO_BTF_ID, /* pointer to in-kernel struct */
756 ARG_PTR_TO_RINGBUF_MEM, /* pointer to dynamically reserved ringbuf memory */
757 ARG_CONST_ALLOC_SIZE_OR_ZERO, /* number of allocated bytes requested */
758 ARG_PTR_TO_BTF_ID_SOCK_COMMON, /* pointer to in-kernel sock_common or bpf-mirrored bpf_sock */
759 ARG_PTR_TO_PERCPU_BTF_ID, /* pointer to in-kernel percpu type */
760 ARG_PTR_TO_FUNC, /* pointer to a bpf program function */
761 ARG_PTR_TO_STACK, /* pointer to stack */
762 ARG_PTR_TO_CONST_STR, /* pointer to a null terminated read-only string */
763 ARG_PTR_TO_TIMER, /* pointer to bpf_timer */
764 ARG_KPTR_XCHG_DEST, /* pointer to destination that kptrs are bpf_kptr_xchg'd into */
765 ARG_PTR_TO_DYNPTR, /* pointer to bpf_dynptr. See bpf_type_flag for dynptr type */
766 __BPF_ARG_TYPE_MAX,
767
768 /* Extended arg_types. */
769 ARG_PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_MAP_VALUE,
770 ARG_PTR_TO_MEM_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_MEM,
771 ARG_PTR_TO_CTX_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_CTX,
772 ARG_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_SOCKET,
773 ARG_PTR_TO_STACK_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_STACK,
774 ARG_PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | ARG_PTR_TO_BTF_ID,
775 /* Pointer to memory does not need to be initialized, since helper function
776 * fills all bytes or clears them in error case.
777 */
778 ARG_PTR_TO_UNINIT_MEM = MEM_UNINIT | MEM_WRITE | ARG_PTR_TO_MEM,
779 /* Pointer to valid memory of size known at compile time. */
780 ARG_PTR_TO_FIXED_SIZE_MEM = MEM_FIXED_SIZE | ARG_PTR_TO_MEM,
781
782 /* This must be the last entry. Its purpose is to ensure the enum is
783 * wide enough to hold the higher bits reserved for bpf_type_flag.
784 */
785 __BPF_ARG_TYPE_LIMIT = BPF_TYPE_LIMIT,
786 };
787 static_assert(__BPF_ARG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
788
789 /* type of values returned from helper functions */
790 enum bpf_return_type {
791 RET_INTEGER, /* function returns integer */
792 RET_VOID, /* function doesn't return anything */
793 RET_PTR_TO_MAP_VALUE, /* returns a pointer to map elem value */
794 RET_PTR_TO_SOCKET, /* returns a pointer to a socket */
795 RET_PTR_TO_TCP_SOCK, /* returns a pointer to a tcp_sock */
796 RET_PTR_TO_SOCK_COMMON, /* returns a pointer to a sock_common */
797 RET_PTR_TO_MEM, /* returns a pointer to memory */
798 RET_PTR_TO_MEM_OR_BTF_ID, /* returns a pointer to a valid memory or a btf_id */
799 RET_PTR_TO_BTF_ID, /* returns a pointer to a btf_id */
800 __BPF_RET_TYPE_MAX,
801
802 /* Extended ret_types. */
803 RET_PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_MAP_VALUE,
804 RET_PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCKET,
805 RET_PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_TCP_SOCK,
806 RET_PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_SOCK_COMMON,
807 RET_PTR_TO_RINGBUF_MEM_OR_NULL = PTR_MAYBE_NULL | MEM_RINGBUF | RET_PTR_TO_MEM,
808 RET_PTR_TO_DYNPTR_MEM_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_MEM,
809 RET_PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | RET_PTR_TO_BTF_ID,
810 RET_PTR_TO_BTF_ID_TRUSTED = PTR_TRUSTED | RET_PTR_TO_BTF_ID,
811
812 /* This must be the last entry. Its purpose is to ensure the enum is
813 * wide enough to hold the higher bits reserved for bpf_type_flag.
814 */
815 __BPF_RET_TYPE_LIMIT = BPF_TYPE_LIMIT,
816 };
817 static_assert(__BPF_RET_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
818
819 /* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs
820 * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL
821 * instructions after verifying
822 */
823 struct bpf_func_proto {
824 u64 (*func)(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
825 bool gpl_only;
826 bool pkt_access;
827 bool might_sleep;
828 /* set to true if helper follows contract for llvm
829 * attribute bpf_fastcall:
830 * - void functions do not scratch r0
831 * - functions taking N arguments scratch only registers r1-rN
832 */
833 bool allow_fastcall;
834 enum bpf_return_type ret_type;
835 union {
836 struct {
837 enum bpf_arg_type arg1_type;
838 enum bpf_arg_type arg2_type;
839 enum bpf_arg_type arg3_type;
840 enum bpf_arg_type arg4_type;
841 enum bpf_arg_type arg5_type;
842 };
843 enum bpf_arg_type arg_type[5];
844 };
845 union {
846 struct {
847 u32 *arg1_btf_id;
848 u32 *arg2_btf_id;
849 u32 *arg3_btf_id;
850 u32 *arg4_btf_id;
851 u32 *arg5_btf_id;
852 };
853 u32 *arg_btf_id[5];
854 struct {
855 size_t arg1_size;
856 size_t arg2_size;
857 size_t arg3_size;
858 size_t arg4_size;
859 size_t arg5_size;
860 };
861 size_t arg_size[5];
862 };
863 int *ret_btf_id; /* return value btf_id */
864 bool (*allowed)(const struct bpf_prog *prog);
865 };
866
867 /* bpf_context is intentionally undefined structure. Pointer to bpf_context is
868 * the first argument to eBPF programs.
869 * For socket filters: 'struct bpf_context *' == 'struct sk_buff *'
870 */
871 struct bpf_context;
872
873 enum bpf_access_type {
874 BPF_READ = 1,
875 BPF_WRITE = 2
876 };
877
878 /* types of values stored in eBPF registers */
879 /* Pointer types represent:
880 * pointer
881 * pointer + imm
882 * pointer + (u16) var
883 * pointer + (u16) var + imm
884 * if (range > 0) then [ptr, ptr + range - off) is safe to access
885 * if (id > 0) means that some 'var' was added
886 * if (off > 0) means that 'imm' was added
887 */
888 enum bpf_reg_type {
889 NOT_INIT = 0, /* nothing was written into register */
890 SCALAR_VALUE, /* reg doesn't contain a valid pointer */
891 PTR_TO_CTX, /* reg points to bpf_context */
892 CONST_PTR_TO_MAP, /* reg points to struct bpf_map */
893 PTR_TO_MAP_VALUE, /* reg points to map element value */
894 PTR_TO_MAP_KEY, /* reg points to a map element key */
895 PTR_TO_STACK, /* reg == frame_pointer + offset */
896 PTR_TO_PACKET_META, /* skb->data - meta_len */
897 PTR_TO_PACKET, /* reg points to skb->data */
898 PTR_TO_PACKET_END, /* skb->data + headlen */
899 PTR_TO_FLOW_KEYS, /* reg points to bpf_flow_keys */
900 PTR_TO_SOCKET, /* reg points to struct bpf_sock */
901 PTR_TO_SOCK_COMMON, /* reg points to sock_common */
902 PTR_TO_TCP_SOCK, /* reg points to struct tcp_sock */
903 PTR_TO_TP_BUFFER, /* reg points to a writable raw tp's buffer */
904 PTR_TO_XDP_SOCK, /* reg points to struct xdp_sock */
905 /* PTR_TO_BTF_ID points to a kernel struct that does not need
906 * to be null checked by the BPF program. This does not imply the
907 * pointer is _not_ null and in practice this can easily be a null
908 * pointer when reading pointer chains. The assumption is program
909 * context will handle null pointer dereference typically via fault
910 * handling. The verifier must keep this in mind and can make no
911 * assumptions about null or non-null when doing branch analysis.
912 * Further, when passed into helpers the helpers can not, without
913 * additional context, assume the value is non-null.
914 */
915 PTR_TO_BTF_ID,
916 /* PTR_TO_BTF_ID_OR_NULL points to a kernel struct that has not
917 * been checked for null. Used primarily to inform the verifier
918 * an explicit null check is required for this struct.
919 */
920 PTR_TO_MEM, /* reg points to valid memory region */
921 PTR_TO_ARENA,
922 PTR_TO_BUF, /* reg points to a read/write buffer */
923 PTR_TO_FUNC, /* reg points to a bpf program function */
924 CONST_PTR_TO_DYNPTR, /* reg points to a const struct bpf_dynptr */
925 __BPF_REG_TYPE_MAX,
926
927 /* Extended reg_types. */
928 PTR_TO_MAP_VALUE_OR_NULL = PTR_MAYBE_NULL | PTR_TO_MAP_VALUE,
929 PTR_TO_SOCKET_OR_NULL = PTR_MAYBE_NULL | PTR_TO_SOCKET,
930 PTR_TO_SOCK_COMMON_OR_NULL = PTR_MAYBE_NULL | PTR_TO_SOCK_COMMON,
931 PTR_TO_TCP_SOCK_OR_NULL = PTR_MAYBE_NULL | PTR_TO_TCP_SOCK,
932 PTR_TO_BTF_ID_OR_NULL = PTR_MAYBE_NULL | PTR_TO_BTF_ID,
933
934 /* This must be the last entry. Its purpose is to ensure the enum is
935 * wide enough to hold the higher bits reserved for bpf_type_flag.
936 */
937 __BPF_REG_TYPE_LIMIT = BPF_TYPE_LIMIT,
938 };
939 static_assert(__BPF_REG_TYPE_MAX <= BPF_BASE_TYPE_LIMIT);
940
941 /* The information passed from prog-specific *_is_valid_access
942 * back to the verifier.
943 */
944 struct bpf_insn_access_aux {
945 enum bpf_reg_type reg_type;
946 bool is_ldsx;
947 union {
948 int ctx_field_size;
949 struct {
950 struct btf *btf;
951 u32 btf_id;
952 };
953 };
954 struct bpf_verifier_log *log; /* for verbose logs */
955 bool is_retval; /* is accessing function return value ? */
956 };
957
958 static inline void
bpf_ctx_record_field_size(struct bpf_insn_access_aux * aux,u32 size)959 bpf_ctx_record_field_size(struct bpf_insn_access_aux *aux, u32 size)
960 {
961 aux->ctx_field_size = size;
962 }
963
bpf_is_ldimm64(const struct bpf_insn * insn)964 static bool bpf_is_ldimm64(const struct bpf_insn *insn)
965 {
966 return insn->code == (BPF_LD | BPF_IMM | BPF_DW);
967 }
968
bpf_pseudo_func(const struct bpf_insn * insn)969 static inline bool bpf_pseudo_func(const struct bpf_insn *insn)
970 {
971 return bpf_is_ldimm64(insn) && insn->src_reg == BPF_PSEUDO_FUNC;
972 }
973
974 struct bpf_prog_ops {
975 int (*test_run)(struct bpf_prog *prog, const union bpf_attr *kattr,
976 union bpf_attr __user *uattr);
977 };
978
979 struct bpf_reg_state;
980 struct bpf_verifier_ops {
981 /* return eBPF function prototype for verification */
982 const struct bpf_func_proto *
983 (*get_func_proto)(enum bpf_func_id func_id,
984 const struct bpf_prog *prog);
985
986 /* return true if 'size' wide access at offset 'off' within bpf_context
987 * with 'type' (read or write) is allowed
988 */
989 bool (*is_valid_access)(int off, int size, enum bpf_access_type type,
990 const struct bpf_prog *prog,
991 struct bpf_insn_access_aux *info);
992 int (*gen_prologue)(struct bpf_insn *insn, bool direct_write,
993 const struct bpf_prog *prog);
994 int (*gen_epilogue)(struct bpf_insn *insn, const struct bpf_prog *prog,
995 s16 ctx_stack_off);
996 int (*gen_ld_abs)(const struct bpf_insn *orig,
997 struct bpf_insn *insn_buf);
998 u32 (*convert_ctx_access)(enum bpf_access_type type,
999 const struct bpf_insn *src,
1000 struct bpf_insn *dst,
1001 struct bpf_prog *prog, u32 *target_size);
1002 int (*btf_struct_access)(struct bpf_verifier_log *log,
1003 const struct bpf_reg_state *reg,
1004 int off, int size);
1005 ANDROID_KABI_RESERVE(1);
1006 };
1007
1008 struct bpf_prog_offload_ops {
1009 /* verifier basic callbacks */
1010 int (*insn_hook)(struct bpf_verifier_env *env,
1011 int insn_idx, int prev_insn_idx);
1012 int (*finalize)(struct bpf_verifier_env *env);
1013 /* verifier optimization callbacks (called after .finalize) */
1014 int (*replace_insn)(struct bpf_verifier_env *env, u32 off,
1015 struct bpf_insn *insn);
1016 int (*remove_insns)(struct bpf_verifier_env *env, u32 off, u32 cnt);
1017 /* program management callbacks */
1018 int (*prepare)(struct bpf_prog *prog);
1019 int (*translate)(struct bpf_prog *prog);
1020 void (*destroy)(struct bpf_prog *prog);
1021 ANDROID_KABI_RESERVE(1);
1022 };
1023
1024 struct bpf_prog_offload {
1025 struct bpf_prog *prog;
1026 struct net_device *netdev;
1027 struct bpf_offload_dev *offdev;
1028 void *dev_priv;
1029 struct list_head offloads;
1030 bool dev_state;
1031 bool opt_failed;
1032 void *jited_image;
1033 u32 jited_len;
1034 ANDROID_KABI_RESERVE(1);
1035 };
1036
1037 enum bpf_cgroup_storage_type {
1038 BPF_CGROUP_STORAGE_SHARED,
1039 BPF_CGROUP_STORAGE_PERCPU,
1040 __BPF_CGROUP_STORAGE_MAX
1041 };
1042
1043 #define MAX_BPF_CGROUP_STORAGE_TYPE __BPF_CGROUP_STORAGE_MAX
1044
1045 /* The longest tracepoint has 12 args.
1046 * See include/trace/bpf_probe.h
1047 */
1048 #define MAX_BPF_FUNC_ARGS 12
1049
1050 /* The maximum number of arguments passed through registers
1051 * a single function may have.
1052 */
1053 #define MAX_BPF_FUNC_REG_ARGS 5
1054
1055 /* The argument is a structure. */
1056 #define BTF_FMODEL_STRUCT_ARG BIT(0)
1057
1058 /* The argument is signed. */
1059 #define BTF_FMODEL_SIGNED_ARG BIT(1)
1060
1061 struct btf_func_model {
1062 u8 ret_size;
1063 u8 ret_flags;
1064 u8 nr_args;
1065 u8 arg_size[MAX_BPF_FUNC_ARGS];
1066 u8 arg_flags[MAX_BPF_FUNC_ARGS];
1067 };
1068
1069 /* Restore arguments before returning from trampoline to let original function
1070 * continue executing. This flag is used for fentry progs when there are no
1071 * fexit progs.
1072 */
1073 #define BPF_TRAMP_F_RESTORE_REGS BIT(0)
1074 /* Call original function after fentry progs, but before fexit progs.
1075 * Makes sense for fentry/fexit, normal calls and indirect calls.
1076 */
1077 #define BPF_TRAMP_F_CALL_ORIG BIT(1)
1078 /* Skip current frame and return to parent. Makes sense for fentry/fexit
1079 * programs only. Should not be used with normal calls and indirect calls.
1080 */
1081 #define BPF_TRAMP_F_SKIP_FRAME BIT(2)
1082 /* Store IP address of the caller on the trampoline stack,
1083 * so it's available for trampoline's programs.
1084 */
1085 #define BPF_TRAMP_F_IP_ARG BIT(3)
1086 /* Return the return value of fentry prog. Only used by bpf_struct_ops. */
1087 #define BPF_TRAMP_F_RET_FENTRY_RET BIT(4)
1088
1089 /* Get original function from stack instead of from provided direct address.
1090 * Makes sense for trampolines with fexit or fmod_ret programs.
1091 */
1092 #define BPF_TRAMP_F_ORIG_STACK BIT(5)
1093
1094 /* This trampoline is on a function with another ftrace_ops with IPMODIFY,
1095 * e.g., a live patch. This flag is set and cleared by ftrace call backs,
1096 */
1097 #define BPF_TRAMP_F_SHARE_IPMODIFY BIT(6)
1098
1099 /* Indicate that current trampoline is in a tail call context. Then, it has to
1100 * cache and restore tail_call_cnt to avoid infinite tail call loop.
1101 */
1102 #define BPF_TRAMP_F_TAIL_CALL_CTX BIT(7)
1103
1104 /*
1105 * Indicate the trampoline should be suitable to receive indirect calls;
1106 * without this indirectly calling the generated code can result in #UD/#CP,
1107 * depending on the CFI options.
1108 *
1109 * Used by bpf_struct_ops.
1110 *
1111 * Incompatible with FENTRY usage, overloads @func_addr argument.
1112 */
1113 #define BPF_TRAMP_F_INDIRECT BIT(8)
1114
1115 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
1116 * bytes on x86.
1117 */
1118 enum {
1119 #if defined(__s390x__)
1120 BPF_MAX_TRAMP_LINKS = 27,
1121 #else
1122 BPF_MAX_TRAMP_LINKS = 38,
1123 #endif
1124 };
1125
1126 struct bpf_tramp_links {
1127 struct bpf_tramp_link *links[BPF_MAX_TRAMP_LINKS];
1128 int nr_links;
1129 };
1130
1131 struct bpf_tramp_run_ctx;
1132
1133 /* Different use cases for BPF trampoline:
1134 * 1. replace nop at the function entry (kprobe equivalent)
1135 * flags = BPF_TRAMP_F_RESTORE_REGS
1136 * fentry = a set of programs to run before returning from trampoline
1137 *
1138 * 2. replace nop at the function entry (kprobe + kretprobe equivalent)
1139 * flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME
1140 * orig_call = fentry_ip + MCOUNT_INSN_SIZE
1141 * fentry = a set of program to run before calling original function
1142 * fexit = a set of program to run after original function
1143 *
1144 * 3. replace direct call instruction anywhere in the function body
1145 * or assign a function pointer for indirect call (like tcp_congestion_ops->cong_avoid)
1146 * With flags = 0
1147 * fentry = a set of programs to run before returning from trampoline
1148 * With flags = BPF_TRAMP_F_CALL_ORIG
1149 * orig_call = original callback addr or direct function addr
1150 * fentry = a set of program to run before calling original function
1151 * fexit = a set of program to run after original function
1152 */
1153 struct bpf_tramp_image;
1154 int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end,
1155 const struct btf_func_model *m, u32 flags,
1156 struct bpf_tramp_links *tlinks,
1157 void *func_addr);
1158 void *arch_alloc_bpf_trampoline(unsigned int size);
1159 void arch_free_bpf_trampoline(void *image, unsigned int size);
1160 int __must_check arch_protect_bpf_trampoline(void *image, unsigned int size);
1161 int arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags,
1162 struct bpf_tramp_links *tlinks, void *func_addr);
1163
1164 u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog,
1165 struct bpf_tramp_run_ctx *run_ctx);
1166 void notrace __bpf_prog_exit_sleepable_recur(struct bpf_prog *prog, u64 start,
1167 struct bpf_tramp_run_ctx *run_ctx);
1168 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr);
1169 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr);
1170 typedef u64 (*bpf_trampoline_enter_t)(struct bpf_prog *prog,
1171 struct bpf_tramp_run_ctx *run_ctx);
1172 typedef void (*bpf_trampoline_exit_t)(struct bpf_prog *prog, u64 start,
1173 struct bpf_tramp_run_ctx *run_ctx);
1174 bpf_trampoline_enter_t bpf_trampoline_enter(const struct bpf_prog *prog);
1175 bpf_trampoline_exit_t bpf_trampoline_exit(const struct bpf_prog *prog);
1176
1177 struct bpf_ksym {
1178 unsigned long start;
1179 unsigned long end;
1180 char name[KSYM_NAME_LEN];
1181 struct list_head lnode;
1182 struct latch_tree_node tnode;
1183 bool prog;
1184 };
1185
1186 enum bpf_tramp_prog_type {
1187 BPF_TRAMP_FENTRY,
1188 BPF_TRAMP_FEXIT,
1189 BPF_TRAMP_MODIFY_RETURN,
1190 BPF_TRAMP_MAX,
1191 BPF_TRAMP_REPLACE, /* more than MAX */
1192 };
1193
1194 struct bpf_tramp_image {
1195 void *image;
1196 int size;
1197 struct bpf_ksym ksym;
1198 struct percpu_ref pcref;
1199 void *ip_after_call;
1200 void *ip_epilogue;
1201 union {
1202 struct rcu_head rcu;
1203 struct work_struct work;
1204 };
1205 };
1206
1207 struct bpf_trampoline {
1208 /* hlist for trampoline_table */
1209 struct hlist_node hlist;
1210 struct ftrace_ops *fops;
1211 /* serializes access to fields of this trampoline */
1212 struct mutex mutex;
1213 refcount_t refcnt;
1214 u32 flags;
1215 u64 key;
1216 struct {
1217 struct btf_func_model model;
1218 void *addr;
1219 bool ftrace_managed;
1220 } func;
1221 /* if !NULL this is BPF_PROG_TYPE_EXT program that extends another BPF
1222 * program by replacing one of its functions. func.addr is the address
1223 * of the function it replaced.
1224 */
1225 struct bpf_prog *extension_prog;
1226 /* list of BPF programs using this trampoline */
1227 struct hlist_head progs_hlist[BPF_TRAMP_MAX];
1228 /* Number of attached programs. A counter per kind. */
1229 int progs_cnt[BPF_TRAMP_MAX];
1230 /* Executable image of trampoline */
1231 struct bpf_tramp_image *cur_image;
1232 ANDROID_KABI_RESERVE(1);
1233 };
1234
1235 struct bpf_attach_target_info {
1236 struct btf_func_model fmodel;
1237 long tgt_addr;
1238 struct module *tgt_mod;
1239 const char *tgt_name;
1240 const struct btf_type *tgt_type;
1241 };
1242
1243 #define BPF_DISPATCHER_MAX 48 /* Fits in 2048B */
1244
1245 struct bpf_dispatcher_prog {
1246 struct bpf_prog *prog;
1247 refcount_t users;
1248 };
1249
1250 struct bpf_dispatcher {
1251 /* dispatcher mutex */
1252 struct mutex mutex;
1253 void *func;
1254 struct bpf_dispatcher_prog progs[BPF_DISPATCHER_MAX];
1255 int num_progs;
1256 void *image;
1257 void *rw_image;
1258 u32 image_off;
1259 struct bpf_ksym ksym;
1260 #ifdef CONFIG_HAVE_STATIC_CALL
1261 struct static_call_key *sc_key;
1262 void *sc_tramp;
1263 #endif
1264 ANDROID_KABI_RESERVE(1);
1265 };
1266
1267 #ifndef __bpfcall
1268 #define __bpfcall __nocfi
1269 #endif
1270
bpf_dispatcher_nop_func(const void * ctx,const struct bpf_insn * insnsi,bpf_func_t bpf_func)1271 static __always_inline __bpfcall unsigned int bpf_dispatcher_nop_func(
1272 const void *ctx,
1273 const struct bpf_insn *insnsi,
1274 bpf_func_t bpf_func)
1275 {
1276 return bpf_func(ctx, insnsi);
1277 }
1278
1279 /* the implementation of the opaque uapi struct bpf_dynptr */
1280 struct bpf_dynptr_kern {
1281 void *data;
1282 /* Size represents the number of usable bytes of dynptr data.
1283 * If for example the offset is at 4 for a local dynptr whose data is
1284 * of type u64, the number of usable bytes is 4.
1285 *
1286 * The upper 8 bits are reserved. It is as follows:
1287 * Bits 0 - 23 = size
1288 * Bits 24 - 30 = dynptr type
1289 * Bit 31 = whether dynptr is read-only
1290 */
1291 u32 size;
1292 u32 offset;
1293 } __aligned(8);
1294
1295 enum bpf_dynptr_type {
1296 BPF_DYNPTR_TYPE_INVALID,
1297 /* Points to memory that is local to the bpf program */
1298 BPF_DYNPTR_TYPE_LOCAL,
1299 /* Underlying data is a ringbuf record */
1300 BPF_DYNPTR_TYPE_RINGBUF,
1301 /* Underlying data is a sk_buff */
1302 BPF_DYNPTR_TYPE_SKB,
1303 /* Underlying data is a xdp_buff */
1304 BPF_DYNPTR_TYPE_XDP,
1305 };
1306
1307 int bpf_dynptr_check_size(u32 size);
1308 u32 __bpf_dynptr_size(const struct bpf_dynptr_kern *ptr);
1309 const void *__bpf_dynptr_data(const struct bpf_dynptr_kern *ptr, u32 len);
1310 void *__bpf_dynptr_data_rw(const struct bpf_dynptr_kern *ptr, u32 len);
1311 bool __bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr);
1312
1313 #ifdef CONFIG_BPF_JIT
1314 int bpf_trampoline_link_prog(struct bpf_tramp_link *link,
1315 struct bpf_trampoline *tr,
1316 struct bpf_prog *tgt_prog);
1317 int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link,
1318 struct bpf_trampoline *tr,
1319 struct bpf_prog *tgt_prog);
1320 struct bpf_trampoline *bpf_trampoline_get(u64 key,
1321 struct bpf_attach_target_info *tgt_info);
1322 void bpf_trampoline_put(struct bpf_trampoline *tr);
1323 int arch_prepare_bpf_dispatcher(void *image, void *buf, s64 *funcs, int num_funcs);
1324
1325 /*
1326 * When the architecture supports STATIC_CALL replace the bpf_dispatcher_fn
1327 * indirection with a direct call to the bpf program. If the architecture does
1328 * not have STATIC_CALL, avoid a double-indirection.
1329 */
1330 #ifdef CONFIG_HAVE_STATIC_CALL
1331
1332 #define __BPF_DISPATCHER_SC_INIT(_name) \
1333 .sc_key = &STATIC_CALL_KEY(_name), \
1334 .sc_tramp = STATIC_CALL_TRAMP_ADDR(_name),
1335
1336 #define __BPF_DISPATCHER_SC(name) \
1337 DEFINE_STATIC_CALL(bpf_dispatcher_##name##_call, bpf_dispatcher_nop_func)
1338
1339 #define __BPF_DISPATCHER_CALL(name) \
1340 static_call(bpf_dispatcher_##name##_call)(ctx, insnsi, bpf_func)
1341
1342 #define __BPF_DISPATCHER_UPDATE(_d, _new) \
1343 __static_call_update((_d)->sc_key, (_d)->sc_tramp, (_new))
1344
1345 #else
1346 #define __BPF_DISPATCHER_SC_INIT(name)
1347 #define __BPF_DISPATCHER_SC(name)
1348 #define __BPF_DISPATCHER_CALL(name) bpf_func(ctx, insnsi)
1349 #define __BPF_DISPATCHER_UPDATE(_d, _new)
1350 #endif
1351
1352 #define BPF_DISPATCHER_INIT(_name) { \
1353 .mutex = __MUTEX_INITIALIZER(_name.mutex), \
1354 .func = &_name##_func, \
1355 .progs = {}, \
1356 .num_progs = 0, \
1357 .image = NULL, \
1358 .image_off = 0, \
1359 .ksym = { \
1360 .name = #_name, \
1361 .lnode = LIST_HEAD_INIT(_name.ksym.lnode), \
1362 }, \
1363 __BPF_DISPATCHER_SC_INIT(_name##_call) \
1364 }
1365
1366 #define DEFINE_BPF_DISPATCHER(name) \
1367 __BPF_DISPATCHER_SC(name); \
1368 noinline __bpfcall unsigned int bpf_dispatcher_##name##_func( \
1369 const void *ctx, \
1370 const struct bpf_insn *insnsi, \
1371 bpf_func_t bpf_func) \
1372 { \
1373 return __BPF_DISPATCHER_CALL(name); \
1374 } \
1375 EXPORT_SYMBOL(bpf_dispatcher_##name##_func); \
1376 struct bpf_dispatcher bpf_dispatcher_##name = \
1377 BPF_DISPATCHER_INIT(bpf_dispatcher_##name);
1378
1379 #define DECLARE_BPF_DISPATCHER(name) \
1380 unsigned int bpf_dispatcher_##name##_func( \
1381 const void *ctx, \
1382 const struct bpf_insn *insnsi, \
1383 bpf_func_t bpf_func); \
1384 extern struct bpf_dispatcher bpf_dispatcher_##name;
1385
1386 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_##name##_func
1387 #define BPF_DISPATCHER_PTR(name) (&bpf_dispatcher_##name)
1388 void bpf_dispatcher_change_prog(struct bpf_dispatcher *d, struct bpf_prog *from,
1389 struct bpf_prog *to);
1390 /* Called only from JIT-enabled code, so there's no need for stubs. */
1391 void bpf_image_ksym_init(void *data, unsigned int size, struct bpf_ksym *ksym);
1392 void bpf_image_ksym_add(struct bpf_ksym *ksym);
1393 void bpf_image_ksym_del(struct bpf_ksym *ksym);
1394 void bpf_ksym_add(struct bpf_ksym *ksym);
1395 void bpf_ksym_del(struct bpf_ksym *ksym);
1396 int bpf_jit_charge_modmem(u32 size);
1397 void bpf_jit_uncharge_modmem(u32 size);
1398 bool bpf_prog_has_trampoline(const struct bpf_prog *prog);
1399 #else
bpf_trampoline_link_prog(struct bpf_tramp_link * link,struct bpf_trampoline * tr,struct bpf_prog * tgt_prog)1400 static inline int bpf_trampoline_link_prog(struct bpf_tramp_link *link,
1401 struct bpf_trampoline *tr,
1402 struct bpf_prog *tgt_prog)
1403 {
1404 return -ENOTSUPP;
1405 }
bpf_trampoline_unlink_prog(struct bpf_tramp_link * link,struct bpf_trampoline * tr,struct bpf_prog * tgt_prog)1406 static inline int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link,
1407 struct bpf_trampoline *tr,
1408 struct bpf_prog *tgt_prog)
1409 {
1410 return -ENOTSUPP;
1411 }
bpf_trampoline_get(u64 key,struct bpf_attach_target_info * tgt_info)1412 static inline struct bpf_trampoline *bpf_trampoline_get(u64 key,
1413 struct bpf_attach_target_info *tgt_info)
1414 {
1415 return NULL;
1416 }
bpf_trampoline_put(struct bpf_trampoline * tr)1417 static inline void bpf_trampoline_put(struct bpf_trampoline *tr) {}
1418 #define DEFINE_BPF_DISPATCHER(name)
1419 #define DECLARE_BPF_DISPATCHER(name)
1420 #define BPF_DISPATCHER_FUNC(name) bpf_dispatcher_nop_func
1421 #define BPF_DISPATCHER_PTR(name) NULL
bpf_dispatcher_change_prog(struct bpf_dispatcher * d,struct bpf_prog * from,struct bpf_prog * to)1422 static inline void bpf_dispatcher_change_prog(struct bpf_dispatcher *d,
1423 struct bpf_prog *from,
1424 struct bpf_prog *to) {}
is_bpf_image_address(unsigned long address)1425 static inline bool is_bpf_image_address(unsigned long address)
1426 {
1427 return false;
1428 }
bpf_prog_has_trampoline(const struct bpf_prog * prog)1429 static inline bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
1430 {
1431 return false;
1432 }
1433 #endif
1434
1435 struct bpf_func_info_aux {
1436 u16 linkage;
1437 bool unreliable;
1438 bool called : 1;
1439 bool verified : 1;
1440 };
1441
1442 enum bpf_jit_poke_reason {
1443 BPF_POKE_REASON_TAIL_CALL,
1444 };
1445
1446 /* Descriptor of pokes pointing /into/ the JITed image. */
1447 struct bpf_jit_poke_descriptor {
1448 void *tailcall_target;
1449 void *tailcall_bypass;
1450 void *bypass_addr;
1451 void *aux;
1452 union {
1453 struct {
1454 struct bpf_map *map;
1455 u32 key;
1456 } tail_call;
1457 };
1458 bool tailcall_target_stable;
1459 u8 adj_off;
1460 u16 reason;
1461 u32 insn_idx;
1462 };
1463
1464 /* reg_type info for ctx arguments */
1465 struct bpf_ctx_arg_aux {
1466 u32 offset;
1467 enum bpf_reg_type reg_type;
1468 struct btf *btf;
1469 u32 btf_id;
1470 };
1471
1472 struct btf_mod_pair {
1473 struct btf *btf;
1474 struct module *module;
1475 };
1476
1477 struct bpf_kfunc_desc_tab;
1478
1479 struct bpf_prog_aux {
1480 atomic64_t refcnt;
1481 u32 used_map_cnt;
1482 u32 used_btf_cnt;
1483 u32 max_ctx_offset;
1484 u32 max_pkt_offset;
1485 u32 max_tp_access;
1486 u32 stack_depth;
1487 u32 id;
1488 u32 func_cnt; /* used by non-func prog as the number of func progs */
1489 u32 real_func_cnt; /* includes hidden progs, only used for JIT and freeing progs */
1490 u32 func_idx; /* 0 for non-func prog, the index in func array for func prog */
1491 u32 attach_btf_id; /* in-kernel BTF type id to attach to */
1492 u32 ctx_arg_info_size;
1493 u32 max_rdonly_access;
1494 u32 max_rdwr_access;
1495 struct btf *attach_btf;
1496 const struct bpf_ctx_arg_aux *ctx_arg_info;
1497 struct mutex dst_mutex; /* protects dst_* pointers below, *after* prog becomes visible */
1498 struct bpf_prog *dst_prog;
1499 struct bpf_trampoline *dst_trampoline;
1500 enum bpf_prog_type saved_dst_prog_type;
1501 enum bpf_attach_type saved_dst_attach_type;
1502 bool verifier_zext; /* Zero extensions has been inserted by verifier. */
1503 bool dev_bound; /* Program is bound to the netdev. */
1504 bool offload_requested; /* Program is bound and offloaded to the netdev. */
1505 bool attach_btf_trace; /* true if attaching to BTF-enabled raw tp */
1506 bool attach_tracing_prog; /* true if tracing another tracing program */
1507 bool func_proto_unreliable;
1508 bool tail_call_reachable;
1509 bool xdp_has_frags;
1510 bool exception_cb;
1511 bool exception_boundary;
1512 bool is_extended; /* true if extended by freplace program */
1513 u64 prog_array_member_cnt; /* counts how many times as member of prog_array */
1514 struct mutex ext_mutex; /* mutex for is_extended and prog_array_member_cnt */
1515 struct bpf_arena *arena;
1516 /* BTF_KIND_FUNC_PROTO for valid attach_btf_id */
1517 const struct btf_type *attach_func_proto;
1518 /* function name for valid attach_btf_id */
1519 const char *attach_func_name;
1520 struct bpf_prog **func;
1521 void *jit_data; /* JIT specific data. arch dependent */
1522 struct bpf_jit_poke_descriptor *poke_tab;
1523 struct bpf_kfunc_desc_tab *kfunc_tab;
1524 struct bpf_kfunc_btf_tab *kfunc_btf_tab;
1525 u32 size_poke_tab;
1526 #ifdef CONFIG_FINEIBT
1527 struct bpf_ksym ksym_prefix;
1528 #endif
1529 struct bpf_ksym ksym;
1530 const struct bpf_prog_ops *ops;
1531 struct bpf_map **used_maps;
1532 struct mutex used_maps_mutex; /* mutex for used_maps and used_map_cnt */
1533 struct btf_mod_pair *used_btfs;
1534 struct bpf_prog *prog;
1535 struct user_struct *user;
1536 u64 load_time; /* ns since boottime */
1537 u32 verified_insns;
1538 int cgroup_atype; /* enum cgroup_bpf_attach_type */
1539 struct bpf_map *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
1540 char name[BPF_OBJ_NAME_LEN];
1541 u64 (*bpf_exception_cb)(u64 cookie, u64 sp, u64 bp, u64, u64);
1542 #ifdef CONFIG_SECURITY
1543 void *security;
1544 #endif
1545 struct bpf_token *token;
1546 struct bpf_prog_offload *offload;
1547 struct btf *btf;
1548 struct bpf_func_info *func_info;
1549 struct bpf_func_info_aux *func_info_aux;
1550 /* bpf_line_info loaded from userspace. linfo->insn_off
1551 * has the xlated insn offset.
1552 * Both the main and sub prog share the same linfo.
1553 * The subprog can access its first linfo by
1554 * using the linfo_idx.
1555 */
1556 struct bpf_line_info *linfo;
1557 /* jited_linfo is the jited addr of the linfo. It has a
1558 * one to one mapping to linfo:
1559 * jited_linfo[i] is the jited addr for the linfo[i]->insn_off.
1560 * Both the main and sub prog share the same jited_linfo.
1561 * The subprog can access its first jited_linfo by
1562 * using the linfo_idx.
1563 */
1564 void **jited_linfo;
1565 u32 func_info_cnt;
1566 u32 nr_linfo;
1567 /* subprog can use linfo_idx to access its first linfo and
1568 * jited_linfo.
1569 * main prog always has linfo_idx == 0
1570 */
1571 u32 linfo_idx;
1572 struct module *mod;
1573 u32 num_exentries;
1574 struct exception_table_entry *extable;
1575 union {
1576 struct work_struct work;
1577 struct rcu_head rcu;
1578 };
1579 ANDROID_KABI_RESERVE(1);
1580 };
1581
1582 struct bpf_prog {
1583 u16 pages; /* Number of allocated pages */
1584 u16 jited:1, /* Is our filter JIT'ed? */
1585 jit_requested:1,/* archs need to JIT the prog */
1586 gpl_compatible:1, /* Is filter GPL compatible? */
1587 cb_access:1, /* Is control block accessed? */
1588 dst_needed:1, /* Do we need dst entry? */
1589 blinding_requested:1, /* needs constant blinding */
1590 blinded:1, /* Was blinded */
1591 is_func:1, /* program is a bpf function */
1592 kprobe_override:1, /* Do we override a kprobe? */
1593 has_callchain_buf:1, /* callchain buffer allocated? */
1594 enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */
1595 call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */
1596 call_get_func_ip:1, /* Do we call get_func_ip() */
1597 tstamp_type_access:1, /* Accessed __sk_buff->tstamp_type */
1598 sleepable:1; /* BPF program is sleepable */
1599 enum bpf_prog_type type; /* Type of BPF program */
1600 enum bpf_attach_type expected_attach_type; /* For some prog types */
1601 u32 len; /* Number of filter blocks */
1602 u32 jited_len; /* Size of jited insns in bytes */
1603 u8 tag[BPF_TAG_SIZE];
1604 struct bpf_prog_stats __percpu *stats;
1605 int __percpu *active;
1606 unsigned int (*bpf_func)(const void *ctx,
1607 const struct bpf_insn *insn);
1608 struct bpf_prog_aux *aux; /* Auxiliary fields */
1609 struct sock_fprog_kern *orig_prog; /* Original BPF program */
1610 ANDROID_KABI_RESERVE(1);
1611 /* Instructions for interpreter */
1612 union {
1613 DECLARE_FLEX_ARRAY(struct sock_filter, insns);
1614 DECLARE_FLEX_ARRAY(struct bpf_insn, insnsi);
1615 };
1616 };
1617
1618 struct bpf_array_aux {
1619 /* Programs with direct jumps into programs part of this array. */
1620 struct list_head poke_progs;
1621 struct bpf_map *map;
1622 struct mutex poke_mutex;
1623 struct work_struct work;
1624 };
1625
1626 struct bpf_link {
1627 atomic64_t refcnt;
1628 u32 id;
1629 enum bpf_link_type type;
1630 const struct bpf_link_ops *ops;
1631 struct bpf_prog *prog;
1632 /* rcu is used before freeing, work can be used to schedule that
1633 * RCU-based freeing before that, so they never overlap
1634 */
1635 union {
1636 struct rcu_head rcu;
1637 struct work_struct work;
1638 };
1639 };
1640
1641 struct bpf_link_ops {
1642 void (*release)(struct bpf_link *link);
1643 /* deallocate link resources callback, called without RCU grace period
1644 * waiting
1645 */
1646 void (*dealloc)(struct bpf_link *link);
1647 /* deallocate link resources callback, called after RCU grace period;
1648 * if underlying BPF program is sleepable we go through tasks trace
1649 * RCU GP and then "classic" RCU GP
1650 */
1651 void (*dealloc_deferred)(struct bpf_link *link);
1652 int (*detach)(struct bpf_link *link);
1653 int (*update_prog)(struct bpf_link *link, struct bpf_prog *new_prog,
1654 struct bpf_prog *old_prog);
1655 void (*show_fdinfo)(const struct bpf_link *link, struct seq_file *seq);
1656 int (*fill_link_info)(const struct bpf_link *link,
1657 struct bpf_link_info *info);
1658 int (*update_map)(struct bpf_link *link, struct bpf_map *new_map,
1659 struct bpf_map *old_map);
1660 __poll_t (*poll)(struct file *file, struct poll_table_struct *pts);
1661 ANDROID_KABI_RESERVE(1);
1662 };
1663
1664 struct bpf_tramp_link {
1665 struct bpf_link link;
1666 struct hlist_node tramp_hlist;
1667 u64 cookie;
1668 };
1669
1670 struct bpf_shim_tramp_link {
1671 struct bpf_tramp_link link;
1672 struct bpf_trampoline *trampoline;
1673 };
1674
1675 struct bpf_tracing_link {
1676 struct bpf_tramp_link link;
1677 enum bpf_attach_type attach_type;
1678 struct bpf_trampoline *trampoline;
1679 struct bpf_prog *tgt_prog;
1680 };
1681
1682 struct bpf_raw_tp_link {
1683 struct bpf_link link;
1684 struct bpf_raw_event_map *btp;
1685 u64 cookie;
1686 };
1687
1688 struct bpf_link_primer {
1689 struct bpf_link *link;
1690 struct file *file;
1691 int fd;
1692 u32 id;
1693 };
1694
1695 struct bpf_mount_opts {
1696 kuid_t uid;
1697 kgid_t gid;
1698 umode_t mode;
1699
1700 /* BPF token-related delegation options */
1701 u64 delegate_cmds;
1702 u64 delegate_maps;
1703 u64 delegate_progs;
1704 u64 delegate_attachs;
1705 };
1706
1707 struct bpf_token {
1708 struct work_struct work;
1709 atomic64_t refcnt;
1710 struct user_namespace *userns;
1711 u64 allowed_cmds;
1712 u64 allowed_maps;
1713 u64 allowed_progs;
1714 u64 allowed_attachs;
1715 #ifdef CONFIG_SECURITY
1716 void *security;
1717 #endif
1718 };
1719
1720 struct bpf_struct_ops_value;
1721 struct btf_member;
1722
1723 #define BPF_STRUCT_OPS_MAX_NR_MEMBERS 64
1724 /**
1725 * struct bpf_struct_ops - A structure of callbacks allowing a subsystem to
1726 * define a BPF_MAP_TYPE_STRUCT_OPS map type composed
1727 * of BPF_PROG_TYPE_STRUCT_OPS progs.
1728 * @verifier_ops: A structure of callbacks that are invoked by the verifier
1729 * when determining whether the struct_ops progs in the
1730 * struct_ops map are valid.
1731 * @init: A callback that is invoked a single time, and before any other
1732 * callback, to initialize the structure. A nonzero return value means
1733 * the subsystem could not be initialized.
1734 * @check_member: When defined, a callback invoked by the verifier to allow
1735 * the subsystem to determine if an entry in the struct_ops map
1736 * is valid. A nonzero return value means that the map is
1737 * invalid and should be rejected by the verifier.
1738 * @init_member: A callback that is invoked for each member of the struct_ops
1739 * map to allow the subsystem to initialize the member. A nonzero
1740 * value means the member could not be initialized. This callback
1741 * is exclusive with the @type, @type_id, @value_type, and
1742 * @value_id fields.
1743 * @reg: A callback that is invoked when the struct_ops map has been
1744 * initialized and is being attached to. Zero means the struct_ops map
1745 * has been successfully registered and is live. A nonzero return value
1746 * means the struct_ops map could not be registered.
1747 * @unreg: A callback that is invoked when the struct_ops map should be
1748 * unregistered.
1749 * @update: A callback that is invoked when the live struct_ops map is being
1750 * updated to contain new values. This callback is only invoked when
1751 * the struct_ops map is loaded with BPF_F_LINK. If not defined, the
1752 * it is assumed that the struct_ops map cannot be updated.
1753 * @validate: A callback that is invoked after all of the members have been
1754 * initialized. This callback should perform static checks on the
1755 * map, meaning that it should either fail or succeed
1756 * deterministically. A struct_ops map that has been validated may
1757 * not necessarily succeed in being registered if the call to @reg
1758 * fails. For example, a valid struct_ops map may be loaded, but
1759 * then fail to be registered due to there being another active
1760 * struct_ops map on the system in the subsystem already. For this
1761 * reason, if this callback is not defined, the check is skipped as
1762 * the struct_ops map will have final verification performed in
1763 * @reg.
1764 * @type: BTF type.
1765 * @value_type: Value type.
1766 * @name: The name of the struct bpf_struct_ops object.
1767 * @func_models: Func models
1768 * @type_id: BTF type id.
1769 * @value_id: BTF value id.
1770 */
1771 struct bpf_struct_ops {
1772 const struct bpf_verifier_ops *verifier_ops;
1773 int (*init)(struct btf *btf);
1774 int (*check_member)(const struct btf_type *t,
1775 const struct btf_member *member,
1776 const struct bpf_prog *prog);
1777 int (*init_member)(const struct btf_type *t,
1778 const struct btf_member *member,
1779 void *kdata, const void *udata);
1780 int (*reg)(void *kdata, struct bpf_link *link);
1781 void (*unreg)(void *kdata, struct bpf_link *link);
1782 int (*update)(void *kdata, void *old_kdata, struct bpf_link *link);
1783 int (*validate)(void *kdata);
1784 void *cfi_stubs;
1785 struct module *owner;
1786 const char *name;
1787 struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
1788 };
1789
1790 /* Every member of a struct_ops type has an instance even a member is not
1791 * an operator (function pointer). The "info" field will be assigned to
1792 * prog->aux->ctx_arg_info of BPF struct_ops programs to provide the
1793 * argument information required by the verifier to verify the program.
1794 *
1795 * btf_ctx_access() will lookup prog->aux->ctx_arg_info to find the
1796 * corresponding entry for an given argument.
1797 */
1798 struct bpf_struct_ops_arg_info {
1799 struct bpf_ctx_arg_aux *info;
1800 u32 cnt;
1801 };
1802
1803 struct bpf_struct_ops_desc {
1804 struct bpf_struct_ops *st_ops;
1805
1806 const struct btf_type *type;
1807 const struct btf_type *value_type;
1808 u32 type_id;
1809 u32 value_id;
1810
1811 /* Collection of argument information for each member */
1812 struct bpf_struct_ops_arg_info *arg_info;
1813 ANDROID_KABI_RESERVE(1);
1814 };
1815
1816 enum bpf_struct_ops_state {
1817 BPF_STRUCT_OPS_STATE_INIT,
1818 BPF_STRUCT_OPS_STATE_INUSE,
1819 BPF_STRUCT_OPS_STATE_TOBEFREE,
1820 BPF_STRUCT_OPS_STATE_READY,
1821 };
1822
1823 struct bpf_struct_ops_common_value {
1824 refcount_t refcnt;
1825 enum bpf_struct_ops_state state;
1826 };
1827
1828 #if defined(CONFIG_BPF_JIT) && defined(CONFIG_BPF_SYSCALL)
1829 /* This macro helps developer to register a struct_ops type and generate
1830 * type information correctly. Developers should use this macro to register
1831 * a struct_ops type instead of calling __register_bpf_struct_ops() directly.
1832 */
1833 #define register_bpf_struct_ops(st_ops, type) \
1834 ({ \
1835 struct bpf_struct_ops_##type { \
1836 struct bpf_struct_ops_common_value common; \
1837 struct type data ____cacheline_aligned_in_smp; \
1838 }; \
1839 BTF_TYPE_EMIT(struct bpf_struct_ops_##type); \
1840 __register_bpf_struct_ops(st_ops); \
1841 })
1842 #define BPF_MODULE_OWNER ((void *)((0xeB9FUL << 2) + POISON_POINTER_DELTA))
1843 bool bpf_struct_ops_get(const void *kdata);
1844 void bpf_struct_ops_put(const void *kdata);
1845 int bpf_struct_ops_supported(const struct bpf_struct_ops *st_ops, u32 moff);
1846 int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
1847 void *value);
1848 int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_links *tlinks,
1849 struct bpf_tramp_link *link,
1850 const struct btf_func_model *model,
1851 void *stub_func,
1852 void **image, u32 *image_off,
1853 bool allow_alloc);
1854 void bpf_struct_ops_image_free(void *image);
bpf_try_module_get(const void * data,struct module * owner)1855 static inline bool bpf_try_module_get(const void *data, struct module *owner)
1856 {
1857 if (owner == BPF_MODULE_OWNER)
1858 return bpf_struct_ops_get(data);
1859 else
1860 return try_module_get(owner);
1861 }
bpf_module_put(const void * data,struct module * owner)1862 static inline void bpf_module_put(const void *data, struct module *owner)
1863 {
1864 if (owner == BPF_MODULE_OWNER)
1865 bpf_struct_ops_put(data);
1866 else
1867 module_put(owner);
1868 }
1869 int bpf_struct_ops_link_create(union bpf_attr *attr);
1870
1871 #ifdef CONFIG_NET
1872 /* Define it here to avoid the use of forward declaration */
1873 struct bpf_dummy_ops_state {
1874 int val;
1875 };
1876
1877 struct bpf_dummy_ops {
1878 int (*test_1)(struct bpf_dummy_ops_state *cb);
1879 int (*test_2)(struct bpf_dummy_ops_state *cb, int a1, unsigned short a2,
1880 char a3, unsigned long a4);
1881 int (*test_sleepable)(struct bpf_dummy_ops_state *cb);
1882 };
1883
1884 int bpf_struct_ops_test_run(struct bpf_prog *prog, const union bpf_attr *kattr,
1885 union bpf_attr __user *uattr);
1886 #endif
1887 int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
1888 struct btf *btf,
1889 struct bpf_verifier_log *log);
1890 void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map);
1891 void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_ops_desc);
1892 #else
1893 #define register_bpf_struct_ops(st_ops, type) ({ (void *)(st_ops); 0; })
bpf_try_module_get(const void * data,struct module * owner)1894 static inline bool bpf_try_module_get(const void *data, struct module *owner)
1895 {
1896 return try_module_get(owner);
1897 }
bpf_module_put(const void * data,struct module * owner)1898 static inline void bpf_module_put(const void *data, struct module *owner)
1899 {
1900 module_put(owner);
1901 }
bpf_struct_ops_supported(const struct bpf_struct_ops * st_ops,u32 moff)1902 static inline int bpf_struct_ops_supported(const struct bpf_struct_ops *st_ops, u32 moff)
1903 {
1904 return -ENOTSUPP;
1905 }
bpf_struct_ops_map_sys_lookup_elem(struct bpf_map * map,void * key,void * value)1906 static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
1907 void *key,
1908 void *value)
1909 {
1910 return -EINVAL;
1911 }
bpf_struct_ops_link_create(union bpf_attr * attr)1912 static inline int bpf_struct_ops_link_create(union bpf_attr *attr)
1913 {
1914 return -EOPNOTSUPP;
1915 }
bpf_map_struct_ops_info_fill(struct bpf_map_info * info,struct bpf_map * map)1916 static inline void bpf_map_struct_ops_info_fill(struct bpf_map_info *info, struct bpf_map *map)
1917 {
1918 }
1919
bpf_struct_ops_desc_release(struct bpf_struct_ops_desc * st_ops_desc)1920 static inline void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_ops_desc)
1921 {
1922 }
1923
1924 #endif
1925
1926 #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM)
1927 int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1928 int cgroup_atype);
1929 void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog);
1930 #else
bpf_trampoline_link_cgroup_shim(struct bpf_prog * prog,int cgroup_atype)1931 static inline int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
1932 int cgroup_atype)
1933 {
1934 return -EOPNOTSUPP;
1935 }
bpf_trampoline_unlink_cgroup_shim(struct bpf_prog * prog)1936 static inline void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)
1937 {
1938 }
1939 #endif
1940
1941 struct bpf_array {
1942 struct bpf_map map;
1943 u32 elem_size;
1944 u32 index_mask;
1945 struct bpf_array_aux *aux;
1946 union {
1947 DECLARE_FLEX_ARRAY(char, value) __aligned(8);
1948 DECLARE_FLEX_ARRAY(void *, ptrs) __aligned(8);
1949 DECLARE_FLEX_ARRAY(void __percpu *, pptrs) __aligned(8);
1950 };
1951 };
1952
1953 #define BPF_COMPLEXITY_LIMIT_INSNS 1000000 /* yes. 1M insns */
1954 #define MAX_TAIL_CALL_CNT 33
1955
1956 /* Maximum number of loops for bpf_loop and bpf_iter_num.
1957 * It's enum to expose it (and thus make it discoverable) through BTF.
1958 */
1959 enum {
1960 BPF_MAX_LOOPS = 8 * 1024 * 1024,
1961 };
1962
1963 #define BPF_F_ACCESS_MASK (BPF_F_RDONLY | \
1964 BPF_F_RDONLY_PROG | \
1965 BPF_F_WRONLY | \
1966 BPF_F_WRONLY_PROG)
1967
1968 #define BPF_MAP_CAN_READ BIT(0)
1969 #define BPF_MAP_CAN_WRITE BIT(1)
1970
1971 /* Maximum number of user-producer ring buffer samples that can be drained in
1972 * a call to bpf_user_ringbuf_drain().
1973 */
1974 #define BPF_MAX_USER_RINGBUF_SAMPLES (128 * 1024)
1975
bpf_map_flags_to_cap(struct bpf_map * map)1976 static inline u32 bpf_map_flags_to_cap(struct bpf_map *map)
1977 {
1978 u32 access_flags = map->map_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1979
1980 /* Combination of BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG is
1981 * not possible.
1982 */
1983 if (access_flags & BPF_F_RDONLY_PROG)
1984 return BPF_MAP_CAN_READ;
1985 else if (access_flags & BPF_F_WRONLY_PROG)
1986 return BPF_MAP_CAN_WRITE;
1987 else
1988 return BPF_MAP_CAN_READ | BPF_MAP_CAN_WRITE;
1989 }
1990
bpf_map_flags_access_ok(u32 access_flags)1991 static inline bool bpf_map_flags_access_ok(u32 access_flags)
1992 {
1993 return (access_flags & (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG)) !=
1994 (BPF_F_RDONLY_PROG | BPF_F_WRONLY_PROG);
1995 }
1996
1997 struct bpf_event_entry {
1998 struct perf_event *event;
1999 struct file *perf_file;
2000 struct file *map_file;
2001 struct rcu_head rcu;
2002 };
2003
map_type_contains_progs(struct bpf_map * map)2004 static inline bool map_type_contains_progs(struct bpf_map *map)
2005 {
2006 return map->map_type == BPF_MAP_TYPE_PROG_ARRAY ||
2007 map->map_type == BPF_MAP_TYPE_DEVMAP ||
2008 map->map_type == BPF_MAP_TYPE_CPUMAP;
2009 }
2010
2011 bool bpf_prog_map_compatible(struct bpf_map *map, const struct bpf_prog *fp);
2012 int bpf_prog_calc_tag(struct bpf_prog *fp);
2013
2014 const struct bpf_func_proto *bpf_get_trace_printk_proto(void);
2015 const struct bpf_func_proto *bpf_get_trace_vprintk_proto(void);
2016
2017 typedef unsigned long (*bpf_ctx_copy_t)(void *dst, const void *src,
2018 unsigned long off, unsigned long len);
2019 typedef u32 (*bpf_convert_ctx_access_t)(enum bpf_access_type type,
2020 const struct bpf_insn *src,
2021 struct bpf_insn *dst,
2022 struct bpf_prog *prog,
2023 u32 *target_size);
2024
2025 u64 bpf_event_output(struct bpf_map *map, u64 flags, void *meta, u64 meta_size,
2026 void *ctx, u64 ctx_size, bpf_ctx_copy_t ctx_copy);
2027
2028 /* an array of programs to be executed under rcu_lock.
2029 *
2030 * Typical usage:
2031 * ret = bpf_prog_run_array(rcu_dereference(&bpf_prog_array), ctx, bpf_prog_run);
2032 *
2033 * the structure returned by bpf_prog_array_alloc() should be populated
2034 * with program pointers and the last pointer must be NULL.
2035 * The user has to keep refcnt on the program and make sure the program
2036 * is removed from the array before bpf_prog_put().
2037 * The 'struct bpf_prog_array *' should only be replaced with xchg()
2038 * since other cpus are walking the array of pointers in parallel.
2039 */
2040 struct bpf_prog_array_item {
2041 struct bpf_prog *prog;
2042 union {
2043 struct bpf_cgroup_storage *cgroup_storage[MAX_BPF_CGROUP_STORAGE_TYPE];
2044 u64 bpf_cookie;
2045 };
2046 };
2047
2048 struct bpf_prog_array {
2049 struct rcu_head rcu;
2050 struct bpf_prog_array_item items[];
2051 };
2052
2053 struct bpf_empty_prog_array {
2054 struct bpf_prog_array hdr;
2055 struct bpf_prog *null_prog;
2056 };
2057
2058 /* to avoid allocating empty bpf_prog_array for cgroups that
2059 * don't have bpf program attached use one global 'bpf_empty_prog_array'
2060 * It will not be modified the caller of bpf_prog_array_alloc()
2061 * (since caller requested prog_cnt == 0)
2062 * that pointer should be 'freed' by bpf_prog_array_free()
2063 */
2064 extern struct bpf_empty_prog_array bpf_empty_prog_array;
2065
2066 struct bpf_prog_array *bpf_prog_array_alloc(u32 prog_cnt, gfp_t flags);
2067 void bpf_prog_array_free(struct bpf_prog_array *progs);
2068 /* Use when traversal over the bpf_prog_array uses tasks_trace rcu */
2069 void bpf_prog_array_free_sleepable(struct bpf_prog_array *progs);
2070 int bpf_prog_array_length(struct bpf_prog_array *progs);
2071 bool bpf_prog_array_is_empty(struct bpf_prog_array *array);
2072 int bpf_prog_array_copy_to_user(struct bpf_prog_array *progs,
2073 __u32 __user *prog_ids, u32 cnt);
2074
2075 void bpf_prog_array_delete_safe(struct bpf_prog_array *progs,
2076 struct bpf_prog *old_prog);
2077 int bpf_prog_array_delete_safe_at(struct bpf_prog_array *array, int index);
2078 int bpf_prog_array_update_at(struct bpf_prog_array *array, int index,
2079 struct bpf_prog *prog);
2080 int bpf_prog_array_copy_info(struct bpf_prog_array *array,
2081 u32 *prog_ids, u32 request_cnt,
2082 u32 *prog_cnt);
2083 int bpf_prog_array_copy(struct bpf_prog_array *old_array,
2084 struct bpf_prog *exclude_prog,
2085 struct bpf_prog *include_prog,
2086 u64 bpf_cookie,
2087 struct bpf_prog_array **new_array);
2088
2089 struct bpf_run_ctx {};
2090
2091 struct bpf_cg_run_ctx {
2092 struct bpf_run_ctx run_ctx;
2093 const struct bpf_prog_array_item *prog_item;
2094 int retval;
2095 };
2096
2097 struct bpf_trace_run_ctx {
2098 struct bpf_run_ctx run_ctx;
2099 u64 bpf_cookie;
2100 bool is_uprobe;
2101 };
2102
2103 struct bpf_tramp_run_ctx {
2104 struct bpf_run_ctx run_ctx;
2105 u64 bpf_cookie;
2106 struct bpf_run_ctx *saved_run_ctx;
2107 };
2108
bpf_set_run_ctx(struct bpf_run_ctx * new_ctx)2109 static inline struct bpf_run_ctx *bpf_set_run_ctx(struct bpf_run_ctx *new_ctx)
2110 {
2111 struct bpf_run_ctx *old_ctx = NULL;
2112
2113 #ifdef CONFIG_BPF_SYSCALL
2114 old_ctx = current->bpf_ctx;
2115 current->bpf_ctx = new_ctx;
2116 #endif
2117 return old_ctx;
2118 }
2119
bpf_reset_run_ctx(struct bpf_run_ctx * old_ctx)2120 static inline void bpf_reset_run_ctx(struct bpf_run_ctx *old_ctx)
2121 {
2122 #ifdef CONFIG_BPF_SYSCALL
2123 current->bpf_ctx = old_ctx;
2124 #endif
2125 }
2126
2127 /* BPF program asks to bypass CAP_NET_BIND_SERVICE in bind. */
2128 #define BPF_RET_BIND_NO_CAP_NET_BIND_SERVICE (1 << 0)
2129 /* BPF program asks to set CN on the packet. */
2130 #define BPF_RET_SET_CN (1 << 0)
2131
2132 typedef u32 (*bpf_prog_run_fn)(const struct bpf_prog *prog, const void *ctx);
2133
2134 static __always_inline u32
bpf_prog_run_array(const struct bpf_prog_array * array,const void * ctx,bpf_prog_run_fn run_prog)2135 bpf_prog_run_array(const struct bpf_prog_array *array,
2136 const void *ctx, bpf_prog_run_fn run_prog)
2137 {
2138 const struct bpf_prog_array_item *item;
2139 const struct bpf_prog *prog;
2140 struct bpf_run_ctx *old_run_ctx;
2141 struct bpf_trace_run_ctx run_ctx;
2142 u32 ret = 1;
2143
2144 RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "no rcu lock held");
2145
2146 if (unlikely(!array))
2147 return ret;
2148
2149 run_ctx.is_uprobe = false;
2150
2151 migrate_disable();
2152 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
2153 item = &array->items[0];
2154 while ((prog = READ_ONCE(item->prog))) {
2155 run_ctx.bpf_cookie = item->bpf_cookie;
2156 ret &= run_prog(prog, ctx);
2157 item++;
2158 }
2159 bpf_reset_run_ctx(old_run_ctx);
2160 migrate_enable();
2161 return ret;
2162 }
2163
2164 /* Notes on RCU design for bpf_prog_arrays containing sleepable programs:
2165 *
2166 * We use the tasks_trace rcu flavor read section to protect the bpf_prog_array
2167 * overall. As a result, we must use the bpf_prog_array_free_sleepable
2168 * in order to use the tasks_trace rcu grace period.
2169 *
2170 * When a non-sleepable program is inside the array, we take the rcu read
2171 * section and disable preemption for that program alone, so it can access
2172 * rcu-protected dynamically sized maps.
2173 */
2174 static __always_inline u32
bpf_prog_run_array_uprobe(const struct bpf_prog_array * array,const void * ctx,bpf_prog_run_fn run_prog)2175 bpf_prog_run_array_uprobe(const struct bpf_prog_array *array,
2176 const void *ctx, bpf_prog_run_fn run_prog)
2177 {
2178 const struct bpf_prog_array_item *item;
2179 const struct bpf_prog *prog;
2180 struct bpf_run_ctx *old_run_ctx;
2181 struct bpf_trace_run_ctx run_ctx;
2182 u32 ret = 1;
2183
2184 might_fault();
2185 RCU_LOCKDEP_WARN(!rcu_read_lock_trace_held(), "no rcu lock held");
2186
2187 if (unlikely(!array))
2188 return ret;
2189
2190 migrate_disable();
2191
2192 run_ctx.is_uprobe = true;
2193
2194 old_run_ctx = bpf_set_run_ctx(&run_ctx.run_ctx);
2195 item = &array->items[0];
2196 while ((prog = READ_ONCE(item->prog))) {
2197 if (!prog->sleepable)
2198 rcu_read_lock();
2199
2200 run_ctx.bpf_cookie = item->bpf_cookie;
2201 ret &= run_prog(prog, ctx);
2202 item++;
2203
2204 if (!prog->sleepable)
2205 rcu_read_unlock();
2206 }
2207 bpf_reset_run_ctx(old_run_ctx);
2208 migrate_enable();
2209 return ret;
2210 }
2211
2212 #ifdef CONFIG_BPF_SYSCALL
2213 DECLARE_PER_CPU(int, bpf_prog_active);
2214 extern struct mutex bpf_stats_enabled_mutex;
2215
2216 /*
2217 * Block execution of BPF programs attached to instrumentation (perf,
2218 * kprobes, tracepoints) to prevent deadlocks on map operations as any of
2219 * these events can happen inside a region which holds a map bucket lock
2220 * and can deadlock on it.
2221 */
bpf_disable_instrumentation(void)2222 static inline void bpf_disable_instrumentation(void)
2223 {
2224 migrate_disable();
2225 this_cpu_inc(bpf_prog_active);
2226 }
2227
bpf_enable_instrumentation(void)2228 static inline void bpf_enable_instrumentation(void)
2229 {
2230 this_cpu_dec(bpf_prog_active);
2231 migrate_enable();
2232 }
2233
2234 extern const struct super_operations bpf_super_ops;
2235 extern const struct file_operations bpf_map_fops;
2236 extern const struct file_operations bpf_prog_fops;
2237 extern const struct file_operations bpf_iter_fops;
2238
2239 #define BPF_PROG_TYPE(_id, _name, prog_ctx_type, kern_ctx_type) \
2240 extern const struct bpf_prog_ops _name ## _prog_ops; \
2241 extern const struct bpf_verifier_ops _name ## _verifier_ops;
2242 #define BPF_MAP_TYPE(_id, _ops) \
2243 extern const struct bpf_map_ops _ops;
2244 #define BPF_LINK_TYPE(_id, _name)
2245 #include <linux/bpf_types.h>
2246 #undef BPF_PROG_TYPE
2247 #undef BPF_MAP_TYPE
2248 #undef BPF_LINK_TYPE
2249
2250 extern const struct bpf_prog_ops bpf_offload_prog_ops;
2251 extern const struct bpf_verifier_ops tc_cls_act_analyzer_ops;
2252 extern const struct bpf_verifier_ops xdp_analyzer_ops;
2253
2254 struct bpf_prog *bpf_prog_get(u32 ufd);
2255 struct bpf_prog *bpf_prog_get_type_dev(u32 ufd, enum bpf_prog_type type,
2256 bool attach_drv);
2257 void bpf_prog_add(struct bpf_prog *prog, int i);
2258 void bpf_prog_sub(struct bpf_prog *prog, int i);
2259 void bpf_prog_inc(struct bpf_prog *prog);
2260 struct bpf_prog * __must_check bpf_prog_inc_not_zero(struct bpf_prog *prog);
2261 void bpf_prog_put(struct bpf_prog *prog);
2262
2263 void bpf_prog_free_id(struct bpf_prog *prog);
2264 void bpf_map_free_id(struct bpf_map *map);
2265
2266 struct btf_field *btf_record_find(const struct btf_record *rec,
2267 u32 offset, u32 field_mask);
2268 void btf_record_free(struct btf_record *rec);
2269 void bpf_map_free_record(struct bpf_map *map);
2270 struct btf_record *btf_record_dup(const struct btf_record *rec);
2271 bool btf_record_equal(const struct btf_record *rec_a, const struct btf_record *rec_b);
2272 void bpf_obj_free_timer(const struct btf_record *rec, void *obj);
2273 void bpf_obj_free_workqueue(const struct btf_record *rec, void *obj);
2274 void bpf_obj_free_fields(const struct btf_record *rec, void *obj);
2275 void __bpf_obj_drop_impl(void *p, const struct btf_record *rec, bool percpu);
2276
2277 struct bpf_map *bpf_map_get(u32 ufd);
2278 struct bpf_map *bpf_map_get_with_uref(u32 ufd);
2279
__bpf_map_get(struct fd f)2280 static inline struct bpf_map *__bpf_map_get(struct fd f)
2281 {
2282 if (fd_empty(f))
2283 return ERR_PTR(-EBADF);
2284 if (unlikely(fd_file(f)->f_op != &bpf_map_fops))
2285 return ERR_PTR(-EINVAL);
2286 return fd_file(f)->private_data;
2287 }
2288
2289 void bpf_map_inc(struct bpf_map *map);
2290 void bpf_map_inc_with_uref(struct bpf_map *map);
2291 struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, bool uref);
2292 struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map);
2293 void bpf_map_put_with_uref(struct bpf_map *map);
2294 void bpf_map_put(struct bpf_map *map);
2295 void *bpf_map_area_alloc(u64 size, int numa_node);
2296 void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
2297 void bpf_map_area_free(void *base);
2298 bool bpf_map_write_active(const struct bpf_map *map);
2299 void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
2300 int generic_map_lookup_batch(struct bpf_map *map,
2301 const union bpf_attr *attr,
2302 union bpf_attr __user *uattr);
2303 int generic_map_update_batch(struct bpf_map *map, struct file *map_file,
2304 const union bpf_attr *attr,
2305 union bpf_attr __user *uattr);
2306 int generic_map_delete_batch(struct bpf_map *map,
2307 const union bpf_attr *attr,
2308 union bpf_attr __user *uattr);
2309 struct bpf_map *bpf_map_get_curr_or_next(u32 *id);
2310 struct bpf_prog *bpf_prog_get_curr_or_next(u32 *id);
2311
2312 int bpf_map_alloc_pages(const struct bpf_map *map, gfp_t gfp, int nid,
2313 unsigned long nr_pages, struct page **page_array);
2314 #ifdef CONFIG_MEMCG
2315 void *bpf_map_kmalloc_node(const struct bpf_map *map, size_t size, gfp_t flags,
2316 int node);
2317 void *bpf_map_kzalloc(const struct bpf_map *map, size_t size, gfp_t flags);
2318 void *bpf_map_kvcalloc(struct bpf_map *map, size_t n, size_t size,
2319 gfp_t flags);
2320 void __percpu *bpf_map_alloc_percpu(const struct bpf_map *map, size_t size,
2321 size_t align, gfp_t flags);
2322 #else
2323 /*
2324 * These specialized allocators have to be macros for their allocations to be
2325 * accounted separately (to have separate alloc_tag).
2326 */
2327 #define bpf_map_kmalloc_node(_map, _size, _flags, _node) \
2328 kmalloc_node(_size, _flags, _node)
2329 #define bpf_map_kzalloc(_map, _size, _flags) \
2330 kzalloc(_size, _flags)
2331 #define bpf_map_kvcalloc(_map, _n, _size, _flags) \
2332 kvcalloc(_n, _size, _flags)
2333 #define bpf_map_alloc_percpu(_map, _size, _align, _flags) \
2334 __alloc_percpu_gfp(_size, _align, _flags)
2335 #endif
2336
2337 static inline int
bpf_map_init_elem_count(struct bpf_map * map)2338 bpf_map_init_elem_count(struct bpf_map *map)
2339 {
2340 size_t size = sizeof(*map->elem_count), align = size;
2341 gfp_t flags = GFP_USER | __GFP_NOWARN;
2342
2343 map->elem_count = bpf_map_alloc_percpu(map, size, align, flags);
2344 if (!map->elem_count)
2345 return -ENOMEM;
2346
2347 return 0;
2348 }
2349
2350 static inline void
bpf_map_free_elem_count(struct bpf_map * map)2351 bpf_map_free_elem_count(struct bpf_map *map)
2352 {
2353 free_percpu(map->elem_count);
2354 }
2355
bpf_map_inc_elem_count(struct bpf_map * map)2356 static inline void bpf_map_inc_elem_count(struct bpf_map *map)
2357 {
2358 this_cpu_inc(*map->elem_count);
2359 }
2360
bpf_map_dec_elem_count(struct bpf_map * map)2361 static inline void bpf_map_dec_elem_count(struct bpf_map *map)
2362 {
2363 this_cpu_dec(*map->elem_count);
2364 }
2365
2366 extern int sysctl_unprivileged_bpf_disabled;
2367
2368 bool bpf_token_capable(const struct bpf_token *token, int cap);
2369
bpf_allow_ptr_leaks(const struct bpf_token * token)2370 static inline bool bpf_allow_ptr_leaks(const struct bpf_token *token)
2371 {
2372 return bpf_token_capable(token, CAP_PERFMON);
2373 }
2374
bpf_allow_uninit_stack(const struct bpf_token * token)2375 static inline bool bpf_allow_uninit_stack(const struct bpf_token *token)
2376 {
2377 return bpf_token_capable(token, CAP_PERFMON);
2378 }
2379
bpf_bypass_spec_v1(const struct bpf_token * token)2380 static inline bool bpf_bypass_spec_v1(const struct bpf_token *token)
2381 {
2382 return cpu_mitigations_off() || bpf_token_capable(token, CAP_PERFMON);
2383 }
2384
bpf_bypass_spec_v4(const struct bpf_token * token)2385 static inline bool bpf_bypass_spec_v4(const struct bpf_token *token)
2386 {
2387 return cpu_mitigations_off() || bpf_token_capable(token, CAP_PERFMON);
2388 }
2389
2390 int bpf_map_new_fd(struct bpf_map *map, int flags);
2391 int bpf_prog_new_fd(struct bpf_prog *prog);
2392
2393 void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2394 const struct bpf_link_ops *ops, struct bpf_prog *prog);
2395 int bpf_link_prime(struct bpf_link *link, struct bpf_link_primer *primer);
2396 int bpf_link_settle(struct bpf_link_primer *primer);
2397 void bpf_link_cleanup(struct bpf_link_primer *primer);
2398 void bpf_link_inc(struct bpf_link *link);
2399 struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link);
2400 void bpf_link_put(struct bpf_link *link);
2401 int bpf_link_new_fd(struct bpf_link *link);
2402 struct bpf_link *bpf_link_get_from_fd(u32 ufd);
2403 struct bpf_link *bpf_link_get_curr_or_next(u32 *id);
2404
2405 void bpf_token_inc(struct bpf_token *token);
2406 void bpf_token_put(struct bpf_token *token);
2407 int bpf_token_create(union bpf_attr *attr);
2408 struct bpf_token *bpf_token_get_from_fd(u32 ufd);
2409
2410 bool bpf_token_allow_cmd(const struct bpf_token *token, enum bpf_cmd cmd);
2411 bool bpf_token_allow_map_type(const struct bpf_token *token, enum bpf_map_type type);
2412 bool bpf_token_allow_prog_type(const struct bpf_token *token,
2413 enum bpf_prog_type prog_type,
2414 enum bpf_attach_type attach_type);
2415
2416 int bpf_obj_pin_user(u32 ufd, int path_fd, const char __user *pathname);
2417 int bpf_obj_get_user(int path_fd, const char __user *pathname, int flags);
2418 struct inode *bpf_get_inode(struct super_block *sb, const struct inode *dir,
2419 umode_t mode);
2420
2421 #define BPF_ITER_FUNC_PREFIX "bpf_iter_"
2422 #define DEFINE_BPF_ITER_FUNC(target, args...) \
2423 extern int bpf_iter_ ## target(args); \
2424 int __init bpf_iter_ ## target(args) { return 0; }
2425
2426 /*
2427 * The task type of iterators.
2428 *
2429 * For BPF task iterators, they can be parameterized with various
2430 * parameters to visit only some of tasks.
2431 *
2432 * BPF_TASK_ITER_ALL (default)
2433 * Iterate over resources of every task.
2434 *
2435 * BPF_TASK_ITER_TID
2436 * Iterate over resources of a task/tid.
2437 *
2438 * BPF_TASK_ITER_TGID
2439 * Iterate over resources of every task of a process / task group.
2440 */
2441 enum bpf_iter_task_type {
2442 BPF_TASK_ITER_ALL = 0,
2443 BPF_TASK_ITER_TID,
2444 BPF_TASK_ITER_TGID,
2445 };
2446
2447 struct bpf_iter_aux_info {
2448 /* for map_elem iter */
2449 struct bpf_map *map;
2450
2451 /* for cgroup iter */
2452 struct {
2453 struct cgroup *start; /* starting cgroup */
2454 enum bpf_cgroup_iter_order order;
2455 } cgroup;
2456 struct {
2457 enum bpf_iter_task_type type;
2458 u32 pid;
2459 } task;
2460 };
2461
2462 typedef int (*bpf_iter_attach_target_t)(struct bpf_prog *prog,
2463 union bpf_iter_link_info *linfo,
2464 struct bpf_iter_aux_info *aux);
2465 typedef void (*bpf_iter_detach_target_t)(struct bpf_iter_aux_info *aux);
2466 typedef void (*bpf_iter_show_fdinfo_t) (const struct bpf_iter_aux_info *aux,
2467 struct seq_file *seq);
2468 typedef int (*bpf_iter_fill_link_info_t)(const struct bpf_iter_aux_info *aux,
2469 struct bpf_link_info *info);
2470 typedef const struct bpf_func_proto *
2471 (*bpf_iter_get_func_proto_t)(enum bpf_func_id func_id,
2472 const struct bpf_prog *prog);
2473
2474 enum bpf_iter_feature {
2475 BPF_ITER_RESCHED = BIT(0),
2476 };
2477
2478 #define BPF_ITER_CTX_ARG_MAX 2
2479 struct bpf_iter_reg {
2480 const char *target;
2481 bpf_iter_attach_target_t attach_target;
2482 bpf_iter_detach_target_t detach_target;
2483 bpf_iter_show_fdinfo_t show_fdinfo;
2484 bpf_iter_fill_link_info_t fill_link_info;
2485 bpf_iter_get_func_proto_t get_func_proto;
2486 u32 ctx_arg_info_size;
2487 u32 feature;
2488 struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX];
2489 const struct bpf_iter_seq_info *seq_info;
2490 };
2491
2492 struct bpf_iter_meta {
2493 __bpf_md_ptr(struct seq_file *, seq);
2494 u64 session_id;
2495 u64 seq_num;
2496 };
2497
2498 struct bpf_iter__bpf_map_elem {
2499 __bpf_md_ptr(struct bpf_iter_meta *, meta);
2500 __bpf_md_ptr(struct bpf_map *, map);
2501 __bpf_md_ptr(void *, key);
2502 __bpf_md_ptr(void *, value);
2503 };
2504
2505 int bpf_iter_reg_target(const struct bpf_iter_reg *reg_info);
2506 void bpf_iter_unreg_target(const struct bpf_iter_reg *reg_info);
2507 bool bpf_iter_prog_supported(struct bpf_prog *prog);
2508 const struct bpf_func_proto *
2509 bpf_iter_get_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog);
2510 int bpf_iter_link_attach(const union bpf_attr *attr, bpfptr_t uattr, struct bpf_prog *prog);
2511 int bpf_iter_new_fd(struct bpf_link *link);
2512 bool bpf_link_is_iter(struct bpf_link *link);
2513 struct bpf_prog *bpf_iter_get_info(struct bpf_iter_meta *meta, bool in_stop);
2514 int bpf_iter_run_prog(struct bpf_prog *prog, void *ctx);
2515 void bpf_iter_map_show_fdinfo(const struct bpf_iter_aux_info *aux,
2516 struct seq_file *seq);
2517 int bpf_iter_map_fill_link_info(const struct bpf_iter_aux_info *aux,
2518 struct bpf_link_info *info);
2519
2520 int map_set_for_each_callback_args(struct bpf_verifier_env *env,
2521 struct bpf_func_state *caller,
2522 struct bpf_func_state *callee);
2523
2524 int bpf_percpu_hash_copy(struct bpf_map *map, void *key, void *value);
2525 int bpf_percpu_array_copy(struct bpf_map *map, void *key, void *value);
2526 int bpf_percpu_hash_update(struct bpf_map *map, void *key, void *value,
2527 u64 flags);
2528 int bpf_percpu_array_update(struct bpf_map *map, void *key, void *value,
2529 u64 flags);
2530
2531 int bpf_stackmap_copy(struct bpf_map *map, void *key, void *value);
2532
2533 int bpf_fd_array_map_update_elem(struct bpf_map *map, struct file *map_file,
2534 void *key, void *value, u64 map_flags);
2535 int bpf_fd_array_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
2536 int bpf_fd_htab_map_update_elem(struct bpf_map *map, struct file *map_file,
2537 void *key, void *value, u64 map_flags);
2538 int bpf_fd_htab_map_lookup_elem(struct bpf_map *map, void *key, u32 *value);
2539
2540 int bpf_get_file_flag(int flags);
2541 int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size,
2542 size_t actual_size);
2543
2544 /* verify correctness of eBPF program */
2545 int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr, u32 uattr_size);
2546
2547 #ifndef CONFIG_BPF_JIT_ALWAYS_ON
2548 void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth);
2549 #endif
2550
2551 struct btf *bpf_get_btf_vmlinux(void);
2552
2553 /* Map specifics */
2554 struct xdp_frame;
2555 struct sk_buff;
2556 struct bpf_dtab_netdev;
2557 struct bpf_cpu_map_entry;
2558
2559 void __dev_flush(struct list_head *flush_list);
2560 int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
2561 struct net_device *dev_rx);
2562 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
2563 struct net_device *dev_rx);
2564 int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
2565 struct bpf_map *map, bool exclude_ingress);
2566 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
2567 struct bpf_prog *xdp_prog);
2568 int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
2569 struct bpf_prog *xdp_prog, struct bpf_map *map,
2570 bool exclude_ingress);
2571
2572 void __cpu_map_flush(struct list_head *flush_list);
2573 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf,
2574 struct net_device *dev_rx);
2575 int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
2576 struct sk_buff *skb);
2577
2578 /* Return map's numa specified by userspace */
bpf_map_attr_numa_node(const union bpf_attr * attr)2579 static inline int bpf_map_attr_numa_node(const union bpf_attr *attr)
2580 {
2581 return (attr->map_flags & BPF_F_NUMA_NODE) ?
2582 attr->numa_node : NUMA_NO_NODE;
2583 }
2584
2585 struct bpf_prog *bpf_prog_get_type_path(const char *name, enum bpf_prog_type type);
2586 int array_map_alloc_check(union bpf_attr *attr);
2587
2588 int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
2589 union bpf_attr __user *uattr);
2590 int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
2591 union bpf_attr __user *uattr);
2592 int bpf_prog_test_run_tracing(struct bpf_prog *prog,
2593 const union bpf_attr *kattr,
2594 union bpf_attr __user *uattr);
2595 int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
2596 const union bpf_attr *kattr,
2597 union bpf_attr __user *uattr);
2598 int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
2599 const union bpf_attr *kattr,
2600 union bpf_attr __user *uattr);
2601 int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
2602 const union bpf_attr *kattr,
2603 union bpf_attr __user *uattr);
2604 int bpf_prog_test_run_nf(struct bpf_prog *prog,
2605 const union bpf_attr *kattr,
2606 union bpf_attr __user *uattr);
2607 bool btf_ctx_access(int off, int size, enum bpf_access_type type,
2608 const struct bpf_prog *prog,
2609 struct bpf_insn_access_aux *info);
2610
bpf_tracing_ctx_access(int off,int size,enum bpf_access_type type)2611 static inline bool bpf_tracing_ctx_access(int off, int size,
2612 enum bpf_access_type type)
2613 {
2614 if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
2615 return false;
2616 if (type != BPF_READ)
2617 return false;
2618 if (off % size != 0)
2619 return false;
2620 return true;
2621 }
2622
bpf_tracing_btf_ctx_access(int off,int size,enum bpf_access_type type,const struct bpf_prog * prog,struct bpf_insn_access_aux * info)2623 static inline bool bpf_tracing_btf_ctx_access(int off, int size,
2624 enum bpf_access_type type,
2625 const struct bpf_prog *prog,
2626 struct bpf_insn_access_aux *info)
2627 {
2628 if (!bpf_tracing_ctx_access(off, size, type))
2629 return false;
2630 return btf_ctx_access(off, size, type, prog, info);
2631 }
2632
2633 int btf_struct_access(struct bpf_verifier_log *log,
2634 const struct bpf_reg_state *reg,
2635 int off, int size, enum bpf_access_type atype,
2636 u32 *next_btf_id, enum bpf_type_flag *flag, const char **field_name);
2637 bool btf_struct_ids_match(struct bpf_verifier_log *log,
2638 const struct btf *btf, u32 id, int off,
2639 const struct btf *need_btf, u32 need_type_id,
2640 bool strict);
2641
2642 int btf_distill_func_proto(struct bpf_verifier_log *log,
2643 struct btf *btf,
2644 const struct btf_type *func_proto,
2645 const char *func_name,
2646 struct btf_func_model *m);
2647
2648 struct bpf_reg_state;
2649 int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog);
2650 int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
2651 struct btf *btf, const struct btf_type *t);
2652 const char *btf_find_decl_tag_value(const struct btf *btf, const struct btf_type *pt,
2653 int comp_idx, const char *tag_key);
2654 int btf_find_next_decl_tag(const struct btf *btf, const struct btf_type *pt,
2655 int comp_idx, const char *tag_key, int last_id);
2656
2657 struct bpf_prog *bpf_prog_by_id(u32 id);
2658 struct bpf_link *bpf_link_by_id(u32 id);
2659
2660 const struct bpf_func_proto *bpf_base_func_proto(enum bpf_func_id func_id,
2661 const struct bpf_prog *prog);
2662 void bpf_task_storage_free(struct task_struct *task);
2663 void bpf_cgrp_storage_free(struct cgroup *cgroup);
2664 bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog);
2665 const struct btf_func_model *
2666 bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
2667 const struct bpf_insn *insn);
2668 int bpf_get_kfunc_addr(const struct bpf_prog *prog, u32 func_id,
2669 u16 btf_fd_idx, u8 **func_addr);
2670
2671 struct bpf_core_ctx {
2672 struct bpf_verifier_log *log;
2673 const struct btf *btf;
2674 };
2675
2676 bool btf_nested_type_is_trusted(struct bpf_verifier_log *log,
2677 const struct bpf_reg_state *reg,
2678 const char *field_name, u32 btf_id, const char *suffix);
2679
2680 bool btf_type_ids_nocast_alias(struct bpf_verifier_log *log,
2681 const struct btf *reg_btf, u32 reg_id,
2682 const struct btf *arg_btf, u32 arg_id);
2683
2684 int bpf_core_apply(struct bpf_core_ctx *ctx, const struct bpf_core_relo *relo,
2685 int relo_idx, void *insn);
2686
unprivileged_ebpf_enabled(void)2687 static inline bool unprivileged_ebpf_enabled(void)
2688 {
2689 return !sysctl_unprivileged_bpf_disabled;
2690 }
2691
2692 /* Not all bpf prog type has the bpf_ctx.
2693 * For the bpf prog type that has initialized the bpf_ctx,
2694 * this function can be used to decide if a kernel function
2695 * is called by a bpf program.
2696 */
has_current_bpf_ctx(void)2697 static inline bool has_current_bpf_ctx(void)
2698 {
2699 return !!current->bpf_ctx;
2700 }
2701
2702 void notrace bpf_prog_inc_misses_counter(struct bpf_prog *prog);
2703
2704 void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
2705 enum bpf_dynptr_type type, u32 offset, u32 size);
2706 void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
2707 void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr);
2708
2709 #else /* !CONFIG_BPF_SYSCALL */
bpf_prog_get(u32 ufd)2710 static inline struct bpf_prog *bpf_prog_get(u32 ufd)
2711 {
2712 return ERR_PTR(-EOPNOTSUPP);
2713 }
2714
bpf_prog_get_type_dev(u32 ufd,enum bpf_prog_type type,bool attach_drv)2715 static inline struct bpf_prog *bpf_prog_get_type_dev(u32 ufd,
2716 enum bpf_prog_type type,
2717 bool attach_drv)
2718 {
2719 return ERR_PTR(-EOPNOTSUPP);
2720 }
2721
bpf_prog_add(struct bpf_prog * prog,int i)2722 static inline void bpf_prog_add(struct bpf_prog *prog, int i)
2723 {
2724 }
2725
bpf_prog_sub(struct bpf_prog * prog,int i)2726 static inline void bpf_prog_sub(struct bpf_prog *prog, int i)
2727 {
2728 }
2729
bpf_prog_put(struct bpf_prog * prog)2730 static inline void bpf_prog_put(struct bpf_prog *prog)
2731 {
2732 }
2733
bpf_prog_inc(struct bpf_prog * prog)2734 static inline void bpf_prog_inc(struct bpf_prog *prog)
2735 {
2736 }
2737
2738 static inline struct bpf_prog *__must_check
bpf_prog_inc_not_zero(struct bpf_prog * prog)2739 bpf_prog_inc_not_zero(struct bpf_prog *prog)
2740 {
2741 return ERR_PTR(-EOPNOTSUPP);
2742 }
2743
bpf_link_init(struct bpf_link * link,enum bpf_link_type type,const struct bpf_link_ops * ops,struct bpf_prog * prog)2744 static inline void bpf_link_init(struct bpf_link *link, enum bpf_link_type type,
2745 const struct bpf_link_ops *ops,
2746 struct bpf_prog *prog)
2747 {
2748 }
2749
bpf_link_prime(struct bpf_link * link,struct bpf_link_primer * primer)2750 static inline int bpf_link_prime(struct bpf_link *link,
2751 struct bpf_link_primer *primer)
2752 {
2753 return -EOPNOTSUPP;
2754 }
2755
bpf_link_settle(struct bpf_link_primer * primer)2756 static inline int bpf_link_settle(struct bpf_link_primer *primer)
2757 {
2758 return -EOPNOTSUPP;
2759 }
2760
bpf_link_cleanup(struct bpf_link_primer * primer)2761 static inline void bpf_link_cleanup(struct bpf_link_primer *primer)
2762 {
2763 }
2764
bpf_link_inc(struct bpf_link * link)2765 static inline void bpf_link_inc(struct bpf_link *link)
2766 {
2767 }
2768
bpf_link_inc_not_zero(struct bpf_link * link)2769 static inline struct bpf_link *bpf_link_inc_not_zero(struct bpf_link *link)
2770 {
2771 return NULL;
2772 }
2773
bpf_link_put(struct bpf_link * link)2774 static inline void bpf_link_put(struct bpf_link *link)
2775 {
2776 }
2777
bpf_obj_get_user(const char __user * pathname,int flags)2778 static inline int bpf_obj_get_user(const char __user *pathname, int flags)
2779 {
2780 return -EOPNOTSUPP;
2781 }
2782
bpf_token_capable(const struct bpf_token * token,int cap)2783 static inline bool bpf_token_capable(const struct bpf_token *token, int cap)
2784 {
2785 return capable(cap) || (cap != CAP_SYS_ADMIN && capable(CAP_SYS_ADMIN));
2786 }
2787
bpf_token_inc(struct bpf_token * token)2788 static inline void bpf_token_inc(struct bpf_token *token)
2789 {
2790 }
2791
bpf_token_put(struct bpf_token * token)2792 static inline void bpf_token_put(struct bpf_token *token)
2793 {
2794 }
2795
bpf_token_get_from_fd(u32 ufd)2796 static inline struct bpf_token *bpf_token_get_from_fd(u32 ufd)
2797 {
2798 return ERR_PTR(-EOPNOTSUPP);
2799 }
2800
__dev_flush(struct list_head * flush_list)2801 static inline void __dev_flush(struct list_head *flush_list)
2802 {
2803 }
2804
2805 struct xdp_frame;
2806 struct bpf_dtab_netdev;
2807 struct bpf_cpu_map_entry;
2808
2809 static inline
dev_xdp_enqueue(struct net_device * dev,struct xdp_frame * xdpf,struct net_device * dev_rx)2810 int dev_xdp_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
2811 struct net_device *dev_rx)
2812 {
2813 return 0;
2814 }
2815
2816 static inline
dev_map_enqueue(struct bpf_dtab_netdev * dst,struct xdp_frame * xdpf,struct net_device * dev_rx)2817 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_frame *xdpf,
2818 struct net_device *dev_rx)
2819 {
2820 return 0;
2821 }
2822
2823 static inline
dev_map_enqueue_multi(struct xdp_frame * xdpf,struct net_device * dev_rx,struct bpf_map * map,bool exclude_ingress)2824 int dev_map_enqueue_multi(struct xdp_frame *xdpf, struct net_device *dev_rx,
2825 struct bpf_map *map, bool exclude_ingress)
2826 {
2827 return 0;
2828 }
2829
2830 struct sk_buff;
2831
dev_map_generic_redirect(struct bpf_dtab_netdev * dst,struct sk_buff * skb,struct bpf_prog * xdp_prog)2832 static inline int dev_map_generic_redirect(struct bpf_dtab_netdev *dst,
2833 struct sk_buff *skb,
2834 struct bpf_prog *xdp_prog)
2835 {
2836 return 0;
2837 }
2838
2839 static inline
dev_map_redirect_multi(struct net_device * dev,struct sk_buff * skb,struct bpf_prog * xdp_prog,struct bpf_map * map,bool exclude_ingress)2840 int dev_map_redirect_multi(struct net_device *dev, struct sk_buff *skb,
2841 struct bpf_prog *xdp_prog, struct bpf_map *map,
2842 bool exclude_ingress)
2843 {
2844 return 0;
2845 }
2846
__cpu_map_flush(struct list_head * flush_list)2847 static inline void __cpu_map_flush(struct list_head *flush_list)
2848 {
2849 }
2850
cpu_map_enqueue(struct bpf_cpu_map_entry * rcpu,struct xdp_frame * xdpf,struct net_device * dev_rx)2851 static inline int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu,
2852 struct xdp_frame *xdpf,
2853 struct net_device *dev_rx)
2854 {
2855 return 0;
2856 }
2857
cpu_map_generic_redirect(struct bpf_cpu_map_entry * rcpu,struct sk_buff * skb)2858 static inline int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
2859 struct sk_buff *skb)
2860 {
2861 return -EOPNOTSUPP;
2862 }
2863
bpf_prog_get_type_path(const char * name,enum bpf_prog_type type)2864 static inline struct bpf_prog *bpf_prog_get_type_path(const char *name,
2865 enum bpf_prog_type type)
2866 {
2867 return ERR_PTR(-EOPNOTSUPP);
2868 }
2869
bpf_prog_test_run_xdp(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)2870 static inline int bpf_prog_test_run_xdp(struct bpf_prog *prog,
2871 const union bpf_attr *kattr,
2872 union bpf_attr __user *uattr)
2873 {
2874 return -ENOTSUPP;
2875 }
2876
bpf_prog_test_run_skb(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)2877 static inline int bpf_prog_test_run_skb(struct bpf_prog *prog,
2878 const union bpf_attr *kattr,
2879 union bpf_attr __user *uattr)
2880 {
2881 return -ENOTSUPP;
2882 }
2883
bpf_prog_test_run_tracing(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)2884 static inline int bpf_prog_test_run_tracing(struct bpf_prog *prog,
2885 const union bpf_attr *kattr,
2886 union bpf_attr __user *uattr)
2887 {
2888 return -ENOTSUPP;
2889 }
2890
bpf_prog_test_run_flow_dissector(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)2891 static inline int bpf_prog_test_run_flow_dissector(struct bpf_prog *prog,
2892 const union bpf_attr *kattr,
2893 union bpf_attr __user *uattr)
2894 {
2895 return -ENOTSUPP;
2896 }
2897
bpf_prog_test_run_sk_lookup(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)2898 static inline int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,
2899 const union bpf_attr *kattr,
2900 union bpf_attr __user *uattr)
2901 {
2902 return -ENOTSUPP;
2903 }
2904
bpf_map_put(struct bpf_map * map)2905 static inline void bpf_map_put(struct bpf_map *map)
2906 {
2907 }
2908
bpf_prog_by_id(u32 id)2909 static inline struct bpf_prog *bpf_prog_by_id(u32 id)
2910 {
2911 return ERR_PTR(-ENOTSUPP);
2912 }
2913
btf_struct_access(struct bpf_verifier_log * log,const struct bpf_reg_state * reg,int off,int size,enum bpf_access_type atype,u32 * next_btf_id,enum bpf_type_flag * flag,const char ** field_name)2914 static inline int btf_struct_access(struct bpf_verifier_log *log,
2915 const struct bpf_reg_state *reg,
2916 int off, int size, enum bpf_access_type atype,
2917 u32 *next_btf_id, enum bpf_type_flag *flag,
2918 const char **field_name)
2919 {
2920 return -EACCES;
2921 }
2922
2923 static inline const struct bpf_func_proto *
bpf_base_func_proto(enum bpf_func_id func_id,const struct bpf_prog * prog)2924 bpf_base_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
2925 {
2926 return NULL;
2927 }
2928
bpf_task_storage_free(struct task_struct * task)2929 static inline void bpf_task_storage_free(struct task_struct *task)
2930 {
2931 }
2932
bpf_prog_has_kfunc_call(const struct bpf_prog * prog)2933 static inline bool bpf_prog_has_kfunc_call(const struct bpf_prog *prog)
2934 {
2935 return false;
2936 }
2937
2938 static inline const struct btf_func_model *
bpf_jit_find_kfunc_model(const struct bpf_prog * prog,const struct bpf_insn * insn)2939 bpf_jit_find_kfunc_model(const struct bpf_prog *prog,
2940 const struct bpf_insn *insn)
2941 {
2942 return NULL;
2943 }
2944
2945 static inline int
bpf_get_kfunc_addr(const struct bpf_prog * prog,u32 func_id,u16 btf_fd_idx,u8 ** func_addr)2946 bpf_get_kfunc_addr(const struct bpf_prog *prog, u32 func_id,
2947 u16 btf_fd_idx, u8 **func_addr)
2948 {
2949 return -ENOTSUPP;
2950 }
2951
unprivileged_ebpf_enabled(void)2952 static inline bool unprivileged_ebpf_enabled(void)
2953 {
2954 return false;
2955 }
2956
has_current_bpf_ctx(void)2957 static inline bool has_current_bpf_ctx(void)
2958 {
2959 return false;
2960 }
2961
bpf_prog_inc_misses_counter(struct bpf_prog * prog)2962 static inline void bpf_prog_inc_misses_counter(struct bpf_prog *prog)
2963 {
2964 }
2965
bpf_cgrp_storage_free(struct cgroup * cgroup)2966 static inline void bpf_cgrp_storage_free(struct cgroup *cgroup)
2967 {
2968 }
2969
bpf_dynptr_init(struct bpf_dynptr_kern * ptr,void * data,enum bpf_dynptr_type type,u32 offset,u32 size)2970 static inline void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
2971 enum bpf_dynptr_type type, u32 offset, u32 size)
2972 {
2973 }
2974
bpf_dynptr_set_null(struct bpf_dynptr_kern * ptr)2975 static inline void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)
2976 {
2977 }
2978
bpf_dynptr_set_rdonly(struct bpf_dynptr_kern * ptr)2979 static inline void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr)
2980 {
2981 }
2982 #endif /* CONFIG_BPF_SYSCALL */
2983
2984 static __always_inline int
bpf_probe_read_kernel_common(void * dst,u32 size,const void * unsafe_ptr)2985 bpf_probe_read_kernel_common(void *dst, u32 size, const void *unsafe_ptr)
2986 {
2987 int ret = -EFAULT;
2988
2989 if (IS_ENABLED(CONFIG_BPF_EVENTS))
2990 ret = copy_from_kernel_nofault(dst, unsafe_ptr, size);
2991 if (unlikely(ret < 0))
2992 memset(dst, 0, size);
2993 return ret;
2994 }
2995
2996 void __bpf_free_used_btfs(struct btf_mod_pair *used_btfs, u32 len);
2997
bpf_prog_get_type(u32 ufd,enum bpf_prog_type type)2998 static inline struct bpf_prog *bpf_prog_get_type(u32 ufd,
2999 enum bpf_prog_type type)
3000 {
3001 return bpf_prog_get_type_dev(ufd, type, false);
3002 }
3003
3004 void __bpf_free_used_maps(struct bpf_prog_aux *aux,
3005 struct bpf_map **used_maps, u32 len);
3006
3007 bool bpf_prog_get_ok(struct bpf_prog *, enum bpf_prog_type *, bool);
3008
3009 int bpf_prog_offload_compile(struct bpf_prog *prog);
3010 void bpf_prog_dev_bound_destroy(struct bpf_prog *prog);
3011 int bpf_prog_offload_info_fill(struct bpf_prog_info *info,
3012 struct bpf_prog *prog);
3013
3014 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map);
3015
3016 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value);
3017 int bpf_map_offload_update_elem(struct bpf_map *map,
3018 void *key, void *value, u64 flags);
3019 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key);
3020 int bpf_map_offload_get_next_key(struct bpf_map *map,
3021 void *key, void *next_key);
3022
3023 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map);
3024
3025 struct bpf_offload_dev *
3026 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv);
3027 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev);
3028 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev);
3029 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev,
3030 struct net_device *netdev);
3031 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev,
3032 struct net_device *netdev);
3033 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev);
3034
3035 void unpriv_ebpf_notify(int new_state);
3036
3037 #if defined(CONFIG_NET) && defined(CONFIG_BPF_SYSCALL)
3038 int bpf_dev_bound_kfunc_check(struct bpf_verifier_log *log,
3039 struct bpf_prog_aux *prog_aux);
3040 void *bpf_dev_bound_resolve_kfunc(struct bpf_prog *prog, u32 func_id);
3041 int bpf_prog_dev_bound_init(struct bpf_prog *prog, union bpf_attr *attr);
3042 int bpf_prog_dev_bound_inherit(struct bpf_prog *new_prog, struct bpf_prog *old_prog);
3043 void bpf_dev_bound_netdev_unregister(struct net_device *dev);
3044
bpf_prog_is_dev_bound(const struct bpf_prog_aux * aux)3045 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
3046 {
3047 return aux->dev_bound;
3048 }
3049
bpf_prog_is_offloaded(const struct bpf_prog_aux * aux)3050 static inline bool bpf_prog_is_offloaded(const struct bpf_prog_aux *aux)
3051 {
3052 return aux->offload_requested;
3053 }
3054
3055 bool bpf_prog_dev_bound_match(const struct bpf_prog *lhs, const struct bpf_prog *rhs);
3056
bpf_map_is_offloaded(struct bpf_map * map)3057 static inline bool bpf_map_is_offloaded(struct bpf_map *map)
3058 {
3059 return unlikely(map->ops == &bpf_map_offload_ops);
3060 }
3061
3062 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr);
3063 void bpf_map_offload_map_free(struct bpf_map *map);
3064 u64 bpf_map_offload_map_mem_usage(const struct bpf_map *map);
3065 int bpf_prog_test_run_syscall(struct bpf_prog *prog,
3066 const union bpf_attr *kattr,
3067 union bpf_attr __user *uattr);
3068
3069 int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog);
3070 int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype);
3071 int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags);
3072 int sock_map_bpf_prog_query(const union bpf_attr *attr,
3073 union bpf_attr __user *uattr);
3074 int sock_map_link_create(const union bpf_attr *attr, struct bpf_prog *prog);
3075
3076 void sock_map_unhash(struct sock *sk);
3077 void sock_map_destroy(struct sock *sk);
3078 void sock_map_close(struct sock *sk, long timeout);
3079 #else
bpf_dev_bound_kfunc_check(struct bpf_verifier_log * log,struct bpf_prog_aux * prog_aux)3080 static inline int bpf_dev_bound_kfunc_check(struct bpf_verifier_log *log,
3081 struct bpf_prog_aux *prog_aux)
3082 {
3083 return -EOPNOTSUPP;
3084 }
3085
bpf_dev_bound_resolve_kfunc(struct bpf_prog * prog,u32 func_id)3086 static inline void *bpf_dev_bound_resolve_kfunc(struct bpf_prog *prog,
3087 u32 func_id)
3088 {
3089 return NULL;
3090 }
3091
bpf_prog_dev_bound_init(struct bpf_prog * prog,union bpf_attr * attr)3092 static inline int bpf_prog_dev_bound_init(struct bpf_prog *prog,
3093 union bpf_attr *attr)
3094 {
3095 return -EOPNOTSUPP;
3096 }
3097
bpf_prog_dev_bound_inherit(struct bpf_prog * new_prog,struct bpf_prog * old_prog)3098 static inline int bpf_prog_dev_bound_inherit(struct bpf_prog *new_prog,
3099 struct bpf_prog *old_prog)
3100 {
3101 return -EOPNOTSUPP;
3102 }
3103
bpf_dev_bound_netdev_unregister(struct net_device * dev)3104 static inline void bpf_dev_bound_netdev_unregister(struct net_device *dev)
3105 {
3106 }
3107
bpf_prog_is_dev_bound(const struct bpf_prog_aux * aux)3108 static inline bool bpf_prog_is_dev_bound(const struct bpf_prog_aux *aux)
3109 {
3110 return false;
3111 }
3112
bpf_prog_is_offloaded(struct bpf_prog_aux * aux)3113 static inline bool bpf_prog_is_offloaded(struct bpf_prog_aux *aux)
3114 {
3115 return false;
3116 }
3117
bpf_prog_dev_bound_match(const struct bpf_prog * lhs,const struct bpf_prog * rhs)3118 static inline bool bpf_prog_dev_bound_match(const struct bpf_prog *lhs, const struct bpf_prog *rhs)
3119 {
3120 return false;
3121 }
3122
bpf_map_is_offloaded(struct bpf_map * map)3123 static inline bool bpf_map_is_offloaded(struct bpf_map *map)
3124 {
3125 return false;
3126 }
3127
bpf_map_offload_map_alloc(union bpf_attr * attr)3128 static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr)
3129 {
3130 return ERR_PTR(-EOPNOTSUPP);
3131 }
3132
bpf_map_offload_map_free(struct bpf_map * map)3133 static inline void bpf_map_offload_map_free(struct bpf_map *map)
3134 {
3135 }
3136
bpf_map_offload_map_mem_usage(const struct bpf_map * map)3137 static inline u64 bpf_map_offload_map_mem_usage(const struct bpf_map *map)
3138 {
3139 return 0;
3140 }
3141
bpf_prog_test_run_syscall(struct bpf_prog * prog,const union bpf_attr * kattr,union bpf_attr __user * uattr)3142 static inline int bpf_prog_test_run_syscall(struct bpf_prog *prog,
3143 const union bpf_attr *kattr,
3144 union bpf_attr __user *uattr)
3145 {
3146 return -ENOTSUPP;
3147 }
3148
3149 #ifdef CONFIG_BPF_SYSCALL
sock_map_get_from_fd(const union bpf_attr * attr,struct bpf_prog * prog)3150 static inline int sock_map_get_from_fd(const union bpf_attr *attr,
3151 struct bpf_prog *prog)
3152 {
3153 return -EINVAL;
3154 }
3155
sock_map_prog_detach(const union bpf_attr * attr,enum bpf_prog_type ptype)3156 static inline int sock_map_prog_detach(const union bpf_attr *attr,
3157 enum bpf_prog_type ptype)
3158 {
3159 return -EOPNOTSUPP;
3160 }
3161
sock_map_update_elem_sys(struct bpf_map * map,void * key,void * value,u64 flags)3162 static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value,
3163 u64 flags)
3164 {
3165 return -EOPNOTSUPP;
3166 }
3167
sock_map_bpf_prog_query(const union bpf_attr * attr,union bpf_attr __user * uattr)3168 static inline int sock_map_bpf_prog_query(const union bpf_attr *attr,
3169 union bpf_attr __user *uattr)
3170 {
3171 return -EINVAL;
3172 }
3173
sock_map_link_create(const union bpf_attr * attr,struct bpf_prog * prog)3174 static inline int sock_map_link_create(const union bpf_attr *attr, struct bpf_prog *prog)
3175 {
3176 return -EOPNOTSUPP;
3177 }
3178 #endif /* CONFIG_BPF_SYSCALL */
3179 #endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */
3180
3181 static __always_inline void
bpf_prog_inc_misses_counters(const struct bpf_prog_array * array)3182 bpf_prog_inc_misses_counters(const struct bpf_prog_array *array)
3183 {
3184 const struct bpf_prog_array_item *item;
3185 struct bpf_prog *prog;
3186
3187 if (unlikely(!array))
3188 return;
3189
3190 item = &array->items[0];
3191 while ((prog = READ_ONCE(item->prog))) {
3192 bpf_prog_inc_misses_counter(prog);
3193 item++;
3194 }
3195 }
3196
3197 #if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL)
3198 void bpf_sk_reuseport_detach(struct sock *sk);
3199 int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key,
3200 void *value);
3201 int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key,
3202 void *value, u64 map_flags);
3203 #else
bpf_sk_reuseport_detach(struct sock * sk)3204 static inline void bpf_sk_reuseport_detach(struct sock *sk)
3205 {
3206 }
3207
3208 #ifdef CONFIG_BPF_SYSCALL
bpf_fd_reuseport_array_lookup_elem(struct bpf_map * map,void * key,void * value)3209 static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map,
3210 void *key, void *value)
3211 {
3212 return -EOPNOTSUPP;
3213 }
3214
bpf_fd_reuseport_array_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags)3215 static inline int bpf_fd_reuseport_array_update_elem(struct bpf_map *map,
3216 void *key, void *value,
3217 u64 map_flags)
3218 {
3219 return -EOPNOTSUPP;
3220 }
3221 #endif /* CONFIG_BPF_SYSCALL */
3222 #endif /* defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) */
3223
3224 /* verifier prototypes for helper functions called from eBPF programs */
3225 extern const struct bpf_func_proto bpf_map_lookup_elem_proto;
3226 extern const struct bpf_func_proto bpf_map_update_elem_proto;
3227 extern const struct bpf_func_proto bpf_map_delete_elem_proto;
3228 extern const struct bpf_func_proto bpf_map_push_elem_proto;
3229 extern const struct bpf_func_proto bpf_map_pop_elem_proto;
3230 extern const struct bpf_func_proto bpf_map_peek_elem_proto;
3231 extern const struct bpf_func_proto bpf_map_lookup_percpu_elem_proto;
3232
3233 extern const struct bpf_func_proto bpf_get_prandom_u32_proto;
3234 extern const struct bpf_func_proto bpf_get_smp_processor_id_proto;
3235 extern const struct bpf_func_proto bpf_get_numa_node_id_proto;
3236 extern const struct bpf_func_proto bpf_tail_call_proto;
3237 extern const struct bpf_func_proto bpf_ktime_get_ns_proto;
3238 extern const struct bpf_func_proto bpf_ktime_get_boot_ns_proto;
3239 extern const struct bpf_func_proto bpf_ktime_get_tai_ns_proto;
3240 extern const struct bpf_func_proto bpf_get_current_pid_tgid_proto;
3241 extern const struct bpf_func_proto bpf_get_current_uid_gid_proto;
3242 extern const struct bpf_func_proto bpf_get_current_comm_proto;
3243 extern const struct bpf_func_proto bpf_get_stackid_proto;
3244 extern const struct bpf_func_proto bpf_get_stack_proto;
3245 extern const struct bpf_func_proto bpf_get_stack_sleepable_proto;
3246 extern const struct bpf_func_proto bpf_get_task_stack_proto;
3247 extern const struct bpf_func_proto bpf_get_task_stack_sleepable_proto;
3248 extern const struct bpf_func_proto bpf_get_stackid_proto_pe;
3249 extern const struct bpf_func_proto bpf_get_stack_proto_pe;
3250 extern const struct bpf_func_proto bpf_sock_map_update_proto;
3251 extern const struct bpf_func_proto bpf_sock_hash_update_proto;
3252 extern const struct bpf_func_proto bpf_get_current_cgroup_id_proto;
3253 extern const struct bpf_func_proto bpf_get_current_ancestor_cgroup_id_proto;
3254 extern const struct bpf_func_proto bpf_get_cgroup_classid_curr_proto;
3255 extern const struct bpf_func_proto bpf_current_task_under_cgroup_proto;
3256 extern const struct bpf_func_proto bpf_msg_redirect_hash_proto;
3257 extern const struct bpf_func_proto bpf_msg_redirect_map_proto;
3258 extern const struct bpf_func_proto bpf_sk_redirect_hash_proto;
3259 extern const struct bpf_func_proto bpf_sk_redirect_map_proto;
3260 extern const struct bpf_func_proto bpf_spin_lock_proto;
3261 extern const struct bpf_func_proto bpf_spin_unlock_proto;
3262 extern const struct bpf_func_proto bpf_get_local_storage_proto;
3263 extern const struct bpf_func_proto bpf_strtol_proto;
3264 extern const struct bpf_func_proto bpf_strtoul_proto;
3265 extern const struct bpf_func_proto bpf_tcp_sock_proto;
3266 extern const struct bpf_func_proto bpf_jiffies64_proto;
3267 extern const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto;
3268 extern const struct bpf_func_proto bpf_event_output_data_proto;
3269 extern const struct bpf_func_proto bpf_ringbuf_output_proto;
3270 extern const struct bpf_func_proto bpf_ringbuf_reserve_proto;
3271 extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
3272 extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
3273 extern const struct bpf_func_proto bpf_ringbuf_query_proto;
3274 extern const struct bpf_func_proto bpf_ringbuf_reserve_dynptr_proto;
3275 extern const struct bpf_func_proto bpf_ringbuf_submit_dynptr_proto;
3276 extern const struct bpf_func_proto bpf_ringbuf_discard_dynptr_proto;
3277 extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
3278 extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
3279 extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
3280 extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
3281 extern const struct bpf_func_proto bpf_skc_to_udp6_sock_proto;
3282 extern const struct bpf_func_proto bpf_skc_to_unix_sock_proto;
3283 extern const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto;
3284 extern const struct bpf_func_proto bpf_copy_from_user_proto;
3285 extern const struct bpf_func_proto bpf_snprintf_btf_proto;
3286 extern const struct bpf_func_proto bpf_snprintf_proto;
3287 extern const struct bpf_func_proto bpf_per_cpu_ptr_proto;
3288 extern const struct bpf_func_proto bpf_this_cpu_ptr_proto;
3289 extern const struct bpf_func_proto bpf_ktime_get_coarse_ns_proto;
3290 extern const struct bpf_func_proto bpf_sock_from_file_proto;
3291 extern const struct bpf_func_proto bpf_get_socket_ptr_cookie_proto;
3292 extern const struct bpf_func_proto bpf_task_storage_get_recur_proto;
3293 extern const struct bpf_func_proto bpf_task_storage_get_proto;
3294 extern const struct bpf_func_proto bpf_task_storage_delete_recur_proto;
3295 extern const struct bpf_func_proto bpf_task_storage_delete_proto;
3296 extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
3297 extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;
3298 extern const struct bpf_func_proto bpf_sk_setsockopt_proto;
3299 extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
3300 extern const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto;
3301 extern const struct bpf_func_proto bpf_unlocked_sk_getsockopt_proto;
3302 extern const struct bpf_func_proto bpf_find_vma_proto;
3303 extern const struct bpf_func_proto bpf_loop_proto;
3304 extern const struct bpf_func_proto bpf_copy_from_user_task_proto;
3305 extern const struct bpf_func_proto bpf_set_retval_proto;
3306 extern const struct bpf_func_proto bpf_get_retval_proto;
3307 extern const struct bpf_func_proto bpf_user_ringbuf_drain_proto;
3308 extern const struct bpf_func_proto bpf_cgrp_storage_get_proto;
3309 extern const struct bpf_func_proto bpf_cgrp_storage_delete_proto;
3310
3311 const struct bpf_func_proto *tracing_prog_func_proto(
3312 enum bpf_func_id func_id, const struct bpf_prog *prog);
3313
3314 /* Shared helpers among cBPF and eBPF. */
3315 void bpf_user_rnd_init_once(void);
3316 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
3317 u64 bpf_get_raw_cpu_id(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
3318
3319 #if defined(CONFIG_NET)
3320 bool bpf_sock_common_is_valid_access(int off, int size,
3321 enum bpf_access_type type,
3322 struct bpf_insn_access_aux *info);
3323 bool bpf_sock_is_valid_access(int off, int size, enum bpf_access_type type,
3324 struct bpf_insn_access_aux *info);
3325 u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
3326 const struct bpf_insn *si,
3327 struct bpf_insn *insn_buf,
3328 struct bpf_prog *prog,
3329 u32 *target_size);
3330 int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags,
3331 struct bpf_dynptr *ptr);
3332 #else
bpf_sock_common_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)3333 static inline bool bpf_sock_common_is_valid_access(int off, int size,
3334 enum bpf_access_type type,
3335 struct bpf_insn_access_aux *info)
3336 {
3337 return false;
3338 }
bpf_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)3339 static inline bool bpf_sock_is_valid_access(int off, int size,
3340 enum bpf_access_type type,
3341 struct bpf_insn_access_aux *info)
3342 {
3343 return false;
3344 }
bpf_sock_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)3345 static inline u32 bpf_sock_convert_ctx_access(enum bpf_access_type type,
3346 const struct bpf_insn *si,
3347 struct bpf_insn *insn_buf,
3348 struct bpf_prog *prog,
3349 u32 *target_size)
3350 {
3351 return 0;
3352 }
bpf_dynptr_from_skb_rdonly(struct __sk_buff * skb,u64 flags,struct bpf_dynptr * ptr)3353 static inline int bpf_dynptr_from_skb_rdonly(struct __sk_buff *skb, u64 flags,
3354 struct bpf_dynptr *ptr)
3355 {
3356 return -EOPNOTSUPP;
3357 }
3358 #endif
3359
3360 #ifdef CONFIG_INET
3361 struct sk_reuseport_kern {
3362 struct sk_buff *skb;
3363 struct sock *sk;
3364 struct sock *selected_sk;
3365 struct sock *migrating_sk;
3366 void *data_end;
3367 u32 hash;
3368 u32 reuseport_id;
3369 bool bind_inany;
3370 };
3371 bool bpf_tcp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
3372 struct bpf_insn_access_aux *info);
3373
3374 u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
3375 const struct bpf_insn *si,
3376 struct bpf_insn *insn_buf,
3377 struct bpf_prog *prog,
3378 u32 *target_size);
3379
3380 bool bpf_xdp_sock_is_valid_access(int off, int size, enum bpf_access_type type,
3381 struct bpf_insn_access_aux *info);
3382
3383 u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
3384 const struct bpf_insn *si,
3385 struct bpf_insn *insn_buf,
3386 struct bpf_prog *prog,
3387 u32 *target_size);
3388 #else
bpf_tcp_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)3389 static inline bool bpf_tcp_sock_is_valid_access(int off, int size,
3390 enum bpf_access_type type,
3391 struct bpf_insn_access_aux *info)
3392 {
3393 return false;
3394 }
3395
bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)3396 static inline u32 bpf_tcp_sock_convert_ctx_access(enum bpf_access_type type,
3397 const struct bpf_insn *si,
3398 struct bpf_insn *insn_buf,
3399 struct bpf_prog *prog,
3400 u32 *target_size)
3401 {
3402 return 0;
3403 }
bpf_xdp_sock_is_valid_access(int off,int size,enum bpf_access_type type,struct bpf_insn_access_aux * info)3404 static inline bool bpf_xdp_sock_is_valid_access(int off, int size,
3405 enum bpf_access_type type,
3406 struct bpf_insn_access_aux *info)
3407 {
3408 return false;
3409 }
3410
bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,const struct bpf_insn * si,struct bpf_insn * insn_buf,struct bpf_prog * prog,u32 * target_size)3411 static inline u32 bpf_xdp_sock_convert_ctx_access(enum bpf_access_type type,
3412 const struct bpf_insn *si,
3413 struct bpf_insn *insn_buf,
3414 struct bpf_prog *prog,
3415 u32 *target_size)
3416 {
3417 return 0;
3418 }
3419 #endif /* CONFIG_INET */
3420
3421 enum bpf_text_poke_type {
3422 BPF_MOD_CALL,
3423 BPF_MOD_JUMP,
3424 };
3425
3426 int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type t,
3427 void *addr1, void *addr2);
3428
3429 void bpf_arch_poke_desc_update(struct bpf_jit_poke_descriptor *poke,
3430 struct bpf_prog *new, struct bpf_prog *old);
3431
3432 void *bpf_arch_text_copy(void *dst, void *src, size_t len);
3433 int bpf_arch_text_invalidate(void *dst, size_t len);
3434
3435 struct btf_id_set;
3436 bool btf_id_set_contains(const struct btf_id_set *set, u32 id);
3437
3438 #define MAX_BPRINTF_VARARGS 12
3439 #define MAX_BPRINTF_BUF 1024
3440
3441 struct bpf_bprintf_data {
3442 u32 *bin_args;
3443 char *buf;
3444 bool get_bin_args;
3445 bool get_buf;
3446 };
3447
3448 int bpf_bprintf_prepare(char *fmt, u32 fmt_size, const u64 *raw_args,
3449 u32 num_args, struct bpf_bprintf_data *data);
3450 void bpf_bprintf_cleanup(struct bpf_bprintf_data *data);
3451
3452 #ifdef CONFIG_BPF_LSM
3453 void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype);
3454 void bpf_cgroup_atype_put(int cgroup_atype);
3455 #else
bpf_cgroup_atype_get(u32 attach_btf_id,int cgroup_atype)3456 static inline void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype) {}
bpf_cgroup_atype_put(int cgroup_atype)3457 static inline void bpf_cgroup_atype_put(int cgroup_atype) {}
3458 #endif /* CONFIG_BPF_LSM */
3459
3460 struct key;
3461
3462 #ifdef CONFIG_KEYS
3463 struct bpf_key {
3464 struct key *key;
3465 bool has_ref;
3466 };
3467 #endif /* CONFIG_KEYS */
3468
type_is_alloc(u32 type)3469 static inline bool type_is_alloc(u32 type)
3470 {
3471 return type & MEM_ALLOC;
3472 }
3473
bpf_memcg_flags(gfp_t flags)3474 static inline gfp_t bpf_memcg_flags(gfp_t flags)
3475 {
3476 if (memcg_bpf_enabled())
3477 return flags | __GFP_ACCOUNT;
3478 return flags;
3479 }
3480
bpf_is_subprog(const struct bpf_prog * prog)3481 static inline bool bpf_is_subprog(const struct bpf_prog *prog)
3482 {
3483 return prog->aux->func_idx != 0;
3484 }
3485
3486 #endif /* _LINUX_BPF_H */
3487