BSS entries point to other entries through hidden_beacon_bss (a probe response entry to the hidden-SSID beacon entry it is grouped with) and transmitted_bss (a nontransmitted BSS to its transmitting BSS). Each get or put on an entry is also applied to the entries it points to, one level deep, so that those stay alive while it is in use. With multi-BSSID these links can be chained: a nontransmitted BSS points to its transmitting BSS, which can itself be a probe response entry grouped with a hidden-SSID beacon entry. One level of propagation doesn't cover that, and the two places that create such a chain count it inconsistently: - __cfg80211_bss_update() takes the reference for a new nontransmitted BSS with bss_ref_get() on the transmitting BSS, which also increments the beacon entry, but putting the new entry only decrements the transmitting BSS. - cfg80211_combine_bsses() adds the whole refcount of a probe response entry to the beacon entry it is grouped with. If the probe response entry already has nontransmitted BSSes, that count includes the references they propagated to it, which they later drop only on the probe response entry. Either way the beacon entry keeps a reference that is never dropped, and once it expires it is never freed. A station scanning near an AP that hides its SSID in beacons but sends a Multiple BSSID element in probe responses leaks such an entry each time it expires; spoofed frames can repeat this. Taking a single reference in the first case would stop that leak, but then the beacon entry can be freed while the transmitting BSS, kept alive by a nontransmitted BSS that is still in use, points to it and shares its beacon IEs. Instead, give each entry exactly one reference on each entry it points to, and drop it only when the pointing entry is freed. Getting or putting an entry then no longer touches other entries, chains of any length are covered, and an entry can't be freed while another entry still points to it. Freeing an entry can now free the entries it points to in turn, so do that iteratively rather than recursively. The leak was found with a userspace fuzz harness around cfg80211_inform_bss_*() whose oracle checks BSS refcounts against the table's links. An AI coding assistant was used to analyse the finding and to draft this patch and changelog. Fixes: a3584f56de1c ("cfg80211: Properly track transmitting and non-transmitting BSS") Cc: stable@vger.kernel.org Signed-off-by: MariusVanDerWijden --- Notes (not part of the commit message): - Testing: KUnit on ARCH=um (all cfg80211 suites), with the tests from patch 2/2, on master 8d7fe9885c62: leak test leak test held nontx beacon first presp first BSS test master refcount 2 refcount 2 pass this patch pass pass pass With this patch all 61 cfg80211 KUnit tests pass, with and without CONFIG_KASAN=y, and KASAN reports nothing. Not tested on hardware or with mac80211_hwsim. - Since get and put no longer follow links, entries that pointed to each other in a cycle would simply never be freed rather than cause a loop. I haven't checked whether received frames can create such a cycle. net/wireless/scan.c | 76 ++++++++++++++++++++++++--------------------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/net/wireless/scan.c b/net/wireless/scan.c index 1c50a16c0e9b..0a978f5d4a10 100644 --- a/net/wireless/scan.c +++ b/net/wireless/scan.c @@ -44,13 +44,13 @@ * beacon's information. * * Reference counting is done for all these references except for - * the hidden_list, so that a beacon BSS struct that is otherwise - * not referenced has one reference for being on the bss_list and - * one for each probe response entry that points to it using the - * hidden_beacon_bss pointer. When a BSS struct that has such a - * pointer is get/put, the refcount update is also propagated to - * the referenced struct, this ensure that it cannot get removed - * while somebody is using the probe response version. + * the hidden_list, so that a BSS struct that is otherwise not + * referenced has one reference for being on the bss_list and one + * for each entry that points to it using the hidden_beacon_bss or + * transmitted_bss pointer. Those references are only dropped when + * the pointing entry is freed, which ensures that a referenced + * struct cannot get removed while any entry pointing to it, e.g. + * the probe response version, still exists. * * Note that the hidden_beacon_bss pointer never changes, due to * the reference counting. Therefore, no locking is needed for @@ -107,40 +107,47 @@ static inline void bss_ref_get(struct cfg80211_registered_device *rdev, lockdep_assert_held(&rdev->bss_lock); bss->refcount++; - - if (bss->pub.hidden_beacon_bss) - bss_from_pub(bss->pub.hidden_beacon_bss)->refcount++; - - if (bss->pub.transmitted_bss) - bss_from_pub(bss->pub.transmitted_bss)->refcount++; } -static inline void bss_ref_put(struct cfg80211_registered_device *rdev, - struct cfg80211_internal_bss *bss) +static void bss_put_link(struct cfg80211_bss *pub, struct list_head *free_list) { - lockdep_assert_held(&rdev->bss_lock); + struct cfg80211_internal_bss *bss; - if (bss->pub.hidden_beacon_bss) { - struct cfg80211_internal_bss *hbss; + if (!pub) + return; - hbss = bss_from_pub(bss->pub.hidden_beacon_bss); - hbss->refcount--; - if (hbss->refcount == 0) - bss_free(hbss); - } + bss = bss_from_pub(pub); + if (--bss->refcount == 0) + list_add_tail(&bss->list, free_list); +} - if (bss->pub.transmitted_bss) { - struct cfg80211_internal_bss *tbss; +static void bss_ref_put(struct cfg80211_registered_device *rdev, + struct cfg80211_internal_bss *bss) +{ + LIST_HEAD(free_list); - tbss = bss_from_pub(bss->pub.transmitted_bss); - tbss->refcount--; - if (tbss->refcount == 0) - bss_free(tbss); - } + lockdep_assert_held(&rdev->bss_lock); + + if (--bss->refcount) + return; - bss->refcount--; - if (bss->refcount == 0) + /* + * Freeing an entry drops the references it holds on the entries + * it points to, which may free those in turn. Use the list member + * to do this without recursion: an entry whose last reference is + * gone is no longer on the bss_list, other than when the whole + * list is torn down in cfg80211_dev_free(), and an entry still on + * the bss_list always holds its own reference for being there. + */ + list_add_tail(&bss->list, &free_list); + while (!list_empty(&free_list)) { + bss = list_first_entry(&free_list, struct cfg80211_internal_bss, + list); + list_del(&bss->list); + bss_put_link(bss->pub.hidden_beacon_bss, &free_list); + bss_put_link(bss->pub.transmitted_bss, &free_list); bss_free(bss); + } } static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev, @@ -1855,7 +1862,7 @@ static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev, /* combine them */ list_add(&bss->hidden_list, &new->hidden_list); bss->pub.hidden_beacon_bss = &new->pub; - new->refcount += bss->refcount; + new->refcount++; rcu_assign_pointer(bss->pub.beacon_ies, new->pub.beacon_ies); } @@ -2118,10 +2125,9 @@ __cfg80211_bss_update(struct cfg80211_registered_device *rdev, return NULL; } - /* This must be before the call to bss_ref_get */ if (tmp->pub.transmitted_bss) { new->pub.transmitted_bss = tmp->pub.transmitted_bss; - bss_ref_get(rdev, bss_from_pub(tmp->pub.transmitted_bss)); + bss_from_pub(tmp->pub.transmitted_bss)->refcount++; } cfg80211_insert_bss(rdev, new); -- 2.56.0