-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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 the managed signer to remove the code signature from singlefile bundles #110063
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,13 @@ | |
using System.Diagnostics; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.IO.MemoryMappedFiles; | ||
using System.Linq; | ||
using System.Reflection.PortableExecutable; | ||
using System.Runtime.InteropServices; | ||
using Microsoft.DotNet.CoreSetup; | ||
using Microsoft.NET.HostModel.AppHost; | ||
using Microsoft.NET.HostModel.MachO; | ||
|
||
namespace Microsoft.NET.HostModel.Bundle | ||
{ | ||
|
@@ -92,7 +94,7 @@ private bool ShouldCompress(FileType type) | |
/// startOffset: offset of the start 'file' within 'bundle' | ||
/// compressedSize: size of the compressed data, if entry was compressed, otherwise 0 | ||
/// </returns> | ||
private (long startOffset, long compressedSize) AddToBundle(Stream bundle, FileStream file, FileType type) | ||
private (long startOffset, long compressedSize) AddToBundle(FileStream bundle, FileStream file, FileType type) | ||
{ | ||
long startOffset = bundle.Position; | ||
if (ShouldCompress(type)) | ||
|
@@ -273,20 +275,23 @@ public string GenerateBundle(IReadOnlyList<FileSpec> fileSpecs) | |
|
||
BinaryUtils.CopyFile(hostSource, bundlePath); | ||
|
||
if (_target.IsOSX && RuntimeInformation.IsOSPlatform(OSPlatform.OSX) && Codesign.IsAvailable) | ||
{ | ||
Codesign.Run("--remove-signature", bundlePath); | ||
} | ||
|
||
// Note: We're comparing file paths both on the OS we're running on as well as on the target OS for the app | ||
// We can't really make assumptions about the file systems (even on Linux there can be case insensitive file systems | ||
// and vice versa for Windows). So it's safer to do case sensitive comparison everywhere. | ||
var relativePathToSpec = new Dictionary<string, FileSpec>(StringComparer.Ordinal); | ||
|
||
long headerOffset = 0; | ||
using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(bundlePath))) | ||
using (FileStream bundle = File.Open(bundlePath, FileMode.Open, FileAccess.ReadWrite)) | ||
using (BinaryWriter writer = new BinaryWriter(bundle)) | ||
{ | ||
Stream bundle = writer.BaseStream; | ||
long? newLength; | ||
using (MemoryMappedFile mmap = MemoryMappedFile.CreateFromFile(bundle, null, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true)) | ||
using (MemoryMappedViewAccessor accessor = mmap.CreateViewAccessor(0, 0, MemoryMappedFileAccess.ReadWrite)) | ||
{ | ||
MachObjectFile.TryRemoveCodesign(accessor, out newLength); | ||
} | ||
newLength ??= new FileInfo(bundlePath).Length; | ||
bundle.SetLength(newLength.Value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updating the length should only be needed if |
||
bundle.Position = bundle.Length; | ||
|
||
foreach (var fileSpec in fileSpecs) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,11 @@ | |
|
||
using FluentAssertions; | ||
using Microsoft.DotNet.Cli.Build.Framework; | ||
using Microsoft.DotNet.CoreSetup; | ||
using Microsoft.DotNet.CoreSetup.Test; | ||
using Microsoft.NET.HostModel.AppHost.Tests; | ||
using Microsoft.NET.HostModel.MachO; | ||
using Microsoft.NET.HostModel.MachO.CodeSign.Tests; | ||
using Xunit; | ||
|
||
namespace Microsoft.NET.HostModel.Bundle.Tests | ||
|
@@ -313,13 +317,12 @@ public void AssemblyAlignment() | |
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
[PlatformSpecific(TestPlatforms.OSX)] | ||
public void Codesign(bool shouldCodesign) | ||
public void MacOSBundleIsCodeSigned(bool shouldCodesign) | ||
{ | ||
TestApp app = sharedTestState.App; | ||
FileSpec[] fileSpecs = new FileSpec[] | ||
{ | ||
new FileSpec(Binaries.AppHost.FilePath, BundlerHostName), | ||
new FileSpec(CreateAppHost.PrepareMockMachAppHostFile(app.Location, singleFile: true), BundlerHostName), | ||
new FileSpec(app.AppDll, Path.GetRelativePath(app.Location, app.AppDll)), | ||
new FileSpec(app.DepsJson, Path.GetRelativePath(app.Location, app.DepsJson)), | ||
new FileSpec(app.RuntimeConfigJson, Path.GetRelativePath(app.Location, app.RuntimeConfigJson)), | ||
|
@@ -328,19 +331,23 @@ public void Codesign(bool shouldCodesign) | |
Bundler bundler = CreateBundlerInstance(macosCodesign: shouldCodesign); | ||
string bundledApp = bundler.GenerateBundle(fileSpecs); | ||
|
||
// Check if the file is signed | ||
CommandResult result = Command.Create("codesign", $"-v {bundledApp}") | ||
.CaptureStdErr() | ||
.CaptureStdOut() | ||
.Execute(expectedToFail: !shouldCodesign); | ||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
SigningTests.IsSigned(bundledApp).Should().BeFalse(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be based on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, this is because the signing still goes through |
||
return; | ||
} | ||
|
||
// Check if the file is signed | ||
var result = Codesign.Run("-v", bundledApp); | ||
if (shouldCodesign) | ||
{ | ||
result.Should().Pass(); | ||
result.ExitCode.Should().Be(0); | ||
} | ||
else | ||
{ | ||
result.Should().Fail(); | ||
result.ExitCode.Should().NotBe(0); | ||
// Ensure we can sign it again | ||
Codesign.Run("-s -", bundledApp).ExitCode.Should().Be(0); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BinaryWriter
will close the stream on dispose. Now that we're disposing of the stream ourselves, I expect we want to use the overload that allows specifying to leave it open.