Skip to content

KVM: x86: Fix NULL pointer #GP due to RSM bug

Sean Christopherson kvm lkml 1 version Applied

Status from this message: Queued for kvm/master, thanks.

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest9 patches 8 repliesApplied

Patches in v1 Applied

#Subject and filesSizeRepliesApplied
1/9KVM: x86: Immediately reset the MMU context when the SMM flag is clearedarch/x86/kvm/x86.c+4 -11 reply
2/9KVM: x86: Emulate triple fault shutdown if RSM emulation failsarch/x86/kvm/emulate.c arch/x86/kvm/kvm_emulate.h arch/x86/kvm/x86.c+14 -56 replies
3/9KVM: x86: Replace .set_hflags() with dedicated .exiting_smm() helperarch/x86/kvm/emulate.c arch/x86/kvm/kvm_emulate.h arch/x86/kvm/x86.c+5 -60 replies
4/9KVM: x86: Invoke kvm_smm_changed() immediately after clearing SMM flagarch/x86/kvm/x86.c+2 -20 replies
5/9KVM: x86: Move (most) SMM hflags modifications into kvm_smm_changed()arch/x86/kvm/x86.c+11 -130 replies
6/9KVM: x86: Move "entering SMM" tracepoint into kvm_smm_changed()arch/x86/kvm/x86.c+2 -40 replies
7/9KVM: x86: Rename SMM tracepoint to make it reflect realityarch/x86/kvm/trace.h arch/x86/kvm/x86.c+2 -20 replies
8/9KVM: x86: Drop .post_leave_smm(), i.e. the manual post-RSM MMU resetarch/x86/kvm/emulate.c arch/x86/kvm/kvm_emulate.h arch/x86/kvm/x86.c+8 -90 replies
9/9KVM: x86: Drop "pre_" from enter/leave_smm() helpersarch/x86/include/asm/kvm-x86-ops.h arch/x86/include/asm/kvm_host.h arch/x86/kvm/emulate.c +4 more+23 -240 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/9] KVM: x86: Fix NULL pointer #GP due to RSM bug

Fix a NULL pointer dereference in gfn_to_rmap() that occurs if RSM fails, reported by syzbot.

The immediate problem is that the MMU context's role gets out of sync because KVM clears the SMM flag in the vCPU at the start of RSM emulation, but only resets the MMU context if RSM succeeds. The divergence in vCPU vs. MMU role with respect to the SMM flag causes explosions if the non-SMM memslots have gfn ranges that are not present in the SMM memslots, because the MMU expects that the memslot for a shadow page cannot magically disappear.

The other obvious problem is that KVM doesn't emulate triple fault on RSM failure, e.g. it keeps running the vCPU in a frankenstate instead of exiting to userspace. Fixing that would squash the syzbot repro, but would not fix the underlying issue because nothing prevents userspace from calling KVM_RUN on a vCPU that hit shutdown (yay lack of a shutdown state). But, it's easy to fix and definitely worth doing.

Everything after the two bug fixes is cleanup.

Ben Gardon has an internal patch or two that guards against the NULL pointer dereference in gfn_to_rmap(). I'm planning on getting that functionality posted (needs a little massaging) so that these types of snafus don't crash the host (this isn't the first time I've introduced a bug that broke gfn_to_rmap(), though thankfully it's the first time such a bug has made it upstream, knock on wood).

Amusingly, adding gfn_to_rmap() NULL memslot checks might even be a performance improvement. Because gfn_to_rmap() doesn't check the memslot before using it, and because the compiler can see the search_memslots() returns NULL/0, gcc often/always generates dedicated (and hilarious) code for NULL, e.g. this #GP was caused by an explicit load from 0:

48 8b 14 25 00 00 00 00 mov 0x0,%rdx

Sean Christopherson (9):
KVM: x86: Immediately reset the MMU context when the SMM flag is
cleared
KVM: x86: Emulate triple fault shutdown if RSM emulation fails
KVM: x86: Replace .set_hflags() with dedicated .exiting_smm() helper
KVM: x86: Invoke kvm_smm_changed() immediately after clearing SMM flag
KVM: x86: Move (most) SMM hflags modifications into kvm_smm_changed()
KVM: x86: Move "entering SMM" tracepoint into kvm_smm_changed()
KVM: x86: Rename SMM tracepoint to make it reflect reality
KVM: x86: Drop .post_leave_smm(), i.e. the manual post-RSM MMU reset
KVM: x86: Drop "pre_" from enter/leave_smm() helpers

 arch/x86/include/asm/kvm-x86-ops.h |  4 +--
 arch/x86/include/asm/kvm_host.h    |  4 +--
 arch/x86/kvm/emulate.c             | 31 ++++++++++-------
 arch/x86/kvm/kvm_emulate.h         |  7 ++--
 arch/x86/kvm/svm/svm.c             |  8 ++---
 arch/x86/kvm/trace.h               |  2 +-
 arch/x86/kvm/vmx/vmx.c             |  8 ++---
 arch/x86/kvm/x86.c                 | 53 +++++++++++++++---------------
 8 files changed, 61 insertions(+), 56 deletions(-)
Signature · 1 line
2.32.0.rc1.229.g3e70b5a671-goog