mcstrans: defer SIGTERM cleanup to the main loop
Stephen Smalley
maintainerPATCH 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
@@ -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
Stephen Smalley
maintainerPATCH 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
@@ -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
Stephen Smalley
maintainerQuoted 7 lines · Stephen Smalley
sigterm_handler()callscleanup_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 insidefinish_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 SIGHUPrestart_daemonpattern.poll()is not restarted (SA_RESTARTis 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.Signed-off-by Stephen Smalley <[email protected]>
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@@ -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