XGameRuntime: use-after-free in XUserRegisterForDeviceAssociationChanged / Unregister #1389

Open
opened 2026-11-26 04:52:42 +00:00 by github-bot · 0 comments
Owner

@⁠microwave805 on GitHub · 2026-11-24 15:31 UTC · original

Checked on main (930e8b4).

Problem

In crates/xcl/src/hle/xgameruntime/mod.rs:851-923:

unsafe extern "system" fn association_call(rec: *mut c_void, _canceled: bool) {
    let r = Box::from_raw(rec as *mut AssociationCall);
    let f: unsafe extern "system" fn(*mut c_void, *const DeviceAssociationChange) = std::mem::transmute(r.cb);
    f(r.ctx as *mut c_void, &r.change);
}
...
pub unsafe extern "system" fn XUserUnregisterForDeviceAssociationChanged(token: u64, _wait: bool) -> bool {
    ASSOCIATION_CALLBACKS.lock().unwrap().retain(|c| c.0 != token);
    true
}
  1. XUserUnregisterForDeviceAssociationChanged ignores _wait and returns true immediately without waiting for any in-flight association_call or preventing already-queued AssociationCall items from invoking r.cb.
  2. association_call ignores _canceled and never checks whether the registration is still active — so even terminating the task queue (XTaskQueueTerminate, which passes canceled = true) still invokes f(r.ctx, &r.change).
  3. Lines 897-901 vs 915-922 read USER_SIGNED_IN outside ASSOCIATION_CALLBACKS's mutex, so a concurrent sign-in and register can submit the same callback twice.

Minimal trigger

  1. Register a callback via XUserRegisterForDeviceAssociationChanged(queue, ctx, cb, &mut token) on a manual or thread-pool completion port so notify_association queues an AssociationCall.
  2. Call XUserUnregisterForDeviceAssociationChanged(token, /*wait=*/true) and free ctx (valid per GDK contract once wait = true returns true).
  3. Dispatch the completion port (or call XTaskQueueTerminate) → association_call invokes cb(ctx, &change) on the freed ctx.

Suggested fix

Track each registration with an Arc state holding stopped, in_flight, and a Condvar (mirroring CallbackEntry in crates/xcl/src/hle/gameinput.rs:658-751), bail out of association_call without calling f when canceled || stopped (still dropping Box<AssociationCall>), wait while wait && in_flight > 0 on unregister, and check/update USER_SIGNED_IN under the same lock as ASSOCIATION_CALLBACKS.


AI use: Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on 930e8b4 before opening.

**@⁠microwave805** on GitHub · 2026-11-24 15:31 UTC · [original](https://github.com/xcl-dev/xcl/issues/1389) Checked on `main` (`930e8b4`). ### Problem In `crates/xcl/src/hle/xgameruntime/mod.rs:851-923`: ```rust unsafe extern "system" fn association_call(rec: *mut c_void, _canceled: bool) { let r = Box::from_raw(rec as *mut AssociationCall); let f: unsafe extern "system" fn(*mut c_void, *const DeviceAssociationChange) = std::mem::transmute(r.cb); f(r.ctx as *mut c_void, &r.change); } ... pub unsafe extern "system" fn XUserUnregisterForDeviceAssociationChanged(token: u64, _wait: bool) -> bool { ASSOCIATION_CALLBACKS.lock().unwrap().retain(|c| c.0 != token); true } ``` 1. `XUserUnregisterForDeviceAssociationChanged` ignores `_wait` and returns `true` immediately without waiting for any in-flight `association_call` or preventing already-queued `AssociationCall` items from invoking `r.cb`. 2. `association_call` ignores `_canceled` and never checks whether the registration is still active — so even terminating the task queue (`XTaskQueueTerminate`, which passes `canceled = true`) still invokes `f(r.ctx, &r.change)`. 3. Lines `897-901` vs `915-922` read `USER_SIGNED_IN` outside `ASSOCIATION_CALLBACKS`'s mutex, so a concurrent sign-in and register can submit the same callback twice. ### Minimal trigger 1. Register a callback via `XUserRegisterForDeviceAssociationChanged(queue, ctx, cb, &mut token)` on a manual or thread-pool completion port so `notify_association` queues an `AssociationCall`. 2. Call `XUserUnregisterForDeviceAssociationChanged(token, /*wait=*/true)` and free `ctx` (valid per GDK contract once `wait = true` returns `true`). 3. Dispatch the completion port (or call `XTaskQueueTerminate`) → `association_call` invokes `cb(ctx, &change)` on the freed `ctx`. ### Suggested fix Track each registration with an `Arc` state holding `stopped`, `in_flight`, and a `Condvar` (mirroring `CallbackEntry` in `crates/xcl/src/hle/gameinput.rs:658-751`), bail out of `association_call` without calling `f` when `canceled || stopped` (still dropping `Box<AssociationCall>`), wait while `wait && in_flight > 0` on unregister, and check/update `USER_SIGNED_IN` under the same lock as `ASSOCIATION_CALLBACKS`. --- **AI use:** Antigravity (Gemini 4 Neon) performed the read-only source audit and drafted this issue; checked and verified on `930e8b4` before opening.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:52:42 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.