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