Skip to contentSkip to conversation

block: Fix error code for bdrv_getlength when the image is too big

PATCH Superseded Fam Zheng 3 participants 7 messages May 15, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

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
author
#1 lore raw

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
index 7904098..5ee3fdf 100644
@@ -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
#2 lore raw

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

#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] block: Detect multiplication overflow in bdrv_getlength

In reply to Fam Zheng
Quoted 15 lines · Fam Zheng

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 +0 -0
index 7904098..5ee3fdf 100644

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

author
#4 lore raw

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_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 +0 -0
index 7904098..5ee3fdf 100644

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.

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

author
#5 lore raw

PATCH 2/2 qemu-iotests: qemu-img info on afl VMDK image with a huge capacity

In reply to Fam Zheng

The image is contributed by Richard W.M. Jones.

Cc Richard W.M. Jones <[email protected]>
---
 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
index 50ca5ce..0ded0c3 100755
@@ -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
index cbb0de4..67e3cf5 100644
@@ -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
new file mode 100644
index 0000000000000000000000000000000000000000..03615d36a12425cf4240bab86f4cfe648db14572
GIT binary patch
Signature · 1 line
2.4.0