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 patch ?
-Lukas
Ted Ts'o
maintainerQuoted 9 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]>
Added to the ext4 patch queue. Thanks for the ping.
- Ted
Quoted 9 lines · Ted Ts'o
Quoted 8 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.
Quoted 6 lines
Quoted 1 line
Signed-off-by Lukas Czerner <[email protected]>Added to the ext4 patch queue. Thanks for the ping.
- Ted
Hi Ted,
Actually the patch is not enough, there is still a possibility for overflow to happen. Jan Kara notice the same bug in ext3 batched discard implementation. I am sorry for this, I'll sen updated patch ASAP.
-Lukas
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 | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-)
fs/ext4/mballoc.c +6 -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,11 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range) minlen = range->minlen >> sb->s_blocksize_bits; trimmed =0 ; +if (start >= blocks_count) +return -EINVAL ; +if (start + len > blocks_count) + len = blocks_count - start; +if (unlikely (minlen >EXT4_BLOCKS_PER_GROUP (sb)))return -EINVAL ;
Signature · 1 line
1.7.2.3
Ted Ts'o
maintainerQuoted 5 lines · Lukas Czerner
Actually the patch is not enough, there is still a possibility for overflow to happen. Jan Kara notice the same bug in ext3 batched discard implementation. I am sorry for this, I'll sen updated patch ASAP.
This isn't a new patch, and not a replacement for the previous one, correct?
- Ted
Quoted 7 lines · Ted Ts'o
Quoted 5 lines · Lukas Czerner
Actually the patch is not enough, there is still a possibility for overflow to happen. Jan Kara notice the same bug in ext3 batched discard implementation. I am sorry for this, I'll sen updated patch ASAP.
Quoted 5 lines
This isn't a new patch, and not a replacement for the previous one, correct?
- Ted
It is the replacement for the previous one.
-Lukas
Ted Ts'o
maintainerQuoted 3 lines · Lukas Czerner
It is the replacement for the previous one.
Ah, I see, I got it confused with another trim_fs patch. I wish you had sent this to me earlier, but ok, I'll fix it up.
- Ted
Quoted 10 lines · Ted Ts'o
Quoted 3 lines · Lukas Czerner
It is the replacement for the previous one.
Ah, I see, I got it confused with another
trim_fspatch. I wish you had sent this to me earlier, but ok, I'll fix it up.- Ted
Yes, sorry about that and thanks.
-Lukas