staging: rtl8723bs: bound WPS attribute copy in rtw_get_wps_attr_content()
PATCH 1/1 staging: rtl8723bs: bound WPS attribute copy in rtw_get_wps_attr_content()
rtw_get_wps_attr_content() copies attr_len - 4 (the WPS attribute's 2-byte data-length field, up to 0xffff) from a WPS information element into the caller's buffer with no destination-size bound:
memcpy(buf_content, attr_ptr + 4, attr_len - 4);
The information element comes straight from a received beacon / probe
response: collect_bss_info() copies the frame's IEs verbatim into bssid->ies, which reaches the scan queue, so attr_len is attacker controlled. rtw_cfg80211_inform_bss() and two sites in rtw_mlme_ext.c call this for WPS_ATTR_SELECTED_REGISTRAR with a one-byte destination (u8 sr / u8 selected_registrar), because that attribute is a single byte by spec. A frame that declares a longer Selected Registrar attribute therefore overflows the one-byte stack variable during a scan, which happens automatically (NetworkManager/iwd), giving an unauthenticated adjacent attacker a remote stack buffer overflow (at minimum a stack-protector panic).
Commit 1463ca3ec660 ("staging: rtl8723bs: fix OOB reads in rtw_get_sec_ie(), rtw_get_wapi_ie(), and rtw_get_wps_attr()") added a bounds check for the attribute header in rtw_get_wps_attr() but not for the attribute data length, and did not touch rtw_get_wps_attr_content(), so the copy remained both an out-of-bounds read of the attribute data and an out-of-bounds write of the destination.
Reject attributes that claim more data than the IE holds (fixing the out-of-bounds read and the latent memcpy(buf_attr, ...) in rtw_get_wps_attr()), give rtw_get_wps_attr_content() the destination buffer size, and clamp the copy to it.
Found using mwemu (https://github.com/sha0coder/mwemu).
--- drivers/staging/rtl8723bs/core/rtw_ieee80211.c | 17 ++++++++++++++--- drivers/staging/rtl8723bs/core/rtw_mlme_ext.c | 12 ++++++++++-- drivers/staging/rtl8723bs/include/ieee80211.h | 4 +++- .../staging/rtl8723bs/os_dep/ioctl_cfg80211.c | 5 ++++- 4 files changed, 31 insertions(+), 7 deletions(-)
drivers/staging/rtl8723bs/core/rtw_ieee80211.c +14 -3
@@ -741,6 +741,10 @@ u8 *rtw_get_wps_attr(u8 *wps_ie, uint wps_ielen, u16 target_attr_id, u8 *buf_attu16 attr_data_len =get_unaligned_be16 (attr_ptr +2 );u16 attr_len = attr_data_len +4 ; +/* An attribute must not claim more data than the IE holds. */ +if (attr_ptr + attr_len > wps_ie + wps_ielen) +break ; +if (attr_id == target_attr_id) { target_attr_ptr = attr_ptr;
@@ -768,7 +772,9 @@ u8 *rtw_get_wps_attr(u8 *wps_ie, uint wps_ielen, u16 target_attr_id, u8 *buf_att* * Returns: the address of the specific WPS attribute content found, or NULL */ -u8 *rtw_get_wps_attr_content (u8 *wps_ie,uint wps_ielen,u16 target_attr_id,u8 *buf_content,uint *len_content) +u8 *rtw_get_wps_attr_content (u8 *wps_ie,uint wps_ielen,u16 target_attr_id, +u8 *buf_content,uint buf_content_len, +uint *len_content) {u8 *attr_ptr;u32 attr_len;
@@ -779,11 +785,16 @@ u8 *rtw_get_wps_attr_content(u8 *wps_ie, uint wps_ielen, u16 target_attr_id, u8 attr_ptr =rtw_get_wps_attr (wps_ie, wps_ielen, target_attr_id,NULL , &attr_len);if (attr_ptr && attr_len) { +uint content_len = attr_len -4 ; + +if (content_len > buf_content_len) + content_len = buf_content_len; +if (buf_content) -memcpy (buf_content, attr_ptr +4 , attr_len -4 ); +memcpy (buf_content, attr_ptr +4 , content_len);if (len_content) - *len_content = attr_len -4 ; + *len_content = content_len;return attr_ptr +4 ; }
drivers/staging/rtl8723bs/core/rtw_mlme_ext.c +10 -2
@@ -1123,7 +1123,12 @@ unsigned int OnAssocReq(struct adapter *padapter, union recv_frame *precv_frame)if (pmlmepriv->wps_beacon_ie) {u8 selected_registrar =0 ; -rtw_get_wps_attr_content (pmlmepriv->wps_beacon_ie, pmlmepriv->wps_beacon_ie_len,WPS_ATTR_SELECTED_REGISTRAR , &selected_registrar,NULL ); +rtw_get_wps_attr_content (pmlmepriv->wps_beacon_ie, + pmlmepriv->wps_beacon_ie_len, +WPS_ATTR_SELECTED_REGISTRAR , + &selected_registrar, +sizeof (selected_registrar), +NULL );if (!selected_registrar) { status =WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA ;
@@ -2131,7 +2136,10 @@ void issue_beacon(struct adapter *padapter, int timeout_ms)sizeof (struct ieee80211_hdr_3addr ) - _BEACON_IE_OFFSET_, NULL, &wps_ielen);if (wps_ie && wps_ielen >0 ) -rtw_get_wps_attr_content (wps_ie, wps_ielen,WPS_ATTR_SELECTED_REGISTRAR , (u8 *)(&sr),NULL ); +rtw_get_wps_attr_content (wps_ie, wps_ielen, +WPS_ATTR_SELECTED_REGISTRAR , + (u8 *)(&sr),sizeof (sr), +NULL );if (sr !=0 )set_fwstate (pmlmepriv,WIFI_UNDER_WPS );else
drivers/staging/rtl8723bs/include/ieee80211.h +3 -1
@@ -710,7 +710,9 @@ void rtw_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len, u8 *wpa_ieu8 *rtw_get_wps_ie (u8 *in_ie,uint in_len,u8 *wps_ie,uint *wps_ielen);u8 *rtw_get_wps_attr (u8 *wps_ie,uint wps_ielen,u16 target_attr_id,u8 *buf_attr,u32 *len_attr); -u8 *rtw_get_wps_attr_content (u8 *wps_ie,uint wps_ielen,u16 target_attr_id,u8 *buf_content,uint *len_content); +u8 *rtw_get_wps_attr_content (u8 *wps_ie,uint wps_ielen,u16 target_attr_id, +u8 *buf_content,uint buf_content_len, +uint *len_content);/** * for_each_ie - iterate over continuous IEs
drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c +4 -1
@@ -244,7 +244,10 @@ struct cfg80211_bss *rtw_cfg80211_inform_bss(struct adapter *padapter, struct wl wpsie =rtw_get_wps_ie (pnetwork->network.ies + _FIXED_IE_LENGTH_, pnetwork->network.ie_length - _FIXED_IE_LENGTH_,NULL , &wpsielen);if (wpsie && wpsielen >0 ) - psr =rtw_get_wps_attr_content (wpsie, wpsielen,WPS_ATTR_SELECTED_REGISTRAR , (u8 *)(&sr),NULL ); + psr =rtw_get_wps_attr_content (wpsie, wpsielen, +WPS_ATTR_SELECTED_REGISTRAR , + (u8 *)(&sr),sizeof (sr), +NULL );if (sr !=0 ) {/* it means under processing WPS */
Signature · 1 line
2.55.0
Greg Kroah-Hartman
maintainerQuoted 25 lines · Jesus Olmos
rtw_get_wps_attr_content()copiesattr_len- 4 (the WPS attribute's 2-byte data-length field, up to0xffff) from a WPS information element into the caller's buffer with no destination-size bound:memcpy(buf_content, attr_ptr + 4, attr_len - 4);The information element comes straight from a received beacon / probe
response:collect_bss_info()copies the frame's IEs verbatim intobssid->ies, which reaches the scan queue, soattr_lenis attacker controlled.rtw_cfg80211_inform_bss()and two sites inrtw_mlme_ext.ccall this forWPS_ATTR_SELECTED_REGISTRARwith a one-byte destination (u8 sr / u8selected_registrar), because that attribute is a single byte by spec. A frame that declares a longer Selected Registrar attribute therefore overflows the one-byte stack variable during a scan, which happens automatically (NetworkManager/iwd), giving an unauthenticated adjacent attacker a remote stack buffer overflow (at minimum a stack-protector panic).Commit
1463ca3ec660("staging: rtl8723bs: fix OOB reads inrtw_get_sec_ie(),rtw_get_wapi_ie(), andrtw_get_wps_attr()") added a bounds check for the attribute header inrtw_get_wps_attr()but not for the attribute data length, and did not touchrtw_get_wps_attr_content(), so the copy remained both an out-of-bounds read of the attribute data and an out-of-bounds write of the destination.
Quoted 6 lines
Reject attributes that claim more data than the IE holds (fixing the out-of-bounds read and the latent
memcpy(buf_attr, ...)inrtw_get_wps_attr()), givertw_get_wps_attr_content()the destination buffer size, and clamp the copy to it.Found using mwemu (https://github.com/sha0coder/mwemu).
So shouldn't this have an Assisted-by: tag?
And have you tested this on real hardware?
Also, this looks a bit wrong, you have an endless loop in this patch :(
thanks,
greg k-h
PATCH v2 1/1 staging: rtl8723bs: bound WPS attribute copy in rtw_get_wps_attr_content()
In reply to Jesus Olmosrtw_get_wps_attr_content() copies attr_len - 4 (the WPS attribute's 2-byte data-length field, up to 0xffff) from a WPS information element into the caller's buffer with no destination-size bound:
memcpy(buf_content, attr_ptr + 4, attr_len - 4);
The information element comes straight from a received beacon / probe
response: collect_bss_info() copies the frame's IEs verbatim into bssid->ies, which reaches the scan queue, so attr_len is attacker controlled. rtw_cfg80211_inform_bss() and two sites in rtw_mlme_ext.c call this for WPS_ATTR_SELECTED_REGISTRAR with a one-byte destination (u8 sr / u8 selected_registrar), because that attribute is a single byte by spec. A frame that declares a longer Selected Registrar attribute therefore overflows the one-byte stack variable during a scan, which happens automatically (NetworkManager/iwd), giving an unauthenticated adjacent attacker a remote stack buffer overflow (at minimum a stack-protector panic).
Commit 1463ca3ec660 ("staging: rtl8723bs: fix OOB reads in rtw_get_sec_ie(), rtw_get_wapi_ie(), and rtw_get_wps_attr()") added a bounds check for the attribute header in rtw_get_wps_attr() but not for the attribute data length, and did not touch rtw_get_wps_attr_content(), so the copy remained both an out-of-bounds read of the attribute data and an out-of-bounds write of the destination.
Reject attributes that claim more data than the IE holds (fixing the out-of-bounds read and the latent memcpy(buf_attr, ...) in rtw_get_wps_attr()), give rtw_get_wps_attr_content() the destination buffer size, and clamp the copy to it.
Compute the attribute length in an unsigned int rather than u16: a declared data length of 0xfffc made (u16)(attr_data_len + 4) wrap to 0, which slipped past that bounds check and advanced the parser by zero, looping forever.
Found using mwemu (https://github.com/sha0coder/mwemu).
---
v2:
- Widen attr_len to unsigned int so a declared data length near 0xffff cannot wrap; for 0xfffc the old u16 wrap defeated the bounds check and made rtw_get_wps_attr() loop forever. Thanks Greg for spotting it.
- Add Assisted-by: tag for the AI-assisted analysis.
Build-tested as a module (x86_64 defconfig + CONFIG_RTL8723BS=m). Not tested on real hardware (I don't have an RTL8723BS device). The bug and the fix were found and checked by source review plus function-level emulation of rtw_get_wps_attr()/rtw_get_wps_attr_content() under mwemu: with a 0xfffc data length the pre-fix code spins forever (u16 wrap -> attr_ptr += 0) while the fix returns immediately, and the Selected Registrar overflow is clamped to the 1-byte destination.
.../staging/rtl8723bs/core/rtw_ieee80211.c | 19 +++++++++++++++---- drivers/staging/rtl8723bs/core/rtw_mlme_ext.c | 12 ++++++++++-- drivers/staging/rtl8723bs/include/ieee80211.h | 4 +++- .../staging/rtl8723bs/os_dep/ioctl_cfg80211.c | 5 ++++- 4 files changed, 32 insertions(+), 8 deletions(-)
drivers/staging/rtl8723bs/core/rtw_ieee80211.c +15 -4
@@ -739,7 +739,11 @@ u8 *rtw_get_wps_attr(u8 *wps_ie, uint wps_ielen, u16 target_attr_id, u8 *buf_attbreak ;u16 attr_id =get_unaligned_be16 (attr_ptr);u16 attr_data_len =get_unaligned_be16 (attr_ptr +2 ); -u16 attr_len = attr_data_len +4 ; +uint attr_len = attr_data_len +4 ; + +/* An attribute must not claim more data than the IE holds. */ +if (attr_ptr + attr_len > wps_ie + wps_ielen) +break ;if (attr_id == target_attr_id) { target_attr_ptr = attr_ptr;
@@ -768,7 +772,9 @@ u8 *rtw_get_wps_attr(u8 *wps_ie, uint wps_ielen, u16 target_attr_id, u8 *buf_att* * Returns: the address of the specific WPS attribute content found, or NULL */ -u8 *rtw_get_wps_attr_content (u8 *wps_ie,uint wps_ielen,u16 target_attr_id,u8 *buf_content,uint *len_content) +u8 *rtw_get_wps_attr_content (u8 *wps_ie,uint wps_ielen,u16 target_attr_id, +u8 *buf_content,uint buf_content_len, +uint *len_content) {u8 *attr_ptr;u32 attr_len;
@@ -779,11 +785,16 @@ u8 *rtw_get_wps_attr_content(u8 *wps_ie, uint wps_ielen, u16 target_attr_id, u8 attr_ptr =rtw_get_wps_attr (wps_ie, wps_ielen, target_attr_id,NULL , &attr_len);if (attr_ptr && attr_len) { +uint content_len = attr_len -4 ; + +if (content_len > buf_content_len) + content_len = buf_content_len; +if (buf_content) -memcpy (buf_content, attr_ptr +4 , attr_len -4 ); +memcpy (buf_content, attr_ptr +4 , content_len);if (len_content) - *len_content = attr_len -4 ; + *len_content = content_len;return attr_ptr +4 ; }
drivers/staging/rtl8723bs/core/rtw_mlme_ext.c +10 -2
@@ -1123,7 +1123,12 @@ unsigned int OnAssocReq(struct adapter *padapter, union recv_frame *precv_frame)if (pmlmepriv->wps_beacon_ie) {u8 selected_registrar =0 ; -rtw_get_wps_attr_content (pmlmepriv->wps_beacon_ie, pmlmepriv->wps_beacon_ie_len,WPS_ATTR_SELECTED_REGISTRAR , &selected_registrar,NULL ); +rtw_get_wps_attr_content (pmlmepriv->wps_beacon_ie, + pmlmepriv->wps_beacon_ie_len, +WPS_ATTR_SELECTED_REGISTRAR , + &selected_registrar, +sizeof (selected_registrar), +NULL );if (!selected_registrar) { status =WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA ;
@@ -2131,7 +2136,10 @@ void issue_beacon(struct adapter *padapter, int timeout_ms)sizeof (struct ieee80211_hdr_3addr ) - _BEACON_IE_OFFSET_, NULL, &wps_ielen);if (wps_ie && wps_ielen >0 ) -rtw_get_wps_attr_content (wps_ie, wps_ielen,WPS_ATTR_SELECTED_REGISTRAR , (u8 *)(&sr),NULL ); +rtw_get_wps_attr_content (wps_ie, wps_ielen, +WPS_ATTR_SELECTED_REGISTRAR , + (u8 *)(&sr),sizeof (sr), +NULL );if (sr !=0 )set_fwstate (pmlmepriv,WIFI_UNDER_WPS );else
drivers/staging/rtl8723bs/include/ieee80211.h +3 -1
@@ -710,7 +710,9 @@ void rtw_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len, u8 *wpa_ieu8 *rtw_get_wps_ie (u8 *in_ie,uint in_len,u8 *wps_ie,uint *wps_ielen);u8 *rtw_get_wps_attr (u8 *wps_ie,uint wps_ielen,u16 target_attr_id,u8 *buf_attr,u32 *len_attr); -u8 *rtw_get_wps_attr_content (u8 *wps_ie,uint wps_ielen,u16 target_attr_id,u8 *buf_content,uint *len_content); +u8 *rtw_get_wps_attr_content (u8 *wps_ie,uint wps_ielen,u16 target_attr_id, +u8 *buf_content,uint buf_content_len, +uint *len_content);/** * for_each_ie - iterate over continuous IEs
drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c +4 -1
@@ -244,7 +244,10 @@ struct cfg80211_bss *rtw_cfg80211_inform_bss(struct adapter *padapter, struct wl wpsie =rtw_get_wps_ie (pnetwork->network.ies + _FIXED_IE_LENGTH_, pnetwork->network.ie_length - _FIXED_IE_LENGTH_,NULL , &wpsielen);if (wpsie && wpsielen >0 ) - psr =rtw_get_wps_attr_content (wpsie, wpsielen,WPS_ATTR_SELECTED_REGISTRAR , (u8 *)(&sr),NULL ); + psr =rtw_get_wps_attr_content (wpsie, wpsielen, +WPS_ATTR_SELECTED_REGISTRAR , + (u8 *)(&sr),sizeof (sr), +NULL );if (sr !=0 ) {/* it means under processing WPS */
Signature · 1 line
2.55.0
Greg Kroah-Hartman
maintainerQuoted 47 lines · Jesus Olmos
rtw_get_wps_attr_content()copiesattr_len- 4 (the WPS attribute's 2-byte data-length field, up to0xffff) from a WPS information element into the caller's buffer with no destination-size bound:memcpy(buf_content, attr_ptr + 4, attr_len - 4);The information element comes straight from a received beacon / probe
response:collect_bss_info()copies the frame's IEs verbatim intobssid->ies, which reaches the scan queue, soattr_lenis attacker controlled.rtw_cfg80211_inform_bss()and two sites inrtw_mlme_ext.ccall this forWPS_ATTR_SELECTED_REGISTRARwith a one-byte destination (u8 sr / u8selected_registrar), because that attribute is a single byte by spec. A frame that declares a longer Selected Registrar attribute therefore overflows the one-byte stack variable during a scan, which happens automatically (NetworkManager/iwd), giving an unauthenticated adjacent attacker a remote stack buffer overflow (at minimum a stack-protector panic).Commit
1463ca3ec660("staging: rtl8723bs: fix OOB reads inrtw_get_sec_ie(),rtw_get_wapi_ie(), andrtw_get_wps_attr()") added a bounds check for the attribute header inrtw_get_wps_attr()but not for the attribute data length, and did not touchrtw_get_wps_attr_content(), so the copy remained both an out-of-bounds read of the attribute data and an out-of-bounds write of the destination.Reject attributes that claim more data than the IE holds (fixing the out-of-bounds read and the latent
memcpy(buf_attr, ...)inrtw_get_wps_attr()), givertw_get_wps_attr_content()the destination buffer size, and clamp the copy to it.Compute the attribute length in an unsigned int rather than u16: a declared data length of
0xfffcmade (u16)(attr_data_len + 4) wrap to 0, which slipped past that bounds check and advanced the parser by zero, looping forever.Found using mwemu (https://github.com/sha0coder/mwemu).
Fixes 554c0a3abf21 ("staging: Add rtl8723bs sdio wifi driver")Assisted-by Claude (Anthropic)Signed-off-by Jesus Olmos <[email protected]>---
v2:
- Widenattr_lento unsigned int so a declared data length near0xffffcannot wrap; for0xfffcthe old u16 wrap defeated the bounds check and madertw_get_wps_attr()loop forever. Thanks Greg for spotting it.
- Add Assisted-by: tag for the AI-assisted analysis.
Quoted 7 lines
Build-tested as a module (
x86_64defconfig +CONFIG_RTL8723BS=m). Not tested on real hardware (I don't have an RTL8723BS device). The bug and the fix were found and checked by source review plus function-level emulation of rtw_get_wps_attr()/rtw_get_wps_attr_content() under mwemu: with a0xfffcdata length the pre-fix code spins forever (u16 wrap ->attr_ptr+= 0) while the fix returns immediately, and the Selected Registrar overflow is clamped to the 1-byte destination.
Please see:
https://lore.kernel.org/r/2026080354-skater-urgent-31b2@gregkh
for why I can't take this patch.
thanks,
greg k-h
Re: [PATCH v2] staging: rtl8723bs: fix stack overflow in rtw_get_wps_attr_content
In reply to Jesus OlmosHi Greg,
Hardware test as requested.
Tested on a RTL8812AU USB adapter using the aircrack-ng out-of-tree driver [1], which shares the same Realtek vendor code and vulnerable call path as the in-tree rtl8723bs.
A crafted beacon with WPS Selected Registrar data_len=200 triggers stack-protector during scan:
[ 2066.558409] Kernel panic - not syncing: stack-protector: Kernel stack is corrupted in: _rtw_cfg80211_surveydone_event_callback+0x257/0x260 [88XXau] [ 2066.560453] CPU: 5 PID: 8151 Comm: RTW_CMD_THREAD Tainted: P OE 6.1.0-39-amd64 #1 Debian 6.1.148-1 [ 2066.562495] Hardware name: Dell Inc. Precision 7510/02H5GH, BIOS 1.22.5 05/26/2021 [ 2066.564135] Call Trace: [ 2066.564853] <TASK> [ 2066.565569] dump_stack_lvl+0x44/0x5c [ 2066.566296] panic+0x118/0x2f4 [ 2066.567020] ? _rtw_cfg80211_surveydone_event_callback+0x257/0x260 [88XXau] [ 2066.567817] __stack_chk_fail+0x10/0x10 [ 2066.568542] _rtw_cfg80211_surveydone_event_callback+0x257/0x260 [88XXau] [ 2066.569336] ? mlme_evt_hdl+0x63/0x80 [88XXau] [ 2066.570107] ? rtw_cmd_thread+0x23a/0x510 [88XXau] [ 2066.570865] ? set_tx_beacon_cmd+0xd0/0xd0 [88XXau] [ 2066.571635] ? rtw_stop_cmd_thread+0x50/0x50 [88XXau] [ 2066.572403] ? kthread+0xd7/0x100 [ 2066.573139] ? kthread_complete_and_exit+0x20/0x20 [ 2066.573867] ? ret_from_fork+0x1f/0x30 [ 2066.574570] </TASK> [ 2067.487113] ---[ end Kernel panic - not syncing: stack-protector: Kernel stack is corrupted in: _rtw_cfg80211_surveydone_event_callback+0x257/0x260 [88XXau] ]---
Sourcing rtl8723bs SDIO hardware for a direct in-tree test.
[1] https://github.com/aircrack-ng/rtl8812au
Jesus Olmos <[email protected]>