958e1b
From fca7c0bc430ddd279549cc4a10a529f0b99680fc Mon Sep 17 00:00:00 2001
eb5a2f
From: Kevin Wolf <kwolf@redhat.com>
eb5a2f
Date: Mon, 2 Jun 2014 13:54:47 +0200
958e1b
Subject: [PATCH 30/31] qcow1: Validate image size (CVE-2014-0223)
eb5a2f
eb5a2f
RH-Author: Kevin Wolf <kwolf@redhat.com>
eb5a2f
Message-id: <1401717288-3918-6-git-send-email-kwolf@redhat.com>
eb5a2f
Patchwork-id: 59101
eb5a2f
O-Subject: [RHEL-7.1/7.0.z qemu-kvm PATCH 5/6] qcow1: Validate image size (CVE-2014-0223)
958e1b
Bugzilla: 1097236 1097237
eb5a2f
RH-Acked-by: Max Reitz <mreitz@redhat.com>
eb5a2f
RH-Acked-by: Fam Zheng <famz@redhat.com>
eb5a2f
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
eb5a2f
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
eb5a2f
958e1b
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1097237
958e1b
eb5a2f
A huge image size could cause s->l1_size to overflow. Make sure that
eb5a2f
images never require a L1 table larger than what fits in s->l1_size.
eb5a2f
eb5a2f
This cannot only cause unbounded allocations, but also the allocation of
eb5a2f
a too small L1 table, resulting in out-of-bounds array accesses (both
eb5a2f
reads and writes).
eb5a2f
eb5a2f
Cc: qemu-stable@nongnu.org
eb5a2f
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
eb5a2f
(cherry picked from commit 46485de0cb357b57373e1ca895adedf1f3ed46ec)
eb5a2f
---
eb5a2f
 block/qcow.c               | 16 ++++++++++++++--
eb5a2f
 tests/qemu-iotests/092     |  9 +++++++++
eb5a2f
 tests/qemu-iotests/092.out |  7 +++++++
eb5a2f
 3 files changed, 30 insertions(+), 2 deletions(-)
eb5a2f
eb5a2f
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
eb5a2f
---
eb5a2f
 block/qcow.c               |   16 ++++++++++++++--
eb5a2f
 tests/qemu-iotests/092     |    9 +++++++++
eb5a2f
 tests/qemu-iotests/092.out |    7 +++++++
eb5a2f
 3 files changed, 30 insertions(+), 2 deletions(-)
eb5a2f
eb5a2f
diff --git a/block/qcow.c b/block/qcow.c
eb5a2f
index f266701..4fdc751 100644
eb5a2f
--- a/block/qcow.c
eb5a2f
+++ b/block/qcow.c
eb5a2f
@@ -61,7 +61,7 @@ typedef struct BDRVQcowState {
eb5a2f
     int cluster_sectors;
eb5a2f
     int l2_bits;
eb5a2f
     int l2_size;
eb5a2f
-    int l1_size;
eb5a2f
+    unsigned int l1_size;
eb5a2f
     uint64_t cluster_offset_mask;
eb5a2f
     uint64_t l1_table_offset;
eb5a2f
     uint64_t *l1_table;
eb5a2f
@@ -164,7 +164,19 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
eb5a2f
 
eb5a2f
     /* read the level 1 table */
eb5a2f
     shift = s->cluster_bits + s->l2_bits;
eb5a2f
-    s->l1_size = (header.size + (1LL << shift) - 1) >> shift;
eb5a2f
+    if (header.size > UINT64_MAX - (1LL << shift)) {
eb5a2f
+        error_setg(errp, "Image too large");
eb5a2f
+        ret = -EINVAL;
eb5a2f
+        goto fail;
eb5a2f
+    } else {
eb5a2f
+        uint64_t l1_size = (header.size + (1LL << shift) - 1) >> shift;
eb5a2f
+        if (l1_size > INT_MAX / sizeof(uint64_t)) {
eb5a2f
+            error_setg(errp, "Image too large");
eb5a2f
+            ret = -EINVAL;
eb5a2f
+            goto fail;
eb5a2f
+        }
eb5a2f
+        s->l1_size = l1_size;
eb5a2f
+    }
eb5a2f
 
eb5a2f
     s->l1_table_offset = header.l1_table_offset;
eb5a2f
     s->l1_table = g_malloc(s->l1_size * sizeof(uint64_t));
eb5a2f
diff --git a/tests/qemu-iotests/092 b/tests/qemu-iotests/092
eb5a2f
index fb8bacc..ae6ca76 100755
eb5a2f
--- a/tests/qemu-iotests/092
eb5a2f
+++ b/tests/qemu-iotests/092
eb5a2f
@@ -43,6 +43,7 @@ _supported_fmt qcow
eb5a2f
 _supported_proto generic
eb5a2f
 _supported_os Linux
eb5a2f
 
eb5a2f
+offset_size=24
eb5a2f
 offset_cluster_bits=32
eb5a2f
 offset_l2_bits=33
eb5a2f
 
eb5a2f
@@ -72,6 +73,14 @@ poke_file "$TEST_IMG" "$offset_l2_bits" "\x0e"
eb5a2f
 poke_file "$TEST_IMG" "$offset_l2_bits" "\x1b"
eb5a2f
 { $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
eb5a2f
 
eb5a2f
+echo
eb5a2f
+echo "== Invalid size =="
eb5a2f
+_make_test_img 64M
eb5a2f
+poke_file "$TEST_IMG" "$offset_size" "\xee\xee\xee\xee\xee\xee\xee\xee"
eb5a2f
+{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
eb5a2f
+poke_file "$TEST_IMG" "$offset_size" "\x7f\xff\xff\xff\xff\xff\xff\xff"
eb5a2f
+{ $QEMU_IO -c "write 0 64M" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
eb5a2f
+
eb5a2f
 # success, all done
eb5a2f
 echo "*** done"
eb5a2f
 rm -f $seq.full
eb5a2f
diff --git a/tests/qemu-iotests/092.out b/tests/qemu-iotests/092.out
eb5a2f
index 73918b3..ac03302 100644
eb5a2f
--- a/tests/qemu-iotests/092.out
eb5a2f
+++ b/tests/qemu-iotests/092.out
eb5a2f
@@ -21,4 +21,11 @@ qemu-io: can't open device TEST_DIR/t.qcow: L2 table size must be between 512 an
eb5a2f
 no file open, try 'help open'
eb5a2f
 qemu-io: can't open device TEST_DIR/t.qcow: L2 table size must be between 512 and 64k
eb5a2f
 no file open, try 'help open'
eb5a2f
+
eb5a2f
+== Invalid size ==
eb5a2f
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 
eb5a2f
+qemu-io: can't open device TEST_DIR/t.qcow: Image too large
eb5a2f
+no file open, try 'help open'
eb5a2f
+qemu-io: can't open device TEST_DIR/t.qcow: Image too large
eb5a2f
+no file open, try 'help open'
eb5a2f
 *** done
eb5a2f
-- 
eb5a2f
1.7.1
eb5a2f