Files
reverse-engineering-anything/scripts/lib
2SinceJulyandmorluto 742cdf0428 feat(artifacts): inventory systemd journal headers and object table offline (#10151)
* feat(artifacts): inventory systemd journal headers and object table offline

Journals copied from a host or carved from a disk image had no reader, so an
analyst needed journalctl and a live host to see what a file declared. Recognize
the LPKSHHRH header by bytes and report it as its own artifact format.

Parse the fixed 272-byte header: flag bits and their names, state, the four
128-bit ids, header and arena sizes, hash-table extents, and the object, entry,
data, field, tag and entry-array counts. Anchor the object area on header_size,
requiring an aligned offset inside the file, then walk the object chain front to
back by each header's declared extent. Report totals by object type, the
valid/truncated/unknown-type counts, a bounded window of 32 object headers with
source offsets, and the trailing bytes the chain could not claim.

Payloads stay undecoded: fields, entries, entry arrays, hash tables and
compressed objects are reported as limitations rather than guessed, and the
whole file is bounded at a fixed 4 GiB ceiling.

* style(artifacts): format the systemd journal reader and its fixtures

`format:check` fails on these three files, which breaks the Static checks
lane and, through it, the CI required aggregate. `oxfmt` only reflows
lines; no behaviour changes.

* fix(artifacts): read journal objects the way systemd lays them out

The object walk that shipped with the reader did not match the on-disk
format, so a valid journal could be reported as truncated and an
incompatible one could produce plausible but wrong evidence.

- Advance to `align8(offset + size)` and validate the raw extent. systemd
  keeps a declared size unpadded and aligns only the next object's start,
  so a size that is not a multiple of eight is valid, not truncated.
- Stop at `tail_object_offset` instead of at end of file. Bytes after the
  declared tail are the arena's preallocated capacity; walking them read a
  zero header as an extra unused object and called capacity corruption.
- Stream the object chain instead of buffering the file. Payload bytes are
  skipped as chunks arrive, so a journal near the 4 GiB ceiling inventories
  in chunk-sized memory and cancellation between chunks is never queued
  behind a whole-file parse.
- Read `tail_entry_array_offset` and `tail_entry_array_n_entries` with
  `getUint32`. They are adjacent 32-bit members added in revision 252;
  reading each as 64 bits folded the following field into both values.
- Accept the 208-byte header that predates `n_data`, and read every later
  field only when the declared `header_size` covers it. Older acquired
  journals stay inspectable instead of classifying as unknown.
- Refuse to describe objects when `incompatible_flags` sets a bit outside
  the known set. Such bits change the parser's assumptions, and a journal
  from a newer systemd would otherwise yield invalid object evidence.
- Record fields whose declaration exceeds 2^53-1 in `overflowed_fields`,
  so distinct source values no longer collapse onto the same ceiling.
- Report skipped compressed payloads only when a walked object actually
  carries a compression flag, not merely because the header advertises a
  compression algorithm.

Regression tests cover the unaligned size, the declared tail with arena
capacity after it, the 208-byte header, the compact 32-bit tail-array
fields, the 2^53-1 ceiling, unknown incompatible flags, and the
compression message. The decoder was also checked against every chunk
boundary from one byte to the whole file: counts and offsets are identical.

* style(artifacts): apply repository formatting

* chore(systemd): merge main and read the journal through BinaryReader

main's guards reject new raw DataView reads, so every header, object and
state field now goes through BinaryReader, which bounds each read.

Two offsets are write cursors rather than resource allowances, and each
carries the not-an-allowance reason the budget rule asks for.

* chore(systemd): apply oxfmt to the journal header helper

format:check runs oxfmt, and the BinaryReader rewrite left one call wrapped across three lines that oxfmt wants on one. No behavior change; this is what turned the Static checks job red on the previous run.

* chore(systemd): record the open_binary output schema summary

The journal format adds artifact format metadata, which changes the open_binary output schema digest. Build package runs verify:advertised-schemas and rejects the stale summary, so commit the regenerated digest. Only the output compact hash moves; input schemas and patterns are unchanged.

* fix(contracts): recognize damaged systemd journals by signature

hasSystemdJournalHeader also required a usable header_size, so a journal with
a short or unaligned header never reached SystemdJournalArtifactReader: normal
inventory classified it as a plain file and dropped the header facts and the
object-walk limitation the reader already reports. The malformed-header test
only passed because it constructed the reader directly.

Recognition is now the signature alone. Deciding whether header_size anchors
the object area belongs to the owning reader, which keeps whatever it could
read and records why traversal stopped.

* fix(journal): separate undersized objects from truncation and bound the tail

- An object declaring a size below its own 16-byte header is a contradictory
  declaration, not a file cut short: it is counted separately so the analyst
  is not told the writer was interrupted.
- An object size above 2^53-1 now carries `size_overflowed` on its header
  record, matching how the header fields already report the ceiling.
- Object extents are validated against the declared arena, not only the
  physical file, so a short arena is reported instead of accepted.
- `tail_object_offset = 0` with `n_objects = 0` is an empty chain rather
  than an absent boundary; a declared tail the walk never reaches is
  reported as an incomplete object area.
- `tail_entry_boot_id` is null unless the compatible `tail-entry-boot-id`
  flag is set, so the legacy boot id at the same offset is not published
  as a tail-entry identity.
- A header the probe could not anchor reports `unread_fields`, so its
  zeroed numeric declarations are not read as observed values.

* fix(journal): keep journal occurrences out of the executable role

A recognized .journal with no special path role fell through to the default
executable role, so every ordinary journal was inventoried as a program.
The bytes are data, not code, so the format now keeps the resource role the
same path would carry without a recognized content format.

* fix(journal): report a short signed journal as truncated input

The reader mapped every format failure to `malformed`, so a file that
carries the signature and stops before the header shape was complete was
reported as damaged structure rather than missing bytes. The signature is
now read before the decoder runs, and a signed short file carries a
`truncatedInput` cause; unsigned bytes keep `malformedInput`.

* fix(journal): keep the legacy boot id and read flags once

The 56-71 id has two readings selected by the compatible `tail-entry-boot-id`
bit; nulling it when the bit is clear dropped a readable header value. Both
names are now reported with exactly one non-null, and `readIdFacts` takes
the compatible flags the caller already read instead of a second u32
reader.

* Merge origin/main into feat/10123-systemd-journal

Import systemdJournalMetadataSchema from androidAndSystemMetadata directly instead
of the artifactFormatMetadata barrel. main moved format metadata into per-group
modules; the barrel re-exports platform parsers that import back through
resources, and the cycle left the schema undefined at module init, failing every
SystemdJournalArtifactReader test.

* test(filesystem): align stable artifact stream failures with split error reasons

Upstream #14225 split reader path refusals from malformed target bytes and moved two stable-artifact contract breaks off ArtifactReaderFailure("format"): an invalid chunk size is now an internal invariant violation, and a descriptor returning an impossible byte count is an io failure. The suite still asserted reason "format" for both, so shard 9 and the required CI lane went red on every branch that re-merged main.

Update the assertions to the reasons the producers now raise so the boundary suite matches the intended contract.

* test(filesystem): assert path refusal for a directory entry in the reader suite

DirectoryArtifactReader has refused non-file entries as reason "path" since the reader moved into src/resources (#11520), but the boundary test still expected "format", so the re-merged branch fails shard 4 with an ArtifactReaderFailure whose reason is path.

Assert the reason the reader raises.

* fix(artifacts): report unknown journal layouts as unsupported, not truncated

A complete journal whose header sets an incompatible flag bit this reader does not know was refused through the signature-based path, which wraps every decode refusal in truncatedInput. Callers therefore told the analyst the artifact was invalid and to obtain a fresh copy, although no bytes are missing and a fresh copy cannot help.

Carry an unsupported tag from the journal decoder through the artifact boundary so truncation stays reserved for genuinely short structures, and the public reason selects the compatible-decoder recovery.

* fix(pe): accept systemd-journal as a PE overlay leading format

Merging main brought the systemd-journal recognizer into the shared format
union while this branch kept its own overlay list, so
recognizeLeadingFormat no longer assigned to PeOverlayLeadingFormat and
typecheck failed. Recognizers can name systemd-journal, so the overlay
leading-format list has to include it.

* fix(systemd-journal): bound the scan by its window, not a file-size ceiling

- Drop the 4 GiB whole-file ceiling. It was justified by ENTRY_SIZE_MAX,
  which bounds one entry, while the scanner streams and retains one chunk
  plus a 32-header window whatever the file size.
- Make the object-header window selectable: createJournalDecoder and the
  reader accept objectHeaderStart, so next_object_header_index can be
  followed to later headers.
- Treat a zero arena_size as an empty arena rather than an absent one, and
  report declared arena bytes past the end of the file even when the
  readable objects and counts agree.
- Re-verify the source before reporting a decoder failure, so a file that
  changes mid-read reports an identity failure instead of truncation.
- Read the state byte through one lookup, so a header that cannot anchor
  its objects keeps its observed state; name the tail-entry-boot-id flag.

* feat(artifacts): page format record headers past the inline window

Inventory keeps a bounded window of per-record headers, and nothing could
follow the systemd journal's next_object_header_index over MCP or CLI.
inspect_analysis_view gains a generic `format_record_headers` inventory
collection (#14919) that uses the existing offset and limit.

- Each page re-reads the root file through its format's artifact reader,
  with the window started at `offset`, after binding the file to the parent
  inventory's root digest. A changed file fails with
  artifact_integrity_mismatch (container-identity) instead of returning
  shifted rows.
- Memory is one read chunk plus one window. The read is a cancellable
  streaming scan, and the reader is released through the view's resource
  scope.
- Formats plug in through two single lists: the domain's record total and
  the artifacts layer's window reader. The systemd journal is wired; NTFS
  $MFT is left to #14919 because its streaming decoder has no start index.
- The journal metadata drops object_header_start_index and
  next_object_header_index, which nothing could follow; the page's
  coverage.next_offset is the one continuation.
- The journal test builder moves into a shared fixture for reader and
  boundary tests.

Refs #14919

* fix(artifacts): page record headers only from a caller-named file

- Opening a file must not be selected by Evidence: subject.local_path is
  display metadata and can change without changing the Evidence ID. The
  format_record_headers collection now requires an explicit absolute
  `path`, which the CLI resolves like other caller paths. The root digest
  still binds that file to the inventory.
- Keep a completed page when the reader fails to close. The view Evidence
  is attached to the cleanup failure as its partial observation, and the
  reader stays retained for retry.
- Keep a completed page when cancellation arrives after it was read.

* fix(artifacts): scope record-page file selection and cleanup to its owners

- Name the page's file selector artifact_path. The CLI's caller-path rewrite
  of view.path also rewrote other views' semantic selectors, such as a
  devicetree node path, into host paths on Windows; artifact_path exists
  only on format_record_headers pages.
- Run each page's read and release through the supplied resource scope, so
  a reader an earlier page failed to close is retried first and a new page
  is refused while that cleanup stays unresolved.

* fix(views): own the record-page row type beside the view parent

The parent module imported the inventory projector for the page row type,
which closed an import cycle the analysis-view cycle check forbids. The type
now lives in analysisViewParent.ts and its consumers import it from there.
The CLI JSON-input schema test now expects view.artifact_path among the
caller paths inspect-analysis-view resolves.

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-27 15:46:45 -05:00
..