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 if (!addr_has_metadata(p))
40 while (p < addr + size && !(*(u8 *)kasan_mem_to_shadow(p)))
41 p += KASAN_GRANULE_SIZE;
46 static const char *get_shadow_bug_type(struct kasan_report_info *info)
48 const char *bug_type = "unknown-crash";
51 shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
54 * If shadow byte value is in [0, KASAN_GRANULE_SIZE) we can look
55 * at the next shadow byte to determine the type of the bad access.
57 if (*shadow_addr > 0 && *shadow_addr <= KASAN_GRANULE_SIZE - 1)
60 switch (*shadow_addr) {
61 case 0 ... KASAN_GRANULE_SIZE - 1:
63 * In theory it's still possible to see these shadow values
64 * due to a data race in the kernel code.
66 bug_type = "out-of-bounds";
68 case KASAN_PAGE_REDZONE:
69 case KASAN_SLAB_REDZONE:
70 bug_type = "slab-out-of-bounds";
72 case KASAN_GLOBAL_REDZONE:
73 bug_type = "global-out-of-bounds";
75 case KASAN_STACK_LEFT:
77 case KASAN_STACK_RIGHT:
78 case KASAN_STACK_PARTIAL:
79 bug_type = "stack-out-of-bounds";
83 case KASAN_SLAB_FREETRACK:
84 bug_type = "use-after-free";
86 case KASAN_ALLOCA_LEFT:
87 case KASAN_ALLOCA_RIGHT:
88 bug_type = "alloca-out-of-bounds";
90 case KASAN_VMALLOC_INVALID:
91 bug_type = "vmalloc-out-of-bounds";
98 static const char *get_wild_bug_type(struct kasan_report_info *info)
100 const char *bug_type = "unknown-crash";
102 if ((unsigned long)info->access_addr < PAGE_SIZE)
103 bug_type = "null-ptr-deref";
104 else if ((unsigned long)info->access_addr < TASK_SIZE)
105 bug_type = "user-memory-access";
107 bug_type = "wild-memory-access";
112 static const char *get_bug_type(struct kasan_report_info *info)
115 * If access_size is a negative number, then it has reason to be
116 * defined as out-of-bounds bug type.
118 * Casting negative numbers to size_t would indeed turn up as
119 * a large size_t and its value will be larger than ULONG_MAX/2,
120 * so that this can qualify as out-of-bounds.
122 if (info->access_addr + info->access_size < info->access_addr)
123 return "out-of-bounds";
125 if (addr_has_metadata(info->access_addr))
126 return get_shadow_bug_type(info);
127 return get_wild_bug_type(info);
130 void kasan_complete_mode_report_info(struct kasan_report_info *info)
132 struct kasan_alloc_meta *alloc_meta;
133 struct kasan_free_meta *free_meta;
136 info->bug_type = get_bug_type(info);
138 if (!info->cache || !info->object)
141 alloc_meta = kasan_get_alloc_meta(info->cache, info->object);
143 memcpy(&info->alloc_track, &alloc_meta->alloc_track,
144 sizeof(info->alloc_track));
146 if (*(u8 *)kasan_mem_to_shadow(info->object) == KASAN_SLAB_FREETRACK) {
147 /* Free meta must be present with KASAN_SLAB_FREETRACK. */
148 free_meta = kasan_get_free_meta(info->cache, info->object);
149 memcpy(&info->free_track, &free_meta->free_track,
150 sizeof(info->free_track));
154 void kasan_metadata_fetch_row(char *buffer, void *row)
156 memcpy(buffer, kasan_mem_to_shadow(row), META_BYTES_PER_ROW);
159 void kasan_print_aux_stacks(struct kmem_cache *cache, const void *object)
161 struct kasan_alloc_meta *alloc_meta;
163 alloc_meta = kasan_get_alloc_meta(cache, object);
167 if (alloc_meta->aux_stack[0]) {
168 pr_err("Last potentially related work creation:\n");
169 stack_depot_print(alloc_meta->aux_stack[0]);
172 if (alloc_meta->aux_stack[1]) {
173 pr_err("Second to last potentially related work creation:\n");
174 stack_depot_print(alloc_meta->aux_stack[1]);
179 #ifdef CONFIG_KASAN_STACK
180 static bool __must_check tokenize_frame_descr(const char **frame_descr,
181 char *token, size_t max_tok_len,
182 unsigned long *value)
184 const char *sep = strchr(*frame_descr, ' ');
187 sep = *frame_descr + strlen(*frame_descr);
190 const size_t tok_len = sep - *frame_descr;
192 if (tok_len + 1 > max_tok_len) {
193 pr_err("KASAN internal error: frame description too long: %s\n",
198 /* Copy token (+ 1 byte for '\0'). */
199 strscpy(token, *frame_descr, tok_len + 1);
202 /* Advance frame_descr past separator. */
203 *frame_descr = sep + 1;
205 if (value != NULL && kstrtoul(token, 10, value)) {
206 pr_err("KASAN internal error: not a valid number: %s\n", token);
213 static void print_decoded_frame_descr(const char *frame_descr)
216 * We need to parse the following string:
217 * "n alloc_1 alloc_2 ... alloc_n"
218 * where alloc_i looks like
219 * "offset size len name"
220 * or "offset size len name:line".
224 unsigned long num_objects;
226 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
231 pr_err("This frame has %lu %s:\n", num_objects,
232 num_objects == 1 ? "object" : "objects");
234 while (num_objects--) {
235 unsigned long offset;
239 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
243 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
246 /* name length (unused) */
247 if (!tokenize_frame_descr(&frame_descr, NULL, 0, NULL))
250 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
254 /* Strip line number; without filename it's not very helpful. */
255 strreplace(token, ':', '\0');
257 /* Finally, print object information. */
258 pr_err(" [%lu, %lu) '%s'", offset, offset + size, token);
262 /* Returns true only if the address is on the current task's stack. */
263 static bool __must_check get_address_stack_frame_info(const void *addr,
264 unsigned long *offset,
265 const char **frame_descr,
266 const void **frame_pc)
268 unsigned long aligned_addr;
269 unsigned long mem_ptr;
270 const u8 *shadow_bottom;
271 const u8 *shadow_ptr;
272 const unsigned long *frame;
274 BUILD_BUG_ON(IS_ENABLED(CONFIG_STACK_GROWSUP));
276 aligned_addr = round_down((unsigned long)addr, sizeof(long));
277 mem_ptr = round_down(aligned_addr, KASAN_GRANULE_SIZE);
278 shadow_ptr = kasan_mem_to_shadow((void *)aligned_addr);
279 shadow_bottom = kasan_mem_to_shadow(end_of_stack(current));
281 while (shadow_ptr >= shadow_bottom && *shadow_ptr != KASAN_STACK_LEFT) {
283 mem_ptr -= KASAN_GRANULE_SIZE;
286 while (shadow_ptr >= shadow_bottom && *shadow_ptr == KASAN_STACK_LEFT) {
288 mem_ptr -= KASAN_GRANULE_SIZE;
291 if (shadow_ptr < shadow_bottom)
294 frame = (const unsigned long *)(mem_ptr + KASAN_GRANULE_SIZE);
295 if (frame[0] != KASAN_CURRENT_STACK_FRAME_MAGIC) {
296 pr_err("KASAN internal error: frame info validation failed; invalid marker: %lu\n",
301 *offset = (unsigned long)addr - (unsigned long)frame;
302 *frame_descr = (const char *)frame[1];
303 *frame_pc = (void *)frame[2];
308 void kasan_print_address_stack_frame(const void *addr)
310 unsigned long offset;
311 const char *frame_descr;
312 const void *frame_pc;
314 if (WARN_ON(!object_is_on_stack(addr)))
317 pr_err("The buggy address belongs to stack of task %s/%d\n",
318 current->comm, task_pid_nr(current));
320 if (!get_address_stack_frame_info(addr, &offset, &frame_descr,
324 pr_err(" and is located at offset %lu in frame:\n", offset);
325 pr_err(" %pS\n", frame_pc);
330 print_decoded_frame_descr(frame_descr);
332 #endif /* CONFIG_KASAN_STACK */
334 #define DEFINE_ASAN_REPORT_LOAD(size) \
335 void __asan_report_load##size##_noabort(unsigned long addr) \
337 kasan_report(addr, size, false, _RET_IP_); \
339 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
341 #define DEFINE_ASAN_REPORT_STORE(size) \
342 void __asan_report_store##size##_noabort(unsigned long addr) \
344 kasan_report(addr, size, true, _RET_IP_); \
346 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
348 DEFINE_ASAN_REPORT_LOAD(1);
349 DEFINE_ASAN_REPORT_LOAD(2);
350 DEFINE_ASAN_REPORT_LOAD(4);
351 DEFINE_ASAN_REPORT_LOAD(8);
352 DEFINE_ASAN_REPORT_LOAD(16);
353 DEFINE_ASAN_REPORT_STORE(1);
354 DEFINE_ASAN_REPORT_STORE(2);
355 DEFINE_ASAN_REPORT_STORE(4);
356 DEFINE_ASAN_REPORT_STORE(8);
357 DEFINE_ASAN_REPORT_STORE(16);
359 void __asan_report_load_n_noabort(unsigned long addr, size_t size)
361 kasan_report(addr, size, false, _RET_IP_);
363 EXPORT_SYMBOL(__asan_report_load_n_noabort);
365 void __asan_report_store_n_noabort(unsigned long addr, size_t size)
367 kasan_report(addr, size, true, _RET_IP_);
369 EXPORT_SYMBOL(__asan_report_store_n_noabort);