mac80211: backport security fixes
[openwrt/openwrt.git] / package / kernel / mac80211 / patches / subsys / 355-wifi-cfg80211-fix-BSS-refcounting-bugs.patch
1 From: Johannes Berg <johannes.berg@intel.com>
2 Date: Fri, 30 Sep 2022 23:44:23 +0200
3 Subject: [PATCH] wifi: cfg80211: fix BSS refcounting bugs
4 MIME-Version: 1.0
5 Content-Type: text/plain; charset=UTF-8
6 Content-Transfer-Encoding: 8bit
7
8 commit 0b7808818cb9df6680f98996b8e9a439fa7bcc2f upstream.
9
10 There are multiple refcounting bugs related to multi-BSSID:
11 - In bss_ref_get(), if the BSS has a hidden_beacon_bss, then
12 the bss pointer is overwritten before checking for the
13 transmitted BSS, which is clearly wrong. Fix this by using
14 the bss_from_pub() macro.
15
16 - In cfg80211_bss_update() we copy the transmitted_bss pointer
17 from tmp into new, but then if we release new, we'll unref
18 it erroneously. We already set the pointer and ref it, but
19 need to NULL it since it was copied from the tmp data.
20
21 - In cfg80211_inform_single_bss_data(), if adding to the non-
22 transmitted list fails, we unlink the BSS and yet still we
23 return it, but this results in returning an entry without
24 a reference. We shouldn't return it anyway if it was broken
25 enough to not get added there.
26
27 This fixes CVE-2022-42720.
28
29 Reported-by: Sönke Huster <shuster@seemoo.tu-darmstadt.de>
30 Tested-by: Sönke Huster <shuster@seemoo.tu-darmstadt.de>
31 Fixes: a3584f56de1c ("cfg80211: Properly track transmitting and non-transmitting BSS")
32 Signed-off-by: Johannes Berg <johannes.berg@intel.com>
33 ---
34
35 --- a/net/wireless/scan.c
36 +++ b/net/wireless/scan.c
37 @@ -143,18 +143,12 @@ static inline void bss_ref_get(struct cf
38 lockdep_assert_held(&rdev->bss_lock);
39
40 bss->refcount++;
41 - if (bss->pub.hidden_beacon_bss) {
42 - bss = container_of(bss->pub.hidden_beacon_bss,
43 - struct cfg80211_internal_bss,
44 - pub);
45 - bss->refcount++;
46 - }
47 - if (bss->pub.transmitted_bss) {
48 - bss = container_of(bss->pub.transmitted_bss,
49 - struct cfg80211_internal_bss,
50 - pub);
51 - bss->refcount++;
52 - }
53 +
54 + if (bss->pub.hidden_beacon_bss)
55 + bss_from_pub(bss->pub.hidden_beacon_bss)->refcount++;
56 +
57 + if (bss->pub.transmitted_bss)
58 + bss_from_pub(bss->pub.transmitted_bss)->refcount++;
59 }
60
61 static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
62 @@ -1743,6 +1737,8 @@ cfg80211_bss_update(struct cfg80211_regi
63 new->refcount = 1;
64 INIT_LIST_HEAD(&new->hidden_list);
65 INIT_LIST_HEAD(&new->pub.nontrans_list);
66 + /* we'll set this later if it was non-NULL */
67 + new->pub.transmitted_bss = NULL;
68
69 if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
70 hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
71 @@ -1983,10 +1979,15 @@ cfg80211_inform_single_bss_data(struct w
72 spin_lock_bh(&rdev->bss_lock);
73 if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
74 &res->pub)) {
75 - if (__cfg80211_unlink_bss(rdev, res))
76 + if (__cfg80211_unlink_bss(rdev, res)) {
77 rdev->bss_generation++;
78 + res = NULL;
79 + }
80 }
81 spin_unlock_bh(&rdev->bss_lock);
82 +
83 + if (!res)
84 + return NULL;
85 }
86
87 trace_cfg80211_return_bss(&res->pub);