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]>
This commit is contained in:
Manuel PérezandClaude Opus 5.5 committed 2026-11-24 00:19:38 +01:00
1 parent 8c26061f3a
commit e98d3ca96e
1 file changed
+13 -1
+13 -1
View File
@@ -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());