Re: [PATCH v2] x86/fpu: Fault-in user stack if copy_fpstate_to_sigframe() fails

From: Borislav Petkov
Date: Fri May 03 2019 - 02:05:08 EST


On Thu, May 02, 2019 at 07:11:39PM +0200, Sebastian Andrzej Siewior wrote:
> 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
>
> 04944b793e18 ("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: 69277c98f5eef ("x86/fpu: Always store the registers in copy_fpstate_to_sigframe()")
> Reported-by: Kurt Kanzenbach <kurt.kanzenbach@xxxxxxxxxxxxx>
> Suggested-by: Dave Hansen <dave.hansen@xxxxxxxxx>
> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@xxxxxxxxxxxxx>
> ---
> v1âv2:
> - s/get_user_pages()/get_user_pages_unlocked()/
> - merge cleanups
>
> I'm posting this all-in-one fix up replacing the original patch so we
> don't have a merge window with known bugs (that is the one that the
> patch was going the fix and the KASAN fallout that it introduced).
>
> arch/x86/kernel/fpu/signal.c | 31 +++++++++++++++----------------
> 1 file changed, 15 insertions(+), 16 deletions(-)

Queued to tip:WIP.x86/fpu for some hammering ontop ...

--
Regards/Gruss,
Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.