Skip to content

Commit 44b0491

Browse files
kirylIngo Molnar
authored andcommitted
x86/mpx: Do not allow MPX if we have mappings above 47-bit
MPX (without MAWA extension) cannot handle addresses above 47 bits, so we need to make sure that MPX cannot be enabled if we already have a VMA above the boundary and forbid creating such VMAs once MPX is enabled. The patch implements mpx_unmapped_area_check() which is called from all variants of get_unmapped_area() to check if the requested address fits mpx. On enabling MPX, we check if we already have any vma above 47-bit boundary and forbit the enabling if we do. As long as DEFAULT_MAP_WINDOW is equal to TASK_SIZE_MAX, the change is nop. It will change when we allow userspace to have mappings above 47-bits. Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Andy Lutomirski <luto@amacapital.net> Cc: Dave Hansen <dave.hansen@intel.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-arch@vger.kernel.org Cc: linux-mm@kvack.org Link: http://lkml.kernel.org/r/20170716225954.74185-6-kirill.shutemov@linux.intel.com [ Readability edits. ] Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent e8f01a8 commit 44b0491

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

arch/x86/include/asm/mpx.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ static inline void mpx_mm_init(struct mm_struct *mm)
7373
}
7474
void mpx_notify_unmap(struct mm_struct *mm, struct vm_area_struct *vma,
7575
unsigned long start, unsigned long end);
76+
77+
unsigned long mpx_unmapped_area_check(unsigned long addr, unsigned long len,
78+
unsigned long flags);
7679
#else
7780
static inline siginfo_t *mpx_generate_siginfo(struct pt_regs *regs)
7881
{
@@ -94,6 +97,12 @@ static inline void mpx_notify_unmap(struct mm_struct *mm,
9497
unsigned long start, unsigned long end)
9598
{
9699
}
100+
101+
static inline unsigned long mpx_unmapped_area_check(unsigned long addr,
102+
unsigned long len, unsigned long flags)
103+
{
104+
return addr;
105+
}
97106
#endif /* CONFIG_X86_INTEL_MPX */
98107

99108
#endif /* _ASM_X86_MPX_H */

arch/x86/include/asm/processor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ static inline void spin_lock_prefetch(const void *x)
809809
#define IA32_PAGE_OFFSET PAGE_OFFSET
810810
#define TASK_SIZE PAGE_OFFSET
811811
#define TASK_SIZE_MAX TASK_SIZE
812+
#define DEFAULT_MAP_WINDOW TASK_SIZE
812813
#define STACK_TOP TASK_SIZE
813814
#define STACK_TOP_MAX STACK_TOP
814815

@@ -850,6 +851,8 @@ static inline void spin_lock_prefetch(const void *x)
850851
*/
851852
#define TASK_SIZE_MAX ((1UL << 47) - PAGE_SIZE)
852853

854+
#define DEFAULT_MAP_WINDOW TASK_SIZE_MAX
855+
853856
/* This decides where the kernel will search for a free chunk of vm
854857
* space during mmap's.
855858
*/

arch/x86/kernel/sys_x86_64.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <asm/compat.h>
2222
#include <asm/ia32.h>
2323
#include <asm/syscalls.h>
24+
#include <asm/mpx.h>
2425

2526
/*
2627
* Align a virtual address to avoid aliasing in the I$ on AMD F15h.
@@ -132,6 +133,10 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
132133
struct vm_unmapped_area_info info;
133134
unsigned long begin, end;
134135

136+
addr = mpx_unmapped_area_check(addr, len, flags);
137+
if (IS_ERR_VALUE(addr))
138+
return addr;
139+
135140
if (flags & MAP_FIXED)
136141
return addr;
137142

@@ -171,6 +176,10 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
171176
unsigned long addr = addr0;
172177
struct vm_unmapped_area_info info;
173178

179+
addr = mpx_unmapped_area_check(addr, len, flags);
180+
if (IS_ERR_VALUE(addr))
181+
return addr;
182+
174183
/* requested length too big for entire address space */
175184
if (len > TASK_SIZE)
176185
return -ENOMEM;

arch/x86/mm/hugetlbpage.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <asm/tlbflush.h>
1919
#include <asm/pgalloc.h>
2020
#include <asm/elf.h>
21+
#include <asm/mpx.h>
2122

2223
#if 0 /* This is just for testing */
2324
struct page *
@@ -135,6 +136,11 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
135136

136137
if (len & ~huge_page_mask(h))
137138
return -EINVAL;
139+
140+
addr = mpx_unmapped_area_check(addr, len, flags);
141+
if (IS_ERR_VALUE(addr))
142+
return addr;
143+
138144
if (len > TASK_SIZE)
139145
return -ENOMEM;
140146

arch/x86/mm/mpx.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,19 @@ int mpx_enable_management(void)
355355
*/
356356
bd_base = mpx_get_bounds_dir();
357357
down_write(&mm->mmap_sem);
358+
359+
/* MPX doesn't support addresses above 47 bits yet. */
360+
if (find_vma(mm, DEFAULT_MAP_WINDOW)) {
361+
pr_warn_once("%s (%d): MPX cannot handle addresses "
362+
"above 47-bits. Disabling.",
363+
current->comm, current->pid);
364+
ret = -ENXIO;
365+
goto out;
366+
}
358367
mm->context.bd_addr = bd_base;
359368
if (mm->context.bd_addr == MPX_INVALID_BOUNDS_DIR)
360369
ret = -ENXIO;
361-
370+
out:
362371
up_write(&mm->mmap_sem);
363372
return ret;
364373
}
@@ -1030,3 +1039,25 @@ void mpx_notify_unmap(struct mm_struct *mm, struct vm_area_struct *vma,
10301039
if (ret)
10311040
force_sig(SIGSEGV, current);
10321041
}
1042+
1043+
/* MPX cannot handle addresses above 47 bits yet. */
1044+
unsigned long mpx_unmapped_area_check(unsigned long addr, unsigned long len,
1045+
unsigned long flags)
1046+
{
1047+
if (!kernel_managing_mpx_tables(current->mm))
1048+
return addr;
1049+
if (addr + len <= DEFAULT_MAP_WINDOW)
1050+
return addr;
1051+
if (flags & MAP_FIXED)
1052+
return -ENOMEM;
1053+
1054+
/*
1055+
* Requested len is larger than the whole area we're allowed to map in.
1056+
* Resetting hinting address wouldn't do much good -- fail early.
1057+
*/
1058+
if (len > DEFAULT_MAP_WINDOW)
1059+
return -ENOMEM;
1060+
1061+
/* Look for unmap area within DEFAULT_MAP_WINDOW */
1062+
return 0;
1063+
}

0 commit comments

Comments
 (0)