xref: /openbmc/linux/include/linux/kasan.h (revision 0207df4f)
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 
1769786cdbSAndrey Ryabinin extern unsigned char kasan_zero_page[PAGE_SIZE];
1869786cdbSAndrey Ryabinin extern pte_t kasan_zero_pte[PTRS_PER_PTE];
1969786cdbSAndrey Ryabinin extern pmd_t kasan_zero_pmd[PTRS_PER_PMD];
2069786cdbSAndrey Ryabinin extern pud_t kasan_zero_pud[PTRS_PER_PUD];
21c65e774fSKirill A. Shutemov extern p4d_t kasan_zero_p4d[MAX_PTRS_PER_P4D];
2269786cdbSAndrey Ryabinin 
230207df4fSAndrey Ryabinin int kasan_populate_zero_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);
4855834c59SAlexander Potapenko void kasan_cache_shrink(struct kmem_cache *cache);
49f9fa1d91SGreg Thelen void kasan_cache_shutdown(struct kmem_cache *cache);
507ed2f9e6SAlexander Potapenko 
510316bec2SAndrey Ryabinin void kasan_poison_slab(struct page *page);
520316bec2SAndrey Ryabinin void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
530316bec2SAndrey Ryabinin void kasan_poison_object_data(struct kmem_cache *cache, void *object);
54b3cbd9bfSAndrey Ryabinin void kasan_init_slab_obj(struct kmem_cache *cache, const void *object);
550316bec2SAndrey Ryabinin 
56505f5dcbSAlexander Potapenko void kasan_kmalloc_large(const void *ptr, size_t size, 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);
59505f5dcbSAlexander Potapenko void kasan_kmalloc(struct kmem_cache *s, const void *object, size_t size,
60505f5dcbSAlexander Potapenko 		  gfp_t flags);
61505f5dcbSAlexander Potapenko void kasan_krealloc(const void *object, size_t new_size, gfp_t flags);
620316bec2SAndrey Ryabinin 
63505f5dcbSAlexander Potapenko void kasan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags);
64ee3ce779SDmitry Vyukov bool kasan_slab_free(struct kmem_cache *s, void *object, unsigned long ip);
650316bec2SAndrey Ryabinin 
667ed2f9e6SAlexander Potapenko struct kasan_cache {
677ed2f9e6SAlexander Potapenko 	int alloc_meta_offset;
687ed2f9e6SAlexander Potapenko 	int free_meta_offset;
697ed2f9e6SAlexander Potapenko };
707ed2f9e6SAlexander Potapenko 
71bebf56a1SAndrey Ryabinin int kasan_module_alloc(void *addr, size_t size);
72a5af5aa8SAndrey Ryabinin void kasan_free_shadow(const struct vm_struct *vm);
73bebf56a1SAndrey Ryabinin 
740207df4fSAndrey Ryabinin int kasan_add_zero_shadow(void *start, unsigned long size);
750207df4fSAndrey Ryabinin void kasan_remove_zero_shadow(void *start, unsigned long size);
760207df4fSAndrey Ryabinin 
779b75a867SAndrey Ryabinin size_t ksize(const void *);
789b75a867SAndrey Ryabinin static inline void kasan_unpoison_slab(const void *ptr) { ksize(ptr); }
7980a9201aSAlexander Potapenko size_t kasan_metadata_size(struct kmem_cache *cache);
809b75a867SAndrey Ryabinin 
81b0845ce5SMark Rutland bool kasan_save_enable_multi_shot(void);
82b0845ce5SMark Rutland void kasan_restore_multi_shot(bool enabled);
83b0845ce5SMark Rutland 
840b24beccSAndrey Ryabinin #else /* CONFIG_KASAN */
850b24beccSAndrey Ryabinin 
860b24beccSAndrey Ryabinin static inline void kasan_unpoison_shadow(const void *address, size_t size) {}
870b24beccSAndrey Ryabinin 
88e3ae1163SMark Rutland static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
899f7d416cSDmitry Vyukov static inline void kasan_unpoison_stack_above_sp_to(const void *watermark) {}
90e3ae1163SMark Rutland 
910b24beccSAndrey Ryabinin static inline void kasan_enable_current(void) {}
920b24beccSAndrey Ryabinin static inline void kasan_disable_current(void) {}
930b24beccSAndrey Ryabinin 
94b8c73fc2SAndrey Ryabinin static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
95b8c73fc2SAndrey Ryabinin static inline void kasan_free_pages(struct page *page, unsigned int order) {}
96b8c73fc2SAndrey Ryabinin 
977ed2f9e6SAlexander Potapenko static inline void kasan_cache_create(struct kmem_cache *cache,
98be4a7988SAlexey Dobriyan 				      unsigned int *size,
99d50112edSAlexey Dobriyan 				      slab_flags_t *flags) {}
10055834c59SAlexander Potapenko static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
101f9fa1d91SGreg Thelen static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
1027ed2f9e6SAlexander Potapenko 
1030316bec2SAndrey Ryabinin static inline void kasan_poison_slab(struct page *page) {}
1040316bec2SAndrey Ryabinin static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
1050316bec2SAndrey Ryabinin 					void *object) {}
1060316bec2SAndrey Ryabinin static inline void kasan_poison_object_data(struct kmem_cache *cache,
1070316bec2SAndrey Ryabinin 					void *object) {}
108b3cbd9bfSAndrey Ryabinin static inline void kasan_init_slab_obj(struct kmem_cache *cache,
109b3cbd9bfSAndrey Ryabinin 				const void *object) {}
1100316bec2SAndrey Ryabinin 
111505f5dcbSAlexander Potapenko static inline void kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags) {}
112ee3ce779SDmitry Vyukov static inline void kasan_kfree_large(void *ptr, unsigned long ip) {}
1136860f634SDmitry Vyukov static inline void kasan_poison_kfree(void *ptr, unsigned long ip) {}
1140316bec2SAndrey Ryabinin static inline void kasan_kmalloc(struct kmem_cache *s, const void *object,
115505f5dcbSAlexander Potapenko 				size_t size, gfp_t flags) {}
116505f5dcbSAlexander Potapenko static inline void kasan_krealloc(const void *object, size_t new_size,
117505f5dcbSAlexander Potapenko 				 gfp_t flags) {}
1180316bec2SAndrey Ryabinin 
119505f5dcbSAlexander Potapenko static inline void kasan_slab_alloc(struct kmem_cache *s, void *object,
120505f5dcbSAlexander Potapenko 				   gfp_t flags) {}
121ee3ce779SDmitry Vyukov static inline bool kasan_slab_free(struct kmem_cache *s, void *object,
122ee3ce779SDmitry Vyukov 				   unsigned long ip)
12355834c59SAlexander Potapenko {
12455834c59SAlexander Potapenko 	return false;
12555834c59SAlexander Potapenko }
1260316bec2SAndrey Ryabinin 
127bebf56a1SAndrey Ryabinin static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
128a5af5aa8SAndrey Ryabinin static inline void kasan_free_shadow(const struct vm_struct *vm) {}
129bebf56a1SAndrey Ryabinin 
1300207df4fSAndrey Ryabinin static inline int kasan_add_zero_shadow(void *start, unsigned long size)
1310207df4fSAndrey Ryabinin {
1320207df4fSAndrey Ryabinin 	return 0;
1330207df4fSAndrey Ryabinin }
1340207df4fSAndrey Ryabinin static inline void kasan_remove_zero_shadow(void *start,
1350207df4fSAndrey Ryabinin 					unsigned long size)
1360207df4fSAndrey Ryabinin {}
1370207df4fSAndrey Ryabinin 
1389b75a867SAndrey Ryabinin static inline void kasan_unpoison_slab(const void *ptr) { }
13980a9201aSAlexander Potapenko static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
1409b75a867SAndrey Ryabinin 
1410b24beccSAndrey Ryabinin #endif /* CONFIG_KASAN */
1420b24beccSAndrey Ryabinin 
1430b24beccSAndrey Ryabinin #endif /* LINUX_KASAN_H */
144