forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostWriter.cs
More file actions
279 lines (244 loc) · 13.5 KB
/
Copy pathHostWriter.cs
File metadata and controls
279 lines (244 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.ComponentModel;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.NET.HostModel.MachO;
namespace Microsoft.NET.HostModel.AppHost
{
/// <summary>
/// Embeds the App Name into the AppHost.exe
/// If an apphost is a single-file bundle, updates the location of the bundle headers.
/// </summary>
public static partial class HostWriter
{
/// <summary>
/// hash value embedded in default apphost executable in a place where the path to the app binary should be stored.
/// </summary>
private const string AppBinaryPathPlaceholder = "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2";
private static readonly byte[] AppBinaryPathPlaceholderSearchValue = Encoding.UTF8.GetBytes(AppBinaryPathPlaceholder);
// See placeholder array in corehost.cpp
private const int MaxAppBinaryPathSizeInBytes = 1024;
/// <summary>
/// Value embedded in default apphost executable for configuration of how it will search for the .NET install
/// </summary>
private const string DotNetSearchPlaceholder = "\0\019ff3e9c3602ae8e841925bb461a0adb064a1f1903667a5e0d87e8f608f425ac";
private static readonly byte[] DotNetSearchPlaceholderSearchValue = Encoding.UTF8.GetBytes(DotNetSearchPlaceholder);
// See placeholder array in hostfxr_resolver.cpp
private const int MaxDotNetSearchSizeInBytes = 512;
private const int MaxAppRelativeDotNetSizeInBytes = MaxDotNetSearchSizeInBytes - 3; // -2 for search location + null, -1 for null terminator
public class DotNetSearchOptions
{
// Keep in sync with fxr_resolver::search_location in fxr_resolver.h
[Flags]
public enum SearchLocation : byte
{
Default,
AppLocal = 1 << 0,
AppRelative = 1 << 1,
EnvironmentVariable = 1 << 2,
Global = 1 << 3,
}
public SearchLocation Location { get; set; } = SearchLocation.Default;
public string AppRelativeDotNet { get; set; }
}
/// <summary>
/// Create an AppHost with embedded configuration of app binary location
/// </summary>
/// <param name="appHostSourceFilePath">The path of Apphost template, which has the place holder</param>
/// <param name="appHostDestinationFilePath">The destination path for desired location to place, including the file name</param>
/// <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 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(
string appHostSourceFilePath,
string appHostDestinationFilePath,
string appBinaryFilePath,
bool windowsGraphicalUserInterface = false,
string assemblyToCopyResourcesFrom = null,
bool enableMacOSCodeSign = false,
bool disableCetCompat = false,
DotNetSearchOptions dotNetSearchOptions = null)
{
byte[] appPathBytes = Encoding.UTF8.GetBytes(appBinaryFilePath);
if (appPathBytes.Length > MaxAppBinaryPathSizeInBytes)
{
throw new AppNameTooLongException(appBinaryFilePath, MaxAppBinaryPathSizeInBytes);
}
byte[] searchOptionsBytes = dotNetSearchOptions != null
? GetSearchOptionBytes(dotNetSearchOptions)
: null;
bool appHostIsPEImage = false;
void RewriteAppHost(MemoryMappedFile mappedFile, MemoryMappedViewAccessor accessor)
{
// Re-write the destination apphost with the proper contents.
BinaryUtils.SearchAndReplace(accessor, AppBinaryPathPlaceholderSearchValue, appPathBytes);
// Update the .NET search configuration
if (searchOptionsBytes != null)
{
BinaryUtils.SearchAndReplace(accessor, DotNetSearchPlaceholderSearchValue, searchOptionsBytes);
}
appHostIsPEImage = PEUtils.IsPEImage(accessor);
if (windowsGraphicalUserInterface)
{
if (!appHostIsPEImage)
{
throw new AppHostNotPEFileException("PE file signature not found.");
}
PEUtils.SetWindowsGraphicalUserInterfaceBit(accessor);
}
if (disableCetCompat && appHostIsPEImage)
{
PEUtils.RemoveCetCompatBit(mappedFile, accessor);
}
}
try
{
RetryUtil.RetryOnIOError(() =>
{
bool isMachOImage;
// MacOS requires a new inode to be created when updating a signed file, so we'll delete the file and create a new one.
if (File.Exists(appHostDestinationFilePath))
File.Delete(appHostDestinationFilePath);
using (FileStream appHostDestinationStream = new FileStream(appHostDestinationFilePath, FileMode.CreateNew, FileAccess.ReadWrite))
{
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.
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))
{
// Transform the host file in-memory.
RewriteAppHost(memoryMappedFile, memoryMappedViewAccessor);
if (isMachOImage)
{
MachObjectFile machObjectFile = MachObjectFile.Create(memoryMappedViewAccessor);
if (enableMacOSCodeSign)
{
appHostLength = machObjectFile.AdHocSignFile(memoryMappedViewAccessor, destinationFileName);
}
else if (machObjectFile.RemoveCodeSignatureIfPresent(memoryMappedViewAccessor, out long? length))
{
appHostLength = length.Value;
}
}
}
appHostDestinationStream.SetLength(appHostLength);
if (assemblyToCopyResourcesFrom != null && appHostIsPEImage)
{
using var updater = new ResourceUpdater(appHostDestinationStream, true);
updater.AddResourcesFromPEImage(assemblyToCopyResourcesFrom);
updater.Update();
}
}
});
Chmod755(appHostDestinationFilePath);
}
catch (Exception ex)
{
// Delete the destination file so we don't leave an unmodified apphost
try
{
File.Delete(appHostDestinationFilePath);
}
catch (Exception failedToDeleteEx)
{
throw new AggregateException(ex, failedToDeleteEx);
}
throw;
}
}
/// <summary>
/// Check if the an AppHost is a single-file bundle
/// </summary>
/// <param name="appHostFilePath">The path of Apphost to check</param>
/// <param name="bundleHeaderOffset">An out parameter containing the offset of the bundle header (if any)</param>
/// <returns>True if the AppHost is a single-file bundle, false otherwise</returns>
public static bool IsBundle(string appHostFilePath, out long bundleHeaderOffset)
{
byte[] bundleSignature = {
// 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
};
long headerOffset = 0;
void FindBundleHeader()
{
using (var memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostFilePath, FileMode.Open, null, 0, MemoryMappedFileAccess.Read))
{
using (MemoryMappedViewAccessor accessor = memoryMappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read))
{
int position = BinaryUtils.SearchInFile(accessor, bundleSignature);
if (position == -1)
{
throw new PlaceHolderNotFoundInAppHostException(bundleSignature);
}
headerOffset = accessor.ReadInt64(position - sizeof(long));
}
}
}
RetryUtil.RetryOnIOError(FindBundleHeader);
bundleHeaderOffset = headerOffset;
return headerOffset != 0;
}
private static byte[] GetSearchOptionBytes(DotNetSearchOptions searchOptions)
{
if (Path.IsPathRooted(searchOptions.AppRelativeDotNet))
throw new AppRelativePathRootedException(searchOptions.AppRelativeDotNet);
byte[] pathBytes = searchOptions.AppRelativeDotNet != null
? Encoding.UTF8.GetBytes(searchOptions.AppRelativeDotNet)
: [];
if (pathBytes.Length > MaxAppRelativeDotNetSizeInBytes)
throw new AppRelativePathTooLongException(searchOptions.AppRelativeDotNet, MaxAppRelativeDotNetSizeInBytes);
// <search_location> 0 <app_relative_dotnet_root> 0
byte[] searchOptionsBytes = new byte[pathBytes.Length + 3]; // +2 for search location + null, +1 for null terminator
searchOptionsBytes[0] = (byte)searchOptions.Location;
searchOptionsBytes[1] = 0;
searchOptionsBytes[searchOptionsBytes.Length - 1] = 0;
if (pathBytes.Length > 0)
pathBytes.CopyTo(searchOptionsBytes, 2);
return searchOptionsBytes;
}
internal static void Chmod755(string pathName)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return;
var filePermissionOctal = Convert.ToInt32("755", 8); // -rwxr-xr-x
const int EINTR = 4;
int chmodReturnCode;
do
{
chmodReturnCode = chmod(pathName, filePermissionOctal);
}
while (chmodReturnCode == -1 && Marshal.GetLastWin32Error() == EINTR);
if (chmodReturnCode == -1)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), $"Could not set file permission {Convert.ToString(filePermissionOctal, 8)} for {pathName}.");
}
}
[LibraryImport("libc", SetLastError = true)]
private static partial int chmod([MarshalAs(UnmanagedType.LPStr)] string pathname, int mode);
}
}