Repository navigation
feat(apple): inspect Image4 (IMG4, IM4P, IM4M) firmware containers offline - #14907
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
[Medium impact] The PR appears safe to merge based on the reviewed code. SummaryAdds offline Image4 recognition, inspection, and extraction through the existing artifact workflows.
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart LR
F["Local firmware file"] --> R["Read DER magic"]
R --> P["inspectImage4"]
P --> M["Metadata and limitations"]
P --> E["Stored entry ranges"]
E --> O{"Encrypted payload?"}
O -->|Yes| X["Refuse open"]
O -->|No| S["Return stored bytes"]
Reviews (11) · Last reviewed commit: "Merge origin/main into fix/apple-image4-..." · Reviewed by Greptile |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 768ab1c7cf
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
Thanks for this. I addressed the review threads with two commits on top of yours. Your history isn't rewritten.
DER handling. X.690 DER forbids constructed string encodings and non-minimal lengths. This reader keeps your tolerant approach: such values are reported as limitations, coverage becomes Sources: DeepWiki answers for Every review thread has a reply with its regression, and each new test fails on |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7fffb03ecf
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7fffb03ecf
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
I did a brittleness pass on top of my earlier two commits and pushed it as Fixed
Left as is, on purpose
Sources
CI: the only real failure on |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 323b3c1ec6
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…fline Image4 files are recognized from a DER SEQUENCE whose first element is the IA5String IMG4, IM4P, or IM4M, independent of the filename, and are inventoried as apple-img4. The stored payload octet string is exposed as payload, the embedded IM4M sequence as manifest.der, and the IM4R sequence as restore-info.der. Keybags contribute a count and key types, with initialization vectors and keys left out of metadata; an encrypted payload is refused on open; signatures and certificates are reported by size; LZFSE and complzss payloads stay compressed; IM4R property names are listed and their values stay redacted. Four fixed allowances refuse excessive DER depth, element count, property count, and string length, and inventory coverage is unknown whenever a limitation is recorded.
Compare universal tag numbers instead of identifier octets, so constructed OCTET STRING and string values are recognized and reported. Read IM4P fields by position so an unsupported payload keeps its slot and a constructed keybag still marks the payload encrypted. Walk every constructed value once, caching the children of header-walked values so each element is charged once, and check large values a consumer does not decode. Report unknown restore members, extra property and object fields and a mistyped certificate chain. Decode UTF8String as UTF-8, accept DER sign padding on unsigned 32-bit integers, and recognize the magic with the shared BER header decoder.
…nts null Follow img4tool and img4lib: after the payload, an optional keybag OCTET STRING, then an optional compression SEQUENCE, then PAYP in [0]; IMG4 holds IM4P, [0] IM4M and [1] IM4R in that order. A value outside its slot is reported as unknown and never takes another field's role, and an EXPLICIT wrapper holds exactly one value. An absent or undecodable description is null rather than empty, and an undecoded keybag reports a null count and types instead of zero; a decoded keybag counts every entry.
An encrypted payload without a compression field, or a declared algorithm other than LZFSE with no recognizable magic, now reports compression "unknown" instead of "none". Make ia5Text the single owner of the retained-text allowance so a string too large to tree-parse is refused rather than reported absent, keep the canonical BOOLEAN check in the structural pass only, check the keybag's inner SEQUENCE through the shared decoder instead of its first octet, note a manifest version other than 0, and name the spec constants after their sources. Add a hand-encoded IM4P golden that does not use the fixture builders.
323b3c1 to
b4db59c
Compare
A value whose end cannot be found keeps its identifier, so a stopped keybag still marks the payload encrypted and a stopped payload keeps its slot; the values after it become a limitation. The IMG4 payload slot is consumed whatever it holds, so an unsupported value does not hide the manifest and restore slots. A property body that is not a SET or SEQUENCE is reported instead of read as empty, and an entry whose name cannot be read is one unknown entry whose value is never walked for more names, so restore values stay redacted. Load a value only when it may be decoded and fits an 8 MiB retained budget; larger primitives and values past the budget are walked by header.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f41fe00c6e
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
A PRIVATE-tagged restore value is one property: only its name is reported, and its value is never searched for names, even when the name is unreadable or is IM4R. Only the IM4R property SET is walked as a container. Small primitives are always loaded, so names and scalars stay readable after the retained budget is used. A declared uncompressed size that is negative or not an exact integer is reported as a limitation.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 53d324898a
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…TE manifest entries opaque Reserve retained bytes only for a value that will be tree-parsed, so a ranged wrapper around a large payload no longer uses budget a later small indefinite value needs. A PRIVATE manifest entry must name itself; when its name cannot be read it is reported and its value is never interpreted as manifest facts.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f9ed1e0a6b
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
# Conflicts: # docs/reference/supported-targets.md # scripts/ci/scopes.mjs # scripts/verify/fixed-ceiling-registry.json # tests/boundary/providers/native/artifactProvider.standaloneFormats.test.ts
A MANB, MANP or IM4R entry that holds only its name is a missing body, not a confirmed-empty one, so it is reported as a limitation while sibling facts are kept.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 555fbd8419
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
# Conflicts: # docs/reference/supported-targets.md
A context-, application- or other-tagged manifest value that begins with a name is unsupported structure, reported as an unknown tag rather than read as a manifest entry.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: df15cc6686
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
One slot assigner owns the IMG4 layout: a context-tagged value fills the slot with its tag number, skipping earlier optional slots, and any other value occupies the next slot even when it cannot fill it. An unsupported manifest value therefore no longer hides a following [1] IM4R.
Keep the Image4 and Mega Drive registrations that both sides added. Co-authored-by: morluto <[email protected]>
|
@greptileai review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8baff0f7bb
ℹ️ 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".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| compression = await asCompression(compressionSlot, walk, depth + 1, notes); | ||
| if (compression === undefined) unknownTag(compressionSlot, notes); |
There was a problem hiding this comment.
Keep undecodable compression fields distinct from absent fields
For an IM4P without a keybag or recognized payload magic, a compression slot such as SEQUENCE { INTEGER 1 } makes asCompression return undefined, conflating an unreadable compression declaration with an omitted field. Probes reproduce this in both parsing strategies: buildPayload publishes compression: "none", which the reader also maps to compressedSize: null, despite the declaration indicating LZFSE. Preserve a present-but-undecoded state, or retain the readable algorithm with an unknown size, rather than advertising these stored bytes as uncompressed.
AGENTS.md reference: AGENTS.md:L19-L19
Useful? React with 👍 / 👎.
|
@morluto thanks for merging this, and for the follow-up commits that tightened the field model. Really appreciate |
Summary
Apple ships boot chain components, kernel caches and coprocessor firmware wrapped in Image4: a DER
IM4Ppayload, anIM4Msigned manifest, and anIMG4that bundles both with optionalIM4Rrestore info. REA does not recognize these files today, soinspect-artifactreports a.im4por.img4as unknown bytes and there is no offline way to see the payload type, whether it is encrypted, or what the manifest declares.This adds an
apple-img4artifact format.inspect-artifactandextract-artifactrecognize the container from its content (a DER SEQUENCE whose first element is the IA5StringIMG4,IM4PorIM4M), report its structure, and expose the payload, manifest and restore info as entries. Nothing is decrypted, decompressed, executed or signature-checked. Discussed in #14934. It is a read-only format-inspection addition within the supported contribution scope.Problem and expected behavior
Use case: given
kernelcache.im4por a personalized.img4from a restore bundle, find out what the payload is, whether it is encrypted or compressed, and which chip, board and objects the manifest names, then pull the stored payload out for the Mach-O tools.Observable outcome:
format_metadatahas kindapple-img4andmagic(IMG4,IM4PorIM4M).payloadreportstype(such askrnl,ibot),description, the storedoffsetandlength,compression(none, an LZFSE block magic, orcomplzss),uncompressed_size,payload_magic,encrypted,keybag_countandkeybag_types. Keybag IVs and keys are never copied into metadata, limitations or errors.manifestreportsversion, body properties (CHIP,BORD,ECID,CPRO,SDOMand others), per-object properties withDGSTas hex,signature_sizeandcertificate_chain_size.restore_infolistsproperties_presentwithvalues: "redacted".payload,manifest.der(the IM4M SEQUENCE without the[0]wrapper) andrestore-info.der. An encrypted payload is listed andopenrefuses it. Compressed payloads are returned as stored withcompressedSizeequal to the stored length.unknownwhenever one is present. A truncated value fails the read.Change and scope
src/domain/apple/image4.ts: the Image4 parser. It walks DER by header, loads nested values up to 256 KiB and walks wider ones by header so a large payload is ranged, not read. It decodes IM4P, IM4M and IM4R, keybags, compression and the four allowances.src/artifacts/Image4ArtifactReader.ts: the positional reader that serves the three entries and refuses an encrypted payload on open.src/domain/asn1Ber.ts: the shared BER header decoding moves into an exportedreadAsn1BerHeader, which also decodes high-tag-number form (needed for the PRIVATE-classMANB,MANPandBNCNtags) with a 5-octet cap and rejects non-minimal forms.Asn1BerFailuregains atruncatedflag set by every failure that means the input ended early. Image4 uses that flag to fail truncated reads instead of matching message text.artifactFormats.ts,artifactFormatRecognition.ts,artifactFormatMetadata.ts,firmwareMetadata.ts,targetFormats.ts,artifactGraph.ts,artifactInventoryView.ts,ArtifactReader.tsand the PE overlay format list.docs/guides/apple/image4-containers.md, CLI examples indocs/getting-started/cli.md,docs/reference/limits-and-recovery.md,docs/reference/supported-targets.md, the docs index and sidebar, and the skill references under.agents/skills/reverse-engineer-anything/and.cursor-plugin/. The four allowances are registered inscripts/verify/fixed-ceiling-registry.json, andscripts/ci/scopes.mjsmaps the new files.Not included: decryption, LZFSE or LZSS expansion, signature or certificate validation, and decoding IM4R values.
Contract and boundary impact
image4.tsinterprets the DER once at recognition; the reader, metadata and inventory view consume its parsed facts.inspect-artifactandextract-artifactgain one format,apple-img4.apple-img4metadata and entry metadata (decoded: false,unpacked: false).docs/public/product-catalog.json), package, or installation impact (CI generates and validates ignored catalogs andskills/; edit authored instructions in.agents/skills/reverse-engineer-anything/and never commit build outputs): none committed; authored skill references updated under.agents/skills/reverse-engineer-anything/and.cursor-plugin/.Expected effects (check known effects; use documentation-only on its own).
Unchecked items do not establish absence; leave uncertainties visible.
Evidence and regression coverage
src/domain/apple/image4.test.ts(21 tests: IM4P, IM4M and IMG4 structure, keybags with and without a decodable layout, compression agreement and disagreement,complzssheaders, oversize octet strings and integers, tag-name mismatches, indefinite and non-minimal lengths, trailing bytes, truncation, the four allowances),src/artifacts/Image4ArtifactReader.test.tsandsrc/artifacts/Image4ArtifactReader.boundaries.test.ts(the three entries per container type, encrypted refusal on open, stored LZFSE and LZVN bytes returned undecoded, truncation, a malformed high tag, each of the four allowances, and indefinite values that are not bounded),src/domain/asn1Ber.test.ts(5 tests: low and high tags, header lengths, malformed high tags, thetruncatedflag), plus entries inartifactFormatRecognition.test.ts,TargetFormatVocabularies.test.tsandtests/boundary/providers/native/artifactProvider.standaloneFormats.test.ts. Fixtures are built insrc/domain/apple/image4.fixture.ts.inspect-artifactdoes not recognize these files. The CLI output below is executed against this branch.rea inspect-artifact sample.img4 --jsonon an IMG4 holding an LZFSEkrnlIM4P, an IM4M and an IM4R:rea extract-artifact sample.img4 --jsonwrotemanifest.der(247 bytes),payload(32) andrestore-info.der(51) withcontainment_verified: trueand coverage complete.rea inspect-artifact encrypted.im4p --jsonon anibotIM4P with two keybags reportedencrypted: true,keybag_count: 2,keybag_types: [1, 2]. The JSON contains none of the fixture IV or key bytes.rea extract-artifact encrypted.im4p --jsonrefused withunsupported_target: "Archive entry payload is encrypted; REA does not decrypt archive entries, so the archive cannot be extracted completely".rea inspect-artifact deep.im4m --jsonon an IM4M with 40 nested SEQUENCEs refused with "Image4 DER nesting exceeds 32 levels. This release has no runtime override." and{ allowance: "The Image4 DER depth allowance", limit: 32, unit: "levels" }.complzsshandling is shown on synthetic headers.Not a boundary fix. The new reader's boundary tests cover truncation, a malformed high tag, the four allowances, and indefinite or trailing data kept as limitations.
For evidence-bearing changes:
Validation performed
npx vitest runon the seven test files above: 7 files, 143 tests passed.npm run lint: exit 0, including the fixed-ceilings verification.node scripts/rea.mjs inspect-artifactandextract-artifacton the fixture-built files: output as shown above.Compatibility, safety, and release
readAsn1BerHeaderand thetruncatedflag are additions. In the shared reader, a high-tag-number form is now decoded instead of refused; existing callers match on low tags and are unaffected.AI was used for assistance.
Author readiness
Human owner: @mvanhorn
Author checklist (completed by the PR author before requesting review)
type(scope): outcome.REA_runtime setting with a default, floor, and maximum, unless a fixed ceiling has a stated reason. See Adding a runtime setting.Demo