Skip to content

Add pure-Ruby ad-hoc signing#997

Open
MikeMcQuaid wants to merge 1 commit into
mainfrom
pure-ruby-code-signing
Open

Add pure-Ruby ad-hoc signing#997
MikeMcQuaid wants to merge 1 commit into
mainfrom
pure-ruby-code-signing

Conversation

@MikeMcQuaid

Copy link
Copy Markdown
Member
  • remove the /usr/bin/codesign dependency from MachO.codesign! so Homebrew can repair bottle signatures on Apple Silicon using Ruby
  • parse and build XNU-compatible SuperBlob and CodeDirectory data so replacement signatures can retain security-relevant metadata
  • add or replace LC_CODE_SIGNATURE, resize __LINKEDIT and rebuild fat offsets so every page hash describes the final on-disk layout
  • use SHA-256 with legacy SHA-1 agility only for old deployment targets because XNU accepts the open ld64 ad-hoc format without an identity
  • preserve non-linker requirements, entitlements, flags and runtime metadata while deliberately replacing linker-generated metadata
  • validate before an in-place write to retain hard links and leave failed inputs unchanged
  • cover thin, fat, byte-order, metadata and macOS verification paths with regression tests, and document the compatibility boundaries

Fixes #262

Copilot AI review requested due to automatic review settings July 10, 2026 16:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a pure-Ruby implementation of ad-hoc code signing for Mach-O binaries, removing the runtime dependency on invoking /usr/bin/codesign and enabling signature repair after Mach-O modifications (notably for Homebrew bottle relocation on Apple Silicon).

Changes:

  • Introduces a new MachO::CodeSigning implementation that can build and embed XNU-compatible SuperBlob/CodeDirectory signatures (with SHA-256 and conditional SHA-1 agility).
  • Extends MachOFile and FatFile with #codesign!, updating LC_CODE_SIGNATURE, resizing __LINKEDIT, and rebuilding fat slice offsets/sizes as needed.
  • Adds regression tests and documentation for ad-hoc signing behavior and compatibility boundaries.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/test_code_signing.rb Adds regression coverage for unsigned/signed, fat/thin, metadata preservation, and failure-safety behaviors.
README.md Documents the new pure-Ruby ad-hoc signing behavior, constraints, and scope.
lib/macho/macho_file.rb Adds MachOFile#codesign! and improves low_fileoff handling for segments without sections.
lib/macho/load_commands.rb Makes LC_CODE_SIGNATURE modifiable; adds serialization for LinkeditDataCommand and parsing helper for embedded signatures.
lib/macho/fat_file.rb Adds FatFile#codesign! and a rebuild path for resized slices with updated offsets/sizes.
lib/macho/code_signing.rb Implements code-signing structures (Blob/SuperBlob/CodeDirectory) and the ad-hoc signer logic.
lib/macho.rb Switches MachO.codesign! to use the in-library implementation and normalizes error handling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/macho/code_signing.rb
- remove the `/usr/bin/codesign` dependency from `MachO.codesign!` so
  Homebrew can repair bottle signatures on Apple Silicon using Ruby
- parse and build XNU-compatible `SuperBlob` and `CodeDirectory` data
  so replacement signatures can retain security-relevant metadata
- add or replace `LC_CODE_SIGNATURE`, resize `__LINKEDIT` and rebuild
  fat offsets so every page hash describes the final on-disk layout
- use SHA-256 with legacy SHA-1 agility only for old deployment targets
  because XNU accepts the open `ld64` ad-hoc format without an identity
- preserve non-linker requirements, entitlements, flags and runtime
  metadata while deliberately replacing linker-generated metadata
- reject malformed `CodeDirectory` hash arrays before exposing offsets
  and validate before in-place writes to retain hard links and leave
  failed inputs unchanged
- cover thin, fat, byte-order, metadata and macOS verification paths
  with regression tests, and document the compatibility boundaries
@MikeMcQuaid MikeMcQuaid force-pushed the pure-ruby-code-signing branch from f1bfb7e to d6a9829 Compare July 10, 2026 16:25
@MikeMcQuaid MikeMcQuaid marked this pull request as draft July 10, 2026 18:19
@MikeMcQuaid MikeMcQuaid marked this pull request as ready for review July 10, 2026 18:33
@MikeMcQuaid

Copy link
Copy Markdown
Member Author

Working as expected from Homebrew/brew#23047

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.

Pure-Ruby Code Directory/Code signing parsing and manipulation

2 participants