Skip to contentSkip to conversation

virtio-balloon: fix integer overflow in BALLOON_CHANGE QMP event

PATCH In review Luiz Capitulino 2 participants 2 messages Apr 18, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-balloon: fix integer overflow in BALLOON_CHANGE QMP event

Because dev->actual is uint32_t, the expression 'dev->actual << VIRTIO_BALLOON_PFN_SHIFT' is truncated to 32 bits. This overflows when dev->actual >= 1048576.

To reproduce:

1. Start a VM with a QMP socket and 5G of RAM
2. Connect to the QMP socket, negotiate capabilities and issue:

{ "execute":"balloon", "arguments": { "value": 1073741824 } }

3. What the BALLOON_CHANGE QMP event, the last one will incorretly be:

{ "timestamp": { "seconds": 1366228965, "microseconds": 245466 },

     "event": "BALLOON_CHANGE", "data": { "actual": 5368709120 } }

To fix it this commit casts it to ram_addr_t, which is ram_size's type.

---
 hw/virtio/virtio-balloon.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/virtio/virtio-balloon.c +1 -1
index c2c446e..76e32ce 100644
@@ -275,7 +275,7 @@ static void virtio_balloon_set_config(VirtIODevice *vdev,
     dev->actual = le32_to_cpu(config.actual);
     if (dev->actual != oldactual) {
         qemu_balloon_changed(ram_size -
-                             (dev->actual << VIRTIO_BALLOON_PFN_SHIFT));
+                       ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT));
     }
 }
 
Signature · 1 line
1.8.1.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 16 lines · Luiz Capitulino

Because dev->actual is uint32_t, the expression 'dev->actual << VIRTIO_BALLOON_PFN_SHIFT' is truncated to 32 bits. This overflows when dev->actual >= 1048576.

To reproduce:

1. Start a VM with a QMP socket and 5G of RAM
2. Connect to the QMP socket, negotiate capabilities and issue:

   { "execute":"balloon", "arguments": { "value": 1073741824 } }

3. What the BALLOON_CHANGE QMP event, the last one will incorretly be:

   { "timestamp": { "seconds": 1366228965, "microseconds": 245466 },
     "event": "BALLOON_CHANGE", "data": { "actual": 5368709120 } }
Quoted 6 lines

To fix it this commit casts it to ram_addr_t, which is ram_size's type.

---
 hw/virtio/virtio-balloon.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org