Skip to contentSkip to conversation

qcow2: improve error handling when luks creation fails

PATCH In review Daniel P. Berrangé 3 participants 10 messages Feb 19, 2019 – Feb 25, 2019 Block layer

lore
Share & tools
lore
#0 lore raw

cover qcow2: improve error handling when luks creation fails

If qemu is built without crypto support luks creation will fail. The 188 iotest however still carried on using the partially created file. This showed a few flaws in error handling, one during creation and one during opening of the image.

Daniel P. Berrangé (2):
qcow2: fail if encryption opts are provided to non-encrypted image
qcow2: mark image as corrupt if failing during create

 block/qcow2.c | 9 +++++++++
 1 file changed, 9 insertions(+)
Signature · 1 line
2.20.1
#5 lore raw

PATCH 2/2 qcow2: mark image as corrupt if failing during create

During creation we write a minimal qcow2 header and then update it with extra features. If the updating fails for some reason we might still be left with a valid qcow2 image that will be mistakenly used for I/O. We cannot delete the image, since we don't know if we created the underlying storage or not. Thus we mark the header as corrupt to prevents its later usage.

---
 block/qcow2.c | 3 +++
 1 file changed, 3 insertions(+)
block/qcow2.c +3 -0
index ecc577175f..338513e652 100644
@@ -3104,6 +3104,9 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
 
     ret = 0;
 out:
+    if (ret < 0) {
+        qcow2_mark_corrupt(blk_bs(blk));
+    }
     blk_unref(blk);
     bdrv_unref(bs);
     return ret;
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/2 qcow2: fail if encryption opts are provided to non-encrypted image

In reply to Daniel P. Berrangé

If the qcow2 image does not have any encryption method specified in its header, the user should not be providing any encryption options when opening it. We already detect this if the user had set "encrypt.format" but this field is optional so must consider any "encrypt.*" option to be an error.

---
 block/qcow2.c | 6 ++++++
 1 file changed, 6 insertions(+)
block/qcow2.c +6 -0
index 65a54c9ac6..ecc577175f 100644
@@ -1045,6 +1045,12 @@ static int qcow2_update_options_prepare(BlockDriverState *bs,
             ret = -EINVAL;
             goto fail;
         }
+        if (encryptopts && qdict_size(encryptopts)) {
+            error_setg(errp, "No encryption in image header, but encryption "
+                       "options provided");
+            ret = -EINVAL;
+            goto fail;
+        }
         break;
 
     case QCOW_CRYPT_AES:
Signature · 1 line
2.20.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] qcow2: fail if encryption opts are provided to non-encrypted image

Quoted 11 lines · Daniel P. Berrangé

If the qcow2 image does not have any encryption method specified in its header, the user should not be providing any encryption options when opening it. We already detect this if the user had set "encrypt.format" but this field is optional so must consider any "encrypt.*" option to be an error.

---
 block/qcow2.c | 6 ++++++
 1 file changed, 6 insertions(+)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#6 lore raw

Re: [Qemu-devel] [PATCH 2/2] qcow2: mark image as corrupt if failing during create

In reply to Daniel P. Berrangé
Quoted 6 lines · Daniel P. Berrangé

During creation we write a minimal qcow2 header and then update it with extra features. If the updating fails for some reason we might still be left with a valid qcow2 image that will be mistakenly used for I/O. We cannot delete the image, since we don't know if we created the underlying storage or not. Thus we mark the header as corrupt to prevents its later usage.

Should we unconditionally mark the image as corrupt at the time we write the minimal qcow2 header, and then update the image to non-corrupt on the final update?

Quoted 11 lines
---
 block/qcow2.c | 3 +++
 1 file changed, 3 insertions(+)
block/qcow2.c +0 -0
index ecc577175f..338513e652 100644

block/qcow2.c qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp) · line 3109

Quoted 6 lines
block/qcow2.c +3 -0
@@ -3104,6 +3104,9 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
 
     ret = 0;
 out:
+    if (ret < 0) {
+        qcow2_mark_corrupt(blk_bs(blk));
+    }

If ret < 0 because of an EIO error, this may also fail to write the change to the header. Hence my question as to whether this is too late.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#7 lore raw

Re: [Qemu-devel] [PATCH 2/2] qcow2: mark image as corrupt if failing during create

Quoted 11 lines · Eric Blake
Quoted 6 lines · Daniel P. Berrangé

During creation we write a minimal qcow2 header and then update it with extra features. If the updating fails for some reason we might still be left with a valid qcow2 image that will be mistakenly used for I/O. We cannot delete the image, since we don't know if we created the underlying storage or not. Thus we mark the header as corrupt to prevents its later usage.

Should we unconditionally mark the image as corrupt at the time we write the minimal qcow2 header, and then update the image to non-corrupt on the final update?

That's a nice idea, but we call blk_new_open() half way through to qcow2_co_create method to open the minimal image. If we mark it corrupt upfront we'll never be able to open this minimal image.

Adding a flag to allow blk_new_open to ignore the "corrupt" marker feels unplesant to me.

Quoted 13 lines
Quoted 13 lines
---
 block/qcow2.c | 3 +++
 1 file changed, 3 insertions(+)
block/qcow2.c +0 -0
index ecc577175f..338513e652 100644
@@ -3104,6 +3104,9 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
 
Quoted 8 lines

block/qcow2.c qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp) · line 3109

Quoted 5 lines
block/qcow2.c +3 -0
     ret = 0;
 out:
+    if (ret < 0) {
+        qcow2_mark_corrupt(blk_bs(blk));
+    }

If ret < 0 because of an EIO error, this may also fail to write the change to the header. Hence my question as to whether this is too late.

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 :|
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] qcow2: fail if encryption opts are provided to non-encrypted image

In reply to Daniel P. Berrangé
Quoted 16 lines · Daniel P. Berrangé

If the qcow2 image does not have any encryption method specified in its header, the user should not be providing any encryption options when opening it. We already detect this if the user had set "encrypt.format" but this field is optional so must consider any "encrypt.*" option to be an error.

---
 block/qcow2.c | 6 ++++++
 1 file changed, 6 insertions(+)
block/qcow2.c +0 -0
index 65a54c9ac6..ecc577175f 100644

block/qcow2.c static int qcow2_update_options_prepare(BlockDriverState *bs, · line 1053

Quoted 9 lines
block/qcow2.c +6 -0
@@ -1045,6 +1045,12 @@ static int qcow2_update_options_prepare(BlockDriverState *bs,
             ret = -EINVAL;
             goto fail;
         }
+        if (encryptopts && qdict_size(encryptopts)) {
+            error_setg(errp, "No encryption in image header, but encryption "
+                       "options provided");
+            ret = -EINVAL;
+            goto fail;
+        }

Doesn't this make the block right before this one a bit superfluous?

Max

Quoted 4 lines
block/qcow2.c +0 -0
         break;
 
     case QCOW_CRYPT_AES:
#8 lore raw

Re: [Qemu-devel] [PATCH 2/2] qcow2: mark image as corrupt if failing during create

In reply to Daniel P. Berrangé
Quoted 17 lines · Daniel P. Berrangé

During creation we write a minimal qcow2 header and then update it with extra features. If the updating fails for some reason we might still be left with a valid qcow2 image that will be mistakenly used for I/O. We cannot delete the image, since we don't know if we created the underlying storage or not. Thus we mark the header as corrupt to prevents its later usage.

---
 block/qcow2.c | 3 +++
 1 file changed, 3 insertions(+)
block/qcow2.c +0 -0
index ecc577175f..338513e652 100644

block/qcow2.c qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp) · line 3108

Quoted 5 lines
block/qcow2.c +2 -0
@@ -3104,6 +3104,9 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
 
     ret = 0;
 out:
+    if (ret < 0) {
+        qcow2_mark_corrupt(blk_bs(blk));

First, blk_bs(blk) may be the qcow2 BDS, or it may the protocol BDS here (it is the latter before the first blk_new_open() call). Calling qcow2_mark_corrupt() unconditionally may mean an invalid access to bs->opaque (which is not going to be of type BDRVQcow2State if the BDS is not a qcow2 BDS).

Second, blk may be NULL (at various points, e.g. after blk_new_open() failed). Then this would yield a segfault in in blk_bs().

Third, blk_bs(blk) may be NULL (if blk_insert_bs() failed). Then this would yield a segfault in qcow2_mark_corrupt().

On a minor note, it is rather probably that blk_new_open() fails. In that case, there is currently no way to mark the image corrupt. Would it be useful and possible to have a function to mark a qcow2 image corrupt without relying on qcow2 features, i.e. by writing directly to the protocol layer (which is always @bs)? This would be unsafe to use as long as the protocol layer is opened by the qcow2 driver in some other node, but we could invoke this function safely after @blk has been freed.

Or maybe Eric's suggestion really is for the best, i.e. mark the image corrupt from the start and then clean that after we're all done. You don't need a new flag for that, we already have BDRV_O_CHECK.

Max

Quoted 5 lines
block/qcow2.c +1 -0
+    }
     blk_unref(blk);
     bdrv_unref(bs);
     return ret;
#4 lore raw

Re: [Qemu-devel] [PATCH 1/2] qcow2: fail if encryption opts are provided to non-encrypted image

In reply to Max Reitz
Quoted 12 lines · Max Reitz
Quoted 11 lines · Daniel P. Berrangé

If the qcow2 image does not have any encryption method specified in its header, the user should not be providing any encryption options when opening it. We already detect this if the user had set "encrypt.format" but this field is optional so must consider any "encrypt.*" option to be an error.

---
 block/qcow2.c | 6 ++++++
 1 file changed, 6 insertions(+)
Quoted 16 lines
Quoted 5 lines
block/qcow2.c +0 -0
index 65a54c9ac6..ecc577175f 100644

block/qcow2.c static int qcow2_update_options_prepare(BlockDriverState *bs, · line 1053

Quoted 9 lines
block/qcow2.c +6 -0
@@ -1045,6 +1045,12 @@ static int qcow2_update_options_prepare(BlockDriverState *bs,
             ret = -EINVAL;
             goto fail;
         }
+        if (encryptopts && qdict_size(encryptopts)) {
+            error_setg(errp, "No encryption in image header, but encryption "
+                       "options provided");
+            ret = -EINVAL;
+            goto fail;
+        }

Doesn't this make the block right before this one a bit superfluous?

Yes, in the sense that we'll still get an error if we removed the prior block. The prior block has a more useful error message which will help diagnosis though, so I thought it worth keeping both.

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 :|
#9 lore raw

Re: [Qemu-devel] [PATCH 2/2] qcow2: mark image as corrupt if failing during create

In reply to Max Reitz
Quoted 23 lines · Max Reitz
Quoted 22 lines · Daniel P. Berrangé

During creation we write a minimal qcow2 header and then update it with extra features. If the updating fails for some reason we might still be left with a valid qcow2 image that will be mistakenly used for I/O. We cannot delete the image, since we don't know if we created the underlying storage or not. Thus we mark the header as corrupt to prevents its later usage.

---
 block/qcow2.c | 3 +++
 1 file changed, 3 insertions(+)
block/qcow2.c +2 -0
index ecc577175f..338513e652 100644
@@ -3104,6 +3104,9 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
 
     ret = 0;
 out:
+    if (ret < 0) {
+        qcow2_mark_corrupt(blk_bs(blk));

...snip...

Quoted 3 lines

Or maybe Eric's suggestion really is for the best, i.e. mark the image corrupt from the start and then clean that after we're all done. You don't need a new flag for that, we already have BDRV_O_CHECK.

Ah, I didn't realize that is what BDRV_O_CHECK could do. I'll try this approach as it is nicer.

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 :|