Received: by 10.223.176.46 with SMTP id f43csp1357844wra; Fri, 19 Jan 2018 10:19:30 -0800 (PST) X-Google-Smtp-Source: ACJfBouMd8lmB6se+lt/q2YUR771uBdxwWUqLkXJDegcpr15lPg1pJjiDthh0aDgkmZq12JbzECE X-Received: by 10.98.245.69 with SMTP id n66mr36103067pfh.137.1516385970838; Fri, 19 Jan 2018 10:19:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516385970; cv=none; d=google.com; s=arc-20160816; b=o7oUeuxvDgjRPrIN3RllRnsUuNNA5fRO0gqKfe8fffgzxNWyfFzeTSTBVThUvf/7K+ 1t9/da9doP7e/xUDZFc40Boz2FCpB1N+8pnM2F45xEipa5aGKGFqE+/viQ8/dSRFcWJr A4blrliH0CqmPqLhzIzxCdWS2HXZxecCLgyQeoWgjSTos2786WcVyOG3XPEwXjCu5hhc NHio/GHBNwfeRILBphRsU4zV12uzTe6LaGzwy3zQmP2oVeTF6bWOPYCkRxPY4IWSOVIz N0XpsrXduJaGW7JnFjos7KKcF8I75yZ6Pk2Tq8CG0SHyCnhn79DhOh/iNm8TO0kmu0PK UZ2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=4XhHurMK/2nXtYrxVRiwjVry8OK39pOlUSg5N+xy4l8=; b=gwXgqXpzf7JgM/Mj3R2RAN8IWu7l9HPepyalT4HVcOkBBdFD6cYuyG3zl+UuNxUmJa rxIYZD1he9AkE5XVQxY9x3XuNk/I7dr1mQtd8xCwfmJsy0y47e/XTbwzBWAJHD9ILl28 3wZ0kXMcgsi7FZmk/SEBANzbboAtSprG65Wc867MEH9ULUVBilBrMsTbdkZuYclEKztS 1S+VH7i7B1MynVJRNiIK1Qhs1UW2HHLjRj/6PAZcBr2LVA/lAFwHTAsD/xxWvNLdto8U YHru1T4xQ7kjudV6qqbwJ4PGaD0qWekEG9pJX0pjR1Y/QIjoHffTtyWunGDRjGMWwhe+ 5AVQ== 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 j1si8781691pgn.209.2018.01.19.10.19.17; Fri, 19 Jan 2018 10:19:30 -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 S932620AbeASSSf (ORCPT + 99 others); Fri, 19 Jan 2018 13:18:35 -0500 Received: from foss.arm.com ([217.140.101.70]:41790 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755949AbeASSS1 (ORCPT ); Fri, 19 Jan 2018 13:18:27 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 1249E1529; Fri, 19 Jan 2018 10:18:27 -0800 (PST) Received: from edgewater-inn.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id D67AC3F487; Fri, 19 Jan 2018 10:18:26 -0800 (PST) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id 84FDE1AE1341; Fri, 19 Jan 2018 18:18:33 +0000 (GMT) Date: Fri, 19 Jan 2018 18:18:33 +0000 From: Will Deacon To: Dan Williams Cc: Adam Sampson , Jann Horn , kernel list , linux-arch , Kernel Hardening , Catalin Marinas , the arch/x86 maintainers , Russell King , Ingo Molnar , Greg Kroah-Hartman , "H. Peter Anvin" , Thomas Gleixner , Linus Torvalds , Andrew Morton , Alan Cox , Alexei Starovoitov Subject: Re: [kernel-hardening] [PATCH v4 02/10] asm/nospec, array_ptr: sanitize speculative array de-references Message-ID: <20180119181833.GA1878@arm.com> References: <151632009605.21271.11304291057104672116.stgit@dwillia2-desk3.amr.corp.intel.com> <151632010687.21271.12004432287640499992.stgit@dwillia2-desk3.amr.corp.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jan 19, 2018 at 10:12:47AM -0800, Dan Williams wrote: > [ adding Alexei back to the cc ] > > On Fri, Jan 19, 2018 at 9:48 AM, Adam Sampson wrote: > > Jann Horn writes: > > > >>> +/* > >>> + * If idx is negative or if idx > size then bit 63 is set in the mask, > >>> + * and the value of ~(-1L) is zero. When the mask is zero, bounds check > >>> + * failed, array_ptr will return NULL. > >>> + */ > >>> +#ifndef array_ptr_mask > >>> +static inline unsigned long array_ptr_mask(unsigned long idx, > >>> unsigned long sz) > >>> +{ > >>> + return ~(long)(idx | (sz - 1 - idx)) >> (BITS_PER_LONG - 1); > >>> +} > >>> +#endif > >> > >> Nit: Maybe add a comment saying that this is equivalent to > >> "return ((long)idx >= 0 && idx < sz) ? ULONG_MAX : 0"? > > > > That's only true when sz < LONG_MAX, which is documented below but not > > here; it's also different from the asm version, which doesn't do the idx > > <= LONG_MAX check. So making the constraint explicit would be a good idea. > > > > From a bit of experimentation, when the top bit of sz is set, this > > expression, the C version and the assembler version all have different > > behaviour. For example, with 32-bit unsigned long: > > > > index=00000000 size=80000001: expr=ffffffff c=00000000 asm=ffffffff > > index=80000000 size=80000001: expr=00000000 c=00000000 asm=ffffffff > > index=00000000 size=a0000000: expr=ffffffff c=00000000 asm=ffffffff > > index=00000001 size=a0000000: expr=ffffffff c=00000000 asm=ffffffff > > index=fffffffe size=ffffffff: expr=00000000 c=00000000 asm=ffffffff > > > > It may be worth noting that: > > > > return 0 - ((long) (idx < sz)); > > > > causes GCC, on ia32 and amd64, to generate exactly the same cmp/sbb > > sequence as in Linus's asm. Are there architectures where this form > > would allow speculation? > > We're operating on the assumption that compilers will not try to > introduce branches where they don't exist in the code, so if this is > producing identical assembly I think we should go with it and drop the > x86 array_ptr_mask. Branches, perhaps, but this could easily be compiled to a conditional select (CSEL) instruction on arm64 and that wouldn't be safe without a CSDB. Of course, we can do our own thing in assembly to prevent that, but it would mean that the generic C implementation would not be robust for us. Will