* [PATCH] ntb: ntb_tool: fix use-after-free of memory window debugfs files on remove
@ 2026-11-26 3:19 Oguzhan KAYNAK
2026-11-27 6:30 ` Koichiro Den
0 siblings, 1 reply; 2+ messages in thread
From: Oguzhan KAYNAK @ 2026-11-26 3:19 UTC (permalink / raw)
To: Jon Mason, Dave Jiang, Allen Hubbe
Cc: ntb, linux-kernel, Oguzhan KAYNAK, stable
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>
---
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] ntb: ntb_tool: fix use-after-free of memory window debugfs files on remove
2026-11-26 3:19 [PATCH] ntb: ntb_tool: fix use-after-free of memory window debugfs files on remove Oguzhan KAYNAK
@ 2026-11-27 6:30 ` Koichiro Den
0 siblings, 0 replies; 2+ messages in thread
From: Koichiro Den @ 2026-11-27 6:30 UTC (permalink / raw)
To: Oguzhan KAYNAK
Cc: Jon Mason, Dave Jiang, Allen Hubbe, ntb, linux-kernel, stable
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-27 6:30 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 3:19 [PATCH] ntb: ntb_tool: fix use-after-free of memory window debugfs files on remove Oguzhan KAYNAK
2026-11-27 6:30 ` Koichiro Den
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox