]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
0b24becc AR |
2 | #ifndef __MM_KASAN_KASAN_H |
3 | #define __MM_KASAN_KASAN_H | |
4 | ||
5 | #include <linux/kasan.h> | |
cd11016e | 6 | #include <linux/stackdepot.h> |
0b24becc AR |
7 | |
8 | #define KASAN_SHADOW_SCALE_SIZE (1UL << KASAN_SHADOW_SCALE_SHIFT) | |
9 | #define KASAN_SHADOW_MASK (KASAN_SHADOW_SCALE_SIZE - 1) | |
10 | ||
3c9e3aa1 AK |
11 | #define KASAN_TAG_KERNEL 0xFF /* native kernel pointers tag */ |
12 | #define KASAN_TAG_INVALID 0xFE /* inaccessible memory tag */ | |
13 | #define KASAN_TAG_MAX 0xFD /* maximum value for random tags */ | |
14 | ||
0316bec2 AR |
15 | #define KASAN_FREE_PAGE 0xFF /* page was freed */ |
16 | #define KASAN_PAGE_REDZONE 0xFE /* redzone for kmalloc_large allocations */ | |
17 | #define KASAN_KMALLOC_REDZONE 0xFC /* redzone inside slub object */ | |
18 | #define KASAN_KMALLOC_FREE 0xFB /* object was freed (kmem_cache_free/kfree) */ | |
bebf56a1 | 19 | #define KASAN_GLOBAL_REDZONE 0xFA /* redzone for global variable */ |
0316bec2 | 20 | |
c420f167 AR |
21 | /* |
22 | * Stack redzone shadow values | |
23 | * (Those are compiler's ABI, don't change them) | |
24 | */ | |
25 | #define KASAN_STACK_LEFT 0xF1 | |
26 | #define KASAN_STACK_MID 0xF2 | |
27 | #define KASAN_STACK_RIGHT 0xF3 | |
28 | #define KASAN_STACK_PARTIAL 0xF4 | |
828347f8 | 29 | #define KASAN_USE_AFTER_SCOPE 0xF8 |
c420f167 | 30 | |
342061ee PL |
31 | /* |
32 | * alloca redzone shadow values | |
33 | */ | |
34 | #define KASAN_ALLOCA_LEFT 0xCA | |
35 | #define KASAN_ALLOCA_RIGHT 0xCB | |
36 | ||
37 | #define KASAN_ALLOCA_REDZONE_SIZE 32 | |
38 | ||
bebf56a1 AR |
39 | /* Don't break randconfig/all*config builds */ |
40 | #ifndef KASAN_ABI_VERSION | |
41 | #define KASAN_ABI_VERSION 1 | |
42 | #endif | |
b8c73fc2 | 43 | |
0b24becc AR |
44 | struct kasan_access_info { |
45 | const void *access_addr; | |
46 | const void *first_bad_addr; | |
47 | size_t access_size; | |
48 | bool is_write; | |
49 | unsigned long ip; | |
50 | }; | |
51 | ||
bebf56a1 AR |
52 | /* The layout of struct dictated by compiler */ |
53 | struct kasan_source_location { | |
54 | const char *filename; | |
55 | int line_no; | |
56 | int column_no; | |
57 | }; | |
58 | ||
59 | /* The layout of struct dictated by compiler */ | |
60 | struct kasan_global { | |
61 | const void *beg; /* Address of the beginning of the global variable. */ | |
62 | size_t size; /* Size of the global variable. */ | |
63 | size_t size_with_redzone; /* Size of the variable + size of the red zone. 32 bytes aligned */ | |
64 | const void *name; | |
65 | const void *module_name; /* Name of the module where the global variable is declared. */ | |
66 | unsigned long has_dynamic_init; /* This needed for C++ */ | |
67 | #if KASAN_ABI_VERSION >= 4 | |
68 | struct kasan_source_location *location; | |
69 | #endif | |
045d599a DV |
70 | #if KASAN_ABI_VERSION >= 5 |
71 | char *odr_indicator; | |
72 | #endif | |
bebf56a1 AR |
73 | }; |
74 | ||
7ed2f9e6 AP |
75 | /** |
76 | * Structures to keep alloc and free tracks * | |
77 | */ | |
78 | ||
cd11016e AP |
79 | #define KASAN_STACK_DEPTH 64 |
80 | ||
7ed2f9e6 | 81 | struct kasan_track { |
cd11016e AP |
82 | u32 pid; |
83 | depot_stack_handle_t stack; | |
7ed2f9e6 AP |
84 | }; |
85 | ||
86 | struct kasan_alloc_meta { | |
b3cbd9bf AR |
87 | struct kasan_track alloc_track; |
88 | struct kasan_track free_track; | |
7ed2f9e6 AP |
89 | }; |
90 | ||
55834c59 AP |
91 | struct qlist_node { |
92 | struct qlist_node *next; | |
93 | }; | |
7ed2f9e6 | 94 | struct kasan_free_meta { |
55834c59 AP |
95 | /* This field is used while the object is in the quarantine. |
96 | * Otherwise it might be used for the allocator freelist. | |
97 | */ | |
98 | struct qlist_node quarantine_link; | |
7ed2f9e6 AP |
99 | }; |
100 | ||
101 | struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache, | |
102 | const void *object); | |
103 | struct kasan_free_meta *get_free_info(struct kmem_cache *cache, | |
104 | const void *object); | |
105 | ||
0b24becc AR |
106 | static inline const void *kasan_shadow_to_mem(const void *shadow_addr) |
107 | { | |
108 | return (void *)(((unsigned long)shadow_addr - KASAN_SHADOW_OFFSET) | |
109 | << KASAN_SHADOW_SCALE_SHIFT); | |
110 | } | |
111 | ||
bffa986c AK |
112 | void kasan_poison_shadow(const void *address, size_t size, u8 value); |
113 | ||
114 | void check_memory_region(unsigned long addr, size_t size, bool write, | |
115 | unsigned long ret_ip); | |
116 | ||
0b24becc AR |
117 | void kasan_report(unsigned long addr, size_t size, |
118 | bool is_write, unsigned long ip); | |
ee3ce779 | 119 | void kasan_report_invalid_free(void *object, unsigned long ip); |
0b24becc | 120 | |
2bd926b4 AK |
121 | #if defined(CONFIG_KASAN_GENERIC) && \ |
122 | (defined(CONFIG_SLAB) || defined(CONFIG_SLUB)) | |
55834c59 AP |
123 | void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache); |
124 | void quarantine_reduce(void); | |
125 | void quarantine_remove_cache(struct kmem_cache *cache); | |
126 | #else | |
127 | static inline void quarantine_put(struct kasan_free_meta *info, | |
128 | struct kmem_cache *cache) { } | |
129 | static inline void quarantine_reduce(void) { } | |
130 | static inline void quarantine_remove_cache(struct kmem_cache *cache) { } | |
131 | #endif | |
132 | ||
3c9e3aa1 AK |
133 | #ifdef CONFIG_KASAN_SW_TAGS |
134 | ||
135 | u8 random_tag(void); | |
136 | ||
137 | #else | |
138 | ||
139 | static inline u8 random_tag(void) | |
140 | { | |
141 | return 0; | |
142 | } | |
143 | ||
144 | #endif | |
145 | ||
146 | #ifndef arch_kasan_set_tag | |
147 | #define arch_kasan_set_tag(addr, tag) ((void *)(addr)) | |
148 | #endif | |
149 | #ifndef arch_kasan_reset_tag | |
150 | #define arch_kasan_reset_tag(addr) ((void *)(addr)) | |
151 | #endif | |
152 | #ifndef arch_kasan_get_tag | |
153 | #define arch_kasan_get_tag(addr) 0 | |
154 | #endif | |
155 | ||
156 | #define set_tag(addr, tag) ((void *)arch_kasan_set_tag((addr), (tag))) | |
157 | #define reset_tag(addr) ((void *)arch_kasan_reset_tag(addr)) | |
158 | #define get_tag(addr) arch_kasan_get_tag(addr) | |
159 | ||
d321599c AP |
160 | /* |
161 | * Exported functions for interfaces called from assembly or from generated | |
162 | * code. Declarations here to avoid warning about missing declarations. | |
163 | */ | |
164 | asmlinkage void kasan_unpoison_task_stack_below(const void *watermark); | |
165 | void __asan_register_globals(struct kasan_global *globals, size_t size); | |
166 | void __asan_unregister_globals(struct kasan_global *globals, size_t size); | |
167 | void __asan_loadN(unsigned long addr, size_t size); | |
168 | void __asan_storeN(unsigned long addr, size_t size); | |
169 | void __asan_handle_no_return(void); | |
170 | void __asan_poison_stack_memory(const void *addr, size_t size); | |
171 | void __asan_unpoison_stack_memory(const void *addr, size_t size); | |
172 | void __asan_alloca_poison(unsigned long addr, size_t size); | |
173 | void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom); | |
174 | ||
175 | void __asan_load1(unsigned long addr); | |
176 | void __asan_store1(unsigned long addr); | |
177 | void __asan_load2(unsigned long addr); | |
178 | void __asan_store2(unsigned long addr); | |
179 | void __asan_load4(unsigned long addr); | |
180 | void __asan_store4(unsigned long addr); | |
181 | void __asan_load8(unsigned long addr); | |
182 | void __asan_store8(unsigned long addr); | |
183 | void __asan_load16(unsigned long addr); | |
184 | void __asan_store16(unsigned long addr); | |
185 | ||
186 | void __asan_load1_noabort(unsigned long addr); | |
187 | void __asan_store1_noabort(unsigned long addr); | |
188 | void __asan_load2_noabort(unsigned long addr); | |
189 | void __asan_store2_noabort(unsigned long addr); | |
190 | void __asan_load4_noabort(unsigned long addr); | |
191 | void __asan_store4_noabort(unsigned long addr); | |
192 | void __asan_load8_noabort(unsigned long addr); | |
193 | void __asan_store8_noabort(unsigned long addr); | |
194 | void __asan_load16_noabort(unsigned long addr); | |
195 | void __asan_store16_noabort(unsigned long addr); | |
196 | ||
197 | void __asan_set_shadow_00(const void *addr, size_t size); | |
198 | void __asan_set_shadow_f1(const void *addr, size_t size); | |
199 | void __asan_set_shadow_f2(const void *addr, size_t size); | |
200 | void __asan_set_shadow_f3(const void *addr, size_t size); | |
201 | void __asan_set_shadow_f5(const void *addr, size_t size); | |
202 | void __asan_set_shadow_f8(const void *addr, size_t size); | |
203 | ||
0b24becc | 204 | #endif |