Skip to contentSkip to conversation

qcow2-bitmap: Fix uint64_t left-shift overflow

PATCH Superseded Tuguoyi 2 participants 3 messages Oct 26, 2019 – Oct 28, 2019 Block layer

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Tuguoyi

author
#0 lore raw

PATCH 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 which is even smaller than the default value for a qcow2 disk with cluster_size set to 64k or bigger. This patch fix the issue by right-shift @len instead.

---
 block/qcow2-bitmap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/qcow2-bitmap.c +2 -2
index 98294a7..2a1d789 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))
+        ((len >> granularity_bits) > (uint64_t)BME_MAX_TABLE_SIZE *
+                s->cluster_size))
     {
         error_setg(errp, "Too much space will be occupied by the bitmap. "
                    "Use larger granularity");

--
2.7.4
-------------------------------------------------------------------------------------------------------------------------------------
本邮件及其附件含有新华三集团的保密信息,仅限于发送给上面地址中列出
的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、 或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本
邮件!
This e-mail and its attachments contain confidential information from New H3C, which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender by phone or email immediately and delete it!

#1 lore raw
Quoted 17 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 which is even smaller than the default value for a qcow2 disk with cluster_size set to 64k or bigger. This patch fix the issue by right-shift @len instead.

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

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

Quoted 5 lines
block/qcow2-bitmap.c +0 -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))

Hmm.
BME_MAX_TABLE_SIZE = 0x8000000

0x8000000 * 1024 * 1024 * 2 << 16 = 2 ** 64, so for 64k granularity it owerflows.. But for 32k doesn't. Or am I wrong?

Anyway, thanks for fixing!

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

Quoted 2 lines
block/qcow2-bitmap.c +2 -0
+        ((len >> granularity_bits) > (uint64_t)BME_MAX_TABLE_SIZE *
+                s->cluster_size))

It's a bit incorrect, as len may be unaligned, we need ((len + granularity - 1) >> granularity_bits) on the left, or better DIV_ROUNTD_UP(len, granularity).

Quoted 8 lines
block/qcow2-bitmap.c +0 -1
      {
          error_setg(errp, "Too much space will be occupied by the bitmap. "
                     "Use larger granularity");
--

2.7.4
-------------------------------------------------------------------------------------------------------------------------------------
本邮件及其附件含有新华三集团的保密信息,仅限于发送给上面地址中列出
的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、

Quoted 9 lines

或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本
邮件!
This e-mail and its attachments contain confidential information from New H3C, which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender by phone or email immediately and delete it!

Not sure that this is possible, as it's automatically available here:
https://lists.gnu.org/archive/html/qemu-devel/2019-10/msg07336.html

Signature · 2 lines
Best regards,
Vladimir
author
#2 lore raw
Quoted 35 lines

-----邮件原件-----
发件人: Vladimir Sementsov-Ogievskiy [mailto:[email protected]] 发送时间: 2019年10月27日 0:50
收件人: tuguoyi (Cloud) <[email protected]>; [email protected]; [email protected]; [email protected]
抄送: chengchiwen (Cloud) <[email protected]>; [email protected]; wangyongqing (Cloud) <[email protected]>; changlimin (Cloud) <[email protected]>; gaoliang (Cloud) <[email protected]>; wangyong (Cloud) <[email protected]> 主题: Re: [PATCH] qcow2-bitmap: Fix uint64_t left-shift overflow

block/qcow2-bitmap.c static int

Quoted 16 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 which is even smaller than the default value for a qcow2 disk with cluster_size set to 64k or bigger. This patch fix the issue by right-shift @len instead.

---
  block/qcow2-bitmap.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
block/qcow2-bitmap.c index renamed from block/qcow2-bitmap.c +0 -0

98294a7..2a1d789 100644

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

check_constraints_on_bitmap(BlockDriverState *bs,

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

Quoted 5 lines
block/qcow2-bitmap.c +0 -2
      }
 
      if ((len > (uint64_t)BME_MAX_PHYS_SIZE << granularity_bits) ||
-        (len > (uint64_t)BME_MAX_TABLE_SIZE * s->cluster_size <<
-               granularity_bits))
Quoted 6 lines

Hmm.
BME_MAX_TABLE_SIZE = 0x8000000

0x8000000 * 1024 * 1024 * 2 << 16 = 2 ** 64, so for 64k granularity it owerflows..
But for 32k doesn't. Or am I wrong?

You are right, it doesn't overflow for 32K.

Quoted 8 lines

Anyway, thanks for fixing!

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

Quoted 2 lines
block/qcow2-bitmap.c +2 -0
+        ((len >> granularity_bits) > (uint64_t)BME_MAX_TABLE_SIZE *
+                s->cluster_size))

It's a bit incorrect, as len may be unaligned, we need ((len + granularity - 1) >> granularity_bits) on the left, or better DIV_ROUNTD_UP(len, granularity).

Yes, @len should be ROUND-UP, thanks for pointing it out, and I'll fix it and send another patch

Quoted 25 lines

block/qcow2-bitmap.c static int · line 178

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

bitmap. "

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

2.7.4
----------------------------------------------------------------------
---------------------------------------------------------------
本邮件及其附件含有新华三集团的保密信息,仅限于发送给上面地址中

列出

Quoted 1 line

的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或

部分地泄露、复制、

Quoted 1 line

或散发)本邮件中的信息。如果您错收了本邮件,请您立即电话或邮件

通知发件人并删除本

Quoted 9 lines

邮件!
This e-mail and its attachments contain confidential information from New H3C, which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended recipient(s) is prohibited. If you receive this e-mail in error, please notify the sender by phone or email immediately and delete it!

Quoted 7 lines

Not sure that this is possible, as it's automatically available here:
https://lists.gnu.org/archive/html/qemu-devel/2019-10/msg07336.html

--
Best regards,
Vladimir

Thanks a lot for reviewing it