-
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
BA3006.EnableNonExecutableStack #383
Merged
Merged
Changes from 3 commits
Commits
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
78 changes: 78 additions & 0 deletions
78
src/BinSkim.Rules/ELFRules/BA3006.EnableNonExecutableStack.cs
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,78 @@ | ||
// 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 ELFSharp.ELF.Segments; | ||
|
||
using Microsoft.CodeAnalysis.BinaryParsers; | ||
using Microsoft.CodeAnalysis.BinaryParsers.ELF; | ||
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))] | ||
public class EnableNonExecutableStack : ELFBinarySkimmerBase | ||
{ | ||
/// <summary> | ||
/// BA3006 | ||
/// </summary> | ||
public override string Id => RuleIds.EnableNonExecutableStack; | ||
|
||
/// <summary> | ||
/// This check ensures that non-executable stack is enabled. A common type of exploit is the stack buffer overflow. | ||
/// An application receives, from an attacker, more data than it is prepared for and stores this information on its stack, | ||
/// writing beyond the space reserved for it. This can be designed to cause execution of the data written on the stack. | ||
/// One mechanism to mitigate this vulnerability is for the system to not allow the execution of instructions in sections | ||
/// of memory identified as part of the stack. Use the compiler flags '-z noexecstack' to enable this. | ||
/// </summary> | ||
public override MultiformatMessageString FullDescription => | ||
new MultiformatMessageString { Text = RuleResources.BA3006_EnableNonExecutableStack_Description }; | ||
|
||
protected override IEnumerable<string> MessageResourceNames => new string[] { | ||
nameof(RuleResources.BA3006_Pass), | ||
nameof(RuleResources.BA3006_Error), | ||
nameof(RuleResources.NotApplicable_InvalidMetadata) | ||
}; | ||
|
||
public override AnalysisApplicability CanAnalyzeElf(ELFBinary target, Sarif.PropertiesDictionary policy, out string reasonForNotAnalyzing) | ||
{ | ||
reasonForNotAnalyzing = null; | ||
|
||
if (target.GetSegmentFlags(ELFSegmentType.PT_GNU_STACK) == null) | ||
{ | ||
reasonForNotAnalyzing = MetadataConditions.ElfNotContainSegment; | ||
return AnalysisApplicability.NotApplicableToSpecifiedTarget; | ||
} | ||
|
||
reasonForNotAnalyzing = null; | ||
return AnalysisApplicability.ApplicableToSpecifiedTarget; | ||
} | ||
|
||
public override void Analyze(BinaryAnalyzerContext context) | ||
{ | ||
ELFBinary elfBinary = context.ELFBinary(); | ||
|
||
if ((elfBinary.GetSegmentFlags(ELFSegmentType.PT_GNU_STACK) & SegmentFlags.Execute) != 0) | ||
{ | ||
// The non-executable stack is not enabled for this binary, | ||
// so '{0}' can have a vulnerability of execution of the data written on the stack. | ||
// Ensure you are compiling with the flag '-z noexecstack' to address this. | ||
context.Logger.Log(this, | ||
RuleUtilities.BuildResult(FailureLevel.Error, context, null, | ||
nameof(RuleResources.BA3006_Error), | ||
context.TargetUri.GetFileName())); | ||
return; | ||
} | ||
|
||
// The enable non-executable stack flag was present, so '{0}' is protected. | ||
context.Logger.Log(this, | ||
RuleUtilities.BuildResult(ResultKind.Pass, context, null, | ||
nameof(RuleResources.BA3006_Pass), | ||
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.
remove the word 'enable'. #Closed
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.
fixed