KVM: x86: Add IDT limit check in __emulate_int_real()
PATCH 1/1 KVM: x86: Add IDT limit check in __emulate_int_real()
During real mode interrupt emulation, KVM currently pushes data to the stack before checking IDT limits.
This patch fetches the IDT early and injects a #GP if the limit is exceeded. This prevents potential corruption.
--- arch/x86/kvm/emulate.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +5 -3
@@ -2022,7 +2022,11 @@ static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)gva_t eip_addr;u16 cs, eip; -/* TODO: Add limit checks */ + ops->get_idt (ctxt, &dt); + +if (dt.size < (irq <<2 ) +3 ) +return emulate_gp (ctxt,0 ); + ctxt->src.val = ctxt->eflags; rc =em_push (ctxt);if (rc !=X86EMUL_CONTINUE )
@@ -2040,8 +2044,6 @@ static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)if (rc !=X86EMUL_CONTINUE )return rc; - ops->get_idt (ctxt, &dt); - eip_addr = dt.address + (irq <<2 ); cs_addr = dt.address + (irq <<2 ) +2 ;
Signature · 1 line
2.54.0
PATCH 1/1 Linux 7.1-rc7
From: Linus Torvalds <[email protected]>
--- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Makefile +1 -1
@@ -2,7 +2,7 @@ VERSION = 7 PATCHLEVEL = 1 SUBLEVEL = 0 -EXTRAVERSION = -rc6 +EXTRAVERSION = -rc7 NAME = Baby Opossum Posse # *DOCUMENTATION*
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] IDT limit check is bypassed for software interrupt vectors >= 128 due to sign-extension, leaving an out-of-bounds memory read and undefined behavior unaddressed. --
commit 1d15914e95742042e4c5113eceda0a57155cbdd4
Author: Kayra Cizmeci <[email protected]>
KVM: x86: Add IDT limit check in __emulate_int_real()
During real mode interrupt emulation, KVM currently pushes data to the stack before checking IDT limits. This commit fetches the IDT early and injects a #GP if the limit is exceeded.
arch/x86/kvm/emulate.c static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq) · line 2015
Quoted 13 lines
arch/x86/kvm/emulate.c +4
-1@@ -2009,7 +2009,11 @@ static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)gva_t eip_addr;u16 cs, eip; -/* TODO: Add limit checks */ + ops->get_idt (ctxt, &dt); + +if (dt.size < (irq <<2 ) +3 ) +return emulate_gp (ctxt,0 );
[Severity: High]
This is a pre-existing issue, but does this check protect against software interrupt vectors >= 128?
The KVM instruction decoder treats the immediate operand of the INT n instruction (opcode 0xcd) as a sign-extended byte (SrcImmByte in the opcode table). This causes the decoder to sign-extend vectors like INT 0x80 to a negative number.
When a negative vector is passed to __emulate_int_real() as the irq parameter, (irq << 2) results in undefined behavior due to left-shifting a negative integer.
Furthermore, dt.size is an unsigned short, which promotes to a signed int during the comparison. If irq is negative, the right-hand side of the comparison is negative, causing the dt.size check to evaluate to false and silently bypass the intended protection.
This would then allow the emulator to calculate a negative offset for dt.address + (irq << 2) and read out-of-bounds memory.
Could the decoder be updated to use SrcImmUByte for opcode 0xcd, or could the irq parameter be handled as an unsigned value to prevent this bypass?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 KVM: x86: Add IDT limit check in __emulate_int_real()
In reply to Kayra CizmeciDuring real mode interrupt emulation, KVM currently pushes data to the stack before checking IDT limits.
This patch fetches the IDT early and injects a #GP if the limit is exceeded. This prevents potential corruption.
--- arch/x86/kvm/emulate.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/kvm/emulate.c +6 -4
@@ -2022,7 +2022,11 @@ static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)gva_t eip_addr;u16 cs, eip; -/* TODO: Add limit checks */ + ops->get_idt (ctxt, &dt); + +if (dt.size < (irq <<2 ) +3 ) +return emulate_gp (ctxt,0 ); + ctxt->src.val = ctxt->eflags; rc =em_push (ctxt);if (rc !=X86EMUL_CONTINUE )
@@ -2040,8 +2044,6 @@ static int __emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq)if (rc !=X86EMUL_CONTINUE )return rc; - ops->get_idt (ctxt, &dt); - eip_addr = dt.address + (irq <<2 ); cs_addr = dt.address + (irq <<2 ) +2 ;
@@ -4324,7 +4326,7 @@ static const struct opcode opcode_table[256] = {I (Stack, em_leave),I (ImplicitOps | SrcImmU16 | IsBranch | ShadowStack, em_ret_far_imm),I (ImplicitOps | IsBranch | ShadowStack, em_ret_far), -D (ImplicitOps | IsBranch),DI (SrcImmByte | IsBranch | ShadowStack, intn), +D (ImplicitOps | IsBranch),DI (SrcImmUByte | IsBranch | ShadowStack, intn),D (ImplicitOps | No64 | IsBranch), II(ImplicitOps | IsBranch | ShadowStack, em_iret, iret),/* 0xD0 - 0xD7 */
Signature · 1 line
2.54.0
Hello, just following on this patch. I wanted to also add some missing details.
The reason that SrcImmByte is changed to SrcImmUByte is when the vectors are bigger than 127, become negative. And our calculations for the limit check does not work. So with this change this problem is solved. Also I accidentally sent out Linux 7.1-rc7 patch. Sorry for that and skipping the details at first place.