[PATCH for-11.2 0/2] block/qcow2: Avoid crash in qemu-img amend when reftable is empty

Peter Maydell posted 2 patches 3 days, 14 hours ago
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
[PATCH for-11.2 0/2] block/qcow2: Avoid crash in qemu-img amend when reftable is empty
Posted by Peter Maydell 3 days, 14 hours ago
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
Re: [PATCH for-11.2 0/2] block/qcow2: Avoid crash in qemu-img amend when reftable is empty
Posted by Kevin Wolf 2 days, 10 hours ago
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