* Misc minor cleanups and corrections to the fsgsbase code and

respective selftests.
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCgAdFiEEzv7L6UO9uDPlPSfHEsHwGGHeVUoFAl+EMr8ACgkQEsHwGGHe
 VUpoBQ/+Pe7MaMbS7d+IATzhedKW7PNKcQr2g734rnZkz0BgG+1sBkCQPRAKBZoa
 SIvt3gURM3aEeD4Dp3am4nLElyhldZrlwKoGKGv6AYv2BpLPQM9PG0fHJGUyYBze
 ekKMdPu5YK0hYqoWctrY8h+qbExNdkfAvM7bJMFJMBqypVicm0n5wlgfZthGz0DU
 tkD34WZNE2GGAfsi/NNJ2H+hcZo8bQVrqW98bkgdzIA7+KI3cyZZ132VbKxb03tK
 A69C7+J4B20q/traWFlb4mTcFy/a1Txrt3cJXIv/Xer74gDMqNYcciGgnTJdhryY
 gzBmWNTxuQr1EC8DYJaxjlQbBp6VSAwhELlyc6UeRxLAViEpMxyPfBVMOYqraImc
 sZ8QKGgI02PggInN9yo4qCbtUWAGMCHV7HGGW8stVBrh1lia7o6Dy9jhO+nmTnzV
 EEe/vEoSsp/ydnkgFNjaRwjFLp+vDX2lAf513ZuZukpt+IGQ0nAO5phzgcZVAyH4
 qzr9uXdM3j+NtlXZgLttNppWEvHxzIpkri3Ly46VUFYOqTuKYPmS8A7stEfqx3NO
 T8g38+dDirFfKCoJz8NJBUUs+1KXer8QmJvogfHx5fsZ01Q2qz6AmOmsmMfe7Wqm
 +C9mvZJOJnW/7NunGWGVsuAZJOPx6o2oivjVpxxOyl8AeQnE6fg=
 =itDm
 -----END PGP SIGNATURE-----

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
This commit is contained in:
Linus Torvalds 2020-10-12 10:44:24 -07:00
commit 87194efe7e
4 changed files with 77 additions and 8 deletions

View File

@ -842,8 +842,9 @@ SYM_CODE_START_LOCAL(paranoid_entry)
* retrieve and set the current CPUs kernel GSBASE. The stored value * retrieve and set the current CPUs kernel GSBASE. The stored value
* has to be restored in paranoid_exit unconditionally. * has to be restored in paranoid_exit unconditionally.
* *
* The MSR write ensures that no subsequent load is based on a * The unconditional write to GS base below ensures that no subsequent
* mispredicted GSBASE. No extra FENCE required. * loads based on a mispredicted GS base can happen, therefore no LFENCE
* is needed here.
*/ */
SAVE_AND_SET_GSBASE scratch_reg=%rax save_reg=%rbx SAVE_AND_SET_GSBASE scratch_reg=%rax save_reg=%rbx
ret ret

View File

@ -57,7 +57,7 @@ static inline unsigned long x86_fsbase_read_cpu(void)
{ {
unsigned long fsbase; unsigned long fsbase;
if (static_cpu_has(X86_FEATURE_FSGSBASE)) if (boot_cpu_has(X86_FEATURE_FSGSBASE))
fsbase = rdfsbase(); fsbase = rdfsbase();
else else
rdmsrl(MSR_FS_BASE, fsbase); rdmsrl(MSR_FS_BASE, fsbase);
@ -67,7 +67,7 @@ static inline unsigned long x86_fsbase_read_cpu(void)
static inline void x86_fsbase_write_cpu(unsigned long fsbase) static inline void x86_fsbase_write_cpu(unsigned long fsbase)
{ {
if (static_cpu_has(X86_FEATURE_FSGSBASE)) if (boot_cpu_has(X86_FEATURE_FSGSBASE))
wrfsbase(fsbase); wrfsbase(fsbase);
else else
wrmsrl(MSR_FS_BASE, fsbase); wrmsrl(MSR_FS_BASE, fsbase);

View File

@ -407,7 +407,7 @@ unsigned long x86_gsbase_read_cpu_inactive(void)
{ {
unsigned long gsbase; unsigned long gsbase;
if (static_cpu_has(X86_FEATURE_FSGSBASE)) { if (boot_cpu_has(X86_FEATURE_FSGSBASE)) {
unsigned long flags; unsigned long flags;
local_irq_save(flags); local_irq_save(flags);
@ -422,7 +422,7 @@ unsigned long x86_gsbase_read_cpu_inactive(void)
void x86_gsbase_write_cpu_inactive(unsigned long gsbase) void x86_gsbase_write_cpu_inactive(unsigned long gsbase)
{ {
if (static_cpu_has(X86_FEATURE_FSGSBASE)) { if (boot_cpu_has(X86_FEATURE_FSGSBASE)) {
unsigned long flags; unsigned long flags;
local_irq_save(flags); local_irq_save(flags);
@ -439,7 +439,7 @@ unsigned long x86_fsbase_read_task(struct task_struct *task)
if (task == current) if (task == current)
fsbase = x86_fsbase_read_cpu(); fsbase = x86_fsbase_read_cpu();
else if (static_cpu_has(X86_FEATURE_FSGSBASE) || else if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
(task->thread.fsindex == 0)) (task->thread.fsindex == 0))
fsbase = task->thread.fsbase; fsbase = task->thread.fsbase;
else else
@ -454,7 +454,7 @@ unsigned long x86_gsbase_read_task(struct task_struct *task)
if (task == current) if (task == current)
gsbase = x86_gsbase_read_cpu_inactive(); gsbase = x86_gsbase_read_cpu_inactive();
else if (static_cpu_has(X86_FEATURE_FSGSBASE) || else if (boot_cpu_has(X86_FEATURE_FSGSBASE) ||
(task->thread.gsindex == 0)) (task->thread.gsindex == 0))
gsbase = task->thread.gsbase; gsbase = task->thread.gsbase;
else else

View File

@ -443,6 +443,68 @@ static void test_unexpected_base(void)
#define USER_REGS_OFFSET(r) offsetof(struct user_regs_struct, r) #define USER_REGS_OFFSET(r) offsetof(struct user_regs_struct, r)
static void test_ptrace_write_gs_read_base(void)
{
int status;
pid_t child = fork();
if (child < 0)
err(1, "fork");
if (child == 0) {
printf("[RUN]\tPTRACE_POKE GS, read GSBASE back\n");
printf("[RUN]\tARCH_SET_GS to 1\n");
if (syscall(SYS_arch_prctl, ARCH_SET_GS, 1) != 0)
err(1, "ARCH_SET_GS");
if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0)
err(1, "PTRACE_TRACEME");
raise(SIGTRAP);
_exit(0);
}
wait(&status);
if (WSTOPSIG(status) == SIGTRAP) {
unsigned long base;
unsigned long gs_offset = USER_REGS_OFFSET(gs);
unsigned long base_offset = USER_REGS_OFFSET(gs_base);
/* Read the initial base. It should be 1. */
base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL);
if (base == 1) {
printf("[OK]\tGSBASE started at 1\n");
} else {
nerrs++;
printf("[FAIL]\tGSBASE started at 0x%lx\n", base);
}
printf("[RUN]\tSet GS = 0x7, read GSBASE\n");
/* Poke an LDT selector into GS. */
if (ptrace(PTRACE_POKEUSER, child, gs_offset, 0x7) != 0)
err(1, "PTRACE_POKEUSER");
/* And read the base. */
base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL);
if (base == 0 || base == 1) {
printf("[OK]\tGSBASE reads as 0x%lx with invalid GS\n", base);
} else {
nerrs++;
printf("[FAIL]\tGSBASE=0x%lx (should be 0 or 1)\n", base);
}
}
ptrace(PTRACE_CONT, child, NULL, NULL);
wait(&status);
if (!WIFEXITED(status))
printf("[WARN]\tChild didn't exit cleanly.\n");
}
static void test_ptrace_write_gsbase(void) static void test_ptrace_write_gsbase(void)
{ {
int status; int status;
@ -517,6 +579,9 @@ static void test_ptrace_write_gsbase(void)
END: END:
ptrace(PTRACE_CONT, child, NULL, NULL); ptrace(PTRACE_CONT, child, NULL, NULL);
wait(&status);
if (!WIFEXITED(status))
printf("[WARN]\tChild didn't exit cleanly.\n");
} }
int main() int main()
@ -526,6 +591,9 @@ int main()
shared_scratch = mmap(NULL, 4096, PROT_READ | PROT_WRITE, shared_scratch = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_SHARED, -1, 0); MAP_ANONYMOUS | MAP_SHARED, -1, 0);
/* Do these tests before we have an LDT. */
test_ptrace_write_gs_read_base();
/* Probe FSGSBASE */ /* Probe FSGSBASE */
sethandler(SIGILL, sigill, 0); sethandler(SIGILL, sigill, 0);
if (sigsetjmp(jmpbuf, 1) == 0) { if (sigsetjmp(jmpbuf, 1) == 0) {