[PATCH] f2fs: don't read inode->i_ino after iput() in gc_data_segment()
From: Suyuan Wang via B4 Relay
Date: Wed Nov 18 2026 - 07:53:24 EST
From: Suyuan Wang <stevewong@xxxxxxxxxxx>
When gc_data_segment() finds an inode that has both the inline-data flag
and a data block, it puts the inode before logging the error:
iput(inode);
set_sbi_flag(sbi, SBI_NEED_FSCK);
f2fs_err_ratelimited(sbi,
"inode %llu has both inline_data flag and "
"data block, nid=%u, ofs_in_node=%u",
inode->i_ino, dni.nid, ofs_in_node);
At this point the inode has not been added to the GC inode list and
i_gc_rwsem has not been taken, so nothing else is holding it. iput()
may drop the last reference, so reading inode->i_ino for the message is
a use-after-free.
Move the iput() below the error message so i_ino is read while the
reference is still held.
Found with a static analysis tool and checked by hand.
Fixes: fc01008c92f4 ("f2fs: fix to do sanity check on F2FS_INLINE_DATA flag in inode during GC")
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Kaixuan Li <kaixuan.li@xxxxxxxxxx>
Signed-off-by: Kaixuan Li <kaixuan.li@xxxxxxxxxx>
Signed-off-by: Suyuan Wang <stevewong@xxxxxxxxxxx>
---
Compile tested only (x86_64, W=1 and sparse show no new warnings). The
use-after-free is in the corrupted-inode error path and hasn't been
reproduced at runtime.
---
fs/f2fs/gc.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
index 5b948c92f981..edbfb45f15fc 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -1664,12 +1664,12 @@ static int gc_data_segment(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
}
if (f2fs_has_inline_data(inode)) {
- iput(inode);
set_sbi_flag(sbi, SBI_NEED_FSCK);
f2fs_err_ratelimited(sbi,
"inode %llu has both inline_data flag and "
"data block, nid=%u, ofs_in_node=%u",
inode->i_ino, dni.nid, ofs_in_node);
+ iput(inode);
continue;
}