Commit | Line | Data |
---|---|---|
0b24becc AR |
1 | /* |
2 | * This file contains error reporting code. | |
3 | * | |
4 | * Copyright (c) 2014 Samsung Electronics Co., Ltd. | |
2baf9e89 | 5 | * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com> |
0b24becc | 6 | * |
5d0926ef | 7 | * Some code borrowed from https://github.com/xairy/kasan-prototype by |
0b24becc AR |
8 | * Andrey Konovalov <adech.fo@gmail.com> |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License version 2 as | |
12 | * published by the Free Software Foundation. | |
13 | * | |
14 | */ | |
15 | ||
16 | #include <linux/kernel.h> | |
17 | #include <linux/mm.h> | |
18 | #include <linux/printk.h> | |
19 | #include <linux/sched.h> | |
20 | #include <linux/slab.h> | |
cd11016e | 21 | #include <linux/stackdepot.h> |
0b24becc AR |
22 | #include <linux/stacktrace.h> |
23 | #include <linux/string.h> | |
24 | #include <linux/types.h> | |
25 | #include <linux/kasan.h> | |
527f215b | 26 | #include <linux/module.h> |
0b24becc | 27 | |
bebf56a1 AR |
28 | #include <asm/sections.h> |
29 | ||
0b24becc | 30 | #include "kasan.h" |
0316bec2 | 31 | #include "../slab.h" |
0b24becc AR |
32 | |
33 | /* Shadow layout customization. */ | |
34 | #define SHADOW_BYTES_PER_BLOCK 1 | |
35 | #define SHADOW_BLOCKS_PER_ROW 16 | |
36 | #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK) | |
37 | #define SHADOW_ROWS_AROUND_ADDR 2 | |
38 | ||
39 | static const void *find_first_bad_addr(const void *addr, size_t size) | |
40 | { | |
41 | u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr); | |
42 | const void *first_bad_addr = addr; | |
43 | ||
44 | while (!shadow_val && first_bad_addr < addr + size) { | |
45 | first_bad_addr += KASAN_SHADOW_SCALE_SIZE; | |
46 | shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr); | |
47 | } | |
48 | return first_bad_addr; | |
49 | } | |
50 | ||
51 | static void print_error_description(struct kasan_access_info *info) | |
52 | { | |
0952d87f | 53 | const char *bug_type = "unknown-crash"; |
cdf6a273 | 54 | u8 *shadow_addr; |
0b24becc AR |
55 | |
56 | info->first_bad_addr = find_first_bad_addr(info->access_addr, | |
57 | info->access_size); | |
58 | ||
cdf6a273 | 59 | shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr); |
0b24becc | 60 | |
cdf6a273 AK |
61 | /* |
62 | * If shadow byte value is in [0, KASAN_SHADOW_SCALE_SIZE) we can look | |
63 | * at the next shadow byte to determine the type of the bad access. | |
64 | */ | |
65 | if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1) | |
66 | shadow_addr++; | |
67 | ||
68 | switch (*shadow_addr) { | |
0952d87f | 69 | case 0 ... KASAN_SHADOW_SCALE_SIZE - 1: |
cdf6a273 AK |
70 | /* |
71 | * In theory it's still possible to see these shadow values | |
72 | * due to a data race in the kernel code. | |
73 | */ | |
0952d87f | 74 | bug_type = "out-of-bounds"; |
b8c73fc2 | 75 | break; |
0316bec2 AR |
76 | case KASAN_PAGE_REDZONE: |
77 | case KASAN_KMALLOC_REDZONE: | |
0952d87f AK |
78 | bug_type = "slab-out-of-bounds"; |
79 | break; | |
bebf56a1 | 80 | case KASAN_GLOBAL_REDZONE: |
0952d87f | 81 | bug_type = "global-out-of-bounds"; |
0b24becc | 82 | break; |
c420f167 AR |
83 | case KASAN_STACK_LEFT: |
84 | case KASAN_STACK_MID: | |
85 | case KASAN_STACK_RIGHT: | |
86 | case KASAN_STACK_PARTIAL: | |
0952d87f AK |
87 | bug_type = "stack-out-of-bounds"; |
88 | break; | |
89 | case KASAN_FREE_PAGE: | |
90 | case KASAN_KMALLOC_FREE: | |
91 | bug_type = "use-after-free"; | |
c420f167 | 92 | break; |
0b24becc AR |
93 | } |
94 | ||
25add7ec | 95 | pr_err("BUG: KASAN: %s in %pS at addr %p\n", |
0b24becc AR |
96 | bug_type, (void *)info->ip, |
97 | info->access_addr); | |
98 | pr_err("%s of size %zu by task %s/%d\n", | |
99 | info->is_write ? "Write" : "Read", | |
100 | info->access_size, current->comm, task_pid_nr(current)); | |
101 | } | |
102 | ||
bebf56a1 AR |
103 | static inline bool kernel_or_module_addr(const void *addr) |
104 | { | |
527f215b AK |
105 | if (addr >= (void *)_stext && addr < (void *)_end) |
106 | return true; | |
107 | if (is_module_address((unsigned long)addr)) | |
108 | return true; | |
109 | return false; | |
bebf56a1 AR |
110 | } |
111 | ||
112 | static inline bool init_task_stack_addr(const void *addr) | |
113 | { | |
114 | return addr >= (void *)&init_thread_union.stack && | |
115 | (addr <= (void *)&init_thread_union.stack + | |
116 | sizeof(init_thread_union.stack)); | |
117 | } | |
118 | ||
7ed2f9e6 AP |
119 | #ifdef CONFIG_SLAB |
120 | static void print_track(struct kasan_track *track) | |
121 | { | |
cd11016e AP |
122 | pr_err("PID = %u\n", track->pid); |
123 | if (track->stack) { | |
124 | struct stack_trace trace; | |
125 | ||
126 | depot_fetch_stack(track->stack, &trace); | |
127 | print_stack_trace(&trace, 0); | |
128 | } else { | |
129 | pr_err("(stack is not available)\n"); | |
130 | } | |
7ed2f9e6 AP |
131 | } |
132 | ||
133 | static void object_err(struct kmem_cache *cache, struct page *page, | |
134 | void *object, char *unused_reason) | |
135 | { | |
136 | struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object); | |
137 | struct kasan_free_meta *free_info; | |
138 | ||
139 | dump_stack(); | |
140 | pr_err("Object at %p, in cache %s\n", object, cache->name); | |
141 | if (!(cache->flags & SLAB_KASAN)) | |
142 | return; | |
143 | switch (alloc_info->state) { | |
144 | case KASAN_STATE_INIT: | |
145 | pr_err("Object not allocated yet\n"); | |
146 | break; | |
147 | case KASAN_STATE_ALLOC: | |
148 | pr_err("Object allocated with size %u bytes.\n", | |
149 | alloc_info->alloc_size); | |
150 | pr_err("Allocation:\n"); | |
151 | print_track(&alloc_info->track); | |
152 | break; | |
153 | case KASAN_STATE_FREE: | |
55834c59 | 154 | case KASAN_STATE_QUARANTINE: |
7ed2f9e6 AP |
155 | pr_err("Object freed, allocated with size %u bytes\n", |
156 | alloc_info->alloc_size); | |
157 | free_info = get_free_info(cache, object); | |
158 | pr_err("Allocation:\n"); | |
159 | print_track(&alloc_info->track); | |
160 | pr_err("Deallocation:\n"); | |
161 | print_track(&free_info->track); | |
162 | break; | |
163 | } | |
164 | } | |
165 | #endif | |
166 | ||
0b24becc AR |
167 | static void print_address_description(struct kasan_access_info *info) |
168 | { | |
b8c73fc2 AR |
169 | const void *addr = info->access_addr; |
170 | ||
171 | if ((addr >= (void *)PAGE_OFFSET) && | |
172 | (addr < high_memory)) { | |
173 | struct page *page = virt_to_head_page(addr); | |
0316bec2 AR |
174 | |
175 | if (PageSlab(page)) { | |
176 | void *object; | |
177 | struct kmem_cache *cache = page->slab_cache; | |
7ed2f9e6 AP |
178 | object = nearest_obj(cache, page, |
179 | (void *)info->access_addr); | |
0316bec2 | 180 | object_err(cache, page, object, |
7ed2f9e6 | 181 | "kasan: bad access detected"); |
0316bec2 AR |
182 | return; |
183 | } | |
b8c73fc2 AR |
184 | dump_page(page, "kasan: bad access detected"); |
185 | } | |
186 | ||
bebf56a1 AR |
187 | if (kernel_or_module_addr(addr)) { |
188 | if (!init_task_stack_addr(addr)) | |
189 | pr_err("Address belongs to variable %pS\n", addr); | |
190 | } | |
0b24becc AR |
191 | dump_stack(); |
192 | } | |
193 | ||
194 | static bool row_is_guilty(const void *row, const void *guilty) | |
195 | { | |
196 | return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW); | |
197 | } | |
198 | ||
199 | static int shadow_pointer_offset(const void *row, const void *shadow) | |
200 | { | |
201 | /* The length of ">ff00ff00ff00ff00: " is | |
202 | * 3 + (BITS_PER_LONG/8)*2 chars. | |
203 | */ | |
204 | return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 + | |
205 | (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1; | |
206 | } | |
207 | ||
208 | static void print_shadow_for_address(const void *addr) | |
209 | { | |
210 | int i; | |
211 | const void *shadow = kasan_mem_to_shadow(addr); | |
212 | const void *shadow_row; | |
213 | ||
214 | shadow_row = (void *)round_down((unsigned long)shadow, | |
215 | SHADOW_BYTES_PER_ROW) | |
216 | - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW; | |
217 | ||
218 | pr_err("Memory state around the buggy address:\n"); | |
219 | ||
220 | for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) { | |
221 | const void *kaddr = kasan_shadow_to_mem(shadow_row); | |
222 | char buffer[4 + (BITS_PER_LONG/8)*2]; | |
f2377d4e | 223 | char shadow_buf[SHADOW_BYTES_PER_ROW]; |
0b24becc AR |
224 | |
225 | snprintf(buffer, sizeof(buffer), | |
226 | (i == 0) ? ">%p: " : " %p: ", kaddr); | |
f2377d4e AK |
227 | /* |
228 | * We should not pass a shadow pointer to generic | |
229 | * function, because generic functions may try to | |
230 | * access kasan mapping for the passed address. | |
231 | */ | |
f2377d4e | 232 | memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW); |
0b24becc AR |
233 | print_hex_dump(KERN_ERR, buffer, |
234 | DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1, | |
f2377d4e | 235 | shadow_buf, SHADOW_BYTES_PER_ROW, 0); |
0b24becc AR |
236 | |
237 | if (row_is_guilty(shadow_row, shadow)) | |
238 | pr_err("%*c\n", | |
239 | shadow_pointer_offset(shadow_row, shadow), | |
240 | '^'); | |
241 | ||
242 | shadow_row += SHADOW_BYTES_PER_ROW; | |
243 | } | |
244 | } | |
245 | ||
246 | static DEFINE_SPINLOCK(report_lock); | |
247 | ||
e9121076 | 248 | static void kasan_report_error(struct kasan_access_info *info) |
0b24becc AR |
249 | { |
250 | unsigned long flags; | |
e9121076 | 251 | const char *bug_type; |
0b24becc | 252 | |
fc5aeeaf AK |
253 | /* |
254 | * Make sure we don't end up in loop. | |
255 | */ | |
256 | kasan_disable_current(); | |
0b24becc | 257 | spin_lock_irqsave(&report_lock, flags); |
756a025f | 258 | pr_err("==================================================================\n"); |
e9121076 AK |
259 | if (info->access_addr < |
260 | kasan_shadow_to_mem((void *)KASAN_SHADOW_START)) { | |
261 | if ((unsigned long)info->access_addr < PAGE_SIZE) | |
262 | bug_type = "null-ptr-deref"; | |
263 | else if ((unsigned long)info->access_addr < TASK_SIZE) | |
264 | bug_type = "user-memory-access"; | |
265 | else | |
266 | bug_type = "wild-memory-access"; | |
25add7ec | 267 | pr_err("BUG: KASAN: %s on address %p\n", |
e9121076 AK |
268 | bug_type, info->access_addr); |
269 | pr_err("%s of size %zu by task %s/%d\n", | |
270 | info->is_write ? "Write" : "Read", | |
271 | info->access_size, current->comm, | |
272 | task_pid_nr(current)); | |
273 | dump_stack(); | |
274 | } else { | |
275 | print_error_description(info); | |
276 | print_address_description(info); | |
277 | print_shadow_for_address(info->first_bad_addr); | |
278 | } | |
756a025f | 279 | pr_err("==================================================================\n"); |
eb06f43f | 280 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
0b24becc | 281 | spin_unlock_irqrestore(&report_lock, flags); |
fc5aeeaf | 282 | kasan_enable_current(); |
0b24becc AR |
283 | } |
284 | ||
285 | void kasan_report(unsigned long addr, size_t size, | |
286 | bool is_write, unsigned long ip) | |
287 | { | |
288 | struct kasan_access_info info; | |
289 | ||
0ba8663c | 290 | if (likely(!kasan_report_enabled())) |
0b24becc AR |
291 | return; |
292 | ||
293 | info.access_addr = (void *)addr; | |
294 | info.access_size = size; | |
295 | info.is_write = is_write; | |
296 | info.ip = ip; | |
e9121076 | 297 | |
0b24becc AR |
298 | kasan_report_error(&info); |
299 | } | |
300 | ||
301 | ||
302 | #define DEFINE_ASAN_REPORT_LOAD(size) \ | |
303 | void __asan_report_load##size##_noabort(unsigned long addr) \ | |
304 | { \ | |
305 | kasan_report(addr, size, false, _RET_IP_); \ | |
306 | } \ | |
307 | EXPORT_SYMBOL(__asan_report_load##size##_noabort) | |
308 | ||
309 | #define DEFINE_ASAN_REPORT_STORE(size) \ | |
310 | void __asan_report_store##size##_noabort(unsigned long addr) \ | |
311 | { \ | |
312 | kasan_report(addr, size, true, _RET_IP_); \ | |
313 | } \ | |
314 | EXPORT_SYMBOL(__asan_report_store##size##_noabort) | |
315 | ||
316 | DEFINE_ASAN_REPORT_LOAD(1); | |
317 | DEFINE_ASAN_REPORT_LOAD(2); | |
318 | DEFINE_ASAN_REPORT_LOAD(4); | |
319 | DEFINE_ASAN_REPORT_LOAD(8); | |
320 | DEFINE_ASAN_REPORT_LOAD(16); | |
321 | DEFINE_ASAN_REPORT_STORE(1); | |
322 | DEFINE_ASAN_REPORT_STORE(2); | |
323 | DEFINE_ASAN_REPORT_STORE(4); | |
324 | DEFINE_ASAN_REPORT_STORE(8); | |
325 | DEFINE_ASAN_REPORT_STORE(16); | |
326 | ||
327 | void __asan_report_load_n_noabort(unsigned long addr, size_t size) | |
328 | { | |
329 | kasan_report(addr, size, false, _RET_IP_); | |
330 | } | |
331 | EXPORT_SYMBOL(__asan_report_load_n_noabort); | |
332 | ||
333 | void __asan_report_store_n_noabort(unsigned long addr, size_t size) | |
334 | { | |
335 | kasan_report(addr, size, true, _RET_IP_); | |
336 | } | |
337 | EXPORT_SYMBOL(__asan_report_store_n_noabort); |