Skip to content

Commit b495764

Browse files
kevmwmdroth
authored andcommitted
qcow2: Fix header extension size check
After reading the extension header, offset is incremented, but not checked against end_offset any more. This way an integer overflow could happen when checking whether the extension end is within the allowed range, effectively disabling the check. This patch adds the missing check and a test case for it. Cc: [email protected] Reported-by: Max Reitz <[email protected]> Signed-off-by: Kevin Wolf <[email protected]> Reviewed-by: Max Reitz <[email protected]> Message-id: [email protected] Signed-off-by: Stefan Hajnoczi <[email protected]> Signed-off-by: Kevin Wolf <[email protected]> (cherry picked from commit 2ebafc8) Signed-off-by: Michael Roth <[email protected]>
1 parent 21640bf commit b495764

File tree

3 files changed

+5
-1
lines changed

3 files changed

+5
-1
lines changed

block/qcow2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
114114
#ifdef DEBUG_EXT
115115
printf("ext.magic = 0x%x\n", ext.magic);
116116
#endif
117-
if (ext.len > end_offset - offset) {
117+
if (offset > end_offset || ext.len > end_offset - offset) {
118118
error_setg(errp, "Header extension too large");
119119
return -EINVAL;
120120
}

tests/qemu-iotests/080

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ poke_file "$TEST_IMG" "$offset_backing_file_offset" "\xff\xff\xff\xff\xff\xff\xf
7878
poke_file "$TEST_IMG" "$offset_ext_magic" "\x12\x34\x56\x78"
7979
poke_file "$TEST_IMG" "$offset_ext_size" "\x7f\xff\xff\xff"
8080
{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
81+
poke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\x00\x00\x00\x00\x$(printf %x $offset_ext_size)"
82+
{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
8183
poke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\x00\x00\x00\x00\x00"
8284
{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
8385

tests/qemu-iotests/080.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ qemu-io: can't open device TEST_DIR/t.qcow2: Invalid backing file offset
1313
no file open, try 'help open'
1414
qemu-io: can't open device TEST_DIR/t.qcow2: Header extension too large
1515
no file open, try 'help open'
16+
qemu-io: can't open device TEST_DIR/t.qcow2: Header extension too large
17+
no file open, try 'help open'
1618

1719
== Huge refcount table size ==
1820
Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864

0 commit comments

Comments
 (0)