blob: 24c1211fe9d5e16afa715fb0b69f4aee96424224 [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>
Alexander Potapenkocd110162016-03-25 14:22:08 -070021#include <linux/stackdepot.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080022#include <linux/stacktrace.h>
23#include <linux/string.h>
24#include <linux/types.h>
25#include <linux/kasan.h>
Aneesh Kumar K.V527f2152015-11-05 18:50:46 -080026#include <linux/module.h>
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080027
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080028#include <asm/sections.h>
29
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080030#include "kasan.h"
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080031#include "../slab.h"
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080032
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
39static 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
51static void print_error_description(struct kasan_access_info *info)
52{
Andrey Konovalov0952d872015-11-05 18:50:58 -080053 const char *bug_type = "unknown-crash";
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080054 u8 *shadow_addr;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080055
56 info->first_bad_addr = find_first_bad_addr(info->access_addr,
57 info->access_size);
58
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080059 shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080060
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080061 /*
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) {
Andrey Konovalov0952d872015-11-05 18:50:58 -080069 case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080070 /*
71 * In theory it's still possible to see these shadow values
72 * due to a data race in the kernel code.
73 */
Andrey Konovalov0952d872015-11-05 18:50:58 -080074 bug_type = "out-of-bounds";
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080075 break;
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080076 case KASAN_PAGE_REDZONE:
77 case KASAN_KMALLOC_REDZONE:
Andrey Konovalov0952d872015-11-05 18:50:58 -080078 bug_type = "slab-out-of-bounds";
79 break;
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080080 case KASAN_GLOBAL_REDZONE:
Andrey Konovalov0952d872015-11-05 18:50:58 -080081 bug_type = "global-out-of-bounds";
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080082 break;
Andrey Ryabininc420f162015-02-13 14:39:59 -080083 case KASAN_STACK_LEFT:
84 case KASAN_STACK_MID:
85 case KASAN_STACK_RIGHT:
86 case KASAN_STACK_PARTIAL:
Andrey Konovalov0952d872015-11-05 18:50:58 -080087 bug_type = "stack-out-of-bounds";
88 break;
89 case KASAN_FREE_PAGE:
90 case KASAN_KMALLOC_FREE:
91 bug_type = "use-after-free";
Andrey Ryabininc420f162015-02-13 14:39:59 -080092 break;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080093 }
94
Andrey Konovalov25add7e2015-11-05 18:51:03 -080095 pr_err("BUG: KASAN: %s in %pS at addr %p\n",
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080096 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
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800103static inline bool kernel_or_module_addr(const void *addr)
104{
Aneesh Kumar K.V527f2152015-11-05 18:50:46 -0800105 if (addr >= (void *)_stext && addr < (void *)_end)
106 return true;
107 if (is_module_address((unsigned long)addr))
108 return true;
109 return false;
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800110}
111
112static 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
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700119static DEFINE_SPINLOCK(report_lock);
120
121static void kasan_start_report(unsigned long *flags)
122{
123 /*
124 * Make sure we don't end up in loop.
125 */
126 kasan_disable_current();
127 spin_lock_irqsave(&report_lock, *flags);
128 pr_err("==================================================================\n");
129}
130
131static void kasan_end_report(unsigned long *flags)
132{
133 pr_err("==================================================================\n");
134 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
135 spin_unlock_irqrestore(&report_lock, *flags);
136 kasan_enable_current();
137}
138
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700139static void print_track(struct kasan_track *track)
140{
Alexander Potapenkocd110162016-03-25 14:22:08 -0700141 pr_err("PID = %u\n", track->pid);
142 if (track->stack) {
143 struct stack_trace trace;
144
145 depot_fetch_stack(track->stack, &trace);
146 print_stack_trace(&trace, 0);
147 } else {
148 pr_err("(stack is not available)\n");
149 }
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700150}
151
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700152static void kasan_object_err(struct kmem_cache *cache, void *object)
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700153{
154 struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700155
156 dump_stack();
Andrey Ryabinin47b5c2a2016-08-02 14:02:49 -0700157 pr_err("Object at %p, in cache %s size: %d\n", object, cache->name,
158 cache->object_size);
159
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700160 if (!(cache->flags & SLAB_KASAN))
161 return;
Andrey Ryabininb3cbd9b2016-08-02 14:02:52 -0700162
163 pr_err("Allocated:\n");
164 print_track(&alloc_info->alloc_track);
165 pr_err("Freed:\n");
166 print_track(&alloc_info->free_track);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700167}
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700168
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700169void kasan_report_double_free(struct kmem_cache *cache, void *object,
170 s8 shadow)
171{
172 unsigned long flags;
173
174 kasan_start_report(&flags);
175 pr_err("BUG: Double free or freeing an invalid pointer\n");
176 pr_err("Unexpected shadow byte: 0x%hhX\n", shadow);
177 kasan_object_err(cache, object);
178 kasan_end_report(&flags);
179}
180
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800181static void print_address_description(struct kasan_access_info *info)
182{
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -0800183 const void *addr = info->access_addr;
184
185 if ((addr >= (void *)PAGE_OFFSET) &&
186 (addr < high_memory)) {
187 struct page *page = virt_to_head_page(addr);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800188
189 if (PageSlab(page)) {
190 void *object;
191 struct kmem_cache *cache = page->slab_cache;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700192 object = nearest_obj(cache, page,
193 (void *)info->access_addr);
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700194 kasan_object_err(cache, object);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -0800195 return;
196 }
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -0800197 dump_page(page, "kasan: bad access detected");
198 }
199
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800200 if (kernel_or_module_addr(addr)) {
201 if (!init_task_stack_addr(addr))
202 pr_err("Address belongs to variable %pS\n", addr);
203 }
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800204 dump_stack();
205}
206
207static bool row_is_guilty(const void *row, const void *guilty)
208{
209 return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
210}
211
212static int shadow_pointer_offset(const void *row, const void *shadow)
213{
214 /* The length of ">ff00ff00ff00ff00: " is
215 * 3 + (BITS_PER_LONG/8)*2 chars.
216 */
217 return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
218 (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
219}
220
221static void print_shadow_for_address(const void *addr)
222{
223 int i;
224 const void *shadow = kasan_mem_to_shadow(addr);
225 const void *shadow_row;
226
227 shadow_row = (void *)round_down((unsigned long)shadow,
228 SHADOW_BYTES_PER_ROW)
229 - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
230
231 pr_err("Memory state around the buggy address:\n");
232
233 for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
234 const void *kaddr = kasan_shadow_to_mem(shadow_row);
235 char buffer[4 + (BITS_PER_LONG/8)*2];
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800236 char shadow_buf[SHADOW_BYTES_PER_ROW];
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800237
238 snprintf(buffer, sizeof(buffer),
239 (i == 0) ? ">%p: " : " %p: ", kaddr);
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800240 /*
241 * We should not pass a shadow pointer to generic
242 * function, because generic functions may try to
243 * access kasan mapping for the passed address.
244 */
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800245 memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800246 print_hex_dump(KERN_ERR, buffer,
247 DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800248 shadow_buf, SHADOW_BYTES_PER_ROW, 0);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800249
250 if (row_is_guilty(shadow_row, shadow))
251 pr_err("%*c\n",
252 shadow_pointer_offset(shadow_row, shadow),
253 '^');
254
255 shadow_row += SHADOW_BYTES_PER_ROW;
256 }
257}
258
Andrey Konovalove91210762015-11-05 18:50:55 -0800259static void kasan_report_error(struct kasan_access_info *info)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800260{
261 unsigned long flags;
Andrey Konovalove91210762015-11-05 18:50:55 -0800262 const char *bug_type;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800263
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700264 kasan_start_report(&flags);
265
Andrey Konovalove91210762015-11-05 18:50:55 -0800266 if (info->access_addr <
267 kasan_shadow_to_mem((void *)KASAN_SHADOW_START)) {
268 if ((unsigned long)info->access_addr < PAGE_SIZE)
269 bug_type = "null-ptr-deref";
270 else if ((unsigned long)info->access_addr < TASK_SIZE)
271 bug_type = "user-memory-access";
272 else
273 bug_type = "wild-memory-access";
Andrey Konovalov25add7e2015-11-05 18:51:03 -0800274 pr_err("BUG: KASAN: %s on address %p\n",
Andrey Konovalove91210762015-11-05 18:50:55 -0800275 bug_type, info->access_addr);
276 pr_err("%s of size %zu by task %s/%d\n",
277 info->is_write ? "Write" : "Read",
278 info->access_size, current->comm,
279 task_pid_nr(current));
280 dump_stack();
281 } else {
282 print_error_description(info);
283 print_address_description(info);
284 print_shadow_for_address(info->first_bad_addr);
285 }
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700286
287 kasan_end_report(&flags);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800288}
289
290void kasan_report(unsigned long addr, size_t size,
291 bool is_write, unsigned long ip)
292{
293 struct kasan_access_info info;
294
Aneesh Kumar K.V0ba86632015-11-05 18:50:43 -0800295 if (likely(!kasan_report_enabled()))
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800296 return;
297
298 info.access_addr = (void *)addr;
299 info.access_size = size;
300 info.is_write = is_write;
301 info.ip = ip;
Andrey Konovalove91210762015-11-05 18:50:55 -0800302
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800303 kasan_report_error(&info);
304}
305
306
307#define DEFINE_ASAN_REPORT_LOAD(size) \
308void __asan_report_load##size##_noabort(unsigned long addr) \
309{ \
310 kasan_report(addr, size, false, _RET_IP_); \
311} \
312EXPORT_SYMBOL(__asan_report_load##size##_noabort)
313
314#define DEFINE_ASAN_REPORT_STORE(size) \
315void __asan_report_store##size##_noabort(unsigned long addr) \
316{ \
317 kasan_report(addr, size, true, _RET_IP_); \
318} \
319EXPORT_SYMBOL(__asan_report_store##size##_noabort)
320
321DEFINE_ASAN_REPORT_LOAD(1);
322DEFINE_ASAN_REPORT_LOAD(2);
323DEFINE_ASAN_REPORT_LOAD(4);
324DEFINE_ASAN_REPORT_LOAD(8);
325DEFINE_ASAN_REPORT_LOAD(16);
326DEFINE_ASAN_REPORT_STORE(1);
327DEFINE_ASAN_REPORT_STORE(2);
328DEFINE_ASAN_REPORT_STORE(4);
329DEFINE_ASAN_REPORT_STORE(8);
330DEFINE_ASAN_REPORT_STORE(16);
331
332void __asan_report_load_n_noabort(unsigned long addr, size_t size)
333{
334 kasan_report(addr, size, false, _RET_IP_);
335}
336EXPORT_SYMBOL(__asan_report_load_n_noabort);
337
338void __asan_report_store_n_noabort(unsigned long addr, size_t size)
339{
340 kasan_report(addr, size, true, _RET_IP_);
341}
342EXPORT_SYMBOL(__asan_report_store_n_noabort);