Skip to content

Implement WSLC container group policies#40466

Merged
benhillis merged 2 commits into
masterfrom
user/benhill/wslc-policies
May 13, 2026
Merged

Implement WSLC container group policies#40466
benhillis merged 2 commits into
masterfrom
user/benhill/wslc-policies

Conversation

@benhillis
Copy link
Copy Markdown
Member

@benhillis benhillis commented May 8, 2026

Implement WSLC container group policies

Adds enforcement for two group policies whose ADMX templates landed in #40422:

  • AllowWSLContainer (DWORD): master switch for wslc.exe and the WSLCSessionManager COM API. Enforced inside WSLCSessionManagerFactory so CoCreateInstance returns WSL_E_CONTAINER_DISABLED directly when the policy is off; every caller (wslc.exe, WslcSDK, plugins) fails fast through one code path rather than getting a manager whose every method errors out individually.

  • WSLContainerCLIRegistryAllowlist: ADMX <list valuePrefix=\"AllowedRegistry\"> policy stored as a sub-key whose REG_SZ value data is each allowed registry hostname. Enforced in PullImage and PushImage via a shared helper. BuildImage is currently rejected outright when an allowlist is configured. This will be addressed in a follow-up change.

Adds two new HRESULTs:

  • WSL_E_CONTAINER_DISABLED (0x33)
  • WSL_E_REGISTRY_BLOCKED_BY_POLICY (0x34)

Adds PolicyTests covering disabled-state, CLI surfacing, allowlist denial, build rejection, and pure-function unit tests for the allowlist evaluator.

Copilot AI review requested due to automatic review settings May 8, 2026 16:23
@benhillis benhillis requested a review from craigloewen-msft May 8, 2026 16:27
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Implements enforcement for new WSLC-related group policies across the WSLC COM service boundary and CLI, including new HRESULTs and localized user-facing error messages, plus policy-focused tests.

Changes:

  • Enforce AllowWSLContainer at WSLCSessionManager COM activation time so clients fail fast with WSL_E_CONTAINER_DISABLED.
  • Implement WSLContainerCLIRegistryAllowlist evaluation and enforce it for image pull/push; block image builds when an allowlist is configured.
  • Add new HRESULTs and localized messages, plus new policy tests (including pure-function allowlist evaluator tests).

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/windows/PolicyTests.cpp Adds WSLC policy tests for container disable + registry allowlist behavior, including CLI coverage and evaluator unit tests.
src/windows/wslcsession/WSLCSession.cpp Enforces registry allowlist for Pull/Push and blocks BuildImage when allowlist is active.
src/windows/service/inc/wslservice.idl Defines WSL_E_CONTAINER_DISABLED and WSL_E_REGISTRY_BLOCKED_BY_POLICY.
src/windows/service/exe/WSLCSessionManagerFactory.cpp Gates COM factory creation on AllowWSLContainer policy.
src/windows/inc/wslpolicies.h Adds allowlist subkey constants and helper functions to read/evaluate the registry allowlist.
src/windows/common/wslutil.cpp Registers new HRESULTs in common error mapping and adds a localized string for WSL_E_CONTAINER_DISABLED.
localization/strings/en-US/Resources.resw Adds localized strings for the new policy-related user-facing error messages.

Comment thread test/windows/PolicyTests.cpp Outdated
Comment thread src/windows/inc/wslpolicies.h Outdated
Comment thread test/windows/PolicyTests.cpp
Comment thread src/windows/inc/wslpolicies.h Outdated
Comment thread src/windows/inc/wslpolicies.h Outdated
@benhillis benhillis force-pushed the user/benhill/wslc-policies branch from 08e7ed9 to cf811de Compare May 8, 2026 16:40
@benhillis benhillis requested a review from Copilot May 8, 2026 16:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Comment thread src/windows/service/exe/WSLCSessionManagerFactory.cpp Outdated
Comment thread src/windows/inc/wslpolicies.h
@benhillis benhillis force-pushed the user/benhill/wslc-policies branch from cf811de to 5891de1 Compare May 8, 2026 17:31
Copilot AI review requested due to automatic review settings May 8, 2026 21:09
@benhillis benhillis force-pushed the user/benhill/wslc-policies branch from 5891de1 to 753c107 Compare May 8, 2026 21:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Comment thread src/windows/inc/wslpolicies.h
Comment thread test/windows/PolicyTests.cpp
Comment thread src/windows/wslcsession/WSLCSession.cpp
Comment thread src/windows/wslcsession/WSLCSession.cpp
Adds enforcement for two group policies whose ADMX templates landed in
PR #40422:

- AllowWSLContainer (DWORD): master switch for wslc.exe and the
  WSLCSessionManager COM API. Enforced inside WSLCSessionManagerFactory
  so CoCreateInstance returns WSL_E_CONTAINER_DISABLED directly when the
  policy is off; every caller (wslc.exe, WslcSDK, plugins) fails fast
  through one code path rather than getting a manager whose every method
  errors out individually.

- WSLContainerRegistryAllowlist: ADMX `<list valuePrefix=\"AllowedRegistry\">`
  policy stored as a sub-key whose REG_SZ value data is each allowed
  registry hostname. Enforced in PullImage and PushImage via a shared
  helper. BuildImage is rejected outright when an allowlist is
  configured, since the in-VM docker daemon fetches FROM base images
  through its own pull mechanism and cannot be reliably gated
  per-registry. The policy is fail-open: an absent or empty sub-key is
  treated as no restriction, and registry I/O errors fall through to
  allow rather than break all container operations on a transient
  hiccup.

Adds two new HRESULTs:
- WSL_E_CONTAINER_DISABLED (0x33)
- WSL_E_REGISTRY_BLOCKED_BY_POLICY (0x34)

Adds PolicyTests covering disabled-state, CLI surfacing, allowlist
denial, build rejection, and pure-function unit tests for the allowlist
evaluator.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@benhillis benhillis force-pushed the user/benhill/wslc-policies branch from 753c107 to 8a6fcae Compare May 8, 2026 22:46
dkbennett
dkbennett previously approved these changes May 11, 2026
Copy link
Copy Markdown
Member

@dkbennett dkbennett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like blocking at the session level, though should we also have the CLI check the container disabled policy at the beginning of any execution for user friendliness? Most commands do use the session, but some don't, like "--help". IMO, it would be nice for user friendliness and to not give the illusion to the user that the CLI may be functional and when anything of value is disabled by policy.

@benhillis
Copy link
Copy Markdown
Member Author

I like blocking at the session level, though should we also have the CLI check the container disabled policy at the beginning of any execution for user friendliness? Most commands do use the session, but some don't, like "--help". IMO, it would be nice for user friendliness and to not give the illusion to the user that the CLI may be functional and when anything of value is disabled by policy.

Yep we definitely could, I'm not against belt and suspenders, we can tweak the UX of wslc.exe in a follow-up.

@benhillis benhillis marked this pull request as ready for review May 11, 2026 17:57
Copilot AI review requested due to automatic review settings May 11, 2026 17:57
@benhillis benhillis requested a review from a team as a code owner May 11, 2026 17:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Comments suppressed due to low confidence (1)

src/windows/common/wslutil.cpp:1

  • WSL_E_REGISTRY_BLOCKED_BY_POLICY is added to g_commonErrors, but GetErrorString does not map it to a user-friendly message. If this HRESULT ever surfaces without a user-error payload (e.g., different caller path), users may see a generic HRESULT string. Consider adding a generic localized message for this HRESULT (without requiring the registry hostname placeholder) and returning it here.
/*++

Comment thread test/windows/PolicyTests.cpp
Comment thread test/windows/PolicyTests.cpp
Comment thread src/windows/inc/wslpolicies.h
Comment thread src/windows/inc/wslpolicies.h
* SetRegistryAllowlist: delete the WSLContainerRegistryAllowlist
  sub-key before recreating it so stale AllowedRegistryN values from
  a previous (possibly interrupted) test run can't leak into the
  current test.
* GetWslcExePath: avoid std::optional::value() throwing
  bad_optional_access; use THROW_HR_IF_MSG with a clear diagnostic
  when the MSI install location can't be read from the registry.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@benhillis
Copy link
Copy Markdown
Member Author

@OneBlue - I loaded up the .admx file and verified that this multi subkey format is correct.

Copy link
Copy Markdown
Collaborator

@OneBlue OneBlue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, minor comments


std::wstring nameBuffer(static_cast<size_t>(maxValueNameChars) + 1, L'\0');
std::wstring dataBuffer(maxValueDataBytes / sizeof(wchar_t) + 1, L'\0');
for (DWORD index = 0;; ++index)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We could simplify this a bit by using wsl::windows::common::registry::EnumValues() like PluginManager::LoadPlugins() does for instance

It might also be worth creating a helper that load everything and returns a map of name -> value

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I considered that, but the rest of the policies header was using raw registry apis so I kept it like that. Let me look at this in a follow-up.

cpp_quote("#define WSL_E_DISTRIBUTION_NAME_NEEDED MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, WSL_E_BASE + 0x30) /* 0x80040330 */")
cpp_quote("#define WSL_E_INVALID_JSON MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, WSL_E_BASE + 0x31) /* 0x80040331 */")
cpp_quote("#define WSL_E_VM_CRASHED MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, WSL_E_BASE + 0x32) /* 0x80040332 */")
cpp_quote("#define WSL_E_CONTAINER_DISABLED MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, WSL_E_BASE + 0x33) /* 0x80040333 */")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These new error codes should be added to wslcsdk.h

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, will address in a follow-up.

auto [stdoutText, stderrText, exitCode] = LxsstuLaunchCommandAndCaptureOutputWithResult(cmd.data(), nullptr, nullptr);

VERIFY_ARE_NOT_EQUAL(0, exitCode);
const std::wstring combined = stdoutText + stderrText;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd have a preference towards validating the exact error message, and which HANDLE it was written to, so the behavior is locked in

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call will address in a follow-up.

@benhillis benhillis merged commit aa585a2 into master May 13, 2026
11 checks passed
@benhillis benhillis deleted the user/benhill/wslc-policies branch May 13, 2026 19:43
benhillis pushed a commit that referenced this pull request May 13, 2026
Three items @OneBlue flagged in the merged PR were tagged "follow-up";
this change addresses all of them.

1. Refactor EnumerateRegistryAllowlist to use shared registry helpers
   - Add wsl::windows::common::registry::EnumStringValues(HKEY) returning
     a name->value map for REG_SZ/REG_EXPAND_SZ values (skipping other
     types). Mirrors the suggestion to centralise the EnumValues+ReadString
     pattern used by PluginManager::LoadPlugins.
   - wslpolicies.h's EnumerateRegistryAllowlist now calls EnumStringValues
     instead of hand-rolling RegQueryInfoKeyW + RegEnumValueW. Empty-entry
     filter and fail-open catch are preserved.
   - wslpolicies.h now explicitly includes registry.hpp instead of relying
     on precomp include order.

2. Surface new HRESULTs in wslcsdk.h
   - Declare WSL_E_CONTAINER_DISABLED and WSL_E_REGISTRY_BLOCKED_BY_POLICY
     in wslcsdk.h so SDK consumers can reference them by name. Definitions
     are guarded with #ifndef and use the exact MAKE_HRESULT replacement
     text from wslservice.idl, so co-existence with the generated
     wslservice_h.h is safe in either include order.

3. Tighten WSLContainerDisabledCli test
   - Validate stdoutText is empty (locks down which HANDLE the disabled
     message goes to).
   - Validate stderrText equals exactly
     MessageWSLContainerDisabled() + "\r\nError code: WSL_E_CONTAINER_DISABLED\r\n"
     using the localization helper, so the message text and the error-code
     mapping are both locked in.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
benhillis pushed a commit that referenced this pull request May 13, 2026
Three items @OneBlue flagged in the merged PR were tagged "follow-up";
this change addresses all of them.

1. Refactor EnumerateRegistryAllowlist to use shared registry helpers
   - Add wsl::windows::common::registry::EnumStringValues(HKEY) returning
     a name->value map for REG_SZ/REG_EXPAND_SZ values (skipping other
     types). Mirrors the suggestion to centralise the EnumValues+ReadString
     pattern used by PluginManager::LoadPlugins.
   - wslpolicies.h's EnumerateRegistryAllowlist now calls EnumStringValues
     instead of hand-rolling RegQueryInfoKeyW + RegEnumValueW. Empty-entry
     filter and fail-open catch are preserved.
   - wslpolicies.h now explicitly includes registry.hpp instead of relying
     on precomp include order.

2. Reclassify the new HRESULTs as WSLC_E_* and surface them in wslcsdk.h
   - Move WSL_E_CONTAINER_DISABLED / WSL_E_REGISTRY_BLOCKED_BY_POLICY out of
     wslservice.idl and redefine them as WSLC_E_CONTAINER_DISABLED (0x8004060C)
     and WSLC_E_REGISTRY_BLOCKED_BY_POLICY (0x8004060D) in wslc.idl alongside
     the rest of the WSLC_E_* block.
   - Mirror the definitions in wslcsdk.h so SDK consumers can reference them
     by name without depending on the generated wslservice_h.h.
   - Update the service factory, wslcsession, wslutil error-code map, and
     PolicyTests to use the new names.

3. Tighten WSLContainerDisabledCli test
   - Validate stdoutText is empty (locks down which HANDLE the disabled
     message goes to).
   - Validate stderrText equals exactly
     MessageWSLContainerDisabled() + "\r\nError code: WSLC_E_CONTAINER_DISABLED\r\n"
     using the localization helper, so the message text and the error-code
     mapping are both locked in.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
benhillis pushed a commit that referenced this pull request May 13, 2026
Three items @OneBlue flagged in the merged PR were tagged "follow-up";
this change addresses all of them.

1. Refactor EnumerateRegistryAllowlist to use shared registry helpers
   - Add wsl::windows::common::registry::EnumStringValues(HKEY) returning
     a name->value map for REG_SZ/REG_EXPAND_SZ values (skipping other
     types). Mirrors the suggestion to centralise the EnumValues+ReadString
     pattern used by PluginManager::LoadPlugins.
   - wslpolicies.h's EnumerateRegistryAllowlist now calls EnumStringValues
     instead of hand-rolling RegQueryInfoKeyW + RegEnumValueW. Empty-entry
     filter and fail-open catch are preserved.
   - wslpolicies.h now explicitly includes registry.hpp instead of relying
     on precomp include order.

2. Reclassify the new HRESULTs as WSLC_E_* and surface them in wslcsdk.h
   - Move WSL_E_CONTAINER_DISABLED / WSL_E_REGISTRY_BLOCKED_BY_POLICY out of
     wslservice.idl and redefine them as WSLC_E_CONTAINER_DISABLED (0x8004060C)
     and WSLC_E_REGISTRY_BLOCKED_BY_POLICY (0x8004060D) in wslc.idl alongside
     the rest of the WSLC_E_* block.
   - Mirror the definitions in wslcsdk.h so SDK consumers can reference them
     by name without depending on the generated wslservice_h.h.
   - Update the service factory, wslcsession, wslutil error-code map, and
     PolicyTests to use the new names.

3. Tighten WSLContainerDisabledCli test
   - Validate stdoutText is empty (locks down which HANDLE the disabled
     message goes to).
   - Validate stderrText equals exactly
     MessageWSLContainerDisabled() + "\r\nError code: WSLC_E_CONTAINER_DISABLED\r\n"
     using the localization helper, so the message text and the error-code
     mapping are both locked in.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
benhillis added a commit that referenced this pull request May 13, 2026
* Address WSLC policy review follow-ups from #40466

Three items @OneBlue flagged in the merged PR were tagged "follow-up";
this change addresses all of them.

1. Refactor EnumerateRegistryAllowlist to use shared registry helpers
   - Add wsl::windows::common::registry::EnumStringValues(HKEY) returning
     a name->value map for REG_SZ/REG_EXPAND_SZ values (skipping other
     types). Mirrors the suggestion to centralise the EnumValues+ReadString
     pattern used by PluginManager::LoadPlugins.
   - wslpolicies.h's EnumerateRegistryAllowlist now calls EnumStringValues
     instead of hand-rolling RegQueryInfoKeyW + RegEnumValueW. Empty-entry
     filter and fail-open catch are preserved.
   - wslpolicies.h now explicitly includes registry.hpp instead of relying
     on precomp include order.

2. Reclassify the new HRESULTs as WSLC_E_* and surface them in wslcsdk.h
   - Move WSL_E_CONTAINER_DISABLED / WSL_E_REGISTRY_BLOCKED_BY_POLICY out of
     wslservice.idl and redefine them as WSLC_E_CONTAINER_DISABLED (0x8004060C)
     and WSLC_E_REGISTRY_BLOCKED_BY_POLICY (0x8004060D) in wslc.idl alongside
     the rest of the WSLC_E_* block.
   - Mirror the definitions in wslcsdk.h so SDK consumers can reference them
     by name without depending on the generated wslservice_h.h.
   - Update the service factory, wslcsession, wslutil error-code map, and
     PolicyTests to use the new names.

3. Tighten WSLContainerDisabledCli test
   - Validate stdoutText is empty (locks down which HANDLE the disabled
     message goes to).
   - Validate stderrText equals exactly
     MessageWSLContainerDisabled() + "\r\nError code: WSLC_E_CONTAINER_DISABLED\r\n"
     using the localization helper, so the message text and the error-code
     mapping are both locked in.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Remove unused CreatePoliciesKey helper from wslpolicies.h

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants