From: "Chang S. Bae" <chang.seok....@intel.com>

This validates that GS selector and base are independently preserved in
ptrace commands.

Suggested-by: Andy Lutomirski <l...@kernel.org>
Signed-off-by: Chang S. Bae <chang.seok....@intel.com>
Signed-off-by: Sasha Levin <sas...@kernel.org>
Reviewed-by: Tony Luck <tony.l...@intel.com>
Cc: Thomas Gleixner <t...@linutronix.de>
Cc: Borislav Petkov <b...@alien8.de>
Cc: Andy Lutomirski <l...@kernel.org>
Cc: H. Peter Anvin <h...@zytor.com>
Cc: Dave Hansen <dave.han...@intel.com>
Cc: Tony Luck <tony.l...@intel.com>
Cc: Andi Kleen <a...@linux.intel.com>
---
 tools/testing/selftests/x86/fsgsbase.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/x86/fsgsbase.c 
b/tools/testing/selftests/x86/fsgsbase.c
index 950a48b2e3662..9a4349813a30a 100644
--- a/tools/testing/selftests/x86/fsgsbase.c
+++ b/tools/testing/selftests/x86/fsgsbase.c
@@ -465,7 +465,7 @@ static void test_ptrace_write_gsbase(void)
        wait(&status);
 
        if (WSTOPSIG(status) == SIGTRAP) {
-               unsigned long gs;
+               unsigned long gs, base;
                unsigned long gs_offset = USER_REGS_OFFSET(gs);
                unsigned long base_offset = USER_REGS_OFFSET(gs_base);
 
@@ -481,6 +481,7 @@ static void test_ptrace_write_gsbase(void)
                        err(1, "PTRACE_POKEUSER");
 
                gs = ptrace(PTRACE_PEEKUSER, child, gs_offset, NULL);
+               base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL);
 
                /*
                 * In a non-FSGSBASE system, the nonzero selector will load
@@ -501,8 +502,14 @@ static void test_ptrace_write_gsbase(void)
                         */
                        if (gs == 0)
                                printf("\tNote: this is expected behavior on 
older kernels.\n");
+               } else if (have_fsgsbase && (base != 0xFF)) {
+                       nerrs++;
+                       printf("[FAIL]\tGSBASE changed to %lx\n", base);
                } else {
-                       printf("[OK]\tGS remained 0x%hx\n", *shared_scratch);
+                       printf("[OK]\tGS remained 0x%hx", *shared_scratch);
+                       if (have_fsgsbase)
+                               printf(" and GSBASE changed to 0xFF");
+                       printf("\n");
                }
        }
 
-- 
2.20.1

Reply via email to