arm64: kasan: support CONFIG_KASAN_VMALLOC
Subsystems (4)
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
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
@@ -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
PATCH 2/3 arm64: kasan: abstract _text and _end to KERNEL_START/END
In reply to Lecopzer ChenArm64 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
@@ -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
PATCH 3/3 arm64: Kconfig: support CONFIG_KASAN_VMALLOC
In reply to Lecopzer ChenNow 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
@@ -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
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.Signed-off-by Lecopzer Chen <[email protected]>--- arch/arm64/Kconfig | 1 + 1 file changed, 1 insertion(+)arch/arm64/Kconfig +0
-0
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
-1select 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.
Re: [PATCH 3/3] arm64: Kconfig: support CONFIG_KASAN_VMALLOC
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.Signed-off-by Lecopzer Chen <[email protected]>--- 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@@ -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_VMALLOCcurrently "depends on"KASAN_GENERIC. I think we should either do "HAVE_ARCH_KASAN&&KASAN_GENERIC" here as well, or just do "ifHAVE_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.
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_VADDRin arm64 by not to populate the vmalloc area except for kimg address.Test environment:
4G and 8G Qemu virt,
39-bit VA + 4kPAGE_SIZEwith 3-level page table, test bylib/test_kasan.koandlib/test_kasan_module.koIt also works in Kaslr with
CONFIG_RANDOMIZE_MODULE_REGION_FULL, but not test forHW_TAG(I have no proper device), thus keepHW_TAGandKASAN_VMALLOCmutual exclusion until confirming the functionality.
Quoted 3 lines
[1]: commit
0609ae011deb41c("x86/kasan: supportKASAN_VMALLOC")Signed-off-by Lecopzer Chen <[email protected]>
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!
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_VADDRin arm64 by not to populate the vmalloc area except for kimg address.Test environment:
4G and 8G Qemu virt,
39-bit VA + 4kPAGE_SIZEwith 3-level page table, test bylib/test_kasan.koandlib/test_kasan_module.koIt also works in Kaslr with
CONFIG_RANDOMIZE_MODULE_REGION_FULL, but not test forHW_TAG(I have no proper device), thus keepHW_TAGandKASAN_VMALLOCmutual exclusion until confirming the functionality.Quoted 3 lines
[1]: commit
0609ae011deb41c("x86/kasan: supportKASAN_VMALLOC")Signed-off-by Lecopzer Chen <[email protected]>Hi Lecopzer,
Thanks for working on this!
Quoted 9 lines
Acked-by Andrey Konovalov <[email protected]>Tested-by Andrey Konovalov <[email protected]>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.
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_VADDRin arm64 by not to populate the vmalloc area except for kimg address.Test environment:
4G and 8G Qemu virt,
39-bit VA + 4kPAGE_SIZEwith 3-level page table, test bylib/test_kasan.koandlib/test_kasan_module.koIt also works in Kaslr with
CONFIG_RANDOMIZE_MODULE_REGION_FULL, but not test forHW_TAG(I have no proper device), thus keepHW_TAGandKASAN_VMALLOCmutual exclusion until confirming the functionality.Quoted 3 lines
[1]: commit
0609ae011deb41c("x86/kasan: supportKASAN_VMALLOC")Signed-off-by Lecopzer Chen <[email protected]>Hi Lecopzer,
Thanks for working on this!
Quoted 9 lines
Acked-by Andrey Konovalov <[email protected]>Tested-by Andrey Konovalov <[email protected]>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.cshould 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
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_VADDRin arm64 by not to populate the vmalloc area except for kimg address.Test environment:
4G and 8G Qemu virt,
39-bit VA + 4kPAGE_SIZEwith 3-level page table, test bylib/test_kasan.koandlib/test_kasan_module.ko
Quoted 4 lines
It also works in Kaslr with
CONFIG_RANDOMIZE_MODULE_REGION_FULL, but not test forHW_TAG(I have no proper device), thus keepHW_TAGandKASAN_VMALLOCmutual 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.
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_VADDRin arm64 by not to populate the vmalloc area except for kimg address.Test environment:
4G and 8G Qemu virt,
39-bit VA + 4kPAGE_SIZEwith 3-level page table, test bylib/test_kasan.koandlib/test_kasan_module.koQuoted 4 lines
It also works in Kaslr with
CONFIG_RANDOMIZE_MODULE_REGION_FULL, but not test forHW_TAG(I have no proper device), thus keepHW_TAGandKASAN_VMALLOCmutual exclusion until confirming the functionality.
Quoted 6 lines
Re this: it makes sense to introduce vmalloc support one step a time and add
SW_TAGSsupport before taking onHW_TAGS.SW_TAGSdoesn't require any special hardware. Working onSW_TAGSfirst 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