1: 54af452f1f75 ! 1: c940e7d13b6b wifi: mac80211: do not copy the mesh RSN IE twice into the beacon @@ Metadata ## Commit message ## wifi: mac80211: do not copy the mesh RSN IE twice into the beacon  - ieee80211_mesh_build_beacon() budgets the user's extra IE blob once: + mesh_add_rsn_ie() searches the whole of the user's mesh IE blob for an RSN + element, and mesh_add_vendor_ies() copies everything from the first vendor + element to the end of that blob verbatim:  - tail_len = ... + - ifmsh->ie_len; - - bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL); - - but two of the builders that fill the tail draw from that blob. - mesh_add_vendor_ies() emits everything from the first vendor IE to the - end of it: + data = cfg80211_find_ie(WLAN_EID_RSN, ifmsh->ie, ifmsh->ie_len); + ... + skb_put_data(skb, data, len);  offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0); + ... + skb_put_data(skb, ifmsh->ie + offset, len);  - if (offset < ifmsh->ie_len) { - len = ifmsh->ie_len - offset; - data = ifmsh->ie + offset; - ... - skb_put_data(skb, data, len); - - while mesh_add_rsn_ie() searches the whole blob: - - data = cfg80211_find_ie(WLAN_EID_RSN, ifmsh->ie, ifmsh->ie_len); + So an RSN element placed BEHIND a vendor element is copied by both, while + ieee80211_mesh_build_beacon() reserved room for one copy:  - A blob that begins with a vendor IE therefore has its RSN IE emitted - twice - once inside the verbatim vendor range, once on its own - and the - tail copy runs past the allocation: - - BUG: KASAN: slab-out-of-bounds in ieee80211_mesh_build_beacon+0xe3f - Write of size 479 at addr ffff88805ca1887b by task syz-executor - __asan_memcpy - ieee80211_mesh_build_beacon net/mac80211/mesh.c:1110 - ieee80211_start_mesh net/mac80211/mesh.c:1187 - ieee80211_join_mesh net/mac80211/cfg.c:3330 - rdev_join_mesh - __cfg80211_join_mesh net/wireless/mesh.c:214 - nl80211_join_mesh net/wireless/nl80211.c:15399 - - One NL80211_CMD_JOIN_MESH carrying such an NL80211_ATTR_IE is enough. - - Confine the RSN search to the non-vendor part of the blob, so the two - builders cover disjoint ranges and their total cannot exceed the single - ifmsh->ie_len the beacon was sized with. The blob reaches us having passed - validate_ie_attr(), which walks the element chain and requires it to end - exactly at the end of the attribute, so the split returns an element - boundary that is not past ifmsh->ie_len. - - An RSN IE placed behind a vendor IE is still emitted, as part of that - verbatim vendor range; nothing that a well-formed configuration sends is - dropped. - - How this was found: by AI-driven fuzzing built on top of syzkaller. Its - analysis was checked by hand against the code, the fault was reproduced on - net.git main under KASAN, and the patched kernel is clean on the same - reproducer with ordinary secure-mesh beacons unaffected. + tail_len = ... + ifmsh->ie_len; + bcn = kzalloc(sizeof(*bcn) + head_len + tail_len, GFP_KERNEL); + ... + bcn->tail_len = skb->len; + memcpy(bcn->tail, skb->data, bcn->tail_len); + + The builder skb is allocated separately and rounds up to a kmalloc bucket, + so the skb_tailroom() checks pass; bcn->tail_len is then taken from what + was actually assembled, and the memcpy writes the excess past the end of + the kzalloc()ed block. KASAN reports a slab-out-of-bounds write in + ieee80211_mesh_build_beacon(). + + The blob is NL80211_MESH_SETUP_IE, nested inside NL80211_ATTR_MESH_SETUP, + which nl80211_parse_mesh_setup() stores and copy_mesh_setup() kmemdup()s. + One NL80211_CMD_JOIN_MESH carrying a vendor element followed by an RSN + element is enough. + + Search only the part of the blob that comes before the first vendor + element, so the two emitters cover disjoint ranges and their combined + output fits the budget. An RSN element behind a vendor element is still + emitted, as part of that verbatim vendor range. + + The bound is clamped to ifmsh->ie_len, which matters and is not belt and + braces: ieee80211_ie_split_vendor() advances one whole element at a time, + + while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC) + pos += 2 + ies[pos + 1]; + + so it returns a position past ielen whenever ielen ends inside an element. + That is reachable, because ifmsh->ie_len is a u8 in struct + ieee80211_if_mesh while NL80211_MESH_SETUP_IE is policy-capped at + IEEE80211_MAX_DATA_LEN, and copy_mesh_setup() assigns the size_t length to + it: + + ifmsh->ie_len = setup->ie_len; + + A 257-byte blob holding a 255-byte RSN element then leaves ifmsh->ie_len + at 1, and an unclamped search bound of 255 would copy 255 bytes into a + one-byte budget - the same overrun by another route. With the clamp the + bound is never wider than the one this search had before it was narrowed, + and cfg80211_find_ie() returns only an element lying wholly inside its + bound (for_each_element() requires 2 + datalen to fit), so the copy fits. + The truncating assignment itself is a separate pre-existing problem. + + This also changes Mesh Peering Open/Confirm frames, which + mesh_plink_frame_tx() builds with the same two helpers - mesh_add_rsn_ie() + after the extended supported rates, mesh_add_vendor_ies() at the end. An + RSN element behind a vendor element used to appear twice there as well, + once in order and once in the trailing vendor block; now it appears once, + in the trailing block, after the Mesh ID, Mesh Config, MPM, HT, VHT, HE + and EHT elements. Element order in a management frame is not something a + receiver may rely on - 802.11 parsers walk elements by id - and the + alternative is emitting the element twice, which is the bug. A + configuration that puts its RSN element before its vendor elements, which + is what the ordering in these frames was built around, is unaffected. + + Found by AI-driven fuzzing built on top of syzkaller, and reproduced on + mainline 7.4-rc3 and 7.3.1 with a 72-line program: one interface in mesh + point mode, one JOIN_MESH whose NL80211_MESH_SETUP_IE holds a vendor + element followed by an RSN element.  Fixes: 2b5e19677592 ("mac80211: cache mesh beacon") Cc: stable@vger.kernel.org @@ Commit message Signed-off-by: Piyus Kedia   ## net/mac80211/mesh.c ## -@@ net/mac80211/mesh.c: int mesh_add_rsn_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb) +@@ net/mac80211/mesh.c: int mesh_add_vendor_ies(struct ieee80211_sub_if_data *sdata, + int mesh_add_rsn_ie(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb) + { struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; ++ size_t prefix; u8 len = 0; const u8 *data; -+ size_t prefix;  if (!ifmsh->ie || !ifmsh->ie_len) return 0;  -- /* find RSN IE */ -- data = cfg80211_find_ie(WLAN_EID_RSN, ifmsh->ie, ifmsh->ie_len); -+ /* Find the RSN IE in the non-vendor part of the blob only. Everything -+ * from the first vendor IE onwards is emitted verbatim by -+ * mesh_add_vendor_ies(), and ieee80211_mesh_build_beacon() budgets -+ * ifmsh->ie_len once for both of them, so an RSN IE sitting behind a -+ * vendor IE would be copied twice and overrun the beacon's tail. ++ /* Only the part before the first vendor element: mesh_add_vendor_ies() ++ * emits everything from there to the end verbatim, so searching the ++ * whole blob here makes an RSN element behind a vendor element be ++ * copied by both, and ieee80211_mesh_build_beacon() reserved room for ++ * one copy. ++ * ++ * CLAMPED, because ieee80211_ie_split_vendor() walks one whole element ++ * at a time and returns a value past ifmsh->ie_len when the length ++ * ends inside an element - which ifmsh->ie_len being a u8 truncated ++ * from the size_t the netlink attribute carries makes reachable. With ++ * the clamp the bound is never wider than it was before this search ++ * was narrowed, and cfg80211_find_ie() only returns an element lying ++ * wholly inside its bound, so the copy below still fits the budget.  + */  + prefix = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0); ++ prefix = min_t(size_t, prefix, ifmsh->ie_len); ++ + /* find RSN IE */ +- data = cfg80211_find_ie(WLAN_EID_RSN, ifmsh->ie, ifmsh->ie_len);  + data = cfg80211_find_ie(WLAN_EID_RSN, ifmsh->ie, prefix); if (!data) return 0; 2: 56d9ec3c436b < -: ------------ wifi: mac80211: widen the mesh IE offsets a u8 cannot hold