Commit ed632274 authored by Kees Cook's avatar Kees Cook Committed by Linus Torvalds

powerpc: standardize mmap_rnd() usage

In preparation for splitting out ET_DYN ASLR, this refactors the use of
mmap_rnd() to be used similarly to arm and x86.

(Can mmap ASLR be safely enabled in the legacy mmap case here?  Other
archs use "mm->mmap_base = TASK_UNMAPPED_BASE + random_factor".)
Signed-off-by: default avatarKees Cook <keescook@chromium.org>
Reviewed-by: default avatarIngo Molnar <mingo@kernel.org>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 1f0569df
...@@ -55,19 +55,18 @@ static inline int mmap_is_legacy(void) ...@@ -55,19 +55,18 @@ static inline int mmap_is_legacy(void)
static unsigned long mmap_rnd(void) static unsigned long mmap_rnd(void)
{ {
unsigned long rnd = 0; unsigned long rnd;
/* 8MB for 32bit, 1GB for 64bit */
if (is_32bit_task())
rnd = (unsigned long)get_random_int() % (1<<(23-PAGE_SHIFT));
else
rnd = (unsigned long)get_random_int() % (1<<(30-PAGE_SHIFT));
if (current->flags & PF_RANDOMIZE) {
/* 8MB for 32bit, 1GB for 64bit */
if (is_32bit_task())
rnd = (long)(get_random_int() % (1<<(23-PAGE_SHIFT)));
else
rnd = (long)(get_random_int() % (1<<(30-PAGE_SHIFT)));
}
return rnd << PAGE_SHIFT; return rnd << PAGE_SHIFT;
} }
static inline unsigned long mmap_base(void) static inline unsigned long mmap_base(unsigned long rnd)
{ {
unsigned long gap = rlimit(RLIMIT_STACK); unsigned long gap = rlimit(RLIMIT_STACK);
...@@ -76,7 +75,7 @@ static inline unsigned long mmap_base(void) ...@@ -76,7 +75,7 @@ static inline unsigned long mmap_base(void)
else if (gap > MAX_GAP) else if (gap > MAX_GAP)
gap = MAX_GAP; gap = MAX_GAP;
return PAGE_ALIGN(TASK_SIZE - gap - mmap_rnd()); return PAGE_ALIGN(TASK_SIZE - gap - rnd);
} }
/* /*
...@@ -85,6 +84,11 @@ static inline unsigned long mmap_base(void) ...@@ -85,6 +84,11 @@ static inline unsigned long mmap_base(void)
*/ */
void arch_pick_mmap_layout(struct mm_struct *mm) void arch_pick_mmap_layout(struct mm_struct *mm)
{ {
unsigned long random_factor = 0UL;
if (current->flags & PF_RANDOMIZE)
random_factor = mmap_rnd();
/* /*
* Fall back to the standard layout if the personality * Fall back to the standard layout if the personality
* bit is set, or if the expected stack growth is unlimited: * bit is set, or if the expected stack growth is unlimited:
...@@ -93,7 +97,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm) ...@@ -93,7 +97,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm)
mm->mmap_base = TASK_UNMAPPED_BASE; mm->mmap_base = TASK_UNMAPPED_BASE;
mm->get_unmapped_area = arch_get_unmapped_area; mm->get_unmapped_area = arch_get_unmapped_area;
} else { } else {
mm->mmap_base = mmap_base(); mm->mmap_base = mmap_base(random_factor);
mm->get_unmapped_area = arch_get_unmapped_area_topdown; mm->get_unmapped_area = arch_get_unmapped_area_topdown;
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment