Skip to content

Commit 9fc06ff

Browse files
ntsironsnitm
authored andcommitted
dm clone: Add missing casts to prevent overflows and data corruption
Add missing casts when converting from regions to sectors. In case BITS_PER_LONG == 32, the lack of the appropriate casts can lead to overflows and miscalculation of the device sector. As a result, we could end up discarding and/or copying the wrong parts of the device, thus corrupting the device's data. 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 cd481c1 commit 9fc06ff

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

drivers/md/dm-clone-target.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ static bool bio_triggers_commit(struct clone *clone, struct bio *bio)
282282
/* Get the address of the region in sectors */
283283
static inline sector_t region_to_sector(struct clone *clone, unsigned long region_nr)
284284
{
285-
return (region_nr << clone->region_shift);
285+
return ((sector_t)region_nr << clone->region_shift);
286286
}
287287

288288
/* Get the region number of the bio */
@@ -471,7 +471,7 @@ static void complete_discard_bio(struct clone *clone, struct bio *bio, bool succ
471471
if (test_bit(DM_CLONE_DISCARD_PASSDOWN, &clone->flags) && success) {
472472
remap_to_dest(clone, bio);
473473
bio_region_range(clone, bio, &rs, &nr_regions);
474-
trim_bio(bio, rs << clone->region_shift,
474+
trim_bio(bio, region_to_sector(clone, rs),
475475
nr_regions << clone->region_shift);
476476
generic_make_request(bio);
477477
} else
@@ -804,11 +804,14 @@ static void hydration_copy(struct dm_clone_region_hydration *hd, unsigned int nr
804804
struct dm_io_region from, to;
805805
struct clone *clone = hd->clone;
806806

807+
if (WARN_ON(!nr_regions))
808+
return;
809+
807810
region_size = clone->region_size;
808811
region_start = hd->region_nr;
809812
region_end = region_start + nr_regions - 1;
810813

811-
total_size = (nr_regions - 1) << clone->region_shift;
814+
total_size = region_to_sector(clone, nr_regions - 1);
812815

813816
if (region_end == clone->nr_regions - 1) {
814817
/*

0 commit comments

Comments
 (0)