xref: /openbmc/linux/include/linux/kasan.h (revision 26f21f3a)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
20b24beccSAndrey Ryabinin #ifndef _LINUX_KASAN_H
30b24beccSAndrey Ryabinin #define _LINUX_KASAN_H
40b24beccSAndrey Ryabinin 
57a3b8353SPeter Collingbourne #include <linux/bug.h>
6f9b5e46fSPeter Collingbourne #include <linux/kasan-enabled.h>
72db710ccSMarco Elver #include <linux/kernel.h>
834303244SAndrey Konovalov #include <linux/static_key.h>
90b24beccSAndrey Ryabinin #include <linux/types.h>
100b24beccSAndrey Ryabinin 
110b24beccSAndrey Ryabinin struct kmem_cache;
120b24beccSAndrey Ryabinin struct page;
136e48a966SMatthew Wilcox (Oracle) struct slab;
14a5af5aa8SAndrey Ryabinin struct vm_struct;
155be9b730SMasami Hiramatsu struct task_struct;
160b24beccSAndrey Ryabinin 
170b24beccSAndrey Ryabinin #ifdef CONFIG_KASAN
180b24beccSAndrey Ryabinin 
19d5750edfSAndrey Konovalov #include <linux/linkage.h>
2065fddcfcSMike Rapoport #include <asm/kasan.h>
210b24beccSAndrey Ryabinin 
22d5750edfSAndrey Konovalov #endif
23d5750edfSAndrey Konovalov 
2423689e91SAndrey Konovalov typedef unsigned int __bitwise kasan_vmalloc_flags_t;
2523689e91SAndrey Konovalov 
26ec2a0f9cSAndrey Konovalov #define KASAN_VMALLOC_NONE		((__force kasan_vmalloc_flags_t)0x00u)
27ec2a0f9cSAndrey Konovalov #define KASAN_VMALLOC_INIT		((__force kasan_vmalloc_flags_t)0x01u)
28ec2a0f9cSAndrey Konovalov #define KASAN_VMALLOC_VM_ALLOC		((__force kasan_vmalloc_flags_t)0x02u)
29ec2a0f9cSAndrey Konovalov #define KASAN_VMALLOC_PROT_NORMAL	((__force kasan_vmalloc_flags_t)0x04u)
3023689e91SAndrey Konovalov 
31d5750edfSAndrey Konovalov #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
32d5750edfSAndrey Konovalov 
33d5750edfSAndrey Konovalov #include <linux/pgtable.h>
34d5750edfSAndrey Konovalov 
35d5750edfSAndrey Konovalov /* Software KASAN implementations use shadow memory. */
36d5750edfSAndrey Konovalov 
37d5750edfSAndrey Konovalov #ifdef CONFIG_KASAN_SW_TAGS
38a064cb00SAndrey Konovalov /* This matches KASAN_TAG_INVALID. */
39a064cb00SAndrey Konovalov #define KASAN_SHADOW_INIT 0xFE
40d5750edfSAndrey Konovalov #else
41d5750edfSAndrey Konovalov #define KASAN_SHADOW_INIT 0
42d5750edfSAndrey Konovalov #endif
43d5750edfSAndrey Konovalov 
4429970dc2SHailong Liu #ifndef PTE_HWTABLE_PTRS
4529970dc2SHailong Liu #define PTE_HWTABLE_PTRS 0
4629970dc2SHailong Liu #endif
4729970dc2SHailong Liu 
489577dd74SAndrey Konovalov extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
49cb32c9c5SDaniel Axtens extern pte_t kasan_early_shadow_pte[MAX_PTRS_PER_PTE + PTE_HWTABLE_PTRS];
50cb32c9c5SDaniel Axtens extern pmd_t kasan_early_shadow_pmd[MAX_PTRS_PER_PMD];
51cb32c9c5SDaniel Axtens extern pud_t kasan_early_shadow_pud[MAX_PTRS_PER_PUD];
529577dd74SAndrey Konovalov extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
5369786cdbSAndrey Ryabinin 
549577dd74SAndrey Konovalov int kasan_populate_early_shadow(const void *shadow_start,
5569786cdbSAndrey Ryabinin 				const void *shadow_end);
5669786cdbSAndrey Ryabinin 
570b24beccSAndrey Ryabinin static inline void *kasan_mem_to_shadow(const void *addr)
580b24beccSAndrey Ryabinin {
590b24beccSAndrey Ryabinin 	return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
600b24beccSAndrey Ryabinin 		+ KASAN_SHADOW_OFFSET;
610b24beccSAndrey Ryabinin }
620b24beccSAndrey Ryabinin 
63d5750edfSAndrey Konovalov int kasan_add_zero_shadow(void *start, unsigned long size);
64d5750edfSAndrey Konovalov void kasan_remove_zero_shadow(void *start, unsigned long size);
65d5750edfSAndrey Konovalov 
66d73b4936SAndrey Konovalov /* Enable reporting bugs after kasan_disable_current() */
67d73b4936SAndrey Konovalov extern void kasan_enable_current(void);
68d73b4936SAndrey Konovalov 
69d73b4936SAndrey Konovalov /* Disable reporting bugs for current task */
70d73b4936SAndrey Konovalov extern void kasan_disable_current(void);
71d73b4936SAndrey Konovalov 
72d5750edfSAndrey Konovalov #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
73d5750edfSAndrey Konovalov 
74d5750edfSAndrey Konovalov static inline int kasan_add_zero_shadow(void *start, unsigned long size)
75d5750edfSAndrey Konovalov {
76d5750edfSAndrey Konovalov 	return 0;
77d5750edfSAndrey Konovalov }
78d5750edfSAndrey Konovalov static inline void kasan_remove_zero_shadow(void *start,
79d5750edfSAndrey Konovalov 					unsigned long size)
80d5750edfSAndrey Konovalov {}
81d5750edfSAndrey Konovalov 
82d73b4936SAndrey Konovalov static inline void kasan_enable_current(void) {}
83d73b4936SAndrey Konovalov static inline void kasan_disable_current(void) {}
84d73b4936SAndrey Konovalov 
85d5750edfSAndrey Konovalov #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
86d5750edfSAndrey Konovalov 
8734303244SAndrey Konovalov #ifdef CONFIG_KASAN_HW_TAGS
88e86f8b09SAndrey Konovalov 
89e86f8b09SAndrey Konovalov #else /* CONFIG_KASAN_HW_TAGS */
90e86f8b09SAndrey Konovalov 
91e86f8b09SAndrey Konovalov #endif /* CONFIG_KASAN_HW_TAGS */
92e86f8b09SAndrey Konovalov 
93e5af50a5SPeter Collingbourne static inline bool kasan_has_integrated_init(void)
94e5af50a5SPeter Collingbourne {
95e5af50a5SPeter Collingbourne 	return kasan_hw_tags_enabled();
96e5af50a5SPeter Collingbourne }
97e5af50a5SPeter Collingbourne 
987a3b8353SPeter Collingbourne #ifdef CONFIG_KASAN
997a3b8353SPeter Collingbourne 
1007a3b8353SPeter Collingbourne struct kasan_cache {
101*26f21f3aSAndrey Konovalov #ifdef CONFIG_KASAN_GENERIC
1027a3b8353SPeter Collingbourne 	int alloc_meta_offset;
1037a3b8353SPeter Collingbourne 	int free_meta_offset;
104*26f21f3aSAndrey Konovalov #endif
1057a3b8353SPeter Collingbourne 	bool is_kmalloc;
1067a3b8353SPeter Collingbourne };
1077a3b8353SPeter Collingbourne 
10834303244SAndrey Konovalov void __kasan_unpoison_range(const void *addr, size_t size);
10934303244SAndrey Konovalov static __always_inline void kasan_unpoison_range(const void *addr, size_t size)
11034303244SAndrey Konovalov {
11134303244SAndrey Konovalov 	if (kasan_enabled())
11234303244SAndrey Konovalov 		__kasan_unpoison_range(addr, size);
11334303244SAndrey Konovalov }
11434303244SAndrey Konovalov 
1157a3b8353SPeter Collingbourne void __kasan_poison_pages(struct page *page, unsigned int order, bool init);
1167a3b8353SPeter Collingbourne static __always_inline void kasan_poison_pages(struct page *page,
1171bb5eab3SAndrey Konovalov 						unsigned int order, bool init)
11834303244SAndrey Konovalov {
11934303244SAndrey Konovalov 	if (kasan_enabled())
1207a3b8353SPeter Collingbourne 		__kasan_poison_pages(page, order, init);
12134303244SAndrey Konovalov }
12234303244SAndrey Konovalov 
1237a3b8353SPeter Collingbourne void __kasan_unpoison_pages(struct page *page, unsigned int order, bool init);
1247a3b8353SPeter Collingbourne static __always_inline void kasan_unpoison_pages(struct page *page,
1251bb5eab3SAndrey Konovalov 						 unsigned int order, bool init)
12634303244SAndrey Konovalov {
12734303244SAndrey Konovalov 	if (kasan_enabled())
1287a3b8353SPeter Collingbourne 		__kasan_unpoison_pages(page, order, init);
12934303244SAndrey Konovalov }
13034303244SAndrey Konovalov 
13134303244SAndrey Konovalov void __kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
13234303244SAndrey Konovalov 				slab_flags_t *flags);
13334303244SAndrey Konovalov static __always_inline void kasan_cache_create(struct kmem_cache *cache,
13434303244SAndrey Konovalov 				unsigned int *size, slab_flags_t *flags)
13534303244SAndrey Konovalov {
13634303244SAndrey Konovalov 	if (kasan_enabled())
13734303244SAndrey Konovalov 		__kasan_cache_create(cache, size, flags);
13834303244SAndrey Konovalov }
13934303244SAndrey Konovalov 
14092850134SAndrey Konovalov void __kasan_cache_create_kmalloc(struct kmem_cache *cache);
14192850134SAndrey Konovalov static __always_inline void kasan_cache_create_kmalloc(struct kmem_cache *cache)
14292850134SAndrey Konovalov {
14392850134SAndrey Konovalov 	if (kasan_enabled())
14492850134SAndrey Konovalov 		__kasan_cache_create_kmalloc(cache);
14592850134SAndrey Konovalov }
14692850134SAndrey Konovalov 
1476e48a966SMatthew Wilcox (Oracle) void __kasan_poison_slab(struct slab *slab);
1486e48a966SMatthew Wilcox (Oracle) static __always_inline void kasan_poison_slab(struct slab *slab)
14934303244SAndrey Konovalov {
15034303244SAndrey Konovalov 	if (kasan_enabled())
1516e48a966SMatthew Wilcox (Oracle) 		__kasan_poison_slab(slab);
15234303244SAndrey Konovalov }
15334303244SAndrey Konovalov 
15434303244SAndrey Konovalov void __kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
15534303244SAndrey Konovalov static __always_inline void kasan_unpoison_object_data(struct kmem_cache *cache,
15634303244SAndrey Konovalov 							void *object)
15734303244SAndrey Konovalov {
15834303244SAndrey Konovalov 	if (kasan_enabled())
15934303244SAndrey Konovalov 		__kasan_unpoison_object_data(cache, object);
16034303244SAndrey Konovalov }
16134303244SAndrey Konovalov 
16234303244SAndrey Konovalov void __kasan_poison_object_data(struct kmem_cache *cache, void *object);
16334303244SAndrey Konovalov static __always_inline void kasan_poison_object_data(struct kmem_cache *cache,
16434303244SAndrey Konovalov 							void *object)
16534303244SAndrey Konovalov {
16634303244SAndrey Konovalov 	if (kasan_enabled())
16734303244SAndrey Konovalov 		__kasan_poison_object_data(cache, object);
16834303244SAndrey Konovalov }
16934303244SAndrey Konovalov 
17034303244SAndrey Konovalov void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache,
17134303244SAndrey Konovalov 					  const void *object);
17234303244SAndrey Konovalov static __always_inline void * __must_check kasan_init_slab_obj(
17334303244SAndrey Konovalov 				struct kmem_cache *cache, const void *object)
17434303244SAndrey Konovalov {
17534303244SAndrey Konovalov 	if (kasan_enabled())
17634303244SAndrey Konovalov 		return __kasan_init_slab_obj(cache, object);
17734303244SAndrey Konovalov 	return (void *)object;
17834303244SAndrey Konovalov }
17934303244SAndrey Konovalov 
180d57a964eSAndrey Konovalov bool __kasan_slab_free(struct kmem_cache *s, void *object,
181d57a964eSAndrey Konovalov 			unsigned long ip, bool init);
182d57a964eSAndrey Konovalov static __always_inline bool kasan_slab_free(struct kmem_cache *s,
183d57a964eSAndrey Konovalov 						void *object, bool init)
18434303244SAndrey Konovalov {
18534303244SAndrey Konovalov 	if (kasan_enabled())
186d57a964eSAndrey Konovalov 		return __kasan_slab_free(s, object, _RET_IP_, init);
18734303244SAndrey Konovalov 	return false;
18834303244SAndrey Konovalov }
18934303244SAndrey Konovalov 
190200072ceSAndrey Konovalov void __kasan_kfree_large(void *ptr, unsigned long ip);
191200072ceSAndrey Konovalov static __always_inline void kasan_kfree_large(void *ptr)
192200072ceSAndrey Konovalov {
193200072ceSAndrey Konovalov 	if (kasan_enabled())
194200072ceSAndrey Konovalov 		__kasan_kfree_large(ptr, _RET_IP_);
195200072ceSAndrey Konovalov }
196200072ceSAndrey Konovalov 
197eeb3160cSAndrey Konovalov void __kasan_slab_free_mempool(void *ptr, unsigned long ip);
198027b37b5SAndrey Konovalov static __always_inline void kasan_slab_free_mempool(void *ptr)
199eeb3160cSAndrey Konovalov {
200eeb3160cSAndrey Konovalov 	if (kasan_enabled())
201027b37b5SAndrey Konovalov 		__kasan_slab_free_mempool(ptr, _RET_IP_);
202eeb3160cSAndrey Konovalov }
203eeb3160cSAndrey Konovalov 
20434303244SAndrey Konovalov void * __must_check __kasan_slab_alloc(struct kmem_cache *s,
205da844b78SAndrey Konovalov 				       void *object, gfp_t flags, bool init);
20634303244SAndrey Konovalov static __always_inline void * __must_check kasan_slab_alloc(
207da844b78SAndrey Konovalov 		struct kmem_cache *s, void *object, gfp_t flags, bool init)
20834303244SAndrey Konovalov {
20934303244SAndrey Konovalov 	if (kasan_enabled())
210da844b78SAndrey Konovalov 		return __kasan_slab_alloc(s, object, flags, init);
21134303244SAndrey Konovalov 	return object;
21234303244SAndrey Konovalov }
21334303244SAndrey Konovalov 
21434303244SAndrey Konovalov void * __must_check __kasan_kmalloc(struct kmem_cache *s, const void *object,
21534303244SAndrey Konovalov 				    size_t size, gfp_t flags);
21634303244SAndrey Konovalov static __always_inline void * __must_check kasan_kmalloc(struct kmem_cache *s,
21734303244SAndrey Konovalov 				const void *object, size_t size, gfp_t flags)
21834303244SAndrey Konovalov {
21934303244SAndrey Konovalov 	if (kasan_enabled())
22034303244SAndrey Konovalov 		return __kasan_kmalloc(s, object, size, flags);
22134303244SAndrey Konovalov 	return (void *)object;
22234303244SAndrey Konovalov }
22334303244SAndrey Konovalov 
22434303244SAndrey Konovalov void * __must_check __kasan_kmalloc_large(const void *ptr,
22534303244SAndrey Konovalov 					  size_t size, gfp_t flags);
22634303244SAndrey Konovalov static __always_inline void * __must_check kasan_kmalloc_large(const void *ptr,
22734303244SAndrey Konovalov 						      size_t size, gfp_t flags)
22834303244SAndrey Konovalov {
22934303244SAndrey Konovalov 	if (kasan_enabled())
23034303244SAndrey Konovalov 		return __kasan_kmalloc_large(ptr, size, flags);
23134303244SAndrey Konovalov 	return (void *)ptr;
23234303244SAndrey Konovalov }
23334303244SAndrey Konovalov 
23434303244SAndrey Konovalov void * __must_check __kasan_krealloc(const void *object,
23534303244SAndrey Konovalov 				     size_t new_size, gfp_t flags);
23634303244SAndrey Konovalov static __always_inline void * __must_check kasan_krealloc(const void *object,
23734303244SAndrey Konovalov 						 size_t new_size, gfp_t flags)
23834303244SAndrey Konovalov {
23934303244SAndrey Konovalov 	if (kasan_enabled())
24034303244SAndrey Konovalov 		return __kasan_krealloc(object, new_size, flags);
24134303244SAndrey Konovalov 	return (void *)object;
24234303244SAndrey Konovalov }
24334303244SAndrey Konovalov 
244611806b4SAndrey Konovalov /*
245611806b4SAndrey Konovalov  * Unlike kasan_check_read/write(), kasan_check_byte() is performed even for
246611806b4SAndrey Konovalov  * the hardware tag-based mode that doesn't rely on compiler instrumentation.
247611806b4SAndrey Konovalov  */
248611806b4SAndrey Konovalov bool __kasan_check_byte(const void *addr, unsigned long ip);
249611806b4SAndrey Konovalov static __always_inline bool kasan_check_byte(const void *addr)
250611806b4SAndrey Konovalov {
251611806b4SAndrey Konovalov 	if (kasan_enabled())
252611806b4SAndrey Konovalov 		return __kasan_check_byte(addr, _RET_IP_);
253611806b4SAndrey Konovalov 	return true;
254611806b4SAndrey Konovalov }
255611806b4SAndrey Konovalov 
2560b24beccSAndrey Ryabinin #else /* CONFIG_KASAN */
2570b24beccSAndrey Ryabinin 
258cebd0eb2SAndrey Konovalov static inline void kasan_unpoison_range(const void *address, size_t size) {}
2597a3b8353SPeter Collingbourne static inline void kasan_poison_pages(struct page *page, unsigned int order,
2607a3b8353SPeter Collingbourne 				      bool init) {}
2617a3b8353SPeter Collingbourne static inline void kasan_unpoison_pages(struct page *page, unsigned int order,
2627a3b8353SPeter Collingbourne 					bool init) {}
2637ed2f9e6SAlexander Potapenko static inline void kasan_cache_create(struct kmem_cache *cache,
264be4a7988SAlexey Dobriyan 				      unsigned int *size,
265d50112edSAlexey Dobriyan 				      slab_flags_t *flags) {}
26692850134SAndrey Konovalov static inline void kasan_cache_create_kmalloc(struct kmem_cache *cache) {}
2676e48a966SMatthew Wilcox (Oracle) static inline void kasan_poison_slab(struct slab *slab) {}
2680316bec2SAndrey Ryabinin static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
2690316bec2SAndrey Ryabinin 					void *object) {}
2700316bec2SAndrey Ryabinin static inline void kasan_poison_object_data(struct kmem_cache *cache,
2710316bec2SAndrey Ryabinin 					void *object) {}
2720116523cSAndrey Konovalov static inline void *kasan_init_slab_obj(struct kmem_cache *cache,
2730116523cSAndrey Konovalov 				const void *object)
2740116523cSAndrey Konovalov {
2750116523cSAndrey Konovalov 	return (void *)object;
2760116523cSAndrey Konovalov }
277d57a964eSAndrey Konovalov static inline bool kasan_slab_free(struct kmem_cache *s, void *object, bool init)
2780116523cSAndrey Konovalov {
27934303244SAndrey Konovalov 	return false;
2800116523cSAndrey Konovalov }
281200072ceSAndrey Konovalov static inline void kasan_kfree_large(void *ptr) {}
282027b37b5SAndrey Konovalov static inline void kasan_slab_free_mempool(void *ptr) {}
28334303244SAndrey Konovalov static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object,
284da844b78SAndrey Konovalov 				   gfp_t flags, bool init)
28534303244SAndrey Konovalov {
28634303244SAndrey Konovalov 	return object;
28734303244SAndrey Konovalov }
2880116523cSAndrey Konovalov static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object,
2890116523cSAndrey Konovalov 				size_t size, gfp_t flags)
2900116523cSAndrey Konovalov {
2910116523cSAndrey Konovalov 	return (void *)object;
2920116523cSAndrey Konovalov }
29334303244SAndrey Konovalov static inline void *kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags)
29434303244SAndrey Konovalov {
29534303244SAndrey Konovalov 	return (void *)ptr;
29634303244SAndrey Konovalov }
2970116523cSAndrey Konovalov static inline void *kasan_krealloc(const void *object, size_t new_size,
2980116523cSAndrey Konovalov 				 gfp_t flags)
2990116523cSAndrey Konovalov {
3000116523cSAndrey Konovalov 	return (void *)object;
3010116523cSAndrey Konovalov }
302611806b4SAndrey Konovalov static inline bool kasan_check_byte(const void *address)
303611806b4SAndrey Konovalov {
304611806b4SAndrey Konovalov 	return true;
305611806b4SAndrey Konovalov }
3069b75a867SAndrey Ryabinin 
3070b24beccSAndrey Ryabinin #endif /* CONFIG_KASAN */
3080b24beccSAndrey Ryabinin 
30902c58773SWalter Wu #if defined(CONFIG_KASAN) && defined(CONFIG_KASAN_STACK)
310d56a9ef8SAndrey Konovalov void kasan_unpoison_task_stack(struct task_struct *task);
311d56a9ef8SAndrey Konovalov #else
312d56a9ef8SAndrey Konovalov static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
313d56a9ef8SAndrey Konovalov #endif
314d56a9ef8SAndrey Konovalov 
3152bd926b4SAndrey Konovalov #ifdef CONFIG_KASAN_GENERIC
3162bd926b4SAndrey Konovalov 
317f372bde9SAndrey Konovalov size_t kasan_metadata_size(struct kmem_cache *cache);
3183b7f8813SAndrey Konovalov slab_flags_t kasan_never_merge(void);
319f372bde9SAndrey Konovalov 
3202bd926b4SAndrey Konovalov void kasan_cache_shrink(struct kmem_cache *cache);
3212bd926b4SAndrey Konovalov void kasan_cache_shutdown(struct kmem_cache *cache);
32226e760c9SWalter Wu void kasan_record_aux_stack(void *ptr);
3237cb3007cSMarco Elver void kasan_record_aux_stack_noalloc(void *ptr);
3242bd926b4SAndrey Konovalov 
3252bd926b4SAndrey Konovalov #else /* CONFIG_KASAN_GENERIC */
3262bd926b4SAndrey Konovalov 
327f372bde9SAndrey Konovalov /* Tag-based KASAN modes do not use per-object metadata. */
328f372bde9SAndrey Konovalov static inline size_t kasan_metadata_size(struct kmem_cache *cache)
329f372bde9SAndrey Konovalov {
330f372bde9SAndrey Konovalov 	return 0;
331f372bde9SAndrey Konovalov }
3323b7f8813SAndrey Konovalov /* And thus nothing prevents cache merging. */
3333b7f8813SAndrey Konovalov static inline slab_flags_t kasan_never_merge(void)
3343b7f8813SAndrey Konovalov {
3353b7f8813SAndrey Konovalov 	return 0;
3363b7f8813SAndrey Konovalov }
337f372bde9SAndrey Konovalov 
3382bd926b4SAndrey Konovalov static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
3392bd926b4SAndrey Konovalov static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
34026e760c9SWalter Wu static inline void kasan_record_aux_stack(void *ptr) {}
3417cb3007cSMarco Elver static inline void kasan_record_aux_stack_noalloc(void *ptr) {}
3422bd926b4SAndrey Konovalov 
3432bd926b4SAndrey Konovalov #endif /* CONFIG_KASAN_GENERIC */
3442bd926b4SAndrey Konovalov 
3452e903b91SAndrey Konovalov #if defined(CONFIG_KASAN_SW_TAGS) || defined(CONFIG_KASAN_HW_TAGS)
3463c9e3aa1SAndrey Konovalov 
347c0054c56SAndrey Konovalov static inline void *kasan_reset_tag(const void *addr)
348c0054c56SAndrey Konovalov {
349c0054c56SAndrey Konovalov 	return (void *)arch_kasan_reset_tag(addr);
350c0054c56SAndrey Konovalov }
3513c9e3aa1SAndrey Konovalov 
35249c6631dSVincenzo Frascino /**
35349c6631dSVincenzo Frascino  * kasan_report - print a report about a bad memory access detected by KASAN
35449c6631dSVincenzo Frascino  * @addr: address of the bad access
35549c6631dSVincenzo Frascino  * @size: size of the bad access
35649c6631dSVincenzo Frascino  * @is_write: whether the bad access is a write or a read
35749c6631dSVincenzo Frascino  * @ip: instruction pointer for the accessibility check or the bad access itself
35849c6631dSVincenzo Frascino  */
3598cceeff4SWalter Wu bool kasan_report(unsigned long addr, size_t size,
36041eea9cdSAndrey Konovalov 		bool is_write, unsigned long ip);
36141eea9cdSAndrey Konovalov 
3622e903b91SAndrey Konovalov #else /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS */
3633c9e3aa1SAndrey Konovalov 
3643c9e3aa1SAndrey Konovalov static inline void *kasan_reset_tag(const void *addr)
3653c9e3aa1SAndrey Konovalov {
3663c9e3aa1SAndrey Konovalov 	return (void *)addr;
3673c9e3aa1SAndrey Konovalov }
3683c9e3aa1SAndrey Konovalov 
3692e903b91SAndrey Konovalov #endif /* CONFIG_KASAN_SW_TAGS || CONFIG_KASAN_HW_TAGS*/
3702e903b91SAndrey Konovalov 
3718f7b5054SVincenzo Frascino #ifdef CONFIG_KASAN_HW_TAGS
3728f7b5054SVincenzo Frascino 
3738f7b5054SVincenzo Frascino void kasan_report_async(void);
3748f7b5054SVincenzo Frascino 
3758f7b5054SVincenzo Frascino #endif /* CONFIG_KASAN_HW_TAGS */
3768f7b5054SVincenzo Frascino 
3772e903b91SAndrey Konovalov #ifdef CONFIG_KASAN_SW_TAGS
3782e903b91SAndrey Konovalov void __init kasan_init_sw_tags(void);
3792e903b91SAndrey Konovalov #else
3802e903b91SAndrey Konovalov static inline void kasan_init_sw_tags(void) { }
3812e903b91SAndrey Konovalov #endif
3822e903b91SAndrey Konovalov 
3832e903b91SAndrey Konovalov #ifdef CONFIG_KASAN_HW_TAGS
3842e903b91SAndrey Konovalov void kasan_init_hw_tags_cpu(void);
3852e903b91SAndrey Konovalov void __init kasan_init_hw_tags(void);
3862e903b91SAndrey Konovalov #else
3872e903b91SAndrey Konovalov static inline void kasan_init_hw_tags_cpu(void) { }
3882e903b91SAndrey Konovalov static inline void kasan_init_hw_tags(void) { }
3892e903b91SAndrey Konovalov #endif
390080eb83fSAndrey Konovalov 
3913c5c3cfbSDaniel Axtens #ifdef CONFIG_KASAN_VMALLOC
3923b1a4a86SAndrey Konovalov 
39323689e91SAndrey Konovalov #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
39423689e91SAndrey Konovalov 
3955bd9bae2SAndrey Konovalov void kasan_populate_early_vm_area_shadow(void *start, unsigned long size);
396d98c9e83SAndrey Ryabinin int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
3973c5c3cfbSDaniel Axtens void kasan_release_vmalloc(unsigned long start, unsigned long end,
3983c5c3cfbSDaniel Axtens 			   unsigned long free_region_start,
3993c5c3cfbSDaniel Axtens 			   unsigned long free_region_end);
4003b1a4a86SAndrey Konovalov 
40123689e91SAndrey Konovalov #else /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
40223689e91SAndrey Konovalov 
40323689e91SAndrey Konovalov static inline void kasan_populate_early_vm_area_shadow(void *start,
40423689e91SAndrey Konovalov 						       unsigned long size)
40523689e91SAndrey Konovalov { }
40623689e91SAndrey Konovalov static inline int kasan_populate_vmalloc(unsigned long start,
407579fb0acSAndrey Konovalov 					unsigned long size)
408579fb0acSAndrey Konovalov {
40923689e91SAndrey Konovalov 	return 0;
41023689e91SAndrey Konovalov }
41123689e91SAndrey Konovalov static inline void kasan_release_vmalloc(unsigned long start,
41223689e91SAndrey Konovalov 					 unsigned long end,
41323689e91SAndrey Konovalov 					 unsigned long free_region_start,
41423689e91SAndrey Konovalov 					 unsigned long free_region_end) { }
41523689e91SAndrey Konovalov 
41623689e91SAndrey Konovalov #endif /* CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS */
41723689e91SAndrey Konovalov 
41823689e91SAndrey Konovalov void *__kasan_unpoison_vmalloc(const void *start, unsigned long size,
41923689e91SAndrey Konovalov 			       kasan_vmalloc_flags_t flags);
42023689e91SAndrey Konovalov static __always_inline void *kasan_unpoison_vmalloc(const void *start,
42123689e91SAndrey Konovalov 						unsigned long size,
42223689e91SAndrey Konovalov 						kasan_vmalloc_flags_t flags)
42323689e91SAndrey Konovalov {
424579fb0acSAndrey Konovalov 	if (kasan_enabled())
42523689e91SAndrey Konovalov 		return __kasan_unpoison_vmalloc(start, size, flags);
4261d96320fSAndrey Konovalov 	return (void *)start;
427579fb0acSAndrey Konovalov }
428579fb0acSAndrey Konovalov 
429579fb0acSAndrey Konovalov void __kasan_poison_vmalloc(const void *start, unsigned long size);
430579fb0acSAndrey Konovalov static __always_inline void kasan_poison_vmalloc(const void *start,
431579fb0acSAndrey Konovalov 						 unsigned long size)
432579fb0acSAndrey Konovalov {
433579fb0acSAndrey Konovalov 	if (kasan_enabled())
434579fb0acSAndrey Konovalov 		__kasan_poison_vmalloc(start, size);
435579fb0acSAndrey Konovalov }
4363252b1d8SKefeng Wang 
4373b1a4a86SAndrey Konovalov #else /* CONFIG_KASAN_VMALLOC */
4383b1a4a86SAndrey Konovalov 
4395bd9bae2SAndrey Konovalov static inline void kasan_populate_early_vm_area_shadow(void *start,
4405bd9bae2SAndrey Konovalov 						       unsigned long size) { }
441d98c9e83SAndrey Ryabinin static inline int kasan_populate_vmalloc(unsigned long start,
442d98c9e83SAndrey Ryabinin 					unsigned long size)
4433c5c3cfbSDaniel Axtens {
4443c5c3cfbSDaniel Axtens 	return 0;
4453c5c3cfbSDaniel Axtens }
4463c5c3cfbSDaniel Axtens static inline void kasan_release_vmalloc(unsigned long start,
4473c5c3cfbSDaniel Axtens 					 unsigned long end,
4483c5c3cfbSDaniel Axtens 					 unsigned long free_region_start,
4493c5c3cfbSDaniel Axtens 					 unsigned long free_region_end) { }
4503b1a4a86SAndrey Konovalov 
4511d96320fSAndrey Konovalov static inline void *kasan_unpoison_vmalloc(const void *start,
45223689e91SAndrey Konovalov 					   unsigned long size,
45323689e91SAndrey Konovalov 					   kasan_vmalloc_flags_t flags)
4541d96320fSAndrey Konovalov {
4551d96320fSAndrey Konovalov 	return (void *)start;
4561d96320fSAndrey Konovalov }
4575bd9bae2SAndrey Konovalov static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
4583252b1d8SKefeng Wang { }
4593252b1d8SKefeng Wang 
4603b1a4a86SAndrey Konovalov #endif /* CONFIG_KASAN_VMALLOC */
4613b1a4a86SAndrey Konovalov 
4620fea6e9aSAndrey Konovalov #if (defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)) && \
4630fea6e9aSAndrey Konovalov 		!defined(CONFIG_KASAN_VMALLOC)
4643b1a4a86SAndrey Konovalov 
4653b1a4a86SAndrey Konovalov /*
46663840de2SAndrey Konovalov  * These functions allocate and free shadow memory for kernel modules.
46763840de2SAndrey Konovalov  * They are only required when KASAN_VMALLOC is not supported, as otherwise
46863840de2SAndrey Konovalov  * shadow memory is allocated by the generic vmalloc handlers.
4693b1a4a86SAndrey Konovalov  */
47063840de2SAndrey Konovalov int kasan_alloc_module_shadow(void *addr, size_t size, gfp_t gfp_mask);
47163840de2SAndrey Konovalov void kasan_free_module_shadow(const struct vm_struct *vm);
4723b1a4a86SAndrey Konovalov 
4730fea6e9aSAndrey Konovalov #else /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
4743b1a4a86SAndrey Konovalov 
47563840de2SAndrey Konovalov static inline int kasan_alloc_module_shadow(void *addr, size_t size, gfp_t gfp_mask) { return 0; }
47663840de2SAndrey Konovalov static inline void kasan_free_module_shadow(const struct vm_struct *vm) {}
4773b1a4a86SAndrey Konovalov 
4780fea6e9aSAndrey Konovalov #endif /* (CONFIG_KASAN_GENERIC || CONFIG_KASAN_SW_TAGS) && !CONFIG_KASAN_VMALLOC */
4793c5c3cfbSDaniel Axtens 
4802f004eeaSJann Horn #ifdef CONFIG_KASAN_INLINE
4812f004eeaSJann Horn void kasan_non_canonical_hook(unsigned long addr);
4822f004eeaSJann Horn #else /* CONFIG_KASAN_INLINE */
4832f004eeaSJann Horn static inline void kasan_non_canonical_hook(unsigned long addr) { }
4842f004eeaSJann Horn #endif /* CONFIG_KASAN_INLINE */
4852f004eeaSJann Horn 
4860b24beccSAndrey Ryabinin #endif /* LINUX_KASAN_H */
487