blob: d2dc0438d654a8bbcfc71583cea8f49f693afcff [file] [log] [blame]
Jiri Kosinacc503c12008-01-30 13:31:07 +01001/*
Harvey Harrison675a0812008-01-30 13:31:10 +01002 * Flexible mmap layout support
Jiri Kosinacc503c12008-01-30 13:31:07 +01003 *
4 * Based on code by Ingo Molnar and Andi Kleen, copyrighted
5 * as follows:
6 *
Ingo Molnar8f47e162009-01-31 02:03:42 +01007 * Copyright 2003-2009 Red Hat Inc.
Jiri Kosinacc503c12008-01-30 13:31:07 +01008 * All Rights Reserved.
9 * Copyright 2005 Andi Kleen, SUSE Labs.
10 * Copyright 2007 Jiri Kosina, SUSE Labs.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Andi Kleen88172102006-01-17 07:03:38 +010025 */
Jiri Kosinacc503c12008-01-30 13:31:07 +010026
27#include <linux/personality.h>
Andi Kleen88172102006-01-17 07:03:38 +010028#include <linux/mm.h>
Andi Kleen88172102006-01-17 07:03:38 +010029#include <linux/random.h>
Jiri Kosinacc503c12008-01-30 13:31:07 +010030#include <linux/limits.h>
31#include <linux/sched.h>
Michal Hocko80938332009-09-08 11:01:55 +020032#include <asm/elf.h>
33
Jan-Simon Möllercc995352014-09-05 16:16:45 -070034struct va_alignment __read_mostly va_align = {
Borislav Petkov9387f772011-08-06 14:31:38 +020035 .flags = -1,
36};
37
Hector Marco-Gisbert4e7c22d2015-02-14 09:33:50 -080038static unsigned long stack_maxrandom_size(void)
Michal Hocko80938332009-09-08 11:01:55 +020039{
Hector Marco-Gisbert4e7c22d2015-02-14 09:33:50 -080040 unsigned long max = 0;
Michal Hocko80938332009-09-08 11:01:55 +020041 if ((current->flags & PF_RANDOMIZE) &&
42 !(current->personality & ADDR_NO_RANDOMIZE)) {
Hector Marco-Gisbert4e7c22d2015-02-14 09:33:50 -080043 max = ((-1UL) & STACK_RND_MASK) << PAGE_SHIFT;
Michal Hocko80938332009-09-08 11:01:55 +020044 }
45
46 return max;
47}
48
Jiri Kosinacc503c12008-01-30 13:31:07 +010049/*
50 * Top of mmap area (just below the process stack).
51 *
Michal Hocko80938332009-09-08 11:01:55 +020052 * Leave an at least ~128 MB hole with possible stack randomization.
Jiri Kosinacc503c12008-01-30 13:31:07 +010053 */
Michal Hocko80938332009-09-08 11:01:55 +020054#define MIN_GAP (128*1024*1024UL + stack_maxrandom_size())
Jiri Kosinacc503c12008-01-30 13:31:07 +010055#define MAX_GAP (TASK_SIZE/6*5)
Andi Kleen88172102006-01-17 07:03:38 +010056
Andrew Morton954683a2008-01-30 13:31:07 +010057static int mmap_is_legacy(void)
Jiri Kosinacc503c12008-01-30 13:31:07 +010058{
59 if (current->personality & ADDR_COMPAT_LAYOUT)
60 return 1;
61
Jiri Slaby2854e722010-01-27 17:32:22 +010062 if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
Jiri Kosinacc503c12008-01-30 13:31:07 +010063 return 1;
64
65 return sysctl_legacy_va_layout;
66}
67
Kees Cook2b68f6c2015-04-14 15:48:00 -070068unsigned long arch_mmap_rnd(void)
Harvey Harrison675a0812008-01-30 13:31:10 +010069{
Kees Cook82168142015-04-14 15:47:45 -070070 unsigned long rnd;
Harvey Harrison675a0812008-01-30 13:31:10 +010071
Kees Cook82168142015-04-14 15:47:45 -070072 if (mmap_is_ia32())
Daniel Cashman9e08f572016-01-14 15:20:06 -080073#ifdef CONFIG_COMPAT
Daniel Cashman5ef11c32016-02-26 15:19:37 -080074 rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
Daniel Cashman9e08f572016-01-14 15:20:06 -080075#else
Daniel Cashman5ef11c32016-02-26 15:19:37 -080076 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
Daniel Cashman9e08f572016-01-14 15:20:06 -080077#endif
Kees Cook82168142015-04-14 15:47:45 -070078 else
Daniel Cashman5ef11c32016-02-26 15:19:37 -080079 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
Kees Cook82168142015-04-14 15:47:45 -070080
Harvey Harrison675a0812008-01-30 13:31:10 +010081 return rnd << PAGE_SHIFT;
82}
83
Kees Cook82168142015-04-14 15:47:45 -070084static unsigned long mmap_base(unsigned long rnd)
Harvey Harrison675a0812008-01-30 13:31:10 +010085{
Jiri Slaby2854e722010-01-27 17:32:22 +010086 unsigned long gap = rlimit(RLIMIT_STACK);
Harvey Harrison675a0812008-01-30 13:31:10 +010087
88 if (gap < MIN_GAP)
89 gap = MIN_GAP;
90 else if (gap > MAX_GAP)
91 gap = MAX_GAP;
92
Kees Cook82168142015-04-14 15:47:45 -070093 return PAGE_ALIGN(TASK_SIZE - gap - rnd);
Harvey Harrison675a0812008-01-30 13:31:10 +010094}
95
96/*
Jiri Kosinacc503c12008-01-30 13:31:07 +010097 * This function, called very early during the creation of a new
98 * process VM image, sets up which VM layout function to use:
99 */
100void arch_pick_mmap_layout(struct mm_struct *mm)
101{
Kees Cook82168142015-04-14 15:47:45 -0700102 unsigned long random_factor = 0UL;
103
104 if (current->flags & PF_RANDOMIZE)
Kees Cook2b68f6c2015-04-14 15:48:00 -0700105 random_factor = arch_mmap_rnd();
Kees Cook82168142015-04-14 15:47:45 -0700106
Hector Marco-Gisbert8b8addf2016-03-10 20:51:00 +0100107 mm->mmap_legacy_base = TASK_UNMAPPED_BASE + random_factor;
Radu Caragea41aacc12013-08-21 20:55:59 +0300108
Harvey Harrison675a0812008-01-30 13:31:10 +0100109 if (mmap_is_legacy()) {
Radu Caragea41aacc12013-08-21 20:55:59 +0300110 mm->mmap_base = mm->mmap_legacy_base;
Jiri Kosinacc503c12008-01-30 13:31:07 +0100111 mm->get_unmapped_area = arch_get_unmapped_area;
Jiri Kosinacc503c12008-01-30 13:31:07 +0100112 } else {
Kees Cook82168142015-04-14 15:47:45 -0700113 mm->mmap_base = mmap_base(random_factor);
Jiri Kosinacc503c12008-01-30 13:31:07 +0100114 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
Jiri Kosinacc503c12008-01-30 13:31:07 +0100115 }
Jiri Kosinacc503c12008-01-30 13:31:07 +0100116}
Kirill A. Shutemova89652762015-07-20 14:29:58 -0700117
118const char *arch_vma_name(struct vm_area_struct *vma)
119{
120 if (vma->vm_flags & VM_MPX)
121 return "[mpx]";
122 return NULL;
123}