Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Martin Schwidefsky | 638ad34 | 2011-10-30 15:17:13 +0100 | [diff] [blame] | 27 | #include <linux/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
Heiko Carstens | df1ca53 | 2011-01-12 09:55:27 +0100 | [diff] [blame] | 29 | #include <linux/random.h> |
Heiko Carstens | 048cd4e | 2012-02-27 10:01:52 +0100 | [diff] [blame] | 30 | #include <linux/compat.h> |
Martin Schwidefsky | 1f6b83e | 2015-01-14 17:51:17 +0100 | [diff] [blame] | 31 | #include <linux/security.h> |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 32 | #include <asm/pgalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Martin Schwidefsky | 1f6b83e | 2015-01-14 17:51:17 +0100 | [diff] [blame] | 34 | unsigned long mmap_rnd_mask; |
Heiko Carstens | 3ddb1b7 | 2015-03-16 12:44:10 +0100 | [diff] [blame] | 35 | static unsigned long mmap_align_mask; |
Martin Schwidefsky | 1f6b83e | 2015-01-14 17:51:17 +0100 | [diff] [blame] | 36 | |
Heiko Carstens | 9046e40 | 2011-01-12 09:55:22 +0100 | [diff] [blame] | 37 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /* |
| 47 | * Top of mmap area (just below the process stack). |
| 48 | * |
Heiko Carstens | 9e78a13 | 2011-01-12 09:55:23 +0100 | [diff] [blame] | 49 | * Leave at least a ~32 MB hole. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | */ |
Heiko Carstens | 9e78a13 | 2011-01-12 09:55:23 +0100 | [diff] [blame] | 51 | #define MIN_GAP (32*1024*1024) |
Martin Schwidefsky | f481bfa | 2009-03-18 13:27:36 +0100 | [diff] [blame] | 52 | #define MAX_GAP (STACK_TOP/6*5) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Heiko Carstens | 1060f62 | 2011-01-12 09:55:26 +0100 | [diff] [blame] | 54 | static 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 Cook | 2b68f6c | 2015-04-14 15:48:00 -0700 | [diff] [blame] | 63 | unsigned long arch_mmap_rnd(void) |
Heiko Carstens | df1ca53 | 2011-01-12 09:55:27 +0100 | [diff] [blame] | 64 | { |
Martin Schwidefsky | 1f6b83e | 2015-01-14 17:51:17 +0100 | [diff] [blame] | 65 | 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 Carstens | df1ca53 | 2011-01-12 09:55:27 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Kees Cook | 8e89a35 | 2015-04-14 15:47:57 -0700 | [diff] [blame] | 71 | static unsigned long mmap_base_legacy(unsigned long rnd) |
Heiko Carstens | 7aba842 | 2013-11-12 15:07:55 -0800 | [diff] [blame] | 72 | { |
Kees Cook | 8e89a35 | 2015-04-14 15:47:57 -0700 | [diff] [blame] | 73 | return TASK_UNMAPPED_BASE + rnd; |
Heiko Carstens | 7aba842 | 2013-11-12 15:07:55 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Kees Cook | 8e89a35 | 2015-04-14 15:47:57 -0700 | [diff] [blame] | 76 | static inline unsigned long mmap_base(unsigned long rnd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Jiri Slaby | a58c26b | 2010-01-13 20:44:33 +0100 | [diff] [blame] | 78 | unsigned long gap = rlimit(RLIMIT_STACK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | if (gap < MIN_GAP) |
| 81 | gap = MIN_GAP; |
| 82 | else if (gap > MAX_GAP) |
| 83 | gap = MAX_GAP; |
Heiko Carstens | df1ca53 | 2011-01-12 09:55:27 +0100 | [diff] [blame] | 84 | gap &= PAGE_MASK; |
Kees Cook | 8e89a35 | 2015-04-14 15:47:57 -0700 | [diff] [blame] | 85 | return STACK_TOP - stack_maxrandom_size() - rnd - gap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Martin Schwidefsky | 1f6b83e | 2015-01-14 17:51:17 +0100 | [diff] [blame] | 88 | unsigned long |
| 89 | arch_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 | |
| 124 | unsigned long |
| 125 | arch_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 Brueckner | 486c0a0 | 2013-02-11 14:29:49 +0100 | [diff] [blame] | 180 | int s390_mmap_check(unsigned long addr, unsigned long len, unsigned long flags) |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 181 | { |
Hendrik Brueckner | 486c0a0 | 2013-02-11 14:29:49 +0100 | [diff] [blame] | 182 | if (is_compat_task() || (TASK_SIZE >= (1UL << 53))) |
| 183 | return 0; |
| 184 | if (!(flags & MAP_FIXED)) |
| 185 | addr = 0; |
Martin Schwidefsky | 1060786 | 2013-10-28 14:48:30 +0100 | [diff] [blame] | 186 | if ((addr + len) >= TASK_SIZE) |
| 187 | return crst_table_upgrade(current->mm, 1UL << 53); |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 191 | static unsigned long |
| 192 | s390_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 Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 196 | unsigned long area; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 197 | int rc; |
| 198 | |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 199 | area = arch_get_unmapped_area(filp, addr, len, pgoff, flags); |
| 200 | if (!(area & ~PAGE_MASK)) |
| 201 | return area; |
Heiko Carstens | 7757591 | 2009-06-12 10:26:25 +0200 | [diff] [blame] | 202 | if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) { |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 203 | /* Upgrade the page table to 4 levels and retry. */ |
| 204 | rc = crst_table_upgrade(mm, 1UL << 53); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 205 | if (rc) |
| 206 | return (unsigned long) rc; |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 207 | area = arch_get_unmapped_area(filp, addr, len, pgoff, flags); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 208 | } |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 209 | return area; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static unsigned long |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 213 | s390_get_unmapped_area_topdown(struct file *filp, const unsigned long addr, |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 214 | const unsigned long len, const unsigned long pgoff, |
| 215 | const unsigned long flags) |
| 216 | { |
| 217 | struct mm_struct *mm = current->mm; |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 218 | unsigned long area; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 219 | int rc; |
| 220 | |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 221 | area = arch_get_unmapped_area_topdown(filp, addr, len, pgoff, flags); |
| 222 | if (!(area & ~PAGE_MASK)) |
| 223 | return area; |
Heiko Carstens | 7757591 | 2009-06-12 10:26:25 +0200 | [diff] [blame] | 224 | if (area == -ENOMEM && !is_compat_task() && TASK_SIZE < (1UL << 53)) { |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 225 | /* Upgrade the page table to 4 levels and retry. */ |
| 226 | rc = crst_table_upgrade(mm, 1UL << 53); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 227 | if (rc) |
| 228 | return (unsigned long) rc; |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 229 | area = arch_get_unmapped_area_topdown(filp, addr, len, |
| 230 | pgoff, flags); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 231 | } |
Martin Schwidefsky | 0fb1d9b | 2009-03-18 13:27:37 +0100 | [diff] [blame] | 232 | return area; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 233 | } |
| 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 | */ |
| 238 | void arch_pick_mmap_layout(struct mm_struct *mm) |
| 239 | { |
Kees Cook | 8e89a35 | 2015-04-14 15:47:57 -0700 | [diff] [blame] | 240 | unsigned long random_factor = 0UL; |
| 241 | |
| 242 | if (current->flags & PF_RANDOMIZE) |
Kees Cook | 2b68f6c | 2015-04-14 15:48:00 -0700 | [diff] [blame] | 243 | random_factor = arch_mmap_rnd(); |
Kees Cook | 8e89a35 | 2015-04-14 15:47:57 -0700 | [diff] [blame] | 244 | |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 245 | /* |
| 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 Cook | 8e89a35 | 2015-04-14 15:47:57 -0700 | [diff] [blame] | 250 | mm->mmap_base = mmap_base_legacy(random_factor); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 251 | mm->get_unmapped_area = s390_get_unmapped_area; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 252 | } else { |
Kees Cook | 8e89a35 | 2015-04-14 15:47:57 -0700 | [diff] [blame] | 253 | mm->mmap_base = mmap_base(random_factor); |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 254 | mm->get_unmapped_area = s390_get_unmapped_area_topdown; |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 255 | } |
| 256 | } |
Martin Schwidefsky | 6252d70 | 2008-02-09 18:24:37 +0100 | [diff] [blame] | 257 | |
Martin Schwidefsky | 1f6b83e | 2015-01-14 17:51:17 +0100 | [diff] [blame] | 258 | static 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 | } |
| 288 | early_initcall(setup_mmap_rnd); |