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 4/4] Cygwin: console: Fix potential deadlock regarding acuqiring mutex.
Date: Thu, 13 Jan 2022 21:28:11 +0900	[thread overview]
Message-ID: <20220113122811.241-5-takashi.yano@nifty.ne.jp> (raw)
In-Reply-To: <20220113122811.241-1-takashi.yano@nifty.ne.jp>

- Acquiring input_mutex and attach_mutex in console code has potential
  risk of deadlock. This patch fixes the issue.
---
 winsup/cygwin/fhandler.h | 2 ++
 winsup/cygwin/select.cc  | 7 ++++---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index 0cea1b7f3..fb4747608 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2208,9 +2208,11 @@ private:
   void acquire_input_mutex_if_necessary (DWORD ms)
   {
     acquire_input_mutex (ms);
+    acquire_attach_mutex (ms);
   }
   void release_input_mutex_if_necessary (void)
   {
+    release_attach_mutex ();
     release_input_mutex ();
   }
 
diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index 5b8fc0f81..d01a319ef 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -1122,29 +1122,30 @@ peek_console (select_record *me, bool)
   HANDLE h;
   set_handle_or_return_if_not_open (h, me);
 
+  fh->acquire_input_mutex (mutex_timeout);
   acquire_attach_mutex (mutex_timeout);
   while (!fh->input_ready && !fh->get_cons_readahead_valid ())
     {
       if (fh->bg_check (SIGTTIN, true) <= bg_eof)
 	{
 	  release_attach_mutex ();
+	  fh->release_input_mutex ();
 	  return me->read_ready = true;
 	}
       else if (!PeekConsoleInputW (h, &irec, 1, &events_read) || !events_read)
 	break;
-      fh->acquire_input_mutex (mutex_timeout);
       if (fhandler_console::input_winch == fh->process_input_message ()
 	  && global_sigs[SIGWINCH].sa_handler != SIG_IGN
 	  && global_sigs[SIGWINCH].sa_handler != SIG_DFL)
 	{
 	  set_sig_errno (EINTR);
-	  fh->release_input_mutex ();
 	  release_attach_mutex ();
+	  fh->release_input_mutex ();
 	  return -1;
 	}
-      fh->release_input_mutex ();
     }
   release_attach_mutex ();
+  fh->release_input_mutex ();
   if (fh->input_ready || fh->get_cons_readahead_valid ())
     return me->read_ready = true;
 
-- 
2.34.1


  parent reply	other threads:[~2022-01-13 12:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-13 12:28 [PATCH 0/4] Some fixes for console and pty Takashi Yano
2022-01-13 12:28 ` [PATCH 1/4] Cygwin: pty, console: Fix deadlock in GDB regarding mutex Takashi Yano
2022-01-13 12:28 ` [PATCH 2/4] Cygwin: pty: Fix memory leak in master_fwd_thread Takashi Yano
2022-01-13 12:28 ` [PATCH 3/4] Cygwin: pty: Stop closing and recreating attach_mutex Takashi Yano
2022-01-13 12:28 ` Takashi Yano [this message]
2022-01-14  9:15 ` [PATCH 0/4] Some fixes for console and pty Corinna Vinschen

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=20220113122811.241-5-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).