[tip:x86/urgent] x86: Fix norandmaps/ADDR_NO_RANDOMIZE

From: tip-bot for Oleg Nesterov
Date: Wed Aug 16 2017 - 14:40:22 EST


Commit-ID: 47ac5484fd961420e5ec0bb5b972fde381f57365
Gitweb: http://git.kernel.org/tip/47ac5484fd961420e5ec0bb5b972fde381f57365
Author: Oleg Nesterov <oleg@xxxxxxxxxx>
AuthorDate: Tue, 15 Aug 2017 17:39:52 +0200
Committer: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
CommitDate: Wed, 16 Aug 2017 20:32:01 +0200

x86: Fix norandmaps/ADDR_NO_RANDOMIZE

Documentation/admin-guide/kernel-parameters.txt says:

norandmaps Don't use address space randomization. Equivalent
to echo 0 > /proc/sys/kernel/randomize_va_space

but it doesn't work because arch_rnd() which is used to randomize
mm->mmap_base returns a random value unconditionally. And as Kirill
pointed out, ADDR_NO_RANDOMIZE is broken by the same reason.

Just shift the PF_RANDOMIZE check from arch_mmap_rnd() to arch_rnd().

Fixes: 1b028f784e8c ("x86/mm: Introduce mmap_compat_base() for 32-bit mmap()")
Signed-off-by: Oleg Nesterov <oleg@xxxxxxxxxx>
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Acked-by: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx>
Acked-by: Cyrill Gorcunov <gorcunov@xxxxxxxxxx>
Reviewed-by: Dmitry Safonov <dsafonov@xxxxxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Cc: Andy Lutomirski <luto@xxxxxxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Borislav Petkov <bp@xxxxxxx>
Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Link: http://lkml.kernel.org/r/20170815153952.GA1076@xxxxxxxxxx

---
arch/x86/mm/mmap.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/x86/mm/mmap.c b/arch/x86/mm/mmap.c
index 229d04a..c94df12 100644
--- a/arch/x86/mm/mmap.c
+++ b/arch/x86/mm/mmap.c
@@ -79,13 +79,13 @@ static int mmap_is_legacy(void)

static unsigned long arch_rnd(unsigned int rndbits)
{
+ if (!(current->flags & PF_RANDOMIZE))
+ return 0;
return (get_random_long() & ((1UL << rndbits) - 1)) << PAGE_SHIFT;
}

unsigned long arch_mmap_rnd(void)
{
- if (!(current->flags & PF_RANDOMIZE))
- return 0;
return arch_rnd(mmap_is_ia32() ? mmap32_rnd_bits : mmap64_rnd_bits);
}