Skip to content

Commit 1e4d300

Browse files
Peter Zijlstraingomolnar
authored andcommitted
x86/entry: Harden return-to-user
Make the CONFIG_DEBUG_ENTRY=y check that validates CS is a user segment unconditional and move it nearer to IRET. PRE: 140,026,608 cycles:k ( +- 0.01% ) 236,696,176 instructions:k # 1.69 insn per cycle ( +- 0.00% ) POST: 139,957,681 cycles:k ( +- 0.01% ) 236,681,819 instructions:k # 1.69 insn per cycle ( +- 0.00% ) (this is with --repeat 100 and the run-to-run variance is bigger than the difference shown) Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Ingo Molnar <mingo@kernel.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Link: https://lore.kernel.org/r/20231120143626.753200755@infradead.org
1 parent c516213 commit 1e4d300

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

arch/x86/entry/entry_64.S

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -559,13 +559,6 @@ __irqentry_text_end:
559559
SYM_CODE_START_LOCAL(common_interrupt_return)
560560
SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
561561
IBRS_EXIT
562-
#ifdef CONFIG_DEBUG_ENTRY
563-
/* Assert that pt_regs indicates user mode. */
564-
testb $3, CS(%rsp)
565-
jnz 1f
566-
ud2
567-
1:
568-
#endif
569562
#ifdef CONFIG_XEN_PV
570563
ALTERNATIVE "", "jmp xenpv_restore_regs_and_return_to_usermode", X86_FEATURE_XENPV
571564
#endif
@@ -576,8 +569,14 @@ SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
576569
STACKLEAK_ERASE
577570
POP_REGS
578571
add $8, %rsp /* orig_ax */
572+
UNWIND_HINT_IRET_REGS
573+
574+
.Lswapgs_and_iret:
579575
swapgs
580-
jmp .Lnative_iret
576+
/* Assert that the IRET frame indicates user mode. */
577+
testb $3, 8(%rsp)
578+
jnz .Lnative_iret
579+
ud2
581580

582581
#ifdef CONFIG_PAGE_TABLE_ISOLATION
583582
.Lpti_restore_regs_and_return_to_usermode:
@@ -613,8 +612,7 @@ SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
613612

614613
/* Restore RDI. */
615614
popq %rdi
616-
swapgs
617-
jmp .Lnative_iret
615+
jmp .Lswapgs_and_iret
618616
#endif
619617

620618
SYM_INNER_LABEL(restore_regs_and_return_to_kernel, SYM_L_GLOBAL)

0 commit comments

Comments
 (0)