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 const void *kasan_find_first_bad_addr(const 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 size_t kasan_get_alloc_size(void *object, struct kmem_cache *cache)
52 * Skip the addr_has_metadata check, as this function only operates on
53 * slab memory, which must have metadata.
57 * The loop below returns 0 for freed objects, for which KASAN cannot
58 * calculate the allocation size based on the metadata.
60 shadow = (u8 *)kasan_mem_to_shadow(object);
61 while (size < cache->object_size) {
63 size += KASAN_GRANULE_SIZE;
64 else if (*shadow >= 1 && *shadow <= KASAN_GRANULE_SIZE - 1)
65 return size + *shadow;
71 return cache->object_size;
74 static const char *get_shadow_bug_type(struct kasan_report_info *info)
76 const char *bug_type = "unknown-crash";
79 shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
82 * If shadow byte value is in [0, KASAN_GRANULE_SIZE) we can look
83 * at the next shadow byte to determine the type of the bad access.
85 if (*shadow_addr > 0 && *shadow_addr <= KASAN_GRANULE_SIZE - 1)
88 switch (*shadow_addr) {
89 case 0 ... KASAN_GRANULE_SIZE - 1:
91 * In theory it's still possible to see these shadow values
92 * due to a data race in the kernel code.
94 bug_type = "out-of-bounds";
96 case KASAN_PAGE_REDZONE:
97 case KASAN_SLAB_REDZONE:
98 bug_type = "slab-out-of-bounds";
100 case KASAN_GLOBAL_REDZONE:
101 bug_type = "global-out-of-bounds";
103 case KASAN_STACK_LEFT:
104 case KASAN_STACK_MID:
105 case KASAN_STACK_RIGHT:
106 case KASAN_STACK_PARTIAL:
107 bug_type = "stack-out-of-bounds";
109 case KASAN_PAGE_FREE:
110 bug_type = "use-after-free";
112 case KASAN_SLAB_FREE:
113 case KASAN_SLAB_FREE_META:
114 bug_type = "slab-use-after-free";
116 case KASAN_ALLOCA_LEFT:
117 case KASAN_ALLOCA_RIGHT:
118 bug_type = "alloca-out-of-bounds";
120 case KASAN_VMALLOC_INVALID:
121 bug_type = "vmalloc-out-of-bounds";
128 static const char *get_wild_bug_type(struct kasan_report_info *info)
130 const char *bug_type = "unknown-crash";
132 if ((unsigned long)info->access_addr < PAGE_SIZE)
133 bug_type = "null-ptr-deref";
134 else if ((unsigned long)info->access_addr < TASK_SIZE)
135 bug_type = "user-memory-access";
137 bug_type = "wild-memory-access";
142 static const char *get_bug_type(struct kasan_report_info *info)
145 * If access_size is a negative number, then it has reason to be
146 * defined as out-of-bounds bug type.
148 * Casting negative numbers to size_t would indeed turn up as
149 * a large size_t and its value will be larger than ULONG_MAX/2,
150 * so that this can qualify as out-of-bounds.
152 if (info->access_addr + info->access_size < info->access_addr)
153 return "out-of-bounds";
155 if (addr_has_metadata(info->access_addr))
156 return get_shadow_bug_type(info);
157 return get_wild_bug_type(info);
160 void kasan_complete_mode_report_info(struct kasan_report_info *info)
162 struct kasan_alloc_meta *alloc_meta;
163 struct kasan_free_meta *free_meta;
166 info->bug_type = get_bug_type(info);
168 if (!info->cache || !info->object)
171 alloc_meta = kasan_get_alloc_meta(info->cache, info->object);
173 memcpy(&info->alloc_track, &alloc_meta->alloc_track,
174 sizeof(info->alloc_track));
176 if (*(u8 *)kasan_mem_to_shadow(info->object) == KASAN_SLAB_FREE_META) {
177 /* Free meta must be present with KASAN_SLAB_FREE_META. */
178 free_meta = kasan_get_free_meta(info->cache, info->object);
179 memcpy(&info->free_track, &free_meta->free_track,
180 sizeof(info->free_track));
184 void kasan_metadata_fetch_row(char *buffer, void *row)
186 memcpy(buffer, kasan_mem_to_shadow(row), META_BYTES_PER_ROW);
189 void kasan_print_aux_stacks(struct kmem_cache *cache, const void *object)
191 struct kasan_alloc_meta *alloc_meta;
193 alloc_meta = kasan_get_alloc_meta(cache, object);
197 if (alloc_meta->aux_stack[0]) {
198 pr_err("Last potentially related work creation:\n");
199 stack_depot_print(alloc_meta->aux_stack[0]);
202 if (alloc_meta->aux_stack[1]) {
203 pr_err("Second to last potentially related work creation:\n");
204 stack_depot_print(alloc_meta->aux_stack[1]);
209 #ifdef CONFIG_KASAN_STACK
210 static bool __must_check tokenize_frame_descr(const char **frame_descr,
211 char *token, size_t max_tok_len,
212 unsigned long *value)
214 const char *sep = strchr(*frame_descr, ' ');
217 sep = *frame_descr + strlen(*frame_descr);
220 const size_t tok_len = sep - *frame_descr;
222 if (tok_len + 1 > max_tok_len) {
223 pr_err("internal error: frame description too long: %s\n",
228 /* Copy token (+ 1 byte for '\0'). */
229 strscpy(token, *frame_descr, tok_len + 1);
232 /* Advance frame_descr past separator. */
233 *frame_descr = sep + 1;
235 if (value != NULL && kstrtoul(token, 10, value)) {
236 pr_err("internal error: not a valid number: %s\n", token);
243 static void print_decoded_frame_descr(const char *frame_descr)
246 * We need to parse the following string:
247 * "n alloc_1 alloc_2 ... alloc_n"
248 * where alloc_i looks like
249 * "offset size len name"
250 * or "offset size len name:line".
254 unsigned long num_objects;
256 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
261 pr_err("This frame has %lu %s:\n", num_objects,
262 num_objects == 1 ? "object" : "objects");
264 while (num_objects--) {
265 unsigned long offset;
269 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
273 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
276 /* name length (unused) */
277 if (!tokenize_frame_descr(&frame_descr, NULL, 0, NULL))
280 if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
284 /* Strip line number; without filename it's not very helpful. */
285 strreplace(token, ':', '\0');
287 /* Finally, print object information. */
288 pr_err(" [%lu, %lu) '%s'", offset, offset + size, token);
292 /* Returns true only if the address is on the current task's stack. */
293 static bool __must_check get_address_stack_frame_info(const void *addr,
294 unsigned long *offset,
295 const char **frame_descr,
296 const void **frame_pc)
298 unsigned long aligned_addr;
299 unsigned long mem_ptr;
300 const u8 *shadow_bottom;
301 const u8 *shadow_ptr;
302 const unsigned long *frame;
304 BUILD_BUG_ON(IS_ENABLED(CONFIG_STACK_GROWSUP));
306 aligned_addr = round_down((unsigned long)addr, sizeof(long));
307 mem_ptr = round_down(aligned_addr, KASAN_GRANULE_SIZE);
308 shadow_ptr = kasan_mem_to_shadow((void *)aligned_addr);
309 shadow_bottom = kasan_mem_to_shadow(end_of_stack(current));
311 while (shadow_ptr >= shadow_bottom && *shadow_ptr != KASAN_STACK_LEFT) {
313 mem_ptr -= KASAN_GRANULE_SIZE;
316 while (shadow_ptr >= shadow_bottom && *shadow_ptr == KASAN_STACK_LEFT) {
318 mem_ptr -= KASAN_GRANULE_SIZE;
321 if (shadow_ptr < shadow_bottom)
324 frame = (const unsigned long *)(mem_ptr + KASAN_GRANULE_SIZE);
325 if (frame[0] != KASAN_CURRENT_STACK_FRAME_MAGIC) {
326 pr_err("internal error: frame has invalid marker: %lu\n",
331 *offset = (unsigned long)addr - (unsigned long)frame;
332 *frame_descr = (const char *)frame[1];
333 *frame_pc = (void *)frame[2];
338 void kasan_print_address_stack_frame(const void *addr)
340 unsigned long offset;
341 const char *frame_descr;
342 const void *frame_pc;
344 if (WARN_ON(!object_is_on_stack(addr)))
347 pr_err("The buggy address belongs to stack of task %s/%d\n",
348 current->comm, task_pid_nr(current));
350 if (!get_address_stack_frame_info(addr, &offset, &frame_descr,
354 pr_err(" and is located at offset %lu in frame:\n", offset);
355 pr_err(" %pS\n", frame_pc);
360 print_decoded_frame_descr(frame_descr);
362 #endif /* CONFIG_KASAN_STACK */
364 #define DEFINE_ASAN_REPORT_LOAD(size) \
365 void __asan_report_load##size##_noabort(void *addr) \
367 kasan_report(addr, size, false, _RET_IP_); \
369 EXPORT_SYMBOL(__asan_report_load##size##_noabort)
371 #define DEFINE_ASAN_REPORT_STORE(size) \
372 void __asan_report_store##size##_noabort(void *addr) \
374 kasan_report(addr, size, true, _RET_IP_); \
376 EXPORT_SYMBOL(__asan_report_store##size##_noabort)
378 DEFINE_ASAN_REPORT_LOAD(1);
379 DEFINE_ASAN_REPORT_LOAD(2);
380 DEFINE_ASAN_REPORT_LOAD(4);
381 DEFINE_ASAN_REPORT_LOAD(8);
382 DEFINE_ASAN_REPORT_LOAD(16);
383 DEFINE_ASAN_REPORT_STORE(1);
384 DEFINE_ASAN_REPORT_STORE(2);
385 DEFINE_ASAN_REPORT_STORE(4);
386 DEFINE_ASAN_REPORT_STORE(8);
387 DEFINE_ASAN_REPORT_STORE(16);
389 void __asan_report_load_n_noabort(void *addr, ssize_t size)
391 kasan_report(addr, size, false, _RET_IP_);
393 EXPORT_SYMBOL(__asan_report_load_n_noabort);
395 void __asan_report_store_n_noabort(void *addr, ssize_t size)
397 kasan_report(addr, size, true, _RET_IP_);
399 EXPORT_SYMBOL(__asan_report_store_n_noabort);