提交 3b195ac9 编写于 作者: S Sean Christopherson 提交者: Paolo Bonzini

KVM: SVM: Inject #UD on RDTSCP when it should be disabled in the guest

Intercept RDTSCP to inject #UD if RDTSC is disabled in the guest.

Note, SVM does not support intercepting RDPID.  Unlike VMX's
ENABLE_RDTSCP control, RDTSCP interception does not apply to RDPID.  This
is a benign virtualization hole as the host kernel (incorrectly) sets
MSR_TSC_AUX if RDTSCP is supported, and KVM loads the guest's MSR_TSC_AUX
into hardware if RDTSCP is supported in the host, i.e. KVM will not leak
the host's MSR_TSC_AUX to the guest.

But, when the kernel bug is fixed, KVM will start leaking the host's
MSR_TSC_AUX if RDPID is supported in hardware, but RDTSCP isn't available
for whatever reason.  This leak will be remedied in a future commit.

Fixes: 46896c73 ("KVM: svm: add support for RDTSCP")
Cc: stable@vger.kernel.org
Signed-off-by: NSean Christopherson <seanjc@google.com>
Message-Id: <20210504171734.1434054-4-seanjc@google.com>
Reviewed-by: NJim Mattson <jmattson@google.com>
Reviewed-by: NReiji Watanabe <reijiw@google.com>
Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
上级 85d00112
...@@ -1100,7 +1100,9 @@ static u64 svm_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) ...@@ -1100,7 +1100,9 @@ static u64 svm_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
return svm->vmcb->control.tsc_offset; return svm->vmcb->control.tsc_offset;
} }
static void svm_check_invpcid(struct vcpu_svm *svm) /* Evaluate instruction intercepts that depend on guest CPUID features. */
static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu,
struct vcpu_svm *svm)
{ {
/* /*
* Intercept INVPCID if shadow paging is enabled to sync/free shadow * Intercept INVPCID if shadow paging is enabled to sync/free shadow
...@@ -1113,6 +1115,13 @@ static void svm_check_invpcid(struct vcpu_svm *svm) ...@@ -1113,6 +1115,13 @@ static void svm_check_invpcid(struct vcpu_svm *svm)
else else
svm_clr_intercept(svm, INTERCEPT_INVPCID); svm_clr_intercept(svm, INTERCEPT_INVPCID);
} }
if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) {
if (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
svm_clr_intercept(svm, INTERCEPT_RDTSCP);
else
svm_set_intercept(svm, INTERCEPT_RDTSCP);
}
} }
static void init_vmcb(struct kvm_vcpu *vcpu) static void init_vmcb(struct kvm_vcpu *vcpu)
...@@ -1248,7 +1257,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu) ...@@ -1248,7 +1257,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu)
svm_clr_intercept(svm, INTERCEPT_PAUSE); svm_clr_intercept(svm, INTERCEPT_PAUSE);
} }
svm_check_invpcid(svm); svm_recalc_instruction_intercepts(vcpu, svm);
/* /*
* If the host supports V_SPEC_CTRL then disable the interception * If the host supports V_SPEC_CTRL then disable the interception
...@@ -3084,6 +3093,7 @@ static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = { ...@@ -3084,6 +3093,7 @@ static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = {
[SVM_EXIT_STGI] = stgi_interception, [SVM_EXIT_STGI] = stgi_interception,
[SVM_EXIT_CLGI] = clgi_interception, [SVM_EXIT_CLGI] = clgi_interception,
[SVM_EXIT_SKINIT] = skinit_interception, [SVM_EXIT_SKINIT] = skinit_interception,
[SVM_EXIT_RDTSCP] = kvm_handle_invalid_op,
[SVM_EXIT_WBINVD] = kvm_emulate_wbinvd, [SVM_EXIT_WBINVD] = kvm_emulate_wbinvd,
[SVM_EXIT_MONITOR] = kvm_emulate_monitor, [SVM_EXIT_MONITOR] = kvm_emulate_monitor,
[SVM_EXIT_MWAIT] = kvm_emulate_mwait, [SVM_EXIT_MWAIT] = kvm_emulate_mwait,
...@@ -4007,8 +4017,7 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) ...@@ -4007,8 +4017,7 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) && svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
guest_cpuid_has(vcpu, X86_FEATURE_NRIPS); guest_cpuid_has(vcpu, X86_FEATURE_NRIPS);
/* Check again if INVPCID interception if required */ svm_recalc_instruction_intercepts(vcpu, svm);
svm_check_invpcid(svm);
/* For sev guests, the memory encryption bit is not reserved in CR3. */ /* For sev guests, the memory encryption bit is not reserved in CR3. */
if (sev_guest(vcpu->kvm)) { if (sev_guest(vcpu->kvm)) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册