blob: 6d476a7b56112bf0fe985a39bd0ea8585cbd60b6 [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>
Anton Blanchard9f14c422009-02-22 01:50:01 +000027#include <linux/random.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010028#include <linux/sched/signal.h>
Ingo Molnar01042602017-02-08 18:51:31 +010029#include <linux/sched/mm.h>
Daniel Axtens7f92bc52016-01-06 11:45:51 +110030#include <linux/elf-randomize.h>
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +100031#include <linux/security.h>
32#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/*
35 * Top of mmap area (just below the process stack).
36 *
Rik van Riel0a782dc2017-07-12 14:36:39 -070037 * Leave at least a ~128 MB hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
Rik van Riel0a782dc2017-07-12 14:36:39 -070039#define MIN_GAP (128*1024*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define MAX_GAP (TASK_SIZE/6*5)
41
Anton Blanchard13a2cb32009-02-22 01:50:00 +000042static inline int mmap_is_legacy(void)
43{
44 if (current->personality & ADDR_COMPAT_LAYOUT)
45 return 1;
46
Jiri Slaby4bf936b2010-01-06 05:24:31 +000047 if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
Anton Blanchard13a2cb32009-02-22 01:50:00 +000048 return 1;
49
50 return sysctl_legacy_va_layout;
51}
52
Kees Cook2b68f6c2015-04-14 15:48:00 -070053unsigned long arch_mmap_rnd(void)
Anton Blanchard9f14c422009-02-22 01:50:01 +000054{
Michael Ellerman9fea59b2017-04-21 00:36:20 +100055 unsigned long shift, rnd;
Anton Blanchard9f14c422009-02-22 01:50:01 +000056
Michael Ellerman9fea59b2017-04-21 00:36:20 +100057 shift = mmap_rnd_bits;
58#ifdef CONFIG_COMPAT
Kees Cooked632272015-04-14 15:47:54 -070059 if (is_32bit_task())
Michael Ellerman9fea59b2017-04-21 00:36:20 +100060 shift = mmap_rnd_compat_bits;
61#endif
Michael Ellermanb4099462017-04-25 20:49:24 +100062 rnd = get_random_long() % (1ul << shift);
Kees Cooked632272015-04-14 15:47:54 -070063
Dan McGeefa8cbaa2011-10-17 13:05:23 +000064 return rnd << PAGE_SHIFT;
Anton Blanchard9f14c422009-02-22 01:50:01 +000065}
66
Rik van Riel0a782dc2017-07-12 14:36:39 -070067static inline unsigned long stack_maxrandom_size(void)
68{
69 if (!(current->flags & PF_RANDOMIZE))
70 return 0;
71
72 /* 8MB for 32bit, 1GB for 64bit */
73 if (is_32bit_task())
74 return (1<<23);
75 else
76 return (1<<30);
77}
78
Kees Cooked632272015-04-14 15:47:54 -070079static inline unsigned long mmap_base(unsigned long rnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Jiri Slaby4bf936b2010-01-06 05:24:31 +000081 unsigned long gap = rlimit(RLIMIT_STACK);
Rik van Riel0a782dc2017-07-12 14:36:39 -070082 unsigned long pad = stack_maxrandom_size() + stack_guard_gap;
83
84 /* Values close to RLIM_INFINITY can overflow. */
85 if (gap + pad > gap)
86 gap += pad;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 if (gap < MIN_GAP)
89 gap = MIN_GAP;
90 else if (gap > MAX_GAP)
91 gap = MAX_GAP;
92
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +053093 return PAGE_ALIGN(DEFAULT_MAP_WINDOW - gap - rnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +100096#ifdef CONFIG_PPC_RADIX_MMU
97/*
98 * Same function as generic code used only for radix, because we don't need to overload
99 * the generic one. But we will have to duplicate, because hash select
100 * HAVE_ARCH_UNMAPPED_AREA
101 */
102static unsigned long
103radix__arch_get_unmapped_area(struct file *filp, unsigned long addr,
104 unsigned long len, unsigned long pgoff,
105 unsigned long flags)
106{
107 struct mm_struct *mm = current->mm;
108 struct vm_area_struct *vma;
Nicholas Pigginde9c3552017-11-10 04:27:39 +1100109 int fixed = (flags & MAP_FIXED);
110 unsigned long high_limit;
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000111 struct vm_unmapped_area_info info;
112
Nicholas Pigginde9c3552017-11-10 04:27:39 +1100113 high_limit = DEFAULT_MAP_WINDOW;
114 if (addr >= high_limit || (fixed && (addr + len > high_limit)))
115 high_limit = TASK_SIZE;
116
117 if (len > high_limit)
118 return -ENOMEM;
119 if (fixed) {
120 if (addr > high_limit - len)
121 return -ENOMEM;
122 }
123
Aneesh Kumar K.V321f7d22017-04-18 12:31:27 +0530124 if (unlikely(addr > mm->context.addr_limit &&
125 mm->context.addr_limit != TASK_SIZE))
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530126 mm->context.addr_limit = TASK_SIZE;
127
Nicholas Pigginde9c3552017-11-10 04:27:39 +1100128 if (fixed)
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000129 return addr;
130
131 if (addr) {
132 addr = PAGE_ALIGN(addr);
133 vma = find_vma(mm, addr);
Nicholas Pigginde9c3552017-11-10 04:27:39 +1100134 if (high_limit - len >= addr && addr >= mmap_min_addr &&
Hugh Dickins1be71072017-06-19 04:03:24 -0700135 (!vma || addr + len <= vm_start_gap(vma)))
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000136 return addr;
137 }
138
139 info.flags = 0;
140 info.length = len;
141 info.low_limit = mm->mmap_base;
Nicholas Pigginde9c3552017-11-10 04:27:39 +1100142 info.high_limit = high_limit;
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000143 info.align_mask = 0;
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530144
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000145 return vm_unmapped_area(&info);
146}
147
148static unsigned long
149radix__arch_get_unmapped_area_topdown(struct file *filp,
150 const unsigned long addr0,
151 const unsigned long len,
152 const unsigned long pgoff,
153 const unsigned long flags)
154{
155 struct vm_area_struct *vma;
156 struct mm_struct *mm = current->mm;
157 unsigned long addr = addr0;
Nicholas Pigginde9c3552017-11-10 04:27:39 +1100158 int fixed = (flags & MAP_FIXED);
159 unsigned long high_limit;
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000160 struct vm_unmapped_area_info info;
161
Nicholas Pigginde9c3552017-11-10 04:27:39 +1100162 high_limit = DEFAULT_MAP_WINDOW;
163 if (addr >= high_limit || (fixed && (addr + len > high_limit)))
164 high_limit = TASK_SIZE;
165
166 if (len > high_limit)
167 return -ENOMEM;
168 if (fixed) {
169 if (addr > high_limit - len)
170 return -ENOMEM;
171 }
172
Aneesh Kumar K.V321f7d22017-04-18 12:31:27 +0530173 if (unlikely(addr > mm->context.addr_limit &&
174 mm->context.addr_limit != TASK_SIZE))
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530175 mm->context.addr_limit = TASK_SIZE;
176
Nicholas Pigginde9c3552017-11-10 04:27:39 +1100177 if (fixed)
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000178 return addr;
179
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000180 if (addr) {
181 addr = PAGE_ALIGN(addr);
182 vma = find_vma(mm, addr);
Nicholas Pigginde9c3552017-11-10 04:27:39 +1100183 if (high_limit - len >= addr && addr >= mmap_min_addr &&
184 (!vma || addr + len <= vm_start_gap(vma)))
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000185 return addr;
186 }
187
188 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
189 info.length = len;
190 info.low_limit = max(PAGE_SIZE, mmap_min_addr);
Nicholas Pigginde9c3552017-11-10 04:27:39 +1100191 info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW);
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000192 info.align_mask = 0;
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530193
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000194 addr = vm_unmapped_area(&info);
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530195 if (!(addr & ~PAGE_MASK))
196 return addr;
197 VM_BUG_ON(addr != -ENOMEM);
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000198
199 /*
200 * A failed mmap() very likely causes application failure,
201 * so fall back to the bottom-up function here. This scenario
202 * can happen with large stack limits and large mmap()
203 * allocations.
204 */
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530205 return radix__arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000206}
207
208static void radix__arch_pick_mmap_layout(struct mm_struct *mm,
209 unsigned long random_factor)
210{
211 if (mmap_is_legacy()) {
212 mm->mmap_base = TASK_UNMAPPED_BASE;
213 mm->get_unmapped_area = radix__arch_get_unmapped_area;
214 } else {
215 mm->mmap_base = mmap_base(random_factor);
216 mm->get_unmapped_area = radix__arch_get_unmapped_area_topdown;
217 }
218}
219#else
220/* dummy */
221extern void radix__arch_pick_mmap_layout(struct mm_struct *mm,
222 unsigned long random_factor);
223#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
225 * This function, called very early during the creation of a new
226 * process VM image, sets up which VM layout function to use:
227 */
228void arch_pick_mmap_layout(struct mm_struct *mm)
229{
Kees Cooked632272015-04-14 15:47:54 -0700230 unsigned long random_factor = 0UL;
231
232 if (current->flags & PF_RANDOMIZE)
Kees Cook2b68f6c2015-04-14 15:48:00 -0700233 random_factor = arch_mmap_rnd();
Kees Cooked632272015-04-14 15:47:54 -0700234
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000235 if (radix_enabled())
236 return radix__arch_pick_mmap_layout(mm, random_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /*
238 * Fall back to the standard layout if the personality
239 * bit is set, or if the expected stack growth is unlimited:
240 */
241 if (mmap_is_legacy()) {
242 mm->mmap_base = TASK_UNMAPPED_BASE;
243 mm->get_unmapped_area = arch_get_unmapped_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 } else {
Kees Cooked632272015-04-14 15:47:54 -0700245 mm->mmap_base = mmap_base(random_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248}