Linux 4.1.18
[linux/fpc-iii.git] / mm / kasan / kasan.c
blob6c513a63ea84c3c7ffd41201b7a419ff7b6dfd5d
1 /*
2 * This file contains shadow memory manipulation code.
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <a.ryabinin@samsung.com>
7 * Some of code borrowed from https://github.com/xairy/linux 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/memblock.h>
23 #include <linux/memory.h>
24 #include <linux/mm.h>
25 #include <linux/module.h>
26 #include <linux/printk.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/stacktrace.h>
30 #include <linux/string.h>
31 #include <linux/types.h>
32 #include <linux/vmalloc.h>
33 #include <linux/kasan.h>
35 #include "kasan.h"
36 #include "../slab.h"
39 * Poisons the shadow memory for 'size' bytes starting from 'addr'.
40 * Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE.
42 static void kasan_poison_shadow(const void *address, size_t size, u8 value)
44 void *shadow_start, *shadow_end;
46 shadow_start = kasan_mem_to_shadow(address);
47 shadow_end = kasan_mem_to_shadow(address + size);
49 memset(shadow_start, value, shadow_end - shadow_start);
52 void kasan_unpoison_shadow(const void *address, size_t size)
54 kasan_poison_shadow(address, size, 0);
56 if (size & KASAN_SHADOW_MASK) {
57 u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size);
58 *shadow = size & KASAN_SHADOW_MASK;
64 * All functions below always inlined so compiler could
65 * perform better optimizations in each of __asan_loadX/__assn_storeX
66 * depending on memory access size X.
69 static __always_inline bool memory_is_poisoned_1(unsigned long addr)
71 s8 shadow_value = *(s8 *)kasan_mem_to_shadow((void *)addr);
73 if (unlikely(shadow_value)) {
74 s8 last_accessible_byte = addr & KASAN_SHADOW_MASK;
75 return unlikely(last_accessible_byte >= shadow_value);
78 return false;
81 static __always_inline bool memory_is_poisoned_2(unsigned long addr)
83 u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
85 if (unlikely(*shadow_addr)) {
86 if (memory_is_poisoned_1(addr + 1))
87 return true;
89 if (likely(((addr + 1) & KASAN_SHADOW_MASK) != 0))
90 return false;
92 return unlikely(*(u8 *)shadow_addr);
95 return false;
98 static __always_inline bool memory_is_poisoned_4(unsigned long addr)
100 u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
102 if (unlikely(*shadow_addr)) {
103 if (memory_is_poisoned_1(addr + 3))
104 return true;
106 if (likely(((addr + 3) & KASAN_SHADOW_MASK) >= 3))
107 return false;
109 return unlikely(*(u8 *)shadow_addr);
112 return false;
115 static __always_inline bool memory_is_poisoned_8(unsigned long addr)
117 u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr);
119 if (unlikely(*shadow_addr)) {
120 if (memory_is_poisoned_1(addr + 7))
121 return true;
123 if (likely(((addr + 7) & KASAN_SHADOW_MASK) >= 7))
124 return false;
126 return unlikely(*(u8 *)shadow_addr);
129 return false;
132 static __always_inline bool memory_is_poisoned_16(unsigned long addr)
134 u32 *shadow_addr = (u32 *)kasan_mem_to_shadow((void *)addr);
136 if (unlikely(*shadow_addr)) {
137 u16 shadow_first_bytes = *(u16 *)shadow_addr;
138 s8 last_byte = (addr + 15) & KASAN_SHADOW_MASK;
140 if (unlikely(shadow_first_bytes))
141 return true;
143 if (likely(!last_byte))
144 return false;
146 return memory_is_poisoned_1(addr + 15);
149 return false;
152 static __always_inline unsigned long bytes_is_zero(const u8 *start,
153 size_t size)
155 while (size) {
156 if (unlikely(*start))
157 return (unsigned long)start;
158 start++;
159 size--;
162 return 0;
165 static __always_inline unsigned long memory_is_zero(const void *start,
166 const void *end)
168 unsigned int words;
169 unsigned long ret;
170 unsigned int prefix = (unsigned long)start % 8;
172 if (end - start <= 16)
173 return bytes_is_zero(start, end - start);
175 if (prefix) {
176 prefix = 8 - prefix;
177 ret = bytes_is_zero(start, prefix);
178 if (unlikely(ret))
179 return ret;
180 start += prefix;
183 words = (end - start) / 8;
184 while (words) {
185 if (unlikely(*(u64 *)start))
186 return bytes_is_zero(start, 8);
187 start += 8;
188 words--;
191 return bytes_is_zero(start, (end - start) % 8);
194 static __always_inline bool memory_is_poisoned_n(unsigned long addr,
195 size_t size)
197 unsigned long ret;
199 ret = memory_is_zero(kasan_mem_to_shadow((void *)addr),
200 kasan_mem_to_shadow((void *)addr + size - 1) + 1);
202 if (unlikely(ret)) {
203 unsigned long last_byte = addr + size - 1;
204 s8 *last_shadow = (s8 *)kasan_mem_to_shadow((void *)last_byte);
206 if (unlikely(ret != (unsigned long)last_shadow ||
207 ((last_byte & KASAN_SHADOW_MASK) >= *last_shadow)))
208 return true;
210 return false;
213 static __always_inline bool memory_is_poisoned(unsigned long addr, size_t size)
215 if (__builtin_constant_p(size)) {
216 switch (size) {
217 case 1:
218 return memory_is_poisoned_1(addr);
219 case 2:
220 return memory_is_poisoned_2(addr);
221 case 4:
222 return memory_is_poisoned_4(addr);
223 case 8:
224 return memory_is_poisoned_8(addr);
225 case 16:
226 return memory_is_poisoned_16(addr);
227 default:
228 BUILD_BUG();
232 return memory_is_poisoned_n(addr, size);
236 static __always_inline void check_memory_region(unsigned long addr,
237 size_t size, bool write)
239 struct kasan_access_info info;
241 if (unlikely(size == 0))
242 return;
244 if (unlikely((void *)addr <
245 kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
246 info.access_addr = (void *)addr;
247 info.access_size = size;
248 info.is_write = write;
249 info.ip = _RET_IP_;
250 kasan_report_user_access(&info);
251 return;
254 if (likely(!memory_is_poisoned(addr, size)))
255 return;
257 kasan_report(addr, size, write, _RET_IP_);
260 void __asan_loadN(unsigned long addr, size_t size);
261 void __asan_storeN(unsigned long addr, size_t size);
263 #undef memset
264 void *memset(void *addr, int c, size_t len)
266 __asan_storeN((unsigned long)addr, len);
268 return __memset(addr, c, len);
271 #undef memmove
272 void *memmove(void *dest, const void *src, size_t len)
274 __asan_loadN((unsigned long)src, len);
275 __asan_storeN((unsigned long)dest, len);
277 return __memmove(dest, src, len);
280 #undef memcpy
281 void *memcpy(void *dest, const void *src, size_t len)
283 __asan_loadN((unsigned long)src, len);
284 __asan_storeN((unsigned long)dest, len);
286 return __memcpy(dest, src, len);
289 void kasan_alloc_pages(struct page *page, unsigned int order)
291 if (likely(!PageHighMem(page)))
292 kasan_unpoison_shadow(page_address(page), PAGE_SIZE << order);
295 void kasan_free_pages(struct page *page, unsigned int order)
297 if (likely(!PageHighMem(page)))
298 kasan_poison_shadow(page_address(page),
299 PAGE_SIZE << order,
300 KASAN_FREE_PAGE);
303 void kasan_poison_slab(struct page *page)
305 kasan_poison_shadow(page_address(page),
306 PAGE_SIZE << compound_order(page),
307 KASAN_KMALLOC_REDZONE);
310 void kasan_unpoison_object_data(struct kmem_cache *cache, void *object)
312 kasan_unpoison_shadow(object, cache->object_size);
315 void kasan_poison_object_data(struct kmem_cache *cache, void *object)
317 kasan_poison_shadow(object,
318 round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
319 KASAN_KMALLOC_REDZONE);
322 void kasan_slab_alloc(struct kmem_cache *cache, void *object)
324 kasan_kmalloc(cache, object, cache->object_size);
327 void kasan_slab_free(struct kmem_cache *cache, void *object)
329 unsigned long size = cache->object_size;
330 unsigned long rounded_up_size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
332 /* RCU slabs could be legally used after free within the RCU period */
333 if (unlikely(cache->flags & SLAB_DESTROY_BY_RCU))
334 return;
336 kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE);
339 void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size)
341 unsigned long redzone_start;
342 unsigned long redzone_end;
344 if (unlikely(object == NULL))
345 return;
347 redzone_start = round_up((unsigned long)(object + size),
348 KASAN_SHADOW_SCALE_SIZE);
349 redzone_end = round_up((unsigned long)object + cache->object_size,
350 KASAN_SHADOW_SCALE_SIZE);
352 kasan_unpoison_shadow(object, size);
353 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
354 KASAN_KMALLOC_REDZONE);
356 EXPORT_SYMBOL(kasan_kmalloc);
358 void kasan_kmalloc_large(const void *ptr, size_t size)
360 struct page *page;
361 unsigned long redzone_start;
362 unsigned long redzone_end;
364 if (unlikely(ptr == NULL))
365 return;
367 page = virt_to_page(ptr);
368 redzone_start = round_up((unsigned long)(ptr + size),
369 KASAN_SHADOW_SCALE_SIZE);
370 redzone_end = (unsigned long)ptr + (PAGE_SIZE << compound_order(page));
372 kasan_unpoison_shadow(ptr, size);
373 kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
374 KASAN_PAGE_REDZONE);
377 void kasan_krealloc(const void *object, size_t size)
379 struct page *page;
381 if (unlikely(object == ZERO_SIZE_PTR))
382 return;
384 page = virt_to_head_page(object);
386 if (unlikely(!PageSlab(page)))
387 kasan_kmalloc_large(object, size);
388 else
389 kasan_kmalloc(page->slab_cache, object, size);
392 void kasan_kfree(void *ptr)
394 struct page *page;
396 page = virt_to_head_page(ptr);
398 if (unlikely(!PageSlab(page)))
399 kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page),
400 KASAN_FREE_PAGE);
401 else
402 kasan_slab_free(page->slab_cache, ptr);
405 void kasan_kfree_large(const void *ptr)
407 struct page *page = virt_to_page(ptr);
409 kasan_poison_shadow(ptr, PAGE_SIZE << compound_order(page),
410 KASAN_FREE_PAGE);
413 int kasan_module_alloc(void *addr, size_t size)
415 void *ret;
416 size_t shadow_size;
417 unsigned long shadow_start;
419 shadow_start = (unsigned long)kasan_mem_to_shadow(addr);
420 shadow_size = round_up(size >> KASAN_SHADOW_SCALE_SHIFT,
421 PAGE_SIZE);
423 if (WARN_ON(!PAGE_ALIGNED(shadow_start)))
424 return -EINVAL;
426 ret = __vmalloc_node_range(shadow_size, 1, shadow_start,
427 shadow_start + shadow_size,
428 GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO,
429 PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE,
430 __builtin_return_address(0));
432 if (ret) {
433 find_vm_area(addr)->flags |= VM_KASAN;
434 return 0;
437 return -ENOMEM;
440 void kasan_free_shadow(const struct vm_struct *vm)
442 if (vm->flags & VM_KASAN)
443 vfree(kasan_mem_to_shadow(vm->addr));
446 static void register_global(struct kasan_global *global)
448 size_t aligned_size = round_up(global->size, KASAN_SHADOW_SCALE_SIZE);
450 kasan_unpoison_shadow(global->beg, global->size);
452 kasan_poison_shadow(global->beg + aligned_size,
453 global->size_with_redzone - aligned_size,
454 KASAN_GLOBAL_REDZONE);
457 void __asan_register_globals(struct kasan_global *globals, size_t size)
459 int i;
461 for (i = 0; i < size; i++)
462 register_global(&globals[i]);
464 EXPORT_SYMBOL(__asan_register_globals);
466 void __asan_unregister_globals(struct kasan_global *globals, size_t size)
469 EXPORT_SYMBOL(__asan_unregister_globals);
471 #define DEFINE_ASAN_LOAD_STORE(size) \
472 void __asan_load##size(unsigned long addr) \
474 check_memory_region(addr, size, false); \
476 EXPORT_SYMBOL(__asan_load##size); \
477 __alias(__asan_load##size) \
478 void __asan_load##size##_noabort(unsigned long); \
479 EXPORT_SYMBOL(__asan_load##size##_noabort); \
480 void __asan_store##size(unsigned long addr) \
482 check_memory_region(addr, size, true); \
484 EXPORT_SYMBOL(__asan_store##size); \
485 __alias(__asan_store##size) \
486 void __asan_store##size##_noabort(unsigned long); \
487 EXPORT_SYMBOL(__asan_store##size##_noabort)
489 DEFINE_ASAN_LOAD_STORE(1);
490 DEFINE_ASAN_LOAD_STORE(2);
491 DEFINE_ASAN_LOAD_STORE(4);
492 DEFINE_ASAN_LOAD_STORE(8);
493 DEFINE_ASAN_LOAD_STORE(16);
495 void __asan_loadN(unsigned long addr, size_t size)
497 check_memory_region(addr, size, false);
499 EXPORT_SYMBOL(__asan_loadN);
501 __alias(__asan_loadN)
502 void __asan_loadN_noabort(unsigned long, size_t);
503 EXPORT_SYMBOL(__asan_loadN_noabort);
505 void __asan_storeN(unsigned long addr, size_t size)
507 check_memory_region(addr, size, true);
509 EXPORT_SYMBOL(__asan_storeN);
511 __alias(__asan_storeN)
512 void __asan_storeN_noabort(unsigned long, size_t);
513 EXPORT_SYMBOL(__asan_storeN_noabort);
515 /* to shut up compiler complaints */
516 void __asan_handle_no_return(void) {}
517 EXPORT_SYMBOL(__asan_handle_no_return);
519 #ifdef CONFIG_MEMORY_HOTPLUG
520 static int kasan_mem_notifier(struct notifier_block *nb,
521 unsigned long action, void *data)
523 return (action == MEM_GOING_ONLINE) ? NOTIFY_BAD : NOTIFY_OK;
526 static int __init kasan_memhotplug_init(void)
528 pr_err("WARNING: KASan doesn't support memory hot-add\n");
529 pr_err("Memory hot-add will be disabled\n");
531 hotplug_memory_notifier(kasan_mem_notifier, 0);
533 return 0;
536 module_init(kasan_memhotplug_init);
537 #endif