Skip to contentSkip to conversation

x86/emul: Poision the stubs with debug traps

PATCH Superseded Andrew Cooper 2 participants 6 messages Mar 20, 2017 – Mar 31, 2017

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/emul: Poision the stubs with debug traps

...rather than leaving fragments of old instructions in place. This reduces the chances of something going further-wrong (as the debug trap will be caught and terminate the guest) in a cascade-failure where we end up executing the instruction fragments.

Before:

    (XEN) d2v0 exception 6 (ec=0000) in emulation stub (line 6239)
    (XEN) d2v0 stub: c4 e1 44 77 c3 80 d0 82 ff ff ff d1 90 ec 90

After:
(XEN) d3v0 exception 6 (ec=0000) in emulation stub (line 6239) (XEN) d3v0 stub: c4 e1 44 77 c3 cc cc cc cc cc cc cc cc cc cc

To make this work, the int3 handler needs to be extended to attempt recovery rather than simply returning back to Xen context. This is a good general precaution anyway, as nothing good will happen from letting Xen blindly execute over 0xcc's.

Extend the selftests to include int3, and add an extra printk indicating the start of the recovery selftests, to avoid leaving two otherwise-spurious faults visible in the log.

    (XEN) build-id: f0a0617cdb725551d03689c23bc59f34d329c182
    (XEN) Running stub recovery selftests...
    (XEN) traps.c:3463: GPF (0000): ffff82d0bffff041 [ffff82d0bffff041] -> ffff82d08025789a
    (XEN) traps.c:813: Trap 12: ffff82d0bffff040 [ffff82d0bffff040] -> ffff82d08025789a
    (XEN) ACPI sleep modes: S3

---
CC: Jan Beulich <[email protected]>

v2:
* Add selftest. Recover from int3.

---
 xen/arch/x86/extable.c     |  4 ++++
 xen/arch/x86/traps.c       | 18 +++++++++++++++---
 xen/arch/x86/x86_emulate.c |  4 ++--
 3 files changed, 21 insertions(+), 5 deletions(-)
xen/arch/x86/extable.c +4 -0
index 03af2c9..6fffe05 100644
@@ -140,10 +140,14 @@ static int __init stub_selftest(void)
         { .opc = { 0x02, 0x04, 0x04, 0xc3 }, /* add (%rsp,%rax),%al */
           .rax = 0xfedcba9876543210,
           .res.fields.trapnr = TRAP_stack_error },
+        { .opc = { 0xcc, 0xc3, 0xc3, 0xc3 }, /* int3 */
+          .res.fields.trapnr = TRAP_int3 },
     };
     unsigned long addr = this_cpu(stubs.addr) + STUB_BUF_SIZE / 2;
     unsigned int i;
 
+    printk("Running stub recovery selftests...\n");
+
     for ( i = 0; i < ARRAY_SIZE(tests); ++i )
     {
         uint8_t *ptr = map_domain_page(_mfn(this_cpu(stubs.mfn))) +
xen/arch/x86/traps.c +15 -3
index 0d54baf..fd5fb56 100644
@@ -1206,9 +1206,21 @@ void do_int3(struct cpu_user_regs *regs)
 
     if ( !guest_mode(regs) )
     {
-        debugger_trap_fatal(TRAP_int3, regs);
-        return;
-    } 
+        unsigned long fixup;
+
+        if ( (fixup = search_exception_table(regs)) != 0 )
+        {
+            this_cpu(last_extable_addr) = regs->rip;
+            regs->rip = fixup;
+            return;
+        }
+
+        if ( debugger_trap_fatal(TRAP_int3, regs) )
+            return;
+
+        show_execution_state(regs);
+        panic("FATAL TRAP: vector = %d (int3)", TRAP_int3);
+    }
 
     do_guest_trap(TRAP_int3, regs);
 }
xen/arch/x86/x86_emulate.c +2 -2
index 51df340..cc334ca 100644
@@ -30,8 +30,8 @@
     BUILD_BUG_ON(STUB_BUF_SIZE / 2 < MAX_INST_LEN + 1);         \
     ASSERT(!(stb).ptr);                                         \
     (stb).addr = this_cpu(stubs.addr) + STUB_BUF_SIZE / 2;      \
-    ((stb).ptr = map_domain_page(_mfn(this_cpu(stubs.mfn)))) +  \
-        ((stb).addr & ~PAGE_MASK);                              \
+    memset(((stb).ptr = map_domain_page(_mfn(this_cpu(stubs.mfn)))) +  \
+           ((stb).addr & ~PAGE_MASK), 0xcc, STUB_BUF_SIZE / 2);        \
 })
 #define put_stub(stb) ({                                   \
     if ( (stb).ptr )                                       \
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 20.03.17 at 11:58, <[email protected]> wrote:

...rather than leaving fragments of old instructions in place. This reduces the chances of something going further-wrong (as the debug trap will be caught
and terminate the guest) in a cascade-failure where we end up executing the instruction fragments.

Before:

    (XEN) d2v0 exception 6 (ec=0000) in emulation stub (line 6239)
    (XEN) d2v0 stub: c4 e1 44 77 c3 80 d0 82 ff ff ff d1 90 ec 90
Quoted 8 lines

After:
(XEN) d3v0 exception 6 (ec=0000) in emulation stub (line 6239) (XEN) d3v0 stub: c4 e1 44 77 c3 cc cc cc cc cc cc cc cc cc cc

To make this work, the int3 handler needs to be extended to attempt recovery rather than simply returning back to Xen context. This is a good general precaution anyway, as nothing good will happen from letting Xen blindly execute over 0xcc's.

I partly disagree: It can be a very useful thing to not have to rebuild Xen with hard coded INT3 invocations removed/re-added between runs with or without a debugger attached.

Quoted 5 lines
xen/arch/x86/traps.c +0 -0
@@ -1206,9 +1206,21 @@ void do_int3(struct cpu_user_regs *regs)
 
     if ( !guest_mode(regs) )

xen/arch/x86/traps.c void do_int3(struct cpu_user_regs *regs) · line 1214

Quoted 10 lines
xen/arch/x86/traps.c +6 -3
     {
-        debugger_trap_fatal(TRAP_int3, regs);
-        return;
-    } 
+        unsigned long fixup;
+
+        if ( (fixup = search_exception_table(regs)) != 0 )
+        {
+            this_cpu(last_extable_addr) = regs->rip;
+            regs->rip = fixup;

Should this be accompanied by a dprintk(), like most other such cases are?

xen/arch/x86/traps.c void do_int3(struct cpu_user_regs *regs) · line 1222

Quoted 8 lines
xen/arch/x86/traps.c +8 -0
+            return;
+        }
+
+        if ( debugger_trap_fatal(TRAP_int3, regs) )
+            return;
+
+        show_execution_state(regs);
+        panic("FATAL TRAP: vector = %d (int3)", TRAP_int3);

Along the lines of what I've said above, I'd like to see this being made conditional upon NDEBUG. Plus I think you want to call fatal_trap() here.

Jan

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

#2 lore raw
Quoted 15 lines · Jan Beulich
Quoted 15 lines
Quoted 1 line
Quoted 1 line · On 20.03.17 at 11:58, <[email protected]> wrote:

...rather than leaving fragments of old instructions in place. This reduces the chances of something going further-wrong (as the debug trap will be caught
and terminate the guest) in a cascade-failure where we end up executing the instruction fragments.

Before:

    (XEN) d2v0 exception 6 (ec=0000) in emulation stub (line 6239)
    (XEN) d2v0 stub: c4 e1 44 77 c3 80 d0 82 ff ff ff d1 90 ec 90

After:
(XEN) d3v0 exception 6 (ec=0000) in emulation stub (line 6239) (XEN) d3v0 stub: c4 e1 44 77 c3 cc cc cc cc cc cc cc cc cc cc

Quoted 7 lines
Quoted 4 lines

To make this work, the int3 handler needs to be extended to attempt recovery rather than simply returning back to Xen context. This is a good general precaution anyway, as nothing good will happen from letting Xen blindly execute over 0xcc's.

I partly disagree: It can be a very useful thing to not have to rebuild Xen with hard coded INT3 invocations removed/re-added between runs with or without a debugger attached.

What possible usecase is this for? If you don't already modify do_int3(), use of embedded int3's are of no use.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/traps.c +0 -0
@@ -1206,9 +1206,21 @@ void do_int3(struct cpu_user_regs *regs)
 
Quoted 13 lines

xen/arch/x86/traps.c void do_int3(struct cpu_user_regs *regs) · line 1214

Quoted 11 lines
xen/arch/x86/traps.c +6 -3
     if ( !guest_mode(regs) )
     {
-        debugger_trap_fatal(TRAP_int3, regs);
-        return;
-    } 
+        unsigned long fixup;
+
+        if ( (fixup = search_exception_table(regs)) != 0 )
+        {
+            this_cpu(last_extable_addr) = regs->rip;
+            regs->rip = fixup;

Should this be accompanied by a dprintk(), like most other such cases are?

Perhaps. I'm not fussed, and it should be a rare occurrence.

Quoted 11 lines

xen/arch/x86/traps.c void do_int3(struct cpu_user_regs *regs) · line 1222

Quoted 9 lines
xen/arch/x86/traps.c +8 -0
+            return;
+        }
+
+        if ( debugger_trap_fatal(TRAP_int3, regs) )
+            return;
+
+        show_execution_state(regs);
+        panic("FATAL TRAP: vector = %d (int3)", TRAP_int3);

Along the lines of what I've said above, I'd like to see this being made conditional upon NDEBUG.

Without some further explanation, I don't buy it as a plausible scenario.

Quoted 1 line

Plus I think you want to call fatal_trap() here.

fatal_trap() however would be better.

~Andrew

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

#3 lore raw
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 30.03.17 at 13:56, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 20.03.17 at 11:58, <[email protected]> wrote:

...rather than leaving fragments of old instructions in place. This reduces the chances of something going further-wrong (as the debug trap will be caught
and terminate the guest) in a cascade-failure where we end up executing the instruction fragments.

Before:

    (XEN) d2v0 exception 6 (ec=0000) in emulation stub (line 6239)
    (XEN) d2v0 stub: c4 e1 44 77 c3 80 d0 82 ff ff ff d1 90 ec 90

After:
(XEN) d3v0 exception 6 (ec=0000) in emulation stub (line 6239) (XEN) d3v0 stub: c4 e1 44 77 c3 cc cc cc cc cc cc cc cc cc cc

To make this work, the int3 handler needs to be extended to attempt recovery rather than simply returning back to Xen context. This is a good general precaution anyway, as nothing good will happen from letting Xen blindly execute over 0xcc's.

Quoted 6 lines
Quoted 3 lines

I partly disagree: It can be a very useful thing to not have to rebuild Xen with hard coded INT3 invocations removed/re-added between runs with or without a debugger attached.

What possible usecase is this for? If you don't already modify do_int3(), use of embedded int3's are of no use.

How are they not, with an external debugger attached? (I admit that I have no idea in what good or bad a state the external debugger support code is that we have.)

Jan

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

#4 lore raw
Quoted 21 lines · Jan Beulich
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 30.03.17 at 13:56, <[email protected]> wrote:
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 20.03.17 at 11:58, <[email protected]> wrote:

...rather than leaving fragments of old instructions in place. This reduces the chances of something going further-wrong (as the debug trap will be caught
and terminate the guest) in a cascade-failure where we end up executing the instruction fragments.

Before:

    (XEN) d2v0 exception 6 (ec=0000) in emulation stub (line 6239)
    (XEN) d2v0 stub: c4 e1 44 77 c3 80 d0 82 ff ff ff d1 90 ec 90

After:
(XEN) d3v0 exception 6 (ec=0000) in emulation stub (line 6239) (XEN) d3v0 stub: c4 e1 44 77 c3 cc cc cc cc cc cc cc cc cc cc

To make this work, the int3 handler needs to be extended to attempt recovery rather than simply returning back to Xen context. This is a good general precaution anyway, as nothing good will happen from letting Xen blindly execute over 0xcc's.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I partly disagree: It can be a very useful thing to not have to rebuild Xen with hard coded INT3 invocations removed/re-added between runs with or without a debugger attached.

What possible usecase is this for? If you don't already modify do_int3(), use of embedded int3's are of no use.

How are they not, with an external debugger attached? (I admit that I have no idea in what good or bad a state the external debugger support code is that we have.)

I have never succeeded at using the external debugger facilities, but I have only tried once or twice.

If you do have an external debugger attached, you don't need embedded int3's to start with, and you'd still hit the debugger_trap_*() calls before dying.

If you don't have an external debugger attached, ignoring int3's is very poor behaviour. The fact that it ever worked is a bug IMO.

~Andrew

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

#5 lore raw
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 30.03.17 at 19:09, <[email protected]> wrote:

If you do have an external debugger attached, you don't need embedded int3's to start with, and you'd still hit the debugger_trap_*() calls before dying.

Well, my experience (on other OSes with a better debugger) is different - there are occasions where it is easier to embed an int3 right away than to break into the system (doing of which may be a problem of its own if you need the breakpoint quite early during boot), go hunt for the precise instruction you want the breakpoint on, and then set the intended breakpoint using debugger facilities.

Quoted 2 lines

If you don't have an external debugger attached, ignoring int3's is very poor behaviour.

Well, that's your opinion. Mine (again based on past experience) is different, and it appears to match that of whoever originally wrote things this way. Nevertheless I appreciate the (assumed) reasoning behind your way of thinking.

Jan

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