xref: /openbmc/linux/mm/kasan/report.c (revision f00748bfa0246c428bf93f45267b8f1aa1816098)
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>
280b24beccSAndrey Ryabinin 
29bebf56a1SAndrey Ryabinin #include <asm/sections.h>
30bebf56a1SAndrey Ryabinin 
3183c4e7a0SPatricia Alfonso #include <kunit/test.h>
3283c4e7a0SPatricia Alfonso 
330b24beccSAndrey Ryabinin #include "kasan.h"
340316bec2SAndrey Ryabinin #include "../slab.h"
350b24beccSAndrey Ryabinin 
3611cd3cd6SAndrey Konovalov static unsigned long kasan_flags;
3711cd3cd6SAndrey Konovalov 
3811cd3cd6SAndrey Konovalov #define KASAN_BIT_REPORTED	0
3911cd3cd6SAndrey Konovalov #define KASAN_BIT_MULTI_SHOT	1
4011cd3cd6SAndrey Konovalov 
4111cd3cd6SAndrey Konovalov bool kasan_save_enable_multi_shot(void)
420b24beccSAndrey Ryabinin {
4311cd3cd6SAndrey Konovalov 	return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
440b24beccSAndrey Ryabinin }
4511cd3cd6SAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
460b24beccSAndrey Ryabinin 
4711cd3cd6SAndrey Konovalov void kasan_restore_multi_shot(bool enabled)
485e82cd12SAndrey Konovalov {
4911cd3cd6SAndrey Konovalov 	if (!enabled)
5011cd3cd6SAndrey Konovalov 		clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
515e82cd12SAndrey Konovalov }
5211cd3cd6SAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
535e82cd12SAndrey Konovalov 
5411cd3cd6SAndrey Konovalov static int __init kasan_set_multi_shot(char *str)
550b24beccSAndrey Ryabinin {
5611cd3cd6SAndrey Konovalov 	set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
5711cd3cd6SAndrey Konovalov 	return 1;
580b24beccSAndrey Ryabinin }
5911cd3cd6SAndrey Konovalov __setup("kasan_multi_shot", kasan_set_multi_shot);
600b24beccSAndrey Ryabinin 
61121e8f81SAndrey Konovalov static void print_error_description(struct kasan_access_info *info)
625e82cd12SAndrey Konovalov {
637f0a84c2SAndrey Konovalov 	pr_err("BUG: KASAN: %s in %pS\n",
64*f00748bfSAndrey Konovalov 		kasan_get_bug_type(info), (void *)info->ip);
654291e9eeSAndrey Konovalov 	if (info->access_size)
666424f6bbSTobin C. Harding 		pr_err("%s of size %zu at addr %px by task %s/%d\n",
677d418f7bSAndrey Konovalov 			info->is_write ? "Write" : "Read", info->access_size,
687f0a84c2SAndrey Konovalov 			info->access_addr, current->comm, task_pid_nr(current));
694291e9eeSAndrey Konovalov 	else
704291e9eeSAndrey Konovalov 		pr_err("%s at addr %px by task %s/%d\n",
714291e9eeSAndrey Konovalov 			info->is_write ? "Write" : "Read",
724291e9eeSAndrey Konovalov 			info->access_addr, current->comm, task_pid_nr(current));
730b24beccSAndrey Ryabinin }
740b24beccSAndrey Ryabinin 
757e088978SAndrey Ryabinin static DEFINE_SPINLOCK(report_lock);
767e088978SAndrey Ryabinin 
7711cd3cd6SAndrey Konovalov static void start_report(unsigned long *flags)
787e088978SAndrey Ryabinin {
797e088978SAndrey Ryabinin 	/*
807e088978SAndrey Ryabinin 	 * Make sure we don't end up in loop.
817e088978SAndrey Ryabinin 	 */
827e088978SAndrey Ryabinin 	kasan_disable_current();
837e088978SAndrey Ryabinin 	spin_lock_irqsave(&report_lock, *flags);
847e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
857e088978SAndrey Ryabinin }
867e088978SAndrey Ryabinin 
8711cd3cd6SAndrey Konovalov static void end_report(unsigned long *flags)
887e088978SAndrey Ryabinin {
897e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
907e088978SAndrey Ryabinin 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
917e088978SAndrey Ryabinin 	spin_unlock_irqrestore(&report_lock, *flags);
92be4f1ae9SDavid Gow 	if (panic_on_warn && !test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) {
931d2252faSKees Cook 		/*
941d2252faSKees Cook 		 * This thread may hit another WARN() in the panic path.
951d2252faSKees Cook 		 * Resetting this prevents additional WARN() from panicking the
961d2252faSKees Cook 		 * system on this thread.  Other threads are blocked by the
971d2252faSKees Cook 		 * panic_mutex in panic().
981d2252faSKees Cook 		 */
991d2252faSKees Cook 		panic_on_warn = 0;
1005c5c1f36SDmitry Vyukov 		panic("panic_on_warn set ...\n");
1011d2252faSKees Cook 	}
1028028caacSAndrey Konovalov #ifdef CONFIG_KASAN_HW_TAGS
1038028caacSAndrey Konovalov 	if (kasan_flag_panic)
1048028caacSAndrey Konovalov 		panic("kasan.fault=panic set ...\n");
1058028caacSAndrey Konovalov #endif
1067e088978SAndrey Ryabinin 	kasan_enable_current();
1077e088978SAndrey Ryabinin }
1087e088978SAndrey Ryabinin 
10926e760c9SWalter Wu static void print_stack(depot_stack_handle_t stack)
11026e760c9SWalter Wu {
11126e760c9SWalter Wu 	unsigned long *entries;
11226e760c9SWalter Wu 	unsigned int nr_entries;
11326e760c9SWalter Wu 
11426e760c9SWalter Wu 	nr_entries = stack_depot_fetch(stack, &entries);
11526e760c9SWalter Wu 	stack_trace_print(entries, nr_entries, 0);
11626e760c9SWalter Wu }
11726e760c9SWalter Wu 
118b6b72f49SAndrey Konovalov static void print_track(struct kasan_track *track, const char *prefix)
1197ed2f9e6SAlexander Potapenko {
120b6b72f49SAndrey Konovalov 	pr_err("%s by task %u:\n", prefix, track->pid);
121cd11016eSAlexander Potapenko 	if (track->stack) {
12226e760c9SWalter Wu 		print_stack(track->stack);
123cd11016eSAlexander Potapenko 	} else {
124cd11016eSAlexander Potapenko 		pr_err("(stack is not available)\n");
125cd11016eSAlexander Potapenko 	}
1267ed2f9e6SAlexander Potapenko }
1277ed2f9e6SAlexander Potapenko 
128ae8f06b3SWalter Wu struct page *kasan_addr_to_page(const void *addr)
129db429f16SAndrey Konovalov {
130db429f16SAndrey Konovalov 	if ((addr >= (void *)PAGE_OFFSET) &&
131db429f16SAndrey Konovalov 			(addr < high_memory))
132db429f16SAndrey Konovalov 		return virt_to_head_page(addr);
133db429f16SAndrey Konovalov 	return NULL;
134db429f16SAndrey Konovalov }
135db429f16SAndrey Konovalov 
1360c06f1f8SAndrey Konovalov static void describe_object_addr(struct kmem_cache *cache, void *object,
1370c06f1f8SAndrey Konovalov 				const void *addr)
1380c06f1f8SAndrey Konovalov {
1390c06f1f8SAndrey Konovalov 	unsigned long access_addr = (unsigned long)addr;
1400c06f1f8SAndrey Konovalov 	unsigned long object_addr = (unsigned long)object;
1410c06f1f8SAndrey Konovalov 	const char *rel_type;
1420c06f1f8SAndrey Konovalov 	int rel_bytes;
1430c06f1f8SAndrey Konovalov 
1446424f6bbSTobin C. Harding 	pr_err("The buggy address belongs to the object at %px\n"
1450c06f1f8SAndrey Konovalov 	       " which belongs to the cache %s of size %d\n",
1460c06f1f8SAndrey Konovalov 		object, cache->name, cache->object_size);
1470c06f1f8SAndrey Konovalov 
1480c06f1f8SAndrey Konovalov 	if (!addr)
1490c06f1f8SAndrey Konovalov 		return;
1500c06f1f8SAndrey Konovalov 
1510c06f1f8SAndrey Konovalov 	if (access_addr < object_addr) {
1520c06f1f8SAndrey Konovalov 		rel_type = "to the left";
1530c06f1f8SAndrey Konovalov 		rel_bytes = object_addr - access_addr;
1540c06f1f8SAndrey Konovalov 	} else if (access_addr >= object_addr + cache->object_size) {
1550c06f1f8SAndrey Konovalov 		rel_type = "to the right";
1560c06f1f8SAndrey Konovalov 		rel_bytes = access_addr - (object_addr + cache->object_size);
1570c06f1f8SAndrey Konovalov 	} else {
1580c06f1f8SAndrey Konovalov 		rel_type = "inside";
1590c06f1f8SAndrey Konovalov 		rel_bytes = access_addr - object_addr;
1600c06f1f8SAndrey Konovalov 	}
1610c06f1f8SAndrey Konovalov 
1620c06f1f8SAndrey Konovalov 	pr_err("The buggy address is located %d bytes %s of\n"
1636424f6bbSTobin C. Harding 	       " %d-byte region [%px, %px)\n",
1640c06f1f8SAndrey Konovalov 		rel_bytes, rel_type, cache->object_size, (void *)object_addr,
1650c06f1f8SAndrey Konovalov 		(void *)(object_addr + cache->object_size));
1660c06f1f8SAndrey Konovalov }
1670c06f1f8SAndrey Konovalov 
1688028caacSAndrey Konovalov static void describe_object_stacks(struct kmem_cache *cache, void *object,
169ae8f06b3SWalter Wu 					const void *addr, u8 tag)
1707ed2f9e6SAlexander Potapenko {
17197593cadSAndrey Konovalov 	struct kasan_alloc_meta *alloc_meta;
172ae8f06b3SWalter Wu 	struct kasan_track *free_track;
173ae8f06b3SWalter Wu 
17497593cadSAndrey Konovalov 	alloc_meta = kasan_get_alloc_meta(cache, object);
17597593cadSAndrey Konovalov 	if (alloc_meta) {
1766476792fSAndrey Konovalov 		print_track(&alloc_meta->alloc_track, "Allocated");
177b1938599SAndrey Konovalov 		pr_err("\n");
17897593cadSAndrey Konovalov 	}
17997593cadSAndrey Konovalov 
180ae8f06b3SWalter Wu 	free_track = kasan_get_free_track(cache, object, tag);
181e4b7818bSWalter Wu 	if (free_track) {
182ae8f06b3SWalter Wu 		print_track(free_track, "Freed");
183b1938599SAndrey Konovalov 		pr_err("\n");
184e4b7818bSWalter Wu 	}
18526e760c9SWalter Wu 
18626e760c9SWalter Wu #ifdef CONFIG_KASAN_GENERIC
18797593cadSAndrey Konovalov 	if (!alloc_meta)
18897593cadSAndrey Konovalov 		return;
1896476792fSAndrey Konovalov 	if (alloc_meta->aux_stack[0]) {
190ef133461SWalter Wu 		pr_err("Last potentially related work creation:\n");
1916476792fSAndrey Konovalov 		print_stack(alloc_meta->aux_stack[0]);
19226e760c9SWalter Wu 		pr_err("\n");
19326e760c9SWalter Wu 	}
1946476792fSAndrey Konovalov 	if (alloc_meta->aux_stack[1]) {
195ef133461SWalter Wu 		pr_err("Second to last potentially related work creation:\n");
1966476792fSAndrey Konovalov 		print_stack(alloc_meta->aux_stack[1]);
19726e760c9SWalter Wu 		pr_err("\n");
19826e760c9SWalter Wu 	}
19926e760c9SWalter Wu #endif
2007ed2f9e6SAlexander Potapenko }
2017ed2f9e6SAlexander Potapenko 
2028028caacSAndrey Konovalov static void describe_object(struct kmem_cache *cache, void *object,
2038028caacSAndrey Konovalov 				const void *addr, u8 tag)
2048028caacSAndrey Konovalov {
2058028caacSAndrey Konovalov 	if (kasan_stack_collection_enabled())
2068028caacSAndrey Konovalov 		describe_object_stacks(cache, object, addr, tag);
2070c06f1f8SAndrey Konovalov 	describe_object_addr(cache, object, addr);
2080c06f1f8SAndrey Konovalov }
2090c06f1f8SAndrey Konovalov 
21011cd3cd6SAndrey Konovalov static inline bool kernel_or_module_addr(const void *addr)
21111cd3cd6SAndrey Konovalov {
21211cd3cd6SAndrey Konovalov 	if (addr >= (void *)_stext && addr < (void *)_end)
21311cd3cd6SAndrey Konovalov 		return true;
21411cd3cd6SAndrey Konovalov 	if (is_module_address((unsigned long)addr))
21511cd3cd6SAndrey Konovalov 		return true;
21611cd3cd6SAndrey Konovalov 	return false;
21711cd3cd6SAndrey Konovalov }
21811cd3cd6SAndrey Konovalov 
21911cd3cd6SAndrey Konovalov static inline bool init_task_stack_addr(const void *addr)
22011cd3cd6SAndrey Konovalov {
22111cd3cd6SAndrey Konovalov 	return addr >= (void *)&init_thread_union.stack &&
22211cd3cd6SAndrey Konovalov 		(addr <= (void *)&init_thread_union.stack +
22311cd3cd6SAndrey Konovalov 			sizeof(init_thread_union.stack));
22411cd3cd6SAndrey Konovalov }
22511cd3cd6SAndrey Konovalov 
226ae8f06b3SWalter Wu static void print_address_description(void *addr, u8 tag)
2277e088978SAndrey Ryabinin {
228ae8f06b3SWalter Wu 	struct page *page = kasan_addr_to_page(addr);
229b8c73fc2SAndrey Ryabinin 
230db429f16SAndrey Konovalov 	dump_stack();
231b1938599SAndrey Konovalov 	pr_err("\n");
232db429f16SAndrey Konovalov 
233db429f16SAndrey Konovalov 	if (page && PageSlab(page)) {
234db429f16SAndrey Konovalov 		struct kmem_cache *cache = page->slab_cache;
2350c06f1f8SAndrey Konovalov 		void *object = nearest_obj(cache, page,	addr);
236db429f16SAndrey Konovalov 
237ae8f06b3SWalter Wu 		describe_object(cache, object, addr, tag);
238b8c73fc2SAndrey Ryabinin 	}
239b8c73fc2SAndrey Ryabinin 
240430a05f9SAndrey Konovalov 	if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
241430a05f9SAndrey Konovalov 		pr_err("The buggy address belongs to the variable:\n");
242430a05f9SAndrey Konovalov 		pr_err(" %pS\n", addr);
243430a05f9SAndrey Konovalov 	}
244430a05f9SAndrey Konovalov 
245430a05f9SAndrey Konovalov 	if (page) {
246430a05f9SAndrey Konovalov 		pr_err("The buggy address belongs to the page:\n");
247430a05f9SAndrey Konovalov 		dump_page(page, "kasan: bad access detected");
248bebf56a1SAndrey Ryabinin 	}
249e8969219SMarco Elver 
250*f00748bfSAndrey Konovalov 	kasan_print_address_stack_frame(addr);
2510b24beccSAndrey Ryabinin }
2520b24beccSAndrey Ryabinin 
25396e0279dSAndrey Konovalov static bool meta_row_is_guilty(const void *row, const void *addr)
2540b24beccSAndrey Ryabinin {
25596e0279dSAndrey Konovalov 	return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW);
2560b24beccSAndrey Ryabinin }
2570b24beccSAndrey Ryabinin 
25896e0279dSAndrey Konovalov static int meta_pointer_offset(const void *row, const void *addr)
2590b24beccSAndrey Ryabinin {
26096e0279dSAndrey Konovalov 	/*
26196e0279dSAndrey Konovalov 	 * Memory state around the buggy address:
26296e0279dSAndrey Konovalov 	 *  ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe
26396e0279dSAndrey Konovalov 	 *  ...
26496e0279dSAndrey Konovalov 	 *
26596e0279dSAndrey Konovalov 	 * The length of ">ff00ff00ff00ff00: " is
2660b24beccSAndrey Ryabinin 	 *    3 + (BITS_PER_LONG / 8) * 2 chars.
26796e0279dSAndrey Konovalov 	 * The length of each granule metadata is 2 bytes
26896e0279dSAndrey Konovalov 	 *    plus 1 byte for space.
2690b24beccSAndrey Ryabinin 	 */
27096e0279dSAndrey Konovalov 	return 3 + (BITS_PER_LONG / 8) * 2 +
27196e0279dSAndrey Konovalov 		(addr - row) / KASAN_GRANULE_SIZE * 3 + 1;
2720b24beccSAndrey Ryabinin }
2730b24beccSAndrey Ryabinin 
274db3de8f7SAndrey Konovalov static void print_memory_metadata(const void *addr)
2750b24beccSAndrey Ryabinin {
2760b24beccSAndrey Ryabinin 	int i;
27796e0279dSAndrey Konovalov 	void *row;
2780b24beccSAndrey Ryabinin 
27996e0279dSAndrey Konovalov 	row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW)
28096e0279dSAndrey Konovalov 			- META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW;
2810b24beccSAndrey Ryabinin 
2820b24beccSAndrey Ryabinin 	pr_err("Memory state around the buggy address:\n");
2830b24beccSAndrey Ryabinin 
28488b86597SAndrey Konovalov 	for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
2850b24beccSAndrey Ryabinin 		char buffer[4 + (BITS_PER_LONG / 8) * 2];
28696e0279dSAndrey Konovalov 		char metadata[META_BYTES_PER_ROW];
2870b24beccSAndrey Ryabinin 
2880b24beccSAndrey Ryabinin 		snprintf(buffer, sizeof(buffer),
28996e0279dSAndrey Konovalov 				(i == 0) ? ">%px: " : " %px: ", row);
29096e0279dSAndrey Konovalov 
291f2377d4eSAneesh Kumar K.V 		/*
292f2377d4eSAneesh Kumar K.V 		 * We should not pass a shadow pointer to generic
293f2377d4eSAneesh Kumar K.V 		 * function, because generic functions may try to
294f2377d4eSAneesh Kumar K.V 		 * access kasan mapping for the passed address.
295f2377d4eSAneesh Kumar K.V 		 */
296*f00748bfSAndrey Konovalov 		kasan_metadata_fetch_row(&metadata[0], row);
29796e0279dSAndrey Konovalov 
2980b24beccSAndrey Ryabinin 		print_hex_dump(KERN_ERR, buffer,
29988b86597SAndrey Konovalov 			DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
30096e0279dSAndrey Konovalov 			metadata, META_BYTES_PER_ROW, 0);
3010b24beccSAndrey Ryabinin 
30296e0279dSAndrey Konovalov 		if (meta_row_is_guilty(row, addr))
30396e0279dSAndrey Konovalov 			pr_err("%*c\n", meta_pointer_offset(row, addr), '^');
3040b24beccSAndrey Ryabinin 
30596e0279dSAndrey Konovalov 		row += META_MEM_BYTES_PER_ROW;
3060b24beccSAndrey Ryabinin 	}
3070b24beccSAndrey Ryabinin }
3080b24beccSAndrey Ryabinin 
3094fba3758SAndrey Konovalov static bool report_enabled(void)
310b0845ce5SMark Rutland {
311d73b4936SAndrey Konovalov #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
312b0845ce5SMark Rutland 	if (current->kasan_depth)
313b0845ce5SMark Rutland 		return false;
314d73b4936SAndrey Konovalov #endif
315b0845ce5SMark Rutland 	if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
316b0845ce5SMark Rutland 		return true;
317b0845ce5SMark Rutland 	return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
318b0845ce5SMark Rutland }
319b0845ce5SMark Rutland 
32083c4e7a0SPatricia Alfonso #if IS_ENABLED(CONFIG_KUNIT)
32183c4e7a0SPatricia Alfonso static void kasan_update_kunit_status(struct kunit *cur_test)
32283c4e7a0SPatricia Alfonso {
32383c4e7a0SPatricia Alfonso 	struct kunit_resource *resource;
32483c4e7a0SPatricia Alfonso 	struct kunit_kasan_expectation *kasan_data;
32583c4e7a0SPatricia Alfonso 
32683c4e7a0SPatricia Alfonso 	resource = kunit_find_named_resource(cur_test, "kasan_data");
32783c4e7a0SPatricia Alfonso 
32883c4e7a0SPatricia Alfonso 	if (!resource) {
32983c4e7a0SPatricia Alfonso 		kunit_set_failure(cur_test);
33083c4e7a0SPatricia Alfonso 		return;
33183c4e7a0SPatricia Alfonso 	}
33283c4e7a0SPatricia Alfonso 
33383c4e7a0SPatricia Alfonso 	kasan_data = (struct kunit_kasan_expectation *)resource->data;
33483c4e7a0SPatricia Alfonso 	kasan_data->report_found = true;
33583c4e7a0SPatricia Alfonso 	kunit_put_resource(resource);
33683c4e7a0SPatricia Alfonso }
33783c4e7a0SPatricia Alfonso #endif /* IS_ENABLED(CONFIG_KUNIT) */
33883c4e7a0SPatricia Alfonso 
33911cd3cd6SAndrey Konovalov void kasan_report_invalid_free(void *object, unsigned long ip)
34011cd3cd6SAndrey Konovalov {
34111cd3cd6SAndrey Konovalov 	unsigned long flags;
342ae8f06b3SWalter Wu 	u8 tag = get_tag(object);
34311cd3cd6SAndrey Konovalov 
344c0054c56SAndrey Konovalov 	object = kasan_reset_tag(object);
34583c4e7a0SPatricia Alfonso 
34683c4e7a0SPatricia Alfonso #if IS_ENABLED(CONFIG_KUNIT)
34783c4e7a0SPatricia Alfonso 	if (current->kunit_test)
34883c4e7a0SPatricia Alfonso 		kasan_update_kunit_status(current->kunit_test);
34983c4e7a0SPatricia Alfonso #endif /* IS_ENABLED(CONFIG_KUNIT) */
35083c4e7a0SPatricia Alfonso 
35111cd3cd6SAndrey Konovalov 	start_report(&flags);
35211cd3cd6SAndrey Konovalov 	pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
353*f00748bfSAndrey Konovalov 	kasan_print_tags(tag, object);
35411cd3cd6SAndrey Konovalov 	pr_err("\n");
355ae8f06b3SWalter Wu 	print_address_description(object, tag);
35611cd3cd6SAndrey Konovalov 	pr_err("\n");
357db3de8f7SAndrey Konovalov 	print_memory_metadata(object);
35811cd3cd6SAndrey Konovalov 	end_report(&flags);
35911cd3cd6SAndrey Konovalov }
36011cd3cd6SAndrey Konovalov 
3614fba3758SAndrey Konovalov static void __kasan_report(unsigned long addr, size_t size, bool is_write,
3624fba3758SAndrey Konovalov 				unsigned long ip)
3630b24beccSAndrey Ryabinin {
3640b24beccSAndrey Ryabinin 	struct kasan_access_info info;
365121e8f81SAndrey Konovalov 	void *tagged_addr;
366121e8f81SAndrey Konovalov 	void *untagged_addr;
367121e8f81SAndrey Konovalov 	unsigned long flags;
3680b24beccSAndrey Ryabinin 
36983c4e7a0SPatricia Alfonso #if IS_ENABLED(CONFIG_KUNIT)
37083c4e7a0SPatricia Alfonso 	if (current->kunit_test)
37183c4e7a0SPatricia Alfonso 		kasan_update_kunit_status(current->kunit_test);
37283c4e7a0SPatricia Alfonso #endif /* IS_ENABLED(CONFIG_KUNIT) */
37383c4e7a0SPatricia Alfonso 
3744f40c6e5SPeter Zijlstra 	disable_trace_on_warning();
3754f40c6e5SPeter Zijlstra 
376121e8f81SAndrey Konovalov 	tagged_addr = (void *)addr;
377c0054c56SAndrey Konovalov 	untagged_addr = kasan_reset_tag(tagged_addr);
378121e8f81SAndrey Konovalov 
379121e8f81SAndrey Konovalov 	info.access_addr = tagged_addr;
3806882464fSAndrey Konovalov 	if (addr_has_metadata(untagged_addr))
381*f00748bfSAndrey Konovalov 		info.first_bad_addr =
382*f00748bfSAndrey Konovalov 			kasan_find_first_bad_addr(tagged_addr, size);
383121e8f81SAndrey Konovalov 	else
384121e8f81SAndrey Konovalov 		info.first_bad_addr = untagged_addr;
3850b24beccSAndrey Ryabinin 	info.access_size = size;
3860b24beccSAndrey Ryabinin 	info.is_write = is_write;
3870b24beccSAndrey Ryabinin 	info.ip = ip;
388e9121076SAndrey Konovalov 
389121e8f81SAndrey Konovalov 	start_report(&flags);
390121e8f81SAndrey Konovalov 
391121e8f81SAndrey Konovalov 	print_error_description(&info);
3926882464fSAndrey Konovalov 	if (addr_has_metadata(untagged_addr))
393*f00748bfSAndrey Konovalov 		kasan_print_tags(get_tag(tagged_addr), info.first_bad_addr);
394121e8f81SAndrey Konovalov 	pr_err("\n");
395121e8f81SAndrey Konovalov 
3966882464fSAndrey Konovalov 	if (addr_has_metadata(untagged_addr)) {
397ae8f06b3SWalter Wu 		print_address_description(untagged_addr, get_tag(tagged_addr));
398121e8f81SAndrey Konovalov 		pr_err("\n");
399db3de8f7SAndrey Konovalov 		print_memory_metadata(info.first_bad_addr);
400121e8f81SAndrey Konovalov 	} else {
401121e8f81SAndrey Konovalov 		dump_stack();
402121e8f81SAndrey Konovalov 	}
403121e8f81SAndrey Konovalov 
404121e8f81SAndrey Konovalov 	end_report(&flags);
4050b24beccSAndrey Ryabinin }
4062f004eeaSJann Horn 
4074fba3758SAndrey Konovalov bool kasan_report(unsigned long addr, size_t size, bool is_write,
4084fba3758SAndrey Konovalov 			unsigned long ip)
4094fba3758SAndrey Konovalov {
4104fba3758SAndrey Konovalov 	unsigned long flags = user_access_save();
4114fba3758SAndrey Konovalov 	bool ret = false;
4124fba3758SAndrey Konovalov 
4134fba3758SAndrey Konovalov 	if (likely(report_enabled())) {
4144fba3758SAndrey Konovalov 		__kasan_report(addr, size, is_write, ip);
4154fba3758SAndrey Konovalov 		ret = true;
4164fba3758SAndrey Konovalov 	}
4174fba3758SAndrey Konovalov 
4184fba3758SAndrey Konovalov 	user_access_restore(flags);
4194fba3758SAndrey Konovalov 
4204fba3758SAndrey Konovalov 	return ret;
4214fba3758SAndrey Konovalov }
4224fba3758SAndrey Konovalov 
4232f004eeaSJann Horn #ifdef CONFIG_KASAN_INLINE
4242f004eeaSJann Horn /*
4252f004eeaSJann Horn  * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
4262f004eeaSJann Horn  * canonical half of the address space) cause out-of-bounds shadow memory reads
4272f004eeaSJann Horn  * before the actual access. For addresses in the low canonical half of the
4282f004eeaSJann Horn  * address space, as well as most non-canonical addresses, that out-of-bounds
4292f004eeaSJann Horn  * shadow memory access lands in the non-canonical part of the address space.
4302f004eeaSJann Horn  * Help the user figure out what the original bogus pointer was.
4312f004eeaSJann Horn  */
4322f004eeaSJann Horn void kasan_non_canonical_hook(unsigned long addr)
4332f004eeaSJann Horn {
4342f004eeaSJann Horn 	unsigned long orig_addr;
4352f004eeaSJann Horn 	const char *bug_type;
4362f004eeaSJann Horn 
4372f004eeaSJann Horn 	if (addr < KASAN_SHADOW_OFFSET)
4382f004eeaSJann Horn 		return;
4392f004eeaSJann Horn 
4402f004eeaSJann Horn 	orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
4412f004eeaSJann Horn 	/*
4422f004eeaSJann Horn 	 * For faults near the shadow address for NULL, we can be fairly certain
4432f004eeaSJann Horn 	 * that this is a KASAN shadow memory access.
4442f004eeaSJann Horn 	 * For faults that correspond to shadow for low canonical addresses, we
4452f004eeaSJann Horn 	 * can still be pretty sure - that shadow region is a fairly narrow
4462f004eeaSJann Horn 	 * chunk of the non-canonical address space.
4472f004eeaSJann Horn 	 * But faults that look like shadow for non-canonical addresses are a
4482f004eeaSJann Horn 	 * really large chunk of the address space. In that case, we still
4492f004eeaSJann Horn 	 * print the decoded address, but make it clear that this is not
4502f004eeaSJann Horn 	 * necessarily what's actually going on.
4512f004eeaSJann Horn 	 */
4522f004eeaSJann Horn 	if (orig_addr < PAGE_SIZE)
4532f004eeaSJann Horn 		bug_type = "null-ptr-deref";
4542f004eeaSJann Horn 	else if (orig_addr < TASK_SIZE)
4552f004eeaSJann Horn 		bug_type = "probably user-memory-access";
4562f004eeaSJann Horn 	else
4572f004eeaSJann Horn 		bug_type = "maybe wild-memory-access";
4582f004eeaSJann Horn 	pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
4591f600626SAndrey Konovalov 		 orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1);
4602f004eeaSJann Horn }
4612f004eeaSJann Horn #endif
462