brcm80211.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH][next] wifi: brcmfmac: fweh: Fix boot crash on Raspberry Pi 4
@ 2024-02-16 19:27 Gustavo A. R. Silva
  2024-02-16 21:42 ` Kees Cook
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Gustavo A. R. Silva @ 2024-02-16 19:27 UTC (permalink / raw)
  To: Arend van Spriel, Kalle Valo, Nathan Chancellor
  Cc: linux-wireless, brcm80211, brcm80211-dev-list.pdl, linux-kernel,
	Gustavo A. R. Silva, linux-hardening

Fix boot crash on Raspberry Pi by moving the update to `event->datalen`
before data is copied into flexible-array member `data` via `memcpy()`.

Flexible-array member `data` was annotated with `__counted_by(datalen)`
in commit 62d19b358088 ("wifi: brcmfmac: fweh: Add __counted_by for
struct brcmf_fweh_queue_item and use struct_size()"). The intention of
this is to gain visibility into the size of `data` at run-time through
its _counter_ (in this case `datalen`), and with this have its accesses
bounds-checked at run-time via CONFIG_FORTIFY_SOURCE and
CONFIG_UBSAN_BOUNDS.

To effectively accomplish the above, we shall update the counter
(`datalen`), before the first access to the flexible array (`data`),
which was also done in the mentioned commit.

However, commit edec42821911 ("wifi: brcmfmac: allow per-vendor event
handling") inadvertently caused a buffer overflow, detected by
FORTIFY_SOURCE. It moved the `event->datalen = datalen;` update to after
the first `data` access, at which point `event->datalen` was not yet
updated from zero (after calling `kzalloc()`), leading to the overflow
issue.

This fix repositions the `event->datalen = datalen;` update before
accessing `data`, restoring the intended buffer overflow protection. :)

Fixes: edec42821911 ("wifi: brcmfmac: allow per-vendor event handling")
Reported-by: Nathan Chancellor <nathan@kernel.org>
Closes: https://gist.github.com/nathanchance/e22f681f3bfc467f15cdf6605021aaa6
Tested-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c
index 0774f6c59226..f0b6a7607f16 100644
--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c
+++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c
@@ -497,12 +497,12 @@ void brcmf_fweh_process_event(struct brcmf_pub *drvr,
 		return;
 
 	event->code = fwevt_idx;
+	event->datalen = datalen;
 	event->ifidx = event_packet->msg.ifidx;
 
 	/* use memcpy to get aligned event message */
 	memcpy(&event->emsg, &event_packet->msg, sizeof(event->emsg));
 	memcpy(event->data, data, datalen);
-	event->datalen = datalen;
 	memcpy(event->ifaddr, event_packet->eth.h_dest, ETH_ALEN);
 
 	brcmf_fweh_queue_event(fweh, event);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH][next] wifi: brcmfmac: fweh: Fix boot crash on Raspberry Pi 4
  2024-02-16 19:27 [PATCH][next] wifi: brcmfmac: fweh: Fix boot crash on Raspberry Pi 4 Gustavo A. R. Silva
@ 2024-02-16 21:42 ` Kees Cook
  2024-02-27  9:19 ` Kalle Valo
  2024-02-27 14:43 ` Kalle Valo
  2 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2024-02-16 21:42 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Arend van Spriel, Kalle Valo, Nathan Chancellor, linux-wireless,
	brcm80211, brcm80211-dev-list.pdl, linux-kernel, linux-hardening

On Fri, Feb 16, 2024 at 01:27:56PM -0600, Gustavo A. R. Silva wrote:
> Fix boot crash on Raspberry Pi by moving the update to `event->datalen`
> before data is copied into flexible-array member `data` via `memcpy()`.
> 
> Flexible-array member `data` was annotated with `__counted_by(datalen)`
> in commit 62d19b358088 ("wifi: brcmfmac: fweh: Add __counted_by for
> struct brcmf_fweh_queue_item and use struct_size()"). The intention of
> this is to gain visibility into the size of `data` at run-time through
> its _counter_ (in this case `datalen`), and with this have its accesses
> bounds-checked at run-time via CONFIG_FORTIFY_SOURCE and
> CONFIG_UBSAN_BOUNDS.
> 
> To effectively accomplish the above, we shall update the counter
> (`datalen`), before the first access to the flexible array (`data`),
> which was also done in the mentioned commit.
> 
> However, commit edec42821911 ("wifi: brcmfmac: allow per-vendor event
> handling") inadvertently caused a buffer overflow, detected by
> FORTIFY_SOURCE. It moved the `event->datalen = datalen;` update to after
> the first `data` access, at which point `event->datalen` was not yet
> updated from zero (after calling `kzalloc()`), leading to the overflow
> issue.
> 
> This fix repositions the `event->datalen = datalen;` update before
> accessing `data`, restoring the intended buffer overflow protection. :)
> 
> Fixes: edec42821911 ("wifi: brcmfmac: allow per-vendor event handling")
> Reported-by: Nathan Chancellor <nathan@kernel.org>
> Closes: https://gist.github.com/nathanchance/e22f681f3bfc467f15cdf6605021aaa6
> Tested-by: Nathan Chancellor <nathan@kernel.org>
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Yup, this looks correct. Thanks!

Reviewed-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH][next] wifi: brcmfmac: fweh: Fix boot crash on Raspberry Pi 4
  2024-02-16 19:27 [PATCH][next] wifi: brcmfmac: fweh: Fix boot crash on Raspberry Pi 4 Gustavo A. R. Silva
  2024-02-16 21:42 ` Kees Cook
@ 2024-02-27  9:19 ` Kalle Valo
  2024-02-27 11:12   ` Arend van Spriel
  2024-02-27 14:43 ` Kalle Valo
  2 siblings, 1 reply; 5+ messages in thread
From: Kalle Valo @ 2024-02-27  9:19 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Arend van Spriel, Nathan Chancellor, linux-wireless, brcm80211,
	brcm80211-dev-list.pdl, linux-kernel, Gustavo A. R. Silva,
	linux-hardening

"Gustavo A. R. Silva" <gustavoars@kernel.org> wrote:

> Fix boot crash on Raspberry Pi by moving the update to `event->datalen`
> before data is copied into flexible-array member `data` via `memcpy()`.
> 
> Flexible-array member `data` was annotated with `__counted_by(datalen)`
> in commit 62d19b358088 ("wifi: brcmfmac: fweh: Add __counted_by for
> struct brcmf_fweh_queue_item and use struct_size()"). The intention of
> this is to gain visibility into the size of `data` at run-time through
> its _counter_ (in this case `datalen`), and with this have its accesses
> bounds-checked at run-time via CONFIG_FORTIFY_SOURCE and
> CONFIG_UBSAN_BOUNDS.
> 
> To effectively accomplish the above, we shall update the counter
> (`datalen`), before the first access to the flexible array (`data`),
> which was also done in the mentioned commit.
> 
> However, commit edec42821911 ("wifi: brcmfmac: allow per-vendor event
> handling") inadvertently caused a buffer overflow, detected by
> FORTIFY_SOURCE. It moved the `event->datalen = datalen;` update to after
> the first `data` access, at which point `event->datalen` was not yet
> updated from zero (after calling `kzalloc()`), leading to the overflow
> issue.
> 
> This fix repositions the `event->datalen = datalen;` update before
> accessing `data`, restoring the intended buffer overflow protection. :)
> 
> Fixes: edec42821911 ("wifi: brcmfmac: allow per-vendor event handling")
> Reported-by: Nathan Chancellor <nathan@kernel.org>
> Closes: https://gist.github.com/nathanchance/e22f681f3bfc467f15cdf6605021aaa6
> Tested-by: Nathan Chancellor <nathan@kernel.org>
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> Reviewed-by: Kees Cook <keescook@chromium.org>

Arend, ack?

-- 
https://patchwork.kernel.org/project/linux-wireless/patch/Zc+3PFCUvLoVlpg8@neat/

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH][next] wifi: brcmfmac: fweh: Fix boot crash on Raspberry Pi 4
  2024-02-27  9:19 ` Kalle Valo
@ 2024-02-27 11:12   ` Arend van Spriel
  0 siblings, 0 replies; 5+ messages in thread
From: Arend van Spriel @ 2024-02-27 11:12 UTC (permalink / raw)
  To: Kalle Valo, Gustavo A. R. Silva
  Cc: Nathan Chancellor, linux-wireless, brcm80211,
	brcm80211-dev-list.pdl, linux-kernel, linux-hardening

[-- Attachment #1: Type: text/plain, Size: 1948 bytes --]

On 2/27/2024 10:19 AM, Kalle Valo wrote:
> "Gustavo A. R. Silva" <gustavoars@kernel.org> wrote:
> 
>> Fix boot crash on Raspberry Pi by moving the update to `event->datalen`
>> before data is copied into flexible-array member `data` via `memcpy()`.
>>
>> Flexible-array member `data` was annotated with `__counted_by(datalen)`
>> in commit 62d19b358088 ("wifi: brcmfmac: fweh: Add __counted_by for
>> struct brcmf_fweh_queue_item and use struct_size()"). The intention of
>> this is to gain visibility into the size of `data` at run-time through
>> its _counter_ (in this case `datalen`), and with this have its accesses
>> bounds-checked at run-time via CONFIG_FORTIFY_SOURCE and
>> CONFIG_UBSAN_BOUNDS.
>>
>> To effectively accomplish the above, we shall update the counter
>> (`datalen`), before the first access to the flexible array (`data`),
>> which was also done in the mentioned commit.
>>
>> However, commit edec42821911 ("wifi: brcmfmac: allow per-vendor event
>> handling") inadvertently caused a buffer overflow, detected by
>> FORTIFY_SOURCE. It moved the `event->datalen = datalen;` update to after
>> the first `data` access, at which point `event->datalen` was not yet
>> updated from zero (after calling `kzalloc()`), leading to the overflow
>> issue.
>>
>> This fix repositions the `event->datalen = datalen;` update before
>> accessing `data`, restoring the intended buffer overflow protection. :)
>>
>> Fixes: edec42821911 ("wifi: brcmfmac: allow per-vendor event handling")
>> Reported-by: Nathan Chancellor <nathan@kernel.org>
>> Closes: https://gist.github.com/nathanchance/e22f681f3bfc467f15cdf6605021aaa6
>> Tested-by: Nathan Chancellor <nathan@kernel.org>
>> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
>> Reviewed-by: Kees Cook <keescook@chromium.org>
> 
> Arend, ack?

Figured Kees Cook was the trumping authority here, but here it is:

Acked-by: Arend van Spriel <arend.vanspriel@broadcom.com>

Gr. AvS

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4219 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH][next] wifi: brcmfmac: fweh: Fix boot crash on Raspberry Pi 4
  2024-02-16 19:27 [PATCH][next] wifi: brcmfmac: fweh: Fix boot crash on Raspberry Pi 4 Gustavo A. R. Silva
  2024-02-16 21:42 ` Kees Cook
  2024-02-27  9:19 ` Kalle Valo
@ 2024-02-27 14:43 ` Kalle Valo
  2 siblings, 0 replies; 5+ messages in thread
From: Kalle Valo @ 2024-02-27 14:43 UTC (permalink / raw)
  To: Gustavo A. R. Silva
  Cc: Arend van Spriel, Nathan Chancellor, linux-wireless, brcm80211,
	brcm80211-dev-list.pdl, linux-kernel, Gustavo A. R. Silva,
	linux-hardening

"Gustavo A. R. Silva" <gustavoars@kernel.org> wrote:

> Fix boot crash on Raspberry Pi by moving the update to `event->datalen`
> before data is copied into flexible-array member `data` via `memcpy()`.
> 
> Flexible-array member `data` was annotated with `__counted_by(datalen)`
> in commit 62d19b358088 ("wifi: brcmfmac: fweh: Add __counted_by for
> struct brcmf_fweh_queue_item and use struct_size()"). The intention of
> this is to gain visibility into the size of `data` at run-time through
> its _counter_ (in this case `datalen`), and with this have its accesses
> bounds-checked at run-time via CONFIG_FORTIFY_SOURCE and
> CONFIG_UBSAN_BOUNDS.
> 
> To effectively accomplish the above, we shall update the counter
> (`datalen`), before the first access to the flexible array (`data`),
> which was also done in the mentioned commit.
> 
> However, commit edec42821911 ("wifi: brcmfmac: allow per-vendor event
> handling") inadvertently caused a buffer overflow, detected by
> FORTIFY_SOURCE. It moved the `event->datalen = datalen;` update to after
> the first `data` access, at which point `event->datalen` was not yet
> updated from zero (after calling `kzalloc()`), leading to the overflow
> issue.
> 
> This fix repositions the `event->datalen = datalen;` update before
> accessing `data`, restoring the intended buffer overflow protection. :)
> 
> Fixes: edec42821911 ("wifi: brcmfmac: allow per-vendor event handling")
> Reported-by: Nathan Chancellor <nathan@kernel.org>
> Closes: https://gist.github.com/nathanchance/e22f681f3bfc467f15cdf6605021aaa6
> Tested-by: Nathan Chancellor <nathan@kernel.org>
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> Reviewed-by: Kees Cook <keescook@chromium.org>
> Acked-by: Arend van Spriel <arend.vanspriel@broadcom.com>

Patch applied to wireless-next.git, thanks.

ec1aae190c77 wifi: brcmfmac: fweh: Fix boot crash on Raspberry Pi 4

-- 
https://patchwork.kernel.org/project/linux-wireless/patch/Zc+3PFCUvLoVlpg8@neat/

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-02-27 14:43 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-16 19:27 [PATCH][next] wifi: brcmfmac: fweh: Fix boot crash on Raspberry Pi 4 Gustavo A. R. Silva
2024-02-16 21:42 ` Kees Cook
2024-02-27  9:19 ` Kalle Valo
2024-02-27 11:12   ` Arend van Spriel
2024-02-27 14:43 ` Kalle Valo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).