wifi: brcmfmac: fix use-after-free when rescheduling brcmf_btcoex_info work
Subsystems (3)
PATCH 1/1 wifi: brcmfmac: fix use-after-free when rescheduling brcmf_btcoex_info work
From: Duoming Zhou <[email protected]>
[ Upstream commit 9cb83d4be0b9b697eae93d321e0da999f9cdfcfc ]
The brcmf_btcoex_detach() only shuts down the btcoex timer, if the flag timer_on is false. However, the brcmf_btcoex_timerfunc(), which runs as timer handler, sets timer_on to false. This creates critical race conditions:
1.If brcmf_btcoex_detach() is called while brcmf_btcoex_timerfunc() is executing, it may observe timer_on as false and skip the call to timer_shutdown_sync().
2.The brcmf_btcoex_timerfunc() may then reschedule the brcmf_btcoex_info worker after the cancel_work_sync() has been executed, resulting in use-after-free bugs.
The use-after-free bugs occur in two distinct scenarios, depending on the timing of when the brcmf_btcoex_info struct is freed relative to the execution of its worker thread.
Scenario 1: Freed before the worker is scheduled
The brcmf_btcoex_info is deallocated before the worker is scheduled. A race condition can occur when schedule_work(&bt_local->work) is called after the target memory has been freed. The sequence of events is detailed below:
CPU0 | CPU1
brcmf_btcoex_detach | brcmf_btcoex_timerfunc
| bt_local->timer_on = false;
if (cfg->btcoex->timer_on) |
... |
cancel_work_sync(); |
... |
kfree(cfg->btcoex); // FREE |
| schedule_work(&bt_local->work); // USEScenario 2: Freed after the worker is scheduled
The brcmf_btcoex_info is freed after the worker has been scheduled but before or during its execution. In this case, statements within the brcmf_btcoex_handler() — such as the container_of macro and subsequent dereferences of the brcmf_btcoex_info object will cause a use-after-free access. The following timeline illustrates this scenario:
CPU0 | CPU1
brcmf_btcoex_detach | brcmf_btcoex_timerfunc
| bt_local->timer_on = false;
if (cfg->btcoex->timer_on) |
... |
cancel_work_sync(); |
... | schedule_work(); // Reschedule
|
kfree(cfg->btcoex); // FREE | brcmf_btcoex_handler() // Worker
/* | btci = container_of(....); // USE
The kfree() above could | ...
also occur at any point | btci-> // USE
during the worker's execution|
*/ |To resolve the race conditions, drop the conditional check and call timer_shutdown_sync() directly. It can deactivate the timer reliably, regardless of its current state. Once stopped, the timer_on state is then set to false.
--- drivers/net/wireless/broadcom/brcm80211/brcmfmac/btcoex.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/btcoex.c +2 -4
@@ -392,10 +392,8 @@ void brcmf_btcoex_detach(struct brcmf_cfg80211_info *cfg)if (!cfg->btcoex)return ; -if (cfg->btcoex->timer_on) { - cfg->btcoex->timer_on = false; -del_timer_sync (&cfg->btcoex->timer); - } +del_timer_sync (&cfg->btcoex->timer); + cfg->btcoex->timer_on = false;cancel_work_sync (&cfg->btcoex->work);
Signature · 1 line
2.34.1
Queued for 5.15.y, thanks.
Signature · 2 lines
Thanks, Sasha
Quoted 3 lines · Robert Garcia
From: Duoming Zhou <[email protected]>
[ Upstream commit
9cb83d4be0b9b697eae93d321e0da999f9cdfcfc]
[...]
drivers/net/wireless/broadcom/brcm80211/brcmfmac/btcoex.c void brcmf_btcoex_detach(struct brcmf_cfg80211_info *cfg) · line 398
Quoted 4 lines
drivers/net/wireless/broadcom/brcm80211/brcmfmac/btcoex.c +2
-0+del_timer_sync (&cfg->btcoex->timer); + cfg->btcoex->timer_on = false;cancel_work_sync (&cfg->btcoex->work);
The upstream fix uses timer_shutdown_sync() which prevents the timer from being re-armed after it returns. del_timer_sync() does not have this guarantee.
brcmf_btcoex_handler() has three mod_timer() calls - two in BRCMF_BT_DHCP_START and one in BRCMF_BT_DHCP_OPPR_WIN - none of which are guarded by timer_on. A work item running between del_timer_sync() and cancel_work_sync() can re-arm the timer, leaving it pending when kfree() is called.
If the mod_timer() calls were made conditional on timer_on, setting timer_on = false before del_timer_sync() would be sufficient. That would be my preferred fix. Alternatively a second del_timer_sync() after cancel_work_sync() would also work.
Also the commit message still mentions timer_shutdown_sync() rather than del_timer_sync().
Regards,
Arend
Sasha Levin
maintainerQuoted 9 lines
The upstream fix uses
timer_shutdown_sync()which prevents the timer from being re-armed after it returns.del_timer_sync()does not have this guarantee.
brcmf_btcoex_handler()has threemod_timer()calls - two inBRCMF_BT_DHCP_STARTand one inBRCMF_BT_DHCP_OPPR_WIN- none of which are guarded bytimer_on. A work item running betweendel_timer_sync()andcancel_work_sync()can re-arm the timer, leaving it pending whenkfree()is called.
Agreed on the analysis. One thing to note: this backport already shipped in v5.15.210, and 6.1.y shipped the same weakened del_timer_sync() variant in v6.1.167, so this needs follow-up patches rather than a v2 of the original.
Quoted 7 lines
If the
mod_timer()calls were made conditional ontimer_on, settingtimer_on= false beforedel_timer_sync()would be sufficient. That would be my preferred fix. Alternatively a seconddel_timer_sync()aftercancel_work_sync()would also work.Also the commit message still mentions
timer_shutdown_sync()rather thandel_timer_sync().
There's a simpler option: timer_shutdown_sync() is actually available in both 5.15.y and 6.1.y, so the del_timer_sync() substitution was never necessary in the first place. A one-line follow-up converting del_timer_sync() to timer_shutdown_sync() makes brcmf_btcoex_detach() match the upstream post-image exactly, closes the re-arm window you describe, and avoids a stable-only divergence guarding the mod_timer() calls.
Robert, could you send follow-up patches for 5.15.y and 6.1.y doing that conversion?
Signature · 2 lines
Thanks, Sasha