-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating shadowstack logic Removing debugging files Improving message strings Updating tests
- Loading branch information
Showing
14 changed files
with
164 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Composition; | ||
using System.Reflection.PortableExecutable; | ||
|
||
using Microsoft.CodeAnalysis.BinaryParsers; | ||
using Microsoft.CodeAnalysis.BinaryParsers.PortableExecutable; | ||
using Microsoft.CodeAnalysis.IL.Sdk; | ||
using Microsoft.CodeAnalysis.Sarif; | ||
using Microsoft.CodeAnalysis.Sarif.Driver; | ||
|
||
namespace Microsoft.CodeAnalysis.IL.Rules | ||
{ | ||
[Export(typeof(Skimmer<BinaryAnalyzerContext>)), Export(typeof(ReportingDescriptor)), Export(typeof(IOptionsProvider))] | ||
public class EnableShadowStack : WindowsBinaryAndPdbSkimmerBase | ||
{ | ||
private const int IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS = 20; | ||
private const ushort IMAGE_DLLCHARACTERISTICS_EX_CET_COMPAT = 0x001; | ||
|
||
/// <summary> | ||
/// BA2025 | ||
/// </summary> | ||
public override string Id => RuleIds.EnableShadowStack; | ||
|
||
/// <summary> | ||
/// Control-flow Enforcement Technology (CET) Shadow Stack is a computer processor feature | ||
/// that provides capabilities to defend against return-oriented programming (ROP) based | ||
/// malware attacks. | ||
/// </summary> | ||
public override MultiformatMessageString FullDescription => new MultiformatMessageString | ||
{ | ||
Text = RuleResources.BA2025_EnableShadowStack_Description | ||
}; | ||
|
||
protected override IEnumerable<string> MessageResourceNames => new string[] { | ||
nameof(RuleResources.BA2025_Pass), | ||
nameof(RuleResources.BA2025_Warning), | ||
nameof(RuleResources.NotApplicable_InvalidMetadata) | ||
}; | ||
|
||
public override AnalysisApplicability CanAnalyzePE(PEBinary target, Sarif.PropertiesDictionary policy, out string reasonForNotAnalyzing) | ||
{ | ||
PE portableExecutable = target.PE; | ||
AnalysisApplicability result = AnalysisApplicability.NotApplicableToSpecifiedTarget; | ||
|
||
reasonForNotAnalyzing = MetadataConditions.ImageIsILOnlyAssembly; | ||
if (portableExecutable.IsILOnly) { return result; } | ||
|
||
reasonForNotAnalyzing = MetadataConditions.ImageIsResourceOnlyBinary; | ||
if (portableExecutable.IsResourceOnly) { return result; } | ||
|
||
reasonForNotAnalyzing = MetadataConditions.ImageIsNativeUniversalWindowsPlatformBinary; | ||
if (portableExecutable.IsNativeUniversalWindowsPlatform) { return result; } | ||
|
||
reasonForNotAnalyzing = null; | ||
return AnalysisApplicability.ApplicableToSpecifiedTarget; | ||
} | ||
|
||
public override void AnalyzePortableExecutableAndPdb(BinaryAnalyzerContext context) | ||
{ | ||
PEBinary target = context.PEBinary(); | ||
IEnumerable<DebugDirectoryEntry> debugDirectories = target.PE.DebugDirectories; | ||
|
||
if (debugDirectories == null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (DebugDirectoryEntry debugDirectory in debugDirectories) | ||
{ | ||
if (debugDirectory.Type == (DebugDirectoryEntryType)IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS) | ||
{ | ||
PEMemoryBlock memory = target.PE.GetSectionData(debugDirectory.DataRelativeVirtualAddress); | ||
if ((memory.GetReader().ReadUInt16() & IMAGE_DLLCHARACTERISTICS_EX_CET_COMPAT) == IMAGE_DLLCHARACTERISTICS_EX_CET_COMPAT) | ||
{ | ||
// '{0}' enables the Control-flow Enforcement Technology (CET) Shadow Stack mitigation. | ||
context.Logger.Log(this, | ||
RuleUtilities.BuildResult(ResultKind.Pass, context, null, | ||
nameof(RuleResources.BA2025_Pass), | ||
context.TargetUri.GetFileName())); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
// '{0}' does not enable the Control-flow Enforcement Technology (CET) Shadow Stack mitigation. | ||
// To resolve this issue, pass /CETCOMPAT on the linker command lines. | ||
context.Logger.Log(this, | ||
RuleUtilities.BuildResult(FailureLevel.Warning, context, null, | ||
nameof(RuleResources.BA2025_Warning), | ||
context.TargetUri.GetFileName())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+65.5 KB
.../FunctionalTestsData/BA2025.EnableShadowStack/Fail/Native_x64_CETShadowStack_Disabled.exe
Binary file not shown.
Binary file added
BIN
+468 KB
.../FunctionalTestsData/BA2025.EnableShadowStack/Fail/Native_x64_CETShadowStack_Disabled.pdb
Binary file not shown.
Binary file added
BIN
+65.5 KB
...unctionalTestsData/BA2025.EnableShadowStack/Fail/Native_x64_CETShadowStack_NotEnabled.exe
Binary file not shown.
Binary file added
BIN
+468 KB
...unctionalTestsData/BA2025.EnableShadowStack/Fail/Native_x64_CETShadowStack_NotEnabled.pdb
Binary file not shown.
Binary file added
BIN
+65.5 KB
...s/FunctionalTestsData/BA2025.EnableShadowStack/Pass/Native_x64_CETShadowStack_Enabled.exe
Binary file not shown.
Binary file added
BIN
+468 KB
...s/FunctionalTestsData/BA2025.EnableShadowStack/Pass/Native_x64_CETShadowStack_Enabled.pdb
Binary file not shown.
Binary file added
BIN
+37 KB
...s/FunctionalTestsData/BA2025.EnableShadowStack/Pass/Native_x86_CETShadowStack_Enabled.dll
Binary file not shown.
Binary file added
BIN
+500 KB
...s/FunctionalTestsData/BA2025.EnableShadowStack/Pass/Native_x86_CETShadowStack_Enabled.pdb
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters