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

read/macho: handle invalid cmdsize and section align #516

Merged
merged 3 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
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
28 changes: 18 additions & 10 deletions crates/examples/src/objdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,28 @@ fn dump_parsed_object<W: Write, E: Write>(w: &mut W, e: &mut E, file: &object::F
}
}

let imports = file.imports().unwrap();
if !imports.is_empty() {
writeln!(w)?;
for import in imports {
writeln!(w, "{:?}", import)?;
match file.imports() {
Ok(imports) => {
if !imports.is_empty() {
writeln!(w)?;
for import in imports {
writeln!(w, "{:x?}", import)?;
}
}
}
Err(err) => writeln!(e, "Failed to parse imports: {}", err)?,
}

let exports = file.exports().unwrap();
if !exports.is_empty() {
writeln!(w)?;
for export in exports {
writeln!(w, "{:x?}", export)?;
match file.exports() {
Ok(exports) => {
if !exports.is_empty() {
writeln!(w)?;
for export in exports {
writeln!(w, "{:x?}", export)?;
}
}
}
Err(err) => writeln!(e, "Failed to parse exports: {}", err)?,
}

Ok(())
Expand Down
22 changes: 21 additions & 1 deletion src/read/macho/load_command.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use core::marker::PhantomData;
use core::mem;

use crate::endian::Endian;
use crate::macho;
use crate::pod::Pod;
use crate::read::macho::{MachHeader, SymbolTable};
use crate::read::{Bytes, ReadError, ReadRef, Result, StringTable};
use crate::read::{Bytes, Error, ReadError, ReadRef, Result, StringTable};

/// An iterator over the load commands of a `MachHeader`.
#[derive(Debug, Default, Clone, Copy)]
Expand Down Expand Up @@ -34,6 +35,9 @@ impl<'data, E: Endian> LoadCommandIterator<'data, E> {
.read_error("Invalid Mach-O load command header")?;
let cmd = header.cmd.get(self.endian);
let cmdsize = header.cmdsize.get(self.endian) as usize;
if cmdsize < mem::size_of::<macho::LoadCommand<E>>() {
return Err(Error("Invalid Mach-O load command size"));
}
let data = self
.data
.read_bytes(cmdsize)
Expand Down Expand Up @@ -351,3 +355,19 @@ impl<E: Endian> macho::SymtabCommand<E> {
Ok(SymbolTable::new(symbols, strings))
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::LittleEndian;

#[test]
fn cmd_size_invalid() {
let mut commands = LoadCommandIterator::new(LittleEndian, &[0; 8], 10);
assert!(commands.next().is_err());
let mut commands = LoadCommandIterator::new(LittleEndian, &[0, 0, 0, 0, 7, 0, 0, 0, 0], 10);
assert!(commands.next().is_err());
let mut commands = LoadCommandIterator::new(LittleEndian, &[0, 0, 0, 0, 8, 0, 0, 0, 0], 10);
assert!(commands.next().is_ok());
}
}
7 changes: 6 additions & 1 deletion src/read/macho/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ where

#[inline]
fn align(&self) -> u64 {
1 << self.internal.section.align(self.file.endian)
let align = self.internal.section.align(self.file.endian);
if align < 64 {
1 << align
} else {
0
}
}

#[inline]
Expand Down