D3D12.X: TOCTOU use-after-free on RTVS resource pointer in present::note_execute #2381

Open
opened 2026-11-26 05:29:36 +00:00 by github-bot · 0 comments
Owner

@⁠ahmetbagci8 on GitHub · 2026-11-25 17:32 UTC · original

Summary

In crates/xcl/src/hle/d3d12x/present.rs, note_execute reads a raw COM resource pointer res out of RTVS and drops RTVS.lock() without calling add_ref(res) (lines 276-288):

let res = RTVS.lock().unwrap().as_ref().and_then(|m| m.get(&h).copied());
let Some(res) = res else { return };
if backbuffer_like(res).is_none() {
    return;
}
{
    let mut c = CANDIDATE.lock().unwrap();
    if c.map(|(_, r)| r) != Some(res) {
        add_ref(res);
        if let Some((_, old)) = c.replace((queue as usize, res)) {
            release(old);
        }
    }
}

Meanwhile, note_rtv (lines 158-171) removes or replaces entries in RTVS under RTVS.lock() and calls release(o):

let old = if res.is_null() { m.remove(&handle) } else { m.insert(handle, res as usize) };
if let Some(o) = old {
    release(o);
}

If another thread calls note_rtv(h, ...) between note_execute releasing RTVS.lock() (line 276) and calling backbuffer_like(res) (line 278) / add_ref(res) (line 284), and RTVS held the last reference, res is freed before note_execute dereferences its COM vtable (TOCTOU use-after-free).

Suggested fix

Acquire a reference while still holding RTVS.lock(), and release it on early return or if CANDIDATE already holds res:

let res = {
    let g = RTVS.lock().unwrap();
    let Some(res) = g.as_ref().and_then(|m| m.get(&h).copied()) else { return };
    add_ref(res);
    res
};
if backbuffer_like(res).is_none() {
    release(res);
    return;
}
{
    let mut c = CANDIDATE.lock().unwrap();
    if c.map(|(_, r)| r) != Some(res) {
        if let Some((_, old)) = c.replace((queue as usize, res)) {
            release(old);
        }
    } else {
        release(res);
    }
}

Not tested

  • Read from commit 082944135cd2b96288470a595861fa608eee08dc without a new runtime test.

AI use

  • Antigravity harness (Gemini 4 Neon) audited present.rs and drafted this report; the author reviewed it before posting.
**@⁠ahmetbagci8** on GitHub · 2026-11-25 17:32 UTC · [original](https://github.com/xcl-dev/xcl/issues/2381) ### Summary In `crates/xcl/src/hle/d3d12x/present.rs`, `note_execute` reads a raw COM resource pointer `res` out of `RTVS` and drops `RTVS.lock()` without calling `add_ref(res)` (lines 276-288): ```rust let res = RTVS.lock().unwrap().as_ref().and_then(|m| m.get(&h).copied()); let Some(res) = res else { return }; if backbuffer_like(res).is_none() { return; } { let mut c = CANDIDATE.lock().unwrap(); if c.map(|(_, r)| r) != Some(res) { add_ref(res); if let Some((_, old)) = c.replace((queue as usize, res)) { release(old); } } } ``` Meanwhile, `note_rtv` (lines 158-171) removes or replaces entries in `RTVS` under `RTVS.lock()` and calls `release(o)`: ```rust let old = if res.is_null() { m.remove(&handle) } else { m.insert(handle, res as usize) }; if let Some(o) = old { release(o); } ``` If another thread calls `note_rtv(h, ...)` between `note_execute` releasing `RTVS.lock()` (line 276) and calling `backbuffer_like(res)` (line 278) / `add_ref(res)` (line 284), and `RTVS` held the last reference, `res` is freed before `note_execute` dereferences its COM vtable (TOCTOU use-after-free). ### Suggested fix Acquire a reference while still holding `RTVS.lock()`, and release it on early return or if `CANDIDATE` already holds `res`: ```rust let res = { let g = RTVS.lock().unwrap(); let Some(res) = g.as_ref().and_then(|m| m.get(&h).copied()) else { return }; add_ref(res); res }; if backbuffer_like(res).is_none() { release(res); return; } { let mut c = CANDIDATE.lock().unwrap(); if c.map(|(_, r)| r) != Some(res) { if let Some((_, old)) = c.replace((queue as usize, res)) { release(old); } } else { release(res); } } ``` ### Not tested - Read from commit `082944135cd2b96288470a595861fa608eee08dc` without a new runtime test. ### AI use - Antigravity harness (Gemini 4 Neon) audited `present.rs` and drafted this report; the author reviewed it before posting.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:29:36 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.