diff mbox series

[v2] x86: apic: pv ipis out-of-bounds access testing

Message ID 1535537545-31023-1-git-send-email-wanpengli@tencent.com (mailing list archive)
State New, archived
Headers show
Series [v2] x86: apic: pv ipis out-of-bounds access testing | expand

Commit Message

Wanpeng Li Aug. 29, 2018, 10:12 a.m. UTC
From: Wanpeng Li <wanpengli@tencent.com>

Dan Carpenter reported that the untrusted data returns from kvm_register_read()
results in the following static checker warning:
  arch/x86/kvm/lapic.c:576 kvm_pv_send_ipi()
  error: buffer underflow 'map->phys_map' 's32min-s32max'

KVM guest can easily trigger this by executing the following assembly sequence 
in Ring0:

mov $10, %rax
mov $0xFFFFFFFF, %rbx
mov $0xFFFFFFFF, %rdx
mov $0, %rsi
vmcall

As this will cause KVM to execute the following code-path:
vmx_handle_exit() -> handle_vmcall() -> kvm_emulate_hypercall() -> kvm_pv_send_ipi()
which will reach out-of-bounds access.

This patch adds the testcase to kvm-unit-tests.

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Cc: Liran Alon <liran.alon@oracle.com>
Reviewed-By: Liran Alon <liran.alon@oracle.com>
Signed-off-by: Wanpeng Li <wanpengli@tencent.com>
---
v1 -> v2:
 * add more changelog
 * arguments are documented in Documentation/virtual/kvm/hypercalls.txt
 * Define KVM_HC_SEND_IPI instead of using hard-coded

 x86/apic.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
diff mbox series

Patch

diff --git a/x86/apic.c b/x86/apic.c
index c976e8d..6c6ed20 100644
--- a/x86/apic.c
+++ b/x86/apic.c
@@ -566,6 +566,17 @@  static void test_apic_change_mode(void)
 	report("TMCCT should stay at zero", !apic_read(APIC_TMCCT));
 }
 
+#define KVM_HC_SEND_IPI 10
+
+void test_pv_ipi()
+{
+    int ret;
+    unsigned long a0 = 0xFFFFFFFF, a1 = 0, a2 = 0xFFFFFFFF, a3 = 0x0;
+
+    asm volatile("vmcall" : "=a"(ret) :"a"(KVM_HC_SEND_IPI), "b"(a0), "c"(a1), "d"(a2), "S"(a3));
+    report("PV IPIs testing", !ret);
+}
+
 int main(void)
 {
     setup_vm();
@@ -581,6 +592,7 @@  int main(void)
 
     test_self_ipi();
     test_physical_broadcast();
+    test_pv_ipi();
 
     test_sti_nmi();
     test_multiple_nmi();