2
0
Эх сурвалжийг харах

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.
Manuel Pérez 3 өдөр өмнө
parent
commit
ef22723e84

+ 0 - 11
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);
-    }
-}

+ 24 - 6
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<usize> {
     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<usize> {
     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<usize> {
     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<u32> {
-    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<u64> {
-    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);
+    }
 }