mm: expose arch_mmap_rnd when available
authorKees Cook <keescook@chromium.org>
Tue, 14 Apr 2015 22:48:00 +0000 (15:48 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 14 Apr 2015 23:49:05 +0000 (16:49 -0700)
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 <keescook@chromium.org>
Cc: Hector Marco-Gisbert <hecmargi@upv.es>
Cc: Russell King <linux@arm.linux.org.uk>
Reviewed-by: Ingo Molnar <mingo@kernel.org>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: "David A. Long" <dave.long@linaro.org>
Cc: Andrey Ryabinin <a.ryabinin@samsung.com>
Cc: Arun Chandran <achandran@mvista.com>
Cc: Yann Droneaud <ydroneaud@opteya.com>
Cc: Min-Hua Chen <orca.chen@gmail.com>
Cc: Paul Burton <paul.burton@imgtec.com>
Cc: Alex Smith <alex@alex-smith.me.uk>
Cc: Markos Chandras <markos.chandras@imgtec.com>
Cc: Vineeth Vijayan <vvijayan@mvista.com>
Cc: Jeff Bailey <jeffbailey@google.com>
Cc: Michael Holzheu <holzheu@linux.vnet.ibm.com>
Cc: Ben Hutchings <ben@decadent.org.uk>
Cc: Behan Webster <behanw@converseincode.com>
Cc: Ismael Ripoll <iripoll@upv.es>
Cc: Jan-Simon Mller <dl9pf@gmx.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
14 files changed:
arch/Kconfig
arch/arm/Kconfig
arch/arm/mm/mmap.c
arch/arm64/Kconfig
arch/arm64/mm/mmap.c
arch/mips/Kconfig
arch/mips/mm/mmap.c
arch/powerpc/Kconfig
arch/powerpc/mm/mmap.c
arch/s390/Kconfig
arch/s390/mm/mmap.c
arch/x86/Kconfig
arch/x86/mm/mmap.c
include/linux/elf-randomize.h [new file with mode: 0644]

index c88c23f0a1da0a22f7da170b60b7f0f7b8aedf90..474904a8e540e040e318f14a60fea3bf1f387e77 100644 (file)
@@ -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
 #
index 696cf3c61e0f0a6f78c9e80aa2bf821421b23438..f85200a63a8b830069ad546ba7d5bd631cb4aa33 100644 (file)
@@ -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
index 15a8160096b34127be8a7193097dddd17770e72c..407dc786583aec0e6077f50a8374f99002ca8233 100644 (file)
@@ -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;
 
@@ -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;
index 3f2fba996bc236524c6c4dca452b5c22bc67eceb..7c1dbeb73e8d3505c1213461afd6c8e2f0559828 100644 (file)
@@ -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
index ba776c01b5528d81e63e1deb63abe7fa3c491aa1..ed177475dd8ce6abbee845f40e1cdbe3ebe72adb 100644 (file)
@@ -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;
 
@@ -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
index a9d112d2a135af2ac6064a52d61f490c00bac29b..688ce274f59d47de023332e5517c1a3a73ea8ab4 100644 (file)
@@ -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
index 9a4f1f5c1f0ea6ac0781fa8f76d9abb22b9bf2fc..5c81fdd032c3b1269549f27e27348e9606eb5424 100644 (file)
@@ -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;
 
@@ -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;
index 91ad76f30d18d56b9075ab500e072a407f2ed077..fc5fffbb331b1f462ff172198cc5c8abd7f6e578 100644 (file)
@@ -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
index 1ad2299d795dd0689d2f693b80b928f8d52c3bd2..0f0502e12f6c4c8accbe7fc28eb4db08158decfb 100644 (file)
@@ -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;
 
@@ -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
index f6aebcb7a0f862b89160990f1a42cd6b294f2ae5..ac2b75d74cd29db125fb98076037387871e7245a 100644 (file)
@@ -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
index db57078075c5ff03ea0b9be5ba32e929c55c9143..a94504d99c47e8eabb8ff1531bb556e505e5b8f4 100644 (file)
@@ -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;
@@ -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;
 }
@@ -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
@@ -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
index 0f948cefaeb15d49700aa826010466eeeae26f96..782ddbbc1c9a6fe417a68448dd86731126036b78 100644 (file)
@@ -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
index ebfa52030d5c2f9098ba8e6a3ab543ca53634c58..9d518d693b4b7adf07463c695f3cd14412a19192 100644 (file)
@@ -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;
 
@@ -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);
 
diff --git a/include/linux/elf-randomize.h b/include/linux/elf-randomize.h
new file mode 100644 (file)
index 0000000..7a4eda0
--- /dev/null
@@ -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