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