yeahuh / rpms / qemu-kvm

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