Skip to contentSkip to conversation

KVM: arm64: nv: Keep the shadow S2 MMUs at fixed addresses

PATCH New Karl Mehltretter 2 participants 2 messages Aug 8

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

PATCH v5 1/1 KVM: arm64: nv: Keep the shadow S2 MMUs at fixed addresses

kvm_vcpu_init_nested() can grow kvm->arch.nested_mmus while initialising another vCPU: it copies the MMUs, publishes the new allocation, and frees the old one. It updates pgt->mmu back-pointers, but not hw_mmu, leaving already-running vCPUs with pointers to freed memory. hw_mmu cannot be fixed up the same way: a running vCPU reads it without holding mmu_lock. The nested S2 ptdump file's debugfs private data is also affected, as it points into the freed array.

KASAN reports an access through the stale hw_mmu pointer as a slab-use-after-free in kvm_handle_guest_abort().

Turn nested_mmus into a pointer table allocated once for the maximum number of vCPUs during VM creation. Allocate the MMUs separately as vCPUs are initialised and append their pointers to that table. The MMU objects never move, so cached hw_mmu pointers, pgt->mmu back-pointers, and ptdump private data remain valid.

Two issues in the old implementation are also fixed:

- The old failure path passed uninitialised MMUs to kvm_free_stage2_pgd(), which derives kvm from mmu->arch and can therefore dereference an invalid pointer. Only call kvm_free_stage2_pgd() for initialised MMUs.
- Previously, initialisation of the new MMUs was not ordered before publication of nested_mmus_size. Fix this by taking mmu_lock when increasing nested_mmus_size.

Fixes 4f128f8e1aaa ("KVM: arm64: nv: Support multiple nested Stage-2 mmu structures")
Assisted-by Claude:claude-fable-5

---
Changes in v5:
- Free the fixed nested_mmus pointer table from kvm_arch_destroy_vm() when generic VM creation fails after kvm_arch_init_vm() succeeds (reported by Sashiko AI).

Changes in v4:
- The code diff changes only arch/arm64/kvm/nested.c compared to v3:
- Restore v2's centralized error paths (Wei-Lin Chang).
- Restore explicit write_lock()/write_unlock() to avoid mixing goto-based error handling with cleanup guards.
- Drop the allocation-failure comment (Marc Zyngier).

v3: https://lore.kernel.org/r/[email protected]/

Changes in v3:
- Tighten the commit message (Wei-Lin Chang).
- The code diff changes only arch/arm64/kvm/nested.c compared to v2:
- Drop the redundant pointer-table allocation comment and use guard(write_lock) when publishing nested_mmus_size (Wei-Lin Chang).
- Rewrite the error handling without gotos.

v2: https://lore.kernel.org/r/[email protected]/

Changes in v2:
- Allocate the fixed pointer table during VM creation, as suggested by Marc.
- Allocate the MMUs individually and simplify the error and teardown paths.
- Drop the selftest patch that triggered KASAN.
It is not a good fit for the existing suite.

v1: https://lore.kernel.org/r/[email protected]/

 arch/arm64/include/asm/kvm_host.h   |  6 +-
 arch/arm64/include/asm/kvm_nested.h |  2 +-
 arch/arm64/kvm/arm.c                |  8 ++-
 arch/arm64/kvm/nested.c             | 95 ++++++++++++++++-------------
 4 files changed, 61 insertions(+), 50 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -3
index bae2c4f92ef5..59d1d77ee116 100644
@@ -319,10 +319,10 @@ struct kvm_arch {
 	u64 fgu[__NR_FGT_GROUP_IDS__];
 
 	/*
-	 * Stage 2 paging state for VMs with nested S2 using a virtual
-	 * VMID.
+	 * Stage 2 paging state for VMs with nested S2 using a virtual VMID.
+	 * MMUs are allocated separately to keep their addresses stable.
 	 */
-	struct kvm_s2_mmu *nested_mmus;
+	struct kvm_s2_mmu **nested_mmus;
 	size_t nested_mmus_size;
 	int nested_mmus_next;
 
arch/arm64/include/asm/kvm_nested.h +1 -1
index 012d711034d1..d21be647ac57 100644
@@ -66,7 +66,7 @@ static inline u64 translate_ttbr0_el2_to_ttbr0_el1(u64 ttbr0)
 
 extern bool forward_smc_trap(struct kvm_vcpu *vcpu);
 extern bool forward_debug_exception(struct kvm_vcpu *vcpu);
-extern void kvm_init_nested(struct kvm *kvm);
+extern int kvm_init_nested(struct kvm *kvm);
 extern int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu);
 extern void kvm_init_nested_s2_mmu(struct kvm_s2_mmu *mmu);
 extern struct kvm_s2_mmu *lookup_s2_mmu(struct kvm_vcpu *vcpu);
arch/arm64/kvm/arm.c +6 -2
index 50adfff75be8..12bb94a95b36 100644
@@ -223,8 +223,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	mutex_unlock(&kvm->lock);
 #endif
 
-	kvm_init_nested(kvm);
-
 	ret = kvm_share_hyp(kvm, kvm + 1);
 	if (ret)
 		return ret;
@@ -239,6 +237,10 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 	if (ret)
 		goto err_free_cpumask;
 
+	ret = kvm_init_nested(kvm);
+	if (ret)
+		goto err_uninit_mmu;
+
 	if (is_protected_kvm_enabled()) {
 		/*
 		 * If any failures occur after this is successful, make sure to
@@ -267,6 +269,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
 
 err_uninit_mmu:
 	kvm_uninit_stage2_mmu(kvm);
+	kvfree(kvm->arch.nested_mmus);
 err_free_cpumask:
 	free_cpumask_var(kvm->arch.supported_cpus);
 err_unshare_kvm:
@@ -317,6 +320,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)
 		pkvm_destroy_hyp_vm(kvm);
 
 	kvm_uninit_stage2_mmu(kvm);
+	kvfree(kvm->arch.nested_mmus);
 	kvm_destroy_mpidr_data(kvm);
 
 	kfree(kvm->arch.sysreg_masks);
arch/arm64/kvm/nested.c +51 -44

177 lines not on this page · raw message

Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] `kvm_arch_flush_shadow_all` incorrectly frees the fixed `nested_mmus` pointer table, causing a…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] kvm_arch_flush_shadow_all incorrectly frees the fixed nested_mmus pointer table, causing a guaranteed NULL pointer dereference on subsequent VCPU initialization.
- [Low] Inconsistent error path label ordering in kvm_vcpu_init_nested bypasses the cleanup of vncr_array if MMU object allocation fails.
--

--- Patch [1]: [PATCH v5] KVM: arm64: nv: Keep the shadow S2 MMUs at fixed addresses --- commit f14f067b1d63cdcaee4dee273e4e4dcdb1e8d284
Author: Karl Mehltretter <[email protected]>

KVM: arm64: nv: Keep the shadow S2 MMUs at fixed addresses

This commit refactors nested S2 MMUs to use a fixed-size pointer table allocated during VM creation. The MMU objects are allocated separately so their addresses remain stable, fixing a use-after-free bug.

Quoted 3 lines
arch/arm64/kvm/nested.c +0 -0

[ ... ]

Quoted 1 line
arch/arm64/kvm/nested.c +0 -0
@@ -82,52 +95,46 @@ int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)

[ ... ]

Quoted 6 lines
arch/arm64/kvm/nested.c +5 -0
+	for (i = 0; i < S2_MMU_PER_VCPU; i++) {
+		mmu = kzalloc_obj(*mmu, GFP_KERNEL_ACCOUNT);
+		if (!mmu) {
+			ret = -ENOMEM;
+			goto err_free_mmus;
 		}

[ ... ]

arch/arm64/kvm/nested.c int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu) · line 139

Quoted 12 lines
arch/arm64/kvm/nested.c +7 -2
+err_free_vncr:
+	free_page((unsigned long)vcpu->arch.ctxt.vncr_array);
+	vcpu->arch.ctxt.vncr_array = NULL;
 
-	kvm->arch.nested_mmus_size = num_mmus;
+err_free_mmus:
+	while (i--)
+		free_nested_s2_mmu(kvm->arch.nested_mmus[kvm->arch.nested_mmus_size + i]);
 
-	return 0;
+	return ret;
 }

[Severity: Low]
Does this error path unwind in the correct order?

If kzalloc_obj() fails during initialization, the code jumps to the err_free_mmus label, which completely bypasses the cleanup in err_free_vncr.

This leaves the newly allocated vncr_array anchored to vcpu->arch.ctxt.vncr_array instead of freeing it immediately. While it will eventually be freed when the VCPU is destroyed, this seems to create an inconsistent state compared to the other error paths in this function.

[ ... ]

arch/arm64/kvm/nested.c void kvm_arch_flush_shadow_all(struct kvm *kvm) · line 1288

Quoted 13 lines
arch/arm64/kvm/nested.c +2 -2
@@ -1272,10 +1279,10 @@ void kvm_arch_flush_shadow_all(struct kvm *kvm)
 	int i;
 
 	for (i = 0; i < kvm->arch.nested_mmus_size; i++) {
-		struct kvm_s2_mmu *mmu = &kvm->arch.nested_mmus[i];
+		struct kvm_s2_mmu *mmu = kvm->arch.nested_mmus[i];
 
 		if (!WARN_ON(atomic_read(&mmu->refcnt)))
-			kvm_free_stage2_pgd(mmu);
+			free_nested_s2_mmu(mmu);
 	}
 	kvfree(kvm->arch.nested_mmus);
 	kvm->arch.nested_mmus = NULL;

[Severity: High]
Can this lead to a NULL pointer dereference on subsequent VCPU initialization?

The patch redesigns nested_mmus to be a fixed-size pointer table allocated once at VM creation. However, it appears this legacy cleanup remains, freeing and nulling kvm->arch.nested_mmus when kvm_arch_flush_shadow_all() runs.

If a VM's file descriptor is kept alive by another process, and a subsequent KVM_ARM_VCPU_INIT ioctl is issued, kvm_vcpu_init_nested() will attempt to append to the array:

    kvm->arch.nested_mmus[kvm->arch.nested_mmus_size + i] = mmu;

Since the dynamic reallocation logic was removed in this patch, nested_mmus will still be NULL here, triggering a kernel panic. Should this teardown path avoid freeing the array itself now?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1