1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __MM_KASAN_KASAN_H 3 #define __MM_KASAN_KASAN_H 4 5 #include <linux/kasan.h> 6 #include <linux/stackdepot.h> 7 8 #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT) 9 #define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1) 10 11 #define KASAN_FREE_PAGE 0xFF /* page was freed */ 12 #define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */ 13 #define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */ 14 #define KASAN_KMALLOC_FREE 0xFB /* object was freed (kmem_cache_free/kfree) */ 15 #define KASAN_GLOBAL_REDZONE 0xFA /* redzone for global variable */ 16 17 /* 18 * Stack redzone shadow values 19 * (Those are compiler's ABI, don't change them) 20 */ 21 #define KASAN_STACK_LEFT 0xF1 22 #define KASAN_STACK_MID 0xF2 23 #define KASAN_STACK_RIGHT 0xF3 24 #define KASAN_STACK_PARTIAL 0xF4 25 #define KASAN_USE_AFTER_SCOPE 0xF8 26 27 /* 28 * alloca redzone shadow values 29 */ 30 #define KASAN_ALLOCA_LEFT 0xCA 31 #define KASAN_ALLOCA_RIGHT 0xCB 32 33 #define KASAN_ALLOCA_REDZONE_SIZE 32 34 35 /* Don't break randconfig/all*config builds */ 36 #ifndef KASAN_ABI_VERSION 37 #define KASAN_ABI_VERSION 1 38 #endif 39 40 struct kasan_access_info { 41 const void *access_addr; 42 const void *first_bad_addr; 43 size_t access_size; 44 bool is_write; 45 unsigned long ip; 46 }; 47 48 /* The layout of struct dictated by compiler */ 49 struct kasan_source_location { 50 const char *filename; 51 int line_no; 52 int column_no; 53 }; 54 55 /* The layout of struct dictated by compiler */ 56 struct kasan_global { 57 const void *beg; /* Address of the beginning of the global variable. */ 58 size_t size; /* Size of the global variable. */ 59 size_t size_with_redzone; /* Size of the variable + size of the red zone. 32 bytes aligned */ 60 const void *name; 61 const void *module_name; /* Name of the module where the global variable is declared. */ 62 unsigned long has_dynamic_init; /* This needed for C++ */ 63 #if KASAN_ABI_VERSION >= 4 64 struct kasan_source_location *location; 65 #endif 66 #if KASAN_ABI_VERSION >= 5 67 char *odr_indicator; 68 #endif 69 }; 70 71 /** 72 * Structures to keep alloc and free tracks * 73 */ 74 75 #define KASAN_STACK_DEPTH 64 76 77 struct kasan_track { 78 u32 pid; 79 depot_stack_handle_t stack; 80 }; 81 82 struct kasan_alloc_meta { 83 struct kasan_track alloc_track; 84 struct kasan_track free_track; 85 }; 86 87 struct qlist_node { 88 struct qlist_node *next; 89 }; 90 struct kasan_free_meta { 91 /* This field is used while the object is in the quarantine. 92 * Otherwise it might be used for the allocator freelist. 93 */ 94 struct qlist_node quarantine_link; 95 }; 96 97 struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache, 98 const void *object); 99 struct kasan_free_meta *get_free_info(struct kmem_cache *cache, 100 const void *object); 101 102 static inline const void *kasan_shadow_to_mem(const void *shadow_addr) 103 { 104 return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET) 105 << KASAN_SHADOW_SCALE_SHIFT); 106 } 107 108 void kasan_report(unsigned long addr, size_t size, 109 bool is_write, unsigned long ip); 110 void kasan_report_invalid_free(void *object, unsigned long ip); 111 112 #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB) 113 void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache); 114 void quarantine_reduce(void); 115 void quarantine_remove_cache(struct kmem_cache *cache); 116 #else 117 static inline void quarantine_put(struct kasan_free_meta *info, 118 struct kmem_cache *cache) { } 119 static inline void quarantine_reduce(void) { } 120 static inline void quarantine_remove_cache(struct kmem_cache *cache) { } 121 #endif 122 123 /* 124 * Exported functions for interfaces called from assembly or from generated 125 * code. Declarations here to avoid warning about missing declarations. 126 */ 127 asmlinkage void kasan_unpoison_task_stack_below(const void *watermark); 128 void __asan_register_globals(struct kasan_global *globals, size_t size); 129 void __asan_unregister_globals(struct kasan_global *globals, size_t size); 130 void __asan_loadN(unsigned long addr, size_t size); 131 void __asan_storeN(unsigned long addr, size_t size); 132 void __asan_handle_no_return(void); 133 void __asan_poison_stack_memory(const void *addr, size_t size); 134 void __asan_unpoison_stack_memory(const void *addr, size_t size); 135 void __asan_alloca_poison(unsigned long addr, size_t size); 136 void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom); 137 138 void __asan_load1(unsigned long addr); 139 void __asan_store1(unsigned long addr); 140 void __asan_load2(unsigned long addr); 141 void __asan_store2(unsigned long addr); 142 void __asan_load4(unsigned long addr); 143 void __asan_store4(unsigned long addr); 144 void __asan_load8(unsigned long addr); 145 void __asan_store8(unsigned long addr); 146 void __asan_load16(unsigned long addr); 147 void __asan_store16(unsigned long addr); 148 149 void __asan_load1_noabort(unsigned long addr); 150 void __asan_store1_noabort(unsigned long addr); 151 void __asan_load2_noabort(unsigned long addr); 152 void __asan_store2_noabort(unsigned long addr); 153 void __asan_load4_noabort(unsigned long addr); 154 void __asan_store4_noabort(unsigned long addr); 155 void __asan_load8_noabort(unsigned long addr); 156 void __asan_store8_noabort(unsigned long addr); 157 void __asan_load16_noabort(unsigned long addr); 158 void __asan_store16_noabort(unsigned long addr); 159 160 void __asan_set_shadow_00(const void *addr, size_t size); 161 void __asan_set_shadow_f1(const void *addr, size_t size); 162 void __asan_set_shadow_f2(const void *addr, size_t size); 163 void __asan_set_shadow_f3(const void *addr, size_t size); 164 void __asan_set_shadow_f5(const void *addr, size_t size); 165 void __asan_set_shadow_f8(const void *addr, size_t size); 166 167 #endif 168