Skip to content

Commit d7e5ace

Browse files
khueyhansendc
authored andcommitted
x86/fpu: Emulate XRSTOR's behavior if the xfeatures PKRU bit is not set
The hardware XRSTOR instruction resets the PKRU register to its hardware init value (namely 0) if the PKRU bit is not set in the xfeatures mask. Emulating that here restores the pre-5.14 behavior for PTRACE_SET_REGSET with NT_X86_XSTATE, and makes sigreturn (which still uses XRSTOR) and ptrace behave identically. KVM has never used XRSTOR and never had this behavior, so KVM opts-out of this emulation by passing a NULL pkru pointer to copy_uabi_to_xstate(). 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-6-khuey%40kylehuey.com
1 parent 4a804c4 commit d7e5ace

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

arch/x86/kernel/fpu/core.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,14 @@ int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *gfpu, const void *buf,
404404
if (ustate->xsave.header.xfeatures & ~xcr0)
405405
return -EINVAL;
406406

407+
/*
408+
* Nullify @vpkru to preserve its current value if PKRU's bit isn't set
409+
* in the header. KVM's odd ABI is to leave PKRU untouched in this
410+
* case (all other components are eventually re-initialized).
411+
*/
412+
if (!(ustate->xsave.header.xfeatures & XFEATURE_MASK_PKRU))
413+
vpkru = NULL;
414+
407415
return copy_uabi_from_kernel_to_xstate(kstate, ustate, vpkru);
408416
}
409417
EXPORT_SYMBOL_GPL(fpu_copy_uabi_to_guest_fpstate);

arch/x86/kernel/fpu/xstate.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,8 +1219,14 @@ static int copy_from_buffer(void *dst, unsigned int offset, unsigned int size,
12191219
* it is harmless.
12201220
* 2. When called from ptrace the PKRU register will be restored from the
12211221
* thread_struct's pkru field. A pointer to that is passed in @pkru.
1222+
* The kernel will restore it manually, so the XRSTOR behavior that resets
1223+
* the PKRU register to the hardware init value (0) if the corresponding
1224+
* xfeatures bit is not set is emulated here.
12221225
* 3. When called from KVM the PKRU register will be restored from the vcpu's
1223-
* pkru field. A pointer to that is passed in @pkru.
1226+
* pkru field. A pointer to that is passed in @pkru. KVM hasn't used
1227+
* XRSTOR and hasn't had the PKRU resetting behavior described above. To
1228+
* preserve that KVM behavior, it passes NULL for @pkru if the xfeatures
1229+
* bit is not set.
12241230
*/
12251231
static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf,
12261232
const void __user *ubuf, u32 *pkru)
@@ -1277,6 +1283,13 @@ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf,
12771283

12781284
xpkru = __raw_xsave_addr(xsave, XFEATURE_PKRU);
12791285
*pkru = xpkru->pkru;
1286+
} else {
1287+
/*
1288+
* KVM may pass NULL here to indicate that it does not need
1289+
* PKRU updated.
1290+
*/
1291+
if (pkru)
1292+
*pkru = 0;
12801293
}
12811294

12821295
/*

0 commit comments

Comments
 (0)