Linux wireless drivers development
 help / color / mirror / Atom feed
From: Ping-Ke Shih <pkshih@realtek.com>
To: Falconet3 <Falconet3@proton.me>,
	"linux-wireless@vger.kernel.org" <linux-wireless@vger.kernel.org>
Subject: RE: [PATCH v4 1/2] wifi: rtw89: fix element walk bounds checks
Date: Fri, 27 Nov 2026 06:45:53 +0000	[thread overview]
Message-ID: <98320cb119c54cd9be1c36f0ca1126a7@realtek.com> (raw)
In-Reply-To: <B6QON2a1ZGk3ddppttcN5qus6WXB2bodBbLcQQEXK7zfi7AcYQjU2qTIjYb4nS7QE62oRYH14MK65-AZ4VafzlrFpNKhEFunww-H6B1kuV4=@proton.me>

Falconet3 <Falconet3@proton.me> wrote:
> __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.

Please reduce the message. The main point is that you just want to avoid
overflow case.

> 
> 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)) {

This becomes not readable. I don't think people can be easy to understand
what it does. 

Maybe,

u32 offset_hdr_end;

while (!check_add_overflow(offset, sizeof(*hdr), &offset_hdr_end) &&
	   offset_hdr_end < 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 (elm_size > firmware->size - offset - sizeof(*hdr)) {

Similarly. 

u32 offset_elm_end;

if (check_add_overflow(offset, elm_size, &offset_elm_end) ||
    offset_elm_end >= firmware->size)


>                         rtw89_warn(rtwdev, "firmware element size exceeds\n");
>                         break;
>                 }
> --
> 2.53.0

      reply	other threads:[~2026-11-27  6:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=98320cb119c54cd9be1c36f0ca1126a7@realtek.com \
    --to=pkshih@realtek.com \
    --cc=Falconet3@proton.me \
    --cc=linux-wireless@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox