xref: /openbmc/linux/mm/kasan/report.c (revision 22d55f02)
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 		unsigned long *entries;
104 		unsigned int nr_entries;
105 
106 		nr_entries = stack_depot_fetch(track->stack, &entries);
107 		stack_trace_print(entries, nr_entries, 0);
108 	} else {
109 		pr_err("(stack is not available)\n");
110 	}
111 }
112 
113 static struct page *addr_to_page(const void *addr)
114 {
115 	if ((addr >= (void *)PAGE_OFFSET) &&
116 			(addr < high_memory))
117 		return virt_to_head_page(addr);
118 	return NULL;
119 }
120 
121 static void describe_object_addr(struct kmem_cache *cache, void *object,
122 				const void *addr)
123 {
124 	unsigned long access_addr = (unsigned long)addr;
125 	unsigned long object_addr = (unsigned long)object;
126 	const char *rel_type;
127 	int rel_bytes;
128 
129 	pr_err("The buggy address belongs to the object at %px\n"
130 	       " which belongs to the cache %s of size %d\n",
131 		object, cache->name, cache->object_size);
132 
133 	if (!addr)
134 		return;
135 
136 	if (access_addr < object_addr) {
137 		rel_type = "to the left";
138 		rel_bytes = object_addr - access_addr;
139 	} else if (access_addr >= object_addr + cache->object_size) {
140 		rel_type = "to the right";
141 		rel_bytes = access_addr - (object_addr + cache->object_size);
142 	} else {
143 		rel_type = "inside";
144 		rel_bytes = access_addr - object_addr;
145 	}
146 
147 	pr_err("The buggy address is located %d bytes %s of\n"
148 	       " %d-byte region [%px, %px)\n",
149 		rel_bytes, rel_type, cache->object_size, (void *)object_addr,
150 		(void *)(object_addr + cache->object_size));
151 }
152 
153 static void describe_object(struct kmem_cache *cache, void *object,
154 				const void *addr)
155 {
156 	struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
157 
158 	if (cache->flags & SLAB_KASAN) {
159 		print_track(&alloc_info->alloc_track, "Allocated");
160 		pr_err("\n");
161 		print_track(&alloc_info->free_track, "Freed");
162 		pr_err("\n");
163 	}
164 
165 	describe_object_addr(cache, object, addr);
166 }
167 
168 static inline bool kernel_or_module_addr(const void *addr)
169 {
170 	if (addr >= (void *)_stext && addr < (void *)_end)
171 		return true;
172 	if (is_module_address((unsigned long)addr))
173 		return true;
174 	return false;
175 }
176 
177 static inline bool init_task_stack_addr(const void *addr)
178 {
179 	return addr >= (void *)&init_thread_union.stack &&
180 		(addr <= (void *)&init_thread_union.stack +
181 			sizeof(init_thread_union.stack));
182 }
183 
184 static void print_address_description(void *addr)
185 {
186 	struct page *page = addr_to_page(addr);
187 
188 	dump_stack();
189 	pr_err("\n");
190 
191 	if (page && PageSlab(page)) {
192 		struct kmem_cache *cache = page->slab_cache;
193 		void *object = nearest_obj(cache, page,	addr);
194 
195 		describe_object(cache, object, addr);
196 	}
197 
198 	if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
199 		pr_err("The buggy address belongs to the variable:\n");
200 		pr_err(" %pS\n", addr);
201 	}
202 
203 	if (page) {
204 		pr_err("The buggy address belongs to the page:\n");
205 		dump_page(page, "kasan: bad access detected");
206 	}
207 }
208 
209 static bool row_is_guilty(const void *row, const void *guilty)
210 {
211 	return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW);
212 }
213 
214 static int shadow_pointer_offset(const void *row, const void *shadow)
215 {
216 	/* The length of ">ff00ff00ff00ff00: " is
217 	 *    3 + (BITS_PER_LONG/8)*2 chars.
218 	 */
219 	return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
220 		(shadow - row) / SHADOW_BYTES_PER_BLOCK + 1;
221 }
222 
223 static void print_shadow_for_address(const void *addr)
224 {
225 	int i;
226 	const void *shadow = kasan_mem_to_shadow(addr);
227 	const void *shadow_row;
228 
229 	shadow_row = (void *)round_down((unsigned long)shadow,
230 					SHADOW_BYTES_PER_ROW)
231 		- SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW;
232 
233 	pr_err("Memory state around the buggy address:\n");
234 
235 	for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) {
236 		const void *kaddr = kasan_shadow_to_mem(shadow_row);
237 		char buffer[4 + (BITS_PER_LONG/8)*2];
238 		char shadow_buf[SHADOW_BYTES_PER_ROW];
239 
240 		snprintf(buffer, sizeof(buffer),
241 			(i == 0) ? ">%px: " : " %px: ", kaddr);
242 		/*
243 		 * We should not pass a shadow pointer to generic
244 		 * function, because generic functions may try to
245 		 * access kasan mapping for the passed address.
246 		 */
247 		memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW);
248 		print_hex_dump(KERN_ERR, buffer,
249 			DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1,
250 			shadow_buf, SHADOW_BYTES_PER_ROW, 0);
251 
252 		if (row_is_guilty(shadow_row, shadow))
253 			pr_err("%*c\n",
254 				shadow_pointer_offset(shadow_row, shadow),
255 				'^');
256 
257 		shadow_row += SHADOW_BYTES_PER_ROW;
258 	}
259 }
260 
261 static bool report_enabled(void)
262 {
263 	if (current->kasan_depth)
264 		return false;
265 	if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
266 		return true;
267 	return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
268 }
269 
270 void kasan_report_invalid_free(void *object, unsigned long ip)
271 {
272 	unsigned long flags;
273 
274 	start_report(&flags);
275 	pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
276 	print_tags(get_tag(object), reset_tag(object));
277 	object = reset_tag(object);
278 	pr_err("\n");
279 	print_address_description(object);
280 	pr_err("\n");
281 	print_shadow_for_address(object);
282 	end_report(&flags);
283 }
284 
285 void __kasan_report(unsigned long addr, size_t size, 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