xref: /openbmc/linux/include/linux/kasan.h (revision 83c4e7a0)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
20b24beccSAndrey Ryabinin #ifndef _LINUX_KASAN_H
30b24beccSAndrey Ryabinin #define _LINUX_KASAN_H
40b24beccSAndrey Ryabinin 
50b24beccSAndrey Ryabinin #include <linux/types.h>
60b24beccSAndrey Ryabinin 
70b24beccSAndrey Ryabinin struct kmem_cache;
80b24beccSAndrey Ryabinin struct page;
9a5af5aa8SAndrey Ryabinin struct vm_struct;
105be9b730SMasami Hiramatsu struct task_struct;
110b24beccSAndrey Ryabinin 
120b24beccSAndrey Ryabinin #ifdef CONFIG_KASAN
130b24beccSAndrey Ryabinin 
14ca5999fdSMike Rapoport #include <linux/pgtable.h>
1565fddcfcSMike Rapoport #include <asm/kasan.h>
160b24beccSAndrey Ryabinin 
1783c4e7a0SPatricia Alfonso /* kasan_data struct is used in KUnit tests for KASAN expected failures */
1883c4e7a0SPatricia Alfonso struct kunit_kasan_expectation {
1983c4e7a0SPatricia Alfonso 	bool report_expected;
2083c4e7a0SPatricia Alfonso 	bool report_found;
2183c4e7a0SPatricia Alfonso };
2283c4e7a0SPatricia Alfonso 
239577dd74SAndrey Konovalov extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
249577dd74SAndrey Konovalov extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
259577dd74SAndrey Konovalov extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
269577dd74SAndrey Konovalov extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD];
279577dd74SAndrey Konovalov extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
2869786cdbSAndrey Ryabinin 
299577dd74SAndrey Konovalov int kasan_populate_early_shadow(const void *shadow_start,
3069786cdbSAndrey Ryabinin 				const void *shadow_end);
3169786cdbSAndrey Ryabinin 
320b24beccSAndrey Ryabinin static inline void *kasan_mem_to_shadow(const void *addr)
330b24beccSAndrey Ryabinin {
340b24beccSAndrey Ryabinin 	return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
350b24beccSAndrey Ryabinin 		+ KASAN_SHADOW_OFFSET;
360b24beccSAndrey Ryabinin }
370b24beccSAndrey Ryabinin 
380b24beccSAndrey Ryabinin /* Enable reporting bugs after kasan_disable_current() */
39af8601adSIngo Molnar extern void kasan_enable_current(void);
400b24beccSAndrey Ryabinin 
410b24beccSAndrey Ryabinin /* Disable reporting bugs for current task */
42af8601adSIngo Molnar extern void kasan_disable_current(void);
430b24beccSAndrey Ryabinin 
440b24beccSAndrey Ryabinin void kasan_unpoison_shadow(const void *address, size_t size);
450b24beccSAndrey Ryabinin 
46e3ae1163SMark Rutland void kasan_unpoison_task_stack(struct task_struct *task);
47e3ae1163SMark Rutland 
48b8c73fc2SAndrey Ryabinin void kasan_alloc_pages(struct page *page, unsigned int order);
49b8c73fc2SAndrey Ryabinin void kasan_free_pages(struct page *page, unsigned int order);
50b8c73fc2SAndrey Ryabinin 
51be4a7988SAlexey Dobriyan void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
52d50112edSAlexey Dobriyan 			slab_flags_t *flags);
537ed2f9e6SAlexander Potapenko 
540316bec2SAndrey Ryabinin void kasan_poison_slab(struct page *page);
550316bec2SAndrey Ryabinin void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
560316bec2SAndrey Ryabinin void kasan_poison_object_data(struct kmem_cache *cache, void *object);
5766afc7f1SAndrey Konovalov void * __must_check kasan_init_slab_obj(struct kmem_cache *cache,
5866afc7f1SAndrey Konovalov 					const void *object);
590316bec2SAndrey Ryabinin 
6066afc7f1SAndrey Konovalov void * __must_check kasan_kmalloc_large(const void *ptr, size_t size,
6166afc7f1SAndrey Konovalov 						gfp_t flags);
62ee3ce779SDmitry Vyukov void kasan_kfree_large(void *ptr, unsigned long ip);
636860f634SDmitry Vyukov void kasan_poison_kfree(void *ptr, unsigned long ip);
6466afc7f1SAndrey Konovalov void * __must_check kasan_kmalloc(struct kmem_cache *s, const void *object,
6566afc7f1SAndrey Konovalov 					size_t size, gfp_t flags);
6666afc7f1SAndrey Konovalov void * __must_check kasan_krealloc(const void *object, size_t new_size,
67505f5dcbSAlexander Potapenko 					gfp_t flags);
680316bec2SAndrey Ryabinin 
6966afc7f1SAndrey Konovalov void * __must_check kasan_slab_alloc(struct kmem_cache *s, void *object,
7066afc7f1SAndrey Konovalov 					gfp_t flags);
71ee3ce779SDmitry Vyukov bool kasan_slab_free(struct kmem_cache *s, void *object, unsigned long ip);
720316bec2SAndrey Ryabinin 
737ed2f9e6SAlexander Potapenko struct kasan_cache {
747ed2f9e6SAlexander Potapenko 	int alloc_meta_offset;
757ed2f9e6SAlexander Potapenko 	int free_meta_offset;
767ed2f9e6SAlexander Potapenko };
777ed2f9e6SAlexander Potapenko 
783c5c3cfbSDaniel Axtens /*
793c5c3cfbSDaniel Axtens  * These functions provide a special case to support backing module
803c5c3cfbSDaniel Axtens  * allocations with real shadow memory. With KASAN vmalloc, the special
813c5c3cfbSDaniel Axtens  * case is unnecessary, as the work is handled in the generic case.
823c5c3cfbSDaniel Axtens  */
833c5c3cfbSDaniel Axtens #ifndef CONFIG_KASAN_VMALLOC
84bebf56a1SAndrey Ryabinin int kasan_module_alloc(void *addr, size_t size);
85a5af5aa8SAndrey Ryabinin void kasan_free_shadow(const struct vm_struct *vm);
863c5c3cfbSDaniel Axtens #else
873c5c3cfbSDaniel Axtens static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
883c5c3cfbSDaniel Axtens static inline void kasan_free_shadow(const struct vm_struct *vm) {}
893c5c3cfbSDaniel Axtens #endif
90bebf56a1SAndrey Ryabinin 
910207df4fSAndrey Ryabinin int kasan_add_zero_shadow(void *start, unsigned long size);
920207df4fSAndrey Ryabinin void kasan_remove_zero_shadow(void *start, unsigned long size);
930207df4fSAndrey Ryabinin 
940d4ca4c9SMarco Elver size_t __ksize(const void *);
950d4ca4c9SMarco Elver static inline void kasan_unpoison_slab(const void *ptr)
960d4ca4c9SMarco Elver {
970d4ca4c9SMarco Elver 	kasan_unpoison_shadow(ptr, __ksize(ptr));
980d4ca4c9SMarco Elver }
9980a9201aSAlexander Potapenko size_t kasan_metadata_size(struct kmem_cache *cache);
1009b75a867SAndrey Ryabinin 
101b0845ce5SMark Rutland bool kasan_save_enable_multi_shot(void);
102b0845ce5SMark Rutland void kasan_restore_multi_shot(bool enabled);
103b0845ce5SMark Rutland 
1040b24beccSAndrey Ryabinin #else /* CONFIG_KASAN */
1050b24beccSAndrey Ryabinin 
1060b24beccSAndrey Ryabinin static inline void kasan_unpoison_shadow(const void *address, size_t size) {}
1070b24beccSAndrey Ryabinin 
108e3ae1163SMark Rutland static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
109e3ae1163SMark Rutland 
1100b24beccSAndrey Ryabinin static inline void kasan_enable_current(void) {}
1110b24beccSAndrey Ryabinin static inline void kasan_disable_current(void) {}
1120b24beccSAndrey Ryabinin 
113b8c73fc2SAndrey Ryabinin static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
114b8c73fc2SAndrey Ryabinin static inline void kasan_free_pages(struct page *page, unsigned int order) {}
115b8c73fc2SAndrey Ryabinin 
1167ed2f9e6SAlexander Potapenko static inline void kasan_cache_create(struct kmem_cache *cache,
117be4a7988SAlexey Dobriyan 				      unsigned int *size,
118d50112edSAlexey Dobriyan 				      slab_flags_t *flags) {}
1197ed2f9e6SAlexander Potapenko 
1200316bec2SAndrey Ryabinin static inline void kasan_poison_slab(struct page *page) {}
1210316bec2SAndrey Ryabinin static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
1220316bec2SAndrey Ryabinin 					void *object) {}
1230316bec2SAndrey Ryabinin static inline void kasan_poison_object_data(struct kmem_cache *cache,
1240316bec2SAndrey Ryabinin 					void *object) {}
1250116523cSAndrey Konovalov static inline void *kasan_init_slab_obj(struct kmem_cache *cache,
1260116523cSAndrey Konovalov 				const void *object)
1270116523cSAndrey Konovalov {
1280116523cSAndrey Konovalov 	return (void *)object;
1290116523cSAndrey Konovalov }
1300316bec2SAndrey Ryabinin 
1310116523cSAndrey Konovalov static inline void *kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags)
1320116523cSAndrey Konovalov {
1330116523cSAndrey Konovalov 	return ptr;
1340116523cSAndrey Konovalov }
135ee3ce779SDmitry Vyukov static inline void kasan_kfree_large(void *ptr, unsigned long ip) {}
1366860f634SDmitry Vyukov static inline void kasan_poison_kfree(void *ptr, unsigned long ip) {}
1370116523cSAndrey Konovalov static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object,
1380116523cSAndrey Konovalov 				size_t size, gfp_t flags)
1390116523cSAndrey Konovalov {
1400116523cSAndrey Konovalov 	return (void *)object;
1410116523cSAndrey Konovalov }
1420116523cSAndrey Konovalov static inline void *kasan_krealloc(const void *object, size_t new_size,
1430116523cSAndrey Konovalov 				 gfp_t flags)
1440116523cSAndrey Konovalov {
1450116523cSAndrey Konovalov 	return (void *)object;
1460116523cSAndrey Konovalov }
1470316bec2SAndrey Ryabinin 
1480116523cSAndrey Konovalov static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object,
1490116523cSAndrey Konovalov 				   gfp_t flags)
1500116523cSAndrey Konovalov {
1510116523cSAndrey Konovalov 	return object;
1520116523cSAndrey Konovalov }
153ee3ce779SDmitry Vyukov static inline bool kasan_slab_free(struct kmem_cache *s, void *object,
154ee3ce779SDmitry Vyukov 				   unsigned long ip)
15555834c59SAlexander Potapenko {
15655834c59SAlexander Potapenko 	return false;
15755834c59SAlexander Potapenko }
1580316bec2SAndrey Ryabinin 
159bebf56a1SAndrey Ryabinin static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
160a5af5aa8SAndrey Ryabinin static inline void kasan_free_shadow(const struct vm_struct *vm) {}
161bebf56a1SAndrey Ryabinin 
1620207df4fSAndrey Ryabinin static inline int kasan_add_zero_shadow(void *start, unsigned long size)
1630207df4fSAndrey Ryabinin {
1640207df4fSAndrey Ryabinin 	return 0;
1650207df4fSAndrey Ryabinin }
1660207df4fSAndrey Ryabinin static inline void kasan_remove_zero_shadow(void *start,
1670207df4fSAndrey Ryabinin 					unsigned long size)
1680207df4fSAndrey Ryabinin {}
1690207df4fSAndrey Ryabinin 
1709b75a867SAndrey Ryabinin static inline void kasan_unpoison_slab(const void *ptr) { }
17180a9201aSAlexander Potapenko static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
1729b75a867SAndrey Ryabinin 
1730b24beccSAndrey Ryabinin #endif /* CONFIG_KASAN */
1740b24beccSAndrey Ryabinin 
1752bd926b4SAndrey Konovalov #ifdef CONFIG_KASAN_GENERIC
1762bd926b4SAndrey Konovalov 
177080eb83fSAndrey Konovalov #define KASAN_SHADOW_INIT 0
178080eb83fSAndrey Konovalov 
1792bd926b4SAndrey Konovalov void kasan_cache_shrink(struct kmem_cache *cache);
1802bd926b4SAndrey Konovalov void kasan_cache_shutdown(struct kmem_cache *cache);
18126e760c9SWalter Wu void kasan_record_aux_stack(void *ptr);
1822bd926b4SAndrey Konovalov 
1832bd926b4SAndrey Konovalov #else /* CONFIG_KASAN_GENERIC */
1842bd926b4SAndrey Konovalov 
1852bd926b4SAndrey Konovalov static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
1862bd926b4SAndrey Konovalov static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
18726e760c9SWalter Wu static inline void kasan_record_aux_stack(void *ptr) {}
1882bd926b4SAndrey Konovalov 
1892bd926b4SAndrey Konovalov #endif /* CONFIG_KASAN_GENERIC */
1902bd926b4SAndrey Konovalov 
191080eb83fSAndrey Konovalov #ifdef CONFIG_KASAN_SW_TAGS
192080eb83fSAndrey Konovalov 
193080eb83fSAndrey Konovalov #define KASAN_SHADOW_INIT 0xFF
194080eb83fSAndrey Konovalov 
1953c9e3aa1SAndrey Konovalov void kasan_init_tags(void);
1963c9e3aa1SAndrey Konovalov 
1973c9e3aa1SAndrey Konovalov void *kasan_reset_tag(const void *addr);
1983c9e3aa1SAndrey Konovalov 
1998cceeff4SWalter Wu bool kasan_report(unsigned long addr, size_t size,
20041eea9cdSAndrey Konovalov 		bool is_write, unsigned long ip);
20141eea9cdSAndrey Konovalov 
2023c9e3aa1SAndrey Konovalov #else /* CONFIG_KASAN_SW_TAGS */
2033c9e3aa1SAndrey Konovalov 
2043c9e3aa1SAndrey Konovalov static inline void kasan_init_tags(void) { }
2053c9e3aa1SAndrey Konovalov 
2063c9e3aa1SAndrey Konovalov static inline void *kasan_reset_tag(const void *addr)
2073c9e3aa1SAndrey Konovalov {
2083c9e3aa1SAndrey Konovalov 	return (void *)addr;
2093c9e3aa1SAndrey Konovalov }
2103c9e3aa1SAndrey Konovalov 
211080eb83fSAndrey Konovalov #endif /* CONFIG_KASAN_SW_TAGS */
212080eb83fSAndrey Konovalov 
2133c5c3cfbSDaniel Axtens #ifdef CONFIG_KASAN_VMALLOC
214d98c9e83SAndrey Ryabinin int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
215d98c9e83SAndrey Ryabinin void kasan_poison_vmalloc(const void *start, unsigned long size);
216d98c9e83SAndrey Ryabinin void kasan_unpoison_vmalloc(const void *start, unsigned long size);
2173c5c3cfbSDaniel Axtens void kasan_release_vmalloc(unsigned long start, unsigned long end,
2183c5c3cfbSDaniel Axtens 			   unsigned long free_region_start,
2193c5c3cfbSDaniel Axtens 			   unsigned long free_region_end);
2203c5c3cfbSDaniel Axtens #else
221d98c9e83SAndrey Ryabinin static inline int kasan_populate_vmalloc(unsigned long start,
222d98c9e83SAndrey Ryabinin 					unsigned long size)
2233c5c3cfbSDaniel Axtens {
2243c5c3cfbSDaniel Axtens 	return 0;
2253c5c3cfbSDaniel Axtens }
2263c5c3cfbSDaniel Axtens 
227d98c9e83SAndrey Ryabinin static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
228d98c9e83SAndrey Ryabinin { }
229d98c9e83SAndrey Ryabinin static inline void kasan_unpoison_vmalloc(const void *start, unsigned long size)
230d98c9e83SAndrey Ryabinin { }
2313c5c3cfbSDaniel Axtens static inline void kasan_release_vmalloc(unsigned long start,
2323c5c3cfbSDaniel Axtens 					 unsigned long end,
2333c5c3cfbSDaniel Axtens 					 unsigned long free_region_start,
2343c5c3cfbSDaniel Axtens 					 unsigned long free_region_end) {}
2353c5c3cfbSDaniel Axtens #endif
2363c5c3cfbSDaniel Axtens 
2372f004eeaSJann Horn #ifdef CONFIG_KASAN_INLINE
2382f004eeaSJann Horn void kasan_non_canonical_hook(unsigned long addr);
2392f004eeaSJann Horn #else /* CONFIG_KASAN_INLINE */
2402f004eeaSJann Horn static inline void kasan_non_canonical_hook(unsigned long addr) { }
2412f004eeaSJann Horn #endif /* CONFIG_KASAN_INLINE */
2422f004eeaSJann Horn 
2430b24beccSAndrey Ryabinin #endif /* LINUX_KASAN_H */
244