1 // SPDX-License-Identifier: GPL-2.0
3 * This file contains common KASAN error reporting code.
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
8 * Some code borrowed from https://github.com/xairy/kasan-prototype by
9 * Andrey Konovalov <andreyknvl@gmail.com>
12 #include <linux/bitops.h>
13 #include <linux/ftrace.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
17 #include <linux/printk.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/stackdepot.h>
21 #include <linux/stacktrace.h>
22 #include <linux/string.h>
23 #include <linux/types.h>
24 #include <linux/kasan.h>
25 #include <linux/module.h>
26 #include <linux/sched/task_stack.h>
27 #include <linux/uaccess.h>
29 #include <asm/sections.h>
31 #include <kunit/test.h>
36 /* Metadata layout customization. */
37 #define META_BYTES_PER_BLOCK 1
38 #define META_BLOCKS_PER_ROW 16
39 #define META_BYTES_PER_ROW (META_BLOCKS_PER_ROW * META_BYTES_PER_BLOCK)
40 #define META_ROWS_AROUND_ADDR 2
42 static unsigned long kasan_flags;
44 #define KASAN_BIT_REPORTED 0
45 #define KASAN_BIT_MULTI_SHOT 1
47 bool kasan_save_enable_multi_shot(void)
49 return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
51 EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot);
53 void kasan_restore_multi_shot(bool enabled)
56 clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
58 EXPORT_SYMBOL_GPL(kasan_restore_multi_shot);
60 static int __init kasan_set_multi_shot(char *str)
62 set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags);
65 __setup("kasan_multi_shot", kasan_set_multi_shot);
67 static void print_error_description(struct kasan_access_info *info)
69 pr_err("BUG: KASAN: %s in %pS\n",
70 get_bug_type(info), (void *)info->ip);
71 pr_err("%s of size %zu at addr %px by task %s/%d\n",
72 info->is_write ? "Write" : "Read", info->access_size,
73 info->access_addr, current->comm, task_pid_nr(current));
76 static DEFINE_SPINLOCK(report_lock);
78 static void start_report(unsigned long *flags)
81 * Make sure we don't end up in loop.
83 kasan_disable_current();
84 spin_lock_irqsave(&report_lock, *flags);
85 pr_err("==================================================================\n");
88 static void end_report(unsigned long *flags)
90 pr_err("==================================================================\n");
91 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
92 spin_unlock_irqrestore(&report_lock, *flags);
93 if (panic_on_warn && !test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) {
95 * This thread may hit another WARN() in the panic path.
96 * Resetting this prevents additional WARN() from panicking the
97 * system on this thread. Other threads are blocked by the
98 * panic_mutex in panic().
101 panic("panic_on_warn set ...\n");
103 kasan_enable_current();
106 static void print_stack(depot_stack_handle_t stack)
108 unsigned long *entries;
109 unsigned int nr_entries;
111 nr_entries = stack_depot_fetch(stack, &entries);
112 stack_trace_print(entries, nr_entries, 0);
115 static void print_track(struct kasan_track *track, const char *prefix)
117 pr_err("%s by task %u:\n", prefix, track->pid);
119 print_stack(track->stack);
121 pr_err("(stack is not available)\n");
125 struct page *kasan_addr_to_page(const void *addr)
127 if ((addr >= (void *)PAGE_OFFSET) &&
128 (addr < high_memory))
129 return virt_to_head_page(addr);
133 static void describe_object_addr(struct kmem_cache *cache, void *object,
136 unsigned long access_addr = (unsigned long)addr;
137 unsigned long object_addr = (unsigned long)object;
138 const char *rel_type;
141 pr_err("The buggy address belongs to the object at %px\n"
142 " which belongs to the cache %s of size %d\n",
143 object, cache->name, cache->object_size);
148 if (access_addr < object_addr) {
149 rel_type = "to the left";
150 rel_bytes = object_addr - access_addr;
151 } else if (access_addr >= object_addr + cache->object_size) {
152 rel_type = "to the right";
153 rel_bytes = access_addr - (object_addr + cache->object_size);
156 rel_bytes = access_addr - object_addr;
159 pr_err("The buggy address is located %d bytes %s of\n"
160 " %d-byte region [%px, %px)\n",
161 rel_bytes, rel_type, cache->object_size, (void *)object_addr,
162 (void *)(object_addr + cache->object_size));
165 static void describe_object(struct kmem_cache *cache, void *object,
166 const void *addr, u8 tag)
168 struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object);
170 if (cache->flags & SLAB_KASAN) {
171 struct kasan_track *free_track;
173 print_track(&alloc_info->alloc_track, "Allocated");
175 free_track = kasan_get_free_track(cache, object, tag);
177 print_track(free_track, "Freed");
181 #ifdef CONFIG_KASAN_GENERIC
182 if (alloc_info->aux_stack[0]) {
183 pr_err("Last potentially related work creation:\n");
184 print_stack(alloc_info->aux_stack[0]);
187 if (alloc_info->aux_stack[1]) {
188 pr_err("Second to last potentially related work creation:\n");
189 print_stack(alloc_info->aux_stack[1]);
195 describe_object_addr(cache, object, addr);
198 static inline bool kernel_or_module_addr(const void *addr)
200 if (addr >= (void *)_stext && addr < (void *)_end)
202 if (is_module_address((unsigned long)addr))
207 static inline bool init_task_stack_addr(const void *addr)
209 return addr >= (void *)&init_thread_union.stack &&
210 (addr <= (void *)&init_thread_union.stack +
211 sizeof(init_thread_union.stack));
214 static void print_address_description(void *addr, u8 tag)
216 struct page *page = kasan_addr_to_page(addr);
221 if (page && PageSlab(page)) {
222 struct kmem_cache *cache = page->slab_cache;
223 void *object = nearest_obj(cache, page, addr);
225 describe_object(cache, object, addr, tag);
228 if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) {
229 pr_err("The buggy address belongs to the variable:\n");
230 pr_err(" %pS\n", addr);
234 pr_err("The buggy address belongs to the page:\n");
235 dump_page(page, "kasan: bad access detected");
238 print_address_stack_frame(addr);
241 static bool row_is_guilty(const void *row, const void *guilty)
243 return (row <= guilty) && (guilty < row + META_BYTES_PER_ROW);
246 static int shadow_pointer_offset(const void *row, const void *shadow)
248 /* The length of ">ff00ff00ff00ff00: " is
249 * 3 + (BITS_PER_LONG/8)*2 chars.
251 return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 +
252 (shadow - row) / META_BYTES_PER_BLOCK + 1;
255 static void print_memory_metadata(const void *addr)
258 const void *shadow = kasan_mem_to_shadow(addr);
259 const void *shadow_row;
261 shadow_row = (void *)round_down((unsigned long)shadow,
263 - META_ROWS_AROUND_ADDR * META_BYTES_PER_ROW;
265 pr_err("Memory state around the buggy address:\n");
267 for (i = -META_ROWS_AROUND_ADDR; i <= META_ROWS_AROUND_ADDR; i++) {
268 const void *kaddr = kasan_shadow_to_mem(shadow_row);
269 char buffer[4 + (BITS_PER_LONG/8)*2];
270 char shadow_buf[META_BYTES_PER_ROW];
272 snprintf(buffer, sizeof(buffer),
273 (i == 0) ? ">%px: " : " %px: ", kaddr);
275 * We should not pass a shadow pointer to generic
276 * function, because generic functions may try to
277 * access kasan mapping for the passed address.
279 memcpy(shadow_buf, shadow_row, META_BYTES_PER_ROW);
280 print_hex_dump(KERN_ERR, buffer,
281 DUMP_PREFIX_NONE, META_BYTES_PER_ROW, 1,
282 shadow_buf, META_BYTES_PER_ROW, 0);
284 if (row_is_guilty(shadow_row, shadow))
286 shadow_pointer_offset(shadow_row, shadow),
289 shadow_row += META_BYTES_PER_ROW;
293 static bool report_enabled(void)
295 #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS)
296 if (current->kasan_depth)
299 if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags))
301 return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags);
304 #if IS_ENABLED(CONFIG_KUNIT)
305 static void kasan_update_kunit_status(struct kunit *cur_test)
307 struct kunit_resource *resource;
308 struct kunit_kasan_expectation *kasan_data;
310 resource = kunit_find_named_resource(cur_test, "kasan_data");
313 kunit_set_failure(cur_test);
317 kasan_data = (struct kunit_kasan_expectation *)resource->data;
318 kasan_data->report_found = true;
319 kunit_put_resource(resource);
321 #endif /* IS_ENABLED(CONFIG_KUNIT) */
323 void kasan_report_invalid_free(void *object, unsigned long ip)
326 u8 tag = get_tag(object);
328 object = reset_tag(object);
330 #if IS_ENABLED(CONFIG_KUNIT)
331 if (current->kunit_test)
332 kasan_update_kunit_status(current->kunit_test);
333 #endif /* IS_ENABLED(CONFIG_KUNIT) */
335 start_report(&flags);
336 pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip);
337 print_tags(tag, object);
339 print_address_description(object, tag);
341 print_memory_metadata(object);
345 static void __kasan_report(unsigned long addr, size_t size, bool is_write,
348 struct kasan_access_info info;
353 #if IS_ENABLED(CONFIG_KUNIT)
354 if (current->kunit_test)
355 kasan_update_kunit_status(current->kunit_test);
356 #endif /* IS_ENABLED(CONFIG_KUNIT) */
358 disable_trace_on_warning();
360 tagged_addr = (void *)addr;
361 untagged_addr = reset_tag(tagged_addr);
363 info.access_addr = tagged_addr;
364 if (addr_has_metadata(untagged_addr))
365 info.first_bad_addr = find_first_bad_addr(tagged_addr, size);
367 info.first_bad_addr = untagged_addr;
368 info.access_size = size;
369 info.is_write = is_write;
372 start_report(&flags);
374 print_error_description(&info);
375 if (addr_has_metadata(untagged_addr))
376 print_tags(get_tag(tagged_addr), info.first_bad_addr);
379 if (addr_has_metadata(untagged_addr)) {
380 print_address_description(untagged_addr, get_tag(tagged_addr));
382 print_memory_metadata(info.first_bad_addr);
390 bool kasan_report(unsigned long addr, size_t size, bool is_write,
393 unsigned long flags = user_access_save();
396 if (likely(report_enabled())) {
397 __kasan_report(addr, size, is_write, ip);
401 user_access_restore(flags);
406 #ifdef CONFIG_KASAN_INLINE
408 * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high
409 * canonical half of the address space) cause out-of-bounds shadow memory reads
410 * before the actual access. For addresses in the low canonical half of the
411 * address space, as well as most non-canonical addresses, that out-of-bounds
412 * shadow memory access lands in the non-canonical part of the address space.
413 * Help the user figure out what the original bogus pointer was.
415 void kasan_non_canonical_hook(unsigned long addr)
417 unsigned long orig_addr;
418 const char *bug_type;
420 if (addr < KASAN_SHADOW_OFFSET)
423 orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT;
425 * For faults near the shadow address for NULL, we can be fairly certain
426 * that this is a KASAN shadow memory access.
427 * For faults that correspond to shadow for low canonical addresses, we
428 * can still be pretty sure - that shadow region is a fairly narrow
429 * chunk of the non-canonical address space.
430 * But faults that look like shadow for non-canonical addresses are a
431 * really large chunk of the address space. In that case, we still
432 * print the decoded address, but make it clear that this is not
433 * necessarily what's actually going on.
435 if (orig_addr < PAGE_SIZE)
436 bug_type = "null-ptr-deref";
437 else if (orig_addr < TASK_SIZE)
438 bug_type = "probably user-memory-access";
440 bug_type = "maybe wild-memory-access";
441 pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type,
442 orig_addr, orig_addr + KASAN_GRANULE_SIZE - 1);