From: Zhao Li <enderaoelyther@gmail.com>
To: Johannes Berg <johannes@sipsolutions.net>
Cc: "John W. Linville" <linville@tuxdriver.com>,
Javier Lopez <jlopex@gmail.com>,
Jukka Rissanen <jukka.rissanen@linux.intel.com>,
linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: [PATCH 2/2] wifi: mac80211_hwsim: serialize radio removal with frame commands
Date: Wed, 28 Oct 2026 22:17:51 +0800 [thread overview]
Message-ID: <20261028141751.6728-3-enderaoelyther@gmail.com> (raw)
In-Reply-To: <20261028141751.6728-1-enderaoelyther@gmail.com>
Destroying a radio while a frame, TX status, or PMSR command is in
progress can make the command access the radio after it has been freed.
The command handlers keep using the result of an RCU rhashtable lookup
after the lookup has finished. Socket release and network namespace
teardown can concurrently remove and free that radio.
Serialize non-dump generic netlink command handlers and asynchronous
removal paths with an hwsim-private mutex. Use the same serialization for
virtio dispatch, which invokes the handlers outside generic netlink.
LLM assistance was used to find the bug and review.
Fixes: e9ed49bf4c2c ("mac80211-hwsim: Add support for HWSIM_ATTR_DESTROY_RADIO_ON_CLOSE")
Cc: stable@vger.kernel.org
Signed-off-by: Zhao Li <enderaoelyther@gmail.com>
---
Validation: A W=1 module build with MODPOST passed. A forced
QEMU/KASAN race between HWSIM_CMD_FRAME and socket-close removal changed 13
unique use-after-free reports to 0, with no lockdep splat after the change.
Network-namespace, virtio, TX-status and PMSR paths were not run.
A reproducer is available on request.
Sparse C=2 passed. Targeted Smatch introduced no new diagnostics.
.../wireless/virtual/mac80211_hwsim_main.c | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/drivers/net/wireless/virtual/mac80211_hwsim_main.c b/drivers/net/wireless/virtual/mac80211_hwsim_main.c
index fcbc7f4355bd..61d0e2c1d00c 100644
--- a/drivers/net/wireless/virtual/mac80211_hwsim_main.c
+++ b/drivers/net/wireless/virtual/mac80211_hwsim_main.c
@@ -673,6 +673,7 @@ static const struct nl80211_vendor_cmd_info mac80211_hwsim_vendor_events[] = {
};
DEFINE_SPINLOCK(hwsim_radio_lock);
+static DEFINE_MUTEX(hwsim_radio_mutex);
LIST_HEAD(hwsim_radios);
static struct rhashtable hwsim_radios_rht;
static int hwsim_radio_idx;
@@ -6310,6 +6311,7 @@ static void mac80211_hwsim_free(void)
{
struct mac80211_hwsim_data *data;
+ mutex_lock(&hwsim_radio_mutex);
spin_lock_bh(&hwsim_radio_lock);
while ((data = list_first_entry_or_null(&hwsim_radios,
struct mac80211_hwsim_data,
@@ -6323,6 +6325,7 @@ static void mac80211_hwsim_free(void)
spin_lock_bh(&hwsim_radio_lock);
}
spin_unlock_bh(&hwsim_radio_lock);
+ mutex_unlock(&hwsim_radio_mutex);
class_unregister(&hwsim_class);
}
@@ -7153,12 +7156,28 @@ static const struct genl_small_ops hwsim_ops[] = {
},
};
+static int hwsim_genl_pre_doit(const struct genl_split_ops *ops,
+ struct sk_buff *skb, struct genl_info *info)
+{
+ mutex_lock(&hwsim_radio_mutex);
+
+ return 0;
+}
+
+static void hwsim_genl_post_doit(const struct genl_split_ops *ops,
+ struct sk_buff *skb, struct genl_info *info)
+{
+ mutex_unlock(&hwsim_radio_mutex);
+}
+
static struct genl_family hwsim_genl_family __ro_after_init = {
.name = "MAC80211_HWSIM",
.version = 1,
.maxattr = HWSIM_ATTR_MAX,
.policy = hwsim_genl_policy,
.netnsok = true,
+ .pre_doit = hwsim_genl_pre_doit,
+ .post_doit = hwsim_genl_post_doit,
.module = THIS_MODULE,
.small_ops = hwsim_ops,
.n_small_ops = ARRAY_SIZE(hwsim_ops),
@@ -7172,6 +7191,7 @@ static void remove_user_radios(u32 portid, int netgroup)
struct mac80211_hwsim_data *entry, *tmp;
LIST_HEAD(list);
+ mutex_lock(&hwsim_radio_mutex);
spin_lock_bh(&hwsim_radio_lock);
list_for_each_entry_safe(entry, tmp, &hwsim_radios, list) {
if (entry->destroy_on_close && entry->portid == portid &&
@@ -7189,6 +7209,7 @@ static void remove_user_radios(u32 portid, int netgroup)
mac80211_hwsim_del_radio(entry, wiphy_name(entry->hw->wiphy),
NULL);
}
+ mutex_unlock(&hwsim_radio_mutex);
}
static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
@@ -7248,6 +7269,7 @@ static void __net_exit hwsim_exit_net(struct net *net)
struct mac80211_hwsim_data *data, *tmp;
LIST_HEAD(list);
+ mutex_lock(&hwsim_radio_mutex);
spin_lock_bh(&hwsim_radio_lock);
list_for_each_entry_safe(data, tmp, &hwsim_radios, list) {
if (!net_eq(wiphy_net(data->hw->wiphy), net))
@@ -7270,6 +7292,7 @@ static void __net_exit hwsim_exit_net(struct net *net)
wiphy_name(data->hw->wiphy),
NULL);
}
+ mutex_unlock(&hwsim_radio_mutex);
ida_free(&hwsim_netgroup_ida, hwsim_net_get_netgroup(net));
}
@@ -7364,7 +7387,9 @@ static void hwsim_virtio_rx_work(struct work_struct *work)
skb_reset_tail_pointer(skb);
len = min(len, skb_end_offset(skb));
skb_put(skb, len);
+ mutex_lock(&hwsim_radio_mutex);
hwsim_virtio_handle_cmd(skb);
+ mutex_unlock(&hwsim_radio_mutex);
spin_lock_irqsave(&hwsim_virtio_lock, flags);
if (!hwsim_virtio_enabled) {
--
2.56.0
next prev parent reply other threads:[~2026-10-28 14:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-28 14:17 [PATCH 0/2] wifi: mac80211_hwsim: fix netlink release and radio removal races Zhao Li
2026-10-28 14:17 ` [PATCH 1/2] wifi: mac80211_hwsim: ignore non-generic netlink releases Zhao Li
2026-10-28 14:17 ` Zhao Li [this message]
2026-10-29 8:38 ` [syzbot ci] Re: wifi: mac80211_hwsim: fix netlink release and radio removal races syzbot ci
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261028141751.6728-3-enderaoelyther@gmail.com \
--to=enderaoelyther@gmail.com \
--cc=jlopex@gmail.com \
--cc=johannes@sipsolutions.net \
--cc=jukka.rissanen@linux.intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-wireless@vger.kernel.org \
--cc=linville@tuxdriver.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.