More virtio-snd fortifications/coverity fixes
cover More virtio-snd fortifications/coverity fixes
Added checks for stuff coverity pointed out.
---
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 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 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
Re: [PATCH 1/2] virtio-snd: check rx buffer descriptor size
Quoted 6 lines · Manos Pitsidianakis
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 so add a check.
Signed-off-by Manos Pitsidianakis <[email protected]>
Quoted 26 lines
--- 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 · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro
PATCH 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.
--- 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_uint_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_uint_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
Re: [PATCH 2/2] virtio-snd: check for overflow before g_malloc0
hw/audio/virtio-snd.c static void virtio_snd_handle_tx_xfer(VirtIODevice *vdev, VirtQueue *vq) · line 900
Quoted 4 lines · Manos Pitsidianakis
hw/audio/virtio-snd.c +4
-0+/* Check for g_malloc0 overflow. */ +if (!g_uint_checked_add (&tmp,sizeof (VirtIOSoundPCMBuffer), size)) { + goto tx_err; + }
This works by accident because of _GLIB_HAVE_BUILTIN_OVERFLOW_CHECKS.
The variables are of size_t, so g_size_checked_add is what you want.
r~
Quoted 1 line · Manos Pitsidianakis
Added checks for stuff coverity pointed out.
Could you add the CID ids for the issues please. Either inline (seems to be the most common) or the:
Resolves: Coverity CID xxx
form.
Thanks.
Quoted 12 lines
Signed-off-by Manos Pitsidianakis <[email protected]>---
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
Alex Bennée Virtualisation Tech Lead @ Linaro