Pārlūkot izejas kodu

PE: checked file offset in rva_to_file_offset

A section whose PointerToRawData lies near 4 GiB made
file_offset + delta overflow: a panic in debug builds, a wrong offset in
release ones. The section now yields no offset.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Manuel Pérez 3 dienas atpakaļ
vecāks
revīzija
e98d3ca96e
1 mainītis faili ar 13 papildinājumiem un 1 dzēšanām
  1. 13 1
      crates/victory-pe/src/image.rs

+ 13 - 1
crates/victory-pe/src/image.rs

@@ -215,7 +215,8 @@ impl ImageInfo {
                 0 => s.file_size,
                 vs => vs.min(s.file_size),
             };
-            (delta < backed).then(|| s.file_offset + delta)
+            // Header values are untrusted: an offset near 4 GiB overflows.
+            (delta < backed).then(|| s.file_offset.checked_add(delta))?
         })
     }
 
@@ -433,6 +434,17 @@ mod tests {
         assert!(off < data.len());
     }
 
+    #[test]
+    fn rva_to_file_offset_rejects_offsets_past_4_gib() {
+        let mut info = ImageInfo::parse(&own_image()).unwrap();
+        let rva = info.sections[0].rva + 0x20;
+        assert!(info.rva_to_file_offset(rva).is_some());
+        // Used to overflow (a panic in debug builds, a wrong offset in
+        // release ones).
+        info.sections[0].file_offset = u32::MAX - 0x10;
+        assert_eq!(info.rva_to_file_offset(rva), None);
+    }
+
     #[test]
     fn rejects_garbage_without_panicking() {
         assert!(ImageInfo::parse(b"").is_err());