Skip to contentSkip to conversation

wifi: mac80211_hwsim: drain address lookup users before radio removal

PATCH New Zhao Li 1 participant 1 message Nov 15

Subsystems (2)
lore
Share & tools
lore

Zhao Li

author
#0 lore raw

PATCH v3 1/1 wifi: mac80211_hwsim: drain address lookup users before radio removal

Closing the socket that owns a destroy-on-close radio can free the radio while a frame, TX status, or PMSR command still uses the pointer returned by get_hwsim_data_ref_from_addr(). A forced QEMU/KASAN run reproduced the resulting use-after-free in hwsim_cloned_frame_received_nl().

rhashtable_lookup_fast() protects only the lookup itself. The hwsim deletion paths already unpublish a radio from the hash table under hwsim_radio_lock before performing the sleeping teardown.

Take a per-radio reference under hwsim_radio_lock when an address lookup succeeds. After unpublishing a radio, drop its publication reference and wait for lookup users before unregistering the hardware. This avoids serializing unrelated radios with asynchronous teardown while preventing teardown from freeing an active lookup result.

AI assistance was used.

Fixes e9ed49bf4c2c ("mac80211-hwsim: Add support for HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE")

---
Changes in v3:
- Resend with git send-email. v2 went out through a Gmail connector that line-wrapped the diff, so it does not apply. No code changes.

v2: https://lore.kernel.org/linux-wireless/CACHPgzxg0i5S5hKyuSLjNYhLKr7ZoxKO=VefOtsiU6zgod=yfg@mail.gmail.com/

Changes in v2:
- Replace the family-wide command/removal mutex with per-radio lookup references.
- Unpublish before dropping the publication reference, then drain lookup users before hardware teardown.
- Cover all four lookup sites and make this a standalone patch on wireless/main, where patch 1/2 is already merged.

Testing:
- GCC 12.2 W=1 module/built-in builds; sparse, Smatch and Coccinelle.
- QEMU/KASAN/lockdep forced removal at all four lookup sites. Every base contrast reproduced the use-after-free; the fixed revision stayed clean and passed per-radio isolation, real-cookie TX status and teardown churn.
- Not covered: PMSR normal completion, virtio dispatch and module exit.

 .../net/wireless/virtual/mac80211_hwsim_i.h   |  4 ++
 .../wireless/virtual/mac80211_hwsim_main.c    | 47 +++++++++++++++----
 2 files changed, 43 insertions(+), 8 deletions(-)
drivers/net/wireless/virtual/mac80211_hwsim_i.h +4 -0
index 0f0f2ac6d80e..c91bbd99b5a2 100644
@@ -10,6 +10,8 @@
 #ifndef __MAC80211_HWSIM_I_H
 #define __MAC80211_HWSIM_I_H
 
+#include <linux/completion.h>
+#include <linux/refcount.h>
 #include <net/mac80211.h>
 #include "mac80211_hwsim.h"
 #include "mac80211_hwsim_nan.h"
@@ -51,6 +53,8 @@ struct mac80211_hwsim_link_data {
 struct mac80211_hwsim_data {
 	struct list_head list;
 	struct rhash_head rht;
+	refcount_t users;
+	struct completion users_done;
 	struct ieee80211_hw *hw;
 	struct device *dev;
 	struct ieee80211_supported_band bands[NUM_NL80211_BANDS];
drivers/net/wireless/virtual/mac80211_hwsim_main.c +39 -8

115 lines not on this page · raw message

base-commit: 831e33ad02ff884ecc793c07a9c9bd9e06941fe5

Signature · 1 line
2.56.1