Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp5214512ybl; Tue, 27 Aug 2019 00:55:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqzxY4ng61KC+QvUUrzk7x6A9ebyhyV/HC4yTWGCdViyR+ODwm3yGvWcXnsXOc43IHc10/RQ X-Received: by 2002:a63:481c:: with SMTP id v28mr19885042pga.50.1566892517339; Tue, 27 Aug 2019 00:55:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566892517; cv=none; d=google.com; s=arc-20160816; b=Fg5aI9SMIcdLyy1OJM3FDxv31YWlQF6Yb92K2LDQ80c0iIJzKifYyYInTlEmaZFbSw +H57FMFoXJAdZCO7TQJsG6FNHkicn9ur5hlgVl57uSlMwD1nuhDwrqYL/jal6QHfBhbw XNlfyBiKWwJFWa0/aJfai8sHO2Qq3b+jy+2fqaOrv7Fv16XeUT+TjzhCAwY7nalBicEK nLa+o7OsTlPRjIfTOuoDx4l0aP+zYyuduQOMeJmdIDCIz84hXyY5oDAp0aCzIteCl48n Ollwt3G2dkl/gCGL7BB58h8JBsR6HUB0c/V9lfM1kCCqvhpAwMcUr49Jd26z1v3/WzpG J3HA== 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=LzlpYq3ZZL2DAnZ4ZxeeJ600PRzsGoJI7+HNRAv4ppc=; b=h3PcgF6FsaO0Xv3vjiWk80NgBZzyPB57tj2h6K3DuFs/Rt+W2krFWmV9UmzEk/4cB4 XKiRJGeQMiPQtTdy7vx4p/0inw1vpGwb5jjUM6oyZ/HdGWnVyH5rIwCw0Mea6zcFVUr2 HKuwqcTo3DuNNhIMhkSa25+uE61DqT0eaP974P8W1FtfxghTn4vR+j85whuUsRzto3xV lqOtihszThnxeUFjI3UywYuNPdgh/jYBeyKezaDAl46fRV8RlyD5CQFo08KM3nuyG7UL iosxw0vp3y4VA0WvjULKEotKLruQk7lTo8PBCmh6Di28mdV2qzaP2ETQuZkTU7o9X6qn wJgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=GWo3pSCh; 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 k9si7805211pls.313.2019.08.27.00.55.01; Tue, 27 Aug 2019 00:55:17 -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=GWo3pSCh; 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 S1729820AbfH0Hxe (ORCPT + 99 others); Tue, 27 Aug 2019 03:53:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:44966 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729169AbfH0Hx3 (ORCPT ); Tue, 27 Aug 2019 03:53:29 -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 ED239206BF; Tue, 27 Aug 2019 07:53:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1566892408; bh=GeuldnW2RahsHtyZX8pg5iseW2idGOp+eakQQ2Ylqfk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GWo3pSChxG79dUt1Vjsq6OKadBNElZBLmAd0VthIhH+STGAp2MjIa+5B3K0zUSLt7 Te2ZSYZkRpM0RT0mR0BN1go1EUWEAofGT4JBgUQYZhoBUIAjvii/fDF8I9VSGsOJPf 2sQ77kzRBDJ2S1F9xrGEWVkr98C+HYOHO+6p41qk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sean Christopherson , Thomas Gleixner , "Peter Zijlstra (Intel)" Subject: [PATCH 4.14 44/62] x86/retpoline: Dont clobber RFLAGS during CALL_NOSPEC on i386 Date: Tue, 27 Aug 2019 09:50:49 +0200 Message-Id: <20190827072703.106012532@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190827072659.803647352@linuxfoundation.org> References: <20190827072659.803647352@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 b63f20a778c88b6a04458ed6ffc69da953d3a109 upstream. Use 'lea' instead of 'add' when adjusting %rsp in CALL_NOSPEC so as to avoid clobbering flags. KVM's emulator makes indirect calls into a jump table of sorts, where the destination of the CALL_NOSPEC is a small blob of code that performs fast emulation by executing the target instruction with fixed operands. adcb_al_dl: 0x000339f8 <+0>: adc %dl,%al 0x000339fa <+2>: ret A major motiviation for doing fast emulation is to leverage the CPU to handle consumption and manipulation of arithmetic flags, i.e. RFLAGS is both an input and output to the target of CALL_NOSPEC. Clobbering flags results in all sorts of incorrect emulation, e.g. Jcc instructions often take the wrong path. Sans the nops... asm("push %[flags]; popf; " CALL_NOSPEC " ; pushf; pop %[flags]\n" 0x0003595a <+58>: mov 0xc0(%ebx),%eax 0x00035960 <+64>: mov 0x60(%ebx),%edx 0x00035963 <+67>: mov 0x90(%ebx),%ecx 0x00035969 <+73>: push %edi 0x0003596a <+74>: popf 0x0003596b <+75>: call *%esi 0x000359a0 <+128>: pushf 0x000359a1 <+129>: pop %edi 0x000359a2 <+130>: mov %eax,0xc0(%ebx) 0x000359b1 <+145>: mov %edx,0x60(%ebx) ctxt->eflags = (ctxt->eflags & ~EFLAGS_MASK) | (flags & EFLAGS_MASK); 0x000359a8 <+136>: mov -0x10(%ebp),%eax 0x000359ab <+139>: and $0x8d5,%edi 0x000359b4 <+148>: and $0xfffff72a,%eax 0x000359b9 <+153>: or %eax,%edi 0x000359bd <+157>: mov %edi,0x4(%ebx) For the most part this has gone unnoticed as emulation of guest code that can trigger fast emulation is effectively limited to MMIO when running on modern hardware, and MMIO is rarely, if ever, accessed by instructions that affect or consume flags. Breakage is almost instantaneous when running with unrestricted guest disabled, in which case KVM must emulate all instructions when the guest has invalid state, e.g. when the guest is in Big Real Mode during early BIOS. Fixes: 776b043848fd2 ("x86/retpoline: Add initial retpoline support") Fixes: 1a29b5b7f347a ("KVM: x86: Make indirect calls in emulator speculation safe") Signed-off-by: Sean Christopherson Signed-off-by: Thomas Gleixner Acked-by: Peter Zijlstra (Intel) Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20190822211122.27579-1-sean.j.christopherson@intel.com Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/nospec-branch.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) --- a/arch/x86/include/asm/nospec-branch.h +++ b/arch/x86/include/asm/nospec-branch.h @@ -202,7 +202,7 @@ " lfence;\n" \ " jmp 902b;\n" \ " .align 16\n" \ - "903: addl $4, %%esp;\n" \ + "903: lea 4(%%esp), %%esp;\n" \ " pushl %[thunk_target];\n" \ " ret;\n" \ " .align 16\n" \