Skip to content

Commit 3b66455

Browse files
Mikulas Patockagregkh
authored andcommitted
dm-integrity: don't modify bio's immutable bio_vec in integrity_metadata()
commit b86f4b7 upstream. __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: stable@vger.kernel.org # v4.12+ Signed-off-by: Mikulas Patocka <mpatocka@redhat.com> Signed-off-by: Mike Snitzer <snitzer@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7e39c55 commit 3b66455

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

drivers/md/dm-integrity.c

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

17671767
__bio_for_each_segment(bv, bio, iter, dio->bio_details.bi_iter) {
1768+
struct bio_vec bv_copy = bv;
17681769
unsigned int pos;
17691770
char *mem, *checksums_ptr;
17701771

17711772
again:
1772-
mem = bvec_kmap_local(&bv);
1773+
mem = bvec_kmap_local(&bv_copy);
17731774
pos = 0;
17741775
checksums_ptr = checksums;
17751776
do {
@@ -1778,7 +1779,7 @@ static void integrity_metadata(struct work_struct *w)
17781779
sectors_to_process -= ic->sectors_per_block;
17791780
pos += ic->sectors_per_block << SECTOR_SHIFT;
17801781
sector += ic->sectors_per_block;
1781-
} while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
1782+
} while (pos < bv_copy.bv_len && sectors_to_process && checksums != checksums_onstack);
17821783
kunmap_local(mem);
17831784

17841785
r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
@@ -1803,9 +1804,9 @@ static void integrity_metadata(struct work_struct *w)
18031804
if (!sectors_to_process)
18041805
break;
18051806

1806-
if (unlikely(pos < bv.bv_len)) {
1807-
bv.bv_offset += pos;
1808-
bv.bv_len -= pos;
1807+
if (unlikely(pos < bv_copy.bv_len)) {
1808+
bv_copy.bv_offset += pos;
1809+
bv_copy.bv_len -= pos;
18091810
goto again;
18101811
}
18111812
}

0 commit comments

Comments
 (0)