xref: /openbmc/linux/include/linux/kasan.h (revision bebf56a1)
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;
80b24beccSAndrey Ryabinin 
90b24beccSAndrey Ryabinin #ifdef CONFIG_KASAN
100b24beccSAndrey Ryabinin 
110b24beccSAndrey Ryabinin #define KASAN_SHADOW_SCALE_SHIFT 3
120b24beccSAndrey Ryabinin #define KASAN_SHADOW_OFFSET _AC(CONFIG_KASAN_SHADOW_OFFSET, UL)
130b24beccSAndrey Ryabinin 
140b24beccSAndrey Ryabinin #include <asm/kasan.h>
150b24beccSAndrey Ryabinin #include <linux/sched.h>
160b24beccSAndrey Ryabinin 
170b24beccSAndrey Ryabinin static inline void *kasan_mem_to_shadow(const void *addr)
180b24beccSAndrey Ryabinin {
190b24beccSAndrey Ryabinin 	return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT)
200b24beccSAndrey Ryabinin 		+ KASAN_SHADOW_OFFSET;
210b24beccSAndrey Ryabinin }
220b24beccSAndrey Ryabinin 
230b24beccSAndrey Ryabinin /* Enable reporting bugs after kasan_disable_current() */
240b24beccSAndrey Ryabinin static inline void kasan_enable_current(void)
250b24beccSAndrey Ryabinin {
260b24beccSAndrey Ryabinin 	current->kasan_depth++;
270b24beccSAndrey Ryabinin }
280b24beccSAndrey Ryabinin 
290b24beccSAndrey Ryabinin /* Disable reporting bugs for current task */
300b24beccSAndrey Ryabinin static inline void kasan_disable_current(void)
310b24beccSAndrey Ryabinin {
320b24beccSAndrey Ryabinin 	current->kasan_depth--;
330b24beccSAndrey Ryabinin }
340b24beccSAndrey Ryabinin 
350b24beccSAndrey Ryabinin void kasan_unpoison_shadow(const void *address, size_t size);
360b24beccSAndrey Ryabinin 
37b8c73fc2SAndrey Ryabinin void kasan_alloc_pages(struct page *page, unsigned int order);
38b8c73fc2SAndrey Ryabinin void kasan_free_pages(struct page *page, unsigned int order);
39b8c73fc2SAndrey Ryabinin 
400316bec2SAndrey Ryabinin void kasan_poison_slab(struct page *page);
410316bec2SAndrey Ryabinin void kasan_unpoison_object_data(struct kmem_cache *cache, void *object);
420316bec2SAndrey Ryabinin void kasan_poison_object_data(struct kmem_cache *cache, void *object);
430316bec2SAndrey Ryabinin 
440316bec2SAndrey Ryabinin void kasan_kmalloc_large(const void *ptr, size_t size);
450316bec2SAndrey Ryabinin void kasan_kfree_large(const void *ptr);
460316bec2SAndrey Ryabinin void kasan_kmalloc(struct kmem_cache *s, const void *object, size_t size);
470316bec2SAndrey Ryabinin void kasan_krealloc(const void *object, size_t new_size);
480316bec2SAndrey Ryabinin 
490316bec2SAndrey Ryabinin void kasan_slab_alloc(struct kmem_cache *s, void *object);
500316bec2SAndrey Ryabinin void kasan_slab_free(struct kmem_cache *s, void *object);
510316bec2SAndrey Ryabinin 
52bebf56a1SAndrey Ryabinin #define MODULE_ALIGN (PAGE_SIZE << KASAN_SHADOW_SCALE_SHIFT)
53bebf56a1SAndrey Ryabinin 
54bebf56a1SAndrey Ryabinin int kasan_module_alloc(void *addr, size_t size);
55bebf56a1SAndrey Ryabinin void kasan_module_free(void *addr);
56bebf56a1SAndrey Ryabinin 
570b24beccSAndrey Ryabinin #else /* CONFIG_KASAN */
580b24beccSAndrey Ryabinin 
59bebf56a1SAndrey Ryabinin #define MODULE_ALIGN 1
60bebf56a1SAndrey Ryabinin 
610b24beccSAndrey Ryabinin static inline void kasan_unpoison_shadow(const void *address, size_t size) {}
620b24beccSAndrey Ryabinin 
630b24beccSAndrey Ryabinin static inline void kasan_enable_current(void) {}
640b24beccSAndrey Ryabinin static inline void kasan_disable_current(void) {}
650b24beccSAndrey Ryabinin 
66b8c73fc2SAndrey Ryabinin static inline void kasan_alloc_pages(struct page *page, unsigned int order) {}
67b8c73fc2SAndrey Ryabinin static inline void kasan_free_pages(struct page *page, unsigned int order) {}
68b8c73fc2SAndrey Ryabinin 
690316bec2SAndrey Ryabinin static inline void kasan_poison_slab(struct page *page) {}
700316bec2SAndrey Ryabinin static inline void kasan_unpoison_object_data(struct kmem_cache *cache,
710316bec2SAndrey Ryabinin 					void *object) {}
720316bec2SAndrey Ryabinin static inline void kasan_poison_object_data(struct kmem_cache *cache,
730316bec2SAndrey Ryabinin 					void *object) {}
740316bec2SAndrey Ryabinin 
750316bec2SAndrey Ryabinin static inline void kasan_kmalloc_large(void *ptr, size_t size) {}
760316bec2SAndrey Ryabinin static inline void kasan_kfree_large(const void *ptr) {}
770316bec2SAndrey Ryabinin static inline void kasan_kmalloc(struct kmem_cache *s, const void *object,
780316bec2SAndrey Ryabinin 				size_t size) {}
790316bec2SAndrey Ryabinin static inline void kasan_krealloc(const void *object, size_t new_size) {}
800316bec2SAndrey Ryabinin 
810316bec2SAndrey Ryabinin static inline void kasan_slab_alloc(struct kmem_cache *s, void *object) {}
820316bec2SAndrey Ryabinin static inline void kasan_slab_free(struct kmem_cache *s, void *object) {}
830316bec2SAndrey Ryabinin 
84bebf56a1SAndrey Ryabinin static inline int kasan_module_alloc(void *addr, size_t size) { return 0; }
85bebf56a1SAndrey Ryabinin static inline void kasan_module_free(void *addr) {}
86bebf56a1SAndrey Ryabinin 
870b24beccSAndrey Ryabinin #endif /* CONFIG_KASAN */
880b24beccSAndrey Ryabinin 
890b24beccSAndrey Ryabinin #endif /* LINUX_KASAN_H */
90