Re: [PATCH v5 net 4/4] net: hns3: fix use-after-free in debugfs read during reset/unload

From: Simon Horman

Date: Wed Nov 25 2026 - 05:39:20 EST


On Wed, Nov 18, 2026 at 09:03:30PM +0800, Jijie Shao wrote:
> The seq_file refactoring of hns3 debugfs removed the per-file entry
> state checks. Without them, debugfs show callbacks can access
> priv->ring, priv->tqp_vector, and ring->desc while these resources
> are being freed by reset, driver unload, or ethtool ring resize.
>
> Plain state-bit checks leave a TOCTOU window between check and use.
>
> Fix by adding a mutex (dbg_mutex) to struct hnae3_handle, used by
> enet-layer debugfs callbacks. Readers acquire the mutex, then check
> device state per iteration -- this lets an in-flight reader exit
> promptly when reset begins mid-dump, keeping reset latency bounded
> by one iteration, and also rejects a reader that arrives after the
> writer has finished tearing down resources (priv->ring is NULL by
> then). The dbg_uninit() call stays outside the mutex to avoid
> deadlock with debugfs_remove_recursive().
>
> Two kinds of writer paths are protected:
>
> * hns3_client_uninit() and hns3_reset_notify_uninit_enet() clear
> HNS3_NIC_STATE_INITED first so that readers running outside the
> mutex see the device as busy and return -EBUSY early, then take
> dbg_mutex before freeing resources.
>
> * hns3_set_ringparam() calls ndo_stop() first (which sets
> HNS3_NIC_STATE_DOWN) and then takes dbg_mutex around the ring
> re-allocation. The busy check does not test the DOWN bit, so
> readers that arrive here are blocked by the mutex itself rather
> than by the INITED signal.
>
> Userspace-visible changes: debugfs reads under
> /sys/kernel/debug/hns3/*/ that race with reset, driver unload, or
> ethtool ring resize now return -EBUSY instead of potentially reading
> freed or zero-filled memory. The per-iteration busy error is -EBUSY
> instead of -EPERM, and a NULL priv->ring now returns -EBUSY instead
> of -EFAULT. Reading page_pool_info when page pools are not
> initialized now succeeds and prints "page pool not initialized" per
> queue instead of failing with -EFAULT.
>
> Fixes: a723fb8efe29d3 ("net: hns3: refine for set ring parameters")
> Fixes: eced3d1c41db ("net: hns3: use seq_file for files in queue/ in debugfs")
> Closes: https://lore.kernel.org/all/a0853cd9-cab5-441d-b181-8ba97f2f58b0@xxxxxxxxxx/
> Signed-off-by: Jijie Shao <shaojijie@xxxxxxxxxx>

Reviewed-by: Simon Horman <horms@xxxxxxxxxx>