1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_KASAN_H 3 #define _LINUX_KASAN_H 4 5 #include <linux/static_key.h> 6 #include <linux/types.h> 7 8 struct kmem_cache; 9 struct page; 10 struct vm_struct; 11 struct task_struct; 12 13 #ifdef CONFIG_KASAN 14 15 #include <linux/linkage.h> 16 #include <asm/kasan.h> 17 18 /* kasan_data struct is used in KUnit tests for KASAN expected failures */ 19 struct kunit_kasan_expectation { 20 bool report_found; 21 }; 22 23 #endif 24 25 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) 26 27 #include <linux/pgtable.h> 28 29 /* Software KASAN implementations use shadow memory. */ 30 31 #ifdef CONFIG_KASAN_SW_TAGS 32 /* This matches KASAN_TAG_INVALID. */ 33 #define KASAN_SHADOW_INIT 0xFE 34 #else 35 #define KASAN_SHADOW_INIT 0 36 #endif 37 38 #ifndef PTE_HWTABLE_PTRS 39 #define PTE_HWTABLE_PTRS 0 40 #endif 41 42 extern unsigned char kasan_early_shadow_page[PAGE_SIZE]; 43 extern pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS]; 44 extern pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD]; 45 extern pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD]; 46 extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D]; 47 48 int kasan_populate_early_shadow(const void *shadow_start, 49 const void *shadow_end); 50 51 static inline void *kasan_mem_to_shadow(const void *addr) 52 { 53 return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) 54 + KASAN_SHADOW_OFFSET; 55 } 56 57 int kasan_add_zero_shadow(void *start, unsigned long size); 58 void kasan_remove_zero_shadow(void *start, unsigned long size); 59 60 /* Enable reporting bugs after kasan_disable_current() */ 61 extern void kasan_enable_current(void); 62 63 /* Disable reporting bugs for current task */ 64 extern void kasan_disable_current(void); 65 66 #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ 67 68 static inline int kasan_add_zero_shadow(void *start, unsigned long size) 69 { 70 return 0; 71 } 72 static inline void kasan_remove_zero_shadow(void *start, 73 unsigned long size) 74 {} 75 76 static inline void kasan_enable_current(void) {} 77 static inline void kasan_disable_current(void) {} 78 79 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */ 80 81 #ifdef CONFIG_KASAN 82 83 struct kasan_cache { 84 int alloc_meta_offset; 85 int free_meta_offset; 86 bool is_kmalloc; 87 }; 88 89 #ifdef CONFIG_KASAN_HW_TAGS 90 91 DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled); 92 93 static __always_inline bool kasan_enabled(void) 94 { 95 return static_branch_likely(&kasan_flag_enabled); 96 } 97 98 static inline bool kasan_has_integrated_init(void) 99 { 100 return kasan_enabled(); 101 } 102 103 #else /* CONFIG_KASAN_HW_TAGS */ 104 105 static inline bool kasan_enabled(void) 106 { 107 return true; 108 } 109 110 static inline bool kasan_has_integrated_init(void) 111 { 112 return false; 113 } 114 115 #endif /* CONFIG_KASAN_HW_TAGS */ 116 117 slab_flags_t __kasan_never_merge(void); 118 static __always_inline slab_flags_t kasan_never_merge(void) 119 { 120 if (kasan_enabled()) 121 return __kasan_never_merge(); 122 return 0; 123 } 124 125 void __kasan_unpoison_range(const void *addr, size_t size); 126 static __always_inline void kasan_unpoison_range(const void *addr, size_t size) 127 { 128 if (kasan_enabled()) 129 __kasan_unpoison_range(addr, size); 130 } 131 132 void __kasan_alloc_pages(struct page *page, unsigned int order, bool init); 133 static __always_inline void kasan_alloc_pages(struct page *page, 134 unsigned int order, bool init) 135 { 136 if (kasan_enabled()) 137 __kasan_alloc_pages(page, order, init); 138 } 139 140 void __kasan_free_pages(struct page *page, unsigned int order, bool init); 141 static __always_inline void kasan_free_pages(struct page *page, 142 unsigned int order, bool init) 143 { 144 if (kasan_enabled()) 145 __kasan_free_pages(page, order, init); 146 } 147 148 void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size, 149 slab_flags_t *flags); 150 static __always_inline void kasan_cache_create(struct kmem_cache *cache, 151 unsigned int *size, slab_flags_t *flags) 152 { 153 if (kasan_enabled()) 154 __kasan_cache_create(cache, size, flags); 155 } 156 157 void __kasan_cache_create_kmalloc(struct kmem_cache *cache); 158 static __always_inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) 159 { 160 if (kasan_enabled()) 161 __kasan_cache_create_kmalloc(cache); 162 } 163 164 size_t __kasan_metadata_size(struct kmem_cache *cache); 165 static __always_inline size_t kasan_metadata_size(struct kmem_cache *cache) 166 { 167 if (kasan_enabled()) 168 return __kasan_metadata_size(cache); 169 return 0; 170 } 171 172 void __kasan_poison_slab(struct page *page); 173 static __always_inline void kasan_poison_slab(struct page *page) 174 { 175 if (kasan_enabled()) 176 __kasan_poison_slab(page); 177 } 178 179 void __kasan_unpoison_object_data(struct kmem_cache *cache, void *object); 180 static __always_inline void kasan_unpoison_object_data(struct kmem_cache *cache, 181 void *object) 182 { 183 if (kasan_enabled()) 184 __kasan_unpoison_object_data(cache, object); 185 } 186 187 void __kasan_poison_object_data(struct kmem_cache *cache, void *object); 188 static __always_inline void kasan_poison_object_data(struct kmem_cache *cache, 189 void *object) 190 { 191 if (kasan_enabled()) 192 __kasan_poison_object_data(cache, object); 193 } 194 195 void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache, 196 const void *object); 197 static __always_inline void * __must_check kasan_init_slab_obj( 198 struct kmem_cache *cache, const void *object) 199 { 200 if (kasan_enabled()) 201 return __kasan_init_slab_obj(cache, object); 202 return (void *)object; 203 } 204 205 bool __kasan_slab_free(struct kmem_cache *s, void *object, 206 unsigned long ip, bool init); 207 static __always_inline bool kasan_slab_free(struct kmem_cache *s, 208 void *object, bool init) 209 { 210 if (kasan_enabled()) 211 return __kasan_slab_free(s, object, _RET_IP_, init); 212 return false; 213 } 214 215 void __kasan_kfree_large(void *ptr, unsigned long ip); 216 static __always_inline void kasan_kfree_large(void *ptr) 217 { 218 if (kasan_enabled()) 219 __kasan_kfree_large(ptr, _RET_IP_); 220 } 221 222 void __kasan_slab_free_mempool(void *ptr, unsigned long ip); 223 static __always_inline void kasan_slab_free_mempool(void *ptr) 224 { 225 if (kasan_enabled()) 226 __kasan_slab_free_mempool(ptr, _RET_IP_); 227 } 228 229 void * __must_check __kasan_slab_alloc(struct kmem_cache *s, 230 void *object, gfp_t flags, bool init); 231 static __always_inline void * __must_check kasan_slab_alloc( 232 struct kmem_cache *s, void *object, gfp_t flags, bool init) 233 { 234 if (kasan_enabled()) 235 return __kasan_slab_alloc(s, object, flags, init); 236 return object; 237 } 238 239 void * __must_check __kasan_kmalloc(struct kmem_cache *s, const void *object, 240 size_t size, gfp_t flags); 241 static __always_inline void * __must_check kasan_kmalloc(struct kmem_cache *s, 242 const void *object, size_t size, gfp_t flags) 243 { 244 if (kasan_enabled()) 245 return __kasan_kmalloc(s, object, size, flags); 246 return (void *)object; 247 } 248 249 void * __must_check __kasan_kmalloc_large(const void *ptr, 250 size_t size, gfp_t flags); 251 static __always_inline void * __must_check kasan_kmalloc_large(const void *ptr, 252 size_t size, gfp_t flags) 253 { 254 if (kasan_enabled()) 255 return __kasan_kmalloc_large(ptr, size, flags); 256 return (void *)ptr; 257 } 258 259 void * __must_check __kasan_krealloc(const void *object, 260 size_t new_size, gfp_t flags); 261 static __always_inline void * __must_check kasan_krealloc(const void *object, 262 size_t new_size, gfp_t flags) 263 { 264 if (kasan_enabled()) 265 return __kasan_krealloc(object, new_size, flags); 266 return (void *)object; 267 } 268 269 /* 270 * Unlike kasan_check_read/write(), kasan_check_byte() is performed even for 271 * the hardware tag-based mode that doesn't rely on compiler instrumentation. 272 */ 273 bool __kasan_check_byte(const void *addr, unsigned long ip); 274 static __always_inline bool kasan_check_byte(const void *addr) 275 { 276 if (kasan_enabled()) 277 return __kasan_check_byte(addr, _RET_IP_); 278 return true; 279 } 280 281 282 bool kasan_save_enable_multi_shot(void); 283 void kasan_restore_multi_shot(bool enabled); 284 285 #else /* CONFIG_KASAN */ 286 287 static inline bool kasan_enabled(void) 288 { 289 return false; 290 } 291 static inline bool kasan_has_integrated_init(void) 292 { 293 return false; 294 } 295 static inline slab_flags_t kasan_never_merge(void) 296 { 297 return 0; 298 } 299 static inline void kasan_unpoison_range(const void *address, size_t size) {} 300 static inline void kasan_alloc_pages(struct page *page, unsigned int order, bool init) {} 301 static inline void kasan_free_pages(struct page *page, unsigned int order, bool init) {} 302 static inline void kasan_cache_create(struct kmem_cache *cache, 303 unsigned int *size, 304 slab_flags_t *flags) {} 305 static inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) {} 306 static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; } 307 static inline void kasan_poison_slab(struct page *page) {} 308 static inline void kasan_unpoison_object_data(struct kmem_cache *cache, 309 void *object) {} 310 static inline void kasan_poison_object_data(struct kmem_cache *cache, 311 void *object) {} 312 static inline void *kasan_init_slab_obj(struct kmem_cache *cache, 313 const void *object) 314 { 315 return (void *)object; 316 } 317 static inline bool kasan_slab_free(struct kmem_cache *s, void *object, bool init) 318 { 319 return false; 320 } 321 static inline void kasan_kfree_large(void *ptr) {} 322 static inline void kasan_slab_free_mempool(void *ptr) {} 323 static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object, 324 gfp_t flags, bool init) 325 { 326 return object; 327 } 328 static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object, 329 size_t size, gfp_t flags) 330 { 331 return (void *)object; 332 } 333 static inline void *kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags) 334 { 335 return (void *)ptr; 336 } 337 static inline void *kasan_krealloc(const void *object, size_t new_size, 338 gfp_t flags) 339 { 340 return (void *)object; 341 } 342 static inline bool kasan_check_byte(const void *address) 343 { 344 return true; 345 } 346 347 #endif /* CONFIG_KASAN */ 348 349 #if defined(CONFIG_KASAN) && defined(CONFIG_KASAN_STACK) 350 void kasan_unpoison_task_stack(struct task_struct *task); 351 #else 352 static inline void kasan_unpoison_task_stack(struct task_struct *task) {} 353 #endif 354 355 #ifdef CONFIG_KASAN_GENERIC 356 357 void kasan_cache_shrink(struct kmem_cache *cache); 358 void kasan_cache_shutdown(struct kmem_cache *cache); 359 void kasan_record_aux_stack(void *ptr); 360 361 #else /* CONFIG_KASAN_GENERIC */ 362 363 static inline void kasan_cache_shrink(struct kmem_cache *cache) {} 364 static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} 365 static inline void kasan_record_aux_stack(void *ptr) {} 366 367 #endif /* CONFIG_KASAN_GENERIC */ 368 369 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS) 370 371 static inline void *kasan_reset_tag(const void *addr) 372 { 373 return (void *)arch_kasan_reset_tag(addr); 374 } 375 376 /** 377 * kasan_report - print a report about a bad memory access detected by KASAN 378 * @addr: address of the bad access 379 * @size: size of the bad access 380 * @is_write: whether the bad access is a write or a read 381 * @ip: instruction pointer for the accessibility check or the bad access itself 382 */ 383 bool kasan_report(unsigned long addr, size_t size, 384 bool is_write, unsigned long ip); 385 386 #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */ 387 388 static inline void *kasan_reset_tag(const void *addr) 389 { 390 return (void *)addr; 391 } 392 393 #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS*/ 394 395 #ifdef CONFIG_KASAN_HW_TAGS 396 397 void kasan_report_async(void); 398 399 #endif /* CONFIG_KASAN_HW_TAGS */ 400 401 #ifdef CONFIG_KASAN_SW_TAGS 402 void __init kasan_init_sw_tags(void); 403 #else 404 static inline void kasan_init_sw_tags(void) { } 405 #endif 406 407 #ifdef CONFIG_KASAN_HW_TAGS 408 void kasan_init_hw_tags_cpu(void); 409 void __init kasan_init_hw_tags(void); 410 #else 411 static inline void kasan_init_hw_tags_cpu(void) { } 412 static inline void kasan_init_hw_tags(void) { } 413 #endif 414 415 #ifdef CONFIG_KASAN_VMALLOC 416 417 int kasan_populate_vmalloc(unsigned long addr, unsigned long size); 418 void kasan_poison_vmalloc(const void *start, unsigned long size); 419 void kasan_unpoison_vmalloc(const void *start, unsigned long size); 420 void kasan_release_vmalloc(unsigned long start, unsigned long end, 421 unsigned long free_region_start, 422 unsigned long free_region_end); 423 424 #else /* CONFIG_KASAN_VMALLOC */ 425 426 static inline int kasan_populate_vmalloc(unsigned long start, 427 unsigned long size) 428 { 429 return 0; 430 } 431 432 static inline void kasan_poison_vmalloc(const void *start, unsigned long size) 433 { } 434 static inline void kasan_unpoison_vmalloc(const void *start, unsigned long size) 435 { } 436 static inline void kasan_release_vmalloc(unsigned long start, 437 unsigned long end, 438 unsigned long free_region_start, 439 unsigned long free_region_end) {} 440 441 #endif /* CONFIG_KASAN_VMALLOC */ 442 443 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \ 444 !defined(CONFIG_KASAN_VMALLOC) 445 446 /* 447 * These functions provide a special case to support backing module 448 * allocations with real shadow memory. With KASAN vmalloc, the special 449 * case is unnecessary, as the work is handled in the generic case. 450 */ 451 int kasan_module_alloc(void *addr, size_t size); 452 void kasan_free_shadow(const struct vm_struct *vm); 453 454 #else /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */ 455 456 static inline int kasan_module_alloc(void *addr, size_t size) { return 0; } 457 static inline void kasan_free_shadow(const struct vm_struct *vm) {} 458 459 #endif /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */ 460 461 #ifdef CONFIG_KASAN_INLINE 462 void kasan_non_canonical_hook(unsigned long addr); 463 #else /* CONFIG_KASAN_INLINE */ 464 static inline void kasan_non_canonical_hook(unsigned long addr) { } 465 #endif /* CONFIG_KASAN_INLINE */ 466 467 #endif /* LINUX_KASAN_H */ 468