xref: /openbmc/linux/mm/kasan/report.c (revision 5b4cb650)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This file contains common generic and tag-based KASAN error reporting code.
4  *
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
7  *
8  * Some code borrowed from https://github.com/xairy/kasan-prototype by
9  *        Andrey Konovalov <andreyknvl@gmail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  */
16 
17 #include <linux/bitops.h>
18 #include <linux/ftrace.h>
19 #include <linux/init.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/printk.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/stackdepot.h>
26 #include <linux/stacktrace.h>
27 #include <linux/string.h>
28 #include <linux/types.h>
29 #include <linux/kasan.h>
30 #include <linux/module.h>
31 
32 #include <asm/sections.h>
33 
34 #include "kasan.h"
35 #include "../slab.h"
36 
37 /* Shadow layout customization. */
38 #define SHADOW_BYTES_PER_BLOCK 1
39 #define SHADOW_BLOCKS_PER_ROW 16
40 #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK)
41 #define SHADOW_ROWS_AROUND_ADDR 2
42 
43 static unsigned long kasan_flags;
44 
45 #define KASAN_BIT_REPORTED	0
46 #define KASAN_BIT_MULTI_SHOT	1
47 
48 bool kasan_save_enable_multi_shot(void)
49 {
50 	return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
51 }
52 EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
53 
54 void kasan_restore_multi_shot(bool enabled)
55 {
56 	if (!enabled)
57 		clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
58 }
59 EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
60 
61 static int __init kasan_set_multi_shot(char *str)
62 {
63 	set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
64 	return 1;
65 }
66 __setup("kasan_multi_shot", kasan_set_multi_shot);
67 
68 static void print_error_description(struct kasan_access_info *info)
69 {
70 	pr_err("BUG: KASAN: %s in %pS\n",
71 		get_bug_type(info), (void *)info->ip);
72 	pr_err("%s of size %zu at addr %px by task %s/%d\n",
73 		info->is_write ? "Write" : "Read", info->access_size,
74 		info->access_addr, current->comm, task_pid_nr(current));
75 }
76 
77 static DEFINE_SPINLOCK(report_lock);
78 
79 static void start_report(unsigned long *flags)
80 {
81 	/*
82 	 * Make sure we don't end up in loop.
83 	 */
84 	kasan_disable_current();
85 	spin_lock_irqsave(&report_lock, *flags);
86 	pr_err("==================================================================\n");
87 }
88 
89 static void end_report(unsigned long *flags)
90 {
91 	pr_err("==================================================================\n");
92 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
93 	spin_unlock_irqrestore(&report_lock, *flags);
94 	if (panic_on_warn)
95 		panic("panic_on_warn set ...\n");
96 	kasan_enable_current();
97 }
98 
99 static void print_track(struct kasan_track *track, const char *prefix)
100 {
101 	pr_err("%s by task %u:\n", prefix, track->pid);
102 	if (track->stack) {
103 		struct stack_trace trace;
104 
105 		depot_fetch_stack(track->stack, &trace);
106 		print_stack_trace(&trace, 0);
107 	} else {
108 		pr_err("(stack is not available)\n");
109 	}
110 }
111 
112 static struct page *addr_to_page(const void *addr)
113 {
114 	if ((addr >= (void *)PAGE_OFFSET) &&
115 			(addr < high_memory))
116 		return virt_to_head_page(addr);
117 	return NULL;
118 }
119 
120 static void describe_object_addr(struct kmem_cache *cache, void *object,
121 				const void *addr)
122 {
123 	unsigned long access_addr = (unsigned long)addr;
124 	unsigned long object_addr = (unsigned long)object;
125 	const char *rel_type;
126 	int rel_bytes;
127 
128 	pr_err("The buggy address belongs to the object at %px\n"
129 	       " which belongs to the cache %s of size %d\n",
130 		object, cache->name, cache->object_size);
131 
132 	if (!addr)
133 		return;
134 
135 	if (access_addr < object_addr) {
136 		rel_type = "to the left";
137 		rel_bytes = object_addr - access_addr;
138 	} else if (access_addr >= object_addr + cache->object_size) {
139 		rel_type = "to the right";
140 		rel_bytes = access_addr - (object_addr + cache->object_size);
141 	} else {
142 		rel_type = "inside";
143 		rel_bytes = access_addr - object_addr;
144 	}
145 
146 	pr_err("The buggy address is located %d bytes %s of\n"
147 	       " %d-byte region [%px, %px)\n",
148 		rel_bytes, rel_type, cache->object_size, (void *)object_addr,
149 		(void *)(object_addr + cache->object_size));
150 }
151 
152 static void describe_object(struct kmem_cache *cache, void *object,
153 				const void *addr)
154 {
155 	struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
156 
157 	if (cache->flags & SLAB_KASAN) {
158 		print_track(&alloc_info->alloc_track, "Allocated");
159 		pr_err("\n");
160 		print_track(&alloc_info->free_track, "Freed");
161 		pr_err("\n");
162 	}
163 
164 	describe_object_addr(cache, object, addr);
165 }
166 
167 static inline bool kernel_or_module_addr(const void *addr)
168 {
169 	if (addr >= (void *)_stext && addr < (void *)_end)
170 		return true;
171 	if (is_module_address((unsigned long)addr))
172 		return true;
173 	return false;
174 }
175 
176 static inline bool init_task_stack_addr(const void *addr)
177 {
178 	return addr >= (void *)&init_thread_union.stack &&
179 		(addr <= (void *)&init_thread_union.stack +
180 			sizeof(init_thread_union.stack));
181 }
182 
183 static void print_address_description(void *addr)
184 {
185 	struct page *page = addr_to_page(addr);
186 
187 	dump_stack();
188 	pr_err("\n");
189 
190 	if (page && PageSlab(page)) {
191 		struct kmem_cache *cache = page->slab_cache;
192 		void *object = nearest_obj(cache, page,	addr);
193 
194 		describe_object(cache, object, addr);
195 	}
196 
197 	if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
198 		pr_err("The buggy address belongs to the variable:\n");
199 		pr_err(" %pS\n", addr);
200 	}
201 
202 	if (page) {
203 		pr_err("The buggy address belongs to the page:\n");
204 		dump_page(page, "kasan: bad access detected");
205 	}
206 }
207 
208 static bool row_is_guilty(const void *row, const void *guilty)
209 {
210 	return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
211 }
212 
213 static int shadow_pointer_offset(const void *row, const void *shadow)
214 {
215 	/* The length of ">ff00ff00ff00ff00: " is
216 	 *    3 + (BITS_PER_LONG/8)*2 chars.
217 	 */
218 	return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
219 		(shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
220 }
221 
222 static void print_shadow_for_address(const void *addr)
223 {
224 	int i;
225 	const void *shadow = kasan_mem_to_shadow(addr);
226 	const void *shadow_row;
227 
228 	shadow_row = (void *)round_down((unsigned long)shadow,
229 					SHADOW_BYTES_PER_ROW)
230 		- SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
231 
232 	pr_err("Memory state around the buggy address:\n");
233 
234 	for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
235 		const void *kaddr = kasan_shadow_to_mem(shadow_row);
236 		char buffer[4 + (BITS_PER_LONG/8)*2];
237 		char shadow_buf[SHADOW_BYTES_PER_ROW];
238 
239 		snprintf(buffer, sizeof(buffer),
240 			(i == 0) ? ">%px: " : " %px: ", kaddr);
241 		/*
242 		 * We should not pass a shadow pointer to generic
243 		 * function, because generic functions may try to
244 		 * access kasan mapping for the passed address.
245 		 */
246 		memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
247 		print_hex_dump(KERN_ERR, buffer,
248 			DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
249 			shadow_buf, SHADOW_BYTES_PER_ROW, 0);
250 
251 		if (row_is_guilty(shadow_row, shadow))
252 			pr_err("%*c\n",
253 				shadow_pointer_offset(shadow_row, shadow),
254 				'^');
255 
256 		shadow_row += SHADOW_BYTES_PER_ROW;
257 	}
258 }
259 
260 static bool report_enabled(void)
261 {
262 	if (current->kasan_depth)
263 		return false;
264 	if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
265 		return true;
266 	return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
267 }
268 
269 void kasan_report_invalid_free(void *object, unsigned long ip)
270 {
271 	unsigned long flags;
272 
273 	start_report(&flags);
274 	pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
275 	print_tags(get_tag(object), reset_tag(object));
276 	object = reset_tag(object);
277 	pr_err("\n");
278 	print_address_description(object);
279 	pr_err("\n");
280 	print_shadow_for_address(object);
281 	end_report(&flags);
282 }
283 
284 void kasan_report(unsigned long addr, size_t size,
285 		bool is_write, unsigned long ip)
286 {
287 	struct kasan_access_info info;
288 	void *tagged_addr;
289 	void *untagged_addr;
290 	unsigned long flags;
291 
292 	if (likely(!report_enabled()))
293 		return;
294 
295 	disable_trace_on_warning();
296 
297 	tagged_addr = (void *)addr;
298 	untagged_addr = reset_tag(tagged_addr);
299 
300 	info.access_addr = tagged_addr;
301 	if (addr_has_shadow(untagged_addr))
302 		info.first_bad_addr = find_first_bad_addr(tagged_addr, size);
303 	else
304 		info.first_bad_addr = untagged_addr;
305 	info.access_size = size;
306 	info.is_write = is_write;
307 	info.ip = ip;
308 
309 	start_report(&flags);
310 
311 	print_error_description(&info);
312 	if (addr_has_shadow(untagged_addr))
313 		print_tags(get_tag(tagged_addr), info.first_bad_addr);
314 	pr_err("\n");
315 
316 	if (addr_has_shadow(untagged_addr)) {
317 		print_address_description(untagged_addr);
318 		pr_err("\n");
319 		print_shadow_for_address(info.first_bad_addr);
320 	} else {
321 		dump_stack();
322 	}
323 
324 	end_report(&flags);
325 }
326