From: Andrei Vagin <avagin@google.com>
To: Borislav Petkov <bp@alien8.de>,
"Chang S. Bae" <chang.seok.bae@intel.com>
Cc: linux-kernel@vger.kernel.org, criu@lists.linux.dev,
Thomas Gleixner <tglx@kernel.org>,
Ingo Molnar <mingo@redhat.com>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, Andrei Vagin <avagin@google.com>,
Alexander Mikhalitsyn <alexander@mihalicyn.com>,
"H. Peter Anvin" <hpa@zytor.com>
Subject: [PATCH 6/7] x86/fpu: Pre-fault only required size of xstate buffer
Date: Fri, 25 Sep 2026 16:24:52 +0000 [thread overview]
Message-ID: <20260925162454.1403405-7-avagin@google.com> (raw)
In-Reply-To: <20260925162454.1403405-1-avagin@google.com>
The kernel previously used the default task FPU state size (user_size)
to fault in the user buffer when restoring FPU registers from a signal
frame. This can lead to attempting to fault in memory past the end of
the actual frame if the frame was smaller than the default size.
Introduce consistency checks to calculate the actual required size for
the features enabled in the xfeatures mask, ensure that the provided
xstate_size is sufficient, and shrink it to the actual required size.
Use this validated size to fault in the user buffer.
Keep the strict check that the provided xstate_size does not exceed the
default user_size for now.
Reviewed-by: Alexander Mikhalitsyn <alexander@mihalicyn.com>
Reviewed-by: Chang S. Bae <chang.seok.bae@intel.com>
Signed-off-by: Andrei Vagin <avagin@google.com>
---
arch/x86/include/asm/fpu/xstate.h | 2 ++
arch/x86/kernel/fpu/signal.c | 38 +++++++++++++++++++++++--------
arch/x86/kernel/fpu/xstate.c | 2 +-
3 files changed, 32 insertions(+), 10 deletions(-)
diff --git a/arch/x86/include/asm/fpu/xstate.h b/arch/x86/include/asm/fpu/xstate.h
index 7a7dc9d56027..33343e5d54b0 100644
--- a/arch/x86/include/asm/fpu/xstate.h
+++ b/arch/x86/include/asm/fpu/xstate.h
@@ -111,6 +111,8 @@ int xfeature_size(int xfeature_nr);
void xsaves(struct xregs_state *xsave, u64 mask);
void xrstors(struct xregs_state *xsave, u64 mask);
+unsigned int xstate_calculate_size(u64 xfeatures, bool compacted);
+
int xfd_enable_feature(u64 xfd_err);
#ifdef CONFIG_X86_64
diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c
index d56819fe491e..594ba36dec73 100644
--- a/arch/x86/kernel/fpu/signal.c
+++ b/arch/x86/kernel/fpu/signal.c
@@ -27,9 +27,10 @@
static inline bool check_xstate_in_sigframe(struct fxregs_state __user *buf_fx,
struct _fpx_sw_bytes *fx_sw)
{
+ struct fpstate *fpstate = x86_task_fpu(current)->fpstate;
int min_xstate_size = sizeof(struct fxregs_state) +
sizeof(struct xstate_header);
- void __user *fpstate = buf_fx;
+ void __user *buf = buf_fx;
unsigned int magic2;
if (__copy_from_user(fx_sw, &buf_fx->sw_reserved[0], sizeof(*fx_sw)))
@@ -38,8 +39,8 @@ static inline bool check_xstate_in_sigframe(struct fxregs_state __user *buf_fx,
/* Check for the first magic field and other error scenarios. */
if (fx_sw->magic1 != FP_XSTATE_MAGIC1 ||
fx_sw->xstate_size < min_xstate_size ||
- fx_sw->xstate_size > x86_task_fpu(current)->fpstate->user_size ||
- fx_sw->xstate_size > fx_sw->extended_size)
+ fx_sw->xstate_size > fpstate->user_size ||
+ fx_sw->extended_size < fx_sw->xstate_size + FP_XSTATE_MAGIC2_SIZE)
goto err_setfx;
/*
@@ -48,11 +49,27 @@ static inline bool check_xstate_in_sigframe(struct fxregs_state __user *buf_fx,
* fpstate layout with out copying the extended state information
* in the memory layout.
*/
- if (__get_user(magic2, (__u32 __user *)(fpstate + fx_sw->xstate_size)))
+ if (__get_user(magic2, (__u32 __user *)(buf + fx_sw->xstate_size)))
return false;
+ if (unlikely(magic2 != FP_XSTATE_MAGIC2))
+ goto err_setfx;
- if (likely(magic2 == FP_XSTATE_MAGIC2))
- return true;
+ if (fx_sw->xstate_size != fpstate->user_size ||
+ fx_sw->xfeatures != fpstate->user_xfeatures) {
+ unsigned int xsize;
+ u64 xfeatures;
+
+ /* Calculate size of enabled features only. */
+ xfeatures = fx_sw->xfeatures & fpstate->user_xfeatures;
+
+ xsize = xstate_calculate_size(xfeatures, false);
+ if (fx_sw->xstate_size < xsize)
+ return false;
+
+ fx_sw->xstate_size = xsize;
+ }
+
+ return true;
err_setfx:
/*
* The fallback to FX-only state is used to preserve backward
@@ -277,7 +294,8 @@ static int __restore_fpregs_from_user(void __user *buf, u64 task_xfeatures,
* Attempt to restore the FPU registers directly from user memory.
* Pagefaults are handled and any errors returned are fatal.
*/
-static bool restore_fpregs_from_user(void __user *buf, u64 xrestore_mask, bool fx_only)
+static bool restore_fpregs_from_user(void __user *buf, u64 xrestore_mask,
+ bool fx_only, size_t xstate_size)
{
struct fpu *fpu = x86_task_fpu(current);
int ret;
@@ -311,7 +329,7 @@ static bool restore_fpregs_from_user(void __user *buf, u64 xrestore_mask, bool f
if (ret != X86_TRAP_PF)
return false;
- if (!fault_in_readable(buf, fpu->fpstate->user_size))
+ if (!fault_in_readable(buf, xstate_size))
goto retry;
return false;
}
@@ -496,14 +514,16 @@ bool fpu__restore_sig(void __user *buf, int ia32_frame)
fx_only = !fx_sw_user.magic1;
xrestore_mask = fx_sw_user.xfeatures;
+ size = fx_sw_user.xstate_size;
} else {
xrestore_mask = XFEATURE_MASK_FPSSE;
+ size = fpu->fpstate->user_size;
}
if (ia32_fxstate)
success = restore_from_ia32_fxstate(buf, buf_fx, xrestore_mask, fx_only);
else
- success = restore_fpregs_from_user(buf_fx, xrestore_mask, fx_only);
+ success = restore_fpregs_from_user(buf_fx, xrestore_mask, fx_only, size);
out:
if (unlikely(!success))
fpu__clear_user_states(fpu);
diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index efa7879d2f97..a2b6ede343fd 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -587,7 +587,7 @@ static bool __init check_xstate_against_struct(int nr)
return true;
}
-static unsigned int xstate_calculate_size(u64 xfeatures, bool compacted)
+unsigned int xstate_calculate_size(u64 xfeatures, bool compacted)
{
unsigned int topmost, offset, i;
--
2.56.0.rc1.315.gc6ed9934b7-goog
next prev parent reply other threads:[~2026-09-25 16:25 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-25 16:24 [PATCH v8.1 0/7] x86/fpu: Restore and reinforce signal frame portability Andrei Vagin
2026-09-25 16:24 ` [PATCH 1/7] x86/fpu: Document signal frame layout and portability Andrei Vagin
2026-09-25 16:24 ` [PATCH 2/7] x86/fpu: Clean up and rename variables in signal frame handling Andrei Vagin
2026-09-25 16:24 ` [PATCH 3/7] x86/fpu: Extract restore_from_ia32_fxstate() and clean up fpu__restore_sig() Andrei Vagin
2026-09-25 16:24 ` [PATCH 4/7] x86/fpu: Document reasoning of FX-only fallback Andrei Vagin
2026-09-25 16:24 ` [PATCH 5/7] x86/fpu: Fix potential underflow in xstate_calculate_size() Andrei Vagin
2026-09-25 16:24 ` Andrei Vagin [this message]
2026-09-25 16:24 ` [PATCH 7/7] selftests/x86: Add tests for signal frame FPU portability Andrei Vagin
-- strict thread matches above, loose matches on Subject: below --
2026-09-24 21:01 [PATCH v8 0/7] x86/fpu: Restore and reinforce signal frame portability Andrei Vagin
2026-09-24 21:01 ` [PATCH 6/7] x86/fpu: Pre-fault only required size of xstate buffer Andrei Vagin
2026-09-25 4:02 ` Borislav Petkov
2026-09-25 16:51 ` Andrei Vagin
2026-09-25 19:16 ` Borislav Petkov
2026-09-24 4:15 [PATCH v7 0/7] x86/fpu: Restore and reinforce signal frame portability Andrei Vagin
2026-09-24 4:16 ` [PATCH 6/7] x86/fpu: Pre-fault only required size of xstate buffer Andrei Vagin
2026-09-16 23:23 [PATCH v6 0/7] x86/fpu: Restore and reinforce signal frame portability Andrei Vagin
2026-09-16 23:23 ` [PATCH 6/7] x86/fpu: Pre-fault only required size of xstate buffer Andrei Vagin
2026-09-23 23:56 ` Borislav Petkov
2026-09-08 4:34 [PATCH v5 0/7] x86/fpu: Restore and reinforce signal frame portability Andrei Vagin
2026-09-08 4:34 ` [PATCH 6/7] x86/fpu: Pre-fault only required size of xstate buffer Andrei Vagin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260925162454.1403405-7-avagin@google.com \
--to=avagin@google.com \
--cc=alexander@mihalicyn.com \
--cc=bp@alien8.de \
--cc=chang.seok.bae@intel.com \
--cc=criu@lists.linux.dev \
--cc=dave.hansen@linux.intel.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=tglx@kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®