Commit 37553c2db7 for qemu.org

commit 37553c2db772747aa3a041ada99b970ff7591399
Author: Peter Maydell <peter.maydell@linaro.org>
Date:   Mon Nov 23 18:43:24 2026 +0000

    block/vhdx-log: Fix loop index type in vhdx_log_flush_desc()

    In vhdx_log_flush_desc(), the when the log descriptor is a zero
    descriptor it has a 64-bit "zero_length" field telling us how many
    bytes of zeroes we should be writing.  However, the 'i' variable we
    use as a loop index when we're actually writing out the zeroes is
    only of "int" type, so we will mishandle the descriptor if it
    specifies a large number of bytes.

    Fix the type mismatch; since we're touching it anyway, restrict the
    variable to the minimum necessary scope (the loop).

    Note that although this bug could cause us to spin in this loop
    performing disk writes for 2^31 iterations, the image could equally
    well ask us to do that directly, since the file format permits a
    valid log descriptor to specify a very large length.  This is at most
    "guest self-DoS"; management applications should be imposing CPU and
    memory caps when they validate untrusted images.

    Cc: qemu-stable@nongnu.org
    Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4019
    Reported-by: Tristan <https://gitlab.com/TristanInSec>
    Fixes: 0a43a1b5d7c33 ("block: vhdx - log parsing, replay, and flush support")
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
    Message-ID: <20261123184324.4085961-1-peter.maydell@linaro.org>
    Reviewed-by: Kevin Wolf <kwolf@redhat.com>
    Signed-off-by: Kevin Wolf <kwolf@redhat.com>

diff --git a/block/vhdx-log.c b/block/vhdx-log.c
index 5c20468e1e..6f2996ee3b 100644
--- a/block/vhdx-log.c
+++ b/block/vhdx-log.c
@@ -427,7 +427,6 @@ vhdx_log_flush_desc(BlockDriverState *bs, VHDXLogDescriptor *desc,
     uint32_t offset = 0;
     void *buffer = NULL;
     uint64_t count = 1;
-    int i;

     buffer = qemu_blockalign(bs, VHDX_LOG_SECTOR_SIZE);

@@ -473,7 +472,7 @@ vhdx_log_flush_desc(BlockDriverState *bs, VHDXLogDescriptor *desc,
     file_offset = desc->file_offset;

     /* count is only > 1 if we are writing zeroes */
-    for (i = 0; i < count; i++) {
+    for (uint64_t i = 0; i < count; i++) {
         ret = bdrv_pwrite_sync(bs->file, file_offset, VHDX_LOG_SECTOR_SIZE,
                                buffer, 0);
         if (ret < 0) {