Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file contains shadow memory manipulation 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 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 17 | #define DISABLE_BRANCH_PROFILING |
| 18 | |
| 19 | #include <linux/export.h> |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 21 | #include <linux/init.h> |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 22 | #include <linux/kasan.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 23 | #include <linux/kernel.h> |
Andrey Ryabinin | 4593725 | 2015-11-20 15:57:18 -0800 | [diff] [blame] | 24 | #include <linux/kmemleak.h> |
Mark Rutland | e3ae116 | 2016-03-09 14:08:15 -0800 | [diff] [blame] | 25 | #include <linux/linkage.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 26 | #include <linux/memblock.h> |
Andrey Ryabinin | 786a895 | 2015-02-13 14:39:21 -0800 | [diff] [blame] | 27 | #include <linux/memory.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 28 | #include <linux/mm.h> |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 29 | #include <linux/module.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 30 | #include <linux/printk.h> |
| 31 | #include <linux/sched.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/stacktrace.h> |
| 34 | #include <linux/string.h> |
| 35 | #include <linux/types.h> |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 36 | #include <linux/vmalloc.h> |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 37 | |
| 38 | #include "kasan.h" |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 39 | #include "../slab.h" |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * Poisons the shadow memory for 'size' bytes starting from 'addr'. |
| 43 | * Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE. |
| 44 | */ |
| 45 | static void kasan_poison_shadow(const void *address, size_t size, u8 value) |
| 46 | { |
| 47 | void *shadow_start, *shadow_end; |
| 48 | |
| 49 | shadow_start = kasan_mem_to_shadow(address); |
| 50 | shadow_end = kasan_mem_to_shadow(address + size); |
| 51 | |
| 52 | memset(shadow_start, value, shadow_end - shadow_start); |
| 53 | } |
| 54 | |
| 55 | void kasan_unpoison_shadow(const void *address, size_t size) |
| 56 | { |
| 57 | kasan_poison_shadow(address, size, 0); |
| 58 | |
| 59 | if (size & KASAN_SHADOW_MASK) { |
| 60 | u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size); |
| 61 | *shadow = size & KASAN_SHADOW_MASK; |
| 62 | } |
| 63 | } |
| 64 | |
Mark Rutland | e3ae116 | 2016-03-09 14:08:15 -0800 | [diff] [blame] | 65 | static void __kasan_unpoison_stack(struct task_struct *task, void *sp) |
| 66 | { |
| 67 | void *base = task_stack_page(task); |
| 68 | size_t size = sp - base; |
| 69 | |
| 70 | kasan_unpoison_shadow(base, size); |
| 71 | } |
| 72 | |
| 73 | /* Unpoison the entire stack for a task. */ |
| 74 | void kasan_unpoison_task_stack(struct task_struct *task) |
| 75 | { |
| 76 | __kasan_unpoison_stack(task, task_stack_page(task) + THREAD_SIZE); |
| 77 | } |
| 78 | |
| 79 | /* Unpoison the stack for the current task beyond a watermark sp value. */ |
| 80 | asmlinkage void kasan_unpoison_remaining_stack(void *sp) |
| 81 | { |
| 82 | __kasan_unpoison_stack(current, sp); |
| 83 | } |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * All functions below always inlined so compiler could |
| 87 | * perform better optimizations in each of __asan_loadX/__assn_storeX |
| 88 | * depending on memory access size X. |
| 89 | */ |
| 90 | |
| 91 | static __always_inline bool memory_is_poisoned_1(unsigned long addr) |
| 92 | { |
| 93 | s8 shadow_value = *(s8 *)kasan_mem_to_shadow((void *)addr); |
| 94 | |
| 95 | if (unlikely(shadow_value)) { |
| 96 | s8 last_accessible_byte = addr & KASAN_SHADOW_MASK; |
| 97 | return unlikely(last_accessible_byte >= shadow_value); |
| 98 | } |
| 99 | |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | static __always_inline bool memory_is_poisoned_2(unsigned long addr) |
| 104 | { |
| 105 | u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr); |
| 106 | |
| 107 | if (unlikely(*shadow_addr)) { |
| 108 | if (memory_is_poisoned_1(addr + 1)) |
| 109 | return true; |
| 110 | |
Xishi Qiu | 10f7026 | 2015-11-05 18:51:21 -0800 | [diff] [blame] | 111 | /* |
| 112 | * If single shadow byte covers 2-byte access, we don't |
| 113 | * need to do anything more. Otherwise, test the first |
| 114 | * shadow byte. |
| 115 | */ |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 116 | if (likely(((addr + 1) & KASAN_SHADOW_MASK) != 0)) |
| 117 | return false; |
| 118 | |
| 119 | return unlikely(*(u8 *)shadow_addr); |
| 120 | } |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | static __always_inline bool memory_is_poisoned_4(unsigned long addr) |
| 126 | { |
| 127 | u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr); |
| 128 | |
| 129 | if (unlikely(*shadow_addr)) { |
| 130 | if (memory_is_poisoned_1(addr + 3)) |
| 131 | return true; |
| 132 | |
Xishi Qiu | 10f7026 | 2015-11-05 18:51:21 -0800 | [diff] [blame] | 133 | /* |
| 134 | * If single shadow byte covers 4-byte access, we don't |
| 135 | * need to do anything more. Otherwise, test the first |
| 136 | * shadow byte. |
| 137 | */ |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 138 | if (likely(((addr + 3) & KASAN_SHADOW_MASK) >= 3)) |
| 139 | return false; |
| 140 | |
| 141 | return unlikely(*(u8 *)shadow_addr); |
| 142 | } |
| 143 | |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | static __always_inline bool memory_is_poisoned_8(unsigned long addr) |
| 148 | { |
| 149 | u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr); |
| 150 | |
| 151 | if (unlikely(*shadow_addr)) { |
| 152 | if (memory_is_poisoned_1(addr + 7)) |
| 153 | return true; |
| 154 | |
Xishi Qiu | 10f7026 | 2015-11-05 18:51:21 -0800 | [diff] [blame] | 155 | /* |
| 156 | * If single shadow byte covers 8-byte access, we don't |
| 157 | * need to do anything more. Otherwise, test the first |
| 158 | * shadow byte. |
| 159 | */ |
| 160 | if (likely(IS_ALIGNED(addr, KASAN_SHADOW_SCALE_SIZE))) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 161 | return false; |
| 162 | |
| 163 | return unlikely(*(u8 *)shadow_addr); |
| 164 | } |
| 165 | |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | static __always_inline bool memory_is_poisoned_16(unsigned long addr) |
| 170 | { |
| 171 | u32 *shadow_addr = (u32 *)kasan_mem_to_shadow((void *)addr); |
| 172 | |
| 173 | if (unlikely(*shadow_addr)) { |
| 174 | u16 shadow_first_bytes = *(u16 *)shadow_addr; |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 175 | |
| 176 | if (unlikely(shadow_first_bytes)) |
| 177 | return true; |
| 178 | |
Xishi Qiu | 10f7026 | 2015-11-05 18:51:21 -0800 | [diff] [blame] | 179 | /* |
| 180 | * If two shadow bytes covers 16-byte access, we don't |
| 181 | * need to do anything more. Otherwise, test the last |
| 182 | * shadow byte. |
| 183 | */ |
| 184 | if (likely(IS_ALIGNED(addr, KASAN_SHADOW_SCALE_SIZE))) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 185 | return false; |
| 186 | |
| 187 | return memory_is_poisoned_1(addr + 15); |
| 188 | } |
| 189 | |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | static __always_inline unsigned long bytes_is_zero(const u8 *start, |
| 194 | size_t size) |
| 195 | { |
| 196 | while (size) { |
| 197 | if (unlikely(*start)) |
| 198 | return (unsigned long)start; |
| 199 | start++; |
| 200 | size--; |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static __always_inline unsigned long memory_is_zero(const void *start, |
| 207 | const void *end) |
| 208 | { |
| 209 | unsigned int words; |
| 210 | unsigned long ret; |
| 211 | unsigned int prefix = (unsigned long)start % 8; |
| 212 | |
| 213 | if (end - start <= 16) |
| 214 | return bytes_is_zero(start, end - start); |
| 215 | |
| 216 | if (prefix) { |
| 217 | prefix = 8 - prefix; |
| 218 | ret = bytes_is_zero(start, prefix); |
| 219 | if (unlikely(ret)) |
| 220 | return ret; |
| 221 | start += prefix; |
| 222 | } |
| 223 | |
| 224 | words = (end - start) / 8; |
| 225 | while (words) { |
| 226 | if (unlikely(*(u64 *)start)) |
| 227 | return bytes_is_zero(start, 8); |
| 228 | start += 8; |
| 229 | words--; |
| 230 | } |
| 231 | |
| 232 | return bytes_is_zero(start, (end - start) % 8); |
| 233 | } |
| 234 | |
| 235 | static __always_inline bool memory_is_poisoned_n(unsigned long addr, |
| 236 | size_t size) |
| 237 | { |
| 238 | unsigned long ret; |
| 239 | |
| 240 | ret = memory_is_zero(kasan_mem_to_shadow((void *)addr), |
| 241 | kasan_mem_to_shadow((void *)addr + size - 1) + 1); |
| 242 | |
| 243 | if (unlikely(ret)) { |
| 244 | unsigned long last_byte = addr + size - 1; |
| 245 | s8 *last_shadow = (s8 *)kasan_mem_to_shadow((void *)last_byte); |
| 246 | |
| 247 | if (unlikely(ret != (unsigned long)last_shadow || |
Wang Long | e0d5771 | 2015-11-05 18:51:18 -0800 | [diff] [blame] | 248 | ((long)(last_byte & KASAN_SHADOW_MASK) >= *last_shadow))) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | static __always_inline bool memory_is_poisoned(unsigned long addr, size_t size) |
| 255 | { |
| 256 | if (__builtin_constant_p(size)) { |
| 257 | switch (size) { |
| 258 | case 1: |
| 259 | return memory_is_poisoned_1(addr); |
| 260 | case 2: |
| 261 | return memory_is_poisoned_2(addr); |
| 262 | case 4: |
| 263 | return memory_is_poisoned_4(addr); |
| 264 | case 8: |
| 265 | return memory_is_poisoned_8(addr); |
| 266 | case 16: |
| 267 | return memory_is_poisoned_16(addr); |
| 268 | default: |
| 269 | BUILD_BUG(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return memory_is_poisoned_n(addr, size); |
| 274 | } |
| 275 | |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 276 | static __always_inline void check_memory_region_inline(unsigned long addr, |
| 277 | size_t size, bool write, |
| 278 | unsigned long ret_ip) |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 279 | { |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 280 | if (unlikely(size == 0)) |
| 281 | return; |
| 282 | |
| 283 | if (unlikely((void *)addr < |
| 284 | kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) { |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 285 | kasan_report(addr, size, write, ret_ip); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 286 | return; |
| 287 | } |
| 288 | |
| 289 | if (likely(!memory_is_poisoned(addr, size))) |
| 290 | return; |
| 291 | |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 292 | kasan_report(addr, size, write, ret_ip); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 295 | static void check_memory_region(unsigned long addr, |
| 296 | size_t size, bool write, |
| 297 | unsigned long ret_ip) |
| 298 | { |
| 299 | check_memory_region_inline(addr, size, write, ret_ip); |
| 300 | } |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 301 | |
Andrey Ryabinin | 64f8eba | 2016-05-20 16:59:28 -0700 | [diff] [blame] | 302 | void kasan_check_read(const void *p, unsigned int size) |
| 303 | { |
| 304 | check_memory_region((unsigned long)p, size, false, _RET_IP_); |
| 305 | } |
| 306 | EXPORT_SYMBOL(kasan_check_read); |
| 307 | |
| 308 | void kasan_check_write(const void *p, unsigned int size) |
| 309 | { |
| 310 | check_memory_region((unsigned long)p, size, true, _RET_IP_); |
| 311 | } |
| 312 | EXPORT_SYMBOL(kasan_check_write); |
| 313 | |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 314 | #undef memset |
| 315 | void *memset(void *addr, int c, size_t len) |
| 316 | { |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 317 | check_memory_region((unsigned long)addr, len, true, _RET_IP_); |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 318 | |
| 319 | return __memset(addr, c, len); |
| 320 | } |
| 321 | |
| 322 | #undef memmove |
| 323 | void *memmove(void *dest, const void *src, size_t len) |
| 324 | { |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 325 | check_memory_region((unsigned long)src, len, false, _RET_IP_); |
| 326 | check_memory_region((unsigned long)dest, len, true, _RET_IP_); |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 327 | |
| 328 | return __memmove(dest, src, len); |
| 329 | } |
| 330 | |
| 331 | #undef memcpy |
| 332 | void *memcpy(void *dest, const void *src, size_t len) |
| 333 | { |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 334 | check_memory_region((unsigned long)src, len, false, _RET_IP_); |
| 335 | check_memory_region((unsigned long)dest, len, true, _RET_IP_); |
Andrey Ryabinin | 393f203 | 2015-02-13 14:39:56 -0800 | [diff] [blame] | 336 | |
| 337 | return __memcpy(dest, src, len); |
| 338 | } |
| 339 | |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 340 | void kasan_alloc_pages(struct page *page, unsigned int order) |
| 341 | { |
| 342 | if (likely(!PageHighMem(page))) |
| 343 | kasan_unpoison_shadow(page_address(page), PAGE_SIZE << order); |
| 344 | } |
| 345 | |
| 346 | void kasan_free_pages(struct page *page, unsigned int order) |
| 347 | { |
| 348 | if (likely(!PageHighMem(page))) |
| 349 | kasan_poison_shadow(page_address(page), |
| 350 | PAGE_SIZE << order, |
| 351 | KASAN_FREE_PAGE); |
| 352 | } |
| 353 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 354 | /* |
| 355 | * Adaptive redzone policy taken from the userspace AddressSanitizer runtime. |
| 356 | * For larger allocations larger redzones are used. |
| 357 | */ |
| 358 | static size_t optimal_redzone(size_t object_size) |
| 359 | { |
| 360 | int rz = |
| 361 | object_size <= 64 - 16 ? 16 : |
| 362 | object_size <= 128 - 32 ? 32 : |
| 363 | object_size <= 512 - 64 ? 64 : |
| 364 | object_size <= 4096 - 128 ? 128 : |
| 365 | object_size <= (1 << 14) - 256 ? 256 : |
| 366 | object_size <= (1 << 15) - 512 ? 512 : |
| 367 | object_size <= (1 << 16) - 1024 ? 1024 : 2048; |
| 368 | return rz; |
| 369 | } |
| 370 | |
| 371 | void kasan_cache_create(struct kmem_cache *cache, size_t *size, |
| 372 | unsigned long *flags) |
| 373 | { |
| 374 | int redzone_adjust; |
Alexander Potapenko | 80a9201 | 2016-07-28 15:49:07 -0700 | [diff] [blame] | 375 | int orig_size = *size; |
| 376 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 377 | /* Add alloc meta. */ |
| 378 | cache->kasan_info.alloc_meta_offset = *size; |
| 379 | *size += sizeof(struct kasan_alloc_meta); |
| 380 | |
| 381 | /* Add free meta. */ |
| 382 | if (cache->flags & SLAB_DESTROY_BY_RCU || cache->ctor || |
| 383 | cache->object_size < sizeof(struct kasan_free_meta)) { |
| 384 | cache->kasan_info.free_meta_offset = *size; |
| 385 | *size += sizeof(struct kasan_free_meta); |
| 386 | } |
| 387 | redzone_adjust = optimal_redzone(cache->object_size) - |
| 388 | (*size - cache->object_size); |
Alexander Potapenko | 80a9201 | 2016-07-28 15:49:07 -0700 | [diff] [blame] | 389 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 390 | if (redzone_adjust > 0) |
| 391 | *size += redzone_adjust; |
Alexander Potapenko | 80a9201 | 2016-07-28 15:49:07 -0700 | [diff] [blame] | 392 | |
| 393 | *size = min(KMALLOC_MAX_SIZE, max(*size, cache->object_size + |
| 394 | optimal_redzone(cache->object_size))); |
| 395 | |
| 396 | /* |
| 397 | * If the metadata doesn't fit, don't enable KASAN at all. |
| 398 | */ |
| 399 | if (*size <= cache->kasan_info.alloc_meta_offset || |
| 400 | *size <= cache->kasan_info.free_meta_offset) { |
| 401 | cache->kasan_info.alloc_meta_offset = 0; |
| 402 | cache->kasan_info.free_meta_offset = 0; |
| 403 | *size = orig_size; |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | *flags |= SLAB_KASAN; |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 408 | } |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 409 | |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 410 | void kasan_cache_shrink(struct kmem_cache *cache) |
| 411 | { |
| 412 | quarantine_remove_cache(cache); |
| 413 | } |
| 414 | |
| 415 | void kasan_cache_destroy(struct kmem_cache *cache) |
| 416 | { |
| 417 | quarantine_remove_cache(cache); |
| 418 | } |
| 419 | |
Alexander Potapenko | 80a9201 | 2016-07-28 15:49:07 -0700 | [diff] [blame] | 420 | size_t kasan_metadata_size(struct kmem_cache *cache) |
| 421 | { |
| 422 | return (cache->kasan_info.alloc_meta_offset ? |
| 423 | sizeof(struct kasan_alloc_meta) : 0) + |
| 424 | (cache->kasan_info.free_meta_offset ? |
| 425 | sizeof(struct kasan_free_meta) : 0); |
| 426 | } |
| 427 | |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 428 | void kasan_poison_slab(struct page *page) |
| 429 | { |
| 430 | kasan_poison_shadow(page_address(page), |
| 431 | PAGE_SIZE << compound_order(page), |
| 432 | KASAN_KMALLOC_REDZONE); |
| 433 | } |
| 434 | |
| 435 | void kasan_unpoison_object_data(struct kmem_cache *cache, void *object) |
| 436 | { |
| 437 | kasan_unpoison_shadow(object, cache->object_size); |
| 438 | } |
| 439 | |
| 440 | void kasan_poison_object_data(struct kmem_cache *cache, void *object) |
| 441 | { |
| 442 | kasan_poison_shadow(object, |
| 443 | round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE), |
| 444 | KASAN_KMALLOC_REDZONE); |
| 445 | } |
| 446 | |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 447 | static inline int in_irqentry_text(unsigned long ptr) |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 448 | { |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 449 | return (ptr >= (unsigned long)&__irqentry_text_start && |
| 450 | ptr < (unsigned long)&__irqentry_text_end) || |
| 451 | (ptr >= (unsigned long)&__softirqentry_text_start && |
| 452 | ptr < (unsigned long)&__softirqentry_text_end); |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 455 | static inline void filter_irq_stacks(struct stack_trace *trace) |
| 456 | { |
| 457 | int i; |
| 458 | |
| 459 | if (!trace->nr_entries) |
| 460 | return; |
| 461 | for (i = 0; i < trace->nr_entries; i++) |
| 462 | if (in_irqentry_text(trace->entries[i])) { |
| 463 | /* Include the irqentry function into the stack. */ |
| 464 | trace->nr_entries = i + 1; |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | static inline depot_stack_handle_t save_stack(gfp_t flags) |
| 470 | { |
| 471 | unsigned long entries[KASAN_STACK_DEPTH]; |
| 472 | struct stack_trace trace = { |
| 473 | .nr_entries = 0, |
| 474 | .entries = entries, |
| 475 | .max_entries = KASAN_STACK_DEPTH, |
| 476 | .skip = 0 |
| 477 | }; |
| 478 | |
| 479 | save_stack_trace(&trace); |
| 480 | filter_irq_stacks(&trace); |
| 481 | if (trace.nr_entries != 0 && |
| 482 | trace.entries[trace.nr_entries-1] == ULONG_MAX) |
| 483 | trace.nr_entries--; |
| 484 | |
| 485 | return depot_save_stack(&trace, flags); |
| 486 | } |
| 487 | |
| 488 | static inline void set_track(struct kasan_track *track, gfp_t flags) |
| 489 | { |
| 490 | track->pid = current->pid; |
| 491 | track->stack = save_stack(flags); |
| 492 | } |
| 493 | |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 494 | struct kasan_alloc_meta *get_alloc_info(struct kmem_cache *cache, |
| 495 | const void *object) |
| 496 | { |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 497 | BUILD_BUG_ON(sizeof(struct kasan_alloc_meta) > 32); |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 498 | return (void *)object + cache->kasan_info.alloc_meta_offset; |
| 499 | } |
| 500 | |
| 501 | struct kasan_free_meta *get_free_info(struct kmem_cache *cache, |
| 502 | const void *object) |
| 503 | { |
Alexander Potapenko | cd11016 | 2016-03-25 14:22:08 -0700 | [diff] [blame] | 504 | BUILD_BUG_ON(sizeof(struct kasan_free_meta) > 32); |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 505 | return (void *)object + cache->kasan_info.free_meta_offset; |
| 506 | } |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 507 | |
Andrey Ryabinin | b3cbd9b | 2016-08-02 14:02:52 -0700 | [diff] [blame] | 508 | void kasan_init_slab_obj(struct kmem_cache *cache, const void *object) |
| 509 | { |
| 510 | struct kasan_alloc_meta *alloc_info; |
| 511 | |
| 512 | if (!(cache->flags & SLAB_KASAN)) |
| 513 | return; |
| 514 | |
| 515 | alloc_info = get_alloc_info(cache, object); |
| 516 | __memset(alloc_info, 0, sizeof(*alloc_info)); |
| 517 | } |
| 518 | |
Alexander Potapenko | 505f5dc | 2016-03-25 14:22:02 -0700 | [diff] [blame] | 519 | void kasan_slab_alloc(struct kmem_cache *cache, void *object, gfp_t flags) |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 520 | { |
Alexander Potapenko | 505f5dc | 2016-03-25 14:22:02 -0700 | [diff] [blame] | 521 | kasan_kmalloc(cache, object, cache->object_size, flags); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Andrey Ryabinin | 9b75a86 | 2016-06-24 14:49:34 -0700 | [diff] [blame] | 524 | static void kasan_poison_slab_free(struct kmem_cache *cache, void *object) |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 525 | { |
| 526 | unsigned long size = cache->object_size; |
| 527 | unsigned long rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE); |
| 528 | |
| 529 | /* RCU slabs could be legally used after free within the RCU period */ |
| 530 | if (unlikely(cache->flags & SLAB_DESTROY_BY_RCU)) |
| 531 | return; |
| 532 | |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 533 | kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE); |
| 534 | } |
| 535 | |
| 536 | bool kasan_slab_free(struct kmem_cache *cache, void *object) |
| 537 | { |
Andrey Ryabinin | b3cbd9b | 2016-08-02 14:02:52 -0700 | [diff] [blame] | 538 | s8 shadow_byte; |
| 539 | |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 540 | /* RCU slabs could be legally used after free within the RCU period */ |
| 541 | if (unlikely(cache->flags & SLAB_DESTROY_BY_RCU)) |
| 542 | return false; |
| 543 | |
Andrey Ryabinin | b3cbd9b | 2016-08-02 14:02:52 -0700 | [diff] [blame] | 544 | shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(object)); |
| 545 | if (shadow_byte < 0 || shadow_byte >= KASAN_SHADOW_SCALE_SIZE) { |
Andrey Ryabinin | 7e08897 | 2016-08-02 14:02:55 -0700 | [diff] [blame] | 546 | kasan_report_double_free(cache, object, shadow_byte); |
Andrey Ryabinin | b3cbd9b | 2016-08-02 14:02:52 -0700 | [diff] [blame] | 547 | return true; |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 548 | } |
Andrey Ryabinin | b3cbd9b | 2016-08-02 14:02:52 -0700 | [diff] [blame] | 549 | |
| 550 | kasan_poison_slab_free(cache, object); |
| 551 | |
| 552 | if (unlikely(!(cache->flags & SLAB_KASAN))) |
| 553 | return false; |
| 554 | |
| 555 | set_track(&get_alloc_info(cache, object)->free_track, GFP_NOWAIT); |
| 556 | quarantine_put(get_free_info(cache, object), cache); |
| 557 | return true; |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 558 | } |
| 559 | |
Alexander Potapenko | 505f5dc | 2016-03-25 14:22:02 -0700 | [diff] [blame] | 560 | void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size, |
| 561 | gfp_t flags) |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 562 | { |
| 563 | unsigned long redzone_start; |
| 564 | unsigned long redzone_end; |
| 565 | |
Andrey Ryabinin | 4b3ec5a | 2016-08-02 14:02:43 -0700 | [diff] [blame] | 566 | if (gfpflags_allow_blocking(flags)) |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 567 | quarantine_reduce(); |
| 568 | |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 569 | if (unlikely(object == NULL)) |
| 570 | return; |
| 571 | |
| 572 | redzone_start = round_up((unsigned long)(object + size), |
| 573 | KASAN_SHADOW_SCALE_SIZE); |
| 574 | redzone_end = round_up((unsigned long)object + cache->object_size, |
| 575 | KASAN_SHADOW_SCALE_SIZE); |
| 576 | |
| 577 | kasan_unpoison_shadow(object, size); |
| 578 | kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start, |
| 579 | KASAN_KMALLOC_REDZONE); |
Alexander Potapenko | 7ed2f9e | 2016-03-25 14:21:59 -0700 | [diff] [blame] | 580 | |
Andrey Ryabinin | b3cbd9b | 2016-08-02 14:02:52 -0700 | [diff] [blame] | 581 | if (cache->flags & SLAB_KASAN) |
| 582 | set_track(&get_alloc_info(cache, object)->alloc_track, flags); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 583 | } |
| 584 | EXPORT_SYMBOL(kasan_kmalloc); |
| 585 | |
Alexander Potapenko | 505f5dc | 2016-03-25 14:22:02 -0700 | [diff] [blame] | 586 | void kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags) |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 587 | { |
| 588 | struct page *page; |
| 589 | unsigned long redzone_start; |
| 590 | unsigned long redzone_end; |
| 591 | |
Andrey Ryabinin | 4b3ec5a | 2016-08-02 14:02:43 -0700 | [diff] [blame] | 592 | if (gfpflags_allow_blocking(flags)) |
Alexander Potapenko | 55834c5 | 2016-05-20 16:59:11 -0700 | [diff] [blame] | 593 | quarantine_reduce(); |
| 594 | |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 595 | if (unlikely(ptr == NULL)) |
| 596 | return; |
| 597 | |
| 598 | page = virt_to_page(ptr); |
| 599 | redzone_start = round_up((unsigned long)(ptr + size), |
| 600 | KASAN_SHADOW_SCALE_SIZE); |
| 601 | redzone_end = (unsigned long)ptr + (PAGE_SIZE << compound_order(page)); |
| 602 | |
| 603 | kasan_unpoison_shadow(ptr, size); |
| 604 | kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start, |
| 605 | KASAN_PAGE_REDZONE); |
| 606 | } |
| 607 | |
Alexander Potapenko | 505f5dc | 2016-03-25 14:22:02 -0700 | [diff] [blame] | 608 | void kasan_krealloc(const void *object, size_t size, gfp_t flags) |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 609 | { |
| 610 | struct page *page; |
| 611 | |
| 612 | if (unlikely(object == ZERO_SIZE_PTR)) |
| 613 | return; |
| 614 | |
| 615 | page = virt_to_head_page(object); |
| 616 | |
| 617 | if (unlikely(!PageSlab(page))) |
Alexander Potapenko | 505f5dc | 2016-03-25 14:22:02 -0700 | [diff] [blame] | 618 | kasan_kmalloc_large(object, size, flags); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 619 | else |
Alexander Potapenko | 505f5dc | 2016-03-25 14:22:02 -0700 | [diff] [blame] | 620 | kasan_kmalloc(page->slab_cache, object, size, flags); |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Andrey Ryabinin | 9b75a86 | 2016-06-24 14:49:34 -0700 | [diff] [blame] | 623 | void kasan_poison_kfree(void *ptr) |
Andrey Ryabinin | 9239361 | 2015-04-15 16:15:05 -0700 | [diff] [blame] | 624 | { |
| 625 | struct page *page; |
| 626 | |
| 627 | page = virt_to_head_page(ptr); |
| 628 | |
| 629 | if (unlikely(!PageSlab(page))) |
| 630 | kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page), |
| 631 | KASAN_FREE_PAGE); |
| 632 | else |
Andrey Ryabinin | 9b75a86 | 2016-06-24 14:49:34 -0700 | [diff] [blame] | 633 | kasan_poison_slab_free(page->slab_cache, ptr); |
Andrey Ryabinin | 9239361 | 2015-04-15 16:15:05 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Andrey Ryabinin | 0316bec | 2015-02-13 14:39:42 -0800 | [diff] [blame] | 636 | void kasan_kfree_large(const void *ptr) |
| 637 | { |
| 638 | struct page *page = virt_to_page(ptr); |
| 639 | |
| 640 | kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page), |
| 641 | KASAN_FREE_PAGE); |
| 642 | } |
| 643 | |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 644 | int kasan_module_alloc(void *addr, size_t size) |
| 645 | { |
| 646 | void *ret; |
| 647 | size_t shadow_size; |
| 648 | unsigned long shadow_start; |
| 649 | |
| 650 | shadow_start = (unsigned long)kasan_mem_to_shadow(addr); |
| 651 | shadow_size = round_up(size >> KASAN_SHADOW_SCALE_SHIFT, |
| 652 | PAGE_SIZE); |
| 653 | |
| 654 | if (WARN_ON(!PAGE_ALIGNED(shadow_start))) |
| 655 | return -EINVAL; |
| 656 | |
| 657 | ret = __vmalloc_node_range(shadow_size, 1, shadow_start, |
| 658 | shadow_start + shadow_size, |
| 659 | GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, |
| 660 | PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE, |
| 661 | __builtin_return_address(0)); |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 662 | |
| 663 | if (ret) { |
| 664 | find_vm_area(addr)->flags |= VM_KASAN; |
Andrey Ryabinin | 4593725 | 2015-11-20 15:57:18 -0800 | [diff] [blame] | 665 | kmemleak_ignore(ret); |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | return -ENOMEM; |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 670 | } |
| 671 | |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 672 | void kasan_free_shadow(const struct vm_struct *vm) |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 673 | { |
Andrey Ryabinin | a5af5aa | 2015-03-12 16:26:11 -0700 | [diff] [blame] | 674 | if (vm->flags & VM_KASAN) |
| 675 | vfree(kasan_mem_to_shadow(vm->addr)); |
Andrey Ryabinin | bebf56a | 2015-02-13 14:40:17 -0800 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | static void register_global(struct kasan_global *global) |
| 679 | { |
| 680 | size_t aligned_size = round_up(global->size, KASAN_SHADOW_SCALE_SIZE); |
| 681 | |
| 682 | kasan_unpoison_shadow(global->beg, global->size); |
| 683 | |
| 684 | kasan_poison_shadow(global->beg + aligned_size, |
| 685 | global->size_with_redzone - aligned_size, |
| 686 | KASAN_GLOBAL_REDZONE); |
| 687 | } |
| 688 | |
| 689 | void __asan_register_globals(struct kasan_global *globals, size_t size) |
| 690 | { |
| 691 | int i; |
| 692 | |
| 693 | for (i = 0; i < size; i++) |
| 694 | register_global(&globals[i]); |
| 695 | } |
| 696 | EXPORT_SYMBOL(__asan_register_globals); |
| 697 | |
| 698 | void __asan_unregister_globals(struct kasan_global *globals, size_t size) |
| 699 | { |
| 700 | } |
| 701 | EXPORT_SYMBOL(__asan_unregister_globals); |
| 702 | |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 703 | #define DEFINE_ASAN_LOAD_STORE(size) \ |
| 704 | void __asan_load##size(unsigned long addr) \ |
| 705 | { \ |
| 706 | check_memory_region_inline(addr, size, false, _RET_IP_);\ |
| 707 | } \ |
| 708 | EXPORT_SYMBOL(__asan_load##size); \ |
| 709 | __alias(__asan_load##size) \ |
| 710 | void __asan_load##size##_noabort(unsigned long); \ |
| 711 | EXPORT_SYMBOL(__asan_load##size##_noabort); \ |
| 712 | void __asan_store##size(unsigned long addr) \ |
| 713 | { \ |
| 714 | check_memory_region_inline(addr, size, true, _RET_IP_); \ |
| 715 | } \ |
| 716 | EXPORT_SYMBOL(__asan_store##size); \ |
| 717 | __alias(__asan_store##size) \ |
| 718 | void __asan_store##size##_noabort(unsigned long); \ |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 719 | EXPORT_SYMBOL(__asan_store##size##_noabort) |
| 720 | |
| 721 | DEFINE_ASAN_LOAD_STORE(1); |
| 722 | DEFINE_ASAN_LOAD_STORE(2); |
| 723 | DEFINE_ASAN_LOAD_STORE(4); |
| 724 | DEFINE_ASAN_LOAD_STORE(8); |
| 725 | DEFINE_ASAN_LOAD_STORE(16); |
| 726 | |
| 727 | void __asan_loadN(unsigned long addr, size_t size) |
| 728 | { |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 729 | check_memory_region(addr, size, false, _RET_IP_); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 730 | } |
| 731 | EXPORT_SYMBOL(__asan_loadN); |
| 732 | |
| 733 | __alias(__asan_loadN) |
| 734 | void __asan_loadN_noabort(unsigned long, size_t); |
| 735 | EXPORT_SYMBOL(__asan_loadN_noabort); |
| 736 | |
| 737 | void __asan_storeN(unsigned long addr, size_t size) |
| 738 | { |
Andrey Ryabinin | 936bb4b | 2016-05-20 16:59:20 -0700 | [diff] [blame] | 739 | check_memory_region(addr, size, true, _RET_IP_); |
Andrey Ryabinin | 0b24bec | 2015-02-13 14:39:17 -0800 | [diff] [blame] | 740 | } |
| 741 | EXPORT_SYMBOL(__asan_storeN); |
| 742 | |
| 743 | __alias(__asan_storeN) |
| 744 | void __asan_storeN_noabort(unsigned long, size_t); |
| 745 | EXPORT_SYMBOL(__asan_storeN_noabort); |
| 746 | |
| 747 | /* to shut up compiler complaints */ |
| 748 | void __asan_handle_no_return(void) {} |
| 749 | EXPORT_SYMBOL(__asan_handle_no_return); |
Andrey Ryabinin | 786a895 | 2015-02-13 14:39:21 -0800 | [diff] [blame] | 750 | |
| 751 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 752 | static int kasan_mem_notifier(struct notifier_block *nb, |
| 753 | unsigned long action, void *data) |
| 754 | { |
| 755 | return (action == MEM_GOING_ONLINE) ? NOTIFY_BAD : NOTIFY_OK; |
| 756 | } |
| 757 | |
| 758 | static int __init kasan_memhotplug_init(void) |
| 759 | { |
Shuah Khan | 91a4c27 | 2016-06-08 15:33:45 -0700 | [diff] [blame] | 760 | pr_info("WARNING: KASAN doesn't support memory hot-add\n"); |
| 761 | pr_info("Memory hot-add will be disabled\n"); |
Andrey Ryabinin | 786a895 | 2015-02-13 14:39:21 -0800 | [diff] [blame] | 762 | |
| 763 | hotplug_memory_notifier(kasan_mem_notifier, 0); |
| 764 | |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | module_init(kasan_memhotplug_init); |
| 769 | #endif |