xref: /openbmc/linux/arch/x86/mm/mmap.c (revision 3f07c0144132e4f59d88055ac8ff3e691a5fa2b8)
1675a0813SHarvey Harrison /*
2675a0813SHarvey Harrison  * Flexible mmap layout support
3675a0813SHarvey Harrison  *
4675a0813SHarvey Harrison  * Based on code by Ingo Molnar and Andi Kleen, copyrighted
5675a0813SHarvey Harrison  * as follows:
6675a0813SHarvey Harrison  *
78f47e163SIngo Molnar  * Copyright 2003-2009 Red Hat Inc.
8675a0813SHarvey Harrison  * All Rights Reserved.
9675a0813SHarvey Harrison  * Copyright 2005 Andi Kleen, SUSE Labs.
10675a0813SHarvey Harrison  * Copyright 2007 Jiri Kosina, SUSE Labs.
11675a0813SHarvey Harrison  *
12675a0813SHarvey Harrison  * This program is free software; you can redistribute it and/or modify
13675a0813SHarvey Harrison  * it under the terms of the GNU General Public License as published by
14675a0813SHarvey Harrison  * the Free Software Foundation; either version 2 of the License, or
15675a0813SHarvey Harrison  * (at your option) any later version.
16675a0813SHarvey Harrison  *
17675a0813SHarvey Harrison  * This program is distributed in the hope that it will be useful,
18675a0813SHarvey Harrison  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19675a0813SHarvey Harrison  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20675a0813SHarvey Harrison  * GNU General Public License for more details.
21675a0813SHarvey Harrison  *
22675a0813SHarvey Harrison  * You should have received a copy of the GNU General Public License
23675a0813SHarvey Harrison  * along with this program; if not, write to the Free Software
24675a0813SHarvey Harrison  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25675a0813SHarvey Harrison  */
26675a0813SHarvey Harrison 
27675a0813SHarvey Harrison #include <linux/personality.h>
28675a0813SHarvey Harrison #include <linux/mm.h>
29675a0813SHarvey Harrison #include <linux/random.h>
30675a0813SHarvey Harrison #include <linux/limits.h>
31*3f07c014SIngo Molnar #include <linux/sched/signal.h>
3280938332SMichal Hocko #include <asm/elf.h>
3380938332SMichal Hocko 
34cc99535eSJan-Simon Möller struct va_alignment __read_mostly va_align = {
359387f774SBorislav Petkov 	.flags = -1,
369387f774SBorislav Petkov };
379387f774SBorislav Petkov 
384e7c22d4SHector Marco-Gisbert static unsigned long stack_maxrandom_size(void)
3980938332SMichal Hocko {
404e7c22d4SHector Marco-Gisbert 	unsigned long max = 0;
4180938332SMichal Hocko 	if ((current->flags & PF_RANDOMIZE) &&
4280938332SMichal Hocko 		!(current->personality & ADDR_NO_RANDOMIZE)) {
434e7c22d4SHector Marco-Gisbert 		max = ((-1UL) & STACK_RND_MASK) << PAGE_SHIFT;
4480938332SMichal Hocko 	}
4580938332SMichal Hocko 
4680938332SMichal Hocko 	return max;
4780938332SMichal Hocko }
4880938332SMichal Hocko 
49675a0813SHarvey Harrison /*
50675a0813SHarvey Harrison  * Top of mmap area (just below the process stack).
51675a0813SHarvey Harrison  *
5280938332SMichal Hocko  * Leave an at least ~128 MB hole with possible stack randomization.
53675a0813SHarvey Harrison  */
5480938332SMichal Hocko #define MIN_GAP (128*1024*1024UL + stack_maxrandom_size())
55675a0813SHarvey Harrison #define MAX_GAP (TASK_SIZE/6*5)
56675a0813SHarvey Harrison 
57675a0813SHarvey Harrison static int mmap_is_legacy(void)
58675a0813SHarvey Harrison {
59675a0813SHarvey Harrison 	if (current->personality & ADDR_COMPAT_LAYOUT)
60675a0813SHarvey Harrison 		return 1;
61675a0813SHarvey Harrison 
622854e72bSJiri Slaby 	if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
63675a0813SHarvey Harrison 		return 1;
64675a0813SHarvey Harrison 
65675a0813SHarvey Harrison 	return sysctl_legacy_va_layout;
66675a0813SHarvey Harrison }
67675a0813SHarvey Harrison 
682b68f6caSKees Cook unsigned long arch_mmap_rnd(void)
69675a0813SHarvey Harrison {
7082168140SKees Cook 	unsigned long rnd;
71675a0813SHarvey Harrison 
72675a0813SHarvey Harrison 	if (mmap_is_ia32())
739e08f57dSDaniel Cashman #ifdef CONFIG_COMPAT
745ef11c35SDaniel Cashman 		rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
759e08f57dSDaniel Cashman #else
765ef11c35SDaniel Cashman 		rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
779e08f57dSDaniel Cashman #endif
78675a0813SHarvey Harrison 	else
795ef11c35SDaniel Cashman 		rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
8082168140SKees Cook 
81675a0813SHarvey Harrison 	return rnd << PAGE_SHIFT;
82675a0813SHarvey Harrison }
83675a0813SHarvey Harrison 
8482168140SKees Cook static unsigned long mmap_base(unsigned long rnd)
85675a0813SHarvey Harrison {
862854e72bSJiri Slaby 	unsigned long gap = rlimit(RLIMIT_STACK);
87675a0813SHarvey Harrison 
88675a0813SHarvey Harrison 	if (gap < MIN_GAP)
89675a0813SHarvey Harrison 		gap = MIN_GAP;
90675a0813SHarvey Harrison 	else if (gap > MAX_GAP)
91675a0813SHarvey Harrison 		gap = MAX_GAP;
92675a0813SHarvey Harrison 
9382168140SKees Cook 	return PAGE_ALIGN(TASK_SIZE - gap - rnd);
94675a0813SHarvey Harrison }
95675a0813SHarvey Harrison 
96675a0813SHarvey Harrison /*
97675a0813SHarvey Harrison  * This function, called very early during the creation of a new
98675a0813SHarvey Harrison  * process VM image, sets up which VM layout function to use:
99675a0813SHarvey Harrison  */
100675a0813SHarvey Harrison void arch_pick_mmap_layout(struct mm_struct *mm)
101675a0813SHarvey Harrison {
10282168140SKees Cook 	unsigned long random_factor = 0UL;
10382168140SKees Cook 
10482168140SKees Cook 	if (current->flags & PF_RANDOMIZE)
1052b68f6caSKees Cook 		random_factor = arch_mmap_rnd();
10682168140SKees Cook 
1078b8addf8SHector Marco-Gisbert 	mm->mmap_legacy_base = TASK_UNMAPPED_BASE + random_factor;
10841aacc1eSRadu Caragea 
109675a0813SHarvey Harrison 	if (mmap_is_legacy()) {
11041aacc1eSRadu Caragea 		mm->mmap_base = mm->mmap_legacy_base;
111675a0813SHarvey Harrison 		mm->get_unmapped_area = arch_get_unmapped_area;
112675a0813SHarvey Harrison 	} else {
11382168140SKees Cook 		mm->mmap_base = mmap_base(random_factor);
114675a0813SHarvey Harrison 		mm->get_unmapped_area = arch_get_unmapped_area_topdown;
115675a0813SHarvey Harrison 	}
116675a0813SHarvey Harrison }
117a8965276SKirill A. Shutemov 
118a8965276SKirill A. Shutemov const char *arch_vma_name(struct vm_area_struct *vma)
119a8965276SKirill A. Shutemov {
120a8965276SKirill A. Shutemov 	if (vma->vm_flags & VM_MPX)
121a8965276SKirill A. Shutemov 		return "[mpx]";
122a8965276SKirill A. Shutemov 	return NULL;
123a8965276SKirill A. Shutemov }
124