staging: rtl8723bs: fix multiple security vulnerabilities
Subsystems (2)
v6 cover staging: rtl8723bs: fix multiple security vulnerabilities
This series fixes five remotely-triggerable memory safety issues in the rtl8723bs driver. All of them are reachable from the air by an attacker within WiFi radio range, without authentication, via crafted management or data frames:
1. Heap buffer overflow in recvframe_defrag() when reassembling fragmented frames whose total payload exceeds the receive buffer capacity.
2. Integer underflow in TKIP MIC verification when a frame is shorter than the sum of header, IV, ICV and MIC sizes.
3. Out-of-bounds read in portctrl() when a non-EAPOL frame is shorter than the 802.11 header + IV + LLC + ether_type.
4. Out-of-bounds reads in three IE walkers (rtw_get_wapi_ie(), rtw_get_sec_ie(), rtw_get_wps_ie()) due to missing validation of the TLV length byte and of the byte ranges touched by the subsequent memcmp() calls.
5. Integer underflow in rtw_wep_decrypt() when a WEP frame is shorter than the header + IV + ICV.
Each patch was found by code review and is not tested on hardware.
Changes since v5:
- Patch 1/5: restore the "/* memcpy */" comment that v5 had removed as drive-by cleanup (Dan Carpenter).
- Patch 3/5: drop the unrelated cleanups (ptr = ptr + X -> ptr += X, inversion of the ether_type == eapol_type branch into direct return NULL); the patch now only adds the short-frame length check before dereferencing the LLC header (Dan Carpenter).
- Patches 2/5, 4/5 and 5/5 are unchanged.
Changes since v4:
- Patch 1/5: collapse the identical cleanup sites in recvframe_defrag() into a single out_err label (Dan Carpenter).
- Patch 4/5: in addition to the outer TLV length check, add an inner bound check before each memcmp() so that the OUI read at offset 6 (WAPI) or offset 2 (WPA/WPS) stays inside the declared element (Dan Carpenter).
- Patch 5/5: tighten the length check to also cover the 4-byte ICV, so that the subsequent crc32_le(payload, length - 4) call cannot underflow length - 4.
Changes since v3:
- All patches: add Fixes: tag pointing at the driver import and add Cc: stable per Dan Carpenter.
Changes since v2:
- Sent as numbered series with cover letter.
Changes since v1:
- Rebased on staging-next.
Delene Tchio Romuald (5):
staging: rtl8723bs: fix heap buffer overflow in recvframe_defrag()
staging: rtl8723bs: fix integer underflow in TKIP MIC verification
staging: rtl8723bs: fix out-of-bounds read in portctrl()
staging: rtl8723bs: fix out-of-bounds reads in IE parsing functions
staging: rtl8723bs: fix negative length in WEP decryption
.../staging/rtl8723bs/core/rtw_ieee80211.c | 70 +++++++++++++------ drivers/staging/rtl8723bs/core/rtw_recv.c | 51 +++++++++----- drivers/staging/rtl8723bs/core/rtw_security.c | 6 ++ 3 files changed, 87 insertions(+), 40 deletions(-)
base-commit: bf9c95f3eeefb7fc4b4a6380cc23f1dca744e379 --
2.43.0
PATCH v6 1/5 staging: rtl8723bs: fix heap buffer overflow in recvframe_defrag()
In recvframe_defrag(), a memcpy() copies fragment data into the reassembly buffer before validating that the buffer has sufficient space. If the total reassembled payload exceeds the receive buffer capacity, this results in a heap buffer overflow.
Additionally, the return values of recvframe_pull() and recvframe_pull_tail() were ignored. On failure those helpers revert their pointer updates and return NULL; continuing past such a failure would leave pfhdr->rx_tail at its pre-strip value, so the subsequent bounds check against rx_end - rx_tail would operate on stale pointers.
An attacker within WiFi radio range can exploit this by sending crafted 802.11 fragmented frames. No authentication is required.
Check the return values of recvframe_pull() and recvframe_pull_tail(), then verify that the fragment payload fits within the remaining buffer space before the memcpy(). Consolidate the five cleanup paths through a single out_err label.
Found by reviewing memory operations in the driver and tracing buffer pointer manipulation through rtw_recv.h inline helpers. Not tested on hardware.
---
v6: restore the '/* memcpy */' comment that v5 had removed
as drive-by cleanup (Dan Carpenter).
v5: collapse the identical cleanup sites into a single
out_err label (Dan Carpenter).
v4: check return values of recvframe_pull() and
recvframe_pull_tail(); drop unnecessary (uint) cast; add Fixes: tag and Cc: stable (Dan Carpenter).
v3: rebased on staging-next; sent as numbered series with
proper Cc from get_maintainer.pl.
v2: rebased on staging-next (v1 was based on v7.0-rc6 and
did not apply).
drivers/staging/rtl8723bs/core/rtw_recv.c | 36 ++++++++++++----------- 1 file changed, 19 insertions(+), 17 deletions(-)
drivers/staging/rtl8723bs/core/rtw_recv.c +19 -17
@@ -1090,14 +1090,9 @@ static union recv_frame *recvframe_defrag(struct adapter *adapter, pfhdr = &prframe->u.hdr;list_del_init (&(prframe->u.list)); -if (curfragnum != pfhdr->attrib.frag_num) { -/* the first fragment number must be 0 */ -/* free the whole queue */ -rtw_free_recvframe (prframe, pfree_recv_queue); -rtw_free_recvframe_queue (defrag_q, pfree_recv_queue); - -return NULL ; - } +/* the first fragment number must be 0 */ +if (curfragnum != pfhdr->attrib.frag_num) + goto out_err; curfragnum++;
@@ -1112,13 +1107,9 @@ static union recv_frame *recvframe_defrag(struct adapter *adapter,/* check the fragment sequence (2nd ~n fragment frame) */ -if (curfragnum != pnfhdr->attrib.frag_num) { -/* the fragment number must be increasing (after decache) */ -/* release the defrag_q & prframe */ -rtw_free_recvframe (prframe, pfree_recv_queue); -rtw_free_recvframe_queue (defrag_q, pfree_recv_queue); -return NULL ; - } +/* the fragment number must be increasing (after decache) */ +if (curfragnum != pnfhdr->attrib.frag_num) + goto out_err; curfragnum++;
@@ -1127,10 +1118,16 @@ static union recv_frame *recvframe_defrag(struct adapter *adapter, wlanhdr_offset = pnfhdr->attrib.hdrlen + pnfhdr->attrib.iv_len; -recvframe_pull (pnextrframe, wlanhdr_offset); +if (!recvframe_pull (pnextrframe, wlanhdr_offset)) + goto out_err;/* append to first fragment frame's tail (if privacy frame, pull the ICV) */ -recvframe_pull_tail (prframe, pfhdr->attrib.icv_len); +if (!recvframe_pull_tail (prframe, pfhdr->attrib.icv_len)) + goto out_err; + +/* Verify the receiving buffer has enough space for the fragment */ +if (pnfhdr->len > pfhdr->rx_end - pfhdr->rx_tail) + goto out_err;/* memcpy */ memcpy (pfhdr->rx_tail, pnfhdr->rx_data, pnfhdr->len);
@@ -1146,6 +1143,11 @@ static union recv_frame *recvframe_defrag(struct adapter *adapter,rtw_free_recvframe_queue (defrag_q, pfree_recv_queue);return prframe; + +out_err: +rtw_free_recvframe (prframe, pfree_recv_queue); +rtw_free_recvframe_queue (defrag_q, pfree_recv_queue); +return NULL ; }/* check if need to defrag, if needed queue the frame to defrag_q */
Signature · 1 line
2.43.0
PATCH v6 2/5 staging: rtl8723bs: fix integer underflow in TKIP MIC verification
In reply to Delene Tchio RomualdIn recvframe_chkmic(), the payload length is computed as:
datalen = precvframe->u.hdr.len - prxattrib->hdrlen
- prxattrib->iv_len - prxattrib->icv_len - 8;
All operands are unsigned. If the receive frame is shorter than the sum of the header, IV, ICV and MIC sizes, this subtraction wraps around and datalen becomes a huge unsigned value. That value is then passed to rtw_secmicappend(), which reads past the end of the receive buffer and can leak kernel memory or trigger a crash.
An attacker within WiFi radio range can exploit this by sending a crafted short TKIP-encrypted frame. No authentication is required.
Validate that the frame is large enough for the TKIP MIC computation before the subtraction.
Found by reviewing length arithmetic in the TKIP receive path. Not tested on hardware.
---
v6: unchanged.
v5: unchanged.
v4: add Fixes: tag and Cc: stable (Dan Carpenter); carry
Luka Gejak's Reviewed-by.
v3: rebased on staging-next; sent as numbered series with
proper Cc from get_maintainer.pl.
v2: rebased on staging-next (v1 was based on v7.0-rc6 and
did not apply).
drivers/staging/rtl8723bs/core/rtw_recv.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/staging/rtl8723bs/core/rtw_recv.c +7 -0
@@ -390,6 +390,13 @@ static signed int recvframe_chkmic(struct adapter *adapter, union recv_frame *p mickey = &stainfo->dot11tkiprxmickey.skey[0 ]; } +/* Ensure the frame is large enough for TKIP MIC verification */ +if (precvframe->u.hdr.len <= prxattrib->hdrlen + + prxattrib->iv_len + prxattrib->icv_len +8 ) { + res = _FAIL; + goto exit; + } + datalen = precvframe->u.hdr.len - prxattrib->hdrlen - prxattrib->iv_len - prxattrib->icv_len -8 ;/* icv_len included the mic code */ pframe = precvframe->u.hdr.rx_data; payload = pframe + prxattrib->hdrlen + prxattrib->iv_len;
Signature · 1 line
2.43.0
PATCH v6 3/5 staging: rtl8723bs: fix out-of-bounds read in portctrl()
In reply to Delene Tchio RomualdIn portctrl(), when 802.1X port control is enabled and a non-EAPOL frame is received, the ether_type is read from the LLC header without verifying that the frame actually contains enough bytes to hold the MAC header, IV and the LLC header plus two bytes of ether_type. For sufficiently short frames, the memcpy() that loads be_tmp reads past the end of the receive buffer.
An attacker within WiFi radio range can exploit this by sending a crafted short frame. No authentication is required.
Validate the frame length before dereferencing the LLC header; drop the frame if it is too short.
Found by reviewing length validation in the receive path. Not tested on hardware.
---
v6: drop the unrelated cleanups (ptr = ptr + X -> ptr += X,
ether_type inversion into direct return NULL); the patch now only adds the short-frame length check before dereferencing the LLC header (Dan Carpenter).
v5: return NULL directly on the short-frame and non-EAPOL
error paths (Dan Carpenter).
v4: add Fixes: tag and Cc: stable (Dan Carpenter).
v3: rebased on staging-next; sent as numbered series with
proper Cc from get_maintainer.pl.
v2: rebased on staging-next (v1 was based on v7.0-rc6 and
did not apply).
drivers/staging/rtl8723bs/core/rtw_recv.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/staging/rtl8723bs/core/rtw_recv.c +8 -0
@@ -539,6 +539,14 @@ static union recv_frame *portctrl(struct adapter *adapter, union recv_frame *pre prtnframe = precv_frame; +/* Ensure frame has LLC header and ether_type */ +if (pfhdr->len < pattrib->hdrlen + + pattrib->iv_len +LLC_HEADER_LENGTH +2 ) { +rtw_free_recvframe (precv_frame, + &adapter->recvpriv.free_recv_queue); +return NULL ; + } +/* get ether_type */ ptr = ptr + pfhdr->attrib.hdrlen + pfhdr->attrib.iv_len +LLC_HEADER_LENGTH ;memcpy (&be_tmp, ptr,2 );
Signature · 1 line
2.43.0
PATCH v6 4/5 staging: rtl8723bs: fix out-of-bounds reads in IE parsing functions
In reply to Delene Tchio Romualdrtw_get_wapi_ie(), rtw_get_sec_ie() and rtw_get_wps_ie() walk a buffer of Information Elements using the TLV length field without first verifying that the length byte itself is inside the buffer, and without verifying that the specific bytes dereferenced by the subsequent memcmp() calls fit inside the declared element.
An attacker within WiFi radio range can exploit this by sending crafted beacon or probe-response frames carrying truncated or oversized IEs. No authentication is required.
Ensure the length byte is inside the buffer (cnt + 1 < in_len), break out of the loop if the declared element length would read past in_len, and before each memcmp() verify that the offsets it touches are inside the buffer: cnt + 10 for the WAPI OUI compared at offset 6, and cnt + 6 for the WPA/WPS OUIs compared at offset 2.
Found by reviewing bounds checks in IE walkers.
Not tested on hardware.
---
v6: unchanged.
v5: add an inner bound check before each memcmp() so that
the OUI read at offset 6 (WAPI) or offset 2 (WPA/WPS) stays inside the declared element (Dan Carpenter).
v4: add Fixes: tag and Cc: stable (Dan Carpenter).
v3: rebased on staging-next; sent as numbered series with
proper Cc from get_maintainer.pl.
v2: rebased on staging-next (v1 was based on v7.0-rc6 and
did not apply).
.../staging/rtl8723bs/core/rtw_ieee80211.c | 70 +++++++++++++------ 1 file changed, 47 insertions(+), 23 deletions(-)
drivers/staging/rtl8723bs/core/rtw_ieee80211.c +47 -23
@@ -582,18 +582,25 @@ int rtw_get_wapi_ie(u8 *in_ie, uint in_len, u8 *wapi_ie, u16 *wapi_len) cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_); -while (cnt < in_len) { +while (cnt +1 < in_len) { authmode = in_ie[cnt]; -if (authmode ==WLAN_EID_BSS_AC_ACCESS_DELAY && - (!memcmp (&in_ie[cnt +6 ], wapi_oui1,4 ) || - !memcmp (&in_ie[cnt +6 ], wapi_oui2,4 ))) { -if (wapi_ie) -memcpy (wapi_ie, &in_ie[cnt], in_ie[cnt +1 ] +2 ); +if (cnt +2 + in_ie[cnt +1 ] > in_len) +break ; + +if (authmode ==WLAN_EID_BSS_AC_ACCESS_DELAY ) { +if (cnt +10 > in_len) +break ; -if (wapi_len) - *wapi_len = in_ie[cnt +1 ] +2 ; +if (!memcmp (&in_ie[cnt +6 ], wapi_oui1,4 ) || + !memcmp (&in_ie[cnt +6 ], wapi_oui2,4 )) { +if (wapi_ie) +memcpy (wapi_ie, &in_ie[cnt], + in_ie[cnt +1 ] +2 ); +if (wapi_len) + *wapi_len = in_ie[cnt +1 ] +2 ; + } } cnt += in_ie[cnt +1 ] +2 ;/* get next */
@@ -615,15 +622,23 @@ void rtw_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len, u8 *wpa_ie cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_); -while (cnt < in_len) { +while (cnt +1 < in_len) { authmode = in_ie[cnt]; -if ((authmode ==WLAN_EID_VENDOR_SPECIFIC ) && - (!memcmp (&in_ie[cnt +2 ], &wpa_oui[0 ],4 ))) { -if (wpa_ie) -memcpy (wpa_ie, &in_ie[cnt], in_ie[cnt +1 ] +2 ); +if (cnt +2 + in_ie[cnt +1 ] > in_len) +break ; + +if (authmode ==WLAN_EID_VENDOR_SPECIFIC ) { +if (cnt +6 > in_len) +break ; + +if (!memcmp (&in_ie[cnt +2 ], &wpa_oui[0 ],4 )) { +if (wpa_ie) +memcpy (wpa_ie, &in_ie[cnt], + in_ie[cnt +1 ] +2 ); - *wpa_len = in_ie[cnt +1 ] +2 ; + *wpa_len = in_ie[cnt +1 ] +2 ; + } }else if (authmode ==WLAN_EID_RSN ) {if (rsn_ie)memcpy (rsn_ie, &in_ie[cnt], in_ie[cnt +1 ] +2 );
@@ -658,21 +673,30 @@ u8 *rtw_get_wps_ie(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen) cnt =0 ; -while (cnt < in_len) { +while (cnt +1 < in_len) { eid = in_ie[cnt]; -if ((eid ==WLAN_EID_VENDOR_SPECIFIC ) && (!memcmp (&in_ie[cnt +2 ], wps_oui,4 ))) { - wpsie_ptr = &in_ie[cnt]; +if (cnt +2 + in_ie[cnt +1 ] > in_len) +break ; -if (wps_ie) -memcpy (wps_ie, &in_ie[cnt], in_ie[cnt +1 ] +2 ); +if (eid ==WLAN_EID_VENDOR_SPECIFIC ) { +if (cnt +6 > in_len) +break ; -if (wps_ielen) - *wps_ielen = in_ie[cnt +1 ] +2 ; +if (!memcmp (&in_ie[cnt +2 ], wps_oui,4 )) { + wpsie_ptr = &in_ie[cnt]; - cnt += in_ie[cnt +1 ] +2 ; +if (wps_ie) +memcpy (wps_ie, &in_ie[cnt], + in_ie[cnt +1 ] +2 ); -break ; +if (wps_ielen) + *wps_ielen = in_ie[cnt +1 ] +2 ; + + cnt += in_ie[cnt +1 ] +2 ; + +break ; + } } cnt += in_ie[cnt +1 ] +2 ;/* goto next */ }
Signature · 1 line
2.43.0
PATCH v6 5/5 staging: rtl8723bs: fix negative length in WEP decryption
In reply to Delene Tchio RomualdIn rtw_wep_decrypt(), the payload length is computed as:
length = frame->len - prxattrib->hdrlen - prxattrib->iv_len;
All operands are unsigned. If the frame is shorter than the sum of the header length, IV length and the 4-byte ICV, this subtraction wraps around or produces a value smaller than 4; the subsequent crc32_le(~0, payload, length - 4) call then wraps length - 4 to a huge value and reads past the end of the receive buffer.
An attacker within WiFi radio range can exploit this by sending a crafted short WEP-encrypted frame. No authentication is required.
Validate that the frame is large enough to contain at least the 4-byte ICV on top of the header and IV before computing length.
Found by reviewing length arithmetic in the WEP decrypt path. Not tested on hardware.
---
v6: unchanged.
v5: tighten the length check to also cover the 4-byte ICV
so that the subsequent crc32_le(payload, length - 4) call cannot underflow length - 4.
v4: add Fixes: tag and Cc: stable (Dan Carpenter).
v3: rebased on staging-next; sent as numbered series with
proper Cc from get_maintainer.pl.
v2: rebased on staging-next (v1 was based on v7.0-rc6 and
did not apply).
drivers/staging/rtl8723bs/core/rtw_security.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/staging/rtl8723bs/core/rtw_security.c +6 -0
@@ -113,6 +113,12 @@ void rtw_wep_decrypt(struct adapter *padapter, u8 *precvframe)memcpy (&wepkey[0 ], iv,3 );/* memcpy(&wepkey[3], &psecuritypriv->dot11DefKey[psecuritypriv->dot11PrivacyKeyIndex].skey[0], keylength); */ memcpy (&wepkey[3 ], &psecuritypriv->dot11DefKey[keyindex].skey[0 ], keylength); + +/* Ensure the frame is long enough for WEP payload and ICV */ +if (((union recv_frame *)precvframe)->u.hdr.len < + prxattrib->hdrlen + prxattrib->iv_len +4 ) +return ; + length = ((union recv_frame *)precvframe)->u.hdr.len - prxattrib->hdrlen - prxattrib->iv_len; payload = pframe + prxattrib->iv_len + prxattrib->hdrlen;
Signature · 1 line
2.43.0
Quoted 5 lines · Delene Tchio Romuald
This series fixes five remotely-triggerable memory safety issues in the rtl8723bs driver. All of them are reachable from the air by an attacker within WiFi radio range, without authentication, via crafted management or data frames:
LGTM.
Greg KH
maintainerQuoted 19 lines · Delene Tchio Romuald
This series fixes five remotely-triggerable memory safety issues in the rtl8723bs driver. All of them are reachable from the air by an attacker within WiFi radio range, without authentication, via crafted management or data frames:
1. Heap buffer overflow in
recvframe_defrag()when reassembling fragmented frames whose total payload exceeds the receive buffer capacity.
2. Integer underflow in TKIP MIC verification when a frame is shorter than the sum of header, IV, ICV and MIC sizes.
3. Out-of-bounds read inportctrl()when a non-EAPOL frame is shorter than the 802.11 header + IV + LLC +ether_type.
4. Out-of-bounds reads in three IE walkers (rtw_get_wapi_ie(),rtw_get_sec_ie(),rtw_get_wps_ie()) due to missing validation of the TLV length byte and of the byte ranges touched by the subsequentmemcmp()calls.
5. Integer underflow inrtw_wep_decrypt()when a WEP frame is shorter than the header + IV + ICV.
Quoted 1 line
Each patch was found by code review and is not tested on hardware.
Please address the comments found here:
https://sashiko.dev/#/patchset/[email protected]
and ideally, test it on some real hardware to verify it all still works?
thanks,
greg k-h