Skip to contentSkip to conversation

x86_64: fix KASan shadow region page tables

PATCH Superseded Alexander Popov 2 participants 5 messages May 22, 2015 – May 28, 2015 x86

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86_64: fix KASan shadow region page tables

Physical addresses in KASan shadow region page tables need fixup:
kernel halts without it if phys_base is not zero.

---
 arch/x86/kernel/head_64.S | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/x86/kernel/head_64.S +14 -0
index ae6588b..f7711bb 100644
@@ -95,6 +95,20 @@ startup_64:
 
 	addq	%rbp, level2_fixmap_pgt + (506*8)(%rip)
 
+#ifdef CONFIG_KASAN
+	xor	%rax, %rax
+	leaq	kasan_zero_pud(%rip), %rbx
+	leaq	kasan_zero_pmd(%rip), %rcx
+	leaq	kasan_zero_pte(%rip), %rdx
+1:
+	addq	%rbp, (%rbx,%rax,8)
+	addq	%rbp, (%rcx,%rax,8)
+	addq	%rbp, (%rdx,%rax,8)
+	inc	%rax
+	cmp	$512, %rax
+	jne	1b
+#endif
+
 	/*
 	 * Set up the identity mapping for the switchover.  These
 	 * entries should *NOT* have the global bit set!  This also
Signature · 1 line
1.9.1
#1 lore raw
Quoted 3 lines · Alexander Popov

Physical addresses in KASan shadow region page tables need fixup:
kernel halts without it if phys_base is not zero.

Indeed.
Since we have to patch page tables anyway, compile-time created kasan page tables become pointless.
So, I'd suggest to create them in runtime. This could be done in plain C in kasan_map_early_shadow().

#2 lore raw
Quoted 11 lines · Andrey Ryabinin
Quoted 3 lines · Alexander Popov

Physical addresses in KASan shadow region page tables need fixup:
kernel halts without it if phys_base is not zero.

Indeed.
Since we have to patch page tables anyway, compile-time created kasan page tables become pointless.
So, I'd suggest to create them in runtime. This could be done in plain C in kasan_map_early_shadow().

Thanks for your reply, Andrey.

Creating kasan_zero_pud, kasan_zero_pmd and kasan_zero_pte in kasan_map_early_shadow() doesn't look handy for me because this function is called twice in x86_64_start_kernel().

What do you think about leaving the initialization of KASan shadow region page tables in arch/x86/kernel/head_64.S and calling something like kasan_fixup_early_shadow() before calling kasan_map_early_shadow() for the first time?

Best regards,
Alexander

#3 lore raw
Quoted 15 lines · Alexander Popov
Quoted 11 lines · Andrey Ryabinin
Quoted 3 lines · Alexander Popov

Physical addresses in KASan shadow region page tables need fixup:
kernel halts without it if phys_base is not zero.

Indeed.
Since we have to patch page tables anyway, compile-time created kasan page tables become pointless.
So, I'd suggest to create them in runtime. This could be done in plain C in kasan_map_early_shadow().

Thanks for your reply, Andrey.

Quoted 9 lines

Creating kasan_zero_pud, kasan_zero_pmd and kasan_zero_pte in kasan_map_early_shadow() doesn't look handy for me because this function is called twice in x86_64_start_kernel().

What do you think about leaving the initialization of KASan shadow region page tables in arch/x86/kernel/head_64.S and calling something like kasan_fixup_early_shadow() before calling kasan_map_early_shadow() for the first time?

Why do we have initialize + fixup later if we could simply initialize page tables properly?

Just move everything into kasan_init_64.c
kasan_zero_p* could be a normal C arrays (with __page_aligned_bss attribute). Initialize these page tables in something like kasan_early_init() before kasan_map_early_shadow() call.

Quoted 4 lines

Best regards,
Alexander

#4 lore raw
Quoted 6 lines · Andrey Ryabinin
Quoted 4 lines · Alexander Popov

What do you think about leaving the initialization of KASan shadow region page tables in arch/x86/kernel/head_64.S and calling something like kasan_fixup_early_shadow() before calling kasan_map_early_shadow() for the first time?

Quoted 6 lines

Why do we have initialize + fixup later if we could simply initialize page tables properly?

Just move everything into kasan_init_64.c
kasan_zero_p* could be a normal C arrays (with __page_aligned_bss attribute). Initialize these page tables in something like kasan_early_init() before kasan_map_early_shadow() call.

Thanks, I'll do that and return with the second version.

Best regards,
Alexander