Skip to content

Commit 2fdbcf7

Browse files
committed
Merge tag 'x86-entry-2024-01-08' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 entry updates from Ingo Molnar: - Optimize common_interrupt_return() - Harden the return-to-user code by making a CONFIG_DEBUG_ENTRY=y check unconditional & moving it closer to the IRET. * tag 'x86-entry-2024-01-08' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/entry: Harden return-to-user x86/entry: Optimize common_interrupt_return()
2 parents 33677ae + 1e4d300 commit 2fdbcf7

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

arch/x86/entry/calling.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ For 32-bit we have the following conventions - kernel is built with
175175
#define THIS_CPU_user_pcid_flush_mask \
176176
PER_CPU_VAR(cpu_tlbstate) + TLB_STATE_user_pcid_flush_mask
177177

178-
.macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req
179-
ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
178+
.macro SWITCH_TO_USER_CR3 scratch_reg:req scratch_reg2:req
180179
mov %cr3, \scratch_reg
181180

182181
ALTERNATIVE "jmp .Lwrcr3_\@", "", X86_FEATURE_PCID
@@ -206,13 +205,20 @@ For 32-bit we have the following conventions - kernel is built with
206205
/* Flip the PGD to the user version */
207206
orq $(PTI_USER_PGTABLE_MASK), \scratch_reg
208207
mov \scratch_reg, %cr3
208+
.endm
209+
210+
.macro SWITCH_TO_USER_CR3_NOSTACK scratch_reg:req scratch_reg2:req
211+
ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
212+
SWITCH_TO_USER_CR3 \scratch_reg \scratch_reg2
209213
.Lend_\@:
210214
.endm
211215

212216
.macro SWITCH_TO_USER_CR3_STACK scratch_reg:req
217+
ALTERNATIVE "jmp .Lend_\@", "", X86_FEATURE_PTI
213218
pushq %rax
214-
SWITCH_TO_USER_CR3_NOSTACK scratch_reg=\scratch_reg scratch_reg2=%rax
219+
SWITCH_TO_USER_CR3 scratch_reg=\scratch_reg scratch_reg2=%rax
215220
popq %rax
221+
.Lend_\@:
216222
.endm
217223

218224
.macro SAVE_AND_SWITCH_TO_KERNEL_CR3 scratch_reg:req save_reg:req

arch/x86/entry/entry_64.S

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -559,17 +559,27 @@ __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
565+
#ifdef CONFIG_PAGE_TABLE_ISOLATION
566+
ALTERNATIVE "", "jmp .Lpti_restore_regs_and_return_to_usermode", X86_FEATURE_PTI
567+
#endif
568+
569+
STACKLEAK_ERASE
570+
POP_REGS
571+
add $8, %rsp /* orig_ax */
572+
UNWIND_HINT_IRET_REGS
573+
574+
.Lswapgs_and_iret:
575+
swapgs
576+
/* Assert that the IRET frame indicates user mode. */
577+
testb $3, 8(%rsp)
578+
jnz .Lnative_iret
579+
ud2
572580

581+
#ifdef CONFIG_PAGE_TABLE_ISOLATION
582+
.Lpti_restore_regs_and_return_to_usermode:
573583
POP_REGS pop_rdi=0
574584

575585
/*
@@ -596,13 +606,14 @@ SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
596606
*/
597607
STACKLEAK_ERASE_NOCLOBBER
598608

599-
SWITCH_TO_USER_CR3_STACK scratch_reg=%rdi
609+
push %rax
610+
SWITCH_TO_USER_CR3 scratch_reg=%rdi scratch_reg2=%rax
611+
pop %rax
600612

601613
/* Restore RDI. */
602614
popq %rdi
603-
swapgs
604-
jmp .Lnative_iret
605-
615+
jmp .Lswapgs_and_iret
616+
#endif
606617

607618
SYM_INNER_LABEL(restore_regs_and_return_to_kernel, SYM_L_GLOBAL)
608619
#ifdef CONFIG_DEBUG_ENTRY

0 commit comments

Comments
 (0)