block: qcow: Reject backing file size with zero offset

A qcow2 header with backing_file_offset == 0 indicates the image
has no backing file, so any non-zero backing_file_size is malformed.
Qemu silently ignores the size in this case, which hides image
corruption. Reject it explicitly with a dedicated error so the user
gets a clear diagnostic.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
Anatol Belski
2026-06-01 16:25:32 +02:00
committed by Rob Bradford
parent 4294a4b862
commit 39e9376f5b
2 changed files with 7 additions and 0 deletions

View File

@@ -331,6 +331,11 @@ impl QcowHeader {
if header.backing_file_size > MAX_BACKING_FILE_SIZE {
return Err(Error::BackingFileTooLong(header.backing_file_size as usize));
}
if header.backing_file_offset == 0 && header.backing_file_size != 0 {
return Err(Error::BackingFileSizeWithoutOffset(
header.backing_file_size,
));
}
if header.backing_file_offset != 0 {
let cluster_size = 1u64
.checked_shl(header.cluster_bits)

View File

@@ -74,6 +74,8 @@ pub enum Error {
BackingFileOutsideFirstCluster(u64, u32, u64),
#[error("Backing file name at offset {0:#x} length {1:#x} overlaps header of size {2:#x}")]
BackingFileOverlapsHeader(u64, u32, u32),
#[error("Backing file size {0:#x} with zero offset")]
BackingFileSizeWithoutOffset(u32),
#[error("Backing file support is disabled")]
BackingFilesDisabled,
#[error("Backing file name is too long: {0} bytes over")]