Skip to content

Part of #1011: warn on Windows for the win32kfull.sys kernel Token leak - #1015

Merged
fdaviddpt merged 4 commits into
mainfrom
fix/1011
Oct 10, 2026
Merged

fdaviddpt merged 4 commits into
mainfrom
fix/1011

Conversation

@fdaviddpt

@fdaviddpt fdaviddpt commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Part of #1011.

Windows 11 25H2 leaks one kernel Token object per console-attached process start, traced by the reporter to win32kfull!CForegroundLaunch::_CheckAllowForeground -> PsReferencePrimaryToken, confirmed on two builds (26200.9457, 26300.9550) and reported upstream in two OPEN trackers with no fix from Microsoft. This is #1011's suggestion #1 only (a doctor.sh WARN plus a docs/windows.md section); suggestion #2, the fork-reduction half tracked separately as #660/#664-666/#914, is intentionally left for a separate lane -- #1011 remains open after this merge.

scripts/doctor.sh gains a new WARN-only section (outside the VERDICT ladder, matching the file's existing log-rotation/case-divergence WARN convention): it detects a known-affected Windows build (>=26200, the confirmed floor -- builds below that are UNKNOWN, not confirmed-unaffected) and whether the live foreground-lock timeout (SPI_GETFOREGROUNDLOCKTIMEOUT) is not 0. Each probe is bounded by a plain bash background-job + sleep-then-kill-9 watchdog (the external timeout binary isn't guaranteed present, mirroring lib-detach.sh's own watchdog reasoning), writes a tiny .ps1 file via mktemp+printf, and converts the script path with cygpath -w when available (falling back to the POSIX path otherwise) before invoking powershell.exe/powershell -File -- the same MSYS-POSIX-path hazard lib-detach.sh already guards for wscript.exe. A missing, slow or malformed PowerShell call always degrades to a could not determine / could not be read WARN, never a false OK -- this plugin never applies the workaround itself.

docs/windows.md gains a new section, following the file's existing flat bold-paragraph convention: the bug, both confirmed builds, both upstream citations, the confirmed workaround (SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0)) with its exact PowerShell snippet, the logon-task requirement (the live value resets at every logon), the focus-stealing trade-off, Restart-vs-Shut-down (Fast Startup's Shut down keeps already-leaked tokens), and a pointer to doctor.sh's own check -- "a known-affected build whose live timeout is not 0," matching what the code actually warns on.

docs/windows-skip-triage.md gains two new rows, one per new test file's win32-mentioning skipif, following the exact precedent already set for test_windows_real_wscript_1002.py's own inverted-skip row.

Review round 4 (this push):

  1. BLOCKING: real-Windows CI proof + fix. A diagnostic run on windows-latest (job #114307420708 on this same PR) measured elapsed=10.81s, returncode=0, empty stdout/stderr for the timeout probe alone -- more than double the 5s bash watchdog _remember_doctor_run_powershell_file hard-coded. That probe's Add-Type C# compile is the only thing distinguishing it from the build probe (no Add-Type), which passed reliably on the same hosts. Fixed by making the watchdog bound overridable per call (default 5), passing 20 only for the Add-Type-using timeout probe. doctor.sh is not a hot path, so the wider bound is a fair trade against a false WARN. The elapsed time and exit code captured during diagnosis stay in the real test's assertion message permanently, so a future regression here won't need a separate round-trip to tell a watchdog-bound issue apart from a fast failure.

  2. Dropped the three env-var knobs (_REMEMBER_DOCTOR_FORCE_WINDOWS / _REMEMBER_DOCTOR_WIN_BUILD_OVERRIDE / _REMEMBER_DOCTOR_FGLOCK_TIMEOUT_OVERRIDE) doctor.sh's production code used to read unconditionally -- a real user could set any one of them to suppress a genuine WARN. tests/test_doctor_windows_fglock_1011.py now drives the same six cases by setting OS=Windows_NT (the same real signal _remember_doctor_is_windows() already reads first) and putting a stub powershell.exe on PATH, matching how tests/test_windows_hidden_detach_1002.py already stubs wscript.exe. The stub's own env vars are read by the stub alone, never by doctor.sh. Same round: macos-latest CI was observed to carry a real powershell/pwsh reachable via command -v powershell, which answered the build probe for real with a non-Windows build value -- the "unavailable" test now forces the degrade path with a stub that deterministically fails, rather than relying on absence.

  3. "whose live timeout has not been changed from its default" read backwards from what the code warns on (any value other than 0, not merely a changed one) -- reworded to "is not 0" in docs/windows.md, the changelog fragment, a test docstring, and this PR body.

Tests: tests/test_doctor_windows_fglock_1011.py (6 unit tests on every platform via the OS=Windows_NT + stub-powershell.exe approach above: affected+nonzero timeout -> WARN positive control, affected+zero timeout -> OK, unaffected build -> OK, not-Windows -> section absent, broken-powershell -> WARN could not determine as a third state distinct from a false OK) and tests/test_windows_real_fglock_1011.py (CI-only, skipif sys.platform not win32, sources doctor.sh for real on windows-latest and calls both probes directly).

changelog.d/1011.added.md credits @NanoInG, checked against .oss/assemble_changelog.py --check.

Self-review (this round): Explore + oss:auditor spawned per agents/developer/review.md; tree_snapshot clean.

Red before green: before doctor.sh had the #1011 section, the test file's own _fglock_block() helper returned empty for every scenario; before this round's watchdog widening, test_real_timeout_probe_returns_an_integer failed on real windows-latest CI exactly as described in finding 1 above. Full relevant battery after this round's commit (442 passed, 0 failed): pytest tests/test_doctor_windows_fglock_1011.py tests/test_doctor.py tests/test_doctor_cap_disabled_360.py tests/test_windows_skip_triage_497.py tests/test_scanner_shapes_source_898.py tests/test_locale_ranges_695.py -q. Did not run the full pytest suite locally, per repo policy -- CI is the authority for the three-OS/four-interpreter matrix.

Cross-platform claims: OBSERVED the windows-latest and macos-latest failures and this round's fix via real CI runs on this same PR (jobs cited above). REASONED, not directly observed: the 20s bound is sized against one measured 10.81s sample, not a distribution -- generous rather than exact.

Out of scope, verified untouched: scripts/post-tool-hook.sh, tests/spawn_counting.py, scripts/lib-detach.sh (owned by another lane, #913). README.md needs no edit -- its existing generic Windows section already points at docs/windows.md.

[AI-generated]

fdaviddpt and others added 4 commits October 10, 2026 21:51
doctor.sh gains a WARN-only check (never FAIL, never touches the setting)
that fires on a known-affected Windows build (confirmed 26200.9457 and
26300.9550) whose live foreground lock timeout has not been changed from
its default. It points at a new docs/windows.md section covering the
confirmed workaround, its logon-task requirement and focus-stealing
trade-off, and Restart vs Shut down -- an opt-in system change the user
decides on, never applied by this plugin itself.

Build/timeout detection is two bounded PowerShell probes, gated behind
_REMEMBER_DOCTOR_FORCE_WINDOWS / _REMEMBER_DOCTOR_WIN_BUILD_OVERRIDE /
_REMEMBER_DOCTOR_FGLOCK_TIMEOUT_OVERRIDE test-only env hooks so the
decision logic runs on every CI platform, not only a real Windows host.
A second module drives the real PowerShell calls on windows-latest itself.

Credit: @NanoInG

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Both the review (Explore) and audit (oss:auditor) passes flagged
non-blocking items against the #1011 PowerShell probes in doctor.sh:

- The auditor found the two new functions hand powershell.exe an MSYS
  POSIX path for -File with no conversion, unlike lib-detach.sh's own
  wscript.exe launcher (which already guards the same hazard with
  cygpath -w). Fixed: convert when cygpath is on PATH, fall back to the
  POSIX path otherwise rather than refusing outright -- an unresolvable
  path still degrades to the existing "could not determine" WARN, never
  a false OK.
- The reviewer noted the mktemp+mv-to-.ps1 rename is a silent, safe
  degrade if mv fails (powershell -File then errors, caller reports its
  own WARN) but was undocumented. Added a one-line comment at each call
  site naming the failure mode for a future "always WARNs could-not-
  check on this host" report.

The PID-recycling watchdog race the reviewer also raised is a generic,
already-accepted bash-watchdog limitation (same shape as the #1002
precedent this comment borrows from) with an extremely narrow window;
left as-is.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
… real Windows timeout probe

The reviewer suspects the Add-Type C# compile in
_remember_doctor_fglock_timeout exceeds the 5s bash watchdog in
_remember_doctor_run_powershell_file, which kills the process and leaves
empty output -- exactly the failure seen on real windows-latest CI (jobs
114303128794, 114303128471): "timeout probe did not return a bare integer:
'' (stderr: '')". The build probe, which has no Add-Type call, passes
reliably on the same hosts.

This commit only adds elapsed-time and exit-code reporting to the failing
assertion so the next CI run tells watchdog-kill (elapsed ~5s) apart from a
fast failure (quoting/execution-policy, elapsed well under 5s) -- temporary,
to be removed once the cause is confirmed.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
… the suppressible knobs, fix backwards phrasing

1. BLOCKING: real-Windows CI proof + fix. A diagnostic run on
   windows-latest (this same PR, job #114307420708) measured
   elapsed=10.81s, returncode=0, empty stdout/stderr for
   _remember_doctor_fglock_timeout alone -- more than double the 5s bash
   watchdog _remember_doctor_run_powershell_file hard-coded. That probe's
   Add-Type C# compile is the only thing distinguishing it from the build
   probe (no Add-Type), which passed reliably on the same hosts. Fixed by
   making the watchdog bound overridable per call ($2, default 5) and
   passing 20 only for the Add-Type-using timeout probe; the build probe
   keeps the 5s default. doctor.sh is not a hot path, so the wider bound is
   a fair trade against a false "could not determine" WARN. The elapsed
   time and exit code captured during diagnosis stay in
   test_real_timeout_probe_returns_an_integer's assertion message
   permanently, at no extra cost, so a future regression here won't need a
   separate round-trip to tell a watchdog-bound issue apart from a fast
   failure.

2. Dropped the three env-var knobs
   (_REMEMBER_DOCTOR_FORCE_WINDOWS / _REMEMBER_DOCTOR_WIN_BUILD_OVERRIDE /
   _REMEMBER_DOCTOR_FGLOCK_TIMEOUT_OVERRIDE) doctor.sh's production code
   used to read unconditionally -- a real user could set any one of them
   to suppress a genuine WARN. tests/test_doctor_windows_fglock_1011.py
   now drives the same six cases by setting OS=Windows_NT (the same real
   signal _remember_doctor_is_windows() already reads first) and putting a
   stub powershell.exe on PATH, matching how
   tests/test_windows_hidden_detach_1002.py already stubs wscript.exe. The
   stub's own env vars (_TEST_STUB_FGLOCK_BUILD / _TEST_STUB_FGLOCK_TIMEOUT)
   are read by the stub alone, never by doctor.sh.

   Same round: macos-latest CI (jobs #114307420782/747/809/838, two
   compiled-hooks legs) was observed to carry a REAL powershell/pwsh
   reachable via `command -v powershell`, which answered doctor.sh's build
   probe for real with a non-Windows build value (seen as "2") -- the old
   "unavailable" test assumed no powershell existed on CI at all, which
   this runner image made false. test_powershell_unavailable_... now
   forces the degrade path with a stub that deterministically fails
   (exit 1) rather than relying on absence, so this holds regardless of
   what the runner image happens to ship.

3. docs/windows.md:56, the matching changelog.d/1011.added.md line and a
   test docstring all said "whose live timeout has not been changed from
   its default", backwards from what the code actually warns on (any
   value other than 0, not merely a changed one). Reworded all three to
   "whose live timeout is not 0" / "is not 0". PR #1015's own body and
   title are being corrected to match in the same round.

Full local run: pytest tests/test_doctor_windows_fglock_1011.py
tests/test_doctor.py tests/test_doctor_cap_disabled_360.py
tests/test_windows_skip_triage_497.py tests/test_scanner_shapes_source_898.py
tests/test_locale_ranges_695.py -q -> 442 passed. bash -n scripts/doctor.sh
and python3 .oss/assemble_changelog.py --check both clean. Did not run the
full pytest suite locally, per repo policy -- CI is the authority for the
three-OS/four-interpreter matrix.

Cross-platform claims: OBSERVED the windows-latest and macos-latest
failures and the fix's effect via real CI runs on this same PR (jobs cited
above). REASONED, not directly observed: the 20s bound is sized against one
measured 10.81s sample, not a distribution -- generous rather than exact.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@fdaviddpt
fdaviddpt merged commit 0c0f17c into main Oct 10, 2026
19 checks passed
@fdaviddpt
fdaviddpt deleted the fix/1011 branch October 10, 2026 21:33
fdaviddpt added a commit that referenced this pull request Oct 11, 2026
* Fix flaky #1011 timeout probe: compile-free P/Invoke

The live SPI_GETFOREGROUNDLOCKTIMEOUT probe in scripts/doctor.sh
(_remember_doctor_fglock_timeout) used Add-Type to compile an inline C#
P/Invoke declaration via csc.exe. That compile step was measured at
10.81s on real windows-latest CI (#1015 round 4) and exceeded the 20s
watchdog bound entirely in a later run (test_real_timeout_probe_returns_an_integer,
run 38110311076, job 114384312765), making the probe -- and the test
asserting it returns a bare integer -- flaky by host/compiler load
rather than by anything this plugin's own logic does.

Replace Add-Type with a compile-free P/Invoke built at runtime via
System.Reflection.Emit (DefineDynamicAssembly -> DefineDynamicModule ->
DefineType -> DefinePInvokeMethod("SystemParametersInfo", "user32.dll",
...) -> CreateType), called with the same uint/ref-uint signature as
before. No csc.exe process is spawned, so the probe no longer needs a
wider watchdog bound than the build probe above it -- both now share
the 5s default.

Degrade semantics are unchanged: a kill, a non-zero exit or empty
output from _remember_doctor_run_powershell_file still returns nothing,
which doctor.sh's callers already treat as "could not determine",
never a false OK.

Part of #1011.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* Add changelog fragment for the #1011 timeout-probe CI fix

Part of #1011.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* Fix #1011: set PreserveSig on the Reflection.Emit P/Invoke method

Self-review finding (Explore reviewer): DefinePInvokeMethod, unlike
Add-Type, does not infer MethodImplAttributes.PreserveSig for a
non-void P/Invoke return type. Without it the CLR applies
COM/HRESULT-style return marshaling to SystemParametersInfo (which
returns bool), which is expected to fail the call outright on every
real host -- not merely be slow, which is what this branch was
otherwise fixing. Added the missing
$methodBuilder.SetImplementationFlags(... -bor
[System.Reflection.MethodImplAttributes]::PreserveSig) call, matching
the documented MSDN DefinePInvokeMethod sample.

Also updates two comments the same reviewer flagged as now stale:
the _remember_doctor_run_powershell_file watchdog-bound comment (it
described the removed Add-Type compile time as the reason for a wider
bound, which no longer applies) and the matching comment in
tests/test_windows_real_fglock_1011.py::test_real_timeout_probe_returns_an_integer.

Part of #1011.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* Fix #1011 review: widen the PowerShell probe bound, reject a false 0

The 5s shared watchdog bound had no measured margin: CI only times whole
tests, each of which also sources doctor.sh, so no run isolated a probe.
Doctor is on-demand, so a 15s default costs nothing on a healthy host.

The real-host timeout test only asserted isdigit(), which accepts "0" --
the one value doctor.sh treats as "workaround applied" and so stays
silent. A stock CI host never applied the workaround, so assert > 0 too.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
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.

1 participant