xref: /openbmc/linux/include/linux/kasan.h (revision 7ed2f9e6)
10b24beccSAndrey Ryabinin #ifndef _LINUX_KASAN_H
20b24beccSAndrey Ryabinin #define _LINUX_KASAN_H
30b24beccSAndrey Ryabinin 
4e3ae1163SMark Rutland #include <linux/sched.h>
50b24beccSAndrey Ryabinin #include <linux/types.h>
60b24beccSAndrey Ryabinin 
70b24beccSAndrey Ryabinin struct kmem_cache;
80b24beccSAndrey Ryabinin struct page;
9a5af5aa8SAndrey Ryabinin struct vm_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];
2269786cdbSAndrey Ryabinin 
2369786cdbSAndrey Ryabinin void 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() */
330b24beccSAndrey Ryabinin static inline void kasan_enable_current(void)
340b24beccSAndrey Ryabinin {
350b24beccSAndrey Ryabinin 	current->kasan_depth++;
360b24beccSAndrey Ryabinin }
370b24beccSAndrey Ryabinin 
380b24beccSAndrey Ryabinin /* Disable reporting bugs for current task */
390b24beccSAndrey Ryabinin static inline void kasan_disable_current(void)
400b24beccSAndrey Ryabinin {
410b24beccSAndrey Ryabinin 	current->kasan_depth--;
420b24beccSAndrey Ryabinin }
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 
517ed2f9e6SAlexander Potapenko void kasan_cache_create(struct kmem_cache *cache, size_t *size,
527ed2f9e6SAlexander Potapenko 			unsigned long *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);
570316bec2SAndrey Ryabinin 
580316bec2SAndrey Ryabinin void kasan_kmalloc_large(const void *ptr, size_t size);
590316bec2SAndrey Ryabinin void kasan_kfree_large(const void *ptr);
6092393615SAndrey Ryabinin void kasan_kfree(void *ptr);
610316bec2SAndrey Ryabinin void kasan_kmalloc(struct kmem_cache *s, const void *object, size_t size);
620316bec2SAndrey Ryabinin void kasan_krealloc(const void *object, size_t new_size);
630316bec2SAndrey Ryabinin 
640316bec2SAndrey Ryabinin void kasan_slab_alloc(struct kmem_cache *s, void *object);
650316bec2SAndrey Ryabinin void 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 
750b24beccSAndrey Ryabinin #else /* CONFIG_KASAN */
760b24beccSAndrey Ryabinin 
770b24beccSAndrey Ryabinin static inline void kasan_unpoison_shadow(const void *address, size_t size) {}
780b24beccSAndrey Ryabinin 
79e3ae1163SMark Rutland static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
80e3ae1163SMark Rutland 
810b24beccSAndrey Ryabinin static inline void kasan_enable_current(void) {}
820b24beccSAndrey Ryabinin static inline void kasan_disable_current(void) {}
830b24beccSAndrey Ryabinin 
84b8c73fc2SAndrey Ryabinin static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
85b8c73fc2SAndrey Ryabinin static inline void kasan_free_pages(struct page *page, unsigned int order) {}
86b8c73fc2SAndrey Ryabinin 
877ed2f9e6SAlexander Potapenko static inline void kasan_cache_create(struct kmem_cache *cache,
887ed2f9e6SAlexander Potapenko 				      size_t *size,
897ed2f9e6SAlexander Potapenko 				      unsigned long *flags) {}
907ed2f9e6SAlexander Potapenko 
910316bec2SAndrey Ryabinin static inline void kasan_poison_slab(struct page *page) {}
920316bec2SAndrey Ryabinin static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
930316bec2SAndrey Ryabinin 					void *object) {}
940316bec2SAndrey Ryabinin static inline void kasan_poison_object_data(struct kmem_cache *cache,
950316bec2SAndrey Ryabinin 					void *object) {}
960316bec2SAndrey Ryabinin 
970316bec2SAndrey Ryabinin static inline void kasan_kmalloc_large(void *ptr, size_t size) {}
980316bec2SAndrey Ryabinin static inline void kasan_kfree_large(const void *ptr) {}
9992393615SAndrey Ryabinin static inline void kasan_kfree(void *ptr) {}
1000316bec2SAndrey Ryabinin static inline void kasan_kmalloc(struct kmem_cache *s, const void *object,
1010316bec2SAndrey Ryabinin 				size_t size) {}
1020316bec2SAndrey Ryabinin static inline void kasan_krealloc(const void *object, size_t new_size) {}
1030316bec2SAndrey Ryabinin 
1040316bec2SAndrey Ryabinin static inline void kasan_slab_alloc(struct kmem_cache *s, void *object) {}
1050316bec2SAndrey Ryabinin static inline void kasan_slab_free(struct kmem_cache *s, void *object) {}
1060316bec2SAndrey Ryabinin 
107bebf56a1SAndrey Ryabinin static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
108a5af5aa8SAndrey Ryabinin static inline void kasan_free_shadow(const struct vm_struct *vm) {}
109bebf56a1SAndrey Ryabinin 
1100b24beccSAndrey Ryabinin #endif /* CONFIG_KASAN */
1110b24beccSAndrey Ryabinin 
1120b24beccSAndrey Ryabinin #endif /* LINUX_KASAN_H */
113