1e886bf9dSAndrey Konovalov // SPDX-License-Identifier: GPL-2.0 20b24beccSAndrey Ryabinin /* 311cd3cd6SAndrey Konovalov * This file contains common generic and tag-based KASAN error reporting code. 40b24beccSAndrey Ryabinin * 50b24beccSAndrey Ryabinin * Copyright (c) 2014 Samsung Electronics Co., Ltd. 62baf9e89SAndrey Ryabinin * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com> 70b24beccSAndrey Ryabinin * 85d0926efSAndrey Konovalov * Some code borrowed from https://github.com/xairy/kasan-prototype by 95f21f3a8SAndrey Konovalov * Andrey Konovalov <andreyknvl@gmail.com> 100b24beccSAndrey Ryabinin * 110b24beccSAndrey Ryabinin * This program is free software; you can redistribute it and/or modify 120b24beccSAndrey Ryabinin * it under the terms of the GNU General Public License version 2 as 130b24beccSAndrey Ryabinin * published by the Free Software Foundation. 140b24beccSAndrey Ryabinin * 150b24beccSAndrey Ryabinin */ 160b24beccSAndrey Ryabinin 17b0845ce5SMark Rutland #include <linux/bitops.h> 184f40c6e5SPeter Zijlstra #include <linux/ftrace.h> 19b0845ce5SMark Rutland #include <linux/init.h> 200b24beccSAndrey Ryabinin #include <linux/kernel.h> 210b24beccSAndrey Ryabinin #include <linux/mm.h> 220b24beccSAndrey Ryabinin #include <linux/printk.h> 230b24beccSAndrey Ryabinin #include <linux/sched.h> 240b24beccSAndrey Ryabinin #include <linux/slab.h> 25cd11016eSAlexander Potapenko #include <linux/stackdepot.h> 260b24beccSAndrey Ryabinin #include <linux/stacktrace.h> 270b24beccSAndrey Ryabinin #include <linux/string.h> 280b24beccSAndrey Ryabinin #include <linux/types.h> 290b24beccSAndrey Ryabinin #include <linux/kasan.h> 30527f215bSAneesh Kumar K.V #include <linux/module.h> 31e8969219SMarco Elver #include <linux/sched/task_stack.h> 324fba3758SAndrey Konovalov #include <linux/uaccess.h> 330b24beccSAndrey Ryabinin 34bebf56a1SAndrey Ryabinin #include <asm/sections.h> 35bebf56a1SAndrey Ryabinin 360b24beccSAndrey Ryabinin #include "kasan.h" 370316bec2SAndrey Ryabinin #include "../slab.h" 380b24beccSAndrey Ryabinin 390b24beccSAndrey Ryabinin /* Shadow layout customization. */ 400b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_BLOCK 1 410b24beccSAndrey Ryabinin #define SHADOW_BLOCKS_PER_ROW 16 420b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK) 430b24beccSAndrey Ryabinin #define SHADOW_ROWS_AROUND_ADDR 2 440b24beccSAndrey Ryabinin 4511cd3cd6SAndrey Konovalov static unsigned long kasan_flags; 4611cd3cd6SAndrey Konovalov 4711cd3cd6SAndrey Konovalov #define KASAN_BIT_REPORTED 0 4811cd3cd6SAndrey Konovalov #define KASAN_BIT_MULTI_SHOT 1 4911cd3cd6SAndrey Konovalov 5011cd3cd6SAndrey Konovalov bool kasan_save_enable_multi_shot(void) 510b24beccSAndrey Ryabinin { 5211cd3cd6SAndrey Konovalov return test_and_set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); 530b24beccSAndrey Ryabinin } 5411cd3cd6SAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_save_enable_multi_shot); 550b24beccSAndrey Ryabinin 5611cd3cd6SAndrey Konovalov void kasan_restore_multi_shot(bool enabled) 575e82cd12SAndrey Konovalov { 5811cd3cd6SAndrey Konovalov if (!enabled) 5911cd3cd6SAndrey Konovalov clear_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); 605e82cd12SAndrey Konovalov } 6111cd3cd6SAndrey Konovalov EXPORT_SYMBOL_GPL(kasan_restore_multi_shot); 625e82cd12SAndrey Konovalov 6311cd3cd6SAndrey Konovalov static int __init kasan_set_multi_shot(char *str) 640b24beccSAndrey Ryabinin { 6511cd3cd6SAndrey Konovalov set_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags); 6611cd3cd6SAndrey Konovalov return 1; 670b24beccSAndrey Ryabinin } 6811cd3cd6SAndrey Konovalov __setup("kasan_multi_shot", kasan_set_multi_shot); 690b24beccSAndrey Ryabinin 70121e8f81SAndrey Konovalov static void print_error_description(struct kasan_access_info *info) 715e82cd12SAndrey Konovalov { 727f0a84c2SAndrey Konovalov pr_err("BUG: KASAN: %s in %pS\n", 73121e8f81SAndrey Konovalov get_bug_type(info), (void *)info->ip); 746424f6bbSTobin C. Harding pr_err("%s of size %zu at addr %px by task %s/%d\n", 757d418f7bSAndrey Konovalov info->is_write ? "Write" : "Read", info->access_size, 767f0a84c2SAndrey Konovalov info->access_addr, current->comm, task_pid_nr(current)); 770b24beccSAndrey Ryabinin } 780b24beccSAndrey Ryabinin 797e088978SAndrey Ryabinin static DEFINE_SPINLOCK(report_lock); 807e088978SAndrey Ryabinin 8111cd3cd6SAndrey Konovalov static void start_report(unsigned long *flags) 827e088978SAndrey Ryabinin { 837e088978SAndrey Ryabinin /* 847e088978SAndrey Ryabinin * Make sure we don't end up in loop. 857e088978SAndrey Ryabinin */ 867e088978SAndrey Ryabinin kasan_disable_current(); 877e088978SAndrey Ryabinin spin_lock_irqsave(&report_lock, *flags); 887e088978SAndrey Ryabinin pr_err("==================================================================\n"); 897e088978SAndrey Ryabinin } 907e088978SAndrey Ryabinin 9111cd3cd6SAndrey Konovalov static void end_report(unsigned long *flags) 927e088978SAndrey Ryabinin { 937e088978SAndrey Ryabinin pr_err("==================================================================\n"); 947e088978SAndrey Ryabinin add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 957e088978SAndrey Ryabinin spin_unlock_irqrestore(&report_lock, *flags); 961d2252faSKees Cook if (panic_on_warn) { 971d2252faSKees Cook /* 981d2252faSKees Cook * This thread may hit another WARN() in the panic path. 991d2252faSKees Cook * Resetting this prevents additional WARN() from panicking the 1001d2252faSKees Cook * system on this thread. Other threads are blocked by the 1011d2252faSKees Cook * panic_mutex in panic(). 1021d2252faSKees Cook */ 1031d2252faSKees Cook panic_on_warn = 0; 1045c5c1f36SDmitry Vyukov panic("panic_on_warn set ...\n"); 1051d2252faSKees Cook } 1067e088978SAndrey Ryabinin kasan_enable_current(); 1077e088978SAndrey Ryabinin } 1087e088978SAndrey Ryabinin 109*26e760c9SWalter Wu static void print_stack(depot_stack_handle_t stack) 110*26e760c9SWalter Wu { 111*26e760c9SWalter Wu unsigned long *entries; 112*26e760c9SWalter Wu unsigned int nr_entries; 113*26e760c9SWalter Wu 114*26e760c9SWalter Wu nr_entries = stack_depot_fetch(stack, &entries); 115*26e760c9SWalter Wu stack_trace_print(entries, nr_entries, 0); 116*26e760c9SWalter Wu } 117*26e760c9SWalter Wu 118b6b72f49SAndrey Konovalov static void print_track(struct kasan_track *track, const char *prefix) 1197ed2f9e6SAlexander Potapenko { 120b6b72f49SAndrey Konovalov pr_err("%s by task %u:\n", prefix, track->pid); 121cd11016eSAlexander Potapenko if (track->stack) { 122*26e760c9SWalter Wu print_stack(track->stack); 123cd11016eSAlexander Potapenko } else { 124cd11016eSAlexander Potapenko pr_err("(stack is not available)\n"); 125cd11016eSAlexander Potapenko } 1267ed2f9e6SAlexander Potapenko } 1277ed2f9e6SAlexander Potapenko 128ae8f06b3SWalter Wu struct page *kasan_addr_to_page(const void *addr) 129db429f16SAndrey Konovalov { 130db429f16SAndrey Konovalov if ((addr >= (void *)PAGE_OFFSET) && 131db429f16SAndrey Konovalov (addr < high_memory)) 132db429f16SAndrey Konovalov return virt_to_head_page(addr); 133db429f16SAndrey Konovalov return NULL; 134db429f16SAndrey Konovalov } 135db429f16SAndrey Konovalov 1360c06f1f8SAndrey Konovalov static void describe_object_addr(struct kmem_cache *cache, void *object, 1370c06f1f8SAndrey Konovalov const void *addr) 1380c06f1f8SAndrey Konovalov { 1390c06f1f8SAndrey Konovalov unsigned long access_addr = (unsigned long)addr; 1400c06f1f8SAndrey Konovalov unsigned long object_addr = (unsigned long)object; 1410c06f1f8SAndrey Konovalov const char *rel_type; 1420c06f1f8SAndrey Konovalov int rel_bytes; 1430c06f1f8SAndrey Konovalov 1446424f6bbSTobin C. Harding pr_err("The buggy address belongs to the object at %px\n" 1450c06f1f8SAndrey Konovalov " which belongs to the cache %s of size %d\n", 1460c06f1f8SAndrey Konovalov object, cache->name, cache->object_size); 1470c06f1f8SAndrey Konovalov 1480c06f1f8SAndrey Konovalov if (!addr) 1490c06f1f8SAndrey Konovalov return; 1500c06f1f8SAndrey Konovalov 1510c06f1f8SAndrey Konovalov if (access_addr < object_addr) { 1520c06f1f8SAndrey Konovalov rel_type = "to the left"; 1530c06f1f8SAndrey Konovalov rel_bytes = object_addr - access_addr; 1540c06f1f8SAndrey Konovalov } else if (access_addr >= object_addr + cache->object_size) { 1550c06f1f8SAndrey Konovalov rel_type = "to the right"; 1560c06f1f8SAndrey Konovalov rel_bytes = access_addr - (object_addr + cache->object_size); 1570c06f1f8SAndrey Konovalov } else { 1580c06f1f8SAndrey Konovalov rel_type = "inside"; 1590c06f1f8SAndrey Konovalov rel_bytes = access_addr - object_addr; 1600c06f1f8SAndrey Konovalov } 1610c06f1f8SAndrey Konovalov 1620c06f1f8SAndrey Konovalov pr_err("The buggy address is located %d bytes %s of\n" 1636424f6bbSTobin C. Harding " %d-byte region [%px, %px)\n", 1640c06f1f8SAndrey Konovalov rel_bytes, rel_type, cache->object_size, (void *)object_addr, 1650c06f1f8SAndrey Konovalov (void *)(object_addr + cache->object_size)); 1660c06f1f8SAndrey Konovalov } 1670c06f1f8SAndrey Konovalov 168ae8f06b3SWalter Wu static struct kasan_track *kasan_get_free_track(struct kmem_cache *cache, 169ae8f06b3SWalter Wu void *object, u8 tag) 170ae8f06b3SWalter Wu { 171ae8f06b3SWalter Wu struct kasan_alloc_meta *alloc_meta; 172ae8f06b3SWalter Wu int i = 0; 173ae8f06b3SWalter Wu 174ae8f06b3SWalter Wu alloc_meta = get_alloc_info(cache, object); 175ae8f06b3SWalter Wu 176ae8f06b3SWalter Wu #ifdef CONFIG_KASAN_SW_TAGS_IDENTIFY 177ae8f06b3SWalter Wu for (i = 0; i < KASAN_NR_FREE_STACKS; i++) { 178ae8f06b3SWalter Wu if (alloc_meta->free_pointer_tag[i] == tag) 179ae8f06b3SWalter Wu break; 180ae8f06b3SWalter Wu } 181ae8f06b3SWalter Wu if (i == KASAN_NR_FREE_STACKS) 182ae8f06b3SWalter Wu i = alloc_meta->free_track_idx; 183ae8f06b3SWalter Wu #endif 184ae8f06b3SWalter Wu 185ae8f06b3SWalter Wu return &alloc_meta->free_track[i]; 186ae8f06b3SWalter Wu } 187ae8f06b3SWalter Wu 1880c06f1f8SAndrey Konovalov static void describe_object(struct kmem_cache *cache, void *object, 189ae8f06b3SWalter Wu const void *addr, u8 tag) 1907ed2f9e6SAlexander Potapenko { 1917ed2f9e6SAlexander Potapenko struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object); 1927ed2f9e6SAlexander Potapenko 1930c06f1f8SAndrey Konovalov if (cache->flags & SLAB_KASAN) { 194ae8f06b3SWalter Wu struct kasan_track *free_track; 195ae8f06b3SWalter Wu 196b6b72f49SAndrey Konovalov print_track(&alloc_info->alloc_track, "Allocated"); 197b1938599SAndrey Konovalov pr_err("\n"); 198ae8f06b3SWalter Wu free_track = kasan_get_free_track(cache, object, tag); 199ae8f06b3SWalter Wu print_track(free_track, "Freed"); 200b1938599SAndrey Konovalov pr_err("\n"); 201*26e760c9SWalter Wu 202*26e760c9SWalter Wu #ifdef CONFIG_KASAN_GENERIC 203*26e760c9SWalter Wu if (alloc_info->aux_stack[0]) { 204*26e760c9SWalter Wu pr_err("Last call_rcu():\n"); 205*26e760c9SWalter Wu print_stack(alloc_info->aux_stack[0]); 206*26e760c9SWalter Wu pr_err("\n"); 207*26e760c9SWalter Wu } 208*26e760c9SWalter Wu if (alloc_info->aux_stack[1]) { 209*26e760c9SWalter Wu pr_err("Second to last call_rcu():\n"); 210*26e760c9SWalter Wu print_stack(alloc_info->aux_stack[1]); 211*26e760c9SWalter Wu pr_err("\n"); 212*26e760c9SWalter Wu } 213*26e760c9SWalter Wu #endif 2147ed2f9e6SAlexander Potapenko } 2157ed2f9e6SAlexander Potapenko 2160c06f1f8SAndrey Konovalov describe_object_addr(cache, object, addr); 2170c06f1f8SAndrey Konovalov } 2180c06f1f8SAndrey Konovalov 21911cd3cd6SAndrey Konovalov static inline bool kernel_or_module_addr(const void *addr) 22011cd3cd6SAndrey Konovalov { 22111cd3cd6SAndrey Konovalov if (addr >= (void *)_stext && addr < (void *)_end) 22211cd3cd6SAndrey Konovalov return true; 22311cd3cd6SAndrey Konovalov if (is_module_address((unsigned long)addr)) 22411cd3cd6SAndrey Konovalov return true; 22511cd3cd6SAndrey Konovalov return false; 22611cd3cd6SAndrey Konovalov } 22711cd3cd6SAndrey Konovalov 22811cd3cd6SAndrey Konovalov static inline bool init_task_stack_addr(const void *addr) 22911cd3cd6SAndrey Konovalov { 23011cd3cd6SAndrey Konovalov return addr >= (void *)&init_thread_union.stack && 23111cd3cd6SAndrey Konovalov (addr <= (void *)&init_thread_union.stack + 23211cd3cd6SAndrey Konovalov sizeof(init_thread_union.stack)); 23311cd3cd6SAndrey Konovalov } 23411cd3cd6SAndrey Konovalov 235e8969219SMarco Elver static bool __must_check tokenize_frame_descr(const char **frame_descr, 236e8969219SMarco Elver char *token, size_t max_tok_len, 237e8969219SMarco Elver unsigned long *value) 238e8969219SMarco Elver { 239e8969219SMarco Elver const char *sep = strchr(*frame_descr, ' '); 240e8969219SMarco Elver 241e8969219SMarco Elver if (sep == NULL) 242e8969219SMarco Elver sep = *frame_descr + strlen(*frame_descr); 243e8969219SMarco Elver 244e8969219SMarco Elver if (token != NULL) { 245e8969219SMarco Elver const size_t tok_len = sep - *frame_descr; 246e8969219SMarco Elver 247e8969219SMarco Elver if (tok_len + 1 > max_tok_len) { 248e8969219SMarco Elver pr_err("KASAN internal error: frame description too long: %s\n", 249e8969219SMarco Elver *frame_descr); 250e8969219SMarco Elver return false; 251e8969219SMarco Elver } 252e8969219SMarco Elver 253e8969219SMarco Elver /* Copy token (+ 1 byte for '\0'). */ 254e8969219SMarco Elver strlcpy(token, *frame_descr, tok_len + 1); 255e8969219SMarco Elver } 256e8969219SMarco Elver 257e8969219SMarco Elver /* Advance frame_descr past separator. */ 258e8969219SMarco Elver *frame_descr = sep + 1; 259e8969219SMarco Elver 260e8969219SMarco Elver if (value != NULL && kstrtoul(token, 10, value)) { 261e8969219SMarco Elver pr_err("KASAN internal error: not a valid number: %s\n", token); 262e8969219SMarco Elver return false; 263e8969219SMarco Elver } 264e8969219SMarco Elver 265e8969219SMarco Elver return true; 266e8969219SMarco Elver } 267e8969219SMarco Elver 268e8969219SMarco Elver static void print_decoded_frame_descr(const char *frame_descr) 269e8969219SMarco Elver { 270e8969219SMarco Elver /* 271e8969219SMarco Elver * We need to parse the following string: 272e8969219SMarco Elver * "n alloc_1 alloc_2 ... alloc_n" 273e8969219SMarco Elver * where alloc_i looks like 274e8969219SMarco Elver * "offset size len name" 275e8969219SMarco Elver * or "offset size len name:line". 276e8969219SMarco Elver */ 277e8969219SMarco Elver 278e8969219SMarco Elver char token[64]; 279e8969219SMarco Elver unsigned long num_objects; 280e8969219SMarco Elver 281e8969219SMarco Elver if (!tokenize_frame_descr(&frame_descr, token, sizeof(token), 282e8969219SMarco Elver &num_objects)) 283e8969219SMarco Elver return; 284e8969219SMarco Elver 285e8969219SMarco Elver pr_err("\n"); 286e8969219SMarco Elver pr_err("this frame has %lu %s:\n", num_objects, 287e8969219SMarco Elver num_objects == 1 ? "object" : "objects"); 288e8969219SMarco Elver 289e8969219SMarco Elver while (num_objects--) { 290e8969219SMarco Elver unsigned long offset; 291e8969219SMarco Elver unsigned long size; 292e8969219SMarco Elver 293e8969219SMarco Elver /* access offset */ 294e8969219SMarco Elver if (!tokenize_frame_descr(&frame_descr, token, sizeof(token), 295e8969219SMarco Elver &offset)) 296e8969219SMarco Elver return; 297e8969219SMarco Elver /* access size */ 298e8969219SMarco Elver if (!tokenize_frame_descr(&frame_descr, token, sizeof(token), 299e8969219SMarco Elver &size)) 300e8969219SMarco Elver return; 301e8969219SMarco Elver /* name length (unused) */ 302e8969219SMarco Elver if (!tokenize_frame_descr(&frame_descr, NULL, 0, NULL)) 303e8969219SMarco Elver return; 304e8969219SMarco Elver /* object name */ 305e8969219SMarco Elver if (!tokenize_frame_descr(&frame_descr, token, sizeof(token), 306e8969219SMarco Elver NULL)) 307e8969219SMarco Elver return; 308e8969219SMarco Elver 309e8969219SMarco Elver /* Strip line number; without filename it's not very helpful. */ 310e8969219SMarco Elver strreplace(token, ':', '\0'); 311e8969219SMarco Elver 312e8969219SMarco Elver /* Finally, print object information. */ 313e8969219SMarco Elver pr_err(" [%lu, %lu) '%s'", offset, offset + size, token); 314e8969219SMarco Elver } 315e8969219SMarco Elver } 316e8969219SMarco Elver 317e8969219SMarco Elver static bool __must_check get_address_stack_frame_info(const void *addr, 318e8969219SMarco Elver unsigned long *offset, 319e8969219SMarco Elver const char **frame_descr, 320e8969219SMarco Elver const void **frame_pc) 321e8969219SMarco Elver { 322e8969219SMarco Elver unsigned long aligned_addr; 323e8969219SMarco Elver unsigned long mem_ptr; 324e8969219SMarco Elver const u8 *shadow_bottom; 325e8969219SMarco Elver const u8 *shadow_ptr; 326e8969219SMarco Elver const unsigned long *frame; 327e8969219SMarco Elver 328e8969219SMarco Elver BUILD_BUG_ON(IS_ENABLED(CONFIG_STACK_GROWSUP)); 329e8969219SMarco Elver 330e8969219SMarco Elver /* 331e8969219SMarco Elver * NOTE: We currently only support printing frame information for 332e8969219SMarco Elver * accesses to the task's own stack. 333e8969219SMarco Elver */ 334e8969219SMarco Elver if (!object_is_on_stack(addr)) 335e8969219SMarco Elver return false; 336e8969219SMarco Elver 337e8969219SMarco Elver aligned_addr = round_down((unsigned long)addr, sizeof(long)); 338e8969219SMarco Elver mem_ptr = round_down(aligned_addr, KASAN_SHADOW_SCALE_SIZE); 339e8969219SMarco Elver shadow_ptr = kasan_mem_to_shadow((void *)aligned_addr); 340e8969219SMarco Elver shadow_bottom = kasan_mem_to_shadow(end_of_stack(current)); 341e8969219SMarco Elver 342e8969219SMarco Elver while (shadow_ptr >= shadow_bottom && *shadow_ptr != KASAN_STACK_LEFT) { 343e8969219SMarco Elver shadow_ptr--; 344e8969219SMarco Elver mem_ptr -= KASAN_SHADOW_SCALE_SIZE; 345e8969219SMarco Elver } 346e8969219SMarco Elver 347e8969219SMarco Elver while (shadow_ptr >= shadow_bottom && *shadow_ptr == KASAN_STACK_LEFT) { 348e8969219SMarco Elver shadow_ptr--; 349e8969219SMarco Elver mem_ptr -= KASAN_SHADOW_SCALE_SIZE; 350e8969219SMarco Elver } 351e8969219SMarco Elver 352e8969219SMarco Elver if (shadow_ptr < shadow_bottom) 353e8969219SMarco Elver return false; 354e8969219SMarco Elver 355e8969219SMarco Elver frame = (const unsigned long *)(mem_ptr + KASAN_SHADOW_SCALE_SIZE); 356e8969219SMarco Elver if (frame[0] != KASAN_CURRENT_STACK_FRAME_MAGIC) { 357e8969219SMarco Elver pr_err("KASAN internal error: frame info validation failed; invalid marker: %lu\n", 358e8969219SMarco Elver frame[0]); 359e8969219SMarco Elver return false; 360e8969219SMarco Elver } 361e8969219SMarco Elver 362e8969219SMarco Elver *offset = (unsigned long)addr - (unsigned long)frame; 363e8969219SMarco Elver *frame_descr = (const char *)frame[1]; 364e8969219SMarco Elver *frame_pc = (void *)frame[2]; 365e8969219SMarco Elver 366e8969219SMarco Elver return true; 367e8969219SMarco Elver } 368e8969219SMarco Elver 369e8969219SMarco Elver static void print_address_stack_frame(const void *addr) 370e8969219SMarco Elver { 371e8969219SMarco Elver unsigned long offset; 372e8969219SMarco Elver const char *frame_descr; 373e8969219SMarco Elver const void *frame_pc; 374e8969219SMarco Elver 375e8969219SMarco Elver if (IS_ENABLED(CONFIG_KASAN_SW_TAGS)) 376e8969219SMarco Elver return; 377e8969219SMarco Elver 378e8969219SMarco Elver if (!get_address_stack_frame_info(addr, &offset, &frame_descr, 379e8969219SMarco Elver &frame_pc)) 380e8969219SMarco Elver return; 381e8969219SMarco Elver 382e8969219SMarco Elver /* 383e8969219SMarco Elver * get_address_stack_frame_info only returns true if the given addr is 384e8969219SMarco Elver * on the current task's stack. 385e8969219SMarco Elver */ 386e8969219SMarco Elver pr_err("\n"); 387e8969219SMarco Elver pr_err("addr %px is located in stack of task %s/%d at offset %lu in frame:\n", 388e8969219SMarco Elver addr, current->comm, task_pid_nr(current), offset); 389e8969219SMarco Elver pr_err(" %pS\n", frame_pc); 390e8969219SMarco Elver 391e8969219SMarco Elver if (!frame_descr) 392e8969219SMarco Elver return; 393e8969219SMarco Elver 394e8969219SMarco Elver print_decoded_frame_descr(frame_descr); 395e8969219SMarco Elver } 396e8969219SMarco Elver 397ae8f06b3SWalter Wu static void print_address_description(void *addr, u8 tag) 3987e088978SAndrey Ryabinin { 399ae8f06b3SWalter Wu struct page *page = kasan_addr_to_page(addr); 400b8c73fc2SAndrey Ryabinin 401db429f16SAndrey Konovalov dump_stack(); 402b1938599SAndrey Konovalov pr_err("\n"); 403db429f16SAndrey Konovalov 404db429f16SAndrey Konovalov if (page && PageSlab(page)) { 405db429f16SAndrey Konovalov struct kmem_cache *cache = page->slab_cache; 4060c06f1f8SAndrey Konovalov void *object = nearest_obj(cache, page, addr); 407db429f16SAndrey Konovalov 408ae8f06b3SWalter Wu describe_object(cache, object, addr, tag); 409b8c73fc2SAndrey Ryabinin } 410b8c73fc2SAndrey Ryabinin 411430a05f9SAndrey Konovalov if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) { 412430a05f9SAndrey Konovalov pr_err("The buggy address belongs to the variable:\n"); 413430a05f9SAndrey Konovalov pr_err(" %pS\n", addr); 414430a05f9SAndrey Konovalov } 415430a05f9SAndrey Konovalov 416430a05f9SAndrey Konovalov if (page) { 417430a05f9SAndrey Konovalov pr_err("The buggy address belongs to the page:\n"); 418430a05f9SAndrey Konovalov dump_page(page, "kasan: bad access detected"); 419bebf56a1SAndrey Ryabinin } 420e8969219SMarco Elver 421e8969219SMarco Elver print_address_stack_frame(addr); 4220b24beccSAndrey Ryabinin } 4230b24beccSAndrey Ryabinin 4240b24beccSAndrey Ryabinin static bool row_is_guilty(const void *row, const void *guilty) 4250b24beccSAndrey Ryabinin { 4260b24beccSAndrey Ryabinin return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW); 4270b24beccSAndrey Ryabinin } 4280b24beccSAndrey Ryabinin 4290b24beccSAndrey Ryabinin static int shadow_pointer_offset(const void *row, const void *shadow) 4300b24beccSAndrey Ryabinin { 4310b24beccSAndrey Ryabinin /* The length of ">ff00ff00ff00ff00: " is 4320b24beccSAndrey Ryabinin * 3 + (BITS_PER_LONG/8)*2 chars. 4330b24beccSAndrey Ryabinin */ 4340b24beccSAndrey Ryabinin return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 + 4350b24beccSAndrey Ryabinin (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1; 4360b24beccSAndrey Ryabinin } 4370b24beccSAndrey Ryabinin 4380b24beccSAndrey Ryabinin static void print_shadow_for_address(const void *addr) 4390b24beccSAndrey Ryabinin { 4400b24beccSAndrey Ryabinin int i; 4410b24beccSAndrey Ryabinin const void *shadow = kasan_mem_to_shadow(addr); 4420b24beccSAndrey Ryabinin const void *shadow_row; 4430b24beccSAndrey Ryabinin 4440b24beccSAndrey Ryabinin shadow_row = (void *)round_down((unsigned long)shadow, 4450b24beccSAndrey Ryabinin SHADOW_BYTES_PER_ROW) 4460b24beccSAndrey Ryabinin - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW; 4470b24beccSAndrey Ryabinin 4480b24beccSAndrey Ryabinin pr_err("Memory state around the buggy address:\n"); 4490b24beccSAndrey Ryabinin 4500b24beccSAndrey Ryabinin for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) { 4510b24beccSAndrey Ryabinin const void *kaddr = kasan_shadow_to_mem(shadow_row); 4520b24beccSAndrey Ryabinin char buffer[4 + (BITS_PER_LONG/8)*2]; 453f2377d4eSAneesh Kumar K.V char shadow_buf[SHADOW_BYTES_PER_ROW]; 4540b24beccSAndrey Ryabinin 4550b24beccSAndrey Ryabinin snprintf(buffer, sizeof(buffer), 4566424f6bbSTobin C. Harding (i == 0) ? ">%px: " : " %px: ", kaddr); 457f2377d4eSAneesh Kumar K.V /* 458f2377d4eSAneesh Kumar K.V * We should not pass a shadow pointer to generic 459f2377d4eSAneesh Kumar K.V * function, because generic functions may try to 460f2377d4eSAneesh Kumar K.V * access kasan mapping for the passed address. 461f2377d4eSAneesh Kumar K.V */ 462f2377d4eSAneesh Kumar K.V memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW); 4630b24beccSAndrey Ryabinin print_hex_dump(KERN_ERR, buffer, 4640b24beccSAndrey Ryabinin DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1, 465f2377d4eSAneesh Kumar K.V shadow_buf, SHADOW_BYTES_PER_ROW, 0); 4660b24beccSAndrey Ryabinin 4670b24beccSAndrey Ryabinin if (row_is_guilty(shadow_row, shadow)) 4680b24beccSAndrey Ryabinin pr_err("%*c\n", 4690b24beccSAndrey Ryabinin shadow_pointer_offset(shadow_row, shadow), 4700b24beccSAndrey Ryabinin '^'); 4710b24beccSAndrey Ryabinin 4720b24beccSAndrey Ryabinin shadow_row += SHADOW_BYTES_PER_ROW; 4730b24beccSAndrey Ryabinin } 4740b24beccSAndrey Ryabinin } 4750b24beccSAndrey Ryabinin 4764fba3758SAndrey Konovalov static bool report_enabled(void) 477b0845ce5SMark Rutland { 478b0845ce5SMark Rutland if (current->kasan_depth) 479b0845ce5SMark Rutland return false; 480b0845ce5SMark Rutland if (test_bit(KASAN_BIT_MULTI_SHOT, &kasan_flags)) 481b0845ce5SMark Rutland return true; 482b0845ce5SMark Rutland return !test_and_set_bit(KASAN_BIT_REPORTED, &kasan_flags); 483b0845ce5SMark Rutland } 484b0845ce5SMark Rutland 48511cd3cd6SAndrey Konovalov void kasan_report_invalid_free(void *object, unsigned long ip) 48611cd3cd6SAndrey Konovalov { 48711cd3cd6SAndrey Konovalov unsigned long flags; 488ae8f06b3SWalter Wu u8 tag = get_tag(object); 48911cd3cd6SAndrey Konovalov 490ae8f06b3SWalter Wu object = reset_tag(object); 49111cd3cd6SAndrey Konovalov start_report(&flags); 49211cd3cd6SAndrey Konovalov pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", (void *)ip); 493ae8f06b3SWalter Wu print_tags(tag, object); 49411cd3cd6SAndrey Konovalov pr_err("\n"); 495ae8f06b3SWalter Wu print_address_description(object, tag); 49611cd3cd6SAndrey Konovalov pr_err("\n"); 49711cd3cd6SAndrey Konovalov print_shadow_for_address(object); 49811cd3cd6SAndrey Konovalov end_report(&flags); 49911cd3cd6SAndrey Konovalov } 50011cd3cd6SAndrey Konovalov 5014fba3758SAndrey Konovalov static void __kasan_report(unsigned long addr, size_t size, bool is_write, 5024fba3758SAndrey Konovalov unsigned long ip) 5030b24beccSAndrey Ryabinin { 5040b24beccSAndrey Ryabinin struct kasan_access_info info; 505121e8f81SAndrey Konovalov void *tagged_addr; 506121e8f81SAndrey Konovalov void *untagged_addr; 507121e8f81SAndrey Konovalov unsigned long flags; 5080b24beccSAndrey Ryabinin 5094f40c6e5SPeter Zijlstra disable_trace_on_warning(); 5104f40c6e5SPeter Zijlstra 511121e8f81SAndrey Konovalov tagged_addr = (void *)addr; 512121e8f81SAndrey Konovalov untagged_addr = reset_tag(tagged_addr); 513121e8f81SAndrey Konovalov 514121e8f81SAndrey Konovalov info.access_addr = tagged_addr; 515121e8f81SAndrey Konovalov if (addr_has_shadow(untagged_addr)) 516121e8f81SAndrey Konovalov info.first_bad_addr = find_first_bad_addr(tagged_addr, size); 517121e8f81SAndrey Konovalov else 518121e8f81SAndrey Konovalov info.first_bad_addr = untagged_addr; 5190b24beccSAndrey Ryabinin info.access_size = size; 5200b24beccSAndrey Ryabinin info.is_write = is_write; 5210b24beccSAndrey Ryabinin info.ip = ip; 522e9121076SAndrey Konovalov 523121e8f81SAndrey Konovalov start_report(&flags); 524121e8f81SAndrey Konovalov 525121e8f81SAndrey Konovalov print_error_description(&info); 526121e8f81SAndrey Konovalov if (addr_has_shadow(untagged_addr)) 527121e8f81SAndrey Konovalov print_tags(get_tag(tagged_addr), info.first_bad_addr); 528121e8f81SAndrey Konovalov pr_err("\n"); 529121e8f81SAndrey Konovalov 530121e8f81SAndrey Konovalov if (addr_has_shadow(untagged_addr)) { 531ae8f06b3SWalter Wu print_address_description(untagged_addr, get_tag(tagged_addr)); 532121e8f81SAndrey Konovalov pr_err("\n"); 533121e8f81SAndrey Konovalov print_shadow_for_address(info.first_bad_addr); 534121e8f81SAndrey Konovalov } else { 535121e8f81SAndrey Konovalov dump_stack(); 536121e8f81SAndrey Konovalov } 537121e8f81SAndrey Konovalov 538121e8f81SAndrey Konovalov end_report(&flags); 5390b24beccSAndrey Ryabinin } 5402f004eeaSJann Horn 5414fba3758SAndrey Konovalov bool kasan_report(unsigned long addr, size_t size, bool is_write, 5424fba3758SAndrey Konovalov unsigned long ip) 5434fba3758SAndrey Konovalov { 5444fba3758SAndrey Konovalov unsigned long flags = user_access_save(); 5454fba3758SAndrey Konovalov bool ret = false; 5464fba3758SAndrey Konovalov 5474fba3758SAndrey Konovalov if (likely(report_enabled())) { 5484fba3758SAndrey Konovalov __kasan_report(addr, size, is_write, ip); 5494fba3758SAndrey Konovalov ret = true; 5504fba3758SAndrey Konovalov } 5514fba3758SAndrey Konovalov 5524fba3758SAndrey Konovalov user_access_restore(flags); 5534fba3758SAndrey Konovalov 5544fba3758SAndrey Konovalov return ret; 5554fba3758SAndrey Konovalov } 5564fba3758SAndrey Konovalov 5572f004eeaSJann Horn #ifdef CONFIG_KASAN_INLINE 5582f004eeaSJann Horn /* 5592f004eeaSJann Horn * With CONFIG_KASAN_INLINE, accesses to bogus pointers (outside the high 5602f004eeaSJann Horn * canonical half of the address space) cause out-of-bounds shadow memory reads 5612f004eeaSJann Horn * before the actual access. For addresses in the low canonical half of the 5622f004eeaSJann Horn * address space, as well as most non-canonical addresses, that out-of-bounds 5632f004eeaSJann Horn * shadow memory access lands in the non-canonical part of the address space. 5642f004eeaSJann Horn * Help the user figure out what the original bogus pointer was. 5652f004eeaSJann Horn */ 5662f004eeaSJann Horn void kasan_non_canonical_hook(unsigned long addr) 5672f004eeaSJann Horn { 5682f004eeaSJann Horn unsigned long orig_addr; 5692f004eeaSJann Horn const char *bug_type; 5702f004eeaSJann Horn 5712f004eeaSJann Horn if (addr < KASAN_SHADOW_OFFSET) 5722f004eeaSJann Horn return; 5732f004eeaSJann Horn 5742f004eeaSJann Horn orig_addr = (addr - KASAN_SHADOW_OFFSET) << KASAN_SHADOW_SCALE_SHIFT; 5752f004eeaSJann Horn /* 5762f004eeaSJann Horn * For faults near the shadow address for NULL, we can be fairly certain 5772f004eeaSJann Horn * that this is a KASAN shadow memory access. 5782f004eeaSJann Horn * For faults that correspond to shadow for low canonical addresses, we 5792f004eeaSJann Horn * can still be pretty sure - that shadow region is a fairly narrow 5802f004eeaSJann Horn * chunk of the non-canonical address space. 5812f004eeaSJann Horn * But faults that look like shadow for non-canonical addresses are a 5822f004eeaSJann Horn * really large chunk of the address space. In that case, we still 5832f004eeaSJann Horn * print the decoded address, but make it clear that this is not 5842f004eeaSJann Horn * necessarily what's actually going on. 5852f004eeaSJann Horn */ 5862f004eeaSJann Horn if (orig_addr < PAGE_SIZE) 5872f004eeaSJann Horn bug_type = "null-ptr-deref"; 5882f004eeaSJann Horn else if (orig_addr < TASK_SIZE) 5892f004eeaSJann Horn bug_type = "probably user-memory-access"; 5902f004eeaSJann Horn else 5912f004eeaSJann Horn bug_type = "maybe wild-memory-access"; 5922f004eeaSJann Horn pr_alert("KASAN: %s in range [0x%016lx-0x%016lx]\n", bug_type, 5932f004eeaSJann Horn orig_addr, orig_addr + KASAN_SHADOW_MASK); 5942f004eeaSJann Horn } 5952f004eeaSJann Horn #endif 596