]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
dm-integrity: don't modify bio's immutable bio_vec in integrity_metadata()
authorMikulas Patocka <mpatocka@redhat.com>
Tue, 5 Dec 2023 15:39:16 +0000 (16:39 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 8 Jan 2024 10:25:04 +0000 (11:25 +0100)
commit b86f4b790c998afdbc88fe1aa55cfe89c4068726 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: 7eada909bfd7 ("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>
drivers/md/dm-integrity.c

index 94ab60397c958bdfb212b6b80a3398248ffc7d19..8af3ec3c692e6bb4550eaecb411500c8e0a53231 100644 (file)
@@ -1257,11 +1257,12 @@ static void integrity_metadata(struct work_struct *w)
                        checksums = checksums_onstack;
 
                __bio_for_each_segment(bv, bio, iter, dio->orig_bi_iter) {
+                       struct bio_vec bv_copy = bv;
                        unsigned pos;
                        char *mem, *checksums_ptr;
 
 again:
-                       mem = (char *)kmap_atomic(bv.bv_page) + bv.bv_offset;
+                       mem = (char *)kmap_atomic(bv_copy.bv_page) + bv_copy.bv_offset;
                        pos = 0;
                        checksums_ptr = checksums;
                        do {
@@ -1270,7 +1271,7 @@ again:
                                sectors_to_process -= ic->sectors_per_block;
                                pos += ic->sectors_per_block << SECTOR_SHIFT;
                                sector += ic->sectors_per_block;
-                       } while (pos < bv.bv_len && sectors_to_process && checksums != checksums_onstack);
+                       } while (pos < bv_copy.bv_len && sectors_to_process && checksums != checksums_onstack);
                        kunmap_atomic(mem);
 
                        r = dm_integrity_rw_tag(ic, checksums, &dio->metadata_block, &dio->metadata_offset,
@@ -1290,9 +1291,9 @@ again:
                        if (!sectors_to_process)
                                break;
 
-                       if (unlikely(pos < bv.bv_len)) {
-                               bv.bv_offset += pos;
-                               bv.bv_len -= pos;
+                       if (unlikely(pos < bv_copy.bv_len)) {
+                               bv_copy.bv_offset += pos;
+                               bv_copy.bv_len -= pos;
                                goto again;
                        }
                }