Skip to content

Commit 18ec54f

Browse files
jpoimboeKAGA-KOKO
authored andcommitted
x86/speculation: Prepare entry code for Spectre v1 swapgs mitigations
Spectre v1 isn't only about array bounds checks. It can affect any conditional checks. The kernel entry code interrupt, exception, and NMI handlers all have conditional swapgs checks. Those may be problematic in the context of Spectre v1, as kernel code can speculatively run with a user GS. For example: if (coming from user space) swapgs mov %gs:<percpu_offset>, %reg mov (%reg), %reg1 When coming from user space, the CPU can speculatively skip the swapgs, and then do a speculative percpu load using the user GS value. So the user can speculatively force a read of any kernel value. If a gadget exists which uses the percpu value as an address in another load/store, then the contents of the kernel value may become visible via an L1 side channel attack. A similar attack exists when coming from kernel space. The CPU can speculatively do the swapgs, causing the user GS to get used for the rest of the speculative window. The mitigation is similar to a traditional Spectre v1 mitigation, except: a) index masking isn't possible; because the index (percpu offset) isn't user-controlled; and b) an lfence is needed in both the "from user" swapgs path and the "from kernel" non-swapgs path (because of the two attacks described above). The user entry swapgs paths already have SWITCH_TO_KERNEL_CR3, which has a CR3 write when PTI is enabled. Since CR3 writes are serializing, the lfences can be skipped in those cases. On the other hand, the kernel entry swapgs paths don't depend on PTI. To avoid unnecessary lfences for the user entry case, create two separate features for alternative patching: X86_FEATURE_FENCE_SWAPGS_USER X86_FEATURE_FENCE_SWAPGS_KERNEL Use these features in entry code to patch in lfences where needed. The features aren't enabled yet, so there's no functional change. Signed-off-by: Josh Poimboeuf <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Dave Hansen <[email protected]>
1 parent 222a21d commit 18ec54f

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

arch/x86/entry/calling.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,23 @@ For 32-bit we have the following conventions - kernel is built with
314314

315315
#endif
316316

317+
/*
318+
* Mitigate Spectre v1 for conditional swapgs code paths.
319+
*
320+
* FENCE_SWAPGS_USER_ENTRY is used in the user entry swapgs code path, to
321+
* prevent a speculative swapgs when coming from kernel space.
322+
*
323+
* FENCE_SWAPGS_KERNEL_ENTRY is used in the kernel entry non-swapgs code path,
324+
* to prevent the swapgs from getting speculatively skipped when coming from
325+
* user space.
326+
*/
327+
.macro FENCE_SWAPGS_USER_ENTRY
328+
ALTERNATIVE "", "lfence", X86_FEATURE_FENCE_SWAPGS_USER
329+
.endm
330+
.macro FENCE_SWAPGS_KERNEL_ENTRY
331+
ALTERNATIVE "", "lfence", X86_FEATURE_FENCE_SWAPGS_KERNEL
332+
.endm
333+
317334
.macro STACKLEAK_ERASE_NOCLOBBER
318335
#ifdef CONFIG_GCC_PLUGIN_STACKLEAK
319336
PUSH_AND_CLEAR_REGS

arch/x86/entry/entry_64.S

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ ENTRY(interrupt_entry)
519519
testb $3, CS-ORIG_RAX+8(%rsp)
520520
jz 1f
521521
SWAPGS
522-
522+
FENCE_SWAPGS_USER_ENTRY
523523
/*
524524
* Switch to the thread stack. The IRET frame and orig_ax are
525525
* on the stack, as well as the return address. RDI..R12 are
@@ -549,8 +549,10 @@ ENTRY(interrupt_entry)
549549
UNWIND_HINT_FUNC
550550

551551
movq (%rdi), %rdi
552+
jmpq 2f
552553
1:
553-
554+
FENCE_SWAPGS_KERNEL_ENTRY
555+
2:
554556
PUSH_AND_CLEAR_REGS save_ret=1
555557
ENCODE_FRAME_POINTER 8
556558

@@ -1221,6 +1223,13 @@ ENTRY(paranoid_entry)
12211223
*/
12221224
SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg=%rax save_reg=%r14
12231225

1226+
/*
1227+
* The above SAVE_AND_SWITCH_TO_KERNEL_CR3 macro doesn't do an
1228+
* unconditional CR3 write, even in the PTI case. So do an lfence
1229+
* to prevent GS speculation, regardless of whether PTI is enabled.
1230+
*/
1231+
FENCE_SWAPGS_KERNEL_ENTRY
1232+
12241233
ret
12251234
END(paranoid_entry)
12261235

@@ -1271,6 +1280,7 @@ ENTRY(error_entry)
12711280
* from user mode due to an IRET fault.
12721281
*/
12731282
SWAPGS
1283+
FENCE_SWAPGS_USER_ENTRY
12741284
/* We have user CR3. Change to kernel CR3. */
12751285
SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
12761286

@@ -1292,6 +1302,8 @@ ENTRY(error_entry)
12921302
CALL_enter_from_user_mode
12931303
ret
12941304

1305+
.Lerror_entry_done_lfence:
1306+
FENCE_SWAPGS_KERNEL_ENTRY
12951307
.Lerror_entry_done:
12961308
TRACE_IRQS_OFF
12971309
ret
@@ -1310,14 +1322,15 @@ ENTRY(error_entry)
13101322
cmpq %rax, RIP+8(%rsp)
13111323
je .Lbstep_iret
13121324
cmpq $.Lgs_change, RIP+8(%rsp)
1313-
jne .Lerror_entry_done
1325+
jne .Lerror_entry_done_lfence
13141326

13151327
/*
13161328
* hack: .Lgs_change can fail with user gsbase. If this happens, fix up
13171329
* gsbase and proceed. We'll fix up the exception and land in
13181330
* .Lgs_change's error handler with kernel gsbase.
13191331
*/
13201332
SWAPGS
1333+
FENCE_SWAPGS_USER_ENTRY
13211334
SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
13221335
jmp .Lerror_entry_done
13231336

@@ -1332,6 +1345,7 @@ ENTRY(error_entry)
13321345
* gsbase and CR3. Switch to kernel gsbase and CR3:
13331346
*/
13341347
SWAPGS
1348+
FENCE_SWAPGS_USER_ENTRY
13351349
SWITCH_TO_KERNEL_CR3 scratch_reg=%rax
13361350

13371351
/*
@@ -1423,6 +1437,7 @@ ENTRY(nmi)
14231437

14241438
swapgs
14251439
cld
1440+
FENCE_SWAPGS_USER_ENTRY
14261441
SWITCH_TO_KERNEL_CR3 scratch_reg=%rdx
14271442
movq %rsp, %rdx
14281443
movq PER_CPU_VAR(cpu_current_top_of_stack), %rsp

arch/x86/include/asm/cpufeatures.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@
281281
#define X86_FEATURE_CQM_OCCUP_LLC (11*32+ 1) /* LLC occupancy monitoring */
282282
#define X86_FEATURE_CQM_MBM_TOTAL (11*32+ 2) /* LLC Total MBM monitoring */
283283
#define X86_FEATURE_CQM_MBM_LOCAL (11*32+ 3) /* LLC Local MBM monitoring */
284+
#define X86_FEATURE_FENCE_SWAPGS_USER (11*32+ 4) /* "" LFENCE in user entry SWAPGS path */
285+
#define X86_FEATURE_FENCE_SWAPGS_KERNEL (11*32+ 5) /* "" LFENCE in kernel entry SWAPGS path */
284286

285287
/* Intel-defined CPU features, CPUID level 0x00000007:1 (EAX), word 12 */
286288
#define X86_FEATURE_AVX512_BF16 (12*32+ 5) /* AVX512 BFLOAT16 instructions */

0 commit comments

Comments
 (0)