Re: [PATCH v5 14/28] x86/fpu/xstate: Prevent unauthorised use of dynamic user state

Bae, Chang Seok
2021-06-29T18:35:49Z

On Jun 29, 2021, at 10:54, Hansen, Dave <dave.hansen@intel.com> wrote:
> How about we pay this 26-cycle cost, but only when XFD is in use? We
> could either look at the shadowed value of the XFD MSR, or flip a global
> variable the first time XFD gets used.

How about something like this:

diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c
index 777cccab0b47..9752ebe6be79 100644
--- a/arch/x86/kernel/fpu/core.c
+++ b/arch/x86/kernel/fpu/core.c
@@ -99,6 +99,23 @@ EXPORT_SYMBOL(irq_fpu_usable);
void save_fpregs_to_fpstate(struct fpu *fpu)
{
if (likely(use_xsave())) {
+ /*
+ * MSR IA32_XFD write follows after this XSAVE(S). So if a
+ * state component is in use, XFD should not be armed for
+ * current. But, for potential changes in the future,
+ * cross-check XINUSE and XFD values. If a XINUSE state
+ * is XFD-armed, the following XSAVE(S) does not save the
+ * state.
+ *
+ * Reference the shadow XFD value instead of reading the
+ * MSR.
+ */
+ if (xfd_capable() && boot_cpu_has(X86_FEATURE_XGETBV1)) {
+ u64 current_xfd = (fpu->state_mask & xfd_capable()) ^ xfd_capable();
+
+ WARN_ON_FPU(xgetbv(1) & current_xfd);
+ }
+
os_xsave(&fpu->state->xsave, fpu->state_mask);

Thanks,
Chang