From: Liran Alon Subject: Re: [PATCH 6/7] x86/svm: Set IBPB when running a different VCPU Date: Tue, 9 Jan 2018 03:31:14 -0800 (PST) Message-ID: <30cf953d-3132-4dbb-97ba-4b49aa949580@default> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: , , , , , , To: Return-path: Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-Id: kvm.vger.kernel.org ----- pbonzini@redhat.com wrote: > On 08/01/2018 21:00, Liran Alon wrote: > >=20 > >=20 > > On 08/01/18 20:08, Paolo Bonzini wrote: > >> From: Tom Lendacky > >> > >> Set IBPB (Indirect Branch Prediction Barrier) when the current CPU > is > >> going to run a VCPU different from what was previously run.=C2=A0 > Nested > >> virtualization uses the same VMCB for the second level guest, but > the > >> L1 hypervisor should be using IBRS to protect itself. > >> > >> Signed-off-by: Tom Lendacky > >> Signed-off-by: Paolo Bonzini > >> --- > >> =C2=A0 arch/x86/kvm/svm.c | 31 +++++++++++++++++++++++++++++++ > >> =C2=A0 1 file changed, 31 insertions(+) > >> > >> diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c > >> index 779981a00d01..dd744d896cec 100644 > >> --- a/arch/x86/kvm/svm.c > >> +++ b/arch/x86/kvm/svm.c > >> @@ -289,6 +289,7 @@ struct amd_svm_iommu_ir { > >> =C2=A0 module_param(vgif, int, 0444); > >> > >> =C2=A0 static bool __read_mostly have_spec_ctrl; > >> +static bool __read_mostly have_ibpb_support; > >> > >> =C2=A0 static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long > cr0); > >> =C2=A0 static void svm_flush_tlb(struct kvm_vcpu *vcpu, bool > invalidate_gpa); > >> @@ -540,6 +541,7 @@ struct svm_cpu_data { > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct kvm_ldttss_desc *tss_desc; > >> > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct page *save_area; > >> +=C2=A0=C2=A0=C2=A0 struct vmcb *current_vmcb; > >> =C2=A0 }; > >> > >> =C2=A0 static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); > >> @@ -1151,6 +1153,11 @@ static __init int svm_hardware_setup(void) > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pr_info("kvm: S= PEC_CTRL available\n"); > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 else > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pr_info("kvm: S= PEC_CTRL not available\n"); > >> +=C2=A0=C2=A0=C2=A0 have_ibpb_support =3D have_spec_ctrl || cpu_has_ib= pb_support(); > >> +=C2=A0=C2=A0=C2=A0 if (have_ibpb_support) > >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pr_info("kvm: IBPB_SUPPORT= available\n"); > >> +=C2=A0=C2=A0=C2=A0 else > >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pr_info("kvm: IBPB_SUPPORT= not available\n"); > >> > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return 0; > >> > >> @@ -1725,11 +1732,19 @@ static void svm_free_vcpu(struct kvm_vcpu > *vcpu) > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 __free_pages(virt_to_page(svm->nested.m= srpm), > MSRPM_ALLOC_ORDER); > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 kvm_vcpu_uninit(vcpu); > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 kmem_cache_free(kvm_vcpu_cache, svm); > >> + > >> +=C2=A0=C2=A0=C2=A0 /* > >> +=C2=A0=C2=A0=C2=A0=C2=A0 * The VMCB could be recycled, causing a fals= e negative in > >> +=C2=A0=C2=A0=C2=A0=C2=A0 * svm_vcpu_load; block speculative execution= . > >> +=C2=A0=C2=A0=C2=A0=C2=A0 */ > >> +=C2=A0=C2=A0=C2=A0 if (have_ibpb_support) > >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 wrmsrl(MSR_IA32_PRED_CMD, = FEATURE_SET_IBPB); > >> =C2=A0 } > >> > >> =C2=A0 static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) > >> =C2=A0 { > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 struct vcpu_svm *svm =3D to_svm(vcpu); > >> +=C2=A0=C2=A0=C2=A0 struct svm_cpu_data *sd =3D per_cpu(svm_data, cpu)= ; > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 int i; > >> > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (unlikely(cpu !=3D vcpu->cpu)) { > >> @@ -1758,6 +1773,12 @@ static void svm_vcpu_load(struct kvm_vcpu > >> *vcpu, int cpu) > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (static_cpu_has(X86_FEATURE_RDTSCP)) > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 wrmsrl(MSR_TSC_= AUX, svm->tsc_aux); > >> > >> +=C2=A0=C2=A0=C2=A0 if (sd->current_vmcb !=3D svm->vmcb) { > >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 sd->current_vmcb =3D svm->= vmcb; > >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (have_ibpb_support) > >> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 wr= msrl(MSR_IA32_PRED_CMD, FEATURE_SET_IBPB); > >> +=C2=A0=C2=A0=C2=A0 } > >> + > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 avic_vcpu_load(vcpu, cpu); > >> =C2=A0 } > >> > >> @@ -2798,6 +2819,11 @@ static int nested_svm_vmexit(struct vcpu_svm > *svm) > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (!nested_vmcb) > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return 1; > >> > >> +=C2=A0=C2=A0=C2=A0 /* > >> +=C2=A0=C2=A0=C2=A0=C2=A0 * No need for IBPB here, the L1 hypervisor s= hould be running > with > >> +=C2=A0=C2=A0=C2=A0=C2=A0 * IBRS=3D1 and inserts one already when swit= ching L2 VMs. > >> +=C2=A0=C2=A0=C2=A0=C2=A0 */ > >> + > >=20 > > I am not fully convinced yet this is OK from security perspective. > > From the CPU point-of-view, both L1 & L2 run in the same > prediction-mode > > (as they are both running in SVM guest-mode). Therefore, IBRS will > not > > hide the BHB & BTB of L1 from L2. >=20 > Indeed, IBRS will not hide the indirect branch predictor state of L2 > user mode from L1 user mode. >=20 > On current generation processors, as I understand it, IBRS simply > disables the indirect branch predictor when set to 1. Therefore, as This is not how I understood what IBRS does. Intel (not sure about AMD) states that if IBRS is set, indirect branches wi= ll now allow their predicted target address to be controlled by code that e= xecuted in a less privileged prediction-mode before the IBRS was last writt= en with a value of 1. (Intel also states that thus an indirect branch may be affected by code in = a less privileged prediction-mode that executed AFTER IBRS mode was last wr= itten with a value of 1). Therefore, L2 could also inject BTB/BHB entries that will be used by L1: Consider the case that L2 injects values into BTB/BHB and then issues a hyp= ercall which will cause #VMExit which will be forwarded to L1. Because L2 &= L1 runs in the same prediction-mode (both Ring0 SVM guest-mode) from physi= cal CPU perspective, Ring0 L1 code could be using BTB/BHB entries injected = by Ring0 L2. (The fact that L0 have set IBRS to 1 when exiting from L2 to L0 doesn't pre= vent those entries from being used by L1 after L0 enters L1). This is different than what happens in non-nested case as L0 & L1 runs in d= ifferent physical prediction-modes and therefore setting IBRS=3D1 on every = #VMExit should suffice. Therefore, I don't understand how L1 setting IBRS to 1 will help him in thi= s case. Maybe this mechanism works differently on AMD? -Liran > long as the L1 hypervisor sets IBRS=3D1, the indirect branch predictor > state left by L2 does not affect execution of the L1 hypervisor. >=20 > Future processors might have a mode that says "just set IBRS=3D1 and > I'll > DTRT". If that's done by indexing the BTB using the full virtual > address, the CPL _and_ the ASID/PCID/VPID, then IBPB is not needed > here > because the L2 VM uses a different ASID. If that's done by only > augmenting the BTB index with the CPL, we'd need an IBPB. But in > this > new world we've been thrown into, that would be a processor bug... >=20 > Note that AMD currently doesn't implement IBRS at all. In order to > mitigate against CVE-2017-5715, the hypervisor should issue an IBPB > on > every vmexit (in both L0 and L1). However, the cost of that is very > high. While we may want a paranoia mode that does it, it is outside > the > scope of this series (which is more of a "let's unblock QEMU patches" > thing than a complete fix). >=20 > > 6. One can implicitly assume that L1 hypervisor did issued a IBPB > when > > loading an VMCB and therefore it doesn't have to do it again in L0. > > However, I see 2 problems with this approach: > > (a) We don't protect old non-patched L1 hypervisors from Spectre > even > > though we could easily do that from L0 with small performance hit. >=20 > Yeah, that's a nice thing to have. However, I disagree on the > "small" > performance hit... on a patched hypervisor, the cost of a double fix > can > be very noticeable. >=20 > > (b) When L1 hypervisor runs only a single L2 VM, it doesn't have to > > issue an IBPB when loading the VMCB (as it didn't run any other > VMCB > > before) and it should be sufficient from L1 perspective to just > write 1 > > to IBRS. However, in our nested-case, this is a security-hole. >=20 > This is the main difference in our reasoning. I think IBRS=3D1 (or > IBPB > on vmexit) should be enough. >=20 > Paolo >=20 > > Am I misunderstanding something? > >=20 > > Regards, > > -Liran > >=20 > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* Exit Guest-Mode */ > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 leave_guest_mode(&svm->vcpu); > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 svm->nested.vmcb =3D 0; > >> @@ -3061,6 +3087,11 @@ static bool nested_svm_vmrun(struct vcpu_svm > *svm) > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (!nested_vmcb) > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return false; > >> > >> +=C2=A0=C2=A0=C2=A0 /* > >> +=C2=A0=C2=A0=C2=A0=C2=A0 * No need for IBPB here, since the nested VM= is less > >> privileged.=C2=A0 The > >> +=C2=A0=C2=A0=C2=A0=C2=A0 * L1 hypervisor inserts one already when swi= tching L2 VMs. > >> +=C2=A0=C2=A0=C2=A0=C2=A0 */ > >> + > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (!nested_vmcb_checks(nested_vmcb)) { > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 nested_vmcb->co= ntrol.exit_code=C2=A0=C2=A0=C2=A0 =3D SVM_EXIT_ERR; > >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 nested_vmcb->co= ntrol.exit_code_hi =3D 0; > >> > >