Skip to content

Commit 2b68f6c

Browse files
keestorvalds
authored andcommitted
mm: expose arch_mmap_rnd when available
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]>
1 parent 8e89a35 commit 2b68f6c

File tree

14 files changed

+37
-14
lines changed

14 files changed

+37
-14
lines changed

arch/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,13 @@ config PGTABLE_LEVELS
491491
int
492492
default 2
493493

494+
config ARCH_HAS_ELF_RANDOMIZE
495+
bool
496+
help
497+
An architecture supports choosing randomized locations for
498+
stack, mmap, brk, and ET_DYN. Defined functions:
499+
- arch_mmap_rnd()
500+
494501
#
495502
# ABI hall of shame
496503
#

arch/arm/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ config ARM
33
default y
44
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
55
select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
6+
select ARCH_HAS_ELF_RANDOMIZE
67
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
78
select ARCH_HAVE_CUSTOM_GPIO_H
89
select ARCH_HAS_GCOV_PROFILE_ALL

arch/arm/mm/mmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
169169
return addr;
170170
}
171171

172-
static unsigned long mmap_rnd(void)
172+
unsigned long arch_mmap_rnd(void)
173173
{
174174
unsigned long rnd;
175175

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

186186
if (current->flags & PF_RANDOMIZE)
187-
random_factor = mmap_rnd();
187+
random_factor = arch_mmap_rnd();
188188

189189
if (mmap_is_legacy()) {
190190
mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;

arch/arm64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ config ARM64
22
def_bool y
33
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
44
select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
5+
select ARCH_HAS_ELF_RANDOMIZE
56
select ARCH_HAS_GCOV_PROFILE_ALL
67
select ARCH_HAS_SG_CHAIN
78
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST

arch/arm64/mm/mmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static int mmap_is_legacy(void)
4747
return sysctl_legacy_va_layout;
4848
}
4949

50-
static unsigned long mmap_rnd(void)
50+
unsigned long arch_mmap_rnd(void)
5151
{
5252
unsigned long rnd;
5353

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

7979
if (current->flags & PF_RANDOMIZE)
80-
random_factor = mmap_rnd();
80+
random_factor = arch_mmap_rnd();
8181

8282
/*
8383
* Fall back to the standard layout if the personality bit is set, or

arch/mips/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ config MIPS
2424
select HAVE_DEBUG_KMEMLEAK
2525
select HAVE_SYSCALL_TRACEPOINTS
2626
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
27+
select ARCH_HAS_ELF_RANDOMIZE
2728
select HAVE_ARCH_TRANSPARENT_HUGEPAGE if CPU_SUPPORTS_HUGEPAGES && 64BIT
2829
select RTC_LIB if !MACH_LOONGSON
2930
select GENERIC_ATOMIC64 if !64BIT

arch/mips/mm/mmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ unsigned long arch_get_unmapped_area_topdown(struct file *filp,
142142
addr0, len, pgoff, flags, DOWN);
143143
}
144144

145-
static unsigned long mmap_rnd(void)
145+
unsigned long arch_mmap_rnd(void)
146146
{
147147
unsigned long rnd;
148148

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

163163
if (current->flags & PF_RANDOMIZE)
164-
random_factor = mmap_rnd();
164+
random_factor = arch_mmap_rnd();
165165

166166
if (mmap_is_legacy()) {
167167
mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;

arch/powerpc/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ config PPC
8989
select ARCH_MIGHT_HAVE_PC_SERIO
9090
select BINFMT_ELF
9191
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
92+
select ARCH_HAS_ELF_RANDOMIZE
9293
select OF
9394
select OF_EARLY_FLATTREE
9495
select OF_RESERVED_MEM

arch/powerpc/mm/mmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static inline int mmap_is_legacy(void)
5353
return sysctl_legacy_va_layout;
5454
}
5555

56-
static unsigned long mmap_rnd(void)
56+
unsigned long arch_mmap_rnd(void)
5757
{
5858
unsigned long rnd;
5959

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

8989
if (current->flags & PF_RANDOMIZE)
90-
random_factor = mmap_rnd();
90+
random_factor = arch_mmap_rnd();
9191

9292
/*
9393
* Fall back to the standard layout if the personality

arch/s390/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ config S390
6565
def_bool y
6666
select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
6767
select ARCH_HAS_DEBUG_STRICT_USER_COPY_CHECKS
68+
select ARCH_HAS_ELF_RANDOMIZE
6869
select ARCH_HAS_GCOV_PROFILE_ALL
6970
select ARCH_HAS_SG_CHAIN
7071
select ARCH_HAVE_NMI_SAFE_CMPXCHG

arch/s390/mm/mmap.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static inline int mmap_is_legacy(void)
6060
return sysctl_legacy_va_layout;
6161
}
6262

63-
static unsigned long mmap_rnd(void)
63+
unsigned long arch_mmap_rnd(void)
6464
{
6565
if (is_32bit_task())
6666
return (get_random_int() & 0x7ff) << PAGE_SHIFT;
@@ -187,7 +187,7 @@ unsigned long randomize_et_dyn(void)
187187
base &= ~((1UL << 32) - 1);
188188

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

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

205205
if (current->flags & PF_RANDOMIZE)
206-
random_factor = mmap_rnd();
206+
random_factor = arch_mmap_rnd();
207207

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

285285
if (current->flags & PF_RANDOMIZE)
286-
random_factor = mmap_rnd();
286+
random_factor = arch_mmap_rnd();
287287

288288
/*
289289
* Fall back to the standard layout if the personality

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ config X86
8888
select HAVE_ARCH_KASAN if X86_64 && SPARSEMEM_VMEMMAP
8989
select HAVE_USER_RETURN_NOTIFIER
9090
select ARCH_BINFMT_ELF_RANDOMIZE_PIE
91+
select ARCH_HAS_ELF_RANDOMIZE
9192
select HAVE_ARCH_JUMP_LABEL
9293
select ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
9394
select SPARSE_IRQ

arch/x86/mm/mmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static int mmap_is_legacy(void)
6565
return sysctl_legacy_va_layout;
6666
}
6767

68-
static unsigned long mmap_rnd(void)
68+
unsigned long arch_mmap_rnd(void)
6969
{
7070
unsigned long rnd;
7171

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

116116
if (current->flags & PF_RANDOMIZE)
117-
random_factor = mmap_rnd();
117+
random_factor = arch_mmap_rnd();
118118

119119
mm->mmap_legacy_base = mmap_legacy_base(random_factor);
120120

include/linux/elf-randomize.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef _ELF_RANDOMIZE_H
2+
#define _ELF_RANDOMIZE_H
3+
4+
#ifndef CONFIG_ARCH_HAS_ELF_RANDOMIZE
5+
static inline unsigned long arch_mmap_rnd(void) { return 0; }
6+
#else
7+
extern unsigned long arch_mmap_rnd(void);
8+
#endif
9+
10+
#endif

0 commit comments

Comments
 (0)