| 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _LINUX_KASAN_H |
| 3 | #define _LINUX_KASAN_H |
| 4 | |
| 5 | #include <linux/bug.h> |
| 6 | #include <linux/kasan-enabled.h> |
| 7 | #include <linux/kasan-tags.h> |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/static_key.h> |
| 10 | #include <linux/types.h> |
| 11 | |
| 12 | struct kmem_cache; |
| 13 | struct page; |
| 14 | struct slab; |
| 15 | struct vm_struct; |
| 16 | struct task_struct; |
| 17 | |
| 18 | #ifdef CONFIG_KASAN |
| 19 | |
| 20 | #include <linux/linkage.h> |
| 21 | #include <asm/kasan.h> |
| 22 | |
| 23 | #endif |
| 24 | |
| 25 | typedef unsigned int __bitwise kasan_vmalloc_flags_t; |
| 26 | |
| 27 | #define KASAN_VMALLOC_NONE ((__force kasan_vmalloc_flags_t)0x00u) |
| 28 | #define KASAN_VMALLOC_INIT ((__force kasan_vmalloc_flags_t)0x01u) |
| 29 | #define KASAN_VMALLOC_VM_ALLOC ((__force kasan_vmalloc_flags_t)0x02u) |
| 30 | #define KASAN_VMALLOC_PROT_NORMAL ((__force kasan_vmalloc_flags_t)0x04u) |
| 31 | #define KASAN_VMALLOC_KEEP_TAG ((__force kasan_vmalloc_flags_t)0x08u) |
| 32 | |
| 33 | #define KASAN_VMALLOC_PAGE_RANGE 0x1 /* Apply exsiting page range */ |
| 34 | #define KASAN_VMALLOC_TLB_FLUSH 0x2 /* TLB flush */ |
| 35 | |
| 36 | #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) |
| 37 | |
| 38 | #include <linux/pgtable.h> |
| 39 | |
| 40 | /* Software KASAN implementations use shadow memory. */ |
| 41 | |
| 42 | #ifdef CONFIG_KASAN_SW_TAGS |
| 43 | /* This matches KASAN_TAG_INVALID. */ |
| 44 | #define KASAN_SHADOW_INIT 0xFE |
| 45 | #else |
| 46 | #define KASAN_SHADOW_INIT 0 |
| 47 | #endif |
| 48 | |
| 49 | #ifndef PTE_HWTABLE_PTRS |
| 50 | #define PTE_HWTABLE_PTRS 0 |
| 51 | #endif |
| 52 | |
| 53 | extern unsigned char kasan_early_shadow_page[PAGE_SIZE]; |
| 54 | extern pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS]; |
| 55 | extern pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD]; |
| 56 | extern pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD]; |
| 57 | extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D]; |
| 58 | |
| 59 | int kasan_populate_early_shadow(const void *shadow_start, |
| 60 | const void *shadow_end); |
| 61 | |
| 62 | #ifndef kasan_mem_to_shadow |
| 63 | static inline void *kasan_mem_to_shadow(const void *addr) |
| 64 | { |
| 65 | return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) |
| 66 | + KASAN_SHADOW_OFFSET; |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | int kasan_add_zero_shadow(void *start, unsigned long size); |
| 71 | void kasan_remove_zero_shadow(void *start, unsigned long size); |
| 72 | |
| 73 | /* Enable reporting bugs after kasan_disable_current() */ |
| 74 | extern void kasan_enable_current(void); |
| 75 | |
| 76 | /* Disable reporting bugs for current task */ |
| 77 | extern void kasan_disable_current(void); |
| 78 | |
| 79 | #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ |
| 80 | |
| 81 | static inline int kasan_add_zero_shadow(void *start, unsigned long size) |
| 82 | { |
| 83 | return 0; |
| 84 | } |
| 85 | static inline void kasan_remove_zero_shadow(void *start, |
| 86 | unsigned long size) |
| 87 | {} |
| 88 | |
| 89 | static inline void kasan_enable_current(void) {} |
| 90 | static inline void kasan_disable_current(void) {} |
| 91 | |
| 92 | #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ |
| 93 | |
| 94 | #ifdef CONFIG_KASAN_HW_TAGS |
| 95 | |
| 96 | #else /* CONFIG_KASAN_HW_TAGS */ |
| 97 | |
| 98 | #endif /* CONFIG_KASAN_HW_TAGS */ |
| 99 | |
| 100 | static inline bool kasan_has_integrated_init(void) |
| 101 | { |
| 102 | return kasan_hw_tags_enabled(); |
| 103 | } |
| 104 | |
| 105 | #ifdef CONFIG_KASAN |
| 106 | void __kasan_unpoison_range(const void *addr, size_t size); |
| 107 | static __always_inline void kasan_unpoison_range(const void *addr, size_t size) |
| 108 | { |
| 109 | if (kasan_enabled()) |
| 110 | __kasan_unpoison_range(addr, size); |
| 111 | } |
| 112 | |
| 113 | void __kasan_poison_pages(struct page *page, unsigned int order, bool init); |
| 114 | static __always_inline void kasan_poison_pages(struct page *page, |
| 115 | unsigned int order, bool init) |
| 116 | { |
| 117 | if (kasan_enabled()) |
| 118 | __kasan_poison_pages(page, order, init); |
| 119 | } |
| 120 | |
| 121 | bool __kasan_unpoison_pages(struct page *page, unsigned int order, bool init); |
| 122 | static __always_inline bool kasan_unpoison_pages(struct page *page, |
| 123 | unsigned int order, bool init) |
| 124 | { |
| 125 | if (kasan_enabled()) |
| 126 | return __kasan_unpoison_pages(page, order, init); |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | void __kasan_poison_slab(struct slab *slab); |
| 131 | static __always_inline void kasan_poison_slab(struct slab *slab) |
| 132 | { |
| 133 | if (kasan_enabled()) |
| 134 | __kasan_poison_slab(slab); |
| 135 | } |
| 136 | |
| 137 | void __kasan_unpoison_new_object(struct kmem_cache *cache, void *object); |
| 138 | /** |
| 139 | * kasan_unpoison_new_object - Temporarily unpoison a new slab object. |
| 140 | * @cache: Cache the object belong to. |
| 141 | * @object: Pointer to the object. |
| 142 | * |
| 143 | * This function is intended for the slab allocator's internal use. It |
| 144 | * temporarily unpoisons an object from a newly allocated slab without doing |
| 145 | * anything else. The object must later be repoisoned by |
| 146 | * kasan_poison_new_object(). |
| 147 | */ |
| 148 | static __always_inline void kasan_unpoison_new_object(struct kmem_cache *cache, |
| 149 | void *object) |
| 150 | { |
| 151 | if (kasan_enabled()) |
| 152 | __kasan_unpoison_new_object(cache, object); |
| 153 | } |
| 154 | |
| 155 | void __kasan_poison_new_object(struct kmem_cache *cache, void *object); |
| 156 | /** |
| 157 | * kasan_poison_new_object - Repoison a new slab object. |
| 158 | * @cache: Cache the object belong to. |
| 159 | * @object: Pointer to the object. |
| 160 | * |
| 161 | * This function is intended for the slab allocator's internal use. It |
| 162 | * repoisons an object that was previously unpoisoned by |
| 163 | * kasan_unpoison_new_object() without doing anything else. |
| 164 | */ |
| 165 | static __always_inline void kasan_poison_new_object(struct kmem_cache *cache, |
| 166 | void *object) |
| 167 | { |
| 168 | if (kasan_enabled()) |
| 169 | __kasan_poison_new_object(cache, object); |
| 170 | } |
| 171 | |
| 172 | void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache, |
| 173 | const void *object); |
| 174 | static __always_inline void * __must_check kasan_init_slab_obj( |
| 175 | struct kmem_cache *cache, const void *object) |
| 176 | { |
| 177 | if (kasan_enabled()) |
| 178 | return __kasan_init_slab_obj(cache, object); |
| 179 | return (void *)object; |
| 180 | } |
| 181 | |
| 182 | bool __kasan_slab_pre_free(struct kmem_cache *s, void *object, |
| 183 | unsigned long ip); |
| 184 | /** |
| 185 | * kasan_slab_pre_free - Check whether freeing a slab object is safe. |
| 186 | * @object: Object to be freed. |
| 187 | * |
| 188 | * This function checks whether freeing the given object is safe. It may |
| 189 | * check for double-free and invalid-free bugs and report them. |
| 190 | * |
| 191 | * This function is intended only for use by the slab allocator. |
| 192 | * |
| 193 | * @Return true if freeing the object is unsafe; false otherwise. |
| 194 | */ |
| 195 | static __always_inline bool kasan_slab_pre_free(struct kmem_cache *s, |
| 196 | void *object) |
| 197 | { |
| 198 | if (kasan_enabled()) |
| 199 | return __kasan_slab_pre_free(s, object, _RET_IP_); |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | bool __kasan_slab_free(struct kmem_cache *s, void *object, bool init, |
| 204 | bool still_accessible, bool no_quarantine); |
| 205 | /** |
| 206 | * kasan_slab_free - Poison, initialize, and quarantine a slab object. |
| 207 | * @object: Object to be freed. |
| 208 | * @init: Whether to initialize the object. |
| 209 | * @still_accessible: Whether the object contents are still accessible. |
| 210 | * |
| 211 | * This function informs that a slab object has been freed and is not |
| 212 | * supposed to be accessed anymore, except when @still_accessible is set |
| 213 | * (indicating that the object is in a SLAB_TYPESAFE_BY_RCU cache and an RCU |
| 214 | * grace period might not have passed yet). |
| 215 | * |
| 216 | * For KASAN modes that have integrated memory initialization |
| 217 | * (kasan_has_integrated_init() == true), this function also initializes |
| 218 | * the object's memory. For other modes, the @init argument is ignored. |
| 219 | * |
| 220 | * This function might also take ownership of the object to quarantine it. |
| 221 | * When this happens, KASAN will defer freeing the object to a later |
| 222 | * stage and handle it internally until then. The return value indicates |
| 223 | * whether KASAN took ownership of the object. |
| 224 | * |
| 225 | * This function is intended only for use by the slab allocator. |
| 226 | * |
| 227 | * @Return true if KASAN took ownership of the object; false otherwise. |
| 228 | */ |
| 229 | static __always_inline bool kasan_slab_free(struct kmem_cache *s, |
| 230 | void *object, bool init, |
| 231 | bool still_accessible, |
| 232 | bool no_quarantine) |
| 233 | { |
| 234 | if (kasan_enabled()) |
| 235 | return __kasan_slab_free(s, object, init, still_accessible, |
| 236 | no_quarantine); |
| 237 | return false; |
| 238 | } |
| 239 | |
| 240 | void __kasan_kfree_large(void *ptr, unsigned long ip); |
| 241 | static __always_inline void kasan_kfree_large(void *ptr) |
| 242 | { |
| 243 | if (kasan_enabled()) |
| 244 | __kasan_kfree_large(ptr, _RET_IP_); |
| 245 | } |
| 246 | |
| 247 | void * __must_check __kasan_slab_alloc(struct kmem_cache *s, |
| 248 | void *object, gfp_t flags, bool init); |
| 249 | static __always_inline void * __must_check kasan_slab_alloc( |
| 250 | struct kmem_cache *s, void *object, gfp_t flags, bool init) |
| 251 | { |
| 252 | if (kasan_enabled()) |
| 253 | return __kasan_slab_alloc(s, object, flags, init); |
| 254 | return object; |
| 255 | } |
| 256 | |
| 257 | void * __must_check __kasan_kmalloc(struct kmem_cache *s, const void *object, |
| 258 | size_t size, gfp_t flags); |
| 259 | static __always_inline void * __must_check kasan_kmalloc(struct kmem_cache *s, |
| 260 | const void *object, size_t size, gfp_t flags) |
| 261 | { |
| 262 | if (kasan_enabled()) |
| 263 | return __kasan_kmalloc(s, object, size, flags); |
| 264 | return (void *)object; |
| 265 | } |
| 266 | |
| 267 | void * __must_check __kasan_kmalloc_large(const void *ptr, |
| 268 | size_t size, gfp_t flags); |
| 269 | static __always_inline void * __must_check kasan_kmalloc_large(const void *ptr, |
| 270 | size_t size, gfp_t flags) |
| 271 | { |
| 272 | if (kasan_enabled()) |
| 273 | return __kasan_kmalloc_large(ptr, size, flags); |
| 274 | return (void *)ptr; |
| 275 | } |
| 276 | |
| 277 | void * __must_check __kasan_krealloc(const void *object, |
| 278 | size_t new_size, gfp_t flags); |
| 279 | static __always_inline void * __must_check kasan_krealloc(const void *object, |
| 280 | size_t new_size, gfp_t flags) |
| 281 | { |
| 282 | if (kasan_enabled()) |
| 283 | return __kasan_krealloc(object, new_size, flags); |
| 284 | return (void *)object; |
| 285 | } |
| 286 | |
| 287 | bool __kasan_mempool_poison_pages(struct page *page, unsigned int order, |
| 288 | unsigned long ip); |
| 289 | /** |
| 290 | * kasan_mempool_poison_pages - Check and poison a mempool page allocation. |
| 291 | * @page: Pointer to the page allocation. |
| 292 | * @order: Order of the allocation. |
| 293 | * |
| 294 | * This function is intended for kernel subsystems that cache page allocations |
| 295 | * to reuse them instead of freeing them back to page_alloc (e.g. mempool). |
| 296 | * |
| 297 | * This function is similar to kasan_mempool_poison_object() but operates on |
| 298 | * page allocations. |
| 299 | * |
| 300 | * Before the poisoned allocation can be reused, it must be unpoisoned via |
| 301 | * kasan_mempool_unpoison_pages(). |
| 302 | * |
| 303 | * Return: true if the allocation can be safely reused; false otherwise. |
| 304 | */ |
| 305 | static __always_inline bool kasan_mempool_poison_pages(struct page *page, |
| 306 | unsigned int order) |
| 307 | { |
| 308 | if (kasan_enabled()) |
| 309 | return __kasan_mempool_poison_pages(page, order, _RET_IP_); |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | void __kasan_mempool_unpoison_pages(struct page *page, unsigned int order, |
| 314 | unsigned long ip); |
| 315 | /** |
| 316 | * kasan_mempool_unpoison_pages - Unpoison a mempool page allocation. |
| 317 | * @page: Pointer to the page allocation. |
| 318 | * @order: Order of the allocation. |
| 319 | * |
| 320 | * This function is intended for kernel subsystems that cache page allocations |
| 321 | * to reuse them instead of freeing them back to page_alloc (e.g. mempool). |
| 322 | * |
| 323 | * This function unpoisons a page allocation that was previously poisoned by |
| 324 | * kasan_mempool_poison_pages() without zeroing the allocation's memory. For |
| 325 | * the tag-based modes, this function assigns a new tag to the allocation. |
| 326 | */ |
| 327 | static __always_inline void kasan_mempool_unpoison_pages(struct page *page, |
| 328 | unsigned int order) |
| 329 | { |
| 330 | if (kasan_enabled()) |
| 331 | __kasan_mempool_unpoison_pages(page, order, _RET_IP_); |
| 332 | } |
| 333 | |
| 334 | bool __kasan_mempool_poison_object(void *ptr, unsigned long ip); |
| 335 | /** |
| 336 | * kasan_mempool_poison_object - Check and poison a mempool slab allocation. |
| 337 | * @ptr: Pointer to the slab allocation. |
| 338 | * |
| 339 | * This function is intended for kernel subsystems that cache slab allocations |
| 340 | * to reuse them instead of freeing them back to the slab allocator (e.g. |
| 341 | * mempool). |
| 342 | * |
| 343 | * This function poisons a slab allocation and saves a free stack trace for it |
| 344 | * without initializing the allocation's memory and without putting it into the |
| 345 | * quarantine (for the Generic mode). |
| 346 | * |
| 347 | * This function also performs checks to detect double-free and invalid-free |
| 348 | * bugs and reports them. The caller can use the return value of this function |
| 349 | * to find out if the allocation is buggy. |
| 350 | * |
| 351 | * Before the poisoned allocation can be reused, it must be unpoisoned via |
| 352 | * kasan_mempool_unpoison_object(). |
| 353 | * |
| 354 | * This function operates on all slab allocations including large kmalloc |
| 355 | * allocations (the ones returned by kmalloc_large() or by kmalloc() with the |
| 356 | * size > KMALLOC_MAX_SIZE). |
| 357 | * |
| 358 | * Return: true if the allocation can be safely reused; false otherwise. |
| 359 | */ |
| 360 | static __always_inline bool kasan_mempool_poison_object(void *ptr) |
| 361 | { |
| 362 | if (kasan_enabled()) |
| 363 | return __kasan_mempool_poison_object(ptr, _RET_IP_); |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip); |
| 368 | /** |
| 369 | * kasan_mempool_unpoison_object - Unpoison a mempool slab allocation. |
| 370 | * @ptr: Pointer to the slab allocation. |
| 371 | * @size: Size to be unpoisoned. |
| 372 | * |
| 373 | * This function is intended for kernel subsystems that cache slab allocations |
| 374 | * to reuse them instead of freeing them back to the slab allocator (e.g. |
| 375 | * mempool). |
| 376 | * |
| 377 | * This function unpoisons a slab allocation that was previously poisoned via |
| 378 | * kasan_mempool_poison_object() and saves an alloc stack trace for it without |
| 379 | * initializing the allocation's memory. For the tag-based modes, this function |
| 380 | * does not assign a new tag to the allocation and instead restores the |
| 381 | * original tags based on the pointer value. |
| 382 | * |
| 383 | * This function operates on all slab allocations including large kmalloc |
| 384 | * allocations (the ones returned by kmalloc_large() or by kmalloc() with the |
| 385 | * size > KMALLOC_MAX_SIZE). |
| 386 | */ |
| 387 | static __always_inline void kasan_mempool_unpoison_object(void *ptr, |
| 388 | size_t size) |
| 389 | { |
| 390 | if (kasan_enabled()) |
| 391 | __kasan_mempool_unpoison_object(ptr, size, _RET_IP_); |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Unlike kasan_check_read/write(), kasan_check_byte() is performed even for |
| 396 | * the hardware tag-based mode that doesn't rely on compiler instrumentation. |
| 397 | */ |
| 398 | bool __kasan_check_byte(const void *addr, unsigned long ip); |
| 399 | static __always_inline bool kasan_check_byte(const void *addr) |
| 400 | { |
| 401 | if (kasan_enabled()) |
| 402 | return __kasan_check_byte(addr, _RET_IP_); |
| 403 | return true; |
| 404 | } |
| 405 | |
| 406 | #else /* CONFIG_KASAN */ |
| 407 | |
| 408 | static inline void kasan_unpoison_range(const void *address, size_t size) {} |
| 409 | static inline void kasan_poison_pages(struct page *page, unsigned int order, |
| 410 | bool init) {} |
| 411 | static inline bool kasan_unpoison_pages(struct page *page, unsigned int order, |
| 412 | bool init) |
| 413 | { |
| 414 | return false; |
| 415 | } |
| 416 | static inline void kasan_poison_slab(struct slab *slab) {} |
| 417 | static inline void kasan_unpoison_new_object(struct kmem_cache *cache, |
| 418 | void *object) {} |
| 419 | static inline void kasan_poison_new_object(struct kmem_cache *cache, |
| 420 | void *object) {} |
| 421 | static inline void *kasan_init_slab_obj(struct kmem_cache *cache, |
| 422 | const void *object) |
| 423 | { |
| 424 | return (void *)object; |
| 425 | } |
| 426 | |
| 427 | static inline bool kasan_slab_pre_free(struct kmem_cache *s, void *object) |
| 428 | { |
| 429 | return false; |
| 430 | } |
| 431 | |
| 432 | static inline bool kasan_slab_free(struct kmem_cache *s, void *object, |
| 433 | bool init, bool still_accessible, |
| 434 | bool no_quarantine) |
| 435 | { |
| 436 | return false; |
| 437 | } |
| 438 | static inline void kasan_kfree_large(void *ptr) {} |
| 439 | static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object, |
| 440 | gfp_t flags, bool init) |
| 441 | { |
| 442 | return object; |
| 443 | } |
| 444 | static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object, |
| 445 | size_t size, gfp_t flags) |
| 446 | { |
| 447 | return (void *)object; |
| 448 | } |
| 449 | static inline void *kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags) |
| 450 | { |
| 451 | return (void *)ptr; |
| 452 | } |
| 453 | static inline void *kasan_krealloc(const void *object, size_t new_size, |
| 454 | gfp_t flags) |
| 455 | { |
| 456 | return (void *)object; |
| 457 | } |
| 458 | static inline bool kasan_mempool_poison_pages(struct page *page, unsigned int order) |
| 459 | { |
| 460 | return true; |
| 461 | } |
| 462 | static inline void kasan_mempool_unpoison_pages(struct page *page, unsigned int order) {} |
| 463 | static inline bool kasan_mempool_poison_object(void *ptr) |
| 464 | { |
| 465 | return true; |
| 466 | } |
| 467 | static inline void kasan_mempool_unpoison_object(void *ptr, size_t size) {} |
| 468 | |
| 469 | static inline bool kasan_check_byte(const void *address) |
| 470 | { |
| 471 | return true; |
| 472 | } |
| 473 | |
| 474 | #endif /* CONFIG_KASAN */ |
| 475 | |
| 476 | #if defined(CONFIG_KASAN) && defined(CONFIG_KASAN_STACK) |
| 477 | void kasan_unpoison_task_stack(struct task_struct *task); |
| 478 | asmlinkage void kasan_unpoison_task_stack_below(const void *watermark); |
| 479 | #else |
| 480 | static inline void kasan_unpoison_task_stack(struct task_struct *task) {} |
| 481 | static inline void kasan_unpoison_task_stack_below(const void *watermark) {} |
| 482 | #endif |
| 483 | |
| 484 | #ifdef CONFIG_KASAN_GENERIC |
| 485 | |
| 486 | struct kasan_cache { |
| 487 | int alloc_meta_offset; |
| 488 | int free_meta_offset; |
| 489 | }; |
| 490 | |
| 491 | size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object); |
| 492 | void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, |
| 493 | slab_flags_t *flags); |
| 494 | |
| 495 | void kasan_cache_shrink(struct kmem_cache *cache); |
| 496 | void kasan_cache_shutdown(struct kmem_cache *cache); |
| 497 | void kasan_record_aux_stack(void *ptr); |
| 498 | |
| 499 | #else /* CONFIG_KASAN_GENERIC */ |
| 500 | |
| 501 | /* Tag-based KASAN modes do not use per-object metadata. */ |
| 502 | static inline size_t kasan_metadata_size(struct kmem_cache *cache, |
| 503 | bool in_object) |
| 504 | { |
| 505 | return 0; |
| 506 | } |
| 507 | /* And no cache-related metadata initialization is required. */ |
| 508 | static inline void kasan_cache_create(struct kmem_cache *cache, |
| 509 | unsigned int *size, |
| 510 | slab_flags_t *flags) {} |
| 511 | |
| 512 | static inline void kasan_cache_shrink(struct kmem_cache *cache) {} |
| 513 | static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} |
| 514 | static inline void kasan_record_aux_stack(void *ptr) {} |
| 515 | |
| 516 | #endif /* CONFIG_KASAN_GENERIC */ |
| 517 | |
| 518 | #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS) |
| 519 | |
| 520 | static inline void *kasan_reset_tag(const void *addr) |
| 521 | { |
| 522 | return (void *)arch_kasan_reset_tag(addr); |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * kasan_report - print a report about a bad memory access detected by KASAN |
| 527 | * @addr: address of the bad access |
| 528 | * @size: size of the bad access |
| 529 | * @is_write: whether the bad access is a write or a read |
| 530 | * @ip: instruction pointer for the accessibility check or the bad access itself |
| 531 | */ |
| 532 | bool kasan_report(const void *addr, size_t size, |
| 533 | bool is_write, unsigned long ip); |
| 534 | |
| 535 | #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */ |
| 536 | |
| 537 | static inline void *kasan_reset_tag(const void *addr) |
| 538 | { |
| 539 | return (void *)addr; |
| 540 | } |
| 541 | |
| 542 | #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS*/ |
| 543 | |
| 544 | #ifdef CONFIG_KASAN_HW_TAGS |
| 545 | |
| 546 | void kasan_report_async(void); |
| 547 | |
| 548 | #endif /* CONFIG_KASAN_HW_TAGS */ |
| 549 | |
| 550 | #ifdef CONFIG_KASAN_GENERIC |
| 551 | void __init kasan_init_generic(void); |
| 552 | #else |
| 553 | static inline void kasan_init_generic(void) { } |
| 554 | #endif |
| 555 | |
| 556 | #ifdef CONFIG_KASAN_SW_TAGS |
| 557 | void __init kasan_init_sw_tags(void); |
| 558 | #else |
| 559 | static inline void kasan_init_sw_tags(void) { } |
| 560 | #endif |
| 561 | |
| 562 | #ifdef CONFIG_KASAN_HW_TAGS |
| 563 | void kasan_init_hw_tags_cpu(void); |
| 564 | void __init kasan_init_hw_tags(void); |
| 565 | #else |
| 566 | static inline void kasan_init_hw_tags_cpu(void) { } |
| 567 | static inline void kasan_init_hw_tags(void) { } |
| 568 | #endif |
| 569 | |
| 570 | #ifdef CONFIG_KASAN_VMALLOC |
| 571 | |
| 572 | #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) |
| 573 | |
| 574 | void kasan_populate_early_vm_area_shadow(void *start, unsigned long size); |
| 575 | int __kasan_populate_vmalloc(unsigned long addr, unsigned long size, gfp_t gfp_mask); |
| 576 | static inline int kasan_populate_vmalloc(unsigned long addr, |
| 577 | unsigned long size, gfp_t gfp_mask) |
| 578 | { |
| 579 | if (kasan_enabled()) |
| 580 | return __kasan_populate_vmalloc(addr, size, gfp_mask); |
| 581 | return 0; |
| 582 | } |
| 583 | void __kasan_release_vmalloc(unsigned long start, unsigned long end, |
| 584 | unsigned long free_region_start, |
| 585 | unsigned long free_region_end, |
| 586 | unsigned long flags); |
| 587 | static inline void kasan_release_vmalloc(unsigned long start, unsigned long end, |
| 588 | unsigned long free_region_start, |
| 589 | unsigned long free_region_end, |
| 590 | unsigned long flags) |
| 591 | { |
| 592 | if (kasan_enabled()) |
| 593 | return __kasan_release_vmalloc(start, end, free_region_start, |
| 594 | free_region_end, flags); |
| 595 | } |
| 596 | |
| 597 | #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ |
| 598 | |
| 599 | static inline void kasan_populate_early_vm_area_shadow(void *start, |
| 600 | unsigned long size) |
| 601 | { } |
| 602 | static inline int kasan_populate_vmalloc(unsigned long start, |
| 603 | unsigned long size, gfp_t gfp_mask) |
| 604 | { |
| 605 | return 0; |
| 606 | } |
| 607 | static inline void kasan_release_vmalloc(unsigned long start, |
| 608 | unsigned long end, |
| 609 | unsigned long free_region_start, |
| 610 | unsigned long free_region_end, |
| 611 | unsigned long flags) { } |
| 612 | |
| 613 | #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ |
| 614 | |
| 615 | void *__kasan_unpoison_vmalloc(const void *start, unsigned long size, |
| 616 | kasan_vmalloc_flags_t flags); |
| 617 | static __always_inline void *kasan_unpoison_vmalloc(const void *start, |
| 618 | unsigned long size, |
| 619 | kasan_vmalloc_flags_t flags) |
| 620 | { |
| 621 | if (kasan_enabled()) |
| 622 | return __kasan_unpoison_vmalloc(start, size, flags); |
| 623 | return (void *)start; |
| 624 | } |
| 625 | |
| 626 | void __kasan_poison_vmalloc(const void *start, unsigned long size); |
| 627 | static __always_inline void kasan_poison_vmalloc(const void *start, |
| 628 | unsigned long size) |
| 629 | { |
| 630 | if (kasan_enabled()) |
| 631 | __kasan_poison_vmalloc(start, size); |
| 632 | } |
| 633 | |
| 634 | void __kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms, |
| 635 | kasan_vmalloc_flags_t flags); |
| 636 | static __always_inline void |
| 637 | kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms, |
| 638 | kasan_vmalloc_flags_t flags) |
| 639 | { |
| 640 | if (kasan_enabled()) |
| 641 | __kasan_unpoison_vmap_areas(vms, nr_vms, flags); |
| 642 | } |
| 643 | |
| 644 | void __kasan_vrealloc(const void *start, unsigned long old_size, |
| 645 | unsigned long new_size); |
| 646 | |
| 647 | static __always_inline void kasan_vrealloc(const void *start, |
| 648 | unsigned long old_size, |
| 649 | unsigned long new_size) |
| 650 | { |
| 651 | if (kasan_enabled()) |
| 652 | __kasan_vrealloc(start, old_size, new_size); |
| 653 | } |
| 654 | |
| 655 | #else /* CONFIG_KASAN_VMALLOC */ |
| 656 | |
| 657 | static inline void kasan_populate_early_vm_area_shadow(void *start, |
| 658 | unsigned long size) { } |
| 659 | static inline int kasan_populate_vmalloc(unsigned long start, |
| 660 | unsigned long size, gfp_t gfp_mask) |
| 661 | { |
| 662 | return 0; |
| 663 | } |
| 664 | static inline void kasan_release_vmalloc(unsigned long start, |
| 665 | unsigned long end, |
| 666 | unsigned long free_region_start, |
| 667 | unsigned long free_region_end, |
| 668 | unsigned long flags) { } |
| 669 | |
| 670 | static inline void *kasan_unpoison_vmalloc(const void *start, |
| 671 | unsigned long size, |
| 672 | kasan_vmalloc_flags_t flags) |
| 673 | { |
| 674 | return (void *)start; |
| 675 | } |
| 676 | static inline void kasan_poison_vmalloc(const void *start, unsigned long size) |
| 677 | { } |
| 678 | |
| 679 | static __always_inline void |
| 680 | kasan_unpoison_vmap_areas(struct vm_struct **vms, int nr_vms, |
| 681 | kasan_vmalloc_flags_t flags) |
| 682 | { } |
| 683 | |
| 684 | static inline void kasan_vrealloc(const void *start, unsigned long old_size, |
| 685 | unsigned long new_size) { } |
| 686 | |
| 687 | #endif /* CONFIG_KASAN_VMALLOC */ |
| 688 | |
| 689 | #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \ |
| 690 | !defined(CONFIG_KASAN_VMALLOC) |
| 691 | |
| 692 | /* |
| 693 | * These functions allocate and free shadow memory for kernel modules. |
| 694 | * They are only required when KASAN_VMALLOC is not supported, as otherwise |
| 695 | * shadow memory is allocated by the generic vmalloc handlers. |
| 696 | */ |
| 697 | int kasan_alloc_module_shadow(void *addr, size_t size, gfp_t gfp_mask); |
| 698 | void kasan_free_module_shadow(const struct vm_struct *vm); |
| 699 | |
| 700 | #else /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */ |
| 701 | |
| 702 | static inline int kasan_alloc_module_shadow(void *addr, size_t size, gfp_t gfp_mask) { return 0; } |
| 703 | static inline void kasan_free_module_shadow(const struct vm_struct *vm) {} |
| 704 | |
| 705 | #endif /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */ |
| 706 | |
| 707 | #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) |
| 708 | void kasan_non_canonical_hook(unsigned long addr); |
| 709 | #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ |
| 710 | static inline void kasan_non_canonical_hook(unsigned long addr) { } |
| 711 | #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ |
| 712 | |
| 713 | #endif /* LINUX_KASAN_H */ |
| 714 | |