KVM: x86/mmu: Refresh CR0.WP prior to checking for emulated permission faults
authorSean Christopherson <seanjc@google.com>
Wed, 5 Apr 2023 00:26:08 +0000 (17:26 -0700)
committerSean Christopherson <seanjc@google.com>
Mon, 10 Apr 2023 22:25:36 +0000 (15:25 -0700)
commitcf9f4c0eb1699d306e348b1fd0225af7b2c282d3
tree72cb92045e0bedefe658f9923e9e993c39e9f92b
parent944a8dad8b4eb0b565cc36600ee1339c21278d76
KVM: x86/mmu: Refresh CR0.WP prior to checking for emulated permission faults

Refresh the MMU's snapshot of the vCPU's CR0.WP prior to checking for
permission faults when emulating a guest memory access and CR0.WP may be
guest owned.  If the guest toggles only CR0.WP and triggers emulation of
a supervisor write, e.g. when KVM is emulating UMIP, KVM may consume a
stale CR0.WP, i.e. use stale protection bits metadata.

Note, KVM passes through CR0.WP if and only if EPT is enabled as CR0.WP
is part of the MMU role for legacy shadow paging, and SVM (NPT) doesn't
support per-bit interception controls for CR0.  Don't bother checking for
EPT vs. NPT as the "old == new" check will always be true under NPT, i.e.
the only cost is the read of vcpu->arch.cr4 (SVM unconditionally grabs CR0
from the VMCB on VM-Exit).

Reported-by: Mathias Krause <minipli@grsecurity.net>
Link: https://lkml.kernel.org/r/677169b4-051f-fcae-756b-9a3e1bb9f8fe%40grsecurity.net
Fixes: fb509f76acc8 ("KVM: VMX: Make CR0.WP a guest owned bit")
Tested-by: Mathias Krause <minipli@grsecurity.net>
Link: https://lore.kernel.org/r/20230405002608.418442-1-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/mmu.h
arch/x86/kvm/mmu/mmu.c