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