xref: /openbmc/linux/mm/kasan/report.c (revision be4f1ae978ffe98cc95ec49ceb95386fb4474974)
1e886bf9dSAndrey Konovalov // SPDX-License-Identifier: GPL-2.0
20b24beccSAndrey Ryabinin /*
311cd3cd6SAndrey Konovalov  * This file contains common generic and tag-based 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  * This program is free software; you can redistribute it and/or modify
120b24beccSAndrey Ryabinin  * it under the terms of the GNU General Public License version 2 as
130b24beccSAndrey Ryabinin  * published by the Free Software Foundation.
140b24beccSAndrey Ryabinin  *
150b24beccSAndrey Ryabinin  */
160b24beccSAndrey Ryabinin 
17b0845ce5SMark Rutland #include <linux/bitops.h>
184f40c6e5SPeter Zijlstra #include <linux/ftrace.h>
19b0845ce5SMark Rutland #include <linux/init.h>
200b24beccSAndrey Ryabinin #include <linux/kernel.h>
210b24beccSAndrey Ryabinin #include <linux/mm.h>
220b24beccSAndrey Ryabinin #include <linux/printk.h>
230b24beccSAndrey Ryabinin #include <linux/sched.h>
240b24beccSAndrey Ryabinin #include <linux/slab.h>
25cd11016eSAlexander Potapenko #include <linux/stackdepot.h>
260b24beccSAndrey Ryabinin #include <linux/stacktrace.h>
270b24beccSAndrey Ryabinin #include <linux/string.h>
280b24beccSAndrey Ryabinin #include <linux/types.h>
290b24beccSAndrey Ryabinin #include <linux/kasan.h>
30527f215bSAneesh Kumar K.V #include <linux/module.h>
31e8969219SMarco Elver #include <linux/sched/task_stack.h>
324fba3758SAndrey Konovalov #include <linux/uaccess.h>
330b24beccSAndrey Ryabinin 
34bebf56a1SAndrey Ryabinin #include <asm/sections.h>
35bebf56a1SAndrey Ryabinin 
3683c4e7a0SPatricia Alfonso #include <kunit/test.h>
3783c4e7a0SPatricia Alfonso 
380b24beccSAndrey Ryabinin #include "kasan.h"
390316bec2SAndrey Ryabinin #include "../slab.h"
400b24beccSAndrey Ryabinin 
410b24beccSAndrey Ryabinin /* Shadow layout customization. */
420b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_BLOCK 1
430b24beccSAndrey Ryabinin #define SHADOW_BLOCKS_PER_ROW 16
440b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK)
450b24beccSAndrey Ryabinin #define SHADOW_ROWS_AROUND_ADDR 2
460b24beccSAndrey Ryabinin 
4711cd3cd6SAndrey Konovalov static unsigned long kasan_flags;
4811cd3cd6SAndrey Konovalov 
4911cd3cd6SAndrey Konovalov #define KASAN_BIT_REPORTED	0
5011cd3cd6SAndrey Konovalov #define KASAN_BIT_MULTI_SHOT	1
5111cd3cd6SAndrey Konovalov 
5211cd3cd6SAndrey Konovalov bool kasan_save_enable_multi_shot(void)
530b24beccSAndrey Ryabinin {
5411cd3cd6SAndrey Konovalov 	return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
550b24beccSAndrey Ryabinin }
5611cd3cd6SAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
570b24beccSAndrey Ryabinin 
5811cd3cd6SAndrey Konovalov void kasan_restore_multi_shot(bool enabled)
595e82cd12SAndrey Konovalov {
6011cd3cd6SAndrey Konovalov 	if (!enabled)
6111cd3cd6SAndrey Konovalov 		clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
625e82cd12SAndrey Konovalov }
6311cd3cd6SAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
645e82cd12SAndrey Konovalov 
6511cd3cd6SAndrey Konovalov static int __init kasan_set_multi_shot(char *str)
660b24beccSAndrey Ryabinin {
6711cd3cd6SAndrey Konovalov 	set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
6811cd3cd6SAndrey Konovalov 	return 1;
690b24beccSAndrey Ryabinin }
7011cd3cd6SAndrey Konovalov __setup("kasan_multi_shot", kasan_set_multi_shot);
710b24beccSAndrey Ryabinin 
72121e8f81SAndrey Konovalov static void print_error_description(struct kasan_access_info *info)
735e82cd12SAndrey Konovalov {
747f0a84c2SAndrey Konovalov 	pr_err("BUG: KASAN: %s in %pS\n",
75121e8f81SAndrey Konovalov 		get_bug_type(info), (void *)info->ip);
766424f6bbSTobin C. Harding 	pr_err("%s of size %zu at addr %px by task %s/%d\n",
777d418f7bSAndrey Konovalov 		info->is_write ? "Write" : "Read", info->access_size,
787f0a84c2SAndrey Konovalov 		info->access_addr, current->comm, task_pid_nr(current));
790b24beccSAndrey Ryabinin }
800b24beccSAndrey Ryabinin 
817e088978SAndrey Ryabinin static DEFINE_SPINLOCK(report_lock);
827e088978SAndrey Ryabinin 
8311cd3cd6SAndrey Konovalov static void start_report(unsigned long *flags)
847e088978SAndrey Ryabinin {
857e088978SAndrey Ryabinin 	/*
867e088978SAndrey Ryabinin 	 * Make sure we don't end up in loop.
877e088978SAndrey Ryabinin 	 */
887e088978SAndrey Ryabinin 	kasan_disable_current();
897e088978SAndrey Ryabinin 	spin_lock_irqsave(&report_lock, *flags);
907e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
917e088978SAndrey Ryabinin }
927e088978SAndrey Ryabinin 
9311cd3cd6SAndrey Konovalov static void end_report(unsigned long *flags)
947e088978SAndrey Ryabinin {
957e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
967e088978SAndrey Ryabinin 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
977e088978SAndrey Ryabinin 	spin_unlock_irqrestore(&report_lock, *flags);
98*be4f1ae9SDavid Gow 	if (panic_on_warn && !test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) {
991d2252faSKees Cook 		/*
1001d2252faSKees Cook 		 * This thread may hit another WARN() in the panic path.
1011d2252faSKees Cook 		 * Resetting this prevents additional WARN() from panicking the
1021d2252faSKees Cook 		 * system on this thread.  Other threads are blocked by the
1031d2252faSKees Cook 		 * panic_mutex in panic().
1041d2252faSKees Cook 		 */
1051d2252faSKees Cook 		panic_on_warn = 0;
1065c5c1f36SDmitry Vyukov 		panic("panic_on_warn set ...\n");
1071d2252faSKees Cook 	}
1087e088978SAndrey Ryabinin 	kasan_enable_current();
1097e088978SAndrey Ryabinin }
1107e088978SAndrey Ryabinin 
11126e760c9SWalter Wu static void print_stack(depot_stack_handle_t stack)
11226e760c9SWalter Wu {
11326e760c9SWalter Wu 	unsigned long *entries;
11426e760c9SWalter Wu 	unsigned int nr_entries;
11526e760c9SWalter Wu 
11626e760c9SWalter Wu 	nr_entries = stack_depot_fetch(stack, &entries);
11726e760c9SWalter Wu 	stack_trace_print(entries, nr_entries, 0);
11826e760c9SWalter Wu }
11926e760c9SWalter Wu 
120b6b72f49SAndrey Konovalov static void print_track(struct kasan_track *track, const char *prefix)
1217ed2f9e6SAlexander Potapenko {
122b6b72f49SAndrey Konovalov 	pr_err("%s by task %u:\n", prefix, track->pid);
123cd11016eSAlexander Potapenko 	if (track->stack) {
12426e760c9SWalter Wu 		print_stack(track->stack);
125cd11016eSAlexander Potapenko 	} else {
126cd11016eSAlexander Potapenko 		pr_err("(stack is not available)\n");
127cd11016eSAlexander Potapenko 	}
1287ed2f9e6SAlexander Potapenko }
1297ed2f9e6SAlexander Potapenko 
130ae8f06b3SWalter Wu struct page *kasan_addr_to_page(const void *addr)
131db429f16SAndrey Konovalov {
132db429f16SAndrey Konovalov 	if ((addr >= (void *)PAGE_OFFSET) &&
133db429f16SAndrey Konovalov 			(addr < high_memory))
134db429f16SAndrey Konovalov 		return virt_to_head_page(addr);
135db429f16SAndrey Konovalov 	return NULL;
136db429f16SAndrey Konovalov }
137db429f16SAndrey Konovalov 
1380c06f1f8SAndrey Konovalov static void describe_object_addr(struct kmem_cache *cache, void *object,
1390c06f1f8SAndrey Konovalov 				const void *addr)
1400c06f1f8SAndrey Konovalov {
1410c06f1f8SAndrey Konovalov 	unsigned long access_addr = (unsigned long)addr;
1420c06f1f8SAndrey Konovalov 	unsigned long object_addr = (unsigned long)object;
1430c06f1f8SAndrey Konovalov 	const char *rel_type;
1440c06f1f8SAndrey Konovalov 	int rel_bytes;
1450c06f1f8SAndrey Konovalov 
1466424f6bbSTobin C. Harding 	pr_err("The buggy address belongs to the object at %px\n"
1470c06f1f8SAndrey Konovalov 	       " which belongs to the cache %s of size %d\n",
1480c06f1f8SAndrey Konovalov 		object, cache->name, cache->object_size);
1490c06f1f8SAndrey Konovalov 
1500c06f1f8SAndrey Konovalov 	if (!addr)
1510c06f1f8SAndrey Konovalov 		return;
1520c06f1f8SAndrey Konovalov 
1530c06f1f8SAndrey Konovalov 	if (access_addr < object_addr) {
1540c06f1f8SAndrey Konovalov 		rel_type = "to the left";
1550c06f1f8SAndrey Konovalov 		rel_bytes = object_addr - access_addr;
1560c06f1f8SAndrey Konovalov 	} else if (access_addr >= object_addr + cache->object_size) {
1570c06f1f8SAndrey Konovalov 		rel_type = "to the right";
1580c06f1f8SAndrey Konovalov 		rel_bytes = access_addr - (object_addr + cache->object_size);
1590c06f1f8SAndrey Konovalov 	} else {
1600c06f1f8SAndrey Konovalov 		rel_type = "inside";
1610c06f1f8SAndrey Konovalov 		rel_bytes = access_addr - object_addr;
1620c06f1f8SAndrey Konovalov 	}
1630c06f1f8SAndrey Konovalov 
1640c06f1f8SAndrey Konovalov 	pr_err("The buggy address is located %d bytes %s of\n"
1656424f6bbSTobin C. Harding 	       " %d-byte region [%px, %px)\n",
1660c06f1f8SAndrey Konovalov 		rel_bytes, rel_type, cache->object_size, (void *)object_addr,
1670c06f1f8SAndrey Konovalov 		(void *)(object_addr + cache->object_size));
1680c06f1f8SAndrey Konovalov }
1690c06f1f8SAndrey Konovalov 
1700c06f1f8SAndrey Konovalov static void describe_object(struct kmem_cache *cache, void *object,
171ae8f06b3SWalter Wu 				const void *addr, u8 tag)
1727ed2f9e6SAlexander Potapenko {
1737ed2f9e6SAlexander Potapenko 	struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
1747ed2f9e6SAlexander Potapenko 
1750c06f1f8SAndrey Konovalov 	if (cache->flags & SLAB_KASAN) {
176ae8f06b3SWalter Wu 		struct kasan_track *free_track;
177ae8f06b3SWalter Wu 
178b6b72f49SAndrey Konovalov 		print_track(&alloc_info->alloc_track, "Allocated");
179b1938599SAndrey Konovalov 		pr_err("\n");
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
18726e760c9SWalter Wu 		if (alloc_info->aux_stack[0]) {
18826e760c9SWalter Wu 			pr_err("Last call_rcu():\n");
18926e760c9SWalter Wu 			print_stack(alloc_info->aux_stack[0]);
19026e760c9SWalter Wu 			pr_err("\n");
19126e760c9SWalter Wu 		}
19226e760c9SWalter Wu 		if (alloc_info->aux_stack[1]) {
19326e760c9SWalter Wu 			pr_err("Second to last call_rcu():\n");
19426e760c9SWalter Wu 			print_stack(alloc_info->aux_stack[1]);
19526e760c9SWalter Wu 			pr_err("\n");
19626e760c9SWalter Wu 		}
19726e760c9SWalter Wu #endif
1987ed2f9e6SAlexander Potapenko 	}
1997ed2f9e6SAlexander Potapenko 
2000c06f1f8SAndrey Konovalov 	describe_object_addr(cache, object, addr);
2010c06f1f8SAndrey Konovalov }
2020c06f1f8SAndrey Konovalov 
20311cd3cd6SAndrey Konovalov static inline bool kernel_or_module_addr(const void *addr)
20411cd3cd6SAndrey Konovalov {
20511cd3cd6SAndrey Konovalov 	if (addr >= (void *)_stext && addr < (void *)_end)
20611cd3cd6SAndrey Konovalov 		return true;
20711cd3cd6SAndrey Konovalov 	if (is_module_address((unsigned long)addr))
20811cd3cd6SAndrey Konovalov 		return true;
20911cd3cd6SAndrey Konovalov 	return false;
21011cd3cd6SAndrey Konovalov }
21111cd3cd6SAndrey Konovalov 
21211cd3cd6SAndrey Konovalov static inline bool init_task_stack_addr(const void *addr)
21311cd3cd6SAndrey Konovalov {
21411cd3cd6SAndrey Konovalov 	return addr >= (void *)&init_thread_union.stack &&
21511cd3cd6SAndrey Konovalov 		(addr <= (void *)&init_thread_union.stack +
21611cd3cd6SAndrey Konovalov 			sizeof(init_thread_union.stack));
21711cd3cd6SAndrey Konovalov }
21811cd3cd6SAndrey Konovalov 
219e8969219SMarco Elver static bool __must_check tokenize_frame_descr(const char **frame_descr,
220e8969219SMarco Elver 					      char *token, size_t max_tok_len,
221e8969219SMarco Elver 					      unsigned long *value)
222e8969219SMarco Elver {
223e8969219SMarco Elver 	const char *sep = strchr(*frame_descr, ' ');
224e8969219SMarco Elver 
225e8969219SMarco Elver 	if (sep == NULL)
226e8969219SMarco Elver 		sep = *frame_descr + strlen(*frame_descr);
227e8969219SMarco Elver 
228e8969219SMarco Elver 	if (token != NULL) {
229e8969219SMarco Elver 		const size_t tok_len = sep - *frame_descr;
230e8969219SMarco Elver 
231e8969219SMarco Elver 		if (tok_len + 1 > max_tok_len) {
232e8969219SMarco Elver 			pr_err("KASAN internal error: frame description too long: %s\n",
233e8969219SMarco Elver 			       *frame_descr);
234e8969219SMarco Elver 			return false;
235e8969219SMarco Elver 		}
236e8969219SMarco Elver 
237e8969219SMarco Elver 		/* Copy token (+ 1 byte for '\0'). */
238e8969219SMarco Elver 		strlcpy(token, *frame_descr, tok_len + 1);
239e8969219SMarco Elver 	}
240e8969219SMarco Elver 
241e8969219SMarco Elver 	/* Advance frame_descr past separator. */
242e8969219SMarco Elver 	*frame_descr = sep + 1;
243e8969219SMarco Elver 
244e8969219SMarco Elver 	if (value != NULL && kstrtoul(token, 10, value)) {
245e8969219SMarco Elver 		pr_err("KASAN internal error: not a valid number: %s\n", token);
246e8969219SMarco Elver 		return false;
247e8969219SMarco Elver 	}
248e8969219SMarco Elver 
249e8969219SMarco Elver 	return true;
250e8969219SMarco Elver }
251e8969219SMarco Elver 
252e8969219SMarco Elver static void print_decoded_frame_descr(const char *frame_descr)
253e8969219SMarco Elver {
254e8969219SMarco Elver 	/*
255e8969219SMarco Elver 	 * We need to parse the following string:
256e8969219SMarco Elver 	 *    "n alloc_1 alloc_2 ... alloc_n"
257e8969219SMarco Elver 	 * where alloc_i looks like
258e8969219SMarco Elver 	 *    "offset size len name"
259e8969219SMarco Elver 	 * or "offset size len name:line".
260e8969219SMarco Elver 	 */
261e8969219SMarco Elver 
262e8969219SMarco Elver 	char token[64];
263e8969219SMarco Elver 	unsigned long num_objects;
264e8969219SMarco Elver 
265e8969219SMarco Elver 	if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
266e8969219SMarco Elver 				  &num_objects))
267e8969219SMarco Elver 		return;
268e8969219SMarco Elver 
269e8969219SMarco Elver 	pr_err("\n");
270e8969219SMarco Elver 	pr_err("this frame has %lu %s:\n", num_objects,
271e8969219SMarco Elver 	       num_objects == 1 ? "object" : "objects");
272e8969219SMarco Elver 
273e8969219SMarco Elver 	while (num_objects--) {
274e8969219SMarco Elver 		unsigned long offset;
275e8969219SMarco Elver 		unsigned long size;
276e8969219SMarco Elver 
277e8969219SMarco Elver 		/* access offset */
278e8969219SMarco Elver 		if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
279e8969219SMarco Elver 					  &offset))
280e8969219SMarco Elver 			return;
281e8969219SMarco Elver 		/* access size */
282e8969219SMarco Elver 		if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
283e8969219SMarco Elver 					  &size))
284e8969219SMarco Elver 			return;
285e8969219SMarco Elver 		/* name length (unused) */
286e8969219SMarco Elver 		if (!tokenize_frame_descr(&frame_descr, NULL, 0, NULL))
287e8969219SMarco Elver 			return;
288e8969219SMarco Elver 		/* object name */
289e8969219SMarco Elver 		if (!tokenize_frame_descr(&frame_descr, token, sizeof(token),
290e8969219SMarco Elver 					  NULL))
291e8969219SMarco Elver 			return;
292e8969219SMarco Elver 
293e8969219SMarco Elver 		/* Strip line number; without filename it's not very helpful. */
294e8969219SMarco Elver 		strreplace(token, ':', '\0');
295e8969219SMarco Elver 
296e8969219SMarco Elver 		/* Finally, print object information. */
297e8969219SMarco Elver 		pr_err(" [%lu, %lu) '%s'", offset, offset + size, token);
298e8969219SMarco Elver 	}
299e8969219SMarco Elver }
300e8969219SMarco Elver 
301e8969219SMarco Elver static bool __must_check get_address_stack_frame_info(const void *addr,
302e8969219SMarco Elver 						      unsigned long *offset,
303e8969219SMarco Elver 						      const char **frame_descr,
304e8969219SMarco Elver 						      const void **frame_pc)
305e8969219SMarco Elver {
306e8969219SMarco Elver 	unsigned long aligned_addr;
307e8969219SMarco Elver 	unsigned long mem_ptr;
308e8969219SMarco Elver 	const u8 *shadow_bottom;
309e8969219SMarco Elver 	const u8 *shadow_ptr;
310e8969219SMarco Elver 	const unsigned long *frame;
311e8969219SMarco Elver 
312e8969219SMarco Elver 	BUILD_BUG_ON(IS_ENABLED(CONFIG_STACK_GROWSUP));
313e8969219SMarco Elver 
314e8969219SMarco Elver 	/*
315e8969219SMarco Elver 	 * NOTE: We currently only support printing frame information for
316e8969219SMarco Elver 	 * accesses to the task's own stack.
317e8969219SMarco Elver 	 */
318e8969219SMarco Elver 	if (!object_is_on_stack(addr))
319e8969219SMarco Elver 		return false;
320e8969219SMarco Elver 
321e8969219SMarco Elver 	aligned_addr = round_down((unsigned long)addr, sizeof(long));
322e8969219SMarco Elver 	mem_ptr = round_down(aligned_addr, KASAN_SHADOW_SCALE_SIZE);
323e8969219SMarco Elver 	shadow_ptr = kasan_mem_to_shadow((void *)aligned_addr);
324e8969219SMarco Elver 	shadow_bottom = kasan_mem_to_shadow(end_of_stack(current));
325e8969219SMarco Elver 
326e8969219SMarco Elver 	while (shadow_ptr >= shadow_bottom && *shadow_ptr != KASAN_STACK_LEFT) {
327e8969219SMarco Elver 		shadow_ptr--;
328e8969219SMarco Elver 		mem_ptr -= KASAN_SHADOW_SCALE_SIZE;
329e8969219SMarco Elver 	}
330e8969219SMarco Elver 
331e8969219SMarco Elver 	while (shadow_ptr >= shadow_bottom && *shadow_ptr == KASAN_STACK_LEFT) {
332e8969219SMarco Elver 		shadow_ptr--;
333e8969219SMarco Elver 		mem_ptr -= KASAN_SHADOW_SCALE_SIZE;
334e8969219SMarco Elver 	}
335e8969219SMarco Elver 
336e8969219SMarco Elver 	if (shadow_ptr < shadow_bottom)
337e8969219SMarco Elver 		return false;
338e8969219SMarco Elver 
339e8969219SMarco Elver 	frame = (const unsigned long *)(mem_ptr + KASAN_SHADOW_SCALE_SIZE);
340e8969219SMarco Elver 	if (frame[0] != KASAN_CURRENT_STACK_FRAME_MAGIC) {
341e8969219SMarco Elver 		pr_err("KASAN internal error: frame info validation failed; invalid marker: %lu\n",
342e8969219SMarco Elver 		       frame[0]);
343e8969219SMarco Elver 		return false;
344e8969219SMarco Elver 	}
345e8969219SMarco Elver 
346e8969219SMarco Elver 	*offset = (unsigned long)addr - (unsigned long)frame;
347e8969219SMarco Elver 	*frame_descr = (const char *)frame[1];
348e8969219SMarco Elver 	*frame_pc = (void *)frame[2];
349e8969219SMarco Elver 
350e8969219SMarco Elver 	return true;
351e8969219SMarco Elver }
352e8969219SMarco Elver 
353e8969219SMarco Elver static void print_address_stack_frame(const void *addr)
354e8969219SMarco Elver {
355e8969219SMarco Elver 	unsigned long offset;
356e8969219SMarco Elver 	const char *frame_descr;
357e8969219SMarco Elver 	const void *frame_pc;
358e8969219SMarco Elver 
359e8969219SMarco Elver 	if (IS_ENABLED(CONFIG_KASAN_SW_TAGS))
360e8969219SMarco Elver 		return;
361e8969219SMarco Elver 
362e8969219SMarco Elver 	if (!get_address_stack_frame_info(addr, &offset, &frame_descr,
363e8969219SMarco Elver 					  &frame_pc))
364e8969219SMarco Elver 		return;
365e8969219SMarco Elver 
366e8969219SMarco Elver 	/*
367e8969219SMarco Elver 	 * get_address_stack_frame_info only returns true if the given addr is
368e8969219SMarco Elver 	 * on the current task's stack.
369e8969219SMarco Elver 	 */
370e8969219SMarco Elver 	pr_err("\n");
371e8969219SMarco Elver 	pr_err("addr %px is located in stack of task %s/%d at offset %lu in frame:\n",
372e8969219SMarco Elver 	       addr, current->comm, task_pid_nr(current), offset);
373e8969219SMarco Elver 	pr_err(" %pS\n", frame_pc);
374e8969219SMarco Elver 
375e8969219SMarco Elver 	if (!frame_descr)
376e8969219SMarco Elver 		return;
377e8969219SMarco Elver 
378e8969219SMarco Elver 	print_decoded_frame_descr(frame_descr);
379e8969219SMarco Elver }
380e8969219SMarco Elver 
381ae8f06b3SWalter Wu static void print_address_description(void *addr, u8 tag)
3827e088978SAndrey Ryabinin {
383ae8f06b3SWalter Wu 	struct page *page = kasan_addr_to_page(addr);
384b8c73fc2SAndrey Ryabinin 
385db429f16SAndrey Konovalov 	dump_stack();
386b1938599SAndrey Konovalov 	pr_err("\n");
387db429f16SAndrey Konovalov 
388db429f16SAndrey Konovalov 	if (page && PageSlab(page)) {
389db429f16SAndrey Konovalov 		struct kmem_cache *cache = page->slab_cache;
3900c06f1f8SAndrey Konovalov 		void *object = nearest_obj(cache, page,	addr);
391db429f16SAndrey Konovalov 
392ae8f06b3SWalter Wu 		describe_object(cache, object, addr, tag);
393b8c73fc2SAndrey Ryabinin 	}
394b8c73fc2SAndrey Ryabinin 
395430a05f9SAndrey Konovalov 	if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
396430a05f9SAndrey Konovalov 		pr_err("The buggy address belongs to the variable:\n");
397430a05f9SAndrey Konovalov 		pr_err(" %pS\n", addr);
398430a05f9SAndrey Konovalov 	}
399430a05f9SAndrey Konovalov 
400430a05f9SAndrey Konovalov 	if (page) {
401430a05f9SAndrey Konovalov 		pr_err("The buggy address belongs to the page:\n");
402430a05f9SAndrey Konovalov 		dump_page(page, "kasan: bad access detected");
403bebf56a1SAndrey Ryabinin 	}
404e8969219SMarco Elver 
405e8969219SMarco Elver 	print_address_stack_frame(addr);
4060b24beccSAndrey Ryabinin }
4070b24beccSAndrey Ryabinin 
4080b24beccSAndrey Ryabinin static bool row_is_guilty(const void *row, const void *guilty)
4090b24beccSAndrey Ryabinin {
4100b24beccSAndrey Ryabinin 	return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
4110b24beccSAndrey Ryabinin }
4120b24beccSAndrey Ryabinin 
4130b24beccSAndrey Ryabinin static int shadow_pointer_offset(const void *row, const void *shadow)
4140b24beccSAndrey Ryabinin {
4150b24beccSAndrey Ryabinin 	/* The length of ">ff00ff00ff00ff00: " is
4160b24beccSAndrey Ryabinin 	 *    3 + (BITS_PER_LONG/8)*2 chars.
4170b24beccSAndrey Ryabinin 	 */
4180b24beccSAndrey Ryabinin 	return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
4190b24beccSAndrey Ryabinin 		(shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
4200b24beccSAndrey Ryabinin }
4210b24beccSAndrey Ryabinin 
4220b24beccSAndrey Ryabinin static void print_shadow_for_address(const void *addr)
4230b24beccSAndrey Ryabinin {
4240b24beccSAndrey Ryabinin 	int i;
4250b24beccSAndrey Ryabinin 	const void *shadow = kasan_mem_to_shadow(addr);
4260b24beccSAndrey Ryabinin 	const void *shadow_row;
4270b24beccSAndrey Ryabinin 
4280b24beccSAndrey Ryabinin 	shadow_row = (void *)round_down((unsigned long)shadow,
4290b24beccSAndrey Ryabinin 					SHADOW_BYTES_PER_ROW)
4300b24beccSAndrey Ryabinin 		- SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
4310b24beccSAndrey Ryabinin 
4320b24beccSAndrey Ryabinin 	pr_err("Memory state around the buggy address:\n");
4330b24beccSAndrey Ryabinin 
4340b24beccSAndrey Ryabinin 	for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
4350b24beccSAndrey Ryabinin 		const void *kaddr = kasan_shadow_to_mem(shadow_row);
4360b24beccSAndrey Ryabinin 		char buffer[4 + (BITS_PER_LONG/8)*2];
437f2377d4eSAneesh Kumar K.V 		char shadow_buf[SHADOW_BYTES_PER_ROW];
4380b24beccSAndrey Ryabinin 
4390b24beccSAndrey Ryabinin 		snprintf(buffer, sizeof(buffer),
4406424f6bbSTobin C. Harding 			(i == 0) ? ">%px: " : " %px: ", kaddr);
441f2377d4eSAneesh Kumar K.V 		/*
442f2377d4eSAneesh Kumar K.V 		 * We should not pass a shadow pointer to generic
443f2377d4eSAneesh Kumar K.V 		 * function, because generic functions may try to
444f2377d4eSAneesh Kumar K.V 		 * access kasan mapping for the passed address.
445f2377d4eSAneesh Kumar K.V 		 */
446f2377d4eSAneesh Kumar K.V 		memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
4470b24beccSAndrey Ryabinin 		print_hex_dump(KERN_ERR, buffer,
4480b24beccSAndrey Ryabinin 			DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
449f2377d4eSAneesh Kumar K.V 			shadow_buf, SHADOW_BYTES_PER_ROW, 0);
4500b24beccSAndrey Ryabinin 
4510b24beccSAndrey Ryabinin 		if (row_is_guilty(shadow_row, shadow))
4520b24beccSAndrey Ryabinin 			pr_err("%*c\n",
4530b24beccSAndrey Ryabinin 				shadow_pointer_offset(shadow_row, shadow),
4540b24beccSAndrey Ryabinin 				'^');
4550b24beccSAndrey Ryabinin 
4560b24beccSAndrey Ryabinin 		shadow_row += SHADOW_BYTES_PER_ROW;
4570b24beccSAndrey Ryabinin 	}
4580b24beccSAndrey Ryabinin }
4590b24beccSAndrey Ryabinin 
4604fba3758SAndrey Konovalov static bool report_enabled(void)
461b0845ce5SMark Rutland {
462b0845ce5SMark Rutland 	if (current->kasan_depth)
463b0845ce5SMark Rutland 		return false;
464b0845ce5SMark Rutland 	if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
465b0845ce5SMark Rutland 		return true;
466b0845ce5SMark Rutland 	return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
467b0845ce5SMark Rutland }
468b0845ce5SMark Rutland 
46983c4e7a0SPatricia Alfonso #if IS_ENABLED(CONFIG_KUNIT)
47083c4e7a0SPatricia Alfonso static void kasan_update_kunit_status(struct kunit *cur_test)
47183c4e7a0SPatricia Alfonso {
47283c4e7a0SPatricia Alfonso 	struct kunit_resource *resource;
47383c4e7a0SPatricia Alfonso 	struct kunit_kasan_expectation *kasan_data;
47483c4e7a0SPatricia Alfonso 
47583c4e7a0SPatricia Alfonso 	resource = kunit_find_named_resource(cur_test, "kasan_data");
47683c4e7a0SPatricia Alfonso 
47783c4e7a0SPatricia Alfonso 	if (!resource) {
47883c4e7a0SPatricia Alfonso 		kunit_set_failure(cur_test);
47983c4e7a0SPatricia Alfonso 		return;
48083c4e7a0SPatricia Alfonso 	}
48183c4e7a0SPatricia Alfonso 
48283c4e7a0SPatricia Alfonso 	kasan_data = (struct kunit_kasan_expectation *)resource->data;
48383c4e7a0SPatricia Alfonso 	kasan_data->report_found = true;
48483c4e7a0SPatricia Alfonso 	kunit_put_resource(resource);
48583c4e7a0SPatricia Alfonso }
48683c4e7a0SPatricia Alfonso #endif /* IS_ENABLED(CONFIG_KUNIT) */
48783c4e7a0SPatricia Alfonso 
48811cd3cd6SAndrey Konovalov void kasan_report_invalid_free(void *object, unsigned long ip)
48911cd3cd6SAndrey Konovalov {
49011cd3cd6SAndrey Konovalov 	unsigned long flags;
491ae8f06b3SWalter Wu 	u8 tag = get_tag(object);
49211cd3cd6SAndrey Konovalov 
493ae8f06b3SWalter Wu 	object = reset_tag(object);
49483c4e7a0SPatricia Alfonso 
49583c4e7a0SPatricia Alfonso #if IS_ENABLED(CONFIG_KUNIT)
49683c4e7a0SPatricia Alfonso 	if (current->kunit_test)
49783c4e7a0SPatricia Alfonso 		kasan_update_kunit_status(current->kunit_test);
49883c4e7a0SPatricia Alfonso #endif /* IS_ENABLED(CONFIG_KUNIT) */
49983c4e7a0SPatricia Alfonso 
50011cd3cd6SAndrey Konovalov 	start_report(&flags);
50111cd3cd6SAndrey Konovalov 	pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
502ae8f06b3SWalter Wu 	print_tags(tag, object);
50311cd3cd6SAndrey Konovalov 	pr_err("\n");
504ae8f06b3SWalter Wu 	print_address_description(object, tag);
50511cd3cd6SAndrey Konovalov 	pr_err("\n");
50611cd3cd6SAndrey Konovalov 	print_shadow_for_address(object);
50711cd3cd6SAndrey Konovalov 	end_report(&flags);
50811cd3cd6SAndrey Konovalov }
50911cd3cd6SAndrey Konovalov 
5104fba3758SAndrey Konovalov static void __kasan_report(unsigned long addr, size_t size, bool is_write,
5114fba3758SAndrey Konovalov 				unsigned long ip)
5120b24beccSAndrey Ryabinin {
5130b24beccSAndrey Ryabinin 	struct kasan_access_info info;
514121e8f81SAndrey Konovalov 	void *tagged_addr;
515121e8f81SAndrey Konovalov 	void *untagged_addr;
516121e8f81SAndrey Konovalov 	unsigned long flags;
5170b24beccSAndrey Ryabinin 
51883c4e7a0SPatricia Alfonso #if IS_ENABLED(CONFIG_KUNIT)
51983c4e7a0SPatricia Alfonso 	if (current->kunit_test)
52083c4e7a0SPatricia Alfonso 		kasan_update_kunit_status(current->kunit_test);
52183c4e7a0SPatricia Alfonso #endif /* IS_ENABLED(CONFIG_KUNIT) */
52283c4e7a0SPatricia Alfonso 
5234f40c6e5SPeter Zijlstra 	disable_trace_on_warning();
5244f40c6e5SPeter Zijlstra 
525121e8f81SAndrey Konovalov 	tagged_addr = (void *)addr;
526121e8f81SAndrey Konovalov 	untagged_addr = reset_tag(tagged_addr);
527121e8f81SAndrey Konovalov 
528121e8f81SAndrey Konovalov 	info.access_addr = tagged_addr;
529121e8f81SAndrey Konovalov 	if (addr_has_shadow(untagged_addr))
530121e8f81SAndrey Konovalov 		info.first_bad_addr = find_first_bad_addr(tagged_addr, size);
531121e8f81SAndrey Konovalov 	else
532121e8f81SAndrey Konovalov 		info.first_bad_addr = untagged_addr;
5330b24beccSAndrey Ryabinin 	info.access_size = size;
5340b24beccSAndrey Ryabinin 	info.is_write = is_write;
5350b24beccSAndrey Ryabinin 	info.ip = ip;
536e9121076SAndrey Konovalov 
537121e8f81SAndrey Konovalov 	start_report(&flags);
538121e8f81SAndrey Konovalov 
539121e8f81SAndrey Konovalov 	print_error_description(&info);
540121e8f81SAndrey Konovalov 	if (addr_has_shadow(untagged_addr))
541121e8f81SAndrey Konovalov 		print_tags(get_tag(tagged_addr), info.first_bad_addr);
542121e8f81SAndrey Konovalov 	pr_err("\n");
543121e8f81SAndrey Konovalov 
544121e8f81SAndrey Konovalov 	if (addr_has_shadow(untagged_addr)) {
545ae8f06b3SWalter Wu 		print_address_description(untagged_addr, get_tag(tagged_addr));
546121e8f81SAndrey Konovalov 		pr_err("\n");
547121e8f81SAndrey Konovalov 		print_shadow_for_address(info.first_bad_addr);
548121e8f81SAndrey Konovalov 	} else {
549121e8f81SAndrey Konovalov 		dump_stack();
550121e8f81SAndrey Konovalov 	}
551121e8f81SAndrey Konovalov 
552121e8f81SAndrey Konovalov 	end_report(&flags);
5530b24beccSAndrey Ryabinin }
5542f004eeaSJann Horn 
5554fba3758SAndrey Konovalov bool kasan_report(unsigned long addr, size_t size, bool is_write,
5564fba3758SAndrey Konovalov 			unsigned long ip)
5574fba3758SAndrey Konovalov {
5584fba3758SAndrey Konovalov 	unsigned long flags = user_access_save();
5594fba3758SAndrey Konovalov 	bool ret = false;
5604fba3758SAndrey Konovalov 
5614fba3758SAndrey Konovalov 	if (likely(report_enabled())) {
5624fba3758SAndrey Konovalov 		__kasan_report(addr, size, is_write, ip);
5634fba3758SAndrey Konovalov 		ret = true;
5644fba3758SAndrey Konovalov 	}
5654fba3758SAndrey Konovalov 
5664fba3758SAndrey Konovalov 	user_access_restore(flags);
5674fba3758SAndrey Konovalov 
5684fba3758SAndrey Konovalov 	return ret;
5694fba3758SAndrey Konovalov }
5704fba3758SAndrey Konovalov 
5712f004eeaSJann Horn #ifdef CONFIG_KASAN_INLINE
5722f004eeaSJann Horn /*
5732f004eeaSJann Horn  * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
5742f004eeaSJann Horn  * canonical half of the address space) cause out-of-bounds shadow memory reads
5752f004eeaSJann Horn  * before the actual access. For addresses in the low canonical half of the
5762f004eeaSJann Horn  * address space, as well as most non-canonical addresses, that out-of-bounds
5772f004eeaSJann Horn  * shadow memory access lands in the non-canonical part of the address space.
5782f004eeaSJann Horn  * Help the user figure out what the original bogus pointer was.
5792f004eeaSJann Horn  */
5802f004eeaSJann Horn void kasan_non_canonical_hook(unsigned long addr)
5812f004eeaSJann Horn {
5822f004eeaSJann Horn 	unsigned long orig_addr;
5832f004eeaSJann Horn 	const char *bug_type;
5842f004eeaSJann Horn 
5852f004eeaSJann Horn 	if (addr < KASAN_SHADOW_OFFSET)
5862f004eeaSJann Horn 		return;
5872f004eeaSJann Horn 
5882f004eeaSJann Horn 	orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
5892f004eeaSJann Horn 	/*
5902f004eeaSJann Horn 	 * For faults near the shadow address for NULL, we can be fairly certain
5912f004eeaSJann Horn 	 * that this is a KASAN shadow memory access.
5922f004eeaSJann Horn 	 * For faults that correspond to shadow for low canonical addresses, we
5932f004eeaSJann Horn 	 * can still be pretty sure - that shadow region is a fairly narrow
5942f004eeaSJann Horn 	 * chunk of the non-canonical address space.
5952f004eeaSJann Horn 	 * But faults that look like shadow for non-canonical addresses are a
5962f004eeaSJann Horn 	 * really large chunk of the address space. In that case, we still
5972f004eeaSJann Horn 	 * print the decoded address, but make it clear that this is not
5982f004eeaSJann Horn 	 * necessarily what's actually going on.
5992f004eeaSJann Horn 	 */
6002f004eeaSJann Horn 	if (orig_addr < PAGE_SIZE)
6012f004eeaSJann Horn 		bug_type = "null-ptr-deref";
6022f004eeaSJann Horn 	else if (orig_addr < TASK_SIZE)
6032f004eeaSJann Horn 		bug_type = "probably user-memory-access";
6042f004eeaSJann Horn 	else
6052f004eeaSJann Horn 		bug_type = "maybe wild-memory-access";
6062f004eeaSJann Horn 	pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
6072f004eeaSJann Horn 		 orig_addr, orig_addr + KASAN_SHADOW_MASK);
6082f004eeaSJann Horn }
6092f004eeaSJann Horn #endif
610