Received: by 10.223.176.5 with SMTP id f5csp2855398wra; Mon, 5 Feb 2018 11:01:36 -0800 (PST) X-Google-Smtp-Source: AH8x224N7/TC9otBbTrfRH8ehE++JCTp2SAECIs9UJt4w1fr+8d1b7AEs3oyAwoLXxhg2evgwaXR X-Received: by 2002:a17:902:968e:: with SMTP id n14-v6mr45138456plp.21.1517857296341; Mon, 05 Feb 2018 11:01:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517857296; cv=none; d=google.com; s=arc-20160816; b=pZUAK0K5baxOFoyyzNJcFZ+Q3uq2w0gSr8DANlKuEUgBti4JPkAUWZvW772Esbfvcp GuYgv5EGlsf8g7pc69O9+I0iTj3XSADM5jMqLlc3Rgft5npf9GnEp2RQhqlFW2nnPiy6 gG5aywSLol4Ap9eEO80Je8AGxKBV4mMMX5YRmSnvJzCebzeG/9WUX9jt8ndPdSbfenIi OqwKEGiZkQp8D/JURIq7NmxUfRBwiRh/f3E3Jo92rRsC8n+q0pZNRzB6qFoz39yhLAbQ ZsUTQwghl/yI88WGtGbeidjqp1KgnITHp9dI2m+E5EJMYbp1rwd6m5NrRd5usqhq0tv/ tavw== 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=bLRGGgxbbF1S/s+kS8Ha3QGxuYjsfbbaRVG4pJ4Xcoc=; b=njt5JkopfXJMo5n8Gc/dTs3bvEz1SWo97Ih12EjAT0feh/KKjU9ibbmC498Tz3y2Or p5Vdvq71T8j3Bkyg1tSSCpQ+Uoi5oqac3qbQNF8XULILeGt91fh+WrECThdzOBECoYjB U0FZamIzDnpETquQPrfeR4KJK5C3V61sVhqtKICIYiFNxhEDFzpe+9Y9O8z0oSy9S5go +/0Jw5U8l1eEeJT+Jgg6L6215z1hbxIfHIoDPvhWLrt5JRvQVtR1861LrPlIdNKvwsqp 3ljpQ+SGwIgeN68KodXOFZkt7Rg1eSrCvmBiNb1QVWaL6eRXaqq80vZYJqj7fv29vbtO cHXA== 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 h20si5051553pfh.395.2018.02.05.11.01.21; Mon, 05 Feb 2018 11:01: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 S1753879AbeBETBA (ORCPT + 99 others); Mon, 5 Feb 2018 14:01:00 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:51914 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753591AbeBESYj (ORCPT ); Mon, 5 Feb 2018 13:24:39 -0500 Received: from localhost (unknown [104.132.1.108]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id DE80B10D9; Mon, 5 Feb 2018 18:24:36 +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 Subject: [PATCH 4.14 38/64] x86: Implement array_index_mask_nospec Date: Mon, 5 Feb 2018 10:22:57 -0800 Message-Id: <20180205182140.147895085@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180205182138.571333346@linuxfoundation.org> References: <20180205182138.571333346@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.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dan Williams dan.j.williams@intel.com 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: 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 @@ -24,6 +24,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