From 5d949a15afaa4a65730d21a5e935265f831fc23c Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Thu, 21 Nov 2024 14:39:46 -0500 Subject: [PATCH 1/6] Use the managed signer to remove the code signature from singlefile bundles --- .../Microsoft.NET.HostModel/Bundle/Bundler.cs | 21 +++++++++------ .../AppHost/CreateAppHost.cs | 17 +++++++++--- .../Bundle/BundlerConsistencyTests.cs | 26 ++++++++++++------- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs b/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs index a548a5358e28b..1e7f9c22403a4 100644 --- a/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs +++ b/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs @@ -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 /// - 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 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(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); bundle.Position = bundle.Length; foreach (var fileSpec in fileSpecs) diff --git a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost/CreateAppHost.cs b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost/CreateAppHost.cs index fd3934d6e1352..099813230c78a 100644 --- a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost/CreateAppHost.cs +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost/CreateAppHost.cs @@ -485,12 +485,23 @@ private void ResourceWithUnknownLanguage() } } - private static readonly byte[] s_placeholderData = AppBinaryPathPlaceholderSearchValue.Concat(DotNetSearchPlaceholderValue).ToArray(); + private static readonly byte[] s_apphostPlaceholderData = AppBinaryPathPlaceholderSearchValue.Concat(DotNetSearchPlaceholderValue).ToArray(); + private static readonly byte[] s_singleFileApphostPlaceholderData = { + // 8 bytes represent the bundle header-offset + // Zero for non-bundle apphosts (default). + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + // 32 bytes represent the bundle signature: SHA-256 for ".net core bundle" + 0x8b, 0x12, 0x02, 0xb9, 0x6a, 0x61, 0x20, 0x38, + 0x72, 0x7b, 0x93, 0x02, 0x14, 0xd7, 0xa0, 0x32, + 0x13, 0xf5, 0xb9, 0xe6, 0xef, 0xae, 0x33, 0x18, + 0xee, 0x3b, 0x2d, 0xce, 0x24, 0xb3, 0x6a, 0xae + }; + /// /// Prepares a mock executable file with the AppHost placeholder embedded in it. /// This file will not run, but can be used to test HostWriter and signing process. /// - public static string PrepareMockMachAppHostFile(string directory) + public static string PrepareMockMachAppHostFile(string directory, bool singleFile = false) { string fileName = "MockAppHost.mach.o"; string outputFilePath = Path.Combine(directory, fileName); @@ -501,7 +512,7 @@ public static string PrepareMockMachAppHostFile(string directory) // Add the placeholder - it just needs to exist somewhere in the image // We'll put it at 4096 bytes into the file - this should be in the middle of the __TEXT segment managedSignFile.Position = 4096; - managedSignFile.Write(s_placeholderData); + managedSignFile.Write(singleFile ? s_singleFileApphostPlaceholderData : s_apphostPlaceholderData); } return outputFilePath; } diff --git a/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs b/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs index 0087effaa8cb2..5e7480c6727c4 100644 --- a/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs @@ -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,22 @@ 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(); + } + // 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); } } From 1ee174d8d700bd02267401615c365aa046bfa907 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Fri, 22 Nov 2024 09:56:19 -0500 Subject: [PATCH 2/6] Return before calling codesign on non-Mac hosts --- .../Bundle/BundlerConsistencyTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs b/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs index 5e7480c6727c4..de7ca8e04cfa5 100644 --- a/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs @@ -334,6 +334,7 @@ public void MacOSBundleIsCodeSigned(bool shouldCodesign) if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { SigningTests.IsSigned(bundledApp).Should().BeFalse(); + return; } // Check if the file is signed From 3f85250defa0fcea6ec05bc57b47695da7a40c98 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:47:14 -0500 Subject: [PATCH 3/6] PR Feedback: - Add comment why single file hosts aren't signed on non-mac yet - Set length of file only if the signature is removed --- .../managed/Microsoft.NET.HostModel/Bundle/Bundler.cs | 11 ++++++----- .../Bundle/BundlerConsistencyTests.cs | 1 + 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs b/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs index 1e7f9c22403a4..66bd6659219f0 100644 --- a/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs +++ b/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs @@ -10,6 +10,7 @@ using System.Linq; using System.Reflection.PortableExecutable; using System.Runtime.InteropServices; +using System.Text; using Microsoft.DotNet.CoreSetup; using Microsoft.NET.HostModel.AppHost; using Microsoft.NET.HostModel.MachO; @@ -282,18 +283,18 @@ public string GenerateBundle(IReadOnlyList fileSpecs) long headerOffset = 0; using (FileStream bundle = File.Open(bundlePath, FileMode.Open, FileAccess.ReadWrite)) - using (BinaryWriter writer = new BinaryWriter(bundle)) + using (BinaryWriter writer = new BinaryWriter(bundle, Encoding.Default, leaveOpen: true)) { 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); + if(MachObjectFile.TryRemoveCodesign(accessor, out newLength)) + { + bundle.SetLength(newLength.Value); + } } - newLength ??= new FileInfo(bundlePath).Length; - bundle.SetLength(newLength.Value); bundle.Position = bundle.Length; - foreach (var fileSpec in fileSpecs) { string relativePath = fileSpec.BundleRelativePath; diff --git a/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs b/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs index de7ca8e04cfa5..d1efdb9227aab 100644 --- a/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs @@ -333,6 +333,7 @@ public void MacOSBundleIsCodeSigned(bool shouldCodesign) if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { + // SingleFile is still only signed on MacOS with codesign SigningTests.IsSigned(bundledApp).Should().BeFalse(); return; } From f8ecd43fbebccfcc1901071b338239110f1f84ea Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Mon, 25 Nov 2024 13:24:58 -0500 Subject: [PATCH 4/6] Resize after memory mapped file is disposed --- .../Microsoft.NET.HostModel/Bundle/Bundler.cs | 9 ++------ .../MachO/MachObjectFile.cs | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs b/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs index 66bd6659219f0..4c616ef1c765a 100644 --- a/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs +++ b/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs @@ -285,14 +285,9 @@ public string GenerateBundle(IReadOnlyList fileSpecs) using (FileStream bundle = File.Open(bundlePath, FileMode.Open, FileAccess.ReadWrite)) using (BinaryWriter writer = new BinaryWriter(bundle, Encoding.Default, leaveOpen: true)) { - long? newLength; - using (MemoryMappedFile mmap = MemoryMappedFile.CreateFromFile(bundle, null, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true)) - using (MemoryMappedViewAccessor accessor = mmap.CreateViewAccessor(0, 0, MemoryMappedFileAccess.ReadWrite)) + if (_target.IsOSX) { - if(MachObjectFile.TryRemoveCodesign(accessor, out newLength)) - { - bundle.SetLength(newLength.Value); - } + MachObjectFile.TryRemoveCodesign(bundle); } bundle.Position = bundle.Length; foreach (var fileSpec in fileSpecs) diff --git a/src/installer/managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs b/src/installer/managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs index 99a822c0c7546..e50ea11d77a5b 100644 --- a/src/installer/managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs +++ b/src/installer/managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs @@ -166,6 +166,27 @@ public static bool TryRemoveCodesign(MemoryMappedViewAccessor memoryMappedViewAc return true; } + /// + /// Removes the code signature load command and signature, and resizes the file if necessary. + /// Returns true if the signature was removed, false otherwise. + /// + public static bool TryRemoveCodesign(FileStream bundle) + { + long? newLength; + bool resized; + // Windows doesn't allow a FileStream to be resized while the file is memory mapped, so we must dispose of the memory mapped file first. + using (MemoryMappedFile mmap = MemoryMappedFile.CreateFromFile(bundle, null, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true)) + using (MemoryMappedViewAccessor accessor = mmap.CreateViewAccessor(0, 0, MemoryMappedFileAccess.ReadWrite)) + { + resized = TryRemoveCodesign(accessor, out newLength); + } + if (resized) + { + bundle.SetLength(newLength.Value); + } + return resized; + } + /// /// Returns true if the two signed MachObjectFiles are equivalent. /// Since the entire file isn't store in the object, the code signature is required. From 186dfd2538e54d0c112e14c62898b6514bfe0d61 Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Tue, 26 Nov 2024 09:44:20 -0500 Subject: [PATCH 5/6] Rename stream-based helper method --- .../managed/Microsoft.NET.HostModel/Bundle/Bundler.cs | 2 +- .../managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs b/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs index 4c616ef1c765a..ed6ee49775846 100644 --- a/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs +++ b/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs @@ -287,7 +287,7 @@ public string GenerateBundle(IReadOnlyList fileSpecs) { if (_target.IsOSX) { - MachObjectFile.TryRemoveCodesign(bundle); + MachObjectFile.RemoveCodesign(bundle); } bundle.Position = bundle.Length; foreach (var fileSpec in fileSpecs) diff --git a/src/installer/managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs b/src/installer/managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs index e50ea11d77a5b..9a049de28e70c 100644 --- a/src/installer/managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs +++ b/src/installer/managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs @@ -170,7 +170,7 @@ public static bool TryRemoveCodesign(MemoryMappedViewAccessor memoryMappedViewAc /// Removes the code signature load command and signature, and resizes the file if necessary. /// Returns true if the signature was removed, false otherwise. /// - public static bool TryRemoveCodesign(FileStream bundle) + public static void RemoveCodesign(FileStream bundle) { long? newLength; bool resized; @@ -184,7 +184,6 @@ public static bool TryRemoveCodesign(FileStream bundle) { bundle.SetLength(newLength.Value); } - return resized; } /// From 8fb1f722b460482e57e720c85d77a61f44b6325a Mon Sep 17 00:00:00 2001 From: Jackson Schuster <36744439+jtschuster@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:18:28 -0500 Subject: [PATCH 6/6] Rename TryRemoveCodesign, pass OSPlatform.OSX to bundler when testing MachO signing. --- .../managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs | 2 +- .../managed/Microsoft.NET.HostModel/Bundle/Bundler.cs | 2 +- .../Microsoft.NET.HostModel/MachO/MachObjectFile.cs | 7 +++---- .../Bundle/BundlerConsistencyTests.cs | 6 +++--- .../MachObjectSigning/SigningTests.cs | 8 +------- 5 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs b/src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs index 29dda4912d2fc..44e5f66795ee5 100644 --- a/src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs +++ b/src/installer/managed/Microsoft.NET.HostModel/AppHost/HostWriter.cs @@ -153,7 +153,7 @@ void RewriteAppHost(MemoryMappedFile mappedFile, MemoryMappedViewAccessor access MachObjectFile machObjectFile = MachObjectFile.Create(memoryMappedViewAccessor); appHostLength = machObjectFile.CreateAdHocSignature(memoryMappedViewAccessor, fileName); } - else if (MachObjectFile.TryRemoveCodesign(memoryMappedViewAccessor, out long? length)) + else if (MachObjectFile.RemoveCodeSignatureIfPresent(memoryMappedViewAccessor, out long? length)) { appHostLength = length.Value; } diff --git a/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs b/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs index ed6ee49775846..392a3bac51c60 100644 --- a/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs +++ b/src/installer/managed/Microsoft.NET.HostModel/Bundle/Bundler.cs @@ -287,7 +287,7 @@ public string GenerateBundle(IReadOnlyList fileSpecs) { if (_target.IsOSX) { - MachObjectFile.RemoveCodesign(bundle); + MachObjectFile.RemoveCodeSignatureIfPresent(bundle); } bundle.Position = bundle.Length; foreach (var fileSpec in fileSpecs) diff --git a/src/installer/managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs b/src/installer/managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs index 9a049de28e70c..51905550c9a33 100644 --- a/src/installer/managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs +++ b/src/installer/managed/Microsoft.NET.HostModel/MachO/MachObjectFile.cs @@ -143,7 +143,7 @@ public static bool IsMachOImage(string filePath) /// The file to remove the signature from. /// The new length of the file if the signature is remove and the method returns true /// True if a signature was present and removed, false otherwise - public static bool TryRemoveCodesign(MemoryMappedViewAccessor memoryMappedViewAccessor, out long? newLength) + public static bool RemoveCodeSignatureIfPresent(MemoryMappedViewAccessor memoryMappedViewAccessor, out long? newLength) { newLength = null; if (!IsMachOImage(memoryMappedViewAccessor)) @@ -168,9 +168,8 @@ public static bool TryRemoveCodesign(MemoryMappedViewAccessor memoryMappedViewAc /// /// Removes the code signature load command and signature, and resizes the file if necessary. - /// Returns true if the signature was removed, false otherwise. /// - public static void RemoveCodesign(FileStream bundle) + public static void RemoveCodeSignatureIfPresent(FileStream bundle) { long? newLength; bool resized; @@ -178,7 +177,7 @@ public static void RemoveCodesign(FileStream bundle) using (MemoryMappedFile mmap = MemoryMappedFile.CreateFromFile(bundle, null, 0, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true)) using (MemoryMappedViewAccessor accessor = mmap.CreateViewAccessor(0, 0, MemoryMappedFileAccess.ReadWrite)) { - resized = TryRemoveCodesign(accessor, out newLength); + resized = RemoveCodeSignatureIfPresent(accessor, out newLength); } if (resized) { diff --git a/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs b/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs index d1efdb9227aab..6dcb78c29cfa9 100644 --- a/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/Bundle/BundlerConsistencyTests.cs @@ -28,8 +28,8 @@ public BundlerConsistencyTests(SharedTestState fixture) } private static string BundlerHostName = Binaries.GetExeName(SharedTestState.AppName); - private Bundler CreateBundlerInstance(BundleOptions bundleOptions = BundleOptions.None, Version version = null, bool macosCodesign = true) - => new Bundler(BundlerHostName, sharedTestState.App.GetUniqueSubdirectory("bundle"), bundleOptions, targetFrameworkVersion: version, macosCodesign: macosCodesign); + private Bundler CreateBundlerInstance(BundleOptions bundleOptions = BundleOptions.None, Version version = null, bool macosCodesign = true, OSPlatform? targetOS = null) + => new Bundler(BundlerHostName, sharedTestState.App.GetUniqueSubdirectory("bundle"), bundleOptions, targetFrameworkVersion: version, macosCodesign: macosCodesign, targetOS: targetOS); [Fact] public void EnableCompression_Before60_Fails() @@ -328,7 +328,7 @@ public void MacOSBundleIsCodeSigned(bool shouldCodesign) new FileSpec(app.RuntimeConfigJson, Path.GetRelativePath(app.Location, app.RuntimeConfigJson)), }; - Bundler bundler = CreateBundlerInstance(macosCodesign: shouldCodesign); + Bundler bundler = CreateBundlerInstance(targetOS: OSPlatform.OSX, macosCodesign: shouldCodesign); string bundledApp = bundler.GenerateBundle(fileSpecs); if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) diff --git a/src/installer/tests/Microsoft.NET.HostModel.Tests/MachObjectSigning/SigningTests.cs b/src/installer/tests/Microsoft.NET.HostModel.Tests/MachObjectSigning/SigningTests.cs index 6926d5d9a8045..0516638d0fa13 100644 --- a/src/installer/tests/Microsoft.NET.HostModel.Tests/MachObjectSigning/SigningTests.cs +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/MachObjectSigning/SigningTests.cs @@ -228,13 +228,7 @@ internal static void RemoveSignature(string originalFilePath, string removedSign var destinationFileName = Path.GetFileName(removedSignaturePath); var appHostSignedLength = appHostLength + MachObjectFile.GetSignatureSizeEstimate((uint)appHostLength, destinationFileName); - using (MemoryMappedFile memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostDestinationStream, null, appHostSignedLength, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, true)) - using (MemoryMappedViewAccessor memoryMappedViewAccessor = memoryMappedFile.CreateViewAccessor(0, appHostSignedLength, MemoryMappedFileAccess.ReadWrite)) - { - if (MachObjectFile.TryRemoveCodesign(memoryMappedViewAccessor, out long? newLength)) - appHostLength = newLength.Value; - } - appHostDestinationStream.SetLength(appHostLength); + MachObjectFile.RemoveCodeSignatureIfPresent(appHostDestinationStream); } } }