[wireless] wifi: brcmfmac: fix use-after-free of the watchdog task_struct

Message ID 20261120174231.2024897-1-dario.binacchi@amarulasolutions.com
State New
Headers
Series [wireless] wifi: brcmfmac: fix use-after-free of the watchdog task_struct |

Commit Message

Dario Binacchi Nov. 20, 2026, 5:42 p.m. UTC
The watchdog thread exits as soon as it receives a SIGTERM, also from
userspace, e.g. when init kills all the processes at reboot. If the
driver is removed afterwards, the reference taken right before sending
the signal by commit c623b6358088 ("wifi: brcmfmac: Fix potential
use-after-free issue when stopping watchdog task") comes too late, and
kthread_stop() runs on a task_struct already freed.

  WARNING: kernel/kthread.c:84 at kthread_stop+0xd8/0x190
  Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000
  Call trace:
   kthread_stop+0x148/0x190 (P)
   kthread_stop_put+0x14/0xb0
   brcmf_sdio_remove+0x58/0x1d0
   brcmf_sdiod_remove+0x20/0xa0
   brcmf_ops_sdio_remove+0x5c/0x90

Take the reference before the thread is started and drop it with
kthread_stop_put() when it is stopped.

Fixes: c623b6358088 ("wifi: brcmfmac: Fix potential use-after-free issue when stopping watchdog task")
Cc: stable@vger.kernel.org
Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>

---

 .../net/wireless/broadcom/brcm80211/brcmfmac/sdio.c  | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

---
base-commit: d0f871bfb831705d9f55479f5b2b40f636ea7b16
branch: brcmfmac-wdog-uaf

-- 
2.43.0
  

Comments

Arend van Spriel Nov. 24, 2026, 8:13 p.m. UTC | #1
On Fri, 20 Nov 2026 18:42:27 +0100, Dario Binacchi <dario.binacchi@amarulasolutions.com> wrote:
> The watchdog thread exits as soon as it receives a SIGTERM, also from
> userspace, e.g. when init kills all the processes at reboot. If the
> driver is removed afterwards, the reference taken right before sending
> the signal by commit c623b6358088 ("wifi: brcmfmac: Fix potential
> use-after-free issue when stopping watchdog task") comes too late, and
> kthread_stop() runs on a task_struct already freed.

[...]

> Take the reference before the thread is started and drop it with
> kthread_stop_put() when it is stopped.
>
> Fixes: c623b6358088 ("wifi: brcmfmac: Fix potential use-after-free issue when stopping watchdog task")
> Cc: stable@vger.kernel.org
> Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
> ---
>
>  .../net/wireless/broadcom/brcm80211/brcmfmac/sdio.c  | 12 +++++++-----
>  1 file changed, 7 insertions(+), 5 deletions(-)

Acked-by: Arend van Spriel <arend.vanspriel@broadcom.com>

Regards,
Arend
  

Patch

diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
index cea52d408817..67b2291f8ee5 100644
--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
@@ -2478,7 +2478,6 @@  static void brcmf_sdio_bus_stop(struct device *dev)
 	brcmf_dbg(TRACE, "Enter\n");
 
 	if (bus->watchdog_tsk) {
-		get_task_struct(bus->watchdog_tsk);
 		send_sig(SIGTERM, bus->watchdog_tsk, 1);
 		kthread_stop_put(bus->watchdog_tsk);
 		bus->watchdog_tsk = NULL;
@@ -4500,12 +4499,16 @@  int brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
 	timer_setup(&bus->timer, brcmf_sdio_watchdog, 0);
 	/* Initialize watchdog thread */
 	init_completion(&bus->watchdog_wait);
-	bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
-					bus, "brcmf_wdog/%s",
-					dev_name(&sdiodev->func1->dev));
+	bus->watchdog_tsk = kthread_create(brcmf_sdio_watchdog_thread,
+					   bus, "brcmf_wdog/%s",
+					   dev_name(&sdiodev->func1->dev));
 	if (IS_ERR(bus->watchdog_tsk)) {
 		pr_warn("brcmf_watchdog thread failed to start\n");
 		bus->watchdog_tsk = NULL;
+	} else {
+		/* The thread may exit on a SIGTERM before it is stopped */
+		get_task_struct(bus->watchdog_tsk);
+		wake_up_process(bus->watchdog_tsk);
 	}
 	/* Initialize DPC thread */
 	bus->dpc_triggered = false;
@@ -4576,7 +4579,6 @@  void brcmf_sdio_remove(struct brcmf_sdio *bus)
 	if (bus) {
 		/* Stop watchdog task */
 		if (bus->watchdog_tsk) {
-			get_task_struct(bus->watchdog_tsk);
 			send_sig(SIGTERM, bus->watchdog_tsk, 1);
 			kthread_stop_put(bus->watchdog_tsk);
 			bus->watchdog_tsk = NULL;