blob: 866732ef3db332d946c637d79ccb8e45d6ac209a [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.
5 * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
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>
25
26#include "kasan.h"
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080027#include "../slab.h"
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080028
29/* Shadow layout customization. */
30#define SHADOW_BYTES_PER_BLOCK 1
31#define SHADOW_BLOCKS_PER_ROW 16
32#define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK)
33#define SHADOW_ROWS_AROUND_ADDR 2
34
35static const void *find_first_bad_addr(const void *addr, size_t size)
36{
37 u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr);
38 const void *first_bad_addr = addr;
39
40 while (!shadow_val && first_bad_addr < addr + size) {
41 first_bad_addr += KASAN_SHADOW_SCALE_SIZE;
42 shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr);
43 }
44 return first_bad_addr;
45}
46
47static void print_error_description(struct kasan_access_info *info)
48{
49 const char *bug_type = "unknown crash";
50 u8 shadow_val;
51
52 info->first_bad_addr = find_first_bad_addr(info->access_addr,
53 info->access_size);
54
55 shadow_val = *(u8 *)kasan_mem_to_shadow(info->first_bad_addr);
56
57 switch (shadow_val) {
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080058 case KASAN_FREE_PAGE:
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080059 case KASAN_KMALLOC_FREE:
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080060 bug_type = "use after free";
61 break;
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080062 case KASAN_PAGE_REDZONE:
63 case KASAN_KMALLOC_REDZONE:
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080064 case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
65 bug_type = "out of bounds access";
66 break;
Andrey Ryabininc420f162015-02-13 14:39:59 -080067 case KASAN_STACK_LEFT:
68 case KASAN_STACK_MID:
69 case KASAN_STACK_RIGHT:
70 case KASAN_STACK_PARTIAL:
71 bug_type = "out of bounds on stack";
72 break;
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080073 }
74
75 pr_err("BUG: KASan: %s in %pS at addr %p\n",
76 bug_type, (void *)info->ip,
77 info->access_addr);
78 pr_err("%s of size %zu by task %s/%d\n",
79 info->is_write ? "Write" : "Read",
80 info->access_size, current->comm, task_pid_nr(current));
81}
82
83static void print_address_description(struct kasan_access_info *info)
84{
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -080085 const void *addr = info->access_addr;
86
87 if ((addr >= (void *)PAGE_OFFSET) &&
88 (addr < high_memory)) {
89 struct page *page = virt_to_head_page(addr);
Andrey Ryabinin0316bec2015-02-13 14:39:42 -080090
91 if (PageSlab(page)) {
92 void *object;
93 struct kmem_cache *cache = page->slab_cache;
94 void *last_object;
95
96 object = virt_to_obj(cache, page_address(page), addr);
97 last_object = page_address(page) +
98 page->objects * cache->size;
99
100 if (unlikely(object > last_object))
101 object = last_object; /* we hit into padding */
102
103 object_err(cache, page, object,
104 "kasan: bad access detected");
105 return;
106 }
Andrey Ryabininb8c73fc2015-02-13 14:39:28 -0800107 dump_page(page, "kasan: bad access detected");
108 }
109
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -0800110 dump_stack();
111}
112
113static bool row_is_guilty(const void *row, const void *guilty)
114{
115 return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
116}
117
118static int shadow_pointer_offset(const void *row, const void *shadow)
119{
120 /* The length of ">ff00ff00ff00ff00: " is
121 * 3 + (BITS_PER_LONG/8)*2 chars.
122 */
123 return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
124 (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
125}
126
127static void print_shadow_for_address(const void *addr)
128{
129 int i;
130 const void *shadow = kasan_mem_to_shadow(addr);
131 const void *shadow_row;
132
133 shadow_row = (void *)round_down((unsigned long)shadow,
134 SHADOW_BYTES_PER_ROW)
135 - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
136
137 pr_err("Memory state around the buggy address:\n");
138
139 for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
140 const void *kaddr = kasan_shadow_to_mem(shadow_row);
141 char buffer[4 + (BITS_PER_LONG/8)*2];
142
143 snprintf(buffer, sizeof(buffer),
144 (i == 0) ? ">%p: " : " %p: ", kaddr);
145
146 kasan_disable_current();
147 print_hex_dump(KERN_ERR, buffer,
148 DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
149 shadow_row, SHADOW_BYTES_PER_ROW, 0);
150 kasan_enable_current();
151
152 if (row_is_guilty(shadow_row, shadow))
153 pr_err("%*c\n",
154 shadow_pointer_offset(shadow_row, shadow),
155 '^');
156
157 shadow_row += SHADOW_BYTES_PER_ROW;
158 }
159}
160
161static DEFINE_SPINLOCK(report_lock);
162
163void kasan_report_error(struct kasan_access_info *info)
164{
165 unsigned long flags;
166
167 spin_lock_irqsave(&report_lock, flags);
168 pr_err("================================="
169 "=================================\n");
170 print_error_description(info);
171 print_address_description(info);
172 print_shadow_for_address(info->first_bad_addr);
173 pr_err("================================="
174 "=================================\n");
175 spin_unlock_irqrestore(&report_lock, flags);
176}
177
178void kasan_report_user_access(struct kasan_access_info *info)
179{
180 unsigned long flags;
181
182 spin_lock_irqsave(&report_lock, flags);
183 pr_err("================================="
184 "=================================\n");
185 pr_err("BUG: KASan: user-memory-access on address %p\n",
186 info->access_addr);
187 pr_err("%s of size %zu by task %s/%d\n",
188 info->is_write ? "Write" : "Read",
189 info->access_size, current->comm, task_pid_nr(current));
190 dump_stack();
191 pr_err("================================="
192 "=================================\n");
193 spin_unlock_irqrestore(&report_lock, flags);
194}
195
196void kasan_report(unsigned long addr, size_t size,
197 bool is_write, unsigned long ip)
198{
199 struct kasan_access_info info;
200
201 if (likely(!kasan_enabled()))
202 return;
203
204 info.access_addr = (void *)addr;
205 info.access_size = size;
206 info.is_write = is_write;
207 info.ip = ip;
208 kasan_report_error(&info);
209}
210
211
212#define DEFINE_ASAN_REPORT_LOAD(size) \
213void __asan_report_load##size##_noabort(unsigned long addr) \
214{ \
215 kasan_report(addr, size, false, _RET_IP_); \
216} \
217EXPORT_SYMBOL(__asan_report_load##size##_noabort)
218
219#define DEFINE_ASAN_REPORT_STORE(size) \
220void __asan_report_store##size##_noabort(unsigned long addr) \
221{ \
222 kasan_report(addr, size, true, _RET_IP_); \
223} \
224EXPORT_SYMBOL(__asan_report_store##size##_noabort)
225
226DEFINE_ASAN_REPORT_LOAD(1);
227DEFINE_ASAN_REPORT_LOAD(2);
228DEFINE_ASAN_REPORT_LOAD(4);
229DEFINE_ASAN_REPORT_LOAD(8);
230DEFINE_ASAN_REPORT_LOAD(16);
231DEFINE_ASAN_REPORT_STORE(1);
232DEFINE_ASAN_REPORT_STORE(2);
233DEFINE_ASAN_REPORT_STORE(4);
234DEFINE_ASAN_REPORT_STORE(8);
235DEFINE_ASAN_REPORT_STORE(16);
236
237void __asan_report_load_n_noabort(unsigned long addr, size_t size)
238{
239 kasan_report(addr, size, false, _RET_IP_);
240}
241EXPORT_SYMBOL(__asan_report_load_n_noabort);
242
243void __asan_report_store_n_noabort(unsigned long addr, size_t size)
244{
245 kasan_report(addr, size, true, _RET_IP_);
246}
247EXPORT_SYMBOL(__asan_report_store_n_noabort);