xref: /openbmc/linux/mm/kasan/report.c (revision 7f0a84c23b1dede3e76a7b2ebbde45a506252005)
10b24beccSAndrey Ryabinin /*
20b24beccSAndrey Ryabinin  * This file contains error reporting code.
30b24beccSAndrey Ryabinin  *
40b24beccSAndrey Ryabinin  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
52baf9e89SAndrey Ryabinin  * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
60b24beccSAndrey Ryabinin  *
75d0926efSAndrey Konovalov  * Some code borrowed from https://github.com/xairy/kasan-prototype by
80b24beccSAndrey Ryabinin  *        Andrey Konovalov <adech.fo@gmail.com>
90b24beccSAndrey Ryabinin  *
100b24beccSAndrey Ryabinin  * This program is free software; you can redistribute it and/or modify
110b24beccSAndrey Ryabinin  * it under the terms of the GNU General Public License version 2 as
120b24beccSAndrey Ryabinin  * published by the Free Software Foundation.
130b24beccSAndrey Ryabinin  *
140b24beccSAndrey Ryabinin  */
150b24beccSAndrey Ryabinin 
16b0845ce5SMark Rutland #include <linux/bitops.h>
174f40c6e5SPeter Zijlstra #include <linux/ftrace.h>
18b0845ce5SMark Rutland #include <linux/init.h>
190b24beccSAndrey Ryabinin #include <linux/kernel.h>
200b24beccSAndrey Ryabinin #include <linux/mm.h>
210b24beccSAndrey Ryabinin #include <linux/printk.h>
220b24beccSAndrey Ryabinin #include <linux/sched.h>
230b24beccSAndrey Ryabinin #include <linux/slab.h>
24cd11016eSAlexander Potapenko #include <linux/stackdepot.h>
250b24beccSAndrey Ryabinin #include <linux/stacktrace.h>
260b24beccSAndrey Ryabinin #include <linux/string.h>
270b24beccSAndrey Ryabinin #include <linux/types.h>
280b24beccSAndrey Ryabinin #include <linux/kasan.h>
29527f215bSAneesh Kumar K.V #include <linux/module.h>
300b24beccSAndrey Ryabinin 
31bebf56a1SAndrey Ryabinin #include <asm/sections.h>
32bebf56a1SAndrey Ryabinin 
330b24beccSAndrey Ryabinin #include "kasan.h"
340316bec2SAndrey Ryabinin #include "../slab.h"
350b24beccSAndrey Ryabinin 
360b24beccSAndrey Ryabinin /* Shadow layout customization. */
370b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_BLOCK 1
380b24beccSAndrey Ryabinin #define SHADOW_BLOCKS_PER_ROW 16
390b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK)
400b24beccSAndrey Ryabinin #define SHADOW_ROWS_AROUND_ADDR 2
410b24beccSAndrey Ryabinin 
420b24beccSAndrey Ryabinin static const void *find_first_bad_addr(const void *addr, size_t size)
430b24beccSAndrey Ryabinin {
440b24beccSAndrey Ryabinin 	u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr);
450b24beccSAndrey Ryabinin 	const void *first_bad_addr = addr;
460b24beccSAndrey Ryabinin 
470b24beccSAndrey Ryabinin 	while (!shadow_val && first_bad_addr < addr + size) {
480b24beccSAndrey Ryabinin 		first_bad_addr += KASAN_SHADOW_SCALE_SIZE;
490b24beccSAndrey Ryabinin 		shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr);
500b24beccSAndrey Ryabinin 	}
510b24beccSAndrey Ryabinin 	return first_bad_addr;
520b24beccSAndrey Ryabinin }
530b24beccSAndrey Ryabinin 
545e82cd12SAndrey Konovalov static bool addr_has_shadow(struct kasan_access_info *info)
555e82cd12SAndrey Konovalov {
565e82cd12SAndrey Konovalov 	return (info->access_addr >=
575e82cd12SAndrey Konovalov 		kasan_shadow_to_mem((void *)KASAN_SHADOW_START));
585e82cd12SAndrey Konovalov }
595e82cd12SAndrey Konovalov 
605e82cd12SAndrey Konovalov static const char *get_shadow_bug_type(struct kasan_access_info *info)
610b24beccSAndrey Ryabinin {
620952d87fSAndrey Konovalov 	const char *bug_type = "unknown-crash";
63cdf6a273SAndrey Konovalov 	u8 *shadow_addr;
640b24beccSAndrey Ryabinin 
650b24beccSAndrey Ryabinin 	info->first_bad_addr = find_first_bad_addr(info->access_addr,
660b24beccSAndrey Ryabinin 						info->access_size);
670b24beccSAndrey Ryabinin 
68cdf6a273SAndrey Konovalov 	shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr);
690b24beccSAndrey Ryabinin 
70cdf6a273SAndrey Konovalov 	/*
71cdf6a273SAndrey Konovalov 	 * If shadow byte value is in [0, KASAN_SHADOW_SCALE_SIZE) we can look
72cdf6a273SAndrey Konovalov 	 * at the next shadow byte to determine the type of the bad access.
73cdf6a273SAndrey Konovalov 	 */
74cdf6a273SAndrey Konovalov 	if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1)
75cdf6a273SAndrey Konovalov 		shadow_addr++;
76cdf6a273SAndrey Konovalov 
77cdf6a273SAndrey Konovalov 	switch (*shadow_addr) {
780952d87fSAndrey Konovalov 	case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
79cdf6a273SAndrey Konovalov 		/*
80cdf6a273SAndrey Konovalov 		 * In theory it's still possible to see these shadow values
81cdf6a273SAndrey Konovalov 		 * due to a data race in the kernel code.
82cdf6a273SAndrey Konovalov 		 */
830952d87fSAndrey Konovalov 		bug_type = "out-of-bounds";
84b8c73fc2SAndrey Ryabinin 		break;
850316bec2SAndrey Ryabinin 	case KASAN_PAGE_REDZONE:
860316bec2SAndrey Ryabinin 	case KASAN_KMALLOC_REDZONE:
870952d87fSAndrey Konovalov 		bug_type = "slab-out-of-bounds";
880952d87fSAndrey Konovalov 		break;
89bebf56a1SAndrey Ryabinin 	case KASAN_GLOBAL_REDZONE:
900952d87fSAndrey Konovalov 		bug_type = "global-out-of-bounds";
910b24beccSAndrey Ryabinin 		break;
92c420f167SAndrey Ryabinin 	case KASAN_STACK_LEFT:
93c420f167SAndrey Ryabinin 	case KASAN_STACK_MID:
94c420f167SAndrey Ryabinin 	case KASAN_STACK_RIGHT:
95c420f167SAndrey Ryabinin 	case KASAN_STACK_PARTIAL:
960952d87fSAndrey Konovalov 		bug_type = "stack-out-of-bounds";
970952d87fSAndrey Konovalov 		break;
980952d87fSAndrey Konovalov 	case KASAN_FREE_PAGE:
990952d87fSAndrey Konovalov 	case KASAN_KMALLOC_FREE:
1000952d87fSAndrey Konovalov 		bug_type = "use-after-free";
101c420f167SAndrey Ryabinin 		break;
102828347f8SDmitry Vyukov 	case KASAN_USE_AFTER_SCOPE:
103828347f8SDmitry Vyukov 		bug_type = "use-after-scope";
104828347f8SDmitry Vyukov 		break;
1050b24beccSAndrey Ryabinin 	}
1060b24beccSAndrey Ryabinin 
1075e82cd12SAndrey Konovalov 	return bug_type;
1085e82cd12SAndrey Konovalov }
1095e82cd12SAndrey Konovalov 
1105e82cd12SAndrey Konovalov const char *get_wild_bug_type(struct kasan_access_info *info)
1115e82cd12SAndrey Konovalov {
1125e82cd12SAndrey Konovalov 	const char *bug_type = "unknown-crash";
1135e82cd12SAndrey Konovalov 
1145e82cd12SAndrey Konovalov 	if ((unsigned long)info->access_addr < PAGE_SIZE)
1155e82cd12SAndrey Konovalov 		bug_type = "null-ptr-deref";
1165e82cd12SAndrey Konovalov 	else if ((unsigned long)info->access_addr < TASK_SIZE)
1175e82cd12SAndrey Konovalov 		bug_type = "user-memory-access";
1185e82cd12SAndrey Konovalov 	else
1195e82cd12SAndrey Konovalov 		bug_type = "wild-memory-access";
1205e82cd12SAndrey Konovalov 
1215e82cd12SAndrey Konovalov 	return bug_type;
1225e82cd12SAndrey Konovalov }
1235e82cd12SAndrey Konovalov 
1247d418f7bSAndrey Konovalov static const char *get_bug_type(struct kasan_access_info *info)
1257d418f7bSAndrey Konovalov {
1267d418f7bSAndrey Konovalov 	if (addr_has_shadow(info))
1277d418f7bSAndrey Konovalov 		return get_shadow_bug_type(info);
1287d418f7bSAndrey Konovalov 	return get_wild_bug_type(info);
1297d418f7bSAndrey Konovalov }
1307d418f7bSAndrey Konovalov 
1315e82cd12SAndrey Konovalov static void print_error_description(struct kasan_access_info *info)
1325e82cd12SAndrey Konovalov {
1337d418f7bSAndrey Konovalov 	const char *bug_type = get_bug_type(info);
1345e82cd12SAndrey Konovalov 
135*7f0a84c2SAndrey Konovalov 	pr_err("BUG: KASAN: %s in %pS\n",
136*7f0a84c2SAndrey Konovalov 		bug_type, (void *)info->ip);
137*7f0a84c2SAndrey Konovalov 	pr_err("%s of size %zu at addr %p by task %s/%d\n",
1387d418f7bSAndrey Konovalov 		info->is_write ? "Write" : "Read", info->access_size,
139*7f0a84c2SAndrey Konovalov 		info->access_addr, current->comm, task_pid_nr(current));
1400b24beccSAndrey Ryabinin }
1410b24beccSAndrey Ryabinin 
142bebf56a1SAndrey Ryabinin static inline bool kernel_or_module_addr(const void *addr)
143bebf56a1SAndrey Ryabinin {
144527f215bSAneesh Kumar K.V 	if (addr >= (void *)_stext && addr < (void *)_end)
145527f215bSAneesh Kumar K.V 		return true;
146527f215bSAneesh Kumar K.V 	if (is_module_address((unsigned long)addr))
147527f215bSAneesh Kumar K.V 		return true;
148527f215bSAneesh Kumar K.V 	return false;
149bebf56a1SAndrey Ryabinin }
150bebf56a1SAndrey Ryabinin 
151bebf56a1SAndrey Ryabinin static inline bool init_task_stack_addr(const void *addr)
152bebf56a1SAndrey Ryabinin {
153bebf56a1SAndrey Ryabinin 	return addr >= (void *)&init_thread_union.stack &&
154bebf56a1SAndrey Ryabinin 		(addr <= (void *)&init_thread_union.stack +
155bebf56a1SAndrey Ryabinin 			sizeof(init_thread_union.stack));
156bebf56a1SAndrey Ryabinin }
157bebf56a1SAndrey Ryabinin 
1587e088978SAndrey Ryabinin static DEFINE_SPINLOCK(report_lock);
1597e088978SAndrey Ryabinin 
1607e088978SAndrey Ryabinin static void kasan_start_report(unsigned long *flags)
1617e088978SAndrey Ryabinin {
1627e088978SAndrey Ryabinin 	/*
1637e088978SAndrey Ryabinin 	 * Make sure we don't end up in loop.
1647e088978SAndrey Ryabinin 	 */
1657e088978SAndrey Ryabinin 	kasan_disable_current();
1667e088978SAndrey Ryabinin 	spin_lock_irqsave(&report_lock, *flags);
1677e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
1687e088978SAndrey Ryabinin }
1697e088978SAndrey Ryabinin 
1707e088978SAndrey Ryabinin static void kasan_end_report(unsigned long *flags)
1717e088978SAndrey Ryabinin {
1727e088978SAndrey Ryabinin 	pr_err("==================================================================\n");
1737e088978SAndrey Ryabinin 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
1747e088978SAndrey Ryabinin 	spin_unlock_irqrestore(&report_lock, *flags);
1755c5c1f36SDmitry Vyukov 	if (panic_on_warn)
1765c5c1f36SDmitry Vyukov 		panic("panic_on_warn set ...\n");
1777e088978SAndrey Ryabinin 	kasan_enable_current();
1787e088978SAndrey Ryabinin }
1797e088978SAndrey Ryabinin 
180b6b72f49SAndrey Konovalov static void print_track(struct kasan_track *track, const char *prefix)
1817ed2f9e6SAlexander Potapenko {
182b6b72f49SAndrey Konovalov 	pr_err("%s by task %u:\n", prefix, track->pid);
183cd11016eSAlexander Potapenko 	if (track->stack) {
184cd11016eSAlexander Potapenko 		struct stack_trace trace;
185cd11016eSAlexander Potapenko 
186cd11016eSAlexander Potapenko 		depot_fetch_stack(track->stack, &trace);
187cd11016eSAlexander Potapenko 		print_stack_trace(&trace, 0);
188cd11016eSAlexander Potapenko 	} else {
189cd11016eSAlexander Potapenko 		pr_err("(stack is not available)\n");
190cd11016eSAlexander Potapenko 	}
1917ed2f9e6SAlexander Potapenko }
1927ed2f9e6SAlexander Potapenko 
193db429f16SAndrey Konovalov static struct page *addr_to_page(const void *addr)
194db429f16SAndrey Konovalov {
195db429f16SAndrey Konovalov 	if ((addr >= (void *)PAGE_OFFSET) &&
196db429f16SAndrey Konovalov 			(addr < high_memory))
197db429f16SAndrey Konovalov 		return virt_to_head_page(addr);
198db429f16SAndrey Konovalov 	return NULL;
199db429f16SAndrey Konovalov }
200db429f16SAndrey Konovalov 
201db429f16SAndrey Konovalov static void describe_object(struct kmem_cache *cache, void *object)
2027ed2f9e6SAlexander Potapenko {
2037ed2f9e6SAlexander Potapenko 	struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
2047ed2f9e6SAlexander Potapenko 
20547b5c2a0SAndrey Ryabinin 	pr_err("Object at %p, in cache %s size: %d\n", object, cache->name,
20647b5c2a0SAndrey Ryabinin 		cache->object_size);
20747b5c2a0SAndrey Ryabinin 
2087ed2f9e6SAlexander Potapenko 	if (!(cache->flags & SLAB_KASAN))
2097ed2f9e6SAlexander Potapenko 		return;
210b3cbd9bfSAndrey Ryabinin 
211b6b72f49SAndrey Konovalov 	print_track(&alloc_info->alloc_track, "Allocated");
212b6b72f49SAndrey Konovalov 	print_track(&alloc_info->free_track, "Freed");
2137ed2f9e6SAlexander Potapenko }
2147ed2f9e6SAlexander Potapenko 
2157e088978SAndrey Ryabinin void kasan_report_double_free(struct kmem_cache *cache, void *object,
2167e088978SAndrey Ryabinin 			s8 shadow)
2177e088978SAndrey Ryabinin {
2187e088978SAndrey Ryabinin 	unsigned long flags;
2197e088978SAndrey Ryabinin 
2207e088978SAndrey Ryabinin 	kasan_start_report(&flags);
2217e088978SAndrey Ryabinin 	pr_err("BUG: Double free or freeing an invalid pointer\n");
2227e088978SAndrey Ryabinin 	pr_err("Unexpected shadow byte: 0x%hhX\n", shadow);
223db429f16SAndrey Konovalov 	dump_stack();
224db429f16SAndrey Konovalov 	describe_object(cache, object);
2257e088978SAndrey Ryabinin 	kasan_end_report(&flags);
2267e088978SAndrey Ryabinin }
2277e088978SAndrey Ryabinin 
2280b24beccSAndrey Ryabinin static void print_address_description(struct kasan_access_info *info)
2290b24beccSAndrey Ryabinin {
230b8c73fc2SAndrey Ryabinin 	const void *addr = info->access_addr;
231db429f16SAndrey Konovalov 	struct page *page = addr_to_page(addr);
232b8c73fc2SAndrey Ryabinin 
233db429f16SAndrey Konovalov 	if (page)
234b8c73fc2SAndrey Ryabinin 		dump_page(page, "kasan: bad access detected");
235db429f16SAndrey Konovalov 
236db429f16SAndrey Konovalov 	dump_stack();
237db429f16SAndrey Konovalov 
238db429f16SAndrey Konovalov 	if (page && PageSlab(page)) {
239db429f16SAndrey Konovalov 		struct kmem_cache *cache = page->slab_cache;
240db429f16SAndrey Konovalov 		void *object = nearest_obj(cache, page,	(void *)addr);
241db429f16SAndrey Konovalov 
242db429f16SAndrey Konovalov 		describe_object(cache, object);
243b8c73fc2SAndrey Ryabinin 	}
244b8c73fc2SAndrey Ryabinin 
245bebf56a1SAndrey Ryabinin 	if (kernel_or_module_addr(addr)) {
246bebf56a1SAndrey Ryabinin 		if (!init_task_stack_addr(addr))
247bebf56a1SAndrey Ryabinin 			pr_err("Address belongs to variable %pS\n", addr);
248bebf56a1SAndrey Ryabinin 	}
2490b24beccSAndrey Ryabinin }
2500b24beccSAndrey Ryabinin 
2510b24beccSAndrey Ryabinin static bool row_is_guilty(const void *row, const void *guilty)
2520b24beccSAndrey Ryabinin {
2530b24beccSAndrey Ryabinin 	return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
2540b24beccSAndrey Ryabinin }
2550b24beccSAndrey Ryabinin 
2560b24beccSAndrey Ryabinin static int shadow_pointer_offset(const void *row, const void *shadow)
2570b24beccSAndrey Ryabinin {
2580b24beccSAndrey Ryabinin 	/* The length of ">ff00ff00ff00ff00: " is
2590b24beccSAndrey Ryabinin 	 *    3 + (BITS_PER_LONG/8)*2 chars.
2600b24beccSAndrey Ryabinin 	 */
2610b24beccSAndrey Ryabinin 	return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
2620b24beccSAndrey Ryabinin 		(shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
2630b24beccSAndrey Ryabinin }
2640b24beccSAndrey Ryabinin 
2650b24beccSAndrey Ryabinin static void print_shadow_for_address(const void *addr)
2660b24beccSAndrey Ryabinin {
2670b24beccSAndrey Ryabinin 	int i;
2680b24beccSAndrey Ryabinin 	const void *shadow = kasan_mem_to_shadow(addr);
2690b24beccSAndrey Ryabinin 	const void *shadow_row;
2700b24beccSAndrey Ryabinin 
2710b24beccSAndrey Ryabinin 	shadow_row = (void *)round_down((unsigned long)shadow,
2720b24beccSAndrey Ryabinin 					SHADOW_BYTES_PER_ROW)
2730b24beccSAndrey Ryabinin 		- SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
2740b24beccSAndrey Ryabinin 
2750b24beccSAndrey Ryabinin 	pr_err("Memory state around the buggy address:\n");
2760b24beccSAndrey Ryabinin 
2770b24beccSAndrey Ryabinin 	for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
2780b24beccSAndrey Ryabinin 		const void *kaddr = kasan_shadow_to_mem(shadow_row);
2790b24beccSAndrey Ryabinin 		char buffer[4 + (BITS_PER_LONG/8)*2];
280f2377d4eSAneesh Kumar K.V 		char shadow_buf[SHADOW_BYTES_PER_ROW];
2810b24beccSAndrey Ryabinin 
2820b24beccSAndrey Ryabinin 		snprintf(buffer, sizeof(buffer),
2830b24beccSAndrey Ryabinin 			(i == 0) ? ">%p: " : " %p: ", kaddr);
284f2377d4eSAneesh Kumar K.V 		/*
285f2377d4eSAneesh Kumar K.V 		 * We should not pass a shadow pointer to generic
286f2377d4eSAneesh Kumar K.V 		 * function, because generic functions may try to
287f2377d4eSAneesh Kumar K.V 		 * access kasan mapping for the passed address.
288f2377d4eSAneesh Kumar K.V 		 */
289f2377d4eSAneesh Kumar K.V 		memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
2900b24beccSAndrey Ryabinin 		print_hex_dump(KERN_ERR, buffer,
2910b24beccSAndrey Ryabinin 			DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
292f2377d4eSAneesh Kumar K.V 			shadow_buf, SHADOW_BYTES_PER_ROW, 0);
2930b24beccSAndrey Ryabinin 
2940b24beccSAndrey Ryabinin 		if (row_is_guilty(shadow_row, shadow))
2950b24beccSAndrey Ryabinin 			pr_err("%*c\n",
2960b24beccSAndrey Ryabinin 				shadow_pointer_offset(shadow_row, shadow),
2970b24beccSAndrey Ryabinin 				'^');
2980b24beccSAndrey Ryabinin 
2990b24beccSAndrey Ryabinin 		shadow_row += SHADOW_BYTES_PER_ROW;
3000b24beccSAndrey Ryabinin 	}
3010b24beccSAndrey Ryabinin }
3020b24beccSAndrey Ryabinin 
303e9121076SAndrey Konovalov static void kasan_report_error(struct kasan_access_info *info)
3040b24beccSAndrey Ryabinin {
3050b24beccSAndrey Ryabinin 	unsigned long flags;
3060b24beccSAndrey Ryabinin 
3077e088978SAndrey Ryabinin 	kasan_start_report(&flags);
3087e088978SAndrey Ryabinin 
3097d418f7bSAndrey Konovalov 	print_error_description(info);
3107d418f7bSAndrey Konovalov 
3115e82cd12SAndrey Konovalov 	if (!addr_has_shadow(info)) {
312e9121076SAndrey Konovalov 		dump_stack();
313e9121076SAndrey Konovalov 	} else {
3140b24beccSAndrey Ryabinin 		print_address_description(info);
3150b24beccSAndrey Ryabinin 		print_shadow_for_address(info->first_bad_addr);
3160b24beccSAndrey Ryabinin 	}
3177e088978SAndrey Ryabinin 
3187e088978SAndrey Ryabinin 	kasan_end_report(&flags);
3190b24beccSAndrey Ryabinin }
3200b24beccSAndrey Ryabinin 
321b0845ce5SMark Rutland static unsigned long kasan_flags;
322b0845ce5SMark Rutland 
323b0845ce5SMark Rutland #define KASAN_BIT_REPORTED	0
324b0845ce5SMark Rutland #define KASAN_BIT_MULTI_SHOT	1
325b0845ce5SMark Rutland 
326b0845ce5SMark Rutland bool kasan_save_enable_multi_shot(void)
327b0845ce5SMark Rutland {
328b0845ce5SMark Rutland 	return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
329b0845ce5SMark Rutland }
330b0845ce5SMark Rutland EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
331b0845ce5SMark Rutland 
332b0845ce5SMark Rutland void kasan_restore_multi_shot(bool enabled)
333b0845ce5SMark Rutland {
334b0845ce5SMark Rutland 	if (!enabled)
335b0845ce5SMark Rutland 		clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
336b0845ce5SMark Rutland }
337b0845ce5SMark Rutland EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
338b0845ce5SMark Rutland 
339b0845ce5SMark Rutland static int __init kasan_set_multi_shot(char *str)
340b0845ce5SMark Rutland {
341b0845ce5SMark Rutland 	set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
342b0845ce5SMark Rutland 	return 1;
343b0845ce5SMark Rutland }
344b0845ce5SMark Rutland __setup("kasan_multi_shot", kasan_set_multi_shot);
345b0845ce5SMark Rutland 
346b0845ce5SMark Rutland static inline bool kasan_report_enabled(void)
347b0845ce5SMark Rutland {
348b0845ce5SMark Rutland 	if (current->kasan_depth)
349b0845ce5SMark Rutland 		return false;
350b0845ce5SMark Rutland 	if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
351b0845ce5SMark Rutland 		return true;
352b0845ce5SMark Rutland 	return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
353b0845ce5SMark Rutland }
354b0845ce5SMark Rutland 
3550b24beccSAndrey Ryabinin void kasan_report(unsigned long addr, size_t size,
3560b24beccSAndrey Ryabinin 		bool is_write, unsigned long ip)
3570b24beccSAndrey Ryabinin {
3580b24beccSAndrey Ryabinin 	struct kasan_access_info info;
3590b24beccSAndrey Ryabinin 
3600ba8663cSAneesh Kumar K.V 	if (likely(!kasan_report_enabled()))
3610b24beccSAndrey Ryabinin 		return;
3620b24beccSAndrey Ryabinin 
3634f40c6e5SPeter Zijlstra 	disable_trace_on_warning();
3644f40c6e5SPeter Zijlstra 
3650b24beccSAndrey Ryabinin 	info.access_addr = (void *)addr;
3660b24beccSAndrey Ryabinin 	info.access_size = size;
3670b24beccSAndrey Ryabinin 	info.is_write = is_write;
3680b24beccSAndrey Ryabinin 	info.ip = ip;
369e9121076SAndrey Konovalov 
3700b24beccSAndrey Ryabinin 	kasan_report_error(&info);
3710b24beccSAndrey Ryabinin }
3720b24beccSAndrey Ryabinin 
3730b24beccSAndrey Ryabinin 
3740b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_LOAD(size)                     \
3750b24beccSAndrey Ryabinin void __asan_report_load##size##_noabort(unsigned long addr) \
3760b24beccSAndrey Ryabinin {                                                         \
3770b24beccSAndrey Ryabinin 	kasan_report(addr, size, false, _RET_IP_);	  \
3780b24beccSAndrey Ryabinin }                                                         \
3790b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load##size##_noabort)
3800b24beccSAndrey Ryabinin 
3810b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_STORE(size)                     \
3820b24beccSAndrey Ryabinin void __asan_report_store##size##_noabort(unsigned long addr) \
3830b24beccSAndrey Ryabinin {                                                          \
3840b24beccSAndrey Ryabinin 	kasan_report(addr, size, true, _RET_IP_);	   \
3850b24beccSAndrey Ryabinin }                                                          \
3860b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store##size##_noabort)
3870b24beccSAndrey Ryabinin 
3880b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(1);
3890b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(2);
3900b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(4);
3910b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(8);
3920b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(16);
3930b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(1);
3940b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(2);
3950b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(4);
3960b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(8);
3970b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(16);
3980b24beccSAndrey Ryabinin 
3990b24beccSAndrey Ryabinin void __asan_report_load_n_noabort(unsigned long addr, size_t size)
4000b24beccSAndrey Ryabinin {
4010b24beccSAndrey Ryabinin 	kasan_report(addr, size, false, _RET_IP_);
4020b24beccSAndrey Ryabinin }
4030b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load_n_noabort);
4040b24beccSAndrey Ryabinin 
4050b24beccSAndrey Ryabinin void __asan_report_store_n_noabort(unsigned long addr, size_t size)
4060b24beccSAndrey Ryabinin {
4070b24beccSAndrey Ryabinin 	kasan_report(addr, size, true, _RET_IP_);
4080b24beccSAndrey Ryabinin }
4090b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store_n_noabort);
410