1*0b24beccSAndrey Ryabinin /* 2*0b24beccSAndrey Ryabinin * This file contains error reporting code. 3*0b24beccSAndrey Ryabinin * 4*0b24beccSAndrey Ryabinin * Copyright (c) 2014 Samsung Electronics Co., Ltd. 5*0b24beccSAndrey Ryabinin * Author: Andrey Ryabinin <a.ryabinin@samsung.com> 6*0b24beccSAndrey Ryabinin * 7*0b24beccSAndrey Ryabinin * Some of code borrowed from https://github.com/xairy/linux by 8*0b24beccSAndrey Ryabinin * Andrey Konovalov <adech.fo@gmail.com> 9*0b24beccSAndrey Ryabinin * 10*0b24beccSAndrey Ryabinin * This program is free software; you can redistribute it and/or modify 11*0b24beccSAndrey Ryabinin * it under the terms of the GNU General Public License version 2 as 12*0b24beccSAndrey Ryabinin * published by the Free Software Foundation. 13*0b24beccSAndrey Ryabinin * 14*0b24beccSAndrey Ryabinin */ 15*0b24beccSAndrey Ryabinin 16*0b24beccSAndrey Ryabinin #include <linux/kernel.h> 17*0b24beccSAndrey Ryabinin #include <linux/mm.h> 18*0b24beccSAndrey Ryabinin #include <linux/printk.h> 19*0b24beccSAndrey Ryabinin #include <linux/sched.h> 20*0b24beccSAndrey Ryabinin #include <linux/slab.h> 21*0b24beccSAndrey Ryabinin #include <linux/stacktrace.h> 22*0b24beccSAndrey Ryabinin #include <linux/string.h> 23*0b24beccSAndrey Ryabinin #include <linux/types.h> 24*0b24beccSAndrey Ryabinin #include <linux/kasan.h> 25*0b24beccSAndrey Ryabinin 26*0b24beccSAndrey Ryabinin #include "kasan.h" 27*0b24beccSAndrey Ryabinin 28*0b24beccSAndrey Ryabinin /* Shadow layout customization. */ 29*0b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_BLOCK 1 30*0b24beccSAndrey Ryabinin #define SHADOW_BLOCKS_PER_ROW 16 31*0b24beccSAndrey Ryabinin #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK) 32*0b24beccSAndrey Ryabinin #define SHADOW_ROWS_AROUND_ADDR 2 33*0b24beccSAndrey Ryabinin 34*0b24beccSAndrey Ryabinin static const void *find_first_bad_addr(const void *addr, size_t size) 35*0b24beccSAndrey Ryabinin { 36*0b24beccSAndrey Ryabinin u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr); 37*0b24beccSAndrey Ryabinin const void *first_bad_addr = addr; 38*0b24beccSAndrey Ryabinin 39*0b24beccSAndrey Ryabinin while (!shadow_val && first_bad_addr < addr + size) { 40*0b24beccSAndrey Ryabinin first_bad_addr += KASAN_SHADOW_SCALE_SIZE; 41*0b24beccSAndrey Ryabinin shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr); 42*0b24beccSAndrey Ryabinin } 43*0b24beccSAndrey Ryabinin return first_bad_addr; 44*0b24beccSAndrey Ryabinin } 45*0b24beccSAndrey Ryabinin 46*0b24beccSAndrey Ryabinin static void print_error_description(struct kasan_access_info *info) 47*0b24beccSAndrey Ryabinin { 48*0b24beccSAndrey Ryabinin const char *bug_type = "unknown crash"; 49*0b24beccSAndrey Ryabinin u8 shadow_val; 50*0b24beccSAndrey Ryabinin 51*0b24beccSAndrey Ryabinin info->first_bad_addr = find_first_bad_addr(info->access_addr, 52*0b24beccSAndrey Ryabinin info->access_size); 53*0b24beccSAndrey Ryabinin 54*0b24beccSAndrey Ryabinin shadow_val = *(u8 *)kasan_mem_to_shadow(info->first_bad_addr); 55*0b24beccSAndrey Ryabinin 56*0b24beccSAndrey Ryabinin switch (shadow_val) { 57*0b24beccSAndrey Ryabinin case 0 ... KASAN_SHADOW_SCALE_SIZE - 1: 58*0b24beccSAndrey Ryabinin bug_type = "out of bounds access"; 59*0b24beccSAndrey Ryabinin break; 60*0b24beccSAndrey Ryabinin } 61*0b24beccSAndrey Ryabinin 62*0b24beccSAndrey Ryabinin pr_err("BUG: KASan: %s in %pS at addr %p\n", 63*0b24beccSAndrey Ryabinin bug_type, (void *)info->ip, 64*0b24beccSAndrey Ryabinin info->access_addr); 65*0b24beccSAndrey Ryabinin pr_err("%s of size %zu by task %s/%d\n", 66*0b24beccSAndrey Ryabinin info->is_write ? "Write" : "Read", 67*0b24beccSAndrey Ryabinin info->access_size, current->comm, task_pid_nr(current)); 68*0b24beccSAndrey Ryabinin } 69*0b24beccSAndrey Ryabinin 70*0b24beccSAndrey Ryabinin static void print_address_description(struct kasan_access_info *info) 71*0b24beccSAndrey Ryabinin { 72*0b24beccSAndrey Ryabinin dump_stack(); 73*0b24beccSAndrey Ryabinin } 74*0b24beccSAndrey Ryabinin 75*0b24beccSAndrey Ryabinin static bool row_is_guilty(const void *row, const void *guilty) 76*0b24beccSAndrey Ryabinin { 77*0b24beccSAndrey Ryabinin return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW); 78*0b24beccSAndrey Ryabinin } 79*0b24beccSAndrey Ryabinin 80*0b24beccSAndrey Ryabinin static int shadow_pointer_offset(const void *row, const void *shadow) 81*0b24beccSAndrey Ryabinin { 82*0b24beccSAndrey Ryabinin /* The length of ">ff00ff00ff00ff00: " is 83*0b24beccSAndrey Ryabinin * 3 + (BITS_PER_LONG/8)*2 chars. 84*0b24beccSAndrey Ryabinin */ 85*0b24beccSAndrey Ryabinin return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 + 86*0b24beccSAndrey Ryabinin (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1; 87*0b24beccSAndrey Ryabinin } 88*0b24beccSAndrey Ryabinin 89*0b24beccSAndrey Ryabinin static void print_shadow_for_address(const void *addr) 90*0b24beccSAndrey Ryabinin { 91*0b24beccSAndrey Ryabinin int i; 92*0b24beccSAndrey Ryabinin const void *shadow = kasan_mem_to_shadow(addr); 93*0b24beccSAndrey Ryabinin const void *shadow_row; 94*0b24beccSAndrey Ryabinin 95*0b24beccSAndrey Ryabinin shadow_row = (void *)round_down((unsigned long)shadow, 96*0b24beccSAndrey Ryabinin SHADOW_BYTES_PER_ROW) 97*0b24beccSAndrey Ryabinin - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW; 98*0b24beccSAndrey Ryabinin 99*0b24beccSAndrey Ryabinin pr_err("Memory state around the buggy address:\n"); 100*0b24beccSAndrey Ryabinin 101*0b24beccSAndrey Ryabinin for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) { 102*0b24beccSAndrey Ryabinin const void *kaddr = kasan_shadow_to_mem(shadow_row); 103*0b24beccSAndrey Ryabinin char buffer[4 + (BITS_PER_LONG/8)*2]; 104*0b24beccSAndrey Ryabinin 105*0b24beccSAndrey Ryabinin snprintf(buffer, sizeof(buffer), 106*0b24beccSAndrey Ryabinin (i == 0) ? ">%p: " : " %p: ", kaddr); 107*0b24beccSAndrey Ryabinin 108*0b24beccSAndrey Ryabinin kasan_disable_current(); 109*0b24beccSAndrey Ryabinin print_hex_dump(KERN_ERR, buffer, 110*0b24beccSAndrey Ryabinin DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1, 111*0b24beccSAndrey Ryabinin shadow_row, SHADOW_BYTES_PER_ROW, 0); 112*0b24beccSAndrey Ryabinin kasan_enable_current(); 113*0b24beccSAndrey Ryabinin 114*0b24beccSAndrey Ryabinin if (row_is_guilty(shadow_row, shadow)) 115*0b24beccSAndrey Ryabinin pr_err("%*c\n", 116*0b24beccSAndrey Ryabinin shadow_pointer_offset(shadow_row, shadow), 117*0b24beccSAndrey Ryabinin '^'); 118*0b24beccSAndrey Ryabinin 119*0b24beccSAndrey Ryabinin shadow_row += SHADOW_BYTES_PER_ROW; 120*0b24beccSAndrey Ryabinin } 121*0b24beccSAndrey Ryabinin } 122*0b24beccSAndrey Ryabinin 123*0b24beccSAndrey Ryabinin static DEFINE_SPINLOCK(report_lock); 124*0b24beccSAndrey Ryabinin 125*0b24beccSAndrey Ryabinin void kasan_report_error(struct kasan_access_info *info) 126*0b24beccSAndrey Ryabinin { 127*0b24beccSAndrey Ryabinin unsigned long flags; 128*0b24beccSAndrey Ryabinin 129*0b24beccSAndrey Ryabinin spin_lock_irqsave(&report_lock, flags); 130*0b24beccSAndrey Ryabinin pr_err("=================================" 131*0b24beccSAndrey Ryabinin "=================================\n"); 132*0b24beccSAndrey Ryabinin print_error_description(info); 133*0b24beccSAndrey Ryabinin print_address_description(info); 134*0b24beccSAndrey Ryabinin print_shadow_for_address(info->first_bad_addr); 135*0b24beccSAndrey Ryabinin pr_err("=================================" 136*0b24beccSAndrey Ryabinin "=================================\n"); 137*0b24beccSAndrey Ryabinin spin_unlock_irqrestore(&report_lock, flags); 138*0b24beccSAndrey Ryabinin } 139*0b24beccSAndrey Ryabinin 140*0b24beccSAndrey Ryabinin void kasan_report_user_access(struct kasan_access_info *info) 141*0b24beccSAndrey Ryabinin { 142*0b24beccSAndrey Ryabinin unsigned long flags; 143*0b24beccSAndrey Ryabinin 144*0b24beccSAndrey Ryabinin spin_lock_irqsave(&report_lock, flags); 145*0b24beccSAndrey Ryabinin pr_err("=================================" 146*0b24beccSAndrey Ryabinin "=================================\n"); 147*0b24beccSAndrey Ryabinin pr_err("BUG: KASan: user-memory-access on address %p\n", 148*0b24beccSAndrey Ryabinin info->access_addr); 149*0b24beccSAndrey Ryabinin pr_err("%s of size %zu by task %s/%d\n", 150*0b24beccSAndrey Ryabinin info->is_write ? "Write" : "Read", 151*0b24beccSAndrey Ryabinin info->access_size, current->comm, task_pid_nr(current)); 152*0b24beccSAndrey Ryabinin dump_stack(); 153*0b24beccSAndrey Ryabinin pr_err("=================================" 154*0b24beccSAndrey Ryabinin "=================================\n"); 155*0b24beccSAndrey Ryabinin spin_unlock_irqrestore(&report_lock, flags); 156*0b24beccSAndrey Ryabinin } 157*0b24beccSAndrey Ryabinin 158*0b24beccSAndrey Ryabinin void kasan_report(unsigned long addr, size_t size, 159*0b24beccSAndrey Ryabinin bool is_write, unsigned long ip) 160*0b24beccSAndrey Ryabinin { 161*0b24beccSAndrey Ryabinin struct kasan_access_info info; 162*0b24beccSAndrey Ryabinin 163*0b24beccSAndrey Ryabinin if (likely(!kasan_enabled())) 164*0b24beccSAndrey Ryabinin return; 165*0b24beccSAndrey Ryabinin 166*0b24beccSAndrey Ryabinin info.access_addr = (void *)addr; 167*0b24beccSAndrey Ryabinin info.access_size = size; 168*0b24beccSAndrey Ryabinin info.is_write = is_write; 169*0b24beccSAndrey Ryabinin info.ip = ip; 170*0b24beccSAndrey Ryabinin kasan_report_error(&info); 171*0b24beccSAndrey Ryabinin } 172*0b24beccSAndrey Ryabinin 173*0b24beccSAndrey Ryabinin 174*0b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_LOAD(size) \ 175*0b24beccSAndrey Ryabinin void __asan_report_load##size##_noabort(unsigned long addr) \ 176*0b24beccSAndrey Ryabinin { \ 177*0b24beccSAndrey Ryabinin kasan_report(addr, size, false, _RET_IP_); \ 178*0b24beccSAndrey Ryabinin } \ 179*0b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load##size##_noabort) 180*0b24beccSAndrey Ryabinin 181*0b24beccSAndrey Ryabinin #define DEFINE_ASAN_REPORT_STORE(size) \ 182*0b24beccSAndrey Ryabinin void __asan_report_store##size##_noabort(unsigned long addr) \ 183*0b24beccSAndrey Ryabinin { \ 184*0b24beccSAndrey Ryabinin kasan_report(addr, size, true, _RET_IP_); \ 185*0b24beccSAndrey Ryabinin } \ 186*0b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store##size##_noabort) 187*0b24beccSAndrey Ryabinin 188*0b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(1); 189*0b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(2); 190*0b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(4); 191*0b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(8); 192*0b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_LOAD(16); 193*0b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(1); 194*0b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(2); 195*0b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(4); 196*0b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(8); 197*0b24beccSAndrey Ryabinin DEFINE_ASAN_REPORT_STORE(16); 198*0b24beccSAndrey Ryabinin 199*0b24beccSAndrey Ryabinin void __asan_report_load_n_noabort(unsigned long addr, size_t size) 200*0b24beccSAndrey Ryabinin { 201*0b24beccSAndrey Ryabinin kasan_report(addr, size, false, _RET_IP_); 202*0b24beccSAndrey Ryabinin } 203*0b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_load_n_noabort); 204*0b24beccSAndrey Ryabinin 205*0b24beccSAndrey Ryabinin void __asan_report_store_n_noabort(unsigned long addr, size_t size) 206*0b24beccSAndrey Ryabinin { 207*0b24beccSAndrey Ryabinin kasan_report(addr, size, true, _RET_IP_); 208*0b24beccSAndrey Ryabinin } 209*0b24beccSAndrey Ryabinin EXPORT_SYMBOL(__asan_report_store_n_noabort); 210