All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH for-4.16] x86/shstk: Fix use of shadow stacks with XPTI active
@ 2021-11-02 14:39 Andrew Cooper
  2021-11-02 15:14 ` Jan Beulich
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Cooper @ 2021-11-02 14:39 UTC (permalink / raw)
  To: Xen-devel; +Cc: Andrew Cooper, Jan Beulich, Roger Pau Monné, Wei Liu

The call to setup_cpu_root_pgt(0) in smp_prepare_cpus() is too early.  It
clones the BSP's stack while the .data mapping is still in use, causing all
mappings to be fully read read/write (and with no guard pages either).  This
ultimately causes #DF when trying to enter the dom0 kernel for the first time.

Defer setting up BSPs XPTI pagetable until reinit_bsp_stack() after we've set
up proper shadow stack permissions.

Fixes: 60016604739b ("x86/shstk: Rework the stack layout to support shadow stacks")
Fixes: b60ab42db2f0 ("x86/shstk: Activate Supervisor Shadow Stacks")
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Jan Beulich <JBeulich@suse.com>
CC: Roger Pau Monné <roger.pau@citrix.com>
CC: Wei Liu <wl@xen.org>

For 4.16.  This is a rare configuration.  Real hardware supporting CET-SS is
either AMD, or fixed to Meltdown, so doesn't turn on XPTI by default.

The bug can be triggered either by booting real CET-SS hardware with `xpti` on
the cmdline, or booting Xen in a VM where "fixed to meltdown" isn't
advertised.  The result when things go wrong is an unconditional crash.

Risks are minimal - all the change is doing is reordering some actions during
boot.  The shadow stack specific aspects are hard for people to independently
verify, owing to the fact that CET-SS is only on current-generation CPUs, but
I have an example sitting on my desk which is how I discovered this.

However, the paths altered are tested thoroughly by OSSTest on every single
host boot, in a variety of combinations due to the selection of hardware.
---
 xen/arch/x86/setup.c   | 5 +++++
 xen/arch/x86/smpboot.c | 9 ++-------
 xen/include/xen/smp.h  | 1 +
 3 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/xen/arch/x86/setup.c b/xen/arch/x86/setup.c
index b101565f1431..fea86530f9f2 100644
--- a/xen/arch/x86/setup.c
+++ b/xen/arch/x86/setup.c
@@ -666,6 +666,7 @@ static void noreturn init_done(void)
 static void __init noreturn reinit_bsp_stack(void)
 {
     unsigned long *stack = (void*)(get_stack_bottom() & ~(STACK_SIZE - 1));
+    int rc;
 
     /* Update TSS and ISTs */
     load_system_tables();
@@ -676,6 +677,10 @@ static void __init noreturn reinit_bsp_stack(void)
     stack_base[0] = stack;
     memguard_guard_stack(stack);
 
+    rc = setup_cpu_root_pgt(0);
+    if ( rc )
+        panic("Error %d setting up PV root page table\n", rc);
+
     if ( IS_ENABLED(CONFIG_XEN_SHSTK) && cpu_has_xen_shstk )
     {
         wrmsrl(MSR_PL0_SSP,
diff --git a/xen/arch/x86/smpboot.c b/xen/arch/x86/smpboot.c
index 0dce1ae87210..329cfdb6c9f6 100644
--- a/xen/arch/x86/smpboot.c
+++ b/xen/arch/x86/smpboot.c
@@ -821,7 +821,7 @@ static root_pgentry_t common_pgt;
 
 extern const char _stextentry[], _etextentry[];
 
-static int setup_cpu_root_pgt(unsigned int cpu)
+int setup_cpu_root_pgt(unsigned int cpu)
 {
     root_pgentry_t *rpt;
     unsigned int off;
@@ -1138,8 +1138,6 @@ static struct notifier_block cpu_smpboot_nfb = {
 
 void __init smp_prepare_cpus(void)
 {
-    int rc;
-
     register_cpu_notifier(&cpu_smpboot_nfb);
 
     mtrr_aps_sync_begin();
@@ -1153,10 +1151,7 @@ void __init smp_prepare_cpus(void)
 
     stack_base[0] = (void *)((unsigned long)stack_start & ~(STACK_SIZE - 1));
 
-    rc = setup_cpu_root_pgt(0);
-    if ( rc )
-        panic("Error %d setting up PV root page table\n", rc);
-    if ( per_cpu(root_pgt, 0) )
+    if ( opt_xpti_hwdom || opt_xpti_domu )
     {
         get_cpu_info()->pv_cr3 = 0;
 
diff --git a/xen/include/xen/smp.h b/xen/include/xen/smp.h
index d5a3644611db..0a9219173f0f 100644
--- a/xen/include/xen/smp.h
+++ b/xen/include/xen/smp.h
@@ -70,5 +70,6 @@ int alloc_cpu_id(void);
 extern void *stack_base[NR_CPUS];
 
 void initialize_cpu_data(unsigned int cpu);
+int setup_cpu_root_pgt(unsigned int cpu);
 
 #endif /* __XEN_SMP_H__ */
-- 
2.11.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH for-4.16] x86/shstk: Fix use of shadow stacks with XPTI active
  2021-11-02 14:39 [PATCH for-4.16] x86/shstk: Fix use of shadow stacks with XPTI active Andrew Cooper
@ 2021-11-02 15:14 ` Jan Beulich
  2021-11-02 15:53   ` Ian Jackson
  0 siblings, 1 reply; 3+ messages in thread
From: Jan Beulich @ 2021-11-02 15:14 UTC (permalink / raw)
  To: Andrew Cooper; +Cc: Roger Pau Monné, Wei Liu, Xen-devel

On 02.11.2021 15:39, Andrew Cooper wrote:
> The call to setup_cpu_root_pgt(0) in smp_prepare_cpus() is too early.  It
> clones the BSP's stack while the .data mapping is still in use, causing all
> mappings to be fully read read/write (and with no guard pages either).  This
> ultimately causes #DF when trying to enter the dom0 kernel for the first time.
> 
> Defer setting up BSPs XPTI pagetable until reinit_bsp_stack() after we've set
> up proper shadow stack permissions.
> 
> Fixes: 60016604739b ("x86/shstk: Rework the stack layout to support shadow stacks")
> Fixes: b60ab42db2f0 ("x86/shstk: Activate Supervisor Shadow Stacks")
> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>

Reviewed-by: Jan Beulich <jbeulich@suse.com>



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH for-4.16] x86/shstk: Fix use of shadow stacks with XPTI active
  2021-11-02 15:14 ` Jan Beulich
@ 2021-11-02 15:53   ` Ian Jackson
  0 siblings, 0 replies; 3+ messages in thread
From: Ian Jackson @ 2021-11-02 15:53 UTC (permalink / raw)
  To: Jan Beulich; +Cc: Andrew Cooper, Roger Pau Monné, Wei Liu, Xen-devel

Jan Beulich writes ("Re: [PATCH for-4.16] x86/shstk: Fix use of shadow stacks with XPTI active"):
> On 02.11.2021 15:39, Andrew Cooper wrote:
> > The call to setup_cpu_root_pgt(0) in smp_prepare_cpus() is too early.  It
> > clones the BSP's stack while the .data mapping is still in use, causing all
> > mappings to be fully read read/write (and with no guard pages either).  This
> > ultimately causes #DF when trying to enter the dom0 kernel for the first time.
> > 
> > Defer setting up BSPs XPTI pagetable until reinit_bsp_stack() after we've set
> > up proper shadow stack permissions.
> > 
> > Fixes: 60016604739b ("x86/shstk: Rework the stack layout to support shadow stacks")
> > Fixes: b60ab42db2f0 ("x86/shstk: Activate Supervisor Shadow Stacks")
> > Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
> 
> Reviewed-by: Jan Beulich <jbeulich@suse.com>

Release-Acked-by: Ian Jackson <iwj@xenproject.org>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-11-02 15:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-02 14:39 [PATCH for-4.16] x86/shstk: Fix use of shadow stacks with XPTI active Andrew Cooper
2021-11-02 15:14 ` Jan Beulich
2021-11-02 15:53   ` Ian Jackson

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.