Received: by 10.223.176.5 with SMTP id f5csp2828853wra; Mon, 5 Feb 2018 10:32:13 -0800 (PST) X-Google-Smtp-Source: AH8x227NiGWf/JqxcYmDTQT9CgjP5eZ7p4EJ+rvoho4/5ACefZ6/opsT16xB8Z2169jo6DMaX0Fu X-Received: by 2002:a17:902:396a:: with SMTP id e39-v6mr25216201plg.324.1517855533888; Mon, 05 Feb 2018 10:32:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517855533; cv=none; d=google.com; s=arc-20160816; b=0gwxS9CSBZMiF7c2DDpuDx6eJjMeQn1YwAgNLz6Lh70TyBv0DDEzjUJXTMEe9zQMrK UTyFO7LKB9PGbaDS62n+9sODNb5y2OYnu/VhN1PoSFJgp02mhp0ITGmqHissDSCaJq9R K3zl+eOd1s1tg+pgXyNArAvBy3gDeZU3qhlNy9H6Rspq0KpKsi/yij5ame+Gid/A6O67 xuUcMXn/zIxl5OYy+ZAEayJhG2vtIyJ1WWT+3TTBtNV9wiaScE7RpnqNtjksar13UUb1 kC8GTg5fWdL1GJucaEITo+jvIsal3qcc66XV/oC7yJ4/eRUnm7gNcmk2Vn0WDvaGx2zP 08pA== 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=qvaU4YXIQEFLhz524gVG8SuCGktClYtMASQIZYOHi/w=; b=j910r7M9CG9q4vys6P0M9HTiagk+fBNOKOouTh7Pn5ZUQWkqSYkdHmaHvUsSWv8mzI +rUP1Cy0qLFj7bntxODlcUbjwx7TcSWtFt55cGqNoQkcDX2q6OgNNaHOounVo2iB5HfT ZuSvcATb/wy7ZgKJ8ZWG1onSqJaxVIkMO5sI1hYKJ4JeVPQufqVs+BeXr3qLycO3SoEG FiaAvh2faKHJPzs/zfMga4r2BeUYi71/wgX5w/ckpIWJmcZGhBeTnbBvK3mMAY6xXBSd w/VaF0eq8mrs1XQ2HbVV2tEeI+HRI2aItdKEww8GIfOleTHJ2VgyUooS8KxPeWzrZowu hbjA== 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 f15si95358pgt.312.2018.02.05.10.31.59; Mon, 05 Feb 2018 10:32:13 -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 S1753572AbeBESaE (ORCPT + 99 others); Mon, 5 Feb 2018 13:30:04 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:52602 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753535AbeBESZ0 (ORCPT ); Mon, 5 Feb 2018 13:25:26 -0500 Received: from localhost (unknown [104.132.1.108]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 64F9F1131; Mon, 5 Feb 2018 18:25:15 +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.15 33/60] array_index_nospec: Sanitize speculative array de-references Date: Mon, 5 Feb 2018 10:23:06 -0800 Message-Id: <20180205182215.312530327@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180205182213.902626065@linuxfoundation.org> References: <20180205182213.902626065@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.15-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 */