fix(pe): validate section raw data bounds
(cherry picked from commit e57e76a7b4058e50d665ddcc9a11eb3cd4de4e7d)
This commit is contained in:
1 parent
261ee42e99
commit
e190726c68
1 file changed
+94
-1
@@ -143,7 +143,7 @@ impl ImageInfo {
|
|||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let sections = file
|
let sections: Vec<SectionInfo> = file
|
||||||
.section_table()
|
.section_table()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| SectionInfo {
|
.map(|s| SectionInfo {
|
||||||
@@ -156,6 +156,31 @@ impl ImageInfo {
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
// Validate section raw-data boundaries before accepting
|
||||||
|
// the PE image.
|
||||||
|
for section in §ions {
|
||||||
|
if section.file_size == 0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let start = u64::from(section.file_offset);
|
||||||
|
let size = u64::from(section.file_size);
|
||||||
|
|
||||||
|
let Some(end) = start.checked_add(size) else {
|
||||||
|
return Err(PeError::Malformed(format!(
|
||||||
|
"section '{}' has invalid raw-data bounds",
|
||||||
|
section.name
|
||||||
|
)));
|
||||||
|
};
|
||||||
|
|
||||||
|
if end > data.len() as u64 {
|
||||||
|
return Err(PeError::Malformed(format!(
|
||||||
|
"section '{}' extends beyond file bounds",
|
||||||
|
section.name
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let dir_present = |index: usize| {
|
let dir_present = |index: usize| {
|
||||||
file.data_directory(index)
|
file.data_directory(index)
|
||||||
.is_some_and(|d| d.virtual_address.get(LE) != 0 && d.size.get(LE) != 0)
|
.is_some_and(|d| d.virtual_address.get(LE) != 0 && d.size.get(LE) != 0)
|
||||||
@@ -505,6 +530,74 @@ mod tests {
|
|||||||
d
|
d
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rejects_section_extending_beyond_file() {
|
||||||
|
// Create a synthetic PE image.
|
||||||
|
let mut data = synthetic_image(&vec![0u8; 0x200]);
|
||||||
|
|
||||||
|
// Remove the export directory to isolate section validation.
|
||||||
|
let opt = 0x58;
|
||||||
|
data[opt + 112..opt + 120].fill(0);
|
||||||
|
|
||||||
|
// Verify the baseline PE is accepted.
|
||||||
|
assert!(
|
||||||
|
ImageInfo::parse(&data).is_ok(),
|
||||||
|
"baseline synthetic image must be valid"
|
||||||
|
);
|
||||||
|
|
||||||
|
// The section header starts after the 240-byte optional header.
|
||||||
|
let sec = opt + 240;
|
||||||
|
|
||||||
|
// Declare 4096 bytes of raw section data.
|
||||||
|
// The actual file contains only 1024 bytes.
|
||||||
|
data[sec + 16..sec + 20].copy_from_slice(&0x1000u32.to_le_bytes());
|
||||||
|
|
||||||
|
// Investigate whether the parser rejects this malformed image.
|
||||||
|
assert!(
|
||||||
|
ImageInfo::parse(&data).is_err(),
|
||||||
|
"section extends beyond physical file bounds"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn accepts_section_ending_at_file_boundary() {
|
||||||
|
let mut data = synthetic_image(&vec![0u8; 0x200]);
|
||||||
|
|
||||||
|
// Disable the export directory to isolate section validation.
|
||||||
|
let opt = 0x58;
|
||||||
|
data[opt + 112..opt + 120].fill(0);
|
||||||
|
|
||||||
|
// Raw section: offset 0x200 + size 0x200 = file length 0x400.
|
||||||
|
let info = ImageInfo::parse(&data);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
info.is_ok(),
|
||||||
|
"section ending exactly at EOF should be accepted"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn accepts_section_with_zero_raw_size() {
|
||||||
|
let mut data = synthetic_image(&vec![0u8; 0x200]);
|
||||||
|
|
||||||
|
// Disable the export directory.
|
||||||
|
let opt = 0x58;
|
||||||
|
data[opt + 112..opt + 120].fill(0);
|
||||||
|
|
||||||
|
// Section header begins after the optional header.
|
||||||
|
let sec = opt + 240;
|
||||||
|
|
||||||
|
// A section may occupy virtual memory without having raw data.
|
||||||
|
data[sec + 16..sec + 20].copy_from_slice(&0u32.to_le_bytes());
|
||||||
|
|
||||||
|
let info = ImageInfo::parse(&data);
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
info.is_ok(),
|
||||||
|
"section with zero raw size should be accepted"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unused_export_slots_are_not_exports() {
|
fn unused_export_slots_are_not_exports() {
|
||||||
// Ordinal base 1; three address slots: "alpha", an unused hole (0),
|
// Ordinal base 1; three address slots: "alpha", an unused hole (0),
|
||||||
|
|||||||
Reference in new issue
Block a user