Received: by 10.223.176.46 with SMTP id f43csp206223wra; Thu, 18 Jan 2018 16:14:14 -0800 (PST) X-Google-Smtp-Source: ACJfBouvkgpioACMXAhNBqVGABbOJrX893OI3/gGSZS/B8o8x6Vufxz7Nuy7vNzS26pffMb+IzsM X-Received: by 10.101.89.3 with SMTP id f3mr26511287pgu.372.1516320854288; Thu, 18 Jan 2018 16:14:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516320854; cv=none; d=google.com; s=arc-20160816; b=DUsJnphvywdft213k8xN9W7+jihpDjqLee4+iQn3r0gU9HnpFHQ1shi2wXcZDZ08+/ /XQxYKIoZ2DUhrer2/wSIOgPLiFO4y0jm3//Swxp7rOlBWtHW0Tqekn4yF5biI7Akb1g saea1fCfazS5Jdwsx4Zz5ghXjvY7qhhyo10hRLfQ8r7krEG2srd2CKWwAI86QZ0N4E7Z yCc5W8zhD5aKFLdeepp/O8qA1nK/4ci/hH7uuogLxa5nb/MvOnic1exGazFzK0mFMu4b uH1LBm6cedTjW3Airj8QavYlqw5+tTEqNEZHVyhpumA0wakLiHNaxf9KNAggRzCYjT63 gAaw== 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=WhfpIkKvhq27895WRokeQXO4pgxY23SBQzI/YFJmGHY=; b=Ayc+nRWIA6LkyUh8nQejhUkewhOKU9fYaCuJvNIDX/kskVOWdAxIxrv+jjgrJZU8T7 Im8j2jy/5fD+TKsqmTHGC6ma8hWUqYyIOV6JV48IoqX12wRAnX5/yjeWelVYeLbM7cUh 22uhHj+riGTVoBwbkyfZwtgZa9OtLk+L58YGTn8IDw1KB9uAP6ZBEdl28YMYMxvA5yWY gUxIHd2FAxtcUhVDx7D96CgEjPXjNMLHUh8Cfotr/N60HEEY/7fjGMyDetIIX//C5RjS r6y/9ZUsIQpEausgCFO0g6aY05AlpIqlB3AnC8X2rMzagCtJce/oT/DizNLkJiFI7h2O T/bw== 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 r18si7913928pfb.138.2018.01.18.16.14.00; Thu, 18 Jan 2018 16:14:14 -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 S1755377AbeASAL6 (ORCPT + 99 others); Thu, 18 Jan 2018 19:11:58 -0500 Received: from mga11.intel.com ([192.55.52.93]:5057 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754440AbeASALV (ORCPT ); Thu, 18 Jan 2018 19:11:21 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Jan 2018 16:11:21 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,379,1511856000"; d="scan'208";a="11513000" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.16]) by fmsmga008.fm.intel.com with ESMTP; 18 Jan 2018 16:11:20 -0800 Subject: [PATCH v4 06/10] x86, get_user: use pointer masking to limit speculation From: Dan Williams To: linux-kernel@vger.kernel.org Cc: linux-arch@vger.kernel.org, Kees Cook , kernel-hardening@lists.openwall.com, gregkh@linuxfoundation.org, x86@kernel.org, Ingo Molnar , Al Viro , "H. Peter Anvin" , tglx@linutronix.de, torvalds@linux-foundation.org, akpm@linux-foundation.org, alan@linux.intel.com Date: Thu, 18 Jan 2018 16:02:15 -0800 Message-ID: <151632013524.21271.4516878993060956547.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 Quoting Linus: I do think that it would be a good idea to very expressly document the fact that it's not that the user access itself is unsafe. I do agree that things like "get_user()" want to be protected, but not because of any direct bugs or problems with get_user() and friends, but simply because get_user() is an excellent source of a pointer that is obviously controlled from a potentially attacking user space. So it's a prime candidate for then finding _subsequent_ accesses that can then be used to perturb the cache. Unlike the '__get_user' case 'get_user' includes the address limit check near the pointer de-reference. With that locality the speculation can be mitigated with pointer narrowing rather than a barrier. Where the narrowing is performed by: cmp %limit, %ptr sbb %mask, %mask and %mask, %ptr With respect to speculation the value of %ptr is either less than %limit or NULL. Co-developed-by: Linus Torvalds Cc: Al Viro Cc: Kees Cook Cc: Thomas Gleixner Cc: "H. Peter Anvin" Cc: Ingo Molnar Cc: x86@kernel.org Signed-off-by: Dan Williams --- arch/x86/include/asm/smap.h | 17 +++++++++++++++++ arch/x86/lib/getuser.S | 5 +++++ 2 files changed, 22 insertions(+) diff --git a/arch/x86/include/asm/smap.h b/arch/x86/include/asm/smap.h index db333300bd4b..2b4ad4c6a226 100644 --- a/arch/x86/include/asm/smap.h +++ b/arch/x86/include/asm/smap.h @@ -25,6 +25,23 @@ #include +/* + * MASK_NOSPEC - sanitize the value of a user controlled value with + * respect to speculation + * + * In the get_user path once we have determined that the pointer is + * below the current address limit sanitize its value with respect to + * speculation. In the case when the pointer is above the address limit + * this directs the cpu to speculate with a NULL ptr rather than + * something targeting kernel memory. + * + * assumes CF is set from a previous 'cmp TASK_addr_limit, %ptr' + */ +.macro MASK_NOSPEC mask val + sbb \mask, \mask + and \mask, \val +.endm + #ifdef CONFIG_X86_SMAP #define ASM_CLAC \ diff --git a/arch/x86/lib/getuser.S b/arch/x86/lib/getuser.S index c97d935a29e8..07d0e8a28b17 100644 --- a/arch/x86/lib/getuser.S +++ b/arch/x86/lib/getuser.S @@ -40,6 +40,7 @@ ENTRY(__get_user_1) mov PER_CPU_VAR(current_task), %_ASM_DX cmp TASK_addr_limit(%_ASM_DX),%_ASM_AX jae bad_get_user + MASK_NOSPEC %_ASM_DX, %_ASM_AX ASM_STAC 1: movzbl (%_ASM_AX),%edx xor %eax,%eax @@ -54,6 +55,7 @@ ENTRY(__get_user_2) mov PER_CPU_VAR(current_task), %_ASM_DX cmp TASK_addr_limit(%_ASM_DX),%_ASM_AX jae bad_get_user + MASK_NOSPEC %_ASM_DX, %_ASM_AX ASM_STAC 2: movzwl -1(%_ASM_AX),%edx xor %eax,%eax @@ -68,6 +70,7 @@ ENTRY(__get_user_4) mov PER_CPU_VAR(current_task), %_ASM_DX cmp TASK_addr_limit(%_ASM_DX),%_ASM_AX jae bad_get_user + MASK_NOSPEC %_ASM_DX, %_ASM_AX ASM_STAC 3: movl -3(%_ASM_AX),%edx xor %eax,%eax @@ -83,6 +86,7 @@ ENTRY(__get_user_8) mov PER_CPU_VAR(current_task), %_ASM_DX cmp TASK_addr_limit(%_ASM_DX),%_ASM_AX jae bad_get_user + MASK_NOSPEC %_ASM_DX, %_ASM_AX ASM_STAC 4: movq -7(%_ASM_AX),%rdx xor %eax,%eax @@ -94,6 +98,7 @@ ENTRY(__get_user_8) mov PER_CPU_VAR(current_task), %_ASM_DX cmp TASK_addr_limit(%_ASM_DX),%_ASM_AX jae bad_get_user_8 + MASK_NOSPEC %_ASM_DX, %_ASM_AX ASM_STAC 4: movl -7(%_ASM_AX),%edx 5: movl -3(%_ASM_AX),%ecx