From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3310117A2E8; Mon, 10 Mar 2025 18:00:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741629658; cv=none; b=Af+BGS5TN7/HQALicTq1Is41DB5sj/ODUaNIYOhUCOAkqsmP5vSpzTX28AWclicszhGYk6V0Ttze+5Xn9bO40bP4b31rVCpdEh6uZlF/CCmZFp55jHX/vhUUMqMbiaXMJBJHhn/sqDUta6P1BCZvfEgCWIntmBiQcn0w2fmMLxo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741629658; c=relaxed/simple; bh=t0be2u4hB7CxQRFPZLuRaCFGy0Ml7TKT2MMXqWo4t8g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UD9Pioc3pQdOg5659SxfyxkkhAu7B8MJseWTi2iaQqa7L+twA0uq6wG6sYeaoe5qUb8iWDF2q6CIXwjnBqo2U0TMiSmfGlksuYr4WWezSALaGQVsiMhIW9/T5NLVVx3nctwcMaFJgqNHufrrpfCf4eblbTmTO2vDS5zB/4FQKIw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=YLUW3Xhn; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="YLUW3Xhn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC8C9C4CEE5; Mon, 10 Mar 2025 18:00:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1741629658; bh=t0be2u4hB7CxQRFPZLuRaCFGy0Ml7TKT2MMXqWo4t8g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YLUW3Xhng6ayxDwpjPMzBahklK0oyPWfeKI7YW31jvVKGi3c2R+vnmCex4E/OBeqw UYDJ+seRZDv7bYkjR3lIe2uB1A+/MnWXpP1GOv/fBs33n2ip7n0vGmu9kKX6ezUHXb +MSVI1Rc9Qq5NFchHuL+fqKQilcduKA+dgjiwDs0= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, stable , Jann Horn Subject: [PATCH 5.15 370/620] usb: cdc-acm: Check control transfer buffer size before access Date: Mon, 10 Mar 2025 18:03:36 +0100 Message-ID: <20250310170600.207416657@linuxfoundation.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250310170545.553361750@linuxfoundation.org> References: <20250310170545.553361750@linuxfoundation.org> User-Agent: quilt/0.68 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jann Horn commit e563b01208f4d1f609bcab13333b6c0e24ce6a01 upstream. If the first fragment is shorter than struct usb_cdc_notification, we can't calculate an expected_size. Log an error and discard the notification instead of reading lengths from memory outside the received data, which can lead to memory corruption when the expected_size decreases between fragments, causing `expected_size - acm->nb_index` to wrap. This issue has been present since the beginning of git history; however, it only leads to memory corruption since commit ea2583529cd1 ("cdc-acm: reassemble fragmented notifications"). A mitigating factor is that acm_ctrl_irq() can only execute after userspace has opened /dev/ttyACM*; but if ModemManager is running, ModemManager will do that automatically depending on the USB device's vendor/product IDs and its other interfaces. Cc: stable Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Jann Horn Signed-off-by: Greg Kroah-Hartman --- drivers/usb/class/cdc-acm.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) --- a/drivers/usb/class/cdc-acm.c +++ b/drivers/usb/class/cdc-acm.c @@ -360,7 +360,7 @@ static void acm_process_notification(str static void acm_ctrl_irq(struct urb *urb) { struct acm *acm = urb->context; - struct usb_cdc_notification *dr = urb->transfer_buffer; + struct usb_cdc_notification *dr; unsigned int current_size = urb->actual_length; unsigned int expected_size, copy_size, alloc_size; int retval; @@ -387,9 +387,20 @@ static void acm_ctrl_irq(struct urb *urb usb_mark_last_busy(acm->dev); - if (acm->nb_index) + if (acm->nb_index == 0) { + /* + * The first chunk of a message must contain at least the + * notification header with the length field, otherwise we + * can't get an expected_size. + */ + if (current_size < sizeof(struct usb_cdc_notification)) { + dev_dbg(&acm->control->dev, "urb too short\n"); + goto exit; + } + dr = urb->transfer_buffer; + } else { dr = (struct usb_cdc_notification *)acm->notification_buffer; - + } /* size = notification-header + (optional) data */ expected_size = sizeof(struct usb_cdc_notification) + le16_to_cpu(dr->wLength);