vmstate_gpadl allocates gpadl->gfns with num_gfns elements and loads
seen_gfns as a raw uint32_t. If seen_gfns > num_gfns, the unsigned
subtraction num_gfns - seen_gfns in handle_gpadl_body() wraps around
and writes past gpadl->gfns[].

Reject seen_gfns > num_gfns in a post_load hook on vmstate_gpadl.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4776
Signed-off-by: lbyxiaolizi <[email protected]>
---
 hw/hyperv/vmbus.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/hw/hyperv/vmbus.c b/hw/hyperv/vmbus.c
index ab99f08..3973ca8 100644
--- a/hw/hyperv/vmbus.c
+++ b/hw/hyperv/vmbus.c
@@ -531,10 +531,21 @@ void vmbus_unmap_sgl(VMBusChanReq *req, DMADirection dir, 
struct iovec *iov,
     }
 }
 
+static int vmbus_gpadl_post_load(void *opaque, int version_id)
+{
+    VMBusGpadl *gpadl = opaque;
+
+    if (gpadl->seen_gfns > gpadl->num_gfns) {
+        return -EINVAL;
+    }
+    return 0;
+}
+
 static const VMStateDescription vmstate_gpadl = {
     .name = "vmbus/gpadl",
     .version_id = 0,
     .minimum_version_id = 0,
+    .post_load = vmbus_gpadl_post_load,
     .fields = (const VMStateField[]) {
         VMSTATE_UINT32(id, VMBusGpadl),
         VMSTATE_UINT32(child_relid, VMBusGpadl),

Reply via email to