Skip to content

Commit 8a0cae3

Browse files
committed
KVM: arm64: Reassign nested_mmus array behind mmu_lock
cve CVE-2026-46317 commit-author Hyunwoo Kim <imv4bel@gmail.com> commit 7054335 upstream-diff | Due to missing the following change set the merge conflict arose due to a previous drop extra parameters to kvrealloc(). The change set was deemed unnecessary for this fix. https://lore.kernel.org/all/20240722163111.4766-1-dakr@kernel.org/ kvm->arch.nested_mmus[] is walked under kvm->mmu_lock, including from the MMU notifier path (kvm_unmap_gfn_range() -> kvm_nested_s2_unmap()), which can run at any time. kvm_vcpu_init_nested() reallocates the array and frees the old buffer while holding only kvm->arch.config_lock, so such a walker can reference the freed array. Allocate the new array outside of mmu_lock, as the allocation can sleep. Under the lock, copy the existing entries, fix up the back pointers and reassign the array. Free the old buffer after dropping the lock, as kvfree() can sleep as well. Fixes: 4f128f8 ("KVM: arm64: nv: Support multiple nested Stage-2 mmu structures") Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com> Reviewed-by: Oliver Upton <oupton@kernel.org> Link: https://patch.msgid.link/aiKIVVeIr1aAB1yp@v4bel Signed-off-by: Marc Zyngier <maz@kernel.org> Cc: stable@vger,kernel.org (cherry picked from commit 7054335) Signed-off-by: Jonathan Maple <jmaple@ciq.com>
1 parent 91df7a9 commit 8a0cae3

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

arch/arm64/kvm/nested.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,27 @@ int kvm_vcpu_init_nested(struct kvm_vcpu *vcpu)
6161
* again, and there is no reason to affect the whole VM for this.
6262
*/
6363
num_mmus = atomic_read(&kvm->online_vcpus) * S2_MMU_PER_VCPU;
64-
tmp = kvrealloc(kvm->arch.nested_mmus,
65-
size_mul(sizeof(*kvm->arch.nested_mmus), kvm->arch.nested_mmus_size),
66-
size_mul(sizeof(*kvm->arch.nested_mmus), num_mmus),
67-
GFP_KERNEL_ACCOUNT | __GFP_ZERO);
68-
if (!tmp)
69-
return -ENOMEM;
64+
if (num_mmus > kvm->arch.nested_mmus_size) {
65+
tmp = kvcalloc(num_mmus, sizeof(*tmp), GFP_KERNEL_ACCOUNT);
66+
if (!tmp)
67+
return -ENOMEM;
7068

71-
swap(kvm->arch.nested_mmus, tmp);
69+
write_lock(&kvm->mmu_lock);
7270

73-
/*
74-
* If we went through a realocation, adjust the MMU back-pointers in
75-
* the previously initialised kvm_pgtable structures.
76-
*/
77-
if (kvm->arch.nested_mmus != tmp)
78-
for (int i = 0; i < kvm->arch.nested_mmus_size; i++)
79-
kvm->arch.nested_mmus[i].pgt->mmu = &kvm->arch.nested_mmus[i];
71+
if (kvm->arch.nested_mmus_size) {
72+
memcpy(tmp, kvm->arch.nested_mmus,
73+
size_mul(sizeof(*tmp), kvm->arch.nested_mmus_size));
74+
75+
for (int i = 0; i < kvm->arch.nested_mmus_size; i++)
76+
tmp[i].pgt->mmu = &tmp[i];
77+
}
78+
79+
swap(kvm->arch.nested_mmus, tmp);
80+
81+
write_unlock(&kvm->mmu_lock);
82+
83+
kvfree(tmp);
84+
}
8085

8186
for (int i = kvm->arch.nested_mmus_size; !ret && i < num_mmus; i++)
8287
ret = init_nested_s2_mmu(kvm, &kvm->arch.nested_mmus[i]);

0 commit comments

Comments
 (0)