yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
0a122b
From b2cd8e043a6b66ba225b98b6235fc050f956bf5b Mon Sep 17 00:00:00 2001
0a122b
From: Kevin Wolf <kwolf@redhat.com>
0a122b
Date: Tue, 25 Mar 2014 14:23:36 +0100
0a122b
Subject: [PATCH 29/49] qcow2: Check new refcount table size on growth
0a122b
0a122b
RH-Author: Kevin Wolf <kwolf@redhat.com>
0a122b
Message-id: <1395753835-7591-30-git-send-email-kwolf@redhat.com>
0a122b
Patchwork-id: n/a
0a122b
O-Subject: [virt-devel] [EMBARGOED RHEL-7.0 qemu-kvm PATCH 29/48] qcow2: Check new refcount table size on growth
0a122b
Bugzilla: 1066691
0a122b
RH-Acked-by: Jeff Cody <jcody@redhat.com>
0a122b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
0a122b
0a122b
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1066691
0a122b
Upstream status: Series embargoed
0a122b
0a122b
If the size becomes larger than what qcow2_open() would accept, fail the
0a122b
growing operation.
0a122b
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
0a122b
Conflicts:
0a122b
block/qcow2.h
0a122b
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
---
0a122b
 block/qcow2-refcount.c |    4 ++++
0a122b
 block/qcow2.c          |    4 +---
0a122b
 block/qcow2.h          |    9 +++++++++
0a122b
 3 files changed, 14 insertions(+), 3 deletions(-)
0a122b
0a122b
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
0a122b
index c08fb08..7b00dda 100644
0a122b
--- a/block/qcow2-refcount.c
0a122b
+++ b/block/qcow2-refcount.c
0a122b
@@ -310,6 +310,10 @@ static int alloc_refcount_block(BlockDriverState *bs,
0a122b
     uint64_t refcount_block_clusters = 1 << (s->cluster_bits - REFCOUNT_SHIFT);
0a122b
     uint64_t blocks_used = DIV_ROUND_UP(cluster_index, refcount_block_clusters);
0a122b
 
0a122b
+    if (blocks_used > QCOW_MAX_REFTABLE_SIZE / sizeof(uint64_t)) {
0a122b
+        return -EFBIG;
0a122b
+    }
0a122b
+
0a122b
     /* And now we need at least one block more for the new metadata */
0a122b
     uint64_t table_size = next_refcount_table_size(s, blocks_used + 1);
0a122b
     uint64_t last_table_size;
0a122b
diff --git a/block/qcow2.c b/block/qcow2.c
0a122b
index 87f2958..db9f667 100644
0a122b
--- a/block/qcow2.c
0a122b
+++ b/block/qcow2.c
0a122b
@@ -608,9 +608,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
0a122b
     s->refcount_table_size =
0a122b
         header.refcount_table_clusters << (s->cluster_bits - 3);
0a122b
 
0a122b
-    if (header.refcount_table_clusters > (0x800000 >> s->cluster_bits)) {
0a122b
-        /* 8 MB refcount table is enough for 2 PB images at 64k cluster size
0a122b
-         * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
0a122b
+    if (header.refcount_table_clusters > qcow2_max_refcount_clusters(s)) {
0a122b
         error_setg(errp, "Reference count table too large");
0a122b
         ret = -EINVAL;
0a122b
         goto fail;
0a122b
diff --git a/block/qcow2.h b/block/qcow2.h
0a122b
index 5efc96e..0173d95 100644
0a122b
--- a/block/qcow2.h
0a122b
+++ b/block/qcow2.h
0a122b
@@ -40,6 +40,10 @@
0a122b
 #define QCOW_MAX_CRYPT_CLUSTERS 32
0a122b
 #define QCOW_MAX_SNAPSHOTS 65536
0a122b
 
0a122b
+/* 8 MB refcount table is enough for 2 PB images at 64k cluster size
0a122b
+ * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
0a122b
+#define QCOW_MAX_REFTABLE_SIZE 0x800000
0a122b
+
0a122b
 /* indicate that the refcount of the referenced cluster is exactly one. */
0a122b
 #define QCOW_OFLAG_COPIED     (1LL << 63)
0a122b
 /* indicate that the cluster is compressed (they never have the copied flag) */
0a122b
@@ -405,6 +409,11 @@ static inline int64_t align_offset(int64_t offset, int n)
0a122b
     return offset;
0a122b
 }
0a122b
 
0a122b
+static inline uint64_t qcow2_max_refcount_clusters(BDRVQcowState *s)
0a122b
+{
0a122b
+    return QCOW_MAX_REFTABLE_SIZE >> s->cluster_bits;
0a122b
+}
0a122b
+
0a122b
 static inline int qcow2_get_cluster_type(uint64_t l2_entry)
0a122b
 {
0a122b
     if (l2_entry & QCOW_OFLAG_COMPRESSED) {
0a122b
-- 
0a122b
1.7.1
0a122b