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 Signed-off-by: Hang Nan <2122295973@qq.com> 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(+) diff --git a/virt/kvm/coalesced_mmio.c b/virt/kvm/coalesced_mmio.c index 000000000000..000000000000 100644 --- a/virt/kvm/coalesced_mmio.c +++ b/virt/kvm/coalesced_mmio.c @@ -14,6 +14,7 @@ #include #include #include +#include #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;