lkml.org 
[lkml]   [2021]   [Nov]   [26]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    Patch in this message
    /
    From
    Subject[PATCH V6 03/49] x86/xen: Add xenpv_restore_regs_and_return_to_usermode()
    Date
    From: Lai Jiangshan <laijs@linux.alibaba.com>

    While 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. Hence source
    and destination stacks are identical in that case, which means 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, which is dangerous to 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, when 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.

    Fixes: 7f2590a110b8 ("x86/entry/64: Use a per-CPU trampoline stack for IDT entries")
    Cc: Jan Beulich <jbeulich@suse.com>
    Cc: Thomas Gleixner <tglx@linutronix.de>
    Cc: Juergen Gross <jgross@suse.com>
    Cc: Peter Anvin <hpa@zytor.com>
    Cc: xen-devel@lists.xenproject.org>
    Reviewed-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
    Signed-off-by: Lai Jiangshan <laijs@linux.alibaba.com>
    ---
    arch/x86/entry/entry_64.S | 4 ++++
    arch/x86/xen/xen-asm.S | 20 ++++++++++++++++++++
    2 files changed, 24 insertions(+)

    diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
    index 4967edded48d..68e697acefac 100644
    --- a/arch/x86/entry/entry_64.S
    +++ b/arch/x86/entry/entry_64.S
    @@ -574,6 +574,10 @@ SYM_INNER_LABEL(swapgs_restore_regs_and_return_to_usermode, SYM_L_GLOBAL)
    ud2
    1:
    #endif
    +#ifdef CONFIG_XEN_PV
    + ALTERNATIVE "", "jmp xenpv_restore_regs_and_return_to_usermode", X86_FEATURE_XENPV
    +#endif
    +
    POP_REGS pop_rdi=0

    /*
    diff --git a/arch/x86/xen/xen-asm.S b/arch/x86/xen/xen-asm.S
    index 220dd9678494..444d824775f6 100644
    --- a/arch/x86/xen/xen-asm.S
    +++ b/arch/x86/xen/xen-asm.S
    @@ -20,6 +20,7 @@

    #include <linux/init.h>
    #include <linux/linkage.h>
    +#include <../entry/calling.h>

    .pushsection .noinstr.text, "ax"
    /*
    @@ -192,6 +193,25 @@ SYM_CODE_START(xen_iret)
    jmp hypercall_iret
    SYM_CODE_END(xen_iret)

    +/*
    + * XEN pv doesn't use trampoline stack, PER_CPU_VAR(cpu_tss_rw + TSS_sp0) is
    + * also the kernel stack. 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, which is dangerous to be corrupted if #NMI
    + * interrupts. And swapgs_restore_regs_and_return_to_usermode() pushing the IRET
    + * frame at the same address is useless.
    + */
    +SYM_CODE_START(xenpv_restore_regs_and_return_to_usermode)
    + UNWIND_HINT_REGS
    + POP_REGS
    +
    + /* stackleak_erase() can work safely on the kernel stack. */
    + STACKLEAK_ERASE_NOCLOBBER
    +
    + addq $8, %rsp /* skip regs->orig_ax */
    + jmp xen_iret
    +SYM_CODE_END(xenpv_restore_regs_and_return_to_usermode)
    +
    /*
    * Xen handles syscall callbacks much like ordinary exceptions, which
    * means we have:
    --
    2.19.1.6.gb485710b
    \
     
     \ /
      Last update: 2021-11-26 11:23    [W:4.332 / U:0.236 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site