All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH for-11.2 0/2] block/qcow2: Avoid crash in qemu-img amend when reftable is empty
@ 2026-11-19 14:03 Peter Maydell
  2026-11-19 14:03 ` [PATCH for-11.2 1/2] block/qcow2: Detect corrupt images where first refcount block " Peter Maydell
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Peter Maydell @ 2026-11-19 14:03 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-block, Kevin Wolf, Hanna Reitz

In a QCOW2 image, the header is always cluster zero, and so that
cluster must have a non-zero refcount.  This means that an image where
the first entry in the refcount table is zero, or where it points to a
refblock which is empty, must be corrupt.

In the "qemu-img amend -o refcount_bits=..." codepath, we were
implicitly relying on the image not being corrupted in this way.  If
the entire refcount table was zero, our "walk the table to see how big
the new reftable size is" loop would say "zero", and we would then not
allocate any memory for s->refcount_table, and
update_max_refcount_table_index() would underflow when calculating
s->refcount_table_size - 1 and then crash.

This patchset fixes that bug, and adds a test case for it.

It would also be possible to refuse to open an image for some of the
more obvious cases of this corruption (e.g. "on open,
refcount_table[0] is 0"), but checking the actual refcount of cluster
0 is a bit trickier as we only read in that refblock on demand. Plus
some of the existing iotests (060, 098) rely on corrupted images of
that kind only being detected later.  So I have stuck to fixing the
immediate bug.

NB: this isn't a security issue because the crash can only be
provoked for this specific qemu-img amend codepath, so it's a
self-DoS.

thanks
-- PMM

Peter Maydell (2):
  block/qcow2: Detect corrupt images where first refcount block is empty
  tests/qemu-iotest: Add test for qcow2 image with empty reftable

 block/qcow2-refcount.c     | 15 ++++++++++
 tests/qemu-iotests/315     | 59 ++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/315.out |  9 ++++++
 3 files changed, 83 insertions(+)
 create mode 100755 tests/qemu-iotests/315
 create mode 100644 tests/qemu-iotests/315.out

-- 
2.43.0



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH for-11.2 1/2] block/qcow2: Detect corrupt images where first refcount block is empty
  2026-11-19 14:03 [PATCH for-11.2 0/2] block/qcow2: Avoid crash in qemu-img amend when reftable is empty Peter Maydell
@ 2026-11-19 14:03 ` Peter Maydell
  2026-11-19 18:48   ` Peter Maydell
  2026-11-19 19:42   ` Eric Blake
  2026-11-19 14:03 ` [PATCH for-11.2 2/2] tests/qemu-iotest: Add test for qcow2 image with empty reftable Peter Maydell
  2026-11-20 17:32 ` [PATCH for-11.2 0/2] block/qcow2: Avoid crash in qemu-img amend when reftable is empty Kevin Wolf
  2 siblings, 2 replies; 7+ messages in thread
From: Peter Maydell @ 2026-11-19 14:03 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-block, Kevin Wolf, Hanna Reitz

In a QCOW2 image, the header is always cluster zero, and so that
cluster must have a non-zero refcount.  This means that an image
where the first entry in the refcount table is zero, or where it
points to a refblock which is empty, must be corrupt.  Detect this in
alloc_refblock().

This fixes a bug where 'qemu-img amend -o refcount_bits=...' would
crash on a corrupted image where the entire refcount table was empty.
The crash happened because walk_over_reftable() would return with
new_reftable_size == 0 and new_reftable == NULL.
qcow2_change_refcount_table() updated s->refcount_table and
s->refcount_table_size with these bogus values, and
update_max_refcount_table_index() would crash trying to dereference
NULL[UINT_MAX]. We add a defensive assertion here that the
new refcount table isn't zero-sized; this can't be reached because
it only happens in the corruption case that we now detect.

Cc: qemu-stable@nongnu.org
Fixes: 791c9a004efbcff ("qcow2: Add function for refcount order amendment")
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3690
Suggested-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 block/qcow2-refcount.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
index df7f6d2f234..2102386147a 100644
--- a/block/qcow2-refcount.c
+++ b/block/qcow2-refcount.c
@@ -3139,6 +3139,14 @@ alloc_refblock(BlockDriverState *bs, uint64_t **reftable,
     BDRVQcow2State *s = bs->opaque;
     int64_t offset;
 
+    if (reftable_index == 0 && refblock_empty) {
+        /* The header is in cluster 0 so it must have a non-zero refcount */
+        qcow2_signal_corruption(bs, true, -1, -1,
+                                "first refblock is empty");
+        error_setg(errp, "Image is corrupt (first refblock is empty)");
+        return -EIO;
+    }
+
     if (!refblock_empty && reftable_index >= *reftable_size) {
         uint64_t *new_reftable;
         uint64_t new_reftable_size;
@@ -3451,6 +3459,13 @@ int qcow2_change_refcount_order(BlockDriverState *bs, int refcount_order,
         }
     } while (new_allocation);
 
+    /*
+     * This can only happen if the reftable is all zeroes, but the header is
+     * in cluster 0 and must have a non-zero refcount, and we check that in
+     * alloc_refblock().
+     */
+    assert(new_reftable_size != 0);
+
     /* Second, write the new refblocks */
     ret = walk_over_reftable(bs, &new_reftable, &new_reftable_index,
                              &new_reftable_size, new_refblock,
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH for-11.2 2/2] tests/qemu-iotest: Add test for qcow2 image with empty reftable
  2026-11-19 14:03 [PATCH for-11.2 0/2] block/qcow2: Avoid crash in qemu-img amend when reftable is empty Peter Maydell
  2026-11-19 14:03 ` [PATCH for-11.2 1/2] block/qcow2: Detect corrupt images where first refcount block " Peter Maydell
@ 2026-11-19 14:03 ` Peter Maydell
  2026-11-19 19:41   ` Eric Blake
  2026-11-20 17:32 ` [PATCH for-11.2 0/2] block/qcow2: Avoid crash in qemu-img amend when reftable is empty Kevin Wolf
  2 siblings, 1 reply; 7+ messages in thread
From: Peter Maydell @ 2026-11-19 14:03 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-block, Kevin Wolf, Hanna Reitz

This test creates a qcow2 image, manually zeroes out its entire
reftable, and then runs 'qemu-img amend -o refcount_bits=64" on it.
This used to cause qemu-img to crash; now we will report that the
image can't be opened because it's corrupted.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 tests/qemu-iotests/315     | 59 ++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/315.out |  9 ++++++
 2 files changed, 68 insertions(+)
 create mode 100755 tests/qemu-iotests/315
 create mode 100644 tests/qemu-iotests/315.out

diff --git a/tests/qemu-iotests/315 b/tests/qemu-iotests/315
new file mode 100755
index 00000000000..ff1a22bddd5
--- /dev/null
+++ b/tests/qemu-iotests/315
@@ -0,0 +1,59 @@
+#!/usr/bin/env bash
+# group: rw auto quick
+#
+# Test handling of corrupt qcow2 file with all-zeroes reftable
+#
+# Copyright (c) 2026 Linaro Ltd
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+seq=`basename $0`
+echo "QA output created by $seq"
+
+status=1        # failure is the default!
+
+_cleanup()
+{
+    _cleanup_test_img
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+# get standard environment, filters and checks
+. ./common.rc
+. ./common.filter
+
+# This tests qcow2-specific low-level functionality
+_supported_fmt qcow2
+_supported_proto file
+_supported_os Linux
+# We are going to check changing refcount_bits from 16 to 64,
+# so anything else is not supported
+_unsupported_imgopts 'refcount_bits=\([^1]\|.\([^6]\|$\)\)'
+
+echo
+echo "=== Testing empty refcount table ==="
+echo
+
+# Create an image, then modify it to zero out the entire reftable.
+# Check that this corrupted image doesn't cause qemu-img amend to crash.
+
+_make_test_img -o 'refcount_bits=16' 64K
+
+reftable_ofs=$(peek_file_be "$TEST_IMG" 48 8)
+reftable_clusters=$(peek_file_be "$TEST_IMG" 56 4)
+cluster_bits=$(peek_file_be "$TEST_IMG" 20 4)
+# Cluster size is (1 << cluster_bits). Zero out the data starting
+# at reftable_ofs for (cluster_size * reftable_clusters) bytes.
+reftable_size=$(((1 << cluster_bits) * reftable_clusters))
+
+dd "of=$TEST_IMG" "bs=1" "seek=$reftable_ofs" "count=$reftable_size" if=/dev/zero conv=notrunc >&/dev/null
+
+# This should complain that the image is corrupt
+$QEMU_IMG amend -f qcow2 -o refcount_bits=64 "$TEST_IMG" 2>&1
+
+# success, all done
+echo
+echo '*** done'
+rm -f $seq.full
+status=0
+
diff --git a/tests/qemu-iotests/315.out b/tests/qemu-iotests/315.out
new file mode 100644
index 00000000000..91b718df5ca
--- /dev/null
+++ b/tests/qemu-iotests/315.out
@@ -0,0 +1,9 @@
+QA output created by 315
+
+=== Testing empty refcount table ===
+
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=65536
+qcow2: Marking image as corrupt: first refblock is empty; further corruption events will be suppressed
+qemu-img: Image is corrupt (first refblock is empty)
+
+*** done
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH for-11.2 1/2] block/qcow2: Detect corrupt images where first refcount block is empty
  2026-11-19 14:03 ` [PATCH for-11.2 1/2] block/qcow2: Detect corrupt images where first refcount block " Peter Maydell
@ 2026-11-19 18:48   ` Peter Maydell
  2026-11-19 19:42   ` Eric Blake
  1 sibling, 0 replies; 7+ messages in thread
From: Peter Maydell @ 2026-11-19 18:48 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-block, Kevin Wolf, Hanna Reitz

On Thu, 19 Nov 2026 at 14:03, Peter Maydell <peter.maydell@linaro.org> wrote:
>
> In a QCOW2 image, the header is always cluster zero, and so that
> cluster must have a non-zero refcount.  This means that an image
> where the first entry in the refcount table is zero, or where it
> points to a refblock which is empty, must be corrupt.  Detect this in
> alloc_refblock().
>
> This fixes a bug where 'qemu-img amend -o refcount_bits=...' would
> crash on a corrupted image where the entire refcount table was empty.
> The crash happened because walk_over_reftable() would return with
> new_reftable_size == 0 and new_reftable == NULL.
> qcow2_change_refcount_table() updated s->refcount_table and
> s->refcount_table_size with these bogus values, and
> update_max_refcount_table_index() would crash trying to dereference
> NULL[UINT_MAX]. We add a defensive assertion here that the
> new refcount table isn't zero-sized; this can't be reached because
> it only happens in the corruption case that we now detect.
>
> Cc: qemu-stable@nongnu.org
> Fixes: 791c9a004efbcff ("qcow2: Add function for refcount order amendment")
> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3690
> Suggested-by: Kevin Wolf <kwolf@redhat.com>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>a1gxer

I forgot the reported-by credit:
Reported-by: a1gxer

thanks
-- PMM


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH for-11.2 2/2] tests/qemu-iotest: Add test for qcow2 image with empty reftable
  2026-11-19 14:03 ` [PATCH for-11.2 2/2] tests/qemu-iotest: Add test for qcow2 image with empty reftable Peter Maydell
@ 2026-11-19 19:41   ` Eric Blake
  0 siblings, 0 replies; 7+ messages in thread
From: Eric Blake @ 2026-11-19 19:41 UTC (permalink / raw)
  To: Peter Maydell; +Cc: qemu-devel, qemu-block, Kevin Wolf, Hanna Reitz

On Thu, Nov 19, 2026 at 02:03:38PM +0000, Peter Maydell wrote:
> This test creates a qcow2 image, manually zeroes out its entire
> reftable, and then runs 'qemu-img amend -o refcount_bits=64" on it.
> This used to cause qemu-img to crash; now we will report that the
> image can't be opened because it's corrupted.
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>  tests/qemu-iotests/315     | 59 ++++++++++++++++++++++++++++++++++++++
>  tests/qemu-iotests/315.out |  9 ++++++

These days, it seems better to create named tests under
qemu-iotests/tests, rather than as a new three-digit number.  But a
file rename is trivial enough that I fine with giving my review,
whether or not we adjust the patch.

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH for-11.2 1/2] block/qcow2: Detect corrupt images where first refcount block is empty
  2026-11-19 14:03 ` [PATCH for-11.2 1/2] block/qcow2: Detect corrupt images where first refcount block " Peter Maydell
  2026-11-19 18:48   ` Peter Maydell
@ 2026-11-19 19:42   ` Eric Blake
  1 sibling, 0 replies; 7+ messages in thread
From: Eric Blake @ 2026-11-19 19:42 UTC (permalink / raw)
  To: Peter Maydell; +Cc: qemu-devel, qemu-block, Kevin Wolf, Hanna Reitz

On Thu, Nov 19, 2026 at 02:03:37PM +0000, Peter Maydell wrote:
> In a QCOW2 image, the header is always cluster zero, and so that
> cluster must have a non-zero refcount.  This means that an image
> where the first entry in the refcount table is zero, or where it
> points to a refblock which is empty, must be corrupt.  Detect this in
> alloc_refblock().
> 
> This fixes a bug where 'qemu-img amend -o refcount_bits=...' would
> crash on a corrupted image where the entire refcount table was empty.
> The crash happened because walk_over_reftable() would return with
> new_reftable_size == 0 and new_reftable == NULL.
> qcow2_change_refcount_table() updated s->refcount_table and
> s->refcount_table_size with these bogus values, and
> update_max_refcount_table_index() would crash trying to dereference
> NULL[UINT_MAX]. We add a defensive assertion here that the
> new refcount table isn't zero-sized; this can't be reached because
> it only happens in the corruption case that we now detect.
> 
> Cc: qemu-stable@nongnu.org
> Fixes: 791c9a004efbcff ("qcow2: Add function for refcount order amendment")
> Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3690
> Suggested-by: Kevin Wolf <kwolf@redhat.com>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>  block/qcow2-refcount.c | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH for-11.2 0/2] block/qcow2: Avoid crash in qemu-img amend when reftable is empty
  2026-11-19 14:03 [PATCH for-11.2 0/2] block/qcow2: Avoid crash in qemu-img amend when reftable is empty Peter Maydell
  2026-11-19 14:03 ` [PATCH for-11.2 1/2] block/qcow2: Detect corrupt images where first refcount block " Peter Maydell
  2026-11-19 14:03 ` [PATCH for-11.2 2/2] tests/qemu-iotest: Add test for qcow2 image with empty reftable Peter Maydell
@ 2026-11-20 17:32 ` Kevin Wolf
  2 siblings, 0 replies; 7+ messages in thread
From: Kevin Wolf @ 2026-11-20 17:32 UTC (permalink / raw)
  To: Peter Maydell; +Cc: qemu-devel, qemu-block, Hanna Reitz, qemu-stable

Am 19.11.2026 um 15:03 hat Peter Maydell geschrieben:
> In a QCOW2 image, the header is always cluster zero, and so that
> cluster must have a non-zero refcount.  This means that an image where
> the first entry in the refcount table is zero, or where it points to a
> refblock which is empty, must be corrupt.
> 
> In the "qemu-img amend -o refcount_bits=..." codepath, we were
> implicitly relying on the image not being corrupted in this way.  If
> the entire refcount table was zero, our "walk the table to see how big
> the new reftable size is" loop would say "zero", and we would then not
> allocate any memory for s->refcount_table, and
> update_max_refcount_table_index() would underflow when calculating
> s->refcount_table_size - 1 and then crash.
> 
> This patchset fixes that bug, and adds a test case for it.
> 
> It would also be possible to refuse to open an image for some of the
> more obvious cases of this corruption (e.g. "on open,
> refcount_table[0] is 0"), but checking the actual refcount of cluster
> 0 is a bit trickier as we only read in that refblock on demand. Plus
> some of the existing iotests (060, 098) rely on corrupted images of
> that kind only being detected later.  So I have stuck to fixing the
> immediate bug.
> 
> NB: this isn't a security issue because the crash can only be
> provoked for this specific qemu-img amend codepath, so it's a
> self-DoS.
> 
> thanks
> -- PMM
> 
> Peter Maydell (2):
>   block/qcow2: Detect corrupt images where first refcount block is empty
>   tests/qemu-iotest: Add test for qcow2 image with empty reftable

Thanks, applied to the block branch.

Kevin



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-20 17:33 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 14:03 [PATCH for-11.2 0/2] block/qcow2: Avoid crash in qemu-img amend when reftable is empty Peter Maydell
2026-11-19 14:03 ` [PATCH for-11.2 1/2] block/qcow2: Detect corrupt images where first refcount block " Peter Maydell
2026-11-19 18:48   ` Peter Maydell
2026-11-19 19:42   ` Eric Blake
2026-11-19 14:03 ` [PATCH for-11.2 2/2] tests/qemu-iotest: Add test for qcow2 image with empty reftable Peter Maydell
2026-11-19 19:41   ` Eric Blake
2026-11-20 17:32 ` [PATCH for-11.2 0/2] block/qcow2: Avoid crash in qemu-img amend when reftable is empty Kevin Wolf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.