KVM: Fix race conditions in kvm_arch_flush_shadow_all()
Get series with b4
b4 am [email protected]Versions newest first
- v1 Latest5 patches 10 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/5 | KVM: arm64: Grab KVM MMU write lock in kvm_arch_flush_shadow_all()arch/ arch/ arch/ | +23 | 8 replies | |
| 2/5 | KVM: loongarch: Grab MMU lock in kvm_arch_flush_shadow_all()arch/ | +1 | 1 reply | |
| 3/5 | KVM: mips: Grab MMU lock in kvm_arch_flush_shadow_all()arch/ arch/ | +4 | 0 replies | |
| 4/5 | KVM: Hold MMU lock exclusively when calling kvm_arch_flush_shadow_all()arch/ arch/ arch/ +4 more | +13 | 0 replies | |
| 5/5 | DO NOT MERGE: KVM: selftests: Reproducer for arm64 double-freearch/ arch/ tools/ +1 more | +147 | 0 replies |
Reviews trailers per patch
1 of 5 patches has a Reviewed-by or Acked-by.
| Patch | James Houghton | Bibo Mao |
|---|---|---|
| 1/5 | ||
| 2/5 | R-b | |
| 3/5 | ||
| 4/5 | ||
| 5/5 | Not-signed-off-by |
Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.
Cover letter [PATCH 0/5] KVM: Fix race conditions in kvm_arch_flush_shadow_all()
Hi Paolo,
syzbot running on Google production kernels ran into a double-free on KVM/arm64 in kvm_mmu_free_memory_cache(). It turns out that loongarch and mips also have a similar problem.
kvm_arch_flush_shadow_all() can be called on the same memslot concurrently, leading to double-freeing in arm64 and mips. loongarch is also affected: it can at least underflow some counters; I'm not sure what else can break.
To get into this scenario, we need to have a process (P1) share an open VM with another process (P2). If P1 closes its VM to leave P2 holding the last reference, then there is a race between P1 exiting (exit_mm) and P2 dropping its last reference to the VM.
exit_mm() and kvm_vm_release() both call kvm_mmu_notifier_release() on the same KVM, and the only locks held are the KVM srcu lock and the MMU notifier srcu lock.
Please see the arm64 patch for another description of the same race with more context on the ensuing double-free in KVM/arm64.
The first three patches fix each broken architecture; each of those patches have stable CCed with what I think are the appropriate Fixes.
After patching the locking for the broken architectures, it seems better simply to have KVM take the MMU lock exclusively before calling kvm_arch_flush_shadow_all() so that architectures don't need to worry about it. Feel free to drop that patch, the fourth one, if you disagree with it.
The fifth patch provides a repro (with a crude kernel patch to reliably demonstrate the double-free). Please do not merge this.
The arm64 patch has been tested with the repro. The loongarch and mips patches have been compile-tested only.
kvm_arch_guest_memory_reclaimed() is only implemented by one
architecture: x86. Its implementation does not need the KVM MMU lock to be held.
This series is based on 7.1-rc2.
James Houghton (5):
KVM: arm64: Grab KVM MMU write lock in kvm_arch_flush_shadow_all()
KVM: loongarch: Grab MMU lock in kvm_arch_flush_shadow_all()
KVM: mips: Grab MMU lock in kvm_arch_flush_shadow_all()
KVM: Hold MMU lock exclusively when calling
kvm_arch_flush_shadow_all()
DO NOT MERGE: KVM: selftests: Reproducer for arm64 double-free
arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/include/asm/kvm_mmu.h | 1 + arch/arm64/kvm/mmu.c | 39 +++++- arch/arm64/kvm/nested.c | 4 +- arch/loongarch/kvm/mmu.c | 2 + arch/mips/kvm/mips.c | 2 + arch/mips/kvm/mmu.c | 2 + arch/riscv/kvm/mmu.c | 4 +- arch/riscv/kvm/vm.c | 2 + arch/x86/kvm/mmu/mmu.c | 4 +- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../testing/selftests/kvm/transfer_fd_test.c | 129 ++++++++++++++++++ virt/kvm/kvm_main.c | 3 + 13 files changed, 184 insertions(+), 10 deletions(-) create mode 100644 tools/testing/selftests/kvm/transfer_fd_test.c
base-commit: 6d35786de28116ecf78797a62b84e6bf3c45aa5a
Signature · 1 line
2.54.0.545.g6539524ca2-goog