Skip to content

TLS: harden take_slot_zero against TEB UAF and stale MOVED entries - #1083

Open
AlpPesen wants to merge 1 commit into
xcl-dev:mainfrom
AlpPesen:fix/tls-take-slot-zero-safety
Open

AlpPesen wants to merge 1 commit into
xcl-dev:mainfrom
AlpPesen:fix/tls-take-slot-zero-safety

Conversation

@AlpPesen

@AlpPesen AlpPesen commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Hardens tls::take_slot_zero and MOVED tracking against two concurrency bugs from #106:

  1. TEB / ThreadLocalStoragePointer UAF during take_slot_zero: previously thread_tebs() closed every thread handle immediately after reading ThreadBasicInformation, then take_slot_zero dereferenced the raw TEBs and wrote *arr = block after Sleep(20) without verifying the thread was still alive. Publishing XCL_SLOT only after the snapshot also left a window where concurrently starting or exiting threads bypassed MOVED serialization.
  2. Stale MOVED entries after TerminateThread: MOVED stored only raw TEB addresses (Vec<usize>). A thread killed without DLL_THREAD_DETACH left its TEB address in MOVED permanently, causing exchange_for_new_thread to skip swapping slot 0 and slot k when Windows later recycled that TEB address for a new thread.

Part of #106. (Disjoint from open TLS PRs #196, #431, and #464: only touches MOVED, take_slot_zero, exchange_for_new_thread, and on_thread_detach, and places new tests in a separate integration test file.)

What is in it

  • crates/xcl/src/tls.rs:
    • Replace MOVED: Mutex<Vec<usize>> with MovedTeb { teb, tid, handle: ThreadHandle } (Drop closes the handle). Keeping the handle open prevents Windows from reusing tid while tracked and lets exchange_for_new_thread and on_thread_detach prune threads that died without DLL_THREAD_DETACH via WaitForSingleObject(handle, 0) == WAIT_TIMEOUT.
    • Publish XCL_SLOT.store(k, Ordering::SeqCst) under MOVED.lock() before snapshotting peer threads so concurrent attach/detach serializes on MOVED.
    • Keep peer thread handles open (THREAD_QUERY_INFORMATION | THREAD_SUSPEND_RESUME | THREAD_SYNCHRONIZE) across both phases of take_slot_zero, suspending each peer individually with a RAII SuspendGuard across only raw syscalls (WaitForSingleObject, NtQueryInformationThread, ReadProcessMemory) and pointer updates (no heap allocations or locks while suspended). Skip threads mid-LdrpAllocateTls (arr == null, arr[0] == 0, or arr[k] == 0) so their upcoming on_thread_attach performs the swap cleanly.
    • Handle the current thread directly without adding it to MOVED.
  • crates/xcl/tests/tls_slot_zero.rs:
    • Process-isolated integration tests with a .CRT$XLY callback forwarding real DLL_THREAD_ATTACH / DLL_THREAD_DETACH events:
      • reused_teb_after_terminate_thread_still_exchanges_slots — verified to fail deterministically on unmodified main (47efa2e) on the first recycled TEB (host TLS corrupted on new thread), and passes with this fix.
      • take_slot_zero_survives_concurrent_thread_exit_and_termination — exercises take_slot_zero under concurrent thread spawn/exit churn and TerminateThread, validating both survivor TLS integrity and HeapValidate.

Regression run

Baseline (main, warm shader/PSO cache):

xcl-smoke v1
commit: 09940bb87fa11bc394fd9e5de570c2b7cfd16323
gpu: AMD Radeon RX 6800
title: YouKnowWho
seconds: 60
exit: 0x0
frames shown: 405 (PresentX 405)
presents per 2 s (last 3): 11 12 11
device removals: 0
crashes: 0
peak commit: 15870 MB

This PR (fix/tls-take-slot-zero-safety, warm shader/PSO cache):

xcl-smoke v1
commit: 39ea43e9d95d706aaa6ec679c7544d63c822a566
gpu: AMD Radeon RX 6800
title: YouKnowWho
seconds: 60
exit: 0x0
frames shown: 489 (PresentX 489)
presents per 2 s (last 3): 17 16 17
device removals: 0
crashes: 0
peak commit: 15868 MB

Tested

Hardware: Windows 11 Pro for Workstations (Build 26300), Intel Core i9-9900K, 32 GB RAM, AMD Radeon RX 6800 (16337 MiB VRAM, Driver 32.0.21045.29003), rustc 1.100.0 (0ef1bc386 2026-11-09).

  • cargo test -p xcl --test tls_slot_zero (5 consecutive runs): 2 passed; 0 failed; 0 ignored; 0 skipped each run (on unmodified 47efa2e, reused_teb_after_terminate_thread_still_exchanges_slots failed with host TLS was corrupted on new thread (iter=234, reused=1)).
  • cargo test -p xcl --lib tls::: 4 passed; 0 failed; 1 ignored; 0 skipped.
  • cargo test -p xcl --test e2e: 3 passed; 0 failed; 0 ignored; 0 skipped.
  • XCL_REQUIRE_GPU=wave64,mesh cargo build --workspace && cargo test --workspace --no-fail-fast -- --test-threads=4: 740 passed; 3 failed; 41 ignored; 0 skipped. All 3 failures are the pre-existing RDNA2 placed_title_textures_share_vram_and_preserve_pixels + 2 VRAM_TEST_LOCK PoisonError cascades on unmodified main (tracked in D3D12.X: map a one-slice title texture's packed mips where D3D12 addresses them #470).
  • .\scripts\smoke.ps1 -Game '<game folder>\YouKnowWho' on 39ea43e9d95d706aaa6ec679c7544d63c822a566 (see Regression run above).

Not tested

  • MinGW / gnu target builds (MSVC toolchain only).
  • Non-AMD GPUs and Linux / Wine.

AI use

Antigravity (Gemini 4 Neon) audited crates/xcl/src/tls.rs, wrote the fix and process-isolated regression tests, and ran the local test suite and smoke runs.

Keep peer thread handles open across both phases of take_slot_zero, suspend each peer across raw TEB/TLS-array syscalls and pointer swaps with WaitForSingleObject + ReadProcessMemory liveness checks, and publish XCL_SLOT before snapshotting threads so concurrent attach/detach serializes on MOVED.

Track MOVED entries by (teb, tid, open thread handle) and prune signaled handles in exchange_for_new_thread and on_thread_detach so threads terminated without DLL_THREAD_DETACH (e.g. TerminateThread) cannot poison a recycled TEB address and skip the slot 0 / slot k swap on future threads.

Part of xcl-dev#106.

Assisted-by: Antigravity (Gemini 4 Neon)
@futurepro62

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head 39ea43e (on its base 47efa2e).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 743 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: reused_teb_after_terminate_thread_still_exchanges_slots, take_slot_zero_survives_concurrent_thread_exit_and_termination.

Not tested

  • AMD, a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

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.

2 participants