v9fs.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "David Howells" <dhowells@redhat.com>
Cc: v9fs@lists.linux.dev, "Chuck Lever" <cel@kernel.org>,
	"Jeff Layton" <jlayton@kernel.org>,
	"Anna Schumaker" <anna@kernel.org>
Subject: Re: [PATCH v18 14/17] netfs: Switch folioq to bvecq
Date: Tue, 17 Nov 2026 18:19:40 +0000	[thread overview]
Message-ID: <sashiko-outbox-219329@kernel.org> (raw)
In-Reply-To: <20261117171958.2982801-15-dhowells@redhat.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Critical] netfs: use-after-free of bvecq in
  netfs_mark_copy_to_cache()
--

commit 834caa148591ab97e4e6988fdeca3a919acba1a9
Author: David Howells <dhowells@redhat.com>

netfs: Switch folioq to bvecq

This commit switches netfslib's rolling buffer and buffered read/writeback
paths from folio_queue to bvecq.

> diff --git a/fs/netfs/buffered_read.c b/fs/netfs/buffered_read.c
> index 69e68ef0652a7..c6c850ffd56ea 100644
> --- a/fs/netfs/buffered_read.c
> +++ b/fs/netfs/buffered_read.c
[ ... ]
> @@ -215,7 +217,7 @@ static void netfs_issue_read(struct netfs_io_request *rreq,
>   * otherwise we set the deprecated PG_private_2.
>   */
>  static void netfs_mark_copy_to_cache(struct netfs_io_request *rreq,
> -				     struct folio_queue **fq,
> +				     struct bvecq **bq,
>  				     unsigned int *offset,
>  				     int *slot,
>  				     size_t len,
> @@ -225,21 +227,21 @@ static void netfs_mark_copy_to_cache(struct netfs_io_request *rreq,
>  		struct folio *folio;
>  		size_t fsize, overlap;
>  
> -		if (!*fq)
> +		if (!*bq)
>  			break;
> -		if (*slot >= folioq_count(*fq)) {
> -			*fq = (*fq)->next;
> +		if (!bvecq_acquire_slot(*bq, *slot)) {
> +			*bq = bvecq_next(*bq);
>  			*slot = 0;
>  			*offset = 0;
>  			continue;
>  		}
>  
>  		/* Determine how much the subreq overlaps the folio, if at all. */
> -		fsize = folioq_folio_size(*fq, *slot);
> +		fsize = (*bq)->bv[*slot].bv_len;
>  		overlap = min(len, fsize - *offset);
>  
>  		if (overlap > 0 && copy) {
> -			folio = folioq_folio(*fq, *slot);
> +			folio = bvec_folio(&(*bq)->bv[*slot]);
>  			if (netfs_using_pgpriv2(rreq)) {
>  				if (!folio_test_private_2(folio))
>  					folio_start_private_2(folio);
> @@ -275,7 +277,7 @@ static void netfs_read_to_pagecache(struct netfs_io_request *rreq)
>  		.cached_to[1]	= ULLONG_MAX,
>  	};
>  	struct fscache_occupancy *occ = &_occ;
> -	struct folio_queue *fq = rreq->buffer.tail;
> +	struct bvecq *bq = rreq->buffer.tail;
>  	unsigned int offset = 0;
>  	ssize_t size = rreq->len;
>  	uoff_t start = rreq->start;
> @@ -408,10 +410,10 @@ static void netfs_read_to_pagecache(struct netfs_io_request *rreq)
>  		if (size <= 0)
>  			netfs_all_subreqs_queued(rreq);
>  
> -		if (fq) {
> +		if (bq) {
>  			/* See if the cache indicated this should be cached. */
>  			copy = test_bit(NETFS_SREQ_COPY_TO_CACHE, &subreq->flags);
> -			netfs_mark_copy_to_cache(rreq, &fq, &slot, &offset, slice, copy);
> +			netfs_mark_copy_to_cache(rreq, &bq, &slot, &offset, slice, copy);
>  		}
>  
>  		trace_netfs_sreq(subreq, netfs_sreq_trace_submit);

[Severity: Critical]
Can netfs_mark_copy_to_cache() dereference a freed bvecq across subrequest
iterations?

netfs_read_to_pagecache() keeps an unreferenced pointer in bq.  When a
subrequest ends at the last slot of a bvecq (bq0),
netfs_prepare_read_iterator() -> rolling_buffer_advance() advances
rreq->buffer.iter.bvecq to bq1, so the next subrequest pins bq1 rather
than bq0.  However, netfs_mark_copy_to_cache() only checks
!bvecq_acquire_slot(*bq, *slot) at the top of the while (len > 0) loop, so
when len reaches 0 at the end of bq0, bq is left pointing to the exhausted
bq0 with slot == bq0->nr_slots:

CPU0 (issuer)                     CPU1 (collector)
netfs_read_to_pagecache()
  /* subreq N ends at end of bq0 */
  netfs_prepare_read_iterator()
    /* advances rreq->buffer.iter to bq1 */
  netfs_mark_copy_to_cache()
    /* slot == bq0->nr_slots, bq == bq0 */
  netfs_issue_read()
                                  netfs_collect_read_results()
                                    netfs_read_unlock_folios()
                                      rolling_buffer_delete_spent()
                                        bvecq_put(bq0)
                                    netfs_put_subrequest()
                                      netfs_free_subrequest()
                                        bvecq_put(bq0)  /* frees bq0 */
  /* subreq N+1 pins bq1 */
  netfs_mark_copy_to_cache()
    bvecq_acquire_slot(bq0, slot)  <-- use-after-free
    bvecq_next(bq0)                <-- use-after-free

The same happens on a single CPU if subreq N completes synchronously (such
as NETFS_FILL_WITH_ZEROES).  Pinning bq with bvecq_get() in
netfs_read_to_pagecache() and updating that reference as bq advances would
prevent bq0 from being freed before netfs_mark_copy_to_cache() steps to
bq1.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117171958.2982801-1-dhowells@redhat.com?part=14

  reply	other threads:[~2026-11-17 18:19 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 17:18 [PATCH v18 00/17] netfs, iov_iter: Use a chain of bio_vec arrays instead of folio_queue David Howells
2026-11-17 17:18 ` [PATCH v18 01/17] iov_iter: Fix kunit test to remove __init and __refdata marks David Howells
2026-11-17 17:34   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 02/17] Add a function to kmap one page of a multipage bio_vec David Howells
2026-11-17 17:28   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 03/17] iov_iter: Add a segmented queue of bio_vec[] David Howells
2026-11-17 17:56   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 04/17] netfs: Add some tools for managing bvecq chains David Howells
2026-11-17 17:51   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 05/17] iov_iter: Add an API document for the bvecq structure David Howells
2026-11-17 17:32   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 06/17] iov_iter: Add a bvecq kunit test David Howells
2026-11-17 17:30   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 07/17] afs: Fix an inconsistency in the symlink code David Howells
2026-11-17 17:38   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 08/17] netfs: Explicitly pass tail padding to netfs_writeback_single() David Howells
2026-11-17 17:58   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 09/17] netfs: Change netfs_writeback_single() to not look inside the iterator David Howells
2026-11-17 17:53   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 10/17] netfs: Pass the write length to request creation where appropriate David Howells
2026-11-17 17:28   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 11/17] afs: Use a bvecq to hold dir content rather than folioq David Howells
2026-11-17 18:05   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 12/17] cifs: Use a bvecq for buffering instead of a folioq David Howells
2026-11-17 17:45   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 13/17] smbdirect: Support ITER_BVECQ in smbdirect_map_sges_from_iter() David Howells
2026-11-17 17:36   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 14/17] netfs: Switch folioq to bvecq David Howells
2026-11-17 18:19   ` sashiko-bot [this message]
2026-11-17 17:19 ` [PATCH v18 15/17] smbdirect: Remove support for ITER_FOLIOQ from smbdirect_map_sges_from_iter() David Howells
2026-11-17 17:35   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 16/17] iov_iter: Remove ITER_FOLIOQ David Howells
2026-11-17 17:41   ` sashiko-bot
2026-11-17 17:19 ` [PATCH v18 17/17] netfs: Remove folio_queue David Howells
2026-11-17 17:37   ` sashiko-bot

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=sashiko-outbox-219329@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=anna@kernel.org \
    --cc=cel@kernel.org \
    --cc=dhowells@redhat.com \
    --cc=jlayton@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=v9fs@lists.linux.dev \
    /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;
as well as URLs for NNTP newsgroup(s).