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 260b24beccSAndrey Ryabinin #include "kasan.h" 27*0316bec2SAndrey Ryabinin #include "../slab.h" 280b24beccSAndrey Ryabinin 290b24beccSAndrey Ryabinin /* Shadow layout customization. */ 300b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_BLOCK 1 310b24beccSAndrey Ryabinin #define SHADOW_BLOCKS_PER_ROW 16 320b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK) 330b24beccSAndrey Ryabinin #define SHADOW_ROWS_AROUND_ADDR 2 340b24beccSAndrey Ryabinin 350b24beccSAndrey Ryabinin static const void *find_first_bad_addr(const void *addr, size_t size) 360b24beccSAndrey Ryabinin { 370b24beccSAndrey Ryabinin u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr); 380b24beccSAndrey Ryabinin const void *first_bad_addr = addr; 390b24beccSAndrey Ryabinin 400b24beccSAndrey Ryabinin while (!shadow_val && first_bad_addr < addr + size) { 410b24beccSAndrey Ryabinin first_bad_addr += KASAN_SHADOW_SCALE_SIZE; 420b24beccSAndrey Ryabinin shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr); 430b24beccSAndrey Ryabinin } 440b24beccSAndrey Ryabinin return first_bad_addr; 450b24beccSAndrey Ryabinin } 460b24beccSAndrey Ryabinin 470b24beccSAndrey Ryabinin static void print_error_description(struct kasan_access_info *info) 480b24beccSAndrey Ryabinin { 490b24beccSAndrey Ryabinin const char *bug_type = "unknown crash"; 500b24beccSAndrey Ryabinin u8 shadow_val; 510b24beccSAndrey Ryabinin 520b24beccSAndrey Ryabinin info->first_bad_addr = find_first_bad_addr(info->access_addr, 530b24beccSAndrey Ryabinin info->access_size); 540b24beccSAndrey Ryabinin 550b24beccSAndrey Ryabinin shadow_val = *(u8 *)kasan_mem_to_shadow(info->first_bad_addr); 560b24beccSAndrey Ryabinin 570b24beccSAndrey Ryabinin switch (shadow_val) { 58b8c73fc2SAndrey Ryabinin case KASAN_FREE_PAGE: 59*0316bec2SAndrey Ryabinin case KASAN_KMALLOC_FREE: 60b8c73fc2SAndrey Ryabinin bug_type = "use after free"; 61b8c73fc2SAndrey Ryabinin break; 62*0316bec2SAndrey Ryabinin case KASAN_PAGE_REDZONE: 63*0316bec2SAndrey Ryabinin case KASAN_KMALLOC_REDZONE: 640b24beccSAndrey Ryabinin case 0 ... KASAN_SHADOW_SCALE_SIZE - 1: 650b24beccSAndrey Ryabinin bug_type = "out of bounds access"; 660b24beccSAndrey Ryabinin break; 670b24beccSAndrey Ryabinin } 680b24beccSAndrey Ryabinin 690b24beccSAndrey Ryabinin pr_err("BUG: KASan: %s in %pS at addr %p\n", 700b24beccSAndrey Ryabinin bug_type, (void *)info->ip, 710b24beccSAndrey Ryabinin info->access_addr); 720b24beccSAndrey Ryabinin pr_err("%s of size %zu by task %s/%d\n", 730b24beccSAndrey Ryabinin info->is_write ? "Write" : "Read", 740b24beccSAndrey Ryabinin info->access_size, current->comm, task_pid_nr(current)); 750b24beccSAndrey Ryabinin } 760b24beccSAndrey Ryabinin 770b24beccSAndrey Ryabinin static void print_address_description(struct kasan_access_info *info) 780b24beccSAndrey Ryabinin { 79b8c73fc2SAndrey Ryabinin const void *addr = info->access_addr; 80b8c73fc2SAndrey Ryabinin 81b8c73fc2SAndrey Ryabinin if ((addr >= (void *)PAGE_OFFSET) && 82b8c73fc2SAndrey Ryabinin (addr < high_memory)) { 83b8c73fc2SAndrey Ryabinin struct page *page = virt_to_head_page(addr); 84*0316bec2SAndrey Ryabinin 85*0316bec2SAndrey Ryabinin if (PageSlab(page)) { 86*0316bec2SAndrey Ryabinin void *object; 87*0316bec2SAndrey Ryabinin struct kmem_cache *cache = page->slab_cache; 88*0316bec2SAndrey Ryabinin void *last_object; 89*0316bec2SAndrey Ryabinin 90*0316bec2SAndrey Ryabinin object = virt_to_obj(cache, page_address(page), addr); 91*0316bec2SAndrey Ryabinin last_object = page_address(page) + 92*0316bec2SAndrey Ryabinin page->objects * cache->size; 93*0316bec2SAndrey Ryabinin 94*0316bec2SAndrey Ryabinin if (unlikely(object > last_object)) 95*0316bec2SAndrey Ryabinin object = last_object; /* we hit into padding */ 96*0316bec2SAndrey Ryabinin 97*0316bec2SAndrey Ryabinin object_err(cache, page, object, 98*0316bec2SAndrey Ryabinin "kasan: bad access detected"); 99*0316bec2SAndrey Ryabinin return; 100*0316bec2SAndrey Ryabinin } 101b8c73fc2SAndrey Ryabinin dump_page(page, "kasan: bad access detected"); 102b8c73fc2SAndrey Ryabinin } 103b8c73fc2SAndrey Ryabinin 1040b24beccSAndrey Ryabinin dump_stack(); 1050b24beccSAndrey Ryabinin } 1060b24beccSAndrey Ryabinin 1070b24beccSAndrey Ryabinin static bool row_is_guilty(const void *row, const void *guilty) 1080b24beccSAndrey Ryabinin { 1090b24beccSAndrey Ryabinin return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW); 1100b24beccSAndrey Ryabinin } 1110b24beccSAndrey Ryabinin 1120b24beccSAndrey Ryabinin static int shadow_pointer_offset(const void *row, const void *shadow) 1130b24beccSAndrey Ryabinin { 1140b24beccSAndrey Ryabinin /* The length of ">ff00ff00ff00ff00: " is 1150b24beccSAndrey Ryabinin * 3 + (BITS_PER_LONG/8)*2 chars. 1160b24beccSAndrey Ryabinin */ 1170b24beccSAndrey Ryabinin return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 + 1180b24beccSAndrey Ryabinin (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1; 1190b24beccSAndrey Ryabinin } 1200b24beccSAndrey Ryabinin 1210b24beccSAndrey Ryabinin static void print_shadow_for_address(const void *addr) 1220b24beccSAndrey Ryabinin { 1230b24beccSAndrey Ryabinin int i; 1240b24beccSAndrey Ryabinin const void *shadow = kasan_mem_to_shadow(addr); 1250b24beccSAndrey Ryabinin const void *shadow_row; 1260b24beccSAndrey Ryabinin 1270b24beccSAndrey Ryabinin shadow_row = (void *)round_down((unsigned long)shadow, 1280b24beccSAndrey Ryabinin SHADOW_BYTES_PER_ROW) 1290b24beccSAndrey Ryabinin - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW; 1300b24beccSAndrey Ryabinin 1310b24beccSAndrey Ryabinin pr_err("Memory state around the buggy address:\n"); 1320b24beccSAndrey Ryabinin 1330b24beccSAndrey Ryabinin for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) { 1340b24beccSAndrey Ryabinin const void *kaddr = kasan_shadow_to_mem(shadow_row); 1350b24beccSAndrey Ryabinin char buffer[4 + (BITS_PER_LONG/8)*2]; 1360b24beccSAndrey Ryabinin 1370b24beccSAndrey Ryabinin snprintf(buffer, sizeof(buffer), 1380b24beccSAndrey Ryabinin (i == 0) ? ">%p: " : " %p: ", kaddr); 1390b24beccSAndrey Ryabinin 1400b24beccSAndrey Ryabinin kasan_disable_current(); 1410b24beccSAndrey Ryabinin print_hex_dump(KERN_ERR, buffer, 1420b24beccSAndrey Ryabinin DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1, 1430b24beccSAndrey Ryabinin shadow_row, SHADOW_BYTES_PER_ROW, 0); 1440b24beccSAndrey Ryabinin kasan_enable_current(); 1450b24beccSAndrey Ryabinin 1460b24beccSAndrey Ryabinin if (row_is_guilty(shadow_row, shadow)) 1470b24beccSAndrey Ryabinin pr_err("%*c\n", 1480b24beccSAndrey Ryabinin shadow_pointer_offset(shadow_row, shadow), 1490b24beccSAndrey Ryabinin '^'); 1500b24beccSAndrey Ryabinin 1510b24beccSAndrey Ryabinin shadow_row += SHADOW_BYTES_PER_ROW; 1520b24beccSAndrey Ryabinin } 1530b24beccSAndrey Ryabinin } 1540b24beccSAndrey Ryabinin 1550b24beccSAndrey Ryabinin static DEFINE_SPINLOCK(report_lock); 1560b24beccSAndrey Ryabinin 1570b24beccSAndrey Ryabinin void kasan_report_error(struct kasan_access_info *info) 1580b24beccSAndrey Ryabinin { 1590b24beccSAndrey Ryabinin unsigned long flags; 1600b24beccSAndrey Ryabinin 1610b24beccSAndrey Ryabinin spin_lock_irqsave(&report_lock, flags); 1620b24beccSAndrey Ryabinin pr_err("=================================" 1630b24beccSAndrey Ryabinin "=================================\n"); 1640b24beccSAndrey Ryabinin print_error_description(info); 1650b24beccSAndrey Ryabinin print_address_description(info); 1660b24beccSAndrey Ryabinin print_shadow_for_address(info->first_bad_addr); 1670b24beccSAndrey Ryabinin pr_err("=================================" 1680b24beccSAndrey Ryabinin "=================================\n"); 1690b24beccSAndrey Ryabinin spin_unlock_irqrestore(&report_lock, flags); 1700b24beccSAndrey Ryabinin } 1710b24beccSAndrey Ryabinin 1720b24beccSAndrey Ryabinin void kasan_report_user_access(struct kasan_access_info *info) 1730b24beccSAndrey Ryabinin { 1740b24beccSAndrey Ryabinin unsigned long flags; 1750b24beccSAndrey Ryabinin 1760b24beccSAndrey Ryabinin spin_lock_irqsave(&report_lock, flags); 1770b24beccSAndrey Ryabinin pr_err("=================================" 1780b24beccSAndrey Ryabinin "=================================\n"); 1790b24beccSAndrey Ryabinin pr_err("BUG: KASan: user-memory-access on address %p\n", 1800b24beccSAndrey Ryabinin info->access_addr); 1810b24beccSAndrey Ryabinin pr_err("%s of size %zu by task %s/%d\n", 1820b24beccSAndrey Ryabinin info->is_write ? "Write" : "Read", 1830b24beccSAndrey Ryabinin info->access_size, current->comm, task_pid_nr(current)); 1840b24beccSAndrey Ryabinin dump_stack(); 1850b24beccSAndrey Ryabinin pr_err("=================================" 1860b24beccSAndrey Ryabinin "=================================\n"); 1870b24beccSAndrey Ryabinin spin_unlock_irqrestore(&report_lock, flags); 1880b24beccSAndrey Ryabinin } 1890b24beccSAndrey Ryabinin 1900b24beccSAndrey Ryabinin void kasan_report(unsigned long addr, size_t size, 1910b24beccSAndrey Ryabinin bool is_write, unsigned long ip) 1920b24beccSAndrey Ryabinin { 1930b24beccSAndrey Ryabinin struct kasan_access_info info; 1940b24beccSAndrey Ryabinin 1950b24beccSAndrey Ryabinin if (likely(!kasan_enabled())) 1960b24beccSAndrey Ryabinin return; 1970b24beccSAndrey Ryabinin 1980b24beccSAndrey Ryabinin info.access_addr = (void *)addr; 1990b24beccSAndrey Ryabinin info.access_size = size; 2000b24beccSAndrey Ryabinin info.is_write = is_write; 2010b24beccSAndrey Ryabinin info.ip = ip; 2020b24beccSAndrey Ryabinin kasan_report_error(&info); 2030b24beccSAndrey Ryabinin } 2040b24beccSAndrey Ryabinin 2050b24beccSAndrey Ryabinin 2060b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_LOAD(size) \ 2070b24beccSAndrey Ryabinin void __asan_report_load##size##_noabort(unsigned long addr) \ 2080b24beccSAndrey Ryabinin { \ 2090b24beccSAndrey Ryabinin kasan_report(addr, size, false, _RET_IP_); \ 2100b24beccSAndrey Ryabinin } \ 2110b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load##size##_noabort) 2120b24beccSAndrey Ryabinin 2130b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_STORE(size) \ 2140b24beccSAndrey Ryabinin void __asan_report_store##size##_noabort(unsigned long addr) \ 2150b24beccSAndrey Ryabinin { \ 2160b24beccSAndrey Ryabinin kasan_report(addr, size, true, _RET_IP_); \ 2170b24beccSAndrey Ryabinin } \ 2180b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store##size##_noabort) 2190b24beccSAndrey Ryabinin 2200b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(1); 2210b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(2); 2220b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(4); 2230b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(8); 2240b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(16); 2250b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(1); 2260b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(2); 2270b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(4); 2280b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(8); 2290b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(16); 2300b24beccSAndrey Ryabinin 2310b24beccSAndrey Ryabinin void __asan_report_load_n_noabort(unsigned long addr, size_t size) 2320b24beccSAndrey Ryabinin { 2330b24beccSAndrey Ryabinin kasan_report(addr, size, false, _RET_IP_); 2340b24beccSAndrey Ryabinin } 2350b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load_n_noabort); 2360b24beccSAndrey Ryabinin 2370b24beccSAndrey Ryabinin void __asan_report_store_n_noabort(unsigned long addr, size_t size) 2380b24beccSAndrey Ryabinin { 2390b24beccSAndrey Ryabinin kasan_report(addr, size, true, _RET_IP_); 2400b24beccSAndrey Ryabinin } 2410b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store_n_noabort); 242