Skip to contentSkip to conversation

mcstrans: defer SIGTERM cleanup to the main loop

PATCH In review Stephen Smalley 1 participant 3 messages Jul 16 – Jul 21 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/2 mcstrans: defer SIGTERM cleanup to the main loop

sigterm_handler() calls cleanup_exit(), which walks and frees the translation and color tables and unlinks the socket. None of that is async-signal-safe; a SIGTERM arriving while the main loop is inside malloc/free or inside finish_context_translations() during a SIGHUP reload can deadlock or corrupt the heap.

Set a flag from the handler and let the top of the poll loop call cleanup_exit() from normal context, matching the existing SIGHUP restart_daemon pattern. poll() is not restarted (SA_RESTART is not set) so a signal delivered during the blocking poll returns EINTR and the loop rechecks the flag. The narrow check then poll window matches the existing SIGHUP handling.

---
 mcstrans/src/mcstransd.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
mcstrans/src/mcstransd.c +6 -3
index 5bdaadb7..3db58a17 100644
@@ -58,6 +58,7 @@
 static int sockfd = -1; /* socket we are listening on */
 
 static volatile sig_atomic_t restart_daemon = false;
+static volatile sig_atomic_t terminate = false;
 static void cleanup_exit(int ret) __attribute__((noreturn));
 static void cleanup_exit(int ret)
 {
@@ -407,6 +408,10 @@ static void process_connections(void)
 	ufds[0].revents = 0;
 
 	while (1) {
+		if (terminate) {
+			free(ufds);
+			cleanup_exit(0);
+		}
 		if (restart_daemon) {
 			syslog(LOG_NOTICE, "Reload Translations");
 			finish_context_colors();
@@ -442,11 +447,9 @@ static void process_connections(void)
 	}
 }
 
-static void sigterm_handler(int sig) __attribute__((noreturn));
-
 static void sigterm_handler(int UNUSED(sig))
 {
-	cleanup_exit(0);
+	terminate = true;
 }
 
 static void sighup_handler(int UNUSED(sig))
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 2/2 mcstrans: bound Include nesting depth

process_trans() handles Include directives by calling read_translations() on each matched path, which in turn calls process_trans() line by line. A configuration file that Includes itself (directly or via a cycle) recurses until the daemon runs out of file descriptors or stack. Cap the nesting depth at 32.

---
 mcstrans/src/mcstrans.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mcstrans/src/mcstrans.c +8 -0
index b5c8d54a..e79aa3d5 100644
@@ -911,16 +911,23 @@ static int process_trans(char *buffer)
 
 int read_translations(const char *filename)
 {
+	static unsigned int depth;
 	size_t size = 0;
 	char *buffer = NULL;
 	int rval = 0;
 
+	if (depth >= 32) {
+		syslog(LOG_ERR, "%s: Include nesting too deep", filename);
+		return -1;
+	}
+
 	FILE *cfg = fopen(filename, "r");
 	if (!cfg) {
 		syslog(LOG_ERR, "%s file open failed", filename);
 		return -1;
 	}
 
+	depth++;
 	__fsetlocking(cfg, FSETLOCKING_BYCALLER);
 	while (getline(&buffer, &size, cfg) > 0) {
 		if (process_trans(buffer) < 0) {
@@ -931,6 +938,7 @@ int read_translations(const char *filename)
 	}
 	free(buffer);
 	fclose(cfg);
+	depth--;
 	return rval;
 }
 
Signature · 1 line
2.55.0
author maintainer
#2 lore raw
In reply to Stephen Smalley
Quoted 7 lines · Stephen Smalley

sigterm_handler() calls cleanup_exit(), which walks and frees the translation and color tables and unlinks the socket. None of that is async-signal-safe; a SIGTERM arriving while the main loop is inside malloc/free or inside finish_context_translations() during a SIGHUP reload can deadlock or corrupt the heap.

Quoted 8 lines

Set a flag from the handler and let the top of the poll loop call cleanup_exit() from normal context, matching the existing SIGHUP restart_daemon pattern. poll() is not restarted (SA_RESTART is not set) so a signal delivered during the blocking poll returns EINTR and the loop rechecks the flag. The narrow check then poll window matches the existing SIGHUP handling.

This series has been merged.

Quoted 43 lines
---
 mcstrans/src/mcstransd.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
mcstrans/src/mcstransd.c +6 -4
index 5bdaadb7..3db58a17 100644
@@ -58,6 +58,7 @@
 static int sockfd = -1; /* socket we are listening on */
 
 static volatile sig_atomic_t restart_daemon = false;
+static volatile sig_atomic_t terminate = false;
 static void cleanup_exit(int ret) __attribute__((noreturn));
 static void cleanup_exit(int ret)
 {
@@ -407,6 +408,10 @@ static void process_connections(void)
        ufds[0].revents = 0;
 
        while (1) {
+               if (terminate) {
+                       free(ufds);
+                       cleanup_exit(0);
+               }
                if (restart_daemon) {
                        syslog(LOG_NOTICE, "Reload Translations");
                        finish_context_colors();
@@ -442,11 +447,9 @@ static void process_connections(void)
        }
 }
 
-static void sigterm_handler(int sig) __attribute__((noreturn));
-
 static void sigterm_handler(int UNUSED(sig))
 {
-       cleanup_exit(0);
+       terminate = true;
 }
 
 static void sighup_handler(int UNUSED(sig))
--

2.55.0