9ae3a8
From ffbffdc438191db5bfa6ae6da52f0578db0fe7ac Mon Sep 17 00:00:00 2001
9ae3a8
From: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Date: Tue, 25 Mar 2014 14:23:28 +0100
9ae3a8
Subject: [PATCH 21/49] qcow2: Check backing_file_offset (CVE-2014-0144)
9ae3a8
9ae3a8
RH-Author: Kevin Wolf <kwolf@redhat.com>
9ae3a8
Message-id: <1395753835-7591-22-git-send-email-kwolf@redhat.com>
9ae3a8
Patchwork-id: n/a
9ae3a8
O-Subject: [virt-devel] [EMBARGOED RHEL-7.0 qemu-kvm PATCH 21/48] qcow2: Check backing_file_offset (CVE-2014-0144)
9ae3a8
Bugzilla: 1079455
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=1079455
9ae3a8
Upstream status: Embargoed
9ae3a8
9ae3a8
Header, header extension and the backing file name must all be stored in
9ae3a8
the first cluster. Setting the backing file to a much higher value
9ae3a8
allowed header extensions to become much bigger than we want them to be
9ae3a8
(unbounded allocation).
9ae3a8
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2.c              |    6 ++++++
9ae3a8
 tests/qemu-iotests/080     |   12 ++++++++++++
9ae3a8
 tests/qemu-iotests/080.out |    7 +++++++
9ae3a8
 3 files changed, 25 insertions(+), 0 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2.c b/block/qcow2.c
9ae3a8
index ea51f8e..5568cf9 100644
9ae3a8
--- a/block/qcow2.c
9ae3a8
+++ b/block/qcow2.c
9ae3a8
@@ -511,6 +511,12 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
         }
9ae3a8
     }
9ae3a8
 
9ae3a8
+    if (header.backing_file_offset > s->cluster_size) {
9ae3a8
+        error_setg(errp, "Invalid backing file offset");
9ae3a8
+        ret = -EINVAL;
9ae3a8
+        goto fail;
9ae3a8
+    }
9ae3a8
+
9ae3a8
     if (header.backing_file_offset) {
9ae3a8
         ext_end = header.backing_file_offset;
9ae3a8
     } else {
9ae3a8
diff --git a/tests/qemu-iotests/080 b/tests/qemu-iotests/080
9ae3a8
index 6512701..6d588dd 100755
9ae3a8
--- a/tests/qemu-iotests/080
9ae3a8
+++ b/tests/qemu-iotests/080
9ae3a8
@@ -43,6 +43,8 @@ _supported_proto generic
9ae3a8
 _supported_os Linux
9ae3a8
 
9ae3a8
 header_size=104
9ae3a8
+
9ae3a8
+offset_backing_file_offset=8
9ae3a8
 offset_header_size=100
9ae3a8
 offset_ext_magic=$header_size
9ae3a8
 offset_ext_size=$((header_size + 4))
9ae3a8
@@ -55,6 +57,16 @@ poke_file "$TEST_IMG" "$offset_header_size" "\xff\xff\xff\xff"
9ae3a8
 poke_file "$TEST_IMG" "$offset_header_size" "\x7f\xff\xff\xff"
9ae3a8
 { $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
 
9ae3a8
+echo
9ae3a8
+echo "== Huge unknown header extension =="
9ae3a8
+_make_test_img 64M
9ae3a8
+poke_file "$TEST_IMG" "$offset_backing_file_offset" "\xff\xff\xff\xff\xff\xff\xff\xff"
9ae3a8
+poke_file "$TEST_IMG" "$offset_ext_magic" "\x12\x34\x56\x78"
9ae3a8
+poke_file "$TEST_IMG" "$offset_ext_size" "\x7f\xff\xff\xff"
9ae3a8
+{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
9ae3a8
+poke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\x00\x00\x00\x00\x00"
9ae3a8
+{ $QEMU_IO -c "read 0 512" $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 41a166a..48c40aa 100644
9ae3a8
--- a/tests/qemu-iotests/080.out
9ae3a8
+++ b/tests/qemu-iotests/080.out
9ae3a8
@@ -6,4 +6,11 @@ qemu-io: can't open device TEST_DIR/t.qcow2: qcow2 header exceeds cluster size
9ae3a8
 no file open, try 'help open'
9ae3a8
 qemu-io: can't open device TEST_DIR/t.qcow2: qcow2 header exceeds cluster size
9ae3a8
 no file open, try 'help open'
9ae3a8
+
9ae3a8
+== Huge unknown header extension ==
9ae3a8
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 
9ae3a8
+qemu-io: can't open device TEST_DIR/t.qcow2: Invalid backing file offset
9ae3a8
+no file open, try 'help open'
9ae3a8
+qemu-io: can't open device TEST_DIR/t.qcow2: Header extension too large
9ae3a8
+no file open, try 'help open'
9ae3a8
 *** done
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8