qcow2: Don't crash qemu-img info with missing crypto header
PATCH 1/1 qcow2: Don't crash qemu-img info with missing crypto header
qcow2_refresh_limits() assumes that s->crypto is non-NULL whenever bs->encrypted is true. This is actually not the case: qcow2_do_open() allows to open an image with a missing crypto header for BDRV_O_NO_IO, and then bs->encrypted is true, but s->crypto is still NULL.
It doesn't make sense to open an invalid image, so remove the exception for BDRV_O_NO_IO. This catches the problem early and any code that makes the same assumption is safe now.
At the same time, in the name of defensive programming, we shouldn't make the assumption in the first place. Let qcow2_refresh_limits() check s->crypto rather than bs->encrypted. If s->crypto is NULL, it also can't make any requirement on request alignment.
Finally, start a qcow2-encryption test case that only serves as a regression test for this crash for now.
--- block/qcow2.c | 4 +- tests/qemu-iotests/tests/qcow2-encryption | 75 +++++++++++++++++++ tests/qemu-iotests/tests/qcow2-encryption.out | 32 ++++++++ 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100755 tests/qemu-iotests/tests/qcow2-encryption create mode 100644 tests/qemu-iotests/tests/qcow2-encryption.out
block/qcow2.c +2 -2
@@ -1721,7 +1721,7 @@ qcow2_do_open(BlockDriverState *bs, QDict *options, int flags, ret = -EINVAL ; goto fail; } - }else if (!(flags &BDRV_O_NO_IO )) { + }else {error_setg (errp,"Missing CRYPTO header for crypt method %d" , s->crypt_method_header); ret = -EINVAL ;
@@ -1976,7 +1976,7 @@ static void qcow2_refresh_limits(BlockDriverState *bs, Error **errp) {BDRVQcow2State *s = bs->opaque; -if (bs->encrypted) { +if (s->crypto) {/* Encryption works on a sector granularity */ bs->bl.request_alignment =qcrypto_block_get_sector_size (s->crypto); }
tests/qemu-iotests/tests/qcow2-encryption new file +75 -0
75 lines not on this page · raw message
tests/qemu-iotests/tests/qcow2-encryption.out new file +32 -0
@@ -0,0 +1,32 @@ +QA output created by qcow2-encryption + +=== Create an encrypted image === + +Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 +Header extension: +magic 0x537be77 (Crypto header) +length 16 +data <binary> + +Header extension: +magic 0x6803f857 (Feature table) +length 384 +data <binary> + +image: TEST_DIR/t.IMGFMT +file format: IMGFMT +virtual size: 64 MiB (67108864 bytes) +encrypted: yes +cluster_size: 65536 +No errors were found on the image. + +=== Remove the header extension === + +Header extension: +magic 0x6803f857 (Feature table) +length 384 +data <binary> + +qemu-img: Could not open 'TEST_DIR/t.IMGFMT': Missing CRYPTO header for crypt method 2 +qemu-img: Could not open 'file.filename=TEST_DIR/t.qcow2,encrypt.key-secret=sec0': Missing CRYPTO header for crypt method 2 +*** done
Signature · 1 line
2.48.1
Quoted 20 lines · Kevin Wolf
qcow2_refresh_limits()assumes thats->cryptois non-NULL wheneverbs->encryptedis true. This is actually not the case:qcow2_do_open()allows to open an image with a missing crypto header forBDRV_O_NO_IO, and thenbs->encryptedis true, buts->cryptois still NULL.It doesn't make sense to open an invalid image, so remove the exception for
BDRV_O_NO_IO. This catches the problem early and any code that makes the same assumption is safe now.At the same time, in the name of defensive programming, we shouldn't make the assumption in the first place. Let
qcow2_refresh_limits()checks->cryptorather thanbs->encrypted. Ifs->cryptois NULL, it also can't make any requirement on request alignment.Finally, start a qcow2-encryption test case that only serves as a regression test for this crash for now.
Reported-by Leonid Reviakin <[email protected]>Reported-by Denis Rastyogin <[email protected]>Signed-off-by Kevin Wolf <[email protected]>
Quoted 7 lines
--- block/qcow2.c | 4 +- tests/qemu-iotests/tests/qcow2-encryption | 75 +++++++++++++++++++ tests/qemu-iotests/tests/qcow2-encryption.out | 32 ++++++++ 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100755 tests/qemu-iotests/tests/qcow2-encryption create mode 100644 tests/qemu-iotests/tests/qcow2-encryption.out
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|