Linux wireless drivers development
 help / color / mirror / Atom feed
* [PATCH v4 1/2] wifi: rtw89: fix element walk bounds checks
@ 2026-11-23 18:22 Falconet3
  2026-11-27  6:45 ` Ping-Ke Shih
  0 siblings, 1 reply; 2+ messages in thread
From: Falconet3 @ 2026-11-23 18:22 UTC (permalink / raw)
  To: linux-wireless@vger.kernel.org; +Cc: pkshih@realtek.com

__rtw89_fw_recognize_elements() checks only the element body against
firmware->size, omitting the 32-byte struct rtw89_fw_element_hdr:

	while (offset + sizeof(*hdr) < firmware->size) {
		...
		elm_size = le32_to_cpu(hdr->size);
		if (offset + elm_size >= firmware->size)

The element handlers, and rtw89_fw_elem_dup_if_needed() when it copies
elements (size_t len = sizeof(*elm) + le32_to_cpu(elm->size)), read the
header and the body. A crafted element with
offset + elm_size == firmware->size - 1 passes the check and is read up
to sizeof(struct rtw89_fw_element_hdr) - 1 = 31 bytes past
firmware->data. For firmware larger than 2 MB the copy path also stores
those bytes in a persistent devm_ buffer.

The walk start offset is not validated either: rtw89_mfw_get_size()
returns le32_to_cpu(mfw_info->shift) + le32_to_cpu(mfw_info->size), and
rtw89_mfw_validate_hdr() only checks that the info array fits in the
buffer. On 32-bit (sizeof(size_t) == 4) an aligned offset >= 0xffffffe0
wraps "offset + sizeof(*hdr)" in the loop guard, so hdr->size is
dereferenced out of bounds before any check runs.

Check the start offset before dereferencing hdr and use size_t remainder
arithmetic for both bounds checks, which cannot wrap on any
architecture. An element whose header and non-empty body end exactly at
firmware->size is now accepted; before, such exact fits were rejected
even though they are in bounds.

Fixes: a337d4331fd6 ("wifi: rtw89: introduce infrastructure of firmware elements")
Cc: stable@vger.kernel.org
Assisted-by: LLM Mistral Vibe
Signed-off-by: Falconet3 <Falconet3@proton.me>
---
v4: resend as a new thread at the maintainer's request; no code change
    since v3.
v3: resend of the series with the correct subjects and version label; no
    code change since v2.
v2: validate the walk start offset before dereferencing hdr (fixes the
    32-bit wrap), use size_t remainder arithmetic, note the exact-fit
    acceptance, correct Fixes: and the affected range, add Cc: stable.
    Compile- and sparse-tested on x86_64; no reproducer available.

 drivers/net/wireless/realtek/rtw89/fw.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/net/wireless/realtek/rtw89/fw.c b/drivers/net/wireless/realtek/rtw89/fw.c
--- a/drivers/net/wireless/realtek/rtw89/fw.c
+++ b/drivers/net/wireless/realtek/rtw89/fw.c
@@ -1671,11 +1671,12 @@
 	u32 elem_id;
 	int ret;
 
-	while (offset + sizeof(*hdr) < firmware->size) {
+	while (offset < firmware->size &&
+	       firmware->size - offset > sizeof(*hdr)) {
 		hdr = (const struct rtw89_fw_element_hdr *)(firmware->data + offset);
 
 		elm_size = le32_to_cpu(hdr->size);
-		if (offset + elm_size >= firmware->size) {
+		if (elm_size > firmware->size - offset - sizeof(*hdr)) {
 			rtw89_warn(rtwdev, "firmware element size exceeds\n");
 			break;
 		}
-- 
2.53.0

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

end of thread, other threads:[~2026-11-27  6:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 18:22 [PATCH v4 1/2] wifi: rtw89: fix element walk bounds checks Falconet3
2026-11-27  6:45 ` Ping-Ke Shih

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox