Keep pending_with state alive across cancel and complete its own call #348

Closed
github-bot wants to merge 635 commits from github-bot/gh-348 into main AGit
Owner

@⁠ahmetbagci8 on GitHub · 2026-11-27 19:05 UTC · original
ahmetbagci8:fix/xasync-pending-with-cancel-uaf → main · +58894 −5220 · 200 files · 635 commits · closed without merging

Summary

In crates/victory-hle/src/modules/xasync.rs, pending_with spawns a worker thread holding raw pointers to Box<Data> and *mut AsyncBlock. If XAsyncCancel runs while work() is still executing:

  1. complete_call invokes OP_CLEANUP, which frees Box<Data> via Box::from_raw while the worker thread is still running — causing a use-after-free when work() returns Ok(bytes) and locks the destroyed Mutex.
  2. The worker then calls XAsyncComplete(block, hr, len) by raw block address instead of completing its own Arc<Call>, which can erroneously complete a newer call begun on the reused AsyncBlock.

Changes

  • Store pending_with state in Arc<Data> and clone an Arc for the worker thread so OP_CLEANUP cannot free Data while work is in flight.
  • Capture call(data.block) in OP_BEGIN and complete that generation with complete_call(&target, ...) instead of XAsyncComplete(block, ...).
  • Add unit test canceled_pending_with_does_not_complete_reused_block.

Tested

  • cargo test -p victory-hle --lib xasync: 17 passed; 0 failed; 0 ignored
  • cargo clippy -p victory-hle --tests: clean (0 warnings)
**@⁠ahmetbagci8** on GitHub · 2026-11-27 19:05 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/348) `ahmetbagci8:fix/xasync-pending-with-cancel-uaf` → `main` · +58894 −5220 · 200 files · 635 commits · closed without merging ### Summary In `crates/victory-hle/src/modules/xasync.rs`, `pending_with` spawns a worker thread holding raw pointers to `Box<Data>` and `*mut AsyncBlock`. If `XAsyncCancel` runs while `work()` is still executing: 1. `complete_call` invokes `OP_CLEANUP`, which frees `Box<Data>` via `Box::from_raw` while the worker thread is still running — causing a use-after-free when `work()` returns `Ok(bytes)` and locks the destroyed `Mutex`. 2. The worker then calls `XAsyncComplete(block, hr, len)` by raw `block` address instead of completing its own `Arc<Call>`, which can erroneously complete a newer call begun on the reused `AsyncBlock`. ### Changes - Store `pending_with` state in `Arc<Data>` and clone an `Arc` for the worker thread so `OP_CLEANUP` cannot free `Data` while `work` is in flight. - Capture `call(data.block)` in `OP_BEGIN` and complete that generation with `complete_call(&target, ...)` instead of `XAsyncComplete(block, ...)`. - Add unit test `canceled_pending_with_does_not_complete_reused_block`. ### Tested - `cargo test -p victory-hle --lib xasync`: `17 passed; 0 failed; 0 ignored` - `cargo clippy -p victory-hle --tests`: clean (0 warnings)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 19:05:28 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-11-28 16:23 UTC · original

Checked this on native Windows. The new test does catch the bug: with only the test applied on the PR's base (b6ea434), canceled_pending_with_does_not_complete_reused_block fails 3 of 3 runs at the last E_PENDING assert (left 0, right E_PENDING), so the old worker's XAsyncComplete(block, ...) really does complete the next call begun on the same block. With the full PR, the 17 xasync tests pass. It also merges cleanly on current main (fe7c6e5), where pending_with is still the old version, and no CI has run on it yet.

The fix looks right to me:

  • call(data.block) in OP_BEGIN gets this generation, since XAsyncBegin inserts it into CALLS before calling the provider.
  • After a cancel, complete_call on that stale Arc<Call> sees a state that isn't Pending and returns before touching the block, so the late worker doesn't read a block the title may have reused or freed.
  • The worker's own Arc<Data> keeps the result buffer alive across OP_CLEANUP.

It's also the pattern the comment on complete_call already asks for ("our own completions name the call they belong to, not the block"). I looked for other threads completing by block in non-test code and pending_with was the only one.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-11-28 16:23 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/348#issuecomment-7242210022) Checked this on native Windows. The new test does catch the bug: with only the test applied on the PR's base (`b6ea434`), `canceled_pending_with_does_not_complete_reused_block` fails 3 of 3 runs at the last `E_PENDING` assert (left `0`, right `E_PENDING`), so the old worker's `XAsyncComplete(block, ...)` really does complete the next call begun on the same block. With the full PR, the 17 `xasync` tests pass. It also merges cleanly on current `main` (`fe7c6e5`), where `pending_with` is still the old version, and no CI has run on it yet. The fix looks right to me: - `call(data.block)` in `OP_BEGIN` gets this generation, since `XAsyncBegin` inserts it into `CALLS` before calling the provider. - After a cancel, `complete_call` on that stale `Arc<Call>` sees a state that isn't `Pending` and returns before touching the block, so the late worker doesn't read a block the title may have reused or freed. - The worker's own `Arc<Data>` keeps the result buffer alive across `OP_CLEANUP`. It's also the pattern the comment on `complete_call` already asks for ("our own completions name the call they belong to, not the block"). I looked for other threads completing by block in non-test code and `pending_with` was the only one. AI-assisted: yes
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-29 11:37 UTC · original

Thanks for validating the generation-specific cancellation regression on native Windows. Failing three times on the base and passing with the fix is useful evidence. Please keep the result-lifetime and stale-worker cases intact when reconciling the test-module overlap with #436.

**@⁠TaynanGT** on GitHub · 2026-11-29 11:37 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/348#issuecomment-7255243343) Thanks for validating the generation-specific cancellation regression on native Windows. Failing three times on the base and passing with the fix is useful evidence. Please keep the result-lifetime and stale-worker cases intact when reconciling the test-module overlap with #436.
github-bot closed this pull request 2026-11-30 17:14:45 +00:00
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-30 17:11 UTC · original

main has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current main and open a new PR; I'll review it there.

**@⁠TaynanGT** on GitHub · 2026-11-30 17:11 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/348#issuecomment-7285861369) `main` has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current `main` and open a new PR; I'll review it there.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.