Skip to contentSkip to conversation

xen, pvh: fix unbootable VMs (PVH + KASAN - AMD_MEM_ENCRYPT)

PATCH In review Alexey Dobriyan 2 participants 5 messages Aug 2, 2024 – Aug 15, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 xen, pvh: fix unbootable VMs (PVH + KASAN - AMD_MEM_ENCRYPT)

Uninstrument arch/x86/platform/pvh/enlighten.c: KASAN has not been setup _this_ early in the boot process.

Steps to reproduce:

	make allnoconfig
	make sure CONFIG_AMD_MEM_ENCRYPT is disabled
		AMD_MEM_ENCRYPT independently uninstruments lib/string.o
		so PVH boot code calls into uninstrumented memset() and
		memcmp() which can make the bug disappear depending on
		the compiler.
	enable CONFIG_PVH
	enable CONFIG_KASAN
	enable serial console
		this is fun exercise if you never done it from nothing :^)

	make

	qemu-system-x86_64	\
		-enable-kvm	\
		-cpu host	\
		-smp cpus=1	\
		-m 4096		\
		-serial stdio	\
		-kernel vmlinux \
		-append 'console=ttyS0 ignore_loglevel'

Messages on serial console will easily tell OK kernel from unbootable kernel. In bad case qemu hangs in an infinite loop stroboscoping "SeaBIOS" message.

---
 arch/x86/platform/pvh/Makefile | 1 +
 1 file changed, 1 insertion(+)
arch/x86/platform/pvh/Makefile +1 -0
index 5dec5067c9fb..c43fb7964dc4 100644
@@ -1,5 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 OBJECT_FILES_NON_STANDARD_head.o := y
+KASAN_SANITIZE := n
 
 obj-$(CONFIG_PVH) += enlighten.o
 obj-$(CONFIG_PVH) += head.o
Signature · 1 line
2.45.0
#1 lore raw

PATCH 2/3 x86/cpu: fix unbootable VMs by inlining memcmp() in hypervisor_cpuid_base()

If this memcmp() is not inlined then PVH early boot code can call into KASAN-instrumented memcmp() which results in unbootable VMs:

pvh_start_xen
xen_prepare_pvh
xen_cpuid_base
hypervisor_cpuid_base
memcmp

---
 arch/x86/include/asm/cpuid.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpuid.h +6 -1
index 6b122a31da06..80cc6386d7b1 100644
@@ -196,7 +196,12 @@ static inline uint32_t hypervisor_cpuid_base(const char *sig, uint32_t leaves)
 	for_each_possible_hypervisor_cpuid_base(base) {
 		cpuid(base, &eax, &signature[0], &signature[1], &signature[2]);
 
-		if (!memcmp(sig, signature, 12) &&
+		/*
+		 * This must not compile to "call memcmp" because it's called
+		 * from PVH early boot code before instrumentation is set up
+		 * and memcmp() itself may be instrumented.
+		 */
+		if (!__builtin_memcmp(sig, signature, 12) &&
 		    (leaves == 0 || ((eax - base) >= leaves)))
 			return base;
 	}
Signature · 1 line
2.45.0
maintainer Acked-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] x86/cpu: fix unbootable VMs by inlining memcmp() in hypervisor_cpuid_base()

Quoted 10 lines · Alexey Dobriyan

If this memcmp() is not inlined then PVH early boot code can call into KASAN-instrumented memcmp() which results in unbootable VMs:

pvh_start_xen
xen_prepare_pvh
xen_cpuid_base
hypervisor_cpuid_base
memcmp

Juergen

#3 lore raw

PATCH 3/3 xen, pvh: fix unbootable VMs by inlining memset() in xen_prepare_pvh()

In reply to Alexey Dobriyan

If this memset() is not inlined than PVH early boot code can call into KASAN-instrumented memset() which results in unbootable VMs.

---
 arch/x86/platform/pvh/enlighten.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/platform/pvh/enlighten.c +5 -1
index 944e0290f2c0..2263885d16ba 100644
@@ -130,7 +130,11 @@ void __init xen_prepare_pvh(void)
 		BUG();
 	}
 
-	memset(&pvh_bootparams, 0, sizeof(pvh_bootparams));
+	/*
+	 * This must not compile to "call memset" because memset() may be
+	 * instrumented.
+	 */
+	__builtin_memset(&pvh_bootparams, 0, sizeof(pvh_bootparams));
 
 	hypervisor_specific_init(xen_guest);
 
Signature · 1 line
2.45.0