ext4: fix possible overflow in ext4_trim_fs()
PATCH 1/1 ext4: fix possible overflow in ext4_trim_fs()
When determining last group through ext4_get_group_no_and_offset() the result may be wrong in cases when range->start and range-len are too big, because it may overflow when summing up those two numbers.
Fix that by checking range->len and limit its value to ext4_blocks_count(). This commit was tested by myself with expected result.
--- fs/ext4/mballoc.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
fs/ext4/mballoc.c +4 -0
@@ -4819,6 +4819,7 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)ext4_group_t group, ngroups =ext4_get_groups_count (sb);ext4_grpblk_t cnt =0 , first_block, last_block;uint64_t start, len, minlen, trimmed; +ext4_fsblk_t blocks_count =ext4_blocks_count (EXT4_SB (sb)->s_es);int ret =0 ; start = range->start >> sb->s_blocksize_bits;
@@ -4826,6 +4827,9 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) minlen = range->minlen >> sb->s_blocksize_bits; trimmed =0 ; +if (len > blocks_count) + len = blocks_count - start; +if (unlikely (minlen >EXT4_BLOCKS_PER_GROUP (sb)))return -EINVAL ;
Signature · 1 line
1.7.2.3
Quoted 27 lines · Lukas Czerner
When determining last group through
ext4_get_group_no_and_offset()the result may be wrong in cases whenrange->startand range-len are too big, because it may overflow when summing up those two numbers.Fix that by checking
range->lenand limit its value toext4_blocks_count(). This commit was tested by myself with expected result.Signed-off-by Lukas Czerner <[email protected]>--- fs/ext4/mballoc.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)fs/ext4/mballoc.c +1
-0@@ -4819,6 +4819,7 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)ext4_group_t group, ngroups =ext4_get_groups_count (sb);ext4_grpblk_t cnt =0 , first_block, last_block;uint64_t start, len, minlen, trimmed; +ext4_fsblk_t blocks_count =ext4_blocks_count (EXT4_SB (sb)->s_es);int ret =0 ; start = range->start >> sb->s_blocksize_bits;
fs/ext4/mballoc.c int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) · line 4835
Quoted 9 lines
fs/ext4/mballoc.c +3
-0@@ -4826,6 +4827,9 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) minlen = range->minlen >> sb->s_blocksize_bits; trimmed =0 ; +if (len > blocks_count) + len = blocks_count - start; +if (unlikely (minlen >EXT4_BLOCKS_PER_GROUP (sb)))return -EINVAL ;
Hi Ted,
what is the status of this one?
Thanks!
-Lukas
Quoted 41 lines · Lukas Czerner
Quoted 27 lines · Lukas Czerner
When determining last group through
ext4_get_group_no_and_offset()the result may be wrong in cases whenrange->startand range-len are too big, because it may overflow when summing up those two numbers.Fix that by checking
range->lenand limit its value toext4_blocks_count(). This commit was tested by myself with expected result.Signed-off-by Lukas Czerner <[email protected]>--- fs/ext4/mballoc.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)fs/ext4/mballoc.c +1
-0@@ -4819,6 +4819,7 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)ext4_group_t group, ngroups =ext4_get_groups_count (sb);ext4_grpblk_t cnt =0 , first_block, last_block;uint64_t start, len, minlen, trimmed; +ext4_fsblk_t blocks_count =ext4_blocks_count (EXT4_SB (sb)->s_es);int ret =0 ; start = range->start >> sb->s_blocksize_bits;fs/ext4/mballoc.c int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) · line 4835
Quoted 9 lines
fs/ext4/mballoc.c +3
-0@@ -4826,6 +4827,9 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) minlen = range->minlen >> sb->s_blocksize_bits; trimmed =0 ; +if (len > blocks_count) + len = blocks_count - start; +if (unlikely (minlen >EXT4_BLOCKS_PER_GROUP (sb)))return -EINVAL ;Hi Ted,
Quoted 5 lines
what is the status of this one?
Thanks!
-Lukas
Oh, please ignore this, I just found "already applied" message. Sorry for the noise.
-Lukas