Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * flexible mmap layout support |
| 4 | * |
| 5 | * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina. |
| 6 | * All Rights Reserved. |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Started by Ingo Molnar <mingo@elte.hu> |
| 9 | */ |
| 10 | |
| 11 | #include <linux/personality.h> |
| 12 | #include <linux/mm.h> |
Anton Blanchard | 9f14c42 | 2009-02-22 01:50:01 +0000 | [diff] [blame] | 13 | #include <linux/random.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 14 | #include <linux/sched/signal.h> |
Ingo Molnar | 0104260 | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 15 | #include <linux/sched/mm.h> |
Daniel Axtens | 7f92bc5 | 2016-01-06 11:45:51 +1100 | [diff] [blame] | 16 | #include <linux/elf-randomize.h> |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 17 | #include <linux/security.h> |
| 18 | #include <linux/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * Top of mmap area (just below the process stack). |
| 22 | * |
Rik van Riel | 0a782dc | 2017-07-12 14:36:39 -0700 | [diff] [blame] | 23 | * Leave at least a ~128 MB hole. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
Rik van Riel | 0a782dc | 2017-07-12 14:36:39 -0700 | [diff] [blame] | 25 | #define MIN_GAP (128*1024*1024) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #define MAX_GAP (TASK_SIZE/6*5) |
| 27 | |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 28 | static inline int mmap_is_legacy(struct rlimit *rlim_stack) |
Anton Blanchard | 13a2cb3 | 2009-02-22 01:50:00 +0000 | [diff] [blame] | 29 | { |
| 30 | if (current->personality & ADDR_COMPAT_LAYOUT) |
| 31 | return 1; |
| 32 | |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 33 | if (rlim_stack->rlim_cur == RLIM_INFINITY) |
Anton Blanchard | 13a2cb3 | 2009-02-22 01:50:00 +0000 | [diff] [blame] | 34 | return 1; |
| 35 | |
| 36 | return sysctl_legacy_va_layout; |
| 37 | } |
| 38 | |
Kees Cook | 2b68f6c | 2015-04-14 15:48:00 -0700 | [diff] [blame] | 39 | unsigned long arch_mmap_rnd(void) |
Anton Blanchard | 9f14c42 | 2009-02-22 01:50:01 +0000 | [diff] [blame] | 40 | { |
Michael Ellerman | 9fea59b | 2017-04-21 00:36:20 +1000 | [diff] [blame] | 41 | unsigned long shift, rnd; |
Anton Blanchard | 9f14c42 | 2009-02-22 01:50:01 +0000 | [diff] [blame] | 42 | |
Michael Ellerman | 9fea59b | 2017-04-21 00:36:20 +1000 | [diff] [blame] | 43 | shift = mmap_rnd_bits; |
| 44 | #ifdef CONFIG_COMPAT |
Kees Cook | ed63227 | 2015-04-14 15:47:54 -0700 | [diff] [blame] | 45 | if (is_32bit_task()) |
Michael Ellerman | 9fea59b | 2017-04-21 00:36:20 +1000 | [diff] [blame] | 46 | shift = mmap_rnd_compat_bits; |
| 47 | #endif |
Michael Ellerman | b409946 | 2017-04-25 20:49:24 +1000 | [diff] [blame] | 48 | rnd = get_random_long() % (1ul << shift); |
Kees Cook | ed63227 | 2015-04-14 15:47:54 -0700 | [diff] [blame] | 49 | |
Dan McGee | fa8cbaa | 2011-10-17 13:05:23 +0000 | [diff] [blame] | 50 | return rnd << PAGE_SHIFT; |
Anton Blanchard | 9f14c42 | 2009-02-22 01:50:01 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Rik van Riel | 0a782dc | 2017-07-12 14:36:39 -0700 | [diff] [blame] | 53 | static inline unsigned long stack_maxrandom_size(void) |
| 54 | { |
| 55 | if (!(current->flags & PF_RANDOMIZE)) |
| 56 | return 0; |
| 57 | |
| 58 | /* 8MB for 32bit, 1GB for 64bit */ |
| 59 | if (is_32bit_task()) |
| 60 | return (1<<23); |
| 61 | else |
| 62 | return (1<<30); |
| 63 | } |
| 64 | |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 65 | static inline unsigned long mmap_base(unsigned long rnd, |
| 66 | struct rlimit *rlim_stack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 68 | unsigned long gap = rlim_stack->rlim_cur; |
Rik van Riel | 0a782dc | 2017-07-12 14:36:39 -0700 | [diff] [blame] | 69 | unsigned long pad = stack_maxrandom_size() + stack_guard_gap; |
| 70 | |
| 71 | /* Values close to RLIM_INFINITY can overflow. */ |
| 72 | if (gap + pad > gap) |
| 73 | gap += pad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | if (gap < MIN_GAP) |
| 76 | gap = MIN_GAP; |
| 77 | else if (gap > MAX_GAP) |
| 78 | gap = MAX_GAP; |
| 79 | |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 80 | return PAGE_ALIGN(DEFAULT_MAP_WINDOW - gap - rnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 83 | #ifdef CONFIG_PPC_RADIX_MMU |
| 84 | /* |
| 85 | * Same function as generic code used only for radix, because we don't need to overload |
| 86 | * the generic one. But we will have to duplicate, because hash select |
| 87 | * HAVE_ARCH_UNMAPPED_AREA |
| 88 | */ |
| 89 | static unsigned long |
| 90 | radix__arch_get_unmapped_area(struct file *filp, unsigned long addr, |
| 91 | unsigned long len, unsigned long pgoff, |
| 92 | unsigned long flags) |
| 93 | { |
| 94 | struct mm_struct *mm = current->mm; |
| 95 | struct vm_area_struct *vma; |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 96 | int fixed = (flags & MAP_FIXED); |
| 97 | unsigned long high_limit; |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 98 | struct vm_unmapped_area_info info; |
| 99 | |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 100 | high_limit = DEFAULT_MAP_WINDOW; |
| 101 | if (addr >= high_limit || (fixed && (addr + len > high_limit))) |
| 102 | high_limit = TASK_SIZE; |
| 103 | |
| 104 | if (len > high_limit) |
| 105 | return -ENOMEM; |
Nicholas Piggin | 4722476 | 2017-11-10 04:27:40 +1100 | [diff] [blame] | 106 | |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 107 | if (fixed) { |
| 108 | if (addr > high_limit - len) |
| 109 | return -ENOMEM; |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 110 | return addr; |
Nicholas Piggin | 4722476 | 2017-11-10 04:27:40 +1100 | [diff] [blame] | 111 | } |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 112 | |
| 113 | if (addr) { |
| 114 | addr = PAGE_ALIGN(addr); |
| 115 | vma = find_vma(mm, addr); |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 116 | if (high_limit - len >= addr && addr >= mmap_min_addr && |
Hugh Dickins | 1be7107 | 2017-06-19 04:03:24 -0700 | [diff] [blame] | 117 | (!vma || addr + len <= vm_start_gap(vma))) |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 118 | return addr; |
| 119 | } |
| 120 | |
| 121 | info.flags = 0; |
| 122 | info.length = len; |
| 123 | info.low_limit = mm->mmap_base; |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 124 | info.high_limit = high_limit; |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 125 | info.align_mask = 0; |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 126 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 127 | return vm_unmapped_area(&info); |
| 128 | } |
| 129 | |
| 130 | static unsigned long |
| 131 | radix__arch_get_unmapped_area_topdown(struct file *filp, |
| 132 | const unsigned long addr0, |
| 133 | const unsigned long len, |
| 134 | const unsigned long pgoff, |
| 135 | const unsigned long flags) |
| 136 | { |
| 137 | struct vm_area_struct *vma; |
| 138 | struct mm_struct *mm = current->mm; |
| 139 | unsigned long addr = addr0; |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 140 | int fixed = (flags & MAP_FIXED); |
| 141 | unsigned long high_limit; |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 142 | struct vm_unmapped_area_info info; |
| 143 | |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 144 | high_limit = DEFAULT_MAP_WINDOW; |
| 145 | if (addr >= high_limit || (fixed && (addr + len > high_limit))) |
| 146 | high_limit = TASK_SIZE; |
| 147 | |
| 148 | if (len > high_limit) |
| 149 | return -ENOMEM; |
Nicholas Piggin | 4722476 | 2017-11-10 04:27:40 +1100 | [diff] [blame] | 150 | |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 151 | if (fixed) { |
| 152 | if (addr > high_limit - len) |
| 153 | return -ENOMEM; |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 154 | return addr; |
Nicholas Piggin | 4722476 | 2017-11-10 04:27:40 +1100 | [diff] [blame] | 155 | } |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 156 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 157 | if (addr) { |
| 158 | addr = PAGE_ALIGN(addr); |
| 159 | vma = find_vma(mm, addr); |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 160 | if (high_limit - len >= addr && addr >= mmap_min_addr && |
| 161 | (!vma || addr + len <= vm_start_gap(vma))) |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 162 | return addr; |
| 163 | } |
| 164 | |
| 165 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
| 166 | info.length = len; |
| 167 | info.low_limit = max(PAGE_SIZE, mmap_min_addr); |
Nicholas Piggin | 85e3f1a | 2017-11-10 04:27:39 +1100 | [diff] [blame] | 168 | 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] | 169 | info.align_mask = 0; |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 170 | |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 171 | addr = vm_unmapped_area(&info); |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 172 | if (!(addr & ~PAGE_MASK)) |
| 173 | return addr; |
| 174 | VM_BUG_ON(addr != -ENOMEM); |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 175 | |
| 176 | /* |
| 177 | * A failed mmap() very likely causes application failure, |
| 178 | * so fall back to the bottom-up function here. This scenario |
| 179 | * can happen with large stack limits and large mmap() |
| 180 | * allocations. |
| 181 | */ |
Aneesh Kumar K.V | f4ea6dc | 2017-03-30 16:35:21 +0530 | [diff] [blame] | 182 | 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] | 183 | } |
| 184 | |
| 185 | static void radix__arch_pick_mmap_layout(struct mm_struct *mm, |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 186 | unsigned long random_factor, |
| 187 | struct rlimit *rlim_stack) |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 188 | { |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 189 | if (mmap_is_legacy(rlim_stack)) { |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 190 | mm->mmap_base = TASK_UNMAPPED_BASE; |
| 191 | mm->get_unmapped_area = radix__arch_get_unmapped_area; |
| 192 | } else { |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 193 | mm->mmap_base = mmap_base(random_factor, rlim_stack); |
Aneesh Kumar K.V | 7a0eede | 2016-04-29 23:26:11 +1000 | [diff] [blame] | 194 | mm->get_unmapped_area = radix__arch_get_unmapped_area_topdown; |
| 195 | } |
| 196 | } |
| 197 | #else |
| 198 | /* dummy */ |
| 199 | extern 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 | #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 | */ |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 207 | 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] | 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()) |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 215 | return radix__arch_pick_mmap_layout(mm, random_factor, |
| 216 | rlim_stack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* |
| 218 | * Fall back to the standard layout if the personality |
| 219 | * bit is set, or if the expected stack growth is unlimited: |
| 220 | */ |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 221 | if (mmap_is_legacy(rlim_stack)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | mm->mmap_base = TASK_UNMAPPED_BASE; |
| 223 | mm->get_unmapped_area = arch_get_unmapped_area; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } else { |
Kees Cook | 8f2af15 | 2018-04-10 16:34:53 -0700 | [diff] [blame] | 225 | mm->mmap_base = mmap_base(random_factor, rlim_stack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | mm->get_unmapped_area = arch_get_unmapped_area_topdown; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | } |