f2fs: A possible bug on roll-forward recovery (does not verify inode checksum)
Hi,
Disclosure first: this report was prepared with the help of an AI coding assistant (Claude). It was used to analyze the code, write the reproducer and draft this text. I have reproduced the problem myself, reviewed everything below and take responsibility for it.
I would like to report that roll-forward recovery in f2fs does not verify the inode checksum.
With the inode_checksum feature enabled, a corrupted fsync'd inode block is recovered silently after an unclean shutdown and then written back with a fresh, valid checksum. The same corruption is rejected with -EFSBADCRC on the regular node read path.
I am not sure whether this is considered as a bug or missing hardening, so I am reporting it with a suggestion rather than sending a patch.
Affected versions
-----------------
I tested this on the following versions: f2fs dev branch b61a029304bb ("f2fs: don't BUG_ON a missing orphan inode at mount") with 7.4.0-rc1, x86_64 KVM guest, KASAN enabled, and f2fs-tools 1.16.0.
Problem description
-------------------find_fsync_dnodes() and recover_data() read node blocks with f2fs_get_tmp_cache() and only check the footer via is_recoverable_dnode(). recover_inode() and do_recover_data() then copy i_mode, i_uid/i_gid, i_size, i_flags, timestamps and the block addresses from that block. f2fs_inode_chksum_verify() is only called from the node read path in node.c, so nothing verifies the inode body during roll-forward.
We need the following step to reach the situation:
1. mkfs.f2fs -O extra_attr,inode_checksum
2. write + fsync a file (the inode block goes to the warm node log)
3. that inode block gets corrupted on the device before the next checkpoint (for example paired-page corruption or an incomplete program on power loss, which is also when roll-forward runs)
4. unclean shutdown, mount
The corrupted fields are applied without any error or message, the mount succeeds, and fsck.f2fs reports nothing afterwards. What we observed was a wrong i_size and files returning wrong content (corrupted block address). Since the recovered inode is rewritten with a valid checksum, the corruption cannot be detected later either.
I would say that this is not a security issue and it only concerns accidental corruption, which is what the feature is there to catch.
Reproducer
----------
Run as root in a test VM (needs f2fs-tools, python3 and a loop device; it only touches a 256M image file in /tmp):
--- 8< --- repro.sh ---
#!/bin/sh
# Run as root in a test VM. Needs f2fs-tools, python3 and a loop device.
# Usage: sh repro.sh fsync (inode only in the roll-forward chain)
# sh repro.sh ckpt (control: checkpointed inode)
CASE=${1:-fsync}; IMG=/tmp/f2fs-rf.img; MNT=/mnt/f2fs-rf
rm -f $IMG; truncate -s 256M $IMG; mkdir -p $MNT DEV=$(losetup -f --show $IMG)
mkfs.f2fs -q -f -O extra_attr,inode_checksum $DEV
mount -t f2fs $DEV $MNT
head -c 8192 /dev/zero | tr '\0' A > $MNT/foo; sync INO=$(stat -c %i $MNT/foo)
if [ $CASE = fsync ]; then
python3 -c "import os; f = open('$MNT/foo', 'r+b'); f.write(b'B' * 12288); f.flush(); os.fsync(f.fileno())"
f2fs_io shutdown 2 $MNT # no checkpointfi
umount $MNT
# Flip bit 8 of i_size (byte 17) in the newest on-disk copy of the inode # block; for "fsync" only copies with the fsync mark in the footer count. python3 - $DEV $INO $CASE <<'PY'
import os, struct, sys
dev, ino, case = sys.argv[1], int(sys.argv[2]), sys.argv[3] fd = os.open(dev, os.O_RDWR); best = None
for blk in range(os.lseek(fd, 0, os.SEEK_END) // 4096):
nid, fino, flag = struct.unpack("<III", os.pread(fd, 12, blk * 4096 + 4072))
ver = struct.unpack("<Q", os.pread(fd, 8, blk * 4096 + 4084))[0] & 0xffffffff
if nid == ino == fino and (case != "fsync" or flag & 2):
if best is None or ver >= best[1]:
best = (blk, ver)off = best[0] * 4096 + 17os.pwrite(fd, bytes([os.pread(fd, 1, off)[0] ^ 1]), off); os.fsync(fd) print("corrupted inode", ino, "in block", best[0])
PY
dmesg -C
mount -t f2fs $DEV $MNT && stat -c "i_size after mount: %s" $MNT/foo dmesg | grep -E "checksum|recover_inode"
umount $MNT; losetup -d $DEV
--- >8 ---
Output on f2fs dev b61a029304bb:
# sh repro.sh fsync corrupted inode 4 in block 4609 i_size after mount: 12544 <- was 12288, no checksum message F2FS-fs (loop0): recover_inode: ino = 4, name = foo, inline = 21
# sh repro.sh ckpt (control) corrupted inode 4 in block 4608 stat: cannot statx '/mnt/f2fs-rf/foo': Bad message F2FS-fs (loop0): checksum invalid, nid = 4, ino_of_node = 4, 959b6904 vs. a32e4eab
How it was found
----------------
While testing how f2fs recovers from crash images in which a single already-written block is corrupted, bit flips in inode blocks were always caught when the inode was covered by a checkpoint (98 of 98), but never when it was only in the roll-forward chain (0 of 76).
Suggested fix
-------------
Call f2fs_inode_chksum_verify() for inode blocks (IS_INODE()) in find_fsync_dnodes(), right after the is_recoverable_dnode() check, and treat a mismatch like the regular read path does (-EFSBADCRC).
I tried exactly that locally on the dev branch: the reproducer no longer recovers the corrupted inode (i_size stays at the checkpointed value and "checksum invalid" is logged), and in our crash-image tests only the roll-forward inode cases changed outcome.
Two things I was unsure about:
1. Abort vs. skip. Stopping roll-forward at the bad block also loses later fsync'd files. Skipping only the bad inode and continuing keeps more data, but in our states it often left files from different points in time (a file fsync'd earlier rolled back, one fsync'd later recovered), which may break applications that rely on fsync ordering across files. Aborting seemed the safer choice to me.
2. Direct/indirect node blocks carry no checksum, so corrupted block addresses there are recovered (and read at runtime) silently. That is outside what inode_checksum covers; mentioned only for completeness.
I would send a patch if needed. Let me know if more additional information is required.
Thank you for confirming this issue.
Sincerly,
Yewon Choi
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel