lists.arthurdejong.org
RSS feed

[PATCH v2 2/2] Do not pass invalid file descriptor to FD_ISSET()

[Date Prev][Date Next] [Thread Prev][Thread Next]

[PATCH v2 2/2] Do not pass invalid file descriptor to FD_ISSET()



Currently there is a race condition between the main thread and the
workers threads. The main thread sets nslcd_serversocket to -1 without
ensuring that all worker threads are stopped, giving them the window of
opportunity to pass the now invalid fd to FD_ISSET(). This results in
SIGBUS on musl libc.

Closing the file descriptor is enough. I've also dropped close() in
exithandler() to prevent misleading logs. The OS will close the socket
anyway.
---
 nslcd/nslcd.c | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/nslcd/nslcd.c b/nslcd/nslcd.c
index 2f52e01..4a212b0 100644
--- a/nslcd/nslcd.c
+++ b/nslcd/nslcd.c
@@ -221,13 +221,6 @@ static void sig_handler(int signum)
 /* do some cleaning up before terminating */
 static void exithandler(void)
 {
-  /* close socket if it's still in use */
-  if (nslcd_serversocket >= 0)
-  {
-    if (close(nslcd_serversocket))
-      log_log(LOG_WARNING, "problem closing server socket (ignored): %s",
-              strerror(errno));
-  }
   /* remove existing named socket */
   if (unlink(NSLCD_SOCKET) < 0)
   {
@@ -918,7 +911,6 @@ int main(int argc, char *argv[])
               i, strerror(errno));
   /* close server socket to trigger failures in threads waiting on accept() */
   close(nslcd_serversocket);
-  nslcd_serversocket = -1;
   /* if we can, wait a few seconds for the threads to finish */
 #ifdef HAVE_PTHREAD_TIMEDJOIN_NP
   ts.tv_sec = time(NULL) + 3;
-- 
2.45.2