Skip to content

Commit

Permalink
Do not pass invalid file descriptor to FD_ISSET()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
heroin-moose authored and arthurdejong committed Jun 29, 2024
1 parent ed4041c commit b7841fc
Showing 1 changed file with 0 additions and 8 deletions.
8 changes: 0 additions & 8 deletions nslcd/nslcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b7841fc

Please sign in to comment.