2 * This file contains error reporting code.
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
7 * Some code borrowed from https://github.com/xairy/kasan-prototype by
8 * Andrey Konovalov <adech.fo@gmail.com>
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.
16 #include <linux/ftrace.h>
17 #include <linux/kernel.h>
19 #include <linux/printk.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/stacktrace.h>
23 #include <linux/string.h>
24 #include <linux/types.h>
25 #include <linux/kasan.h>
26 #include <linux/module.h>
28 #include <asm/sections.h>
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
39 static const void *find_first_bad_addr(const void *addr, size_t size)
41 u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr);
42 const void *first_bad_addr = addr;
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);
48 return first_bad_addr;
51 static void print_error_description(struct kasan_access_info *info)
53 const char *bug_type = "unknown-crash";
56 info->first_bad_addr = find_first_bad_addr(info->access_addr,
59 shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
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.
65 if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1)
68 switch (*shadow_addr) {
69 case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
71 * In theory it's still possible to see these shadow values
72 * due to a data race in the kernel code.
74 bug_type = "out-of-bounds";
76 case KASAN_PAGE_REDZONE:
77 case KASAN_KMALLOC_REDZONE:
78 bug_type = "slab-out-of-bounds";
80 case KASAN_GLOBAL_REDZONE:
81 bug_type = "global-out-of-bounds";
83 case KASAN_STACK_LEFT:
85 case KASAN_STACK_RIGHT:
86 case KASAN_STACK_PARTIAL:
87 bug_type = "stack-out-of-bounds";
90 case KASAN_KMALLOC_FREE:
91 bug_type = "use-after-free";
95 pr_err("BUG: KASAN: %s in %pS at addr %p\n",
96 bug_type, (void *)info->ip,
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));
103 static inline bool kernel_or_module_addr(const void *addr)
105 if (addr >= (void *)_stext && addr < (void *)_end)
107 if (is_module_address((unsigned long)addr))
112 static inline bool init_task_stack_addr(const void *addr)
114 return addr >= (void *)&init_thread_union.stack &&
115 (addr <= (void *)&init_thread_union.stack +
116 sizeof(init_thread_union.stack));
119 static void print_address_description(struct kasan_access_info *info)
121 const void *addr = info->access_addr;
123 if ((addr >= (void *)PAGE_OFFSET) &&
124 (addr < high_memory)) {
125 struct page *page = virt_to_head_page(addr);
127 if (PageSlab(page)) {
129 struct kmem_cache *cache = page->slab_cache;
132 object = virt_to_obj(cache, page_address(page), addr);
133 last_object = page_address(page) +
134 page->objects * cache->size;
136 if (unlikely(object > last_object))
137 object = last_object; /* we hit into padding */
139 object_err(cache, page, object,
140 "kasan: bad access detected");
143 dump_page(page, "kasan: bad access detected");
146 if (kernel_or_module_addr(addr)) {
147 if (!init_task_stack_addr(addr))
148 pr_err("Address belongs to variable %pS\n", addr);
154 static bool row_is_guilty(const void *row, const void *guilty)
156 return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
159 static int shadow_pointer_offset(const void *row, const void *shadow)
161 /* The length of ">ff00ff00ff00ff00: " is
162 * 3 + (BITS_PER_LONG/8)*2 chars.
164 return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
165 (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
168 static void print_shadow_for_address(const void *addr)
171 const void *shadow = kasan_mem_to_shadow(addr);
172 const void *shadow_row;
174 shadow_row = (void *)round_down((unsigned long)shadow,
175 SHADOW_BYTES_PER_ROW)
176 - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
178 pr_err("Memory state around the buggy address:\n");
180 for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
181 const void *kaddr = kasan_shadow_to_mem(shadow_row);
182 char buffer[4 + (BITS_PER_LONG/8)*2];
183 char shadow_buf[SHADOW_BYTES_PER_ROW];
185 snprintf(buffer, sizeof(buffer),
186 (i == 0) ? ">%p: " : " %p: ", kaddr);
188 * We should not pass a shadow pointer to generic
189 * function, because generic functions may try to
190 * access kasan mapping for the passed address.
192 memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
193 print_hex_dump(KERN_ERR, buffer,
194 DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
195 shadow_buf, SHADOW_BYTES_PER_ROW, 0);
197 if (row_is_guilty(shadow_row, shadow))
199 shadow_pointer_offset(shadow_row, shadow),
202 shadow_row += SHADOW_BYTES_PER_ROW;
206 static DEFINE_SPINLOCK(report_lock);
208 static void kasan_report_error(struct kasan_access_info *info)
211 const char *bug_type;
214 * Make sure we don't end up in loop.
216 kasan_disable_current();
217 spin_lock_irqsave(&report_lock, flags);
218 pr_err("================================="
219 "=================================\n");
220 if (info->access_addr <
221 kasan_shadow_to_mem((void *)KASAN_SHADOW_START)) {
222 if ((unsigned long)info->access_addr < PAGE_SIZE)
223 bug_type = "null-ptr-deref";
224 else if ((unsigned long)info->access_addr < TASK_SIZE)
225 bug_type = "user-memory-access";
227 bug_type = "wild-memory-access";
228 pr_err("BUG: KASAN: %s on address %p\n",
229 bug_type, info->access_addr);
230 pr_err("%s of size %zu by task %s/%d\n",
231 info->is_write ? "Write" : "Read",
232 info->access_size, current->comm,
233 task_pid_nr(current));
236 print_error_description(info);
237 print_address_description(info);
238 print_shadow_for_address(info->first_bad_addr);
240 pr_err("================================="
241 "=================================\n");
242 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
243 spin_unlock_irqrestore(&report_lock, flags);
244 kasan_enable_current();
247 void kasan_report(unsigned long addr, size_t size,
248 bool is_write, unsigned long ip)
250 struct kasan_access_info info;
252 if (likely(!kasan_report_enabled()))
255 disable_trace_on_warning();
257 info.access_addr = (void *)addr;
258 info.access_size = size;
259 info.is_write = is_write;
262 kasan_report_error(&info);
266 #define DEFINE_ASAN_REPORT_LOAD(size) \
267 void __asan_report_load##size##_noabort(unsigned long addr) \
269 kasan_report(addr, size, false, _RET_IP_); \
271 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
273 #define DEFINE_ASAN_REPORT_STORE(size) \
274 void __asan_report_store##size##_noabort(unsigned long addr) \
276 kasan_report(addr, size, true, _RET_IP_); \
278 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
280 DEFINE_ASAN_REPORT_LOAD(1);
281 DEFINE_ASAN_REPORT_LOAD(2);
282 DEFINE_ASAN_REPORT_LOAD(4);
283 DEFINE_ASAN_REPORT_LOAD(8);
284 DEFINE_ASAN_REPORT_LOAD(16);
285 DEFINE_ASAN_REPORT_STORE(1);
286 DEFINE_ASAN_REPORT_STORE(2);
287 DEFINE_ASAN_REPORT_STORE(4);
288 DEFINE_ASAN_REPORT_STORE(8);
289 DEFINE_ASAN_REPORT_STORE(16);
291 void __asan_report_load_n_noabort(unsigned long addr, size_t size)
293 kasan_report(addr, size, false, _RET_IP_);
295 EXPORT_SYMBOL(__asan_report_load_n_noabort);
297 void __asan_report_store_n_noabort(unsigned long addr, size_t size)
299 kasan_report(addr, size, true, _RET_IP_);
301 EXPORT_SYMBOL(__asan_report_store_n_noabort);