Skip to content

Commit e46fad2

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 4b13e66 commit e46fad2

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
@@ -1640,8 +1640,13 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
16401640
const char *dir, const char *prepend_cmd,
16411641
int fhin, int fhout, int fherr)
16421642
{
1643-
STARTUPINFOW si;
1643+
static int restrict_handle_inheritance = 1;
1644+
STARTUPINFOEXW si;
16441645
PROCESS_INFORMATION pi;
1646+
LPPROC_THREAD_ATTRIBUTE_LIST attr_list = NULL;
1647+
HANDLE stdhandles[3];
1648+
DWORD stdhandles_count = 0;
1649+
SIZE_T size;
16451650
struct strbuf args;
16461651
wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL;
16471652
unsigned flags = CREATE_UNICODE_ENVIRONMENT;
@@ -1678,11 +1683,23 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
16781683
CloseHandle(cons);
16791684
}
16801685
memset(&si, 0, sizeof(si));
1681-
si.cb = sizeof(si);
1682-
si.dwFlags = STARTF_USESTDHANDLES;
1683-
si.hStdInput = winansi_get_osfhandle(fhin);
1684-
si.hStdOutput = winansi_get_osfhandle(fhout);
1685-
si.hStdError = winansi_get_osfhandle(fherr);
1686+
si.StartupInfo.cb = sizeof(si);
1687+
si.StartupInfo.hStdInput = winansi_get_osfhandle(fhin);
1688+
si.StartupInfo.hStdOutput = winansi_get_osfhandle(fhout);
1689+
si.StartupInfo.hStdError = winansi_get_osfhandle(fherr);
1690+
1691+
/* The list of handles cannot contain duplicates */
1692+
if (si.StartupInfo.hStdInput != INVALID_HANDLE_VALUE)
1693+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdInput;
1694+
if (si.StartupInfo.hStdOutput != INVALID_HANDLE_VALUE &&
1695+
si.StartupInfo.hStdOutput != si.StartupInfo.hStdInput)
1696+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdOutput;
1697+
if (si.StartupInfo.hStdError != INVALID_HANDLE_VALUE &&
1698+
si.StartupInfo.hStdError != si.StartupInfo.hStdInput &&
1699+
si.StartupInfo.hStdError != si.StartupInfo.hStdOutput)
1700+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdError;
1701+
if (stdhandles_count)
1702+
si.StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
16861703

16871704
/* executables and the current directory don't support long paths */
16881705
if (*argv && !strcmp(cmd, *argv))
@@ -1741,16 +1758,97 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
17411758
wenvblk = make_environment_block(deltaenv);
17421759

17431760
memset(&pi, 0, sizeof(pi));
1744-
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, TRUE,
1745-
flags, wenvblk, dir ? wdir : NULL, &si, &pi);
1761+
if (restrict_handle_inheritance && stdhandles_count &&
1762+
(InitializeProcThreadAttributeList(NULL, 1, 0, &size) ||
1763+
GetLastError() == ERROR_INSUFFICIENT_BUFFER) &&
1764+
(attr_list = (LPPROC_THREAD_ATTRIBUTE_LIST)
1765+
(HeapAlloc(GetProcessHeap(), 0, size))) &&
1766+
InitializeProcThreadAttributeList(attr_list, 1, 0, &size) &&
1767+
UpdateProcThreadAttribute(attr_list, 0,
1768+
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
1769+
stdhandles,
1770+
stdhandles_count * sizeof(HANDLE),
1771+
NULL, NULL)) {
1772+
si.lpAttributeList = attr_list;
1773+
flags |= EXTENDED_STARTUPINFO_PRESENT;
1774+
}
1775+
1776+
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL,
1777+
stdhandles_count ? TRUE : FALSE,
1778+
flags, wenvblk, dir ? wdir : NULL,
1779+
&si.StartupInfo, &pi);
1780+
1781+
/*
1782+
* On Windows 2008 R2, it seems that specifying certain types of handles
1783+
* (such as FILE_TYPE_CHAR or FILE_TYPE_PIPE) will always produce an
1784+
* error. Rather than playing finicky and fragile games, let's just try
1785+
* to detect this situation and simply try again without restricting any
1786+
* handle inheritance. This is still better than failing to create
1787+
* processes.
1788+
*/
1789+
if (!ret && restrict_handle_inheritance && stdhandles_count) {
1790+
DWORD err = GetLastError();
1791+
struct strbuf buf = STRBUF_INIT;
1792+
1793+
if (err != ERROR_NO_SYSTEM_RESOURCES &&
1794+
/*
1795+
* On Windows 7 and earlier, handles on pipes and character
1796+
* devices are inherited automatically, and cannot be
1797+
* specified in the thread handle list. Rather than trying
1798+
* to catch each and every corner case (and running the
1799+
* chance of *still* forgetting a few), let's just fall
1800+
* back to creating the process without trying to limit the
1801+
* handle inheritance.
1802+
*/
1803+
!(err == ERROR_INVALID_PARAMETER &&
1804+
GetVersion() >> 16 < 9200) &&
1805+
!getenv("SUPPRESS_HANDLE_INHERITANCE_WARNING")) {
1806+
DWORD fl = 0;
1807+
int i;
1808+
1809+
setenv("SUPPRESS_HANDLE_INHERITANCE_WARNING", "1", 1);
1810+
1811+
for (i = 0; i < stdhandles_count; i++) {
1812+
HANDLE h = stdhandles[i];
1813+
strbuf_addf(&buf, "handle #%d: %p (type %lx, "
1814+
"handle info (%d) %lx\n", i, h,
1815+
GetFileType(h),
1816+
GetHandleInformation(h, &fl),
1817+
fl);
1818+
}
1819+
strbuf_addstr(&buf, "\nThis is a bug; please report it "
1820+
"at\nhttps://github.com/git-for-windows/"
1821+
"git/issues/new\n\n"
1822+
"To suppress this warning, please set "
1823+
"the environment variable\n\n"
1824+
"\tSUPPRESS_HANDLE_INHERITANCE_WARNING=1"
1825+
"\n");
1826+
}
1827+
restrict_handle_inheritance = 0;
1828+
flags &= ~EXTENDED_STARTUPINFO_PRESENT;
1829+
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL,
1830+
TRUE, flags, wenvblk, dir ? wdir : NULL,
1831+
&si.StartupInfo, &pi);
1832+
if (ret && buf.len) {
1833+
errno = err_win_to_posix(GetLastError());
1834+
warning("failed to restrict file handles (%ld)\n\n%s",
1835+
err, buf.buf);
1836+
}
1837+
strbuf_release(&buf);
1838+
} else if (!ret)
1839+
errno = err_win_to_posix(GetLastError());
1840+
1841+
if (si.lpAttributeList)
1842+
DeleteProcThreadAttributeList(si.lpAttributeList);
1843+
if (attr_list)
1844+
HeapFree(GetProcessHeap(), 0, attr_list);
17461845

17471846
free(wenvblk);
17481847
free(wargs);
17491848

1750-
if (!ret) {
1751-
errno = ENOENT;
1849+
if (!ret)
17521850
return -1;
1753-
}
1851+
17541852
CloseHandle(pi.hThread);
17551853

17561854
/*

t/t0061-run-command.sh

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

15-
test_expect_failure MINGW 'subprocess inherits only std handles' '
15+
test_expect_success MINGW 'subprocess inherits only std handles' '
1616
test-tool run-command inherited-handle
1717
'
1818

0 commit comments

Comments
 (0)