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/interrupt.h>
21 #include <linux/init.h>
22 #include <linux/kasan.h>
23 #include <linux/kernel.h>
24 #include <linux/kmemleak.h>
25 #include <linux/linkage.h>
26 #include <linux/memblock.h>
27 #include <linux/memory.h>
29 #include <linux/module.h>
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>
36 #include <linux/vmalloc.h>
37 #include <linux/bug.h>
43 * Poisons the shadow memory for 'size' bytes starting from 'addr'.
44 * Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE.
46 static void kasan_poison_shadow(const void *address
, size_t size
, u8 value
)
48 void *shadow_start
, *shadow_end
;
50 shadow_start
= kasan_mem_to_shadow(address
);
51 shadow_end
= kasan_mem_to_shadow(address
+ size
);
53 memset(shadow_start
, value
, shadow_end
- shadow_start
);
56 void kasan_unpoison_shadow(const void *address
, size_t size
)
58 kasan_poison_shadow(address
, size
, 0);
60 if (size
& KASAN_SHADOW_MASK
) {
61 u8
*shadow
= (u8
*)kasan_mem_to_shadow(address
+ size
);
62 *shadow
= size
& KASAN_SHADOW_MASK
;
66 static void __kasan_unpoison_stack(struct task_struct
*task
, const void *sp
)
68 void *base
= task_stack_page(task
);
69 size_t size
= sp
- base
;
71 kasan_unpoison_shadow(base
, size
);
74 /* Unpoison the entire stack for a task. */
75 void kasan_unpoison_task_stack(struct task_struct
*task
)
77 __kasan_unpoison_stack(task
, task_stack_page(task
) + THREAD_SIZE
);
80 /* Unpoison the stack for the current task beyond a watermark sp value. */
81 asmlinkage
void kasan_unpoison_task_stack_below(const void *watermark
)
83 __kasan_unpoison_stack(current
, watermark
);
87 * Clear all poison for the region between the current SP and a provided
88 * watermark value, as is sometimes required prior to hand-crafted asm function
89 * returns in the middle of functions.
91 void kasan_unpoison_stack_above_sp_to(const void *watermark
)
93 const void *sp
= __builtin_frame_address(0);
94 size_t size
= watermark
- sp
;
96 if (WARN_ON(sp
> watermark
))
98 kasan_unpoison_shadow(sp
, size
);
102 * All functions below always inlined so compiler could
103 * perform better optimizations in each of __asan_loadX/__assn_storeX
104 * depending on memory access size X.
107 static __always_inline
bool memory_is_poisoned_1(unsigned long addr
)
109 s8 shadow_value
= *(s8
*)kasan_mem_to_shadow((void *)addr
);
111 if (unlikely(shadow_value
)) {
112 s8 last_accessible_byte
= addr
& KASAN_SHADOW_MASK
;
113 return unlikely(last_accessible_byte
>= shadow_value
);
119 static __always_inline
bool memory_is_poisoned_2(unsigned long addr
)
121 u16
*shadow_addr
= (u16
*)kasan_mem_to_shadow((void *)addr
);
123 if (unlikely(*shadow_addr
)) {
124 if (memory_is_poisoned_1(addr
+ 1))
128 * If single shadow byte covers 2-byte access, we don't
129 * need to do anything more. Otherwise, test the first
132 if (likely(((addr
+ 1) & KASAN_SHADOW_MASK
) != 0))
135 return unlikely(*(u8
*)shadow_addr
);
141 static __always_inline
bool memory_is_poisoned_4(unsigned long addr
)
143 u16
*shadow_addr
= (u16
*)kasan_mem_to_shadow((void *)addr
);
145 if (unlikely(*shadow_addr
)) {
146 if (memory_is_poisoned_1(addr
+ 3))
150 * If single shadow byte covers 4-byte access, we don't
151 * need to do anything more. Otherwise, test the first
154 if (likely(((addr
+ 3) & KASAN_SHADOW_MASK
) >= 3))
157 return unlikely(*(u8
*)shadow_addr
);
163 static __always_inline
bool memory_is_poisoned_8(unsigned long addr
)
165 u16
*shadow_addr
= (u16
*)kasan_mem_to_shadow((void *)addr
);
167 if (unlikely(*shadow_addr
)) {
168 if (memory_is_poisoned_1(addr
+ 7))
172 * If single shadow byte covers 8-byte access, we don't
173 * need to do anything more. Otherwise, test the first
176 if (likely(IS_ALIGNED(addr
, KASAN_SHADOW_SCALE_SIZE
)))
179 return unlikely(*(u8
*)shadow_addr
);
185 static __always_inline
bool memory_is_poisoned_16(unsigned long addr
)
187 u32
*shadow_addr
= (u32
*)kasan_mem_to_shadow((void *)addr
);
189 if (unlikely(*shadow_addr
)) {
190 u16 shadow_first_bytes
= *(u16
*)shadow_addr
;
192 if (unlikely(shadow_first_bytes
))
196 * If two shadow bytes covers 16-byte access, we don't
197 * need to do anything more. Otherwise, test the last
200 if (likely(IS_ALIGNED(addr
, KASAN_SHADOW_SCALE_SIZE
)))
203 return memory_is_poisoned_1(addr
+ 15);
209 static __always_inline
unsigned long bytes_is_zero(const u8
*start
,
213 if (unlikely(*start
))
214 return (unsigned long)start
;
222 static __always_inline
unsigned long memory_is_zero(const void *start
,
227 unsigned int prefix
= (unsigned long)start
% 8;
229 if (end
- start
<= 16)
230 return bytes_is_zero(start
, end
- start
);
234 ret
= bytes_is_zero(start
, prefix
);
240 words
= (end
- start
) / 8;
242 if (unlikely(*(u64
*)start
))
243 return bytes_is_zero(start
, 8);
248 return bytes_is_zero(start
, (end
- start
) % 8);
251 static __always_inline
bool memory_is_poisoned_n(unsigned long addr
,
256 ret
= memory_is_zero(kasan_mem_to_shadow((void *)addr
),
257 kasan_mem_to_shadow((void *)addr
+ size
- 1) + 1);
260 unsigned long last_byte
= addr
+ size
- 1;
261 s8
*last_shadow
= (s8
*)kasan_mem_to_shadow((void *)last_byte
);
263 if (unlikely(ret
!= (unsigned long)last_shadow
||
264 ((long)(last_byte
& KASAN_SHADOW_MASK
) >= *last_shadow
)))
270 static __always_inline
bool memory_is_poisoned(unsigned long addr
, size_t size
)
272 if (__builtin_constant_p(size
)) {
275 return memory_is_poisoned_1(addr
);
277 return memory_is_poisoned_2(addr
);
279 return memory_is_poisoned_4(addr
);
281 return memory_is_poisoned_8(addr
);
283 return memory_is_poisoned_16(addr
);
289 return memory_is_poisoned_n(addr
, size
);
292 static __always_inline
void check_memory_region_inline(unsigned long addr
,
293 size_t size
, bool write
,
294 unsigned long ret_ip
)
296 if (unlikely(size
== 0))
299 if (unlikely((void *)addr
<
300 kasan_shadow_to_mem((void *)KASAN_SHADOW_START
))) {
301 kasan_report(addr
, size
, write
, ret_ip
);
305 if (likely(!memory_is_poisoned(addr
, size
)))
308 kasan_report(addr
, size
, write
, ret_ip
);
311 static void check_memory_region(unsigned long addr
,
312 size_t size
, bool write
,
313 unsigned long ret_ip
)
315 check_memory_region_inline(addr
, size
, write
, ret_ip
);
318 void kasan_check_read(const void *p
, unsigned int size
)
320 check_memory_region((unsigned long)p
, size
, false, _RET_IP_
);
322 EXPORT_SYMBOL(kasan_check_read
);
324 void kasan_check_write(const void *p
, unsigned int size
)
326 check_memory_region((unsigned long)p
, size
, true, _RET_IP_
);
328 EXPORT_SYMBOL(kasan_check_write
);
331 void *memset(void *addr
, int c
, size_t len
)
333 check_memory_region((unsigned long)addr
, len
, true, _RET_IP_
);
335 return __memset(addr
, c
, len
);
339 void *memmove(void *dest
, const void *src
, size_t len
)
341 check_memory_region((unsigned long)src
, len
, false, _RET_IP_
);
342 check_memory_region((unsigned long)dest
, len
, true, _RET_IP_
);
344 return __memmove(dest
, src
, len
);
348 void *memcpy(void *dest
, const void *src
, size_t len
)
350 check_memory_region((unsigned long)src
, len
, false, _RET_IP_
);
351 check_memory_region((unsigned long)dest
, len
, true, _RET_IP_
);
353 return __memcpy(dest
, src
, len
);
356 void kasan_alloc_pages(struct page
*page
, unsigned int order
)
358 if (likely(!PageHighMem(page
)))
359 kasan_unpoison_shadow(page_address(page
), PAGE_SIZE
<< order
);
362 void kasan_free_pages(struct page
*page
, unsigned int order
)
364 if (likely(!PageHighMem(page
)))
365 kasan_poison_shadow(page_address(page
),
371 * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
372 * For larger allocations larger redzones are used.
374 static size_t optimal_redzone(size_t object_size
)
377 object_size
<= 64 - 16 ? 16 :
378 object_size
<= 128 - 32 ? 32 :
379 object_size
<= 512 - 64 ? 64 :
380 object_size
<= 4096 - 128 ? 128 :
381 object_size
<= (1 << 14) - 256 ? 256 :
382 object_size
<= (1 << 15) - 512 ? 512 :
383 object_size
<= (1 << 16) - 1024 ? 1024 : 2048;
387 void kasan_cache_create(struct kmem_cache
*cache
, size_t *size
,
388 unsigned long *flags
)
391 int orig_size
= *size
;
393 /* Add alloc meta. */
394 cache
->kasan_info
.alloc_meta_offset
= *size
;
395 *size
+= sizeof(struct kasan_alloc_meta
);
398 if (cache
->flags
& SLAB_DESTROY_BY_RCU
|| cache
->ctor
||
399 cache
->object_size
< sizeof(struct kasan_free_meta
)) {
400 cache
->kasan_info
.free_meta_offset
= *size
;
401 *size
+= sizeof(struct kasan_free_meta
);
403 redzone_adjust
= optimal_redzone(cache
->object_size
) -
404 (*size
- cache
->object_size
);
406 if (redzone_adjust
> 0)
407 *size
+= redzone_adjust
;
409 *size
= min(KMALLOC_MAX_SIZE
, max(*size
, cache
->object_size
+
410 optimal_redzone(cache
->object_size
)));
413 * If the metadata doesn't fit, don't enable KASAN at all.
415 if (*size
<= cache
->kasan_info
.alloc_meta_offset
||
416 *size
<= cache
->kasan_info
.free_meta_offset
) {
417 cache
->kasan_info
.alloc_meta_offset
= 0;
418 cache
->kasan_info
.free_meta_offset
= 0;
423 *flags
|= SLAB_KASAN
;
426 void kasan_cache_shrink(struct kmem_cache
*cache
)
428 quarantine_remove_cache(cache
);
431 void kasan_cache_destroy(struct kmem_cache
*cache
)
433 quarantine_remove_cache(cache
);
436 size_t kasan_metadata_size(struct kmem_cache
*cache
)
438 return (cache
->kasan_info
.alloc_meta_offset
?
439 sizeof(struct kasan_alloc_meta
) : 0) +
440 (cache
->kasan_info
.free_meta_offset
?
441 sizeof(struct kasan_free_meta
) : 0);
444 void kasan_poison_slab(struct page
*page
)
446 kasan_poison_shadow(page_address(page
),
447 PAGE_SIZE
<< compound_order(page
),
448 KASAN_KMALLOC_REDZONE
);
451 void kasan_unpoison_object_data(struct kmem_cache
*cache
, void *object
)
453 kasan_unpoison_shadow(object
, cache
->object_size
);
456 void kasan_poison_object_data(struct kmem_cache
*cache
, void *object
)
458 kasan_poison_shadow(object
,
459 round_up(cache
->object_size
, KASAN_SHADOW_SCALE_SIZE
),
460 KASAN_KMALLOC_REDZONE
);
463 static inline int in_irqentry_text(unsigned long ptr
)
465 return (ptr
>= (unsigned long)&__irqentry_text_start
&&
466 ptr
< (unsigned long)&__irqentry_text_end
) ||
467 (ptr
>= (unsigned long)&__softirqentry_text_start
&&
468 ptr
< (unsigned long)&__softirqentry_text_end
);
471 static inline void filter_irq_stacks(struct stack_trace
*trace
)
475 if (!trace
->nr_entries
)
477 for (i
= 0; i
< trace
->nr_entries
; i
++)
478 if (in_irqentry_text(trace
->entries
[i
])) {
479 /* Include the irqentry function into the stack. */
480 trace
->nr_entries
= i
+ 1;
485 static inline depot_stack_handle_t
save_stack(gfp_t flags
)
487 unsigned long entries
[KASAN_STACK_DEPTH
];
488 struct stack_trace trace
= {
491 .max_entries
= KASAN_STACK_DEPTH
,
495 save_stack_trace(&trace
);
496 filter_irq_stacks(&trace
);
497 if (trace
.nr_entries
!= 0 &&
498 trace
.entries
[trace
.nr_entries
-1] == ULONG_MAX
)
501 return depot_save_stack(&trace
, flags
);
504 static inline void set_track(struct kasan_track
*track
, gfp_t flags
)
506 track
->pid
= current
->pid
;
507 track
->stack
= save_stack(flags
);
510 struct kasan_alloc_meta
*get_alloc_info(struct kmem_cache
*cache
,
513 BUILD_BUG_ON(sizeof(struct kasan_alloc_meta
) > 32);
514 return (void *)object
+ cache
->kasan_info
.alloc_meta_offset
;
517 struct kasan_free_meta
*get_free_info(struct kmem_cache
*cache
,
520 BUILD_BUG_ON(sizeof(struct kasan_free_meta
) > 32);
521 return (void *)object
+ cache
->kasan_info
.free_meta_offset
;
524 void kasan_init_slab_obj(struct kmem_cache
*cache
, const void *object
)
526 struct kasan_alloc_meta
*alloc_info
;
528 if (!(cache
->flags
& SLAB_KASAN
))
531 alloc_info
= get_alloc_info(cache
, object
);
532 __memset(alloc_info
, 0, sizeof(*alloc_info
));
535 void kasan_slab_alloc(struct kmem_cache
*cache
, void *object
, gfp_t flags
)
537 kasan_kmalloc(cache
, object
, cache
->object_size
, flags
);
540 static void kasan_poison_slab_free(struct kmem_cache
*cache
, void *object
)
542 unsigned long size
= cache
->object_size
;
543 unsigned long rounded_up_size
= round_up(size
, KASAN_SHADOW_SCALE_SIZE
);
545 /* RCU slabs could be legally used after free within the RCU period */
546 if (unlikely(cache
->flags
& SLAB_DESTROY_BY_RCU
))
549 kasan_poison_shadow(object
, rounded_up_size
, KASAN_KMALLOC_FREE
);
552 bool kasan_slab_free(struct kmem_cache
*cache
, void *object
)
556 /* RCU slabs could be legally used after free within the RCU period */
557 if (unlikely(cache
->flags
& SLAB_DESTROY_BY_RCU
))
560 shadow_byte
= READ_ONCE(*(s8
*)kasan_mem_to_shadow(object
));
561 if (shadow_byte
< 0 || shadow_byte
>= KASAN_SHADOW_SCALE_SIZE
) {
562 kasan_report_double_free(cache
, object
, shadow_byte
);
566 kasan_poison_slab_free(cache
, object
);
568 if (unlikely(!(cache
->flags
& SLAB_KASAN
)))
571 set_track(&get_alloc_info(cache
, object
)->free_track
, GFP_NOWAIT
);
572 quarantine_put(get_free_info(cache
, object
), cache
);
576 void kasan_kmalloc(struct kmem_cache
*cache
, const void *object
, size_t size
,
579 unsigned long redzone_start
;
580 unsigned long redzone_end
;
582 if (gfpflags_allow_blocking(flags
))
585 if (unlikely(object
== NULL
))
588 redzone_start
= round_up((unsigned long)(object
+ size
),
589 KASAN_SHADOW_SCALE_SIZE
);
590 redzone_end
= round_up((unsigned long)object
+ cache
->object_size
,
591 KASAN_SHADOW_SCALE_SIZE
);
593 kasan_unpoison_shadow(object
, size
);
594 kasan_poison_shadow((void *)redzone_start
, redzone_end
- redzone_start
,
595 KASAN_KMALLOC_REDZONE
);
597 if (cache
->flags
& SLAB_KASAN
)
598 set_track(&get_alloc_info(cache
, object
)->alloc_track
, flags
);
600 EXPORT_SYMBOL(kasan_kmalloc
);
602 void kasan_kmalloc_large(const void *ptr
, size_t size
, gfp_t flags
)
605 unsigned long redzone_start
;
606 unsigned long redzone_end
;
608 if (gfpflags_allow_blocking(flags
))
611 if (unlikely(ptr
== NULL
))
614 page
= virt_to_page(ptr
);
615 redzone_start
= round_up((unsigned long)(ptr
+ size
),
616 KASAN_SHADOW_SCALE_SIZE
);
617 redzone_end
= (unsigned long)ptr
+ (PAGE_SIZE
<< compound_order(page
));
619 kasan_unpoison_shadow(ptr
, size
);
620 kasan_poison_shadow((void *)redzone_start
, redzone_end
- redzone_start
,
624 void kasan_krealloc(const void *object
, size_t size
, gfp_t flags
)
628 if (unlikely(object
== ZERO_SIZE_PTR
))
631 page
= virt_to_head_page(object
);
633 if (unlikely(!PageSlab(page
)))
634 kasan_kmalloc_large(object
, size
, flags
);
636 kasan_kmalloc(page
->slab_cache
, object
, size
, flags
);
639 void kasan_poison_kfree(void *ptr
)
643 page
= virt_to_head_page(ptr
);
645 if (unlikely(!PageSlab(page
)))
646 kasan_poison_shadow(ptr
, PAGE_SIZE
<< compound_order(page
),
649 kasan_poison_slab_free(page
->slab_cache
, ptr
);
652 void kasan_kfree_large(const void *ptr
)
654 struct page
*page
= virt_to_page(ptr
);
656 kasan_poison_shadow(ptr
, PAGE_SIZE
<< compound_order(page
),
660 int kasan_module_alloc(void *addr
, size_t size
)
664 unsigned long shadow_start
;
666 shadow_start
= (unsigned long)kasan_mem_to_shadow(addr
);
667 shadow_size
= round_up(size
>> KASAN_SHADOW_SCALE_SHIFT
,
670 if (WARN_ON(!PAGE_ALIGNED(shadow_start
)))
673 ret
= __vmalloc_node_range(shadow_size
, 1, shadow_start
,
674 shadow_start
+ shadow_size
,
675 GFP_KERNEL
| __GFP_HIGHMEM
| __GFP_ZERO
,
676 PAGE_KERNEL
, VM_NO_GUARD
, NUMA_NO_NODE
,
677 __builtin_return_address(0));
680 find_vm_area(addr
)->flags
|= VM_KASAN
;
681 kmemleak_ignore(ret
);
688 void kasan_free_shadow(const struct vm_struct
*vm
)
690 if (vm
->flags
& VM_KASAN
)
691 vfree(kasan_mem_to_shadow(vm
->addr
));
694 static void register_global(struct kasan_global
*global
)
696 size_t aligned_size
= round_up(global
->size
, KASAN_SHADOW_SCALE_SIZE
);
698 kasan_unpoison_shadow(global
->beg
, global
->size
);
700 kasan_poison_shadow(global
->beg
+ aligned_size
,
701 global
->size_with_redzone
- aligned_size
,
702 KASAN_GLOBAL_REDZONE
);
705 void __asan_register_globals(struct kasan_global
*globals
, size_t size
)
709 for (i
= 0; i
< size
; i
++)
710 register_global(&globals
[i
]);
712 EXPORT_SYMBOL(__asan_register_globals
);
714 void __asan_unregister_globals(struct kasan_global
*globals
, size_t size
)
717 EXPORT_SYMBOL(__asan_unregister_globals
);
719 #define DEFINE_ASAN_LOAD_STORE(size) \
720 void __asan_load##size(unsigned long addr) \
722 check_memory_region_inline(addr, size, false, _RET_IP_);\
724 EXPORT_SYMBOL(__asan_load##size); \
725 __alias(__asan_load##size) \
726 void __asan_load##size##_noabort(unsigned long); \
727 EXPORT_SYMBOL(__asan_load##size##_noabort); \
728 void __asan_store##size(unsigned long addr) \
730 check_memory_region_inline(addr, size, true, _RET_IP_); \
732 EXPORT_SYMBOL(__asan_store##size); \
733 __alias(__asan_store##size) \
734 void __asan_store##size##_noabort(unsigned long); \
735 EXPORT_SYMBOL(__asan_store##size##_noabort)
737 DEFINE_ASAN_LOAD_STORE(1);
738 DEFINE_ASAN_LOAD_STORE(2);
739 DEFINE_ASAN_LOAD_STORE(4);
740 DEFINE_ASAN_LOAD_STORE(8);
741 DEFINE_ASAN_LOAD_STORE(16);
743 void __asan_loadN(unsigned long addr
, size_t size
)
745 check_memory_region(addr
, size
, false, _RET_IP_
);
747 EXPORT_SYMBOL(__asan_loadN
);
749 __alias(__asan_loadN
)
750 void __asan_loadN_noabort(unsigned long, size_t);
751 EXPORT_SYMBOL(__asan_loadN_noabort
);
753 void __asan_storeN(unsigned long addr
, size_t size
)
755 check_memory_region(addr
, size
, true, _RET_IP_
);
757 EXPORT_SYMBOL(__asan_storeN
);
759 __alias(__asan_storeN
)
760 void __asan_storeN_noabort(unsigned long, size_t);
761 EXPORT_SYMBOL(__asan_storeN_noabort
);
763 /* to shut up compiler complaints */
764 void __asan_handle_no_return(void) {}
765 EXPORT_SYMBOL(__asan_handle_no_return
);
767 /* Emitted by compiler to poison large objects when they go out of scope. */
768 void __asan_poison_stack_memory(const void *addr
, size_t size
)
771 * Addr is KASAN_SHADOW_SCALE_SIZE-aligned and the object is surrounded
772 * by redzones, so we simply round up size to simplify logic.
774 kasan_poison_shadow(addr
, round_up(size
, KASAN_SHADOW_SCALE_SIZE
),
775 KASAN_USE_AFTER_SCOPE
);
777 EXPORT_SYMBOL(__asan_poison_stack_memory
);
779 /* Emitted by compiler to unpoison large objects when they go into scope. */
780 void __asan_unpoison_stack_memory(const void *addr
, size_t size
)
782 kasan_unpoison_shadow(addr
, size
);
784 EXPORT_SYMBOL(__asan_unpoison_stack_memory
);
786 #ifdef CONFIG_MEMORY_HOTPLUG
787 static int kasan_mem_notifier(struct notifier_block
*nb
,
788 unsigned long action
, void *data
)
790 return (action
== MEM_GOING_ONLINE
) ? NOTIFY_BAD
: NOTIFY_OK
;
793 static int __init
kasan_memhotplug_init(void)
795 pr_info("WARNING: KASAN doesn't support memory hot-add\n");
796 pr_info("Memory hot-add will be disabled\n");
798 hotplug_memory_notifier(kasan_mem_notifier
, 0);
803 module_init(kasan_memhotplug_init
);