From patchwork Tue Jan 30 01:02:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 126192 Delivered-To: patch@linaro.org Received: by 10.46.84.92 with SMTP id y28csp2916461ljd; Mon, 29 Jan 2018 17:11:28 -0800 (PST) X-Google-Smtp-Source: AH8x225jfwRl9504Hj5XXqEsI2HBvjaITGVZ8YuuzcFg46PXpmgQHNs1pU3rjXRyBR9c8B6jremp X-Received: by 2002:a17:902:82c5:: with SMTP id u5-v6mr7627612plz.401.1517274688653; Mon, 29 Jan 2018 17:11:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517274688; cv=none; d=google.com; s=arc-20160816; b=fXS0Yk3ClmtmMqZRSfLuqP1op38B9+TmaAduPDNXKJioriG9NIwDDxqw6/ZnUeirx/ OYGxa2nQJ1gzW/byTimCh+cHG2Zm1Fj7WBA/TmlaRUqy5j6qJIjEuA0NRr3runHrNNcp kgUE6ZiWtnSpIf9EHscTX4aWbosT8YLjjj2r/r/rO2/vRJ5J+UoXGp2pmcZ61cZluI5P kscBfClyDydZdFpFM6eK+EKTVUySUb+anRTFA5Rpc07wxX6YOr2+GQ/fkcUWbp0bXN1C uoQ8ookZEB+vh8bc3ZbqQdYQAKFIdUMWZOkw3QRRvJP3OAcVZ/oR+8/JSgy2YBur2gG0 svxw== 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:cc:to:from :subject:arc-authentication-results; bh=x7UKNmfF68hcKQZGSsIw4ClWmdlIwFxgl9yCpY2X6HQ=; b=q0zcsD3+Hbj02zdih8kk7sOJAIRNxnOtcD9tqBrekwFles23XMgQYrjU8/A7jM9YCw wVmy2CsTCTBEWW2Es4QMZTwAN49LDDBSiFqvjJQ0zv2JS5gAcbeVVoJb8ct/BgQWx/Z/ scetlhSgtRdYXtAAmG+uSFWfPKq8o8TWVixI8bNlJvkZCvyCHr8DZMk5pANSBxb3/txw Ym2gU4scDBdtCTMPhWmI2nT/xVfBYnXcqR7MHey+xgcHxEUWOSePULtufOA/U1GGQHcJ n/RkhkuftlMf8/FPRoFdVbbkNUo5JwoGinnj7FXaUCpb4MyTM9vfwcaE97DW0u9eAFHa bpGg== 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 q24si2881435pgc.276.2018.01.29.17.11.28; Mon, 29 Jan 2018 17:11:28 -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 S1752529AbeA3BL1 (ORCPT + 25 others); Mon, 29 Jan 2018 20:11:27 -0500 Received: from mga12.intel.com ([192.55.52.136]:38864 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752441AbeA3BLX (ORCPT ); Mon, 29 Jan 2018 20:11:23 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga106.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 29 Jan 2018 17:11:22 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,433,1511856000"; d="scan'208";a="15371074" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by fmsmga002.fm.intel.com with ESMTP; 29 Jan 2018 17:11:21 -0800 Subject: [PATCH v6 01/13] Documentation: document array_index_nospec From: Dan Williams To: tglx@linutronix.de, mingo@kernel.org Cc: Mark Rutland , linux-arch@vger.kernel.org, Kees Cook , Jonathan Corbet , Peter Zijlstra , gregkh@linuxfoundation.org, kernel-hardening@lists.openwall.com, Will Deacon , linux-kernel@vger.kernel.org, torvalds@linux-foundation.org, alan@linux.intel.com Date: Mon, 29 Jan 2018 17:02:16 -0800 Message-ID: <151727413645.33451.15878817161436755393.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <151727412964.33451.17213780323040673404.stgit@dwillia2-desk3.amr.corp.intel.com> References: <151727412964.33451.17213780323040673404.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.17.1-9-g687f MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Mark Rutland Document the rationale and usage of the new array_index_nospec() helper. Signed-off-by: Mark Rutland Signed-off-by: Will Deacon Cc: Dan Williams Cc: Jonathan Corbet Cc: Peter Zijlstra Reviewed-by: Kees Cook Signed-off-by: Dan Williams --- Documentation/speculation.txt | 90 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Documentation/speculation.txt diff --git a/Documentation/speculation.txt b/Documentation/speculation.txt new file mode 100644 index 000000000000..e9e6cbae2841 --- /dev/null +++ b/Documentation/speculation.txt @@ -0,0 +1,90 @@ +This document explains potential effects of speculation, and how undesirable +effects can be mitigated portably using common APIs. + +=========== +Speculation +=========== + +To improve performance and minimize average latencies, many contemporary CPUs +employ speculative execution techniques such as branch prediction, performing +work which may be discarded at a later stage. + +Typically speculative execution cannot be observed from architectural state, +such as the contents of registers. However, in some cases it is possible to +observe its impact on microarchitectural state, such as the presence or +absence of data in caches. Such state may form side-channels which can be +observed to extract secret information. + +For example, in the presence of branch prediction, it is possible for bounds +checks to be ignored by code which is speculatively executed. Consider the +following code: + + int load_array(int *array, unsigned int index) + { + if (index >= MAX_ARRAY_ELEMS) + return 0; + else + return array[index]; + } + +Which, on arm64, may be compiled to an assembly sequence such as: + + CMP , #MAX_ARRAY_ELEMS + B.LT less + MOV , #0 + RET + less: + LDR , [, ] + RET + +It is possible that a CPU mis-predicts the conditional branch, and +speculatively loads array[index], even if index >= MAX_ARRAY_ELEMS. This +value will subsequently be discarded, but the speculated load may affect +microarchitectural state which can be subsequently measured. + +More complex sequences involving multiple dependent memory accesses may +result in sensitive information being leaked. Consider the following +code, building on the prior example: + + int load_dependent_arrays(int *arr1, int *arr2, int index) + { + int val1, val2, + + val1 = load_array(arr1, index); + val2 = load_array(arr2, val1); + + return val2; + } + +Under speculation, the first call to load_array() may return the value +of an out-of-bounds address, while the second call will influence +microarchitectural state dependent on this value. This may provide an +arbitrary read primitive. + +==================================== +Mitigating speculation side-channels +==================================== + +The kernel provides a generic API to ensure that bounds checks are +respected even under speculation. Architectures which are affected by +speculation-based side-channels are expected to implement these +primitives. + +The array_index_nospec() helper in can be used to +prevent information from being leaked via side-channels. + +A call to array_index_nospec(index, size) returns a sanitized index +value that is bounded to [0, size) even under cpu speculation +conditions. + +This can be used to protect the earlier load_array() example: + + int load_array(int *array, unsigned int index) + { + if (index >= MAX_ARRAY_ELEMS) + return 0; + else { + index = array_index_nospec(index, MAX_ARRAY_ELEMS); + return array[index]; + } + }