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/sched/task_stack.h>
33 #include <linux/slab.h>
34 #include <linux/stacktrace.h>
35 #include <linux/string.h>
36 #include <linux/types.h>
37 #include <linux/vmalloc.h>
38 #include <linux/bug.h>
43 void kasan_enable_current(void)
45 current
->kasan_depth
++;
48 void kasan_disable_current(void)
50 current
->kasan_depth
--;
54 * Poisons the shadow memory for 'size' bytes starting from 'addr'.
55 * Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE.
57 static void kasan_poison_shadow(const void *address
, size_t size
, u8 value
)
59 void *shadow_start
, *shadow_end
;
61 shadow_start
= kasan_mem_to_shadow(address
);
62 shadow_end
= kasan_mem_to_shadow(address
+ size
);
64 memset(shadow_start
, value
, shadow_end
- shadow_start
);
67 void kasan_unpoison_shadow(const void *address
, size_t size
)
69 kasan_poison_shadow(address
, size
, 0);
71 if (size
& KASAN_SHADOW_MASK
) {
72 u8
*shadow
= (u8
*)kasan_mem_to_shadow(address
+ size
);
73 *shadow
= size
& KASAN_SHADOW_MASK
;
77 static void __kasan_unpoison_stack(struct task_struct
*task
, const void *sp
)
79 void *base
= task_stack_page(task
);
80 size_t size
= sp
- base
;
82 kasan_unpoison_shadow(base
, size
);
85 /* Unpoison the entire stack for a task. */
86 void kasan_unpoison_task_stack(struct task_struct
*task
)
88 __kasan_unpoison_stack(task
, task_stack_page(task
) + THREAD_SIZE
);
91 /* Unpoison the stack for the current task beyond a watermark sp value. */
92 asmlinkage
void kasan_unpoison_task_stack_below(const void *watermark
)
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).
99 void *base
= (void *)((unsigned long)watermark
& ~(THREAD_SIZE
- 1));
101 kasan_unpoison_shadow(base
, watermark
- base
);
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.
109 void kasan_unpoison_stack_above_sp_to(const void *watermark
)
111 const void *sp
= __builtin_frame_address(0);
112 size_t size
= watermark
- sp
;
114 if (WARN_ON(sp
> watermark
))
116 kasan_unpoison_shadow(sp
, size
);
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.
125 static __always_inline
bool memory_is_poisoned_1(unsigned long addr
)
127 s8 shadow_value
= *(s8
*)kasan_mem_to_shadow((void *)addr
);
129 if (unlikely(shadow_value
)) {
130 s8 last_accessible_byte
= addr
& KASAN_SHADOW_MASK
;
131 return unlikely(last_accessible_byte
>= shadow_value
);
137 static __always_inline
bool memory_is_poisoned_2_4_8(unsigned long addr
,
140 u8
*shadow_addr
= (u8
*)kasan_mem_to_shadow((void *)addr
);
143 * Access crosses 8(shadow size)-byte boundary. Such access maps
144 * into 2 shadow bytes, so we need to check them both.
146 if (unlikely(((addr
+ size
- 1) & KASAN_SHADOW_MASK
) < size
- 1))
147 return *shadow_addr
|| memory_is_poisoned_1(addr
+ size
- 1);
149 return memory_is_poisoned_1(addr
+ size
- 1);
152 static __always_inline
bool memory_is_poisoned_16(unsigned long addr
)
154 u16
*shadow_addr
= (u16
*)kasan_mem_to_shadow((void *)addr
);
156 /* Unaligned 16-bytes access maps into 3 shadow bytes. */
157 if (unlikely(!IS_ALIGNED(addr
, KASAN_SHADOW_SCALE_SIZE
)))
158 return *shadow_addr
|| memory_is_poisoned_1(addr
+ 15);
163 static __always_inline
unsigned long bytes_is_nonzero(const u8
*start
,
167 if (unlikely(*start
))
168 return (unsigned long)start
;
176 static __always_inline
unsigned long memory_is_nonzero(const void *start
,
181 unsigned int prefix
= (unsigned long)start
% 8;
183 if (end
- start
<= 16)
184 return bytes_is_nonzero(start
, end
- start
);
188 ret
= bytes_is_nonzero(start
, prefix
);
194 words
= (end
- start
) / 8;
196 if (unlikely(*(u64
*)start
))
197 return bytes_is_nonzero(start
, 8);
202 return bytes_is_nonzero(start
, (end
- start
) % 8);
205 static __always_inline
bool memory_is_poisoned_n(unsigned long addr
,
210 ret
= memory_is_nonzero(kasan_mem_to_shadow((void *)addr
),
211 kasan_mem_to_shadow((void *)addr
+ size
- 1) + 1);
214 unsigned long last_byte
= addr
+ size
- 1;
215 s8
*last_shadow
= (s8
*)kasan_mem_to_shadow((void *)last_byte
);
217 if (unlikely(ret
!= (unsigned long)last_shadow
||
218 ((long)(last_byte
& KASAN_SHADOW_MASK
) >= *last_shadow
)))
224 static __always_inline
bool memory_is_poisoned(unsigned long addr
, size_t size
)
226 if (__builtin_constant_p(size
)) {
229 return memory_is_poisoned_1(addr
);
233 return memory_is_poisoned_2_4_8(addr
, size
);
235 return memory_is_poisoned_16(addr
);
241 return memory_is_poisoned_n(addr
, size
);
244 static __always_inline
void check_memory_region_inline(unsigned long addr
,
245 size_t size
, bool write
,
246 unsigned long ret_ip
)
248 if (unlikely(size
== 0))
251 if (unlikely((void *)addr
<
252 kasan_shadow_to_mem((void *)KASAN_SHADOW_START
))) {
253 kasan_report(addr
, size
, write
, ret_ip
);
257 if (likely(!memory_is_poisoned(addr
, size
)))
260 kasan_report(addr
, size
, write
, ret_ip
);
263 static void check_memory_region(unsigned long addr
,
264 size_t size
, bool write
,
265 unsigned long ret_ip
)
267 check_memory_region_inline(addr
, size
, write
, ret_ip
);
270 void kasan_check_read(const void *p
, unsigned int size
)
272 check_memory_region((unsigned long)p
, size
, false, _RET_IP_
);
274 EXPORT_SYMBOL(kasan_check_read
);
276 void kasan_check_write(const void *p
, unsigned int size
)
278 check_memory_region((unsigned long)p
, size
, true, _RET_IP_
);
280 EXPORT_SYMBOL(kasan_check_write
);
283 void *memset(void *addr
, int c
, size_t len
)
285 check_memory_region((unsigned long)addr
, len
, true, _RET_IP_
);
287 return __memset(addr
, c
, len
);
291 void *memmove(void *dest
, const void *src
, size_t len
)
293 check_memory_region((unsigned long)src
, len
, false, _RET_IP_
);
294 check_memory_region((unsigned long)dest
, len
, true, _RET_IP_
);
296 return __memmove(dest
, src
, len
);
300 void *memcpy(void *dest
, const void *src
, size_t len
)
302 check_memory_region((unsigned long)src
, len
, false, _RET_IP_
);
303 check_memory_region((unsigned long)dest
, len
, true, _RET_IP_
);
305 return __memcpy(dest
, src
, len
);
308 void kasan_alloc_pages(struct page
*page
, unsigned int order
)
310 if (likely(!PageHighMem(page
)))
311 kasan_unpoison_shadow(page_address(page
), PAGE_SIZE
<< order
);
314 void kasan_free_pages(struct page
*page
, unsigned int order
)
316 if (likely(!PageHighMem(page
)))
317 kasan_poison_shadow(page_address(page
),
323 * Adaptive redzone policy taken from the userspace AddressSanitizer runtime.
324 * For larger allocations larger redzones are used.
326 static size_t optimal_redzone(size_t object_size
)
329 object_size
<= 64 - 16 ? 16 :
330 object_size
<= 128 - 32 ? 32 :
331 object_size
<= 512 - 64 ? 64 :
332 object_size
<= 4096 - 128 ? 128 :
333 object_size
<= (1 << 14) - 256 ? 256 :
334 object_size
<= (1 << 15) - 512 ? 512 :
335 object_size
<= (1 << 16) - 1024 ? 1024 : 2048;
339 void kasan_cache_create(struct kmem_cache
*cache
, size_t *size
,
340 unsigned long *flags
)
343 int orig_size
= *size
;
345 /* Add alloc meta. */
346 cache
->kasan_info
.alloc_meta_offset
= *size
;
347 *size
+= sizeof(struct kasan_alloc_meta
);
350 if (cache
->flags
& SLAB_TYPESAFE_BY_RCU
|| cache
->ctor
||
351 cache
->object_size
< sizeof(struct kasan_free_meta
)) {
352 cache
->kasan_info
.free_meta_offset
= *size
;
353 *size
+= sizeof(struct kasan_free_meta
);
355 redzone_adjust
= optimal_redzone(cache
->object_size
) -
356 (*size
- cache
->object_size
);
358 if (redzone_adjust
> 0)
359 *size
+= redzone_adjust
;
361 *size
= min(KMALLOC_MAX_SIZE
, max(*size
, cache
->object_size
+
362 optimal_redzone(cache
->object_size
)));
365 * If the metadata doesn't fit, don't enable KASAN at all.
367 if (*size
<= cache
->kasan_info
.alloc_meta_offset
||
368 *size
<= cache
->kasan_info
.free_meta_offset
) {
369 cache
->kasan_info
.alloc_meta_offset
= 0;
370 cache
->kasan_info
.free_meta_offset
= 0;
375 *flags
|= SLAB_KASAN
;
378 void kasan_cache_shrink(struct kmem_cache
*cache
)
380 quarantine_remove_cache(cache
);
383 void kasan_cache_shutdown(struct kmem_cache
*cache
)
385 quarantine_remove_cache(cache
);
388 size_t kasan_metadata_size(struct kmem_cache
*cache
)
390 return (cache
->kasan_info
.alloc_meta_offset
?
391 sizeof(struct kasan_alloc_meta
) : 0) +
392 (cache
->kasan_info
.free_meta_offset
?
393 sizeof(struct kasan_free_meta
) : 0);
396 void kasan_poison_slab(struct page
*page
)
398 kasan_poison_shadow(page_address(page
),
399 PAGE_SIZE
<< compound_order(page
),
400 KASAN_KMALLOC_REDZONE
);
403 void kasan_unpoison_object_data(struct kmem_cache
*cache
, void *object
)
405 kasan_unpoison_shadow(object
, cache
->object_size
);
408 void kasan_poison_object_data(struct kmem_cache
*cache
, void *object
)
410 kasan_poison_shadow(object
,
411 round_up(cache
->object_size
, KASAN_SHADOW_SCALE_SIZE
),
412 KASAN_KMALLOC_REDZONE
);
415 static inline int in_irqentry_text(unsigned long ptr
)
417 return (ptr
>= (unsigned long)&__irqentry_text_start
&&
418 ptr
< (unsigned long)&__irqentry_text_end
) ||
419 (ptr
>= (unsigned long)&__softirqentry_text_start
&&
420 ptr
< (unsigned long)&__softirqentry_text_end
);
423 static inline void filter_irq_stacks(struct stack_trace
*trace
)
427 if (!trace
->nr_entries
)
429 for (i
= 0; i
< trace
->nr_entries
; i
++)
430 if (in_irqentry_text(trace
->entries
[i
])) {
431 /* Include the irqentry function into the stack. */
432 trace
->nr_entries
= i
+ 1;
437 static inline depot_stack_handle_t
save_stack(gfp_t flags
)
439 unsigned long entries
[KASAN_STACK_DEPTH
];
440 struct stack_trace trace
= {
443 .max_entries
= KASAN_STACK_DEPTH
,
447 save_stack_trace(&trace
);
448 filter_irq_stacks(&trace
);
449 if (trace
.nr_entries
!= 0 &&
450 trace
.entries
[trace
.nr_entries
-1] == ULONG_MAX
)
453 return depot_save_stack(&trace
, flags
);
456 static inline void set_track(struct kasan_track
*track
, gfp_t flags
)
458 track
->pid
= current
->pid
;
459 track
->stack
= save_stack(flags
);
462 struct kasan_alloc_meta
*get_alloc_info(struct kmem_cache
*cache
,
465 BUILD_BUG_ON(sizeof(struct kasan_alloc_meta
) > 32);
466 return (void *)object
+ cache
->kasan_info
.alloc_meta_offset
;
469 struct kasan_free_meta
*get_free_info(struct kmem_cache
*cache
,
472 BUILD_BUG_ON(sizeof(struct kasan_free_meta
) > 32);
473 return (void *)object
+ cache
->kasan_info
.free_meta_offset
;
476 void kasan_init_slab_obj(struct kmem_cache
*cache
, const void *object
)
478 struct kasan_alloc_meta
*alloc_info
;
480 if (!(cache
->flags
& SLAB_KASAN
))
483 alloc_info
= get_alloc_info(cache
, object
);
484 __memset(alloc_info
, 0, sizeof(*alloc_info
));
487 void kasan_slab_alloc(struct kmem_cache
*cache
, void *object
, gfp_t flags
)
489 kasan_kmalloc(cache
, object
, cache
->object_size
, flags
);
492 static void kasan_poison_slab_free(struct kmem_cache
*cache
, void *object
)
494 unsigned long size
= cache
->object_size
;
495 unsigned long rounded_up_size
= round_up(size
, KASAN_SHADOW_SCALE_SIZE
);
497 /* RCU slabs could be legally used after free within the RCU period */
498 if (unlikely(cache
->flags
& SLAB_TYPESAFE_BY_RCU
))
501 kasan_poison_shadow(object
, rounded_up_size
, KASAN_KMALLOC_FREE
);
504 bool kasan_slab_free(struct kmem_cache
*cache
, void *object
)
508 /* RCU slabs could be legally used after free within the RCU period */
509 if (unlikely(cache
->flags
& SLAB_TYPESAFE_BY_RCU
))
512 shadow_byte
= READ_ONCE(*(s8
*)kasan_mem_to_shadow(object
));
513 if (shadow_byte
< 0 || shadow_byte
>= KASAN_SHADOW_SCALE_SIZE
) {
514 kasan_report_double_free(cache
, object
,
515 __builtin_return_address(1));
519 kasan_poison_slab_free(cache
, object
);
521 if (unlikely(!(cache
->flags
& SLAB_KASAN
)))
524 set_track(&get_alloc_info(cache
, object
)->free_track
, GFP_NOWAIT
);
525 quarantine_put(get_free_info(cache
, object
), cache
);
529 void kasan_kmalloc(struct kmem_cache
*cache
, const void *object
, size_t size
,
532 unsigned long redzone_start
;
533 unsigned long redzone_end
;
535 if (gfpflags_allow_blocking(flags
))
538 if (unlikely(object
== NULL
))
541 redzone_start
= round_up((unsigned long)(object
+ size
),
542 KASAN_SHADOW_SCALE_SIZE
);
543 redzone_end
= round_up((unsigned long)object
+ cache
->object_size
,
544 KASAN_SHADOW_SCALE_SIZE
);
546 kasan_unpoison_shadow(object
, size
);
547 kasan_poison_shadow((void *)redzone_start
, redzone_end
- redzone_start
,
548 KASAN_KMALLOC_REDZONE
);
550 if (cache
->flags
& SLAB_KASAN
)
551 set_track(&get_alloc_info(cache
, object
)->alloc_track
, flags
);
553 EXPORT_SYMBOL(kasan_kmalloc
);
555 void kasan_kmalloc_large(const void *ptr
, size_t size
, gfp_t flags
)
558 unsigned long redzone_start
;
559 unsigned long redzone_end
;
561 if (gfpflags_allow_blocking(flags
))
564 if (unlikely(ptr
== NULL
))
567 page
= virt_to_page(ptr
);
568 redzone_start
= round_up((unsigned long)(ptr
+ size
),
569 KASAN_SHADOW_SCALE_SIZE
);
570 redzone_end
= (unsigned long)ptr
+ (PAGE_SIZE
<< compound_order(page
));
572 kasan_unpoison_shadow(ptr
, size
);
573 kasan_poison_shadow((void *)redzone_start
, redzone_end
- redzone_start
,
577 void kasan_krealloc(const void *object
, size_t size
, gfp_t flags
)
581 if (unlikely(object
== ZERO_SIZE_PTR
))
584 page
= virt_to_head_page(object
);
586 if (unlikely(!PageSlab(page
)))
587 kasan_kmalloc_large(object
, size
, flags
);
589 kasan_kmalloc(page
->slab_cache
, object
, size
, flags
);
592 void kasan_poison_kfree(void *ptr
)
596 page
= virt_to_head_page(ptr
);
598 if (unlikely(!PageSlab(page
)))
599 kasan_poison_shadow(ptr
, PAGE_SIZE
<< compound_order(page
),
602 kasan_poison_slab_free(page
->slab_cache
, ptr
);
605 void kasan_kfree_large(const void *ptr
)
607 struct page
*page
= virt_to_page(ptr
);
609 kasan_poison_shadow(ptr
, PAGE_SIZE
<< compound_order(page
),
613 int kasan_module_alloc(void *addr
, size_t size
)
617 unsigned long shadow_start
;
619 shadow_start
= (unsigned long)kasan_mem_to_shadow(addr
);
620 shadow_size
= round_up(size
>> KASAN_SHADOW_SCALE_SHIFT
,
623 if (WARN_ON(!PAGE_ALIGNED(shadow_start
)))
626 ret
= __vmalloc_node_range(shadow_size
, 1, shadow_start
,
627 shadow_start
+ shadow_size
,
628 GFP_KERNEL
| __GFP_ZERO
,
629 PAGE_KERNEL
, VM_NO_GUARD
, NUMA_NO_NODE
,
630 __builtin_return_address(0));
633 find_vm_area(addr
)->flags
|= VM_KASAN
;
634 kmemleak_ignore(ret
);
641 void kasan_free_shadow(const struct vm_struct
*vm
)
643 if (vm
->flags
& VM_KASAN
)
644 vfree(kasan_mem_to_shadow(vm
->addr
));
647 static void register_global(struct kasan_global
*global
)
649 size_t aligned_size
= round_up(global
->size
, KASAN_SHADOW_SCALE_SIZE
);
651 kasan_unpoison_shadow(global
->beg
, global
->size
);
653 kasan_poison_shadow(global
->beg
+ aligned_size
,
654 global
->size_with_redzone
- aligned_size
,
655 KASAN_GLOBAL_REDZONE
);
658 void __asan_register_globals(struct kasan_global
*globals
, size_t size
)
662 for (i
= 0; i
< size
; i
++)
663 register_global(&globals
[i
]);
665 EXPORT_SYMBOL(__asan_register_globals
);
667 void __asan_unregister_globals(struct kasan_global
*globals
, size_t size
)
670 EXPORT_SYMBOL(__asan_unregister_globals
);
672 #define DEFINE_ASAN_LOAD_STORE(size) \
673 void __asan_load##size(unsigned long addr) \
675 check_memory_region_inline(addr, size, false, _RET_IP_);\
677 EXPORT_SYMBOL(__asan_load##size); \
678 __alias(__asan_load##size) \
679 void __asan_load##size##_noabort(unsigned long); \
680 EXPORT_SYMBOL(__asan_load##size##_noabort); \
681 void __asan_store##size(unsigned long addr) \
683 check_memory_region_inline(addr, size, true, _RET_IP_); \
685 EXPORT_SYMBOL(__asan_store##size); \
686 __alias(__asan_store##size) \
687 void __asan_store##size##_noabort(unsigned long); \
688 EXPORT_SYMBOL(__asan_store##size##_noabort)
690 DEFINE_ASAN_LOAD_STORE(1);
691 DEFINE_ASAN_LOAD_STORE(2);
692 DEFINE_ASAN_LOAD_STORE(4);
693 DEFINE_ASAN_LOAD_STORE(8);
694 DEFINE_ASAN_LOAD_STORE(16);
696 void __asan_loadN(unsigned long addr
, size_t size
)
698 check_memory_region(addr
, size
, false, _RET_IP_
);
700 EXPORT_SYMBOL(__asan_loadN
);
702 __alias(__asan_loadN
)
703 void __asan_loadN_noabort(unsigned long, size_t);
704 EXPORT_SYMBOL(__asan_loadN_noabort
);
706 void __asan_storeN(unsigned long addr
, size_t size
)
708 check_memory_region(addr
, size
, true, _RET_IP_
);
710 EXPORT_SYMBOL(__asan_storeN
);
712 __alias(__asan_storeN
)
713 void __asan_storeN_noabort(unsigned long, size_t);
714 EXPORT_SYMBOL(__asan_storeN_noabort
);
716 /* to shut up compiler complaints */
717 void __asan_handle_no_return(void) {}
718 EXPORT_SYMBOL(__asan_handle_no_return
);
720 /* Emitted by compiler to poison large objects when they go out of scope. */
721 void __asan_poison_stack_memory(const void *addr
, size_t size
)
724 * Addr is KASAN_SHADOW_SCALE_SIZE-aligned and the object is surrounded
725 * by redzones, so we simply round up size to simplify logic.
727 kasan_poison_shadow(addr
, round_up(size
, KASAN_SHADOW_SCALE_SIZE
),
728 KASAN_USE_AFTER_SCOPE
);
730 EXPORT_SYMBOL(__asan_poison_stack_memory
);
732 /* Emitted by compiler to unpoison large objects when they go into scope. */
733 void __asan_unpoison_stack_memory(const void *addr
, size_t size
)
735 kasan_unpoison_shadow(addr
, size
);
737 EXPORT_SYMBOL(__asan_unpoison_stack_memory
);
739 #ifdef CONFIG_MEMORY_HOTPLUG
740 static int __meminit
kasan_mem_notifier(struct notifier_block
*nb
,
741 unsigned long action
, void *data
)
743 struct memory_notify
*mem_data
= data
;
744 unsigned long nr_shadow_pages
, start_kaddr
, shadow_start
;
745 unsigned long shadow_end
, shadow_size
;
747 nr_shadow_pages
= mem_data
->nr_pages
>> KASAN_SHADOW_SCALE_SHIFT
;
748 start_kaddr
= (unsigned long)pfn_to_kaddr(mem_data
->start_pfn
);
749 shadow_start
= (unsigned long)kasan_mem_to_shadow((void *)start_kaddr
);
750 shadow_size
= nr_shadow_pages
<< PAGE_SHIFT
;
751 shadow_end
= shadow_start
+ shadow_size
;
753 if (WARN_ON(mem_data
->nr_pages
% KASAN_SHADOW_SCALE_SIZE
) ||
754 WARN_ON(start_kaddr
% (KASAN_SHADOW_SCALE_SIZE
<< PAGE_SHIFT
)))
758 case MEM_GOING_ONLINE
: {
761 ret
= __vmalloc_node_range(shadow_size
, PAGE_SIZE
, shadow_start
,
762 shadow_end
, GFP_KERNEL
,
763 PAGE_KERNEL
, VM_NO_GUARD
,
764 pfn_to_nid(mem_data
->start_pfn
),
765 __builtin_return_address(0));
769 kmemleak_ignore(ret
);
773 vfree((void *)shadow_start
);
779 static int __init
kasan_memhotplug_init(void)
781 hotplug_memory_notifier(kasan_mem_notifier
, 0);
786 module_init(kasan_memhotplug_init
);