blob: 233033f99d8fc62e2dc1d5dfc0c583f6ea4b1809 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
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 */
9#include <linux/compiler.h>
10#include <linux/elf-randomize.h>
11#include <linux/errno.h>
12#include <linux/mm.h>
13#include <linux/mman.h>
14#include <linux/export.h>
15#include <linux/personality.h>
16#include <linux/random.h>
17#include <linux/sched/signal.h>
18#include <linux/sched/mm.h>
19
20unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */
21EXPORT_SYMBOL(shm_align_mask);
22
23/* gap between mmap and stack */
24#define MIN_GAP (128*1024*1024UL)
25#define MAX_GAP ((TASK_SIZE)/6*5)
26#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12))
27
28static int mmap_is_legacy(struct rlimit *rlim_stack)
29{
30 if (current->personality & ADDR_COMPAT_LAYOUT)
31 return 1;
32
33 if (rlim_stack->rlim_cur == RLIM_INFINITY)
34 return 1;
35
36 return sysctl_legacy_va_layout;
37}
38
39static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack)
40{
41 unsigned long gap = rlim_stack->rlim_cur;
42 unsigned long pad = stack_guard_gap;
43
44 /* Account for stack randomization if necessary */
45 if (current->flags & PF_RANDOMIZE)
46 pad += (STACK_RND_MASK << PAGE_SHIFT);
47
48 /* Values close to RLIM_INFINITY can overflow. */
49 if (gap + pad > gap)
50 gap += pad;
51
52 if (gap < MIN_GAP)
53 gap = MIN_GAP;
54 else if (gap > MAX_GAP)
55 gap = MAX_GAP;
56
57 return PAGE_ALIGN(TASK_SIZE - gap - rnd);
58}
59
60#define COLOUR_ALIGN(addr, pgoff) \
61 ((((addr) + shm_align_mask) & ~shm_align_mask) + \
62 (((pgoff) << PAGE_SHIFT) & shm_align_mask))
63
64enum mmap_allocation_direction {UP, DOWN};
65
66static unsigned long arch_get_unmapped_area_common(struct file *filp,
67 unsigned long addr0, unsigned long len, unsigned long pgoff,
68 unsigned long flags, enum mmap_allocation_direction dir)
69{
70 struct mm_struct *mm = current->mm;
71 struct vm_area_struct *vma;
72 unsigned long addr = addr0;
73 int do_color_align;
74 struct vm_unmapped_area_info info;
75
76 if (unlikely(len > TASK_SIZE))
77 return -ENOMEM;
78
79 if (flags & MAP_FIXED) {
80 /* Even MAP_FIXED mappings must reside within TASK_SIZE */
81 if (TASK_SIZE - len < addr)
82 return -EINVAL;
83
84 /*
85 * We do not accept a shared mapping if it would violate
86 * cache aliasing constraints.
87 */
88 if ((flags & MAP_SHARED) &&
89 ((addr - (pgoff << PAGE_SHIFT)) & shm_align_mask))
90 return -EINVAL;
91 return addr;
92 }
93
94 do_color_align = 0;
95 if (filp || (flags & MAP_SHARED))
96 do_color_align = 1;
97
98 /* requesting a specific address */
99 if (addr) {
100 if (do_color_align)
101 addr = COLOUR_ALIGN(addr, pgoff);
102 else
103 addr = PAGE_ALIGN(addr);
104
105 vma = find_vma(mm, addr);
106 if (TASK_SIZE - len >= addr &&
107 (!vma || addr + len <= vm_start_gap(vma)))
108 return addr;
109 }
110
111 info.length = len;
112 info.align_mask = do_color_align ? (PAGE_MASK & shm_align_mask) : 0;
113 info.align_offset = pgoff << PAGE_SHIFT;
114
115 if (dir == DOWN) {
116 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
117 info.low_limit = PAGE_SIZE;
118 info.high_limit = mm->mmap_base;
119 addr = vm_unmapped_area(&info);
120
121 if (!(addr & ~PAGE_MASK))
122 return addr;
123
124 /*
125 * A failed mmap() very likely causes application failure,
126 * so fall back to the bottom-up function here. This scenario
127 * can happen with large stack limits and large mmap()
128 * allocations.
129 */
130 }
131
132 info.flags = 0;
133 info.low_limit = mm->mmap_base;
134 info.high_limit = TASK_SIZE;
135 return vm_unmapped_area(&info);
136}
137
138unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr0,
139 unsigned long len, unsigned long pgoff, unsigned long flags)
140{
141 return arch_get_unmapped_area_common(filp,
142 addr0, len, pgoff, flags, UP);
143}
144
145/*
146 * There is no need to export this but sched.h declares the function as
147 * extern so making it static here results in an error.
148 */
149unsigned long arch_get_unmapped_area_topdown(struct file *filp,
150 unsigned long addr0, unsigned long len, unsigned long pgoff,
151 unsigned long flags)
152{
153 return arch_get_unmapped_area_common(filp,
154 addr0, len, pgoff, flags, DOWN);
155}
156
157unsigned long arch_mmap_rnd(void)
158{
159 unsigned long rnd;
160
161#ifdef CONFIG_COMPAT
162 if (TASK_IS_32BIT_ADDR)
163 rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1);
164 else
165#endif /* CONFIG_COMPAT */
166 rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1);
167
168 return rnd << PAGE_SHIFT;
169}
170
171void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack)
172{
173 unsigned long random_factor = 0UL;
174
175 if (current->flags & PF_RANDOMIZE)
176 random_factor = arch_mmap_rnd();
177
178 if (mmap_is_legacy(rlim_stack)) {
179 mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
180 mm->get_unmapped_area = arch_get_unmapped_area;
181 } else {
182 mm->mmap_base = mmap_base(random_factor, rlim_stack);
183 mm->get_unmapped_area = arch_get_unmapped_area_topdown;
184 }
185}
186
187static inline unsigned long brk_rnd(void)
188{
189 unsigned long rnd = get_random_long();
190
191 rnd = rnd << PAGE_SHIFT;
192 /* 8MB for 32bit, 256MB for 64bit */
193 if (TASK_IS_32BIT_ADDR)
194 rnd = rnd & 0x7ffffful;
195 else
196 rnd = rnd & 0xffffffful;
197
198 return rnd;
199}
200
201unsigned long arch_randomize_brk(struct mm_struct *mm)
202{
203 unsigned long base = mm->brk;
204 unsigned long ret;
205
206 ret = PAGE_ALIGN(base + brk_rnd());
207
208 if (ret < mm->brk)
209 return mm->brk;
210
211 return ret;
212}
213
214int __virt_addr_valid(const volatile void *kaddr)
215{
216 unsigned long vaddr = (unsigned long)kaddr;
217
218 if ((vaddr < PAGE_OFFSET) || (vaddr >= MAP_BASE))
219 return 0;
220
221 return pfn_valid(PFN_DOWN(virt_to_phys(kaddr)));
222}
223EXPORT_SYMBOL_GPL(__virt_addr_valid);