Add KUnit tests for a probe response that carries a Multiple BSSID element and is grouped with a hidden-SSID beacon entry, with the beacon received both before and after the probe response: - once all entries are unlinked, only the reference returned to the caller may remain on the beacon entry; - while a reference to the nontransmitted BSS is held, the transmitting BSS and the beacon entry it points to must stay alive, each with exactly one reference. Without the previous patch the first test fails in both orders. An AI coding assistant was used to write these tests. Signed-off-by: MariusVanDerWijden --- net/wireless/tests/scan.c | 132 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/net/wireless/tests/scan.c b/net/wireless/tests/scan.c index 8c20278b5d3a..7ae599f17390 100644 --- a/net/wireless/tests/scan.c +++ b/net/wireless/tests/scan.c @@ -520,6 +520,135 @@ static void test_get_bss_miss_reason(struct kunit *test) cfg80211_put_bss(wiphy, bss); } +static const u8 mbssid_hidden_beacon_ies[] = { + WLAN_EID_SSID, 0, +}; + +static const u8 mbssid_hidden_presp_ies[] = { + WLAN_EID_SSID, 4, 'T', 'E', 'S', 'T', + WLAN_EID_MULTIPLE_BSSID, 15, + 2, /* MaxBSSID Indicator */ + 0, 12, /* Nontransmitted BSSID Profile */ + WLAN_EID_NON_TX_BSSID_CAP, 2, 0x01, 0x00, + WLAN_EID_SSID, 3, 'N', 'T', 'X', + WLAN_EID_MULTI_BSSID_IDX, 1, 1, +}; + +static const struct mbssid_hidden_case { + const char *desc; + bool beacon_first; +} mbssid_hidden_cases[] = { + { .desc = "beacon_first", .beacon_first = true, }, + { .desc = "presp_first", .beacon_first = false, }, +}; +KUNIT_ARRAY_PARAM_DESC(mbssid_hidden, mbssid_hidden_cases, desc) + +/* + * Inform a hidden-SSID beacon and a probe response for the same BSSID that + * carries a Multiple BSSID element, in the order given by the test + * parameter, so that the probe response entry is grouped with the beacon + * entry and is the transmitting BSS of one nontransmitted BSS. + */ +static void mbssid_hidden_setup(struct kunit *test, struct wiphy *wiphy, + struct cfg80211_inform_bss *inform_bss, + struct cfg80211_bss **beacon, + struct cfg80211_bss **trans) +{ + const struct mbssid_hidden_case *params = test->param_value; + const u8 bssid[ETH_ALEN] = { 0x10, 0x22, 0x33, 0x44, 0x55, 0x60 }; + int i; + + wiphy->support_mbssid = true; + + inform_bss->chan = ieee80211_get_channel_khz(wiphy, MHZ_TO_KHZ(2412)); + KUNIT_ASSERT_NOT_NULL(test, inform_bss->chan); + + for (i = 0; i < 2; i++) { + if (i == 0 ? params->beacon_first : !params->beacon_first) { + *beacon = cfg80211_inform_bss_data(wiphy, inform_bss, + CFG80211_BSS_FTYPE_BEACON, + bssid, 0, 0x1234, 100, + mbssid_hidden_beacon_ies, + sizeof(mbssid_hidden_beacon_ies), + GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, *beacon); + } else { + *trans = cfg80211_inform_bss_data(wiphy, inform_bss, + CFG80211_BSS_FTYPE_PRESP, + bssid, 0, 0x1234, 100, + mbssid_hidden_presp_ies, + sizeof(mbssid_hidden_presp_ies), + GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, *trans); + } + } + + KUNIT_ASSERT_PTR_EQ(test, (*trans)->hidden_beacon_bss, *beacon); + KUNIT_ASSERT_FALSE(test, list_empty(&(*trans)->nontrans_list)); +} + +static void test_inform_bss_mbssid_hidden(struct kunit *test) +{ + struct inform_bss ctx = { + .test = test, + }; + struct wiphy *wiphy = T_WIPHY(test, ctx); + struct cfg80211_inform_bss inform_bss = { + .signal = 50, + .drv_data = &ctx, + }; + struct cfg80211_bss *beacon, *trans; + + mbssid_hidden_setup(test, wiphy, &inform_bss, &beacon, &trans); + + /* drop the transmitting BSS together with its nontransmitted BSS */ + cfg80211_unlink_bss(wiphy, trans); + cfg80211_put_bss(wiphy, trans); + cfg80211_unlink_bss(wiphy, beacon); + + /* only the reference returned to us is left */ + KUNIT_EXPECT_EQ(test, bss_from_pub(beacon)->refcount, 1); + cfg80211_put_bss(wiphy, beacon); +} + +static void test_inform_bss_mbssid_hidden_held_nontx(struct kunit *test) +{ + struct inform_bss ctx = { + .test = test, + }; + struct wiphy *wiphy = T_WIPHY(test, ctx); + struct cfg80211_inform_bss inform_bss = { + .signal = 50, + .drv_data = &ctx, + }; + struct cfg80211_bss *beacon, *trans, *nontx; + + mbssid_hidden_setup(test, wiphy, &inform_bss, &beacon, &trans); + + nontx = list_first_entry(&trans->nontrans_list, struct cfg80211_bss, + nontrans_list); + cfg80211_ref_bss(wiphy, nontx); + + /* drop everything except our reference to the nontransmitted BSS */ + cfg80211_unlink_bss(wiphy, trans); + cfg80211_put_bss(wiphy, trans); + cfg80211_unlink_bss(wiphy, beacon); + cfg80211_put_bss(wiphy, beacon); + + /* + * The nontransmitted BSS keeps the transmitting BSS alive, and that + * keeps the beacon entry it points to alive, each by exactly one + * reference. + */ + KUNIT_ASSERT_PTR_EQ(test, nontx->transmitted_bss, trans); + KUNIT_ASSERT_PTR_EQ(test, trans->hidden_beacon_bss, beacon); + KUNIT_EXPECT_EQ(test, bss_from_pub(nontx)->refcount, 1); + KUNIT_EXPECT_EQ(test, bss_from_pub(trans)->refcount, 1); + KUNIT_EXPECT_EQ(test, bss_from_pub(beacon)->refcount, 1); + + cfg80211_put_bss(wiphy, nontx); +} + static struct inform_bss_ml_sta_case { const char *desc; int mld_id; @@ -975,6 +1104,9 @@ static struct kunit_case inform_bss_test_cases[] = { KUNIT_CASE(test_inform_bss_ssid_only), KUNIT_CASE(test_get_bss_miss_reason), KUNIT_CASE_PARAM(test_inform_bss_ml_sta, inform_bss_ml_sta_gen_params), + KUNIT_CASE_PARAM(test_inform_bss_mbssid_hidden, mbssid_hidden_gen_params), + KUNIT_CASE_PARAM(test_inform_bss_mbssid_hidden_held_nontx, + mbssid_hidden_gen_params), {} }; -- 2.56.0 On 11/25/26 1:23 AM, m.vanderwijden@live.de wrote: > 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); -- Marius van der Wijden https://github.com/MariusVanDerWijden