Skip to content

Commit

Permalink
msys2-runtime: some backports
Browse files Browse the repository at this point in the history
This corresponds to msys2/msys2-runtime#205.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Apr 7, 2024
1 parent 0def141 commit 73b702e
Show file tree
Hide file tree
Showing 7 changed files with 324 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
From 9fd76414684cfc137252b4a63a015dfc29e79fdb Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Fri, 2 Feb 2024 13:59:19 +0900
Subject: [PATCH 55/N] Cygwin: console: Fix exit code for non-cygwin process.

If non-cygwin process is executed in console, the exit code is not
set correctly. This is because the stub process for non-cygwin app
crashes in fhandler_console::set_disable_master_thread() due to NULL
pointer dereference. This bug was introduced by the commit:
3721a756b0d8 ("Cygwin: console: Make the console accessible from
other terminals."), that the pointer cons is accessed before fixing
when it is NULL. This patch fixes the issue.

Backported-from: aa73e11524 (Cygwin: console: Fix exit code for non-cygwin process., 2024-02-02)
Fixes: 3721a756b0d8 ("Cygwin: console: Make the console accessible from other terminals.")
Reported-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
winsup/cygwin/fhandler/console.cc | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
index c9b27c9..6c485f9 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -4328,8 +4328,6 @@ fhandler_console::need_console_handler ()
void
fhandler_console::set_disable_master_thread (bool x, fhandler_console *cons)
{
- if (con.disable_master_thread == x)
- return;
if (cons == NULL)
{
if (cygheap->ctty && cygheap->ctty->get_major () == DEV_CONS_MAJOR)
@@ -4337,6 +4335,8 @@ fhandler_console::set_disable_master_thread (bool x, fhandler_console *cons)
else
return;
}
+ if (con.disable_master_thread == x)
+ return;
cons->acquire_input_mutex (mutex_timeout);
con.disable_master_thread = x;
cons->release_input_mutex ();
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
From fb060f79f664189f3b61a9d361dafbedf79e2441 Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Sat, 3 Feb 2024 00:54:23 +0900
Subject: [PATCH 56/N] Cygwin: console: Avoid slipping past
disable_master_thread check.

If disable_master_thread flag is set between the code checking that
flag not be set and the code acquiring input_mutex, input record is
processed once after setting disable_master_thread flag. This patch
prevents that.

Backported-from: 9bcfd06045 (Cygwin: console: Avoid slipping past disable_master_thread check., 2024-02-03)
Fixes: d4aacd50e6cf ("Cygwin: console: Add missing input_mutex guard.")
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
winsup/cygwin/fhandler/console.cc | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
index 6c485f9..b415a0a 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -361,6 +361,12 @@ fhandler_console::cons_master_thread (handle_set_t *p, tty *ttyp)
}

WaitForSingleObject (p->input_mutex, mutex_timeout);
+ /* Ensure accessing input recored is not disabled. */
+ if (con.disable_master_thread)
+ {
+ ReleaseMutex (p->input_mutex);
+ continue;
+ }
total_read = 0;
switch (cygwait (p->input_handle, (DWORD) 0))
{
@@ -4335,8 +4341,6 @@ fhandler_console::set_disable_master_thread (bool x, fhandler_console *cons)
else
return;
}
- if (con.disable_master_thread == x)
- return;
cons->acquire_input_mutex (mutex_timeout);
con.disable_master_thread = x;
cons->release_input_mutex ();
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
From f769444ad2f17709dfcb9c0067aa3b39b525abee Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Tue, 13 Feb 2024 11:17:46 +0900
Subject: [PATCH 57/N] Cygwin: pty: Fix handle leak in master process.

If non-cygwin process is started in pty, closing from_master_nat
pipe handle was missing in fhandler_pty_slave::input_transfer().
This occured because the handle was duplicated but not closed.

https://github.com/msys2/msys2-runtime/issues/198

Backported-from: a6ac7b4138 (Cygwin: pty: Fix handle leak in master process., 2024-02-13)
Fixes: 29431fcb5b14 ("Cygwin: pty: Inherit typeahead data between two input pipes.")
Reported-by: Hakkin Lain
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
winsup/cygwin/fhandler/pty.cc | 1 +
1 file changed, 1 insertion(+)

diff --git a/winsup/cygwin/fhandler/pty.cc b/winsup/cygwin/fhandler/pty.cc
index dbeffc9..9d1a119 100644
--- a/winsup/cygwin/fhandler/pty.cc
+++ b/winsup/cygwin/fhandler/pty.cc
@@ -4066,6 +4066,7 @@ fhandler_pty_slave::transfer_input (tty::xfer_dir dir, HANDLE from, tty *ttyp,
transfered = true;;
}
}
+ CloseHandle (to);

/* Fix input_available_event which indicates availability in cyg pipe. */
if (dir == tty::to_nat) /* all data is transfered to nat pipe,
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
From 85e89bbeff42c0195ec1f4cbbff063aeac145ad8 Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Tue, 13 Feb 2024 11:36:05 +0900
Subject: [PATCH 58/N] 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.

Backported-from: 73cd80c976 (Cygwin: pty: Fix potential handle leak regarding CallNamedPipe()., 2024-02-13)
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
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 9d1a119..17b7b32 100644
--- a/winsup/cygwin/fhandler/pty.cc
+++ b/winsup/cygwin/fhandler/pty.cc
@@ -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;
@@ -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 ());
@@ -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;
74 changes: 74 additions & 0 deletions msys2-runtime/0059-Cygwin-console-Make-VMIN-and-VTIME-work.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
From b3295b576d7662daf6e2c14761f50c5e05a21209 Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Tue, 13 Feb 2024 11:42:42 +0900
Subject: [PATCH 59/N] Cygwin: console: Make VMIN and VTIME work.

Previously, VMIN and VTIME did not work at all. This patch fixes that.

Backported-from: 73cd80c976 (Cygwin: pty: Fix potential handle leak regarding CallNamedPipe()., 2024-02-13)
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
winsup/cygwin/fhandler/console.cc | 26 ++++++++++++++++++--------
1 file changed, 18 insertions(+), 8 deletions(-)

diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
index b415a0a..f9a6946 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -1011,10 +1011,14 @@ fhandler_console::read (void *pv, size_t& buflen)

push_process_state process_state (PID_TTYIN);

- int copied_chars = 0;
+ size_t copied_chars = 0;

- DWORD timeout = is_nonblocking () ? 0 : INFINITE;
+ DWORD timeout = is_nonblocking () ? 0 :
+ (get_ttyp ()->ti.c_lflag & ICANON ? INFINITE :
+ (get_ttyp ()->ti.c_cc[VMIN] == 0 ? 0 :
+ (get_ttyp ()->ti.c_cc[VTIME]*100 ? : INFINITE)));

+read_more:
while (!input_ready && !get_cons_readahead_valid ())
{
int bgres;
@@ -1037,6 +1041,11 @@ wait_retry:
pthread::static_cancel_self ();
/*NOTREACHED*/
case WAIT_TIMEOUT:
+ if (copied_chars)
+ {
+ buflen = copied_chars;
+ return;
+ }
set_sig_errno (EAGAIN);
buflen = (size_t) -1;
return;
@@ -1082,19 +1091,20 @@ wait_retry:
}

/* Check console read-ahead buffer filled from terminal requests */
- while (con.cons_rapoi && *con.cons_rapoi && buflen)
- {
- buf[copied_chars++] = *con.cons_rapoi++;
- buflen --;
- }
+ while (con.cons_rapoi && *con.cons_rapoi && buflen > copied_chars)
+ buf[copied_chars++] = *con.cons_rapoi++;

copied_chars +=
- get_readahead_into_buffer (buf + copied_chars, buflen);
+ get_readahead_into_buffer (buf + copied_chars, buflen - copied_chars);

if (!con_ra.ralen)
input_ready = false;
release_input_mutex ();

+ if (buflen > copied_chars && !(get_ttyp ()->ti.c_lflag & ICANON)
+ && copied_chars < get_ttyp ()->ti.c_cc[VMIN])
+ goto read_more;
+
#undef buf

buflen = copied_chars;
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
From 436c8752437af86d24712a5a95b78fecde3ad35b Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Sun, 3 Mar 2024 14:09:07 +0900
Subject: [PATCH 60/N] Cygwin: pipe: Give up to use query_hdl for non-cygwin
apps.

Non-cygwin app may call ReadFile() which makes NtQueryObject() for
ObjectNameInformation block in fhandler_pipe::get_query_hdl_per_process.
Therefore, stop to try to get query_hdl for non-cygwin apps.

Addresses: https://github.com/msys2/msys2-runtime/issues/202

Backported-from: https://inbox.sourceware.org/cygwin-patches/20240303050915.2024-1-takashi.yano@nifty.ne.jp/
Fixes: b531d6b06eeb ("Cygwin: pipe: Introduce temporary query_hdl.")
Reported-by: Alisa Sireneva, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by:
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
winsup/cygwin/fhandler/pipe.cc | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/winsup/cygwin/fhandler/pipe.cc b/winsup/cygwin/fhandler/pipe.cc
index 283319c..9319a57 100644
--- a/winsup/cygwin/fhandler/pipe.cc
+++ b/winsup/cygwin/fhandler/pipe.cc
@@ -1251,6 +1251,16 @@ fhandler_pipe::get_query_hdl_per_process (WCHAR *name,

for (LONG i = (LONG) n_process - 1; i >= 0; i--)
{
+ /* Non-cygwin app may call ReadFile() which makes NtQueryObject()
+ for ObjectNameInformation block. Therefore, stop to try to get
+ query_hdl for non-cygwin apps. */
+ pid_t cygpid;
+ if (!(cygpid = cygwin_pid (proc_pids[i])))
+ continue;
+ pinfo p (cygpid);
+ if (p && ISSTATE (p, PID_NOTCYGWIN))
+ continue;
+
HANDLE proc = OpenProcess (PROCESS_DUP_HANDLE
| PROCESS_QUERY_INFORMATION,
0, proc_pids[i]);
26 changes: 22 additions & 4 deletions msys2-runtime/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pkgbase=msys2-runtime
pkgname=('msys2-runtime' 'msys2-runtime-devel')
pkgver=3.4.10
pkgrel=7
pkgrel=8
pkgdesc="Cygwin POSIX emulation engine"
arch=('x86_64')
url="https://www.cygwin.com/"
Expand Down Expand Up @@ -82,7 +82,13 @@ source=('msys2-runtime'::git://sourceware.org/git/newlib-cygwin.git#tag=cygwin-$
0051-Work-around-fragile-include-in-binutils.patch
0052-Cygwin-try-to-avoid-recalling-offline-files.patch
0053-Cygwin-get-set-security-descriptors-using-FILE_OPEN_.patch
0054-Cygwin-FILE_OPEN_NO_RECALL-is-incompatible-with-FILE.patch)
0054-Cygwin-FILE_OPEN_NO_RECALL-is-incompatible-with-FILE.patch
0055-Cygwin-console-Fix-exit-code-for-non-cygwin-process.patch
0056-Cygwin-console-Avoid-slipping-past-disable_master_th.patch
0057-Cygwin-pty-Fix-handle-leak-in-master-process.patch
0058-Cygwin-pty-Fix-potential-handle-leak-regarding-CallN.patch
0059-Cygwin-console-Make-VMIN-and-VTIME-work.patch
0060-Cygwin-pipe-Give-up-to-use-query_hdl-for-non-cygwin-.patch)
sha256sums=('SKIP'
'351bb1efdbdafe80c981e92d6b425c6ab71c85ce4e990db184e2118158eb2ab6'
'd3d3a01feeae9f7d5e6cb32f4662df74fc9476ff11a1aac3dad2df3e43fd88e4'
Expand Down Expand Up @@ -137,7 +143,13 @@ sha256sums=('SKIP'
'164527ad2e289050ed336a6f1aa8e1af40fb864e7e4aed545bde2703d41203bd'
'9879a0fe09147b1e0d41f42211bb309dd08ea088186f413d4dec6e0300ef9fbc'
'677c013c456ca1ec239a694340ae36eae99a5700d0dda09e3679a29997234512'
'266db90ae4f67f2818d015272cdda3d751a62724149d035d1a04a4407a7dbcef')
'266db90ae4f67f2818d015272cdda3d751a62724149d035d1a04a4407a7dbcef'
'368c5a3c9df12d4fd0716979ec449890cb39690801b13121c06575a136727b5f'
'27b0596bea70532f0f1a017bda81a87e273ddcb76b638887adb3dd3171bdccf4'
'54afb4ad79fccdb1230181729edca861119b7668aa35715265461c0b0317a6a2'
'71d11ca9d0a39bb643ccff2c74e90fd2008453ef81361ddf00f3cc4429b2e687'
'43383dd8358ae49865f1856493f482eec1fa56710e990029028296888f50ac2c'
'45cf4e509845d27bf0d12bc6b878e8c2e33754e232a8d1de43ed7e19e451bfac')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
Expand Down Expand Up @@ -228,7 +240,13 @@ prepare() {
0051-Work-around-fragile-include-in-binutils.patch \
0052-Cygwin-try-to-avoid-recalling-offline-files.patch \
0053-Cygwin-get-set-security-descriptors-using-FILE_OPEN_.patch \
0054-Cygwin-FILE_OPEN_NO_RECALL-is-incompatible-with-FILE.patch
0054-Cygwin-FILE_OPEN_NO_RECALL-is-incompatible-with-FILE.patch \
0055-Cygwin-console-Fix-exit-code-for-non-cygwin-process.patch \
0056-Cygwin-console-Avoid-slipping-past-disable_master_th.patch \
0057-Cygwin-pty-Fix-handle-leak-in-master-process.patch \
0058-Cygwin-pty-Fix-potential-handle-leak-regarding-CallN.patch \
0059-Cygwin-console-Make-VMIN-and-VTIME-work.patch \
0060-Cygwin-pipe-Give-up-to-use-query_hdl-for-non-cygwin-.patch
}

build() {
Expand Down

0 comments on commit 73b702e

Please sign in to comment.