Skip to content

Commit 07301df

Browse files
adam900710kdave
authored andcommitted
btrfs: trim: Check the range passed into to prevent overflow
Normally the range->len is set to default value (U64_MAX), but when it's not default value, we should check if the range overflows. And if it overflows, return -EINVAL before doing anything. Reviewed-by: Nikolay Borisov <[email protected]> Reviewed-by: Anand Jain <[email protected]> Signed-off-by: Qu Wenruo <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent d7cd4dd commit 07301df

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

fs/btrfs/extent-tree.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8966,6 +8966,7 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
89668966
struct btrfs_device *device;
89678967
struct list_head *devices;
89688968
u64 group_trimmed;
8969+
u64 range_end = U64_MAX;
89698970
u64 start;
89708971
u64 end;
89718972
u64 trimmed = 0;
@@ -8975,16 +8976,23 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
89758976
int dev_ret = 0;
89768977
int ret = 0;
89778978

8979+
/*
8980+
* Check range overflow if range->len is set.
8981+
* The default range->len is U64_MAX.
8982+
*/
8983+
if (range->len != U64_MAX &&
8984+
check_add_overflow(range->start, range->len, &range_end))
8985+
return -EINVAL;
8986+
89788987
cache = btrfs_lookup_first_block_group(fs_info, range->start);
89798988
for (; cache; cache = next_block_group(cache)) {
8980-
if (cache->key.objectid >= (range->start + range->len)) {
8989+
if (cache->key.objectid >= range_end) {
89818990
btrfs_put_block_group(cache);
89828991
break;
89838992
}
89848993

89858994
start = max(range->start, cache->key.objectid);
8986-
end = min(range->start + range->len,
8987-
cache->key.objectid + cache->key.offset);
8995+
end = min(range_end, cache->key.objectid + cache->key.offset);
89888996

89898997
if (end - start >= range->minlen) {
89908998
if (!block_group_cache_done(cache)) {

0 commit comments

Comments
 (0)