fix(hle/dstorage): use-after-free of Instance<File> in queued requests and WALLS_FILES/factory resource leaks #319

Closed
opened 2026-12-02 09:15:39 +00:00 by github-bot · 1 comment
Owner

@⁠RUIZ-Enrique-25002136 on GitHub · 2026-12-02 09:14 UTC · original

Summary

Two memory/handle safety issues exist in crates/victory-hle/src/modules/dstorage.rs:

  1. Use-After-Free of Instance<File>: enqueue_request copies a raw Request pointer (request.source) into the queue without incrementing the File COM refcount or releasing it on completion, unlike enqueue_status.
  2. Unbounded WALLS_FILES Leak / Address Aliasing & Factory Leaks on Null out: open_file pushes (file as usize, kept) to WALLS_FILES unconditionally with no cleanup on File drop, and open_file / create_status_array / create_queue allocate Instance / Win32 event HANDLE / scheduler Lane before checking out.is_null().

Evidence (file:line & Code Proof)

1. Use-After-Free of Instance<File> in Queued Reads

  • crates/victory-hle/src/modules/dstorage.rs:1516 (enqueue_status, safe pattern): calls add_ref_instance(array) and releases it in Status::drop (dstorage.rs:1479-1483).
  • crates/victory-hle/src/modules/dstorage.rs:1497-1504 (enqueue_request):
    extern "system" fn enqueue_request(this: QueueRef, request: *const Request) {
        let Some(&request) = (unsafe { request.as_ref() }) else {
            return;
        };
        let note = Note::enqueued(&request);
        queue(this, Command::Read(Read { request, note }));
    }
    
    When request.options & MEMORY_SOURCE == 0, release::<File> (dstorage.rs:99-107) frees Box<Instance<File>> as soon as the caller drops its IDStorageFile refcount to 0 (e.g., right after Submit()). Worker threads later dereference request.source.cast::<Instance<File>>() in read_into (dstorage.rs:2035) and ErrorRecord::of (dstorage.rs:638).

2. WALLS_FILES Growth / Address Aliasing & Null-out Factory Leaks

  • crates/victory-hle/src/modules/dstorage.rs:163-164 (open_file) & 847-860 (walls_request):
    open_file unconditionally appends (file as usize, kept) to WALLS_FILES even when VICTORY_WALLS_TRACE is off, and File never removes its entry on release. walls_request scans files.iter().find(|f| f.0 == source) from the front, matching stale freed entries when heap addresses are reused.
  • crates/victory-hle/src/modules/dstorage.rs:164 (open_file), 187 (create_status_array), 229-243 (create_queue):
    All three allocate Instance::create(...) (and create_queue:229-232 creates a Win32 event via CreateEventW and inserts a Lane into lanes.lanes) before put(out, ...) checks out.is_null() and returns E_POINTER.

Impact

  • UAF: Crash or memory corruption in DirectStorage worker threads if the caller releases an IDStorageFile while requests referencing it remain queued or in flight.
  • Leaks & Trace Corruption: Unbounded WALLS_FILES growth, misattributed file paths on pointer reuse, and leaked Instance / Win32 event HANDLE / scheduler Lane objects on E_POINTER.

AI-Ready Fix Specification

  1. In enqueue_request, when request.options & MEMORY_SOURCE == 0 && !request.source.is_null(), call add_ref_instance(request.source.cast::<Instance<File>>()) and attach an RAII guard (or impl Drop on Read and Job, transferring ownership when moving from Read to Job in Lanes::start) that calls release::<File> on completion or close_lane.
  2. Guard if out.is_null() { return E_POINTER; } at the entry of open_file, create_status_array, and create_queue before allocating any Instance, CreateEventW handle, or Lane.
  3. Only record into WALLS_FILES (switched to a HashMap<usize, String>) when walls_trace() > 0, and remove the entry when File is dropped in release::<File>.
  4. Add unit tests covering queued-file refcount retention across release::<File> and null-out early E_POINTER.
**@⁠RUIZ-Enrique-25002136** on GitHub · 2026-12-02 09:14 UTC · [original](https://github.com/Androtim/MiniVI/issues/319) ## Summary Two memory/handle safety issues exist in `crates/victory-hle/src/modules/dstorage.rs`: 1. **Use-After-Free of `Instance<File>`:** `enqueue_request` copies a raw `Request` pointer (`request.source`) into the queue without incrementing the `File` COM refcount or releasing it on completion, unlike `enqueue_status`. 2. **Unbounded `WALLS_FILES` Leak / Address Aliasing & Factory Leaks on Null `out`:** `open_file` pushes `(file as usize, kept)` to `WALLS_FILES` unconditionally with no cleanup on `File` drop, and `open_file` / `create_status_array` / `create_queue` allocate `Instance` / Win32 event `HANDLE` / scheduler `Lane` before checking `out.is_null()`. ## Evidence (`file:line` & Code Proof) ### 1. Use-After-Free of `Instance<File>` in Queued Reads - **`crates/victory-hle/src/modules/dstorage.rs:1516` (`enqueue_status`, safe pattern):** calls `add_ref_instance(array)` and releases it in `Status::drop` (`dstorage.rs:1479-1483`). - **`crates/victory-hle/src/modules/dstorage.rs:1497-1504` (`enqueue_request`):** ```rust extern "system" fn enqueue_request(this: QueueRef, request: *const Request) { let Some(&request) = (unsafe { request.as_ref() }) else { return; }; let note = Note::enqueued(&request); queue(this, Command::Read(Read { request, note })); } ``` When `request.options & MEMORY_SOURCE == 0`, `release::<File>` (`dstorage.rs:99-107`) frees `Box<Instance<File>>` as soon as the caller drops its `IDStorageFile` refcount to 0 (e.g., right after `Submit()`). Worker threads later dereference `request.source.cast::<Instance<File>>()` in `read_into` (`dstorage.rs:2035`) and `ErrorRecord::of` (`dstorage.rs:638`). ### 2. `WALLS_FILES` Growth / Address Aliasing & Null-`out` Factory Leaks - **`crates/victory-hle/src/modules/dstorage.rs:163-164` (`open_file`) & `847-860` (`walls_request`):** `open_file` unconditionally appends `(file as usize, kept)` to `WALLS_FILES` even when `VICTORY_WALLS_TRACE` is off, and `File` never removes its entry on release. `walls_request` scans `files.iter().find(|f| f.0 == source)` from the front, matching stale freed entries when heap addresses are reused. - **`crates/victory-hle/src/modules/dstorage.rs:164` (`open_file`), `187` (`create_status_array`), `229-243` (`create_queue`):** All three allocate `Instance::create(...)` (and `create_queue:229-232` creates a Win32 event via `CreateEventW` and inserts a `Lane` into `lanes.lanes`) *before* `put(out, ...)` checks `out.is_null()` and returns `E_POINTER`. ## Impact - **UAF:** Crash or memory corruption in DirectStorage worker threads if the caller releases an `IDStorageFile` while requests referencing it remain queued or in flight. - **Leaks & Trace Corruption:** Unbounded `WALLS_FILES` growth, misattributed file paths on pointer reuse, and leaked `Instance` / Win32 event `HANDLE` / scheduler `Lane` objects on `E_POINTER`. ## AI-Ready Fix Specification 1. In `enqueue_request`, when `request.options & MEMORY_SOURCE == 0 && !request.source.is_null()`, call `add_ref_instance(request.source.cast::<Instance<File>>())` and attach an RAII guard (or `impl Drop` on `Read` and `Job`, transferring ownership when moving from `Read` to `Job` in `Lanes::start`) that calls `release::<File>` on completion or `close_lane`. 2. Guard `if out.is_null() { return E_POINTER; }` at the entry of `open_file`, `create_status_array`, and `create_queue` before allocating any `Instance`, `CreateEventW` handle, or `Lane`. 3. Only record into `WALLS_FILES` (switched to a `HashMap<usize, String>`) when `walls_trace() > 0`, and remove the entry when `File` is dropped in `release::<File>`. 4. Add unit tests covering queued-file refcount retention across `release::<File>` and null-`out` early `E_POINTER`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 09:15:39 +00:00
Author
Owner

@⁠github-actions[bot] on GitHub · 2026-12-02 09:14 UTC · original

Please attach both logs from the run that went wrong so we can investigate without guessing. Paste %LOCALAPPDATA% into the File Explorer address bar to find them:

  1. Play log in %LOCALAPPDATA%\victory\logs\: the newest .log from that run (the console prints its path at startup on the Log: ... line).
  2. Crash log: %LOCALAPPDATA%\victory\data\<TitleId>\crashes.log, and if present, the newest .crash.log in %LOCALAPPDATA%\victory\data\<TitleId>\temp\.

Please also include:

  • GPU & VRAM, Windows version (10 or 11)
  • MiniVI preset / launcher flags (play-minivi.ps1 preset, DLSS/FG/RT, custom VICTORY_* vars) and commit SHA

(Clean-room policy: please only attach text logs/screenshots — do not upload copyrighted game files or executables.)

**@⁠github-actions[bot]** on GitHub · 2026-12-02 09:14 UTC · [original](https://github.com/Androtim/MiniVI/issues/319#issuecomment-7340586957) Please attach both logs from the run that went wrong so we can investigate without guessing. Paste `%LOCALAPPDATA%` into the File Explorer address bar to find them: 1. **Play log** in `%LOCALAPPDATA%\victory\logs\`: the newest `.log` from that run (the console prints its path at startup on the `Log: ...` line). 2. **Crash log**: `%LOCALAPPDATA%\victory\data\<TitleId>\crashes.log`, and if present, the newest `.crash.log` in `%LOCALAPPDATA%\victory\data\<TitleId>\temp\`. Please also include: - **GPU & VRAM**, **Windows version** (10 or 11) - **MiniVI preset / launcher flags** (`play-minivi.ps1` preset, DLSS/FG/RT, custom `VICTORY_*` vars) and **commit SHA** *(Clean-room policy: please only attach text logs/screenshots — do not upload copyrighted game files or executables.)*
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/Androtim--MiniVI#319
No description provided.