0a122b
From b2472117867fc233fcb634b7370051bcb7fc892c Mon Sep 17 00:00:00 2001
0a122b
From: Kevin Wolf <kwolf@redhat.com>
0a122b
Date: Tue, 25 Mar 2014 14:23:52 +0100
0a122b
Subject: [PATCH 45/49] qcow2: Check maximum L1 size in qcow2_snapshot_load_tmp() (CVE-2014-0143)
0a122b
0a122b
RH-Author: Kevin Wolf <kwolf@redhat.com>
0a122b
Message-id: <1395753835-7591-46-git-send-email-kwolf@redhat.com>
0a122b
Patchwork-id: n/a
0a122b
O-Subject: [virt-devel] [EMBARGOED RHEL-7.0 qemu-kvm PATCH 45/48] qcow2: Check maximum L1 size in qcow2_snapshot_load_tmp() (CVE-2014-0143)
0a122b
Bugzilla: 1079320
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=1079320
0a122b
Upstream status: Embargoed
0a122b
0a122b
This avoids an unbounded allocation.
0a122b
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
0a122b
RHEL 7.0 doesn't have commit 7b4c4781, so use error_report() instead of
0a122b
error_setg().
0a122b
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
---
0a122b
 block/qcow2-snapshot.c     |    4 ++++
0a122b
 block/qcow2.c              |    4 +---
0a122b
 block/qcow2.h              |    4 ++++
0a122b
 tests/qemu-iotests/080     |   15 ++++++++++++++-
0a122b
 tests/qemu-iotests/080.out |    7 +++++++
0a122b
 5 files changed, 30 insertions(+), 4 deletions(-)
0a122b
0a122b
diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c
0a122b
index b29f7bd..626a8d4 100644
0a122b
--- a/block/qcow2-snapshot.c
0a122b
+++ b/block/qcow2-snapshot.c
0a122b
@@ -635,6 +635,10 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name)
0a122b
     sn = &s->snapshots[snapshot_index];
0a122b
 
0a122b
     /* Allocate and read in the snapshot's L1 table */
0a122b
+    if (sn->l1_size > QCOW_MAX_L1_SIZE) {
0a122b
+        error_report("Snapshot L1 table too large");
0a122b
+        return -EFBIG;
0a122b
+    }
0a122b
     new_l1_bytes = sn->l1_size * sizeof(uint64_t);
0a122b
     new_l1_table = g_malloc0(align_offset(new_l1_bytes, 512));
0a122b
 
0a122b
diff --git a/block/qcow2.c b/block/qcow2.c
0a122b
index 33b46b8..3f8febc 100644
0a122b
--- a/block/qcow2.c
0a122b
+++ b/block/qcow2.c
0a122b
@@ -637,9 +637,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
0a122b
     }
0a122b
 
0a122b
     /* read the level 1 table */
0a122b
-    if (header.l1_size > 0x2000000) {
0a122b
-        /* 32 MB L1 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.l1_size > QCOW_MAX_L1_SIZE) {
0a122b
         error_setg(errp, "Active L1 table too large");
0a122b
         ret = -EFBIG;
0a122b
         goto fail;
0a122b
diff --git a/block/qcow2.h b/block/qcow2.h
0a122b
index 9f9f5ae..87e256a 100644
0a122b
--- a/block/qcow2.h
0a122b
+++ b/block/qcow2.h
0a122b
@@ -44,6 +44,10 @@
0a122b
  * (128 GB for 512 byte clusters, 2 EB for 2 MB clusters) */
0a122b
 #define QCOW_MAX_REFTABLE_SIZE 0x800000
0a122b
 
0a122b
+/* 32 MB L1 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_L1_SIZE 0x2000000
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
diff --git a/tests/qemu-iotests/080 b/tests/qemu-iotests/080
0a122b
index 59e7a44..6b3a3e7 100755
0a122b
--- a/tests/qemu-iotests/080
0a122b
+++ b/tests/qemu-iotests/080
0a122b
@@ -30,7 +30,8 @@ status=1	# failure is the default!
0a122b
 
0a122b
 _cleanup()
0a122b
 {
0a122b
-	_cleanup_test_img
0a122b
+    rm -f $TEST_IMG.snap
0a122b
+    _cleanup_test_img
0a122b
 }
0a122b
 trap "_cleanup; exit \$status" 0 1 2 3 15
0a122b
 
0a122b
@@ -58,6 +59,10 @@ offset_ext_size=$((header_size + 4))
0a122b
 
0a122b
 offset_l2_table_0=$((0x40000))
0a122b
 
0a122b
+offset_snap1=$((0x70000))
0a122b
+offset_snap1_l1_offset=$((offset_snap1 + 0))
0a122b
+offset_snap1_l1_size=$((offset_snap1 + 8))
0a122b
+
0a122b
 echo
0a122b
 echo "== Huge header size =="
0a122b
 _make_test_img 64M
0a122b
@@ -161,6 +166,14 @@ poke_file "$TEST_IMG" "$offset_l2_table_0" "\xbf\xff\xff\xff\xff\xff\x00\x00"
0a122b
 poke_file "$TEST_IMG" "$offset_l2_table_0" "\x80\x00\x00\xff\xff\xff\x00\x00"
0a122b
 { $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
0a122b
 
0a122b
+echo
0a122b
+echo "== Invalid snapshot L1 table =="
0a122b
+_make_test_img 64M
0a122b
+{ $QEMU_IO -c "write 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
0a122b
+{ $QEMU_IMG snapshot -c test $TEST_IMG; } 2>&1 | _filter_testdir
0a122b
+poke_file "$TEST_IMG" "$offset_snap1_l1_size" "\x10\x00\x00\x00"
0a122b
+{ $QEMU_IMG convert -s test $TEST_IMG $TEST_IMG.snap; } 2>&1 | _filter_testdir
0a122b
+
0a122b
 # success, all done
0a122b
 echo "*** done"
0a122b
 rm -f $seq.full
0a122b
diff --git a/tests/qemu-iotests/080.out b/tests/qemu-iotests/080.out
0a122b
index 4d84fbf..1fa0672 100644
0a122b
--- a/tests/qemu-iotests/080.out
0a122b
+++ b/tests/qemu-iotests/080.out
0a122b
@@ -74,4 +74,11 @@ wrote 512/512 bytes at offset 0
0a122b
 512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
0a122b
 qemu-img: Could not create snapshot 'test': -27 (File too large)
0a122b
 qemu-img: Could not create snapshot 'test': -11 (Resource temporarily unavailable)
0a122b
+
0a122b
+== Invalid snapshot L1 table ==
0a122b
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 
0a122b
+wrote 512/512 bytes at offset 0
0a122b
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
0a122b
+qemu-img: Snapshot L1 table too large
0a122b
+qemu-img: Failed to load snapshot
0a122b
 *** done
0a122b
-- 
0a122b
1.7.1
0a122b