From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 95979C54E5D for ; Tue, 12 Mar 2024 19:53:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=EUh9rS6OzmItakiZ/38iq9U5ZF9zSeqmDItbyFGp3Mw=; b=q6wuaWrurukH2y ZH5o65XqU8snyzaZIEieSzcEtFxsoW7pRCzuUGWUpkTz+8yjHm/kYS4QZu9AvUKB1hFEhWM/H9cCt Rdm6n49uaVoHutncFJcsdFy2smGni0QWQ6gzeRxFLLzKREQsXD7XmLAqBWTQwxZj25w30a2rxvg+L sVvZMtIrLelxVE7eu9jPkkwegg6B/N0Du26OLLS9wbFeLXVOM6Ixiko7eKGsjFXZaNjIe2k6gJG05 B8LrsZqLG9LzpXUHIcID9ThQr4WGPMQ/BuC6MUCyEUMAFF7fIITHTkKof7gajE9kYiI7+nljQrLAT r4JOJXblQL3tT+V9C4Kg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rk8BV-00000007Md5-0k3X; Tue, 12 Mar 2024 19:53:13 +0000 Received: from sin.source.kernel.org ([145.40.73.55]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rk8BR-00000007McX-1e8z for linux-arm-kernel@lists.infradead.org; Tue, 12 Mar 2024 19:53:10 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id B52CACE1903; Tue, 12 Mar 2024 19:53:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B20FFC433C7; Tue, 12 Mar 2024 19:53:03 +0000 (UTC) Date: Tue, 12 Mar 2024 19:53:01 +0000 From: Catalin Marinas To: Ard Biesheuvel Cc: linux-arm-kernel@lists.infradead.org, Ard Biesheuvel , Will Deacon , Marc Zyngier , Mark Rutland , Ryan Roberts , Anshuman Khandual , Kees Cook Subject: Re: [PATCH v8 42/43] mm: add arch hook to validate mmap() prot flags Message-ID: References: <20240214122845.2033971-45-ardb+git@google.com> <20240214122845.2033971-87-ardb+git@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20240214122845.2033971-87-ardb+git@google.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240312_125309_636362_0BA522E7 X-CRM114-Status: GOOD ( 28.47 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, Feb 14, 2024 at 01:29:28PM +0100, Ard Biesheuvel wrote: > From: Ard Biesheuvel > > Add a hook to permit architectures to perform validation on the prot > flags passed to mmap(), like arch_validate_prot() does for mprotect(). > This will be used by arm64 to reject PROT_WRITE+PROT_EXEC mappings on > configurations that run with WXN enabled. > > Reviewed-by: Kees Cook > Signed-off-by: Ard Biesheuvel > --- > include/linux/mman.h | 15 +++++++++++++++ > mm/mmap.c | 3 +++ > 2 files changed, 18 insertions(+) > > diff --git a/include/linux/mman.h b/include/linux/mman.h > index dc7048824be8..ec5e7f606e43 100644 > --- a/include/linux/mman.h > +++ b/include/linux/mman.h > @@ -124,6 +124,21 @@ static inline bool arch_validate_flags(unsigned long flags) > #define arch_validate_flags arch_validate_flags > #endif > > +#ifndef arch_validate_mmap_prot > +/* > + * This is called from mmap(), which ignores unknown prot bits so the default > + * is to accept anything. > + * > + * Returns true if the prot flags are valid > + */ > +static inline bool arch_validate_mmap_prot(unsigned long prot, > + unsigned long addr) > +{ > + return true; > +} > +#define arch_validate_mmap_prot arch_validate_mmap_prot > +#endif > + > /* > * Optimisation macro. It is equivalent to: > * (x & bit1) ? bit2 : 0 > diff --git a/mm/mmap.c b/mm/mmap.c > index d89770eaab6b..977a8c3fd9f5 100644 > --- a/mm/mmap.c > +++ b/mm/mmap.c > @@ -1229,6 +1229,9 @@ unsigned long do_mmap(struct file *file, unsigned long addr, > if (!(file && path_noexec(&file->f_path))) > prot |= PROT_EXEC; > > + if (!arch_validate_mmap_prot(prot, addr)) > + return -EACCES; > + > /* force arch specific MAP_FIXED handling in get_unmapped_area */ > if (flags & MAP_FIXED_NOREPLACE) > flags |= MAP_FIXED; While writing the pull request for Linus (and looking to justify this change), I realised that we already have arch_validate_flags() that can do a similar check but on VM_* flags instead of PROT_* (we use it for VM_MTE checks). What was the reason for adding a new hook? The only difference is that here it returns -EACCESS while on arch_validate_flags() failure it would return -EINVAL. It probably makes more to return -EACCESS as it matches map_deny_write_exec() but with your patches we are inconsistent between mmap() and mprotect() errors (the latter is still -EINVAL). It also got me thinking on whether we could use this as a hardened version of the MDWE feature instead a CPU feature (though we'd end up context-switching this SCTLR_EL1 bit). I think your patches have been around before the MDWE feature was added to the kernel. Sorry for not catching this early. -- Catalin _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel