i2c: i801: Fix Failed to allocate irq -2147483648 error
[linux/fpc-iii.git] / mm / kasan / report.c
blob8ca412aebcf1f39c65de1e09b89d8fdcf071efca
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 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>
18 #include <linux/mm.h>
19 #include <linux/printk.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/stackdepot.h>
23 #include <linux/stacktrace.h>
24 #include <linux/string.h>
25 #include <linux/types.h>
26 #include <linux/kasan.h>
27 #include <linux/module.h>
29 #include <asm/sections.h>
31 #include "kasan.h"
32 #include "../slab.h"
34 /* Shadow layout customization. */
35 #define SHADOW_BYTES_PER_BLOCK 1
36 #define SHADOW_BLOCKS_PER_ROW 16
37 #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK)
38 #define SHADOW_ROWS_AROUND_ADDR 2
40 static const void *find_first_bad_addr(const void *addr, size_t size)
42 u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr);
43 const void *first_bad_addr = addr;
45 while (!shadow_val && first_bad_addr < addr + size) {
46 first_bad_addr += KASAN_SHADOW_SCALE_SIZE;
47 shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr);
49 return first_bad_addr;
52 static void print_error_description(struct kasan_access_info *info)
54 const char *bug_type = "unknown-crash";
55 u8 *shadow_addr;
57 info->first_bad_addr = find_first_bad_addr(info->access_addr,
58 info->access_size);
60 shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
63 * If shadow byte value is in [0, KASAN_SHADOW_SCALE_SIZE) we can look
64 * at the next shadow byte to determine the type of the bad access.
66 if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1)
67 shadow_addr++;
69 switch (*shadow_addr) {
70 case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
72 * In theory it's still possible to see these shadow values
73 * due to a data race in the kernel code.
75 bug_type = "out-of-bounds";
76 break;
77 case KASAN_PAGE_REDZONE:
78 case KASAN_KMALLOC_REDZONE:
79 bug_type = "slab-out-of-bounds";
80 break;
81 case KASAN_GLOBAL_REDZONE:
82 bug_type = "global-out-of-bounds";
83 break;
84 case KASAN_STACK_LEFT:
85 case KASAN_STACK_MID:
86 case KASAN_STACK_RIGHT:
87 case KASAN_STACK_PARTIAL:
88 bug_type = "stack-out-of-bounds";
89 break;
90 case KASAN_FREE_PAGE:
91 case KASAN_KMALLOC_FREE:
92 bug_type = "use-after-free";
93 break;
94 case KASAN_USE_AFTER_SCOPE:
95 bug_type = "use-after-scope";
96 break;
99 pr_err("BUG: KASAN: %s in %pS at addr %p\n",
100 bug_type, (void *)info->ip,
101 info->access_addr);
102 pr_err("%s of size %zu by task %s/%d\n",
103 info->is_write ? "Write" : "Read",
104 info->access_size, current->comm, task_pid_nr(current));
107 static inline bool kernel_or_module_addr(const void *addr)
109 if (addr >= (void *)_stext && addr < (void *)_end)
110 return true;
111 if (is_module_address((unsigned long)addr))
112 return true;
113 return false;
116 static inline bool init_task_stack_addr(const void *addr)
118 return addr >= (void *)&init_thread_union.stack &&
119 (addr <= (void *)&init_thread_union.stack +
120 sizeof(init_thread_union.stack));
123 static DEFINE_SPINLOCK(report_lock);
125 static void kasan_start_report(unsigned long *flags)
128 * Make sure we don't end up in loop.
130 kasan_disable_current();
131 spin_lock_irqsave(&report_lock, *flags);
132 pr_err("==================================================================\n");
135 static void kasan_end_report(unsigned long *flags)
137 pr_err("==================================================================\n");
138 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
139 spin_unlock_irqrestore(&report_lock, *flags);
140 kasan_enable_current();
143 static void print_track(struct kasan_track *track)
145 pr_err("PID = %u\n", track->pid);
146 if (track->stack) {
147 struct stack_trace trace;
149 depot_fetch_stack(track->stack, &trace);
150 print_stack_trace(&trace, 0);
151 } else {
152 pr_err("(stack is not available)\n");
156 static void kasan_object_err(struct kmem_cache *cache, void *object)
158 struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
160 dump_stack();
161 pr_err("Object at %p, in cache %s size: %d\n", object, cache->name,
162 cache->object_size);
164 if (!(cache->flags & SLAB_KASAN))
165 return;
167 pr_err("Allocated:\n");
168 print_track(&alloc_info->alloc_track);
169 pr_err("Freed:\n");
170 print_track(&alloc_info->free_track);
173 void kasan_report_double_free(struct kmem_cache *cache, void *object,
174 s8 shadow)
176 unsigned long flags;
178 kasan_start_report(&flags);
179 pr_err("BUG: Double free or freeing an invalid pointer\n");
180 pr_err("Unexpected shadow byte: 0x%hhX\n", shadow);
181 kasan_object_err(cache, object);
182 kasan_end_report(&flags);
185 static void print_address_description(struct kasan_access_info *info)
187 const void *addr = info->access_addr;
189 if ((addr >= (void *)PAGE_OFFSET) &&
190 (addr < high_memory)) {
191 struct page *page = virt_to_head_page(addr);
193 if (PageSlab(page)) {
194 void *object;
195 struct kmem_cache *cache = page->slab_cache;
196 object = nearest_obj(cache, page,
197 (void *)info->access_addr);
198 kasan_object_err(cache, object);
199 return;
201 dump_page(page, "kasan: bad access detected");
204 if (kernel_or_module_addr(addr)) {
205 if (!init_task_stack_addr(addr))
206 pr_err("Address belongs to variable %pS\n", addr);
208 dump_stack();
211 static bool row_is_guilty(const void *row, const void *guilty)
213 return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
216 static int shadow_pointer_offset(const void *row, const void *shadow)
218 /* The length of ">ff00ff00ff00ff00: " is
219 * 3 + (BITS_PER_LONG/8)*2 chars.
221 return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
222 (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
225 static void print_shadow_for_address(const void *addr)
227 int i;
228 const void *shadow = kasan_mem_to_shadow(addr);
229 const void *shadow_row;
231 shadow_row = (void *)round_down((unsigned long)shadow,
232 SHADOW_BYTES_PER_ROW)
233 - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
235 pr_err("Memory state around the buggy address:\n");
237 for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
238 const void *kaddr = kasan_shadow_to_mem(shadow_row);
239 char buffer[4 + (BITS_PER_LONG/8)*2];
240 char shadow_buf[SHADOW_BYTES_PER_ROW];
242 snprintf(buffer, sizeof(buffer),
243 (i == 0) ? ">%p: " : " %p: ", kaddr);
245 * We should not pass a shadow pointer to generic
246 * function, because generic functions may try to
247 * access kasan mapping for the passed address.
249 memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
250 print_hex_dump(KERN_ERR, buffer,
251 DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
252 shadow_buf, SHADOW_BYTES_PER_ROW, 0);
254 if (row_is_guilty(shadow_row, shadow))
255 pr_err("%*c\n",
256 shadow_pointer_offset(shadow_row, shadow),
257 '^');
259 shadow_row += SHADOW_BYTES_PER_ROW;
263 static void kasan_report_error(struct kasan_access_info *info)
265 unsigned long flags;
266 const char *bug_type;
268 kasan_start_report(&flags);
270 if (info->access_addr <
271 kasan_shadow_to_mem((void *)KASAN_SHADOW_START)) {
272 if ((unsigned long)info->access_addr < PAGE_SIZE)
273 bug_type = "null-ptr-deref";
274 else if ((unsigned long)info->access_addr < TASK_SIZE)
275 bug_type = "user-memory-access";
276 else
277 bug_type = "wild-memory-access";
278 pr_err("BUG: KASAN: %s on address %p\n",
279 bug_type, info->access_addr);
280 pr_err("%s of size %zu by task %s/%d\n",
281 info->is_write ? "Write" : "Read",
282 info->access_size, current->comm,
283 task_pid_nr(current));
284 dump_stack();
285 } else {
286 print_error_description(info);
287 print_address_description(info);
288 print_shadow_for_address(info->first_bad_addr);
291 kasan_end_report(&flags);
294 void kasan_report(unsigned long addr, size_t size,
295 bool is_write, unsigned long ip)
297 struct kasan_access_info info;
299 if (likely(!kasan_report_enabled()))
300 return;
302 disable_trace_on_warning();
304 info.access_addr = (void *)addr;
305 info.access_size = size;
306 info.is_write = is_write;
307 info.ip = ip;
309 kasan_report_error(&info);
313 #define DEFINE_ASAN_REPORT_LOAD(size) \
314 void __asan_report_load##size##_noabort(unsigned long addr) \
316 kasan_report(addr, size, false, _RET_IP_); \
318 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
320 #define DEFINE_ASAN_REPORT_STORE(size) \
321 void __asan_report_store##size##_noabort(unsigned long addr) \
323 kasan_report(addr, size, true, _RET_IP_); \
325 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
327 DEFINE_ASAN_REPORT_LOAD(1);
328 DEFINE_ASAN_REPORT_LOAD(2);
329 DEFINE_ASAN_REPORT_LOAD(4);
330 DEFINE_ASAN_REPORT_LOAD(8);
331 DEFINE_ASAN_REPORT_LOAD(16);
332 DEFINE_ASAN_REPORT_STORE(1);
333 DEFINE_ASAN_REPORT_STORE(2);
334 DEFINE_ASAN_REPORT_STORE(4);
335 DEFINE_ASAN_REPORT_STORE(8);
336 DEFINE_ASAN_REPORT_STORE(16);
338 void __asan_report_load_n_noabort(unsigned long addr, size_t size)
340 kasan_report(addr, size, false, _RET_IP_);
342 EXPORT_SYMBOL(__asan_report_load_n_noabort);
344 void __asan_report_store_n_noabort(unsigned long addr, size_t size)
346 kasan_report(addr, size, true, _RET_IP_);
348 EXPORT_SYMBOL(__asan_report_store_n_noabort);