Skip to contentSkip to conversation

monitor: fix double-free of request error

PATCH In review Marc-André Lureau 3 participants 5 messages Jul 5, 2018 – Jul 6, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 monitor: fix double-free of request error

qmp_error_response() will free the given error. Fix double-free in later qmp_request_free().

---
 monitor.c | 1 +
 1 file changed, 1 insertion(+)
monitor.c +1 -0
index 3c9c97b73f..7af1f18d13 100644
@@ -4186,6 +4186,7 @@ static void monitor_qmp_bh_dispatcher(void *data)
     } else {
         assert(req_obj->err);
         rsp = qmp_error_response(req_obj->err);
+        req_obj->err = NULL;
         monitor_qmp_respond(req_obj->mon, rsp, NULL);
         qobject_unref(rsp);
     }
Signature · 1 line
2.18.0.rc1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Marc-André Lureau

qmp_error_response() will free the given error. Fix double-free in later qmp_request_free().

And not related to current patch...

Quoted 9 lines
---
 monitor.c | 1 +
 1 file changed, 1 insertion(+)
monitor.c +0 -0
index 3c9c97b73f..7af1f18d13 100644

monitor.c static void monitor_qmp_bh_dispatcher(void *data) · line 4190

Quoted 5 lines
monitor.c +1 -0
@@ -4186,6 +4186,7 @@ static void monitor_qmp_bh_dispatcher(void *data)
     } else {
         assert(req_obj->err);
         rsp = qmp_error_response(req_obj->err);
+        req_obj->err = NULL;
         monitor_qmp_respond(req_obj->mon, rsp, NULL);

... here not sure whether we should just pass in req_obj->id instead of NULL, or maybe we can do some more assertions like:

monitor.c +2 -0
index 9eb9f06599..04d2c50f4e 100644
@@ -4215,10 +4215,12 @@ static void monitor_qmp_bh_dispatcher(void *data)
 
     mon = req_obj->mon;
     if (req_obj->req) {
+        assert(!req_obj->err);
         trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj->id) ?: "");
         monitor_qmp_dispatch(mon, req_obj->req, req_obj->id);
     } else {
         assert(req_obj->err);
+        assert(!req_obj->id);
         rsp = qmp_error_response(req_obj->err);
         monitor_qmp_respond(mon, rsp, NULL);
         qobject_unref(rsp);

Thanks,

Signature · 1 line
Peter Xu
#2 lore raw
Quoted 33 lines · Peter Xu
Quoted 4 lines · Marc-André Lureau

qmp_error_response() will free the given error. Fix double-free in later qmp_request_free().

And not related to current patch...

Quoted 9 lines
---
 monitor.c | 1 +
 1 file changed, 1 insertion(+)
monitor.c +0 -0
index 3c9c97b73f..7af1f18d13 100644

monitor.c static void monitor_qmp_bh_dispatcher(void *data) · line 4190

Quoted 5 lines
monitor.c +1 -0
@@ -4186,6 +4186,7 @@ static void monitor_qmp_bh_dispatcher(void *data)
     } else {
         assert(req_obj->err);
         rsp = qmp_error_response(req_obj->err);
+        req_obj->err = NULL;
         monitor_qmp_respond(req_obj->mon, rsp, NULL);

... here not sure whether we should just pass in req_obj->id instead of NULL, or maybe we can do some more assertions like:

monitor.c +0 -0
index 9eb9f06599..04d2c50f4e 100644

monitor.c static void monitor_qmp_bh_dispatcher(void *data) · line 4218

Quoted 4 lines
monitor.c +1 -0
@@ -4215,10 +4215,12 @@ static void monitor_qmp_bh_dispatcher(void *data)
 
     mon = req_obj->mon;
     if (req_obj->req) {
+        assert(!req_obj->err);

Makes sense.

monitor.c static void monitor_qmp_bh_dispatcher(void *data) · line 4223

Quoted 5 lines
monitor.c +1 -0
         trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj->id) ?: "");
         monitor_qmp_dispatch(mon, req_obj->req, req_obj->id);
     } else {
         assert(req_obj->err);
+        assert(!req_obj->id);

I'd simply pass req_obj->id to monitor_qmp_respond(). Yes, it'll always be null, but the code would do the right thing if that should ever change.

Quoted 5 lines
monitor.c +0 -0
         rsp = qmp_error_response(req_obj->err);
         monitor_qmp_respond(mon, rsp, NULL);
         qobject_unref(rsp);

Thanks,

Perhaps even reorder to put the error case first:

    if (req_obj->err) {
        assert(!req_obj->req);
        rsp = qmp_error_response(req_obj->err);
        req_obj->err = NULL;
        monitor_qmp_respond(req_obj->mon, rsp, req_obj->id);
        qobject_unref(rsp);
    } else if (req_obj->req) {
        trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj->id) ?: "");
        monitor_qmp_dispatch(req_obj->mon, req_obj->req, req_obj->id);
    }

Matter of taste.

#3 lore raw
Quoted 48 lines · Markus Armbruster
Quoted 33 lines · Peter Xu
Quoted 4 lines · Marc-André Lureau

qmp_error_response() will free the given error. Fix double-free in later qmp_request_free().

And not related to current patch...

Quoted 9 lines
---
 monitor.c | 1 +
 1 file changed, 1 insertion(+)
monitor.c +0 -0
index 3c9c97b73f..7af1f18d13 100644

monitor.c static void monitor_qmp_bh_dispatcher(void *data) · line 4190

Quoted 5 lines
monitor.c +1 -0
@@ -4186,6 +4186,7 @@ static void monitor_qmp_bh_dispatcher(void *data)
     } else {
         assert(req_obj->err);
         rsp = qmp_error_response(req_obj->err);
+        req_obj->err = NULL;
         monitor_qmp_respond(req_obj->mon, rsp, NULL);

... here not sure whether we should just pass in req_obj->id instead of NULL, or maybe we can do some more assertions like:

monitor.c +0 -0
index 9eb9f06599..04d2c50f4e 100644

monitor.c static void monitor_qmp_bh_dispatcher(void *data) · line 4218

Quoted 4 lines
monitor.c +1 -0
@@ -4215,10 +4215,12 @@ static void monitor_qmp_bh_dispatcher(void *data)
 
     mon = req_obj->mon;
     if (req_obj->req) {
+        assert(!req_obj->err);

Makes sense.

monitor.c static void monitor_qmp_bh_dispatcher(void *data) · line 4223

Quoted 5 lines
monitor.c +1 -0
         trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj->id) ?: "");
         monitor_qmp_dispatch(mon, req_obj->req, req_obj->id);
     } else {
         assert(req_obj->err);
+        assert(!req_obj->id);
Quoted 3 lines

I'd simply pass req_obj->id to monitor_qmp_respond(). Yes, it'll always be null, but the code would do the right thing if that should ever change.

Agreed.

Quoted 20 lines
Quoted 6 lines
monitor.c +0 -0
         rsp = qmp_error_response(req_obj->err);
         monitor_qmp_respond(mon, rsp, NULL);
         qobject_unref(rsp);

Thanks,

Perhaps even reorder to put the error case first:

    if (req_obj->err) {
        assert(!req_obj->req);
        rsp = qmp_error_response(req_obj->err);
        req_obj->err = NULL;
        monitor_qmp_respond(req_obj->mon, rsp, req_obj->id);
        qobject_unref(rsp);
    } else if (req_obj->req) {
        trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj->id) ?: "");
        monitor_qmp_dispatch(req_obj->mon, req_obj->req, req_obj->id);
    }
Quoted 1 line

Matter of taste.

Looks good to me. Thanks,

Signature · 1 line
Peter Xu