Skip to contentSkip to conversation

block: Fix Transaction leaks

PATCH In review Kevin Wolf 3 participants 17 messages May 3, 2021 – May 5, 2021

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

cover block: Fix Transaction leaks

These are two follow-up fixes for Vladimir's "block: update graph permissions update". The bugs were reported by Coverity.

Kevin Wolf (2):
block: Fix Transaction leak in bdrv_root_attach_child()
block: Fix Transaction leak in bdrv_reopen_multiple()

 block.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.30.2
author
#1 lore raw

PATCH 1/2 block: Fix Transaction leak in bdrv_root_attach_child()

The error path needs to call tran_finalize(), too.

Fixes CID 1452773
Fixes 548a74c0dbc858edd1a7ee3045b5f2fe710bd8b1
---
 block.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
block.c +4 -3
index 874c22c43e..5c0ced6238 100644
@@ -2918,13 +2918,14 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
                                    child_role, perm, shared_perm, opaque,
                                    &child, tran, errp);
     if (ret < 0) {
-        bdrv_unref(child_bs);
-        return NULL;
+        assert(child == NULL);
+        goto out;
     }
 
     ret = bdrv_refresh_perms(child_bs, errp);
-    tran_finalize(tran, ret);
 
+out:
+    tran_finalize(tran, ret);
     bdrv_unref(child_bs);
     return child;
 }
Signature · 1 line
2.30.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] block: Fix Transaction leak in bdrv_root_attach_child()

Quoted 22 lines · Kevin Wolf

The error path needs to call tran_finalize(), too.

Fixes CID 1452773
Fixes 548a74c0dbc858edd1a7ee3045b5f2fe710bd8b1
---
  block.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
block.c +2 -2
index 874c22c43e..5c0ced6238 100644
@@ -2918,13 +2918,14 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
                                     child_role, perm, shared_perm, opaque,
                                     &child, tran, errp);
      if (ret < 0) {
-        bdrv_unref(child_bs);
-        return NULL;
+        assert(child == NULL);
+        goto out;

block.c BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, · line 2931

Quoted 10 lines
block.c +2 -1
      }
 
      ret = bdrv_refresh_perms(child_bs, errp);
-    tran_finalize(tran, ret);
 
+out:
+    tran_finalize(tran, ret);
      bdrv_unref(child_bs);
      return child;
  }

I like my additional comment and assertion in "[PATCH 1/6] block: fix leak of tran in bdrv_root_attach_child", still this is OK too:

Signature · 2 lines
Best regards,
Vladimir
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] block: Fix Transaction leak in bdrv_root_attach_child()

In reply to Kevin Wolf
Quoted 20 lines · Kevin Wolf

The error path needs to call tran_finalize(), too.

Fixes CID 1452773
Fixes 548a74c0dbc858edd1a7ee3045b5f2fe710bd8b1
---
  block.c | 7 ++++---
  1 file changed, 4 insertions(+), 3 deletions(-)
block.c +1 -2
index 874c22c43e..5c0ced6238 100644
@@ -2918,13 +2918,14 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
                                     child_role, perm, shared_perm, opaque,
                                     &child, tran, errp);
      if (ret < 0) {
-        bdrv_unref(child_bs);
-        return NULL;
+        assert(child == NULL);

block.c BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, · line 2930

Quoted 10 lines
block.c +3 -1
+        goto out;
      }
 
      ret = bdrv_refresh_perms(child_bs, errp);
-    tran_finalize(tran, ret);
 
+out:
+    tran_finalize(tran, ret);
      bdrv_unref(child_bs);
      return child;

Looks OK, so:

However, the function’s description says that it will return NULL on error. But if bdrv_refresh_perms() fails, it will still return a non-NULL child. Is that right?

#4 lore raw

Re: [PATCH 1/2] block: Fix Transaction leak in bdrv_root_attach_child()

Quoted 32 lines · Max Reitz
Quoted 18 lines · Kevin Wolf

The error path needs to call tran_finalize(), too.

Fixes CID 1452773
Fixes 548a74c0dbc858edd1a7ee3045b5f2fe710bd8b1

---
  block.c | 7 ++++---

  1 file changed, 4 insertions(+), 3 deletions(-)
block.c +0 -1
index 874c22c43e..5c0ced6238 100644
@@ -2918,13 +2918,14 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
                                      child_role, perm, shared_perm, opaque,
                                      &child, tran, errp);
       if (ret < 0) {
-        bdrv_unref(child_bs);

block.c BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, · line 2928

Quoted 10 lines
block.c +4 -2
-        return NULL;
+        assert(child == NULL);
+        goto out;
       }
       ret = bdrv_refresh_perms(child_bs, errp);
-    tran_finalize(tran, ret);
+out:
+    tran_finalize(tran, ret);
       bdrv_unref(child_bs);
       return child;

Looks OK, so:

Quoted 4 lines

However, the function’s description says that it will return NULL on error.  But if bdrv_refresh_perms() fails, it will still return a non-NULL child.  Is that right?

No, it's reset to NULL on transaction abort, so code is correct. It's not obvious, and I've added a comment and assertion in my version of this fix "[PATCH 1/6] block: fix leak of tran in bdrv_root_attach_child"

Signature · 2 lines
Best regards,
Vladimir
#5 lore raw

Re: [PATCH 1/2] block: Fix Transaction leak in bdrv_root_attach_child()

Quoted 34 lines · Vladimir Sementsov-Ogievskiy
Quoted 33 lines · Max Reitz
Quoted 19 lines · Kevin Wolf

The error path needs to call tran_finalize(), too.

Fixes CID 1452773
Fixes 548a74c0dbc858edd1a7ee3045b5f2fe710bd8b1

---
  block.c | 7 ++++---

  1 file changed, 4 insertions(+), 3 deletions(-)
block.c +0 -1
index 874c22c43e..5c0ced6238 100644
@@ -2918,13 +2918,14 @@ BdrvChild 
 *bdrv_root_attach_child(BlockDriverState *child_bs,
                                      child_role, perm, shared_perm, opaque,
                                      &child, tran, errp);
       if (ret < 0) {
-        bdrv_unref(child_bs);

block.c BdrvChild · line 2929

Quoted 10 lines
block.c +4 -2
-        return NULL;
+        assert(child == NULL);
+        goto out;
       }
       ret = bdrv_refresh_perms(child_bs, errp);
-    tran_finalize(tran, ret);
+out:
+    tran_finalize(tran, ret);
       bdrv_unref(child_bs);
       return child;

Looks OK, so:

Quoted 6 lines
Quoted 4 lines

However, the function’s description says that it will return NULL on error.  But if bdrv_refresh_perms() fails, it will still return a non-NULL child.  Is that right?

No, it's reset to NULL on transaction abort, so code is correct. It's not obvious, and I've added a comment and assertion in my version of this fix "[PATCH 1/6] block: fix leak of tran in bdrv_root_attach_child"

The fact that the transaction keeps the pointer to this local variable around is a bit horrifying, but well.

Max

#6 lore raw

Re: [PATCH 1/2] block: Fix Transaction leak in bdrv_root_attach_child()

Quoted 34 lines · Max Reitz
Quoted 33 lines · Vladimir Sementsov-Ogievskiy
Quoted 32 lines · Max Reitz
Quoted 18 lines · Kevin Wolf

The error path needs to call tran_finalize(), too.

Fixes CID 1452773
Fixes 548a74c0dbc858edd1a7ee3045b5f2fe710bd8b1

---
  block.c | 7 ++++---

  1 file changed, 4 insertions(+), 3 deletions(-)
block.c +0 -1
index 874c22c43e..5c0ced6238 100644
@@ -2918,13 +2918,14 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
                                      child_role, perm, shared_perm, opaque,
                                      &child, tran, errp);
       if (ret < 0) {
-        bdrv_unref(child_bs);

block.c BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, · line 2928

Quoted 10 lines
block.c +4 -2
-        return NULL;
+        assert(child == NULL);
+        goto out;
       }
       ret = bdrv_refresh_perms(child_bs, errp);
-    tran_finalize(tran, ret);
+out:
+    tran_finalize(tran, ret);
       bdrv_unref(child_bs);
       return child;

Looks OK, so:

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

However, the function’s description says that it will return NULL on error.  But if bdrv_refresh_perms() fails, it will still return a non-NULL child.  Is that right?

No, it's reset to NULL on transaction abort, so code is correct. It's not obvious, and I've added a comment and assertion in my version of this fix "[PATCH 1/6] block: fix leak of tran in bdrv_root_attach_child"

The fact that the transaction keeps the pointer to this local variable around is a bit horrifying, but well.

Yes this looks overcomplicated here. But it's useful for bdrv_set_backing_noperm where we would have to add a separate entry to @tran to rollback bs->backing change. Probably, it's better to refactor this thing. Or at least document that out-pointer argument goes into transaction, and should be valid up to transaction finalization.

Signature · 2 lines
Best regards,
Vladimir
#7 lore raw

Re: [PATCH 1/2] block: Fix Transaction leak in bdrv_root_attach_child()

Quoted 40 lines · Vladimir Sementsov-Ogievskiy
Quoted 39 lines · Max Reitz
Quoted 38 lines · Vladimir Sementsov-Ogievskiy
Quoted 36 lines · Max Reitz
Quoted 18 lines · Kevin Wolf

The error path needs to call tran_finalize(), too.

Fixes CID 1452773
Fixes 548a74c0dbc858edd1a7ee3045b5f2fe710bd8b1

---
  block.c | 7 ++++---

  1 file changed, 4 insertions(+), 3 deletions(-)
block.c +0 -1
index 874c22c43e..5c0ced6238 100644
@@ -2918,13 +2918,14 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
                                      child_role, perm, shared_perm, opaque,
                                      &child, tran, errp);
       if (ret < 0) {
-        bdrv_unref(child_bs);

block.c BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, · line 2928

Quoted 10 lines
block.c +4 -2
-        return NULL;
+        assert(child == NULL);
+        goto out;
       }
       ret = bdrv_refresh_perms(child_bs, errp);
-    tran_finalize(tran, ret);
+out:
+    tran_finalize(tran, ret);
       bdrv_unref(child_bs);
       return child;

Looks OK, so:

However, the function’s description says that it will return NULL on error.  But if bdrv_refresh_perms() fails, it will still return a non-NULL child.  Is that right?

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

No, it's reset to NULL on transaction abort, so code is correct. It's not obvious, and I've added a comment and assertion in my version of this fix "[PATCH 1/6] block: fix leak of tran in bdrv_root_attach_child"

The fact that the transaction keeps the pointer to this local variable around is a bit horrifying, but well.

Yes this looks overcomplicated here. But it's useful for bdrv_set_backing_noperm where we would have to add a separate entry to @tran to rollback bs->backing change. Probably, it's better to refactor this thing. Or at least document that out-pointer argument goes into transaction, and should be valid up to transaction finalization.

documentation done:

[PATCH 7/6] block: document child argument of bdrv_attach_child_common()

Signature · 2 lines
Best regards,
Vladimir
author
#8 lore raw

PATCH 2/2 block: Fix Transaction leak in bdrv_reopen_multiple()

In reply to Kevin Wolf

Like other error paths, this one needs to call tran_finalize() and clean up the BlockReopenQueue, too.

Fixes CID 1452772
Fixes 72373e40fbc7e4218061a8211384db362d3e7348
---
 block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
block.c +1 -1
index 5c0ced6238..69615fabd1 100644
@@ -4052,7 +4052,7 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
         ret = bdrv_flush(bs_entry->state.bs);
         if (ret < 0) {
             error_setg_errno(errp, -ret, "Error flushing drive");
-            goto cleanup;
+            goto abort;
         }
     }
 
Signature · 1 line
2.30.2
#9 lore raw

Re: [PATCH 2/2] block: Fix Transaction leak in bdrv_reopen_multiple()

Quoted 2 lines · Kevin Wolf

Like other error paths, this one needs to call tran_finalize() and clean up the BlockReopenQueue, too.

We don't need the "abort" loop on that path. And clean-up of BlockReopenQueue is at "cleanup:" label.

So I'd prefer Peter's suggestion (my "[PATCH 2/6] block: bdrv_reopen_multiple(): fix leak of tran object")

Quoted 22 lines
Fixes CID 1452772
Fixes 72373e40fbc7e4218061a8211384db362d3e7348
---
  block.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
block.c +1 -1
index 5c0ced6238..69615fabd1 100644
@@ -4052,7 +4052,7 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
          ret = bdrv_flush(bs_entry->state.bs);
          if (ret < 0) {
              error_setg_errno(errp, -ret, "Error flushing drive");
-            goto cleanup;
+            goto abort;
          }
      }
 
Signature · 2 lines
Best regards,
Vladimir
author
#10 lore raw

Re: [PATCH 2/2] block: Fix Transaction leak in bdrv_reopen_multiple()

Am 03.05.2021 um 13:40 hat Vladimir Sementsov-Ogievskiy geschrieben:

Quoted 6 lines
Quoted 2 lines · Kevin Wolf

Like other error paths, this one needs to call tran_finalize() and clean up the BlockReopenQueue, too.

We don't need the "abort" loop on that path. And clean-up of BlockReopenQueue is at "cleanup:" label.

The cleanup of the BlockReopenQueue itself is there, but not of all fields in it. Specifically, these lines are needed:

    qobject_unref(bs_entry->state.explicit_options);
    qobject_unref(bs_entry->state.options);

The references are taken in bdrv_reopen_queue_child() and either used in commit or released on abort. Doing nothing with them leaks them.

Quoted 2 lines

So I'd prefer Peter's suggestion (my "[PATCH 2/6] block:
bdrv_reopen_multiple(): fix leak of tran object")

I don't like it because I think every call that doesn't end in a commit, should jump to the abort label to make sure that everything that remains unused because of this is correctly cleaned up.

Kevin

#11 lore raw

Re: [PATCH 2/2] block: Fix Transaction leak in bdrv_reopen_multiple()

Quoted 8 lines · Kevin Wolf

Am 03.05.2021 um 13:40 hat Vladimir Sementsov-Ogievskiy geschrieben:

Quoted 6 lines
Quoted 2 lines · Kevin Wolf

Like other error paths, this one needs to call tran_finalize() and clean up the BlockReopenQueue, too.

We don't need the "abort" loop on that path. And clean-up of BlockReopenQueue is at "cleanup:" label.

Quoted 8 lines

The cleanup of the BlockReopenQueue itself is there, but not of all fields in it. Specifically, these lines are needed:

     qobject_unref(bs_entry->state.explicit_options);
     qobject_unref(bs_entry->state.options);

The references are taken in bdrv_reopen_queue_child() and either used in commit or released on abort. Doing nothing with them leaks them.

Oops. Somehow I decided they are set in _prepare.

Quoted 8 lines
Quoted 3 lines

So I'd prefer Peter's suggestion (my "[PATCH 2/6] block:
bdrv_reopen_multiple(): fix leak of tran object")

I don't like it because I think every call that doesn't end in a commit, should jump to the abort label to make sure that everything that remains unused because of this is correctly cleaned up.

agree now..

Still, don't we miss these two qobject_unref() calls on success path?

Signature · 2 lines
Best regards,
Vladimir
author
#12 lore raw

Re: [PATCH 2/2] block: Fix Transaction leak in bdrv_reopen_multiple()

Am 03.05.2021 um 15:09 hat Vladimir Sementsov-Ogievskiy geschrieben:

Quoted 30 lines
Quoted 8 lines · Kevin Wolf

Am 03.05.2021 um 13:40 hat Vladimir Sementsov-Ogievskiy geschrieben:

Quoted 6 lines
Quoted 2 lines · Kevin Wolf

Like other error paths, this one needs to call tran_finalize() and clean up the BlockReopenQueue, too.

We don't need the "abort" loop on that path. And clean-up of BlockReopenQueue is at "cleanup:" label.

Quoted 8 lines

The cleanup of the BlockReopenQueue itself is there, but not of all fields in it. Specifically, these lines are needed:

     qobject_unref(bs_entry->state.explicit_options);
     qobject_unref(bs_entry->state.options);

The references are taken in bdrv_reopen_queue_child() and either used in commit or released on abort. Doing nothing with them leaks them.

Oops. Somehow I decided they are set in _prepare.

Quoted 8 lines
Quoted 3 lines

So I'd prefer Peter's suggestion (my "[PATCH 2/6] block:
bdrv_reopen_multiple(): fix leak of tran object")

I don't like it because I think every call that doesn't end in a commit, should jump to the abort label to make sure that everything that remains unused because of this is correctly cleaned up.

Quoted 3 lines

agree now..

Still, don't we miss these two qobject_unref() calls on success path?

No, they are put to use in bdrv_reopen_commit():

    qobject_unref(bs->explicit_options);
    qobject_unref(bs->options);

    bs->explicit_options   = reopen_state->explicit_options;
    bs->options            = reopen_state->options;

Kevin

#13 lore raw

Re: [PATCH 2/2] block: Fix Transaction leak in bdrv_reopen_multiple()

Quoted 37 lines · Kevin Wolf

Am 03.05.2021 um 15:09 hat Vladimir Sementsov-Ogievskiy geschrieben:

Quoted 30 lines
Quoted 8 lines · Kevin Wolf

Am 03.05.2021 um 13:40 hat Vladimir Sementsov-Ogievskiy geschrieben:

Quoted 6 lines
Quoted 2 lines · Kevin Wolf

Like other error paths, this one needs to call tran_finalize() and clean up the BlockReopenQueue, too.

We don't need the "abort" loop on that path. And clean-up of BlockReopenQueue is at "cleanup:" label.

Quoted 8 lines

The cleanup of the BlockReopenQueue itself is there, but not of all fields in it. Specifically, these lines are needed:

      qobject_unref(bs_entry->state.explicit_options);
      qobject_unref(bs_entry->state.options);

The references are taken in bdrv_reopen_queue_child() and either used in commit or released on abort. Doing nothing with them leaks them.

Oops. Somehow I decided they are set in _prepare.

Quoted 8 lines
Quoted 3 lines

So I'd prefer Peter's suggestion (my "[PATCH 2/6] block:
bdrv_reopen_multiple(): fix leak of tran object")

I don't like it because I think every call that doesn't end in a commit, should jump to the abort label to make sure that everything that remains unused because of this is correctly cleaned up.

Quoted 3 lines

agree now..

Still, don't we miss these two qobject_unref() calls on success path?

No, they are put to use in bdrv_reopen_commit():

Quoted 8 lines
     qobject_unref(bs->explicit_options);
     qobject_unref(bs->options);

     bs->explicit_options   = reopen_state->explicit_options;
     bs->options            = reopen_state->options;

Kevin

OK then. I should have checked myself :\

Signature · 2 lines
Best regards,
Vladimir
Reviewed-by on v1 2/2
#14 lore raw

Re: [PATCH 2/2] block: Fix Transaction leak in bdrv_reopen_multiple()

In reply to Kevin Wolf
Quoted 16 lines · Kevin Wolf

Like other error paths, this one needs to call tran_finalize() and clean up the BlockReopenQueue, too.

Fixes CID 1452772
Fixes 72373e40fbc7e4218061a8211384db362d3e7348
---
  block.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
block.c +0 -0
index 5c0ced6238..69615fabd1 100644

block.c int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) · line 4058

Quoted 8 lines
block.c +1 -1
@@ -4052,7 +4052,7 @@ int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
          ret = bdrv_flush(bs_entry->state.bs);
          if (ret < 0) {
              error_setg_errno(errp, -ret, "Error flushing drive");
-            goto cleanup;
+            goto abort;
          }
      }
 
Signature · 2 lines
Best regards,
Vladimir
#15 lore raw
In reply to Kevin Wolf
Quoted 10 lines · Kevin Wolf

These are two follow-up fixes for Vladimir's "block: update graph permissions update". The bugs were reported by Coverity.

Kevin Wolf (2):
block: Fix Transaction leak in bdrv_root_attach_child()
block: Fix Transaction leak in bdrv_reopen_multiple()

  block.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)

I'll rebase my "[PATCH 0/6] block permission updated follow-up" on top of this.

Signature · 2 lines
Best regards,
Vladimir
author
#16 lore raw

Am 04.05.2021 um 08:56 hat Vladimir Sementsov-Ogievskiy geschrieben:

Quoted 8 lines
Quoted 7 lines · Kevin Wolf

These are two follow-up fixes for Vladimir's "block: update graph permissions update". The bugs were reported by Coverity.

Kevin Wolf (2):
block: Fix Transaction leak in bdrv_root_attach_child()
block: Fix Transaction leak in bdrv_reopen_multiple()

Quoted 5 lines
Quoted 2 lines
  block.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)

I'll rebase my "[PATCH 0/6] block permission updated follow-up" on top of this.

Ok, thanks. I've applied this to my block branch now.

Kevin