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

Add managed MachO signing #108992

Merged
merged 35 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8901e5e
Add managed MachO signing
jtschuster Oct 17, 2024
10fc3b3
Add managed MachO signing
jtschuster Oct 17, 2024
f2f8402
Merge branch 'MachSigning' of https://github.com/jtschuster/runtime i…
jtschuster Oct 17, 2024
65a5a46
PR feedback, use different memory map file workaround
jtschuster Oct 18, 2024
c4425e9
Set enableMacOSCodesign=true in singlefile test
jtschuster Oct 19, 2024
622b32c
Make NameBuffer constants more readable
jtschuster Oct 21, 2024
b69eafd
Update src/installer/managed/Microsoft.NET.HostModel/MachO/MachMagicE…
jtschuster Oct 21, 2024
ed70909
Update src/installer/managed/Microsoft.NET.HostModel/MachO/MachMagicE…
jtschuster Oct 21, 2024
72e2ccf
PR Feedback:
jtschuster Oct 22, 2024
f92c9fa
Handle folders in the test data package
jtschuster Oct 22, 2024
6784db8
PR feedback
jtschuster Oct 24, 2024
4be00c2
Merge branch 'main' of https://github.com/dotnet/runtime into MachSig…
jtschuster Oct 24, 2024
9c6b1bc
Fix test issues
jtschuster Oct 25, 2024
5d8fe8e
Merge branch 'main' of https://github.com/dotnet/runtime into MachSig…
jtschuster Oct 25, 2024
3a2351b
Move BinaryFormat files and Enums to separate folders and add notes a…
jtschuster Oct 28, 2024
5eafdcb
Extend the AppHost template on disk before modifying and signing. Rem…
jtschuster Oct 28, 2024
f58574e
Use new MachO types in MachOUtils.cs
jtschuster Oct 28, 2024
b551128
Fix comments, use explicit types, update darc dependencies
jtschuster Oct 28, 2024
58f5144
Use unsafe and sizeof instead of Marshal.SizeOf, fix format nits
jtschuster Oct 29, 2024
4b77556
Rename signatureLC to signatureLoadCommand, remove unnecessary methods
jtschuster Oct 29, 2024
4a1f133
Remove extra 'unsafe' blocks
jtschuster Oct 29, 2024
78605a0
Revert arguments string change
jtschuster Oct 29, 2024
272f0e5
Re-add ResourceUpdater to HostWriter
jtschuster Oct 29, 2024
4a46cb9
Move CodeSignature to a nested type within MachObjectFile, move signa…
jtschuster Oct 29, 2024
e81000a
Merge branch 'main' of https://github.com/dotnet/runtime into MachSig…
jtschuster Oct 29, 2024
3cdc7ce
Set file length after memory mapped file is disposed
jtschuster Oct 29, 2024
a18cb1b
Close memory mapped file in test signing method.
jtschuster Oct 30, 2024
5320d1f
PR Feedback
jtschuster Oct 31, 2024
71cadf4
Merge branch 'main' of https://github.com/dotnet/runtime into MachSig…
jtschuster Oct 31, 2024
2a36163
Revert local testing change that enabled codesigning always.
jtschuster Oct 31, 2024
980c755
Revert extra change in TestApp.cs
jtschuster Oct 31, 2024
2cf8e41
Remove license from ThirdPartyNotices
jtschuster Oct 31, 2024
a8956b3
Add more definitions links, fix test nits.
jtschuster Nov 11, 2024
0c7e528
Merge branch 'main' of https://github.com/dotnet/runtime into MachSig…
jtschuster Nov 11, 2024
753c73c
Add comment explaining why the file must be written to create signatu…
jtschuster Nov 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -474,5 +474,9 @@
<Uri>https://github.com/dotnet/node</Uri>
<Sha>78c56619da525bd37de4c2828092762fb4fa03c4</Sha>
</Dependency>
<Dependency Name="Microsoft.NET.HostModel.TestData" Version="10.0.0-beta.24522.1">
<Uri>https://github.com/dotnet/runtime-assets</Uri>
<Sha>24f902e6d5bfe3fec9f07d55efe44794aec614a1</Sha>
</Dependency>
</ToolsetDependencies>
</Dependencies>
1 change: 1 addition & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<SystemTextRegularExpressionsTestDataVersion>10.0.0-beta.24530.1</SystemTextRegularExpressionsTestDataVersion>
<SystemWindowsExtensionsTestDataVersion>10.0.0-beta.24530.1</SystemWindowsExtensionsTestDataVersion>
<MicrosoftDotNetCilStripSourcesVersion>10.0.0-beta.24530.1</MicrosoftDotNetCilStripSourcesVersion>
<MicrosoftNETHostModelTestDataVersion>10.0.0-beta.24522.1</MicrosoftNETHostModelTestDataVersion>
jtschuster marked this conversation as resolved.
Show resolved Hide resolved
<!-- dotnet-optimization dependencies -->
<optimizationwindows_ntx64MIBCRuntimeVersion>1.0.0-prerelease.24462.2</optimizationwindows_ntx64MIBCRuntimeVersion>
<optimizationwindows_ntx86MIBCRuntimeVersion>1.0.0-prerelease.24462.2</optimizationwindows_ntx86MIBCRuntimeVersion>
Expand Down
85 changes: 42 additions & 43 deletions src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.NET.HostModel.MachO;

namespace Microsoft.NET.HostModel.AppHost
{
Expand Down Expand Up @@ -60,7 +61,7 @@ public enum SearchLocation : byte
/// <param name="appBinaryFilePath">Full path to app binary or relative path to the result apphost file</param>
/// <param name="windowsGraphicalUserInterface">Specify whether to set the subsystem to GUI. Only valid for PE apphosts.</param>
/// <param name="assemblyToCopyResourcesFrom">Path to the intermediate assembly, used for copying resources to PE apphosts.</param>
/// <param name="enableMacOSCodeSign">Sign the app binary using codesign with an anonymous certificate.</param>
/// <param name="enableMacOSCodeSign">Sign the app binary with an anonymous certificate. Only use when the AppHost is a Mach-O file built for MacOS.</param>
/// <param name="disableCetCompat">Remove CET Shadow Stack compatibility flag if set</param>
/// <param name="dotNetSearchOptions">Options for how the created apphost should look for the .NET install</param>
public static void CreateAppHost(
Expand Down Expand Up @@ -118,48 +119,55 @@ void RewriteAppHost(MemoryMappedFile mappedFile, MemoryMappedViewAccessor access
{
RetryUtil.RetryOnIOError(() =>
{
FileStream appHostSourceStream = null;
MemoryMappedFile memoryMappedFile = null;
MemoryMappedViewAccessor memoryMappedViewAccessor = null;
try
bool isMachOImage;
using (FileStream appHostDestinationStream = new FileStream(appHostDestinationFilePath, FileMode.Create, FileAccess.ReadWrite))
{
// Open the source host file.
appHostSourceStream = new FileStream(appHostSourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1);
memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostSourceStream, null, 0, MemoryMappedFileAccess.Read, HandleInheritability.None, true);
memoryMappedViewAccessor = memoryMappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.CopyOnWrite);

using (FileStream appHostSourceStream = new(appHostSourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1))
{
isMachOImage = MachObjectFile.IsMachOImage(appHostSourceStream);
if (!isMachOImage && enableMacOSCodeSign)
{
throw new InvalidDataException("Cannot sign a non-Mach-O file.");
}
appHostSourceStream.CopyTo(appHostDestinationStream);
}
// Get the size of the source app host to ensure that we don't write extra data to the destination.
// On Windows, the size of the view accessor is rounded up to the next page boundary.
jtschuster marked this conversation as resolved.
Show resolved Hide resolved
long sourceAppHostLength = appHostSourceStream.Length;

// Transform the host file in-memory.
RewriteAppHost(memoryMappedFile, memoryMappedViewAccessor);

// Save the transformed host.
using (FileStream fileStream = new FileStream(appHostDestinationFilePath, FileMode.Create))
long appHostLength = appHostDestinationStream.Length;
string destinationFileName = Path.GetFileName(appHostDestinationFilePath);
// On Mac, we need to extend the file size to accommodate the signature.
long appHostTmpCapacity = enableMacOSCodeSign ?
appHostLength + MachObjectFile.GetSignatureSizeEstimate((uint)appHostLength, destinationFileName)
: appHostLength;

using (MemoryMappedFile memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostDestinationStream, null, appHostTmpCapacity, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true))
using (MemoryMappedViewAccessor memoryMappedViewAccessor = memoryMappedFile.CreateViewAccessor(0, appHostTmpCapacity, MemoryMappedFileAccess.ReadWrite))
{
BinaryUtils.WriteToStream(memoryMappedViewAccessor, fileStream, sourceAppHostLength);

// Remove the signature from MachO hosts.
if (!appHostIsPEImage)
// Transform the host file in-memory.
RewriteAppHost(memoryMappedFile, memoryMappedViewAccessor);
if (isMachOImage)
{
MachOUtils.RemoveSignature(fileStream);
if (enableMacOSCodeSign)
{
string fileName = Path.GetFileName(appHostDestinationFilePath);
MachObjectFile machObjectFile = MachObjectFile.Create(memoryMappedViewAccessor);
appHostLength = machObjectFile.CreateAdHocSignature(memoryMappedViewAccessor, fileName);
}
else if (MachObjectFile.TryRemoveCodesign(memoryMappedViewAccessor, out long? length))
{
appHostLength = length.Value;
}
}
}
appHostDestinationStream.SetLength(appHostLength);

if (assemblyToCopyResourcesFrom != null && appHostIsPEImage)
{
using var updater = new ResourceUpdater(fileStream, true);
updater.AddResourcesFromPEImage(assemblyToCopyResourcesFrom);
updater.Update();
jtschuster marked this conversation as resolved.
Show resolved Hide resolved
}
if (assemblyToCopyResourcesFrom != null && appHostIsPEImage)
{
using var updater = new ResourceUpdater(appHostDestinationStream, true);
updater.AddResourcesFromPEImage(assemblyToCopyResourcesFrom);
updater.Update();
}
}
finally
{
memoryMappedViewAccessor?.Dispose();
memoryMappedFile?.Dispose();
appHostSourceStream?.Dispose();
}
});

if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand All @@ -178,15 +186,6 @@ void RewriteAppHost(MemoryMappedFile mappedFile, MemoryMappedViewAccessor access
{
throw new Win32Exception(Marshal.GetLastWin32Error(), $"Could not set file permission {Convert.ToString(filePermissionOctal, 8)} for {appHostDestinationFilePath}.");
}

if (enableMacOSCodeSign && RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && HostModelUtils.IsCodesignAvailable())
{
(int exitCode, string stdErr) = HostModelUtils.RunCodesign("-s -", appHostDestinationFilePath);
if (exitCode != 0)
{
throw new AppHostSigningException(exitCode, stdErr);
}
}
}
}
catch (Exception ex)
Expand Down
Loading
Loading