Blame SOURCES/012-notify-crash.patch

33afe3
From ed8b2c86ab77aaa3d7fd688c049ad5e1b922a9c6 Mon Sep 17 00:00:00 2001
33afe3
From: Reid Wahl <nrwahl@protonmail.com>
33afe3
Date: Thu, 13 Jan 2022 02:56:55 -0800
33afe3
Subject: [PATCH] Fix: liblrmd: Avoid double-free during notify operation
33afe3
33afe3
This commit fixes a regression introduced by 31c7fa8a, causing a
33afe3
double-free in notify operations. lrmd_dispatch_internal() assigns the
33afe3
exit_reason string directly from an XML node to a new lrmd_event_data_t
33afe3
object (without duplicating), and this string gets freed twice.
33afe3
33afe3
Free #1: pcmk__create_history_xml() (reached via callback) calls
33afe3
lrmd__set_result(), which frees event.exit_reason and sets it to NULL.
33afe3
Free #2: lrmd_ipc_dispatch() frees the XML node, which contains a
33afe3
pointer to the exit_reason string just freed, after
33afe3
lrmd_dispatch_internal() returns.
33afe3
33afe3
Prior to 31c7fa8a, pcmk__create_history_xml reset event.rc and
33afe3
event.op_status but **not** event.exit_reason.
33afe3
33afe3
In this commit we simply make a copy of event.exit_reason in
33afe3
lrmd_dispatch_internal() before the callback. This way we don't have to
33afe3
worry about whatever happens in the callback, and we can continue to
33afe3
unset the exit_reason alongside the rc and op_status. The added overhead
33afe3
should be minimal.
33afe3
33afe3
This commit also makes a copy of output. That's not strictly necessary
33afe3
but adds some futureproofing and allows us to call lrmd__reset_result()
33afe3
at the end of lrmd_dispatch_internal().
33afe3
33afe3
Resolves: RHBZ#2039675
33afe3
33afe3
Signed-off-by: Reid Wahl <nrwahl@protonmail.com>
33afe3
---
33afe3
 lib/lrmd/lrmd_client.c | 8 +++++---
33afe3
 1 file changed, 5 insertions(+), 3 deletions(-)
33afe3
33afe3
diff --git a/lib/lrmd/lrmd_client.c b/lib/lrmd/lrmd_client.c
33afe3
index ee31bb5ae9..5131a648b7 100644
33afe3
--- a/lib/lrmd/lrmd_client.c
33afe3
+++ b/lib/lrmd/lrmd_client.c
33afe3
@@ -305,9 +305,10 @@ lrmd_dispatch_internal(lrmd_t * lrmd, xmlNode * msg)
33afe3
         event.user_data = crm_element_value(msg, F_LRMD_RSC_USERDATA_STR);
33afe3
         event.type = lrmd_event_exec_complete;
33afe3
 
33afe3
-        // No need to duplicate the memory, so don't use setter functions
33afe3
-        event.output = crm_element_value(msg, F_LRMD_RSC_OUTPUT);
33afe3
-        event.exit_reason = crm_element_value(msg, F_LRMD_RSC_EXIT_REASON);
33afe3
+        /* output and exit_reason may be freed by a callback */
33afe3
+        event.output = crm_element_value_copy(msg, F_LRMD_RSC_OUTPUT);
33afe3
+        lrmd__set_result(&event, event.rc, event.op_status,
33afe3
+                         crm_element_value(msg, F_LRMD_RSC_EXIT_REASON));
33afe3
 
33afe3
         event.params = xml2list(msg);
33afe3
     } else if (pcmk__str_eq(type, LRMD_OP_NEW_CLIENT, pcmk__str_none)) {
33afe3
@@ -324,6 +325,7 @@ lrmd_dispatch_internal(lrmd_t * lrmd, xmlNode * msg)
33afe3
     if (event.params) {
33afe3
         g_hash_table_destroy(event.params);
33afe3
     }
33afe3
+    lrmd__reset_result(&event);
33afe3
 }
33afe3
 
33afe3
 // \return Always 0, to indicate that IPC mainloop source should be kept
33afe3
-- 
33afe3
2.27.0
33afe3