qemu-img: Fix convert -n -B for backing-less targets
cover qemu-img: Fix convert -n -B for backing-less targets
Hi,
When reviewing David’s series to add --target-is-zero convert, I looked for a case to show that the current implementation will crash if -n --target-is-zero is used together with -B. It then turned out that -B will always crash when combined with -n and the target image does not have a backing file set in its image header.
This series fixes that.
Max Reitz (2):
qemu-img: Fix convert -n -B for backing-less targets
iotests: Test convert -n -B to backing-less target
qemu-img.c | 2 +- tests/qemu-iotests/122 | 14 ++++++++++++++ tests/qemu-iotests/122.out | 5 +++++ 3 files changed, 20 insertions(+), 1 deletion(-)
Signature · 1 line
2.24.1
PATCH 1/2 qemu-img: Fix convert -n -B for backing-less targets
s.target_has_backing does not reflect whether the target BDS has a backing file; it only tells whether we should use a backing file during conversion (specified by -B).
As such, if you use convert -n, the target does not necessarily actually have a backing file, and then dereferencing out_bs->backing fails here.
When converting to an existing file, we should set target_backing_sectors to a negative value, because first, as the comment explains, this value is only used for optimization, so it is always fine to do that.
Second, we use this value to determine where the target must be initialized to zeroes (overlays are initialized to zero after the end of their backing file). When converting to an existing file, we cannot assume that to be true.
("qemu-img: Special post-backing convert handling")
--- qemu-img.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
qemu-img.c +1 -1
@@ -2503,7 +2503,7 @@ static int img_convert(int argc, char **argv) } } -if (s.target_has_backing) { +if (s.target_has_backing && s.target_is_new) {/* Errors are treated as "backing length unknown" (which means * s.target_backing_sectors has to be negative, which it will * be automatically). The backing file length is used only
Signature · 1 line
2.24.1
PATCH 2/2 iotests: Test convert -n -B to backing-less target
In reply to Max ReitzThis must not crash.
--- tests/qemu-iotests/122 | 14 ++++++++++++++ tests/qemu-iotests/122.out | 5 +++++ 2 files changed, 19 insertions(+)
tests/qemu-iotests/122 +14 -0
@@ -276,6 +276,20 @@ $QEMU_IMG convert -O $IMGFMT -n "$TEST_IMG" "$TEST_IMG".orig $QEMU_IMG compare "$TEST_IMG" "$TEST_IMG".orig +echo +echo '=== -n -B to an image without a backing file ===' +echo + +# Base for the output +TEST_IMG="$TEST_IMG".base _make_test_img 64M + +# Output that does have $TEST_IMG.base set as its (implicit) backing file +TEST_IMG="$TEST_IMG".orig _make_test_img 64M + +# Convert with -n, which should not confuse -B with "target BDS has a +# backing file" +$QEMU_IMG convert -O $IMGFMT -B "$TEST_IMG".base -n "$TEST_IMG" "$TEST_IMG".orig + # success, all done echo '*** done' rm -f $seq.full
tests/qemu-iotests/122.out +5 -0
@@ -228,4 +228,9 @@ Formatting 'TEST_DIR/t.IMGFMT.orig', fmt=IMGFMT size=67108864 wrote 65536/65536 bytes at offset 0 64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) Images are identical. + +=== -n -B to an image without a backing file === + +Formatting 'TEST_DIR/t.IMGFMT.base', fmt=IMGFMT size=67108864 +Formatting 'TEST_DIR/t.IMGFMT.orig', fmt=IMGFMT size=67108864 *** done
Signature · 1 line
2.24.1
Quoted 11 lines · Max Reitz
Hi,
When reviewing David’s series to add --target-is-zero convert, I looked for a case to show that the current implementation will crash if
(quoted diff) +0
-2-n --target-is-zero is used together with -B. It then turned out that -B will always crash when combined with -n and the target image does nothave a backing file set in its image header.
This series fixes that.
Quoted 9 lines
Max Reitz (2):
qemu-img: Fix convert -n -B for backing-less targets
iotests: Test convert -n -B to backing-less targetqemu-img.c | 2 +- tests/qemu-iotests/122 | 14 ++++++++++++++ tests/qemu-iotests/122.out | 5 +++++ 3 files changed, 20 insertions(+), 1 deletion(-)
Hello.
Makes sense to me.
(My brain had an awfully tumultuous 35 seconds comprehending that "is_new" was not a synonym for "-n was provided", but actually means the opposite.)
Quoted 9 lines · Max Reitz
Hi,
When reviewing David’s series to add --target-is-zero convert, I looked for a case to show that the current implementation will crash if
(quoted diff) +0
-2-n --target-is-zero is used together with -B. It then turned out that -B will always crash when combined with -n and the target image does nothave a backing file set in its image header.
This series fixes that.
Thanks for the review, applied to my block branch.
Max