Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 1 | #ifndef __MM_KASAN_KASAN_H |
| 2 | #define __MM_KASAN_KASAN_H |
| 3 | |
| 4 | #include <linux/kasan.h> |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 5 | #include <linux/stackdepot.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 6 | |
| 7 | #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT) |
| 8 | #define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1) |
| 9 | |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 10 | #define KASAN_FREE_PAGE 0xFF /* page was freed */ |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 11 | #define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */ |
| 12 | #define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */ |
| 13 | #define KASAN_KMALLOC_FREE 0xFB /* object was freed (kmem_cache_free/kfree) */ |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 14 | #define KASAN_GLOBAL_REDZONE 0xFA /* redzone for global variable */ |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 15 | |
Andrey Ryabinin | c420f16 | 2015-02-13 14:39:59 -0800 | [diff] [blame] | 16 | /* |
| 17 | * Stack redzone shadow values |
| 18 | * (Those are compiler's ABI, don't change them) |
| 19 | */ |
| 20 | #define KASAN_STACK_LEFT 0xF1 |
| 21 | #define KASAN_STACK_MID 0xF2 |
| 22 | #define KASAN_STACK_RIGHT 0xF3 |
| 23 | #define KASAN_STACK_PARTIAL 0xF4 |
Dmitry Vyukov | 828347f | 2016-11-30 15:54:16 -0800 | [diff] [blame] | 24 | #define KASAN_USE_AFTER_SCOPE 0xF8 |
Andrey Ryabinin | c420f16 | 2015-02-13 14:39:59 -0800 | [diff] [blame] | 25 | |
Paul Lawrence | 9351dcd | 2018-02-06 15:36:11 -0800 | [diff] [blame] | 26 | /* |
| 27 | * alloca redzone shadow values |
| 28 | */ |
| 29 | #define KASAN_ALLOCA_LEFT 0xCA |
| 30 | #define KASAN_ALLOCA_RIGHT 0xCB |
| 31 | |
| 32 | #define KASAN_ALLOCA_REDZONE_SIZE 32 |
| 33 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 34 | /* Don't break randconfig/all*config builds */ |
| 35 | #ifndef KASAN_ABI_VERSION |
| 36 | #define KASAN_ABI_VERSION 1 |
| 37 | #endif |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 38 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 39 | struct kasan_access_info { |
| 40 | const void *access_addr; |
| 41 | const void *first_bad_addr; |
| 42 | size_t access_size; |
| 43 | bool is_write; |
| 44 | unsigned long ip; |
| 45 | }; |
| 46 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 47 | /* The layout of struct dictated by compiler */ |
| 48 | struct kasan_source_location { |
| 49 | const char *filename; |
| 50 | int line_no; |
| 51 | int column_no; |
| 52 | }; |
| 53 | |
| 54 | /* The layout of struct dictated by compiler */ |
| 55 | struct kasan_global { |
| 56 | const void *beg; /* Address of the beginning of the global variable. */ |
| 57 | size_t size; /* Size of the global variable. */ |
| 58 | size_t size_with_redzone; /* Size of the variable + size of the red zone. 32 bytes aligned */ |
| 59 | const void *name; |
| 60 | const void *module_name; /* Name of the module where the global variable is declared. */ |
| 61 | unsigned long has_dynamic_init; /* This needed for C++ */ |
| 62 | #if KASAN_ABI_VERSION >= 4 |
| 63 | struct kasan_source_location *location; |
| 64 | #endif |
Dmitry Vyukov | 045d599 | 2016-11-30 15:54:13 -0800 | [diff] [blame] | 65 | #if KASAN_ABI_VERSION >= 5 |
| 66 | char *odr_indicator; |
| 67 | #endif |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 68 | }; |
| 69 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Structures to keep alloc and free tracks * |
| 72 | */ |
| 73 | |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 74 | #define KASAN_STACK_DEPTH 64 |
| 75 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 76 | struct kasan_track { |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 77 | u32 pid; |
| 78 | depot_stack_handle_t stack; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | struct kasan_alloc_meta { |
Andrey Ryabinin | b3cbd9b | 2016-08-02 14:02:52 -0700 | [diff] [blame] | 82 | struct kasan_track alloc_track; |
| 83 | struct kasan_track free_track; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 86 | struct qlist_node { |
| 87 | struct qlist_node *next; |
| 88 | }; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 89 | struct kasan_free_meta { |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 90 | /* This field is used while the object is in the quarantine. |
| 91 | * Otherwise it might be used for the allocator freelist. |
| 92 | */ |
| 93 | struct qlist_node quarantine_link; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache, |
| 97 | const void *object); |
| 98 | struct kasan_free_meta *get_free_info(struct kmem_cache *cache, |
| 99 | const void *object); |
| 100 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 101 | static inline const void *kasan_shadow_to_mem(const void *shadow_addr) |
| 102 | { |
| 103 | return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET) |
| 104 | << KASAN_SHADOW_SCALE_SHIFT); |
| 105 | } |
| 106 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 107 | void kasan_report(unsigned long addr, size_t size, |
| 108 | bool is_write, unsigned long ip); |
Andrey Ryabinin | 7e08897 | 2016-08-02 14:02:55 -0700 | [diff] [blame] | 109 | void kasan_report_double_free(struct kmem_cache *cache, void *object, |
Andrey Konovalov | bef4eb3 | 2017-05-03 14:56:47 -0700 | [diff] [blame] | 110 | void *ip); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 111 | |
Alexander Potapenko | 80a9201 | 2016-07-28 15:49:07 -0700 | [diff] [blame] | 112 | #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB) |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 113 | void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache); |
| 114 | void quarantine_reduce(void); |
| 115 | void quarantine_remove_cache(struct kmem_cache *cache); |
| 116 | #else |
| 117 | static inline void quarantine_put(struct kasan_free_meta *info, |
| 118 | struct kmem_cache *cache) { } |
| 119 | static inline void quarantine_reduce(void) { } |
| 120 | static inline void quarantine_remove_cache(struct kmem_cache *cache) { } |
| 121 | #endif |
| 122 | |
Alexander Potapenko | d6f2774 | 2018-02-06 15:36:20 -0800 | [diff] [blame] | 123 | /* |
| 124 | * Exported functions for interfaces called from assembly or from generated |
| 125 | * code. Declarations here to avoid warning about missing declarations. |
| 126 | */ |
| 127 | asmlinkage void kasan_unpoison_task_stack_below(const void *watermark); |
| 128 | void __asan_register_globals(struct kasan_global *globals, size_t size); |
| 129 | void __asan_unregister_globals(struct kasan_global *globals, size_t size); |
| 130 | void __asan_loadN(unsigned long addr, size_t size); |
| 131 | void __asan_storeN(unsigned long addr, size_t size); |
| 132 | void __asan_handle_no_return(void); |
| 133 | void __asan_poison_stack_memory(const void *addr, size_t size); |
| 134 | void __asan_unpoison_stack_memory(const void *addr, size_t size); |
| 135 | void __asan_alloca_poison(unsigned long addr, size_t size); |
| 136 | void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom); |
| 137 | |
| 138 | void __asan_load1(unsigned long addr); |
| 139 | void __asan_store1(unsigned long addr); |
| 140 | void __asan_load2(unsigned long addr); |
| 141 | void __asan_store2(unsigned long addr); |
| 142 | void __asan_load4(unsigned long addr); |
| 143 | void __asan_store4(unsigned long addr); |
| 144 | void __asan_load8(unsigned long addr); |
| 145 | void __asan_store8(unsigned long addr); |
| 146 | void __asan_load16(unsigned long addr); |
| 147 | void __asan_store16(unsigned long addr); |
| 148 | |
| 149 | void __asan_load1_noabort(unsigned long addr); |
| 150 | void __asan_store1_noabort(unsigned long addr); |
| 151 | void __asan_load2_noabort(unsigned long addr); |
| 152 | void __asan_store2_noabort(unsigned long addr); |
| 153 | void __asan_load4_noabort(unsigned long addr); |
| 154 | void __asan_store4_noabort(unsigned long addr); |
| 155 | void __asan_load8_noabort(unsigned long addr); |
| 156 | void __asan_store8_noabort(unsigned long addr); |
| 157 | void __asan_load16_noabort(unsigned long addr); |
| 158 | void __asan_store16_noabort(unsigned long addr); |
| 159 | |
| 160 | void __asan_set_shadow_00(const void *addr, size_t size); |
| 161 | void __asan_set_shadow_f1(const void *addr, size_t size); |
| 162 | void __asan_set_shadow_f2(const void *addr, size_t size); |
| 163 | void __asan_set_shadow_f3(const void *addr, size_t size); |
| 164 | void __asan_set_shadow_f5(const void *addr, size_t size); |
| 165 | void __asan_set_shadow_f8(const void *addr, size_t size); |
| 166 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 167 | #endif |