Skip to content

Commit 8aad547

Browse files
WenjieQiHUSTgregkh
authored andcommitted
f2fs: validate orphan inode entry count
commit 846c499 upstream. 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: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 1e48fef commit 8aad547

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
@@ -745,6 +745,7 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
745745
for (i = 0; i < orphan_blocks; i++) {
746746
struct folio *folio;
747747
struct f2fs_orphan_block *orphan_blk;
748+
unsigned int entry_count;
748749

749750
folio = f2fs_get_meta_folio(sbi, start_blk + i);
750751
if (IS_ERR(folio)) {
@@ -753,7 +754,18 @@ int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi)
753754
}
754755

755756
orphan_blk = folio_address(folio);
756-
for (j = 0; j < le32_to_cpu(orphan_blk->entry_count); j++) {
757+
entry_count = le32_to_cpu(orphan_blk->entry_count);
758+
if (entry_count > F2FS_ORPHANS_PER_BLOCK) {
759+
f2fs_err(sbi, "invalid orphan inode entry count %u",
760+
entry_count);
761+
set_sbi_flag(sbi, SBI_NEED_FSCK);
762+
f2fs_handle_error(sbi, ERROR_INCONSISTENT_ORPHAN);
763+
err = -EFSCORRUPTED;
764+
f2fs_folio_put(folio, true);
765+
goto out;
766+
}
767+
768+
for (j = 0; j < entry_count; j++) {
757769
nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
758770

759771
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
@@ -104,6 +104,7 @@ enum f2fs_error {
104104
ERROR_CORRUPTED_XATTR,
105105
ERROR_INVALID_NODE_REFERENCE,
106106
ERROR_INCONSISTENT_NAT,
107+
ERROR_INCONSISTENT_ORPHAN,
107108
ERROR_MAX,
108109
};
109110

0 commit comments

Comments
 (0)