public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Takashi Yano <tyan0@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: pty: Fix potential handle leak regarding CallNamedPipe().
Date: Tue, 13 Feb 2024 14:38:05 +0000 (GMT)	[thread overview]
Message-ID: <20240213143805.DEC0738582BC@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=8e24d162f4da6bad32712f902e4cc4d2f438d649

commit 8e24d162f4da6bad32712f902e4cc4d2f438d649
Author: Takashi Yano <takashi.yano@nifty.ne.jp>
Date:   Tue Feb 13 11:36:05 2024 +0900

    Cygwin: pty: Fix potential handle leak regarding CallNamedPipe().
    
    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 <takashi.yano@nifty.ne.jp>

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 = "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;
@@ -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 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;

                 reply	other threads:[~2024-02-13 14:38 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240213143805.DEC0738582BC@sourceware.org \
    --to=tyan0@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).