Re: [PATCH v2 1/2] wifi: rtw89: fix OOB read past firmware buffer when duplicating elements

From: Falconet3

Date: Sun Nov 22 2026 - 06:00:27 EST


__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@xxxxxxxxxxxxxxx
Assisted-by: LLM Mistral Vibe
Signed-off-by: Falconet3 <Falconet3@xxxxxxxxx>
---
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