Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch/x86/x86 Revert 1.67 "Restore the lwp's fpu state, n...



details:   https://anonhg.NetBSD.org/src/rev/d82a851aee3c
branches:  trunk
changeset: 936178:d82a851aee3c
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Mon Jul 20 16:37:34 2020 +0000

description:
Revert 1.67 "Restore the lwp's fpu state, not zeros, and leave with fpu enabled."

This didn't actually avoid double-restore, and it doesn't solve the
problem anyway, and made it harder to detect in-kernel fpu abuse.

diffstat:

 sys/arch/x86/x86/fpu.c |  28 ++++++++++++----------------
 1 files changed, 12 insertions(+), 16 deletions(-)

diffs (57 lines):

diff -r aa6dfefbdea4 -r d82a851aee3c sys/arch/x86/x86/fpu.c
--- a/sys/arch/x86/x86/fpu.c    Mon Jul 20 16:32:14 2020 +0000
+++ b/sys/arch/x86/x86/fpu.c    Mon Jul 20 16:37:34 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fpu.c,v 1.68 2020/07/13 16:51:51 riastradh Exp $       */
+/*     $NetBSD: fpu.c,v 1.69 2020/07/20 16:37:34 riastradh Exp $       */
 
 /*
  * Copyright (c) 2008, 2019 The NetBSD Foundation, Inc.  All
@@ -96,7 +96,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.68 2020/07/13 16:51:51 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fpu.c,v 1.69 2020/07/20 16:37:34 riastradh Exp $");
 
 #include "opt_multiprocessor.h"
 
@@ -421,9 +421,6 @@
 fpu_kern_leave(void)
 {
        static const union savefpu zero_fpu __aligned(64);
-       const union savefpu *savefpu;
-       struct lwp *l = curlwp;
-       struct pcb *pcb;
        struct cpu_info *ci = curcpu();
        int s;
 
@@ -431,18 +428,17 @@
        KASSERT(ci->ci_kfpu_spl != -1);
 
        /*
-        * Restore the FPU state immediately to avoid leaking any
-        * kernel secrets, or zero it if this is a kthread.
+        * Zero the fpu registers; otherwise we might leak secrets
+        * through Spectre-class attacks to userland, even if there are
+        * no bugs in fpu state management.
         */
-       if ((l->l_pflag & LP_INTR) && (l->l_switchto != NULL))
-               l = l->l_switchto;
-       if (l->l_flag & LW_SYSTEM) {
-               savefpu = &zero_fpu;
-       } else {
-               pcb = lwp_getpcb(l);
-               savefpu = &pcb->pcb_savefpu;
-       }
-       fpu_area_restore(savefpu, x86_xsave_features);
+       fpu_area_restore(&zero_fpu, x86_xsave_features);
+
+       /*
+        * Set CR0_TS again so that the kernel can't accidentally use
+        * the FPU.
+        */
+       stts();
 
        s = ci->ci_kfpu_spl;
        ci->ci_kfpu_spl = -1;



Home | Main Index | Thread Index | Old Index