Skip to contentSkip to conversation

ath12k: fix NULL pointer dereference in rhash table destroy

PATCH Applied Jose Ignacio Tornos Martinez 3 participants 6 messages Jun 15 – Jul 13

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ath12k: fix NULL pointer dereference in rhash table destroy

When unbinding the ath12k driver, kernel NULL pointer dereferences occur in irq_work_sync() called from rhashtable_destroy().

Two hash tables are affected:
1. ath12k_link_sta hash table in ath12k_base
2. ath12k_dp_link_peer hash table in ath12k_dp

The issue happens because the destroy functions are called unconditionally in cleanup paths, but the hash tables are only initialized late in their respective init functions. If the device was never fully started or if the init functions failed before initializing the hash tables, the pointers will be NULL. The issues are always reproducible from a VM because the MSI addressing initialization is failing.

Call trace for ath12k_link_sta_rhash_tbl_destroy:

 RIP: irq_work_sync+0x1e/0x70
 rhashtable_destroy+0x12/0x60
 ath12k_link_sta_rhash_tbl_destroy+0x19/0x40 [ath12k]
 ath12k_core_stop+0xe/0x80 [ath12k]
 ath12k_core_hw_group_cleanup+0x6b/0xb0 [ath12k]
 ath12k_pci_remove+0x60/0x110 [ath12k]

Call trace for ath12k_dp_link_peer_rhash_tbl_destroy:

 RIP: irq_work_sync+0x1e/0x70
 rhashtable_destroy+0x12/0x60
 ath12k_dp_link_peer_rhash_tbl_destroy+0x29/0x50 [ath12k]
 ath12k_dp_cmn_device_deinit+0x21/0x140 [ath12k]
 ath12k_core_hw_group_cleanup+0x6b/0xb0 [ath12k]
 ath12k_pci_remove+0x60/0x110 [ath12k]

Fix this by adding NULL checks before calling rhashtable_destroy() in both destroy functions.

The NULL check approach was chosen because the rhashtable pointer serves as the initialization state indicator. The init can fail at various points, leaving some components uninitialized. Checking the pointer directly is simpler than adding separate state flags that would need synchronization.

Fixes 57ccca410237 ("wifi: ath12k: Add hash table for ath12k_link_sta in ath12k_base")
Fixes a88cf5f71adf ("wifi: ath12k: Add hash table for ath12k_dp_link_peer")

---
v2: - Use guard(mutex) instead of manual mutex_lock/unlock with goto in
ath12k_dp_link_peer_rhash_tbl_destroy (suggested by Jeff Johnson)
- Add rationale paragraph in commit message explaining why NULL check approach was chosen over state flags
v1: https://lore.kernel.org/all/[email protected]/

 drivers/net/wireless/ath/ath12k/dp_peer.c | 7 +++++--
 drivers/net/wireless/ath/ath12k/peer.c    | 3 +++
 2 files changed, 8 insertions(+), 2 deletions(-)
drivers/net/wireless/ath/ath12k/dp_peer.c +5 -2
index a1100782d45e..ab3e3e107782 100644
@@ -274,11 +274,14 @@ int ath12k_dp_link_peer_rhash_tbl_init(struct ath12k_dp *dp)
 
 void ath12k_dp_link_peer_rhash_tbl_destroy(struct ath12k_dp *dp)
 {
-	mutex_lock(&dp->link_peer_rhash_tbl_lock);
+	guard(mutex)(&dp->link_peer_rhash_tbl_lock);
+
+	if (!dp->rhead_peer_addr)
+		return;
+
 	rhashtable_destroy(dp->rhead_peer_addr);
 	kfree(dp->rhead_peer_addr);
 	dp->rhead_peer_addr = NULL;
-	mutex_unlock(&dp->link_peer_rhash_tbl_lock);
 }
 
 static int ath12k_dp_link_peer_rhash_insert(struct ath12k_dp *dp,
drivers/net/wireless/ath/ath12k/peer.c +3 -0
index 2e875176baaa..80fee2ce68f1 100644
@@ -444,6 +444,9 @@ int ath12k_link_sta_rhash_tbl_init(struct ath12k_base *ab)
 
 void ath12k_link_sta_rhash_tbl_destroy(struct ath12k_base *ab)
 {
+	if (!ab->rhead_sta_addr)
+		return;
+
 	rhashtable_destroy(ab->rhead_sta_addr);
 	kfree(ab->rhead_sta_addr);
 	ab->rhead_sta_addr = NULL;
Signature · 1 line
2.54.0
#1 lore raw

Gentle ping on this patch.

This fixes a NULL pointer dereference during driver unbind that crashes the kernel when initialization failed partially. The crash is 100% reproducible when unbinding after an initialization failure.

This is particularly critical for VM environments with VFIO passthrough.

Regarding the concern from v1 about preferring symmetric init/deinit:
I understand the preference for unwinding init failures at each stage. However, implementing full symmetric cleanup would require extensive refactoring of multiple error paths across ath12k_core_start(), ath12k_dp_alloc(), and related initialization functions.

The NULL check approach provides a safe, minimal fix that:
1. Prevents the crash without changing complex init logic
2. Follows the same pattern used elsewhere in the kernel for conditional cleanup (e.g., other rhashtable users)
3. Has been tested and validated in the failing scenario

I've addressed the guard(mutex) feedback from v1 in this v2.

If Qualcomm engineering prefers a different approach, I'm happy to revise, but no alternative has been suggested since the v1 discussion.

Please let me know if there are any other concerns.

Thanks

Best regards
Jose Ignacio

Reviewed-by on v2 1/1
#2 lore raw
In reply to Jose Ignacio Tornos Martinez
Quoted 39 lines · Jose Ignacio Tornos Martinez

When unbinding the ath12k driver, kernel NULL pointer dereferences occur in irq_work_sync() called from rhashtable_destroy().

Two hash tables are affected:
1. ath12k_link_sta hash table in ath12k_base
2. ath12k_dp_link_peer hash table in ath12k_dp

The issue happens because the destroy functions are called unconditionally in cleanup paths, but the hash tables are only initialized late in their respective init functions. If the device was never fully started or if the init functions failed before initializing the hash tables, the pointers will be NULL. The issues are always reproducible from a VM because the MSI addressing initialization is failing.

Call trace for ath12k_link_sta_rhash_tbl_destroy:

  RIP: irq_work_sync+0x1e/0x70
  rhashtable_destroy+0x12/0x60
  ath12k_link_sta_rhash_tbl_destroy+0x19/0x40 [ath12k]
  ath12k_core_stop+0xe/0x80 [ath12k]
  ath12k_core_hw_group_cleanup+0x6b/0xb0 [ath12k]
  ath12k_pci_remove+0x60/0x110 [ath12k]

Call trace for ath12k_dp_link_peer_rhash_tbl_destroy:

  RIP: irq_work_sync+0x1e/0x70
  rhashtable_destroy+0x12/0x60
  ath12k_dp_link_peer_rhash_tbl_destroy+0x29/0x50 [ath12k]
  ath12k_dp_cmn_device_deinit+0x21/0x140 [ath12k]
  ath12k_core_hw_group_cleanup+0x6b/0xb0 [ath12k]
  ath12k_pci_remove+0x60/0x110 [ath12k]

Fix this by adding NULL checks before calling rhashtable_destroy() in both destroy functions.

The NULL check approach was chosen because the rhashtable pointer serves as the initialization state indicator. The init can fail at various points, leaving some components uninitialized. Checking the pointer directly is simpler than adding separate state flags that would need synchronization.

Quoted 4 lines
Fixes 57ccca410237 ("wifi: ath12k: Add hash table for ath12k_link_sta in ath12k_base")
Fixes a88cf5f71adf ("wifi: ath12k: Add hash table for ath12k_dp_link_peer")
#3 lore raw
Quoted 42 lines · Vasanthakumar Thiagarajan
Quoted 39 lines · Jose Ignacio Tornos Martinez

When unbinding the ath12k driver, kernel NULL pointer dereferences occur in irq_work_sync() called from rhashtable_destroy().

Two hash tables are affected:
1. ath12k_link_sta hash table in ath12k_base
2. ath12k_dp_link_peer hash table in ath12k_dp

The issue happens because the destroy functions are called unconditionally in cleanup paths, but the hash tables are only initialized late in their respective init functions. If the device was never fully started or if the init functions failed before initializing the hash tables, the pointers will be NULL. The issues are always reproducible from a VM because the MSI addressing initialization is failing.

Call trace for ath12k_link_sta_rhash_tbl_destroy:

  RIP: irq_work_sync+0x1e/0x70
  rhashtable_destroy+0x12/0x60
  ath12k_link_sta_rhash_tbl_destroy+0x19/0x40 [ath12k]
  ath12k_core_stop+0xe/0x80 [ath12k]
  ath12k_core_hw_group_cleanup+0x6b/0xb0 [ath12k]
  ath12k_pci_remove+0x60/0x110 [ath12k]

Call trace for ath12k_dp_link_peer_rhash_tbl_destroy:

  RIP: irq_work_sync+0x1e/0x70
  rhashtable_destroy+0x12/0x60
  ath12k_dp_link_peer_rhash_tbl_destroy+0x29/0x50 [ath12k]
  ath12k_dp_cmn_device_deinit+0x21/0x140 [ath12k]
  ath12k_core_hw_group_cleanup+0x6b/0xb0 [ath12k]
  ath12k_pci_remove+0x60/0x110 [ath12k]

Fix this by adding NULL checks before calling rhashtable_destroy() in both destroy functions.

The NULL check approach was chosen because the rhashtable pointer serves as the initialization state indicator. The init can fail at various points, leaving some components uninitialized. Checking the pointer directly is simpler than adding separate state flags that would need synchronization.

Quoted 6 lines
Quoted 4 lines
Fixes 57ccca410237 ("wifi: ath12k: Add hash table for ath12k_link_sta in ath12k_base")
Fixes a88cf5f71adf ("wifi: ath12k: Add hash table for ath12k_dp_link_peer")

Missed to mention that pls add wifi prefix to the patch title.

#4 lore raw
Quoted 45 lines · Vasanthakumar Thiagarajan
Quoted 42 lines · Vasanthakumar Thiagarajan
Quoted 39 lines · Jose Ignacio Tornos Martinez

When unbinding the ath12k driver, kernel NULL pointer dereferences occur in irq_work_sync() called from rhashtable_destroy().

Two hash tables are affected:
1. ath12k_link_sta hash table in ath12k_base
2. ath12k_dp_link_peer hash table in ath12k_dp

The issue happens because the destroy functions are called unconditionally in cleanup paths, but the hash tables are only initialized late in their respective init functions. If the device was never fully started or if the init functions failed before initializing the hash tables, the pointers will be NULL. The issues are always reproducible from a VM because the MSI addressing initialization is failing.

Call trace for ath12k_link_sta_rhash_tbl_destroy:

  RIP: irq_work_sync+0x1e/0x70
  rhashtable_destroy+0x12/0x60
  ath12k_link_sta_rhash_tbl_destroy+0x19/0x40 [ath12k]
  ath12k_core_stop+0xe/0x80 [ath12k]
  ath12k_core_hw_group_cleanup+0x6b/0xb0 [ath12k]
  ath12k_pci_remove+0x60/0x110 [ath12k]

Call trace for ath12k_dp_link_peer_rhash_tbl_destroy:

  RIP: irq_work_sync+0x1e/0x70
  rhashtable_destroy+0x12/0x60
  ath12k_dp_link_peer_rhash_tbl_destroy+0x29/0x50 [ath12k]
  ath12k_dp_cmn_device_deinit+0x21/0x140 [ath12k]
  ath12k_core_hw_group_cleanup+0x6b/0xb0 [ath12k]
  ath12k_pci_remove+0x60/0x110 [ath12k]

Fix this by adding NULL checks before calling rhashtable_destroy() in both destroy functions.

The NULL check approach was chosen because the rhashtable pointer serves as the initialization state indicator. The init can fail at various points, leaving some components uninitialized. Checking the pointer directly is simpler than adding separate state flags that would need synchronization.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Fixes 57ccca410237 ("wifi: ath12k: Add hash table for ath12k_link_sta in ath12k_base")
Fixes a88cf5f71adf ("wifi: ath12k: Add hash table for ath12k_dp_link_peer")

Missed to mention that pls add wifi prefix to the patch title.

I'll fix this in my pending branch.

/jeff

Applied
#5 lore raw
In reply to Jose Ignacio Tornos Martinez
Quoted 8 lines · Jose Ignacio Tornos Martinez

When unbinding the ath12k driver, kernel NULL pointer dereferences occur in irq_work_sync() called from rhashtable_destroy().

Two hash tables are affected:
1. ath12k_link_sta hash table in ath12k_base
2. ath12k_dp_link_peer hash table in ath12k_dp

[...]

Applied, thanks!

[1/1] ath12k: fix NULL pointer dereference in rhash table destroy

      commit: 70231dcd782201579990ded73e0435d18bb524ca

Best regards,

Signature · 1 line
Jeff Johnson <[email protected]>