Repository navigation
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Tested this tool against a real 700MB F2FS image built with
mkfs.f2fsandpopulated 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):
readNodefell back through several NAT lookup attempts and, if none ofthem 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/extractdirreported as a normal success,no warning, no error.
readNodenow returns an error in that case, andthe existing callers in
extractDiralready handle areadFile/getInodeerror by skipping the file with a
Warning:line, so this alone turnssilent 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 -aof the whole test tree) followed bysync+umount, the mostrecently 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 inodescorrectly, 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.
readNodenow checks this journal first andfalls 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
0644and noos.Chtimescall anywhere, soextracted output always showed the extraction time and a single fixed
mode regardless of what the image actually recorded. Added
applyMetadatain
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, even0000) that have nothing to do with who should be able toread the extracted copy. This mirrors common practice for ext-family
image extraction, e.g. e2fsprogs'
debugfs dump/rdump: output iswritten 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 realAndroid firmware content (not synthetic magic-bytes-only fixtures):
files correct, 0 missing, 0 wrong content, both symlinks correct
fsck.f2fs/dump.f2fsto confirm the raw NAT table genuinelydoesn't have the right answer for those nids, not a bug in the
existing lookup logic
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
0600comes out
0644), and setuid/setgid/sticky bits confirmed stripped--user 1000:1000 --cap-drop ALL --security-opt no-new-privileges --read-only, no/devaccess, no loop device used(
losetup -aempty throughout)regression from before these changes