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

Fix BA2027.EnableSourceLink cause ExceptionLoadingPdb #988

Merged
merged 3 commits into from
Mar 29, 2024
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
3 changes: 2 additions & 1 deletion ReleaseHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
## UNRELEASED
* DEP: Update `Sarif.Sdk` submodule from [bc8cb57 to fd6e615](https://github.com/microsoft/sarif-sdk/compare/bc8cb57...fd6e615). Reference [SARIF SDK Release History](https://github.com/microsoft/sarif-sdk/blob/fd6e615/ReleaseHistory.md).
* NEW: Add `--disable-telemetry` argument to disable telemetry collection.
* BUG: Fix `ERR998.ExceptionInAnalyze`: `InvalidOperationException: Unrecognized crypto HRESULT: 0x80096011` for check `BA2022.SignSecurely` when the signature is malformed, by adding missing error code to error description mappings. [969](https://github.com/microsoft/binskim/pull/969)
* BUG: Fix `ERR998.ExceptionInAnalyze`: `InvalidOperationException: Unrecognized crypto HRESULT: 0x80096011` for check `BA2022.SignSecurely` when the signature is malformed, by adding missing error code to error description mappings. [969](https://github.com/microsoft/binskim/pull/969).
* BUG: Fix `BA2027.EnableSourceLink` unexpectedly causes `ExceptionLoadingPdb` error when the PDB file is missing. [988](https://github.com/microsoft/binskim/pull/988).

## **v4.2.1**
* FPS: `BA2004.EnableSecureSourceCodeHashing` now will no longer generate false positives on precompiled headers, they are always without hash. [#965](https://github.com/microsoft/binskim/pull/965)
Expand Down
11 changes: 10 additions & 1 deletion src/BinSkim.Rules/PERules/BA2027.EnableSourceLink.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
Expand All @@ -22,6 +21,8 @@ public class EnableSourceLink : WindowsBinaryAndPdbSkimmerBase
/// </summary>
public override string Id => RuleIds.EnableSourceLink;

public override bool LogPdbLoadException => false;

/// <summary>
/// Enable SourceLink.
/// </summary>
Expand Down Expand Up @@ -66,6 +67,14 @@ public override AnalysisApplicability CanAnalyzePE(PEBinary target, BinaryAnalyz

public override void AnalyzePortableExecutableAndPdb(BinaryAnalyzerContext context)
{
PEBinary target = context.PEBinary();
Pdb pdb = target.Pdb;

if (pdb == null)
{
return;
}

if (!HasSourceLink(context))
{
// The PDB for '{0}' does not contain SourceLink information, compromising
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void AnalyzeCommand_DeterminismTest()

WindowsBinaryAndPdbSkimmerBase.s_PdbExceptions.Clear();
string fileName = Path.Combine(Path.GetTempPath(), "AnalyzeCommand_DeterminismTest.sarif");
string pathDeterminismTest = Path.Combine(PEBinaryTests.TestData, "PE", "Determinism", "*.dll");
string pathDeterminismTest = Path.Combine(PEBinaryTests.TestData, "PE", "Determinism", "*.exe");
var options = new AnalyzeOptions
{
TargetFileSpecifiers = new string[] {
Expand Down
66 changes: 66 additions & 0 deletions src/Test.UnitTests.BinSkim.Rules/RulePropertyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

using FluentAssertions;

using Xunit;

namespace Microsoft.CodeAnalysis.IL.Rules
{
public class RulePropertyTests
{
private static readonly string[] ExpectedLogPdbLoadExceptionRules = new string[]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Please see PR desc.

{
"BA2002.DoNotIncorporateVulnerableDependencies",
"BA2006.BuildWithSecureTools",
"BA2007.EnableCriticalCompilerWarnings",
"BA2011.EnableStackProtection",
"BA2013.InitializeStackProtection",
"BA2014.DoNotDisableStackProtectionForFunctions",
"BA2024.EnableSpectreMitigations",
"BA2025.EnableShadowStack",
"BA2026.EnableMicrosoftCompilerSdlSwitch",
"BA6001.DisableIncrementalLinkingInReleaseBuilds",
"BA6002.EliminateDuplicateStrings",
"BA6004.EnableComdatFolding",
"BA6005.EnableOptimizeReferences",
"BA6006.EnableLinkTimeCodeGeneration"
};

[Fact]
public void RulePropertyTests_LogPdbLoadException()
{
WindowsBinaryAndPdbSkimmerBase[] skimmers =
GetAllWindowsBinaryAndPdbSkimmers("BinSkim.Rules.dll");
IEnumerable<WindowsBinaryAndPdbSkimmerBase> actualLogPdbLoadExceptionRules =
skimmers.Where(s => s.LogPdbLoadException);
IEnumerable<WindowsBinaryAndPdbSkimmerBase> unexpectedLogPdbLoadExceptionRules =
actualLogPdbLoadExceptionRules.Where(s => !ExpectedLogPdbLoadExceptionRules.Contains(s.Moniker));

if (unexpectedLogPdbLoadExceptionRules.Any())
{
Assert.Fail(string.Format("Please examine if the following rules should enable 'LogPdbLoadException': {0}",
string.Join(", ", unexpectedLogPdbLoadExceptionRules.Select(skimmer => skimmer.Moniker))));
}
}

private static WindowsBinaryAndPdbSkimmerBase[] GetAllWindowsBinaryAndPdbSkimmers(string rulesAssemblyName)
{
string directory = AppDomain.CurrentDomain.BaseDirectory;
string assemblyPath = Path.Combine(directory, rulesAssemblyName);
var assembly = Assembly.LoadFrom(assemblyPath);
Type[] assemblyTypes = assembly.GetTypes();
IEnumerable<Type> inheritanceTypes =
assemblyTypes.Where(t => t.BaseType == typeof(WindowsBinaryAndPdbSkimmerBase));
IEnumerable<WindowsBinaryAndPdbSkimmerBase> instances =
inheritanceTypes.Select(t => (WindowsBinaryAndPdbSkimmerBase)Activator.CreateInstance(t));
return instances.ToArray();
}
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The old test was based on BA2027 throw that error, now that is fixed, so need to change to another file triggered by other rules.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading