f2fs: a same-name dirent whose target inode has no NAT entry hits WARN_ON(1) in recover_dentry() at mount - kernel panic with panic_on_warn
(Resending to the list: my previous mail used a nonexistent list address [email protected] and was rejected; the correct address from MAINTAINERS is lists.sourceforge.net. The report below is unchanged.)
Hi Jaegeuk, Chao,
Mounting a crafted f2fs image with fsync data to recover ends in a WARN_ON(1) during mount-time recovery, before any syscall beyond mount(); with panic_on_warn the box panics at boot.
F2FS-fs (vda): f2fs_recover_fsync_data: recovery fsync data, check_only: 0 F2FS-fs (vda): recover_dentry: inode with [ino:xxx] has invalid encrypted name
------------[ cut here ]------------ WARNING: fs/f2fs/recovery.c:223 at f2fs_recover_fsync_data+... Kernel panic - not syncing: kernel: panic_on_warn set ...
## The path
fs/f2fs/recovery.c, recover_dentry():
de = __f2fs_find_entry(dir, &fname, &dentry_blk);
if (de && inode->i_ino == le32_to_cpu(de->ino))
goto out_put;
if (de) {
einode = f2fs_iget_retry(inode->i_sb, le32_to_cpu(de->ino));
if (IS_ERR(einode)) {
WARN_ON(1); /* :223 */
err = PTR_ERR(einode);
if (err == -ENOENT)
err = -EEXIST;
goto out_put;
}The recovered inode's filename is looked up in the parent directory. When a same-name dirent exists but points at a different ino whose NAT entry is missing (blk_addr == NULL_ADDR), f2fs_iget() returns -ENOENT and the function hits WARN_ON(1). Nothing on the way to the assertion checks that the dirent target exists: the name lookup and the inode lookup read two independent on-disk structures, and a corrupted or fuzzed image can leave them inconsistent.
The mount is enough: no post-mount syscall is needed, the recovery walk reaches the entry as soon as the last checkpoint carries fsync data for the crafted dentry.
## Related but distinct reports
This is a third independent spot in the same recovery walk:
- recover_orphan_inode() had f2fs_bug_on(PTR_ERR(inode) == -ENOENT) for an orphan whose NAT entry is missing; fixed by treating the missing inode as fs corruption (downgrade to f2fs_err + SBI_NEED_FSCK) - that fix does not touch recover_dentry().
- do_recover_data() cross-checks ni.ino against the node footer and hits f2fs_bug_on at recovery.c:698 - reported separately.
Same family: an ino read from one on-disk structure does not resolve through the NAT, and the recovery code asserts instead of treating it as corruption.
## Reproducer
A crafted image where the fsync dentry entry to recover names a file whose dirent in the parent block points at an ino with no NAT entry; mounting the image reproduces the WARNING (3/3 runs with panic_on_warn=0, panic at boot with panic_on_warn=1). A control image identical except that the dirent points at an existing inode mounts and recovers normally.
The image builder and the run script are available on request.
## Versions
Reproduced on 7.3.0 (self-built, panic_on_warn=1 for the panic demonstration). The WARN_ON(1) at recovery.c:223 is unchanged in current mainline. No report found on the syzbot dashboard for recover_dentry/f2fs_iget_retry, and no list thread found for this assertion.
## Possible fix direction
Treat the missing dirent target like the orphan case: log f2fs_err, set SBI_NEED_FSCK, skip the dentry (the recovered inode keeps its name from the fsync entry) or fall back to -EEXIST handling, instead of WARN_ON(1).
Thanks,
Guikun Yang