-
Notifications
You must be signed in to change notification settings - Fork 156
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
Enable shadow-stack #376
Merged
Merged
Enable shadow-stack #376
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
other way is to move the assign statement inside the
if() {assign statement;return result; }
since it is only needed when if is true
#Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pls, do the proper changes :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it, updated.