Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm: fold arch_randomize_brk into ARCH_HAS_ELF_RANDOMIZE

The arch_randomize_brk() function is used on several architectures,
even those that don't support ET_DYN ASLR. To avoid bulky extern/#define
tricks, consolidate the support under CONFIG_ARCH_HAS_ELF_RANDOMIZE for
the architectures that support it, while still handling CONFIG_COMPAT_BRK.

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>

authored by

Kees Cook and committed by
Linus Torvalds
204db6ed d1fd836d

+14 -25
+1
arch/Kconfig
··· 497 497 An architecture supports choosing randomized locations for 498 498 stack, mmap, brk, and ET_DYN. Defined functions: 499 499 - arch_mmap_rnd() 500 + - arch_randomize_brk() 500 501 501 502 # 502 503 # ABI hall of shame
-4
arch/arm/include/asm/elf.h
··· 125 125 extern void elf_set_personality(const struct elf32_hdr *); 126 126 #define SET_PERSONALITY(ex) elf_set_personality(&(ex)) 127 127 128 - struct mm_struct; 129 - extern unsigned long arch_randomize_brk(struct mm_struct *mm); 130 - #define arch_randomize_brk arch_randomize_brk 131 - 132 128 #ifdef CONFIG_MMU 133 129 #define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1 134 130 struct linux_binprm;
-4
arch/arm64/include/asm/elf.h
··· 156 156 #define STACK_RND_MASK (0x3ffff >> (PAGE_SHIFT - 12)) 157 157 #endif 158 158 159 - struct mm_struct; 160 - extern unsigned long arch_randomize_brk(struct mm_struct *mm); 161 - #define arch_randomize_brk arch_randomize_brk 162 - 163 159 #ifdef CONFIG_COMPAT 164 160 165 161 #ifdef __AARCH64EB__
-4
arch/mips/include/asm/elf.h
··· 410 410 extern int arch_setup_additional_pages(struct linux_binprm *bprm, 411 411 int uses_interp); 412 412 413 - struct mm_struct; 414 - extern unsigned long arch_randomize_brk(struct mm_struct *mm); 415 - #define arch_randomize_brk arch_randomize_brk 416 - 417 413 struct arch_elf_state { 418 414 int fp_abi; 419 415 int interp_fp_abi;
-4
arch/powerpc/include/asm/elf.h
··· 128 128 (0x7ff >> (PAGE_SHIFT - 12)) : \ 129 129 (0x3ffff >> (PAGE_SHIFT - 12))) 130 130 131 - extern unsigned long arch_randomize_brk(struct mm_struct *mm); 132 - #define arch_randomize_brk arch_randomize_brk 133 - 134 - 135 131 #ifdef CONFIG_SPU_BASE 136 132 /* Notes used in ET_CORE. Note name is "SPU/<fd>/<filename>". */ 137 133 #define NT_SPU 1
-3
arch/s390/include/asm/elf.h
··· 226 226 #define ARCH_HAS_SETUP_ADDITIONAL_PAGES 1 227 227 int arch_setup_additional_pages(struct linux_binprm *, int); 228 228 229 - extern unsigned long arch_randomize_brk(struct mm_struct *mm); 230 - #define arch_randomize_brk arch_randomize_brk 231 - 232 229 void *fill_cpu_elf_notes(void *ptr, struct save_area *sa, __vector128 *vxrs); 233 230 234 231 #endif
-3
arch/x86/include/asm/elf.h
··· 339 339 int uses_interp); 340 340 #define compat_arch_setup_additional_pages compat_arch_setup_additional_pages 341 341 342 - extern unsigned long arch_randomize_brk(struct mm_struct *mm); 343 - #define arch_randomize_brk arch_randomize_brk 344 - 345 342 /* 346 343 * True on X86_32 or when emulating IA32 on X86_64 347 344 */
+1 -3
fs/binfmt_elf.c
··· 1050 1050 current->mm->end_data = end_data; 1051 1051 current->mm->start_stack = bprm->p; 1052 1052 1053 - #ifdef arch_randomize_brk 1054 1053 if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1)) { 1055 1054 current->mm->brk = current->mm->start_brk = 1056 1055 arch_randomize_brk(current->mm); 1057 - #ifdef CONFIG_COMPAT_BRK 1056 + #ifdef compat_brk_randomized 1058 1057 current->brk_randomized = 1; 1059 1058 #endif 1060 1059 } 1061 - #endif 1062 1060 1063 1061 if (current->personality & MMAP_PAGE_ZERO) { 1064 1062 /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
+12
include/linux/elf-randomize.h
··· 1 1 #ifndef _ELF_RANDOMIZE_H 2 2 #define _ELF_RANDOMIZE_H 3 3 4 + struct mm_struct; 5 + 4 6 #ifndef CONFIG_ARCH_HAS_ELF_RANDOMIZE 5 7 static inline unsigned long arch_mmap_rnd(void) { return 0; } 8 + # if defined(arch_randomize_brk) && defined(CONFIG_COMPAT_BRK) 9 + # define compat_brk_randomized 10 + # endif 11 + # ifndef arch_randomize_brk 12 + # define arch_randomize_brk(mm) (mm->brk) 13 + # endif 6 14 #else 7 15 extern unsigned long arch_mmap_rnd(void); 16 + extern unsigned long arch_randomize_brk(struct mm_struct *mm); 17 + # ifdef CONFIG_COMPAT_BRK 18 + # define compat_brk_randomized 19 + # endif 8 20 #endif 9 21 10 22 #endif