0a122b
From ac19619fcd0eb051d57e5519a9fe8983b9e02fc0 Mon Sep 17 00:00:00 2001
0a122b
From: Max Reitz <mreitz@redhat.com>
0a122b
Date: Sat, 15 Feb 2014 16:03:48 +0100
0a122b
Subject: [PATCH 3/5] qcow2: fix offset overflow in qcow2_alloc_clusters_at()
0a122b
0a122b
RH-Author: Max Reitz <mreitz@redhat.com>
0a122b
Message-id: <1392480230-24011-3-git-send-email-mreitz@redhat.com>
0a122b
Patchwork-id: 57293
0a122b
O-Subject: [RHEL-7.0 qemu-kvm PATCH 2/4] qcow2: fix offset overflow in qcow2_alloc_clusters_at()
0a122b
Bugzilla: 1049176
0a122b
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
0a122b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
RH-Acked-by: Fam Zheng <famz@redhat.com>
0a122b
0a122b
From: Hu Tao <hutao@cn.fujitsu.com>
0a122b
0a122b
BZ: 1049176
0a122b
BZ: 1055848
0a122b
0a122b
When cluster size is big enough it can lead to an offset overflow
0a122b
in qcow2_alloc_clusters_at(). This patch fixes it.
0a122b
0a122b
The allocation is stopped each time at L2 table boundary
0a122b
(see handle_alloc()), so the possible maximum bytes could be
0a122b
0a122b
  2^(cluster_bits - 3 + cluster_bits)
0a122b
0a122b
cluster_bits - 3 is used to compute the number of entry by L2
0a122b
and the additional cluster_bits is to take into account each
0a122b
clusters referenced by the L2 entries.
0a122b
0a122b
so int is safe for cluster_bits<=17, unsafe otherwise.
0a122b
0a122b
Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
0a122b
Reviewed-by: Max Reitz <mreitz@redhat.com>
0a122b
Reviewed-by: Benoit Canet <benoit@irqsave.net>
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
(cherry picked from commit 33304ec9fa484e765c6249673e09e1b7d49c5b85)
0a122b
0a122b
Signed-off-by: Max Reitz <mreitz@redhat.com>
0a122b
---
0a122b
 block/qcow2-refcount.c | 8 +++++++-
0a122b
 1 file changed, 7 insertions(+), 1 deletion(-)
0a122b
0a122b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
0a122b
---
0a122b
 block/qcow2-refcount.c |    8 +++++++-
0a122b
 1 files changed, 7 insertions(+), 1 deletions(-)
0a122b
0a122b
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
0a122b
index 389a837..09c638f 100644
0a122b
--- a/block/qcow2-refcount.c
0a122b
+++ b/block/qcow2-refcount.c
0a122b
@@ -676,7 +676,13 @@ int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
0a122b
     BDRVQcowState *s = bs->opaque;
0a122b
     uint64_t cluster_index;
0a122b
     uint64_t old_free_cluster_index;
0a122b
-    int i, refcount, ret;
0a122b
+    uint64_t i;
0a122b
+    int refcount, ret;
0a122b
+
0a122b
+    assert(nb_clusters >= 0);
0a122b
+    if (nb_clusters == 0) {
0a122b
+        return 0;
0a122b
+    }
0a122b
 
0a122b
     /* Check how many clusters there are free */
0a122b
     cluster_index = offset >> s->cluster_bits;
0a122b
-- 
0a122b
1.7.1
0a122b