Linux PCI Non-Transparent Bridge framework and drivers
 help / color / mirror / Atom feed
From: Koichiro Den <den@valinux.co.jp>
To: sashiko-reviews@lists.linux.dev
Cc: ntb@lists.linux.dev
Subject: Re: [PATCH v4 12/15] NTB: ntb_transport: Prepare remote RX info accesses for MW teardown
Date: Wed, 2 Dec 2026 22:35:22 +0900	[thread overview]
Message-ID: <aw_YzPZjUGCS1atP@z790sl> (raw)
In-Reply-To: <sashiko-outbox-236448@kernel.org>

On Fri, Nov 27, 2026 at 02:21:12AM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [High] ntb: TOCTOU use-after-free on remote_rx_info during MW teardown
> --

Note this is a pre-existing issue, already flagged by Sashiko in v2 thread:
https://lore.kernel.org/r/20260910043152.9C2EE1F000FF@smtp.kernel.org/
IOW, this is not a regression introduced by this series.

We discussed leaving the broader fix for separate work in the thread, and Dave
was fine with that:
https://lore.kernel.org/r/82407c03-e208-41b9-a9ca-6a5bbe60f3a7@intel.com/

I'd prefer to keep that for this series.

Best regards,
Koichiro Den

> 
> commit 19817e05f16664b733ed390594b75eacca761094
> Author: Koichiro Den <den@valinux.co.jp>
> 
> NTB: ntb_transport: Prepare remote RX info accesses for MW teardown
> 
> This commit updates qp->remote_rx_info accesses to use READ_ONCE() and
> WRITE_ONCE() and check for NULL in preparation for clearing remote_rx_info
> during memory window teardown, and removes the redundant free-entry
> warning in ntb_async_tx().
> 
> > diff --git a/drivers/ntb/ntb_transport.c b/drivers/ntb/ntb_transport.c
> > index 0c661558a5ebe..fa53fcb87bad5 100644
> > --- a/drivers/ntb/ntb_transport.c
> > +++ b/drivers/ntb/ntb_transport.c
> [ ... ]
> > @@ -2559,8 +2564,14 @@ EXPORT_SYMBOL_GPL(ntb_transport_max_size);
> >  
> >  unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
> >  {
> > +	struct ntb_rx_info *remote_rx_info = READ_ONCE(qp->remote_rx_info);
> >  	unsigned int head = qp->tx_index;
> > -	unsigned int tail = qp->remote_rx_info->entry;
> > +	unsigned int tail;
> > +
> > +	if (!remote_rx_info)
> > +		return 0;
> > +
> > +	tail = remote_rx_info->entry;
> >  
> >  	return tail >= head ? tail - head : qp->tx_max_entry + tail - head;
> >  }
> 
> [Severity: High]
> Can ntb_transport_tx_free_entry(), ntb_qp_debugfs_stats_show(), and
> ntb_qp_link_down_reset() still access freed DMA memory if ntb_free_mw()
> runs between READ_ONCE(qp->remote_rx_info) and the dereference of
> remote_rx_info->entry?
> 
> qp->remote_rx_info points into the inbound MW DMA buffer allocated in
> ntb_alloc_mw_buffer() and freed by dma_free_attrs() in ntb_free_mw().
> In the next commit in the series, commit "NTB: ntb_transport: Clear QP
> pointers when freeing an MW", ntb_free_mw() clears qp->remote_rx_info
> with WRITE_ONCE() and immediately frees the buffer without waiting for
> concurrent lockless readers or writers to finish:
> 
> CPU0                                  CPU1
> ntb_netdev_tx_timer()
>   ntb_transport_tx_free_entry(qp)
>     remote_rx_info =
>       READ_ONCE(qp->remote_rx_info);
>     if (!remote_rx_info)
>                                       ntb_transport_link_cleanup_work()
>                                         ntb_transport_link_cleanup()
>                                           ntb_free_mw()
>                                             WRITE_ONCE(qp->remote_rx_info,
>                                                        NULL);
>                                             dma_free_attrs(...);
>     tail = remote_rx_info->entry;  <-- use-after-free
> 
> During runtime link-down cleanup, ntb_qp_link_cleanup() notifies the
> client to stop the subqueue, but does not stop ntb_netdev_tx_timer() or
> serialize against in-flight ntb_qp_debugfs_stats_show() reads or
> ntb_transport_link_down() -> ntb_send_link_down() ->
> ntb_qp_link_down_reset() writes.  Any caller delayed after loading a
> non-NULL remote_rx_info will read or write the freed DMA buffer.
> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20261127020533.2481725-1-den@valinux.co.jp?part=12

  reply	other threads:[~2026-12-02 13:35 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  2:05 [PATCH v4 00/15] NTB: ntb_transport: Miscellaneous fixes Koichiro Den
2026-11-27  2:05 ` [PATCH v4 01/15] NTB: ntb_transport: Remove the device debugfs directory Koichiro Den
2026-11-27  2:05 ` [PATCH v4 02/15] NTB: ntb_transport: Start TX offload thread after queue setup Koichiro Den
2026-11-27  2:05 ` [PATCH v4 03/15] NTB: ntb_transport: Make link setup flags atomic Koichiro Den
2026-11-27  2:05 ` [PATCH v4 04/15] NTB: ntb_transport: Avoid deadlock when cancelling link work Koichiro Den
2026-11-27  2:05 ` [PATCH v4 05/15] NTB: ntb_transport: Publish link state after QP setup Koichiro Den
2026-11-27  2:05 ` [PATCH v4 06/15] NTB: ntb_transport: Avoid losing QP link-up requests Koichiro Den
2026-11-27  2:05 ` [PATCH v4 07/15] NTB: ntb_transport: Clear link state before QP cleanup Koichiro Den
2026-11-27  2:05 ` [PATCH v4 08/15] NTB: ntb_transport: Stop QP work before freeing a queue Koichiro Den
2026-11-27  2:05 ` [PATCH v4 09/15] NTB: ntb_transport: Stop RX tasklet scheduling " Koichiro Den
2026-11-27  2:05 ` [PATCH v4 10/15] NTB: ntb_transport: Drain RX tasklets during link cleanup Koichiro Den
2026-11-27  2:05 ` [PATCH v4 11/15] NTB: ntb_transport: Wait for RX completions before resetting a QP Koichiro Den
2026-11-27  2:05 ` [PATCH v4 12/15] NTB: ntb_transport: Prepare remote RX info accesses for MW teardown Koichiro Den
2026-11-27  2:21   ` sashiko-bot
2026-12-02 13:35     ` Koichiro Den [this message]
2026-11-27  2:05 ` [PATCH v4 13/15] NTB: ntb_transport: Clear QP pointers when freeing an MW Koichiro Den
2026-11-27  2:24   ` sashiko-bot
2026-12-02 13:38     ` Koichiro Den
2026-11-27  2:05 ` [PATCH v4 14/15] NTB: ntb_transport: Abort link setup on QP MW allocation failure Koichiro Den
2026-11-27  2:05 ` [PATCH v4 15/15] NTB: ntb_transport: Remove clients before freeing transport resources Koichiro Den

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=aw_YzPZjUGCS1atP@z790sl \
    --to=den@valinux.co.jp \
    --cc=ntb@lists.linux.dev \
    --cc=sashiko-reviews@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