0a122b
From 34d9d86de2d566b4c9ecd1ed0294edc0bb95bec6 Mon Sep 17 00:00:00 2001
0a122b
From: Kevin Wolf <kwolf@redhat.com>
0a122b
Date: Tue, 25 Mar 2014 14:23:27 +0100
0a122b
Subject: [PATCH 20/49] qcow2: Check header_length (CVE-2014-0144)
0a122b
0a122b
RH-Author: Kevin Wolf <kwolf@redhat.com>
0a122b
Message-id: <1395753835-7591-21-git-send-email-kwolf@redhat.com>
0a122b
Patchwork-id: n/a
0a122b
O-Subject: [virt-devel] [EMBARGOED RHEL-7.0 qemu-kvm PATCH 20/48] qcow2: Check header_length (CVE-2014-0144)
0a122b
Bugzilla: 1079455
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=1079455
0a122b
Upstream status: Embargoed
0a122b
0a122b
This fixes an unbounded allocation for s->unknown_header_fields.
0a122b
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
0a122b
Conflicts:
0a122b
tests/qemu-iotests/group
0a122b
0a122b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
0a122b
---
0a122b
 block/qcow2.c              |   34 ++++++++++++++++++------
0a122b
 tests/qemu-iotests/080     |   61 ++++++++++++++++++++++++++++++++++++++++++++
0a122b
 tests/qemu-iotests/080.out |    9 ++++++
0a122b
 tests/qemu-iotests/group   |    1 +
0a122b
 4 files changed, 96 insertions(+), 9 deletions(-)
0a122b
 create mode 100755 tests/qemu-iotests/080
0a122b
 create mode 100644 tests/qemu-iotests/080.out
0a122b
0a122b
diff --git a/block/qcow2.c b/block/qcow2.c
0a122b
index 18e136e..ea51f8e 100644
0a122b
--- a/block/qcow2.c
0a122b
+++ b/block/qcow2.c
0a122b
@@ -460,6 +460,18 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
0a122b
 
0a122b
     s->qcow_version = header.version;
0a122b
 
0a122b
+    /* Initialise cluster size */
0a122b
+    if (header.cluster_bits < MIN_CLUSTER_BITS ||
0a122b
+        header.cluster_bits > MAX_CLUSTER_BITS) {
0a122b
+        error_setg(errp, "Unsupported cluster size: 2^%i", header.cluster_bits);
0a122b
+        ret = -EINVAL;
0a122b
+        goto fail;
0a122b
+    }
0a122b
+
0a122b
+    s->cluster_bits = header.cluster_bits;
0a122b
+    s->cluster_size = 1 << s->cluster_bits;
0a122b
+    s->cluster_sectors = 1 << (s->cluster_bits - 9);
0a122b
+
0a122b
     /* Initialise version 3 header fields */
0a122b
     if (header.version == 2) {
0a122b
         header.incompatible_features    = 0;
0a122b
@@ -473,6 +485,18 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
0a122b
         be64_to_cpus(&header.autoclear_features);
0a122b
         be32_to_cpus(&header.refcount_order);
0a122b
         be32_to_cpus(&header.header_length);
0a122b
+
0a122b
+        if (header.header_length < 104) {
0a122b
+            error_setg(errp, "qcow2 header too short");
0a122b
+            ret = -EINVAL;
0a122b
+            goto fail;
0a122b
+        }
0a122b
+    }
0a122b
+
0a122b
+    if (header.header_length > s->cluster_size) {
0a122b
+        error_setg(errp, "qcow2 header exceeds cluster size");
0a122b
+        ret = -EINVAL;
0a122b
+        goto fail;
0a122b
     }
0a122b
 
0a122b
     if (header.header_length > sizeof(header)) {
0a122b
@@ -529,12 +553,6 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
0a122b
     }
0a122b
     s->refcount_order = header.refcount_order;
0a122b
 
0a122b
-    if (header.cluster_bits < MIN_CLUSTER_BITS ||
0a122b
-        header.cluster_bits > MAX_CLUSTER_BITS) {
0a122b
-        error_setg(errp, "Unsupported cluster size: 2^%i", header.cluster_bits);
0a122b
-        ret = -EINVAL;
0a122b
-        goto fail;
0a122b
-    }
0a122b
     if (header.crypt_method > QCOW_CRYPT_AES) {
0a122b
         error_setg(errp, "Unsupported encryption method: %i",
0a122b
                    header.crypt_method);
0a122b
@@ -545,9 +563,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
0a122b
     if (s->crypt_method_header) {
0a122b
         bs->encrypted = 1;
0a122b
     }
0a122b
-    s->cluster_bits = header.cluster_bits;
0a122b
-    s->cluster_size = 1 << s->cluster_bits;
0a122b
-    s->cluster_sectors = 1 << (s->cluster_bits - 9);
0a122b
+
0a122b
     s->l2_bits = s->cluster_bits - 3; /* L2 is always one cluster */
0a122b
     s->l2_size = 1 << s->l2_bits;
0a122b
     bs->total_sectors = header.size / 512;
0a122b
diff --git a/tests/qemu-iotests/080 b/tests/qemu-iotests/080
0a122b
new file mode 100755
0a122b
index 0000000..6512701
0a122b
--- /dev/null
0a122b
+++ b/tests/qemu-iotests/080
0a122b
@@ -0,0 +1,61 @@
0a122b
+#!/bin/bash
0a122b
+#
0a122b
+# qcow2 format input validation tests
0a122b
+#
0a122b
+# Copyright (C) 2013 Red Hat, Inc.
0a122b
+#
0a122b
+# This program is free software; you can redistribute it and/or modify
0a122b
+# it under the terms of the GNU General Public License as published by
0a122b
+# the Free Software Foundation; either version 2 of the License, or
0a122b
+# (at your option) any later version.
0a122b
+#
0a122b
+# This program is distributed in the hope that it will be useful,
0a122b
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
0a122b
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0a122b
+# GNU General Public License for more details.
0a122b
+#
0a122b
+# You should have received a copy of the GNU General Public License
0a122b
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
0a122b
+#
0a122b
+
0a122b
+# creator
0a122b
+owner=kwolf@redhat.com
0a122b
+
0a122b
+seq=`basename $0`
0a122b
+echo "QA output created by $seq"
0a122b
+
0a122b
+here=`pwd`
0a122b
+tmp=/tmp/$$
0a122b
+status=1	# failure is the default!
0a122b
+
0a122b
+_cleanup()
0a122b
+{
0a122b
+	_cleanup_test_img
0a122b
+}
0a122b
+trap "_cleanup; exit \$status" 0 1 2 3 15
0a122b
+
0a122b
+# get standard environment, filters and checks
0a122b
+. ./common.rc
0a122b
+. ./common.filter
0a122b
+
0a122b
+_supported_fmt qcow2
0a122b
+_supported_proto generic
0a122b
+_supported_os Linux
0a122b
+
0a122b
+header_size=104
0a122b
+offset_header_size=100
0a122b
+offset_ext_magic=$header_size
0a122b
+offset_ext_size=$((header_size + 4))
0a122b
+
0a122b
+echo
0a122b
+echo "== Huge header size =="
0a122b
+_make_test_img 64M
0a122b
+poke_file "$TEST_IMG" "$offset_header_size" "\xff\xff\xff\xff"
0a122b
+{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
0a122b
+poke_file "$TEST_IMG" "$offset_header_size" "\x7f\xff\xff\xff"
0a122b
+{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
0a122b
+
0a122b
+# success, all done
0a122b
+echo "*** done"
0a122b
+rm -f $seq.full
0a122b
+status=0
0a122b
diff --git a/tests/qemu-iotests/080.out b/tests/qemu-iotests/080.out
0a122b
new file mode 100644
0a122b
index 0000000..41a166a
0a122b
--- /dev/null
0a122b
+++ b/tests/qemu-iotests/080.out
0a122b
@@ -0,0 +1,9 @@
0a122b
+QA output created by 080
0a122b
+
0a122b
+== Huge header size ==
0a122b
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 
0a122b
+qemu-io: can't open device TEST_DIR/t.qcow2: qcow2 header exceeds cluster size
0a122b
+no file open, try 'help open'
0a122b
+qemu-io: can't open device TEST_DIR/t.qcow2: qcow2 header exceeds cluster size
0a122b
+no file open, try 'help open'
0a122b
+*** done
0a122b
diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group
0a122b
index 7d0d07e..fc34194 100644
0a122b
--- a/tests/qemu-iotests/group
0a122b
+++ b/tests/qemu-iotests/group
0a122b
@@ -75,6 +75,7 @@
0a122b
 077 rw auto
0a122b
 078 rw auto
0a122b
 079 rw auto
0a122b
+080 rw auto
0a122b
 082 rw auto quick
0a122b
 086 rw auto quick
0a122b
 088 rw auto
0a122b
-- 
0a122b
1.7.1
0a122b