]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * 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> | |
9f14c42d | 27 | #include <linux/random.h> |
3f07c014 | 28 | #include <linux/sched/signal.h> |
01042607 | 29 | #include <linux/sched/mm.h> |
7f92bc56 | 30 | #include <linux/elf-randomize.h> |
7a0eedee AK |
31 | #include <linux/security.h> |
32 | #include <linux/mman.h> | |
1da177e4 LT |
33 | |
34 | /* | |
35 | * Top of mmap area (just below the process stack). | |
36 | * | |
0a782dc3 | 37 | * Leave at least a ~128 MB hole. |
1da177e4 | 38 | */ |
0a782dc3 | 39 | #define MIN_GAP (128*1024*1024) |
1da177e4 LT |
40 | #define MAX_GAP (TASK_SIZE/6*5) |
41 | ||
13a2cb36 AB |
42 | static inline int mmap_is_legacy(void) |
43 | { | |
44 | if (current->personality & ADDR_COMPAT_LAYOUT) | |
45 | return 1; | |
46 | ||
4bf936b9 | 47 | if (rlimit(RLIMIT_STACK) == RLIM_INFINITY) |
13a2cb36 AB |
48 | return 1; |
49 | ||
50 | return sysctl_legacy_va_layout; | |
51 | } | |
52 | ||
2b68f6ca | 53 | unsigned long arch_mmap_rnd(void) |
9f14c42d | 54 | { |
9fea59bd | 55 | unsigned long shift, rnd; |
ed632274 | 56 | |
9fea59bd ME |
57 | shift = mmap_rnd_bits; |
58 | #ifdef CONFIG_COMPAT | |
ed632274 | 59 | if (is_32bit_task()) |
9fea59bd ME |
60 | shift = mmap_rnd_compat_bits; |
61 | #endif | |
b409946b | 62 | rnd = get_random_long() % (1ul << shift); |
9f14c42d | 63 | |
fa8cbaaf | 64 | return rnd << PAGE_SHIFT; |
9f14c42d AB |
65 | } |
66 | ||
0a782dc3 RR |
67 | static 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 | ||
ed632274 | 79 | static inline unsigned long mmap_base(unsigned long rnd) |
1da177e4 | 80 | { |
4bf936b9 | 81 | unsigned long gap = rlimit(RLIMIT_STACK); |
0a782dc3 RR |
82 | 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; | |
1da177e4 LT |
87 | |
88 | if (gap < MIN_GAP) | |
89 | gap = MIN_GAP; | |
90 | else if (gap > MAX_GAP) | |
91 | gap = MAX_GAP; | |
92 | ||
f4ea6dcb | 93 | return PAGE_ALIGN(DEFAULT_MAP_WINDOW - gap - rnd); |
1da177e4 LT |
94 | } |
95 | ||
7a0eedee AK |
96 | #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 | */ | |
102 | static unsigned long | |
103 | radix__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; | |
109 | struct vm_unmapped_area_info info; | |
110 | ||
321f7d29 AK |
111 | if (unlikely(addr > mm->context.addr_limit && |
112 | mm->context.addr_limit != TASK_SIZE)) | |
f4ea6dcb AK |
113 | mm->context.addr_limit = TASK_SIZE; |
114 | ||
be77e999 | 115 | if (len > mm->task_size - mmap_min_addr) |
7a0eedee AK |
116 | return -ENOMEM; |
117 | ||
118 | if (flags & MAP_FIXED) | |
119 | return addr; | |
120 | ||
121 | if (addr) { | |
122 | addr = PAGE_ALIGN(addr); | |
123 | vma = find_vma(mm, addr); | |
be77e999 | 124 | if (mm->task_size - len >= addr && addr >= mmap_min_addr && |
1be7107f | 125 | (!vma || addr + len <= vm_start_gap(vma))) |
7a0eedee AK |
126 | return addr; |
127 | } | |
128 | ||
129 | info.flags = 0; | |
130 | info.length = len; | |
131 | info.low_limit = mm->mmap_base; | |
7a0eedee | 132 | info.align_mask = 0; |
f4ea6dcb AK |
133 | |
134 | if (unlikely(addr > DEFAULT_MAP_WINDOW)) | |
135 | info.high_limit = mm->context.addr_limit; | |
136 | else | |
137 | info.high_limit = DEFAULT_MAP_WINDOW; | |
138 | ||
7a0eedee AK |
139 | return vm_unmapped_area(&info); |
140 | } | |
141 | ||
142 | static unsigned long | |
143 | radix__arch_get_unmapped_area_topdown(struct file *filp, | |
144 | const unsigned long addr0, | |
145 | const unsigned long len, | |
146 | const unsigned long pgoff, | |
147 | const unsigned long flags) | |
148 | { | |
149 | struct vm_area_struct *vma; | |
150 | struct mm_struct *mm = current->mm; | |
151 | unsigned long addr = addr0; | |
152 | struct vm_unmapped_area_info info; | |
153 | ||
321f7d29 AK |
154 | if (unlikely(addr > mm->context.addr_limit && |
155 | mm->context.addr_limit != TASK_SIZE)) | |
f4ea6dcb AK |
156 | mm->context.addr_limit = TASK_SIZE; |
157 | ||
7a0eedee | 158 | /* requested length too big for entire address space */ |
be77e999 | 159 | if (len > mm->task_size - mmap_min_addr) |
7a0eedee AK |
160 | return -ENOMEM; |
161 | ||
162 | if (flags & MAP_FIXED) | |
163 | return addr; | |
164 | ||
165 | /* requesting a specific address */ | |
166 | if (addr) { | |
167 | addr = PAGE_ALIGN(addr); | |
168 | vma = find_vma(mm, addr); | |
be77e999 | 169 | if (mm->task_size - len >= addr && addr >= mmap_min_addr && |
1be7107f | 170 | (!vma || addr + len <= vm_start_gap(vma))) |
7a0eedee AK |
171 | return addr; |
172 | } | |
173 | ||
174 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; | |
175 | info.length = len; | |
176 | info.low_limit = max(PAGE_SIZE, mmap_min_addr); | |
177 | info.high_limit = mm->mmap_base; | |
178 | info.align_mask = 0; | |
f4ea6dcb AK |
179 | |
180 | if (addr > DEFAULT_MAP_WINDOW) | |
181 | info.high_limit += mm->context.addr_limit - DEFAULT_MAP_WINDOW; | |
182 | ||
7a0eedee | 183 | addr = vm_unmapped_area(&info); |
f4ea6dcb AK |
184 | if (!(addr & ~PAGE_MASK)) |
185 | return addr; | |
186 | VM_BUG_ON(addr != -ENOMEM); | |
7a0eedee AK |
187 | |
188 | /* | |
189 | * A failed mmap() very likely causes application failure, | |
190 | * so fall back to the bottom-up function here. This scenario | |
191 | * can happen with large stack limits and large mmap() | |
192 | * allocations. | |
193 | */ | |
f4ea6dcb | 194 | return radix__arch_get_unmapped_area(filp, addr0, len, pgoff, flags); |
7a0eedee AK |
195 | } |
196 | ||
197 | static void radix__arch_pick_mmap_layout(struct mm_struct *mm, | |
198 | unsigned long random_factor) | |
199 | { | |
200 | if (mmap_is_legacy()) { | |
201 | mm->mmap_base = TASK_UNMAPPED_BASE; | |
202 | mm->get_unmapped_area = radix__arch_get_unmapped_area; | |
203 | } else { | |
204 | mm->mmap_base = mmap_base(random_factor); | |
205 | mm->get_unmapped_area = radix__arch_get_unmapped_area_topdown; | |
206 | } | |
207 | } | |
208 | #else | |
209 | /* dummy */ | |
210 | extern void radix__arch_pick_mmap_layout(struct mm_struct *mm, | |
211 | unsigned long random_factor); | |
212 | #endif | |
1da177e4 LT |
213 | /* |
214 | * This function, called very early during the creation of a new | |
215 | * process VM image, sets up which VM layout function to use: | |
216 | */ | |
217 | void arch_pick_mmap_layout(struct mm_struct *mm) | |
218 | { | |
ed632274 KC |
219 | unsigned long random_factor = 0UL; |
220 | ||
221 | if (current->flags & PF_RANDOMIZE) | |
2b68f6ca | 222 | random_factor = arch_mmap_rnd(); |
ed632274 | 223 | |
7a0eedee AK |
224 | if (radix_enabled()) |
225 | return radix__arch_pick_mmap_layout(mm, random_factor); | |
1da177e4 LT |
226 | /* |
227 | * Fall back to the standard layout if the personality | |
228 | * bit is set, or if the expected stack growth is unlimited: | |
229 | */ | |
230 | if (mmap_is_legacy()) { | |
231 | mm->mmap_base = TASK_UNMAPPED_BASE; | |
232 | mm->get_unmapped_area = arch_get_unmapped_area; | |
1da177e4 | 233 | } else { |
ed632274 | 234 | mm->mmap_base = mmap_base(random_factor); |
1da177e4 | 235 | mm->get_unmapped_area = arch_get_unmapped_area_topdown; |
1da177e4 LT |
236 | } |
237 | } |