]>
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 <[email protected]> |
0b24becc AR |
6 | * |
7 | * Some of code borrowed from https://github.com/xairy/linux by | |
8 | * Andrey Konovalov <[email protected]> | |
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> | |
21 | #include <linux/stacktrace.h> | |
22 | #include <linux/string.h> | |
23 | #include <linux/types.h> | |
24 | #include <linux/kasan.h> | |
527f215b | 25 | #include <linux/module.h> |
0b24becc | 26 | |
bebf56a1 AR |
27 | #include <asm/sections.h> |
28 | ||
0b24becc | 29 | #include "kasan.h" |
0316bec2 | 30 | #include "../slab.h" |
0b24becc AR |
31 | |
32 | /* Shadow layout customization. */ | |
33 | #define SHADOW_BYTES_PER_BLOCK 1 | |
34 | #define SHADOW_BLOCKS_PER_ROW 16 | |
35 | #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK) | |
36 | #define SHADOW_ROWS_AROUND_ADDR 2 | |
37 | ||
38 | static const void *find_first_bad_addr(const void *addr, size_t size) | |
39 | { | |
40 | u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr); | |
41 | const void *first_bad_addr = addr; | |
42 | ||
43 | while (!shadow_val && first_bad_addr < addr + size) { | |
44 | first_bad_addr += KASAN_SHADOW_SCALE_SIZE; | |
45 | shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr); | |
46 | } | |
47 | return first_bad_addr; | |
48 | } | |
49 | ||
50 | static void print_error_description(struct kasan_access_info *info) | |
51 | { | |
52 | const char *bug_type = "unknown crash"; | |
53 | u8 shadow_val; | |
54 | ||
55 | info->first_bad_addr = find_first_bad_addr(info->access_addr, | |
56 | info->access_size); | |
57 | ||
58 | shadow_val = *(u8 *)kasan_mem_to_shadow(info->first_bad_addr); | |
59 | ||
60 | switch (shadow_val) { | |
b8c73fc2 | 61 | case KASAN_FREE_PAGE: |
0316bec2 | 62 | case KASAN_KMALLOC_FREE: |
b8c73fc2 AR |
63 | bug_type = "use after free"; |
64 | break; | |
0316bec2 AR |
65 | case KASAN_PAGE_REDZONE: |
66 | case KASAN_KMALLOC_REDZONE: | |
bebf56a1 | 67 | case KASAN_GLOBAL_REDZONE: |
0b24becc AR |
68 | case 0 ... KASAN_SHADOW_SCALE_SIZE - 1: |
69 | bug_type = "out of bounds access"; | |
70 | break; | |
c420f167 AR |
71 | case KASAN_STACK_LEFT: |
72 | case KASAN_STACK_MID: | |
73 | case KASAN_STACK_RIGHT: | |
74 | case KASAN_STACK_PARTIAL: | |
75 | bug_type = "out of bounds on stack"; | |
76 | break; | |
0b24becc AR |
77 | } |
78 | ||
79 | pr_err("BUG: KASan: %s in %pS at addr %p\n", | |
80 | bug_type, (void *)info->ip, | |
81 | info->access_addr); | |
82 | pr_err("%s of size %zu by task %s/%d\n", | |
83 | info->is_write ? "Write" : "Read", | |
84 | info->access_size, current->comm, task_pid_nr(current)); | |
85 | } | |
86 | ||
bebf56a1 AR |
87 | static inline bool kernel_or_module_addr(const void *addr) |
88 | { | |
527f215b AK |
89 | if (addr >= (void *)_stext && addr < (void *)_end) |
90 | return true; | |
91 | if (is_module_address((unsigned long)addr)) | |
92 | return true; | |
93 | return false; | |
bebf56a1 AR |
94 | } |
95 | ||
96 | static inline bool init_task_stack_addr(const void *addr) | |
97 | { | |
98 | return addr >= (void *)&init_thread_union.stack && | |
99 | (addr <= (void *)&init_thread_union.stack + | |
100 | sizeof(init_thread_union.stack)); | |
101 | } | |
102 | ||
0b24becc AR |
103 | static void print_address_description(struct kasan_access_info *info) |
104 | { | |
b8c73fc2 AR |
105 | const void *addr = info->access_addr; |
106 | ||
107 | if ((addr >= (void *)PAGE_OFFSET) && | |
108 | (addr < high_memory)) { | |
109 | struct page *page = virt_to_head_page(addr); | |
0316bec2 AR |
110 | |
111 | if (PageSlab(page)) { | |
112 | void *object; | |
113 | struct kmem_cache *cache = page->slab_cache; | |
114 | void *last_object; | |
115 | ||
116 | object = virt_to_obj(cache, page_address(page), addr); | |
117 | last_object = page_address(page) + | |
118 | page->objects * cache->size; | |
119 | ||
120 | if (unlikely(object > last_object)) | |
121 | object = last_object; /* we hit into padding */ | |
122 | ||
123 | object_err(cache, page, object, | |
124 | "kasan: bad access detected"); | |
125 | return; | |
126 | } | |
b8c73fc2 AR |
127 | dump_page(page, "kasan: bad access detected"); |
128 | } | |
129 | ||
bebf56a1 AR |
130 | if (kernel_or_module_addr(addr)) { |
131 | if (!init_task_stack_addr(addr)) | |
132 | pr_err("Address belongs to variable %pS\n", addr); | |
133 | } | |
134 | ||
0b24becc AR |
135 | dump_stack(); |
136 | } | |
137 | ||
138 | static bool row_is_guilty(const void *row, const void *guilty) | |
139 | { | |
140 | return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW); | |
141 | } | |
142 | ||
143 | static int shadow_pointer_offset(const void *row, const void *shadow) | |
144 | { | |
145 | /* The length of ">ff00ff00ff00ff00: " is | |
146 | * 3 + (BITS_PER_LONG/8)*2 chars. | |
147 | */ | |
148 | return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 + | |
149 | (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1; | |
150 | } | |
151 | ||
152 | static void print_shadow_for_address(const void *addr) | |
153 | { | |
154 | int i; | |
155 | const void *shadow = kasan_mem_to_shadow(addr); | |
156 | const void *shadow_row; | |
157 | ||
158 | shadow_row = (void *)round_down((unsigned long)shadow, | |
159 | SHADOW_BYTES_PER_ROW) | |
160 | - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW; | |
161 | ||
162 | pr_err("Memory state around the buggy address:\n"); | |
163 | ||
164 | for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) { | |
165 | const void *kaddr = kasan_shadow_to_mem(shadow_row); | |
166 | char buffer[4 + (BITS_PER_LONG/8)*2]; | |
f2377d4e | 167 | char shadow_buf[SHADOW_BYTES_PER_ROW]; |
0b24becc AR |
168 | |
169 | snprintf(buffer, sizeof(buffer), | |
170 | (i == 0) ? ">%p: " : " %p: ", kaddr); | |
f2377d4e AK |
171 | /* |
172 | * We should not pass a shadow pointer to generic | |
173 | * function, because generic functions may try to | |
174 | * access kasan mapping for the passed address. | |
175 | */ | |
f2377d4e | 176 | memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW); |
0b24becc AR |
177 | print_hex_dump(KERN_ERR, buffer, |
178 | DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1, | |
f2377d4e | 179 | shadow_buf, SHADOW_BYTES_PER_ROW, 0); |
0b24becc AR |
180 | |
181 | if (row_is_guilty(shadow_row, shadow)) | |
182 | pr_err("%*c\n", | |
183 | shadow_pointer_offset(shadow_row, shadow), | |
184 | '^'); | |
185 | ||
186 | shadow_row += SHADOW_BYTES_PER_ROW; | |
187 | } | |
188 | } | |
189 | ||
190 | static DEFINE_SPINLOCK(report_lock); | |
191 | ||
e9121076 | 192 | static void kasan_report_error(struct kasan_access_info *info) |
0b24becc AR |
193 | { |
194 | unsigned long flags; | |
e9121076 | 195 | const char *bug_type; |
0b24becc | 196 | |
fc5aeeaf AK |
197 | /* |
198 | * Make sure we don't end up in loop. | |
199 | */ | |
200 | kasan_disable_current(); | |
0b24becc AR |
201 | spin_lock_irqsave(&report_lock, flags); |
202 | pr_err("=================================" | |
203 | "=================================\n"); | |
e9121076 AK |
204 | if (info->access_addr < |
205 | kasan_shadow_to_mem((void *)KASAN_SHADOW_START)) { | |
206 | if ((unsigned long)info->access_addr < PAGE_SIZE) | |
207 | bug_type = "null-ptr-deref"; | |
208 | else if ((unsigned long)info->access_addr < TASK_SIZE) | |
209 | bug_type = "user-memory-access"; | |
210 | else | |
211 | bug_type = "wild-memory-access"; | |
212 | pr_err("BUG: KASan: %s on address %p\n", | |
213 | bug_type, info->access_addr); | |
214 | pr_err("%s of size %zu by task %s/%d\n", | |
215 | info->is_write ? "Write" : "Read", | |
216 | info->access_size, current->comm, | |
217 | task_pid_nr(current)); | |
218 | dump_stack(); | |
219 | } else { | |
220 | print_error_description(info); | |
221 | print_address_description(info); | |
222 | print_shadow_for_address(info->first_bad_addr); | |
223 | } | |
0b24becc AR |
224 | pr_err("=================================" |
225 | "=================================\n"); | |
226 | spin_unlock_irqrestore(&report_lock, flags); | |
fc5aeeaf | 227 | kasan_enable_current(); |
0b24becc AR |
228 | } |
229 | ||
230 | void kasan_report(unsigned long addr, size_t size, | |
231 | bool is_write, unsigned long ip) | |
232 | { | |
233 | struct kasan_access_info info; | |
234 | ||
0ba8663c | 235 | if (likely(!kasan_report_enabled())) |
0b24becc AR |
236 | return; |
237 | ||
238 | info.access_addr = (void *)addr; | |
239 | info.access_size = size; | |
240 | info.is_write = is_write; | |
241 | info.ip = ip; | |
e9121076 | 242 | |
0b24becc AR |
243 | kasan_report_error(&info); |
244 | } | |
245 | ||
246 | ||
247 | #define DEFINE_ASAN_REPORT_LOAD(size) \ | |
248 | void __asan_report_load##size##_noabort(unsigned long addr) \ | |
249 | { \ | |
250 | kasan_report(addr, size, false, _RET_IP_); \ | |
251 | } \ | |
252 | EXPORT_SYMBOL(__asan_report_load##size##_noabort) | |
253 | ||
254 | #define DEFINE_ASAN_REPORT_STORE(size) \ | |
255 | void __asan_report_store##size##_noabort(unsigned long addr) \ | |
256 | { \ | |
257 | kasan_report(addr, size, true, _RET_IP_); \ | |
258 | } \ | |
259 | EXPORT_SYMBOL(__asan_report_store##size##_noabort) | |
260 | ||
261 | DEFINE_ASAN_REPORT_LOAD(1); | |
262 | DEFINE_ASAN_REPORT_LOAD(2); | |
263 | DEFINE_ASAN_REPORT_LOAD(4); | |
264 | DEFINE_ASAN_REPORT_LOAD(8); | |
265 | DEFINE_ASAN_REPORT_LOAD(16); | |
266 | DEFINE_ASAN_REPORT_STORE(1); | |
267 | DEFINE_ASAN_REPORT_STORE(2); | |
268 | DEFINE_ASAN_REPORT_STORE(4); | |
269 | DEFINE_ASAN_REPORT_STORE(8); | |
270 | DEFINE_ASAN_REPORT_STORE(16); | |
271 | ||
272 | void __asan_report_load_n_noabort(unsigned long addr, size_t size) | |
273 | { | |
274 | kasan_report(addr, size, false, _RET_IP_); | |
275 | } | |
276 | EXPORT_SYMBOL(__asan_report_load_n_noabort); | |
277 | ||
278 | void __asan_report_store_n_noabort(unsigned long addr, size_t size) | |
279 | { | |
280 | kasan_report(addr, size, true, _RET_IP_); | |
281 | } | |
282 | EXPORT_SYMBOL(__asan_report_store_n_noabort); |