Skip to contentSkip to conversation

block: Raise an error when backing file parameter is an empty string

PATCH Superseded Connor Kuehl 2 participants 3 messages Aug 11, 2020 – Aug 13, 2020

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 block: Raise an error when backing file parameter is an empty string

Providing an empty string for the backing file parameter like so:

qemu-img create -f qcow2 -b '' /tmp/foo

allows the flow of control to reach and subsequently fail an assert statement because passing an empty string to

bdrv_get_full_backing_filename_from_filename()

simply results in NULL being returned without an error being raised.

To fix this, let's check for an empty string when getting the value from the opts list.

---
v2:
- Removed 4 spaces to resolve pylint warning
- Updated format to be 'iotests.imgfmt' instead
of hardcoding 'qcow2'
- Use temporary file instead of '/tmp/foo'
- Give a size parameter to qemu-img
- Run test for qcow2, qcow, qed and *not* raw

 block.c                    |  4 ++++
 tests/qemu-iotests/298     | 49 ++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/298.out |  5 ++++
 tests/qemu-iotests/group   |  1 +
 4 files changed, 59 insertions(+)
 create mode 100755 tests/qemu-iotests/298
 create mode 100644 tests/qemu-iotests/298.out
block.c +4 -0
index d9ac0e07eb..1f72275b87 100644
@@ -6117,6 +6117,10 @@ void bdrv_img_create(const char *filename, const char *fmt,
                              "same filename as the backing file");
             goto out;
         }
+        if (backing_file[0] == '\0') {
+            error_setg(errp, "Expected backing file name, got empty string");
+            goto out;
+        }
     }
 
     backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
tests/qemu-iotests/298 new file +49 -0

49 lines not on this page · raw message

tests/qemu-iotests/298.out new file +5 -0
new file mode 100644
index 0000000000..ae1213e6f8
@@ -0,0 +1,5 @@
+.
+----------------------------------------------------------------------
+Ran 1 tests
+
+OK
tests/qemu-iotests/group +1 -0
index 025ed5238d..6f80c884a1 100644
@@ -306,6 +306,7 @@
 295 rw
 296 rw
 297 meta
+298 img auto quick
 299 auto quick
 301 backing quick
 302 quick
Signature · 1 line
2.25.4
#1 lore raw

Am 11.08.2020 um 23:23 hat Connor Kuehl geschrieben:

Quoted 9 lines

Providing an empty string for the backing file parameter like so:

qemu-img create -f qcow2 -b '' /tmp/foo

allows the flow of control to reach and subsequently fail an assert statement because passing an empty string to

bdrv_get_full_backing_filename_from_filename()

Quoted 8 lines

simply results in NULL being returned without an error being raised.

To fix this, let's check for an empty string when getting the value from the opts list.

In 'qemu-img rebase' we accept an empty string to mean "no backing file". I guess it would be a bit more consistent to do the same here, though of course there is no real reason to use -b '' in 'create'. So I don't really mind an error either, I just wanted to mention it.

Quoted 11 lines

v2:
- Removed 4 spaces to resolve pylint warning
- Updated format to be 'iotests.imgfmt' instead
of hardcoding 'qcow2'
- Use temporary file instead of '/tmp/foo'
- Give a size parameter to qemu-img
- Run test for qcow2, qcow, qed and *not* raw

 block.c                    |  4 ++++
 tests/qemu-iotests/298     | 49 ++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/298.out |  5 ++++

We have multiple series using 298. You were first, though, so in case of doubt, the others will have to rebase.

Quoted 65 lines
 tests/qemu-iotests/group   |  1 +
 4 files changed, 59 insertions(+)
 create mode 100755 tests/qemu-iotests/298
 create mode 100644 tests/qemu-iotests/298.out
block.c +4 -0
index d9ac0e07eb..1f72275b87 100644
@@ -6117,6 +6117,10 @@ void bdrv_img_create(const char *filename, const char *fmt,
                              "same filename as the backing file");
             goto out;
         }
+        if (backing_file[0] == '\0') {
+            error_setg(errp, "Expected backing file name, got empty string");
+            goto out;
+        }
     }
 
     backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT);
tests/qemu-iotests/298 new file +39 -0
new file mode 100755
index 0000000000..879dae2d8e
@@ -0,0 +1,49 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2020 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/>.
+
+
+
+# Regression test for avoiding an assertion when the 'backing file'
+# parameter (-b) is set to an empty string for qemu-img create
+#
+#   qemu-img create -f qcow2 -b '' /tmp/foo
+#
+# This ensures the invalid parameter is handled with a user-
+# friendly message instead of a failed assertion.
+
+import iotests
+
+class TestEmptyBackingFilename(iotests.QMPTestCase):
+
+
+    def test_empty_backing_file_name(self):
+        with iotests.FilePath('test.img') as img_path:
+            actual = iotests.qemu_img_pipe(
+                'create',
+                '-f', iotests.imgfmt,
+                '-b', '',
+                img_path,

tests/qemu-iotests/298 line 49

Quoted 10 lines
tests/qemu-iotests/298 new file +10 -0
+                '1M'
+            )
+            expected = f'qemu-img: {img_path}: Expected backing file name,' \
+                       ' got empty string'
+
+            self.assertEqual(actual.strip(), expected.strip())
+
+
+if __name__ == '__main__':
+    iotests.main(supported_fmts=['qed', 'qcow', 'qcow2'])

This looks like a test case that would be better served by not using QMPTestCase, but just printing the qemu-img output and having the message compared against the reference output.

In fact, there is already 049 for testing some qemu-img create options and we could just add a line there (or multiple lines to cover other backing file related error cases, too).

Putting it there would both simplify the test code and keep 298 free for the other series.

Quoted 16 lines
tests/qemu-iotests/298.out new file +5 -0
new file mode 100644
index 0000000000..ae1213e6f8
@@ -0,0 +1,5 @@
+.
+----------------------------------------------------------------------
+Ran 1 tests
+
+OK
tests/qemu-iotests/group +0 -0
index 025ed5238d..6f80c884a1 100644

tests/qemu-iotests/group line 312

Quoted 7 lines
tests/qemu-iotests/group +1 -0
@@ -306,6 +306,7 @@
 295 rw
 296 rw
 297 meta
+298 img auto quick
 299 auto quick
 301 backing quick
 302 quick

None of the above is really a reason to reject the patch. I guess this is more of a "are you sure? (y/n)" before I apply it. :-)

Kevin

#2 lore raw
Quoted 8 lines · Kevin Wolf

This looks like a test case that would be better served by not using QMPTestCase, but just printing the qemu-img output and having the message compared against the reference output.

In fact, there is already 049 for testing some qemu-img create options and we could just add a line there (or multiple lines to cover other backing file related error cases, too).

Quoted 5 lines

Putting it there would both simplify the test code and keep 298 free for the other series.

None of the above is really a reason to reject the patch. I guess this is more of a "are you sure? (y/n)" before I apply it. :-)

Hi Kevin! Thanks for the review :-)

I think it'd be best for my own edification to address your comments here instead of applying this now. I'll send a v3.

Connor

Quoted 3 lines

Kevin