WSLC: sanitize HostingProcessNameSuffix to keep vmmem process name well-formed (#40471)

The HCS HostingProcessNameSuffix becomes the vmmem-XXX process name visible in Task Manager and parsed by various tooling. When the caller's DisplayName contains spaces, unicode, or other non-ASCII characters (which can easily happen because default session names are derived from the caller's username via LookupAccountSidW), it produces a malformed process name. Fix: when assigning the suffix, replace any character outside the conservative ASCII allowlist [A-Za-z0-9._-] with '_'. Settings->DisplayName itself (used for the HCS Owner field, session lookup, etc.) is left untouched so existing session-naming behavior is unchanged. Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Ben Hillis committed May 11, 2026 at 11:53 UTC 1a3e068f5b4c895f6f2ddd04e70f42ea4095b266
1 file changed +28 -1
src/windows/service/exe/HcsVirtualMachine.cpp
+28 -1
@@ -14,6 +14,8 @@ Abstract:
14
15 #include "HcsVirtualMachine.h"
16 #include <format>
17 +#include <string>
18 +#include <string_view>
19 #include "hcs_schema.h"
20 #include "VirtioNetworking.h"
21 #include "NatNetworking.h"
@@ -30,6 +32,28 @@ constexpr auto MAX_VM_CRASH_FILES = 3;
32 constexpr auto SAVED_STATE_FILE_EXTENSION = L".vmrs";
33 constexpr auto SAVED_STATE_FILE_PREFIX = L"saved-state-";
34
35 +namespace {
36 +
37 +// Replace any character outside the conservative ASCII allowlist with '_' so the
38 +// result is safe to use as the HCS HostingProcessNameSuffix (which becomes the
39 +// vmmem-XXX process name visible in Task Manager and parsed by various tooling).
40 +std::wstring SanitizeHostingProcessNameSuffix(std::wstring_view name)
41 +{
42 + constexpr std::wstring_view c_allowed = L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
43 + std::wstring sanitized{name};
44 + for (auto& c : sanitized)
45 + {
46 + if (c_allowed.find(c) == std::wstring_view::npos)
47 + {
48 + c = L'_';
49 + }
50 + }
51 +
52 + return sanitized;
53 +}
54 +
55 +} // namespace
56 +
57 HcsVirtualMachine::HcsVirtualMachine(_In_ const WSLCSessionSettings* Settings)
58 {
59 THROW_HR_IF(E_POINTER, Settings == nullptr);
@@ -95,7 +119,10 @@ HcsVirtualMachine::HcsVirtualMachine(_In_ const WSLCSessionSettings* Settings)
119
120 if (helpers::IsVmemmSuffixSupported() && Settings->DisplayName)
121 {
98 - vmSettings.ComputeTopology.Memory.HostingProcessNameSuffix = Settings->DisplayName;
122 + // The vmmem-XXX process name shown in Task Manager (and parsed by tooling)
123 + // can't tolerate spaces / unicode / etc., so sanitize before use. Note that
124 + // Settings->DisplayName itself (e.g. the HCS Owner) is left untouched.
125 + vmSettings.ComputeTopology.Memory.HostingProcessNameSuffix = SanitizeHostingProcessNameSuffix(Settings->DisplayName);
126 }
127
128 #ifdef _AMD64_