arm64, mm: move generic mmap layout functions to mm
arm64 handles top-down mmap layout in a way that can be easily reused by other architectures, so make it available in mm. It then introduces a new config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT that can be set by other architectures to benefit from those functions. Note that this new config depends on MMU being enabled, if selected without MMU support, a warning will be thrown. Link: http://lkml.kernel.org/r/20190730055113.23635-5-alex@ghiti.fr Signed-off-by: Alexandre Ghiti <alex@ghiti.fr> Suggested-by: Christoph Hellwig <hch@infradead.org> Acked-by: Catalin Marinas <catalin.marinas@arm.com> Acked-by: Kees Cook <keescook@chromium.org> Reviewed-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Luis Chamberlain <mcgrof@kernel.org> Cc: Albert Ou <aou@eecs.berkeley.edu> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: James Hogan <jhogan@kernel.org> Cc: Palmer Dabbelt <palmer@sifive.com> Cc: Paul Burton <paul.burton@mips.com> Cc: Ralf Baechle <ralf@linux-mips.org> Cc: Russell King <linux@armlinux.org.uk> Cc: Will Deacon <will.deacon@arm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
e8d54b62c5
commit
67f3977f80
10
arch/Kconfig
10
arch/Kconfig
|
@ -706,6 +706,16 @@ config HAVE_ARCH_COMPAT_MMAP_BASES
|
||||||
and vice-versa 32-bit applications to call 64-bit mmap().
|
and vice-versa 32-bit applications to call 64-bit mmap().
|
||||||
Required for applications doing different bitness syscalls.
|
Required for applications doing different bitness syscalls.
|
||||||
|
|
||||||
|
# This allows to use a set of generic functions to determine mmap base
|
||||||
|
# address by giving priority to top-down scheme only if the process
|
||||||
|
# is not in legacy mode (compat task, unlimited stack size or
|
||||||
|
# sysctl_legacy_va_layout).
|
||||||
|
# Architecture that selects this option can provide its own version of:
|
||||||
|
# - STACK_RND_MASK
|
||||||
|
config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT
|
||||||
|
bool
|
||||||
|
depends on MMU
|
||||||
|
|
||||||
config HAVE_COPY_THREAD_TLS
|
config HAVE_COPY_THREAD_TLS
|
||||||
bool
|
bool
|
||||||
help
|
help
|
||||||
|
|
|
@ -71,6 +71,7 @@ config ARM64
|
||||||
select ARCH_SUPPORTS_INT128 if GCC_VERSION >= 50000 || CC_IS_CLANG
|
select ARCH_SUPPORTS_INT128 if GCC_VERSION >= 50000 || CC_IS_CLANG
|
||||||
select ARCH_SUPPORTS_NUMA_BALANCING
|
select ARCH_SUPPORTS_NUMA_BALANCING
|
||||||
select ARCH_WANT_COMPAT_IPC_PARSE_VERSION if COMPAT
|
select ARCH_WANT_COMPAT_IPC_PARSE_VERSION if COMPAT
|
||||||
|
select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT
|
||||||
select ARCH_WANT_FRAME_POINTERS
|
select ARCH_WANT_FRAME_POINTERS
|
||||||
select ARCH_WANT_HUGE_PMD_SHARE if ARM64_4K_PAGES || (ARM64_16K_PAGES && !ARM64_VA_BITS_36)
|
select ARCH_WANT_HUGE_PMD_SHARE if ARM64_4K_PAGES || (ARM64_16K_PAGES && !ARM64_VA_BITS_36)
|
||||||
select ARCH_HAS_UBSAN_SANITIZE_ALL
|
select ARCH_HAS_UBSAN_SANITIZE_ALL
|
||||||
|
|
|
@ -280,8 +280,6 @@ static inline void spin_lock_prefetch(const void *ptr)
|
||||||
"nop") : : "p" (ptr));
|
"nop") : : "p" (ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
#define HAVE_ARCH_PICK_MMAP_LAYOUT
|
|
||||||
|
|
||||||
extern unsigned long __ro_after_init signal_minsigstksz; /* sigframe size */
|
extern unsigned long __ro_after_init signal_minsigstksz; /* sigframe size */
|
||||||
extern void __init minsigstksz_setup(void);
|
extern void __init minsigstksz_setup(void);
|
||||||
|
|
||||||
|
|
|
@ -20,82 +20,6 @@
|
||||||
|
|
||||||
#include <asm/cputype.h>
|
#include <asm/cputype.h>
|
||||||
|
|
||||||
/*
|
|
||||||
* Leave enough space between the mmap area and the stack to honour ulimit in
|
|
||||||
* the face of randomisation.
|
|
||||||
*/
|
|
||||||
#define MIN_GAP (SZ_128M)
|
|
||||||
#define MAX_GAP (STACK_TOP/6*5)
|
|
||||||
|
|
||||||
static int mmap_is_legacy(struct rlimit *rlim_stack)
|
|
||||||
{
|
|
||||||
if (current->personality & ADDR_COMPAT_LAYOUT)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
if (rlim_stack->rlim_cur == RLIM_INFINITY)
|
|
||||||
return 1;
|
|
||||||
|
|
||||||
return sysctl_legacy_va_layout;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned long arch_mmap_rnd(void)
|
|
||||||
{
|
|
||||||
unsigned long rnd;
|
|
||||||
|
|
||||||
#ifdef CONFIG_COMPAT
|
|
||||||
if (is_compat_task())
|
|
||||||
rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
|
|
||||||
else
|
|
||||||
#endif
|
|
||||||
rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
|
|
||||||
return rnd << PAGE_SHIFT;
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack)
|
|
||||||
{
|
|
||||||
unsigned long gap = rlim_stack->rlim_cur;
|
|
||||||
unsigned long pad = stack_guard_gap;
|
|
||||||
|
|
||||||
/* Account for stack randomization if necessary */
|
|
||||||
if (current->flags & PF_RANDOMIZE)
|
|
||||||
pad += (STACK_RND_MASK << PAGE_SHIFT);
|
|
||||||
|
|
||||||
/* Values close to RLIM_INFINITY can overflow. */
|
|
||||||
if (gap + pad > gap)
|
|
||||||
gap += pad;
|
|
||||||
|
|
||||||
if (gap < MIN_GAP)
|
|
||||||
gap = MIN_GAP;
|
|
||||||
else if (gap > MAX_GAP)
|
|
||||||
gap = MAX_GAP;
|
|
||||||
|
|
||||||
return PAGE_ALIGN(STACK_TOP - gap - rnd);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* This function, called very early during the creation of a new process VM
|
|
||||||
* image, sets up which VM layout function to use:
|
|
||||||
*/
|
|
||||||
void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
|
|
||||||
{
|
|
||||||
unsigned long random_factor = 0UL;
|
|
||||||
|
|
||||||
if (current->flags & PF_RANDOMIZE)
|
|
||||||
random_factor = arch_mmap_rnd();
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Fall back to the standard layout if the personality bit is set, or
|
|
||||||
* if the expected stack growth is unlimited:
|
|
||||||
*/
|
|
||||||
if (mmap_is_legacy(rlim_stack)) {
|
|
||||||
mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
|
|
||||||
mm->get_unmapped_area = arch_get_unmapped_area;
|
|
||||||
} else {
|
|
||||||
mm->mmap_base = mmap_base(random_factor, rlim_stack);
|
|
||||||
mm->get_unmapped_area = arch_get_unmapped_area_topdown;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* You really shouldn't be using read() or write() on /dev/mem. This might go
|
* You really shouldn't be using read() or write() on /dev/mem. This might go
|
||||||
* away in the future.
|
* away in the future.
|
||||||
|
|
|
@ -264,7 +264,8 @@ extern struct ctl_table epoll_table[];
|
||||||
extern struct ctl_table firmware_config_table[];
|
extern struct ctl_table firmware_config_table[];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
|
#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \
|
||||||
|
defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT)
|
||||||
int sysctl_legacy_va_layout;
|
int sysctl_legacy_va_layout;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -1573,7 +1574,8 @@ static struct ctl_table vm_table[] = {
|
||||||
.proc_handler = proc_dointvec,
|
.proc_handler = proc_dointvec,
|
||||||
.extra1 = SYSCTL_ZERO,
|
.extra1 = SYSCTL_ZERO,
|
||||||
},
|
},
|
||||||
#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT
|
#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \
|
||||||
|
defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT)
|
||||||
{
|
{
|
||||||
.procname = "legacy_va_layout",
|
.procname = "legacy_va_layout",
|
||||||
.data = &sysctl_legacy_va_layout,
|
.data = &sysctl_legacy_va_layout,
|
||||||
|
|
78
mm/util.c
78
mm/util.c
|
@ -17,7 +17,12 @@
|
||||||
#include <linux/vmalloc.h>
|
#include <linux/vmalloc.h>
|
||||||
#include <linux/userfaultfd_k.h>
|
#include <linux/userfaultfd_k.h>
|
||||||
#include <linux/elf.h>
|
#include <linux/elf.h>
|
||||||
|
#include <linux/elf-randomize.h>
|
||||||
|
#include <linux/personality.h>
|
||||||
#include <linux/random.h>
|
#include <linux/random.h>
|
||||||
|
#include <linux/processor.h>
|
||||||
|
#include <linux/sizes.h>
|
||||||
|
#include <linux/compat.h>
|
||||||
|
|
||||||
#include <linux/uaccess.h>
|
#include <linux/uaccess.h>
|
||||||
|
|
||||||
|
@ -315,7 +320,78 @@ unsigned long randomize_stack_top(unsigned long stack_top)
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
|
#ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT
|
||||||
|
#ifdef CONFIG_ARCH_HAS_ELF_RANDOMIZE
|
||||||
|
unsigned long arch_mmap_rnd(void)
|
||||||
|
{
|
||||||
|
unsigned long rnd;
|
||||||
|
|
||||||
|
#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS
|
||||||
|
if (is_compat_task())
|
||||||
|
rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
|
||||||
|
else
|
||||||
|
#endif /* CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS */
|
||||||
|
rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
|
||||||
|
|
||||||
|
return rnd << PAGE_SHIFT;
|
||||||
|
}
|
||||||
|
#endif /* CONFIG_ARCH_HAS_ELF_RANDOMIZE */
|
||||||
|
|
||||||
|
static int mmap_is_legacy(struct rlimit *rlim_stack)
|
||||||
|
{
|
||||||
|
if (current->personality & ADDR_COMPAT_LAYOUT)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
if (rlim_stack->rlim_cur == RLIM_INFINITY)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return sysctl_legacy_va_layout;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Leave enough space between the mmap area and the stack to honour ulimit in
|
||||||
|
* the face of randomisation.
|
||||||
|
*/
|
||||||
|
#define MIN_GAP (SZ_128M)
|
||||||
|
#define MAX_GAP (STACK_TOP / 6 * 5)
|
||||||
|
|
||||||
|
static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack)
|
||||||
|
{
|
||||||
|
unsigned long gap = rlim_stack->rlim_cur;
|
||||||
|
unsigned long pad = stack_guard_gap;
|
||||||
|
|
||||||
|
/* Account for stack randomization if necessary */
|
||||||
|
if (current->flags & PF_RANDOMIZE)
|
||||||
|
pad += (STACK_RND_MASK << PAGE_SHIFT);
|
||||||
|
|
||||||
|
/* Values close to RLIM_INFINITY can overflow. */
|
||||||
|
if (gap + pad > gap)
|
||||||
|
gap += pad;
|
||||||
|
|
||||||
|
if (gap < MIN_GAP)
|
||||||
|
gap = MIN_GAP;
|
||||||
|
else if (gap > MAX_GAP)
|
||||||
|
gap = MAX_GAP;
|
||||||
|
|
||||||
|
return PAGE_ALIGN(STACK_TOP - gap - rnd);
|
||||||
|
}
|
||||||
|
|
||||||
|
void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
|
||||||
|
{
|
||||||
|
unsigned long random_factor = 0UL;
|
||||||
|
|
||||||
|
if (current->flags & PF_RANDOMIZE)
|
||||||
|
random_factor = arch_mmap_rnd();
|
||||||
|
|
||||||
|
if (mmap_is_legacy(rlim_stack)) {
|
||||||
|
mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
|
||||||
|
mm->get_unmapped_area = arch_get_unmapped_area;
|
||||||
|
} else {
|
||||||
|
mm->mmap_base = mmap_base(random_factor, rlim_stack);
|
||||||
|
mm->get_unmapped_area = arch_get_unmapped_area_topdown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#elif defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT)
|
||||||
void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
|
void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
|
||||||
{
|
{
|
||||||
mm->mmap_base = TASK_UNMAPPED_BASE;
|
mm->mmap_base = TASK_UNMAPPED_BASE;
|
||||||
|
|
Loading…
Reference in New Issue