* [Qemu-devel] [PATCH V2] virtio: properly validate address before accessing config
@ 2013-05-07 5:42 Jason Wang
2013-05-07 8:08 ` Michael S. Tsirkin
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Jason Wang @ 2013-05-07 5:42 UTC (permalink / raw)
To: aliguori, mst, qemu-devel; +Cc: Jason Wang, Petr Matousek
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>
---
Changes from V1:
- Doing check in virtio.c instead of virtio-pci.c
- Drop the patch of virtio-ccw and s390-virtio-bus
---
hw/virtio/virtio.c | 30 ++++++++++++++++++------------
1 files changed, 18 insertions(+), 12 deletions(-)
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
index 53a0d90..8176c14 100644
--- a/hw/virtio/virtio.c
+++ b/hw/virtio/virtio.c
@@ -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;
@@ -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;
@@ -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;
@@ -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);
@@ -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);
@@ -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);
--
1.7.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH V2] virtio: properly validate address before accessing config
2013-05-07 5:42 [Qemu-devel] [PATCH V2] virtio: properly validate address before accessing config Jason Wang
@ 2013-05-07 8:08 ` Michael S. Tsirkin
2013-05-07 8:33 ` Petr Matousek
2013-05-13 16:47 ` Anthony Liguori
2 siblings, 0 replies; 4+ messages in thread
From: Michael S. Tsirkin @ 2013-05-07 8:08 UTC (permalink / raw)
To: Jason Wang; +Cc: aliguori, Petr Matousek, qemu-devel
On Tue, May 07, 2013 at 01:42:49PM +0800, Jason Wang wrote:
> 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>
> ---
> Changes from V1:
> - Doing check in virtio.c instead of virtio-pci.c
> - Drop the patch of virtio-ccw and s390-virtio-bus
> ---
> hw/virtio/virtio.c | 30 ++++++++++++++++++------------
> 1 files changed, 18 insertions(+), 12 deletions(-)
>
> diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
> index 53a0d90..8176c14 100644
> --- a/hw/virtio/virtio.c
> +++ b/hw/virtio/virtio.c
> @@ -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;
> @@ -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;
> @@ -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;
> @@ -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);
>
> @@ -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);
>
> @@ -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);
>
> --
> 1.7.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH V2] virtio: properly validate address before accessing config
2013-05-07 5:42 [Qemu-devel] [PATCH V2] virtio: properly validate address before accessing config Jason Wang
2013-05-07 8:08 ` Michael S. Tsirkin
@ 2013-05-07 8:33 ` Petr Matousek
2013-05-13 16:47 ` Anthony Liguori
2 siblings, 0 replies; 4+ messages in thread
From: Petr Matousek @ 2013-05-07 8:33 UTC (permalink / raw)
To: Jason Wang; +Cc: aliguori, qemu-devel, mst
On Tue, May 07, 2013 at 01:42:49PM +0800, Jason Wang wrote:
> 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>
I would rather see explicit check for (addr + sizeof(val)) overflow, but
I was able to convince myself that it cannot happen due to the addr
properties.
Acked-by: Petr Matousek <pmatouse@redhat.com>
>
> ---
> Changes from V1:
> - Doing check in virtio.c instead of virtio-pci.c
> - Drop the patch of virtio-ccw and s390-virtio-bus
> ---
> hw/virtio/virtio.c | 30 ++++++++++++++++++------------
> 1 files changed, 18 insertions(+), 12 deletions(-)
>
> diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
> index 53a0d90..8176c14 100644
> --- a/hw/virtio/virtio.c
> +++ b/hw/virtio/virtio.c
> @@ -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;
> @@ -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;
> @@ -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;
> @@ -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);
>
> @@ -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);
>
> @@ -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);
>
> --
> 1.7.1
>
--
Petr Matousek / Red Hat Security Response Team
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH V2] virtio: properly validate address before accessing config
2013-05-07 5:42 [Qemu-devel] [PATCH V2] virtio: properly validate address before accessing config Jason Wang
2013-05-07 8:08 ` Michael S. Tsirkin
2013-05-07 8:33 ` Petr Matousek
@ 2013-05-13 16:47 ` Anthony Liguori
2 siblings, 0 replies; 4+ messages in thread
From: Anthony Liguori @ 2013-05-13 16:47 UTC (permalink / raw)
To: Jason Wang, aliguori, mst, qemu-devel; +Cc: Petr Matousek
Applied. Thanks.
Regards,
Anthony Liguori
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-05-13 16:47 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-07 5:42 [Qemu-devel] [PATCH V2] virtio: properly validate address before accessing config Jason Wang
2013-05-07 8:08 ` Michael S. Tsirkin
2013-05-07 8:33 ` Petr Matousek
2013-05-13 16:47 ` Anthony Liguori
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).