Skip to content

Commit

Permalink
Cygwin: pty: Fix potential handle leak regarding CallNamedPipe().
Browse files Browse the repository at this point in the history
In pty master_thread, 6 handles are duplicated when CallNamedPipe()
requests that. Though some of them are not used so should be closed,
they were not. This causes handle leak potentially.

Signed-off-by: Takashi Yano <[email protected]>
Signed-off-by: Johannes Schindelin <[email protected]>
  • Loading branch information
tyan0 authored and dscho committed Feb 13, 2024
1 parent 915e146 commit 9bf75c4
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions winsup/cygwin/fhandler/pty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,8 @@ fhandler_pty_slave::open (int flags, mode_t)
errmsg = "can't call master, %E";
goto err;
}
CloseHandle (repl.to_slave_nat); /* not used. */
CloseHandle (repl.to_slave); /* not used. */
from_master_nat_local = repl.from_master_nat;
from_master_local = repl.from_master;
to_master_nat_local = repl.to_master_nat;
Expand Down Expand Up @@ -1218,6 +1220,10 @@ fhandler_pty_slave::reset_switch_to_nat_pipe (void)
if (!CallNamedPipe (pipe, &req, sizeof req,
&repl, sizeof repl, &len, 500))
return; /* What can we do? */
CloseHandle (repl.from_master); /* not used. */
CloseHandle (repl.to_master); /* not used. */
CloseHandle (repl.to_slave_nat); /* not used. */
CloseHandle (repl.to_slave); /* not used. */
CloseHandle (get_handle_nat ());
set_handle_nat (repl.from_master_nat);
CloseHandle (get_output_handle_nat ());
Expand Down Expand Up @@ -3932,10 +3938,20 @@ fhandler_pty_slave::transfer_input (tty::xfer_dir dir, HANDLE from, tty *ttyp,
if (!CallNamedPipe (pipe, &req, sizeof req,
&repl, sizeof repl, &len, 500))
return; /* What can we do? */
CloseHandle (repl.from_master_nat); /* not used. */
CloseHandle (repl.from_master); /* not used. */
CloseHandle (repl.to_master_nat); /* not used. */
CloseHandle (repl.to_master); /* not used. */
if (dir == tty::to_nat)
to = repl.to_slave_nat;
{
CloseHandle (repl.to_slave); /* not used. */
to = repl.to_slave_nat;
}
else
to = repl.to_slave;
{
CloseHandle (repl.to_slave_nat); /* not used. */
to = repl.to_slave;
}
}

UINT cp_from = 0, cp_to = 0;
Expand Down

0 comments on commit 9bf75c4

Please sign in to comment.