From patchwork Sat Jan 25 23:54:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Foster Snowhill X-Patchwork-Id: 13950545 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 01C222066E5 for ; Sat, 25 Jan 2025 23:54:43 +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=1737849285; cv=none; b=uYLuB4hQbN+xink1fG6ukMh2rGCSG9uGFvr+yFwy4PJYUd4imdWOAma5ORi71JjXuuqDL/+TZnOnC+vBKcXZf3hl9jwBqfZ8JnTKKix0cidoteuA2AsZLf+zyFnTN8+jg22MnYHq1mtjZgsNLyG4rz4gjB0lQ4CX27yk+gEou/Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737849285; c=relaxed/simple; bh=bdjryjI9zyF2uXSXIVjyqdeV7IohiaYXfLNPmiiuY6s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BNTq/ay04qEVPm3mU5NZZV/qI0ZOi0k+XjPc0rqY8bSLwwEHEhejrYe2VGjMfgDOO2vL9bkQhdDiorJMySvyfYcDXdz7C5VKAYbjppwxsp64r4uMuo1jdkvm6mVMQvH+DJAk1yp4NcDwIBzgMuvxZl2iKJaPizWs5evOT7slnk0= 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=qKS3FLIN; 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="qKS3FLIN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pen.gy; s=sig1; bh=NO3ktCMh3wSfaToy35Cacua85eQzHdak8/MTQl8j+kk=; h=From:To:Subject:Date:Message-ID:MIME-Version:x-icloud-hme; b=qKS3FLINsGH9Pm3Prpxv8ksWC0begBeZRjiFW9j8Cm7tCW92mkXeSWt6MuJb/EcKZ VTjvkksFZphgvb4TxLbN8DD5LAeJDvAcPxN1wqVcgt96P3QoNTsl+A9mVkKstHYW/8 3/9i8NoM61wZwvCTmPCXrPjZvIMdafW9Ld2b96Aln3U0iQ7Df20xJoY1RKwjCwqp/L cLphiX6+xoBMIY/oUA8Unaj9V4qcUu4/bg/weJrpAv+OaSQSU6dZ5JbyokGWQuVQ+5 bNBE2IbXxcb8M6ckVe2eXzhOCVa7ABUQNKcRbfLb1V70YMWG1yg0eNINwv+ow/htuB xAZK4vY5cH8yQ== 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 CEEB65AC0107; Sat, 25 Jan 2025 23:54:39 +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 1/7] usbnet: ipheth: fix possible overflow in DPE length check Date: Sun, 26 Jan 2025 00:54:03 +0100 Message-ID: <20250125235409.3106594-2-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: UGb2UpjwBRa72tpLU-r3jJT9klJun5UK X-Proofpoint-GUID: UGb2UpjwBRa72tpLU-r3jJT9klJun5UK 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=647 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 Originally, it was possible for the DPE length check to overflow if wDatagramIndex + wDatagramLength > U16_MAX. This could lead to an OoB read. Move the wDatagramIndex term to the other side of the inequality. An existing condition ensures that wDatagramIndex < urb->actual_length. Fixes: a2d274c62e44 ("usbnet: ipheth: add CDC NCM support") Cc: stable@vger.kernel.org Signed-off-by: Foster Snowhill Reviewed-by: Jakub Kicinski --- v5: No code changes. Added Cc to stable and Reviewed-by Jakub tags. v4: https://lore.kernel.org/netdev/20250105010121.12546-3-forst@pen.gy/ No change since v3. v3: https://lore.kernel.org/netdev/20241123235432.821220-2-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 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/net/usb/ipheth.c b/drivers/net/usb/ipheth.c index 46afb95ffabe..45daae234cb8 100644 --- a/drivers/net/usb/ipheth.c +++ b/drivers/net/usb/ipheth.c @@ -243,8 +243,8 @@ static int ipheth_rcvbulk_callback_ncm(struct urb *urb) while (le16_to_cpu(dpe->wDatagramIndex) != 0 && le16_to_cpu(dpe->wDatagramLength) != 0) { if (le16_to_cpu(dpe->wDatagramIndex) >= urb->actual_length || - le16_to_cpu(dpe->wDatagramIndex) + - le16_to_cpu(dpe->wDatagramLength) > urb->actual_length) { + le16_to_cpu(dpe->wDatagramLength) > urb->actual_length - + le16_to_cpu(dpe->wDatagramIndex)) { dev->net->stats.rx_length_errors++; return retval; } From patchwork Sat Jan 25 23:54:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Foster Snowhill X-Patchwork-Id: 13950546 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 029EB2066E5 for ; Sat, 25 Jan 2025 23:54:48 +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=1737849289; cv=none; b=hkztBO8xNt9meZRHVY0kEy3vu9ZH9UTh4ELHemLvzxbObVjdl059G9b6GJo5rdC6myqlPQR7K/JW9S9sHPmx0POnRMsFZDrm+XeDm9tnItP01cyoAZbmx8P8/NDGc0aj2TvajJwnOGPJ8hPEnWBC20KxSQlyvRGRcz2n4GSPTwM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737849289; c=relaxed/simple; bh=ByQoRGwpNFwth3ZZx17pvWQtfSy2ycPI/iHTO8xVcfg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TuQVXl9a55lJJgTna6lOvZxfDhjluvreD88fb4KPl++kyy1MxXLAzCitIHLvGmXFVN1Vt4Fs6KBCJ2nSSl/gACE/Lerd/SjfaGuZcS1VAMTJCtoKNGjhfkngO305UjCg/JKWzsCrqkqKGZeDlyhSwlUS2qxJ4Kd+JiKQybskYOs= 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=Wk/bnWmX; 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="Wk/bnWmX" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pen.gy; s=sig1; bh=hxAqtiK/pZshS4jUbncXOLS1RWrFRFfUAspaw326iwU=; h=From:To:Subject:Date:Message-ID:MIME-Version:x-icloud-hme; b=Wk/bnWmXWFwBWJsgtFCIr0KZWh8NBpI9szMr+qsHHZa4rj4lBp2fcn4K//I2u3GCZ S/XqQkNK51j9kHR2vY3oFJxsVcKQGdldvhpqVHKN9UBB9CtpJBSQ5eIwRM7pjzbvkl mcUcCzaRwpfrHpRwlyiSR4+6Gjy9cUlS67/VjFIOMR52pe08o9PL8BxfoYGpbKo4Z7 OsTaH4Y4swYcDT3jg3kF06wEquyYCKLieFlsSjU2u9ycWnvrom8aDG67gbc7y/287N i9pV6Av89Twj/h9asriKr5fIWAQ7NzAEYT+P1LXNB5sYu6Q1fJbDtRWRjlLrsM6yxu KpOIbAJy1iDKQ== 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 EC6605AC016E; Sat, 25 Jan 2025 23:54:43 +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 2/7] usbnet: ipheth: check that DPE points past NCM header Date: Sun, 26 Jan 2025 00:54:04 +0100 Message-ID: <20250125235409.3106594-3-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: QSFwQVc-PMvtKRSKL4UX3Ei0qjbiNcJl X-Proofpoint-GUID: QSFwQVc-PMvtKRSKL4UX3Ei0qjbiNcJl 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=455 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 By definition, a DPE points at the start of a network frame/datagram. Thus it makes no sense for it to point at anything that's part of the NCM header. It is not a security issue, but merely an indication of a malformed DPE. Enforce that all DPEs point at the data portion of the URB, past the NCM header. Fixes: a2d274c62e44 ("usbnet: ipheth: add CDC NCM support") Cc: stable@vger.kernel.org Signed-off-by: Foster Snowhill Reviewed-by: Jakub Kicinski --- v5: No code changes. Added Cc to stable and Reviewed-by Jakub tags. v4: https://lore.kernel.org/netdev/20250105010121.12546-4-forst@pen.gy/ No change since v3. v3: https://lore.kernel.org/netdev/20241123235432.821220-3-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 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/net/usb/ipheth.c b/drivers/net/usb/ipheth.c index 45daae234cb8..1ff5f7076ad5 100644 --- a/drivers/net/usb/ipheth.c +++ b/drivers/net/usb/ipheth.c @@ -242,7 +242,8 @@ static int ipheth_rcvbulk_callback_ncm(struct urb *urb) dpe = ncm0->dpe16; while (le16_to_cpu(dpe->wDatagramIndex) != 0 && le16_to_cpu(dpe->wDatagramLength) != 0) { - if (le16_to_cpu(dpe->wDatagramIndex) >= urb->actual_length || + 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)) { dev->net->stats.rx_length_errors++; From patchwork Sat Jan 25 23:54:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Foster Snowhill X-Patchwork-Id: 13950547 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 43CCF2066E3 for ; Sat, 25 Jan 2025 23:54:52 +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=1737849294; cv=none; b=CKnq1azBMhJd8KO3BrJN+rSYamZjvHDObUWBUEWHTCfWn+/3PG32C5s7maMiXMBehVI11pdq0H1QD1BhwSfaHSQXRdn+Z2RbbsqcCcuzzYW+IN+HSIvEovNbZjlOC0EZw18dk03wHuwOJB/9vYzJvgQqFZTGWdd2YSXHKtRWbdc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737849294; c=relaxed/simple; bh=yAezLCf3sL/xyEXXHmjuMG8Vmf3ZU7WdJSdoJuy2RT8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RJQe/k580e1Jr3r7N0Lv8IFeld0EHLmQpquR0Y7iieVj9kVCwQQNkGZ9mHQHap6a6LreNuI8OwJfkNv6Oni+9t6nMFdLV4mrcQLhJ8efO2Xfq9IBi7Uq6RAOdA9JVneXS4Imoqh2b8+NvufEbyc7AsaNUBzcSzjq6jrhRogKJQE= 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=kAiGWKpc; 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="kAiGWKpc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pen.gy; s=sig1; bh=ROb1MbEkJEsKoBRZN5KIVcKLJLOWT5N4PgijIhXcNCc=; h=From:To:Subject:Date:Message-ID:MIME-Version:x-icloud-hme; b=kAiGWKpcJhgvI2BUcJnSGkYd1W7If7dLEJDuq/zX0HdRAPJQupHBxmCHk3P0ot2rh Yn/UR1tlM+Q/6RuOi6cl9tT5qc0XBL7UdaHMT2fCFUkozkH2UnxxRDgwnOPgej1qG3 DLwt+SA/je8Sh+qnvJxu+BNEPTl5Cb18TNmgG7RzMTgY1pZuxZRi3FOe4iMAt5cSE9 sagsBAHE2kqqFxKYSfM1X0nhlTndAhOEQ3frjzxCsKlZQYrp+2FCI2fh1zCetAL9tm dy7egkitADCoCjIpSeXb8XAFaKdYk/js9ye6JcDX7c0nGFdR+gDPjU5QPNjvsoCCxu aBVwnd80FuMvg== 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 419725AC0107; Sat, 25 Jan 2025 23:54:47 +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 3/7] usbnet: ipheth: use static NDP16 location in URB Date: Sun, 26 Jan 2025 00:54:05 +0100 Message-ID: <20250125235409.3106594-4-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: qyXH2cwgft0pV76oK8zTS8U0dIDGsXyt X-Proofpoint-GUID: qyXH2cwgft0pV76oK8zTS8U0dIDGsXyt 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=560 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 Original code allowed for the start of NDP16 to be anywhere within the URB based on the `wNdpIndex` value in NTH16. Only the start position of NDP16 was checked, so it was possible for even the fixed-length part of NDP16 to extend past the end of URB, leading to an out-of-bounds read. On iOS devices, the NDP16 header always directly follows NTH16. Rely on and check for this specific format. This, along with NCM-specific minimal URB length check that already exists, will ensure that the fixed-length part of NDP16 plus a set amount of DPEs fit within the URB. Note that this commit alone does not fully address the OoB read. The limit on the amount of DPEs needs to be enforced separately. Fixes: a2d274c62e44 ("usbnet: ipheth: add CDC NCM support") Cc: stable@vger.kernel.org Signed-off-by: Foster Snowhill Reviewed-by: Jakub Kicinski --- v5: No code changes. Added Cc to stable and Reviewed-by Jakub tags. v4: https://lore.kernel.org/netdev/20250105010121.12546-5-forst@pen.gy/ Additionally check that ncmh->wNdpIndex points immediately after NTH16. This covers an unexpected on real devices, and highly unlikely otherwise, case where the bytes after NTH16 are set to `USB_CDC_NCM_NDP16_NOCRC_SIGN`, yet aren't the beginning of the NDP16 header. v3: https://lore.kernel.org/netdev/20241123235432.821220-4-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 | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/drivers/net/usb/ipheth.c b/drivers/net/usb/ipheth.c index 1ff5f7076ad5..c385623596d2 100644 --- a/drivers/net/usb/ipheth.c +++ b/drivers/net/usb/ipheth.c @@ -226,15 +226,14 @@ static int ipheth_rcvbulk_callback_ncm(struct urb *urb) ncmh = urb->transfer_buffer; if (ncmh->dwSignature != cpu_to_le32(USB_CDC_NCM_NTH16_SIGN) || - le16_to_cpu(ncmh->wNdpIndex) >= urb->actual_length) { + /* On iOS, NDP16 directly follows NTH16 */ + ncmh->wNdpIndex != cpu_to_le16(sizeof(struct usb_cdc_ncm_nth16))) { dev->net->stats.rx_errors++; return retval; } - ncm0 = urb->transfer_buffer + le16_to_cpu(ncmh->wNdpIndex); - if (ncm0->dwSignature != cpu_to_le32(USB_CDC_NCM_NDP16_NOCRC_SIGN) || - le16_to_cpu(ncmh->wHeaderLength) + le16_to_cpu(ncm0->wLength) >= - urb->actual_length) { + 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; } 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) From patchwork Sat Jan 25 23:54:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Foster Snowhill X-Patchwork-Id: 13950549 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 4010220A5F3 for ; Sat, 25 Jan 2025 23:55:00 +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=1737849301; cv=none; b=Y5iZX+uqAkh7yyOSeQ/AiAjs3Yz4axrz3AgIr1X8ToMMcFpyQ2oHHrlCHKXnZ/VFnSCpPIiRQGzm/s/XwXtIzoS/IzLOfBS40Q0yE/r6e4IVLrNKXsUT2xvaS9jvmTPWWctTrEBL5j17KoOOj8fsunOxWJjklZl296d1On+TqZw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737849301; c=relaxed/simple; bh=OmUwIl748E1NiRp4NDBMx2YVi2CKtxjAJuOW59bABlI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ibmr1w+yicebbVoOHg2Yhgm3VfmOtF2sj+OewXSKNmDTnLZ7mn5r9EIIUjhLbZRbxpMLBqhiwSO6OISTv6cHVRkU/H79vw2/kCCv/w0It4QGknRW52NodIsSE5XIS7kzdgP8b4VK9zS8BpZ1wFovJOqNHEOPToIIvgEuEM2BhSs= 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=kZDi1mNZ; 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="kZDi1mNZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pen.gy; s=sig1; bh=PD5FeGTwLG0+0QPNJJgudxv+cer6F45p803rYPvQLjI=; h=From:To:Subject:Date:Message-ID:MIME-Version:x-icloud-hme; b=kZDi1mNZKU9eyRgcDoG7trbtkHEuOvriEQlPHN60o80o4nZ8/MywYMBX0XYJZhGmH W0LMDamEp0LFoZmFxnvUHCA4y9DtfXK/NioptDKhDnQNDFQgY/ql/Gvny38a4Gd69i hYY55eLADbgOUZCesmpIwAbDq7DDoxHqeK2hoSr/kKBVI5ykjQIwxA8KXriD4XF242 D/IeAEzxLuPJNPuvs4f8moNM5fom4n8pfFHqPO2RzR+86wcKb0RFMYJEpnV28zNUZ6 G5b/eNv/02i/N5KMtn30Do5JAOfYSjUFCSHZsWU2Wj8N4Aeol8ctjPRsXpslktPt5L q3TGXEa9L8oXw== 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 3E5855AC0129; Sat, 25 Jan 2025 23:54:55 +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 5/7] usbnet: ipheth: break up NCM header size computation Date: Sun, 26 Jan 2025 00:54:07 +0100 Message-ID: <20250125235409.3106594-6-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: NNBvOkX-DEhe-bROStvtWOMObw_B8bA3 X-Proofpoint-GUID: NNBvOkX-DEhe-bROStvtWOMObw_B8bA3 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=446 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 Originally, the total NCM header size was computed as the sum of two vaguely labelled constants. While accurate, it wasn't particularly clear where they were coming from. Use sizes of existing NCM structs where available. Define the total NDP16 size based on the maximum amount of DPEs that can fit into the iOS-specific fixed-size header. This change does not fix any particular issue. Rather, it introduces intermediate constants that will simplify subsequent commits. It should also make it clearer for the reader where the constant values come from. Cc: stable@vger.kernel.org # 6.5.x Signed-off-by: Foster Snowhill Reviewed-by: Jakub Kicinski --- v5: No code changes. Moved patch closer in series to subsequent patch "usbnet: ipheth: fix DPE OoB read", as that is when one of the introduced preprocessor constants is first used. Added Cc to stable and Reviewed-by Jakub tags. v4: https://lore.kernel.org/netdev/20250105010121.12546-2-forst@pen.gy/ No code changes. Remove "Fixes" from the commit message, and clarify the purpose/scope of the commit. v3: https://lore.kernel.org/netdev/20241123235432.821220-1-forst@pen.gy/ * NDP16 header size is computed from max DPE count constant, not the other way around. * Split out from a monolithic patch in v2. 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 | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/net/usb/ipheth.c b/drivers/net/usb/ipheth.c index 069979e2bb6e..03249208612e 100644 --- a/drivers/net/usb/ipheth.c +++ b/drivers/net/usb/ipheth.c @@ -61,7 +61,18 @@ #define IPHETH_USBINTF_PROTO 1 #define IPHETH_IP_ALIGN 2 /* padding at front of URB */ -#define IPHETH_NCM_HEADER_SIZE (12 + 96) /* NCMH + NCM0 */ +/* On iOS devices, NCM headers in RX have a fixed size regardless of DPE count: + * - NTH16 (NCMH): 12 bytes, as per CDC NCM 1.0 spec + * - NDP16 (NCM0): 96 bytes, of which + * - NDP16 fixed header: 8 bytes + * - maximum of 22 DPEs (21 datagrams + trailer), 4 bytes each + */ +#define IPHETH_NDP16_MAX_DPE 22 +#define IPHETH_NDP16_HEADER_SIZE (sizeof(struct usb_cdc_ncm_ndp16) + \ + IPHETH_NDP16_MAX_DPE * \ + sizeof(struct usb_cdc_ncm_dpe16)) +#define IPHETH_NCM_HEADER_SIZE (sizeof(struct usb_cdc_ncm_nth16) + \ + IPHETH_NDP16_HEADER_SIZE) #define IPHETH_TX_BUF_SIZE ETH_FRAME_LEN #define IPHETH_RX_BUF_SIZE_LEGACY (IPHETH_IP_ALIGN + ETH_FRAME_LEN) #define IPHETH_RX_BUF_SIZE_NCM 65536 From patchwork Sat Jan 25 23:54:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Foster Snowhill X-Patchwork-Id: 13950550 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 159BF20A5ED for ; Sat, 25 Jan 2025 23:55:04 +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=1737849305; cv=none; b=I50gjoQEBcXtqjP2Hcb6mUq33A0imVpru6BTYjNjAbUAS+cEK4F7qdPLx8RSWopeHBHdD1HVoTdztM4pEq3iYwQdokNMEZgodgdR8QuBZZ37Yhd/+MwFHzr3thP5ncKlk/WjHvGleo1YhzjFVbhMF5SEwEBINac3HzAztG9oiF8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737849305; c=relaxed/simple; bh=DGuO7TlKUa6pyOpfjBb+ApWsz3kZK2sk4PWVnQ94SeM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lz9xeuUCI9okZxYK/KHalcXgM2xhiYYgRKKrP6sYcdRDrwjltrUBqKt/lc2kyJh26XetLNs/PT1YWRCQlEABbsvRkbKCNVaNvH2hL9lzhpD7GFt4YQv3UBRjwESHzFiTffLNjKm0zmQXeUTK4X5GNqboSYLjhcB5/mvc28VITaI= 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=uZ7qRFSP; 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="uZ7qRFSP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pen.gy; s=sig1; bh=bl4FRmnikIquDofpR5WYLZVKN0mG3Op/eGbhERBKyiI=; h=From:To:Subject:Date:Message-ID:MIME-Version:x-icloud-hme; b=uZ7qRFSPy+E6F8ae6ELwd+VciXOygjWmKiFnxCVe4JlQLtoUJ6K8ex4ajMrp4hJR1 sG8qjJ/JsmJn1oFKZ3oo24/8FqR+zrSVvifH48obpbtnLlw6zsa5ntq454Gu+3MHf8 YMCKNwDOqrZoEh4mPSn5T/VycYqxArJIPLkTBE4SQrmMdGI8TrAeLQpHEVkZjHd+1P nPFsP3Z/E8GoWuSluNwETwNnmDzVln3UcGoYFfSgyjPj6NfBO1I41iX9J6uh3vhyMM 5ow1m3m7vkH0uXJpXjSLoGAnbFPF7W+dDAweglE0SiEZIR+eBlYVrhQ1qqWyK48Upe sbCy9Qbyeumfw== 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 428DD5AC01E7; Sat, 25 Jan 2025 23:54:59 +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 6/7] usbnet: ipheth: fix DPE OoB read Date: Sun, 26 Jan 2025 00:54:08 +0100 Message-ID: <20250125235409.3106594-7-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: kt6AUo67MxJ0SkeWAqT_i0JtkgmaEmQt X-Proofpoint-GUID: kt6AUo67MxJ0SkeWAqT_i0JtkgmaEmQt 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=734 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 Fix an out-of-bounds DPE read, limit the number of processed DPEs to the amount that fits into the fixed-size NDP16 header. Fixes: a2d274c62e44 ("usbnet: ipheth: add CDC NCM support") Cc: stable@vger.kernel.org Signed-off-by: Foster Snowhill Reviewed-by: Jakub Kicinski --- v5: No code changes. Added Cc to stable and Reviewed-by Jakub tags. v4: https://lore.kernel.org/netdev/20250105010121.12546-7-forst@pen.gy/ Split from "usbnet: ipheth: refactor NCM datagram loop, fix DPE OoB read" in v3. This commit is responsible for addressing the potential OoB read. 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 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/net/usb/ipheth.c b/drivers/net/usb/ipheth.c index 03249208612e..5347cd7e295b 100644 --- a/drivers/net/usb/ipheth.c +++ b/drivers/net/usb/ipheth.c @@ -246,7 +246,7 @@ static int ipheth_rcvbulk_callback_ncm(struct urb *urb) goto rx_error; dpe = ncm0->dpe16; - while (true) { + for (int dpe_i = 0; dpe_i < IPHETH_NDP16_MAX_DPE; ++dpe_i, ++dpe) { dg_idx = le16_to_cpu(dpe->wDatagramIndex); dg_len = le16_to_cpu(dpe->wDatagramLength); @@ -268,8 +268,6 @@ static int ipheth_rcvbulk_callback_ncm(struct urb *urb) retval = ipheth_consume_skb(buf, dg_len, dev); if (retval != 0) return retval; - - dpe++; } rx_error: From patchwork Sat Jan 25 23:54:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Foster Snowhill X-Patchwork-Id: 13950551 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 6387C20A5E7 for ; Sat, 25 Jan 2025 23:55:08 +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=1737849310; cv=none; b=HO3+/CzxU4jDL0R7GBXcjnFFRQnal7oWRgaUc/XjTCF/+oqWn8jp+ssHEUCsukvTi8O7uJP3AvjRSJNnDyfGkBbTIlDwdPnqXqOPMhKWS4guXQn5vVu/yGLD2I9Aj989aLGr7bIzzTV5OZniTQyC6xVcSWiqG5DwDoRvBG6u4ZY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737849310; c=relaxed/simple; bh=bJuWLljkVyoePxGrUFio0TJXY/etfCOcNW50W+svjBU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=itN97zX34eOO87xy80YBDi5VrbTQ4agYrw1HMI8Ua1omJXuE8W5vLOG/Ll8bCKMrOP+XTXGcIkXrZI3D5ngU2Rz5wrM5+GwqbRsfhI4Lx+eCzX1XlQv8Ap3nyRJU/jJ6KWV2Y2jD+4VxHBehTITmqVpTWSAhY34o+88LCP73njk= 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=Zp4qXN9e; 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="Zp4qXN9e" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pen.gy; s=sig1; bh=x2y+Y+MmmvlF9GZ+lzwqAiszYOs2lA7NdwGFv67/xjQ=; h=From:To:Subject:Date:Message-ID:MIME-Version:x-icloud-hme; b=Zp4qXN9eON5vMb9Qkv1DKYm3aVJTviSfambmJz+lv6QAuMzh9hQDTZXXL0pba/4t9 ppN3+sh2MGN7rqwSELDAGhkrvId8CFmYn3YOtMi5pRnEJATxlmrt5PIdi6Y41WcZBe SRhOORTHLsmusAvV5bxTIa3vcHruQmnV6bBHtV0Q9Yi1cptSIy9ZFz2jOM5PI2RjI2 f5e9b0uDUmROt2p/DM6ngtr0Ml3hthh9aiFGIMs1Y4pXU6Yq7q/ddEGBoP9xeQ1UBF /5gQ9OzcMXvsZJWZBoJMps320FHAH57Pxp7Qlg261Cw2Gkp40UsC4rwIdeKpTexjMG sBz7LXwbuBiug== 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 27E045AC01D8; Sat, 25 Jan 2025 23:55:03 +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 7/7] usbnet: ipheth: document scope of NCM implementation Date: Sun, 26 Jan 2025 00:54:09 +0100 Message-ID: <20250125235409.3106594-8-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: P8TGLKtoKamGVk0qdEMVbjqCf3hMmMGI X-Proofpoint-GUID: P8TGLKtoKamGVk0qdEMVbjqCf3hMmMGI 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=930 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 Clarify that the "NCM" implementation in `ipheth` is very limited, as iOS devices aren't compatible with the CDC NCM specification in regular tethering mode. For a standards-compliant implementation, one shall turn to the `cdc_ncm` module. Cc: stable@vger.kernel.org # 6.5.x Signed-off-by: Foster Snowhill Reviewed-by: Jakub Kicinski --- v5: No code changes. Added Cc to stable and Reviewed-by Jakub tags. v4: https://lore.kernel.org/netdev/20250105010121.12546-8-forst@pen.gy/ No changes. v3: https://lore.kernel.org/netdev/20241123235432.821220-6-forst@pen.gy/ This comment was part of the commit message for v2. With v3, given how the patches are split up, it makes more sense to add the comment directly in code. 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: n/a --- drivers/net/usb/ipheth.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/net/usb/ipheth.c b/drivers/net/usb/ipheth.c index 5347cd7e295b..a19789b57190 100644 --- a/drivers/net/usb/ipheth.c +++ b/drivers/net/usb/ipheth.c @@ -218,6 +218,14 @@ static int ipheth_rcvbulk_callback_legacy(struct urb *urb) return ipheth_consume_skb(buf, len, dev); } +/* In "NCM mode", the iOS device encapsulates RX (phone->computer) traffic + * in NCM Transfer Blocks (similarly to CDC NCM). However, unlike reverse + * tethering (handled by the `cdc_ncm` driver), regular tethering is not + * compliant with the CDC NCM spec, as the device is missing the necessary + * descriptors, and TX (computer->phone) traffic is not encapsulated + * at all. Thus `ipheth` implements a very limited subset of the spec with + * the sole purpose of parsing RX URBs. + */ static int ipheth_rcvbulk_callback_ncm(struct urb *urb) { struct usb_cdc_ncm_nth16 *ncmh;