public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: cygwin-patches@cygwin.com
Subject: [PATCH] Cygwin: pty: Fix race issue between closing and opening master.
Date: Fri, 14 Jan 2022 23:07:36 +0900	[thread overview]
Message-ID: <20220114140736.984-1-takashi.yano@nifty.ne.jp> (raw)

- If the from_master is closed before cleaning up other pipes, such
  as from_slave_nat, the same pty may be allocated and pty master may
  try to open the pipe which is not closed yet, and it will fail.
  This patch fixes the issue.
---
 winsup/cygwin/fhandler_tty.cc | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 1ae4edd63..7bef6958c 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -2146,8 +2146,6 @@ fhandler_pty_master::close ()
 
   if (!ForceCloseHandle (from_master_nat))
     termios_printf ("error closing from_master_nat %p, %E", from_master_nat);
-  if (!ForceCloseHandle (from_master))
-    termios_printf ("error closing from_master %p, %E", from_master);
   if (!ForceCloseHandle (to_master_nat))
     termios_printf ("error closing to_master_nat %p, %E", to_master_nat);
   from_master_nat = to_master_nat = NULL;
@@ -2156,7 +2154,7 @@ fhandler_pty_master::close ()
   from_slave_nat = NULL;
   if (!ForceCloseHandle (to_master))
     termios_printf ("error closing to_master %p, %E", to_master);
-  to_master = from_master = NULL;
+  to_master = NULL;
   ForceCloseHandle (echo_r);
   ForceCloseHandle (echo_w);
   echo_r = echo_w = NULL;
@@ -2171,6 +2169,12 @@ fhandler_pty_master::close ()
     termios_printf ("CloseHandle (input_available_event<%p>), %E",
 		    input_available_event);
 
+  /* The from_master must be closed last so that the same pty is not
+     allocated before cleaning up the other corresponding instances. */
+  if (!ForceCloseHandle (from_master))
+    termios_printf ("error closing from_master %p, %E", from_master);
+  from_master = NULL;
+
   return 0;
 }
 
@@ -3069,12 +3073,14 @@ err:
   close_maybe (output_mutex);
   close_maybe (input_mutex);
   close_maybe (from_master_nat);
-  close_maybe (from_master);
   close_maybe (to_master_nat);
   close_maybe (to_master);
   close_maybe (echo_r);
   close_maybe (echo_w);
   close_maybe (master_ctl);
+  /* The from_master must be closed last so that the same pty is not
+     allocated before cleaning up the other corresponding instances. */
+  close_maybe (from_master);
   termios_printf ("pty%d open failed - failed to create %s", unit, errstr);
   return false;
 }
-- 
2.34.1


                 reply	other threads:[~2022-01-14 14:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220114140736.984-1-takashi.yano@nifty.ne.jp \
    --to=takashi.yano@nifty.ne.jp \
    --cc=cygwin-patches@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).