Skip to content

Commit 2015ffa

Browse files
Lai Jiangshangregkh
authored andcommitted
x86/xen: Add xenpv_restore_regs_and_return_to_usermode()
[ Upstream commit 5c8f6a2 ] In the native case, PER_CPU_VAR(cpu_tss_rw + TSS_sp0) is the trampoline stack. But XEN pv doesn't use trampoline stack, so PER_CPU_VAR(cpu_tss_rw + TSS_sp0) is also the kernel stack. In that case, source and destination stacks are identical, which means that reusing swapgs_restore_regs_and_return_to_usermode() in XEN pv would cause %rsp to move up to the top of the kernel stack and leave the IRET frame below %rsp. This is dangerous as it can be corrupted if #NMI / #MC hit as either of these events occurring in the middle of the stack pushing would clobber data on the (original) stack. And, with XEN pv, swapgs_restore_regs_and_return_to_usermode() pushing the IRET frame on to the original address is useless and error-prone when there is any future attempt to modify the code. [ bp: Massage commit message. ] Fixes: 7f2590a ("x86/entry/64: Use a per-CPU trampoline stack for IDT entries") Signed-off-by: Lai Jiangshan <laijs@linux.alibaba.com> Signed-off-by: Borislav Petkov <bp@suse.de> Reviewed-by: Boris Ostrovsky <boris.ostrovsky@oracle.com> Link: https://lkml.kernel.org/r/20211126101209.8613-4-jiangshanlai@gmail.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 8b9279c commit 2015ffa

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎arch/x86/entry/entry_64.S‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,10 @@ SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
575575
ud2
576576
1:
577577
#endif
578+
#ifdef CONFIG_XEN_PV
579+
ALTERNATIVE "", "jmp xenpv_restore_regs_and_return_to_usermode", X86_FEATURE_XENPV
580+
#endif
581+
578582
POP_REGS pop_rdi=0
579583

580584
/*

‎arch/x86/xen/xen-asm.S‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include <linux/init.h>
2121
#include <linux/linkage.h>
22+
#include <../entry/calling.h>
2223

2324
/*
2425
* Enable events. This clears the event mask and tests the pending
@@ -235,6 +236,25 @@ SYM_CODE_START(xen_sysret64)
235236
jmp hypercall_iret
236237
SYM_CODE_END(xen_sysret64)
237238

239+
/*
240+
* XEN pv doesn't use trampoline stack, PER_CPU_VAR(cpu_tss_rw + TSS_sp0) is
241+
* also the kernel stack. Reusing swapgs_restore_regs_and_return_to_usermode()
242+
* in XEN pv would cause %rsp to move up to the top of the kernel stack and
243+
* leave the IRET frame below %rsp, which is dangerous to be corrupted if #NMI
244+
* interrupts. And swapgs_restore_regs_and_return_to_usermode() pushing the IRET
245+
* frame at the same address is useless.
246+
*/
247+
SYM_CODE_START(xenpv_restore_regs_and_return_to_usermode)
248+
UNWIND_HINT_REGS
249+
POP_REGS
250+
251+
/* stackleak_erase() can work safely on the kernel stack. */
252+
STACKLEAK_ERASE_NOCLOBBER
253+
254+
addq $8, %rsp /* skip regs->orig_ax */
255+
jmp xen_iret
256+
SYM_CODE_END(xenpv_restore_regs_and_return_to_usermode)
257+
238258
/*
239259
* Xen handles syscall callbacks much like ordinary exceptions, which
240260
* means we have:

0 commit comments

Comments
 (0)