1e886bf9dSAndrey Konovalov // SPDX-License-Identifier: GPL-2.0
20b24beccSAndrey Ryabinin /*
359fd51b2SAndrey Konovalov * This file contains common KASAN error reporting code.
40b24beccSAndrey Ryabinin *
50b24beccSAndrey Ryabinin * Copyright (c) 2014 Samsung Electronics Co., Ltd.
62baf9e89SAndrey Ryabinin * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
70b24beccSAndrey Ryabinin *
85d0926efSAndrey Konovalov * Some code borrowed from https://github.com/xairy/kasan-prototype by
95f21f3a8SAndrey Konovalov * Andrey Konovalov <andreyknvl@gmail.com>
100b24beccSAndrey Ryabinin */
110b24beccSAndrey Ryabinin
12c8c7016fSAndrey Konovalov #include <kunit/test.h>
13b0845ce5SMark Rutland #include <linux/bitops.h>
144f40c6e5SPeter Zijlstra #include <linux/ftrace.h>
15b0845ce5SMark Rutland #include <linux/init.h>
160b24beccSAndrey Ryabinin #include <linux/kernel.h>
17c32caa26SAndrey Konovalov #include <linux/lockdep.h>
180b24beccSAndrey Ryabinin #include <linux/mm.h>
190b24beccSAndrey Ryabinin #include <linux/printk.h>
200b24beccSAndrey Ryabinin #include <linux/sched.h>
210b24beccSAndrey Ryabinin #include <linux/slab.h>
22cd11016eSAlexander Potapenko #include <linux/stackdepot.h>
230b24beccSAndrey Ryabinin #include <linux/stacktrace.h>
240b24beccSAndrey Ryabinin #include <linux/string.h>
250b24beccSAndrey Ryabinin #include <linux/types.h>
260b24beccSAndrey Ryabinin #include <linux/kasan.h>
27527f215bSAneesh Kumar K.V #include <linux/module.h>
28e8969219SMarco Elver #include <linux/sched/task_stack.h>
294fba3758SAndrey Konovalov #include <linux/uaccess.h>
30d3a61f74SAlexander Potapenko #include <trace/events/error_report.h>
310b24beccSAndrey Ryabinin
32bebf56a1SAndrey Ryabinin #include <asm/sections.h>
33bebf56a1SAndrey Ryabinin
340b24beccSAndrey Ryabinin #include "kasan.h"
350316bec2SAndrey Ryabinin #include "../slab.h"
360b24beccSAndrey Ryabinin
3711cd3cd6SAndrey Konovalov static unsigned long kasan_flags;
3811cd3cd6SAndrey Konovalov
3911cd3cd6SAndrey Konovalov #define KASAN_BIT_REPORTED 0
4011cd3cd6SAndrey Konovalov #define KASAN_BIT_MULTI_SHOT 1
4111cd3cd6SAndrey Konovalov
42c9d1af2bSWoody Lin enum kasan_arg_fault {
43c9d1af2bSWoody Lin KASAN_ARG_FAULT_DEFAULT,
44c9d1af2bSWoody Lin KASAN_ARG_FAULT_REPORT,
45c9d1af2bSWoody Lin KASAN_ARG_FAULT_PANIC,
46452c03fdSMarco Elver KASAN_ARG_FAULT_PANIC_ON_WRITE,
47c9d1af2bSWoody Lin };
48c9d1af2bSWoody Lin
49c9d1af2bSWoody Lin static enum kasan_arg_fault kasan_arg_fault __ro_after_init = KASAN_ARG_FAULT_DEFAULT;
50c9d1af2bSWoody Lin
51c9d1af2bSWoody Lin /* kasan.fault=report/panic */
early_kasan_fault(char * arg)52c9d1af2bSWoody Lin static int __init early_kasan_fault(char *arg)
53c9d1af2bSWoody Lin {
54c9d1af2bSWoody Lin if (!arg)
55c9d1af2bSWoody Lin return -EINVAL;
56c9d1af2bSWoody Lin
57c9d1af2bSWoody Lin if (!strcmp(arg, "report"))
58c9d1af2bSWoody Lin kasan_arg_fault = KASAN_ARG_FAULT_REPORT;
59c9d1af2bSWoody Lin else if (!strcmp(arg, "panic"))
60c9d1af2bSWoody Lin kasan_arg_fault = KASAN_ARG_FAULT_PANIC;
61452c03fdSMarco Elver else if (!strcmp(arg, "panic_on_write"))
62452c03fdSMarco Elver kasan_arg_fault = KASAN_ARG_FAULT_PANIC_ON_WRITE;
63c9d1af2bSWoody Lin else
64c9d1af2bSWoody Lin return -EINVAL;
65c9d1af2bSWoody Lin
66c9d1af2bSWoody Lin return 0;
67c9d1af2bSWoody Lin }
68c9d1af2bSWoody Lin early_param("kasan.fault", early_kasan_fault);
69c9d1af2bSWoody Lin
kasan_set_multi_shot(char * str)7011cd3cd6SAndrey Konovalov static int __init kasan_set_multi_shot(char *str)
710b24beccSAndrey Ryabinin {
7211cd3cd6SAndrey Konovalov set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
7311cd3cd6SAndrey Konovalov return 1;
740b24beccSAndrey Ryabinin }
7511cd3cd6SAndrey Konovalov __setup("kasan_multi_shot", kasan_set_multi_shot);
760b24beccSAndrey Ryabinin
77865bfa28SAndrey Konovalov /*
78c6a690e0SAndrey Konovalov * This function is used to check whether KASAN reports are suppressed for
79c6a690e0SAndrey Konovalov * software KASAN modes via kasan_disable/enable_current() critical sections.
80c6a690e0SAndrey Konovalov *
81c6a690e0SAndrey Konovalov * This is done to avoid:
82c6a690e0SAndrey Konovalov * 1. False-positive reports when accessing slab metadata,
83c6a690e0SAndrey Konovalov * 2. Deadlocking when poisoned memory is accessed by the reporting code.
84c6a690e0SAndrey Konovalov *
85c6a690e0SAndrey Konovalov * Hardware Tag-Based KASAN instead relies on:
86c6a690e0SAndrey Konovalov * For #1: Resetting tags via kasan_reset_tag().
87c6a690e0SAndrey Konovalov * For #2: Suppression of tag checks via CPU, see report_suppress_start/end().
88865bfa28SAndrey Konovalov */
report_suppressed_sw(void)89c6a690e0SAndrey Konovalov static bool report_suppressed_sw(void)
905e82cd12SAndrey Konovalov {
91865bfa28SAndrey Konovalov #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
92865bfa28SAndrey Konovalov if (current->kasan_depth)
93865bfa28SAndrey Konovalov return true;
94865bfa28SAndrey Konovalov #endif
95865bfa28SAndrey Konovalov return false;
9631c65110SAndrey Konovalov }
9731c65110SAndrey Konovalov
report_suppress_start(void)98c6a690e0SAndrey Konovalov static void report_suppress_start(void)
99c6a690e0SAndrey Konovalov {
100c6a690e0SAndrey Konovalov #ifdef CONFIG_KASAN_HW_TAGS
101c6a690e0SAndrey Konovalov /*
102c6a690e0SAndrey Konovalov * Disable preemption for the duration of printing a KASAN report, as
103c6a690e0SAndrey Konovalov * hw_suppress_tag_checks_start() disables checks on the current CPU.
104c6a690e0SAndrey Konovalov */
105c6a690e0SAndrey Konovalov preempt_disable();
106c6a690e0SAndrey Konovalov hw_suppress_tag_checks_start();
107c6a690e0SAndrey Konovalov #else
108c6a690e0SAndrey Konovalov kasan_disable_current();
109c6a690e0SAndrey Konovalov #endif
110c6a690e0SAndrey Konovalov }
111c6a690e0SAndrey Konovalov
report_suppress_stop(void)112c6a690e0SAndrey Konovalov static void report_suppress_stop(void)
113c6a690e0SAndrey Konovalov {
114c6a690e0SAndrey Konovalov #ifdef CONFIG_KASAN_HW_TAGS
115c6a690e0SAndrey Konovalov hw_suppress_tag_checks_stop();
116c6a690e0SAndrey Konovalov preempt_enable();
117c6a690e0SAndrey Konovalov #else
118c6a690e0SAndrey Konovalov kasan_enable_current();
119c6a690e0SAndrey Konovalov #endif
120c6a690e0SAndrey Konovalov }
121c6a690e0SAndrey Konovalov
122865bfa28SAndrey Konovalov /*
123865bfa28SAndrey Konovalov * Used to avoid reporting more than one KASAN bug unless kasan_multi_shot
124865bfa28SAndrey Konovalov * is enabled. Note that KASAN tests effectively enable kasan_multi_shot
125865bfa28SAndrey Konovalov * for their duration.
126865bfa28SAndrey Konovalov */
report_enabled(void)127865bfa28SAndrey Konovalov static bool report_enabled(void)
128865bfa28SAndrey Konovalov {
129865bfa28SAndrey Konovalov if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
130865bfa28SAndrey Konovalov return true;
131865bfa28SAndrey Konovalov return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
1320b24beccSAndrey Ryabinin }
1330b24beccSAndrey Ryabinin
13480207910SAndrey Konovalov #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) || IS_ENABLED(CONFIG_KASAN_MODULE_TEST)
13580207910SAndrey Konovalov
kasan_save_enable_multi_shot(void)13680207910SAndrey Konovalov bool kasan_save_enable_multi_shot(void)
13780207910SAndrey Konovalov {
13880207910SAndrey Konovalov return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
13980207910SAndrey Konovalov }
14080207910SAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
14180207910SAndrey Konovalov
kasan_restore_multi_shot(bool enabled)14280207910SAndrey Konovalov void kasan_restore_multi_shot(bool enabled)
14380207910SAndrey Konovalov {
14480207910SAndrey Konovalov if (!enabled)
14580207910SAndrey Konovalov clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
14680207910SAndrey Konovalov }
14780207910SAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
14880207910SAndrey Konovalov
14980207910SAndrey Konovalov #endif
15080207910SAndrey Konovalov
151c8c7016fSAndrey Konovalov #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST)
152c8c7016fSAndrey Konovalov
153c8c7016fSAndrey Konovalov /*
154c8c7016fSAndrey Konovalov * Whether the KASAN KUnit test suite is currently being executed.
155c8c7016fSAndrey Konovalov * Updated in kasan_test.c.
156c8c7016fSAndrey Konovalov */
1570de4a7f5SAndrey Konovalov static bool kasan_kunit_executing;
158c8c7016fSAndrey Konovalov
kasan_kunit_test_suite_start(void)159c8c7016fSAndrey Konovalov void kasan_kunit_test_suite_start(void)
160c8c7016fSAndrey Konovalov {
161c8c7016fSAndrey Konovalov WRITE_ONCE(kasan_kunit_executing, true);
162c8c7016fSAndrey Konovalov }
163c8c7016fSAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_kunit_test_suite_start);
164c8c7016fSAndrey Konovalov
kasan_kunit_test_suite_end(void)165c8c7016fSAndrey Konovalov void kasan_kunit_test_suite_end(void)
166c8c7016fSAndrey Konovalov {
167c8c7016fSAndrey Konovalov WRITE_ONCE(kasan_kunit_executing, false);
168c8c7016fSAndrey Konovalov }
169c8c7016fSAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_kunit_test_suite_end);
170c8c7016fSAndrey Konovalov
kasan_kunit_test_suite_executing(void)171c8c7016fSAndrey Konovalov static bool kasan_kunit_test_suite_executing(void)
172c8c7016fSAndrey Konovalov {
173c8c7016fSAndrey Konovalov return READ_ONCE(kasan_kunit_executing);
174c8c7016fSAndrey Konovalov }
175c8c7016fSAndrey Konovalov
176c8c7016fSAndrey Konovalov #else /* CONFIG_KASAN_KUNIT_TEST */
177c8c7016fSAndrey Konovalov
kasan_kunit_test_suite_executing(void)178c8c7016fSAndrey Konovalov static inline bool kasan_kunit_test_suite_executing(void) { return false; }
179c8c7016fSAndrey Konovalov
180c8c7016fSAndrey Konovalov #endif /* CONFIG_KASAN_KUNIT_TEST */
181c8c7016fSAndrey Konovalov
182c8c7016fSAndrey Konovalov #if IS_ENABLED(CONFIG_KUNIT)
183c8c7016fSAndrey Konovalov
fail_non_kasan_kunit_test(void)184c8c7016fSAndrey Konovalov static void fail_non_kasan_kunit_test(void)
185c8c7016fSAndrey Konovalov {
186c8c7016fSAndrey Konovalov struct kunit *test;
187c8c7016fSAndrey Konovalov
188c8c7016fSAndrey Konovalov if (kasan_kunit_test_suite_executing())
189c8c7016fSAndrey Konovalov return;
190c8c7016fSAndrey Konovalov
191c8c7016fSAndrey Konovalov test = current->kunit_test;
192c8c7016fSAndrey Konovalov if (test)
193c8c7016fSAndrey Konovalov kunit_set_failure(test);
194c8c7016fSAndrey Konovalov }
195c8c7016fSAndrey Konovalov
196c8c7016fSAndrey Konovalov #else /* CONFIG_KUNIT */
197c8c7016fSAndrey Konovalov
fail_non_kasan_kunit_test(void)198c8c7016fSAndrey Konovalov static inline void fail_non_kasan_kunit_test(void) { }
199c8c7016fSAndrey Konovalov
200c8c7016fSAndrey Konovalov #endif /* CONFIG_KUNIT */
201c8c7016fSAndrey Konovalov
202*cb9945f2SJared Kangas static DEFINE_RAW_SPINLOCK(report_lock);
2037e088978SAndrey Ryabinin
start_report(unsigned long * flags,bool sync)204a260d281SAndrey Konovalov static void start_report(unsigned long *flags, bool sync)
2057e088978SAndrey Ryabinin {
206c8c7016fSAndrey Konovalov fail_non_kasan_kunit_test();
2070a6e8a07SAndrey Konovalov /* Respect the /proc/sys/kernel/traceoff_on_warning interface. */
2080a6e8a07SAndrey Konovalov disable_trace_on_warning();
209c32caa26SAndrey Konovalov /* Do not allow LOCKDEP mangling KASAN reports. */
210c32caa26SAndrey Konovalov lockdep_off();
211a260d281SAndrey Konovalov /* Make sure we don't end up in loop. */
212c6a690e0SAndrey Konovalov report_suppress_start();
213*cb9945f2SJared Kangas raw_spin_lock_irqsave(&report_lock, *flags);
2147e088978SAndrey Ryabinin pr_err("==================================================================\n");
2157e088978SAndrey Ryabinin }
2167e088978SAndrey Ryabinin
end_report(unsigned long * flags,const void * addr,bool is_write)217452c03fdSMarco Elver static void end_report(unsigned long *flags, const void *addr, bool is_write)
2187e088978SAndrey Ryabinin {
219476b1dc2SAndrey Konovalov if (addr)
2209d7b7dd9SAndrey Konovalov trace_error_report_end(ERROR_DETECTOR_KASAN,
2219d7b7dd9SAndrey Konovalov (unsigned long)addr);
2227e088978SAndrey Ryabinin pr_err("==================================================================\n");
223*cb9945f2SJared Kangas raw_spin_unlock_irqrestore(&report_lock, *flags);
22479cc1ba7SKees Cook if (!test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
22579cc1ba7SKees Cook check_panic_on_warn("KASAN");
226452c03fdSMarco Elver switch (kasan_arg_fault) {
227452c03fdSMarco Elver case KASAN_ARG_FAULT_DEFAULT:
228452c03fdSMarco Elver case KASAN_ARG_FAULT_REPORT:
229452c03fdSMarco Elver break;
230452c03fdSMarco Elver case KASAN_ARG_FAULT_PANIC:
2318028caacSAndrey Konovalov panic("kasan.fault=panic set ...\n");
232452c03fdSMarco Elver break;
233452c03fdSMarco Elver case KASAN_ARG_FAULT_PANIC_ON_WRITE:
234452c03fdSMarco Elver if (is_write)
235452c03fdSMarco Elver panic("kasan.fault=panic_on_write set ...\n");
236452c03fdSMarco Elver break;
237452c03fdSMarco Elver }
238c32caa26SAndrey Konovalov add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
239c32caa26SAndrey Konovalov lockdep_on();
240c6a690e0SAndrey Konovalov report_suppress_stop();
2417e088978SAndrey Ryabinin }
2427e088978SAndrey Ryabinin
print_error_description(struct kasan_report_info * info)243865bfa28SAndrey Konovalov static void print_error_description(struct kasan_report_info *info)
244865bfa28SAndrey Konovalov {
24559e6e098SAndrey Konovalov pr_err("BUG: KASAN: %s in %pS\n", info->bug_type, (void *)info->ip);
246dcc57966SAndrey Konovalov
247dcc57966SAndrey Konovalov if (info->type != KASAN_REPORT_ACCESS) {
248dcc57966SAndrey Konovalov pr_err("Free of addr %px by task %s/%d\n",
249dcc57966SAndrey Konovalov info->access_addr, current->comm, task_pid_nr(current));
250dcc57966SAndrey Konovalov return;
251dcc57966SAndrey Konovalov }
252dcc57966SAndrey Konovalov
253865bfa28SAndrey Konovalov if (info->access_size)
254865bfa28SAndrey Konovalov pr_err("%s of size %zu at addr %px by task %s/%d\n",
255865bfa28SAndrey Konovalov info->is_write ? "Write" : "Read", info->access_size,
256865bfa28SAndrey Konovalov info->access_addr, current->comm, task_pid_nr(current));
257865bfa28SAndrey Konovalov else
258865bfa28SAndrey Konovalov pr_err("%s at addr %px by task %s/%d\n",
259865bfa28SAndrey Konovalov info->is_write ? "Write" : "Read",
260865bfa28SAndrey Konovalov info->access_addr, current->comm, task_pid_nr(current));
261865bfa28SAndrey Konovalov }
262865bfa28SAndrey Konovalov
print_track(struct kasan_track * track,const char * prefix)263b6b72f49SAndrey Konovalov static void print_track(struct kasan_track *track, const char *prefix)
2647ed2f9e6SAlexander Potapenko {
265b6b72f49SAndrey Konovalov pr_err("%s by task %u:\n", prefix, track->pid);
2669ef08d26SAndrey Konovalov if (track->stack)
267505be481SImran Khan stack_depot_print(track->stack);
2689ef08d26SAndrey Konovalov else
269cd11016eSAlexander Potapenko pr_err("(stack is not available)\n");
270cd11016eSAlexander Potapenko }
2717ed2f9e6SAlexander Potapenko
addr_to_page(const void * addr)272559756e8SAndrey Konovalov static inline struct page *addr_to_page(const void *addr)
273db429f16SAndrey Konovalov {
2742c9fb1fdSAndrey Konovalov if (virt_addr_valid(addr))
275db429f16SAndrey Konovalov return virt_to_head_page(addr);
276db429f16SAndrey Konovalov return NULL;
277db429f16SAndrey Konovalov }
278db429f16SAndrey Konovalov
describe_object_addr(const void * addr,struct kasan_report_info * info)2798f17febbSKuan-Ying Lee static void describe_object_addr(const void *addr, struct kasan_report_info *info)
2800c06f1f8SAndrey Konovalov {
2810c06f1f8SAndrey Konovalov unsigned long access_addr = (unsigned long)addr;
2828f17febbSKuan-Ying Lee unsigned long object_addr = (unsigned long)info->object;
2838f17febbSKuan-Ying Lee const char *rel_type, *region_state = "";
2840c06f1f8SAndrey Konovalov int rel_bytes;
2850c06f1f8SAndrey Konovalov
2866424f6bbSTobin C. Harding pr_err("The buggy address belongs to the object at %px\n"
2870c06f1f8SAndrey Konovalov " which belongs to the cache %s of size %d\n",
2888f17febbSKuan-Ying Lee info->object, info->cache->name, info->cache->object_size);
2890c06f1f8SAndrey Konovalov
2900c06f1f8SAndrey Konovalov if (access_addr < object_addr) {
2910c06f1f8SAndrey Konovalov rel_type = "to the left";
2920c06f1f8SAndrey Konovalov rel_bytes = object_addr - access_addr;
2938f17febbSKuan-Ying Lee } else if (access_addr >= object_addr + info->alloc_size) {
2940c06f1f8SAndrey Konovalov rel_type = "to the right";
2958f17febbSKuan-Ying Lee rel_bytes = access_addr - (object_addr + info->alloc_size);
2960c06f1f8SAndrey Konovalov } else {
2970c06f1f8SAndrey Konovalov rel_type = "inside";
2980c06f1f8SAndrey Konovalov rel_bytes = access_addr - object_addr;
2990c06f1f8SAndrey Konovalov }
3000c06f1f8SAndrey Konovalov
3018f17febbSKuan-Ying Lee /*
3028f17febbSKuan-Ying Lee * Tag-Based modes use the stack ring to infer the bug type, but the
3038f17febbSKuan-Ying Lee * memory region state description is generated based on the metadata.
3048f17febbSKuan-Ying Lee * Thus, defining the region state as below can contradict the metadata.
3058f17febbSKuan-Ying Lee * Fixing this requires further improvements, so only infer the state
3068f17febbSKuan-Ying Lee * for the Generic mode.
3078f17febbSKuan-Ying Lee */
3088f17febbSKuan-Ying Lee if (IS_ENABLED(CONFIG_KASAN_GENERIC)) {
3098f17febbSKuan-Ying Lee if (strcmp(info->bug_type, "slab-out-of-bounds") == 0)
3108f17febbSKuan-Ying Lee region_state = "allocated ";
3118f17febbSKuan-Ying Lee else if (strcmp(info->bug_type, "slab-use-after-free") == 0)
3128f17febbSKuan-Ying Lee region_state = "freed ";
3138f17febbSKuan-Ying Lee }
3148f17febbSKuan-Ying Lee
3150c06f1f8SAndrey Konovalov pr_err("The buggy address is located %d bytes %s of\n"
3168f17febbSKuan-Ying Lee " %s%zu-byte region [%px, %px)\n",
3178f17febbSKuan-Ying Lee rel_bytes, rel_type, region_state, info->alloc_size,
3188f17febbSKuan-Ying Lee (void *)object_addr, (void *)(object_addr + info->alloc_size));
3190c06f1f8SAndrey Konovalov }
3200c06f1f8SAndrey Konovalov
describe_object_stacks(struct kasan_report_info * info)32159e6e098SAndrey Konovalov static void describe_object_stacks(struct kasan_report_info *info)
3227ed2f9e6SAlexander Potapenko {
32359e6e098SAndrey Konovalov if (info->alloc_track.stack) {
32459e6e098SAndrey Konovalov print_track(&info->alloc_track, "Allocated");
325b1938599SAndrey Konovalov pr_err("\n");
32697593cadSAndrey Konovalov }
32797593cadSAndrey Konovalov
32859e6e098SAndrey Konovalov if (info->free_track.stack) {
32959e6e098SAndrey Konovalov print_track(&info->free_track, "Freed");
330b1938599SAndrey Konovalov pr_err("\n");
331e4b7818bSWalter Wu }
33226e760c9SWalter Wu
33392a38eacSAndrey Konovalov kasan_print_aux_stacks(info->cache, info->object);
3347ed2f9e6SAlexander Potapenko }
3357ed2f9e6SAlexander Potapenko
describe_object(const void * addr,struct kasan_report_info * info)33659e6e098SAndrey Konovalov static void describe_object(const void *addr, struct kasan_report_info *info)
3378028caacSAndrey Konovalov {
3388028caacSAndrey Konovalov if (kasan_stack_collection_enabled())
33959e6e098SAndrey Konovalov describe_object_stacks(info);
3408f17febbSKuan-Ying Lee describe_object_addr(addr, info);
3410c06f1f8SAndrey Konovalov }
3420c06f1f8SAndrey Konovalov
kernel_or_module_addr(const void * addr)34311cd3cd6SAndrey Konovalov static inline bool kernel_or_module_addr(const void *addr)
34411cd3cd6SAndrey Konovalov {
3453298cbe8SKefeng Wang if (is_kernel((unsigned long)addr))
34611cd3cd6SAndrey Konovalov return true;
34711cd3cd6SAndrey Konovalov if (is_module_address((unsigned long)addr))
34811cd3cd6SAndrey Konovalov return true;
34911cd3cd6SAndrey Konovalov return false;
35011cd3cd6SAndrey Konovalov }
35111cd3cd6SAndrey Konovalov
init_task_stack_addr(const void * addr)35211cd3cd6SAndrey Konovalov static inline bool init_task_stack_addr(const void *addr)
35311cd3cd6SAndrey Konovalov {
35411cd3cd6SAndrey Konovalov return addr >= (void *)&init_thread_union.stack &&
35511cd3cd6SAndrey Konovalov (addr <= (void *)&init_thread_union.stack +
35611cd3cd6SAndrey Konovalov sizeof(init_thread_union.stack));
35711cd3cd6SAndrey Konovalov }
35811cd3cd6SAndrey Konovalov
print_address_description(void * addr,u8 tag,struct kasan_report_info * info)3597fae3dd0SAndrey Konovalov static void print_address_description(void *addr, u8 tag,
3607fae3dd0SAndrey Konovalov struct kasan_report_info *info)
3617e088978SAndrey Ryabinin {
362559756e8SAndrey Konovalov struct page *page = addr_to_page(addr);
363b8c73fc2SAndrey Ryabinin
364336abff6SAlexander Potapenko dump_stack_lvl(KERN_ERR);
365b1938599SAndrey Konovalov pr_err("\n");
366db429f16SAndrey Konovalov
3677fae3dd0SAndrey Konovalov if (info->cache && info->object) {
36859e6e098SAndrey Konovalov describe_object(addr, info);
369038fd2b4SAndrey Konovalov pr_err("\n");
370b8c73fc2SAndrey Ryabinin }
371b8c73fc2SAndrey Ryabinin
372430a05f9SAndrey Konovalov if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
373430a05f9SAndrey Konovalov pr_err("The buggy address belongs to the variable:\n");
374430a05f9SAndrey Konovalov pr_err(" %pS\n", addr);
375038fd2b4SAndrey Konovalov pr_err("\n");
376430a05f9SAndrey Konovalov }
377430a05f9SAndrey Konovalov
3780f9b35f3SAndrey Konovalov if (object_is_on_stack(addr)) {
3790f9b35f3SAndrey Konovalov /*
3800f9b35f3SAndrey Konovalov * Currently, KASAN supports printing frame information only
3810f9b35f3SAndrey Konovalov * for accesses to the task's own stack.
3820f9b35f3SAndrey Konovalov */
3830f9b35f3SAndrey Konovalov kasan_print_address_stack_frame(addr);
3840f9b35f3SAndrey Konovalov pr_err("\n");
3850f9b35f3SAndrey Konovalov }
3860f9b35f3SAndrey Konovalov
387c056a364SAndrey Konovalov if (is_vmalloc_addr(addr)) {
388c056a364SAndrey Konovalov struct vm_struct *va = find_vm_area(addr);
389c056a364SAndrey Konovalov
390c056a364SAndrey Konovalov if (va) {
391c056a364SAndrey Konovalov pr_err("The buggy address belongs to the virtual mapping at\n"
392c056a364SAndrey Konovalov " [%px, %px) created by:\n"
393c056a364SAndrey Konovalov " %pS\n",
394c056a364SAndrey Konovalov va->addr, va->addr + va->size, va->caller);
395038fd2b4SAndrey Konovalov pr_err("\n");
396c056a364SAndrey Konovalov
397fbf4df06SKefeng Wang page = vmalloc_to_page(addr);
398c056a364SAndrey Konovalov }
399c056a364SAndrey Konovalov }
400c056a364SAndrey Konovalov
401430a05f9SAndrey Konovalov if (page) {
402c056a364SAndrey Konovalov pr_err("The buggy address belongs to the physical page:\n");
403430a05f9SAndrey Konovalov dump_page(page, "kasan: bad access detected");
404038fd2b4SAndrey Konovalov pr_err("\n");
405bebf56a1SAndrey Ryabinin }
4060b24beccSAndrey Ryabinin }
4070b24beccSAndrey Ryabinin
meta_row_is_guilty(const void * row,const void * addr)40896e0279dSAndrey Konovalov static bool meta_row_is_guilty(const void *row, const void *addr)
4090b24beccSAndrey Ryabinin {
41096e0279dSAndrey Konovalov return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW);
4110b24beccSAndrey Ryabinin }
4120b24beccSAndrey Ryabinin
meta_pointer_offset(const void * row,const void * addr)41396e0279dSAndrey Konovalov static int meta_pointer_offset(const void *row, const void *addr)
4140b24beccSAndrey Ryabinin {
41596e0279dSAndrey Konovalov /*
41696e0279dSAndrey Konovalov * Memory state around the buggy address:
41796e0279dSAndrey Konovalov * ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe
41896e0279dSAndrey Konovalov * ...
41996e0279dSAndrey Konovalov *
42096e0279dSAndrey Konovalov * The length of ">ff00ff00ff00ff00: " is
4210b24beccSAndrey Ryabinin * 3 + (BITS_PER_LONG / 8) * 2 chars.
42296e0279dSAndrey Konovalov * The length of each granule metadata is 2 bytes
42396e0279dSAndrey Konovalov * plus 1 byte for space.
4240b24beccSAndrey Ryabinin */
42596e0279dSAndrey Konovalov return 3 + (BITS_PER_LONG / 8) * 2 +
42696e0279dSAndrey Konovalov (addr - row) / KASAN_GRANULE_SIZE * 3 + 1;
4270b24beccSAndrey Ryabinin }
4280b24beccSAndrey Ryabinin
print_memory_metadata(const void * addr)429db3de8f7SAndrey Konovalov static void print_memory_metadata(const void *addr)
4300b24beccSAndrey Ryabinin {
4310b24beccSAndrey Ryabinin int i;
43296e0279dSAndrey Konovalov void *row;
4330b24beccSAndrey Ryabinin
43496e0279dSAndrey Konovalov row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW)
43596e0279dSAndrey Konovalov - META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW;
4360b24beccSAndrey Ryabinin
4370b24beccSAndrey Ryabinin pr_err("Memory state around the buggy address:\n");
4380b24beccSAndrey Ryabinin
43988b86597SAndrey Konovalov for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
4400b24beccSAndrey Ryabinin char buffer[4 + (BITS_PER_LONG / 8) * 2];
44196e0279dSAndrey Konovalov char metadata[META_BYTES_PER_ROW];
4420b24beccSAndrey Ryabinin
4430b24beccSAndrey Ryabinin snprintf(buffer, sizeof(buffer),
44496e0279dSAndrey Konovalov (i == 0) ? ">%px: " : " %px: ", row);
44596e0279dSAndrey Konovalov
446f2377d4eSAneesh Kumar K.V /*
447f2377d4eSAneesh Kumar K.V * We should not pass a shadow pointer to generic
448f2377d4eSAneesh Kumar K.V * function, because generic functions may try to
449f2377d4eSAneesh Kumar K.V * access kasan mapping for the passed address.
450f2377d4eSAneesh Kumar K.V */
451f00748bfSAndrey Konovalov kasan_metadata_fetch_row(&metadata[0], row);
45296e0279dSAndrey Konovalov
4530b24beccSAndrey Ryabinin print_hex_dump(KERN_ERR, buffer,
45488b86597SAndrey Konovalov DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
45596e0279dSAndrey Konovalov metadata, META_BYTES_PER_ROW, 0);
4560b24beccSAndrey Ryabinin
45796e0279dSAndrey Konovalov if (meta_row_is_guilty(row, addr))
45896e0279dSAndrey Konovalov pr_err("%*c\n", meta_pointer_offset(row, addr), '^');
4590b24beccSAndrey Ryabinin
46096e0279dSAndrey Konovalov row += META_MEM_BYTES_PER_ROW;
4610b24beccSAndrey Ryabinin }
4620b24beccSAndrey Ryabinin }
4630b24beccSAndrey Ryabinin
print_report(struct kasan_report_info * info)464c965cdd6SAndrey Konovalov static void print_report(struct kasan_report_info *info)
4659d7b7dd9SAndrey Konovalov {
466bb6e04a1SArnd Bergmann void *addr = kasan_reset_tag((void *)info->access_addr);
467bb6e04a1SArnd Bergmann u8 tag = get_tag((void *)info->access_addr);
4689d7b7dd9SAndrey Konovalov
4699d7b7dd9SAndrey Konovalov print_error_description(info);
470a794898aSAndrey Konovalov if (addr_has_metadata(addr))
4719d7b7dd9SAndrey Konovalov kasan_print_tags(tag, info->first_bad_addr);
4729d7b7dd9SAndrey Konovalov pr_err("\n");
4739d7b7dd9SAndrey Konovalov
474a794898aSAndrey Konovalov if (addr_has_metadata(addr)) {
4757fae3dd0SAndrey Konovalov print_address_description(addr, tag, info);
4769d7b7dd9SAndrey Konovalov print_memory_metadata(info->first_bad_addr);
4779d7b7dd9SAndrey Konovalov } else {
4789d7b7dd9SAndrey Konovalov dump_stack_lvl(KERN_ERR);
4799d7b7dd9SAndrey Konovalov }
4809d7b7dd9SAndrey Konovalov }
4819d7b7dd9SAndrey Konovalov
complete_report_info(struct kasan_report_info * info)482015b109fSAndrey Konovalov static void complete_report_info(struct kasan_report_info *info)
483015b109fSAndrey Konovalov {
484bb6e04a1SArnd Bergmann void *addr = kasan_reset_tag((void *)info->access_addr);
4857fae3dd0SAndrey Konovalov struct slab *slab;
486015b109fSAndrey Konovalov
487015b109fSAndrey Konovalov if (info->type == KASAN_REPORT_ACCESS)
488015b109fSAndrey Konovalov info->first_bad_addr = kasan_find_first_bad_addr(
489bb6e04a1SArnd Bergmann (void *)info->access_addr, info->access_size);
490015b109fSAndrey Konovalov else
491015b109fSAndrey Konovalov info->first_bad_addr = addr;
4927fae3dd0SAndrey Konovalov
4937fae3dd0SAndrey Konovalov slab = kasan_addr_to_slab(addr);
4947fae3dd0SAndrey Konovalov if (slab) {
4957fae3dd0SAndrey Konovalov info->cache = slab->slab_cache;
4967fae3dd0SAndrey Konovalov info->object = nearest_obj(info->cache, slab, addr);
4978f17febbSKuan-Ying Lee
4988f17febbSKuan-Ying Lee /* Try to determine allocation size based on the metadata. */
4998f17febbSKuan-Ying Lee info->alloc_size = kasan_get_alloc_size(info->object, info->cache);
5008f17febbSKuan-Ying Lee /* Fallback to the object size if failed. */
5018f17febbSKuan-Ying Lee if (!info->alloc_size)
5028f17febbSKuan-Ying Lee info->alloc_size = info->cache->object_size;
5037fae3dd0SAndrey Konovalov } else
5047fae3dd0SAndrey Konovalov info->cache = info->object = NULL;
50559e6e098SAndrey Konovalov
506dcc57966SAndrey Konovalov switch (info->type) {
507dcc57966SAndrey Konovalov case KASAN_REPORT_INVALID_FREE:
508dcc57966SAndrey Konovalov info->bug_type = "invalid-free";
509dcc57966SAndrey Konovalov break;
510dcc57966SAndrey Konovalov case KASAN_REPORT_DOUBLE_FREE:
511dcc57966SAndrey Konovalov info->bug_type = "double-free";
512dcc57966SAndrey Konovalov break;
513dcc57966SAndrey Konovalov default:
514dcc57966SAndrey Konovalov /* bug_type filled in by kasan_complete_mode_report_info. */
515dcc57966SAndrey Konovalov break;
516dcc57966SAndrey Konovalov }
517dcc57966SAndrey Konovalov
51859e6e098SAndrey Konovalov /* Fill in mode-specific report info fields. */
51959e6e098SAndrey Konovalov kasan_complete_mode_report_info(info);
520015b109fSAndrey Konovalov }
521015b109fSAndrey Konovalov
kasan_report_invalid_free(void * ptr,unsigned long ip,enum kasan_report_type type)5223de0de75SKuan-Ying Lee void kasan_report_invalid_free(void *ptr, unsigned long ip, enum kasan_report_type type)
52331c65110SAndrey Konovalov {
52431c65110SAndrey Konovalov unsigned long flags;
525c965cdd6SAndrey Konovalov struct kasan_report_info info;
52631c65110SAndrey Konovalov
527c068664cSAndrey Konovalov /*
528c6a690e0SAndrey Konovalov * Do not check report_suppressed_sw(), as an invalid-free cannot be
529c6a690e0SAndrey Konovalov * caused by accessing poisoned memory and thus should not be suppressed
530c6a690e0SAndrey Konovalov * by kasan_disable/enable_current() critical sections.
531c6a690e0SAndrey Konovalov *
532c6a690e0SAndrey Konovalov * Note that for Hardware Tag-Based KASAN, kasan_report_invalid_free()
533c6a690e0SAndrey Konovalov * is triggered by explicit tag checks and not by the ones performed by
534c6a690e0SAndrey Konovalov * the CPU. Thus, reporting invalid-free is not suppressed as well.
535c068664cSAndrey Konovalov */
536c068664cSAndrey Konovalov if (unlikely(!report_enabled()))
537c068664cSAndrey Konovalov return;
538c068664cSAndrey Konovalov
53931c65110SAndrey Konovalov start_report(&flags, true);
54031c65110SAndrey Konovalov
54159e6e098SAndrey Konovalov memset(&info, 0, sizeof(info));
5423de0de75SKuan-Ying Lee info.type = type;
54331c65110SAndrey Konovalov info.access_addr = ptr;
54431c65110SAndrey Konovalov info.access_size = 0;
54531c65110SAndrey Konovalov info.is_write = false;
54631c65110SAndrey Konovalov info.ip = ip;
54731c65110SAndrey Konovalov
548015b109fSAndrey Konovalov complete_report_info(&info);
549015b109fSAndrey Konovalov
55031c65110SAndrey Konovalov print_report(&info);
55131c65110SAndrey Konovalov
552452c03fdSMarco Elver /*
553452c03fdSMarco Elver * Invalid free is considered a "write" since the allocator's metadata
554452c03fdSMarco Elver * updates involves writes.
555452c03fdSMarco Elver */
556452c03fdSMarco Elver end_report(&flags, ptr, true);
55731c65110SAndrey Konovalov }
55831c65110SAndrey Konovalov
559795b760fSAndrey Konovalov /*
560795b760fSAndrey Konovalov * kasan_report() is the only reporting function that uses
561795b760fSAndrey Konovalov * user_access_save/restore(): kasan_report_invalid_free() cannot be called
562795b760fSAndrey Konovalov * from a UACCESS region, and kasan_report_async() is not used on x86.
563795b760fSAndrey Konovalov */
kasan_report(const void * addr,size_t size,bool is_write,unsigned long ip)564bb6e04a1SArnd Bergmann bool kasan_report(const void *addr, size_t size, bool is_write,
5654fba3758SAndrey Konovalov unsigned long ip)
5664fba3758SAndrey Konovalov {
567b3bb1d70SAndrey Konovalov bool ret = true;
568be8631a1SAndrey Konovalov unsigned long ua_flags = user_access_save();
569be8631a1SAndrey Konovalov unsigned long irq_flags;
570c965cdd6SAndrey Konovalov struct kasan_report_info info;
5714fba3758SAndrey Konovalov
572c6a690e0SAndrey Konovalov if (unlikely(report_suppressed_sw()) || unlikely(!report_enabled())) {
573b3bb1d70SAndrey Konovalov ret = false;
574b3bb1d70SAndrey Konovalov goto out;
5754fba3758SAndrey Konovalov }
5764fba3758SAndrey Konovalov
577be8631a1SAndrey Konovalov start_report(&irq_flags, true);
578be8631a1SAndrey Konovalov
57959e6e098SAndrey Konovalov memset(&info, 0, sizeof(info));
58031c65110SAndrey Konovalov info.type = KASAN_REPORT_ACCESS;
581bb6e04a1SArnd Bergmann info.access_addr = addr;
582be8631a1SAndrey Konovalov info.access_size = size;
583be8631a1SAndrey Konovalov info.is_write = is_write;
584be8631a1SAndrey Konovalov info.ip = ip;
585be8631a1SAndrey Konovalov
586015b109fSAndrey Konovalov complete_report_info(&info);
587015b109fSAndrey Konovalov
588be8631a1SAndrey Konovalov print_report(&info);
589be8631a1SAndrey Konovalov
590452c03fdSMarco Elver end_report(&irq_flags, (void *)addr, is_write);
591b3bb1d70SAndrey Konovalov
592b3bb1d70SAndrey Konovalov out:
593b3bb1d70SAndrey Konovalov user_access_restore(ua_flags);
5944fba3758SAndrey Konovalov
5954fba3758SAndrey Konovalov return ret;
5964fba3758SAndrey Konovalov }
5974fba3758SAndrey Konovalov
598bb2f967cSAndrey Konovalov #ifdef CONFIG_KASAN_HW_TAGS
kasan_report_async(void)599bb2f967cSAndrey Konovalov void kasan_report_async(void)
600bb2f967cSAndrey Konovalov {
601bb2f967cSAndrey Konovalov unsigned long flags;
602bb2f967cSAndrey Konovalov
603c068664cSAndrey Konovalov /*
604c6a690e0SAndrey Konovalov * Do not check report_suppressed_sw(), as
605c6a690e0SAndrey Konovalov * kasan_disable/enable_current() critical sections do not affect
606c6a690e0SAndrey Konovalov * Hardware Tag-Based KASAN.
607c068664cSAndrey Konovalov */
608c068664cSAndrey Konovalov if (unlikely(!report_enabled()))
609c068664cSAndrey Konovalov return;
610c068664cSAndrey Konovalov
611bb2f967cSAndrey Konovalov start_report(&flags, false);
612bb2f967cSAndrey Konovalov pr_err("BUG: KASAN: invalid-access\n");
613bb2f967cSAndrey Konovalov pr_err("Asynchronous fault: no details available\n");
614bb2f967cSAndrey Konovalov pr_err("\n");
615bb2f967cSAndrey Konovalov dump_stack_lvl(KERN_ERR);
616452c03fdSMarco Elver /*
617452c03fdSMarco Elver * Conservatively set is_write=true, because no details are available.
618452c03fdSMarco Elver * In this mode, kasan.fault=panic_on_write is like kasan.fault=panic.
619452c03fdSMarco Elver */
620452c03fdSMarco Elver end_report(&flags, NULL, true);
621bb2f967cSAndrey Konovalov }
622bb2f967cSAndrey Konovalov #endif /* CONFIG_KASAN_HW_TAGS */
623bb2f967cSAndrey Konovalov
62417c17567SArnd Bergmann #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
6252f004eeaSJann Horn /*
62617c17567SArnd Bergmann * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
6272f004eeaSJann Horn * canonical half of the address space) cause out-of-bounds shadow memory reads
6282f004eeaSJann Horn * before the actual access. For addresses in the low canonical half of the
6292f004eeaSJann Horn * address space, as well as most non-canonical addresses, that out-of-bounds
6302f004eeaSJann Horn * shadow memory access lands in the non-canonical part of the address space.
6312f004eeaSJann Horn * Help the user figure out what the original bogus pointer was.
6322f004eeaSJann Horn */
kasan_non_canonical_hook(unsigned long addr)6332f004eeaSJann Horn void kasan_non_canonical_hook(unsigned long addr)
6342f004eeaSJann Horn {
6352f004eeaSJann Horn unsigned long orig_addr;
6362f004eeaSJann Horn const char *bug_type;
6372f004eeaSJann Horn
6382f004eeaSJann Horn if (addr < KASAN_SHADOW_OFFSET)
6392f004eeaSJann Horn return;
6402f004eeaSJann Horn
6412f004eeaSJann Horn orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
6422f004eeaSJann Horn /*
6432f004eeaSJann Horn * For faults near the shadow address for NULL, we can be fairly certain
6442f004eeaSJann Horn * that this is a KASAN shadow memory access.
6452f004eeaSJann Horn * For faults that correspond to shadow for low canonical addresses, we
6462f004eeaSJann Horn * can still be pretty sure - that shadow region is a fairly narrow
6472f004eeaSJann Horn * chunk of the non-canonical address space.
6482f004eeaSJann Horn * But faults that look like shadow for non-canonical addresses are a
6492f004eeaSJann Horn * really large chunk of the address space. In that case, we still
6502f004eeaSJann Horn * print the decoded address, but make it clear that this is not
6512f004eeaSJann Horn * necessarily what's actually going on.
6522f004eeaSJann Horn */
6532f004eeaSJann Horn if (orig_addr < PAGE_SIZE)
6542f004eeaSJann Horn bug_type = "null-ptr-deref";
6552f004eeaSJann Horn else if (orig_addr < TASK_SIZE)
6562f004eeaSJann Horn bug_type = "probably user-memory-access";
6572f004eeaSJann Horn else
6582f004eeaSJann Horn bug_type = "maybe wild-memory-access";
6592f004eeaSJann Horn pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
6601f600626SAndrey Konovalov orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1);
6612f004eeaSJann Horn }
66217c17567SArnd Bergmann #endif
663