xref: /openbmc/linux/mm/kasan/report.c (revision 505be48165fa2f2cb795659b4fc61f35563d105e)
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 
1017e088978SAndrey Ryabinin static DEFINE_SPINLOCK(report_lock);
1027e088978SAndrey Ryabinin 
10311cd3cd6SAndrey Konovalov static void start_report(unsigned long *flags)
1047e088978SAndrey Ryabinin {
1057e088978SAndrey Ryabinin 	/*
1067e088978SAndrey Ryabinin 	 * Make sure we don't end up in loop.
1077e088978SAndrey Ryabinin 	 */
1087e088978SAndrey Ryabinin 	kasan_disable_current();
1097e088978SAndrey Ryabinin 	spin_lock_irqsave(&report_lock, *flags);
1107e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
1117e088978SAndrey Ryabinin }
1127e088978SAndrey Ryabinin 
113d3a61f74SAlexander Potapenko static void end_report(unsigned long *flags, unsigned long addr)
1147e088978SAndrey Ryabinin {
1158f7b5054SVincenzo Frascino 	if (!kasan_async_mode_enabled())
116d3a61f74SAlexander Potapenko 		trace_error_report_end(ERROR_DETECTOR_KASAN, addr);
1177e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
1187e088978SAndrey Ryabinin 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
1197e088978SAndrey Ryabinin 	spin_unlock_irqrestore(&report_lock, *flags);
120be4f1ae9SDavid Gow 	if (panic_on_warn && !test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) {
1211d2252faSKees Cook 		/*
1221d2252faSKees Cook 		 * This thread may hit another WARN() in the panic path.
1231d2252faSKees Cook 		 * Resetting this prevents additional WARN() from panicking the
1241d2252faSKees Cook 		 * system on this thread.  Other threads are blocked by the
1251d2252faSKees Cook 		 * panic_mutex in panic().
1261d2252faSKees Cook 		 */
1271d2252faSKees Cook 		panic_on_warn = 0;
1285c5c1f36SDmitry Vyukov 		panic("panic_on_warn set ...\n");
1291d2252faSKees Cook 	}
130c9d1af2bSWoody Lin 	if (kasan_arg_fault == KASAN_ARG_FAULT_PANIC)
1318028caacSAndrey Konovalov 		panic("kasan.fault=panic set ...\n");
1327e088978SAndrey Ryabinin 	kasan_enable_current();
1337e088978SAndrey Ryabinin }
1347e088978SAndrey Ryabinin 
135b6b72f49SAndrey Konovalov static void print_track(struct kasan_track *track, const char *prefix)
1367ed2f9e6SAlexander Potapenko {
137b6b72f49SAndrey Konovalov 	pr_err("%s by task %u:\n", prefix, track->pid);
138cd11016eSAlexander Potapenko 	if (track->stack) {
139*505be481SImran Khan 		stack_depot_print(track->stack);
140cd11016eSAlexander Potapenko 	} else {
141cd11016eSAlexander Potapenko 		pr_err("(stack is not available)\n");
142cd11016eSAlexander Potapenko 	}
1437ed2f9e6SAlexander Potapenko }
1447ed2f9e6SAlexander Potapenko 
145ae8f06b3SWalter Wu struct page *kasan_addr_to_page(const void *addr)
146db429f16SAndrey Konovalov {
147db429f16SAndrey Konovalov 	if ((addr >= (void *)PAGE_OFFSET) &&
148db429f16SAndrey Konovalov 			(addr < high_memory))
149db429f16SAndrey Konovalov 		return virt_to_head_page(addr);
150db429f16SAndrey Konovalov 	return NULL;
151db429f16SAndrey Konovalov }
152db429f16SAndrey Konovalov 
1530c06f1f8SAndrey Konovalov static void describe_object_addr(struct kmem_cache *cache, void *object,
1540c06f1f8SAndrey Konovalov 				const void *addr)
1550c06f1f8SAndrey Konovalov {
1560c06f1f8SAndrey Konovalov 	unsigned long access_addr = (unsigned long)addr;
1570c06f1f8SAndrey Konovalov 	unsigned long object_addr = (unsigned long)object;
1580c06f1f8SAndrey Konovalov 	const char *rel_type;
1590c06f1f8SAndrey Konovalov 	int rel_bytes;
1600c06f1f8SAndrey Konovalov 
1616424f6bbSTobin C. Harding 	pr_err("The buggy address belongs to the object at %px\n"
1620c06f1f8SAndrey Konovalov 	       " which belongs to the cache %s of size %d\n",
1630c06f1f8SAndrey Konovalov 		object, cache->name, cache->object_size);
1640c06f1f8SAndrey Konovalov 
1650c06f1f8SAndrey Konovalov 	if (!addr)
1660c06f1f8SAndrey Konovalov 		return;
1670c06f1f8SAndrey Konovalov 
1680c06f1f8SAndrey Konovalov 	if (access_addr < object_addr) {
1690c06f1f8SAndrey Konovalov 		rel_type = "to the left";
1700c06f1f8SAndrey Konovalov 		rel_bytes = object_addr - access_addr;
1710c06f1f8SAndrey Konovalov 	} else if (access_addr >= object_addr + cache->object_size) {
1720c06f1f8SAndrey Konovalov 		rel_type = "to the right";
1730c06f1f8SAndrey Konovalov 		rel_bytes = access_addr - (object_addr + cache->object_size);
1740c06f1f8SAndrey Konovalov 	} else {
1750c06f1f8SAndrey Konovalov 		rel_type = "inside";
1760c06f1f8SAndrey Konovalov 		rel_bytes = access_addr - object_addr;
1770c06f1f8SAndrey Konovalov 	}
1780c06f1f8SAndrey Konovalov 
1790c06f1f8SAndrey Konovalov 	pr_err("The buggy address is located %d bytes %s of\n"
1806424f6bbSTobin C. Harding 	       " %d-byte region [%px, %px)\n",
1810c06f1f8SAndrey Konovalov 		rel_bytes, rel_type, cache->object_size, (void *)object_addr,
1820c06f1f8SAndrey Konovalov 		(void *)(object_addr + cache->object_size));
1830c06f1f8SAndrey Konovalov }
1840c06f1f8SAndrey Konovalov 
1858028caacSAndrey Konovalov static void describe_object_stacks(struct kmem_cache *cache, void *object,
186ae8f06b3SWalter Wu 					const void *addr, u8 tag)
1877ed2f9e6SAlexander Potapenko {
18897593cadSAndrey Konovalov 	struct kasan_alloc_meta *alloc_meta;
189ae8f06b3SWalter Wu 	struct kasan_track *free_track;
190ae8f06b3SWalter Wu 
19197593cadSAndrey Konovalov 	alloc_meta = kasan_get_alloc_meta(cache, object);
19297593cadSAndrey Konovalov 	if (alloc_meta) {
1936476792fSAndrey Konovalov 		print_track(&alloc_meta->alloc_track, "Allocated");
194b1938599SAndrey Konovalov 		pr_err("\n");
19597593cadSAndrey Konovalov 	}
19697593cadSAndrey Konovalov 
197ae8f06b3SWalter Wu 	free_track = kasan_get_free_track(cache, object, tag);
198e4b7818bSWalter Wu 	if (free_track) {
199ae8f06b3SWalter Wu 		print_track(free_track, "Freed");
200b1938599SAndrey Konovalov 		pr_err("\n");
201e4b7818bSWalter Wu 	}
20226e760c9SWalter Wu 
20326e760c9SWalter Wu #ifdef CONFIG_KASAN_GENERIC
20497593cadSAndrey Konovalov 	if (!alloc_meta)
20597593cadSAndrey Konovalov 		return;
2066476792fSAndrey Konovalov 	if (alloc_meta->aux_stack[0]) {
207ef133461SWalter Wu 		pr_err("Last potentially related work creation:\n");
208*505be481SImran Khan 		stack_depot_print(alloc_meta->aux_stack[0]);
20926e760c9SWalter Wu 		pr_err("\n");
21026e760c9SWalter Wu 	}
2116476792fSAndrey Konovalov 	if (alloc_meta->aux_stack[1]) {
212ef133461SWalter Wu 		pr_err("Second to last potentially related work creation:\n");
213*505be481SImran Khan 		stack_depot_print(alloc_meta->aux_stack[1]);
21426e760c9SWalter Wu 		pr_err("\n");
21526e760c9SWalter Wu 	}
21626e760c9SWalter Wu #endif
2177ed2f9e6SAlexander Potapenko }
2187ed2f9e6SAlexander Potapenko 
2198028caacSAndrey Konovalov static void describe_object(struct kmem_cache *cache, void *object,
2208028caacSAndrey Konovalov 				const void *addr, u8 tag)
2218028caacSAndrey Konovalov {
2228028caacSAndrey Konovalov 	if (kasan_stack_collection_enabled())
2238028caacSAndrey Konovalov 		describe_object_stacks(cache, object, addr, tag);
2240c06f1f8SAndrey Konovalov 	describe_object_addr(cache, object, addr);
2250c06f1f8SAndrey Konovalov }
2260c06f1f8SAndrey Konovalov 
22711cd3cd6SAndrey Konovalov static inline bool kernel_or_module_addr(const void *addr)
22811cd3cd6SAndrey Konovalov {
22911cd3cd6SAndrey Konovalov 	if (addr >= (void *)_stext && addr < (void *)_end)
23011cd3cd6SAndrey Konovalov 		return true;
23111cd3cd6SAndrey Konovalov 	if (is_module_address((unsigned long)addr))
23211cd3cd6SAndrey Konovalov 		return true;
23311cd3cd6SAndrey Konovalov 	return false;
23411cd3cd6SAndrey Konovalov }
23511cd3cd6SAndrey Konovalov 
23611cd3cd6SAndrey Konovalov static inline bool init_task_stack_addr(const void *addr)
23711cd3cd6SAndrey Konovalov {
23811cd3cd6SAndrey Konovalov 	return addr >= (void *)&init_thread_union.stack &&
23911cd3cd6SAndrey Konovalov 		(addr <= (void *)&init_thread_union.stack +
24011cd3cd6SAndrey Konovalov 			sizeof(init_thread_union.stack));
24111cd3cd6SAndrey Konovalov }
24211cd3cd6SAndrey Konovalov 
243ae8f06b3SWalter Wu static void print_address_description(void *addr, u8 tag)
2447e088978SAndrey Ryabinin {
245ae8f06b3SWalter Wu 	struct page *page = kasan_addr_to_page(addr);
246b8c73fc2SAndrey Ryabinin 
247336abff6SAlexander Potapenko 	dump_stack_lvl(KERN_ERR);
248b1938599SAndrey Konovalov 	pr_err("\n");
249db429f16SAndrey Konovalov 
250db429f16SAndrey Konovalov 	if (page && PageSlab(page)) {
251db429f16SAndrey Konovalov 		struct kmem_cache *cache = page->slab_cache;
2520c06f1f8SAndrey Konovalov 		void *object = nearest_obj(cache, page,	addr);
253db429f16SAndrey Konovalov 
254ae8f06b3SWalter Wu 		describe_object(cache, object, addr, tag);
255b8c73fc2SAndrey Ryabinin 	}
256b8c73fc2SAndrey Ryabinin 
257430a05f9SAndrey Konovalov 	if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
258430a05f9SAndrey Konovalov 		pr_err("The buggy address belongs to the variable:\n");
259430a05f9SAndrey Konovalov 		pr_err(" %pS\n", addr);
260430a05f9SAndrey Konovalov 	}
261430a05f9SAndrey Konovalov 
262430a05f9SAndrey Konovalov 	if (page) {
263430a05f9SAndrey Konovalov 		pr_err("The buggy address belongs to the page:\n");
264430a05f9SAndrey Konovalov 		dump_page(page, "kasan: bad access detected");
265bebf56a1SAndrey Ryabinin 	}
266e8969219SMarco Elver 
267f00748bfSAndrey Konovalov 	kasan_print_address_stack_frame(addr);
2680b24beccSAndrey Ryabinin }
2690b24beccSAndrey Ryabinin 
27096e0279dSAndrey Konovalov static bool meta_row_is_guilty(const void *row, const void *addr)
2710b24beccSAndrey Ryabinin {
27296e0279dSAndrey Konovalov 	return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW);
2730b24beccSAndrey Ryabinin }
2740b24beccSAndrey Ryabinin 
27596e0279dSAndrey Konovalov static int meta_pointer_offset(const void *row, const void *addr)
2760b24beccSAndrey Ryabinin {
27796e0279dSAndrey Konovalov 	/*
27896e0279dSAndrey Konovalov 	 * Memory state around the buggy address:
27996e0279dSAndrey Konovalov 	 *  ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe
28096e0279dSAndrey Konovalov 	 *  ...
28196e0279dSAndrey Konovalov 	 *
28296e0279dSAndrey Konovalov 	 * The length of ">ff00ff00ff00ff00: " is
2830b24beccSAndrey Ryabinin 	 *    3 + (BITS_PER_LONG / 8) * 2 chars.
28496e0279dSAndrey Konovalov 	 * The length of each granule metadata is 2 bytes
28596e0279dSAndrey Konovalov 	 *    plus 1 byte for space.
2860b24beccSAndrey Ryabinin 	 */
28796e0279dSAndrey Konovalov 	return 3 + (BITS_PER_LONG / 8) * 2 +
28896e0279dSAndrey Konovalov 		(addr - row) / KASAN_GRANULE_SIZE * 3 + 1;
2890b24beccSAndrey Ryabinin }
2900b24beccSAndrey Ryabinin 
291db3de8f7SAndrey Konovalov static void print_memory_metadata(const void *addr)
2920b24beccSAndrey Ryabinin {
2930b24beccSAndrey Ryabinin 	int i;
29496e0279dSAndrey Konovalov 	void *row;
2950b24beccSAndrey Ryabinin 
29696e0279dSAndrey Konovalov 	row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW)
29796e0279dSAndrey Konovalov 			- META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW;
2980b24beccSAndrey Ryabinin 
2990b24beccSAndrey Ryabinin 	pr_err("Memory state around the buggy address:\n");
3000b24beccSAndrey Ryabinin 
30188b86597SAndrey Konovalov 	for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
3020b24beccSAndrey Ryabinin 		char buffer[4 + (BITS_PER_LONG / 8) * 2];
30396e0279dSAndrey Konovalov 		char metadata[META_BYTES_PER_ROW];
3040b24beccSAndrey Ryabinin 
3050b24beccSAndrey Ryabinin 		snprintf(buffer, sizeof(buffer),
30696e0279dSAndrey Konovalov 				(i == 0) ? ">%px: " : " %px: ", row);
30796e0279dSAndrey Konovalov 
308f2377d4eSAneesh Kumar K.V 		/*
309f2377d4eSAneesh Kumar K.V 		 * We should not pass a shadow pointer to generic
310f2377d4eSAneesh Kumar K.V 		 * function, because generic functions may try to
311f2377d4eSAneesh Kumar K.V 		 * access kasan mapping for the passed address.
312f2377d4eSAneesh Kumar K.V 		 */
313f00748bfSAndrey Konovalov 		kasan_metadata_fetch_row(&metadata[0], row);
31496e0279dSAndrey Konovalov 
3150b24beccSAndrey Ryabinin 		print_hex_dump(KERN_ERR, buffer,
31688b86597SAndrey Konovalov 			DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
31796e0279dSAndrey Konovalov 			metadata, META_BYTES_PER_ROW, 0);
3180b24beccSAndrey Ryabinin 
31996e0279dSAndrey Konovalov 		if (meta_row_is_guilty(row, addr))
32096e0279dSAndrey Konovalov 			pr_err("%*c\n", meta_pointer_offset(row, addr), '^');
3210b24beccSAndrey Ryabinin 
32296e0279dSAndrey Konovalov 		row += META_MEM_BYTES_PER_ROW;
3230b24beccSAndrey Ryabinin 	}
3240b24beccSAndrey Ryabinin }
3250b24beccSAndrey Ryabinin 
3264fba3758SAndrey Konovalov static bool report_enabled(void)
327b0845ce5SMark Rutland {
328d73b4936SAndrey Konovalov #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
329b0845ce5SMark Rutland 	if (current->kasan_depth)
330b0845ce5SMark Rutland 		return false;
331d73b4936SAndrey Konovalov #endif
332b0845ce5SMark Rutland 	if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
333b0845ce5SMark Rutland 		return true;
334b0845ce5SMark Rutland 	return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
335b0845ce5SMark Rutland }
336b0845ce5SMark Rutland 
33783c4e7a0SPatricia Alfonso #if IS_ENABLED(CONFIG_KUNIT)
33883c4e7a0SPatricia Alfonso static void kasan_update_kunit_status(struct kunit *cur_test)
33983c4e7a0SPatricia Alfonso {
34083c4e7a0SPatricia Alfonso 	struct kunit_resource *resource;
34183c4e7a0SPatricia Alfonso 	struct kunit_kasan_expectation *kasan_data;
34283c4e7a0SPatricia Alfonso 
34383c4e7a0SPatricia Alfonso 	resource = kunit_find_named_resource(cur_test, "kasan_data");
34483c4e7a0SPatricia Alfonso 
34583c4e7a0SPatricia Alfonso 	if (!resource) {
34683c4e7a0SPatricia Alfonso 		kunit_set_failure(cur_test);
34783c4e7a0SPatricia Alfonso 		return;
34883c4e7a0SPatricia Alfonso 	}
34983c4e7a0SPatricia Alfonso 
35083c4e7a0SPatricia Alfonso 	kasan_data = (struct kunit_kasan_expectation *)resource->data;
3512e4bde6aSAndrey Konovalov 	WRITE_ONCE(kasan_data->report_found, true);
35283c4e7a0SPatricia Alfonso 	kunit_put_resource(resource);
35383c4e7a0SPatricia Alfonso }
35483c4e7a0SPatricia Alfonso #endif /* IS_ENABLED(CONFIG_KUNIT) */
35583c4e7a0SPatricia Alfonso 
35611cd3cd6SAndrey Konovalov void kasan_report_invalid_free(void *object, unsigned long ip)
35711cd3cd6SAndrey Konovalov {
35811cd3cd6SAndrey Konovalov 	unsigned long flags;
359ae8f06b3SWalter Wu 	u8 tag = get_tag(object);
36011cd3cd6SAndrey Konovalov 
361c0054c56SAndrey Konovalov 	object = kasan_reset_tag(object);
36283c4e7a0SPatricia Alfonso 
36383c4e7a0SPatricia Alfonso #if IS_ENABLED(CONFIG_KUNIT)
36483c4e7a0SPatricia Alfonso 	if (current->kunit_test)
36583c4e7a0SPatricia Alfonso 		kasan_update_kunit_status(current->kunit_test);
36683c4e7a0SPatricia Alfonso #endif /* IS_ENABLED(CONFIG_KUNIT) */
36783c4e7a0SPatricia Alfonso 
36811cd3cd6SAndrey Konovalov 	start_report(&flags);
36911cd3cd6SAndrey Konovalov 	pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
370f00748bfSAndrey Konovalov 	kasan_print_tags(tag, object);
37111cd3cd6SAndrey Konovalov 	pr_err("\n");
372ae8f06b3SWalter Wu 	print_address_description(object, tag);
37311cd3cd6SAndrey Konovalov 	pr_err("\n");
374db3de8f7SAndrey Konovalov 	print_memory_metadata(object);
375d3a61f74SAlexander Potapenko 	end_report(&flags, (unsigned long)object);
37611cd3cd6SAndrey Konovalov }
37711cd3cd6SAndrey Konovalov 
3788f7b5054SVincenzo Frascino #ifdef CONFIG_KASAN_HW_TAGS
3798f7b5054SVincenzo Frascino void kasan_report_async(void)
3808f7b5054SVincenzo Frascino {
3818f7b5054SVincenzo Frascino 	unsigned long flags;
3828f7b5054SVincenzo Frascino 
383e80a76aaSAndrey Konovalov #if IS_ENABLED(CONFIG_KUNIT)
384e80a76aaSAndrey Konovalov 	if (current->kunit_test)
385e80a76aaSAndrey Konovalov 		kasan_update_kunit_status(current->kunit_test);
386e80a76aaSAndrey Konovalov #endif /* IS_ENABLED(CONFIG_KUNIT) */
387e80a76aaSAndrey Konovalov 
3888f7b5054SVincenzo Frascino 	start_report(&flags);
3898f7b5054SVincenzo Frascino 	pr_err("BUG: KASAN: invalid-access\n");
3908f7b5054SVincenzo Frascino 	pr_err("Asynchronous mode enabled: no access details available\n");
3918f7b5054SVincenzo Frascino 	pr_err("\n");
392336abff6SAlexander Potapenko 	dump_stack_lvl(KERN_ERR);
3938f7b5054SVincenzo Frascino 	end_report(&flags, 0);
3948f7b5054SVincenzo Frascino }
3958f7b5054SVincenzo Frascino #endif /* CONFIG_KASAN_HW_TAGS */
3968f7b5054SVincenzo Frascino 
3974fba3758SAndrey Konovalov static void __kasan_report(unsigned long addr, size_t size, bool is_write,
3984fba3758SAndrey Konovalov 				unsigned long ip)
3990b24beccSAndrey Ryabinin {
4000b24beccSAndrey Ryabinin 	struct kasan_access_info info;
401121e8f81SAndrey Konovalov 	void *tagged_addr;
402121e8f81SAndrey Konovalov 	void *untagged_addr;
403121e8f81SAndrey Konovalov 	unsigned long flags;
4040b24beccSAndrey Ryabinin 
40583c4e7a0SPatricia Alfonso #if IS_ENABLED(CONFIG_KUNIT)
40683c4e7a0SPatricia Alfonso 	if (current->kunit_test)
40783c4e7a0SPatricia Alfonso 		kasan_update_kunit_status(current->kunit_test);
40883c4e7a0SPatricia Alfonso #endif /* IS_ENABLED(CONFIG_KUNIT) */
40983c4e7a0SPatricia Alfonso 
4104f40c6e5SPeter Zijlstra 	disable_trace_on_warning();
4114f40c6e5SPeter Zijlstra 
412121e8f81SAndrey Konovalov 	tagged_addr = (void *)addr;
413c0054c56SAndrey Konovalov 	untagged_addr = kasan_reset_tag(tagged_addr);
414121e8f81SAndrey Konovalov 
415121e8f81SAndrey Konovalov 	info.access_addr = tagged_addr;
4166882464fSAndrey Konovalov 	if (addr_has_metadata(untagged_addr))
417f00748bfSAndrey Konovalov 		info.first_bad_addr =
418f00748bfSAndrey Konovalov 			kasan_find_first_bad_addr(tagged_addr, size);
419121e8f81SAndrey Konovalov 	else
420121e8f81SAndrey Konovalov 		info.first_bad_addr = untagged_addr;
4210b24beccSAndrey Ryabinin 	info.access_size = size;
4220b24beccSAndrey Ryabinin 	info.is_write = is_write;
4230b24beccSAndrey Ryabinin 	info.ip = ip;
424e9121076SAndrey Konovalov 
425121e8f81SAndrey Konovalov 	start_report(&flags);
426121e8f81SAndrey Konovalov 
427121e8f81SAndrey Konovalov 	print_error_description(&info);
4286882464fSAndrey Konovalov 	if (addr_has_metadata(untagged_addr))
429f00748bfSAndrey Konovalov 		kasan_print_tags(get_tag(tagged_addr), info.first_bad_addr);
430121e8f81SAndrey Konovalov 	pr_err("\n");
431121e8f81SAndrey Konovalov 
4326882464fSAndrey Konovalov 	if (addr_has_metadata(untagged_addr)) {
433ae8f06b3SWalter Wu 		print_address_description(untagged_addr, get_tag(tagged_addr));
434121e8f81SAndrey Konovalov 		pr_err("\n");
435db3de8f7SAndrey Konovalov 		print_memory_metadata(info.first_bad_addr);
436121e8f81SAndrey Konovalov 	} else {
437336abff6SAlexander Potapenko 		dump_stack_lvl(KERN_ERR);
438121e8f81SAndrey Konovalov 	}
439121e8f81SAndrey Konovalov 
440d3a61f74SAlexander Potapenko 	end_report(&flags, addr);
4410b24beccSAndrey Ryabinin }
4422f004eeaSJann Horn 
4434fba3758SAndrey Konovalov bool kasan_report(unsigned long addr, size_t size, bool is_write,
4444fba3758SAndrey Konovalov 			unsigned long ip)
4454fba3758SAndrey Konovalov {
4464fba3758SAndrey Konovalov 	unsigned long flags = user_access_save();
4474fba3758SAndrey Konovalov 	bool ret = false;
4484fba3758SAndrey Konovalov 
4494fba3758SAndrey Konovalov 	if (likely(report_enabled())) {
4504fba3758SAndrey Konovalov 		__kasan_report(addr, size, is_write, ip);
4514fba3758SAndrey Konovalov 		ret = true;
4524fba3758SAndrey Konovalov 	}
4534fba3758SAndrey Konovalov 
4544fba3758SAndrey Konovalov 	user_access_restore(flags);
4554fba3758SAndrey Konovalov 
4564fba3758SAndrey Konovalov 	return ret;
4574fba3758SAndrey Konovalov }
4584fba3758SAndrey Konovalov 
4592f004eeaSJann Horn #ifdef CONFIG_KASAN_INLINE
4602f004eeaSJann Horn /*
4612f004eeaSJann Horn  * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
4622f004eeaSJann Horn  * canonical half of the address space) cause out-of-bounds shadow memory reads
4632f004eeaSJann Horn  * before the actual access. For addresses in the low canonical half of the
4642f004eeaSJann Horn  * address space, as well as most non-canonical addresses, that out-of-bounds
4652f004eeaSJann Horn  * shadow memory access lands in the non-canonical part of the address space.
4662f004eeaSJann Horn  * Help the user figure out what the original bogus pointer was.
4672f004eeaSJann Horn  */
4682f004eeaSJann Horn void kasan_non_canonical_hook(unsigned long addr)
4692f004eeaSJann Horn {
4702f004eeaSJann Horn 	unsigned long orig_addr;
4712f004eeaSJann Horn 	const char *bug_type;
4722f004eeaSJann Horn 
4732f004eeaSJann Horn 	if (addr < KASAN_SHADOW_OFFSET)
4742f004eeaSJann Horn 		return;
4752f004eeaSJann Horn 
4762f004eeaSJann Horn 	orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
4772f004eeaSJann Horn 	/*
4782f004eeaSJann Horn 	 * For faults near the shadow address for NULL, we can be fairly certain
4792f004eeaSJann Horn 	 * that this is a KASAN shadow memory access.
4802f004eeaSJann Horn 	 * For faults that correspond to shadow for low canonical addresses, we
4812f004eeaSJann Horn 	 * can still be pretty sure - that shadow region is a fairly narrow
4822f004eeaSJann Horn 	 * chunk of the non-canonical address space.
4832f004eeaSJann Horn 	 * But faults that look like shadow for non-canonical addresses are a
4842f004eeaSJann Horn 	 * really large chunk of the address space. In that case, we still
4852f004eeaSJann Horn 	 * print the decoded address, but make it clear that this is not
4862f004eeaSJann Horn 	 * necessarily what's actually going on.
4872f004eeaSJann Horn 	 */
4882f004eeaSJann Horn 	if (orig_addr < PAGE_SIZE)
4892f004eeaSJann Horn 		bug_type = "null-ptr-deref";
4902f004eeaSJann Horn 	else if (orig_addr < TASK_SIZE)
4912f004eeaSJann Horn 		bug_type = "probably user-memory-access";
4922f004eeaSJann Horn 	else
4932f004eeaSJann Horn 		bug_type = "maybe wild-memory-access";
4942f004eeaSJann Horn 	pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
4951f600626SAndrey Konovalov 		 orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1);
4962f004eeaSJann Horn }
4972f004eeaSJann Horn #endif
498