[PATCH] LoongArch: Disable KASAN if PGDIR_SIZE is too large for cpu_vabits

Huacai Chen posted 1 patch 3 weeks, 4 days ago
There is a newer version of this series
arch/loongarch/mm/kasan_init.c | 11 +++++++++++
1 file changed, 11 insertions(+)
[PATCH] LoongArch: Disable KASAN if PGDIR_SIZE is too large for cpu_vabits
Posted by Huacai Chen 3 weeks, 4 days ago
If PGDIR_SIZE is too large for cpu_vabits, KASAN_SHADOW_END will
overflow UINTPTR_MAX because KASAN_SHADOW_START/KASAN_SHADOW_END are
aligned up by PGDIR_SIZE. And then the overflowed KASAN_SHADOW_END looks
like a user space address.

For example, PGDIR_SIZE of CONFIG_4KB_4LEVEL is 2^39, which is too large
for Loongson-2K series whose cpu_vabits = 39.

Since CONFIG_4KB_4LEVEL is completely legal for CPUs with cpu_vabits <=
39, we just disable KASAN via early return in kasan_init(). Otherwise we
get a boot failure.

Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
---
 arch/loongarch/mm/kasan_init.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/arch/loongarch/mm/kasan_init.c b/arch/loongarch/mm/kasan_init.c
index 4a0d1880dd71..6bdcc8751d4e 100644
--- a/arch/loongarch/mm/kasan_init.c
+++ b/arch/loongarch/mm/kasan_init.c
@@ -262,6 +262,17 @@ void __init kasan_init(void)
 	u64 i;
 	phys_addr_t pa_start, pa_end;
 
+	/*
+	 * If PGDIR_SIZE is too large for cpu_vabits, KASAN_SHADOW_END will
+	 * overflow UINTPTR_MAX and then looks like a user space address.
+	 * For example, PGDIR_SIZE of CONFIG_4KB_4LEVEL is 2^39, which is too
+	 * large for Loongson-2K series whose cpu_vabits = 39.
+	 */
+	if (KASAN_SHADOW_END < vm_map_base) {
+		pr_warn("PGDIR_SIZE too large for cpu_vabits, KernelAddressSanitizer disabled.\n");
+		return;
+	}
+
 	/*
 	 * PGD was populated as invalid_pmd_table or invalid_pud_table
 	 * in pagetable_init() which depends on how many levels of page
-- 
2.43.5
Re: [PATCH] LoongArch: Disable KASAN if PGDIR_SIZE is too large for cpu_vabits
Posted by Jiaxun Yang 3 weeks, 4 days ago

在2024年10月30日十月 上午8:11,Huacai Chen写道:
> If PGDIR_SIZE is too large for cpu_vabits, KASAN_SHADOW_END will
> overflow UINTPTR_MAX because KASAN_SHADOW_START/KASAN_SHADOW_END are
> aligned up by PGDIR_SIZE. And then the overflowed KASAN_SHADOW_END looks
> like a user space address.
>
> For example, PGDIR_SIZE of CONFIG_4KB_4LEVEL is 2^39, which is too large
> for Loongson-2K series whose cpu_vabits = 39.
>
> Since CONFIG_4KB_4LEVEL is completely legal for CPUs with cpu_vabits <=
> 39, we just disable KASAN via early return in kasan_init(). Otherwise we
> get a boot failure.
>
> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>

Reviewed-by: Jiaxun Yang <jiaxun.yang@flygoat.com>

I was hit by this issue as well.

Thanks
- Jiaxun

> ---
>  arch/loongarch/mm/kasan_init.c | 11 +++++++++++
>  1 file changed, 11 insertions(+)
>
> diff --git a/arch/loongarch/mm/kasan_init.c b/arch/loongarch/mm/kasan_init.c
> index 4a0d1880dd71..6bdcc8751d4e 100644
> --- a/arch/loongarch/mm/kasan_init.c
> +++ b/arch/loongarch/mm/kasan_init.c
> @@ -262,6 +262,17 @@ void __init kasan_init(void)
>  	u64 i;
>  	phys_addr_t pa_start, pa_end;
> 
> +	/*
> +	 * If PGDIR_SIZE is too large for cpu_vabits, KASAN_SHADOW_END will
> +	 * overflow UINTPTR_MAX and then looks like a user space address.
> +	 * For example, PGDIR_SIZE of CONFIG_4KB_4LEVEL is 2^39, which is too
> +	 * large for Loongson-2K series whose cpu_vabits = 39.
> +	 */
> +	if (KASAN_SHADOW_END < vm_map_base) {
> +		pr_warn("PGDIR_SIZE too large for cpu_vabits, KernelAddressSanitizer 
> disabled.\n");
> +		return;
> +	}
> +
>  	/*
>  	 * PGD was populated as invalid_pmd_table or invalid_pud_table
>  	 * in pagetable_init() which depends on how many levels of page
> -- 
> 2.43.5

-- 
- Jiaxun