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" 270b24beccSAndrey Ryabinin 280b24beccSAndrey Ryabinin /* Shadow layout customization. */ 290b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_BLOCK 1 300b24beccSAndrey Ryabinin #define SHADOW_BLOCKS_PER_ROW 16 310b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK) 320b24beccSAndrey Ryabinin #define SHADOW_ROWS_AROUND_ADDR 2 330b24beccSAndrey Ryabinin 340b24beccSAndrey Ryabinin static const void *find_first_bad_addr(const void *addr, size_t size) 350b24beccSAndrey Ryabinin { 360b24beccSAndrey Ryabinin u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr); 370b24beccSAndrey Ryabinin const void *first_bad_addr = addr; 380b24beccSAndrey Ryabinin 390b24beccSAndrey Ryabinin while (!shadow_val && first_bad_addr < addr + size) { 400b24beccSAndrey Ryabinin first_bad_addr += KASAN_SHADOW_SCALE_SIZE; 410b24beccSAndrey Ryabinin shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr); 420b24beccSAndrey Ryabinin } 430b24beccSAndrey Ryabinin return first_bad_addr; 440b24beccSAndrey Ryabinin } 450b24beccSAndrey Ryabinin 460b24beccSAndrey Ryabinin static void print_error_description(struct kasan_access_info *info) 470b24beccSAndrey Ryabinin { 480b24beccSAndrey Ryabinin const char *bug_type = "unknown crash"; 490b24beccSAndrey Ryabinin u8 shadow_val; 500b24beccSAndrey Ryabinin 510b24beccSAndrey Ryabinin info->first_bad_addr = find_first_bad_addr(info->access_addr, 520b24beccSAndrey Ryabinin info->access_size); 530b24beccSAndrey Ryabinin 540b24beccSAndrey Ryabinin shadow_val = *(u8 *)kasan_mem_to_shadow(info->first_bad_addr); 550b24beccSAndrey Ryabinin 560b24beccSAndrey Ryabinin switch (shadow_val) { 57*b8c73fc2SAndrey Ryabinin case KASAN_FREE_PAGE: 58*b8c73fc2SAndrey Ryabinin bug_type = "use after free"; 59*b8c73fc2SAndrey Ryabinin break; 600b24beccSAndrey Ryabinin case 0 ... KASAN_SHADOW_SCALE_SIZE - 1: 610b24beccSAndrey Ryabinin bug_type = "out of bounds access"; 620b24beccSAndrey Ryabinin break; 630b24beccSAndrey Ryabinin } 640b24beccSAndrey Ryabinin 650b24beccSAndrey Ryabinin pr_err("BUG: KASan: %s in %pS at addr %p\n", 660b24beccSAndrey Ryabinin bug_type, (void *)info->ip, 670b24beccSAndrey Ryabinin info->access_addr); 680b24beccSAndrey Ryabinin pr_err("%s of size %zu by task %s/%d\n", 690b24beccSAndrey Ryabinin info->is_write ? "Write" : "Read", 700b24beccSAndrey Ryabinin info->access_size, current->comm, task_pid_nr(current)); 710b24beccSAndrey Ryabinin } 720b24beccSAndrey Ryabinin 730b24beccSAndrey Ryabinin static void print_address_description(struct kasan_access_info *info) 740b24beccSAndrey Ryabinin { 75*b8c73fc2SAndrey Ryabinin const void *addr = info->access_addr; 76*b8c73fc2SAndrey Ryabinin 77*b8c73fc2SAndrey Ryabinin if ((addr >= (void *)PAGE_OFFSET) && 78*b8c73fc2SAndrey Ryabinin (addr < high_memory)) { 79*b8c73fc2SAndrey Ryabinin struct page *page = virt_to_head_page(addr); 80*b8c73fc2SAndrey Ryabinin dump_page(page, "kasan: bad access detected"); 81*b8c73fc2SAndrey Ryabinin } 82*b8c73fc2SAndrey Ryabinin 830b24beccSAndrey Ryabinin dump_stack(); 840b24beccSAndrey Ryabinin } 850b24beccSAndrey Ryabinin 860b24beccSAndrey Ryabinin static bool row_is_guilty(const void *row, const void *guilty) 870b24beccSAndrey Ryabinin { 880b24beccSAndrey Ryabinin return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW); 890b24beccSAndrey Ryabinin } 900b24beccSAndrey Ryabinin 910b24beccSAndrey Ryabinin static int shadow_pointer_offset(const void *row, const void *shadow) 920b24beccSAndrey Ryabinin { 930b24beccSAndrey Ryabinin /* The length of ">ff00ff00ff00ff00: " is 940b24beccSAndrey Ryabinin * 3 + (BITS_PER_LONG/8)*2 chars. 950b24beccSAndrey Ryabinin */ 960b24beccSAndrey Ryabinin return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 + 970b24beccSAndrey Ryabinin (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1; 980b24beccSAndrey Ryabinin } 990b24beccSAndrey Ryabinin 1000b24beccSAndrey Ryabinin static void print_shadow_for_address(const void *addr) 1010b24beccSAndrey Ryabinin { 1020b24beccSAndrey Ryabinin int i; 1030b24beccSAndrey Ryabinin const void *shadow = kasan_mem_to_shadow(addr); 1040b24beccSAndrey Ryabinin const void *shadow_row; 1050b24beccSAndrey Ryabinin 1060b24beccSAndrey Ryabinin shadow_row = (void *)round_down((unsigned long)shadow, 1070b24beccSAndrey Ryabinin SHADOW_BYTES_PER_ROW) 1080b24beccSAndrey Ryabinin - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW; 1090b24beccSAndrey Ryabinin 1100b24beccSAndrey Ryabinin pr_err("Memory state around the buggy address:\n"); 1110b24beccSAndrey Ryabinin 1120b24beccSAndrey Ryabinin for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) { 1130b24beccSAndrey Ryabinin const void *kaddr = kasan_shadow_to_mem(shadow_row); 1140b24beccSAndrey Ryabinin char buffer[4 + (BITS_PER_LONG/8)*2]; 1150b24beccSAndrey Ryabinin 1160b24beccSAndrey Ryabinin snprintf(buffer, sizeof(buffer), 1170b24beccSAndrey Ryabinin (i == 0) ? ">%p: " : " %p: ", kaddr); 1180b24beccSAndrey Ryabinin 1190b24beccSAndrey Ryabinin kasan_disable_current(); 1200b24beccSAndrey Ryabinin print_hex_dump(KERN_ERR, buffer, 1210b24beccSAndrey Ryabinin DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1, 1220b24beccSAndrey Ryabinin shadow_row, SHADOW_BYTES_PER_ROW, 0); 1230b24beccSAndrey Ryabinin kasan_enable_current(); 1240b24beccSAndrey Ryabinin 1250b24beccSAndrey Ryabinin if (row_is_guilty(shadow_row, shadow)) 1260b24beccSAndrey Ryabinin pr_err("%*c\n", 1270b24beccSAndrey Ryabinin shadow_pointer_offset(shadow_row, shadow), 1280b24beccSAndrey Ryabinin '^'); 1290b24beccSAndrey Ryabinin 1300b24beccSAndrey Ryabinin shadow_row += SHADOW_BYTES_PER_ROW; 1310b24beccSAndrey Ryabinin } 1320b24beccSAndrey Ryabinin } 1330b24beccSAndrey Ryabinin 1340b24beccSAndrey Ryabinin static DEFINE_SPINLOCK(report_lock); 1350b24beccSAndrey Ryabinin 1360b24beccSAndrey Ryabinin void kasan_report_error(struct kasan_access_info *info) 1370b24beccSAndrey Ryabinin { 1380b24beccSAndrey Ryabinin unsigned long flags; 1390b24beccSAndrey Ryabinin 1400b24beccSAndrey Ryabinin spin_lock_irqsave(&report_lock, flags); 1410b24beccSAndrey Ryabinin pr_err("=================================" 1420b24beccSAndrey Ryabinin "=================================\n"); 1430b24beccSAndrey Ryabinin print_error_description(info); 1440b24beccSAndrey Ryabinin print_address_description(info); 1450b24beccSAndrey Ryabinin print_shadow_for_address(info->first_bad_addr); 1460b24beccSAndrey Ryabinin pr_err("=================================" 1470b24beccSAndrey Ryabinin "=================================\n"); 1480b24beccSAndrey Ryabinin spin_unlock_irqrestore(&report_lock, flags); 1490b24beccSAndrey Ryabinin } 1500b24beccSAndrey Ryabinin 1510b24beccSAndrey Ryabinin void kasan_report_user_access(struct kasan_access_info *info) 1520b24beccSAndrey Ryabinin { 1530b24beccSAndrey Ryabinin unsigned long flags; 1540b24beccSAndrey Ryabinin 1550b24beccSAndrey Ryabinin spin_lock_irqsave(&report_lock, flags); 1560b24beccSAndrey Ryabinin pr_err("=================================" 1570b24beccSAndrey Ryabinin "=================================\n"); 1580b24beccSAndrey Ryabinin pr_err("BUG: KASan: user-memory-access on address %p\n", 1590b24beccSAndrey Ryabinin info->access_addr); 1600b24beccSAndrey Ryabinin pr_err("%s of size %zu by task %s/%d\n", 1610b24beccSAndrey Ryabinin info->is_write ? "Write" : "Read", 1620b24beccSAndrey Ryabinin info->access_size, current->comm, task_pid_nr(current)); 1630b24beccSAndrey Ryabinin dump_stack(); 1640b24beccSAndrey Ryabinin pr_err("=================================" 1650b24beccSAndrey Ryabinin "=================================\n"); 1660b24beccSAndrey Ryabinin spin_unlock_irqrestore(&report_lock, flags); 1670b24beccSAndrey Ryabinin } 1680b24beccSAndrey Ryabinin 1690b24beccSAndrey Ryabinin void kasan_report(unsigned long addr, size_t size, 1700b24beccSAndrey Ryabinin bool is_write, unsigned long ip) 1710b24beccSAndrey Ryabinin { 1720b24beccSAndrey Ryabinin struct kasan_access_info info; 1730b24beccSAndrey Ryabinin 1740b24beccSAndrey Ryabinin if (likely(!kasan_enabled())) 1750b24beccSAndrey Ryabinin return; 1760b24beccSAndrey Ryabinin 1770b24beccSAndrey Ryabinin info.access_addr = (void *)addr; 1780b24beccSAndrey Ryabinin info.access_size = size; 1790b24beccSAndrey Ryabinin info.is_write = is_write; 1800b24beccSAndrey Ryabinin info.ip = ip; 1810b24beccSAndrey Ryabinin kasan_report_error(&info); 1820b24beccSAndrey Ryabinin } 1830b24beccSAndrey Ryabinin 1840b24beccSAndrey Ryabinin 1850b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_LOAD(size) \ 1860b24beccSAndrey Ryabinin void __asan_report_load##size##_noabort(unsigned long addr) \ 1870b24beccSAndrey Ryabinin { \ 1880b24beccSAndrey Ryabinin kasan_report(addr, size, false, _RET_IP_); \ 1890b24beccSAndrey Ryabinin } \ 1900b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load##size##_noabort) 1910b24beccSAndrey Ryabinin 1920b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_STORE(size) \ 1930b24beccSAndrey Ryabinin void __asan_report_store##size##_noabort(unsigned long addr) \ 1940b24beccSAndrey Ryabinin { \ 1950b24beccSAndrey Ryabinin kasan_report(addr, size, true, _RET_IP_); \ 1960b24beccSAndrey Ryabinin } \ 1970b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store##size##_noabort) 1980b24beccSAndrey Ryabinin 1990b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(1); 2000b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(2); 2010b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(4); 2020b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(8); 2030b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(16); 2040b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(1); 2050b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(2); 2060b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(4); 2070b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(8); 2080b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(16); 2090b24beccSAndrey Ryabinin 2100b24beccSAndrey Ryabinin void __asan_report_load_n_noabort(unsigned long addr, size_t size) 2110b24beccSAndrey Ryabinin { 2120b24beccSAndrey Ryabinin kasan_report(addr, size, false, _RET_IP_); 2130b24beccSAndrey Ryabinin } 2140b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load_n_noabort); 2150b24beccSAndrey Ryabinin 2160b24beccSAndrey Ryabinin void __asan_report_store_n_noabort(unsigned long addr, size_t size) 2170b24beccSAndrey Ryabinin { 2180b24beccSAndrey Ryabinin kasan_report(addr, size, true, _RET_IP_); 2190b24beccSAndrey Ryabinin } 2200b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store_n_noabort); 221