From: "Michael S. Tsirkin" <mst@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
Jonah Palmer <jonah.palmer@oracle.com>,
terrynini <terrynini38514@gmail.com>,
Si-Wei Liu <si-wei.liu@oracle.com>,
Jason Wang <jasowang@redhat.com>
Subject: [PULL 01/17] virtio: fix off-by-one and invalid access in virtqueue_ordered_fill
Date: Fri, 1 Aug 2025 10:25:02 -0400 [thread overview]
Message-ID: <6fcf5ebafad65adc19a616260ca7dc90005785d1.1754058276.git.mst@redhat.com> (raw)
In-Reply-To: <cover.1754058276.git.mst@redhat.com>
From: Jonah Palmer <jonah.palmer@oracle.com>
Commit b44135daa372 introduced virtqueue_ordered_fill for
VIRTIO_F_IN_ORDER support but had a few issues:
* Conditional while loop used 'steps <= max_steps' but should've been
'steps < max_steps' since reaching steps == max_steps would indicate
that we didn't find an element, which is an error. Without this
change, the code would attempt to read invalid data at an index
outside of our search range.
* Incremented 'steps' using the next chain's ndescs instead of the
current one.
This patch corrects the loop bounds and synchronizes 'steps' and index
increments.
We also add a defensive sanity check against malicious or invalid
descriptor counts to avoid a potential infinite loop and DoS.
Fixes: b44135daa372 ("virtio: virtqueue_ordered_fill - VIRTIO_F_IN_ORDER support")
Reported-by: terrynini <terrynini38514@gmail.com>
Signed-off-by: Jonah Palmer <jonah.palmer@oracle.com>
Message-Id: <20250721150208.2409779-1-jonah.palmer@oracle.com>
Reviewed-by: Si-Wei Liu <si-wei.liu@oracle.com>
Acked-by: Jason Wang <jasowang@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
hw/virtio/virtio.c | 22 ++++++++++++++++------
1 file changed, 16 insertions(+), 6 deletions(-)
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
index 2ab1d20769..9a81ad912e 100644
--- a/hw/virtio/virtio.c
+++ b/hw/virtio/virtio.c
@@ -938,18 +938,18 @@ static void virtqueue_packed_fill(VirtQueue *vq, const VirtQueueElement *elem,
static void virtqueue_ordered_fill(VirtQueue *vq, const VirtQueueElement *elem,
unsigned int len)
{
- unsigned int i, steps, max_steps;
+ unsigned int i, steps, max_steps, ndescs;
i = vq->used_idx % vq->vring.num;
steps = 0;
/*
- * We shouldn't need to increase 'i' by more than the distance
- * between used_idx and last_avail_idx.
+ * We shouldn't need to increase 'i' by more than or equal to
+ * the distance between used_idx and last_avail_idx (max_steps).
*/
max_steps = (vq->last_avail_idx - vq->used_idx) % vq->vring.num;
/* Search for element in vq->used_elems */
- while (steps <= max_steps) {
+ while (steps < max_steps) {
/* Found element, set length and mark as filled */
if (vq->used_elems[i].index == elem->index) {
vq->used_elems[i].len = len;
@@ -957,8 +957,18 @@ static void virtqueue_ordered_fill(VirtQueue *vq, const VirtQueueElement *elem,
break;
}
- i += vq->used_elems[i].ndescs;
- steps += vq->used_elems[i].ndescs;
+ ndescs = vq->used_elems[i].ndescs;
+
+ /* Defensive sanity check */
+ if (unlikely(ndescs == 0 || ndescs > vq->vring.num)) {
+ qemu_log_mask(LOG_GUEST_ERROR,
+ "%s: %s invalid ndescs %u at position %u\n",
+ __func__, vq->vdev->name, ndescs, i);
+ return;
+ }
+
+ i += ndescs;
+ steps += ndescs;
if (i >= vq->vring.num) {
i -= vq->vring.num;
--
MST
next prev parent reply other threads:[~2025-08-01 14:35 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-01 14:24 [PULL 00/17] virtio,pci,pc: bugfixes Michael S. Tsirkin
2025-08-01 14:25 ` Michael S. Tsirkin [this message]
2025-08-01 14:25 ` [PULL 02/17] vhost: Do not abort on log-start error Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 03/17] vhost: Do not abort on log-stop error Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 04/17] virtio-net: Fix VLAN filter table reset timing Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 05/17] pcie_sriov: Fix configuration and state synchronization Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 06/17] hw/i386/amd_iommu: Fix MMIO register write tracing Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 07/17] hw/i386/amd_iommu: Remove unused and wrongly set ats_enabled field Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 08/17] hw/i386/amd_iommu: Move IOAPIC memory region initialization to the end Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 09/17] hw/i386/amd_iommu: Fix amdvi_write*() Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 10/17] hw/i386/amd_iommu: Support MMIO writes to the status register Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 11/17] hw/i386/amd_iommu: Fix event log generation Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 12/17] tests/acpi: virt: add an empty HEST file Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 13/17] tests/qtest/bios-tables-test: extend to also check HEST table Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 14/17] tests/acpi: virt: update HEST file with its current data Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 15/17] intel_iommu: Allow both Status Write and Interrupt Flag in QI wait Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 16/17] MAINTAINERS: add net/vhost* files under `vhost` Michael S. Tsirkin
2025-08-01 14:25 ` [PULL 17/17] net/vdpa: fix potential fd leak in net_init_vhost_vdpa() Michael S. Tsirkin
2025-08-01 19:34 ` [PULL 00/17] virtio,pci,pc: bugfixes Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6fcf5ebafad65adc19a616260ca7dc90005785d1.1754058276.git.mst@redhat.com \
--to=mst@redhat.com \
--cc=jasowang@redhat.com \
--cc=jonah.palmer@oracle.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=si-wei.liu@oracle.com \
--cc=terrynini38514@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).