x86/ptrace: Fix xfpregs_set()'s incorrect xmm clearing

xfpregs_set() handles 32-bit REGSET_XFP and 64-bit REGSET_FP. The actual
code treats these regsets as modern FX state (i.e. the beginning part of
XSTATE). The declarations of the regsets thought they were the legacy
i387 format. The code thought they were the 32-bit (no xmm8..15) variant
of XSTATE and, for good measure, made the high bits disappear by zeroing
the wrong part of the buffer. The latter broke ptrace, and everything
else confused anyone trying to understand the code. In particular, the
nonsense definitions of the regsets confused me when I wrote this code.

Clean this all up. Change the declarations to match reality (which
shouldn't change the generated code, let alone the ABI) and fix
xfpregs_set() to clear the correct bits and to only do so for 32-bit
callers.

Fixes: 6164331d15f7 ("x86/fpu: Rewrite xfpregs_set()")
Reported-by: Luís Ferreira <contact@lsferreira.net>
Signed-off-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org>
Link: https://bugzilla.kernel.org/show_bug.cgi?id=215524
Link: https://lore.kernel.org/r/YgpFnZpF01WwR8wU@zn.tnic

authored by Andy Lutomirski and committed by Borislav Petkov 44cad52c e5733d8c

Changed files
+6 -7
arch
x86
kernel
+4 -5
arch/x86/kernel/fpu/regset.c
··· 91 91 const void *kbuf, const void __user *ubuf) 92 92 { 93 93 struct fpu *fpu = &target->thread.fpu; 94 - struct user32_fxsr_struct newstate; 94 + struct fxregs_state newstate; 95 95 int ret; 96 - 97 - BUILD_BUG_ON(sizeof(newstate) != sizeof(struct fxregs_state)); 98 96 99 97 if (!cpu_feature_enabled(X86_FEATURE_FXSR)) 100 98 return -ENODEV; ··· 114 116 /* Copy the state */ 115 117 memcpy(&fpu->fpstate->regs.fxsave, &newstate, sizeof(newstate)); 116 118 117 - /* Clear xmm8..15 */ 119 + /* Clear xmm8..15 for 32-bit callers */ 118 120 BUILD_BUG_ON(sizeof(fpu->__fpstate.regs.fxsave.xmm_space) != 16 * 16); 119 - memset(&fpu->fpstate->regs.fxsave.xmm_space[8], 0, 8 * 16); 121 + if (in_ia32_syscall()) 122 + memset(&fpu->fpstate->regs.fxsave.xmm_space[8*4], 0, 8 * 16); 120 123 121 124 /* Mark FP and SSE as in use when XSAVE is enabled */ 122 125 if (use_xsave())
+2 -2
arch/x86/kernel/ptrace.c
··· 1224 1224 }, 1225 1225 [REGSET_FP] = { 1226 1226 .core_note_type = NT_PRFPREG, 1227 - .n = sizeof(struct user_i387_struct) / sizeof(long), 1227 + .n = sizeof(struct fxregs_state) / sizeof(long), 1228 1228 .size = sizeof(long), .align = sizeof(long), 1229 1229 .active = regset_xregset_fpregs_active, .regset_get = xfpregs_get, .set = xfpregs_set 1230 1230 }, ··· 1271 1271 }, 1272 1272 [REGSET_XFP] = { 1273 1273 .core_note_type = NT_PRXFPREG, 1274 - .n = sizeof(struct user32_fxsr_struct) / sizeof(u32), 1274 + .n = sizeof(struct fxregs_state) / sizeof(u32), 1275 1275 .size = sizeof(u32), .align = sizeof(u32), 1276 1276 .active = regset_xregset_fpregs_active, .regset_get = xfpregs_get, .set = xfpregs_set 1277 1277 },