thebeanogamer / rpms / qemu-kvm

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