wifi: ath11k: give the regulatory state the lifetime of the device
Subsystems (2)
cover wifi: ath11k: give the regulatory state the lifetime of the device
ab->reg_info_store, with the rules it holds, and the default_regd[] and new_regd[] domains live as long as struct ath11k_base, but their teardown does not follow that lifetime. The SERVICE_READY_EXT error path frees the store from a transient event handler, and the remove paths that follow a failed start never call ath11k_reg_free().
Patch 1 lets ath11k_reg_free() cope with a store that was never allocated. Patch 2 moves ath11k_reg_free() into ath11k_core_free(), so that every teardown path frees the regulatory state. Patch 3 then drops the free from the SERVICE_READY_EXT error path. In this order no step adds a crash or a leak.
A separate patch, sent on its own, makes a failed SERVICE_READY_EXT fail the start instead of letting SERVICE_READY_EXT2 complete it.
Zhao Li (3):
wifi: ath11k: don't walk an unallocated regulatory info store
wifi: ath11k: free the regulatory state when the device is freed
wifi: ath11k: keep the regulatory info store on a SERVICE_READY_EXT
error
drivers/net/wireless/ath/ath11k/core.c | 2 +- drivers/net/wireless/ath/ath11k/reg.c | 6 ++++-- drivers/net/wireless/ath/ath11k/wmi.c | 9 --------- 3 files changed, 5 insertions(+), 12 deletions(-)
base-commit: b1ed5a45b994a6473ca1cf34f35a1bf87f1d8e2a
Signature · 1 line
2.56.1
PATCH 1/3 wifi: ath11k: don't walk an unallocated regulatory info store
ath11k_reg_free() resets the regulatory information of every radio in ab->reg_info_store before it frees the store, without checking that the store exists. SERVICE_READY_EXT counts the radios before it allocates the store, and since commit 64828f091c7d ("wifi: ath11k: fix reg_info_store leak in ath11k_service_ready_ext_event()") its error path can also free the store after a firmware restart, so ab->num_radios can be set while the store is NULL. A later ath11k_reg_free() then resets the radios through the NULL store and faults on the second radio.
Reset the radios only when the store exists.
AI assistance was used.
---
Found by code review. Tested in QEMU with KASAN, using a test module and a test-only allocation failure in the SERVICE_READY_EXT handler that leave a two-radio synthetic device without a store, and then calls ath11k_reg_free(): without this change the call faults in ath11k_reg_free(); with the series applied it completes.
No new W=1, sparse or smatch warnings.
drivers/net/wireless/ath/ath11k/reg.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/wireless/ath/ath11k/reg.c +4 -2
@@ -1049,8 +1049,10 @@ void ath11k_reg_free(struct ath11k_base *ab) {int i; -for (i =0 ; i < ab->num_radios; i++) -ath11k_reg_reset_info (&ab->reg_info_store[i]); +if (ab->reg_info_store) { +for (i =0 ; i < ab->num_radios; i++) +ath11k_reg_reset_info (&ab->reg_info_store[i]); + }kfree (ab->reg_info_store); ab->reg_info_store =NULL ;
Signature · 1 line
2.56.1
PATCH 2/3 wifi: ath11k: free the regulatory state when the device is freed
In reply to Zhao LiThe regulatory state of struct ath11k_base, the reg_info_store array with the rules it holds and the default_regd[] and new_regd[] domains, lives as long as the device, but ath11k_reg_free() runs from ath11k_core_soc_destroy(), which only ath11k_core_deinit() calls. The teardown paths that follow a failed start skip ath11k_core_deinit():ath11k_pci_remove() when ATH11K_FLAG_QMI_FAIL is set, and ath11k_ahb_remove() and ath11k_ahb_shutdown() when ATH11K_FLAG_REGISTERED is clear. They reach ath11k_core_free(), which frees ab, and whatever regulatory state the failed start allocated leaks.
Free the regulatory state in ath11k_core_free(), which every teardown path reaches, right before ab itself.
AI assistance was used.
---
Found by code review. Tested in QEMU with KASAN, using a test module that frees a synthetic device through ath11k_core_free() after a SERVICE_READY_EXT event allocated the store, then reads the store to see whether it was freed: without this change the read succeeds, so the store leaks; with the series applied KASAN reports the read as a use-after-free of the store that ath11k_reg_free() freed from ath11k_core_free().
No new W=1, sparse or smatch warnings.
drivers/net/wireless/ath/ath11k/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/wireless/ath/ath11k/core.c +1 -1
@@ -2021,7 +2021,6 @@ static void ath11k_core_soc_destroy(struct ath11k_base *ab) {ath11k_debugfs_soc_destroy (ab);ath11k_dp_free (ab); -ath11k_reg_free (ab);ath11k_qmi_deinit_service (ab); }
@@ -2734,6 +2733,7 @@ void ath11k_core_free(struct ath11k_base *ab) {destroy_workqueue (ab->workqueue_aux);destroy_workqueue (ab->workqueue); +ath11k_reg_free (ab);kfree (ab); }
Signature · 1 line
2.56.1
PATCH 3/3 wifi: ath11k: keep the regulatory info store on a SERVICE_READY_EXT error
In reply to Zhao Liab->reg_info_store lives as long as the device. The first SERVICE_READY_EXT event allocates it, every later firmware start reuses it, the regulatory events keep the rules of each radio in it, and ath11k_reg_free() frees it together with those rules when the device is freed.
Commit 64828f091c7d ("wifi: ath11k: fix reg_info_store leak in ath11k_service_ready_ext_event()") frees the store and clears the pointer on the SERVICE_READY_EXT error path. When the event fails after a firmware restart, that leaks the rules the store holds and leaves the store NULL while the radios stay registered with mac80211:ath11k_reg_handle_chan_list() then writes through the NULL pointer, and the 6 GHz paths of ath11k_mac_op_add_interface(), ath11k_mac_parse_tx_pwr_env() and ath11k_mac_op_sta_state() read through it. The leak that commit addressed, a store allocated by a first start that later fails, no longer needs it: ath11k_core_free() frees the regulatory state on every teardown path.
Do not free the store on the error path.
AI assistance was used.
---
Found by code review. Tested in QEMU with KASAN, using a test module that runs a SERVICE_READY_EXT and a regulatory event on a synthetic device and then restarts it with a failing SERVICE_READY_EXT: without this change the store pointer is cleared; with the series applied the store keeps the country code and the rule count of the first start.
No new W=1, sparse or smatch warnings.
drivers/net/wireless/ath/ath11k/wmi.c | 9 --------- 1 file changed, 9 deletions(-)
drivers/net/wireless/ath/ath11k/wmi.c +0 -9
@@ -5136,15 +5136,6 @@ static int ath11k_service_ready_ext_event(struct ath11k_base *ab, err:kfree (svc_rdy_ext.mac_phy_caps); - -/* -* During TLV iteration the WMI_TAG_SOC_HAL_REG_CAPABILITIES -* handler may have allocated ab->reg_info_store, so free it -* on error to avoid a memory leak -*/ -kfree (ab->reg_info_store); - ab->reg_info_store =NULL ; -ath11k_wmi_free_dbring_caps (ab);return ret; }
Signature · 1 line
2.56.1