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

Add additional heuristic to distinguish .class and fat mach-o files #175

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 16 additions & 2 deletions apple-codesign/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl MachOType {
let mut fh = File::open(path.as_ref())?;

let mut header = vec![0u8; 4];
let count = fh.read(&mut header)?;
let mut count = fh.read(&mut header)?;

if count < 4 {
return Ok(None);
Expand All @@ -54,7 +54,21 @@ impl MachOType {
let magic = goblin::mach::peek(&header, 0)?;

if magic == FAT_MAGIC {
Ok(Some(Self::Mach))
// As java .class files and FAT Mach-O files share the same magic number
// we need another heuristic to distinguish them, taken from:
// https://stackoverflow.com/questions/73546728/magic-value-collision-between-macho-fat-binaries-and-java-class-files
count = fh.read(&mut header)?;

if count < 4 {
return Ok(None);
}

let architectures = goblin::mach::peek(&header, 0)?;
if architectures < 0x20 {
Ok(Some(Self::Mach))
} else{
Ok(None)
}
} else if let Ok((_, Some(_))) = parse_magic_and_ctx(&header, 0) {
Ok(Some(Self::MachO))
} else {
Expand Down
Loading