From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id BCE2B3858286; Mon, 4 Mar 2024 11:01:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BCE2B3858286 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1709550085; bh=ZmkDo+Ghn/4XfzE/6d3xjUeJxaH3iOvvN7BC50PoXLY=; h=From:To:Subject:Date:From; b=ycqXmHwg31pxH3mq2MW3XzzSphsWcWjjUUie+Hn4NE/qCM2WUnimDJ3grxQjG414W r5fW+PFJCySLr/9N5/9RC6uRhkAm8G4CQtIy7YU/NBtFTQCrTmiYNiW9H5HrVDQhQA FkoyRXIzUtHKlv/r5WFKgbzDHod9rolCaGENjnSU= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Takashi Yano To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/cygwin-3_5-branch] Cygwin: console: Fix a race issue between close() and open(). X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/cygwin-3_5-branch X-Git-Oldrev: cf121e088f928c68a8e3c799db97478c89b6dd96 X-Git-Newrev: fc5e9525453fea7c27b0e13635ae54abaa0db69d Message-Id: <20240304110125.BCE2B3858286@sourceware.org> Date: Mon, 4 Mar 2024 11:01:25 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Dfc5e9525453= fea7c27b0e13635ae54abaa0db69d commit fc5e9525453fea7c27b0e13635ae54abaa0db69d Author: Takashi Yano Date: Mon Mar 4 11:32:34 2024 +0900 Cygwin: console: Fix a race issue between close() and open(). =20 The open() call for console sometimes fails if the console owner process is closing the console by close() at the same time. This is due to mismatch state of con.owner variable and attaching state to the console. With this patch, checking con.owner and attaching to con.owner sequence in open(), and resetting con.owner and freeing console sequence in close() are guarded by output_mutex to avoid such a race issue. Addresses: https://cygwin.com/pipermail/cygwin/2024-March/255575.html =20 Fixes: 3721a756b0d8 ("Cygwin: console: Make the console accessible from= other terminals.") Reported-by: Kate Deplaix Signed-off-by: Takashi Yano Diff: --- winsup/cygwin/fhandler/console.cc | 58 +++++++++++++++++++++++------------= ---- winsup/cygwin/release/3.5.2 | 4 +++ 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/con= sole.cc index 67ea95466..1c0d5c815 100644 --- a/winsup/cygwin/fhandler/console.cc +++ b/winsup/cygwin/fhandler/console.cc @@ -687,14 +687,6 @@ fhandler_console::set_unit () { devset =3D (fh_devices) shared_console_info[unit]->tty_min_state.get= ntty (); _tc =3D &(shared_console_info[unit]->tty_min_state); - if (!created) - { - while (con.owner > MAX_PID) - Sleep (1); - pinfo p (con.owner); - if (!p) - con.owner =3D myself->pid; - } } =20 dev ().parse (devset); @@ -1744,11 +1736,23 @@ fhandler_console::open (int flags, mode_t) set_handle (NULL); set_output_handle (NULL); =20 + setup_io_mutex (); + acquire_output_mutex (mutex_timeout); + + do + { + pinfo p (con.owner); + if (!p) + con.owner =3D myself->pid; + } + while (false); + /* Open the input handle as handle_ */ bool err =3D false; DWORD resume_pid =3D attach_console (con.owner, &err); if (err) { + release_output_mutex (); set_errno (EACCES); return 0; } @@ -1759,6 +1763,7 @@ fhandler_console::open (int flags, mode_t) =20 if (h =3D=3D INVALID_HANDLE_VALUE) { + release_output_mutex (); __seterrno (); return 0; } @@ -1768,6 +1773,7 @@ fhandler_console::open (int flags, mode_t) resume_pid =3D attach_console (con.owner, &err); if (err) { + release_output_mutex (); set_errno (EACCES); return 0; } @@ -1778,14 +1784,16 @@ fhandler_console::open (int flags, mode_t) =20 if (h =3D=3D INVALID_HANDLE_VALUE) { + release_output_mutex (); __seterrno (); return 0; } set_output_handle (h); handle_set.output_handle =3D h; + release_output_mutex (); + wpbuf.init (); =20 - setup_io_mutex (); handle_set.input_mutex =3D input_mutex; handle_set.output_mutex =3D output_mutex; =20 @@ -1895,25 +1903,20 @@ fhandler_console::close () } } =20 - release_output_mutex (); - - if (shared_console_info[unit] && con.owner =3D=3D myself->pid - && master_thread_started) + if (shared_console_info[unit] && con.owner =3D=3D myself->pid) { - char name[MAX_PATH]; - shared_name (name, CONS_THREAD_SYNC, get_minor ()); - thread_sync_event =3D OpenEvent (MAXIMUM_ALLOWED, FALSE, name); - con.owner =3D MAX_PID + 1; - WaitForSingleObject (thread_sync_event, INFINITE); - CloseHandle (thread_sync_event); + if (master_thread_started) + { + char name[MAX_PATH]; + shared_name (name, CONS_THREAD_SYNC, get_minor ()); + thread_sync_event =3D OpenEvent (MAXIMUM_ALLOWED, FALSE, name); + con.owner =3D MAX_PID + 1; + WaitForSingleObject (thread_sync_event, INFINITE); + CloseHandle (thread_sync_event); + } con.owner =3D 0; } =20 - CloseHandle (input_mutex); - input_mutex =3D NULL; - CloseHandle (output_mutex); - output_mutex =3D NULL; - CloseHandle (get_handle ()); CloseHandle (get_output_handle ()); =20 @@ -1926,6 +1929,13 @@ fhandler_console::close () || get_device () =3D=3D (dev_t) myself->ctty)) free_console (); =20 + release_output_mutex (); + + CloseHandle (input_mutex); + input_mutex =3D NULL; + CloseHandle (output_mutex); + output_mutex =3D NULL; + if (shared_console_info[unit] && myself->ctty !=3D tc ()->ntty) { UnmapViewOfFile ((void *) shared_console_info[unit]); diff --git a/winsup/cygwin/release/3.5.2 b/winsup/cygwin/release/3.5.2 index 7d8df9489..fd3c768de 100644 --- a/winsup/cygwin/release/3.5.2 +++ b/winsup/cygwin/release/3.5.2 @@ -5,3 +5,7 @@ Fixes: is already unmapped due to race condition. To avoid this issue, shared console memory will be kept mapped if it belongs to CTTY. Addresses: https://cygwin.com/pipermail/cygwin/2024-February/255561.html + +- Fix a race issue between console open() and close() which is caused + by state mismatch between con.owner and console attaching state. + Addresses: https://cygwin.com/pipermail/cygwin/2024-March/255575.html