[PATCH] vhost: mask the in-order used-ring log cursor

YanXin Li via B4 Relay posted 1 patch 1 day, 22 hours ago
drivers/vhost/vhost.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
[PATCH] vhost: mask the in-order used-ring log cursor
Posted by YanXin Li via B4 Relay 1 day, 22 hours ago
From: YanXin Li <fadouse@pm.me>

vhost_add_used_n_in_order() plays the batch of used-ring writes back a
second time for dirty logging.  Commit e244725e86be ("vhost: fix used
ring index wrap in vhost_add_used_n_in_order") masked the index in the
descriptor-publishing loop.  The dirty-logging loop introduced by
bfcde6b64769 still uses the single-subtraction form

    next = start + nheads[i];
    if (next >= vq->num)
        next -= vq->num;

If the guest refills the available ring while get_rx_bufs() collects
one mergeable-RX packet, that packet can consume more heads than
vq->num.  get_rx_bufs() permits up to UIO_MAXIOV = 1024 descriptor
chains in one call, so a single subtraction is not enough.  For
vq->num = 32, last_used_idx = 0 and nheads[0] = 64, next becomes
64 - 32 = 32, which is still equal to vq->num and out of bounds.  The
next iteration then logs

    used = vq->used->ring + 32;
    log_used(vq, 4 + 32 * 8, 8);

which starts past the end of the 260-byte ring body (4 + 32 * 8).
For a used ring placed inside page 7 at guest address 0x7ef8 (so the
entire 262-byte ring occupies [0x7ef8, 0x7ffe) and
vq_log_used_access_ok() validates only log_base[0]), log_used()
marks [0x7ffc, 0x8004) as dirty.  log_write() then sets a bit in
log_base[1] for page 8, past the single validated byte.  On a
vhost-net RX queue configured with VIRTIO_F_IN_ORDER,
VIRTIO_NET_F_MRG_RXBUF,
VHOST_F_LOG_ALL, VHOST_VRING_F_LOG, vq->num = 32, and
log_guest_addr = 0x7ef8, delivering one 8 KiB frame consuming 64
mergeable buffers followed by one 1-buffer frame in the same
count = 2 flush writes

    log_base[1] = 0x01

where log_base[0] = 0x80 is the only byte the kernel was entitled to
touch.  A guest attached to a vhost-net backend with dirty-page
logging enabled (for example during live migration) and a small RX
ring can therefore corrupt dirty-log bitmap memory that the VMM owns
outside the region registered for this ring.

Wrap the logging loop index the same way the first loop does:

    next = (start + nheads[i]) & (vq->num - 1);

vhost_vring_set_num() already enforces that vq->num is a nonzero
power of two, so this is well defined for every accepted ring size.
The coalescing invariant is preserved: if a run is not flushed,
next == start + 1.  When start == vq->num - 1, start + 1 == vq->num,
whereas the masked next is always less than vq->num.  The run is
therefore flushed before the cursor wraps.
With this change the same vhost-net configuration publishes
used->idx = 65 and confines the dirty log to log_base[0] = 0x80,
leaving log_base[1] = 0x00.  The mask keeps next in [0, vq->num) for
every accepted power-of-two ring size.  As a bounded cross-check of
the index arithmetic, a Z3 model over ring sizes {1, 2, 4, 8, 16, 32}
and buffer counts in [1, 64] finds an out-of-range result for the old
subtraction (vq->num = 16, start = 15, nheads = 17) and none for the
mask within those bounds.

Runtime verification and fix validation were performed in an x86-64
QEMU/KVM guest running mainline after v7.4-rc3, with Generic KASAN,
UBSAN, lockdep, and DEBUG_OBJECTS.

Fixes: bfcde6b64769 ("vhost: fix dirty logging for in-order used ring entries")
Cc: stable@vger.kernel.org
Signed-off-by: YanXin Li <fadouse@pm.me>
---
 drivers/vhost/vhost.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index dc973287567..80b887e1511 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -3206,9 +3206,7 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
 				log_start = start;
 			log_count++;
 
-			next = start + nheads[i];
-			if (next >= vq->num)
-				next -= vq->num;
+			next = (start + nheads[i]) & (vq->num - 1);
 			if (i + 1 == count || next != start + 1) {
 				used = vq->used->ring + log_start;
 				log_used(vq,

---
base-commit: c44104f3642da720164042623e4832836aefdd8e
change-id: 20261122-b4-06-vhost-in-order-log-oob-v1-35efdba8fbab-43734ad10f0a

Best regards,
--  
YanXin Li <fadouse@pm.me>
Re: [PATCH] vhost: mask the in-order used-ring log cursor
Posted by Eugenio Perez Martin 23 hours ago
On Sun, Nov 22, 2026 at 2:20 PM YanXin Li via B4 Relay
<devnull+fadouse.pm.me@kernel.org> wrote:
>
> From: YanXin Li <fadouse@pm.me>
>
> vhost_add_used_n_in_order() plays the batch of used-ring writes back a
> second time for dirty logging.  Commit e244725e86be ("vhost: fix used
> ring index wrap in vhost_add_used_n_in_order") masked the index in the
> descriptor-publishing loop.  The dirty-logging loop introduced by
> bfcde6b64769 still uses the single-subtraction form
>
>     next = start + nheads[i];
>     if (next >= vq->num)
>         next -= vq->num;
>
> If the guest refills the available ring while get_rx_bufs() collects
> one mergeable-RX packet, that packet can consume more heads than
> vq->num.

Although I'm ok with the proposed patch as hardening or optimization,
this sounds more like a bug in get_rx_bufs. Where are the memory
addresses for these pieces stored if there are no more buffers in the
virtqueue? Or is it able to loop through the vq?

> get_rx_bufs() permits up to UIO_MAXIOV = 1024 descriptor
> chains in one call, so a single subtraction is not enough.  For
> vq->num = 32, last_used_idx = 0 and nheads[0] = 64, next becomes
> 64 - 32 = 32, which is still equal to vq->num and out of bounds.  The
> next iteration then logs
>
>     used = vq->used->ring + 32;
>     log_used(vq, 4 + 32 * 8, 8);
>
> which starts past the end of the 260-byte ring body (4 + 32 * 8).
> For a used ring placed inside page 7 at guest address 0x7ef8 (so the
> entire 262-byte ring occupies [0x7ef8, 0x7ffe) and
> vq_log_used_access_ok() validates only log_base[0]), log_used()
> marks [0x7ffc, 0x8004) as dirty.  log_write() then sets a bit in
> log_base[1] for page 8, past the single validated byte.  On a
> vhost-net RX queue configured with VIRTIO_F_IN_ORDER,
> VIRTIO_NET_F_MRG_RXBUF,
> VHOST_F_LOG_ALL, VHOST_VRING_F_LOG, vq->num = 32, and
> log_guest_addr = 0x7ef8, delivering one 8 KiB frame consuming 64
> mergeable buffers followed by one 1-buffer frame in the same
> count = 2 flush writes
>
>     log_base[1] = 0x01
>
> where log_base[0] = 0x80 is the only byte the kernel was entitled to
> touch.  A guest attached to a vhost-net backend with dirty-page
> logging enabled (for example during live migration) and a small RX
> ring can therefore corrupt dirty-log bitmap memory that the VMM owns
> outside the region registered for this ring.
>
> Wrap the logging loop index the same way the first loop does:
>
>     next = (start + nheads[i]) & (vq->num - 1);
>
> vhost_vring_set_num() already enforces that vq->num is a nonzero
> power of two, so this is well defined for every accepted ring size.
> The coalescing invariant is preserved: if a run is not flushed,
> next == start + 1.  When start == vq->num - 1, start + 1 == vq->num,
> whereas the masked next is always less than vq->num.  The run is
> therefore flushed before the cursor wraps.
> With this change the same vhost-net configuration publishes
> used->idx = 65 and confines the dirty log to log_base[0] = 0x80,
> leaving log_base[1] = 0x00.  The mask keeps next in [0, vq->num) for
> every accepted power-of-two ring size.  As a bounded cross-check of
> the index arithmetic, a Z3 model over ring sizes {1, 2, 4, 8, 16, 32}
> and buffer counts in [1, 64] finds an out-of-range result for the old
> subtraction (vq->num = 16, start = 15, nheads = 17) and none for the
> mask within those bounds.
>
> Runtime verification and fix validation were performed in an x86-64
> QEMU/KVM guest running mainline after v7.4-rc3, with Generic KASAN,
> UBSAN, lockdep, and DEBUG_OBJECTS.
>

Can you post the reproducer? Even better if it is integrated in
tools/virtio/vhost_net_test.c.

Thanks!

> Fixes: bfcde6b64769 ("vhost: fix dirty logging for in-order used ring entries")
> Cc: stable@vger.kernel.org
> Signed-off-by: YanXin Li <fadouse@pm.me>
> ---
>  drivers/vhost/vhost.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> index dc973287567..80b887e1511 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -3206,9 +3206,7 @@ static int vhost_add_used_n_in_order(struct vhost_virtqueue *vq,
>                                 log_start = start;
>                         log_count++;
>
> -                       next = start + nheads[i];
> -                       if (next >= vq->num)
> -                               next -= vq->num;
> +                       next = (start + nheads[i]) & (vq->num - 1);
>                         if (i + 1 == count || next != start + 1) {
>                                 used = vq->used->ring + log_start;
>                                 log_used(vq,
>
> ---
> base-commit: c44104f3642da720164042623e4832836aefdd8e
> change-id: 20261122-b4-06-vhost-in-order-log-oob-v1-35efdba8fbab-43734ad10f0a
>
> Best regards,
> --
> YanXin Li <fadouse@pm.me>
>
>
>
Re: [PATCH] vhost: mask the in-order used-ring log cursor
Posted by fadouse 22 hours ago
Hi Eugenio,

> Although I'm ok with the proposed patch as hardening or optimization,
> this sounds more like a bug in get_rx_bufs. Where are the memory
> addresses for these pieces stored if there are no more buffers in the
> virtqueue? Or is it able to loop through the vq?

Yes — get_rx_bufs() loops around the same virtqueue when a
non-conforming guest advances avail->idx while earlier descriptors are
still in flight.  handle_rx() passes quota = UIO_MAXIOV (1024) for
mergeable RX, and each vhost_get_vq_desc_n() call appends the
translated buffer addresses into vq->iov (also sized to UIO_MAXIOV).
Because vhost_get_avail_idx() compares avail_idx against
vq->last_avail_idx rather than vq->last_used_idx, it rejects only when

  (u16)(avail_idx - vq->last_avail_idx) > vq->num

so the guest can reuse the 32 ring slots mid-packet and get_rx_bufs()
appends those addresses again into vq->iov[32..63], returning
headcount = 64.

Bounding get_rx_bufs() (e.g. capping its quota against vq->num, or
checking in-flight descriptors more broadly) is certainly worth
looking at separately, while keeping the mask in
vhost_add_used_n_in_order() keeps the dirty-logging cursor consistent
with the publishing loop from commit e244725e86be.  If you think a
follow-up check in get_rx_bufs() makes sense, I'm happy to look into
that as a separate patch.

> Can you post the reproducer? Even better if it is integrated in
> tools/virtio/vhost_net_test.c.

I just posted the standalone C reproducer in my reply to Jiaming in
this thread.  Note that tools/virtio/vhost_net_test.c drives the ring
through the conforming vring_new_virtqueue() / virtqueue_add_inbuf()
API, whereas reproducing this requires a raw vring advancing avail->idx
past last_used_idx + vq->num with VIRTIO_F_IN_ORDER |
VIRTIO_NET_F_MRG_RXBUF | VHOST_F_LOG_ALL and VHOST_VRING_F_LOG set in
VHOST_SET_VRING_ADDR.  If you'd still like a raw-vring test case added
under tools/virtio/, I'm happy to include it in a v2 (along with fixing
the minor 262 -> 260 byte used-ring size typo in the commit message).

Thanks,
YanXin