ConPTY: Raise the MAX_PATH limit (#17768)

Swapped the `swprintf_s` with no failure checks against a
`str_printf_nothrow` with checks. I also deduplicated the
`CreateProcess` calls since they're mostly identical.

Closes #16860
This commit is contained in:
Leonard Hecker 2024-08-22 18:32:11 +02:00 committed by GitHub
parent 56cfb77c6d
commit b3f41626b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -93,7 +93,7 @@ static bool _HandleIsValid(HANDLE h) noexcept
return (h != INVALID_HANDLE_VALUE) && (h != nullptr); return (h != INVALID_HANDLE_VALUE) && (h != nullptr);
} }
HRESULT _CreatePseudoConsole(const HANDLE hToken, HRESULT _CreatePseudoConsole(HANDLE hToken,
const COORD size, const COORD size,
const HANDLE hInput, const HANDLE hInput,
const HANDLE hOutput, const HANDLE hOutput,
@ -109,6 +109,12 @@ HRESULT _CreatePseudoConsole(const HANDLE hToken,
return E_INVALIDARG; return E_INVALIDARG;
} }
// CreateProcessAsUserW expects the token to be either valid or null.
if (hToken == INVALID_HANDLE_VALUE)
{
hToken = nullptr;
}
wil::unique_handle serverHandle; wil::unique_handle serverHandle;
RETURN_IF_NTSTATUS_FAILED(CreateServerHandle(serverHandle.addressof(), TRUE)); RETURN_IF_NTSTATUS_FAILED(CreateServerHandle(serverHandle.addressof(), TRUE));
@ -132,9 +138,6 @@ HRESULT _CreatePseudoConsole(const HANDLE hToken,
RETURN_IF_WIN32_BOOL_FALSE(CreatePipe(signalPipeConhostSide.addressof(), signalPipeOurSide.addressof(), &sa, 0)); RETURN_IF_WIN32_BOOL_FALSE(CreatePipe(signalPipeConhostSide.addressof(), signalPipeOurSide.addressof(), &sa, 0));
RETURN_IF_WIN32_BOOL_FALSE(SetHandleInformation(signalPipeConhostSide.get(), HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT)); RETURN_IF_WIN32_BOOL_FALSE(SetHandleInformation(signalPipeConhostSide.get(), HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT));
// GH4061: Ensure that the path to executable in the format is escaped so C:\Program.exe cannot collide with C:\Program Files
// This is plenty of space to hold the formatted string
wchar_t cmd[MAX_PATH]{};
const BOOL bInheritCursor = (dwFlags & PSEUDOCONSOLE_INHERIT_CURSOR) == PSEUDOCONSOLE_INHERIT_CURSOR; const BOOL bInheritCursor = (dwFlags & PSEUDOCONSOLE_INHERIT_CURSOR) == PSEUDOCONSOLE_INHERIT_CURSOR;
const wchar_t* textMeasurement; const wchar_t* textMeasurement;
@ -154,16 +157,21 @@ HRESULT _CreatePseudoConsole(const HANDLE hToken,
break; break;
} }
swprintf_s(cmd, const auto conhostPath = _ConsoleHostPath();
MAX_PATH,
L"\"%s\" --headless %s%s--width %hd --height %hd --signal 0x%tx --server 0x%tx", // GH4061: Ensure that the path to executable in the format is escaped so C:\Program.exe cannot collide with C:\Program Files
_ConsoleHostPath(), // This is plenty of space to hold the formatted string
bInheritCursor ? L"--inheritcursor " : L"", wil::unique_process_heap_string cmd;
textMeasurement, RETURN_IF_FAILED(wil::str_printf_nothrow(
size.X, cmd,
size.Y, L"\"%s\" --headless %s%s--width %hd --height %hd --signal 0x%tx --server 0x%tx",
std::bit_cast<uintptr_t>(signalPipeConhostSide.get()), conhostPath,
std::bit_cast<uintptr_t>(serverHandle.get())); bInheritCursor ? L"--inheritcursor " : L"",
textMeasurement,
size.X,
size.Y,
std::bit_cast<uintptr_t>(signalPipeConhostSide.get()),
std::bit_cast<uintptr_t>(serverHandle.get())));
STARTUPINFOEXW siEx{ 0 }; STARTUPINFOEXW siEx{ 0 };
siEx.StartupInfo.cb = sizeof(STARTUPINFOEXW); siEx.StartupInfo.cb = sizeof(STARTUPINFOEXW);
@ -205,7 +213,8 @@ HRESULT _CreatePseudoConsole(const HANDLE hToken,
nullptr, nullptr,
nullptr)); nullptr));
wil::unique_process_information pi; wil::unique_process_information pi;
{ // wow64 disabled filesystem redirection scope {
// wow64 disabled filesystem redirection scope
#if defined(BUILD_WOW6432) #if defined(BUILD_WOW6432)
PVOID RedirectionFlag; PVOID RedirectionFlag;
RETURN_IF_NTSTATUS_FAILED(RtlWow64EnableFsRedirectionEx( RETURN_IF_NTSTATUS_FAILED(RtlWow64EnableFsRedirectionEx(
@ -215,35 +224,20 @@ HRESULT _CreatePseudoConsole(const HANDLE hToken,
RtlWow64EnableFsRedirectionEx(RedirectionFlag, &RedirectionFlag); RtlWow64EnableFsRedirectionEx(RedirectionFlag, &RedirectionFlag);
}); });
#endif #endif
if (hToken == INVALID_HANDLE_VALUE || hToken == nullptr)
{ // Call create process
// Call create process RETURN_IF_WIN32_BOOL_FALSE(CreateProcessAsUserW(
RETURN_IF_WIN32_BOOL_FALSE(CreateProcessW(_ConsoleHostPath(), hToken,
cmd, conhostPath,
nullptr, cmd.get(),
nullptr, nullptr,
TRUE, nullptr,
EXTENDED_STARTUPINFO_PRESENT, TRUE,
nullptr, EXTENDED_STARTUPINFO_PRESENT,
nullptr, nullptr,
&siEx.StartupInfo, nullptr,
pi.addressof())); &siEx.StartupInfo,
} pi.addressof()));
else
{
// Call create process
RETURN_IF_WIN32_BOOL_FALSE(CreateProcessAsUserW(hToken,
_ConsoleHostPath(),
cmd,
nullptr,
nullptr,
TRUE,
EXTENDED_STARTUPINFO_PRESENT,
nullptr,
nullptr,
&siEx.StartupInfo,
pi.addressof()));
}
} }
pPty->hSignal = signalPipeOurSide.release(); pPty->hSignal = signalPipeOurSide.release();