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/slab.h>
20 #include <linux/stackdepot.h>
21 #include <linux/stacktrace.h>
22 #include <linux/string.h>
23 #include <linux/types.h>
24 #include <linux/kasan.h>
25 #include <linux/module.h>
27 #include <asm/sections.h>
32 void *find_first_bad_addr(void *addr, size_t size)
36 while (p < addr + size && !(*(u8 *)kasan_mem_to_shadow(p)))
37 p += KASAN_GRANULE_SIZE;
41 static const char *get_shadow_bug_type(struct kasan_access_info *info)
43 const char *bug_type = "unknown-crash";
46 shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
49 * If shadow byte value is in [0, KASAN_GRANULE_SIZE) we can look
50 * at the next shadow byte to determine the type of the bad access.
52 if (*shadow_addr > 0 && *shadow_addr <= KASAN_GRANULE_SIZE - 1)
55 switch (*shadow_addr) {
56 case 0 ... KASAN_GRANULE_SIZE - 1:
58 * In theory it's still possible to see these shadow values
59 * due to a data race in the kernel code.
61 bug_type = "out-of-bounds";
63 case KASAN_PAGE_REDZONE:
64 case KASAN_KMALLOC_REDZONE:
65 bug_type = "slab-out-of-bounds";
67 case KASAN_GLOBAL_REDZONE:
68 bug_type = "global-out-of-bounds";
70 case KASAN_STACK_LEFT:
72 case KASAN_STACK_RIGHT:
73 case KASAN_STACK_PARTIAL:
74 bug_type = "stack-out-of-bounds";
77 case KASAN_KMALLOC_FREE:
78 case KASAN_KMALLOC_FREETRACK:
79 bug_type = "use-after-free";
81 case KASAN_ALLOCA_LEFT:
82 case KASAN_ALLOCA_RIGHT:
83 bug_type = "alloca-out-of-bounds";
85 case KASAN_VMALLOC_INVALID:
86 bug_type = "vmalloc-out-of-bounds";
93 static const char *get_wild_bug_type(struct kasan_access_info *info)
95 const char *bug_type = "unknown-crash";
97 if ((unsigned long)info->access_addr < PAGE_SIZE)
98 bug_type = "null-ptr-deref";
99 else if ((unsigned long)info->access_addr < TASK_SIZE)
100 bug_type = "user-memory-access";
102 bug_type = "wild-memory-access";
107 const char *get_bug_type(struct kasan_access_info *info)
110 * If access_size is a negative number, then it has reason to be
111 * defined as out-of-bounds bug type.
113 * Casting negative numbers to size_t would indeed turn up as
114 * a large size_t and its value will be larger than ULONG_MAX/2,
115 * so that this can qualify as out-of-bounds.
117 if (info->access_addr + info->access_size < info->access_addr)
118 return "out-of-bounds";
120 if (addr_has_shadow(info->access_addr))
121 return get_shadow_bug_type(info);
122 return get_wild_bug_type(info);
125 #define DEFINE_ASAN_REPORT_LOAD(size) \
126 void __asan_report_load##size##_noabort(unsigned long addr) \
128 kasan_report(addr, size, false, _RET_IP_); \
130 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
132 #define DEFINE_ASAN_REPORT_STORE(size) \
133 void __asan_report_store##size##_noabort(unsigned long addr) \
135 kasan_report(addr, size, true, _RET_IP_); \
137 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
139 DEFINE_ASAN_REPORT_LOAD(1);
140 DEFINE_ASAN_REPORT_LOAD(2);
141 DEFINE_ASAN_REPORT_LOAD(4);
142 DEFINE_ASAN_REPORT_LOAD(8);
143 DEFINE_ASAN_REPORT_LOAD(16);
144 DEFINE_ASAN_REPORT_STORE(1);
145 DEFINE_ASAN_REPORT_STORE(2);
146 DEFINE_ASAN_REPORT_STORE(4);
147 DEFINE_ASAN_REPORT_STORE(8);
148 DEFINE_ASAN_REPORT_STORE(16);
150 void __asan_report_load_n_noabort(unsigned long addr, size_t size)
152 kasan_report(addr, size, false, _RET_IP_);
154 EXPORT_SYMBOL(__asan_report_load_n_noabort);
156 void __asan_report_store_n_noabort(unsigned long addr, size_t size)
158 kasan_report(addr, size, true, _RET_IP_);
160 EXPORT_SYMBOL(__asan_report_store_n_noabort);