direct-io: validate user space vectors during extraction
Subsystems (7)
cover direct-io: validate user space vectors during extraction
From: Keith Busch <[email protected]>
This addresses the misaligned direct-io problem behind various threads:
https://lore.kernel.org/linux-xfs/[email protected]/
https://lore.kernel.org/all/CAC_j7i1R7oy+nRhxEjCTba=DUgn02w9X+p94DCu0aHv5+5tKnQ@mail.gmail.com/
https://lore.kernel.org/linux-block/ai7rnH20IYeSmY8s@gallifrey/
https://lore.kernel.org/linux-block/[email protected]/
The various tested fixes are correct as far as they go, but they treat the
symptom: they only matter because an invalid bio reaches those drivers in the first place.
The reason it reaches them is an assumption I made when I removed direct-io alignment checks in 5ff3f74e145a ("block: simplify direct io validity check") and 7eac331869575 ("iomap: simplify direct io validity check"): every bio is eventually split to the device limits, and the upper layers cope with resulting errors once the bio has formed. Both were optimistic assumptions. Drivers with their own ->submit_bio may never pass through blk_mq_submit_bio()'s split, so the check never runs for them, and as numerous threads showed, the consumers don't uniformly handle this condition.
This patch stops the invalid bio at the source instead. It validates the buffer's alignment against the alignment limits when the bio is built from the iov_iter. The check is folded into the bvec extraction that already walks the vectors, so it adds only a comparison on a path that is pinning direct-io pages anyway. Misalignment is now uniformly rejected with EINVAL before submission for every direct-io submission path.
With this in place, the dm side changes under discussion are no longer required to fix the bugs: the affected targets simply never see the invalid bio. The tested patches remain reasonable as defense-in-depth if desired, but they are not strictly necessary after this.
Keith Busch (1):
block: validate user space vectors during extraction
block/bio.c | 19 ++++++++++++++++--- block/blk-map.c | 2 +- block/fops.c | 3 ++- fs/iomap/direct-io.c | 3 ++- include/linux/bio.h | 2 +- include/linux/uio.h | 3 ++- lib/iov_iter.c | 9 ++++++++- 7 files changed, 32 insertions(+), 9 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/1 block: validate user space vectors during extraction
From: Keith Busch <[email protected]>
The blk-mq based drivers have every incoming bio validated by an unconditional __bio_split_to_limits() call, which rejects any segment that does not meet the queue's dma_alignment with BLK_STS_INVAL, so they only see viable requests. A bio-based driver, though, receives a bio whose memory alignment has not been checked.
Misalignment is possible for vectors supplied from user space direct-io. When a stacking driver forwards a misaligned bio to a member device, that member may reject it with BLK_STS_INVAL if the lower level attempts to split the bio to the queue limits. The stacker tends to mishandle the
error: dm-raid1 may degrade an otherwise healthy array.
Alternatively, some lower level bio based block drivers never attempt to split their bio and assume the one received is viable. If it's unaligned, block devices like brd and pmem may corrupt their data as they have a strong dependency on sector size aligned bvecs.
Validate the source against the device's dma_alignment where the bio is built from the iov_iter, rejecting misaligned I/O with -EINVAL before it is submitted. This is done opportunistically in a path that already pins the pages, so no additional io vector walking is needed.
The required alignment is supplied by the callers as vec_align_mask (bdev_dma_alignment()); passthrough and the bounce path pass 0 as they have no such requirement. If a vector is misaligned while building the bio, any pages already pinned into that bio are released before returning.
--- block/bio.c | 19 ++++++++++++++++--- block/blk-map.c | 2 +- block/fops.c | 3 ++- fs/iomap/direct-io.c | 3 ++- include/linux/bio.h | 2 +- include/linux/uio.h | 3 ++- lib/iov_iter.c | 9 ++++++++- 7 files changed, 32 insertions(+), 9 deletions(-)
block/bio.c +16 -3
@@ -1242,7 +1242,7 @@ static int bio_iov_iter_align_down(struct bio *bio, struct iov_iter *iter,* is returned only if 0 pages could be pinned. */ int bio_iov_iter_get_pages (struct bio *bio,struct iov_iter *iter, -unsigned len_align_mask) +unsigned len_align_mask,unsigned vec_align_mask) {iov_iter_extraction_t flags =0 ;
@@ -1251,6 +1251,11 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter,if (iov_iter_is_bvec (iter)) {bio_iov_bvec_set (bio, iter); + +if (mp_bvec_iter_offset (bio->bi_io_vec, bio->bi_iter) & + vec_align_mask) +return -EINVAL ; +iov_iter_advance (iter, bio->bi_iter.bi_size);return 0 ; }
@@ -1265,8 +1270,16 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter, ret =iov_iter_extract_bvecs (iter, bio->bi_io_vec,BIO_MAX_SIZE - bio->bi_iter.bi_size, - &bio->bi_vcnt, bio->bi_max_vecs, flags); + &bio->bi_vcnt, bio->bi_max_vecs, + vec_align_mask, flags);if (ret <=0 ) { +if (ret == -EINVAL ) { +bio_release_pages (bio, false); +bio_clear_flag (bio,BIO_PAGE_PINNED ); + bio->bi_iter.bi_size =0 ; + bio->bi_vcnt =0 ; +return ret; + }if (!bio->bi_vcnt)return ret;break ;
@@ -1377,7 +1390,7 @@ static int bio_iov_iter_bounce_read(struct bio *bio, struct iov_iter *iter,ssize_t ret; ret =iov_iter_extract_bvecs (iter, bio->bi_io_vec +1 , len, - &bio->bi_vcnt, bio->bi_max_vecs -1 ,0 ); + &bio->bi_vcnt, bio->bi_max_vecs -1 ,0 ,0 );if (ret <=0 ) {if (!bio->bi_vcnt) {folio_put (folio);
block/blk-map.c +1 -1
@@ -274,7 +274,7 @@ static int bio_map_user_iov(struct request *rq, struct iov_iter *iter,* No alignment requirements on our part to support arbitrary * passthrough commands. */ - ret =bio_iov_iter_get_pages (bio, iter,0 ); + ret =bio_iov_iter_get_pages (bio, iter,0 ,0 );if (ret) goto out_put; ret =blk_rq_append_bio (rq, bio);
block/fops.c +2 -1
@@ -47,7 +47,8 @@ static inline int blkdev_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter, struct block_device *bdev) { return bio_iov_iter_get_pages(bio, iter, - bdev_logical_block_size(bdev) - 1); + bdev_logical_block_size(bdev) - 1, +bdev_dma_alignment (bdev )); }#define DIO_INLINE_BIO_VECS 4
fs/iomap/direct-io.c +2 -1
@@ -358,7 +358,8 @@ static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter, iomap_max_bio_size(&iter->iomap), alignment); else ret =bio_iov_iter_get_pages (bio, dio->submit.iter, - alignment -1 ); + alignment -1 , +bdev_dma_alignment (bio->bi_bdev));if (unlikely (ret)) goto out_put_bio; ret = bio->bi_iter.bi_size;
include/linux/bio.h +1 -1
@@ -477,7 +477,7 @@ int bdev_rw_virt(struct block_device *bdev, sector_t sector, void *data, size_t len, enum req_op op);int bio_iov_iter_get_pages (struct bio *bio,struct iov_iter *iter, -unsigned len_align_mask); +unsigned len_align_mask,unsigned vec_align_mask);void bio_iov_bvec_set (struct bio *bio,const struct iov_iter *iter);void __bio_release_pages (struct bio *bio,bool mark_dirty);
include/linux/uio.h +2 -1
@@ -391,7 +391,8 @@ ssize_t iov_iter_extract_pages(struct iov_iter *i, struct page ***pages, size_t *offset0);ssize_t iov_iter_extract_bvecs (struct iov_iter *iter,struct bio_vec *bv,size_t max_size,unsigned short *nr_vecs, -unsigned short max_vecs,iov_iter_extraction_t extraction_flags); +unsigned short max_vecs,unsigned align_mask, +iov_iter_extraction_t extraction_flags);/** * iov_iter_extract_will_pin - Indicate how pages from the iterator will be retained
lib/iov_iter.c +8 -1
@@ -1886,6 +1886,8 @@ static unsigned int get_contig_folio_len(struct page **pages,* @max_size: maximum size to extract from @iter * @nr_vecs: number of vectors in @bv (on in and output) * @max_vecs: maximum vectors in @bv, including those filled before calling +* @align_mask: reject with -EINVAL if the source address or length is not +* aligned to this mask * @extraction_flags: flags to qualify request * * Like iov_iter_extract_pages(), but returns physically contiguous ranges
@@ -1897,14 +1899,19 @@ static unsigned int get_contig_folio_len(struct page **pages,*/ ssize_t iov_iter_extract_bvecs (struct iov_iter *iter,struct bio_vec *bv,size_t max_size,unsigned short *nr_vecs, -unsigned short max_vecs,iov_iter_extraction_t extraction_flags) +unsigned short max_vecs,unsigned align_mask, +iov_iter_extraction_t extraction_flags) { +unsigned long start = (unsigned long )iter_iov_addr (iter);unsigned short entries_left = max_vecs - *nr_vecs;unsigned short nr_pages, i =0 ;size_t left, offset, len;struct page **pages;ssize_t size; +if ((start |iter_iov_len (iter)) & align_mask) +return -EINVAL ; +/* * Move page array up in the allocated memory for the bio vecs as far as * possible so that we can start filling biovecs from the beginning
Signature · 1 line
2.52.0
kernel test robot
build botRe: [PATCH 1/1] block: validate user space vectors during extraction
kernel test robot noticed the following build warnings:
Hi Keith,
kernel test robot noticed the following build warnings:
[auto build test WARNING on axboe/for-next]
[also build test WARNING on brauner-vfs/vfs.all akpm-mm/mm-nonmm-unstable linus/master v7.1 next-20260616] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Keith-Busch/block-validate-user-space-vectors-during-extraction/20260618-073522 base: https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux.git for-next patch link: https://lore.kernel.org/r/20260617233235.1016063-2-kbusch%40meta.com patch subject: [PATCH 1/1] block: validate user space vectors during extraction config: x86_64-kexec (https://download.01.org/0day-ci/archive/20260618/[email protected]/config) compiler: clang version 22.1.8 (https://github.com/llvm/llvm-project ca7933e47d3a3451d81e72ac174dcb5aa28b59d1) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260618/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: block/bio.c:1245 function parameter 'vec_align_mask' not described in 'bio_iov_iter_get_pages' >> Warning: block/bio.c:1245 function parameter 'vec_align_mask' not described in 'bio_iov_iter_get_pages'
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 1/1] block: validate user space vectors during extraction
In reply to Keith Buschblock/bio.c static int bio_iov_iter_align_down(struct bio *bio, struct iov_iter *iter, · line 1245
Quoted 6 lines · Keith Busch
block/bio.c +1
-1@@ -1242,7 +1242,7 @@ static int bio_iov_iter_align_down(struct bio *bio, struct iov_iter *iter,* is returned only if 0 pages could be pinned. */ int bio_iov_iter_get_pages (struct bio *bio,struct iov_iter *iter, -unsigned len_align_mask) +unsigned len_align_mask,unsigned vec_align_mask)
vec_align_mask needs to be documented in the kernel doc. And I find the vec_align_mask name a bit confusing. This is all about the physical address (really the dma address, but the page aligned offset map 1:1), so maybe phys_align_mask or dma_align_mask might be better names?
Also wouldn't it be more natural to pass the start alignment requirement before the length alignment paramter?
block/bio.c int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter, · line 1257
Quoted 8 lines
block/bio.c +4
-0@@ -1251,6 +1251,11 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter,if (iov_iter_is_bvec (iter)) {bio_iov_bvec_set (bio, iter); + +if (mp_bvec_iter_offset (bio->bi_io_vec, bio->bi_iter) & + vec_align_mask) +return -EINVAL ;
Can you add a comment here? Especially as the bvec iter doesn't actually require all individual bvecs to be aligned and I'm not entirely sure this handles all case - writing down the rules might help a bit with that.
block/bio.c int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter, · line 1282
Quoted 13 lines
block/bio.c +2
-1ret =iov_iter_extract_bvecs (iter, bio->bi_io_vec,BIO_MAX_SIZE - bio->bi_iter.bi_size, - &bio->bi_vcnt, bio->bi_max_vecs, flags); + &bio->bi_vcnt, bio->bi_max_vecs, + vec_align_mask, flags);block/bio.c +7
-0if (ret <=0 ) { +if (ret == -EINVAL ) { +bio_release_pages (bio, false); +bio_clear_flag (bio,BIO_PAGE_PINNED ); + bio->bi_iter.bi_size =0 ; + bio->bi_vcnt =0 ; +return ret; + }
Do we need all this cleanups beyoned the bio_release_pages()? Most callers just free the bio, so should not care about it, and the error handling in __blkdev_direct_IO that calls bio_endio looks buggy for other reasons..
Quoted 2 lines
+ * @align_mask: reject with
-EINVALif the source address or length is not
+ * aligned to this mask
Maybe use the same paramater name as on the bio side here?
And not for this patch, but this makes me wonder if we should handle the len alignment in iov_iter_extract_bvecs as well, as that should simplify it quite a bit.
Re: [PATCH 1/1] block: validate user space vectors during extraction
Quoted 13 lines · Christoph Hellwig
block/bio.c int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter, · line 1257
Quoted 8 lines · Keith Busch
block/bio.c +4
-0@@ -1251,6 +1251,11 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter,if (iov_iter_is_bvec (iter)) {bio_iov_bvec_set (bio, iter); + +if (mp_bvec_iter_offset (bio->bi_io_vec, bio->bi_iter) & + vec_align_mask) +return -EINVAL ;Can you add a comment here? Especially as the bvec iter doesn't actually require all individual bvecs to be aligned and I'm not entirely sure this handles all case - writing down the rules might help a bit with that.
The rationale is that the only iter_bvec users come from io_uring registered buffers, which are virtually contiguous. Subsequent IO referencing it provides only an offset and a length, so the only possible unlaignment could bne the first offset (we've already verified the total length earlier). Every subsequent vector must be page aligned at a minimum, which is the largest possible dma alignment the block layer allows, so we don't need to check the rest.
Quoted 10 lines
Quoted 10 lines
block/bio.c +2
-1ret =iov_iter_extract_bvecs (iter, bio->bi_io_vec,BIO_MAX_SIZE - bio->bi_iter.bi_size, - &bio->bi_vcnt, bio->bi_max_vecs, flags); + &bio->bi_vcnt, bio->bi_max_vecs, + vec_align_mask, flags);block/bio.c +4
-0if (ret <=0 ) { +if (ret == -EINVAL ) { +bio_release_pages (bio, false); +bio_clear_flag (bio,BIO_PAGE_PINNED ); + bio->bi_iter.bi_size =0 ;
Quoted 8 lines
block/bio.c int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter, · line 1282
Quoted 3 lines
block/bio.c +3
-0+ bio->bi_vcnt =0 ; +return ret; + }Do we need all this cleanups beyoned the
bio_release_pages()? Most callers just free the bio, so should not care about it, and the error handling in__blkdev_direct_IOthat callsbio_endiolooks buggy for other reasons..
Yeah, it's exactly for the __blkdev_direct_IO() error handling, though I think clearing either the PINNED flag or bi_vcnt is sufficient after bio_release_pages(). The rest is just resetting the bio to the initial state since I didn't want to return both an error and something that looks like a partially constructed bio, even if no one currently cares.
But since you mention it, __blkdev_direct_IO's handling does look wrong, so maybe I can clean that up first.
Re: [PATCH 1/1] block: validate user space vectors during extraction
Quoted 11 lines · Keith Busch
Quoted 10 lines
block/bio.c int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter, · line 1257
Quoted 6 lines
block/bio.c +4
-0if (iov_iter_is_bvec (iter)) {bio_iov_bvec_set (bio, iter); + +if (mp_bvec_iter_offset (bio->bi_io_vec, bio->bi_iter) & + vec_align_mask) +return -EINVAL ;Can you add a comment here? Especially as the bvec iter doesn't actually require all individual bvecs to be aligned and I'm not entirely sure this handles all case - writing down the rules might help a bit with that.
Quoted 2 lines
The rationale is that the only
iter_bvecusers come fromio_uringregistered buffers, which are virtually contiguous.
There's plenty of iov_iter_bdev users, and even without poking deep I know that two directly passed on bvecs from block-layer generated bios to the underlying file system's direct I/O code: loop and zloop.
So we need rules on what can be passed, and preferably some way to enforce that at least for debug builds.
Re: [PATCH 1/1] block: validate user space vectors during extraction
Quoted 12 lines · Christoph Hellwig
Quoted 11 lines · Keith Busch
Quoted 10 lines
block/bio.c int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter, · line 1257
Quoted 6 lines
block/bio.c +4
-0if (iov_iter_is_bvec (iter)) {bio_iov_bvec_set (bio, iter); + +if (mp_bvec_iter_offset (bio->bi_io_vec, bio->bi_iter) & + vec_align_mask) +return -EINVAL ;Can you add a comment here? Especially as the bvec iter doesn't actually require all individual bvecs to be aligned and I'm not entirely sure this handles all case - writing down the rules might help a bit with that.
Quoted 6 lines
Quoted 2 lines
The rationale is that the only
iter_bvecusers come fromio_uringregistered buffers, which are virtually contiguous.There's plenty of
iov_iter_bdevusers, and even without poking deep I know that two directly passed on bvecs from block-layer generated bios to the underlying file system's direct I/O code: loop and zloop.
Oh, I meant only users that go through this direct-io path, but you're right, I was wrong about that too. The nvme target file backend can also get here in addition to what you pointed out.
Quoted 2 lines
So we need rules on what can be passed, and preferably some way to enforce that at least for debug builds.
Yeah.
kernel test robot
build botRe: [PATCH 1/1] block: validate user space vectors during extraction
In reply to Keith Buschkernel test robot noticed the following build warnings:
Hi Keith,
kernel test robot noticed the following build warnings:
[auto build test WARNING on axboe/for-next]
[also build test WARNING on brauner-vfs/vfs.all akpm-mm/mm-nonmm-unstable linus/master v7.1 next-20260619] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Keith-Busch/block-validate-user-space-vectors-during-extraction/20260618-073522 base: https://git.kernel.org/pub/scm/linux/kernel/git/axboe/linux.git for-next patch link: https://lore.kernel.org/r/20260617233235.1016063-2-kbusch%40meta.com patch subject: [PATCH 1/1] block: validate user space vectors during extraction config: openrisc-allnoconfig (https://download.01.org/0day-ci/archive/20260622/[email protected]/config) compiler: or1k-linux-gcc (GCC) 16.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260622/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: block/bio.c:1245 function parameter 'vec_align_mask' not described in 'bio_iov_iter_get_pages' >> Warning: block/bio.c:1245 function parameter 'vec_align_mask' not described in 'bio_iov_iter_get_pages'
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 1/1] block: validate user space vectors during extraction
In reply to Keith BuschQuoted 3 lines · Keith Busch
But since you mention it, __blkdev_direct_IO's handling does look wrong, so maybe I can clean that up first.
After careful reviewing, I think __blkdev_direct_IO() is mostly correct in what it's doing. It looks weird, but appears to be well optimized for the common case such that making it look more readable would produce less efficient execution code. So I'm not going to touch it, but there is a bug here with metadata mapping error handling that I'm going to propose in the next version.