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

Use managed signer for bundles #110417

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

jtschuster
Copy link
Member

@jtschuster jtschuster commented Dec 4, 2024

Updates the single-file bundler to use the managed Mach-O signer. Adds the SymbolTable load command to the MachObjectFile class and adds a method to extend the symbol table to include the bundled data. Adds validation when creating a MachObjectFile from a file on disk.

Now that we don't use codesign for signing, it looks like the executables are sometimes getting killed by antivirus when tests try to run them immediately after they are modified, leading to flaky tests (locally at least). I'm looking into either retrying or running codesign -v before running the apps on Mac.

Fixes #110055

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-HostModel Microsoft.NET.HostModel issues label Dec 4, 2024
@jtschuster jtschuster added the NO-REVIEW Experimental/testing PR, do NOT review it label Dec 4, 2024
- 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
@jtschuster jtschuster removed the NO-REVIEW Experimental/testing PR, do NOT review it label Dec 9, 2024
Copy link

@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.

Copilot reviewed 5 out of 12 changed files in this pull request and generated 2 suggestions.

Files not reviewed (7)
  • src/installer/managed/Microsoft.NET.HostModel/AppHost/MachOUtils.cs: Evaluated as low risk
  • src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs: Evaluated as low risk
  • src/installer/tests/TestUtils/TestApp.cs: Evaluated as low risk
  • src/installer/managed/Microsoft.NET.HostModel/MachO/MachOFormatError.cs: Evaluated as low risk
  • src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost/CreateAppHost.cs: Evaluated as low risk
  • src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs: Evaluated as low risk
  • src/installer/tests/TestUtils/SingleFileTestApp.cs: Evaluated as low risk
Comments skipped due to low confidence (2)

src/installer/managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs:22

  • There is an extra semicolon at the end of the line.
uint csSize = CodeSignature.GetCodeSignatureSize(GetSignatureStart(), identifier);;

src/installer/tests/Microsoft.NET.HostModel.Tests/MachObjectSigning/SigningTests.cs:33

  • The 'MemoryMappedFileAccess.CopyOnWrite' might not be necessary for a read-only check. Consider using 'MemoryMappedFileAccess.Read' instead.
using (var managedSignedAccessor = memoryMappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.CopyOnWrite))

@jtschuster jtschuster marked this pull request as ready for review December 9, 2024 17:08
@jtschuster jtschuster changed the title CI test: Use managed signer for bundles Use managed signer for bundles Dec 10, 2024
Copy link
Member

@elinor-fung elinor-fung left a comment

Choose a reason for hiding this comment

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

Now that we don't use codesign for signing, it looks like the executables are sometimes getting killed by antivirus when tests try to run them immediately after they are modified, leading to flaky tests (locally at least). I'm looking into either retrying or running codesign -v before running the apps on Mac.

What was the end result here? Could a product scenario hit the same thing with running immediately after publish with signing?

Comment on lines 241 to 242
long mmapFileSize = macosCodesign ?
bundleSize + MachObjectFile.GetSignatureSizeEstimate((uint)bundleSize, Path.GetFileName(appHostPath))
Copy link
Member

Choose a reason for hiding this comment

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

nit: I find this alignment easier to read

Suggested change
long mmapFileSize = macosCodesign ?
bundleSize + MachObjectFile.GetSignatureSizeEstimate((uint)bundleSize, Path.GetFileName(appHostPath))
long mmapFileSize = macosCodesign
? bundleSize + MachObjectFile.GetSignatureSizeEstimate((uint)bundleSize, Path.GetFileName(appHostPath))

- Use the same memory-mapped file instance for placeholder replacement
  and signing
- formatting changes
@jtschuster
Copy link
Member Author

What was the end result here?

It looks like the issue is probably related to modifying the exe in place: https://developer.apple.com/documentation/security/updating-mac-software#Update-Files-that-Include-Signed-Code

I'll update the code to use CopyOnWrite memory-mapped files, write a new file, then replace the old one.

@elinor-fung
Copy link
Member

What was the end result here?

It looks like the issue is probably related to modifying the exe in place: https://developer.apple.com/documentation/security/updating-mac-software#Update-Files-that-Include-Signed-Code

Ah, does it cache once the file is created? I had thought it would only cache when it is first run.

I'll update the code to use CopyOnWrite memory-mapped files, write a new file, then replace the old one.

I guess doing the inode check with codesign shows it replaces with an entirely new file? Matching that seems good.

@jtschuster
Copy link
Member Author

What was the end result here?

It looks like the issue is probably related to modifying the exe in place: https://developer.apple.com/documentation/security/updating-mac-software#Update-Files-that-Include-Signed-Code

Ah, does it cache once the file is created? I had thought it would only cache when it is first run.

I'll update the code to use CopyOnWrite memory-mapped files, write a new file, then replace the old one.

I guess doing the inode check with codesign shows it replaces with an entirely new file? Matching that seems good.

It was still broken after changing the behavior to create a new inode. I had forgotten to update the VMSize field of the __LINKEDIT segment when creating the signature. The file size was larger than the VMSize, and that was causing sporadic crashes, but I only noticed it in the larger bundles with larger signatures. I left in the "copy to a temp file, edit, then replace" behavior anyway to make sure we match codesign's behavior.

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.

SingleFile Bundler should use the managed Mach-O signer
2 participants