1 /* SPDX-License-Identifier: GPL-2.0 */
6 #include <linux/kasan-enabled.h>
7 #include <linux/kernel.h>
8 #include <linux/static_key.h>
9 #include <linux/types.h>
19 #include <linux/linkage.h>
20 #include <asm/kasan.h>
24 typedef unsigned int __bitwise kasan_vmalloc_flags_t;
26 #define KASAN_VMALLOC_NONE ((__force kasan_vmalloc_flags_t)0x00u)
27 #define KASAN_VMALLOC_INIT ((__force kasan_vmalloc_flags_t)0x01u)
28 #define KASAN_VMALLOC_VM_ALLOC ((__force kasan_vmalloc_flags_t)0x02u)
29 #define KASAN_VMALLOC_PROT_NORMAL ((__force kasan_vmalloc_flags_t)0x04u)
31 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
33 #include <linux/pgtable.h>
35 /* Software KASAN implementations use shadow memory. */
37 #ifdef CONFIG_KASAN_SW_TAGS
38 /* This matches KASAN_TAG_INVALID. */
39 #define KASAN_SHADOW_INIT 0xFE
41 #define KASAN_SHADOW_INIT 0
44 #ifndef PTE_HWTABLE_PTRS
45 #define PTE_HWTABLE_PTRS 0
48 extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
49 extern pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS];
50 extern pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD];
51 extern pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD];
52 extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
54 int kasan_populate_early_shadow(const void *shadow_start,
55 const void *shadow_end);
57 static inline void *kasan_mem_to_shadow(const void *addr)
59 return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
60 + KASAN_SHADOW_OFFSET;
63 int kasan_add_zero_shadow(void *start, unsigned long size);
64 void kasan_remove_zero_shadow(void *start, unsigned long size);
66 /* Enable reporting bugs after kasan_disable_current() */
67 extern void kasan_enable_current(void);
69 /* Disable reporting bugs for current task */
70 extern void kasan_disable_current(void);
72 #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
74 static inline int kasan_add_zero_shadow(void *start, unsigned long size)
78 static inline void kasan_remove_zero_shadow(void *start,
82 static inline void kasan_enable_current(void) {}
83 static inline void kasan_disable_current(void) {}
85 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
87 #ifdef CONFIG_KASAN_HW_TAGS
89 #else /* CONFIG_KASAN_HW_TAGS */
91 #endif /* CONFIG_KASAN_HW_TAGS */
93 static inline bool kasan_has_integrated_init(void)
95 return kasan_hw_tags_enabled();
99 void __kasan_unpoison_range(const void *addr, size_t size);
100 static __always_inline void kasan_unpoison_range(const void *addr, size_t size)
103 __kasan_unpoison_range(addr, size);
106 void __kasan_poison_pages(struct page *page, unsigned int order, bool init);
107 static __always_inline void kasan_poison_pages(struct page *page,
108 unsigned int order, bool init)
111 __kasan_poison_pages(page, order, init);
114 bool __kasan_unpoison_pages(struct page *page, unsigned int order, bool init);
115 static __always_inline bool kasan_unpoison_pages(struct page *page,
116 unsigned int order, bool init)
119 return __kasan_unpoison_pages(page, order, init);
123 void __kasan_poison_slab(struct slab *slab);
124 static __always_inline void kasan_poison_slab(struct slab *slab)
127 __kasan_poison_slab(slab);
130 void __kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
131 static __always_inline void kasan_unpoison_object_data(struct kmem_cache *cache,
135 __kasan_unpoison_object_data(cache, object);
138 void __kasan_poison_object_data(struct kmem_cache *cache, void *object);
139 static __always_inline void kasan_poison_object_data(struct kmem_cache *cache,
143 __kasan_poison_object_data(cache, object);
146 void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache,
148 static __always_inline void * __must_check kasan_init_slab_obj(
149 struct kmem_cache *cache, const void *object)
152 return __kasan_init_slab_obj(cache, object);
153 return (void *)object;
156 bool __kasan_slab_free(struct kmem_cache *s, void *object,
157 unsigned long ip, bool init);
158 static __always_inline bool kasan_slab_free(struct kmem_cache *s,
159 void *object, bool init)
162 return __kasan_slab_free(s, object, _RET_IP_, init);
166 void __kasan_kfree_large(void *ptr, unsigned long ip);
167 static __always_inline void kasan_kfree_large(void *ptr)
170 __kasan_kfree_large(ptr, _RET_IP_);
173 void __kasan_slab_free_mempool(void *ptr, unsigned long ip);
174 static __always_inline void kasan_slab_free_mempool(void *ptr)
177 __kasan_slab_free_mempool(ptr, _RET_IP_);
180 void * __must_check __kasan_slab_alloc(struct kmem_cache *s,
181 void *object, gfp_t flags, bool init);
182 static __always_inline void * __must_check kasan_slab_alloc(
183 struct kmem_cache *s, void *object, gfp_t flags, bool init)
186 return __kasan_slab_alloc(s, object, flags, init);
190 void * __must_check __kasan_kmalloc(struct kmem_cache *s, const void *object,
191 size_t size, gfp_t flags);
192 static __always_inline void * __must_check kasan_kmalloc(struct kmem_cache *s,
193 const void *object, size_t size, gfp_t flags)
196 return __kasan_kmalloc(s, object, size, flags);
197 return (void *)object;
200 void * __must_check __kasan_kmalloc_large(const void *ptr,
201 size_t size, gfp_t flags);
202 static __always_inline void * __must_check kasan_kmalloc_large(const void *ptr,
203 size_t size, gfp_t flags)
206 return __kasan_kmalloc_large(ptr, size, flags);
210 void * __must_check __kasan_krealloc(const void *object,
211 size_t new_size, gfp_t flags);
212 static __always_inline void * __must_check kasan_krealloc(const void *object,
213 size_t new_size, gfp_t flags)
216 return __kasan_krealloc(object, new_size, flags);
217 return (void *)object;
221 * Unlike kasan_check_read/write(), kasan_check_byte() is performed even for
222 * the hardware tag-based mode that doesn't rely on compiler instrumentation.
224 bool __kasan_check_byte(const void *addr, unsigned long ip);
225 static __always_inline bool kasan_check_byte(const void *addr)
228 return __kasan_check_byte(addr, _RET_IP_);
232 #else /* CONFIG_KASAN */
234 static inline void kasan_unpoison_range(const void *address, size_t size) {}
235 static inline void kasan_poison_pages(struct page *page, unsigned int order,
237 static inline bool kasan_unpoison_pages(struct page *page, unsigned int order,
242 static inline void kasan_poison_slab(struct slab *slab) {}
243 static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
245 static inline void kasan_poison_object_data(struct kmem_cache *cache,
247 static inline void *kasan_init_slab_obj(struct kmem_cache *cache,
250 return (void *)object;
252 static inline bool kasan_slab_free(struct kmem_cache *s, void *object, bool init)
256 static inline void kasan_kfree_large(void *ptr) {}
257 static inline void kasan_slab_free_mempool(void *ptr) {}
258 static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object,
259 gfp_t flags, bool init)
263 static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object,
264 size_t size, gfp_t flags)
266 return (void *)object;
268 static inline void *kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags)
272 static inline void *kasan_krealloc(const void *object, size_t new_size,
275 return (void *)object;
277 static inline bool kasan_check_byte(const void *address)
282 #endif /* CONFIG_KASAN */
284 #if defined(CONFIG_KASAN) && defined(CONFIG_KASAN_STACK)
285 void kasan_unpoison_task_stack(struct task_struct *task);
287 static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
290 #ifdef CONFIG_KASAN_GENERIC
293 int alloc_meta_offset;
294 int free_meta_offset;
297 size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object);
298 slab_flags_t kasan_never_merge(void);
299 void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
300 slab_flags_t *flags);
302 void kasan_cache_shrink(struct kmem_cache *cache);
303 void kasan_cache_shutdown(struct kmem_cache *cache);
304 void kasan_record_aux_stack(void *ptr);
305 void kasan_record_aux_stack_noalloc(void *ptr);
307 #else /* CONFIG_KASAN_GENERIC */
309 /* Tag-based KASAN modes do not use per-object metadata. */
310 static inline size_t kasan_metadata_size(struct kmem_cache *cache,
315 /* And thus nothing prevents cache merging. */
316 static inline slab_flags_t kasan_never_merge(void)
320 /* And no cache-related metadata initialization is required. */
321 static inline void kasan_cache_create(struct kmem_cache *cache,
323 slab_flags_t *flags) {}
325 static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
326 static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
327 static inline void kasan_record_aux_stack(void *ptr) {}
328 static inline void kasan_record_aux_stack_noalloc(void *ptr) {}
330 #endif /* CONFIG_KASAN_GENERIC */
332 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
334 static inline void *kasan_reset_tag(const void *addr)
336 return (void *)arch_kasan_reset_tag(addr);
340 * kasan_report - print a report about a bad memory access detected by KASAN
341 * @addr: address of the bad access
342 * @size: size of the bad access
343 * @is_write: whether the bad access is a write or a read
344 * @ip: instruction pointer for the accessibility check or the bad access itself
346 bool kasan_report(const void *addr, size_t size,
347 bool is_write, unsigned long ip);
349 #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
351 static inline void *kasan_reset_tag(const void *addr)
356 #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS*/
358 #ifdef CONFIG_KASAN_HW_TAGS
360 void kasan_report_async(void);
362 #endif /* CONFIG_KASAN_HW_TAGS */
364 #ifdef CONFIG_KASAN_SW_TAGS
365 void __init kasan_init_sw_tags(void);
367 static inline void kasan_init_sw_tags(void) { }
370 #ifdef CONFIG_KASAN_HW_TAGS
371 void kasan_init_hw_tags_cpu(void);
372 void __init kasan_init_hw_tags(void);
374 static inline void kasan_init_hw_tags_cpu(void) { }
375 static inline void kasan_init_hw_tags(void) { }
378 #ifdef CONFIG_KASAN_VMALLOC
380 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
382 void kasan_populate_early_vm_area_shadow(void *start, unsigned long size);
383 int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
384 void kasan_release_vmalloc(unsigned long start, unsigned long end,
385 unsigned long free_region_start,
386 unsigned long free_region_end);
388 #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
390 static inline void kasan_populate_early_vm_area_shadow(void *start,
393 static inline int kasan_populate_vmalloc(unsigned long start,
398 static inline void kasan_release_vmalloc(unsigned long start,
400 unsigned long free_region_start,
401 unsigned long free_region_end) { }
403 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
405 void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
406 kasan_vmalloc_flags_t flags);
407 static __always_inline void *kasan_unpoison_vmalloc(const void *start,
409 kasan_vmalloc_flags_t flags)
412 return __kasan_unpoison_vmalloc(start, size, flags);
413 return (void *)start;
416 void __kasan_poison_vmalloc(const void *start, unsigned long size);
417 static __always_inline void kasan_poison_vmalloc(const void *start,
421 __kasan_poison_vmalloc(start, size);
424 #else /* CONFIG_KASAN_VMALLOC */
426 static inline void kasan_populate_early_vm_area_shadow(void *start,
427 unsigned long size) { }
428 static inline int kasan_populate_vmalloc(unsigned long start,
433 static inline void kasan_release_vmalloc(unsigned long start,
435 unsigned long free_region_start,
436 unsigned long free_region_end) { }
438 static inline void *kasan_unpoison_vmalloc(const void *start,
440 kasan_vmalloc_flags_t flags)
442 return (void *)start;
444 static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
447 #endif /* CONFIG_KASAN_VMALLOC */
449 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
450 !defined(CONFIG_KASAN_VMALLOC)
453 * These functions allocate and free shadow memory for kernel modules.
454 * They are only required when KASAN_VMALLOC is not supported, as otherwise
455 * shadow memory is allocated by the generic vmalloc handlers.
457 int kasan_alloc_module_shadow(void *addr, size_t size, gfp_t gfp_mask);
458 void kasan_free_module_shadow(const struct vm_struct *vm);
460 #else /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
462 static inline int kasan_alloc_module_shadow(void *addr, size_t size, gfp_t gfp_mask) { return 0; }
463 static inline void kasan_free_module_shadow(const struct vm_struct *vm) {}
465 #endif /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
467 #ifdef CONFIG_KASAN_INLINE
468 void kasan_non_canonical_hook(unsigned long addr);
469 #else /* CONFIG_KASAN_INLINE */
470 static inline void kasan_non_canonical_hook(unsigned long addr) { }
471 #endif /* CONFIG_KASAN_INLINE */
473 #endif /* LINUX_KASAN_H */