Received: by 10.223.176.46 with SMTP id f43csp1015836wra; Sat, 20 Jan 2018 09:03:29 -0800 (PST) X-Google-Smtp-Source: AH8x224OVP38qlVk4OF6kHP4ZSduS7/VqzzoqDMRExHh1hfJjVSGOgSDj0aidyZOSJEJsiRokMC6 X-Received: by 10.98.69.82 with SMTP id s79mr2950995pfa.214.1516467809258; Sat, 20 Jan 2018 09:03:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516467809; cv=none; d=google.com; s=arc-20160816; b=k6N/wvDpPjoqKkbmwJck2dkM3y6DSbtIT5NpbAJG6v2/Rw/L/i6Wz3rJeXAHUam0Cg fbtuN71IKhzRMrBX9K42wvT+fNUy1MdWBV8hpC7iNYfDSLDKhPNbzsTG48jHeESTJ6Es m9FiLZ9j8A+TSr4qm7fpJjnjrocbt4TatXxEbITB0NTAMyP1OeAATfSXs/54r6rftqvx FbGUYZeYFIQQKsfy6XPZJ7v5gmRg6HNcMG66A7ksJqOc9Jzq4hGjr9Kw6AfykgHdFEQU OZsGF8sjKWEAlkUXBMWGw30dhSWJ5+IIzWYNfvdoNi64jwUj5Datb07gzJHwB98c2rya 87XA== 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:dkim-signature:arc-authentication-results; bh=2CCKU7wwp9/cI/7Ro1NAqjadxuT6Lo66n1qgDs1tcqk=; b=J3jFnrqZiFtN/eqqMiAJIO0PVY4vLcirlb3HJV9mJ0jhu+8F7ZxVbDa/fkqzqAxk/j 3ZBuYj3p1QJxIL2mKZEELucFyAHVgzHu0iCeMey1AirWV0NfJDc7uD8sBwJj0wSe1ftO 9g3tjLSr11nYVlpw7mM2kRFAtHBDTryuxdr/5e2gliCR6ApztO4qWrtXQ5J9/c+ixb0w kEIEb6becr6uCD+fS/WR3cu/2HoF1FvxNcdzVp5hK5WMpYzCYBgI7hQPjZBZJ1Wv7Gx3 rJX2xfANZMf2Zkn/hjRd/wra4UGvSrfFz3ZuBj0icigxygFVDIY1Hw+iGuyxtuCoPwzi Km6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=DPsZ59hB; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y11-v6si1841541plg.702.2018.01.20.09.02.41; Sat, 20 Jan 2018 09:03:29 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=DPsZ59hB; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756383AbeATQ4u (ORCPT + 99 others); Sat, 20 Jan 2018 11:56:50 -0500 Received: from mail-pg0-f65.google.com ([74.125.83.65]:45915 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754913AbeATQ4j (ORCPT ); Sat, 20 Jan 2018 11:56:39 -0500 Received: by mail-pg0-f65.google.com with SMTP id c194so3772626pga.12; Sat, 20 Jan 2018 08:56:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=2CCKU7wwp9/cI/7Ro1NAqjadxuT6Lo66n1qgDs1tcqk=; b=DPsZ59hBkYynKMoFMJ1gUkKNDvqxgbBJur6pzaQ0QNwMqEoy9QuIQE+uk97iwlP3AA sr9mIzB7DWYzH5+Pmy6BHJ8+cYYseXqoiJkZaHsrZf3TZn7lYJZP9vNl3hr0YGW4V5Vh /Fs1CzMB17f6w/z0DyuZfGqytOwygq4zwEyHDcOTtARA2+SG+LFPRieBPrztjaI6b2WI 1BgnFsMvM2SmWlQ2gjSqltImYyndCnP0orqYXnQ8/jfllJWZLPKkcPSgGX1k/702qm0n iaBmgcSXB/2eRawkRFGWnc4empi4wQkw+2+faHGuYW/MjzuYqhUTRqm3BRczIFNM92YF mlvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=2CCKU7wwp9/cI/7Ro1NAqjadxuT6Lo66n1qgDs1tcqk=; b=dFQSxpOiM46O5xApxpK6pZgZ66kGO5C82TnS3HMchV62pnxV2Wa6yhQjh5qDgjuX9W TshDsDVouL4tnQ3FeVYlfWGjZv/fg6oHHiAwnJtwDfSx2pTNIfxceUmOYAYV2ECr7q87 lk9a96AEFbAQHbBPFjM4vCpruCYEb+tljMXJcy+FX9+1bIqSd8qM720TVhZdgxQ0Ak+a SMqB0Ns5D0mK/jfmYWMbXMQmgCG5rXdpMgC5Cf/hK8p5y7+H9tDQdvumA0Z88dS2xoo9 jla+QaGxN8E1jkPN1YnHwaYFARN0u191klCbGpuNmR6iD6Y1P361wcOAQFQ+YMcPejf3 Wqnw== X-Gm-Message-State: AKwxytdAFGjJxIugP/r/VjpHdFP3yHHPeewpSUN2LX6BbJ7/J0ANpVXY UFt9R9LmzixGF7NG6GSqE8U= X-Received: by 2002:a17:902:7201:: with SMTP id ba1-v6mr1143819plb.125.1516467398592; Sat, 20 Jan 2018 08:56:38 -0800 (PST) Received: from ast-mbp ([2620:10d:c090:180::6120]) by smtp.gmail.com with ESMTPSA id f78sm23556337pfk.144.2018.01.20.08.56.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 Jan 2018 08:56:37 -0800 (PST) Date: Sat, 20 Jan 2018 08:56:34 -0800 From: Alexei Starovoitov To: Dan Williams Cc: Linux Kernel Mailing List , Mark Rutland , Kernel Hardening , Peter Zijlstra , Catalin Marinas , Will Deacon , "H. Peter Anvin" , Elena Reshetova , linux-arch , Andi Kleen , Jonathan Corbet , X86 ML , Russell King , Ingo Molnar , Andrew Honig , Alan Cox , Tom Lendacky , Kees Cook , Al Viro , Andy Lutomirski , Thomas Gleixner , Andrew Morton , Jim Mattson , Christian Lamparter , Greg KH , Linux Wireless List , stable@vger.kernel.org, Paolo Bonzini , Johannes Berg , Linus Torvalds , "David S. Miller" Subject: Re: [PATCH v4 00/10] prevent bounds-check bypass via speculative execution Message-ID: <20180120165631.e7c3kipmhb5sckor@ast-mbp> References: <151632009605.21271.11304291057104672116.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: NeoMutt/20170421 (1.8.2) 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:58:44PM -0800, Dan Williams wrote: > On Thu, Jan 18, 2018 at 4:01 PM, Dan Williams wrote: > > Changes since v3 [1] > > * Drop 'ifence_array_ptr' and associated compile-time + run-time > > switching and just use the masking approach all the time. > > > > * Convert 'get_user' to use pointer sanitization via masking rather than > > lfence. '__get_user' and associated paths still rely on > > lfence. (Linus) > > > > "Basically, the rule is trivial: find all 'stac' users, and use > > address masking if those users already integrate the limit > > check, and lfence they don't." > > > > * At syscall entry sanitize the syscall number under speculation > > to remove a user controlled pointer de-reference in kernel > > space. (Linus) > > > > * Fix a raw lfence in the kvm code (added for v4.15-rc8) to use > > 'array_ptr'. > > > > * Propose 'array_idx' as a way to sanitize user input that is > > later used as an array index, but where the validation is > > happening in a different code block than the array reference. > > (Christian). > > > > * Fix grammar in speculation.txt (Kees) > > > > --- > > > > Quoting Mark's original RFC: > > > > "Recently, Google Project Zero discovered several classes of attack > > against speculative execution. One of these, known as variant-1, allows > > explicit bounds checks to be bypassed under speculation, providing an > > arbitrary read gadget. Further details can be found on the GPZ blog [2] > > and the Documentation patch in this series." > > > > A precondition of using this attack on the kernel is to get a user > > controlled pointer de-referenced (under speculation) in privileged code. > > The primary source of user controlled pointers in the kernel is the > > arguments passed to 'get_user' and '__get_user'. An example of other > > user controlled pointers are user-controlled array / pointer offsets. > > > > Better tooling is needed to find more arrays / pointers with user > > controlled indices / offsets that can be converted to use 'array_ptr' or > > 'array_idx'. A few are included in this set, and these are not expected > > to be complete. That said, the 'get_user' protections raise the bar on > > finding a vulnerable gadget in the kernel. > > > > These patches are also available via the 'nospec-v4' git branch here: > > > > git://git.kernel.org/pub/scm/linux/kernel/git/djbw/linux nospec-v4 > > I've pushed out a nospec-v4.1 with the below minor cleanup, a fixup of > the changelog for "kvm, x86: fix spectre-v1 mitigation", and added > Paolo's ack. > > git://git.kernel.org/pub/scm/linux/kernel/git/djbw/linux nospec-v4.1 > > diff --git a/include/linux/nospec.h b/include/linux/nospec.h > index 8af35be1869e..b8a9222e34d1 100644 > --- a/include/linux/nospec.h > +++ b/include/linux/nospec.h > @@ -37,7 +37,7 @@ static inline unsigned long array_ptr_mask(unsigned > long idx, unsigned long sz) > unsigned long _i = (idx); \ > unsigned long _mask = array_ptr_mask(_i, (sz)); \ > \ > - __u._ptr = _arr + (_i & _mask); \ > + __u._ptr = _arr + _i; \ > __u._bit &= _mask; \ > __u._ptr; \ hmm. I'm not sure it's the right thing to do, since the macro is forcing cpu to speculate subsequent load from null instead of valid pointer. As Linus said: " So that __u._bit masking wasn't masking the pointer, it was masking the value that was *read* from the pointer, so that you could know that an invalid access returned 0/NULL, not just the first value in the array. " imo just return _arr + (_i & _mask); is enough. No need for union games. The cpu will speculate the load from _arr[0] if _i is out of bounds which is the same as if user passed _i == 0 which would have passed bounds check anyway, so I don't see any data leak from populating cache with _arr[0] data. In-bounds access can do that just as well without any speculation.