Skip to contentSkip to conversation

wifi: brcmfmac: Fix field-spanning write in brcmf_scan_params_v2_to_v1()

PATCH In review Hans de Goede 4 participants 7 messages Jul 29, 2023 – Aug 2, 2023 Broadcom BRCM80211 IEEE802.11 wireless drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 wifi: brcmfmac: Fix field-spanning write in brcmf_scan_params_v2_to_v1()

Using brcmfmac with 6.5-rc3 on a brcmfmac43241b4-sdio triggers a backtrace caused by the following field-spanning error:

memcpy: detected field-spanning write (size 120) of single field
"&params_le->channel_list[0]" at drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c:1072 (size 2)

Fix this by replacing the channel_list[1] declaration at the end of the struct with a flexible array declaration.

Most users of struct brcmf_scan_params_le calculate the size to alloc using the size of the non flex-array part of the struct + needed extra space, so they do not care about sizeof(struct brcmf_scan_params_le).

brcmf_notify_escan_complete() however uses the struct on the stack, expecting there to be room for at least 1 entry in the channel-list to store the special -1 abort channel-id.

To make this work use an anonymous union with a padding member added + the actual channel_list flexible array.

---
 .../net/wireless/broadcom/brcm80211/brcmfmac/fwil_types.h  | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/net/wireless/broadcom/brcm80211/brcmfmac/fwil_types.h +6 -1
index 792adaf880b4..bece26741d3a 100644
@@ -398,7 +398,12 @@ struct brcmf_scan_params_le {
 				 * fixed parameter portion is assumed, otherwise
 				 * ssid in the fixed portion is ignored
 				 */
-	__le16 channel_list[1];	/* list of chanspecs */
+	union {
+		__le16 padding;	/* Reserve space for at least 1 entry for abort
+				 * which uses an on stack brcmf_scan_params_le
+				 */
+		DECLARE_FLEX_ARRAY(__le16, channel_list);	/* chanspecs */
+	};
 };
 
 struct brcmf_scan_params_v2_le {
Signature · 1 line
2.41.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Hans de Goede

Using brcmfmac with 6.5-rc3 on a brcmfmac43241b4-sdio triggers a backtrace caused by the following field-spanning error:

memcpy: detected field-spanning write (size 120) of single field
"&params_le->channel_list[0]" at drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c:1072 (size 2)

Fix this by replacing the channel_list[1] declaration at the end of the struct with a flexible array declaration.

Most users of struct brcmf_scan_params_le calculate the size to alloc using the size of the non flex-array part of the struct + needed extra space, so they do not care about sizeof(struct brcmf_scan_params_le).

brcmf_notify_escan_complete() however uses the struct on the stack, expecting there to be room for at least 1 entry in the channel-list to store the special -1 abort channel-id.

Quoted 5 lines

To make this work use an anonymous union with a padding member added + the actual channel_list flexible array.

Looks good to me; it's consistent with how similar 1-element arrays with sensitive structure sizes have been updated lately. Thanks for the investigation!

-Kees

Signature · 1 line
Kees Cook
#2 lore raw
In reply to Hans de Goede
Quoted 18 lines · Hans de Goede

Using brcmfmac with 6.5-rc3 on a brcmfmac43241b4-sdio triggers a backtrace caused by the following field-spanning error:

memcpy: detected field-spanning write (size 120) of single field
"&params_le->channel_list[0]" at drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c:1072 (size 2)

Fix this by replacing the channel_list[1] declaration at the end of the struct with a flexible array declaration.

Most users of struct brcmf_scan_params_le calculate the size to alloc using the size of the non flex-array part of the struct + needed extra space, so they do not care about sizeof(struct brcmf_scan_params_le).

brcmf_notify_escan_complete() however uses the struct on the stack, expecting there to be room for at least 1 entry in the channel-list to store the special -1 abort channel-id.

Quoted 6 lines

To make this work use an anonymous union with a padding member added + the actual channel_list flexible array.

Does the driver still work even if this warning is printed? I'm wondering should I take this to wireless or wireless-next. Also a review from Broadcom would be really good.

What about a Fixes tag?

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/patch/[email protected]/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#3 lore raw

Hi,

Quoted 23 lines · Kalle Valo
Quoted 21 lines · Hans de Goede

Using brcmfmac with 6.5-rc3 on a brcmfmac43241b4-sdio triggers a backtrace caused by the following field-spanning error:

memcpy: detected field-spanning write (size 120) of single field
"&params_le->channel_list[0]" at drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c:1072 (size 2)

Fix this by replacing the channel_list[1] declaration at the end of the struct with a flexible array declaration.

Most users of struct brcmf_scan_params_le calculate the size to alloc using the size of the non flex-array part of the struct + needed extra space, so they do not care about sizeof(struct brcmf_scan_params_le).

brcmf_notify_escan_complete() however uses the struct on the stack, expecting there to be room for at least 1 entry in the channel-list to store the special -1 abort channel-id.

To make this work use an anonymous union with a padding member added + the actual channel_list flexible array.

Quoted 7 lines
Quoted 3 lines

Does the driver still work even if this warning is printed? I'm wondering should I take this to wireless or wireless-next. Also a review from Broadcom would be really good.

It works fine, but it logs an oops / backtrace.

Note I did test the patch on a device where the warning was triggered and the warning is gone and wifi association still works.

So there is a slight preference to get this as a fix into 6.5 from my side.

Quoted 1 line

What about a Fixes tag?

This is caused by the new field-spanning wtire checks enabled recently, so there is not really a brcmfmac commit to point to as the culprit.

Regards,

Hans

#4 lore raw
Quoted 30 lines · Hans de Goede

Hi,

Quoted 27 lines · Kalle Valo
Quoted 24 lines · Hans de Goede

Using brcmfmac with 6.5-rc3 on a brcmfmac43241b4-sdio triggers a backtrace caused by the following field-spanning error:

memcpy: detected field-spanning write (size 120) of single field
"&params_le->channel_list[0]" at drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c:1072 (size 2)

Fix this by replacing the channel_list[1] declaration at the end of the struct with a flexible array declaration.

Most users of struct brcmf_scan_params_le calculate the size to alloc using the size of the non flex-array part of the struct + needed extra space, so they do not care about sizeof(struct brcmf_scan_params_le).

brcmf_notify_escan_complete() however uses the struct on the stack, expecting there to be room for at least 1 entry in the channel-list to store the special -1 abort channel-id.

To make this work use an anonymous union with a padding member added + the actual channel_list flexible array.

Quoted 5 lines
Quoted 3 lines

Does the driver still work even if this warning is printed? I'm wondering should I take this to wireless or wireless-next. Also a review from Broadcom would be really good.

It works fine, but it logs an oops / backtrace.

I'll add that info to the commit log.

Quoted 4 lines

Note I did test the patch on a device where the warning was triggered and the warning is gone and wifi association still works.

So there is a slight preference to get this as a fix into 6.5 from my side.

I'll queue this wireless then. But I really would like to Broadcom take a look at this in case we are missing something.

Quoted 5 lines
Quoted 1 line

What about a Fixes tag?

This is caused by the new field-spanning wtire checks enabled recently, so there is not really a brcmfmac commit to point to as the culprit.

Ok.

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/list/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
Reviewed-by
#5 lore raw
Quoted 26 lines · Kalle Valo
Quoted 23 lines · Hans de Goede

Hi,

Quoted 20 lines · Kalle Valo
Quoted 18 lines · Hans de Goede

Using brcmfmac with 6.5-rc3 on a brcmfmac43241b4-sdio triggers a backtrace caused by the following field-spanning error:

memcpy: detected field-spanning write (size 120) of single field
"&params_le->channel_list[0]" at drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c:1072 (size 2)

Fix this by replacing the channel_list[1] declaration at the end of the struct with a flexible array declaration.

Most users of struct brcmf_scan_params_le calculate the size to alloc using the size of the non flex-array part of the struct + needed extra space, so they do not care about sizeof(struct brcmf_scan_params_le).

brcmf_notify_escan_complete() however uses the struct on the stack, expecting there to be room for at least 1 entry in the channel-list to store the special -1 abort channel-id.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

To make this work use an anonymous union with a padding member added + the actual channel_list flexible array.

Looks good to me, thanks for taking care of it.

#6 lore raw
In reply to Hans de Goede
Quoted 21 lines · Hans de Goede

Using brcmfmac with 6.5-rc3 on a brcmfmac43241b4-sdio triggers a backtrace caused by the following field-spanning warning:

memcpy: detected field-spanning write (size 120) of single field
"&params_le->channel_list[0]" at drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c:1072 (size 2)

The driver still works after this warning. The warning was introduced by the new field-spanning write checks which were enabled recently.

Fix this by replacing the channel_list[1] declaration at the end of the struct with a flexible array declaration.

Most users of struct brcmf_scan_params_le calculate the size to alloc using the size of the non flex-array part of the struct + needed extra space, so they do not care about sizeof(struct brcmf_scan_params_le).

brcmf_notify_escan_complete() however uses the struct on the stack, expecting there to be room for at least 1 entry in the channel-list to store the special -1 abort channel-id.

Quoted 7 lines

To make this work use an anonymous union with a padding member added + the actual channel_list flexible array.

Patch applied to wireless.git, thanks.

16e455a465fc wifi: brcmfmac: Fix field-spanning write in brcmf_scan_params_v2_to_v1()

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/patch/[email protected]/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches