HLE/xasync: fix pending_with cancel use-after-free and XAsyncComplete blocking wait race #300

Open
github-bot wants to merge 1 commit from github-bot/gh-300 into main AGit
Owner

@⁠AscilCH on GitHub · 2026-11-24 23:25 UTC · original
AscilCH:fix/xasync-cancel-uaf-and-wait-race → main · +186 −22 · 1 files · 1 commits · open

Problem

Two concurrency bugs in crates/victory-hle/src/modules/xasync.rs:

  1. Use-after-free in pending_with when XAsyncCancel races with the worker thread:
    • pending_with boxed Data and passed state as usize to the detached worker thread.
    • If the title calls XAsyncCancel(block) while work() is running, XAsyncCancel completes the block with E_ABORT -> OP_CLEANUP -> Box::from_raw(state), freeing Data immediately.
    • When work() later returns Ok(bytes), the worker dereferences the freed state (use-after-free on a destroyed Mutex) and calls XAsyncComplete(block, ...) which could also complete a newer call reusing the same AsyncBlock.
  2. Blocking XAsyncGetStatus(block, true) can return E_PENDING:
    • For any completion with result < 0 || required_size == 0, XAsyncComplete called release(block) (removing block from CALLS and executing OP_CLEANUP) before storing size and status.
    • Any concurrent thread calling XAsyncGetStatus(block, true) in that window got call(block) == None, skipped waiting on call.finished, and returned E_PENDING from a blocking wait.

Fix

  • Reference-count pending_with's state with Arc<Data>, passing a cloned Arc to the worker thread, and track a cancellation flag updated in OP_CANCEL / OP_CLEANUP so a canceled worker neither touches freed memory nor completes a reused AsyncBlock.
  • In XAsyncComplete, keep block registered in CALLS while OP_CLEANUP runs, then store size/status and remove block from CALLS together under lock(&CALLS) (preserving acyclic lock order call.done -> CALLS).
  • Add unit tests blocking_get_status_does_not_return_e_pending_during_cleanup and pending_with_cancel_does_not_use_after_free_or_complete_reused_block.

Verification

  • rustfmt --edition 2024 --check crates/victory-hle/src/modules/xasync.rs (changed hunks): clean
  • cargo test -p victory-hle --locked xasync -- --test-threads=1: 9 passed, 0 failed
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-hle --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-24 23:25 UTC · [original](https://github.com/fxceless/victory-fork/pull/300) `AscilCH:fix/xasync-cancel-uaf-and-wait-race` → `main` · +186 −22 · 1 files · 1 commits · open ## Problem Two concurrency bugs in `crates/victory-hle/src/modules/xasync.rs`: 1. **Use-after-free in `pending_with` when `XAsyncCancel` races with the worker thread**: - `pending_with` boxed `Data` and passed `state as usize` to the detached worker thread. - If the title calls `XAsyncCancel(block)` while `work()` is running, `XAsyncCancel` completes the block with `E_ABORT` -> `OP_CLEANUP` -> `Box::from_raw(state)`, freeing `Data` immediately. - When `work()` later returns `Ok(bytes)`, the worker dereferences the freed `state` (use-after-free on a destroyed `Mutex`) and calls `XAsyncComplete(block, ...)` which could also complete a newer call reusing the same `AsyncBlock`. 2. **Blocking `XAsyncGetStatus(block, true)` can return `E_PENDING`**: - For any completion with `result < 0 || required_size == 0`, `XAsyncComplete` called `release(block)` (removing `block` from `CALLS` and executing `OP_CLEANUP`) *before* storing `size` and `status`. - Any concurrent thread calling `XAsyncGetStatus(block, true)` in that window got `call(block) == None`, skipped waiting on `call.finished`, and returned `E_PENDING` from a blocking wait. ## Fix - Reference-count `pending_with`'s state with `Arc<Data>`, passing a cloned `Arc` to the worker thread, and track a cancellation flag updated in `OP_CANCEL` / `OP_CLEANUP` so a canceled worker neither touches freed memory nor completes a reused `AsyncBlock`. - In `XAsyncComplete`, keep `block` registered in `CALLS` while `OP_CLEANUP` runs, then store `size`/`status` and remove `block` from `CALLS` together under `lock(&CALLS)` (preserving acyclic lock order `call.done` -> `CALLS`). - Add unit tests `blocking_get_status_does_not_return_e_pending_during_cleanup` and `pending_with_cancel_does_not_use_after_free_or_complete_reused_block`. ## Verification - `rustfmt --edition 2024 --check crates/victory-hle/src/modules/xasync.rs` (changed hunks): clean - `cargo test -p victory-hle --locked xasync -- --test-threads=1`: 9 passed, 0 failed - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-hle --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-hle --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic tests only, no game data.)*
Fix two concurrency bugs in `crates/victory-hle/src/modules/xasync.rs`:

1. `pending_with` passed a raw `Box<Data>` pointer to its detached worker
   thread. If `XAsyncCancel` ran while the worker was still executing,
   `OP_CLEANUP` freed `Box::from_raw(state)`, causing an immediate
   use-after-free when the worker wrote its result bytes and potentially
   completing a later reused `AsyncBlock` at the same address.
   Hold `Data` in `Arc<Data>`, clone a strong reference for the worker thread,
   and record cancellation in `OP_CANCEL` / `OP_CLEANUP` so a canceled worker
   exits without touching a reused block.
2. `XAsyncComplete` called `release(block)` (removing `block` from `CALLS`
   and running `OP_CLEANUP`) *before* storing `size` and `status`. Any
   concurrent thread calling blocking `XAsyncGetStatus(block, true)` during
   that window saw `call(block) == None`, skipped waiting on `call.finished`,
   and returned `E_PENDING`.
   Keep `block` registered in `CALLS` during `OP_CLEANUP`, then publish
   `size` and `status` before removing `block` from `CALLS` under the `CALLS`
   lock, maintaining strict lock order `call.done` -> `CALLS`.

Add unit tests `blocking_get_status_does_not_return_e_pending_during_cleanup`
and `pending_with_cancel_does_not_use_after_free_or_complete_reused_block`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:40:16 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/300/head:github-bot/gh-300'
git switch 'github-bot/gh-300'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-300'
git switch 'github-bot/gh-300'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-300'
git switch 'github-bot/gh-300'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-300'
git switch 'main'
git merge --squash 'github-bot/gh-300'
git switch 'main'
git merge --ff-only 'github-bot/gh-300'
git switch 'main'
git merge 'github-bot/gh-300'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.