1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compat.h>
3 #include <linux/errno.h>
4 #include <linux/sched.h>
5 #include <linux/sched/mm.h>
6 #include <linux/syscalls.h>
7 #include <linux/mm.h>
8 #include <linux/fs.h>
9 #include <linux/smp.h>
10 #include <linux/sem.h>
11 #include <linux/msg.h>
12 #include <linux/shm.h>
13 #include <linux/stat.h>
14 #include <linux/mman.h>
15 #include <linux/file.h>
16 #include <linux/utsname.h>
17 #include <linux/personality.h>
18 #include <linux/random.h>
19 #include <linux/uaccess.h>
20 #include <linux/elf.h>
21 #include <linux/hugetlb.h>
22
23 #include <asm/elf.h>
24 #include <asm/ia32.h>
25
26 /*
27 * Align a virtual address to avoid aliasing in the I$ on AMD F15h.
28 */
get_align_mask(struct file * filp)29 static unsigned long get_align_mask(struct file *filp)
30 {
31 if (filp && is_file_hugepages(filp))
32 return huge_page_mask_align(filp);
33 /* handle 32- and 64-bit case with a single conditional */
34 if (va_align.flags < 0 || !(va_align.flags & (2 - mmap_is_ia32())))
35 return 0;
36
37 if (!(current->flags & PF_RANDOMIZE))
38 return 0;
39
40 return va_align.mask;
41 }
42
43 /*
44 * To avoid aliasing in the I$ on AMD F15h, the bits defined by the
45 * va_align.bits, [12:upper_bit), are set to a random value instead of
46 * zeroing them. This random value is computed once per boot. This form
47 * of ASLR is known as "per-boot ASLR".
48 *
49 * To achieve this, the random value is added to the info.align_offset
50 * value before calling vm_unmapped_area() or ORed directly to the
51 * address.
52 */
get_align_bits(struct file * filp)53 static unsigned long get_align_bits(struct file *filp)
54 {
55 /*
56 * va_align.bits is smaller than the huge page size and will
57 * lead to misaligned huge pages. Ignore it for huge mappings.
58 */
59 if (is_file_hugepages(filp))
60 return 0;
61
62 return va_align.bits & get_align_mask(filp);
63 }
64
control_va_addr_alignment(char * str)65 static int __init control_va_addr_alignment(char *str)
66 {
67 /* guard against enabling this on other CPU families */
68 if (va_align.flags < 0)
69 return 1;
70
71 if (*str == 0)
72 return 1;
73
74 if (!strcmp(str, "32"))
75 va_align.flags = ALIGN_VA_32;
76 else if (!strcmp(str, "64"))
77 va_align.flags = ALIGN_VA_64;
78 else if (!strcmp(str, "off"))
79 va_align.flags = 0;
80 else if (!strcmp(str, "on"))
81 va_align.flags = ALIGN_VA_32 | ALIGN_VA_64;
82 else
83 pr_warn("invalid option value: 'align_va_addr=%s'\n", str);
84
85 return 1;
86 }
87 __setup("align_va_addr=", control_va_addr_alignment);
88
SYSCALL_DEFINE6(mmap,unsigned long,addr,unsigned long,len,unsigned long,prot,unsigned long,flags,unsigned long,fd,unsigned long,off)89 SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
90 unsigned long, prot, unsigned long, flags,
91 unsigned long, fd, unsigned long, off)
92 {
93 if (off & ~PAGE_MASK)
94 return -EINVAL;
95
96 return ksys_mmap_pgoff(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
97 }
98
find_start_end(unsigned long addr,unsigned long flags,unsigned long * begin,unsigned long * end)99 static void find_start_end(unsigned long addr, unsigned long flags,
100 unsigned long *begin, unsigned long *end)
101 {
102 if (!in_32bit_syscall() && (flags & MAP_32BIT)) {
103 /* This is usually used needed to map code in small
104 model, so it needs to be in the first 31bit. Limit
105 it to that. This means we need to move the
106 unmapped base down for this case. This can give
107 conflicts with the heap, but we assume that glibc
108 malloc knows how to fall back to mmap. Give it 1GB
109 of playground for now. -AK */
110 *begin = 0x40000000;
111 *end = 0x80000000;
112 if (current->flags & PF_RANDOMIZE) {
113 *begin = randomize_page(*begin, 0x02000000);
114 }
115 return;
116 }
117
118 *begin = get_mmap_base(1);
119 if (in_32bit_syscall())
120 *end = task_size_32bit();
121 else
122 *end = task_size_64bit(addr > DEFAULT_MAP_WINDOW);
123 }
124
stack_guard_placement(vm_flags_t vm_flags)125 static inline unsigned long stack_guard_placement(vm_flags_t vm_flags)
126 {
127 if (vm_flags & VM_SHADOW_STACK)
128 return PAGE_SIZE;
129
130 return 0;
131 }
132
133 unsigned long
arch_get_unmapped_area(struct file * filp,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags,vm_flags_t vm_flags)134 arch_get_unmapped_area(struct file *filp, unsigned long addr, unsigned long len,
135 unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags)
136 {
137 struct mm_struct *mm = current->mm;
138 struct vm_area_struct *vma;
139 struct vm_unmapped_area_info info = {};
140 unsigned long begin, end;
141
142 if (flags & MAP_FIXED)
143 return addr;
144
145 find_start_end(addr, flags, &begin, &end);
146
147 if (len > end)
148 return -ENOMEM;
149
150 if (addr) {
151 addr = PAGE_ALIGN(addr);
152 vma = find_vma(mm, addr);
153 if (end - len >= addr &&
154 (!vma || addr + len <= vm_start_gap(vma)))
155 return addr;
156 }
157
158 info.length = len;
159 info.low_limit = begin;
160 info.high_limit = end;
161 if (!(filp && is_file_hugepages(filp))) {
162 info.align_offset = pgoff << PAGE_SHIFT;
163 info.start_gap = stack_guard_placement(vm_flags);
164 }
165 if (filp) {
166 info.align_mask = get_align_mask(filp);
167 info.align_offset += get_align_bits(filp);
168 }
169
170 return vm_unmapped_area(&info);
171 }
172
173 unsigned long
arch_get_unmapped_area_topdown(struct file * filp,unsigned long addr0,unsigned long len,unsigned long pgoff,unsigned long flags,vm_flags_t vm_flags)174 arch_get_unmapped_area_topdown(struct file *filp, unsigned long addr0,
175 unsigned long len, unsigned long pgoff,
176 unsigned long flags, vm_flags_t vm_flags)
177 {
178 struct vm_area_struct *vma;
179 struct mm_struct *mm = current->mm;
180 unsigned long addr = addr0;
181 struct vm_unmapped_area_info info = {};
182
183 /* requested length too big for entire address space */
184 if (len > TASK_SIZE)
185 return -ENOMEM;
186
187 /* No address checking. See comment at mmap_address_hint_valid() */
188 if (flags & MAP_FIXED)
189 return addr;
190
191 /* for MAP_32BIT mappings we force the legacy mmap base */
192 if (!in_32bit_syscall() && (flags & MAP_32BIT))
193 goto bottomup;
194
195 /* requesting a specific address */
196 if (addr) {
197 addr &= PAGE_MASK;
198 if (!mmap_address_hint_valid(addr, len))
199 goto get_unmapped_area;
200
201 vma = find_vma(mm, addr);
202 if (!vma || addr + len <= vm_start_gap(vma))
203 return addr;
204 }
205 get_unmapped_area:
206
207 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
208 info.length = len;
209 if (!in_32bit_syscall() && (flags & MAP_ABOVE4G))
210 info.low_limit = SZ_4G;
211 else
212 info.low_limit = PAGE_SIZE;
213
214 info.high_limit = get_mmap_base(0);
215 if (!(filp && is_file_hugepages(filp))) {
216 info.start_gap = stack_guard_placement(vm_flags);
217 info.align_offset = pgoff << PAGE_SHIFT;
218 }
219
220 /*
221 * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area
222 * in the full address space.
223 *
224 * !in_32bit_syscall() check to avoid high addresses for x32
225 * (and make it no op on native i386).
226 */
227 if (addr > DEFAULT_MAP_WINDOW && !in_32bit_syscall())
228 info.high_limit += TASK_SIZE_MAX - DEFAULT_MAP_WINDOW;
229
230 if (filp) {
231 info.align_mask = get_align_mask(filp);
232 info.align_offset += get_align_bits(filp);
233 }
234 addr = vm_unmapped_area(&info);
235 if (!(addr & ~PAGE_MASK))
236 return addr;
237 VM_BUG_ON(addr != -ENOMEM);
238
239 bottomup:
240 /*
241 * A failed mmap() very likely causes application failure,
242 * so fall back to the bottom-up function here. This scenario
243 * can happen with large stack limits and large mmap()
244 * allocations.
245 */
246 return arch_get_unmapped_area(filp, addr0, len, pgoff, flags, 0);
247 }
248