1 // SPDX-License-Identifier: GPL-2.0
3 * This file contains common KASAN 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 <kunit/test.h>
13 #include <linux/bitops.h>
14 #include <linux/ftrace.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/lockdep.h>
19 #include <linux/printk.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/stackdepot.h>
23 #include <linux/stacktrace.h>
24 #include <linux/string.h>
25 #include <linux/types.h>
26 #include <linux/kasan.h>
27 #include <linux/module.h>
28 #include <linux/sched/task_stack.h>
29 #include <linux/uaccess.h>
30 #include <trace/events/error_report.h>
32 #include <asm/sections.h>
37 static unsigned long kasan_flags;
39 #define KASAN_BIT_REPORTED 0
40 #define KASAN_BIT_MULTI_SHOT 1
42 enum kasan_arg_fault {
43 KASAN_ARG_FAULT_DEFAULT,
44 KASAN_ARG_FAULT_REPORT,
45 KASAN_ARG_FAULT_PANIC,
46 KASAN_ARG_FAULT_PANIC_ON_WRITE,
49 static enum kasan_arg_fault kasan_arg_fault __ro_after_init = KASAN_ARG_FAULT_DEFAULT;
51 /* kasan.fault=report/panic */
52 static int __init early_kasan_fault(char *arg)
57 if (!strcmp(arg, "report"))
58 kasan_arg_fault = KASAN_ARG_FAULT_REPORT;
59 else if (!strcmp(arg, "panic"))
60 kasan_arg_fault = KASAN_ARG_FAULT_PANIC;
61 else if (!strcmp(arg, "panic_on_write"))
62 kasan_arg_fault = KASAN_ARG_FAULT_PANIC_ON_WRITE;
68 early_param("kasan.fault", early_kasan_fault);
70 static int __init kasan_set_multi_shot(char *str)
72 set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
75 __setup("kasan_multi_shot", kasan_set_multi_shot);
78 * This function is used to check whether KASAN reports are suppressed for
79 * software KASAN modes via kasan_disable/enable_current() critical sections.
81 * This is done to avoid:
82 * 1. False-positive reports when accessing slab metadata,
83 * 2. Deadlocking when poisoned memory is accessed by the reporting code.
85 * Hardware Tag-Based KASAN instead relies on:
86 * For #1: Resetting tags via kasan_reset_tag().
87 * For #2: Suppression of tag checks via CPU, see report_suppress_start/end().
89 static bool report_suppressed_sw(void)
91 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
92 if (current->kasan_depth)
98 static void report_suppress_start(void)
100 #ifdef CONFIG_KASAN_HW_TAGS
102 * Disable preemption for the duration of printing a KASAN report, as
103 * hw_suppress_tag_checks_start() disables checks on the current CPU.
106 hw_suppress_tag_checks_start();
108 kasan_disable_current();
112 static void report_suppress_stop(void)
114 #ifdef CONFIG_KASAN_HW_TAGS
115 hw_suppress_tag_checks_stop();
118 kasan_enable_current();
123 * Used to avoid reporting more than one KASAN bug unless kasan_multi_shot
124 * is enabled. Note that KASAN tests effectively enable kasan_multi_shot
125 * for their duration.
127 static bool report_enabled(void)
129 if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
131 return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
134 #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) || IS_ENABLED(CONFIG_KASAN_MODULE_TEST)
136 bool kasan_save_enable_multi_shot(void)
138 return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
140 EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
142 void kasan_restore_multi_shot(bool enabled)
145 clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
147 EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
151 #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST)
154 * Whether the KASAN KUnit test suite is currently being executed.
155 * Updated in kasan_test.c.
157 static bool kasan_kunit_executing;
159 void kasan_kunit_test_suite_start(void)
161 WRITE_ONCE(kasan_kunit_executing, true);
163 EXPORT_SYMBOL_GPL(kasan_kunit_test_suite_start);
165 void kasan_kunit_test_suite_end(void)
167 WRITE_ONCE(kasan_kunit_executing, false);
169 EXPORT_SYMBOL_GPL(kasan_kunit_test_suite_end);
171 static bool kasan_kunit_test_suite_executing(void)
173 return READ_ONCE(kasan_kunit_executing);
176 #else /* CONFIG_KASAN_KUNIT_TEST */
178 static inline bool kasan_kunit_test_suite_executing(void) { return false; }
180 #endif /* CONFIG_KASAN_KUNIT_TEST */
182 #if IS_ENABLED(CONFIG_KUNIT)
184 static void fail_non_kasan_kunit_test(void)
188 if (kasan_kunit_test_suite_executing())
191 test = current->kunit_test;
193 kunit_set_failure(test);
196 #else /* CONFIG_KUNIT */
198 static inline void fail_non_kasan_kunit_test(void) { }
200 #endif /* CONFIG_KUNIT */
202 static DEFINE_SPINLOCK(report_lock);
204 static void start_report(unsigned long *flags, bool sync)
206 fail_non_kasan_kunit_test();
207 /* Respect the /proc/sys/kernel/traceoff_on_warning interface. */
208 disable_trace_on_warning();
209 /* Do not allow LOCKDEP mangling KASAN reports. */
211 /* Make sure we don't end up in loop. */
212 report_suppress_start();
213 spin_lock_irqsave(&report_lock, *flags);
214 pr_err("==================================================================\n");
217 static void end_report(unsigned long *flags, const void *addr, bool is_write)
220 trace_error_report_end(ERROR_DETECTOR_KASAN,
221 (unsigned long)addr);
222 pr_err("==================================================================\n");
223 spin_unlock_irqrestore(&report_lock, *flags);
224 if (!test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
225 check_panic_on_warn("KASAN");
226 switch (kasan_arg_fault) {
227 case KASAN_ARG_FAULT_DEFAULT:
228 case KASAN_ARG_FAULT_REPORT:
230 case KASAN_ARG_FAULT_PANIC:
231 panic("kasan.fault=panic set ...\n");
233 case KASAN_ARG_FAULT_PANIC_ON_WRITE:
235 panic("kasan.fault=panic_on_write set ...\n");
238 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
240 report_suppress_stop();
243 static void print_error_description(struct kasan_report_info *info)
245 pr_err("BUG: KASAN: %s in %pS\n", info->bug_type, (void *)info->ip);
247 if (info->type != KASAN_REPORT_ACCESS) {
248 pr_err("Free of addr %px by task %s/%d\n",
249 info->access_addr, current->comm, task_pid_nr(current));
253 if (info->access_size)
254 pr_err("%s of size %zu at addr %px by task %s/%d\n",
255 info->is_write ? "Write" : "Read", info->access_size,
256 info->access_addr, current->comm, task_pid_nr(current));
258 pr_err("%s at addr %px by task %s/%d\n",
259 info->is_write ? "Write" : "Read",
260 info->access_addr, current->comm, task_pid_nr(current));
263 static void print_track(struct kasan_track *track, const char *prefix)
265 pr_err("%s by task %u:\n", prefix, track->pid);
267 stack_depot_print(track->stack);
269 pr_err("(stack is not available)\n");
272 static inline struct page *addr_to_page(const void *addr)
274 if (virt_addr_valid(addr))
275 return virt_to_head_page(addr);
279 static void describe_object_addr(const void *addr, struct kasan_report_info *info)
281 unsigned long access_addr = (unsigned long)addr;
282 unsigned long object_addr = (unsigned long)info->object;
283 const char *rel_type, *region_state = "";
286 pr_err("The buggy address belongs to the object at %px\n"
287 " which belongs to the cache %s of size %d\n",
288 info->object, info->cache->name, info->cache->object_size);
290 if (access_addr < object_addr) {
291 rel_type = "to the left";
292 rel_bytes = object_addr - access_addr;
293 } else if (access_addr >= object_addr + info->alloc_size) {
294 rel_type = "to the right";
295 rel_bytes = access_addr - (object_addr + info->alloc_size);
298 rel_bytes = access_addr - object_addr;
302 * Tag-Based modes use the stack ring to infer the bug type, but the
303 * memory region state description is generated based on the metadata.
304 * Thus, defining the region state as below can contradict the metadata.
305 * Fixing this requires further improvements, so only infer the state
306 * for the Generic mode.
308 if (IS_ENABLED(CONFIG_KASAN_GENERIC)) {
309 if (strcmp(info->bug_type, "slab-out-of-bounds") == 0)
310 region_state = "allocated ";
311 else if (strcmp(info->bug_type, "slab-use-after-free") == 0)
312 region_state = "freed ";
315 pr_err("The buggy address is located %d bytes %s of\n"
316 " %s%zu-byte region [%px, %px)\n",
317 rel_bytes, rel_type, region_state, info->alloc_size,
318 (void *)object_addr, (void *)(object_addr + info->alloc_size));
321 static void describe_object_stacks(struct kasan_report_info *info)
323 if (info->alloc_track.stack) {
324 print_track(&info->alloc_track, "Allocated");
328 if (info->free_track.stack) {
329 print_track(&info->free_track, "Freed");
333 kasan_print_aux_stacks(info->cache, info->object);
336 static void describe_object(const void *addr, struct kasan_report_info *info)
338 if (kasan_stack_collection_enabled())
339 describe_object_stacks(info);
340 describe_object_addr(addr, info);
343 static inline bool kernel_or_module_addr(const void *addr)
345 if (is_kernel((unsigned long)addr))
347 if (is_module_address((unsigned long)addr))
352 static inline bool init_task_stack_addr(const void *addr)
354 return addr >= (void *)&init_thread_union.stack &&
355 (addr <= (void *)&init_thread_union.stack +
356 sizeof(init_thread_union.stack));
359 static void print_address_description(void *addr, u8 tag,
360 struct kasan_report_info *info)
362 struct page *page = addr_to_page(addr);
364 dump_stack_lvl(KERN_ERR);
367 if (info->cache && info->object) {
368 describe_object(addr, info);
372 if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
373 pr_err("The buggy address belongs to the variable:\n");
374 pr_err(" %pS\n", addr);
378 if (object_is_on_stack(addr)) {
380 * Currently, KASAN supports printing frame information only
381 * for accesses to the task's own stack.
383 kasan_print_address_stack_frame(addr);
387 if (is_vmalloc_addr(addr)) {
388 struct vm_struct *va = find_vm_area(addr);
391 pr_err("The buggy address belongs to the virtual mapping at\n"
392 " [%px, %px) created by:\n"
394 va->addr, va->addr + va->size, va->caller);
397 page = vmalloc_to_page(addr);
402 pr_err("The buggy address belongs to the physical page:\n");
403 dump_page(page, "kasan: bad access detected");
408 static bool meta_row_is_guilty(const void *row, const void *addr)
410 return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW);
413 static int meta_pointer_offset(const void *row, const void *addr)
416 * Memory state around the buggy address:
417 * ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe
420 * The length of ">ff00ff00ff00ff00: " is
421 * 3 + (BITS_PER_LONG / 8) * 2 chars.
422 * The length of each granule metadata is 2 bytes
423 * plus 1 byte for space.
425 return 3 + (BITS_PER_LONG / 8) * 2 +
426 (addr - row) / KASAN_GRANULE_SIZE * 3 + 1;
429 static void print_memory_metadata(const void *addr)
434 row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW)
435 - META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW;
437 pr_err("Memory state around the buggy address:\n");
439 for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
440 char buffer[4 + (BITS_PER_LONG / 8) * 2];
441 char metadata[META_BYTES_PER_ROW];
443 snprintf(buffer, sizeof(buffer),
444 (i == 0) ? ">%px: " : " %px: ", row);
447 * We should not pass a shadow pointer to generic
448 * function, because generic functions may try to
449 * access kasan mapping for the passed address.
451 kasan_metadata_fetch_row(&metadata[0], row);
453 print_hex_dump(KERN_ERR, buffer,
454 DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
455 metadata, META_BYTES_PER_ROW, 0);
457 if (meta_row_is_guilty(row, addr))
458 pr_err("%*c\n", meta_pointer_offset(row, addr), '^');
460 row += META_MEM_BYTES_PER_ROW;
464 static void print_report(struct kasan_report_info *info)
466 void *addr = kasan_reset_tag((void *)info->access_addr);
467 u8 tag = get_tag((void *)info->access_addr);
469 print_error_description(info);
470 if (addr_has_metadata(addr))
471 kasan_print_tags(tag, info->first_bad_addr);
474 if (addr_has_metadata(addr)) {
475 print_address_description(addr, tag, info);
476 print_memory_metadata(info->first_bad_addr);
478 dump_stack_lvl(KERN_ERR);
482 static void complete_report_info(struct kasan_report_info *info)
484 void *addr = kasan_reset_tag((void *)info->access_addr);
487 if (info->type == KASAN_REPORT_ACCESS)
488 info->first_bad_addr = kasan_find_first_bad_addr(
489 (void *)info->access_addr, info->access_size);
491 info->first_bad_addr = addr;
493 slab = kasan_addr_to_slab(addr);
495 info->cache = slab->slab_cache;
496 info->object = nearest_obj(info->cache, slab, addr);
498 /* Try to determine allocation size based on the metadata. */
499 info->alloc_size = kasan_get_alloc_size(info->object, info->cache);
500 /* Fallback to the object size if failed. */
501 if (!info->alloc_size)
502 info->alloc_size = info->cache->object_size;
504 info->cache = info->object = NULL;
506 switch (info->type) {
507 case KASAN_REPORT_INVALID_FREE:
508 info->bug_type = "invalid-free";
510 case KASAN_REPORT_DOUBLE_FREE:
511 info->bug_type = "double-free";
514 /* bug_type filled in by kasan_complete_mode_report_info. */
518 /* Fill in mode-specific report info fields. */
519 kasan_complete_mode_report_info(info);
522 void kasan_report_invalid_free(void *ptr, unsigned long ip, enum kasan_report_type type)
525 struct kasan_report_info info;
528 * Do not check report_suppressed_sw(), as an invalid-free cannot be
529 * caused by accessing poisoned memory and thus should not be suppressed
530 * by kasan_disable/enable_current() critical sections.
532 * Note that for Hardware Tag-Based KASAN, kasan_report_invalid_free()
533 * is triggered by explicit tag checks and not by the ones performed by
534 * the CPU. Thus, reporting invalid-free is not suppressed as well.
536 if (unlikely(!report_enabled()))
539 start_report(&flags, true);
541 memset(&info, 0, sizeof(info));
543 info.access_addr = ptr;
544 info.access_size = 0;
545 info.is_write = false;
548 complete_report_info(&info);
553 * Invalid free is considered a "write" since the allocator's metadata
554 * updates involves writes.
556 end_report(&flags, ptr, true);
560 * kasan_report() is the only reporting function that uses
561 * user_access_save/restore(): kasan_report_invalid_free() cannot be called
562 * from a UACCESS region, and kasan_report_async() is not used on x86.
564 bool kasan_report(const void *addr, size_t size, bool is_write,
568 unsigned long ua_flags = user_access_save();
569 unsigned long irq_flags;
570 struct kasan_report_info info;
572 if (unlikely(report_suppressed_sw()) || unlikely(!report_enabled())) {
577 start_report(&irq_flags, true);
579 memset(&info, 0, sizeof(info));
580 info.type = KASAN_REPORT_ACCESS;
581 info.access_addr = addr;
582 info.access_size = size;
583 info.is_write = is_write;
586 complete_report_info(&info);
590 end_report(&irq_flags, (void *)addr, is_write);
593 user_access_restore(ua_flags);
598 #ifdef CONFIG_KASAN_HW_TAGS
599 void kasan_report_async(void)
604 * Do not check report_suppressed_sw(), as
605 * kasan_disable/enable_current() critical sections do not affect
606 * Hardware Tag-Based KASAN.
608 if (unlikely(!report_enabled()))
611 start_report(&flags, false);
612 pr_err("BUG: KASAN: invalid-access\n");
613 pr_err("Asynchronous fault: no details available\n");
615 dump_stack_lvl(KERN_ERR);
617 * Conservatively set is_write=true, because no details are available.
618 * In this mode, kasan.fault=panic_on_write is like kasan.fault=panic.
620 end_report(&flags, NULL, true);
622 #endif /* CONFIG_KASAN_HW_TAGS */
624 #ifdef CONFIG_KASAN_INLINE
626 * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
627 * canonical half of the address space) cause out-of-bounds shadow memory reads
628 * before the actual access. For addresses in the low canonical half of the
629 * address space, as well as most non-canonical addresses, that out-of-bounds
630 * shadow memory access lands in the non-canonical part of the address space.
631 * Help the user figure out what the original bogus pointer was.
633 void kasan_non_canonical_hook(unsigned long addr)
635 unsigned long orig_addr;
636 const char *bug_type;
638 if (addr < KASAN_SHADOW_OFFSET)
641 orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
643 * For faults near the shadow address for NULL, we can be fairly certain
644 * that this is a KASAN shadow memory access.
645 * For faults that correspond to shadow for low canonical addresses, we
646 * can still be pretty sure - that shadow region is a fairly narrow
647 * chunk of the non-canonical address space.
648 * But faults that look like shadow for non-canonical addresses are a
649 * really large chunk of the address space. In that case, we still
650 * print the decoded address, but make it clear that this is not
651 * necessarily what's actually going on.
653 if (orig_addr < PAGE_SIZE)
654 bug_type = "null-ptr-deref";
655 else if (orig_addr < TASK_SIZE)
656 bug_type = "probably user-memory-access";
658 bug_type = "maybe wild-memory-access";
659 pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
660 orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1);