Re: [PATCH] vhost: mask the in-order used-ring log cursor
From: Dongli Zhang
Date: Thu Dec 03 2026 - 04:00:26 EST
On Thu, Dec 3, 2026 12:55:20AM -0800, Michael S. Tsirkin wrote:
> On Sun, Nov 22, 2026 at 09:19:07PM +0800, YanXin Li via B4 Relay wrote:
>> From: YanXin Li <fadouse@xxxxx>
>>
>> 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@xxxxxxxxxxxxxxx
>> Signed-off-by: YanXin Li <fadouse@xxxxx>
>
>
> vhost: reject used ring batches larger than the ring
>
>
> is in stable now, and seems to be the most comprehensive way to address
> this class of issues.
>
You meant mainline tree instead of stable?
Thank you very much!
Dongli Zhang