Skip to content

Commit 3f1c814

Browse files
khueygregkh
authored andcommitted
x86/fpu: Allow PKRU to be (once again) written by ptrace.
commit 4a804c4 upstream Handle PKRU in copy_uabi_to_xstate() for the benefit of APIs that write the XSTATE such as PTRACE_SETREGSET with NT_X86_XSTATE. This restores the pre-5.14 behavior of ptrace. The regression can be seen by running gdb and executing `p $pkru`, `set $pkru = 42`, and `p $pkru`. On affected kernels (5.14+) the write to the PKRU register (which gdb performs through ptrace) is ignored. Fixes: e84ba47 ("x86/fpu: Hook up PKRU into ptrace()") Signed-off-by: Kyle Huey <me@kylehuey.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Link: https://lore.kernel.org/all/20221115230932.7126-5-khuey%40kylehuey.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b29773d commit 3f1c814

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

arch/x86/kernel/fpu/xstate.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,29 @@ static int copy_from_buffer(void *dst, unsigned int offset, unsigned int size,
10911091
}
10921092

10931093

1094+
/**
1095+
* copy_uabi_to_xstate - Copy a UABI format buffer to the kernel xstate
1096+
* @fpstate: The fpstate buffer to copy to
1097+
* @kbuf: The UABI format buffer, if it comes from the kernel
1098+
* @ubuf: The UABI format buffer, if it comes from userspace
1099+
* @pkru: The location to write the PKRU value to
1100+
*
1101+
* Converts from the UABI format into the kernel internal hardware
1102+
* dependent format.
1103+
*
1104+
* This function ultimately has two different callers with distinct PKRU
1105+
* behavior.
1106+
* 1. When called from sigreturn the PKRU register will be restored from
1107+
* @fpstate via an XRSTOR. Correctly copying the UABI format buffer to
1108+
* @fpstate is sufficient to cover this case, but the caller will also
1109+
* pass a pointer to the thread_struct's pkru field in @pkru and updating
1110+
* it is harmless.
1111+
* 2. When called from ptrace the PKRU register will be restored from the
1112+
* thread_struct's pkru field. A pointer to that is passed in @pkru.
1113+
* The kernel will restore it manually, so the XRSTOR behavior that resets
1114+
* the PKRU register to the hardware init value (0) if the corresponding
1115+
* xfeatures bit is not set is emulated here.
1116+
*/
10941117
static int copy_uabi_to_xstate(struct xregs_state *xsave, const void *kbuf,
10951118
const void __user *ubuf, u32 *pkru)
10961119
{
@@ -1140,6 +1163,13 @@ static int copy_uabi_to_xstate(struct xregs_state *xsave, const void *kbuf,
11401163
}
11411164
}
11421165

1166+
if (hdr.xfeatures & XFEATURE_MASK_PKRU) {
1167+
struct pkru_state *xpkru;
1168+
1169+
xpkru = __raw_xsave_addr(xsave, XFEATURE_PKRU);
1170+
*pkru = xpkru->pkru;
1171+
}
1172+
11431173
/*
11441174
* The state that came in from userspace was user-state only.
11451175
* Mask all the user states out of 'xfeatures':

0 commit comments

Comments
 (0)