From ff0376a82ddb3b0e1fff5f9c4e12964ff15a7c29 Mon Sep 17 00:00:00 2001
Message-Id: <ff0376a82ddb3b0e1fff5f9c4e12964ff15a7c29.1429902956.git.jen@redhat.com>
In-Reply-To: <67968bc615637394c3ef7dfefa360dab90f33d5d.1429902956.git.jen@redhat.com>
References: <67968bc615637394c3ef7dfefa360dab90f33d5d.1429902956.git.jen@redhat.com>
From: Max Reitz <mreitz@redhat.com>
Date: Wed, 18 Mar 2015 19:22:22 -0500
Subject: [CHANGE 39/42] block: Don't probe for unknown backing file format
To: rhvirt-patches@redhat.com,
    jen@redhat.com

RH-Author: Max Reitz <mreitz@redhat.com>
Message-id: <1426706542-30384-40-git-send-email-mreitz@redhat.com>
Patchwork-id: 64503
O-Subject: [RHEL-6.7 qemu-kvm PATCH v2 39/39] block: Don't probe for unknown backing file format
Bugzilla: 1129892
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>

From: Kevin Wolf <kwolf@redhat.com>

BZ: 1129892

If a qcow2 image specifies a backing file format that doesn't correspond
to any format driver that qemu knows, we shouldn't fall back to probing,
but simply error out.

Not looking up the backing file driver in bdrv_open_backing_file(), but
just filling in the "driver" option if it isn't there moves us closer to
the goal of having everything in QDict options and gets us the error
handling of bdrv_open(), which correctly refuses unknown drivers.

Cc: qemu-stable@nongnu.org
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Message-id: 1416935562-7760-4-git-send-email-kwolf@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
(cherry picked from commit c5f6e493bb5339d244eae5d3f21c5b6d73996739)
Signed-off-by: Jeff E. Nelson <jen@redhat.com>

Conflicts:
	block.c
	tests/qemu-iotests/114
	tests/qemu-iotests/114.out
	tests/qemu-iotests/group

There are no QDict options downstream, therefore the implementation is
completely different from upstream; also, a test case (overriding the
backing file format on the command line) has to be removed.

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block.c                    |  6 +++++
 tests/qemu-iotests/114     | 59 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/114.out | 12 ++++++++++
 tests/qemu-iotests/group   |  1 +
 4 files changed, 78 insertions(+)
 create mode 100755 tests/qemu-iotests/114
 create mode 100644 tests/qemu-iotests/114.out

Signed-off-by: Jeff E. Nelson <jen@redhat.com>
---
 block.c                    |  6 +++++
 tests/qemu-iotests/114     | 59 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/114.out | 12 ++++++++++
 tests/qemu-iotests/group   |  1 +
 4 files changed, 78 insertions(+)
 create mode 100755 tests/qemu-iotests/114
 create mode 100644 tests/qemu-iotests/114.out

diff --git a/block.c b/block.c
index 4aef5d0..99600ea 100644
--- a/block.c
+++ b/block.c
@@ -826,6 +826,12 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
 
         if (bs->backing_format[0] != '\0') {
             back_drv = bdrv_find_format(bs->backing_format);
+            if (!back_drv) {
+                error_report("Invalid backing file format '%s'",
+                             bs->backing_format);
+                bdrv_close(bs);
+                return -EINVAL;
+            }
         }
 
         /* backing files always opened read-only */
diff --git a/tests/qemu-iotests/114 b/tests/qemu-iotests/114
new file mode 100755
index 0000000..cb3f9f6
--- /dev/null
+++ b/tests/qemu-iotests/114
@@ -0,0 +1,59 @@
+#!/bin/bash
+#
+# Test invalid backing file format in qcow2 images
+#
+# Copyright (C) 2014 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+# creator
+owner=kwolf@redhat.com
+
+seq="$(basename $0)"
+echo "QA output created by $seq"
+
+here="$PWD"
+tmp=/tmp/$$
+status=1	# failure is the default!
+
+_cleanup()
+{
+	_cleanup_test_img
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+# get standard environment, filters and checks
+. ./common.rc
+. ./common.filter
+
+_supported_fmt qcow2
+_supported_proto generic
+_supported_os Linux
+
+
+TEST_IMG="$TEST_IMG.base" _make_test_img 64M
+_make_test_img -b "$TEST_IMG.base" 64M
+
+# Set an invalid backing file format
+$PYTHON qcow2.py "$TEST_IMG" add-header-ext 0xE2792ACA "foo"
+_img_info
+
+# Try opening the image. Should fail (and not probe).
+$QEMU_IO -c "open $TEST_IMG" -c "read 0 4k" 2>&1 | _filter_qemu_io | _filter_testdir
+
+# success, all done
+echo '*** done'
+rm -f $seq.full
+status=0
diff --git a/tests/qemu-iotests/114.out b/tests/qemu-iotests/114.out
new file mode 100644
index 0000000..33e699f
--- /dev/null
+++ b/tests/qemu-iotests/114.out
@@ -0,0 +1,12 @@
+QA output created by 114
+Formatting 'TEST_DIR/t.IMGFMT.base', fmt=IMGFMT size=67108864 
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 backing_file='TEST_DIR/t.IMGFMT.base' 
+image: TEST_DIR/t.IMGFMT
+file format: IMGFMT
+virtual size: 64M (67108864 bytes)
+cluster_size: 65536
+backing file: TEST_DIR/t.IMGFMT.base
+backing file format: foo
+Invalid backing file format 'foo'
+qemu-io: can't open device TEST_DIR/t.qcow2
+*** done
diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group
index 5aaed5d..3f185ee 100644
--- a/tests/qemu-iotests/group
+++ b/tests/qemu-iotests/group
@@ -102,3 +102,4 @@
 095 rw auto quick
 107 rw auto quick
 108 rw auto quick
+114 rw auto quick
-- 
2.1.0

