Skip to contentSkip to conversation

x86/xsaves: Fix PTRACE frames for XSAVES

PATCH Superseded Yu-cheng Yu 3 participants 9 messages Feb 22, 2016 x86

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH 8/10 x86/xsaves: Fix PTRACE frames for XSAVES

XSAVES uses compacted format and is a kernel instruction. The kernel should use standard-format, non-supervisor state data for PTRACE.

---
 arch/x86/include/asm/fpu/xstate.h |   4 +
 arch/x86/kernel/fpu/regset.c      |  56 +++++++++----
 arch/x86/kernel/fpu/xstate.c      | 167 +++++++++++++++++++++++++++++++++++++-
 3 files changed, 211 insertions(+), 16 deletions(-)
arch/x86/include/asm/fpu/xstate.h +4 -0
index b4f5d94..a5cd808 100644
@@ -50,5 +50,9 @@ extern void update_regset_xstate_info(unsigned int size, u64 xstate_mask);
 void fpu__xstate_clear_all_cpu_caps(void);
 void *get_xsave_addr(struct xregs_state *xsave, int xstate);
 const void *get_xsave_field_ptr(int xstate_field);
+int copyout_from_xsaves(unsigned int pos, unsigned int count, void *kbuf,
+			void __user *ubuf, const struct xregs_state *xsave);
+int copyin_to_xsaves(const void *kbuf, const void __user *ubuf,
+		     struct xregs_state *xsave);
 
 #endif
arch/x86/kernel/fpu/regset.c +42 -14

80 lines not on this page · raw message

arch/x86/kernel/fpu/xstate.c +165 -2

188 lines not on this page · raw message

Signature · 1 line
1.9.1
maintainer
#1 lore raw

arch/x86/kernel/fpu/xstate.c const void *get_xsave_field_ptr(int xsave_state) · line 945

Quoted 4 lines · Yu-cheng Yu
arch/x86/kernel/fpu/xstate.c +4 -0
+	if (xsave->header.xfeatures & XFEATURE_MASK_SUPERVISOR)
+		xsave->header.xfeatures = xfeatures | XFEATURE_MASK_SUPERVISOR;
+	else
+		xsave->header.xfeatures = xfeatures;

This is dangerous. It says, "if any supervisor feature bit is set, then set *ALL* of the known bits". There's no way that can work.

Don't you just want to or in the new bits that were in the passed-in 'xfeatures':

	xsave->header.xfeatures |= xfeatures;

'xfeatures' is known not to contain any supervisor bits.

#2 lore raw
Quoted 9 lines · Dave Hansen

arch/x86/kernel/fpu/xstate.c const void *get_xsave_field_ptr(int xsave_state) · line 945

Quoted 4 lines · Yu-cheng Yu
arch/x86/kernel/fpu/xstate.c +4 -0
+	if (xsave->header.xfeatures & XFEATURE_MASK_SUPERVISOR)
+		xsave->header.xfeatures = xfeatures | XFEATURE_MASK_SUPERVISOR;
+	else
+		xsave->header.xfeatures = xfeatures;

This is dangerous. It says, "if any supervisor feature bit is set, then set *ALL* of the known bits". There's no way that can work.

Quoted 7 lines

Don't you just want to or in the new bits that were in the passed-in 'xfeatures':

	xsave->header.xfeatures |= xfeatures;

'xfeatures' is known not to contain any supervisor bits.

It should have been:

	xsave->header.xfeatures = xfeatures |
		(xsave->header.xfeatures & XFEATURE_MASK_SUPERVISOR);

I'll fix it.

--Yu-cheng

maintainer
#3 lore raw
Quoted 6 lines · Yu-cheng Yu

It should have been:

	xsave->header.xfeatures = xfeatures |
		(xsave->header.xfeatures & XFEATURE_MASK_SUPERVISOR);

I'll fix it.

Can we break it out to make it more clear?

/*
* 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; /*
* add back in the features that came in from userspace */
xsave->header.xfeatures |= xfeatures

#4 lore raw
Quoted 10 lines · Dave Hansen
Quoted 6 lines · Yu-cheng Yu

It should have been:

	xsave->header.xfeatures = xfeatures |
		(xsave->header.xfeatures & XFEATURE_MASK_SUPERVISOR);

I'll fix it.

Can we break it out to make it more clear?

Quoted 9 lines

/*
* 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; /*
* add back in the features that came in from userspace */
xsave->header.xfeatures |= xfeatures

I will update it in the next version. Thanks!

#5 lore raw
In reply to Yu-cheng Yu

arch/x86/kernel/fpu/xstate.c const void *get_xsave_field_ptr(int xsave_state) · line 833

Quoted 12 lines · Yu-cheng Yu

XSAVES uses compacted format and is a kernel instruction. The kernel should use standard-format, non-supervisor state data for PTRACE.

arch/x86/kernel/fpu/xstate.c +8 -0
+/*
+ * Convert from kernel XSAVES compacted format to standard format and copy
+ * to a ptrace buffer. It supports partial copy but pos always starts from
+ * zero. This is called from xstateregs_get() and there we check the cpu
+ * has XSAVES.
+ */
+int copyout_from_xsaves(unsigned int pos, unsigned int count, void *kbuf,
+                       void __user *ubuf, const struct xregs_state *xsave)

Now that you've written this code, can it be shared with the signal handling code?

--Andy

--Andy

#6 lore raw
Quoted 12 lines · Andy Lutomirski

arch/x86/kernel/fpu/xstate.c const void *get_xsave_field_ptr(int xsave_state) · line 833

Quoted 8 lines
arch/x86/kernel/fpu/xstate.c +8 -0
+/*
+ * Convert from kernel XSAVES compacted format to standard format and copy
+ * to a ptrace buffer. It supports partial copy but pos always starts from
+ * zero. This is called from xstateregs_get() and there we check the cpu
+ * has XSAVES.
+ */
+int copyout_from_xsaves(unsigned int pos, unsigned int count, void *kbuf,
+                       void __user *ubuf, const struct xregs_state *xsave)

Now that you've written this code, can it be shared with the signal handling code?

For signal handling, we most likely save registers directly to memory. But for ptrace, the thread being debugged is not the active thread. Please let me think about it more.

--Yu-cheng

maintainer
#7 lore raw
In reply to Andy Lutomirski
Quoted 10 lines · Andy Lutomirski
Quoted 8 lines

+/*

Quoted 7 lines

+ * Convert from kernel XSAVES compacted format to standard format and copy
+ * to a ptrace buffer. It supports partial copy but pos always starts from
+ * zero. This is called from xstateregs_get() and there we check the cpu
+ * has XSAVES.
+ */

arch/x86/kernel/fpu/xstate.c +2 -0
+int copyout_from_xsaves(unsigned int pos, unsigned int count, void *kbuf,
+                       void __user *ubuf, const struct xregs_state *xsave)

Now that you've written this code, can it be shared with the signal handling code?

It could be. But the signal handler code has the advantage of already having the data in the registers since it's running on its *own* FPU state, so it can just call XSAVE(S) directly.

This ptrace code *could* do a kernel_fpu_begin(), XRSTOR the user buffer into the registers, XRSTOR the ptracee's system state in to the registers, then XSAVES the whole thing to the kernel buffer, then kernel_fpu_end().

Or, we could remove the signal handler's ability to XSAVE directly to userspace. But it already *had* that and we know it works.

#8 lore raw
Quoted 16 lines · Dave Hansen
Quoted 10 lines · Andy Lutomirski
Quoted 8 lines

+/*

Quoted 7 lines

+ * Convert from kernel XSAVES compacted format to standard format and copy
+ * to a ptrace buffer. It supports partial copy but pos always starts from
+ * zero. This is called from xstateregs_get() and there we check the cpu
+ * has XSAVES.
+ */

arch/x86/kernel/fpu/xstate.c +2 -0
+int copyout_from_xsaves(unsigned int pos, unsigned int count, void *kbuf,
+                       void __user *ubuf, const struct xregs_state *xsave)

Now that you've written this code, can it be shared with the signal handling code?

It could be. But the signal handler code has the advantage of already having the data in the registers since it's running on its *own* FPU state, so it can just call XSAVE(S) directly.

Quoted 7 lines

This ptrace code *could* do a kernel_fpu_begin(), XRSTOR the user buffer into the registers, XRSTOR the ptracee's system state in to the registers, then XSAVES the whole thing to the kernel buffer, then kernel_fpu_end().

Or, we could remove the signal handler's ability to XSAVE directly to userspace. But it already *had* that and we know it works.

Some day I kind of want to delete all this xsave/xrstor directly on
user buffers code. I've never been thrilled with the concept, and it
has messy (although AFAICT not presently buggy [1]) interactions with context switches, and it can't run with preemption disabled because it can take page faults.

In the mean time, it's fine as far as I know, but maybe it would be cleaner if it used the software copy code. Or maybe we can change it later if a good reason shows up.

[1] actually there's a minor bug in the 32-bit compat code that Borislav has a patch for.

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC