e2fsck, libext2fs: fix resize_inode with too many backup groups
cover e2fsck, libext2fs: fix resize_inode with too many backup groups
A crafted image with resize_inode, no sparse_super and more than blocksize/4 groups makes e2fsck read and write past a one-block buffer. Patches 1-2 add the missing bounds checks, patch 3 makes e2fsck disable resize_inode on such a filesystem so the repair can finish, and patch 4 adds a test.
Lukas Czerner (4):
libext2fs: limit index in ext2fs_create_resize_inode()
e2fsck: limit index in check_resize_inode()
e2fsck: disable resize_inode when too many backups
e2fsck: add test for resize inode with too many backups
e2fsck/problem.c | 6 + e2fsck/problem.h | 3 + e2fsck/super.c | 29 +++++ lib/ext2fs/res_gdt.c | 4 + tests/f_resize_inode_many_backups/expect.1 | 144 +++++++++++++++++++++ tests/f_resize_inode_many_backups/expect.2 | 7 + tests/f_resize_inode_many_backups/image.gz | Bin 0 -> 69405 bytes tests/f_resize_inode_many_backups/name | 1 + 8 files changed, 194 insertions(+) create mode 100644 tests/f_resize_inode_many_backups/expect.1 create mode 100644 tests/f_resize_inode_many_backups/expect.2 create mode 100644 tests/f_resize_inode_many_backups/image.gz create mode 100644 tests/f_resize_inode_many_backups/name
base-commit: 8fd79523d051d5ea881237f27eb1c664486e0078
Signature · 1 line
2.56.0
PATCH 1/4 libext2fs: limit index in ext2fs_create_resize_inode()
ext2fs_create_resize_inode() walks the backup superblock groups returned by ext2fs_list_backups() and writes one reserved GDT entry per group in gdt_buf buffer. The buffer is exactly one filesystem block in size.
The index is never checked against that limit. A crafted filesystem image (resize_inode, no sparse_super and more block groups than blocksize/4) can force OOB read and write past the end of gdt_buf buffer. e2fsck with ASAN build aborts "e2fsck -f -y" with heap-buffer-overflow.
Fix it by actually checking the index (last) against EXT2_ADDR_PER_BLOCK(sb) and return EXT2_ET_RESIZE_INODE_CORRUPT if it is larger than the buffer.
--- lib/ext2fs/res_gdt.c | 4 ++++ 1 file changed, 4 insertions(+)
lib/ext2fs/res_gdt.c +4 -0
@@ -195,6 +195,10 @@ errcode_t ext2fs_create_resize_inode(ext2_filsys fs) fs->group_desc_count) {blk_t expect = gdt_blk + grp * sb->s_blocks_per_group; +if (last >= apb) { + retval =EXT2_ET_RESIZE_INODE_CORRUPT ; + goto out_dindir; + }if (!gdt_buf[last]) {#ifdef RES_GDT_DEBUG printf("added backup GDT %u grp %u@%u[%u]\n",
Signature · 1 line
2.56.0
PATCH 2/4 e2fsck: limit index in check_resize_inode()
In reply to Lukas Czernercheck_resize_inode() reads one backup GDT block of the resize inode into ind_buf and walks the backup backup superblock groups, comparing ind_buf[ind_off] against the expected block number. The buffer is exactly one filesystem block in size.
The index (ind_off) is never checked against that limit. A crafted filesystem image (resize_inode, no sparse_super and more block groups than blocksize/4) can force OOB read past the end of ind_buf buffer. e2fsck with ASAN build aborts "e2fsck -f -n" with heap-buffer-overflow.
Fix it by actually checking the index (ind_off) against fs->blocksize / sizeof(__u32) before reading ind_buf[ind_off] and goto resize_inode_invalid if it is past the end of the buffer.
--- e2fsck/super.c | 2 ++ 1 file changed, 2 insertions(+)
e2fsck/super.c +2 -0
@@ -831,6 +831,8 @@ void check_resize_inode(e2fsck_t ctx)for (j =1 ; j < fs->group_desc_count; j++) {if (!ext2fs_bg_has_super (fs, j))continue ; +if ((unsigned ) ind_off >= fs->blocksize /sizeof (__u32)) + goto resize_inode_invalid; expect = pblk +EXT2_GROUPS_TO_BLOCKS (fs->super, j);if (ind_buf[ind_off] != expect) goto resize_inode_invalid;
Signature · 1 line
2.56.0
PATCH 3/4 e2fsck: disable resize_inode when too many backups
In reply to Lukas Czernercheck_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.
--- e2fsck/problem.c | 6 ++++++ e2fsck/problem.h | 3 +++ e2fsck/super.c | 27 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+)
e2fsck/problem.c +6 -0
@@ -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 */
e2fsck/problem.h +3 -0
@@ -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 */
e2fsck/super.c +27 -0
@@ -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)) {
Signature · 1 line
2.56.0
PATCH 4/4 e2fsck: add test for resize inode with too many backups
In reply to Lukas CzernerAdd a test file f_resize_inode_many_backups with a filesystem image which has resize_inode feature enabled, sparse_super disabled and 258 block groups at 1KiB blocksize. Every block group has a backup superblock, so the resize inode would need 257 backup GDT entries in one indirect block, which only fits 256 (blocksize / 4). The resize inode on the image is zeroed, which is the state e2fsck ends with when it repeatedly fails to recreate it on such a filesystem.
The test framework runs e2fsck twice. With the fix, the first run offers to disable the resize_inode feature, clears it together with s_reserved_gdt_blocks and the second run finds a clean filesystem (exit status 1 then 0). Without the fix the first run instead offers to recreate the resize inode which always fails, and the second run reports the same errors again (exit status 1 then 1), so the test fails and catches the regression.
--- tests/f_resize_inode_many_backups/expect.1 | 144 +++++++++++++++++++++ tests/f_resize_inode_many_backups/expect.2 | 7 + tests/f_resize_inode_many_backups/image.gz | Bin 0 -> 69405 bytes tests/f_resize_inode_many_backups/name | 1 + 4 files changed, 152 insertions(+) create mode 100644 tests/f_resize_inode_many_backups/expect.1 create mode 100644 tests/f_resize_inode_many_backups/expect.2 create mode 100644 tests/f_resize_inode_many_backups/image.gz create mode 100644 tests/f_resize_inode_many_backups/name
tests/f_resize_inode_many_backups/expect.1 new file +144 -0
@@ -0,0 +1,144 @@ +Too many backup superblock groups (257) to fit in the resize +inode indirect block. Resize inode should be disabled. Fix? yes + +Pass 1: Checking inodes, blocks, and sizes +Root inode is not a directory. Clear? yes + +Pass 2: Checking directory structure +Pass 3: Checking directory connectivity +Root inode not allocated. Allocate? yes + +/lost+found not found. Create? yes + +Pass 4: Checking reference counts +Pass 5: Checking group summary information +Block bitmap differences: +(3--10) -25 +(259--266) +(513--522) +(770--778) +(1025--1034) +(1282--1290) +(1537--1546) +(1794--1802) +(2049--2058) +(2306--2314) +(2561--2570) +(2817--2826) +(3073--3082) +(3329--3338) +(3585--3594) +(3841--3850) +(4097--4106) +(4353--4362) +(4609--4618) +(4865--4874) +(5121--5130) +(5377--5386) +(5633--5642) +(5889--5898) +(6145--6154) +(6402--6410) +(6657--6666) +(6914--6922) +(7169--7178) +(7425--7434) +(7681--7690) +(7938--7946) +(8194--8202) +(8450--8458) +(8705--8714) +(8961--8970) +(9217--9226) +(9473--9482) +(9729--9738) +(9985--9994) +(10241--10250) +(10497--10506) +(10753--10762) +(11009--11018) +(11265--11274) +(11521--11530) +(11777--11786) +(12033--12042) +(12289--12298) +(12546--12554) +(12801--12810) +(13057--13066) +(13313--13322) +(13569--13578) +(13825--13834) +(14081--14090) +(14337--14346) +(14593--14602) +(14849--14858) +(15105--15114) +(15361--15370) +(15617--15626) +(15873--15882) +(16130--16138) +(16386--16394) +(16642--16650) +(16897--16906) +(17153--17162) +(17409--17418) +(17665--17674) +(17921--17930) +(18177--18186) +(18433--18442) +(18689--18698) +(18945--18954) +(19201--19210) +(19457--19466) +(19713--19722) +(19969--19978) +(20225--20234) +(20481--20490) +(20738--20746) +(20993--21002) +(21249--21258) +(21505--21514) +(21761--21770) +(22017--22026) +(22273--22282) +(22529--22538) +(22785--22794) +(23041--23050) +(23297--23306) +(23553--23562) +(23809--23818) +(24065--24074) +(24322--24330) +(24578--24586) +(24834--24842) +(25089--25098) +(25345--25354) +(25601--25610) +(25857--25866) +(26113--26122) +(26369--26378) +(26625--26634) +(26881--26890) +(27137--27146) +(27393--27402) +(27649--27658) +(27905--27914) +(28161--28170) +(28417--28426) +(28673--28682) +(28929--28938) +(29185--29194) +(29441--29450) +(29697--29706) +(29953--29962) +(30209--30218) +(30465--30474) +(30721--30730) +(30977--30986) +(31233--31242) +(31489--31498) +(31745--31754) +(32002--32010) +(32257--32266) +(32514--32522) +(32770--32778) +(33026--33034) +(33281--33290) +(33537--33546) +(33793--33802) +(34049--34058) +(34305--34314) +(34561--34570) +(34817--34826) +(35073--35082) +(35329--35338) +(35585--35594) +(35841--35850) +(36097--36106) +(36353--36362) +(36609--36618) +(36865--36874) +(37121--37130) +(37377--37386) +(37633--37642) +(37889--37898) +(38145--38154) +(38401--38410) +(38657--38666) +(38913--38922) +(39169--39178) +(39425--39434) +(39681--39690) +(39937--39946) +(40193--40202) +(40449--40458) +(40706--40714) +(40962--40970) +(41218--41226) +(41473--41482) +(41729--41738) +(41985--41994) +(42241--42250) +(42497--42506) +(42753--42762) +(43009--43018) +(43265--43274) +(43521--43530) +(43777--43786) +(44033--44042) +(44289--44298) +(44545--44554) +(44801--44810) +(45057--45066) +(45313--45322) +(45569--45578) +(45825--45834) +(46081--46090) +(46337--46346) +(46593--46602) +(46849--46858) +(47105--47114) +(47361--47370) +(47617--47626) +(47873--47882) +(48129--48138) +(48385--48394) +(48641--48650) +(48898--48906) +(49154--49162) +(49410--49418) +(49665--49674) +(49921--49930) +(50177--50186) +(50433--50442) +(50689--50698) +(50945--50954) +(51201--51210) +(51457--51466) +(51713--51722) +(51969--51978) +(52225--52234) +(52481--52490) +(52737--52746) +(52993--53002) +(53249--53258) +(53505--53514) +(53761--53770) +(54017--54026) +(54273--54282) +(54529--54538) +(54785--54794) +(55041--55050) +(55297--55306) +(55553--55562) +(55809--55818) +(56065--56074) +(56321--56330) +(56577--56586) +(56833--56842) +(57090--57098) +(57346--57354) +(57602--57610) +(57857--57866) +(58113--58122) +(58369--58378) +(58625--58634) +(58881--58890) +(59137--59146) +(59393--59402) +(59649--59658) +(59905--59914) +(60161--60170) +(60417--60426) +(60673--60682) +(60929--60938) +(61185--61194) +(61441--61450) +(61697--61706) +(61953--61962) +(62210--62218) +(62465--62474) +(62721--62730) +(62977--62986) +(63233--63242) +(63489--63498) +(63745--63754) +(64001--64010) +(64257--64266) +(64513--64522) +(64769--64778) +(65025--65034) +(65282--65290) +(65538--65546) +(65794--65802) +Fix? yes + +Free blocks count wrong for group #0 (232, counted=234). +Fix? yes + +Free blocks count wrong for group #1 (234, counted=236). +Fix? yes + +Free blocks count wrong for group #3 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #5 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #7 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #9 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #25 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #27 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #31 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #32 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #33 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #49 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #63 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #64 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #65 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #81 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #95 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #96 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #97 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #125 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #127 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #128 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #129 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #159 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #160 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #161 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #191 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #192 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #193 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #223 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #224 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #225 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #243 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #255 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #256 (235, counted=236). +Fix? yes + +Free blocks count wrong for group #257 (235, counted=236). +Fix? yes + +Free blocks count wrong (60848, counted=60886). +Fix? yes + +Inode bitmap differences: +1 +(3--10) +Fix? yes + +Free inodes count wrong for group #0 (63, counted=53). +Fix? yes + +Directories count wrong for group #0 (1, counted=2). +Fix? yes + +Free inodes count wrong (16511, counted=16501). +Fix? yes + + +test_filesys: ***** FILE SYSTEM WAS MODIFIED ***** +test_filesys: 11/16512 files (0.0% non-contiguous), 5163/66049 blocks +Exit status is 1
tests/f_resize_inode_many_backups/expect.2 new file +7 -0
@@ -0,0 +1,7 @@ +Pass 1: Checking inodes, blocks, and sizes +Pass 2: Checking directory structure +Pass 3: Checking directory connectivity +Pass 4: Checking reference counts +Pass 5: Checking group summary information +test_filesys: 11/16512 files (0.0% non-contiguous), 5163/66049 blocks +Exit status is 0
tests/f_resize_inode_many_backups/image.gz new file binary +0 -0
tests/f_resize_inode_many_backups/name new file +1 -0
@@ -0,0 +1 @@ +e2fsck: resize inode with more backups than fit in one indirect block
Signature · 1 line
2.56.0