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

Open
opened 2026-11-24 15:31:22 +00:00 by microwave805 · 0 comments
microwave805 commented 2026-11-24 15:31:22 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by microwave805 on 2026-11-24T15:31:22Z.


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.

<!-- greyfinch-sync:ivall/xcl:issue:6585383945 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/1389). Originally posted by [microwave805](https://github.com/microwave805) on 2026-11-24T15:31:22Z. --- 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.
Sign in to join this conversation.
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
xcl-dev/xcl#1379
No description provided.