From c4d0b6c000b61af112208b902ade586daa8d59da Mon Sep 17 00:00:00 2001 From: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:57:26 +0530 Subject: [PATCH] loader: stop the static probe panicking on truncated PE names (#270) `probe_pe_bytes` panicked on a truncated PE instead of returning an error or reporting the import as unreadable. The cause was in `crates/loader/src/probe.rs`. `rva_to_offset` maps an RVA to a file offset using the section header, so on a truncated file the computed offset can point past the end of the buffer. The loop that finds the terminating NUL then stopped at `bytes.len()`, leaving `no > end`, and the slice `bytes[no..end]` panicked with "range start index 1632 out of range for slice of length 1556". Fix both name reads: - `rva_to_offset` now returns `None` when the computed offset is at or past the end of the file, because such an offset is not readable. This applies to every caller, so the relocation, TLS, thunk and disassembly paths are covered by the same rule. - A new `read_c_string` helper reads a NUL-terminated string and returns `None` when the string starts outside the buffer. It replaces the hand-written scan in the DLL-name read and in the symbol-name read, which also had an unchecked `ho + 2` for the two-byte import hint. A name that cannot be read is reported as `UNKNOWN.dll` or `UNKNOWN_SYM`, which is what the probe already did when the RVA did not resolve at all. The probe stays read-only and still performs no guest execution. Add a regression test that truncates the in-repo `fixtures/pe/trusted-runner.exe` at every length from 1 to the file size and asserts that `probe_pe_bytes` never panics. The reported range was 1,556 to 1,665 bytes; the sweep covers the whole file, so it also covers the other name and thunk reads that the report noted might share the pattern. The test fails against the unmodified probe with the reported panic and passes with this change. Closes #270 Signed-off-by: Swastick Chowdhury <28708247+Swastickc@users.noreply.github.com> --- crates/loader/src/probe.rs | 56 +++++++++++++++++-------- crates/loader/tests/probe_truncation.rs | 18 ++++++++ 2 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 crates/loader/tests/probe_truncation.rs diff --git a/crates/loader/src/probe.rs b/crates/loader/src/probe.rs index 9e5c8b7..f14c763 100644 --- a/crates/loader/src/probe.rs +++ b/crates/loader/src/probe.rs @@ -791,17 +791,42 @@ pub fn probe_pe_bytes(bytes: &[u8], config: &ProbeConfig) -> Result Option { for sec in §ions { let max_span = std::cmp::max(sec.virtual_size, sec.raw_data_size); if rva >= sec.virtual_address && rva < sec.virtual_address.saturating_add(max_span) { let off_in_sec = (rva - sec.virtual_address) as usize; - return Some(sec.raw_data_ptr as usize + off_in_sec); + let off = sec.raw_data_ptr as usize + off_in_sec; + if off >= bytes.len() { + return None; + } + return Some(off); } } None }; + // Read a NUL-terminated string that starts at `start`. + // + // Returns `None` when the string starts outside the buffer or is not + // terminated, so a truncated file yields an unreadable name instead of a + // slice that panics. + let read_c_string = |start: usize| -> Option { + if start >= bytes.len() { + return None; + } + let mut end = start; + while end < bytes.len() && bytes[end] != 0 { + end += 1; + } + Some(String::from_utf8_lossy(&bytes[start..end]).into_owned()) + }; + // 1. Relocations Census let mut relocations = RelocationCensus { total_entries: 0, @@ -895,14 +920,11 @@ pub fn probe_pe_bytes(bytes: &[u8], config: &ProbeConfig) -> Result name, + // The name is outside the file or is not NUL-terminated + // within it, which a truncated file can cause. + None => "UNKNOWN.dll".to_string(), }; let category = ProbeReport::categorize_dll(&dll_name); @@ -925,15 +947,13 @@ pub fn probe_pe_bytes(bytes: &[u8], config: &ProbeConfig) -> Result name, + None => "UNKNOWN_SYM".to_string(), } }; diff --git a/crates/loader/tests/probe_truncation.rs b/crates/loader/tests/probe_truncation.rs new file mode 100644 index 0000000..9ff771b --- /dev/null +++ b/crates/loader/tests/probe_truncation.rs @@ -0,0 +1,18 @@ +use xenora_loader::probe::{ProbeConfig, probe_pe_bytes}; + +/// The static probe must never panic on a truncated PE. +/// +/// The fixture is truncated across its whole length, which covers the range +/// where the import directory and the import names overlap the end of the +/// file, and every other range where a name or thunk read computed from the +/// section header can run past the buffer. +#[test] +fn probe_never_panics_on_truncated_input() { + let pe = include_bytes!("../../../fixtures/pe/trusted-runner.exe"); + for len in 1..=pe.len() { + let truncated = &pe[..len]; + // The probe may reject the file or report it with unreadable + // imports, but it must not panic. + let _ = probe_pe_bytes(truncated, &ProbeConfig::default()); + } +}