Skip to contentSkip to conversation

x86/smpboot: Unconditionally call memguard_unguard_stack() in cpu_smpboot_free()

PATCH In review Andrew Cooper 3 participants 5 messages Oct 5, 2020 – Oct 13, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/smpboot: Unconditionally call memguard_unguard_stack() in cpu_smpboot_free()

For simplicity between various configuration, Xen always uses shadow stack mappings (Read-only + Dirty) for the guard page, irrespective of whether CET-SS is enabled.

memguard_guard_stack() writes shadow stack tokens with plain writes. This is necessary to configure the BSP shadow stack correctly, and cannot be implemented with WRSS.

Therefore, unconditionally call memguard_unguard_stack() to return the mappings to fully writeable, so a subsequent call to memguard_guard_stack() will succeed.

Fixes 91d26ed304f ("x86/shstk: Create shadow stacks")

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

This can more easily be demonstrated with CPU hotplug than S3, and the absence of bug reports goes to show how rarely hotplug is used.

---
 xen/arch/x86/smpboot.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/smpboot.c +3 -3
index 5708573c41..c193cc0fb8 100644
@@ -971,16 +971,16 @@ static void cpu_smpboot_free(unsigned int cpu, bool remove)
     if ( IS_ENABLED(CONFIG_PV32) )
         FREE_XENHEAP_PAGE(per_cpu(compat_gdt, cpu));
 
+    if ( stack_base[cpu] )
+        memguard_unguard_stack(stack_base[cpu]);
+
     if ( remove )
     {
         FREE_XENHEAP_PAGE(per_cpu(gdt, cpu));
         FREE_XENHEAP_PAGE(idt_tables[cpu]);
 
         if ( stack_base[cpu] )
-        {
-            memguard_unguard_stack(stack_base[cpu]);
             FREE_XENHEAP_PAGES(stack_base[cpu], STACK_ORDER);
-        }
     }
 }
 
Signature · 1 line
2.11.0
Tested-by on v1 1/1
#1 lore raw
Quoted 8 lines · Andrew Cooper

For simplicity between various configuration, Xen always uses shadow stack mappings (Read-only + Dirty) for the guard page, irrespective of whether CET-SS is enabled.

memguard_guard_stack() writes shadow stack tokens with plain writes. This is necessary to configure the BSP shadow stack correctly, and cannot be implemented with WRSS.

Quoted 7 lines

Therefore, unconditionally call memguard_unguard_stack() to return the mappings to fully writeable, so a subsequent call to memguard_guard_stack() will succeed.

Fixes 91d26ed304f ("x86/shstk: Create shadow stacks")
Quoted 35 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>

This can more easily be demonstrated with CPU hotplug than S3, and the absence of bug reports goes to show how rarely hotplug is used.

---
 xen/arch/x86/smpboot.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/smpboot.c +3 -3
index 5708573c41..c193cc0fb8 100644
@@ -971,16 +971,16 @@ static void cpu_smpboot_free(unsigned int cpu, bool remove)
     if ( IS_ENABLED(CONFIG_PV32) )
         FREE_XENHEAP_PAGE(per_cpu(compat_gdt, cpu));
 
+    if ( stack_base[cpu] )
+        memguard_unguard_stack(stack_base[cpu]);
+
     if ( remove )
     {
         FREE_XENHEAP_PAGE(per_cpu(gdt, cpu));
         FREE_XENHEAP_PAGE(idt_tables[cpu]);
 
         if ( stack_base[cpu] )
-        {
-            memguard_unguard_stack(stack_base[cpu]);
             FREE_XENHEAP_PAGES(stack_base[cpu], STACK_ORDER);
-        }
     }
 }
 
Signature · 5 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
#2 lore raw
In reply to Andrew Cooper
Quoted 11 lines · Andrew Cooper
xen/arch/x86/smpboot.c +3 -0
@@ -971,16 +971,16 @@ static void cpu_smpboot_free(unsigned int cpu, bool remove)
     if ( IS_ENABLED(CONFIG_PV32) )
         FREE_XENHEAP_PAGE(per_cpu(compat_gdt, cpu));
 
+    if ( stack_base[cpu] )
+        memguard_unguard_stack(stack_base[cpu]);
+
     if ( remove )
     {

xen/arch/x86/smpboot.c static void cpu_smpboot_free(unsigned int cpu, bool remove) · line 985

Quoted 10 lines
xen/arch/x86/smpboot.c +0 -3
         FREE_XENHEAP_PAGE(per_cpu(gdt, cpu));
         FREE_XENHEAP_PAGE(idt_tables[cpu]);
 
         if ( stack_base[cpu] )
-        {
-            memguard_unguard_stack(stack_base[cpu]);
             FREE_XENHEAP_PAGES(stack_base[cpu], STACK_ORDER);
-        }
     }
 }

In my initial reply to Marek's report I did suggest putting the fix in the alloc path in order to keep the pages "guarded" while the CPU is parked, as the CPU during that period may still access at least some of the stacks (e.g. when sending it an NMI IPI to enter a deeper C state).

Otherwise, if the fix really was to remain here, the if() could now also be dropped. And in this case I'd also suggest adding the new piece of code a few lines earlier, so that all the FREE_XENHEAP_PAGE() stay close together.

Jan

#3 lore raw
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Andrew Cooper
xen/arch/x86/smpboot.c +3 -0
@@ -971,16 +971,16 @@ static void cpu_smpboot_free(unsigned int cpu, bool remove)
     if ( IS_ENABLED(CONFIG_PV32) )
         FREE_XENHEAP_PAGE(per_cpu(compat_gdt, cpu));
 
+    if ( stack_base[cpu] )
+        memguard_unguard_stack(stack_base[cpu]);
+
     if ( remove )
     {
         FREE_XENHEAP_PAGE(per_cpu(gdt, cpu));
         FREE_XENHEAP_PAGE(idt_tables[cpu]);
 
Quoted 10 lines

xen/arch/x86/smpboot.c static void cpu_smpboot_free(unsigned int cpu, bool remove) · line 985

Quoted 7 lines
xen/arch/x86/smpboot.c +0 -3
         if ( stack_base[cpu] )
-        {
-            memguard_unguard_stack(stack_base[cpu]);
             FREE_XENHEAP_PAGES(stack_base[cpu], STACK_ORDER);
-        }
     }
 }

In my initial reply to Marek's report I did suggest putting the fix in the alloc path in order to keep the pages "guarded" while the CPU is parked

In which case you should have identified that bug explicitly.

Because I can't read your mind while fixing the other real bugs in your suggestion.

~Andrew

#4 lore raw
Quoted 27 lines · Andrew Cooper
Quoted 15 lines · Jan Beulich
Quoted 14 lines · Andrew Cooper
xen/arch/x86/smpboot.c +3 -0
@@ -971,16 +971,16 @@ static void cpu_smpboot_free(unsigned int cpu, bool remove)
     if ( IS_ENABLED(CONFIG_PV32) )
         FREE_XENHEAP_PAGE(per_cpu(compat_gdt, cpu));
 
+    if ( stack_base[cpu] )
+        memguard_unguard_stack(stack_base[cpu]);
+
     if ( remove )
     {
         FREE_XENHEAP_PAGE(per_cpu(gdt, cpu));
         FREE_XENHEAP_PAGE(idt_tables[cpu]);
 
Quoted 10 lines

xen/arch/x86/smpboot.c static void cpu_smpboot_free(unsigned int cpu, bool remove) · line 985

Quoted 7 lines
xen/arch/x86/smpboot.c +0 -3
         if ( stack_base[cpu] )
-        {
-            memguard_unguard_stack(stack_base[cpu]);
             FREE_XENHEAP_PAGES(stack_base[cpu], STACK_ORDER);
-        }
     }
 }

In my initial reply to Marek's report I did suggest putting the fix in the alloc path in order to keep the pages "guarded" while the CPU is parked

Quoted 4 lines

In which case you should have identified that bug explicitly.

Because I can't read your mind while fixing the other real bugs in your suggestion.

I'm sorry for the brevity at that point - it was a Sunday, and I merely thought I'd write down my observation after reading the report. And of course I'm curious as to the other real bugs in my suggestion (when I anyway said "something like").

Jan