Skip to contentSkip to conversation

md/raid10: fix stale bi_offset in sync_request_write()

PATCH Applied Zhou Minqiang 2 participants 2 messages Nov 26 – Nov 29 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 md/raid10: fix stale bi_offset in sync_request_write()

sync_request_write() reuses the bio that completed the resync read (fbio) as the source bio of bio_copy_data(). Commit cc57858831e3 ("md/raid10:
fix data corruption and crash during resync") restored bi_iter.bi_size and bi_iter.bi_idx, but missed bi_iter.bi_offset. When bi_offset is non-zero, bio_copy_data() starts copying from the middle of the first bvec and walks past the end of the bvec table:

    BUG: KASAN: wild-memory-access in bio_copy_data+0x3d4/0x8f0
    Read of size 1919 at addr 00050880ffff8881 by task md0_raid10/2674
    CPU: 7 UID: 0 PID: 2674 Comm: md0_raid10 Not tainted
    Call Trace:
      bio_copy_data
      raid10d
      md_thread

Commit d4aa57a1cac3 ("block: don't bother iter advancing a fully done bio") added a fast path to bio_advance(): a bio that completes its full length returns without calling __bio_advance(), which makes a stale bi_offset possible and lets this bug be triggered.

The residue is produced when the block layer splits the resync bio inside a merged bvec. The resync bio is built in raid10_sync_request() with a series of bio_add_page() calls, and bio_add_page() merges physically contiguous pages into one bvec. blk_mq_submit_bio() then passes the bio to __bio_split_to_limits(), where bio_may_need_split() checks the merged bvec against max_fast_segment_size (4096 bytes on loop), and exceeding it forces bio_split_rw() to cut in the middle of the bvec and resubmit the remainder. With all 16 resync pages merged into a single bvec:

split 1:  orig 65536 offset=0     -> frag 4096 | orig 61440 offset=4096
split 2:  orig 61440 offset=4096  -> frag 4096 | orig 57344 offset=8192

...
split 15: orig 8192 offset=57344 -> frag 4096 | orig 4096 offset=61440 no split: orig 4096 offset=61440

After these 15 splits, there is nothing left to cut, so the remaining 4096 bytes are submitted as the original bio itself. When it completes, blk_update_request() calls bio_advance() with nbytes equal to bi_size, which takes the fast path and skips __bio_advance():

blk_mq_end_request
blk_update_request
bio_advance(bio, 4096) /* 4096 == bi_size, fast path */
bi_size = 0 /* bi_offset left at 61440 */

sync_request_write() then resets bi_idx to 0 but keeps bi_offset at 61440, so bio_copy_data() starts at offset 61440 of bvec 0 while claiming to copy the full 64 KiB, and the crash above follows.

Reuse fbio with bio_reuse() so the iterator always starts at the beginning of the first bvec, and no other field is left stale.

Fixes cc57858831e3 ("md/raid10: fix data corruption and crash during resync")
Fixes d4aa57a1cac3 ("block: don't bother iter advancing a fully done bio")

---
v2:
- use bio_reuse() instead of clearing bi_offset directly
v1: https://lore.kernel.org/all/[email protected]/

 drivers/md/raid10.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/md/raid10.c +1 -2
index 066c43154497..085998fd35de 100644
@@ -2328,8 +2328,7 @@ static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
 
 	first = i;
 	fbio = r10_bio->devs[i].bio;
-	fbio->bi_iter.bi_size = r10_bio->sectors << 9;
-	fbio->bi_iter.bi_idx = 0;
+	bio_reuse(fbio, fbio->bi_opf);
 	fpages = get_resync_pages(fbio)->pages;
 
 	vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
Signature · 1 line
2.52.0
maintainer Applied
#1 lore raw
Quoted 55 lines · Zhou Minqiang

sync_request_write() reuses the bio that completed the resync read (fbio) as the source bio of bio_copy_data(). Commit cc57858831e3 ("md/raid10:
fix data corruption and crash during resync") restored bi_iter.bi_size and bi_iter.bi_idx, but missed bi_iter.bi_offset. When bi_offset is non-zero, bio_copy_data() starts copying from the middle of the first bvec and walks past the end of the bvec table:

     BUG: KASAN: wild-memory-access in bio_copy_data+0x3d4/0x8f0
     Read of size 1919 at addr 00050880ffff8881 by task md0_raid10/2674
     CPU: 7 UID: 0 PID: 2674 Comm: md0_raid10 Not tainted
     Call Trace:
       bio_copy_data
       raid10d
       md_thread

Commit d4aa57a1cac3 ("block: don't bother iter advancing a fully done bio") added a fast path to bio_advance(): a bio that completes its full length returns without calling __bio_advance(), which makes a stale bi_offset possible and lets this bug be triggered.

The residue is produced when the block layer splits the resync bio inside a merged bvec. The resync bio is built in raid10_sync_request() with a series of bio_add_page() calls, and bio_add_page() merges physically contiguous pages into one bvec. blk_mq_submit_bio() then passes the bio to __bio_split_to_limits(), where bio_may_need_split() checks the merged bvec against max_fast_segment_size (4096 bytes on loop), and exceeding it forces bio_split_rw() to cut in the middle of the bvec and resubmit the remainder. With all 16 resync pages merged into a single bvec:

split 1:  orig 65536 offset=0     -> frag 4096 | orig 61440 offset=4096
split 2:  orig 61440 offset=4096  -> frag 4096 | orig 57344 offset=8192

...
split 15: orig 8192 offset=57344 -> frag 4096 | orig 4096 offset=61440 no split: orig 4096 offset=61440

After these 15 splits, there is nothing left to cut, so the remaining 4096 bytes are submitted as the original bio itself. When it completes, blk_update_request() calls bio_advance() with nbytes equal to bi_size, which takes the fast path and skips __bio_advance():

blk_mq_end_request
blk_update_request
bio_advance(bio, 4096) /* 4096 == bi_size, fast path */
bi_size = 0 /* bi_offset left at 61440 */

sync_request_write() then resets bi_idx to 0 but keeps bi_offset at 61440, so bio_copy_data() starts at offset 61440 of bvec 0 while claiming to copy the full 64 KiB, and the crash above follows.

Reuse fbio with bio_reuse() so the iterator always starts at the beginning of the first bvec, and no other field is left stale.

Fixes cc57858831e3 ("md/raid10: fix data corruption and crash during resync")
Fixes d4aa57a1cac3 ("block: don't bother iter advancing a fully done bio")
Quoted 7 lines

---
v2:
- use bio_reuse() instead of clearing bi_offset directly
v1:https://lore.kernel.org/all/[email protected]/

  drivers/md/raid10.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)

Applied

Signature · 2 lines
Thanks,
Kuai