1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_KASAN_H 3 #define _LINUX_KASAN_H 4 5 #include <linux/types.h> 6 7 struct kmem_cache; 8 struct page; 9 struct vm_struct; 10 struct task_struct; 11 12 #ifdef CONFIG_KASAN 13 14 #define KASAN_SHADOW_SCALE_SHIFT 3 15 16 #include <asm/kasan.h> 17 #include <asm/pgtable.h> 18 19 extern unsigned char kasan_zero_page[PAGE_SIZE]; 20 extern pte_t kasan_zero_pte[PTRS_PER_PTE]; 21 extern pmd_t kasan_zero_pmd[PTRS_PER_PMD]; 22 extern pud_t kasan_zero_pud[PTRS_PER_PUD]; 23 extern p4d_t kasan_zero_p4d[PTRS_PER_P4D]; 24 25 void kasan_populate_zero_shadow(const void *shadow_start, 26 const void *shadow_end); 27 28 static inline void *kasan_mem_to_shadow(const void *addr) 29 { 30 return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) 31 + KASAN_SHADOW_OFFSET; 32 } 33 34 /* Enable reporting bugs after kasan_disable_current() */ 35 extern void kasan_enable_current(void); 36 37 /* Disable reporting bugs for current task */ 38 extern void kasan_disable_current(void); 39 40 void kasan_unpoison_shadow(const void *address, size_t size); 41 42 void kasan_unpoison_task_stack(struct task_struct *task); 43 void kasan_unpoison_stack_above_sp_to(const void *watermark); 44 45 void kasan_alloc_pages(struct page *page, unsigned int order); 46 void kasan_free_pages(struct page *page, unsigned int order); 47 48 void kasan_cache_create(struct kmem_cache *cache, size_t *size, 49 slab_flags_t *flags); 50 void kasan_cache_shrink(struct kmem_cache *cache); 51 void kasan_cache_shutdown(struct kmem_cache *cache); 52 53 void kasan_poison_slab(struct page *page); 54 void kasan_unpoison_object_data(struct kmem_cache *cache, void *object); 55 void kasan_poison_object_data(struct kmem_cache *cache, void *object); 56 void kasan_init_slab_obj(struct kmem_cache *cache, const void *object); 57 58 void kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags); 59 void kasan_kfree_large(const void *ptr); 60 void kasan_poison_kfree(void *ptr); 61 void kasan_kmalloc(struct kmem_cache *s, const void *object, size_t size, 62 gfp_t flags); 63 void kasan_krealloc(const void *object, size_t new_size, gfp_t flags); 64 65 void kasan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags); 66 bool kasan_slab_free(struct kmem_cache *s, void *object); 67 68 struct kasan_cache { 69 int alloc_meta_offset; 70 int free_meta_offset; 71 }; 72 73 int kasan_module_alloc(void *addr, size_t size); 74 void kasan_free_shadow(const struct vm_struct *vm); 75 76 size_t ksize(const void *); 77 static inline void kasan_unpoison_slab(const void *ptr) { ksize(ptr); } 78 size_t kasan_metadata_size(struct kmem_cache *cache); 79 80 bool kasan_save_enable_multi_shot(void); 81 void kasan_restore_multi_shot(bool enabled); 82 83 #else /* CONFIG_KASAN */ 84 85 static inline void kasan_unpoison_shadow(const void *address, size_t size) {} 86 87 static inline void kasan_unpoison_task_stack(struct task_struct *task) {} 88 static inline void kasan_unpoison_stack_above_sp_to(const void *watermark) {} 89 90 static inline void kasan_enable_current(void) {} 91 static inline void kasan_disable_current(void) {} 92 93 static inline void kasan_alloc_pages(struct page *page, unsigned int order) {} 94 static inline void kasan_free_pages(struct page *page, unsigned int order) {} 95 96 static inline void kasan_cache_create(struct kmem_cache *cache, 97 size_t *size, 98 slab_flags_t *flags) {} 99 static inline void kasan_cache_shrink(struct kmem_cache *cache) {} 100 static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} 101 102 static inline void kasan_poison_slab(struct page *page) {} 103 static inline void kasan_unpoison_object_data(struct kmem_cache *cache, 104 void *object) {} 105 static inline void kasan_poison_object_data(struct kmem_cache *cache, 106 void *object) {} 107 static inline void kasan_init_slab_obj(struct kmem_cache *cache, 108 const void *object) {} 109 110 static inline void kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags) {} 111 static inline void kasan_kfree_large(const void *ptr) {} 112 static inline void kasan_poison_kfree(void *ptr) {} 113 static inline void kasan_kmalloc(struct kmem_cache *s, const void *object, 114 size_t size, gfp_t flags) {} 115 static inline void kasan_krealloc(const void *object, size_t new_size, 116 gfp_t flags) {} 117 118 static inline void kasan_slab_alloc(struct kmem_cache *s, void *object, 119 gfp_t flags) {} 120 static inline bool kasan_slab_free(struct kmem_cache *s, void *object) 121 { 122 return false; 123 } 124 125 static inline int kasan_module_alloc(void *addr, size_t size) { return 0; } 126 static inline void kasan_free_shadow(const struct vm_struct *vm) {} 127 128 static inline void kasan_unpoison_slab(const void *ptr) { } 129 static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; } 130 131 #endif /* CONFIG_KASAN */ 132 133 #endif /* LINUX_KASAN_H */ 134