Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965095AbeALRvN (ORCPT + 1 other); Fri, 12 Jan 2018 12:51:13 -0500 Received: from mx1.redhat.com ([209.132.183.28]:24664 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964844AbeALRvM (ORCPT ); Fri, 12 Jan 2018 12:51:12 -0500 Date: Fri, 12 Jan 2018 11:51:09 -0600 From: Josh Poimboeuf To: Dan Williams Cc: linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, Andi Kleen , Kees Cook , kernel-hardening@lists.openwall.com, 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 Subject: Re: [PATCH v2 07/19] x86: introduce __uaccess_begin_nospec and ASM_IFENCE Message-ID: <20180112175109.yoz4jaaipztdj34k@treble> References: <151571798296.27429.7166552848688034184.stgit@dwillia2-desk3.amr.corp.intel.com> <151571802258.27429.932636277047687877.stgit@dwillia2-desk3.amr.corp.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <151571802258.27429.932636277047687877.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: Mutt/1.6.0.1 (2016-04-01) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 12 Jan 2018 17:51:12 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Return-Path: On Thu, Jan 11, 2018 at 04:47:02PM -0800, Dan Williams wrote: > For 'get_user' paths, do not allow the kernel to speculate on the value > of a user controlled pointer. In addition to the 'stac' instruction for > Supervisor Mode Access Protection, an 'ifence' causes the 'access_ok' > result to resolve in the pipeline before the cpu might take any > speculative action on the pointer value. So I understand the need to "patch first and ask questions later". I also understand that usercopy is an obvious attack point for speculative bugs. However, I'm still hopelessly confused about what exactly this patch (and the next one) are supposed to accomplish. I can't figure out if: a) I'm missing something completely obvious; b) this is poorly described; or c) it doesn't actually fix/protect/harden anything. The commit log doesn't help me at all. In fact, it confuses me more. For example, this paragraph: > Since this is a major kernel interface that deals with user controlled > data, the '__uaccess_begin_nospec' mechanism will prevent speculative > execution past an 'access_ok' permission check. While speculative > execution past 'access_ok' is not enough to lead to a kernel memory > leak, it is a necessary precondition. That just sounds wrong. What if the speculation starts *after* the access_ok() check? Then the barrier has no purpose. Most access_ok/get_user/copy_from_user calls are like this: if (copy_from_user(...uptr..)) /* or access_ok() or get_user() */ return -EFAULT; So in other words, the usercopy function is called *before* the branch. But to halt speculation, the lfence needs to come *after* the branch. So putting lfences *before* the branch doesn't solve anything. So what am I missing? -- Josh