1 // SPDX-License-Identifier: GPL-2.0
3 * This file contains generic KASAN specific error reporting code.
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
8 * Some code borrowed from https://github.com/xairy/kasan-prototype by
9 * Andrey Konovalov <andreyknvl@gmail.com>
12 #include <linux/bitops.h>
13 #include <linux/ftrace.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
17 #include <linux/printk.h>
18 #include <linux/sched.h>
19 #include <linux/sched/task_stack.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 void *kasan_find_first_bad_addr(void *addr, size_t size)
37 while (p < addr + size && !(*(u8 *)kasan_mem_to_shadow(p)))
38 p += KASAN_GRANULE_SIZE;
42 static const char *get_shadow_bug_type(struct kasan_access_info *info)
44 const char *bug_type = "unknown-crash";
47 shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
50 * If shadow byte value is in [0, KASAN_GRANULE_SIZE) we can look
51 * at the next shadow byte to determine the type of the bad access.
53 if (*shadow_addr > 0 && *shadow_addr <= KASAN_GRANULE_SIZE - 1)
56 switch (*shadow_addr) {
57 case 0 ... KASAN_GRANULE_SIZE - 1:
59 * In theory it's still possible to see these shadow values
60 * due to a data race in the kernel code.
62 bug_type = "out-of-bounds";
64 case KASAN_PAGE_REDZONE:
65 case KASAN_KMALLOC_REDZONE:
66 bug_type = "slab-out-of-bounds";
68 case KASAN_GLOBAL_REDZONE:
69 bug_type = "global-out-of-bounds";
71 case KASAN_STACK_LEFT:
73 case KASAN_STACK_RIGHT:
74 case KASAN_STACK_PARTIAL:
75 bug_type = "stack-out-of-bounds";
78 case KASAN_KMALLOC_FREE:
79 case KASAN_KMALLOC_FREETRACK:
80 bug_type = "use-after-free";
82 case KASAN_ALLOCA_LEFT:
83 case KASAN_ALLOCA_RIGHT:
84 bug_type = "alloca-out-of-bounds";
86 case KASAN_VMALLOC_INVALID:
87 bug_type = "vmalloc-out-of-bounds";
94 static const char *get_wild_bug_type(struct kasan_access_info *info)
96 const char *bug_type = "unknown-crash";
98 if ((unsigned long)info->access_addr < PAGE_SIZE)
99 bug_type = "null-ptr-deref";
100 else if ((unsigned long)info->access_addr < TASK_SIZE)
101 bug_type = "user-memory-access";
103 bug_type = "wild-memory-access";
108 const char *kasan_get_bug_type(struct kasan_access_info *info)
111 * If access_size is a negative number, then it has reason to be
112 * defined as out-of-bounds bug type.
114 * Casting negative numbers to size_t would indeed turn up as
115 * a large size_t and its value will be larger than ULONG_MAX/2,
116 * so that this can qualify as out-of-bounds.
118 if (info->access_addr + info->access_size < info->access_addr)
119 return "out-of-bounds";
121 if (addr_has_metadata(info->access_addr))
122 return get_shadow_bug_type(info);
123 return get_wild_bug_type(info);
126 void kasan_metadata_fetch_row(char *buffer, void *row)
128 memcpy(buffer, kasan_mem_to_shadow(row), META_BYTES_PER_ROW);
131 #ifdef CONFIG_KASAN_STACK
132 static bool __must_check tokenize_frame_descr(const char **frame_descr,
133 char *token, size_t max_tok_len,
134 unsigned long *value)
136 const char *sep = strchr(*frame_descr, ' ');
139 sep = *frame_descr + strlen(*frame_descr);
142 const size_t tok_len = sep - *frame_descr;
144 if (tok_len + 1 > max_tok_len) {
145 pr_err("KASAN internal error: frame description too long: %s\n",
150 /* Copy token (+ 1 byte for '\0'). */
151 strscpy(token, *frame_descr, tok_len + 1);
154 /* Advance frame_descr past separator. */
155 *frame_descr = sep + 1;
157 if (value != NULL && kstrtoul(token, 10, value)) {
158 pr_err("KASAN internal error: not a valid number: %s\n", token);
165 static void print_decoded_frame_descr(const char *frame_descr)
168 * We need to parse the following string:
169 * "n alloc_1 alloc_2 ... alloc_n"
170 * where alloc_i looks like
171 * "offset size len name"
172 * or "offset size len name:line".
176 unsigned long num_objects;
178 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
183 pr_err("this frame has %lu %s:\n", num_objects,
184 num_objects == 1 ? "object" : "objects");
186 while (num_objects--) {
187 unsigned long offset;
191 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
195 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
198 /* name length (unused) */
199 if (!tokenize_frame_descr(&frame_descr, NULL, 0, NULL))
202 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
206 /* Strip line number; without filename it's not very helpful. */
207 strreplace(token, ':', '\0');
209 /* Finally, print object information. */
210 pr_err(" [%lu, %lu) '%s'", offset, offset + size, token);
214 static bool __must_check get_address_stack_frame_info(const void *addr,
215 unsigned long *offset,
216 const char **frame_descr,
217 const void **frame_pc)
219 unsigned long aligned_addr;
220 unsigned long mem_ptr;
221 const u8 *shadow_bottom;
222 const u8 *shadow_ptr;
223 const unsigned long *frame;
225 BUILD_BUG_ON(IS_ENABLED(CONFIG_STACK_GROWSUP));
228 * NOTE: We currently only support printing frame information for
229 * accesses to the task's own stack.
231 if (!object_is_on_stack(addr))
234 aligned_addr = round_down((unsigned long)addr, sizeof(long));
235 mem_ptr = round_down(aligned_addr, KASAN_GRANULE_SIZE);
236 shadow_ptr = kasan_mem_to_shadow((void *)aligned_addr);
237 shadow_bottom = kasan_mem_to_shadow(end_of_stack(current));
239 while (shadow_ptr >= shadow_bottom && *shadow_ptr != KASAN_STACK_LEFT) {
241 mem_ptr -= KASAN_GRANULE_SIZE;
244 while (shadow_ptr >= shadow_bottom && *shadow_ptr == KASAN_STACK_LEFT) {
246 mem_ptr -= KASAN_GRANULE_SIZE;
249 if (shadow_ptr < shadow_bottom)
252 frame = (const unsigned long *)(mem_ptr + KASAN_GRANULE_SIZE);
253 if (frame[0] != KASAN_CURRENT_STACK_FRAME_MAGIC) {
254 pr_err("KASAN internal error: frame info validation failed; invalid marker: %lu\n",
259 *offset = (unsigned long)addr - (unsigned long)frame;
260 *frame_descr = (const char *)frame[1];
261 *frame_pc = (void *)frame[2];
266 void kasan_print_address_stack_frame(const void *addr)
268 unsigned long offset;
269 const char *frame_descr;
270 const void *frame_pc;
272 if (!get_address_stack_frame_info(addr, &offset, &frame_descr,
277 * get_address_stack_frame_info only returns true if the given addr is
278 * on the current task's stack.
281 pr_err("addr %px is located in stack of task %s/%d at offset %lu in frame:\n",
282 addr, current->comm, task_pid_nr(current), offset);
283 pr_err(" %pS\n", frame_pc);
288 print_decoded_frame_descr(frame_descr);
290 #endif /* CONFIG_KASAN_STACK */
292 #define DEFINE_ASAN_REPORT_LOAD(size) \
293 void __asan_report_load##size##_noabort(unsigned long addr) \
295 kasan_report(addr, size, false, _RET_IP_); \
297 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
299 #define DEFINE_ASAN_REPORT_STORE(size) \
300 void __asan_report_store##size##_noabort(unsigned long addr) \
302 kasan_report(addr, size, true, _RET_IP_); \
304 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
306 DEFINE_ASAN_REPORT_LOAD(1);
307 DEFINE_ASAN_REPORT_LOAD(2);
308 DEFINE_ASAN_REPORT_LOAD(4);
309 DEFINE_ASAN_REPORT_LOAD(8);
310 DEFINE_ASAN_REPORT_LOAD(16);
311 DEFINE_ASAN_REPORT_STORE(1);
312 DEFINE_ASAN_REPORT_STORE(2);
313 DEFINE_ASAN_REPORT_STORE(4);
314 DEFINE_ASAN_REPORT_STORE(8);
315 DEFINE_ASAN_REPORT_STORE(16);
317 void __asan_report_load_n_noabort(unsigned long addr, size_t size)
319 kasan_report(addr, size, false, _RET_IP_);
321 EXPORT_SYMBOL(__asan_report_load_n_noabort);
323 void __asan_report_store_n_noabort(unsigned long addr, size_t size)
325 kasan_report(addr, size, true, _RET_IP_);
327 EXPORT_SYMBOL(__asan_report_store_n_noabort);