Skip to content

Commit 2dea4d4

Browse files
Copilotnohwnd
andcommitted
Add magic bytes validation for Mach-O binaries in DotnetHostHelper
Co-authored-by: nohwnd <[email protected]>
1 parent 9e534b3 commit 2dea4d4

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/Microsoft.TestPlatform.CoreUtilities/Helpers/DotnetHostHelper.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public class DotnetHostHelper : IDotnetHostHelper
2323
{
2424
public const string MONOEXENAME = "mono";
2525

26+
// Mach-O magic numbers from https://en.wikipedia.org/wiki/Mach-O
27+
private const uint MachOMagic32BigEndian = 0xfeedface; // 32-bit big-endian
28+
private const uint MachOMagic64BigEndian = 0xfeedfacf; // 64-bit big-endian
29+
private const uint MachOMagic32LittleEndian = 0xcefaedfe; // 32-bit little-endian
30+
private const uint MachOMagic64LittleEndian = 0xcffaedfe; // 64-bit little-endian
31+
private const uint MachOMagicFatBigEndian = 0xcafebabe; // Multi-architecture big-endian
32+
2633
private readonly IFileHelper _fileHelper;
2734
private readonly IEnvironment _environment;
2835
private readonly IWindowsRegistryHelper _windowsRegistryHelper;
@@ -414,6 +421,14 @@ public bool TryGetDotnetPathByArchitecture(
414421
ReadExactly(headerReader, cpuInfoBytes, 0, cpuInfoBytes.Length);
415422

416423
var magic = BitConverter.ToUInt32(magicBytes, 0);
424+
425+
// Validate magic bytes to ensure this is a valid Mach-O binary
426+
if (magic is not (MachOMagic32BigEndian or MachOMagic64BigEndian or MachOMagic32LittleEndian or MachOMagic64LittleEndian or MachOMagicFatBigEndian))
427+
{
428+
EqtTrace.Error($"DotnetHostHelper.GetMuxerArchitectureByMachoOnMac: Invalid Mach-O magic bytes: 0x{magic:X8}");
429+
return null;
430+
}
431+
417432
var cpuInfo = BitConverter.ToUInt32(cpuInfoBytes, 0);
418433
PlatformArchitecture? architecture = (MacOsCpuType)cpuInfo switch
419434
{

0 commit comments

Comments
 (0)