Re: [PATCH 1/2] KVM: x86: Defer IBPBs for vCPU and nested transitions until core run loop
From: Yosry Ahmed
Date: Thu Dec 03 2026 - 15:55:25 EST
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index 8acfdfc583a1..e5ae655702b4 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -5187,7 +5187,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
> > * is handled on the nested VM-Exit path.
> > */
> > if (static_branch_likely(&switch_vcpu_ibpb))
> > - indirect_branch_prediction_barrier();
> > + vcpu->arch.need_ibpb = true;
>
> This means that if we run vCPU A on a pCPU, then load vCPU B without
> running it, we won't do an IBPB. At least not until vCPU B (or any vCPU)
> actually runs on the pCPU.
>
> My question would be, is it possible for training done by vCPU A to lead
> KVM into leaking some of vCPU B's state after loading it, even without
> actually running vCPU B?
Revisiting this (I may or may not have been bribed), and asking an LLM
to verify if the scenario I came up with is legit, the findings were
interesting:
>
> Basically this scenario (all on the same pCPU):
>
> 1. Malicious vCPU A runs and injects branch predictor entries.
>
> 2. KVM loads vCPU B to perform some action without actually running
> vCPU B. The training from (1) leaks some of vCPU B's state into the
> microarchitectural state.
According to the LLM, this step is a guest->host attack which should be
mitigated by guest->host mitigations (IBRS/retpoline/...), so this
scenario isn't feasible.
>
> 3. Malicious vCPU A runs again and extracts the leaked data.
> > per_cpu(last_vcpu, cpu) = vcpu;
However, I think updating the tracking here is a problem.
The following scenario is possible AFAICT:
1. vCPU#A runs on pCPU#1
last_vcpu[pCPU#1] = vCPU#A
2. vCPU#B is loaded on pCPU#1, but doesn't actually VM-Enter
last_vcpu[pCPU#1] updated to vCPU#B
vCPU#B->need_ibpb = true
3. vCPU#B is migrated to pCPU#2 and does VM-Enter
last_vcpu[pCPU#2] updated to vCPU#B
vCPU#B->need_ibpb = false
4. vCPU#B is migrated back to pCPU#1 and does VM-Enter
last_vcpu[pCPU#1] already vCPU#B
vCPU#B->need_ibpb = false
So vCPU#B ends up running on pCPU#1 after vCPU#A without an IBPB. This
is because the tracking is updated before the IBPB is actually executed,
and the vCPU can be migrated in between.
Maybe move some/all of the tracking to vcpu_enter_guest()? Not sure.
> > }
> >
> > @@ -11315,6 +11315,11 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
> > kvm_make_request(KVM_REQ_EVENT, vcpu);
> > }
> >
> > + if (unlikely(vcpu->arch.need_ibpb)) {
> > + indirect_branch_prediction_barrier();
> > + vcpu->arch.need_ibpb = false;
> > + }
> > +
> > fpregs_assert_state_consistent();
> > if (test_thread_flag(TIF_NEED_FPU_LOAD))
> > switch_fpu_return();
> > diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
> > index 70e81f008030..6708142d051d 100644
> > --- a/arch/x86/kvm/x86.h
> > +++ b/arch/x86/kvm/x86.h
> > @@ -169,7 +169,7 @@ static inline void kvm_nested_vmexit_handle_ibrs(struct kvm_vcpu *vcpu)
> >
> > if (guest_cpu_cap_has(vcpu, X86_FEATURE_SPEC_CTRL) ||
> > guest_cpu_cap_has(vcpu, X86_FEATURE_AMD_IBRS))
> > - indirect_branch_prediction_barrier();
> > + vcpu->arch.need_ibpb = true;
>
> I think the same question more-or-less applies here, could an L2 guest
> lead KVM to leak some of L1's state before L1 is actually run? Although
> in this case it could be harder for any leaked state to survive KVM
> running L1 and then going back to L2.
According to the LLM, this would also be a guest->host attack, but the
same per-vCPU vs per-CPU tracking problem exists. For example:
- L2 runs on pCPU#1.
- nested VM-Exit, need_ibpb = true
- vCPU migrates to pCPU#2.
- L1 runs, clears need_ibpb, executes IBPB on pCPU#2.
- L1 migrates back to pCPU#1.