xref: /openbmc/linux/include/linux/kasan.h (revision b0845ce5)
10b24beccSAndrey Ryabinin #ifndef _LINUX_KASAN_H
20b24beccSAndrey Ryabinin #define _LINUX_KASAN_H
30b24beccSAndrey Ryabinin 
40b24beccSAndrey Ryabinin #include <linux/types.h>
50b24beccSAndrey Ryabinin 
60b24beccSAndrey Ryabinin struct kmem_cache;
70b24beccSAndrey Ryabinin struct page;
8a5af5aa8SAndrey Ryabinin struct vm_struct;
95be9b730SMasami Hiramatsu struct task_struct;
100b24beccSAndrey Ryabinin 
110b24beccSAndrey Ryabinin #ifdef CONFIG_KASAN
120b24beccSAndrey Ryabinin 
130b24beccSAndrey Ryabinin #define KASAN_SHADOW_SCALE_SHIFT 3
140b24beccSAndrey Ryabinin 
150b24beccSAndrey Ryabinin #include <asm/kasan.h>
1669786cdbSAndrey Ryabinin #include <asm/pgtable.h>
170b24beccSAndrey Ryabinin 
1869786cdbSAndrey Ryabinin extern unsigned char kasan_zero_page[PAGE_SIZE];
1969786cdbSAndrey Ryabinin extern pte_t kasan_zero_pte[PTRS_PER_PTE];
2069786cdbSAndrey Ryabinin extern pmd_t kasan_zero_pmd[PTRS_PER_PMD];
2169786cdbSAndrey Ryabinin extern pud_t kasan_zero_pud[PTRS_PER_PUD];
22c2febafcSKirill A. Shutemov extern p4d_t kasan_zero_p4d[PTRS_PER_P4D];
2369786cdbSAndrey Ryabinin 
2469786cdbSAndrey Ryabinin void kasan_populate_zero_shadow(const void *shadow_start,
2569786cdbSAndrey Ryabinin 				const void *shadow_end);
2669786cdbSAndrey Ryabinin 
270b24beccSAndrey Ryabinin static inline void *kasan_mem_to_shadow(const void *addr)
280b24beccSAndrey Ryabinin {
290b24beccSAndrey Ryabinin 	return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
300b24beccSAndrey Ryabinin 		+ KASAN_SHADOW_OFFSET;
310b24beccSAndrey Ryabinin }
320b24beccSAndrey Ryabinin 
330b24beccSAndrey Ryabinin /* Enable reporting bugs after kasan_disable_current() */
34af8601adSIngo Molnar extern void kasan_enable_current(void);
350b24beccSAndrey Ryabinin 
360b24beccSAndrey Ryabinin /* Disable reporting bugs for current task */
37af8601adSIngo Molnar extern void kasan_disable_current(void);
380b24beccSAndrey Ryabinin 
390b24beccSAndrey Ryabinin void kasan_unpoison_shadow(const void *address, size_t size);
400b24beccSAndrey Ryabinin 
41e3ae1163SMark Rutland void kasan_unpoison_task_stack(struct task_struct *task);
429f7d416cSDmitry Vyukov void kasan_unpoison_stack_above_sp_to(const void *watermark);
43e3ae1163SMark Rutland 
44b8c73fc2SAndrey Ryabinin void kasan_alloc_pages(struct page *page, unsigned int order);
45b8c73fc2SAndrey Ryabinin void kasan_free_pages(struct page *page, unsigned int order);
46b8c73fc2SAndrey Ryabinin 
477ed2f9e6SAlexander Potapenko void kasan_cache_create(struct kmem_cache *cache, size_t *size,
487ed2f9e6SAlexander Potapenko 			unsigned long *flags);
4955834c59SAlexander Potapenko void kasan_cache_shrink(struct kmem_cache *cache);
50f9fa1d91SGreg Thelen void kasan_cache_shutdown(struct kmem_cache *cache);
517ed2f9e6SAlexander Potapenko 
520316bec2SAndrey Ryabinin void kasan_poison_slab(struct page *page);
530316bec2SAndrey Ryabinin void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
540316bec2SAndrey Ryabinin void kasan_poison_object_data(struct kmem_cache *cache, void *object);
55b3cbd9bfSAndrey Ryabinin void kasan_init_slab_obj(struct kmem_cache *cache, const void *object);
560316bec2SAndrey Ryabinin 
57505f5dcbSAlexander Potapenko void kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags);
580316bec2SAndrey Ryabinin void kasan_kfree_large(const void *ptr);
599b75a867SAndrey Ryabinin void kasan_poison_kfree(void *ptr);
60505f5dcbSAlexander Potapenko void kasan_kmalloc(struct kmem_cache *s, const void *object, size_t size,
61505f5dcbSAlexander Potapenko 		  gfp_t flags);
62505f5dcbSAlexander Potapenko void kasan_krealloc(const void *object, size_t new_size, gfp_t flags);
630316bec2SAndrey Ryabinin 
64505f5dcbSAlexander Potapenko void kasan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags);
6555834c59SAlexander Potapenko bool kasan_slab_free(struct kmem_cache *s, void *object);
660316bec2SAndrey Ryabinin 
677ed2f9e6SAlexander Potapenko struct kasan_cache {
687ed2f9e6SAlexander Potapenko 	int alloc_meta_offset;
697ed2f9e6SAlexander Potapenko 	int free_meta_offset;
707ed2f9e6SAlexander Potapenko };
717ed2f9e6SAlexander Potapenko 
72bebf56a1SAndrey Ryabinin int kasan_module_alloc(void *addr, size_t size);
73a5af5aa8SAndrey Ryabinin void kasan_free_shadow(const struct vm_struct *vm);
74bebf56a1SAndrey Ryabinin 
759b75a867SAndrey Ryabinin size_t ksize(const void *);
769b75a867SAndrey Ryabinin static inline void kasan_unpoison_slab(const void *ptr) { ksize(ptr); }
7780a9201aSAlexander Potapenko size_t kasan_metadata_size(struct kmem_cache *cache);
789b75a867SAndrey Ryabinin 
79b0845ce5SMark Rutland bool kasan_save_enable_multi_shot(void);
80b0845ce5SMark Rutland void kasan_restore_multi_shot(bool enabled);
81b0845ce5SMark Rutland 
820b24beccSAndrey Ryabinin #else /* CONFIG_KASAN */
830b24beccSAndrey Ryabinin 
840b24beccSAndrey Ryabinin static inline void kasan_unpoison_shadow(const void *address, size_t size) {}
850b24beccSAndrey Ryabinin 
86e3ae1163SMark Rutland static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
879f7d416cSDmitry Vyukov static inline void kasan_unpoison_stack_above_sp_to(const void *watermark) {}
88e3ae1163SMark Rutland 
890b24beccSAndrey Ryabinin static inline void kasan_enable_current(void) {}
900b24beccSAndrey Ryabinin static inline void kasan_disable_current(void) {}
910b24beccSAndrey Ryabinin 
92b8c73fc2SAndrey Ryabinin static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
93b8c73fc2SAndrey Ryabinin static inline void kasan_free_pages(struct page *page, unsigned int order) {}
94b8c73fc2SAndrey Ryabinin 
957ed2f9e6SAlexander Potapenko static inline void kasan_cache_create(struct kmem_cache *cache,
967ed2f9e6SAlexander Potapenko 				      size_t *size,
977ed2f9e6SAlexander Potapenko 				      unsigned long *flags) {}
9855834c59SAlexander Potapenko static inline void kasan_cache_shrink(struct kmem_cache *cache) {}
99f9fa1d91SGreg Thelen static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
1007ed2f9e6SAlexander Potapenko 
1010316bec2SAndrey Ryabinin static inline void kasan_poison_slab(struct page *page) {}
1020316bec2SAndrey Ryabinin static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
1030316bec2SAndrey Ryabinin 					void *object) {}
1040316bec2SAndrey Ryabinin static inline void kasan_poison_object_data(struct kmem_cache *cache,
1050316bec2SAndrey Ryabinin 					void *object) {}
106b3cbd9bfSAndrey Ryabinin static inline void kasan_init_slab_obj(struct kmem_cache *cache,
107b3cbd9bfSAndrey Ryabinin 				const void *object) {}
1080316bec2SAndrey Ryabinin 
109505f5dcbSAlexander Potapenko static inline void kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags) {}
1100316bec2SAndrey Ryabinin static inline void kasan_kfree_large(const void *ptr) {}
1119b75a867SAndrey Ryabinin static inline void kasan_poison_kfree(void *ptr) {}
1120316bec2SAndrey Ryabinin static inline void kasan_kmalloc(struct kmem_cache *s, const void *object,
113505f5dcbSAlexander Potapenko 				size_t size, gfp_t flags) {}
114505f5dcbSAlexander Potapenko static inline void kasan_krealloc(const void *object, size_t new_size,
115505f5dcbSAlexander Potapenko 				 gfp_t flags) {}
1160316bec2SAndrey Ryabinin 
117505f5dcbSAlexander Potapenko static inline void kasan_slab_alloc(struct kmem_cache *s, void *object,
118505f5dcbSAlexander Potapenko 				   gfp_t flags) {}
11955834c59SAlexander Potapenko static inline bool kasan_slab_free(struct kmem_cache *s, void *object)
12055834c59SAlexander Potapenko {
12155834c59SAlexander Potapenko 	return false;
12255834c59SAlexander Potapenko }
1230316bec2SAndrey Ryabinin 
124bebf56a1SAndrey Ryabinin static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
125a5af5aa8SAndrey Ryabinin static inline void kasan_free_shadow(const struct vm_struct *vm) {}
126bebf56a1SAndrey Ryabinin 
1279b75a867SAndrey Ryabinin static inline void kasan_unpoison_slab(const void *ptr) { }
12880a9201aSAlexander Potapenko static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
1299b75a867SAndrey Ryabinin 
1300b24beccSAndrey Ryabinin #endif /* CONFIG_KASAN */
1310b24beccSAndrey Ryabinin 
1320b24beccSAndrey Ryabinin #endif /* LINUX_KASAN_H */
133