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