Skip to content

Commit 291fd83

Browse files
ChangSeokBaeKAGA-KOKO
authored andcommitted
selftests/x86/fsgsbase: Test GS selector on ptracer-induced GS base write
The test validates that the selector is not changed when a ptracer writes the ptracee's GS base. Originally-by: Andy Lutomirski <[email protected]> Signed-off-by: Chang S. Bae <[email protected]> Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Tony Luck <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 82c0c7d commit 291fd83

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

tools/testing/selftests/x86/fsgsbase.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ static void test_ptrace_write_gsbase(void)
465465
wait(&status);
466466

467467
if (WSTOPSIG(status) == SIGTRAP) {
468-
unsigned long gs, base;
468+
unsigned long gs;
469469
unsigned long gs_offset = USER_REGS_OFFSET(gs);
470470
unsigned long base_offset = USER_REGS_OFFSET(gs_base);
471471

@@ -481,19 +481,28 @@ static void test_ptrace_write_gsbase(void)
481481
err(1, "PTRACE_POKEUSER");
482482

483483
gs = ptrace(PTRACE_PEEKUSER, child, gs_offset, NULL);
484-
base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL);
485484

486485
/*
487486
* In a non-FSGSBASE system, the nonzero selector will load
488487
* GSBASE (again). But what is tested here is whether the
489488
* selector value is changed or not by the GSBASE write in
490489
* a ptracer.
491490
*/
492-
if (gs == 0 && base == 0xFF) {
493-
printf("[OK]\tGS was reset as expected\n");
494-
} else {
491+
if (gs != *shared_scratch) {
495492
nerrs++;
496-
printf("[FAIL]\tGS=0x%lx, GSBASE=0x%lx (should be 0, 0xFF)\n", gs, base);
493+
printf("[FAIL]\tGS changed to %lx\n", gs);
494+
495+
/*
496+
* On older kernels, poking a nonzero value into the
497+
* base would zero the selector. On newer kernels,
498+
* this behavior has changed -- poking the base
499+
* changes only the base and, if FSGSBASE is not
500+
* available, this may not effect.
501+
*/
502+
if (gs == 0)
503+
printf("\tNote: this is expected behavior on older kernels.\n");
504+
} else {
505+
printf("[OK]\tGS remained 0x%hx\n", *shared_scratch);
497506
}
498507
}
499508

0 commit comments

Comments
 (0)