10b24beccSAndrey Ryabinin /* 20b24beccSAndrey Ryabinin * This file contains error reporting code. 30b24beccSAndrey Ryabinin * 40b24beccSAndrey Ryabinin * Copyright (c) 2014 Samsung Electronics Co., Ltd. 50b24beccSAndrey Ryabinin * Author: Andrey Ryabinin <a.ryabinin@samsung.com> 60b24beccSAndrey Ryabinin * 70b24beccSAndrey Ryabinin * Some of code borrowed from https://github.com/xairy/linux 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> 250b24beccSAndrey Ryabinin 26*bebf56a1SAndrey Ryabinin #include <asm/sections.h> 27*bebf56a1SAndrey Ryabinin 280b24beccSAndrey Ryabinin #include "kasan.h" 290316bec2SAndrey Ryabinin #include "../slab.h" 300b24beccSAndrey Ryabinin 310b24beccSAndrey Ryabinin /* Shadow layout customization. */ 320b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_BLOCK 1 330b24beccSAndrey Ryabinin #define SHADOW_BLOCKS_PER_ROW 16 340b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK) 350b24beccSAndrey Ryabinin #define SHADOW_ROWS_AROUND_ADDR 2 360b24beccSAndrey Ryabinin 370b24beccSAndrey Ryabinin static const void *find_first_bad_addr(const void *addr, size_t size) 380b24beccSAndrey Ryabinin { 390b24beccSAndrey Ryabinin u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr); 400b24beccSAndrey Ryabinin const void *first_bad_addr = addr; 410b24beccSAndrey Ryabinin 420b24beccSAndrey Ryabinin while (!shadow_val && first_bad_addr < addr + size) { 430b24beccSAndrey Ryabinin first_bad_addr += KASAN_SHADOW_SCALE_SIZE; 440b24beccSAndrey Ryabinin shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr); 450b24beccSAndrey Ryabinin } 460b24beccSAndrey Ryabinin return first_bad_addr; 470b24beccSAndrey Ryabinin } 480b24beccSAndrey Ryabinin 490b24beccSAndrey Ryabinin static void print_error_description(struct kasan_access_info *info) 500b24beccSAndrey Ryabinin { 510b24beccSAndrey Ryabinin const char *bug_type = "unknown crash"; 520b24beccSAndrey Ryabinin u8 shadow_val; 530b24beccSAndrey Ryabinin 540b24beccSAndrey Ryabinin info->first_bad_addr = find_first_bad_addr(info->access_addr, 550b24beccSAndrey Ryabinin info->access_size); 560b24beccSAndrey Ryabinin 570b24beccSAndrey Ryabinin shadow_val = *(u8 *)kasan_mem_to_shadow(info->first_bad_addr); 580b24beccSAndrey Ryabinin 590b24beccSAndrey Ryabinin switch (shadow_val) { 60b8c73fc2SAndrey Ryabinin case KASAN_FREE_PAGE: 610316bec2SAndrey Ryabinin case KASAN_KMALLOC_FREE: 62b8c73fc2SAndrey Ryabinin bug_type = "use after free"; 63b8c73fc2SAndrey Ryabinin break; 640316bec2SAndrey Ryabinin case KASAN_PAGE_REDZONE: 650316bec2SAndrey Ryabinin case KASAN_KMALLOC_REDZONE: 66*bebf56a1SAndrey Ryabinin case KASAN_GLOBAL_REDZONE: 670b24beccSAndrey Ryabinin case 0 ... KASAN_SHADOW_SCALE_SIZE - 1: 680b24beccSAndrey Ryabinin bug_type = "out of bounds access"; 690b24beccSAndrey Ryabinin break; 70c420f167SAndrey Ryabinin case KASAN_STACK_LEFT: 71c420f167SAndrey Ryabinin case KASAN_STACK_MID: 72c420f167SAndrey Ryabinin case KASAN_STACK_RIGHT: 73c420f167SAndrey Ryabinin case KASAN_STACK_PARTIAL: 74c420f167SAndrey Ryabinin bug_type = "out of bounds on stack"; 75c420f167SAndrey Ryabinin break; 760b24beccSAndrey Ryabinin } 770b24beccSAndrey Ryabinin 780b24beccSAndrey Ryabinin pr_err("BUG: KASan: %s in %pS at addr %p\n", 790b24beccSAndrey Ryabinin bug_type, (void *)info->ip, 800b24beccSAndrey Ryabinin info->access_addr); 810b24beccSAndrey Ryabinin pr_err("%s of size %zu by task %s/%d\n", 820b24beccSAndrey Ryabinin info->is_write ? "Write" : "Read", 830b24beccSAndrey Ryabinin info->access_size, current->comm, task_pid_nr(current)); 840b24beccSAndrey Ryabinin } 850b24beccSAndrey Ryabinin 86*bebf56a1SAndrey Ryabinin static inline bool kernel_or_module_addr(const void *addr) 87*bebf56a1SAndrey Ryabinin { 88*bebf56a1SAndrey Ryabinin return (addr >= (void *)_stext && addr < (void *)_end) 89*bebf56a1SAndrey Ryabinin || (addr >= (void *)MODULES_VADDR 90*bebf56a1SAndrey Ryabinin && addr < (void *)MODULES_END); 91*bebf56a1SAndrey Ryabinin } 92*bebf56a1SAndrey Ryabinin 93*bebf56a1SAndrey Ryabinin static inline bool init_task_stack_addr(const void *addr) 94*bebf56a1SAndrey Ryabinin { 95*bebf56a1SAndrey Ryabinin return addr >= (void *)&init_thread_union.stack && 96*bebf56a1SAndrey Ryabinin (addr <= (void *)&init_thread_union.stack + 97*bebf56a1SAndrey Ryabinin sizeof(init_thread_union.stack)); 98*bebf56a1SAndrey Ryabinin } 99*bebf56a1SAndrey Ryabinin 1000b24beccSAndrey Ryabinin static void print_address_description(struct kasan_access_info *info) 1010b24beccSAndrey Ryabinin { 102b8c73fc2SAndrey Ryabinin const void *addr = info->access_addr; 103b8c73fc2SAndrey Ryabinin 104b8c73fc2SAndrey Ryabinin if ((addr >= (void *)PAGE_OFFSET) && 105b8c73fc2SAndrey Ryabinin (addr < high_memory)) { 106b8c73fc2SAndrey Ryabinin struct page *page = virt_to_head_page(addr); 1070316bec2SAndrey Ryabinin 1080316bec2SAndrey Ryabinin if (PageSlab(page)) { 1090316bec2SAndrey Ryabinin void *object; 1100316bec2SAndrey Ryabinin struct kmem_cache *cache = page->slab_cache; 1110316bec2SAndrey Ryabinin void *last_object; 1120316bec2SAndrey Ryabinin 1130316bec2SAndrey Ryabinin object = virt_to_obj(cache, page_address(page), addr); 1140316bec2SAndrey Ryabinin last_object = page_address(page) + 1150316bec2SAndrey Ryabinin page->objects * cache->size; 1160316bec2SAndrey Ryabinin 1170316bec2SAndrey Ryabinin if (unlikely(object > last_object)) 1180316bec2SAndrey Ryabinin object = last_object; /* we hit into padding */ 1190316bec2SAndrey Ryabinin 1200316bec2SAndrey Ryabinin object_err(cache, page, object, 1210316bec2SAndrey Ryabinin "kasan: bad access detected"); 1220316bec2SAndrey Ryabinin return; 1230316bec2SAndrey Ryabinin } 124b8c73fc2SAndrey Ryabinin dump_page(page, "kasan: bad access detected"); 125b8c73fc2SAndrey Ryabinin } 126b8c73fc2SAndrey Ryabinin 127*bebf56a1SAndrey Ryabinin if (kernel_or_module_addr(addr)) { 128*bebf56a1SAndrey Ryabinin if (!init_task_stack_addr(addr)) 129*bebf56a1SAndrey Ryabinin pr_err("Address belongs to variable %pS\n", addr); 130*bebf56a1SAndrey Ryabinin } 131*bebf56a1SAndrey Ryabinin 1320b24beccSAndrey Ryabinin dump_stack(); 1330b24beccSAndrey Ryabinin } 1340b24beccSAndrey Ryabinin 1350b24beccSAndrey Ryabinin static bool row_is_guilty(const void *row, const void *guilty) 1360b24beccSAndrey Ryabinin { 1370b24beccSAndrey Ryabinin return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW); 1380b24beccSAndrey Ryabinin } 1390b24beccSAndrey Ryabinin 1400b24beccSAndrey Ryabinin static int shadow_pointer_offset(const void *row, const void *shadow) 1410b24beccSAndrey Ryabinin { 1420b24beccSAndrey Ryabinin /* The length of ">ff00ff00ff00ff00: " is 1430b24beccSAndrey Ryabinin * 3 + (BITS_PER_LONG/8)*2 chars. 1440b24beccSAndrey Ryabinin */ 1450b24beccSAndrey Ryabinin return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 + 1460b24beccSAndrey Ryabinin (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1; 1470b24beccSAndrey Ryabinin } 1480b24beccSAndrey Ryabinin 1490b24beccSAndrey Ryabinin static void print_shadow_for_address(const void *addr) 1500b24beccSAndrey Ryabinin { 1510b24beccSAndrey Ryabinin int i; 1520b24beccSAndrey Ryabinin const void *shadow = kasan_mem_to_shadow(addr); 1530b24beccSAndrey Ryabinin const void *shadow_row; 1540b24beccSAndrey Ryabinin 1550b24beccSAndrey Ryabinin shadow_row = (void *)round_down((unsigned long)shadow, 1560b24beccSAndrey Ryabinin SHADOW_BYTES_PER_ROW) 1570b24beccSAndrey Ryabinin - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW; 1580b24beccSAndrey Ryabinin 1590b24beccSAndrey Ryabinin pr_err("Memory state around the buggy address:\n"); 1600b24beccSAndrey Ryabinin 1610b24beccSAndrey Ryabinin for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) { 1620b24beccSAndrey Ryabinin const void *kaddr = kasan_shadow_to_mem(shadow_row); 1630b24beccSAndrey Ryabinin char buffer[4 + (BITS_PER_LONG/8)*2]; 1640b24beccSAndrey Ryabinin 1650b24beccSAndrey Ryabinin snprintf(buffer, sizeof(buffer), 1660b24beccSAndrey Ryabinin (i == 0) ? ">%p: " : " %p: ", kaddr); 1670b24beccSAndrey Ryabinin 1680b24beccSAndrey Ryabinin kasan_disable_current(); 1690b24beccSAndrey Ryabinin print_hex_dump(KERN_ERR, buffer, 1700b24beccSAndrey Ryabinin DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1, 1710b24beccSAndrey Ryabinin shadow_row, SHADOW_BYTES_PER_ROW, 0); 1720b24beccSAndrey Ryabinin kasan_enable_current(); 1730b24beccSAndrey Ryabinin 1740b24beccSAndrey Ryabinin if (row_is_guilty(shadow_row, shadow)) 1750b24beccSAndrey Ryabinin pr_err("%*c\n", 1760b24beccSAndrey Ryabinin shadow_pointer_offset(shadow_row, shadow), 1770b24beccSAndrey Ryabinin '^'); 1780b24beccSAndrey Ryabinin 1790b24beccSAndrey Ryabinin shadow_row += SHADOW_BYTES_PER_ROW; 1800b24beccSAndrey Ryabinin } 1810b24beccSAndrey Ryabinin } 1820b24beccSAndrey Ryabinin 1830b24beccSAndrey Ryabinin static DEFINE_SPINLOCK(report_lock); 1840b24beccSAndrey Ryabinin 1850b24beccSAndrey Ryabinin void kasan_report_error(struct kasan_access_info *info) 1860b24beccSAndrey Ryabinin { 1870b24beccSAndrey Ryabinin unsigned long flags; 1880b24beccSAndrey Ryabinin 1890b24beccSAndrey Ryabinin spin_lock_irqsave(&report_lock, flags); 1900b24beccSAndrey Ryabinin pr_err("=================================" 1910b24beccSAndrey Ryabinin "=================================\n"); 1920b24beccSAndrey Ryabinin print_error_description(info); 1930b24beccSAndrey Ryabinin print_address_description(info); 1940b24beccSAndrey Ryabinin print_shadow_for_address(info->first_bad_addr); 1950b24beccSAndrey Ryabinin pr_err("=================================" 1960b24beccSAndrey Ryabinin "=================================\n"); 1970b24beccSAndrey Ryabinin spin_unlock_irqrestore(&report_lock, flags); 1980b24beccSAndrey Ryabinin } 1990b24beccSAndrey Ryabinin 2000b24beccSAndrey Ryabinin void kasan_report_user_access(struct kasan_access_info *info) 2010b24beccSAndrey Ryabinin { 2020b24beccSAndrey Ryabinin unsigned long flags; 2030b24beccSAndrey Ryabinin 2040b24beccSAndrey Ryabinin spin_lock_irqsave(&report_lock, flags); 2050b24beccSAndrey Ryabinin pr_err("=================================" 2060b24beccSAndrey Ryabinin "=================================\n"); 2070b24beccSAndrey Ryabinin pr_err("BUG: KASan: user-memory-access on address %p\n", 2080b24beccSAndrey Ryabinin info->access_addr); 2090b24beccSAndrey Ryabinin pr_err("%s of size %zu by task %s/%d\n", 2100b24beccSAndrey Ryabinin info->is_write ? "Write" : "Read", 2110b24beccSAndrey Ryabinin info->access_size, current->comm, task_pid_nr(current)); 2120b24beccSAndrey Ryabinin dump_stack(); 2130b24beccSAndrey Ryabinin pr_err("=================================" 2140b24beccSAndrey Ryabinin "=================================\n"); 2150b24beccSAndrey Ryabinin spin_unlock_irqrestore(&report_lock, flags); 2160b24beccSAndrey Ryabinin } 2170b24beccSAndrey Ryabinin 2180b24beccSAndrey Ryabinin void kasan_report(unsigned long addr, size_t size, 2190b24beccSAndrey Ryabinin bool is_write, unsigned long ip) 2200b24beccSAndrey Ryabinin { 2210b24beccSAndrey Ryabinin struct kasan_access_info info; 2220b24beccSAndrey Ryabinin 2230b24beccSAndrey Ryabinin if (likely(!kasan_enabled())) 2240b24beccSAndrey Ryabinin return; 2250b24beccSAndrey Ryabinin 2260b24beccSAndrey Ryabinin info.access_addr = (void *)addr; 2270b24beccSAndrey Ryabinin info.access_size = size; 2280b24beccSAndrey Ryabinin info.is_write = is_write; 2290b24beccSAndrey Ryabinin info.ip = ip; 2300b24beccSAndrey Ryabinin kasan_report_error(&info); 2310b24beccSAndrey Ryabinin } 2320b24beccSAndrey Ryabinin 2330b24beccSAndrey Ryabinin 2340b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_LOAD(size) \ 2350b24beccSAndrey Ryabinin void __asan_report_load##size##_noabort(unsigned long addr) \ 2360b24beccSAndrey Ryabinin { \ 2370b24beccSAndrey Ryabinin kasan_report(addr, size, false, _RET_IP_); \ 2380b24beccSAndrey Ryabinin } \ 2390b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load##size##_noabort) 2400b24beccSAndrey Ryabinin 2410b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_STORE(size) \ 2420b24beccSAndrey Ryabinin void __asan_report_store##size##_noabort(unsigned long addr) \ 2430b24beccSAndrey Ryabinin { \ 2440b24beccSAndrey Ryabinin kasan_report(addr, size, true, _RET_IP_); \ 2450b24beccSAndrey Ryabinin } \ 2460b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store##size##_noabort) 2470b24beccSAndrey Ryabinin 2480b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(1); 2490b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(2); 2500b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(4); 2510b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(8); 2520b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(16); 2530b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(1); 2540b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(2); 2550b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(4); 2560b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(8); 2570b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(16); 2580b24beccSAndrey Ryabinin 2590b24beccSAndrey Ryabinin void __asan_report_load_n_noabort(unsigned long addr, size_t size) 2600b24beccSAndrey Ryabinin { 2610b24beccSAndrey Ryabinin kasan_report(addr, size, false, _RET_IP_); 2620b24beccSAndrey Ryabinin } 2630b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load_n_noabort); 2640b24beccSAndrey Ryabinin 2650b24beccSAndrey Ryabinin void __asan_report_store_n_noabort(unsigned long addr, size_t size) 2660b24beccSAndrey Ryabinin { 2670b24beccSAndrey Ryabinin kasan_report(addr, size, true, _RET_IP_); 2680b24beccSAndrey Ryabinin } 2690b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store_n_noabort); 270