lightningd: fix bogus memleak report.
What changed, and why it matters
This commit fixes a false alarm in Core Lightning's internal memory-leak checker. When the node asked a sub-process to report its memory usage, the reply handling briefly set aside a connection pointer, making the leak detector wrongly report that connection as leaked. The fix delays the final leak check until after the reply handling is finished, so the temporary pointer state is no longer visible. It is a diagnostic/robustness fix, not a security vulnerability fix, and does not create a way for an attacker to exploit the node.
Treat as a normal bugfix / diagnostic improvement. No urgent security action is required. Operators and developers may notice fewer spurious MEMLEAK log entries after upgrading. If running a node, apply the patch through the usual release process.
Security signals we found
False-positive memory-leak report caused by transient pointer state during subdaemon reply handling
Fix uses deferred timer rather than marking the connection notleak(), explicitly to avoid hiding a real future leak
No input validation, parsing, cryptographic, or authorization changes
No externally reachable attack surface introduced or removed
Evidence from the diff
The memleak RPC in lightningd sends requests to subdaemons and, when all replies are in, calls finish_report() to scan for tal allocations that look leaked. The reply callback (leak_detect_req_done) was invoked while subd->conn was temporarily saved and set to NULL; the subsequent finish_report() then saw that struct io_conn as an unreachable allocation and reported a MEMLEAK. The patch defers finish_report() via a zero-timeout relative timer so the subd reply path completes and subd->conn is restored before the scan runs. It also moves struct leak_detect from the header into the C file and adds an ld pointer so the timer can be created.
Changed components
lightningd/memdump.clightningd/memdump.hlightningd/subd.c (interacting code referenced in log backtrace, not modified)Inspect captured patch +16 / −8
diff --git a/lightningd/memdump.c b/lightningd/memdump.c
index cbd1b31d..6c915707 100644
--- a/lightningd/memdump.c
+++ b/lightningd/memdump.c
@@ -16,6 +16,13 @@
#include <lightningd/memdump.h>
#include <lightningd/subd.h>
+struct leak_detect {
+ struct command *cmd;
+ struct lightningd *ld;
+ size_t num_outstanding_requests;
+ const char **leakers;
+};
+
static void json_add_ptr(struct json_stream *response, const char *name,
const void *ptr)
{
@@ -162,8 +169,13 @@ static void leak_detect_req_done(const struct subd_req *req,
struct leak_detect *leak_detect)
{
leak_detect->num_outstanding_requests--;
- if (leak_detect->num_outstanding_requests == 0)
- finish_report(leak_detect);
+ if (leak_detect->num_outstanding_requests == 0) {
+ /* We do this off a timer: doing it off a subd reply makes us think that the
+ * subd->conn (temporarily set to NULL during the cb) is a leak! */
+ new_reltimer(leak_detect->ld->timers, leak_detect,
+ time_from_sec(0),
+ finish_report, leak_detect);
+ }
}
/* Start a leak request: decrements num_outstanding_requests when freed. */
@@ -230,6 +242,7 @@ static struct command_result *json_memleak(struct command *cmd,
return command_check_done(cmd);
leaks = tal(cmd, struct leak_detect);
+ leaks->ld = cmd->ld;
leaks->cmd = cmd;
leaks->num_outstanding_requests = 0;
leaks->leakers = tal_arr(leaks, const char *, 0);
diff --git a/lightningd/memdump.h b/lightningd/memdump.h
index 8ce39fd2..07a628b7 100644
--- a/lightningd/memdump.h
+++ b/lightningd/memdump.h
@@ -3,15 +3,10 @@
#include "config.h"
struct command;
+struct leak_detect;
struct subd;
struct subd_req;
-struct leak_detect {
- struct command *cmd;
- size_t num_outstanding_requests;
- const char **leakers;
-};
-
/* Start a leak request: decrements num_outstanding_requests when freed. */
void start_leak_request(const struct subd_req *req,
struct leak_detect *leak_detect);
Why this scored 19/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.