cfg80211 syzbot fixes
Johannes Berg
maintainercover cfg80211 syzbot fixes
Maybe this was the plan all along with the enablement of all the LLM stuff for syzbot? I created all of these before my vacation, but only did the polishing (of which a lot was needed) now.
johannes
Johannes Berg
maintainerPATCH 1/8 wifi: cfg80211: don't get the radio mask for netdev-less wdevs
From: Johannes Berg <[email protected]>
cfg80211_calculate_bi_data() calls rdev_get_radio_mask() with wdev->netdev, which can be NULL and then crashes in mac80211.
To avoid that, invert the order of checks since wdev->netdev is always valid for beaconing interfaces.
--- net/wireless/util.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
net/wireless/util.c +4 -5
@@ -2477,16 +2477,15 @@ static void cfg80211_calculate_bi_data(struct wiphy *wiphy, u32 new_beacon_int,if (wdev->valid_links)continue ; + wdev_bi =cfg80211_wdev_bi (wdev); +if (!wdev_bi) +continue ; +/* skip wdevs not active on the given wiphy radio */ if (radio_idx >=0 && !(rdev_get_radio_mask (rdev, wdev->netdev) &BIT (radio_idx)))continue ; - wdev_bi =cfg80211_wdev_bi (wdev); - -if (!wdev_bi) -continue ; -if (!*beacon_int_gcd) { *beacon_int_gcd = wdev_bi;continue ;
Signature · 1 line
2.55.0
Johannes Berg
maintainerPATCH 2/8 wifi: cfg80211: check IP header size in cfg80211_classify8021d()
In reply to Johannes BergFrom: Johannes Berg <[email protected]>
A frame that looks like IP can be transmitted, but be too short, so the DS field is read incorrectly:
BUG: KMSAN: uninit-value in cfg80211_classify8021d+0x99d/0x12b0 net/wireless/util.c:1027 cfg80211_classify8021d+0x99d/0x12b0 net/wireless/util.c:1027 ieee80211_select_queue+0x37a/0x9e0 net/mac80211/wme.c:180 __ieee80211_subif_start_xmit+0x60f/0x1d90 net/mac80211/tx.c:4304 ieee80211_subif_start_xmit+0xa8/0x6d0 net/mac80211/tx.c:4538 ... packet_sendmsg+0x9173/0xa2a0 net/packet/af_packet.c:3108
Use skb_header_pointer() like the MPLS case.
--- net/wireless/util.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)
net/wireless/util.c +22 -4
@@ -1039,12 +1039,30 @@ unsigned int cfg80211_classify8021d(struct sk_buff *skb, }switch (skb->protocol) { -case htons (ETH_P_IP ): - dscp =ipv4_get_dsfield (ip_hdr (skb)) &0xfc ; +case htons (ETH_P_IP ): { +const struct iphdr *iph; +struct iphdr _iph; + + iph =skb_header_pointer (skb,sizeof (struct ethhdr ), +sizeof (*iph), &_iph); +if (!iph) +return 0 ; + + dscp =ipv4_get_dsfield (iph) &0xfc ;break ; -case htons (ETH_P_IPV6 ): - dscp =ipv6_get_dsfield (ipv6_hdr (skb)) &0xfc ; + } +case htons (ETH_P_IPV6 ): { +const struct ipv6hdr *ip6h; +struct ipv6hdr _ip6h; + + ip6h =skb_header_pointer (skb,sizeof (struct ethhdr ), +sizeof (*ip6h), &_ip6h); +if (!ip6h) +return 0 ; + + dscp =ipv6_get_dsfield (ip6h) &0xfc ;break ; + }case htons (ETH_P_MPLS_UC ):case htons (ETH_P_MPLS_MC ): {struct mpls_label mpls_tmp, *mpls;
Signature · 1 line
2.55.0
Johannes Berg
maintainerPATCH 3/8 wifi: cfg80211: don't free driver-owned scan requests
In reply to Johannes BergFrom: Johannes Berg <[email protected]>
When an interface goes down while a scan is running, cfg80211 completes the scan towards userspace and frees the scan request. However, the driver can be convinced that it owns the request, since the cancellation is (intended to be) asynchronous.
The WARN_ON() in the netdev notifier was meant to catch this, but it's not actually avoidable, so it triggers and we get a UAF in scan_done().
There doesn't seem to be a great way around it, so just track that the driver is still convinced it owns the request, and then just free it on completion if it was already cancelled. Also remove the warnings since they can trigger in the intended architecture.
--- net/wireless/core.c | 11 +++++------ net/wireless/core.h | 10 ++++++++++ net/wireless/rdev-ops.h | 3 +++ net/wireless/scan.c | 31 +++++++++++++++++++++++++++++-- 4 files changed, 47 insertions(+), 8 deletions(-)
net/wireless/core.c +5 -6
@@ -244,9 +244,8 @@ void cfg80211_stop_p2p_device(struct cfg80211_registered_device *rdev, rdev->opencount--;if (rdev->scan_req && rdev->scan_req->req.wdev == wdev) { -if (WARN_ON (!rdev->scan_req->notified && - (!rdev->int_scan_req || - !rdev->int_scan_req->notified))) +if (!rdev->scan_req->notified && + (!rdev->int_scan_req || !rdev->int_scan_req->notified)) rdev->scan_req->info.aborted = true;___cfg80211_scan_done (rdev, false); }
@@ -1758,9 +1757,9 @@ static int cfg80211_netdev_notifier_call(struct notifier_block *nb,wiphy_lock (&rdev->wiphy);cfg80211_update_iface_num (rdev, wdev->iftype,-1 );if (rdev->scan_req && rdev->scan_req->req.wdev == wdev) { -if (WARN_ON (!rdev->scan_req->notified && - (!rdev->int_scan_req || - !rdev->int_scan_req->notified))) +if (!rdev->scan_req->notified && + (!rdev->int_scan_req || + !rdev->int_scan_req->notified)) rdev->scan_req->info.aborted = true;___cfg80211_scan_done (rdev, false); }
net/wireless/core.h +10 -0
@@ -24,6 +24,16 @@struct cfg80211_scan_request_int {struct cfg80211_scan_info info;bool notified; +/* +* set while the request is handed to the driver, i.e. between +* rdev_scan() and cfg80211_scan_done() +*/ +bool driver_owns; +/* +* set when cfg80211 is done with the request but the driver still +* owns it, so that cfg80211_scan_done() knows to just free it +*/ +bool stale;/* must be last - variable members */ struct cfg80211_scan_request req; };
net/wireless/rdev-ops.h +3 -0
@@ -464,7 +464,10 @@ static inline int rdev_scan(struct cfg80211_registered_device *rdev,return -EINVAL ;trace_rdev_scan (&rdev->wiphy, request); + request->driver_owns = true; ret = rdev->ops->scan (&rdev->wiphy, &request->req); +if (ret) + request->driver_owns = false;trace_rdev_return_int (&rdev->wiphy, ret);return ret; }
net/wireless/scan.c +29 -2
@@ -1114,6 +1114,21 @@ int cfg80211_scan(struct cfg80211_registered_device *rdev)return 0 ; } +/* +* Release the scan request, but free it only if the driver is also done, +* e.g. mac80211 may cancel it asynchronously and still use it. +*/ +static void cfg80211_put_scan_req (struct cfg80211_scan_request_int *req) +{ +if (!req) +return ; + +if (req->driver_owns) + req->stale = true; +else +kfree (req); +} +void ___cfg80211_scan_done (struct cfg80211_registered_device *rdev,bool send_message) {
@@ -1173,10 +1188,10 @@ void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,dev_put (wdev->netdev); -kfree (rdev->int_scan_req); +cfg80211_put_scan_req (rdev->int_scan_req); rdev->int_scan_req =NULL ; -kfree (rdev->scan_req); +cfg80211_put_scan_req (rdev->scan_req); rdev->scan_req =NULL ;if (!send_message)
@@ -1199,6 +1214,18 @@ void cfg80211_scan_done(struct cfg80211_scan_request *request,struct cfg80211_scan_info old_info = intreq->info;trace_cfg80211_scan_done (intreq, info); + + intreq->driver_owns = false; + +if (intreq->stale) { +/* +* The scan is already completed as far as we're concerned, +* it was just kept around for the driver - done now, free it. +*/ +kfree (intreq); +return ; + } +WARN_ON (intreq != rdev->scan_req && intreq != rdev->int_scan_req);
Signature · 1 line
2.55.0
Johannes Berg
maintainerPATCH 4/8 wifi: cfg80211: only group hidden BSSes with beacon entries
In reply to Johannes BergFrom: Johannes Berg <[email protected]>
When a probe response for an unknown BSS comes in, __cfg80211_bss_update() looks for an existing entry with the same BSSID and a hidden (zero-length or NUL-filled) SSID, and if it finds one it groups them, using the beacon IEs from the existing entry.
But that could find another entry without a beacon, if it was also from a probe response (with SSID), so there's a group without beacon elements.
If a beacon with a hidden SSID for that BSSID arrives later, cfg80211_combine_bsses() goes looking for the probe response entries that belong to it - i.e. entries with the same BSSID and channel that have no beacon IEs - and finds those two. They are already grouped with each other, so it hits its
WARN_ON_ONCE(bss->pub.hidden_beacon_bss)
WARN_ON_ONCE(!list_empty(&bss->hidden_list))
which are there because an entry without beacon elements is not supposed to be part of a group yet.
Only combine entries when a beacon was already received, ones that are kept separate will be combined when a beacon arrives.
--- net/wireless/scan.c | 7 +++++++ 1 file changed, 7 insertions(+)
net/wireless/scan.c +7 -0
@@ -2077,6 +2077,13 @@ __cfg80211_bss_update(struct cfg80211_registered_device *rdev,if (!hidden) hidden =rb_find_bss (rdev, tmp,BSS_CMP_HIDE_NUL ); +/* +* Only group with an entry with beacon data, otherwise +* beacon data can never be filled/updated. +*/ +if (hidden && + !rcu_access_pointer (hidden->pub.beacon_ies)) + hidden =NULL ;if (hidden) { new->pub.hidden_beacon_bss = &hidden->pub; list_add(&new->hidden_list,
Signature · 1 line
2.55.0
Johannes Berg
maintainerPATCH 5/8 wifi: cfg80211: don't filter by BSS type when removing stale entries
In reply to Johannes BergFrom: Johannes Berg <[email protected]>
When an assoc AP switches to a channel that already has a BSS entry, cfg80211_update_assoc_bss_entry() removes that entry before rehashing the real one, since the two would otherwise collide in the BSS rbtree.
The lookup for that entry also required it to match the connection's BSS type, so an entry advertising e.g. the IBSS capability bit was left in place, and the following cfg80211_rehash_bss() then ran into it:
WARN_ON(!cmp)
Changing the type shouldn't really happen, but can be triggered by a rogue AP/device, so drop the check and remove any entries matching the comparison.
--- net/wireless/scan.c | 5 ----- 1 file changed, 5 deletions(-)
net/wireless/scan.c +0 -5
@@ -3502,11 +3502,6 @@ void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev, cbss->pub.channel = chan;list_for_each_entry (bss, &rdev->bss_list, list) { -if (!cfg80211_bss_type_match (bss->pub.capability, - bss->pub.channel->band, - wdev->conn_bss_type)) -continue ; -if (bss == cbss)continue ;
Signature · 1 line
2.55.0
Johannes Berg
maintainerPATCH 6/8 wifi: cfg80211: ibss: ref BSS entry for joined event
In reply to Johannes BergFrom: Johannes Berg <[email protected]>
When the IBSS is joined, we only record the BSSID/channel in the event and look up the BSS entry when processing it. However, that's racy, e.g. a new scan with NL80211_SCAN_FLAG_FLUSH can remove it, causing a warning in the event work:
!bss
WARNING: net/wireless/ibss.c:37 at __cfg80211_ibss_joined+0x3d3/0x440 Workqueue: cfg80211 cfg80211_event_work cfg80211_process_wdev_events+0x39f/0x5b0 net/wireless/util.c:1144 cfg80211_process_rdev_events+0xa1/0x110 net/wireless/util.c:1179 cfg80211_event_work+0x2f/0x40 net/wireless/core.c:393
Do the lookup early (the driver is expected to only join an IBSS that has a BSS entry) and keep a reference to it.
--- net/wireless/core.h | 6 ++---- net/wireless/ibss.c | 38 +++++++++++++++++++++----------------- net/wireless/util.c | 3 +-- 3 files changed, 24 insertions(+), 23 deletions(-)
net/wireless/core.h +2 -4
@@ -290,8 +290,7 @@ struct cfg80211_event {bool locally_generated; } dc;struct { -u8 bssid[ETH_ALEN ]; -struct ieee80211_channel *channel; +struct cfg80211_bss *bss; } ij;struct {u8 peer_addr[ETH_ALEN ];
@@ -354,8 +353,7 @@ int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,void cfg80211_clear_ibss (struct net_device *dev,bool nowext);int cfg80211_leave_ibss (struct cfg80211_registered_device *rdev,struct net_device *dev,bool nowext); -void __cfg80211_ibss_joined (struct net_device *dev,const u8 *bssid, -struct ieee80211_channel *channel); +void __cfg80211_ibss_joined (struct net_device *dev,struct cfg80211_bss *bss);int cfg80211_ibss_wext_join (struct cfg80211_registered_device *rdev,struct wireless_dev *wdev);
net/wireless/ibss.c +21 -17
@@ -16,26 +16,18 @@#include "rdev-ops.h" -void __cfg80211_ibss_joined (struct net_device *dev,const u8 *bssid, -struct ieee80211_channel *channel) +void __cfg80211_ibss_joined (struct net_device *dev,struct cfg80211_bss *bss) {struct wireless_dev *wdev = dev->ieee80211_ptr; -struct cfg80211_bss *bss;#ifdef CONFIG_CFG80211_WEXT union iwreq_data wrqu;#endif if (WARN_ON (wdev->iftype !=NL80211_IFTYPE_ADHOC )) -return ; + goto put_bss;if (!wdev->u.ibss.ssid_len) -return ; - - bss =cfg80211_get_bss (wdev->wiphy, channel, bssid,NULL ,0 , -IEEE80211_BSS_TYPE_IBSS ,IEEE80211_PRIVACY_ANY ); - -if (WARN_ON (!bss)) -return ; + goto put_bss;if (wdev->u.ibss.current_bss) {cfg80211_unhold_bss (wdev->u.ibss.current_bss);
@@ -43,17 +35,22 @@ void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid, }cfg80211_hold_bss (bss_from_pub (bss)); +/* the reference from the event is transferred to current_bss */ wdev->u.ibss.current_bss =bss_from_pub (bss);cfg80211_upload_connect_keys (wdev); -nl80211_send_ibss_bssid (wiphy_to_rdev (wdev->wiphy), dev, bssid, +nl80211_send_ibss_bssid (wiphy_to_rdev (wdev->wiphy), dev, bss->bssid,GFP_KERNEL );#ifdef CONFIG_CFG80211_WEXT memset (&wrqu,0 ,sizeof (wrqu)); -memcpy (wrqu.ap_addr.sa_data, bssid,ETH_ALEN ); +memcpy (wrqu.ap_addr.sa_data, bss->bssid,ETH_ALEN );wireless_send_event (dev,SIOCGIWAP , &wrqu,NULL );#endif +return ; + +put_bss: +cfg80211_put_bss (wdev->wiphy, bss); } void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
@@ -62,6 +59,7 @@ void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,struct wireless_dev *wdev = dev->ieee80211_ptr;struct cfg80211_registered_device *rdev =wiphy_to_rdev (wdev->wiphy);struct cfg80211_event *ev; +struct cfg80211_bss *bss;unsigned long flags;trace_cfg80211_ibss_joined (dev, bssid, channel);
@@ -69,13 +67,19 @@ void cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,if (WARN_ON (!channel))return ; - ev =kzalloc_obj (*ev, gfp); -if (!ev) + bss =cfg80211_get_bss (wdev->wiphy, channel, bssid,NULL ,0 , +IEEE80211_BSS_TYPE_IBSS ,IEEE80211_PRIVACY_ANY ); +if (WARN_ON (!bss))return ; + ev =kzalloc_obj (*ev, gfp); +if (!ev) { +cfg80211_put_bss (wdev->wiphy, bss); +return ; + } + ev->type =EVENT_IBSS_JOINED ; -memcpy (ev->ij.bssid, bssid,ETH_ALEN ); - ev->ij.channel = channel; + ev->ij.bss = bss;spin_lock_irqsave (&wdev->event_lock, flags);list_add_tail (&ev->list, &wdev->event_list);
net/wireless/util.c +1 -2
@@ -1235,8 +1235,7 @@ void cfg80211_process_wdev_events(struct wireless_dev *wdev) !ev->dc.locally_generated);break ;case EVENT_IBSS_JOINED : -__cfg80211_ibss_joined (wdev->netdev, ev->ij.bssid, - ev->ij.channel); +__cfg80211_ibss_joined (wdev->netdev, ev->ij.bss);break ;case EVENT_STOPPED :/*
Signature · 1 line
2.55.0
Johannes Berg
maintainerPATCH 7/8 wifi: cfg80211: fix NAN regulatory enforcement
In reply to Johannes BergFrom: Johannes Berg <[email protected]>
reg_wdev_chan_valid() returns early for any wdev that has no netdev, which is fine for P2P originally (and later PD still), but NAN has no netdev and yet enforcement code was added and is needed, but is dead code right now.
Use wdev_running() instead so that netdev-less wdevs aren't skipped. P2P/PD don't do anything in the later switch, but NAN code can now be reached.
--- net/wireless/reg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/wireless/reg.c +1 -1
@@ -2345,7 +2345,7 @@ static bool reg_wdev_chan_valid(struct wiphy *wiphy, struct wireless_dev *wdev) iftype = wdev->iftype;/* make sure the interface is active */ -if (!wdev->netdev || !netif_running (wdev->netdev)) +if (!wdev_running (wdev))return true;/* NAN doesn't have links, handle it separately */
Signature · 1 line
2.55.0
Johannes Berg
maintainerPATCH 8/8 wifi: cfg80211: reduce RTNL holding in regulatory enforcement
In reply to Johannes BergFrom: Johannes Berg <[email protected]>
Regulatory enforcement in reg_check_chans_work() does all work with the RTNL held, which can block the RTNL for a long time, which syzbot can hit and report hung tasks.
Except for NAN, we don't need the RTNL for the enforcement, and the list iteration can be done with RCU instead.
Split the enforcement off into new work structs: for NAN, we have to have the RTNL to close dependent NAN_DATA interfaces, everything else can use cfg80211_leave_locked() in a wiphy work.
It'd be doable to use just a single work with RTNL, but then the RTNL would end up being used all the time, and really it only needs to be used for NAN.
--- net/wireless/core.c | 3 +++ net/wireless/core.h | 2 ++ net/wireless/reg.c | 50 +++++++++++++++++++++++++++++++++++++-------- net/wireless/reg.h | 16 +++++++++++++++ 4 files changed, 63 insertions(+), 8 deletions(-)
net/wireless/core.c +3 -0
@@ -645,6 +645,8 @@ struct wiphy *wiphy_new_nm(const struct cfg80211_ops *ops, int sizeof_priv,INIT_WORK (&rdev->destroy_work, cfg80211_destroy_iface_wk);wiphy_work_init (&rdev->sched_scan_stop_wk, cfg80211_sched_scan_stop_wk);INIT_WORK (&rdev->sched_scan_res_wk, cfg80211_sched_scan_results_wk); +wiphy_work_init (&rdev->reg_check_chans_wk, reg_leave_invalid_chans_wk); +INIT_WORK (&rdev->reg_leave_nan_wk, reg_leave_invalid_nan_wk);INIT_WORK (&rdev->propagate_radar_detect_wk, cfg80211_propagate_radar_detect_wk);INIT_WORK (&rdev->propagate_cac_done_wk, cfg80211_propagate_cac_done_wk);
@@ -1344,6 +1346,7 @@ void wiphy_unregister(struct wiphy *wiphy)cancel_delayed_work_sync (&rdev->dfs_update_channels_wk);cancel_delayed_work_sync (&rdev->background_cac_done_wk);flush_work (&rdev->destroy_work); +flush_work (&rdev->reg_leave_nan_wk);flush_work (&rdev->propagate_radar_detect_wk);flush_work (&rdev->propagate_cac_done_wk);flush_work (&rdev->mgmt_registrations_update_wk);
net/wireless/core.h +2 -0
@@ -114,6 +114,8 @@ struct cfg80211_registered_device {struct work_struct destroy_work;struct wiphy_work sched_scan_stop_wk;struct work_struct sched_scan_res_wk; +struct wiphy_work reg_check_chans_wk; +struct work_struct reg_leave_nan_wk;struct cfg80211_chan_def radar_chandef;struct work_struct propagate_radar_detect_wk;
net/wireless/reg.c +42 -8
73 lines not on this page · raw message
net/wireless/reg.h +16 -0
@@ -178,6 +178,22 @@ int reg_reload_regdb(void);*/ void reg_check_channels (void ); +/** +* reg_leave_invalid_chans_wk - check if channels are no longer usable and leave +* @wiphy: the wiphy to check +* @work: the work struct +*/ +void reg_leave_invalid_chans_wk (struct wiphy *wiphy,struct wiphy_work *work); + +/** +* reg_leave_invalid_nan_wk - check channels and tear down NAN when unusable +* @work: the work struct +* +* Stopping a NAN interface needs the RTNL, so it cannot be done from +* reg_leave_invalid_chans_wk() which runs with the wiphy mutex held. +*/ +void reg_leave_invalid_nan_wk (struct work_struct *work); +extern const u8 shipped_regdb_certs[];extern unsigned int shipped_regdb_certs_len;extern const u8 extra_regdb_certs[];
Signature · 1 line
2.55.0