Skip to content

Commit 9a780a3

Browse files
dschogitster
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 VFS for 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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c5a03b1 commit 9a780a3

File tree

2 files changed

+110
-12
lines changed

2 files changed

+110
-12
lines changed

compat/mingw.c

+109-11
Original file line numberDiff line numberDiff line change
@@ -1398,8 +1398,13 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
13981398
const char *dir,
13991399
int prepend_cmd, int fhin, int fhout, int fherr)
14001400
{
1401-
STARTUPINFOW si;
1401+
static int restrict_handle_inheritance = 1;
1402+
STARTUPINFOEXW si;
14021403
PROCESS_INFORMATION pi;
1404+
LPPROC_THREAD_ATTRIBUTE_LIST attr_list = NULL;
1405+
HANDLE stdhandles[3];
1406+
DWORD stdhandles_count = 0;
1407+
SIZE_T size;
14031408
struct strbuf args;
14041409
wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL;
14051410
unsigned flags = CREATE_UNICODE_ENVIRONMENT;
@@ -1435,11 +1440,23 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14351440
CloseHandle(cons);
14361441
}
14371442
memset(&si, 0, sizeof(si));
1438-
si.cb = sizeof(si);
1439-
si.dwFlags = STARTF_USESTDHANDLES;
1440-
si.hStdInput = winansi_get_osfhandle(fhin);
1441-
si.hStdOutput = winansi_get_osfhandle(fhout);
1442-
si.hStdError = winansi_get_osfhandle(fherr);
1443+
si.StartupInfo.cb = sizeof(si);
1444+
si.StartupInfo.hStdInput = winansi_get_osfhandle(fhin);
1445+
si.StartupInfo.hStdOutput = winansi_get_osfhandle(fhout);
1446+
si.StartupInfo.hStdError = winansi_get_osfhandle(fherr);
1447+
1448+
/* The list of handles cannot contain duplicates */
1449+
if (si.StartupInfo.hStdInput != INVALID_HANDLE_VALUE)
1450+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdInput;
1451+
if (si.StartupInfo.hStdOutput != INVALID_HANDLE_VALUE &&
1452+
si.StartupInfo.hStdOutput != si.StartupInfo.hStdInput)
1453+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdOutput;
1454+
if (si.StartupInfo.hStdError != INVALID_HANDLE_VALUE &&
1455+
si.StartupInfo.hStdError != si.StartupInfo.hStdInput &&
1456+
si.StartupInfo.hStdError != si.StartupInfo.hStdOutput)
1457+
stdhandles[stdhandles_count++] = si.StartupInfo.hStdError;
1458+
if (stdhandles_count)
1459+
si.StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
14431460

14441461
if (*argv && !strcmp(cmd, *argv))
14451462
wcmd[0] = L'\0';
@@ -1472,16 +1489,97 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
14721489
wenvblk = make_environment_block(deltaenv);
14731490

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

14781577
free(wenvblk);
14791578
free(wargs);
14801579

1481-
if (!ret) {
1482-
errno = ENOENT;
1580+
if (!ret)
14831581
return -1;
1484-
}
1582+
14851583
CloseHandle(pi.hThread);
14861584

14871585
/*

t/t0061-run-command.sh

+1-1
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)