Skip to content

Commit 87194ef

Browse files
committed
Merge tag 'x86_fsgsbase_for_v5.10' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 fsgsbase updates from Borislav Petkov: "Misc minor cleanups and corrections to the fsgsbase code and respective selftests" * tag 'x86_fsgsbase_for_v5.10' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: selftests/x86/fsgsbase: Test PTRACE_PEEKUSER for GSBASE with invalid LDT GS selftests/x86/fsgsbase: Reap a forgotten child x86/fsgsbase: Replace static_cpu_has() with boot_cpu_has() x86/entry/64: Correct the comment over SAVE_AND_SET_GSBASE
2 parents 9e536c8 + 1b9abd1 commit 87194ef

4 files changed

Lines changed: 77 additions & 8 deletions

File tree

arch/x86/entry/entry_64.S

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,9 @@ SYM_CODE_START_LOCAL(paranoid_entry)
842842
* retrieve and set the current CPUs kernel GSBASE. The stored value
843843
* has to be restored in paranoid_exit unconditionally.
844844
*
845-
* The MSR write ensures that no subsequent load is based on a
846-
* mispredicted GSBASE. No extra FENCE required.
845+
* The unconditional write to GS base below ensures that no subsequent
846+
* loads based on a mispredicted GS base can happen, therefore no LFENCE
847+
* is needed here.
847848
*/
848849
SAVE_AND_SET_GSBASE scratch_reg=%rax save_reg=%rbx
849850
ret

arch/x86/include/asm/fsgsbase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static inline unsigned long x86_fsbase_read_cpu(void)
5757
{
5858
unsigned long fsbase;
5959

60-
if (static_cpu_has(X86_FEATURE_FSGSBASE))
60+
if (boot_cpu_has(X86_FEATURE_FSGSBASE))
6161
fsbase = rdfsbase();
6262
else
6363
rdmsrl(MSR_FS_BASE, fsbase);
@@ -67,7 +67,7 @@ static inline unsigned long x86_fsbase_read_cpu(void)
6767

6868
static inline void x86_fsbase_write_cpu(unsigned long fsbase)
6969
{
70-
if (static_cpu_has(X86_FEATURE_FSGSBASE))
70+
if (boot_cpu_has(X86_FEATURE_FSGSBASE))
7171
wrfsbase(fsbase);
7272
else
7373
wrmsrl(MSR_FS_BASE, fsbase);

arch/x86/kernel/process_64.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ unsigned long x86_gsbase_read_cpu_inactive(void)
407407
{
408408
unsigned long gsbase;
409409

410-
if (static_cpu_has(X86_FEATURE_FSGSBASE)) {
410+
if (boot_cpu_has(X86_FEATURE_FSGSBASE)) {
411411
unsigned long flags;
412412

413413
local_irq_save(flags);
@@ -422,7 +422,7 @@ unsigned long x86_gsbase_read_cpu_inactive(void)
422422

423423
void x86_gsbase_write_cpu_inactive(unsigned long gsbase)
424424
{
425-
if (static_cpu_has(X86_FEATURE_FSGSBASE)) {
425+
if (boot_cpu_has(X86_FEATURE_FSGSBASE)) {
426426
unsigned long flags;
427427

428428
local_irq_save(flags);
@@ -439,7 +439,7 @@ unsigned long x86_fsbase_read_task(struct task_struct *task)
439439

440440
if (task == current)
441441
fsbase = x86_fsbase_read_cpu();
442-
else if (static_cpu_has(X86_FEATURE_FSGSBASE) ||
442+
else if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
443443
(task->thread.fsindex == 0))
444444
fsbase = task->thread.fsbase;
445445
else
@@ -454,7 +454,7 @@ unsigned long x86_gsbase_read_task(struct task_struct *task)
454454

455455
if (task == current)
456456
gsbase = x86_gsbase_read_cpu_inactive();
457-
else if (static_cpu_has(X86_FEATURE_FSGSBASE) ||
457+
else if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
458458
(task->thread.gsindex == 0))
459459
gsbase = task->thread.gsbase;
460460
else

tools/testing/selftests/x86/fsgsbase.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,68 @@ static void test_unexpected_base(void)
443443

444444
#define USER_REGS_OFFSET(r) offsetof(struct user_regs_struct, r)
445445

446+
static void test_ptrace_write_gs_read_base(void)
447+
{
448+
int status;
449+
pid_t child = fork();
450+
451+
if (child < 0)
452+
err(1, "fork");
453+
454+
if (child == 0) {
455+
printf("[RUN]\tPTRACE_POKE GS, read GSBASE back\n");
456+
457+
printf("[RUN]\tARCH_SET_GS to 1\n");
458+
if (syscall(SYS_arch_prctl, ARCH_SET_GS, 1) != 0)
459+
err(1, "ARCH_SET_GS");
460+
461+
if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0)
462+
err(1, "PTRACE_TRACEME");
463+
464+
raise(SIGTRAP);
465+
_exit(0);
466+
}
467+
468+
wait(&status);
469+
470+
if (WSTOPSIG(status) == SIGTRAP) {
471+
unsigned long base;
472+
unsigned long gs_offset = USER_REGS_OFFSET(gs);
473+
unsigned long base_offset = USER_REGS_OFFSET(gs_base);
474+
475+
/* Read the initial base. It should be 1. */
476+
base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL);
477+
if (base == 1) {
478+
printf("[OK]\tGSBASE started at 1\n");
479+
} else {
480+
nerrs++;
481+
printf("[FAIL]\tGSBASE started at 0x%lx\n", base);
482+
}
483+
484+
printf("[RUN]\tSet GS = 0x7, read GSBASE\n");
485+
486+
/* Poke an LDT selector into GS. */
487+
if (ptrace(PTRACE_POKEUSER, child, gs_offset, 0x7) != 0)
488+
err(1, "PTRACE_POKEUSER");
489+
490+
/* And read the base. */
491+
base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL);
492+
493+
if (base == 0 || base == 1) {
494+
printf("[OK]\tGSBASE reads as 0x%lx with invalid GS\n", base);
495+
} else {
496+
nerrs++;
497+
printf("[FAIL]\tGSBASE=0x%lx (should be 0 or 1)\n", base);
498+
}
499+
}
500+
501+
ptrace(PTRACE_CONT, child, NULL, NULL);
502+
503+
wait(&status);
504+
if (!WIFEXITED(status))
505+
printf("[WARN]\tChild didn't exit cleanly.\n");
506+
}
507+
446508
static void test_ptrace_write_gsbase(void)
447509
{
448510
int status;
@@ -517,6 +579,9 @@ static void test_ptrace_write_gsbase(void)
517579

518580
END:
519581
ptrace(PTRACE_CONT, child, NULL, NULL);
582+
wait(&status);
583+
if (!WIFEXITED(status))
584+
printf("[WARN]\tChild didn't exit cleanly.\n");
520585
}
521586

522587
int main()
@@ -526,6 +591,9 @@ int main()
526591
shared_scratch = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
527592
MAP_ANONYMOUS | MAP_SHARED, -1, 0);
528593

594+
/* Do these tests before we have an LDT. */
595+
test_ptrace_write_gs_read_base();
596+
529597
/* Probe FSGSBASE */
530598
sethandler(SIGILL, sigill, 0);
531599
if (sigsetjmp(jmpbuf, 1) == 0) {

0 commit comments

Comments
 (0)