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 X-Spam-Level: X-Spam-Status: No, score=-8.5 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_PASS,USER_AGENT_MUTT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B871C282CE for ; Wed, 24 Apr 2019 20:51:41 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id CB0792183E for ; Wed, 24 Apr 2019 20:51:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="dRolOcXw" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CB0792183E Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject: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=KFu8U5192I6DSZILKykMg9ItrrbDUBDxMcYGUqhquio=; b=dRolOcXw70mTj5 vD3FtYiz3Q/djN/VMl3To/fwCXhXPQu12MxxHNvclobOquneFQfIr4b1TxfKV3Ymi/EgBaYubMRCs nogalwYy/ZEbFNObwI94nUrx6e5OzXF4wfmz16beMfpXhcboAXBogAkx/gelNvwX0Yjgk4XeoWFLy YfCu1JgPERlAFuxVycB5lPgbrtPYp8V61XD61dCCz7ph7NN3Pjpbrf9RKZJ/Fl/BhWhgAUhg26Aed Oh/jKfiMZkx1XxDv7rnZkB1NvuptkTWxcCYwKxoozoYWKDVSjAs66XXdL1S6ZGEc7XrnuhfgYZAK5 unLZ8A1bSrDchFa4Jt6A==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hJOrd-00060B-5E; Wed, 24 Apr 2019 20:51:33 +0000 Received: from foss.arm.com ([217.140.101.70]) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hJOrZ-0005zS-JE for linux-arm-kernel@lists.infradead.org; Wed, 24 Apr 2019 20:51:31 +0000 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 DB241A78; Wed, 24 Apr 2019 13:51:27 -0700 (PDT) Received: from brain-police (usa-sjc-mx-foss1.foss.arm.com [217.140.101.70]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 585253F557; Wed, 24 Apr 2019 13:51:24 -0700 (PDT) Date: Wed, 24 Apr 2019 21:51:17 +0100 From: Will Deacon To: Kees Cook Subject: Re: [PATCH v2] binfmt_elf: Update READ_IMPLIES_EXEC logic for modern CPUs Message-ID: <20190424205117.GA5291@brain-police> References: <20190424203408.GA11386@beast> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20190424203408.GA11386@beast> User-Agent: Mutt/1.9.4 (2018-02-28) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190424_135129_642405_C230DE00 X-CRM114-Status: GOOD ( 20.86 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Stephen Rothwell , Arnd Bergmann , Marc Gonzalez , Catalin Marinas , x86@kernel.org, Hector Marco-Gisbert , linux-kernel@vger.kernel.org, Andy Lutomirski , Jason Gunthorpe , Kernel Hardening , Andrew Morton , Thomas Gleixner , Linux ARM Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, Apr 24, 2019 at 01:34:08PM -0700, Kees Cook wrote: > The READ_IMPLIES_EXEC work-around was designed for old CPUs lacking NX > (to have the visible permission flags on memory regions reflect reality: > they are all executable), and for old toolchains that lacked the ELF > PT_GNU_STACK marking (under the assumption that toolchains that couldn't > even specify memory protection flags may have it wrong for all memory > regions). > > This logic is sensible, but was implemented in a way that equated having > a PT_GNU_STACK marked executable as being as "broken" as lacking the > PT_GNU_STACK marking entirely. This is not a reasonable assumption > for CPUs that have had NX support from the start (or very close to > the start). This confusion has led to situations where modern 64-bit > programs with explicitly marked executable stack are forced into the > READ_IMPLIES_EXEC state when no such thing is needed. (And leads to > unexpected failures when mmap()ing regions of device driver memory that > wish to disallow VM_EXEC[1].) > > To fix this, elf_read_implies_exec() is adjusted on arm64 (where NX has > always existed and toolchains have implemented PT_GNU_STACK for a while), > and x86 is adjusted to handle this combination of possible outcomes: > > CPU: | lacks NX | has NX, ia32 | has NX, x86_64 | > ELF: | | | | > ------------------------------|------------------|------------------| > missing GNU_STACK | needs RIE | needs RIE | no RIE | > GNU_STACK == RWX | needs RIE | no RIE: stack X | no RIE: stack X | > GNU_STACK == RW | needs RIE | no RIE: stack NX | no RIE: stack NX | > > This has the effect of making binfmt_elf's EXSTACK_DEFAULT actually take > on the correct architecture default of being non-executable on arm64 and > x86_64, and being executable on ia32. > > [1] https://lkml.kernel.org/r/20190418055759.GA3155@mellanox.com > > Suggested-by: Hector Marco-Gisbert > Signed-off-by: Kees Cook > --- > v2: adjust arm64 to avoid is_compat_task() (marc.w.gonzalez) > --- > arch/arm64/include/asm/elf.h | 8 +++++++- > arch/x86/include/asm/elf.h | 24 +++++++++++++++++++++--- > 2 files changed, 28 insertions(+), 4 deletions(-) > > diff --git a/arch/arm64/include/asm/elf.h b/arch/arm64/include/asm/elf.h > index 6adc1a90e7e6..f1bb4b388b8f 100644 > --- a/arch/arm64/include/asm/elf.h > +++ b/arch/arm64/include/asm/elf.h > @@ -107,7 +107,13 @@ > */ > #define elf_check_arch(x) ((x)->e_machine == EM_AARCH64) > > -#define elf_read_implies_exec(ex,stk) (stk != EXSTACK_DISABLE_X) > +/* > + * 64-bit processes should not automatically gain READ_IMPLIES_EXEC. Only > + * 32-bit processes without PT_GNU_STACK should trigger READ_IMPLIES_EXEC > + * out of an abundance of caution against ancient toolchains not knowing > + * how to mark memory protection flags correctly. > + */ > +#define compat_elf_read_implies_exec(ex, stk) (stk == EXSTACK_DEFAULT) Don't you need to hack fs/compat_binfmt_elf.c to pick this up, or am I missing some trick? Should just be something like below. Will --->8 diff --git a/fs/compat_binfmt_elf.c b/fs/compat_binfmt_elf.c index 15f6e96b3bd9..694bc3ee77eb 100644 --- a/fs/compat_binfmt_elf.c +++ b/fs/compat_binfmt_elf.c @@ -116,6 +116,11 @@ #define arch_setup_additional_pages compat_arch_setup_additional_pages #endif +#ifdef compat_elf_read_implies_exec +#undef elf_read_implies_exec +#define elf_read_implies_exec compat_elf_read_implies_exec +#endif + /* * Rename a few of the symbols that binfmt_elf.c will define. * These are all local so the names don't really matter, but it _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel