1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/static_key.h>
6 #include <linux/types.h>
15 #include <linux/linkage.h>
16 #include <asm/kasan.h>
18 /* kasan_data struct is used in KUnit tests for KASAN expected failures */
19 struct kunit_kasan_expectation {
26 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
28 #include <linux/pgtable.h>
30 /* Software KASAN implementations use shadow memory. */
32 #ifdef CONFIG_KASAN_SW_TAGS
33 #define KASAN_SHADOW_INIT 0xFF
35 #define KASAN_SHADOW_INIT 0
38 extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
39 extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
40 extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
41 extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD];
42 extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
44 int kasan_populate_early_shadow(const void *shadow_start,
45 const void *shadow_end);
47 static inline void *kasan_mem_to_shadow(const void *addr)
49 return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
50 + KASAN_SHADOW_OFFSET;
53 int kasan_add_zero_shadow(void *start, unsigned long size);
54 void kasan_remove_zero_shadow(void *start, unsigned long size);
56 /* Enable reporting bugs after kasan_disable_current() */
57 extern void kasan_enable_current(void);
59 /* Disable reporting bugs for current task */
60 extern void kasan_disable_current(void);
62 #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
64 static inline int kasan_add_zero_shadow(void *start, unsigned long size)
68 static inline void kasan_remove_zero_shadow(void *start,
72 static inline void kasan_enable_current(void) {}
73 static inline void kasan_disable_current(void) {}
75 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
80 int alloc_meta_offset;
84 #ifdef CONFIG_KASAN_HW_TAGS
85 DECLARE_STATIC_KEY_FALSE(kasan_flag_enabled);
86 static __always_inline bool kasan_enabled(void)
88 return static_branch_likely(&kasan_flag_enabled);
91 static inline bool kasan_enabled(void)
97 void __kasan_unpoison_range(const void *addr, size_t size);
98 static __always_inline void kasan_unpoison_range(const void *addr, size_t size)
101 __kasan_unpoison_range(addr, size);
104 void __kasan_alloc_pages(struct page *page, unsigned int order);
105 static __always_inline void kasan_alloc_pages(struct page *page,
109 __kasan_alloc_pages(page, order);
112 void __kasan_free_pages(struct page *page, unsigned int order);
113 static __always_inline void kasan_free_pages(struct page *page,
117 __kasan_free_pages(page, order);
120 void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
121 slab_flags_t *flags);
122 static __always_inline void kasan_cache_create(struct kmem_cache *cache,
123 unsigned int *size, slab_flags_t *flags)
126 __kasan_cache_create(cache, size, flags);
129 size_t __kasan_metadata_size(struct kmem_cache *cache);
130 static __always_inline size_t kasan_metadata_size(struct kmem_cache *cache)
133 return __kasan_metadata_size(cache);
137 void __kasan_poison_slab(struct page *page);
138 static __always_inline void kasan_poison_slab(struct page *page)
141 __kasan_poison_slab(page);
144 void __kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
145 static __always_inline void kasan_unpoison_object_data(struct kmem_cache *cache,
149 __kasan_unpoison_object_data(cache, object);
152 void __kasan_poison_object_data(struct kmem_cache *cache, void *object);
153 static __always_inline void kasan_poison_object_data(struct kmem_cache *cache,
157 __kasan_poison_object_data(cache, object);
160 void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache,
162 static __always_inline void * __must_check kasan_init_slab_obj(
163 struct kmem_cache *cache, const void *object)
166 return __kasan_init_slab_obj(cache, object);
167 return (void *)object;
170 bool __kasan_slab_free(struct kmem_cache *s, void *object, unsigned long ip);
171 static __always_inline bool kasan_slab_free(struct kmem_cache *s, void *object,
175 return __kasan_slab_free(s, object, ip);
179 void __kasan_slab_free_mempool(void *ptr, unsigned long ip);
180 static __always_inline void kasan_slab_free_mempool(void *ptr, unsigned long ip)
183 __kasan_slab_free_mempool(ptr, ip);
186 void * __must_check __kasan_slab_alloc(struct kmem_cache *s,
187 void *object, gfp_t flags);
188 static __always_inline void * __must_check kasan_slab_alloc(
189 struct kmem_cache *s, void *object, gfp_t flags)
192 return __kasan_slab_alloc(s, object, flags);
196 void * __must_check __kasan_kmalloc(struct kmem_cache *s, const void *object,
197 size_t size, gfp_t flags);
198 static __always_inline void * __must_check kasan_kmalloc(struct kmem_cache *s,
199 const void *object, size_t size, gfp_t flags)
202 return __kasan_kmalloc(s, object, size, flags);
203 return (void *)object;
206 void * __must_check __kasan_kmalloc_large(const void *ptr,
207 size_t size, gfp_t flags);
208 static __always_inline void * __must_check kasan_kmalloc_large(const void *ptr,
209 size_t size, gfp_t flags)
212 return __kasan_kmalloc_large(ptr, size, flags);
216 void * __must_check __kasan_krealloc(const void *object,
217 size_t new_size, gfp_t flags);
218 static __always_inline void * __must_check kasan_krealloc(const void *object,
219 size_t new_size, gfp_t flags)
222 return __kasan_krealloc(object, new_size, flags);
223 return (void *)object;
226 void __kasan_kfree_large(void *ptr, unsigned long ip);
227 static __always_inline void kasan_kfree_large(void *ptr, unsigned long ip)
230 __kasan_kfree_large(ptr, ip);
233 bool kasan_save_enable_multi_shot(void);
234 void kasan_restore_multi_shot(bool enabled);
236 #else /* CONFIG_KASAN */
238 static inline bool kasan_enabled(void)
242 static inline void kasan_unpoison_range(const void *address, size_t size) {}
243 static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
244 static inline void kasan_free_pages(struct page *page, unsigned int order) {}
245 static inline void kasan_cache_create(struct kmem_cache *cache,
247 slab_flags_t *flags) {}
248 static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
249 static inline void kasan_poison_slab(struct page *page) {}
250 static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
252 static inline void kasan_poison_object_data(struct kmem_cache *cache,
254 static inline void *kasan_init_slab_obj(struct kmem_cache *cache,
257 return (void *)object;
259 static inline bool kasan_slab_free(struct kmem_cache *s, void *object,
264 static inline void kasan_slab_free_mempool(void *ptr, unsigned long ip) {}
265 static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object,
270 static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object,
271 size_t size, gfp_t flags)
273 return (void *)object;
275 static inline void *kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags)
279 static inline void *kasan_krealloc(const void *object, size_t new_size,
282 return (void *)object;
284 static inline void kasan_kfree_large(void *ptr, unsigned long ip) {}
286 #endif /* CONFIG_KASAN */
288 #if defined(CONFIG_KASAN) && CONFIG_KASAN_STACK
289 void kasan_unpoison_task_stack(struct task_struct *task);
291 static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
294 #ifdef CONFIG_KASAN_GENERIC
296 void kasan_cache_shrink(struct kmem_cache *cache);
297 void kasan_cache_shutdown(struct kmem_cache *cache);
298 void kasan_record_aux_stack(void *ptr);
300 #else /* CONFIG_KASAN_GENERIC */
302 static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
303 static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
304 static inline void kasan_record_aux_stack(void *ptr) {}
306 #endif /* CONFIG_KASAN_GENERIC */
308 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
310 static inline void *kasan_reset_tag(const void *addr)
312 return (void *)arch_kasan_reset_tag(addr);
315 bool kasan_report(unsigned long addr, size_t size,
316 bool is_write, unsigned long ip);
318 #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
320 static inline void *kasan_reset_tag(const void *addr)
325 #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS*/
327 #ifdef CONFIG_KASAN_SW_TAGS
328 void __init kasan_init_sw_tags(void);
330 static inline void kasan_init_sw_tags(void) { }
333 #ifdef CONFIG_KASAN_HW_TAGS
334 void kasan_init_hw_tags_cpu(void);
335 void __init kasan_init_hw_tags(void);
337 static inline void kasan_init_hw_tags_cpu(void) { }
338 static inline void kasan_init_hw_tags(void) { }
341 #ifdef CONFIG_KASAN_VMALLOC
343 int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
344 void kasan_poison_vmalloc(const void *start, unsigned long size);
345 void kasan_unpoison_vmalloc(const void *start, unsigned long size);
346 void kasan_release_vmalloc(unsigned long start, unsigned long end,
347 unsigned long free_region_start,
348 unsigned long free_region_end);
350 #else /* CONFIG_KASAN_VMALLOC */
352 static inline int kasan_populate_vmalloc(unsigned long start,
358 static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
360 static inline void kasan_unpoison_vmalloc(const void *start, unsigned long size)
362 static inline void kasan_release_vmalloc(unsigned long start,
364 unsigned long free_region_start,
365 unsigned long free_region_end) {}
367 #endif /* CONFIG_KASAN_VMALLOC */
369 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
370 !defined(CONFIG_KASAN_VMALLOC)
373 * These functions provide a special case to support backing module
374 * allocations with real shadow memory. With KASAN vmalloc, the special
375 * case is unnecessary, as the work is handled in the generic case.
377 int kasan_module_alloc(void *addr, size_t size);
378 void kasan_free_shadow(const struct vm_struct *vm);
380 #else /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
382 static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
383 static inline void kasan_free_shadow(const struct vm_struct *vm) {}
385 #endif /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
387 #ifdef CONFIG_KASAN_INLINE
388 void kasan_non_canonical_hook(unsigned long addr);
389 #else /* CONFIG_KASAN_INLINE */
390 static inline void kasan_non_canonical_hook(unsigned long addr) { }
391 #endif /* CONFIG_KASAN_INLINE */
393 #endif /* LINUX_KASAN_H */