Skip to content

Commit

Permalink
ConPTY: Raise the MAX_PATH limit (microsoft#17768)
Browse files Browse the repository at this point in the history
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 microsoft#16860
  • Loading branch information
lhecker authored Aug 22, 2024
1 parent 56cfb77 commit b3f4162
Showing 1 changed file with 38 additions and 44 deletions.
82 changes: 38 additions & 44 deletions src/winconpty/winconpty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static bool _HandleIsValid(HANDLE h) noexcept
return (h != INVALID_HANDLE_VALUE) && (h != nullptr);
}

HRESULT _CreatePseudoConsole(const HANDLE hToken,
HRESULT _CreatePseudoConsole(HANDLE hToken,
const COORD size,
const HANDLE hInput,
const HANDLE hOutput,
Expand All @@ -109,6 +109,12 @@ HRESULT _CreatePseudoConsole(const HANDLE hToken,
return E_INVALIDARG;
}

// CreateProcessAsUserW expects the token to be either valid or null.
if (hToken == INVALID_HANDLE_VALUE)
{
hToken = nullptr;
}

wil::unique_handle serverHandle;
RETURN_IF_NTSTATUS_FAILED(CreateServerHandle(serverHandle.addressof(), TRUE));

Expand All @@ -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(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 wchar_t* textMeasurement;
Expand All @@ -154,16 +157,21 @@ HRESULT _CreatePseudoConsole(const HANDLE hToken,
break;
}

swprintf_s(cmd,
MAX_PATH,
L"\"%s\" --headless %s%s--width %hd --height %hd --signal 0x%tx --server 0x%tx",
_ConsoleHostPath(),
bInheritCursor ? L"--inheritcursor " : L"",
textMeasurement,
size.X,
size.Y,
std::bit_cast<uintptr_t>(signalPipeConhostSide.get()),
std::bit_cast<uintptr_t>(serverHandle.get()));
const auto conhostPath = _ConsoleHostPath();

// 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
wil::unique_process_heap_string cmd;
RETURN_IF_FAILED(wil::str_printf_nothrow(
cmd,
L"\"%s\" --headless %s%s--width %hd --height %hd --signal 0x%tx --server 0x%tx",
conhostPath,
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 };
siEx.StartupInfo.cb = sizeof(STARTUPINFOEXW);
Expand Down Expand Up @@ -205,7 +213,8 @@ HRESULT _CreatePseudoConsole(const HANDLE hToken,
nullptr,
nullptr));
wil::unique_process_information pi;
{ // wow64 disabled filesystem redirection scope
{
// wow64 disabled filesystem redirection scope
#if defined(BUILD_WOW6432)
PVOID RedirectionFlag;
RETURN_IF_NTSTATUS_FAILED(RtlWow64EnableFsRedirectionEx(
Expand All @@ -215,35 +224,20 @@ HRESULT _CreatePseudoConsole(const HANDLE hToken,
RtlWow64EnableFsRedirectionEx(RedirectionFlag, &RedirectionFlag);
});
#endif
if (hToken == INVALID_HANDLE_VALUE || hToken == nullptr)
{
// Call create process
RETURN_IF_WIN32_BOOL_FALSE(CreateProcessW(_ConsoleHostPath(),
cmd,
nullptr,
nullptr,
TRUE,
EXTENDED_STARTUPINFO_PRESENT,
nullptr,
nullptr,
&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()));
}

// Call create process
RETURN_IF_WIN32_BOOL_FALSE(CreateProcessAsUserW(
hToken,
conhostPath,
cmd.get(),
nullptr,
nullptr,
TRUE,
EXTENDED_STARTUPINFO_PRESENT,
nullptr,
nullptr,
&siEx.StartupInfo,
pi.addressof()));
}

pPty->hSignal = signalPipeOurSide.release();
Expand Down

0 comments on commit b3f4162

Please sign in to comment.