Linux CIFS filesystem development
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: Christian Brauner <christian@brauner.io>
Cc: David Howells <dhowells@redhat.com>,
	Paulo Alcantara <pc@manguebit.org>,
	netfs@lists.linux.dev, linux-afs@lists.infradead.org,
	linux-cifs@vger.kernel.org, ceph-devel@vger.kernel.org,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Matthew Wilcox <willy@infradead.org>,
	Christoph Hellwig <hch@infradead.org>,
	Jens Axboe <axboe@kernel.dk>, Mike Marshall <hubcap@omnibond.com>
Subject: [PATCH v2 04/14] iov_iter: Fix potential underflow in iov_iter_extract_xarray_pages()
Date: Wed, 24 Jun 2026 12:57:25 +0100	[thread overview]
Message-ID: <20260624115737.2964520-5-dhowells@redhat.com> (raw)
In-Reply-To: <20260624115737.2964520-1-dhowells@redhat.com>

In iov_iter_extract_xarray_pages(), if no pages are extracted because
there's a hole (or something otherwise unextractable) in the xarray, then
the calculation of maxsize at the end can go wrong if the starting offset
is not zero.

Fix this by returning 0 in such a case and freeing the page array if
allocated here rather than being passed in.

Note that in the near future, ITER_XARRAY should be removed.

Fixes: 7d58fe731028 ("iov_iter: Add a function to extract a page list from an iterator")
Link: https://sashiko.dev/#/patchset/20260608145432.681865-1-dhowells%40redhat.com
Link: https://sashiko.dev/#/patchset/20260616100821.2062304-1-dhowells%40redhat.com
Signed-off-by: David Howells <dhowells@redhat.com>
cc: Paulo Alcantara <pc@manguebit.org>
cc: Matthew Wilcox <willy@infradead.org>
cc: Christoph Hellwig <hch@infradead.org>
cc: Jens Axboe <axboe@kernel.dk>
cc: Mike Marshall <hubcap@omnibond.com>
cc: netfs@lists.linux.dev
cc: linux-fsdevel@vger.kernel.org
---
 lib/iov_iter.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lib/iov_iter.c b/lib/iov_iter.c
index 243662af1af7..5f60733f3280 100644
--- a/lib/iov_iter.c
+++ b/lib/iov_iter.c
@@ -1568,6 +1568,7 @@ static ssize_t iov_iter_extract_xarray_pages(struct iov_iter *i,
 	struct folio *folio;
 	unsigned int nr = 0, offset;
 	loff_t pos = i->xarray_start + i->iov_offset;
+	bool will_alloc = !*pages;
 	XA_STATE(xas, i->xarray, pos >> PAGE_SHIFT);
 
 	offset = pos & ~PAGE_MASK;
@@ -1595,6 +1596,14 @@ static ssize_t iov_iter_extract_xarray_pages(struct iov_iter *i,
 	}
 	rcu_read_unlock();
 
+	if (!nr) {
+		if (will_alloc) {
+			kvfree(*pages);
+			*pages = NULL;
+		}
+		return 0;
+	}
+
 	maxsize = min_t(size_t, nr * PAGE_SIZE - offset, maxsize);
 	iov_iter_advance(i, maxsize);
 	return maxsize;


  parent reply	other threads:[~2026-06-24 11:58 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-24 11:57 [PATCH v2 00/14] netfs: Miscellaneous fixes David Howells
2026-06-24 11:57 ` [PATCH v2 01/14] netfs: Fix decision whether to disallow write-streaming due to fscache use David Howells
2026-06-24 11:57 ` [PATCH v2 02/14] cachefiles: Fix double fput David Howells
2026-06-24 11:57 ` [PATCH v2 03/14] cachefiles: Fix file burial to take lock when unsetting S_KERNEL_FILE David Howells
2026-06-24 11:57 ` David Howells [this message]
2026-06-24 13:41   ` [PATCH v2 04/14] iov_iter: Fix potential underflow in iov_iter_extract_xarray_pages() Christoph Hellwig
2026-06-24 11:57 ` [PATCH v2 05/14] iov_iter: Fix missing alloc fail check in iov_iter_extract_bvec_pages() David Howells
2026-06-24 11:57 ` [PATCH v2 06/14] iov_iter: Fix a memory leak in iov_iter_extract_user_pages() David Howells
2026-06-24 11:57 ` [PATCH v2 07/14] iov_iter: Remove unused variable in kunit_iov_iter.c David Howells
2026-06-24 11:57 ` [PATCH v2 08/14] scatterlist: Fix offset in folio calc in extract_xarray_to_sg() David Howells
2026-06-24 11:57 ` [PATCH v2 09/14] netfs: Fix kdoc warning David Howells
2026-06-24 11:57 ` [PATCH v2 10/14] netfs: Replace wb_lock with a bit lock for asynchronicity David Howells
2026-06-24 11:57 ` [PATCH v2 11/14] netfs: Fix writethrough to use collection offload David Howells
2026-06-24 11:57 ` [PATCH v2 12/14] netfs: Fix writeback error handling David Howells
2026-06-24 11:57 ` [PATCH v2 13/14] netfs: Fix folio state after ENOMEM whilst under writeback iteration David Howells
2026-06-24 11:57 ` [PATCH v2 14/14] netfs: Fix DIO write retry for filesystems without a ->prepare_write() David Howells
2026-06-24 14:21   ` ChenXiaoSong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260624115737.2964520-5-dhowells@redhat.com \
    --to=dhowells@redhat.com \
    --cc=axboe@kernel.dk \
    --cc=ceph-devel@vger.kernel.org \
    --cc=christian@brauner.io \
    --cc=hch@infradead.org \
    --cc=hubcap@omnibond.com \
    --cc=linux-afs@lists.infradead.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netfs@lists.linux.dev \
    --cc=pc@manguebit.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox