Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp470481pxy; Wed, 5 May 2021 06:41:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwuLAPJHKVYhxOsxqdjHUEHKLY6Z6zUyRXh6Zx99LELxmKZFM+qX0KanfT0Tgj/uvWtD+Sd X-Received: by 2002:a05:6a00:2308:b029:28e:d370:d435 with SMTP id h8-20020a056a002308b029028ed370d435mr9361473pfh.31.1620222094874; Wed, 05 May 2021 06:41:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620222094; cv=none; d=google.com; s=arc-20160816; b=Mi2+7Y0yClnp7BzYzYCsI1/CfbF+KKaUfYhC1A+ANdv7hrn8ro+UCItvAJfMA3cv6J pzNkIDABaUSLWym5l6mRZ9kWLSLOTpwx6Uu/XeFkQ4HzlRuS+KFuNBkldbp9dV77lb6D 5FleK8pubqHPRdyv2aElyIQGrvxMfcPTxJEslE/Paus50Z+Acr3K4I9bft3ZPpVfik7Y 9B+A7eUCaUZlg6NbYLaOwpD/NSbgHcBvsr6SonvLPbFKJNjHHsopfDV/oIxAhC3vyWnH x4UxTHdcSRn9wLmLkBWkViMrQPSFlxrRSaltO7HcwvLdIqNrajJpWUy/QjhmZoKQe7Ii mJGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=buwK358EXACur1dtLJkV3OPco7maWEjjFk36E2kFDig=; b=HTMHUET/oNxb0E7IQz4ABCOGRM4Gj2ruHKX8s9h9zP4oPhBisgKgkC5wEsaUdtHlzf Oriv+BNRiFk24lkfvAG643yGnv7uIga6rgIj80544Ua1o6zPOuEoOVHgCWt3bf9AiaxH JlaeonBOSE+kqIylvnBtAur/FA+uYciKZIiG9I+TwLK6VvEEOkXrszmmz2x9bHbiJdzA wv428lJknChvTdRwR0hNxqtBdemNtP5levIBOJ7IxOpg0wemUsQ0ubXgqC0n6w++keo2 j1Kvr/+0wJ+hAUzXJREEhfidVdZC0X0fEFX+Sb5NKCEPwtHFEG5etl+TJmDMriFWhUec uEjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=J72tJ8AG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e6si6992133pge.592.2021.05.05.06.41.22; Wed, 05 May 2021 06:41:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=J72tJ8AG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232221AbhEENVK (ORCPT + 99 others); Wed, 5 May 2021 09:21:10 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:20583 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229606AbhEENVI (ORCPT ); Wed, 5 May 2021 09:21:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620220811; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=buwK358EXACur1dtLJkV3OPco7maWEjjFk36E2kFDig=; b=J72tJ8AG3ZGzX0Knlhwui0TTPAjACPE7Es75IJD2Aj/Ms/dnaQ9jkEJwCD9RdKk8zABJiK oGfK9Ze2lHQ0UFUv/D6SsfbnCdS0XfTGhm/TP2qYtcLTsxK6zrMTlmEWuz04yDyoF3rjL6 NlWCfaOIAWRBjpP5JTZWv2eSjhYAOek= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-127-mYc0GcoHMeSmsywLbE8jWg-1; Wed, 05 May 2021 09:20:07 -0400 X-MC-Unique: mYc0GcoHMeSmsywLbE8jWg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E0B6584A5FD; Wed, 5 May 2021 13:19:46 +0000 (UTC) Received: from treble (ovpn-115-93.rdu2.redhat.com [10.10.115.93]) by smtp.corp.redhat.com (Postfix) with SMTP id 4B73D5D703; Wed, 5 May 2021 13:19:44 +0000 (UTC) Date: Wed, 5 May 2021 08:19:43 -0500 From: Josh Poimboeuf To: David Laight Cc: Al Viro , "x86@kernel.org" , "linux-kernel@vger.kernel.org" , Linus Torvalds , Will Deacon , Dan Williams , Andrea Arcangeli , Waiman Long , Peter Zijlstra , Thomas Gleixner , Andrew Cooper , Andy Lutomirski , Christoph Hellwig , Mark Rutland , Borislav Petkov Subject: Re: [PATCH v4 3/4] x86/uaccess: Use pointer masking to limit uaccess speculation Message-ID: <20210505131943.ci2svd6fmb22y7ac@treble> References: <5ba93cdbf35ab40264a9265fc24575a9b2f813b3.1620186182.git.jpoimboe@redhat.com> <2f75c496ac774444b75ff808854b8e5f@AcuMS.aculab.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <2f75c496ac774444b75ff808854b8e5f@AcuMS.aculab.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 05, 2021 at 08:48:48AM +0000, David Laight wrote: > From: Josh Poimboeuf > > Sent: 05 May 2021 04:55 > > > > The x86 uaccess code uses barrier_nospec() in various places to prevent > > speculative dereferencing of user-controlled pointers (which might be > > combined with further gadgets or CPU bugs to leak data). > ... > > Remove existing barrier_nospec() usage, and instead do user pointer > > masking, throughout the x86 uaccess code. This is similar to what arm64 > > is already doing with uaccess_mask_ptr(). > ... > > diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h > > index fb75657b5e56..ebe9ab46b183 100644 > > --- a/arch/x86/include/asm/uaccess.h > > +++ b/arch/x86/include/asm/uaccess.h > > @@ -66,12 +66,35 @@ static inline bool pagefault_disabled(void); > > * Return: true (nonzero) if the memory block may be valid, false (zero) > > * if it is definitely invalid. > > */ > > -#define access_ok(addr, size) \ > > +#define access_ok(addr, size) \ > > ({ \ > > WARN_ON_IN_IRQ(); \ > > likely(!__range_not_ok(addr, size, TASK_SIZE_MAX)); \ > > }) > > > > +/* > > + * Sanitize a user pointer such that it becomes NULL if it's not a valid user > > + * pointer. This prevents speculatively dereferencing a user-controlled > > + * pointer to kernel space if access_ok() speculatively returns true. This > > + * should be done *after* access_ok(), to avoid affecting error handling > > + * behavior. > > + */ > > +#define mask_user_ptr(ptr) \ > > +({ \ > > + unsigned long _ptr = (__force unsigned long)ptr; \ > > + unsigned long mask; \ > > + \ > > + asm volatile("cmp %[max], %[_ptr]\n\t" \ > > + "sbb %[mask], %[mask]\n\t" \ > > + : [mask] "=r" (mask) \ > > + : [_ptr] "r" (_ptr), \ > > + [max] "r" (TASK_SIZE_MAX) \ > > + : "cc"); \ > > + \ > > + mask &= _ptr; \ > > + ((typeof(ptr)) mask); \ > > +}) > > + > > access_ok() and mask_user_ptr() are doing much the same check. > Is there scope for making access_ok() return the masked pointer? > > So the canonical calling code would be: > uptr = access_ok(uptr, size); > if (!uptr) > return -EFAULT; > > This would error requests for address 0 earlier - but I don't > believe they are ever valid in Linux. > (Some historic x86 a.out formats did load to address 0.) > > Clearly for a follow up patch. Yeah. I mentioned a similar idea in the cover letter. But I'm thinking we should still rename it to access_ok_mask(), or otherwise change the API to avoid the masked value getting ignored. But that'll be a much bigger patch. -- Josh