Skip to content

BA3003: tolerate exceptions from DWARF CommandLineInfos parser#1191

Open
himaja-kesari wants to merge 2 commits into
microsoft:mainfrom
himaja-kesari:fix/ba3003-tolerate-dwarf-parser-exception
Open

BA3003: tolerate exceptions from DWARF CommandLineInfos parser#1191
himaja-kesari wants to merge 2 commits into
microsoft:mainfrom
himaja-kesari:fix/ba3003-tolerate-dwarf-parser-exception

Conversation

@himaja-kesari
Copy link
Copy Markdown

Summary

When binary.CommandLineInfos throws while parsing DWARF producer entries, BA3003 currently aborts for the entire target with an ERR998 instead of falling back to the symbol-table heuristic the rule already has for ELF binaries with no DWARF info.

This happens in practice on real-world binaries where the DWARF parser encounters producer entries it does not yet understand — e.g. newer DW_FORM_* values emitted by recent toolchains (GCC 15 with -gdwarf-5), or string-form references whose backing section the parser cannot resolve (e.g. DW_FORM_GNU_strp_alt pointing into a .gnu_debugaltlink alt file produced by dwz).

Change

Wrap the binary.CommandLineInfos access in a try/catch. On exception, treat the binary as having no DWARF compile-unit information, which lets the existing symbol-table fallback (__stack_chk_fail / __stack_chk_guard / __intel_security_cookie) run as it already does for ELF binaries that simply have no DWARF info.

No behavior change for inputs whose DWARF producer tags parse successfully.

Repro that motivated this

Stripped ELF binaries shipped by Azure Linux 4 (e.g. /usr/lib/dracut/skipcpio from dracut-107-9.azl4.x86_64.rpm) cause BinSkim 4.4.x to throw InvalidOperationException (Producer : 7969) inside BA3003 when --local-symbol-directories is pointed at the matching *-debuginfo tree, because the producer tag in the separate .debug file uses DW_FORM_GNU_strp_alt. With this patch the rule still cannot affirmatively validate the producer (that requires alt-file support, a separate change), but it no longer hard-fails — it falls back to the symbol heuristic, which is the documented behavior for the no-DWARF case.

Testing

  • Builds clean against main (commit 696f2ba) with dotnet build src/BinSkim.Rules/BinSkim.Rules.csproj -c Release.
  • Verified locally with a self-contained linux-x64 publish that the previously-crashing skipcpio scan now completes and falls through to the symbol check, matching the no-DWARF code path.

I am happy to add a functional-test fixture if maintainers can suggest the preferred shape (a minimal ELF with a deliberately malformed producer abbrev) — the existing BA3003 test data only exercises the happy path.

Some real-world ELF binaries contain DWARF producer entries that the
DWARF parser does not yet understand (for example, newer DW_FORM_*
values emitted by recent toolchains, or string-form references whose
backing section the parser cannot resolve).

When that happens, binary.CommandLineInfos throws and currently aborts
BA3003 entirely for the target with an ERR998. After this change the
exception is caught and the rule falls back to the symbol-table
heuristic (looking for __stack_chk_fail / __stack_chk_guard /
__intel_security_cookie), matching the behavior already used for ELF
binaries that simply lack any DWARF compile-unit information.

No behavior change for inputs whose DWARF producer tags parse
successfully.
@himaja-kesari himaja-kesari requested a review from a team as a code owner May 28, 2026 20:05
Comment thread src/BinSkim.Rules/DwarfRules/BA3003.EnableStackProtector.cs Fixed
The DWARF parser code paths reachable from CommandLineInfos throw a
mix of exception types -- InvalidOperationException,
NotImplementedException, ArgumentException, DwarfBufferOverreadException,
and bare System.Exception (see DwarfCompilationUnit / DwarfMemoryReader
/ DwarfCommonInformationEntry). Because some throw the base Exception
type directly, we cannot narrow this catch any further without
re-introducing the ERR998 abort the parent commit set out to fix.

Add a #pragma warning disable for CA1031 with an inline justification
and expand the existing comment to document the constraint.
@himaja-kesari
Copy link
Copy Markdown
Author

Addressed the CodeQL CA1031 note in 9df5035: I expanded the comment to enumerate the exception types thrown by the DWARF parser paths reachable from CommandLineInfos (InvalidOperationException, NotImplementedException, ArgumentException, DwarfBufferOverreadException, and bare System.Exception in DwarfCompilationUnit / DwarfMemoryReader / DwarfCommonInformationEntry) and wrapped the catch with #pragma warning disable CA1031 + justification.

Because some of those call sites throw the base Exception type directly, narrowing the catch any further would re-introduce the ERR998 abort this PR exists to fix. If maintainers would prefer those parser sites be changed to throw a dedicated exception type (e.g. DwarfParseException) so the catch can be narrowed, I am happy to do that as a follow-up PR — just let me know which shape you prefer.

Comment on lines +178 to +182
catch (Exception)
#pragma warning restore CA1031
{
commandLineInfos = new List<DwarfCompileCommandLineInfo>();
}
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