Skip to contentSkip to conversation

arm64: kasan: support CONFIG_KASAN_VMALLOC

PATCH Superseded Lecopzer Chen 4 participants 11 messages Jan 3, 2021 – Jan 9, 2021

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover arm64: kasan: support CONFIG_KASAN_VMALLOC

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

Test environment:
4G and 8G Qemu virt,
39-bit VA + 4k PAGE_SIZE with 3-level page table, test by lib/test_kasan.ko and lib/test_kasan_module.ko

It also works in Kaslr with CONFIG_RANDOMIZE_MODULE_REGION_FULL, but not test for HW_TAG(I have no proper device), thus keep HW_TAG and KASAN_VMALLOC mutual exclusion until confirming the functionality.

[1]: commit 0609ae011deb41c ("x86/kasan: support KASAN_VMALLOC")

Lecopzer Chen (3):
arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC
arm64: kasan: abstract _text and _end to KERNEL_START/END
arm64: Kconfig: support CONFIG_KASAN_VMALLOC

 arch/arm64/Kconfig         |  1 +
 arch/arm64/mm/kasan_init.c | 29 +++++++++++++++++++++--------
 2 files changed, 22 insertions(+), 8 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/3 arm64: kasan: don't populate vmalloc area for CONFIG_KASAN_VMALLOC

Linux support KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Like how the MODULES_VADDR does now, just not to early populate the VMALLOC_START between VMALLOC_END.
similarly, the kernel code mapping is now in the VMALLOC area and should keep these area populated.

---
 arch/arm64/mm/kasan_init.c | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)
arch/arm64/mm/kasan_init.c +18 -5
index d8e66c78440e..d7ad3f1e9c4d 100644
@@ -214,6 +214,7 @@ static void __init kasan_init_shadow(void)
 {
 	u64 kimg_shadow_start, kimg_shadow_end;
 	u64 mod_shadow_start, mod_shadow_end;
+	u64 vmalloc_shadow_start, vmalloc_shadow_end;
 	phys_addr_t pa_start, pa_end;
 	u64 i;
 
@@ -223,6 +224,9 @@ static void __init kasan_init_shadow(void)
 	mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
 	mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
 
+	vmalloc_shadow_start = (u64)kasan_mem_to_shadow((void *)VMALLOC_START);
+	vmalloc_shadow_end = (u64)kasan_mem_to_shadow((void *)VMALLOC_END);
+
 	/*
 	 * We are going to perform proper setup of shadow memory.
 	 * At first we should unmap early shadow (clear_pgds() call below).
@@ -241,12 +245,21 @@ static void __init kasan_init_shadow(void)
 
 	kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
 				   (void *)mod_shadow_start);
-	kasan_populate_early_shadow((void *)kimg_shadow_end,
-				   (void *)KASAN_SHADOW_END);
+	if (IS_ENABLED(CONFIG_KASAN_VMALLOC)) {
+		kasan_populate_early_shadow((void *)vmalloc_shadow_end,
+					   (void *)KASAN_SHADOW_END);
+		if (vmalloc_shadow_start > mod_shadow_end)
+			kasan_populate_early_shadow((void *)mod_shadow_end,
+						    (void *)vmalloc_shadow_start);
+
+	}	else {
+		kasan_populate_early_shadow((void *)kimg_shadow_end,
+					   (void *)KASAN_SHADOW_END);
+		if (kimg_shadow_start > mod_shadow_end)
+			kasan_populate_early_shadow((void *)mod_shadow_end,
+						    (void *)kimg_shadow_start);
+	}
 
-	if (kimg_shadow_start > mod_shadow_end)
-		kasan_populate_early_shadow((void *)mod_shadow_end,
-					    (void *)kimg_shadow_start);
 
 	for_each_mem_range(i, &pa_start, &pa_end) {
 		void *start = (void *)__phys_to_virt(pa_start);
Signature · 1 line
2.25.1
#2 lore raw

PATCH 2/3 arm64: kasan: abstract _text and _end to KERNEL_START/END

In reply to Lecopzer Chen

Arm64 provide defined macro for KERNEL_START and KERNEL_END, thus replace by the abstration instead of using _text and _end directly.

---
 arch/arm64/mm/kasan_init.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/mm/kasan_init.c +3 -3
index d7ad3f1e9c4d..acb549951f87 100644
@@ -218,8 +218,8 @@ static void __init kasan_init_shadow(void)
 	phys_addr_t pa_start, pa_end;
 	u64 i;
 
-	kimg_shadow_start = (u64)kasan_mem_to_shadow(_text) & PAGE_MASK;
-	kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(_end));
+	kimg_shadow_start = (u64)kasan_mem_to_shadow(KERNEL_START) & PAGE_MASK;
+	kimg_shadow_end = PAGE_ALIGN((u64)kasan_mem_to_shadow(KERNEL_END));
 
 	mod_shadow_start = (u64)kasan_mem_to_shadow((void *)MODULES_VADDR);
 	mod_shadow_end = (u64)kasan_mem_to_shadow((void *)MODULES_END);
@@ -241,7 +241,7 @@ static void __init kasan_init_shadow(void)
 	clear_pgds(KASAN_SHADOW_START, KASAN_SHADOW_END);
 
 	kasan_map_populate(kimg_shadow_start, kimg_shadow_end,
-			   early_pfn_to_nid(virt_to_pfn(lm_alias(_text))));
+			   early_pfn_to_nid(virt_to_pfn(lm_alias(KERNEL_START))));
 
 	kasan_populate_early_shadow(kasan_mem_to_shadow((void *)PAGE_END),
 				   (void *)mod_shadow_start);
Signature · 1 line
2.25.1
#3 lore raw

PATCH 3/3 arm64: Kconfig: support CONFIG_KASAN_VMALLOC

In reply to Lecopzer Chen

Now I have no device to test for HW_TAG, so keep it not selected until someone can test this.

---
 arch/arm64/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/Kconfig +1 -0
index 05e17351e4f3..29ab35aab59e 100644
@@ -136,6 +136,7 @@ config ARM64
 	select HAVE_ARCH_JUMP_LABEL
 	select HAVE_ARCH_JUMP_LABEL_RELATIVE
 	select HAVE_ARCH_KASAN if !(ARM64_16K_PAGES && ARM64_VA_BITS_48)
+	select HAVE_ARCH_KASAN_VMALLOC if (HAVE_ARCH_KASAN && !KASAN_HW_TAGS)
 	select HAVE_ARCH_KASAN_SW_TAGS if HAVE_ARCH_KASAN
 	select HAVE_ARCH_KASAN_HW_TAGS if (HAVE_ARCH_KASAN && ARM64_MTE)
 	select HAVE_ARCH_KGDB
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH 3/3] arm64: Kconfig: support CONFIG_KASAN_VMALLOC

Quoted 14 lines · Lecopzer Chen

Now I have no device to test for HW_TAG, so keep it not selected until someone can test this.

---
 arch/arm64/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/arm64/Kconfig +0 -0
index 05e17351e4f3..29ab35aab59e 100644

arch/arm64/Kconfig config ARM64 · line 139

Quoted 4 lines
arch/arm64/Kconfig +1 -0
@@ -136,6 +136,7 @@ config ARM64
        select HAVE_ARCH_JUMP_LABEL
        select HAVE_ARCH_JUMP_LABEL_RELATIVE
        select HAVE_ARCH_KASAN if !(ARM64_16K_PAGES && ARM64_VA_BITS_48)
+       select HAVE_ARCH_KASAN_VMALLOC if (HAVE_ARCH_KASAN && !KASAN_HW_TAGS)

KASAN_VMALLOC currently "depends on" KASAN_GENERIC. I think we should either do "HAVE_ARCH_KASAN && KASAN_GENERIC" here as well, or just do "if HAVE_ARCH_KASAN".

Quoted 10 lines
arch/arm64/Kconfig +0 -1
        select HAVE_ARCH_KASAN_SW_TAGS if HAVE_ARCH_KASAN
        select HAVE_ARCH_KASAN_HW_TAGS if (HAVE_ARCH_KASAN && ARM64_MTE)
        select HAVE_ARCH_KGDB
--

2.25.1

--
You received this message because you are subscribed to the Google Groups "kasan-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20210103171137.153834-4-lecopzer%40gmail.com.

Acked-by Tested-by on the v1 series
#6 lore raw
In reply to Lecopzer Chen
Quoted 19 lines · Lecopzer Chen

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

Test environment:
4G and 8G Qemu virt,
39-bit VA + 4k PAGE_SIZE with 3-level page table, test by lib/test_kasan.ko and lib/test_kasan_module.ko

It also works in Kaslr with CONFIG_RANDOMIZE_MODULE_REGION_FULL, but not test for HW_TAG(I have no proper device), thus keep HW_TAG and KASAN_VMALLOC mutual exclusion until confirming the functionality.

Quoted 3 lines

[1]: commit 0609ae011deb41c ("x86/kasan: support KASAN_VMALLOC")

Hi Lecopzer,

Thanks for working on this!

for the series along with the other two patches minding the nit in patch #3.

Will, Catalin, could you please take a look at the arm changes?

Thanks!

#9 lore raw
In reply to Lecopzer Chen
Quoted 13 lines · Lecopzer Chen

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

Test environment:
4G and 8G Qemu virt,
39-bit VA + 4k PAGE_SIZE with 3-level page table, test by lib/test_kasan.ko and lib/test_kasan_module.ko

Quoted 4 lines

It also works in Kaslr with CONFIG_RANDOMIZE_MODULE_REGION_FULL, but not test for HW_TAG(I have no proper device), thus keep HW_TAG and KASAN_VMALLOC mutual exclusion until confirming the functionality.

Re this: it makes sense to introduce vmalloc support one step a time and add SW_TAGS support before taking on HW_TAGS. SW_TAGS doesn't require any special hardware. Working on SW_TAGS first will also allow dealing with potential conflicts between vmalloc and tags without having MTE in the picture as well. Just FYI, no need to include that in this change.

#7 lore raw
In reply to Andrey Konovalov
Quoted 29 lines · Andrey Konovalov
Quoted 19 lines · Lecopzer Chen

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

Test environment:
4G and 8G Qemu virt,
39-bit VA + 4k PAGE_SIZE with 3-level page table, test by lib/test_kasan.ko and lib/test_kasan_module.ko

It also works in Kaslr with CONFIG_RANDOMIZE_MODULE_REGION_FULL, but not test for HW_TAG(I have no proper device), thus keep HW_TAG and KASAN_VMALLOC mutual exclusion until confirming the functionality.

Quoted 3 lines

[1]: commit 0609ae011deb41c ("x86/kasan: support KASAN_VMALLOC")

Hi Lecopzer,

Thanks for working on this!

Quoted 9 lines

for the series along with the other two patches minding the nit in patch #3.

Will, Catalin, could you please take a look at the arm changes?

Thanks!

If vmalloc can now be backed with real shadow memory, we no longer have to keep the module region in its default location when KASLR and KASAN are both enabled.

So the check on line 164 in arch/arm64/kernel/kaslr.c should probably be updated to reflect this change.

#5 lore raw

Re: [PATCH 3/3] arm64: Kconfig: support CONFIG_KASAN_VMALLOC

In reply to Andrey Konovalov

Hi Andrey,

Quoted 10 lines · Lecopzer Chen
Quoted 9 lines

Now I have no device to test for HW_TAG, so keep it not selected until someone can test this.

---
 arch/arm64/Kconfig | 1 +
 1 file changed, 1 insertion(+)
Quoted 13 lines

arch/arm64/Kconfig config ARM64 · line 139

Quoted 9 lines
arch/arm64/Kconfig +1 -0
index 05e17351e4f3..29ab35aab59e 100644
@@ -136,6 +136,7 @@ config ARM64
        select HAVE_ARCH_JUMP_LABEL
        select HAVE_ARCH_JUMP_LABEL_RELATIVE
        select HAVE_ARCH_KASAN if !(ARM64_16K_PAGES && ARM64_VA_BITS_48)
+       select HAVE_ARCH_KASAN_VMALLOC if (HAVE_ARCH_KASAN && !KASAN_HW_TAGS)

KASAN_VMALLOC currently "depends on" KASAN_GENERIC. I think we should either do "HAVE_ARCH_KASAN && KASAN_GENERIC" here as well, or just do "if HAVE_ARCH_KASAN".

Thanks for the correctness, I'll change to the following in V2 patch.
"select HAVE_ARCH_KASAN_VMALLOC if HAVE_ARCH_KASAN"

Let KASAN_VMALLOC depend on the mode it supports to avoid modifying two places if KASAN_VMALLOC can support other than GENERIC in the future.

#10 lore raw
In reply to Andrey Konovalov

Hi Andrey,

Quoted 20 lines · Lecopzer Chen
Quoted 13 lines

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

Test environment:
4G and 8G Qemu virt,
39-bit VA + 4k PAGE_SIZE with 3-level page table, test by lib/test_kasan.ko and lib/test_kasan_module.ko

Quoted 4 lines

It also works in Kaslr with CONFIG_RANDOMIZE_MODULE_REGION_FULL, but not test for HW_TAG(I have no proper device), thus keep HW_TAG and KASAN_VMALLOC mutual exclusion until confirming the functionality.

Quoted 6 lines

Re this: it makes sense to introduce vmalloc support one step a time and add SW_TAGS support before taking on HW_TAGS. SW_TAGS doesn't require any special hardware. Working on SW_TAGS first will also allow dealing with potential conflicts between vmalloc and tags without having MTE in the picture as well. Just FYI, no need to include that in this change.

Thanks for the information and suggestion, so this serise I'll keep only for KASAN_GENERIC support :)

BRs,
Lecopzer

#8 lore raw
In reply to Ard Biesheuvel

Hi Ard,

Quoted 41 lines · Andrey Konovalov
Quoted 29 lines · Lecopzer Chen
Quoted 19 lines

Linux supports KAsan for VMALLOC since commit 3c5c3cfb9ef4da9 ("kasan: support backing vmalloc space with real shadow memory")

Acroding to how x86 ported it [1], they early allocated p4d and pgd, but in arm64 I just simulate how KAsan supports MODULES_VADDR in arm64 by not to populate the vmalloc area except for kimg address.

Test environment:
4G and 8G Qemu virt,
39-bit VA + 4k PAGE_SIZE with 3-level page table, test by lib/test_kasan.ko and lib/test_kasan_module.ko

It also works in Kaslr with CONFIG_RANDOMIZE_MODULE_REGION_FULL, but not test for HW_TAG(I have no proper device), thus keep HW_TAG and KASAN_VMALLOC mutual exclusion until confirming the functionality.

Quoted 3 lines

[1]: commit 0609ae011deb41c ("x86/kasan: support KASAN_VMALLOC")

Hi Lecopzer,

Thanks for working on this!

Quoted 9 lines

for the series along with the other two patches minding the nit in patch #3.

Will, Catalin, could you please take a look at the arm changes?

Thanks!

Quoted 7 lines

If vmalloc can now be backed with real shadow memory, we no longer have to keep the module region in its default location when KASLR and KASAN are both enabled.

So the check on line 164 in arch/arm64/kernel/kaslr.c should probably be updated to reflect this change.

I've tested supporting module region randomized and It looks fine in some easy test(insmod some modules).

I'll add this to patch v2, thanks for your suggestion.

BRs,
Lecopzer