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