Skip to contentSkip to conversation

mac802154: lock rx_mac_cmd_list and drain it before freeing sdata

PATCH In review Ibrahim Hashimov 3 participants 3 messages Jul 22 – Jul 24

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

PATCH v2 1/1 mac802154: lock rx_mac_cmd_list and drain it before freeing sdata

rx_mac_cmd_list has no locking. The RX softirq producer in ieee802154_subif_frame() list_add_tail()s while the mac_wq worker list_del()s, and the worker dereferences mac_pkt->sdata with no liveness check.

Removing an interface (or the phy) thus frees its sdata while a queued packet still points at it, and a later worker run reads freed memory: a KASAN use-after-free, reproduced by flooding a victim interface with MAC command frames and deleting it. Meanwhile sibling interfaces on the same phy keep adding to the list, corrupting it.

Add rx_mac_cmd_lock around every access -- producer, worker (which now dequeues under the lock and processes the packet after) and the new mac802154_flush_queued_mac_cmds(). Drain the queue before the sdata is
freed: from ieee802154_if_remove() after the RCU grace period (filtered to that sdata) and from ieee802154_unregister_hw(). The drain precedes cancel_work_sync() so a run that already dequeued a matching packet is waited out while its sdata is still alive.

Fixes d021d218f6d9 ("mac802154: Handle received BEACON_REQ")
Assisted-by AuditCode-AI:2026.07

---
v2: v1 only added mac802154_flush_queued_mac_cmds() guarded by
cancel_work_sync(), which cannot fix a race whose corrupting party is the lockless softirq producer (not a work item), as the review of v1 pointed out. Add rx_mac_cmd_lock around every list access and drain before cancel_work_sync(). rx_beacon_list has the same lockless pattern, but its worker never dereferences sdata, so it is left as a separate follow-up to keep this fix minimal.

net/mac802154/ieee802154_i.h +6 -0
index 8f2bff268392..b497b5abab0a 100644
@@ -74,6 +74,10 @@ struct ieee802154_local {
 	struct work_struct rx_beacon_work;
 	struct list_head rx_mac_cmd_list;
 	struct work_struct rx_mac_cmd_work;
+	/* protects rx_mac_cmd_list against the RX softirq producer, the
+	 * mac_wq worker and the teardown flush running concurrently
+	 */
+	spinlock_t rx_mac_cmd_lock;
 
 	/* Association */
 	struct ieee802154_pan_device *assoc_dev;
@@ -300,6 +304,8 @@ static inline bool mac802154_is_beaconing(struct ieee802154_local *local)
 }
 
 void mac802154_rx_mac_cmd_worker(struct work_struct *work);
+void mac802154_flush_queued_mac_cmds(struct ieee802154_local *local,
+				     struct ieee802154_sub_if_data *sdata);
 
 int mac802154_perform_association(struct ieee802154_sub_if_data *sdata,
 				  struct ieee802154_pan_device *coord,
net/mac802154/iface.c +7 -0
index b823720630e7..82c86f253b5d 100644
@@ -694,6 +694,13 @@ void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata)
 	mutex_unlock(&sdata->local->iflist_mtx);
 
 	synchronize_rcu();
+
+	/* After the grace period no new rx_mac_cmd_list entry can point at
+	 * @sdata; drop the ones already queued before it is freed below, as
+	 * the mac_wq worker derefs mac_pkt->sdata with no liveness check.
+	 */
+	mac802154_flush_queued_mac_cmds(sdata->local, sdata);
+
 	unregister_netdevice(sdata->dev);
 }
 
net/mac802154/main.c +7 -0
index ea1efef3572a..24d1e398d3d9 100644
@@ -91,6 +91,7 @@ ieee802154_alloc_hw(size_t priv_data_len, const struct ieee802154_ops *ops)
 	INIT_LIST_HEAD(&local->interfaces);
 	INIT_LIST_HEAD(&local->rx_beacon_list);
 	INIT_LIST_HEAD(&local->rx_mac_cmd_list);
+	spin_lock_init(&local->rx_mac_cmd_lock);
 	mutex_init(&local->iflist_mtx);
 
 	tasklet_setup(&local->tasklet, ieee802154_tasklet_handler);
@@ -277,6 +278,12 @@ void ieee802154_unregister_hw(struct ieee802154_hw *hw)
 	tasklet_kill(&local->tasklet);
 	flush_workqueue(local->workqueue);
 
+	/* Drain rx_mac_cmd_list before ieee802154_remove_interfaces()
+	 * frees every sdata: the mac_wq worker derefs mac_pkt->sdata and
+	 * is not covered by the flush above (that is the data workqueue).
+	 */
+	mac802154_flush_queued_mac_cmds(local, NULL);
+
 	rtnl_lock();
 
 	ieee802154_remove_interfaces(local);
net/mac802154/rx.c +46 -1

72 lines not on this page · raw message

Signature · 1 line
2.50.1 (Apple Git-155)
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 20 lines

rx_mac_cmd_list has no locking. The RX softirq producer in ieee802154_subif_frame() list_add_tail()s while the mac_wq worker list_del()s, and the worker dereferences mac_pkt->sdata with no liveness check.

Removing an interface (or the phy) thus frees its sdata while a queued packet still points at it, and a later worker run reads freed memory: a KASAN use-after-free, reproduced by flooding a victim interface with MAC command frames and deleting it. Meanwhile sibling interfaces on the same phy keep adding to the list, corrupting it.

Add rx_mac_cmd_lock around every access -- producer, worker (which now dequeues under the lock and processes the packet after) and the new mac802154_flush_queued_mac_cmds(). Drain the queue before the sdata is
freed: from ieee802154_if_remove() after the RCU grace period (filtered to that sdata) and from ieee802154_unregister_hw(). The drain precedes cancel_work_sync() so a run that already dequeued a matching packet is waited out while its sdata is still alive.

Quoted 5 lines
Fixes d021d218f6d9 ("mac802154: Handle received BEACON_REQ")
Assisted-by AuditCode-AI:2026.07

---

Looks fine to me, but honestly this is flirting with the edge of my human memory.

maintainer
#2 lore raw
In reply to Ibrahim Hashimov
Quoted 4 lines · On Wed, 22 Jul 2026 12:16:08 +0200 Ibrahim Hashimov wrote:

rx_mac_cmd_list has no locking. The RX softirq producer in ieee802154_subif_frame() list_add_tail()s while the mac_wq worker list_del()s, and the worker dereferences mac_pkt->sdata with no liveness check.

Both sashikos complain about races:
https://sashiko.dev/#/patchset/[email protected]
https://netdev-ai.bots.linux.dev/sashiko/#/patchset/[email protected]