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