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