tpm: move the PPI attributes to character device directory.
[linux/fpc-iii.git] / mm / kasan / report.c
blobe07c94fbd0ac5a141ecf95ab7d39d046fea13e67
1 /*
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 of code borrowed from https://github.com/xairy/linux 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/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>
26 #include <asm/sections.h>
28 #include "kasan.h"
29 #include "../slab.h"
31 /* Shadow layout customization. */
32 #define SHADOW_BYTES_PER_BLOCK 1
33 #define SHADOW_BLOCKS_PER_ROW 16
34 #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK)
35 #define SHADOW_ROWS_AROUND_ADDR 2
37 static const void *find_first_bad_addr(const void *addr, size_t size)
39 u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr);
40 const void *first_bad_addr = addr;
42 while (!shadow_val && first_bad_addr < addr + size) {
43 first_bad_addr += KASAN_SHADOW_SCALE_SIZE;
44 shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr);
46 return first_bad_addr;
49 static void print_error_description(struct kasan_access_info *info)
51 const char *bug_type = "unknown crash";
52 u8 shadow_val;
54 info->first_bad_addr = find_first_bad_addr(info->access_addr,
55 info->access_size);
57 shadow_val = *(u8 *)kasan_mem_to_shadow(info->first_bad_addr);
59 switch (shadow_val) {
60 case KASAN_FREE_PAGE:
61 case KASAN_KMALLOC_FREE:
62 bug_type = "use after free";
63 break;
64 case KASAN_PAGE_REDZONE:
65 case KASAN_KMALLOC_REDZONE:
66 case KASAN_GLOBAL_REDZONE:
67 case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
68 bug_type = "out of bounds access";
69 break;
70 case KASAN_STACK_LEFT:
71 case KASAN_STACK_MID:
72 case KASAN_STACK_RIGHT:
73 case KASAN_STACK_PARTIAL:
74 bug_type = "out of bounds on stack";
75 break;
78 pr_err("BUG: KASan: %s in %pS at addr %p\n",
79 bug_type, (void *)info->ip,
80 info->access_addr);
81 pr_err("%s of size %zu by task %s/%d\n",
82 info->is_write ? "Write" : "Read",
83 info->access_size, current->comm, task_pid_nr(current));
86 static inline bool kernel_or_module_addr(const void *addr)
88 return (addr >= (void *)_stext && addr < (void *)_end)
89 || (addr >= (void *)MODULES_VADDR
90 && addr < (void *)MODULES_END);
93 static inline bool init_task_stack_addr(const void *addr)
95 return addr >= (void *)&init_thread_union.stack &&
96 (addr <= (void *)&init_thread_union.stack +
97 sizeof(init_thread_union.stack));
100 static void print_address_description(struct kasan_access_info *info)
102 const void *addr = info->access_addr;
104 if ((addr >= (void *)PAGE_OFFSET) &&
105 (addr < high_memory)) {
106 struct page *page = virt_to_head_page(addr);
108 if (PageSlab(page)) {
109 void *object;
110 struct kmem_cache *cache = page->slab_cache;
111 void *last_object;
113 object = virt_to_obj(cache, page_address(page), addr);
114 last_object = page_address(page) +
115 page->objects * cache->size;
117 if (unlikely(object > last_object))
118 object = last_object; /* we hit into padding */
120 object_err(cache, page, object,
121 "kasan: bad access detected");
122 return;
124 dump_page(page, "kasan: bad access detected");
127 if (kernel_or_module_addr(addr)) {
128 if (!init_task_stack_addr(addr))
129 pr_err("Address belongs to variable %pS\n", addr);
132 dump_stack();
135 static bool row_is_guilty(const void *row, const void *guilty)
137 return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
140 static int shadow_pointer_offset(const void *row, const void *shadow)
142 /* The length of ">ff00ff00ff00ff00: " is
143 * 3 + (BITS_PER_LONG/8)*2 chars.
145 return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
146 (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
149 static void print_shadow_for_address(const void *addr)
151 int i;
152 const void *shadow = kasan_mem_to_shadow(addr);
153 const void *shadow_row;
155 shadow_row = (void *)round_down((unsigned long)shadow,
156 SHADOW_BYTES_PER_ROW)
157 - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
159 pr_err("Memory state around the buggy address:\n");
161 for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
162 const void *kaddr = kasan_shadow_to_mem(shadow_row);
163 char buffer[4 + (BITS_PER_LONG/8)*2];
165 snprintf(buffer, sizeof(buffer),
166 (i == 0) ? ">%p: " : " %p: ", kaddr);
168 kasan_disable_current();
169 print_hex_dump(KERN_ERR, buffer,
170 DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
171 shadow_row, SHADOW_BYTES_PER_ROW, 0);
172 kasan_enable_current();
174 if (row_is_guilty(shadow_row, shadow))
175 pr_err("%*c\n",
176 shadow_pointer_offset(shadow_row, shadow),
177 '^');
179 shadow_row += SHADOW_BYTES_PER_ROW;
183 static DEFINE_SPINLOCK(report_lock);
185 void kasan_report_error(struct kasan_access_info *info)
187 unsigned long flags;
189 spin_lock_irqsave(&report_lock, flags);
190 pr_err("================================="
191 "=================================\n");
192 print_error_description(info);
193 print_address_description(info);
194 print_shadow_for_address(info->first_bad_addr);
195 pr_err("================================="
196 "=================================\n");
197 spin_unlock_irqrestore(&report_lock, flags);
200 void kasan_report_user_access(struct kasan_access_info *info)
202 unsigned long flags;
204 spin_lock_irqsave(&report_lock, flags);
205 pr_err("================================="
206 "=================================\n");
207 pr_err("BUG: KASan: user-memory-access on address %p\n",
208 info->access_addr);
209 pr_err("%s of size %zu by task %s/%d\n",
210 info->is_write ? "Write" : "Read",
211 info->access_size, current->comm, task_pid_nr(current));
212 dump_stack();
213 pr_err("================================="
214 "=================================\n");
215 spin_unlock_irqrestore(&report_lock, flags);
218 void kasan_report(unsigned long addr, size_t size,
219 bool is_write, unsigned long ip)
221 struct kasan_access_info info;
223 if (likely(!kasan_enabled()))
224 return;
226 info.access_addr = (void *)addr;
227 info.access_size = size;
228 info.is_write = is_write;
229 info.ip = ip;
230 kasan_report_error(&info);
234 #define DEFINE_ASAN_REPORT_LOAD(size) \
235 void __asan_report_load##size##_noabort(unsigned long addr) \
237 kasan_report(addr, size, false, _RET_IP_); \
239 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
241 #define DEFINE_ASAN_REPORT_STORE(size) \
242 void __asan_report_store##size##_noabort(unsigned long addr) \
244 kasan_report(addr, size, true, _RET_IP_); \
246 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
248 DEFINE_ASAN_REPORT_LOAD(1);
249 DEFINE_ASAN_REPORT_LOAD(2);
250 DEFINE_ASAN_REPORT_LOAD(4);
251 DEFINE_ASAN_REPORT_LOAD(8);
252 DEFINE_ASAN_REPORT_LOAD(16);
253 DEFINE_ASAN_REPORT_STORE(1);
254 DEFINE_ASAN_REPORT_STORE(2);
255 DEFINE_ASAN_REPORT_STORE(4);
256 DEFINE_ASAN_REPORT_STORE(8);
257 DEFINE_ASAN_REPORT_STORE(16);
259 void __asan_report_load_n_noabort(unsigned long addr, size_t size)
261 kasan_report(addr, size, false, _RET_IP_);
263 EXPORT_SYMBOL(__asan_report_load_n_noabort);
265 void __asan_report_store_n_noabort(unsigned long addr, size_t size)
267 kasan_report(addr, size, true, _RET_IP_);
269 EXPORT_SYMBOL(__asan_report_store_n_noabort);