Repository navigation
TLS: harden take_slot_zero against TEB UAF and stale MOVED entries - #1083
Conversation
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 #106. Assisted-by: Antigravity (Gemini 4 Neon)
|
Full-suite run, since this PR has no test report yet. Tested exact head Tested
Not tested
AI use
|
|
Verification on RTX 3060 Ti (Windows 11), branch merged with current
Thanks! |
|
NVIDIA test report for
AI-assisted (Claude Code, Claude Opus 5.5). Synthetic tests only; no title run. |
|
merge-check claim |
|
merge-check |
|
merge-check |
Hardens
tls::take_slot_zeroandMOVEDtracking against two concurrency bugs from #106:ThreadLocalStoragePointerUAF duringtake_slot_zero: previouslythread_tebs()closed every thread handle immediately after readingThreadBasicInformation, thentake_slot_zerodereferenced the raw TEBs and wrote*arr = blockafterSleep(20)without verifying the thread was still alive. PublishingXCL_SLOTonly after the snapshot also left a window where concurrently starting or exiting threads bypassedMOVEDserialization.MOVEDentries afterTerminateThread:MOVEDstored only raw TEB addresses (Vec<usize>). A thread killed withoutDLL_THREAD_DETACHleft its TEB address inMOVEDpermanently, causingexchange_for_new_threadto skip swapping slot0and slotkwhen 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, andon_thread_detach, and places new tests in a separate integration test file.)What is in it
crates/xcl/src/tls.rs:MOVED: Mutex<Vec<usize>>withMovedTeb { teb, tid, handle: ThreadHandle }(Dropcloses the handle). Keeping the handle open prevents Windows from reusingtidwhile tracked and letsexchange_for_new_threadandon_thread_detachprune threads that died withoutDLL_THREAD_DETACHviaWaitForSingleObject(handle, 0) == WAIT_TIMEOUT.XCL_SLOT.store(k, Ordering::SeqCst)underMOVED.lock()before snapshotting peer threads so concurrent attach/detach serializes onMOVED.THREAD_QUERY_INFORMATION | THREAD_SUSPEND_RESUME | THREAD_SYNCHRONIZE) across both phases oftake_slot_zero, suspending each peer individually with a RAIISuspendGuardacross 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, orarr[k] == 0) so their upcomingon_thread_attachperforms the swap cleanly.MOVED.crates/xcl/tests/tls_slot_zero.rs:.CRT$XLYcallback forwarding realDLL_THREAD_ATTACH/DLL_THREAD_DETACHevents:reused_teb_after_terminate_thread_still_exchanges_slots— verified to fail deterministically on unmodifiedmain(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— exercisestake_slot_zerounder concurrent thread spawn/exit churn andTerminateThread, validating both survivor TLS integrity andHeapValidate.Regression run
Baseline (
main, warm shader/PSO cache):This PR (
fix/tls-take-slot-zero-safety, warm shader/PSO cache):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 skippedeach run (on unmodified47efa2e,reused_teb_after_terminate_thread_still_exchanges_slotsfailed withhost 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 RDNA2placed_title_textures_share_vram_and_preserve_pixels+ 2VRAM_TEST_LOCKPoisonErrorcascades on unmodifiedmain(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'on39ea43e9d95d706aaa6ec679c7544d63c822a566(see Regression run above).Not tested
gnutarget builds (MSVC toolchain only).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.