mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH rtw-next v4] wifi: rtw88: 8822c: validate bw and nrx read from registers
@ 2026-11-25 20:37 Zeshan Ahmed Nobin
  2026-11-25 21:14 ` Luka Gejak
  0 siblings, 1 reply; 2+ messages in thread
From: Zeshan Ahmed Nobin @ 2026-11-25 20:37 UTC (permalink / raw)
  To: linux-wireless; +Cc: Ping-Ke Shih, Luka Gejak, Kalle Valo, linux-kernel

rtw8822c_phy_cck_pd_set() reads nrx and bw from the chip with
rtw_read32_mask() and uses them as indexes into dm_info->cck_pd_lv[][]
and rtw8822c_cck_pd_reg[][].

When the chip stops responding on the PCIe bus, register reads return
all ones, so bw and nrx both become 3. bw = 3 is past the end of both
arrays.  When the chip stopped responding on Ubuntu's 7.0.0-43 kernel,
UBSAN reported:

  UBSAN: array-index-out-of-bounds in .../rtw88/rtw8822c.c:4407:21
  index 3 is out of range for type 'u8 [2][4]'
  Workqueue: phy0 rtw_watch_dog_work [rtw88_core]
  Call Trace:
   rtw8822c_phy_cck_pd_set+0x33c/0x510 [rtw88_8822c]
   rtw_phy_dynamic_mechanism+0x310/0x330 [rtw88_core]
   rtw_watch_dog_work+0x217/0x330 [rtw88_core]

nrx = 3 fits in cck_pd_lv[][], but rtw8822c_cck_pd_reg[][] only has
registers for nrx 0 and 1. The entry for nrx 3 is empty: register 0,
mask 0. If bw is valid and the level changes, the driver uses that
empty entry. rtw_read32_mask() then shifts by __ffs(0), which is
undefined; in a test that faked only nrx 3, UBSAN reported "shift
exponent 64 is too large".

Return early if bw or nrx is out of range, before either is used as
an index.

Fixes: 479c4ee931a6 ("rtw88: add dynamic cck pd mechanism")
Reported-by: Luka Gejak <luka.gejak@linux.dev>
Closes: https://lore.kernel.org/linux-wireless/f6a7d3272fe7635c6a83698dfab5d85500437ca6@linux.dev/
Cc: stable@vger.kernel.org
Signed-off-by: Zeshan Ahmed Nobin <zeshanahmednobin@gmail.com>
---
v4:
- One patch instead of two: check both values right after they are
  read, as Ping-Ke suggested.
- No code comment, and a shorter commit message.
- Dropped Luka's Suggested-by and Reviewed-by from v3: this code is new.

Tested on an RTL8822CE with Ubuntu 7.0.0-43. A debug-only patch made
one or both reads return 0xffffffff for 30 s. cck_pd_set() runs every
2 s; "stopped" means it returned at the new check. Each faked run also
logged 2 or 3 normal calls before the fake began.

  Test                      Calls  Stopped  Ran with a 3  UBSAN
  healthy chip (60 s)          30        0             0      0
  bw and nrx both faked        17       15             0      0
  only bw faked                16       14             0      0
  only nrx faked               18       16             0      0
  both faked, without fix      18        -            15      4

The nrx part was tested in v3 (Ubuntu 6.8.0-147, bw valid, nrx faked
to 3), without the nrx check. UBSAN then reported, once in the read
and once in the write:

  UBSAN: shift-out-of-bounds in .../rtw88/hci.h:203:22
  shift exponent 64 is too large for 32-bit type 'unsigned int'
  Workqueue: phy0 rtw_watch_dog_work [rtw88_core]
  Call Trace:
   rtw8822c_phy_cck_pd_set.cold+0x15/0x36 [rtw88_8822c]
   rtw_phy_dynamic_mechanism+0x322/0x6e0 [rtw88_core]
   rtw_watch_dog_work+0x1e7/0x290 [rtw88_core]

Not tested: the value 2 (the fake only gives 3), and the USB and SDIO
versions of the 8822c.

An AI assistant (Claude, by Anthropic) helped me read the code, write
the debug-only fake reads and draft this message. I wrote the fix, ran
every test and checked the results myself.

v3: https://lore.kernel.org/all/20261115201826.181662-1-zeshanahmednobin@gmail.com/
v2: https://lore.kernel.org/all/20261108182837.682661-1-zeshanahmednobin@gmail.com/
v1: https://lore.kernel.org/linux-wireless/20261105113415.73184-1-zeshanahmednobin@gmail.com/
---
 drivers/net/wireless/realtek/rtw88/rtw8822c.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/net/wireless/realtek/rtw88/rtw8822c.c b/drivers/net/wireless/realtek/rtw88/rtw8822c.c
index 09315d23e2c4..830dea8169c5 100644
--- a/drivers/net/wireless/realtek/rtw88/rtw8822c.c
+++ b/drivers/net/wireless/realtek/rtw88/rtw8822c.c
@@ -4406,6 +4406,9 @@ static void rtw8822c_phy_cck_pd_set(struct rtw_dev *rtwdev, u8 new_lvl)
 	nrx = (u8)rtw_read32_mask(rtwdev, 0x1a2c, 0x60000);
 	bw = (u8)rtw_read32_mask(rtwdev, 0x9b0, 0xc);
 
+	if (bw > RTW_CHANNEL_WIDTH_40 || nrx > RF_PATH_B)
+		return;
+
 	rtw_dbg(rtwdev, RTW_DBG_PHY, "lv: (%d) -> (%d) bw=%d nr=%d cck_fa_avg=%d\n",
 		dm_info->cck_pd_lv[bw][nrx], new_lvl, bw, nrx,
 		dm_info->cck_fa_avg);

base-commit: 8b29fd95e2d3cb80564eb7cb34248b844422ba1e
-- 
2.43.0


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

* Re: [PATCH rtw-next v4] wifi: rtw88: 8822c: validate bw and nrx read from registers
  2026-11-25 20:37 [PATCH rtw-next v4] wifi: rtw88: 8822c: validate bw and nrx read from registers Zeshan Ahmed Nobin
@ 2026-11-25 21:14 ` Luka Gejak
  0 siblings, 0 replies; 2+ messages in thread
From: Luka Gejak @ 2026-11-25 21:14 UTC (permalink / raw)
  To: Zeshan Ahmed Nobin, linux-wireless; +Cc: Ping-Ke Shih, Kalle Valo, linux-kernel

On November 25, 2026 9:37:53 PM GMT+01:00, Zeshan Ahmed Nobin
<zeshanahmednobin@gmail.com> wrote:
>rtw8822c_phy_cck_pd_set() reads nrx and bw from the chip with
>rtw_read32_mask() and uses them as indexes into dm_info->cck_pd_lv[][]
>and rtw8822c_cck_pd_reg[][].
>
>When the chip stops responding on the PCIe bus, register reads return
>all ones, so bw and nrx both become 3. bw = 3 is past the end of both
>arrays.  When the chip stopped responding on Ubuntu's 7.0.0-43 kernel,
>UBSAN reported:
>
>  UBSAN: array-index-out-of-bounds in .../rtw88/rtw8822c.c:4407:21
>  index 3 is out of range for type 'u8 [2][4]'
>  Workqueue: phy0 rtw_watch_dog_work [rtw88_core]
>  Call Trace:
>   rtw8822c_phy_cck_pd_set+0x33c/0x510 [rtw88_8822c]
>   rtw_phy_dynamic_mechanism+0x310/0x330 [rtw88_core]
>   rtw_watch_dog_work+0x217/0x330 [rtw88_core]
>
>nrx = 3 fits in cck_pd_lv[][], but rtw8822c_cck_pd_reg[][] only has
>registers for nrx 0 and 1. The entry for nrx 3 is empty: register 0,
>mask 0. If bw is valid and the level changes, the driver uses that
>empty entry. rtw_read32_mask() then shifts by __ffs(0), which is
>undefined; in a test that faked only nrx 3, UBSAN reported "shift
>exponent 64 is too large".
>
>Return early if bw or nrx is out of range, before either is used as
>an index.
>
>Fixes: 479c4ee931a6 ("rtw88: add dynamic cck pd mechanism")
>Reported-by: Luka Gejak <luka.gejak@linux.dev>
>Closes: https://lore.kernel.org/linux-wireless/f6a7d3272fe7635c6a83698dfab5d85500437ca6@linux.dev/
>Cc: stable@vger.kernel.org
>Signed-off-by: Zeshan Ahmed Nobin <zeshanahmednobin@gmail.com>
>---

Looks good to me,

Reviewed-by: Luka Gejak <luka.gejak@linux.dev>

Best regards,
Luka Gejak

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

end of thread, other threads:[~2026-11-25 21:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 20:37 [PATCH rtw-next v4] wifi: rtw88: 8822c: validate bw and nrx read from registers Zeshan Ahmed Nobin
2026-11-25 21:14 ` Luka Gejak

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

all inboxes | Powered by JetHome®