x86/ptrace: Fix xfpregs_set()'s incorrect xmm clearing
Subsystems (3)
Borislav Petkov
maintainerPATCH 1/1 x86/ptrace: Fix xfpregs_set()'s incorrect xmm clearing
Lemme send it out as Luto looks busy.
If there are no complaints I'll queue it in a couple of days...
Thx.
---
From: Andy Lutomirski <[email protected]>
Date: Mon, 31 Jan 2022 12:38:47 -0800
Subject: [PATCH] x86/ptrace: Fix xfpregs_set()'s incorrect xmm clearing
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
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.
--- arch/x86/kernel/fpu/regset.c | 9 ++++----- arch/x86/kernel/ptrace.c | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-)
arch/x86/kernel/fpu/regset.c +4 -5
@@ -91,11 +91,9 @@ int xfpregs_set(struct task_struct *target, const struct user_regset *regset, const void *kbuf, const void __user *ubuf) { struct fpu *fpu = &target->thread.fpu; -struct user32_fxsr_struct newstate; +struct fxregs_state newstate;int ret; -BUILD_BUG_ON (sizeof (newstate) !=sizeof (struct fxregs_state )); -if (!cpu_feature_enabled (X86_FEATURE_FXSR ))return -ENODEV ;
@@ -116,9 +114,10 @@ int xfpregs_set(struct task_struct *target, const struct user_regset *regset,/* Copy the state */ memcpy (&fpu->fpstate->regs.fxsave, &newstate,sizeof (newstate)); -/* Clear xmm8..15 */ +/* Clear xmm8..15 for 32-bit callers */ BUILD_BUG_ON (sizeof (fpu->__fpstate.regs.fxsave.xmm_space) !=16 *16 ); -memset (&fpu->fpstate->regs.fxsave.xmm_space[8 ],0 ,8 *16 ); +if (in_ia32_syscall ()) +memset (&fpu->fpstate->regs.fxsave.xmm_space[8 *4 ],0 ,8 *16 );/* Mark FP and SSE as in use when XSAVE is enabled */ if (use_xsave ())
arch/x86/kernel/ptrace.c +2 -2
@@ -1224,7 +1224,7 @@ static struct user_regset x86_64_regsets[] __ro_after_init = { }, [REGSET_FP] = { .core_note_type = NT_PRFPREG, - .n = sizeof(struct user_i387_struct) / sizeof(long), + .n = sizeof(struct fxregs_state) / sizeof(long), .size = sizeof(long), .align = sizeof(long), .active = regset_xregset_fpregs_active, .regset_get = xfpregs_get, .set = xfpregs_set },
@@ -1271,7 +1271,7 @@ static struct user_regset x86_32_regsets[] __ro_after_init = { }, [REGSET_XFP] = { .core_note_type = NT_PRXFPREG, - .n = sizeof(struct user32_fxsr_struct) / sizeof(u32), + .n = sizeof(struct fxregs_state) / sizeof(u32), .size = sizeof(u32), .align = sizeof(u32), .active = regset_xregset_fpregs_active, .regset_get = xfpregs_get, .set = xfpregs_set },
Signature · 7 lines
2.29.2
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteThe following commit has been merged into the x86/urgent branch of tip:
The following commit has been merged into the x86/urgent branch of tip:
Commit-ID: 44cad52cc14ae10062f142ec16ede489bccf4469 Gitweb: https://git.kernel.org/tip/44cad52cc14ae10062f142ec16ede489bccf4469 Author: Andy Lutomirski <[email protected]> AuthorDate: Mon, 14 Feb 2022 13:05:49 +01:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Fri, 18 Feb 2022 11:23:21 +01:00
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.
--- arch/x86/kernel/fpu/regset.c | 9 ++++----- arch/x86/kernel/ptrace.c | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-)
arch/x86/kernel/fpu/regset.c +4 -5
@@ -91,11 +91,9 @@ int xfpregs_set(struct task_struct *target, const struct user_regset *regset, const void *kbuf, const void __user *ubuf) { struct fpu *fpu = &target->thread.fpu; -struct user32_fxsr_struct newstate; +struct fxregs_state newstate;int ret; -BUILD_BUG_ON (sizeof (newstate) !=sizeof (struct fxregs_state )); -if (!cpu_feature_enabled (X86_FEATURE_FXSR ))return -ENODEV ;
@@ -116,9 +114,10 @@ int xfpregs_set(struct task_struct *target, const struct user_regset *regset,/* Copy the state */ memcpy (&fpu->fpstate->regs.fxsave, &newstate,sizeof (newstate)); -/* Clear xmm8..15 */ +/* Clear xmm8..15 for 32-bit callers */ BUILD_BUG_ON (sizeof (fpu->__fpstate.regs.fxsave.xmm_space) !=16 *16 ); -memset (&fpu->fpstate->regs.fxsave.xmm_space[8 ],0 ,8 *16 ); +if (in_ia32_syscall ()) +memset (&fpu->fpstate->regs.fxsave.xmm_space[8 *4 ],0 ,8 *16 );/* Mark FP and SSE as in use when XSAVE is enabled */ if (use_xsave ())
arch/x86/kernel/ptrace.c +2 -2
@@ -1224,7 +1224,7 @@ static struct user_regset x86_64_regsets[] __ro_after_init = { }, [REGSET_FP] = { .core_note_type = NT_PRFPREG, - .n = sizeof(struct user_i387_struct) / sizeof(long), + .n = sizeof(struct fxregs_state) / sizeof(long), .size = sizeof(long), .align = sizeof(long), .active = regset_xregset_fpregs_active, .regset_get = xfpregs_get, .set = xfpregs_set },
@@ -1271,7 +1271,7 @@ static struct user_regset x86_32_regsets[] __ro_after_init = { }, [REGSET_XFP] = { .core_note_type = NT_PRXFPREG, - .n = sizeof(struct user32_fxsr_struct) / sizeof(u32), + .n = sizeof(struct fxregs_state) / sizeof(u32), .size = sizeof(u32), .align = sizeof(u32), .active = regset_xregset_fpregs_active, .regset_get = xfpregs_get, .set = xfpregs_set },