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> |
Anton Blanchard | 9f14c42 | 2009-02-22 01:50:01 +0000 | [diff] [blame] | 27 | #include <linux/random.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 28 | #include <linux/sched/signal.h> |
Ingo Molnar | 0104260 | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 29 | #include <linux/sched/mm.h> |
Daniel Axtens | 7f92bc5 | 2016-01-06 11:45:51 +1100 | [diff] [blame] | 30 | #include <linux/elf-randomize.h> |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 31 | #include <linux/security.h> |
| 32 | #include <linux/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * Top of mmap area (just below the process stack). |
| 36 | * |
Anton Blanchard | 002b0ec | 2009-02-22 01:50:06 +0000 | [diff] [blame] | 37 | * Leave at least a ~128 MB hole on 32bit applications. |
| 38 | * |
| 39 | * On 64bit applications we randomise the stack by 1GB so we need to |
| 40 | * space our mmap start address by a further 1GB, otherwise there is a |
| 41 | * chance the mmap area will end up closer to the stack than our ulimit |
| 42 | * requires. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | */ |
Anton Blanchard | 002b0ec | 2009-02-22 01:50:06 +0000 | [diff] [blame] | 44 | #define MIN_GAP32 (128*1024*1024) |
| 45 | #define MIN_GAP64 ((128 + 1024)*1024*1024UL) |
| 46 | #define MIN_GAP ((is_32bit_task()) ? MIN_GAP32 : MIN_GAP64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #define MAX_GAP (TASK_SIZE/6*5) |
| 48 | |
Anton Blanchard | 13a2cb3 | 2009-02-22 01:50:00 +0000 | [diff] [blame] | 49 | static inline int mmap_is_legacy(void) |
| 50 | { |
| 51 | if (current->personality & ADDR_COMPAT_LAYOUT) |
| 52 | return 1; |
| 53 | |
Jiri Slaby | 4bf936b | 2010-01-06 05:24:31 +0000 | [diff] [blame] | 54 | if (rlimit(RLIMIT_STACK) == RLIM_INFINITY) |
Anton Blanchard | 13a2cb3 | 2009-02-22 01:50:00 +0000 | [diff] [blame] | 55 | return 1; |
| 56 | |
| 57 | return sysctl_legacy_va_layout; |
| 58 | } |
| 59 | |
Kees Cook | 2b68f6c | 2015-04-14 15:48:00 -0700 | [diff] [blame] | 60 | unsigned long arch_mmap_rnd(void) |
Anton Blanchard | 9f14c42 | 2009-02-22 01:50:01 +0000 | [diff] [blame] | 61 | { |
Michael Ellerman | 9fea59b | 2017-04-21 00:36:20 +1000 | [diff] [blame] | 62 | unsigned long shift, rnd; |
Anton Blanchard | 9f14c42 | 2009-02-22 01:50:01 +0000 | [diff] [blame] | 63 | |
Michael Ellerman | 9fea59b | 2017-04-21 00:36:20 +1000 | [diff] [blame] | 64 | shift = mmap_rnd_bits; |
| 65 | #ifdef CONFIG_COMPAT |
Kees Cook | ed63227 | 2015-04-14 15:47:54 -0700 | [diff] [blame] | 66 | if (is_32bit_task()) |
Michael Ellerman | 9fea59b | 2017-04-21 00:36:20 +1000 | [diff] [blame] | 67 | shift = mmap_rnd_compat_bits; |
| 68 | #endif |
Michael Ellerman | b409946 | 2017-04-25 20:49:24 +1000 | [diff] [blame] | 69 | rnd = get_random_long() % (1ul << shift); |
Kees Cook | ed63227 | 2015-04-14 15:47:54 -0700 | [diff] [blame] | 70 | |
Dan McGee | fa8cbaa | 2011-10-17 13:05:23 +0000 | [diff] [blame] | 71 | return rnd << PAGE_SHIFT; |
Anton Blanchard | 9f14c42 | 2009-02-22 01:50:01 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Kees Cook | ed63227 | 2015-04-14 15:47:54 -0700 | [diff] [blame] | 74 | static inline unsigned long mmap_base(unsigned long rnd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Jiri Slaby | 4bf936b | 2010-01-06 05:24:31 +0000 | [diff] [blame] | 76 | unsigned long gap = rlimit(RLIMIT_STACK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | if (gap < MIN_GAP) |
| 79 | gap = MIN_GAP; |
| 80 | else if (gap > MAX_GAP) |
| 81 | gap = MAX_GAP; |
| 82 | |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 83 | return PAGE_ALIGN(DEFAULT_MAP_WINDOW - gap - rnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 86 | #ifdef CONFIG_PPC_RADIX_MMU |
| 87 | /* |
| 88 | * Same function as generic code used only for radix, because we don't need to overload |
| 89 | * the generic one. But we will have to duplicate, because hash select |
| 90 | * HAVE_ARCH_UNMAPPED_AREA |
| 91 | */ |
| 92 | static unsigned long |
| 93 | radix__arch_get_unmapped_area(struct file *filp, unsigned long addr, |
| 94 | unsigned long len, unsigned long pgoff, |
| 95 | unsigned long flags) |
| 96 | { |
| 97 | struct mm_struct *mm = current->mm; |
| 98 | struct vm_area_struct *vma; |
| 99 | struct vm_unmapped_area_info info; |
| 100 | |
Aneesh Kumar K.V | 321f7d2 | 2017-04-18 12:31:27 +0530 | [diff] [blame] | 101 | if (unlikely(addr > mm->context.addr_limit && |
| 102 | mm->context.addr_limit != TASK_SIZE)) |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 103 | mm->context.addr_limit = TASK_SIZE; |
| 104 | |
Aneesh Kumar K.V | be77e99 | 2017-04-14 00:48:21 +0530 | [diff] [blame] | 105 | if (len > mm->task_size - mmap_min_addr) |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 106 | return -ENOMEM; |
| 107 | |
| 108 | if (flags & MAP_FIXED) |
| 109 | return addr; |
| 110 | |
| 111 | if (addr) { |
| 112 | addr = PAGE_ALIGN(addr); |
| 113 | vma = find_vma(mm, addr); |
Aneesh Kumar K.V | be77e99 | 2017-04-14 00:48:21 +0530 | [diff] [blame] | 114 | if (mm->task_size - len >= addr && addr >= mmap_min_addr && |
Hugh Dickins | 1be7107 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 115 | (!vma || addr + len <= vm_start_gap(vma))) |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 116 | return addr; |
| 117 | } |
| 118 | |
| 119 | info.flags = 0; |
| 120 | info.length = len; |
| 121 | info.low_limit = mm->mmap_base; |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 122 | info.align_mask = 0; |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 123 | |
| 124 | if (unlikely(addr > DEFAULT_MAP_WINDOW)) |
| 125 | info.high_limit = mm->context.addr_limit; |
| 126 | else |
| 127 | info.high_limit = DEFAULT_MAP_WINDOW; |
| 128 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 129 | return vm_unmapped_area(&info); |
| 130 | } |
| 131 | |
| 132 | static unsigned long |
| 133 | radix__arch_get_unmapped_area_topdown(struct file *filp, |
| 134 | const unsigned long addr0, |
| 135 | const unsigned long len, |
| 136 | const unsigned long pgoff, |
| 137 | const unsigned long flags) |
| 138 | { |
| 139 | struct vm_area_struct *vma; |
| 140 | struct mm_struct *mm = current->mm; |
| 141 | unsigned long addr = addr0; |
| 142 | struct vm_unmapped_area_info info; |
| 143 | |
Aneesh Kumar K.V | 321f7d2 | 2017-04-18 12:31:27 +0530 | [diff] [blame] | 144 | if (unlikely(addr > mm->context.addr_limit && |
| 145 | mm->context.addr_limit != TASK_SIZE)) |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 146 | mm->context.addr_limit = TASK_SIZE; |
| 147 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 148 | /* requested length too big for entire address space */ |
Aneesh Kumar K.V | be77e99 | 2017-04-14 00:48:21 +0530 | [diff] [blame] | 149 | if (len > mm->task_size - mmap_min_addr) |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 150 | return -ENOMEM; |
| 151 | |
| 152 | if (flags & MAP_FIXED) |
| 153 | return addr; |
| 154 | |
| 155 | /* requesting a specific address */ |
| 156 | if (addr) { |
| 157 | addr = PAGE_ALIGN(addr); |
| 158 | vma = find_vma(mm, addr); |
Aneesh Kumar K.V | be77e99 | 2017-04-14 00:48:21 +0530 | [diff] [blame] | 159 | if (mm->task_size - len >= addr && addr >= mmap_min_addr && |
Hugh Dickins | 1be7107 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 160 | (!vma || addr + len <= vm_start_gap(vma))) |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 161 | return addr; |
| 162 | } |
| 163 | |
| 164 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
| 165 | info.length = len; |
| 166 | info.low_limit = max(PAGE_SIZE, mmap_min_addr); |
| 167 | info.high_limit = mm->mmap_base; |
| 168 | info.align_mask = 0; |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 169 | |
| 170 | if (addr > DEFAULT_MAP_WINDOW) |
| 171 | info.high_limit += mm->context.addr_limit - DEFAULT_MAP_WINDOW; |
| 172 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 173 | addr = vm_unmapped_area(&info); |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 174 | if (!(addr & ~PAGE_MASK)) |
| 175 | return addr; |
| 176 | VM_BUG_ON(addr != -ENOMEM); |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * A failed mmap() very likely causes application failure, |
| 180 | * so fall back to the bottom-up function here. This scenario |
| 181 | * can happen with large stack limits and large mmap() |
| 182 | * allocations. |
| 183 | */ |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 184 | return radix__arch_get_unmapped_area(filp, addr0, len, pgoff, flags); |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static void radix__arch_pick_mmap_layout(struct mm_struct *mm, |
| 188 | unsigned long random_factor) |
| 189 | { |
| 190 | if (mmap_is_legacy()) { |
| 191 | mm->mmap_base = TASK_UNMAPPED_BASE; |
| 192 | mm->get_unmapped_area = radix__arch_get_unmapped_area; |
| 193 | } else { |
| 194 | mm->mmap_base = mmap_base(random_factor); |
| 195 | mm->get_unmapped_area = radix__arch_get_unmapped_area_topdown; |
| 196 | } |
| 197 | } |
| 198 | #else |
| 199 | /* dummy */ |
| 200 | extern void radix__arch_pick_mmap_layout(struct mm_struct *mm, |
| 201 | unsigned long random_factor); |
| 202 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | /* |
| 204 | * This function, called very early during the creation of a new |
| 205 | * process VM image, sets up which VM layout function to use: |
| 206 | */ |
| 207 | void arch_pick_mmap_layout(struct mm_struct *mm) |
| 208 | { |
Kees Cook | ed63227 | 2015-04-14 15:47:54 -0700 | [diff] [blame] | 209 | unsigned long random_factor = 0UL; |
| 210 | |
| 211 | if (current->flags & PF_RANDOMIZE) |
Kees Cook | 2b68f6c | 2015-04-14 15:48:00 -0700 | [diff] [blame] | 212 | random_factor = arch_mmap_rnd(); |
Kees Cook | ed63227 | 2015-04-14 15:47:54 -0700 | [diff] [blame] | 213 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 214 | if (radix_enabled()) |
| 215 | return radix__arch_pick_mmap_layout(mm, random_factor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | /* |
| 217 | * Fall back to the standard layout if the personality |
| 218 | * bit is set, or if the expected stack growth is unlimited: |
| 219 | */ |
| 220 | if (mmap_is_legacy()) { |
| 221 | mm->mmap_base = TASK_UNMAPPED_BASE; |
| 222 | mm->get_unmapped_area = arch_get_unmapped_area; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } else { |
Kees Cook | ed63227 | 2015-04-14 15:47:54 -0700 | [diff] [blame] | 224 | mm->mmap_base = mmap_base(random_factor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | mm->get_unmapped_area = arch_get_unmapped_area_topdown; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | } |