commit a18c2f7: [Minor] Plug memory leak for sigaltstack

Vsevolod Stakhov vsevolod at rspamd.com
Sat Oct 29 09:49:05 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-10-29 10:42:30 +0100
URL: https://github.com/rspamd/rspamd/commit/a18c2f7c4b3628b33b7fde7508f4bd1763a9cde4 (HEAD -> master)

[Minor] Plug memory leak for sigaltstack

---
 src/controller.c            |  1 +
 src/fuzzy_storage.c         |  1 +
 src/hs_helper.c             |  1 +
 src/libserver/worker_util.c | 28 ++++++++++++++++++++++------
 src/libserver/worker_util.h |  5 +++++
 src/rspamd.c                |  1 +
 src/rspamd_proxy.c          |  1 +
 src/worker.c                |  1 +
 8 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/src/controller.c b/src/controller.c
index 0ff7d64c0..7250291f0 100644
--- a/src/controller.c
+++ b/src/controller.c
@@ -4294,6 +4294,7 @@ start_controller_worker (struct rspamd_worker *worker)
 
 	REF_RELEASE (ctx->cfg);
 	rspamd_log_close (worker->srv->logger);
+	rspamd_unset_crash_handler (worker->srv);
 
 	exit (EXIT_SUCCESS);
 }
diff --git a/src/fuzzy_storage.c b/src/fuzzy_storage.c
index 8dc27e4b4..bdbc2b08d 100644
--- a/src/fuzzy_storage.c
+++ b/src/fuzzy_storage.c
@@ -2752,6 +2752,7 @@ start_fuzzy (struct rspamd_worker *worker)
 
 	REF_RELEASE (ctx->cfg);
 	rspamd_log_close (worker->srv->logger);
+	rspamd_unset_crash_handler (worker->srv);
 
 	exit (EXIT_SUCCESS);
 }
diff --git a/src/hs_helper.c b/src/hs_helper.c
index e57859696..5995ce5e9 100644
--- a/src/hs_helper.c
+++ b/src/hs_helper.c
@@ -415,6 +415,7 @@ start_hs_helper (struct rspamd_worker *worker)
 
 	rspamd_log_close (worker->srv->logger);
 	REF_RELEASE (ctx->cfg);
+	rspamd_unset_crash_handler (worker->srv);
 
 	exit (EXIT_SUCCESS);
 }
diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c
index 4529e4ee0..92ae46bf0 100644
--- a/src/libserver/worker_util.c
+++ b/src/libserver/worker_util.c
@@ -1664,12 +1664,6 @@ rspamd_set_crash_handler (struct rspamd_main *rspamd_main)
 	stack_t ss;
 	memset (&ss, 0, sizeof ss);
 
-	/*
-	 * Allocate special stack, NOT freed at the end so far
-	 * It also cannot be on stack as this memory is used when
-	 * stack corruption is detected. Leak sanitizer blames about it but
-	 * I don't know any good ways to stop this behaviour.
-	 */
 	ss.ss_size = MAX (SIGSTKSZ, 8192 * 4);
 	stack_mem = g_malloc0 (ss.ss_size);
 	ss.ss_sp = stack_mem;
@@ -1687,6 +1681,28 @@ rspamd_set_crash_handler (struct rspamd_main *rspamd_main)
 #endif
 }
 
+RSPAMD_NO_SANITIZE void rspamd_unset_crash_handler (struct rspamd_main *unused_)
+{
+#ifdef HAVE_SIGALTSTACK
+	int ret;
+	stack_t ss;
+	ret = sigaltstack (NULL, &ss);
+
+	if (ret != -1) {
+		if (ss.ss_size > 0 && ss.ss_sp) {
+			g_free(ss.ss_sp);
+		}
+
+		ss.ss_size = 0;
+		ss.ss_sp = NULL;
+#ifdef SS_DISABLE
+		ss.ss_flags |= SS_DISABLE;
+#endif
+		sigaltstack(&ss, NULL);
+	}
+#endif
+}
+
 static void
 rspamd_enable_accept_event (EV_P_ ev_timer *w, int revents)
 {
diff --git a/src/libserver/worker_util.h b/src/libserver/worker_util.h
index c215f9523..38ba9c0d7 100644
--- a/src/libserver/worker_util.h
+++ b/src/libserver/worker_util.h
@@ -230,6 +230,11 @@ struct rspamd_worker *rspamd_fork_worker (struct rspamd_main *,
  */
 RSPAMD_NO_SANITIZE void rspamd_set_crash_handler (struct rspamd_main *);
 
+/**
+ * Restore memory for crash signals
+ */
+RSPAMD_NO_SANITIZE void rspamd_unset_crash_handler (struct rspamd_main *);
+
 /**
  * Initialise the main monitoring worker
  * @param worker
diff --git a/src/rspamd.c b/src/rspamd.c
index d8371de55..d0b382856 100644
--- a/src/rspamd.c
+++ b/src/rspamd.c
@@ -1660,6 +1660,7 @@ main (gint argc, gchar **argv, gchar **env)
 		rspamd_pidfile_close (rspamd_main->pfh);
 	}
 
+	rspamd_unset_crash_handler (rspamd_main);
 	g_free (rspamd_main);
 	ev_unref (event_loop);
 	sqlite3_shutdown ();
diff --git a/src/rspamd_proxy.c b/src/rspamd_proxy.c
index 3bf83b0e4..80c28b975 100644
--- a/src/rspamd_proxy.c
+++ b/src/rspamd_proxy.c
@@ -2461,6 +2461,7 @@ start_rspamd_proxy (struct rspamd_worker *worker)
 
 	REF_RELEASE (ctx->cfg);
 	rspamd_log_close (worker->srv->logger);
+	rspamd_unset_crash_handler (worker->srv);
 
 	exit (EXIT_SUCCESS);
 }
diff --git a/src/worker.c b/src/worker.c
index 26cdc2390..10f7c076c 100644
--- a/src/worker.c
+++ b/src/worker.c
@@ -559,6 +559,7 @@ start_worker (struct rspamd_worker *worker)
 	rspamd_stat_close ();
 	REF_RELEASE (ctx->cfg);
 	rspamd_log_close (worker->srv->logger);
+	rspamd_unset_crash_handler (worker->srv);
 
 	exit (EXIT_SUCCESS);
 }


More information about the Commits mailing list