xref: /linux/mm/kasan/report.c (revision 52990390f91c1c39ca742fc8f390b29891d95127)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This file contains common 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 
12 #include <kunit/test.h>
13 #include <linux/bitops.h>
14 #include <linux/ftrace.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/lockdep.h>
18 #include <linux/mm.h>
19 #include <linux/printk.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/stackdepot.h>
23 #include <linux/stacktrace.h>
24 #include <linux/string.h>
25 #include <linux/types.h>
26 #include <linux/kasan.h>
27 #include <linux/module.h>
28 #include <linux/sched/task_stack.h>
29 #include <linux/uaccess.h>
30 #include <trace/events/error_report.h>
31 
32 #include <asm/sections.h>
33 
34 #include "kasan.h"
35 #include "../slab.h"
36 
37 static unsigned long kasan_flags;
38 
39 #define KASAN_BIT_REPORTED	0
40 #define KASAN_BIT_MULTI_SHOT	1
41 
42 enum kasan_arg_fault {
43 	KASAN_ARG_FAULT_DEFAULT,
44 	KASAN_ARG_FAULT_REPORT,
45 	KASAN_ARG_FAULT_PANIC,
46 };
47 
48 static enum kasan_arg_fault kasan_arg_fault __ro_after_init = KASAN_ARG_FAULT_DEFAULT;
49 
50 /* kasan.fault=report/panic */
51 static int __init early_kasan_fault(char *arg)
52 {
53 	if (!arg)
54 		return -EINVAL;
55 
56 	if (!strcmp(arg, "report"))
57 		kasan_arg_fault = KASAN_ARG_FAULT_REPORT;
58 	else if (!strcmp(arg, "panic"))
59 		kasan_arg_fault = KASAN_ARG_FAULT_PANIC;
60 	else
61 		return -EINVAL;
62 
63 	return 0;
64 }
65 early_param("kasan.fault", early_kasan_fault);
66 
67 static int __init kasan_set_multi_shot(char *str)
68 {
69 	set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
70 	return 1;
71 }
72 __setup("kasan_multi_shot", kasan_set_multi_shot);
73 
74 /*
75  * This function is used to check whether KASAN reports are suppressed for
76  * software KASAN modes via kasan_disable/enable_current() critical sections.
77  *
78  * This is done to avoid:
79  * 1. False-positive reports when accessing slab metadata,
80  * 2. Deadlocking when poisoned memory is accessed by the reporting code.
81  *
82  * Hardware Tag-Based KASAN instead relies on:
83  * For #1: Resetting tags via kasan_reset_tag().
84  * For #2: Suppression of tag checks via CPU, see report_suppress_start/end().
85  */
86 static bool report_suppressed_sw(void)
87 {
88 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
89 	if (current->kasan_depth)
90 		return true;
91 #endif
92 	return false;
93 }
94 
95 static void report_suppress_start(void)
96 {
97 #ifdef CONFIG_KASAN_HW_TAGS
98 	/*
99 	 * Disable preemption for the duration of printing a KASAN report, as
100 	 * hw_suppress_tag_checks_start() disables checks on the current CPU.
101 	 */
102 	preempt_disable();
103 	hw_suppress_tag_checks_start();
104 #else
105 	kasan_disable_current();
106 #endif
107 }
108 
109 static void report_suppress_stop(void)
110 {
111 #ifdef CONFIG_KASAN_HW_TAGS
112 	hw_suppress_tag_checks_stop();
113 	preempt_enable();
114 #else
115 	kasan_enable_current();
116 #endif
117 }
118 
119 /*
120  * Used to avoid reporting more than one KASAN bug unless kasan_multi_shot
121  * is enabled. Note that KASAN tests effectively enable kasan_multi_shot
122  * for their duration.
123  */
124 static bool report_enabled(void)
125 {
126 	if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
127 		return true;
128 	return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
129 }
130 
131 #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST) || IS_ENABLED(CONFIG_KASAN_MODULE_TEST)
132 
133 bool kasan_save_enable_multi_shot(void)
134 {
135 	return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
136 }
137 EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
138 
139 void kasan_restore_multi_shot(bool enabled)
140 {
141 	if (!enabled)
142 		clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
143 }
144 EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
145 
146 #endif
147 
148 #if IS_ENABLED(CONFIG_KASAN_KUNIT_TEST)
149 
150 /*
151  * Whether the KASAN KUnit test suite is currently being executed.
152  * Updated in kasan_test.c.
153  */
154 static bool kasan_kunit_executing;
155 
156 void kasan_kunit_test_suite_start(void)
157 {
158 	WRITE_ONCE(kasan_kunit_executing, true);
159 }
160 EXPORT_SYMBOL_GPL(kasan_kunit_test_suite_start);
161 
162 void kasan_kunit_test_suite_end(void)
163 {
164 	WRITE_ONCE(kasan_kunit_executing, false);
165 }
166 EXPORT_SYMBOL_GPL(kasan_kunit_test_suite_end);
167 
168 static bool kasan_kunit_test_suite_executing(void)
169 {
170 	return READ_ONCE(kasan_kunit_executing);
171 }
172 
173 #else /* CONFIG_KASAN_KUNIT_TEST */
174 
175 static inline bool kasan_kunit_test_suite_executing(void) { return false; }
176 
177 #endif /* CONFIG_KASAN_KUNIT_TEST */
178 
179 #if IS_ENABLED(CONFIG_KUNIT)
180 
181 static void fail_non_kasan_kunit_test(void)
182 {
183 	struct kunit *test;
184 
185 	if (kasan_kunit_test_suite_executing())
186 		return;
187 
188 	test = current->kunit_test;
189 	if (test)
190 		kunit_set_failure(test);
191 }
192 
193 #else /* CONFIG_KUNIT */
194 
195 static inline void fail_non_kasan_kunit_test(void) { }
196 
197 #endif /* CONFIG_KUNIT */
198 
199 static DEFINE_SPINLOCK(report_lock);
200 
201 static void start_report(unsigned long *flags, bool sync)
202 {
203 	fail_non_kasan_kunit_test();
204 	/* Respect the /proc/sys/kernel/traceoff_on_warning interface. */
205 	disable_trace_on_warning();
206 	/* Do not allow LOCKDEP mangling KASAN reports. */
207 	lockdep_off();
208 	/* Make sure we don't end up in loop. */
209 	report_suppress_start();
210 	spin_lock_irqsave(&report_lock, *flags);
211 	pr_err("==================================================================\n");
212 }
213 
214 static void end_report(unsigned long *flags, void *addr)
215 {
216 	if (addr)
217 		trace_error_report_end(ERROR_DETECTOR_KASAN,
218 				       (unsigned long)addr);
219 	pr_err("==================================================================\n");
220 	spin_unlock_irqrestore(&report_lock, *flags);
221 	if (!test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
222 		check_panic_on_warn("KASAN");
223 	if (kasan_arg_fault == KASAN_ARG_FAULT_PANIC)
224 		panic("kasan.fault=panic set ...\n");
225 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
226 	lockdep_on();
227 	report_suppress_stop();
228 }
229 
230 static void print_error_description(struct kasan_report_info *info)
231 {
232 	pr_err("BUG: KASAN: %s in %pS\n", info->bug_type, (void *)info->ip);
233 
234 	if (info->type != KASAN_REPORT_ACCESS) {
235 		pr_err("Free of addr %px by task %s/%d\n",
236 			info->access_addr, current->comm, task_pid_nr(current));
237 		return;
238 	}
239 
240 	if (info->access_size)
241 		pr_err("%s of size %zu at addr %px by task %s/%d\n",
242 			info->is_write ? "Write" : "Read", info->access_size,
243 			info->access_addr, current->comm, task_pid_nr(current));
244 	else
245 		pr_err("%s at addr %px by task %s/%d\n",
246 			info->is_write ? "Write" : "Read",
247 			info->access_addr, current->comm, task_pid_nr(current));
248 }
249 
250 static void print_track(struct kasan_track *track, const char *prefix)
251 {
252 	pr_err("%s by task %u:\n", prefix, track->pid);
253 	if (track->stack)
254 		stack_depot_print(track->stack);
255 	else
256 		pr_err("(stack is not available)\n");
257 }
258 
259 static inline struct page *addr_to_page(const void *addr)
260 {
261 	if (virt_addr_valid(addr))
262 		return virt_to_head_page(addr);
263 	return NULL;
264 }
265 
266 static void describe_object_addr(const void *addr, struct kasan_report_info *info)
267 {
268 	unsigned long access_addr = (unsigned long)addr;
269 	unsigned long object_addr = (unsigned long)info->object;
270 	const char *rel_type, *region_state = "";
271 	int rel_bytes;
272 
273 	pr_err("The buggy address belongs to the object at %px\n"
274 	       " which belongs to the cache %s of size %d\n",
275 		info->object, info->cache->name, info->cache->object_size);
276 
277 	if (access_addr < object_addr) {
278 		rel_type = "to the left";
279 		rel_bytes = object_addr - access_addr;
280 	} else if (access_addr >= object_addr + info->alloc_size) {
281 		rel_type = "to the right";
282 		rel_bytes = access_addr - (object_addr + info->alloc_size);
283 	} else {
284 		rel_type = "inside";
285 		rel_bytes = access_addr - object_addr;
286 	}
287 
288 	/*
289 	 * Tag-Based modes use the stack ring to infer the bug type, but the
290 	 * memory region state description is generated based on the metadata.
291 	 * Thus, defining the region state as below can contradict the metadata.
292 	 * Fixing this requires further improvements, so only infer the state
293 	 * for the Generic mode.
294 	 */
295 	if (IS_ENABLED(CONFIG_KASAN_GENERIC)) {
296 		if (strcmp(info->bug_type, "slab-out-of-bounds") == 0)
297 			region_state = "allocated ";
298 		else if (strcmp(info->bug_type, "slab-use-after-free") == 0)
299 			region_state = "freed ";
300 	}
301 
302 	pr_err("The buggy address is located %d bytes %s of\n"
303 	       " %s%zu-byte region [%px, %px)\n",
304 	       rel_bytes, rel_type, region_state, info->alloc_size,
305 	       (void *)object_addr, (void *)(object_addr + info->alloc_size));
306 }
307 
308 static void describe_object_stacks(struct kasan_report_info *info)
309 {
310 	if (info->alloc_track.stack) {
311 		print_track(&info->alloc_track, "Allocated");
312 		pr_err("\n");
313 	}
314 
315 	if (info->free_track.stack) {
316 		print_track(&info->free_track, "Freed");
317 		pr_err("\n");
318 	}
319 
320 	kasan_print_aux_stacks(info->cache, info->object);
321 }
322 
323 static void describe_object(const void *addr, struct kasan_report_info *info)
324 {
325 	if (kasan_stack_collection_enabled())
326 		describe_object_stacks(info);
327 	describe_object_addr(addr, info);
328 }
329 
330 static inline bool kernel_or_module_addr(const void *addr)
331 {
332 	if (is_kernel((unsigned long)addr))
333 		return true;
334 	if (is_module_address((unsigned long)addr))
335 		return true;
336 	return false;
337 }
338 
339 static inline bool init_task_stack_addr(const void *addr)
340 {
341 	return addr >= (void *)&init_thread_union.stack &&
342 		(addr <= (void *)&init_thread_union.stack +
343 			sizeof(init_thread_union.stack));
344 }
345 
346 static void print_address_description(void *addr, u8 tag,
347 				      struct kasan_report_info *info)
348 {
349 	struct page *page = addr_to_page(addr);
350 
351 	dump_stack_lvl(KERN_ERR);
352 	pr_err("\n");
353 
354 	if (info->cache && info->object) {
355 		describe_object(addr, info);
356 		pr_err("\n");
357 	}
358 
359 	if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
360 		pr_err("The buggy address belongs to the variable:\n");
361 		pr_err(" %pS\n", addr);
362 		pr_err("\n");
363 	}
364 
365 	if (object_is_on_stack(addr)) {
366 		/*
367 		 * Currently, KASAN supports printing frame information only
368 		 * for accesses to the task's own stack.
369 		 */
370 		kasan_print_address_stack_frame(addr);
371 		pr_err("\n");
372 	}
373 
374 	if (is_vmalloc_addr(addr)) {
375 		struct vm_struct *va = find_vm_area(addr);
376 
377 		if (va) {
378 			pr_err("The buggy address belongs to the virtual mapping at\n"
379 			       " [%px, %px) created by:\n"
380 			       " %pS\n",
381 			       va->addr, va->addr + va->size, va->caller);
382 			pr_err("\n");
383 
384 			page = vmalloc_to_page(addr);
385 		}
386 	}
387 
388 	if (page) {
389 		pr_err("The buggy address belongs to the physical page:\n");
390 		dump_page(page, "kasan: bad access detected");
391 		pr_err("\n");
392 	}
393 }
394 
395 static bool meta_row_is_guilty(const void *row, const void *addr)
396 {
397 	return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW);
398 }
399 
400 static int meta_pointer_offset(const void *row, const void *addr)
401 {
402 	/*
403 	 * Memory state around the buggy address:
404 	 *  ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe
405 	 *  ...
406 	 *
407 	 * The length of ">ff00ff00ff00ff00: " is
408 	 *    3 + (BITS_PER_LONG / 8) * 2 chars.
409 	 * The length of each granule metadata is 2 bytes
410 	 *    plus 1 byte for space.
411 	 */
412 	return 3 + (BITS_PER_LONG / 8) * 2 +
413 		(addr - row) / KASAN_GRANULE_SIZE * 3 + 1;
414 }
415 
416 static void print_memory_metadata(const void *addr)
417 {
418 	int i;
419 	void *row;
420 
421 	row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW)
422 			- META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW;
423 
424 	pr_err("Memory state around the buggy address:\n");
425 
426 	for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
427 		char buffer[4 + (BITS_PER_LONG / 8) * 2];
428 		char metadata[META_BYTES_PER_ROW];
429 
430 		snprintf(buffer, sizeof(buffer),
431 				(i == 0) ? ">%px: " : " %px: ", row);
432 
433 		/*
434 		 * We should not pass a shadow pointer to generic
435 		 * function, because generic functions may try to
436 		 * access kasan mapping for the passed address.
437 		 */
438 		kasan_metadata_fetch_row(&metadata[0], row);
439 
440 		print_hex_dump(KERN_ERR, buffer,
441 			DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
442 			metadata, META_BYTES_PER_ROW, 0);
443 
444 		if (meta_row_is_guilty(row, addr))
445 			pr_err("%*c\n", meta_pointer_offset(row, addr), '^');
446 
447 		row += META_MEM_BYTES_PER_ROW;
448 	}
449 }
450 
451 static void print_report(struct kasan_report_info *info)
452 {
453 	void *addr = kasan_reset_tag(info->access_addr);
454 	u8 tag = get_tag(info->access_addr);
455 
456 	print_error_description(info);
457 	if (addr_has_metadata(addr))
458 		kasan_print_tags(tag, info->first_bad_addr);
459 	pr_err("\n");
460 
461 	if (addr_has_metadata(addr)) {
462 		print_address_description(addr, tag, info);
463 		print_memory_metadata(info->first_bad_addr);
464 	} else {
465 		dump_stack_lvl(KERN_ERR);
466 	}
467 }
468 
469 static void complete_report_info(struct kasan_report_info *info)
470 {
471 	void *addr = kasan_reset_tag(info->access_addr);
472 	struct slab *slab;
473 
474 	if (info->type == KASAN_REPORT_ACCESS)
475 		info->first_bad_addr = kasan_find_first_bad_addr(
476 					info->access_addr, info->access_size);
477 	else
478 		info->first_bad_addr = addr;
479 
480 	slab = kasan_addr_to_slab(addr);
481 	if (slab) {
482 		info->cache = slab->slab_cache;
483 		info->object = nearest_obj(info->cache, slab, addr);
484 
485 		/* Try to determine allocation size based on the metadata. */
486 		info->alloc_size = kasan_get_alloc_size(info->object, info->cache);
487 		/* Fallback to the object size if failed. */
488 		if (!info->alloc_size)
489 			info->alloc_size = info->cache->object_size;
490 	} else
491 		info->cache = info->object = NULL;
492 
493 	switch (info->type) {
494 	case KASAN_REPORT_INVALID_FREE:
495 		info->bug_type = "invalid-free";
496 		break;
497 	case KASAN_REPORT_DOUBLE_FREE:
498 		info->bug_type = "double-free";
499 		break;
500 	default:
501 		/* bug_type filled in by kasan_complete_mode_report_info. */
502 		break;
503 	}
504 
505 	/* Fill in mode-specific report info fields. */
506 	kasan_complete_mode_report_info(info);
507 }
508 
509 void kasan_report_invalid_free(void *ptr, unsigned long ip, enum kasan_report_type type)
510 {
511 	unsigned long flags;
512 	struct kasan_report_info info;
513 
514 	/*
515 	 * Do not check report_suppressed_sw(), as an invalid-free cannot be
516 	 * caused by accessing poisoned memory and thus should not be suppressed
517 	 * by kasan_disable/enable_current() critical sections.
518 	 *
519 	 * Note that for Hardware Tag-Based KASAN, kasan_report_invalid_free()
520 	 * is triggered by explicit tag checks and not by the ones performed by
521 	 * the CPU. Thus, reporting invalid-free is not suppressed as well.
522 	 */
523 	if (unlikely(!report_enabled()))
524 		return;
525 
526 	start_report(&flags, true);
527 
528 	memset(&info, 0, sizeof(info));
529 	info.type = type;
530 	info.access_addr = ptr;
531 	info.access_size = 0;
532 	info.is_write = false;
533 	info.ip = ip;
534 
535 	complete_report_info(&info);
536 
537 	print_report(&info);
538 
539 	end_report(&flags, ptr);
540 }
541 
542 /*
543  * kasan_report() is the only reporting function that uses
544  * user_access_save/restore(): kasan_report_invalid_free() cannot be called
545  * from a UACCESS region, and kasan_report_async() is not used on x86.
546  */
547 bool kasan_report(unsigned long addr, size_t size, bool is_write,
548 			unsigned long ip)
549 {
550 	bool ret = true;
551 	void *ptr = (void *)addr;
552 	unsigned long ua_flags = user_access_save();
553 	unsigned long irq_flags;
554 	struct kasan_report_info info;
555 
556 	if (unlikely(report_suppressed_sw()) || unlikely(!report_enabled())) {
557 		ret = false;
558 		goto out;
559 	}
560 
561 	start_report(&irq_flags, true);
562 
563 	memset(&info, 0, sizeof(info));
564 	info.type = KASAN_REPORT_ACCESS;
565 	info.access_addr = ptr;
566 	info.access_size = size;
567 	info.is_write = is_write;
568 	info.ip = ip;
569 
570 	complete_report_info(&info);
571 
572 	print_report(&info);
573 
574 	end_report(&irq_flags, ptr);
575 
576 out:
577 	user_access_restore(ua_flags);
578 
579 	return ret;
580 }
581 
582 #ifdef CONFIG_KASAN_HW_TAGS
583 void kasan_report_async(void)
584 {
585 	unsigned long flags;
586 
587 	/*
588 	 * Do not check report_suppressed_sw(), as
589 	 * kasan_disable/enable_current() critical sections do not affect
590 	 * Hardware Tag-Based KASAN.
591 	 */
592 	if (unlikely(!report_enabled()))
593 		return;
594 
595 	start_report(&flags, false);
596 	pr_err("BUG: KASAN: invalid-access\n");
597 	pr_err("Asynchronous fault: no details available\n");
598 	pr_err("\n");
599 	dump_stack_lvl(KERN_ERR);
600 	end_report(&flags, NULL);
601 }
602 #endif /* CONFIG_KASAN_HW_TAGS */
603 
604 #ifdef CONFIG_KASAN_INLINE
605 /*
606  * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
607  * canonical half of the address space) cause out-of-bounds shadow memory reads
608  * before the actual access. For addresses in the low canonical half of the
609  * address space, as well as most non-canonical addresses, that out-of-bounds
610  * shadow memory access lands in the non-canonical part of the address space.
611  * Help the user figure out what the original bogus pointer was.
612  */
613 void kasan_non_canonical_hook(unsigned long addr)
614 {
615 	unsigned long orig_addr;
616 	const char *bug_type;
617 
618 	if (addr < KASAN_SHADOW_OFFSET)
619 		return;
620 
621 	orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
622 	/*
623 	 * For faults near the shadow address for NULL, we can be fairly certain
624 	 * that this is a KASAN shadow memory access.
625 	 * For faults that correspond to shadow for low canonical addresses, we
626 	 * can still be pretty sure - that shadow region is a fairly narrow
627 	 * chunk of the non-canonical address space.
628 	 * But faults that look like shadow for non-canonical addresses are a
629 	 * really large chunk of the address space. In that case, we still
630 	 * print the decoded address, but make it clear that this is not
631 	 * necessarily what's actually going on.
632 	 */
633 	if (orig_addr < PAGE_SIZE)
634 		bug_type = "null-ptr-deref";
635 	else if (orig_addr < TASK_SIZE)
636 		bug_type = "probably user-memory-access";
637 	else
638 		bug_type = "maybe wild-memory-access";
639 	pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
640 		 orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1);
641 }
642 #endif
643