Skip to content

Commit ebf9c6a

Browse files
committed
mingw: spawned processes need to inherit only standard handles
By default, CreateProcess() does not inherit any open file handles, unless the bInheritHandles parameter is set to TRUE. Which we do need to set because we need to pass in stdin/stdout/stderr to talk to the child processes. Sadly, this means that all file handles (unless marked via O_NOINHERIT) are inherited. This lead to problems in GVFS Git, where a long-running read-object hook is used to hydrate missing objects, and depending on the circumstances, might only be called *after* Git opened a file handle. Ideally, we would not open files without O_NOINHERIT unless *really* necessary (i.e. when we want to pass the opened file handle as standard handle into a child process), but apparently it is all-too-easy to introduce incorrect open() calls: this happened, and prevented updating a file after the read-object hook was started because the hook still held a handle on said file. Happily, there is a solution: as described in the "Old New Thing" https://blogs.msdn.microsoft.com/oldnewthing/20111216-00/?p=8873 there is a way, starting with Windows Vista, that lets us define precisely which handles should be inherited by the child process. And since we bumped the minimum Windows version for use with Git for Windows to Vista with v2.10.1 (i.e. a *long* time ago), we can use this method. So let's do exactly that. We need to make sure that the list of handles to inherit does not contain duplicates; Otherwise CreateProcessW() would fail with ERROR_INVALID_ARGUMENT. While at it, stop setting errno to ENOENT unless it really is the correct value. Also, fall back to not limiting handle inheritance under certain error conditions (e.g. on Windows 7, which is a lot stricter in what handles you can specify to limit to). Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 6913d62 commit ebf9c6a

File tree

2 files changed

+110
-12
lines changed

2 files changed

+110
-12
lines changed

compat/mingw.c

Lines changed: 109 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,8 +1603,13 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
16031603
const char *dir, const char *prepend_cmd,
16041604
int fhin, int fhout, int fherr)
16051605
{
1606-
STARTUPINFOW si;
1606+
static int restrict_handle_inheritance = 1;
1607+
STARTUPINFOEXW si;
16071608
PROCESS_INFORMATION pi;
1609+
LPPROC_THREAD_ATTRIBUTE_LIST attr_list = NULL;
1610+
HANDLE stdhandles[3];
1611+
DWORD stdhandles_count = 0;
1612+
SIZE_T size;
16081613
struct strbuf args;
16091614
wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL;
16101615
unsigned flags = CREATE_UNICODE_ENVIRONMENT;
@@ -1641,11 +1646,23 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
16411646
CloseHandle(cons);
16421647
}
16431648
memset(&si, 0, sizeof(si));
1644-
si.cb = sizeof(si);
1645-
si.dwFlags = STARTF_USESTDHANDLES;
1646-
si.hStdInput = winansi_get_osfhandle(fhin);
1647-
si.hStdOutput = winansi_get_osfhandle(fhout);
1648-
si.hStdError = winansi_get_osfhandle(fherr);
1649+
si.StartupInfo.cb = sizeof(si);
1650+
si.StartupInfo.hStdInput = winansi_get_osfhandle(fhin);
1651+
si.StartupInfo.hStdOutput = winansi_get_osfhandle(fhout);
1652+
si.StartupInfo.hStdError = winansi_get_osfhandle(fherr);
1653+
1654+
/* The list of handles cannot contain duplicates */
1655+
if (si.StartupInfo.hStdInput != INVALID_HANDLE_VALUE)
1656+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdInput;
1657+
if (si.StartupInfo.hStdOutput != INVALID_HANDLE_VALUE &&
1658+
si.StartupInfo.hStdOutput != si.StartupInfo.hStdInput)
1659+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdOutput;
1660+
if (si.StartupInfo.hStdError != INVALID_HANDLE_VALUE &&
1661+
si.StartupInfo.hStdError != si.StartupInfo.hStdInput &&
1662+
si.StartupInfo.hStdError != si.StartupInfo.hStdOutput)
1663+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdError;
1664+
if (stdhandles_count)
1665+
si.StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
16491666

16501667
/* executables and the current directory don't support long paths */
16511668
if (*argv && !strcmp(cmd, *argv))
@@ -1704,16 +1721,97 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
17041721
wenvblk = make_environment_block(deltaenv);
17051722

17061723
memset(&pi, 0, sizeof(pi));
1707-
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, TRUE,
1708-
flags, wenvblk, dir ? wdir : NULL, &si, &pi);
1724+
if (restrict_handle_inheritance && stdhandles_count &&
1725+
(InitializeProcThreadAttributeList(NULL, 1, 0, &size) ||
1726+
GetLastError() == ERROR_INSUFFICIENT_BUFFER) &&
1727+
(attr_list = (LPPROC_THREAD_ATTRIBUTE_LIST)
1728+
(HeapAlloc(GetProcessHeap(), 0, size))) &&
1729+
InitializeProcThreadAttributeList(attr_list, 1, 0, &size) &&
1730+
UpdateProcThreadAttribute(attr_list, 0,
1731+
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
1732+
stdhandles,
1733+
stdhandles_count * sizeof(HANDLE),
1734+
NULL, NULL)) {
1735+
si.lpAttributeList = attr_list;
1736+
flags |= EXTENDED_STARTUPINFO_PRESENT;
1737+
}
1738+
1739+
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL,
1740+
stdhandles_count ? TRUE : FALSE,
1741+
flags, wenvblk, dir ? wdir : NULL,
1742+
&si.StartupInfo, &pi);
1743+
1744+
/*
1745+
* On Windows 2008 R2, it seems that specifying certain types of handles
1746+
* (such as FILE_TYPE_CHAR or FILE_TYPE_PIPE) will always produce an
1747+
* error. Rather than playing finicky and fragile games, let's just try
1748+
* to detect this situation and simply try again without restricting any
1749+
* handle inheritance. This is still better than failing to create
1750+
* processes.
1751+
*/
1752+
if (!ret && restrict_handle_inheritance && stdhandles_count) {
1753+
DWORD err = GetLastError();
1754+
struct strbuf buf = STRBUF_INIT;
1755+
1756+
if (err != ERROR_NO_SYSTEM_RESOURCES &&
1757+
/*
1758+
* On Windows 7 and earlier, handles on pipes and character
1759+
* devices are inherited automatically, and cannot be
1760+
* specified in the thread handle list. Rather than trying
1761+
* to catch each and every corner case (and running the
1762+
* chance of *still* forgetting a few), let's just fall
1763+
* back to creating the process without trying to limit the
1764+
* handle inheritance.
1765+
*/
1766+
!(err == ERROR_INVALID_PARAMETER &&
1767+
GetVersion() >> 16 < 9200) &&
1768+
!getenv("SUPPRESS_HANDLE_INHERITANCE_WARNING")) {
1769+
DWORD fl = 0;
1770+
int i;
1771+
1772+
setenv("SUPPRESS_HANDLE_INHERITANCE_WARNING", "1", 1);
1773+
1774+
for (i = 0; i < stdhandles_count; i++) {
1775+
HANDLE h = stdhandles[i];
1776+
strbuf_addf(&buf, "handle #%d: %p (type %lx, "
1777+
"handle info (%d) %lx\n", i, h,
1778+
GetFileType(h),
1779+
GetHandleInformation(h, &fl),
1780+
fl);
1781+
}
1782+
strbuf_addstr(&buf, "\nThis is a bug; please report it "
1783+
"at\nhttps://github.com/git-for-windows/"
1784+
"git/issues/new\n\n"
1785+
"To suppress this warning, please set "
1786+
"the environment variable\n\n"
1787+
"\tSUPPRESS_HANDLE_INHERITANCE_WARNING=1"
1788+
"\n");
1789+
}
1790+
restrict_handle_inheritance = 0;
1791+
flags &= ~EXTENDED_STARTUPINFO_PRESENT;
1792+
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL,
1793+
TRUE, flags, wenvblk, dir ? wdir : NULL,
1794+
&si.StartupInfo, &pi);
1795+
if (ret && buf.len) {
1796+
errno = err_win_to_posix(GetLastError());
1797+
warning("failed to restrict file handles (%ld)\n\n%s",
1798+
err, buf.buf);
1799+
}
1800+
strbuf_release(&buf);
1801+
} else if (!ret)
1802+
errno = err_win_to_posix(GetLastError());
1803+
1804+
if (si.lpAttributeList)
1805+
DeleteProcThreadAttributeList(si.lpAttributeList);
1806+
if (attr_list)
1807+
HeapFree(GetProcessHeap(), 0, attr_list);
17091808

17101809
free(wenvblk);
17111810
free(wargs);
17121811

1713-
if (!ret) {
1714-
errno = ENOENT;
1812+
if (!ret)
17151813
return -1;
1716-
}
1814+
17171815
CloseHandle(pi.hThread);
17181816

17191817
/*

t/t0061-run-command.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ cat >hello-script <<-EOF
1313
EOF
1414
>empty
1515

16-
test_expect_failure MINGW 'subprocess inherits only std handles' '
16+
test_expect_success MINGW 'subprocess inherits only std handles' '
1717
test-run-command inherited-handle
1818
'
1919

0 commit comments

Comments
 (0)