Skip to content

Commit 62600af

Browse files
jankaragregkh
authored andcommitted
ext2: Fix underflow in ext2_max_size()
commit 1c2d142 upstream. When ext2 filesystem is created with 64k block size, ext2_max_size() will return value less than 0. Also, we cannot write any file in this fs since the sb->maxbytes is less than 0. The core of the problem is that the size of block index tree for such large block size is more than i_blocks can carry. So fix the computation to count with this possibility. File size limits computed with the new function for the full range of possible block sizes look like: bits file_size 10 17247252480 11 275415851008 12 2196873666560 13 2197948973056 14 2198486220800 15 2198754754560 16 2198888906752 CC: stable@vger.kernel.org Reported-by: yangerkun <yangerkun@huawei.com> Signed-off-by: Jan Kara <jack@suse.cz> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent c335b49 commit 62600af

1 file changed

Lines changed: 25 additions & 14 deletions

File tree

‎fs/ext2/super.c‎

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,8 @@ static loff_t ext2_max_size(int bits)
761761
{
762762
loff_t res = EXT2_NDIR_BLOCKS;
763763
int meta_blocks;
764-
loff_t upper_limit;
764+
unsigned int upper_limit;
765+
unsigned int ppb = 1 << (bits-2);
765766

766767
/* This is calculated to be the largest file size for a
767768
* dense, file such that the total number of
@@ -775,24 +776,34 @@ static loff_t ext2_max_size(int bits)
775776
/* total blocks in file system block size */
776777
upper_limit >>= (bits - 9);
777778

779+
/* Compute how many blocks we can address by block tree */
780+
res += 1LL << (bits-2);
781+
res += 1LL << (2*(bits-2));
782+
res += 1LL << (3*(bits-2));
783+
/* Does block tree limit file size? */
784+
if (res < upper_limit)
785+
goto check_lfs;
778786

787+
res = upper_limit;
788+
/* How many metadata blocks are needed for addressing upper_limit? */
789+
upper_limit -= EXT2_NDIR_BLOCKS;
779790
/* indirect blocks */
780791
meta_blocks = 1;
792+
upper_limit -= ppb;
781793
/* double indirect blocks */
782-
meta_blocks += 1 + (1LL << (bits-2));
783-
/* tripple indirect blocks */
784-
meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
785-
786-
upper_limit -= meta_blocks;
787-
upper_limit <<= bits;
788-
789-
res += 1LL << (bits-2);
790-
res += 1LL << (2*(bits-2));
791-
res += 1LL << (3*(bits-2));
794+
if (upper_limit < ppb * ppb) {
795+
meta_blocks += 1 + DIV_ROUND_UP(upper_limit, ppb);
796+
res -= meta_blocks;
797+
goto check_lfs;
798+
}
799+
meta_blocks += 1 + ppb;
800+
upper_limit -= ppb * ppb;
801+
/* tripple indirect blocks for the rest */
802+
meta_blocks += 1 + DIV_ROUND_UP(upper_limit, ppb) +
803+
DIV_ROUND_UP(upper_limit, ppb*ppb);
804+
res -= meta_blocks;
805+
check_lfs:
792806
res <<= bits;
793-
if (res > upper_limit)
794-
res = upper_limit;
795-
796807
if (res > MAX_LFS_FILESIZE)
797808
res = MAX_LFS_FILESIZE;
798809

0 commit comments

Comments
 (0)