Skip to contentSkip to conversation

block: Fix start and length check added to iov_iter_extract_bvecs()

PATCH Superseded David Howells 2 participants 2 messages Sep 7 – Sep 9

Subsystems (5)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

PATCH v3 1/1 block: Fix start and length check added to iov_iter_extract_bvecs()

Commit 14b007e17881 added an address check using iter_iov_addr() and a length check using iter_iov_len() to iov_iter_extract_bvecs(), but these cannot be used so and are unsafe in this circumstance as the functions have hardwired assumptions about the iterator type. They should only be used with ITER_UBUF or ITER_IOVEC-type iterators; they shouldn't be used with ITER_BVEC, ITER_KVEC, ITER_FOLIOQ, ITER_XARRAY or ITER_DISCARD iterators.

This proves to be a problem for cachefiles as an iterator of type ITER_FOLIOQ is passed and iter_iov_addr() and iter_iov_len() both malfunction because iter->__iov in iter_iov() is not pointing to an iovec array.

Fix this by using iov_iter_alignment() instead.

Fixes 14b007e17881 ("block: validate user space vectors during extraction")
cc Hannes Reinecke <[email protected]>
cc Christoph Hellwig <[email protected]>
cc Jens Axboe <[email protected]>
cc Alexander Viro <[email protected]>
cc Paulo Alcantara <[email protected]>
---
 lib/iov_iter.c |   18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
lib/iov_iter.c +16 -2
index 6665372ecf71..5e9f9e2c30b1 100644
@@ -1921,15 +1921,29 @@ ssize_t iov_iter_extract_bvecs(struct iov_iter *iter, struct bio_vec *bv,
 		unsigned short max_vecs, unsigned mem_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)) & mem_align_mask)
+	/*
+	 * DMA engines typically have both memory address and length alignment
+	 * requirements, so check these against the alignment mask.  For UBUF,
+	 * IOVEC and KVEC, only the current segment will be drawn from; for
+	 * everything else we might draw from multiple segments, so we need to
+	 * check those too.
+	 */
+	if (likely(iter_is_ubuf(iter) ||
+		   iter_is_iovec(iter) ||
+		   iov_iter_is_kvec(iter))) {
+		unsigned long start = (unsigned long)iter_iov_addr(iter);
+
+		if ((start | iter_iov_len(iter)) & mem_align_mask)
+			return -EINVAL;
+	} else if (iov_iter_alignment(iter) & mem_align_mask) {
 		return -EINVAL;
+	}
 
 	/*
 	 * Move page array up in the allocated memory for the bio vecs as far as
#1 lore raw
Quoted 11 lines · David Howells

index 6665372ecf71..5e9f9e2c30b1 100644

lib/iov_iter.c +0 -1
@@ -1921,15 +1921,29 @@ ssize_t iov_iter_extract_bvecs(struct iov_iter *iter, struct bio_vec *bv,
 		unsigned short max_vecs, unsigned mem_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;

lib/iov_iter.c ssize_t iov_iter_extract_bvecs(struct iov_iter *iter, struct bio_vec *bv, · line 1935

Quoted 10 lines
lib/iov_iter.c +6 -1
 	struct page **pages;
 	ssize_t size;
 
-	if ((start | iter_iov_len(iter)) & mem_align_mask)
+	/*
+	 * DMA engines typically have both memory address and length alignment
+	 * requirements, so check these against the alignment mask.  For UBUF,
+	 * IOVEC and KVEC, only the current segment will be drawn from; for
+	 * everything else we might draw from multiple segments, so we need to
+	 * check those too.

I have a bit of a hard time understanding what "draw from" means here? I guess you mean that iov_iter_extract_pages only operates on a single iov/kvec/bvec but could touch multiple segments for the others? Maybe try to explain that without introducing new terminology?