blob: 35d2db86cd6db83debbfb674ebf942f8d604f180 [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
Andrey Konovalov9316eb72017-05-03 14:56:25 -070051static bool addr_has_shadow(struct kasan_access_info *info)
52{
53 return (info->access_addr >=
54 kasan_shadow_to_mem((void *)KASAN_SHADOW_START));
55}
56
57static const char *get_shadow_bug_type(struct kasan_access_info *info)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080058{
Andrey Konovalov0952d872015-11-05 18:50:58 -080059 const char *bug_type = "unknown-crash";
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080060 u8 *shadow_addr;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080061
62 info->first_bad_addr = find_first_bad_addr(info->access_addr,
63 info->access_size);
64
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080065 shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080066
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080067 /*
68 * If shadow byte value is in [0, KASAN_SHADOW_SCALE_SIZE) we can look
69 * at the next shadow byte to determine the type of the bad access.
70 */
71 if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1)
72 shadow_addr++;
73
74 switch (*shadow_addr) {
Andrey Konovalov0952d872015-11-05 18:50:58 -080075 case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
Andrey Konovalovcdf6a272015-11-05 18:51:01 -080076 /*
77 * In theory it's still possible to see these shadow values
78 * due to a data race in the kernel code.
79 */
Andrey Konovalov0952d872015-11-05 18:50:58 -080080 bug_type = "out-of-bounds";
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080081 break;
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080082 case KASAN_PAGE_REDZONE:
83 case KASAN_KMALLOC_REDZONE:
Andrey Konovalov0952d872015-11-05 18:50:58 -080084 bug_type = "slab-out-of-bounds";
85 break;
Andrey Ryabininbebf56a2015-02-13 14:40:17 -080086 case KASAN_GLOBAL_REDZONE:
Andrey Konovalov0952d872015-11-05 18:50:58 -080087 bug_type = "global-out-of-bounds";
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080088 break;
Andrey Ryabininc420f162015-02-13 14:39:59 -080089 case KASAN_STACK_LEFT:
90 case KASAN_STACK_MID:
91 case KASAN_STACK_RIGHT:
92 case KASAN_STACK_PARTIAL:
Andrey Konovalov0952d872015-11-05 18:50:58 -080093 bug_type = "stack-out-of-bounds";
94 break;
95 case KASAN_FREE_PAGE:
96 case KASAN_KMALLOC_FREE:
97 bug_type = "use-after-free";
Andrey Ryabininc420f162015-02-13 14:39:59 -080098 break;
Dmitry Vyukov828347f2016-11-30 15:54:16 -080099 case KASAN_USE_AFTER_SCOPE:
100 bug_type = "use-after-scope";
101 break;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800102 }
103
Andrey Konovalov9316eb72017-05-03 14:56:25 -0700104 return bug_type;
105}
106
107const char *get_wild_bug_type(struct kasan_access_info *info)
108{
109 const char *bug_type = "unknown-crash";
110
111 if ((unsigned long)info->access_addr < PAGE_SIZE)
112 bug_type = "null-ptr-deref";
113 else if ((unsigned long)info->access_addr < TASK_SIZE)
114 bug_type = "user-memory-access";
115 else
116 bug_type = "wild-memory-access";
117
118 return bug_type;
119}
120
Andrey Konovalov294a4862017-05-03 14:56:28 -0700121static const char *get_bug_type(struct kasan_access_info *info)
122{
123 if (addr_has_shadow(info))
124 return get_shadow_bug_type(info);
125 return get_wild_bug_type(info);
126}
127
Andrey Konovalov9316eb72017-05-03 14:56:25 -0700128static void print_error_description(struct kasan_access_info *info)
129{
Andrey Konovalov294a4862017-05-03 14:56:28 -0700130 const char *bug_type = get_bug_type(info);
Andrey Konovalov9316eb72017-05-03 14:56:25 -0700131
Andrey Konovalovf7bf9a22017-05-03 14:56:38 -0700132 pr_err("BUG: KASAN: %s in %pS\n",
133 bug_type, (void *)info->ip);
134 pr_err("%s of size %zu at addr %p by task %s/%d\n",
Andrey Konovalov294a4862017-05-03 14:56:28 -0700135 info->is_write ? "Write" : "Read", info->access_size,
Andrey Konovalovf7bf9a22017-05-03 14:56:38 -0700136 info->access_addr, current->comm, task_pid_nr(current));
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800137}
138
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800139static inline bool kernel_or_module_addr(const void *addr)
140{
Aneesh Kumar K.V527f2152015-11-05 18:50:46 -0800141 if (addr >= (void *)_stext && addr < (void *)_end)
142 return true;
143 if (is_module_address((unsigned long)addr))
144 return true;
145 return false;
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800146}
147
148static inline bool init_task_stack_addr(const void *addr)
149{
150 return addr >= (void *)&init_thread_union.stack &&
151 (addr <= (void *)&init_thread_union.stack +
152 sizeof(init_thread_union.stack));
153}
154
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700155static DEFINE_SPINLOCK(report_lock);
156
157static void kasan_start_report(unsigned long *flags)
158{
159 /*
160 * Make sure we don't end up in loop.
161 */
162 kasan_disable_current();
163 spin_lock_irqsave(&report_lock, *flags);
164 pr_err("==================================================================\n");
165}
166
167static void kasan_end_report(unsigned long *flags)
168{
169 pr_err("==================================================================\n");
170 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
171 spin_unlock_irqrestore(&report_lock, *flags);
172 kasan_enable_current();
173}
174
Andrey Konovalove6275c42017-05-03 14:56:31 -0700175static void print_track(struct kasan_track *track, const char *prefix)
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700176{
Andrey Konovalove6275c42017-05-03 14:56:31 -0700177 pr_err("%s by task %u:\n", prefix, track->pid);
Alexander Potapenkocd110162016-03-25 14:22:08 -0700178 if (track->stack) {
179 struct stack_trace trace;
180
181 depot_fetch_stack(track->stack, &trace);
182 print_stack_trace(&trace, 0);
183 } else {
184 pr_err("(stack is not available)\n");
185 }
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700186}
187
Andrey Konovalov047bc042017-05-03 14:56:34 -0700188static struct page *addr_to_page(const void *addr)
189{
190 if ((addr >= (void *)PAGE_OFFSET) &&
191 (addr < high_memory))
192 return virt_to_head_page(addr);
193 return NULL;
194}
195
Andrey Konovalov1be25182017-05-03 14:56:41 -0700196static void describe_object_addr(struct kmem_cache *cache, void *object,
197 const void *addr)
198{
199 unsigned long access_addr = (unsigned long)addr;
200 unsigned long object_addr = (unsigned long)object;
201 const char *rel_type;
202 int rel_bytes;
203
204 pr_err("The buggy address belongs to the object at %p\n"
205 " which belongs to the cache %s of size %d\n",
206 object, cache->name, cache->object_size);
207
208 if (!addr)
209 return;
210
211 if (access_addr < object_addr) {
212 rel_type = "to the left";
213 rel_bytes = object_addr - access_addr;
214 } else if (access_addr >= object_addr + cache->object_size) {
215 rel_type = "to the right";
216 rel_bytes = access_addr - (object_addr + cache->object_size);
217 } else {
218 rel_type = "inside";
219 rel_bytes = access_addr - object_addr;
220 }
221
222 pr_err("The buggy address is located %d bytes %s of\n"
223 " %d-byte region [%p, %p)\n",
224 rel_bytes, rel_type, cache->object_size, (void *)object_addr,
225 (void *)(object_addr + cache->object_size));
226}
227
228static void describe_object(struct kmem_cache *cache, void *object,
229 const void *addr)
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700230{
231 struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700232
Andrey Konovalov1be25182017-05-03 14:56:41 -0700233 if (cache->flags & SLAB_KASAN) {
234 print_track(&alloc_info->alloc_track, "Allocated");
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700235 pr_err("\n");
Andrey Konovalov1be25182017-05-03 14:56:41 -0700236 print_track(&alloc_info->free_track, "Freed");
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700237 pr_err("\n");
Andrey Konovalov1be25182017-05-03 14:56:41 -0700238 }
Andrey Ryabinin47b5c2a2016-08-02 14:02:49 -0700239
Andrey Konovalov1be25182017-05-03 14:56:41 -0700240 describe_object_addr(cache, object, addr);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700241}
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -0700242
Andrey Konovalovdcbca172017-05-03 14:56:47 -0700243static void print_address_description(void *addr)
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700244{
Andrey Konovalov047bc042017-05-03 14:56:34 -0700245 struct page *page = addr_to_page(addr);
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -0800246
Andrey Konovalov047bc042017-05-03 14:56:34 -0700247 dump_stack();
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700248 pr_err("\n");
Andrey Konovalov047bc042017-05-03 14:56:34 -0700249
250 if (page && PageSlab(page)) {
251 struct kmem_cache *cache = page->slab_cache;
Andrey Konovalov1be25182017-05-03 14:56:41 -0700252 void *object = nearest_obj(cache, page, addr);
Andrey Konovalov047bc042017-05-03 14:56:34 -0700253
Andrey Konovalov1be25182017-05-03 14:56:41 -0700254 describe_object(cache, object, addr);
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -0800255 }
256
Andrey Konovalov95b36162017-05-03 14:56:44 -0700257 if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
258 pr_err("The buggy address belongs to the variable:\n");
259 pr_err(" %pS\n", addr);
260 }
261
262 if (page) {
263 pr_err("The buggy address belongs to the page:\n");
264 dump_page(page, "kasan: bad access detected");
Andrey Ryabininbebf56a2015-02-13 14:40:17 -0800265 }
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800266}
267
268static bool row_is_guilty(const void *row, const void *guilty)
269{
270 return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
271}
272
273static int shadow_pointer_offset(const void *row, const void *shadow)
274{
275 /* The length of ">ff00ff00ff00ff00: " is
276 * 3 + (BITS_PER_LONG/8)*2 chars.
277 */
278 return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
279 (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
280}
281
282static void print_shadow_for_address(const void *addr)
283{
284 int i;
285 const void *shadow = kasan_mem_to_shadow(addr);
286 const void *shadow_row;
287
288 shadow_row = (void *)round_down((unsigned long)shadow,
289 SHADOW_BYTES_PER_ROW)
290 - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
291
292 pr_err("Memory state around the buggy address:\n");
293
294 for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
295 const void *kaddr = kasan_shadow_to_mem(shadow_row);
296 char buffer[4 + (BITS_PER_LONG/8)*2];
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800297 char shadow_buf[SHADOW_BYTES_PER_ROW];
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800298
299 snprintf(buffer, sizeof(buffer),
300 (i == 0) ? ">%p: " : " %p: ", kaddr);
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800301 /*
302 * We should not pass a shadow pointer to generic
303 * function, because generic functions may try to
304 * access kasan mapping for the passed address.
305 */
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800306 memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800307 print_hex_dump(KERN_ERR, buffer,
308 DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
Aneesh Kumar K.Vf2377d42015-11-05 18:50:48 -0800309 shadow_buf, SHADOW_BYTES_PER_ROW, 0);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800310
311 if (row_is_guilty(shadow_row, shadow))
312 pr_err("%*c\n",
313 shadow_pointer_offset(shadow_row, shadow),
314 '^');
315
316 shadow_row += SHADOW_BYTES_PER_ROW;
317 }
318}
319
Andrey Konovalovdcbca172017-05-03 14:56:47 -0700320void kasan_report_double_free(struct kmem_cache *cache, void *object,
321 void *ip)
322{
323 unsigned long flags;
324
325 kasan_start_report(&flags);
326 pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", ip);
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700327 pr_err("\n");
Andrey Konovalovdcbca172017-05-03 14:56:47 -0700328 print_address_description(object);
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700329 pr_err("\n");
Andrey Konovalovdcbca172017-05-03 14:56:47 -0700330 print_shadow_for_address(object);
331 kasan_end_report(&flags);
332}
333
Andrey Konovalove91210762015-11-05 18:50:55 -0800334static void kasan_report_error(struct kasan_access_info *info)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800335{
336 unsigned long flags;
337
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700338 kasan_start_report(&flags);
339
Andrey Konovalov294a4862017-05-03 14:56:28 -0700340 print_error_description(info);
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700341 pr_err("\n");
Andrey Konovalov294a4862017-05-03 14:56:28 -0700342
Andrey Konovalov9316eb72017-05-03 14:56:25 -0700343 if (!addr_has_shadow(info)) {
Andrey Konovalove91210762015-11-05 18:50:55 -0800344 dump_stack();
345 } else {
Andrey Konovalovdcbca172017-05-03 14:56:47 -0700346 print_address_description((void *)info->access_addr);
Andrey Konovalov376eb6e2017-05-03 14:56:50 -0700347 pr_err("\n");
Andrey Konovalove91210762015-11-05 18:50:55 -0800348 print_shadow_for_address(info->first_bad_addr);
349 }
Andrey Ryabinin7e088972016-08-02 14:02:55 -0700350
351 kasan_end_report(&flags);
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800352}
353
354void kasan_report(unsigned long addr, size_t size,
355 bool is_write, unsigned long ip)
356{
357 struct kasan_access_info info;
358
Aneesh Kumar K.V0ba86632015-11-05 18:50:43 -0800359 if (likely(!kasan_report_enabled()))
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800360 return;
361
362 info.access_addr = (void *)addr;
363 info.access_size = size;
364 info.is_write = is_write;
365 info.ip = ip;
Andrey Konovalove91210762015-11-05 18:50:55 -0800366
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800367 kasan_report_error(&info);
368}
369
370
371#define DEFINE_ASAN_REPORT_LOAD(size) \
372void __asan_report_load##size##_noabort(unsigned long addr) \
373{ \
374 kasan_report(addr, size, false, _RET_IP_); \
375} \
376EXPORT_SYMBOL(__asan_report_load##size##_noabort)
377
378#define DEFINE_ASAN_REPORT_STORE(size) \
379void __asan_report_store##size##_noabort(unsigned long addr) \
380{ \
381 kasan_report(addr, size, true, _RET_IP_); \
382} \
383EXPORT_SYMBOL(__asan_report_store##size##_noabort)
384
385DEFINE_ASAN_REPORT_LOAD(1);
386DEFINE_ASAN_REPORT_LOAD(2);
387DEFINE_ASAN_REPORT_LOAD(4);
388DEFINE_ASAN_REPORT_LOAD(8);
389DEFINE_ASAN_REPORT_LOAD(16);
390DEFINE_ASAN_REPORT_STORE(1);
391DEFINE_ASAN_REPORT_STORE(2);
392DEFINE_ASAN_REPORT_STORE(4);
393DEFINE_ASAN_REPORT_STORE(8);
394DEFINE_ASAN_REPORT_STORE(16);
395
396void __asan_report_load_n_noabort(unsigned long addr, size_t size)
397{
398 kasan_report(addr, size, false, _RET_IP_);
399}
400EXPORT_SYMBOL(__asan_report_load_n_noabort);
401
402void __asan_report_store_n_noabort(unsigned long addr, size_t size)
403{
404 kasan_report(addr, size, true, _RET_IP_);
405}
406EXPORT_SYMBOL(__asan_report_store_n_noabort);