blob: 5cbd2de2b1e8bdf7ee790e5ba6b6f6f478f02c83 [file] [log] [blame]
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08001/*
2 * This file contains error reporting code.
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
Andrey Ryabinin2baf9e82015-08-14 15:35:13 -07005 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08006 *
Andrey Konovalov5d0926e2015-11-05 18:51:12 -08007 * Some code borrowed from https://github.com/xairy/kasan-prototype by
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08008 * 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
Peter Zijlstra5331baa2017-05-23 21:53:57 -040016#include <linux/ftrace.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080017#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/printk.h>
20#include <linux/sched.h>
21#include <linux/slab.h>
Alexander Potapenkocd110162016-03-25 14:22:08 -070022#include <linux/stackdepot.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080023#include <linux/stacktrace.h>
24#include <linux/string.h>
25#include <linux/types.h>
26#include <linux/kasan.h>
Aneesh Kumar K.V527f2152015-11-05 18:50:46 -080027#include <linux/module.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080028
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080029#include <asm/sections.h>
30
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080031#include "kasan.h"
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080032#include "../slab.h"
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080033
34/* Shadow layout customization. */
35#define SHADOW_BYTES_PER_BLOCK 1
36#define SHADOW_BLOCKS_PER_ROW 16
37#define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK)
38#define SHADOW_ROWS_AROUND_ADDR 2
39
40static const void *find_first_bad_addr(const void *addr, size_t size)
41{
42 u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr);
43 const void *first_bad_addr = addr;
44
45 while (!shadow_val && first_bad_addr < addr + size) {
46 first_bad_addr += KASAN_SHADOW_SCALE_SIZE;
47 shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr);
48 }
49 return first_bad_addr;
50}
51
Andrey Konovalov9316eb72017-05-03 14:56:25 -070052static bool addr_has_shadow(struct kasan_access_info *info)
53{
54 return (info->access_addr >=
55 kasan_shadow_to_mem((void *)KASAN_SHADOW_START));
56}
57
58static const char *get_shadow_bug_type(struct kasan_access_info *info)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080059{
Andrey Konovalov0952d872015-11-05 18:50:58 -080060 const char *bug_type = "unknown-crash";
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080061 u8 *shadow_addr;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080062
63 info->first_bad_addr = find_first_bad_addr(info->access_addr,
64 info->access_size);
65
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080066 shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080067
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080068 /*
69 * If shadow byte value is in [0, KASAN_SHADOW_SCALE_SIZE) we can look
70 * at the next shadow byte to determine the type of the bad access.
71 */
72 if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1)
73 shadow_addr++;
74
75 switch (*shadow_addr) {
Andrey Konovalov0952d872015-11-05 18:50:58 -080076 case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080077 /*
78 * In theory it's still possible to see these shadow values
79 * due to a data race in the kernel code.
80 */
Andrey Konovalov0952d872015-11-05 18:50:58 -080081 bug_type = "out-of-bounds";
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080082 break;
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080083 case KASAN_PAGE_REDZONE:
84 case KASAN_KMALLOC_REDZONE:
Andrey Konovalov0952d872015-11-05 18:50:58 -080085 bug_type = "slab-out-of-bounds";
86 break;
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080087 case KASAN_GLOBAL_REDZONE:
Andrey Konovalov0952d872015-11-05 18:50:58 -080088 bug_type = "global-out-of-bounds";
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080089 break;
Andrey Ryabininc420f162015-02-13 14:39:59 -080090 case KASAN_STACK_LEFT:
91 case KASAN_STACK_MID:
92 case KASAN_STACK_RIGHT:
93 case KASAN_STACK_PARTIAL:
Andrey Konovalov0952d872015-11-05 18:50:58 -080094 bug_type = "stack-out-of-bounds";
95 break;
96 case KASAN_FREE_PAGE:
97 case KASAN_KMALLOC_FREE:
98 bug_type = "use-after-free";
Andrey Ryabininc420f162015-02-13 14:39:59 -080099 break;
Dmitry Vyukov828347f2016-11-30 15:54:16 -0800100 case KASAN_USE_AFTER_SCOPE:
101 bug_type = "use-after-scope";
102 break;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800103 }
104
Andrey Konovalov9316eb72017-05-03 14:56:25 -0700105 return bug_type;
106}
107
108const char *get_wild_bug_type(struct kasan_access_info *info)
109{
110 const char *bug_type = "unknown-crash";
111
112 if ((unsigned long)info->access_addr < PAGE_SIZE)
113 bug_type = "null-ptr-deref";
114 else if ((unsigned long)info->access_addr < TASK_SIZE)
115 bug_type = "user-memory-access";
116 else
117 bug_type = "wild-memory-access";
118
119 return bug_type;
120}
121
Andrey Konovalov294a4862017-05-03 14:56:28 -0700122static const char *get_bug_type(struct kasan_access_info *info)
123{
124 if (addr_has_shadow(info))
125 return get_shadow_bug_type(info);
126 return get_wild_bug_type(info);
127}
128
Andrey Konovalov9316eb72017-05-03 14:56:25 -0700129static void print_error_description(struct kasan_access_info *info)
130{
Andrey Konovalov294a4862017-05-03 14:56:28 -0700131 const char *bug_type = get_bug_type(info);
Andrey Konovalov9316eb72017-05-03 14:56:25 -0700132
Andrey Konovalovf7bf9a22017-05-03 14:56:38 -0700133 pr_err("BUG: KASAN: %s in %pS\n",
134 bug_type, (void *)info->ip);
135 pr_err("%s of size %zu at addr %p by task %s/%d\n",
Andrey Konovalov294a4862017-05-03 14:56:28 -0700136 info->is_write ? "Write" : "Read", info->access_size,
Andrey Konovalovf7bf9a22017-05-03 14:56:38 -0700137 info->access_addr, current->comm, task_pid_nr(current));
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800138}
139
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800140static inline bool kernel_or_module_addr(const void *addr)
141{
Aneesh Kumar K.V527f2152015-11-05 18:50:46 -0800142 if (addr >= (void *)_stext && addr < (void *)_end)
143 return true;
144 if (is_module_address((unsigned long)addr))
145 return true;
146 return false;
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800147}
148
149static inline bool init_task_stack_addr(const void *addr)
150{
151 return addr >= (void *)&init_thread_union.stack &&
152 (addr <= (void *)&init_thread_union.stack +
153 sizeof(init_thread_union.stack));
154}
155
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700156static DEFINE_SPINLOCK(report_lock);
157
158static void kasan_start_report(unsigned long *flags)
159{
160 /*
161 * Make sure we don't end up in loop.
162 */
163 kasan_disable_current();
164 spin_lock_irqsave(&report_lock, *flags);
165 pr_err("==================================================================\n");
166}
167
168static void kasan_end_report(unsigned long *flags)
169{
170 pr_err("==================================================================\n");
171 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
172 spin_unlock_irqrestore(&report_lock, *flags);
173 kasan_enable_current();
174}
175
Andrey Konovalove6275c42017-05-03 14:56:31 -0700176static void print_track(struct kasan_track *track, const char *prefix)
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700177{
Andrey Konovalove6275c42017-05-03 14:56:31 -0700178 pr_err("%s by task %u:\n", prefix, track->pid);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700179 if (track->stack) {
180 struct stack_trace trace;
181
182 depot_fetch_stack(track->stack, &trace);
183 print_stack_trace(&trace, 0);
184 } else {
185 pr_err("(stack is not available)\n");
186 }
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700187}
188
Andrey Konovalov047bc042017-05-03 14:56:34 -0700189static struct page *addr_to_page(const void *addr)
190{
191 if ((addr >= (void *)PAGE_OFFSET) &&
192 (addr < high_memory))
193 return virt_to_head_page(addr);
194 return NULL;
195}
196
Andrey Konovalov1be25182017-05-03 14:56:41 -0700197static void describe_object_addr(struct kmem_cache *cache, void *object,
198 const void *addr)
199{
200 unsigned long access_addr = (unsigned long)addr;
201 unsigned long object_addr = (unsigned long)object;
202 const char *rel_type;
203 int rel_bytes;
204
205 pr_err("The buggy address belongs to the object at %p\n"
206 " which belongs to the cache %s of size %d\n",
207 object, cache->name, cache->object_size);
208
209 if (!addr)
210 return;
211
212 if (access_addr < object_addr) {
213 rel_type = "to the left";
214 rel_bytes = object_addr - access_addr;
215 } else if (access_addr >= object_addr + cache->object_size) {
216 rel_type = "to the right";
217 rel_bytes = access_addr - (object_addr + cache->object_size);
218 } else {
219 rel_type = "inside";
220 rel_bytes = access_addr - object_addr;
221 }
222
223 pr_err("The buggy address is located %d bytes %s of\n"
224 " %d-byte region [%p, %p)\n",
225 rel_bytes, rel_type, cache->object_size, (void *)object_addr,
226 (void *)(object_addr + cache->object_size));
227}
228
229static void describe_object(struct kmem_cache *cache, void *object,
230 const void *addr)
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700231{
232 struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700233
Andrey Konovalov1be25182017-05-03 14:56:41 -0700234 if (cache->flags & SLAB_KASAN) {
235 print_track(&alloc_info->alloc_track, "Allocated");
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700236 pr_err("\n");
Andrey Konovalov1be25182017-05-03 14:56:41 -0700237 print_track(&alloc_info->free_track, "Freed");
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700238 pr_err("\n");
Andrey Konovalov1be25182017-05-03 14:56:41 -0700239 }
Andrey Ryabinin47b5c2a2016-08-02 14:02:49 -0700240
Andrey Konovalov1be25182017-05-03 14:56:41 -0700241 describe_object_addr(cache, object, addr);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700242}
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700243
Andrey Konovalovdcbca172017-05-03 14:56:47 -0700244static void print_address_description(void *addr)
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700245{
Andrey Konovalov047bc042017-05-03 14:56:34 -0700246 struct page *page = addr_to_page(addr);
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -0800247
Andrey Konovalov047bc042017-05-03 14:56:34 -0700248 dump_stack();
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700249 pr_err("\n");
Andrey Konovalov047bc042017-05-03 14:56:34 -0700250
251 if (page && PageSlab(page)) {
252 struct kmem_cache *cache = page->slab_cache;
Andrey Konovalov1be25182017-05-03 14:56:41 -0700253 void *object = nearest_obj(cache, page, addr);
Andrey Konovalov047bc042017-05-03 14:56:34 -0700254
Andrey Konovalov1be25182017-05-03 14:56:41 -0700255 describe_object(cache, object, addr);
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -0800256 }
257
Andrey Konovalov95b36162017-05-03 14:56:44 -0700258 if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
259 pr_err("The buggy address belongs to the variable:\n");
260 pr_err(" %pS\n", addr);
261 }
262
263 if (page) {
264 pr_err("The buggy address belongs to the page:\n");
265 dump_page(page, "kasan: bad access detected");
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800266 }
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800267}
268
269static bool row_is_guilty(const void *row, const void *guilty)
270{
271 return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
272}
273
274static int shadow_pointer_offset(const void *row, const void *shadow)
275{
276 /* The length of ">ff00ff00ff00ff00: " is
277 * 3 + (BITS_PER_LONG/8)*2 chars.
278 */
279 return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
280 (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
281}
282
283static void print_shadow_for_address(const void *addr)
284{
285 int i;
286 const void *shadow = kasan_mem_to_shadow(addr);
287 const void *shadow_row;
288
289 shadow_row = (void *)round_down((unsigned long)shadow,
290 SHADOW_BYTES_PER_ROW)
291 - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
292
293 pr_err("Memory state around the buggy address:\n");
294
295 for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
296 const void *kaddr = kasan_shadow_to_mem(shadow_row);
297 char buffer[4 + (BITS_PER_LONG/8)*2];
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800298 char shadow_buf[SHADOW_BYTES_PER_ROW];
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800299
300 snprintf(buffer, sizeof(buffer),
301 (i == 0) ? ">%p: " : " %p: ", kaddr);
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800302 /*
303 * We should not pass a shadow pointer to generic
304 * function, because generic functions may try to
305 * access kasan mapping for the passed address.
306 */
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800307 memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800308 print_hex_dump(KERN_ERR, buffer,
309 DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800310 shadow_buf, SHADOW_BYTES_PER_ROW, 0);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800311
312 if (row_is_guilty(shadow_row, shadow))
313 pr_err("%*c\n",
314 shadow_pointer_offset(shadow_row, shadow),
315 '^');
316
317 shadow_row += SHADOW_BYTES_PER_ROW;
318 }
319}
320
Andrey Konovalovdcbca172017-05-03 14:56:47 -0700321void kasan_report_double_free(struct kmem_cache *cache, void *object,
322 void *ip)
323{
324 unsigned long flags;
325
326 kasan_start_report(&flags);
327 pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", ip);
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700328 pr_err("\n");
Andrey Konovalovdcbca172017-05-03 14:56:47 -0700329 print_address_description(object);
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700330 pr_err("\n");
Andrey Konovalovdcbca172017-05-03 14:56:47 -0700331 print_shadow_for_address(object);
332 kasan_end_report(&flags);
333}
334
Andrey Konovalove91210762015-11-05 18:50:55 -0800335static void kasan_report_error(struct kasan_access_info *info)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800336{
337 unsigned long flags;
338
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700339 kasan_start_report(&flags);
340
Andrey Konovalov294a4862017-05-03 14:56:28 -0700341 print_error_description(info);
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700342 pr_err("\n");
Andrey Konovalov294a4862017-05-03 14:56:28 -0700343
Andrey Konovalov9316eb72017-05-03 14:56:25 -0700344 if (!addr_has_shadow(info)) {
Andrey Konovalove91210762015-11-05 18:50:55 -0800345 dump_stack();
346 } else {
Andrey Konovalovdcbca172017-05-03 14:56:47 -0700347 print_address_description((void *)info->access_addr);
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700348 pr_err("\n");
Andrey Konovalove91210762015-11-05 18:50:55 -0800349 print_shadow_for_address(info->first_bad_addr);
350 }
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700351
352 kasan_end_report(&flags);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800353}
354
355void kasan_report(unsigned long addr, size_t size,
356 bool is_write, unsigned long ip)
357{
358 struct kasan_access_info info;
359
Aneesh Kumar K.V0ba86632015-11-05 18:50:43 -0800360 if (likely(!kasan_report_enabled()))
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800361 return;
362
Peter Zijlstra5331baa2017-05-23 21:53:57 -0400363 disable_trace_on_warning();
364
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800365 info.access_addr = (void *)addr;
366 info.access_size = size;
367 info.is_write = is_write;
368 info.ip = ip;
Sridhar Parasuram096551a2017-07-10 12:57:55 -0700369 info.first_bad_addr = NULL;
Andrey Konovalove91210762015-11-05 18:50:55 -0800370
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800371 kasan_report_error(&info);
372}
373
374
375#define DEFINE_ASAN_REPORT_LOAD(size) \
376void __asan_report_load##size##_noabort(unsigned long addr) \
377{ \
378 kasan_report(addr, size, false, _RET_IP_); \
379} \
380EXPORT_SYMBOL(__asan_report_load##size##_noabort)
381
382#define DEFINE_ASAN_REPORT_STORE(size) \
383void __asan_report_store##size##_noabort(unsigned long addr) \
384{ \
385 kasan_report(addr, size, true, _RET_IP_); \
386} \
387EXPORT_SYMBOL(__asan_report_store##size##_noabort)
388
389DEFINE_ASAN_REPORT_LOAD(1);
390DEFINE_ASAN_REPORT_LOAD(2);
391DEFINE_ASAN_REPORT_LOAD(4);
392DEFINE_ASAN_REPORT_LOAD(8);
393DEFINE_ASAN_REPORT_LOAD(16);
394DEFINE_ASAN_REPORT_STORE(1);
395DEFINE_ASAN_REPORT_STORE(2);
396DEFINE_ASAN_REPORT_STORE(4);
397DEFINE_ASAN_REPORT_STORE(8);
398DEFINE_ASAN_REPORT_STORE(16);
399
400void __asan_report_load_n_noabort(unsigned long addr, size_t size)
401{
402 kasan_report(addr, size, false, _RET_IP_);
403}
404EXPORT_SYMBOL(__asan_report_load_n_noabort);
405
406void __asan_report_store_n_noabort(unsigned long addr, size_t size)
407{
408 kasan_report(addr, size, true, _RET_IP_);
409}
410EXPORT_SYMBOL(__asan_report_store_n_noabort);