xref: /openbmc/linux/mm/kasan/report.c (revision 9ef08d265e3f02b3266a46f684de5741724bd7f8)
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 
12b0845ce5SMark Rutland #include <linux/bitops.h>
134f40c6e5SPeter Zijlstra #include <linux/ftrace.h>
14b0845ce5SMark Rutland #include <linux/init.h>
150b24beccSAndrey Ryabinin #include <linux/kernel.h>
16c32caa26SAndrey Konovalov #include <linux/lockdep.h>
170b24beccSAndrey Ryabinin #include <linux/mm.h>
180b24beccSAndrey Ryabinin #include <linux/printk.h>
190b24beccSAndrey Ryabinin #include <linux/sched.h>
200b24beccSAndrey Ryabinin #include <linux/slab.h>
21cd11016eSAlexander Potapenko #include <linux/stackdepot.h>
220b24beccSAndrey Ryabinin #include <linux/stacktrace.h>
230b24beccSAndrey Ryabinin #include <linux/string.h>
240b24beccSAndrey Ryabinin #include <linux/types.h>
250b24beccSAndrey Ryabinin #include <linux/kasan.h>
26527f215bSAneesh Kumar K.V #include <linux/module.h>
27e8969219SMarco Elver #include <linux/sched/task_stack.h>
284fba3758SAndrey Konovalov #include <linux/uaccess.h>
29d3a61f74SAlexander Potapenko #include <trace/events/error_report.h>
300b24beccSAndrey Ryabinin 
31bebf56a1SAndrey Ryabinin #include <asm/sections.h>
32bebf56a1SAndrey Ryabinin 
3383c4e7a0SPatricia Alfonso #include <kunit/test.h>
3483c4e7a0SPatricia Alfonso 
350b24beccSAndrey Ryabinin #include "kasan.h"
360316bec2SAndrey Ryabinin #include "../slab.h"
370b24beccSAndrey Ryabinin 
3811cd3cd6SAndrey Konovalov static unsigned long kasan_flags;
3911cd3cd6SAndrey Konovalov 
4011cd3cd6SAndrey Konovalov #define KASAN_BIT_REPORTED	0
4111cd3cd6SAndrey Konovalov #define KASAN_BIT_MULTI_SHOT	1
4211cd3cd6SAndrey Konovalov 
43c9d1af2bSWoody Lin enum kasan_arg_fault {
44c9d1af2bSWoody Lin 	KASAN_ARG_FAULT_DEFAULT,
45c9d1af2bSWoody Lin 	KASAN_ARG_FAULT_REPORT,
46c9d1af2bSWoody Lin 	KASAN_ARG_FAULT_PANIC,
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 */
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;
61c9d1af2bSWoody Lin 	else
62c9d1af2bSWoody Lin 		return -EINVAL;
63c9d1af2bSWoody Lin 
64c9d1af2bSWoody Lin 	return 0;
65c9d1af2bSWoody Lin }
66c9d1af2bSWoody Lin early_param("kasan.fault", early_kasan_fault);
67c9d1af2bSWoody Lin 
6811cd3cd6SAndrey Konovalov static int __init kasan_set_multi_shot(char *str)
690b24beccSAndrey Ryabinin {
7011cd3cd6SAndrey Konovalov 	set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
7111cd3cd6SAndrey Konovalov 	return 1;
720b24beccSAndrey Ryabinin }
7311cd3cd6SAndrey Konovalov __setup("kasan_multi_shot", kasan_set_multi_shot);
740b24beccSAndrey Ryabinin 
75865bfa28SAndrey Konovalov /*
76865bfa28SAndrey Konovalov  * Used to suppress reports within kasan_disable/enable_current() critical
77865bfa28SAndrey Konovalov  * sections, which are used for marking accesses to slab metadata.
78865bfa28SAndrey Konovalov  */
79865bfa28SAndrey Konovalov static bool report_suppressed(void)
805e82cd12SAndrey Konovalov {
81865bfa28SAndrey Konovalov #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
82865bfa28SAndrey Konovalov 	if (current->kasan_depth)
83865bfa28SAndrey Konovalov 		return true;
84865bfa28SAndrey Konovalov #endif
85865bfa28SAndrey Konovalov 	return false;
8631c65110SAndrey Konovalov }
8731c65110SAndrey Konovalov 
88865bfa28SAndrey Konovalov /*
89865bfa28SAndrey Konovalov  * Used to avoid reporting more than one KASAN bug unless kasan_multi_shot
90865bfa28SAndrey Konovalov  * is enabled. Note that KASAN tests effectively enable kasan_multi_shot
91865bfa28SAndrey Konovalov  * for their duration.
92865bfa28SAndrey Konovalov  */
93865bfa28SAndrey Konovalov static bool report_enabled(void)
94865bfa28SAndrey Konovalov {
95865bfa28SAndrey Konovalov 	if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
96865bfa28SAndrey Konovalov 		return true;
97865bfa28SAndrey Konovalov 	return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
980b24beccSAndrey Ryabinin }
990b24beccSAndrey Ryabinin 
10080207910SAndrey Konovalov #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) || IS_ENABLED(CONFIG_KASAN_MODULE_TEST)
10180207910SAndrey Konovalov 
10280207910SAndrey Konovalov bool kasan_save_enable_multi_shot(void)
10380207910SAndrey Konovalov {
10480207910SAndrey Konovalov 	return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
10580207910SAndrey Konovalov }
10680207910SAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
10780207910SAndrey Konovalov 
10880207910SAndrey Konovalov void kasan_restore_multi_shot(bool enabled)
10980207910SAndrey Konovalov {
11080207910SAndrey Konovalov 	if (!enabled)
11180207910SAndrey Konovalov 		clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
11280207910SAndrey Konovalov }
11380207910SAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
11480207910SAndrey Konovalov 
11580207910SAndrey Konovalov #endif
11680207910SAndrey Konovalov 
117a260d281SAndrey Konovalov #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST)
118a260d281SAndrey Konovalov static void update_kunit_status(bool sync)
119a260d281SAndrey Konovalov {
120a260d281SAndrey Konovalov 	struct kunit *test;
121a260d281SAndrey Konovalov 	struct kunit_resource *resource;
122a260d281SAndrey Konovalov 	struct kunit_kasan_status *status;
123a260d281SAndrey Konovalov 
124a260d281SAndrey Konovalov 	test = current->kunit_test;
125a260d281SAndrey Konovalov 	if (!test)
126a260d281SAndrey Konovalov 		return;
127a260d281SAndrey Konovalov 
128a260d281SAndrey Konovalov 	resource = kunit_find_named_resource(test, "kasan_status");
129a260d281SAndrey Konovalov 	if (!resource) {
130a260d281SAndrey Konovalov 		kunit_set_failure(test);
131a260d281SAndrey Konovalov 		return;
132a260d281SAndrey Konovalov 	}
133a260d281SAndrey Konovalov 
134a260d281SAndrey Konovalov 	status = (struct kunit_kasan_status *)resource->data;
135a260d281SAndrey Konovalov 	WRITE_ONCE(status->report_found, true);
136a260d281SAndrey Konovalov 	WRITE_ONCE(status->sync_fault, sync);
137a260d281SAndrey Konovalov 
138a260d281SAndrey Konovalov 	kunit_put_resource(resource);
139a260d281SAndrey Konovalov }
140a260d281SAndrey Konovalov #else
141a260d281SAndrey Konovalov static void update_kunit_status(bool sync) { }
142a260d281SAndrey Konovalov #endif
143a260d281SAndrey Konovalov 
1447e088978SAndrey Ryabinin static DEFINE_SPINLOCK(report_lock);
1457e088978SAndrey Ryabinin 
146a260d281SAndrey Konovalov static void start_report(unsigned long *flags, bool sync)
1477e088978SAndrey Ryabinin {
1480a6e8a07SAndrey Konovalov 	/* Respect the /proc/sys/kernel/traceoff_on_warning interface. */
1490a6e8a07SAndrey Konovalov 	disable_trace_on_warning();
150a260d281SAndrey Konovalov 	/* Update status of the currently running KASAN test. */
151a260d281SAndrey Konovalov 	update_kunit_status(sync);
152c32caa26SAndrey Konovalov 	/* Do not allow LOCKDEP mangling KASAN reports. */
153c32caa26SAndrey Konovalov 	lockdep_off();
154a260d281SAndrey Konovalov 	/* Make sure we don't end up in loop. */
1557e088978SAndrey Ryabinin 	kasan_disable_current();
1567e088978SAndrey Ryabinin 	spin_lock_irqsave(&report_lock, *flags);
1577e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
1587e088978SAndrey Ryabinin }
1597e088978SAndrey Ryabinin 
1609d7b7dd9SAndrey Konovalov static void end_report(unsigned long *flags, void *addr)
1617e088978SAndrey Ryabinin {
162476b1dc2SAndrey Konovalov 	if (addr)
1639d7b7dd9SAndrey Konovalov 		trace_error_report_end(ERROR_DETECTOR_KASAN,
1649d7b7dd9SAndrey Konovalov 				       (unsigned long)addr);
1657e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
1667e088978SAndrey Ryabinin 	spin_unlock_irqrestore(&report_lock, *flags);
167e7ce7500STiezhu Yang 	if (panic_on_warn && !test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
1685c5c1f36SDmitry Vyukov 		panic("panic_on_warn set ...\n");
169c9d1af2bSWoody Lin 	if (kasan_arg_fault == KASAN_ARG_FAULT_PANIC)
1708028caacSAndrey Konovalov 		panic("kasan.fault=panic set ...\n");
171c32caa26SAndrey Konovalov 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
172c32caa26SAndrey Konovalov 	lockdep_on();
1737e088978SAndrey Ryabinin 	kasan_enable_current();
1747e088978SAndrey Ryabinin }
1757e088978SAndrey Ryabinin 
176865bfa28SAndrey Konovalov static void print_error_description(struct kasan_report_info *info)
177865bfa28SAndrey Konovalov {
178865bfa28SAndrey Konovalov 	if (info->type == KASAN_REPORT_INVALID_FREE) {
1793de0de75SKuan-Ying Lee 		pr_err("BUG: KASAN: invalid-free in %pS\n", (void *)info->ip);
1803de0de75SKuan-Ying Lee 		return;
1813de0de75SKuan-Ying Lee 	}
1823de0de75SKuan-Ying Lee 
1833de0de75SKuan-Ying Lee 	if (info->type == KASAN_REPORT_DOUBLE_FREE) {
1843de0de75SKuan-Ying Lee 		pr_err("BUG: KASAN: double-free in %pS\n", (void *)info->ip);
185865bfa28SAndrey Konovalov 		return;
186865bfa28SAndrey Konovalov 	}
187865bfa28SAndrey Konovalov 
188865bfa28SAndrey Konovalov 	pr_err("BUG: KASAN: %s in %pS\n",
189865bfa28SAndrey Konovalov 		kasan_get_bug_type(info), (void *)info->ip);
190865bfa28SAndrey Konovalov 	if (info->access_size)
191865bfa28SAndrey Konovalov 		pr_err("%s of size %zu at addr %px by task %s/%d\n",
192865bfa28SAndrey Konovalov 			info->is_write ? "Write" : "Read", info->access_size,
193865bfa28SAndrey Konovalov 			info->access_addr, current->comm, task_pid_nr(current));
194865bfa28SAndrey Konovalov 	else
195865bfa28SAndrey Konovalov 		pr_err("%s at addr %px by task %s/%d\n",
196865bfa28SAndrey Konovalov 			info->is_write ? "Write" : "Read",
197865bfa28SAndrey Konovalov 			info->access_addr, current->comm, task_pid_nr(current));
198865bfa28SAndrey Konovalov }
199865bfa28SAndrey Konovalov 
200b6b72f49SAndrey Konovalov static void print_track(struct kasan_track *track, const char *prefix)
2017ed2f9e6SAlexander Potapenko {
202b6b72f49SAndrey Konovalov 	pr_err("%s by task %u:\n", prefix, track->pid);
203*9ef08d26SAndrey Konovalov 	if (track->stack)
204505be481SImran Khan 		stack_depot_print(track->stack);
205*9ef08d26SAndrey Konovalov 	else
206cd11016eSAlexander Potapenko 		pr_err("(stack is not available)\n");
207cd11016eSAlexander Potapenko }
2087ed2f9e6SAlexander Potapenko 
209ae8f06b3SWalter Wu struct page *kasan_addr_to_page(const void *addr)
210db429f16SAndrey Konovalov {
211*9ef08d26SAndrey Konovalov 	if ((addr >= (void *)PAGE_OFFSET) && (addr < high_memory))
212db429f16SAndrey Konovalov 		return virt_to_head_page(addr);
213db429f16SAndrey Konovalov 	return NULL;
214db429f16SAndrey Konovalov }
215db429f16SAndrey Konovalov 
2166e48a966SMatthew Wilcox (Oracle) struct slab *kasan_addr_to_slab(const void *addr)
2176e48a966SMatthew Wilcox (Oracle) {
218*9ef08d26SAndrey Konovalov 	if ((addr >= (void *)PAGE_OFFSET) && (addr < high_memory))
2196e48a966SMatthew Wilcox (Oracle) 		return virt_to_slab(addr);
2206e48a966SMatthew Wilcox (Oracle) 	return NULL;
2216e48a966SMatthew Wilcox (Oracle) }
2226e48a966SMatthew Wilcox (Oracle) 
2230c06f1f8SAndrey Konovalov static void describe_object_addr(struct kmem_cache *cache, void *object,
2240c06f1f8SAndrey Konovalov 				const void *addr)
2250c06f1f8SAndrey Konovalov {
2260c06f1f8SAndrey Konovalov 	unsigned long access_addr = (unsigned long)addr;
2270c06f1f8SAndrey Konovalov 	unsigned long object_addr = (unsigned long)object;
2280c06f1f8SAndrey Konovalov 	const char *rel_type;
2290c06f1f8SAndrey Konovalov 	int rel_bytes;
2300c06f1f8SAndrey Konovalov 
2316424f6bbSTobin C. Harding 	pr_err("The buggy address belongs to the object at %px\n"
2320c06f1f8SAndrey Konovalov 	       " which belongs to the cache %s of size %d\n",
2330c06f1f8SAndrey Konovalov 		object, cache->name, cache->object_size);
2340c06f1f8SAndrey Konovalov 
2350c06f1f8SAndrey Konovalov 	if (access_addr < object_addr) {
2360c06f1f8SAndrey Konovalov 		rel_type = "to the left";
2370c06f1f8SAndrey Konovalov 		rel_bytes = object_addr - access_addr;
2380c06f1f8SAndrey Konovalov 	} else if (access_addr >= object_addr + cache->object_size) {
2390c06f1f8SAndrey Konovalov 		rel_type = "to the right";
2400c06f1f8SAndrey Konovalov 		rel_bytes = access_addr - (object_addr + cache->object_size);
2410c06f1f8SAndrey Konovalov 	} else {
2420c06f1f8SAndrey Konovalov 		rel_type = "inside";
2430c06f1f8SAndrey Konovalov 		rel_bytes = access_addr - object_addr;
2440c06f1f8SAndrey Konovalov 	}
2450c06f1f8SAndrey Konovalov 
2460c06f1f8SAndrey Konovalov 	pr_err("The buggy address is located %d bytes %s of\n"
2476424f6bbSTobin C. Harding 	       " %d-byte region [%px, %px)\n",
2480c06f1f8SAndrey Konovalov 		rel_bytes, rel_type, cache->object_size, (void *)object_addr,
2490c06f1f8SAndrey Konovalov 		(void *)(object_addr + cache->object_size));
2500c06f1f8SAndrey Konovalov }
2510c06f1f8SAndrey Konovalov 
2528028caacSAndrey Konovalov static void describe_object_stacks(struct kmem_cache *cache, void *object,
253ae8f06b3SWalter Wu 					const void *addr, u8 tag)
2547ed2f9e6SAlexander Potapenko {
255f3647cbfSAndrey Konovalov 	struct kasan_track *alloc_track;
256ae8f06b3SWalter Wu 	struct kasan_track *free_track;
257ae8f06b3SWalter Wu 
258f3647cbfSAndrey Konovalov 	alloc_track = kasan_get_alloc_track(cache, object);
259f3647cbfSAndrey Konovalov 	if (alloc_track) {
260f3647cbfSAndrey Konovalov 		print_track(alloc_track, "Allocated");
261b1938599SAndrey Konovalov 		pr_err("\n");
26297593cadSAndrey Konovalov 	}
26397593cadSAndrey Konovalov 
264ae8f06b3SWalter Wu 	free_track = kasan_get_free_track(cache, object, tag);
265e4b7818bSWalter Wu 	if (free_track) {
266ae8f06b3SWalter Wu 		print_track(free_track, "Freed");
267b1938599SAndrey Konovalov 		pr_err("\n");
268e4b7818bSWalter Wu 	}
26926e760c9SWalter Wu 
27088f29765SAndrey Konovalov 	kasan_print_aux_stacks(cache, object);
2717ed2f9e6SAlexander Potapenko }
2727ed2f9e6SAlexander Potapenko 
2738028caacSAndrey Konovalov static void describe_object(struct kmem_cache *cache, void *object,
2748028caacSAndrey Konovalov 				const void *addr, u8 tag)
2758028caacSAndrey Konovalov {
2768028caacSAndrey Konovalov 	if (kasan_stack_collection_enabled())
2778028caacSAndrey Konovalov 		describe_object_stacks(cache, object, addr, tag);
2780c06f1f8SAndrey Konovalov 	describe_object_addr(cache, object, addr);
2790c06f1f8SAndrey Konovalov }
2800c06f1f8SAndrey Konovalov 
28111cd3cd6SAndrey Konovalov static inline bool kernel_or_module_addr(const void *addr)
28211cd3cd6SAndrey Konovalov {
2833298cbe8SKefeng Wang 	if (is_kernel((unsigned long)addr))
28411cd3cd6SAndrey Konovalov 		return true;
28511cd3cd6SAndrey Konovalov 	if (is_module_address((unsigned long)addr))
28611cd3cd6SAndrey Konovalov 		return true;
28711cd3cd6SAndrey Konovalov 	return false;
28811cd3cd6SAndrey Konovalov }
28911cd3cd6SAndrey Konovalov 
29011cd3cd6SAndrey Konovalov static inline bool init_task_stack_addr(const void *addr)
29111cd3cd6SAndrey Konovalov {
29211cd3cd6SAndrey Konovalov 	return addr >= (void *)&init_thread_union.stack &&
29311cd3cd6SAndrey Konovalov 		(addr <= (void *)&init_thread_union.stack +
29411cd3cd6SAndrey Konovalov 			sizeof(init_thread_union.stack));
29511cd3cd6SAndrey Konovalov }
29611cd3cd6SAndrey Konovalov 
297ae8f06b3SWalter Wu static void print_address_description(void *addr, u8 tag)
2987e088978SAndrey Ryabinin {
299ae8f06b3SWalter Wu 	struct page *page = kasan_addr_to_page(addr);
300b8c73fc2SAndrey Ryabinin 
301336abff6SAlexander Potapenko 	dump_stack_lvl(KERN_ERR);
302b1938599SAndrey Konovalov 	pr_err("\n");
303db429f16SAndrey Konovalov 
304db429f16SAndrey Konovalov 	if (page && PageSlab(page)) {
3056e48a966SMatthew Wilcox (Oracle) 		struct slab *slab = page_slab(page);
3066e48a966SMatthew Wilcox (Oracle) 		struct kmem_cache *cache = slab->slab_cache;
3076e48a966SMatthew Wilcox (Oracle) 		void *object = nearest_obj(cache, slab,	addr);
308db429f16SAndrey Konovalov 
309ae8f06b3SWalter Wu 		describe_object(cache, object, addr, tag);
310038fd2b4SAndrey Konovalov 		pr_err("\n");
311b8c73fc2SAndrey Ryabinin 	}
312b8c73fc2SAndrey Ryabinin 
313430a05f9SAndrey Konovalov 	if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
314430a05f9SAndrey Konovalov 		pr_err("The buggy address belongs to the variable:\n");
315430a05f9SAndrey Konovalov 		pr_err(" %pS\n", addr);
316038fd2b4SAndrey Konovalov 		pr_err("\n");
317430a05f9SAndrey Konovalov 	}
318430a05f9SAndrey Konovalov 
3190f9b35f3SAndrey Konovalov 	if (object_is_on_stack(addr)) {
3200f9b35f3SAndrey Konovalov 		/*
3210f9b35f3SAndrey Konovalov 		 * Currently, KASAN supports printing frame information only
3220f9b35f3SAndrey Konovalov 		 * for accesses to the task's own stack.
3230f9b35f3SAndrey Konovalov 		 */
3240f9b35f3SAndrey Konovalov 		kasan_print_address_stack_frame(addr);
3250f9b35f3SAndrey Konovalov 		pr_err("\n");
3260f9b35f3SAndrey Konovalov 	}
3270f9b35f3SAndrey Konovalov 
328c056a364SAndrey Konovalov 	if (is_vmalloc_addr(addr)) {
329c056a364SAndrey Konovalov 		struct vm_struct *va = find_vm_area(addr);
330c056a364SAndrey Konovalov 
331c056a364SAndrey Konovalov 		if (va) {
332c056a364SAndrey Konovalov 			pr_err("The buggy address belongs to the virtual mapping at\n"
333c056a364SAndrey Konovalov 			       " [%px, %px) created by:\n"
334c056a364SAndrey Konovalov 			       " %pS\n",
335c056a364SAndrey Konovalov 			       va->addr, va->addr + va->size, va->caller);
336038fd2b4SAndrey Konovalov 			pr_err("\n");
337c056a364SAndrey Konovalov 
338fbf4df06SKefeng Wang 			page = vmalloc_to_page(addr);
339c056a364SAndrey Konovalov 		}
340c056a364SAndrey Konovalov 	}
341c056a364SAndrey Konovalov 
342430a05f9SAndrey Konovalov 	if (page) {
343c056a364SAndrey Konovalov 		pr_err("The buggy address belongs to the physical page:\n");
344430a05f9SAndrey Konovalov 		dump_page(page, "kasan: bad access detected");
345038fd2b4SAndrey Konovalov 		pr_err("\n");
346bebf56a1SAndrey Ryabinin 	}
3470b24beccSAndrey Ryabinin }
3480b24beccSAndrey Ryabinin 
34996e0279dSAndrey Konovalov static bool meta_row_is_guilty(const void *row, const void *addr)
3500b24beccSAndrey Ryabinin {
35196e0279dSAndrey Konovalov 	return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW);
3520b24beccSAndrey Ryabinin }
3530b24beccSAndrey Ryabinin 
35496e0279dSAndrey Konovalov static int meta_pointer_offset(const void *row, const void *addr)
3550b24beccSAndrey Ryabinin {
35696e0279dSAndrey Konovalov 	/*
35796e0279dSAndrey Konovalov 	 * Memory state around the buggy address:
35896e0279dSAndrey Konovalov 	 *  ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe
35996e0279dSAndrey Konovalov 	 *  ...
36096e0279dSAndrey Konovalov 	 *
36196e0279dSAndrey Konovalov 	 * The length of ">ff00ff00ff00ff00: " is
3620b24beccSAndrey Ryabinin 	 *    3 + (BITS_PER_LONG / 8) * 2 chars.
36396e0279dSAndrey Konovalov 	 * The length of each granule metadata is 2 bytes
36496e0279dSAndrey Konovalov 	 *    plus 1 byte for space.
3650b24beccSAndrey Ryabinin 	 */
36696e0279dSAndrey Konovalov 	return 3 + (BITS_PER_LONG / 8) * 2 +
36796e0279dSAndrey Konovalov 		(addr - row) / KASAN_GRANULE_SIZE * 3 + 1;
3680b24beccSAndrey Ryabinin }
3690b24beccSAndrey Ryabinin 
370db3de8f7SAndrey Konovalov static void print_memory_metadata(const void *addr)
3710b24beccSAndrey Ryabinin {
3720b24beccSAndrey Ryabinin 	int i;
37396e0279dSAndrey Konovalov 	void *row;
3740b24beccSAndrey Ryabinin 
37596e0279dSAndrey Konovalov 	row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW)
37696e0279dSAndrey Konovalov 			- META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW;
3770b24beccSAndrey Ryabinin 
3780b24beccSAndrey Ryabinin 	pr_err("Memory state around the buggy address:\n");
3790b24beccSAndrey Ryabinin 
38088b86597SAndrey Konovalov 	for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
3810b24beccSAndrey Ryabinin 		char buffer[4 + (BITS_PER_LONG / 8) * 2];
38296e0279dSAndrey Konovalov 		char metadata[META_BYTES_PER_ROW];
3830b24beccSAndrey Ryabinin 
3840b24beccSAndrey Ryabinin 		snprintf(buffer, sizeof(buffer),
38596e0279dSAndrey Konovalov 				(i == 0) ? ">%px: " : " %px: ", row);
38696e0279dSAndrey Konovalov 
387f2377d4eSAneesh Kumar K.V 		/*
388f2377d4eSAneesh Kumar K.V 		 * We should not pass a shadow pointer to generic
389f2377d4eSAneesh Kumar K.V 		 * function, because generic functions may try to
390f2377d4eSAneesh Kumar K.V 		 * access kasan mapping for the passed address.
391f2377d4eSAneesh Kumar K.V 		 */
392f00748bfSAndrey Konovalov 		kasan_metadata_fetch_row(&metadata[0], row);
39396e0279dSAndrey Konovalov 
3940b24beccSAndrey Ryabinin 		print_hex_dump(KERN_ERR, buffer,
39588b86597SAndrey Konovalov 			DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
39696e0279dSAndrey Konovalov 			metadata, META_BYTES_PER_ROW, 0);
3970b24beccSAndrey Ryabinin 
39896e0279dSAndrey Konovalov 		if (meta_row_is_guilty(row, addr))
39996e0279dSAndrey Konovalov 			pr_err("%*c\n", meta_pointer_offset(row, addr), '^');
4000b24beccSAndrey Ryabinin 
40196e0279dSAndrey Konovalov 		row += META_MEM_BYTES_PER_ROW;
4020b24beccSAndrey Ryabinin 	}
4030b24beccSAndrey Ryabinin }
4040b24beccSAndrey Ryabinin 
405c965cdd6SAndrey Konovalov static void print_report(struct kasan_report_info *info)
4069d7b7dd9SAndrey Konovalov {
4079d7b7dd9SAndrey Konovalov 	void *tagged_addr = info->access_addr;
4089d7b7dd9SAndrey Konovalov 	void *untagged_addr = kasan_reset_tag(tagged_addr);
4099d7b7dd9SAndrey Konovalov 	u8 tag = get_tag(tagged_addr);
4109d7b7dd9SAndrey Konovalov 
4119d7b7dd9SAndrey Konovalov 	print_error_description(info);
4129d7b7dd9SAndrey Konovalov 	if (addr_has_metadata(untagged_addr))
4139d7b7dd9SAndrey Konovalov 		kasan_print_tags(tag, info->first_bad_addr);
4149d7b7dd9SAndrey Konovalov 	pr_err("\n");
4159d7b7dd9SAndrey Konovalov 
4169d7b7dd9SAndrey Konovalov 	if (addr_has_metadata(untagged_addr)) {
4179d7b7dd9SAndrey Konovalov 		print_address_description(untagged_addr, tag);
4189d7b7dd9SAndrey Konovalov 		print_memory_metadata(info->first_bad_addr);
4199d7b7dd9SAndrey Konovalov 	} else {
4209d7b7dd9SAndrey Konovalov 		dump_stack_lvl(KERN_ERR);
4219d7b7dd9SAndrey Konovalov 	}
4229d7b7dd9SAndrey Konovalov }
4239d7b7dd9SAndrey Konovalov 
4243de0de75SKuan-Ying Lee void kasan_report_invalid_free(void *ptr, unsigned long ip, enum kasan_report_type type)
42531c65110SAndrey Konovalov {
42631c65110SAndrey Konovalov 	unsigned long flags;
427c965cdd6SAndrey Konovalov 	struct kasan_report_info info;
42831c65110SAndrey Konovalov 
429c068664cSAndrey Konovalov 	/*
430c068664cSAndrey Konovalov 	 * Do not check report_suppressed(), as an invalid-free cannot be
431c068664cSAndrey Konovalov 	 * caused by accessing slab metadata and thus should not be
432c068664cSAndrey Konovalov 	 * suppressed by kasan_disable/enable_current() critical sections.
433c068664cSAndrey Konovalov 	 */
434c068664cSAndrey Konovalov 	if (unlikely(!report_enabled()))
435c068664cSAndrey Konovalov 		return;
436c068664cSAndrey Konovalov 
43731c65110SAndrey Konovalov 	start_report(&flags, true);
43831c65110SAndrey Konovalov 
4393de0de75SKuan-Ying Lee 	info.type = type;
44031c65110SAndrey Konovalov 	info.access_addr = ptr;
44131c65110SAndrey Konovalov 	info.first_bad_addr = kasan_reset_tag(ptr);
44231c65110SAndrey Konovalov 	info.access_size = 0;
44331c65110SAndrey Konovalov 	info.is_write = false;
44431c65110SAndrey Konovalov 	info.ip = ip;
44531c65110SAndrey Konovalov 
44631c65110SAndrey Konovalov 	print_report(&info);
44731c65110SAndrey Konovalov 
44831c65110SAndrey Konovalov 	end_report(&flags, ptr);
44931c65110SAndrey Konovalov }
45031c65110SAndrey Konovalov 
451795b760fSAndrey Konovalov /*
452795b760fSAndrey Konovalov  * kasan_report() is the only reporting function that uses
453795b760fSAndrey Konovalov  * user_access_save/restore(): kasan_report_invalid_free() cannot be called
454795b760fSAndrey Konovalov  * from a UACCESS region, and kasan_report_async() is not used on x86.
455795b760fSAndrey Konovalov  */
4564fba3758SAndrey Konovalov bool kasan_report(unsigned long addr, size_t size, bool is_write,
4574fba3758SAndrey Konovalov 			unsigned long ip)
4584fba3758SAndrey Konovalov {
459b3bb1d70SAndrey Konovalov 	bool ret = true;
460be8631a1SAndrey Konovalov 	void *ptr = (void *)addr;
461be8631a1SAndrey Konovalov 	unsigned long ua_flags = user_access_save();
462be8631a1SAndrey Konovalov 	unsigned long irq_flags;
463c965cdd6SAndrey Konovalov 	struct kasan_report_info info;
4644fba3758SAndrey Konovalov 
465c068664cSAndrey Konovalov 	if (unlikely(report_suppressed()) || unlikely(!report_enabled())) {
466b3bb1d70SAndrey Konovalov 		ret = false;
467b3bb1d70SAndrey Konovalov 		goto out;
4684fba3758SAndrey Konovalov 	}
4694fba3758SAndrey Konovalov 
470be8631a1SAndrey Konovalov 	start_report(&irq_flags, true);
471be8631a1SAndrey Konovalov 
47231c65110SAndrey Konovalov 	info.type = KASAN_REPORT_ACCESS;
473be8631a1SAndrey Konovalov 	info.access_addr = ptr;
474be8631a1SAndrey Konovalov 	info.first_bad_addr = kasan_find_first_bad_addr(ptr, size);
475be8631a1SAndrey Konovalov 	info.access_size = size;
476be8631a1SAndrey Konovalov 	info.is_write = is_write;
477be8631a1SAndrey Konovalov 	info.ip = ip;
478be8631a1SAndrey Konovalov 
479be8631a1SAndrey Konovalov 	print_report(&info);
480be8631a1SAndrey Konovalov 
481be8631a1SAndrey Konovalov 	end_report(&irq_flags, ptr);
482b3bb1d70SAndrey Konovalov 
483b3bb1d70SAndrey Konovalov out:
484b3bb1d70SAndrey Konovalov 	user_access_restore(ua_flags);
4854fba3758SAndrey Konovalov 
4864fba3758SAndrey Konovalov 	return ret;
4874fba3758SAndrey Konovalov }
4884fba3758SAndrey Konovalov 
489bb2f967cSAndrey Konovalov #ifdef CONFIG_KASAN_HW_TAGS
490bb2f967cSAndrey Konovalov void kasan_report_async(void)
491bb2f967cSAndrey Konovalov {
492bb2f967cSAndrey Konovalov 	unsigned long flags;
493bb2f967cSAndrey Konovalov 
494c068664cSAndrey Konovalov 	/*
495c068664cSAndrey Konovalov 	 * Do not check report_suppressed(), as kasan_disable/enable_current()
496c068664cSAndrey Konovalov 	 * critical sections do not affect Hardware Tag-Based KASAN.
497c068664cSAndrey Konovalov 	 */
498c068664cSAndrey Konovalov 	if (unlikely(!report_enabled()))
499c068664cSAndrey Konovalov 		return;
500c068664cSAndrey Konovalov 
501bb2f967cSAndrey Konovalov 	start_report(&flags, false);
502bb2f967cSAndrey Konovalov 	pr_err("BUG: KASAN: invalid-access\n");
503bb2f967cSAndrey Konovalov 	pr_err("Asynchronous fault: no details available\n");
504bb2f967cSAndrey Konovalov 	pr_err("\n");
505bb2f967cSAndrey Konovalov 	dump_stack_lvl(KERN_ERR);
506bb2f967cSAndrey Konovalov 	end_report(&flags, NULL);
507bb2f967cSAndrey Konovalov }
508bb2f967cSAndrey Konovalov #endif /* CONFIG_KASAN_HW_TAGS */
509bb2f967cSAndrey Konovalov 
5102f004eeaSJann Horn #ifdef CONFIG_KASAN_INLINE
5112f004eeaSJann Horn /*
5122f004eeaSJann Horn  * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
5132f004eeaSJann Horn  * canonical half of the address space) cause out-of-bounds shadow memory reads
5142f004eeaSJann Horn  * before the actual access. For addresses in the low canonical half of the
5152f004eeaSJann Horn  * address space, as well as most non-canonical addresses, that out-of-bounds
5162f004eeaSJann Horn  * shadow memory access lands in the non-canonical part of the address space.
5172f004eeaSJann Horn  * Help the user figure out what the original bogus pointer was.
5182f004eeaSJann Horn  */
5192f004eeaSJann Horn void kasan_non_canonical_hook(unsigned long addr)
5202f004eeaSJann Horn {
5212f004eeaSJann Horn 	unsigned long orig_addr;
5222f004eeaSJann Horn 	const char *bug_type;
5232f004eeaSJann Horn 
5242f004eeaSJann Horn 	if (addr < KASAN_SHADOW_OFFSET)
5252f004eeaSJann Horn 		return;
5262f004eeaSJann Horn 
5272f004eeaSJann Horn 	orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
5282f004eeaSJann Horn 	/*
5292f004eeaSJann Horn 	 * For faults near the shadow address for NULL, we can be fairly certain
5302f004eeaSJann Horn 	 * that this is a KASAN shadow memory access.
5312f004eeaSJann Horn 	 * For faults that correspond to shadow for low canonical addresses, we
5322f004eeaSJann Horn 	 * can still be pretty sure - that shadow region is a fairly narrow
5332f004eeaSJann Horn 	 * chunk of the non-canonical address space.
5342f004eeaSJann Horn 	 * But faults that look like shadow for non-canonical addresses are a
5352f004eeaSJann Horn 	 * really large chunk of the address space. In that case, we still
5362f004eeaSJann Horn 	 * print the decoded address, but make it clear that this is not
5372f004eeaSJann Horn 	 * necessarily what's actually going on.
5382f004eeaSJann Horn 	 */
5392f004eeaSJann Horn 	if (orig_addr < PAGE_SIZE)
5402f004eeaSJann Horn 		bug_type = "null-ptr-deref";
5412f004eeaSJann Horn 	else if (orig_addr < TASK_SIZE)
5422f004eeaSJann Horn 		bug_type = "probably user-memory-access";
5432f004eeaSJann Horn 	else
5442f004eeaSJann Horn 		bug_type = "maybe wild-memory-access";
5452f004eeaSJann Horn 	pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
5461f600626SAndrey Konovalov 		 orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1);
5472f004eeaSJann Horn }
5482f004eeaSJann Horn #endif
549