Skip to contentSkip to conversation

qcow2-bitmap: Fix uint64_t left-shift overflow

PATCH Applied Tuguoyi 3 participants 6 messages Nov 1, 2019 – Nov 5, 2019 Block layer

lore
Share & tools
lore

Tuguoyi

author
#0 lore raw

PATCH v4 1/1 qcow2-bitmap: Fix uint64_t left-shift overflow

There are two issues in In check_constraints_on_bitmap(),
1) The sanity check on the granularity will cause uint64_t integer left-shift overflow when cluster_size is 2M and the granularity is BIGGER than 32K.
2) The way to calculate image size that the maximum bitmap supported can map to is a bit incorrect.
This patch fix it by add a helper function to calculate the number of bytes needed by a normal bitmap in image and compare it to the maximum bitmap bytes supported by qemu.

Fixes 5f72826e7fc62167cf3a
---
 block/qcow2-bitmap.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
block/qcow2-bitmap.c +11 -3
index 98294a7..ef9ef62 100644
@@ -142,6 +142,13 @@ static int check_table_entry(uint64_t entry, int cluster_size)
     return 0;
 }
 
+static int64_t get_bitmap_bytes_needed(int64_t len, uint32_t granularity)
+{
+    int64_t num_bits = DIV_ROUND_UP(len, granularity);
+
+    return DIV_ROUND_UP(num_bits, 8);
+}
+
 static int check_constraints_on_bitmap(BlockDriverState *bs,
                                        const char *name,
                                        uint32_t granularity,
@@ -150,6 +157,7 @@ static int check_constraints_on_bitmap(BlockDriverState *bs,
     BDRVQcow2State *s = bs->opaque;
     int granularity_bits = ctz32(granularity);
     int64_t len = bdrv_getlength(bs);
+    int64_t bitmap_bytes;
 
     assert(granularity > 0);
     assert((granularity & (granularity - 1)) == 0);
@@ -171,9 +179,9 @@ static int check_constraints_on_bitmap(BlockDriverState *bs,
         return -EINVAL;
     }
 
-    if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||
-        (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
-               granularity_bits))
+    bitmap_bytes = get_bitmap_bytes_needed(len, granularity);
+    if ((bitmap_bytes > (uint64_t)BME_MAX_PHYS_SIZE) ||
+        (bitmap_bytes > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size))
     {
         error_setg(errp, "Too much space will be occupied by the bitmap. "
                    "Use larger granularity");
Signature · 5 lines
2.7.4

[Patch v3]: https://lists.gnu.org/archive/html/qemu-devel/2019-10/msg07989.html
[Patch v2]: https://lists.gnu.org/archive/html/qemu-devel/2019-10/msg07490.html
[Patch v1]: https://lists.gnu.org/archive/html/qemu-devel/2019-10/msg07336.html
Reviewed-by on v4 1/1
#1 lore raw
Quoted 10 lines · Tuguoyi

There are two issues in In check_constraints_on_bitmap(),
1) The sanity check on the granularity will cause uint64_t integer left-shift overflow when cluster_size is 2M and the granularity is BIGGER than 32K.
2) The way to calculate image size that the maximum bitmap supported can map to is a bit incorrect.
This patch fix it by add a helper function to calculate the number of bytes needed by a normal bitmap in image and compare it to the maximum bitmap bytes supported by qemu.

Quoted 2 lines
Fixes 5f72826e7fc62167cf3a

You forget my

(I don't see changes except add "Fixes: " to commit msg and put get_bitmap_bytes_needed definition header into one line.)

Quoted 44 lines
---
  block/qcow2-bitmap.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
block/qcow2-bitmap.c +11 -3
index 98294a7..ef9ef62 100644
@@ -142,6 +142,13 @@ static int check_table_entry(uint64_t entry, int cluster_size)
      return 0;
  }
 
+static int64_t get_bitmap_bytes_needed(int64_t len, uint32_t granularity)
+{
+    int64_t num_bits = DIV_ROUND_UP(len, granularity);
+
+    return DIV_ROUND_UP(num_bits, 8);
+}
+
  static int check_constraints_on_bitmap(BlockDriverState *bs,
                                         const char *name,
                                         uint32_t granularity,
@@ -150,6 +157,7 @@ static int check_constraints_on_bitmap(BlockDriverState *bs,
      BDRVQcow2State *s = bs->opaque;
      int granularity_bits = ctz32(granularity);
      int64_t len = bdrv_getlength(bs);
+    int64_t bitmap_bytes;
 
      assert(granularity > 0);
      assert((granularity & (granularity - 1)) == 0);
@@ -171,9 +179,9 @@ static int check_constraints_on_bitmap(BlockDriverState *bs,
          return -EINVAL;
      }
 
-    if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||
-        (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
-               granularity_bits))
+    bitmap_bytes = get_bitmap_bytes_needed(len, granularity);
+    if ((bitmap_bytes > (uint64_t)BME_MAX_PHYS_SIZE) ||
+        (bitmap_bytes > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size))
      {
          error_setg(errp, "Too much space will be occupied by the bitmap. "
                     "Use larger granularity");
Signature · 2 lines
Best regards,
Vladimir
author
#2 lore raw
Quoted 11 lines · On 01.11.2019 17:25 Vladimir Sementsov-Ogievskiy wrote:
Quoted 10 lines · Tuguoyi

There are two issues in In check_constraints_on_bitmap(),
1) The sanity check on the granularity will cause uint64_t integer left-shift overflow when cluster_size is 2M and the granularity is BIGGER than 32K.
2) The way to calculate image size that the maximum bitmap supported can map to is a bit incorrect.
This patch fix it by add a helper function to calculate the number of bytes needed by a normal bitmap in image and compare it to the maximum bitmap bytes supported by qemu.

Quoted 5 lines
Quoted 2 lines
Fixes 5f72826e7fc62167cf3a

You forget my

Sorry for that, it's my first time to submit patch to qemu, and should I send another patch or not ?

Quoted 2 lines

(I don't see changes except add "Fixes: " to commit msg and put get_bitmap_bytes_needed definition header into one line.)

Yes, Only minor changes are made, including removing 'inline' keyword.

Thanks for your patience in reviewing

Quoted 52 lines

block/qcow2-bitmap.c static int check_table_entry(uint64_t entry, int

Quoted 9 lines
---
  block/qcow2-bitmap.c | 14 +++++++++++---
  1 file changed, 11 insertions(+), 3 deletions(-)
block/qcow2-bitmap.c index renamed from block/qcow2-bitmap.c +0 -0

98294a7..ef9ef62 100644

block/qcow2-bitmap.c +0 -0
@@ -142,6 +142,13 @@ static int check_table_entry(uint64_t entry, int

cluster_size)

block/qcow2-bitmap.c static int check_table_entry(uint64_t entry, int cluster_size)

Quoted 13 lines
block/qcow2-bitmap.c +6 -1
      return 0;
  }
 
+static int64_t get_bitmap_bytes_needed(int64_t len, uint32_t
+granularity) {
+    int64_t num_bits = DIV_ROUND_UP(len, granularity);
+
+    return DIV_ROUND_UP(num_bits, 8); }
+
  static int check_constraints_on_bitmap(BlockDriverState *bs,
                                         const char *name,
                                         uint32_t granularity, @@
-150,6 +157,7 @@ static int check_constraints_on_bitmap(BlockDriverState

*bs,

Quoted 11 lines
      BDRVQcow2State *s = bs->opaque;
      int granularity_bits = ctz32(granularity);
      int64_t len = bdrv_getlength(bs);

+ int64_t bitmap_bytes;

      assert(granularity > 0);
      assert((granularity & (granularity - 1)) == 0); @@ -171,9 +179,9
@@ static int check_constraints_on_bitmap(BlockDriverState *bs,
          return -EINVAL;
      }

block/qcow2-bitmap.c static int check_constraints_on_bitmap(BlockDriverState *bs,

Quoted 9 lines
block/qcow2-bitmap.c +4 -3
-    if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||
-        (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
-               granularity_bits))
+    bitmap_bytes = get_bitmap_bytes_needed(len, granularity);
+    if ((bitmap_bytes > (uint64_t)BME_MAX_PHYS_SIZE) ||
+        (bitmap_bytes > (uint64_t)BME_MAX_TABLE_SIZE *
+ s->cluster_size))
      {
          error_setg(errp, "Too much space will be occupied by the

bitmap. "

Quoted 2 lines
                     "Use larger granularity");

--
Best regards,
Vladimir

#3 lore raw
Quoted 12 lines · Tuguoyi
Quoted 11 lines · On 01.11.2019 17:25 Vladimir Sementsov-Ogievskiy wrote:
Quoted 10 lines · Tuguoyi

There are two issues in In check_constraints_on_bitmap(),
1) The sanity check on the granularity will cause uint64_t integer left-shift overflow when cluster_size is 2M and the granularity is BIGGER than 32K.
2) The way to calculate image size that the maximum bitmap supported can map to is a bit incorrect.
This patch fix it by add a helper function to calculate the number of bytes needed by a normal bitmap in image and compare it to the maximum bitmap bytes supported by qemu.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Fixes 5f72826e7fc62167cf3a

You forget my

Sorry for that, it's my first time to submit patch to qemu, and should I send another patch or not ?

Good start!

No, you shouldn't. Maintainer will take such marks (and may be other proposed improvements for commit message like this "Fixes: ", when applying, so no reason to resend.

Also, when sending a new version of patch, summarize what was changed since previous version (you may do it under "---" which follows commit message, or in cover letter if it's a patch set with several patches.

Quoted 59 lines
Quoted 3 lines

(I don't see changes except add "Fixes: " to commit msg and put get_bitmap_bytes_needed definition header into one line.)

Yes, Only minor changes are made, including removing 'inline' keyword.

Thanks for your patience in reviewing >

Quoted 52 lines

block/qcow2-bitmap.c static int check_table_entry(uint64_t entry, int

Quoted 9 lines
---
   block/qcow2-bitmap.c | 14 +++++++++++---
   1 file changed, 11 insertions(+), 3 deletions(-)
block/qcow2-bitmap.c index renamed from block/qcow2-bitmap.c +0 -0

98294a7..ef9ef62 100644

block/qcow2-bitmap.c +0 -0
@@ -142,6 +142,13 @@ static int check_table_entry(uint64_t entry, int

cluster_size)

block/qcow2-bitmap.c static int check_table_entry(uint64_t entry, int cluster_size)

Quoted 13 lines
block/qcow2-bitmap.c +6 -1
       return 0;
   }
 
+static int64_t get_bitmap_bytes_needed(int64_t len, uint32_t
+granularity) {
+    int64_t num_bits = DIV_ROUND_UP(len, granularity);
+
+    return DIV_ROUND_UP(num_bits, 8); }
+
   static int check_constraints_on_bitmap(BlockDriverState *bs,
                                          const char *name,
                                          uint32_t granularity, @@
-150,6 +157,7 @@ static int check_constraints_on_bitmap(BlockDriverState

*bs,

Quoted 11 lines
       BDRVQcow2State *s = bs->opaque;
       int granularity_bits = ctz32(granularity);
       int64_t len = bdrv_getlength(bs);

+ int64_t bitmap_bytes;

       assert(granularity > 0);
       assert((granularity & (granularity - 1)) == 0); @@ -171,9 +179,9
@@ static int check_constraints_on_bitmap(BlockDriverState *bs,
           return -EINVAL;
       }

block/qcow2-bitmap.c static int check_constraints_on_bitmap(BlockDriverState *bs,

Quoted 9 lines
block/qcow2-bitmap.c +4 -3
-    if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||
-        (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
-               granularity_bits))
+    bitmap_bytes = get_bitmap_bytes_needed(len, granularity);
+    if ((bitmap_bytes > (uint64_t)BME_MAX_PHYS_SIZE) ||
+        (bitmap_bytes > (uint64_t)BME_MAX_TABLE_SIZE *
+ s->cluster_size))
       {
           error_setg(errp, "Too much space will be occupied by the

bitmap. "

Quoted 2 lines
                      "Use larger granularity");

--
Best regards,
Vladimir

Signature · 2 lines
Best regards,
Vladimir
author
#4 lore raw
Quoted 24 lines · Vladimir Sementsov-Ogievskiy
Quoted 12 lines · Tuguoyi
Quoted 11 lines · On 01.11.2019 17:25 Vladimir Sementsov-Ogievskiy wrote:
Quoted 10 lines · Tuguoyi

There are two issues in In check_constraints_on_bitmap(),
1) The sanity check on the granularity will cause uint64_t integer left-shift overflow when cluster_size is 2M and the granularity is BIGGER than 32K.
2) The way to calculate image size that the maximum bitmap supported can map to is a bit incorrect.
This patch fix it by add a helper function to calculate the number of bytes needed by a normal bitmap in image and compare it to the maximum bitmap bytes supported by qemu.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Fixes 5f72826e7fc62167cf3a

You forget my

Sorry for that, it's my first time to submit patch to qemu, and should I send

another patch or not ?

Good start!

Quoted 7 lines

No, you shouldn't. Maintainer will take such marks (and may be other proposed improvements for commit message like this "Fixes: ", when applying, so no reason to resend.

Also, when sending a new version of patch, summarize what was changed since previous version (you may do it under "---" which follows commit message, or in cover letter if it's a patch set with several patches.

OK, I got it, thanks a lot.

Quoted 67 lines
Quoted 21 lines
Quoted 3 lines

(I don't see changes except add "Fixes: " to commit msg and put get_bitmap_bytes_needed definition header into one line.)

Yes, Only minor changes are made, including removing 'inline' keyword.

Thanks for your patience in reviewing >

Quoted 14 lines
Quoted 10 lines
---
   block/qcow2-bitmap.c | 14 +++++++++++---
   1 file changed, 11 insertions(+), 3 deletions(-)
block/qcow2-bitmap.c index renamed from block/qcow2-bitmap.c +0 -0

98294a7..ef9ef62 100644

block/qcow2-bitmap.c +0 -0
@@ -142,6 +142,13 @@ static int check_table_entry(uint64_t entry,

int

cluster_size)

Quoted 3 lines
       return 0;

}

Quoted 9 lines
Quoted 9 lines

block/qcow2-bitmap.c static int check_table_entry(uint64_t entry, int cluster_size) · line 154

Quoted 9 lines
block/qcow2-bitmap.c +6 -0
+static int64_t get_bitmap_bytes_needed(int64_t len, uint32_t
+granularity) {
+    int64_t num_bits = DIV_ROUND_UP(len, granularity);
+
+    return DIV_ROUND_UP(num_bits, 8); }
+
   static int check_constraints_on_bitmap(BlockDriverState *bs,
                                          const char *name,
                                          uint32_t granularity,

@@

Quoted 23 lines
Quoted 23 lines
Quoted 2 lines

-150,6 +157,7 @@ static int
check_constraints_on_bitmap(BlockDriverState

*bs,

Quoted 20 lines
       BDRVQcow2State *s = bs->opaque;
       int granularity_bits = ctz32(granularity);
       int64_t len = bdrv_getlength(bs);
block/qcow2-bitmap.c +6 -3
+    int64_t bitmap_bytes;
 
       assert(granularity > 0);
       assert((granularity & (granularity - 1)) == 0); @@ -171,9
+179,9 @@ static int check_constraints_on_bitmap(BlockDriverState *bs,
           return -EINVAL;
       }
 
-    if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||
-        (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
-               granularity_bits))
+    bitmap_bytes = get_bitmap_bytes_needed(len, granularity);
+    if ((bitmap_bytes > (uint64_t)BME_MAX_PHYS_SIZE) ||
+        (bitmap_bytes > (uint64_t)BME_MAX_TABLE_SIZE *
+ s->cluster_size))
       {
           error_setg(errp, "Too much space will be occupied by the
Quoted 8 lines
Quoted 8 lines

bitmap. "

Quoted 2 lines
                      "Use larger granularity");

--
Best regards,
Vladimir

--
Best regards,
Vladimir

Applied
#5 lore raw
In reply to Tuguoyi
Quoted 10 lines · Tuguoyi

There are two issues in In check_constraints_on_bitmap(),
1) The sanity check on the granularity will cause uint64_t integer left-shift overflow when cluster_size is 2M and the granularity is BIGGER than 32K.
2) The way to calculate image size that the maximum bitmap supported can map to is a bit incorrect.
This patch fix it by add a helper function to calculate the number of bytes needed by a normal bitmap in image and compare it to the maximum bitmap bytes supported by qemu.

Quoted 5 lines
Fixes 5f72826e7fc62167cf3a
---
 block/qcow2-bitmap.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

Thanks, applied to my block branch:

https://git.xanclic.moe/XanClic/qemu/commits/branch/block

Max