blob: 12f222d0224b93120ed7131a80172666e78276d4 [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
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>
Aneesh Kumar K.V527f2152015-11-05 18:50:46 -080025#include <linux/module.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080026
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080027#include <asm/sections.h>
28
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080029#include "kasan.h"
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080030#include "../slab.h"
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080031
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
38static 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
50static void print_error_description(struct kasan_access_info *info)
51{
Andrey Konovalov0952d872015-11-05 18:50:58 -080052 const char *bug_type = "unknown-crash";
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080053 u8 *shadow_addr;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080054
55 info->first_bad_addr = find_first_bad_addr(info->access_addr,
56 info->access_size);
57
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080058 shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080059
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080060 /*
61 * If shadow byte value is in [0, KASAN_SHADOW_SCALE_SIZE) we can look
62 * at the next shadow byte to determine the type of the bad access.
63 */
64 if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1)
65 shadow_addr++;
66
67 switch (*shadow_addr) {
Andrey Konovalov0952d872015-11-05 18:50:58 -080068 case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080069 /*
70 * In theory it's still possible to see these shadow values
71 * due to a data race in the kernel code.
72 */
Andrey Konovalov0952d872015-11-05 18:50:58 -080073 bug_type = "out-of-bounds";
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080074 break;
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080075 case KASAN_PAGE_REDZONE:
76 case KASAN_KMALLOC_REDZONE:
Andrey Konovalov0952d872015-11-05 18:50:58 -080077 bug_type = "slab-out-of-bounds";
78 break;
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080079 case KASAN_GLOBAL_REDZONE:
Andrey Konovalov0952d872015-11-05 18:50:58 -080080 bug_type = "global-out-of-bounds";
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080081 break;
Andrey Ryabininc420f162015-02-13 14:39:59 -080082 case KASAN_STACK_LEFT:
83 case KASAN_STACK_MID:
84 case KASAN_STACK_RIGHT:
85 case KASAN_STACK_PARTIAL:
Andrey Konovalov0952d872015-11-05 18:50:58 -080086 bug_type = "stack-out-of-bounds";
87 break;
88 case KASAN_FREE_PAGE:
89 case KASAN_KMALLOC_FREE:
90 bug_type = "use-after-free";
Andrey Ryabininc420f162015-02-13 14:39:59 -080091 break;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080092 }
93
Andrey Konovalov25add7e2015-11-05 18:51:03 -080094 pr_err("BUG: KASAN: %s in %pS at addr %p\n",
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080095 bug_type, (void *)info->ip,
96 info->access_addr);
97 pr_err("%s of size %zu by task %s/%d\n",
98 info->is_write ? "Write" : "Read",
99 info->access_size, current->comm, task_pid_nr(current));
100}
101
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800102static inline bool kernel_or_module_addr(const void *addr)
103{
Aneesh Kumar K.V527f2152015-11-05 18:50:46 -0800104 if (addr >= (void *)_stext && addr < (void *)_end)
105 return true;
106 if (is_module_address((unsigned long)addr))
107 return true;
108 return false;
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800109}
110
111static inline bool init_task_stack_addr(const void *addr)
112{
113 return addr >= (void *)&init_thread_union.stack &&
114 (addr <= (void *)&init_thread_union.stack +
115 sizeof(init_thread_union.stack));
116}
117
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800118static void print_address_description(struct kasan_access_info *info)
119{
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -0800120 const void *addr = info->access_addr;
121
122 if ((addr >= (void *)PAGE_OFFSET) &&
123 (addr < high_memory)) {
124 struct page *page = virt_to_head_page(addr);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800125
126 if (PageSlab(page)) {
127 void *object;
128 struct kmem_cache *cache = page->slab_cache;
129 void *last_object;
130
131 object = virt_to_obj(cache, page_address(page), addr);
132 last_object = page_address(page) +
133 page->objects * cache->size;
134
135 if (unlikely(object > last_object))
136 object = last_object; /* we hit into padding */
137
138 object_err(cache, page, object,
139 "kasan: bad access detected");
140 return;
141 }
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -0800142 dump_page(page, "kasan: bad access detected");
143 }
144
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800145 if (kernel_or_module_addr(addr)) {
146 if (!init_task_stack_addr(addr))
147 pr_err("Address belongs to variable %pS\n", addr);
148 }
149
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800150 dump_stack();
151}
152
153static bool row_is_guilty(const void *row, const void *guilty)
154{
155 return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
156}
157
158static int shadow_pointer_offset(const void *row, const void *shadow)
159{
160 /* The length of ">ff00ff00ff00ff00: " is
161 * 3 + (BITS_PER_LONG/8)*2 chars.
162 */
163 return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
164 (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
165}
166
167static void print_shadow_for_address(const void *addr)
168{
169 int i;
170 const void *shadow = kasan_mem_to_shadow(addr);
171 const void *shadow_row;
172
173 shadow_row = (void *)round_down((unsigned long)shadow,
174 SHADOW_BYTES_PER_ROW)
175 - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
176
177 pr_err("Memory state around the buggy address:\n");
178
179 for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
180 const void *kaddr = kasan_shadow_to_mem(shadow_row);
181 char buffer[4 + (BITS_PER_LONG/8)*2];
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800182 char shadow_buf[SHADOW_BYTES_PER_ROW];
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800183
184 snprintf(buffer, sizeof(buffer),
185 (i == 0) ? ">%p: " : " %p: ", kaddr);
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800186 /*
187 * We should not pass a shadow pointer to generic
188 * function, because generic functions may try to
189 * access kasan mapping for the passed address.
190 */
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800191 memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800192 print_hex_dump(KERN_ERR, buffer,
193 DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800194 shadow_buf, SHADOW_BYTES_PER_ROW, 0);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800195
196 if (row_is_guilty(shadow_row, shadow))
197 pr_err("%*c\n",
198 shadow_pointer_offset(shadow_row, shadow),
199 '^');
200
201 shadow_row += SHADOW_BYTES_PER_ROW;
202 }
203}
204
205static DEFINE_SPINLOCK(report_lock);
206
Andrey Konovalove91210762015-11-05 18:50:55 -0800207static void kasan_report_error(struct kasan_access_info *info)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800208{
209 unsigned long flags;
Andrey Konovalove91210762015-11-05 18:50:55 -0800210 const char *bug_type;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800211
Aneesh Kumar K.Vfc5aeea2015-11-05 18:50:51 -0800212 /*
213 * Make sure we don't end up in loop.
214 */
215 kasan_disable_current();
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800216 spin_lock_irqsave(&report_lock, flags);
217 pr_err("================================="
218 "=================================\n");
Andrey Konovalove91210762015-11-05 18:50:55 -0800219 if (info->access_addr <
220 kasan_shadow_to_mem((void *)KASAN_SHADOW_START)) {
221 if ((unsigned long)info->access_addr < PAGE_SIZE)
222 bug_type = "null-ptr-deref";
223 else if ((unsigned long)info->access_addr < TASK_SIZE)
224 bug_type = "user-memory-access";
225 else
226 bug_type = "wild-memory-access";
Andrey Konovalov25add7e2015-11-05 18:51:03 -0800227 pr_err("BUG: KASAN: %s on address %p\n",
Andrey Konovalove91210762015-11-05 18:50:55 -0800228 bug_type, info->access_addr);
229 pr_err("%s of size %zu by task %s/%d\n",
230 info->is_write ? "Write" : "Read",
231 info->access_size, current->comm,
232 task_pid_nr(current));
233 dump_stack();
234 } else {
235 print_error_description(info);
236 print_address_description(info);
237 print_shadow_for_address(info->first_bad_addr);
238 }
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800239 pr_err("================================="
240 "=================================\n");
Andrey Ryabinineb06f432015-11-05 18:51:26 -0800241 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800242 spin_unlock_irqrestore(&report_lock, flags);
Aneesh Kumar K.Vfc5aeea2015-11-05 18:50:51 -0800243 kasan_enable_current();
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800244}
245
246void kasan_report(unsigned long addr, size_t size,
247 bool is_write, unsigned long ip)
248{
249 struct kasan_access_info info;
250
Aneesh Kumar K.V0ba86632015-11-05 18:50:43 -0800251 if (likely(!kasan_report_enabled()))
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800252 return;
253
254 info.access_addr = (void *)addr;
255 info.access_size = size;
256 info.is_write = is_write;
257 info.ip = ip;
Andrey Konovalove91210762015-11-05 18:50:55 -0800258
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800259 kasan_report_error(&info);
260}
261
262
263#define DEFINE_ASAN_REPORT_LOAD(size) \
264void __asan_report_load##size##_noabort(unsigned long addr) \
265{ \
266 kasan_report(addr, size, false, _RET_IP_); \
267} \
268EXPORT_SYMBOL(__asan_report_load##size##_noabort)
269
270#define DEFINE_ASAN_REPORT_STORE(size) \
271void __asan_report_store##size##_noabort(unsigned long addr) \
272{ \
273 kasan_report(addr, size, true, _RET_IP_); \
274} \
275EXPORT_SYMBOL(__asan_report_store##size##_noabort)
276
277DEFINE_ASAN_REPORT_LOAD(1);
278DEFINE_ASAN_REPORT_LOAD(2);
279DEFINE_ASAN_REPORT_LOAD(4);
280DEFINE_ASAN_REPORT_LOAD(8);
281DEFINE_ASAN_REPORT_LOAD(16);
282DEFINE_ASAN_REPORT_STORE(1);
283DEFINE_ASAN_REPORT_STORE(2);
284DEFINE_ASAN_REPORT_STORE(4);
285DEFINE_ASAN_REPORT_STORE(8);
286DEFINE_ASAN_REPORT_STORE(16);
287
288void __asan_report_load_n_noabort(unsigned long addr, size_t size)
289{
290 kasan_report(addr, size, false, _RET_IP_);
291}
292EXPORT_SYMBOL(__asan_report_load_n_noabort);
293
294void __asan_report_store_n_noabort(unsigned long addr, size_t size)
295{
296 kasan_report(addr, size, true, _RET_IP_);
297}
298EXPORT_SYMBOL(__asan_report_store_n_noabort);