All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] wifi: rtw89: fix OOB read past firmware buffer when duplicating elements
@ 2026-11-22  0:06 Falconet3
  2026-11-22  1:31 ` Ping-Ke Shih
  2026-11-22 11:00 ` [PATCH v2 1/2] " Falconet3
  0 siblings, 2 replies; 5+ messages in thread
From: Falconet3 @ 2026-11-22  0:06 UTC (permalink / raw)
  To: pkshih@realtek.com
  Cc: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org

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

Fix for an OOB read (up to 31 bytes past the firmware buffer) introduced in v7.4-rc1 by the fw-element copy-on-parse rework. Patch attached, applies to v7.4-rc3. Thanks!

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-wifi-rtw89-fix-OOB-read-in-fw-element-dup.patch --]
[-- Type: text/x-patch; name=0001-wifi-rtw89-fix-OOB-read-in-fw-element-dup.patch, Size: 2685 bytes --]

From: Falconet3 <Falconet3@proton.me>
Date: Sat, 21 Nov 2026 21:00:00 +0000
Subject: [PATCH] wifi: rtw89: fix OOB read past firmware buffer when duplicating elements

Since v7.4-rc1, rtw89 copies firmware elements at parse time so the
struct firmware can be released after probe
(rtw89_fw_elem_dup_if_needed(), fw.c:168). The copy length is
sizeof(*elm) + le32_to_cpu(elm->size) (the element header is 32 bytes,
fw.h:4646), but the element walk in __rtw89_fw_recognize_elements()
only validates "offset + elm_size < firmware->size" (fw.c:1678),
omitting the header size.

A crafted firmware element with offset + elm_size == firmware->size - 1
therefore passes the check, and devm_kmemdup()/vmalloc+memcpy in
__rtw89_fw_elem_dup_if_needed() (fw.c:141-160, called from fw.c:1361,
1417, 1524, 1536, 1562) reads up to sizeof(struct rtw89_fw_element_hdr)-1
= 31 bytes past the end of firmware->data.

Impact: heap/vmalloc OOB read of up to 31 bytes at driver probe,
copied into a persistent devm_ buffer; on exact-size vmalloc firmware
buffers the read can hit the guard page (probe-time oops/DoS).
Threat model: malicious/replaced firmware file (root or supply chain).
Affects v7.4-rc1..v7.4-rc3 only (in v7.3.5 and earlier the handlers
stored pointers into the firmware instead of copying, fw.c:1346).

Fix the walk to validate header + body, overflow-safe.

Fix was verified with git apply --check against v7.4-rc3; not
build-tested and no reproducer available in this environment.

Fixes: FILL-IN-SHA ("wifi: rtw89: ...")
Assisted-by: LLM Mistral Vibe
Signed-off-by: Falconet3 <Falconet3@proton.me>
---
 drivers/net/wireless/realtek/rtw89/fw.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/net/wireless/realtek/rtw89/fw.c b/drivers/net/wireless/realtek/rtw89/fw.c
index 0000000..0000000 100644
--- a/drivers/net/wireless/realtek/rtw89/fw.c
+++ b/drivers/net/wireless/realtek/rtw89/fw.c
@@ -1668,6 +1668,7 @@ static int __rtw89_fw_recognize_elements(struct rtw89_dev *rtwdev,
 	const struct rtw89_fw_element_handler *handler;
 	const struct rtw89_fw_element_hdr *hdr;
 	u32 elm_size;
 	u32 elem_id;
+	u32 end;
 	int ret;
 
@@ -1674,8 +1675,10 @@ static int __rtw89_fw_recognize_elements(struct rtw89_dev *rtwdev,
 	while (offset + sizeof(*hdr) < firmware->size) {
 		hdr = (const struct rtw89_fw_element_hdr *)(firmware->data + offset);
 
 		elm_size = le32_to_cpu(hdr->size);
-		if (offset + elm_size >= firmware->size) {
+		if (check_add_overflow(offset, (u32)sizeof(*hdr), &end) ||
+		    check_add_overflow(end, elm_size, &end) ||
+		    end > firmware->size) {
 			rtw89_warn(rtwdev, "firmware element size exceeds\n");
 			break;
 		}
-- 
2.53.0

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

* RE: [PATCH] wifi: rtw89: fix OOB read past firmware buffer when duplicating elements
  2026-11-22  0:06 [PATCH] wifi: rtw89: fix OOB read past firmware buffer when duplicating elements Falconet3
@ 2026-11-22  1:31 ` Ping-Ke Shih
  2026-11-22 11:00 ` [PATCH v2 1/2] " Falconet3
  1 sibling, 0 replies; 5+ messages in thread
From: Ping-Ke Shih @ 2026-11-22  1:31 UTC (permalink / raw)
  To: Falconet3; +Cc: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org

Falconet3 <Falconet3@proton.me> wrote:
> Fix for an OOB read (up to 31 bytes past the firmware buffer) introduced in v7.4-rc1 by the fw-element
> copy-on-parse rework. Patch attached, applies to v7.4-rc3. Thanks!

Please rephrase commit message generated by LLM and send patch inline. 

---
pw-bot: cr


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

* Re: [PATCH v2 1/2] wifi: rtw89: fix OOB read past firmware buffer when duplicating elements
  2026-11-22  0:06 [PATCH] wifi: rtw89: fix OOB read past firmware buffer when duplicating elements Falconet3
  2026-11-22  1:31 ` Ping-Ke Shih
@ 2026-11-22 11:00 ` Falconet3
  2026-11-22 11:02   ` [PATCH v2 2/2] " Falconet3
  1 sibling, 1 reply; 5+ messages in thread
From: Falconet3 @ 2026-11-22 11:00 UTC (permalink / raw)
  To: pkshih@realtek.com
  Cc: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org

__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, read both header and 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 body end exactly at
firmware->size is then accepted; it is fully in bounds, while such exact
fits were rejected before.

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>
---
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
index 0000000..0000000 100644
--- a/drivers/net/wireless/realtek/rtw89/fw.c
+++ b/drivers/net/wireless/realtek/rtw89/fw.c
@@ -1668,16 +1668,17 @@ static int __rtw89_fw_recognize_elements(struct rtw89_dev *rtwdev,
 	const struct rtw89_fw_element_handler *handler;
 	const struct rtw89_fw_element_hdr *hdr;
 	u32 elm_size;
 	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;
 		}
 
 		elem_id = le32_to_cpu(hdr->id);
-- 
2.53.0

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

* Re: [PATCH v2 2/2] wifi: rtw89: fix OOB read past firmware buffer when duplicating elements
  2026-11-22 11:00 ` [PATCH v2 1/2] " Falconet3
@ 2026-11-22 11:02   ` Falconet3
  2026-11-22 11:06     ` [PATCH v3 1/2] " Falconet3
  0 siblings, 1 reply; 5+ messages in thread
From: Falconet3 @ 2026-11-22 11:02 UTC (permalink / raw)
  To: pkshih@realtek.com
  Cc: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org

rtw89_board_elm_seek() validates the selected entry against the board
element file with

	if (unlikely(board_elm->size < ofst + size))

where ofst and size are u32 values read from the file. The addition
wraps in u32, so a crafted file with, for example,
ofst = 0xffffff00 and size = 0x200 passes the check and
pseudo_fw->data is set to board_elm->data + 0xffffff00 with
pseudo_fw->size = 0x200. The element walk then dereferences that
pointer and reads out of bounds.

Use remainder arithmetic against board_elm->size, which cannot wrap on
any architecture. The helper is new in v7.4-rc1, so only the current
cycle is affected.

Fixes: af5905e22a43 ("wifi: rtw89: support variants of FW elements")
Assisted-by: LLM Mistral Vibe
Signed-off-by: Falconet3 <Falconet3@proton.me>
---
v2: new. Found while fixing the element walk bounds check; split out as
    an independent caller. Compile- and sparse-tested on x86_64; no
    reproducer available.

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

diff --git a/drivers/net/wireless/realtek/rtw89/fw.c b/drivers/net/wireless/realtek/rtw89/fw.c
index 0000000..0000000 100644
--- a/drivers/net/wireless/realtek/rtw89/fw.c
+++ b/drivers/net/wireless/realtek/rtw89/fw.c
@@ -1734,12 +1734,13 @@ static int rtw89_board_elm_seek(const struct firmware *board_elm,
 	if (num <= board_id)
 		return -ENOENT;
 
 	ent = &hdr->ents[board_id];
 
 	ofst = le32_to_cpu(ent->ofst);
 	size = le32_to_cpu(ent->size);
 
-	if (unlikely(board_elm->size < ofst + size))
+	if (unlikely(ofst > board_elm->size ||
+		     size > board_elm->size - ofst))
 		return -EFAULT;
 
 	memset(pseudo_fw, 0, sizeof(*pseudo_fw));
-- 
2.53.0

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

* Re: [PATCH v3 1/2] wifi: rtw89: fix OOB read past firmware buffer when duplicating elements
  2026-11-22 11:02   ` [PATCH v2 2/2] " Falconet3
@ 2026-11-22 11:06     ` Falconet3
  0 siblings, 0 replies; 5+ messages in thread
From: Falconet3 @ 2026-11-22 11:06 UTC (permalink / raw)
  To: pkshih@realtek.com
  Cc: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org

__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, read both header and 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 body end exactly at
firmware->size is then accepted; it is fully in bounds, while such exact
fits were rejected before.

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>
---
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
index 0000000..0000000 100644
--- a/drivers/net/wireless/realtek/rtw89/fw.c
+++ b/drivers/net/wireless/realtek/rtw89/fw.c
@@ -1668,16 +1668,17 @@ static int __rtw89_fw_recognize_elements(struct rtw89_dev *rtwdev,
 	const struct rtw89_fw_element_handler *handler;
 	const struct rtw89_fw_element_hdr *hdr;
 	u32 elm_size;
 	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;
 		}
 
 		elem_id = le32_to_cpu(hdr->id);
-- 
2.53.0

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

end of thread, other threads:[~2026-11-22 11:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22  0:06 [PATCH] wifi: rtw89: fix OOB read past firmware buffer when duplicating elements Falconet3
2026-11-22  1:31 ` Ping-Ke Shih
2026-11-22 11:00 ` [PATCH v2 1/2] " Falconet3
2026-11-22 11:02   ` [PATCH v2 2/2] " Falconet3
2026-11-22 11:06     ` [PATCH v3 1/2] " Falconet3

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.