aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Safonov <dsafonov@virtuozzo.com>2016-06-28 07:35:38 -0400
committerIngo Molnar <mingo@kernel.org>2016-07-08 08:17:51 -0400
commitb059a453b1cf1c8453c2b2ed373d3147d6264ebd (patch)
treed06330fe10b872e9df51589e986ac43aabf8d762
parent39380b80d72723282f0ea1d1bbf2294eae45013e (diff)
x86/vdso: Add mremap hook to vm_special_mapping
Add possibility for 32-bit user-space applications to move the vDSO mapping. Previously, when a user-space app called mremap() for the vDSO address, in the syscall return path it would land on the previous address of the vDSOpage, resulting in segmentation violation. Now it lands fine and returns to userspace with a remapped vDSO. This will also fix the context.vdso pointer for 64-bit, which does not affect the user of vDSO after mremap() currently, but this may change in the future. As suggested by Andy, return -EINVAL for mremap() that would split the vDSO image: that operation cannot possibly result in a working system so reject it. Renamed and moved the text_mapping structure declaration inside map_vdso(), as it used only there and now it complements the vvar_mapping variable. There is still a problem for remapping the vDSO in glibc applications: the linker relocates addresses for syscalls on the vDSO page, so you need to relink with the new addresses. Without that the next syscall through glibc may fail: Program received signal SIGSEGV, Segmentation fault. #0 0xf7fd9b80 in __kernel_vsyscall () #1 0xf7ec8238 in _exit () from /usr/lib32/libc.so.6 Signed-off-by: Dmitry Safonov <dsafonov@virtuozzo.com> Acked-by: Andy Lutomirski <luto@kernel.org> Cc: 0x7f454c46@gmail.com Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-mm@kvack.org Link: http://lkml.kernel.org/r/20160628113539.13606-2-dsafonov@virtuozzo.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
-rw-r--r--arch/x86/entry/vdso/vma.c47
-rw-r--r--include/linux/mm_types.h3
-rw-r--r--mm/mmap.c10
3 files changed, 55 insertions, 5 deletions
diff --git a/arch/x86/entry/vdso/vma.c b/arch/x86/entry/vdso/vma.c
index ab220ac9b3b9..3329844e3c43 100644
--- a/arch/x86/entry/vdso/vma.c
+++ b/arch/x86/entry/vdso/vma.c
@@ -12,6 +12,7 @@
12#include <linux/random.h> 12#include <linux/random.h>
13#include <linux/elf.h> 13#include <linux/elf.h>
14#include <linux/cpu.h> 14#include <linux/cpu.h>
15#include <linux/ptrace.h>
15#include <asm/pvclock.h> 16#include <asm/pvclock.h>
16#include <asm/vgtod.h> 17#include <asm/vgtod.h>
17#include <asm/proto.h> 18#include <asm/proto.h>
@@ -97,10 +98,40 @@ static int vdso_fault(const struct vm_special_mapping *sm,
97 return 0; 98 return 0;
98} 99}
99 100
100static const struct vm_special_mapping text_mapping = { 101static void vdso_fix_landing(const struct vdso_image *image,
101 .name = "[vdso]", 102 struct vm_area_struct *new_vma)
102 .fault = vdso_fault, 103{
103}; 104#if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
105 if (in_ia32_syscall() && image == &vdso_image_32) {
106 struct pt_regs *regs = current_pt_regs();
107 unsigned long vdso_land = image->sym_int80_landing_pad;
108 unsigned long old_land_addr = vdso_land +
109 (unsigned long)current->mm->context.vdso;
110
111 /* Fixing userspace landing - look at do_fast_syscall_32 */
112 if (regs->ip == old_land_addr)
113 regs->ip = new_vma->vm_start + vdso_land;
114 }
115#endif
116}
117
118static int vdso_mremap(const struct vm_special_mapping *sm,
119 struct vm_area_struct *new_vma)
120{
121 unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
122 const struct vdso_image *image = current->mm->context.vdso_image;
123
124 if (image->size != new_size)
125 return -EINVAL;
126
127 if (WARN_ON_ONCE(current->mm != new_vma->vm_mm))
128 return -EFAULT;
129
130 vdso_fix_landing(image, new_vma);
131 current->mm->context.vdso = (void __user *)new_vma->vm_start;
132
133 return 0;
134}
104 135
105static int vvar_fault(const struct vm_special_mapping *sm, 136static int vvar_fault(const struct vm_special_mapping *sm,
106 struct vm_area_struct *vma, struct vm_fault *vmf) 137 struct vm_area_struct *vma, struct vm_fault *vmf)
@@ -151,6 +182,12 @@ static int map_vdso(const struct vdso_image *image, bool calculate_addr)
151 struct vm_area_struct *vma; 182 struct vm_area_struct *vma;
152 unsigned long addr, text_start; 183 unsigned long addr, text_start;
153 int ret = 0; 184 int ret = 0;
185
186 static const struct vm_special_mapping vdso_mapping = {
187 .name = "[vdso]",
188 .fault = vdso_fault,
189 .mremap = vdso_mremap,
190 };
154 static const struct vm_special_mapping vvar_mapping = { 191 static const struct vm_special_mapping vvar_mapping = {
155 .name = "[vvar]", 192 .name = "[vvar]",
156 .fault = vvar_fault, 193 .fault = vvar_fault,
@@ -185,7 +222,7 @@ static int map_vdso(const struct vdso_image *image, bool calculate_addr)
185 image->size, 222 image->size,
186 VM_READ|VM_EXEC| 223 VM_READ|VM_EXEC|
187 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, 224 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
188 &text_mapping); 225 &vdso_mapping);
189 226
190 if (IS_ERR(vma)) { 227 if (IS_ERR(vma)) {
191 ret = PTR_ERR(vma); 228 ret = PTR_ERR(vma);
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index ca3e517980a0..917f2b6a0cde 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -594,6 +594,9 @@ struct vm_special_mapping {
594 int (*fault)(const struct vm_special_mapping *sm, 594 int (*fault)(const struct vm_special_mapping *sm,
595 struct vm_area_struct *vma, 595 struct vm_area_struct *vma,
596 struct vm_fault *vmf); 596 struct vm_fault *vmf);
597
598 int (*mremap)(const struct vm_special_mapping *sm,
599 struct vm_area_struct *new_vma);
597}; 600};
598 601
599enum tlb_flush_reason { 602enum tlb_flush_reason {
diff --git a/mm/mmap.c b/mm/mmap.c
index de2c1769cc68..234edffec1d0 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -2943,9 +2943,19 @@ static const char *special_mapping_name(struct vm_area_struct *vma)
2943 return ((struct vm_special_mapping *)vma->vm_private_data)->name; 2943 return ((struct vm_special_mapping *)vma->vm_private_data)->name;
2944} 2944}
2945 2945
2946static int special_mapping_mremap(struct vm_area_struct *new_vma)
2947{
2948 struct vm_special_mapping *sm = new_vma->vm_private_data;
2949
2950 if (sm->mremap)
2951 return sm->mremap(sm, new_vma);
2952 return 0;
2953}
2954
2946static const struct vm_operations_struct special_mapping_vmops = { 2955static const struct vm_operations_struct special_mapping_vmops = {
2947 .close = special_mapping_close, 2956 .close = special_mapping_close,
2948 .fault = special_mapping_fault, 2957 .fault = special_mapping_fault,
2958 .mremap = special_mapping_mremap,
2949 .name = special_mapping_name, 2959 .name = special_mapping_name,
2950}; 2960};
2951 2961