MSYS2-packages/msys2-runtime/0039-Cygwin-fhandler_pipe-get_query_hdl_per_process-avoid.patch
Johannes Schindelin f33f945644 msys2-runtime: special case the SHELL and ORIGINAL_PATH env vars
In MSYS2 <-> MINGW transitions, we convert a couple of environment
variables between Unix and Windows styles, most notably `PATH`.

In the Git for Windows project, we realized that the environment
variables `SHELL` and `ORIGINAL_PATH` need the same treatment. These
patches have proved their worth for years, and it is time for them to be
integrated back into the actual MSYS2 runtime.

This should fix https://github.com/msys2/msys2-runtime/issues/43.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2022-01-13 17:44:33 +01:00

66 lines
2.5 KiB
Diff

From e4fbec279b49055a139a8f26ab4601ea30064e2d Mon Sep 17 00:00:00 2001
From: Ken Brown <kbrown@cornell.edu>
Date: Sun, 26 Dec 2021 16:42:26 -0500
Subject: [PATCH 39/N] Cygwin: fhandler_pipe::get_query_hdl_per_process: avoid
a crash
NtQueryInformationProcess(ProcessHandleInformation) can return
STATUS_SUCCESS with invalid handle data for certain processes
("minimal" processes on Windows 10). This can cause a crash when
there's an attempt to access that data. Fix that by setting
NumberOfHandles to zero before calling NtQueryInformationProcess.
Addresses: https://cygwin.com/pipermail/cygwin-patches/2021q4/011611.html
---
winsup/cygwin/fhandler_pipe.cc | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/winsup/cygwin/fhandler_pipe.cc b/winsup/cygwin/fhandler_pipe.cc
index 9ce1400..e4b71b5 100644
--- a/winsup/cygwin/fhandler_pipe.cc
+++ b/winsup/cygwin/fhandler_pipe.cc
@@ -6,8 +6,6 @@ This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
-/* FIXME: Should this really be fhandler_pipe.cc? */
-
#include "winsup.h"
#include <stdlib.h>
#include <sys/socket.h>
@@ -20,6 +18,7 @@ details. */
#include "pinfo.h"
#include "shared_info.h"
#include "tls_pbuf.h"
+#include <assert.h>
/* This is only to be used for writing. When reading,
STATUS_PIPE_EMPTY simply means there's no data to be read. */
@@ -1260,6 +1259,12 @@ fhandler_pipe::get_query_hdl_per_process (WCHAR *name,
HeapAlloc (GetProcessHeap (), 0, nbytes);
if (!phi)
goto close_proc;
+ /* NtQueryInformationProcess can return STATUS_SUCCESS with
+ invalid handle data for certain processes. See
+ https://github.com/processhacker/processhacker/blob/05f5e9fa477dcaa1709d9518170d18e1b3b8330d/phlib/native.c#L5754.
+ We need to ensure that NumberOfHandles is zero in this
+ case to avoid a crash in the for loop below. */
+ phi->NumberOfHandles = 0;
status = NtQueryInformationProcess (proc, ProcessHandleInformation,
phi, nbytes, &len);
if (NT_SUCCESS (status))
@@ -1271,6 +1276,10 @@ fhandler_pipe::get_query_hdl_per_process (WCHAR *name,
if (!NT_SUCCESS (status))
goto close_proc;
+ /* Sanity check in case Microsoft changes
+ NtQueryInformationProcess and the initialization of
+ NumberOfHandles above is no longer sufficient. */
+ assert (phi->NumberOfHandles <= n_handle);
for (ULONG j = 0; j < phi->NumberOfHandles; j++)
{
/* Check for the peculiarity of cygwin read pipe */
--
2.34.1