Skip to contentSkip to conversation

block: check bio split for unaligned bvec

PATCH Applied Keith Busch 5 participants 6 messages Jun 12 – Jun 16 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: check bio split for unaligned bvec

From: Keith Busch <[email protected]>

Offsets and lengths need to be validated against the dma alignment. This check was skipped for sufficiently a small bio with a single bvec, which may allow an invalid request dispatched to the driver. Force the validation for an unaligned bvec by forcing the bio split path that handles this condition.

Fixes 7eac33186957 ("iomap: simplify direct io validity check")
Fixes 5ff3f74e145a ("block: simplify direct io validity check")
---
 block/blk.h | 2 ++
 1 file changed, 2 insertions(+)
block/blk.h +2 -0
index 1a2d9101bba04..004048fa0c5a8 100644
@@ -404,6 +404,8 @@ static inline bool bio_may_need_split(struct bio *bio,
 	bv = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
 	if (bio->bi_iter.bi_size > bv->bv_len - bio->bi_iter.bi_bvec_done)
 		return true;
+	if ((bv->bv_offset | bv->bv_len) & lim->dma_alignment)
+		return true;
 	return bv->bv_len + bv->bv_offset > lim->max_fast_segment_size;
 }
 
Signature · 1 line
2.52.0
maintainer Applied
#1 lore raw
Quoted 8 lines · Keith Busch

Offsets and lengths need to be validated against the dma alignment. This check was skipped for sufficiently a small bio with a single bvec, which may allow an invalid request dispatched to the driver. Force the validation for an unaligned bvec by forcing the bio split path that handles this condition.

[...]

Applied, thanks!

[1/1] block: check bio split for unaligned bvec

      commit: 9b0c3673c88588d613d8f09f5931b2b466c6a83d

Best regards,

Signature · 1 line
Jens Axboe
Tested-by Reviewed-by on v1 1/1
#2 lore raw
In reply to Keith Busch
Quoted 8 lines · Keith Busch

From: Keith Busch <[email protected]>

Offsets and lengths need to be validated against the dma alignment. This check was skipped for sufficiently a small bio with a single bvec, which may allow an invalid request dispatched to the driver. Force the validation for an unaligned bvec by forcing the bio split path that handles this condition.

Quoted 4 lines
Fixes 7eac33186957 ("iomap: simplify direct io validity check")
Fixes 5ff3f74e145a ("block: simplify direct io validity check")

Jens was quick enough but if needed anyway, I've tested this locally, so:

Quoted 20 lines
---
 block/blk.h | 2 ++
 1 file changed, 2 insertions(+)
block/blk.h +2 -0
index 1a2d9101bba04..004048fa0c5a8 100644
@@ -404,6 +404,8 @@ static inline bool bio_may_need_split(struct bio *bio,
 	bv = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
 	if (bio->bi_iter.bi_size > bv->bv_len - bio->bi_iter.bi_bvec_done)
 		return true;
+	if ((bv->bv_offset | bv->bv_len) & lim->dma_alignment)
+		return true;
 	return bv->bv_len + bv->bv_offset > lim->max_fast_segment_size;
 }
 
Signature · 1 line
2.52.0
#3 lore raw
In reply to Keith Busch
Quoted 7 lines · Keith Busch

From: Keith Busch <[email protected]>

Offsets and lengths need to be validated against the dma alignment. This check was skipped for sufficiently a small bio with a single bvec, which may allow an invalid request dispatched to the driver. Force the validation for an unaligned bvec by forcing the bio split path that handles this condition.

This fix itself looks good, but we'll also need something similar for bio-based drivers that never call into the splitting helper.

#4 lore raw
Quoted 11 lines · Christoph Hellwig
Quoted 7 lines · Keith Busch

From: Keith Busch <[email protected]>

Offsets and lengths need to be validated against the dma alignment. This check was skipped for sufficiently a small bio with a single bvec, which may allow an invalid request dispatched to the driver. Force the validation for an unaligned bvec by forcing the bio split path that handles this condition.

This fix itself looks good, but we'll also need something similar for bio-based drivers that never call into the splitting helper.

Totally agree. I'm looking at all the .submit_bio drivers, and I think they fall into one of four catagories:

1: already split (md/nvme-mp/drbd; dm conditional)
2: don't split
* btt, dcssblk: already reject unaligned
* n64cart: WARNs, but potentially proceeds to undefined behavior
* nfhd: silently corrupts, but looks like a driver problem
3: can handle arbitrary memory but advertise default dma_alignment=511
(brd, pmem, zram, ps3vram, simdisk - "limits lie")
4: forward/self-split (bcache)

I think the block layer can fix 3 with a BLK_FEAT flag to allow a zero dma_alignment limit for the drivers that really don't need it from the source buffer.

As for the rest, I don't know of anyone caring to ensure n64 or nfhd are correctly handling degenerate applications.

#5 lore raw
Quoted 2 lines · Keith Busch

3: can handle arbitrary memory but advertise default dma_alignment=511
(brd, pmem, zram, ps3vram, simdisk - "limits lie")

That's actually not right because they iterate with bio_for_each_segment, which requires all the bv_len's are sector size granularity, so their default limits are correct. Just no one's enforcing them right now.