Skip to content

Commit d1fd836

Browse files
keestorvalds
authored andcommitted
mm: split ET_DYN ASLR from mmap ASLR
This fixes the "offset2lib" weakness in ASLR for arm, arm64, mips, powerpc, and x86. The problem is that if there is a leak of ASLR from the executable (ET_DYN), it means a leak of shared library offset as well (mmap), and vice versa. Further details and a PoC of this attack is available here: http://cybersecurity.upv.es/attacks/offset2lib/offset2lib.html With this patch, a PIE linked executable (ET_DYN) has its own ASLR region: $ ./show_mmaps_pie 54859ccd6000-54859ccd7000 r-xp ... /tmp/show_mmaps_pie 54859ced6000-54859ced7000 r--p ... /tmp/show_mmaps_pie 54859ced7000-54859ced8000 rw-p ... /tmp/show_mmaps_pie 7f75be764000-7f75be91f000 r-xp ... /lib/x86_64-linux-gnu/libc.so.6 7f75be91f000-7f75beb1f000 ---p ... /lib/x86_64-linux-gnu/libc.so.6 7f75beb1f000-7f75beb23000 r--p ... /lib/x86_64-linux-gnu/libc.so.6 7f75beb23000-7f75beb25000 rw-p ... /lib/x86_64-linux-gnu/libc.so.6 7f75beb25000-7f75beb2a000 rw-p ... 7f75beb2a000-7f75beb4d000 r-xp ... /lib64/ld-linux-x86-64.so.2 7f75bed45000-7f75bed46000 rw-p ... 7f75bed46000-7f75bed47000 r-xp ... 7f75bed47000-7f75bed4c000 rw-p ... 7f75bed4c000-7f75bed4d000 r--p ... /lib64/ld-linux-x86-64.so.2 7f75bed4d000-7f75bed4e000 rw-p ... /lib64/ld-linux-x86-64.so.2 7f75bed4e000-7f75bed4f000 rw-p ... 7fffb3741000-7fffb3762000 rw-p ... [stack] 7fffb377b000-7fffb377d000 r--p ... [vvar] 7fffb377d000-7fffb377f000 r-xp ... [vdso] The change is to add a call the newly created arch_mmap_rnd() into the ELF loader for handling ET_DYN ASLR in a separate region from mmap ASLR, as was already done on s390. Removes CONFIG_BINFMT_ELF_RANDOMIZE_PIE, which is no longer needed. Signed-off-by: Kees Cook <[email protected]> Reported-by: Hector Marco-Gisbert <[email protected]> Cc: Russell King <[email protected]> Reviewed-by: Ingo Molnar <[email protected]> Cc: Catalin Marinas <[email protected]> Cc: Will Deacon <[email protected]> Cc: Ralf Baechle <[email protected]> Cc: Benjamin Herrenschmidt <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Michael Ellerman <[email protected]> Cc: Martin Schwidefsky <[email protected]> Cc: Heiko Carstens <[email protected]> Cc: Alexander Viro <[email protected]> Cc: Oleg Nesterov <[email protected]> Cc: Andy Lutomirski <[email protected]> Cc: "David A. Long" <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Arun Chandran <[email protected]> Cc: Yann Droneaud <[email protected]> Cc: Min-Hua Chen <[email protected]> Cc: Paul Burton <[email protected]> Cc: Alex Smith <[email protected]> Cc: Markos Chandras <[email protected]> Cc: Vineeth Vijayan <[email protected]> Cc: Jeff Bailey <[email protected]> Cc: Michael Holzheu <[email protected]> Cc: Ben Hutchings <[email protected]> Cc: Behan Webster <[email protected]> Cc: Ismael Ripoll <[email protected]> Cc: Jan-Simon Mller <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent c6f5b00 commit d1fd836

File tree

9 files changed

+6
-33
lines changed

9 files changed

+6
-33
lines changed

arch/arm/Kconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
config ARM
22
bool
33
default y
4-
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
54
select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
65
select ARCH_HAS_ELF_RANDOMIZE
76
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST

arch/arm64/Kconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
config ARM64
22
def_bool y
3-
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
43
select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
54
select ARCH_HAS_ELF_RANDOMIZE
65
select ARCH_HAS_GCOV_PROFILE_ALL

arch/mips/Kconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ config MIPS
2323
select HAVE_KRETPROBES
2424
select HAVE_DEBUG_KMEMLEAK
2525
select HAVE_SYSCALL_TRACEPOINTS
26-
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
2726
select ARCH_HAS_ELF_RANDOMIZE
2827
select HAVE_ARCH_TRANSPARENT_HUGEPAGE if CPU_SUPPORTS_HUGEPAGES && 64BIT
2928
select RTC_LIB if !MACH_LOONGSON

arch/powerpc/Kconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ config PPC
8888
select ARCH_MIGHT_HAVE_PC_PARPORT
8989
select ARCH_MIGHT_HAVE_PC_SERIO
9090
select BINFMT_ELF
91-
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
9291
select ARCH_HAS_ELF_RANDOMIZE
9392
select OF
9493
select OF_EARLY_FLATTREE

arch/s390/include/asm/elf.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,9 @@ extern unsigned int vdso_enabled;
163163
the loader. We need to make sure that it is out of the way of the program
164164
that it will "exec", and that there is sufficient room for the brk. 64-bit
165165
tasks are aligned to 4GB. */
166-
extern unsigned long randomize_et_dyn(void);
167-
#define ELF_ET_DYN_BASE (randomize_et_dyn() + (is_32bit_task() ? \
166+
#define ELF_ET_DYN_BASE (is_32bit_task() ? \
168167
(STACK_TOP / 3 * 2) : \
169-
(STACK_TOP / 3 * 2) & ~((1UL << 32) - 1)))
168+
(STACK_TOP / 3 * 2) & ~((1UL << 32) - 1))
170169

171170
/* This yields a mask that user programs can use to figure out what
172171
instruction set this CPU supports. */

arch/s390/mm/mmap.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,6 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
177177
return addr;
178178
}
179179

180-
unsigned long randomize_et_dyn(void)
181-
{
182-
if (current->flags & PF_RANDOMIZE)
183-
return arch_mmap_rnd();
184-
185-
return 0UL;
186-
}
187-
188180
#ifndef CONFIG_64BIT
189181

190182
/*

arch/x86/Kconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ config X86
8787
select HAVE_ARCH_KMEMCHECK
8888
select HAVE_ARCH_KASAN if X86_64 && SPARSEMEM_VMEMMAP
8989
select HAVE_USER_RETURN_NOTIFIER
90-
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
9190
select ARCH_HAS_ELF_RANDOMIZE
9291
select HAVE_ARCH_JUMP_LABEL
9392
select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE

fs/Kconfig.binfmt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ config COMPAT_BINFMT_ELF
2727
bool
2828
depends on COMPAT && BINFMT_ELF
2929

30-
config ARCH_BINFMT_ELF_RANDOMIZE_PIE
31-
bool
32-
3330
config ARCH_BINFMT_ELF_STATE
3431
bool
3532

fs/binfmt_elf.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/security.h>
3232
#include <linux/random.h>
3333
#include <linux/elf.h>
34+
#include <linux/elf-randomize.h>
3435
#include <linux/utsname.h>
3536
#include <linux/coredump.h>
3637
#include <linux/sched.h>
@@ -910,21 +911,10 @@ static int load_elf_binary(struct linux_binprm *bprm)
910911
* default mmap base, as well as whatever program they
911912
* might try to exec. This is because the brk will
912913
* follow the loader, and is not movable. */
913-
#ifdef CONFIG_ARCH_BINFMT_ELF_RANDOMIZE_PIE
914-
/* Memory randomization might have been switched off
915-
* in runtime via sysctl or explicit setting of
916-
* personality flags.
917-
* If that is the case, retain the original non-zero
918-
* load_bias value in order to establish proper
919-
* non-randomized mappings.
920-
*/
914+
load_bias = ELF_ET_DYN_BASE - vaddr;
921915
if (current->flags & PF_RANDOMIZE)
922-
load_bias = 0;
923-
else
924-
load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
925-
#else
926-
load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
927-
#endif
916+
load_bias += arch_mmap_rnd();
917+
load_bias = ELF_PAGESTART(load_bias);
928918
total_size = total_mapping_size(elf_phdata,
929919
loc->elf_ex.e_phnum);
930920
if (!total_size) {

0 commit comments

Comments
 (0)