Skip to content
Permalink
Browse files Browse the repository at this point in the history
virtio: properly validate address before accessing config
There are several several issues in the current checking:

- The check was based on the minus of unsigned values which can overflow
- It was done after .{set|get}_config() which can lead crash when config_len
  is zero since vdev->config is NULL

Fix this by:

- Validate the address in virtio_pci_config_{read|write}() before
  .{set|get}_config
- Use addition instead minus to do the validation

Cc: Michael S. Tsirkin <mst@redhat.com>
Cc: Petr Matousek <pmatouse@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Petr Matousek <pmatouse@redhat.com>
Message-id: 1367905369-10765-1-git-send-email-jasowang@redhat.com
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
  • Loading branch information
jasowang authored and Anthony Liguori committed May 8, 2013
1 parent 62c9636 commit 5f5a131
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions hw/virtio/virtio.c
Expand Up @@ -568,10 +568,11 @@ uint32_t virtio_config_readb(VirtIODevice *vdev, uint32_t addr)
VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
uint8_t val;

k->get_config(vdev, vdev->config);

if (addr > (vdev->config_len - sizeof(val)))
if (addr + sizeof(val) > vdev->config_len) {
return (uint32_t)-1;
}

k->get_config(vdev, vdev->config);

val = ldub_p(vdev->config + addr);
return val;
Expand All @@ -582,10 +583,11 @@ uint32_t virtio_config_readw(VirtIODevice *vdev, uint32_t addr)
VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
uint16_t val;

k->get_config(vdev, vdev->config);

if (addr > (vdev->config_len - sizeof(val)))
if (addr + sizeof(val) > vdev->config_len) {
return (uint32_t)-1;
}

k->get_config(vdev, vdev->config);

val = lduw_p(vdev->config + addr);
return val;
Expand All @@ -596,10 +598,11 @@ uint32_t virtio_config_readl(VirtIODevice *vdev, uint32_t addr)
VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
uint32_t val;

k->get_config(vdev, vdev->config);

if (addr > (vdev->config_len - sizeof(val)))
if (addr + sizeof(val) > vdev->config_len) {
return (uint32_t)-1;
}

k->get_config(vdev, vdev->config);

val = ldl_p(vdev->config + addr);
return val;
Expand All @@ -610,8 +613,9 @@ void virtio_config_writeb(VirtIODevice *vdev, uint32_t addr, uint32_t data)
VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
uint8_t val = data;

if (addr > (vdev->config_len - sizeof(val)))
if (addr + sizeof(val) > vdev->config_len) {
return;
}

stb_p(vdev->config + addr, val);

Expand All @@ -625,8 +629,9 @@ void virtio_config_writew(VirtIODevice *vdev, uint32_t addr, uint32_t data)
VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
uint16_t val = data;

if (addr > (vdev->config_len - sizeof(val)))
if (addr + sizeof(val) > vdev->config_len) {
return;
}

stw_p(vdev->config + addr, val);

Expand All @@ -640,8 +645,9 @@ void virtio_config_writel(VirtIODevice *vdev, uint32_t addr, uint32_t data)
VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev);
uint32_t val = data;

if (addr > (vdev->config_len - sizeof(val)))
if (addr + sizeof(val) > vdev->config_len) {
return;
}

stl_p(vdev->config + addr, val);

Expand Down

0 comments on commit 5f5a131

Please sign in to comment.