x86/CPU/AMD: Fix the DIV(0) initial fix attempt
authorBorislav Petkov (AMD) <bp@alien8.de>
Fri, 11 Aug 2023 21:38:24 +0000 (23:38 +0200)
committerBorislav Petkov (AMD) <bp@alien8.de>
Mon, 14 Aug 2023 09:02:50 +0000 (11:02 +0200)
Initially, it was thought that doing an innocuous division in the #DE
handler would take care to prevent any leaking of old data from the
divider but by the time the fault is raised, the speculation has already
advanced too far and such data could already have been used by younger
operations.

Therefore, do the innocuous division on every exit to userspace so that
userspace doesn't see any potentially old data from integer divisions in
kernel space.

Do the same before VMRUN too, to protect host data from leaking into the
guest too.

Fixes: 77245f1c3c64 ("x86/CPU/AMD: Do not leak quotient data after a division by 0")
Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de>
Cc: <stable@kernel.org>
Link: https://lore.kernel.org/r/20230811213824.10025-1-bp@alien8.de
arch/x86/include/asm/entry-common.h
arch/x86/kernel/cpu/amd.c
arch/x86/kernel/traps.c
arch/x86/kvm/svm/svm.c

index 117903881fe43eacf5d2167afcfc02461bfffbbd..ce8f50192ae3e46da87fe3a24fc736b3b2fc3b21 100644 (file)
@@ -92,6 +92,7 @@ static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
 static __always_inline void arch_exit_to_user_mode(void)
 {
        mds_user_clear_cpu_buffers();
+       amd_clear_divider();
 }
 #define arch_exit_to_user_mode arch_exit_to_user_mode
 
index 70f9d56f93057dde0fc12a053319f234a5be19a5..7eca6a8abbb1c67ddf3f48164f2aec03b194a916 100644 (file)
@@ -1329,3 +1329,4 @@ void noinstr amd_clear_divider(void)
        asm volatile(ALTERNATIVE("", "div %2\n\t", X86_BUG_DIV0)
                     :: "a" (0), "d" (0), "r" (1));
 }
+EXPORT_SYMBOL_GPL(amd_clear_divider);
index 1885326a8f65969a1258a5e1bf16afdf4871d870..4a817d20ce3bb0e15e9437a224a7e1f721b61a4e 100644 (file)
@@ -206,8 +206,6 @@ DEFINE_IDTENTRY(exc_divide_error)
 {
        do_error_trap(regs, 0, "divide error", X86_TRAP_DE, SIGFPE,
                      FPE_INTDIV, error_get_trap_addr(regs));
-
-       amd_clear_divider();
 }
 
 DEFINE_IDTENTRY(exc_overflow)
index 03e852dedcc1bda3812e31b563e1fd3b5db5d8d6..d4bfdc607fe7f3257c9c68a4ee34c6a2c3a7e5c9 100644 (file)
@@ -4006,6 +4006,8 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu, bool spec_ctrl_in
 
        guest_state_enter_irqoff();
 
+       amd_clear_divider();
+
        if (sev_es_guest(vcpu->kvm))
                __svm_sev_es_vcpu_run(svm, spec_ctrl_intercepted);
        else