From patchwork Wed Jan 3 22:38:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 123360 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp10722553qgn; Wed, 3 Jan 2018 14:39:00 -0800 (PST) X-Google-Smtp-Source: ACJfBoume3kHUqpCv0eLi5zE9rWaITACXIgMspE4ETkgg4hKDbwJ7qtLhJ770FGt4xcIHS6uQnTl X-Received: by 10.84.198.67 with SMTP id o61mr2750835pld.229.1515019140422; Wed, 03 Jan 2018 14:39:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1515019140; cv=none; d=google.com; s=arc-20160816; b=JJRCPGfY8k/a5YCjH8eUdLcybYqjggzF0jEmXuHXnDrU3F1aVOjK1TWcHsYKSmM/Dw CCofWnEMXy8gFgQ4WPM7Y4dLCq54cUxb5AsLeTtepDJ8qy06SsoWvLQxOWZsL8MtW9eb JiPVq6OgrGdFLAaApr0joy7T5yda5n136HiIQJPnCqoRXsjhOpBI4esj+QILXmAJGkfm dpoWQyoUeZhd4bjnSy/31+IxsEQe4EU9FzjHgbsMFAWenf3fLJoFRiIb/diYAtrYcdYZ 8eajiwQ1qCF2qdQ2iWWxXD0V998wvIox1Wpkei5rGKHnk+yoJjhN2JuyPh5CUgIH3VEQ acXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=jxS4YClD+1AU65lUv//CQpvaInYJey3daUvbSf8jOpE=; b=WuURpCQ2sD4V5bcOUngL/OhIqDgYRznzCwwEbRFJyd9WMXJP2TaH6QPmhmkFIc2HYc 06tmEyHKDFFjCA0RIW+NzPF1YB8sJHI+WNenoZcsgJz5XqBQA6zxw2i5zFxW0hICyxW1 88jfq+NKs4wDOlo2NfaeOdysgPwYvbwtwrMtJU+JmahyO2YtJMwS2BZ96t+pMvm+j8Ni cdt5cHhpwLlR/0YGNLqbQxY7hK6tdj9YiZ7gg+0OLnyEHNp7fim8wx3/8K8aSE2uGe6i SVykrlXykk3tGoUHbY3lOZsYQaUCsRmM/+GouY6HYiMS+3RSIKSzs6Y3B8rQuY1r2twu hPzw== 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 h34si1309456pld.202.2018.01.03.14.39.00; Wed, 03 Jan 2018 14:39:00 -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 S1751403AbeACWim (ORCPT + 28 others); Wed, 3 Jan 2018 17:38:42 -0500 Received: from foss.arm.com ([217.140.101.70]:55506 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751302AbeACWij (ORCPT ); Wed, 3 Jan 2018 17:38:39 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id EFB291529; Wed, 3 Jan 2018 14:38:38 -0800 (PST) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 107B63F24A; Wed, 3 Jan 2018 14:38:37 -0800 (PST) From: Mark Rutland To: linux-kernel@vger.kernel.org Cc: linux-arch@vger.kernel.org, Mark Rutland , Will Deacon Subject: [RFC PATCH 2/4] Documentation: document nospec helpers Date: Wed, 3 Jan 2018 22:38:25 +0000 Message-Id: <20180103223827.39601-3-mark.rutland@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180103223827.39601-1-mark.rutland@arm.com> References: <20180103223827.39601-1-mark.rutland@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Document the rationale and usage of the new nospec*() helpers. Signed-off-by: Mark Rutland Signed-off-by: Will Deacon --- Documentation/speculation.txt | 99 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Documentation/speculation.txt -- 2.11.0 diff --git a/Documentation/speculation.txt b/Documentation/speculation.txt new file mode 100644 index 000000000000..0bec4ed5ac29 --- /dev/null +++ b/Documentation/speculation.txt @@ -0,0 +1,99 @@ +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 idx) { + if (idx >= MAX_ARRAY_ELEMS) + return 0; + else + return array[idx]; + } + +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[idx], even if idx >= 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 idx) { + int val1, val2, + + val1 = load_array(arr1, idx); + 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 following helpers found in can be used to prevent +information from being leaked via side-channels. + +* nospec_load(ptr, lo, hi) + + Returns the data at *ptr only if ptr falls in the [lo, hi) interval. When + ptr < lo or ptr >= hi, typeof(*ptr)0 is returned, even under speculation. + + This does not prevent an out-of-bounds load from being speculated, but does + prevent its value from influencing code which is subsequently speculated, + preventing the value from being leaked. + +* nospec_array_load(arr, idx, sz) + + Returns the data at arr[idx] only if idx falls in the [0, sz) interval. When + idx < 0 or idx > sz, typeof(*arr)0 is returned, even under speculation. + + This is a wrapper around nospec_load() provided for convenience. + +* nospec_ptr(ptr, lo, hi) + + Returns a sanitized pointer that is bounded by the [lo, hi) interval, even + under speculation. If ptr < lo, or ptr >= hi, NULL is returned. + + This is expected to be used by code which computes a pointer to an element + of a data structure, or where multiple fields of a data structure will be + accessed. + + Note that it is not safe to compare the returned value to the original + pointer, as compiler optimizations may infer that the original unsanitized + pointer is safe to use when the two compare equal.