DirectStorage: execute allocates 4-12 GiB from unvalidated request sizes before checking source/file/pages #1392

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

dstorage::execute (crates/xcl/src/hle/dstorage.rs:872-893, 919) allocates heap buffers sized directly from guest-supplied 32-bit source_size and destination_size before validating that the source or destination can actually supply/accept that many bytes:

let src: Vec<u8> = if r.memory_source() {
    let mut b = vec![0u8; r.source_size as usize];
    if r.source_physical_pages() { ... } else {
        if !crate::gpu::shader::readable(r.source as usize, r.source_size as usize) {
            return Err(E_ACCESS_VIOLATION);
        }
        ...
    }
    b
} else {
    let f = file.and_then(|f| f.handle()).ok_or(E_FILE_HANDLE)?;
    let mut b = vec![0u8; r.source_size as usize];
    read_exact_at(&f, r.offset, &mut b)?;
    b
};
  1. Line 873 (memory source): vec![0u8; r.source_size as usize] allocates up to ~4 GiB before shader::readable (line 880, which rejects spans > 256 MiB) or read_physical_pages (line 876).
  2. Line 890 (file source): vec![0u8; r.source_size as usize] allocates up to ~4 GiB before checking f.metadata()?.len().
  3. Line 919 (BCPack): When DEST_PHYS_PAGES_BIT is set, line 869 skips writable(), and dest_pages are not validated until line 935. For BC6H/BC7 (block = 16, stride = 32), bcpack::decode (bcpack/mod.rs:63) allocates vec![0u8; out_len] (~4 GiB) and victory::decode (bcpack/victory.rs:112) allocates vec![0u8; blocks * 32] (~8 GiB) — ~12 GiB total — before inspecting any input byte or validating dest_pages.

Minimal trigger

Enqueue a DirectStorage request with DEST_PHYS_PAGES_BIT and a 512 KiB page-number array:

  • File request (source_size = destination_size = 0xFFFF_FFFF) against a 1-byte file → allocates 4 GiB before read_exact_at returns E_END_OF_FILE.
  • Memory request (source_size = destination_size = 0xFFFF_FFFF) → allocates 4 GiB before shader::readable rejects it.
  • BC7 request (source_size = 16, destination_size = 0xFFFF_FFF0) → allocates ~12 GiB across bcpack::decode + victory::decode before failing.

This causes an unrecoverable worker-thread OOM abort crashing XCL instead of returning E_END_OF_FILE / E_ACCESS_VIOLATION / E_DECOMPRESSION / E_OUTOFMEMORY.

Suggested fix

Validate shader::readable (memory), r.offset.checked_add(r.source_size as u64) <= f.metadata()?.len() (file), and dest_pages validity before allocating src or BCPack staging buffers, and use try_reserve_exact (returning E_OUTOFMEMORY) for large request allocations.


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/1392) Checked on `main` (`930e8b4`). ### Problem `dstorage::execute` (`crates/xcl/src/hle/dstorage.rs:872-893, 919`) allocates heap buffers sized directly from guest-supplied 32-bit `source_size` and `destination_size` **before** validating that the source or destination can actually supply/accept that many bytes: ```rust let src: Vec<u8> = if r.memory_source() { let mut b = vec![0u8; r.source_size as usize]; if r.source_physical_pages() { ... } else { if !crate::gpu::shader::readable(r.source as usize, r.source_size as usize) { return Err(E_ACCESS_VIOLATION); } ... } b } else { let f = file.and_then(|f| f.handle()).ok_or(E_FILE_HANDLE)?; let mut b = vec![0u8; r.source_size as usize]; read_exact_at(&f, r.offset, &mut b)?; b }; ``` 1. **Line 873 (memory source):** `vec![0u8; r.source_size as usize]` allocates up to ~4 GiB *before* `shader::readable` (line `880`, which rejects spans > 256 MiB) or `read_physical_pages` (line `876`). 2. **Line 890 (file source):** `vec![0u8; r.source_size as usize]` allocates up to ~4 GiB *before* checking `f.metadata()?.len()`. 3. **Line 919 (BCPack):** When `DEST_PHYS_PAGES_BIT` is set, line `869` skips `writable()`, and `dest_pages` are not validated until line `935`. For BC6H/BC7 (`block = 16`, `stride = 32`), `bcpack::decode` (`bcpack/mod.rs:63`) allocates `vec![0u8; out_len]` (~4 GiB) and `victory::decode` (`bcpack/victory.rs:112`) allocates `vec![0u8; blocks * 32]` (~8 GiB) — ~12 GiB total — before inspecting any input byte or validating `dest_pages`. ### Minimal trigger Enqueue a DirectStorage request with `DEST_PHYS_PAGES_BIT` and a 512 KiB page-number array: - File request (`source_size = destination_size = 0xFFFF_FFFF`) against a 1-byte file → allocates 4 GiB before `read_exact_at` returns `E_END_OF_FILE`. - Memory request (`source_size = destination_size = 0xFFFF_FFFF`) → allocates 4 GiB before `shader::readable` rejects it. - BC7 request (`source_size = 16, destination_size = 0xFFFF_FFF0`) → allocates ~12 GiB across `bcpack::decode` + `victory::decode` before failing. This causes an unrecoverable worker-thread OOM abort crashing XCL instead of returning `E_END_OF_FILE` / `E_ACCESS_VIOLATION` / `E_DECOMPRESSION` / `E_OUTOFMEMORY`. ### Suggested fix Validate `shader::readable` (memory), `r.offset.checked_add(r.source_size as u64) <= f.metadata()?.len()` (file), and `dest_pages` validity before allocating `src` or BCPack staging buffers, and use `try_reserve_exact` (returning `E_OUTOFMEMORY`) for large request allocations. --- **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.