mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 22:50:44 +00:00
`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>
19 lines
772 B
Rust
19 lines
772 B
Rust
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());
|
|
}
|
|
}
|