Skip to content

Commit da1fabb

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 d955ddf commit da1fabb

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
@@ -1436,8 +1436,13 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14361436
const char *dir,
14371437
int prepend_cmd, int fhin, int fhout, int fherr)
14381438
{
1439-
STARTUPINFOW si;
1439+
static int restrict_handle_inheritance = 1;
1440+
STARTUPINFOEXW si;
14401441
PROCESS_INFORMATION pi;
1442+
LPPROC_THREAD_ATTRIBUTE_LIST attr_list = NULL;
1443+
HANDLE stdhandles[3];
1444+
DWORD stdhandles_count = 0;
1445+
SIZE_T size;
14411446
struct strbuf args;
14421447
wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL;
14431448
unsigned flags = CREATE_UNICODE_ENVIRONMENT;
@@ -1474,11 +1479,23 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14741479
CloseHandle(cons);
14751480
}
14761481
memset(&si, 0, sizeof(si));
1477-
si.cb = sizeof(si);
1478-
si.dwFlags = STARTF_USESTDHANDLES;
1479-
si.hStdInput = winansi_get_osfhandle(fhin);
1480-
si.hStdOutput = winansi_get_osfhandle(fhout);
1481-
si.hStdError = winansi_get_osfhandle(fherr);
1482+
si.StartupInfo.cb = sizeof(si);
1483+
si.StartupInfo.hStdInput = winansi_get_osfhandle(fhin);
1484+
si.StartupInfo.hStdOutput = winansi_get_osfhandle(fhout);
1485+
si.StartupInfo.hStdError = winansi_get_osfhandle(fherr);
1486+
1487+
/* The list of handles cannot contain duplicates */
1488+
if (si.StartupInfo.hStdInput != INVALID_HANDLE_VALUE)
1489+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdInput;
1490+
if (si.StartupInfo.hStdOutput != INVALID_HANDLE_VALUE &&
1491+
si.StartupInfo.hStdOutput != si.StartupInfo.hStdInput)
1492+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdOutput;
1493+
if (si.StartupInfo.hStdError != INVALID_HANDLE_VALUE &&
1494+
si.StartupInfo.hStdError != si.StartupInfo.hStdInput &&
1495+
si.StartupInfo.hStdError != si.StartupInfo.hStdOutput)
1496+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdError;
1497+
if (stdhandles_count)
1498+
si.StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
14821499

14831500
if (*argv && !strcmp(cmd, *argv))
14841501
wcmd[0] = L'\0';
@@ -1511,16 +1528,97 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
15111528
wenvblk = make_environment_block(deltaenv);
15121529

15131530
memset(&pi, 0, sizeof(pi));
1514-
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, TRUE,
1515-
flags, wenvblk, dir ? wdir : NULL, &si, &pi);
1531+
if (restrict_handle_inheritance && stdhandles_count &&
1532+
(InitializeProcThreadAttributeList(NULL, 1, 0, &size) ||
1533+
GetLastError() == ERROR_INSUFFICIENT_BUFFER) &&
1534+
(attr_list = (LPPROC_THREAD_ATTRIBUTE_LIST)
1535+
(HeapAlloc(GetProcessHeap(), 0, size))) &&
1536+
InitializeProcThreadAttributeList(attr_list, 1, 0, &size) &&
1537+
UpdateProcThreadAttribute(attr_list, 0,
1538+
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
1539+
stdhandles,
1540+
stdhandles_count * sizeof(HANDLE),
1541+
NULL, NULL)) {
1542+
si.lpAttributeList = attr_list;
1543+
flags |= EXTENDED_STARTUPINFO_PRESENT;
1544+
}
1545+
1546+
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL,
1547+
stdhandles_count ? TRUE : FALSE,
1548+
flags, wenvblk, dir ? wdir : NULL,
1549+
&si.StartupInfo, &pi);
1550+
1551+
/*
1552+
* On Windows 2008 R2, it seems that specifying certain types of handles
1553+
* (such as FILE_TYPE_CHAR or FILE_TYPE_PIPE) will always produce an
1554+
* error. Rather than playing finicky and fragile games, let's just try
1555+
* to detect this situation and simply try again without restricting any
1556+
* handle inheritance. This is still better than failing to create
1557+
* processes.
1558+
*/
1559+
if (!ret && restrict_handle_inheritance && stdhandles_count) {
1560+
DWORD err = GetLastError();
1561+
struct strbuf buf = STRBUF_INIT;
1562+
1563+
if (err != ERROR_NO_SYSTEM_RESOURCES &&
1564+
/*
1565+
* On Windows 7 and earlier, handles on pipes and character
1566+
* devices are inherited automatically, and cannot be
1567+
* specified in the thread handle list. Rather than trying
1568+
* to catch each and every corner case (and running the
1569+
* chance of *still* forgetting a few), let's just fall
1570+
* back to creating the process without trying to limit the
1571+
* handle inheritance.
1572+
*/
1573+
!(err == ERROR_INVALID_PARAMETER &&
1574+
GetVersion() >> 16 < 9200) &&
1575+
!getenv("SUPPRESS_HANDLE_INHERITANCE_WARNING")) {
1576+
DWORD fl = 0;
1577+
int i;
1578+
1579+
setenv("SUPPRESS_HANDLE_INHERITANCE_WARNING", "1", 1);
1580+
1581+
for (i = 0; i < stdhandles_count; i++) {
1582+
HANDLE h = stdhandles[i];
1583+
strbuf_addf(&buf, "handle #%d: %p (type %lx, "
1584+
"handle info (%d) %lx\n", i, h,
1585+
GetFileType(h),
1586+
GetHandleInformation(h, &fl),
1587+
fl);
1588+
}
1589+
strbuf_addstr(&buf, "\nThis is a bug; please report it "
1590+
"at\nhttps://github.com/git-for-windows/"
1591+
"git/issues/new\n\n"
1592+
"To suppress this warning, please set "
1593+
"the environment variable\n\n"
1594+
"\tSUPPRESS_HANDLE_INHERITANCE_WARNING=1"
1595+
"\n");
1596+
}
1597+
restrict_handle_inheritance = 0;
1598+
flags &= ~EXTENDED_STARTUPINFO_PRESENT;
1599+
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL,
1600+
TRUE, flags, wenvblk, dir ? wdir : NULL,
1601+
&si.StartupInfo, &pi);
1602+
if (ret && buf.len) {
1603+
errno = err_win_to_posix(GetLastError());
1604+
warning("failed to restrict file handles (%ld)\n\n%s",
1605+
err, buf.buf);
1606+
}
1607+
strbuf_release(&buf);
1608+
} else if (!ret)
1609+
errno = err_win_to_posix(GetLastError());
1610+
1611+
if (si.lpAttributeList)
1612+
DeleteProcThreadAttributeList(si.lpAttributeList);
1613+
if (attr_list)
1614+
HeapFree(GetProcessHeap(), 0, attr_list);
15161615

15171616
free(wenvblk);
15181617
free(wargs);
15191618

1520-
if (!ret) {
1521-
errno = ENOENT;
1619+
if (!ret)
15221620
return -1;
1523-
}
1621+
15241622
CloseHandle(pi.hThread);
15251623

15261624
/*

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)