blob: ae683fdc716c08c08b1e23eaa894e22c8b2e41c2 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * flexible mmap layout support
4 *
5 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
6 * All Rights Reserved.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Started by Ingo Molnar <mingo@elte.hu>
9 */
10
11#include <linux/personality.h>
12#include <linux/mm.h>
Anton Blanchard9f14c422009-02-22 01:50:01 +000013#include <linux/random.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010014#include <linux/sched/signal.h>
Ingo Molnar01042602017-02-08 18:51:31 +010015#include <linux/sched/mm.h>
Daniel Axtens7f92bc52016-01-06 11:45:51 +110016#include <linux/elf-randomize.h>
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +100017#include <linux/security.h>
18#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/*
21 * Top of mmap area (just below the process stack).
22 *
Rik van Riel0a782dc2017-07-12 14:36:39 -070023 * Leave at least a ~128 MB hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
Rik van Riel0a782dc2017-07-12 14:36:39 -070025#define MIN_GAP (128*1024*1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define MAX_GAP (TASK_SIZE/6*5)
27
Kees Cook8f2af152018-04-10 16:34:53 -070028static inline int mmap_is_legacy(struct rlimit *rlim_stack)
Anton Blanchard13a2cb32009-02-22 01:50:00 +000029{
30 if (current->personality & ADDR_COMPAT_LAYOUT)
31 return 1;
32
Kees Cook8f2af152018-04-10 16:34:53 -070033 if (rlim_stack->rlim_cur == RLIM_INFINITY)
Anton Blanchard13a2cb32009-02-22 01:50:00 +000034 return 1;
35
36 return sysctl_legacy_va_layout;
37}
38
Kees Cook2b68f6c2015-04-14 15:48:00 -070039unsigned long arch_mmap_rnd(void)
Anton Blanchard9f14c422009-02-22 01:50:01 +000040{
Michael Ellerman9fea59b2017-04-21 00:36:20 +100041 unsigned long shift, rnd;
Anton Blanchard9f14c422009-02-22 01:50:01 +000042
Michael Ellerman9fea59b2017-04-21 00:36:20 +100043 shift = mmap_rnd_bits;
44#ifdef CONFIG_COMPAT
Kees Cooked632272015-04-14 15:47:54 -070045 if (is_32bit_task())
Michael Ellerman9fea59b2017-04-21 00:36:20 +100046 shift = mmap_rnd_compat_bits;
47#endif
Michael Ellermanb4099462017-04-25 20:49:24 +100048 rnd = get_random_long() % (1ul << shift);
Kees Cooked632272015-04-14 15:47:54 -070049
Dan McGeefa8cbaa2011-10-17 13:05:23 +000050 return rnd << PAGE_SHIFT;
Anton Blanchard9f14c422009-02-22 01:50:01 +000051}
52
Rik van Riel0a782dc2017-07-12 14:36:39 -070053static 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 Cook8f2af152018-04-10 16:34:53 -070065static inline unsigned long mmap_base(unsigned long rnd,
66 struct rlimit *rlim_stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Kees Cook8f2af152018-04-10 16:34:53 -070068 unsigned long gap = rlim_stack->rlim_cur;
Rik van Riel0a782dc2017-07-12 14:36:39 -070069 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 Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (gap < MIN_GAP)
76 gap = MIN_GAP;
77 else if (gap > MAX_GAP)
78 gap = MAX_GAP;
79
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +053080 return PAGE_ALIGN(DEFAULT_MAP_WINDOW - gap - rnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +100083#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 */
89static unsigned long
90radix__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 Piggin85e3f1a2017-11-10 04:27:39 +110096 int fixed = (flags & MAP_FIXED);
97 unsigned long high_limit;
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +100098 struct vm_unmapped_area_info info;
99
Nicholas Piggin85e3f1a2017-11-10 04:27:39 +1100100 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 Piggin47224762017-11-10 04:27:40 +1100106
Nicholas Piggin85e3f1a2017-11-10 04:27:39 +1100107 if (fixed) {
108 if (addr > high_limit - len)
109 return -ENOMEM;
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000110 return addr;
Nicholas Piggin47224762017-11-10 04:27:40 +1100111 }
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000112
113 if (addr) {
114 addr = PAGE_ALIGN(addr);
115 vma = find_vma(mm, addr);
Nicholas Piggin85e3f1a2017-11-10 04:27:39 +1100116 if (high_limit - len >= addr && addr >= mmap_min_addr &&
Hugh Dickins1be71072017-06-19 04:03:24 -0700117 (!vma || addr + len <= vm_start_gap(vma)))
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000118 return addr;
119 }
120
121 info.flags = 0;
122 info.length = len;
123 info.low_limit = mm->mmap_base;
Nicholas Piggin85e3f1a2017-11-10 04:27:39 +1100124 info.high_limit = high_limit;
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000125 info.align_mask = 0;
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530126
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000127 return vm_unmapped_area(&info);
128}
129
130static unsigned long
131radix__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 Piggin85e3f1a2017-11-10 04:27:39 +1100140 int fixed = (flags & MAP_FIXED);
141 unsigned long high_limit;
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000142 struct vm_unmapped_area_info info;
143
Nicholas Piggin85e3f1a2017-11-10 04:27:39 +1100144 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 Piggin47224762017-11-10 04:27:40 +1100150
Nicholas Piggin85e3f1a2017-11-10 04:27:39 +1100151 if (fixed) {
152 if (addr > high_limit - len)
153 return -ENOMEM;
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000154 return addr;
Nicholas Piggin47224762017-11-10 04:27:40 +1100155 }
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000156
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000157 if (addr) {
158 addr = PAGE_ALIGN(addr);
159 vma = find_vma(mm, addr);
Nicholas Piggin85e3f1a2017-11-10 04:27:39 +1100160 if (high_limit - len >= addr && addr >= mmap_min_addr &&
161 (!vma || addr + len <= vm_start_gap(vma)))
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000162 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 Piggin85e3f1a2017-11-10 04:27:39 +1100168 info.high_limit = mm->mmap_base + (high_limit - DEFAULT_MAP_WINDOW);
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000169 info.align_mask = 0;
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530170
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000171 addr = vm_unmapped_area(&info);
Aneesh Kumar K.Vf4ea6dc2017-03-30 16:35:21 +0530172 if (!(addr & ~PAGE_MASK))
173 return addr;
174 VM_BUG_ON(addr != -ENOMEM);
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000175
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.Vf4ea6dc2017-03-30 16:35:21 +0530182 return radix__arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000183}
184
185static void radix__arch_pick_mmap_layout(struct mm_struct *mm,
Kees Cook8f2af152018-04-10 16:34:53 -0700186 unsigned long random_factor,
187 struct rlimit *rlim_stack)
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000188{
Kees Cook8f2af152018-04-10 16:34:53 -0700189 if (mmap_is_legacy(rlim_stack)) {
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000190 mm->mmap_base = TASK_UNMAPPED_BASE;
191 mm->get_unmapped_area = radix__arch_get_unmapped_area;
192 } else {
Kees Cook8f2af152018-04-10 16:34:53 -0700193 mm->mmap_base = mmap_base(random_factor, rlim_stack);
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000194 mm->get_unmapped_area = radix__arch_get_unmapped_area_topdown;
195 }
196}
197#else
198/* dummy */
199extern void radix__arch_pick_mmap_layout(struct mm_struct *mm,
Kees Cook8f2af152018-04-10 16:34:53 -0700200 unsigned long random_factor,
201 struct rlimit *rlim_stack);
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
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 Cook8f2af152018-04-10 16:34:53 -0700207void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Kees Cooked632272015-04-14 15:47:54 -0700209 unsigned long random_factor = 0UL;
210
211 if (current->flags & PF_RANDOMIZE)
Kees Cook2b68f6c2015-04-14 15:48:00 -0700212 random_factor = arch_mmap_rnd();
Kees Cooked632272015-04-14 15:47:54 -0700213
Aneesh Kumar K.V7a0eede2016-04-29 23:26:11 +1000214 if (radix_enabled())
Kees Cook8f2af152018-04-10 16:34:53 -0700215 return radix__arch_pick_mmap_layout(mm, random_factor,
216 rlim_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /*
218 * Fall back to the standard layout if the personality
219 * bit is set, or if the expected stack growth is unlimited:
220 */
Kees Cook8f2af152018-04-10 16:34:53 -0700221 if (mmap_is_legacy(rlim_stack)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 mm->mmap_base = TASK_UNMAPPED_BASE;
223 mm->get_unmapped_area = arch_get_unmapped_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 } else {
Kees Cook8f2af152018-04-10 16:34:53 -0700225 mm->mmap_base = mmap_base(random_factor, rlim_stack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228}