Skip to content

Commit baf8361

Browse files
pa1guptahansendc
authored andcommitted
x86/bugs: Add asm helpers for executing VERW
MDS mitigation requires clearing the CPU buffers before returning to user. This needs to be done late in the exit-to-user path. Current location of VERW leaves a possibility of kernel data ending up in CPU buffers for memory accesses done after VERW such as: 1. Kernel data accessed by an NMI between VERW and return-to-user can remain in CPU buffers since NMI returning to kernel does not execute VERW to clear CPU buffers. 2. Alyssa reported that after VERW is executed, CONFIG_GCC_PLUGIN_STACKLEAK=y scrubs the stack used by a system call. Memory accesses during stack scrubbing can move kernel stack contents into CPU buffers. 3. When caller saved registers are restored after a return from function executing VERW, the kernel stack accesses can remain in CPU buffers(since they occur after VERW). To fix this VERW needs to be moved very late in exit-to-user path. In preparation for moving VERW to entry/exit asm code, create macros that can be used in asm. Also make VERW patching depend on a new feature flag X86_FEATURE_CLEAR_CPU_BUF. Reported-by: Alyssa Milburn <alyssa.milburn@intel.com> Suggested-by: Andrew Cooper <andrew.cooper3@citrix.com> Suggested-by: Peter Zijlstra <peterz@infradead.org> Signed-off-by: Pawan Gupta <pawan.kumar.gupta@linux.intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Link: https://lore.kernel.org/all/20240213-delay-verw-v8-1-a6216d83edb7%40linux.intel.com
1 parent b401b62 commit baf8361

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

arch/x86/entry/entry.S

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#include <linux/export.h>
77
#include <linux/linkage.h>
88
#include <asm/msr-index.h>
9+
#include <asm/unwind_hints.h>
10+
#include <asm/segment.h>
11+
#include <asm/cache.h>
912

1013
.pushsection .noinstr.text, "ax"
1114

@@ -20,3 +23,23 @@ SYM_FUNC_END(entry_ibpb)
2023
EXPORT_SYMBOL_GPL(entry_ibpb);
2124

2225
.popsection
26+
27+
/*
28+
* Define the VERW operand that is disguised as entry code so that
29+
* it can be referenced with KPTI enabled. This ensure VERW can be
30+
* used late in exit-to-user path after page tables are switched.
31+
*/
32+
.pushsection .entry.text, "ax"
33+
34+
.align L1_CACHE_BYTES, 0xcc
35+
SYM_CODE_START_NOALIGN(mds_verw_sel)
36+
UNWIND_HINT_UNDEFINED
37+
ANNOTATE_NOENDBR
38+
.word __KERNEL_DS
39+
.align L1_CACHE_BYTES, 0xcc
40+
SYM_CODE_END(mds_verw_sel);
41+
/* For KVM */
42+
EXPORT_SYMBOL_GPL(mds_verw_sel);
43+
44+
.popsection
45+

arch/x86/include/asm/cpufeatures.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
#define X86_FEATURE_SYSENTER32 ( 3*32+15) /* "" sysenter in IA32 userspace */
9696
#define X86_FEATURE_REP_GOOD ( 3*32+16) /* REP microcode works well */
9797
#define X86_FEATURE_AMD_LBR_V2 ( 3*32+17) /* AMD Last Branch Record Extension Version 2 */
98-
/* FREE, was #define X86_FEATURE_LFENCE_RDTSC ( 3*32+18) "" LFENCE synchronizes RDTSC */
98+
#define X86_FEATURE_CLEAR_CPU_BUF ( 3*32+18) /* "" Clear CPU buffers using VERW */
9999
#define X86_FEATURE_ACC_POWER ( 3*32+19) /* AMD Accumulated Power Mechanism */
100100
#define X86_FEATURE_NOPL ( 3*32+20) /* The NOPL (0F 1F) instructions */
101101
#define X86_FEATURE_ALWAYS ( 3*32+21) /* "" Always-present feature */

arch/x86/include/asm/nospec-branch.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,17 @@
315315
#endif
316316
.endm
317317

318+
/*
319+
* Macro to execute VERW instruction that mitigate transient data sampling
320+
* attacks such as MDS. On affected systems a microcode update overloaded VERW
321+
* instruction to also clear the CPU buffers. VERW clobbers CFLAGS.ZF.
322+
*
323+
* Note: Only the memory operand variant of VERW clears the CPU buffers.
324+
*/
325+
.macro CLEAR_CPU_BUFFERS
326+
ALTERNATIVE "", __stringify(verw _ASM_RIP(mds_verw_sel)), X86_FEATURE_CLEAR_CPU_BUF
327+
.endm
328+
318329
#else /* __ASSEMBLY__ */
319330

320331
#define ANNOTATE_RETPOLINE_SAFE \
@@ -536,6 +547,8 @@ DECLARE_STATIC_KEY_FALSE(switch_mm_cond_l1d_flush);
536547

537548
DECLARE_STATIC_KEY_FALSE(mmio_stale_data_clear);
538549

550+
extern u16 mds_verw_sel;
551+
539552
#include <asm/segment.h>
540553

541554
/**

0 commit comments

Comments
 (0)