Skip to content

Commit b86f4b7

Browse files
Mikulas PatockaMike Snitzer
authored andcommitted
dm-integrity: don't modify bio's immutable bio_vec in integrity_metadata()
__bio_for_each_segment assumes that the first struct bio_vec argument doesn't change - it calls "bio_advance_iter_single((bio), &(iter), (bvl).bv_len)" to advance the iterator. Unfortunately, the dm-integrity code changes the bio_vec with "bv.bv_len -= pos". When this code path is taken, the iterator would be out of sync and dm-integrity would report errors. This happens if the machine is out of memory and "kmalloc" fails. Fix this bug by making a copy of "bv" and changing the copy instead. Fixes: 7eada90 ("dm: add integrity target") Cc: [email protected] # v4.12+ Signed-off-by: Mikulas Patocka <[email protected]> Signed-off-by: Mike Snitzer <[email protected]>
1 parent db29d79 commit b86f4b7

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

drivers/md/dm-integrity.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,11 +1755,12 @@ static void integrity_metadata(struct work_struct *w)
17551755
sectors_to_process = dio->range.n_sectors;
17561756

17571757
__bio_for_each_segment(bv, bio, iter, dio->bio_details.bi_iter) {
1758+
struct bio_vec bv_copy = bv;
17581759
unsigned int pos;
17591760
char *mem, *checksums_ptr;
17601761

17611762
again:
1762-
mem = bvec_kmap_local(&bv);
1763+
mem = bvec_kmap_local(&bv_copy);
17631764
pos = 0;
17641765
checksums_ptr = checksums;
17651766
do {
@@ -1768,7 +1769,7 @@ static void integrity_metadata(struct work_struct *w)
17681769
sectors_to_process -= ic->sectors_per_block;
17691770
pos += ic->sectors_per_block << SECTOR_SHIFT;
17701771
sector += ic->sectors_per_block;
1771-
} while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
1772+
} while (pos < bv_copy.bv_len && sectors_to_process && checksums != checksums_onstack);
17721773
kunmap_local(mem);
17731774

17741775
r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
@@ -1793,9 +1794,9 @@ static void integrity_metadata(struct work_struct *w)
17931794
if (!sectors_to_process)
17941795
break;
17951796

1796-
if (unlikely(pos < bv.bv_len)) {
1797-
bv.bv_offset += pos;
1798-
bv.bv_len -= pos;
1797+
if (unlikely(pos < bv_copy.bv_len)) {
1798+
bv_copy.bv_offset += pos;
1799+
bv_copy.bv_len -= pos;
17991800
goto again;
18001801
}
18011802
}

0 commit comments

Comments
 (0)