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