Skip to content

Commit 3ca9419

Browse files
Wanpeng Libonzini
authored andcommitted
KVM: X86: Fix userspace set invalid CR4
Reported by syzkaller: WARNING: CPU: 0 PID: 6544 at /home/kernel/data/kvm/arch/x86/kvm//vmx/vmx.c:4689 handle_desc+0x37/0x40 [kvm_intel] CPU: 0 PID: 6544 Comm: a.out Tainted: G OE 5.3.0-rc4+ #4 RIP: 0010:handle_desc+0x37/0x40 [kvm_intel] Call Trace: vmx_handle_exit+0xbe/0x6b0 [kvm_intel] vcpu_enter_guest+0x4dc/0x18d0 [kvm] kvm_arch_vcpu_ioctl_run+0x407/0x660 [kvm] kvm_vcpu_ioctl+0x3ad/0x690 [kvm] do_vfs_ioctl+0xa2/0x690 ksys_ioctl+0x6d/0x80 __x64_sys_ioctl+0x1a/0x20 do_syscall_64+0x74/0x720 entry_SYSCALL_64_after_hwframe+0x49/0xbe When CR4.UMIP is set, guest should have UMIP cpuid flag. Current kvm set_sregs function doesn't have such check when userspace inputs sregs values. SECONDARY_EXEC_DESC is enabled on writes to CR4.UMIP in vmx_set_cr4 though guest doesn't have UMIP cpuid flag. The testcast triggers handle_desc warning when executing ltr instruction since guest architectural CR4 doesn't set UMIP. This patch fixes it by adding valid CR4 and CPUID combination checking in __set_sregs. syzkaller source: https://syzkaller.appspot.com/x/repro.c?x=138efb99600000 Reported-by: [email protected] Cc: [email protected] Signed-off-by: Wanpeng Li <[email protected]> Reviewed-by: Sean Christopherson <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent a1a640b commit 3ca9419

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

‎arch/x86/kvm/x86.c‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -885,34 +885,42 @@ int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
885885
}
886886
EXPORT_SYMBOL_GPL(kvm_set_xcr);
887887

888-
int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
888+
static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
889889
{
890-
unsigned long old_cr4 = kvm_read_cr4(vcpu);
891-
unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
892-
X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
893-
894890
if (cr4 & CR4_RESERVED_BITS)
895-
return 1;
891+
return -EINVAL;
896892

897893
if (!guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && (cr4 & X86_CR4_OSXSAVE))
898-
return 1;
894+
return -EINVAL;
899895

900896
if (!guest_cpuid_has(vcpu, X86_FEATURE_SMEP) && (cr4 & X86_CR4_SMEP))
901-
return 1;
897+
return -EINVAL;
902898

903899
if (!guest_cpuid_has(vcpu, X86_FEATURE_SMAP) && (cr4 & X86_CR4_SMAP))
904-
return 1;
900+
return -EINVAL;
905901

906902
if (!guest_cpuid_has(vcpu, X86_FEATURE_FSGSBASE) && (cr4 & X86_CR4_FSGSBASE))
907-
return 1;
903+
return -EINVAL;
908904

909905
if (!guest_cpuid_has(vcpu, X86_FEATURE_PKU) && (cr4 & X86_CR4_PKE))
910-
return 1;
906+
return -EINVAL;
911907

912908
if (!guest_cpuid_has(vcpu, X86_FEATURE_LA57) && (cr4 & X86_CR4_LA57))
913-
return 1;
909+
return -EINVAL;
914910

915911
if (!guest_cpuid_has(vcpu, X86_FEATURE_UMIP) && (cr4 & X86_CR4_UMIP))
912+
return -EINVAL;
913+
914+
return 0;
915+
}
916+
917+
int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
918+
{
919+
unsigned long old_cr4 = kvm_read_cr4(vcpu);
920+
unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
921+
X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
922+
923+
if (kvm_valid_cr4(vcpu, cr4))
916924
return 1;
917925

918926
if (is_long_mode(vcpu)) {
@@ -8714,10 +8722,6 @@ EXPORT_SYMBOL_GPL(kvm_task_switch);
87148722

87158723
static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
87168724
{
8717-
if (!guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
8718-
(sregs->cr4 & X86_CR4_OSXSAVE))
8719-
return -EINVAL;
8720-
87218725
if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
87228726
/*
87238727
* When EFER.LME and CR0.PG are set, the processor is in
@@ -8736,7 +8740,7 @@ static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
87368740
return -EINVAL;
87378741
}
87388742

8739-
return 0;
8743+
return kvm_valid_cr4(vcpu, sregs->cr4);
87408744
}
87418745

87428746
static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)

0 commit comments

Comments
 (0)