Skip to contentSkip to conversation

block: Fix leaks of opts in block drivers

PATCH Superseded Fam Zheng 3 participants 13 messages Aug 27, 2014 – Aug 28, 2014 Block layer

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

cover block: Fix leaks of opts in block drivers

Remember to call qemu_opts_del before function returning.

Fam Zheng (4):
nfs: Fix leak of opts in nfs_file_open
blkverify: Fix leak of opts in blkverify_open
quorum: Fix leak of opts in quorum_open
qcow2: Fix leak of opts in qcow2_open

 block/blkverify.c | 1 +
 block/nfs.c       | 7 +++++--
 block/qcow2.c     | 5 ++---
 block/quorum.c    | 3 ++-
 4 files changed, 10 insertions(+), 6 deletions(-)
Signature · 1 line
2.1.0
author
#1 lore raw

PATCH 1/4 nfs: Fix leak of opts in nfs_file_open

---
 block/nfs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
block/nfs.c +5 -2
index 93d87f3..36e8057 100644
@@ -393,15 +393,18 @@ static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
     qemu_opts_absorb_qdict(opts, options, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
-        return -EINVAL;
+        ret = -EINVAL;
+        goto out;
     }
     ret = nfs_client_open(client, qemu_opt_get(opts, "filename"),
                           (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
                           errp);
     if (ret < 0) {
-        return ret;
+        goto out;
     }
     bs->total_sectors = ret;
+out:
+    qemu_opts_del(opts);
     return 0;
 }
 
Signature · 1 line
2.1.0
#2 lore raw

Re: [Qemu-devel] [PATCH 1/4] nfs: Fix leak of opts in nfs_file_open

Quoted 10 lines · Fam Zheng
---
 block/nfs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
block/nfs.c +0 -0
index 93d87f3..36e8057 100644

block/nfs.c static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags, · line 396

Quoted 5 lines
block/nfs.c +1 -1
@@ -393,15 +393,18 @@ static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
     qemu_opts_absorb_qdict(opts, options, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
-        return -EINVAL;
+        ret = -EINVAL;

1)

Here you put the return code in ret and goto out.

block/nfs.c static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags, · line 403

Quoted 8 lines
block/nfs.c +2 -1
+        goto out;
     }
     ret = nfs_client_open(client, qemu_opt_get(opts, "filename"),
                           (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
                           errp);
     if (ret < 0) {
-        return ret;
+        goto out;

2) here you seem to want to do something later with the value of ret.

block/nfs.c static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,

Quoted 6 lines
block/nfs.c +2 -0
     }
     bs->total_sectors = ret;
+out:
+    qemu_opts_del(opts);
 
     return 0;

Here the code simply return 0 discarding the return code you set in 1) and 2).

Quoted 6 lines
block/nfs.c +0 -0
 }
Signature · 2 lines
-- 
2.1.0
author
#3 lore raw

Re: [Qemu-devel] [PATCH 1/4] nfs: Fix leak of opts in nfs_file_open

Quoted 32 lines · Benoît Canet
Quoted 10 lines · Fam Zheng
---
 block/nfs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
block/nfs.c +0 -0
index 93d87f3..36e8057 100644

block/nfs.c static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags, · line 396

Quoted 5 lines
block/nfs.c +1 -1
@@ -393,15 +393,18 @@ static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
     qemu_opts_absorb_qdict(opts, options, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
-        return -EINVAL;
+        ret = -EINVAL;

1)

Here you put the return code in ret and goto out.

block/nfs.c static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags, · line 403

Quoted 8 lines
block/nfs.c +2 -1
+        goto out;
     }
     ret = nfs_client_open(client, qemu_opt_get(opts, "filename"),
                           (flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
                           errp);
     if (ret < 0) {
-        return ret;
+        goto out;

2) here you seem to want to do something later with the value of ret.

Quoted 8 lines

block/nfs.c static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,

Quoted 6 lines
block/nfs.c +2 -0
     }
     bs->total_sectors = ret;
+out:
+    qemu_opts_del(opts);
 
     return 0;

Here the code simply return 0 discarding the return code you set in 1) and 2).

Good catch. Will fix.

Fam

Quoted 7 lines
Quoted 7 lines
block/nfs.c +0 -0
 }
Signature · 2 lines
-- 
2.1.0
author
#4 lore raw

PATCH 2/4 blkverify: Fix leak of opts in blkverify_open

In reply to Fam Zheng
---
 block/blkverify.c | 1 +
 1 file changed, 1 insertion(+)
block/blkverify.c +1 -0
index 7c78ca4..163064c 100644
@@ -158,6 +158,7 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
 
     ret = 0;
 fail:
+    qemu_opts_del(opts);
     return ret;
 }
 
Signature · 1 line
2.1.0
Reviewed-by on v1 2/4
#5 lore raw

Re: [Qemu-devel] [PATCH 2/4] blkverify: Fix leak of opts in blkverify_open

Quoted 21 lines · Fam Zheng
---
 block/blkverify.c | 1 +
 1 file changed, 1 insertion(+)
block/blkverify.c +1 -0
index 7c78ca4..163064c 100644
@@ -158,6 +158,7 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
 
     ret = 0;
 fail:
+    qemu_opts_del(opts);
     return ret;
 }
 
Signature · 1 line
2.1.0
author
#6 lore raw

PATCH 3/4 quorum: Fix leak of opts in quorum_open

In reply to Fam Zheng
---
 block/quorum.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/quorum.c +2 -1
index 0de07bb..ffe8bd9 100644
@@ -796,7 +796,7 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
 {
     BDRVQuorumState *s = bs->opaque;
     Error *local_err = NULL;
-    QemuOpts *opts;
+    QemuOpts *opts = NULL;
     bool *opened;
     QDict *sub = NULL;
     QList *list = NULL;
@@ -908,6 +908,7 @@ close_exit:
     g_free(s->bs);
     g_free(opened);
 exit:
+    qemu_opts_del(opts);
     /* propagate error */
     if (local_err) {
         error_propagate(errp, local_err);
Signature · 1 line
2.1.0
Reviewed-by on v1 3/4
#7 lore raw

Re: [Qemu-devel] [PATCH 3/4] quorum: Fix leak of opts in quorum_open

Quoted 30 lines · Fam Zheng
---
 block/quorum.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/quorum.c +2 -1
index 0de07bb..ffe8bd9 100644
@@ -796,7 +796,7 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
 {
     BDRVQuorumState *s = bs->opaque;
     Error *local_err = NULL;
-    QemuOpts *opts;
+    QemuOpts *opts = NULL;
     bool *opened;
     QDict *sub = NULL;
     QList *list = NULL;
@@ -908,6 +908,7 @@ close_exit:
     g_free(s->bs);
     g_free(opened);
 exit:
+    qemu_opts_del(opts);
     /* propagate error */
     if (local_err) {
         error_propagate(errp, local_err);
Signature · 1 line
2.1.0
author
#8 lore raw

PATCH 4/4 qcow2: Fix leak of opts in qcow2_open

In reply to Fam Zheng

Not all the error paths release opts, fix it by moving it after the fail label.

---
 block/qcow2.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
block/qcow2.c +2 -3
index f9e045f..b4c4a6e 100644
@@ -535,7 +535,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
     unsigned int len, i;
     int ret = 0;
     QCowHeader header;
-    QemuOpts *opts;
+    QemuOpts *opts = NULL;
     Error *local_err = NULL;
     uint64_t ext_end;
     uint64_t l1_vm_state_index;
@@ -932,7 +932,6 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
         error_setg(errp, "Unsupported value '%s' for qcow2 option "
                    "'overlap-check'. Allowed are either of the following: "
                    "none, constant, cached, all", opt_overlap_check);
-        qemu_opts_del(opts);
         ret = -EINVAL;
         goto fail;
     }
@@ -946,7 +945,6 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
                               overlap_check_template & (1 << i)) << i;
     }
 
-    qemu_opts_del(opts);
 
     if (s->use_lazy_refcounts && s->qcow_version < 3) {
         error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
@@ -964,6 +962,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
     return ret;
 
  fail:
+    qemu_opts_del(opts);
     g_free(s->unknown_header_fields);
     cleanup_unknown_header_ext(bs);
     qcow2_free_snapshots(bs);
Signature · 1 line
2.1.0
#9 lore raw

Re: [Qemu-devel] [PATCH 4/4] qcow2: Fix leak of opts in qcow2_open

Quoted 38 lines · Fam Zheng

Not all the error paths release opts, fix it by moving it after the fail label.

---
 block/qcow2.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
block/qcow2.c +1 -3
index f9e045f..b4c4a6e 100644
@@ -535,7 +535,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
     unsigned int len, i;
     int ret = 0;
     QCowHeader header;
-    QemuOpts *opts;
+    QemuOpts *opts = NULL;
     Error *local_err = NULL;
     uint64_t ext_end;
     uint64_t l1_vm_state_index;
@@ -932,7 +932,6 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
         error_setg(errp, "Unsupported value '%s' for qcow2 option "
                    "'overlap-check'. Allowed are either of the following: "
                    "none, constant, cached, all", opt_overlap_check);
-        qemu_opts_del(opts);
         ret = -EINVAL;
         goto fail;
     }
@@ -946,7 +945,6 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
                               overlap_check_template & (1 << i)) << i;
     }
 
-    qemu_opts_del(opts);
 
     if (s->use_lazy_refcounts && s->qcow_version < 3) {
         error_setg(errp, "Lazy refcounts require a qcow2 image with at least "

block/qcow2.c static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, · line 964

Quoted 3 lines
block/qcow2.c +0 -0
@@ -964,6 +962,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
 
 
     return ret;

The code is returning here in this case and the qemu_opts_del is hidden in the fail label. Do we need to qemu_opts_del on success just before return like it was done before ?

Quoted 10 lines
block/qcow2.c +1 -0
  fail:
+    qemu_opts_del(opts);
     g_free(s->unknown_header_fields);
     cleanup_unknown_header_ext(bs);
     qcow2_free_snapshots(bs);
Signature · 1 line
2.1.0
author
#10 lore raw

Re: [Qemu-devel] [PATCH 4/4] qcow2: Fix leak of opts in qcow2_open

Quoted 36 lines · Benoît Canet
Quoted 35 lines · Fam Zheng

Not all the error paths release opts, fix it by moving it after the fail label.

---
 block/qcow2.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
block/qcow2.c +1 -3
index f9e045f..b4c4a6e 100644
@@ -535,7 +535,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
     unsigned int len, i;
     int ret = 0;
     QCowHeader header;
-    QemuOpts *opts;
+    QemuOpts *opts = NULL;
     Error *local_err = NULL;
     uint64_t ext_end;
     uint64_t l1_vm_state_index;
@@ -932,7 +932,6 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
         error_setg(errp, "Unsupported value '%s' for qcow2 option "
                    "'overlap-check'. Allowed are either of the following: "
                    "none, constant, cached, all", opt_overlap_check);
-        qemu_opts_del(opts);
         ret = -EINVAL;
         goto fail;
     }
@@ -946,7 +945,6 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
                               overlap_check_template & (1 << i)) << i;
     }
 
-    qemu_opts_del(opts);
 
Quoted 8 lines

block/qcow2.c static int qcow2_open(BlockDriverState *bs, QDict *options, int flags, · line 964

Quoted 6 lines
block/qcow2.c +0 -0
     if (s->use_lazy_refcounts && s->qcow_version < 3) {
         error_setg(errp, "Lazy refcounts require a qcow2 image with at least "
@@ -964,6 +962,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
 
 
     return ret;

The code is returning here in this case and the qemu_opts_del is hidden in the fail label. Do we need to qemu_opts_del on success just before return like it was done before ?

Yes, good catch.

Thanks,
Fam

Quoted 11 lines
Quoted 11 lines
block/qcow2.c +1 -0
  fail:
+    qemu_opts_del(opts);
     g_free(s->unknown_header_fields);
     cleanup_unknown_header_ext(bs);
     qcow2_free_snapshots(bs);
Signature · 1 line
2.1.0
#11 lore raw

Re: [Qemu-devel] [PATCH 4/4] qcow2: Fix leak of opts in qcow2_open

In reply to Fam Zheng
Quoted 7 lines · Fam Zheng

Not all the error paths release opts, fix it by moving it after the fail label.

---
  block/qcow2.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)

This is pretty much the same patch as 'qcow2: Fix leak of QemuOpts in qcow2_open()' from my 'qapi/block-core: Add "new" qcow2 options' series. I guess it's up to the maintainer(s) which one gets pulled, I just want to point out that pulling one means leaving out the other.

Max

author
#12 lore raw

Re: [Qemu-devel] [PATCH 4/4] qcow2: Fix leak of opts in qcow2_open

Quoted 9 lines · Max Reitz
Quoted 7 lines · Fam Zheng

Not all the error paths release opts, fix it by moving it after the fail label.

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

This is pretty much the same patch as 'qcow2: Fix leak of QemuOpts in qcow2_open()' from my 'qapi/block-core: Add "new" qcow2 options' series. I guess it's up to the maintainer(s) which one gets pulled, I just want to point out that pulling one means leaving out the other.

Since I need to respin this series, I will drop this one and leave it for your series.

Thanks for pointing out. :)

Fam