Received: by 10.223.176.5 with SMTP id f5csp2825267wra; Mon, 5 Feb 2018 10:28:36 -0800 (PST) X-Google-Smtp-Source: AH8x2273MM9PyA/+0+5O5Kz+OBar8KhZsgfPR3CL36rVESfKb0B0uWlNWKMi/LJCSOTqhPzqfl3B X-Received: by 2002:a17:902:904b:: with SMTP id w11-v6mr44880036plz.41.1517855316340; Mon, 05 Feb 2018 10:28:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517855316; cv=none; d=google.com; s=arc-20160816; b=NrNmR2k8Gz3yNqxDnutTVSeVxI4d04Y7mHGqEiEiCdE+ugXoMuSpdmm6BbKS1gZndx qkg15cy28JTcbuluGGhBZX6nSmHpuaR9Gx4XnR/671WYDPaRrTXvLk2hPzaXoK1Y+0iR LDnvXii5/bFaTes87fCba7ccwsKgCc8GpWVqA+RsiN7Ur3UE34MYXqqJeXQtYchvryla N3YvWAjQhLMFuDLpnyBv89E/CpycOoHbBHvqGtN7dVNjJMgrnIZDiUGBMkp4mTjgiQ79 tZ5OyZ6OOMHx5Q/vFwiuBti1/2LGiwOi/aewT4rMt/CRm3cGArG9JDYnySqKcy9Cq6O4 JGkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=zBfsNEPtxoIxKy2GiOwKCyoSbBq9btzKSnHwE05PFM0=; b=Foy1gDOVxiIZOoBum0Iln9+1VIb+3Dgul3i2itpBpeNWkUPcArLpMBpM4iKRMH+5vn Mqpt+eeOzcC3YsCyW+7sXCyUI2dZkCfa7PvUf+Za8hEax2yPf610qlDFVxk4cmvFwClG hPOmy3hzbZ6C6qPSnPcjeUGVIkYaGlWTw3OXPGxAe68abrN98/yLZL/96t5wpAR5s6sF c5D+F3a/qv1TbBl9twWMvXnRv2/Wxg2Ml6IDtpe1afkKWuYp76OTxQ7SdkH5LR5FLEpF gVdxdEET/WH3sXOXbTXBgzapT0b0DQXq8i5bzmwSI6FngukEf0gKM0/vwMVERmEqerzJ gdqg== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m14si179564pgd.207.2018.02.05.10.28.22; Mon, 05 Feb 2018 10:28:36 -0800 (PST) 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753593AbeBES0W (ORCPT + 99 others); Mon, 5 Feb 2018 13:26:22 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:51448 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753513AbeBESYP (ORCPT ); Mon, 5 Feb 2018 13:24:15 -0500 Received: from localhost (unknown [104.132.1.108]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 7230710B4; Mon, 5 Feb 2018 18:24:07 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Cyril Novikov , Dan Williams , Thomas Gleixner , linux-arch@vger.kernel.org, kernel-hardening@lists.openwall.com, Peter Zijlstra , Catalin Marinas , Will Deacon , Russell King , torvalds@linux-foundation.org, alan@linux.intel.com Subject: [PATCH 4.14 37/64] array_index_nospec: Sanitize speculative array de-references Date: Mon, 5 Feb 2018 10:22:56 -0800 Message-Id: <20180205182140.097335263@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180205182138.571333346@linuxfoundation.org> References: <20180205182138.571333346@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dan Williams dan.j.williams@intel.com commit f3804203306e098dae9ca51540fcd5eb700d7f40 array_index_nospec() is proposed as a generic mechanism to mitigate against Spectre-variant-1 attacks, i.e. an attack that bypasses boundary checks via speculative execution. The array_index_nospec() implementation is expected to be safe for current generation CPUs across multiple architectures (ARM, x86). Based on an original implementation by Linus Torvalds, tweaked to remove speculative flows by Alexei Starovoitov, and tweaked again by Linus to introduce an x86 assembly implementation for the mask generation. Co-developed-by: Linus Torvalds Co-developed-by: Alexei Starovoitov Suggested-by: Cyril Novikov Signed-off-by: Dan Williams Signed-off-by: Thomas Gleixner Cc: linux-arch@vger.kernel.org Cc: kernel-hardening@lists.openwall.com Cc: Peter Zijlstra Cc: Catalin Marinas Cc: Will Deacon Cc: Russell King Cc: gregkh@linuxfoundation.org Cc: torvalds@linux-foundation.org Cc: alan@linux.intel.com Link: https://lkml.kernel.org/r/151727414229.33451.18411580953862676575.stgit@dwillia2-desk3.amr.corp.intel.com Signed-off-by: Greg Kroah-Hartman --- include/linux/nospec.h | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) --- /dev/null +++ b/include/linux/nospec.h @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright(c) 2018 Linus Torvalds. All rights reserved. +// Copyright(c) 2018 Alexei Starovoitov. All rights reserved. +// Copyright(c) 2018 Intel Corporation. All rights reserved. + +#ifndef _LINUX_NOSPEC_H +#define _LINUX_NOSPEC_H + +/** + * array_index_mask_nospec() - generate a ~0 mask when index < size, 0 otherwise + * @index: array element index + * @size: number of elements in array + * + * When @index is out of bounds (@index >= @size), the sign bit will be + * set. Extend the sign bit to all bits and invert, giving a result of + * zero for an out of bounds index, or ~0 if within bounds [0, @size). + */ +#ifndef array_index_mask_nospec +static inline unsigned long array_index_mask_nospec(unsigned long index, + unsigned long size) +{ + /* + * Warn developers about inappropriate array_index_nospec() usage. + * + * Even if the CPU speculates past the WARN_ONCE branch, the + * sign bit of @index is taken into account when generating the + * mask. + * + * This warning is compiled out when the compiler can infer that + * @index and @size are less than LONG_MAX. + */ + if (WARN_ONCE(index > LONG_MAX || size > LONG_MAX, + "array_index_nospec() limited to range of [0, LONG_MAX]\n")) + return 0; + + /* + * Always calculate and emit the mask even if the compiler + * thinks the mask is not needed. The compiler does not take + * into account the value of @index under speculation. + */ + OPTIMIZER_HIDE_VAR(index); + return ~(long)(index | (size - 1UL - index)) >> (BITS_PER_LONG - 1); +} +#endif + +/* + * array_index_nospec - sanitize an array index after a bounds check + * + * For a code sequence like: + * + * if (index < size) { + * index = array_index_nospec(index, size); + * val = array[index]; + * } + * + * ...if the CPU speculates past the bounds check then + * array_index_nospec() will clamp the index within the range of [0, + * size). + */ +#define array_index_nospec(index, size) \ +({ \ + typeof(index) _i = (index); \ + typeof(size) _s = (size); \ + unsigned long _mask = array_index_mask_nospec(_i, _s); \ + \ + BUILD_BUG_ON(sizeof(_i) > sizeof(long)); \ + BUILD_BUG_ON(sizeof(_s) > sizeof(long)); \ + \ + _i &= _mask; \ + _i; \ +}) +#endif /* _LINUX_NOSPEC_H */