Skip to content

Commit d2f2361

Browse files
WenjieQiHUSTgregkh
authored andcommitted
f2fs: validate orphan inode entry count
[ Upstream commit 846c499 ] f2fs_recover_orphan_inodes() trusts the orphan block entry_count when replaying orphan inodes from the checkpoint pack. A corrupted entry_count larger than F2FS_ORPHANS_PER_BLOCK makes the recovery loop read past the ino[] array and interpret footer or following data as inode numbers. On a crafted image, mounting an unpatched kernel can drive orphan recovery into f2fs_bug_on() and panic the kernel. Validate entry_count before consuming entries so corrupted checkpoint data fails the mount with -EFSCORRUPTED and requests fsck instead. Set ERROR_INCONSISTENT_ORPHAN as well, so the corruption reason can be recorded in the superblock s_errors[] field. This gives fsck a persistent hint even though mount-time orphan recovery failure may leave no chance to persist SBI_NEED_FSCK through a checkpoint. Cc: stable@kernel.org Fixes: 127e670 ("f2fs: add checkpoint operations") Signed-off-by: Wenjie Qi <qiwenjie@xiaomi.com> Reviewed-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f0b4e1c commit d2f2361

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

fs/f2fs/checkpoint.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
742742
for (i = 0; i < orphan_blocks; i++) {
743743
struct page *page;
744744
struct f2fs_orphan_block *orphan_blk;
745+
unsigned int entry_count;
745746

746747
page = f2fs_get_meta_page(sbi, start_blk + i);
747748
if (IS_ERR(page)) {
@@ -750,7 +751,18 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
750751
}
751752

752753
orphan_blk = (struct f2fs_orphan_block *)page_address(page);
753-
for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
754+
entry_count = le32_to_cpu(orphan_blk->entry_count);
755+
if (entry_count > F2FS_ORPHANS_PER_BLOCK) {
756+
f2fs_err(sbi, "invalid orphan inode entry count %u",
757+
entry_count);
758+
set_sbi_flag(sbi, SBI_NEED_FSCK);
759+
f2fs_handle_error(sbi, ERROR_INCONSISTENT_ORPHAN);
760+
err = -EFSCORRUPTED;
761+
f2fs_put_page(page, 1);
762+
goto out;
763+
}
764+
765+
for (j = 0; j < entry_count; j++) {
754766
nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
755767

756768
err = recover_orphan_inode(sbi, ino);

include/linux/f2fs_fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ enum f2fs_error {
102102
ERROR_CORRUPTED_XATTR,
103103
ERROR_INVALID_NODE_REFERENCE,
104104
ERROR_INCONSISTENT_NAT,
105+
ERROR_INCONSISTENT_ORPHAN,
105106
ERROR_MAX,
106107
};
107108

0 commit comments

Comments
 (0)