xref: /openbmc/linux/include/linux/kasan.h (revision 8cceeff4)
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 
140b24beccSAndrey Ryabinin #include <asm/kasan.h>
1569786cdbSAndrey Ryabinin #include <asm/pgtable.h>
160b24beccSAndrey Ryabinin 
179577dd74SAndrey Konovalov extern unsigned char kasan_early_shadow_page[PAGE_SIZE];
189577dd74SAndrey Konovalov extern pte_t kasan_early_shadow_pte[PTRS_PER_PTE];
199577dd74SAndrey Konovalov extern pmd_t kasan_early_shadow_pmd[PTRS_PER_PMD];
209577dd74SAndrey Konovalov extern pud_t kasan_early_shadow_pud[PTRS_PER_PUD];
219577dd74SAndrey Konovalov extern p4d_t kasan_early_shadow_p4d[MAX_PTRS_PER_P4D];
2269786cdbSAndrey Ryabinin 
239577dd74SAndrey Konovalov int kasan_populate_early_shadow(const void *shadow_start,
2469786cdbSAndrey Ryabinin 				const void *shadow_end);
2569786cdbSAndrey Ryabinin 
260b24beccSAndrey Ryabinin static inline void *kasan_mem_to_shadow(const void *addr)
270b24beccSAndrey Ryabinin {
280b24beccSAndrey Ryabinin 	return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
290b24beccSAndrey Ryabinin 		+ KASAN_SHADOW_OFFSET;
300b24beccSAndrey Ryabinin }
310b24beccSAndrey Ryabinin 
320b24beccSAndrey Ryabinin /* Enable reporting bugs after kasan_disable_current() */
33af8601adSIngo Molnar extern void kasan_enable_current(void);
340b24beccSAndrey Ryabinin 
350b24beccSAndrey Ryabinin /* Disable reporting bugs for current task */
36af8601adSIngo Molnar extern void kasan_disable_current(void);
370b24beccSAndrey Ryabinin 
380b24beccSAndrey Ryabinin void kasan_unpoison_shadow(const void *address, size_t size);
390b24beccSAndrey Ryabinin 
40e3ae1163SMark Rutland void kasan_unpoison_task_stack(struct task_struct *task);
419f7d416cSDmitry Vyukov void kasan_unpoison_stack_above_sp_to(const void *watermark);
42e3ae1163SMark Rutland 
43b8c73fc2SAndrey Ryabinin void kasan_alloc_pages(struct page *page, unsigned int order);
44b8c73fc2SAndrey Ryabinin void kasan_free_pages(struct page *page, unsigned int order);
45b8c73fc2SAndrey Ryabinin 
46be4a7988SAlexey Dobriyan void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
47d50112edSAlexey Dobriyan 			slab_flags_t *flags);
487ed2f9e6SAlexander Potapenko 
490316bec2SAndrey Ryabinin void kasan_poison_slab(struct page *page);
500316bec2SAndrey Ryabinin void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
510316bec2SAndrey Ryabinin void kasan_poison_object_data(struct kmem_cache *cache, void *object);
5266afc7f1SAndrey Konovalov void * __must_check kasan_init_slab_obj(struct kmem_cache *cache,
5366afc7f1SAndrey Konovalov 					const void *object);
540316bec2SAndrey Ryabinin 
5566afc7f1SAndrey Konovalov void * __must_check kasan_kmalloc_large(const void *ptr, size_t size,
5666afc7f1SAndrey Konovalov 						gfp_t flags);
57ee3ce779SDmitry Vyukov void kasan_kfree_large(void *ptr, unsigned long ip);
586860f634SDmitry Vyukov void kasan_poison_kfree(void *ptr, unsigned long ip);
5966afc7f1SAndrey Konovalov void * __must_check kasan_kmalloc(struct kmem_cache *s, const void *object,
6066afc7f1SAndrey Konovalov 					size_t size, gfp_t flags);
6166afc7f1SAndrey Konovalov void * __must_check kasan_krealloc(const void *object, size_t new_size,
62505f5dcbSAlexander Potapenko 					gfp_t flags);
630316bec2SAndrey Ryabinin 
6466afc7f1SAndrey Konovalov void * __must_check kasan_slab_alloc(struct kmem_cache *s, void *object,
6566afc7f1SAndrey Konovalov 					gfp_t flags);
66ee3ce779SDmitry Vyukov bool kasan_slab_free(struct kmem_cache *s, void *object, unsigned long ip);
670316bec2SAndrey Ryabinin 
687ed2f9e6SAlexander Potapenko struct kasan_cache {
697ed2f9e6SAlexander Potapenko 	int alloc_meta_offset;
707ed2f9e6SAlexander Potapenko 	int free_meta_offset;
717ed2f9e6SAlexander Potapenko };
727ed2f9e6SAlexander Potapenko 
733c5c3cfbSDaniel Axtens /*
743c5c3cfbSDaniel Axtens  * These functions provide a special case to support backing module
753c5c3cfbSDaniel Axtens  * allocations with real shadow memory. With KASAN vmalloc, the special
763c5c3cfbSDaniel Axtens  * case is unnecessary, as the work is handled in the generic case.
773c5c3cfbSDaniel Axtens  */
783c5c3cfbSDaniel Axtens #ifndef CONFIG_KASAN_VMALLOC
79bebf56a1SAndrey Ryabinin int kasan_module_alloc(void *addr, size_t size);
80a5af5aa8SAndrey Ryabinin void kasan_free_shadow(const struct vm_struct *vm);
813c5c3cfbSDaniel Axtens #else
823c5c3cfbSDaniel Axtens static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
833c5c3cfbSDaniel Axtens static inline void kasan_free_shadow(const struct vm_struct *vm) {}
843c5c3cfbSDaniel Axtens #endif
85bebf56a1SAndrey Ryabinin 
860207df4fSAndrey Ryabinin int kasan_add_zero_shadow(void *start, unsigned long size);
870207df4fSAndrey Ryabinin void kasan_remove_zero_shadow(void *start, unsigned long size);
880207df4fSAndrey Ryabinin 
890d4ca4c9SMarco Elver size_t __ksize(const void *);
900d4ca4c9SMarco Elver static inline void kasan_unpoison_slab(const void *ptr)
910d4ca4c9SMarco Elver {
920d4ca4c9SMarco Elver 	kasan_unpoison_shadow(ptr, __ksize(ptr));
930d4ca4c9SMarco Elver }
9480a9201aSAlexander Potapenko size_t kasan_metadata_size(struct kmem_cache *cache);
959b75a867SAndrey Ryabinin 
96b0845ce5SMark Rutland bool kasan_save_enable_multi_shot(void);
97b0845ce5SMark Rutland void kasan_restore_multi_shot(bool enabled);
98b0845ce5SMark Rutland 
990b24beccSAndrey Ryabinin #else /* CONFIG_KASAN */
1000b24beccSAndrey Ryabinin 
1010b24beccSAndrey Ryabinin static inline void kasan_unpoison_shadow(const void *address, size_t size) {}
1020b24beccSAndrey Ryabinin 
103e3ae1163SMark Rutland static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
1049f7d416cSDmitry Vyukov static inline void kasan_unpoison_stack_above_sp_to(const void *watermark) {}
105e3ae1163SMark Rutland 
1060b24beccSAndrey Ryabinin static inline void kasan_enable_current(void) {}
1070b24beccSAndrey Ryabinin static inline void kasan_disable_current(void) {}
1080b24beccSAndrey Ryabinin 
109b8c73fc2SAndrey Ryabinin static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
110b8c73fc2SAndrey Ryabinin static inline void kasan_free_pages(struct page *page, unsigned int order) {}
111b8c73fc2SAndrey Ryabinin 
1127ed2f9e6SAlexander Potapenko static inline void kasan_cache_create(struct kmem_cache *cache,
113be4a7988SAlexey Dobriyan 				      unsigned int *size,
114d50112edSAlexey Dobriyan 				      slab_flags_t *flags) {}
1157ed2f9e6SAlexander Potapenko 
1160316bec2SAndrey Ryabinin static inline void kasan_poison_slab(struct page *page) {}
1170316bec2SAndrey Ryabinin static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
1180316bec2SAndrey Ryabinin 					void *object) {}
1190316bec2SAndrey Ryabinin static inline void kasan_poison_object_data(struct kmem_cache *cache,
1200316bec2SAndrey Ryabinin 					void *object) {}
1210116523cSAndrey Konovalov static inline void *kasan_init_slab_obj(struct kmem_cache *cache,
1220116523cSAndrey Konovalov 				const void *object)
1230116523cSAndrey Konovalov {
1240116523cSAndrey Konovalov 	return (void *)object;
1250116523cSAndrey Konovalov }
1260316bec2SAndrey Ryabinin 
1270116523cSAndrey Konovalov static inline void *kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags)
1280116523cSAndrey Konovalov {
1290116523cSAndrey Konovalov 	return ptr;
1300116523cSAndrey Konovalov }
131ee3ce779SDmitry Vyukov static inline void kasan_kfree_large(void *ptr, unsigned long ip) {}
1326860f634SDmitry Vyukov static inline void kasan_poison_kfree(void *ptr, unsigned long ip) {}
1330116523cSAndrey Konovalov static inline void *kasan_kmalloc(struct kmem_cache *s, const void *object,
1340116523cSAndrey Konovalov 				size_t size, gfp_t flags)
1350116523cSAndrey Konovalov {
1360116523cSAndrey Konovalov 	return (void *)object;
1370116523cSAndrey Konovalov }
1380116523cSAndrey Konovalov static inline void *kasan_krealloc(const void *object, size_t new_size,
1390116523cSAndrey Konovalov 				 gfp_t flags)
1400116523cSAndrey Konovalov {
1410116523cSAndrey Konovalov 	return (void *)object;
1420116523cSAndrey Konovalov }
1430316bec2SAndrey Ryabinin 
1440116523cSAndrey Konovalov static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object,
1450116523cSAndrey Konovalov 				   gfp_t flags)
1460116523cSAndrey Konovalov {
1470116523cSAndrey Konovalov 	return object;
1480116523cSAndrey Konovalov }
149ee3ce779SDmitry Vyukov static inline bool kasan_slab_free(struct kmem_cache *s, void *object,
150ee3ce779SDmitry Vyukov 				   unsigned long ip)
15155834c59SAlexander Potapenko {
15255834c59SAlexander Potapenko 	return false;
15355834c59SAlexander Potapenko }
1540316bec2SAndrey Ryabinin 
155bebf56a1SAndrey Ryabinin static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
156a5af5aa8SAndrey Ryabinin static inline void kasan_free_shadow(const struct vm_struct *vm) {}
157bebf56a1SAndrey Ryabinin 
1580207df4fSAndrey Ryabinin static inline int kasan_add_zero_shadow(void *start, unsigned long size)
1590207df4fSAndrey Ryabinin {
1600207df4fSAndrey Ryabinin 	return 0;
1610207df4fSAndrey Ryabinin }
1620207df4fSAndrey Ryabinin static inline void kasan_remove_zero_shadow(void *start,
1630207df4fSAndrey Ryabinin 					unsigned long size)
1640207df4fSAndrey Ryabinin {}
1650207df4fSAndrey Ryabinin 
1669b75a867SAndrey Ryabinin static inline void kasan_unpoison_slab(const void *ptr) { }
16780a9201aSAlexander Potapenko static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
1689b75a867SAndrey Ryabinin 
1690b24beccSAndrey Ryabinin #endif /* CONFIG_KASAN */
1700b24beccSAndrey Ryabinin 
1712bd926b4SAndrey Konovalov #ifdef CONFIG_KASAN_GENERIC
1722bd926b4SAndrey Konovalov 
173080eb83fSAndrey Konovalov #define KASAN_SHADOW_INIT 0
174080eb83fSAndrey Konovalov 
1752bd926b4SAndrey Konovalov void kasan_cache_shrink(struct kmem_cache *cache);
1762bd926b4SAndrey Konovalov void kasan_cache_shutdown(struct kmem_cache *cache);
1772bd926b4SAndrey Konovalov 
1782bd926b4SAndrey Konovalov #else /* CONFIG_KASAN_GENERIC */
1792bd926b4SAndrey Konovalov 
1802bd926b4SAndrey Konovalov static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
1812bd926b4SAndrey Konovalov static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
1822bd926b4SAndrey Konovalov 
1832bd926b4SAndrey Konovalov #endif /* CONFIG_KASAN_GENERIC */
1842bd926b4SAndrey Konovalov 
185080eb83fSAndrey Konovalov #ifdef CONFIG_KASAN_SW_TAGS
186080eb83fSAndrey Konovalov 
187080eb83fSAndrey Konovalov #define KASAN_SHADOW_INIT 0xFF
188080eb83fSAndrey Konovalov 
1893c9e3aa1SAndrey Konovalov void kasan_init_tags(void);
1903c9e3aa1SAndrey Konovalov 
1913c9e3aa1SAndrey Konovalov void *kasan_reset_tag(const void *addr);
1923c9e3aa1SAndrey Konovalov 
1938cceeff4SWalter Wu bool kasan_report(unsigned long addr, size_t size,
19441eea9cdSAndrey Konovalov 		bool is_write, unsigned long ip);
19541eea9cdSAndrey Konovalov 
1963c9e3aa1SAndrey Konovalov #else /* CONFIG_KASAN_SW_TAGS */
1973c9e3aa1SAndrey Konovalov 
1983c9e3aa1SAndrey Konovalov static inline void kasan_init_tags(void) { }
1993c9e3aa1SAndrey Konovalov 
2003c9e3aa1SAndrey Konovalov static inline void *kasan_reset_tag(const void *addr)
2013c9e3aa1SAndrey Konovalov {
2023c9e3aa1SAndrey Konovalov 	return (void *)addr;
2033c9e3aa1SAndrey Konovalov }
2043c9e3aa1SAndrey Konovalov 
205080eb83fSAndrey Konovalov #endif /* CONFIG_KASAN_SW_TAGS */
206080eb83fSAndrey Konovalov 
2073c5c3cfbSDaniel Axtens #ifdef CONFIG_KASAN_VMALLOC
208d98c9e83SAndrey Ryabinin int kasan_populate_vmalloc(unsigned long addr, unsigned long size);
209d98c9e83SAndrey Ryabinin void kasan_poison_vmalloc(const void *start, unsigned long size);
210d98c9e83SAndrey Ryabinin void kasan_unpoison_vmalloc(const void *start, unsigned long size);
2113c5c3cfbSDaniel Axtens void kasan_release_vmalloc(unsigned long start, unsigned long end,
2123c5c3cfbSDaniel Axtens 			   unsigned long free_region_start,
2133c5c3cfbSDaniel Axtens 			   unsigned long free_region_end);
2143c5c3cfbSDaniel Axtens #else
215d98c9e83SAndrey Ryabinin static inline int kasan_populate_vmalloc(unsigned long start,
216d98c9e83SAndrey Ryabinin 					unsigned long size)
2173c5c3cfbSDaniel Axtens {
2183c5c3cfbSDaniel Axtens 	return 0;
2193c5c3cfbSDaniel Axtens }
2203c5c3cfbSDaniel Axtens 
221d98c9e83SAndrey Ryabinin static inline void kasan_poison_vmalloc(const void *start, unsigned long size)
222d98c9e83SAndrey Ryabinin { }
223d98c9e83SAndrey Ryabinin static inline void kasan_unpoison_vmalloc(const void *start, unsigned long size)
224d98c9e83SAndrey Ryabinin { }
2253c5c3cfbSDaniel Axtens static inline void kasan_release_vmalloc(unsigned long start,
2263c5c3cfbSDaniel Axtens 					 unsigned long end,
2273c5c3cfbSDaniel Axtens 					 unsigned long free_region_start,
2283c5c3cfbSDaniel Axtens 					 unsigned long free_region_end) {}
2293c5c3cfbSDaniel Axtens #endif
2303c5c3cfbSDaniel Axtens 
2312f004eeaSJann Horn #ifdef CONFIG_KASAN_INLINE
2322f004eeaSJann Horn void kasan_non_canonical_hook(unsigned long addr);
2332f004eeaSJann Horn #else /* CONFIG_KASAN_INLINE */
2342f004eeaSJann Horn static inline void kasan_non_canonical_hook(unsigned long addr) { }
2352f004eeaSJann Horn #endif /* CONFIG_KASAN_INLINE */
2362f004eeaSJann Horn 
2370b24beccSAndrey Ryabinin #endif /* LINUX_KASAN_H */
238