Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 2011 Wind River Systems, |
| 7 | * written by Ralf Baechle <ralf@linux-mips.org> |
| 8 | */ |
Kevin Cernekee | 1665010 | 2011-06-18 11:28:48 -0700 | [diff] [blame] | 9 | #include <linux/compiler.h> |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 10 | #include <linux/errno.h> |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/mman.h> |
Paul Gortmaker | d9ba577 | 2016-08-21 15:58:14 -0400 | [diff] [blame] | 13 | #include <linux/export.h> |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 14 | #include <linux/personality.h> |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 15 | #include <linux/random.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 16 | #include <linux/sched/signal.h> |
Ingo Molnar | 0104260 | 2017-02-08 18:51:31 +0100 | [diff] [blame] | 17 | #include <linux/sched/mm.h> |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 18 | |
| 19 | unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */ |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 20 | EXPORT_SYMBOL(shm_align_mask); |
| 21 | |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 22 | /* gap between mmap and stack */ |
| 23 | #define MIN_GAP (128*1024*1024UL) |
Kevin Cernekee | 1665010 | 2011-06-18 11:28:48 -0700 | [diff] [blame] | 24 | #define MAX_GAP ((TASK_SIZE)/6*5) |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 25 | |
| 26 | static int mmap_is_legacy(void) |
| 27 | { |
| 28 | if (current->personality & ADDR_COMPAT_LAYOUT) |
| 29 | return 1; |
| 30 | |
| 31 | if (rlimit(RLIMIT_STACK) == RLIM_INFINITY) |
| 32 | return 1; |
| 33 | |
| 34 | return sysctl_legacy_va_layout; |
| 35 | } |
| 36 | |
| 37 | static unsigned long mmap_base(unsigned long rnd) |
| 38 | { |
| 39 | unsigned long gap = rlimit(RLIMIT_STACK); |
| 40 | |
| 41 | if (gap < MIN_GAP) |
| 42 | gap = MIN_GAP; |
| 43 | else if (gap > MAX_GAP) |
| 44 | gap = MAX_GAP; |
| 45 | |
| 46 | return PAGE_ALIGN(TASK_SIZE - gap - rnd); |
| 47 | } |
| 48 | |
Kevin Cernekee | 1665010 | 2011-06-18 11:28:48 -0700 | [diff] [blame] | 49 | #define COLOUR_ALIGN(addr, pgoff) \ |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 50 | ((((addr) + shm_align_mask) & ~shm_align_mask) + \ |
| 51 | (((pgoff) << PAGE_SHIFT) & shm_align_mask)) |
| 52 | |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 53 | enum mmap_allocation_direction {UP, DOWN}; |
| 54 | |
Kevin Cernekee | 1665010 | 2011-06-18 11:28:48 -0700 | [diff] [blame] | 55 | static unsigned long arch_get_unmapped_area_common(struct file *filp, |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 56 | unsigned long addr0, unsigned long len, unsigned long pgoff, |
| 57 | unsigned long flags, enum mmap_allocation_direction dir) |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 58 | { |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 59 | struct mm_struct *mm = current->mm; |
| 60 | struct vm_area_struct *vma; |
| 61 | unsigned long addr = addr0; |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 62 | int do_color_align; |
Michel Lespinasse | b666186 | 2012-12-11 16:02:06 -0800 | [diff] [blame] | 63 | struct vm_unmapped_area_info info; |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 64 | |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 65 | if (unlikely(len > TASK_SIZE)) |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 66 | return -ENOMEM; |
| 67 | |
| 68 | if (flags & MAP_FIXED) { |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 69 | /* Even MAP_FIXED mappings must reside within TASK_SIZE */ |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 70 | if (TASK_SIZE - len < addr) |
| 71 | return -EINVAL; |
| 72 | |
| 73 | /* |
| 74 | * We do not accept a shared mapping if it would violate |
| 75 | * cache aliasing constraints. |
| 76 | */ |
| 77 | if ((flags & MAP_SHARED) && |
| 78 | ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask)) |
| 79 | return -EINVAL; |
| 80 | return addr; |
| 81 | } |
| 82 | |
| 83 | do_color_align = 0; |
| 84 | if (filp || (flags & MAP_SHARED)) |
| 85 | do_color_align = 1; |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 86 | |
| 87 | /* requesting a specific address */ |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 88 | if (addr) { |
| 89 | if (do_color_align) |
| 90 | addr = COLOUR_ALIGN(addr, pgoff); |
| 91 | else |
| 92 | addr = PAGE_ALIGN(addr); |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 93 | |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 94 | vma = find_vma(mm, addr); |
| 95 | if (TASK_SIZE - len >= addr && |
Hugh Dickins | 1be7107 | 2017-06-19 04:03:24 -0700 | [diff] [blame^] | 96 | (!vma || addr + len <= vm_start_gap(vma))) |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 97 | return addr; |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 98 | } |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 99 | |
Michel Lespinasse | b666186 | 2012-12-11 16:02:06 -0800 | [diff] [blame] | 100 | info.length = len; |
| 101 | info.align_mask = do_color_align ? (PAGE_MASK & shm_align_mask) : 0; |
| 102 | info.align_offset = pgoff << PAGE_SHIFT; |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 103 | |
Michel Lespinasse | b666186 | 2012-12-11 16:02:06 -0800 | [diff] [blame] | 104 | if (dir == DOWN) { |
| 105 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
| 106 | info.low_limit = PAGE_SIZE; |
| 107 | info.high_limit = mm->mmap_base; |
| 108 | addr = vm_unmapped_area(&info); |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 109 | |
Michel Lespinasse | b666186 | 2012-12-11 16:02:06 -0800 | [diff] [blame] | 110 | if (!(addr & ~PAGE_MASK)) |
| 111 | return addr; |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 112 | |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 113 | /* |
| 114 | * A failed mmap() very likely causes application failure, |
| 115 | * so fall back to the bottom-up function here. This scenario |
| 116 | * can happen with large stack limits and large mmap() |
| 117 | * allocations. |
| 118 | */ |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 119 | } |
Michel Lespinasse | b666186 | 2012-12-11 16:02:06 -0800 | [diff] [blame] | 120 | |
| 121 | info.flags = 0; |
| 122 | info.low_limit = mm->mmap_base; |
| 123 | info.high_limit = TASK_SIZE; |
| 124 | return vm_unmapped_area(&info); |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0, |
| 128 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 129 | { |
Kevin Cernekee | 1665010 | 2011-06-18 11:28:48 -0700 | [diff] [blame] | 130 | return arch_get_unmapped_area_common(filp, |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 131 | addr0, len, pgoff, flags, UP); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * There is no need to export this but sched.h declares the function as |
| 136 | * extern so making it static here results in an error. |
| 137 | */ |
| 138 | unsigned long arch_get_unmapped_area_topdown(struct file *filp, |
| 139 | unsigned long addr0, unsigned long len, unsigned long pgoff, |
| 140 | unsigned long flags) |
| 141 | { |
Kevin Cernekee | 1665010 | 2011-06-18 11:28:48 -0700 | [diff] [blame] | 142 | return arch_get_unmapped_area_common(filp, |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 143 | addr0, len, pgoff, flags, DOWN); |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Kees Cook | 2b68f6c | 2015-04-14 15:48:00 -0700 | [diff] [blame] | 146 | unsigned long arch_mmap_rnd(void) |
Kees Cook | 1f0569d | 2015-04-14 15:47:51 -0700 | [diff] [blame] | 147 | { |
| 148 | unsigned long rnd; |
| 149 | |
Matt Redfearn | 109c32f | 2016-11-24 17:32:45 +0000 | [diff] [blame] | 150 | #ifdef CONFIG_COMPAT |
Kees Cook | 1f0569d | 2015-04-14 15:47:51 -0700 | [diff] [blame] | 151 | if (TASK_IS_32BIT_ADDR) |
Matt Redfearn | 109c32f | 2016-11-24 17:32:45 +0000 | [diff] [blame] | 152 | rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); |
Kees Cook | 1f0569d | 2015-04-14 15:47:51 -0700 | [diff] [blame] | 153 | else |
Matt Redfearn | 109c32f | 2016-11-24 17:32:45 +0000 | [diff] [blame] | 154 | #endif /* CONFIG_COMPAT */ |
| 155 | rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); |
Kees Cook | 1f0569d | 2015-04-14 15:47:51 -0700 | [diff] [blame] | 156 | |
Matt Redfearn | 109c32f | 2016-11-24 17:32:45 +0000 | [diff] [blame] | 157 | return rnd << PAGE_SHIFT; |
Kees Cook | 1f0569d | 2015-04-14 15:47:51 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 160 | void arch_pick_mmap_layout(struct mm_struct *mm) |
| 161 | { |
| 162 | unsigned long random_factor = 0UL; |
| 163 | |
Kees Cook | 1f0569d | 2015-04-14 15:47:51 -0700 | [diff] [blame] | 164 | if (current->flags & PF_RANDOMIZE) |
Kees Cook | 2b68f6c | 2015-04-14 15:48:00 -0700 | [diff] [blame] | 165 | random_factor = arch_mmap_rnd(); |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 166 | |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 167 | if (mmap_is_legacy()) { |
| 168 | mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; |
| 169 | mm->get_unmapped_area = arch_get_unmapped_area; |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 170 | } else { |
| 171 | mm->mmap_base = mmap_base(random_factor); |
| 172 | mm->get_unmapped_area = arch_get_unmapped_area_topdown; |
Jian Peng | d0be89f | 2011-05-17 12:27:49 -0700 | [diff] [blame] | 173 | } |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static inline unsigned long brk_rnd(void) |
| 177 | { |
Daniel Cashman | 5ef11c3 | 2016-02-26 15:19:37 -0800 | [diff] [blame] | 178 | unsigned long rnd = get_random_long(); |
Ralf Baechle | 6f6c3c33 | 2011-05-19 09:21:33 +0100 | [diff] [blame] | 179 | |
| 180 | rnd = rnd << PAGE_SHIFT; |
| 181 | /* 8MB for 32bit, 256MB for 64bit */ |
| 182 | if (TASK_IS_32BIT_ADDR) |
| 183 | rnd = rnd & 0x7ffffful; |
| 184 | else |
| 185 | rnd = rnd & 0xffffffful; |
| 186 | |
| 187 | return rnd; |
| 188 | } |
| 189 | |
| 190 | unsigned long arch_randomize_brk(struct mm_struct *mm) |
| 191 | { |
| 192 | unsigned long base = mm->brk; |
| 193 | unsigned long ret; |
| 194 | |
| 195 | ret = PAGE_ALIGN(base + brk_rnd()); |
| 196 | |
| 197 | if (ret < mm->brk) |
| 198 | return mm->brk; |
| 199 | |
| 200 | return ret; |
| 201 | } |
Steven Rostedt | 196897a | 2013-01-25 17:13:15 +0000 | [diff] [blame] | 202 | |
| 203 | int __virt_addr_valid(const volatile void *kaddr) |
| 204 | { |
| 205 | return pfn_valid(PFN_DOWN(virt_to_phys(kaddr))); |
| 206 | } |
| 207 | EXPORT_SYMBOL_GPL(__virt_addr_valid); |