diff mbox series

[SRU,Focal,3/3] x86/CPU/AMD: Fix the DIV(0) initial fix attempt

Message ID 20230830143837.220465-4-cascardo@canonical.com
State New
Headers show
Series CVE-2023-20588 | expand

Commit Message

Thadeu Lima de Souza Cascardo Aug. 30, 2023, 2:38 p.m. UTC
From: "Borislav Petkov (AMD)" <bp@alien8.de>

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
(backported from commit f58d6fbcb7c848b7f2469be339bc571f2e9d245b)
[cascardo: context conflict fixup at arch/x86/kvm/svm/svm.c]
[cascardo: call amd_clear_divider before vmload and at kernel exit]
CVE-2023-20588
Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@canonical.com>
---
 arch/x86/entry/common.c   | 1 +
 arch/x86/kernel/cpu/amd.c | 1 +
 arch/x86/kvm/svm.c        | 2 ++
 3 files changed, 4 insertions(+)
diff mbox series

Patch

diff --git a/arch/x86/entry/common.c b/arch/x86/entry/common.c
index 3f8e22615812..721109c0cf99 100644
--- a/arch/x86/entry/common.c
+++ b/arch/x86/entry/common.c
@@ -218,6 +218,7 @@  __visible inline void prepare_exit_to_usermode(struct pt_regs *regs)
 	user_enter_irqoff();
 
 	mds_user_clear_cpu_buffers();
+	amd_clear_divider();
 }
 
 #define SYSCALL_EXIT_WORK_FLAGS				\
diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c
index 5413f25cf9d7..d068bdfb3e56 100644
--- a/arch/x86/kernel/cpu/amd.c
+++ b/arch/x86/kernel/cpu/amd.c
@@ -1259,3 +1259,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);
diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
index 7e66a2b1d4dd..c2885d4aa43d 100644
--- a/arch/x86/kvm/svm.c
+++ b/arch/x86/kvm/svm.c
@@ -5718,6 +5718,8 @@  static void svm_vcpu_run(struct kvm_vcpu *vcpu)
 
 	local_irq_enable();
 
+	amd_clear_divider();
+
 	asm volatile (
 		"push %%" _ASM_BP "; \n\t"
 		"mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"