Skip to content

fix: correct NAT journal handling, stop silent corruption, preserve safe metadata - #1

Open
palmarci wants to merge 1 commit into
Sushrut1101:mainfrom
palmarci:fix-nat-journal-and-metadata
Open

palmarci wants to merge 1 commit into
Sushrut1101:mainfrom
palmarci:fix-nat-journal-and-metadata

Conversation

@palmarci

Copy link
Copy Markdown

Summary

Tested this tool against a real 700MB F2FS image built with mkfs.f2fs and
populated with real Android firmware content (16045 files, 728 dirs, 2
symlinks). Found and fixed three issues, verified with a full sha256
comparison against the source content after each fix.

Changes

  • Silent data corruption on ~0.2% of files (33/16045 in testing):
    readNode fell back through several NAT lookup attempts and, if none of
    them found a block whose footer actually matched the requested nid, it
    returned that wrong block's bytes anyway instead of an error. Since the
    wrong block was typically an unwritten/zeroed one, this silently produced
    a 0-byte file that extract/extractdir reported as a normal success,
    no warning, no error. readNode now returns an error in that case, and
    the existing callers in extractDir already handle a readFile/getInode
    error by skipping the file with a Warning: line, so this alone turns
    silent corruption into an honest, visible skip.

  • Root cause of those 33 files, actually fixed (not just skipped): F2FS
    batches recent NAT (node address table) updates into a journal embedded in
    the checkpoint, and only periodically flushes them back into the main
    on-disk NAT table this reader parses. Right after a burst of file creates
    (a cp -a of the whole test tree) followed by sync+umount, the most
    recently created inodes' real locations were still only in that journal,
    not yet reflected in the raw NAT table. Verified against the real
    fsck.f2fs/dump.f2fs (f2fs-tools), which resolves these same inodes
    correctly, confirming the raw table alone is not enough. Added
    journal.go: locates the current checkpoint pack (of the two redundant,
    self-mirrored copies, picking by version and validating header against
    footer), and when it uses the compact summary format, parses the NAT
    journal entries out of it. readNode now checks this journal first and
    falls back to the existing raw-table path when a nid isn't in it or the
    non-compact (per-log) summary format is in use (not handled here, that
    format spreads the journal across up to 3 separate blocks instead of one
    merged block; left as a graceful fallback to the existing behavior rather
    than guessed at). With this, all 16045/16045 files in the test image
    extract correctly, sha256-verified against the source.

  • Extracted output never preserved permissions or timestamps: every file
    was written with a hardcoded 0644 and no os.Chtimes call anywhere, so
    extracted output always showed the extraction time and a single fixed
    mode regardless of what the image actually recorded. Added applyMetadata
    in extract.go, called after every file/symlink/directory is written.
    Deliberately not a verbatim copy of the original mode: setuid/setgid/
    sticky bits are stripped (replaying a setuid bit from firmware onto a
    file on the extracting machine is worth stripping regardless of
    privilege level), and read access (and, for a directory, search) for
    everyone is guaranteed on top of whatever the original bits were, so a
    later pipeline stage reading the extracted tree under a different uid
    than whoever ran the extractor can still open everything. A firmware
    image can legitimately contain files with restrictive owner-only modes
    (0600, even 0000) that have nothing to do with who should be able to
    read the extracted copy. This mirrors common practice for ext-family
    image extraction, e.g. e2fsprogs' debugfs dump/rdump: output is
    written at the invoking process's default umask rather than replaying
    the original device's mode bits verbatim.

Test plan

Manual, against a real mkfs.f2fs-built 700MB image populated with real
Android firmware content (not synthetic magic-bytes-only fixtures):

  • Full sha256 comparison, extracted output vs. source: 16045/16045
    files correct, 0 missing, 0 wrong content, both symlinks correct
  • Cross-checked the pre-journal-fix failures against real
    fsck.f2fs/dump.f2fs to confirm the raw NAT table genuinely
    doesn't have the right answer for those nids, not a bug in the
    existing lookup logic
  • Mtimes spot-checked file-by-file against the source, exact match,
    including on the previously-corrupted 2.1MB classes.dex.
    Permissions verified world-readable on every extracted file/dir
    regardless of original mode (e.g. a source file that was 0600
    comes out 0644), and setuid/setgid/sticky bits confirmed stripped
  • Re-confirmed still fully rootless after all changes:
    --user 1000:1000 --cap-drop ALL --security-opt no-new-privileges --read-only, no /dev access, no loop device used
    (losetup -a empty throughout)
  • ~1.3s for the full 700MB/16045-file extraction, no meaningful
    regression from before these changes

…le metadata

Root cause: F2FS batches recent NAT updates into a journal in the
checkpoint before flushing them to the main table, so the most
recently created inodes were unreadable from the raw table alone right
after a big write. Added journal.go to parse that journal (verified
against real fsck.f2fs/dump.f2fs output and the kernel's
read_compacted_summaries), so those files are now recovered, not just
skipped.

Extracted output also never carried over permissions or timestamps,
always 0644 with the extraction time. applyMetadata now restores the
original mtime and a safe derivative of the mode: setuid/setgid/sticky
stripped, read access guaranteed for everyone regardless of the
original bits (matches how debugfs dump/rdump handles ext-family
images elsewhere).

Tested against a real mkfs.f2fs image with 16045 files of real
firmware content: 16045/16045 correct by sha256,
correct, still fully rootless.
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.

1 participant