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

Throwing once per file #465

Merged
merged 6 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -141,7 +141,7 @@ public override void AnalyzePortableExecutableAndPdb(BinaryAnalyzerContext conte

if (warningLevel < 3)
{
exampleTooLowWarningCommandLine = exampleTooLowWarningCommandLine ?? omDetails.RawCommandLine;
exampleTooLowWarningCommandLine ??= omDetails.RawCommandLine;

string msg = "[warning level: " + warningLevel.ToString(CultureInfo.InvariantCulture) + "]";
warningTooLowModules.Add(om.CreateCompilandRecordWithSuffix(msg));
Expand All @@ -151,7 +151,7 @@ public override void AnalyzePortableExecutableAndPdb(BinaryAnalyzerContext conte
if (requiredDisabledWarnings.Count != 0)
{
MergeInto(overallDisabledWarnings, requiredDisabledWarnings);
exampleDisabledWarningCommandLine = exampleDisabledWarningCommandLine ?? omDetails.RawCommandLine;
exampleDisabledWarningCommandLine ??= omDetails.RawCommandLine;

string msg = "[Explicitly disabled warnings: " + CreateTextWarningList(requiredDisabledWarnings) + "]";
disabledWarningModules.Add(om.CreateCompilandRecordWithSuffix(msg));
Expand Down
21 changes: 16 additions & 5 deletions src/BinSkim.Rules/PERules/WindowsBinaryAndPdbSkimmerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;

using Microsoft.CodeAnalysis.BinaryParsers;
Expand All @@ -26,6 +28,8 @@ public abstract class WindowsBinaryAndPdbSkimmerBase : WindowsBinarySkimmerBase

public virtual bool LogPdbLoadException => true;

public static readonly ConcurrentDictionary<string, bool> s_PdbExceptions = new ConcurrentDictionary<string, bool>();

public sealed override void Analyze(BinaryAnalyzerContext context)
{
// Uses PDB Parsing.
Expand Down Expand Up @@ -130,20 +134,27 @@ public static void LogExceptionLoadingPdb(IAnalysisContext context, PdbException
throw new ArgumentNullException(nameof(context));
}

// '{0}' was not evaluated for check '{1}' because its PDB could not be loaded ({2}).
string fileName = context.TargetUri.GetFileName();
Copy link
Collaborator

@shaopeng-gh shaopeng-gh Aug 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fileName

key sould also consider path?
/x86/a.exe
/x64/a.exe #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to OriginalString as path. That will prevent issues like you showed.

thanks!

string key = $"{fileName}@{pdbException.ExceptionDisplayMessage}";
if (s_PdbExceptions.ContainsKey(key))
Copy link
Contributor Author

@eddynaka eddynaka Aug 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s_PdbExceptions.ContainsKey(key)

If we already reported this issue for this target, we won't report twice. #ByDesign

{
return;
}

// '{0}' was not evaluated because its PDB could not be loaded ({1}).
context.Logger.LogConfigurationNotification(
Errors.CreateNotification(
context.TargetUri,
"ERR997.ExceptionLoadingPdb",
context.Rule.Id,
string.Empty,
Copy link
Contributor Author

@eddynaka eddynaka Aug 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string.Empty

To be generic, I removed context.Rule.Id and context.Rule.Name. #ByDesign

FailureLevel.Error,
pdbException,
persistExceptionStack: false,
RuleResources.ERR997_ExceptionLoadingPdb,
context.TargetUri.GetFileName(),
context.Rule.Name,
RuleResources.ERR997_ExceptionLoadingPdbGeneric,
fileName,
pdbException.ExceptionDisplayMessage));

s_PdbExceptions.TryAdd(key, true);
Copy link
Contributor Author

@eddynaka eddynaka Aug 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s_PdbExceptions.TryAdd

Once we log the notification, we will add to the caching variable. Since it's static and concurrent, it won't face any issue during analysis #ByDesign

context.RuntimeErrors |= RuntimeConditions.ExceptionLoadingPdb;

if (!string.IsNullOrEmpty(pdbException.LoadTrace))
Expand Down
11 changes: 10 additions & 1 deletion src/BinSkim.Rules/RuleResources.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.Rules/RuleResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,7 @@ Modules did not meet the criteria: {1}</value>
<data name="BA5002_Pass" xml:space="preserve">
<value>Executable stack is not allowed on executable '{0}'.</value>
</data>
<data name="ERR997)ExceptionLoadingPdbGeneric" xml:space="preserve">
Copy link
Collaborator

@shaopeng-gh shaopeng-gh Aug 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)

typo? #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! thanks for reviewing!

<value>'{0}' was not evaluated because its PDB could not be loaded ({1}).</value>
</data>
</root>
18 changes: 4 additions & 14 deletions src/Test.FunctionalTests.BinSkim.Rules/RuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,8 @@ public void BA2002_DoNotIncorporateVulnerableDependencies_Fail()
{
if (BinaryParsers.PlatformSpecificHelpers.RunningOnWindows())
{
var failureConditions = new HashSet<string> { MetadataConditions.CouldNotLoadPdb };
this.VerifyFail(
new DoNotIncorporateVulnerableDependencies(),
this.GetTestFilesMatchingConditions(failureConditions),
useDefaultPolicy: true);
}
else
Expand Down Expand Up @@ -625,14 +623,8 @@ public void BA2007_EnableCriticalCompilerWarnings_Fail()
{
if (BinaryParsers.PlatformSpecificHelpers.RunningOnWindows())
{
var failureConditions = new HashSet<string>
{
MetadataConditions.CouldNotLoadPdb
};

this.VerifyFail(
new EnableCriticalCompilerWarnings(),
this.GetTestFilesMatchingConditions(failureConditions),
useDefaultPolicy: true);
}
else
Expand Down Expand Up @@ -841,16 +833,14 @@ public void BA2013_InitializeStackProtection_Fail()
{
if (BinaryParsers.PlatformSpecificHelpers.RunningOnWindows())
{
var failureConditions = new HashSet<string>
{
MetadataConditions.CouldNotLoadPdb
};

this.VerifyFail(
new InitializeStackProtection(),
this.GetTestFilesMatchingConditions(failureConditions),
useDefaultPolicy: true);
}
else
{
this.VerifyThrows<PlatformNotSupportedException>(new DoNotShipVulnerableBinaries(), useDefaultPolicy: true);
}
}

[Fact]
Expand Down