From patchwork Wed Apr 24 20:34:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10915723 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B355C1390 for ; Wed, 24 Apr 2019 20:34:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9F37E28AFF for ; Wed, 24 Apr 2019 20:34:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9268928AEB; Wed, 24 Apr 2019 20:34:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 12E8528AEB for ; Wed, 24 Apr 2019 20:34:19 +0000 (UTC) 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:MIME-Version: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:In-Reply-To:References: List-Owner; bh=cN2kXngr93RGRu2GS0ROiWjr/S0q0xi2Xe+HbqSkl5k=; b=kzTHSyMVDu6wct z6nSBJSiNlyNEQCZ1o6UXYN/3wcAX0uQE91zWMq7W8uI90/9ftskTM3+PGEnqPWoPubOD8tRq4Ge1 sCPY8ipqHt+KFoSvBe9iN0fIyDnVDeuiShxphjPCmaYKLAHr5bmhf41f60C43jkzEDyJCR1IIn+yb cLEFC0y+iDrDBdTbvNBkZsu0mfKvA02Ui02JJ8DVsIF4lFITZf+vYcSM52BafXXaWuE6iwm+XoCEa OTxENQgRvnOWvwwGAeQTd6sbjzV+Q+O9u/bwVRJfv7/vtwDYRJp6GtYXwDhOgCSQPzpuq6X8r9OfE IVaCmWvmA0HDk6jUnxQA==; 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 1hJOat-00088W-JP; Wed, 24 Apr 2019 20:34:15 +0000 Received: from mail-pl1-x642.google.com ([2607:f8b0:4864:20::642]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hJOap-00087p-LB for linux-arm-kernel@lists.infradead.org; Wed, 24 Apr 2019 20:34:14 +0000 Received: by mail-pl1-x642.google.com with SMTP id n8so9847670plp.10 for ; Wed, 24 Apr 2019 13:34:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:mime-version:content-disposition; bh=suc5YIphguybOVTE6QyZmRLNdlVBsBpozTlH+RsbifA=; b=R5KQiyKpY4aUSx9teSJh93jt0UeO1AONuuDa9IZDvbiNZCO8R3kVQ5iUT0dLm2O9e5 lL/MuPn5JfrXxme4DLLRG+j4lP6WAFbmE1Lhz+x3wo9Eh0lAKDGILxcJPTX9+EVcX+/3 S8zS8fDIjvJiMdAU3kqSh8NMHLUxMyxSKXpbw= 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:mime-version :content-disposition; bh=suc5YIphguybOVTE6QyZmRLNdlVBsBpozTlH+RsbifA=; b=olxtRQmCm5c8VY+6h4ANXa55yPkY8zn/yH/465LZ2bFPCxTYYh2/91awJtP4K/IUUV MYFixndfvZTSwnQVLTxNSXezzXYbQQwtDgjnfKYdP5UQzWHhWeOr2KQ5/8nqAfJ2/ThA Uq8W2BzFNOWm1X7CYcmBs+CRTS3LhUB0V40u3RcKRHxenrWaDERUuwlpoeSVIhxqiu7o 3qTVyp57ycS6R90duKPTo+AKP1PJB+uyC3RYYGbWzceXdbTYm2fQCyLTa3PbXSXeOj++ M7B7ua2kXF2FypeOcDyVPzTzLd853Sh22hSQya99ZAW1BsdZ+wfcbXS2dsHovi1VagJE c4Ug== X-Gm-Message-State: APjAAAX5BUPhqqIFAFbN3zEn3eR1M2JPVGdtS5jeF9U9j1gAYQvLpUh5 /LIt/Cw/yDQDFj6VDOoOtgynig== X-Google-Smtp-Source: APXvYqxUBKxS6wikJnQuKLXX6a6Z/8pD+GkMvzVv6y2IAqFW8W3uvRYvN4Yye5p83JuELO8tsE35sQ== X-Received: by 2002:a17:902:2aa8:: with SMTP id j37mr35147430plb.164.1556138050966; Wed, 24 Apr 2019 13:34:10 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id d3sm26389989pfn.113.2019.04.24.13.34.09 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 24 Apr 2019 13:34:09 -0700 (PDT) Date: Wed, 24 Apr 2019 13:34:08 -0700 From: Kees Cook To: Andrew Morton Subject: [PATCH v2] binfmt_elf: Update READ_IMPLIES_EXEC logic for modern CPUs Message-ID: <20190424203408.GA11386@beast> MIME-Version: 1.0 Content-Disposition: inline X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190424_133411_723817_7A43695B X-CRM114-Status: GOOD ( 15.88 ) 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 , Hector Marco-Gisbert , x86@kernel.org, Will Deacon , linux-kernel@vger.kernel.org, Andy Lutomirski , Jason Gunthorpe , Catalin Marinas , Kernel Hardening , Thomas Gleixner , Linux ARM Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP 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) #define CORE_DUMP_USE_REGSET #define ELF_EXEC_PAGESIZE PAGE_SIZE diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h index 69c0f892e310..5e65f1dcefc9 100644 --- a/arch/x86/include/asm/elf.h +++ b/arch/x86/include/asm/elf.h @@ -280,10 +280,28 @@ extern u32 elf_hwcap2; /* * An executable for which elf_read_implies_exec() returns TRUE will - * have the READ_IMPLIES_EXEC personality flag set automatically. + * have the READ_IMPLIES_EXEC personality flag set automatically. This + * is needed either to show the truth about a memory mapping (i.e. CPUs + * that lack NX have all memory implicitly executable, so this makes + * sure that the visible permissions reflect reality), or to deal with + * old toolchains on new CPUs. Old binaries entirely lacking a GNU_STACK + * indicate they were likely built with a toolchain that has no idea about + * memory permissions, and so we must default to the lowest reasonable + * common denominator for the architecture: on ia32 we assume all memory + * to be executable by default, and on x86_64 we assume all memory to be + * non-executable by default. + * + * 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 | + * */ -#define elf_read_implies_exec(ex, executable_stack) \ - (executable_stack != EXSTACK_DISABLE_X) +#define elf_read_implies_exec(ex, stk) \ + (!(__supported_pte_mask & _PAGE_NX) || \ + (mmap_is_ia32() && stk == EXSTACK_DEFAULT)) struct task_struct;