x86/fpu: Restore and reinforce signal frame portability
Subsystems (3)
cover x86/fpu: Restore and reinforce signal frame portability
The x86 signal frame is designed to be self-describing. The xstate_size field in the software-reserved bytes indicates the actual size of the xstate context and is used by the kernel to locate the FP_XSTATE_MAGIC2 marker during signal return.
This design is required to provide portability of signal frames across different machines. For example, a process checkpointed on a system with fewer xstate features and restored on a system with more features will have a signal frame on its stack that is smaller than the destination host's default. By relying on the frame's internal xstate_size, the kernel can correctly validate and restore such frames.
This series restores and improves signal frame portability. The goal is to allow process migration across CPUs with heterogeneous FPU capabilities, as long as the process only uses features supported by both systems. This version addresses the original issues by pre-faulting only the required size of the xstate buffer (rather than the default task size), and includes cleanups requested by Ingo Molnar.
v8.1:
- Rebase on top of tip/x86/fpu.
v8: Address Sashiko's comments:
- Combine %ymm0 load/store and syscall into a single inline assembly block.
v7: Address Borislav's comments:
- Replace #ifdefs and stub for restore_from_ia32_fxstate() with an early return using IS_ENABLED().
- Use reverse fir tree variable declaration ordering in check_xstate_in_sigframe().
v6:
- Clarify struct _fpstate_32 layout and reformat documentation per Borislav comments.
- Add Reviewed-by tags from Chang S. Bae.
v5: Address Chang's comments:
- Documentation:
- Expanded Documentation/arch/x86/xstate.rst with a dedicated section describing struct _fpx_sw_bytes, 32-bit vs 64-bit frame layout differences, and architectural portability constraints.
- Trimmed the comment in <uapi/asm/sigcontext.h> and added a reference to xstate.rst.
- x86/fpu core:
- Renamed the 32-bit helper to restore_from_ia32_fxstate() and inlined the remaining native path directly into fpu__restore_sig().
- Selftests:
- Consolidated the shrunk-frame and insufficient-size test cases.
v4:
- Update documentation to describe architectural XSAVE layout constraints and feature repurposing (e.g. MPX vs APX).
- The patch "x86/fpu: Allow restoring signal frames with larger xstate_size" will be sent in a separate series.
- Address sashiko comments.
v3:
- Include cleanups and refactoring of signal frame handling code as requested by Ingo Molnar.
- Fix potential underflow in xstate_calculate_size()
v2:
- Address sashiko comments.
- 44eeff9bc467 ("Revert "x86/fpu: Refine and simplify the magic number check during signal return"") has been merged.
Andrei Vagin (7):
x86/fpu: Document signal frame layout and portability x86/fpu: Clean up and rename variables in signal frame handling x86/fpu: Extract restore_from_ia32_fxstate() and clean up
fpu__restore_sig()
x86/fpu: Document reasoning of FX-only fallback x86/fpu: Fix potential underflow in xstate_calculate_size() x86/fpu: Pre-fault only required size of xstate buffer selftests/x86: Add tests for signal frame FPU portability
Documentation/arch/x86/xstate.rst | 54 +++++ arch/x86/include/uapi/asm/sigcontext.h | 15 ++ arch/x86/kernel/fpu/signal.c | 127 +++++++---- arch/x86/kernel/fpu/xstate.c | 7 +- arch/x86/kernel/fpu/xstate.h | 2 + tools/testing/selftests/x86/Makefile | 5 +- .../selftests/x86/sigframe_fpu_portability.c | 235 +++++++++++++++++++++ tools/testing/selftests/x86/xstate.c | 12 -- tools/testing/selftests/x86/xstate.h | 20 ++ 9 files changed, 417 insertions(+), 60 deletions(-) create mode 100644 tools/testing/selftests/x86/sigframe_fpu_portability.c
base-commit: c0a44d1745d3f209e4840b78ab75274fb74f01a0
Signature · 1 line
2.55.0.979.g7e5102b832-goog
PATCH 1/7 x86/fpu: Document signal frame layout and portability
The x86 signal frame is designed to be self-describing, with the 'xstate_size' field in the software-reserved bytes indicating the actual size of the context. This design is required for portability, allowing a signal frame created on a system with a specific set of xstate features to be restored on a machine with a different (larger) set of features.
--- Documentation/arch/x86/xstate.rst | 54 ++++++++++++++++++++++++++ arch/x86/include/uapi/asm/sigcontext.h | 15 +++++++ 2 files changed, 69 insertions(+)
Documentation/arch/x86/xstate.rst +54 -0
@@ -172,3 +172,57 @@ are extended to control the guest permission: Note that some VMMs may have already established a set of supported state components. These options are not presumed to support any particular VMM. + +Signal Frame Layout and Portability +----------------------------------- + +The signal frame is designed to be self-describing and portable. This is +especially important for checkpoint/restore tools like CRIU, which may restore +a process on a different host than where it was checkpointed. A signal frame +created on a machine with fewer CPU features can be successfully restored on a +machine with more CPU features, but not vice-versa. + +Signal Frame Software Reserved Bytes +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +On CPUs supporting XSAVE, bytes 464..511 in the 512-byte FXSAVE/FXRSTOR frame +are reserved for software use and contain ``struct _fpx_sw_bytes`` (defined in +``<uapi/asm/sigcontext.h>``):: + + struct _fpx_sw_bytes { + __u32 magic1; + __u32 extended_size; + __u64 xfeatures; + __u32 xstate_size; + __u32 padding[7]; + }; + +- ``magic1``: Set to ``FP_XSTATE_MAGIC1`` (``0x46505853U``) if an extended + xstate context is present; 0 for a legacy frame. +- ``extended_size``: The total size allocated on the stack for the frame, + measured from the ``fpstate`` pointer. In 32-bit signal frames, this also + includes the 112-byte legacy FPU state prefix of ``struct _fpstate_32``. +- ``xfeatures``: The mask of xstate features saved in the frame. +- ``xstate_size``: The actual size of the xstate context for the enabled + features (including the 512-byte FXSAVE area and the 64-byte XSAVE header). + +The kernel uses ``xstate_size`` in conjunction with the pointer to the xstate +context to locate the ``FP_XSTATE_MAGIC2`` (``0x46505845U``) marker right after +the xstate context (at ``xstate_context + xstate_size``). In 64-bit signal frames, +the ``fpstate`` pointer points directly to the xstate context. In 32-bit signal +frames (including 32-bit compat tasks on 64-bit kernels), the ``fpstate`` +pointer points to ``struct _fpstate_32``, which contains the 112-byte legacy +FPU state followed by the 512-byte FXSR state (and any extended xstate). Since +there is no standalone UAPI structure defined for just the 112-byte legacy +state, the xstate context starts at ``fpstate + 112`` (and ``extended_size`` +spans the entire allocation from ``fpstate``). + +Portability Constraints +^^^^^^^^^^^^^^^^^^^^^^^ + +Signal frame portability is constrained by the architectural XSAVE layout. +Restoration is supported only if the destination host supports all features +present in the frame and uses matching component offsets and sizes for them. +While layout compatibility is generally maintained across CPUs from the same +vendor, differences can occur across vendors or if the XSAVE space of a +deprecated feature (e.g. MPX) is repurposed for a newer feature (e.g. APX).
arch/x86/include/uapi/asm/sigcontext.h +15 -0
@@ -34,6 +34,21 @@* fpstate+extended_size-FP_XSTATE_MAGIC2_SIZE address) is set to * FP_XSTATE_MAGIC2 so that you can sanity check your size calculations.) * +* The xstate_size field indicates the actual size of the xstate context +* (including the 512-byte FXSAVE area and the 64-byte XSAVE header struct +* _header). This size is used in conjunction with the pointer to the xstate +* context to locate FP_XSTATE_MAGIC2. +* +* In 64-bit signal frames, the fpstate pointer points directly to the xstate +* context. In 32-bit signal frames (including 32-bit compat tasks on 64-bit +* kernels), the fpstate pointer points to struct _fpstate_32, which contains +* the 112-byte legacy FPU state followed by the 512-byte FXSR state (and any +* extended xstate), so the xstate context starts at fpstate + 112. +* +* This makes the signal frame self-describing and portable across machines +* with different xstate features. See Documentation/arch/x86/xstate.rst +* for details on signal frame portability and its architectural constraints. +* * This extended area typically grows with newer CPUs that have larger and * larger XSAVE areas. */
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[tip: x86/fpu] x86/fpu: Document signal frame layout and portability
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 3d69596c684bb943ff2f74c13635bb11c3c75e86 Gitweb: https://git.kernel.org/tip/3d69596c684bb943ff2f74c13635bb11c3c75e86 Author: Andrei Vagin <[email protected]> AuthorDate: Fri, 25 Sep 2026 16:24:47 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Fri, 25 Sep 2026 12:08:45 -07:00
x86/fpu: Document signal frame layout and portability
The x86 signal frame is designed to be self-describing, with the 'xstate_size' field in the software-reserved bytes indicating the actual size of the context. This design is required for portability, allowing a signal frame created on a system with a specific set of xstate features to be restored on a machine with a different (larger) set of features.
--- Documentation/arch/x86/xstate.rst | 54 +++++++++++++++++++++++++- arch/x86/include/uapi/asm/sigcontext.h | 15 +++++++- 2 files changed, 69 insertions(+)
Documentation/arch/x86/xstate.rst +54 -0
@@ -172,3 +172,57 @@ are extended to control the guest permission: Note that some VMMs may have already established a set of supported state components. These options are not presumed to support any particular VMM. + +Signal Frame Layout and Portability +----------------------------------- + +The signal frame is designed to be self-describing and portable. This is +especially important for checkpoint/restore tools like CRIU, which may restore +a process on a different host than where it was checkpointed. A signal frame +created on a machine with fewer CPU features can be successfully restored on a +machine with more CPU features, but not vice-versa. + +Signal Frame Software Reserved Bytes +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +On CPUs supporting XSAVE, bytes 464..511 in the 512-byte FXSAVE/FXRSTOR frame +are reserved for software use and contain ``struct _fpx_sw_bytes`` (defined in +``<uapi/asm/sigcontext.h>``):: + + struct _fpx_sw_bytes { + __u32 magic1; + __u32 extended_size; + __u64 xfeatures; + __u32 xstate_size; + __u32 padding[7]; + }; + +- ``magic1``: Set to ``FP_XSTATE_MAGIC1`` (``0x46505853U``) if an extended + xstate context is present; 0 for a legacy frame. +- ``extended_size``: The total size allocated on the stack for the frame, + measured from the ``fpstate`` pointer. In 32-bit signal frames, this also + includes the 112-byte legacy FPU state prefix of ``struct _fpstate_32``. +- ``xfeatures``: The mask of xstate features saved in the frame. +- ``xstate_size``: The actual size of the xstate context for the enabled + features (including the 512-byte FXSAVE area and the 64-byte XSAVE header). + +The kernel uses ``xstate_size`` in conjunction with the pointer to the xstate +context to locate the ``FP_XSTATE_MAGIC2`` (``0x46505845U``) marker right after +the xstate context (at ``xstate_context + xstate_size``). In 64-bit signal frames, +the ``fpstate`` pointer points directly to the xstate context. In 32-bit signal +frames (including 32-bit compat tasks on 64-bit kernels), the ``fpstate`` +pointer points to ``struct _fpstate_32``, which contains the 112-byte legacy +FPU state followed by the 512-byte FXSR state (and any extended xstate). Since +there is no standalone UAPI structure defined for just the 112-byte legacy +state, the xstate context starts at ``fpstate + 112`` (and ``extended_size`` +spans the entire allocation from ``fpstate``). + +Portability Constraints +^^^^^^^^^^^^^^^^^^^^^^^ + +Signal frame portability is constrained by the architectural XSAVE layout. +Restoration is supported only if the destination host supports all features +present in the frame and uses matching component offsets and sizes for them. +While layout compatibility is generally maintained across CPUs from the same +vendor, differences can occur across vendors or if the XSAVE space of a +deprecated feature (e.g. MPX) is repurposed for a newer feature (e.g. APX).
arch/x86/include/uapi/asm/sigcontext.h +15 -0
@@ -34,6 +34,21 @@* fpstate+extended_size-FP_XSTATE_MAGIC2_SIZE address) is set to * FP_XSTATE_MAGIC2 so that you can sanity check your size calculations.) * +* The xstate_size field indicates the actual size of the xstate context +* (including the 512-byte FXSAVE area and the 64-byte XSAVE header struct +* _header). This size is used in conjunction with the pointer to the xstate +* context to locate FP_XSTATE_MAGIC2. +* +* In 64-bit signal frames, the fpstate pointer points directly to the xstate +* context. In 32-bit signal frames (including 32-bit compat tasks on 64-bit +* kernels), the fpstate pointer points to struct _fpstate_32, which contains +* the 112-byte legacy FPU state followed by the 512-byte FXSR state (and any +* extended xstate), so the xstate context starts at fpstate + 112. +* +* This makes the signal frame self-describing and portable across machines +* with different xstate features. See Documentation/arch/x86/xstate.rst +* for details on signal frame portability and its architectural constraints. +* * This extended area typically grows with newer CPUs that have larger and * larger XSAVE areas. */
PATCH 2/7 x86/fpu: Clean up and rename variables in signal frame handling
In reply to Andrei VaginClean up signal frame handling code by renaming several variables for clarity and consistency, and moving masking logic closer to its usage.
- Rename 'fxbuf' to 'buf_fx' in check_xstate_in_sigframe() for consistency.
- Rename label 'setfx' to 'err_setfx' in check_xstate_in_sigframe() to indicate it is an error path.
- In __restore_fpregs_from_user(), rename 'ufeatures' to 'task_xfeatures' and 'xrestore' to 'xrestore_mask'.
- Move the masking logic 'xrestore_mask &= task_xfeatures' from restore_fpregs_from_user() into __restore_fpregs_from_user().
- Rename 'xrestore' to 'xrestore_mask' in restore_fpregs_from_user() to match the name in __restore_fpregs_from_user() and __fpu_restore_sig().
- In __fpu_restore_sig(), rename 'buf' to 'buf_f' to distinguish it from 'buf_fx', and 'user_xfeatures' to 'xrestore_mask'.
No functional changes.
--- arch/x86/kernel/fpu/signal.c | 41 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 20 deletions(-)
arch/x86/kernel/fpu/signal.c +21 -20
@@ -24,15 +24,15 @@* Check for the presence of extended state information in the * user fpstate pointer in the sigcontext. */ -static inline bool check_xstate_in_sigframe (struct fxregs_state __user *fxbuf, +static inline bool check_xstate_in_sigframe (struct fxregs_state __user *buf_fx,struct _fpx_sw_bytes *fx_sw) {int min_xstate_size =sizeof (struct fxregs_state ) +sizeof (struct xstate_header ); -void __user *fpstate = fxbuf; +void __user *fpstate = buf_fx;unsigned int magic2; -if (__copy_from_user (fx_sw, &fxbuf->sw_reserved[0 ],sizeof (*fx_sw))) +if (__copy_from_user (fx_sw, &buf_fx->sw_reserved[0 ],sizeof (*fx_sw)))return false;/* Check for the first magic field and other error scenarios. */
@@ -40,7 +40,7 @@ static inline bool check_xstate_in_sigframe(struct fxregs_state __user *fxbuf, 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) - goto setfx; + goto err_setfx;/* * Check for the presence of second magic word at the end of memory
@@ -53,7 +53,7 @@ static inline bool check_xstate_in_sigframe(struct fxregs_state __user *fxbuf,if (likely (magic2 ==FP_XSTATE_MAGIC2 ))return true; -setfx: +err_setfx:trace_x86_fpu_xstate_check_failed (x86_task_fpu (current));/* Set the parameters for fx only state */
@@ -240,15 +240,18 @@ bool copy_fpstate_to_sigframe(void __user *buf, void __user *buf_fx, int size, ureturn true; } -static int __restore_fpregs_from_user (void __user *buf,u64 ufeatures, -u64 xrestore,bool fx_only) +static int __restore_fpregs_from_user (void __user *buf,u64 task_xfeatures, +u64 xrestore_mask,bool fx_only) {if (use_xsave ()) { -u64 init_bv = ufeatures & ~xrestore; +u64 init_bv;int ret; +/* Restore enabled features only. */ + xrestore_mask &= task_xfeatures; + init_bv = task_xfeatures & ~xrestore_mask;if (likely (!fx_only)) - ret =xrstor_from_user_sigframe (buf, xrestore); + ret =xrstor_from_user_sigframe (buf, xrestore_mask);else ret =fxrstor_from_user_sigframe (buf);
@@ -266,20 +269,18 @@ static int __restore_fpregs_from_user(void __user *buf, u64 ufeatures,* 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,bool fx_only) +static bool restore_fpregs_from_user (void __user *buf,u64 xrestore_mask,bool fx_only) {struct fpu *fpu =x86_task_fpu (current);int ret; -/* Restore enabled features only. */ - xrestore &= fpu->fpstate->user_xfeatures; retry:fpregs_lock ();/* Ensure that XFD is up to date */ xfd_update_state (fpu->fpstate);pagefault_disable (); ret =__restore_fpregs_from_user (buf, fpu->fpstate->user_xfeatures, - xrestore, fx_only); + xrestore_mask, fx_only);pagefault_enable ();if (unlikely (ret)) {
@@ -324,7 +325,7 @@ static bool restore_fpregs_from_user(void __user *buf, u64 xrestore, bool fx_onlreturn true; } -static bool __fpu_restore_sig (void __user *buf,void __user *buf_fx, +static bool __fpu_restore_sig (void __user *buf_f,void __user *buf_fx,bool ia32_fxstate) {struct task_struct *tsk = current;
@@ -332,7 +333,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,struct user_i387_ia32_struct env;bool success, fx_only = false;union fpregs_state *fpregs; -u64 user_xfeatures =0 ; +u64 xrestore_mask =0 ;if (use_xsave ()) {struct _fpx_sw_bytes fx_sw_user;
@@ -341,14 +342,14 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,return false; fx_only = !fx_sw_user.magic1; - user_xfeatures = fx_sw_user.xfeatures; + xrestore_mask = fx_sw_user.xfeatures; }else { - user_xfeatures =XFEATURE_MASK_FPSSE ; + xrestore_mask =XFEATURE_MASK_FPSSE ; }if (likely (!ia32_fxstate)) {/* Restore the FPU registers directly from user memory. */ -return restore_fpregs_from_user (buf_fx, user_xfeatures, fx_only); +return restore_fpregs_from_user (buf_fx, xrestore_mask, fx_only); }/*
@@ -356,7 +357,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,* to be ignored for histerical raisins. The legacy state is folded * in once the larger state has been copied. */ -if (__copy_from_user (&env, buf,sizeof (env))) +if (__copy_from_user (&env, buf_f,sizeof (env)))return false;/*
@@ -420,7 +421,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,* * Preserve supervisor states! */ -u64 mask = user_xfeatures |xfeatures_mask_supervisor (); +u64 mask = xrestore_mask |xfeatures_mask_supervisor (); fpregs->xsave.header.xfeatures &= mask; success = !os_xrstor_safe(fpu->fpstate,
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[tip: x86/fpu] x86/fpu: Clean up and rename variables in signal frame handling
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 344907fab5fdf2ab908e5d2f456cda87a51e9497 Gitweb: https://git.kernel.org/tip/344907fab5fdf2ab908e5d2f456cda87a51e9497 Author: Andrei Vagin <[email protected]> AuthorDate: Fri, 25 Sep 2026 16:24:48 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Fri, 25 Sep 2026 12:09:09 -07:00
x86/fpu: Clean up and rename variables in signal frame handling
Clean up signal frame handling code by renaming several variables for clarity and consistency, and moving masking logic closer to its usage.
- Rename 'fxbuf' to 'buf_fx' in check_xstate_in_sigframe() for consistency.
- Rename label 'setfx' to 'err_setfx' in check_xstate_in_sigframe() to indicate it is an error path.
- In __restore_fpregs_from_user(), rename 'ufeatures' to 'task_xfeatures' and 'xrestore' to 'xrestore_mask'.
- Move the masking logic 'xrestore_mask &= task_xfeatures' from restore_fpregs_from_user() into __restore_fpregs_from_user().
- Rename 'xrestore' to 'xrestore_mask' in restore_fpregs_from_user() to match the name in __restore_fpregs_from_user() and __fpu_restore_sig().
- In __fpu_restore_sig(), rename 'buf' to 'buf_f' to distinguish it from 'buf_fx', and 'user_xfeatures' to 'xrestore_mask'.
No functional changes.
--- arch/x86/kernel/fpu/signal.c | 41 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 20 deletions(-)
arch/x86/kernel/fpu/signal.c +21 -20
@@ -24,15 +24,15 @@* Check for the presence of extended state information in the * user fpstate pointer in the sigcontext. */ -static inline bool check_xstate_in_sigframe (struct fxregs_state __user *fxbuf, +static inline bool check_xstate_in_sigframe (struct fxregs_state __user *buf_fx,struct _fpx_sw_bytes *fx_sw) {int min_xstate_size =sizeof (struct fxregs_state ) +sizeof (struct xstate_header ); -void __user *fpstate = fxbuf; +void __user *fpstate = buf_fx;unsigned int magic2; -if (__copy_from_user (fx_sw, &fxbuf->sw_reserved[0 ],sizeof (*fx_sw))) +if (__copy_from_user (fx_sw, &buf_fx->sw_reserved[0 ],sizeof (*fx_sw)))return false;/* Check for the first magic field and other error scenarios. */
@@ -40,7 +40,7 @@ static inline bool check_xstate_in_sigframe(struct fxregs_state __user *fxbuf, 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) - goto setfx; + goto err_setfx;/* * Check for the presence of second magic word at the end of memory
@@ -53,7 +53,7 @@ static inline bool check_xstate_in_sigframe(struct fxregs_state __user *fxbuf,if (likely (magic2 ==FP_XSTATE_MAGIC2 ))return true; -setfx: +err_setfx:trace_x86_fpu_xstate_check_failed (x86_task_fpu (current));/* Set the parameters for fx only state */
@@ -240,15 +240,18 @@ retry:return true; } -static int __restore_fpregs_from_user (void __user *buf,u64 ufeatures, -u64 xrestore,bool fx_only) +static int __restore_fpregs_from_user (void __user *buf,u64 task_xfeatures, +u64 xrestore_mask,bool fx_only) {if (use_xsave ()) { -u64 init_bv = ufeatures & ~xrestore; +u64 init_bv;int ret; +/* Restore enabled features only. */ + xrestore_mask &= task_xfeatures; + init_bv = task_xfeatures & ~xrestore_mask;if (likely (!fx_only)) - ret =xrstor_from_user_sigframe (buf, xrestore); + ret =xrstor_from_user_sigframe (buf, xrestore_mask);else ret =fxrstor_from_user_sigframe (buf);
@@ -266,20 +269,18 @@ static int __restore_fpregs_from_user(void __user *buf, u64 ufeatures,* 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,bool fx_only) +static bool restore_fpregs_from_user (void __user *buf,u64 xrestore_mask,bool fx_only) {struct fpu *fpu =x86_task_fpu (current);int ret; -/* Restore enabled features only. */ - xrestore &= fpu->fpstate->user_xfeatures; retry:fpregs_lock ();/* Ensure that XFD is up to date */ xfd_update_state (fpu->fpstate);pagefault_disable (); ret =__restore_fpregs_from_user (buf, fpu->fpstate->user_xfeatures, - xrestore, fx_only); + xrestore_mask, fx_only);pagefault_enable ();if (unlikely (ret)) {
@@ -324,7 +325,7 @@ retry:return true; } -static bool __fpu_restore_sig (void __user *buf,void __user *buf_fx, +static bool __fpu_restore_sig (void __user *buf_f,void __user *buf_fx,bool ia32_fxstate) {struct task_struct *tsk = current;
@@ -332,7 +333,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,struct user_i387_ia32_struct env;bool success, fx_only = false;union fpregs_state *fpregs; -u64 user_xfeatures =0 ; +u64 xrestore_mask =0 ;if (use_xsave ()) {struct _fpx_sw_bytes fx_sw_user;
@@ -341,14 +342,14 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,return false; fx_only = !fx_sw_user.magic1; - user_xfeatures = fx_sw_user.xfeatures; + xrestore_mask = fx_sw_user.xfeatures; }else { - user_xfeatures =XFEATURE_MASK_FPSSE ; + xrestore_mask =XFEATURE_MASK_FPSSE ; }if (likely (!ia32_fxstate)) {/* Restore the FPU registers directly from user memory. */ -return restore_fpregs_from_user (buf_fx, user_xfeatures, fx_only); +return restore_fpregs_from_user (buf_fx, xrestore_mask, fx_only); }/*
@@ -356,7 +357,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,* to be ignored for histerical raisins. The legacy state is folded * in once the larger state has been copied. */ -if (__copy_from_user (&env, buf,sizeof (env))) +if (__copy_from_user (&env, buf_f,sizeof (env)))return false;/*
@@ -420,7 +421,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx,* * Preserve supervisor states! */ -u64 mask = user_xfeatures |xfeatures_mask_supervisor (); +u64 mask = xrestore_mask |xfeatures_mask_supervisor (); fpregs->xsave.header.xfeatures &= mask; success = !os_xrstor_safe(fpu->fpstate,
PATCH 3/7 x86/fpu: Extract restore_from_ia32_fxstate() and clean up fpu__restore_sig()
In reply to Andrei VaginWhen restoring an FPU signal frame for a 32-bit or IA32-compat task on FXSR-enabled systems, a legacy 32-bit FP frame is present alongside the FX/XSAVE frame. Because the legacy FP frame duplicates the FP state portion of the FX/XSAVE frame, for backward compatibility it is treated as the source of truth, and its state is folded into the FX/XSAVE state before restoring the registers.
Currently, most of __fpu_restore_sig() is dedicated to handling this 32-bit legacy/compat fpstate, while the native direct restore path lives in restore_fpregs_from_user(). Having the compat handling intermixed with the main signal restoration flow makes it tricky to quickly see what code is doing what.
Extract the 32-bit legacy/compat FPU restore handling into a separate helper function, restore_from_ia32_fxstate(), and inline the remainder of __fpu_restore_sig() directly into fpu__restore_sig().
--- arch/x86/kernel/fpu/signal.c | 56 +++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 23 deletions(-)
arch/x86/kernel/fpu/signal.c +33 -23
@@ -325,32 +325,26 @@ static bool restore_fpregs_from_user(void __user *buf, u64 xrestore_mask, bool freturn true; } -static bool __fpu_restore_sig (void __user *buf_f,void __user *buf_fx, -bool ia32_fxstate) +/* +* Restore FPU state from a signal frame when a legacy 32-bit FP frame +* (buf_f) is present. +* +* The legacy FP frame duplicates the FP state portion of the FX/XSAVE +* frame (buf_fx). For backward compatibility, the legacy FP frame is +* treated as the source of truth, and its state is folded into the +* FX/XSAVE state before restoring the registers. +*/ +static bool restore_from_ia32_fxstate (void __user *buf_f,void __user *buf_fx, +u64 xrestore_mask,bool fx_only) {struct task_struct *tsk = current;struct fpu *fpu =x86_task_fpu (tsk);struct user_i387_ia32_struct env; -bool success, fx_only = false;union fpregs_state *fpregs; -u64 xrestore_mask =0 ; - -if (use_xsave ()) { -struct _fpx_sw_bytes fx_sw_user; - -if (!check_xstate_in_sigframe (buf_fx, &fx_sw_user)) -return false; - - fx_only = !fx_sw_user.magic1; - xrestore_mask = fx_sw_user.xfeatures; - }else { - xrestore_mask =XFEATURE_MASK_FPSSE ; - } +bool success; -if (likely (!ia32_fxstate)) { -/* Restore the FPU registers directly from user memory. */ -return restore_fpregs_from_user (buf_fx, xrestore_mask, fx_only); - } +if (!IS_ENABLED (CONFIG_X86_32 ) && !IS_ENABLED (CONFIG_IA32_EMULATION )) +return false;/* * Copy the legacy state because the FP portion of the FX frame has
@@ -450,10 +444,11 @@ static inline unsigned int xstate_sigframe_size(struct fpstate *fpstate)bool fpu__restore_sig (void __user *buf,int ia32_frame) {struct fpu *fpu =x86_task_fpu (current); -void __user *buf_fx = buf; +bool success = false, fx_only = false;bool ia32_fxstate = false; -bool success = false; +void __user *buf_fx = buf;unsigned int size; +u64 xrestore_mask;if (unlikely (!buf)) {fpu__clear_user_states (fpu);
@@ -482,10 +477,25 @@ bool fpu__restore_sig(void __user *buf, int ia32_frame) success = !fpregs_soft_set (current,NULL ,0 ,sizeof (struct user_i387_ia32_struct ),NULL , buf); + goto out; + } + +if (use_xsave ()) { +struct _fpx_sw_bytes fx_sw_user; + +if (!check_xstate_in_sigframe (buf_fx, &fx_sw_user)) + goto out; + + fx_only = !fx_sw_user.magic1; + xrestore_mask = fx_sw_user.xfeatures; }else { - success =__fpu_restore_sig (buf, buf_fx, ia32_fxstate); + xrestore_mask =XFEATURE_MASK_FPSSE ; } +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); out:if (unlikely (!success))fpu__clear_user_states (fpu);
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[tip: x86/fpu] x86/fpu: Extract restore_from_ia32_fxstate() and clean up fpu__restore_sig()
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 6916dec7a02fe0f50d6dc80e5f5c55f11cebd874 Gitweb: https://git.kernel.org/tip/6916dec7a02fe0f50d6dc80e5f5c55f11cebd874 Author: Andrei Vagin <[email protected]> AuthorDate: Fri, 25 Sep 2026 16:24:49 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Fri, 25 Sep 2026 12:14:57 -07:00
x86/fpu: Extract restore_from_ia32_fxstate() and clean up fpu__restore_sig()
When restoring an FPU signal frame for a 32-bit or IA32-compat task on FXSR-enabled systems, a legacy 32-bit FP frame is present alongside the FX/XSAVE frame. Because the legacy FP frame duplicates the FP state portion of the FX/XSAVE frame, for backward compatibility it is treated as the source of truth, and its state is folded into the FX/XSAVE state before restoring the registers.
Currently, most of __fpu_restore_sig() is dedicated to handling this 32-bit legacy/compat fpstate, while the native direct restore path lives in restore_fpregs_from_user(). Having the compat handling intermixed with the main signal restoration flow makes it tricky to quickly see what code is doing what.
Extract the 32-bit legacy/compat FPU restore handling into a separate helper function, restore_from_ia32_fxstate(), and inline the remainder of __fpu_restore_sig() directly into fpu__restore_sig().
--- arch/x86/kernel/fpu/signal.c | 56 ++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 23 deletions(-)
arch/x86/kernel/fpu/signal.c +33 -23
@@ -325,32 +325,26 @@ retry:return true; } -static bool __fpu_restore_sig (void __user *buf_f,void __user *buf_fx, -bool ia32_fxstate) +/* +* Restore FPU state from a signal frame when a legacy 32-bit FP frame +* (buf_f) is present. +* +* The legacy FP frame duplicates the FP state portion of the FX/XSAVE +* frame (buf_fx). For backward compatibility, the legacy FP frame is +* treated as the source of truth, and its state is folded into the +* FX/XSAVE state before restoring the registers. +*/ +static bool restore_from_ia32_fxstate (void __user *buf_f,void __user *buf_fx, +u64 xrestore_mask,bool fx_only) {struct task_struct *tsk = current;struct fpu *fpu =x86_task_fpu (tsk);struct user_i387_ia32_struct env; -bool success, fx_only = false;union fpregs_state *fpregs; -u64 xrestore_mask =0 ; - -if (use_xsave ()) { -struct _fpx_sw_bytes fx_sw_user; - -if (!check_xstate_in_sigframe (buf_fx, &fx_sw_user)) -return false; - - fx_only = !fx_sw_user.magic1; - xrestore_mask = fx_sw_user.xfeatures; - }else { - xrestore_mask =XFEATURE_MASK_FPSSE ; - } +bool success; -if (likely (!ia32_fxstate)) { -/* Restore the FPU registers directly from user memory. */ -return restore_fpregs_from_user (buf_fx, xrestore_mask, fx_only); - } +if (!IS_ENABLED (CONFIG_X86_32 ) && !IS_ENABLED (CONFIG_IA32_EMULATION )) +return false;/* * Copy the legacy state because the FP portion of the FX frame has
@@ -450,10 +444,11 @@ static inline unsigned int xstate_sigframe_size(struct fpstate *fpstate)bool fpu__restore_sig (void __user *buf,int ia32_frame) {struct fpu *fpu =x86_task_fpu (current); -void __user *buf_fx = buf; +bool success = false, fx_only = false;bool ia32_fxstate = false; -bool success = false; +void __user *buf_fx = buf;unsigned int size; +u64 xrestore_mask;if (unlikely (!buf)) {fpu__clear_user_states (fpu);
@@ -482,10 +477,25 @@ bool fpu__restore_sig(void __user *buf, int ia32_frame) success = !fpregs_soft_set (current,NULL ,0 ,sizeof (struct user_i387_ia32_struct ),NULL , buf); + goto out; + } + +if (use_xsave ()) { +struct _fpx_sw_bytes fx_sw_user; + +if (!check_xstate_in_sigframe (buf_fx, &fx_sw_user)) + goto out; + + fx_only = !fx_sw_user.magic1; + xrestore_mask = fx_sw_user.xfeatures; }else { - success =__fpu_restore_sig (buf, buf_fx, ia32_fxstate); + xrestore_mask =XFEATURE_MASK_FPSSE ; } +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); out:if (unlikely (!success))fpu__clear_user_states (fpu);
PATCH 4/7 x86/fpu: Document reasoning of FX-only fallback
In reply to Andrei VaginAdd a comment to check_xstate_in_sigframe() to explain the reasoning behind falling back to the FX-only state when signal frame metadata is inconsistent.
The fallback is intended to preserve backward compatibility with legacy user-space processes that are not aware of XSAVE states and might only fill or copy the legacy FP state.
However, this fallback should be avoided whenever possible. If a process was actively using extended features, falling back to the FX-only state silently resets those extended registers to their initial state, which can lead to silent user-space state corruption.
--- arch/x86/kernel/fpu/signal.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/kernel/fpu/signal.c +8 -0
@@ -54,6 +54,14 @@ static inline bool check_xstate_in_sigframe(struct fxregs_state __user *buf_fx,if (likely (magic2 ==FP_XSTATE_MAGIC2 ))return true; err_setfx: +/* +* The fallback to FX-only state is used to preserve backward +* compatibility with user-space processes that are not aware of xsave +* states. +* +* In all other cases, returning false (to trigger SIGSEGV) is +* preferred to avoid silent user-space state corruption. +*/ trace_x86_fpu_xstate_check_failed (x86_task_fpu (current));/* Set the parameters for fx only state */
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[tip: x86/fpu] x86/fpu: Document reasoning of FX-only fallback
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 731300df4f7d7d7153a2aa23b6c4992df3d2a60b Gitweb: https://git.kernel.org/tip/731300df4f7d7d7153a2aa23b6c4992df3d2a60b Author: Andrei Vagin <[email protected]> AuthorDate: Fri, 25 Sep 2026 16:24:50 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Fri, 25 Sep 2026 12:21:14 -07:00
x86/fpu: Document reasoning of FX-only fallback
Add a comment to check_xstate_in_sigframe() to explain the reasoning behind falling back to the FX-only state when signal frame metadata is inconsistent.
The fallback is intended to preserve backward compatibility with legacy user-space processes that are not aware of XSAVE states and might only fill or copy the legacy FP state.
However, this fallback should be avoided whenever possible. If a process was actively using extended features, falling back to the FX-only state silently resets those extended registers to their initial state, which can lead to silent user-space state corruption.
--- arch/x86/kernel/fpu/signal.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/kernel/fpu/signal.c +8 -0
@@ -54,6 +54,14 @@ static inline bool check_xstate_in_sigframe(struct fxregs_state __user *buf_fx,if (likely (magic2 ==FP_XSTATE_MAGIC2 ))return true; err_setfx: +/* +* The fallback to FX-only state is used to preserve backward +* compatibility with user-space processes that are not aware of xsave +* states. +* +* In all other cases, returning false (to trigger SIGSEGV) is +* preferred to avoid silent user-space state corruption. +*/ trace_x86_fpu_xstate_check_failed (x86_task_fpu (current));/* Set the parameters for fx only state */
PATCH 5/7 x86/fpu: Fix potential underflow in xstate_calculate_size()
In reply to Andrei Vaginxstate_calculate_size() calculates the size required for a given set of xfeatures. It determines the topmost feature by finding the most significant bit in xfeatures using fls64(xfeatures) - 1.
If xfeatures is 0, fls64(0) returns 0, and topmost becomes -1. Previously, topmost was unsigned int, so -1 underflowed to UINT_MAX. This caused the subsequent check topmost <= XFEATURE_SSE to fail, and the code proceeded to access xstate arrays using topmost (UINT_MAX) as an index, leading to an out-of-bounds access.
Fix this by checking if xfeatures only contains legacy features (FP/SSE) or is empty (!(xfeatures & ~XFEATURE_MASK_FPSSE)) before calculating topmost.
--- arch/x86/kernel/fpu/xstate.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kernel/fpu/xstate.c +4 -3
@@ -589,12 +589,13 @@ static bool __init check_xstate_against_struct(int nr)static unsigned int xstate_calculate_size (u64 xfeatures,bool compacted) { -unsigned int topmost =fls64 (xfeatures) -1 ; -unsigned int offset, i; +unsigned int topmost, offset, i; -if (topmost <=XFEATURE_SSE ) +if (!(xfeatures & ~XFEATURE_MASK_FPSSE ))return sizeof (struct xregs_state ); + topmost =fls64 (xfeatures) -1 ; +if (compacted) { offset =xfeature_get_offset (xfeatures, topmost); }else {
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[tip: x86/fpu] x86/fpu: Fix potential underflow in xstate_calculate_size()
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: f1751b220bc8a9bd2dc38ecf61f5c29f3826ace0 Gitweb: https://git.kernel.org/tip/f1751b220bc8a9bd2dc38ecf61f5c29f3826ace0 Author: Andrei Vagin <[email protected]> AuthorDate: Fri, 25 Sep 2026 16:24:51 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Fri, 25 Sep 2026 12:25:54 -07:00
x86/fpu: Fix potential underflow in xstate_calculate_size()
xstate_calculate_size() calculates the size required for a given set of xfeatures. It determines the topmost feature by finding the most significant bit in xfeatures using fls64(xfeatures) - 1.
If xfeatures is 0, fls64(0) returns 0, and topmost becomes -1. Previously, topmost was unsigned int, so -1 underflowed to UINT_MAX. This caused the subsequent check topmost <= XFEATURE_SSE to fail, and the code proceeded to access xstate arrays using topmost (UINT_MAX) as an index, leading to an out-of-bounds access.
[ bp: Remove text explaining what the patch does. ]
--- arch/x86/kernel/fpu/xstate.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kernel/fpu/xstate.c +4 -3
@@ -589,12 +589,13 @@ static bool __init check_xstate_against_struct(int nr)static unsigned int xstate_calculate_size (u64 xfeatures,bool compacted) { -unsigned int topmost =fls64 (xfeatures) -1 ; -unsigned int offset, i; +unsigned int topmost, offset, i; -if (topmost <=XFEATURE_SSE ) +if (!(xfeatures & ~XFEATURE_MASK_FPSSE ))return sizeof (struct xregs_state ); + topmost =fls64 (xfeatures) -1 ; +if (compacted) { offset =xfeature_get_offset (xfeatures, topmost); }else {
PATCH 6/7 x86/fpu: Pre-fault only required size of xstate buffer
In reply to Andrei VaginThe 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.
--- 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(-)
arch/x86/include/asm/fpu/xstate.h +2 -0
@@ -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
arch/x86/kernel/fpu/signal.c +29 -9
@@ -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 fif (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);
arch/x86/kernel/fpu/xstate.c +1 -1
@@ -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;
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[tip: x86/fpu] x86/fpu: Pre-fault only required size of xstate buffer
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: fd14edd82077b8655273b6d32503903a2ee35ffd Gitweb: https://git.kernel.org/tip/fd14edd82077b8655273b6d32503903a2ee35ffd Author: Andrei Vagin <[email protected]> AuthorDate: Fri, 25 Sep 2026 16:24:52 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Fri, 25 Sep 2026 12:26:05 -07:00
x86/fpu: Pre-fault only required size of xstate buffer
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.
--- 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(-)
arch/x86/include/asm/fpu/xstate.h +2 -0
@@ -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
arch/x86/kernel/fpu/signal.c +29 -9
@@ -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 @@ retry: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);
arch/x86/kernel/fpu/xstate.c +1 -1
@@ -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;
PATCH 7/7 selftests/x86: Add tests for signal frame FPU portability
In reply to Andrei VaginAdd a new selftest tools/testing/selftests/x86/sigframe_fpu_portability.c to verify signal frame portability and consistency when the xstate size is shrunk:
- test_valid_shrunk_xstate_size: Verifies that the kernel correctly restores the xstate context from a signal frame where xstate_size has been manually shrunk to only cover active features, as long as the FP_XSTATE_MAGIC2 marker is correctly placed. This simulates migrating a process created on a host with fewer xstate features to a host with more features.
- test_invalid_shrunk_xstate_size: Verifies that the kernel rejects (via SIGSEGV) a signal frame where xstate_size is smaller than required by the enabled features in the xfeatures mask.
--- tools/testing/selftests/x86/Makefile | 5 +- .../selftests/x86/sigframe_fpu_portability.c | 235 ++++++++++++++++++ tools/testing/selftests/x86/xstate.c | 12 - tools/testing/selftests/x86/xstate.h | 20 ++ 4 files changed, 259 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/x86/sigframe_fpu_portability.c
tools/testing/selftests/x86/Makefile +4 -1
@@ -19,7 +19,8 @@ TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \ test_FCMOV test_FCOMI test_FISTTP \ vdso_restorer TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \ - corrupt_xstate_header amx lam test_shadow_stack avx apx + corrupt_xstate_header amx lam test_shadow_stack avx apx \ + sigframe_fpu_portability # Some selftests require 32bit support enabled also on 64bit systems TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
@@ -138,3 +139,5 @@ $(OUTPUT)/avx_64: CFLAGS += -mno-avx -mno-avx512f $(OUTPUT)/amx_64: EXTRA_FILES += xstate.c $(OUTPUT)/avx_64: EXTRA_FILES += xstate.c $(OUTPUT)/apx_64: EXTRA_FILES += xstate.c + +$(OUTPUT)/sigframe_fpu_portability_64: CFLAGS += -mno-avx -mno-avx512f
tools/testing/selftests/x86/sigframe_fpu_portability.c new file +235 -0
235 lines not on this page · raw message
tools/testing/selftests/x86/xstate.c +0 -12
@@ -34,18 +34,6 @@ (1 << XFEATURE_XTILEDATA) | \ (1 << XFEATURE_APX)) -static inline uint64_t xgetbv (uint32_t index) -{ -uint32_t eax, edx; - - asmvolatile ("xgetbv" :"=a" (eax),"=d" (edx) :"c" (index)); -return eax + ((uint64_t )edx <<32 ); -} - -static inline uint64_t get_xstatebv (struct xsave_buffer *xbuf) -{ -return *(uint64_t *)(&xbuf->header); -}static struct xstate_info xstate;
tools/testing/selftests/x86/xstate.h +20 -0
@@ -3,6 +3,8 @@#define __SELFTESTS_X86_XSTATE_H#include <stdint.h> +#include <stdlib.h> +#include <string.h> #include "kselftest.h"
@@ -94,6 +96,14 @@ static inline void xrstor(struct xsave_buffer *xbuf, uint64_t rfbm) : : "D" (xbuf), "a" (rfbm_lo), "d" (rfbm_hi)); } +static inline uint64_t xgetbv (uint32_t index) +{ +uint32_t eax, edx; + + asmvolatile ("xgetbv" :"=a" (eax),"=d" (edx) :"c" (index)); +return eax + ((uint64_t )edx <<32 ); +} +#define CPUID_LEAF_XSTATE 0xd#define CPUID_SUBLEAF_XSTATE_USER 0x0
@@ -160,6 +170,11 @@ static inline void set_xstatebv(struct xsave_buffer *xbuf, uint64_t bv) *(uint64_t *)(&xbuf->header) = bv; } +static inline uint64_t get_xstatebv (struct xsave_buffer *xbuf) +{ +return *(uint64_t *)(&xbuf->header); +} +/* See 'struct _fpx_sw_bytes' at sigcontext.h */ #define SW_BYTES_OFFSET 464/* N.B. The struct's field name varies so read from the offset. */
@@ -175,6 +190,11 @@ static inline uint64_t get_fpx_sw_bytes_features(void *buffer)return *(uint64_t *)(buffer +SW_BYTES_BV_OFFSET ); } +static inline void set_fpx_sw_bytes_features (void *buffer,uint64_t features) +{ + *(uint64_t *)(buffer +SW_BYTES_BV_OFFSET ) = features; +} +static inline void set_rand_data (struct xstate_info *xstate,struct xsave_buffer *xbuf) {int *ptr = (int *)&xbuf->bytes[xstate->xbuf_offset];
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[tip: x86/fpu] selftests/x86: Add tests for signal frame FPU portability
The following commit has been merged into the x86/fpu branch of tip:
The following commit has been merged into the x86/fpu branch of tip:
Commit-ID: 391cac638eddae8f4a0a8dc8168a260f939eb3eb Gitweb: https://git.kernel.org/tip/391cac638eddae8f4a0a8dc8168a260f939eb3eb Author: Andrei Vagin <[email protected]> AuthorDate: Fri, 25 Sep 2026 16:24:53 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Fri, 25 Sep 2026 12:43:41 -07:00
selftests/x86: Add tests for signal frame FPU portability
Add a new selftest tools/testing/selftests/x86/sigframe_fpu_portability.c to verify signal frame portability and consistency when the xstate size is shrunk:
- test_valid_shrunk_xstate_size: Verifies that the kernel correctly restores the xstate context from a signal frame where xstate_size has been manually shrunk to only cover active features, as long as the FP_XSTATE_MAGIC2 marker is correctly placed. This simulates migrating a process created on a host with fewer xstate features to a host with more features.
- test_invalid_shrunk_xstate_size: Verifies that the kernel rejects (via SIGSEGV) a signal frame where xstate_size is smaller than required by the enabled features in the xfeatures mask.
--- tools/testing/selftests/x86/Makefile | 5 +- tools/testing/selftests/x86/sigframe_fpu_portability.c | 235 ++++++++- tools/testing/selftests/x86/xstate.c | 12 +- tools/testing/selftests/x86/xstate.h | 20 +- 4 files changed, 259 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/x86/sigframe_fpu_portability.c
tools/testing/selftests/x86/Makefile +4 -1
@@ -19,7 +19,8 @@ TARGETS_C_32BIT_ONLY := entry_from_vm86 test_syscall_vdso unwind_vdso \ test_FCMOV test_FCOMI test_FISTTP \ vdso_restorer TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \ - corrupt_xstate_header amx lam test_shadow_stack avx apx + corrupt_xstate_header amx lam test_shadow_stack avx apx \ + sigframe_fpu_portability # Some selftests require 32bit support enabled also on 64bit systems TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
@@ -138,3 +139,5 @@ $(OUTPUT)/avx_64: CFLAGS += -mno-avx -mno-avx512f $(OUTPUT)/amx_64: EXTRA_FILES += xstate.c $(OUTPUT)/avx_64: EXTRA_FILES += xstate.c $(OUTPUT)/apx_64: EXTRA_FILES += xstate.c + +$(OUTPUT)/sigframe_fpu_portability_64: CFLAGS += -mno-avx -mno-avx512f
tools/testing/selftests/x86/sigframe_fpu_portability.c new file +235 -0
235 lines not on this page · raw message
tools/testing/selftests/x86/xstate.c +0 -12
@@ -34,18 +34,6 @@ (1 << XFEATURE_XTILEDATA) | \ (1 << XFEATURE_APX)) -static inline uint64_t xgetbv (uint32_t index) -{ -uint32_t eax, edx; - - asmvolatile ("xgetbv" :"=a" (eax),"=d" (edx) :"c" (index)); -return eax + ((uint64_t )edx <<32 ); -} - -static inline uint64_t get_xstatebv (struct xsave_buffer *xbuf) -{ -return *(uint64_t *)(&xbuf->header); -}static struct xstate_info xstate;
tools/testing/selftests/x86/xstate.h +20 -0
@@ -3,6 +3,8 @@#define __SELFTESTS_X86_XSTATE_H#include <stdint.h> +#include <stdlib.h> +#include <string.h> #include "kselftest.h"
@@ -94,6 +96,14 @@ static inline void xrstor(struct xsave_buffer *xbuf, uint64_t rfbm) : : "D" (xbuf), "a" (rfbm_lo), "d" (rfbm_hi)); } +static inline uint64_t xgetbv (uint32_t index) +{ +uint32_t eax, edx; + + asmvolatile ("xgetbv" :"=a" (eax),"=d" (edx) :"c" (index)); +return eax + ((uint64_t )edx <<32 ); +} +#define CPUID_LEAF_XSTATE 0xd#define CPUID_SUBLEAF_XSTATE_USER 0x0
@@ -160,6 +170,11 @@ static inline void set_xstatebv(struct xsave_buffer *xbuf, uint64_t bv) *(uint64_t *)(&xbuf->header) = bv; } +static inline uint64_t get_xstatebv (struct xsave_buffer *xbuf) +{ +return *(uint64_t *)(&xbuf->header); +} +/* See 'struct _fpx_sw_bytes' at sigcontext.h */ #define SW_BYTES_OFFSET 464/* N.B. The struct's field name varies so read from the offset. */
@@ -175,6 +190,11 @@ static inline uint64_t get_fpx_sw_bytes_features(void *buffer)return *(uint64_t *)(buffer +SW_BYTES_BV_OFFSET ); } +static inline void set_fpx_sw_bytes_features (void *buffer,uint64_t features) +{ + *(uint64_t *)(buffer +SW_BYTES_BV_OFFSET ) = features; +} +static inline void set_rand_data (struct xstate_info *xstate,struct xsave_buffer *xbuf) {int *ptr = (int *)&xbuf->bytes[xstate->xbuf_offset];