KVM: arm64: nv: Keep the shadow S2 MMUs at fixed addresses
Subsystems (3)
PATCH v3 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.
--- arch/arm64/include/asm/kvm_host.h | 6 +-- arch/arm64/include/asm/kvm_nested.h | 2 +- arch/arm64/kvm/arm.c | 7 ++- arch/arm64/kvm/nested.c | 81 ++++++++++++++++------------- 4 files changed, 54 insertions(+), 42 deletions(-)
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 +3 -3
@@ -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
@@ -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 +5 -2
@@ -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:
arch/arm64/kvm/nested.c +45 -36
161 lines not on this page · raw message
Signature · 1 line
2.39.5 (Apple Git-154)
Quoted 32 lines · Karl Mehltretter
kvm_vcpu_init_nested()can growkvm->arch.nested_mmuswhile initialising another vCPU: it copies the MMUs, publishes the new allocation, and frees the old one. It updatespgt->mmuback-pointers, but nothw_mmu, leaving already-running vCPUs with pointers to freed memory.hw_mmucannot be fixed up the same way: a running vCPU reads it without holdingmmu_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_mmupointer as a slab-use-after-free inkvm_handle_guest_abort().Turn
nested_mmusinto 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 cachedhw_mmupointers,pgt->mmuback-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 frommmu->archand can therefore dereference an invalid pointer. Only callkvm_free_stage2_pgd()for initialised MMUs.
- Previously, initialisation of the new MMUs was not ordered before publication ofnested_mmus_size. Fix this by takingmmu_lockwhen increasingnested_mmus_size.Fixes 4f128f8e1aaa ("KVM: arm64: nv: Support multiple nested Stage-2 mmu structures")Suggested-by Marc Zyngier <[email protected]>Assisted-by Claude:claude-fable-5Signed-off-by Karl Mehltretter <[email protected]>
[...]
Quoted 63 lines
int
kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)
{arch/arm64/kvm/nested.c +20
-25struct kvm *kvm = vcpu->kvm; -struct kvm_s2_mmu *tmp; -int num_mmus, ret =0 ; +struct kvm_s2_mmu *mmu; +int num_mmus, ret =0 , i;if (test_bit (KVM_ARM_VCPU_HAS_EL2_E2H0 , kvm->arch.vcpu_features) && !cpus_have_final_cap (ARM64_HAS_HCR_NV1 ))@@ -91,42 +104,38 @@ int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)*/ num_mmus =atomic_read (&kvm->online_vcpus) *S2_MMU_PER_VCPU ; -if (num_mmus > kvm->arch.nested_mmus_size) { - tmp =kvcalloc (num_mmus,sizeof (*tmp),GFP_KERNEL_ACCOUNT ); -if (!tmp) -return -ENOMEM ; - -write_lock (&kvm->mmu_lock); +if (num_mmus <= kvm->arch.nested_mmus_size) +return 0 ; -if (kvm->arch.nested_mmus_size) { -memcpy (tmp, kvm->arch.nested_mmus, -size_mul (sizeof (*tmp), kvm->arch.nested_mmus_size)); +lockdep_assert_held (&kvm->arch.config_lock); -for (int i =0 ; i < kvm->arch.nested_mmus_size; i++) - tmp[i].pgt->mmu = &tmp[i]; +for (i =0 ; i <S2_MMU_PER_VCPU ; i++) { + mmu =kzalloc_obj (*mmu,GFP_KERNEL_ACCOUNT ); +if (!mmu) { + ret = -ENOMEM ; +break ; } -swap (kvm->arch.nested_mmus, tmp); - -write_unlock (&kvm->mmu_lock); + ret =init_nested_s2_mmu (kvm, mmu); +if (ret) { +kfree (mmu); +free_page ((unsigned long )vcpu->arch.ctxt.vncr_array); + vcpu->arch.ctxt.vncr_array =NULL ; +break ; + } -kvfree (tmp); + kvm->arch.nested_mmus[kvm->arch.nested_mmus_size + i] = mmu; } -for (int i = kvm->arch.nested_mmus_size; !ret && i < num_mmus; i++) - ret =init_nested_s2_mmu (kvm, &kvm->arch.nested_mmus[i]); -if (ret) { -for (int i = kvm->arch.nested_mmus_size; i < num_mmus; i++) -kvm_free_stage2_pgd (&kvm->arch.nested_mmus[i]); - -free_page ((unsigned long )vcpu->arch.ctxt.vncr_array); - vcpu->arch.ctxt.vncr_array =NULL ; +while (i--) +free_nested_s2_mmu (kvm->arch.nested_mmus[kvm->arch.nested_mmus_size + i]);
arch/arm64/kvm/nested.c int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu) · line 141
Quoted 10 lines
arch/arm64/kvm/nested.c +2
-1return ret; } - kvm->arch.nested_mmus_size = num_mmus; +guard (write_lock)(&kvm->mmu_lock); + kvm->arch.nested_mmus_size +=S2_MMU_PER_VCPU ;return 0 ; }
Hey sorry for nitpicking once more, I actually like v2 more, as this version splits the error handling to two places. Marc's implementation
[1] is also good, but that requires a twist in kvm_arch_flush_shadow_all(). So I'd personally go with v2 (+my suggestions for it :)), but also fine if others have other opinions.
I also think the comment in kvm_vcpu_init_nested() is worth adjusting slightly, too.
Thanks,
Wei-Lin Chang
[1]: https://lore.kernel.org/kvmarm/[email protected]/
[...]
Quoted 16 lines
arch/arm64/kvm/nested.c +2
-2@@ -1273,10 +1282,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 ;Signature · 1 line
2.39.5 (Apple Git-154)
Quoted 10 lines · Wei-Lin Chang
Hey sorry for nitpicking once more, I actually like v2 more, as this version splits the error handling to two places. Marc's implementation
[1] is also good, but that requires a twist inkvm_arch_flush_shadow_all(). So I'd personally go with v2 (+my suggestions for it :)), but also fine if others have other opinions.I also think the comment in
kvm_vcpu_init_nested()is worth adjusting slightly, too.
No worries, thanks for taking another look.
I was following the guidance in include/linux/cleanup.h:
the expectation is that usage of "goto" and cleanup helpers is never mixed in the same function.
But maybe keeping goto is better. I'll wait a bit for others to comment.
For the comment, I propose replacing the current text with:
/*
* If allocating an MMU object fails, keep the VNCR page alive so
* that userspace can retry vCPU initialisation without affecting
* the VM.
*/
Is that the adjustment you had in mind?
Thanks,
Karl
Marc Zyngier
maintainerQuoted 15 lines · Karl Mehltretter
Quoted 10 lines · Wei-Lin Chang
Hey sorry for nitpicking once more, I actually like v2 more, as this version splits the error handling to two places. Marc's implementation
[1] is also good, but that requires a twist inkvm_arch_flush_shadow_all(). So I'd personally go with v2 (+my suggestions for it :)), but also fine if others have other opinions.I also think the comment in
kvm_vcpu_init_nested()is worth adjusting slightly, too.No worries, thanks for taking another look.
Quoted 7 lines
I was following the guidance in
include/linux/cleanup.h:the expectation is that usage of "goto" and cleanup helpers is never mixed in the same function.
But maybe keeping goto is better. I'll wait a bit for others to comment.
The version I proposed in response to your first version had no goto, and trivial error handling. Can we *please* stop the bike shedding and settle on the simplest possible fix? Or *any* fix at all?
Quoted 10 lines
For the comment, I propose replacing the current text with:
/*
* If allocating an MMU object fails, keep the VNCR page alive so
* that userspace can retry vCPU initialisation without affecting
* the VM.
*/Is that the adjustment you had in mind?
I don't think we need to change any comment. If anything, it should be deleted altogether. This is a security fix, not a literary exercise.
M.
Signature · 1 line
Jazz isn't dead. It just smells funny.