Skip to content

Commit

Permalink
mm: expose arch_mmap_rnd when available
Browse files Browse the repository at this point in the history
When an architecture fully supports randomizing the ELF load location,
a per-arch mmap_rnd() function is used to find a randomized mmap base.
In preparation for randomizing the location of ET_DYN binaries
separately from mmap, this renames and exports these functions as
arch_mmap_rnd(). Additionally introduces CONFIG_ARCH_HAS_ELF_RANDOMIZE
for describing this feature on architectures that support it
(which is a superset of ARCH_BINFMT_ELF_RANDOMIZE_PIE, since s390
already supports a separated ET_DYN ASLR from mmap ASLR without the
ARCH_BINFMT_ELF_RANDOMIZE_PIE logic).

Signed-off-by: Kees Cook <[email protected]>
Cc: 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]>
  • Loading branch information
kees authored and torvalds committed Apr 14, 2015
1 parent 8e89a35 commit 2b68f6c
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 14 deletions.
7 changes: 7 additions & 0 deletions arch/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,13 @@ config PGTABLE_LEVELS
int
default 2

config ARCH_HAS_ELF_RANDOMIZE
bool
help
An architecture supports choosing randomized locations for
stack, mmap, brk, and ET_DYN. Defined functions:
- arch_mmap_rnd()

#
# ABI hall of shame
#
Expand Down
1 change: 1 addition & 0 deletions arch/arm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ config ARM
default y
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
select ARCH_HAS_ELF_RANDOMIZE
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
select ARCH_HAVE_CUSTOM_GPIO_H
select ARCH_HAS_GCOV_PROFILE_ALL
Expand Down
4 changes: 2 additions & 2 deletions arch/arm/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
return addr;
}

static unsigned long mmap_rnd(void)
unsigned long arch_mmap_rnd(void)
{
unsigned long rnd;

Expand All @@ -184,7 +184,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm)
unsigned long random_factor = 0UL;

if (current->flags & PF_RANDOMIZE)
random_factor = mmap_rnd();
random_factor = arch_mmap_rnd();

if (mmap_is_legacy()) {
mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
Expand Down
1 change: 1 addition & 0 deletions arch/arm64/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ config ARM64
def_bool y
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
select ARCH_HAS_ELF_RANDOMIZE
select ARCH_HAS_GCOV_PROFILE_ALL
select ARCH_HAS_SG_CHAIN
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
Expand Down
4 changes: 2 additions & 2 deletions arch/arm64/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static int mmap_is_legacy(void)
return sysctl_legacy_va_layout;
}

static unsigned long mmap_rnd(void)
unsigned long arch_mmap_rnd(void)
{
unsigned long rnd;

Expand Down Expand Up @@ -77,7 +77,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm)
unsigned long random_factor = 0UL;

if (current->flags & PF_RANDOMIZE)
random_factor = mmap_rnd();
random_factor = arch_mmap_rnd();

/*
* Fall back to the standard layout if the personality bit is set, or
Expand Down
1 change: 1 addition & 0 deletions arch/mips/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ config MIPS
select HAVE_DEBUG_KMEMLEAK
select HAVE_SYSCALL_TRACEPOINTS
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
select ARCH_HAS_ELF_RANDOMIZE
select HAVE_ARCH_TRANSPARENT_HUGEPAGE if CPU_SUPPORTS_HUGEPAGES && 64BIT
select RTC_LIB if !MACH_LOONGSON
select GENERIC_ATOMIC64 if !64BIT
Expand Down
4 changes: 2 additions & 2 deletions arch/mips/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ unsigned long arch_get_unmapped_area_topdown(struct file *filp,
addr0, len, pgoff, flags, DOWN);
}

static unsigned long mmap_rnd(void)
unsigned long arch_mmap_rnd(void)
{
unsigned long rnd;

Expand All @@ -161,7 +161,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm)
unsigned long random_factor = 0UL;

if (current->flags & PF_RANDOMIZE)
random_factor = mmap_rnd();
random_factor = arch_mmap_rnd();

if (mmap_is_legacy()) {
mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
Expand Down
1 change: 1 addition & 0 deletions arch/powerpc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ config PPC
select ARCH_MIGHT_HAVE_PC_SERIO
select BINFMT_ELF
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
select ARCH_HAS_ELF_RANDOMIZE
select OF
select OF_EARLY_FLATTREE
select OF_RESERVED_MEM
Expand Down
4 changes: 2 additions & 2 deletions arch/powerpc/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static inline int mmap_is_legacy(void)
return sysctl_legacy_va_layout;
}

static unsigned long mmap_rnd(void)
unsigned long arch_mmap_rnd(void)
{
unsigned long rnd;

Expand Down Expand Up @@ -87,7 +87,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm)
unsigned long random_factor = 0UL;

if (current->flags & PF_RANDOMIZE)
random_factor = mmap_rnd();
random_factor = arch_mmap_rnd();

/*
* Fall back to the standard layout if the personality
Expand Down
1 change: 1 addition & 0 deletions arch/s390/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ config S390
def_bool y
select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
select ARCH_HAS_DEBUG_STRICT_USER_COPY_CHECKS
select ARCH_HAS_ELF_RANDOMIZE
select ARCH_HAS_GCOV_PROFILE_ALL
select ARCH_HAS_SG_CHAIN
select ARCH_HAVE_NMI_SAFE_CMPXCHG
Expand Down
8 changes: 4 additions & 4 deletions arch/s390/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static inline int mmap_is_legacy(void)
return sysctl_legacy_va_layout;
}

static unsigned long mmap_rnd(void)
unsigned long arch_mmap_rnd(void)
{
if (is_32bit_task())
return (get_random_int() & 0x7ff) << PAGE_SHIFT;
Expand Down Expand Up @@ -187,7 +187,7 @@ unsigned long randomize_et_dyn(void)
base &= ~((1UL << 32) - 1);

if (current->flags & PF_RANDOMIZE)
base += mmap_rnd();
base += arch_mmap_rnd();

return base;
}
Expand All @@ -203,7 +203,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm)
unsigned long random_factor = 0UL;

if (current->flags & PF_RANDOMIZE)
random_factor = mmap_rnd();
random_factor = arch_mmap_rnd();

/*
* Fall back to the standard layout if the personality
Expand Down Expand Up @@ -283,7 +283,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm)
unsigned long random_factor = 0UL;

if (current->flags & PF_RANDOMIZE)
random_factor = mmap_rnd();
random_factor = arch_mmap_rnd();

/*
* Fall back to the standard layout if the personality
Expand Down
1 change: 1 addition & 0 deletions arch/x86/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ config X86
select HAVE_ARCH_KASAN if X86_64 && SPARSEMEM_VMEMMAP
select HAVE_USER_RETURN_NOTIFIER
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
select ARCH_HAS_ELF_RANDOMIZE
select HAVE_ARCH_JUMP_LABEL
select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
select SPARSE_IRQ
Expand Down
4 changes: 2 additions & 2 deletions arch/x86/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static int mmap_is_legacy(void)
return sysctl_legacy_va_layout;
}

static unsigned long mmap_rnd(void)
unsigned long arch_mmap_rnd(void)
{
unsigned long rnd;

Expand Down Expand Up @@ -114,7 +114,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm)
unsigned long random_factor = 0UL;

if (current->flags & PF_RANDOMIZE)
random_factor = mmap_rnd();
random_factor = arch_mmap_rnd();

mm->mmap_legacy_base = mmap_legacy_base(random_factor);

Expand Down
10 changes: 10 additions & 0 deletions include/linux/elf-randomize.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef _ELF_RANDOMIZE_H
#define _ELF_RANDOMIZE_H

#ifndef CONFIG_ARCH_HAS_ELF_RANDOMIZE
static inline unsigned long arch_mmap_rnd(void) { return 0; }
#else
extern unsigned long arch_mmap_rnd(void);
#endif

#endif

0 comments on commit 2b68f6c

Please sign in to comment.