Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86/CPU/AMD: Fix the DIV(0) initial fix attempt

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

+4 -2
+1
arch/x86/include/asm/entry-common.h
··· 92 92 static __always_inline void arch_exit_to_user_mode(void) 93 93 { 94 94 mds_user_clear_cpu_buffers(); 95 + amd_clear_divider(); 95 96 } 96 97 #define arch_exit_to_user_mode arch_exit_to_user_mode 97 98
+1
arch/x86/kernel/cpu/amd.c
··· 1329 1329 asm volatile(ALTERNATIVE("", "div %2\n\t", X86_BUG_DIV0) 1330 1330 :: "a" (0), "d" (0), "r" (1)); 1331 1331 } 1332 + EXPORT_SYMBOL_GPL(amd_clear_divider);
-2
arch/x86/kernel/traps.c
··· 206 206 { 207 207 do_error_trap(regs, 0, "divide error", X86_TRAP_DE, SIGFPE, 208 208 FPE_INTDIV, error_get_trap_addr(regs)); 209 - 210 - amd_clear_divider(); 211 209 } 212 210 213 211 DEFINE_IDTENTRY(exc_overflow)
+2
arch/x86/kvm/svm/svm.c
··· 4006 4006 4007 4007 guest_state_enter_irqoff(); 4008 4008 4009 + amd_clear_divider(); 4010 + 4009 4011 if (sev_es_guest(vcpu->kvm)) 4010 4012 __svm_sev_es_vcpu_run(svm, spec_ctrl_intercepted); 4011 4013 else