Mark McLoughlin 90dddf
From 71de8d92f20a9a9ee76d4d5df77ff477f1b7d441 Mon Sep 17 00:00:00 2001
Mark McLoughlin 90dddf
From: Matthias Bolte <matthias.bolte@googlemail.com>
Mark McLoughlin 90dddf
Date: Wed, 30 Sep 2009 02:17:27 +0200
Mark McLoughlin 90dddf
Subject: [PATCH] Fix memory leaks in libvirtd's message processing
Mark McLoughlin 90dddf
Mark McLoughlin 90dddf
Commit 47cab734995fa9521b1df05d37e9978eedd8d3a2 changed the way how
Mark McLoughlin 90dddf
qemud_client_message objects were reused. Before this commit
Mark McLoughlin 90dddf
remoteDispatchClientRequest() reused the received message for normal responses
Mark McLoughlin 90dddf
and to report non-fatal errors. If a fatal error occurred qemudWorker() frees
Mark McLoughlin 90dddf
the message. After this commit non-fatal errors are reported by
Mark McLoughlin 90dddf
remoteSerializeReplyError() using a new qemud_client_message object and the
Mark McLoughlin 90dddf
original message leaks.
Mark McLoughlin 90dddf
Mark McLoughlin 90dddf
To fix this leak the original message has to be freed if
Mark McLoughlin 90dddf
remoteSerializeReplyError() succeeds. If remoteSerializeReplyError()
Mark McLoughlin 90dddf
fails the original message is freed in qemudWorker().
Mark McLoughlin 90dddf
Mark McLoughlin 90dddf
* daemon/dispatch.c: free qemud_client_message objects that will not be reused
Mark McLoughlin 90dddf
  and would leak otherwise, also free the allocated qemud_client_message object
Mark McLoughlin 90dddf
  in remoteSerializeError() if an error occurs
Mark McLoughlin 90dddf
Mark McLoughlin 90dddf
(cherry-picked from commit c6f1459eb998619ab21a92d9bb87341f26978181)
Mark McLoughlin 90dddf
Mark McLoughlin 90dddf
Fedora-patch: libvirt-fix-libvirtd-leak-in-error-reply.patch
Mark McLoughlin 90dddf
---
Mark McLoughlin 90dddf
 qemud/dispatch.c |   15 +++++++++++++--
Mark McLoughlin 90dddf
 1 files changed, 13 insertions(+), 2 deletions(-)
Mark McLoughlin 90dddf
Mark McLoughlin 90dddf
diff --git a/qemud/dispatch.c b/qemud/dispatch.c
Mark McLoughlin 90dddf
index a60f2f4..ddb3215 100644
Mark McLoughlin 90dddf
--- a/qemud/dispatch.c
Mark McLoughlin 90dddf
+++ b/qemud/dispatch.c
Mark McLoughlin 90dddf
@@ -191,6 +191,7 @@ remoteSerializeError(struct qemud_client *client,
Mark McLoughlin 90dddf
 
Mark McLoughlin 90dddf
 xdr_error:
Mark McLoughlin 90dddf
     xdr_destroy(&xdr;;
Mark McLoughlin 90dddf
+    VIR_FREE(msg);
Mark McLoughlin 90dddf
 fatal_error:
Mark McLoughlin 90dddf
     xdr_free((xdrproc_t)xdr_remote_error,  (char *)rerr);
Mark McLoughlin 90dddf
     return -1;
Mark McLoughlin 90dddf
@@ -336,6 +337,7 @@ remoteDispatchClientRequest (struct qemud_server *server,
Mark McLoughlin 90dddf
                              struct qemud_client *client,
Mark McLoughlin 90dddf
                              struct qemud_client_message *msg)
Mark McLoughlin 90dddf
 {
Mark McLoughlin 90dddf
+    int ret;
Mark McLoughlin 90dddf
     remote_error rerr;
Mark McLoughlin 90dddf
 
Mark McLoughlin 90dddf
     memset(&rerr, 0, sizeof rerr);
Mark McLoughlin 90dddf
@@ -364,7 +366,12 @@ remoteDispatchClientRequest (struct qemud_server *server,
Mark McLoughlin 90dddf
     }
Mark McLoughlin 90dddf
 
Mark McLoughlin 90dddf
 error:
Mark McLoughlin 90dddf
-    return remoteSerializeReplyError(client, &rerr, &msg->hdr);
Mark McLoughlin 90dddf
+    ret = remoteSerializeReplyError(client, &rerr, &msg->hdr);
Mark McLoughlin 90dddf
+
Mark McLoughlin 90dddf
+    if (ret >= 0)
Mark McLoughlin 90dddf
+        VIR_FREE(msg);
Mark McLoughlin 90dddf
+
Mark McLoughlin 90dddf
+    return ret;
Mark McLoughlin 90dddf
 }
Mark McLoughlin 90dddf
 
Mark McLoughlin 90dddf
 
Mark McLoughlin 90dddf
@@ -521,8 +528,12 @@ remoteDispatchClientCall (struct qemud_server *server,
Mark McLoughlin 90dddf
 rpc_error:
Mark McLoughlin 90dddf
     /* Semi-bad stuff happened, we can still try to send back
Mark McLoughlin 90dddf
      * an RPC error message to client */
Mark McLoughlin 90dddf
-    return remoteSerializeReplyError(client, &rerr, &msg->hdr);
Mark McLoughlin 90dddf
+    rv = remoteSerializeReplyError(client, &rerr, &msg->hdr);
Mark McLoughlin 90dddf
+
Mark McLoughlin 90dddf
+    if (rv >= 0)
Mark McLoughlin 90dddf
+        VIR_FREE(msg);
Mark McLoughlin 90dddf
 
Mark McLoughlin 90dddf
+    return rv;
Mark McLoughlin 90dddf
 
Mark McLoughlin 90dddf
 xdr_error:
Mark McLoughlin 90dddf
     /* Seriously bad stuff happened, so we'll kill off this client
Mark McLoughlin 90dddf
-- 
Mark McLoughlin 90dddf
1.6.2.5
Mark McLoughlin 90dddf