958e1b
From 4abd8ed02c277e6cd3202f61c2044a5cdff417de Mon Sep 17 00:00:00 2001
eb5a2f
From: Kevin Wolf <kwolf@redhat.com>
eb5a2f
Date: Mon, 2 Jun 2014 13:54:48 +0200
958e1b
Subject: [PATCH 31/31] qcow1: Stricter backing file length check
eb5a2f
MIME-Version: 1.0
eb5a2f
Content-Type: text/plain; charset=UTF-8
eb5a2f
Content-Transfer-Encoding: 8bit
eb5a2f
eb5a2f
RH-Author: Kevin Wolf <kwolf@redhat.com>
eb5a2f
Message-id: <1401717288-3918-7-git-send-email-kwolf@redhat.com>
eb5a2f
Patchwork-id: 59100
eb5a2f
O-Subject: [RHEL-7.1/7.0.z qemu-kvm PATCH 6/6] qcow1: Stricter backing file length check
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
Like qcow2 since commit 6d33e8e7, error out on invalid lengths instead
eb5a2f
of silently truncating them to 1023.
eb5a2f
eb5a2f
Also don't rely on bdrv_pread() catching integer overflows that make len
eb5a2f
negative, but use unsigned variables in the first place.
eb5a2f
eb5a2f
Cc: qemu-stable@nongnu.org
eb5a2f
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
eb5a2f
Reviewed-by: Benoit Canet <benoit@irqsave.net>
eb5a2f
│(cherry picked from commit d66e5cee002c471b78139228a4e7012736b375f9)
eb5a2f
---
eb5a2f
 block/qcow.c               |  7 +++++--
eb5a2f
 tests/qemu-iotests/092     | 11 +++++++++++
eb5a2f
 tests/qemu-iotests/092.out |  7 +++++++
eb5a2f
 3 files changed, 23 insertions(+), 2 deletions(-)
eb5a2f
eb5a2f
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
eb5a2f
---
eb5a2f
 block/qcow.c               |    7 +++++--
eb5a2f
 tests/qemu-iotests/092     |   11 +++++++++++
eb5a2f
 tests/qemu-iotests/092.out |    7 +++++++
eb5a2f
 3 files changed, 23 insertions(+), 2 deletions(-)
eb5a2f
eb5a2f
diff --git a/block/qcow.c b/block/qcow.c
eb5a2f
index 4fdc751..ad44f78 100644
eb5a2f
--- a/block/qcow.c
eb5a2f
+++ b/block/qcow.c
eb5a2f
@@ -97,7 +97,8 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
eb5a2f
                      Error **errp)
eb5a2f
 {
eb5a2f
     BDRVQcowState *s = bs->opaque;
eb5a2f
-    int len, i, shift, ret;
eb5a2f
+    unsigned int len, i, shift;
eb5a2f
+    int ret;
eb5a2f
     QCowHeader header;
eb5a2f
 
eb5a2f
     ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
eb5a2f
@@ -200,7 +201,9 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
eb5a2f
     if (header.backing_file_offset != 0) {
eb5a2f
         len = header.backing_file_size;
eb5a2f
         if (len > 1023) {
eb5a2f
-            len = 1023;
eb5a2f
+            error_setg(errp, "Backing file name too long");
eb5a2f
+            ret = -EINVAL;
eb5a2f
+            goto fail;
eb5a2f
         }
eb5a2f
         ret = bdrv_pread(bs->file, header.backing_file_offset,
eb5a2f
                    bs->backing_file, len);
eb5a2f
diff --git a/tests/qemu-iotests/092 b/tests/qemu-iotests/092
eb5a2f
index ae6ca76..a8c0c9c 100755
eb5a2f
--- a/tests/qemu-iotests/092
eb5a2f
+++ b/tests/qemu-iotests/092
eb5a2f
@@ -43,6 +43,8 @@ _supported_fmt qcow
eb5a2f
 _supported_proto generic
eb5a2f
 _supported_os Linux
eb5a2f
 
eb5a2f
+offset_backing_file_offset=8
eb5a2f
+offset_backing_file_size=16
eb5a2f
 offset_size=24
eb5a2f
 offset_cluster_bits=32
eb5a2f
 offset_l2_bits=33
eb5a2f
@@ -81,6 +83,15 @@ poke_file "$TEST_IMG" "$offset_size" "\xee\xee\xee\xee\xee\xee\xee\xee"
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
+echo
eb5a2f
+echo "== Invalid backing file length =="
eb5a2f
+_make_test_img 64M
eb5a2f
+poke_file "$TEST_IMG" "$offset_backing_file_offset" "\x00\x00\x00\xff"
eb5a2f
+poke_file "$TEST_IMG" "$offset_backing_file_size" "\xff\xff\xff\xff"
eb5a2f
+{ $QEMU_IO -c "read 0 512" $TEST_IMG; } 2>&1 | _filter_qemu_io | _filter_testdir
eb5a2f
+poke_file "$TEST_IMG" "$offset_backing_file_size" "\x7f\xff\xff\xff"
eb5a2f
+{ $QEMU_IO -c "read 0 512" $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 ac03302..496d8f0 100644
eb5a2f
--- a/tests/qemu-iotests/092.out
eb5a2f
+++ b/tests/qemu-iotests/092.out
eb5a2f
@@ -28,4 +28,11 @@ 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
+
eb5a2f
+== Invalid backing file length ==
eb5a2f
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 
eb5a2f
+qemu-io: can't open device TEST_DIR/t.qcow: Backing file name too long
eb5a2f
+no file open, try 'help open'
eb5a2f
+qemu-io: can't open device TEST_DIR/t.qcow: Backing file name too long
eb5a2f
+no file open, try 'help open'
eb5a2f
 *** done
eb5a2f
-- 
eb5a2f
1.7.1
eb5a2f