qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] qom-qmp-cmds: Fix another memory leak in qmp_object_add()
@ 2020-03-17  9:22 Markus Armbruster
  2020-03-17  9:32 ` Marc-André Lureau
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Markus Armbruster @ 2020-03-17  9:22 UTC (permalink / raw)
  To: qemu-devel; +Cc: Kevin Wolf, pbonzini, berrange, ehabkost

When user_creatable_add_type() fails, qmp_object_add() returns both
its error and the usual empty QDict success value.  The QMP core
handles the error, and ignores the success value, leaking it.  Exposed
by qmp-cmd-test case /x86_64/qmp/object-add-without-props, and duly
reported both by ASan and valgrind.

To plug the leak, set the success value only on success.

Fixes: 5f07c4d60d091320186e7b0edaf9ed2cc16b2d1e
Cc: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 qom/qom-qmp-cmds.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/qom/qom-qmp-cmds.c b/qom/qom-qmp-cmds.c
index 435193b036..6bd137ccbf 100644
--- a/qom/qom-qmp-cmds.c
+++ b/qom/qom-qmp-cmds.c
@@ -287,8 +287,8 @@ void qmp_object_add(QDict *qdict, QObject **ret_data, Error **errp)
     visit_free(v);
     if (obj) {
         object_unref(obj);
+        *ret_data = QOBJECT(qdict_new());
     }
-    *ret_data = QOBJECT(qdict_new());
 }
 
 void qmp_object_del(const char *id, Error **errp)
-- 
2.21.1



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] qom-qmp-cmds: Fix another memory leak in qmp_object_add()
  2020-03-17  9:22 [PATCH] qom-qmp-cmds: Fix another memory leak in qmp_object_add() Markus Armbruster
@ 2020-03-17  9:32 ` Marc-André Lureau
  2020-03-17  9:54 ` Daniel P. Berrangé
  2020-03-17 10:15 ` Chenqun (kuhn)
  2 siblings, 0 replies; 5+ messages in thread
From: Marc-André Lureau @ 2020-03-17  9:32 UTC (permalink / raw)
  To: Markus Armbruster
  Cc: Kevin Wolf, Paolo Bonzini, Daniel P. Berrange, QEMU,
	Eduardo Habkost

On Tue, Mar 17, 2020 at 10:23 AM Markus Armbruster <armbru@redhat.com> wrote:
>
> When user_creatable_add_type() fails, qmp_object_add() returns both
> its error and the usual empty QDict success value.  The QMP core
> handles the error, and ignores the success value, leaking it.  Exposed
> by qmp-cmd-test case /x86_64/qmp/object-add-without-props, and duly
> reported both by ASan and valgrind.
>
> To plug the leak, set the success value only on success.
>
> Fixes: 5f07c4d60d091320186e7b0edaf9ed2cc16b2d1e
> Cc: Kevin Wolf <kwolf@redhat.com>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>


> ---
>  qom/qom-qmp-cmds.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/qom/qom-qmp-cmds.c b/qom/qom-qmp-cmds.c
> index 435193b036..6bd137ccbf 100644
> --- a/qom/qom-qmp-cmds.c
> +++ b/qom/qom-qmp-cmds.c
> @@ -287,8 +287,8 @@ void qmp_object_add(QDict *qdict, QObject **ret_data, Error **errp)
>      visit_free(v);
>      if (obj) {
>          object_unref(obj);
> +        *ret_data = QOBJECT(qdict_new());
>      }
> -    *ret_data = QOBJECT(qdict_new());
>  }
>
>  void qmp_object_del(const char *id, Error **errp)
> --
> 2.21.1
>
>


-- 
Marc-André Lureau


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] qom-qmp-cmds: Fix another memory leak in qmp_object_add()
  2020-03-17  9:22 [PATCH] qom-qmp-cmds: Fix another memory leak in qmp_object_add() Markus Armbruster
  2020-03-17  9:32 ` Marc-André Lureau
@ 2020-03-17  9:54 ` Daniel P. Berrangé
  2020-03-17 10:15 ` Chenqun (kuhn)
  2 siblings, 0 replies; 5+ messages in thread
From: Daniel P. Berrangé @ 2020-03-17  9:54 UTC (permalink / raw)
  To: Markus Armbruster; +Cc: Kevin Wolf, pbonzini, qemu-devel, ehabkost

On Tue, Mar 17, 2020 at 10:22:41AM +0100, Markus Armbruster wrote:
> When user_creatable_add_type() fails, qmp_object_add() returns both
> its error and the usual empty QDict success value.  The QMP core
> handles the error, and ignores the success value, leaking it.  Exposed
> by qmp-cmd-test case /x86_64/qmp/object-add-without-props, and duly
> reported both by ASan and valgrind.
> 
> To plug the leak, set the success value only on success.
> 
> Fixes: 5f07c4d60d091320186e7b0edaf9ed2cc16b2d1e
> Cc: Kevin Wolf <kwolf@redhat.com>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  qom/qom-qmp-cmds.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>

> 
> diff --git a/qom/qom-qmp-cmds.c b/qom/qom-qmp-cmds.c
> index 435193b036..6bd137ccbf 100644
> --- a/qom/qom-qmp-cmds.c
> +++ b/qom/qom-qmp-cmds.c
> @@ -287,8 +287,8 @@ void qmp_object_add(QDict *qdict, QObject **ret_data, Error **errp)
>      visit_free(v);
>      if (obj) {
>          object_unref(obj);
> +        *ret_data = QOBJECT(qdict_new());
>      }
> -    *ret_data = QOBJECT(qdict_new());
>  }
>  
>  void qmp_object_del(const char *id, Error **errp)
> -- 
> 2.21.1
> 
> 

Regards,
Daniel
-- 
|: 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 :|



^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [PATCH] qom-qmp-cmds: Fix another memory leak in qmp_object_add()
  2020-03-17  9:22 [PATCH] qom-qmp-cmds: Fix another memory leak in qmp_object_add() Markus Armbruster
  2020-03-17  9:32 ` Marc-André Lureau
  2020-03-17  9:54 ` Daniel P. Berrangé
@ 2020-03-17 10:15 ` Chenqun (kuhn)
  2020-03-17 10:47   ` Markus Armbruster
  2 siblings, 1 reply; 5+ messages in thread
From: Chenqun (kuhn) @ 2020-03-17 10:15 UTC (permalink / raw)
  To: Markus Armbruster, qemu-devel@nongnu.org
  Cc: Kevin Wolf, pbonzini@redhat.com, berrange@redhat.com,
	ehabkost@redhat.com, Pannengyuan

>-----Original Message-----
>From: Qemu-devel [mailto:qemu-devel-
>bounces+kuhn.chenqun=huawei.com@nongnu.org] On Behalf Of Markus
>Armbruster
>Sent: Tuesday, March 17, 2020 5:23 PM
>To: qemu-devel@nongnu.org
>Cc: Kevin Wolf <kwolf@redhat.com>; pbonzini@redhat.com;
>berrange@redhat.com; ehabkost@redhat.com
>Subject: [PATCH] qom-qmp-cmds: Fix another memory leak in
>qmp_object_add()
>
>When user_creatable_add_type() fails, qmp_object_add() returns both its
>error and the usual empty QDict success value.  The QMP core handles the
>error, and ignores the success value, leaking it.  Exposed by qmp-cmd-test
>case /x86_64/qmp/object-add-without-props, and duly reported both by
>ASan and valgrind.
>
>To plug the leak, set the success value only on success.
>
>Fixes: 5f07c4d60d091320186e7b0edaf9ed2cc16b2d1e
>Cc: Kevin Wolf <kwolf@redhat.com>
>Signed-off-by: Markus Armbruster <armbru@redhat.com>
>---
Hi,  Markus

        Looks like the same patch that has been reported already here:
https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg03928.html

Maybe we should initialize ret_data in xen-block to avoid a possible uninitialized error ?

Thanks.
> qom/qom-qmp-cmds.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
>diff --git a/qom/qom-qmp-cmds.c b/qom/qom-qmp-cmds.c index
>435193b036..6bd137ccbf 100644
>--- a/qom/qom-qmp-cmds.c
>+++ b/qom/qom-qmp-cmds.c
>@@ -287,8 +287,8 @@ void qmp_object_add(QDict *qdict, QObject
>**ret_data, Error **errp)
>     visit_free(v);
>     if (obj) {
>         object_unref(obj);
>+        *ret_data = QOBJECT(qdict_new());
>     }
>-    *ret_data = QOBJECT(qdict_new());
> }
>
> void qmp_object_del(const char *id, Error **errp)
>--
>2.21.1
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] qom-qmp-cmds: Fix another memory leak in qmp_object_add()
  2020-03-17 10:15 ` Chenqun (kuhn)
@ 2020-03-17 10:47   ` Markus Armbruster
  0 siblings, 0 replies; 5+ messages in thread
From: Markus Armbruster @ 2020-03-17 10:47 UTC (permalink / raw)
  To: Chenqun (kuhn)
  Cc: Kevin Wolf, berrange@redhat.com, ehabkost@redhat.com, Pannengyuan,
	qemu-devel@nongnu.org, pbonzini@redhat.com

"Chenqun (kuhn)" <kuhn.chenqun@huawei.com> writes:

>>-----Original Message-----
>>From: Qemu-devel [mailto:qemu-devel-
>>bounces+kuhn.chenqun=huawei.com@nongnu.org] On Behalf Of Markus
>>Armbruster
>>Sent: Tuesday, March 17, 2020 5:23 PM
>>To: qemu-devel@nongnu.org
>>Cc: Kevin Wolf <kwolf@redhat.com>; pbonzini@redhat.com;
>>berrange@redhat.com; ehabkost@redhat.com
>>Subject: [PATCH] qom-qmp-cmds: Fix another memory leak in
>>qmp_object_add()
>>
>>When user_creatable_add_type() fails, qmp_object_add() returns both its
>>error and the usual empty QDict success value.  The QMP core handles the
>>error, and ignores the success value, leaking it.  Exposed by qmp-cmd-test
>>case /x86_64/qmp/object-add-without-props, and duly reported both by
>>ASan and valgrind.
>>
>>To plug the leak, set the success value only on success.
>>
>>Fixes: 5f07c4d60d091320186e7b0edaf9ed2cc16b2d1e
>>Cc: Kevin Wolf <kwolf@redhat.com>
>>Signed-off-by: Markus Armbruster <armbru@redhat.com>
>>---
> Hi,  Markus
>
>         Looks like the same patch that has been reported already here:
> https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg03928.html

That patch has an additional hunk:

  diff --git a/hw/block/xen-block.c b/hw/block/xen-block.c
  index 3885464513..041866b846 100644
  --- a/hw/block/xen-block.c
  +++ b/hw/block/xen-block.c
  @@ -860,7 +860,7 @@ static XenBlockIOThread *xen_block_iothread_create(const 
  char *id,
       XenBlockIOThread *iothread = g_new(XenBlockIOThread, 1);
       Error *local_err = NULL;
       QDict *opts;
  -    QObject *ret_data;
  +    QObject *ret_data = NULL;

       iothread->id = g_strdup(id);

       opts = qdict_new();
       qdict_put_str(opts, "qom-type", TYPE_IOTHREAD);
       qdict_put_str(opts, "id", id);
       qmp_object_add(opts, &ret_data, &local_err);
       qobject_unref(opts);
       qobject_unref(ret_data);

       if (local_err) {
           error_propagate(errp, local_err);

           g_free(iothread->id);
           g_free(iothread);
           return NULL;
       }

       return iothread;

> Maybe we should initialize ret_data in xen-block to avoid a possible uninitialized error ?

Yes, because xen_block_iothread_create() passes @ret_data to
qobject_unref() even after qmp_object_add() failed.

In short, Pan Nengyuan's patch is more complete.



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-03-17 10:52 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-03-17  9:22 [PATCH] qom-qmp-cmds: Fix another memory leak in qmp_object_add() Markus Armbruster
2020-03-17  9:32 ` Marc-André Lureau
2020-03-17  9:54 ` Daniel P. Berrangé
2020-03-17 10:15 ` Chenqun (kuhn)
2020-03-17 10:47   ` Markus Armbruster

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).