Received: by 10.223.176.5 with SMTP id f5csp714913wra; Fri, 9 Feb 2018 06:07:36 -0800 (PST) X-Google-Smtp-Source: AH8x225VStHVfzAXKKeknlOEvBQb3xxhXmQqLe0th7CjDahX/2p/d2wrIPm/Ppw/Hx2Te2mlNBRu X-Received: by 10.101.75.11 with SMTP id r11mr2477864pgq.36.1518185256468; Fri, 09 Feb 2018 06:07:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518185256; cv=none; d=google.com; s=arc-20160816; b=oPe/5wRtpF2DRBwsx9wCTW0WY0mnpsWVfOA58U9/OSA5Ph8BdpBXtYefXnpAKVzEGj RMyK8jq1bt/COOvEZmHu30pUxTMBcPF5TYtRNgdoBsJhcqssudbVARZZ1ywDXNK2hqur ozyJwXyLdks86CaiJ6Vol81KMwlhcaRHX1nclajHlvzvOq955M3qWyleRwhEbc37IzbX klOzDZoXAcux9HWR3pIzQkHvsVgWayEsvfn5W4LSnRFPd//yVb0xc3lsTDGXK3NHTDfG K+Hylv11sMGEOHFdaLUHWuDdIcvcbdNZ/Qx8asp9MwCCZbcb73o2JpbLerwDpjpXnh+b CEFA== 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=gZA3dbX0ns+XMEksinixFSeb+S/ySMIAUmqnKkLqXNM=; b=dxIDmTM7DhMC3vh4DFr295MB9PHEEkcfPQHYgoLQSPnrl+3/eat0g6InTlLGeJRONt +l5VmwwYcpBgp7pzS0vJ++apqrmq6rj4tkofEsU7rzCuNx2dntmr+Oq4z1OeOH/syVOt tR5ADxAAL4Ew3eIw4j5BzHADTLbQfeVjbBzhSdad7aNHwZrVxLTq7EkpiJICB/Dl0dHU P55fFtFz6hiKYlgkffNbEA3iP5Jqrhc5wYKjSdQtDR5/56SSGV/oTT8gGoBIIuG3IWcZ R0mG31h81f0ROENm4GNpGg+qDIuloYOOMbhZUGjYJEbyhYuY92IYB/lrlqV5PRF1zjZ/ 3Z5Q== 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 p15si1412218pgr.423.2018.02.09.06.07.22; Fri, 09 Feb 2018 06:07: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 S1753125AbeBINm4 (ORCPT + 99 others); Fri, 9 Feb 2018 08:42:56 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:50070 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753109AbeBINmy (ORCPT ); Fri, 9 Feb 2018 08:42:54 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 6871BF5E; Fri, 9 Feb 2018 13:42:53 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Linus Torvalds , Dan Williams , Thomas Gleixner , linux-arch@vger.kernel.org, kernel-hardening@lists.openwall.com, alan@linux.intel.com, David Woodhouse Subject: [PATCH 4.9 58/92] x86: Implement array_index_mask_nospec Date: Fri, 9 Feb 2018 14:39:27 +0100 Message-Id: <20180209133935.416247744@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180209133931.211869118@linuxfoundation.org> References: <20180209133931.211869118@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.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dan Williams (cherry picked from commit babdde2698d482b6c0de1eab4f697cf5856c5859) array_index_nospec() uses a mask to sanitize user controllable array indexes, i.e. generate a 0 mask if 'index' >= 'size', and a ~0 mask otherwise. While the default array_index_mask_nospec() handles the carry-bit from the (index - size) result in software. The x86 array_index_mask_nospec() does the same, but the carry-bit is handled in the processor CF flag without conditional instructions in the control flow. Suggested-by: Linus Torvalds Signed-off-by: Dan Williams Signed-off-by: Thomas Gleixner Cc: linux-arch@vger.kernel.org Cc: kernel-hardening@lists.openwall.com Cc: gregkh@linuxfoundation.org Cc: alan@linux.intel.com Link: https://lkml.kernel.org/r/151727414808.33451.1873237130672785331.stgit@dwillia2-desk3.amr.corp.intel.com Signed-off-by: David Woodhouse Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/barrier.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) --- a/arch/x86/include/asm/barrier.h +++ b/arch/x86/include/asm/barrier.h @@ -23,6 +23,30 @@ #define wmb() asm volatile("sfence" ::: "memory") #endif +/** + * array_index_mask_nospec() - generate a mask that is ~0UL when the + * bounds check succeeds and 0 otherwise + * @index: array element index + * @size: number of elements in array + * + * Returns: + * 0 - (index < size) + */ +static inline unsigned long array_index_mask_nospec(unsigned long index, + unsigned long size) +{ + unsigned long mask; + + asm ("cmp %1,%2; sbb %0,%0;" + :"=r" (mask) + :"r"(size),"r" (index) + :"cc"); + return mask; +} + +/* Override the default implementation from linux/nospec.h. */ +#define array_index_mask_nospec array_index_mask_nospec + #ifdef CONFIG_X86_PPRO_FENCE #define dma_rmb() rmb() #else