Patchwork ext4: fix possible overflow in ext4_trim_fs()

login
register
mail settings
Submitter Lukas Czerner
Date Sept. 5, 2011, 2:34 p.m.
Message ID <1315233249-27167-1-git-send-email-lczerner@redhat.com>
Download mbox | patch
Permalink /patch/113377/
State Superseded
Headers show

Comments

Lukas Czerner - Sept. 5, 2011, 2:34 p.m.
In ext4_trim_fs it is possible that start+len might overflow. Fix it by
decrementing the len so that start+len equals to the file system size in
the worst case.

Signed-off-by: Lukas Czerner <lczerner@redhat.com>
---
 fs/ext4/mballoc.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
Tao Ma - Sept. 6, 2011, 4:15 a.m.
Hi Lucas,
On 09/05/2011 10:34 PM, Lukas Czerner wrote:
> In ext4_trim_fs it is possible that start+len might overflow. Fix it by
> decrementing the len so that start+len equals to the file system size in
> the worst case.
Actually start + len can never overflow since they are changed by
start = range->start >> sb->s_blocksize_bits;
len = range->len >> sb->s_blocksize_bits;

range->start and range->len are u64, and after they are shifted with
blocksize_bits, start+len(ext4_fsblk_t is also 64bit) can't overflow.

Thanks
Tao
> 
> Signed-off-by: Lukas Czerner <lczerner@redhat.com>
> ---
>  fs/ext4/mballoc.c |    6 +++++-
>  1 files changed, 5 insertions(+), 1 deletions(-)
> 
> diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
> index 17a5a57..d86dc14 100644
> --- a/fs/ext4/mballoc.c
> +++ b/fs/ext4/mballoc.c
> @@ -4952,14 +4952,18 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
>  	uint64_t start, len, minlen, trimmed = 0;
>  	ext4_fsblk_t first_data_blk =
>  			le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
> +	ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
>  	int ret = 0;
>  
>  	start = range->start >> sb->s_blocksize_bits;
>  	len = range->len >> sb->s_blocksize_bits;
>  	minlen = range->minlen >> sb->s_blocksize_bits;
>  
> -	if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)))
> +	if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)) ||
> +	    unlikely(start > max_blks))
>  		return -EINVAL;
> +	if (unlikely(len > max_blks))
> +		len = max_blks - start;
>  	if (start + len <= first_data_blk)
>  		goto out;
>  	if (start < first_data_blk) {

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Lukas Czerner - Sept. 6, 2011, 6:26 a.m.
On Tue, 6 Sep 2011, Tao Ma wrote:

> Hi Lucas,
> On 09/05/2011 10:34 PM, Lukas Czerner wrote:
> > In ext4_trim_fs it is possible that start+len might overflow. Fix it by
> > decrementing the len so that start+len equals to the file system size in
> > the worst case.
> Actually start + len can never overflow since they are changed by
> start = range->start >> sb->s_blocksize_bits;
> len = range->len >> sb->s_blocksize_bits;
> 
> range->start and range->len are u64, and after they are shifted with
> blocksize_bits, start+len(ext4_fsblk_t is also 64bit) can't overflow.
> 
> Thanks
> Tao

Hi Tao,

I am really sorry for badly worded commit description. The problem is
real, however as you pointed out the overflow does not happen in
len+start addition but in ext4_get_group_no_and_offset() when we are
storing group number into the ext4_group_t which is 32 bit long, but
the result of do_div() might be bigger than that.

I will rephrase the commit description and resend the patch. I was doing
similar thing for xfs and I have just used almost the same description
which is wrong.

Thanks!
-Lukas

> > 
> > Signed-off-by: Lukas Czerner <lczerner@redhat.com>
> > ---
> >  fs/ext4/mballoc.c |    6 +++++-
> >  1 files changed, 5 insertions(+), 1 deletions(-)
> > 
> > diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
> > index 17a5a57..d86dc14 100644
> > --- a/fs/ext4/mballoc.c
> > +++ b/fs/ext4/mballoc.c
> > @@ -4952,14 +4952,18 @@ int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
> >  	uint64_t start, len, minlen, trimmed = 0;
> >  	ext4_fsblk_t first_data_blk =
> >  			le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
> > +	ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
> >  	int ret = 0;
> >  
> >  	start = range->start >> sb->s_blocksize_bits;
> >  	len = range->len >> sb->s_blocksize_bits;
> >  	minlen = range->minlen >> sb->s_blocksize_bits;
> >  
> > -	if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)))
> > +	if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)) ||
> > +	    unlikely(start > max_blks))
> >  		return -EINVAL;
> > +	if (unlikely(len > max_blks))
> > +		len = max_blks - start;
> >  	if (start + len <= first_data_blk)
> >  		goto out;
> >  	if (start < first_data_blk) {
> 
>

Patch

diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 17a5a57..d86dc14 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -4952,14 +4952,18 @@  int ext4_trim_fs(struct super_block *sb, struct fstrim_range *range)
 	uint64_t start, len, minlen, trimmed = 0;
 	ext4_fsblk_t first_data_blk =
 			le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
+	ext4_fsblk_t max_blks = ext4_blocks_count(EXT4_SB(sb)->s_es);
 	int ret = 0;
 
 	start = range->start >> sb->s_blocksize_bits;
 	len = range->len >> sb->s_blocksize_bits;
 	minlen = range->minlen >> sb->s_blocksize_bits;
 
-	if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)))
+	if (unlikely(minlen > EXT4_BLOCKS_PER_GROUP(sb)) ||
+	    unlikely(start > max_blks))
 		return -EINVAL;
+	if (unlikely(len > max_blks))
+		len = max_blks - start;
 	if (start + len <= first_data_blk)
 		goto out;
 	if (start < first_data_blk) {