Skip to contentSkip to conversation

qcow2-bitmap: Fix uint64_t left-shift overflow

PATCH Superseded Tuguoyi 3 participants 5 messages Oct 28, 2019 – Oct 30, 2019 Block layer

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tuguoyi

author
#0 lore raw

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

In check_constraints_on_bitmap(), 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. As a result, for a qcow2 disk with cluster_size set to 2M, we could not even create a dirty bitmap with default granularity. This patch fix the issue by dividing @len by granularity instead.

---
 block/qcow2-bitmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/qcow2-bitmap.c +2 -2
index 98294a7..71ac822 100644
@@ -172,8 +172,8 @@ static int check_constraints_on_bitmap(BlockDriverState *bs,
     }
 
     if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||
-        (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
-               granularity_bits))
+        (DIV_ROUND_UP(len, granularity) > (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 · 1 line
2.7.4
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Tuguoyi

In check_constraints_on_bitmap(), 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. As a result, for a qcow2 disk with cluster_size set to 2M, we could not even create a dirty bitmap with default granularity. This patch fix the issue by dividing @len by granularity instead.

The buggy code was introduced in 5f72826e7fc6216 (by me, sorry :(, so it's an old bug, appeared in 2.10. Still, I add stable to CC, as fix is very simple.

Fixes 5f72826e7fc62167cf3a
Quoted 20 lines
---
  block/qcow2-bitmap.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
block/qcow2-bitmap.c +2 -2
index 98294a7..71ac822 100644
@@ -172,8 +172,8 @@ static int check_constraints_on_bitmap(BlockDriverState *bs,
      }
 
      if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||
-        (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
-               granularity_bits))
+        (DIV_ROUND_UP(len, granularity) > (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
#2 lore raw
In reply to Tuguoyi
Quoted 18 lines · Tuguoyi

In check_constraints_on_bitmap(), 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. As a result, for a qcow2 disk with cluster_size set to 2M, we could not even create a dirty bitmap with default granularity. This patch fix the issue by dividing @len by granularity instead.

---
 block/qcow2-bitmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/qcow2-bitmap.c +0 -0
index 98294a7..71ac822 100644

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

Quoted 7 lines
block/qcow2-bitmap.c +2 -2
@@ -172,8 +172,8 @@ static int check_constraints_on_bitmap(BlockDriverState *bs,
     }
 
     if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||
-        (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
-               granularity_bits))
+        (DIV_ROUND_UP(len, granularity) > (uint64_t)BME_MAX_TABLE_SIZE *
+                s->cluster_size))

This didn’t change because of this patch, but doesn’t this comparison need a conversion of bits to bytes somewhere?

len / granularity gives us the number of bits needed for the bitmap.

BME_MAX_TABLE_SIZE is, as far as I can see, a number of bitmap clusters, so multiplying it by the cluster size gives the number of bytes in the bitmap. But the number of bits is eight times higher.

Another topic: Isn’t BME_MAX_TABLE_SIZE too big? As it is, bitmap tables can have a size of 1 GB, and that’s the table alone. Depending on the cluster size, the bitmap would take up at least 64 GB and cover at least 32 TB (at a granularity of 512 bytes).

Max

Quoted 4 lines
block/qcow2-bitmap.c +0 -0
     {
         error_setg(errp, "Too much space will be occupied by the bitmap. "
                    "Use larger granularity");
#3 lore raw
Quoted 30 lines · Max Reitz
Quoted 18 lines · Tuguoyi

In check_constraints_on_bitmap(), 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. As a result, for a qcow2 disk with cluster_size set to 2M, we could not even create a dirty bitmap with default granularity. This patch fix the issue by dividing @len by granularity instead.

---
  block/qcow2-bitmap.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
block/qcow2-bitmap.c +0 -0
index 98294a7..71ac822 100644

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

Quoted 7 lines
block/qcow2-bitmap.c +2 -2
@@ -172,8 +172,8 @@ static int check_constraints_on_bitmap(BlockDriverState *bs,
      }
 
      if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||
-        (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
-               granularity_bits))
+        (DIV_ROUND_UP(len, granularity) > (uint64_t)BME_MAX_TABLE_SIZE *
+                s->cluster_size))

This didn’t change because of this patch, but doesn’t this comparison need a conversion of bits to bytes somewhere?

Quoted 5 lines

len / granularity gives us the number of bits needed for the bitmap.

BME_MAX_TABLE_SIZE is, as far as I can see, a number of bitmap clusters, so multiplying it by the cluster size gives the number of bytes in the bitmap. But the number of bits is eight times higher.

I think you are right. It would be better to fix it in the same patch..

Quoted 2 lines

Another topic: Isn’t BME_MAX_TABLE_SIZE too big?

Maybe) Still, I don't sure that we need to change it..

Quoted 13 lines

As it is, bitmap
tables can have a size of 1 GB, and that’s the table alone. Depending on the cluster size, the bitmap would take up at least 64 GB and cover at least 32 TB (at a granularity of 512 bytes).

Max

Quoted 4 lines
block/qcow2-bitmap.c +0 -0
      {
          error_setg(errp, "Too much space will be occupied by the bitmap. "
                     "Use larger granularity");
Signature · 2 lines
Best regards,
Vladimir
author
#4 lore raw
Quoted 14 lines · On 29.10.2019 19:57 Vladimir Sementsov-Ogievskiy wrote:
Quoted 13 lines · Max Reitz
Quoted 12 lines · Tuguoyi

In check_constraints_on_bitmap(), 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. As a result, for a qcow2 disk with cluster_size set to 2M, we could not even create a dirty bitmap with default granularity. This patch fix the issue by dividing @len by granularity instead.

---
  block/qcow2-bitmap.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 9 lines
Quoted 5 lines

block/qcow2-bitmap.c static int

Quoted 5 lines
block/qcow2-bitmap.c index renamed from block/qcow2-bitmap.c +0 -0

98294a7..71ac822 100644

block/qcow2-bitmap.c +0 -0
@@ -172,8 +172,8 @@ static int

check_constraints_on_bitmap(BlockDriverState *bs,

Quoted 3 lines
Quoted 3 lines
block/qcow2-bitmap.c +0 -0
      }
 
      if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||

Here is the same problem, we need also to calculate the number of bits instead of bytes.

Quoted 9 lines
Quoted 3 lines

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

Quoted 3 lines
block/qcow2-bitmap.c +1 -2
-        (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
-               granularity_bits))
+        (DIV_ROUND_UP(len, granularity) >

(uint64_t)BME_MAX_TABLE_SIZE *

Quoted 5 lines

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

Quoted 1 line
block/qcow2-bitmap.c +1 -0
+                s->cluster_size))

This didn’t change because of this patch, but doesn’t this comparison need a conversion of bits to bytes somewhere?

Quoted 8 lines
Quoted 5 lines

len / granularity gives us the number of bits needed for the bitmap.

BME_MAX_TABLE_SIZE is, as far as I can see, a number of bitmap clusters, so multiplying it by the cluster size gives the number of bytes in the bitmap. But the number of bits is eight times higher.

I think you are right. It would be better to fix it in the same patch..

I think it would be better to add a inline function to calculate the max bytes of bitmap with given virtual size and granularity.

I'll send another patch later

Quoted 24 lines
Quoted 2 lines

Another topic: Isn’t BME_MAX_TABLE_SIZE too big?

Maybe) Still, I don't sure that we need to change it..

Quoted 9 lines

As it is, bitmap
tables can have a size of 1 GB, and that’s the table alone. Depending on the cluster size, the bitmap would take up at least 64 GB and cover at least 32 TB (at a granularity of 512 bytes).

Max

Quoted 2 lines
block/qcow2-bitmap.c +0 -0
      {
          error_setg(errp, "Too much space will be occupied by the

bitmap. "

Quoted 4 lines

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

Quoted 2 lines
block/qcow2-bitmap.c +0 -0
                     "Use larger granularity");

--
Best regards,
Vladimir