xref: /openbmc/linux/mm/kasan/report.c (revision a260d2814e6d294e878840d9c6fdeca516205096)
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>
160b24beccSAndrey Ryabinin #include <linux/mm.h>
170b24beccSAndrey Ryabinin #include <linux/printk.h>
180b24beccSAndrey Ryabinin #include <linux/sched.h>
190b24beccSAndrey Ryabinin #include <linux/slab.h>
20cd11016eSAlexander Potapenko #include <linux/stackdepot.h>
210b24beccSAndrey Ryabinin #include <linux/stacktrace.h>
220b24beccSAndrey Ryabinin #include <linux/string.h>
230b24beccSAndrey Ryabinin #include <linux/types.h>
240b24beccSAndrey Ryabinin #include <linux/kasan.h>
25527f215bSAneesh Kumar K.V #include <linux/module.h>
26e8969219SMarco Elver #include <linux/sched/task_stack.h>
274fba3758SAndrey Konovalov #include <linux/uaccess.h>
28d3a61f74SAlexander Potapenko #include <trace/events/error_report.h>
290b24beccSAndrey Ryabinin 
30bebf56a1SAndrey Ryabinin #include <asm/sections.h>
31bebf56a1SAndrey Ryabinin 
3283c4e7a0SPatricia Alfonso #include <kunit/test.h>
3383c4e7a0SPatricia Alfonso 
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,
46c9d1af2bSWoody Lin };
47c9d1af2bSWoody Lin 
48c9d1af2bSWoody Lin static enum kasan_arg_fault kasan_arg_fault __ro_after_init = KASAN_ARG_FAULT_DEFAULT;
49c9d1af2bSWoody Lin 
50c9d1af2bSWoody Lin /* kasan.fault=report/panic */
51c9d1af2bSWoody Lin static int __init early_kasan_fault(char *arg)
52c9d1af2bSWoody Lin {
53c9d1af2bSWoody Lin 	if (!arg)
54c9d1af2bSWoody Lin 		return -EINVAL;
55c9d1af2bSWoody Lin 
56c9d1af2bSWoody Lin 	if (!strcmp(arg, "report"))
57c9d1af2bSWoody Lin 		kasan_arg_fault = KASAN_ARG_FAULT_REPORT;
58c9d1af2bSWoody Lin 	else if (!strcmp(arg, "panic"))
59c9d1af2bSWoody Lin 		kasan_arg_fault = KASAN_ARG_FAULT_PANIC;
60c9d1af2bSWoody Lin 	else
61c9d1af2bSWoody Lin 		return -EINVAL;
62c9d1af2bSWoody Lin 
63c9d1af2bSWoody Lin 	return 0;
64c9d1af2bSWoody Lin }
65c9d1af2bSWoody Lin early_param("kasan.fault", early_kasan_fault);
66c9d1af2bSWoody Lin 
6711cd3cd6SAndrey Konovalov bool kasan_save_enable_multi_shot(void)
680b24beccSAndrey Ryabinin {
6911cd3cd6SAndrey Konovalov 	return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
700b24beccSAndrey Ryabinin }
7111cd3cd6SAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
720b24beccSAndrey Ryabinin 
7311cd3cd6SAndrey Konovalov void kasan_restore_multi_shot(bool enabled)
745e82cd12SAndrey Konovalov {
7511cd3cd6SAndrey Konovalov 	if (!enabled)
7611cd3cd6SAndrey Konovalov 		clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
775e82cd12SAndrey Konovalov }
7811cd3cd6SAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
795e82cd12SAndrey Konovalov 
8011cd3cd6SAndrey Konovalov static int __init kasan_set_multi_shot(char *str)
810b24beccSAndrey Ryabinin {
8211cd3cd6SAndrey Konovalov 	set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
8311cd3cd6SAndrey Konovalov 	return 1;
840b24beccSAndrey Ryabinin }
8511cd3cd6SAndrey Konovalov __setup("kasan_multi_shot", kasan_set_multi_shot);
860b24beccSAndrey Ryabinin 
87121e8f81SAndrey Konovalov static void print_error_description(struct kasan_access_info *info)
885e82cd12SAndrey Konovalov {
897f0a84c2SAndrey Konovalov 	pr_err("BUG: KASAN: %s in %pS\n",
90f00748bfSAndrey Konovalov 		kasan_get_bug_type(info), (void *)info->ip);
914291e9eeSAndrey Konovalov 	if (info->access_size)
926424f6bbSTobin C. Harding 		pr_err("%s of size %zu at addr %px by task %s/%d\n",
937d418f7bSAndrey Konovalov 			info->is_write ? "Write" : "Read", info->access_size,
947f0a84c2SAndrey Konovalov 			info->access_addr, current->comm, task_pid_nr(current));
954291e9eeSAndrey Konovalov 	else
964291e9eeSAndrey Konovalov 		pr_err("%s at addr %px by task %s/%d\n",
974291e9eeSAndrey Konovalov 			info->is_write ? "Write" : "Read",
984291e9eeSAndrey Konovalov 			info->access_addr, current->comm, task_pid_nr(current));
990b24beccSAndrey Ryabinin }
1000b24beccSAndrey Ryabinin 
101*a260d281SAndrey Konovalov #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST)
102*a260d281SAndrey Konovalov static void update_kunit_status(bool sync)
103*a260d281SAndrey Konovalov {
104*a260d281SAndrey Konovalov 	struct kunit *test;
105*a260d281SAndrey Konovalov 	struct kunit_resource *resource;
106*a260d281SAndrey Konovalov 	struct kunit_kasan_status *status;
107*a260d281SAndrey Konovalov 
108*a260d281SAndrey Konovalov 	test = current->kunit_test;
109*a260d281SAndrey Konovalov 	if (!test)
110*a260d281SAndrey Konovalov 		return;
111*a260d281SAndrey Konovalov 
112*a260d281SAndrey Konovalov 	resource = kunit_find_named_resource(test, "kasan_status");
113*a260d281SAndrey Konovalov 	if (!resource) {
114*a260d281SAndrey Konovalov 		kunit_set_failure(test);
115*a260d281SAndrey Konovalov 		return;
116*a260d281SAndrey Konovalov 	}
117*a260d281SAndrey Konovalov 
118*a260d281SAndrey Konovalov 	status = (struct kunit_kasan_status *)resource->data;
119*a260d281SAndrey Konovalov 	WRITE_ONCE(status->report_found, true);
120*a260d281SAndrey Konovalov 	WRITE_ONCE(status->sync_fault, sync);
121*a260d281SAndrey Konovalov 
122*a260d281SAndrey Konovalov 	kunit_put_resource(resource);
123*a260d281SAndrey Konovalov }
124*a260d281SAndrey Konovalov #else
125*a260d281SAndrey Konovalov static void update_kunit_status(bool sync) { }
126*a260d281SAndrey Konovalov #endif
127*a260d281SAndrey Konovalov 
1287e088978SAndrey Ryabinin static DEFINE_SPINLOCK(report_lock);
1297e088978SAndrey Ryabinin 
130*a260d281SAndrey Konovalov static void start_report(unsigned long *flags, bool sync)
1317e088978SAndrey Ryabinin {
132*a260d281SAndrey Konovalov 	/* Update status of the currently running KASAN test. */
133*a260d281SAndrey Konovalov 	update_kunit_status(sync);
134*a260d281SAndrey Konovalov 	/* Make sure we don't end up in loop. */
1357e088978SAndrey Ryabinin 	kasan_disable_current();
1367e088978SAndrey Ryabinin 	spin_lock_irqsave(&report_lock, *flags);
1377e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
1387e088978SAndrey Ryabinin }
1397e088978SAndrey Ryabinin 
140d3a61f74SAlexander Potapenko static void end_report(unsigned long *flags, unsigned long addr)
1417e088978SAndrey Ryabinin {
142476b1dc2SAndrey Konovalov 	if (addr)
143d3a61f74SAlexander Potapenko 		trace_error_report_end(ERROR_DETECTOR_KASAN, addr);
1447e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
1457e088978SAndrey Ryabinin 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
1467e088978SAndrey Ryabinin 	spin_unlock_irqrestore(&report_lock, *flags);
147e7ce7500STiezhu Yang 	if (panic_on_warn && !test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
1485c5c1f36SDmitry Vyukov 		panic("panic_on_warn set ...\n");
149c9d1af2bSWoody Lin 	if (kasan_arg_fault == KASAN_ARG_FAULT_PANIC)
1508028caacSAndrey Konovalov 		panic("kasan.fault=panic set ...\n");
1517e088978SAndrey Ryabinin 	kasan_enable_current();
1527e088978SAndrey Ryabinin }
1537e088978SAndrey Ryabinin 
154b6b72f49SAndrey Konovalov static void print_track(struct kasan_track *track, const char *prefix)
1557ed2f9e6SAlexander Potapenko {
156b6b72f49SAndrey Konovalov 	pr_err("%s by task %u:\n", prefix, track->pid);
157cd11016eSAlexander Potapenko 	if (track->stack) {
158505be481SImran Khan 		stack_depot_print(track->stack);
159cd11016eSAlexander Potapenko 	} else {
160cd11016eSAlexander Potapenko 		pr_err("(stack is not available)\n");
161cd11016eSAlexander Potapenko 	}
1627ed2f9e6SAlexander Potapenko }
1637ed2f9e6SAlexander Potapenko 
164ae8f06b3SWalter Wu struct page *kasan_addr_to_page(const void *addr)
165db429f16SAndrey Konovalov {
166db429f16SAndrey Konovalov 	if ((addr >= (void *)PAGE_OFFSET) &&
167db429f16SAndrey Konovalov 			(addr < high_memory))
168db429f16SAndrey Konovalov 		return virt_to_head_page(addr);
169db429f16SAndrey Konovalov 	return NULL;
170db429f16SAndrey Konovalov }
171db429f16SAndrey Konovalov 
1726e48a966SMatthew Wilcox (Oracle) struct slab *kasan_addr_to_slab(const void *addr)
1736e48a966SMatthew Wilcox (Oracle) {
1746e48a966SMatthew Wilcox (Oracle) 	if ((addr >= (void *)PAGE_OFFSET) &&
1756e48a966SMatthew Wilcox (Oracle) 			(addr < high_memory))
1766e48a966SMatthew Wilcox (Oracle) 		return virt_to_slab(addr);
1776e48a966SMatthew Wilcox (Oracle) 	return NULL;
1786e48a966SMatthew Wilcox (Oracle) }
1796e48a966SMatthew Wilcox (Oracle) 
1800c06f1f8SAndrey Konovalov static void describe_object_addr(struct kmem_cache *cache, void *object,
1810c06f1f8SAndrey Konovalov 				const void *addr)
1820c06f1f8SAndrey Konovalov {
1830c06f1f8SAndrey Konovalov 	unsigned long access_addr = (unsigned long)addr;
1840c06f1f8SAndrey Konovalov 	unsigned long object_addr = (unsigned long)object;
1850c06f1f8SAndrey Konovalov 	const char *rel_type;
1860c06f1f8SAndrey Konovalov 	int rel_bytes;
1870c06f1f8SAndrey Konovalov 
1886424f6bbSTobin C. Harding 	pr_err("The buggy address belongs to the object at %px\n"
1890c06f1f8SAndrey Konovalov 	       " which belongs to the cache %s of size %d\n",
1900c06f1f8SAndrey Konovalov 		object, cache->name, cache->object_size);
1910c06f1f8SAndrey Konovalov 
1920c06f1f8SAndrey Konovalov 	if (access_addr < object_addr) {
1930c06f1f8SAndrey Konovalov 		rel_type = "to the left";
1940c06f1f8SAndrey Konovalov 		rel_bytes = object_addr - access_addr;
1950c06f1f8SAndrey Konovalov 	} else if (access_addr >= object_addr + cache->object_size) {
1960c06f1f8SAndrey Konovalov 		rel_type = "to the right";
1970c06f1f8SAndrey Konovalov 		rel_bytes = access_addr - (object_addr + cache->object_size);
1980c06f1f8SAndrey Konovalov 	} else {
1990c06f1f8SAndrey Konovalov 		rel_type = "inside";
2000c06f1f8SAndrey Konovalov 		rel_bytes = access_addr - object_addr;
2010c06f1f8SAndrey Konovalov 	}
2020c06f1f8SAndrey Konovalov 
2030c06f1f8SAndrey Konovalov 	pr_err("The buggy address is located %d bytes %s of\n"
2046424f6bbSTobin C. Harding 	       " %d-byte region [%px, %px)\n",
2050c06f1f8SAndrey Konovalov 		rel_bytes, rel_type, cache->object_size, (void *)object_addr,
2060c06f1f8SAndrey Konovalov 		(void *)(object_addr + cache->object_size));
2070c06f1f8SAndrey Konovalov }
2080c06f1f8SAndrey Konovalov 
2098028caacSAndrey Konovalov static void describe_object_stacks(struct kmem_cache *cache, void *object,
210ae8f06b3SWalter Wu 					const void *addr, u8 tag)
2117ed2f9e6SAlexander Potapenko {
21297593cadSAndrey Konovalov 	struct kasan_alloc_meta *alloc_meta;
213ae8f06b3SWalter Wu 	struct kasan_track *free_track;
214ae8f06b3SWalter Wu 
21597593cadSAndrey Konovalov 	alloc_meta = kasan_get_alloc_meta(cache, object);
21697593cadSAndrey Konovalov 	if (alloc_meta) {
2176476792fSAndrey Konovalov 		print_track(&alloc_meta->alloc_track, "Allocated");
218b1938599SAndrey Konovalov 		pr_err("\n");
21997593cadSAndrey Konovalov 	}
22097593cadSAndrey Konovalov 
221ae8f06b3SWalter Wu 	free_track = kasan_get_free_track(cache, object, tag);
222e4b7818bSWalter Wu 	if (free_track) {
223ae8f06b3SWalter Wu 		print_track(free_track, "Freed");
224b1938599SAndrey Konovalov 		pr_err("\n");
225e4b7818bSWalter Wu 	}
22626e760c9SWalter Wu 
22726e760c9SWalter Wu #ifdef CONFIG_KASAN_GENERIC
22897593cadSAndrey Konovalov 	if (!alloc_meta)
22997593cadSAndrey Konovalov 		return;
2306476792fSAndrey Konovalov 	if (alloc_meta->aux_stack[0]) {
231ef133461SWalter Wu 		pr_err("Last potentially related work creation:\n");
232505be481SImran Khan 		stack_depot_print(alloc_meta->aux_stack[0]);
23326e760c9SWalter Wu 		pr_err("\n");
23426e760c9SWalter Wu 	}
2356476792fSAndrey Konovalov 	if (alloc_meta->aux_stack[1]) {
236ef133461SWalter Wu 		pr_err("Second to last potentially related work creation:\n");
237505be481SImran Khan 		stack_depot_print(alloc_meta->aux_stack[1]);
23826e760c9SWalter Wu 		pr_err("\n");
23926e760c9SWalter Wu 	}
24026e760c9SWalter Wu #endif
2417ed2f9e6SAlexander Potapenko }
2427ed2f9e6SAlexander Potapenko 
2438028caacSAndrey Konovalov static void describe_object(struct kmem_cache *cache, void *object,
2448028caacSAndrey Konovalov 				const void *addr, u8 tag)
2458028caacSAndrey Konovalov {
2468028caacSAndrey Konovalov 	if (kasan_stack_collection_enabled())
2478028caacSAndrey Konovalov 		describe_object_stacks(cache, object, addr, tag);
2480c06f1f8SAndrey Konovalov 	describe_object_addr(cache, object, addr);
2490c06f1f8SAndrey Konovalov }
2500c06f1f8SAndrey Konovalov 
25111cd3cd6SAndrey Konovalov static inline bool kernel_or_module_addr(const void *addr)
25211cd3cd6SAndrey Konovalov {
2533298cbe8SKefeng Wang 	if (is_kernel((unsigned long)addr))
25411cd3cd6SAndrey Konovalov 		return true;
25511cd3cd6SAndrey Konovalov 	if (is_module_address((unsigned long)addr))
25611cd3cd6SAndrey Konovalov 		return true;
25711cd3cd6SAndrey Konovalov 	return false;
25811cd3cd6SAndrey Konovalov }
25911cd3cd6SAndrey Konovalov 
26011cd3cd6SAndrey Konovalov static inline bool init_task_stack_addr(const void *addr)
26111cd3cd6SAndrey Konovalov {
26211cd3cd6SAndrey Konovalov 	return addr >= (void *)&init_thread_union.stack &&
26311cd3cd6SAndrey Konovalov 		(addr <= (void *)&init_thread_union.stack +
26411cd3cd6SAndrey Konovalov 			sizeof(init_thread_union.stack));
26511cd3cd6SAndrey Konovalov }
26611cd3cd6SAndrey Konovalov 
267ae8f06b3SWalter Wu static void print_address_description(void *addr, u8 tag)
2687e088978SAndrey Ryabinin {
269ae8f06b3SWalter Wu 	struct page *page = kasan_addr_to_page(addr);
270b8c73fc2SAndrey Ryabinin 
271336abff6SAlexander Potapenko 	dump_stack_lvl(KERN_ERR);
272b1938599SAndrey Konovalov 	pr_err("\n");
273db429f16SAndrey Konovalov 
274db429f16SAndrey Konovalov 	if (page && PageSlab(page)) {
2756e48a966SMatthew Wilcox (Oracle) 		struct slab *slab = page_slab(page);
2766e48a966SMatthew Wilcox (Oracle) 		struct kmem_cache *cache = slab->slab_cache;
2776e48a966SMatthew Wilcox (Oracle) 		void *object = nearest_obj(cache, slab,	addr);
278db429f16SAndrey Konovalov 
279ae8f06b3SWalter Wu 		describe_object(cache, object, addr, tag);
280038fd2b4SAndrey Konovalov 		pr_err("\n");
281b8c73fc2SAndrey Ryabinin 	}
282b8c73fc2SAndrey Ryabinin 
283430a05f9SAndrey Konovalov 	if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
284430a05f9SAndrey Konovalov 		pr_err("The buggy address belongs to the variable:\n");
285430a05f9SAndrey Konovalov 		pr_err(" %pS\n", addr);
286038fd2b4SAndrey Konovalov 		pr_err("\n");
287430a05f9SAndrey Konovalov 	}
288430a05f9SAndrey Konovalov 
2890f9b35f3SAndrey Konovalov 	if (object_is_on_stack(addr)) {
2900f9b35f3SAndrey Konovalov 		/*
2910f9b35f3SAndrey Konovalov 		 * Currently, KASAN supports printing frame information only
2920f9b35f3SAndrey Konovalov 		 * for accesses to the task's own stack.
2930f9b35f3SAndrey Konovalov 		 */
2940f9b35f3SAndrey Konovalov 		kasan_print_address_stack_frame(addr);
2950f9b35f3SAndrey Konovalov 		pr_err("\n");
2960f9b35f3SAndrey Konovalov 	}
2970f9b35f3SAndrey Konovalov 
298c056a364SAndrey Konovalov 	if (is_vmalloc_addr(addr)) {
299c056a364SAndrey Konovalov 		struct vm_struct *va = find_vm_area(addr);
300c056a364SAndrey Konovalov 
301c056a364SAndrey Konovalov 		if (va) {
302c056a364SAndrey Konovalov 			pr_err("The buggy address belongs to the virtual mapping at\n"
303c056a364SAndrey Konovalov 			       " [%px, %px) created by:\n"
304c056a364SAndrey Konovalov 			       " %pS\n",
305c056a364SAndrey Konovalov 			       va->addr, va->addr + va->size, va->caller);
306038fd2b4SAndrey Konovalov 			pr_err("\n");
307c056a364SAndrey Konovalov 
308c056a364SAndrey Konovalov 			page = vmalloc_to_page(page);
309c056a364SAndrey Konovalov 		}
310c056a364SAndrey Konovalov 	}
311c056a364SAndrey Konovalov 
312430a05f9SAndrey Konovalov 	if (page) {
313c056a364SAndrey Konovalov 		pr_err("The buggy address belongs to the physical page:\n");
314430a05f9SAndrey Konovalov 		dump_page(page, "kasan: bad access detected");
315038fd2b4SAndrey Konovalov 		pr_err("\n");
316bebf56a1SAndrey Ryabinin 	}
3170b24beccSAndrey Ryabinin }
3180b24beccSAndrey Ryabinin 
31996e0279dSAndrey Konovalov static bool meta_row_is_guilty(const void *row, const void *addr)
3200b24beccSAndrey Ryabinin {
32196e0279dSAndrey Konovalov 	return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW);
3220b24beccSAndrey Ryabinin }
3230b24beccSAndrey Ryabinin 
32496e0279dSAndrey Konovalov static int meta_pointer_offset(const void *row, const void *addr)
3250b24beccSAndrey Ryabinin {
32696e0279dSAndrey Konovalov 	/*
32796e0279dSAndrey Konovalov 	 * Memory state around the buggy address:
32896e0279dSAndrey Konovalov 	 *  ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe
32996e0279dSAndrey Konovalov 	 *  ...
33096e0279dSAndrey Konovalov 	 *
33196e0279dSAndrey Konovalov 	 * The length of ">ff00ff00ff00ff00: " is
3320b24beccSAndrey Ryabinin 	 *    3 + (BITS_PER_LONG / 8) * 2 chars.
33396e0279dSAndrey Konovalov 	 * The length of each granule metadata is 2 bytes
33496e0279dSAndrey Konovalov 	 *    plus 1 byte for space.
3350b24beccSAndrey Ryabinin 	 */
33696e0279dSAndrey Konovalov 	return 3 + (BITS_PER_LONG / 8) * 2 +
33796e0279dSAndrey Konovalov 		(addr - row) / KASAN_GRANULE_SIZE * 3 + 1;
3380b24beccSAndrey Ryabinin }
3390b24beccSAndrey Ryabinin 
340db3de8f7SAndrey Konovalov static void print_memory_metadata(const void *addr)
3410b24beccSAndrey Ryabinin {
3420b24beccSAndrey Ryabinin 	int i;
34396e0279dSAndrey Konovalov 	void *row;
3440b24beccSAndrey Ryabinin 
34596e0279dSAndrey Konovalov 	row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW)
34696e0279dSAndrey Konovalov 			- META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW;
3470b24beccSAndrey Ryabinin 
3480b24beccSAndrey Ryabinin 	pr_err("Memory state around the buggy address:\n");
3490b24beccSAndrey Ryabinin 
35088b86597SAndrey Konovalov 	for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
3510b24beccSAndrey Ryabinin 		char buffer[4 + (BITS_PER_LONG / 8) * 2];
35296e0279dSAndrey Konovalov 		char metadata[META_BYTES_PER_ROW];
3530b24beccSAndrey Ryabinin 
3540b24beccSAndrey Ryabinin 		snprintf(buffer, sizeof(buffer),
35596e0279dSAndrey Konovalov 				(i == 0) ? ">%px: " : " %px: ", row);
35696e0279dSAndrey Konovalov 
357f2377d4eSAneesh Kumar K.V 		/*
358f2377d4eSAneesh Kumar K.V 		 * We should not pass a shadow pointer to generic
359f2377d4eSAneesh Kumar K.V 		 * function, because generic functions may try to
360f2377d4eSAneesh Kumar K.V 		 * access kasan mapping for the passed address.
361f2377d4eSAneesh Kumar K.V 		 */
362f00748bfSAndrey Konovalov 		kasan_metadata_fetch_row(&metadata[0], row);
36396e0279dSAndrey Konovalov 
3640b24beccSAndrey Ryabinin 		print_hex_dump(KERN_ERR, buffer,
36588b86597SAndrey Konovalov 			DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
36696e0279dSAndrey Konovalov 			metadata, META_BYTES_PER_ROW, 0);
3670b24beccSAndrey Ryabinin 
36896e0279dSAndrey Konovalov 		if (meta_row_is_guilty(row, addr))
36996e0279dSAndrey Konovalov 			pr_err("%*c\n", meta_pointer_offset(row, addr), '^');
3700b24beccSAndrey Ryabinin 
37196e0279dSAndrey Konovalov 		row += META_MEM_BYTES_PER_ROW;
3720b24beccSAndrey Ryabinin 	}
3730b24beccSAndrey Ryabinin }
3740b24beccSAndrey Ryabinin 
3754fba3758SAndrey Konovalov static bool report_enabled(void)
376b0845ce5SMark Rutland {
377d73b4936SAndrey Konovalov #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
378b0845ce5SMark Rutland 	if (current->kasan_depth)
379b0845ce5SMark Rutland 		return false;
380d73b4936SAndrey Konovalov #endif
381b0845ce5SMark Rutland 	if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
382b0845ce5SMark Rutland 		return true;
383b0845ce5SMark Rutland 	return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
384b0845ce5SMark Rutland }
385b0845ce5SMark Rutland 
38611cd3cd6SAndrey Konovalov void kasan_report_invalid_free(void *object, unsigned long ip)
38711cd3cd6SAndrey Konovalov {
38811cd3cd6SAndrey Konovalov 	unsigned long flags;
389ae8f06b3SWalter Wu 	u8 tag = get_tag(object);
39011cd3cd6SAndrey Konovalov 
391c0054c56SAndrey Konovalov 	object = kasan_reset_tag(object);
39283c4e7a0SPatricia Alfonso 
393*a260d281SAndrey Konovalov 	start_report(&flags, true);
39411cd3cd6SAndrey Konovalov 	pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
395f00748bfSAndrey Konovalov 	kasan_print_tags(tag, object);
39611cd3cd6SAndrey Konovalov 	pr_err("\n");
397ae8f06b3SWalter Wu 	print_address_description(object, tag);
398db3de8f7SAndrey Konovalov 	print_memory_metadata(object);
399d3a61f74SAlexander Potapenko 	end_report(&flags, (unsigned long)object);
40011cd3cd6SAndrey Konovalov }
40111cd3cd6SAndrey Konovalov 
4028f7b5054SVincenzo Frascino #ifdef CONFIG_KASAN_HW_TAGS
4038f7b5054SVincenzo Frascino void kasan_report_async(void)
4048f7b5054SVincenzo Frascino {
4058f7b5054SVincenzo Frascino 	unsigned long flags;
4068f7b5054SVincenzo Frascino 
407*a260d281SAndrey Konovalov 	start_report(&flags, false);
4088f7b5054SVincenzo Frascino 	pr_err("BUG: KASAN: invalid-access\n");
4098f7b5054SVincenzo Frascino 	pr_err("Asynchronous mode enabled: no access details available\n");
4108f7b5054SVincenzo Frascino 	pr_err("\n");
411336abff6SAlexander Potapenko 	dump_stack_lvl(KERN_ERR);
4128f7b5054SVincenzo Frascino 	end_report(&flags, 0);
4138f7b5054SVincenzo Frascino }
4148f7b5054SVincenzo Frascino #endif /* CONFIG_KASAN_HW_TAGS */
4158f7b5054SVincenzo Frascino 
4164fba3758SAndrey Konovalov static void __kasan_report(unsigned long addr, size_t size, bool is_write,
4174fba3758SAndrey Konovalov 				unsigned long ip)
4180b24beccSAndrey Ryabinin {
4190b24beccSAndrey Ryabinin 	struct kasan_access_info info;
420121e8f81SAndrey Konovalov 	void *tagged_addr;
421121e8f81SAndrey Konovalov 	void *untagged_addr;
422121e8f81SAndrey Konovalov 	unsigned long flags;
4230b24beccSAndrey Ryabinin 
4244f40c6e5SPeter Zijlstra 	disable_trace_on_warning();
425*a260d281SAndrey Konovalov 	start_report(&flags, true);
4264f40c6e5SPeter Zijlstra 
427121e8f81SAndrey Konovalov 	tagged_addr = (void *)addr;
428c0054c56SAndrey Konovalov 	untagged_addr = kasan_reset_tag(tagged_addr);
429121e8f81SAndrey Konovalov 
430121e8f81SAndrey Konovalov 	info.access_addr = tagged_addr;
4316882464fSAndrey Konovalov 	if (addr_has_metadata(untagged_addr))
432f00748bfSAndrey Konovalov 		info.first_bad_addr =
433f00748bfSAndrey Konovalov 			kasan_find_first_bad_addr(tagged_addr, size);
434121e8f81SAndrey Konovalov 	else
435121e8f81SAndrey Konovalov 		info.first_bad_addr = untagged_addr;
4360b24beccSAndrey Ryabinin 	info.access_size = size;
4370b24beccSAndrey Ryabinin 	info.is_write = is_write;
4380b24beccSAndrey Ryabinin 	info.ip = ip;
439e9121076SAndrey Konovalov 
440121e8f81SAndrey Konovalov 	print_error_description(&info);
4416882464fSAndrey Konovalov 	if (addr_has_metadata(untagged_addr))
442f00748bfSAndrey Konovalov 		kasan_print_tags(get_tag(tagged_addr), info.first_bad_addr);
443121e8f81SAndrey Konovalov 	pr_err("\n");
444121e8f81SAndrey Konovalov 
4456882464fSAndrey Konovalov 	if (addr_has_metadata(untagged_addr)) {
446ae8f06b3SWalter Wu 		print_address_description(untagged_addr, get_tag(tagged_addr));
447db3de8f7SAndrey Konovalov 		print_memory_metadata(info.first_bad_addr);
448121e8f81SAndrey Konovalov 	} else {
449336abff6SAlexander Potapenko 		dump_stack_lvl(KERN_ERR);
450121e8f81SAndrey Konovalov 	}
451121e8f81SAndrey Konovalov 
452d3a61f74SAlexander Potapenko 	end_report(&flags, addr);
4530b24beccSAndrey Ryabinin }
4542f004eeaSJann Horn 
4554fba3758SAndrey Konovalov bool kasan_report(unsigned long addr, size_t size, bool is_write,
4564fba3758SAndrey Konovalov 			unsigned long ip)
4574fba3758SAndrey Konovalov {
4584fba3758SAndrey Konovalov 	unsigned long flags = user_access_save();
4594fba3758SAndrey Konovalov 	bool ret = false;
4604fba3758SAndrey Konovalov 
4614fba3758SAndrey Konovalov 	if (likely(report_enabled())) {
4624fba3758SAndrey Konovalov 		__kasan_report(addr, size, is_write, ip);
4634fba3758SAndrey Konovalov 		ret = true;
4644fba3758SAndrey Konovalov 	}
4654fba3758SAndrey Konovalov 
4664fba3758SAndrey Konovalov 	user_access_restore(flags);
4674fba3758SAndrey Konovalov 
4684fba3758SAndrey Konovalov 	return ret;
4694fba3758SAndrey Konovalov }
4704fba3758SAndrey Konovalov 
4712f004eeaSJann Horn #ifdef CONFIG_KASAN_INLINE
4722f004eeaSJann Horn /*
4732f004eeaSJann Horn  * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
4742f004eeaSJann Horn  * canonical half of the address space) cause out-of-bounds shadow memory reads
4752f004eeaSJann Horn  * before the actual access. For addresses in the low canonical half of the
4762f004eeaSJann Horn  * address space, as well as most non-canonical addresses, that out-of-bounds
4772f004eeaSJann Horn  * shadow memory access lands in the non-canonical part of the address space.
4782f004eeaSJann Horn  * Help the user figure out what the original bogus pointer was.
4792f004eeaSJann Horn  */
4802f004eeaSJann Horn void kasan_non_canonical_hook(unsigned long addr)
4812f004eeaSJann Horn {
4822f004eeaSJann Horn 	unsigned long orig_addr;
4832f004eeaSJann Horn 	const char *bug_type;
4842f004eeaSJann Horn 
4852f004eeaSJann Horn 	if (addr < KASAN_SHADOW_OFFSET)
4862f004eeaSJann Horn 		return;
4872f004eeaSJann Horn 
4882f004eeaSJann Horn 	orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
4892f004eeaSJann Horn 	/*
4902f004eeaSJann Horn 	 * For faults near the shadow address for NULL, we can be fairly certain
4912f004eeaSJann Horn 	 * that this is a KASAN shadow memory access.
4922f004eeaSJann Horn 	 * For faults that correspond to shadow for low canonical addresses, we
4932f004eeaSJann Horn 	 * can still be pretty sure - that shadow region is a fairly narrow
4942f004eeaSJann Horn 	 * chunk of the non-canonical address space.
4952f004eeaSJann Horn 	 * But faults that look like shadow for non-canonical addresses are a
4962f004eeaSJann Horn 	 * really large chunk of the address space. In that case, we still
4972f004eeaSJann Horn 	 * print the decoded address, but make it clear that this is not
4982f004eeaSJann Horn 	 * necessarily what's actually going on.
4992f004eeaSJann Horn 	 */
5002f004eeaSJann Horn 	if (orig_addr < PAGE_SIZE)
5012f004eeaSJann Horn 		bug_type = "null-ptr-deref";
5022f004eeaSJann Horn 	else if (orig_addr < TASK_SIZE)
5032f004eeaSJann Horn 		bug_type = "probably user-memory-access";
5042f004eeaSJann Horn 	else
5052f004eeaSJann Horn 		bug_type = "maybe wild-memory-access";
5062f004eeaSJann Horn 	pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
5071f600626SAndrey Konovalov 		 orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1);
5082f004eeaSJann Horn }
5092f004eeaSJann Horn #endif
510