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. |
Andrey Ryabinin | 2baf9e8 | 2015-08-14 15:35:13 -0700 | [diff] [blame] | 5 | * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 6 | * |
Andrey Konovalov | 5d0926e | 2015-11-05 18:51:12 -0800 | [diff] [blame] | 7 | * Some code borrowed from https://github.com/xairy/kasan-prototype by |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 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> |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 21 | #include <linux/stackdepot.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 22 | #include <linux/stacktrace.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/kasan.h> |
Aneesh Kumar K.V | 527f215 | 2015-11-05 18:50:46 -0800 | [diff] [blame] | 26 | #include <linux/module.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 27 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 28 | #include <asm/sections.h> |
| 29 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 30 | #include "kasan.h" |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 31 | #include "../slab.h" |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 32 | |
| 33 | /* Shadow layout customization. */ |
| 34 | #define SHADOW_BYTES_PER_BLOCK 1 |
| 35 | #define SHADOW_BLOCKS_PER_ROW 16 |
| 36 | #define SHADOW_BYTES_PER_ROW (SHADOW_BLOCKS_PER_ROW * SHADOW_BYTES_PER_BLOCK) |
| 37 | #define SHADOW_ROWS_AROUND_ADDR 2 |
| 38 | |
| 39 | static const void *find_first_bad_addr(const void *addr, size_t size) |
| 40 | { |
| 41 | u8 shadow_val = *(u8 *)kasan_mem_to_shadow(addr); |
| 42 | const void *first_bad_addr = addr; |
| 43 | |
| 44 | while (!shadow_val && first_bad_addr < addr + size) { |
| 45 | first_bad_addr += KASAN_SHADOW_SCALE_SIZE; |
| 46 | shadow_val = *(u8 *)kasan_mem_to_shadow(first_bad_addr); |
| 47 | } |
| 48 | return first_bad_addr; |
| 49 | } |
| 50 | |
| 51 | static void print_error_description(struct kasan_access_info *info) |
| 52 | { |
Andrey Konovalov | 0952d87 | 2015-11-05 18:50:58 -0800 | [diff] [blame] | 53 | const char *bug_type = "unknown-crash"; |
Andrey Konovalov | cdf6a27 | 2015-11-05 18:51:01 -0800 | [diff] [blame] | 54 | u8 *shadow_addr; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 55 | |
| 56 | info->first_bad_addr = find_first_bad_addr(info->access_addr, |
| 57 | info->access_size); |
| 58 | |
Andrey Konovalov | cdf6a27 | 2015-11-05 18:51:01 -0800 | [diff] [blame] | 59 | shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 60 | |
Andrey Konovalov | cdf6a27 | 2015-11-05 18:51:01 -0800 | [diff] [blame] | 61 | /* |
| 62 | * If shadow byte value is in [0, KASAN_SHADOW_SCALE_SIZE) we can look |
| 63 | * at the next shadow byte to determine the type of the bad access. |
| 64 | */ |
| 65 | if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1) |
| 66 | shadow_addr++; |
| 67 | |
| 68 | switch (*shadow_addr) { |
Andrey Konovalov | 0952d87 | 2015-11-05 18:50:58 -0800 | [diff] [blame] | 69 | case 0 ... KASAN_SHADOW_SCALE_SIZE - 1: |
Andrey Konovalov | cdf6a27 | 2015-11-05 18:51:01 -0800 | [diff] [blame] | 70 | /* |
| 71 | * In theory it's still possible to see these shadow values |
| 72 | * due to a data race in the kernel code. |
| 73 | */ |
Andrey Konovalov | 0952d87 | 2015-11-05 18:50:58 -0800 | [diff] [blame] | 74 | bug_type = "out-of-bounds"; |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 75 | break; |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 76 | case KASAN_PAGE_REDZONE: |
| 77 | case KASAN_KMALLOC_REDZONE: |
Andrey Konovalov | 0952d87 | 2015-11-05 18:50:58 -0800 | [diff] [blame] | 78 | bug_type = "slab-out-of-bounds"; |
| 79 | break; |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 80 | case KASAN_GLOBAL_REDZONE: |
Andrey Konovalov | 0952d87 | 2015-11-05 18:50:58 -0800 | [diff] [blame] | 81 | bug_type = "global-out-of-bounds"; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 82 | break; |
Andrey Ryabinin | c420f16 | 2015-02-13 14:39:59 -0800 | [diff] [blame] | 83 | case KASAN_STACK_LEFT: |
| 84 | case KASAN_STACK_MID: |
| 85 | case KASAN_STACK_RIGHT: |
| 86 | case KASAN_STACK_PARTIAL: |
Andrey Konovalov | 0952d87 | 2015-11-05 18:50:58 -0800 | [diff] [blame] | 87 | bug_type = "stack-out-of-bounds"; |
| 88 | break; |
| 89 | case KASAN_FREE_PAGE: |
| 90 | case KASAN_KMALLOC_FREE: |
| 91 | bug_type = "use-after-free"; |
Andrey Ryabinin | c420f16 | 2015-02-13 14:39:59 -0800 | [diff] [blame] | 92 | break; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Andrey Konovalov | 25add7e | 2015-11-05 18:51:03 -0800 | [diff] [blame] | 95 | pr_err("BUG: KASAN: %s in %pS at addr %p\n", |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 96 | bug_type, (void *)info->ip, |
| 97 | info->access_addr); |
| 98 | pr_err("%s of size %zu by task %s/%d\n", |
| 99 | info->is_write ? "Write" : "Read", |
| 100 | info->access_size, current->comm, task_pid_nr(current)); |
| 101 | } |
| 102 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 103 | static inline bool kernel_or_module_addr(const void *addr) |
| 104 | { |
Aneesh Kumar K.V | 527f215 | 2015-11-05 18:50:46 -0800 | [diff] [blame] | 105 | if (addr >= (void *)_stext && addr < (void *)_end) |
| 106 | return true; |
| 107 | if (is_module_address((unsigned long)addr)) |
| 108 | return true; |
| 109 | return false; |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static inline bool init_task_stack_addr(const void *addr) |
| 113 | { |
| 114 | return addr >= (void *)&init_thread_union.stack && |
| 115 | (addr <= (void *)&init_thread_union.stack + |
| 116 | sizeof(init_thread_union.stack)); |
| 117 | } |
| 118 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 119 | #ifdef CONFIG_SLAB |
| 120 | static void print_track(struct kasan_track *track) |
| 121 | { |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 122 | pr_err("PID = %u\n", track->pid); |
| 123 | if (track->stack) { |
| 124 | struct stack_trace trace; |
| 125 | |
| 126 | depot_fetch_stack(track->stack, &trace); |
| 127 | print_stack_trace(&trace, 0); |
| 128 | } else { |
| 129 | pr_err("(stack is not available)\n"); |
| 130 | } |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static void object_err(struct kmem_cache *cache, struct page *page, |
| 134 | void *object, char *unused_reason) |
| 135 | { |
| 136 | struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object); |
| 137 | struct kasan_free_meta *free_info; |
| 138 | |
| 139 | dump_stack(); |
| 140 | pr_err("Object at %p, in cache %s\n", object, cache->name); |
| 141 | if (!(cache->flags & SLAB_KASAN)) |
| 142 | return; |
| 143 | switch (alloc_info->state) { |
| 144 | case KASAN_STATE_INIT: |
| 145 | pr_err("Object not allocated yet\n"); |
| 146 | break; |
| 147 | case KASAN_STATE_ALLOC: |
| 148 | pr_err("Object allocated with size %u bytes.\n", |
| 149 | alloc_info->alloc_size); |
| 150 | pr_err("Allocation:\n"); |
| 151 | print_track(&alloc_info->track); |
| 152 | break; |
| 153 | case KASAN_STATE_FREE: |
| 154 | pr_err("Object freed, allocated with size %u bytes\n", |
| 155 | alloc_info->alloc_size); |
| 156 | free_info = get_free_info(cache, object); |
| 157 | pr_err("Allocation:\n"); |
| 158 | print_track(&alloc_info->track); |
| 159 | pr_err("Deallocation:\n"); |
| 160 | print_track(&free_info->track); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | #endif |
| 165 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 166 | static void print_address_description(struct kasan_access_info *info) |
| 167 | { |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 168 | const void *addr = info->access_addr; |
| 169 | |
| 170 | if ((addr >= (void *)PAGE_OFFSET) && |
| 171 | (addr < high_memory)) { |
| 172 | struct page *page = virt_to_head_page(addr); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 173 | |
| 174 | if (PageSlab(page)) { |
| 175 | void *object; |
| 176 | struct kmem_cache *cache = page->slab_cache; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 177 | object = nearest_obj(cache, page, |
| 178 | (void *)info->access_addr); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 179 | object_err(cache, page, object, |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 180 | "kasan: bad access detected"); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 181 | return; |
| 182 | } |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 183 | dump_page(page, "kasan: bad access detected"); |
| 184 | } |
| 185 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 186 | if (kernel_or_module_addr(addr)) { |
| 187 | if (!init_task_stack_addr(addr)) |
| 188 | pr_err("Address belongs to variable %pS\n", addr); |
| 189 | } |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 190 | dump_stack(); |
| 191 | } |
| 192 | |
| 193 | static bool row_is_guilty(const void *row, const void *guilty) |
| 194 | { |
| 195 | return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW); |
| 196 | } |
| 197 | |
| 198 | static int shadow_pointer_offset(const void *row, const void *shadow) |
| 199 | { |
| 200 | /* The length of ">ff00ff00ff00ff00: " is |
| 201 | * 3 + (BITS_PER_LONG/8)*2 chars. |
| 202 | */ |
| 203 | return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 + |
| 204 | (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1; |
| 205 | } |
| 206 | |
| 207 | static void print_shadow_for_address(const void *addr) |
| 208 | { |
| 209 | int i; |
| 210 | const void *shadow = kasan_mem_to_shadow(addr); |
| 211 | const void *shadow_row; |
| 212 | |
| 213 | shadow_row = (void *)round_down((unsigned long)shadow, |
| 214 | SHADOW_BYTES_PER_ROW) |
| 215 | - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW; |
| 216 | |
| 217 | pr_err("Memory state around the buggy address:\n"); |
| 218 | |
| 219 | for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) { |
| 220 | const void *kaddr = kasan_shadow_to_mem(shadow_row); |
| 221 | char buffer[4 + (BITS_PER_LONG/8)*2]; |
Aneesh Kumar K.V | f2377d4 | 2015-11-05 18:50:48 -0800 | [diff] [blame] | 222 | char shadow_buf[SHADOW_BYTES_PER_ROW]; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 223 | |
| 224 | snprintf(buffer, sizeof(buffer), |
| 225 | (i == 0) ? ">%p: " : " %p: ", kaddr); |
Aneesh Kumar K.V | f2377d4 | 2015-11-05 18:50:48 -0800 | [diff] [blame] | 226 | /* |
| 227 | * We should not pass a shadow pointer to generic |
| 228 | * function, because generic functions may try to |
| 229 | * access kasan mapping for the passed address. |
| 230 | */ |
Aneesh Kumar K.V | f2377d4 | 2015-11-05 18:50:48 -0800 | [diff] [blame] | 231 | memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 232 | print_hex_dump(KERN_ERR, buffer, |
| 233 | DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1, |
Aneesh Kumar K.V | f2377d4 | 2015-11-05 18:50:48 -0800 | [diff] [blame] | 234 | shadow_buf, SHADOW_BYTES_PER_ROW, 0); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 235 | |
| 236 | if (row_is_guilty(shadow_row, shadow)) |
| 237 | pr_err("%*c\n", |
| 238 | shadow_pointer_offset(shadow_row, shadow), |
| 239 | '^'); |
| 240 | |
| 241 | shadow_row += SHADOW_BYTES_PER_ROW; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | static DEFINE_SPINLOCK(report_lock); |
| 246 | |
Andrey Konovalov | e9121076 | 2015-11-05 18:50:55 -0800 | [diff] [blame] | 247 | static void kasan_report_error(struct kasan_access_info *info) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 248 | { |
| 249 | unsigned long flags; |
Andrey Konovalov | e9121076 | 2015-11-05 18:50:55 -0800 | [diff] [blame] | 250 | const char *bug_type; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 251 | |
Aneesh Kumar K.V | fc5aeea | 2015-11-05 18:50:51 -0800 | [diff] [blame] | 252 | /* |
| 253 | * Make sure we don't end up in loop. |
| 254 | */ |
| 255 | kasan_disable_current(); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 256 | spin_lock_irqsave(&report_lock, flags); |
Joe Perches | 756a025 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 257 | pr_err("==================================================================\n"); |
Andrey Konovalov | e9121076 | 2015-11-05 18:50:55 -0800 | [diff] [blame] | 258 | if (info->access_addr < |
| 259 | kasan_shadow_to_mem((void *)KASAN_SHADOW_START)) { |
| 260 | if ((unsigned long)info->access_addr < PAGE_SIZE) |
| 261 | bug_type = "null-ptr-deref"; |
| 262 | else if ((unsigned long)info->access_addr < TASK_SIZE) |
| 263 | bug_type = "user-memory-access"; |
| 264 | else |
| 265 | bug_type = "wild-memory-access"; |
Andrey Konovalov | 25add7e | 2015-11-05 18:51:03 -0800 | [diff] [blame] | 266 | pr_err("BUG: KASAN: %s on address %p\n", |
Andrey Konovalov | e9121076 | 2015-11-05 18:50:55 -0800 | [diff] [blame] | 267 | bug_type, info->access_addr); |
| 268 | pr_err("%s of size %zu by task %s/%d\n", |
| 269 | info->is_write ? "Write" : "Read", |
| 270 | info->access_size, current->comm, |
| 271 | task_pid_nr(current)); |
| 272 | dump_stack(); |
| 273 | } else { |
| 274 | print_error_description(info); |
| 275 | print_address_description(info); |
| 276 | print_shadow_for_address(info->first_bad_addr); |
| 277 | } |
Joe Perches | 756a025 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 278 | pr_err("==================================================================\n"); |
Andrey Ryabinin | eb06f43 | 2015-11-05 18:51:26 -0800 | [diff] [blame] | 279 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 280 | spin_unlock_irqrestore(&report_lock, flags); |
Aneesh Kumar K.V | fc5aeea | 2015-11-05 18:50:51 -0800 | [diff] [blame] | 281 | kasan_enable_current(); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | void kasan_report(unsigned long addr, size_t size, |
| 285 | bool is_write, unsigned long ip) |
| 286 | { |
| 287 | struct kasan_access_info info; |
| 288 | |
Aneesh Kumar K.V | 0ba8663 | 2015-11-05 18:50:43 -0800 | [diff] [blame] | 289 | if (likely(!kasan_report_enabled())) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 290 | return; |
| 291 | |
| 292 | info.access_addr = (void *)addr; |
| 293 | info.access_size = size; |
| 294 | info.is_write = is_write; |
| 295 | info.ip = ip; |
Andrey Konovalov | e9121076 | 2015-11-05 18:50:55 -0800 | [diff] [blame] | 296 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 297 | kasan_report_error(&info); |
| 298 | } |
| 299 | |
| 300 | |
| 301 | #define DEFINE_ASAN_REPORT_LOAD(size) \ |
| 302 | void __asan_report_load##size##_noabort(unsigned long addr) \ |
| 303 | { \ |
| 304 | kasan_report(addr, size, false, _RET_IP_); \ |
| 305 | } \ |
| 306 | EXPORT_SYMBOL(__asan_report_load##size##_noabort) |
| 307 | |
| 308 | #define DEFINE_ASAN_REPORT_STORE(size) \ |
| 309 | void __asan_report_store##size##_noabort(unsigned long addr) \ |
| 310 | { \ |
| 311 | kasan_report(addr, size, true, _RET_IP_); \ |
| 312 | } \ |
| 313 | EXPORT_SYMBOL(__asan_report_store##size##_noabort) |
| 314 | |
| 315 | DEFINE_ASAN_REPORT_LOAD(1); |
| 316 | DEFINE_ASAN_REPORT_LOAD(2); |
| 317 | DEFINE_ASAN_REPORT_LOAD(4); |
| 318 | DEFINE_ASAN_REPORT_LOAD(8); |
| 319 | DEFINE_ASAN_REPORT_LOAD(16); |
| 320 | DEFINE_ASAN_REPORT_STORE(1); |
| 321 | DEFINE_ASAN_REPORT_STORE(2); |
| 322 | DEFINE_ASAN_REPORT_STORE(4); |
| 323 | DEFINE_ASAN_REPORT_STORE(8); |
| 324 | DEFINE_ASAN_REPORT_STORE(16); |
| 325 | |
| 326 | void __asan_report_load_n_noabort(unsigned long addr, size_t size) |
| 327 | { |
| 328 | kasan_report(addr, size, false, _RET_IP_); |
| 329 | } |
| 330 | EXPORT_SYMBOL(__asan_report_load_n_noabort); |
| 331 | |
| 332 | void __asan_report_store_n_noabort(unsigned long addr, size_t size) |
| 333 | { |
| 334 | kasan_report(addr, size, true, _RET_IP_); |
| 335 | } |
| 336 | EXPORT_SYMBOL(__asan_report_store_n_noabort); |