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 | |
| 5 | #include <linux/types.h> |
| 6 | |
| 7 | struct kmem_cache; |
| 8 | struct page; |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 9 | struct vm_struct; |
Masami Hiramatsu | 5be9b73 | 2017-03-16 16:40:21 -0700 | [diff] [blame] | 10 | struct task_struct; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 11 | |
| 12 | #ifdef CONFIG_KASAN |
| 13 | |
Mike Rapoport | ca5999f | 2020-06-08 21:32:38 -0700 | [diff] [blame] | 14 | #include <linux/pgtable.h> |
Mike Rapoport | 65fddcf | 2020-06-08 21:32:42 -0700 | [diff] [blame] | 15 | #include <asm/kasan.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 16 | |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 17 | extern unsigned char kasan_early_shadow_page[PAGE_SIZE]; |
| 18 | extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE]; |
| 19 | extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD]; |
| 20 | extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD]; |
| 21 | extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D]; |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 22 | |
Andrey Konovalov | 9577dd7 | 2018-12-28 00:30:01 -0800 | [diff] [blame] | 23 | int kasan_populate_early_shadow(const void *shadow_start, |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 24 | const void *shadow_end); |
| 25 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 26 | static inline void *kasan_mem_to_shadow(const void *addr) |
| 27 | { |
| 28 | return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) |
| 29 | + KASAN_SHADOW_OFFSET; |
| 30 | } |
| 31 | |
| 32 | /* Enable reporting bugs after kasan_disable_current() */ |
Ingo Molnar | af8601a | 2017-02-03 09:57:00 +0100 | [diff] [blame] | 33 | extern void kasan_enable_current(void); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 34 | |
| 35 | /* Disable reporting bugs for current task */ |
Ingo Molnar | af8601a | 2017-02-03 09:57:00 +0100 | [diff] [blame] | 36 | extern void kasan_disable_current(void); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 37 | |
| 38 | void kasan_unpoison_shadow(const void *address, size_t size); |
| 39 | |
Mark Rutland | e3ae116 | 2016-03-09 14:08:15 -0800 | [diff] [blame] | 40 | void kasan_unpoison_task_stack(struct task_struct *task); |
Dmitry Vyukov | 9f7d416 | 2016-10-14 16:07:23 +0200 | [diff] [blame] | 41 | void kasan_unpoison_stack_above_sp_to(const void *watermark); |
Mark Rutland | e3ae116 | 2016-03-09 14:08:15 -0800 | [diff] [blame] | 42 | |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 43 | void kasan_alloc_pages(struct page *page, unsigned int order); |
| 44 | void kasan_free_pages(struct page *page, unsigned int order); |
| 45 | |
Alexey Dobriyan | be4a798 | 2018-04-05 16:21:28 -0700 | [diff] [blame] | 46 | void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, |
Alexey Dobriyan | d50112e | 2017-11-15 17:32:18 -0800 | [diff] [blame] | 47 | slab_flags_t *flags); |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 48 | |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 49 | void kasan_poison_slab(struct page *page); |
| 50 | void kasan_unpoison_object_data(struct kmem_cache *cache, void *object); |
| 51 | void kasan_poison_object_data(struct kmem_cache *cache, void *object); |
Andrey Konovalov | 66afc7f | 2018-12-28 00:31:01 -0800 | [diff] [blame] | 52 | void * __must_check kasan_init_slab_obj(struct kmem_cache *cache, |
| 53 | const void *object); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 54 | |
Andrey Konovalov | 66afc7f | 2018-12-28 00:31:01 -0800 | [diff] [blame] | 55 | void * __must_check kasan_kmalloc_large(const void *ptr, size_t size, |
| 56 | gfp_t flags); |
Dmitry Vyukov | ee3ce77 | 2018-02-06 15:36:27 -0800 | [diff] [blame] | 57 | void kasan_kfree_large(void *ptr, unsigned long ip); |
Dmitry Vyukov | 6860f63 | 2018-02-06 15:36:30 -0800 | [diff] [blame] | 58 | void kasan_poison_kfree(void *ptr, unsigned long ip); |
Andrey Konovalov | 66afc7f | 2018-12-28 00:31:01 -0800 | [diff] [blame] | 59 | void * __must_check kasan_kmalloc(struct kmem_cache *s, const void *object, |
| 60 | size_t size, gfp_t flags); |
| 61 | void * __must_check kasan_krealloc(const void *object, size_t new_size, |
| 62 | gfp_t flags); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 63 | |
Andrey Konovalov | 66afc7f | 2018-12-28 00:31:01 -0800 | [diff] [blame] | 64 | void * __must_check kasan_slab_alloc(struct kmem_cache *s, void *object, |
| 65 | gfp_t flags); |
Dmitry Vyukov | ee3ce77 | 2018-02-06 15:36:27 -0800 | [diff] [blame] | 66 | bool kasan_slab_free(struct kmem_cache *s, void *object, unsigned long ip); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 67 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 68 | struct kasan_cache { |
| 69 | int alloc_meta_offset; |
| 70 | int free_meta_offset; |
| 71 | }; |
| 72 | |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 73 | /* |
| 74 | * These functions provide a special case to support backing module |
| 75 | * allocations with real shadow memory. With KASAN vmalloc, the special |
| 76 | * case is unnecessary, as the work is handled in the generic case. |
| 77 | */ |
| 78 | #ifndef CONFIG_KASAN_VMALLOC |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 79 | int kasan_module_alloc(void *addr, size_t size); |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 80 | void kasan_free_shadow(const struct vm_struct *vm); |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 81 | #else |
| 82 | static inline int kasan_module_alloc(void *addr, size_t size) { return 0; } |
| 83 | static inline void kasan_free_shadow(const struct vm_struct *vm) {} |
| 84 | #endif |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 85 | |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 86 | int kasan_add_zero_shadow(void *start, unsigned long size); |
| 87 | void kasan_remove_zero_shadow(void *start, unsigned long size); |
| 88 | |
Marco Elver | 0d4ca4c | 2019-07-11 20:54:18 -0700 | [diff] [blame] | 89 | size_t __ksize(const void *); |
| 90 | static inline void kasan_unpoison_slab(const void *ptr) |
| 91 | { |
| 92 | kasan_unpoison_shadow(ptr, __ksize(ptr)); |
| 93 | } |
Alexander Potapenko | 80a9201 | 2016-07-28 15:49:07 -0700 | [diff] [blame] | 94 | size_t kasan_metadata_size(struct kmem_cache *cache); |
Andrey Ryabinin | 9b75a86 | 2016-06-24 14:49:34 -0700 | [diff] [blame] | 95 | |
Mark Rutland | b0845ce | 2017-03-31 15:12:04 -0700 | [diff] [blame] | 96 | bool kasan_save_enable_multi_shot(void); |
| 97 | void kasan_restore_multi_shot(bool enabled); |
| 98 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 99 | #else /* CONFIG_KASAN */ |
| 100 | |
| 101 | static inline void kasan_unpoison_shadow(const void *address, size_t size) {} |
| 102 | |
Mark Rutland | e3ae116 | 2016-03-09 14:08:15 -0800 | [diff] [blame] | 103 | static inline void kasan_unpoison_task_stack(struct task_struct *task) {} |
Dmitry Vyukov | 9f7d416 | 2016-10-14 16:07:23 +0200 | [diff] [blame] | 104 | static inline void kasan_unpoison_stack_above_sp_to(const void *watermark) {} |
Mark Rutland | e3ae116 | 2016-03-09 14:08:15 -0800 | [diff] [blame] | 105 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 106 | static inline void kasan_enable_current(void) {} |
| 107 | static inline void kasan_disable_current(void) {} |
| 108 | |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 109 | static inline void kasan_alloc_pages(struct page *page, unsigned int order) {} |
| 110 | static inline void kasan_free_pages(struct page *page, unsigned int order) {} |
| 111 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 112 | static inline void kasan_cache_create(struct kmem_cache *cache, |
Alexey Dobriyan | be4a798 | 2018-04-05 16:21:28 -0700 | [diff] [blame] | 113 | unsigned int *size, |
Alexey Dobriyan | d50112e | 2017-11-15 17:32:18 -0800 | [diff] [blame] | 114 | slab_flags_t *flags) {} |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 115 | |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 116 | static inline void kasan_poison_slab(struct page *page) {} |
| 117 | static inline void kasan_unpoison_object_data(struct kmem_cache *cache, |
| 118 | void *object) {} |
| 119 | static inline void kasan_poison_object_data(struct kmem_cache *cache, |
| 120 | void *object) {} |
Andrey Konovalov | 0116523 | 2018-12-28 00:29:37 -0800 | [diff] [blame] | 121 | static inline void *kasan_init_slab_obj(struct kmem_cache *cache, |
| 122 | const void *object) |
| 123 | { |
| 124 | return (void *)object; |
| 125 | } |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 126 | |
Andrey Konovalov | 0116523 | 2018-12-28 00:29:37 -0800 | [diff] [blame] | 127 | static inline void *kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags) |
| 128 | { |
| 129 | return ptr; |
| 130 | } |
Dmitry Vyukov | ee3ce77 | 2018-02-06 15:36:27 -0800 | [diff] [blame] | 131 | static inline void kasan_kfree_large(void *ptr, unsigned long ip) {} |
Dmitry Vyukov | 6860f63 | 2018-02-06 15:36:30 -0800 | [diff] [blame] | 132 | static inline void kasan_poison_kfree(void *ptr, unsigned long ip) {} |
Andrey Konovalov | 0116523 | 2018-12-28 00:29:37 -0800 | [diff] [blame] | 133 | static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object, |
| 134 | size_t size, gfp_t flags) |
| 135 | { |
| 136 | return (void *)object; |
| 137 | } |
| 138 | static inline void *kasan_krealloc(const void *object, size_t new_size, |
| 139 | gfp_t flags) |
| 140 | { |
| 141 | return (void *)object; |
| 142 | } |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 143 | |
Andrey Konovalov | 0116523 | 2018-12-28 00:29:37 -0800 | [diff] [blame] | 144 | static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object, |
| 145 | gfp_t flags) |
| 146 | { |
| 147 | return object; |
| 148 | } |
Dmitry Vyukov | ee3ce77 | 2018-02-06 15:36:27 -0800 | [diff] [blame] | 149 | static inline bool kasan_slab_free(struct kmem_cache *s, void *object, |
| 150 | unsigned long ip) |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 151 | { |
| 152 | return false; |
| 153 | } |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 154 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 155 | static inline int kasan_module_alloc(void *addr, size_t size) { return 0; } |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 156 | static inline void kasan_free_shadow(const struct vm_struct *vm) {} |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 157 | |
Andrey Ryabinin | 0207df4 | 2018-08-17 15:47:04 -0700 | [diff] [blame] | 158 | static inline int kasan_add_zero_shadow(void *start, unsigned long size) |
| 159 | { |
| 160 | return 0; |
| 161 | } |
| 162 | static inline void kasan_remove_zero_shadow(void *start, |
| 163 | unsigned long size) |
| 164 | {} |
| 165 | |
Andrey Ryabinin | 9b75a86 | 2016-06-24 14:49:34 -0700 | [diff] [blame] | 166 | static inline void kasan_unpoison_slab(const void *ptr) { } |
Alexander Potapenko | 80a9201 | 2016-07-28 15:49:07 -0700 | [diff] [blame] | 167 | static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; } |
Andrey Ryabinin | 9b75a86 | 2016-06-24 14:49:34 -0700 | [diff] [blame] | 168 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 169 | #endif /* CONFIG_KASAN */ |
| 170 | |
Andrey Konovalov | 2bd926b | 2018-12-28 00:29:53 -0800 | [diff] [blame] | 171 | #ifdef CONFIG_KASAN_GENERIC |
| 172 | |
Andrey Konovalov | 080eb83 | 2018-12-28 00:30:09 -0800 | [diff] [blame] | 173 | #define KASAN_SHADOW_INIT 0 |
| 174 | |
Andrey Konovalov | 2bd926b | 2018-12-28 00:29:53 -0800 | [diff] [blame] | 175 | void kasan_cache_shrink(struct kmem_cache *cache); |
| 176 | void kasan_cache_shutdown(struct kmem_cache *cache); |
| 177 | |
| 178 | #else /* CONFIG_KASAN_GENERIC */ |
| 179 | |
| 180 | static inline void kasan_cache_shrink(struct kmem_cache *cache) {} |
| 181 | static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} |
| 182 | |
| 183 | #endif /* CONFIG_KASAN_GENERIC */ |
| 184 | |
Andrey Konovalov | 080eb83 | 2018-12-28 00:30:09 -0800 | [diff] [blame] | 185 | #ifdef CONFIG_KASAN_SW_TAGS |
| 186 | |
| 187 | #define KASAN_SHADOW_INIT 0xFF |
| 188 | |
Andrey Konovalov | 3c9e3aa | 2018-12-28 00:30:16 -0800 | [diff] [blame] | 189 | void kasan_init_tags(void); |
| 190 | |
| 191 | void *kasan_reset_tag(const void *addr); |
| 192 | |
Walter Wu | 8cceeff | 2020-04-01 21:09:37 -0700 | [diff] [blame] | 193 | bool kasan_report(unsigned long addr, size_t size, |
Andrey Konovalov | 41eea9c | 2018-12-28 00:30:54 -0800 | [diff] [blame] | 194 | bool is_write, unsigned long ip); |
| 195 | |
Andrey Konovalov | 3c9e3aa | 2018-12-28 00:30:16 -0800 | [diff] [blame] | 196 | #else /* CONFIG_KASAN_SW_TAGS */ |
| 197 | |
| 198 | static inline void kasan_init_tags(void) { } |
| 199 | |
| 200 | static inline void *kasan_reset_tag(const void *addr) |
| 201 | { |
| 202 | return (void *)addr; |
| 203 | } |
| 204 | |
Andrey Konovalov | 080eb83 | 2018-12-28 00:30:09 -0800 | [diff] [blame] | 205 | #endif /* CONFIG_KASAN_SW_TAGS */ |
| 206 | |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 207 | #ifdef CONFIG_KASAN_VMALLOC |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 208 | int kasan_populate_vmalloc(unsigned long addr, unsigned long size); |
| 209 | void kasan_poison_vmalloc(const void *start, unsigned long size); |
| 210 | void kasan_unpoison_vmalloc(const void *start, unsigned long size); |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 211 | void kasan_release_vmalloc(unsigned long start, unsigned long end, |
| 212 | unsigned long free_region_start, |
| 213 | unsigned long free_region_end); |
| 214 | #else |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 215 | static inline int kasan_populate_vmalloc(unsigned long start, |
| 216 | unsigned long size) |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 217 | { |
| 218 | return 0; |
| 219 | } |
| 220 | |
Andrey Ryabinin | d98c9e8 | 2019-12-17 20:51:38 -0800 | [diff] [blame] | 221 | static inline void kasan_poison_vmalloc(const void *start, unsigned long size) |
| 222 | { } |
| 223 | static inline void kasan_unpoison_vmalloc(const void *start, unsigned long size) |
| 224 | { } |
Daniel Axtens | 3c5c3cf | 2019-11-30 17:54:50 -0800 | [diff] [blame] | 225 | static inline void kasan_release_vmalloc(unsigned long start, |
| 226 | unsigned long end, |
| 227 | unsigned long free_region_start, |
| 228 | unsigned long free_region_end) {} |
| 229 | #endif |
| 230 | |
Jann Horn | 2f004ee | 2019-12-19 00:11:50 +0100 | [diff] [blame] | 231 | #ifdef CONFIG_KASAN_INLINE |
| 232 | void kasan_non_canonical_hook(unsigned long addr); |
| 233 | #else /* CONFIG_KASAN_INLINE */ |
| 234 | static inline void kasan_non_canonical_hook(unsigned long addr) { } |
| 235 | #endif /* CONFIG_KASAN_INLINE */ |
| 236 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 237 | #endif /* LINUX_KASAN_H */ |