1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/types.h>
14 #include <linux/linkage.h>
15 #include <asm/kasan.h>
17 /* kasan_data struct is used in KUnit tests for KASAN expected failures */
18 struct kunit_kasan_expectation {
25 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
27 #include <linux/pgtable.h>
29 /* Software KASAN implementations use shadow memory. */
31 #ifdef CONFIG_KASAN_SW_TAGS
32 #define KASAN_SHADOW_INIT 0xFF
34 #define KASAN_SHADOW_INIT 0
37 extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
38 extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
39 extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
40 extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD];
41 extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
43 int kasan_populate_early_shadow(const void *shadow_start,
44 const void *shadow_end);
46 static inline void *kasan_mem_to_shadow(const void *addr)
48 return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
49 + KASAN_SHADOW_OFFSET;
52 int kasan_add_zero_shadow(void *start, unsigned long size);
53 void kasan_remove_zero_shadow(void *start, unsigned long size);
55 /* Enable reporting bugs after kasan_disable_current() */
56 extern void kasan_enable_current(void);
58 /* Disable reporting bugs for current task */
59 extern void kasan_disable_current(void);
61 #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
63 static inline int kasan_add_zero_shadow(void *start, unsigned long size)
67 static inline void kasan_remove_zero_shadow(void *start,
71 static inline void kasan_enable_current(void) {}
72 static inline void kasan_disable_current(void) {}
74 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
78 void kasan_unpoison_range(const void *address, size_t size);
80 void kasan_alloc_pages(struct page *page, unsigned int order);
81 void kasan_free_pages(struct page *page, unsigned int order);
83 void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
86 void kasan_poison_slab(struct page *page);
87 void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
88 void kasan_poison_object_data(struct kmem_cache *cache, void *object);
89 void * __must_check kasan_init_slab_obj(struct kmem_cache *cache,
92 void * __must_check kasan_kmalloc_large(const void *ptr, size_t size,
94 void kasan_kfree_large(void *ptr, unsigned long ip);
95 void kasan_poison_kfree(void *ptr, unsigned long ip);
96 void * __must_check kasan_kmalloc(struct kmem_cache *s, const void *object,
97 size_t size, gfp_t flags);
98 void * __must_check kasan_krealloc(const void *object, size_t new_size,
101 void * __must_check kasan_slab_alloc(struct kmem_cache *s, void *object,
103 bool kasan_slab_free(struct kmem_cache *s, void *object, unsigned long ip);
106 int alloc_meta_offset;
107 int free_meta_offset;
110 size_t __ksize(const void *);
111 static inline void kasan_unpoison_slab(const void *ptr)
113 kasan_unpoison_range(ptr, __ksize(ptr));
115 size_t kasan_metadata_size(struct kmem_cache *cache);
117 bool kasan_save_enable_multi_shot(void);
118 void kasan_restore_multi_shot(bool enabled);
120 #else /* CONFIG_KASAN */
122 static inline void kasan_unpoison_range(const void *address, size_t size) {}
124 static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
125 static inline void kasan_free_pages(struct page *page, unsigned int order) {}
127 static inline void kasan_cache_create(struct kmem_cache *cache,
129 slab_flags_t *flags) {}
131 static inline void kasan_poison_slab(struct page *page) {}
132 static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
134 static inline void kasan_poison_object_data(struct kmem_cache *cache,
136 static inline void *kasan_init_slab_obj(struct kmem_cache *cache,
139 return (void *)object;
142 static inline void *kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags)
146 static inline void kasan_kfree_large(void *ptr, unsigned long ip) {}
147 static inline void kasan_poison_kfree(void *ptr, unsigned long ip) {}
148 static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object,
149 size_t size, gfp_t flags)
151 return (void *)object;
153 static inline void *kasan_krealloc(const void *object, size_t new_size,
156 return (void *)object;
159 static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object,
164 static inline bool kasan_slab_free(struct kmem_cache *s, void *object,
170 static inline void kasan_unpoison_slab(const void *ptr) { }
171 static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
173 #endif /* CONFIG_KASAN */
175 #if defined(CONFIG_KASAN) && CONFIG_KASAN_STACK
176 void kasan_unpoison_task_stack(struct task_struct *task);
178 static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
181 #ifdef CONFIG_KASAN_GENERIC
183 void kasan_cache_shrink(struct kmem_cache *cache);
184 void kasan_cache_shutdown(struct kmem_cache *cache);
185 void kasan_record_aux_stack(void *ptr);
187 #else /* CONFIG_KASAN_GENERIC */
189 static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
190 static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
191 static inline void kasan_record_aux_stack(void *ptr) {}
193 #endif /* CONFIG_KASAN_GENERIC */
195 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
197 void *kasan_reset_tag(const void *addr);
199 bool kasan_report(unsigned long addr, size_t size,
200 bool is_write, unsigned long ip);
202 #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
204 static inline void *kasan_reset_tag(const void *addr)
209 #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS*/
211 #ifdef CONFIG_KASAN_SW_TAGS
212 void __init kasan_init_sw_tags(void);
214 static inline void kasan_init_sw_tags(void) { }
217 #ifdef CONFIG_KASAN_HW_TAGS
218 void kasan_init_hw_tags_cpu(void);
219 void __init kasan_init_hw_tags(void);
221 static inline void kasan_init_hw_tags_cpu(void) { }
222 static inline void kasan_init_hw_tags(void) { }
225 #ifdef CONFIG_KASAN_VMALLOC
227 int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
228 void kasan_poison_vmalloc(const void *start, unsigned long size);
229 void kasan_unpoison_vmalloc(const void *start, unsigned long size);
230 void kasan_release_vmalloc(unsigned long start, unsigned long end,
231 unsigned long free_region_start,
232 unsigned long free_region_end);
234 #else /* CONFIG_KASAN_VMALLOC */
236 static inline int kasan_populate_vmalloc(unsigned long start,
242 static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
244 static inline void kasan_unpoison_vmalloc(const void *start, unsigned long size)
246 static inline void kasan_release_vmalloc(unsigned long start,
248 unsigned long free_region_start,
249 unsigned long free_region_end) {}
251 #endif /* CONFIG_KASAN_VMALLOC */
253 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
254 !defined(CONFIG_KASAN_VMALLOC)
257 * These functions provide a special case to support backing module
258 * allocations with real shadow memory. With KASAN vmalloc, the special
259 * case is unnecessary, as the work is handled in the generic case.
261 int kasan_module_alloc(void *addr, size_t size);
262 void kasan_free_shadow(const struct vm_struct *vm);
264 #else /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
266 static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
267 static inline void kasan_free_shadow(const struct vm_struct *vm) {}
269 #endif /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
271 #ifdef CONFIG_KASAN_INLINE
272 void kasan_non_canonical_hook(unsigned long addr);
273 #else /* CONFIG_KASAN_INLINE */
274 static inline void kasan_non_canonical_hook(unsigned long addr) { }
275 #endif /* CONFIG_KASAN_INLINE */
277 #endif /* LINUX_KASAN_H */