Skip to content

Commit cd481c1

Browse files
ntsironsnitm
authored andcommitted
dm clone: Add overflow check for number of regions
Add overflow check for clone->nr_regions variable, which holds the number of regions of the target. The overflow can occur with sufficiently large devices, if BITS_PER_LONG == 32. E.g., if the region size is 8 sectors (4K), the overflow would occur for device sizes > 34359738360 sectors (~16TB). This could result in multiple device sectors wrongly mapping to the same region number, due to the truncation from 64 bits to 32 bits, which would lead to data corruption. Fixes: 7431b78 ("dm: add clone target") Cc: [email protected] # v5.4+ Signed-off-by: Nikos Tsironis <[email protected]> Signed-off-by: Mike Snitzer <[email protected]>
1 parent 4b51429 commit cd481c1

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

drivers/md/dm-clone-target.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,7 @@ static int copy_ctr_args(struct clone *clone, int argc, const char **argv, char
17901790
static int clone_ctr(struct dm_target *ti, unsigned int argc, char **argv)
17911791
{
17921792
int r;
1793+
sector_t nr_regions;
17931794
struct clone *clone;
17941795
struct dm_arg_set as;
17951796

@@ -1831,7 +1832,16 @@ static int clone_ctr(struct dm_target *ti, unsigned int argc, char **argv)
18311832
goto out_with_source_dev;
18321833

18331834
clone->region_shift = __ffs(clone->region_size);
1834-
clone->nr_regions = dm_sector_div_up(ti->len, clone->region_size);
1835+
nr_regions = dm_sector_div_up(ti->len, clone->region_size);
1836+
1837+
/* Check for overflow */
1838+
if (nr_regions != (unsigned long)nr_regions) {
1839+
ti->error = "Too many regions. Consider increasing the region size";
1840+
r = -EOVERFLOW;
1841+
goto out_with_source_dev;
1842+
}
1843+
1844+
clone->nr_regions = nr_regions;
18351845

18361846
r = validate_nr_regions(clone->nr_regions, &ti->error);
18371847
if (r)

0 commit comments

Comments
 (0)