ar5523_cmd() stores the caller's reply buffer in ar->tx_cmd.odata and clears it only when the command times out. Every other exit leaves the pointer set after the caller has returned: usb_submit_urb() failure, a command TX URB that completes with an error (ar5523_cmd_tx_cb() sets cmd->res and completes, e.g. -EPIPE when the device halts the command endpoint), and reply errors such as -EOVERFLOW from ar5523_read_reply(). The command RX URB keeps running after such a failure, and ar5523_cmd_rx_cb() copies the payload of any WDCMSG_TARGET_START message, or of any read reply, through cmd->odata as long as it is not NULL. ar5523_start() passes the address of an on-stack variable for WDCMSG_TARGET_START, and when ar5523_start() fails mac80211 does not call ->stop(), so no further command replaces the pointer. A USB device that fails that one command can then write 4 bytes of its choice into the stack of the task that tried to bring the interface up, at any later time and as often as it likes. With KASAN, after that task has exited: BUG: KASAN: vmalloc-out-of-bounds in ar5523_cmd_rx_cb+0x2b4/0x980 Write of size 4 at addr ffff80008c597480 by task swapper/1/0 Call trace: ... __asan_memcpy+0x54/0xa0 ar5523_cmd_rx_cb+0x2b4/0x980 __usb_hcd_giveback_urb+0x350/0x64c usb_hcd_giveback_urb+0x3c8/0x4a4 dummy_timer+0xdd8/0x2d5c ... Clear cmd->odata on every exit of ar5523_cmd(), not only on timeout. The callback tests the pointer and then writes through it from URB completion context, so clearing it alone could still race with a reply that arrives just as the command finishes. Protect the pointer with a spinlock that the callback holds across the test and the copy. Commit b6702a942a06 ("wifi: ar5523: Fix use-after-free on ar5523_cmd() timed out") fixed the timeout exit only. 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. The device completes the probe handshake, so wlan0 appears. A short-lived process brings the interface up; the device accepts the setup commands, then halts its command OUT endpoint, so WDCMSG_TARGET_START fails with -EPIPE and the interface does not come up. After that process has exited, the device sends a 36-byte WDCMSG_TARGET_START message, which is copied into the freed stack. A variant without the STALL answers TARGET_START with a default-path reply whose length word is 8 ("olen too small", -EOVERFLOW) and gives the same report. Bringing the interface up is the only local action needed. Without the failed command, or without the late message, nothing happens. The reproducer is available to maintainers on request. Decoded trace, v7.4-rc2-141-g9d66f26fefac (arm64, QEMU virt, dummy_hcd): BUG: KASAN: vmalloc-out-of-bounds in ar5523_cmd_rx_cb (drivers/net/wireless/ath/ar5523/ar5523.c:160 (discriminator 27)) Write of size 4 at addr ffff80008c597480 by task swapper/1/0 Call trace: __asan_memcpy (mm/kasan/shadow.c:106 (discriminator 1)) ar5523_cmd_rx_cb (drivers/net/wireless/ath/ar5523/ar5523.c:160 (discriminator 27)) __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) ... The buggy address belongs to a vmalloc virtual mapping Without the patch: v7.3.1 5/5 on dummy_hcd and 2/2 on xHCI, the -EOVERFLOW variant 3/3 (1 on xHCI); 9d66f26fefac 4/4 (1 on xHCI) and the -EOVERFLOW variant 1/1. ar5523.c is identical in v7.3.1 and 9d66f26fefac. This patch is written on top of 2/3 (the only nearby code is the else branch 2/3 adds after the wait). Testing of the whole series is in the notes of 2/3: no report in 64 boots on v7.3.1 and 9d66f26fefac. After this patch a late WDCMSG_TARGET_START message takes the existing "Unexpected WDCMSG_TARGET_START reply" branch, which returns without resubmitting the command RX URB, so the driver gets no further command replies until it is re-probed. That is pre-existing behaviour for that branch and not changed here. Thanks, Alexandre Becholey drivers/net/wireless/ath/ar5523/ar5523.c | 27 +++++++++++++++++++++--- drivers/net/wireless/ath/ar5523/ar5523.h | 1 + 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/drivers/net/wireless/ath/ar5523/ar5523.c b/drivers/net/wireless/ath/ar5523/ar5523.c index 51d8d998b07d..9569bd3bc090 100644 --- a/drivers/net/wireless/ath/ar5523/ar5523.c +++ b/drivers/net/wireless/ath/ar5523/ar5523.c @@ -48,6 +48,7 @@ static void ar5523_data_tx_pkt_put(struct ar5523 *ar); static void ar5523_read_reply(struct ar5523 *ar, struct ar5523_cmd_hdr *hdr, struct ar5523_tx_cmd *cmd) { + unsigned long flags; int dlen, olen; __be32 *rp; @@ -75,6 +76,7 @@ static void ar5523_read_reply(struct ar5523 *ar, struct ar5523_cmd_hdr *hdr, } else olen = 0; + spin_lock_irqsave(&cmd->lock, flags); if (cmd->odata) { if (cmd->olen < olen) { ar5523_err(ar, "olen too small %d < %d\n", @@ -87,6 +89,7 @@ static void ar5523_read_reply(struct ar5523 *ar, struct ar5523_cmd_hdr *hdr, cmd->res = 0; } } + spin_unlock_irqrestore(&cmd->lock, flags); out: complete(&cmd->done); @@ -97,6 +100,7 @@ static void ar5523_cmd_rx_cb(struct urb *urb) struct ar5523 *ar = urb->context; struct ar5523_tx_cmd *cmd = &ar->tx_cmd; struct ar5523_cmd_hdr *hdr = ar->rx_cmd_buf; + unsigned long flags; int dlen; u32 code, hdrlen; @@ -156,12 +160,15 @@ static void ar5523_cmd_rx_cb(struct urb *urb) ar5523_err(ar, "Invalid reply to WDCMSG_TARGET_START"); return; } + spin_lock_irqsave(&cmd->lock, flags); if (!cmd->odata) { + spin_unlock_irqrestore(&cmd->lock, flags); ar5523_err(ar, "Unexpected WDCMSG_TARGET_START reply"); return; } memcpy(cmd->odata, hdr + 1, sizeof(u32)); cmd->olen = sizeof(u32); + spin_unlock_irqrestore(&cmd->lock, flags); cmd->res = 0; complete(&cmd->done); break; @@ -254,6 +261,7 @@ static int ar5523_cmd(struct ar5523 *ar, u32 code, const void *idata, { struct ar5523_cmd_hdr *hdr; struct ar5523_tx_cmd *cmd = &ar->tx_cmd; + unsigned long irqflags; int xferlen, error; /* always bulk-out a multiple of 4 bytes */ @@ -270,8 +278,10 @@ static int ar5523_cmd(struct ar5523 *ar, u32 code, const void *idata, if (ilen) memcpy(hdr + 1, idata, ilen); + spin_lock_irqsave(&cmd->lock, irqflags); cmd->odata = odata; cmd->olen = olen; + spin_unlock_irqrestore(&cmd->lock, irqflags); cmd->flags = flags; ar5523_dbg(ar, "do cmd %02x\n", code); @@ -287,12 +297,11 @@ static int ar5523_cmd(struct ar5523 *ar, u32 code, const void *idata, if (error) { ar5523_err(ar, "could not send command 0x%x, error=%d\n", code, error); - return error; + goto out; } if (!wait_for_completion_timeout(&cmd->done, 2 * HZ)) { ar5523_cancel_tx_cmd(ar); - cmd->odata = NULL; ar5523_err(ar, "timeout waiting for command %02x reply\n", code); cmd->res = -ETIMEDOUT; @@ -305,7 +314,18 @@ static int ar5523_cmd(struct ar5523 *ar, u32 code, const void *idata, */ ar5523_cancel_tx_cmd(ar); } - return cmd->res; + error = cmd->res; + +out: + /* + * The caller's output buffer is often on its stack. Make sure a + * late or unsolicited reply from the device can no longer write + * to it once we return, whatever the outcome of the command. + */ + spin_lock_irqsave(&cmd->lock, irqflags); + cmd->odata = NULL; + spin_unlock_irqrestore(&cmd->lock, irqflags); + return error; } static int ar5523_cmd_write(struct ar5523 *ar, u32 code, const void *data, @@ -969,6 +989,7 @@ static int ar5523_alloc_tx_cmd(struct ar5523 *ar) cmd->ar = ar; init_completion(&cmd->done); + spin_lock_init(&cmd->lock); cmd->urb_tx = usb_alloc_urb(0, GFP_KERNEL); if (!cmd->urb_tx) diff --git a/drivers/net/wireless/ath/ar5523/ar5523.h b/drivers/net/wireless/ath/ar5523/ar5523.h index 9a322a65cdb5..9db606332acd 100644 --- a/drivers/net/wireless/ath/ar5523/ar5523.h +++ b/drivers/net/wireless/ath/ar5523/ar5523.h @@ -61,6 +61,7 @@ struct ar5523_tx_cmd { struct ar5523 *ar; struct urb *urb_tx; void *buf_tx; + spinlock_t lock; /* protects odata and olen */ void *odata; int olen; int flags; -- 2.53.0