Skip to content

Preserve entitlements in MacOS signer #115800

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

Draft
wants to merge 40 commits into
base: main
Choose a base branch
from

Conversation

jtschuster
Copy link
Member

@jtschuster jtschuster commented May 20, 2025

Preserve entitlements in the manages Mach-O signer for both regular apphost and singlefilehost.

Adds entitlements to singlefilehost during the build.

Refactors the signature blob types to be classes that implement IBlob. The various named blobs derive from SimpleBlob or SuperBlob (except for CodeDirectoryBlob which is more complex). SimpleBlob and SuperBlob provide methods for accessing info about the blobs and writing to a file. IMachFile is used for writing to allow reading and writing from/to a Stream or MemoryMappedViewAccessor.

The CodeSignature class was replaced with just the EmbeddedSignatureBlob, a SuperBlob containing other relevant signature blobs. Methods from the CodeSignature class were move to the relevant blob class.

HostWriter.SetAsBundle was moved to the Bundle class to avoid having to remap the memory mapped file multiple times. Since the host destination stream is never closed between creating the bundle and SetAsBundle / Signing the bundle, we don't need to create a new copy with a new inode to sign and replace the existing bundle.

cc @dotnet/appmodel

- Write out the updated symtab command when writing Mach-O file
- Pass macosCodesign through to test CreateAppHost methods
- Remove redundant `codesign` checks
- Warn when bundler is told to sign the bundle for a non-macos target
- Allow macosCodesign to be true for non-mac bundles
- Allow some padding between string table and signature
- Use the same memory-mapped file instance for placeholder replacement
  and signing
- formatting changes
@github-actions github-actions bot added the area-HostModel Microsoft.NET.HostModel issues label May 20, 2025
Copy link
Contributor

Tagging subscribers to this area: @vitek-karas, @agocke
See info in area-owners.md if you want to be subscribed.

@jtschuster jtschuster requested a review from Copilot May 20, 2025 20:09
- Delete CodeSignature class and move methods on relevant blobs instead.
- Use EmbeddedSignatureBlob as the "signature" for the MachO file.
- Save old EmbeddedSignatureBlob in Bundling to preserve the entitlements.
- Add test to verify that the entitlements are preserved in the bundle.
@jtschuster jtschuster linked an issue Jun 3, 2025 that may be closed by this pull request
@jtschuster jtschuster requested a review from Copilot June 3, 2025 20:37
Copy link
Contributor

@Copilot Copilot AI left a comment

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 refactors the Mach-O signing code to use class-based blob types, preserves entitlements (and DER entitlements) during signing, and streamlines how bundles and signatures are applied.

  • Introduce new BlobMagic entries for Entitlements and DerEntitlements.
  • Convert signature blob structs to an OO hierarchy (Blob, SimpleBlob, SuperBlob, etc.).
  • Update Bundler and HostWriter to preserve existing entitlements and avoid remapping files unnecessarily.

Reviewed Changes

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

File Description
src/installer/managed/Microsoft.NET.HostModel/MachO/Enums/BlobMagic.cs Add Entitlements and DerEntitlements magic constants.
src/installer/managed/Microsoft.NET.HostModel/MachO/BinaryFormat/LinkEditCommand.cs Rename struct to LinkEditLoadCommand.
src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs Refactor bundle creation to use streams and memory‐mapped file; preserve entitlements.
src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs Consolidate signature handling and update method visibility.
Comments suppressed due to low confidence (3)

src/installer/managed/Microsoft.NET.HostModel/MachO/BinaryFormat/LinkEditCommand.cs:15

  • The file name 'LinkEditCommand.cs' no longer matches the updated struct name 'LinkEditLoadCommand'; consider renaming the file to 'LinkEditLoadCommand.cs' for consistency.
internal struct LinkEditLoadCommand

src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs:281

  • The 'relativePathToSpec' dictionary is declared but never used; consider removing this variable to clean up unused code.
var relativePathToSpec = new Dictionary<string, FileSpec>(StringComparer.Ordinal);

src/installer/managed/Microsoft.NET.HostModel/MachO/BinaryFormat/Blobs/EmbeddedSignatureBlob.cs:170

  • This new method for calculating signature size and preserving entitlements could benefit from dedicated unit tests to validate size calculations across scenarios, including with and without existing entitlements.
internal static unsafe long GetSignatureSize(uint fileSize, string identifier, EmbeddedSignatureBlob? existingSignature, byte? hashSize = null)

- Don't use multiple levels of inheritance for blobs
- Add IMachOFileReader/Writer to abstract Stream and MemoryMappedViewAccessor based files
- Use IMachOFileReader/Writer as the interface for reading/writing to the disk for MachO files and blobs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-HostModel Microsoft.NET.HostModel issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Codesigning the apphost on macOS should preserve entitlements
3 participants