blob: 6e552af08c76a61027c023f7305133ee1b4efe02 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * flexible mmap layout support
3 *
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
5 * All Rights Reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 *
22 * Started by Ingo Molnar <mingo@elte.hu>
23 */
24
25#include <linux/personality.h>
26#include <linux/mm.h>
Martin Schwidefsky638ad342011-10-30 15:17:13 +010027#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
Heiko Carstensdf1ca532011-01-12 09:55:27 +010029#include <linux/random.h>
Heiko Carstens048cd4e2012-02-27 10:01:52 +010030#include <linux/compat.h>
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010031#include <linux/security.h>
Martin Schwidefsky6252d702008-02-09 18:24:37 +010032#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010034unsigned long mmap_rnd_mask;
Heiko Carstens3ddb1b72015-03-16 12:44:10 +010035static unsigned long mmap_align_mask;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010036
Heiko Carstens9046e402011-01-12 09:55:22 +010037static unsigned long stack_maxrandom_size(void)
38{
39 if (!(current->flags & PF_RANDOMIZE))
40 return 0;
41 if (current->personality & ADDR_NO_RANDOMIZE)
42 return 0;
43 return STACK_RND_MASK << PAGE_SHIFT;
44}
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 * Top of mmap area (just below the process stack).
48 *
Heiko Carstens9e78a132011-01-12 09:55:23 +010049 * Leave at least a ~32 MB hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
Heiko Carstens9e78a132011-01-12 09:55:23 +010051#define MIN_GAP (32*1024*1024)
Martin Schwidefskyf481bfa2009-03-18 13:27:36 +010052#define MAX_GAP (STACK_TOP/6*5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Heiko Carstens1060f622011-01-12 09:55:26 +010054static inline int mmap_is_legacy(void)
55{
56 if (current->personality & ADDR_COMPAT_LAYOUT)
57 return 1;
58 if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
59 return 1;
60 return sysctl_legacy_va_layout;
61}
62
Kees Cook2b68f6c2015-04-14 15:48:00 -070063unsigned long arch_mmap_rnd(void)
Heiko Carstensdf1ca532011-01-12 09:55:27 +010064{
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010065 if (is_32bit_task())
66 return (get_random_int() & 0x7ff) << PAGE_SHIFT;
67 else
68 return (get_random_int() & mmap_rnd_mask) << PAGE_SHIFT;
Heiko Carstensdf1ca532011-01-12 09:55:27 +010069}
70
Kees Cook8e89a352015-04-14 15:47:57 -070071static unsigned long mmap_base_legacy(unsigned long rnd)
Heiko Carstens7aba8422013-11-12 15:07:55 -080072{
Kees Cook8e89a352015-04-14 15:47:57 -070073 return TASK_UNMAPPED_BASE + rnd;
Heiko Carstens7aba8422013-11-12 15:07:55 -080074}
75
Kees Cook8e89a352015-04-14 15:47:57 -070076static inline unsigned long mmap_base(unsigned long rnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Jiri Slabya58c26b2010-01-13 20:44:33 +010078 unsigned long gap = rlimit(RLIMIT_STACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 if (gap < MIN_GAP)
81 gap = MIN_GAP;
82 else if (gap > MAX_GAP)
83 gap = MAX_GAP;
Heiko Carstensdf1ca532011-01-12 09:55:27 +010084 gap &= PAGE_MASK;
Kees Cook8e89a352015-04-14 15:47:57 -070085 return STACK_TOP - stack_maxrandom_size() - rnd - gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010088unsigned long
89arch_get_unmapped_area(struct file *filp, unsigned long addr,
90 unsigned long len, unsigned long pgoff, unsigned long flags)
91{
92 struct mm_struct *mm = current->mm;
93 struct vm_area_struct *vma;
94 struct vm_unmapped_area_info info;
95 int do_color_align;
96
97 if (len > TASK_SIZE - mmap_min_addr)
98 return -ENOMEM;
99
100 if (flags & MAP_FIXED)
101 return addr;
102
103 if (addr) {
104 addr = PAGE_ALIGN(addr);
105 vma = find_vma(mm, addr);
106 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
107 (!vma || addr + len <= vma->vm_start))
108 return addr;
109 }
110
111 do_color_align = 0;
112 if (filp || (flags & MAP_SHARED))
113 do_color_align = !is_32bit_task();
114
115 info.flags = 0;
116 info.length = len;
117 info.low_limit = mm->mmap_base;
118 info.high_limit = TASK_SIZE;
119 info.align_mask = do_color_align ? (mmap_align_mask << PAGE_SHIFT) : 0;
120 info.align_offset = pgoff << PAGE_SHIFT;
121 return vm_unmapped_area(&info);
122}
123
124unsigned long
125arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
126 const unsigned long len, const unsigned long pgoff,
127 const unsigned long flags)
128{
129 struct vm_area_struct *vma;
130 struct mm_struct *mm = current->mm;
131 unsigned long addr = addr0;
132 struct vm_unmapped_area_info info;
133 int do_color_align;
134
135 /* requested length too big for entire address space */
136 if (len > TASK_SIZE - mmap_min_addr)
137 return -ENOMEM;
138
139 if (flags & MAP_FIXED)
140 return addr;
141
142 /* requesting a specific address */
143 if (addr) {
144 addr = PAGE_ALIGN(addr);
145 vma = find_vma(mm, addr);
146 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
147 (!vma || addr + len <= vma->vm_start))
148 return addr;
149 }
150
151 do_color_align = 0;
152 if (filp || (flags & MAP_SHARED))
153 do_color_align = !is_32bit_task();
154
155 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
156 info.length = len;
157 info.low_limit = max(PAGE_SIZE, mmap_min_addr);
158 info.high_limit = mm->mmap_base;
159 info.align_mask = do_color_align ? (mmap_align_mask << PAGE_SHIFT) : 0;
160 info.align_offset = pgoff << PAGE_SHIFT;
161 addr = vm_unmapped_area(&info);
162
163 /*
164 * A failed mmap() very likely causes application failure,
165 * so fall back to the bottom-up function here. This scenario
166 * can happen with large stack limits and large mmap()
167 * allocations.
168 */
169 if (addr & ~PAGE_MASK) {
170 VM_BUG_ON(addr != -ENOMEM);
171 info.flags = 0;
172 info.low_limit = TASK_UNMAPPED_BASE;
173 info.high_limit = TASK_SIZE;
174 addr = vm_unmapped_area(&info);
175 }
176
177 return addr;
178}
179
Hendrik Brueckner486c0a02013-02-11 14:29:49 +0100180int s390_mmap_check(unsigned long addr, unsigned long len, unsigned long flags)
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100181{
Hendrik Brueckner486c0a02013-02-11 14:29:49 +0100182 if (is_compat_task() || (TASK_SIZE >= (1UL << 53)))
183 return 0;
184 if (!(flags & MAP_FIXED))
185 addr = 0;
Martin Schwidefsky10607862013-10-28 14:48:30 +0100186 if ((addr + len) >= TASK_SIZE)
187 return crst_table_upgrade(current->mm, 1UL << 53);
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100188 return 0;
189}
190
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100191static unsigned long
192s390_get_unmapped_area(struct file *filp, unsigned long addr,
193 unsigned long len, unsigned long pgoff, unsigned long flags)
194{
195 struct mm_struct *mm = current->mm;
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100196 unsigned long area;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100197 int rc;
198
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100199 area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
200 if (!(area & ~PAGE_MASK))
201 return area;
Heiko Carstens77575912009-06-12 10:26:25 +0200202 if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) {
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100203 /* Upgrade the page table to 4 levels and retry. */
204 rc = crst_table_upgrade(mm, 1UL << 53);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100205 if (rc)
206 return (unsigned long) rc;
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100207 area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100208 }
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100209 return area;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100210}
211
212static unsigned long
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100213s390_get_unmapped_area_topdown(struct file *filp, const unsigned long addr,
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100214 const unsigned long len, const unsigned long pgoff,
215 const unsigned long flags)
216{
217 struct mm_struct *mm = current->mm;
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100218 unsigned long area;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100219 int rc;
220
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100221 area = arch_get_unmapped_area_topdown(filp, addr, len, pgoff, flags);
222 if (!(area & ~PAGE_MASK))
223 return area;
Heiko Carstens77575912009-06-12 10:26:25 +0200224 if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) {
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100225 /* Upgrade the page table to 4 levels and retry. */
226 rc = crst_table_upgrade(mm, 1UL << 53);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100227 if (rc)
228 return (unsigned long) rc;
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100229 area = arch_get_unmapped_area_topdown(filp, addr, len,
230 pgoff, flags);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100231 }
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100232 return area;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100233}
234/*
235 * This function, called very early during the creation of a new
236 * process VM image, sets up which VM layout function to use:
237 */
238void arch_pick_mmap_layout(struct mm_struct *mm)
239{
Kees Cook8e89a352015-04-14 15:47:57 -0700240 unsigned long random_factor = 0UL;
241
242 if (current->flags & PF_RANDOMIZE)
Kees Cook2b68f6c2015-04-14 15:48:00 -0700243 random_factor = arch_mmap_rnd();
Kees Cook8e89a352015-04-14 15:47:57 -0700244
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100245 /*
246 * Fall back to the standard layout if the personality
247 * bit is set, or if the expected stack growth is unlimited:
248 */
249 if (mmap_is_legacy()) {
Kees Cook8e89a352015-04-14 15:47:57 -0700250 mm->mmap_base = mmap_base_legacy(random_factor);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100251 mm->get_unmapped_area = s390_get_unmapped_area;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100252 } else {
Kees Cook8e89a352015-04-14 15:47:57 -0700253 mm->mmap_base = mmap_base(random_factor);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100254 mm->get_unmapped_area = s390_get_unmapped_area_topdown;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100255 }
256}
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100257
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100258static int __init setup_mmap_rnd(void)
259{
260 struct cpuid cpu_id;
261
262 get_cpu_id(&cpu_id);
263 switch (cpu_id.machine) {
264 case 0x9672:
265 case 0x2064:
266 case 0x2066:
267 case 0x2084:
268 case 0x2086:
269 case 0x2094:
270 case 0x2096:
271 case 0x2097:
272 case 0x2098:
273 case 0x2817:
274 case 0x2818:
275 case 0x2827:
276 case 0x2828:
277 mmap_rnd_mask = 0x7ffUL;
278 mmap_align_mask = 0UL;
279 break;
280 case 0x2964: /* z13 */
281 default:
282 mmap_rnd_mask = 0x3ff80UL;
283 mmap_align_mask = 0x7fUL;
284 break;
285 }
286 return 0;
287}
288early_initcall(setup_mmap_rnd);