[PATCH 1/3] wifi: ar5523: don't WARN on a short command reply
From: Alexandre Becholey
Date: Mon Nov 16 2026 - 02:53:36 EST
ar5523_read_reply() computes the reply payload length from the header
length sent by the device:
dlen = be32_to_cpu(hdr->len) - sizeof(*hdr);
ar5523_cmd_rx_cb() only checks that the USB transfer carries at least a
full header; hdr->len itself is not validated. A device that sends a
reply of at least 32 bytes with hdr->len < 32 (or with bit 31 set) makes
dlen negative and hits WARN_ON(1). This is reachable during probe, from
the first read command (WDCMSG_HOST_AVAILABLE), and also later from an
unsolicited reply once the interface is up. With panic_on_warn set it
panics the machine. With an emulated device on
v7.4-rc2-141-g9d66f26fefac (arm64):
[ 2.565535] WARNING: drivers/net/wireless/ath/ar5523/ar5523.c:57 at ar5523_cmd_rx_cb+0x730/0x980, CPU#0: swapper/0/0
[ 2.573914] ar5523_cmd_rx_cb+0x730/0x980 (P)
The branch also completes the command without setting cmd->res, so the
caller gets the result of the previous command (normally 0) and goes on
with an output buffer that was never written. For example, a corrupted
GET_STATUS(ST_MAC_ADDR) reply lets probe succeed and registers a wiphy
with the all-zero MAC address read from the unwritten stack buffer.
A bad length from the device is not a kernel bug. Report it with
ar5523_err() and fail the command with -EPROTO, like the existing
"olen too small" case in the same function.
The bug was found, and the reproducer and this fix were written, by AI
agents (Claude Mythos and Claude Opus models). The analysis and the
patch were reviewed by a human.
Fixes: b7d572e1871d ("ar5523: Add new driver")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Alexandre Becholey <alex@xxxxxxxxxxx>
---
Notes for reviewers (not part of the commit message):
Trigger: an AR5523 device emulated with raw-gadget behind dummy_hcd
(arm64, QEMU virt), also seen with QEMU's xHCI. The device answers the
WDCMSG_HOST_AVAILABLE read command during probe with a 36-byte reply
whose header length is 16. Other variants that hit the same WARN: a
header length of 31, a header length of 0xffffffff in the
GET_STATUS(ST_MAC_ADDR) reply, and an unsolicited reply with header
length 16 after the interface has been brought up. No local action is
needed for the probe case. The reproducer is available to maintainers
on request.
Decoded trace, torvalds/master 9d66f26fefac (arm64, QEMU virt):
WARNING: drivers/net/wireless/ath/ar5523/ar5523.c:57 at ar5523_cmd_rx_cb+0x730/0x980, CPU#0: swapper/0/0
CPU: 0 UID: 0 PID: 0 Comm: swapper/0 Not tainted 7.4.0-rc2-00141-g9d66f26fefac #1 PREEMPT(full)
pc : ar5523_cmd_rx_cb (drivers/net/wireless/ath/ar5523/ar5523.c:57 (discriminator 2) drivers/net/wireless/ath/ar5523/ar5523.c:125 (discriminator 2))
Call trace:
ar5523_cmd_rx_cb (drivers/net/wireless/ath/ar5523/ar5523.c:57 (discriminator 2) drivers/net/wireless/ath/ar5523/ar5523.c:125 (discriminator 2)) (P)
__usb_hcd_giveback_urb (drivers/usb/core/hcd.c:1657)
usb_hcd_giveback_urb (drivers/usb/core/hcd.c:1741)
dummy_timer (drivers/usb/gadget/udc/dummy_hcd.c:2051 (discriminator 1))
__hrtimer_run_queues (kernel/time/hrtimer.c:2079 kernel/time/hrtimer.c:2136)
hrtimer_run_softirq (kernel/time/hrtimer.c:2153)
handle_softirqs (kernel/softirq.c:669)
Kernel panic - not syncing: kernel: panic_on_warn set ...
ar5523.c is identical in v7.3.1 and 9d66f26fefac; the same WARN
appears on both and the patch applies unchanged to both.
Testing (KASAN, UBSAN, lockdep, emulated device): without the patch,
6 of 6 boots with a bad reply length hit the WARN (v7.3.1 5, one of
them on xHCI; 9d66f26fefac 1). With the patch, the ar5523.c:57 WARN
appears in none of 35 boots (v7.3.1 27, 9d66f26fefac 8; 5 of them on
xHCI). Every bad length is reported as "invalid reply length N" and the
command fails; probe then fails cleanly with -EPROTO. A header length
of exactly 32 (empty payload) is still accepted, and a device with a
valid handshake still probes, reports its MAC address and the interface
comes up.
Not fixed here, but by patch 2/3 of this series: any unsolicited
reply carrying the command id, even with a valid length, completes
cmd->done although no read command is pending. The next command then
returns early and the one after that resubmits a URB that is still
active ("URB submitted while active" WARN in usb_submit_urb() from
ar5523_cmd()). This happens with and without this patch; with this
patch alone an unsolicited reply with a bad length ends in that same
state instead of the ar5523.c:57 WARN.
Related: Weiming Shi's "wifi: ar5523: reject negative reply lengths"
(2026-08-31, not merged,
https://lore.kernel.org/all/20260831191502.3321906-1-bestswngs@xxxxxxxxx/)
fixes a different bug, a negative olen word in the reply payload, a few
lines further down in ar5523_read_reply(). None of the three patches
here depends on it; 1/3 and 2/3 apply with or without it. 3/3 and his
patch overlap in one context line: with this series applied first, his
patch still applies with git am -3. If his patch goes in first, 3/3
needs a one-line rebase (the check becomes
"if (olen < 0 || cmd->olen < olen) {" inside the new locked section),
and I can send a rebased version on request.
Thanks,
Alexandre Becholey
drivers/net/wireless/ath/ar5523/ar5523.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/net/wireless/ath/ar5523/ar5523.c b/drivers/net/wireless/ath/ar5523/ar5523.c
index 66ac396858a5..734f0561cf73 100644
--- a/drivers/net/wireless/ath/ar5523/ar5523.c
+++ b/drivers/net/wireless/ath/ar5523/ar5523.c
@@ -54,7 +54,10 @@ static void ar5523_read_reply(struct ar5523 *ar, struct ar5523_cmd_hdr *hdr,
dlen = be32_to_cpu(hdr->len) - sizeof(*hdr);
if (dlen < 0) {
- WARN_ON(1);
+ ar5523_err(ar, "invalid reply length %u\n",
+ be32_to_cpu(hdr->len));
+ cmd->olen = 0;
+ cmd->res = -EPROTO;
goto out;
}
--
2.53.0