UPSTREAM: drivers: char: random: add get_random_long()

Commit d07e22597d1d ("mm: mmap: add new /proc tunable for mmap_base
ASLR") added the ability to choose from a range of values to use for
entropy count in generating the random offset to the mmap_base address.

The maximum value on this range was set to 32 bits for 64-bit x86
systems, but this value could be increased further, requiring more than
the 32 bits of randomness provided by get_random_int(), as is already
possible for arm64.  Add a new function: get_random_long() which more
naturally fits with the mmap usage of get_random_int() but operates
exactly the same as get_random_int().

Also, fix the shifting constant in mmap_rnd() to be an unsigned long so
that values greater than 31 bits generate an appropriate mask without
overflow.  This is especially important on x86, as its shift instruction
uses a 5-bit mask for the shift operand, which meant that any value for
mmap_rnd_bits over 31 acts as a no-op and effectively disables mmap_base

Finally, replace calls to get_random_int() with get_random_long() where

This patch (of 2):

Add get_random_long().

Signed-off-by: Daniel Cashman <>
Acked-by: Kees Cook <>
Cc: "Theodore Ts'o" <>
Cc: Arnd Bergmann <>
Cc: Greg Kroah-Hartman <>
Cc: Catalin Marinas <>
Cc: Will Deacon <>
Cc: Ralf Baechle <>
Cc: Benjamin Herrenschmidt <>
Cc: Paul Mackerras <>
Cc: Michael Ellerman <>
Cc: David S. Miller <>
Cc: Thomas Gleixner <>
Cc: Ingo Molnar <>
Cc: H. Peter Anvin <>
Cc: Al Viro <>
Cc: Nick Kralevich <>
Cc: Jeff Vander Stoep <>
Cc: Mark Salyzyn <>
Signed-off-by: Andrew Morton <>
Signed-off-by: Linus Torvalds <>

Bug: 27796957
Patchset: ASLR sysctl

(cherry picked from commit ec9ee4acd97c0039a61c0ae4f12705767ae62153)
Signed-off-by: Kees Cook <>
Change-Id: I80d9574d98ed871873301530ad453589658410ee
2 files changed