xasync: prevent use-after-free on XAsyncCancel in pending_with and concurrent XAsyncGetResult #677

Open
MasterWolf wants to merge 1 commit from MasterWolf/Darner:pr/post-merge-xasync-cancel-uaf into post-merge
First-time contributor

Not run in the game (verified via cargo test, cargo clippy, and cargo build on 2ee54ba; concurrency/cancel paths covered by unit tests).

Contributor role (optional)

  • Fluxer ID: 1577824307575267331

What it changes

  • The problem on post-merge: read from crates/darner-hle/src/modules/xasync.rs (28a9b42):
    1. pending_with passes a raw Box<Data> pointer to XAsyncBegin and moves raw state: usize / block: usize into a spawned worker thread. Calling XAsyncCancel while work() is running invokes release(block) -> OP_CLEANUP, freeing Data while the worker still writes into it (heap UAF) and later calls XAsyncComplete(block, ...) which can match a reused AsyncBlock (corrupting the new call).
    2. XAsyncGetResult clones Arc<Call> from CALLS and calls release(block) only after OP_GET_RESULT. Two concurrent callers can race OP_GET_RESULT against OP_CLEANUP, freeing the provider context in use.

This PR:

  • Adds complete_call(&Arc<Call>, ...) and release_call(block, &Arc<Call>) (Arc::ptr_eq-guarded removal) so stale completions lock call.done without touching block or evicting a reused call.
  • Switches pending_with from Box<Data> to Arc<Data>, cloning both Arc<Data> and Arc<Call> in OP_BEGIN for the worker thread and completing via complete_call.
  • Makes XAsyncGetResult validate identity and buffer size under CALLS lock, then atomically remove the Arc<Call> before invoking OP_GET_RESULT and OP_CLEANUP.
  • Adds 3 unit tests (pending_with_cancel_while_running_neither_uafs_nor_corrupts_reused_block, pending_with_completes_and_delivers_payload, get_result_buffer_too_small_keeps_call_alive_and_concurrent_get_result_runs_once).

How it was tested

  • Tested revision: 2ee54ba (on post-merge 28a9b42)
  • Commands and results:
    • cargo test --offline -p darner-hle --lib modules::xasync — 3 passed; 0 failed (0.00s)
    • cargo clippy --offline -p darner-hle --tests — passed (0 warnings in darner-hle)
    • cargo build --offline -p darner — passed (12.02s)
  • Changes a default: no
  • A/B result: not applicable (FFI/concurrency memory-safety fix).
  • Checks run on that revision: cargo build, cargo test, cargo clippy
  • Gameplay, when relevant: not run in the game
  • Hardware dependence: likely general
  • Coverage gaps: verified with multi-threaded unit tests rather than an in-game launch.

Checklist

  • I ran this change in the game, or say below why it can't be (not run in the game; verified via multi-threaded unit tests, cargo clippy, and cargo build).
  • This PR contains one change based on post-merge.
  • The diff contains no game files, code or assets.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: none found
  • Replaces or overlaps: none found
**Not run in the game** (verified via `cargo test`, `cargo clippy`, and `cargo build` on `2ee54ba`; concurrency/cancel paths covered by unit tests). ## Contributor role (optional) - Fluxer ID: 1577824307575267331 ## What it changes - The problem on `post-merge`: read from `crates/darner-hle/src/modules/xasync.rs` (`28a9b42`): 1. `pending_with` passes a raw `Box<Data>` pointer to `XAsyncBegin` and moves raw `state: usize` / `block: usize` into a spawned worker thread. Calling `XAsyncCancel` while `work()` is running invokes `release(block)` -> `OP_CLEANUP`, freeing `Data` while the worker still writes into it (heap UAF) and later calls `XAsyncComplete(block, ...)` which can match a reused `AsyncBlock` (corrupting the new call). 2. `XAsyncGetResult` clones `Arc<Call>` from `CALLS` and calls `release(block)` only *after* `OP_GET_RESULT`. Two concurrent callers can race `OP_GET_RESULT` against `OP_CLEANUP`, freeing the provider context in use. This PR: - Adds `complete_call(&Arc<Call>, ...)` and `release_call(block, &Arc<Call>)` (`Arc::ptr_eq`-guarded removal) so stale completions lock `call.done` without touching `block` or evicting a reused call. - Switches `pending_with` from `Box<Data>` to `Arc<Data>`, cloning both `Arc<Data>` and `Arc<Call>` in `OP_BEGIN` for the worker thread and completing via `complete_call`. - Makes `XAsyncGetResult` validate `identity` and buffer size under `CALLS` lock, then atomically remove the `Arc<Call>` before invoking `OP_GET_RESULT` and `OP_CLEANUP`. - Adds 3 unit tests (`pending_with_cancel_while_running_neither_uafs_nor_corrupts_reused_block`, `pending_with_completes_and_delivers_payload`, `get_result_buffer_too_small_keeps_call_alive_and_concurrent_get_result_runs_once`). ## How it was tested - Tested revision: `2ee54ba` (on `post-merge` `28a9b42`) - Commands and results: - `cargo test --offline -p darner-hle --lib modules::xasync` — `3 passed; 0 failed` (`0.00s`) - `cargo clippy --offline -p darner-hle --tests` — passed (`0` warnings in `darner-hle`) - `cargo build --offline -p darner` — passed (`12.02s`) - Changes a default: no - A/B result: not applicable (FFI/concurrency memory-safety fix). - Checks run on that revision: `cargo build`, `cargo test`, `cargo clippy` - Gameplay, when relevant: not run in the game - Hardware dependence: likely general - Coverage gaps: verified with multi-threaded unit tests rather than an in-game launch. ## Checklist - [ ] I ran this change in the game, or say below why it can't be (not run in the game; verified via multi-threaded unit tests, `cargo clippy`, and `cargo build`). - [x] This PR contains one change based on `post-merge`. - [x] The diff contains no game files, code or assets. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: none found - Replaces or overlaps: none found
TrustNo1 changed title from [pr-fix][hle] xasync: prevent use-after-free on XAsyncCancel in pending_with and concurrent XAsyncGetResult to xasync: prevent use-after-free on XAsyncCancel in pending_with and concurrent XAsyncGetResult 2026-12-06 03:25:16 +00:00
This pull request can be merged automatically.
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 'pr/post-merge-xasync-cancel-uaf:MasterWolf-pr/post-merge-xasync-cancel-uaf'
git switch 'MasterWolf-pr/post-merge-xasync-cancel-uaf'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'post-merge'
git merge --no-ff 'MasterWolf-pr/post-merge-xasync-cancel-uaf'
git switch 'MasterWolf-pr/post-merge-xasync-cancel-uaf'
git rebase 'post-merge'
git switch 'post-merge'
git merge --ff-only 'MasterWolf-pr/post-merge-xasync-cancel-uaf'
git switch 'MasterWolf-pr/post-merge-xasync-cancel-uaf'
git rebase 'post-merge'
git switch 'post-merge'
git merge --no-ff 'MasterWolf-pr/post-merge-xasync-cancel-uaf'
git switch 'post-merge'
git merge --squash 'MasterWolf-pr/post-merge-xasync-cancel-uaf'
git switch 'post-merge'
git merge --ff-only 'MasterWolf-pr/post-merge-xasync-cancel-uaf'
git switch 'post-merge'
git merge 'MasterWolf-pr/post-merge-xasync-cancel-uaf'
git push origin 'post-merge'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!677
No description provided.