More virtio-snd fortifications/coverity fixes
v3 cover More virtio-snd fortifications/coverity fixes
Added checks for stuff coverity pointed out (CID 1547527).
---
Changes in v3:
- Added Coverity CID in patch message (thanks Alex)
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]
Changes in v2:
- Use g_size_checked_add (thanks rth)
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
---
Manos Pitsidianakis (2):
virtio-snd: check rx buffer descriptor size
virtio-snd: check for overflow before g_malloc0
hw/audio/virtio-snd.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
---
base-commit: da6c4fe60fee30dd77267764d55b38af9cb89d4b
change-id: 20260415-virtio-fixups-3bc3a1a1cd27
--
γαῖα πυρί μιχθήτω
PATCH v3 1/2 virtio-snd: check rx buffer descriptor size
It must be at least sizeof(virtio_snd_pcm_status).
I haven't verified if it's possible to get an underflow, but coverity points it out in CID 1547527 so add a check.
--- hw/audio/virtio-snd.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
hw/audio/virtio-snd.c +5 -3
@@ -970,12 +970,14 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq) } stream = vsnd->pcm.streams[stream_id]; -if (stream ==NULL || stream->info.direction !=VIRTIO_SND_D_INPUT ) { + size =iov_size (elem->in_sg, elem->in_num); +if (stream ==NULL + || stream->info.direction !=VIRTIO_SND_D_INPUT + || size <sizeof (virtio_snd_pcm_status)) { goto rx_err; } + size -=sizeof (virtio_snd_pcm_status); WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - size =iov_size (elem->in_sg, elem->in_num) - -sizeof (virtio_snd_pcm_status); buffer =g_malloc0 (sizeof (VirtIOSoundPCMBuffer) + size); buffer->elem = elem; buffer->vq = vq;
Signature · 1 line
2.47.3
PATCH v3 2/2 virtio-snd: check for overflow before g_malloc0
In reply to Manos PitsidianakisCoverity points out one g_malloc0 overflow, but it seems to be a false positive. Add a check to it regardless to fortify the code, and also add checks for every other g_malloc0 use.
Resolves: Coverity CID 1547527
--- hw/audio/virtio-snd.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
hw/audio/virtio-snd.c +12 -4
@@ -850,7 +850,7 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq)VirtIOSound *vsnd =VIRTIO_SND (vdev);VirtIOSoundPCMBuffer *buffer;VirtQueueElement *elem; -size_t msg_sz, size; +size_t msg_sz, size, tmp;virtio_snd_pcm_xfer hdr;uint32_t stream_id;/*
@@ -880,6 +880,8 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq)if (msg_sz !=sizeof (virtio_snd_pcm_xfer)) { goto tx_err; } +assert (iov_size (elem->out_sg, elem->out_num) >= msg_sz); + size =iov_size (elem->out_sg, elem->out_num) - msg_sz; stream_id =le32_to_cpu (hdr.stream_id); if (stream_id >= vsnd->snd_conf.streams
@@ -892,9 +894,11 @@ static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq) goto tx_err; } +/* Check for g_malloc0 overflow. */ +if (!g_size_checked_add (&tmp,sizeof (VirtIOSoundPCMBuffer), size)) { + goto tx_err; + } WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { - size =iov_size (elem->out_sg, elem->out_num) - msg_sz; - buffer =g_malloc0 (sizeof (VirtIOSoundPCMBuffer) + size); buffer->elem = elem; buffer->populated = false;
@@ -932,7 +936,7 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq)VirtIOSound *vsnd =VIRTIO_SND (vdev);VirtIOSoundPCMBuffer *buffer;VirtQueueElement *elem; -size_t msg_sz, size; +size_t msg_sz, size, tmp;virtio_snd_pcm_xfer hdr;uint32_t stream_id;/*
@@ -977,6 +981,10 @@ static void virtio_snd_handle_rx_xfer(VirtIODevice *vdev, VirtQueue *vq) goto rx_err; } size -=sizeof (virtio_snd_pcm_status); +/* Check for g_malloc0 overflow. */ +if (!g_size_checked_add (&tmp,sizeof (VirtIOSoundPCMBuffer), size)) { + goto rx_err; + } WITH_QEMU_LOCK_GUARD(&stream->queue_mutex) { buffer =g_malloc0 (sizeof (VirtIOSoundPCMBuffer) + size); buffer->elem = elem;
Signature · 1 line
2.47.3
Quoted 11 lines · Manos Pitsidianakis
Added checks for stuff coverity pointed out (CID 1547527).
Signed-off-by Manos Pitsidianakis <[email protected]>---
Changes in v3:
- Added Coverity CID in patch message (thanks Alex)
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]Changes in v2:
- Useg_size_checked_add(thanks rth)
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
Hi!
It looks like this patchset has been lost somehow. Is it still needed?
/mjt
Quoted 15 lines
---
Manos Pitsidianakis (2):
virtio-snd: check rx buffer descriptor size
virtio-snd: check for overflow beforeg_malloc0hw/audio/virtio-snd.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)---
base-commit:da6c4fe60fee30dd77267764d55b38af9cb89d4b
change-id: 20260415-virtio-fixups-3bc3a1a1cd27--
γαῖα πυρί μιχθήτω
Quoted 14 lines · Michael Tokarev
Quoted 11 lines · Manos Pitsidianakis
Added checks for stuff coverity pointed out (CID 1547527).
Signed-off-by Manos Pitsidianakis <[email protected]>---
Changes in v3:
- Added Coverity CID in patch message (thanks Alex)
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]Changes in v2:
- Useg_size_checked_add(thanks rth)
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
Quoted 5 lines
Hi!
It looks like this patchset has been lost somehow. Is it still needed?
/mjt
Patch 2 is still unreviewed it seems. I might send a new series with more patches soon. Thanks for the reminder!
Quoted 17 lines
Quoted 16 lines
---
Manos Pitsidianakis (2):
virtio-snd: check rx buffer descriptor size
virtio-snd: check for overflow beforeg_malloc0hw/audio/virtio-snd.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)---
base-commit:da6c4fe60fee30dd77267764d55b38af9cb89d4b
change-id: 20260415-virtio-fixups-3bc3a1a1cd27--
γαῖα πυρί μιχθήτω
Signature · 2 lines
Manos Pitsidianakis Emulation and Virtualization Engineer at Linaro Ltd
Quoted 15 lines · Manos Pitsidianakis
Quoted 14 lines · Michael Tokarev
Quoted 11 lines · Manos Pitsidianakis
Added checks for stuff coverity pointed out (CID 1547527).
Signed-off-by Manos Pitsidianakis <[email protected]>---
Changes in v3:
- Added Coverity CID in patch message (thanks Alex)
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]Changes in v2:
- Useg_size_checked_add(thanks rth)
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
Quoted 8 lines
Quoted 5 lines
Hi!
It looks like this patchset has been lost somehow. Is it still needed?
/mjt
Patch 2 is still unreviewed it seems. I might send a new series with more patches soon. Thanks for the reminder!
so you will repost all this?
Quoted 21 lines
Quoted 17 lines
Quoted 16 lines
---
Manos Pitsidianakis (2):
virtio-snd: check rx buffer descriptor size
virtio-snd: check for overflow beforeg_malloc0hw/audio/virtio-snd.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)---
base-commit:da6c4fe60fee30dd77267764d55b38af9cb89d4b
change-id: 20260415-virtio-fixups-3bc3a1a1cd27--
γαῖα πυρί μιχθήτωSignature · 2 lines
Manos Pitsidianakis Emulation and Virtualization Engineer at Linaro Ltd
Quoted 15 lines · Manos Pitsidianakis
Quoted 14 lines · Michael Tokarev
Quoted 11 lines · Manos Pitsidianakis
Added checks for stuff coverity pointed out (CID 1547527).
Signed-off-by Manos Pitsidianakis <[email protected]>---
Changes in v3:
- Added Coverity CID in patch message (thanks Alex)
- Link to v2: https://lore.kernel.org/qemu-devel/[email protected]Changes in v2:
- Useg_size_checked_add(thanks rth)
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]
Quoted 8 lines
Quoted 5 lines
Hi!
It looks like this patchset has been lost somehow. Is it still needed?
/mjt
Patch 2 is still unreviewed it seems. I might send a new series with more patches soon. Thanks for the reminder!
I think a bigger problem is that we allocate as much as guest wants in the 1st place. Want to look into this?
Quoted 21 lines
Quoted 17 lines
Quoted 16 lines
---
Manos Pitsidianakis (2):
virtio-snd: check rx buffer descriptor size
virtio-snd: check for overflow beforeg_malloc0hw/audio/virtio-snd.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)---
base-commit:da6c4fe60fee30dd77267764d55b38af9cb89d4b
change-id: 20260415-virtio-fixups-3bc3a1a1cd27--
γαῖα πυρί μιχθήτωSignature · 2 lines
Manos Pitsidianakis Emulation and Virtualization Engineer at Linaro Ltd