Skip to content

Commit

Permalink
Cygwin: console: Fix conflict on shared names between sessions.
Browse files Browse the repository at this point in the history
Previously, shared names in the console were created using get_minor().
However, get_minor() was not unique to the console across sessions.
This is because EnumWindows(), which is used to look for console windows,
cannot enumerate windows across sessions. This causes conflict on the
shared names between sessions (e.g. sessions of different users,
different services, a service and a user session, etc.).

With this patch, GetConsoleWindow() is used instead of get_minor().
GetConsoleWindow() has been used for the name of shared memory, which
should be unique to each console.

Addresses: https://cygwin.com/pipermail/cygwin/2024-April/255893.html
Fixes: ff4440f ("Cygwin: console: Introduce new thread which handles input signal.");
Reported-by: Johannes Khoshnazar-Thoma <[email protected]>
Signed-off-by: Takashi Yano <[email protected]>
  • Loading branch information
tyan0 committed Jul 3, 2024
1 parent a5ffae1 commit f7a77d0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
15 changes: 11 additions & 4 deletions winsup/cygwin/fhandler/console.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ static struct fhandler_base::rabuf_t con_ra;
in xterm compatible mode */
static wchar_t last_char;

static char *
cons_shared_name (char *ret_buf, const char *str, HWND hw)
{
__small_sprintf (ret_buf, "%s.%p", str, hw);
return ret_buf;
}

DWORD
fhandler_console::attach_console (pid_t owner, bool *err)
{
Expand Down Expand Up @@ -922,7 +929,7 @@ fhandler_console::setup_io_mutex (void)
res = WAIT_FAILED;
if (!input_mutex || WAIT_FAILED == (res = acquire_input_mutex (0)))
{
shared_name (buf, "cygcons.input.mutex", get_minor ());
cons_shared_name (buf, "cygcons.input.mutex", GetConsoleWindow ());
input_mutex = OpenMutex (MAXIMUM_ALLOWED, TRUE, buf);
if (!input_mutex)
input_mutex = CreateMutex (&sec_none, FALSE, buf);
Expand All @@ -938,7 +945,7 @@ fhandler_console::setup_io_mutex (void)
res = WAIT_FAILED;
if (!output_mutex || WAIT_FAILED == (res = acquire_output_mutex (0)))
{
shared_name (buf, "cygcons.output.mutex", get_minor ());
cons_shared_name (buf, "cygcons.output.mutex", GetConsoleWindow ());
output_mutex = OpenMutex (MAXIMUM_ALLOWED, TRUE, buf);
if (!output_mutex)
output_mutex = CreateMutex (&sec_none, FALSE, buf);
Expand Down Expand Up @@ -1853,7 +1860,7 @@ fhandler_console::open (int flags, mode_t)
if (GetModuleHandle ("ConEmuHk64.dll"))
hook_conemu_cygwin_connector ();
char name[MAX_PATH];
shared_name (name, CONS_THREAD_SYNC, get_minor ());
cons_shared_name (name, CONS_THREAD_SYNC, GetConsoleWindow ());
thread_sync_event = CreateEvent(NULL, FALSE, FALSE, name);
if (thread_sync_event)
{
Expand Down Expand Up @@ -1922,7 +1929,7 @@ fhandler_console::close ()
if (master_thread_started)
{
char name[MAX_PATH];
shared_name (name, CONS_THREAD_SYNC, get_minor ());
cons_shared_name (name, CONS_THREAD_SYNC, GetConsoleWindow ());
thread_sync_event = OpenEvent (MAXIMUM_ALLOWED, FALSE, name);
if (thread_sync_event)
{
Expand Down
3 changes: 3 additions & 0 deletions winsup/cygwin/release/3.5.4
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ Fixes:
- Fix a problem that pty slave hangs on writing when pty master stops
to read.
Addresses: https://cygwin.com/pipermail/cygwin/2024-June/256178.html

- Fix conflict on shared name in console between sessions.
Addresses: https://cygwin.com/pipermail/cygwin/2024-April/255893.html

0 comments on commit f7a77d0

Please sign in to comment.