Skip to content

Commit 02f173d

Browse files
authored
Include the hash of the SID in the Window Class and Mutant (#19109)
Right now, we do not use a sufficiently unique name to disambiguate Terminal instances running on the same desktop. Mutexes (mutants) are named objects that live in the user's session, under `Sessions\1\BaseNamedObjects` (for the local desktop session). When multiple users are logged into the same session--such as with "Run as different user"--they share a local BaseNamedObjects namespace. Ugh. We cannot use [`CreatePrivateNamespace`] as it requires a boundary descriptor, and the only boundary descriptors supported by the current API are based on package identity. I also fear that `CreatePrivateNamespace` is subject to a race condition with `OpenPrivateNamespace`; Create will not Open an existing one, so we would need to back off and retry either opening or creating. Yuck. After this commit, we will hash the user's SID into the name of both the window class and the mutant, right after the path hash (if running unpackaged). Closes #18704 [`CreatePrivateNamespace`]: https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createprivatenamespacea
1 parent ac07afe commit 02f173d

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/cascadia/WindowsTerminal/WindowEmperor.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <ScopedResourceLoader.h>
1010
#include <WtExeUtils.h>
1111
#include <til/hash.h>
12+
#include <wil/token_helpers.h>
1213

1314
#include "AppHost.h"
1415
#include "resource.h"
@@ -275,7 +276,7 @@ AppHost* WindowEmperor::_mostRecentWindow() const noexcept
275276
void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
276277
{
277278
std::wstring windowClassName;
278-
windowClassName.reserve(47); // "Windows Terminal Preview Admin 0123456789012345"
279+
windowClassName.reserve(64); // "Windows Terminal Preview Admin 0123456789012345 0123456789012345"
279280
#if defined(WT_BRANDING_RELEASE)
280281
windowClassName.append(L"Windows Terminal");
281282
#elif defined(WT_BRANDING_PREVIEW)
@@ -300,6 +301,18 @@ void WindowEmperor::HandleCommandlineArgs(int nCmdShow)
300301
#endif
301302
}
302303

304+
{
305+
wil::unique_handle processToken{ GetCurrentProcessToken() };
306+
const auto userTokenInfo{ wil::get_token_information<TOKEN_USER>(processToken.get()) };
307+
const auto sidLength{ GetLengthSid(userTokenInfo->User.Sid) };
308+
const auto hash{ til::hash(userTokenInfo->User.Sid, sidLength) };
309+
#ifdef _WIN64
310+
fmt::format_to(std::back_inserter(windowClassName), FMT_COMPILE(L" {:016x}"), hash);
311+
#else
312+
fmt::format_to(std::back_inserter(windowClassName), FMT_COMPILE(L" {:08x}"), hash);
313+
#endif
314+
}
315+
303316
// Windows Terminal is a single-instance application. Either acquire ownership
304317
// over the mutex, or hand off the command line to the existing instance.
305318
const auto mutex = acquireMutexOrAttemptHandoff(windowClassName.c_str(), nCmdShow);

0 commit comments

Comments
 (0)