Re: [PATCH] ntb: ntb_tool: fix use-after-free of memory window debugfs files on remove
From: Koichiro Den
Date: Fri Nov 27 2026 - 01:30:29 EST
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@xxxxxxxxxxxxxxx
> Assisted-by: LLM
> Signed-off-by: Oguzhan KAYNAK <kaynak.oguzhan0@xxxxxxxxx>
Reviewed-by: Koichiro Den <den@xxxxxxxxxxxxx>
Tested-by: Koichiro Den <den@xxxxxxxxxxxxx>
> ---
> 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