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