Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp4191138ybh; Tue, 17 Mar 2020 14:05:03 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvCdvMiwZRHblg+QxbjN/sn84OeFLpgJr4fX9LwreQ+d0VimKsYInYqN7jvfgkZHButBvV4 X-Received: by 2002:aca:a94a:: with SMTP id s71mr653454oie.152.1584479103572; Tue, 17 Mar 2020 14:05:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584479103; cv=none; d=google.com; s=arc-20160816; b=R0Hno7tE7RIRP5D020E43TNAH4KAKtDP1zHSZ8hPZZLJJ5mds2xi9KkD9FwOkcZjnA f/VM2HzSXaowslyYVneHFtHnT8bozgLuCq3Wn7MOzExXTfL700MV6fdqrXmTGNsMFQv4 782xVavwZYGb+oiRIHgTYxBsJZGlIf6m5X72XCRhmxsarz7kuVwpnql1OLOruW77V+Pl o1QFWkLJRmhGd87YizJc+aoXedN08/uZjpfdseDQp3gyPLkIYmyVRqKxU5VET9oaBWN/ LZJhMlCfX0CeERUJfj7a8hcZwv9VgjAIypZSH9AcahA7cCa/GGedpUFnw/NV/N5CuGoc xrcw== 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; bh=8W3NMZBBusSjrCIsyHpZKleMYtd1j9qRYVd7RlbZ/Pk=; b=W7Cp/BDZDeThm4ascIRMhOHu7a84mcMwx51UietPfGuQ4FtuL7TH2NCaBaTrBQkFXb 6qsfHtQupeHmFYve5BLFhq8GsjCQvi5N403af1eIvlDrN9KQlo0wemjYWIxwC9yxq2aS iFdsziG9D8Kc3FwacO/gpWaEZfD8Foqvc6uBYRs9hiVceP2UhSiolaXv3w85SqHnstnM xjqGWqse0HSBJ3eO5Dv9wWw35e1/GALhcxdocq3t7EFlhGq0AUL1IalSjFwfNK3p+tKh 0UbgPI0vZuM77Cf6EfT69MgQKrZA4waMkovInZ2XVCwHU7+xHJ9OmFzArgG4WS+0OmZi LsKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=AdjUeQBk; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l185si2237742oif.274.2020.03.17.14.04.27; Tue, 17 Mar 2020 14:05:03 -0700 (PDT) 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=@kernel.org header.s=default header.b=AdjUeQBk; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726856AbgCQVCA (ORCPT + 99 others); Tue, 17 Mar 2020 17:02:00 -0400 Received: from mail.kernel.org ([198.145.29.99]:38616 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726730AbgCQVB7 (ORCPT ); Tue, 17 Mar 2020 17:01:59 -0400 Received: from willie-the-truck (236.31.169.217.in-addr.arpa [217.169.31.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 251D42051A; Tue, 17 Mar 2020 21:01:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1584478919; bh=U57LyJghsjeiPtAtTLPXCjGsnVcnXSXPnKEiXhlMHk4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=AdjUeQBkECR3/3jMUEzPlKStC/EKe86EYt7MekRUU8J7qVCc2PecOGBC5Q0RvTnjl 2SYKAlC4uFBF0S2+SkBPdbAlFN2dxNJvdXWD95/IItFhmZkAZRDqtYhkfoydnRP1M+ e2r2otpHwEJOtVu886GnbWU00FAjpDxRH2wRPWrY= Date: Tue, 17 Mar 2020 21:01:54 +0000 From: Will Deacon To: Mark Brown Cc: Mark Rutland , Hongbo Yao , linux-kernel@vger.kernel.org, catalin.marinas@arm.com, linux-arm-kernel@lists.infradead.org Subject: Re: [RFC PATCH] arm64: fix the missing ktpi= cmdline check in arm64_kernel_unmapped_at_el0() Message-ID: <20200317210154.GA19752@willie-the-truck> References: <20200317114708.109283-1-yaohongbo@huawei.com> <20200317121050.GH8831@lakrids.cambridge.arm.com> <20200317124323.GA16200@willie-the-truck> <20200317135719.GH3971@sirena.org.uk> <20200317151813.GA16579@willie-the-truck> <20200317163638.GI3971@sirena.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200317163638.GI3971@sirena.org.uk> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 17, 2020 at 04:36:38PM +0000, Mark Brown wrote: > On Tue, Mar 17, 2020 at 03:18:14PM +0000, Will Deacon wrote: > > On Tue, Mar 17, 2020 at 01:57:19PM +0000, Mark Brown wrote: > > > On Tue, Mar 17, 2020 at 12:43:24PM +0000, Will Deacon wrote: > > > > On Tue, Mar 17, 2020 at 12:10:51PM +0000, Mark Rutland wrote: > > > > > On Tue, Mar 17, 2020 at 07:47:08PM +0800, Hongbo Yao wrote: > > > > > > > - return arm64_use_ng_mappings; > > > > > > + return arm64_use_ng_mappings && > > > > > > + cpus_have_const_cap(ARM64_UNMAP_KERNEL_AT_EL0); > > > > > This probably isn't the right fix, since this will mean that early mappings > > > > will be global and we'll have to go through the painful page-table rewrite > > > > logic when the cap gets enabled for KASLR-enabled kernels. > > > > Aren't we looking for a rewrite from non-global to global here (disable > > > KPTI where we would otherwise have it), which we don't currently have > > > code for? > > > What I mean is that cpus_have_const_cap() will be false initially, so we'll > > put down global mappings early on because PTE_MAYBE_NG will be 0, which > > means that we'll have to invoke the rewriting code if we then realise we > > want non-global mappings after the caps are finalised. > > Ah, I see - a different case to the one originally reported but also an > issue. > > > > That is probably a good idea but I think that runs too late to affect > > > the early mappings, they're done based on kaslr_requires_kpti() well > > > before we start secondaries. My first pass not having paged everything > > > back in yet is that there needs to be command line parsing in > > > kaslr_requires_kpti() but as things stand the command line isn't > > > actually ready then... > > > Yeah, and I think you probably run into chicken and egg problems mapping > > The whole area is just a mess. > > > the thing. With the change above, it's true that /some/ mappings will > > still be nG if you pass kpti=off, but I was hoping that didn't really matter > > :) > > > What was the behaviour prior to your patch? If it used to work without > > any nG mappings, then I suppose we should try to restore that behaviour. > > I'd need to go back and retest to confirm but it looks like always had > the issue that we'd install some nG mappings early even with KPTI > disabled on the command line so your change is just restoring the > previous behaviour and we're no worse than we were before. Urgh, this code brings back really bad memories :( :( :( I just ran 5.4 and it looks like we leave everything non-global with KASLR, even when "kpti=off". Great -- that means we're ok with my patch! Well, we would be except that when we finalise the linear mapping we'll end up trying to transition the old non-global entry to global, which is a break-before-make violation (we panic early in __create_pgd_mapping()). Staring more at the code, I think we're conflating the global/non-global mappings with whether or not the kpti trampoline is active and it looks like this might lead to other issues in mainline right now -- for example, I don't think we clear TPIDRRO_EL0 properly for native tasks because the arm64_kernel_unmapped_at_el0() check in tls_thread_switch() will defer the zeroing to the trampoline code, but that might not even run! So I've hacked the following, which appears to work but damn I'd like somebody else to look at this. I also have a nagging feeling that you implemented it like this at some point, but we tried to consolidate things during review. Thoughts? Will --->8 diff --git a/arch/arm64/include/asm/mmu.h b/arch/arm64/include/asm/mmu.h index e4d862420bb4..d79ce6df9e12 100644 --- a/arch/arm64/include/asm/mmu.h +++ b/arch/arm64/include/asm/mmu.h @@ -29,11 +29,9 @@ typedef struct { */ #define ASID(mm) ((mm)->context.id.counter & 0xffff) -extern bool arm64_use_ng_mappings; - static inline bool arm64_kernel_unmapped_at_el0(void) { - return arm64_use_ng_mappings; + return cpus_have_const_cap(ARM64_UNMAP_KERNEL_AT_EL0); } typedef void (*bp_hardening_cb_t)(void); diff --git a/arch/arm64/include/asm/pgtable-prot.h b/arch/arm64/include/asm/pgtable-prot.h index 6f87839f0249..1305e28225fc 100644 --- a/arch/arm64/include/asm/pgtable-prot.h +++ b/arch/arm64/include/asm/pgtable-prot.h @@ -23,11 +23,13 @@ #include +extern bool arm64_use_ng_mappings; + #define _PROT_DEFAULT (PTE_TYPE_PAGE | PTE_AF | PTE_SHARED) #define _PROT_SECT_DEFAULT (PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S) -#define PTE_MAYBE_NG (arm64_kernel_unmapped_at_el0() ? PTE_NG : 0) -#define PMD_MAYBE_NG (arm64_kernel_unmapped_at_el0() ? PMD_SECT_NG : 0) +#define PTE_MAYBE_NG (arm64_use_ng_mappings ? PTE_NG : 0) +#define PMD_MAYBE_NG (arm64_use_ng_mappings ? PMD_SECT_NG : 0) #define PROT_DEFAULT (_PROT_DEFAULT | PTE_MAYBE_NG) #define PROT_SECT_DEFAULT (_PROT_SECT_DEFAULT | PMD_MAYBE_NG)