From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id DEC0738582BC; Tue, 13 Feb 2024 14:38:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DEC0738582BC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1707835085; bh=85Svkr3PKtsysJUMwolwQ/J4Nh2QOFiUYvTCp8hXV98=; h=From:To:Subject:Date:From; b=KkhMtsk/RRcEnJVdTUkSzaLZ802jAwsXp1+jTWyTIAqqUFcgG8zEZh2Dce0ppPaS1 H7CnLAZOchm3qie+F9+4Y3o5D4MdwTzmd0sDUw7+JGdYvC4AbiJh8IF7f9LtdvR8IT lXTOoq4uyh+gnRUDU54hRSSWe8hXGbcZfpAHdKDM= 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: pty: Fix potential handle leak regarding CallNamedPipe(). X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: 06992ed6b4c63c6e77052fdc9359ce003fc3409f X-Git-Newrev: 8e24d162f4da6bad32712f902e4cc4d2f438d649 Message-Id: <20240213143805.DEC0738582BC@sourceware.org> Date: Tue, 13 Feb 2024 14:38:05 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D8e24d162f4d= a6bad32712f902e4cc4d2f438d649 commit 8e24d162f4da6bad32712f902e4cc4d2f438d649 Author: Takashi Yano Date: Tue Feb 13 11:36:05 2024 +0900 Cygwin: pty: Fix potential handle leak regarding CallNamedPipe(). =20 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. =20 Signed-off-by: Takashi Yano Diff: --- winsup/cygwin/fhandler/pty.cc | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/winsup/cygwin/fhandler/pty.cc b/winsup/cygwin/fhandler/pty.cc index c5c792144..d31d30b1f 100644 --- a/winsup/cygwin/fhandler/pty.cc +++ b/winsup/cygwin/fhandler/pty.cc @@ -936,6 +936,8 @@ fhandler_pty_slave::open (int flags, mode_t) errmsg =3D "can't call master, %E"; goto err; } + CloseHandle (repl.to_slave_nat); /* not used. */ + CloseHandle (repl.to_slave); /* not used. */ from_master_nat_local =3D repl.from_master_nat; from_master_local =3D repl.from_master; to_master_nat_local =3D repl.to_master_nat; @@ -1210,6 +1212,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 ()); @@ -3861,10 +3867,20 @@ fhandler_pty_slave::transfer_input (tty::xfer_dir d= ir, 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 =3D=3D tty::to_nat) - to =3D repl.to_slave_nat; + { + CloseHandle (repl.to_slave); /* not used. */ + to =3D repl.to_slave_nat; + } else - to =3D repl.to_slave; + { + CloseHandle (repl.to_slave_nat); /* not used. */ + to =3D repl.to_slave; + } } =20 UINT cp_from =3D 0, cp_to =3D 0;