Skip to content

feat(firmware): inspect Qualcomm MBN image headers and hash segments offline - #14649

Merged
morluto merged 11 commits into
morluto:mainfrom
mvanhorn:fix/qualcomm-mbn-headers
Nov 26, 2026
Merged

morluto merged 11 commits into
morluto:mainfrom
mvanhorn:fix/qualcomm-mbn-headers

Conversation

@mvanhorn

@mvanhorn mvanhorn commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Android bootloader, TrustZone and modem partitions (sbl1.mbn, aboot.mbn, tz.mbn, modem.mbn) ship as Qualcomm MBN images. REA does not recognize them today: a legacy MBN has no magic number, so it comes back as an unsupported binary, and an ELF-wrapped modem image is read as plain ELF. An analyst cannot see the header fields, where the signature and certificate chain sit, or which hash in the hash segment belongs to which program header without a separate tool.

This adds an offline qualcomm-mbn artifact format. inspect-artifact and extract-artifact (and the MCP inspect_artifact / extract_artifact tools) decode the 40-byte version 3 and 80-byte version 5/6 headers, report the file range of the code, signature, certificate-chain and metadata regions, and for an ELF image that declares a Qualcomm hash segment, decode that segment's header and list the hash position for each program header. Signatures and certificates are located, not verified. No issue exists for this; it is a format-inspection addition within the contribution scope.

Problem and expected behavior

Use case: given sbl1.mbn or modem.mbn, see the image id, version, image source, sizes and pointers, the byte range of each region, and for the modem image the hash size, hash count and per-segment hash offsets. Then extract signature.bin or certificate-chain.bin for further analysis.

Observable outcome:

  • A legacy header is claimed only when the version word is 3, 5 or 6, image_size equals the sum of the region sizes, the signature and certificate-chain pointers follow the code region, and the whole code region lies inside the file. It runs after every signature-based format. A header whose sizes disagree is not claimed, and the file stays unsupported.
  • Root format_metadata has kind qualcomm-mbn, layout legacy or elf, the declared header words and header.ranges with the derived file offset and length of each region. Each region inside the file is a child occurrence (header.bin, code.bin or hash-table.bin, signature.bin, certificate-chain.bin, metadata0.bin, metadata1.bin).
  • A region that runs past the end of the file is not listed and not clamped. Its range is a named limitation and inventory coverage is unknown.
  • An ELF image whose program headers include one with p_flags bits 24-26 equal to 2 is inventoried and extracted as qualcomm-mbn. Binary target resolution still reads it as ELF. An ELF without a hash segment stays ELF.
  • ELF extended numbering (e_phnum 65535), a wrong program header entry size, or a table past the file end leaves the hash segment unknown and the file stays ELF. Further hash segments, a hash count that differs from the program header count, and a version 5/6 hash header cut short by the segment size or the file end are limitations with unknown coverage.

Change and scope

  • src/domain/qualcommMbnImage.ts owns parsing: legacy header recognition and plausibility checks, region ranges, ELF program header scan for the hash segment (reusing elfFixedHeader.ts, elfProgramHeader.ts and elfRawTableDeclarations.ts), hash size selection (SHA-256, SHA-384 or SHA-1 by code size and program header count), per-program-header hash offsets, and inventory coverage.
  • src/artifacts/QualcommMbnLocation.ts reads only the spans the domain owner asks for: the 80-byte prefix, then for ELF the program header table and the hash segment header. QualcommMbnArtifactReader.ts lists the stored regions and opens their bytes.
  • Wiring: recognition in artifactFormatRecognition.ts, registration in artifactFormats.ts, qualcomm-mbn metadata schema in formatMetadata/firmwareMetadata.ts, ELF-to-MBN promotion for inventory in inventory/classify.ts and extraction/ArtifactExtraction.ts, plus target format, artifact graph and inventory view entries.
  • Docs: new docs/guides/firmware/qualcomm-mbn-images.md, a section in docs/guides/firmware/firmware-analysis.md, CLI examples in docs/getting-started/cli.md, sidebar entry, docs index, and an MBN entry in the skill references under .agents/skills/reverse-engineer-anything/ and .cursor-plugin/.
  • Hardcoded values and why none is an REA_ setting:
    • segment_hashes lists at most 64 rows inline (QUALCOMM_MBN_INLINE_SEGMENT_HASH_LIMIT), with omitted_segment_hashes counting the rest. This is a fixed output ceiling, not an admission limit: every hash position is the hash-table offset plus index times hash size, and the whole table stays readable as the hash-table.bin region, so no data becomes unreachable.
    • 40 and 80 header bytes, hash segment type 2, hash sizes 20/32/48 and the e_phnum value 65535 are format constants, not budgets.
    • Plausibility bounds are the file itself: the code region must lie inside the file, the program header table is scanned only when it lies wholly inside the file, and region sums are checked against the 32-bit word width. No separate byte budget is added.

Not included: signature or certificate verification, hash recomputation, ELF64 extended numbering via section 0, and MBN formats beyond the legacy header and the ELF hash segment.

Contract and boundary impact

  • Semantic owner and earliest changed stage: qualcommMbnImage.ts produces the header, range and hash-segment facts; the reader, inventory classification, extraction and metadata schema consume them without reparsing raw bytes.
  • Contribution scope fit and linked maintainer decision: offline format inspection and ordinary extraction of a local file; not applicable (no target execution, injection, mutation, or protection recovery). Signature and certificate bytes are located, not verified or bypassed.
  • CLI and MCP/tool-catalog contract: no new tools or parameters. Existing artifact tools recognize one more format value, qualcomm-mbn.
  • Provider, bridge, target-format, or platform compatibility: new target format qualcomm-mbn. ELF target resolution is unchanged; an ELF image with a hash segment is read as qualcomm-mbn only for inventory and extraction.
  • Evidence, artifact, provenance, or reconstruction contract: new qualcomm-mbn format metadata shape with layout, header (including ranges) and elf; region children carry their source ranges and SHA-256.
  • Target selection, authority, upstream behavior, and cleanup details (if applicable): not applicable.
  • Generated metadata (docs/public/product-catalog.json), package, or installation impact: none committed; authored skill references updated under .agents/skills/reverse-engineer-anything/ and .cursor-plugin/.

Expected effects:

  • Read existing files or captured data.
  • Launch or attach to a process.
  • Inject instrumentation or run scripts/code.
  • Change process memory, registers, files, or configuration.
  • Contact network services.
  • Recover protected code or work around protections.
  • Documentation or usability only; no target interaction.
  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated: src/domain/qualcommMbnImage.test.ts (version 3 and version 6 legacy headers, sizes or pointers that disagree, truncated signature range, header-sized files without their code region, ELF with and without a hash segment), src/domain/qualcommMbnImage.boundaries.test.ts (e_phnum 65535, wrong entry size, table past the file end, several hash segments, hash count mismatch, version 6 hash header cut short by p_filesz or by the file end), src/artifacts/QualcommMbnArtifactReader.test.ts (inventory by content, region bytes, ranges past the file end, ELF MBN inventory and extraction while resolved as an ELF target, ELF without a hash segment staying ELF, inline hash row ceiling, refusal of non-MBN bytes), and src/application/binary/TargetFormatVocabularies.test.ts. Fixtures are built by src/domain/qualcommMbnImage.fixture.ts; no vendor bytes.
  • Base reproduction or other evidence: source-derived for the base revision, which has no qualcomm-mbn format value, so a legacy MBN is rejected as an unsupported binary and an ELF MBN is inventoried as ELF. The base revision was not run.
  • User-visible CLI/MCP output (if applicable): executed with the CLI compiled from this branch on files generated by the repo fixture helper.

rea inspect-artifact ./modem.mbn --json, an ELF32 image built by mbnElfWithHashSegment({ loads: 2 }) (848 bytes). Manifest root_format is qualcomm-mbn, 5 occurrences (., header.bin, hash-table.bin, signature.bin, certificate-chain.bin), coverage complete. Root format_metadata, trimmed:

{
  "kind": "qualcomm-mbn",
  "layout": "elf",
  "header": {
    "version": 3,
    "header_bytes": 40,
    "image_id": 0,
    "image_source": 3,
    "image_source_name": "emmc-user",
    "image_size": 480,
    "code_size": 128,
    "signature_size": 256,
    "certificate_chain_size": 96,
    "ranges": {
      "header": { "offset": 192, "length": 40 },
      "code": { "offset": 232, "length": 128 },
      "signature": { "offset": 360, "length": 256 },
      "certificate_chain": { "offset": 616, "length": 96 },
      "metadata0": null,
      "metadata1": null
    }
  },
  "elf": {
    "elf_class": 32,
    "byte_order": "little",
    "machine": 164,
    "program_header_count": 4,
    "hash_segment_program_header": 1,
    "hash_segment_offset": 192,
    "hash_segment_file_size": 520,
    "hash_size": 32,
    "hash_count": 4,
    "segment_hashes": [
      { "program_header": 0, "segment_type": 7, "file_offset": 0, "file_size": 0, "hash_offset": 232 },
      { "program_header": 1, "segment_type": 2, "file_offset": 192, "file_size": 520, "hash_offset": 264 },
      { "program_header": 2, "segment_type": 0, "file_offset": 720, "file_size": 64, "hash_offset": 296 },
      { "program_header": 3, "segment_type": 0, "file_offset": 784, "file_size": 64, "hash_offset": 328 }
    ],
    "omitted_segment_hashes": 0
  }
}

rea inspect-artifact ./sbl1.mbn --json, a raw version 3 header built by mbnLegacyImage (200-byte code, 256-byte signature, 96-byte certificate chain, 592 bytes total): root_format qualcomm-mbn, layout legacy, elf: null, header.ranges of header 0+40, code 40+200, signature 240+256, certificate_chain 496+96, coverage complete. A version 6 image with a 16-byte metadata0 region additionally reported an 80-byte header.bin and metadata0.bin.

rea extract-artifact ./sbl1.mbn --output-root "$PWD/ex-sbl1" --json wrote header.bin (40 bytes), code.bin (200), signature.bin (256) and certificate-chain.bin (96) with containment_verified: true. The same command on modem.mbn wrote header.bin (40), hash-table.bin (128), signature.bin (256) and certificate-chain.bin (96).

Limitation and refusal examples from the same run:

  • sbl1.mbn cut to its first 400 bytes: still qualcomm-mbn with the full declared header, but only header.bin and code.bin are listed, coverage is unknown, and the limitations include "signature range 240+256 exceeds the 400-byte file", "certificate chain range 496+96 exceeds the 400-byte file" and "image range 0+552 exceeds the 400-byte file". extract-artifact wrote only header.bin and code.bin.

  • sbl1.mbn with image_size overwritten to 9999: not claimed. inspect-artifact exits 1 with "code": "target_unavailable", "message": "Cannot open artifact: unsupported binary format.", "constraint": "file_format_rejected".

  • An ELF32 image with one PT_LOAD and no hash segment (mbnElfImage): root_format elf, one occurrence, coverage complete.

  • Remaining proof gaps: only generated images were used. Vendor sbl1, tz or modem images, ELF64 MBNs and big-endian ELF wrappers were not run through the CLI. The MCP adapter was not exercised directly; it shares the artifact readers with the CLI. The full npm run check suite and the acceptance and MCP boundary suites were not run.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Demo

Rendered walkthrough of the CLI inspect output above (simulated terminal, values taken from a real run on generated fixtures).

demo

rea-mbn-demo.mp4

Validation performed

  • npm run test:local -- src/domain/qualcommMbnImage.test.ts src/domain/qualcommMbnImage.boundaries.test.ts src/artifacts/QualcommMbnArtifactReader.test.ts src/application/binary/TargetFormatVocabularies.test.ts: 4 files, 81 tests passed
  • npm run check:fast: typecheck and lint passed (turbo cache hit for these inputs); architecture guards passed across 3136 source files
  • npm run docs:check: doc facts check passed; tests/conformance/readmeConsistency.test.ts 8 tests passed
  • npm run format:check: all matched files use the correct format (8915 files)
  • npm run verify:test-discovery: all 3919 test and spec files discovered exactly once
  • npm run compile, then node scripts/rea.mjs inspect-artifact <file> --json on six generated files and node scripts/rea.mjs extract-artifact <file> --output-root <dir> --json on three: results as shown above

Compatibility, safety, and release

  • Breaking changes or migration steps: none.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable; offline parsing only, run on macOS.
  • Package or release metadata impact: one new format value.
  • Security, privacy, process, or containment review: reads a local file only and extracts through the existing contained extraction path. Header words are untrusted: pointer and size sums are checked for overflow, regions outside the file are reported rather than read, and the program header table is read only when it lies inside the file. Reads are positional and limited to the 80-byte prefix, the program header table and the hash segment header; region bytes are read only when a region is opened.

AI was used for assistance.

Author readiness

Human owner: @mvanhorn

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Author checklist (completed by the PR author before requesting review)

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.
  • I ran every test, build, or measurement needed to validate this change, reported a browser, GPU, or environment gap only when that runtime or hardware is unavailable, and reported the checks I ran and their results without presenting inferred correctness or speedups as measured.
  • Comments explain constraints or non-obvious decisions; task history and conversations stay out of code and docs.
  • Documentation is concise and factual, without punchy slogans or promotional language.
  • This change fixes the root cause and follows the engineering rules in AGENTS.md.
  • Formatted input is interpreted once at the owning boundary, and consumers use the owner's parsed facts. See Interpret boundary facts once.
  • Combined failures preserve partial observations, remaining resources, and cleanup facts. See Own the lifecycle.
  • A hardcoded limit is an REA_ runtime setting with a default, floor, and maximum, unless a fixed ceiling has a stated reason. See Adding a runtime setting.

@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-26T03:52:40.021965Z
We couldn’t complete this review. Comment @codex review to try again.
3163b0c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@greptile-apps

greptile-apps Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The reviewed changes appear safe to merge, with the previous numbered finding fixed and no new blocking issue found.RetriggerView in GreptileConfidence Score: 5/5 Tier: plus

[Medium impact] The reviewed changes appear safe to merge, with the previous numbered finding fixed and no new blocking issue found.

Summary

Adds offline qualcomm-mbn inspection and extraction for legacy images and ELF hash segments.

  • Legacy Qualcomm MBN files now show their headers and stored regions.
  • ELF images with Qualcomm hash segments now show per-header hash positions.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  Input[Local image] --> Recognition[Content recognition]
  Recognition --> Legacy[Legacy MBN]
  Recognition --> ELF[ELF]
  Legacy --> Parser[MBN header and region facts]
  ELF --> Hash{Qualcomm hash segment?}
  Hash -->|Yes| Parser
  Hash -->|No| Plain[ELF inventory]
  Parser --> Output[MBN inventory and extraction]
  ELF --> Native[Native target remains ELF]
Loading

Reviews (10) · Last reviewed commit: "fix(firmware): stop mapping Qualcomm MBN..." · Reviewed by Greptile

Comment thread src/domain/qualcommMbnImage.ts Resolved
Comment thread src/domain/qualcommMbnImage.ts Resolved
Comment thread src/domain/qualcommMbnImage.ts Resolved
Comment thread src/domain/qualcommMbnImage.ts Resolved
@mvanhorn
mvanhorn force-pushed the fix/qualcomm-mbn-headers branch from 3163b0c to 29ef8b5 Compare November 26, 2026 06:26
@mvanhorn

Copy link
Copy Markdown
Contributor Author

Rebased onto main at 6ae722a and pushed as 29ef8b5 to pick up the updated trace test from #14647. The patch itself is unchanged. CI on the new head is still queued.

…offline

Artifact recognition now accepts a legacy Qualcomm MBN header only for
versions 3, 5, and 6, and only after every signature-based format, when
the image size matches the region sizes, the signature and
certificate-chain pointers follow the code, and the code region lies
inside the file. Inventory and extraction list the header, code,
signature, certificate-chain, and version 5/6 metadata ranges; a range
past the end of the file is reported as a limitation with unknown
coverage and is left unlisted. An ELF image whose program-header flags
mark a Qualcomm hash segment (bits 24-26 equal 2) is inventoried as
qualcomm-mbn, with the hash-segment header, hash size, hash count, and
the first 64 segment-hash rows, while binary target resolution keeps the
ELF classification and extraction still returns those regions for an ELF
input. Signatures and certificates are reported by location and size,
and hash bytes are left as stored.
@mvanhorn
mvanhorn force-pushed the fix/qualcomm-mbn-headers branch from 29ef8b5 to 472c535 Compare November 26, 2026 08:58
Comment thread src/domain/artifactFormatRecognition.ts Resolved
@morluto morluto added area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior enhancement New feature or request labels Nov 26, 2026
Comment thread src/domain/qualcommMbnImage.ts Resolved
Comment thread src/domain/qualcommMbnImage.ts Resolved
Comment thread src/domain/qualcommMbnImage.ts Resolved
@morluto
morluto enabled auto-merge (squash) November 26, 2026 18:13
…ders

# Conflicts:
#	.agents/skills/reverse-engineer-anything/references/native-and-artifacts.md
#	.cursor-plugin/skills/reverse-engineer-anything/references/native-and-artifacts.md
#	docs/getting-started/cli.md
#	src/domain/formatMetadata/firmwareMetadata.ts
# Conflicts:
#	.agents/skills/reverse-engineer-anything/references/native-and-artifacts.md
#	.cursor-plugin/skills/reverse-engineer-anything/references/native-and-artifacts.md
#	docs/getting-started/cli.md
#	docs/guides/firmware/firmware-analysis.md
#	src/domain/analysisView/artifactInventoryView.ts
#	src/domain/artifactFormatRecognition.ts
Comment thread src/domain/qualcommMbnImage.ts Resolved
@morluto
morluto disabled auto-merge November 26, 2026 21:45
@morluto
morluto enabled auto-merge (squash) November 26, 2026 21:52
@morluto
morluto merged commit f78470e into morluto:main Nov 26, 2026
54 checks passed
morluto added a commit to mvanhorn/rea that referenced this pull request Nov 26, 2026
Preserve both origin/main (morluto#12469 VSIX, morluto#13245 PE overlay, morluto#14649 Qualcomm MBN) and YARA entries across conflicted skill docs and test-projects, and include qualcomm-mbn in PE_OVERLAY_LEADING_FORMATS to resolve the typecheck regression between morluto#13245 and morluto#14649.
morluto added a commit that referenced this pull request Nov 26, 2026
The PE overlay (#13245) and Qualcomm MBN recognition (#14649) merged
concurrently. Bounded artifact recognition can now return qualcomm-mbn,
which the overlay's leading-format enum did not list, so main failed to
typecheck and build.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants