Skip to contentSkip to conversation

x86/fpu: Fix PKRU state saving for coredump

PATCH In review Chang S. Bae 1 participant 2 messages Oct 15 – Nov 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/fpu: Fix PKRU state saving for coredump

Christina reported mismatched PKRU values from GDB coredump tests [1].

PKRU is stored in thread.pkru rather than in fpstate. Then the coredump path currently syncs fpstate only and hands thread.pkru to copy_xstate_to_uabi_buf(), so a task that still owns the register state can end up dumping a stale PKRU value.

Save PKRU from the register when the target is the current task.

---
Testing:
Christina verified the fix through the GDB tests that originally exposed the issue. Also a simple test that sets PKRU and triggers a coredump by dereferencing a NULL pointer can verify the saved PKRU from the corefile read.

The existing ptrace selftests cover most of this path but not completely, e.g. not checking back from a corefile. So it may worth adding coredump tests for XSTATEs in general. I'd follow up on this separately.

Code changes:
The PKRU saving could be folded into sync_fpstate() similar to restore_fpregs_from_init_fpstate(). Instead, since this is on the saving path and PKRU storage is separate, sync_pkru() makes that dinstinction more explicit along with the fix.

---
 arch/x86/kernel/fpu/regset.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/x86/kernel/fpu/regset.c +10 -0
index 0986c2200adc..213a06fd72f3 100644
@@ -49,6 +49,13 @@ static void sync_fpstate(struct fpu *fpu)
 		fpu_sync_fpstate(fpu);
 }
 
+/* Keep the same invocation context with sync_fpstate() */
+static void sync_pkru(struct task_struct *target)
+{
+	if (target == current)
+		target->thread.pkru = read_pkru();
+}
+
 /*
  * Invalidate cached FPU registers before modifying the stopped target
  * task's fpstate.
@@ -135,6 +142,9 @@ int xstateregs_get(struct task_struct *target, const struct user_regset *regset,
 
 	sync_fpstate(x86_task_fpu(target));
 
+	/* copy_xstate_to_uabi_buf() takes PKRU from thread.pkru */
+	sync_pkru(target);
+
 	copy_xstate_to_uabi_buf(to, target, XSTATE_COPY_XSAVE);
 	return 0;
 }
Signature · 1 line
2.53.0
#1 lore raw
Quoted 4 lines · Chang S. Bae

The existing ptrace selftests cover most of this path but not completely, e.g. not checking back from a corefile. So it may worth adding coredump tests for XSTATEs in general. I'd follow up on this separately.

Here is, and there the test for the fix as well:
https://lore.kernel.org/[email protected]

Thanks,
Chang