check_resize_inode() requires that every backup sb group has an entry in the resize inode indirect block, which holds at most fs->blocksize / sizeof(__u32) entries. On a crafted or corrupted filesystem image with more backup sb groups than that, the resize inode can never be valid. Previous fixes prevent OOB read/write, but e2fsck can never succeed in recreating the resize inode, so the repair never succeeds. Fix it by checking whether entries can fit into one file system block and offer to clear the resize_inode feature, clear s_reserved_gdt_blocks and zero the resize inode. Signed-off-by: Lukas Czerner --- e2fsck/problem.c | 6 ++++++ e2fsck/problem.h | 3 +++ e2fsck/super.c | 27 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/e2fsck/problem.c b/e2fsck/problem.c index e433281f..c17f242a 100644 --- a/e2fsck/problem.c +++ b/e2fsck/problem.c @@ -546,6 +546,12 @@ static struct e2fsck_problem problem_table[] = { N_("Orphan file (@i %i) size is not multiple of block size. Terminating orphan file recovery.\n"), PROMPT_NONE, 0 }, + /* More backup-superblock groups than fit in one indirect block */ + { PR_0_RESIZE_INODE_TOO_MANY_BACKUPS, + N_("Too many backup superblock groups (%N) to fit in the resize\n" + "@i indirect block. Resize @i should be disabled. "), + PROMPT_FIX, 0, 0, 0, 0 }, + /* Pass 1 errors */ /* Pass 1: Checking inodes, blocks, and sizes */ diff --git a/e2fsck/problem.h b/e2fsck/problem.h index ef15b8c8..4e8a8f43 100644 --- a/e2fsck/problem.h +++ b/e2fsck/problem.h @@ -300,6 +300,9 @@ struct problem_context { /* Orphan file size isn't multiple of blocks size */ #define PR_0_ORPHAN_FILE_WRONG_SIZE 0x000055 +/* Too many backup-superblock groups to fit the resize inode */ +#define PR_0_RESIZE_INODE_TOO_MANY_BACKUPS 0x000056 + /* * Pass 1 errors */ diff --git a/e2fsck/super.c b/e2fsck/super.c index eb1c7e52..2a5025c3 100644 --- a/e2fsck/super.c +++ b/e2fsck/super.c @@ -731,6 +731,33 @@ void check_resize_inode(e2fsck_t ctx) clear_problem_context(&pctx); + /* + * The resize inode must fit backup GDT entries in a single indirect + * block. If it does not, it won't work. Clear it. + */ + if (ext2fs_has_feature_resize_inode(fs->super) && + fs->super->s_reserved_gdt_blocks) { + dgrp_t three = 1, five = 5, seven = 7, backups = 0; + + while (ext2fs_list_backups(fs, &three, &five, &seven) < + fs->group_desc_count) + backups++; + if (backups > fs->blocksize / sizeof(__u32)) { + pctx.num = backups; + if (fix_problem(ctx, + PR_0_RESIZE_INODE_TOO_MANY_BACKUPS, + &pctx)) { + ext2fs_clear_feature_resize_inode(fs->super); + fs->super->s_reserved_gdt_blocks = 0; + memset(&inode, 0, sizeof(inode)); + e2fsck_write_inode(ctx, EXT2_RESIZE_INO, &inode, + "clear_resize"); + ext2fs_mark_super_dirty(fs); + goto cleanup; + } + } + } + if (ext2fs_has_feature_resize_inode(fs->super) && ext2fs_has_feature_meta_bg(fs->super) && fix_problem(ctx, PR_0_DISABLE_RESIZE_INODE, &pctx)) { -- 2.56.0