Skip to contentSkip to conversation

ext4: fix possible overflow in ext4_trim_fs()

PATCH In review Lukas Czerner 2 participants 9 messages Nov 25, 2010 – Jan 10, 2011 ext4 file system

lore
Share & tools
lore
#0 lore raw

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
index 5b4d4e3..7b05d92 100644
@@ -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
#1 lore raw
Quoted 27 lines · Lukas Czerner

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 +1 -0
index 5b4d4e3..7b05d92 100644
@@ -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

maintainer
#2 lore raw
In reply to Lukas Czerner
Quoted 9 lines · Lukas Czerner

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.

Added to the ext4 patch queue. Thanks for the ping.

- Ted

#3 lore raw
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 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.

Quoted 6 lines
Quoted 1 line

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

#4 lore raw

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
index 5b4d4e3..5f3e8a3 100644
@@ -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
maintainer
#5 lore raw
In reply to Lukas Czerner
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.

This isn't a new patch, and not a replacement for the previous one, correct?

- Ted

#6 lore raw
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

maintainer
#7 lore raw
Quoted 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

#8 lore raw
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_fs patch. I wish you had sent this to me earlier, but ok, I'll fix it up.

- Ted

Yes, sorry about that and thanks.

-Lukas