HLE: prevent concurrent XAsyncGetResult use-after-free and XAsyncBegin overwrite leak #2549

Closed
reeledMarek wants to merge 1 commit from upstream-pr/2551 into main
reeledMarek commented 2026-11-25 15:40:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T15:40:10Z.


Problem

  1. Concurrent XAsyncGetResult use-after-free: XAsyncGetResult cloned Arc<Call> from CALLS, invoked OP_GET_RESULT unlocked, and only afterward called release(block) (OP_CLEANUP). Two threads racing XAsyncGetResult on the same completed block could execute OP_GET_RESULT concurrently while the first thread's OP_CLEANUP freed the provider context.
  2. Concurrent XAsyncBegin overwrite leak: XAsyncBegin checked call(block) and inserted into CALLS under separate lock acquisitions, allowing two racing XAsyncBegin calls on the same block to overwrite and leak the first call without OP_CLEANUP.

Fix

  • In XAsyncGetResult, validate identity and buffer size and remove the entry from CALLS atomically under one lock (leaving the entry intact on E_NOT_SUFFICIENT_BUFFER or E_INVALIDARG), then invoke OP_GET_RESULT and OP_CLEANUP on the exclusively owned Call.
  • In XAsyncBegin, serialize the existing-call check and insertion under a single CALLS lock (using try_lock on left.done so lock ordering with complete_call is never inverted) and run stale OP_CLEANUP after unlocking.

Testing

  • Added concurrent_get_result_claims_result_and_runs_cleanup_once in crates/victory-hle/src/modules/xasync.rs.
  • Verified cargo test -p xtask --locked passes asset policy.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6608187416 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2551). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T15:40:10Z. --- ### Problem 1. **Concurrent `XAsyncGetResult` use-after-free:** `XAsyncGetResult` cloned `Arc<Call>` from `CALLS`, invoked `OP_GET_RESULT` unlocked, and only afterward called `release(block)` (`OP_CLEANUP`). Two threads racing `XAsyncGetResult` on the same completed block could execute `OP_GET_RESULT` concurrently while the first thread's `OP_CLEANUP` freed the provider context. 2. **Concurrent `XAsyncBegin` overwrite leak:** `XAsyncBegin` checked `call(block)` and inserted into `CALLS` under separate lock acquisitions, allowing two racing `XAsyncBegin` calls on the same block to overwrite and leak the first call without `OP_CLEANUP`. ### Fix - In `XAsyncGetResult`, validate identity and buffer size and remove the entry from `CALLS` atomically under one lock (leaving the entry intact on `E_NOT_SUFFICIENT_BUFFER` or `E_INVALIDARG`), then invoke `OP_GET_RESULT` and `OP_CLEANUP` on the exclusively owned `Call`. - In `XAsyncBegin`, serialize the existing-call check and insertion under a single `CALLS` lock (using `try_lock` on `left.done` so lock ordering with `complete_call` is never inverted) and run stale `OP_CLEANUP` after unlocking. ### Testing - Added `concurrent_get_result_claims_result_and_runs_cleanup_once` in `crates/victory-hle/src/modules/xasync.rs`. - Verified `cargo test -p xtask --locked` passes asset policy.
Marvisak commented 2026-11-25 19:06:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T18:36:29Z.


Automated triage for the maintainers.

Needs work: please add evidence. In crates/victory-hle/src/modules/xasync.rs, XAsyncGetResult now takes the Call out of CALLS under one lock before OP_GET_RESULT/OP_CLEANUP. XAsyncBegin now checks for a call left behind and inserts the new one under one lock, using try_lock on left.done. I checked the lock order: complete_call holds call.done and then takes CALLS (through release). The new try_lock avoids the inversion, and I found no deadlock.

Both races, though, need the title to use one async block from two threads at once: two XAsyncGetResult calls on the same completed block, or two XAsyncBegin calls on the same block. One block carries one call, so a title that follows the API would not do this. The PR gives no sign that the title does. The new test concurrent_get_result_claims_result_and_runs_cleanup_once shows the new code's behaviour, not something the title needs. The change also alters the order of events: the call is now gone from CALLS while the provider's OP_GET_RESULT runs. What would change the verdict: a log or trace from a run showing two threads on one block, or a reference for what the runtime does in that case.

CI: the check run passed on the current head.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7171142890 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2551#issuecomment-7171142890). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T18:36:29Z. --- <!-- victory-triage v1 head=fbd63111d7ecafa33e040bef5173360975de6056 verdict=needs-work at=2026-11-25T18:36:16Z --> Automated triage for the maintainers. **Needs work: please add evidence.** In `crates/victory-hle/src/modules/xasync.rs`, `XAsyncGetResult` now takes the `Call` out of `CALLS` under one lock before `OP_GET_RESULT`/`OP_CLEANUP`. `XAsyncBegin` now checks for a call left behind and inserts the new one under one lock, using `try_lock` on `left.done`. I checked the lock order: `complete_call` holds `call.done` and then takes `CALLS` (through `release`). The new `try_lock` avoids the inversion, and I found no deadlock. Both races, though, need the title to use **one async block from two threads at once**: two `XAsyncGetResult` calls on the same completed block, or two `XAsyncBegin` calls on the same block. One block carries one call, so a title that follows the API would not do this. The PR gives no sign that the title does. The new test `concurrent_get_result_claims_result_and_runs_cleanup_once` shows the new code's behaviour, not something the title needs. The change also alters the order of events: the call is now gone from `CALLS` while the provider's `OP_GET_RESULT` runs. What would change the verdict: a log or trace from a run showing two threads on one block, or a reference for what the runtime does in that case. CI: the `check` run passed on the current head. --- _Generated by [Claude Code](https://claude.ai/code)_
reeledMarek commented 2026-11-25 20:06:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T20:05:41Z.


Closing per triage — concurrent same-block XAsyncGetResult/XAsyncBegin violates single-owner API usage without evidence of a title doing this. Thanks for the review!

<!-- greyfinch-sync:Marvisak/VIctory:comment:7173182497 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2551#issuecomment-7173182497). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T20:05:41Z. --- Closing per triage — concurrent same-block `XAsyncGetResult`/`XAsyncBegin` violates single-owner API usage without evidence of a title doing this. Thanks for the review!
upstream-sync closed this pull request 2026-11-25 21:02:35 +00:00

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
Marvisak/VIctory!2549
No description provided.