x86emul: Zero-extend unsigned 8-bit immediates when decoding
PATCH v3 1/1 x86emul: Zero-extend unsigned 8-bit immediates when decoding
The decoder sign-extends all 8-bit immediates. This is right for e.g. Jcc rel8 and Grp1 (83), but some 8-bit immediates are unsigned: the vector of INT, the port of IN/OUT, the base of AAM/AAD, the count of the Grp2 shifts (C0, C1), and the 8-bit immediates in the 0F, 0F3A and XOP08 maps. For these, s->imm1 holds a wrong value for 0x80 and above. x86_insn_immediate() can return this value too.
The worst case is INT $0x80 to INT $0xff, which puts a negative vector into the event (e.g. -128 for 0x80). hvm_inject_event() then fails its "vector == event->vector" assertion, and a debug Xen panics. An HVM guest can trigger this with the forced emulation prefix (hvm_fep).
Zero-extend these immediates in the decoder. At the IN/OUT and AAM/AAD use sites, replace the (uint8_t) casts, which are not needed any more, with ASSERT()s.
---
v3:
- Move the new case block down, to keep the case blocks sorted (Jan).
- Add a comment for the 0F, 0F3A and XOP08 maps (Jan).
- Use "&= 0xff" instead of casts (Jan).
- Add ASSERT()s at the IN/OUT and AAM/AAD use sites (Jan).
v2:
- Fix this in the decoder instead of at the INT use site, and also cover the IN/OUT, AAM/AAD and C0/C1 immediates, plus the 0F, 0F3A and XOP08 maps (Jan).
- Drop the (uint8_t) casts at the IN/OUT and AAM/AAD use sites (Jan).
- Add a Fixes: tag for the commit that added x86_insn_immediate() (Jan).
- Drop Ross's R-b (patch changed a lot).
v2: https://lore.kernel.org/xen-devel/[email protected]/
v1: https://lore.kernel.org/xen-devel/[email protected]/
xen/arch/x86/x86_emulate/decode.c | 20 ++++++++++++++++++-- xen/arch/x86/x86_emulate/private.h | 2 +- xen/arch/x86/x86_emulate/x86_emulate.c | 6 ++++-- 3 files changed, 23 insertions(+), 5 deletions(-)
xen/arch/x86/x86_emulate/decode.c +18 -2
@@ -650,8 +650,6 @@ decode_onebyte(struct x86_emulate_state *s,case 0xc4 :/* les */ case 0xc5 :/* lds */ case 0xce :/* into */ -case 0xd4 :/* aam */ -case 0xd5 :/* aad */ case 0xd6 :/* salc / udb */ s->not_64bit = true;break ;
@@ -690,6 +688,17 @@ decode_onebyte(struct x86_emulate_state *s, ((uint64_t)insn_fetch_type(uint32_t) << 32));break ; +case 0xd4 :/* aam */ +case 0xd5 :/* aad */ + s->not_64bit = true; + fallthrough; +case 0xc0 :case 0xc1 :/* Grp2 */ +case 0xcd :/* int imm8 */ + case 0xe4 ... 0xe7: /* in/out imm8 */ +/* The 8-bit immediate is unsigned. */ + s->imm1 &=0xff ; +break ; +case 0xc8 :/* enter imm16,imm8 */ s->imm2 =insn_fetch_type (uint8_t);break ;
@@ -1796,6 +1805,9 @@ int x86emul_decode(struct x86_emulate_state *s,break ;case ext_0f: +/* All immediates in this encoding space are unsigned ones. */ +if ( (d & SrcMask) == SrcImmByte ) + s->imm1 &=0xff ; rc =decode_twobyte (s, ctxt, ops);break ;
@@ -1804,6 +1816,8 @@ int x86emul_decode(struct x86_emulate_state *s,break ;case ext_0f3a: +/* All immediates in this encoding space are unsigned ones. */ + s->imm1 &=0xff ; d = ext0f3a_table[b].to_mem ? DstMem | SrcReg : DstReg | SrcMem;if ( ext0f3a_table[b].two_op ) d |= TwoOp;
@@ -1814,6 +1828,8 @@ int x86emul_decode(struct x86_emulate_state *s,break ;case ext_8f08: +/* All immediates in this encoding space are unsigned ones. */ + s->imm1 &=0xff ; d = DstReg | SrcMem;if ( ext8f08_table[b].two_op ) d |= TwoOp;
xen/arch/x86/x86_emulate/private.h +1 -1
@@ -62,7 +62,7 @@ void BUG(void);#define SrcMem (2<<3)/* Memory operand. */ #define SrcMem16 (3<<3)/* Memory operand (16-bit). */ #define SrcImm (4<<3)/* Immediate operand. */ -#define SrcImmByte (5<<3)/* 8-bit sign-extended immediate operand. */ +#define SrcImmByte (5<<3)/* 8-bit immediate operand. */ #define SrcImm16 (6<<3)/* 16-bit zero-extended immediate operand. */ #define SrcMask (7<<3)/* Generic ModRM decode. */
xen/arch/x86/x86_emulate/x86_emulate.c +4 -2
@@ -2505,7 +2505,8 @@ x86_emulate(case 0xd4 :/* aam */ case 0xd5 :/* aad */ - n = (uint8_t )src.val; +ASSERT (src.val <=0xff ); + n = src.val;if ( b &0x01 ) _regs.ax = (uint8_t )(_regs.al + (_regs.ah * n));else
@@ -2561,8 +2562,9 @@ x86_emulate(case 0xed :/* in %dx,%eax */ case 0xee :/* out %al,%dx */ case 0xef :/* out %eax,%dx */ { -unsigned int port = ((b <0xe8 ) ? (uint8_t )src.val : _regs.dx); +unsigned int port = ((b <0xe8 ) ? src.val : _regs.dx); +ASSERT (b >=0xe8 || src.val <=0xff ); op_bytes = !(b &1 ) ?1 : (op_bytes ==8 ) ?4 : op_bytes;if ( (rc =ioport_access_check (port, op_bytes, ctxt, ops)) !=0 ) goto done;
Signature · 1 line
2.49.0
Quoted 12 lines · Stephen Cheng
The decoder sign-extends all 8-bit immediates. This is right for e.g. Jcc rel8 and Grp1 (83), but some 8-bit immediates are unsigned: the vector of INT, the port of IN/OUT, the base of AAM/AAD, the count of the Grp2 shifts (C0, C1), and the 8-bit immediates in the 0F, 0F3A and XOP08 maps. For these,
s->imm1holds a wrong value for0x80and above.x86_insn_immediate()can return this value too.The worst case is INT $0x80 to INT $0xff, which puts a negative vector into the event (e.g. -128 for
0x80).hvm_inject_event()then fails its "vector ==event->vector" assertion, and a debug Xen panics. An HVM guest can trigger this with the forced emulation prefix (hvm_fep).
Quoted 8 lines
Zero-extend these immediates in the decoder. At the IN/OUT and AAM/AAD use sites, replace the (
uint8_t) casts, which are not needed any more, with ASSERT()s.Fixes 653cae725cea ("x86/32on64: use generic instruction decoding for call gate emulation")Fixes 52053d5fc2f9 ("x86/emul: Drop swint_emulate infrastructure")Assisted-by Claude:claude-opus-5-5Signed-off-by Stephen Cheng <[email protected]>
xen/arch/x86/x86_emulate/decode.c int x86emul_decode(struct x86_emulate_state *s, · line 1823
Quoted 9 lines
xen/arch/x86/x86_emulate/decode.c +2
-0@@ -1804,6 +1816,8 @@ int x86emul_decode(struct x86_emulate_state *s,break ;case ext_0f3a: +/* All immediates in this encoding space are unsigned ones. */ + s->imm1 &=0xff ; d = ext0f3a_table[b].to_mem ? DstMem | SrcReg : DstReg | SrcMem;if ( ext0f3a_table[b].two_op ) d |= TwoOp;
RORX in this space is interesting. Both AMD and Intel implicitly say the immediate there is unsigned (by clarifying that the upper few bits are ignored), but (largely like for VPROTx below, just with directions reversed) one can view negative values are rotates to the left.
xen/arch/x86/x86_emulate/decode.c int x86emul_decode(struct x86_emulate_state *s, · line 1835
Quoted 9 lines
xen/arch/x86/x86_emulate/decode.c +2
-0@@ -1814,6 +1828,8 @@ int x86emul_decode(struct x86_emulate_state *s,break ;case ext_8f08: +/* All immediates in this encoding space are unsigned ones. */ + s->imm1 &=0xff ; d = DstReg | SrcMem;if ( ext8f08_table[b].two_op ) d |= TwoOp;
I think I'll adjust this comment slightly: For VPROTx one can look at the immediate as both signed and unsigned: "When the count value is negative, bits are rotated to the right". I.e. I'll do s/are/can be viewed as/.
Would you further be up to following up with another, smaller patch dealing with a similar issue? XOP0A has a 32-bit immediate, which we (apparently wrongly) sign-extend, as all three insns there use only the low 16 bits anyway. (This only affects x86_insn_immediate() afaics.) This may extend elsewhere, so the scope of this isn't entirely clear just
yet: For example, MOV (opcode B8) in 64-bit mode without REX.W zero- extends its immediate. The zero-extension we do in decode_onebyte() for the REX.W case wants pulling out. The 16-bit forms may also better zero- extend their immediates.
While benign there (for now), I'll adjust the JMPABS patch in the APX series accordingly as well. Similarly Map7 handling will want to have the immediate zero-extended (for now uniformly), even if again only for x86_insn_immediate()'s sake.
Jan
Quoted 3 lines · Jan Beulich
I think I'll adjust this comment slightly: For VPROTx one can look at the immediate as both signed and unsigned: "When the count value is negative, bits are rotated to the right". I.e. I'll do s/are/can be viewed as/.
Thanks for the review. That's fine with me.
Quoted 2 lines
Would you further be up to following up with another, smaller patch dealing with a similar issue?
Yes, I can do that. I'll start with XOP0A and then look at the other cases you mention.
Stephen