Re: [PATCH v2] KVM: X86: Fix exception untrigger on ret to user

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 2021-06-28 at 15:48 +0300, Stas Sergeev wrote:
> When returning to user, the special care is taken about the
> exception that was already injected to VMCS but not yet to guest.
> cancel_injection removes such exception from VMCS. It is set as
> pending, and if the user does KVM_SET_REGS, it gets completely canceled.
> 
> This didn't happen though, because the vcpu->arch.exception.injected
> and vcpu->arch.exception.pending were forgotten to update in
> cancel_injection. As the result, KVM_SET_REGS didn't cancel out
> anything, and the exception was re-injected on the next KVM_RUN,
> even though the guest registers (like EIP) were already modified.
> This was leading to an exception coming from the "wrong place".
> 
> This patch makes sure the vcpu->arch.exception.injected and
> vcpu->arch.exception.pending are in sync with the reality (and
> with VMCS). Also it adds clearing of pending exception to
> __set_sregs() the same way it is in __set_regs(). See patch
> b4f14abd9 that added it to __set_regs().
> 
> How to trigger the buggy scenario (that is, without this patch):
> - Make sure you have the old CPU where shadow page tables
> are used. Core2 family should be fine for the task. In this
> case, all PF exceptions produce the exit to monitor.
> - You need the _TIF_SIGPENDING flag set at the right moment
> to get kvm_vcpu_exit_request() to return true when the PF
> exception was just injected. In that case the cancel_injection
> path is executed.
> - You need the "unlucky" user-space that executes KVM_SET_REGS
> at the right moment. This leads to KVM_SET_REGS not clearing
> the exception, but instead corrupting its context.
> 
> v2 changes:
> - do not add WARN_ON_ONCE() to __set_regs(). As explained by
> Vitaly Kuznetsov, it can be user-triggerable.
> - clear pending exception also in __set_sregs().
> - update description with the bug-triggering scenario.

I used to know that area very very well, and I basically combed
the whole thing back and forth, 
and I have patch series to decouple injected and
pending exceptions. 

I'll refresh my memory on this and then I'll review your patch.

My gut feeling is that you discovered too that injections of
exceptions from userspace is kind of broken and only works
because Qemu doesn't really inject much (only some #MC exceptions
which are mostly fatal anyway, and #DB/#BP which only happen
when guest debugging is enabled which is not a standard feature).

Best regards,
	Maxim Levitsky

> 
> Signed-off-by: Stas Sergeev <stsp2@xxxxxxxxx>
> 
> CC: Paolo Bonzini <pbonzini@xxxxxxxxxx>
> CC: Sean Christopherson <seanjc@xxxxxxxxxx>
> CC: Vitaly Kuznetsov <vkuznets@xxxxxxxxxx>
> CC: Jim Mattson <jmattson@xxxxxxxxxx>
> CC: Joerg Roedel <joro@xxxxxxxxxx>
> CC: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> CC: Ingo Molnar <mingo@xxxxxxxxxx>
> CC: Borislav Petkov <bp@xxxxxxxxx>
> CC: Jan Kiszka <jan.kiszka@xxxxxxxxxxx>
> CC: x86@xxxxxxxxxx
> CC: "H. Peter Anvin" <hpa@xxxxxxxxx>
> CC: kvm@xxxxxxxxxxxxxxx
> ---
>  arch/x86/kvm/x86.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index e0f4a46649d7..d1026e9216e4 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -9450,7 +9450,11 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
>  cancel_injection:
>  	if (req_immediate_exit)
>  		kvm_make_request(KVM_REQ_EVENT, vcpu);
> -	static_call(kvm_x86_cancel_injection)(vcpu);
> +	if (vcpu->arch.exception.injected) {
> +		static_call(kvm_x86_cancel_injection)(vcpu);
> +		vcpu->arch.exception.injected = false;
> +		vcpu->arch.exception.pending = true;
> +	}
>  	if (unlikely(vcpu->arch.apic_attention))
>  		kvm_lapic_sync_from_vapic(vcpu);
>  out:
> @@ -10077,6 +10081,8 @@ static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
>  		pr_debug("Set back pending irq %d\n", pending_vec);
>  	}
>  
> +	vcpu->arch.exception.pending = false;
> +
>  	kvm_make_request(KVM_REQ_EVENT, vcpu);
>  
>  	ret = 0;





[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux