From patchwork Sun Jan 5 01:01:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Foster Snowhill X-Patchwork-Id: 855271 Received: from qs51p00im-qukt01080502.me.com (qs51p00im-qukt01080502.me.com [17.57.155.23]) (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 E14971EF01 for ; Sun, 5 Jan 2025 01:02:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.57.155.23 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736038926; cv=none; b=itz+c6SYEgIhVI/TdPeKi/rODRsTQiXwi62ToHxKByAaBSHOIZ16+YKnxEgBgkwtNeLZVzumZvWpsoig4KrCgsFgfFYbSatPp7tzY3Jvfy0RYzR2I5DOoYg85l7lAn6D0QNo5u72gN0szsa9OQV8aIpMWtMaGMO5LkbYzO8Shnk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736038926; c=relaxed/simple; bh=v48SwYQToZkEEuFw38f6V78gYlZ0/JB0VB4uCu3g/6g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bdZxO8d5jyINaRAyBwTM5Ba+C6NOzn7z6Xs23gSpKEdGOoS6NWymNOHsQusCpfyZJ6ZBhBOAKCQxfRWFjNRSgZk7EzWel9GPJF1IoGktKlXj8r9+ZVnfNmtgX0HDPUx7eY3UKLqC43ytlIARzL3F1sJ/NrjPmu2G2fIpwWKtzpg= 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=ev/RzBbr; arc=none smtp.client-ip=17.57.155.23 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="ev/RzBbr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pen.gy; s=sig1; t=1736038924; bh=UBQCEhKGnnf26A1CQv5YwjWX3q/4vt5KvKPq9EdLqT4=; h=From:To:Subject:Date:Message-ID:MIME-Version:x-icloud-hme; b=ev/RzBbrm5Qd0/4504Zt1/Bgy2VQdTzJJajWaYf4sSHv51wZt92pc33QFGIayUbe8 fRbiRUU17Wpd7RZgS64gjKtLstS7Im72EizQkxkp1uUBIGfnzdChw22DlEEYb9PrM1 qCR0mNBy5qujDzFjN4nWDuQuP9FNRIu0Kc3dK79//Wbp75P5fcFmAZA1I5PKNVMXp8 CDXfDM/LdCpEcVhT8UsMyQEsLKsAn2m+EL38GvWwOlBmTzk9VGSXCiXO6SSb1OvuWo vtX5l9B+HUqGcFp5C8oyuSUjx7kAwDKTW/djT0qC2LNGYO9XXPTUhemDlwWgaBAgpi WkM5Ie+kLHxIQ== Received: from fossa.se1.pen.gy (qs51p00im-dlb-asmtp-mailmevip.me.com [17.57.155.28]) by qs51p00im-qukt01080502.me.com (Postfix) with ESMTPSA id A9EB94E4030D; Sun, 5 Jan 2025 01:02:00 +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 v4 2/7] usbnet: ipheth: fix possible overflow in DPE length check Date: Sun, 5 Jan 2025 02:01:16 +0100 Message-ID: <20250105010121.12546-3-forst@pen.gy> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20250105010121.12546-1-forst@pen.gy> References: <20250105010121.12546-1-forst@pen.gy> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-GUID: gs-J8ilmDYTYXLzeDW4-0ncSSaxOu_Ws X-Proofpoint-ORIG-GUID: gs-J8ilmDYTYXLzeDW4-0ncSSaxOu_Ws 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-02_03,2025-01-02_01,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 adultscore=0 mlxlogscore=661 malwarescore=0 phishscore=0 bulkscore=0 suspectscore=0 mlxscore=0 clxscore=1030 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2501050007 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") Signed-off-by: Foster Snowhill --- v4: 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 2084b940b4ea..4b590a5269fd 100644 --- a/drivers/net/usb/ipheth.c +++ b/drivers/net/usb/ipheth.c @@ -254,8 +254,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 Sun Jan 5 01:01:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Foster Snowhill X-Patchwork-Id: 855270 Received: from qs51p00im-qukt01080502.me.com (qs51p00im-qukt01080502.me.com [17.57.155.23]) (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 B2438A920 for ; Sun, 5 Jan 2025 01:02:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.57.155.23 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736038933; cv=none; b=iBxqg9TfxZsUmNhbv1Cl6d8aaHFhrV1jBycsuVfEkiCFuB6A139fgYKtHdafgt748YmGDbxG9GZ1YOOVDiYwTv3EbDXVFwZ21NCWiGYRj2xmXmQnUtZB+dcj3ZgG/0x9FR+FG5w1hfhSN+YoaClPCqavf0F2w5FEi/hsiiNo2kg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736038933; c=relaxed/simple; bh=v08LP/vA38rMGaE48oYXJiKyqTcyaBXnpqHV1cBNF/M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ok7XhaClKp8EDNFcKZqb7V/5ASq2bcVidYbg+WWDH7Qhr84vim+EYK+lv26i3VHdMxtd+jqB/8ArBXjUMcf41JFqRU9OeETqmKef1ICMGpUKJHC7r/K8Kg/CP65eNsUisIERUYiO1957e8VEScWLvHZ/EeBYYhGho48ladQICkk= 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=fkzeQRYQ; arc=none smtp.client-ip=17.57.155.23 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="fkzeQRYQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pen.gy; s=sig1; t=1736038930; bh=jA5Elsv6DJ1PRK/AyDutknBxgJAPX7pUFb8V5z5GRWw=; h=From:To:Subject:Date:Message-ID:MIME-Version:x-icloud-hme; b=fkzeQRYQ/byD/2KFnLzI+zDnZ/HzzutKpSKhtrO8/xoOi+cVk3dB03EWVGxTbgMgQ 2Mn7yA883bzXNd3g/5jlOwfbVOrKSqjvRdhMANzd0henzGSz3dIfb0+y/FUs47bsiW 0UCkpbb3M1I/7AV29Ol164+VKSjUoh9S8IQ8qYqP97AtPlCjbGmhlNIXheIVyOaOGz agNAr4W8Zf9tWTyHHLHHWBf2yIFpJQXi6gdk1mFBE6lqce+FLSfxyVTpG6EVbovUca OItMkgwuQ9g1/8Jn+zyEepxWFQM5gKR+/zm3e8mt1M7+PyQaSOTzZx0sUJvDMmDR6i jvnjThLe7sSzw== Received: from fossa.se1.pen.gy (qs51p00im-dlb-asmtp-mailmevip.me.com [17.57.155.28]) by qs51p00im-qukt01080502.me.com (Postfix) with ESMTPSA id D43B54E40302; Sun, 5 Jan 2025 01:02:07 +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 v4 4/7] usbnet: ipheth: use static NDP16 location in URB Date: Sun, 5 Jan 2025 02:01:18 +0100 Message-ID: <20250105010121.12546-5-forst@pen.gy> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20250105010121.12546-1-forst@pen.gy> References: <20250105010121.12546-1-forst@pen.gy> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-GUID: 19I-srsZhN7ftCtPly6qMe1jvkUlC3kC X-Proofpoint-ORIG-GUID: 19I-srsZhN7ftCtPly6qMe1jvkUlC3kC 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-02_03,2025-01-02_01,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 adultscore=0 mlxlogscore=578 malwarescore=0 phishscore=0 bulkscore=0 suspectscore=0 mlxscore=0 clxscore=1030 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2501050007 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") Signed-off-by: Foster Snowhill --- v4: 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 48c79e69bb7b..35f507db2c4a 100644 --- a/drivers/net/usb/ipheth.c +++ b/drivers/net/usb/ipheth.c @@ -237,15 +237,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 Sun Jan 5 01:01:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Foster Snowhill X-Patchwork-Id: 855269 Received: from qs51p00im-qukt01080502.me.com (qs51p00im-qukt01080502.me.com [17.57.155.23]) (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 A81F349625 for ; Sun, 5 Jan 2025 01:02:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=17.57.155.23 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736038940; cv=none; b=o6dkM4q3gxnPck09sKIUTTInuTb7JTOc0OPIKQnAy17iCTK75xt9etmrSijHpy9x+h9ZutqjsssziFxG9t4TX9IxTTKXIUCdfaB1ZM0qsbpNbax3y70d87qBbDdSvVfFHvG6OEnsoHsBTCXcNHgCFqop3DnT8McTYE7UyQxk1Ag= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736038940; c=relaxed/simple; bh=flyKp3RQ53pkfVwzwBlpnTDsA60UWcGD7KZcCgsPMPk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WjPMU9gVw1RDsS9uB5yeRIZzd8qNxogrPBObuFdGYTB1Xqd33cHqihAjM5pMDRDt3ivWoCTKL4sulkykYaBDfnlTOOh7ihmnR3VlsaJSe3OxLmxINg3b5RKVYmM6CBUakAZ+6xZ/MOwFg1u60G3AH0YCleYyZanG+jzrT6C+IOQ= 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=FXf4HhcR; arc=none smtp.client-ip=17.57.155.23 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="FXf4HhcR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pen.gy; s=sig1; t=1736038937; bh=oWa6tcfAyF//NHcdPXeZ1i6bnr87+mwpPqXxI3ayEQE=; h=From:To:Subject:Date:Message-ID:MIME-Version:x-icloud-hme; b=FXf4HhcRhg0Ih6rNTEEoxohcs0DaamOa2vXvSuBKVQ7iA1R2iaFb3rGevMPibgGYk kOHKrg1aXHktn0BcMpjjOc3LNiBg4bqoVsA595gsGVfhTzi59iJJukqg6SEL9o92AV oDvKs8XC8pWuHUi+BlekgsruiNV4/cgzXGmpvuSmDHn5eUACCBwmgpcOMzoZ8dU5qP DNKPEhkGowt+AZKWTjlfjq4+4CwesplF+Gl4DsPl+mBzh+/JBs4O4aZ1sGzpqBVkBM nyHwrwA2IaUzs3B9fkURaLXr59DMSTtukpThCLgF+esCJ3DCmFn6MHXq/+tfGWIN2o Hbop086Os0e/g== Received: from fossa.se1.pen.gy (qs51p00im-dlb-asmtp-mailmevip.me.com [17.57.155.28]) by qs51p00im-qukt01080502.me.com (Postfix) with ESMTPSA id B43484E4030F; Sun, 5 Jan 2025 01:02:14 +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 v4 6/7] usbnet: ipheth: fix DPE OoB read Date: Sun, 5 Jan 2025 02:01:20 +0100 Message-ID: <20250105010121.12546-7-forst@pen.gy> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20250105010121.12546-1-forst@pen.gy> References: <20250105010121.12546-1-forst@pen.gy> Precedence: bulk X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Proofpoint-GUID: 1wRceMRCbWig1Waecu5rj_D2gdrov75F X-Proofpoint-ORIG-GUID: 1wRceMRCbWig1Waecu5rj_D2gdrov75F 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-02_03,2025-01-02_01,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 adultscore=0 mlxlogscore=738 malwarescore=0 phishscore=0 bulkscore=0 suspectscore=0 mlxscore=0 clxscore=1030 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2308100000 definitions=main-2501050007 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") Signed-off-by: Foster Snowhill --- v4: 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: