yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
9ae3a8
From a2b10eec76a72aa7fe63e797181b93f69de9600e Mon Sep 17 00:00:00 2001
9ae3a8
From: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Date: Tue, 25 Mar 2014 14:23:34 +0100
9ae3a8
Subject: [PATCH 27/49] qcow2: Don't rely on free_cluster_index in alloc_refcount_block() (CVE-2014-0147)
9ae3a8
9ae3a8
RH-Author: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Message-id: <1395753835-7591-28-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: n/a
9ae3a8
O-Subject: [virt-devel] [EMBARGOED RHEL-7.0 qemu-kvm PATCH 27/48] qcow2: Don't rely on free_cluster_index in alloc_refcount_block() (CVE-2014-0147)
9ae3a8
Bugzilla: 1079339
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=1079339
9ae3a8
Upstream status: Embargoed
9ae3a8
9ae3a8
free_cluster_index is only correct if update_refcount() was called from
9ae3a8
an allocation function, and even there it's brittle because it's used to
9ae3a8
protect unfinished allocations which still have a refcount of 0 - if it
9ae3a8
moves in the wrong place, the unfinished allocation can be corrupted.
9ae3a8
9ae3a8
So not using it any more seems to be a good idea. Instead, use the
9ae3a8
first requested cluster to do the calculations. Return -EAGAIN if
9ae3a8
unfinished allocations could become invalid and let the caller restart
9ae3a8
its search for some free clusters.
9ae3a8
9ae3a8
The context of creating a snapsnot is one situation where
9ae3a8
update_refcount() is called outside of a cluster allocation. For this
9ae3a8
case, the change fixes a buffer overflow if a cluster is referenced in
9ae3a8
an L2 table that cannot be represented by an existing refcount block.
9ae3a8
(new_table[refcount_table_index] was out of bounds)
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2-refcount.c     |   74 ++++++++++++++++++++++---------------------
9ae3a8
 block/qcow2.c              |    7 ++--
9ae3a8
 tests/qemu-iotests/044.out |    2 +-
9ae3a8
 tests/qemu-iotests/080     |   11 ++++++
9ae3a8
 tests/qemu-iotests/080.out |    7 ++++
9ae3a8
 5 files changed, 61 insertions(+), 40 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
9ae3a8
index 13ea5f7..54bcbd1 100644
9ae3a8
--- a/block/qcow2-refcount.c
9ae3a8
+++ b/block/qcow2-refcount.c
9ae3a8
@@ -193,10 +193,11 @@ static int alloc_refcount_block(BlockDriverState *bs,
9ae3a8
      *   they can describe them themselves.
9ae3a8
      *
9ae3a8
      * - We need to consider that at this point we are inside update_refcounts
9ae3a8
-     *   and doing the initial refcount increase. This means that some clusters
9ae3a8
-     *   have already been allocated by the caller, but their refcount isn't
9ae3a8
-     *   accurate yet. free_cluster_index tells us where this allocation ends
9ae3a8
-     *   as long as we don't overwrite it by freeing clusters.
9ae3a8
+     *   and potentially doing an initial refcount increase. This means that
9ae3a8
+     *   some clusters have already been allocated by the caller, but their
9ae3a8
+     *   refcount isn't accurate yet. If we allocate clusters for metadata, we
9ae3a8
+     *   need to return -EAGAIN to signal the caller that it needs to restart
9ae3a8
+     *   the search for free clusters.
9ae3a8
      *
9ae3a8
      * - alloc_clusters_noref and qcow2_free_clusters may load a different
9ae3a8
      *   refcount block into the cache
9ae3a8
@@ -281,7 +282,10 @@ static int alloc_refcount_block(BlockDriverState *bs,
9ae3a8
         }
9ae3a8
 
9ae3a8
         s->refcount_table[refcount_table_index] = new_block;
9ae3a8
-        return 0;
9ae3a8
+
9ae3a8
+        /* The new refcount block may be where the caller intended to put its
9ae3a8
+         * data, so let it restart the search. */
9ae3a8
+        return -EAGAIN;
9ae3a8
     }
9ae3a8
 
9ae3a8
     ret = qcow2_cache_put(bs, s->refcount_block_cache, (void**) refcount_block);
9ae3a8
@@ -304,8 +308,7 @@ static int alloc_refcount_block(BlockDriverState *bs,
9ae3a8
 
9ae3a8
     /* Calculate the number of refcount blocks needed so far */
9ae3a8
     uint64_t refcount_block_clusters = 1 << (s->cluster_bits - REFCOUNT_SHIFT);
9ae3a8
-    uint64_t blocks_used = (s->free_cluster_index +
9ae3a8
-        refcount_block_clusters - 1) / refcount_block_clusters;
9ae3a8
+    uint64_t blocks_used = DIV_ROUND_UP(cluster_index, refcount_block_clusters);
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
@@ -338,8 +341,6 @@ static int alloc_refcount_block(BlockDriverState *bs,
9ae3a8
     uint16_t *new_blocks = g_malloc0(blocks_clusters * s->cluster_size);
9ae3a8
     uint64_t *new_table = g_malloc0(table_size * sizeof(uint64_t));
9ae3a8
 
9ae3a8
-    assert(meta_offset >= (s->free_cluster_index * s->cluster_size));
9ae3a8
-
9ae3a8
     /* Fill the new refcount table */
9ae3a8
     memcpy(new_table, s->refcount_table,
9ae3a8
         s->refcount_table_size * sizeof(uint64_t));
9ae3a8
@@ -402,18 +403,19 @@ static int alloc_refcount_block(BlockDriverState *bs,
9ae3a8
     s->refcount_table_size = table_size;
9ae3a8
     s->refcount_table_offset = table_offset;
9ae3a8
 
9ae3a8
-    /* Free old table. Remember, we must not change free_cluster_index */
9ae3a8
-    uint64_t old_free_cluster_index = s->free_cluster_index;
9ae3a8
+    /* Free old table. */
9ae3a8
     qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t),
9ae3a8
                         QCOW2_DISCARD_OTHER);
9ae3a8
-    s->free_cluster_index = old_free_cluster_index;
9ae3a8
 
9ae3a8
     ret = load_refcount_block(bs, new_block, (void**) refcount_block);
9ae3a8
     if (ret < 0) {
9ae3a8
         return ret;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    return 0;
9ae3a8
+    /* If we were trying to do the initial refcount update for some cluster
9ae3a8
+     * allocation, we might have used the same clusters to store newly
9ae3a8
+     * allocated metadata. Make the caller search some new space. */
9ae3a8
+    return -EAGAIN;
9ae3a8
 
9ae3a8
 fail_table:
9ae3a8
     g_free(new_table);
9ae3a8
@@ -659,12 +661,15 @@ int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
9ae3a8
     int ret;
9ae3a8
 
9ae3a8
     BLKDBG_EVENT(bs->file, BLKDBG_CLUSTER_ALLOC);
9ae3a8
-    offset = alloc_clusters_noref(bs, size);
9ae3a8
-    if (offset < 0) {
9ae3a8
-        return offset;
9ae3a8
-    }
9ae3a8
+    do {
9ae3a8
+        offset = alloc_clusters_noref(bs, size);
9ae3a8
+        if (offset < 0) {
9ae3a8
+            return offset;
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        ret = update_refcount(bs, offset, size, 1, QCOW2_DISCARD_NEVER);
9ae3a8
+    } while (ret == -EAGAIN);
9ae3a8
 
9ae3a8
-    ret = update_refcount(bs, offset, size, 1, QCOW2_DISCARD_NEVER);
9ae3a8
     if (ret < 0) {
9ae3a8
         return ret;
9ae3a8
     }
9ae3a8
@@ -677,7 +682,6 @@ int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
9ae3a8
 {
9ae3a8
     BDRVQcowState *s = bs->opaque;
9ae3a8
     uint64_t cluster_index;
9ae3a8
-    uint64_t old_free_cluster_index;
9ae3a8
     uint64_t i;
9ae3a8
     int refcount, ret;
9ae3a8
 
9ae3a8
@@ -686,30 +690,28 @@ int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset,
9ae3a8
         return 0;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    /* Check how many clusters there are free */
9ae3a8
-    cluster_index = offset >> s->cluster_bits;
9ae3a8
-    for(i = 0; i < nb_clusters; i++) {
9ae3a8
-        refcount = get_refcount(bs, cluster_index++);
9ae3a8
+    do {
9ae3a8
+        /* Check how many clusters there are free */
9ae3a8
+        cluster_index = offset >> s->cluster_bits;
9ae3a8
+        for(i = 0; i < nb_clusters; i++) {
9ae3a8
+            refcount = get_refcount(bs, cluster_index++);
9ae3a8
 
9ae3a8
-        if (refcount < 0) {
9ae3a8
-            return refcount;
9ae3a8
-        } else if (refcount != 0) {
9ae3a8
-            break;
9ae3a8
+            if (refcount < 0) {
9ae3a8
+                return refcount;
9ae3a8
+            } else if (refcount != 0) {
9ae3a8
+                break;
9ae3a8
+            }
9ae3a8
         }
9ae3a8
-    }
9ae3a8
 
9ae3a8
-    /* And then allocate them */
9ae3a8
-    old_free_cluster_index = s->free_cluster_index;
9ae3a8
-    s->free_cluster_index = cluster_index + i;
9ae3a8
+        /* And then allocate them */
9ae3a8
+        ret = update_refcount(bs, offset, i << s->cluster_bits, 1,
9ae3a8
+                              QCOW2_DISCARD_NEVER);
9ae3a8
+    } while (ret == -EAGAIN);
9ae3a8
 
9ae3a8
-    ret = update_refcount(bs, offset, i << s->cluster_bits, 1,
9ae3a8
-                          QCOW2_DISCARD_NEVER);
9ae3a8
     if (ret < 0) {
9ae3a8
         return ret;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    s->free_cluster_index = old_free_cluster_index;
9ae3a8
-
9ae3a8
     return i;
9ae3a8
 }
9ae3a8
 
9ae3a8
diff --git a/block/qcow2.c b/block/qcow2.c
9ae3a8
index a8ad9e1..87f2958 100644
9ae3a8
--- a/block/qcow2.c
9ae3a8
+++ b/block/qcow2.c
9ae3a8
@@ -1580,7 +1580,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
9ae3a8
      */
9ae3a8
     BlockDriverState* bs;
9ae3a8
     QCowHeader *header;
9ae3a8
-    uint8_t* refcount_table;
9ae3a8
+    uint64_t* refcount_table;
9ae3a8
     Error *local_err = NULL;
9ae3a8
     int ret;
9ae3a8
 
9ae3a8
@@ -1630,8 +1630,9 @@ static int qcow2_create2(const char *filename, int64_t total_size,
9ae3a8
         goto out;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    /* Write an empty refcount table */
9ae3a8
+    /* Write a refcount table with one refcount block */
9ae3a8
     refcount_table = g_malloc0(cluster_size);
9ae3a8
+    refcount_table[0] = cpu_to_be64(2 * cluster_size);
9ae3a8
     ret = bdrv_pwrite(bs, cluster_size, refcount_table, cluster_size);
9ae3a8
     g_free(refcount_table);
9ae3a8
 
9ae3a8
@@ -1656,7 +1657,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
9ae3a8
         goto out;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    ret = qcow2_alloc_clusters(bs, 2 * cluster_size);
9ae3a8
+    ret = qcow2_alloc_clusters(bs, 3 * cluster_size);
9ae3a8
     if (ret < 0) {
9ae3a8
         error_setg_errno(errp, -ret, "Could not allocate clusters for qcow2 "
9ae3a8
                          "header and refcount table");
9ae3a8
diff --git a/tests/qemu-iotests/044.out b/tests/qemu-iotests/044.out
9ae3a8
index 5c5aa92..4789a53 100644
9ae3a8
--- a/tests/qemu-iotests/044.out
9ae3a8
+++ b/tests/qemu-iotests/044.out
9ae3a8
@@ -1,6 +1,6 @@
9ae3a8
 No errors were found on the image.
9ae3a8
 7292415/33554432 = 21.73% allocated, 0.00% fragmented, 0.00% compressed clusters
9ae3a8
-Image end offset: 4296448000
9ae3a8
+Image end offset: 4296152064
9ae3a8
 .
9ae3a8
 ----------------------------------------------------------------------
9ae3a8
 Ran 1 tests
9ae3a8
diff --git a/tests/qemu-iotests/080 b/tests/qemu-iotests/080
9ae3a8
index f3091a9..56f8903 100755
9ae3a8
--- a/tests/qemu-iotests/080
9ae3a8
+++ b/tests/qemu-iotests/080
9ae3a8
@@ -56,6 +56,8 @@ offset_header_size=100
9ae3a8
 offset_ext_magic=$header_size
9ae3a8
 offset_ext_size=$((header_size + 4))
9ae3a8
 
9ae3a8
+offset_l2_table_0=$((0x40000))
9ae3a8
+
9ae3a8
 echo
9ae3a8
 echo "== Huge header size =="
9ae3a8
 _make_test_img 64M
9ae3a8
@@ -143,6 +145,15 @@ poke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\x00\x00\x00\x1
9ae3a8
 poke_file "$TEST_IMG" "$offset_backing_file_size" "\xff\xff\xff\xff"
9ae3a8
 { $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
 
9ae3a8
+echo
9ae3a8
+echo "== Invalid L2 entry (huge physical offset) =="
9ae3a8
+_make_test_img 64M
9ae3a8
+{ $QEMU_IO -c "write 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
+poke_file "$TEST_IMG" "$offset_l2_table_0" "\xbf\xff\xff\xff\xff\xff\x00\x00"
9ae3a8
+{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
+poke_file "$TEST_IMG" "$offset_l2_table_0" "\x80\x00\x00\xff\xff\xff\x00\x00"
9ae3a8
+{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
+
9ae3a8
 # success, all done
9ae3a8
 echo "*** done"
9ae3a8
 rm -f $seq.full
9ae3a8
diff --git a/tests/qemu-iotests/080.out b/tests/qemu-iotests/080.out
9ae3a8
index 8103211..303d6c3 100644
9ae3a8
--- a/tests/qemu-iotests/080.out
9ae3a8
+++ b/tests/qemu-iotests/080.out
9ae3a8
@@ -63,4 +63,11 @@ no file open, try 'help open'
9ae3a8
 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 
9ae3a8
 qemu-io: can't open device TEST_DIR/t.qcow2: Backing file name too long
9ae3a8
 no file open, try 'help open'
9ae3a8
+
9ae3a8
+== Invalid L2 entry (huge physical offset) ==
9ae3a8
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 
9ae3a8
+wrote 512/512 bytes at offset 0
9ae3a8
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
9ae3a8
+qemu-img: Could not create snapshot 'test': -27 (File too large)
9ae3a8
+qemu-img: Could not create snapshot 'test': -11 (Resource temporarily unavailable)
9ae3a8
 *** done
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8