Skip to content

Commit 77245f1

Browse files
bp3tk0vtorvalds
authored andcommitted
x86/CPU/AMD: Do not leak quotient data after a division by 0
Under certain circumstances, an integer division by 0 which faults, can leave stale quotient data from a previous division operation on Zen1 microarchitectures. Do a dummy division 0/1 before returning from the #DE exception handler in order to avoid any leaks of potentially sensitive data. Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Cc: <stable@kernel.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 13b9372 commit 77245f1

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

arch/x86/include/asm/cpufeatures.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,4 +495,5 @@
495495

496496
/* BUG word 2 */
497497
#define X86_BUG_SRSO X86_BUG(1*32 + 0) /* AMD SRSO bug */
498+
#define X86_BUG_DIV0 X86_BUG(1*32 + 1) /* AMD DIV0 speculation bug */
498499
#endif /* _ASM_X86_CPUFEATURES_H */

arch/x86/include/asm/processor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,10 +683,12 @@ extern u16 get_llc_id(unsigned int cpu);
683683
extern u32 amd_get_nodes_per_socket(void);
684684
extern u32 amd_get_highest_perf(void);
685685
extern bool cpu_has_ibpb_brtype_microcode(void);
686+
extern void amd_clear_divider(void);
686687
#else
687688
static inline u32 amd_get_nodes_per_socket(void) { return 0; }
688689
static inline u32 amd_get_highest_perf(void) { return 0; }
689690
static inline bool cpu_has_ibpb_brtype_microcode(void) { return false; }
691+
static inline void amd_clear_divider(void) { }
690692
#endif
691693

692694
extern unsigned long arch_align_stack(unsigned long sp);

arch/x86/kernel/cpu/amd.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ static const int amd_zenbleed[] =
7575
AMD_MODEL_RANGE(0x17, 0x60, 0x0, 0x7f, 0xf),
7676
AMD_MODEL_RANGE(0x17, 0xa0, 0x0, 0xaf, 0xf));
7777

78+
static const int amd_div0[] =
79+
AMD_LEGACY_ERRATUM(AMD_MODEL_RANGE(0x17, 0x00, 0x0, 0x2f, 0xf),
80+
AMD_MODEL_RANGE(0x17, 0x50, 0x0, 0x5f, 0xf));
81+
7882
static bool cpu_has_amd_erratum(struct cpuinfo_x86 *cpu, const int *erratum)
7983
{
8084
int osvw_id = *erratum++;
@@ -1130,6 +1134,11 @@ static void init_amd(struct cpuinfo_x86 *c)
11301134
WARN_ON_ONCE(msr_set_bit(MSR_EFER, _EFER_AUTOIBRS));
11311135

11321136
zenbleed_check(c);
1137+
1138+
if (cpu_has_amd_erratum(c, amd_div0)) {
1139+
pr_notice_once("AMD Zen1 DIV0 bug detected. Disable SMT for full protection.\n");
1140+
setup_force_cpu_bug(X86_BUG_DIV0);
1141+
}
11331142
}
11341143

11351144
#ifdef CONFIG_X86_32
@@ -1309,3 +1318,13 @@ bool cpu_has_ibpb_brtype_microcode(void)
13091318
return false;
13101319
}
13111320
}
1321+
1322+
/*
1323+
* Issue a DIV 0/1 insn to clear any division data from previous DIV
1324+
* operations.
1325+
*/
1326+
void noinstr amd_clear_divider(void)
1327+
{
1328+
asm volatile(ALTERNATIVE("", "div %2\n\t", X86_BUG_DIV0)
1329+
:: "a" (0), "d" (0), "r" (1));
1330+
}

arch/x86/kernel/traps.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ DEFINE_IDTENTRY(exc_divide_error)
206206
{
207207
do_error_trap(regs, 0, "divide error", X86_TRAP_DE, SIGFPE,
208208
FPE_INTDIV, error_get_trap_addr(regs));
209+
210+
amd_clear_divider();
209211
}
210212

211213
DEFINE_IDTENTRY(exc_overflow)

0 commit comments

Comments
 (0)