Received: by 10.223.176.46 with SMTP id f43csp205505wra; Thu, 18 Jan 2018 16:13:39 -0800 (PST) X-Google-Smtp-Source: ACJfBos7CRXssKFZRE51GbvowMsN2D9VBL3o9CXVVd9X6fSVUoSKtac+mCO3IHJRHd1Txl1vmBr4 X-Received: by 10.98.14.3 with SMTP id w3mr18418056pfi.154.1516320819317; Thu, 18 Jan 2018 16:13:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516320819; cv=none; d=google.com; s=arc-20160816; b=BtP9ILHtyFy+acDLMPDgvC44GGDwMddEVUnHLcwF+0d5MncY5m9qPJbl42vItsyVj2 22mib7o8onAPjjki+fBHucgTrBopkdX087yA+/FERJ+mJMYA2lJcIIgslMNTOFxkDKRa 22Rea4fsskPPxACRlErosMD89SA699nu3N74pSEWMCLuHteobRLBebPcZj+y3nJrsW4F oD4p4zJ9s4O6j7y47CriltjgZVUN3Ejg2IHJvKRXIpyHqzSDd9T7RWI90QjJhD/zvSoX Lx5yvuK21D4XjzIPi4TWdK7obTsH/t/EjpPdQY+YEfPAphGi+5PVhaSBF60Z1VB2cLbq cu9Q== 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=kF7ofWH91s1WrL2CSh1Df72zggQNvr0nZkQIkzSDJ6M=; b=FuThEn6YA6oaMbgtfgAeV3HLbyBlRXcV27mx1zwSPxLmZYeMDlWwliFIjdt5t7kX5F JxMdwt6d+rGreBE7YjoVZ3E3mkxkz5saRbHJm7wU548hdOn6/VMZPNH14PxMW8Dl4Zn2 g1aU1I9nPnUn+PxDASzbFIwoLXK4XmWrZs5qXCizphPjT8zv3ylNE5BW1JlE2wrJCF0L nlBCMpksGYk6JdX8zXok1Aao2ek3JIQiVPApDidMra2pfDZ/fl8nLcEJLVY1a4Z9fJ+A 34KEm0T9d25zvIj6+cIYFZo6N/0H25ce2HyA6pcikgGN+gFlkcu4PXLXy7h8oF4PpmtL 8vSw== 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 y9si6761792pgp.312.2018.01.18.16.13.25; Thu, 18 Jan 2018 16:13:39 -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 S1755294AbeASALc (ORCPT + 99 others); Thu, 18 Jan 2018 19:11:32 -0500 Received: from mga05.intel.com ([192.55.52.43]:62046 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755093AbeASAK7 (ORCPT ); Thu, 18 Jan 2018 19:10:59 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Jan 2018 16:10:58 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,379,1511856000"; d="scan'208";a="11371267" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by fmsmga007.fm.intel.com with ESMTP; 18 Jan 2018 16:10:58 -0800 Subject: [PATCH v4 03/10] x86: implement array_ptr_mask() From: Dan Williams To: linux-kernel@vger.kernel.org Cc: linux-arch@vger.kernel.org, kernel-hardening@lists.openwall.com, gregkh@linuxfoundation.org, x86@kernel.org, Ingo Molnar , "H. Peter Anvin" , tglx@linutronix.de, torvalds@linux-foundation.org, akpm@linux-foundation.org, alan@linux.intel.com Date: Thu, 18 Jan 2018 16:01:52 -0800 Message-ID: <151632011276.21271.6230510107230588371.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <151632009605.21271.11304291057104672116.stgit@dwillia2-desk3.amr.corp.intel.com> References: <151632009605.21271.11304291057104672116.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.17.1-9-g687f MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 'array_ptr' uses a mask to sanitize user controllable pointers. The x86 'array_ptr_mask' is an assembler optimized way to generate a 0 or ~0 mask if an array index is out-of-bounds or in-bounds. Suggested-by: Linus Torvalds Cc: Thomas Gleixner Cc: Ingo Molnar Cc: "H. Peter Anvin" Cc: x86@kernel.org Signed-off-by: Dan Williams --- arch/x86/include/asm/barrier.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/arch/x86/include/asm/barrier.h b/arch/x86/include/asm/barrier.h index 7fb336210e1b..67f6d4707a2c 100644 --- 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_ptr_mask - generate a mask for array_ptr() that is ~0UL when + * the bounds check succeeds and 0 otherwise + */ +#define array_ptr_mask array_ptr_mask +static inline unsigned long array_ptr_mask(unsigned long idx, unsigned long sz) +{ + unsigned long mask; + + /* + * mask = index - size, if that result is >= 0 then the index is + * invalid and the mask is 0 else ~0 + */ +#ifdef CONFIG_X86_32 + asm ("cmpl %1,%2; sbbl %0,%0;" +#else + asm ("cmpq %1,%2; sbbq %0,%0;" +#endif + :"=r" (mask) + :"r"(sz),"r" (idx) + :"cc"); + return mask; +} + #ifdef CONFIG_X86_PPRO_FENCE #define dma_rmb() rmb() #else