Skip to contentSkip to conversation

KVM: coalesced_mmio: reject zone sizes that don't fit in an int

PATCH Superseded Hang Nan 2 participants 2 messages Oct 24 – Oct 27 KVM

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Hang Nan

author
#0 lore raw

PATCH 1/1 KVM: coalesced_mmio: reject zone sizes that don't fit in an int

kvm_vm_ioctl_register_coalesced_mmio() validates @zone->pio but not @zone->size. The u32 size is handed to kvm_io_bus_register_dev() as an "int len", so any size >= 0x80000000 silently becomes negative.

coalesced_mmio_in_range() bails out immediately for len < 0, so such a zone never matches its own range:

- coalesced MMIO writes for the zone are no longer coalesced and are

    forwarded to userspace instead;

- KVM_UNREGISTER_COALESCED_MMIO returns 0 but does not remove the zone, because the unregister path uses coalesced_mmio_in_range() for matching too, leaving the kvm_io_device permanently registered;
- the leaked device occupies a slot on the MMIO/PIO bus. After NR_IOBUS_DEVS - 1 (998) such zones every further kvm_io_bus_register_dev() on that bus fails with -ENOSPC, including ioeventfds and valid coalesced zones, effectively killing the VM's device model until the VM is destroyed.

A negative len additionally breaks the sorted-interval invariant used by kvm_io_bus_cmp(), which computes the end of an interval as "addr + len".

ioeventfd rejects lengths outside {0, 1, 2, 4, 8}, so the same ioctl family cannot be abused this way; coalesced MMIO has no equivalent check.

Reject the ioctl with -EINVAL instead. The check has never existed (git log -S 'INT_MAX' -- virt/kvm/coalesced_mmio.c is empty), so there is no Fixes: tag; this is not a regression.

Reproducer, unprivileged on /dev/kvm (x86_64, v7.3-rc):

- KVM_CREATE_VM
- KVM_REGISTER_COALESCED_MMIO { addr = 0x1000, size = 0x80000000,

                                  pio = 0 }            -> 0 (accepted)
  - KVM_UNREGISTER_COALESCED_MMIO { same }             -> 0 (zone stays)
  - repeat 998 times, then register a valid zone       -> -ENOSPC

A PoC plus the serial log from a KASAN kernel built from v7.3-11778-g5b0764a53235 is available on request (it is ~150 lines of static C that only uses /dev/kvm).

---
 virt/kvm/coalesced_mmio.c | 4 ++++
 1 file changed, 4 insertions(+)
virt/kvm/coalesced_mmio.c +5 -0
index 000000000000..000000000000 100644
@@ -14,6 +14,7 @@
 #include <linux/kvm_host.h>
 #include <linux/slab.h>
 #include <linux/kvm.h>
+#include <linux/limits.h>
 
 #ifndef __KVM_NO_MMIO
 
@@ -130,6 +131,10 @@
 	if (zone->pio != 1 && zone->pio != 0)
 		return -EINVAL;
 
+	/* kvm_io_bus_register_dev() takes an int, don't let the u32 wrap */
+	if (zone->size > INT_MAX)
+		return -EINVAL;
+
 	dev = kzalloc_obj(struct kvm_coalesced_mmio_dev, GFP_KERNEL_ACCOUNT);
 	if (!dev)
 		return -ENOMEM;
#1 lore raw

In the future, please use scripts/get_maintainer.pl.

Quoted 28 lines · Hang Nan

kvm_vm_ioctl_register_coalesced_mmio() validates @zone->pio but not @zone->size. The u32 size is handed to kvm_io_bus_register_dev() as an "int len", so any size >= 0x80000000 silently becomes negative.

coalesced_mmio_in_range() bails out immediately for len < 0, so such a zone never matches its own range:

- coalesced MMIO writes for the zone are no longer coalesced and are

    forwarded to userspace instead;

- KVM_UNREGISTER_COALESCED_MMIO returns 0 but does not remove the zone, because the unregister path uses coalesced_mmio_in_range() for matching too, leaving the kvm_io_device permanently registered;
- the leaked device occupies a slot on the MMIO/PIO bus. After NR_IOBUS_DEVS - 1 (998) such zones every further kvm_io_bus_register_dev() on that bus fails with -ENOSPC, including ioeventfds and valid coalesced zones, effectively killing the VM's device model until the VM is destroyed.

A negative len additionally breaks the sorted-interval invariant used by kvm_io_bus_cmp(), which computes the end of an interval as "addr + len".

ioeventfd rejects lengths outside {0, 1, 2, 4, 8}, so the same ioctl family cannot be abused this way; coalesced MMIO has no equivalent check.

Reject the ioctl with -EINVAL instead. The check has never existed (git log -S 'INT_MAX' -- virt/kvm/coalesced_mmio.c is empty), so there is no Fixes: tag; this is not a regression.

Quoted 7 lines

Reproducer, unprivileged on /dev/kvm (x86_64, v7.3-rc):

- KVM_CREATE_VM
- KVM_REGISTER_COALESCED_MMIO { addr = 0x1000, size = 0x80000000,

                                  pio = 0 }            -> 0 (accepted)
  - KVM_UNREGISTER_COALESCED_MMIO { same }             -> 0 (zone stays)
  - repeat 998 times, then register a valid zone       -> -ENOSPC
Fixes 743eeb0b01d2 ("KVM: Intelligent device lookup on I/O bus")
Quoted 3 lines

A PoC plus the serial log from a KASAN kernel built from

LOL, the above pretty much spills the beans...

Quoted 19 lines

v7.3-11778-g5b0764a53235 is available on request (it is ~150 lines of static C that only uses /dev/kvm).

---
 virt/kvm/coalesced_mmio.c | 4 ++++
 1 file changed, 4 insertions(+)
virt/kvm/coalesced_mmio.c +1 -0
index 000000000000..000000000000 100644
@@ -14,6 +14,7 @@
 #include <linux/kvm_host.h>
 #include <linux/slab.h>
 #include <linux/kvm.h>
+#include <linux/limits.h>
 
 #ifndef __KVM_NO_MMIO
 

virt/kvm/coalesced_mmio.c line 134

Quoted 4 lines
virt/kvm/coalesced_mmio.c +1 -0
@@ -130,6 +131,10 @@
 	if (zone->pio != 1 && zone->pio != 0)
 		return -EINVAL;
 
+	/* kvm_io_bus_register_dev() takes an int, don't let the u32 wrap */

Eh, I'd omit the comment, the code is self-explanatory. And it's not just kvm_io_bus_register_dev() that's problematic, e.g. we can't simply change that code to take a u32, because "struct kvm_io_range" also tracks a signed int, and I'm sure there's code spread throughout KVM that compares against signed values.

Actually, kvm_vm_ioctl_unregister_coalesced_mmio() has the same validation issue, it just gets papered over by coalesced_mmio_in_range() thanks to commit 1a214246cbb4 ("KVM: make checks stricter in coalesced_mmio_in_range()"). We should take the opportunity to clean that up as well.

E.g. this?

virt/kvm/coalesced_mmio.c +13 -2
index 3b5430036cae..4afa86b72cf6 100644
@@ -121,13 +121,24 @@ void kvm_coalesced_mmio_free(struct kvm *kvm)
 		free_page((unsigned long)kvm->coalesced_mmio_ring);
 }
 
+static bool kvm_is_valid_io_zone(struct kvm_coalesced_mmio_zone *zone)
+{
+	if (zone->pio != 1 && zone->pio != 0)
+		return false;
+
+	if (zone->size > INT_MAX)
+		return false;
+
+	return true;
+}
+
 int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
 					 struct kvm_coalesced_mmio_zone *zone)
 {
 	int ret;
 	struct kvm_coalesced_mmio_dev *dev;
 
-	if (zone->pio != 1 && zone->pio != 0)
+	if (!kvm_is_valid_io_zone(zone))
 		return -EINVAL;
 
 	dev = kzalloc_obj(struct kvm_coalesced_mmio_dev, GFP_KERNEL_ACCOUNT);
@@ -162,7 +173,7 @@ int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
 	struct kvm_coalesced_mmio_dev *dev, *tmp;
 	int r;
 
-	if (zone->pio != 1 && zone->pio != 0)
+	if (!kvm_is_valid_io_zone(zone))
 		return -EINVAL;
 
 	mutex_lock(&kvm->slots_lock);