Message ID | 20220808141538.102394-1-khuey@kylehuey.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | [v5,1/2] x86/fpu: Allow PKRU to be (once again) written by ptrace. | expand |
On Mon, Aug 8, 2022 at 7:15 AM Kyle Huey <me@kylehuey.com> wrote: > > From: Kyle Huey <me@kylehuey.com> > > When management of the PKRU register was moved away from XSTATE, emulation > of PKRU's existence in XSTATE was added for APIs that read XSTATE, but not > for APIs that write XSTATE. This 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. > > There are three relevant APIs: PTRACE_SETREGSET with NT_X86_XSTATE, > sigreturn, and KVM_SET_XSAVE. KVM_SET_XSAVE has its own special handling to > make PKRU writes take effect (in fpu_copy_uabi_to_guest_fpstate). Push that > down into copy_uabi_to_xstate and have PTRACE_SETREGSET with NT_X86_XSTATE > and sigreturn pass in pointers to the appropriate PKRU value. > > This also adds code to initialize the PKRU value to the hardware init value > (namely 0) if the PKRU bit is not set in the XSTATE header to match XRSTOR. > This is a change to the current KVM_SET_XSAVE behavior. > > Changelog since v4: > - Selftest additionally checks PKRU readbacks through ptrace. > - Selftest flips all PKRU bits (except the key used for PROT_EXEC). > > Changelog since v3: > - The v3 patch is now part 1 of 2. > - Adds a selftest in part 2 of 2. > > Changelog since v2: > - Removed now unused variables in fpu_copy_uabi_to_guest_fpstate > > Changelog since v1: > - Handles the error case of copy_to_buffer(). > > Signed-off-by: Kyle Huey <me@kylehuey.com> > Cc: Dave Hansen <dave.hansen@linux.intel.com> > Cc: Thomas Gleixner <tglx@linutronix.de> > Cc: Borislav Petkov <bp@suse.de> > Cc: kvm@vger.kernel.org # For edge case behavior of KVM_SET_XSAVE > Cc: stable@vger.kernel.org # 5.14+ > Fixes: e84ba47e313d ("x86/fpu: Hook up PKRU into ptrace()") > --- > arch/x86/kernel/fpu/core.c | 13 +------------ > arch/x86/kernel/fpu/regset.c | 2 +- > arch/x86/kernel/fpu/signal.c | 2 +- > arch/x86/kernel/fpu/xstate.c | 28 +++++++++++++++++++++++----- > arch/x86/kernel/fpu/xstate.h | 4 ++-- > 5 files changed, 28 insertions(+), 21 deletions(-) > > diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c > index 3b28c5b25e12..46b935bc87c8 100644 > --- a/arch/x86/kernel/fpu/core.c > +++ b/arch/x86/kernel/fpu/core.c > @@ -391,8 +391,6 @@ int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *gfpu, const void *buf, > { > struct fpstate *kstate = gfpu->fpstate; > const union fpregs_state *ustate = buf; > - struct pkru_state *xpkru; > - int ret; > > if (!cpu_feature_enabled(X86_FEATURE_XSAVE)) { > if (ustate->xsave.header.xfeatures & ~XFEATURE_MASK_FPSSE) > @@ -406,16 +404,7 @@ int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *gfpu, const void *buf, > if (ustate->xsave.header.xfeatures & ~xcr0) > return -EINVAL; > > - ret = copy_uabi_from_kernel_to_xstate(kstate, ustate); > - if (ret) > - return ret; > - > - /* Retrieve PKRU if not in init state */ > - if (kstate->regs.xsave.header.xfeatures & XFEATURE_MASK_PKRU) { > - xpkru = get_xsave_addr(&kstate->regs.xsave, XFEATURE_PKRU); > - *vpkru = xpkru->pkru; > - } > - return 0; > + return copy_uabi_from_kernel_to_xstate(kstate, ustate, vpkru); > } > EXPORT_SYMBOL_GPL(fpu_copy_uabi_to_guest_fpstate); > #endif /* CONFIG_KVM */ > diff --git a/arch/x86/kernel/fpu/regset.c b/arch/x86/kernel/fpu/regset.c > index 75ffaef8c299..6d056b68f4ed 100644 > --- a/arch/x86/kernel/fpu/regset.c > +++ b/arch/x86/kernel/fpu/regset.c > @@ -167,7 +167,7 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset, > } > > fpu_force_restore(fpu); > - ret = copy_uabi_from_kernel_to_xstate(fpu->fpstate, kbuf ?: tmpbuf); > + ret = copy_uabi_from_kernel_to_xstate(fpu->fpstate, kbuf ?: tmpbuf, &target->thread.pkru); > > out: > vfree(tmpbuf); > diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c > index 91d4b6de58ab..558076dbde5b 100644 > --- a/arch/x86/kernel/fpu/signal.c > +++ b/arch/x86/kernel/fpu/signal.c > @@ -396,7 +396,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx, > > fpregs = &fpu->fpstate->regs; > if (use_xsave() && !fx_only) { > - if (copy_sigframe_from_user_to_xstate(fpu->fpstate, buf_fx)) > + if (copy_sigframe_from_user_to_xstate(tsk, buf_fx)) > return false; > } else { > if (__copy_from_user(&fpregs->fxsave, buf_fx, > diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c > index c8340156bfd2..e01d3514ae68 100644 > --- a/arch/x86/kernel/fpu/xstate.c > +++ b/arch/x86/kernel/fpu/xstate.c > @@ -1197,7 +1197,7 @@ static int copy_from_buffer(void *dst, unsigned int offset, unsigned int size, > > > static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, > - const void __user *ubuf) > + const void __user *ubuf, u32 *pkru) > { > struct xregs_state *xsave = &fpstate->regs.xsave; > unsigned int offset, size; > @@ -1235,6 +1235,24 @@ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, > for (i = 0; i < XFEATURE_MAX; i++) { > mask = BIT_ULL(i); > > + if (i == XFEATURE_PKRU) { > + /* > + * Retrieve PKRU if not in init state, otherwise > + * initialize it. > + */ > + if (hdr.xfeatures & mask) { > + struct pkru_state xpkru = {0}; > + > + if (copy_from_buffer(&xpkru, xstate_offsets[i], > + sizeof(xpkru), kbuf, ubuf)) > + return -EFAULT; > + > + *pkru = xpkru.pkru; > + } else { > + *pkru = 0; > + } > + } > + > if (hdr.xfeatures & mask) { > void *dst = __raw_xsave_addr(xsave, i); > > @@ -1264,9 +1282,9 @@ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, > * Convert from a ptrace standard-format kernel buffer to kernel XSAVE[S] > * format and copy to the target thread. Used by ptrace and KVM. > */ > -int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf) > +int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf, u32 *pkru) > { > - return copy_uabi_to_xstate(fpstate, kbuf, NULL); > + return copy_uabi_to_xstate(fpstate, kbuf, NULL, pkru); > } > > /* > @@ -1274,10 +1292,10 @@ int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf) > * XSAVE[S] format and copy to the target thread. This is called from the > * sigreturn() and rt_sigreturn() system calls. > */ > -int copy_sigframe_from_user_to_xstate(struct fpstate *fpstate, > +int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, > const void __user *ubuf) > { > - return copy_uabi_to_xstate(fpstate, NULL, ubuf); > + return copy_uabi_to_xstate(tsk->thread.fpu.fpstate, NULL, ubuf, &tsk->thread.pkru); > } > > static bool validate_independent_components(u64 mask) > diff --git a/arch/x86/kernel/fpu/xstate.h b/arch/x86/kernel/fpu/xstate.h > index 5ad47031383b..a4ecb04d8d64 100644 > --- a/arch/x86/kernel/fpu/xstate.h > +++ b/arch/x86/kernel/fpu/xstate.h > @@ -46,8 +46,8 @@ extern void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate, > u32 pkru_val, enum xstate_copy_mode copy_mode); > extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk, > enum xstate_copy_mode mode); > -extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf); > -extern int copy_sigframe_from_user_to_xstate(struct fpstate *fpstate, const void __user *ubuf); > +extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf, u32 *pkru); > +extern int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void __user *ubuf); > > > extern void fpu__init_cpu_xstate(void); > -- > 2.37.1 > Bump. If there are no further comments/complaints, can we get this queued up via x86/urgent (or something)? We're eager to get this moving and eventually onto stable to fix rr users on 5.15. - Kyle
Kyle! On Mon, Aug 08 2022 at 07:15, Kyle Huey wrote: > When management of the PKRU register was moved away from XSTATE, emulation > of PKRU's existence in XSTATE was added for APIs that read XSTATE, but not > for APIs that write XSTATE. This 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. > > There are three relevant APIs: PTRACE_SETREGSET with NT_X86_XSTATE, > sigreturn, and KVM_SET_XSAVE. KVM_SET_XSAVE has its own special handling to > make PKRU writes take effect (in fpu_copy_uabi_to_guest_fpstate). Push that > down into copy_uabi_to_xstate and have PTRACE_SETREGSET with NT_X86_XSTATE > and sigreturn pass in pointers to the appropriate PKRU value. > > This also adds code to initialize the PKRU value to the hardware init value > (namely 0) if the PKRU bit is not set in the XSTATE header to match XRSTOR. > This is a change to the current KVM_SET_XSAVE behavior. You are stating a fact here, but provide 0 justification why this is correct. > > Changelog since v4: Can you please put the change log past the --- seperator line, so it gets stripped off when the patch is applied? That spares manual fixups. > > Signed-off-by: Kyle Huey <me@kylehuey.com> > Cc: Dave Hansen <dave.hansen@linux.intel.com> > Cc: Thomas Gleixner <tglx@linutronix.de> > Cc: Borislav Petkov <bp@suse.de> > Cc: kvm@vger.kernel.org # For edge case behavior of KVM_SET_XSAVE > Cc: stable@vger.kernel.org # 5.14+ > Fixes: e84ba47e313d ("x86/fpu: Hook up PKRU into ptrace()") Can you please use the documented tag ordering? https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#patch-submission-notes > @@ -1235,6 +1235,24 @@ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, > for (i = 0; i < XFEATURE_MAX; i++) { > mask = BIT_ULL(i); > > + if (i == XFEATURE_PKRU) { > + /* > + * Retrieve PKRU if not in init state, otherwise > + * initialize it. > + */ > + if (hdr.xfeatures & mask) { > + struct pkru_state xpkru = {0}; > + > + if (copy_from_buffer(&xpkru, xstate_offsets[i], > + sizeof(xpkru), kbuf, ubuf)) > + return -EFAULT; > + > + *pkru = xpkru.pkru; > + } else { > + *pkru = 0; > + } > + } That's really horrible and there is no point in copying the stuff from the buffer twice: @@ -1246,6 +1246,15 @@ static int copy_uabi_to_xstate(struct fp } } + /* Update the user protection key storage */ + *pkru = 0; + if (hdr.xfeatures & XFEATURE_MASK_PKRU) { + struct pkru_state *xpkru; + + xpkru = get_xsave_addr(xsave, XFEATURE_PKRU); + *pkru = xpkru->pkru; + } + Hmm? Thanks, tglx
On Thu, Aug 18, 2022 at 3:57 AM Thomas Gleixner <tglx@linutronix.de> wrote: > > Kyle! Hi. > On Mon, Aug 08 2022 at 07:15, Kyle Huey wrote: > > When management of the PKRU register was moved away from XSTATE, emulation > > of PKRU's existence in XSTATE was added for APIs that read XSTATE, but not > > for APIs that write XSTATE. This 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. > > > > There are three relevant APIs: PTRACE_SETREGSET with NT_X86_XSTATE, > > sigreturn, and KVM_SET_XSAVE. KVM_SET_XSAVE has its own special handling to > > make PKRU writes take effect (in fpu_copy_uabi_to_guest_fpstate). Push that > > down into copy_uabi_to_xstate and have PTRACE_SETREGSET with NT_X86_XSTATE > > and sigreturn pass in pointers to the appropriate PKRU value. > > > > This also adds code to initialize the PKRU value to the hardware init value > > (namely 0) if the PKRU bit is not set in the XSTATE header to match XRSTOR. > > This is a change to the current KVM_SET_XSAVE behavior. > > You are stating a fact here, but provide 0 justification why this is > correct. Well, the justification is that this *is* the behavior we want for ptrace/sigreturn, and it's very likely the existing KVM_SET_XSAVE behavior in this edge case is an oversight rather than intentional, and in the absence of confirmation that KVM wants the existing behavior (the KVM mailing list and maintainer are CCd) one correct code path is better than one correct code path and one buggy code path. > > > > Changelog since v4: > > Can you please put the change log past the --- seperator line, so it > gets stripped off when the patch is applied? That spares manual fixups. Ok. > > > > Signed-off-by: Kyle Huey <me@kylehuey.com> > > Cc: Dave Hansen <dave.hansen@linux.intel.com> > > Cc: Thomas Gleixner <tglx@linutronix.de> > > Cc: Borislav Petkov <bp@suse.de> > > Cc: kvm@vger.kernel.org # For edge case behavior of KVM_SET_XSAVE > > Cc: stable@vger.kernel.org # 5.14+ > > Fixes: e84ba47e313d ("x86/fpu: Hook up PKRU into ptrace()") > > Can you please use the documented tag ordering? > > https://www.kernel.org/doc/html/latest/process/maintainer-tip.html#patch-submission-notes Ok. > > @@ -1235,6 +1235,24 @@ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, > > for (i = 0; i < XFEATURE_MAX; i++) { > > mask = BIT_ULL(i); > > > > + if (i == XFEATURE_PKRU) { > > + /* > > + * Retrieve PKRU if not in init state, otherwise > > + * initialize it. > > + */ > > + if (hdr.xfeatures & mask) { > > + struct pkru_state xpkru = {0}; > > + > > + if (copy_from_buffer(&xpkru, xstate_offsets[i], > > + sizeof(xpkru), kbuf, ubuf)) > > + return -EFAULT; > > + > > + *pkru = xpkru.pkru; > > + } else { > > + *pkru = 0; > > + } > > + } > > That's really horrible and there is no point in copying the stuff from > the buffer twice: > > @@ -1246,6 +1246,15 @@ static int copy_uabi_to_xstate(struct fp > } > } > > + /* Update the user protection key storage */ > + *pkru = 0; > + if (hdr.xfeatures & XFEATURE_MASK_PKRU) { > + struct pkru_state *xpkru; > + > + xpkru = get_xsave_addr(xsave, XFEATURE_PKRU); > + *pkru = xpkru->pkru; > + } > + > > Hmm? It took me a bit to figure out what this is actually trying to do. To work, it would need to come at the very end of copy_uabi_to_xstate after xsave->header.xfeatures is updated. If you just want to avoid two copies I would counter-propose this though: @@ -1235,7 +1235,19 @@ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, for (i = 0; i < XFEATURE_MAX; i++) { mask = BIT_ULL(i); - if (hdr.xfeatures & mask) { + if (i == XFEATURE_PKRU) { + /* Update the user protection key storage */ + *pkru = 0; + if (hdr.xfeatures & XFEATURE_MASK_PKRU) { + struct pkru_state xpkru = {0}; + + if (copy_from_buffer(&xpkru, xstate_offsets[i], + sizeof(xpkru), kbuf, ubuf)) + return -EFAULT; + + *pkru = xpkru.pkru; + } + } else if (hdr.xfeatures & mask) { void *dst = __raw_xsave_addr(xsave, i); offset = xstate_offsets[i]; Thoughts? This avoids a second copy and avoids having to calculate the offset into the (now potentially compressed) XSTATE. - Kyle > > Thanks, > > tglx
On Thu, Aug 18, 2022, Kyle Huey wrote: > On Thu, Aug 18, 2022 at 3:57 AM Thomas Gleixner <tglx@linutronix.de> wrote: > > On Mon, Aug 08 2022 at 07:15, Kyle Huey wrote: > > > When management of the PKRU register was moved away from XSTATE, emulation > > > of PKRU's existence in XSTATE was added for APIs that read XSTATE, but not > > > for APIs that write XSTATE. This 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. > > > > > > There are three relevant APIs: PTRACE_SETREGSET with NT_X86_XSTATE, > > > sigreturn, and KVM_SET_XSAVE. KVM_SET_XSAVE has its own special handling to > > > make PKRU writes take effect (in fpu_copy_uabi_to_guest_fpstate). Push that > > > down into copy_uabi_to_xstate and have PTRACE_SETREGSET with NT_X86_XSTATE > > > and sigreturn pass in pointers to the appropriate PKRU value. > > > > > > This also adds code to initialize the PKRU value to the hardware init value > > > (namely 0) if the PKRU bit is not set in the XSTATE header to match XRSTOR. > > > This is a change to the current KVM_SET_XSAVE behavior. > > > > You are stating a fact here, but provide 0 justification why this is > > correct. > > Well, the justification is that this *is* the behavior we want for > ptrace/sigreturn, and it's very likely the existing KVM_SET_XSAVE > behavior in this edge case is an oversight rather than intentional, > and in the absence of confirmation that KVM wants the existing > behavior (the KVM mailing list and maintainer are CCd) one correct > code path is better than one correct code path and one buggy code > path. Sorry, I missed the KVM-relevant flags. Hrm, the current behavior has been KVM ABI for a very long time. It's definitely odd because all other components will be initialized due to their bits being cleared in the header during kvm_load_guest_fpu(), and it probably wouldn't cause problems in practice as most VMMs likely do "all or nothing" loads. But, in theory, userspace could save/restore a subset of guest XSTATE and rely on the kernel not overwriting guest PKRU when its bit is cleared in the header. All that said, I don't see any reason to force KVM to change at this time, it's trivial enough to handle KVM's oddities while providing sane behavior for others. Nullify the pointer in the guest path and then update copy_uabi_to_xstate() to play nice with a NULL pointer, e.g. /* * Nullify @vpkru to preserve its current value if PKRU's bit isn't set * in the header. KVM's odd ABI is to leave PKRU untouched in this * case (all other components are eventually re-initialized). */ if (!(kstate->regs.xsave.header.xfeatures & XFEATURE_MASK_PKRU)) vpkru = NULL; return copy_uabi_from_kernel_to_xstate(kstate, ustate, vpkru);
On Thu, Aug 18, 2022 at 2:19 PM Sean Christopherson <seanjc@google.com> wrote: > > On Thu, Aug 18, 2022, Kyle Huey wrote: > > On Thu, Aug 18, 2022 at 3:57 AM Thomas Gleixner <tglx@linutronix.de> wrote: > > > On Mon, Aug 08 2022 at 07:15, Kyle Huey wrote: > > > > When management of the PKRU register was moved away from XSTATE, emulation > > > > of PKRU's existence in XSTATE was added for APIs that read XSTATE, but not > > > > for APIs that write XSTATE. This 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. > > > > > > > > There are three relevant APIs: PTRACE_SETREGSET with NT_X86_XSTATE, > > > > sigreturn, and KVM_SET_XSAVE. KVM_SET_XSAVE has its own special handling to > > > > make PKRU writes take effect (in fpu_copy_uabi_to_guest_fpstate). Push that > > > > down into copy_uabi_to_xstate and have PTRACE_SETREGSET with NT_X86_XSTATE > > > > and sigreturn pass in pointers to the appropriate PKRU value. > > > > > > > > This also adds code to initialize the PKRU value to the hardware init value > > > > (namely 0) if the PKRU bit is not set in the XSTATE header to match XRSTOR. > > > > This is a change to the current KVM_SET_XSAVE behavior. > > > > > > You are stating a fact here, but provide 0 justification why this is > > > correct. > > > > Well, the justification is that this *is* the behavior we want for > > ptrace/sigreturn, and it's very likely the existing KVM_SET_XSAVE > > behavior in this edge case is an oversight rather than intentional, > > and in the absence of confirmation that KVM wants the existing > > behavior (the KVM mailing list and maintainer are CCd) one correct > > code path is better than one correct code path and one buggy code > > path. > > Sorry, I missed the KVM-relevant flags. > > Hrm, the current behavior has been KVM ABI for a very long time. > > It's definitely odd because all other components will be initialized due to their > bits being cleared in the header during kvm_load_guest_fpu(), and it probably > wouldn't cause problems in practice as most VMMs likely do "all or nothing" loads. > But, in theory, userspace could save/restore a subset of guest XSTATE and rely on > the kernel not overwriting guest PKRU when its bit is cleared in the header. This seems extremely conservative, but ok. As you note, PKRU is the only XSTATE component you could theoretically do this subset save/restore with in the KVM ABI since all the others really do have their hardware behavior. > All that said, I don't see any reason to force KVM to change at this time, it's > trivial enough to handle KVM's oddities while providing sane behavior for others. > Nullify the pointer in the guest path and then update copy_uabi_to_xstate() to > play nice with a NULL pointer, e.g. > > /* > * Nullify @vpkru to preserve its current value if PKRU's bit isn't set > * in the header. KVM's odd ABI is to leave PKRU untouched in this > * case (all other components are eventually re-initialized). > */ > if (!(kstate->regs.xsave.header.xfeatures & XFEATURE_MASK_PKRU)) > vpkru = NULL; You meant ustate->... here (since this is before the copy now), but yes, ok, I will do that. > return copy_uabi_from_kernel_to_xstate(kstate, ustate, vpkru); - Kyle
diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 3b28c5b25e12..46b935bc87c8 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -391,8 +391,6 @@ int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *gfpu, const void *buf, { struct fpstate *kstate = gfpu->fpstate; const union fpregs_state *ustate = buf; - struct pkru_state *xpkru; - int ret; if (!cpu_feature_enabled(X86_FEATURE_XSAVE)) { if (ustate->xsave.header.xfeatures & ~XFEATURE_MASK_FPSSE) @@ -406,16 +404,7 @@ int fpu_copy_uabi_to_guest_fpstate(struct fpu_guest *gfpu, const void *buf, if (ustate->xsave.header.xfeatures & ~xcr0) return -EINVAL; - ret = copy_uabi_from_kernel_to_xstate(kstate, ustate); - if (ret) - return ret; - - /* Retrieve PKRU if not in init state */ - if (kstate->regs.xsave.header.xfeatures & XFEATURE_MASK_PKRU) { - xpkru = get_xsave_addr(&kstate->regs.xsave, XFEATURE_PKRU); - *vpkru = xpkru->pkru; - } - return 0; + return copy_uabi_from_kernel_to_xstate(kstate, ustate, vpkru); } EXPORT_SYMBOL_GPL(fpu_copy_uabi_to_guest_fpstate); #endif /* CONFIG_KVM */ diff --git a/arch/x86/kernel/fpu/regset.c b/arch/x86/kernel/fpu/regset.c index 75ffaef8c299..6d056b68f4ed 100644 --- a/arch/x86/kernel/fpu/regset.c +++ b/arch/x86/kernel/fpu/regset.c @@ -167,7 +167,7 @@ int xstateregs_set(struct task_struct *target, const struct user_regset *regset, } fpu_force_restore(fpu); - ret = copy_uabi_from_kernel_to_xstate(fpu->fpstate, kbuf ?: tmpbuf); + ret = copy_uabi_from_kernel_to_xstate(fpu->fpstate, kbuf ?: tmpbuf, &target->thread.pkru); out: vfree(tmpbuf); diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c index 91d4b6de58ab..558076dbde5b 100644 --- a/arch/x86/kernel/fpu/signal.c +++ b/arch/x86/kernel/fpu/signal.c @@ -396,7 +396,7 @@ static bool __fpu_restore_sig(void __user *buf, void __user *buf_fx, fpregs = &fpu->fpstate->regs; if (use_xsave() && !fx_only) { - if (copy_sigframe_from_user_to_xstate(fpu->fpstate, buf_fx)) + if (copy_sigframe_from_user_to_xstate(tsk, buf_fx)) return false; } else { if (__copy_from_user(&fpregs->fxsave, buf_fx, diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index c8340156bfd2..e01d3514ae68 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -1197,7 +1197,7 @@ static int copy_from_buffer(void *dst, unsigned int offset, unsigned int size, static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, - const void __user *ubuf) + const void __user *ubuf, u32 *pkru) { struct xregs_state *xsave = &fpstate->regs.xsave; unsigned int offset, size; @@ -1235,6 +1235,24 @@ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, for (i = 0; i < XFEATURE_MAX; i++) { mask = BIT_ULL(i); + if (i == XFEATURE_PKRU) { + /* + * Retrieve PKRU if not in init state, otherwise + * initialize it. + */ + if (hdr.xfeatures & mask) { + struct pkru_state xpkru = {0}; + + if (copy_from_buffer(&xpkru, xstate_offsets[i], + sizeof(xpkru), kbuf, ubuf)) + return -EFAULT; + + *pkru = xpkru.pkru; + } else { + *pkru = 0; + } + } + if (hdr.xfeatures & mask) { void *dst = __raw_xsave_addr(xsave, i); @@ -1264,9 +1282,9 @@ static int copy_uabi_to_xstate(struct fpstate *fpstate, const void *kbuf, * Convert from a ptrace standard-format kernel buffer to kernel XSAVE[S] * format and copy to the target thread. Used by ptrace and KVM. */ -int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf) +int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf, u32 *pkru) { - return copy_uabi_to_xstate(fpstate, kbuf, NULL); + return copy_uabi_to_xstate(fpstate, kbuf, NULL, pkru); } /* @@ -1274,10 +1292,10 @@ int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf) * XSAVE[S] format and copy to the target thread. This is called from the * sigreturn() and rt_sigreturn() system calls. */ -int copy_sigframe_from_user_to_xstate(struct fpstate *fpstate, +int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void __user *ubuf) { - return copy_uabi_to_xstate(fpstate, NULL, ubuf); + return copy_uabi_to_xstate(tsk->thread.fpu.fpstate, NULL, ubuf, &tsk->thread.pkru); } static bool validate_independent_components(u64 mask) diff --git a/arch/x86/kernel/fpu/xstate.h b/arch/x86/kernel/fpu/xstate.h index 5ad47031383b..a4ecb04d8d64 100644 --- a/arch/x86/kernel/fpu/xstate.h +++ b/arch/x86/kernel/fpu/xstate.h @@ -46,8 +46,8 @@ extern void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate, u32 pkru_val, enum xstate_copy_mode copy_mode); extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk, enum xstate_copy_mode mode); -extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf); -extern int copy_sigframe_from_user_to_xstate(struct fpstate *fpstate, const void __user *ubuf); +extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const void *kbuf, u32 *pkru); +extern int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, const void __user *ubuf); extern void fpu__init_cpu_xstate(void);