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/kernel.h>
18 #include <linux/printk.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/stackdepot.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";
93 case KASAN_USE_AFTER_SCOPE
:
94 bug_type
= "use-after-scope";
98 pr_err("BUG: KASAN: %s in %pS at addr %p\n",
99 bug_type
, (void *)info
->ip
,
101 pr_err("%s of size %zu by task %s/%d\n",
102 info
->is_write
? "Write" : "Read",
103 info
->access_size
, current
->comm
, task_pid_nr(current
));
106 static inline bool kernel_or_module_addr(const void *addr
)
108 if (addr
>= (void *)_stext
&& addr
< (void *)_end
)
110 if (is_module_address((unsigned long)addr
))
115 static inline bool init_task_stack_addr(const void *addr
)
117 return addr
>= (void *)&init_thread_union
.stack
&&
118 (addr
<= (void *)&init_thread_union
.stack
+
119 sizeof(init_thread_union
.stack
));
122 static DEFINE_SPINLOCK(report_lock
);
124 static void kasan_start_report(unsigned long *flags
)
127 * Make sure we don't end up in loop.
129 kasan_disable_current();
130 spin_lock_irqsave(&report_lock
, *flags
);
131 pr_err("==================================================================\n");
134 static void kasan_end_report(unsigned long *flags
)
136 pr_err("==================================================================\n");
137 add_taint(TAINT_BAD_PAGE
, LOCKDEP_NOW_UNRELIABLE
);
138 spin_unlock_irqrestore(&report_lock
, *flags
);
139 kasan_enable_current();
142 static void print_track(struct kasan_track
*track
)
144 pr_err("PID = %u\n", track
->pid
);
146 struct stack_trace trace
;
148 depot_fetch_stack(track
->stack
, &trace
);
149 print_stack_trace(&trace
, 0);
151 pr_err("(stack is not available)\n");
155 static void kasan_object_err(struct kmem_cache
*cache
, void *object
)
157 struct kasan_alloc_meta
*alloc_info
= get_alloc_info(cache
, object
);
160 pr_err("Object at %p, in cache %s size: %d\n", object
, cache
->name
,
163 if (!(cache
->flags
& SLAB_KASAN
))
166 pr_err("Allocated:\n");
167 print_track(&alloc_info
->alloc_track
);
169 print_track(&alloc_info
->free_track
);
172 void kasan_report_double_free(struct kmem_cache
*cache
, void *object
,
177 kasan_start_report(&flags
);
178 pr_err("BUG: Double free or freeing an invalid pointer\n");
179 pr_err("Unexpected shadow byte: 0x%hhX\n", shadow
);
180 kasan_object_err(cache
, object
);
181 kasan_end_report(&flags
);
184 static void print_address_description(struct kasan_access_info
*info
)
186 const void *addr
= info
->access_addr
;
188 if ((addr
>= (void *)PAGE_OFFSET
) &&
189 (addr
< high_memory
)) {
190 struct page
*page
= virt_to_head_page(addr
);
192 if (PageSlab(page
)) {
194 struct kmem_cache
*cache
= page
->slab_cache
;
195 object
= nearest_obj(cache
, page
,
196 (void *)info
->access_addr
);
197 kasan_object_err(cache
, object
);
200 dump_page(page
, "kasan: bad access detected");
203 if (kernel_or_module_addr(addr
)) {
204 if (!init_task_stack_addr(addr
))
205 pr_err("Address belongs to variable %pS\n", addr
);
210 static bool row_is_guilty(const void *row
, const void *guilty
)
212 return (row
<= guilty
) && (guilty
< row
+ SHADOW_BYTES_PER_ROW
);
215 static int shadow_pointer_offset(const void *row
, const void *shadow
)
217 /* The length of ">ff00ff00ff00ff00: " is
218 * 3 + (BITS_PER_LONG/8)*2 chars.
220 return 3 + (BITS_PER_LONG
/8)*2 + (shadow
- row
)*2 +
221 (shadow
- row
) / SHADOW_BYTES_PER_BLOCK
+ 1;
224 static void print_shadow_for_address(const void *addr
)
227 const void *shadow
= kasan_mem_to_shadow(addr
);
228 const void *shadow_row
;
230 shadow_row
= (void *)round_down((unsigned long)shadow
,
231 SHADOW_BYTES_PER_ROW
)
232 - SHADOW_ROWS_AROUND_ADDR
* SHADOW_BYTES_PER_ROW
;
234 pr_err("Memory state around the buggy address:\n");
236 for (i
= -SHADOW_ROWS_AROUND_ADDR
; i
<= SHADOW_ROWS_AROUND_ADDR
; i
++) {
237 const void *kaddr
= kasan_shadow_to_mem(shadow_row
);
238 char buffer
[4 + (BITS_PER_LONG
/8)*2];
239 char shadow_buf
[SHADOW_BYTES_PER_ROW
];
241 snprintf(buffer
, sizeof(buffer
),
242 (i
== 0) ? ">%p: " : " %p: ", kaddr
);
244 * We should not pass a shadow pointer to generic
245 * function, because generic functions may try to
246 * access kasan mapping for the passed address.
248 memcpy(shadow_buf
, shadow_row
, SHADOW_BYTES_PER_ROW
);
249 print_hex_dump(KERN_ERR
, buffer
,
250 DUMP_PREFIX_NONE
, SHADOW_BYTES_PER_ROW
, 1,
251 shadow_buf
, SHADOW_BYTES_PER_ROW
, 0);
253 if (row_is_guilty(shadow_row
, shadow
))
255 shadow_pointer_offset(shadow_row
, shadow
),
258 shadow_row
+= SHADOW_BYTES_PER_ROW
;
262 static void kasan_report_error(struct kasan_access_info
*info
)
265 const char *bug_type
;
267 kasan_start_report(&flags
);
269 if (info
->access_addr
<
270 kasan_shadow_to_mem((void *)KASAN_SHADOW_START
)) {
271 if ((unsigned long)info
->access_addr
< PAGE_SIZE
)
272 bug_type
= "null-ptr-deref";
273 else if ((unsigned long)info
->access_addr
< TASK_SIZE
)
274 bug_type
= "user-memory-access";
276 bug_type
= "wild-memory-access";
277 pr_err("BUG: KASAN: %s on address %p\n",
278 bug_type
, info
->access_addr
);
279 pr_err("%s of size %zu by task %s/%d\n",
280 info
->is_write
? "Write" : "Read",
281 info
->access_size
, current
->comm
,
282 task_pid_nr(current
));
285 print_error_description(info
);
286 print_address_description(info
);
287 print_shadow_for_address(info
->first_bad_addr
);
290 kasan_end_report(&flags
);
293 void kasan_report(unsigned long addr
, size_t size
,
294 bool is_write
, unsigned long ip
)
296 struct kasan_access_info info
;
298 if (likely(!kasan_report_enabled()))
301 info
.access_addr
= (void *)addr
;
302 info
.access_size
= size
;
303 info
.is_write
= is_write
;
306 kasan_report_error(&info
);
310 #define DEFINE_ASAN_REPORT_LOAD(size) \
311 void __asan_report_load##size##_noabort(unsigned long addr) \
313 kasan_report(addr, size, false, _RET_IP_); \
315 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
317 #define DEFINE_ASAN_REPORT_STORE(size) \
318 void __asan_report_store##size##_noabort(unsigned long addr) \
320 kasan_report(addr, size, true, _RET_IP_); \
322 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
324 DEFINE_ASAN_REPORT_LOAD(1);
325 DEFINE_ASAN_REPORT_LOAD(2);
326 DEFINE_ASAN_REPORT_LOAD(4);
327 DEFINE_ASAN_REPORT_LOAD(8);
328 DEFINE_ASAN_REPORT_LOAD(16);
329 DEFINE_ASAN_REPORT_STORE(1);
330 DEFINE_ASAN_REPORT_STORE(2);
331 DEFINE_ASAN_REPORT_STORE(4);
332 DEFINE_ASAN_REPORT_STORE(8);
333 DEFINE_ASAN_REPORT_STORE(16);
335 void __asan_report_load_n_noabort(unsigned long addr
, size_t size
)
337 kasan_report(addr
, size
, false, _RET_IP_
);
339 EXPORT_SYMBOL(__asan_report_load_n_noabort
);
341 void __asan_report_store_n_noabort(unsigned long addr
, size_t size
)
343 kasan_report(addr
, size
, true, _RET_IP_
);
345 EXPORT_SYMBOL(__asan_report_store_n_noabort
);