From patchwork Fri Feb 23 10:41:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jinpu Wang X-Patchwork-Id: 10237435 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 0CA3C602DC for ; Fri, 23 Feb 2018 11:24:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 020E929515 for ; Fri, 23 Feb 2018 11:24:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EA0D629530; Fri, 23 Feb 2018 11:24:28 +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 E2BBC29515 for ; Fri, 23 Feb 2018 11:24:26 +0000 (UTC) Received: (qmail 13520 invoked by uid 550); 23 Feb 2018 11:22:16 -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 4074 invoked from network); 23 Feb 2018 10:42:45 -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=qf+M5sny4qTsp1WhrvpMRRzF0J/RLDLkCnHjX5Hcc0s=; b=j7eixpY8JmU0cjCYERWDc3oz+ssB467S5+aEeu0v3+s5yXNsGZ9bjYtM6eL5qLoi87 XiVtY/wETv0RCyyc4U773WbLgFhEg3dTLlGiAgru9GWflbQGe9O/5wC2npovtvLJgLNB sSlb5kMlzBnCgtAJ0cpKVZ16pqQ8wsXE/bb2hY3DSCYxXntEUYuUuG9fdm0ABa/dkOEl zHl4XTWMxxrbSj4LeIWHUlR8g88jpl2zbrc+Uds4iid3C1qII/92/Md3J569v5BuUkCD AiyegxDZyoNIolwqQ5fVu91hxwospzhwWyX1HexaTOyWfbWoeWR/AhTRfUHzN9UCYgSb BJFA== 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=qf+M5sny4qTsp1WhrvpMRRzF0J/RLDLkCnHjX5Hcc0s=; b=cXPGr2cr9IRjtwzxj9f4qC7m5tCT5+Rl4uOgslIdN+A4Zp4Fh0UihdjuVgfimHHCvu CaCKsvpPLIadXj/GAqLtpThz6ZFpz5Wx5lZoKZC7DtAwmN1ZwvWwM2yr4BZMfTW+mq6N tB4IMuH5H4SxRjjPmbDRF9EtcHt9hWiU6Ec7ZLnLBeczfMGS0e3pxvIJxnRKj6rCP+R0 4ZREsU3knT0grmvZLaFNpUMuTj7bDNrYiEIRPLEEJctva7KEx7Q5g4Qryur6tnhaTWy4 Ljri9KPkr7bPdrk3W1sRDH3JS+Z8z9O9u5kTpgCo0Mn+0GMeQO6vXfSpg2uNczdzY8VJ CVMQ== X-Gm-Message-State: APf1xPAnljQIdBJjS7KeVcdrcyvA2fbF5PMZ4dweO3mQD8UTD3Mk85MX dTh132cvvE0te28Lw9lQXuTnDA== X-Google-Smtp-Source: AH8x227oGk/KoTN3LECgrQrDL1iHWVFF2bX0CLLwsn9BzpRW8SZRAgKJRlb5Xdwrw4WmzuMzQAPZ0w== X-Received: by 10.223.154.165 with SMTP id a34mr1295738wrc.85.1519382553099; Fri, 23 Feb 2018 02:42:33 -0800 (PST) From: Jack Wang X-Google-Original-From: Jack Wang To: gregkh@linuxfoundation.org, stable@vger.kernel.org Cc: Mark Rutland , Will Deacon , Dan Williams , Thomas Gleixner , linux-arch@vger.kernel.org, Jonathan Corbet , Peter Zijlstra , kernel-hardening@lists.openwall.com, torvalds@linux-foundation.org, alan@linux.intel.com, David Woodhouse , Jack Wang Subject: [stable 4.4 10/29] Documentation: Document array_index_nospec Date: Fri, 23 Feb 2018 11:41:59 +0100 Message-Id: <1519382538-15143-11-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: Mark Rutland (cherry picked from commit f84a56f73dddaeac1dba8045b007f742f61cd2da) Document the rationale and usage of the new array_index_nospec() helper. Signed-off-by: Mark Rutland Signed-off-by: Will Deacon Signed-off-by: Dan Williams Signed-off-by: Thomas Gleixner Reviewed-by: Kees Cook Cc: linux-arch@vger.kernel.org Cc: Jonathan Corbet Cc: Peter Zijlstra Cc: gregkh@linuxfoundation.org Cc: kernel-hardening@lists.openwall.com Cc: torvalds@linux-foundation.org Cc: alan@linux.intel.com Link: https://lkml.kernel.org/r/151727413645.33451.15878817161436755393.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 --- 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 0000000..e9e6cba --- /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]; + } + }