Skip to contentSkip to conversation

x86/traps: improve show_trace()'s top-of-stack handling

PATCH In review Jan Beulich 2 participants 8 messages Jul 15, 2019 – Sep 23, 2019

lore
Share & tools
lore
#0 lore raw

v3 cover x86/traps: improve show_trace()'s top-of-stack handling

1: guard top-of-stack reads
2: widen condition for logging top-of-stack

The issue patch 2 fixes (a curious lack of an intermediate call stack entry) was observed in practice; patch 1 is a result of me just looking at the code.

Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#1 lore raw

PATCH v3 1/2 x86/traps: guard top-of-stack reads

Nothing guarantees that the original frame's stack pointer points at readable memory. Avoid a (likely nested) crash by attaching exception recovery to the read (making it a single read at the same time). Don't even invoke _show_trace() in case of a non-readable top slot.

---
v2: Name asm() arguments. Use explicit "fault" variable.

xen/arch/x86/traps.c +17 -3
@@ -486,17 +486,31 @@ static void _show_trace(unsigned long sp
  
  static void show_trace(const struct cpu_user_regs *regs)
  {
-    unsigned long *sp = ESP_BEFORE_EXCEPTION(regs);
+    unsigned long *sp = ESP_BEFORE_EXCEPTION(regs), tos = 0;
+    bool fault = false;
  
      printk("Xen call trace:\n");
  
+    /* Guarded read of the stack top. */
+    asm ( "1: mov %[data], %[tos]; 2:\n"
+          ".pushsection .fixup,\"ax\"\n"
+          "3: movb $1, %[fault]; jmp 2b\n"
+          ".popsection\n"
+          _ASM_EXTABLE(1b, 3b)
+          : [tos] "+r" (tos), [fault] "+qm" (fault) : [data] "m" (*sp) );
+
      /*
       * If RIP looks sensible, or the top of the stack doesn't, print RIP at
       * the top of the stack trace.
       */
      if ( is_active_kernel_text(regs->rip) ||
-         !is_active_kernel_text(*sp) )
+         !is_active_kernel_text(tos) )
          printk("   [<%p>] %pS\n", _p(regs->rip), _p(regs->rip));
+    else if ( fault )
+    {
+        printk("   [Fault on access]\n");
+        return;
+    }
      /*
       * Else RIP looks bad but the top of the stack looks good.  Perhaps we
       * followed a wild function pointer? Lets assume the top of the stack is a
@@ -505,7 +519,7 @@ static void show_trace(const struct cpu_
       */
      else
      {
-        printk("   [<%p>] %pS\n", _p(*sp), _p(*sp));
+        printk("   [<%p>] %pS\n", _p(tos), _p(tos));
          sp++;
      }
  

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by on v3 1/2
#2 lore raw

Re: [Xen-devel] [PATCH v3 1/2] x86/traps: guard top-of-stack reads

Quoted 7 lines · Jan Beulich

Nothing guarantees that the original frame's stack pointer points at readable memory. Avoid a (likely nested) crash by attaching exception recovery to the read (making it a single read at the same time). Don't even invoke _show_trace() in case of a non-readable top slot.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

Re: [Xen-devel] [PATCH v3 1/2] x86/traps: guard top-of-stack reads

Quoted 8 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich

Nothing guarantees that the original frame's stack pointer points at readable memory. Avoid a (likely nested) crash by attaching exception recovery to the read (making it a single read at the same time). Don't even invoke _show_trace() in case of a non-readable top slot.

With this, ...

Quoted 1 line

... was this perhaps meant for patch 2 of this short series?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

Re: [Xen-devel] [PATCH v3 1/2] x86/traps: guard top-of-stack reads

Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Jan Beulich

Nothing guarantees that the original frame's stack pointer points at readable memory. Avoid a (likely nested) crash by attaching exception recovery to the read (making it a single read at the same time). Don't even invoke _show_trace() in case of a non-readable top slot.

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines

With this, ...

Quoted 1 line

... was this perhaps meant for patch 2 of this short series?

No.  I didn't spot my R-b tag and only had enough free time to read the first email.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw

PATCH v3 2/2 x86/traps: widen condition for logging top-of-stack

In reply to Jan Beulich

Despite -fno-omit-frame-pointer the compiler may omit the frame pointer, often for relatively simple leaf functions. (To give a specific example, the case I've run into this with is _pci_hide_device() and gcc 8. Interestingly the even more simple neighboring iommu_has_feature() does get a frame pointer set up, around just a single instruction. But this may be a result of the size-of-asm() effects discussed elsewhere.)

Log the top-of-stack value if it looks valid _or_ if RIP looks invalid.

Also annotate all stack trace entries with a marker, to indicate their origin:
R: register state
F: frame pointer based
S: raw stack contents

---
v3: Tag stack entries consistently, but differently than in v2.
v2: Re-base over changes to earlier patch.

xen/arch/x86/traps.c +13 -8
@@ -433,7 +433,7 @@ static void _show_trace(unsigned long sp
      {
          addr = *stack++;
          if ( is_active_kernel_text(addr) )
-            printk("   [<%p>] %pS\n", _p(addr), _p(addr));
+            printk("   [<%p>] S %pS\n", _p(addr), _p(addr));
      }
  }
  
@@ -476,7 +476,7 @@ static void _show_trace(unsigned long sp
              addr  = frame[1];
          }
  
-        printk("   [<%p>] %pS\n", _p(addr), _p(addr));
+        printk("   [<%p>] F %pS\n", _p(addr), _p(addr));
  
          low = (unsigned long)&frame[2];
      }
@@ -505,21 +505,26 @@ static void show_trace(const struct cpu_
       */
      if ( is_active_kernel_text(regs->rip) ||
           !is_active_kernel_text(tos) )
-        printk("   [<%p>] %pS\n", _p(regs->rip), _p(regs->rip));
-    else if ( fault )
+        printk("   [<%p>] R %pS\n", _p(regs->rip), _p(regs->rip));
+
+    if ( fault )
      {
          printk("   [Fault on access]\n");
          return;
      }
+
      /*
-     * Else RIP looks bad but the top of the stack looks good.  Perhaps we
-     * followed a wild function pointer? Lets assume the top of the stack is a
+     * If RIP looks bad or the top of the stack looks good, log the top of
+     * stack as well.  Perhaps we followed a wild function pointer, or we're
+     * in a function without frame pointer, or in a function prologue before
+     * the frame pointer gets set up?  Let's assume the top of the stack is a
       * return address; print it and skip past so _show_trace() doesn't print
       * it again.
       */
-    else
+    if ( !is_active_kernel_text(regs->rip) ||
+         is_active_kernel_text(tos) )
      {
-        printk("   [<%p>] %pS\n", _p(tos), _p(tos));
+        printk("   [<%p>] S %pS\n", _p(tos), _p(tos));
          sp++;
      }
  

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by on v3 2/2
#6 lore raw

Re: [Xen-devel] [PATCH v3 2/2] x86/traps: widen condition for logging top-of-stack

Quoted 9 lines · Jan Beulich

Despite -fno-omit-frame-pointer the compiler may omit the frame pointer, often for relatively simple leaf functions. (To give a specific example, the case I've run into this with is _pci_hide_device() and gcc 8. Interestingly the even more simple neighboring iommu_has_feature() does get a frame pointer set up, around just a single instruction. But this may be a result of the size-of-asm() effects discussed elsewhere.)

Log the top-of-stack value if it looks valid _or_ if RIP looks invalid.

Quoted 7 lines

Also annotate all stack trace entries with a marker, to indicate their origin:
R: register state
F: frame pointer based
S: raw stack contents

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw

[Xen-devel] Ping: [PATCH v3 0/2] x86/traps: improve show_trace()'s top-of-stack handling

In reply to Jan Beulich

Andrew,

Quoted 6 lines · Jan Beulich

1: guard top-of-stack reads
2: widen condition for logging top-of-stack

The issue patch 2 fixes (a curious lack of an intermediate call stack entry) was observed in practice; patch 1 is a result of me just looking at the code.

I realize you're not in the office for the next several days (and in particular not until code freeze), but I think this is another item which would be nice if we could have in 4.13, and it's been pending for quite some time. Of course, if we can't reach agreement on how this should work, then so be it (and I'll carry the patches locally for the foreseeable future) ...

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel