x86/fsgsbase: Some fixes
Subsystems (3)
cover x86/fsgsbase: Some fixes
The first two are trivial selftest fixes. The third one fixes an actual regression.
Andy Lutomirski (3):
selftests/x86/fsgsbase: Fix a comment in the ptrace_write_gsbase test selftests/x86/fsgsbase: Add a missing memory constraint x86/ptrace: Fix 32-bit PTRACE_SETREGS vs fsbase and gsbase
arch/x86/include/asm/fsgsbase.h | 2 + arch/x86/kernel/process_64.c | 4 +- arch/x86/kernel/ptrace.c | 43 ++- tools/testing/selftests/x86/Makefile | 2 +- tools/testing/selftests/x86/fsgsbase.c | 6 +- .../testing/selftests/x86/fsgsbase_restore.c | 245 ++++++++++++++++++ 6 files changed, 284 insertions(+), 18 deletions(-) create mode 100644 tools/testing/selftests/x86/fsgsbase_restore.c
Signature · 1 line
2.25.4
PATCH 1/3 selftests/x86/fsgsbase: Fix a comment in the ptrace_write_gsbase test
A comment was unclear. Fix it.
--- tools/testing/selftests/x86/fsgsbase.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/x86/fsgsbase.c +2 -1
@@ -498,7 +498,8 @@ static void test_ptrace_write_gsbase(void)* base would zero the selector. On newer kernels, * this behavior has changed -- poking the base * changes only the base and, if FSGSBASE is not -* available, this may not effect. +* available, this may have no effect once the tracee +* is resumed. */ if (gs ==0 )printf ("\tNote: this is expected behavior on older kernels.\n" );
Signature · 1 line
2.25.4
PATCH 2/3 selftests/x86/fsgsbase: Add a missing memory constraint
In reply to Andy LutomirskiThe manual call to set_thread_area() via int $0x80 was missing any indication that the descriptor was a pointer, causing gcc to occasionally generate wrong code. Add the missing constraint.
--- tools/testing/selftests/x86/fsgsbase.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/testing/selftests/x86/fsgsbase.c +2 -1
@@ -285,7 +285,8 @@ static unsigned short load_gs(void)/* 32-bit set_thread_area */ long ret; asmvolatile ("int $0x80" - :"=a" (ret) :"a" (243 ),"b" (low_desc) + :"=a" (ret),"+m" (*low_desc) + :"a" (243 ),"b" (low_desc) :"r8" ,"r9" ,"r10" ,"r11" );memcpy (&desc, low_desc,sizeof (desc));munmap (low_desc,sizeof (desc));
Signature · 1 line
2.25.4
PATCH 3/3 x86/ptrace: Fix 32-bit PTRACE_SETREGS vs fsbase and gsbase
In reply to Andy LutomirskiDebuggers expect that doing PTRACE_GETREGS, then poking at a tracee and maybe letting it run for a while, then doing PTRACE_SETREGS will put the tracee back where it was. In the specific case of a 32-bit tracer and tracee, the PTRACE_GETREGS/SETREGS data structure doesn't have fs_base or gs_base fields, so FSBASE and GSBASE fields are never stored anywhere. Everything used to still work because nonzero FS or GS would result full reloads of the segment registers when the tracee resumes, and the bases associated with FS==0 or GS==0 are irrelevant to 32-bit code.
Adding FSGSBASE support broke this: when FSGSBASE is enabled, FSBASE and GSBASE are now restored independently of FS and GS for all tasks when context-switched in. This means that, if a 32-bit tracer restores a previous state using PTRACE_SETREGS but the tracee's pre-restore and post-restore bases don't match, then the tracee is resumed with the wrong base.
Fix it by explicitly loading the base when a 32-bit tracer pokes FS or GS on a 64-bit kernel.
Also add a test case.
--- arch/x86/include/asm/fsgsbase.h | 2 + arch/x86/kernel/process_64.c | 4 +- arch/x86/kernel/ptrace.c | 43 ++- tools/testing/selftests/x86/Makefile | 2 +- .../testing/selftests/x86/fsgsbase_restore.c | 245 ++++++++++++++++++ 5 files changed, 280 insertions(+), 16 deletions(-) create mode 100644 tools/testing/selftests/x86/fsgsbase_restore.c
arch/x86/include/asm/fsgsbase.h +2 -0
@@ -75,6 +75,8 @@ static inline void x86_fsbase_write_cpu(unsigned long fsbase)extern unsigned long x86_gsbase_read_cpu_inactive (void );extern void x86_gsbase_write_cpu_inactive (unsigned long gsbase); +extern unsigned long x86_fsgsbase_read_task (struct task_struct *task, +unsigned short selector);#endif /* CONFIG_X86_64 */
arch/x86/kernel/process_64.c +2 -2
@@ -347,8 +347,8 @@ static __always_inline void x86_fsgsbase_load(struct thread_struct *prev, } } -static unsigned long x86_fsgsbase_read_task (struct task_struct *task, -unsigned short selector) +unsigned long x86_fsgsbase_read_task (struct task_struct *task, +unsigned short selector) {unsigned short idx = selector >>3 ;unsigned long base;
arch/x86/kernel/ptrace.c +30 -13
61 lines not on this page · raw message
tools/testing/selftests/x86/Makefile +1 -1
@@ -13,7 +13,7 @@ CAN_BUILD_WITH_NOPIE := $(shell ./check_cc.sh $(CC) trivial_program.c -no-pie) TARGETS_C_BOTHBITS := single_step_syscall sysret_ss_attrs syscall_nt test_mremap_vdso \ check_initial_reg_state sigreturn iopl ioperm \ test_vdso test_vsyscall mov_ss_trap \ - syscall_arg_fault + syscall_arg_fault fsgsbase_restore TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \ test_FCMOV test_FCOMI test_FISTTP \ vdso_restorer
tools/testing/selftests/x86/fsgsbase_restore.c new file +245 -0
245 lines not on this page · raw message
Signature · 1 line
2.25.4
Re: [PATCH 3/3] x86/ptrace: Fix 32-bit PTRACE_SETREGS vs fsbase and gsbase
Quoted 26 lines · Andy Lutomirski
Debuggers expect that doing
PTRACE_GETREGS, then poking at a tracee and maybe letting it run for a while, then doingPTRACE_SETREGSwill put the tracee back where it was. In the specific case of a 32-bit tracer and tracee, the PTRACE_GETREGS/SETREGS data structure doesn't havefs_baseorgs_basefields, so FSBASE and GSBASE fields are never stored anywhere. Everything used to still work because nonzero FS or GS would result full reloads of the segment registers when the tracee resumes, and the bases associated with FS==0 or GS==0 are irrelevant to 32-bit code.Adding FSGSBASE support broke this: when FSGSBASE is enabled, FSBASE and GSBASE are now restored independently of FS and GS for all tasks when context-switched in. This means that, if a 32-bit tracer restores a previous state using
PTRACE_SETREGSbut the tracee's pre-restore and post-restore bases don't match, then the tracee is resumed with the wrong base.Fix it by explicitly loading the base when a 32-bit tracer pokes FS or GS on a 64-bit kernel.
tools/testing/selftests/x86/fsgsbase_restore.c new file +0
-0
tools/testing/selftests/x86/fsgsbase_restore.c
Quoted 2 lines
tools/testing/selftests/x86/fsgsbase_restore.c new file +1
-0+if (false &&syscall (SYS_modify_ldt,1 , &desc,sizeof (desc)) ==0 ) {
Whoops. That 'false &&' shouldn't be there. Want a v2?