sys_x86_64.c 6.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3
#include <linux/errno.h>
#include <linux/sched.h>
4
#include <linux/sched/mm.h>
L
Linus Torvalds 已提交
5 6
#include <linux/syscalls.h>
#include <linux/mm.h>
A
Alexey Dobriyan 已提交
7
#include <linux/fs.h>
L
Linus Torvalds 已提交
8 9 10 11 12 13 14 15 16
#include <linux/smp.h>
#include <linux/sem.h>
#include <linux/msg.h>
#include <linux/shm.h>
#include <linux/stat.h>
#include <linux/mman.h>
#include <linux/file.h>
#include <linux/utsname.h>
#include <linux/personality.h>
J
Jiri Kosina 已提交
17
#include <linux/random.h>
18
#include <linux/uaccess.h>
19
#include <linux/elf.h>
L
Linus Torvalds 已提交
20

21 22
#include <asm/elf.h>
#include <asm/compat.h>
L
Linus Torvalds 已提交
23
#include <asm/ia32.h>
24
#include <asm/syscalls.h>
25
#include <asm/mpx.h>
L
Linus Torvalds 已提交
26

27 28 29
/*
 * Align a virtual address to avoid aliasing in the I$ on AMD F15h.
 */
30
static unsigned long get_align_mask(void)
31 32 33
{
	/* handle 32- and 64-bit case with a single conditional */
	if (va_align.flags < 0 || !(va_align.flags & (2 - mmap_is_ia32())))
34
		return 0;
35 36

	if (!(current->flags & PF_RANDOMIZE))
37
		return 0;
38

39 40
	return va_align.mask;
}
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
 * To avoid aliasing in the I$ on AMD F15h, the bits defined by the
 * va_align.bits, [12:upper_bit), are set to a random value instead of
 * zeroing them. This random value is computed once per boot. This form
 * of ASLR is known as "per-boot ASLR".
 *
 * To achieve this, the random value is added to the info.align_offset
 * value before calling vm_unmapped_area() or ORed directly to the
 * address.
 */
static unsigned long get_align_bits(void)
{
	return va_align.bits & get_align_mask();
}

57 58 59
unsigned long align_vdso_addr(unsigned long addr)
{
	unsigned long align_mask = get_align_mask();
60 61
	addr = (addr + align_mask) & ~align_mask;
	return addr | get_align_bits();
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
}

static int __init control_va_addr_alignment(char *str)
{
	/* guard against enabling this on other CPU families */
	if (va_align.flags < 0)
		return 1;

	if (*str == 0)
		return 1;

	if (*str == '=')
		str++;

	if (!strcmp(str, "32"))
		va_align.flags = ALIGN_VA_32;
	else if (!strcmp(str, "64"))
		va_align.flags = ALIGN_VA_64;
	else if (!strcmp(str, "off"))
		va_align.flags = 0;
	else if (!strcmp(str, "on"))
		va_align.flags = ALIGN_VA_32 | ALIGN_VA_64;
	else
		return 0;

	return 1;
}
__setup("align_va_addr", control_va_addr_alignment);

91 92 93
SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
		unsigned long, prot, unsigned long, flags,
		unsigned long, fd, unsigned long, off)
L
Linus Torvalds 已提交
94 95 96 97 98 99
{
	long error;
	error = -EINVAL;
	if (off & ~PAGE_MASK)
		goto out;

A
Al Viro 已提交
100
	error = sys_mmap_pgoff(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
L
Linus Torvalds 已提交
101 102 103 104
out:
	return error;
}

105 106
static void find_start_end(unsigned long addr, unsigned long flags,
		unsigned long *begin, unsigned long *end)
L
Linus Torvalds 已提交
107
{
108
	if (!in_compat_syscall() && (flags & MAP_32BIT)) {
L
Linus Torvalds 已提交
109 110 111 112 113 114
		/* This is usually used needed to map code in small
		   model, so it needs to be in the first 31bit. Limit
		   it to that.  This means we need to move the
		   unmapped base down for this case. This can give
		   conflicts with the heap, but we assume that glibc
		   malloc knows how to fall back to mmap. Give it 1GB
115 116 117
		   of playground for now. -AK */
		*begin = 0x40000000;
		*end = 0x80000000;
J
Jiri Kosina 已提交
118
		if (current->flags & PF_RANDOMIZE) {
119
			*begin = randomize_page(*begin, 0x02000000);
J
Jiri Kosina 已提交
120
		}
121
		return;
122
	}
123 124

	*begin	= get_mmap_base(1);
125 126 127 128
	if (in_compat_syscall())
		*end = task_size_32bit();
	else
		*end = task_size_64bit(addr > DEFAULT_MAP_WINDOW);
129
}
L
Linus Torvalds 已提交
130 131 132 133 134 135 136

unsigned long
arch_get_unmapped_area(struct file *filp, unsigned long addr,
		unsigned long len, unsigned long pgoff, unsigned long flags)
{
	struct mm_struct *mm = current->mm;
	struct vm_area_struct *vma;
137
	struct vm_unmapped_area_info info;
L
Linus Torvalds 已提交
138
	unsigned long begin, end;
139

140 141 142 143
	addr = mpx_unmapped_area_check(addr, len, flags);
	if (IS_ERR_VALUE(addr))
		return addr;

144 145 146
	if (flags & MAP_FIXED)
		return addr;

147
	find_start_end(addr, flags, &begin, &end);
L
Linus Torvalds 已提交
148 149 150 151 152 153 154 155

	if (len > end)
		return -ENOMEM;

	if (addr) {
		addr = PAGE_ALIGN(addr);
		vma = find_vma(mm, addr);
		if (end - len >= addr &&
156
		    (!vma || addr + len <= vm_start_gap(vma)))
L
Linus Torvalds 已提交
157 158
			return addr;
	}
159

160 161 162 163
	info.flags = 0;
	info.length = len;
	info.low_limit = begin;
	info.high_limit = end;
164
	info.align_mask = 0;
165
	info.align_offset = pgoff << PAGE_SHIFT;
166 167 168 169
	if (filp) {
		info.align_mask = get_align_mask();
		info.align_offset += get_align_bits();
	}
170
	return vm_unmapped_area(&info);
L
Linus Torvalds 已提交
171 172
}

J
Jiri Kosina 已提交
173 174 175 176 177 178 179
unsigned long
arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
			  const unsigned long len, const unsigned long pgoff,
			  const unsigned long flags)
{
	struct vm_area_struct *vma;
	struct mm_struct *mm = current->mm;
180 181
	unsigned long addr = addr0;
	struct vm_unmapped_area_info info;
J
Jiri Kosina 已提交
182

183 184 185 186
	addr = mpx_unmapped_area_check(addr, len, flags);
	if (IS_ERR_VALUE(addr))
		return addr;

J
Jiri Kosina 已提交
187 188 189 190
	/* requested length too big for entire address space */
	if (len > TASK_SIZE)
		return -ENOMEM;

191
	/* No address checking. See comment at mmap_address_hint_valid() */
J
Jiri Kosina 已提交
192 193 194
	if (flags & MAP_FIXED)
		return addr;

Y
Yuanhan Liu 已提交
195
	/* for MAP_32BIT mappings we force the legacy mmap base */
196
	if (!in_compat_syscall() && (flags & MAP_32BIT))
J
Jiri Kosina 已提交
197 198 199 200
		goto bottomup;

	/* requesting a specific address */
	if (addr) {
201 202 203 204
		addr &= PAGE_MASK;
		if (!mmap_address_hint_valid(addr, len))
			goto get_unmapped_area;

J
Jiri Kosina 已提交
205
		vma = find_vma(mm, addr);
206
		if (!vma || addr + len <= vm_start_gap(vma))
J
Jiri Kosina 已提交
207 208
			return addr;
	}
209
get_unmapped_area:
J
Jiri Kosina 已提交
210

211 212 213
	info.flags = VM_UNMAPPED_AREA_TOPDOWN;
	info.length = len;
	info.low_limit = PAGE_SIZE;
214
	info.high_limit = get_mmap_base(0);
215 216 217 218 219 220 221 222 223 224

	/*
	 * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area
	 * in the full address space.
	 *
	 * !in_compat_syscall() check to avoid high addresses for x32.
	 */
	if (addr > DEFAULT_MAP_WINDOW && !in_compat_syscall())
		info.high_limit += TASK_SIZE_MAX - DEFAULT_MAP_WINDOW;

225
	info.align_mask = 0;
226
	info.align_offset = pgoff << PAGE_SHIFT;
227 228 229 230
	if (filp) {
		info.align_mask = get_align_mask();
		info.align_offset += get_align_bits();
	}
231 232 233 234
	addr = vm_unmapped_area(&info);
	if (!(addr & ~PAGE_MASK))
		return addr;
	VM_BUG_ON(addr != -ENOMEM);
235

J
Jiri Kosina 已提交
236 237 238 239 240 241 242
bottomup:
	/*
	 * A failed mmap() very likely causes application failure,
	 * so fall back to the bottom-up function here. This scenario
	 * can happen with large stack limits and large mmap()
	 * allocations.
	 */
243
	return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
J
Jiri Kosina 已提交
244
}