From d20418ee514774626ac47a1ad0aa9149c7249cf0 Mon Sep 17 00:00:00 2001 From: Max Reitz Date: Tue, 28 Oct 2014 11:12:32 +0100 Subject: [PATCH] block/vdi: Limit maximum size even futher The block layer read and write functions do not like requests which are bigger than INT_MAX bytes. Since the VDI bmap is read and written in a single operation, its size is therefore limited accordingly. This reduces the maximum VDI image size supported by QEMU to half of what it currently is (down to approximately 512 TB). The VDI test 084 has to be adapted accordingly. Actually, one could clearly see that it was broken from the "Could not open 'TEST_DIR/t.IMGFMT': Invalid argument" line for an image which was supposed to work just fine. Signed-off-by: Max Reitz Reviewed-by: Stefan Hajnoczi Reviewed-by: Peter Lieven --- block/vdi.c | 14 ++++++++++++-- tests/qemu-iotests/084 | 14 +++++++------- tests/qemu-iotests/084.out | 13 ++++++++----- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/block/vdi.c b/block/vdi.c index e1d211c9f7..39070b75e8 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -120,8 +120,18 @@ typedef unsigned char uuid_t[16]; #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED) -/* max blocks in image is (0xffffffff / 4) */ -#define VDI_BLOCKS_IN_IMAGE_MAX 0x3fffffff +/* The bmap will take up VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) bytes; since + * the bmap is read and written in a single operation, its size needs to be + * limited to INT_MAX; furthermore, when opening an image, the bmap size is + * rounded up to be aligned on BDRV_SECTOR_SIZE. + * Therefore this should satisfy the following: + * VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) + BDRV_SECTOR_SIZE == INT_MAX + 1 + * (INT_MAX + 1 is the first value not representable as an int) + * This guarantees that any value below or equal to the constant will, when + * multiplied by sizeof(uint32_t) and rounded up to a BDRV_SECTOR_SIZE boundary, + * still be below or equal to INT_MAX. */ +#define VDI_BLOCKS_IN_IMAGE_MAX \ + ((unsigned)((INT_MAX + 1u - BDRV_SECTOR_SIZE) / sizeof(uint32_t))) #define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \ (uint64_t)DEFAULT_CLUSTER_SIZE) diff --git a/tests/qemu-iotests/084 b/tests/qemu-iotests/084 index 2712c023a9..733018d4a8 100755 --- a/tests/qemu-iotests/084 +++ b/tests/qemu-iotests/084 @@ -66,15 +66,15 @@ stat -c"disk image file size in bytes: %s" "${TEST_IMG}" # check for image size too large # poke max image size, and appropriate blocks_in_image value -echo "Test 1: Maximum size (1024 TB):" -poke_file "$TEST_IMG" "$ds_offset" "\x00\x00\xf0\xff\xff\xff\x03\x00" -poke_file "$TEST_IMG" "$bii_offset" "\xff\xff\xff\x3f" +echo "Test 1: Maximum size (512 TB - 128 MB):" +poke_file "$TEST_IMG" "$ds_offset" "\x00\x00\x00\xf8\xff\xff\x01\x00" +poke_file "$TEST_IMG" "$bii_offset" "\x80\xff\xff\x1f" _img_info echo -echo "Test 2: Size too large (1024TB + 1)" +echo "Test 2: Size too large (512 TB - 128 MB + 64 kB)" # This should be too large (-EINVAL): -poke_file "$TEST_IMG" "$ds_offset" "\x00\x00\xf1\xff\xff\xff\x03\x00" +poke_file "$TEST_IMG" "$ds_offset" "\x00\x00\x01\xf8\xff\xff\x01\x00" _img_info echo @@ -89,9 +89,9 @@ _img_info echo echo "Test 4: Size valid (64M), but Blocks In Image exceeds max allowed" -# Now check the bounds of blocks_in_image - 0x3fffffff should be the max +# Now check the bounds of blocks_in_image - 0x1fffff80 should be the max # value here, and we should get -ENOTSUP -poke_file "$TEST_IMG" "$bii_offset" "\x00\x00\x00\x40" +poke_file "$TEST_IMG" "$bii_offset" "\x81\xff\xff\x1f" _img_info # Finally, 1MB is the only block size supported. Verify that diff --git a/tests/qemu-iotests/084.out b/tests/qemu-iotests/084.out index ea29ae0b9d..5ece8299c8 100644 --- a/tests/qemu-iotests/084.out +++ b/tests/qemu-iotests/084.out @@ -17,17 +17,20 @@ file format: IMGFMT virtual size: 64M (67108864 bytes) cluster_size: 1048576 disk image file size in bytes: 1024 -Test 1: Maximum size (1024 TB): -qemu-img: Could not open 'TEST_DIR/t.IMGFMT': Could not open 'TEST_DIR/t.IMGFMT': Invalid argument +Test 1: Maximum size (512 TB - 128 MB): +image: TEST_DIR/t.IMGFMT +file format: IMGFMT +virtual size: 512T (562949819203584 bytes) +cluster_size: 1048576 -Test 2: Size too large (1024TB + 1) -qemu-img: Could not open 'TEST_DIR/t.IMGFMT': Unsupported VDI image size (size is 0x3fffffff10000, max supported is 0x3fffffff00000) +Test 2: Size too large (512 TB - 128 MB + 64 kB) +qemu-img: Could not open 'TEST_DIR/t.IMGFMT': Unsupported VDI image size (size is 0x1fffff8010000, max supported is 0x1fffff8000000) Test 3: Size valid (64M), but Blocks In Image too small (63) qemu-img: Could not open 'TEST_DIR/t.IMGFMT': unsupported VDI image (disk size 67108864, image bitmap has room for 66060288) Test 4: Size valid (64M), but Blocks In Image exceeds max allowed -qemu-img: Could not open 'TEST_DIR/t.IMGFMT': unsupported VDI image (too many blocks 1073741824, max is 1073741823) +qemu-img: Could not open 'TEST_DIR/t.IMGFMT': unsupported VDI image (too many blocks 536870785, max is 536870784) Test 5: Valid Image: 64MB, Blocks In Image 64, Block Size 1MB image: TEST_DIR/t.IMGFMT