block: Fix error code for bdrv_getlength when the image is too big
cover block: Fix error code for bdrv_getlength when the image is too big
If the image has a huge enough virtual size,
$ qemu-img info afl9.img qemu-img: Can't get size of device 'image': Unknown error -512
It's because of the multiplication overflow in the return statement in bdrv_getlength (the big nagetive value is later truncated to 0x200). Fix it to return -EFBIG:
qemu-img: Can't get size of device 'image': File too large
Bug reported by Richard Jones in:
https://bugzilla.redhat.com/show_bug.cgi?id=1221499
Fam Zheng (2):
block: Detect multiplication overflow in bdrv_getlength
qemu-iotests: qemu-img info on afl VMDK image with a huge capacity
block.c | 1 + tests/qemu-iotests/059 | 5 +++++ tests/qemu-iotests/059.out | 3 +++ tests/qemu-iotests/sample_images/afl9.vmdk.bz2 | Bin 0 -> 178 bytes 4 files changed, 9 insertions(+) create mode 100644 tests/qemu-iotests/sample_images/afl9.vmdk.bz2
Signature · 1 line
2.4.0
PATCH 1/2 block: Detect multiplication overflow in bdrv_getlength
Bogus image may have a large total_sectors that will overflow the multiplication. For cleanness, fix the return code so the error message will be meaningful.
--- block.c | 1 + 1 file changed, 1 insertion(+)
block.c +1 -0
@@ -2330,6 +2330,7 @@ int64_t bdrv_getlength(BlockDriverState *bs) {int64_t ret =bdrv_nb_sectors (bs); + ret = (int64_t )(ret *BDRV_SECTOR_SIZE ) <0 ? -EFBIG : ret;return ret <0 ? ret : ret *BDRV_SECTOR_SIZE ; }
Signature · 1 line
2.4.0
Re: [Qemu-devel] [Qemu-block] [PATCH 1/2] block: Detect multiplication overflow in bdrv_getlength
Quoted 4 lines · Fam Zheng
int64_t ret = bdrv_nb_sectors(bs);+ ret = (int64_t)(ret * BDRV_SECTOR_SIZE) < 0 ? -EFBIG : ret; return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
Maybe in this case you're safe, but in general there's no guarantee that if there's an overflow the result will be negative.
You can do something like this instead:
ret = ret > INT64_MAX / BDRV_SECTOR_SIZE ? -EFBIG : ret;
Of course this is only valid if BDRV_SECTOR_SIZE != 0 ;)
Berto
Re: [Qemu-devel] [PATCH 1/2] block: Detect multiplication overflow in bdrv_getlength
In reply to Fam ZhengQuoted 15 lines · Fam Zheng
Bogus image may have a large
total_sectorsthat will overflow the multiplication. For cleanness, fix the return code so the error message will be meaningful.Reported-by Richard W.M. Jones <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- block.c | 1 + 1 file changed, 1 insertion(+)block.c +0
-0
block.c int64_t bdrv_getlength(BlockDriverState *bs) · line 2335
Quoted 6 lines
block.c +1
-0@@ -2330,6 +2330,7 @@ int64_t bdrv_getlength(BlockDriverState *bs) {int64_t ret =bdrv_nb_sectors (bs); + ret = (int64_t )(ret *BDRV_SECTOR_SIZE ) <0 ? -EFBIG : ret;return ret <0 ? ret : ret *BDRV_SECTOR_SIZE ; }
Signed integer overflow is undefined behavior. Your code works just fine on any remotely sane machine, *except* when the optimizer decides to use its undefined behavior license to mess with you.
A more prudent way to test for overflow would be something like
ret > INT64_MAX / BDRV_SECTOR_SIZE
Re: [Qemu-devel] [PATCH 1/2] block: Detect multiplication overflow in bdrv_getlength
Quoted 28 lines · Markus Armbruster
Quoted 15 lines · Fam Zheng
Bogus image may have a large
total_sectorsthat will overflow the multiplication. For cleanness, fix the return code so the error message will be meaningful.Reported-by Richard W.M. Jones <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- block.c | 1 + 1 file changed, 1 insertion(+)block.c +0
-0block.c int64_t bdrv_getlength(BlockDriverState *bs) · line 2335
Quoted 6 lines
block.c +1
-0@@ -2330,6 +2330,7 @@ int64_t bdrv_getlength(BlockDriverState *bs) {int64_t ret =bdrv_nb_sectors (bs); + ret = (int64_t )(ret *BDRV_SECTOR_SIZE ) <0 ? -EFBIG : ret;return ret <0 ? ret : ret *BDRV_SECTOR_SIZE ; }Signed integer overflow is undefined behavior. Your code works just fine on any remotely sane machine, *except* when the optimizer decides to use its undefined behavior license to mess with you.
Quoted 3 lines
A more prudent way to test for overflow would be something like
ret >
INT64_MAX/BDRV_SECTOR_SIZE
Yes, this is better, will fix.
Thanks,
Fam
PATCH 2/2 qemu-iotests: qemu-img info on afl VMDK image with a huge capacity
In reply to Fam ZhengThe image is contributed by Richard W.M. Jones.
--- tests/qemu-iotests/059 | 5 +++++ tests/qemu-iotests/059.out | 3 +++ tests/qemu-iotests/sample_images/afl9.vmdk.bz2 | Bin 0 -> 178 bytes 3 files changed, 8 insertions(+) create mode 100644 tests/qemu-iotests/sample_images/afl9.vmdk.bz2
tests/qemu-iotests/059 +5 -0
@@ -132,6 +132,11 @@ _img_info $QEMU_IO -c "write -P 0xa 900G 512" "$TEST_IMG" | _filter_qemu_io $QEMU_IO -c "read -v 900G 1024" "$TEST_IMG" | _filter_qemu_io +echo +echo "=== Testing afl image with a very large capacity ===" +_use_sample_img afl9.vmdk.bz2 +_img_info + # success, all done echo "*** done" rm -f $seq.full
tests/qemu-iotests/059.out +3 -0
@@ -2336,4 +2336,7 @@ e1000003e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ e1000003f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ read 1024/1024 bytes at offset 966367641600 1 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) + +=== Testing afl image with a very large capacity === +qemu-img: Can't get size of device 'image': File too large *** done
tests/qemu-iotests/sample_images/afl9.vmdk.bz2 new file binary +0 -0
Signature · 1 line
2.4.0
Re: [Qemu-devel] [Qemu-block] [PATCH 2/2] qemu-iotests: qemu-img info on afl VMDK image with a huge capacity
Quoted 4 lines · Fam Zheng
The image is contributed by Richard W.M. Jones.
Cc Richard W.M. Jones <[email protected]>Signed-off-by Fam Zheng <[email protected]>
Berto