Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 1 | #ifndef _LINUX_KASAN_H |
| 2 | #define _LINUX_KASAN_H |
| 3 | |
Mark Rutland | e3ae116 | 2016-03-09 14:08:15 -0800 | [diff] [blame] | 4 | #include <linux/sched.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 5 | #include <linux/types.h> |
| 6 | |
| 7 | struct kmem_cache; |
| 8 | struct page; |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 9 | struct vm_struct; |
Masami Hiramatsu | 51a27f0 | 2017-03-16 16:40:21 -0700 | [diff] [blame] | 10 | struct task_struct; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 11 | |
| 12 | #ifdef CONFIG_KASAN |
| 13 | |
| 14 | #define KASAN_SHADOW_SCALE_SHIFT 3 |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 15 | |
| 16 | #include <asm/kasan.h> |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 17 | #include <asm/pgtable.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 18 | |
Andrey Ryabinin | 69786cdb | 2015-08-13 08:37:24 +0300 | [diff] [blame] | 19 | extern unsigned char kasan_zero_page[PAGE_SIZE]; |
| 20 | extern pte_t kasan_zero_pte[PTRS_PER_PTE]; |
| 21 | extern pmd_t kasan_zero_pmd[PTRS_PER_PMD]; |
| 22 | extern pud_t kasan_zero_pud[PTRS_PER_PUD]; |
| 23 | |
| 24 | void kasan_populate_zero_shadow(const void *shadow_start, |
| 25 | const void *shadow_end); |
| 26 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 27 | static inline void *kasan_mem_to_shadow(const void *addr) |
| 28 | { |
| 29 | return (void *)((unsigned long)addr >> KASAN_SHADOW_SCALE_SHIFT) |
| 30 | + KASAN_SHADOW_OFFSET; |
| 31 | } |
| 32 | |
| 33 | /* Enable reporting bugs after kasan_disable_current() */ |
Ingo Molnar | 46ae3e4 | 2017-02-03 09:57:00 +0100 | [diff] [blame] | 34 | extern void kasan_enable_current(void); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 35 | |
| 36 | /* Disable reporting bugs for current task */ |
Ingo Molnar | 46ae3e4 | 2017-02-03 09:57:00 +0100 | [diff] [blame] | 37 | extern void kasan_disable_current(void); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 38 | |
| 39 | void kasan_unpoison_shadow(const void *address, size_t size); |
| 40 | |
Mark Rutland | e3ae116 | 2016-03-09 14:08:15 -0800 | [diff] [blame] | 41 | void kasan_unpoison_task_stack(struct task_struct *task); |
Dmitry Vyukov | 9f7d416 | 2016-10-14 16:07:23 +0200 | [diff] [blame] | 42 | void kasan_unpoison_stack_above_sp_to(const void *watermark); |
Mark Rutland | e3ae116 | 2016-03-09 14:08:15 -0800 | [diff] [blame] | 43 | |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 44 | void kasan_alloc_pages(struct page *page, unsigned int order); |
| 45 | void kasan_free_pages(struct page *page, unsigned int order); |
| 46 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 47 | void kasan_cache_create(struct kmem_cache *cache, size_t *size, |
| 48 | unsigned long *flags); |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 49 | void kasan_cache_shrink(struct kmem_cache *cache); |
Greg Thelen | cb61d5a | 2017-02-24 15:00:05 -0800 | [diff] [blame] | 50 | void kasan_cache_shutdown(struct kmem_cache *cache); |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 51 | |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 52 | void kasan_poison_slab(struct page *page); |
| 53 | void kasan_unpoison_object_data(struct kmem_cache *cache, void *object); |
| 54 | void kasan_poison_object_data(struct kmem_cache *cache, void *object); |
Andrey Ryabinin | b3cbd9b | 2016-08-02 14:02:52 -0700 | [diff] [blame] | 55 | void kasan_init_slab_obj(struct kmem_cache *cache, const void *object); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 56 | |
Alexander Potapenko | 505f5dc | 2016-03-25 14:22:02 -0700 | [diff] [blame] | 57 | void kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 58 | void kasan_kfree_large(const void *ptr); |
Andrey Ryabinin | 9b75a86 | 2016-06-24 14:49:34 -0700 | [diff] [blame] | 59 | void kasan_poison_kfree(void *ptr); |
Alexander Potapenko | 505f5dc | 2016-03-25 14:22:02 -0700 | [diff] [blame] | 60 | void kasan_kmalloc(struct kmem_cache *s, const void *object, size_t size, |
| 61 | gfp_t flags); |
| 62 | void kasan_krealloc(const void *object, size_t new_size, gfp_t flags); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 63 | |
Alexander Potapenko | 505f5dc | 2016-03-25 14:22:02 -0700 | [diff] [blame] | 64 | void kasan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags); |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 65 | bool kasan_slab_free(struct kmem_cache *s, void *object); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 66 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 67 | struct kasan_cache { |
| 68 | int alloc_meta_offset; |
| 69 | int free_meta_offset; |
| 70 | }; |
| 71 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 72 | int kasan_module_alloc(void *addr, size_t size); |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 73 | void kasan_free_shadow(const struct vm_struct *vm); |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 74 | |
Andrey Ryabinin | 9b75a86 | 2016-06-24 14:49:34 -0700 | [diff] [blame] | 75 | size_t ksize(const void *); |
| 76 | static inline void kasan_unpoison_slab(const void *ptr) { ksize(ptr); } |
Alexander Potapenko | 80a9201 | 2016-07-28 15:49:07 -0700 | [diff] [blame] | 77 | size_t kasan_metadata_size(struct kmem_cache *cache); |
Andrey Ryabinin | 9b75a86 | 2016-06-24 14:49:34 -0700 | [diff] [blame] | 78 | |
Mark Rutland | 586b2bd | 2017-03-31 15:12:04 -0700 | [diff] [blame] | 79 | bool kasan_save_enable_multi_shot(void); |
| 80 | void kasan_restore_multi_shot(bool enabled); |
| 81 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 82 | #else /* CONFIG_KASAN */ |
| 83 | |
| 84 | static inline void kasan_unpoison_shadow(const void *address, size_t size) {} |
| 85 | |
Mark Rutland | e3ae116 | 2016-03-09 14:08:15 -0800 | [diff] [blame] | 86 | static inline void kasan_unpoison_task_stack(struct task_struct *task) {} |
Dmitry Vyukov | 9f7d416 | 2016-10-14 16:07:23 +0200 | [diff] [blame] | 87 | static inline void kasan_unpoison_stack_above_sp_to(const void *watermark) {} |
Mark Rutland | e3ae116 | 2016-03-09 14:08:15 -0800 | [diff] [blame] | 88 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 89 | static inline void kasan_enable_current(void) {} |
| 90 | static inline void kasan_disable_current(void) {} |
| 91 | |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 92 | static inline void kasan_alloc_pages(struct page *page, unsigned int order) {} |
| 93 | static inline void kasan_free_pages(struct page *page, unsigned int order) {} |
| 94 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 95 | static inline void kasan_cache_create(struct kmem_cache *cache, |
| 96 | size_t *size, |
| 97 | unsigned long *flags) {} |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 98 | static inline void kasan_cache_shrink(struct kmem_cache *cache) {} |
Greg Thelen | cb61d5a | 2017-02-24 15:00:05 -0800 | [diff] [blame] | 99 | static inline void kasan_cache_shutdown(struct kmem_cache *cache) {} |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 100 | |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 101 | static inline void kasan_poison_slab(struct page *page) {} |
| 102 | static inline void kasan_unpoison_object_data(struct kmem_cache *cache, |
| 103 | void *object) {} |
| 104 | static inline void kasan_poison_object_data(struct kmem_cache *cache, |
| 105 | void *object) {} |
Andrey Ryabinin | b3cbd9b | 2016-08-02 14:02:52 -0700 | [diff] [blame] | 106 | static inline void kasan_init_slab_obj(struct kmem_cache *cache, |
| 107 | const void *object) {} |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 108 | |
Alexander Potapenko | 505f5dc | 2016-03-25 14:22:02 -0700 | [diff] [blame] | 109 | static inline void kasan_kmalloc_large(void *ptr, size_t size, gfp_t flags) {} |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 110 | static inline void kasan_kfree_large(const void *ptr) {} |
Andrey Ryabinin | 9b75a86 | 2016-06-24 14:49:34 -0700 | [diff] [blame] | 111 | static inline void kasan_poison_kfree(void *ptr) {} |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 112 | static inline void kasan_kmalloc(struct kmem_cache *s, const void *object, |
Alexander Potapenko | 505f5dc | 2016-03-25 14:22:02 -0700 | [diff] [blame] | 113 | size_t size, gfp_t flags) {} |
| 114 | static inline void kasan_krealloc(const void *object, size_t new_size, |
| 115 | gfp_t flags) {} |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 116 | |
Alexander Potapenko | 505f5dc | 2016-03-25 14:22:02 -0700 | [diff] [blame] | 117 | static inline void kasan_slab_alloc(struct kmem_cache *s, void *object, |
| 118 | gfp_t flags) {} |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 119 | static inline bool kasan_slab_free(struct kmem_cache *s, void *object) |
| 120 | { |
| 121 | return false; |
| 122 | } |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 123 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 124 | static inline int kasan_module_alloc(void *addr, size_t size) { return 0; } |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 125 | static inline void kasan_free_shadow(const struct vm_struct *vm) {} |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 126 | |
Andrey Ryabinin | 9b75a86 | 2016-06-24 14:49:34 -0700 | [diff] [blame] | 127 | static inline void kasan_unpoison_slab(const void *ptr) { } |
Alexander Potapenko | 80a9201 | 2016-07-28 15:49:07 -0700 | [diff] [blame] | 128 | static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; } |
Andrey Ryabinin | 9b75a86 | 2016-06-24 14:49:34 -0700 | [diff] [blame] | 129 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 130 | #endif /* CONFIG_KASAN */ |
| 131 | |
| 132 | #endif /* LINUX_KASAN_H */ |