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

FEATURE: Add BA3031.EnableClangSafeStack #663

Merged
merged 6 commits into from
Jul 12, 2022

Conversation

shaopeng-gh
Copy link
Collaborator

@shaopeng-gh shaopeng-gh commented Jul 7, 2022

This new rule applies to Clang only since GCC does not support it.
More info:
https://clang.llvm.org/docs/SafeStack.html
#634

Also fixed:

  1. rename BA3030.UseCheckedFunctionsWithGcc to BA3030.UseGccCheckedFunctions
  2. rename MetadataConditions.ElfIsCoreNoneOrObject to MetadataConditions.ElfIsCoreNoneOrRelocatable

@@ -951,7 +951,7 @@
},
{
"id": "BA2026",
"name": "EnableAdditionalSdlSecurityChecks",
"name": "EnableMicrosoftCompilerSdlSwitch",
Copy link
Collaborator Author

@shaopeng-gh shaopeng-gh Jul 7, 2022

Choose a reason for hiding this comment

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

EnableMicrosoftCompilerSdlSwitch

This is not related to the current change, fix by the way. #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice. Thanks for fixing this.

@@ -1416,6 +1416,24 @@ public void BA3030_UseCheckedFunctionsWithGCC_NotApplicable()
this.VerifyApplicability(new UseCheckedFunctionsWithGcc(), new HashSet<string>());
}

[Fact]
public void BA3031_EnableSafeStackWithClang_Pass()
Copy link
Collaborator Author

@shaopeng-gh shaopeng-gh Jul 7, 2022

Choose a reason for hiding this comment

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

public void BA3031_EnableSafeStackWithClang_Pass()

the combination of the test files are:

  1. version, from latest 14 down to 7
  2. language, C and C++

(By these tests we found that lower version has different symbol name) #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome, do we have build script examples to include for these?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes! added.

@@ -4,6 +4,52 @@ This file records scripts used to compile the test files, in alphabetical order.
Base scenario is a simple hello world program built with different parameters for testing purpose.
Test files are located in [BaselineTestData](https://github.com/microsoft/binskim/tree/main/src/Test.FunctionalTests.BinSkim.Driver/BaselineTestData) and [FunctionalTestData](https://github.com/microsoft/binskim/tree/main/src/Test.FunctionalTests.BinSkim.Rules/FunctionalTestData).

## ARM64_CETShadowStack_NotApplicable.exe
Copy link
Collaborator Author

@shaopeng-gh shaopeng-gh Jul 7, 2022

Choose a reason for hiding this comment

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

this is just a re-order alphabetically #Closed

A simple C++ hellow world program, cross compiled using CMake with the `cl.exe` compiler and `Ninja` generator.
`CMakePresets.json` should be configured with a `configurePresets` as below:

```json
Copy link
Collaborator Author

@shaopeng-gh shaopeng-gh Jul 7, 2022

Choose a reason for hiding this comment

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

json

I added the language for ``` per linter request. and empty lines. #Closed

@@ -226,6 +226,28 @@ No checked functions are present/used when compiling '{0}', and it was compiled

---

## Rule `BA3031.EnableSafeStackWithClang`
Copy link
Member

@michaelcfanning michaelcfanning Jul 11, 2022

Choose a reason for hiding this comment

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

EnableSafeStackWithClang

EnableClangSafeStack?

I will think about the name and help refine this help text a bit further. #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@@ -61,6 +61,7 @@ internal static class RuleIds
// BA3012-3029 -- saved for future non-compiler/language specific checks.
// Compiler/Language specific checks follow.
public const string UseCheckedFunctionsWithGcc = "BA3030";
Copy link
Member

@michaelcfanning michaelcfanning Jul 11, 2022

Choose a reason for hiding this comment

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

UseCheckedFunctionsWithGcc

UseGccCheckedFunctions #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed


if (elf.Type == FileType.Core || elf.Type == FileType.None || elf.Type == FileType.Relocatable)
{
reasonForNotAnalyzing = MetadataConditions.ElfIsCoreNoneOrObject;
Copy link
Member

@michaelcfanning michaelcfanning Jul 11, 2022

Choose a reason for hiding this comment

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

ElfIsCoreNoneOrObject

I would tend to name this 'relocatable' as it is, I think, the most precise *nix term for what we're looking at. #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed to ElfIsCoreNoneOrRelocatable


if (!target.Compilers.Any(c => c.Compiler == ElfCompilerType.Clang && c.Version.Major >= 7))
{
reasonForNotAnalyzing = MetadataConditions.ElfNotBuiltWithClangV7OrLater;
Copy link
Member

@michaelcfanning michaelcfanning Jul 11, 2022

Choose a reason for hiding this comment

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

ElfNotBuiltWithClangV7OrLater

Use a generic message, BinaryCompiledWithOutdatedTools or just use the existing one ImageCompiledWithOutdatedTools? #Resolved

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 comment is replaced by later discussions to fire a special error that says you should enable SafeStack, you might need to update your version of Clang to do this.

IEnumerable<ISymbolEntry> symbols =
ElfUtility.GetAllSymbols(elf).Where(sym => sym.Type == SymbolType.File);

if (symbols.Any(s => symbolSafeStack.Contains(s.Name)))
Copy link
Member

@michaelcfanning michaelcfanning Jul 11, 2022

Choose a reason for hiding this comment

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

Foreach over the symbols, to avoid a 2x traversal in the pathological case. Fire a pass result and exit if you meet the first condition. Otherwise fall out and fire the error result. #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changed to not use Linq and just use foreach.
(It may not result in 2x traversal though)

{
reasonForNotAnalyzing = MetadataConditions.ElfNotBuiltWithClangV7OrLater;
return AnalysisApplicability.NotApplicableToSpecifiedTarget;
}
Copy link
Member

@michaelcfanning michaelcfanning Jul 11, 2022

Choose a reason for hiding this comment

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

Remove this. #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed the version check only, we still need to Clang check, if people just use Gcc we will skip this image as NotApplicable.

nameof(RuleResources.BA3031_Pass),
context.TargetUri.GetFileName()));
}
else
Copy link
Member

@michaelcfanning michaelcfanning Jul 11, 2022

Choose a reason for hiding this comment

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

else

When you exit the function, you could do a version check, and if it's v3.8 or whatever minimal version we know about, just fire an error.

Otherwise fire a special error that says you should enable SafeStack, you might need to update your version of Clang to do this. #Resolved

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

agree with this approach, fixed.

Copy link
Member

@michaelcfanning michaelcfanning left a comment

Choose a reason for hiding this comment

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

:shipit:

@shaopeng-gh shaopeng-gh changed the title FEATURE: Add BA3031.EnableSafeStackWithClang FEATURE: Add BA3031.EnableClangSafeStack Jul 12, 2022
Comment on lines +74 to +84
foreach (ISymbolEntry symbol in symbols)
{
if (symbol.Type == SymbolType.File && symbolSafeStack.Contains(symbol.Name))
{
context.Logger.Log(this,
RuleUtilities.BuildResult(ResultKind.Pass, context, null,
nameof(RuleResources.BA3031_Pass),
context.TargetUri.GetFileName()));
return;
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where

This foreach loop implicitly filters its target sequence [here](1) - consider filtering the sequence explicitly using '.Where(...)'.
@@ -1,133 +0,0 @@
{
Copy link
Collaborator Author

@shaopeng-gh shaopeng-gh Jul 12, 2022

Choose a reason for hiding this comment

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

this is just to remove not needed checked in temp file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants