From patchwork Fri Feb 23 10:42:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinpu Wang X-Patchwork-Id: 10237441 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2C59D602DC for ; Fri, 23 Feb 2018 11:25:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 21E4229530 for ; Fri, 23 Feb 2018 11:25:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 14EA529537; Fri, 23 Feb 2018 11:25:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id DF7FE29530 for ; Fri, 23 Feb 2018 11:25:03 +0000 (UTC) Received: (qmail 15641 invoked by uid 550); 23 Feb 2018 11:22:38 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Delivered-To: moderator for kernel-hardening@lists.openwall.com Received: (qmail 5227 invoked from network); 23 Feb 2018 10:42:47 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=profitbricks-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=az5+eHnWE5ZySu112Oa5bHuEurj9kEe0JF4f+AEVAqU=; b=hjPvYKk2RFbd9QY2lm9TV/RIDmtYbk/TNmIgKOBNC+5agzTviu7LXwwWcJGIU/YXZu WbbMVpx/kUNvzvmIOj5a1VIqJ+a7PQfb0r4Fy1bTxawRmmtSdPk6w09ruwATpj5W1CEJ jW7Yvh8MpCXT4Cy+O4aPHDfaaBKic7ywFHPEXnx5qlXB6Vsv/d+xI8x8D+WprrRvmKWR D1oVtSRkzFM8k71ItDp5RkVbWDWt2S7p9rY7fRnl8gXa/GhytegyhE5zq5u9QVAGlNjM tSCzZvtBepPTHbJ2zje5u+YQ7kkQk8NMcDdva8+greziyRfkAykGbt8knmjz/PRL2Ji5 i4Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=az5+eHnWE5ZySu112Oa5bHuEurj9kEe0JF4f+AEVAqU=; b=pIfaJXLhgEEpBTohN5fpMsqdElPIctUQAvG9MC+ofVytE4C5ZVm+lAhvylo9MLN9NT cw3N98GCGfJCSZW0+QkA/rId2ziJqkX1uHX8TbBvTvCZCSjCi/qq9sDrB3c3mDXvovsH XNAb7k4ZqDI5e1SRTzG5U6JrO3FshIWn5LptwcUztGaLSW6r4FzjNg4nEHdzsZNBP+Wa LZG5uHwM5+bp5Yhr1wW+fKWE/5+RAyG4CGbPlGCvRh+mm8oOdH1DjoS0ONlHodWmDH80 WPGpNvvrj7PSnrepeAD0xEHVp/tsiTbeZu71jQzKZyhiqA+ZTpZ8SHKH+QZTxYMcV8cz 6fVQ== X-Gm-Message-State: APf1xPBtYlPctuqkmfo4OHoYfMz9lilHNxZYnC601FUOV+1105FpHg85 HjgJTc6H/EkMzNt2HBNKX+Ddrg== X-Google-Smtp-Source: AG47ELs1jlQUGeoY6fH3TeJS/XtBPQ0qKVfu4TxCrVmh8ct7bs3TCoYm9Fe9JxxmFv99qsN1HVYs6A== X-Received: by 10.28.1.208 with SMTP id 199mr1307947wmb.26.1519382556402; Fri, 23 Feb 2018 02:42:36 -0800 (PST) From: Jack Wang X-Google-Original-From: Jack Wang To: gregkh@linuxfoundation.org, stable@vger.kernel.org Cc: Dan Williams , Thomas Gleixner , linux-arch@vger.kernel.org, Tom Lendacky , Kees Cook , kernel-hardening@lists.openwall.com, Al Viro , alan@linux.intel.com, David Woodhouse , Jack Wang Subject: [stable 4.4 13/29] x86: Introduce barrier_nospec Date: Fri, 23 Feb 2018 11:42:02 +0100 Message-Id: <1519382538-15143-14-git-send-email-jinpu.wangl@profitbricks.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1519382538-15143-1-git-send-email-jinpu.wangl@profitbricks.com> References: <1519382538-15143-1-git-send-email-jinpu.wangl@profitbricks.com> X-Virus-Scanned: ClamAV using ClamSMTP From: Dan Williams (cherry picked from commit b3d7ad85b80bbc404635dca80f5b129f6242bc7a) Rename the open coded form of this instruction sequence from rdtsc_ordered() into a generic barrier primitive, barrier_nospec(). One of the mitigations for Spectre variant1 vulnerabilities is to fence speculative execution after successfully validating a bounds check. I.e. force the result of a bounds check to resolve in the instruction pipeline to ensure speculative execution honors that result before potentially operating on out-of-bounds data. No functional changes. Suggested-by: Linus Torvalds Suggested-by: Andi Kleen Suggested-by: Ingo Molnar Signed-off-by: Dan Williams Signed-off-by: Thomas Gleixner Cc: linux-arch@vger.kernel.org Cc: Tom Lendacky Cc: Kees Cook Cc: kernel-hardening@lists.openwall.com Cc: gregkh@linuxfoundation.org Cc: Al Viro Cc: alan@linux.intel.com Link: https://lkml.kernel.org/r/151727415361.33451.9049453007262764675.stgit@dwillia2-desk3.amr.corp.intel.com Signed-off-by: David Woodhouse Signed-off-by: Greg Kroah-Hartman [jwang: cherry pick to 4.4] Signed-off-by: Jack Wang --- arch/x86/include/asm/barrier.h | 4 ++++ arch/x86/include/asm/msr.h | 3 +-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/barrier.h b/arch/x86/include/asm/barrier.h index b5028e3..814ef83 100644 --- a/arch/x86/include/asm/barrier.h +++ b/arch/x86/include/asm/barrier.h @@ -48,6 +48,10 @@ static inline unsigned long array_index_mask_nospec(unsigned long index, /* Override the default implementation from linux/nospec.h. */ #define array_index_mask_nospec array_index_mask_nospec +/* Prevent speculative execution past this barrier. */ +#define barrier_nospec() alternative_2("", "mfence", X86_FEATURE_MFENCE_RDTSC, \ + "lfence", X86_FEATURE_LFENCE_RDTSC) + #ifdef CONFIG_X86_PPRO_FENCE #define dma_rmb() rmb() #else diff --git a/arch/x86/include/asm/msr.h b/arch/x86/include/asm/msr.h index 77d8b28..5a10ac8 100644 --- a/arch/x86/include/asm/msr.h +++ b/arch/x86/include/asm/msr.h @@ -147,8 +147,7 @@ static __always_inline unsigned long long rdtsc_ordered(void) * that some other imaginary CPU is updating continuously with a * time stamp. */ - alternative_2("", "mfence", X86_FEATURE_MFENCE_RDTSC, - "lfence", X86_FEATURE_LFENCE_RDTSC); + barrier_nospec(); return rdtsc(); }