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 | * |
Rik van Riel | 0a782dc | 2017-07-12 14:36:39 -0700 | [diff] [blame] | 37 | * Leave at least a ~128 MB hole. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | */ |
Rik van Riel | 0a782dc | 2017-07-12 14:36:39 -0700 | [diff] [blame] | 39 | #define MIN_GAP (128*1024*1024) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define MAX_GAP (TASK_SIZE/6*5) |
| 41 | |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 42 | static inline int mmap_is_legacy(struct rlimit *rlim_stack) |
Anton Blanchard | 13a2cb3 | 2009-02-22 01:50:00 +0000 | [diff] [blame] | 43 | { |
| 44 | if (current->personality & ADDR_COMPAT_LAYOUT) |
| 45 | return 1; |
| 46 | |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 47 | if (rlim_stack->rlim_cur == RLIM_INFINITY) |
Anton Blanchard | 13a2cb3 | 2009-02-22 01:50:00 +0000 | [diff] [blame] | 48 | return 1; |
| 49 | |
| 50 | return sysctl_legacy_va_layout; |
| 51 | } |
| 52 | |
Kees Cook | 2b68f6c | 2015-04-14 15:48:00 -0700 | [diff] [blame] | 53 | unsigned long arch_mmap_rnd(void) |
Anton Blanchard | 9f14c42 | 2009-02-22 01:50:01 +0000 | [diff] [blame] | 54 | { |
Michael Ellerman | 9fea59b | 2017-04-21 00:36:20 +1000 | [diff] [blame] | 55 | unsigned long shift, rnd; |
Anton Blanchard | 9f14c42 | 2009-02-22 01:50:01 +0000 | [diff] [blame] | 56 | |
Michael Ellerman | 9fea59b | 2017-04-21 00:36:20 +1000 | [diff] [blame] | 57 | shift = mmap_rnd_bits; |
| 58 | #ifdef CONFIG_COMPAT |
Kees Cook | ed63227 | 2015-04-14 15:47:54 -0700 | [diff] [blame] | 59 | if (is_32bit_task()) |
Michael Ellerman | 9fea59b | 2017-04-21 00:36:20 +1000 | [diff] [blame] | 60 | shift = mmap_rnd_compat_bits; |
| 61 | #endif |
Michael Ellerman | b409946 | 2017-04-25 20:49:24 +1000 | [diff] [blame] | 62 | rnd = get_random_long() % (1ul << shift); |
Kees Cook | ed63227 | 2015-04-14 15:47:54 -0700 | [diff] [blame] | 63 | |
Dan McGee | fa8cbaa | 2011-10-17 13:05:23 +0000 | [diff] [blame] | 64 | return rnd << PAGE_SHIFT; |
Anton Blanchard | 9f14c42 | 2009-02-22 01:50:01 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Rik van Riel | 0a782dc | 2017-07-12 14:36:39 -0700 | [diff] [blame] | 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 | |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 79 | static inline unsigned long mmap_base(unsigned long rnd, |
| 80 | struct rlimit *rlim_stack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 82 | unsigned long gap = rlim_stack->rlim_cur; |
Rik van Riel | 0a782dc | 2017-07-12 14:36:39 -0700 | [diff] [blame] | 83 | unsigned long pad = stack_maxrandom_size() + stack_guard_gap; |
| 84 | |
| 85 | /* Values close to RLIM_INFINITY can overflow. */ |
| 86 | if (gap + pad > gap) |
| 87 | gap += pad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | if (gap < MIN_GAP) |
| 90 | gap = MIN_GAP; |
| 91 | else if (gap > MAX_GAP) |
| 92 | gap = MAX_GAP; |
| 93 | |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 94 | return PAGE_ALIGN(DEFAULT_MAP_WINDOW - gap - rnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 97 | #ifdef CONFIG_PPC_RADIX_MMU |
| 98 | /* |
| 99 | * Same function as generic code used only for radix, because we don't need to overload |
| 100 | * the generic one. But we will have to duplicate, because hash select |
| 101 | * HAVE_ARCH_UNMAPPED_AREA |
| 102 | */ |
| 103 | static unsigned long |
| 104 | radix__arch_get_unmapped_area(struct file *filp, unsigned long addr, |
| 105 | unsigned long len, unsigned long pgoff, |
| 106 | unsigned long flags) |
| 107 | { |
| 108 | struct mm_struct *mm = current->mm; |
| 109 | struct vm_area_struct *vma; |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 110 | int fixed = (flags & MAP_FIXED); |
| 111 | unsigned long high_limit; |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 112 | struct vm_unmapped_area_info info; |
| 113 | |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 114 | high_limit = DEFAULT_MAP_WINDOW; |
| 115 | if (addr >= high_limit || (fixed && (addr + len > high_limit))) |
| 116 | high_limit = TASK_SIZE; |
| 117 | |
| 118 | if (len > high_limit) |
| 119 | return -ENOMEM; |
Nicholas Piggin | 4722476 | 2017-11-10 04:27:40 +1100 | [diff] [blame] | 120 | |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 121 | if (fixed) { |
| 122 | if (addr > high_limit - len) |
| 123 | return -ENOMEM; |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 124 | return addr; |
Nicholas Piggin | 4722476 | 2017-11-10 04:27:40 +1100 | [diff] [blame] | 125 | } |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 126 | |
| 127 | if (addr) { |
| 128 | addr = PAGE_ALIGN(addr); |
| 129 | vma = find_vma(mm, addr); |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 130 | if (high_limit - len >= addr && addr >= mmap_min_addr && |
Hugh Dickins | 1be7107 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 131 | (!vma || addr + len <= vm_start_gap(vma))) |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 132 | return addr; |
| 133 | } |
| 134 | |
| 135 | info.flags = 0; |
| 136 | info.length = len; |
| 137 | info.low_limit = mm->mmap_base; |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 138 | info.high_limit = high_limit; |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 139 | info.align_mask = 0; |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 140 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 141 | return vm_unmapped_area(&info); |
| 142 | } |
| 143 | |
| 144 | static unsigned long |
| 145 | radix__arch_get_unmapped_area_topdown(struct file *filp, |
| 146 | const unsigned long addr0, |
| 147 | const unsigned long len, |
| 148 | const unsigned long pgoff, |
| 149 | const unsigned long flags) |
| 150 | { |
| 151 | struct vm_area_struct *vma; |
| 152 | struct mm_struct *mm = current->mm; |
| 153 | unsigned long addr = addr0; |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 154 | int fixed = (flags & MAP_FIXED); |
| 155 | unsigned long high_limit; |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 156 | struct vm_unmapped_area_info info; |
| 157 | |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 158 | high_limit = DEFAULT_MAP_WINDOW; |
| 159 | if (addr >= high_limit || (fixed && (addr + len > high_limit))) |
| 160 | high_limit = TASK_SIZE; |
| 161 | |
| 162 | if (len > high_limit) |
| 163 | return -ENOMEM; |
Nicholas Piggin | 4722476 | 2017-11-10 04:27:40 +1100 | [diff] [blame] | 164 | |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 165 | if (fixed) { |
| 166 | if (addr > high_limit - len) |
| 167 | return -ENOMEM; |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 168 | return addr; |
Nicholas Piggin | 4722476 | 2017-11-10 04:27:40 +1100 | [diff] [blame] | 169 | } |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 170 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 171 | if (addr) { |
| 172 | addr = PAGE_ALIGN(addr); |
| 173 | vma = find_vma(mm, addr); |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 174 | if (high_limit - len >= addr && addr >= mmap_min_addr && |
| 175 | (!vma || addr + len <= vm_start_gap(vma))) |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 176 | return addr; |
| 177 | } |
| 178 | |
| 179 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
| 180 | info.length = len; |
| 181 | info.low_limit = max(PAGE_SIZE, mmap_min_addr); |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 182 | info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW); |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 183 | info.align_mask = 0; |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 184 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 185 | addr = vm_unmapped_area(&info); |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 186 | if (!(addr & ~PAGE_MASK)) |
| 187 | return addr; |
| 188 | VM_BUG_ON(addr != -ENOMEM); |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * A failed mmap() very likely causes application failure, |
| 192 | * so fall back to the bottom-up function here. This scenario |
| 193 | * can happen with large stack limits and large mmap() |
| 194 | * allocations. |
| 195 | */ |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 196 | 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] | 197 | } |
| 198 | |
| 199 | static void radix__arch_pick_mmap_layout(struct mm_struct *mm, |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 200 | unsigned long random_factor, |
| 201 | struct rlimit *rlim_stack) |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 202 | { |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 203 | if (mmap_is_legacy(rlim_stack)) { |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 204 | mm->mmap_base = TASK_UNMAPPED_BASE; |
| 205 | mm->get_unmapped_area = radix__arch_get_unmapped_area; |
| 206 | } else { |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 207 | mm->mmap_base = mmap_base(random_factor, rlim_stack); |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 208 | mm->get_unmapped_area = radix__arch_get_unmapped_area_topdown; |
| 209 | } |
| 210 | } |
| 211 | #else |
| 212 | /* dummy */ |
| 213 | extern void radix__arch_pick_mmap_layout(struct mm_struct *mm, |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 214 | unsigned long random_factor, |
| 215 | struct rlimit *rlim_stack); |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 216 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* |
| 218 | * This function, called very early during the creation of a new |
| 219 | * process VM image, sets up which VM layout function to use: |
| 220 | */ |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 221 | void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Kees Cook | ed63227 | 2015-04-14 15:47:54 -0700 | [diff] [blame] | 223 | unsigned long random_factor = 0UL; |
| 224 | |
| 225 | if (current->flags & PF_RANDOMIZE) |
Kees Cook | 2b68f6c | 2015-04-14 15:48:00 -0700 | [diff] [blame] | 226 | random_factor = arch_mmap_rnd(); |
Kees Cook | ed63227 | 2015-04-14 15:47:54 -0700 | [diff] [blame] | 227 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 228 | if (radix_enabled()) |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 229 | return radix__arch_pick_mmap_layout(mm, random_factor, |
| 230 | rlim_stack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | /* |
| 232 | * Fall back to the standard layout if the personality |
| 233 | * bit is set, or if the expected stack growth is unlimited: |
| 234 | */ |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 235 | if (mmap_is_legacy(rlim_stack)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | mm->mmap_base = TASK_UNMAPPED_BASE; |
| 237 | mm->get_unmapped_area = arch_get_unmapped_area; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } else { |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 239 | mm->mmap_base = mmap_base(random_factor, rlim_stack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | mm->get_unmapped_area = arch_get_unmapped_area_topdown; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | } |