xref: /openbmc/linux/mm/kasan/report.c (revision c068664c97c7cffa9df706e247046aa5c796efc9)
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 
87c965cdd6SAndrey Konovalov static void print_error_description(struct kasan_report_info *info)
885e82cd12SAndrey Konovalov {
8931c65110SAndrey Konovalov 	if (info->type == KASAN_REPORT_INVALID_FREE) {
9031c65110SAndrey Konovalov 		pr_err("BUG: KASAN: double-free or invalid-free in %pS\n",
9131c65110SAndrey Konovalov 		       (void *)info->ip);
9231c65110SAndrey Konovalov 		return;
9331c65110SAndrey Konovalov 	}
9431c65110SAndrey Konovalov 
957f0a84c2SAndrey Konovalov 	pr_err("BUG: KASAN: %s in %pS\n",
96f00748bfSAndrey Konovalov 		kasan_get_bug_type(info), (void *)info->ip);
974291e9eeSAndrey Konovalov 	if (info->access_size)
986424f6bbSTobin C. Harding 		pr_err("%s of size %zu at addr %px by task %s/%d\n",
997d418f7bSAndrey Konovalov 			info->is_write ? "Write" : "Read", info->access_size,
1007f0a84c2SAndrey Konovalov 			info->access_addr, current->comm, task_pid_nr(current));
1014291e9eeSAndrey Konovalov 	else
1024291e9eeSAndrey Konovalov 		pr_err("%s at addr %px by task %s/%d\n",
1034291e9eeSAndrey Konovalov 			info->is_write ? "Write" : "Read",
1044291e9eeSAndrey Konovalov 			info->access_addr, current->comm, task_pid_nr(current));
1050b24beccSAndrey Ryabinin }
1060b24beccSAndrey Ryabinin 
107a260d281SAndrey Konovalov #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST)
108a260d281SAndrey Konovalov static void update_kunit_status(bool sync)
109a260d281SAndrey Konovalov {
110a260d281SAndrey Konovalov 	struct kunit *test;
111a260d281SAndrey Konovalov 	struct kunit_resource *resource;
112a260d281SAndrey Konovalov 	struct kunit_kasan_status *status;
113a260d281SAndrey Konovalov 
114a260d281SAndrey Konovalov 	test = current->kunit_test;
115a260d281SAndrey Konovalov 	if (!test)
116a260d281SAndrey Konovalov 		return;
117a260d281SAndrey Konovalov 
118a260d281SAndrey Konovalov 	resource = kunit_find_named_resource(test, "kasan_status");
119a260d281SAndrey Konovalov 	if (!resource) {
120a260d281SAndrey Konovalov 		kunit_set_failure(test);
121a260d281SAndrey Konovalov 		return;
122a260d281SAndrey Konovalov 	}
123a260d281SAndrey Konovalov 
124a260d281SAndrey Konovalov 	status = (struct kunit_kasan_status *)resource->data;
125a260d281SAndrey Konovalov 	WRITE_ONCE(status->report_found, true);
126a260d281SAndrey Konovalov 	WRITE_ONCE(status->sync_fault, sync);
127a260d281SAndrey Konovalov 
128a260d281SAndrey Konovalov 	kunit_put_resource(resource);
129a260d281SAndrey Konovalov }
130a260d281SAndrey Konovalov #else
131a260d281SAndrey Konovalov static void update_kunit_status(bool sync) { }
132a260d281SAndrey Konovalov #endif
133a260d281SAndrey Konovalov 
1347e088978SAndrey Ryabinin static DEFINE_SPINLOCK(report_lock);
1357e088978SAndrey Ryabinin 
136a260d281SAndrey Konovalov static void start_report(unsigned long *flags, bool sync)
1377e088978SAndrey Ryabinin {
1380a6e8a07SAndrey Konovalov 	/* Respect the /proc/sys/kernel/traceoff_on_warning interface. */
1390a6e8a07SAndrey Konovalov 	disable_trace_on_warning();
140a260d281SAndrey Konovalov 	/* Update status of the currently running KASAN test. */
141a260d281SAndrey Konovalov 	update_kunit_status(sync);
142a260d281SAndrey Konovalov 	/* Make sure we don't end up in loop. */
1437e088978SAndrey Ryabinin 	kasan_disable_current();
1447e088978SAndrey Ryabinin 	spin_lock_irqsave(&report_lock, *flags);
1457e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
1467e088978SAndrey Ryabinin }
1477e088978SAndrey Ryabinin 
1489d7b7dd9SAndrey Konovalov static void end_report(unsigned long *flags, void *addr)
1497e088978SAndrey Ryabinin {
150476b1dc2SAndrey Konovalov 	if (addr)
1519d7b7dd9SAndrey Konovalov 		trace_error_report_end(ERROR_DETECTOR_KASAN,
1529d7b7dd9SAndrey Konovalov 				       (unsigned long)addr);
1537e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
1547e088978SAndrey Ryabinin 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
1557e088978SAndrey Ryabinin 	spin_unlock_irqrestore(&report_lock, *flags);
156e7ce7500STiezhu Yang 	if (panic_on_warn && !test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
1575c5c1f36SDmitry Vyukov 		panic("panic_on_warn set ...\n");
158c9d1af2bSWoody Lin 	if (kasan_arg_fault == KASAN_ARG_FAULT_PANIC)
1598028caacSAndrey Konovalov 		panic("kasan.fault=panic set ...\n");
1607e088978SAndrey Ryabinin 	kasan_enable_current();
1617e088978SAndrey Ryabinin }
1627e088978SAndrey Ryabinin 
163b6b72f49SAndrey Konovalov static void print_track(struct kasan_track *track, const char *prefix)
1647ed2f9e6SAlexander Potapenko {
165b6b72f49SAndrey Konovalov 	pr_err("%s by task %u:\n", prefix, track->pid);
166cd11016eSAlexander Potapenko 	if (track->stack) {
167505be481SImran Khan 		stack_depot_print(track->stack);
168cd11016eSAlexander Potapenko 	} else {
169cd11016eSAlexander Potapenko 		pr_err("(stack is not available)\n");
170cd11016eSAlexander Potapenko 	}
1717ed2f9e6SAlexander Potapenko }
1727ed2f9e6SAlexander Potapenko 
173ae8f06b3SWalter Wu struct page *kasan_addr_to_page(const void *addr)
174db429f16SAndrey Konovalov {
175db429f16SAndrey Konovalov 	if ((addr >= (void *)PAGE_OFFSET) &&
176db429f16SAndrey Konovalov 			(addr < high_memory))
177db429f16SAndrey Konovalov 		return virt_to_head_page(addr);
178db429f16SAndrey Konovalov 	return NULL;
179db429f16SAndrey Konovalov }
180db429f16SAndrey Konovalov 
1816e48a966SMatthew Wilcox (Oracle) struct slab *kasan_addr_to_slab(const void *addr)
1826e48a966SMatthew Wilcox (Oracle) {
1836e48a966SMatthew Wilcox (Oracle) 	if ((addr >= (void *)PAGE_OFFSET) &&
1846e48a966SMatthew Wilcox (Oracle) 			(addr < high_memory))
1856e48a966SMatthew Wilcox (Oracle) 		return virt_to_slab(addr);
1866e48a966SMatthew Wilcox (Oracle) 	return NULL;
1876e48a966SMatthew Wilcox (Oracle) }
1886e48a966SMatthew Wilcox (Oracle) 
1890c06f1f8SAndrey Konovalov static void describe_object_addr(struct kmem_cache *cache, void *object,
1900c06f1f8SAndrey Konovalov 				const void *addr)
1910c06f1f8SAndrey Konovalov {
1920c06f1f8SAndrey Konovalov 	unsigned long access_addr = (unsigned long)addr;
1930c06f1f8SAndrey Konovalov 	unsigned long object_addr = (unsigned long)object;
1940c06f1f8SAndrey Konovalov 	const char *rel_type;
1950c06f1f8SAndrey Konovalov 	int rel_bytes;
1960c06f1f8SAndrey Konovalov 
1976424f6bbSTobin C. Harding 	pr_err("The buggy address belongs to the object at %px\n"
1980c06f1f8SAndrey Konovalov 	       " which belongs to the cache %s of size %d\n",
1990c06f1f8SAndrey Konovalov 		object, cache->name, cache->object_size);
2000c06f1f8SAndrey Konovalov 
2010c06f1f8SAndrey Konovalov 	if (access_addr < object_addr) {
2020c06f1f8SAndrey Konovalov 		rel_type = "to the left";
2030c06f1f8SAndrey Konovalov 		rel_bytes = object_addr - access_addr;
2040c06f1f8SAndrey Konovalov 	} else if (access_addr >= object_addr + cache->object_size) {
2050c06f1f8SAndrey Konovalov 		rel_type = "to the right";
2060c06f1f8SAndrey Konovalov 		rel_bytes = access_addr - (object_addr + cache->object_size);
2070c06f1f8SAndrey Konovalov 	} else {
2080c06f1f8SAndrey Konovalov 		rel_type = "inside";
2090c06f1f8SAndrey Konovalov 		rel_bytes = access_addr - object_addr;
2100c06f1f8SAndrey Konovalov 	}
2110c06f1f8SAndrey Konovalov 
2120c06f1f8SAndrey Konovalov 	pr_err("The buggy address is located %d bytes %s of\n"
2136424f6bbSTobin C. Harding 	       " %d-byte region [%px, %px)\n",
2140c06f1f8SAndrey Konovalov 		rel_bytes, rel_type, cache->object_size, (void *)object_addr,
2150c06f1f8SAndrey Konovalov 		(void *)(object_addr + cache->object_size));
2160c06f1f8SAndrey Konovalov }
2170c06f1f8SAndrey Konovalov 
2188028caacSAndrey Konovalov static void describe_object_stacks(struct kmem_cache *cache, void *object,
219ae8f06b3SWalter Wu 					const void *addr, u8 tag)
2207ed2f9e6SAlexander Potapenko {
22197593cadSAndrey Konovalov 	struct kasan_alloc_meta *alloc_meta;
222ae8f06b3SWalter Wu 	struct kasan_track *free_track;
223ae8f06b3SWalter Wu 
22497593cadSAndrey Konovalov 	alloc_meta = kasan_get_alloc_meta(cache, object);
22597593cadSAndrey Konovalov 	if (alloc_meta) {
2266476792fSAndrey Konovalov 		print_track(&alloc_meta->alloc_track, "Allocated");
227b1938599SAndrey Konovalov 		pr_err("\n");
22897593cadSAndrey Konovalov 	}
22997593cadSAndrey Konovalov 
230ae8f06b3SWalter Wu 	free_track = kasan_get_free_track(cache, object, tag);
231e4b7818bSWalter Wu 	if (free_track) {
232ae8f06b3SWalter Wu 		print_track(free_track, "Freed");
233b1938599SAndrey Konovalov 		pr_err("\n");
234e4b7818bSWalter Wu 	}
23526e760c9SWalter Wu 
23626e760c9SWalter Wu #ifdef CONFIG_KASAN_GENERIC
23797593cadSAndrey Konovalov 	if (!alloc_meta)
23897593cadSAndrey Konovalov 		return;
2396476792fSAndrey Konovalov 	if (alloc_meta->aux_stack[0]) {
240ef133461SWalter Wu 		pr_err("Last potentially related work creation:\n");
241505be481SImran Khan 		stack_depot_print(alloc_meta->aux_stack[0]);
24226e760c9SWalter Wu 		pr_err("\n");
24326e760c9SWalter Wu 	}
2446476792fSAndrey Konovalov 	if (alloc_meta->aux_stack[1]) {
245ef133461SWalter Wu 		pr_err("Second to last potentially related work creation:\n");
246505be481SImran Khan 		stack_depot_print(alloc_meta->aux_stack[1]);
24726e760c9SWalter Wu 		pr_err("\n");
24826e760c9SWalter Wu 	}
24926e760c9SWalter Wu #endif
2507ed2f9e6SAlexander Potapenko }
2517ed2f9e6SAlexander Potapenko 
2528028caacSAndrey Konovalov static void describe_object(struct kmem_cache *cache, void *object,
2538028caacSAndrey Konovalov 				const void *addr, u8 tag)
2548028caacSAndrey Konovalov {
2558028caacSAndrey Konovalov 	if (kasan_stack_collection_enabled())
2568028caacSAndrey Konovalov 		describe_object_stacks(cache, object, addr, tag);
2570c06f1f8SAndrey Konovalov 	describe_object_addr(cache, object, addr);
2580c06f1f8SAndrey Konovalov }
2590c06f1f8SAndrey Konovalov 
26011cd3cd6SAndrey Konovalov static inline bool kernel_or_module_addr(const void *addr)
26111cd3cd6SAndrey Konovalov {
2623298cbe8SKefeng Wang 	if (is_kernel((unsigned long)addr))
26311cd3cd6SAndrey Konovalov 		return true;
26411cd3cd6SAndrey Konovalov 	if (is_module_address((unsigned long)addr))
26511cd3cd6SAndrey Konovalov 		return true;
26611cd3cd6SAndrey Konovalov 	return false;
26711cd3cd6SAndrey Konovalov }
26811cd3cd6SAndrey Konovalov 
26911cd3cd6SAndrey Konovalov static inline bool init_task_stack_addr(const void *addr)
27011cd3cd6SAndrey Konovalov {
27111cd3cd6SAndrey Konovalov 	return addr >= (void *)&init_thread_union.stack &&
27211cd3cd6SAndrey Konovalov 		(addr <= (void *)&init_thread_union.stack +
27311cd3cd6SAndrey Konovalov 			sizeof(init_thread_union.stack));
27411cd3cd6SAndrey Konovalov }
27511cd3cd6SAndrey Konovalov 
276ae8f06b3SWalter Wu static void print_address_description(void *addr, u8 tag)
2777e088978SAndrey Ryabinin {
278ae8f06b3SWalter Wu 	struct page *page = kasan_addr_to_page(addr);
279b8c73fc2SAndrey Ryabinin 
280336abff6SAlexander Potapenko 	dump_stack_lvl(KERN_ERR);
281b1938599SAndrey Konovalov 	pr_err("\n");
282db429f16SAndrey Konovalov 
283db429f16SAndrey Konovalov 	if (page && PageSlab(page)) {
2846e48a966SMatthew Wilcox (Oracle) 		struct slab *slab = page_slab(page);
2856e48a966SMatthew Wilcox (Oracle) 		struct kmem_cache *cache = slab->slab_cache;
2866e48a966SMatthew Wilcox (Oracle) 		void *object = nearest_obj(cache, slab,	addr);
287db429f16SAndrey Konovalov 
288ae8f06b3SWalter Wu 		describe_object(cache, object, addr, tag);
289038fd2b4SAndrey Konovalov 		pr_err("\n");
290b8c73fc2SAndrey Ryabinin 	}
291b8c73fc2SAndrey Ryabinin 
292430a05f9SAndrey Konovalov 	if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
293430a05f9SAndrey Konovalov 		pr_err("The buggy address belongs to the variable:\n");
294430a05f9SAndrey Konovalov 		pr_err(" %pS\n", addr);
295038fd2b4SAndrey Konovalov 		pr_err("\n");
296430a05f9SAndrey Konovalov 	}
297430a05f9SAndrey Konovalov 
2980f9b35f3SAndrey Konovalov 	if (object_is_on_stack(addr)) {
2990f9b35f3SAndrey Konovalov 		/*
3000f9b35f3SAndrey Konovalov 		 * Currently, KASAN supports printing frame information only
3010f9b35f3SAndrey Konovalov 		 * for accesses to the task's own stack.
3020f9b35f3SAndrey Konovalov 		 */
3030f9b35f3SAndrey Konovalov 		kasan_print_address_stack_frame(addr);
3040f9b35f3SAndrey Konovalov 		pr_err("\n");
3050f9b35f3SAndrey Konovalov 	}
3060f9b35f3SAndrey Konovalov 
307c056a364SAndrey Konovalov 	if (is_vmalloc_addr(addr)) {
308c056a364SAndrey Konovalov 		struct vm_struct *va = find_vm_area(addr);
309c056a364SAndrey Konovalov 
310c056a364SAndrey Konovalov 		if (va) {
311c056a364SAndrey Konovalov 			pr_err("The buggy address belongs to the virtual mapping at\n"
312c056a364SAndrey Konovalov 			       " [%px, %px) created by:\n"
313c056a364SAndrey Konovalov 			       " %pS\n",
314c056a364SAndrey Konovalov 			       va->addr, va->addr + va->size, va->caller);
315038fd2b4SAndrey Konovalov 			pr_err("\n");
316c056a364SAndrey Konovalov 
317c056a364SAndrey Konovalov 			page = vmalloc_to_page(page);
318c056a364SAndrey Konovalov 		}
319c056a364SAndrey Konovalov 	}
320c056a364SAndrey Konovalov 
321430a05f9SAndrey Konovalov 	if (page) {
322c056a364SAndrey Konovalov 		pr_err("The buggy address belongs to the physical page:\n");
323430a05f9SAndrey Konovalov 		dump_page(page, "kasan: bad access detected");
324038fd2b4SAndrey Konovalov 		pr_err("\n");
325bebf56a1SAndrey Ryabinin 	}
3260b24beccSAndrey Ryabinin }
3270b24beccSAndrey Ryabinin 
32896e0279dSAndrey Konovalov static bool meta_row_is_guilty(const void *row, const void *addr)
3290b24beccSAndrey Ryabinin {
33096e0279dSAndrey Konovalov 	return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW);
3310b24beccSAndrey Ryabinin }
3320b24beccSAndrey Ryabinin 
33396e0279dSAndrey Konovalov static int meta_pointer_offset(const void *row, const void *addr)
3340b24beccSAndrey Ryabinin {
33596e0279dSAndrey Konovalov 	/*
33696e0279dSAndrey Konovalov 	 * Memory state around the buggy address:
33796e0279dSAndrey Konovalov 	 *  ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe
33896e0279dSAndrey Konovalov 	 *  ...
33996e0279dSAndrey Konovalov 	 *
34096e0279dSAndrey Konovalov 	 * The length of ">ff00ff00ff00ff00: " is
3410b24beccSAndrey Ryabinin 	 *    3 + (BITS_PER_LONG / 8) * 2 chars.
34296e0279dSAndrey Konovalov 	 * The length of each granule metadata is 2 bytes
34396e0279dSAndrey Konovalov 	 *    plus 1 byte for space.
3440b24beccSAndrey Ryabinin 	 */
34596e0279dSAndrey Konovalov 	return 3 + (BITS_PER_LONG / 8) * 2 +
34696e0279dSAndrey Konovalov 		(addr - row) / KASAN_GRANULE_SIZE * 3 + 1;
3470b24beccSAndrey Ryabinin }
3480b24beccSAndrey Ryabinin 
349db3de8f7SAndrey Konovalov static void print_memory_metadata(const void *addr)
3500b24beccSAndrey Ryabinin {
3510b24beccSAndrey Ryabinin 	int i;
35296e0279dSAndrey Konovalov 	void *row;
3530b24beccSAndrey Ryabinin 
35496e0279dSAndrey Konovalov 	row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW)
35596e0279dSAndrey Konovalov 			- META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW;
3560b24beccSAndrey Ryabinin 
3570b24beccSAndrey Ryabinin 	pr_err("Memory state around the buggy address:\n");
3580b24beccSAndrey Ryabinin 
35988b86597SAndrey Konovalov 	for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
3600b24beccSAndrey Ryabinin 		char buffer[4 + (BITS_PER_LONG / 8) * 2];
36196e0279dSAndrey Konovalov 		char metadata[META_BYTES_PER_ROW];
3620b24beccSAndrey Ryabinin 
3630b24beccSAndrey Ryabinin 		snprintf(buffer, sizeof(buffer),
36496e0279dSAndrey Konovalov 				(i == 0) ? ">%px: " : " %px: ", row);
36596e0279dSAndrey Konovalov 
366f2377d4eSAneesh Kumar K.V 		/*
367f2377d4eSAneesh Kumar K.V 		 * We should not pass a shadow pointer to generic
368f2377d4eSAneesh Kumar K.V 		 * function, because generic functions may try to
369f2377d4eSAneesh Kumar K.V 		 * access kasan mapping for the passed address.
370f2377d4eSAneesh Kumar K.V 		 */
371f00748bfSAndrey Konovalov 		kasan_metadata_fetch_row(&metadata[0], row);
37296e0279dSAndrey Konovalov 
3730b24beccSAndrey Ryabinin 		print_hex_dump(KERN_ERR, buffer,
37488b86597SAndrey Konovalov 			DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
37596e0279dSAndrey Konovalov 			metadata, META_BYTES_PER_ROW, 0);
3760b24beccSAndrey Ryabinin 
37796e0279dSAndrey Konovalov 		if (meta_row_is_guilty(row, addr))
37896e0279dSAndrey Konovalov 			pr_err("%*c\n", meta_pointer_offset(row, addr), '^');
3790b24beccSAndrey Ryabinin 
38096e0279dSAndrey Konovalov 		row += META_MEM_BYTES_PER_ROW;
3810b24beccSAndrey Ryabinin 	}
3820b24beccSAndrey Ryabinin }
3830b24beccSAndrey Ryabinin 
384*c068664cSAndrey Konovalov /*
385*c068664cSAndrey Konovalov  * Used to suppress reports within kasan_disable/enable_current() critical
386*c068664cSAndrey Konovalov  * sections, which are used for marking accesses to slab metadata.
387*c068664cSAndrey Konovalov  */
388*c068664cSAndrey Konovalov static bool report_suppressed(void)
389b0845ce5SMark Rutland {
390d73b4936SAndrey Konovalov #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
391b0845ce5SMark Rutland 	if (current->kasan_depth)
392*c068664cSAndrey Konovalov 		return true;
393d73b4936SAndrey Konovalov #endif
394*c068664cSAndrey Konovalov 	return false;
395*c068664cSAndrey Konovalov }
396*c068664cSAndrey Konovalov 
397*c068664cSAndrey Konovalov /*
398*c068664cSAndrey Konovalov  * Used to avoid reporting more than one KASAN bug unless kasan_multi_shot
399*c068664cSAndrey Konovalov  * is enabled. Note that KASAN tests effectively enable kasan_multi_shot
400*c068664cSAndrey Konovalov  * for their duration.
401*c068664cSAndrey Konovalov  */
402*c068664cSAndrey Konovalov static bool report_enabled(void)
403*c068664cSAndrey Konovalov {
404b0845ce5SMark Rutland 	if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
405b0845ce5SMark Rutland 		return true;
406b0845ce5SMark Rutland 	return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
407b0845ce5SMark Rutland }
408b0845ce5SMark Rutland 
409c965cdd6SAndrey Konovalov static void print_report(struct kasan_report_info *info)
4109d7b7dd9SAndrey Konovalov {
4119d7b7dd9SAndrey Konovalov 	void *tagged_addr = info->access_addr;
4129d7b7dd9SAndrey Konovalov 	void *untagged_addr = kasan_reset_tag(tagged_addr);
4139d7b7dd9SAndrey Konovalov 	u8 tag = get_tag(tagged_addr);
4149d7b7dd9SAndrey Konovalov 
4159d7b7dd9SAndrey Konovalov 	print_error_description(info);
4169d7b7dd9SAndrey Konovalov 	if (addr_has_metadata(untagged_addr))
4179d7b7dd9SAndrey Konovalov 		kasan_print_tags(tag, info->first_bad_addr);
4189d7b7dd9SAndrey Konovalov 	pr_err("\n");
4199d7b7dd9SAndrey Konovalov 
4209d7b7dd9SAndrey Konovalov 	if (addr_has_metadata(untagged_addr)) {
4219d7b7dd9SAndrey Konovalov 		print_address_description(untagged_addr, tag);
4229d7b7dd9SAndrey Konovalov 		print_memory_metadata(info->first_bad_addr);
4239d7b7dd9SAndrey Konovalov 	} else {
4249d7b7dd9SAndrey Konovalov 		dump_stack_lvl(KERN_ERR);
4259d7b7dd9SAndrey Konovalov 	}
4269d7b7dd9SAndrey Konovalov }
4279d7b7dd9SAndrey Konovalov 
42831c65110SAndrey Konovalov void kasan_report_invalid_free(void *ptr, unsigned long ip)
42931c65110SAndrey Konovalov {
43031c65110SAndrey Konovalov 	unsigned long flags;
431c965cdd6SAndrey Konovalov 	struct kasan_report_info info;
43231c65110SAndrey Konovalov 
433*c068664cSAndrey Konovalov 	/*
434*c068664cSAndrey Konovalov 	 * Do not check report_suppressed(), as an invalid-free cannot be
435*c068664cSAndrey Konovalov 	 * caused by accessing slab metadata and thus should not be
436*c068664cSAndrey Konovalov 	 * suppressed by kasan_disable/enable_current() critical sections.
437*c068664cSAndrey Konovalov 	 */
438*c068664cSAndrey Konovalov 	if (unlikely(!report_enabled()))
439*c068664cSAndrey Konovalov 		return;
440*c068664cSAndrey Konovalov 
44131c65110SAndrey Konovalov 	start_report(&flags, true);
44231c65110SAndrey Konovalov 
44331c65110SAndrey Konovalov 	info.type = KASAN_REPORT_INVALID_FREE;
44431c65110SAndrey Konovalov 	info.access_addr = ptr;
44531c65110SAndrey Konovalov 	info.first_bad_addr = kasan_reset_tag(ptr);
44631c65110SAndrey Konovalov 	info.access_size = 0;
44731c65110SAndrey Konovalov 	info.is_write = false;
44831c65110SAndrey Konovalov 	info.ip = ip;
44931c65110SAndrey Konovalov 
45031c65110SAndrey Konovalov 	print_report(&info);
45131c65110SAndrey Konovalov 
45231c65110SAndrey Konovalov 	end_report(&flags, ptr);
45331c65110SAndrey Konovalov }
45431c65110SAndrey Konovalov 
455795b760fSAndrey Konovalov /*
456795b760fSAndrey Konovalov  * kasan_report() is the only reporting function that uses
457795b760fSAndrey Konovalov  * user_access_save/restore(): kasan_report_invalid_free() cannot be called
458795b760fSAndrey Konovalov  * from a UACCESS region, and kasan_report_async() is not used on x86.
459795b760fSAndrey Konovalov  */
4604fba3758SAndrey Konovalov bool kasan_report(unsigned long addr, size_t size, bool is_write,
4614fba3758SAndrey Konovalov 			unsigned long ip)
4624fba3758SAndrey Konovalov {
463b3bb1d70SAndrey Konovalov 	bool ret = true;
464be8631a1SAndrey Konovalov 	void *ptr = (void *)addr;
465be8631a1SAndrey Konovalov 	unsigned long ua_flags = user_access_save();
466be8631a1SAndrey Konovalov 	unsigned long irq_flags;
467c965cdd6SAndrey Konovalov 	struct kasan_report_info info;
4684fba3758SAndrey Konovalov 
469*c068664cSAndrey Konovalov 	if (unlikely(report_suppressed()) || unlikely(!report_enabled())) {
470b3bb1d70SAndrey Konovalov 		ret = false;
471b3bb1d70SAndrey Konovalov 		goto out;
4724fba3758SAndrey Konovalov 	}
4734fba3758SAndrey Konovalov 
474be8631a1SAndrey Konovalov 	start_report(&irq_flags, true);
475be8631a1SAndrey Konovalov 
47631c65110SAndrey Konovalov 	info.type = KASAN_REPORT_ACCESS;
477be8631a1SAndrey Konovalov 	info.access_addr = ptr;
478be8631a1SAndrey Konovalov 	info.first_bad_addr = kasan_find_first_bad_addr(ptr, size);
479be8631a1SAndrey Konovalov 	info.access_size = size;
480be8631a1SAndrey Konovalov 	info.is_write = is_write;
481be8631a1SAndrey Konovalov 	info.ip = ip;
482be8631a1SAndrey Konovalov 
483be8631a1SAndrey Konovalov 	print_report(&info);
484be8631a1SAndrey Konovalov 
485be8631a1SAndrey Konovalov 	end_report(&irq_flags, ptr);
486b3bb1d70SAndrey Konovalov 
487b3bb1d70SAndrey Konovalov out:
488b3bb1d70SAndrey Konovalov 	user_access_restore(ua_flags);
4894fba3758SAndrey Konovalov 
4904fba3758SAndrey Konovalov 	return ret;
4914fba3758SAndrey Konovalov }
4924fba3758SAndrey Konovalov 
493bb2f967cSAndrey Konovalov #ifdef CONFIG_KASAN_HW_TAGS
494bb2f967cSAndrey Konovalov void kasan_report_async(void)
495bb2f967cSAndrey Konovalov {
496bb2f967cSAndrey Konovalov 	unsigned long flags;
497bb2f967cSAndrey Konovalov 
498*c068664cSAndrey Konovalov 	/*
499*c068664cSAndrey Konovalov 	 * Do not check report_suppressed(), as kasan_disable/enable_current()
500*c068664cSAndrey Konovalov 	 * critical sections do not affect Hardware Tag-Based KASAN.
501*c068664cSAndrey Konovalov 	 */
502*c068664cSAndrey Konovalov 	if (unlikely(!report_enabled()))
503*c068664cSAndrey Konovalov 		return;
504*c068664cSAndrey Konovalov 
505bb2f967cSAndrey Konovalov 	start_report(&flags, false);
506bb2f967cSAndrey Konovalov 	pr_err("BUG: KASAN: invalid-access\n");
507bb2f967cSAndrey Konovalov 	pr_err("Asynchronous fault: no details available\n");
508bb2f967cSAndrey Konovalov 	pr_err("\n");
509bb2f967cSAndrey Konovalov 	dump_stack_lvl(KERN_ERR);
510bb2f967cSAndrey Konovalov 	end_report(&flags, NULL);
511bb2f967cSAndrey Konovalov }
512bb2f967cSAndrey Konovalov #endif /* CONFIG_KASAN_HW_TAGS */
513bb2f967cSAndrey Konovalov 
5142f004eeaSJann Horn #ifdef CONFIG_KASAN_INLINE
5152f004eeaSJann Horn /*
5162f004eeaSJann Horn  * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
5172f004eeaSJann Horn  * canonical half of the address space) cause out-of-bounds shadow memory reads
5182f004eeaSJann Horn  * before the actual access. For addresses in the low canonical half of the
5192f004eeaSJann Horn  * address space, as well as most non-canonical addresses, that out-of-bounds
5202f004eeaSJann Horn  * shadow memory access lands in the non-canonical part of the address space.
5212f004eeaSJann Horn  * Help the user figure out what the original bogus pointer was.
5222f004eeaSJann Horn  */
5232f004eeaSJann Horn void kasan_non_canonical_hook(unsigned long addr)
5242f004eeaSJann Horn {
5252f004eeaSJann Horn 	unsigned long orig_addr;
5262f004eeaSJann Horn 	const char *bug_type;
5272f004eeaSJann Horn 
5282f004eeaSJann Horn 	if (addr < KASAN_SHADOW_OFFSET)
5292f004eeaSJann Horn 		return;
5302f004eeaSJann Horn 
5312f004eeaSJann Horn 	orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
5322f004eeaSJann Horn 	/*
5332f004eeaSJann Horn 	 * For faults near the shadow address for NULL, we can be fairly certain
5342f004eeaSJann Horn 	 * that this is a KASAN shadow memory access.
5352f004eeaSJann Horn 	 * For faults that correspond to shadow for low canonical addresses, we
5362f004eeaSJann Horn 	 * can still be pretty sure - that shadow region is a fairly narrow
5372f004eeaSJann Horn 	 * chunk of the non-canonical address space.
5382f004eeaSJann Horn 	 * But faults that look like shadow for non-canonical addresses are a
5392f004eeaSJann Horn 	 * really large chunk of the address space. In that case, we still
5402f004eeaSJann Horn 	 * print the decoded address, but make it clear that this is not
5412f004eeaSJann Horn 	 * necessarily what's actually going on.
5422f004eeaSJann Horn 	 */
5432f004eeaSJann Horn 	if (orig_addr < PAGE_SIZE)
5442f004eeaSJann Horn 		bug_type = "null-ptr-deref";
5452f004eeaSJann Horn 	else if (orig_addr < TASK_SIZE)
5462f004eeaSJann Horn 		bug_type = "probably user-memory-access";
5472f004eeaSJann Horn 	else
5482f004eeaSJann Horn 		bug_type = "maybe wild-memory-access";
5492f004eeaSJann Horn 	pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
5501f600626SAndrey Konovalov 		 orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1);
5512f004eeaSJann Horn }
5522f004eeaSJann Horn #endif
553