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

Fat parser should fail on file with 0 architectures #449

Merged
merged 4 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/macho/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def initialize
end
end

# Raised when a a fat Mach-O file has zero architectures
class ZeroArchitectureError < NotAMachOError
def initialize
super "Fat file has zero internal architectures"
end
end

# Raised when a fat binary is loaded with MachOFile.
class FatBinaryError < MachOError
def initialize
Expand Down
7 changes: 7 additions & 0 deletions lib/macho/fat_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ def to_h
# @raise [MagicError] if the magic is not valid Mach-O magic
# @raise [MachOBinaryError] if the magic is for a non-fat Mach-O file
# @raise [JavaClassFileError] if the file is a Java classfile
# @raise [ZeroArchitectureError] if the file has no internal slices
# (i.e., nfat_arch == 0) and the permissive option is not set
# @api private
def populate_fat_header
# the smallest fat Mach-O header is 8 bytes
Expand All @@ -346,6 +348,11 @@ def populate_fat_header
# formats.
raise JavaClassFileError if fh.nfat_arch > 30

# Rationale: fat Mach-0 files with no internal slices should
# return an error unless the permissive option is set.
permissive = options.fetch(:permissive, false)
Copy link
Member

Choose a reason for hiding this comment

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

IMO we can remove the permissive check here: there's very little a user can do with an empty universal binary, so we should fail unconditionally if we have no architecture slices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense.

raise ZeroArchitectureError if !permissive && fh.nfat_arch.zero?

fh
end

Expand Down
Binary file added test/bin/llvm/macho-invalid-fat-header
Binary file not shown.
10 changes: 9 additions & 1 deletion test/test_fat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def test_java_classfile
end
end

def test_zero_arch_file
assert_raises MachO::ZeroArchitectureError do
MachO::FatFile.new("test/bin/llvm/macho-invalid-fat-header")
end

MachO::FatFile.new("test/bin/llvm/macho-invalid-fat-header", :permissive => true)
end

def test_fat_header
filenames = FAT_ARCH_PAIRS.map { |a| fixture(a, "hello.bin") }

Expand Down Expand Up @@ -518,7 +526,7 @@ def test_dylib_load_commands
end

def test_fail_loading_thin
filename = fixture('x86_64', 'libhello.dylib')
filename = fixture("x86_64", "libhello.dylib")

ex = assert_raises(MachO::MachOBinaryError) do
MachO::FatFile.new_from_bin File.read(filename)
Expand Down