Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1272426ybl; Fri, 23 Aug 2019 16:39:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqzvhQY0sbpAOSfySzCx4VSshiXky761d7FmvmZWm09abbM4oDsVyFD9VndXDaaf8oEDMYkN X-Received: by 2002:a17:902:eb:: with SMTP id a98mr7126746pla.75.1566603582128; Fri, 23 Aug 2019 16:39:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566603582; cv=none; d=google.com; s=arc-20160816; b=1CcEPpXlTYR2GqpTcGcthbhjW3YyO0CGsRnbnJk/JuP4OptJcCCYyK1bQHpQqKoAl8 D3Fce0Rl6UzDKkVj7OcFC1vIpcA9oRGgqTc7894UffvRowC8KWvXBx8Cd28u5c4YJ4l9 z4FSBZbXPbbK4sQ+XwH7DTLbh+ysdNh2HrTXTSLsByztuK7Zm7mUz7G5xFtYtI9ovwD5 oxeVmZthO7AuXuodv3VhMxhtQ/eeTUFZMzw1ANcWbj+NQTat5FquAsMePImPPZjEDXmg ivoUEcxajSAdqfmQjQ0Fby0iZvpgcmwe0nqjRIDT55IZ1hfOauR/frMLUGRxNV6FQ8Xd vc/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=jkZlnIPeyTaOjPBlQ7wp3Gf96mqjjUd4XfytjqyEmd0=; b=WiVgPQLmVqyVVSmtn/gPWgKKaR72hlfcjDnFq/ZtQeflXWdV8IwNZyeROu1I8Z7dMx /iF/he8BtjqOcqC4ij9WxK9jNGD9oo3dgDJtqL9P0QdxM9NS4qXHHfJvd/J7Bk18mI8S cvBXU//LLDXaoD3qb2Jysa/IUTjBwEHWdpB6byJzgGBagqf7Uo2GXfMlkn4aRTNaKTd3 FV82uJUXxDSeu70bJcQpQs3GDMa1hGewx7+s38HF5qOP8J4hhtPdImwqTxfGbv6c8qWZ zxAlFnQVbkYok3pzyTiq1AHrQ7dECJl0Qd67g30PS/fP5KtXUqsX70srvZXaVnTNn6Ns 9Uhg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w11si3006412pgr.321.2019.08.23.16.39.26; Fri, 23 Aug 2019 16:39:42 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2393010AbfHWUzq (ORCPT + 99 others); Fri, 23 Aug 2019 16:55:46 -0400 Received: from mga14.intel.com ([192.55.52.115]:48804 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2392742AbfHWUzq (ORCPT ); Fri, 23 Aug 2019 16:55:46 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 23 Aug 2019 13:55:45 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,422,1559545200"; d="scan'208";a="330833026" Received: from sjchrist-coffee.jf.intel.com ([10.54.74.41]) by orsmga004.jf.intel.com with ESMTP; 23 Aug 2019 13:55:45 -0700 From: Sean Christopherson To: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Nadav Amit , Andy Lutomirski Subject: [PATCH] KVM: x86: Don't update RIP or do single-step on faulting emulation Date: Fri, 23 Aug 2019 13:55:44 -0700 Message-Id: <20190823205544.24052-1-sean.j.christopherson@intel.com> X-Mailer: git-send-email 2.22.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Don't advance RIP or inject a single-step #DB if emulation signals a fault. This logic applies to all state updates that are conditional on clean retirement of the emulation instruction, e.g. updating RFLAGS was previously handled by commit 38827dbd3fb85 ("KVM: x86: Do not update EFLAGS on faulting emulation"). Not advancing RIP is likely a nop, i.e. ctxt->eip isn't updated with ctxt->_eip until emulation "retires" anyways. Skipping #DB injection fixes a bug reported by Andy Lutomirski where a #UD on SYSCALL due to invalid state with RFLAGS.RF=1 would loop indefinitely due to emulation overwriting the #UD with #DB and thus restarting the bad SYSCALL over and over. Cc: Nadav Amit Cc: stable@vger.kernel.org Reported-by: Andy Lutomirski Fixes: 663f4c61b803 ("KVM: x86: handle singlestep during emulation") Signed-off-by: Sean Christopherson --- Note, this has minor conflict with my recent series to cleanup the emulator return flows[*]. The end result should look something like: if (!ctxt->have_exception || exception_type(ctxt->exception.vector) == EXCPT_TRAP) { kvm_rip_write(vcpu, ctxt->eip); if (r && ctxt->tf) r = kvm_vcpu_do_singlestep(vcpu); __kvm_set_rflags(vcpu, ctxt->eflags); } [*] https://lkml.kernel.org/r/20190823010709.24879-1-sean.j.christopherson@intel.com arch/x86/kvm/x86.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b4cfd786d0b6..d2962671c3d3 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6611,12 +6611,13 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long rflags = kvm_x86_ops->get_rflags(vcpu); toggle_interruptibility(vcpu, ctxt->interruptibility); vcpu->arch.emulate_regs_need_sync_to_vcpu = false; - kvm_rip_write(vcpu, ctxt->eip); - if (r == EMULATE_DONE && ctxt->tf) - kvm_vcpu_do_singlestep(vcpu, &r); if (!ctxt->have_exception || - exception_type(ctxt->exception.vector) == EXCPT_TRAP) + exception_type(ctxt->exception.vector) == EXCPT_TRAP) { + kvm_rip_write(vcpu, ctxt->eip); + if (r == EMULATE_DONE && ctxt->tf) + kvm_vcpu_do_singlestep(vcpu, &r); __kvm_set_rflags(vcpu, ctxt->eflags); + } /* * For STI, interrupts are shadowed; so KVM_REQ_EVENT will -- 2.22.0