Re: [PATCHv5 3/3] selftest/x86: add mremap vdso 32-bit test

2016-04-22 Thread Dmitry Safonov

On 04/21/2016 11:01 PM, Andy Lutomirski wrote:

On Mon, Apr 18, 2016 at 6:43 AM, Dmitry Safonov  wrote:

Should print on success:
[root@localhost ~]# ./test_mremap_vdso_32
 AT_SYSINFO_EHDR is 0xf773f000
[NOTE]  Moving vDSO: [f773f000, f774] -> [a00, a001000]
[OK]
Or segfault if landing was bad (before patches):
[root@localhost ~]# ./test_mremap_vdso_32
 AT_SYSINFO_EHDR is 0xf774f000
[NOTE]  Moving vDSO: [f774f000, f775] -> [a00, a001000]
Segmentation fault (core dumped)

Cc: Shuah Khan 
Cc: linux-kselft...@vger.kernel.org
Suggested-by: Andy Lutomirski 
Signed-off-by: Dmitry Safonov 
---
v5: initial version

  tools/testing/selftests/x86/Makefile   |  2 +-
  tools/testing/selftests/x86/test_mremap_vdso.c | 72 ++
  2 files changed, 73 insertions(+), 1 deletion(-)
  create mode 100644 tools/testing/selftests/x86/test_mremap_vdso.c

diff --git a/tools/testing/selftests/x86/Makefile 
b/tools/testing/selftests/x86/Makefile
index b47ebd170690..c7162b511ab0 100644
--- a/tools/testing/selftests/x86/Makefile
+++ b/tools/testing/selftests/x86/Makefile
@@ -7,7 +7,7 @@ include ../lib.mk
  TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt 
ptrace_syscall \
 check_initial_reg_state sigreturn ldt_gdt iopl
  TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault test_syscall_vdso 
unwind_vdso \
-   test_FCMOV test_FCOMI test_FISTTP \
+   test_FCMOV test_FCOMI test_FISTTP test_mremap_vdso \
 vdso_restorer

  TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY)
diff --git a/tools/testing/selftests/x86/test_mremap_vdso.c 
b/tools/testing/selftests/x86/test_mremap_vdso.c
new file mode 100644
index ..a470790e2118
--- /dev/null
+++ b/tools/testing/selftests/x86/test_mremap_vdso.c
@@ -0,0 +1,72 @@
+/*
+ * 32-bit test to check vdso mremap.
+ *
+ * Copyright (c) 2016 Dmitry Safonov
+ * Suggested-by: Andrew Lutomirski
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+/*
+ * Can be built statically:
+ * gcc -Os -Wall -static -m32 test_mremap_vdso.c
+ */
+#define _GNU_SOURCE
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+
+#if !defined(__i386__)
+int main(int argc, char **argv, char **envp)
+{
+   printf("[SKIP]\tNot a 32-bit x86 userspace\n");
+   return 0;

What's wrong with testing on 64-bit systems?


Ok, will drop this.


+}
+#else
+
+#define PAGE_SIZE  4096
+#define VDSO_SIZE  PAGE_SIZE

The vdso is frequently bigger than a page.


Ok, will enlarge this. Are two pages big enough?


+
+int main(int argc, char **argv, char **envp)
+{
+   unsigned long vdso_addr, dest_addr;
+   void *new_addr;
+   const char *ok_string = "[OK]\n";
+
+   vdso_addr = getauxval(AT_SYSINFO_EHDR);
+   printf("\tAT_SYSINFO_EHDR is 0x%lx\n", vdso_addr);
+   if (!vdso_addr || vdso_addr == -ENOENT) {
+   printf("[FAIL]\tgetauxval failed\n");
+   return 1;

Let's make this [WARN] and return 0.  The vdso is optional, and
getauxval is missing on many systems.


Ok


+   }
+
+   /* to low for stack, to high for lib/data/code mappings */
+   dest_addr = 0x0a00;

This could be make reliable -- map a big enough area PROT_NONE and use
that address.


Oh, that's good, will do.


+   printf("[NOTE]\tMoving vDSO: [%lx, %lx] -> [%lx, %lx]\n",
+   vdso_addr, vdso_addr + VDSO_SIZE,
+   dest_addr, dest_addr + VDSO_SIZE);

fflush(stdout), please, for the benefit of test harnesses that use pipes.


Will add.


+   new_addr = mremap((void *)vdso_addr, VDSO_SIZE, VDSO_SIZE,
+   MREMAP_FIXED|MREMAP_MAYMOVE, dest_addr);
+   if ((unsigned long)new_addr == (unsigned long)-1) {
+   printf("[FAIL]\tmremap failed (%d): %m\n", errno);
+   return 1;
+   }
+
+   asm volatile ("int $0x80" : : "a" (__NR_write), "b" (STDOUT_FILENO),
+   "c" (ok_string), "d" (strlen(ok_string)));
+   asm volatile ("int $0x80" : : "a" (__NR_exit), "b" (0));
+
+   return 0;
+}
+#endif
--
2.8.0







--
Regards,
Dmitry Safonov



Re: [PATCHv5 3/3] selftest/x86: add mremap vdso 32-bit test

2016-04-22 Thread Dmitry Safonov

On 04/21/2016 11:01 PM, Andy Lutomirski wrote:

On Mon, Apr 18, 2016 at 6:43 AM, Dmitry Safonov  wrote:

Should print on success:
[root@localhost ~]# ./test_mremap_vdso_32
 AT_SYSINFO_EHDR is 0xf773f000
[NOTE]  Moving vDSO: [f773f000, f774] -> [a00, a001000]
[OK]
Or segfault if landing was bad (before patches):
[root@localhost ~]# ./test_mremap_vdso_32
 AT_SYSINFO_EHDR is 0xf774f000
[NOTE]  Moving vDSO: [f774f000, f775] -> [a00, a001000]
Segmentation fault (core dumped)

Cc: Shuah Khan 
Cc: linux-kselft...@vger.kernel.org
Suggested-by: Andy Lutomirski 
Signed-off-by: Dmitry Safonov 
---
v5: initial version

  tools/testing/selftests/x86/Makefile   |  2 +-
  tools/testing/selftests/x86/test_mremap_vdso.c | 72 ++
  2 files changed, 73 insertions(+), 1 deletion(-)
  create mode 100644 tools/testing/selftests/x86/test_mremap_vdso.c

diff --git a/tools/testing/selftests/x86/Makefile 
b/tools/testing/selftests/x86/Makefile
index b47ebd170690..c7162b511ab0 100644
--- a/tools/testing/selftests/x86/Makefile
+++ b/tools/testing/selftests/x86/Makefile
@@ -7,7 +7,7 @@ include ../lib.mk
  TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt 
ptrace_syscall \
 check_initial_reg_state sigreturn ldt_gdt iopl
  TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault test_syscall_vdso 
unwind_vdso \
-   test_FCMOV test_FCOMI test_FISTTP \
+   test_FCMOV test_FCOMI test_FISTTP test_mremap_vdso \
 vdso_restorer

  TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY)
diff --git a/tools/testing/selftests/x86/test_mremap_vdso.c 
b/tools/testing/selftests/x86/test_mremap_vdso.c
new file mode 100644
index ..a470790e2118
--- /dev/null
+++ b/tools/testing/selftests/x86/test_mremap_vdso.c
@@ -0,0 +1,72 @@
+/*
+ * 32-bit test to check vdso mremap.
+ *
+ * Copyright (c) 2016 Dmitry Safonov
+ * Suggested-by: Andrew Lutomirski
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+/*
+ * Can be built statically:
+ * gcc -Os -Wall -static -m32 test_mremap_vdso.c
+ */
+#define _GNU_SOURCE
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+
+#if !defined(__i386__)
+int main(int argc, char **argv, char **envp)
+{
+   printf("[SKIP]\tNot a 32-bit x86 userspace\n");
+   return 0;

What's wrong with testing on 64-bit systems?


Ok, will drop this.


+}
+#else
+
+#define PAGE_SIZE  4096
+#define VDSO_SIZE  PAGE_SIZE

The vdso is frequently bigger than a page.


Ok, will enlarge this. Are two pages big enough?


+
+int main(int argc, char **argv, char **envp)
+{
+   unsigned long vdso_addr, dest_addr;
+   void *new_addr;
+   const char *ok_string = "[OK]\n";
+
+   vdso_addr = getauxval(AT_SYSINFO_EHDR);
+   printf("\tAT_SYSINFO_EHDR is 0x%lx\n", vdso_addr);
+   if (!vdso_addr || vdso_addr == -ENOENT) {
+   printf("[FAIL]\tgetauxval failed\n");
+   return 1;

Let's make this [WARN] and return 0.  The vdso is optional, and
getauxval is missing on many systems.


Ok


+   }
+
+   /* to low for stack, to high for lib/data/code mappings */
+   dest_addr = 0x0a00;

This could be make reliable -- map a big enough area PROT_NONE and use
that address.


Oh, that's good, will do.


+   printf("[NOTE]\tMoving vDSO: [%lx, %lx] -> [%lx, %lx]\n",
+   vdso_addr, vdso_addr + VDSO_SIZE,
+   dest_addr, dest_addr + VDSO_SIZE);

fflush(stdout), please, for the benefit of test harnesses that use pipes.


Will add.


+   new_addr = mremap((void *)vdso_addr, VDSO_SIZE, VDSO_SIZE,
+   MREMAP_FIXED|MREMAP_MAYMOVE, dest_addr);
+   if ((unsigned long)new_addr == (unsigned long)-1) {
+   printf("[FAIL]\tmremap failed (%d): %m\n", errno);
+   return 1;
+   }
+
+   asm volatile ("int $0x80" : : "a" (__NR_write), "b" (STDOUT_FILENO),
+   "c" (ok_string), "d" (strlen(ok_string)));
+   asm volatile ("int $0x80" : : "a" (__NR_exit), "b" (0));
+
+   return 0;
+}
+#endif
--
2.8.0







--
Regards,
Dmitry Safonov



Re: [PATCHv5 3/3] selftest/x86: add mremap vdso 32-bit test

2016-04-21 Thread Andy Lutomirski
On Mon, Apr 18, 2016 at 6:43 AM, Dmitry Safonov  wrote:
> Should print on success:
> [root@localhost ~]# ./test_mremap_vdso_32
> AT_SYSINFO_EHDR is 0xf773f000
> [NOTE]  Moving vDSO: [f773f000, f774] -> [a00, a001000]
> [OK]
> Or segfault if landing was bad (before patches):
> [root@localhost ~]# ./test_mremap_vdso_32
> AT_SYSINFO_EHDR is 0xf774f000
> [NOTE]  Moving vDSO: [f774f000, f775] -> [a00, a001000]
> Segmentation fault (core dumped)
>
> Cc: Shuah Khan 
> Cc: linux-kselft...@vger.kernel.org
> Suggested-by: Andy Lutomirski 
> Signed-off-by: Dmitry Safonov 
> ---
> v5: initial version
>
>  tools/testing/selftests/x86/Makefile   |  2 +-
>  tools/testing/selftests/x86/test_mremap_vdso.c | 72 
> ++
>  2 files changed, 73 insertions(+), 1 deletion(-)
>  create mode 100644 tools/testing/selftests/x86/test_mremap_vdso.c
>
> diff --git a/tools/testing/selftests/x86/Makefile 
> b/tools/testing/selftests/x86/Makefile
> index b47ebd170690..c7162b511ab0 100644
> --- a/tools/testing/selftests/x86/Makefile
> +++ b/tools/testing/selftests/x86/Makefile
> @@ -7,7 +7,7 @@ include ../lib.mk
>  TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt 
> ptrace_syscall \
> check_initial_reg_state sigreturn ldt_gdt iopl
>  TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault test_syscall_vdso 
> unwind_vdso \
> -   test_FCMOV test_FCOMI test_FISTTP \
> +   test_FCMOV test_FCOMI test_FISTTP test_mremap_vdso \
> vdso_restorer
>
>  TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY)
> diff --git a/tools/testing/selftests/x86/test_mremap_vdso.c 
> b/tools/testing/selftests/x86/test_mremap_vdso.c
> new file mode 100644
> index ..a470790e2118
> --- /dev/null
> +++ b/tools/testing/selftests/x86/test_mremap_vdso.c
> @@ -0,0 +1,72 @@
> +/*
> + * 32-bit test to check vdso mremap.
> + *
> + * Copyright (c) 2016 Dmitry Safonov
> + * Suggested-by: Andrew Lutomirski
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms and conditions of the GNU General Public License,
> + * version 2, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope it will be useful, but
> + * WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * General Public License for more details.
> + */
> +/*
> + * Can be built statically:
> + * gcc -Os -Wall -static -m32 test_mremap_vdso.c
> + */
> +#define _GNU_SOURCE
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#include 
> +#include 
> +#include 
> +
> +#if !defined(__i386__)
> +int main(int argc, char **argv, char **envp)
> +{
> +   printf("[SKIP]\tNot a 32-bit x86 userspace\n");
> +   return 0;

What's wrong with testing on 64-bit systems?

> +}
> +#else
> +
> +#define PAGE_SIZE  4096
> +#define VDSO_SIZE  PAGE_SIZE

The vdso is frequently bigger than a page.

> +
> +int main(int argc, char **argv, char **envp)
> +{
> +   unsigned long vdso_addr, dest_addr;
> +   void *new_addr;
> +   const char *ok_string = "[OK]\n";
> +
> +   vdso_addr = getauxval(AT_SYSINFO_EHDR);
> +   printf("\tAT_SYSINFO_EHDR is 0x%lx\n", vdso_addr);
> +   if (!vdso_addr || vdso_addr == -ENOENT) {
> +   printf("[FAIL]\tgetauxval failed\n");
> +   return 1;

Let's make this [WARN] and return 0.  The vdso is optional, and
getauxval is missing on many systems.


> +   }
> +
> +   /* to low for stack, to high for lib/data/code mappings */
> +   dest_addr = 0x0a00;

This could be make reliable -- map a big enough area PROT_NONE and use
that address.

> +   printf("[NOTE]\tMoving vDSO: [%lx, %lx] -> [%lx, %lx]\n",
> +   vdso_addr, vdso_addr + VDSO_SIZE,
> +   dest_addr, dest_addr + VDSO_SIZE);

fflush(stdout), please, for the benefit of test harnesses that use pipes.

--Andy

> +   new_addr = mremap((void *)vdso_addr, VDSO_SIZE, VDSO_SIZE,
> +   MREMAP_FIXED|MREMAP_MAYMOVE, dest_addr);
> +   if ((unsigned long)new_addr == (unsigned long)-1) {
> +   printf("[FAIL]\tmremap failed (%d): %m\n", errno);
> +   return 1;
> +   }
> +
> +   asm volatile ("int $0x80" : : "a" (__NR_write), "b" (STDOUT_FILENO),
> +   "c" (ok_string), "d" (strlen(ok_string)));
> +   asm volatile ("int $0x80" : : "a" (__NR_exit), "b" (0));
> +
> +   return 0;
> +}
> +#endif
> --
> 2.8.0
>



-- 
Andy Lutomirski
AMA Capital Management, LLC


Re: [PATCHv5 3/3] selftest/x86: add mremap vdso 32-bit test

2016-04-21 Thread Andy Lutomirski
On Mon, Apr 18, 2016 at 6:43 AM, Dmitry Safonov  wrote:
> Should print on success:
> [root@localhost ~]# ./test_mremap_vdso_32
> AT_SYSINFO_EHDR is 0xf773f000
> [NOTE]  Moving vDSO: [f773f000, f774] -> [a00, a001000]
> [OK]
> Or segfault if landing was bad (before patches):
> [root@localhost ~]# ./test_mremap_vdso_32
> AT_SYSINFO_EHDR is 0xf774f000
> [NOTE]  Moving vDSO: [f774f000, f775] -> [a00, a001000]
> Segmentation fault (core dumped)
>
> Cc: Shuah Khan 
> Cc: linux-kselft...@vger.kernel.org
> Suggested-by: Andy Lutomirski 
> Signed-off-by: Dmitry Safonov 
> ---
> v5: initial version
>
>  tools/testing/selftests/x86/Makefile   |  2 +-
>  tools/testing/selftests/x86/test_mremap_vdso.c | 72 
> ++
>  2 files changed, 73 insertions(+), 1 deletion(-)
>  create mode 100644 tools/testing/selftests/x86/test_mremap_vdso.c
>
> diff --git a/tools/testing/selftests/x86/Makefile 
> b/tools/testing/selftests/x86/Makefile
> index b47ebd170690..c7162b511ab0 100644
> --- a/tools/testing/selftests/x86/Makefile
> +++ b/tools/testing/selftests/x86/Makefile
> @@ -7,7 +7,7 @@ include ../lib.mk
>  TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt 
> ptrace_syscall \
> check_initial_reg_state sigreturn ldt_gdt iopl
>  TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault test_syscall_vdso 
> unwind_vdso \
> -   test_FCMOV test_FCOMI test_FISTTP \
> +   test_FCMOV test_FCOMI test_FISTTP test_mremap_vdso \
> vdso_restorer
>
>  TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY)
> diff --git a/tools/testing/selftests/x86/test_mremap_vdso.c 
> b/tools/testing/selftests/x86/test_mremap_vdso.c
> new file mode 100644
> index ..a470790e2118
> --- /dev/null
> +++ b/tools/testing/selftests/x86/test_mremap_vdso.c
> @@ -0,0 +1,72 @@
> +/*
> + * 32-bit test to check vdso mremap.
> + *
> + * Copyright (c) 2016 Dmitry Safonov
> + * Suggested-by: Andrew Lutomirski
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms and conditions of the GNU General Public License,
> + * version 2, as published by the Free Software Foundation.
> + *
> + * This program is distributed in the hope it will be useful, but
> + * WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * General Public License for more details.
> + */
> +/*
> + * Can be built statically:
> + * gcc -Os -Wall -static -m32 test_mremap_vdso.c
> + */
> +#define _GNU_SOURCE
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#include 
> +#include 
> +#include 
> +
> +#if !defined(__i386__)
> +int main(int argc, char **argv, char **envp)
> +{
> +   printf("[SKIP]\tNot a 32-bit x86 userspace\n");
> +   return 0;

What's wrong with testing on 64-bit systems?

> +}
> +#else
> +
> +#define PAGE_SIZE  4096
> +#define VDSO_SIZE  PAGE_SIZE

The vdso is frequently bigger than a page.

> +
> +int main(int argc, char **argv, char **envp)
> +{
> +   unsigned long vdso_addr, dest_addr;
> +   void *new_addr;
> +   const char *ok_string = "[OK]\n";
> +
> +   vdso_addr = getauxval(AT_SYSINFO_EHDR);
> +   printf("\tAT_SYSINFO_EHDR is 0x%lx\n", vdso_addr);
> +   if (!vdso_addr || vdso_addr == -ENOENT) {
> +   printf("[FAIL]\tgetauxval failed\n");
> +   return 1;

Let's make this [WARN] and return 0.  The vdso is optional, and
getauxval is missing on many systems.


> +   }
> +
> +   /* to low for stack, to high for lib/data/code mappings */
> +   dest_addr = 0x0a00;

This could be make reliable -- map a big enough area PROT_NONE and use
that address.

> +   printf("[NOTE]\tMoving vDSO: [%lx, %lx] -> [%lx, %lx]\n",
> +   vdso_addr, vdso_addr + VDSO_SIZE,
> +   dest_addr, dest_addr + VDSO_SIZE);

fflush(stdout), please, for the benefit of test harnesses that use pipes.

--Andy

> +   new_addr = mremap((void *)vdso_addr, VDSO_SIZE, VDSO_SIZE,
> +   MREMAP_FIXED|MREMAP_MAYMOVE, dest_addr);
> +   if ((unsigned long)new_addr == (unsigned long)-1) {
> +   printf("[FAIL]\tmremap failed (%d): %m\n", errno);
> +   return 1;
> +   }
> +
> +   asm volatile ("int $0x80" : : "a" (__NR_write), "b" (STDOUT_FILENO),
> +   "c" (ok_string), "d" (strlen(ok_string)));
> +   asm volatile ("int $0x80" : : "a" (__NR_exit), "b" (0));
> +
> +   return 0;
> +}
> +#endif
> --
> 2.8.0
>



-- 
Andy Lutomirski
AMA Capital Management, LLC


[PATCHv5 3/3] selftest/x86: add mremap vdso 32-bit test

2016-04-18 Thread Dmitry Safonov
Should print on success:
[root@localhost ~]# ./test_mremap_vdso_32
AT_SYSINFO_EHDR is 0xf773f000
[NOTE]  Moving vDSO: [f773f000, f774] -> [a00, a001000]
[OK]
Or segfault if landing was bad (before patches):
[root@localhost ~]# ./test_mremap_vdso_32
AT_SYSINFO_EHDR is 0xf774f000
[NOTE]  Moving vDSO: [f774f000, f775] -> [a00, a001000]
Segmentation fault (core dumped)

Cc: Shuah Khan 
Cc: linux-kselft...@vger.kernel.org
Suggested-by: Andy Lutomirski 
Signed-off-by: Dmitry Safonov 
---
v5: initial version

 tools/testing/selftests/x86/Makefile   |  2 +-
 tools/testing/selftests/x86/test_mremap_vdso.c | 72 ++
 2 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/x86/test_mremap_vdso.c

diff --git a/tools/testing/selftests/x86/Makefile 
b/tools/testing/selftests/x86/Makefile
index b47ebd170690..c7162b511ab0 100644
--- a/tools/testing/selftests/x86/Makefile
+++ b/tools/testing/selftests/x86/Makefile
@@ -7,7 +7,7 @@ include ../lib.mk
 TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt 
ptrace_syscall \
check_initial_reg_state sigreturn ldt_gdt iopl
 TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault test_syscall_vdso 
unwind_vdso \
-   test_FCMOV test_FCOMI test_FISTTP \
+   test_FCMOV test_FCOMI test_FISTTP test_mremap_vdso \
vdso_restorer
 
 TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY)
diff --git a/tools/testing/selftests/x86/test_mremap_vdso.c 
b/tools/testing/selftests/x86/test_mremap_vdso.c
new file mode 100644
index ..a470790e2118
--- /dev/null
+++ b/tools/testing/selftests/x86/test_mremap_vdso.c
@@ -0,0 +1,72 @@
+/*
+ * 32-bit test to check vdso mremap.
+ *
+ * Copyright (c) 2016 Dmitry Safonov
+ * Suggested-by: Andrew Lutomirski
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+/*
+ * Can be built statically:
+ * gcc -Os -Wall -static -m32 test_mremap_vdso.c
+ */
+#define _GNU_SOURCE
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+
+#if !defined(__i386__)
+int main(int argc, char **argv, char **envp)
+{
+   printf("[SKIP]\tNot a 32-bit x86 userspace\n");
+   return 0;
+}
+#else
+
+#define PAGE_SIZE  4096
+#define VDSO_SIZE  PAGE_SIZE
+
+int main(int argc, char **argv, char **envp)
+{
+   unsigned long vdso_addr, dest_addr;
+   void *new_addr;
+   const char *ok_string = "[OK]\n";
+
+   vdso_addr = getauxval(AT_SYSINFO_EHDR);
+   printf("\tAT_SYSINFO_EHDR is 0x%lx\n", vdso_addr);
+   if (!vdso_addr || vdso_addr == -ENOENT) {
+   printf("[FAIL]\tgetauxval failed\n");
+   return 1;
+   }
+
+   /* to low for stack, to high for lib/data/code mappings */
+   dest_addr = 0x0a00;
+   printf("[NOTE]\tMoving vDSO: [%lx, %lx] -> [%lx, %lx]\n",
+   vdso_addr, vdso_addr + VDSO_SIZE,
+   dest_addr, dest_addr + VDSO_SIZE);
+   new_addr = mremap((void *)vdso_addr, VDSO_SIZE, VDSO_SIZE,
+   MREMAP_FIXED|MREMAP_MAYMOVE, dest_addr);
+   if ((unsigned long)new_addr == (unsigned long)-1) {
+   printf("[FAIL]\tmremap failed (%d): %m\n", errno);
+   return 1;
+   }
+
+   asm volatile ("int $0x80" : : "a" (__NR_write), "b" (STDOUT_FILENO),
+   "c" (ok_string), "d" (strlen(ok_string)));
+   asm volatile ("int $0x80" : : "a" (__NR_exit), "b" (0));
+
+   return 0;
+}
+#endif
-- 
2.8.0



[PATCHv5 3/3] selftest/x86: add mremap vdso 32-bit test

2016-04-18 Thread Dmitry Safonov
Should print on success:
[root@localhost ~]# ./test_mremap_vdso_32
AT_SYSINFO_EHDR is 0xf773f000
[NOTE]  Moving vDSO: [f773f000, f774] -> [a00, a001000]
[OK]
Or segfault if landing was bad (before patches):
[root@localhost ~]# ./test_mremap_vdso_32
AT_SYSINFO_EHDR is 0xf774f000
[NOTE]  Moving vDSO: [f774f000, f775] -> [a00, a001000]
Segmentation fault (core dumped)

Cc: Shuah Khan 
Cc: linux-kselft...@vger.kernel.org
Suggested-by: Andy Lutomirski 
Signed-off-by: Dmitry Safonov 
---
v5: initial version

 tools/testing/selftests/x86/Makefile   |  2 +-
 tools/testing/selftests/x86/test_mremap_vdso.c | 72 ++
 2 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/x86/test_mremap_vdso.c

diff --git a/tools/testing/selftests/x86/Makefile 
b/tools/testing/selftests/x86/Makefile
index b47ebd170690..c7162b511ab0 100644
--- a/tools/testing/selftests/x86/Makefile
+++ b/tools/testing/selftests/x86/Makefile
@@ -7,7 +7,7 @@ include ../lib.mk
 TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt 
ptrace_syscall \
check_initial_reg_state sigreturn ldt_gdt iopl
 TARGETS_C_32BIT_ONLY := entry_from_vm86 syscall_arg_fault test_syscall_vdso 
unwind_vdso \
-   test_FCMOV test_FCOMI test_FISTTP \
+   test_FCMOV test_FCOMI test_FISTTP test_mremap_vdso \
vdso_restorer
 
 TARGETS_C_32BIT_ALL := $(TARGETS_C_BOTHBITS) $(TARGETS_C_32BIT_ONLY)
diff --git a/tools/testing/selftests/x86/test_mremap_vdso.c 
b/tools/testing/selftests/x86/test_mremap_vdso.c
new file mode 100644
index ..a470790e2118
--- /dev/null
+++ b/tools/testing/selftests/x86/test_mremap_vdso.c
@@ -0,0 +1,72 @@
+/*
+ * 32-bit test to check vdso mremap.
+ *
+ * Copyright (c) 2016 Dmitry Safonov
+ * Suggested-by: Andrew Lutomirski
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+/*
+ * Can be built statically:
+ * gcc -Os -Wall -static -m32 test_mremap_vdso.c
+ */
+#define _GNU_SOURCE
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+
+#if !defined(__i386__)
+int main(int argc, char **argv, char **envp)
+{
+   printf("[SKIP]\tNot a 32-bit x86 userspace\n");
+   return 0;
+}
+#else
+
+#define PAGE_SIZE  4096
+#define VDSO_SIZE  PAGE_SIZE
+
+int main(int argc, char **argv, char **envp)
+{
+   unsigned long vdso_addr, dest_addr;
+   void *new_addr;
+   const char *ok_string = "[OK]\n";
+
+   vdso_addr = getauxval(AT_SYSINFO_EHDR);
+   printf("\tAT_SYSINFO_EHDR is 0x%lx\n", vdso_addr);
+   if (!vdso_addr || vdso_addr == -ENOENT) {
+   printf("[FAIL]\tgetauxval failed\n");
+   return 1;
+   }
+
+   /* to low for stack, to high for lib/data/code mappings */
+   dest_addr = 0x0a00;
+   printf("[NOTE]\tMoving vDSO: [%lx, %lx] -> [%lx, %lx]\n",
+   vdso_addr, vdso_addr + VDSO_SIZE,
+   dest_addr, dest_addr + VDSO_SIZE);
+   new_addr = mremap((void *)vdso_addr, VDSO_SIZE, VDSO_SIZE,
+   MREMAP_FIXED|MREMAP_MAYMOVE, dest_addr);
+   if ((unsigned long)new_addr == (unsigned long)-1) {
+   printf("[FAIL]\tmremap failed (%d): %m\n", errno);
+   return 1;
+   }
+
+   asm volatile ("int $0x80" : : "a" (__NR_write), "b" (STDOUT_FILENO),
+   "c" (ok_string), "d" (strlen(ok_string)));
+   asm volatile ("int $0x80" : : "a" (__NR_exit), "b" (0));
+
+   return 0;
+}
+#endif
-- 
2.8.0