net: wwan: iosm: clear the MUX pointer after freeing the MUX
Subsystems (4)
PATCH 1/1 net: wwan: iosm: clear the MUX pointer after freeing the MUX
ipc_imem_cleanup() frees the MUX but leaves ipc_imem->mux pointing at it, so closing a netdev that is still up uses the freed MUX:
BUG: KASAN: slab-use-after-free in ipc_mux_close_session Call Trace: ipc_mux_close_session ipc_wwan_link_stop __dev_close_many wwan_unregister_ops ipc_wwan_deinit ipc_imem_cleanup ipc_pcie_remove
Clear the pointer after the free; ipc_imem_sys_wwan_close() already skips the session close when it is NULL. Do the same in the error path of the run state worker.
Reproduced with KASAN on an XMM7360 by unbinding the driver while wwan0 is up. With this change, two such unbinds show no report.
---
Previously sent as patch 1/2 of a series:
https://lore.kernel.org/netdev/[email protected]/
drivers/net/wwan/iosm/iosm_ipc_imem.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/net/wwan/iosm/iosm_ipc_imem.c +2 -0
@@ -635,6 +635,7 @@ static void ipc_imem_run_state_worker(struct work_struct *instance) err_ipc_mux_deinit:ipc_mux_deinit (ipc_imem->mux); + ipc_imem->mux =NULL ; err_out:ipc_uevent_send (ipc_imem->dev,UEVENT_CD_READY_LINK_DOWN ); }
@@ -1245,6 +1246,7 @@ void ipc_imem_cleanup(struct iosm_imem *ipc_imem)if (test_and_clear_bit (FULLY_FUNCTIONAL , &ipc_imem->flag)) {ipc_mux_deinit (ipc_imem->mux); + ipc_imem->mux =NULL ;ipc_debugfs_deinit (ipc_imem);ipc_wwan_deinit (ipc_imem->wwan);ipc_port_deinit (ipc_imem->ipc_port);
Signature · 1 line
2.56.0
This patch was applied to netdev/net-next.git (main) by Willem de Bruijn <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Willem de Bruijn <[email protected]>:
Quoted 13 lines · On Tue, 1 Dec 2026 06:55:53 -0800 you wrote:
ipc_imem_cleanup()frees the MUX but leavesipc_imem->muxpointing at it, so closing a netdev that is still up uses the freed MUX:BUG: KASAN: slab-use-after-free in ipc_mux_close_session Call Trace: ipc_mux_close_session ipc_wwan_link_stop __dev_close_many wwan_unregister_ops ipc_wwan_deinit ipc_imem_cleanup ipc_pcie_remove
Quoted 1 line
[...]
Here is the summary with links:
- [net] net: wwan: iosm: clear the MUX pointer after freeing the MUX
https://git.kernel.org/netdev/net-next/c/162373b54c7c
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 1 (2) · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] The fix relies on ipc_imem_sys_wwan_close() seeing ipc_imem->mux == NULL.
Pre-existing issues:
- [High] ipc_imem_cleanup() in drivers/net/wwan/iosm/iosm_ipc_imem.c never cancels ipc_imem->adb_timer.
- [High] Teardown frees the MUX and then sets ipc_imem->mux to NULL in ipc_imem_cleanup() without first stopping or synchronizing the other…
--
net: wwan: iosm: clear the MUX pointer after freeing the MUX
This commit sets ipc_imem->mux to NULL after ipc_mux_deinit() frees it. It does this in ipc_imem_cleanup() and in the error path of ipc_imem_run_state_worker(). ipc_imem_sys_wwan_close() then skips the session close when the netdevs are unregistered.
Quoted 4 lines
drivers/net/wwan/iosm/iosm_ipc_imem.c +0
-0
[ ... ]
drivers/net/wwan/iosm/iosm_ipc_imem.c void ipc_imem_cleanup(struct iosm_imem *ipc_imem) · line 1252
Quoted 8 lines
drivers/net/wwan/iosm/iosm_ipc_imem.c +1
-0@@ -1245,6 +1246,7 @@ void ipc_imem_cleanup(struct iosm_imem *ipc_imem)if (test_and_clear_bit (FULLY_FUNCTIONAL , &ipc_imem->flag)) {ipc_mux_deinit (ipc_imem->mux); + ipc_imem->mux =NULL ;ipc_debugfs_deinit (ipc_imem);ipc_wwan_deinit (ipc_imem->wwan);ipc_port_deinit (ipc_imem->ipc_port);
[Severity: High]
The fix relies on the NULL check in ipc_imem_sys_wwan_close():
drivers/net/wwan/iosm/iosm_ipc_imem_ops.c:ipc_imem_sys_wwan_close() {
if (ipc_imem->mux && if_id >= IP_MUX_SESSION_START &&
if_id <= IP_MUX_SESSION_END)
ipc_mux_close_session(ipc_imem->mux, if_id);
}Is that check enough when a netdev close races with removal? ipc_imem_cleanup() frees the MUX and clears the pointer without holding RTNL. RTNL is only taken later, in ipc_wwan_deinit()->wwan_unregister_ops().
A userspace ndo_stop can run at the same time, for example "ip link set wwan0 down", possibly in response to the UEVENT_MDM_NOT_READY that cleanup sends first:
CPU1 (holding RTNL)ipc_wwan_link_stop()
ipc_imem_sys_wwan_close()
sees ipc_imem->mux != NULL
ipc_mux_close_session()
ipc_mux_schedule()
ipc_mux_session_close()
ipc_mux_dl_acb_send_cmds(..., true, false)
blocks up to 1s waiting for the CP replyCPU0ipc_pcie_remove()
ipc_imem_cleanup()
ipc_mux_deinit(ipc_imem->mux); /* kfree(ipc_mux) */
ipc_imem->mux = NULL;The CP reply never arrives once phase is IPC_P_OFF_REQ, because ipc_imem_handle_irq() ignores the IRQ. When CPU1 wakes up, ipc_mux_session_close() runs:
ipc_mux->session[if_id].flow_ctl_en_cnt = 0; ipc_mux->session[if_id].flow_ctl_dis_cnt = 0; ipc_mux->session[if_id].flow_ctl_mask = 0; ipc_mux_session_reset(ipc_mux, if_id);
Then ipc_mux_close_session() clears session[session_nr].flags. Wouldn't those writes still land on the freed MUX?
The queued ipc_mux_tq_cmd_send() also reloads ipc_imem->mux in the tasklet. It then dereferences ipc_mux->channel without a NULL check.
The check in ipc_imem_sys_wwan_close() also loads ipc_imem->mux twice without READ_ONCE(). Could the second load see NULL after the first one saw a valid pointer?
The commit message says this fixes "closing a netdev that is still up uses the freed MUX". This race looks like the same class of bug, and the NULL sentinel doesn't seem to close it.
Two alternatives come to mind:
- Unregister the netdevs with ipc_wwan_deinit() before ipc_mux_deinit(). ipc_mux_stop_netif_for_all_sessions() would then need to avoid the freed wwan.
- Serialize the MUX free against ndo_stop with RTNL.
Would either of these be more robust than the NULL sentinel?
[Severity: High]
This isn't a bug introduced by this patch, but ipc_imem_cleanup() cancels only four of the five hrtimers in struct iosm_imem. adb_timer is not one of them:
hrtimer_cancel(&ipc_imem->td_alloc_timer); hrtimer_cancel(&ipc_imem->tdupdate_timer); hrtimer_cancel(&ipc_imem->fast_update_timer); hrtimer_cancel(&ipc_imem->startup_timer);
Whenever MUX_AGGREGATION UL data is encoded, ipc_mux_tq_ul_trigger_encode() and ipc_imem_handle_irq() arm adb_timer with a 500us period. So it is often pending while UL traffic is flowing.
If it fires during teardown:
ipc_imem_adb_timer_cb()
ipc_task_queue_send_task(ipc_imem, ipc_imem_tq_adb_timer_cb, ...)
ipc_imem_tq_adb_timer_cb()
ipc_mux_ul_adb_finish(ipc_imem->mux)
ul_adb = &ipc_mux->ul_adb;
if (!ul_adb->dest_skb)Before this patch that was a use-after-free of the MUX. With this patch it becomes a NULL pointer dereference in tasklet context.
The window is wide. ipc_wwan_deinit() runs synchronize_net() through netdev unregistration after the NULL store and before ipc_task_deinit().
If the timer fires after ipc_task_deinit() and kfree(ipc_imem->ipc_task), can ipc_task_queue_send_task() also touch the freed ipc_task?
Should adb_timer be cancelled along with the other hrtimers in ipc_imem_cleanup()?
[Severity: High]
This is a pre-existing issue, but none of the other users of ipc_imem->mux check for NULL. None of them are synchronized against the free in ipc_imem_cleanup() either. The only thing separating them is a plain store of ipc_imem->phase = IPC_P_OFF_REQ.
On the TX side, ipc_imem_sys_wwan_transmit() checks phase without a lock and then calls:
ret = ipc_mux_ul_trigger_encode(ipc_imem->mux, if_id, skb);
ipc_mux_ul_trigger_encode() dereferences ipc_mux->session[if_id] and ipc_mux->channel right away. A transmit that passed the phase check before cleanup stored IPC_P_OFF_REQ can run at the same time as kfree(ipc_mux).
ipc_mux_ul_trigger_encode() also queues ipc_mux_tq_ul_trigger_encode() to the tasklet:
struct iosm_mux *ipc_mux = ipc_imem->mux;
...
ul_data_pend = ipc_mux_ul_data_encode(ipc_mux);
if (ul_data_pend) {
if (ipc_mux->protocol == MUX_AGGREGATION)
...
ipc_mux->ev_mux_net_transmit_pending = false;ipc_mux_ul_data_encode() handles a NULL MUX, but the write to ev_mux_net_transmit_pending does not. ipc_mux_tq_cmd_send() has the same pattern.
Nothing flushes the tasklet before ipc_mux_deinit(). tasklet_kill() runs only in ipc_task_deinit(), well after the MUX is gone. Once phase != IPC_P_RUN, ipc_imem_channel_close() also skips the synchronous pipe close.
An ipc_imem_handle_irq() already running on another CPU may have passed the IPC_P_OFF_REQ check. It can then call ipc_mux_dl_decode(), ipc_mux_ul_encoded_process() and ipc_mux_check_n_restart_tx() on ipc_imem->mux while cleanup frees it.
This patch turns some of these paths from a use-after-free into a NULL dereference, but can the race itself still happen?
Would these users need to be quiesced first, for example with netif_device_detach() and tasklet_disable() before the free, or by reordering the teardown?
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261201145553.14624-1-davidlwesten%40gmail.com