Skip to content

Commit 080eb83

Browse files
xairytorvalds
authored andcommitted
kasan: initialize shadow to 0xff for tag-based mode
A tag-based KASAN shadow memory cell contains a memory tag, that corresponds to the tag in the top byte of the pointer, that points to that memory. The native top byte value of kernel pointers is 0xff, so with tag-based KASAN we need to initialize shadow memory to 0xff. [cai@lca.pw: arm64: skip kmemleak for KASAN again\ Link: http://lkml.kernel.org/r/20181226020550.63712-1-cai@lca.pw Link: http://lkml.kernel.org/r/5cc1b789aad7c99cf4f3ec5b328b147ad53edb40.1544099024.git.andreyknvl@google.com Signed-off-by: Andrey Konovalov <andreyknvl@google.com> Reviewed-by: Andrey Ryabinin <aryabinin@virtuozzo.com> Reviewed-by: Dmitry Vyukov <dvyukov@google.com> Cc: Christoph Lameter <cl@linux.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Will Deacon <will.deacon@arm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 9577dd7 commit 080eb83

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

arch/arm64/mm/kasan_init.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ static phys_addr_t __init kasan_alloc_zeroed_page(int node)
4343
return __pa(p);
4444
}
4545

46+
static phys_addr_t __init kasan_alloc_raw_page(int node)
47+
{
48+
void *p = memblock_alloc_try_nid_raw(PAGE_SIZE, PAGE_SIZE,
49+
__pa(MAX_DMA_ADDRESS),
50+
MEMBLOCK_ALLOC_KASAN, node);
51+
return __pa(p);
52+
}
53+
4654
static pte_t *__init kasan_pte_offset(pmd_t *pmdp, unsigned long addr, int node,
4755
bool early)
4856
{
@@ -92,7 +100,9 @@ static void __init kasan_pte_populate(pmd_t *pmdp, unsigned long addr,
92100
do {
93101
phys_addr_t page_phys = early ?
94102
__pa_symbol(kasan_early_shadow_page)
95-
: kasan_alloc_zeroed_page(node);
103+
: kasan_alloc_raw_page(node);
104+
if (!early)
105+
memset(__va(page_phys), KASAN_SHADOW_INIT, PAGE_SIZE);
96106
next = addr + PAGE_SIZE;
97107
set_pte(ptep, pfn_pte(__phys_to_pfn(page_phys), PAGE_KERNEL));
98108
} while (ptep++, addr = next, addr != end && pte_none(READ_ONCE(*ptep)));
@@ -239,7 +249,7 @@ void __init kasan_init(void)
239249
pfn_pte(sym_to_pfn(kasan_early_shadow_page),
240250
PAGE_KERNEL_RO));
241251

242-
memset(kasan_early_shadow_page, 0, PAGE_SIZE);
252+
memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
243253
cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
244254

245255
/* At this point kasan is fully initialized. Enable error messages */

include/linux/kasan.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ static inline size_t kasan_metadata_size(struct kmem_cache *cache) { return 0; }
153153

154154
#ifdef CONFIG_KASAN_GENERIC
155155

156+
#define KASAN_SHADOW_INIT 0
157+
156158
void kasan_cache_shrink(struct kmem_cache *cache);
157159
void kasan_cache_shutdown(struct kmem_cache *cache);
158160

@@ -163,4 +165,10 @@ static inline void kasan_cache_shutdown(struct kmem_cache *cache) {}
163165

164166
#endif /* CONFIG_KASAN_GENERIC */
165167

168+
#ifdef CONFIG_KASAN_SW_TAGS
169+
170+
#define KASAN_SHADOW_INIT 0xFF
171+
172+
#endif /* CONFIG_KASAN_SW_TAGS */
173+
166174
#endif /* LINUX_KASAN_H */

mm/kasan/common.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,12 @@ int kasan_module_alloc(void *addr, size_t size)
473473

474474
ret = __vmalloc_node_range(shadow_size, 1, shadow_start,
475475
shadow_start + shadow_size,
476-
GFP_KERNEL | __GFP_ZERO,
476+
GFP_KERNEL,
477477
PAGE_KERNEL, VM_NO_GUARD, NUMA_NO_NODE,
478478
__builtin_return_address(0));
479479

480480
if (ret) {
481+
__memset(ret, KASAN_SHADOW_INIT, shadow_size);
481482
find_vm_area(addr)->flags |= VM_KASAN;
482483
kmemleak_ignore(ret);
483484
return 0;

0 commit comments

Comments
 (0)