Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1025180ybe; Wed, 4 Sep 2019 11:19:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqzXETUzEc3Eo/TmFN5yIKwIMaTpar1b+hvRjn85QQK9C7y5ni0X/FAVZZi7LylMYQ0EuuQ8 X-Received: by 2002:a17:902:b115:: with SMTP id q21mr23203486plr.241.1567621183496; Wed, 04 Sep 2019 11:19:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567621183; cv=none; d=google.com; s=arc-20160816; b=LD8ZpD7pIoTJ+HVsv02/F6XEfVR+3z+wWGdqZlPMN5GxxDv8CeosHOC1Ht1Kxpo/Ed l4qGM3io+uTnwVWJKr4DbQ2ZiIzfBozHuZm0zusKdqOa7G7FM0ct961y0lh6hFQIBoqm a2fvSlE2n5BT9jDqS6LW4/Q4qq2p7OQR0+xQz6Jnmgq8UO5Pxr0dPWWDW4vG7TaBMj6K GJpNgz4qlD+anVCcJUdrw62FNLoSHMHeuIQO/QryjTK0AtR8iiC9g0kG1IVBVspG29/t /NHmwU8TURgdyFw2869Yy8z/cZPiUTI//lWeu1t2VY/kr1N4AkMT1MqUW02yiQtVELfs Bqxg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=PoR0+kcBNnvix6QkrugDbcxtTWCbCy3J6hJj4XxVA3M=; b=RF7cYHHnlT9WRKl5+fnABgZ2jS+qRXv9AYQBPrD8PfWpGeUrFu+E3l8TnkNQEuC/qb hFOI5061maoSZQGv48JuYXclrDcvzAptH2DgrzAaO8Pqr4afyG1grKUXr5b53Witc3XA m7TJPEINN6XHUwrZSfoafjreIwKQpBV1QLyA/IpunHSW1aU5mD7Wj9SMt2pDPjCJ1HP7 kP3hNwAJPlqpvbkmiORCyYfSVz2Y//zwAgaEbaFQq/LPN4JO+jHP3JXFM+gbRlVIUpMb uKj1SEW1hZL+4Bo4pk2ERU2gYTbN+iv6mgdYNheT5lcHrdfnX7vmQIUU5mUKNQJG/4oA kYfg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=in6co2p0; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w18si11173401pfn.277.2019.09.04.11.19.27; Wed, 04 Sep 2019 11:19:43 -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; dkim=pass header.i=@kernel.org header.s=default header.b=in6co2p0; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390425AbfIDSL6 (ORCPT + 99 others); Wed, 4 Sep 2019 14:11:58 -0400 Received: from mail.kernel.org ([198.145.29.99]:56136 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390419AbfIDSL4 (ORCPT ); Wed, 4 Sep 2019 14:11:56 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4EAFA22CEA; Wed, 4 Sep 2019 18:11:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567620715; bh=VFKkMxlmDxUAAOBEKSCj/Ht+0a40E6lIGzO0uhI0/ls=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=in6co2p0WxjSCOHn8XIUeEd56jaLzkTJDmBvVWKV412luUxjQWCgAtDXlLFA3JN2e g77R66fNBisBOkAAiGIbsADFM+HQI+VuXEVpDao++Bl4a7OiPYQgp7WE/uKBzNYXWw sA8XZSn7JhXeqScfZyBvtCLtxvWWQq7023zqU0CE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nadav Amit , Andy Lutomirski , Sean Christopherson , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Subject: [PATCH 5.2 069/143] KVM: x86: Dont update RIP or do single-step on faulting emulation Date: Wed, 4 Sep 2019 19:53:32 +0200 Message-Id: <20190904175316.762609715@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190904175314.206239922@linuxfoundation.org> References: <20190904175314.206239922@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson commit 75ee23b30dc712d80d2421a9a547e7ab6e379b44 upstream. 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 EFLAGS.TF=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 Signed-off-by: Radim Krčmář Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/x86.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6547,12 +6547,13 @@ restart: 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