From patchwork Sat Jan 25 23:54:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Foster Snowhill X-Patchwork-Id: 13950548 X-Patchwork-Delegate: kuba@kernel.org Received: from ci74p00im-qukt09081901.me.com (ci74p00im-qukt09081901.me.com [17.57.156.8]) (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 42C9620A5DF for ; Sat, 25 Jan 2025 23:54:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.57.156.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737849298; cv=none; b=XSsmLuZqWH6G5ZKMXgtIfOL5PUcw/dtShw158zImqo/RJXF8FlTMyc44VSMk30vJVdcrRg96f3otupNweed/sZkR+jR3C1F9fSIPudRivbaEt4YKJcOJhS534PNBexzs9ANUK10VeLCk3mhk/+4uX6gKmhyyNBoe3jNNui4FKwo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737849298; c=relaxed/simple; bh=EiuiZJ51Vj6J3PCusN2PI6H+A8Dypz9UpsPv9b2Q+Ik=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pDKFLFkWWPIZ39frldO4CnGPb7zNlB1gBdMaJB3Nvxz+J6+Ay/TqRV0Ka6Lp/FiktieSRZzKCWwKDAdGkkgrvCxjolXRILUoUz2c0Zgszi5kHaKcMpP7PSYb9wxVVf/jNhFiF2et0GbZJB39KV6Ah20c2X0AJ9C2WYUj+4uX9mY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pen.gy; spf=pass smtp.mailfrom=pen.gy; dkim=pass (2048-bit key) header.d=pen.gy header.i=@pen.gy header.b=FopAVTQG; arc=none smtp.client-ip=17.57.156.8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=pen.gy Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pen.gy Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pen.gy header.i=@pen.gy header.b="FopAVTQG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pen.gy; s=sig1; bh=jcKrRjAZq8ukbKL7X+NvSLcPPQOON6h+vZjBfyWQtSc=; h=From:To:Subject:Date:Message-ID:MIME-Version:x-icloud-hme; b=FopAVTQGsCc8LjkrEg19XwAkXWP+Wct9szUt2X7RcCea+Fk8u9zO8hqAZaqDQ5Gys bPYX0R9HKvdNehqJ0cTGk5M/8ERurjwzv57RS0qGTPNZnmVgtAqfx4ElngNmfJp116 7fCMuXKs7dOjVWCyE0tbfGIzokzOoTcZj+Y/pHMqEPWOhTqzftuyA90L2ZZimzuppd bkWG+E22rNb1uGkXS3+GXkIECGgK7lkdlayjxSq1nktsiafKAUTet1RD69OL8S7l5y JpVid5vlW8nHsGgeHns+vau8ZByAJvrHowwxMUtG1BCtSvyi0LbRI0tWMwhtNyLQXD VubkTJPzatiew== Received: from fossa.se1.pen.gy (ci77p00im-dlb-asmtp-mailmevip.me.com [17.57.156.26]) by ci74p00im-qukt09081901.me.com (Postfix) with ESMTPSA id 467165AC00A8; Sat, 25 Jan 2025 23:54:51 +0000 (UTC) From: Foster Snowhill To: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni Cc: Georgi Valkov , Simon Horman , Oliver Neukum , netdev@vger.kernel.org, linux-usb@vger.kernel.org Subject: [PATCH net v5 4/7] usbnet: ipheth: refactor NCM datagram loop Date: Sun, 26 Jan 2025 00:54:06 +0100 Message-ID: <20250125235409.3106594-5-forst@pen.gy> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20250125235409.3106594-1-forst@pen.gy> References: <20250125235409.3106594-1-forst@pen.gy> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: RZtuZQRDg46vIDzefA64EMPPBKGe_1Jy X-Proofpoint-GUID: RZtuZQRDg46vIDzefA64EMPPBKGe_1Jy X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2025-01-25_11,2025-01-23_01,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=759 spamscore=0 suspectscore=0 malwarescore=0 phishscore=0 mlxscore=0 clxscore=1030 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2501250183 X-Patchwork-Delegate: kuba@kernel.org Introduce an rx_error label to reduce repetitions in the header signature checks. Store wDatagramIndex and wDatagramLength after endianness conversion to avoid repeated le16_to_cpu() calls. Rewrite the loop to return on a null trailing DPE, which is required by the CDC NCM spec. In case it is missing, fall through to rx_error. This change does not fix any particular issue. Its purpose is to simplify a subsequent commit that fixes a potential OoB read by limiting the maximum amount of processed DPEs. Cc: stable@vger.kernel.org # 6.5.x Signed-off-by: Foster Snowhill Reviewed-by: Jakub Kicinski --- v5: No code changes. Removed stray "Fix an out-of-bounds DPE read..." paragraph from commit msg. Added Cc to stable and Reviewed-by Jakub tags. v4: https://lore.kernel.org/netdev/20250105010121.12546-6-forst@pen.gy/ Split from "usbnet: ipheth: refactor NCM datagram loop, fix DPE OoB read" in v3. This commit is responsible for the code refactor, while keeping the behaviour the same. v3: https://lore.kernel.org/netdev/20241123235432.821220-5-forst@pen.gy/ Split out from a monolithic patch in v2 as an atomic change. v2: https://lore.kernel.org/netdev/20240912211817.1707844-1-forst@pen.gy/ No code changes. Update commit message to further clarify that `ipheth` is not and does not aim to be a complete or spec-compliant CDC NCM implementation. v1: https://lore.kernel.org/netdev/20240907230108.978355-1-forst@pen.gy/ --- drivers/net/usb/ipheth.c | 42 ++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/drivers/net/usb/ipheth.c b/drivers/net/usb/ipheth.c index c385623596d2..069979e2bb6e 100644 --- a/drivers/net/usb/ipheth.c +++ b/drivers/net/usb/ipheth.c @@ -213,9 +213,9 @@ static int ipheth_rcvbulk_callback_ncm(struct urb *urb) struct usb_cdc_ncm_ndp16 *ncm0; struct usb_cdc_ncm_dpe16 *dpe; struct ipheth_device *dev; + u16 dg_idx, dg_len; int retval = -EINVAL; char *buf; - int len; dev = urb->context; @@ -227,39 +227,43 @@ static int ipheth_rcvbulk_callback_ncm(struct urb *urb) ncmh = urb->transfer_buffer; if (ncmh->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN) || /* On iOS, NDP16 directly follows NTH16 */ - ncmh->wNdpIndex != cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16))) { - dev->net->stats.rx_errors++; - return retval; - } + ncmh->wNdpIndex != cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16))) + goto rx_error; ncm0 = urb->transfer_buffer + sizeof(struct usb_cdc_ncm_nth16); - if (ncm0->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)) { - dev->net->stats.rx_errors++; - return retval; - } + if (ncm0->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN)) + goto rx_error; dpe = ncm0->dpe16; - while (le16_to_cpu(dpe->wDatagramIndex) != 0 && - le16_to_cpu(dpe->wDatagramLength) != 0) { - if (le16_to_cpu(dpe->wDatagramIndex) < IPHETH_NCM_HEADER_SIZE || - le16_to_cpu(dpe->wDatagramIndex) >= urb->actual_length || - le16_to_cpu(dpe->wDatagramLength) > urb->actual_length - - le16_to_cpu(dpe->wDatagramIndex)) { + while (true) { + dg_idx = le16_to_cpu(dpe->wDatagramIndex); + dg_len = le16_to_cpu(dpe->wDatagramLength); + + /* Null DPE must be present after last datagram pointer entry + * (3.3.1 USB CDC NCM spec v1.0) + */ + if (dg_idx == 0 && dg_len == 0) + return 0; + + if (dg_idx < IPHETH_NCM_HEADER_SIZE || + dg_idx >= urb->actual_length || + dg_len > urb->actual_length - dg_idx) { dev->net->stats.rx_length_errors++; return retval; } - buf = urb->transfer_buffer + le16_to_cpu(dpe->wDatagramIndex); - len = le16_to_cpu(dpe->wDatagramLength); + buf = urb->transfer_buffer + dg_idx; - retval = ipheth_consume_skb(buf, len, dev); + retval = ipheth_consume_skb(buf, dg_len, dev); if (retval != 0) return retval; dpe++; } - return 0; +rx_error: + dev->net->stats.rx_errors++; + return retval; } static void ipheth_rcvbulk_callback(struct urb *urb)