Skip to content

Commit

Permalink
x86: standardize mmap_rnd() usage
Browse files Browse the repository at this point in the history
commit 82168140bc4cec7ec9bad39705518541149ff8b7 upstream.

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

Signed-off-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Ingo Molnar <mingo@kernel.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Andy Lutomirski <luto@amacapital.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
Signed-off-by: Willy Tarreau <w@1wt.eu>
  • Loading branch information
kees authored and AndroiableDroid committed Jan 11, 2018
1 parent 4e14634 commit 5f5e801
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions arch/x86/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ static unsigned long mmap_rnd(void)
{
unsigned long rnd;

if (current->flags & PF_RANDOMIZE) {
if (mmap_is_ia32())
rnd = get_random_long() % (1UL<<8);
else
rnd = get_random_long() % (1UL<<28);
}
/* 8 bits of randomness in 32bit mmaps, 20 address space bits
* 28 bits of randomness in 64bit mmaps, 40 address space bits
*/
if (mmap_is_ia32())
rnd = (unsigned long)get_random_int() % (1<<8);
else
rnd = (unsigned long)get_random_int() % (1<<28);

return rnd << PAGE_SHIFT;
}

Expand All @@ -90,6 +92,18 @@ static unsigned long mmap_base(unsigned long rnd)
return PAGE_ALIGN(TASK_SIZE - gap - rnd);
}

/*
* Bottom-up (legacy) layout on X86_32 did not support randomization, X86_64
* does, but not when emulating X86_32
*/
static unsigned long mmap_legacy_base(unsigned long rnd)
{
if (mmap_is_ia32())
return TASK_UNMAPPED_BASE;
else
return TASK_UNMAPPED_BASE + rnd;
}

/*
* This function, called very early during the creation of a new
* process VM image, sets up which VM layout function to use:
Expand All @@ -101,7 +115,7 @@ void arch_pick_mmap_layout(struct mm_struct *mm)
if (current->flags & PF_RANDOMIZE)
random_factor = mmap_rnd();

mm->mmap_legacy_base = TASK_UNMAPPED_BASE + random_factor;
mm->mmap_legacy_base = mmap_legacy_base(random_factor);

if (mmap_is_legacy()) {
mm->mmap_base = mm->mmap_legacy_base;
Expand Down

0 comments on commit 5f5e801

Please sign in to comment.