Skip to contentSkip to conversation

Assert in x86_emulate_wrapper triggerable by HVM domain

DISCUSSION Manuel Andreas 2 participants 5 messages Apr 16, 2025 – May 7, 2025

lore
Share & tools
lore
#0 lore raw

Dear all,

my fuzzing infrastructure discovered that an assert in x86_emulate_wrapper is able to be triggered by an HVM domain executing a specially crafted repeating movs instruction.

Specifically, if the emulation of the rep movs instruction triggers an exception (e.g. by accessing invalid memory after some amount of iterations), the emulation will be halted at that point.
However, the instruction manual requires that _some_ register state (namely the updated value of rcx) shall be commited, whereas the instruction pointer needs to be rolled back to point to the address of the instruction itself. The assert checks for the latter. Problematic is the fact that for these type of repeating instructions, Xen seems to eventually just commit all register state when it encounters an exception:

   557  #define put_rep_prefix(reps_completed) ({                               \
   558      if ( rep_prefix() )                                                 \
   559 { \
   560          __put_rep_prefix(&_regs, ctxt->regs, ad_bytes, reps_completed); \
   561          if ( unlikely(rc == X86EMUL_EXCEPTION) )                        \
   562              goto complete_insn;                                         \
   563 } \
   564  })

  8356   complete_insn: /* Commit shadow register state. */ 8357      put_fpu(fpu_type, false, state, ctxt, ops);
  8358      fpu_type = X86EMUL_FPU_none;
  8359
  8360      /* Zero the upper 32 bits of %rip if not in 64-bit mode. */ 8361      if ( !mode_64bit() )
  8362          _regs.r(ip) = (uint32_t)_regs.r(ip);
  8363
  8364      /* Should a singlestep #DB be raised? */
  8365      if ( rc == X86EMUL_OKAY && singlestep && !ctxt->retire.mov_ss ) 8366      {
  8367          ctxt->retire.singlestep = true;
  8368          ctxt->retire.sti = false;
  8369      }
  8370
  8371      if ( rc != X86EMUL_DONE )
  8372          *ctxt->regs = _regs; // <- Incorrect RIP is commited

I've attached an XTF test that should trigger the aforementioned assert on the latest release commit: 3ad5d648cda5add395f49fc3704b2552aae734f7

Best,
Manuel

#1 lore raw
Quoted 5 lines · Manuel Andreas

Dear all,

my fuzzing infrastructure discovered that an assert in x86_emulate_wrapper is able to be triggered by an HVM domain executing a specially crafted repeating movs instruction.

That's

    /* All cases returning X86EMUL_EXCEPTION should have fault semantics. */
    if ( rc == X86EMUL_EXCEPTION )
        ASSERT(ctxt->regs->r(ip) == orig_ip);

?

Quoted 10 lines

Specifically, if the emulation of the rep movs instruction triggers an exception (e.g. by accessing invalid memory after some amount of iterations), the emulation will be halted at that point. However, the instruction manual requires that _some_ register state (namely the updated value of rcx) shall be commited, whereas the instruction pointer needs to be rolled back to point to the address of the instruction itself. The assert checks for the latter. Problematic is the fact that for these type of repeating instructions, Xen seems to eventually just commit all register state when it encounters an exception:

Quoted 13 lines

   557  #define put_rep_prefix(reps_completed)
({                               \
   558      if ( rep_prefix()
)                                                 \
   559 { \
   560          __put_rep_prefix(&_regs, ctxt->regs, ad_bytes,
reps_completed); \
   561          if ( unlikely(rc == X86EMUL_EXCEPTION)
)                        \
   562              goto
complete_insn;                                         \
   563 } \
   564  })

I fear I'm not following, considering __put_rep_prefix() has

    /* Reduce counter appropriately, and repeat instruction if non-zero. */
    ecx -= reps_completed;
    if ( ecx != 0 )
        int_regs->r(ip) = ext_regs->r(ip);
Quoted 18 lines

  8356   complete_insn: /* Commit shadow register state. */ 8357      put_fpu(fpu_type, false, state, ctxt, ops);
  8358      fpu_type = X86EMUL_FPU_none;
  8359
  8360      /* Zero the upper 32 bits of %rip if not in 64-bit mode. */ 8361      if ( !mode_64bit() )
  8362          _regs.r(ip) = (uint32_t)_regs.r(ip);
  8363
  8364      /* Should a singlestep #DB be raised? */
  8365      if ( rc == X86EMUL_OKAY && singlestep && !ctxt->retire.mov_ss ) 8366      {
  8367          ctxt->retire.singlestep = true;
  8368          ctxt->retire.sti = false;
  8369      }
  8370
  8371      if ( rc != X86EMUL_DONE )
  8372          *ctxt->regs = _regs; // <- Incorrect RIP is commited

Quoted 2 lines

I've attached an XTF test that should trigger the aforementioned assert on the latest release commit: 3ad5d648cda5add395f49fc3704b2552aae734f7

I'll give this a try, perhaps it'll shed more light on the situation. I'd be surprised though that the fuzzer we have in tree wouldn't have found this (so far).

Jan

#2 lore raw
In reply to Manuel Andreas
Quoted 4 lines · Manuel Andreas

my fuzzing infrastructure discovered that an assert in x86_emulate_wrapper is able to be triggered by an HVM domain executing a specially crafted repeating movs instruction.

Quoted 9 lines

Specifically, if the emulation of the rep movs instruction triggers an exception (e.g. by accessing invalid memory after some amount of iterations), the emulation will be halted at that point. However, the instruction manual requires that _some_ register state (namely the updated value of rcx) shall be commited, whereas the instruction pointer needs to be rolled back to point to the address of the instruction itself. The assert checks for the latter. Problematic is the fact that for these type of repeating instructions, Xen seems to eventually just commit all register state when it encounters an exception:

If my analysis is correct, none of this matters here; the core emulator is working correctly. Hence also why the in-tree fuzzer wouldn't have caught it. Would you please give the patch a try that I just sent, with Cc to you (sorry, the list archive didn't pick it up yet, hence no link)?

Jan

Quoted 37 lines

   557  #define put_rep_prefix(reps_completed)
({                               \
   558      if ( rep_prefix()
)                                                 \
   559 { \
   560          __put_rep_prefix(&_regs, ctxt->regs, ad_bytes,
reps_completed); \
   561          if ( unlikely(rc == X86EMUL_EXCEPTION)
)                        \
   562              goto
complete_insn;                                         \
   563 } \
   564  })

  8356   complete_insn: /* Commit shadow register state. */ 8357      put_fpu(fpu_type, false, state, ctxt, ops);
  8358      fpu_type = X86EMUL_FPU_none;
  8359
  8360      /* Zero the upper 32 bits of %rip if not in 64-bit mode. */ 8361      if ( !mode_64bit() )
  8362          _regs.r(ip) = (uint32_t)_regs.r(ip);
  8363
  8364      /* Should a singlestep #DB be raised? */
  8365      if ( rc == X86EMUL_OKAY && singlestep && !ctxt->retire.mov_ss ) 8366      {
  8367          ctxt->retire.singlestep = true;
  8368          ctxt->retire.sti = false;
  8369      }
  8370
  8371      if ( rc != X86EMUL_DONE )
  8372          *ctxt->regs = _regs; // <- Incorrect RIP is commited

I've attached an XTF test that should trigger the aforementioned assert on the latest release commit: 3ad5d648cda5add395f49fc3704b2552aae734f7

Best,
Manuel

#3 lore raw
Quoted 15 lines · Jan Beulich
Quoted 4 lines · Manuel Andreas

my fuzzing infrastructure discovered that an assert in x86_emulate_wrapper is able to be triggered by an HVM domain executing a specially crafted repeating movs instruction.

Quoted 9 lines

Specifically, if the emulation of the rep movs instruction triggers an exception (e.g. by accessing invalid memory after some amount of iterations), the emulation will be halted at that point. However, the instruction manual requires that _some_ register state (namely the updated value of rcx) shall be commited, whereas the instruction pointer needs to be rolled back to point to the address of the instruction itself. The assert checks for the latter. Problematic is the fact that for these type of repeating instructions, Xen seems to eventually just commit all register state when it encounters an exception:

Quoted 4 lines

If my analysis is correct, none of this matters here; the core emulator is working correctly. Hence also why the in-tree fuzzer wouldn't have caught it. Would you please give the patch a try that I just sent, with Cc to you (sorry, the list archive didn't pick it up yet, hence no link)?

https://lists.xen.org/archives/html/xen-devel/2025-04/msg01283.html

#4 lore raw
In reply to Jan Beulich
Quoted 14 lines · Jan Beulich
Quoted 4 lines · Manuel Andreas

my fuzzing infrastructure discovered that an assert in x86_emulate_wrapper is able to be triggered by an HVM domain executing a specially crafted repeating movs instruction.

Quoted 9 lines

Specifically, if the emulation of the rep movs instruction triggers an exception (e.g. by accessing invalid memory after some amount of iterations), the emulation will be halted at that point. However, the instruction manual requires that _some_ register state (namely the updated value of rcx) shall be commited, whereas the instruction pointer needs to be rolled back to point to the address of the instruction itself. The assert checks for the latter. Problematic is the fact that for these type of repeating instructions, Xen seems to eventually just commit all register state when it encounters an exception:

Quoted 6 lines

If my analysis is correct, none of this matters here; the core emulator is working correctly. Hence also why the in-tree fuzzer wouldn't have caught it. Would you please give the patch a try that I just sent, with Cc to you (sorry, the list archive didn't pick it up yet, hence no link)?

Jan

Sorry about the late reply, just got around to applying your patch a few days ago.

I verified that the provided XTF test does not trigger the assert anymore.
Moreover, I fuzzed the patched version for a few days and the bug (or possibly newly introduced ones) did not pop up, so I believe the root cause was fixed correctly.

Best,
Manuel