api: make pipe handles non-inheritable by default

...and change to inheritable only the ones really needed, eliminating a
window where we'd have inheritable handles that are not supposed to be
inheritable.

Suggested-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman 2020-10-30 07:35:00 +01:00
parent fb6d5b62f1
commit 3dacd1c6ce

View File

@ -1397,19 +1397,16 @@ ExecuteRunDll32(
Result = ERROR_INVALID_PARAMETER; Result = ERROR_INVALID_PARAMETER;
goto cleanupDelete; goto cleanupDelete;
} }
SECURITY_ATTRIBUTES sa = { .nLength = sizeof(SECURITY_ATTRIBUTES),
.bInheritHandle = TRUE,
.lpSecurityDescriptor =
SecurityAttributes ? SecurityAttributes->lpSecurityDescriptor : NULL };
HANDLE StreamRStdout = INVALID_HANDLE_VALUE, StreamRStderr = INVALID_HANDLE_VALUE, HANDLE StreamRStdout = INVALID_HANDLE_VALUE, StreamRStderr = INVALID_HANDLE_VALUE,
StreamWStdout = INVALID_HANDLE_VALUE, StreamWStderr = INVALID_HANDLE_VALUE; StreamWStdout = INVALID_HANDLE_VALUE, StreamWStderr = INVALID_HANDLE_VALUE;
if (!CreatePipe(&StreamRStdout, &StreamWStdout, &sa, 0) || !CreatePipe(&StreamRStderr, &StreamWStderr, &sa, 0)) if (!CreatePipe(&StreamRStdout, &StreamWStdout, SecurityAttributes, 0) ||
!CreatePipe(&StreamRStderr, &StreamWStderr, SecurityAttributes, 0))
{ {
Result = LOG_LAST_ERROR(L"Failed to create pipes"); Result = LOG_LAST_ERROR(L"Failed to create pipes");
goto cleanupPipes; goto cleanupPipes;
} }
if (!SetHandleInformation(StreamRStdout, HANDLE_FLAG_INHERIT, 0) || if (!SetHandleInformation(StreamWStdout, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT) ||
!SetHandleInformation(StreamRStderr, HANDLE_FLAG_INHERIT, 0)) !SetHandleInformation(StreamWStderr, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
{ {
Result = LOG_LAST_ERROR(L"Failed to set handle info"); Result = LOG_LAST_ERROR(L"Failed to set handle info");
goto cleanupPipes; goto cleanupPipes;