Skip to content

Mach-O: Load bundles (MH_BUNDLE) and resolve their imports - #891

Open
NopeCharon wants to merge 1 commit into
angr:masterfrom
NopeCharon:feat/macho-bundle
Open

NopeCharon wants to merge 1 commit into
angr:masterfrom
NopeCharon:feat/macho-bundle

Conversation

@NopeCharon

Copy link
Copy Markdown

Fixes #444.

Test fixtures: angr/binaries#279. It needs to be merged first. CI checks it out from the reference here.

Problem

A Mach-O bundle (MH_BUNDLE) is the image dlopen loads as a plugin, and the format Python extension modules use on macOS. cle refuses to load one:

cle.errors.CLECompatibilityError: Unsupported Mach-O file type: 8. Please open an issue if you need support for this

Accepting the file type alone is not enough. A bundle's imports often name no library: with -bundle_loader they are bound to the main executable, and with -undefined dynamic_lookup they are bound by flat lookup. The backend handles neither:

  • An nlist stores "main executable" as ordinal 0xFF. A chained-fixup import stores the same 8-bit value, so it reuses that symbol, and SymbolTableSymbol.library_name indexes past the end of imported_libraries. Loading a bundle next to its host raises IndexError: list index out of range in MachOSymbolRelocation.resolve_symbol.
  • Bind opcodes store the special ordinals as negative numbers (BIND_OPCODE_SET_DYLIB_SPECIAL_IMM). BindingSymbol.library_name only checks for 0xFE, so -1 and -2 index imported_libraries from the end and quietly name the last library.

Changes

  • MH_BUNDLE takes the MH_DYLIB paths. It is position independent, placed at 0 when it is the main object, and placed by the loader when it is loaded as a dependency.
  • Chained-fixup import ordinals are read the way dyld reads them: the top 16 values of the 8- or 16-bit field are the negative BIND_SPECIAL_DYLIB_* values. Those constants in symbol.py now carry their mach-o/loader.h values (-1, -2, -3) instead of 0xFF, 0xFE, 0xFD.
  • Every Mach-O symbol class now has binds_to_main_executable and binds_by_flat_lookup, and library_name returns None for a special ordinal instead of indexing imported_libraries with it.
  • MachOSymbolRelocation.resolve_symbol resolves a main-executable import against the loader's main object, and a flat-lookup import against every candidate image in load order (main object first). It only uses exported symbols. If nothing provides the symbol, it falls back to the extern object as before. An import with no library name is no longer matched against an object whose binary_basename is None, such as one loaded from a stream.

Weak lookup (-3) still resolves to the extern object, as it did before.

Testing

The new tests in tests/test_macho.py use the fixtures from angr/binaries#279:

  • test_bundle loads the aarch64 bundle (chained fixups) and the x86_64 bundle (bind opcodes) on their own.
  • test_bundle_binds_to_bundle_loader loads each into its host executable. It checks that the -bundle_loader imports resolve to the host's exports and that the GOT slot holds the host's variable, while printf still goes through libSystem.
  • test_bundle_flat_lookup does the same for the -undefined dynamic_lookup bundle.

All three fail on master. The rest of tests/test_macho*.py and tests/test_universal2.py pass. On 10 existing Mach-O fixtures in angr/binaries, every relocation resolves to the same target with the same value as on master, for both path and stream loading.

A bundle is the image dlopen loads as a plugin, and the format Python
extension modules use on macOS. The Mach-O backend refused it with
"Unsupported Mach-O file type: 8".

A bundle is laid out like a dylib and only lacks an install name, so it
now takes the dylib paths: it is position independent, placed at 0 when
it is the main object, and placed by the loader when it is loaded as a
dependency.

A bundle's imports also often name no library. With -bundle_loader they
are bound to the main executable, and with -undefined dynamic_lookup
they are bound by flat lookup. The backend did not handle either:

- An nlist stores the main executable as ordinal 0xFF. Chained fixup
  imports store the same 8-bit value, so they reused that symbol, and
  its library_name indexed past the end of imported_libraries. Loading
  such a bundle next to its host raised an IndexError.
- Bind opcodes store the special ordinals as negative numbers. A
  BindingSymbol only checked for 0xFE, so -1 and -2 indexed
  imported_libraries from the end and quietly named the last library.

Chained fixup ordinals are now read as dyld reads them, with the top 16
values of the field taken as negative BIND_SPECIAL_DYLIB_* values. Those
constants now carry their mach-o/loader.h values. Every Mach-O symbol
class now has binds_to_main_executable and binds_by_flat_lookup, and
reports no library name for a special ordinal. MachOSymbolRelocation
resolves the first kind against the loader's main object and the second
against every candidate image in load order, using exported symbols
only. Each falls back to the extern object as before. An import with no
library name is no longer matched against an object whose
binary_basename is None.

The tests use the bundles from angr/binaries#279: aarch64 with chained
fixups, x86_64 with bind opcodes, and an x86_64 flat-lookup bundle. Each
is loaded both on its own and into its host executable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Loading a bundle file fails

1 participant