Skip to content
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

Skip BA2021 Analysis on .NET R2R & NativeAOT PE on non-Windows Platforms #1013

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -47,6 +47,17 @@ public override AnalysisApplicability CanAnalyzePE(PEBinary target, BinaryAnalyz
PE portableExecutable = target.PE;
AnalysisApplicability result = AnalysisApplicability.NotApplicableToSpecifiedTarget;

if (portableExecutable.PEHeaders.CorHeader != null)
{
CoffHeader coffHeader = portableExecutable.PEHeaders.CoffHeader;

// .NET does not follow Windows layout rules on non-Windows platforms.
// The Machine value in the CoffHeader for Windows ARM64 will not be the same for Linux ARM64.
// As a result, we can detect .NET PE's that are non-Windows and skip.
reasonForNotAnalyzing = MetadataConditions.ImageIsNonWindowsDotNetAssembly;
if (IsNonWindowsMachineTarget(coffHeader.Machine)) { return result; }
}

reasonForNotAnalyzing = MetadataConditions.ImageIsKernelModeBinary;
if (portableExecutable.IsKernelMode) { return result; }

Expand Down Expand Up @@ -116,5 +127,10 @@ public override void Analyze(BinaryAnalyzerContext context)
context.CurrentTarget.Uri.GetFileName(),
badSectionsText));
}

private bool IsNonWindowsMachineTarget(Machine machine)
{
return machine != Machine.Amd64 && machine != Machine.I386 && machine != Machine.Arm && machine != Machine.Arm64;
}
}
}
1 change: 1 addition & 0 deletions src/BinSkim.Sdk/MetadataConditions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static class MetadataConditions
public static readonly string ImageIsDotNetCoreEntryPointDll = SdkResources.MetadataCondition_ImageIsDotNetCoreEntryPointDll;
public static readonly string ImageCompiledWithOutdatedTools = SdkResources.MetadataCondition_ImageCompiledWithOutdatedTools;
public static readonly string ImageIsDotNetNativeBootstrapExe = SdkResources.MetadataCondition_ImageIsDotNetNativeBootstrapExe;
public static readonly string ImageIsNonWindowsDotNetAssembly = SdkResources.MetadataCondition_ImageIsNonWindowsDotNetAssembly;
public static readonly string ImageIsPreVersion7WindowsCEBinary = SdkResources.MetadataCondition_ImageIsPreVersion7WindowsCEBinary;
public static readonly string MachOIsNotExecutableDynamicLibraryOrObject = SdkResources.MetadataCondition_MachOIsNotExecutableDynamicLibraryOrObject;
public static readonly string ImageIsNativeUniversalWindowsPlatformBinary = SdkResources.MetadataCondition_ImageIsNativeUniversalWindowsPlatformBinary;
Expand Down
9 changes: 9 additions & 0 deletions src/BinSkim.Sdk/SdkResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/BinSkim.Sdk/SdkResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@
<data name="MetadataCondition_ImageIsDotNetNativeBootstrapExe" xml:space="preserve">
<value>image is a .NET native bootstrap exe</value>
</data>
<data name="MetadataCondition_ImageIsNonWindowsDotNetAssembly" xml:space="preserve">
<value>image is a non-Windows .NET R2R or NativeAOT assembly</value>
</data>
<data name="Verbose_ReplaceWithLevelAndKind" xml:space="preserve">
<value>use --level and --kind</value>
</data>
Expand Down
Loading