ext3: Fix an overflow in ext3_trim_fs.
authorTao Ma <boyu.mt@taobao.com>
Thu, 3 Mar 2011 14:58:37 +0000 (22:58 +0800)
committerJan Kara <jack@suse.cz>
Thu, 3 Mar 2011 23:34:15 +0000 (00:34 +0100)
In a bs=4096 volume, if we call FITRIM with the following parameter as
fstrim_range(start = 102400, len = 134144000, minlen = 10240), with the
following code:
if (len >= EXT3_BLOCKS_PER_GROUP(sb))
        len -= (EXT3_BLOCKS_PER_GROUP(sb) - first_block);
else
        last_block = first_block + len;

So if len < EXT3_BLOCKS_PER_GROUP while first_block + len >
EXT3_BLOCKS_PER_GROUP, last_block will be set to an overflow value
which exceeds EXT3_BLOCKS_PER_GROUP.

This patch fixes it and adjusts len and last_block accordingly.

Cc: Lukas Czerner <lczerner@redhat.com>
Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Tao Ma <boyu.mt@taobao.com>
Signed-off-by: Jan Kara <jack@suse.cz>
fs/ext3/balloc.c

index db1906b4e39cff78ac59d9326e96605b0ab73025..153242187fce7b3ddade632f538d8bec5ab47724 100644 (file)
@@ -2127,10 +2127,15 @@ int ext3_trim_fs(struct super_block *sb, struct fstrim_range *range)
                if (free_blocks < minlen)
                        continue;
 
-               if (len >= EXT3_BLOCKS_PER_GROUP(sb))
-                       len -= (EXT3_BLOCKS_PER_GROUP(sb) - first_block);
-               else
+               /*
+                * For all the groups except the last one, last block will
+                * always be EXT3_BLOCKS_PER_GROUP(sb), so we only need to
+                * change it for the last group in which case first_block +
+                * len < EXT3_BLOCKS_PER_GROUP(sb).
+                */
+               if (first_block + len < EXT3_BLOCKS_PER_GROUP(sb))
                        last_block = first_block + len;
+               len -= last_block - first_block;
 
                ret = ext3_trim_all_free(sb, group, first_block,
                                        last_block, minlen);