Skip to content

Commit dad86d0

Browse files
committed
Skip BA2021 Analysis on .NET R2R & NativeAOT PE on non-Windows Platforms
This change skips analysis when it finds non-Windows .NET R2R & NativeAOT PE's. The reason for this is the DoNotMarkWritableSectionsAsExecutable is Windows specific and R2R/NativeAOT do not follow Windows layout rules on non-Windows platforms. Fixes microsoft#970
1 parent aa28314 commit dad86d0

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

src/BinSkim.Rules/PERules/BA2021.DoNotMarkWritableSectionsAsExecutable.cs

+17
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public override AnalysisApplicability CanAnalyzePE(PEBinary target, BinaryAnalyz
4747
PE portableExecutable = target.PE;
4848
AnalysisApplicability result = AnalysisApplicability.NotApplicableToSpecifiedTarget;
4949

50+
PEBinary target = context.PEBinary();
51+
if (target.PE.PEHeaders.CorHeader != null)
52+
{
53+
CoffHeader coffHeader = target.PE.PEHeaders.CoffHeader;
54+
55+
// .NET does not follow Windows layout rules on non-Windows platforms.
56+
// The Machine value in the CoffHeader for Windows ARM64 will not be the same for Linux ARM64.
57+
// As a result, we can detect .NET PE's that are non-Windows and skip.
58+
reasonForNotAnalyzing = MetadataConditions.ImageIsNonWindowsDotNetAssembly;
59+
if (IsNonWindowsMachineTarget(coffHeader.Machine)) { return result; }
60+
}
61+
5062
reasonForNotAnalyzing = MetadataConditions.ImageIsKernelModeBinary;
5163
if (portableExecutable.IsKernelMode) { return result; }
5264

@@ -116,5 +128,10 @@ public override void Analyze(BinaryAnalyzerContext context)
116128
context.CurrentTarget.Uri.GetFileName(),
117129
badSectionsText));
118130
}
131+
132+
private bool IsNonWindowsMachineTarget(Machine machine)
133+
{
134+
return (machine & (Machine.Amd64 | Machine.Arm | Machine.Arm64));
135+
}
119136
}
120137
}

src/BinSkim.Sdk/MetadataConditions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static class MetadataConditions
4141
public static readonly string ImageIsDotNetCoreEntryPointDll = SdkResources.MetadataCondition_ImageIsDotNetCoreEntryPointDll;
4242
public static readonly string ImageCompiledWithOutdatedTools = SdkResources.MetadataCondition_ImageCompiledWithOutdatedTools;
4343
public static readonly string ImageIsDotNetNativeBootstrapExe = SdkResources.MetadataCondition_ImageIsDotNetNativeBootstrapExe;
44+
public static readonly string ImageIsNonWindowsDotNetAssembly = SdkResources.MetadataCondition_ImageIsNonWindowsDotNetAssembly;
4445
public static readonly string ImageIsPreVersion7WindowsCEBinary = SdkResources.MetadataCondition_ImageIsPreVersion7WindowsCEBinary;
4546
public static readonly string MachOIsNotExecutableDynamicLibraryOrObject = SdkResources.MetadataCondition_MachOIsNotExecutableDynamicLibraryOrObject;
4647
public static readonly string ImageIsNativeUniversalWindowsPlatformBinary = SdkResources.MetadataCondition_ImageIsNativeUniversalWindowsPlatformBinary;

src/BinSkim.Sdk/SdkResources.Designer.cs

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BinSkim.Sdk/SdkResources.resx

+3
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@
222222
<data name="MetadataCondition_ImageIsDotNetNativeBootstrapExe" xml:space="preserve">
223223
<value>image is a .NET native bootstrap exe</value>
224224
</data>
225+
<data name="MetadataCondition_ImageIsNonWindowsDotNetAssembly" xml:space="preserve">
226+
<value>image is a non-Windows .NET R2R or NativeAOT assembly</value>
227+
</data>
225228
<data name="Verbose_ReplaceWithLevelAndKind" xml:space="preserve">
226229
<value>use --level and --kind</value>
227230
</data>

0 commit comments

Comments
 (0)