blob: 831bdcf407bbc1d2d76edc78e6a9f50aae1406cb [file] [log] [blame]
Greg Kroah-Hartmanac41aae2017-11-24 15:00:35 +01001// SPDX-License-Identifier: GPL-2.0+
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
Heiko Carstensca218722016-05-07 12:15:34 +020011#include <linux/elf-randomize.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/personality.h>
13#include <linux/mm.h>
Martin Schwidefsky638ad342011-10-30 15:17:13 +010014#include <linux/mman.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010015#include <linux/sched/signal.h>
Ingo Molnar01042602017-02-08 18:51:31 +010016#include <linux/sched/mm.h>
Heiko Carstensdf1ca532011-01-12 09:55:27 +010017#include <linux/random.h>
Heiko Carstens048cd4e2012-02-27 10:01:52 +010018#include <linux/compat.h>
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010019#include <linux/security.h>
Martin Schwidefsky6252d702008-02-09 18:24:37 +010020#include <asm/pgalloc.h>
Paul Gortmakerff24b072017-02-09 15:20:24 -050021#include <asm/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Heiko Carstens9046e402011-01-12 09:55:22 +010023static unsigned long stack_maxrandom_size(void)
24{
25 if (!(current->flags & PF_RANDOMIZE))
26 return 0;
27 if (current->personality & ADDR_NO_RANDOMIZE)
28 return 0;
29 return STACK_RND_MASK << PAGE_SHIFT;
30}
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
33 * Top of mmap area (just below the process stack).
34 *
Heiko Carstens9e78a132011-01-12 09:55:23 +010035 * Leave at least a ~32 MB hole.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
Heiko Carstens9e78a132011-01-12 09:55:23 +010037#define MIN_GAP (32*1024*1024)
Martin Schwidefskyf481bfa2009-03-18 13:27:36 +010038#define MAX_GAP (STACK_TOP/6*5)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Heiko Carstens1060f622011-01-12 09:55:26 +010040static inline int mmap_is_legacy(void)
41{
42 if (current->personality & ADDR_COMPAT_LAYOUT)
43 return 1;
44 if (rlimit(RLIMIT_STACK) == RLIM_INFINITY)
45 return 1;
46 return sysctl_legacy_va_layout;
47}
48
Kees Cook2b68f6c2015-04-14 15:48:00 -070049unsigned long arch_mmap_rnd(void)
Heiko Carstensdf1ca532011-01-12 09:55:27 +010050{
Martin Schwidefskyc7e8b2c2015-11-10 12:30:28 +010051 return (get_random_int() & MMAP_RND_MASK) << PAGE_SHIFT;
Heiko Carstensdf1ca532011-01-12 09:55:27 +010052}
53
Kees Cook8e89a352015-04-14 15:47:57 -070054static unsigned long mmap_base_legacy(unsigned long rnd)
Heiko Carstens7aba8422013-11-12 15:07:55 -080055{
Kees Cook8e89a352015-04-14 15:47:57 -070056 return TASK_UNMAPPED_BASE + rnd;
Heiko Carstens7aba8422013-11-12 15:07:55 -080057}
58
Kees Cook8e89a352015-04-14 15:47:57 -070059static inline unsigned long mmap_base(unsigned long rnd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Jiri Slabya58c26b2010-01-13 20:44:33 +010061 unsigned long gap = rlimit(RLIMIT_STACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 if (gap < MIN_GAP)
64 gap = MIN_GAP;
65 else if (gap > MAX_GAP)
66 gap = MAX_GAP;
Heiko Carstensdf1ca532011-01-12 09:55:27 +010067 gap &= PAGE_MASK;
Kees Cook8e89a352015-04-14 15:47:57 -070068 return STACK_TOP - stack_maxrandom_size() - rnd - gap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010071unsigned long
72arch_get_unmapped_area(struct file *filp, unsigned long addr,
73 unsigned long len, unsigned long pgoff, unsigned long flags)
74{
75 struct mm_struct *mm = current->mm;
76 struct vm_area_struct *vma;
77 struct vm_unmapped_area_info info;
Martin Schwidefsky9b11c792017-04-24 18:14:48 +020078 int rc;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010079
Martin Schwidefsky9b11c792017-04-24 18:14:48 +020080 if (len > TASK_SIZE - mmap_min_addr)
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010081 return -ENOMEM;
82
83 if (flags & MAP_FIXED)
Martin Schwidefsky9b11c792017-04-24 18:14:48 +020084 goto check_asce_limit;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010085
86 if (addr) {
87 addr = PAGE_ALIGN(addr);
88 vma = find_vma(mm, addr);
Martin Schwidefsky9b11c792017-04-24 18:14:48 +020089 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Hugh Dickins1be71072017-06-19 04:03:24 -070090 (!vma || addr + len <= vm_start_gap(vma)))
Martin Schwidefsky9b11c792017-04-24 18:14:48 +020091 goto check_asce_limit;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010092 }
93
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +010094 info.flags = 0;
95 info.length = len;
96 info.low_limit = mm->mmap_base;
Martin Schwidefsky9b11c792017-04-24 18:14:48 +020097 info.high_limit = TASK_SIZE;
Martin Schwidefskyc7e8b2c2015-11-10 12:30:28 +010098 if (filp || (flags & MAP_SHARED))
99 info.align_mask = MMAP_ALIGN_MASK << PAGE_SHIFT;
100 else
101 info.align_mask = 0;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100102 info.align_offset = pgoff << PAGE_SHIFT;
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200103 addr = vm_unmapped_area(&info);
104 if (addr & ~PAGE_MASK)
105 return addr;
106
107check_asce_limit:
Martin Schwidefsky8ab867c2017-08-31 13:18:22 +0200108 if (addr + len > current->mm->context.asce_limit &&
109 addr + len <= TASK_SIZE) {
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +0200110 rc = crst_table_upgrade(mm, addr + len);
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200111 if (rc)
112 return (unsigned long) rc;
113 }
114
115 return addr;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100116}
117
118unsigned long
119arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
120 const unsigned long len, const unsigned long pgoff,
121 const unsigned long flags)
122{
123 struct vm_area_struct *vma;
124 struct mm_struct *mm = current->mm;
125 unsigned long addr = addr0;
126 struct vm_unmapped_area_info info;
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200127 int rc;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100128
129 /* requested length too big for entire address space */
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200130 if (len > TASK_SIZE - mmap_min_addr)
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100131 return -ENOMEM;
132
133 if (flags & MAP_FIXED)
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200134 goto check_asce_limit;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100135
136 /* requesting a specific address */
137 if (addr) {
138 addr = PAGE_ALIGN(addr);
139 vma = find_vma(mm, addr);
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200140 if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
Hugh Dickins1be71072017-06-19 04:03:24 -0700141 (!vma || addr + len <= vm_start_gap(vma)))
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200142 goto check_asce_limit;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100143 }
144
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100145 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
146 info.length = len;
147 info.low_limit = max(PAGE_SIZE, mmap_min_addr);
148 info.high_limit = mm->mmap_base;
Martin Schwidefskyc7e8b2c2015-11-10 12:30:28 +0100149 if (filp || (flags & MAP_SHARED))
150 info.align_mask = MMAP_ALIGN_MASK << PAGE_SHIFT;
151 else
152 info.align_mask = 0;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100153 info.align_offset = pgoff << PAGE_SHIFT;
154 addr = vm_unmapped_area(&info);
155
156 /*
157 * A failed mmap() very likely causes application failure,
158 * so fall back to the bottom-up function here. This scenario
159 * can happen with large stack limits and large mmap()
160 * allocations.
161 */
162 if (addr & ~PAGE_MASK) {
163 VM_BUG_ON(addr != -ENOMEM);
164 info.flags = 0;
165 info.low_limit = TASK_UNMAPPED_BASE;
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200166 info.high_limit = TASK_SIZE;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100167 addr = vm_unmapped_area(&info);
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200168 if (addr & ~PAGE_MASK)
169 return addr;
170 }
171
172check_asce_limit:
Martin Schwidefsky8ab867c2017-08-31 13:18:22 +0200173 if (addr + len > current->mm->context.asce_limit &&
174 addr + len <= TASK_SIZE) {
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +0200175 rc = crst_table_upgrade(mm, addr + len);
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200176 if (rc)
177 return (unsigned long) rc;
Martin Schwidefsky1f6b83e2015-01-14 17:51:17 +0100178 }
179
180 return addr;
181}
182
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100183/*
184 * This function, called very early during the creation of a new
185 * process VM image, sets up which VM layout function to use:
186 */
187void arch_pick_mmap_layout(struct mm_struct *mm)
188{
Kees Cook8e89a352015-04-14 15:47:57 -0700189 unsigned long random_factor = 0UL;
190
191 if (current->flags & PF_RANDOMIZE)
Kees Cook2b68f6c2015-04-14 15:48:00 -0700192 random_factor = arch_mmap_rnd();
Kees Cook8e89a352015-04-14 15:47:57 -0700193
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100194 /*
195 * Fall back to the standard layout if the personality
196 * bit is set, or if the expected stack growth is unlimited:
197 */
198 if (mmap_is_legacy()) {
Kees Cook8e89a352015-04-14 15:47:57 -0700199 mm->mmap_base = mmap_base_legacy(random_factor);
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200200 mm->get_unmapped_area = arch_get_unmapped_area;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100201 } else {
Kees Cook8e89a352015-04-14 15:47:57 -0700202 mm->mmap_base = mmap_base(random_factor);
Martin Schwidefsky9b11c792017-04-24 18:14:48 +0200203 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100204 }
205}