blob: a94504d99c47e8eabb8ff1531bb556e505e5b8f4 [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;
35unsigned long mmap_align_mask;
36
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
180unsigned long randomize_et_dyn(void)
181{
182 unsigned long base;
183
Martin Schwidefsky4ba28152015-02-12 14:17:52 +0100184 base = STACK_TOP / 3 * 2;
185 if (!is_32bit_task())
186 /* Align to 4GB */
187 base &= ~((1UL << 32) - 1);
Kees Cook8e89a352015-04-14 15:47:57 -0700188
189 if (current->flags & PF_RANDOMIZE)
Kees Cook2b68f6c2015-04-14 15:48:00 -0700190 base += arch_mmap_rnd();
Kees Cook8e89a352015-04-14 15:47:57 -0700191
192 return base;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100193}
194
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100195#ifndef CONFIG_64BIT
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/*
198 * This function, called very early during the creation of a new
199 * process VM image, sets up which VM layout function to use:
200 */
201void arch_pick_mmap_layout(struct mm_struct *mm)
202{
Kees Cook8e89a352015-04-14 15:47:57 -0700203 unsigned long random_factor = 0UL;
204
205 if (current->flags & PF_RANDOMIZE)
Kees Cook2b68f6c2015-04-14 15:48:00 -0700206 random_factor = arch_mmap_rnd();
Kees Cook8e89a352015-04-14 15:47:57 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 /*
209 * Fall back to the standard layout if the personality
210 * bit is set, or if the expected stack growth is unlimited:
211 */
212 if (mmap_is_legacy()) {
Kees Cook8e89a352015-04-14 15:47:57 -0700213 mm->mmap_base = mmap_base_legacy(random_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 mm->get_unmapped_area = arch_get_unmapped_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 } else {
Kees Cook8e89a352015-04-14 15:47:57 -0700216 mm->mmap_base = mmap_base(random_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100221#else
222
Hendrik Brueckner486c0a02013-02-11 14:29:49 +0100223int s390_mmap_check(unsigned long addr, unsigned long len, unsigned long flags)
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100224{
Hendrik Brueckner486c0a02013-02-11 14:29:49 +0100225 if (is_compat_task() || (TASK_SIZE >= (1UL << 53)))
226 return 0;
227 if (!(flags & MAP_FIXED))
228 addr = 0;
Martin Schwidefsky10607862013-10-28 14:48:30 +0100229 if ((addr + len) >= TASK_SIZE)
230 return crst_table_upgrade(current->mm, 1UL << 53);
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100231 return 0;
232}
233
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100234static unsigned long
235s390_get_unmapped_area(struct file *filp, unsigned long addr,
236 unsigned long len, unsigned long pgoff, unsigned long flags)
237{
238 struct mm_struct *mm = current->mm;
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100239 unsigned long area;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100240 int rc;
241
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100242 area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
243 if (!(area & ~PAGE_MASK))
244 return area;
Heiko Carstens77575912009-06-12 10:26:25 +0200245 if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) {
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100246 /* Upgrade the page table to 4 levels and retry. */
247 rc = crst_table_upgrade(mm, 1UL << 53);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100248 if (rc)
249 return (unsigned long) rc;
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100250 area = arch_get_unmapped_area(filp, addr, len, pgoff, flags);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100251 }
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100252 return area;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100253}
254
255static unsigned long
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100256s390_get_unmapped_area_topdown(struct file *filp, const unsigned long addr,
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100257 const unsigned long len, const unsigned long pgoff,
258 const unsigned long flags)
259{
260 struct mm_struct *mm = current->mm;
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100261 unsigned long area;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100262 int rc;
263
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100264 area = arch_get_unmapped_area_topdown(filp, addr, len, pgoff, flags);
265 if (!(area & ~PAGE_MASK))
266 return area;
Heiko Carstens77575912009-06-12 10:26:25 +0200267 if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) {
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100268 /* Upgrade the page table to 4 levels and retry. */
269 rc = crst_table_upgrade(mm, 1UL << 53);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100270 if (rc)
271 return (unsigned long) rc;
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100272 area = arch_get_unmapped_area_topdown(filp, addr, len,
273 pgoff, flags);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100274 }
Martin Schwidefsky0fb1d9b2009-03-18 13:27:37 +0100275 return area;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100276}
277/*
278 * This function, called very early during the creation of a new
279 * process VM image, sets up which VM layout function to use:
280 */
281void arch_pick_mmap_layout(struct mm_struct *mm)
282{
Kees Cook8e89a352015-04-14 15:47:57 -0700283 unsigned long random_factor = 0UL;
284
285 if (current->flags & PF_RANDOMIZE)
Kees Cook2b68f6c2015-04-14 15:48:00 -0700286 random_factor = arch_mmap_rnd();
Kees Cook8e89a352015-04-14 15:47:57 -0700287
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100288 /*
289 * Fall back to the standard layout if the personality
290 * bit is set, or if the expected stack growth is unlimited:
291 */
292 if (mmap_is_legacy()) {
Kees Cook8e89a352015-04-14 15:47:57 -0700293 mm->mmap_base = mmap_base_legacy(random_factor);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100294 mm->get_unmapped_area = s390_get_unmapped_area;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100295 } else {
Kees Cook8e89a352015-04-14 15:47:57 -0700296 mm->mmap_base = mmap_base(random_factor);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100297 mm->get_unmapped_area = s390_get_unmapped_area_topdown;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100298 }
299}
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100300
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100301static int __init setup_mmap_rnd(void)
302{
303 struct cpuid cpu_id;
304
305 get_cpu_id(&cpu_id);
306 switch (cpu_id.machine) {
307 case 0x9672:
308 case 0x2064:
309 case 0x2066:
310 case 0x2084:
311 case 0x2086:
312 case 0x2094:
313 case 0x2096:
314 case 0x2097:
315 case 0x2098:
316 case 0x2817:
317 case 0x2818:
318 case 0x2827:
319 case 0x2828:
320 mmap_rnd_mask = 0x7ffUL;
321 mmap_align_mask = 0UL;
322 break;
323 case 0x2964: /* z13 */
324 default:
325 mmap_rnd_mask = 0x3ff80UL;
326 mmap_align_mask = 0x7fUL;
327 break;
328 }
329 return 0;
330}
331early_initcall(setup_mmap_rnd);
332
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100333#endif