Skip to contentSkip to conversation

x86/fpu: Mop up XSAVES and related damage

PATCH Superseded Thomas Gleixner 6 participants 33 messages Jun 2, 2021 – Jun 5, 2021

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover x86/fpu: Mop up XSAVES and related damage

syszbot reported a warnon for XRSTOR raising #GP:

https://lore.kernel.org/r/[email protected]

with a syzcaller reproducer and a conclusive bisect result.

It took a while to destill a simple C reproducer out of it which allowed to pin point the root cause: The recent addition of supervisor XSTATEs broke the signal restore path for the case where the signal handler wreckaged the XSTATE on stack because it does not sanitize the XSTATE header which causes a subsequent XRSTOR to fail and #GP.

The following series addresses the problem and fixes related issues which were found while inspecting the related changes.

Thanks to Andy and Dave for working on this with me!

Thanks,

tglx

---
 arch/x86/include/asm/fpu/xstate.h                     |    4 
 arch/x86/kernel/fpu/core.c                            |   62 ++++++---
 arch/x86/kernel/fpu/regset.c                          |   43 ++----
 arch/x86/kernel/fpu/signal.c                          |   30 +++-
 arch/x86/kernel/fpu/xstate.c                          |   95 +++++----------
 b/tools/testing/selftests/x86/corrupt_xstate_header.c |  114 ++++++++++++++++++
 tools/testing/selftests/x86/Makefile                  |    3 
 7 files changed, 234 insertions(+), 117 deletions(-)
#1 lore raw

PATCH 1/8 selftests/x86: Test signal frame XSTATE header corruption handling

From: Andy Lutomirski <[email protected]>

This is very heavily based on some code from Thomas Gleixner. On a system without XSAVES, it triggers the WARN_ON():

    Bad FPU state detected at copy_kernel_to_fpregs+0x2f/0x40, reinitializing FPU registers.
---
 tools/testing/selftests/x86/Makefile                |    3 
 tools/testing/selftests/x86/corrupt_xstate_header.c |  114 ++++++++++++++++++++
 2 files changed, 116 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/x86/corrupt_xstate_header.c
tools/testing/selftests/x86/Makefile +2 -1
@@ -17,7 +17,8 @@ TARGETS_C_BOTHBITS := single_step_syscal
 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
+TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \
+			corrupt_xstate_header
 # Some selftests require 32bit support enabled also on 64bit systems
 TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
 
tools/testing/selftests/x86/corrupt_xstate_header.c new file +114 -0

114 lines not on this page · raw message

maintainer
#2 lore raw

Re: [patch 1/8] selftests/x86: Test signal frame XSTATE header corruption handling

Quoted 6 lines · Thomas Gleixner

From: Andy Lutomirski <[email protected]>

This is very heavily based on some code from Thomas Gleixner. On a system without XSAVES, it triggers the WARN_ON():

    Bad FPU state detected at copy_kernel_to_fpregs+0x2f/0x40, reinitializing FPU registers.

That triggers

[  149.497274] corrupt_xstate_[1627] bad frame in rt_sigreturn frame:00000000dad08ab1 ip:7f031449ffe1 sp:7ffd0c5c59f0 orax:ffffffffffffffff in libpthread-2.31.so[7f0314493000+10000]

on an AMD laptop here.

Quoted 13 lines
tools/testing/selftests/x86/corrupt_xstate_header.c new file +13 -0
+static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
+			   unsigned int *ecx, unsigned int *edx)
+{
+	asm volatile(
+		"cpuid;"
+		: "=a" (*eax),
+		  "=b" (*ebx),
+		  "=c" (*ecx),
+		  "=d" (*edx)
+		: "0" (*eax), "2" (*ecx));
+}
+
+static inline int xsave_enabled(void)

tools/testing/selftests/x86/corrupt_xstate_header.c line 42

Quoted 10 lines
tools/testing/selftests/x86/corrupt_xstate_header.c new file +10 -0
+{
+	unsigned int eax, ebx, ecx, edx;
+
+	eax = 0x1;
+	ecx = 0x0;
+	__cpuid(&eax, &ebx, &ecx, &edx);
+
+	/* Is CR4.OSXSAVE enabled ? */
+	return ecx & (1U << 27);
+}

One fine day someone should sit down and unify all those auxillary functions used in the selftests into a lib...

Quoted 21 lines
tools/testing/selftests/x86/corrupt_xstate_header.c new file +21 -0
+
+static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
+		       int flags)
+{
+	struct sigaction sa;
+
+	memset(&sa, 0, sizeof(sa));
+	sa.sa_sigaction = handler;
+	sa.sa_flags = SA_SIGINFO | flags;
+	sigemptyset(&sa.sa_mask);
+	if (sigaction(sig, &sa, 0))
+		err(1, "sigaction");
+}
+
+static void sigusr1(int sig, siginfo_t *info, void *uc_void)
+{
+	ucontext_t *uc = uc_void;
+	uint8_t *fpstate = (uint8_t *)uc->uc_mcontext.fpregs;
+	uint64_t *xfeatures = (uint64_t *)(fpstate + 512);
+
+	printf("\tWreckage XSTATE header\n");

tools/testing/selftests/x86/corrupt_xstate_header.c line 73

Quoted 10 lines
tools/testing/selftests/x86/corrupt_xstate_header.c new file +10 -0
+	/* Wreckage the first reserved byte in the header */
+	*(xfeatures + 2) = 0xfffffff;
+}
+
+static void sigsegv(int sig, siginfo_t *info, void *uc_void)
+{
+	printf("\tGot SIGSEGV\n");
+}
+
+int main()

ERROR: Bad function definition - int main() should probably be int main(void)

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#3 lore raw

Re: [patch 1/8] selftests/x86: Test signal frame XSTATE header corruption handling

Quoted 6 lines · Borislav Petkov
Quoted 5 lines · Thomas Gleixner

From: Andy Lutomirski <[email protected]>

This is very heavily based on some code from Thomas Gleixner. On a system without XSAVES, it triggers the WARN_ON():

Quoted 7 lines
Quoted 1 line
    Bad FPU state detected at copy_kernel_to_fpregs+0x2f/0x40, reinitializing FPU registers.

That triggers

[  149.497274] corrupt_xstate_[1627] bad frame in rt_sigreturn frame:00000000dad08ab1 ip:7f031449ffe1 sp:7ffd0c5c59f0 orax:ffffffffffffffff in libpthread-2.31.so[7f0314493000+10000]

on an AMD laptop here.

Yes, that's the ratelimited printk in the signal code.

Quoted 18 lines
Quoted 18 lines
tools/testing/selftests/x86/corrupt_xstate_header.c new file +18 -0
+static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
+			   unsigned int *ecx, unsigned int *edx)
+{
+	asm volatile(
+		"cpuid;"
+		: "=a" (*eax),
+		  "=b" (*ebx),
+		  "=c" (*ecx),
+		  "=d" (*edx)
+		: "0" (*eax), "2" (*ecx));
+}
+
+static inline int xsave_enabled(void)
+{
+	unsigned int eax, ebx, ecx, edx;
+
+	eax = 0x1;
+	ecx = 0x0;
Quoted 8 lines

tools/testing/selftests/x86/corrupt_xstate_header.c line 42

Quoted 5 lines
tools/testing/selftests/x86/corrupt_xstate_header.c new file +5 -0
+	__cpuid(&eax, &ebx, &ecx, &edx);
+
+	/* Is CR4.OSXSAVE enabled ? */
+	return ecx & (1U << 27);
+}

One fine day someone should sit down and unify all those auxillary functions used in the selftests into a lib...

Yes please. Shuah, that would be a great newcomer task...

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+int main()

ERROR: Bad function definition - int main() should probably be int main(void)

Bah, I thought I had fixed that.

maintainer
#4 lore raw

Re: [patch 1/8] selftests/x86: Test signal frame XSTATE header corruption handling

Quoted 44 lines · Thomas Gleixner
Quoted 6 lines · Borislav Petkov
Quoted 5 lines · Thomas Gleixner

From: Andy Lutomirski <[email protected]>

This is very heavily based on some code from Thomas Gleixner. On a system without XSAVES, it triggers the WARN_ON():

Quoted 7 lines
Quoted 1 line
     Bad FPU state detected at copy_kernel_to_fpregs+0x2f/0x40, reinitializing FPU registers.

That triggers

[  149.497274] corrupt_xstate_[1627] bad frame in rt_sigreturn frame:00000000dad08ab1 ip:7f031449ffe1 sp:7ffd0c5c59f0 orax:ffffffffffffffff in libpthread-2.31.so[7f0314493000+10000]

on an AMD laptop here.

Yes, that's the ratelimited printk in the signal code.

Quoted 18 lines
Quoted 18 lines
tools/testing/selftests/x86/corrupt_xstate_header.c new file +18 -0
+static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
+			   unsigned int *ecx, unsigned int *edx)
+{
+	asm volatile(
+		"cpuid;"
+		: "=a" (*eax),
+		  "=b" (*ebx),
+		  "=c" (*ecx),
+		  "=d" (*edx)
+		: "0" (*eax), "2" (*ecx));
+}
+
+static inline int xsave_enabled(void)
+{
+	unsigned int eax, ebx, ecx, edx;
+
+	eax = 0x1;
+	ecx = 0x0;
Quoted 8 lines

tools/testing/selftests/x86/corrupt_xstate_header.c line 42

Quoted 5 lines
tools/testing/selftests/x86/corrupt_xstate_header.c new file +5 -0
+	__cpuid(&eax, &ebx, &ecx, &edx);
+
+	/* Is CR4.OSXSAVE enabled ? */
+	return ecx & (1U << 27);
+}

One fine day someone should sit down and unify all those auxillary functions used in the selftests into a lib...

Quoted 2 lines

Yes please. Shuah, that would be a great newcomer task...

Yes. I will add to newcomer task list.

thanks,
-- Shuah

#5 lore raw

PATCH v2 1/8 selftests/x86: Test signal frame XSTATE header corruption handling

In reply to Thomas Gleixner

From: Andy Lutomirski <[email protected]>

This is very heavily based on some code from Thomas Gleixner. On a system without XSAVES, it triggers the WARN_ON():

    Bad FPU state detected at copy_kernel_to_fpregs+0x2f/0x40, reinitializing FPU registers.

---
V2: main() -> main(void) (Borislav)

---
 tools/testing/selftests/x86/Makefile                |    3 
 tools/testing/selftests/x86/corrupt_xstate_header.c |  114 ++++++++++++++++++++
 2 files changed, 116 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/x86/corrupt_xstate_header.c
tools/testing/selftests/x86/Makefile +2 -1
@@ -17,7 +17,8 @@ TARGETS_C_BOTHBITS := single_step_syscal
 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
+TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \
+			corrupt_xstate_header
 # Some selftests require 32bit support enabled also on 64bit systems
 TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
 
tools/testing/selftests/x86/corrupt_xstate_header.c new file +114 -0

114 lines not on this page · raw message

#6 lore raw

PATCH 1/8 selftests/x86: Test signal frame XSTATE header corruption handling

From: Andy Lutomirski <[email protected]>

This is very heavily based on some code from Thomas Gleixner. On a system without XSAVES, it triggers the WARN_ON():

    Bad FPU state detected at copy_kernel_to_fpregs+0x2f/0x40, reinitializing FPU registers.

---
V2a: main() -> main(void) for real ....

---
 tools/testing/selftests/x86/Makefile                |    3 
 tools/testing/selftests/x86/corrupt_xstate_header.c |  114 ++++++++++++++++++++
 2 files changed, 116 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/x86/corrupt_xstate_header.c
tools/testing/selftests/x86/Makefile +2 -1
@@ -17,7 +17,8 @@ TARGETS_C_BOTHBITS := single_step_syscal
 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
+TARGETS_C_64BIT_ONLY := fsgsbase sysret_rip syscall_numbering \
+			corrupt_xstate_header
 # Some selftests require 32bit support enabled also on 64bit systems
 TARGETS_C_32BIT_NEEDED := ldt_gdt ptrace_syscall
 
tools/testing/selftests/x86/corrupt_xstate_header.c new file +114 -0

114 lines not on this page · raw message

#7 lore raw

PATCH 2/8 x86/fpu: Prevent state corruption in __fpu__restore_sig()

In reply to Thomas Gleixner

From: Thomas Gleixner <[email protected]>

The non-compacted slowpath uses __copy_from_user() and copies the entire user buffer into the kernel buffer, verbatim. This means that the kernel buffer may now contain entirely invalid state on which XRSTOR will #GP. validate_user_xstate_header() can detect some of that corruption, but that leaves the onus on callers to clear the buffer.

Prior to XSAVES support it was possible just to reinitialize the buffer, completely, but with supervisor states that is not longer possible as the buffer clearing code split got it backwards. Fixing that is possible, but not corrupting the state in the first place is more robust.

Avoid corruption of the kernel XSAVE buffer by using copy_user_to_xstate() which validates the XSAVE header contents before copying the actual states to the kernel. copy_user_to_xstate() was previously only called for compacted-format kernel buffers, but it works for both compacted and non-compacted forms.

Using it for the non-compacted form is slower because of multiple __copy_from_user() operations, but that cost is less important than robust code in an already slow path.

[ Changelog polished by Dave Hansen ]

Fixes b860eb8dce59 ("x86/fpu/xstate: Define new functions for clearing fpregs and xstates")
Reported-by [email protected]
---
 arch/x86/include/asm/fpu/xstate.h |    4 ----
 arch/x86/kernel/fpu/signal.c      |    9 +--------
 arch/x86/kernel/fpu/xstate.c      |    2 +-
 3 files changed, 2 insertions(+), 13 deletions(-)
arch/x86/include/asm/fpu/xstate.h +0 -4
@@ -112,8 +112,4 @@ void copy_supervisor_to_kernel(struct xr
 void copy_dynamic_supervisor_to_kernel(struct xregs_state *xstate, u64 mask);
 void copy_kernel_to_dynamic_supervisor(struct xregs_state *xstate, u64 mask);
 
-
-/* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
-int validate_user_xstate_header(const struct xstate_header *hdr);
-
 #endif
arch/x86/kernel/fpu/signal.c +1 -8
@@ -405,14 +405,7 @@ static int __fpu__restore_sig(void __use
 	if (use_xsave() && !fx_only) {
 		u64 init_bv = xfeatures_mask_user() & ~user_xfeatures;
 
-		if (using_compacted_format()) {
-			ret = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
-		} else {
-			ret = __copy_from_user(&fpu->state.xsave, buf_fx, state_size);
-
-			if (!ret && state_size > offsetof(struct xregs_state, header))
-				ret = validate_user_xstate_header(&fpu->state.xsave.header);
-		}
+		ret = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
 		if (ret)
 			goto err_out;
 
arch/x86/kernel/fpu/xstate.c +1 -1
@@ -515,7 +515,7 @@ int using_compacted_format(void)
 }
 
 /* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
-int validate_user_xstate_header(const struct xstate_header *hdr)
+static int validate_user_xstate_header(const struct xstate_header *hdr)
 {
 	/* No unknown or supervisor features may be set */
 	if (hdr->xfeatures & ~xfeatures_mask_user())
maintainer
#8 lore raw

Re: [patch 2/8] x86/fpu: Prevent state corruption in __fpu__restore_sig()

Quoted 19 lines · Thomas Gleixner

From: Thomas Gleixner <[email protected]>

The non-compacted slowpath uses __copy_from_user() and copies the entire user buffer into the kernel buffer, verbatim. This means that the kernel buffer may now contain entirely invalid state on which XRSTOR will #GP. validate_user_xstate_header() can detect some of that corruption, but that leaves the onus on callers to clear the buffer.

Prior to XSAVES support it was possible just to reinitialize the buffer, completely, but with supervisor states that is not longer possible as the buffer clearing code split got it backwards. Fixing that is possible, but not corrupting the state in the first place is more robust.

Avoid corruption of the kernel XSAVE buffer by using copy_user_to_xstate() which validates the XSAVE header contents before copying the actual states to the kernel. copy_user_to_xstate() was previously only called for compacted-format kernel buffers, but it works for both compacted and non-compacted forms.

Quoted 5 lines

Using it for the non-compacted form is slower because of multiple __copy_from_user() operations, but that cost is less important than robust code in an already slow path.

[ Changelog polished by Dave Hansen ]

Nice polishing!

Quoted 42 lines
Fixes b860eb8dce59 ("x86/fpu/xstate: Define new functions for clearing fpregs and xstates")
Reported-by [email protected]
---
 arch/x86/include/asm/fpu/xstate.h |    4 ----
 arch/x86/kernel/fpu/signal.c      |    9 +--------
 arch/x86/kernel/fpu/xstate.c      |    2 +-
 3 files changed, 2 insertions(+), 13 deletions(-)
arch/x86/include/asm/fpu/xstate.h +0 -4
@@ -112,8 +112,4 @@ void copy_supervisor_to_kernel(struct xr
 void copy_dynamic_supervisor_to_kernel(struct xregs_state *xstate, u64 mask);
 void copy_kernel_to_dynamic_supervisor(struct xregs_state *xstate, u64 mask);
 
-
-/* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
-int validate_user_xstate_header(const struct xstate_header *hdr);
-
 #endif
arch/x86/kernel/fpu/signal.c +1 -8
@@ -405,14 +405,7 @@ static int __fpu__restore_sig(void __use
 	if (use_xsave() && !fx_only) {
 		u64 init_bv = xfeatures_mask_user() & ~user_xfeatures;
 
-		if (using_compacted_format()) {
-			ret = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
-		} else {
-			ret = __copy_from_user(&fpu->state.xsave, buf_fx, state_size);
-
-			if (!ret && state_size > offsetof(struct xregs_state, header))
-				ret = validate_user_xstate_header(&fpu->state.xsave.header);
-		}
+		ret = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
 		if (ret)
 			goto err_out;
 

arch/x86/kernel/fpu/xstate.c int using_compacted_format(void) · line 518

Quoted 5 lines
arch/x86/kernel/fpu/xstate.c +1 -1
@@ -515,7 +515,7 @@ int using_compacted_format(void)
 }
 
 /* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
-int validate_user_xstate_header(const struct xstate_header *hdr)
+static int validate_user_xstate_header(const struct xstate_header *hdr)

Can't do that yet - that one is still called from regset.c:

arch/x86/kernel/fpu/regset.c: In function ‘xstateregs_set’:
arch/x86/kernel/fpu/regset.c:135:10: error: implicit declaration of function ‘validate_user_xstate_header’ [-Werror=implicit-function-declaration]

  135 |    ret = validate_user_xstate_header(&xsave->header);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors

Maybe after the 5th patch which kills that usage too.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#9 lore raw

Re: [patch 2/8] x86/fpu: Prevent state corruption in __fpu__restore_sig()

Quoted 6 lines · Borislav Petkov

arch/x86/kernel/fpu/xstate.c int using_compacted_format(void) · line 518

Quoted 3 lines

/* Validate an xstate header supplied by userspace (ptrace or sigreturn) */

arch/x86/kernel/fpu/xstate.c +1 -1
-int validate_user_xstate_header(const struct xstate_header *hdr)
+static int validate_user_xstate_header(const struct xstate_header *hdr)

Can't do that yet - that one is still called from regset.c:

Quoted 7 lines

arch/x86/kernel/fpu/regset.c: In function ‘xstateregs_set’:
arch/x86/kernel/fpu/regset.c:135:10: error: implicit declaration of function ‘validate_user_xstate_header’ [-Werror=implicit-function-declaration]

  135 |    ret = validate_user_xstate_header(&xsave->header);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: some warnings being treated as errors

Maybe after the 5th patch which kills that usage too.

Gah, yes. I had the patches ordered differently and then failed to do a full step by step recompile after reshuffling them. Fixed localy.

Thanks,

tglx

#10 lore raw

PATCH v2 2/8 x86/fpu: Prevent state corruption in __fpu__restore_sig()

In reply to Thomas Gleixner

From: Thomas Gleixner <[email protected]>

The non-compacted slowpath uses __copy_from_user() and copies the entire user buffer into the kernel buffer, verbatim. This means that the kernel buffer may now contain entirely invalid state on which XRSTOR will #GP. validate_user_xstate_header() can detect some of that corruption, but that leaves the onus on callers to clear the buffer.

Prior to XSAVES support it was possible just to reinitialize the buffer, completely, but with supervisor states that is not longer possible as the buffer clearing code split got it backwards. Fixing that is possible, but not corrupting the state in the first place is more robust.

Avoid corruption of the kernel XSAVE buffer by using copy_user_to_xstate() which validates the XSAVE header contents before copying the actual states to the kernel. copy_user_to_xstate() was previously only called for compacted-format kernel buffers, but it works for both compacted and non-compacted forms.

Using it for the non-compacted form is slower because of multiple __copy_from_user() operations, but that cost is less important than robust code in an already slow path.

[ Changelog polished by Dave Hansen ]

Fixes b860eb8dce59 ("x86/fpu/xstate: Define new functions for clearing fpregs and xstates")
Reported-by [email protected]

---
V2: Removed the make validate_user_xstate_header() static hunks (Borislav)

---
 arch/x86/kernel/fpu/signal.c |    9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
arch/x86/kernel/fpu/signal.c +1 -8
@@ -405,14 +405,7 @@ static int __fpu__restore_sig(void __use
 	if (use_xsave() && !fx_only) {
 		u64 init_bv = xfeatures_mask_user() & ~user_xfeatures;
 
-		if (using_compacted_format()) {
-			ret = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
-		} else {
-			ret = __copy_from_user(&fpu->state.xsave, buf_fx, state_size);
-
-			if (!ret && state_size > offsetof(struct xregs_state, header))
-				ret = validate_user_xstate_header(&fpu->state.xsave.header);
-		}
+		ret = copy_user_to_xstate(&fpu->state.xsave, buf_fx);
 		if (ret)
 			goto err_out;
 
#11 lore raw

PATCH 3/8 x86/fpu: Invalidate FPU state after a failed XRSTOR from a user buffer

In reply to Thomas Gleixner

From: Andy Lutomirski <[email protected]>

If XRSTOR fails due to sufficiently complicated paging errors (e.g. concurrent TLB invalidation), it may fault with #PF but still modify portions of the user extended state.

If this happens in __fpu_restore_sig() with a victim task's FPU registers loaded and the task is preempted by the victim task, the victim task resumes with partially corrupt extended state.

Invalidate the FPU registers when XRSTOR fails to prevent this scenario.

Fixes 1d731e731c4c ("x86/fpu: Add a fastpath to __fpu__restore_sig()")
---
 arch/x86/kernel/fpu/signal.c |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/x86/kernel/fpu/signal.c +21 -0
@@ -369,6 +369,27 @@ static int __fpu__restore_sig(void __use
 			fpregs_unlock();
 			return 0;
 		}
+
+		if (test_thread_flag(TIF_NEED_FPU_LOAD)) {
+			/*
+			 * The FPU registers do not belong to current, and
+			 * we just did an FPU restore operation, restricted
+			 * to the user portion of the register file, and
+			 * failed.  In the event that the ucode was
+			 * unfriendly and modified the registers at all, we
+			 * need to make sure that we aren't corrupting an
+			 * innocent non-current task's registers.
+			 */
+			__cpu_invalidate_fpregs_state();
+		} else {
+			/*
+			 * As above, we may have just clobbered current's
+			 * user FPU state.  We will either successfully
+			 * load it or clear it below, so no action is
+			 * required here.
+			 */
+		}
+
 		fpregs_unlock();
 	} else {
 		/*
maintainer
#12 lore raw

Re: [patch 3/8] x86/fpu: Invalidate FPU state after a failed XRSTOR from a user buffer

Quoted 4 lines · Thomas Gleixner

From: Andy Lutomirski <[email protected]>

If XRSTOR fails due to sufficiently complicated paging errors (e.g. concurrent TLB invalidation),

I can't connect "concurrent TLB invalidation" to "sufficiently complicated paging errors". Can you elaborate pls?

Quoted 2 lines

it may fault with #PF but still modify
portions of the user extended state.

Yikes, leaky leaky insn.

Quoted 2 lines

If this happens in __fpu_restore_sig() with a victim task's FPU registers loaded and the task is preempted by the victim task,

This is probably meaning another task but the only task mentioned here is a "victim task"?

Quoted 16 lines

the victim task
resumes with partially corrupt extended state.

Invalidate the FPU registers when XRSTOR fails to prevent this scenario.

Fixes 1d731e731c4c ("x86/fpu: Add a fastpath to __fpu__restore_sig()")
---
 arch/x86/kernel/fpu/signal.c |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

arch/x86/kernel/fpu/signal.c static int __fpu__restore_sig(void __use · line 376

Quoted 8 lines
arch/x86/kernel/fpu/signal.c +5 -0
@@ -369,6 +369,27 @@ static int __fpu__restore_sig(void __use
 			fpregs_unlock();
 			return 0;
 		}
+
+		if (test_thread_flag(TIF_NEED_FPU_LOAD)) {
+			/*
+			 * The FPU registers do not belong to current, and
+			 * we just did an FPU restore operation, restricted

Please get rid of the "we"-personal pronouns formulations.

arch/x86/kernel/fpu/signal.c static int __fpu__restore_sig(void __use · line 377

Quoted 1 line
arch/x86/kernel/fpu/signal.c +1 -0
+			 * to the user portion of the register file, and

"register file"? That sounds like comment which belongs in microcode but not in software. :-)

Quoted 4 lines
arch/x86/kernel/fpu/signal.c +4 -0
+			 * failed.  In the event that the ucode was
+			 * unfriendly and modified the registers at all, we
+			 * need to make sure that we aren't corrupting an
+			 * innocent non-current task's registers.

arch/x86/kernel/fpu/signal.c static int __fpu__restore_sig(void __use · line 391

Quoted 10 lines
arch/x86/kernel/fpu/signal.c +10 -0
+			 */
+			__cpu_invalidate_fpregs_state();
+		} else {
+			/*
+			 * As above, we may have just clobbered current's
+			 * user FPU state.  We will either successfully
+			 * load it or clear it below, so no action is
+			 * required here.
+			 */
+		}

I'm wondering if that comment can simply be above the TIF_NEED_FPU_LOAD testing, standalone, instead of having it in an empty else? And then get rid of that else.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#13 lore raw

Re: [patch 3/8] x86/fpu: Invalidate FPU state after a failed XRSTOR from a user buffer

Quoted 8 lines · Borislav Petkov
Quoted 4 lines · Thomas Gleixner

From: Andy Lutomirski <[email protected]>

If XRSTOR fails due to sufficiently complicated paging errors (e.g. concurrent TLB invalidation),

I can't connect "concurrent TLB invalidation" to "sufficiently complicated paging errors". Can you elaborate pls?

Think "complex microarchitectural conditions".

How about:

As far as I can tell, both Intel and AMD consider it to be architecturally valid for XRSTOR to fail with #PF but nonetheless change user state. The actual conditions under which this might occur are unclear [1], but it seems plausible that this might be triggered if one sibling thread unmaps a page and invalidates the shared TLB while another sibling thread is executing XRSTOR on the page in question.

__fpu__restore_sig() can execute XRSTOR while the hardware registers are preserved on behalf of a different victim task (using the fpu_fpregs_owner_ctx mechanism), and, in theory, XRSTOR could fail but modify the registers. If this happens, then there is a window in which __fpu__restore_sig() could schedule out and the victim task could schedule back in without reloading its own FPU registers. This would result in part of the FPU state that __fpu__restore_sig() was attempting to load leaking into the victim task's user-visible state.

Invalidate preserved FPU registers on XRSTOR failure to prevent this situation from corrupting any state.

[1] Frequent readers of the errata lists might imagine "complex microarchitectural conditions".

Quoted 10 lines
Quoted 10 lines
arch/x86/kernel/fpu/signal.c +10 -0
+			 * failed.  In the event that the ucode was
+			 * unfriendly and modified the registers at all, we
+			 * need to make sure that we aren't corrupting an
+			 * innocent non-current task's registers.
+			 */
+			__cpu_invalidate_fpregs_state();
+		} else {
+			/*
+			 * As above, we may have just clobbered current's
+			 * user FPU state.  We will either successfully
Quoted 8 lines

arch/x86/kernel/fpu/signal.c static int __fpu__restore_sig(void __use · line 391

Quoted 4 lines
arch/x86/kernel/fpu/signal.c +4 -0
+			 * load it or clear it below, so no action is
+			 * required here.
+			 */
+		}

I'm wondering if that comment can simply be above the TIF_NEED_FPU_LOAD testing, standalone, instead of having it in an empty else? And then get rid of that else.

I'm fine either way.

maintainer
#14 lore raw

Re: [patch 3/8] x86/fpu: Invalidate FPU state after a failed XRSTOR from a user buffer

Quoted 1 line · Andy Lutomirski

Think "complex microarchitectural conditions".

Ah, the magic phrase.

Quoted 18 lines

How about:

As far as I can tell, both Intel and AMD consider it to be architecturally valid for XRSTOR to fail with #PF but nonetheless change user state. The actual conditions under which this might occur are unclear [1], but it seems plausible that this might be triggered if one sibling thread unmaps a page and invalidates the shared TLB while another sibling thread is executing XRSTOR on the page in question.

__fpu__restore_sig() can execute XRSTOR while the hardware registers are preserved on behalf of a different victim task (using the fpu_fpregs_owner_ctx mechanism), and, in theory, XRSTOR could fail but modify the registers. If this happens, then there is a window in which __fpu__restore_sig() could schedule out and the victim task could schedule back in without reloading its own FPU registers. This would result in part of the FPU state that __fpu__restore_sig() was attempting to load leaking into the victim task's user-visible state.

Quoted 5 lines

Invalidate preserved FPU registers on XRSTOR failure to prevent this situation from corrupting any state.

[1] Frequent readers of the errata lists might imagine "complex microarchitectural conditions".

Yap, very nice, thanks!

Quoted 5 lines
Quoted 3 lines

I'm wondering if that comment can simply be above the TIF_NEED_FPU_LOAD testing, standalone, instead of having it in an empty else? And then get rid of that else.

I'm fine either way.

Ok, then let's aim for common, no-surprise-there patterns as we're in a mine field here anyway.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#15 lore raw

PATCH 4/8 x86/fpu: Limit xstate copy size in xstateregs_set()

In reply to Thomas Gleixner

If the count argument is larger than the xstate size, this will happily copy beyond the end of xstate.

Fixes 91c3dba7dbc1 ("x86/fpu/xstate: Fix PTRACE frames for XSAVES")
---
 arch/x86/kernel/fpu/regset.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/fpu/regset.c +1 -1
@@ -117,7 +117,7 @@ int xstateregs_set(struct task_struct *t
 	/*
 	 * A whole standard-format XSAVE buffer is needed:
 	 */
-	if ((pos != 0) || (count < fpu_user_xstate_size))
+	if (pos != 0 || count != fpu_user_xstate_size)
 		return -EFAULT;
 
 	xsave = &fpu->state.xsave;
maintainer
#16 lore raw

Re: [patch 4/8] x86/fpu: Limit xstate copy size in xstateregs_set()

Quoted 13 lines · Thomas Gleixner

If the count argument is larger than the xstate size, this will happily copy beyond the end of xstate.

Fixes 91c3dba7dbc1 ("x86/fpu/xstate: Fix PTRACE frames for XSAVES")
---
 arch/x86/kernel/fpu/regset.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

arch/x86/kernel/fpu/regset.c int xstateregs_set(struct task_struct *t · line 123

Quoted 8 lines
arch/x86/kernel/fpu/regset.c +1 -1
@@ -117,7 +117,7 @@ int xstateregs_set(struct task_struct *t
 	/*
 	 * A whole standard-format XSAVE buffer is needed:
 	 */
-	if ((pos != 0) || (count < fpu_user_xstate_size))
+	if (pos != 0 || count != fpu_user_xstate_size)
 		return -EFAULT;
 
 	xsave = &fpu->state.xsave;

Looking at this one, I guess it and all the CC:stable ones should be at the beginning of the set so that they go to Linus now...

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#17 lore raw

PATCH 5/8 x86/fpu: Sanitize xstateregs_set()

In reply to Thomas Gleixner

xstateregs_set() operates on a stopped task and tries to copy the provided buffer into the tasks fpu.state.xsave buffer.

Any error while copying or invalid state detected after copying results in wiping the target tasks FPU state completely including supervisor states.

That's just wrong. The caller supplied invalid data or has a problem with unmapped memory, so there is absolutely no justification to wreckage the target.

Fix this with the following modifications:

1) If data has to be copied from userspace, allocate a buffer and copy from user first.

2) Use copy_kernel_to_xstate() unconditionally so that header checking works correctly.

3) Return on error without wreckaging the target state.

This prevents corrupting supervisor states and lets the caller deal with the problem it caused in the first place.

---
 arch/x86/kernel/fpu/regset.c |   41 +++++++++++++++++------------------------
 arch/x86/kernel/fpu/xstate.c |   10 ++++++----
 2 files changed, 23 insertions(+), 28 deletions(-)
arch/x86/kernel/fpu/regset.c +17 -24
@@ -6,8 +6,12 @@
 #include <asm/fpu/signal.h>
 #include <asm/fpu/regset.h>
 #include <asm/fpu/xstate.h>
+
+#include <linux/vmalloc.h>
+
 #include <linux/sched/task_stack.h>
 
+
 /*
  * The xstateregs_active() routine is the same as the regset_fpregs_active() routine,
  * as the "regset->n" for the xstate regset will be updated based on the feature
@@ -107,8 +111,8 @@ int xstateregs_set(struct task_struct *t
 		  unsigned int pos, unsigned int count,
 		  const void *kbuf, const void __user *ubuf)
 {
+	struct xregs_state *xsave, *xbuf = NULL;
 	struct fpu *fpu = &target->thread.fpu;
-	struct xregs_state *xsave;
 	int ret;
 
 	if (!boot_cpu_has(X86_FEATURE_XSAVE))
@@ -120,32 +124,21 @@ int xstateregs_set(struct task_struct *t
 	if (pos != 0 || count != fpu_user_xstate_size)
 		return -EFAULT;
 
-	xsave = &fpu->state.xsave;
-
-	fpu__prepare_write(fpu);
-
-	if (using_compacted_format()) {
-		if (kbuf)
-			ret = copy_kernel_to_xstate(xsave, kbuf);
-		else
-			ret = copy_user_to_xstate(xsave, ubuf);
-	} else {
-		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
-		if (!ret)
-			ret = validate_user_xstate_header(&xsave->header);
+	if (!kbuf) {
+		xbuf = vmalloc(count);
+		if (!xbuf)
+			return -ENOMEM;
+		ret = user_regset_copyin(&pos, &count, NULL, &ubuf, xbuf, 0, -1);
+		if (ret)
+			goto out;
 	}
 
-	/*
-	 * mxcsr reserved bits must be masked to zero for security reasons.
-	 */
-	xsave->i387.mxcsr &= mxcsr_feature_mask;
-
-	/*
-	 * In case of failure, mark all states as init:
-	 */
-	if (ret)
-		fpstate_init(&fpu->state);
+	xsave = &fpu->state.xsave;
+	fpu__prepare_write(fpu);
+	ret = copy_kernel_to_xstate(xsave, kbuf ? kbuf : xbuf);
 
+out:
+	vfree(xbuf);
 	return ret;
 }
 
arch/x86/kernel/fpu/xstate.c +6 -4
@@ -1172,14 +1172,16 @@ int copy_kernel_to_xstate(struct xregs_s
 	 */
 	xsave->header.xfeatures |= hdr.xfeatures;
 
+	/* mxcsr reserved bits must be masked to zero for security reasons. */
+	xsave->i387.mxcsr &= mxcsr_feature_mask;
+
 	return 0;
 }
 
 /*
- * Convert from a ptrace or sigreturn standard-format user-space buffer to
- * kernel XSAVES format and copy to the target thread. This is called from
- * xstateregs_set(), as well as potentially from the sigreturn() and
- * rt_sigreturn() system calls.
+ * Convert from a sigreturn standard-format user-space buffer to kernel
+ * XSAVES format and copy to the target thread. This is called from the
+ * sigreturn() and rt_sigreturn() system calls.
  */
 int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf)
 {
#18 lore raw

PATCH v2 5/8 x86/fpu: Sanitize xstateregs_set()

xstateregs_set() operates on a stopped task and tries to copy the provided buffer into the tasks fpu.state.xsave buffer.

Any error while copying or invalid state detected after copying results in wiping the target tasks FPU state completely including supervisor states.

That's just wrong. The caller supplied invalid data or has a problem with unmapped memory, so there is absolutely no justification to wreckage the target.

Fix this with the following modifications:

1) If data has to be copied from userspace, allocate a buffer and copy from user first.

2) Use copy_kernel_to_xstate() unconditionally so that header checking works correctly.

3) Return on error without wreckaging the target state.

This prevents corrupting supervisor states and lets the caller deal with the problem it caused in the first place.

Make validate_user_xstate_header() static as this was the last user outside of xstate.c

---
V2: Move the validate_user_xstate_header() static here

---
 arch/x86/include/asm/fpu/xstate.h |    4 ---
 arch/x86/kernel/fpu/regset.c      |   41 +++++++++++++++-----------------------
 arch/x86/kernel/fpu/xstate.c      |   12 ++++++-----
 3 files changed, 24 insertions(+), 33 deletions(-)
arch/x86/include/asm/fpu/xstate.h +0 -4
@@ -112,8 +112,4 @@ void copy_supervisor_to_kernel(struct xr
 void copy_dynamic_supervisor_to_kernel(struct xregs_state *xstate, u64 mask);
 void copy_kernel_to_dynamic_supervisor(struct xregs_state *xstate, u64 mask);
 
-
-/* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
-int validate_user_xstate_header(const struct xstate_header *hdr);
-
 #endif
arch/x86/kernel/fpu/regset.c +17 -24

65 lines not on this page · raw message

arch/x86/kernel/fpu/xstate.c +7 -5
@@ -515,7 +515,7 @@ int using_compacted_format(void)
 }
 
 /* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
-int validate_user_xstate_header(const struct xstate_header *hdr)
+static int validate_user_xstate_header(const struct xstate_header *hdr)
 {
 	/* No unknown or supervisor features may be set */
 	if (hdr->xfeatures & ~xfeatures_mask_user())
@@ -1172,14 +1172,16 @@ int copy_kernel_to_xstate(struct xregs_s
 	 */
 	xsave->header.xfeatures |= hdr.xfeatures;
 
+	/* mxcsr reserved bits must be masked to zero for security reasons. */
+	xsave->i387.mxcsr &= mxcsr_feature_mask;
+
 	return 0;
 }
 
 /*
- * Convert from a ptrace or sigreturn standard-format user-space buffer to
- * kernel XSAVES format and copy to the target thread. This is called from
- * xstateregs_set(), as well as potentially from the sigreturn() and
- * rt_sigreturn() system calls.
+ * Convert from a sigreturn standard-format user-space buffer to kernel
+ * XSAVES format and copy to the target thread. This is called from the
+ * sigreturn() and rt_sigreturn() system calls.
  */
 int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf)
 {
maintainer
#19 lore raw

Re: [patch V2 5/8] x86/fpu: Sanitize xstateregs_set()

Quoted 2 lines · Thomas Gleixner

xstateregs_set() operates on a stopped task and tries to copy the provided buffer into the tasks fpu.state.xsave buffer.

task's

Quoted 3 lines

Any error while copying or invalid state detected after copying results in wiping the target tasks FPU state completely including supervisor states.

Ditto.

Quoted 13 lines

That's just wrong. The caller supplied invalid data or has a problem with unmapped memory, so there is absolutely no justification to wreckage the target.

Fix this with the following modifications:

1) If data has to be copied from userspace, allocate a buffer and copy from user first.

2) Use copy_kernel_to_xstate() unconditionally so that header checking works correctly.

Quoted 7 lines

3) Return on error without wreckaging the target state.

This prevents corrupting supervisor states and lets the caller deal with the problem it caused in the first place.

Make validate_user_xstate_header() static as this was the last user outside of xstate.c

Niice.

Quoted 11 lines
arch/x86/kernel/fpu/regset.c +0 -7
@@ -120,32 +124,21 @@ int xstateregs_set(struct task_struct *t
 	if (pos != 0 || count != fpu_user_xstate_size)
 		return -EFAULT;
 
-	xsave = &fpu->state.xsave;
-
-	fpu__prepare_write(fpu);
-
-	if (using_compacted_format()) {
-		if (kbuf)
-			ret = copy_kernel_to_xstate(xsave, kbuf);

arch/x86/kernel/fpu/regset.c int xstateregs_set(struct task_struct *t · line 130

Quoted 10 lines
arch/x86/kernel/fpu/regset.c +4 -6
-		else
-			ret = copy_user_to_xstate(xsave, ubuf);
-	} else {
-		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
-		if (!ret)
-			ret = validate_user_xstate_header(&xsave->header);
+	if (!kbuf) {
+		xbuf = vmalloc(count);
+		if (!xbuf)
+			return -ENOMEM;

<---- newline here.

Btw, AFAICT and if I'm staring properly at the bowels of ptrace, that ENOMEM would percolate up the chain when ptrace(2) is called.

The ptrace(2) manpage doesn't mention ENOMEM and I guess this is a small ABI change. I say "small" because it is still a negative error and properly written luserspace code should check it anyway...

Or am I being overly cautious here?

( Frankly, can you blame me, with that madness called xstate?! )

Quoted 8 lines
arch/x86/kernel/fpu/regset.c +3 -3
+		ret = user_regset_copyin(&pos, &count, NULL, &ubuf, xbuf, 0, -1);
+		if (ret)
+			goto out;
 	}
 
-	/*
-	 * mxcsr reserved bits must be masked to zero for security reasons.
-	 */

arch/x86/kernel/fpu/regset.c int xstateregs_set(struct task_struct *t · line 138

Quoted 10 lines
arch/x86/kernel/fpu/regset.c +3 -7
-	xsave->i387.mxcsr &= mxcsr_feature_mask;
-
-	/*
-	 * In case of failure, mark all states as init:
-	 */
-	if (ret)
-		fpstate_init(&fpu->state);
+	xsave = &fpu->state.xsave;
+	fpu__prepare_write(fpu);
+	ret = copy_kernel_to_xstate(xsave, kbuf ? kbuf : xbuf);

Uff, three buffers in a single function. How about we call "xbuf" "tmp_buf" so that it is even more visible what it is and it differs more from the other <letter>buf thingies?

arch/x86/kernel/fpu/xstate.c int copy_kernel_to_xstate(struct xregs_s · line 1183

Quoted 7 lines
arch/x86/kernel/fpu/xstate.c +2 -4
 /*
- * Convert from a ptrace or sigreturn standard-format user-space buffer to
- * kernel XSAVES format and copy to the target thread. This is called from
- * xstateregs_set(), as well as potentially from the sigreturn() and
- * rt_sigreturn() system calls.
+ * Convert from a sigreturn standard-format user-space buffer to kernel
+ * XSAVES format and copy to the target thread. This is called from the

Yeah, I guess we call that the "compacted" format. Just getting our terminology simplified because this is one helluva mess, as we all know.

Quoted 4 lines

+ * sigreturn() and rt_sigreturn() system calls.
*/
int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf) {

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#20 lore raw

Re: [patch 5/8] x86/fpu: Sanitize xstateregs_set()

In reply to Thomas Gleixner
Quoted 4 lines · Thomas Gleixner

That's just wrong. The caller supplied invalid data or has a problem with unmapped memory, so there is absolutely no justification to wreckage the target.

"Wreckage" is a delightful word, but it likes to be a noun. One can wreak the target, wreak havoc upon the target, annihilate the target, or turn the target's state into system-destroying wreckage :)

--Andy

#21 lore raw

PATCH 6/8 x86/fpu: Add address range checks to copy_user_to_xstate()

In reply to Thomas Gleixner

From: Andy Lutomirski <[email protected]>

copy_user_to_xstate() uses __copy_from_user(), which provides a negligible speedup. Fortunately, both call sites are at least almost correct. __fpu__restore_sig() checks access_ok() with a length of xstate_sigframe_size() and ptrace regset access uses fpu_user_xstate_size. These should be valid upper bounds on the length, so, at worst, this would cause spurious failures and not accesses to kernel memory.

Nonetheless, this is far more fragile than necessary and none of these callers are in a hotpath.

Use copy_from_user() instead.

---
 arch/x86/kernel/fpu/xstate.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/fpu/xstate.c +3 -3
@@ -1192,7 +1192,7 @@ int copy_user_to_xstate(struct xregs_sta
 	offset = offsetof(struct xregs_state, header);
 	size = sizeof(hdr);
 
-	if (__copy_from_user(&hdr, ubuf + offset, size))
+	if (copy_from_user(&hdr, ubuf + offset, size))
 		return -EFAULT;
 
 	if (validate_user_xstate_header(&hdr))
@@ -1207,7 +1207,7 @@ int copy_user_to_xstate(struct xregs_sta
 			offset = xstate_offsets[i];
 			size = xstate_sizes[i];
 
-			if (__copy_from_user(dst, ubuf + offset, size))
+			if (copy_from_user(dst, ubuf + offset, size))
 				return -EFAULT;
 		}
 	}
@@ -1215,7 +1215,7 @@ int copy_user_to_xstate(struct xregs_sta
 	if (xfeatures_mxcsr_quirk(hdr.xfeatures)) {
 		offset = offsetof(struct fxregs_state, mxcsr);
 		size = MXCSR_AND_FLAGS_SIZE;
-		if (__copy_from_user(&xsave->i387.mxcsr, ubuf + offset, size))
+		if (copy_from_user(&xsave->i387.mxcsr, ubuf + offset, size))
 			return -EFAULT;
 	}
 
#22 lore raw

PATCH 7/8 x86/fpu: Clean up the fpu__clear() variants

In reply to Thomas Gleixner

From: Andy Lutomirski <[email protected]>

fpu__clear() currently resets both register state and kernel XSAVE buffer state. It has two modes: one for all state (supervisor and user) and another for user state only. fpu__clear_all() uses the "all state" (user_only=0) mode, while a number of signal paths use the user_only=1 mode.

Make fpu__clear() work only for user state (user_only=1) and remove the "all state" (user_only=0) code. Rename it to match so it can be used by the signal paths.

Replace the "all state" (user_only=0) fpu__clear() functionality. Use the TIF_NEED_FPU_LOAD functionality instead of making any actual hardware registers changes in this path.

[ Changelog polished by Dave Hansen ]

---
 arch/x86/kernel/fpu/core.c |   62 ++++++++++++++++++++++++++++++---------------
 1 file changed, 42 insertions(+), 20 deletions(-)
arch/x86/kernel/fpu/core.c +42 -20
@@ -354,45 +354,67 @@ static inline void copy_init_fpstate_to_
 }
 
 /*
- * Clear the FPU state back to init state.
- *
- * Called by sys_execve(), by the signal handler code and by various
- * error paths.
+ * Reset current's user FPU states to the init states.  current's
+ * supervisor states, if any, are not modified by this function.  The
+ * caller guarantees that the XSTATE header in memory is intact.
  */
-static void fpu__clear(struct fpu *fpu, bool user_only)
+void fpu__clear_user_states(struct fpu *fpu)
 {
 	WARN_ON_FPU(fpu != &current->thread.fpu);
 
 	if (!static_cpu_has(X86_FEATURE_FPU)) {
-		fpu__drop(fpu);
-		fpu__initialize(fpu);
+		fpu__clear_all(fpu);
 		return;
 	}
 
 	fpregs_lock();
 
-	if (user_only) {
-		if (!fpregs_state_valid(fpu, smp_processor_id()) &&
-		    xfeatures_mask_supervisor())
-			copy_kernel_to_xregs(&fpu->state.xsave,
-					     xfeatures_mask_supervisor());
-		copy_init_fpstate_to_fpregs(xfeatures_mask_user());
-	} else {
-		copy_init_fpstate_to_fpregs(xfeatures_mask_all);
+	/*
+	 * Ensure that current's supervisor states are loaded into
+	 * their corresponding registers.
+	 */
+	if (xfeatures_mask_supervisor() &&
+	    !fpregs_state_valid(fpu, smp_processor_id())) {
+		copy_kernel_to_xregs(&fpu->state.xsave,
+				     xfeatures_mask_supervisor());
 	}
 
+	/* Reset user states in registers. */
+	copy_init_fpstate_to_fpregs(xfeatures_mask_user());
+
+	/*
+	 * Now all FPU registers have their desired values.  Inform the
+	 * FPU state machine that current's FPU registers are in the
+	 * hardware registers.
+	 */
 	fpregs_mark_activate();
+
 	fpregs_unlock();
 }
 
-void fpu__clear_user_states(struct fpu *fpu)
-{
-	fpu__clear(fpu, true);
-}
 
+/*
+ * Reset current's FPU registers (user and supervisor) to their INIT values.
+ * This is used by execve(); out of an abundance of caution, it completely
+ * wipes and resets the XSTATE buffer in memory.
+ *
+ * Note that XSAVE (unlike XSAVES) expects the XSTATE buffer in memory to
+ * be valid, so there are certain forms of corruption of the XSTATE buffer
+ * in memory that would survive initializing the FPU registers and XSAVEing
+ * them to memory.
+ */
 void fpu__clear_all(struct fpu *fpu)
 {
-	fpu__clear(fpu, false);
+	fpregs_lock();
+	fpu__drop(fpu);
+	/*
+	 * This does not change the actual hardware registers; when
+	 * fpu__clear_all() returns, TIF_NEED_FPU_LOAD will be set, and a
+	 * subsequent exit to user mode will reload the hardware registers
+	 * from memory.
+	 */
+	fpu__initialize(fpu);
+	fpregs_unlock();
 }
 
 /*
#23 lore raw

PATCH 8/8 x86/fpu: Deduplicate copy_xxx_to_xstate()

In reply to Thomas Gleixner

copy_user_to_xstate() and copy_kernel_to_xstate() are almost identical except for the copy function.

Unify them.

---
 arch/x86/kernel/fpu/xstate.c |   83 +++++++++++++++----------------------------
 1 file changed, 29 insertions(+), 54 deletions(-)
arch/x86/kernel/fpu/xstate.c +29 -54
@@ -1124,20 +1124,30 @@ void copy_xstate_to_kernel(struct membuf
 	fill_gap(&to, &last, size);
 }
 
-/*
- * Convert from a ptrace standard-format kernel buffer to kernel XSAVES format
- * and copy to the target thread. This is called from xstateregs_set().
- */
-int copy_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)
+static int copy_from_buffer(void *dst, unsigned int offset, unsigned int size,
+			    const void *kbuf, const void __user *ubuf)
+{
+	if (kbuf) {
+		memcpy(dst, kbuf + offset, size);
+	} else {
+		if (copy_from_user(dst, ubuf + offset, size))
+			return -EFAULT;
+	}
+	return 0;
+}
+
+static int copy_to_xstate(struct xregs_state *xsave, const void *kbuf,
+			  const void __user *ubuf)
 {
 	unsigned int offset, size;
-	int i;
 	struct xstate_header hdr;
+	int i;
 
 	offset = offsetof(struct xregs_state, header);
 	size = sizeof(hdr);
 
-	memcpy(&hdr, kbuf + offset, size);
+	if (copy_from_buffer(&hdr, offset, size, kbuf, ubuf))
+		return -EFAULT;
 
 	if (validate_user_xstate_header(&hdr))
 		return -EINVAL;
@@ -1151,7 +1161,8 @@ int copy_kernel_to_xstate(struct xregs_s
 			offset = xstate_offsets[i];
 			size = xstate_sizes[i];
 
-			memcpy(dst, kbuf + offset, size);
+			if (copy_from_buffer(dst, offset, size, kbuf, ubuf))
+				return -EFAULT;
 		}
 	}
 
@@ -1179,58 +1190,22 @@ int copy_kernel_to_xstate(struct xregs_s
 }
 
 /*
+ * Convert from a ptrace standard-format kernel buffer to kernel XSAVES format
+ * and copy to the target thread. This is called from xstateregs_set().
+ */
+int copy_kernel_to_xstate(struct xregs_state *xsave, const void *kbuf)
+{
+	return copy_to_xstate(xsave, kbuf, NULL);
+}
+
+/*
  * Convert from a sigreturn standard-format user-space buffer to kernel
  * XSAVES format and copy to the target thread. This is called from the
  * sigreturn() and rt_sigreturn() system calls.
  */
 int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf)
 {
-	unsigned int offset, size;
-	int i;
-	struct xstate_header hdr;
-
-	offset = offsetof(struct xregs_state, header);
-	size = sizeof(hdr);
-
-	if (copy_from_user(&hdr, ubuf + offset, size))
-		return -EFAULT;
-
-	if (validate_user_xstate_header(&hdr))
-		return -EINVAL;
-
-	for (i = 0; i < XFEATURE_MAX; i++) {
-		u64 mask = ((u64)1 << i);
-
-		if (hdr.xfeatures & mask) {
-			void *dst = __raw_xsave_addr(xsave, i);
-
-			offset = xstate_offsets[i];
-			size = xstate_sizes[i];
-
-			if (copy_from_user(dst, ubuf + offset, size))
-				return -EFAULT;
-		}
-	}
-
-	if (xfeatures_mxcsr_quirk(hdr.xfeatures)) {
-		offset = offsetof(struct fxregs_state, mxcsr);
-		size = MXCSR_AND_FLAGS_SIZE;
-		if (copy_from_user(&xsave->i387.mxcsr, ubuf + offset, size))
-			return -EFAULT;
-	}
-
-	/*
-	 * The state that came in from userspace was user-state only.
-	 * Mask all the user states out of 'xfeatures':
-	 */
-	xsave->header.xfeatures &= XFEATURE_MASK_SUPERVISOR_ALL;
-
-	/*
-	 * Add back in the features that came in from userspace:
-	 */
-	xsave->header.xfeatures |= hdr.xfeatures;
-
-	return 0;
+	return copy_to_xstate(xsave, NULL, ubuf);
 }
 
 /*
Acked-by on v1 8/8
#24 lore raw

Re: [patch 8/8] x86/fpu: Deduplicate copy_xxx_to_xstate()

Quoted 4 lines · Thomas Gleixner

copy_user_to_xstate() and copy_kernel_to_xstate() are almost identical except for the copy function.

Unify them.

I keep looking at call sites of these functions and being vaguely confused. Maybe copy_to_userabi_xstate() would make the purpose of these functions clearer?

Otherwise, and even if you don't like my suggestion:

#25 lore raw
In reply to Thomas Gleixner
Quoted 20 lines · Thomas Gleixner

syszbot reported a warnon for XRSTOR raising #GP:

https://lore.kernel.org/r/[email protected]

with a syzcaller reproducer and a conclusive bisect result.

It took a while to destill a simple C reproducer out of it which allowed to pin point the root cause: The recent addition of supervisor XSTATEs broke the signal restore path for the case where the signal handler wreckaged the XSTATE on stack because it does not sanitize the XSTATE header which causes a subsequent XRSTOR to fail and #GP.

The following series addresses the problem and fixes related issues which were found while inspecting the related changes.

Thanks to Andy and Dave for working on this with me!

Thanks,

tglx

Quoted 10 lines
---
  arch/x86/include/asm/fpu/xstate.h                     |    4
  arch/x86/kernel/fpu/core.c                            |   62 ++++++---
  arch/x86/kernel/fpu/regset.c                          |   43 ++----
  arch/x86/kernel/fpu/signal.c                          |   30 +++-
  arch/x86/kernel/fpu/xstate.c                          |   95 +++++----------
  b/tools/testing/selftests/x86/corrupt_xstate_header.c |  114 ++++++++++++++++++
  tools/testing/selftests/x86/Makefile                  |    3
  7 files changed, 234 insertions(+), 117 deletions(-)

With the series applied, glibc pkey test fails sometimes. I will try to find out the cause.

The test:

https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/tst-pkey.c

The output:

error: ../sysdeps/unix/sysv/linux/tst-pkey.c:161: not true: !check_page_access (i, false)
error: ../sysdeps/unix/sysv/linux/tst-pkey.c:162: not true: !check_page_access (i, true)
error: ../sysdeps/unix/sysv/linux/tst-pkey.c:161: not true: !check_page_access (i, false)
error: ../sysdeps/unix/sysv/linux/tst-pkey.c:162: not true: !check_page_access (i, true)
error: ../sysdeps/unix/sysv/linux/tst-pkey.c:161: not true: !check_page_access (i, false)
error: ../sysdeps/unix/sysv/linux/tst-pkey.c:162: not true: !check_page_access (i, true) ../sysdeps/unix/sysv/linux/tst-pkey.c:238: numeric comparison failure
left: 0 (0x0); from: result->access_rights[i]
right: 1 (0x1); from: PKEY_DISABLE_ACCESS
../sysdeps/unix/sysv/linux/tst-pkey.c:238: numeric comparison failure
left: 0 (0x0); from: result->access_rights[i]
right: 1 (0x1); from: PKEY_DISABLE_ACCESS
../sysdeps/unix/sysv/linux/tst-pkey.c:238: numeric comparison failure
left: 0 (0x0); from: result->access_rights[i]
right: 1 (0x1); from: PKEY_DISABLE_ACCESS
../sysdeps/unix/sysv/linux/tst-pkey.c:242: numeric comparison failure
left: 0 (0x0); from: result->access_rights[i]
right: 1 (0x1); from: PKEY_DISABLE_ACCESS
../sysdeps/unix/sysv/linux/tst-pkey.c:242: numeric comparison failure
left: 0 (0x0); from: result->access_rights[i]
right: 1 (0x1); from: PKEY_DISABLE_ACCESS
../sysdeps/unix/sysv/linux/tst-pkey.c:242: numeric comparison failure
left: 0 (0x0); from: result->access_rights[i]
right: 1 (0x1); from: PKEY_DISABLE_ACCESS
error: 12 test failures

#26 lore raw
Quoted 4 lines · Yu-cheng Yu

On 6/2/2021 2:55 AM, Thomas Gleixner wrote:

With the series applied, glibc pkey test fails sometimes. I will try to find out the cause.

That fails not sometimes. It fails always due to patch 7/8. The reason is that before that patch fpu__clear_all() reinitialized the hardware which includes writing the initial PKRU value.

But fpu__initialize() does not store the initial PKRU value in the task's xstate which breaks PKRU. As that patch is not urgent we can postpone it for not.

But looking at the above, it's not clear to me why that PKRU stuff works at all (upstream), but I'll figure it out eventually. I'm quite sure that it does work by pure chance not by design.

Thanks,

tglx

#27 lore raw
Quoted 10 lines · Thomas Gleixner
Quoted 4 lines · Yu-cheng Yu

On 6/2/2021 2:55 AM, Thomas Gleixner wrote:

With the series applied, glibc pkey test fails sometimes. I will try to find out the cause.

That fails not sometimes. It fails always due to patch 7/8. The reason is that before that patch fpu__clear_all() reinitialized the hardware which includes writing the initial PKRU value.

Quoted 8 lines

But fpu__initialize() does not store the initial PKRU value in the task's xstate which breaks PKRU. As that patch is not urgent we can postpone it for not.

But looking at the above, it's not clear to me why that PKRU stuff works at all (upstream), but I'll figure it out eventually. I'm quite sure that it does work by pure chance not by design.

Thanks for the update. I will continue looking at it.

Yu-cheng

#28 lore raw
In reply to Thomas Gleixner
Quoted 3 lines · Thomas Gleixner

But looking at the above, it's not clear to me why that PKRU stuff works at all (upstream), but I'll figure it out eventually. I'm quite sure that it does work by pure chance not by design.

The upstream flush_thread() code appears correct and even intentionally so. Just how we must eagerly load PKRU on a context switch, the fpu__clear*() code eagerly "clears" PKRU. It doesn't actually zero it, of course, but reverts the register and the fpstate back to the 'init_pkru_value':

flush_thread()->fpu__clear_all()->fpu__clear(user_only=false)
copy_init_fpstate_to_fpregs()
copy_kernel_to_xregs(init_fpu) // fpstate copy_init_pkru_to_fpregs()
write_pkru(init_pkru_value_snapshot) // fpregs

Andy said you have a fix for this, but I think the new fpu__clear_all() is failing to do the eager write_pkru().

#29 lore raw
Quoted 11 lines · Dave Hansen
Quoted 3 lines · Thomas Gleixner

But looking at the above, it's not clear to me why that PKRU stuff works at all (upstream), but I'll figure it out eventually. I'm quite sure that it does work by pure chance not by design.

The upstream flush_thread() code appears correct and even intentionally so. Just how we must eagerly load PKRU on a context switch, the fpu__clear*() code eagerly "clears" PKRU. It doesn't actually zero it, of course, but reverts the register and the fpstate back to the 'init_pkru_value':

Quoted 8 lines

flush_thread()->fpu__clear_all()->fpu__clear(user_only=false)
copy_init_fpstate_to_fpregs()
copy_kernel_to_xregs(init_fpu) // fpstate copy_init_pkru_to_fpregs()
write_pkru(init_pkru_value_snapshot) // fpregs

Andy said you have a fix for this, but I think the new fpu__clear_all() is failing to do the eager write_pkru().

Yes, that's the reason and it took some time until I realized that fpu__initialize() is inconsistent vs. PKRU.

We can't use copy_init_pkru_to_fregs() either because that's not updating the xsaves area because XFEATURE_PKRU has been cleared. Yay for consistency!

I'll post a fix soonish after testing it.

Thanks,

tglx

#30 lore raw
In reply to Thomas Gleixner

The nice Intel 0day folks threw some tests at this series. It managed to trigger an oops. I can't right this moment publish the source for the test, but it looks pretty trivial. It basically creates a 0'd XSAVE buffer, sets XCOMP_BV to:

#define XSAVES_FEATURES ( \
        XFEATURE_MASK_PT | \
        XFEATURE_MASK_SHSTK_USER | \
        XFEATURE_MASK_SHSTK_KERNEL | \
        0x8000000000000000 \
        )

Then passes that buffer in to ptrace(PTRACE_SETREGSET, ...).

The oops is below. It doesn't *look* to be XSAVES-related. The oops looks like it's happening in xstateregs_set() itself as opposed to down in the code actually concerned with supervisor state.

No bug is jumping out of the code as I took a brief look at it. The xbuf versus kbuf code looks a bit wonky, but I can't find a hole in it.

I'll see if I can reproduce this locally.

Quoted 70 lines
[   38.612897] BAT XSAVE
[   38.612902]
[   38.619525] os
[   38.619528]
[   38.625885] enumeration
[   38.625890]
[   38.633098] schedule check
[   38.633105]
[   38.640308] fork check
[   38.640321]
[   38.647154] signal check
[   38.647164]
[   38.654065] thread check
[   38.654072]
[   38.661036] multi thread check
[   38.661042]
[   75.771095] BUG: kernel NULL pointer dereference, address: 0000000000000000
[   75.779596] #PF: supervisor read access in kernel mode
[   75.785980] #PF: error_code(0x0000) - not-present page
[   75.792321] PGD 0 P4D 0
[   75.795742] Oops: 0000 [#1] SMP PTI
[   75.800233] CPU: 94 PID: 2145 Comm: ptrace_sys_stat Not tainted 5.13.0-rc4-00096-g2c38e60b245f #1
[   75.810753] Hardware name: Intel Corporation BRICKLAND/BRICKLAND, BIOS BRHSXSD1.86B.0067.R02.1507221722 07/22/2015
[   75.822934] RIP: 0010:xstateregs_set+0x9f/0x140
[   75.828591] Code: e0 5d 41 5c 41 5d 41 5e 41 5f c3 41 89 cf 4d 89 ce 4c 89 ff e8 32 87 2e 00 48 89 c5 48 85 c0 0f 84 8a 00 00 00 45 85 e4 74 20 <48> 8b 34 25 00 00 00 00 48 85 f6 74 2f 4c 89 fa 48 89 c7 e8 89 78
[   75.850813] RSP: 0018:ffffc90023a47e38 EFLAGS: 00010202
[   75.857241] RAX: ffffc9000c9dd000 RBX: ffff88c107824f80 RCX: 8000000000000063
[   75.865827] RDX: 0000000000000001 RSI: ffff888100000000 RDI: ffff888100203328
[   75.874376] RBP: ffffc9000c9dd000 R08: ffff88810cdffef0 R09: 8000000000000063
[   75.882928] R10: 0000000000000ee8 R11: ffff88a08159c3f0 R12: 0000000000000340
[   75.891427] R13: ffff88c1078273c0 R14: 000055576f8960c0 R15: 0000000000000340
[   75.899961] FS:  00007f50ad027500(0000) GS:ffff88bfff980000(0000) knlGS:0000000000000000
[   75.909533] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   75.916458] CR2: 0000000000000000 CR3: 000000208df04004 CR4: 00000000001706e0
[   75.924975] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[   75.933434] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[   75.941911] Call Trace:
[   75.945118]  ptrace_request+0x4cb/0x600
[   75.949893]  arch_ptrace+0x125/0x300
[   75.954350]  ? ptrace_check_attach+0x69/0x140
[   75.959690]  __x64_sys_ptrace+0x80/0x140
[   75.964532]  do_syscall_64+0x40/0x80
[   75.969004]  entry_SYSCALL_64_after_hwframe+0x44/0xae
[   75.975120] RIP: 0033:0x7f50acf5692f
[   75.979581] Code: c7 44 24 10 18 00 00 00 48 89 44 24 18 48 8d 44 24 30 8b 70 08 48 8b 50 10 4c 0f 43 50 18 48 89 44 24 20 b8 65 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 41 48 85 c0 78 06 41 83 f8 02 76 1e 48 8b 4c
[   76.001535] RSP: 002b:00007fff18a9f6f0 EFLAGS: 00000202 ORIG_RAX: 0000000000000065
[   76.010465] RAX: ffffffffffffffda RBX: 0000000000000862 RCX: 00007f50acf5692f
[   76.018960] RDX: 0000000000000202 RSI: 0000000000000862 RDI: 0000000000004205
[   76.027433] RBP: 000055576f8960c0 R08: 0000000000004204 R09: 00007f50ad027500
[   76.035947] R10: 00007fff18a9f770 R11: 0000000000000202 R12: 0000000000000340
[   76.044428] R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
[   76.052957] Modules linked in: xfs libcrc32c sr_mod sd_mod cdrom t10_pi sg intel_rapl_msr intel_rapl_common sb_edac x86_pkg_temp_thermal intel_powerclamp coretemp kvm_intel kvm mgag200 irqbypass crct10dif_pclmul drm_kms_helper crc32_pclmul ipmi_ssif crc32c_intel ghash_clmulni_intel rapl syscopyarea sysfillrect intel_cstate ahci sysimgblt fb_sys_fops libahci mpt3sas acpi_ipmi intel_uncore raid_class drm libata ipmi_si joydev scsi_transport_sas wmi ipmi_devintf ipmi_msghandler acpi_pad ip_tables
[   76.104215] CR2: 0000000000000000
[   76.108641] ---[ end trace dd34396ee89519a4 ]---
[   76.114448] RIP: 0010:xstateregs_set+0x9f/0x140
[   76.120185] Code: e0 5d 41 5c 41 5d 41 5e 41 5f c3 41 89 cf 4d 89 ce 4c 89 ff e8 32 87 2e 00 48 89 c5 48 85 c0 0f 84 8a 00 00 00 45 85 e4 74 20 <48> 8b 34 25 00 00 00 00 48 85 f6 74 2f 4c 89 fa 48 89 c7 e8 89 78
[   76.142571] RSP: 0018:ffffc90023a47e38 EFLAGS: 00010202
[   76.149152] RAX: ffffc9000c9dd000 RBX: ffff88c107824f80 RCX: 8000000000000063
[   76.157855] RDX: 0000000000000001 RSI: ffff888100000000 RDI: ffff888100203328
[   76.166562] RBP: ffffc9000c9dd000 R08: ffff88810cdffef0 R09: 8000000000000063
[   76.175265] R10: 0000000000000ee8 R11: ffff88a08159c3f0 R12: 0000000000000340
[   76.183967] R13: ffff88c1078273c0 R14: 000055576f8960c0 R15: 0000000000000340
[   76.192662] FS:  00007f50ad027500(0000) GS:ffff88bfff980000(0000) knlGS:0000000000000000
[   76.202425] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   76.209590] CR2: 0000000000000000 CR3: 000000208df04004 CR4: 00000000001706e0
[   76.218266] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
[   76.226957] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
[   76.235597] Kernel panic - not syncing: Fatal exception
[   76.284889] Kernel Offset: disabled

ACPI MEMORY or I/O RESET_REG.

#31 lore raw
Quoted 12 lines · Dave Hansen

The nice Intel 0day folks threw some tests at this series. It managed to trigger an oops. I can't right this moment publish the source for the test, but it looks pretty trivial. It basically creates a 0'd XSAVE buffer, sets XCOMP_BV to:

#define XSAVES_FEATURES ( \
        XFEATURE_MASK_PT | \
        XFEATURE_MASK_SHSTK_USER | \
        XFEATURE_MASK_SHSTK_KERNEL | \
        0x8000000000000000 \
        )
Quoted 8 lines

Then passes that buffer in to ptrace(PTRACE_SETREGSET, ...).

The oops is below. It doesn't *look* to be XSAVES-related. The oops looks like it's happening in xstateregs_set() itself as opposed to down in the code actually concerned with supervisor state.

No bug is jumping out of the code as I took a brief look at it. The xbuf versus kbuf code looks a bit wonky, but I can't find a hole in it.

I can....

arch/x86/kernel/fpu/regset.c +1 -1
@@ -128,7 +128,7 @@ int xstateregs_set(struct task_struct *t
 		xbuf = vmalloc(count);
 		if (!xbuf)
 			return -ENOMEM;
-		ret = user_regset_copyin(&pos, &count, NULL, &ubuf, xbuf, 0, -1);
+		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xbuf, 0, -1);
 		if (ret)
 			goto out;
 	}
#32 lore raw
Quoted 9 lines · Thomas Gleixner
Quoted 2 lines · Dave Hansen

No bug is jumping out of the code as I took a brief look at it. The xbuf versus kbuf code looks a bit wonky, but I can't find a hole in it.

I can....

arch/x86/kernel/fpu/regset.c int xstateregs_set(struct task_struct *t · line 134

Quoted 8 lines
arch/x86/kernel/fpu/regset.c +1 -1
@@ -128,7 +128,7 @@ int xstateregs_set(struct task_struct *t
 		xbuf = vmalloc(count);
 		if (!xbuf)
 			return -ENOMEM;
-		ret = user_regset_copyin(&pos, &count, NULL, &ubuf, xbuf, 0, -1);
+		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xbuf, 0, -1);
 		if (ret)
 			goto out;
 	}

But this whole user_regset_copyin() is pointless here. See below.

Thanks,

tglx

---
 include/asm/fpu/xstate.h |    4 ----
 kernel/fpu/regset.c      |   40 ++++++++++++++++------------------------
 kernel/fpu/xstate.c      |   12 +++++++-----
 3 files changed, 23 insertions(+), 33 deletions(-)
arch/x86/include/asm/fpu/xstate.h +0 -4
@@ -112,8 +112,4 @@ void copy_supervisor_to_kernel(struct xr
 void copy_dynamic_supervisor_to_kernel(struct xregs_state *xstate, u64 mask);
 void copy_kernel_to_dynamic_supervisor(struct xregs_state *xstate, u64 mask);
 
-
-/* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
-int validate_user_xstate_header(const struct xstate_header *hdr);
-
 #endif
arch/x86/kernel/fpu/regset.c +16 -24
@@ -6,6 +6,9 @@
 #include <asm/fpu/signal.h>
 #include <asm/fpu/regset.h>
 #include <asm/fpu/xstate.h>
+
+#include <linux/vmalloc.h>
+
 #include <linux/sched/task_stack.h>
 
 /*
@@ -108,7 +111,7 @@ int xstateregs_set(struct task_struct *t
 		  const void *kbuf, const void __user *ubuf)
 {
 	struct fpu *fpu = &target->thread.fpu;
-	struct xregs_state *xsave;
+	struct xregs_state *xbuf = NULL;
 	int ret;
 
 	if (!boot_cpu_has(X86_FEATURE_XSAVE))
@@ -120,32 +123,21 @@ int xstateregs_set(struct task_struct *t
 	if (pos != 0 || count != fpu_user_xstate_size)
 		return -EFAULT;
 
-	xsave = &fpu->state.xsave;
-
-	fpu__prepare_write(fpu);
-
-	if (using_compacted_format()) {
-		if (kbuf)
-			ret = copy_kernel_to_xstate(xsave, kbuf);
-		else
-			ret = copy_user_to_xstate(xsave, ubuf);
-	} else {
-		ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, xsave, 0, -1);
-		if (!ret)
-			ret = validate_user_xstate_header(&xsave->header);
+	if (!kbuf) {
+		xbuf = vmalloc(count);
+		if (!xbuf)
+			return -ENOMEM;
+		if (copy_from_user(xbuf, ubuf, count)) {
+			ret = -EFAULT;
+			goto out;
+		}
 	}
 
-	/*
-	 * mxcsr reserved bits must be masked to zero for security reasons.
-	 */
-	xsave->i387.mxcsr &= mxcsr_feature_mask;
-
-	/*
-	 * In case of failure, mark all states as init:
-	 */
-	if (ret)
-		fpstate_init(&fpu->state);
+	fpu__prepare_write(fpu);
+	ret = copy_kernel_to_xstate(&fpu->state.xsave, kbuf ? kbuf : xbuf);
 
+out:
+	vfree(xbuf);
 	return ret;
 }
 
arch/x86/kernel/fpu/xstate.c +7 -5
@@ -515,7 +515,7 @@ int using_compacted_format(void)
 }
 
 /* Validate an xstate header supplied by userspace (ptrace or sigreturn) */
-int validate_user_xstate_header(const struct xstate_header *hdr)
+static int validate_user_xstate_header(const struct xstate_header *hdr)
 {
 	/* No unknown or supervisor features may be set */
 	if (hdr->xfeatures & ~xfeatures_mask_user())
@@ -1172,14 +1172,16 @@ int copy_kernel_to_xstate(struct xregs_s
 	 */
 	xsave->header.xfeatures |= hdr.xfeatures;
 
+	/* mxcsr reserved bits must be masked to zero for security reasons. */
+	xsave->i387.mxcsr &= mxcsr_feature_mask;
+
 	return 0;
 }
 
 /*
- * Convert from a ptrace or sigreturn standard-format user-space buffer to
- * kernel XSAVES format and copy to the target thread. This is called from
- * xstateregs_set(), as well as potentially from the sigreturn() and
- * rt_sigreturn() system calls.
+ * Convert from a sigreturn standard-format user-space buffer to kernel
+ * XSAVES format and copy to the target thread. This is called from the
+ * sigreturn() and rt_sigreturn() system calls.
  */
 int copy_user_to_xstate(struct xregs_state *xsave, const void __user *ubuf)
 {