Skip to content

Commit fb509f7

Browse files
minipli-osssean-jc
authored andcommitted
KVM: VMX: Make CR0.WP a guest owned bit
Guests like grsecurity that make heavy use of CR0.WP to implement kernel level W^X will suffer from the implied VMEXITs. With EPT there is no need to intercept a guest change of CR0.WP, so simply make it a guest owned bit if we can do so. This implies that a read of a guest's CR0.WP bit might need a VMREAD. However, the only potentially affected user seems to be kvm_init_mmu() which is a heavy operation to begin with. But also most callers already cache the full value of CR0 anyway, so no additional VMREAD is needed. The only exception is nested_vmx_load_cr3(). This change is VMX-specific, as SVM has no such fine grained control register intercept control. Suggested-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Mathias Krause <minipli@grsecurity.net> Link: https://lore.kernel.org/r/20230322013731.102955-7-minipli@grsecurity.net Co-developed-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 74cdc83 commit fb509f7

4 files changed

Lines changed: 22 additions & 4 deletions

File tree

arch/x86/kvm/kvm_cache_regs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include <linux/kvm_host.h>
66

7-
#define KVM_POSSIBLE_CR0_GUEST_BITS X86_CR0_TS
7+
#define KVM_POSSIBLE_CR0_GUEST_BITS (X86_CR0_TS | X86_CR0_WP)
88
#define KVM_POSSIBLE_CR4_GUEST_BITS \
99
(X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
1010
| X86_CR4_OSXMMEXCPT | X86_CR4_PGE | X86_CR4_TSD | X86_CR4_FSGSBASE)

arch/x86/kvm/vmx/nested.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4481,7 +4481,7 @@ static void load_vmcs12_host_state(struct kvm_vcpu *vcpu,
44814481
* CR0_GUEST_HOST_MASK is already set in the original vmcs01
44824482
* (KVM doesn't change it);
44834483
*/
4484-
vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4484+
vcpu->arch.cr0_guest_owned_bits = vmx_l1_guest_owned_cr0_bits();
44854485
vmx_set_cr0(vcpu, vmcs12->host_cr0);
44864486

44874487
/* Same as above - no reason to call set_cr4_guest_host_mask(). */
@@ -4632,7 +4632,7 @@ static void nested_vmx_restore_host_state(struct kvm_vcpu *vcpu)
46324632
*/
46334633
vmx_set_efer(vcpu, nested_vmx_get_vmcs01_guest_efer(vmx));
46344634

4635-
vcpu->arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4635+
vcpu->arch.cr0_guest_owned_bits = vmx_l1_guest_owned_cr0_bits();
46364636
vmx_set_cr0(vcpu, vmcs_readl(CR0_READ_SHADOW));
46374637

46384638
vcpu->arch.cr4_guest_owned_bits = ~vmcs_readl(CR4_GUEST_HOST_MASK);

arch/x86/kvm/vmx/vmx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4790,7 +4790,7 @@ static void init_vmcs(struct vcpu_vmx *vmx)
47904790
/* 22.2.1, 20.8.1 */
47914791
vm_entry_controls_set(vmx, vmx_vmentry_ctrl());
47924792

4793-
vmx->vcpu.arch.cr0_guest_owned_bits = KVM_POSSIBLE_CR0_GUEST_BITS;
4793+
vmx->vcpu.arch.cr0_guest_owned_bits = vmx_l1_guest_owned_cr0_bits();
47944794
vmcs_writel(CR0_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr0_guest_owned_bits);
47954795

47964796
set_cr4_guest_host_mask(vmx);

arch/x86/kvm/vmx/vmx.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,24 @@ BUILD_CONTROLS_SHADOW(tertiary_exec, TERTIARY_VM_EXEC_CONTROL, 64)
640640
(1 << VCPU_EXREG_EXIT_INFO_1) | \
641641
(1 << VCPU_EXREG_EXIT_INFO_2))
642642

643+
static inline unsigned long vmx_l1_guest_owned_cr0_bits(void)
644+
{
645+
unsigned long bits = KVM_POSSIBLE_CR0_GUEST_BITS;
646+
647+
/*
648+
* CR0.WP needs to be intercepted when KVM is shadowing legacy paging
649+
* in order to construct shadow PTEs with the correct protections.
650+
* Note! CR0.WP technically can be passed through to the guest if
651+
* paging is disabled, but checking CR0.PG would generate a cyclical
652+
* dependency of sorts due to forcing the caller to ensure CR0 holds
653+
* the correct value prior to determining which CR0 bits can be owned
654+
* by L1. Keep it simple and limit the optimization to EPT.
655+
*/
656+
if (!enable_ept)
657+
bits &= ~X86_CR0_WP;
658+
return bits;
659+
}
660+
643661
static __always_inline struct kvm_vmx *to_kvm_vmx(struct kvm *kvm)
644662
{
645663
return container_of(kvm, struct kvm_vmx, kvm);

0 commit comments

Comments
 (0)