Skip to content

Commit 5615f69

Browse files
linuswRussell King
authored and
Russell King
committed
ARM: 9016/2: Initialize the mapping of KASan shadow memory
This patch initializes KASan shadow region's page table and memory. There are two stage for KASan initializing: 1. At early boot stage the whole shadow region is mapped to just one physical page (kasan_zero_page). It is finished by the function kasan_early_init which is called by __mmap_switched(arch/arm/kernel/ head-common.S) 2. After the calling of paging_init, we use kasan_zero_page as zero shadow for some memory that KASan does not need to track, and we allocate a new shadow space for the other memory that KASan need to track. These issues are finished by the function kasan_init which is call by setup_arch. When using KASan we also need to increase the THREAD_SIZE_ORDER from 1 to 2 as the extra calls for shadow memory uses quite a bit of stack. As we need to make a temporary copy of the PGD when setting up shadow memory we create a helpful PGD_SIZE definition for both LPAE and non-LPAE setups. The KASan core code unconditionally calls pud_populate() so this needs to be changed from BUG() to do {} while (0) when building with KASan enabled. After the initial development by Andre Ryabinin several modifications have been made to this code: Abbott Liu <[email protected]> - Add support ARM LPAE: If LPAE is enabled, KASan shadow region's mapping table need be copied in the pgd_alloc() function. - Change kasan_pte_populate,kasan_pmd_populate,kasan_pud_populate, kasan_pgd_populate from .meminit.text section to .init.text section. Reported by Florian Fainelli <[email protected]> Linus Walleij <[email protected]>: - Drop the custom mainpulation of TTBR0 and just use cpu_switch_mm() to switch the pgd table. - Adopt to handle 4th level page tabel folding. - Rewrite the entire page directory and page entry initialization sequence to be recursive based on ARM64:s kasan_init.c. Ard Biesheuvel <[email protected]>: - Necessary underlying fixes. - Crucial bug fixes to the memory set-up code. Co-developed-by: Andrey Ryabinin <[email protected]> Co-developed-by: Abbott Liu <[email protected]> Co-developed-by: Ard Biesheuvel <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: [email protected] Cc: Mike Rapoport <[email protected]> Acked-by: Mike Rapoport <[email protected]> Reviewed-by: Ard Biesheuvel <[email protected]> Tested-by: Ard Biesheuvel <[email protected]> # QEMU/KVM/mach-virt/LPAE/8G Tested-by: Florian Fainelli <[email protected]> # Brahma SoCs Tested-by: Ahmad Fatoum <[email protected]> # i.MX6Q Reported-by: Russell King - ARM Linux <[email protected]> Reported-by: Florian Fainelli <[email protected]> Signed-off-by: Andrey Ryabinin <[email protected]> Signed-off-by: Abbott Liu <[email protected]> Signed-off-by: Florian Fainelli <[email protected]> Signed-off-by: Ard Biesheuvel <[email protected]> Signed-off-by: Linus Walleij <[email protected]> Signed-off-by: Russell King <[email protected]>
1 parent c12366b commit 5615f69

File tree

8 files changed

+362
-2
lines changed

8 files changed

+362
-2
lines changed

arch/arm/include/asm/kasan.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* arch/arm/include/asm/kasan.h
4+
*
5+
* Copyright (c) 2015 Samsung Electronics Co., Ltd.
6+
* Author: Andrey Ryabinin <[email protected]>
7+
*
8+
*/
9+
10+
#ifndef __ASM_KASAN_H
11+
#define __ASM_KASAN_H
12+
13+
#ifdef CONFIG_KASAN
14+
15+
#include <asm/kasan_def.h>
16+
17+
#define KASAN_SHADOW_SCALE_SHIFT 3
18+
19+
/*
20+
* The compiler uses a shadow offset assuming that addresses start
21+
* from 0. Kernel addresses don't start from 0, so shadow
22+
* for kernel really starts from 'compiler's shadow offset' +
23+
* ('kernel address space start' >> KASAN_SHADOW_SCALE_SHIFT)
24+
*/
25+
26+
asmlinkage void kasan_early_init(void);
27+
extern void kasan_init(void);
28+
29+
#else
30+
static inline void kasan_init(void) { }
31+
#endif
32+
33+
#endif

arch/arm/include/asm/pgalloc.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,27 @@
2121
#define _PAGE_KERNEL_TABLE (PMD_TYPE_TABLE | PMD_BIT4 | PMD_DOMAIN(DOMAIN_KERNEL))
2222

2323
#ifdef CONFIG_ARM_LPAE
24+
#define PGD_SIZE (PTRS_PER_PGD * sizeof(pgd_t))
2425

2526
static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
2627
{
2728
set_pud(pud, __pud(__pa(pmd) | PMD_TYPE_TABLE));
2829
}
2930

3031
#else /* !CONFIG_ARM_LPAE */
32+
#define PGD_SIZE (PAGE_SIZE << 2)
3133

3234
/*
3335
* Since we have only two-level page tables, these are trivial
3436
*/
3537
#define pmd_alloc_one(mm,addr) ({ BUG(); ((pmd_t *)2); })
3638
#define pmd_free(mm, pmd) do { } while (0)
39+
#ifdef CONFIG_KASAN
40+
/* The KASan core unconditionally calls pud_populate() on all architectures */
41+
#define pud_populate(mm,pmd,pte) do { } while (0)
42+
#else
3743
#define pud_populate(mm,pmd,pte) BUG()
38-
44+
#endif
3945
#endif /* CONFIG_ARM_LPAE */
4046

4147
extern pgd_t *pgd_alloc(struct mm_struct *mm);

arch/arm/include/asm/thread_info.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@
1313
#include <asm/fpstate.h>
1414
#include <asm/page.h>
1515

16+
#ifdef CONFIG_KASAN
17+
/*
18+
* KASan uses a lot of extra stack space so the thread size order needs to
19+
* be increased.
20+
*/
21+
#define THREAD_SIZE_ORDER 2
22+
#else
1623
#define THREAD_SIZE_ORDER 1
24+
#endif
1725
#define THREAD_SIZE (PAGE_SIZE << THREAD_SIZE_ORDER)
1826
#define THREAD_START_SP (THREAD_SIZE - 8)
1927

arch/arm/kernel/head-common.S

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ __mmap_switched:
111111
str r8, [r2] @ Save atags pointer
112112
cmp r3, #0
113113
strne r10, [r3] @ Save control register values
114+
#ifdef CONFIG_KASAN
115+
bl kasan_early_init
116+
#endif
114117
mov lr, #0
115118
b start_kernel
116119
ENDPROC(__mmap_switched)

arch/arm/kernel/setup.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include <asm/unwind.h>
6060
#include <asm/memblock.h>
6161
#include <asm/virt.h>
62+
#include <asm/kasan.h>
6263

6364
#include "atags.h"
6465

@@ -1145,6 +1146,7 @@ void __init setup_arch(char **cmdline_p)
11451146
early_ioremap_reset();
11461147

11471148
paging_init(mdesc);
1149+
kasan_init();
11481150
request_standard_resources(mdesc);
11491151

11501152
if (mdesc->restart)

arch/arm/mm/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,6 @@ obj-$(CONFIG_CACHE_L2X0_PMU) += cache-l2x0-pmu.o
113113
obj-$(CONFIG_CACHE_XSC3L2) += cache-xsc3l2.o
114114
obj-$(CONFIG_CACHE_TAUROS2) += cache-tauros2.o
115115
obj-$(CONFIG_CACHE_UNIPHIER) += cache-uniphier.o
116+
117+
KASAN_SANITIZE_kasan_init.o := n
118+
obj-$(CONFIG_KASAN) += kasan_init.o

0 commit comments

Comments
 (0)