loongarch.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH] LoongArch: Disable KASAN if PGDIR_SIZE is too large for cpu_vabits
@ 2024-10-30  8:11 Huacai Chen
  2024-10-30  9:05 ` Jiaxun Yang
  0 siblings, 1 reply; 2+ messages in thread
From: Huacai Chen @ 2024-10-30  8:11 UTC (permalink / raw)
  To: Huacai Chen
  Cc: loongarch, Xuefeng Li, Guo Ren, Xuerui Wang, Jiaxun Yang,
	linux-kernel, loongson-kernel, 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>
---
 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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] LoongArch: Disable KASAN if PGDIR_SIZE is too large for cpu_vabits
  2024-10-30  8:11 [PATCH] LoongArch: Disable KASAN if PGDIR_SIZE is too large for cpu_vabits Huacai Chen
@ 2024-10-30  9:05 ` Jiaxun Yang
  0 siblings, 0 replies; 2+ messages in thread
From: Jiaxun Yang @ 2024-10-30  9:05 UTC (permalink / raw)
  To: Huacai Chen, Huacai Chen
  Cc: loongarch, Xuefeng Li, Guo Ren, Xuerui Wang, linux-kernel,
	loongson-kernel



在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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-10-30  9:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-30  8:11 [PATCH] LoongArch: Disable KASAN if PGDIR_SIZE is too large for cpu_vabits Huacai Chen
2024-10-30  9:05 ` Jiaxun Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).