Skip to contentSkip to conversation

x86: CET-SS related adjustments

PATCH In review Jan Beulich 2 participants 10 messages Nov 9 – Nov 16

lore
Share & tools
lore
#0 lore raw

v5 cover x86: CET-SS related adjustments

One might think of this as follow-on to XSA-451, but that's not quite the right order of events.

There are a few open aspects; see in particular the final patch.

v5 addresses review feedback on just the 1st patch; the other three are unchanged from v4.

1: record SSP at non-guest entry points
2: traps: use entry_ssp in fixup_exception_return() 3: traps: reduce indentation in fixup_exception_return() 4: prefer shadow stack for producing call traces

Jan

#1 lore raw

PATCH v5 1/4 x86: record SSP at non-guest entry points

We will want to use that value for call trace generation, and likely also to eliminate the somewhat fragile shadow stack searching done in fixup_exception_return(). For those purposes, guest-only entry points do not need to record that value.

To keep the saving code simple, record our own SSP that corresponds to an exception frame, pointing to the top of the shadow stack counterpart of what the CPU has saved on the regular stack. Consuming code can then work its way from there.

In SAVE_ALL / RESTORE_ALL simply drop the use of UREGS_r15. We want the full size in all cases, so what exactly the top-of-stack field is going to be (whose UREGS_* is 0 anyway) doesn't matter this much there. This way we don't need to distinguish between XEN_SHSTK=y and XEN_SHSTK=n.

---
v5: Drop ssp parameter of PUSH_AND_CLEAR_GPRS again.
v4: Re-base.
v3: Put new field at the front of struct cpu_user_regs.
v2: Add comment ahead of SAVE_ALL. Add comma between its parameters.
Re-base.

xen/arch/x86/include/asm/asm_defns.h +7 -0
@@ -259,12 +259,19 @@ static always_inline void stac(void)
         xor   %r14d, %r14d
         push  %r15
         xor   %r15d, %r15d
+#ifdef CONFIG_XEN_SHSTK
+        rdsspq %rcx
+        push  %rcx
+#endif
 .endm
 
 /*
  * POP GPRs from a UREGS_* frame on the stack.  Does not modify flags.
  */
 .macro POP_GPRS skip_rax=0
+#ifdef CONFIG_XEN_SHSTK
+        pop   %rcx
+#endif
         pop   %r15
         pop   %r14
         pop   %r13
xen/arch/x86/include/asm/cpu-user-regs.h +9 -0
@@ -11,6 +11,15 @@
  */
 struct cpu_user_regs
 {
+#ifdef CONFIG_XEN_SHSTK
+    /*
+     * This points _at_ the corresponding shadow stack frame; it is _not_ the
+     * outer context's SSP.  That, if the outer context has CET-SS enabled,
+     * is stored in the top slot of the pointed to shadow stack.
+     */
+    uint64_t entry_ssp;
+#endif
+
     union { uint64_t r15;    uint32_t r15d;   uint16_t r15w;  uint8_t r15b; };
     union { uint64_t r14;    uint32_t r14d;   uint16_t r14w;  uint8_t r14b; };
     union { uint64_t r13;    uint32_t r13d;   uint16_t r13w;  uint8_t r13b; };
xen/arch/x86/x86_64/asm-offsets.c +3 -0
@@ -57,6 +57,9 @@ void __dummy__(void)
     OFFSET(UREGS_eflags, struct cpu_user_regs, rflags);
     OFFSET(UREGS_rsp, struct cpu_user_regs, rsp);
     OFFSET(UREGS_ss, struct cpu_user_regs, ss);
+#ifdef CONFIG_XEN_SHSTK
+    OFFSET(UREGS_entry_ssp, struct cpu_user_regs, entry_ssp);
+#endif
     DEFINE(UREGS_kernel_sizeof, sizeof(struct cpu_user_regs));
     BLANK();
 
#4 lore raw

PATCH v5 2/4 x86/traps: use entry_ssp in fixup_exception_return()

In reply to Jan Beulich

With the value recorded on entry there's no need anymore to go hunt for the respective exception frame on the shadow stack. By deriving "ptr" from that field (without any offset), it then ends up pointing one slot lower than before. Therefore all array indexes need incrementing, nicely doing away with all the negative ones.

---
Indentation of the prior inner (but not innermost) if()'s body is deliberately left untouched, to aid review. It'll be adjusted in a separate follow-on patch.
---
v3: Relax the first BUG_ON().
v2: IS_ENABLED() -> #ifdef. Re-base.

xen/arch/x86/traps.c +26 -48
@@ -690,19 +690,6 @@ unsigned long get_stack_trace_bottom(uns
     }
 }
 
-static unsigned long get_shstk_bottom(unsigned long sp)
-{
-    /* SAF-11-safe */
-    switch ( get_stack_page(sp) )
-    {
-#ifdef CONFIG_XEN_SHSTK
-    case 0:  return ROUNDUP(sp, IST_SHSTK_SIZE) - sizeof(unsigned long);
-    case 5:  return ROUNDUP(sp, PAGE_SIZE)      - sizeof(unsigned long);
-#endif
-    default: return sp - sizeof(unsigned long);
-    }
-}
-
 unsigned long get_stack_dump_bottom(unsigned long sp)
 {
     switch ( get_stack_page(sp) )
@@ -1187,26 +1174,29 @@ void asmlinkage noreturn do_unhandled_tr
 static void fixup_exception_return(struct cpu_user_regs *regs,
                                    unsigned long fixup, unsigned long stub_ra)
 {
-    if ( IS_ENABLED(CONFIG_XEN_SHSTK) )
+#ifdef CONFIG_XEN_SHSTK
     {
-        unsigned long ssp, *ptr, *base;
+        unsigned long ssp = rdssp();
 
-        if ( (ssp = rdssp()) == SSP_NO_SHSTK )
-            goto shstk_done;
+        if ( ssp != SSP_NO_SHSTK )
+        {
+            unsigned long *ptr = _p(regs->entry_ssp);
+            unsigned long primary_shstk =
+                (ssp & ~(STACK_SIZE - 1)) +
+                (PRIMARY_SHSTK_SLOT + 1) * PAGE_SIZE - 8;
 
-        ptr = _p(ssp);
-        base = _p(get_shstk_bottom(ssp));
+            BUG_ON((regs->entry_ssp ^ primary_shstk) >>
+                   (PAGE_SHIFT + STACK_ORDER));
 
-        for ( ; ptr < base; ++ptr )
-        {
             /*
-             * Search for %rip.  The shstk currently looks like this:
+             * The shstk currently looks like this:
              *
              *   tok  [Supervisor token, == &tok | BUSY, only with FRED inactive]
              *   ...  [Pointed to by SSP for most exceptions, empty in IST cases]
              *   %cs  [== regs->cs]
              *   %rip [== regs->rip]
-             *   SSP  [Likely points to 3 slots higher, above %cs]
+             *   SSP  [Pointed to by entry_ssp; Likely points to 3 slots
+             *         higher, above %cs]
              *   ...  [call tree to this function, likely 2/3 slots]
              *
              * and we want to overwrite %rip with fixup.  There are two
@@ -1219,13 +1209,10 @@ static void fixup_exception_return(struc
              *
              * Check for both regs->rip and regs->cs matching.
              */
-            if ( ptr[0] == regs->rip && ptr[1] == regs->cs )
-            {
-                unsigned long primary_shstk =
-                    (ssp & ~(STACK_SIZE - 1)) +
-                    (PRIMARY_SHSTK_SLOT + 1) * PAGE_SIZE - 8;
+            BUG_ON(ptr[1] != regs->rip || ptr[2] != regs->cs);
 
-                wrss(fixup, ptr);
+            {
+                wrss(fixup, &ptr[1]);
 
                 if ( !stub_ra )
                     goto shstk_done;
@@ -1242,7 +1229,7 @@ static void fixup_exception_return(struc
                  * - if we're on an IST stack, we need to increment the
                  *   original SSP.
                  */
-                BUG_ON((ptr[-1] ^ primary_shstk) >> PAGE_SHIFT);
+                BUG_ON((ptr[0] ^ primary_shstk) >> PAGE_SHIFT);
 
                 if ( (ssp ^ primary_shstk) >> PAGE_SHIFT )
                 {
@@ -1251,39 +1238,30 @@ static void fixup_exception_return(struc
                      * addresses actually match.  Then increment the interrupted
                      * context's SSP.
                      */
-                    BUG_ON(stub_ra != *(unsigned long*)ptr[-1]);
-                    wrss(ptr[-1] + 8, &ptr[-1]);
+                    BUG_ON(stub_ra != *(unsigned long*)ptr[0]);
+                    wrss(ptr[0] + 8, &ptr[0]);
                     goto shstk_done;
                 }
 
                 /* Make sure the two return addresses actually match. */
-                BUG_ON(stub_ra != ptr[2]);
+                BUG_ON(stub_ra != ptr[3]);
 
                 /* Move exception frame, updating SSP there. */
-                wrss(ptr[1], &ptr[2]); /* %cs */
-                wrss(ptr[0], &ptr[1]); /* %rip */
-                wrss(ptr[-1] + 8, &ptr[0]); /* SSP */
+                wrss(ptr[2], &ptr[3]); /* %cs */
+                wrss(ptr[1], &ptr[2]); /* %rip */
+                wrss(ptr[0] + 8, &ptr[1]); /* SSP */
 
                 /* Move all newer entries. */
-                while ( --ptr != _p(ssp) )
-                    wrss(ptr[-1], &ptr[0]);
+                while ( ptr-- != _p(ssp) )
+                    wrss(ptr[0], &ptr[1]);
 
                 /* Finally account for our own stack having shifted up. */
                 asm volatile ( "incsspd %0" :: "r" (2) );
-
-                goto shstk_done;
             }
         }
-
-        /*
-         * We failed to locate and fix up the shadow IRET frame.  This could
-         * be due to shadow stack corruption, or bad logic above.  We cannot
-         * continue executing the interrupted context.
-         */
-        BUG();
-
     }
  shstk_done:
+#endif /* CONFIG_XEN_SHSTK */
 
     /* Fixup the regular stack. */
     regs->rip = fixup;
#5 lore raw

PATCH v5 3/4 x86/traps: reduce indentation in fixup_exception_return()

In reply to Jan Beulich

The earlier "x86/traps: use entry_ssp in fixup_exception_return()" left unnecessary scopes and hence unnecessarily deep indentation. While that was intentional (to improve readabilirty of the diff), rectify this now.

---
v4: New.

xen/arch/x86/traps.c +72 -76
@@ -1175,90 +1175,86 @@ static void fixup_exception_return(struc
                                    unsigned long fixup, unsigned long stub_ra)
 {
 #ifdef CONFIG_XEN_SHSTK
+    unsigned long ssp = rdssp();
+
+    if ( ssp != SSP_NO_SHSTK )
     {
-        unsigned long ssp = rdssp();
+        unsigned long *ptr = _p(regs->entry_ssp);
+        unsigned long primary_shstk =
+            (ssp & ~(STACK_SIZE - 1)) +
+            (PRIMARY_SHSTK_SLOT + 1) * PAGE_SIZE - 8;
+
+        BUG_ON((regs->entry_ssp ^ primary_shstk) >>
+               (PAGE_SHIFT + STACK_ORDER));
+
+        /*
+         * The shstk currently looks like this:
+         *
+         *   tok  [Supervisor token, == &tok | BUSY, only with FRED inactive]
+         *   ...  [Pointed to by SSP for most exceptions, empty in IST cases]
+         *   %cs  [== regs->cs]
+         *   %rip [== regs->rip]
+         *   SSP  [Pointed to by entry_ssp; Likely points to 3 slots
+         *         higher, above %cs]
+         *   ...  [call tree to this function, likely 2/3 slots]
+         *
+         * and we want to overwrite %rip with fixup.  There are two
+         * complications:
+         *   1) We cant depend on SSP values, because they won't differ by
+         *      3 slots if the exception is taken on an IST stack.
+         *   2) There are synthetic (unrealistic but not impossible)
+         *      scenarios where %rip can end up in the call tree to this
+         *      function, so we can't check against regs->rip alone.
+         *
+         * Check for both regs->rip and regs->cs matching.
+         */
+        BUG_ON(ptr[1] != regs->rip || ptr[2] != regs->cs);
+
+        wrss(fixup, &ptr[1]);
+
+        if ( !stub_ra )
+            goto shstk_done;
+
+        /*
+         * Stub recovery ought to happen only when the outer context
+         * was on the main shadow stack.  We need to also "pop" the
+         * stub's return address from the interrupted context's shadow
+         * stack.  That is,
+         * - if we're still on the main stack, we need to move the
+         *   entire stack (up to and including the exception frame)
+         *   up by one slot, incrementing the original SSP in the
+         *   exception frame,
+         * - if we're on an IST stack, we need to increment the
+         *   original SSP.
+         */
+        BUG_ON((ptr[0] ^ primary_shstk) >> PAGE_SHIFT);
 
-        if ( ssp != SSP_NO_SHSTK )
+        if ( (ssp ^ primary_shstk) >> PAGE_SHIFT )
         {
-            unsigned long *ptr = _p(regs->entry_ssp);
-            unsigned long primary_shstk =
-                (ssp & ~(STACK_SIZE - 1)) +
-                (PRIMARY_SHSTK_SLOT + 1) * PAGE_SIZE - 8;
-
-            BUG_ON((regs->entry_ssp ^ primary_shstk) >>
-                   (PAGE_SHIFT + STACK_ORDER));
-
             /*
-             * The shstk currently looks like this:
-             *
-             *   tok  [Supervisor token, == &tok | BUSY, only with FRED inactive]
-             *   ...  [Pointed to by SSP for most exceptions, empty in IST cases]
-             *   %cs  [== regs->cs]
-             *   %rip [== regs->rip]
-             *   SSP  [Pointed to by entry_ssp; Likely points to 3 slots
-             *         higher, above %cs]
-             *   ...  [call tree to this function, likely 2/3 slots]
-             *
-             * and we want to overwrite %rip with fixup.  There are two
-             * complications:
-             *   1) We cant depend on SSP values, because they won't differ by
-             *      3 slots if the exception is taken on an IST stack.
-             *   2) There are synthetic (unrealistic but not impossible)
-             *      scenarios where %rip can end up in the call tree to this
-             *      function, so we can't check against regs->rip alone.
-             *
-             * Check for both regs->rip and regs->cs matching.
+             * We're on an IST stack.  First make sure the two return
+             * addresses actually match.  Then increment the interrupted
+             * context's SSP.
              */
-            BUG_ON(ptr[1] != regs->rip || ptr[2] != regs->cs);
+            BUG_ON(stub_ra != *(unsigned long*)ptr[0]);
+            wrss(ptr[0] + 8, &ptr[0]);
+            goto shstk_done;
+        }
 
-            {
-                wrss(fixup, &ptr[1]);
+        /* Make sure the two return addresses actually match. */
+        BUG_ON(stub_ra != ptr[3]);
 
-                if ( !stub_ra )
-                    goto shstk_done;
+        /* Move exception frame, updating SSP there. */
+        wrss(ptr[2], &ptr[3]); /* %cs */
+        wrss(ptr[1], &ptr[2]); /* %rip */
+        wrss(ptr[0] + 8, &ptr[1]); /* SSP */
+
+        /* Move all newer entries. */
+        while ( ptr-- != _p(ssp) )
+            wrss(ptr[0], &ptr[1]);
 
-                /*
-                 * Stub recovery ought to happen only when the outer context
-                 * was on the main shadow stack.  We need to also "pop" the
-                 * stub's return address from the interrupted context's shadow
-                 * stack.  That is,
-                 * - if we're still on the main stack, we need to move the
-                 *   entire stack (up to and including the exception frame)
-                 *   up by one slot, incrementing the original SSP in the
-                 *   exception frame,
-                 * - if we're on an IST stack, we need to increment the
-                 *   original SSP.
-                 */
-                BUG_ON((ptr[0] ^ primary_shstk) >> PAGE_SHIFT);
-
-                if ( (ssp ^ primary_shstk) >> PAGE_SHIFT )
-                {
-                    /*
-                     * We're on an IST stack.  First make sure the two return
-                     * addresses actually match.  Then increment the interrupted
-                     * context's SSP.
-                     */
-                    BUG_ON(stub_ra != *(unsigned long*)ptr[0]);
-                    wrss(ptr[0] + 8, &ptr[0]);
-                    goto shstk_done;
-                }
-
-                /* Make sure the two return addresses actually match. */
-                BUG_ON(stub_ra != ptr[3]);
-
-                /* Move exception frame, updating SSP there. */
-                wrss(ptr[2], &ptr[3]); /* %cs */
-                wrss(ptr[1], &ptr[2]); /* %rip */
-                wrss(ptr[0] + 8, &ptr[1]); /* SSP */
-
-                /* Move all newer entries. */
-                while ( ptr-- != _p(ssp) )
-                    wrss(ptr[0], &ptr[1]);
-
-                /* Finally account for our own stack having shifted up. */
-                asm volatile ( "incsspd %0" :: "r" (2) );
-            }
-        }
+        /* Finally account for our own stack having shifted up. */
+        asm volatile ( "incsspd %0" :: "r" (2) );
     }
  shstk_done:
 #endif /* CONFIG_XEN_SHSTK */
#6 lore raw

PATCH v5 4/4 x86: prefer shadow stack for producing call traces

In reply to Jan Beulich

Shadow stacks contain little more than return addresses, and they in particular allow precise call traces also with FRAME_POINTER=n:

(XEN) Xen call trace:

(XEN)    [<ffff82d04032d730>] R extable.c#search_one_extable+0x70/0x73
(XEN)    [<ffff82d04032d802>] C search_exception_table+0xc2/0x177
(XEN)    [<ffff82d040358378>] C traps.c#extable_fixup.isra.0+0x18/0x6c
(XEN)    [<ffff82d040358e3b>] C do_invalid_op+0xab/0x106
(XEN)    [<ffff82d040201d98>] C x86_64/entry.S#handle_exception_saved+0x88/0xf4
(XEN)    [<ffff82d07fffe044>] E ffff82d07fffe044
(XEN)    [<ffff82d040412db0>] C stub_selftest+0xd0/0x168
(XEN)    [<ffff82d0403508d6>] C setup.c#init_done+0x116/0x15a

as opposed to this counterpart (earlier during the same boot, before CET is enabled):

(XEN) Xen call trace:

(XEN)    [<ffff82d04032d730>] R extable.c#search_one_extable+0x70/0x73
(XEN)    [<ffff82d04032d802>] S search_exception_table+0xc2/0x177
(XEN)    [<ffff82d040358378>] S traps.c#extable_fixup.isra.0+0x18/0x6c
(XEN)    [<ffff82d040358e3b>] S do_invalid_op+0xab/0x106
(XEN)    [<ffff82d040201d98>] S x86_64/entry.S#handle_exception_saved+0x88/0xf4
(XEN)    [<ffff82d040412db0>] S stub_selftest+0xd0/0x168
(XEN)    [<ffff82d0403d0cc9>] S do_initcalls+0x29/0x38
(XEN)    [<ffff82d04041adf2>] S __start_xen+0x1c72/0x2235
(XEN)    [<ffff82d040288a57>] S __high_start+0xb7/0xc0

(note the entirely missing entry for the stub itself [1]; sadly there are no stray entries there).

[1] Arguably we could teach FRAME_POINTER=n traces to recognize stubs as well. But not FRAME_POINTER=y ones. In fact, what's missing there isn't the stub itself, but (of course) its immediate caller.

---
While the 'E' for exception frames is probably okay, I'm not overly happy with the 'C' (for CET). I would have preferred 'S' (for shadow), but we use that character already.

As an alternative to suppressing output for the top level exception frame, adding the new code ahead of the 'R' output line (and then also ahead of the stack top read) could be considered.

Quite likely a number of other uses of is_active_kernel_text() also want amending with in_stub().
---
v3: Correct "link to other shadow stack" check. Don't log a line for the
(impossible) PV case. Add example stack trace to description.
v2: IS_ENABLED() -> #ifdef. Re-base.

xen/arch/x86/traps.c +51 -0
@@ -48,6 +48,7 @@
 #include <asm/shared.h>
 #include <asm/shstk.h>
 #include <asm/smp.h>
+#include <asm/stubs.h>
 #include <asm/system.h>
 #include <asm/traps.h>
 #include <asm/uaccess.h>
@@ -705,6 +706,13 @@ unsigned long get_stack_dump_bottom(unsi
     }
 }
 
+#ifdef CONFIG_XEN_SHSTK
+static bool in_stub(unsigned long addr)
+{
+    return !((this_cpu(stubs.addr) ^ addr) >> STUB_BUF_SHIFT);
+}
+#endif
+
 #if !defined(CONFIG_FRAME_POINTER)
 
 /*
@@ -797,6 +805,49 @@ static void show_trace(const struct cpu_
          !is_active_kernel_text(tos) )
         printk("   [<%p>] R %pS\n", _p(regs->rip), _p(regs->rip));
 
+#ifdef CONFIG_XEN_SHSTK
+    if ( rdssp() != SSP_NO_SHSTK )
+    {
+        const unsigned long *ptr = _p(regs->entry_ssp);
+        unsigned int n;
+
+        for ( n = 0; (unsigned long)ptr & (PAGE_SIZE - sizeof(*ptr)); ++n )
+        {
+            unsigned long val = *ptr;
+
+            if ( is_active_kernel_text(val) || in_stub(val) )
+            {
+                /* Normal return address entry.  */
+                printk("   [<%p>] C %pS\n", _p(val), _p(val));
+                ++ptr;
+            }
+            else if ( !((val ^ (unsigned long)ptr) >>
+                        (PAGE_SHIFT + STACK_ORDER)) )
+            {
+                if ( val & (sizeof(val) - 1) )
+                {
+                    /* Most likely a supervisor token. */
+                    break;
+                }
+
+                /*
+                 * Ought to be a hypervisor interruption frame.  But don't
+                 * (re)log the current frame's %rip.
+                 */
+                if ( n || ptr[1] != regs->rip )
+                    printk("   [<%p>] E %pS\n", _p(ptr[1]), _p(ptr[1]));
+                ptr = _p(val);
+            }
+            else /* Bogus. */
+                break;
+        }
+
+        /* Fall back to legacy stack trace if nothing was logged at all. */
+        if ( n )
+            return;
+    }
+#endif /* CONFIG_XEN_SHSTK */
+
     if ( fault )
     {
         printk("   [Fault on access]\n");
#2 lore raw

Re: [PATCH v5 1/4] x86: record SSP at non-guest entry points

In reply to Jan Beulich
Quoted 10 lines · Jan Beulich

We will want to use that value for call trace generation, and likely also to eliminate the somewhat fragile shadow stack searching done in fixup_exception_return(). For those purposes, guest-only entry points do not need to record that value.

To keep the saving code simple, record our own SSP that corresponds to an exception frame, pointing to the top of the shadow stack counterpart of what the CPU has saved on the regular stack. Consuming code can then work its way from there.

Quoted 6 lines

In SAVE_ALL / RESTORE_ALL simply drop the use of UREGS_r15. We want the full size in all cases, so what exactly the top-of-stack field is going to be (whose UREGS_* is 0 anyway) doesn't matter this much there. This way we don't need to distinguish between XEN_SHSTK=y and XEN_SHSTK=n.

The code is now fine, but the commit message is quite stale.

You need to drop "non-guest" in at least two places, and change SAVE_ALL/RESTORE_ALL.  In fact that whole final paragraph can go.

With the commit message suitably adjusted, Reviewed-by: Andrew Cooper <[email protected]>

Quoted 10 lines

---
v5: Drop ssp parameter of PUSH_AND_CLEAR_GPRS again.
v4: Re-base.
v3: Put new field at the front of struct cpu_user_regs.
v2: Add comment ahead of SAVE_ALL. Add comma between its parameters.
Re-base.

xen/arch/x86/include/asm/asm_defns.h static always_inline void stac(void) · line 265

Quoted 7 lines
xen/arch/x86/include/asm/asm_defns.h +4 -0
@@ -259,12 +259,19 @@ static always_inline void stac(void)
         xor   %r14d, %r14d
         push  %r15
         xor   %r15d, %r15d
+#ifdef CONFIG_XEN_SHSTK
+        rdsspq %rcx
+        push  %rcx
+#endif

It might be worth having /* Zero (from above) if CET-SS is not enabled */ on the push here to make it clear what value is stored.

~Andrew

#3 lore raw

Re: [PATCH v5 1/4] x86: record SSP at non-guest entry points

Quoted 16 lines · Andrew Cooper
Quoted 15 lines · Jan Beulich

We will want to use that value for call trace generation, and likely also to eliminate the somewhat fragile shadow stack searching done in fixup_exception_return(). For those purposes, guest-only entry points do not need to record that value.

To keep the saving code simple, record our own SSP that corresponds to an exception frame, pointing to the top of the shadow stack counterpart of what the CPU has saved on the regular stack. Consuming code can then work its way from there.

In SAVE_ALL / RESTORE_ALL simply drop the use of UREGS_r15. We want the full size in all cases, so what exactly the top-of-stack field is going to be (whose UREGS_* is 0 anyway) doesn't matter this much there. This way we don't need to distinguish between XEN_SHSTK=y and XEN_SHSTK=n.

Quoted 6 lines
Quoted 1 line

The code is now fine, but the commit message is quite stale.

You need to drop "non-guest" in at least two places, and change SAVE_ALL/RESTORE_ALL.  In fact that whole final paragraph can go.

Oh, indeed. Dropped / adjusted.

Quoted 2 lines

With the commit message suitably adjusted, Reviewed-by: Andrew Cooper <[email protected]>

Thanks.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/include/asm/asm_defns.h +0 -0
@@ -259,12 +259,19 @@ static always_inline void stac(void)
         xor   %r14d, %r14d
         push  %r15
Quoted 8 lines

xen/arch/x86/include/asm/asm_defns.h static always_inline void stac(void) · line 265

Quoted 5 lines
xen/arch/x86/include/asm/asm_defns.h +4 -0
         xor   %r15d, %r15d
+#ifdef CONFIG_XEN_SHSTK
+        rdsspq %rcx
+        push  %rcx
+#endif

It might be worth having /* Zero (from above) if CET-SS is not enabled */ on the push here to make it clear what value is stored.

Sure, added.

Jan

#7 lore raw

Re: [PATCH v5 4/4] x86: prefer shadow stack for producing call traces

In reply to Jan Beulich
Quoted 27 lines · Jan Beulich

Shadow stacks contain little more than return addresses, and they in particular allow precise call traces also with FRAME_POINTER=n:

(XEN) Xen call trace:

(XEN)    [<ffff82d04032d730>] R extable.c#search_one_extable+0x70/0x73
(XEN)    [<ffff82d04032d802>] C search_exception_table+0xc2/0x177
(XEN)    [<ffff82d040358378>] C traps.c#extable_fixup.isra.0+0x18/0x6c
(XEN)    [<ffff82d040358e3b>] C do_invalid_op+0xab/0x106
(XEN)    [<ffff82d040201d98>] C x86_64/entry.S#handle_exception_saved+0x88/0xf4
(XEN)    [<ffff82d07fffe044>] E ffff82d07fffe044
(XEN)    [<ffff82d040412db0>] C stub_selftest+0xd0/0x168
(XEN)    [<ffff82d0403508d6>] C setup.c#init_done+0x116/0x15a

as opposed to this counterpart (earlier during the same boot, before CET is enabled):

(XEN) Xen call trace:

(XEN)    [<ffff82d04032d730>] R extable.c#search_one_extable+0x70/0x73
(XEN)    [<ffff82d04032d802>] S search_exception_table+0xc2/0x177
(XEN)    [<ffff82d040358378>] S traps.c#extable_fixup.isra.0+0x18/0x6c
(XEN)    [<ffff82d040358e3b>] S do_invalid_op+0xab/0x106
(XEN)    [<ffff82d040201d98>] S x86_64/entry.S#handle_exception_saved+0x88/0xf4
(XEN)    [<ffff82d040412db0>] S stub_selftest+0xd0/0x168
(XEN)    [<ffff82d0403d0cc9>] S do_initcalls+0x29/0x38
(XEN)    [<ffff82d04041adf2>] S __start_xen+0x1c72/0x2235
(XEN)    [<ffff82d040288a57>] S __high_start+0xb7/0xc0
Quoted 8 lines

(note the entirely missing entry for the stub itself [1]; sadly there are no stray entries there).

[1] Arguably we could teach FRAME_POINTER=n traces to recognize stubs as well. But not FRAME_POINTER=y ones. In fact, what's missing there isn't the stub itself, but (of course) its immediate caller.

Sorry, but this commit message is still deceptive to read.  Please reformat it as:

---8<---
Shadow stacks contain little more than return addresses, and they in particular allow precise call traces also with FRAME_POINTER=n:

Compare a WARN() in stub_selftest() which runs twice on boot, first without Shadow Stacks:

    (XEN) Xen call trace:
    ...
    (XEN)    [<ffff82d040288a57>] S __high_start+0xb7/0xc0

and later with Shadow Stacks:

    (XEN) Xen call trace:
    ...
    (XEN)    [<ffff82d0403508d6>] C setup.c#init_done+0x116/0x15a

Note that ...
---8<---

This way you are presenting the information in chronological order, as well as stating which has CET enabled and which does not.

We probably ought to teach the emulation stubs about CONFIG_FRAME_POINTER.  It should be consistent

Quoted 27 lines

---
While the 'E' for exception frames is probably okay, I'm not overly happy with the 'C' (for CET). I would have preferred 'S' (for shadow), but we use that character already.

As an alternative to suppressing output for the top level exception frame, adding the new code ahead of the 'R' output line (and then also ahead of the stack top read) could be considered.

Quite likely a number of other uses of is_active_kernel_text() also want amending with in_stub().
---
v3: Correct "link to other shadow stack" check. Don't log a line for the
(impossible) PV case. Add example stack trace to description.
v2: IS_ENABLED() -> #ifdef. Re-base.

xen/arch/x86/traps.c +1 -0
@@ -48,6 +48,7 @@
 #include <asm/shared.h>
 #include <asm/shstk.h>
 #include <asm/smp.h>
+#include <asm/stubs.h>
 #include <asm/system.h>
 #include <asm/traps.h>
 #include <asm/uaccess.h>

xen/arch/x86/traps.c unsigned long get_stack_dump_bottom(unsi · line 714

Quoted 9 lines
xen/arch/x86/traps.c +6 -0
@@ -705,6 +706,13 @@ unsigned long get_stack_dump_bottom(unsi
     }
 }
 
+#ifdef CONFIG_XEN_SHSTK
+static bool in_stub(unsigned long addr)
+{
+    return !((this_cpu(stubs.addr) ^ addr) >> STUB_BUF_SHIFT);
+}
+#endif

static inline, or __maybe_unused.  This stub needs using elsewhere, and has nothing to do with XEN_SHSTK beyond the first user.

xen/arch/x86/traps.c static void show_trace(const struct cpu_ · line 809

Quoted 10 lines
xen/arch/x86/traps.c +3 -0
+
 #if !defined(CONFIG_FRAME_POINTER)
 
 /*
@@ -797,6 +805,49 @@ static void show_trace(const struct cpu_
          !is_active_kernel_text(tos) )
         printk("   [<%p>] R %pS\n", _p(regs->rip), _p(regs->rip));
 
+#ifdef CONFIG_XEN_SHSTK
+    if ( rdssp() != SSP_NO_SHSTK )

Thinking about it, the entry condition wants to be regs->entry_ssp.

If shadow stacks where enabled at the time of taking the exception/etc, that's good enough.

xen/arch/x86/traps.c static void show_trace(const struct cpu_ · line 811

Quoted 2 lines
xen/arch/x86/traps.c +2 -0
+    {
+        const unsigned long *ptr = _p(regs->entry_ssp);

Something Linux is gaining might be worth using here.

+struct fred_shstk_entry_frame {
+    u64 ssef_ssp;
+    u64 ssef_lip;
+    u64 ssef_cs;
+};

(Names subject to improvement, especially as this is not actually FRED related, although cs does get extended with SL and WFE when FRED is enabled.)

The logic below will render incorrectly on the IST stacks, I think.

In those cases, we'll get an entry_ssp pointing at the IST shstk with { ssp, lip, cs, (maybe) sup token }, where what we want to do for rendering the dump is to follow ssp first to get to the outer context and then walk up.  This is different from rendering regs->rsp which starts from the outer context.

Quoted 10 lines
xen/arch/x86/traps.c +10 -0
+        unsigned int n;
+
+        for ( n = 0; (unsigned long)ptr & (PAGE_SIZE - sizeof(*ptr)); ++n )
+        {
+            unsigned long val = *ptr;
+
+            if ( is_active_kernel_text(val) || in_stub(val) )
+            {
+                /* Normal return address entry.  */
+                printk("   [<%p>] C %pS\n", _p(val), _p(val));

xen/arch/x86/traps.c static void show_trace(const struct cpu_ · line 831

Quoted 10 lines
xen/arch/x86/traps.c +10 -0
+                ++ptr;
+            }
+            else if ( !((val ^ (unsigned long)ptr) >>
+                        (PAGE_SHIFT + STACK_ORDER)) )
+            {
+                if ( val & (sizeof(val) - 1) )
+                {
+                    /* Most likely a supervisor token. */
+                    break;
+                }

Supervisors tokens only exist in !opt_fred cases.  But I don't see how this is a valid check for one.  You're looking for non-zero in the bottom 3 bits, but that's potentially ambiguous with cs and fails to spot a non-busy token.

The distinguishing characteristic of a supervisor token is that it is it's own address, so check (val ^ ptr) >> 1.

xen/arch/x86/traps.c static void show_trace(const struct cpu_ · line 838

Quoted 7 lines
xen/arch/x86/traps.c +7 -0
+
+                /*
+                 * Ought to be a hypervisor interruption frame.  But don't
+                 * (re)log the current frame's %rip.
+                 */
+                if ( n || ptr[1] != regs->rip )
+                    printk("   [<%p>] E %pS\n", _p(ptr[1]), _p(ptr[1]));

Ah - this n is the thing covering the fact that you didn't move to the outer context originally.  Please do that and drop this.

If we're going to start using E here, we should make the FRAME_POINTER side match.  When printing something we've identified as a stub, we ought to say "in stub" rather than leaving it as a raw number.

Not for this patch, but it might be nice to identify EFI ranges too, because that's the only other case where we commonly get raw numbers.

xen/arch/x86/traps.c static void show_trace(const struct cpu_ · line 847

Quoted 9 lines
xen/arch/x86/traps.c +9 -0
+                ptr = _p(val);
+            }
+            else /* Bogus. */
+                break;
+        }
+
+        /* Fall back to legacy stack trace if nothing was logged at all. */
+        if ( n )
+            return;

This comment wants to be after the return, not before it.

~Andrew

Quoted 7 lines
xen/arch/x86/traps.c +3 -0
+    }
+#endif /* CONFIG_XEN_SHSTK */
+
     if ( fault )
     {
         printk("   [Fault on access]\n");
#8 lore raw

Re: [PATCH v5 4/4] x86: prefer shadow stack for producing call traces

Quoted 58 lines · Andrew Cooper
Quoted 27 lines · Jan Beulich

Shadow stacks contain little more than return addresses, and they in particular allow precise call traces also with FRAME_POINTER=n:

(XEN) Xen call trace:

(XEN)    [<ffff82d04032d730>] R extable.c#search_one_extable+0x70/0x73
(XEN)    [<ffff82d04032d802>] C search_exception_table+0xc2/0x177
(XEN)    [<ffff82d040358378>] C traps.c#extable_fixup.isra.0+0x18/0x6c
(XEN)    [<ffff82d040358e3b>] C do_invalid_op+0xab/0x106
(XEN)    [<ffff82d040201d98>] C x86_64/entry.S#handle_exception_saved+0x88/0xf4
(XEN)    [<ffff82d07fffe044>] E ffff82d07fffe044
(XEN)    [<ffff82d040412db0>] C stub_selftest+0xd0/0x168
(XEN)    [<ffff82d0403508d6>] C setup.c#init_done+0x116/0x15a

as opposed to this counterpart (earlier during the same boot, before CET is enabled):

(XEN) Xen call trace:

(XEN)    [<ffff82d04032d730>] R extable.c#search_one_extable+0x70/0x73
(XEN)    [<ffff82d04032d802>] S search_exception_table+0xc2/0x177
(XEN)    [<ffff82d040358378>] S traps.c#extable_fixup.isra.0+0x18/0x6c
(XEN)    [<ffff82d040358e3b>] S do_invalid_op+0xab/0x106
(XEN)    [<ffff82d040201d98>] S x86_64/entry.S#handle_exception_saved+0x88/0xf4
(XEN)    [<ffff82d040412db0>] S stub_selftest+0xd0/0x168
(XEN)    [<ffff82d0403d0cc9>] S do_initcalls+0x29/0x38
(XEN)    [<ffff82d04041adf2>] S __start_xen+0x1c72/0x2235
(XEN)    [<ffff82d040288a57>] S __high_start+0xb7/0xc0
Quoted 8 lines

(note the entirely missing entry for the stub itself [1]; sadly there are no stray entries there).

[1] Arguably we could teach FRAME_POINTER=n traces to recognize stubs as well. But not FRAME_POINTER=y ones. In fact, what's missing there isn't the stub itself, but (of course) its immediate caller.

Sorry, but this commit message is still deceptive to read.  Please reformat it as:

---8<---
Shadow stacks contain little more than return addresses, and they in particular allow precise call traces also with FRAME_POINTER=n:

Compare a WARN() in stub_selftest() which runs twice on boot, first without Shadow Stacks:

    (XEN) Xen call trace:
    ...
    (XEN)    [<ffff82d040288a57>] S __high_start+0xb7/0xc0

and later with Shadow Stacks:

    (XEN) Xen call trace:
    ...
    (XEN)    [<ffff82d0403508d6>] C setup.c#init_done+0x116/0x15a

Quoted 5 lines

Note that ...
---8<---

This way you are presenting the information in chronological order, as well as stating which has CET enabled and which does not.

This last aspect is stated clearly, by the text between the two traces. Presentation order (chronological vs what I want to emphasize) should also be left to the taste of the author, I think. I see nothing deceptive in there.

Quoted 2 lines

We probably ought to teach the emulation stubs about CONFIG_FRAME_POINTER.  It should be consistent

Maybe, yet perhaps only after (or together with) having all assembly code also obey to FRAME_POINTER.

Quoted 4 lines
Quoted 4 lines
(quoted diff) +0 -0
@@ -705,6 +706,13 @@ unsigned long get_stack_dump_bottom(unsi
     }
 }
 
Quoted 9 lines

xen/arch/x86/traps.c unsigned long get_stack_dump_bottom(unsi · line 714

Quoted 6 lines
xen/arch/x86/traps.c +6 -0
+#ifdef CONFIG_XEN_SHSTK
+static bool in_stub(unsigned long addr)
+{
+    return !((this_cpu(stubs.addr) ^ addr) >> STUB_BUF_SHIFT);
+}
+#endif

static inline, or __maybe_unused.  This stub needs using elsewhere, and has nothing to do with XEN_SHSTK beyond the first user.

Well, no. For the purpose of this patch #ifdef is an appropriate way of dealing with the otherwise resulting build issue. If and when further uses appear, the situation can be re-evaluated. __maybe_unused also isn't going to help the Misra aspect, as a deviation based on it exists solely for rule 2.6 (concerning labels).

If you insist (which I think you shouldn't, as again this is a matter of taste), the compromise I can offer is to move the predicate to a header, e.g. next to the decl of "stubs". Then adding "inline" will be appropriate (and necessary).

Quoted 11 lines

xen/arch/x86/traps.c static void show_trace(const struct cpu_ · line 809

Quoted 6 lines
xen/arch/x86/traps.c +2 -0
@@ -797,6 +805,49 @@ static void show_trace(const struct cpu_
          !is_active_kernel_text(tos) )
         printk("   [<%p>] R %pS\n", _p(regs->rip), _p(regs->rip));
 
+#ifdef CONFIG_XEN_SHSTK
+    if ( rdssp() != SSP_NO_SHSTK )

Thinking about it, the entry condition wants to be regs->entry_ssp.

If shadow stacks where enabled at the time of taking the exception/etc, that's good enough.

Functionally both should be identical (so to a fair degree this again wants leaving to the author's choice). At the same time you didn't request the same for fixup_exception_return(), and I think we want to keep both in sync.

Quoted 15 lines

xen/arch/x86/traps.c static void show_trace(const struct cpu_ · line 811

Quoted 2 lines
xen/arch/x86/traps.c +2 -0
+    {
+        const unsigned long *ptr = _p(regs->entry_ssp);

Something Linux is gaining might be worth using here.

(quoted diff) +5 -0
+struct fred_shstk_entry_frame {
+    u64 ssef_ssp;
+    u64 ssef_lip;
+    u64 ssef_cs;
+};

(Names subject to improvement, especially as this is not actually FRED related, although cs does get extended with SL and WFE when FRED is enabled.)

Quoted 7 lines

The logic below will render incorrectly on the IST stacks, I think.

In those cases, we'll get an entry_ssp pointing at the IST shstk with { ssp, lip, cs, (maybe) sup token }, where what we want to do for rendering the dump is to follow ssp first to get to the outer context and then walk up.  This is different from rendering regs->rsp which starts from the outer context.

Which is what is happening ...

Quoted 7 lines
Quoted 7 lines
xen/arch/x86/traps.c +7 -0
+        unsigned int n;
+
+        for ( n = 0; (unsigned long)ptr & (PAGE_SIZE - sizeof(*ptr)); ++n )
+        {
+            unsigned long val = *ptr;
+
+            if ( is_active_kernel_text(val) || in_stub(val) )
Quoted 8 lines

xen/arch/x86/traps.c static void show_trace(const struct cpu_ · line 826

Quoted 8 lines
xen/arch/x86/traps.c +8 -0
+            {
+                /* Normal return address entry.  */
+                printk("   [<%p>] C %pS\n", _p(val), _p(val));
+                ++ptr;
+            }
+            else if ( !((val ^ (unsigned long)ptr) >>
+                        (PAGE_SHIFT + STACK_ORDER)) )
+            {

... in the body of this "else if()", as in that case the initial if() will yield "false".

Quoted 9 lines

xen/arch/x86/traps.c static void show_trace(const struct cpu_ · line 831

Quoted 5 lines
xen/arch/x86/traps.c +5 -0
+                if ( val & (sizeof(val) - 1) )
+                {
+                    /* Most likely a supervisor token. */
+                    break;
+                }

Supervisors tokens only exist in !opt_fred cases.  But I don't see how this is a valid check for one.  You're looking for non-zero in the bottom 3 bits, but that's potentially ambiguous with cs

Under what conditions? Entry from ring-3 doesn't push anything. And PV32 (i.e. entry from ring-1) is excluded. Are you suggesting we need to cover the case of Xen using a ring-0 CS in the LDT?

Quoted 4 lines

and fails to spot a non-busy token.

The distinguishing characteristic of a supervisor token is that it is it's own address, so check (val ^ ptr) >> 1.

Why would we care about a non-busy token? That's as good as random data on an inactive (part of a) shadow stack.

Quoted 10 lines

xen/arch/x86/traps.c static void show_trace(const struct cpu_ · line 838

Quoted 7 lines
xen/arch/x86/traps.c +7 -0
+
+                /*
+                 * Ought to be a hypervisor interruption frame.  But don't
+                 * (re)log the current frame's %rip.
+                 */
+                if ( n || ptr[1] != regs->rip )
+                    printk("   [<%p>] E %pS\n", _p(ptr[1]), _p(ptr[1]));

Ah - this n is the thing covering the fact that you didn't move to the outer context originally.  Please do that and drop this.

Why would I duplicate the move-to-outer-context code (just below)? We may need to move between contexts more than once, after all.

Quoted 2 lines

If we're going to start using E here, we should make the FRAME_POINTER side match.

Perhaps, but not in this patch.

Quoted 2 lines

  When printing something we've identified as a stub, we ought to say "in stub" rather than leaving it as a raw number.

Can do, albeit like you said for the use of E above this then likely wants mirroring to normal frames as well, again not in this patch though. (At which point the #ifdef you complained about above would simply go away.) Will you be okay with

            if ( is_active_kernel_text(val) )
            {
                /* Normal return address entry.  */
                printk("   [<%p>] C %pS\n", _p(val), _p(val));
                ++ptr;
            }
            else if ( in_stub(val) )
            {
                /* Stub return address entry.  */
                printk("   [<%p>] C <stub>\n", _p(val));
                ++ptr;
            }
            else if ( ... )

?

Quoted 2 lines

Not for this patch, but it might be nice to identify EFI ranges too, because that's the only other case where we commonly get raw numbers.

Might be nice, as you say.

Quoted 11 lines

xen/arch/x86/traps.c static void show_trace(const struct cpu_ · line 847

Quoted 9 lines
xen/arch/x86/traps.c +9 -0
+                ptr = _p(val);
+            }
+            else /* Bogus. */
+                break;
+        }
+
+        /* Fall back to legacy stack trace if nothing was logged at all. */
+        if ( n )
+            return;

This comment wants to be after the return, not before it.

Again a matter of taste. To me the present placement explains why if if() is there. If I saw such a comment placed like you ask for, I'd pretty likely ask for it to be moved the other direction.

Having qualified most of your requests as "a matter of taste", what I now fear is going to happen is that we won't make progress here anymore. I'll be happy to learn otherwise, though.

Jan

#9 lore raw

Re: [PATCH v5 4/4] x86: prefer shadow stack for producing call traces

Quoted 61 lines · Jan Beulich
Quoted 21 lines · Andrew Cooper
Quoted 20 lines · Jan Beulich
xen/arch/x86/traps.c +20 -0
+        for ( n = 0; (unsigned long)ptr & (PAGE_SIZE - sizeof(*ptr)); ++n )
+        {
+            unsigned long val = *ptr;
+
+            if ( is_active_kernel_text(val) || in_stub(val) )
+            {
+                /* Normal return address entry.  */
+                printk("   [<%p>] C %pS\n", _p(val), _p(val));
+                ++ptr;
+            }
+            else if ( !((val ^ (unsigned long)ptr) >>
+                        (PAGE_SHIFT + STACK_ORDER)) )
+            {
+                if ( val & (sizeof(val) - 1) )
+                {
+                    /* Most likely a supervisor token. */
+                    break;
+                }
+
+                /*
Quoted 8 lines

xen/arch/x86/traps.c static void show_trace(const struct cpu_ · line 838

Quoted 5 lines
xen/arch/x86/traps.c +5 -0
+                 * Ought to be a hypervisor interruption frame.  But don't
+                 * (re)log the current frame's %rip.
+                 */
+                if ( n || ptr[1] != regs->rip )
+                    printk("   [<%p>] E %pS\n", _p(ptr[1]), _p(ptr[1]));

Ah - this n is the thing covering the fact that you didn't move to the outer context originally.  Please do that and drop this.

Why would I duplicate the move-to-outer-context code (just below)? We may need to move between contexts more than once, after all.

Quoted 2 lines

If we're going to start using E here, we should make the FRAME_POINTER side match.

Perhaps, but not in this patch.

Quoted 2 lines

  When printing something we've identified as a stub, we ought to say "in stub" rather than leaving it as a raw number.

Can do, albeit like you said for the use of E above this then likely wants mirroring to normal frames as well, again not in this patch though. (At which point the #ifdef you complained about above would simply go away.) Will you be okay with

            if ( is_active_kernel_text(val) )
            {
                /* Normal return address entry.  */
                printk("   [<%p>] C %pS\n", _p(val), _p(val));
                ++ptr;
            }
            else if ( in_stub(val) )
            {
                /* Stub return address entry.  */
                printk("   [<%p>] C <stub>\n", _p(val));
                ++ptr;
            }
            else if ( ... )
Quoted 1 line

?

Hmm, something similar would then be needed for the E case as well. (In fact the above should be extremely unlikely to take effect, as in the middle of a stub there shouldn't normally occur anything other than exceptions.)

Jan