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

fix: Avoid arithmetic overflow and panics #302

Merged
merged 6 commits into from
Feb 14, 2022
Merged
Changes from 1 commit
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
21 changes: 19 additions & 2 deletions src/mach/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,16 @@ impl<'a> ExportTrie<'a> {
/// Create a new, lazy, zero-copy export trie from the `DyldInfo` `command`
pub fn new(bytes: &'a [u8], command: &load_command::DyldInfoCommand) -> Self {
let start = command.export_off as usize;
let end = (command.export_size + command.export_off) as usize;
let end = start.saturating_add(command.export_size as usize);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this silently truncates maybe should do checked add and warn when it’s too large ? I assume this would only ever happen on a 32-bit system reading the 6GB binary ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some log::warn for this and the FatArch case.
This here I found via fuzzing, though the FatArch was indeed a valid file.

// FIXME: Ideally, this should validate `command`, but the best we can
// do for now is return an empty `Range`.
let mut location = start..end;
if bytes.get(location.clone()).is_none() {
location = 0..0;
}
ExportTrie {
data: bytes,
location: start..end,
location,
}
}
}
Expand Down Expand Up @@ -316,4 +322,15 @@ mod tests {
println!("len: {} exports: {:#?}", exports.len(), &exports);
assert_eq!(exports.len() as usize, 3usize)
}

#[test]
fn invalid_range() {
let mut command = load_command::DyldInfoCommand::default();
command.export_off = 0xffff_ff00;
command.export_size = 0x00ff_ff00;
let trie = ExportTrie::new(&[], &command);
// FIXME: it would have been nice if this were an `Err`.
let exports = trie.exports(&[]).unwrap();
assert_eq!(exports.len(), 0);
}
}