Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20190423185128.6SVhFqa1-%akpm@linux-foundation.org>
Date: Tue, 23 Apr 2019 11:51:28 -0700
From: akpm@...ux-foundation.org
To: a.p.zijlstra@...llo.nl, hecmargi@....es, hpa@...or.com,
 jgg@...lanox.com, keescook@...omium.org,
 kernel-hardening@...ts.openwall.com, luto@...capital.net,
 mark.rutland@....com, mingo@...e.hu, mm-commits@...r.kernel.org,
 tglx@...utronix.de
Subject:  +
 binfmt_elf-update-read_implies_exec-logic-for-modern-cpus.patch added to
 -mm tree


The patch titled
     Subject: binfmt_elf: Update READ_IMPLIES_EXEC logic for modern CPUs
has been added to the -mm tree.  Its filename is
     binfmt_elf-update-read_implies_exec-logic-for-modern-cpus.patch

This patch should soon appear at
    http://ozlabs.org/~akpm/mmots/broken-out/binfmt_elf-update-read_implies_exec-logic-for-modern-cpus.patch
and later at
    http://ozlabs.org/~akpm/mmotm/broken-out/binfmt_elf-update-read_implies_exec-logic-for-modern-cpus.patch

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next and is updated
there every 3-4 working days

------------------------------------------------------
From: Kees Cook <keescook@...omium.org>
Subject: binfmt_elf: Update READ_IMPLIES_EXEC logic for modern CPUs

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 than 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 all toolchains include PT_GNU_STACK), 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

Link: http://lkml.kernel.org/r/20190423181210.GA2443@beast
Signed-off-by: Kees Cook <keescook@...omium.org>
Suggested-by: Hector Marco-Gisbert <hecmargi@....es>
Cc: Jason Gunthorpe <jgg@...lanox.com>
Cc: Thomas Gleixner <tglx@...utronix.de>
Cc: Andy Lutomirski <luto@...capital.net>
Cc: Kernel Hardening <kernel-hardening@...ts.openwall.com>
Cc: Mark Rutland <mark.rutland@....com>
Cc: Ingo Molnar <mingo@...e.hu>
Cc: "H. Peter Anvin" <hpa@...or.com>
Cc: Peter Zijlstra <a.p.zijlstra@...llo.nl>
Signed-off-by: Andrew Morton <akpm@...ux-foundation.org>
---

 arch/arm64/include/asm/elf.h |    9 ++++++++-
 arch/x86/include/asm/elf.h   |   24 +++++++++++++++++++++---
 2 files changed, 29 insertions(+), 4 deletions(-)

--- a/arch/arm64/include/asm/elf.h~binfmt_elf-update-read_implies_exec-logic-for-modern-cpus
+++ a/arch/arm64/include/asm/elf.h
@@ -107,7 +107,14 @@
  */
 #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 elf_read_implies_exec(ex, stk)			\
+	(is_compat_task() && stk == EXSTACK_DEFAULT)
 
 #define CORE_DUMP_USE_REGSET
 #define ELF_EXEC_PAGESIZE	PAGE_SIZE
--- a/arch/x86/include/asm/elf.h~binfmt_elf-update-read_implies_exec-logic-for-modern-cpus
+++ a/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) ? 1 :		\
+		(mmap_is_ia32() && stk == EXSTACK_DEFAULT))
 
 struct task_struct;
 
_

Patches currently in -mm which might be from keescook@...omium.org are

binfmt_elf-move-brk-out-of-mmap-when-doing-direct-loader-exec.patch
binfmt_elf-update-read_implies_exec-logic-for-modern-cpus.patch

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.