Re: [PATCH 3/5] x86: KVM: svm: clear interrupt shadow on all paths in skip_emulated_instruction()

From: Sean Christopherson
Date: Thu Aug 01 2019 - 10:18:04 EST


On Thu, Aug 01, 2019 at 07:14:16AM +0200, Vitaly Kuznetsov wrote:
> Regardless of the way how we skip instruction, interrupt shadow needs to be
> cleared.
>
> Signed-off-by: Vitaly Kuznetsov <vkuznets@xxxxxxxxxx>
> Reviewed-by: Jim Mattson <jmattson@xxxxxxxxxx>
> ---
> arch/x86/kvm/svm.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
> index 80f576e05112..7c7dff3f461f 100644
> --- a/arch/x86/kvm/svm.c
> +++ b/arch/x86/kvm/svm.c
> @@ -784,13 +784,15 @@ static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
> EMULATE_DONE)
> pr_err_once("KVM: %s: unable to skip instruction\n",
> __func__);
> - return;
> + goto clear_int_shadow;

A better fix would be to clear the interrupt shadow in x86_emulate_instruction()
after updating RIP for EMULTYPE_SKIP. VMX has this same flaw when running
nested as handle_ept_misconfig() also expects the interrupt shadow to be
handled by kvm_emulate_instruction(). Clearing the shadow if and only if
the skipping is successful also means KVM isn't incorrectly zapping the
shadow when emulation fails.

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 01e18caac825..f25521fb1c42 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -6537,6 +6537,7 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu,
kvm_rip_write(vcpu, ctxt->_eip);
if (ctxt->eflags & X86_EFLAGS_RF)
kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
+ kvm_x86_ops->set_interrupt_shadow(vcpu, 0);
return EMULATE_DONE;
}

> }
> if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
> printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
> __func__, kvm_rip_read(vcpu), svm->next_rip);
>
> kvm_rip_write(vcpu, svm->next_rip);
> +
> +clear_int_shadow:
> svm_set_interrupt_shadow(vcpu, 0);
> }
>
> --
> 2.20.1
>