Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795046766; b=1+hbw4V0yjYX+NJbRbQWmCbZID+HAKjyUkxzU44y0BhRS8uhnhdWBwUZ7T2jRK3rj9AK KzHbm5AHJteTV8YqXIPmXo+Npv4ZfeaHeDrPtYsGNvC4lpVqK5eomlgu4vwRGjFJhJHJf 6i0Y578F5o8lCCYPKedd7c/C/SCCIiIojA7pk/YYw0JggYIiIME6egC6r2CRhgKutDiqY mAT3y1F/GfL4sTOaGmu4U7Wmam8yF4ThMLIWTsTRP5fmnGM3VXcdbwtMJoI8h+U29BP9P k99mdG52Yu1dQ9Tp9JOaj4UMLnMul/vKKybNbWsh+1Tdqxt8QTV4npY6LVNUj1/Lv8w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795046766; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=Saom9JlhkU/8HuBl6ZqBAwn6nQDtc0a70WuhMYlrluo=; b=W/xayHZ/9l6WM/Jf+Xt81/+wIlqL9mLdA9yznwMfJvLAYxIvq42fqynFcsiJ8h8DrMw/ TX7E8lMDR73u4ZnZzZd4BUVlPPG/wL7wk4sOytevXeXAAYG52l7Kh074v/KXXRakpJdZh GU9ZE1mVooM4BTdPkEebX64molUpwIQ8wuPQ4E+K5xvqdF5rM9qU74IWhUGoGRWZnDsO5 9TgVe0rGVhoOJwOQkz+fTM7Z/6asBlM1vkCpMs4/gBKud7AvrpdGN1/VTwDWxdEnHHNTg V9QfaRgBSAGqcQWdakZmUnzstxJMMTWnNOpm2UN+Y8xSK5ofnaxfQHZk26golYTrPnA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b="cT8Xk/k8"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D287B606A320; Thu, 19 Nov 2026 00:06:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=cT8Xk/k8 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C6FE1F000C4; Thu, 19 Nov 2026 00:06:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795046766; bh=Saom9JlhkU/8HuBl6ZqBAwn6nQDtc0a70WuhMYlrluo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=cT8Xk/k8c+oc57gpbjp5DA7h7X4AIk883QXnDHIRXWZETHrqgtXiDIuSKUREYocQa jz2gAbtvqXCmIjxTtZmB7aq9uWq0hKMsvXouExBBADzcWvZUTI+TX3BxxZCa+1/1E7 w17rDFeNFG10tW9v1tPpCs1OFkno5bO4seTmIp4E= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Sinan Turunc , Takashi Iwai , Sasha Levin Subject: [PATCH 6.1 1398/2857] ALSA: usb-audio: Bound packet copies by the PCM buffer size Date: Wed, 18 Nov 2026 15:40:32 +0100 Message-ID: <20261118150251.687792919@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150201.293410965@linuxfoundation.org> References: <20261118150201.293410965@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Sinan Turunc [ Upstream commit 06be918062adc1a5a76dc346803b136b20bdb5ae ] retire_capture_urb() and prepare_playback_urb() assume that one USB packet never carries more data than the whole ALSA ring buffer. Nothing enforces that: the packet size comes from the device (wMaxPacketSize, and curpacksize == maxpacksize when the class-specific endpoint descriptor sets MaxPacketsOnly), while the PCM hardware constraints only require 64-byte periods, two periods and a period time of one data interval. An 8 kHz mono U8 stream on a 128-byte buffer is legal, and a full-speed device can send 1023-byte packets into it. On the first oversized packet hwptr_done is wrapped once and stays beyond buffer_bytes (895 for 1023 bytes into 128). On the next packet the wrap arithmetic in the copy path computes bytes1 = subs->buffer_bytes - oldptr with oldptr > buffer_bytes, so bytes1 wraps to about 4 GB and memcpy() runs off the end of the URB buffer and of the vmalloc'd DMA buffer: BUG: KASAN: slab-out-of-bounds in retire_capture_urb+0x251/0x870 Read of size 4294966529 at addr ffff88800c1e2800 by task vhci_rx/82 and for playback, where copy_to_urb() has the same shape: BUG: KASAN: vmalloc-out-of-bounds in copy_to_urb.isra.0+0x188/0x430 Read of size 4294966529 at addr ffffc9000004537f by task vhci_rx/79 prepare_playback_urb+0x98a/0x1fc0 Truncate an oversized capture packet to the buffer size, and stop filling a playback URB once it holds a whole buffer. With bytes <= buffer_bytes the single wrap of hwptr_done and the two-part memcpy() in both copy paths stay inside the buffer. A device whose packets do not fit the buffer at all was never going to play correctly anyway. Reproduced on 7.3-rc7 with a USB/IP device (dummy_hcd cannot carry isochronous data) advertising a UAC1 endpoint with MaxPacketsOnly and wMaxPacketSize 1023, and a PCM client using period_size 64, periods 2. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Signed-off-by: Sinan Turunc Signed-off-by: Takashi Iwai Link: https://patch.msgid.link/20261026223543.620-2-enscrpt@pm.me [ sashal: adjusted indentation to match the existing loop in prepare_playback_urb() ] Signed-off-by: Sasha Levin --- sound/usb/pcm.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index 17e539b1fdb4c..771ed7e63a55c 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -1248,6 +1248,18 @@ static void retire_capture_urb(struct snd_usb_substream *subs, bytes -= adj; subs->stream_offset_adj -= adj; } + /* + * A packet can be as large as the endpoint's maxpacksize, which + * the device chooses, while the ring buffer can be as small as + * two 64-byte periods. The hwptr_done wrap below handles one + * wrap only, so never take more than one buffer from a packet. + */ + if (unlikely(bytes > subs->buffer_bytes)) { + dev_warn_ratelimited(&subs->dev->dev, + "%u-byte packet exceeds %u-byte buffer, truncating\n", + bytes, subs->buffer_bytes); + bytes = subs->buffer_bytes; + } frames = bytes / stride; if (!subs->txfr_quirk) bytes = frames * stride; @@ -1461,8 +1473,14 @@ static int prepare_playback_urb(struct snd_usb_substream *subs, for (i = 0; i < ctx->packets; i++) { counts = snd_usb_endpoint_next_packet_size(ep, ctx, i, avail); + /* + * Bound by the ring buffer as well as by the URB: + * copy_to_urb() wraps hwptr_done once, so one URB must + * never carry more than the whole buffer. + */ if (counts < 0 || - (frames + counts) * stride > ctx->buffer_size) + (frames + counts) * stride > ctx->buffer_size || + (frames + counts) * stride > subs->buffer_bytes) break; /* set up descriptor */ urb->iso_frame_desc[i].offset = frames * stride; -- 2.53.0