From ef22723e84e4e0a48edcda67fb4d71b6adc08054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9rez?= Date: Tue, 24 Nov 2026 00:43:16 +0100 Subject: [PATCH] PE: remove leftover dbg_tls test and check layout offsets Removes the print-only dbg_tls test in image.rs (TLS presence is already asserted in parses_own_test_binary) and switches layout.rs offset helpers (read_u32, read_u64, optional_header_offset, file_header_offset, data_directory_offset) to checked arithmetic so extreme offsets return None instead of overflowing. --- crates/victory-pe/src/image.rs | 11 ----------- crates/victory-pe/src/layout.rs | 30 ++++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/crates/victory-pe/src/image.rs b/crates/victory-pe/src/image.rs index 8e7f542..b9e54b2 100644 --- a/crates/victory-pe/src/image.rs +++ b/crates/victory-pe/src/image.rs @@ -471,14 +471,3 @@ mod tests { )); } } - -#[cfg(test)] -mod dbg_tls { - #[test] - fn dump_tls() { - let data = std::fs::read(std::env::current_exe().unwrap()).unwrap(); - let info = super::ImageInfo::parse(&data).unwrap(); - eprintln!("TLS = {:?}", info.tls); - eprintln!("image_base = {:x}", info.image_base); - } -} diff --git a/crates/victory-pe/src/layout.rs b/crates/victory-pe/src/layout.rs index fd99b7f..09330a9 100644 --- a/crates/victory-pe/src/layout.rs +++ b/crates/victory-pe/src/layout.rs @@ -66,26 +66,44 @@ pub mod subsystem { /// Offset of the optional header from the start of the file. pub fn optional_header_offset(data: &[u8]) -> Option { let lfanew = read_u32(data, DOS_E_LFANEW)? as usize; - Some(lfanew + NT_SIGNATURE_SIZE + FILE_HEADER_SIZE) + lfanew.checked_add(NT_SIGNATURE_SIZE + FILE_HEADER_SIZE) } /// Offset of the file header from the start of the file. pub fn file_header_offset(data: &[u8]) -> Option { let lfanew = read_u32(data, DOS_E_LFANEW)? as usize; - Some(lfanew + NT_SIGNATURE_SIZE) + lfanew.checked_add(NT_SIGNATURE_SIZE) } /// Offset of data directory `index` from the start of the file. pub fn data_directory_offset(data: &[u8], index: usize) -> Option { let opt = optional_header_offset(data)?; - let count = read_u32(data, opt + OPT_NUMBER_OF_RVA_AND_SIZES)? as usize; - (index < count).then_some(opt + OPT_DATA_DIRECTORY + index * DATA_DIRECTORY_SIZE) + let count = read_u32(data, opt.checked_add(OPT_NUMBER_OF_RVA_AND_SIZES)?)? as usize; + if index >= count { + return None; + } + opt.checked_add(OPT_DATA_DIRECTORY)? + .checked_add(index.checked_mul(DATA_DIRECTORY_SIZE)?) } pub fn read_u32(data: &[u8], at: usize) -> Option { - Some(u32::from_le_bytes(data.get(at..at + 4)?.try_into().ok()?)) + let end = at.checked_add(4)?; + Some(u32::from_le_bytes(data.get(at..end)?.try_into().ok()?)) } pub fn read_u64(data: &[u8], at: usize) -> Option { - Some(u64::from_le_bytes(data.get(at..at + 8)?.try_into().ok()?)) + let end = at.checked_add(8)?; + Some(u64::from_le_bytes(data.get(at..end)?.try_into().ok()?)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reads_reject_overflowing_offsets() { + let data = [0u8; 16]; + assert_eq!(read_u32(&data, usize::MAX - 1), None); + assert_eq!(read_u64(&data, usize::MAX - 2), None); + } }