2 * This file contains shadow memory manipulation code.
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
7 * Some code borrowed from https://github.com/xairy/kasan-prototype by
8 * Andrey Konovalov <adech.fo@gmail.com>
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.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #define DISABLE_BRANCH_PROFILING
19 #include <linux/export.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/kmemleak.h>
23 #include <linux/memblock.h>
24 #include <linux/memory.h>
26 #include <linux/module.h>
27 #include <linux/printk.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/stacktrace.h>
31 #include <linux/string.h>
32 #include <linux/types.h>
33 #include <linux/vmalloc.h>
34 #include <linux/kasan.h>
40 * Poisons the shadow memory for 'size' bytes starting from 'addr'.
41 * Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE.
43 static void kasan_poison_shadow(const void *address, size_t size, u8 value)
45 void *shadow_start, *shadow_end;
47 shadow_start = kasan_mem_to_shadow(address);
48 shadow_end = kasan_mem_to_shadow(address + size);
50 memset(shadow_start, value, shadow_end - shadow_start);
53 void kasan_unpoison_shadow(const void *address, size_t size)
55 kasan_poison_shadow(address, size, 0);
57 if (size & KASAN_SHADOW_MASK) {
58 u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size);
59 *shadow = size & KASAN_SHADOW_MASK;
65 * All functions below always inlined so compiler could
66 * perform better optimizations in each of __asan_loadX/__assn_storeX
67 * depending on memory access size X.
70 static __always_inline bool memory_is_poisoned_1(unsigned long addr)
72 s8 shadow_value = *(s8 *)kasan_mem_to_shadow((void *)addr);
74 if (unlikely(shadow_value)) {
75 s8 last_accessible_byte = addr & KASAN_SHADOW_MASK;
76 return unlikely(last_accessible_byte >= shadow_value);
82 static __always_inline bool memory_is_poisoned_2(unsigned long addr)
84 u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
86 if (unlikely(*shadow_addr)) {
87 if (memory_is_poisoned_1(addr + 1))
91 * If single shadow byte covers 2-byte access, we don't
92 * need to do anything more. Otherwise, test the first
95 if (likely(((addr + 1) & KASAN_SHADOW_MASK) != 0))
98 return unlikely(*(u8 *)shadow_addr);
104 static __always_inline bool memory_is_poisoned_4(unsigned long addr)
106 u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
108 if (unlikely(*shadow_addr)) {
109 if (memory_is_poisoned_1(addr + 3))
113 * If single shadow byte covers 4-byte access, we don't
114 * need to do anything more. Otherwise, test the first
117 if (likely(((addr + 3) & KASAN_SHADOW_MASK) >= 3))
120 return unlikely(*(u8 *)shadow_addr);
126 static __always_inline bool memory_is_poisoned_8(unsigned long addr)
128 u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
130 if (unlikely(*shadow_addr)) {
131 if (memory_is_poisoned_1(addr + 7))
135 * If single shadow byte covers 8-byte access, we don't
136 * need to do anything more. Otherwise, test the first
139 if (likely(IS_ALIGNED(addr, KASAN_SHADOW_SCALE_SIZE)))
142 return unlikely(*(u8 *)shadow_addr);
148 static __always_inline bool memory_is_poisoned_16(unsigned long addr)
150 u32 *shadow_addr = (u32 *)kasan_mem_to_shadow((void *)addr);
152 if (unlikely(*shadow_addr)) {
153 u16 shadow_first_bytes = *(u16 *)shadow_addr;
155 if (unlikely(shadow_first_bytes))
159 * If two shadow bytes covers 16-byte access, we don't
160 * need to do anything more. Otherwise, test the last
163 if (likely(IS_ALIGNED(addr, KASAN_SHADOW_SCALE_SIZE)))
166 return memory_is_poisoned_1(addr + 15);
172 static __always_inline unsigned long bytes_is_zero(const u8 *start,
176 if (unlikely(*start))
177 return (unsigned long)start;
185 static __always_inline unsigned long memory_is_zero(const void *start,
190 unsigned int prefix = (unsigned long)start % 8;
192 if (end - start <= 16)
193 return bytes_is_zero(start, end - start);
197 ret = bytes_is_zero(start, prefix);
203 words = (end - start) / 8;
205 if (unlikely(*(u64 *)start))
206 return bytes_is_zero(start, 8);
211 return bytes_is_zero(start, (end - start) % 8);
214 static __always_inline bool memory_is_poisoned_n(unsigned long addr,
219 ret = memory_is_zero(kasan_mem_to_shadow((void *)addr),
220 kasan_mem_to_shadow((void *)addr + size - 1) + 1);
223 unsigned long last_byte = addr + size - 1;
224 s8 *last_shadow = (s8 *)kasan_mem_to_shadow((void *)last_byte);
226 if (unlikely(ret != (unsigned long)last_shadow ||
227 ((long)(last_byte & KASAN_SHADOW_MASK) >= *last_shadow)))
233 static __always_inline bool memory_is_poisoned(unsigned long addr, size_t size)
235 if (__builtin_constant_p(size)) {
238 return memory_is_poisoned_1(addr);
240 return memory_is_poisoned_2(addr);
242 return memory_is_poisoned_4(addr);
244 return memory_is_poisoned_8(addr);
246 return memory_is_poisoned_16(addr);
252 return memory_is_poisoned_n(addr, size);
256 static __always_inline void check_memory_region(unsigned long addr,
257 size_t size, bool write)
259 if (unlikely(size == 0))
262 if (unlikely((void *)addr <
263 kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
264 kasan_report(addr, size, write, _RET_IP_);
268 if (likely(!memory_is_poisoned(addr, size)))
271 kasan_report(addr, size, write, _RET_IP_);
274 void __asan_loadN(unsigned long addr, size_t size);
275 void __asan_storeN(unsigned long addr, size_t size);
278 void *memset(void *addr, int c, size_t len)
280 __asan_storeN((unsigned long)addr, len);
282 return __memset(addr, c, len);
286 void *memmove(void *dest, const void *src, size_t len)
288 __asan_loadN((unsigned long)src, len);
289 __asan_storeN((unsigned long)dest, len);
291 return __memmove(dest, src, len);
295 void *memcpy(void *dest, const void *src, size_t len)
297 __asan_loadN((unsigned long)src, len);
298 __asan_storeN((unsigned long)dest, len);
300 return __memcpy(dest, src, len);
303 void kasan_alloc_pages(struct page *page, unsigned int order)
305 if (likely(!PageHighMem(page)))
306 kasan_unpoison_shadow(page_address(page), PAGE_SIZE << order);
309 void kasan_free_pages(struct page *page, unsigned int order)
311 if (likely(!PageHighMem(page)))
312 kasan_poison_shadow(page_address(page),
317 void kasan_poison_slab(struct page *page)
319 kasan_poison_shadow(page_address(page),
320 PAGE_SIZE << compound_order(page),
321 KASAN_KMALLOC_REDZONE);
324 void kasan_unpoison_object_data(struct kmem_cache *cache, void *object)
326 kasan_unpoison_shadow(object, cache->object_size);
329 void kasan_poison_object_data(struct kmem_cache *cache, void *object)
331 kasan_poison_shadow(object,
332 round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
333 KASAN_KMALLOC_REDZONE);
336 void kasan_slab_alloc(struct kmem_cache *cache, void *object)
338 kasan_kmalloc(cache, object, cache->object_size);
341 void kasan_slab_free(struct kmem_cache *cache, void *object)
343 unsigned long size = cache->object_size;
344 unsigned long rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
346 /* RCU slabs could be legally used after free within the RCU period */
347 if (unlikely(cache->flags & SLAB_DESTROY_BY_RCU))
350 kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
353 void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size)
355 unsigned long redzone_start;
356 unsigned long redzone_end;
358 if (unlikely(object == NULL))
361 redzone_start = round_up((unsigned long)(object + size),
362 KASAN_SHADOW_SCALE_SIZE);
363 redzone_end = round_up((unsigned long)object + cache->object_size,
364 KASAN_SHADOW_SCALE_SIZE);
366 kasan_unpoison_shadow(object, size);
367 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
368 KASAN_KMALLOC_REDZONE);
370 EXPORT_SYMBOL(kasan_kmalloc);
372 void kasan_kmalloc_large(const void *ptr, size_t size)
375 unsigned long redzone_start;
376 unsigned long redzone_end;
378 if (unlikely(ptr == NULL))
381 page = virt_to_page(ptr);
382 redzone_start = round_up((unsigned long)(ptr + size),
383 KASAN_SHADOW_SCALE_SIZE);
384 redzone_end = (unsigned long)ptr + (PAGE_SIZE << compound_order(page));
386 kasan_unpoison_shadow(ptr, size);
387 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
391 void kasan_krealloc(const void *object, size_t size)
395 if (unlikely(object == ZERO_SIZE_PTR))
398 page = virt_to_head_page(object);
400 if (unlikely(!PageSlab(page)))
401 kasan_kmalloc_large(object, size);
403 kasan_kmalloc(page->slab_cache, object, size);
406 void kasan_kfree(void *ptr)
410 page = virt_to_head_page(ptr);
412 if (unlikely(!PageSlab(page)))
413 kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page),
416 kasan_slab_free(page->slab_cache, ptr);
419 void kasan_kfree_large(const void *ptr)
421 struct page *page = virt_to_page(ptr);
423 kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page),
427 int kasan_module_alloc(void *addr, size_t size)
431 unsigned long shadow_start;
433 shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
434 shadow_size = round_up(size >> KASAN_SHADOW_SCALE_SHIFT,
437 if (WARN_ON(!PAGE_ALIGNED(shadow_start)))
440 ret = __vmalloc_node_range(shadow_size, 1, shadow_start,
441 shadow_start + shadow_size,
442 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
443 PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE,
444 __builtin_return_address(0));
447 find_vm_area(addr)->flags |= VM_KASAN;
448 kmemleak_ignore(ret);
455 void kasan_free_shadow(const struct vm_struct *vm)
457 if (vm->flags & VM_KASAN)
458 vfree(kasan_mem_to_shadow(vm->addr));
461 static void register_global(struct kasan_global *global)
463 size_t aligned_size = round_up(global->size, KASAN_SHADOW_SCALE_SIZE);
465 kasan_unpoison_shadow(global->beg, global->size);
467 kasan_poison_shadow(global->beg + aligned_size,
468 global->size_with_redzone - aligned_size,
469 KASAN_GLOBAL_REDZONE);
472 void __asan_register_globals(struct kasan_global *globals, size_t size)
476 for (i = 0; i < size; i++)
477 register_global(&globals[i]);
479 EXPORT_SYMBOL(__asan_register_globals);
481 void __asan_unregister_globals(struct kasan_global *globals, size_t size)
484 EXPORT_SYMBOL(__asan_unregister_globals);
486 #define DEFINE_ASAN_LOAD_STORE(size) \
487 void __asan_load##size(unsigned long addr) \
489 check_memory_region(addr, size, false); \
491 EXPORT_SYMBOL(__asan_load##size); \
492 __alias(__asan_load##size) \
493 void __asan_load##size##_noabort(unsigned long); \
494 EXPORT_SYMBOL(__asan_load##size##_noabort); \
495 void __asan_store##size(unsigned long addr) \
497 check_memory_region(addr, size, true); \
499 EXPORT_SYMBOL(__asan_store##size); \
500 __alias(__asan_store##size) \
501 void __asan_store##size##_noabort(unsigned long); \
502 EXPORT_SYMBOL(__asan_store##size##_noabort)
504 DEFINE_ASAN_LOAD_STORE(1);
505 DEFINE_ASAN_LOAD_STORE(2);
506 DEFINE_ASAN_LOAD_STORE(4);
507 DEFINE_ASAN_LOAD_STORE(8);
508 DEFINE_ASAN_LOAD_STORE(16);
510 void __asan_loadN(unsigned long addr, size_t size)
512 check_memory_region(addr, size, false);
514 EXPORT_SYMBOL(__asan_loadN);
516 __alias(__asan_loadN)
517 void __asan_loadN_noabort(unsigned long, size_t);
518 EXPORT_SYMBOL(__asan_loadN_noabort);
520 void __asan_storeN(unsigned long addr, size_t size)
522 check_memory_region(addr, size, true);
524 EXPORT_SYMBOL(__asan_storeN);
526 __alias(__asan_storeN)
527 void __asan_storeN_noabort(unsigned long, size_t);
528 EXPORT_SYMBOL(__asan_storeN_noabort);
530 /* to shut up compiler complaints */
531 void __asan_handle_no_return(void) {}
532 EXPORT_SYMBOL(__asan_handle_no_return);
534 #ifdef CONFIG_MEMORY_HOTPLUG
535 static int kasan_mem_notifier(struct notifier_block *nb,
536 unsigned long action, void *data)
538 return (action == MEM_GOING_ONLINE) ? NOTIFY_BAD : NOTIFY_OK;
541 static int __init kasan_memhotplug_init(void)
543 pr_err("WARNING: KASAN doesn't support memory hot-add\n");
544 pr_err("Memory hot-add will be disabled\n");
546 hotplug_memory_notifier(kasan_mem_notifier, 0);
551 module_init(kasan_memhotplug_init);