xref: /openbmc/linux/mm/kasan/report.c (revision e91210766341cb356ead7fd39f07493a3d00b80f)
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  *
70b24beccSAndrey Ryabinin  * Some of code borrowed from https://github.com/xairy/linux 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 
160b24beccSAndrey Ryabinin #include <linux/kernel.h>
170b24beccSAndrey Ryabinin #include <linux/mm.h>
180b24beccSAndrey Ryabinin #include <linux/printk.h>
190b24beccSAndrey Ryabinin #include <linux/sched.h>
200b24beccSAndrey Ryabinin #include <linux/slab.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>
260b24beccSAndrey Ryabinin 
27bebf56a1SAndrey Ryabinin #include <asm/sections.h>
28bebf56a1SAndrey Ryabinin 
290b24beccSAndrey Ryabinin #include "kasan.h"
300316bec2SAndrey Ryabinin #include "../slab.h"
310b24beccSAndrey Ryabinin 
320b24beccSAndrey Ryabinin /* Shadow layout customization. */
330b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_BLOCK 1
340b24beccSAndrey Ryabinin #define SHADOW_BLOCKS_PER_ROW 16
350b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK)
360b24beccSAndrey Ryabinin #define SHADOW_ROWS_AROUND_ADDR 2
370b24beccSAndrey Ryabinin 
380b24beccSAndrey Ryabinin static const void *find_first_bad_addr(const void *addr, size_t size)
390b24beccSAndrey Ryabinin {
400b24beccSAndrey Ryabinin 	u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr);
410b24beccSAndrey Ryabinin 	const void *first_bad_addr = addr;
420b24beccSAndrey Ryabinin 
430b24beccSAndrey Ryabinin 	while (!shadow_val && first_bad_addr < addr + size) {
440b24beccSAndrey Ryabinin 		first_bad_addr += KASAN_SHADOW_SCALE_SIZE;
450b24beccSAndrey Ryabinin 		shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr);
460b24beccSAndrey Ryabinin 	}
470b24beccSAndrey Ryabinin 	return first_bad_addr;
480b24beccSAndrey Ryabinin }
490b24beccSAndrey Ryabinin 
500b24beccSAndrey Ryabinin static void print_error_description(struct kasan_access_info *info)
510b24beccSAndrey Ryabinin {
520b24beccSAndrey Ryabinin 	const char *bug_type = "unknown crash";
530b24beccSAndrey Ryabinin 	u8 shadow_val;
540b24beccSAndrey Ryabinin 
550b24beccSAndrey Ryabinin 	info->first_bad_addr = find_first_bad_addr(info->access_addr,
560b24beccSAndrey Ryabinin 						info->access_size);
570b24beccSAndrey Ryabinin 
580b24beccSAndrey Ryabinin 	shadow_val = *(u8 *)kasan_mem_to_shadow(info->first_bad_addr);
590b24beccSAndrey Ryabinin 
600b24beccSAndrey Ryabinin 	switch (shadow_val) {
61b8c73fc2SAndrey Ryabinin 	case KASAN_FREE_PAGE:
620316bec2SAndrey Ryabinin 	case KASAN_KMALLOC_FREE:
63b8c73fc2SAndrey Ryabinin 		bug_type = "use after free";
64b8c73fc2SAndrey Ryabinin 		break;
650316bec2SAndrey Ryabinin 	case KASAN_PAGE_REDZONE:
660316bec2SAndrey Ryabinin 	case KASAN_KMALLOC_REDZONE:
67bebf56a1SAndrey Ryabinin 	case KASAN_GLOBAL_REDZONE:
680b24beccSAndrey Ryabinin 	case 0 ... KASAN_SHADOW_SCALE_SIZE - 1:
690b24beccSAndrey Ryabinin 		bug_type = "out of bounds access";
700b24beccSAndrey Ryabinin 		break;
71c420f167SAndrey Ryabinin 	case KASAN_STACK_LEFT:
72c420f167SAndrey Ryabinin 	case KASAN_STACK_MID:
73c420f167SAndrey Ryabinin 	case KASAN_STACK_RIGHT:
74c420f167SAndrey Ryabinin 	case KASAN_STACK_PARTIAL:
75c420f167SAndrey Ryabinin 		bug_type = "out of bounds on stack";
76c420f167SAndrey Ryabinin 		break;
770b24beccSAndrey Ryabinin 	}
780b24beccSAndrey Ryabinin 
790b24beccSAndrey Ryabinin 	pr_err("BUG: KASan: %s in %pS at addr %p\n",
800b24beccSAndrey Ryabinin 		bug_type, (void *)info->ip,
810b24beccSAndrey Ryabinin 		info->access_addr);
820b24beccSAndrey Ryabinin 	pr_err("%s of size %zu by task %s/%d\n",
830b24beccSAndrey Ryabinin 		info->is_write ? "Write" : "Read",
840b24beccSAndrey Ryabinin 		info->access_size, current->comm, task_pid_nr(current));
850b24beccSAndrey Ryabinin }
860b24beccSAndrey Ryabinin 
87bebf56a1SAndrey Ryabinin static inline bool kernel_or_module_addr(const void *addr)
88bebf56a1SAndrey Ryabinin {
89527f215bSAneesh Kumar K.V 	if (addr >= (void *)_stext && addr < (void *)_end)
90527f215bSAneesh Kumar K.V 		return true;
91527f215bSAneesh Kumar K.V 	if (is_module_address((unsigned long)addr))
92527f215bSAneesh Kumar K.V 		return true;
93527f215bSAneesh Kumar K.V 	return false;
94bebf56a1SAndrey Ryabinin }
95bebf56a1SAndrey Ryabinin 
96bebf56a1SAndrey Ryabinin static inline bool init_task_stack_addr(const void *addr)
97bebf56a1SAndrey Ryabinin {
98bebf56a1SAndrey Ryabinin 	return addr >= (void *)&init_thread_union.stack &&
99bebf56a1SAndrey Ryabinin 		(addr <= (void *)&init_thread_union.stack +
100bebf56a1SAndrey Ryabinin 			sizeof(init_thread_union.stack));
101bebf56a1SAndrey Ryabinin }
102bebf56a1SAndrey Ryabinin 
1030b24beccSAndrey Ryabinin static void print_address_description(struct kasan_access_info *info)
1040b24beccSAndrey Ryabinin {
105b8c73fc2SAndrey Ryabinin 	const void *addr = info->access_addr;
106b8c73fc2SAndrey Ryabinin 
107b8c73fc2SAndrey Ryabinin 	if ((addr >= (void *)PAGE_OFFSET) &&
108b8c73fc2SAndrey Ryabinin 		(addr < high_memory)) {
109b8c73fc2SAndrey Ryabinin 		struct page *page = virt_to_head_page(addr);
1100316bec2SAndrey Ryabinin 
1110316bec2SAndrey Ryabinin 		if (PageSlab(page)) {
1120316bec2SAndrey Ryabinin 			void *object;
1130316bec2SAndrey Ryabinin 			struct kmem_cache *cache = page->slab_cache;
1140316bec2SAndrey Ryabinin 			void *last_object;
1150316bec2SAndrey Ryabinin 
1160316bec2SAndrey Ryabinin 			object = virt_to_obj(cache, page_address(page), addr);
1170316bec2SAndrey Ryabinin 			last_object = page_address(page) +
1180316bec2SAndrey Ryabinin 				page->objects * cache->size;
1190316bec2SAndrey Ryabinin 
1200316bec2SAndrey Ryabinin 			if (unlikely(object > last_object))
1210316bec2SAndrey Ryabinin 				object = last_object; /* we hit into padding */
1220316bec2SAndrey Ryabinin 
1230316bec2SAndrey Ryabinin 			object_err(cache, page, object,
1240316bec2SAndrey Ryabinin 				"kasan: bad access detected");
1250316bec2SAndrey Ryabinin 			return;
1260316bec2SAndrey Ryabinin 		}
127b8c73fc2SAndrey Ryabinin 		dump_page(page, "kasan: bad access detected");
128b8c73fc2SAndrey Ryabinin 	}
129b8c73fc2SAndrey Ryabinin 
130bebf56a1SAndrey Ryabinin 	if (kernel_or_module_addr(addr)) {
131bebf56a1SAndrey Ryabinin 		if (!init_task_stack_addr(addr))
132bebf56a1SAndrey Ryabinin 			pr_err("Address belongs to variable %pS\n", addr);
133bebf56a1SAndrey Ryabinin 	}
134bebf56a1SAndrey Ryabinin 
1350b24beccSAndrey Ryabinin 	dump_stack();
1360b24beccSAndrey Ryabinin }
1370b24beccSAndrey Ryabinin 
1380b24beccSAndrey Ryabinin static bool row_is_guilty(const void *row, const void *guilty)
1390b24beccSAndrey Ryabinin {
1400b24beccSAndrey Ryabinin 	return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
1410b24beccSAndrey Ryabinin }
1420b24beccSAndrey Ryabinin 
1430b24beccSAndrey Ryabinin static int shadow_pointer_offset(const void *row, const void *shadow)
1440b24beccSAndrey Ryabinin {
1450b24beccSAndrey Ryabinin 	/* The length of ">ff00ff00ff00ff00: " is
1460b24beccSAndrey Ryabinin 	 *    3 + (BITS_PER_LONG/8)*2 chars.
1470b24beccSAndrey Ryabinin 	 */
1480b24beccSAndrey Ryabinin 	return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
1490b24beccSAndrey Ryabinin 		(shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
1500b24beccSAndrey Ryabinin }
1510b24beccSAndrey Ryabinin 
1520b24beccSAndrey Ryabinin static void print_shadow_for_address(const void *addr)
1530b24beccSAndrey Ryabinin {
1540b24beccSAndrey Ryabinin 	int i;
1550b24beccSAndrey Ryabinin 	const void *shadow = kasan_mem_to_shadow(addr);
1560b24beccSAndrey Ryabinin 	const void *shadow_row;
1570b24beccSAndrey Ryabinin 
1580b24beccSAndrey Ryabinin 	shadow_row = (void *)round_down((unsigned long)shadow,
1590b24beccSAndrey Ryabinin 					SHADOW_BYTES_PER_ROW)
1600b24beccSAndrey Ryabinin 		- SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
1610b24beccSAndrey Ryabinin 
1620b24beccSAndrey Ryabinin 	pr_err("Memory state around the buggy address:\n");
1630b24beccSAndrey Ryabinin 
1640b24beccSAndrey Ryabinin 	for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
1650b24beccSAndrey Ryabinin 		const void *kaddr = kasan_shadow_to_mem(shadow_row);
1660b24beccSAndrey Ryabinin 		char buffer[4 + (BITS_PER_LONG/8)*2];
167f2377d4eSAneesh Kumar K.V 		char shadow_buf[SHADOW_BYTES_PER_ROW];
1680b24beccSAndrey Ryabinin 
1690b24beccSAndrey Ryabinin 		snprintf(buffer, sizeof(buffer),
1700b24beccSAndrey Ryabinin 			(i == 0) ? ">%p: " : " %p: ", kaddr);
171f2377d4eSAneesh Kumar K.V 		/*
172f2377d4eSAneesh Kumar K.V 		 * We should not pass a shadow pointer to generic
173f2377d4eSAneesh Kumar K.V 		 * function, because generic functions may try to
174f2377d4eSAneesh Kumar K.V 		 * access kasan mapping for the passed address.
175f2377d4eSAneesh Kumar K.V 		 */
176f2377d4eSAneesh Kumar K.V 		memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
1770b24beccSAndrey Ryabinin 		print_hex_dump(KERN_ERR, buffer,
1780b24beccSAndrey Ryabinin 			DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
179f2377d4eSAneesh Kumar K.V 			shadow_buf, SHADOW_BYTES_PER_ROW, 0);
1800b24beccSAndrey Ryabinin 
1810b24beccSAndrey Ryabinin 		if (row_is_guilty(shadow_row, shadow))
1820b24beccSAndrey Ryabinin 			pr_err("%*c\n",
1830b24beccSAndrey Ryabinin 				shadow_pointer_offset(shadow_row, shadow),
1840b24beccSAndrey Ryabinin 				'^');
1850b24beccSAndrey Ryabinin 
1860b24beccSAndrey Ryabinin 		shadow_row += SHADOW_BYTES_PER_ROW;
1870b24beccSAndrey Ryabinin 	}
1880b24beccSAndrey Ryabinin }
1890b24beccSAndrey Ryabinin 
1900b24beccSAndrey Ryabinin static DEFINE_SPINLOCK(report_lock);
1910b24beccSAndrey Ryabinin 
192*e9121076SAndrey Konovalov static void kasan_report_error(struct kasan_access_info *info)
1930b24beccSAndrey Ryabinin {
1940b24beccSAndrey Ryabinin 	unsigned long flags;
195*e9121076SAndrey Konovalov 	const char *bug_type;
1960b24beccSAndrey Ryabinin 
197fc5aeeafSAneesh Kumar K.V 	/*
198fc5aeeafSAneesh Kumar K.V 	 * Make sure we don't end up in loop.
199fc5aeeafSAneesh Kumar K.V 	 */
200fc5aeeafSAneesh Kumar K.V 	kasan_disable_current();
2010b24beccSAndrey Ryabinin 	spin_lock_irqsave(&report_lock, flags);
2020b24beccSAndrey Ryabinin 	pr_err("================================="
2030b24beccSAndrey Ryabinin 		"=================================\n");
204*e9121076SAndrey Konovalov 	if (info->access_addr <
205*e9121076SAndrey Konovalov 			kasan_shadow_to_mem((void *)KASAN_SHADOW_START)) {
206*e9121076SAndrey Konovalov 		if ((unsigned long)info->access_addr < PAGE_SIZE)
207*e9121076SAndrey Konovalov 			bug_type = "null-ptr-deref";
208*e9121076SAndrey Konovalov 		else if ((unsigned long)info->access_addr < TASK_SIZE)
209*e9121076SAndrey Konovalov 			bug_type = "user-memory-access";
210*e9121076SAndrey Konovalov 		else
211*e9121076SAndrey Konovalov 			bug_type = "wild-memory-access";
212*e9121076SAndrey Konovalov 		pr_err("BUG: KASan: %s on address %p\n",
213*e9121076SAndrey Konovalov 			bug_type, info->access_addr);
214*e9121076SAndrey Konovalov 		pr_err("%s of size %zu by task %s/%d\n",
215*e9121076SAndrey Konovalov 			info->is_write ? "Write" : "Read",
216*e9121076SAndrey Konovalov 			info->access_size, current->comm,
217*e9121076SAndrey Konovalov 			task_pid_nr(current));
218*e9121076SAndrey Konovalov 		dump_stack();
219*e9121076SAndrey Konovalov 	} else {
2200b24beccSAndrey Ryabinin 		print_error_description(info);
2210b24beccSAndrey Ryabinin 		print_address_description(info);
2220b24beccSAndrey Ryabinin 		print_shadow_for_address(info->first_bad_addr);
2230b24beccSAndrey Ryabinin 	}
2240b24beccSAndrey Ryabinin 	pr_err("================================="
2250b24beccSAndrey Ryabinin 		"=================================\n");
2260b24beccSAndrey Ryabinin 	spin_unlock_irqrestore(&report_lock, flags);
227fc5aeeafSAneesh Kumar K.V 	kasan_enable_current();
2280b24beccSAndrey Ryabinin }
2290b24beccSAndrey Ryabinin 
2300b24beccSAndrey Ryabinin void kasan_report(unsigned long addr, size_t size,
2310b24beccSAndrey Ryabinin 		bool is_write, unsigned long ip)
2320b24beccSAndrey Ryabinin {
2330b24beccSAndrey Ryabinin 	struct kasan_access_info info;
2340b24beccSAndrey Ryabinin 
2350ba8663cSAneesh Kumar K.V 	if (likely(!kasan_report_enabled()))
2360b24beccSAndrey Ryabinin 		return;
2370b24beccSAndrey Ryabinin 
2380b24beccSAndrey Ryabinin 	info.access_addr = (void *)addr;
2390b24beccSAndrey Ryabinin 	info.access_size = size;
2400b24beccSAndrey Ryabinin 	info.is_write = is_write;
2410b24beccSAndrey Ryabinin 	info.ip = ip;
242*e9121076SAndrey Konovalov 
2430b24beccSAndrey Ryabinin 	kasan_report_error(&info);
2440b24beccSAndrey Ryabinin }
2450b24beccSAndrey Ryabinin 
2460b24beccSAndrey Ryabinin 
2470b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_LOAD(size)                     \
2480b24beccSAndrey Ryabinin void __asan_report_load##size##_noabort(unsigned long addr) \
2490b24beccSAndrey Ryabinin {                                                         \
2500b24beccSAndrey Ryabinin 	kasan_report(addr, size, false, _RET_IP_);	  \
2510b24beccSAndrey Ryabinin }                                                         \
2520b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load##size##_noabort)
2530b24beccSAndrey Ryabinin 
2540b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_STORE(size)                     \
2550b24beccSAndrey Ryabinin void __asan_report_store##size##_noabort(unsigned long addr) \
2560b24beccSAndrey Ryabinin {                                                          \
2570b24beccSAndrey Ryabinin 	kasan_report(addr, size, true, _RET_IP_);	   \
2580b24beccSAndrey Ryabinin }                                                          \
2590b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store##size##_noabort)
2600b24beccSAndrey Ryabinin 
2610b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(1);
2620b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(2);
2630b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(4);
2640b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(8);
2650b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(16);
2660b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(1);
2670b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(2);
2680b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(4);
2690b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(8);
2700b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(16);
2710b24beccSAndrey Ryabinin 
2720b24beccSAndrey Ryabinin void __asan_report_load_n_noabort(unsigned long addr, size_t size)
2730b24beccSAndrey Ryabinin {
2740b24beccSAndrey Ryabinin 	kasan_report(addr, size, false, _RET_IP_);
2750b24beccSAndrey Ryabinin }
2760b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load_n_noabort);
2770b24beccSAndrey Ryabinin 
2780b24beccSAndrey Ryabinin void __asan_report_store_n_noabort(unsigned long addr, size_t size)
2790b24beccSAndrey Ryabinin {
2800b24beccSAndrey Ryabinin 	kasan_report(addr, size, true, _RET_IP_);
2810b24beccSAndrey Ryabinin }
2820b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store_n_noabort);
283