Skip to content

fix: populate fileHash in CompilerDataLogger telemetry#1199

Closed
martin-reznik wants to merge 7 commits into
mainfrom
marez/fix-compiler-data-hash
Closed

fix: populate fileHash in CompilerDataLogger telemetry#1199
martin-reznik wants to merge 7 commits into
mainfrom
marez/fix-compiler-data-hash

Conversation

@martin-reznik

Copy link
Copy Markdown
Collaborator

Summary

The hash field in CompilerDataLogger telemetry (AppInsights CompilerInformation events + CSV) has always been empty because context.Hashes is not populated by the SARIF framework during rule execution.

Root Cause

CompilerDataLogger.Write() reads context.Hashes?.Sha256, but the SARIF SDK only computes hashes for SARIF artifact entries after analysis - not before dispatching rules.

Fix

Compute the hash directly in the rules via new CompilerData.FileHash property:

  • BA4001 (PE): uses PE.SHA256Hash (zero extra I/O)
  • BA4002 (ELF/Mach-O): SHA256.HashData(File.ReadAllBytes(...))

Tests

Two tests verify hash is populated in CSV output with and without DataToInsert=Hashes.

The hash field in CompilerDataLogger telemetry (AppInsights + CSV) was
always empty because context.Hashes is not populated by the SARIF framework
during rule execution.

Fix: compute the hash in BA4001/BA4002 and pass it via CompilerData.FileHash.
- PE: uses PE.SHA256Hash (zero I/O, already loaded in memory)
- ELF/Mach-O: reads file and computes SHA-256

CompilerDataLogger.Write() now prefers compilerData.FileHash, falling back
to context.Hashes?.Sha256 for backward compatibility.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@martin-reznik martin-reznik requested a review from a team as a code owner July 2, 2026 19:16
Comment thread src/BinSkim.Rules/DwarfRules/BA4002.ReportElfOrMachoCompilerData.cs Fixed
@martin-reznik martin-reznik mentioned this pull request Jul 2, 2026
Add cached SHA256Hash property on BinaryBase so ELF, Mach-O, and PE
all share the same pattern. BA4002 now uses BinaryBase.SHA256Hash
instead of a local helper. For PE, BA4001 still uses target.PE.SHA256Hash
which benefits from the already-loaded in-memory image.

For Mach-O fat binaries (multiple arch slices), the hash is of the whole
file - all slices share one file and one hash.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread src/BinaryParsers/BinaryBase.cs Fixed
martin-reznik and others added 5 commits July 2, 2026 21:49
- Make BinaryBase.SHA256Hash virtual
- Override in PEBinary to delegate to PE.SHA256Hash (uses in-memory image)
- Add SHA256Hash to IDwarfBinary interface so BA4002 can use it without
  casting to BinaryBase
- Update StubDwarfBinary in tests

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ssException

Addresses CodeQL 'Generic catch clause' finding.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…nal build trigger

Replace System.AccessToken with AzureCLI@2 using the
binskim-internal-pipeline-runner service connection to get a token
for the ADO API. Fixes permission error when queuing the internal pipeline.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…tryFileHash

Add IncludeTelemetryFileHash policy option (default: true). When set to
false, BA4001/BA4002 skip SHA-256 computation. Configurable via policy XML.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@martin-reznik

Copy link
Copy Markdown
Collaborator Author

Closing - hash fix will be handled in Guardian instead. Pipeline fixes moved to #1201.

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.

2 participants