Skip to content

Commit 448e644

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 baf1374 commit 448e644

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
@@ -1401,8 +1401,13 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14011401
const char *dir,
14021402
int prepend_cmd, int fhin, int fhout, int fherr)
14031403
{
1404-
STARTUPINFOW si;
1404+
static int restrict_handle_inheritance = 1;
1405+
STARTUPINFOEXW si;
14051406
PROCESS_INFORMATION pi;
1407+
LPPROC_THREAD_ATTRIBUTE_LIST attr_list = NULL;
1408+
HANDLE stdhandles[3];
1409+
DWORD stdhandles_count = 0;
1410+
SIZE_T size;
14061411
struct strbuf args;
14071412
wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL;
14081413
unsigned flags = CREATE_UNICODE_ENVIRONMENT;
@@ -1438,11 +1443,23 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14381443
CloseHandle(cons);
14391444
}
14401445
memset(&si, 0, sizeof(si));
1441-
si.cb = sizeof(si);
1442-
si.dwFlags = STARTF_USESTDHANDLES;
1443-
si.hStdInput = winansi_get_osfhandle(fhin);
1444-
si.hStdOutput = winansi_get_osfhandle(fhout);
1445-
si.hStdError = winansi_get_osfhandle(fherr);
1446+
si.StartupInfo.cb = sizeof(si);
1447+
si.StartupInfo.hStdInput = winansi_get_osfhandle(fhin);
1448+
si.StartupInfo.hStdOutput = winansi_get_osfhandle(fhout);
1449+
si.StartupInfo.hStdError = winansi_get_osfhandle(fherr);
1450+
1451+
/* The list of handles cannot contain duplicates */
1452+
if (si.StartupInfo.hStdInput != INVALID_HANDLE_VALUE)
1453+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdInput;
1454+
if (si.StartupInfo.hStdOutput != INVALID_HANDLE_VALUE &&
1455+
si.StartupInfo.hStdOutput != si.StartupInfo.hStdInput)
1456+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdOutput;
1457+
if (si.StartupInfo.hStdError != INVALID_HANDLE_VALUE &&
1458+
si.StartupInfo.hStdError != si.StartupInfo.hStdInput &&
1459+
si.StartupInfo.hStdError != si.StartupInfo.hStdOutput)
1460+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdError;
1461+
if (stdhandles_count)
1462+
si.StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
14461463

14471464
/* executables and the current directory don't support long paths */
14481465
if (*argv && !strcmp(cmd, *argv))
@@ -1476,16 +1493,97 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14761493
wenvblk = make_environment_block(deltaenv);
14771494

14781495
memset(&pi, 0, sizeof(pi));
1479-
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, TRUE,
1480-
flags, wenvblk, dir ? wdir : NULL, &si, &pi);
1496+
if (restrict_handle_inheritance && stdhandles_count &&
1497+
(InitializeProcThreadAttributeList(NULL, 1, 0, &size) ||
1498+
GetLastError() == ERROR_INSUFFICIENT_BUFFER) &&
1499+
(attr_list = (LPPROC_THREAD_ATTRIBUTE_LIST)
1500+
(HeapAlloc(GetProcessHeap(), 0, size))) &&
1501+
InitializeProcThreadAttributeList(attr_list, 1, 0, &size) &&
1502+
UpdateProcThreadAttribute(attr_list, 0,
1503+
PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
1504+
stdhandles,
1505+
stdhandles_count * sizeof(HANDLE),
1506+
NULL, NULL)) {
1507+
si.lpAttributeList = attr_list;
1508+
flags |= EXTENDED_STARTUPINFO_PRESENT;
1509+
}
1510+
1511+
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL,
1512+
stdhandles_count ? TRUE : FALSE,
1513+
flags, wenvblk, dir ? wdir : NULL,
1514+
&si.StartupInfo, &pi);
1515+
1516+
/*
1517+
* On Windows 2008 R2, it seems that specifying certain types of handles
1518+
* (such as FILE_TYPE_CHAR or FILE_TYPE_PIPE) will always produce an
1519+
* error. Rather than playing finicky and fragile games, let's just try
1520+
* to detect this situation and simply try again without restricting any
1521+
* handle inheritance. This is still better than failing to create
1522+
* processes.
1523+
*/
1524+
if (!ret && restrict_handle_inheritance && stdhandles_count) {
1525+
DWORD err = GetLastError();
1526+
struct strbuf buf = STRBUF_INIT;
1527+
1528+
if (err != ERROR_NO_SYSTEM_RESOURCES &&
1529+
/*
1530+
* On Windows 7 and earlier, handles on pipes and character
1531+
* devices are inherited automatically, and cannot be
1532+
* specified in the thread handle list. Rather than trying
1533+
* to catch each and every corner case (and running the
1534+
* chance of *still* forgetting a few), let's just fall
1535+
* back to creating the process without trying to limit the
1536+
* handle inheritance.
1537+
*/
1538+
!(err == ERROR_INVALID_PARAMETER &&
1539+
GetVersion() >> 16 < 9200) &&
1540+
!getenv("SUPPRESS_HANDLE_INHERITANCE_WARNING")) {
1541+
DWORD fl = 0;
1542+
int i;
1543+
1544+
setenv("SUPPRESS_HANDLE_INHERITANCE_WARNING", "1", 1);
1545+
1546+
for (i = 0; i < stdhandles_count; i++) {
1547+
HANDLE h = stdhandles[i];
1548+
strbuf_addf(&buf, "handle #%d: %p (type %lx, "
1549+
"handle info (%d) %lx\n", i, h,
1550+
GetFileType(h),
1551+
GetHandleInformation(h, &fl),
1552+
fl);
1553+
}
1554+
strbuf_addstr(&buf, "\nThis is a bug; please report it "
1555+
"at\nhttps://github.com/git-for-windows/"
1556+
"git/issues/new\n\n"
1557+
"To suppress this warning, please set "
1558+
"the environment variable\n\n"
1559+
"\tSUPPRESS_HANDLE_INHERITANCE_WARNING=1"
1560+
"\n");
1561+
}
1562+
restrict_handle_inheritance = 0;
1563+
flags &= ~EXTENDED_STARTUPINFO_PRESENT;
1564+
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL,
1565+
TRUE, flags, wenvblk, dir ? wdir : NULL,
1566+
&si.StartupInfo, &pi);
1567+
if (ret && buf.len) {
1568+
errno = err_win_to_posix(GetLastError());
1569+
warning("failed to restrict file handles (%ld)\n\n%s",
1570+
err, buf.buf);
1571+
}
1572+
strbuf_release(&buf);
1573+
} else if (!ret)
1574+
errno = err_win_to_posix(GetLastError());
1575+
1576+
if (si.lpAttributeList)
1577+
DeleteProcThreadAttributeList(si.lpAttributeList);
1578+
if (attr_list)
1579+
HeapFree(GetProcessHeap(), 0, attr_list);
14811580

14821581
free(wenvblk);
14831582
free(wargs);
14841583

1485-
if (!ret) {
1486-
errno = ENOENT;
1584+
if (!ret)
14871585
return -1;
1488-
}
1586+
14891587
CloseHandle(pi.hThread);
14901588

14911589
/*

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)