Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * This file contains common KASAN error reporting code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Some code borrowed from https://github.com/xairy/kasan-prototype by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *        Andrey Konovalov <andreyknvl@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/stackdepot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/stacktrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/kasan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <trace/events/error_report.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <kunit/test.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include "kasan.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include "../slab.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static unsigned long kasan_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define KASAN_BIT_REPORTED	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define KASAN_BIT_MULTI_SHOT	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) bool kasan_save_enable_multi_shot(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) void kasan_restore_multi_shot(bool enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	if (!enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static int __init kasan_set_multi_shot(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) __setup("kasan_multi_shot", kasan_set_multi_shot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static void print_error_description(struct kasan_access_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	pr_err("BUG: KASAN: %s in %pS\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		kasan_get_bug_type(info), (void *)info->ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (info->access_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		pr_err("%s of size %zu at addr %px by task %s/%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			info->is_write ? "Write" : "Read", info->access_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			info->access_addr, current->comm, task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		pr_err("%s at addr %px by task %s/%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			info->is_write ? "Write" : "Read",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 			info->access_addr, current->comm, task_pid_nr(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) static DEFINE_SPINLOCK(report_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static void start_report(unsigned long *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	 * Make sure we don't end up in loop.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	kasan_disable_current();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	spin_lock_irqsave(&report_lock, *flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	pr_err("==================================================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static void end_report(unsigned long *flags, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	if (!kasan_async_mode_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		trace_error_report_end(ERROR_DETECTOR_KASAN, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	pr_err("==================================================================\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	spin_unlock_irqrestore(&report_lock, *flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (panic_on_warn && !test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		 * This thread may hit another WARN() in the panic path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		 * Resetting this prevents additional WARN() from panicking the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		 * system on this thread.  Other threads are blocked by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		 * panic_mutex in panic().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		panic_on_warn = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		panic("panic_on_warn set ...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #ifdef CONFIG_KASAN_HW_TAGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (kasan_flag_panic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		panic("kasan.fault=panic set ...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	kasan_enable_current();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static void print_stack(depot_stack_handle_t stack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	unsigned long *entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	unsigned int nr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	nr_entries = stack_depot_fetch(stack, &entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	stack_trace_print(entries, nr_entries, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static void print_track(struct kasan_track *track, const char *prefix)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	pr_err("%s by task %u:\n", prefix, track->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (track->stack) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		print_stack(track->stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		pr_err("(stack is not available)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct page *kasan_addr_to_page(const void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if ((addr >= (void *)PAGE_OFFSET) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			(addr < high_memory))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return virt_to_head_page(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static void describe_object_addr(struct kmem_cache *cache, void *object,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 				const void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	unsigned long access_addr = (unsigned long)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	unsigned long object_addr = (unsigned long)object;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	const char *rel_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int rel_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	pr_err("The buggy address belongs to the object at %px\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	       " which belongs to the cache %s of size %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		object, cache->name, cache->object_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (!addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (access_addr < object_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		rel_type = "to the left";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		rel_bytes = object_addr - access_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	} else if (access_addr >= object_addr + cache->object_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		rel_type = "to the right";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		rel_bytes = access_addr - (object_addr + cache->object_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		rel_type = "inside";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		rel_bytes = access_addr - object_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	pr_err("The buggy address is located %d bytes %s of\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	       " %d-byte region [%px, %px)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		rel_bytes, rel_type, cache->object_size, (void *)object_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		(void *)(object_addr + cache->object_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static void describe_object_stacks(struct kmem_cache *cache, void *object,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 					const void *addr, u8 tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct kasan_alloc_meta *alloc_meta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	struct kasan_track *free_track;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	alloc_meta = kasan_get_alloc_meta(cache, object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	if (alloc_meta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		print_track(&alloc_meta->alloc_track, "Allocated");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	free_track = kasan_get_free_track(cache, object, tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (free_track) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		print_track(free_track, "Freed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #ifdef CONFIG_KASAN_GENERIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (!alloc_meta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (alloc_meta->aux_stack[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		pr_err("Last potentially related work creation:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		print_stack(alloc_meta->aux_stack[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (alloc_meta->aux_stack[1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		pr_err("Second to last potentially related work creation:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		print_stack(alloc_meta->aux_stack[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static void describe_object(struct kmem_cache *cache, void *object,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 				const void *addr, u8 tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (kasan_stack_collection_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		describe_object_stacks(cache, object, addr, tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	describe_object_addr(cache, object, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static inline bool kernel_or_module_addr(const void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (addr >= (void *)_stext && addr < (void *)_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (is_module_address((unsigned long)addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static inline bool init_task_stack_addr(const void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	return addr >= (void *)&init_thread_union.stack &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		(addr <= (void *)&init_thread_union.stack +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			sizeof(init_thread_union.stack));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static void print_address_description(void *addr, u8 tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	struct page *page = kasan_addr_to_page(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	dump_stack_lvl(KERN_ERR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (page && PageSlab(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		struct kmem_cache *cache = page->slab_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		void *object = nearest_obj(cache, page,	addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		describe_object(cache, object, addr, tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		pr_err("The buggy address belongs to the variable:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		pr_err(" %pS\n", addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	if (page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		pr_err("The buggy address belongs to the page:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		dump_page(page, "kasan: bad access detected");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	kasan_print_address_stack_frame(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static bool meta_row_is_guilty(const void *row, const void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	return (row <= addr) && (addr < row + META_MEM_BYTES_PER_ROW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int meta_pointer_offset(const void *row, const void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	 * Memory state around the buggy address:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	 *  ff00ff00ff00ff00: 00 00 00 05 fe fe fe fe fe fe fe fe fe fe fe fe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	 *  ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	 * The length of ">ff00ff00ff00ff00: " is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	 *    3 + (BITS_PER_LONG / 8) * 2 chars.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	 * The length of each granule metadata is 2 bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	 *    plus 1 byte for space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	return 3 + (BITS_PER_LONG / 8) * 2 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		(addr - row) / KASAN_GRANULE_SIZE * 3 + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static void print_memory_metadata(const void *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	void *row;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	row = (void *)round_down((unsigned long)addr, META_MEM_BYTES_PER_ROW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			- META_ROWS_AROUND_ADDR * META_MEM_BYTES_PER_ROW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	pr_err("Memory state around the buggy address:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		char buffer[4 + (BITS_PER_LONG / 8) * 2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		char metadata[META_BYTES_PER_ROW];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		snprintf(buffer, sizeof(buffer),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 				(i == 0) ? ">%px: " : " %px: ", row);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		 * We should not pass a shadow pointer to generic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		 * function, because generic functions may try to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		 * access kasan mapping for the passed address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		kasan_metadata_fetch_row(&metadata[0], row);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		print_hex_dump(KERN_ERR, buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			metadata, META_BYTES_PER_ROW, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		if (meta_row_is_guilty(row, addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			pr_err("%*c\n", meta_pointer_offset(row, addr), '^');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		row += META_MEM_BYTES_PER_ROW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static bool report_enabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (current->kasan_depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) #if IS_ENABLED(CONFIG_KUNIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static void kasan_update_kunit_status(struct kunit *cur_test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct kunit_resource *resource;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	struct kunit_kasan_expectation *kasan_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	resource = kunit_find_named_resource(cur_test, "kasan_data");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (!resource) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		kunit_set_failure(cur_test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	kasan_data = (struct kunit_kasan_expectation *)resource->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	WRITE_ONCE(kasan_data->report_found, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	kunit_put_resource(resource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) #endif /* IS_ENABLED(CONFIG_KUNIT) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) void kasan_report_invalid_free(void *object, unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	u8 tag = get_tag(object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	object = kasan_reset_tag(object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) #if IS_ENABLED(CONFIG_KUNIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	if (current->kunit_test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		kasan_update_kunit_status(current->kunit_test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) #endif /* IS_ENABLED(CONFIG_KUNIT) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	start_report(&flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	kasan_print_tags(tag, object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	print_address_description(object, tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	print_memory_metadata(object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	end_report(&flags, (unsigned long)object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) #ifdef CONFIG_KASAN_HW_TAGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) void kasan_report_async(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) #if IS_ENABLED(CONFIG_KUNIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (current->kunit_test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		kasan_update_kunit_status(current->kunit_test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) #endif /* IS_ENABLED(CONFIG_KUNIT) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	start_report(&flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	pr_err("BUG: KASAN: invalid-access\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	pr_err("Asynchronous mode enabled: no access details available\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	dump_stack_lvl(KERN_ERR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	end_report(&flags, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) #endif /* CONFIG_KASAN_HW_TAGS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) static void __kasan_report(unsigned long addr, size_t size, bool is_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 				unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	struct kasan_access_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	void *tagged_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	void *untagged_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) #if IS_ENABLED(CONFIG_KUNIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (current->kunit_test)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		kasan_update_kunit_status(current->kunit_test);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) #endif /* IS_ENABLED(CONFIG_KUNIT) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	disable_trace_on_warning();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	tagged_addr = (void *)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	untagged_addr = kasan_reset_tag(tagged_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	info.access_addr = tagged_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (addr_has_metadata(untagged_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		info.first_bad_addr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 			kasan_find_first_bad_addr(tagged_addr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		info.first_bad_addr = untagged_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	info.access_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	info.is_write = is_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	info.ip = ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	start_report(&flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	print_error_description(&info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	if (addr_has_metadata(untagged_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		kasan_print_tags(get_tag(tagged_addr), info.first_bad_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	if (addr_has_metadata(untagged_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		print_address_description(untagged_addr, get_tag(tagged_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		pr_err("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		print_memory_metadata(info.first_bad_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		dump_stack_lvl(KERN_ERR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	end_report(&flags, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) bool kasan_report(unsigned long addr, size_t size, bool is_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			unsigned long ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	unsigned long flags = user_access_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	bool ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if (likely(report_enabled())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		__kasan_report(addr, size, is_write, ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	user_access_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) #ifdef CONFIG_KASAN_INLINE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)  * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)  * canonical half of the address space) cause out-of-bounds shadow memory reads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)  * before the actual access. For addresses in the low canonical half of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)  * address space, as well as most non-canonical addresses, that out-of-bounds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)  * shadow memory access lands in the non-canonical part of the address space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)  * Help the user figure out what the original bogus pointer was.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) void kasan_non_canonical_hook(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	unsigned long orig_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	const char *bug_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (addr < KASAN_SHADOW_OFFSET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	 * For faults near the shadow address for NULL, we can be fairly certain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	 * that this is a KASAN shadow memory access.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	 * For faults that correspond to shadow for low canonical addresses, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	 * can still be pretty sure - that shadow region is a fairly narrow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	 * chunk of the non-canonical address space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	 * But faults that look like shadow for non-canonical addresses are a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	 * really large chunk of the address space. In that case, we still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	 * print the decoded address, but make it clear that this is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	 * necessarily what's actually going on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	if (orig_addr < PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		bug_type = "null-ptr-deref";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	else if (orig_addr < TASK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		bug_type = "probably user-memory-access";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		bug_type = "maybe wild-memory-access";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		 orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) #endif