Merge pull request #30 from AndyLatimer/fix/pe-rva-offset-overflow
PE: checked offsets in rva_to_file_offset and layout, remove dbg_tls
This commit is contained in:
commit
f5626996c3
2 files changed
+37
-18
No files matched your search
@@ -215,7 +215,8 @@ impl ImageInfo {
|
|||||||
0 => s.file_size,
|
0 => s.file_size,
|
||||||
vs => vs.min(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());
|
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]
|
#[test]
|
||||||
fn rejects_garbage_without_panicking() {
|
fn rejects_garbage_without_panicking() {
|
||||||
assert!(ImageInfo::parse(b"").is_err());
|
assert!(ImageInfo::parse(b"").is_err());
|
||||||
@@ -459,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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -67,26 +67,44 @@ pub mod subsystem {
|
|||||||
/// Offset of the optional header from the start of the file.
|
/// Offset of the optional header from the start of the file.
|
||||||
pub fn optional_header_offset(data: &[u8]) -> Option<usize> {
|
pub fn optional_header_offset(data: &[u8]) -> Option<usize> {
|
||||||
let lfanew = read_u32(data, DOS_E_LFANEW)? as 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.
|
/// Offset of the file header from the start of the file.
|
||||||
pub fn file_header_offset(data: &[u8]) -> Option<usize> {
|
pub fn file_header_offset(data: &[u8]) -> Option<usize> {
|
||||||
let lfanew = read_u32(data, DOS_E_LFANEW)? as 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.
|
/// Offset of data directory `index` from the start of the file.
|
||||||
pub fn data_directory_offset(data: &[u8], index: usize) -> Option<usize> {
|
pub fn data_directory_offset(data: &[u8], index: usize) -> Option<usize> {
|
||||||
let opt = optional_header_offset(data)?;
|
let opt = optional_header_offset(data)?;
|
||||||
let count = read_u32(data, opt + OPT_NUMBER_OF_RVA_AND_SIZES)? as usize;
|
let count = read_u32(data, opt.checked_add(OPT_NUMBER_OF_RVA_AND_SIZES)?)? as usize;
|
||||||
(index < count).then_some(opt + OPT_DATA_DIRECTORY + index * DATA_DIRECTORY_SIZE)
|
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> {
|
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> {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user