@samitouri / QOSamiQemu / commits / 61679d7dcf

virtio-snd: handle 5.14.6.2 for PCM_INFO properly

The section 5.14.6.2 of the VIRTIO spec says: 5.14.6.2 Driver Requirements: Item Information Request - The driver MUST NOT set start_id and count such that start_id + count is greater than the total number of particular items that is indicated in the device configuration space. - The driver MUST provide a buffer of sizeof(struct virtio_snd_hdr) + count * size bytes for the response. While we performed some check for the second requirement, it failed to check for integer overflow. Add also a check for the first requirement, which should limit exposure to any overflow, since realistically the number of streams will be low enough in value such that overflow is improbable. Cc: qemu-stable@nongnu.org Reported-by: 罗铭源 <myluo24@m.fudan.edu.cn> Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Message-Id: <20260220-virtio-snd-series-v1-3-207c4f7200a2@linaro.org>

Manos Pitsidianakis committed Feb 20, 2026 at 11:40 UTC 61679d7dcfa2dffc8fb115aa19b09e0e7cf5ea5c
1 file changed +27 -4
hw/audio/virtio-snd.c
+27 -4
@@ -156,7 +156,7 @@ static virtio_snd_pcm_set_params *virtio_snd_pcm_get_params(VirtIOSound *s,
156 static void virtio_snd_handle_pcm_info(VirtIOSound *s,
157 virtio_snd_ctrl_command *cmd)
158 {
159 - uint32_t stream_id, start_id, count, size;
159 + uint32_t stream_id, start_id, count, size, tmp;
160 virtio_snd_pcm_info val;
161 virtio_snd_query_info req;
162 VirtIOSoundPCMStream *stream = NULL;
@@ -179,11 +179,34 @@ static void virtio_snd_handle_pcm_info(VirtIOSound *s,
179 count = le32_to_cpu(req.count);
180 size = le32_to_cpu(req.size);
181
182 - if (iov_size(cmd->elem->in_sg, cmd->elem->in_num) <
183 - sizeof(virtio_snd_hdr) + size * count) {
182 + /*
183 + * 5.14.6.2 Driver Requirements: Item Information Request
184 + * "The driver MUST NOT set start_id and count such that start_id + count
185 + * is greater than the total number of particular items that is indicated
186 + * in the device configuration space."
187 + */
188 + if (start_id > s->snd_conf.streams
189 + || !g_uint_checked_add(&tmp, start_id, count)
190 + || start_id + count > s->snd_conf.streams) {
191 + error_report("pcm info: start_id + count is greater than the total "
192 + "number of streams, got: start_id = %u, count = %u",
193 + start_id, count);
194 + cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
195 + return;
196 + }
197 +
198 + /*
199 + * 5.14.6.2 Driver Requirements: Item Information Request
200 + * "The driver MUST provide a buffer of sizeof(struct virtio_snd_hdr) +
201 + * count * size bytes for the response."
202 + */
203 + if (!g_uint_checked_mul(&tmp, size, count)
204 + || !g_uint_checked_add(&tmp, tmp, sizeof(virtio_snd_hdr))
205 + || iov_size(cmd->elem->in_sg, cmd->elem->in_num) <
206 + sizeof(virtio_snd_hdr) + size * count) {
207 error_report("pcm info: buffer too small, got: %zu, needed: %zu",
208 iov_size(cmd->elem->in_sg, cmd->elem->in_num),
186 - sizeof(virtio_snd_pcm_info));
209 + sizeof(virtio_snd_pcm_info) * count);
210 cmd->resp.code = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
211 return;
212 }