Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elf: prevent overflow #243

Merged
merged 1 commit into from
Nov 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/elf/section_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,11 @@ if_alloc! {
}
/// Returns this section header's file offset range
pub fn file_range(&self) -> Range<usize> {
self.sh_offset as usize..self.sh_offset as usize + self.sh_size as usize
self.sh_offset as usize..(self.sh_offset as usize).saturating_add(self.sh_size as usize)
}
/// Returns this section header's virtual memory range
pub fn vm_range(&self) -> Range<usize> {
self.sh_addr as usize..self.sh_addr as usize + self.sh_size as usize
self.sh_addr as usize..(self.sh_addr as usize).saturating_add(self.sh_size as usize)
}
/// Parse `count` section headers from `bytes` at `offset`, using the given `ctx`
#[cfg(feature = "endian_fd")]
Expand Down Expand Up @@ -473,6 +473,12 @@ if_alloc! {
self.sh_name, self.sh_offset, self.sh_size, overflow);
return Err(error::Error::Malformed(message));
}
let (end, overflow) = self.sh_addr.overflowing_add(self.sh_size);
if overflow || end > size as u64 {
let message = format!("Section {} size ({}) + addr ({}) is out of bounds. Overflowed: {}",
self.sh_name, self.sh_addr, self.sh_size, overflow);
return Err(error::Error::Malformed(message));
}
Ok(())
}
pub fn is_relocation(&self) -> bool {
Expand Down