Skip to content

Commit c07e455

Browse files
Lai Jiangshansuryasaimadhu
authored andcommitted
x86/entry: Add a fence for kernel entry SWAPGS in paranoid_entry()
Commit 18ec54f ("x86/speculation: Prepare entry code for Spectre v1 swapgs mitigations") added FENCE_SWAPGS_{KERNEL|USER}_ENTRY for conditional SWAPGS. In paranoid_entry(), it uses only FENCE_SWAPGS_KERNEL_ENTRY for both branches. This is because the fence is required for both cases since the CR3 write is conditional even when PTI is enabled. But 96b2371 ("x86/entry/64: Switch CR3 before SWAPGS in paranoid entry") changed the order of SWAPGS and the CR3 write. And it missed the needed FENCE_SWAPGS_KERNEL_ENTRY for the user gsbase case. Add it back by changing the branches so that FENCE_SWAPGS_KERNEL_ENTRY can cover both branches. [ bp: Massage, fix typos, remove obsolete comment while at it. ] Fixes: 96b2371 ("x86/entry/64: Switch CR3 before SWAPGS in paranoid entry") Signed-off-by: Lai Jiangshan <laijs@linux.alibaba.com> Signed-off-by: Borislav Petkov <bp@suse.de> Link: https://lkml.kernel.org/r/20211126101209.8613-2-jiangshanlai@gmail.com
1 parent 1d5379d commit c07e455

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

arch/x86/entry/entry_64.S

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -890,28 +890,22 @@ SYM_CODE_START_LOCAL(paranoid_entry)
890890
.Lparanoid_entry_checkgs:
891891
/* EBX = 1 -> kernel GSBASE active, no restore required */
892892
movl $1, %ebx
893+
893894
/*
894895
* The kernel-enforced convention is a negative GSBASE indicates
895896
* a kernel value. No SWAPGS needed on entry and exit.
896897
*/
897898
movl $MSR_GS_BASE, %ecx
898899
rdmsr
899900
testl %edx, %edx
900-
jns .Lparanoid_entry_swapgs
901-
ret
901+
js .Lparanoid_kernel_gsbase
902902

903-
.Lparanoid_entry_swapgs:
903+
/* EBX = 0 -> SWAPGS required on exit */
904+
xorl %ebx, %ebx
904905
swapgs
906+
.Lparanoid_kernel_gsbase:
905907

906-
/*
907-
* The above SAVE_AND_SWITCH_TO_KERNEL_CR3 macro doesn't do an
908-
* unconditional CR3 write, even in the PTI case. So do an lfence
909-
* to prevent GS speculation, regardless of whether PTI is enabled.
910-
*/
911908
FENCE_SWAPGS_KERNEL_ENTRY
912-
913-
/* EBX = 0 -> SWAPGS required on exit */
914-
xorl %ebx, %ebx
915909
ret
916910
SYM_CODE_END(paranoid_entry)
917911

0 commit comments

Comments
 (0)