From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id 4B86F3836416; Fri, 14 Jan 2022 14:15:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4B86F3836416 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Takashi Yano To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/cygwin-3_3-branch] Cygwin: pty: Fix race issue between closing and opening master. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/cygwin-3_3-branch X-Git-Oldrev: 441ca95fef3516d70b2a8339f72b645e76683174 X-Git-Newrev: 31db9f401955b439cf0013e7a0c8bc1334a42efd Message-Id: <20220114141537.4B86F3836416@sourceware.org> Date: Fri, 14 Jan 2022 14:15:37 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 14 Jan 2022 14:15:37 -0000 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=31db9f401955b439cf0013e7a0c8bc1334a42efd commit 31db9f401955b439cf0013e7a0c8bc1334a42efd Author: Takashi Yano Date: Fri Jan 14 19:35:12 2022 +0900 Cygwin: pty: Fix race issue between closing and opening master. - 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. Diff: --- 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; }