From: Koichiro Den <den@valinux.co.jp>
To: Oguzhan KAYNAK <kaynak.oguzhan0@gmail.com>
Cc: Jon Mason <jdmason@kudzu.us>, Dave Jiang <dave.jiang@intel.com>,
Allen Hubbe <allenbh@gmail.com>,
ntb@lists.linux.dev, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH] ntb: ntb_tool: fix use-after-free of memory window debugfs files on remove
Date: Fri, 27 Nov 2026 15:30:19 +0900 [thread overview]
Message-ID: <awke343ZVpkOIDfg@z790sl> (raw)
In-Reply-To: <20261126031954.168544-1-kaynak.oguzhan0@gmail.com>
On Thu, Nov 26, 2026 at 06:19:54AM +0300, Oguzhan KAYNAK wrote:
> tool_remove() removes the device's whole debugfs directory with
> debugfs_remove_recursive() before tool_clear_mws() frees the memory
> windows. The per-window files peer<N>/mw<M> and peer<N>/peer_mw<M> are
> created in that directory, so they are already gone when
> tool_free_mw() and tool_free_peer_mw() pass their stored dentries to
> debugfs_remove() again. Those dentries have been killed (d_inode is
> NULL) and are freed after an RCU grace period.
>
> Removing ntb_tool, or unbinding it from the device, while a memory
> window is set up therefore crashes. With lockdep on x86-64:
>
> BUG: kernel NULL pointer dereference, address: 0000000000000140
> ...
> Call Trace:
> down_write_nested
> __simple_recursive_removal
> debugfs_remove
> tool_free_peer_mw [ntb_tool]
> tool_clear_mws [ntb_tool]
> tool_remove [ntb_tool]
> ntb_remove [ntb]
>
> (0x140 is offsetof(struct inode, i_rwsem) + offsetof(struct
> rw_semaphore, dep_map) read through the NULL d_inode.) With KASAN the
> same access is reported as "null-ptr-deref in range
> [0x0000000000000140-0x0000000000000147]". If an RCU grace period has
> passed by then, the dentries themselves are already freed.
>
> Found and tested on a virtual NTB device in QEMU: removing ntb_tool
> with an inbound window, an outbound window, or both set up crashed every
> time (tool_free_mw() or tool_free_peer_mw() in the trace); with this
> patch it unloads cleanly on lockdep and KASAN kernels.
>
> Forget the window files once the directory has been removed, so that the
> free functions only release the windows themselves. Removing debugfs
> first is kept: it ends all user space access to the files before the
> windows go away.
>
> Fixes: 7f46c8b3a552 ("NTB: ntb_tool: Add full multi-port NTB API support")
> Cc: stable@vger.kernel.org
> Assisted-by: LLM
> Signed-off-by: Oguzhan KAYNAK <kaynak.oguzhan0@gmail.com>
Reviewed-by: Koichiro Den <den@valinux.co.jp>
Tested-by: Koichiro Den <den@valinux.co.jp>
> ---
> drivers/ntb/test/ntb_tool.c | 15 +++++++++++++++
> 1 file changed, 15 insertions(+)
>
> --- a/drivers/ntb/test/ntb_tool.c
> +++ b/drivers/ntb/test/ntb_tool.c
> @@ -1599,7 +1599,22 @@ static void tool_setup_dbgfs(struct tool
>
> static void tool_clear_dbgfs(struct tool_ctx *tc)
> {
> + int pidx, widx;
> +
> debugfs_remove_recursive(tc->dbgfs_dir);
> + tc->dbgfs_dir = NULL;
> +
> + /*
> + * The memory window files lived in tc->dbgfs_dir and are gone now.
> + * Forget their dentries so that tool_clear_mws() doesn't remove
> + * them a second time.
> + */
> + for (widx = 0; widx < tc->outmw_cnt; widx++)
> + tc->outmws[widx].dbgfs_file = NULL;
> +
> + for (pidx = 0; pidx < tc->peer_cnt; pidx++)
> + for (widx = 0; widx < tc->peers[pidx].inmw_cnt; widx++)
> + tc->peers[pidx].inmws[widx].dbgfs_file = NULL;
> }
>
> static int tool_probe(struct ntb_client *self, struct ntb_dev *ntb)
> --
> 2.34.1
prev parent reply other threads:[~2026-11-27 6:30 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 3:19 Oguzhan KAYNAK
2026-11-27 6:30 ` Koichiro Den [this message]
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=awke343ZVpkOIDfg@z790sl \
--to=den@valinux.co.jp \
--cc=allenbh@gmail.com \
--cc=dave.jiang@intel.com \
--cc=jdmason@kudzu.us \
--cc=kaynak.oguzhan0@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=ntb@lists.linux.dev \
--cc=stable@vger.kernel.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
all inboxes | Powered by JetHome®