Skip to contentSkip to conversation

block/snapshot: fix *errp handling in bdrv_snapshot_goto

PATCH In review [email protected] 2 participants 2 messages Mar 4, 2025 – Mar 10, 2025 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block/snapshot: fix *errp handling in bdrv_snapshot_goto

From: Denis Rastyogin <[email protected]>

This error was discovered by fuzzing qemu-img.

If bdrv_snapshot_goto() returns an error, it is not handled immediately, allowing *errp to be reassigned when qcow_open() fails, which triggers assert(*errp == NULL) in util/error.c: void error_setv().

This patch ensures that errors from bdrv_snapshot_goto() are handled immediately after the call, preventing *errp from being modified twice and avoiding unnecessary assertion failures.

---
 block/snapshot.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
block/snapshot.c +8 -2
index 9c44780e96..d1b5a8d33d 100644
@@ -296,14 +296,20 @@ int bdrv_snapshot_goto(BlockDriverState *bs,
         bdrv_graph_wrunlock();
 
         ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp);
+        if (ret < 0) {
+            bdrv_unref(fallback_bs);
+            bs->drv = NULL;
+            /* A bdrv_snapshot_goto() error takes precedence */
+            error_propagate(errp, local_err);
+            return ret;
+        }
         open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err);
         qobject_unref(options);
         if (open_ret < 0) {
             bdrv_unref(fallback_bs);
             bs->drv = NULL;
-            /* A bdrv_snapshot_goto() error takes precedence */
             error_propagate(errp, local_err);
-            return ret < 0 ? ret : open_ret;
+            return open_ret;
         }
 
         /*
Signature · 1 line
2.42.2
#1 lore raw

Am 04.03.2025 um 09:01 hat [email protected] geschrieben:

Quoted 23 lines

From: Denis Rastyogin <[email protected]>

This error was discovered by fuzzing qemu-img.

If bdrv_snapshot_goto() returns an error, it is not handled immediately, allowing *errp to be reassigned when qcow_open() fails, which triggers assert(*errp == NULL) in util/error.c: void error_setv().

This patch ensures that errors from bdrv_snapshot_goto() are handled immediately after the call, preventing *errp from being modified twice and avoiding unnecessary assertion failures.

---
 block/snapshot.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
block/snapshot.c +0 -0
index 9c44780e96..d1b5a8d33d 100644

block/snapshot.c int bdrv_snapshot_goto(BlockDriverState *bs, · line 305

Quoted 10 lines
block/snapshot.c +7 -0
@@ -296,14 +296,20 @@ int bdrv_snapshot_goto(BlockDriverState *bs,
         bdrv_graph_wrunlock();
 
         ret = bdrv_snapshot_goto(fallback_bs, snapshot_id, errp);
+        if (ret < 0) {
+            bdrv_unref(fallback_bs);
+            bs->drv = NULL;
+            /* A bdrv_snapshot_goto() error takes precedence */
+            error_propagate(errp, local_err);
+            return ret;
+        }

Now you return without having reopened the image!

block/snapshot.c int bdrv_snapshot_goto(BlockDriverState *bs, · line 313

Quoted 10 lines
block/snapshot.c +1 -2
         open_ret = drv->bdrv_open(bs, options, bs->open_flags, &local_err);
         qobject_unref(options);
         if (open_ret < 0) {
             bdrv_unref(fallback_bs);
             bs->drv = NULL;
-            /* A bdrv_snapshot_goto() error takes precedence */
             error_propagate(errp, local_err);
-            return ret < 0 ? ret : open_ret;
+            return open_ret;
         }

I don't see a problem in the old code. local_err is still NULL at this point, so it's safe to pass it to drv->bdrv_open(). A previous error from bdrv_snapshot_goto() is stored in errp, which is separate.

The line in qcow.c (test.qed is misleading as a filename!) that causes the assertion failure is this:

    error_setg(&s->migration_blocker, "The qcow format used by node '%s' "
               "does not support live migration",
               bdrv_get_device_or_node_name(bs));

This is an internal field that is not related to either error in bdrv_snapshot_goto(). This is another instance of the same bug that bs->opaque needs to be zeroed before calling drv->bdrv_open(). I sent a patch for that.

Kevin