Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 2 | #ifndef __MM_KASAN_KASAN_H |
| 3 | #define __MM_KASAN_KASAN_H |
| 4 | |
| 5 | #include <linux/kasan.h> |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 6 | #include <linux/stackdepot.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 7 | |
| 8 | #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT) |
| 9 | #define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1) |
| 10 | |
Andrey Konovalov | 3c9e3aa | 2018-12-28 00:30:16 -0800 | [diff] [blame] | 11 | #define KASAN_TAG_KERNEL 0xFF /* native kernel pointers tag */ |
| 12 | #define KASAN_TAG_INVALID 0xFE /* inaccessible memory tag */ |
| 13 | #define KASAN_TAG_MAX 0xFD /* maximum value for random tags */ |
| 14 | |
Andrey Konovalov | 7f94ffb | 2018-12-28 00:30:50 -0800 | [diff] [blame] | 15 | #ifdef CONFIG_KASAN_GENERIC |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 16 | #define KASAN_FREE_PAGE 0xFF /* page was freed */ |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 17 | #define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */ |
| 18 | #define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */ |
| 19 | #define KASAN_KMALLOC_FREE 0xFB /* object was freed (kmem_cache_free/kfree) */ |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 20 | #define KASAN_KMALLOC_FREETRACK 0xFA /* object was freed and has free track set */ |
Andrey Konovalov | 7f94ffb | 2018-12-28 00:30:50 -0800 | [diff] [blame] | 21 | #else |
| 22 | #define KASAN_FREE_PAGE KASAN_TAG_INVALID |
| 23 | #define KASAN_PAGE_REDZONE KASAN_TAG_INVALID |
| 24 | #define KASAN_KMALLOC_REDZONE KASAN_TAG_INVALID |
| 25 | #define KASAN_KMALLOC_FREE KASAN_TAG_INVALID |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 26 | #define KASAN_KMALLOC_FREETRACK KASAN_TAG_INVALID |
Andrey Konovalov | 7f94ffb | 2018-12-28 00:30:50 -0800 | [diff] [blame] | 27 | #endif |
| 28 | |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 29 | #define KASAN_GLOBAL_REDZONE 0xF9 /* redzone for global variable */ |
| 30 | #define KASAN_VMALLOC_INVALID 0xF8 /* unallocated space in vmapped page */ |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 31 | |
Andrey Ryabinin | c420f16 | 2015-02-13 14:39:59 -0800 | [diff] [blame] | 32 | /* |
| 33 | * Stack redzone shadow values |
| 34 | * (Those are compiler's ABI, don't change them) |
| 35 | */ |
| 36 | #define KASAN_STACK_LEFT 0xF1 |
| 37 | #define KASAN_STACK_MID 0xF2 |
| 38 | #define KASAN_STACK_RIGHT 0xF3 |
| 39 | #define KASAN_STACK_PARTIAL 0xF4 |
| 40 | |
Paul Lawrence | 342061e | 2018-02-06 15:36:11 -0800 | [diff] [blame] | 41 | /* |
| 42 | * alloca redzone shadow values |
| 43 | */ |
| 44 | #define KASAN_ALLOCA_LEFT 0xCA |
| 45 | #define KASAN_ALLOCA_RIGHT 0xCB |
| 46 | |
| 47 | #define KASAN_ALLOCA_REDZONE_SIZE 32 |
| 48 | |
Marco Elver | e896921 | 2019-07-11 20:53:49 -0700 | [diff] [blame] | 49 | /* |
| 50 | * Stack frame marker (compiler ABI). |
| 51 | */ |
| 52 | #define KASAN_CURRENT_STACK_FRAME_MAGIC 0x41B58AB3 |
| 53 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 54 | /* Don't break randconfig/all*config builds */ |
| 55 | #ifndef KASAN_ABI_VERSION |
| 56 | #define KASAN_ABI_VERSION 1 |
| 57 | #endif |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 58 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 59 | struct kasan_access_info { |
| 60 | const void *access_addr; |
| 61 | const void *first_bad_addr; |
| 62 | size_t access_size; |
| 63 | bool is_write; |
| 64 | unsigned long ip; |
| 65 | }; |
| 66 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 67 | /* The layout of struct dictated by compiler */ |
| 68 | struct kasan_source_location { |
| 69 | const char *filename; |
| 70 | int line_no; |
| 71 | int column_no; |
| 72 | }; |
| 73 | |
| 74 | /* The layout of struct dictated by compiler */ |
| 75 | struct kasan_global { |
| 76 | const void *beg; /* Address of the beginning of the global variable. */ |
| 77 | size_t size; /* Size of the global variable. */ |
| 78 | size_t size_with_redzone; /* Size of the variable + size of the red zone. 32 bytes aligned */ |
| 79 | const void *name; |
| 80 | const void *module_name; /* Name of the module where the global variable is declared. */ |
| 81 | unsigned long has_dynamic_init; /* This needed for C++ */ |
| 82 | #if KASAN_ABI_VERSION >= 4 |
| 83 | struct kasan_source_location *location; |
| 84 | #endif |
Dmitry Vyukov | 045d599 | 2016-11-30 15:54:13 -0800 | [diff] [blame] | 85 | #if KASAN_ABI_VERSION >= 5 |
| 86 | char *odr_indicator; |
| 87 | #endif |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Structures to keep alloc and free tracks * |
| 92 | */ |
| 93 | |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 94 | #define KASAN_STACK_DEPTH 64 |
| 95 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 96 | struct kasan_track { |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 97 | u32 pid; |
| 98 | depot_stack_handle_t stack; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Walter Wu | ae8f06b | 2019-09-23 15:34:13 -0700 | [diff] [blame] | 101 | #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY |
| 102 | #define KASAN_NR_FREE_STACKS 5 |
| 103 | #else |
| 104 | #define KASAN_NR_FREE_STACKS 1 |
| 105 | #endif |
| 106 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 107 | struct kasan_alloc_meta { |
Andrey Ryabinin | b3cbd9b | 2016-08-02 14:02:52 -0700 | [diff] [blame] | 108 | struct kasan_track alloc_track; |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 109 | #ifdef CONFIG_KASAN_GENERIC |
| 110 | /* |
| 111 | * call_rcu() call stack is stored into struct kasan_alloc_meta. |
| 112 | * The free stack is stored into struct kasan_free_meta. |
| 113 | */ |
| 114 | depot_stack_handle_t aux_stack[2]; |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 115 | #else |
Walter Wu | ae8f06b | 2019-09-23 15:34:13 -0700 | [diff] [blame] | 116 | struct kasan_track free_track[KASAN_NR_FREE_STACKS]; |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 117 | #endif |
Walter Wu | ae8f06b | 2019-09-23 15:34:13 -0700 | [diff] [blame] | 118 | #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY |
| 119 | u8 free_pointer_tag[KASAN_NR_FREE_STACKS]; |
| 120 | u8 free_track_idx; |
| 121 | #endif |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 124 | struct qlist_node { |
| 125 | struct qlist_node *next; |
| 126 | }; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 127 | struct kasan_free_meta { |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 128 | /* This field is used while the object is in the quarantine. |
| 129 | * Otherwise it might be used for the allocator freelist. |
| 130 | */ |
| 131 | struct qlist_node quarantine_link; |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 132 | #ifdef CONFIG_KASAN_GENERIC |
| 133 | struct kasan_track free_track; |
| 134 | #endif |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache, |
| 138 | const void *object); |
| 139 | struct kasan_free_meta *get_free_info(struct kmem_cache *cache, |
| 140 | const void *object); |
| 141 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 142 | static inline const void *kasan_shadow_to_mem(const void *shadow_addr) |
| 143 | { |
| 144 | return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET) |
| 145 | << KASAN_SHADOW_SCALE_SHIFT); |
| 146 | } |
| 147 | |
Andrey Konovalov | 11cd3cd | 2018-12-28 00:30:38 -0800 | [diff] [blame] | 148 | static inline bool addr_has_shadow(const void *addr) |
| 149 | { |
| 150 | return (addr >= kasan_shadow_to_mem((void *)KASAN_SHADOW_START)); |
| 151 | } |
| 152 | |
Andrey Konovalov | bffa986 | 2018-12-28 00:29:45 -0800 | [diff] [blame] | 153 | void kasan_poison_shadow(const void *address, size_t size, u8 value); |
| 154 | |
Marco Elver | b5f6e0f | 2019-07-11 20:54:07 -0700 | [diff] [blame] | 155 | /** |
| 156 | * check_memory_region - Check memory region, and report if invalid access. |
| 157 | * @addr: the accessed address |
| 158 | * @size: the accessed size |
| 159 | * @write: true if access is a write access |
| 160 | * @ret_ip: return address |
| 161 | * @return: true if access was valid, false if invalid |
| 162 | */ |
| 163 | bool check_memory_region(unsigned long addr, size_t size, bool write, |
Andrey Konovalov | bffa986 | 2018-12-28 00:29:45 -0800 | [diff] [blame] | 164 | unsigned long ret_ip); |
| 165 | |
Andrey Konovalov | 121e8f8 | 2018-12-28 00:30:42 -0800 | [diff] [blame] | 166 | void *find_first_bad_addr(void *addr, size_t size); |
Andrey Konovalov | 11cd3cd | 2018-12-28 00:30:38 -0800 | [diff] [blame] | 167 | const char *get_bug_type(struct kasan_access_info *info); |
| 168 | |
Walter Wu | 8cceeff | 2020-04-01 21:09:37 -0700 | [diff] [blame] | 169 | bool kasan_report(unsigned long addr, size_t size, |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 170 | bool is_write, unsigned long ip); |
Dmitry Vyukov | ee3ce77 | 2018-02-06 15:36:27 -0800 | [diff] [blame] | 171 | void kasan_report_invalid_free(void *object, unsigned long ip); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 172 | |
Walter Wu | ae8f06b | 2019-09-23 15:34:13 -0700 | [diff] [blame] | 173 | struct page *kasan_addr_to_page(const void *addr); |
| 174 | |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 175 | depot_stack_handle_t kasan_save_stack(gfp_t flags); |
Walter Wu | e4b7818 | 2020-08-06 23:24:39 -0700 | [diff] [blame] | 176 | void kasan_set_track(struct kasan_track *track, gfp_t flags); |
| 177 | void kasan_set_free_info(struct kmem_cache *cache, void *object, u8 tag); |
| 178 | struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, |
| 179 | void *object, u8 tag); |
Walter Wu | 26e760c | 2020-08-06 23:24:35 -0700 | [diff] [blame] | 180 | |
Andrey Konovalov | 2bd926b | 2018-12-28 00:29:53 -0800 | [diff] [blame] | 181 | #if defined(CONFIG_KASAN_GENERIC) && \ |
| 182 | (defined(CONFIG_SLAB) || defined(CONFIG_SLUB)) |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 183 | void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache); |
| 184 | void quarantine_reduce(void); |
| 185 | void quarantine_remove_cache(struct kmem_cache *cache); |
| 186 | #else |
| 187 | static inline void quarantine_put(struct kasan_free_meta *info, |
| 188 | struct kmem_cache *cache) { } |
| 189 | static inline void quarantine_reduce(void) { } |
| 190 | static inline void quarantine_remove_cache(struct kmem_cache *cache) { } |
| 191 | #endif |
| 192 | |
Andrey Konovalov | 3c9e3aa | 2018-12-28 00:30:16 -0800 | [diff] [blame] | 193 | #ifdef CONFIG_KASAN_SW_TAGS |
| 194 | |
Andrey Konovalov | 121e8f8 | 2018-12-28 00:30:42 -0800 | [diff] [blame] | 195 | void print_tags(u8 addr_tag, const void *addr); |
| 196 | |
Andrey Konovalov | 3c9e3aa | 2018-12-28 00:30:16 -0800 | [diff] [blame] | 197 | u8 random_tag(void); |
| 198 | |
| 199 | #else |
| 200 | |
Andrey Konovalov | 121e8f8 | 2018-12-28 00:30:42 -0800 | [diff] [blame] | 201 | static inline void print_tags(u8 addr_tag, const void *addr) { } |
| 202 | |
Andrey Konovalov | 3c9e3aa | 2018-12-28 00:30:16 -0800 | [diff] [blame] | 203 | static inline u8 random_tag(void) |
| 204 | { |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | #endif |
| 209 | |
| 210 | #ifndef arch_kasan_set_tag |
Qian Cai | c412a76 | 2019-03-28 20:43:15 -0700 | [diff] [blame] | 211 | static inline const void *arch_kasan_set_tag(const void *addr, u8 tag) |
| 212 | { |
| 213 | return addr; |
| 214 | } |
Andrey Konovalov | 3c9e3aa | 2018-12-28 00:30:16 -0800 | [diff] [blame] | 215 | #endif |
| 216 | #ifndef arch_kasan_reset_tag |
| 217 | #define arch_kasan_reset_tag(addr) ((void *)(addr)) |
| 218 | #endif |
| 219 | #ifndef arch_kasan_get_tag |
| 220 | #define arch_kasan_get_tag(addr) 0 |
| 221 | #endif |
| 222 | |
| 223 | #define set_tag(addr, tag) ((void *)arch_kasan_set_tag((addr), (tag))) |
| 224 | #define reset_tag(addr) ((void *)arch_kasan_reset_tag(addr)) |
| 225 | #define get_tag(addr) arch_kasan_get_tag(addr) |
| 226 | |
Alexander Potapenko | d321599 | 2018-02-06 15:36:20 -0800 | [diff] [blame] | 227 | /* |
| 228 | * Exported functions for interfaces called from assembly or from generated |
| 229 | * code. Declarations here to avoid warning about missing declarations. |
| 230 | */ |
| 231 | asmlinkage void kasan_unpoison_task_stack_below(const void *watermark); |
| 232 | void __asan_register_globals(struct kasan_global *globals, size_t size); |
| 233 | void __asan_unregister_globals(struct kasan_global *globals, size_t size); |
Alexander Potapenko | d321599 | 2018-02-06 15:36:20 -0800 | [diff] [blame] | 234 | void __asan_handle_no_return(void); |
Alexander Potapenko | d321599 | 2018-02-06 15:36:20 -0800 | [diff] [blame] | 235 | void __asan_alloca_poison(unsigned long addr, size_t size); |
| 236 | void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom); |
| 237 | |
| 238 | void __asan_load1(unsigned long addr); |
| 239 | void __asan_store1(unsigned long addr); |
| 240 | void __asan_load2(unsigned long addr); |
| 241 | void __asan_store2(unsigned long addr); |
| 242 | void __asan_load4(unsigned long addr); |
| 243 | void __asan_store4(unsigned long addr); |
| 244 | void __asan_load8(unsigned long addr); |
| 245 | void __asan_store8(unsigned long addr); |
| 246 | void __asan_load16(unsigned long addr); |
| 247 | void __asan_store16(unsigned long addr); |
Andrey Konovalov | 13cf048 | 2020-05-13 17:50:54 -0700 | [diff] [blame] | 248 | void __asan_loadN(unsigned long addr, size_t size); |
| 249 | void __asan_storeN(unsigned long addr, size_t size); |
Alexander Potapenko | d321599 | 2018-02-06 15:36:20 -0800 | [diff] [blame] | 250 | |
| 251 | void __asan_load1_noabort(unsigned long addr); |
| 252 | void __asan_store1_noabort(unsigned long addr); |
| 253 | void __asan_load2_noabort(unsigned long addr); |
| 254 | void __asan_store2_noabort(unsigned long addr); |
| 255 | void __asan_load4_noabort(unsigned long addr); |
| 256 | void __asan_store4_noabort(unsigned long addr); |
| 257 | void __asan_load8_noabort(unsigned long addr); |
| 258 | void __asan_store8_noabort(unsigned long addr); |
| 259 | void __asan_load16_noabort(unsigned long addr); |
| 260 | void __asan_store16_noabort(unsigned long addr); |
Andrey Konovalov | 13cf048 | 2020-05-13 17:50:54 -0700 | [diff] [blame] | 261 | void __asan_loadN_noabort(unsigned long addr, size_t size); |
| 262 | void __asan_storeN_noabort(unsigned long addr, size_t size); |
| 263 | |
| 264 | void __asan_report_load1_noabort(unsigned long addr); |
| 265 | void __asan_report_store1_noabort(unsigned long addr); |
| 266 | void __asan_report_load2_noabort(unsigned long addr); |
| 267 | void __asan_report_store2_noabort(unsigned long addr); |
| 268 | void __asan_report_load4_noabort(unsigned long addr); |
| 269 | void __asan_report_store4_noabort(unsigned long addr); |
| 270 | void __asan_report_load8_noabort(unsigned long addr); |
| 271 | void __asan_report_store8_noabort(unsigned long addr); |
| 272 | void __asan_report_load16_noabort(unsigned long addr); |
| 273 | void __asan_report_store16_noabort(unsigned long addr); |
| 274 | void __asan_report_load_n_noabort(unsigned long addr, size_t size); |
| 275 | void __asan_report_store_n_noabort(unsigned long addr, size_t size); |
Alexander Potapenko | d321599 | 2018-02-06 15:36:20 -0800 | [diff] [blame] | 276 | |
| 277 | void __asan_set_shadow_00(const void *addr, size_t size); |
| 278 | void __asan_set_shadow_f1(const void *addr, size_t size); |
| 279 | void __asan_set_shadow_f2(const void *addr, size_t size); |
| 280 | void __asan_set_shadow_f3(const void *addr, size_t size); |
| 281 | void __asan_set_shadow_f5(const void *addr, size_t size); |
| 282 | void __asan_set_shadow_f8(const void *addr, size_t size); |
| 283 | |
Andrey Konovalov | 13cf048 | 2020-05-13 17:50:54 -0700 | [diff] [blame] | 284 | void __hwasan_load1_noabort(unsigned long addr); |
| 285 | void __hwasan_store1_noabort(unsigned long addr); |
| 286 | void __hwasan_load2_noabort(unsigned long addr); |
| 287 | void __hwasan_store2_noabort(unsigned long addr); |
| 288 | void __hwasan_load4_noabort(unsigned long addr); |
| 289 | void __hwasan_store4_noabort(unsigned long addr); |
| 290 | void __hwasan_load8_noabort(unsigned long addr); |
| 291 | void __hwasan_store8_noabort(unsigned long addr); |
| 292 | void __hwasan_load16_noabort(unsigned long addr); |
| 293 | void __hwasan_store16_noabort(unsigned long addr); |
| 294 | void __hwasan_loadN_noabort(unsigned long addr, size_t size); |
| 295 | void __hwasan_storeN_noabort(unsigned long addr, size_t size); |
| 296 | |
| 297 | void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size); |
| 298 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 299 | #endif |