Skip to content

Commit d9c9ce3

Browse files
Sebastian Andrzej Siewiorsuryasaimadhu
authored andcommitted
x86/fpu: Fault-in user stack if copy_fpstate_to_sigframe() fails
In the compacted form, XSAVES may save only the XMM+SSE state but skip FP (x87 state). This is denoted by header->xfeatures = 6. The fastpath (copy_fpregs_to_sigframe()) does that but _also_ initialises the FP state (cwd to 0x37f, mxcsr as we do, remaining fields to 0). The slowpath (copy_xstate_to_user()) leaves most of the FP state untouched. Only mxcsr and mxcsr_flags are set due to xfeatures_mxcsr_quirk(). Now that XFEATURE_MASK_FP is set unconditionally, see 04944b7 ("x86: xsave: set FP, SSE bits in the xsave header in the user sigcontext"), on return from the signal, random garbage is loaded as the FP state. Instead of utilizing copy_xstate_to_user(), fault-in the user memory and retry the fast path. Ideally, the fast path succeeds on the second attempt but may be retried again if the memory is swapped out due to memory pressure. If the user memory can not be faulted-in then get_user_pages() returns an error so we don't loop forever. Fault in memory via get_user_pages_unlocked() so copy_fpregs_to_sigframe() succeeds without a fault. Fixes: 69277c9 ("x86/fpu: Always store the registers in copy_fpstate_to_sigframe()") Reported-by: Kurt Kanzenbach <kurt.kanzenbach@linutronix.de> Suggested-by: Dave Hansen <dave.hansen@intel.com> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Signed-off-by: Borislav Petkov <bp@suse.de> Cc: Andy Lutomirski <luto@amacapital.net> Cc: Dave Hansen <dave.hansen@intel.com> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jann Horn <jannh@google.com> Cc: "linux-mm@kvack.org" <linux-mm@kvack.org> Cc: Qian Cai <cai@lca.pw> Cc: Rik van Riel <riel@surriel.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: x86-ml <x86@kernel.org> Link: https://lkml.kernel.org/r/20190502171139.mqtegctsg35cir2e@linutronix.de
1 parent a5eff72 commit d9c9ce3

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

arch/x86/kernel/fpu/signal.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,9 @@ static inline int copy_fpregs_to_sigframe(struct xregs_state __user *buf)
157157
*/
158158
int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
159159
{
160-
struct fpu *fpu = &current->thread.fpu;
161-
struct xregs_state *xsave = &fpu->state.xsave;
162160
struct task_struct *tsk = current;
163161
int ia32_fxstate = (buf != buf_fx);
164-
int ret = -EFAULT;
162+
int ret;
165163

166164
ia32_fxstate &= (IS_ENABLED(CONFIG_X86_32) ||
167165
IS_ENABLED(CONFIG_IA32_EMULATION));
@@ -174,11 +172,12 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
174172
sizeof(struct user_i387_ia32_struct), NULL,
175173
(struct _fpstate_32 __user *) buf) ? -1 : 1;
176174

175+
retry:
177176
/*
178177
* Load the FPU registers if they are not valid for the current task.
179178
* With a valid FPU state we can attempt to save the state directly to
180-
* userland's stack frame which will likely succeed. If it does not, do
181-
* the slowpath.
179+
* userland's stack frame which will likely succeed. If it does not,
180+
* resolve the fault in the user memory and try again.
182181
*/
183182
fpregs_lock();
184183
if (test_thread_flag(TIF_NEED_FPU_LOAD))
@@ -187,20 +186,20 @@ int copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size)
187186
pagefault_disable();
188187
ret = copy_fpregs_to_sigframe(buf_fx);
189188
pagefault_enable();
190-
if (ret && !test_thread_flag(TIF_NEED_FPU_LOAD))
191-
copy_fpregs_to_fpstate(fpu);
192-
set_thread_flag(TIF_NEED_FPU_LOAD);
193189
fpregs_unlock();
194190

195191
if (ret) {
196-
if (using_compacted_format()) {
197-
if (copy_xstate_to_user(buf_fx, xsave, 0, size))
198-
return -1;
199-
} else {
200-
fpstate_sanitize_xstate(fpu);
201-
if (__copy_to_user(buf_fx, xsave, fpu_user_xstate_size))
202-
return -1;
203-
}
192+
int aligned_size;
193+
int nr_pages;
194+
195+
aligned_size = offset_in_page(buf_fx) + fpu_user_xstate_size;
196+
nr_pages = DIV_ROUND_UP(aligned_size, PAGE_SIZE);
197+
198+
ret = get_user_pages_unlocked((unsigned long)buf_fx, nr_pages,
199+
NULL, FOLL_WRITE);
200+
if (ret == nr_pages)
201+
goto retry;
202+
return -EFAULT;
204203
}
205204

206205
/* Save the fsave header for the 32-bit frames. */

0 commit comments

Comments
 (0)