ar5523_cmd() submits the command URB and waits on cmd->done. For a read command, cmd->done is completed by the reply from the device (ar5523_read_reply() and the WDCMSG_DEVICE_AVAIL/WDCMSG_TARGET_START cases of ar5523_cmd_rx_cb()), not by the TX URB. Nothing checks that the TX URB has finished, and the completion is never reinitialized, so a surplus complete() is consumed by the next command. A device that does not take the command on its bulk OUT endpoint but sends a reply anyway therefore makes ar5523_cmd() return with cmd->urb_tx still queued. If the reply fails the command, for example with "olen too small" (-EOVERFLOW), ar5523_probe() unwinds and ar5523_free_tx_cmd() frees the command buffer and the URB, then ieee80211_free_hw() frees struct ar5523, which holds the URB context. When the URB completes later (the device takes the command, or is unplugged), the host controller reads the freed buffer: BUG: KASAN: slab-use-after-free in dummy_timer+0x154c/0x2d5c Read of size 36 at addr ffff000012a04800 by task swapper/0/0 ... Freed by task 12: ... usb_free_coherent+0x70/0x90 ar5523_probe+0xa88/0x2c78 and ar5523_cmd_tx_cb() then uses the freed struct ar5523 (with init_on_free=1 this oopses at ar5523_cmd_tx_cb+0x150/0x294). With an interface that is up, one unsolicited reply while no command is pending leaves cmd->done completed. The next write command returns before its URB has completed and the command after it resubmits the active URB ("URB ... submitted while active" WARNING in usb_submit_urb()); the driver then stays one completion ahead. Commit b6702a942a06 ("wifi: ar5523: Fix use-after-free on ar5523_cmd() timed out") fixed this use-after-free for the timeout case only. Reinitialize cmd->done before submitting a command, and kill the TX URB also when the wait ended with a completion. usb_kill_urb() returns at once if the URB has already been given back, so the normal command flow is unchanged. 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@vger.kernel.org Signed-off-by: Alexandre Becholey --- Notes for reviewers (not part of the commit message): Trigger: an AR5523 device emulated with raw-gadget behind dummy_hcd (arm64, QEMU virt), also on QEMU's xHCI. At plug-in the device answers WDCMSG_HOST_AVAILABLE, then NAKs the next command (TARGET_GET_STATUS with a 4-byte reply buffer) on bulk OUT while sending a 44-byte reply with length word 8 ("olen too small 4 < 8"). Probe fails with -EOVERFLOW while the command URB is still queued; about 6 s later the device accepts the stale command or is unplugged. No local action is needed. With the command accepted first and the same reply sent afterwards, probe fails the same way and nothing else happens. The reproducer is available to maintainers on request. Decoded trace, v7.4-rc2-141-g9d66f26fefac (arm64, QEMU virt, dummy_hcd): BUG: KASAN: slab-use-after-free in dummy_timer (drivers/usb/gadget/udc/dummy_hcd.c:1413 (discriminator 27) drivers/usb/gadget/udc/dummy_hcd.c:1514 (discriminator 27) drivers/usb/gadget/udc/dummy_hcd.c:2035 (discriminator 27)) Read of size 36 at addr ffff000012a04800 by task swapper/0/0 Call trace: __asan_memcpy (mm/kasan/shadow.c:105 (discriminator 1)) dummy_timer (drivers/usb/gadget/udc/dummy_hcd.c:1413 (discriminator 27) drivers/usb/gadget/udc/dummy_hcd.c:1514 (discriminator 27) drivers/usb/gadget/udc/dummy_hcd.c:2035 (discriminator 27)) __hrtimer_run_queues (kernel/time/hrtimer.c:2079 kernel/time/hrtimer.c:2136) ... Allocated by task 12: __kmalloc_noprof (include/linux/kasan.h:263 mm/slub.c:5531 mm/slub.c:5556) hcd_buffer_alloc (include/linux/slab.h:995 drivers/usb/core/buffer.c:134) usb_alloc_coherent (drivers/usb/core/usb.c:1011) ar5523_probe (drivers/net/wireless/ath/ar5523/ar5523.c:962 drivers/net/wireless/ath/ar5523/ar5523.c:1666) usb_probe_interface (drivers/usb/core/driver.c:399) ... Freed by task 12: kfree (include/linux/kasan.h:235 mm/slub.c:2780 mm/slub.c:6630 mm/slub.c:6923) hcd_buffer_free (drivers/usb/core/buffer.c:163) usb_free_coherent (drivers/usb/core/usb.c:1033) ar5523_probe (drivers/net/wireless/ath/ar5523/ar5523.c:947 drivers/net/wireless/ath/ar5523/ar5523.c:1737) ... On QEMU's xHCI the controller buffer is not tracked by KASAN, and the report comes from the completion handler itself, on v7.3.1: "BUG: KASAN: use-after-free in ar5523_cmd_tx_cb+0x1dc/0x294", a 4-byte read of cmd->flags from the freed struct ar5523; with init_on_free=1 the same path reads cmd->ar as NULL and oopses (dummy_hcd on v7.3.1 and 9d66f26fefac, xHCI on 9d66f26fefac). ar5523.c is identical in v7.3.1 and 9d66f26fefac. Without the patch: v7.3.1 5/5 on dummy_hcd, 2/2 with init_on_free, 2 of 3 on xHCI; 9d66f26fefac 4/4 (1 on xHCI). The stray-reply case ("URB submitted while active" from ar5523_cmd()) also hit in 3 of 3 boots on v7.3.1 (an unsolicited reply after the interface is up, a duplicated read reply, a reply to a write command). Testing: this series (1/3 to 3/3) applied on v7.3.1 and on 9d66f26fefac, KASAN, UBSAN, lockdep, emulated device: without the series 8 of 8 trigger boots for the bugs fixed by 2/3 and 3/3 gave a report; with it there was no report in 64 boots (v7.3.1 46, 9d66f26fefac 18; 10 on xHCI), and a well-behaved device still enumerates and brings the interface up. Probe still fails for the bad device above, now with -ENOENT instead of -EOVERFLOW: killing the queued URB runs ar5523_cmd_tx_cb() with -ENOENT, which logs "Failed to TX command. Status = -2" and overwrites cmd->res. Only a misbehaving device gets there; reading cmd->res before the kill would keep the reply's error if you prefer that. Thanks, Alexandre Becholey drivers/net/wireless/ath/ar5523/ar5523.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/net/wireless/ath/ar5523/ar5523.c b/drivers/net/wireless/ath/ar5523/ar5523.c index 734f0561cf73..51d8d998b07d 100644 --- a/drivers/net/wireless/ath/ar5523/ar5523.c +++ b/drivers/net/wireless/ath/ar5523/ar5523.c @@ -280,6 +280,9 @@ static int ar5523_cmd(struct ar5523 *ar, u32 code, const void *idata, cmd->buf_tx, xferlen, ar5523_cmd_tx_cb, cmd); cmd->urb_tx->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; + /* drop completions left over from earlier commands or stray replies */ + reinit_completion(&cmd->done); + error = usb_submit_urb(cmd->urb_tx, GFP_KERNEL); if (error) { ar5523_err(ar, "could not send command 0x%x, error=%d\n", @@ -293,6 +296,14 @@ static int ar5523_cmd(struct ar5523 *ar, u32 code, const void *idata, ar5523_err(ar, "timeout waiting for command %02x reply\n", code); cmd->res = -ETIMEDOUT; + } else { + /* + * A reply from the device ends the wait even if the device + * has not taken the command yet. Make sure the TX URB is not + * left in flight: it must not be resubmitted by the next + * command or freed while it is still active. + */ + ar5523_cancel_tx_cmd(ar); } return cmd->res; } -- 2.53.0