blob: b017daed6887847c8eb025e146ae22b37dedfb2c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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
Heiko Carstensca218722016-05-07 12:15:34 +020025#include <linux/elf-randomize.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/personality.h>
27#include <linux/mm.h>
Martin Schwidefsky638ad342011-10-30 15:17:13 +010028#include <linux/mman.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010029#include <linux/sched/signal.h>
Ingo Molnar01042602017-02-08 18:51:31 +010030#include <linux/sched/mm.h>
Heiko Carstensdf1ca532011-01-12 09:55:27 +010031#include <linux/random.h>
Heiko Carstens048cd4e2012-02-27 10:01:52 +010032#include <linux/compat.h>
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010033#include <linux/security.h>
Martin Schwidefsky6252d702008-02-09 18:24:37 +010034#include <asm/pgalloc.h>
Paul Gortmakerff24b072017-02-09 15:20:24 -050035#include <asm/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Heiko Carstens9046e402011-01-12 09:55:22 +010037static unsigned long stack_maxrandom_size(void)
38{
39 if (!(current->flags & PF_RANDOMIZE))
40 return 0;
41 if (current->personality & ADDR_NO_RANDOMIZE)
42 return 0;
43 return STACK_RND_MASK << PAGE_SHIFT;
44}
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 * Top of mmap area (just below the process stack).
48 *
Heiko Carstens9e78a132011-01-12 09:55:23 +010049 * Leave at least a ~32 MB hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 */
Heiko Carstens9e78a132011-01-12 09:55:23 +010051#define MIN_GAP (32*1024*1024)
Martin Schwidefskyf481bfa2009-03-18 13:27:36 +010052#define MAX_GAP (STACK_TOP/6*5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Heiko Carstens1060f622011-01-12 09:55:26 +010054static inline int mmap_is_legacy(void)
55{
56 if (current->personality & ADDR_COMPAT_LAYOUT)
57 return 1;
58 if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
59 return 1;
60 return sysctl_legacy_va_layout;
61}
62
Kees Cook2b68f6c2015-04-14 15:48:00 -070063unsigned long arch_mmap_rnd(void)
Heiko Carstensdf1ca532011-01-12 09:55:27 +010064{
Martin Schwidefskyc7e8b2c2015-11-10 12:30:28 +010065 return (get_random_int() & MMAP_RND_MASK) << PAGE_SHIFT;
Heiko Carstensdf1ca532011-01-12 09:55:27 +010066}
67
Kees Cook8e89a352015-04-14 15:47:57 -070068static unsigned long mmap_base_legacy(unsigned long rnd)
Heiko Carstens7aba8422013-11-12 15:07:55 -080069{
Kees Cook8e89a352015-04-14 15:47:57 -070070 return TASK_UNMAPPED_BASE + rnd;
Heiko Carstens7aba8422013-11-12 15:07:55 -080071}
72
Kees Cook8e89a352015-04-14 15:47:57 -070073static inline unsigned long mmap_base(unsigned long rnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Jiri Slabya58c26b2010-01-13 20:44:33 +010075 unsigned long gap = rlimit(RLIMIT_STACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 if (gap < MIN_GAP)
78 gap = MIN_GAP;
79 else if (gap > MAX_GAP)
80 gap = MAX_GAP;
Heiko Carstensdf1ca532011-01-12 09:55:27 +010081 gap &= PAGE_MASK;
Kees Cook8e89a352015-04-14 15:47:57 -070082 return STACK_TOP - stack_maxrandom_size() - rnd - gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010085unsigned long
86arch_get_unmapped_area(struct file *filp, unsigned long addr,
87 unsigned long len, unsigned long pgoff, unsigned long flags)
88{
89 struct mm_struct *mm = current->mm;
90 struct vm_area_struct *vma;
91 struct vm_unmapped_area_info info;
Martin Schwidefsky9b11c792017-04-24 18:14:48 +020092 int rc;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010093
Martin Schwidefsky9b11c792017-04-24 18:14:48 +020094 if (len > TASK_SIZE - mmap_min_addr)
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010095 return -ENOMEM;
96
97 if (flags & MAP_FIXED)
Martin Schwidefsky9b11c792017-04-24 18:14:48 +020098 goto check_asce_limit;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010099
100 if (addr) {
101 addr = PAGE_ALIGN(addr);
102 vma = find_vma(mm, addr);
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200103 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100104 (!vma || addr + len <= vma->vm_start))
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200105 goto check_asce_limit;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100106 }
107
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100108 info.flags = 0;
109 info.length = len;
110 info.low_limit = mm->mmap_base;
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200111 info.high_limit = TASK_SIZE;
Martin Schwidefskyc7e8b2c2015-11-10 12:30:28 +0100112 if (filp || (flags & MAP_SHARED))
113 info.align_mask = MMAP_ALIGN_MASK << PAGE_SHIFT;
114 else
115 info.align_mask = 0;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100116 info.align_offset = pgoff << PAGE_SHIFT;
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200117 addr = vm_unmapped_area(&info);
118 if (addr & ~PAGE_MASK)
119 return addr;
120
121check_asce_limit:
122 if (addr + len > current->mm->context.asce_limit) {
123 rc = crst_table_upgrade(mm);
124 if (rc)
125 return (unsigned long) rc;
126 }
127
128 return addr;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100129}
130
131unsigned long
132arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
133 const unsigned long len, const unsigned long pgoff,
134 const unsigned long flags)
135{
136 struct vm_area_struct *vma;
137 struct mm_struct *mm = current->mm;
138 unsigned long addr = addr0;
139 struct vm_unmapped_area_info info;
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200140 int rc;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100141
142 /* requested length too big for entire address space */
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200143 if (len > TASK_SIZE - mmap_min_addr)
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100144 return -ENOMEM;
145
146 if (flags & MAP_FIXED)
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200147 goto check_asce_limit;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100148
149 /* requesting a specific address */
150 if (addr) {
151 addr = PAGE_ALIGN(addr);
152 vma = find_vma(mm, addr);
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200153 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100154 (!vma || addr + len <= vma->vm_start))
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200155 goto check_asce_limit;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100156 }
157
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100158 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
159 info.length = len;
160 info.low_limit = max(PAGE_SIZE, mmap_min_addr);
161 info.high_limit = mm->mmap_base;
Martin Schwidefskyc7e8b2c2015-11-10 12:30:28 +0100162 if (filp || (flags & MAP_SHARED))
163 info.align_mask = MMAP_ALIGN_MASK << PAGE_SHIFT;
164 else
165 info.align_mask = 0;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100166 info.align_offset = pgoff << PAGE_SHIFT;
167 addr = vm_unmapped_area(&info);
168
169 /*
170 * A failed mmap() very likely causes application failure,
171 * so fall back to the bottom-up function here. This scenario
172 * can happen with large stack limits and large mmap()
173 * allocations.
174 */
175 if (addr & ~PAGE_MASK) {
176 VM_BUG_ON(addr != -ENOMEM);
177 info.flags = 0;
178 info.low_limit = TASK_UNMAPPED_BASE;
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200179 info.high_limit = TASK_SIZE;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100180 addr = vm_unmapped_area(&info);
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200181 if (addr & ~PAGE_MASK)
182 return addr;
183 }
184
185check_asce_limit:
186 if (addr + len > current->mm->context.asce_limit) {
187 rc = crst_table_upgrade(mm);
188 if (rc)
189 return (unsigned long) rc;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100190 }
191
192 return addr;
193}
194
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100195/*
196 * This function, called very early during the creation of a new
197 * process VM image, sets up which VM layout function to use:
198 */
199void arch_pick_mmap_layout(struct mm_struct *mm)
200{
Kees Cook8e89a352015-04-14 15:47:57 -0700201 unsigned long random_factor = 0UL;
202
203 if (current->flags & PF_RANDOMIZE)
Kees Cook2b68f6c2015-04-14 15:48:00 -0700204 random_factor = arch_mmap_rnd();
Kees Cook8e89a352015-04-14 15:47:57 -0700205
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100206 /*
207 * Fall back to the standard layout if the personality
208 * bit is set, or if the expected stack growth is unlimited:
209 */
210 if (mmap_is_legacy()) {
Kees Cook8e89a352015-04-14 15:47:57 -0700211 mm->mmap_base = mmap_base_legacy(random_factor);
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200212 mm->get_unmapped_area = arch_get_unmapped_area;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100213 } else {
Kees Cook8e89a352015-04-14 15:47:57 -0700214 mm->mmap_base = mmap_base(random_factor);
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200215 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100216 }
217}