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 | |
Andrey Konovalov | 9316eb7 | 2017-05-03 14:56:25 -0700 | [diff] [blame] | 51 | static bool addr_has_shadow(struct kasan_access_info *info) |
| 52 | { |
| 53 | return (info->access_addr >= |
| 54 | kasan_shadow_to_mem((void *)KASAN_SHADOW_START)); |
| 55 | } |
| 56 | |
| 57 | static const char *get_shadow_bug_type(struct kasan_access_info *info) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 58 | { |
Andrey Konovalov | 0952d87 | 2015-11-05 18:50:58 -0800 | [diff] [blame] | 59 | const char *bug_type = "unknown-crash"; |
Andrey Konovalov | cdf6a27 | 2015-11-05 18:51:01 -0800 | [diff] [blame] | 60 | u8 *shadow_addr; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 61 | |
| 62 | info->first_bad_addr = find_first_bad_addr(info->access_addr, |
| 63 | info->access_size); |
| 64 | |
Andrey Konovalov | cdf6a27 | 2015-11-05 18:51:01 -0800 | [diff] [blame] | 65 | shadow_addr = (u8 *)kasan_mem_to_shadow(info->first_bad_addr); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 66 | |
Andrey Konovalov | cdf6a27 | 2015-11-05 18:51:01 -0800 | [diff] [blame] | 67 | /* |
| 68 | * If shadow byte value is in [0, KASAN_SHADOW_SCALE_SIZE) we can look |
| 69 | * at the next shadow byte to determine the type of the bad access. |
| 70 | */ |
| 71 | if (*shadow_addr > 0 && *shadow_addr <= KASAN_SHADOW_SCALE_SIZE - 1) |
| 72 | shadow_addr++; |
| 73 | |
| 74 | switch (*shadow_addr) { |
Andrey Konovalov | 0952d87 | 2015-11-05 18:50:58 -0800 | [diff] [blame] | 75 | case 0 ... KASAN_SHADOW_SCALE_SIZE - 1: |
Andrey Konovalov | cdf6a27 | 2015-11-05 18:51:01 -0800 | [diff] [blame] | 76 | /* |
| 77 | * In theory it's still possible to see these shadow values |
| 78 | * due to a data race in the kernel code. |
| 79 | */ |
Andrey Konovalov | 0952d87 | 2015-11-05 18:50:58 -0800 | [diff] [blame] | 80 | bug_type = "out-of-bounds"; |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 81 | break; |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 82 | case KASAN_PAGE_REDZONE: |
| 83 | case KASAN_KMALLOC_REDZONE: |
Andrey Konovalov | 0952d87 | 2015-11-05 18:50:58 -0800 | [diff] [blame] | 84 | bug_type = "slab-out-of-bounds"; |
| 85 | break; |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 86 | case KASAN_GLOBAL_REDZONE: |
Andrey Konovalov | 0952d87 | 2015-11-05 18:50:58 -0800 | [diff] [blame] | 87 | bug_type = "global-out-of-bounds"; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 88 | break; |
Andrey Ryabinin | c420f16 | 2015-02-13 14:39:59 -0800 | [diff] [blame] | 89 | case KASAN_STACK_LEFT: |
| 90 | case KASAN_STACK_MID: |
| 91 | case KASAN_STACK_RIGHT: |
| 92 | case KASAN_STACK_PARTIAL: |
Andrey Konovalov | 0952d87 | 2015-11-05 18:50:58 -0800 | [diff] [blame] | 93 | bug_type = "stack-out-of-bounds"; |
| 94 | break; |
| 95 | case KASAN_FREE_PAGE: |
| 96 | case KASAN_KMALLOC_FREE: |
| 97 | bug_type = "use-after-free"; |
Andrey Ryabinin | c420f16 | 2015-02-13 14:39:59 -0800 | [diff] [blame] | 98 | break; |
Dmitry Vyukov | 828347f | 2016-11-30 15:54:16 -0800 | [diff] [blame] | 99 | case KASAN_USE_AFTER_SCOPE: |
| 100 | bug_type = "use-after-scope"; |
| 101 | break; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Andrey Konovalov | 9316eb7 | 2017-05-03 14:56:25 -0700 | [diff] [blame] | 104 | return bug_type; |
| 105 | } |
| 106 | |
| 107 | const char *get_wild_bug_type(struct kasan_access_info *info) |
| 108 | { |
| 109 | const char *bug_type = "unknown-crash"; |
| 110 | |
| 111 | if ((unsigned long)info->access_addr < PAGE_SIZE) |
| 112 | bug_type = "null-ptr-deref"; |
| 113 | else if ((unsigned long)info->access_addr < TASK_SIZE) |
| 114 | bug_type = "user-memory-access"; |
| 115 | else |
| 116 | bug_type = "wild-memory-access"; |
| 117 | |
| 118 | return bug_type; |
| 119 | } |
| 120 | |
Andrey Konovalov | 294a486 | 2017-05-03 14:56:28 -0700 | [diff] [blame] | 121 | static const char *get_bug_type(struct kasan_access_info *info) |
| 122 | { |
| 123 | if (addr_has_shadow(info)) |
| 124 | return get_shadow_bug_type(info); |
| 125 | return get_wild_bug_type(info); |
| 126 | } |
| 127 | |
Andrey Konovalov | 9316eb7 | 2017-05-03 14:56:25 -0700 | [diff] [blame] | 128 | static void print_error_description(struct kasan_access_info *info) |
| 129 | { |
Andrey Konovalov | 294a486 | 2017-05-03 14:56:28 -0700 | [diff] [blame] | 130 | const char *bug_type = get_bug_type(info); |
Andrey Konovalov | 9316eb7 | 2017-05-03 14:56:25 -0700 | [diff] [blame] | 131 | |
Andrey Konovalov | f7bf9a2 | 2017-05-03 14:56:38 -0700 | [diff] [blame] | 132 | pr_err("BUG: KASAN: %s in %pS\n", |
| 133 | bug_type, (void *)info->ip); |
| 134 | pr_err("%s of size %zu at addr %p by task %s/%d\n", |
Andrey Konovalov | 294a486 | 2017-05-03 14:56:28 -0700 | [diff] [blame] | 135 | info->is_write ? "Write" : "Read", info->access_size, |
Andrey Konovalov | f7bf9a2 | 2017-05-03 14:56:38 -0700 | [diff] [blame] | 136 | info->access_addr, current->comm, task_pid_nr(current)); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 139 | static inline bool kernel_or_module_addr(const void *addr) |
| 140 | { |
Aneesh Kumar K.V | 527f215 | 2015-11-05 18:50:46 -0800 | [diff] [blame] | 141 | if (addr >= (void *)_stext && addr < (void *)_end) |
| 142 | return true; |
| 143 | if (is_module_address((unsigned long)addr)) |
| 144 | return true; |
| 145 | return false; |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static inline bool init_task_stack_addr(const void *addr) |
| 149 | { |
| 150 | return addr >= (void *)&init_thread_union.stack && |
| 151 | (addr <= (void *)&init_thread_union.stack + |
| 152 | sizeof(init_thread_union.stack)); |
| 153 | } |
| 154 | |
Andrey Ryabinin | 7e08897 | 2016-08-02 14:02:55 -0700 | [diff] [blame] | 155 | static DEFINE_SPINLOCK(report_lock); |
| 156 | |
| 157 | static void kasan_start_report(unsigned long *flags) |
| 158 | { |
| 159 | /* |
| 160 | * Make sure we don't end up in loop. |
| 161 | */ |
| 162 | kasan_disable_current(); |
| 163 | spin_lock_irqsave(&report_lock, *flags); |
| 164 | pr_err("==================================================================\n"); |
| 165 | } |
| 166 | |
| 167 | static void kasan_end_report(unsigned long *flags) |
| 168 | { |
| 169 | pr_err("==================================================================\n"); |
| 170 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
| 171 | spin_unlock_irqrestore(&report_lock, *flags); |
| 172 | kasan_enable_current(); |
| 173 | } |
| 174 | |
Andrey Konovalov | e6275c4 | 2017-05-03 14:56:31 -0700 | [diff] [blame] | 175 | static void print_track(struct kasan_track *track, const char *prefix) |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 176 | { |
Andrey Konovalov | e6275c4 | 2017-05-03 14:56:31 -0700 | [diff] [blame] | 177 | pr_err("%s by task %u:\n", prefix, track->pid); |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 178 | if (track->stack) { |
| 179 | struct stack_trace trace; |
| 180 | |
| 181 | depot_fetch_stack(track->stack, &trace); |
| 182 | print_stack_trace(&trace, 0); |
| 183 | } else { |
| 184 | pr_err("(stack is not available)\n"); |
| 185 | } |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Andrey Konovalov | 047bc04 | 2017-05-03 14:56:34 -0700 | [diff] [blame] | 188 | static struct page *addr_to_page(const void *addr) |
| 189 | { |
| 190 | if ((addr >= (void *)PAGE_OFFSET) && |
| 191 | (addr < high_memory)) |
| 192 | return virt_to_head_page(addr); |
| 193 | return NULL; |
| 194 | } |
| 195 | |
Andrey Konovalov | 1be2518 | 2017-05-03 14:56:41 -0700 | [diff] [blame] | 196 | static void describe_object_addr(struct kmem_cache *cache, void *object, |
| 197 | const void *addr) |
| 198 | { |
| 199 | unsigned long access_addr = (unsigned long)addr; |
| 200 | unsigned long object_addr = (unsigned long)object; |
| 201 | const char *rel_type; |
| 202 | int rel_bytes; |
| 203 | |
| 204 | pr_err("The buggy address belongs to the object at %p\n" |
| 205 | " which belongs to the cache %s of size %d\n", |
| 206 | object, cache->name, cache->object_size); |
| 207 | |
| 208 | if (!addr) |
| 209 | return; |
| 210 | |
| 211 | if (access_addr < object_addr) { |
| 212 | rel_type = "to the left"; |
| 213 | rel_bytes = object_addr - access_addr; |
| 214 | } else if (access_addr >= object_addr + cache->object_size) { |
| 215 | rel_type = "to the right"; |
| 216 | rel_bytes = access_addr - (object_addr + cache->object_size); |
| 217 | } else { |
| 218 | rel_type = "inside"; |
| 219 | rel_bytes = access_addr - object_addr; |
| 220 | } |
| 221 | |
| 222 | pr_err("The buggy address is located %d bytes %s of\n" |
| 223 | " %d-byte region [%p, %p)\n", |
| 224 | rel_bytes, rel_type, cache->object_size, (void *)object_addr, |
| 225 | (void *)(object_addr + cache->object_size)); |
| 226 | } |
| 227 | |
| 228 | static void describe_object(struct kmem_cache *cache, void *object, |
| 229 | const void *addr) |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 230 | { |
| 231 | struct kasan_alloc_meta *alloc_info = get_alloc_info(cache, object); |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 232 | |
Andrey Konovalov | 1be2518 | 2017-05-03 14:56:41 -0700 | [diff] [blame] | 233 | if (cache->flags & SLAB_KASAN) { |
| 234 | print_track(&alloc_info->alloc_track, "Allocated"); |
Andrey Konovalov | 376eb6e | 2017-05-03 14:56:50 -0700 | [diff] [blame^] | 235 | pr_err("\n"); |
Andrey Konovalov | 1be2518 | 2017-05-03 14:56:41 -0700 | [diff] [blame] | 236 | print_track(&alloc_info->free_track, "Freed"); |
Andrey Konovalov | 376eb6e | 2017-05-03 14:56:50 -0700 | [diff] [blame^] | 237 | pr_err("\n"); |
Andrey Konovalov | 1be2518 | 2017-05-03 14:56:41 -0700 | [diff] [blame] | 238 | } |
Andrey Ryabinin | 47b5c2a | 2016-08-02 14:02:49 -0700 | [diff] [blame] | 239 | |
Andrey Konovalov | 1be2518 | 2017-05-03 14:56:41 -0700 | [diff] [blame] | 240 | describe_object_addr(cache, object, addr); |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 241 | } |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 242 | |
Andrey Konovalov | dcbca17 | 2017-05-03 14:56:47 -0700 | [diff] [blame] | 243 | static void print_address_description(void *addr) |
Andrey Ryabinin | 7e08897 | 2016-08-02 14:02:55 -0700 | [diff] [blame] | 244 | { |
Andrey Konovalov | 047bc04 | 2017-05-03 14:56:34 -0700 | [diff] [blame] | 245 | struct page *page = addr_to_page(addr); |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 246 | |
Andrey Konovalov | 047bc04 | 2017-05-03 14:56:34 -0700 | [diff] [blame] | 247 | dump_stack(); |
Andrey Konovalov | 376eb6e | 2017-05-03 14:56:50 -0700 | [diff] [blame^] | 248 | pr_err("\n"); |
Andrey Konovalov | 047bc04 | 2017-05-03 14:56:34 -0700 | [diff] [blame] | 249 | |
| 250 | if (page && PageSlab(page)) { |
| 251 | struct kmem_cache *cache = page->slab_cache; |
Andrey Konovalov | 1be2518 | 2017-05-03 14:56:41 -0700 | [diff] [blame] | 252 | void *object = nearest_obj(cache, page, addr); |
Andrey Konovalov | 047bc04 | 2017-05-03 14:56:34 -0700 | [diff] [blame] | 253 | |
Andrey Konovalov | 1be2518 | 2017-05-03 14:56:41 -0700 | [diff] [blame] | 254 | describe_object(cache, object, addr); |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Andrey Konovalov | 95b3616 | 2017-05-03 14:56:44 -0700 | [diff] [blame] | 257 | if (kernel_or_module_addr(addr) && !init_task_stack_addr(addr)) { |
| 258 | pr_err("The buggy address belongs to the variable:\n"); |
| 259 | pr_err(" %pS\n", addr); |
| 260 | } |
| 261 | |
| 262 | if (page) { |
| 263 | pr_err("The buggy address belongs to the page:\n"); |
| 264 | dump_page(page, "kasan: bad access detected"); |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 265 | } |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static bool row_is_guilty(const void *row, const void *guilty) |
| 269 | { |
| 270 | return (row <= guilty) && (guilty < row + SHADOW_BYTES_PER_ROW); |
| 271 | } |
| 272 | |
| 273 | static int shadow_pointer_offset(const void *row, const void *shadow) |
| 274 | { |
| 275 | /* The length of ">ff00ff00ff00ff00: " is |
| 276 | * 3 + (BITS_PER_LONG/8)*2 chars. |
| 277 | */ |
| 278 | return 3 + (BITS_PER_LONG/8)*2 + (shadow - row)*2 + |
| 279 | (shadow - row) / SHADOW_BYTES_PER_BLOCK + 1; |
| 280 | } |
| 281 | |
| 282 | static void print_shadow_for_address(const void *addr) |
| 283 | { |
| 284 | int i; |
| 285 | const void *shadow = kasan_mem_to_shadow(addr); |
| 286 | const void *shadow_row; |
| 287 | |
| 288 | shadow_row = (void *)round_down((unsigned long)shadow, |
| 289 | SHADOW_BYTES_PER_ROW) |
| 290 | - SHADOW_ROWS_AROUND_ADDR * SHADOW_BYTES_PER_ROW; |
| 291 | |
| 292 | pr_err("Memory state around the buggy address:\n"); |
| 293 | |
| 294 | for (i = -SHADOW_ROWS_AROUND_ADDR; i <= SHADOW_ROWS_AROUND_ADDR; i++) { |
| 295 | const void *kaddr = kasan_shadow_to_mem(shadow_row); |
| 296 | char buffer[4 + (BITS_PER_LONG/8)*2]; |
Aneesh Kumar K.V | f2377d4 | 2015-11-05 18:50:48 -0800 | [diff] [blame] | 297 | char shadow_buf[SHADOW_BYTES_PER_ROW]; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 298 | |
| 299 | snprintf(buffer, sizeof(buffer), |
| 300 | (i == 0) ? ">%p: " : " %p: ", kaddr); |
Aneesh Kumar K.V | f2377d4 | 2015-11-05 18:50:48 -0800 | [diff] [blame] | 301 | /* |
| 302 | * We should not pass a shadow pointer to generic |
| 303 | * function, because generic functions may try to |
| 304 | * access kasan mapping for the passed address. |
| 305 | */ |
Aneesh Kumar K.V | f2377d4 | 2015-11-05 18:50:48 -0800 | [diff] [blame] | 306 | memcpy(shadow_buf, shadow_row, SHADOW_BYTES_PER_ROW); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 307 | print_hex_dump(KERN_ERR, buffer, |
| 308 | DUMP_PREFIX_NONE, SHADOW_BYTES_PER_ROW, 1, |
Aneesh Kumar K.V | f2377d4 | 2015-11-05 18:50:48 -0800 | [diff] [blame] | 309 | shadow_buf, SHADOW_BYTES_PER_ROW, 0); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 310 | |
| 311 | if (row_is_guilty(shadow_row, shadow)) |
| 312 | pr_err("%*c\n", |
| 313 | shadow_pointer_offset(shadow_row, shadow), |
| 314 | '^'); |
| 315 | |
| 316 | shadow_row += SHADOW_BYTES_PER_ROW; |
| 317 | } |
| 318 | } |
| 319 | |
Andrey Konovalov | dcbca17 | 2017-05-03 14:56:47 -0700 | [diff] [blame] | 320 | void kasan_report_double_free(struct kmem_cache *cache, void *object, |
| 321 | void *ip) |
| 322 | { |
| 323 | unsigned long flags; |
| 324 | |
| 325 | kasan_start_report(&flags); |
| 326 | pr_err("BUG: KASAN: double-free or invalid-free in %pS\n", ip); |
Andrey Konovalov | 376eb6e | 2017-05-03 14:56:50 -0700 | [diff] [blame^] | 327 | pr_err("\n"); |
Andrey Konovalov | dcbca17 | 2017-05-03 14:56:47 -0700 | [diff] [blame] | 328 | print_address_description(object); |
Andrey Konovalov | 376eb6e | 2017-05-03 14:56:50 -0700 | [diff] [blame^] | 329 | pr_err("\n"); |
Andrey Konovalov | dcbca17 | 2017-05-03 14:56:47 -0700 | [diff] [blame] | 330 | print_shadow_for_address(object); |
| 331 | kasan_end_report(&flags); |
| 332 | } |
| 333 | |
Andrey Konovalov | e9121076 | 2015-11-05 18:50:55 -0800 | [diff] [blame] | 334 | static void kasan_report_error(struct kasan_access_info *info) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 335 | { |
| 336 | unsigned long flags; |
| 337 | |
Andrey Ryabinin | 7e08897 | 2016-08-02 14:02:55 -0700 | [diff] [blame] | 338 | kasan_start_report(&flags); |
| 339 | |
Andrey Konovalov | 294a486 | 2017-05-03 14:56:28 -0700 | [diff] [blame] | 340 | print_error_description(info); |
Andrey Konovalov | 376eb6e | 2017-05-03 14:56:50 -0700 | [diff] [blame^] | 341 | pr_err("\n"); |
Andrey Konovalov | 294a486 | 2017-05-03 14:56:28 -0700 | [diff] [blame] | 342 | |
Andrey Konovalov | 9316eb7 | 2017-05-03 14:56:25 -0700 | [diff] [blame] | 343 | if (!addr_has_shadow(info)) { |
Andrey Konovalov | e9121076 | 2015-11-05 18:50:55 -0800 | [diff] [blame] | 344 | dump_stack(); |
| 345 | } else { |
Andrey Konovalov | dcbca17 | 2017-05-03 14:56:47 -0700 | [diff] [blame] | 346 | print_address_description((void *)info->access_addr); |
Andrey Konovalov | 376eb6e | 2017-05-03 14:56:50 -0700 | [diff] [blame^] | 347 | pr_err("\n"); |
Andrey Konovalov | e9121076 | 2015-11-05 18:50:55 -0800 | [diff] [blame] | 348 | print_shadow_for_address(info->first_bad_addr); |
| 349 | } |
Andrey Ryabinin | 7e08897 | 2016-08-02 14:02:55 -0700 | [diff] [blame] | 350 | |
| 351 | kasan_end_report(&flags); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void kasan_report(unsigned long addr, size_t size, |
| 355 | bool is_write, unsigned long ip) |
| 356 | { |
| 357 | struct kasan_access_info info; |
| 358 | |
Aneesh Kumar K.V | 0ba8663 | 2015-11-05 18:50:43 -0800 | [diff] [blame] | 359 | if (likely(!kasan_report_enabled())) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 360 | return; |
| 361 | |
| 362 | info.access_addr = (void *)addr; |
| 363 | info.access_size = size; |
| 364 | info.is_write = is_write; |
| 365 | info.ip = ip; |
Andrey Konovalov | e9121076 | 2015-11-05 18:50:55 -0800 | [diff] [blame] | 366 | |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 367 | kasan_report_error(&info); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | #define DEFINE_ASAN_REPORT_LOAD(size) \ |
| 372 | void __asan_report_load##size##_noabort(unsigned long addr) \ |
| 373 | { \ |
| 374 | kasan_report(addr, size, false, _RET_IP_); \ |
| 375 | } \ |
| 376 | EXPORT_SYMBOL(__asan_report_load##size##_noabort) |
| 377 | |
| 378 | #define DEFINE_ASAN_REPORT_STORE(size) \ |
| 379 | void __asan_report_store##size##_noabort(unsigned long addr) \ |
| 380 | { \ |
| 381 | kasan_report(addr, size, true, _RET_IP_); \ |
| 382 | } \ |
| 383 | EXPORT_SYMBOL(__asan_report_store##size##_noabort) |
| 384 | |
| 385 | DEFINE_ASAN_REPORT_LOAD(1); |
| 386 | DEFINE_ASAN_REPORT_LOAD(2); |
| 387 | DEFINE_ASAN_REPORT_LOAD(4); |
| 388 | DEFINE_ASAN_REPORT_LOAD(8); |
| 389 | DEFINE_ASAN_REPORT_LOAD(16); |
| 390 | DEFINE_ASAN_REPORT_STORE(1); |
| 391 | DEFINE_ASAN_REPORT_STORE(2); |
| 392 | DEFINE_ASAN_REPORT_STORE(4); |
| 393 | DEFINE_ASAN_REPORT_STORE(8); |
| 394 | DEFINE_ASAN_REPORT_STORE(16); |
| 395 | |
| 396 | void __asan_report_load_n_noabort(unsigned long addr, size_t size) |
| 397 | { |
| 398 | kasan_report(addr, size, false, _RET_IP_); |
| 399 | } |
| 400 | EXPORT_SYMBOL(__asan_report_load_n_noabort); |
| 401 | |
| 402 | void __asan_report_store_n_noabort(unsigned long addr, size_t size) |
| 403 | { |
| 404 | kasan_report(addr, size, true, _RET_IP_); |
| 405 | } |
| 406 | EXPORT_SYMBOL(__asan_report_store_n_noabort); |