bx-probe: harden PE32+ parsing and add real coverage

probe_pe32_plus parses attacker-controlled bytes and drives every
bx-cli import report and the system32 staging dependency walk, yet
it had no test at all (the crate's one test covered classify_dll).

Four defects fixed:

1. rva_to_off computed s.raw_ptr + delta as a u32 add, which wraps
   on a crafted image: 0xFFFFFFF0 + 0x20 -> 0x10, a byte offset near
   the start of the file instead of ~4 GiB. Now a checked 64-bit-safe
   add returning None on overflow.
2. Vec::with_capacity(section_count) allocated from an unvalidated
   u16 before the section table was bounds-checked. The table is now
   validated to fit first.
3. u32_le(...).unwrap_or(0) on the import data-directory read treated
   a truncated directory as 'no imports', indistinguishable from a
   genuine zero. Now a real error.
4. The import descriptor walk is bounded by the image size and by a
   checked desc_off increment, so a malformed table cannot run past
   the end of the image.

Nine new unit tests built from synthetic byte buffers: a minimal
valid PE32+, a two-DLL import table (one by name, one by ordinal),
truncation at every header boundary, a section whose raw_ptr would
wrap, section_count larger than the file, a PE32 image, and an import
directory outside all sections. The wrap test was confirmed to fail
when the u32 add is re-introduced and to pass with the fix.

PeSummary now derives PartialEq/Eq for the assertions. Every public
signature is unchanged for bx-cli; cargo test --locked --workspace
passes.

Closes #507

Signed-off-by: Eterna <295740124+EternaPeptix@users.noreply.github.com>
This commit is contained in:
Eterna committed 2026-11-27 00:30:53 -07:00
1 parent b8724bf04f
commit b15c33e804
2 files changed
+193 -10

No files matched your search

+3
View File
@@ -10,6 +10,9 @@ Guest execution uses original, purpose-built test programs. Commercial Xbox game
remains unsupported.
### Added
- `bx-probe`'s PE32+ parser is hardened and now has real coverage (#507). `rva_to_off` computed `s.raw_ptr + delta` as a `u32` add, which wraps on a crafted image (e.g. `0xFFFFFFF0 + 0x20` → `0x10`, a byte offset near the start of the file instead of ~4 GiB). It now uses a checked 64-bit-safe add and returns `None` on overflow. `Vec::with_capacity(section_count)` allocated from an unvalidated `u16` before the section table was bounds-checked; the table is now validated to fit first. `u32_le(...).unwrap_or(0)` on the import data-directory read treated a truncated directory as "no imports", indistinguishable from a genuine zero; it is now a real error. The import descriptor walk is bounded by the image size and by a checked `desc_off` increment, so a malformed table cannot run past the end.
- **Tests:** 9 new unit tests built from synthetic byte buffers (no real DLLs, no network): a minimal valid PE32+, a two-DLL import table (one by name, one by ordinal), truncation at every header boundary, a section whose `raw_ptr` would wrap, `section_count` larger than the file, a PE32 (0x010B) image, and an import directory outside all sections. `rejects_section_rva_mapping_that_would_wrap_u32` was confirmed to **fail** when the `u32` add is re-introduced and to pass with the fix. `PeSummary` now derives `PartialEq`/`Eq` for the assertions.
- **Compatibility:** every public signature in `PeSummary`, `ProbeError`, `probe_pe32_plus` and `classify_dll` is unchanged for `bx-cli`; `cargo test --locked --workspace` (including `bx-cli`) passes.
- Placed resources created via `CreatePlacedResource` retain the shim heap's budget charge until their final release. The D3D12 backing map returned in `cpu_pointer` expires when the region changes or another host device replaces the cache. Concurrent device creations wait for both proxy hooks to be installed (#464).
- `D3D12ConfigureVirtualMemoryX` copies a BetterXen version-1 block only when the first 8 bytes are magic `BXVM` (`0x4D565842`) and the struct size. The caller's pointer is not kept. `cpu_pointer` is written only before that call returns (the current persistent map, or 0). After `CreateHeap` builds the backing heap, a second call with the same region reads the map and does not recreate the heap. `CreateHeap` returns that one `WRITE_BACK` / `L0` heap only when the block sets `BX_D3D12_VMEM_FLAG_BACKING_HEAP` and the desc is `CUSTOM`, `WRITE_BACK`, `L0`, `D3D12_HEAP_FLAG_NONE`, alignment 0 or 64 KiB, node masks 0 or 1, and the configured size. Every other `CreateHeap` forwards the desc unchanged and returns the host `HRESULT`, wrapped so the live-heap charge against the xmem title budget is credited on the final `Release` (`E_OUTOFMEMORY` only when that budget is exceeded). The wrapper answers `ID3D12Heap1` when the host heap does. `CreatePlacedResource` on any other heap forwards the placement parameters to the host. On the backing heap the offset must be a multiple of the alignment `GetResourceAllocationInfo` reports and the resource must fit. A null output pointer on `CreateHeap` or `CreatePlacedResource` is forwarded to the host with no budget charge and no wrapper. `GetDevice` on every heap wrapper returns the `BxDeviceProxy`. An ordinary heap `AddRef`s that proxy until the wrapper's final `Release`, matching `BxChildProxy`, so the caller can release the device and keep the heap. The process-wide backing heap does not: the cache would keep the device and its budget charge alive. Its pointer is weak. It is cleared when that heap leaves the cache, including a region change or a different host device, and when the proxy's reference count goes from 1 to 0. A weak `GetDevice` never increments a count that is already zero, and that acquire shares one lock with the 1-to-0 release, so only one thread runs final cleanup. Handing out the cached heap checks the host and the configured size, retargets `GetDevice`, and takes the caller's reference in that same lock. A concurrent backing configuration change makes an in-flight backing request return `HRESULT_FROM_WIN32(ERROR_RETRY)` before it can publish a stale heap. `CreatePlacedResource1` and `CreatePlacedResource2` unwrap the wrapper before calling the host. `test_d3d12_placed.exe` checks aliasing, ordinary heaps, small-resource placement, `ID3D12Heap1`, config lifetime, the write-through pointer, a fence-separated GPU copy in both directions, that an ordinary heap keeps its device alive, that `GetDevice` fails closed after a reconfigure and after a forced host replacement even once the proxy is released, that those newer placement calls accept a wrapped heap, and that a second proxy forced onto the same host device places a resource in the configured region without doubling the charge (#464).
- `XCRDAPI.dll` completes a multi-request batch from one `Submit()` in queue order into CPU memory, mapped D3D12 upload buffers, and default buffers (a copy on the resource's device when the heap is not CPU-visible). If that copy has been submitted and its GPU completion is not confirmed, the shim keeps the destination and the copy's GPU objects alive and logs a warning, unless the device was removed or the fence already reached the waited value. The request still returns the failure HRESULT. LZ, BCPack, and GDeflate requests finish as `E_NOTIMPL` and log `BX_VTABLE_STUB_LOG`. `DStorageGetFactory` remains the only export (#467).
+190 -10
View File
@@ -73,7 +73,7 @@ pub fn classify_dll(dll_name: &str) -> ShimTrack {
}
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PeSummary {
pub image_base: u64,
pub size_of_image: u32,
@@ -133,7 +133,7 @@ fn rva_to_off(sections: &[Section], rva: u32) -> Option<usize> {
if rva >= s.vaddr && rva < s.vaddr.saturating_add(span) {
let delta = rva - s.vaddr;
if delta < s.raw_size {
return Some((s.raw_ptr + delta) as usize);
return (s.raw_ptr as usize).checked_add(delta as usize);
}
}
}
@@ -167,8 +167,14 @@ pub fn probe_pe32_plus(bytes: &[u8]) -> Result<PeSummary, ProbeError> {
let size_of_image = u32_le(bytes, opt + 56).ok_or(ProbeError::NotPe32Plus("size_of_image"))?;
let num_rvas = u32_le(bytes, opt + 108).ok_or(ProbeError::NotPe32Plus("num_rvas"))?;
let mut sections = Vec::with_capacity(section_count as usize);
let sec_base = opt + opt_size;
let section_table_size = (section_count as usize)
.checked_mul(40)
.ok_or(ProbeError::NotPe32Plus("section table size overflow"))?;
bytes
.get(sec_base..sec_base + section_table_size)
.ok_or(ProbeError::NotPe32Plus("truncated section table"))?;
let mut sections = Vec::with_capacity(section_count as usize);
for i in 0..section_count as usize {
let so = sec_base + i * 40;
let vsize = u32_le(bytes, so + 8).ok_or(ProbeError::NotPe32Plus("sec vsize"))?;
@@ -185,15 +191,20 @@ pub fn probe_pe32_plus(bytes: &[u8]) -> Result<PeSummary, ProbeError> {
let mut imports: BTreeMap<String, Vec<String>> = BTreeMap::new();
if num_rvas > 1 {
let imp_rva = u32_le(bytes, opt + 112 + 8).unwrap_or(0);
let imp_rva = u32_le(bytes, opt + 112 + 8)
.ok_or(ProbeError::NotPe32Plus("truncated import directory"))?;
if imp_rva != 0
&& let Some(mut desc_off) = rva_to_off(&sections, imp_rva)
{
while let (Some(orig_thunk), Some(name_rva), Some(first_thunk)) = (
u32_le(bytes, desc_off),
u32_le(bytes, desc_off + 12),
u32_le(bytes, desc_off + 16),
) {
let max_descriptors = bytes.len() / 20 + 1;
for _ in 0..max_descriptors {
let (Some(orig_thunk), Some(name_rva), Some(first_thunk)) = (
u32_le(bytes, desc_off),
u32_le(bytes, desc_off + 12),
u32_le(bytes, desc_off + 16),
) else {
break;
};
if orig_thunk == 0 && name_rva == 0 && first_thunk == 0 {
break;
}
@@ -224,7 +235,10 @@ pub fn probe_pe32_plus(bytes: &[u8]) -> Result<PeSummary, ProbeError> {
}
imports.entry(dll).or_default().extend(syms);
}
desc_off += 20;
desc_off = match desc_off.checked_add(20) {
Some(off) => off,
None => break,
};
}
}
}
@@ -242,6 +256,78 @@ pub fn probe_pe32_plus(bytes: &[u8]) -> Result<PeSummary, ProbeError> {
mod tests {
use super::*;
const PE_OFF: usize = 0x80;
const COFF: usize = PE_OFF + 4;
const OPT: usize = COFF + 20;
const OPT_SIZE: usize = 0xF0;
const SEC: usize = OPT + OPT_SIZE;
const SECTION_RVA: u32 = 0x1000;
const RAW_PTR: u32 = 0x200;
fn put_u16(bytes: &mut [u8], off: usize, value: u16) {
bytes[off..off + 2].copy_from_slice(&value.to_le_bytes());
}
fn put_u32(bytes: &mut [u8], off: usize, value: u32) {
bytes[off..off + 4].copy_from_slice(&value.to_le_bytes());
}
fn put_u64(bytes: &mut [u8], off: usize, value: u64) {
bytes[off..off + 8].copy_from_slice(&value.to_le_bytes());
}
fn minimal_pe32_plus() -> Vec<u8> {
let mut bytes = vec![0u8; 0x400];
bytes[0..2].copy_from_slice(b"MZ");
put_u32(&mut bytes, 0x3C, PE_OFF as u32);
bytes[PE_OFF..PE_OFF + 4].copy_from_slice(b"PE\0\0");
put_u16(&mut bytes, COFF, 0x8664);
put_u16(&mut bytes, COFF + 2, 1);
put_u16(&mut bytes, COFF + 16, OPT_SIZE as u16);
put_u16(&mut bytes, OPT, 0x020B);
put_u32(&mut bytes, OPT + 16, 0x1234);
put_u64(&mut bytes, OPT + 24, 0x0000_0001_4000_0000);
put_u32(&mut bytes, OPT + 56, 0x2000);
put_u32(&mut bytes, OPT + 108, 16);
bytes[SEC..SEC + 8].copy_from_slice(b".rdata\0\0");
put_u32(&mut bytes, SEC + 8, 0x200);
put_u32(&mut bytes, SEC + 12, SECTION_RVA);
put_u32(&mut bytes, SEC + 16, 0x200);
put_u32(&mut bytes, SEC + 20, RAW_PTR);
bytes
}
fn two_import_pe32_plus() -> Vec<u8> {
let mut bytes = minimal_pe32_plus();
let import_rva = SECTION_RVA;
put_u32(&mut bytes, OPT + 112 + 8, import_rva);
put_u32(&mut bytes, OPT + 112 + 12, 0x140);
// Two descriptors followed by the required all-zero terminator.
put_u32(&mut bytes, RAW_PTR as usize, SECTION_RVA + 0x80);
put_u32(&mut bytes, RAW_PTR as usize + 12, SECTION_RVA + 0x40);
put_u32(&mut bytes, RAW_PTR as usize + 16, SECTION_RVA + 0x80);
put_u32(&mut bytes, RAW_PTR as usize + 20, SECTION_RVA + 0xA0);
put_u32(&mut bytes, RAW_PTR as usize + 32, SECTION_RVA + 0x50);
put_u32(&mut bytes, RAW_PTR as usize + 36, SECTION_RVA + 0xA0);
bytes[RAW_PTR as usize + 0x40..RAW_PTR as usize + 0x4A].copy_from_slice(b"ALPHA.DLL\0");
bytes[RAW_PTR as usize + 0x50..RAW_PTR as usize + 0x59].copy_from_slice(b"BETA.DLL\0");
put_u64(
&mut bytes,
RAW_PTR as usize + 0x80,
u64::from(SECTION_RVA + 0x60),
);
put_u64(&mut bytes, RAW_PTR as usize + 0x88, 0);
put_u64(&mut bytes, RAW_PTR as usize + 0xA0, (1u64 << 63) | 7);
put_u64(&mut bytes, RAW_PTR as usize + 0xA8, 0);
put_u16(&mut bytes, RAW_PTR as usize + 0x60, 0x1234);
bytes[RAW_PTR as usize + 0x62..RAW_PTR as usize + 0x6C].copy_from_slice(b"NamedFunc\0");
bytes
}
#[test]
fn classifies_tracks_accurately() {
assert_eq!(classify_dll("d3d12_xs.dll"), ShimTrack::TrackCGraphics);
@@ -263,4 +349,98 @@ mod tests {
);
assert_eq!(classify_dll("KERNEL32.dll"), ShimTrack::WineBuiltin);
}
#[test]
fn parses_minimal_pe32_plus_without_imports() {
let summary = probe_pe32_plus(&minimal_pe32_plus()).unwrap();
assert_eq!(summary.entry_point_rva, 0x1234);
assert_eq!(summary.image_base, 0x0000_0001_4000_0000);
assert_eq!(summary.size_of_image, 0x2000);
assert_eq!(summary.section_count, 1);
assert!(summary.imports.is_empty());
}
#[test]
fn parses_named_and_ordinal_imports_from_two_dlls() {
let summary = probe_pe32_plus(&two_import_pe32_plus()).unwrap();
let expected = BTreeMap::from([
("ALPHA.DLL".to_owned(), vec!["NamedFunc".to_owned()]),
("BETA.DLL".to_owned(), vec!["ORDINAL:7".to_owned()]),
]);
assert_eq!(summary.imports, expected);
}
#[test]
fn rejects_truncation_at_every_header_boundary() {
let image = minimal_pe32_plus();
for len in 0..=0x100 {
let result = std::panic::catch_unwind(|| probe_pe32_plus(&image[..len]));
assert!(result.is_ok(), "panic at truncation 0x{len:X}");
assert!(result.unwrap().is_err(), "accepted truncation 0x{len:X}");
}
}
#[test]
fn rejects_section_rva_mapping_that_would_wrap_u32() {
let mut image = minimal_pe32_plus();
put_u32(&mut image, OPT + 112 + 8, SECTION_RVA + 1);
put_u32(&mut image, SEC + 20, u32::MAX);
let result = std::panic::catch_unwind(|| probe_pe32_plus(&image));
assert!(result.is_ok(), "raw pointer addition panicked");
assert!(result.unwrap().unwrap().imports.is_empty());
}
#[test]
fn rejects_section_count_larger_than_file() {
let mut image = minimal_pe32_plus();
put_u16(&mut image, COFF + 2, u16::MAX);
assert!(probe_pe32_plus(&image).is_err());
}
#[test]
fn rejects_pe32_magic() {
let mut image = minimal_pe32_plus();
put_u16(&mut image, OPT, 0x010B);
assert_eq!(
probe_pe32_plus(&image),
Err(ProbeError::NotPe32Plus("not PE32+ (0x020B)"))
);
}
#[test]
fn import_directory_outside_all_sections_is_safe() {
let mut image = minimal_pe32_plus();
put_u32(&mut image, OPT + 112 + 8, 0x8000);
let summary = probe_pe32_plus(&image).unwrap();
assert!(summary.imports.is_empty());
}
#[test]
fn rejects_truncated_import_directory() {
let mut image = minimal_pe32_plus();
image.truncate(OPT + 112 + 8 + 2);
assert!(probe_pe32_plus(&image).is_err());
}
#[test]
fn bounds_unterminated_import_descriptor_walk() {
let mut image = minimal_pe32_plus();
put_u32(&mut image, OPT + 112 + 8, SECTION_RVA);
put_u32(&mut image, SEC + 8, 0x200);
put_u32(&mut image, SEC + 16, 0x200);
for off in (RAW_PTR as usize..image.len().saturating_sub(20)).step_by(20) {
put_u32(&mut image, off, SECTION_RVA + 0x80);
put_u32(&mut image, off + 12, SECTION_RVA + 0x40);
put_u32(&mut image, off + 16, SECTION_RVA + 0x80);
}
bytes_at(&mut image, RAW_PTR as usize + 0x40, b"NOEND.DLL\0");
put_u64(&mut image, RAW_PTR as usize + 0x80, 0);
let result = std::panic::catch_unwind(|| probe_pe32_plus(&image));
assert!(result.is_ok(), "unterminated descriptor table panicked");
assert!(result.unwrap().is_ok());
}
fn bytes_at(image: &mut [u8], off: usize, value: &[u8]) {
image[off..off + value.len()].copy_from_slice(value);
}
}