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