10b24beccSAndrey Ryabinin /* 20b24beccSAndrey Ryabinin * This file contains error reporting code. 30b24beccSAndrey Ryabinin * 40b24beccSAndrey Ryabinin * Copyright (c) 2014 Samsung Electronics Co., Ltd. 52baf9e89SAndrey Ryabinin * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com> 60b24beccSAndrey Ryabinin * 75d0926efSAndrey Konovalov * Some code borrowed from https://github.com/xairy/kasan-prototype by 80b24beccSAndrey Ryabinin * Andrey Konovalov <adech.fo@gmail.com> 90b24beccSAndrey Ryabinin * 100b24beccSAndrey Ryabinin * This program is free software; you can redistribute it and/or modify 110b24beccSAndrey Ryabinin * it under the terms of the GNU General Public License version 2 as 120b24beccSAndrey Ryabinin * published by the Free Software Foundation. 130b24beccSAndrey Ryabinin * 140b24beccSAndrey Ryabinin */ 150b24beccSAndrey Ryabinin 160b24beccSAndrey Ryabinin #include <linux/kernel.h> 170b24beccSAndrey Ryabinin #include <linux/mm.h> 180b24beccSAndrey Ryabinin #include <linux/printk.h> 190b24beccSAndrey Ryabinin #include <linux/sched.h> 200b24beccSAndrey Ryabinin #include <linux/slab.h> 210b24beccSAndrey Ryabinin #include <linux/stacktrace.h> 220b24beccSAndrey Ryabinin #include <linux/string.h> 230b24beccSAndrey Ryabinin #include <linux/types.h> 240b24beccSAndrey Ryabinin #include <linux/kasan.h> 25527f215bSAneesh Kumar K.V #include <linux/module.h> 260b24beccSAndrey Ryabinin 27bebf56a1SAndrey Ryabinin #include <asm/sections.h> 28bebf56a1SAndrey Ryabinin 290b24beccSAndrey Ryabinin #include "kasan.h" 300316bec2SAndrey Ryabinin #include "../slab.h" 310b24beccSAndrey Ryabinin 320b24beccSAndrey Ryabinin /* Shadow layout customization. */ 330b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_BLOCK 1 340b24beccSAndrey Ryabinin #define SHADOW_BLOCKS_PER_ROW 16 350b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK) 360b24beccSAndrey Ryabinin #define SHADOW_ROWS_AROUND_ADDR 2 370b24beccSAndrey Ryabinin 380b24beccSAndrey Ryabinin static const void *find_first_bad_addr(const void *addr, size_t size) 390b24beccSAndrey Ryabinin { 400b24beccSAndrey Ryabinin u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr); 410b24beccSAndrey Ryabinin const void *first_bad_addr = addr; 420b24beccSAndrey Ryabinin 430b24beccSAndrey Ryabinin while (!shadow_val && first_bad_addr < addr + size) { 440b24beccSAndrey Ryabinin first_bad_addr += KASAN_SHADOW_SCALE_SIZE; 450b24beccSAndrey Ryabinin shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr); 460b24beccSAndrey Ryabinin } 470b24beccSAndrey Ryabinin return first_bad_addr; 480b24beccSAndrey Ryabinin } 490b24beccSAndrey Ryabinin 500b24beccSAndrey Ryabinin static void print_error_description(struct kasan_access_info *info) 510b24beccSAndrey Ryabinin { 520952d87fSAndrey Konovalov const char *bug_type = "unknown-crash"; 53cdf6a273SAndrey Konovalov u8 *shadow_addr; 540b24beccSAndrey Ryabinin 550b24beccSAndrey Ryabinin info->first_bad_addr = find_first_bad_addr(info->access_addr, 560b24beccSAndrey Ryabinin info->access_size); 570b24beccSAndrey Ryabinin 58cdf6a273SAndrey Konovalov shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr); 590b24beccSAndrey Ryabinin 60cdf6a273SAndrey Konovalov /* 61cdf6a273SAndrey Konovalov * If shadow byte value is in [0, KASAN_SHADOW_SCALE_SIZE) we can look 62cdf6a273SAndrey Konovalov * at the next shadow byte to determine the type of the bad access. 63cdf6a273SAndrey Konovalov */ 64cdf6a273SAndrey Konovalov if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1) 65cdf6a273SAndrey Konovalov shadow_addr++; 66cdf6a273SAndrey Konovalov 67cdf6a273SAndrey Konovalov switch (*shadow_addr) { 680952d87fSAndrey Konovalov case 0 ... KASAN_SHADOW_SCALE_SIZE - 1: 69cdf6a273SAndrey Konovalov /* 70cdf6a273SAndrey Konovalov * In theory it's still possible to see these shadow values 71cdf6a273SAndrey Konovalov * due to a data race in the kernel code. 72cdf6a273SAndrey Konovalov */ 730952d87fSAndrey Konovalov bug_type = "out-of-bounds"; 74b8c73fc2SAndrey Ryabinin break; 750316bec2SAndrey Ryabinin case KASAN_PAGE_REDZONE: 760316bec2SAndrey Ryabinin case KASAN_KMALLOC_REDZONE: 770952d87fSAndrey Konovalov bug_type = "slab-out-of-bounds"; 780952d87fSAndrey Konovalov break; 79bebf56a1SAndrey Ryabinin case KASAN_GLOBAL_REDZONE: 800952d87fSAndrey Konovalov bug_type = "global-out-of-bounds"; 810b24beccSAndrey Ryabinin break; 82c420f167SAndrey Ryabinin case KASAN_STACK_LEFT: 83c420f167SAndrey Ryabinin case KASAN_STACK_MID: 84c420f167SAndrey Ryabinin case KASAN_STACK_RIGHT: 85c420f167SAndrey Ryabinin case KASAN_STACK_PARTIAL: 860952d87fSAndrey Konovalov bug_type = "stack-out-of-bounds"; 870952d87fSAndrey Konovalov break; 880952d87fSAndrey Konovalov case KASAN_FREE_PAGE: 890952d87fSAndrey Konovalov case KASAN_KMALLOC_FREE: 900952d87fSAndrey Konovalov bug_type = "use-after-free"; 91c420f167SAndrey Ryabinin break; 920b24beccSAndrey Ryabinin } 930b24beccSAndrey Ryabinin 9425add7ecSAndrey Konovalov pr_err("BUG: KASAN: %s in %pS at addr %p\n", 950b24beccSAndrey Ryabinin bug_type, (void *)info->ip, 960b24beccSAndrey Ryabinin info->access_addr); 970b24beccSAndrey Ryabinin pr_err("%s of size %zu by task %s/%d\n", 980b24beccSAndrey Ryabinin info->is_write ? "Write" : "Read", 990b24beccSAndrey Ryabinin info->access_size, current->comm, task_pid_nr(current)); 1000b24beccSAndrey Ryabinin } 1010b24beccSAndrey Ryabinin 102bebf56a1SAndrey Ryabinin static inline bool kernel_or_module_addr(const void *addr) 103bebf56a1SAndrey Ryabinin { 104527f215bSAneesh Kumar K.V if (addr >= (void *)_stext && addr < (void *)_end) 105527f215bSAneesh Kumar K.V return true; 106527f215bSAneesh Kumar K.V if (is_module_address((unsigned long)addr)) 107527f215bSAneesh Kumar K.V return true; 108527f215bSAneesh Kumar K.V return false; 109bebf56a1SAndrey Ryabinin } 110bebf56a1SAndrey Ryabinin 111bebf56a1SAndrey Ryabinin static inline bool init_task_stack_addr(const void *addr) 112bebf56a1SAndrey Ryabinin { 113bebf56a1SAndrey Ryabinin return addr >= (void *)&init_thread_union.stack && 114bebf56a1SAndrey Ryabinin (addr <= (void *)&init_thread_union.stack + 115bebf56a1SAndrey Ryabinin sizeof(init_thread_union.stack)); 116bebf56a1SAndrey Ryabinin } 117bebf56a1SAndrey Ryabinin 118*7ed2f9e6SAlexander Potapenko #ifdef CONFIG_SLAB 119*7ed2f9e6SAlexander Potapenko static void print_track(struct kasan_track *track) 120*7ed2f9e6SAlexander Potapenko { 121*7ed2f9e6SAlexander Potapenko pr_err("PID = %u, CPU = %u, timestamp = %lu\n", track->pid, 122*7ed2f9e6SAlexander Potapenko track->cpu, (unsigned long)track->when); 123*7ed2f9e6SAlexander Potapenko } 124*7ed2f9e6SAlexander Potapenko 125*7ed2f9e6SAlexander Potapenko static void object_err(struct kmem_cache *cache, struct page *page, 126*7ed2f9e6SAlexander Potapenko void *object, char *unused_reason) 127*7ed2f9e6SAlexander Potapenko { 128*7ed2f9e6SAlexander Potapenko struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object); 129*7ed2f9e6SAlexander Potapenko struct kasan_free_meta *free_info; 130*7ed2f9e6SAlexander Potapenko 131*7ed2f9e6SAlexander Potapenko dump_stack(); 132*7ed2f9e6SAlexander Potapenko pr_err("Object at %p, in cache %s\n", object, cache->name); 133*7ed2f9e6SAlexander Potapenko if (!(cache->flags & SLAB_KASAN)) 134*7ed2f9e6SAlexander Potapenko return; 135*7ed2f9e6SAlexander Potapenko switch (alloc_info->state) { 136*7ed2f9e6SAlexander Potapenko case KASAN_STATE_INIT: 137*7ed2f9e6SAlexander Potapenko pr_err("Object not allocated yet\n"); 138*7ed2f9e6SAlexander Potapenko break; 139*7ed2f9e6SAlexander Potapenko case KASAN_STATE_ALLOC: 140*7ed2f9e6SAlexander Potapenko pr_err("Object allocated with size %u bytes.\n", 141*7ed2f9e6SAlexander Potapenko alloc_info->alloc_size); 142*7ed2f9e6SAlexander Potapenko pr_err("Allocation:\n"); 143*7ed2f9e6SAlexander Potapenko print_track(&alloc_info->track); 144*7ed2f9e6SAlexander Potapenko break; 145*7ed2f9e6SAlexander Potapenko case KASAN_STATE_FREE: 146*7ed2f9e6SAlexander Potapenko pr_err("Object freed, allocated with size %u bytes\n", 147*7ed2f9e6SAlexander Potapenko alloc_info->alloc_size); 148*7ed2f9e6SAlexander Potapenko free_info = get_free_info(cache, object); 149*7ed2f9e6SAlexander Potapenko pr_err("Allocation:\n"); 150*7ed2f9e6SAlexander Potapenko print_track(&alloc_info->track); 151*7ed2f9e6SAlexander Potapenko pr_err("Deallocation:\n"); 152*7ed2f9e6SAlexander Potapenko print_track(&free_info->track); 153*7ed2f9e6SAlexander Potapenko break; 154*7ed2f9e6SAlexander Potapenko } 155*7ed2f9e6SAlexander Potapenko } 156*7ed2f9e6SAlexander Potapenko #endif 157*7ed2f9e6SAlexander Potapenko 1580b24beccSAndrey Ryabinin static void print_address_description(struct kasan_access_info *info) 1590b24beccSAndrey Ryabinin { 160b8c73fc2SAndrey Ryabinin const void *addr = info->access_addr; 161b8c73fc2SAndrey Ryabinin 162b8c73fc2SAndrey Ryabinin if ((addr >= (void *)PAGE_OFFSET) && 163b8c73fc2SAndrey Ryabinin (addr < high_memory)) { 164b8c73fc2SAndrey Ryabinin struct page *page = virt_to_head_page(addr); 1650316bec2SAndrey Ryabinin 1660316bec2SAndrey Ryabinin if (PageSlab(page)) { 1670316bec2SAndrey Ryabinin void *object; 1680316bec2SAndrey Ryabinin struct kmem_cache *cache = page->slab_cache; 169*7ed2f9e6SAlexander Potapenko object = nearest_obj(cache, page, 170*7ed2f9e6SAlexander Potapenko (void *)info->access_addr); 1710316bec2SAndrey Ryabinin object_err(cache, page, object, 1720316bec2SAndrey Ryabinin "kasan: bad access detected"); 1730316bec2SAndrey Ryabinin return; 1740316bec2SAndrey Ryabinin } 175b8c73fc2SAndrey Ryabinin dump_page(page, "kasan: bad access detected"); 176b8c73fc2SAndrey Ryabinin } 177b8c73fc2SAndrey Ryabinin 178bebf56a1SAndrey Ryabinin if (kernel_or_module_addr(addr)) { 179bebf56a1SAndrey Ryabinin if (!init_task_stack_addr(addr)) 180bebf56a1SAndrey Ryabinin pr_err("Address belongs to variable %pS\n", addr); 181bebf56a1SAndrey Ryabinin } 1820b24beccSAndrey Ryabinin dump_stack(); 1830b24beccSAndrey Ryabinin } 1840b24beccSAndrey Ryabinin 1850b24beccSAndrey Ryabinin static bool row_is_guilty(const void *row, const void *guilty) 1860b24beccSAndrey Ryabinin { 1870b24beccSAndrey Ryabinin return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW); 1880b24beccSAndrey Ryabinin } 1890b24beccSAndrey Ryabinin 1900b24beccSAndrey Ryabinin static int shadow_pointer_offset(const void *row, const void *shadow) 1910b24beccSAndrey Ryabinin { 1920b24beccSAndrey Ryabinin /* The length of ">ff00ff00ff00ff00: " is 1930b24beccSAndrey Ryabinin * 3 + (BITS_PER_LONG/8)*2 chars. 1940b24beccSAndrey Ryabinin */ 1950b24beccSAndrey Ryabinin return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 + 1960b24beccSAndrey Ryabinin (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1; 1970b24beccSAndrey Ryabinin } 1980b24beccSAndrey Ryabinin 1990b24beccSAndrey Ryabinin static void print_shadow_for_address(const void *addr) 2000b24beccSAndrey Ryabinin { 2010b24beccSAndrey Ryabinin int i; 2020b24beccSAndrey Ryabinin const void *shadow = kasan_mem_to_shadow(addr); 2030b24beccSAndrey Ryabinin const void *shadow_row; 2040b24beccSAndrey Ryabinin 2050b24beccSAndrey Ryabinin shadow_row = (void *)round_down((unsigned long)shadow, 2060b24beccSAndrey Ryabinin SHADOW_BYTES_PER_ROW) 2070b24beccSAndrey Ryabinin - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW; 2080b24beccSAndrey Ryabinin 2090b24beccSAndrey Ryabinin pr_err("Memory state around the buggy address:\n"); 2100b24beccSAndrey Ryabinin 2110b24beccSAndrey Ryabinin for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) { 2120b24beccSAndrey Ryabinin const void *kaddr = kasan_shadow_to_mem(shadow_row); 2130b24beccSAndrey Ryabinin char buffer[4 + (BITS_PER_LONG/8)*2]; 214f2377d4eSAneesh Kumar K.V char shadow_buf[SHADOW_BYTES_PER_ROW]; 2150b24beccSAndrey Ryabinin 2160b24beccSAndrey Ryabinin snprintf(buffer, sizeof(buffer), 2170b24beccSAndrey Ryabinin (i == 0) ? ">%p: " : " %p: ", kaddr); 218f2377d4eSAneesh Kumar K.V /* 219f2377d4eSAneesh Kumar K.V * We should not pass a shadow pointer to generic 220f2377d4eSAneesh Kumar K.V * function, because generic functions may try to 221f2377d4eSAneesh Kumar K.V * access kasan mapping for the passed address. 222f2377d4eSAneesh Kumar K.V */ 223f2377d4eSAneesh Kumar K.V memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW); 2240b24beccSAndrey Ryabinin print_hex_dump(KERN_ERR, buffer, 2250b24beccSAndrey Ryabinin DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1, 226f2377d4eSAneesh Kumar K.V shadow_buf, SHADOW_BYTES_PER_ROW, 0); 2270b24beccSAndrey Ryabinin 2280b24beccSAndrey Ryabinin if (row_is_guilty(shadow_row, shadow)) 2290b24beccSAndrey Ryabinin pr_err("%*c\n", 2300b24beccSAndrey Ryabinin shadow_pointer_offset(shadow_row, shadow), 2310b24beccSAndrey Ryabinin '^'); 2320b24beccSAndrey Ryabinin 2330b24beccSAndrey Ryabinin shadow_row += SHADOW_BYTES_PER_ROW; 2340b24beccSAndrey Ryabinin } 2350b24beccSAndrey Ryabinin } 2360b24beccSAndrey Ryabinin 2370b24beccSAndrey Ryabinin static DEFINE_SPINLOCK(report_lock); 2380b24beccSAndrey Ryabinin 239e9121076SAndrey Konovalov static void kasan_report_error(struct kasan_access_info *info) 2400b24beccSAndrey Ryabinin { 2410b24beccSAndrey Ryabinin unsigned long flags; 242e9121076SAndrey Konovalov const char *bug_type; 2430b24beccSAndrey Ryabinin 244fc5aeeafSAneesh Kumar K.V /* 245fc5aeeafSAneesh Kumar K.V * Make sure we don't end up in loop. 246fc5aeeafSAneesh Kumar K.V */ 247fc5aeeafSAneesh Kumar K.V kasan_disable_current(); 2480b24beccSAndrey Ryabinin spin_lock_irqsave(&report_lock, flags); 249756a025fSJoe Perches pr_err("==================================================================\n"); 250e9121076SAndrey Konovalov if (info->access_addr < 251e9121076SAndrey Konovalov kasan_shadow_to_mem((void *)KASAN_SHADOW_START)) { 252e9121076SAndrey Konovalov if ((unsigned long)info->access_addr < PAGE_SIZE) 253e9121076SAndrey Konovalov bug_type = "null-ptr-deref"; 254e9121076SAndrey Konovalov else if ((unsigned long)info->access_addr < TASK_SIZE) 255e9121076SAndrey Konovalov bug_type = "user-memory-access"; 256e9121076SAndrey Konovalov else 257e9121076SAndrey Konovalov bug_type = "wild-memory-access"; 25825add7ecSAndrey Konovalov pr_err("BUG: KASAN: %s on address %p\n", 259e9121076SAndrey Konovalov bug_type, info->access_addr); 260e9121076SAndrey Konovalov pr_err("%s of size %zu by task %s/%d\n", 261e9121076SAndrey Konovalov info->is_write ? "Write" : "Read", 262e9121076SAndrey Konovalov info->access_size, current->comm, 263e9121076SAndrey Konovalov task_pid_nr(current)); 264e9121076SAndrey Konovalov dump_stack(); 265e9121076SAndrey Konovalov } else { 2660b24beccSAndrey Ryabinin print_error_description(info); 2670b24beccSAndrey Ryabinin print_address_description(info); 2680b24beccSAndrey Ryabinin print_shadow_for_address(info->first_bad_addr); 2690b24beccSAndrey Ryabinin } 270756a025fSJoe Perches pr_err("==================================================================\n"); 271eb06f43fSAndrey Ryabinin add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 2720b24beccSAndrey Ryabinin spin_unlock_irqrestore(&report_lock, flags); 273fc5aeeafSAneesh Kumar K.V kasan_enable_current(); 2740b24beccSAndrey Ryabinin } 2750b24beccSAndrey Ryabinin 2760b24beccSAndrey Ryabinin void kasan_report(unsigned long addr, size_t size, 2770b24beccSAndrey Ryabinin bool is_write, unsigned long ip) 2780b24beccSAndrey Ryabinin { 2790b24beccSAndrey Ryabinin struct kasan_access_info info; 2800b24beccSAndrey Ryabinin 2810ba8663cSAneesh Kumar K.V if (likely(!kasan_report_enabled())) 2820b24beccSAndrey Ryabinin return; 2830b24beccSAndrey Ryabinin 2840b24beccSAndrey Ryabinin info.access_addr = (void *)addr; 2850b24beccSAndrey Ryabinin info.access_size = size; 2860b24beccSAndrey Ryabinin info.is_write = is_write; 2870b24beccSAndrey Ryabinin info.ip = ip; 288e9121076SAndrey Konovalov 2890b24beccSAndrey Ryabinin kasan_report_error(&info); 2900b24beccSAndrey Ryabinin } 2910b24beccSAndrey Ryabinin 2920b24beccSAndrey Ryabinin 2930b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_LOAD(size) \ 2940b24beccSAndrey Ryabinin void __asan_report_load##size##_noabort(unsigned long addr) \ 2950b24beccSAndrey Ryabinin { \ 2960b24beccSAndrey Ryabinin kasan_report(addr, size, false, _RET_IP_); \ 2970b24beccSAndrey Ryabinin } \ 2980b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load##size##_noabort) 2990b24beccSAndrey Ryabinin 3000b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_STORE(size) \ 3010b24beccSAndrey Ryabinin void __asan_report_store##size##_noabort(unsigned long addr) \ 3020b24beccSAndrey Ryabinin { \ 3030b24beccSAndrey Ryabinin kasan_report(addr, size, true, _RET_IP_); \ 3040b24beccSAndrey Ryabinin } \ 3050b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store##size##_noabort) 3060b24beccSAndrey Ryabinin 3070b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(1); 3080b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(2); 3090b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(4); 3100b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(8); 3110b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(16); 3120b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(1); 3130b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(2); 3140b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(4); 3150b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(8); 3160b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(16); 3170b24beccSAndrey Ryabinin 3180b24beccSAndrey Ryabinin void __asan_report_load_n_noabort(unsigned long addr, size_t size) 3190b24beccSAndrey Ryabinin { 3200b24beccSAndrey Ryabinin kasan_report(addr, size, false, _RET_IP_); 3210b24beccSAndrey Ryabinin } 3220b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load_n_noabort); 3230b24beccSAndrey Ryabinin 3240b24beccSAndrey Ryabinin void __asan_report_store_n_noabort(unsigned long addr, size_t size) 3250b24beccSAndrey Ryabinin { 3260b24beccSAndrey Ryabinin kasan_report(addr, size, true, _RET_IP_); 3270b24beccSAndrey Ryabinin } 3280b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store_n_noabort); 329