dm clone: Fix discard handling and overflow bugs which could cause data corruption
cover dm clone: Fix discard handling and overflow bugs which could cause data corruption
There is a bug in the way dm-clone handles partial region discards, which can lead to discarding the wrong blocks or trying to discard blocks beyond the end of the device.
This could lead to data corruption, if the destination device indeed discards the underlying blocks, i.e., if the discard operation results in the original contents of a block to be lost.
The bug manifests when we try to discard part of a single region, i.e., when we try to discard a block with size < region_size, and the discard request both starts at an offset with respect to the beginning of that region and ends before the end of the region.
The root of the bug is the code that calculates the range of regions covered by a discard request and decides which regions to discard.
For more information, please see the relevant commit.
As part of fixing this bug, I also audited dm-clone for other arithmetic/overflow related bugs and found the following:
1. Missing overflow check for the total number of regions
2. Missing casts when converting from regions to sectors
3. Wrong return type of dm_clone_nr_of_hydrated_regions(), which caused an unwanted sign extension to occur.
Again, more information can be found in the relevant commits.
Nikos Tsironis (4):
dm clone: Fix handling of partial region discards
dm clone: Add overflow check for number of regions
dm clone: Add missing casts to prevent overflows and data corruption dm clone metadata: Fix return type of
dm_clone_nr_of_hydrated_regions()
drivers/md/dm-clone-metadata.c | 15 +++++++++- drivers/md/dm-clone-metadata.h | 2 +- drivers/md/dm-clone-target.c | 66 ++++++++++++++++++++++++++++++------------ 3 files changed, 62 insertions(+), 21 deletions(-)
Signature · 1 line
2.11.0
PATCH 1/4 dm clone: Fix handling of partial region discards
There is a bug in the way dm-clone handles discards, which can lead to discarding the wrong blocks or trying to discard blocks beyond the end of the device.
This could lead to data corruption, if the destination device indeed discards the underlying blocks, i.e., if the discard operation results in the original contents of a block to be lost.
The root of the problem is the code that calculates the range of regions covered by a discard request and decides which regions to discard.
Since dm-clone handles the device in units of regions, we don't discard parts of a region, only whole regions.
The range is calculated as:
rs = dm_sector_div_up(bio->bi_iter.bi_sector, clone->region_size);
re = bio_end_sector(bio) >> clone->region_shift;, where 'rs' is the first region to discard and (re - rs) is the number of regions to discard.
The bug manifests when we try to discard part of a single region, i.e., when we try to discard a block with size < region_size, and the discard request both starts at an offset with respect to the beginning of that region and ends before the end of the region.
The root cause is the following comparison:
if (rs == re)
// skip discard and complete original bio immediately
, which doesn't take into account that 'rs' might be greater than 're'.
Thus, we then issue a discard request for the wrong blocks, instead of skipping the discard all together.
Fix the check to also take into account the above case, so we don't end up discarding the wrong blocks.
Also, add some range checks to dm_clone_set_region_hydrated() and dm_clone_cond_set_range(), which update dm-clone's region bitmap.
Note that the aforementioned bug doesn't cause invalid memory accesses, because dm_clone_is_range_hydrated() returns True for this case, so the checks are just precautionary.
--- drivers/md/dm-clone-metadata.c | 13 +++++++++++++ drivers/md/dm-clone-target.c | 43 ++++++++++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 14 deletions(-)
drivers/md/dm-clone-metadata.c +13 -0
@@ -850,6 +850,12 @@ int dm_clone_set_region_hydrated(struct dm_clone_metadata *cmd, unsigned long restruct dirty_map *dmap;unsigned long word, flags; +if (unlikely (region_nr >= cmd->nr_regions)) { +DMERR ("Region %lu out of range (total number of regions %lu)" , + region_nr, cmd->nr_regions); +return -ERANGE ; + } + word = region_nr /BITS_PER_LONG ;spin_lock_irqsave (&cmd->bitmap_lock, flags);
@@ -879,6 +885,13 @@ int dm_clone_cond_set_range(struct dm_clone_metadata *cmd, unsigned long start,struct dirty_map *dmap;unsigned long word, region_nr; +if (unlikely (start >= cmd->nr_regions || (start + nr_regions) < start || + (start + nr_regions) > cmd->nr_regions)) { +DMERR ("Invalid region range: start %lu, nr_regions %lu (total number of regions %lu)" , + start, nr_regions, cmd->nr_regions); +return -ERANGE ; + } +spin_lock_irq (&cmd->bitmap_lock);if (cmd->read_only) {
drivers/md/dm-clone-target.c +29 -14
95 lines not on this page · raw message
Signature · 1 line
2.11.0
PATCH 2/4 dm clone: Add overflow check for number of regions
In reply to Nikos TsironisAdd 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.
--- drivers/md/dm-clone-target.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/md/dm-clone-target.c +11 -1
@@ -1790,6 +1790,7 @@ static int copy_ctr_args(struct clone *clone, int argc, const char **argv, charstatic int clone_ctr (struct dm_target *ti,unsigned int argc,char **argv) {int r; +sector_t nr_regions;struct clone *clone;struct dm_arg_set as;
@@ -1831,7 +1832,16 @@ static int clone_ctr(struct dm_target *ti, unsigned int argc, char **argv) goto out_with_source_dev; clone->region_shift =__ffs (clone->region_size); - clone->nr_regions =dm_sector_div_up (ti->len, clone->region_size); + nr_regions =dm_sector_div_up (ti->len, clone->region_size); + +/* Check for overflow */ +if (nr_regions != (unsigned long )nr_regions) { + ti->error ="Too many regions. Consider increasing the region size" ; + r = -EOVERFLOW ; + goto out_with_source_dev; + } + + clone->nr_regions = nr_regions; r =validate_nr_regions (clone->nr_regions, &ti->error);if (r)
Signature · 1 line
2.11.0
PATCH 3/4 dm clone: Add missing casts to prevent overflows and data corruption
In reply to Nikos TsironisAdd 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.
--- drivers/md/dm-clone-target.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/md/dm-clone-target.c +6 -3
@@ -282,7 +282,7 @@ static bool bio_triggers_commit(struct clone *clone, struct bio *bio)/* Get the address of the region in sectors */ static inline sector_t region_to_sector (struct clone *clone,unsigned long region_nr) { -return (region_nr << clone->region_shift); +return ((sector_t )region_nr << clone->region_shift); }/* Get the region number of the bio */
@@ -471,7 +471,7 @@ static void complete_discard_bio(struct clone *clone, struct bio *bio, bool succif (test_bit (DM_CLONE_DISCARD_PASSDOWN , &clone->flags) && success) {remap_to_dest (clone, bio);bio_region_range (clone, bio, &rs, &nr_regions); -trim_bio (bio, rs << clone->region_shift, +trim_bio (bio,region_to_sector (clone, rs), nr_regions << clone->region_shift);generic_make_request (bio); }else
@@ -804,11 +804,14 @@ static void hydration_copy(struct dm_clone_region_hydration *hd, unsigned int nrstruct dm_io_region from, to;struct clone *clone = hd->clone; +if (WARN_ON (!nr_regions)) +return ; + region_size = clone->region_size; region_start = hd->region_nr; region_end = region_start + nr_regions -1 ; - total_size = (nr_regions -1 ) << clone->region_shift; + total_size =region_to_sector (clone, nr_regions -1 );if (region_end == clone->nr_regions -1 ) {/*
Signature · 1 line
2.11.0
PATCH 4/4 dm clone metadata: Fix return type of dm_clone_nr_of_hydrated_regions()
In reply to Nikos Tsironisdm_clone_nr_of_hydrated_regions() returns the number of regions that have been hydrated so far. In order to do so it employs bitmap_weight().
Until now, the return type of dm_clone_nr_of_hydrated_regions() was unsigned long.
Because bitmap_weight() returns an int, in case BITS_PER_LONG == 64 and the return value of bitmap_weight() is 2^31 (the maximum allowed number of regions for a device), the result is sign extended from 32 bits to 64 bits and an incorrect value is displayed, in the status output of dm-clone, as the number of hydrated regions.
Fix this by having dm_clone_nr_of_hydrated_regions() return an unsigned int.
--- drivers/md/dm-clone-metadata.c | 2 +- drivers/md/dm-clone-metadata.h | 2 +- drivers/md/dm-clone-target.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-)
drivers/md/dm-clone-metadata.c +1 -1
@@ -656,7 +656,7 @@ bool dm_clone_is_range_hydrated(struct dm_clone_metadata *cmd,return (bit >= (start + nr_regions)); } -unsigned long dm_clone_nr_of_hydrated_regions (struct dm_clone_metadata *cmd) +unsigned int dm_clone_nr_of_hydrated_regions (struct dm_clone_metadata *cmd) {return bitmap_weight (cmd->region_map, cmd->nr_regions); }
drivers/md/dm-clone-metadata.h +1 -1
@@ -156,7 +156,7 @@ bool dm_clone_is_range_hydrated(struct dm_clone_metadata *cmd,/* * Returns the number of hydrated regions. */ -unsigned long dm_clone_nr_of_hydrated_regions (struct dm_clone_metadata *cmd); +unsigned int dm_clone_nr_of_hydrated_regions (struct dm_clone_metadata *cmd);/* * Returns the first unhydrated region with region_nr >= @start
drivers/md/dm-clone-target.c +1 -1
@@ -1473,7 +1473,7 @@ static void clone_status(struct dm_target *ti, status_type_t type, goto error; } - DMEMIT("%u %llu/%llu %llu %lu/%lu %u ", + DMEMIT("%u %llu/%llu %llu %u/%lu %u ", DM_CLONE_METADATA_BLOCK_SIZE, (unsigned long long)(nr_metadata_blocks - nr_free_metadata_blocks), (unsigned long long)nr_metadata_blocks,
Signature · 1 line
2.11.0