Skip to content

Commit 3c75017

Browse files
pa1guptahansendc
authored andcommitted
x86/entry_64: Add VERW just before userspace transition
Mitigation for MDS is to use VERW instruction to clear any secrets in CPU Buffers. Any memory accesses after VERW execution can still remain in CPU buffers. It is safer to execute VERW late in return to user path to minimize the window in which kernel data can end up in CPU buffers. There are not many kernel secrets to be had after SWITCH_TO_USER_CR3. Add support for deploying VERW mitigation after user register state is restored. This helps minimize the chances of kernel data ending up into CPU buffers after executing VERW. Note that the mitigation at the new location is not yet enabled. Corner case not handled ======================= Interrupts returning to kernel don't clear CPUs buffers since the exit-to-user path is expected to do that anyways. But, there could be a case when an NMI is generated in kernel after the exit-to-user path has cleared the buffers. This case is not handled and NMI returning to kernel don't clear CPU buffers because: 1. It is rare to get an NMI after VERW, but before returning to userspace. 2. For an unprivileged user, there is no known way to make that NMI less rare or target it. 3. It would take a large number of these precisely-timed NMIs to mount an actual attack. There's presumably not enough bandwidth. 4. The NMI in question occurs after a VERW, i.e. when user state is restored and most interesting data is already scrubbed. Whats left is only the data that NMI touches, and that may or may not be of any interest. Suggested-by: Dave Hansen <dave.hansen@intel.com> 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-2-a6216d83edb7%40linux.intel.com
1 parent baf8361 commit 3c75017

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

arch/x86/entry/entry_64.S

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ syscall_return_via_sysret:
161161
SYM_INNER_LABEL(entry_SYSRETQ_unsafe_stack, SYM_L_GLOBAL)
162162
ANNOTATE_NOENDBR
163163
swapgs
164+
CLEAR_CPU_BUFFERS
164165
sysretq
165166
SYM_INNER_LABEL(entry_SYSRETQ_end, SYM_L_GLOBAL)
166167
ANNOTATE_NOENDBR
@@ -573,6 +574,7 @@ SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
573574

574575
.Lswapgs_and_iret:
575576
swapgs
577+
CLEAR_CPU_BUFFERS
576578
/* Assert that the IRET frame indicates user mode. */
577579
testb $3, 8(%rsp)
578580
jnz .Lnative_iret
@@ -723,6 +725,8 @@ native_irq_return_ldt:
723725
*/
724726
popq %rax /* Restore user RAX */
725727

728+
CLEAR_CPU_BUFFERS
729+
726730
/*
727731
* RSP now points to an ordinary IRET frame, except that the page
728732
* is read-only and RSP[31:16] are preloaded with the userspace
@@ -1449,6 +1453,12 @@ nmi_restore:
14491453
std
14501454
movq $0, 5*8(%rsp) /* clear "NMI executing" */
14511455

1456+
/*
1457+
* Skip CLEAR_CPU_BUFFERS here, since it only helps in rare cases like
1458+
* NMI in kernel after user state is restored. For an unprivileged user
1459+
* these conditions are hard to meet.
1460+
*/
1461+
14521462
/*
14531463
* iretq reads the "iret" frame and exits the NMI stack in a
14541464
* single instruction. We are returning to kernel mode, so this
@@ -1466,6 +1476,7 @@ SYM_CODE_START(entry_SYSCALL32_ignore)
14661476
UNWIND_HINT_END_OF_STACK
14671477
ENDBR
14681478
mov $-ENOSYS, %eax
1479+
CLEAR_CPU_BUFFERS
14691480
sysretl
14701481
SYM_CODE_END(entry_SYSCALL32_ignore)
14711482

arch/x86/entry/entry_64_compat.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ SYM_INNER_LABEL(entry_SYSRETL_compat_unsafe_stack, SYM_L_GLOBAL)
270270
xorl %r9d, %r9d
271271
xorl %r10d, %r10d
272272
swapgs
273+
CLEAR_CPU_BUFFERS
273274
sysretl
274275
SYM_INNER_LABEL(entry_SYSRETL_compat_end, SYM_L_GLOBAL)
275276
ANNOTATE_NOENDBR

0 commit comments

Comments
 (0)