Skip to content

Add magic bytes validation for Mach-O binaries in DotnetHostHelper #15230

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

Merged
merged 2 commits into from
Jul 17, 2025
Merged
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public class DotnetHostHelper : IDotnetHostHelper
{
public const string MONOEXENAME = "mono";

// Mach-O magic numbers from https://en.wikipedia.org/wiki/Mach-O
private const uint MachOMagic32BigEndian = 0xfeedface; // 32-bit big-endian
private const uint MachOMagic64BigEndian = 0xfeedfacf; // 64-bit big-endian
private const uint MachOMagic32LittleEndian = 0xcefaedfe; // 32-bit little-endian
private const uint MachOMagic64LittleEndian = 0xcffaedfe; // 64-bit little-endian
private const uint MachOMagicFatBigEndian = 0xcafebabe; // Multi-architecture big-endian

private readonly IFileHelper _fileHelper;
private readonly IEnvironment _environment;
private readonly IWindowsRegistryHelper _windowsRegistryHelper;
Expand Down Expand Up @@ -414,6 +421,14 @@ public bool TryGetDotnetPathByArchitecture(
ReadExactly(headerReader, cpuInfoBytes, 0, cpuInfoBytes.Length);

var magic = BitConverter.ToUInt32(magicBytes, 0);

// Validate magic bytes to ensure this is a valid Mach-O binary
if (magic is not (MachOMagic32BigEndian or MachOMagic64BigEndian or MachOMagic32LittleEndian or MachOMagic64LittleEndian or MachOMagicFatBigEndian))
{
EqtTrace.Error($"DotnetHostHelper.GetMuxerArchitectureByMachoOnMac: Invalid Mach-O magic bytes: 0x{magic:X8}");
return null;
}

var cpuInfo = BitConverter.ToUInt32(cpuInfoBytes, 0);
PlatformArchitecture? architecture = (MacOsCpuType)cpuInfo switch
{
Expand Down
Loading