Skip to content

GPU: return pinned HostTexture from alias_source to avoid source Release UAF - #3245

Open
fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/gpu-alias-source-drop-uaf
Open

fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/gpu-alias-source-drop-uaf

Conversation

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Problem

  1. Use-after-free across resource::alias_source → texture::alias_copy:
    resource::alias_source(placed: This) locks TEXTURES_AT, finds a GPU-written aliased texture a at the same backing address whose made(a) differs from made(placed), drops the cloned Arc<HostTexture> and releases TEXTURES_AT, and returns the raw unowned pointer Some(a) (This = *const Object) without taking a COM reference.
    Under legal multi-threaded D3D12 usage (for example, releasing a transient placed render target / UAV / depth resource on another thread while a recording thread draws with an incompatible-format aliased texture at the same address), a->Release() removes a from TEXTURES_AT and frees Box<Resource> before texture::alias_copy runs. alias_copy then dereferences the freed Resource in both resource::host_resource(from) and list::ensure_state(list, from, COPY_SOURCE).

  2. Destination identity split under concurrent eviction in texture::alias_copy:
    alias_copy separately called resource::host_resource(to), texture::host_writes(list, to), and list::ensure_state(list, to, COPY_DEST). Because a title-written placed destination texture is evictable before host_writes runs at submission, a concurrent resource::evict_unused() between those calls could recreate a new HostTexture for host_writes and ensure_state while CopyTextureRegion wrote into the earlier, untransitioned and unpinned ID3D12Resource.

Fix

  • Change resource::alias_source(placed: This) to return Option<Arc<HostTexture>>, cloning the source Arc<HostTexture> while still holding TEXTURES_AT (which Resource::drop must acquire before dropping fields or freeing Box<Resource>).
  • Factor HostList::transition_copy(&self, copy: &Arc<HostTexture>, wanted: u32) (preserving depth-twin routing via twin::route) and list::ensure_copy_state so alias_copy transitions and retains pinned Arc<HostTexture> values directly without dereferencing from: This.
  • Update texture::alias_copy to take from: &Arc<HostTexture>, pin to_copy = resource::host_copy(to)? once at entry (skipping self-copy if Arc::ptr_eq(from, &to_copy)), and use the same pinned from and to_copy across first_use, GetDesc, host_writes_copy, ensure_copy_state, CopyTextureRegion, and log deduplication.
  • Update the call site in crates/victory-gpu/src/draw.rs (&a) and the null-view unit test in crates/victory-gpu/src/resource.rs.

Verification

  • Added deterministic GPU regression tests in crates/victory-gpu/src/texture.rs:
    • alias_copy_survives_source_resource_release_after_alias_source: reproduces the bug deterministically on unmodified main (BC1 block (0, 0) reads [0, 0] instead of [0x3f800000, 0x40000000] after Release(src) and slot reuse) and passes with exact readback and zero D3D12 debug errors after the fix.
    • alias_source_and_copy_handle_concurrent_release_and_destination_eviction: exercises a two-thread Barrier(2) race between alias_source + alias_copy and src->Release(), plus resource::give_up_now(dst) eviction between alias_source and alias_copy.
  • Verified on Windows with RTX 5090 (default features and VICTORY_D3D12_DEBUG=1 --features diagnostics), WARP (VICTORY_D3D12_DEBUG=1 --features diagnostics and full victory-gpu suite: 248 passed, 0 failed, 1 ignored), both workspace clippy -D warnings configurations, rustfmt --edition 2024, and xtask policy::tests::repository_passes_policy.

…ase UAF

resource::alias_source previously looked up a GPU-written aliased texture under TEXTURES_AT, dropped the Arc<HostTexture> and lock, and returned a raw *const Object (This) with no COM reference. If another thread legally called ID3D12Resource::Release() on the aliased source texture before texture::alias_copy ran, alias_copy dereferenced the freed Box<Resource> (or a recycled allocation at the same address) in resource::host_resource and list::ensure_state.

Return Option<Arc<HostTexture>> cloned while holding TEXTURES_AT, route pinned host-texture transitions (including depth twins) through HostList::transition_copy and list::ensure_copy_state, and pin the destination HostTexture once inside texture::alias_copy so concurrent eviction cannot split destination identity across GetDesc, host_writes, state tracking and CopyTextureRegion.

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090 + WARP):

  • Cloning Arc inside �lias_source under TEXTURES_AT and pinning o_copy once in �lias_copy (plus ransition_copy / ensure_copy_state / host_writes_copy) cleanly closes both the concurrent source Release() UAF and destination eviction race.
  • cargo test -p victory-gpu --lib texture:: -- --test-threads=1 (RTX 5090): 4 passed, 0 failed (including �lias_copy_survives_source_resource_release_after_alias_source and �lias_source_and_copy_handle_concurrent_release_and_destination_eviction).
  • cargo test -p victory-gpu --lib resource:: -- --test-threads=1 (RTX 5090): 18 passed, 0 failed.
  • VICTORY_FORCE_WARP=1 cargo test -p victory-gpu --lib texture::tests::alias_ -- --test-threads=1 (WARP): 2 passed, 0 failed.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

@KainanP

KainanP commented Nov 26, 2026

Copy link
Copy Markdown

Heads-up: #3258 (a per-draw CPU optimization) also adds a path that moves a host copy the caller already holds (HostList::transition_held / list::ensure_held_state). It touches the same lines of HostList::transition and list::ensure_state as this PR's transition_copy / ensure_copy_state, so the two conflict textually. Whichever lands second should use the other's helper. I'll rebase #3258 if this one goes in first.

AI use: Claude Code (Claude Opus 5.5) drafted this comment.

vanya890 added a commit to vanya890/VIctory that referenced this pull request Nov 26, 2026
Adapt upstream Marvisak#3258 to the fork's Marvisak#3245 pinned-copy helpers. Cache immutable resource flags, use one retained HostTexture for upload/staging/state, and add flag parity and repeated-read regression coverage.
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

Thanks for the heads-up @KainanP! Sounds great — whichever lands first, the other can cleanly reuse the shared held-texture transition helper.

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.

3 participants