public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin/cygwin-3_3-branch] Cygwin: console: Fix potential deadlock regarding acuqiring mutex.
@ 2022-01-14 14:15 Takashi Yano
  0 siblings, 0 replies; only message in thread
From: Takashi Yano @ 2022-01-14 14:15 UTC (permalink / raw)
  To: cygwin-cvs

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=441ca95fef3516d70b2a8339f72b645e76683174

commit 441ca95fef3516d70b2a8339f72b645e76683174
Author: Takashi Yano <takashi.yano@nifty.ne.jp>
Date:   Thu Jan 13 20:57:15 2022 +0900

    Cygwin: console: Fix potential deadlock regarding acuqiring mutex.
    
    - Acquiring input_mutex and attach_mutex in console code has potential
      risk of deadlock. This patch fixes the issue.

Diff:
---
 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 630293ff8..4226bfd69 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2209,9 +2209,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;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-01-14 14:15 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-14 14:15 [newlib-cygwin/cygwin-3_3-branch] Cygwin: console: Fix potential deadlock regarding acuqiring mutex Takashi Yano

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).