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 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame^] | 26 | #include <asm/sections.h> |
| 27 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 28 | #include "kasan.h" |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 29 | #include "../slab.h" |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 30 | |
| 31 | /* Shadow layout customization. */ |
| 32 | #define SHADOW_BYTES_PER_BLOCK 1 |
| 33 | #define SHADOW_BLOCKS_PER_ROW 16 |
| 34 | #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK) |
| 35 | #define SHADOW_ROWS_AROUND_ADDR 2 |
| 36 | |
| 37 | static const void *find_first_bad_addr(const void *addr, size_t size) |
| 38 | { |
| 39 | u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr); |
| 40 | const void *first_bad_addr = addr; |
| 41 | |
| 42 | while (!shadow_val && first_bad_addr < addr + size) { |
| 43 | first_bad_addr += KASAN_SHADOW_SCALE_SIZE; |
| 44 | shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr); |
| 45 | } |
| 46 | return first_bad_addr; |
| 47 | } |
| 48 | |
| 49 | static void print_error_description(struct kasan_access_info *info) |
| 50 | { |
| 51 | const char *bug_type = "unknown crash"; |
| 52 | u8 shadow_val; |
| 53 | |
| 54 | info->first_bad_addr = find_first_bad_addr(info->access_addr, |
| 55 | info->access_size); |
| 56 | |
| 57 | shadow_val = *(u8 *)kasan_mem_to_shadow(info->first_bad_addr); |
| 58 | |
| 59 | switch (shadow_val) { |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 60 | case KASAN_FREE_PAGE: |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 61 | case KASAN_KMALLOC_FREE: |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 62 | bug_type = "use after free"; |
| 63 | break; |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 64 | case KASAN_PAGE_REDZONE: |
| 65 | case KASAN_KMALLOC_REDZONE: |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame^] | 66 | case KASAN_GLOBAL_REDZONE: |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 67 | case 0 ... KASAN_SHADOW_SCALE_SIZE - 1: |
| 68 | bug_type = "out of bounds access"; |
| 69 | break; |
Andrey Ryabinin | c420f16 | 2015-02-13 14:39:59 -0800 | [diff] [blame] | 70 | case KASAN_STACK_LEFT: |
| 71 | case KASAN_STACK_MID: |
| 72 | case KASAN_STACK_RIGHT: |
| 73 | case KASAN_STACK_PARTIAL: |
| 74 | bug_type = "out of bounds on stack"; |
| 75 | break; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | pr_err("BUG: KASan: %s in %pS at addr %p\n", |
| 79 | bug_type, (void *)info->ip, |
| 80 | info->access_addr); |
| 81 | pr_err("%s of size %zu by task %s/%d\n", |
| 82 | info->is_write ? "Write" : "Read", |
| 83 | info->access_size, current->comm, task_pid_nr(current)); |
| 84 | } |
| 85 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame^] | 86 | static inline bool kernel_or_module_addr(const void *addr) |
| 87 | { |
| 88 | return (addr >= (void *)_stext && addr < (void *)_end) |
| 89 | || (addr >= (void *)MODULES_VADDR |
| 90 | && addr < (void *)MODULES_END); |
| 91 | } |
| 92 | |
| 93 | static inline bool init_task_stack_addr(const void *addr) |
| 94 | { |
| 95 | return addr >= (void *)&init_thread_union.stack && |
| 96 | (addr <= (void *)&init_thread_union.stack + |
| 97 | sizeof(init_thread_union.stack)); |
| 98 | } |
| 99 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 100 | static void print_address_description(struct kasan_access_info *info) |
| 101 | { |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 102 | const void *addr = info->access_addr; |
| 103 | |
| 104 | if ((addr >= (void *)PAGE_OFFSET) && |
| 105 | (addr < high_memory)) { |
| 106 | struct page *page = virt_to_head_page(addr); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 107 | |
| 108 | if (PageSlab(page)) { |
| 109 | void *object; |
| 110 | struct kmem_cache *cache = page->slab_cache; |
| 111 | void *last_object; |
| 112 | |
| 113 | object = virt_to_obj(cache, page_address(page), addr); |
| 114 | last_object = page_address(page) + |
| 115 | page->objects * cache->size; |
| 116 | |
| 117 | if (unlikely(object > last_object)) |
| 118 | object = last_object; /* we hit into padding */ |
| 119 | |
| 120 | object_err(cache, page, object, |
| 121 | "kasan: bad access detected"); |
| 122 | return; |
| 123 | } |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 124 | dump_page(page, "kasan: bad access detected"); |
| 125 | } |
| 126 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame^] | 127 | if (kernel_or_module_addr(addr)) { |
| 128 | if (!init_task_stack_addr(addr)) |
| 129 | pr_err("Address belongs to variable %pS\n", addr); |
| 130 | } |
| 131 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 132 | dump_stack(); |
| 133 | } |
| 134 | |
| 135 | static bool row_is_guilty(const void *row, const void *guilty) |
| 136 | { |
| 137 | return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW); |
| 138 | } |
| 139 | |
| 140 | static int shadow_pointer_offset(const void *row, const void *shadow) |
| 141 | { |
| 142 | /* The length of ">ff00ff00ff00ff00: " is |
| 143 | * 3 + (BITS_PER_LONG/8)*2 chars. |
| 144 | */ |
| 145 | return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 + |
| 146 | (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1; |
| 147 | } |
| 148 | |
| 149 | static void print_shadow_for_address(const void *addr) |
| 150 | { |
| 151 | int i; |
| 152 | const void *shadow = kasan_mem_to_shadow(addr); |
| 153 | const void *shadow_row; |
| 154 | |
| 155 | shadow_row = (void *)round_down((unsigned long)shadow, |
| 156 | SHADOW_BYTES_PER_ROW) |
| 157 | - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW; |
| 158 | |
| 159 | pr_err("Memory state around the buggy address:\n"); |
| 160 | |
| 161 | for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) { |
| 162 | const void *kaddr = kasan_shadow_to_mem(shadow_row); |
| 163 | char buffer[4 + (BITS_PER_LONG/8)*2]; |
| 164 | |
| 165 | snprintf(buffer, sizeof(buffer), |
| 166 | (i == 0) ? ">%p: " : " %p: ", kaddr); |
| 167 | |
| 168 | kasan_disable_current(); |
| 169 | print_hex_dump(KERN_ERR, buffer, |
| 170 | DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1, |
| 171 | shadow_row, SHADOW_BYTES_PER_ROW, 0); |
| 172 | kasan_enable_current(); |
| 173 | |
| 174 | if (row_is_guilty(shadow_row, shadow)) |
| 175 | pr_err("%*c\n", |
| 176 | shadow_pointer_offset(shadow_row, shadow), |
| 177 | '^'); |
| 178 | |
| 179 | shadow_row += SHADOW_BYTES_PER_ROW; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static DEFINE_SPINLOCK(report_lock); |
| 184 | |
| 185 | void kasan_report_error(struct kasan_access_info *info) |
| 186 | { |
| 187 | unsigned long flags; |
| 188 | |
| 189 | spin_lock_irqsave(&report_lock, flags); |
| 190 | pr_err("=================================" |
| 191 | "=================================\n"); |
| 192 | print_error_description(info); |
| 193 | print_address_description(info); |
| 194 | print_shadow_for_address(info->first_bad_addr); |
| 195 | pr_err("=================================" |
| 196 | "=================================\n"); |
| 197 | spin_unlock_irqrestore(&report_lock, flags); |
| 198 | } |
| 199 | |
| 200 | void kasan_report_user_access(struct kasan_access_info *info) |
| 201 | { |
| 202 | unsigned long flags; |
| 203 | |
| 204 | spin_lock_irqsave(&report_lock, flags); |
| 205 | pr_err("=================================" |
| 206 | "=================================\n"); |
| 207 | pr_err("BUG: KASan: user-memory-access on address %p\n", |
| 208 | info->access_addr); |
| 209 | pr_err("%s of size %zu by task %s/%d\n", |
| 210 | info->is_write ? "Write" : "Read", |
| 211 | info->access_size, current->comm, task_pid_nr(current)); |
| 212 | dump_stack(); |
| 213 | pr_err("=================================" |
| 214 | "=================================\n"); |
| 215 | spin_unlock_irqrestore(&report_lock, flags); |
| 216 | } |
| 217 | |
| 218 | void kasan_report(unsigned long addr, size_t size, |
| 219 | bool is_write, unsigned long ip) |
| 220 | { |
| 221 | struct kasan_access_info info; |
| 222 | |
| 223 | if (likely(!kasan_enabled())) |
| 224 | return; |
| 225 | |
| 226 | info.access_addr = (void *)addr; |
| 227 | info.access_size = size; |
| 228 | info.is_write = is_write; |
| 229 | info.ip = ip; |
| 230 | kasan_report_error(&info); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | #define DEFINE_ASAN_REPORT_LOAD(size) \ |
| 235 | void __asan_report_load##size##_noabort(unsigned long addr) \ |
| 236 | { \ |
| 237 | kasan_report(addr, size, false, _RET_IP_); \ |
| 238 | } \ |
| 239 | EXPORT_SYMBOL(__asan_report_load##size##_noabort) |
| 240 | |
| 241 | #define DEFINE_ASAN_REPORT_STORE(size) \ |
| 242 | void __asan_report_store##size##_noabort(unsigned long addr) \ |
| 243 | { \ |
| 244 | kasan_report(addr, size, true, _RET_IP_); \ |
| 245 | } \ |
| 246 | EXPORT_SYMBOL(__asan_report_store##size##_noabort) |
| 247 | |
| 248 | DEFINE_ASAN_REPORT_LOAD(1); |
| 249 | DEFINE_ASAN_REPORT_LOAD(2); |
| 250 | DEFINE_ASAN_REPORT_LOAD(4); |
| 251 | DEFINE_ASAN_REPORT_LOAD(8); |
| 252 | DEFINE_ASAN_REPORT_LOAD(16); |
| 253 | DEFINE_ASAN_REPORT_STORE(1); |
| 254 | DEFINE_ASAN_REPORT_STORE(2); |
| 255 | DEFINE_ASAN_REPORT_STORE(4); |
| 256 | DEFINE_ASAN_REPORT_STORE(8); |
| 257 | DEFINE_ASAN_REPORT_STORE(16); |
| 258 | |
| 259 | void __asan_report_load_n_noabort(unsigned long addr, size_t size) |
| 260 | { |
| 261 | kasan_report(addr, size, false, _RET_IP_); |
| 262 | } |
| 263 | EXPORT_SYMBOL(__asan_report_load_n_noabort); |
| 264 | |
| 265 | void __asan_report_store_n_noabort(unsigned long addr, size_t size) |
| 266 | { |
| 267 | kasan_report(addr, size, true, _RET_IP_); |
| 268 | } |
| 269 | EXPORT_SYMBOL(__asan_report_store_n_noabort); |