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 0x00u
27 #define KASAN_VMALLOC_INIT 0x01u
28 #define KASAN_VMALLOC_VM_ALLOC 0x02u
29 #define KASAN_VMALLOC_PROT_NORMAL 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();
101 int alloc_meta_offset;
102 int free_meta_offset;
106 slab_flags_t __kasan_never_merge(void);
107 static __always_inline slab_flags_t kasan_never_merge(void)
110 return __kasan_never_merge();
114 void __kasan_unpoison_range(const void *addr, size_t size);
115 static __always_inline void kasan_unpoison_range(const void *addr, size_t size)
118 __kasan_unpoison_range(addr, size);
121 void __kasan_poison_pages(struct page *page, unsigned int order, bool init);
122 static __always_inline void kasan_poison_pages(struct page *page,
123 unsigned int order, bool init)
126 __kasan_poison_pages(page, order, init);
129 void __kasan_unpoison_pages(struct page *page, unsigned int order, bool init);
130 static __always_inline void kasan_unpoison_pages(struct page *page,
131 unsigned int order, bool init)
134 __kasan_unpoison_pages(page, order, init);
137 void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
138 slab_flags_t *flags);
139 static __always_inline void kasan_cache_create(struct kmem_cache *cache,
140 unsigned int *size, slab_flags_t *flags)
143 __kasan_cache_create(cache, size, flags);
146 void __kasan_cache_create_kmalloc(struct kmem_cache *cache);
147 static __always_inline void kasan_cache_create_kmalloc(struct kmem_cache *cache)
150 __kasan_cache_create_kmalloc(cache);
153 size_t __kasan_metadata_size(struct kmem_cache *cache);
154 static __always_inline size_t kasan_metadata_size(struct kmem_cache *cache)
157 return __kasan_metadata_size(cache);
161 void __kasan_poison_slab(struct slab *slab);
162 static __always_inline void kasan_poison_slab(struct slab *slab)
165 __kasan_poison_slab(slab);
168 void __kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
169 static __always_inline void kasan_unpoison_object_data(struct kmem_cache *cache,
173 __kasan_unpoison_object_data(cache, object);
176 void __kasan_poison_object_data(struct kmem_cache *cache, void *object);
177 static __always_inline void kasan_poison_object_data(struct kmem_cache *cache,
181 __kasan_poison_object_data(cache, object);
184 void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache,
186 static __always_inline void * __must_check kasan_init_slab_obj(
187 struct kmem_cache *cache, const void *object)
190 return __kasan_init_slab_obj(cache, object);
191 return (void *)object;
194 bool __kasan_slab_free(struct kmem_cache *s, void *object,
195 unsigned long ip, bool init);
196 static __always_inline bool kasan_slab_free(struct kmem_cache *s,
197 void *object, bool init)
200 return __kasan_slab_free(s, object, _RET_IP_, init);
204 void __kasan_kfree_large(void *ptr, unsigned long ip);
205 static __always_inline void kasan_kfree_large(void *ptr)
208 __kasan_kfree_large(ptr, _RET_IP_);
211 void __kasan_slab_free_mempool(void *ptr, unsigned long ip);
212 static __always_inline void kasan_slab_free_mempool(void *ptr)
215 __kasan_slab_free_mempool(ptr, _RET_IP_);
218 void * __must_check __kasan_slab_alloc(struct kmem_cache *s,
219 void *object, gfp_t flags, bool init);
220 static __always_inline void * __must_check kasan_slab_alloc(
221 struct kmem_cache *s, void *object, gfp_t flags, bool init)
224 return __kasan_slab_alloc(s, object, flags, init);
228 void * __must_check __kasan_kmalloc(struct kmem_cache *s, const void *object,
229 size_t size, gfp_t flags);
230 static __always_inline void * __must_check kasan_kmalloc(struct kmem_cache *s,
231 const void *object, size_t size, gfp_t flags)
234 return __kasan_kmalloc(s, object, size, flags);
235 return (void *)object;
238 void * __must_check __kasan_kmalloc_large(const void *ptr,
239 size_t size, gfp_t flags);
240 static __always_inline void * __must_check kasan_kmalloc_large(const void *ptr,
241 size_t size, gfp_t flags)
244 return __kasan_kmalloc_large(ptr, size, flags);
248 void * __must_check __kasan_krealloc(const void *object,
249 size_t new_size, gfp_t flags);
250 static __always_inline void * __must_check kasan_krealloc(const void *object,
251 size_t new_size, gfp_t flags)
254 return __kasan_krealloc(object, new_size, flags);
255 return (void *)object;
259 * Unlike kasan_check_read/write(), kasan_check_byte() is performed even for
260 * the hardware tag-based mode that doesn't rely on compiler instrumentation.
262 bool __kasan_check_byte(const void *addr, unsigned long ip);
263 static __always_inline bool kasan_check_byte(const void *addr)
266 return __kasan_check_byte(addr, _RET_IP_);
270 #else /* CONFIG_KASAN */
272 static inline slab_flags_t kasan_never_merge(void)
276 static inline void kasan_unpoison_range(const void *address, size_t size) {}
277 static inline void kasan_poison_pages(struct page *page, unsigned int order,
279 static inline void kasan_unpoison_pages(struct page *page, unsigned int order,
281 static inline void kasan_cache_create(struct kmem_cache *cache,
283 slab_flags_t *flags) {}
284 static inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) {}
285 static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
286 static inline void kasan_poison_slab(struct slab *slab) {}
287 static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
289 static inline void kasan_poison_object_data(struct kmem_cache *cache,
291 static inline void *kasan_init_slab_obj(struct kmem_cache *cache,
294 return (void *)object;
296 static inline bool kasan_slab_free(struct kmem_cache *s, void *object, bool init)
300 static inline void kasan_kfree_large(void *ptr) {}
301 static inline void kasan_slab_free_mempool(void *ptr) {}
302 static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object,
303 gfp_t flags, bool init)
307 static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object,
308 size_t size, gfp_t flags)
310 return (void *)object;
312 static inline void *kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags)
316 static inline void *kasan_krealloc(const void *object, size_t new_size,
319 return (void *)object;
321 static inline bool kasan_check_byte(const void *address)
326 #endif /* CONFIG_KASAN */
328 #if defined(CONFIG_KASAN) && defined(CONFIG_KASAN_STACK)
329 void kasan_unpoison_task_stack(struct task_struct *task);
331 static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
334 #ifdef CONFIG_KASAN_GENERIC
336 void kasan_cache_shrink(struct kmem_cache *cache);
337 void kasan_cache_shutdown(struct kmem_cache *cache);
338 void kasan_record_aux_stack(void *ptr);
339 void kasan_record_aux_stack_noalloc(void *ptr);
341 #else /* CONFIG_KASAN_GENERIC */
343 static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
344 static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
345 static inline void kasan_record_aux_stack(void *ptr) {}
346 static inline void kasan_record_aux_stack_noalloc(void *ptr) {}
348 #endif /* CONFIG_KASAN_GENERIC */
350 #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
352 static inline void *kasan_reset_tag(const void *addr)
354 return (void *)arch_kasan_reset_tag(addr);
358 * kasan_report - print a report about a bad memory access detected by KASAN
359 * @addr: address of the bad access
360 * @size: size of the bad access
361 * @is_write: whether the bad access is a write or a read
362 * @ip: instruction pointer for the accessibility check or the bad access itself
364 bool kasan_report(unsigned long addr, size_t size,
365 bool is_write, unsigned long ip);
367 #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
369 static inline void *kasan_reset_tag(const void *addr)
374 #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS*/
376 #ifdef CONFIG_KASAN_HW_TAGS
378 void kasan_report_async(void);
380 #endif /* CONFIG_KASAN_HW_TAGS */
382 #ifdef CONFIG_KASAN_SW_TAGS
383 void __init kasan_init_sw_tags(void);
385 static inline void kasan_init_sw_tags(void) { }
388 #ifdef CONFIG_KASAN_HW_TAGS
389 void kasan_init_hw_tags_cpu(void);
390 void __init kasan_init_hw_tags(void);
392 static inline void kasan_init_hw_tags_cpu(void) { }
393 static inline void kasan_init_hw_tags(void) { }
396 #ifdef CONFIG_KASAN_VMALLOC
398 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
400 void kasan_populate_early_vm_area_shadow(void *start, unsigned long size);
401 int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
402 void kasan_release_vmalloc(unsigned long start, unsigned long end,
403 unsigned long free_region_start,
404 unsigned long free_region_end);
406 #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
408 static inline void kasan_populate_early_vm_area_shadow(void *start,
411 static inline int kasan_populate_vmalloc(unsigned long start,
416 static inline void kasan_release_vmalloc(unsigned long start,
418 unsigned long free_region_start,
419 unsigned long free_region_end) { }
421 #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
423 void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
424 kasan_vmalloc_flags_t flags);
425 static __always_inline void *kasan_unpoison_vmalloc(const void *start,
427 kasan_vmalloc_flags_t flags)
430 return __kasan_unpoison_vmalloc(start, size, flags);
431 return (void *)start;
434 void __kasan_poison_vmalloc(const void *start, unsigned long size);
435 static __always_inline void kasan_poison_vmalloc(const void *start,
439 __kasan_poison_vmalloc(start, size);
442 #else /* CONFIG_KASAN_VMALLOC */
444 static inline void kasan_populate_early_vm_area_shadow(void *start,
445 unsigned long size) { }
446 static inline int kasan_populate_vmalloc(unsigned long start,
451 static inline void kasan_release_vmalloc(unsigned long start,
453 unsigned long free_region_start,
454 unsigned long free_region_end) { }
456 static inline void *kasan_unpoison_vmalloc(const void *start,
458 kasan_vmalloc_flags_t flags)
460 return (void *)start;
462 static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
465 #endif /* CONFIG_KASAN_VMALLOC */
467 #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
468 !defined(CONFIG_KASAN_VMALLOC)
471 * These functions allocate and free shadow memory for kernel modules.
472 * They are only required when KASAN_VMALLOC is not supported, as otherwise
473 * shadow memory is allocated by the generic vmalloc handlers.
475 int kasan_alloc_module_shadow(void *addr, size_t size, gfp_t gfp_mask);
476 void kasan_free_module_shadow(const struct vm_struct *vm);
478 #else /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
480 static inline int kasan_alloc_module_shadow(void *addr, size_t size, gfp_t gfp_mask) { return 0; }
481 static inline void kasan_free_module_shadow(const struct vm_struct *vm) {}
483 #endif /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
485 #ifdef CONFIG_KASAN_INLINE
486 void kasan_non_canonical_hook(unsigned long addr);
487 #else /* CONFIG_KASAN_INLINE */
488 static inline void kasan_non_canonical_hook(unsigned long addr) { }
489 #endif /* CONFIG_KASAN_INLINE */
491 #endif /* LINUX_KASAN_H */